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