]>
Commit | Line | Data |
---|---|---|
26f54e9a MA |
1 | /* |
2 | * QEMU Block backends | |
3 | * | |
4 | * Copyright (C) 2014 Red Hat, Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Markus Armbruster <armbru@redhat.com>, | |
8 | * | |
9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 | |
10 | * or later. See the COPYING.LIB file in the top-level directory. | |
11 | */ | |
12 | ||
80c71a24 | 13 | #include "qemu/osdep.h" |
26f54e9a MA |
14 | #include "sysemu/block-backend.h" |
15 | #include "block/block_int.h" | |
373340b2 | 16 | #include "block/blockjob.h" |
281d22d8 | 17 | #include "block/throttle-groups.h" |
18e46a03 | 18 | #include "sysemu/blockdev.h" |
373340b2 | 19 | #include "sysemu/sysemu.h" |
a7f53e26 | 20 | #include "qapi-event.h" |
f348b6d1 | 21 | #include "qemu/id.h" |
a7f53e26 MA |
22 | |
23 | /* Number of coroutines to reserve per attached device model */ | |
24 | #define COROUTINE_POOL_RESERVATION 64 | |
26f54e9a | 25 | |
1bf1cbc9 KW |
26 | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
27 | ||
4981bdec HR |
28 | static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb); |
29 | ||
26f54e9a MA |
30 | struct BlockBackend { |
31 | char *name; | |
32 | int refcnt; | |
f21d96d0 | 33 | BdrvChild *root; |
26f8b3a8 | 34 | DriveInfo *legacy_dinfo; /* null unless created by drive_new() */ |
2cf22d6a | 35 | QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */ |
9492b0b9 | 36 | QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */ |
a7f53e26 MA |
37 | |
38 | void *dev; /* attached device model, if any */ | |
39 | /* TODO change to DeviceState when all users are qdevified */ | |
40 | const BlockDevOps *dev_ops; | |
41 | void *dev_opaque; | |
68e9ec01 HR |
42 | |
43 | /* the block size for which the guest device expects atomicity */ | |
44 | int guest_block_size; | |
7f0e9da6 | 45 | |
281d22d8 HR |
46 | /* If the BDS tree is removed, some of its options are stored here (which |
47 | * can be used to restore those options in the new BDS on insert) */ | |
48 | BlockBackendRootState root_state; | |
49 | ||
bfd18d1e KW |
50 | bool enable_write_cache; |
51 | ||
7f0e9da6 HR |
52 | /* I/O stats (display with "info blockstats"). */ |
53 | BlockAcctStats stats; | |
373340b2 HR |
54 | |
55 | BlockdevOnError on_read_error, on_write_error; | |
56 | bool iostatus_enabled; | |
57 | BlockDeviceIoStatus iostatus; | |
3301f6c6 | 58 | |
c10c9d96 KW |
59 | bool allow_write_beyond_eof; |
60 | ||
3301f6c6 | 61 | NotifierList remove_bs_notifiers, insert_bs_notifiers; |
26f54e9a MA |
62 | }; |
63 | ||
e7f7d676 HR |
64 | typedef struct BlockBackendAIOCB { |
65 | BlockAIOCB common; | |
66 | QEMUBH *bh; | |
4981bdec | 67 | BlockBackend *blk; |
e7f7d676 HR |
68 | int ret; |
69 | } BlockBackendAIOCB; | |
70 | ||
71 | static const AIOCBInfo block_backend_aiocb_info = { | |
4981bdec | 72 | .get_aio_context = blk_aiocb_get_aio_context, |
e7f7d676 HR |
73 | .aiocb_size = sizeof(BlockBackendAIOCB), |
74 | }; | |
75 | ||
8fb3c76c MA |
76 | static void drive_info_del(DriveInfo *dinfo); |
77 | ||
2cf22d6a HR |
78 | /* All BlockBackends */ |
79 | static QTAILQ_HEAD(, BlockBackend) block_backends = | |
80 | QTAILQ_HEAD_INITIALIZER(block_backends); | |
81 | ||
9492b0b9 HR |
82 | /* All BlockBackends referenced by the monitor and which are iterated through by |
83 | * blk_next() */ | |
84 | static QTAILQ_HEAD(, BlockBackend) monitor_block_backends = | |
85 | QTAILQ_HEAD_INITIALIZER(monitor_block_backends); | |
26f54e9a | 86 | |
f21d96d0 KW |
87 | static void blk_root_inherit_options(int *child_flags, QDict *child_options, |
88 | int parent_flags, QDict *parent_options) | |
89 | { | |
90 | /* We're not supposed to call this function for root nodes */ | |
91 | abort(); | |
92 | } | |
93 | ||
94 | static const BdrvChildRole child_root = { | |
95 | .inherit_options = blk_root_inherit_options, | |
96 | }; | |
97 | ||
26f54e9a | 98 | /* |
efaa7c4e | 99 | * Create a new BlockBackend with a reference count of one. |
26f54e9a MA |
100 | * Store an error through @errp on failure, unless it's null. |
101 | * Return the new BlockBackend on success, null on failure. | |
102 | */ | |
efaa7c4e | 103 | BlockBackend *blk_new(Error **errp) |
26f54e9a MA |
104 | { |
105 | BlockBackend *blk; | |
106 | ||
26f54e9a | 107 | blk = g_new0(BlockBackend, 1); |
26f54e9a | 108 | blk->refcnt = 1; |
3301f6c6 HR |
109 | notifier_list_init(&blk->remove_bs_notifiers); |
110 | notifier_list_init(&blk->insert_bs_notifiers); | |
2cf22d6a | 111 | QTAILQ_INSERT_TAIL(&block_backends, blk, link); |
26f54e9a MA |
112 | return blk; |
113 | } | |
114 | ||
7e7d56d9 MA |
115 | /* |
116 | * Create a new BlockBackend with a new BlockDriverState attached. | |
7e7d56d9 MA |
117 | * Otherwise just like blk_new(), which see. |
118 | */ | |
efaa7c4e | 119 | BlockBackend *blk_new_with_bs(Error **errp) |
7e7d56d9 MA |
120 | { |
121 | BlockBackend *blk; | |
122 | BlockDriverState *bs; | |
123 | ||
efaa7c4e | 124 | blk = blk_new(errp); |
7e7d56d9 MA |
125 | if (!blk) { |
126 | return NULL; | |
127 | } | |
128 | ||
7f06d47e | 129 | bs = bdrv_new_root(); |
f21d96d0 | 130 | blk->root = bdrv_root_attach_child(bs, "root", &child_root); |
7e7d56d9 MA |
131 | bs->blk = blk; |
132 | return blk; | |
133 | } | |
134 | ||
ca49a4fd HR |
135 | /* |
136 | * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState. | |
137 | * | |
138 | * Just as with bdrv_open(), after having called this function the reference to | |
139 | * @options belongs to the block layer (even on failure). | |
140 | * | |
141 | * TODO: Remove @filename and @flags; it should be possible to specify a whole | |
142 | * BDS tree just by specifying the @options QDict (or @reference, | |
143 | * alternatively). At the time of adding this function, this is not possible, | |
144 | * though, so callers of this function have to be able to specify @filename and | |
145 | * @flags. | |
146 | */ | |
efaa7c4e HR |
147 | BlockBackend *blk_new_open(const char *filename, const char *reference, |
148 | QDict *options, int flags, Error **errp) | |
ca49a4fd HR |
149 | { |
150 | BlockBackend *blk; | |
151 | int ret; | |
152 | ||
efaa7c4e | 153 | blk = blk_new_with_bs(errp); |
ca49a4fd HR |
154 | if (!blk) { |
155 | QDECREF(options); | |
156 | return NULL; | |
157 | } | |
158 | ||
f21d96d0 | 159 | ret = bdrv_open(&blk->root->bs, filename, reference, options, flags, errp); |
ca49a4fd HR |
160 | if (ret < 0) { |
161 | blk_unref(blk); | |
162 | return NULL; | |
163 | } | |
164 | ||
72e775c7 KW |
165 | blk_set_enable_write_cache(blk, true); |
166 | ||
ca49a4fd HR |
167 | return blk; |
168 | } | |
169 | ||
26f54e9a MA |
170 | static void blk_delete(BlockBackend *blk) |
171 | { | |
172 | assert(!blk->refcnt); | |
e5e78550 | 173 | assert(!blk->name); |
a7f53e26 | 174 | assert(!blk->dev); |
f21d96d0 | 175 | if (blk->root) { |
13855c6b | 176 | blk_remove_bs(blk); |
7e7d56d9 | 177 | } |
3301f6c6 HR |
178 | assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers)); |
179 | assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers)); | |
281d22d8 HR |
180 | if (blk->root_state.throttle_state) { |
181 | g_free(blk->root_state.throttle_group); | |
182 | throttle_group_unref(blk->root_state.throttle_state); | |
183 | } | |
2cf22d6a | 184 | QTAILQ_REMOVE(&block_backends, blk, link); |
18e46a03 | 185 | drive_info_del(blk->legacy_dinfo); |
979e9b03 | 186 | block_acct_cleanup(&blk->stats); |
26f54e9a MA |
187 | g_free(blk); |
188 | } | |
189 | ||
8fb3c76c MA |
190 | static void drive_info_del(DriveInfo *dinfo) |
191 | { | |
192 | if (!dinfo) { | |
193 | return; | |
194 | } | |
195 | qemu_opts_del(dinfo->opts); | |
8fb3c76c MA |
196 | g_free(dinfo->serial); |
197 | g_free(dinfo); | |
198 | } | |
199 | ||
f636ae85 AG |
200 | int blk_get_refcnt(BlockBackend *blk) |
201 | { | |
202 | return blk ? blk->refcnt : 0; | |
203 | } | |
204 | ||
26f54e9a MA |
205 | /* |
206 | * Increment @blk's reference count. | |
207 | * @blk must not be null. | |
208 | */ | |
209 | void blk_ref(BlockBackend *blk) | |
210 | { | |
211 | blk->refcnt++; | |
212 | } | |
213 | ||
214 | /* | |
215 | * Decrement @blk's reference count. | |
216 | * If this drops it to zero, destroy @blk. | |
217 | * For convenience, do nothing if @blk is null. | |
218 | */ | |
219 | void blk_unref(BlockBackend *blk) | |
220 | { | |
221 | if (blk) { | |
222 | assert(blk->refcnt > 0); | |
223 | if (!--blk->refcnt) { | |
224 | blk_delete(blk); | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
2cf22d6a HR |
229 | /* |
230 | * Behaves similarly to blk_next() but iterates over all BlockBackends, even the | |
231 | * ones which are hidden (i.e. are not referenced by the monitor). | |
232 | */ | |
233 | static BlockBackend *blk_all_next(BlockBackend *blk) | |
234 | { | |
235 | return blk ? QTAILQ_NEXT(blk, link) | |
236 | : QTAILQ_FIRST(&block_backends); | |
237 | } | |
238 | ||
d8da3cef HR |
239 | void blk_remove_all_bs(void) |
240 | { | |
74d1b8fc | 241 | BlockBackend *blk = NULL; |
d8da3cef | 242 | |
2cf22d6a | 243 | while ((blk = blk_all_next(blk)) != NULL) { |
d8da3cef HR |
244 | AioContext *ctx = blk_get_aio_context(blk); |
245 | ||
246 | aio_context_acquire(ctx); | |
f21d96d0 | 247 | if (blk->root) { |
d8da3cef HR |
248 | blk_remove_bs(blk); |
249 | } | |
250 | aio_context_release(ctx); | |
251 | } | |
252 | } | |
253 | ||
26f54e9a | 254 | /* |
9492b0b9 | 255 | * Return the monitor-owned BlockBackend after @blk. |
26f54e9a MA |
256 | * If @blk is null, return the first one. |
257 | * Else, return @blk's next sibling, which may be null. | |
258 | * | |
259 | * To iterate over all BlockBackends, do | |
260 | * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { | |
261 | * ... | |
262 | * } | |
263 | */ | |
264 | BlockBackend *blk_next(BlockBackend *blk) | |
265 | { | |
9492b0b9 HR |
266 | return blk ? QTAILQ_NEXT(blk, monitor_link) |
267 | : QTAILQ_FIRST(&monitor_block_backends); | |
26f54e9a MA |
268 | } |
269 | ||
981f4f57 HR |
270 | /* |
271 | * Iterates over all BlockDriverStates which are attached to a BlockBackend. | |
272 | * This function is for use by bdrv_next(). | |
273 | * | |
274 | * @bs must be NULL or a BDS that is attached to a BB. | |
275 | */ | |
276 | BlockDriverState *blk_next_root_bs(BlockDriverState *bs) | |
277 | { | |
278 | BlockBackend *blk; | |
279 | ||
280 | if (bs) { | |
281 | assert(bs->blk); | |
282 | blk = bs->blk; | |
283 | } else { | |
284 | blk = NULL; | |
285 | } | |
286 | ||
287 | do { | |
288 | blk = blk_all_next(blk); | |
f21d96d0 | 289 | } while (blk && !blk->root); |
981f4f57 | 290 | |
f21d96d0 | 291 | return blk ? blk->root->bs : NULL; |
981f4f57 HR |
292 | } |
293 | ||
e5e78550 HR |
294 | /* |
295 | * Add a BlockBackend into the list of backends referenced by the monitor, with | |
296 | * the given @name acting as the handle for the monitor. | |
297 | * Strictly for use by blockdev.c. | |
298 | * | |
299 | * @name must not be null or empty. | |
300 | * | |
301 | * Returns true on success and false on failure. In the latter case, an Error | |
302 | * object is returned through @errp. | |
303 | */ | |
304 | bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp) | |
305 | { | |
306 | assert(!blk->name); | |
307 | assert(name && name[0]); | |
308 | ||
309 | if (!id_wellformed(name)) { | |
310 | error_setg(errp, "Invalid device name"); | |
311 | return false; | |
312 | } | |
313 | if (blk_by_name(name)) { | |
314 | error_setg(errp, "Device with id '%s' already exists", name); | |
315 | return false; | |
316 | } | |
317 | if (bdrv_find_node(name)) { | |
318 | error_setg(errp, | |
319 | "Device name '%s' conflicts with an existing node name", | |
320 | name); | |
321 | return false; | |
322 | } | |
323 | ||
324 | blk->name = g_strdup(name); | |
325 | QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link); | |
326 | return true; | |
327 | } | |
328 | ||
329 | /* | |
330 | * Remove a BlockBackend from the list of backends referenced by the monitor. | |
331 | * Strictly for use by blockdev.c. | |
332 | */ | |
333 | void monitor_remove_blk(BlockBackend *blk) | |
334 | { | |
335 | if (!blk->name) { | |
336 | return; | |
337 | } | |
338 | ||
339 | QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link); | |
340 | g_free(blk->name); | |
341 | blk->name = NULL; | |
342 | } | |
343 | ||
26f54e9a | 344 | /* |
7e7d56d9 | 345 | * Return @blk's name, a non-null string. |
e5e78550 | 346 | * Returns an empty string iff @blk is not referenced by the monitor. |
26f54e9a MA |
347 | */ |
348 | const char *blk_name(BlockBackend *blk) | |
349 | { | |
e5e78550 | 350 | return blk->name ?: ""; |
26f54e9a MA |
351 | } |
352 | ||
353 | /* | |
354 | * Return the BlockBackend with name @name if it exists, else null. | |
355 | * @name must not be null. | |
356 | */ | |
357 | BlockBackend *blk_by_name(const char *name) | |
358 | { | |
74d1b8fc | 359 | BlockBackend *blk = NULL; |
26f54e9a MA |
360 | |
361 | assert(name); | |
74d1b8fc | 362 | while ((blk = blk_next(blk)) != NULL) { |
26f54e9a MA |
363 | if (!strcmp(name, blk->name)) { |
364 | return blk; | |
365 | } | |
366 | } | |
367 | return NULL; | |
368 | } | |
7e7d56d9 MA |
369 | |
370 | /* | |
371 | * Return the BlockDriverState attached to @blk if any, else null. | |
372 | */ | |
373 | BlockDriverState *blk_bs(BlockBackend *blk) | |
374 | { | |
f21d96d0 | 375 | return blk->root ? blk->root->bs : NULL; |
7e7d56d9 MA |
376 | } |
377 | ||
18e46a03 MA |
378 | /* |
379 | * Return @blk's DriveInfo if any, else null. | |
380 | */ | |
381 | DriveInfo *blk_legacy_dinfo(BlockBackend *blk) | |
382 | { | |
383 | return blk->legacy_dinfo; | |
384 | } | |
385 | ||
386 | /* | |
387 | * Set @blk's DriveInfo to @dinfo, and return it. | |
388 | * @blk must not have a DriveInfo set already. | |
389 | * No other BlockBackend may have the same DriveInfo set. | |
390 | */ | |
391 | DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo) | |
392 | { | |
393 | assert(!blk->legacy_dinfo); | |
394 | return blk->legacy_dinfo = dinfo; | |
395 | } | |
396 | ||
397 | /* | |
398 | * Return the BlockBackend with DriveInfo @dinfo. | |
399 | * It must exist. | |
400 | */ | |
401 | BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo) | |
402 | { | |
74d1b8fc | 403 | BlockBackend *blk = NULL; |
18e46a03 | 404 | |
74d1b8fc | 405 | while ((blk = blk_next(blk)) != NULL) { |
18e46a03 MA |
406 | if (blk->legacy_dinfo == dinfo) { |
407 | return blk; | |
408 | } | |
409 | } | |
410 | abort(); | |
411 | } | |
412 | ||
1c95f7e1 HR |
413 | /* |
414 | * Disassociates the currently associated BlockDriverState from @blk. | |
415 | */ | |
416 | void blk_remove_bs(BlockBackend *blk) | |
417 | { | |
f21d96d0 | 418 | assert(blk->root->bs->blk == blk); |
13855c6b | 419 | |
3301f6c6 HR |
420 | notifier_list_notify(&blk->remove_bs_notifiers, blk); |
421 | ||
1c95f7e1 HR |
422 | blk_update_root_state(blk); |
423 | ||
f21d96d0 KW |
424 | blk->root->bs->blk = NULL; |
425 | bdrv_root_unref_child(blk->root); | |
426 | blk->root = NULL; | |
1c95f7e1 HR |
427 | } |
428 | ||
0c3c36d6 HR |
429 | /* |
430 | * Associates a new BlockDriverState with @blk. | |
431 | */ | |
432 | void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs) | |
433 | { | |
f21d96d0 | 434 | assert(!blk->root && !bs->blk); |
0c3c36d6 | 435 | bdrv_ref(bs); |
f21d96d0 | 436 | blk->root = bdrv_root_attach_child(bs, "root", &child_root); |
0c3c36d6 | 437 | bs->blk = blk; |
3301f6c6 HR |
438 | |
439 | notifier_list_notify(&blk->insert_bs_notifiers, blk); | |
0c3c36d6 HR |
440 | } |
441 | ||
a7f53e26 MA |
442 | /* |
443 | * Attach device model @dev to @blk. | |
444 | * Return 0 on success, -EBUSY when a device model is attached already. | |
445 | */ | |
4be74634 | 446 | int blk_attach_dev(BlockBackend *blk, void *dev) |
a7f53e26 | 447 | /* TODO change to DeviceState *dev when all users are qdevified */ |
4be74634 | 448 | { |
a7f53e26 MA |
449 | if (blk->dev) { |
450 | return -EBUSY; | |
451 | } | |
84ebe375 | 452 | blk_ref(blk); |
a7f53e26 | 453 | blk->dev = dev; |
373340b2 | 454 | blk_iostatus_reset(blk); |
a7f53e26 | 455 | return 0; |
4be74634 MA |
456 | } |
457 | ||
a7f53e26 MA |
458 | /* |
459 | * Attach device model @dev to @blk. | |
460 | * @blk must not have a device model attached already. | |
461 | * TODO qdevified devices don't use this, remove when devices are qdevified | |
462 | */ | |
4be74634 MA |
463 | void blk_attach_dev_nofail(BlockBackend *blk, void *dev) |
464 | { | |
a7f53e26 MA |
465 | if (blk_attach_dev(blk, dev) < 0) { |
466 | abort(); | |
467 | } | |
4be74634 MA |
468 | } |
469 | ||
a7f53e26 MA |
470 | /* |
471 | * Detach device model @dev from @blk. | |
472 | * @dev must be currently attached to @blk. | |
473 | */ | |
4be74634 | 474 | void blk_detach_dev(BlockBackend *blk, void *dev) |
a7f53e26 | 475 | /* TODO change to DeviceState *dev when all users are qdevified */ |
4be74634 | 476 | { |
a7f53e26 MA |
477 | assert(blk->dev == dev); |
478 | blk->dev = NULL; | |
479 | blk->dev_ops = NULL; | |
480 | blk->dev_opaque = NULL; | |
68e9ec01 | 481 | blk->guest_block_size = 512; |
84ebe375 | 482 | blk_unref(blk); |
4be74634 MA |
483 | } |
484 | ||
a7f53e26 MA |
485 | /* |
486 | * Return the device model attached to @blk if any, else null. | |
487 | */ | |
4be74634 | 488 | void *blk_get_attached_dev(BlockBackend *blk) |
a7f53e26 MA |
489 | /* TODO change to return DeviceState * when all users are qdevified */ |
490 | { | |
491 | return blk->dev; | |
492 | } | |
493 | ||
494 | /* | |
495 | * Set @blk's device model callbacks to @ops. | |
496 | * @opaque is the opaque argument to pass to the callbacks. | |
497 | * This is for use by device models. | |
498 | */ | |
499 | void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, | |
500 | void *opaque) | |
501 | { | |
502 | blk->dev_ops = ops; | |
503 | blk->dev_opaque = opaque; | |
504 | } | |
505 | ||
506 | /* | |
507 | * Notify @blk's attached device model of media change. | |
508 | * If @load is true, notify of media load. | |
509 | * Else, notify of media eject. | |
510 | * Also send DEVICE_TRAY_MOVED events as appropriate. | |
511 | */ | |
512 | void blk_dev_change_media_cb(BlockBackend *blk, bool load) | |
513 | { | |
514 | if (blk->dev_ops && blk->dev_ops->change_media_cb) { | |
f1f57066 | 515 | bool tray_was_open, tray_is_open; |
a7f53e26 | 516 | |
f1f57066 | 517 | tray_was_open = blk_dev_is_tray_open(blk); |
a7f53e26 | 518 | blk->dev_ops->change_media_cb(blk->dev_opaque, load); |
f1f57066 HR |
519 | tray_is_open = blk_dev_is_tray_open(blk); |
520 | ||
521 | if (tray_was_open != tray_is_open) { | |
522 | qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open, | |
523 | &error_abort); | |
a7f53e26 MA |
524 | } |
525 | } | |
526 | } | |
527 | ||
528 | /* | |
529 | * Does @blk's attached device model have removable media? | |
530 | * %true if no device model is attached. | |
531 | */ | |
532 | bool blk_dev_has_removable_media(BlockBackend *blk) | |
533 | { | |
534 | return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb); | |
535 | } | |
536 | ||
8f3a73bc HR |
537 | /* |
538 | * Does @blk's attached device model have a tray? | |
539 | */ | |
540 | bool blk_dev_has_tray(BlockBackend *blk) | |
541 | { | |
542 | return blk->dev_ops && blk->dev_ops->is_tray_open; | |
543 | } | |
544 | ||
a7f53e26 MA |
545 | /* |
546 | * Notify @blk's attached device model of a media eject request. | |
547 | * If @force is true, the medium is about to be yanked out forcefully. | |
548 | */ | |
549 | void blk_dev_eject_request(BlockBackend *blk, bool force) | |
4be74634 | 550 | { |
a7f53e26 MA |
551 | if (blk->dev_ops && blk->dev_ops->eject_request_cb) { |
552 | blk->dev_ops->eject_request_cb(blk->dev_opaque, force); | |
553 | } | |
4be74634 MA |
554 | } |
555 | ||
a7f53e26 MA |
556 | /* |
557 | * Does @blk's attached device model have a tray, and is it open? | |
558 | */ | |
559 | bool blk_dev_is_tray_open(BlockBackend *blk) | |
4be74634 | 560 | { |
8f3a73bc | 561 | if (blk_dev_has_tray(blk)) { |
a7f53e26 MA |
562 | return blk->dev_ops->is_tray_open(blk->dev_opaque); |
563 | } | |
564 | return false; | |
565 | } | |
566 | ||
567 | /* | |
568 | * Does @blk's attached device model have the medium locked? | |
569 | * %false if the device model has no such lock. | |
570 | */ | |
571 | bool blk_dev_is_medium_locked(BlockBackend *blk) | |
572 | { | |
573 | if (blk->dev_ops && blk->dev_ops->is_medium_locked) { | |
574 | return blk->dev_ops->is_medium_locked(blk->dev_opaque); | |
575 | } | |
576 | return false; | |
577 | } | |
578 | ||
579 | /* | |
580 | * Notify @blk's attached device model of a backend size change. | |
581 | */ | |
582 | void blk_dev_resize_cb(BlockBackend *blk) | |
583 | { | |
584 | if (blk->dev_ops && blk->dev_ops->resize_cb) { | |
585 | blk->dev_ops->resize_cb(blk->dev_opaque); | |
586 | } | |
587 | } | |
588 | ||
589 | void blk_iostatus_enable(BlockBackend *blk) | |
590 | { | |
373340b2 HR |
591 | blk->iostatus_enabled = true; |
592 | blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK; | |
593 | } | |
594 | ||
595 | /* The I/O status is only enabled if the drive explicitly | |
596 | * enables it _and_ the VM is configured to stop on errors */ | |
597 | bool blk_iostatus_is_enabled(const BlockBackend *blk) | |
598 | { | |
599 | return (blk->iostatus_enabled && | |
600 | (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || | |
601 | blk->on_write_error == BLOCKDEV_ON_ERROR_STOP || | |
602 | blk->on_read_error == BLOCKDEV_ON_ERROR_STOP)); | |
603 | } | |
604 | ||
605 | BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk) | |
606 | { | |
607 | return blk->iostatus; | |
608 | } | |
609 | ||
610 | void blk_iostatus_disable(BlockBackend *blk) | |
611 | { | |
612 | blk->iostatus_enabled = false; | |
613 | } | |
614 | ||
615 | void blk_iostatus_reset(BlockBackend *blk) | |
616 | { | |
617 | if (blk_iostatus_is_enabled(blk)) { | |
f21d96d0 | 618 | BlockDriverState *bs = blk_bs(blk); |
373340b2 | 619 | blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
f21d96d0 KW |
620 | if (bs && bs->job) { |
621 | block_job_iostatus_reset(bs->job); | |
373340b2 HR |
622 | } |
623 | } | |
624 | } | |
625 | ||
626 | void blk_iostatus_set_err(BlockBackend *blk, int error) | |
627 | { | |
628 | assert(blk_iostatus_is_enabled(blk)); | |
629 | if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { | |
630 | blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : | |
631 | BLOCK_DEVICE_IO_STATUS_FAILED; | |
632 | } | |
4be74634 MA |
633 | } |
634 | ||
c10c9d96 KW |
635 | void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow) |
636 | { | |
637 | blk->allow_write_beyond_eof = allow; | |
638 | } | |
639 | ||
e7f7d676 HR |
640 | static int blk_check_byte_request(BlockBackend *blk, int64_t offset, |
641 | size_t size) | |
642 | { | |
643 | int64_t len; | |
644 | ||
645 | if (size > INT_MAX) { | |
646 | return -EIO; | |
647 | } | |
648 | ||
c09ba36c | 649 | if (!blk_is_available(blk)) { |
e7f7d676 HR |
650 | return -ENOMEDIUM; |
651 | } | |
652 | ||
e7f7d676 HR |
653 | if (offset < 0) { |
654 | return -EIO; | |
655 | } | |
656 | ||
c10c9d96 KW |
657 | if (!blk->allow_write_beyond_eof) { |
658 | len = blk_getlength(blk); | |
659 | if (len < 0) { | |
660 | return len; | |
661 | } | |
662 | ||
663 | if (offset > len || len - offset < size) { | |
664 | return -EIO; | |
665 | } | |
e7f7d676 HR |
666 | } |
667 | ||
668 | return 0; | |
669 | } | |
670 | ||
671 | static int blk_check_request(BlockBackend *blk, int64_t sector_num, | |
672 | int nb_sectors) | |
673 | { | |
674 | if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) { | |
675 | return -EIO; | |
676 | } | |
677 | ||
678 | if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) { | |
679 | return -EIO; | |
680 | } | |
681 | ||
682 | return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE, | |
683 | nb_sectors * BDRV_SECTOR_SIZE); | |
684 | } | |
685 | ||
1bf1cbc9 KW |
686 | static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset, |
687 | unsigned int bytes, QEMUIOVector *qiov, | |
688 | BdrvRequestFlags flags) | |
4be74634 | 689 | { |
1bf1cbc9 | 690 | int ret = blk_check_byte_request(blk, offset, bytes); |
e7f7d676 HR |
691 | if (ret < 0) { |
692 | return ret; | |
693 | } | |
694 | ||
cab3a356 | 695 | return bdrv_co_preadv(blk_bs(blk), offset, bytes, qiov, flags); |
1bf1cbc9 KW |
696 | } |
697 | ||
a8823a3b KW |
698 | static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset, |
699 | unsigned int bytes, QEMUIOVector *qiov, | |
700 | BdrvRequestFlags flags) | |
701 | { | |
bfd18d1e KW |
702 | int ret; |
703 | ||
704 | ret = blk_check_byte_request(blk, offset, bytes); | |
a8823a3b KW |
705 | if (ret < 0) { |
706 | return ret; | |
707 | } | |
708 | ||
bfd18d1e KW |
709 | if (!blk->enable_write_cache) { |
710 | flags |= BDRV_REQ_FUA; | |
711 | } | |
712 | ||
cab3a356 | 713 | return bdrv_co_pwritev(blk_bs(blk), offset, bytes, qiov, flags); |
a8823a3b KW |
714 | } |
715 | ||
1bf1cbc9 KW |
716 | typedef struct BlkRwCo { |
717 | BlockBackend *blk; | |
718 | int64_t offset; | |
719 | QEMUIOVector *qiov; | |
720 | int ret; | |
721 | BdrvRequestFlags flags; | |
722 | } BlkRwCo; | |
723 | ||
724 | static void blk_read_entry(void *opaque) | |
725 | { | |
726 | BlkRwCo *rwco = opaque; | |
727 | ||
728 | rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size, | |
729 | rwco->qiov, rwco->flags); | |
730 | } | |
731 | ||
a8823a3b KW |
732 | static void blk_write_entry(void *opaque) |
733 | { | |
734 | BlkRwCo *rwco = opaque; | |
735 | ||
736 | rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size, | |
737 | rwco->qiov, rwco->flags); | |
738 | } | |
739 | ||
a55d3fba KW |
740 | static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf, |
741 | int64_t bytes, CoroutineEntry co_entry, | |
742 | BdrvRequestFlags flags) | |
1bf1cbc9 KW |
743 | { |
744 | AioContext *aio_context; | |
745 | QEMUIOVector qiov; | |
746 | struct iovec iov; | |
747 | Coroutine *co; | |
748 | BlkRwCo rwco; | |
749 | ||
1bf1cbc9 KW |
750 | iov = (struct iovec) { |
751 | .iov_base = buf, | |
a55d3fba | 752 | .iov_len = bytes, |
1bf1cbc9 KW |
753 | }; |
754 | qemu_iovec_init_external(&qiov, &iov, 1); | |
755 | ||
756 | rwco = (BlkRwCo) { | |
757 | .blk = blk, | |
a55d3fba | 758 | .offset = offset, |
1bf1cbc9 | 759 | .qiov = &qiov, |
fc1453cd | 760 | .flags = flags, |
1bf1cbc9 KW |
761 | .ret = NOT_DONE, |
762 | }; | |
763 | ||
a8823a3b | 764 | co = qemu_coroutine_create(co_entry); |
1bf1cbc9 KW |
765 | qemu_coroutine_enter(co, &rwco); |
766 | ||
767 | aio_context = blk_get_aio_context(blk); | |
768 | while (rwco.ret == NOT_DONE) { | |
769 | aio_poll(aio_context, true); | |
770 | } | |
771 | ||
772 | return rwco.ret; | |
4be74634 MA |
773 | } |
774 | ||
a55d3fba KW |
775 | static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf, |
776 | int nb_sectors, CoroutineEntry co_entry, | |
777 | BdrvRequestFlags flags) | |
778 | { | |
779 | if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { | |
780 | return -EINVAL; | |
781 | } | |
782 | ||
783 | return blk_prw(blk, sector_num << BDRV_SECTOR_BITS, buf, | |
784 | nb_sectors << BDRV_SECTOR_BITS, co_entry, flags); | |
785 | } | |
786 | ||
a8823a3b KW |
787 | int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf, |
788 | int nb_sectors) | |
789 | { | |
fc1453cd | 790 | return blk_rw(blk, sector_num, buf, nb_sectors, blk_read_entry, 0); |
a8823a3b KW |
791 | } |
792 | ||
b7d17f9f EB |
793 | int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf, |
794 | int count) | |
4be74634 | 795 | { |
5bd51196 | 796 | BlockDriverState *bs = blk_bs(blk); |
5bd51196 KW |
797 | int ret; |
798 | ||
b7d17f9f | 799 | ret = blk_check_byte_request(blk, offset, count); |
e7f7d676 HR |
800 | if (ret < 0) { |
801 | return ret; | |
802 | } | |
803 | ||
ce0f1412 | 804 | bdrv_no_throttling_begin(bs); |
b7d17f9f | 805 | ret = blk_pread(blk, offset, buf, count); |
ce0f1412 | 806 | bdrv_no_throttling_end(bs); |
5bd51196 | 807 | return ret; |
4be74634 MA |
808 | } |
809 | ||
810 | int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf, | |
811 | int nb_sectors) | |
812 | { | |
fc1453cd KW |
813 | return blk_rw(blk, sector_num, (uint8_t*) buf, nb_sectors, |
814 | blk_write_entry, 0); | |
4be74634 MA |
815 | } |
816 | ||
0df89e8e KW |
817 | int blk_write_zeroes(BlockBackend *blk, int64_t sector_num, |
818 | int nb_sectors, BdrvRequestFlags flags) | |
819 | { | |
fc1453cd | 820 | return blk_rw(blk, sector_num, NULL, nb_sectors, blk_write_entry, |
16aaf975 | 821 | flags | BDRV_REQ_ZERO_WRITE); |
0df89e8e KW |
822 | } |
823 | ||
e7f7d676 HR |
824 | static void error_callback_bh(void *opaque) |
825 | { | |
826 | struct BlockBackendAIOCB *acb = opaque; | |
827 | qemu_bh_delete(acb->bh); | |
828 | acb->common.cb(acb->common.opaque, acb->ret); | |
829 | qemu_aio_unref(acb); | |
830 | } | |
831 | ||
ca78ecfa PL |
832 | BlockAIOCB *blk_abort_aio_request(BlockBackend *blk, |
833 | BlockCompletionFunc *cb, | |
834 | void *opaque, int ret) | |
e7f7d676 HR |
835 | { |
836 | struct BlockBackendAIOCB *acb; | |
837 | QEMUBH *bh; | |
838 | ||
839 | acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque); | |
4981bdec | 840 | acb->blk = blk; |
e7f7d676 HR |
841 | acb->ret = ret; |
842 | ||
843 | bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb); | |
844 | acb->bh = bh; | |
845 | qemu_bh_schedule(bh); | |
846 | ||
847 | return &acb->common; | |
848 | } | |
849 | ||
57d6a428 KW |
850 | typedef struct BlkAioEmAIOCB { |
851 | BlockAIOCB common; | |
852 | BlkRwCo rwco; | |
7fa84cd8 | 853 | int bytes; |
57d6a428 KW |
854 | bool has_returned; |
855 | QEMUBH* bh; | |
856 | } BlkAioEmAIOCB; | |
857 | ||
858 | static const AIOCBInfo blk_aio_em_aiocb_info = { | |
859 | .aiocb_size = sizeof(BlkAioEmAIOCB), | |
860 | }; | |
861 | ||
862 | static void blk_aio_complete(BlkAioEmAIOCB *acb) | |
863 | { | |
864 | if (acb->bh) { | |
865 | assert(acb->has_returned); | |
866 | qemu_bh_delete(acb->bh); | |
867 | } | |
868 | if (acb->has_returned) { | |
869 | acb->common.cb(acb->common.opaque, acb->rwco.ret); | |
870 | qemu_aio_unref(acb); | |
871 | } | |
872 | } | |
873 | ||
874 | static void blk_aio_complete_bh(void *opaque) | |
875 | { | |
876 | blk_aio_complete(opaque); | |
877 | } | |
878 | ||
7fa84cd8 | 879 | static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes, |
57d6a428 KW |
880 | QEMUIOVector *qiov, CoroutineEntry co_entry, |
881 | BdrvRequestFlags flags, | |
882 | BlockCompletionFunc *cb, void *opaque) | |
883 | { | |
884 | BlkAioEmAIOCB *acb; | |
885 | Coroutine *co; | |
886 | ||
887 | acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque); | |
888 | acb->rwco = (BlkRwCo) { | |
889 | .blk = blk, | |
890 | .offset = offset, | |
891 | .qiov = qiov, | |
892 | .flags = flags, | |
893 | .ret = NOT_DONE, | |
894 | }; | |
7fa84cd8 | 895 | acb->bytes = bytes; |
57d6a428 KW |
896 | acb->bh = NULL; |
897 | acb->has_returned = false; | |
898 | ||
899 | co = qemu_coroutine_create(co_entry); | |
900 | qemu_coroutine_enter(co, acb); | |
901 | ||
902 | acb->has_returned = true; | |
903 | if (acb->rwco.ret != NOT_DONE) { | |
904 | acb->bh = aio_bh_new(blk_get_aio_context(blk), blk_aio_complete_bh, acb); | |
905 | qemu_bh_schedule(acb->bh); | |
906 | } | |
907 | ||
908 | return &acb->common; | |
909 | } | |
910 | ||
911 | static void blk_aio_read_entry(void *opaque) | |
912 | { | |
913 | BlkAioEmAIOCB *acb = opaque; | |
914 | BlkRwCo *rwco = &acb->rwco; | |
915 | ||
7fa84cd8 KW |
916 | assert(rwco->qiov->size == acb->bytes); |
917 | rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes, | |
57d6a428 KW |
918 | rwco->qiov, rwco->flags); |
919 | blk_aio_complete(acb); | |
920 | } | |
921 | ||
922 | static void blk_aio_write_entry(void *opaque) | |
923 | { | |
924 | BlkAioEmAIOCB *acb = opaque; | |
925 | BlkRwCo *rwco = &acb->rwco; | |
926 | ||
7fa84cd8 KW |
927 | assert(!rwco->qiov || rwco->qiov->size == acb->bytes); |
928 | rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes, | |
57d6a428 KW |
929 | rwco->qiov, rwco->flags); |
930 | blk_aio_complete(acb); | |
931 | } | |
932 | ||
4be74634 MA |
933 | BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num, |
934 | int nb_sectors, BdrvRequestFlags flags, | |
935 | BlockCompletionFunc *cb, void *opaque) | |
936 | { | |
57d6a428 KW |
937 | if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { |
938 | return blk_abort_aio_request(blk, cb, opaque, -EINVAL); | |
e7f7d676 HR |
939 | } |
940 | ||
7fa84cd8 KW |
941 | return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, |
942 | nb_sectors << BDRV_SECTOR_BITS, NULL, | |
16aaf975 KW |
943 | blk_aio_write_entry, flags | BDRV_REQ_ZERO_WRITE, |
944 | cb, opaque); | |
4be74634 MA |
945 | } |
946 | ||
947 | int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count) | |
948 | { | |
a55d3fba | 949 | int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0); |
e7f7d676 HR |
950 | if (ret < 0) { |
951 | return ret; | |
952 | } | |
a55d3fba | 953 | return count; |
4be74634 MA |
954 | } |
955 | ||
8341f00d EB |
956 | int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count, |
957 | BdrvRequestFlags flags) | |
4be74634 | 958 | { |
8341f00d EB |
959 | int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry, |
960 | flags); | |
e7f7d676 HR |
961 | if (ret < 0) { |
962 | return ret; | |
963 | } | |
a55d3fba | 964 | return count; |
4be74634 MA |
965 | } |
966 | ||
967 | int64_t blk_getlength(BlockBackend *blk) | |
968 | { | |
c09ba36c HR |
969 | if (!blk_is_available(blk)) { |
970 | return -ENOMEDIUM; | |
971 | } | |
972 | ||
f21d96d0 | 973 | return bdrv_getlength(blk_bs(blk)); |
4be74634 MA |
974 | } |
975 | ||
976 | void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr) | |
977 | { | |
f21d96d0 | 978 | if (!blk_bs(blk)) { |
a46fc9c9 HR |
979 | *nb_sectors_ptr = 0; |
980 | } else { | |
f21d96d0 | 981 | bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr); |
a46fc9c9 | 982 | } |
4be74634 MA |
983 | } |
984 | ||
1ef01253 HR |
985 | int64_t blk_nb_sectors(BlockBackend *blk) |
986 | { | |
c09ba36c HR |
987 | if (!blk_is_available(blk)) { |
988 | return -ENOMEDIUM; | |
989 | } | |
990 | ||
f21d96d0 | 991 | return bdrv_nb_sectors(blk_bs(blk)); |
1ef01253 HR |
992 | } |
993 | ||
4be74634 MA |
994 | BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num, |
995 | QEMUIOVector *iov, int nb_sectors, | |
996 | BlockCompletionFunc *cb, void *opaque) | |
997 | { | |
57d6a428 KW |
998 | if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { |
999 | return blk_abort_aio_request(blk, cb, opaque, -EINVAL); | |
e7f7d676 HR |
1000 | } |
1001 | ||
7fa84cd8 KW |
1002 | assert(nb_sectors << BDRV_SECTOR_BITS == iov->size); |
1003 | return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, iov->size, iov, | |
57d6a428 | 1004 | blk_aio_read_entry, 0, cb, opaque); |
4be74634 MA |
1005 | } |
1006 | ||
1007 | BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num, | |
1008 | QEMUIOVector *iov, int nb_sectors, | |
1009 | BlockCompletionFunc *cb, void *opaque) | |
1010 | { | |
57d6a428 KW |
1011 | if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { |
1012 | return blk_abort_aio_request(blk, cb, opaque, -EINVAL); | |
e7f7d676 HR |
1013 | } |
1014 | ||
7fa84cd8 KW |
1015 | assert(nb_sectors << BDRV_SECTOR_BITS == iov->size); |
1016 | return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, iov->size, iov, | |
57d6a428 | 1017 | blk_aio_write_entry, 0, cb, opaque); |
4be74634 MA |
1018 | } |
1019 | ||
1020 | BlockAIOCB *blk_aio_flush(BlockBackend *blk, | |
1021 | BlockCompletionFunc *cb, void *opaque) | |
1022 | { | |
c09ba36c | 1023 | if (!blk_is_available(blk)) { |
ca78ecfa | 1024 | return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM); |
c09ba36c HR |
1025 | } |
1026 | ||
f21d96d0 | 1027 | return bdrv_aio_flush(blk_bs(blk), cb, opaque); |
4be74634 MA |
1028 | } |
1029 | ||
1030 | BlockAIOCB *blk_aio_discard(BlockBackend *blk, | |
1031 | int64_t sector_num, int nb_sectors, | |
1032 | BlockCompletionFunc *cb, void *opaque) | |
1033 | { | |
e7f7d676 HR |
1034 | int ret = blk_check_request(blk, sector_num, nb_sectors); |
1035 | if (ret < 0) { | |
ca78ecfa | 1036 | return blk_abort_aio_request(blk, cb, opaque, ret); |
e7f7d676 HR |
1037 | } |
1038 | ||
f21d96d0 | 1039 | return bdrv_aio_discard(blk_bs(blk), sector_num, nb_sectors, cb, opaque); |
4be74634 MA |
1040 | } |
1041 | ||
1042 | void blk_aio_cancel(BlockAIOCB *acb) | |
1043 | { | |
1044 | bdrv_aio_cancel(acb); | |
1045 | } | |
1046 | ||
1047 | void blk_aio_cancel_async(BlockAIOCB *acb) | |
1048 | { | |
1049 | bdrv_aio_cancel_async(acb); | |
1050 | } | |
1051 | ||
1052 | int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs) | |
1053 | { | |
e7f7d676 HR |
1054 | int i, ret; |
1055 | ||
1056 | for (i = 0; i < num_reqs; i++) { | |
1057 | ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors); | |
1058 | if (ret < 0) { | |
1059 | return ret; | |
1060 | } | |
1061 | } | |
1062 | ||
f21d96d0 | 1063 | return bdrv_aio_multiwrite(blk_bs(blk), reqs, num_reqs); |
4be74634 MA |
1064 | } |
1065 | ||
1066 | int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf) | |
1067 | { | |
c09ba36c HR |
1068 | if (!blk_is_available(blk)) { |
1069 | return -ENOMEDIUM; | |
1070 | } | |
1071 | ||
f21d96d0 | 1072 | return bdrv_ioctl(blk_bs(blk), req, buf); |
4be74634 MA |
1073 | } |
1074 | ||
1075 | BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf, | |
1076 | BlockCompletionFunc *cb, void *opaque) | |
1077 | { | |
c09ba36c | 1078 | if (!blk_is_available(blk)) { |
ca78ecfa | 1079 | return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM); |
c09ba36c HR |
1080 | } |
1081 | ||
f21d96d0 | 1082 | return bdrv_aio_ioctl(blk_bs(blk), req, buf, cb, opaque); |
4be74634 MA |
1083 | } |
1084 | ||
2bb0dce7 HR |
1085 | int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) |
1086 | { | |
e7f7d676 HR |
1087 | int ret = blk_check_request(blk, sector_num, nb_sectors); |
1088 | if (ret < 0) { | |
1089 | return ret; | |
1090 | } | |
1091 | ||
f21d96d0 | 1092 | return bdrv_co_discard(blk_bs(blk), sector_num, nb_sectors); |
2bb0dce7 HR |
1093 | } |
1094 | ||
1095 | int blk_co_flush(BlockBackend *blk) | |
1096 | { | |
c09ba36c HR |
1097 | if (!blk_is_available(blk)) { |
1098 | return -ENOMEDIUM; | |
1099 | } | |
1100 | ||
f21d96d0 | 1101 | return bdrv_co_flush(blk_bs(blk)); |
2bb0dce7 HR |
1102 | } |
1103 | ||
4be74634 MA |
1104 | int blk_flush(BlockBackend *blk) |
1105 | { | |
c09ba36c HR |
1106 | if (!blk_is_available(blk)) { |
1107 | return -ENOMEDIUM; | |
1108 | } | |
1109 | ||
f21d96d0 | 1110 | return bdrv_flush(blk_bs(blk)); |
4be74634 MA |
1111 | } |
1112 | ||
97b0385a AY |
1113 | void blk_drain(BlockBackend *blk) |
1114 | { | |
f21d96d0 KW |
1115 | if (blk_bs(blk)) { |
1116 | bdrv_drain(blk_bs(blk)); | |
a46fc9c9 | 1117 | } |
97b0385a AY |
1118 | } |
1119 | ||
4be74634 MA |
1120 | void blk_drain_all(void) |
1121 | { | |
1122 | bdrv_drain_all(); | |
1123 | } | |
1124 | ||
373340b2 HR |
1125 | void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error, |
1126 | BlockdevOnError on_write_error) | |
1127 | { | |
1128 | blk->on_read_error = on_read_error; | |
1129 | blk->on_write_error = on_write_error; | |
1130 | } | |
1131 | ||
4be74634 MA |
1132 | BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read) |
1133 | { | |
373340b2 | 1134 | return is_read ? blk->on_read_error : blk->on_write_error; |
4be74634 MA |
1135 | } |
1136 | ||
1137 | BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read, | |
1138 | int error) | |
1139 | { | |
373340b2 HR |
1140 | BlockdevOnError on_err = blk_get_on_error(blk, is_read); |
1141 | ||
1142 | switch (on_err) { | |
1143 | case BLOCKDEV_ON_ERROR_ENOSPC: | |
1144 | return (error == ENOSPC) ? | |
1145 | BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT; | |
1146 | case BLOCKDEV_ON_ERROR_STOP: | |
1147 | return BLOCK_ERROR_ACTION_STOP; | |
1148 | case BLOCKDEV_ON_ERROR_REPORT: | |
1149 | return BLOCK_ERROR_ACTION_REPORT; | |
1150 | case BLOCKDEV_ON_ERROR_IGNORE: | |
1151 | return BLOCK_ERROR_ACTION_IGNORE; | |
1152 | default: | |
1153 | abort(); | |
1154 | } | |
4be74634 MA |
1155 | } |
1156 | ||
373340b2 HR |
1157 | static void send_qmp_error_event(BlockBackend *blk, |
1158 | BlockErrorAction action, | |
1159 | bool is_read, int error) | |
1160 | { | |
1161 | IoOperationType optype; | |
1162 | ||
1163 | optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE; | |
1164 | qapi_event_send_block_io_error(blk_name(blk), optype, action, | |
1165 | blk_iostatus_is_enabled(blk), | |
1166 | error == ENOSPC, strerror(error), | |
1167 | &error_abort); | |
1168 | } | |
1169 | ||
1170 | /* This is done by device models because, while the block layer knows | |
1171 | * about the error, it does not know whether an operation comes from | |
1172 | * the device or the block layer (from a job, for example). | |
1173 | */ | |
4be74634 MA |
1174 | void blk_error_action(BlockBackend *blk, BlockErrorAction action, |
1175 | bool is_read, int error) | |
1176 | { | |
373340b2 HR |
1177 | assert(error >= 0); |
1178 | ||
1179 | if (action == BLOCK_ERROR_ACTION_STOP) { | |
1180 | /* First set the iostatus, so that "info block" returns an iostatus | |
1181 | * that matches the events raised so far (an additional error iostatus | |
1182 | * is fine, but not a lost one). | |
1183 | */ | |
1184 | blk_iostatus_set_err(blk, error); | |
1185 | ||
1186 | /* Then raise the request to stop the VM and the event. | |
1187 | * qemu_system_vmstop_request_prepare has two effects. First, | |
1188 | * it ensures that the STOP event always comes after the | |
1189 | * BLOCK_IO_ERROR event. Second, it ensures that even if management | |
1190 | * can observe the STOP event and do a "cont" before the STOP | |
1191 | * event is issued, the VM will not stop. In this case, vm_start() | |
1192 | * also ensures that the STOP/RESUME pair of events is emitted. | |
1193 | */ | |
1194 | qemu_system_vmstop_request_prepare(); | |
1195 | send_qmp_error_event(blk, action, is_read, error); | |
1196 | qemu_system_vmstop_request(RUN_STATE_IO_ERROR); | |
1197 | } else { | |
1198 | send_qmp_error_event(blk, action, is_read, error); | |
1199 | } | |
4be74634 MA |
1200 | } |
1201 | ||
1202 | int blk_is_read_only(BlockBackend *blk) | |
1203 | { | |
f21d96d0 KW |
1204 | BlockDriverState *bs = blk_bs(blk); |
1205 | ||
1206 | if (bs) { | |
1207 | return bdrv_is_read_only(bs); | |
061959e8 HR |
1208 | } else { |
1209 | return blk->root_state.read_only; | |
1210 | } | |
4be74634 MA |
1211 | } |
1212 | ||
1213 | int blk_is_sg(BlockBackend *blk) | |
1214 | { | |
f21d96d0 KW |
1215 | BlockDriverState *bs = blk_bs(blk); |
1216 | ||
1217 | if (!bs) { | |
a46fc9c9 HR |
1218 | return 0; |
1219 | } | |
1220 | ||
f21d96d0 | 1221 | return bdrv_is_sg(bs); |
4be74634 MA |
1222 | } |
1223 | ||
1224 | int blk_enable_write_cache(BlockBackend *blk) | |
1225 | { | |
bfd18d1e | 1226 | return blk->enable_write_cache; |
4be74634 MA |
1227 | } |
1228 | ||
1229 | void blk_set_enable_write_cache(BlockBackend *blk, bool wce) | |
1230 | { | |
bfd18d1e | 1231 | blk->enable_write_cache = wce; |
4be74634 MA |
1232 | } |
1233 | ||
2bb0dce7 HR |
1234 | void blk_invalidate_cache(BlockBackend *blk, Error **errp) |
1235 | { | |
f21d96d0 KW |
1236 | BlockDriverState *bs = blk_bs(blk); |
1237 | ||
1238 | if (!bs) { | |
c09ba36c HR |
1239 | error_setg(errp, "Device '%s' has no medium", blk->name); |
1240 | return; | |
1241 | } | |
1242 | ||
f21d96d0 | 1243 | bdrv_invalidate_cache(bs, errp); |
2bb0dce7 HR |
1244 | } |
1245 | ||
e031f750 | 1246 | bool blk_is_inserted(BlockBackend *blk) |
4be74634 | 1247 | { |
f21d96d0 KW |
1248 | BlockDriverState *bs = blk_bs(blk); |
1249 | ||
1250 | return bs && bdrv_is_inserted(bs); | |
db0284f8 HR |
1251 | } |
1252 | ||
1253 | bool blk_is_available(BlockBackend *blk) | |
1254 | { | |
1255 | return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk); | |
4be74634 MA |
1256 | } |
1257 | ||
1258 | void blk_lock_medium(BlockBackend *blk, bool locked) | |
1259 | { | |
f21d96d0 KW |
1260 | BlockDriverState *bs = blk_bs(blk); |
1261 | ||
1262 | if (bs) { | |
1263 | bdrv_lock_medium(bs, locked); | |
a46fc9c9 | 1264 | } |
4be74634 MA |
1265 | } |
1266 | ||
1267 | void blk_eject(BlockBackend *blk, bool eject_flag) | |
1268 | { | |
f21d96d0 KW |
1269 | BlockDriverState *bs = blk_bs(blk); |
1270 | ||
1271 | if (bs) { | |
1272 | bdrv_eject(bs, eject_flag); | |
a46fc9c9 | 1273 | } |
4be74634 MA |
1274 | } |
1275 | ||
1276 | int blk_get_flags(BlockBackend *blk) | |
1277 | { | |
f21d96d0 KW |
1278 | BlockDriverState *bs = blk_bs(blk); |
1279 | ||
1280 | if (bs) { | |
1281 | return bdrv_get_flags(bs); | |
061959e8 HR |
1282 | } else { |
1283 | return blk->root_state.open_flags; | |
1284 | } | |
4be74634 MA |
1285 | } |
1286 | ||
454057b7 PL |
1287 | int blk_get_max_transfer_length(BlockBackend *blk) |
1288 | { | |
f21d96d0 KW |
1289 | BlockDriverState *bs = blk_bs(blk); |
1290 | ||
1291 | if (bs) { | |
1292 | return bs->bl.max_transfer_length; | |
a46fc9c9 HR |
1293 | } else { |
1294 | return 0; | |
1295 | } | |
454057b7 PL |
1296 | } |
1297 | ||
648296e0 SH |
1298 | int blk_get_max_iov(BlockBackend *blk) |
1299 | { | |
f21d96d0 | 1300 | return blk->root->bs->bl.max_iov; |
648296e0 SH |
1301 | } |
1302 | ||
4be74634 MA |
1303 | void blk_set_guest_block_size(BlockBackend *blk, int align) |
1304 | { | |
68e9ec01 | 1305 | blk->guest_block_size = align; |
4be74634 MA |
1306 | } |
1307 | ||
f1c17521 PB |
1308 | void *blk_try_blockalign(BlockBackend *blk, size_t size) |
1309 | { | |
f21d96d0 | 1310 | return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size); |
f1c17521 PB |
1311 | } |
1312 | ||
4be74634 MA |
1313 | void *blk_blockalign(BlockBackend *blk, size_t size) |
1314 | { | |
f21d96d0 | 1315 | return qemu_blockalign(blk ? blk_bs(blk) : NULL, size); |
4be74634 MA |
1316 | } |
1317 | ||
1318 | bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp) | |
1319 | { | |
f21d96d0 KW |
1320 | BlockDriverState *bs = blk_bs(blk); |
1321 | ||
1322 | if (!bs) { | |
a46fc9c9 HR |
1323 | return false; |
1324 | } | |
1325 | ||
f21d96d0 | 1326 | return bdrv_op_is_blocked(bs, op, errp); |
4be74634 MA |
1327 | } |
1328 | ||
1329 | void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason) | |
1330 | { | |
f21d96d0 KW |
1331 | BlockDriverState *bs = blk_bs(blk); |
1332 | ||
1333 | if (bs) { | |
1334 | bdrv_op_unblock(bs, op, reason); | |
a46fc9c9 | 1335 | } |
4be74634 MA |
1336 | } |
1337 | ||
1338 | void blk_op_block_all(BlockBackend *blk, Error *reason) | |
1339 | { | |
f21d96d0 KW |
1340 | BlockDriverState *bs = blk_bs(blk); |
1341 | ||
1342 | if (bs) { | |
1343 | bdrv_op_block_all(bs, reason); | |
a46fc9c9 | 1344 | } |
4be74634 MA |
1345 | } |
1346 | ||
1347 | void blk_op_unblock_all(BlockBackend *blk, Error *reason) | |
1348 | { | |
f21d96d0 KW |
1349 | BlockDriverState *bs = blk_bs(blk); |
1350 | ||
1351 | if (bs) { | |
1352 | bdrv_op_unblock_all(bs, reason); | |
a46fc9c9 | 1353 | } |
4be74634 MA |
1354 | } |
1355 | ||
1356 | AioContext *blk_get_aio_context(BlockBackend *blk) | |
1357 | { | |
f21d96d0 KW |
1358 | BlockDriverState *bs = blk_bs(blk); |
1359 | ||
1360 | if (bs) { | |
1361 | return bdrv_get_aio_context(bs); | |
4981bdec HR |
1362 | } else { |
1363 | return qemu_get_aio_context(); | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb) | |
1368 | { | |
1369 | BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb); | |
1370 | return blk_get_aio_context(blk_acb->blk); | |
4be74634 MA |
1371 | } |
1372 | ||
1373 | void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) | |
1374 | { | |
f21d96d0 KW |
1375 | BlockDriverState *bs = blk_bs(blk); |
1376 | ||
1377 | if (bs) { | |
1378 | bdrv_set_aio_context(bs, new_context); | |
a46fc9c9 | 1379 | } |
4be74634 MA |
1380 | } |
1381 | ||
2019ba0a HR |
1382 | void blk_add_aio_context_notifier(BlockBackend *blk, |
1383 | void (*attached_aio_context)(AioContext *new_context, void *opaque), | |
1384 | void (*detach_aio_context)(void *opaque), void *opaque) | |
1385 | { | |
f21d96d0 KW |
1386 | BlockDriverState *bs = blk_bs(blk); |
1387 | ||
1388 | if (bs) { | |
1389 | bdrv_add_aio_context_notifier(bs, attached_aio_context, | |
a46fc9c9 HR |
1390 | detach_aio_context, opaque); |
1391 | } | |
2019ba0a HR |
1392 | } |
1393 | ||
1394 | void blk_remove_aio_context_notifier(BlockBackend *blk, | |
1395 | void (*attached_aio_context)(AioContext *, | |
1396 | void *), | |
1397 | void (*detach_aio_context)(void *), | |
1398 | void *opaque) | |
1399 | { | |
f21d96d0 KW |
1400 | BlockDriverState *bs = blk_bs(blk); |
1401 | ||
1402 | if (bs) { | |
1403 | bdrv_remove_aio_context_notifier(bs, attached_aio_context, | |
a46fc9c9 HR |
1404 | detach_aio_context, opaque); |
1405 | } | |
2019ba0a HR |
1406 | } |
1407 | ||
3301f6c6 HR |
1408 | void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify) |
1409 | { | |
1410 | notifier_list_add(&blk->remove_bs_notifiers, notify); | |
1411 | } | |
1412 | ||
1413 | void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify) | |
1414 | { | |
1415 | notifier_list_add(&blk->insert_bs_notifiers, notify); | |
1416 | } | |
1417 | ||
4be74634 MA |
1418 | void blk_io_plug(BlockBackend *blk) |
1419 | { | |
f21d96d0 KW |
1420 | BlockDriverState *bs = blk_bs(blk); |
1421 | ||
1422 | if (bs) { | |
1423 | bdrv_io_plug(bs); | |
a46fc9c9 | 1424 | } |
4be74634 MA |
1425 | } |
1426 | ||
1427 | void blk_io_unplug(BlockBackend *blk) | |
1428 | { | |
f21d96d0 KW |
1429 | BlockDriverState *bs = blk_bs(blk); |
1430 | ||
1431 | if (bs) { | |
1432 | bdrv_io_unplug(bs); | |
a46fc9c9 | 1433 | } |
4be74634 MA |
1434 | } |
1435 | ||
1436 | BlockAcctStats *blk_get_stats(BlockBackend *blk) | |
1437 | { | |
7f0e9da6 | 1438 | return &blk->stats; |
4be74634 MA |
1439 | } |
1440 | ||
1441 | void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk, | |
1442 | BlockCompletionFunc *cb, void *opaque) | |
1443 | { | |
1444 | return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque); | |
1445 | } | |
1ef01253 HR |
1446 | |
1447 | int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num, | |
1448 | int nb_sectors, BdrvRequestFlags flags) | |
1449 | { | |
8896e088 KW |
1450 | if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { |
1451 | return -EINVAL; | |
e7f7d676 HR |
1452 | } |
1453 | ||
8896e088 KW |
1454 | return blk_co_pwritev(blk, sector_num << BDRV_SECTOR_BITS, |
1455 | nb_sectors << BDRV_SECTOR_BITS, NULL, | |
16aaf975 | 1456 | flags | BDRV_REQ_ZERO_WRITE); |
1ef01253 HR |
1457 | } |
1458 | ||
1459 | int blk_write_compressed(BlockBackend *blk, int64_t sector_num, | |
1460 | const uint8_t *buf, int nb_sectors) | |
1461 | { | |
e7f7d676 HR |
1462 | int ret = blk_check_request(blk, sector_num, nb_sectors); |
1463 | if (ret < 0) { | |
1464 | return ret; | |
1465 | } | |
1466 | ||
f21d96d0 | 1467 | return bdrv_write_compressed(blk_bs(blk), sector_num, buf, nb_sectors); |
1ef01253 HR |
1468 | } |
1469 | ||
1470 | int blk_truncate(BlockBackend *blk, int64_t offset) | |
1471 | { | |
c09ba36c HR |
1472 | if (!blk_is_available(blk)) { |
1473 | return -ENOMEDIUM; | |
1474 | } | |
1475 | ||
f21d96d0 | 1476 | return bdrv_truncate(blk_bs(blk), offset); |
1ef01253 HR |
1477 | } |
1478 | ||
1479 | int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) | |
1480 | { | |
e7f7d676 HR |
1481 | int ret = blk_check_request(blk, sector_num, nb_sectors); |
1482 | if (ret < 0) { | |
1483 | return ret; | |
1484 | } | |
1485 | ||
f21d96d0 | 1486 | return bdrv_discard(blk_bs(blk), sector_num, nb_sectors); |
1ef01253 HR |
1487 | } |
1488 | ||
1489 | int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, | |
1490 | int64_t pos, int size) | |
1491 | { | |
bfd18d1e KW |
1492 | int ret; |
1493 | ||
c09ba36c HR |
1494 | if (!blk_is_available(blk)) { |
1495 | return -ENOMEDIUM; | |
1496 | } | |
1497 | ||
bfd18d1e KW |
1498 | ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size); |
1499 | if (ret < 0) { | |
1500 | return ret; | |
1501 | } | |
1502 | ||
1503 | if (ret == size && !blk->enable_write_cache) { | |
1504 | ret = bdrv_flush(blk_bs(blk)); | |
1505 | } | |
1506 | ||
1507 | return ret < 0 ? ret : size; | |
1ef01253 HR |
1508 | } |
1509 | ||
1510 | int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size) | |
1511 | { | |
c09ba36c HR |
1512 | if (!blk_is_available(blk)) { |
1513 | return -ENOMEDIUM; | |
1514 | } | |
1515 | ||
f21d96d0 | 1516 | return bdrv_load_vmstate(blk_bs(blk), buf, pos, size); |
1ef01253 | 1517 | } |
f0272c4d ET |
1518 | |
1519 | int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz) | |
1520 | { | |
c09ba36c HR |
1521 | if (!blk_is_available(blk)) { |
1522 | return -ENOMEDIUM; | |
1523 | } | |
1524 | ||
f21d96d0 | 1525 | return bdrv_probe_blocksizes(blk_bs(blk), bsz); |
f0272c4d ET |
1526 | } |
1527 | ||
1528 | int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo) | |
1529 | { | |
c09ba36c HR |
1530 | if (!blk_is_available(blk)) { |
1531 | return -ENOMEDIUM; | |
1532 | } | |
1533 | ||
f21d96d0 | 1534 | return bdrv_probe_geometry(blk_bs(blk), geo); |
f0272c4d | 1535 | } |
281d22d8 HR |
1536 | |
1537 | /* | |
1538 | * Updates the BlockBackendRootState object with data from the currently | |
1539 | * attached BlockDriverState. | |
1540 | */ | |
1541 | void blk_update_root_state(BlockBackend *blk) | |
1542 | { | |
f21d96d0 | 1543 | assert(blk->root); |
281d22d8 | 1544 | |
f21d96d0 KW |
1545 | blk->root_state.open_flags = blk->root->bs->open_flags; |
1546 | blk->root_state.read_only = blk->root->bs->read_only; | |
1547 | blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes; | |
281d22d8 HR |
1548 | |
1549 | if (blk->root_state.throttle_group) { | |
1550 | g_free(blk->root_state.throttle_group); | |
1551 | throttle_group_unref(blk->root_state.throttle_state); | |
1552 | } | |
f21d96d0 KW |
1553 | if (blk->root->bs->throttle_state) { |
1554 | const char *name = throttle_group_get_name(blk->root->bs); | |
281d22d8 HR |
1555 | blk->root_state.throttle_group = g_strdup(name); |
1556 | blk->root_state.throttle_state = throttle_group_incref(name); | |
1557 | } else { | |
1558 | blk->root_state.throttle_group = NULL; | |
1559 | blk->root_state.throttle_state = NULL; | |
1560 | } | |
1561 | } | |
1562 | ||
38cb18f5 HR |
1563 | /* |
1564 | * Applies the information in the root state to the given BlockDriverState. This | |
1565 | * does not include the flags which have to be specified for bdrv_open(), use | |
1566 | * blk_get_open_flags_from_root_state() to inquire them. | |
1567 | */ | |
1568 | void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs) | |
1569 | { | |
1570 | bs->detect_zeroes = blk->root_state.detect_zeroes; | |
1571 | if (blk->root_state.throttle_group) { | |
1572 | bdrv_io_limits_enable(bs, blk->root_state.throttle_group); | |
1573 | } | |
1574 | } | |
1575 | ||
1576 | /* | |
1577 | * Returns the flags to be used for bdrv_open() of a BlockDriverState which is | |
1578 | * supposed to inherit the root state. | |
1579 | */ | |
1580 | int blk_get_open_flags_from_root_state(BlockBackend *blk) | |
1581 | { | |
1582 | int bs_flags; | |
1583 | ||
1584 | bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR; | |
1585 | bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR; | |
1586 | ||
1587 | return bs_flags; | |
1588 | } | |
1589 | ||
281d22d8 HR |
1590 | BlockBackendRootState *blk_get_root_state(BlockBackend *blk) |
1591 | { | |
1592 | return &blk->root_state; | |
1593 | } | |
1393f212 HR |
1594 | |
1595 | int blk_commit_all(void) | |
1596 | { | |
fe1a9cbc HR |
1597 | BlockBackend *blk = NULL; |
1598 | ||
1599 | while ((blk = blk_all_next(blk)) != NULL) { | |
1600 | AioContext *aio_context = blk_get_aio_context(blk); | |
1601 | ||
1602 | aio_context_acquire(aio_context); | |
f21d96d0 KW |
1603 | if (blk_is_inserted(blk) && blk->root->bs->backing) { |
1604 | int ret = bdrv_commit(blk->root->bs); | |
fe1a9cbc HR |
1605 | if (ret < 0) { |
1606 | aio_context_release(aio_context); | |
1607 | return ret; | |
1608 | } | |
1609 | } | |
1610 | aio_context_release(aio_context); | |
1611 | } | |
1612 | return 0; | |
1613 | } | |
1614 | ||
1615 | int blk_flush_all(void) | |
1616 | { | |
1617 | BlockBackend *blk = NULL; | |
1618 | int result = 0; | |
1619 | ||
1620 | while ((blk = blk_all_next(blk)) != NULL) { | |
1621 | AioContext *aio_context = blk_get_aio_context(blk); | |
1622 | int ret; | |
1623 | ||
1624 | aio_context_acquire(aio_context); | |
1625 | if (blk_is_inserted(blk)) { | |
1626 | ret = blk_flush(blk); | |
1627 | if (ret < 0 && !result) { | |
1628 | result = ret; | |
1629 | } | |
1630 | } | |
1631 | aio_context_release(aio_context); | |
1632 | } | |
1633 | ||
1634 | return result; | |
1393f212 | 1635 | } |