xref: /qemu/block/block-backend.c (revision 12b35405)
1 /*
2  * QEMU Block backends
3  *
4  * Copyright (C) 2014-2016 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 
13 #include "qemu/osdep.h"
14 #include "sysemu/block-backend.h"
15 #include "block/block_int.h"
16 #include "block/blockjob.h"
17 #include "block/throttle-groups.h"
18 #include "hw/qdev-core.h"
19 #include "sysemu/blockdev.h"
20 #include "sysemu/runstate.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/replay.h"
23 #include "qapi/error.h"
24 #include "qapi/qapi-events-block.h"
25 #include "qemu/id.h"
26 #include "qemu/main-loop.h"
27 #include "qemu/option.h"
28 #include "trace.h"
29 #include "migration/misc.h"
30 
31 /* Number of coroutines to reserve per attached device model */
32 #define COROUTINE_POOL_RESERVATION 64
33 
34 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
35 
36 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
37 
38 typedef struct BlockBackendAioNotifier {
39     void (*attached_aio_context)(AioContext *new_context, void *opaque);
40     void (*detach_aio_context)(void *opaque);
41     void *opaque;
42     QLIST_ENTRY(BlockBackendAioNotifier) list;
43 } BlockBackendAioNotifier;
44 
45 struct BlockBackend {
46     char *name;
47     int refcnt;
48     BdrvChild *root;
49     AioContext *ctx;
50     DriveInfo *legacy_dinfo;    /* null unless created by drive_new() */
51     QTAILQ_ENTRY(BlockBackend) link;         /* for block_backends */
52     QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
53     BlockBackendPublic public;
54 
55     DeviceState *dev;           /* attached device model, if any */
56     const BlockDevOps *dev_ops;
57     void *dev_opaque;
58 
59     /* the block size for which the guest device expects atomicity */
60     int guest_block_size;
61 
62     /* If the BDS tree is removed, some of its options are stored here (which
63      * can be used to restore those options in the new BDS on insert) */
64     BlockBackendRootState root_state;
65 
66     bool enable_write_cache;
67 
68     /* I/O stats (display with "info blockstats"). */
69     BlockAcctStats stats;
70 
71     BlockdevOnError on_read_error, on_write_error;
72     bool iostatus_enabled;
73     BlockDeviceIoStatus iostatus;
74 
75     uint64_t perm;
76     uint64_t shared_perm;
77     bool disable_perm;
78 
79     bool allow_aio_context_change;
80     bool allow_write_beyond_eof;
81 
82     NotifierList remove_bs_notifiers, insert_bs_notifiers;
83     QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers;
84 
85     int quiesce_counter;
86     CoQueue queued_requests;
87     bool disable_request_queuing;
88 
89     VMChangeStateEntry *vmsh;
90     bool force_allow_inactivate;
91 
92     /* Number of in-flight aio requests.  BlockDriverState also counts
93      * in-flight requests but aio requests can exist even when blk->root is
94      * NULL, so we cannot rely on its counter for that case.
95      * Accessed with atomic ops.
96      */
97     unsigned int in_flight;
98 };
99 
100 typedef struct BlockBackendAIOCB {
101     BlockAIOCB common;
102     BlockBackend *blk;
103     int ret;
104 } BlockBackendAIOCB;
105 
106 static const AIOCBInfo block_backend_aiocb_info = {
107     .get_aio_context = blk_aiocb_get_aio_context,
108     .aiocb_size = sizeof(BlockBackendAIOCB),
109 };
110 
111 static void drive_info_del(DriveInfo *dinfo);
112 static BlockBackend *bdrv_first_blk(BlockDriverState *bs);
113 
114 /* All BlockBackends */
115 static QTAILQ_HEAD(, BlockBackend) block_backends =
116     QTAILQ_HEAD_INITIALIZER(block_backends);
117 
118 /* All BlockBackends referenced by the monitor and which are iterated through by
119  * blk_next() */
120 static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
121     QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
122 
123 static void blk_root_inherit_options(BdrvChildRole role, bool parent_is_format,
124                                      int *child_flags, QDict *child_options,
125                                      int parent_flags, QDict *parent_options)
126 {
127     /* We're not supposed to call this function for root nodes */
128     abort();
129 }
130 static void blk_root_drained_begin(BdrvChild *child);
131 static bool blk_root_drained_poll(BdrvChild *child);
132 static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter);
133 
134 static void blk_root_change_media(BdrvChild *child, bool load);
135 static void blk_root_resize(BdrvChild *child);
136 
137 static bool blk_root_can_set_aio_ctx(BdrvChild *child, AioContext *ctx,
138                                      GSList **ignore, Error **errp);
139 static void blk_root_set_aio_ctx(BdrvChild *child, AioContext *ctx,
140                                  GSList **ignore);
141 
142 static char *blk_root_get_parent_desc(BdrvChild *child)
143 {
144     BlockBackend *blk = child->opaque;
145     char *dev_id;
146 
147     if (blk->name) {
148         return g_strdup(blk->name);
149     }
150 
151     dev_id = blk_get_attached_dev_id(blk);
152     if (*dev_id) {
153         return dev_id;
154     } else {
155         /* TODO Callback into the BB owner for something more detailed */
156         g_free(dev_id);
157         return g_strdup("a block device");
158     }
159 }
160 
161 static const char *blk_root_get_name(BdrvChild *child)
162 {
163     return blk_name(child->opaque);
164 }
165 
166 static void blk_vm_state_changed(void *opaque, int running, RunState state)
167 {
168     Error *local_err = NULL;
169     BlockBackend *blk = opaque;
170 
171     if (state == RUN_STATE_INMIGRATE) {
172         return;
173     }
174 
175     qemu_del_vm_change_state_handler(blk->vmsh);
176     blk->vmsh = NULL;
177     blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
178     if (local_err) {
179         error_report_err(local_err);
180     }
181 }
182 
183 /*
184  * Notifies the user of the BlockBackend that migration has completed. qdev
185  * devices can tighten their permissions in response (specifically revoke
186  * shared write permissions that we needed for storage migration).
187  *
188  * If an error is returned, the VM cannot be allowed to be resumed.
189  */
190 static void blk_root_activate(BdrvChild *child, Error **errp)
191 {
192     BlockBackend *blk = child->opaque;
193     Error *local_err = NULL;
194 
195     if (!blk->disable_perm) {
196         return;
197     }
198 
199     blk->disable_perm = false;
200 
201     blk_set_perm(blk, blk->perm, BLK_PERM_ALL, &local_err);
202     if (local_err) {
203         error_propagate(errp, local_err);
204         blk->disable_perm = true;
205         return;
206     }
207 
208     if (runstate_check(RUN_STATE_INMIGRATE)) {
209         /* Activation can happen when migration process is still active, for
210          * example when nbd_server_add is called during non-shared storage
211          * migration. Defer the shared_perm update to migration completion. */
212         if (!blk->vmsh) {
213             blk->vmsh = qemu_add_vm_change_state_handler(blk_vm_state_changed,
214                                                          blk);
215         }
216         return;
217     }
218 
219     blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
220     if (local_err) {
221         error_propagate(errp, local_err);
222         blk->disable_perm = true;
223         return;
224     }
225 }
226 
227 void blk_set_force_allow_inactivate(BlockBackend *blk)
228 {
229     blk->force_allow_inactivate = true;
230 }
231 
232 static bool blk_can_inactivate(BlockBackend *blk)
233 {
234     /* If it is a guest device, inactivate is ok. */
235     if (blk->dev || blk_name(blk)[0]) {
236         return true;
237     }
238 
239     /* Inactivating means no more writes to the image can be done,
240      * even if those writes would be changes invisible to the
241      * guest.  For block job BBs that satisfy this, we can just allow
242      * it.  This is the case for mirror job source, which is required
243      * by libvirt non-shared block migration. */
244     if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) {
245         return true;
246     }
247 
248     return blk->force_allow_inactivate;
249 }
250 
251 static int blk_root_inactivate(BdrvChild *child)
252 {
253     BlockBackend *blk = child->opaque;
254 
255     if (blk->disable_perm) {
256         return 0;
257     }
258 
259     if (!blk_can_inactivate(blk)) {
260         return -EPERM;
261     }
262 
263     blk->disable_perm = true;
264     if (blk->root) {
265         bdrv_child_try_set_perm(blk->root, 0, BLK_PERM_ALL, &error_abort);
266     }
267 
268     return 0;
269 }
270 
271 static void blk_root_attach(BdrvChild *child)
272 {
273     BlockBackend *blk = child->opaque;
274     BlockBackendAioNotifier *notifier;
275 
276     trace_blk_root_attach(child, blk, child->bs);
277 
278     QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
279         bdrv_add_aio_context_notifier(child->bs,
280                 notifier->attached_aio_context,
281                 notifier->detach_aio_context,
282                 notifier->opaque);
283     }
284 }
285 
286 static void blk_root_detach(BdrvChild *child)
287 {
288     BlockBackend *blk = child->opaque;
289     BlockBackendAioNotifier *notifier;
290 
291     trace_blk_root_detach(child, blk, child->bs);
292 
293     QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
294         bdrv_remove_aio_context_notifier(child->bs,
295                 notifier->attached_aio_context,
296                 notifier->detach_aio_context,
297                 notifier->opaque);
298     }
299 }
300 
301 static const BdrvChildClass child_root = {
302     .inherit_options    = blk_root_inherit_options,
303 
304     .change_media       = blk_root_change_media,
305     .resize             = blk_root_resize,
306     .get_name           = blk_root_get_name,
307     .get_parent_desc    = blk_root_get_parent_desc,
308 
309     .drained_begin      = blk_root_drained_begin,
310     .drained_poll       = blk_root_drained_poll,
311     .drained_end        = blk_root_drained_end,
312 
313     .activate           = blk_root_activate,
314     .inactivate         = blk_root_inactivate,
315 
316     .attach             = blk_root_attach,
317     .detach             = blk_root_detach,
318 
319     .can_set_aio_ctx    = blk_root_can_set_aio_ctx,
320     .set_aio_ctx        = blk_root_set_aio_ctx,
321 };
322 
323 /*
324  * Create a new BlockBackend with a reference count of one.
325  *
326  * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
327  * to request for a block driver node that is attached to this BlockBackend.
328  * @shared_perm is a bitmask which describes which permissions may be granted
329  * to other users of the attached node.
330  * Both sets of permissions can be changed later using blk_set_perm().
331  *
332  * Return the new BlockBackend on success, null on failure.
333  */
334 BlockBackend *blk_new(AioContext *ctx, uint64_t perm, uint64_t shared_perm)
335 {
336     BlockBackend *blk;
337 
338     blk = g_new0(BlockBackend, 1);
339     blk->refcnt = 1;
340     blk->ctx = ctx;
341     blk->perm = perm;
342     blk->shared_perm = shared_perm;
343     blk_set_enable_write_cache(blk, true);
344 
345     blk->on_read_error = BLOCKDEV_ON_ERROR_REPORT;
346     blk->on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
347 
348     block_acct_init(&blk->stats);
349 
350     qemu_co_queue_init(&blk->queued_requests);
351     notifier_list_init(&blk->remove_bs_notifiers);
352     notifier_list_init(&blk->insert_bs_notifiers);
353     QLIST_INIT(&blk->aio_notifiers);
354 
355     QTAILQ_INSERT_TAIL(&block_backends, blk, link);
356     return blk;
357 }
358 
359 /*
360  * Create a new BlockBackend connected to an existing BlockDriverState.
361  *
362  * @perm is a bitmasks of BLK_PERM_* constants which describes the
363  * permissions to request for @bs that is attached to this
364  * BlockBackend.  @shared_perm is a bitmask which describes which
365  * permissions may be granted to other users of the attached node.
366  * Both sets of permissions can be changed later using blk_set_perm().
367  *
368  * Return the new BlockBackend on success, null on failure.
369  */
370 BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm,
371                               uint64_t shared_perm, Error **errp)
372 {
373     BlockBackend *blk = blk_new(bdrv_get_aio_context(bs), perm, shared_perm);
374 
375     if (blk_insert_bs(blk, bs, errp) < 0) {
376         blk_unref(blk);
377         return NULL;
378     }
379     return blk;
380 }
381 
382 /*
383  * Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
384  * The new BlockBackend is in the main AioContext.
385  *
386  * Just as with bdrv_open(), after having called this function the reference to
387  * @options belongs to the block layer (even on failure).
388  *
389  * TODO: Remove @filename and @flags; it should be possible to specify a whole
390  * BDS tree just by specifying the @options QDict (or @reference,
391  * alternatively). At the time of adding this function, this is not possible,
392  * though, so callers of this function have to be able to specify @filename and
393  * @flags.
394  */
395 BlockBackend *blk_new_open(const char *filename, const char *reference,
396                            QDict *options, int flags, Error **errp)
397 {
398     BlockBackend *blk;
399     BlockDriverState *bs;
400     uint64_t perm = 0;
401 
402     /* blk_new_open() is mainly used in .bdrv_create implementations and the
403      * tools where sharing isn't a concern because the BDS stays private, so we
404      * just request permission according to the flags.
405      *
406      * The exceptions are xen_disk and blockdev_init(); in these cases, the
407      * caller of blk_new_open() doesn't make use of the permissions, but they
408      * shouldn't hurt either. We can still share everything here because the
409      * guest devices will add their own blockers if they can't share. */
410     if ((flags & BDRV_O_NO_IO) == 0) {
411         perm |= BLK_PERM_CONSISTENT_READ;
412         if (flags & BDRV_O_RDWR) {
413             perm |= BLK_PERM_WRITE;
414         }
415     }
416     if (flags & BDRV_O_RESIZE) {
417         perm |= BLK_PERM_RESIZE;
418     }
419 
420     blk = blk_new(qemu_get_aio_context(), perm, BLK_PERM_ALL);
421     bs = bdrv_open(filename, reference, options, flags, errp);
422     if (!bs) {
423         blk_unref(blk);
424         return NULL;
425     }
426 
427     blk->root = bdrv_root_attach_child(bs, "root", &child_root,
428                                        BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
429                                        blk->ctx, perm, BLK_PERM_ALL, blk, errp);
430     if (!blk->root) {
431         blk_unref(blk);
432         return NULL;
433     }
434 
435     return blk;
436 }
437 
438 static void blk_delete(BlockBackend *blk)
439 {
440     assert(!blk->refcnt);
441     assert(!blk->name);
442     assert(!blk->dev);
443     if (blk->public.throttle_group_member.throttle_state) {
444         blk_io_limits_disable(blk);
445     }
446     if (blk->root) {
447         blk_remove_bs(blk);
448     }
449     if (blk->vmsh) {
450         qemu_del_vm_change_state_handler(blk->vmsh);
451         blk->vmsh = NULL;
452     }
453     assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
454     assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
455     assert(QLIST_EMPTY(&blk->aio_notifiers));
456     QTAILQ_REMOVE(&block_backends, blk, link);
457     drive_info_del(blk->legacy_dinfo);
458     block_acct_cleanup(&blk->stats);
459     g_free(blk);
460 }
461 
462 static void drive_info_del(DriveInfo *dinfo)
463 {
464     if (!dinfo) {
465         return;
466     }
467     qemu_opts_del(dinfo->opts);
468     g_free(dinfo);
469 }
470 
471 int blk_get_refcnt(BlockBackend *blk)
472 {
473     return blk ? blk->refcnt : 0;
474 }
475 
476 /*
477  * Increment @blk's reference count.
478  * @blk must not be null.
479  */
480 void blk_ref(BlockBackend *blk)
481 {
482     assert(blk->refcnt > 0);
483     blk->refcnt++;
484 }
485 
486 /*
487  * Decrement @blk's reference count.
488  * If this drops it to zero, destroy @blk.
489  * For convenience, do nothing if @blk is null.
490  */
491 void blk_unref(BlockBackend *blk)
492 {
493     if (blk) {
494         assert(blk->refcnt > 0);
495         if (blk->refcnt > 1) {
496             blk->refcnt--;
497         } else {
498             blk_drain(blk);
499             /* blk_drain() cannot resurrect blk, nobody held a reference */
500             assert(blk->refcnt == 1);
501             blk->refcnt = 0;
502             blk_delete(blk);
503         }
504     }
505 }
506 
507 /*
508  * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
509  * ones which are hidden (i.e. are not referenced by the monitor).
510  */
511 BlockBackend *blk_all_next(BlockBackend *blk)
512 {
513     return blk ? QTAILQ_NEXT(blk, link)
514                : QTAILQ_FIRST(&block_backends);
515 }
516 
517 void blk_remove_all_bs(void)
518 {
519     BlockBackend *blk = NULL;
520 
521     while ((blk = blk_all_next(blk)) != NULL) {
522         AioContext *ctx = blk_get_aio_context(blk);
523 
524         aio_context_acquire(ctx);
525         if (blk->root) {
526             blk_remove_bs(blk);
527         }
528         aio_context_release(ctx);
529     }
530 }
531 
532 /*
533  * Return the monitor-owned BlockBackend after @blk.
534  * If @blk is null, return the first one.
535  * Else, return @blk's next sibling, which may be null.
536  *
537  * To iterate over all BlockBackends, do
538  * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
539  *     ...
540  * }
541  */
542 BlockBackend *blk_next(BlockBackend *blk)
543 {
544     return blk ? QTAILQ_NEXT(blk, monitor_link)
545                : QTAILQ_FIRST(&monitor_block_backends);
546 }
547 
548 /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by
549  * the monitor or attached to a BlockBackend */
550 BlockDriverState *bdrv_next(BdrvNextIterator *it)
551 {
552     BlockDriverState *bs, *old_bs;
553 
554     /* Must be called from the main loop */
555     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
556 
557     /* First, return all root nodes of BlockBackends. In order to avoid
558      * returning a BDS twice when multiple BBs refer to it, we only return it
559      * if the BB is the first one in the parent list of the BDS. */
560     if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
561         BlockBackend *old_blk = it->blk;
562 
563         old_bs = old_blk ? blk_bs(old_blk) : NULL;
564 
565         do {
566             it->blk = blk_all_next(it->blk);
567             bs = it->blk ? blk_bs(it->blk) : NULL;
568         } while (it->blk && (bs == NULL || bdrv_first_blk(bs) != it->blk));
569 
570         if (it->blk) {
571             blk_ref(it->blk);
572         }
573         blk_unref(old_blk);
574 
575         if (bs) {
576             bdrv_ref(bs);
577             bdrv_unref(old_bs);
578             return bs;
579         }
580         it->phase = BDRV_NEXT_MONITOR_OWNED;
581     } else {
582         old_bs = it->bs;
583     }
584 
585     /* Then return the monitor-owned BDSes without a BB attached. Ignore all
586      * BDSes that are attached to a BlockBackend here; they have been handled
587      * by the above block already */
588     do {
589         it->bs = bdrv_next_monitor_owned(it->bs);
590         bs = it->bs;
591     } while (bs && bdrv_has_blk(bs));
592 
593     if (bs) {
594         bdrv_ref(bs);
595     }
596     bdrv_unref(old_bs);
597 
598     return bs;
599 }
600 
601 static void bdrv_next_reset(BdrvNextIterator *it)
602 {
603     *it = (BdrvNextIterator) {
604         .phase = BDRV_NEXT_BACKEND_ROOTS,
605     };
606 }
607 
608 BlockDriverState *bdrv_first(BdrvNextIterator *it)
609 {
610     bdrv_next_reset(it);
611     return bdrv_next(it);
612 }
613 
614 /* Must be called when aborting a bdrv_next() iteration before
615  * bdrv_next() returns NULL */
616 void bdrv_next_cleanup(BdrvNextIterator *it)
617 {
618     /* Must be called from the main loop */
619     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
620 
621     if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
622         if (it->blk) {
623             bdrv_unref(blk_bs(it->blk));
624             blk_unref(it->blk);
625         }
626     } else {
627         bdrv_unref(it->bs);
628     }
629 
630     bdrv_next_reset(it);
631 }
632 
633 /*
634  * Add a BlockBackend into the list of backends referenced by the monitor, with
635  * the given @name acting as the handle for the monitor.
636  * Strictly for use by blockdev.c.
637  *
638  * @name must not be null or empty.
639  *
640  * Returns true on success and false on failure. In the latter case, an Error
641  * object is returned through @errp.
642  */
643 bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
644 {
645     assert(!blk->name);
646     assert(name && name[0]);
647 
648     if (!id_wellformed(name)) {
649         error_setg(errp, "Invalid device name");
650         return false;
651     }
652     if (blk_by_name(name)) {
653         error_setg(errp, "Device with id '%s' already exists", name);
654         return false;
655     }
656     if (bdrv_find_node(name)) {
657         error_setg(errp,
658                    "Device name '%s' conflicts with an existing node name",
659                    name);
660         return false;
661     }
662 
663     blk->name = g_strdup(name);
664     QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
665     return true;
666 }
667 
668 /*
669  * Remove a BlockBackend from the list of backends referenced by the monitor.
670  * Strictly for use by blockdev.c.
671  */
672 void monitor_remove_blk(BlockBackend *blk)
673 {
674     if (!blk->name) {
675         return;
676     }
677 
678     QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
679     g_free(blk->name);
680     blk->name = NULL;
681 }
682 
683 /*
684  * Return @blk's name, a non-null string.
685  * Returns an empty string iff @blk is not referenced by the monitor.
686  */
687 const char *blk_name(const BlockBackend *blk)
688 {
689     return blk->name ?: "";
690 }
691 
692 /*
693  * Return the BlockBackend with name @name if it exists, else null.
694  * @name must not be null.
695  */
696 BlockBackend *blk_by_name(const char *name)
697 {
698     BlockBackend *blk = NULL;
699 
700     assert(name);
701     while ((blk = blk_next(blk)) != NULL) {
702         if (!strcmp(name, blk->name)) {
703             return blk;
704         }
705     }
706     return NULL;
707 }
708 
709 /*
710  * Return the BlockDriverState attached to @blk if any, else null.
711  */
712 BlockDriverState *blk_bs(BlockBackend *blk)
713 {
714     return blk->root ? blk->root->bs : NULL;
715 }
716 
717 static BlockBackend *bdrv_first_blk(BlockDriverState *bs)
718 {
719     BdrvChild *child;
720     QLIST_FOREACH(child, &bs->parents, next_parent) {
721         if (child->klass == &child_root) {
722             return child->opaque;
723         }
724     }
725 
726     return NULL;
727 }
728 
729 /*
730  * Returns true if @bs has an associated BlockBackend.
731  */
732 bool bdrv_has_blk(BlockDriverState *bs)
733 {
734     return bdrv_first_blk(bs) != NULL;
735 }
736 
737 /*
738  * Returns true if @bs has only BlockBackends as parents.
739  */
740 bool bdrv_is_root_node(BlockDriverState *bs)
741 {
742     BdrvChild *c;
743 
744     QLIST_FOREACH(c, &bs->parents, next_parent) {
745         if (c->klass != &child_root) {
746             return false;
747         }
748     }
749 
750     return true;
751 }
752 
753 /*
754  * Return @blk's DriveInfo if any, else null.
755  */
756 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
757 {
758     return blk->legacy_dinfo;
759 }
760 
761 /*
762  * Set @blk's DriveInfo to @dinfo, and return it.
763  * @blk must not have a DriveInfo set already.
764  * No other BlockBackend may have the same DriveInfo set.
765  */
766 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
767 {
768     assert(!blk->legacy_dinfo);
769     return blk->legacy_dinfo = dinfo;
770 }
771 
772 /*
773  * Return the BlockBackend with DriveInfo @dinfo.
774  * It must exist.
775  */
776 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
777 {
778     BlockBackend *blk = NULL;
779 
780     while ((blk = blk_next(blk)) != NULL) {
781         if (blk->legacy_dinfo == dinfo) {
782             return blk;
783         }
784     }
785     abort();
786 }
787 
788 /*
789  * Returns a pointer to the publicly accessible fields of @blk.
790  */
791 BlockBackendPublic *blk_get_public(BlockBackend *blk)
792 {
793     return &blk->public;
794 }
795 
796 /*
797  * Returns a BlockBackend given the associated @public fields.
798  */
799 BlockBackend *blk_by_public(BlockBackendPublic *public)
800 {
801     return container_of(public, BlockBackend, public);
802 }
803 
804 /*
805  * Disassociates the currently associated BlockDriverState from @blk.
806  */
807 void blk_remove_bs(BlockBackend *blk)
808 {
809     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
810     BlockDriverState *bs;
811 
812     notifier_list_notify(&blk->remove_bs_notifiers, blk);
813     if (tgm->throttle_state) {
814         bs = blk_bs(blk);
815         bdrv_drained_begin(bs);
816         throttle_group_detach_aio_context(tgm);
817         throttle_group_attach_aio_context(tgm, qemu_get_aio_context());
818         bdrv_drained_end(bs);
819     }
820 
821     blk_update_root_state(blk);
822 
823     /* bdrv_root_unref_child() will cause blk->root to become stale and may
824      * switch to a completion coroutine later on. Let's drain all I/O here
825      * to avoid that and a potential QEMU crash.
826      */
827     blk_drain(blk);
828     bdrv_root_unref_child(blk->root);
829     blk->root = NULL;
830 }
831 
832 /*
833  * Associates a new BlockDriverState with @blk.
834  */
835 int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
836 {
837     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
838     bdrv_ref(bs);
839     blk->root = bdrv_root_attach_child(bs, "root", &child_root,
840                                        BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
841                                        blk->ctx, blk->perm, blk->shared_perm,
842                                        blk, errp);
843     if (blk->root == NULL) {
844         return -EPERM;
845     }
846 
847     notifier_list_notify(&blk->insert_bs_notifiers, blk);
848     if (tgm->throttle_state) {
849         throttle_group_detach_aio_context(tgm);
850         throttle_group_attach_aio_context(tgm, bdrv_get_aio_context(bs));
851     }
852 
853     return 0;
854 }
855 
856 /*
857  * Sets the permission bitmasks that the user of the BlockBackend needs.
858  */
859 int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,
860                  Error **errp)
861 {
862     int ret;
863 
864     if (blk->root && !blk->disable_perm) {
865         ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp);
866         if (ret < 0) {
867             return ret;
868         }
869     }
870 
871     blk->perm = perm;
872     blk->shared_perm = shared_perm;
873 
874     return 0;
875 }
876 
877 void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
878 {
879     *perm = blk->perm;
880     *shared_perm = blk->shared_perm;
881 }
882 
883 /*
884  * Attach device model @dev to @blk.
885  * Return 0 on success, -EBUSY when a device model is attached already.
886  */
887 int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
888 {
889     if (blk->dev) {
890         return -EBUSY;
891     }
892 
893     /* While migration is still incoming, we don't need to apply the
894      * permissions of guest device BlockBackends. We might still have a block
895      * job or NBD server writing to the image for storage migration. */
896     if (runstate_check(RUN_STATE_INMIGRATE)) {
897         blk->disable_perm = true;
898     }
899 
900     blk_ref(blk);
901     blk->dev = dev;
902     blk_iostatus_reset(blk);
903 
904     return 0;
905 }
906 
907 /*
908  * Detach device model @dev from @blk.
909  * @dev must be currently attached to @blk.
910  */
911 void blk_detach_dev(BlockBackend *blk, DeviceState *dev)
912 {
913     assert(blk->dev == dev);
914     blk->dev = NULL;
915     blk->dev_ops = NULL;
916     blk->dev_opaque = NULL;
917     blk->guest_block_size = 512;
918     blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
919     blk_unref(blk);
920 }
921 
922 /*
923  * Return the device model attached to @blk if any, else null.
924  */
925 DeviceState *blk_get_attached_dev(BlockBackend *blk)
926 {
927     return blk->dev;
928 }
929 
930 /* Return the qdev ID, or if no ID is assigned the QOM path, of the block
931  * device attached to the BlockBackend. */
932 char *blk_get_attached_dev_id(BlockBackend *blk)
933 {
934     DeviceState *dev = blk->dev;
935 
936     if (!dev) {
937         return g_strdup("");
938     } else if (dev->id) {
939         return g_strdup(dev->id);
940     }
941 
942     return object_get_canonical_path(OBJECT(dev)) ?: g_strdup("");
943 }
944 
945 /*
946  * Return the BlockBackend which has the device model @dev attached if it
947  * exists, else null.
948  *
949  * @dev must not be null.
950  */
951 BlockBackend *blk_by_dev(void *dev)
952 {
953     BlockBackend *blk = NULL;
954 
955     assert(dev != NULL);
956     while ((blk = blk_all_next(blk)) != NULL) {
957         if (blk->dev == dev) {
958             return blk;
959         }
960     }
961     return NULL;
962 }
963 
964 /*
965  * Set @blk's device model callbacks to @ops.
966  * @opaque is the opaque argument to pass to the callbacks.
967  * This is for use by device models.
968  */
969 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
970                      void *opaque)
971 {
972     blk->dev_ops = ops;
973     blk->dev_opaque = opaque;
974 
975     /* Are we currently quiesced? Should we enforce this right now? */
976     if (blk->quiesce_counter && ops->drained_begin) {
977         ops->drained_begin(opaque);
978     }
979 }
980 
981 /*
982  * Notify @blk's attached device model of media change.
983  *
984  * If @load is true, notify of media load. This action can fail, meaning that
985  * the medium cannot be loaded. @errp is set then.
986  *
987  * If @load is false, notify of media eject. This can never fail.
988  *
989  * Also send DEVICE_TRAY_MOVED events as appropriate.
990  */
991 void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp)
992 {
993     if (blk->dev_ops && blk->dev_ops->change_media_cb) {
994         bool tray_was_open, tray_is_open;
995         Error *local_err = NULL;
996 
997         tray_was_open = blk_dev_is_tray_open(blk);
998         blk->dev_ops->change_media_cb(blk->dev_opaque, load, &local_err);
999         if (local_err) {
1000             assert(load == true);
1001             error_propagate(errp, local_err);
1002             return;
1003         }
1004         tray_is_open = blk_dev_is_tray_open(blk);
1005 
1006         if (tray_was_open != tray_is_open) {
1007             char *id = blk_get_attached_dev_id(blk);
1008             qapi_event_send_device_tray_moved(blk_name(blk), id, tray_is_open);
1009             g_free(id);
1010         }
1011     }
1012 }
1013 
1014 static void blk_root_change_media(BdrvChild *child, bool load)
1015 {
1016     blk_dev_change_media_cb(child->opaque, load, NULL);
1017 }
1018 
1019 /*
1020  * Does @blk's attached device model have removable media?
1021  * %true if no device model is attached.
1022  */
1023 bool blk_dev_has_removable_media(BlockBackend *blk)
1024 {
1025     return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
1026 }
1027 
1028 /*
1029  * Does @blk's attached device model have a tray?
1030  */
1031 bool blk_dev_has_tray(BlockBackend *blk)
1032 {
1033     return blk->dev_ops && blk->dev_ops->is_tray_open;
1034 }
1035 
1036 /*
1037  * Notify @blk's attached device model of a media eject request.
1038  * If @force is true, the medium is about to be yanked out forcefully.
1039  */
1040 void blk_dev_eject_request(BlockBackend *blk, bool force)
1041 {
1042     if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
1043         blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
1044     }
1045 }
1046 
1047 /*
1048  * Does @blk's attached device model have a tray, and is it open?
1049  */
1050 bool blk_dev_is_tray_open(BlockBackend *blk)
1051 {
1052     if (blk_dev_has_tray(blk)) {
1053         return blk->dev_ops->is_tray_open(blk->dev_opaque);
1054     }
1055     return false;
1056 }
1057 
1058 /*
1059  * Does @blk's attached device model have the medium locked?
1060  * %false if the device model has no such lock.
1061  */
1062 bool blk_dev_is_medium_locked(BlockBackend *blk)
1063 {
1064     if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
1065         return blk->dev_ops->is_medium_locked(blk->dev_opaque);
1066     }
1067     return false;
1068 }
1069 
1070 /*
1071  * Notify @blk's attached device model of a backend size change.
1072  */
1073 static void blk_root_resize(BdrvChild *child)
1074 {
1075     BlockBackend *blk = child->opaque;
1076 
1077     if (blk->dev_ops && blk->dev_ops->resize_cb) {
1078         blk->dev_ops->resize_cb(blk->dev_opaque);
1079     }
1080 }
1081 
1082 void blk_iostatus_enable(BlockBackend *blk)
1083 {
1084     blk->iostatus_enabled = true;
1085     blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1086 }
1087 
1088 /* The I/O status is only enabled if the drive explicitly
1089  * enables it _and_ the VM is configured to stop on errors */
1090 bool blk_iostatus_is_enabled(const BlockBackend *blk)
1091 {
1092     return (blk->iostatus_enabled &&
1093            (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
1094             blk->on_write_error == BLOCKDEV_ON_ERROR_STOP   ||
1095             blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
1096 }
1097 
1098 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
1099 {
1100     return blk->iostatus;
1101 }
1102 
1103 void blk_iostatus_disable(BlockBackend *blk)
1104 {
1105     blk->iostatus_enabled = false;
1106 }
1107 
1108 void blk_iostatus_reset(BlockBackend *blk)
1109 {
1110     if (blk_iostatus_is_enabled(blk)) {
1111         blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1112     }
1113 }
1114 
1115 void blk_iostatus_set_err(BlockBackend *blk, int error)
1116 {
1117     assert(blk_iostatus_is_enabled(blk));
1118     if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
1119         blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
1120                                           BLOCK_DEVICE_IO_STATUS_FAILED;
1121     }
1122 }
1123 
1124 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
1125 {
1126     blk->allow_write_beyond_eof = allow;
1127 }
1128 
1129 void blk_set_allow_aio_context_change(BlockBackend *blk, bool allow)
1130 {
1131     blk->allow_aio_context_change = allow;
1132 }
1133 
1134 void blk_set_disable_request_queuing(BlockBackend *blk, bool disable)
1135 {
1136     blk->disable_request_queuing = disable;
1137 }
1138 
1139 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
1140                                   size_t size)
1141 {
1142     int64_t len;
1143 
1144     if (size > INT_MAX) {
1145         return -EIO;
1146     }
1147 
1148     if (!blk_is_available(blk)) {
1149         return -ENOMEDIUM;
1150     }
1151 
1152     if (offset < 0) {
1153         return -EIO;
1154     }
1155 
1156     if (!blk->allow_write_beyond_eof) {
1157         len = blk_getlength(blk);
1158         if (len < 0) {
1159             return len;
1160         }
1161 
1162         if (offset > len || len - offset < size) {
1163             return -EIO;
1164         }
1165     }
1166 
1167     return 0;
1168 }
1169 
1170 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1171 static void coroutine_fn blk_wait_while_drained(BlockBackend *blk)
1172 {
1173     assert(blk->in_flight > 0);
1174 
1175     if (blk->quiesce_counter && !blk->disable_request_queuing) {
1176         blk_dec_in_flight(blk);
1177         qemu_co_queue_wait(&blk->queued_requests, NULL);
1178         blk_inc_in_flight(blk);
1179     }
1180 }
1181 
1182 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1183 static int coroutine_fn
1184 blk_do_preadv(BlockBackend *blk, int64_t offset, unsigned int bytes,
1185               QEMUIOVector *qiov, BdrvRequestFlags flags)
1186 {
1187     int ret;
1188     BlockDriverState *bs;
1189 
1190     blk_wait_while_drained(blk);
1191 
1192     /* Call blk_bs() only after waiting, the graph may have changed */
1193     bs = blk_bs(blk);
1194     trace_blk_co_preadv(blk, bs, offset, bytes, flags);
1195 
1196     ret = blk_check_byte_request(blk, offset, bytes);
1197     if (ret < 0) {
1198         return ret;
1199     }
1200 
1201     bdrv_inc_in_flight(bs);
1202 
1203     /* throttling disk I/O */
1204     if (blk->public.throttle_group_member.throttle_state) {
1205         throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1206                 bytes, false);
1207     }
1208 
1209     ret = bdrv_co_preadv(blk->root, offset, bytes, qiov, flags);
1210     bdrv_dec_in_flight(bs);
1211     return ret;
1212 }
1213 
1214 int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
1215                                unsigned int bytes, QEMUIOVector *qiov,
1216                                BdrvRequestFlags flags)
1217 {
1218     int ret;
1219 
1220     blk_inc_in_flight(blk);
1221     ret = blk_do_preadv(blk, offset, bytes, qiov, flags);
1222     blk_dec_in_flight(blk);
1223 
1224     return ret;
1225 }
1226 
1227 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1228 static int coroutine_fn
1229 blk_do_pwritev_part(BlockBackend *blk, int64_t offset, unsigned int bytes,
1230                     QEMUIOVector *qiov, size_t qiov_offset,
1231                     BdrvRequestFlags flags)
1232 {
1233     int ret;
1234     BlockDriverState *bs;
1235 
1236     blk_wait_while_drained(blk);
1237 
1238     /* Call blk_bs() only after waiting, the graph may have changed */
1239     bs = blk_bs(blk);
1240     trace_blk_co_pwritev(blk, bs, offset, bytes, flags);
1241 
1242     ret = blk_check_byte_request(blk, offset, bytes);
1243     if (ret < 0) {
1244         return ret;
1245     }
1246 
1247     bdrv_inc_in_flight(bs);
1248     /* throttling disk I/O */
1249     if (blk->public.throttle_group_member.throttle_state) {
1250         throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1251                 bytes, true);
1252     }
1253 
1254     if (!blk->enable_write_cache) {
1255         flags |= BDRV_REQ_FUA;
1256     }
1257 
1258     ret = bdrv_co_pwritev_part(blk->root, offset, bytes, qiov, qiov_offset,
1259                                flags);
1260     bdrv_dec_in_flight(bs);
1261     return ret;
1262 }
1263 
1264 int coroutine_fn blk_co_pwritev_part(BlockBackend *blk, int64_t offset,
1265                                      unsigned int bytes,
1266                                      QEMUIOVector *qiov, size_t qiov_offset,
1267                                      BdrvRequestFlags flags)
1268 {
1269     int ret;
1270 
1271     blk_inc_in_flight(blk);
1272     ret = blk_do_pwritev_part(blk, offset, bytes, qiov, qiov_offset, flags);
1273     blk_dec_in_flight(blk);
1274 
1275     return ret;
1276 }
1277 
1278 int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
1279                                 unsigned int bytes, QEMUIOVector *qiov,
1280                                 BdrvRequestFlags flags)
1281 {
1282     return blk_co_pwritev_part(blk, offset, bytes, qiov, 0, flags);
1283 }
1284 
1285 typedef struct BlkRwCo {
1286     BlockBackend *blk;
1287     int64_t offset;
1288     void *iobuf;
1289     int ret;
1290     BdrvRequestFlags flags;
1291 } BlkRwCo;
1292 
1293 static void blk_read_entry(void *opaque)
1294 {
1295     BlkRwCo *rwco = opaque;
1296     QEMUIOVector *qiov = rwco->iobuf;
1297 
1298     rwco->ret = blk_do_preadv(rwco->blk, rwco->offset, qiov->size,
1299                               qiov, rwco->flags);
1300     aio_wait_kick();
1301 }
1302 
1303 static void blk_write_entry(void *opaque)
1304 {
1305     BlkRwCo *rwco = opaque;
1306     QEMUIOVector *qiov = rwco->iobuf;
1307 
1308     rwco->ret = blk_do_pwritev_part(rwco->blk, rwco->offset, qiov->size,
1309                                     qiov, 0, rwco->flags);
1310     aio_wait_kick();
1311 }
1312 
1313 static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
1314                    int64_t bytes, CoroutineEntry co_entry,
1315                    BdrvRequestFlags flags)
1316 {
1317     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
1318     BlkRwCo rwco = {
1319         .blk    = blk,
1320         .offset = offset,
1321         .iobuf  = &qiov,
1322         .flags  = flags,
1323         .ret    = NOT_DONE,
1324     };
1325 
1326     blk_inc_in_flight(blk);
1327     if (qemu_in_coroutine()) {
1328         /* Fast-path if already in coroutine context */
1329         co_entry(&rwco);
1330     } else {
1331         Coroutine *co = qemu_coroutine_create(co_entry, &rwco);
1332         bdrv_coroutine_enter(blk_bs(blk), co);
1333         BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE);
1334     }
1335     blk_dec_in_flight(blk);
1336 
1337     return rwco.ret;
1338 }
1339 
1340 int blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1341                       int bytes, BdrvRequestFlags flags)
1342 {
1343     return blk_prw(blk, offset, NULL, bytes, blk_write_entry,
1344                    flags | BDRV_REQ_ZERO_WRITE);
1345 }
1346 
1347 int blk_make_zero(BlockBackend *blk, BdrvRequestFlags flags)
1348 {
1349     return bdrv_make_zero(blk->root, flags);
1350 }
1351 
1352 void blk_inc_in_flight(BlockBackend *blk)
1353 {
1354     atomic_inc(&blk->in_flight);
1355 }
1356 
1357 void blk_dec_in_flight(BlockBackend *blk)
1358 {
1359     atomic_dec(&blk->in_flight);
1360     aio_wait_kick();
1361 }
1362 
1363 static void error_callback_bh(void *opaque)
1364 {
1365     struct BlockBackendAIOCB *acb = opaque;
1366 
1367     blk_dec_in_flight(acb->blk);
1368     acb->common.cb(acb->common.opaque, acb->ret);
1369     qemu_aio_unref(acb);
1370 }
1371 
1372 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
1373                                   BlockCompletionFunc *cb,
1374                                   void *opaque, int ret)
1375 {
1376     struct BlockBackendAIOCB *acb;
1377 
1378     blk_inc_in_flight(blk);
1379     acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
1380     acb->blk = blk;
1381     acb->ret = ret;
1382 
1383     replay_bh_schedule_oneshot_event(blk_get_aio_context(blk),
1384                                      error_callback_bh, acb);
1385     return &acb->common;
1386 }
1387 
1388 typedef struct BlkAioEmAIOCB {
1389     BlockAIOCB common;
1390     BlkRwCo rwco;
1391     int bytes;
1392     bool has_returned;
1393 } BlkAioEmAIOCB;
1394 
1395 static const AIOCBInfo blk_aio_em_aiocb_info = {
1396     .aiocb_size         = sizeof(BlkAioEmAIOCB),
1397 };
1398 
1399 static void blk_aio_complete(BlkAioEmAIOCB *acb)
1400 {
1401     if (acb->has_returned) {
1402         acb->common.cb(acb->common.opaque, acb->rwco.ret);
1403         blk_dec_in_flight(acb->rwco.blk);
1404         qemu_aio_unref(acb);
1405     }
1406 }
1407 
1408 static void blk_aio_complete_bh(void *opaque)
1409 {
1410     BlkAioEmAIOCB *acb = opaque;
1411     assert(acb->has_returned);
1412     blk_aio_complete(acb);
1413 }
1414 
1415 static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
1416                                 void *iobuf, CoroutineEntry co_entry,
1417                                 BdrvRequestFlags flags,
1418                                 BlockCompletionFunc *cb, void *opaque)
1419 {
1420     BlkAioEmAIOCB *acb;
1421     Coroutine *co;
1422 
1423     blk_inc_in_flight(blk);
1424     acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
1425     acb->rwco = (BlkRwCo) {
1426         .blk    = blk,
1427         .offset = offset,
1428         .iobuf  = iobuf,
1429         .flags  = flags,
1430         .ret    = NOT_DONE,
1431     };
1432     acb->bytes = bytes;
1433     acb->has_returned = false;
1434 
1435     co = qemu_coroutine_create(co_entry, acb);
1436     bdrv_coroutine_enter(blk_bs(blk), co);
1437 
1438     acb->has_returned = true;
1439     if (acb->rwco.ret != NOT_DONE) {
1440         replay_bh_schedule_oneshot_event(blk_get_aio_context(blk),
1441                                          blk_aio_complete_bh, acb);
1442     }
1443 
1444     return &acb->common;
1445 }
1446 
1447 static void blk_aio_read_entry(void *opaque)
1448 {
1449     BlkAioEmAIOCB *acb = opaque;
1450     BlkRwCo *rwco = &acb->rwco;
1451     QEMUIOVector *qiov = rwco->iobuf;
1452 
1453     assert(qiov->size == acb->bytes);
1454     rwco->ret = blk_do_preadv(rwco->blk, rwco->offset, acb->bytes,
1455                               qiov, rwco->flags);
1456     blk_aio_complete(acb);
1457 }
1458 
1459 static void blk_aio_write_entry(void *opaque)
1460 {
1461     BlkAioEmAIOCB *acb = opaque;
1462     BlkRwCo *rwco = &acb->rwco;
1463     QEMUIOVector *qiov = rwco->iobuf;
1464 
1465     assert(!qiov || qiov->size == acb->bytes);
1466     rwco->ret = blk_do_pwritev_part(rwco->blk, rwco->offset, acb->bytes,
1467                                     qiov, 0, rwco->flags);
1468     blk_aio_complete(acb);
1469 }
1470 
1471 BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1472                                   int count, BdrvRequestFlags flags,
1473                                   BlockCompletionFunc *cb, void *opaque)
1474 {
1475     return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
1476                         flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
1477 }
1478 
1479 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
1480 {
1481     int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
1482     if (ret < 0) {
1483         return ret;
1484     }
1485     return count;
1486 }
1487 
1488 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
1489                BdrvRequestFlags flags)
1490 {
1491     int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
1492                       flags);
1493     if (ret < 0) {
1494         return ret;
1495     }
1496     return count;
1497 }
1498 
1499 int64_t blk_getlength(BlockBackend *blk)
1500 {
1501     if (!blk_is_available(blk)) {
1502         return -ENOMEDIUM;
1503     }
1504 
1505     return bdrv_getlength(blk_bs(blk));
1506 }
1507 
1508 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
1509 {
1510     if (!blk_bs(blk)) {
1511         *nb_sectors_ptr = 0;
1512     } else {
1513         bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
1514     }
1515 }
1516 
1517 int64_t blk_nb_sectors(BlockBackend *blk)
1518 {
1519     if (!blk_is_available(blk)) {
1520         return -ENOMEDIUM;
1521     }
1522 
1523     return bdrv_nb_sectors(blk_bs(blk));
1524 }
1525 
1526 BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
1527                            QEMUIOVector *qiov, BdrvRequestFlags flags,
1528                            BlockCompletionFunc *cb, void *opaque)
1529 {
1530     return blk_aio_prwv(blk, offset, qiov->size, qiov,
1531                         blk_aio_read_entry, flags, cb, opaque);
1532 }
1533 
1534 BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
1535                             QEMUIOVector *qiov, BdrvRequestFlags flags,
1536                             BlockCompletionFunc *cb, void *opaque)
1537 {
1538     return blk_aio_prwv(blk, offset, qiov->size, qiov,
1539                         blk_aio_write_entry, flags, cb, opaque);
1540 }
1541 
1542 void blk_aio_cancel(BlockAIOCB *acb)
1543 {
1544     bdrv_aio_cancel(acb);
1545 }
1546 
1547 void blk_aio_cancel_async(BlockAIOCB *acb)
1548 {
1549     bdrv_aio_cancel_async(acb);
1550 }
1551 
1552 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1553 static int coroutine_fn
1554 blk_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1555 {
1556     blk_wait_while_drained(blk);
1557 
1558     if (!blk_is_available(blk)) {
1559         return -ENOMEDIUM;
1560     }
1561 
1562     return bdrv_co_ioctl(blk_bs(blk), req, buf);
1563 }
1564 
1565 static void blk_ioctl_entry(void *opaque)
1566 {
1567     BlkRwCo *rwco = opaque;
1568     QEMUIOVector *qiov = rwco->iobuf;
1569 
1570     rwco->ret = blk_do_ioctl(rwco->blk, rwco->offset, qiov->iov[0].iov_base);
1571     aio_wait_kick();
1572 }
1573 
1574 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1575 {
1576     return blk_prw(blk, req, buf, 0, blk_ioctl_entry, 0);
1577 }
1578 
1579 static void blk_aio_ioctl_entry(void *opaque)
1580 {
1581     BlkAioEmAIOCB *acb = opaque;
1582     BlkRwCo *rwco = &acb->rwco;
1583 
1584     rwco->ret = blk_do_ioctl(rwco->blk, rwco->offset, rwco->iobuf);
1585 
1586     blk_aio_complete(acb);
1587 }
1588 
1589 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
1590                           BlockCompletionFunc *cb, void *opaque)
1591 {
1592     return blk_aio_prwv(blk, req, 0, buf, blk_aio_ioctl_entry, 0, cb, opaque);
1593 }
1594 
1595 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1596 static int coroutine_fn
1597 blk_do_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1598 {
1599     int ret;
1600 
1601     blk_wait_while_drained(blk);
1602 
1603     ret = blk_check_byte_request(blk, offset, bytes);
1604     if (ret < 0) {
1605         return ret;
1606     }
1607 
1608     return bdrv_co_pdiscard(blk->root, offset, bytes);
1609 }
1610 
1611 static void blk_aio_pdiscard_entry(void *opaque)
1612 {
1613     BlkAioEmAIOCB *acb = opaque;
1614     BlkRwCo *rwco = &acb->rwco;
1615 
1616     rwco->ret = blk_do_pdiscard(rwco->blk, rwco->offset, acb->bytes);
1617     blk_aio_complete(acb);
1618 }
1619 
1620 BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk,
1621                              int64_t offset, int bytes,
1622                              BlockCompletionFunc *cb, void *opaque)
1623 {
1624     return blk_aio_prwv(blk, offset, bytes, NULL, blk_aio_pdiscard_entry, 0,
1625                         cb, opaque);
1626 }
1627 
1628 int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1629 {
1630     int ret;
1631 
1632     blk_inc_in_flight(blk);
1633     ret = blk_do_pdiscard(blk, offset, bytes);
1634     blk_dec_in_flight(blk);
1635 
1636     return ret;
1637 }
1638 
1639 static void blk_pdiscard_entry(void *opaque)
1640 {
1641     BlkRwCo *rwco = opaque;
1642     QEMUIOVector *qiov = rwco->iobuf;
1643 
1644     rwco->ret = blk_do_pdiscard(rwco->blk, rwco->offset, qiov->size);
1645     aio_wait_kick();
1646 }
1647 
1648 int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1649 {
1650     return blk_prw(blk, offset, NULL, bytes, blk_pdiscard_entry, 0);
1651 }
1652 
1653 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1654 static int coroutine_fn blk_do_flush(BlockBackend *blk)
1655 {
1656     blk_wait_while_drained(blk);
1657 
1658     if (!blk_is_available(blk)) {
1659         return -ENOMEDIUM;
1660     }
1661 
1662     return bdrv_co_flush(blk_bs(blk));
1663 }
1664 
1665 static void blk_aio_flush_entry(void *opaque)
1666 {
1667     BlkAioEmAIOCB *acb = opaque;
1668     BlkRwCo *rwco = &acb->rwco;
1669 
1670     rwco->ret = blk_do_flush(rwco->blk);
1671     blk_aio_complete(acb);
1672 }
1673 
1674 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1675                           BlockCompletionFunc *cb, void *opaque)
1676 {
1677     return blk_aio_prwv(blk, 0, 0, NULL, blk_aio_flush_entry, 0, cb, opaque);
1678 }
1679 
1680 int coroutine_fn blk_co_flush(BlockBackend *blk)
1681 {
1682     int ret;
1683 
1684     blk_inc_in_flight(blk);
1685     ret = blk_do_flush(blk);
1686     blk_dec_in_flight(blk);
1687 
1688     return ret;
1689 }
1690 
1691 static void blk_flush_entry(void *opaque)
1692 {
1693     BlkRwCo *rwco = opaque;
1694     rwco->ret = blk_do_flush(rwco->blk);
1695     aio_wait_kick();
1696 }
1697 
1698 int blk_flush(BlockBackend *blk)
1699 {
1700     return blk_prw(blk, 0, NULL, 0, blk_flush_entry, 0);
1701 }
1702 
1703 void blk_drain(BlockBackend *blk)
1704 {
1705     BlockDriverState *bs = blk_bs(blk);
1706 
1707     if (bs) {
1708         bdrv_drained_begin(bs);
1709     }
1710 
1711     /* We may have -ENOMEDIUM completions in flight */
1712     AIO_WAIT_WHILE(blk_get_aio_context(blk),
1713                    atomic_mb_read(&blk->in_flight) > 0);
1714 
1715     if (bs) {
1716         bdrv_drained_end(bs);
1717     }
1718 }
1719 
1720 void blk_drain_all(void)
1721 {
1722     BlockBackend *blk = NULL;
1723 
1724     bdrv_drain_all_begin();
1725 
1726     while ((blk = blk_all_next(blk)) != NULL) {
1727         AioContext *ctx = blk_get_aio_context(blk);
1728 
1729         aio_context_acquire(ctx);
1730 
1731         /* We may have -ENOMEDIUM completions in flight */
1732         AIO_WAIT_WHILE(ctx, atomic_mb_read(&blk->in_flight) > 0);
1733 
1734         aio_context_release(ctx);
1735     }
1736 
1737     bdrv_drain_all_end();
1738 }
1739 
1740 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1741                       BlockdevOnError on_write_error)
1742 {
1743     blk->on_read_error = on_read_error;
1744     blk->on_write_error = on_write_error;
1745 }
1746 
1747 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
1748 {
1749     return is_read ? blk->on_read_error : blk->on_write_error;
1750 }
1751 
1752 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
1753                                       int error)
1754 {
1755     BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1756 
1757     switch (on_err) {
1758     case BLOCKDEV_ON_ERROR_ENOSPC:
1759         return (error == ENOSPC) ?
1760                BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1761     case BLOCKDEV_ON_ERROR_STOP:
1762         return BLOCK_ERROR_ACTION_STOP;
1763     case BLOCKDEV_ON_ERROR_REPORT:
1764         return BLOCK_ERROR_ACTION_REPORT;
1765     case BLOCKDEV_ON_ERROR_IGNORE:
1766         return BLOCK_ERROR_ACTION_IGNORE;
1767     case BLOCKDEV_ON_ERROR_AUTO:
1768     default:
1769         abort();
1770     }
1771 }
1772 
1773 static void send_qmp_error_event(BlockBackend *blk,
1774                                  BlockErrorAction action,
1775                                  bool is_read, int error)
1776 {
1777     IoOperationType optype;
1778     BlockDriverState *bs = blk_bs(blk);
1779 
1780     optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
1781     qapi_event_send_block_io_error(blk_name(blk), !!bs,
1782                                    bs ? bdrv_get_node_name(bs) : NULL, optype,
1783                                    action, blk_iostatus_is_enabled(blk),
1784                                    error == ENOSPC, strerror(error));
1785 }
1786 
1787 /* This is done by device models because, while the block layer knows
1788  * about the error, it does not know whether an operation comes from
1789  * the device or the block layer (from a job, for example).
1790  */
1791 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1792                       bool is_read, int error)
1793 {
1794     assert(error >= 0);
1795 
1796     if (action == BLOCK_ERROR_ACTION_STOP) {
1797         /* First set the iostatus, so that "info block" returns an iostatus
1798          * that matches the events raised so far (an additional error iostatus
1799          * is fine, but not a lost one).
1800          */
1801         blk_iostatus_set_err(blk, error);
1802 
1803         /* Then raise the request to stop the VM and the event.
1804          * qemu_system_vmstop_request_prepare has two effects.  First,
1805          * it ensures that the STOP event always comes after the
1806          * BLOCK_IO_ERROR event.  Second, it ensures that even if management
1807          * can observe the STOP event and do a "cont" before the STOP
1808          * event is issued, the VM will not stop.  In this case, vm_start()
1809          * also ensures that the STOP/RESUME pair of events is emitted.
1810          */
1811         qemu_system_vmstop_request_prepare();
1812         send_qmp_error_event(blk, action, is_read, error);
1813         qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1814     } else {
1815         send_qmp_error_event(blk, action, is_read, error);
1816     }
1817 }
1818 
1819 bool blk_is_read_only(BlockBackend *blk)
1820 {
1821     BlockDriverState *bs = blk_bs(blk);
1822 
1823     if (bs) {
1824         return bdrv_is_read_only(bs);
1825     } else {
1826         return blk->root_state.read_only;
1827     }
1828 }
1829 
1830 bool blk_is_sg(BlockBackend *blk)
1831 {
1832     BlockDriverState *bs = blk_bs(blk);
1833 
1834     if (!bs) {
1835         return false;
1836     }
1837 
1838     return bdrv_is_sg(bs);
1839 }
1840 
1841 bool blk_enable_write_cache(BlockBackend *blk)
1842 {
1843     return blk->enable_write_cache;
1844 }
1845 
1846 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1847 {
1848     blk->enable_write_cache = wce;
1849 }
1850 
1851 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1852 {
1853     BlockDriverState *bs = blk_bs(blk);
1854 
1855     if (!bs) {
1856         error_setg(errp, "Device '%s' has no medium", blk->name);
1857         return;
1858     }
1859 
1860     bdrv_invalidate_cache(bs, errp);
1861 }
1862 
1863 bool blk_is_inserted(BlockBackend *blk)
1864 {
1865     BlockDriverState *bs = blk_bs(blk);
1866 
1867     return bs && bdrv_is_inserted(bs);
1868 }
1869 
1870 bool blk_is_available(BlockBackend *blk)
1871 {
1872     return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1873 }
1874 
1875 void blk_lock_medium(BlockBackend *blk, bool locked)
1876 {
1877     BlockDriverState *bs = blk_bs(blk);
1878 
1879     if (bs) {
1880         bdrv_lock_medium(bs, locked);
1881     }
1882 }
1883 
1884 void blk_eject(BlockBackend *blk, bool eject_flag)
1885 {
1886     BlockDriverState *bs = blk_bs(blk);
1887     char *id;
1888 
1889     if (bs) {
1890         bdrv_eject(bs, eject_flag);
1891     }
1892 
1893     /* Whether or not we ejected on the backend,
1894      * the frontend experienced a tray event. */
1895     id = blk_get_attached_dev_id(blk);
1896     qapi_event_send_device_tray_moved(blk_name(blk), id,
1897                                       eject_flag);
1898     g_free(id);
1899 }
1900 
1901 int blk_get_flags(BlockBackend *blk)
1902 {
1903     BlockDriverState *bs = blk_bs(blk);
1904 
1905     if (bs) {
1906         return bdrv_get_flags(bs);
1907     } else {
1908         return blk->root_state.open_flags;
1909     }
1910 }
1911 
1912 /* Returns the minimum request alignment, in bytes; guaranteed nonzero */
1913 uint32_t blk_get_request_alignment(BlockBackend *blk)
1914 {
1915     BlockDriverState *bs = blk_bs(blk);
1916     return bs ? bs->bl.request_alignment : BDRV_SECTOR_SIZE;
1917 }
1918 
1919 /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
1920 uint32_t blk_get_max_transfer(BlockBackend *blk)
1921 {
1922     BlockDriverState *bs = blk_bs(blk);
1923     uint32_t max = 0;
1924 
1925     if (bs) {
1926         max = bs->bl.max_transfer;
1927     }
1928     return MIN_NON_ZERO(max, INT_MAX);
1929 }
1930 
1931 int blk_get_max_iov(BlockBackend *blk)
1932 {
1933     return blk->root->bs->bl.max_iov;
1934 }
1935 
1936 void blk_set_guest_block_size(BlockBackend *blk, int align)
1937 {
1938     blk->guest_block_size = align;
1939 }
1940 
1941 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1942 {
1943     return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
1944 }
1945 
1946 void *blk_blockalign(BlockBackend *blk, size_t size)
1947 {
1948     return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
1949 }
1950 
1951 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1952 {
1953     BlockDriverState *bs = blk_bs(blk);
1954 
1955     if (!bs) {
1956         return false;
1957     }
1958 
1959     return bdrv_op_is_blocked(bs, op, errp);
1960 }
1961 
1962 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1963 {
1964     BlockDriverState *bs = blk_bs(blk);
1965 
1966     if (bs) {
1967         bdrv_op_unblock(bs, op, reason);
1968     }
1969 }
1970 
1971 void blk_op_block_all(BlockBackend *blk, Error *reason)
1972 {
1973     BlockDriverState *bs = blk_bs(blk);
1974 
1975     if (bs) {
1976         bdrv_op_block_all(bs, reason);
1977     }
1978 }
1979 
1980 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1981 {
1982     BlockDriverState *bs = blk_bs(blk);
1983 
1984     if (bs) {
1985         bdrv_op_unblock_all(bs, reason);
1986     }
1987 }
1988 
1989 AioContext *blk_get_aio_context(BlockBackend *blk)
1990 {
1991     BlockDriverState *bs = blk_bs(blk);
1992 
1993     if (bs) {
1994         AioContext *ctx = bdrv_get_aio_context(blk_bs(blk));
1995         assert(ctx == blk->ctx);
1996     }
1997 
1998     return blk->ctx;
1999 }
2000 
2001 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
2002 {
2003     BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
2004     return blk_get_aio_context(blk_acb->blk);
2005 }
2006 
2007 static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context,
2008                                   bool update_root_node, Error **errp)
2009 {
2010     BlockDriverState *bs = blk_bs(blk);
2011     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
2012     int ret;
2013 
2014     if (bs) {
2015         if (update_root_node) {
2016             ret = bdrv_child_try_set_aio_context(bs, new_context, blk->root,
2017                                                  errp);
2018             if (ret < 0) {
2019                 return ret;
2020             }
2021         }
2022         if (tgm->throttle_state) {
2023             bdrv_drained_begin(bs);
2024             throttle_group_detach_aio_context(tgm);
2025             throttle_group_attach_aio_context(tgm, new_context);
2026             bdrv_drained_end(bs);
2027         }
2028     }
2029 
2030     blk->ctx = new_context;
2031     return 0;
2032 }
2033 
2034 int blk_set_aio_context(BlockBackend *blk, AioContext *new_context,
2035                         Error **errp)
2036 {
2037     return blk_do_set_aio_context(blk, new_context, true, errp);
2038 }
2039 
2040 static bool blk_root_can_set_aio_ctx(BdrvChild *child, AioContext *ctx,
2041                                      GSList **ignore, Error **errp)
2042 {
2043     BlockBackend *blk = child->opaque;
2044 
2045     if (blk->allow_aio_context_change) {
2046         return true;
2047     }
2048 
2049     /* Only manually created BlockBackends that are not attached to anything
2050      * can change their AioContext without updating their user. */
2051     if (!blk->name || blk->dev) {
2052         /* TODO Add BB name/QOM path */
2053         error_setg(errp, "Cannot change iothread of active block backend");
2054         return false;
2055     }
2056 
2057     return true;
2058 }
2059 
2060 static void blk_root_set_aio_ctx(BdrvChild *child, AioContext *ctx,
2061                                  GSList **ignore)
2062 {
2063     BlockBackend *blk = child->opaque;
2064     blk_do_set_aio_context(blk, ctx, false, &error_abort);
2065 }
2066 
2067 void blk_add_aio_context_notifier(BlockBackend *blk,
2068         void (*attached_aio_context)(AioContext *new_context, void *opaque),
2069         void (*detach_aio_context)(void *opaque), void *opaque)
2070 {
2071     BlockBackendAioNotifier *notifier;
2072     BlockDriverState *bs = blk_bs(blk);
2073 
2074     notifier = g_new(BlockBackendAioNotifier, 1);
2075     notifier->attached_aio_context = attached_aio_context;
2076     notifier->detach_aio_context = detach_aio_context;
2077     notifier->opaque = opaque;
2078     QLIST_INSERT_HEAD(&blk->aio_notifiers, notifier, list);
2079 
2080     if (bs) {
2081         bdrv_add_aio_context_notifier(bs, attached_aio_context,
2082                                       detach_aio_context, opaque);
2083     }
2084 }
2085 
2086 void blk_remove_aio_context_notifier(BlockBackend *blk,
2087                                      void (*attached_aio_context)(AioContext *,
2088                                                                   void *),
2089                                      void (*detach_aio_context)(void *),
2090                                      void *opaque)
2091 {
2092     BlockBackendAioNotifier *notifier;
2093     BlockDriverState *bs = blk_bs(blk);
2094 
2095     if (bs) {
2096         bdrv_remove_aio_context_notifier(bs, attached_aio_context,
2097                                          detach_aio_context, opaque);
2098     }
2099 
2100     QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
2101         if (notifier->attached_aio_context == attached_aio_context &&
2102             notifier->detach_aio_context == detach_aio_context &&
2103             notifier->opaque == opaque) {
2104             QLIST_REMOVE(notifier, list);
2105             g_free(notifier);
2106             return;
2107         }
2108     }
2109 
2110     abort();
2111 }
2112 
2113 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
2114 {
2115     notifier_list_add(&blk->remove_bs_notifiers, notify);
2116 }
2117 
2118 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
2119 {
2120     notifier_list_add(&blk->insert_bs_notifiers, notify);
2121 }
2122 
2123 void blk_io_plug(BlockBackend *blk)
2124 {
2125     BlockDriverState *bs = blk_bs(blk);
2126 
2127     if (bs) {
2128         bdrv_io_plug(bs);
2129     }
2130 }
2131 
2132 void blk_io_unplug(BlockBackend *blk)
2133 {
2134     BlockDriverState *bs = blk_bs(blk);
2135 
2136     if (bs) {
2137         bdrv_io_unplug(bs);
2138     }
2139 }
2140 
2141 BlockAcctStats *blk_get_stats(BlockBackend *blk)
2142 {
2143     return &blk->stats;
2144 }
2145 
2146 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
2147                   BlockCompletionFunc *cb, void *opaque)
2148 {
2149     return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
2150 }
2151 
2152 int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
2153                                       int bytes, BdrvRequestFlags flags)
2154 {
2155     return blk_co_pwritev(blk, offset, bytes, NULL,
2156                           flags | BDRV_REQ_ZERO_WRITE);
2157 }
2158 
2159 int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
2160                           int count)
2161 {
2162     return blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
2163                    BDRV_REQ_WRITE_COMPRESSED);
2164 }
2165 
2166 int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,
2167                  PreallocMode prealloc, BdrvRequestFlags flags, Error **errp)
2168 {
2169     if (!blk_is_available(blk)) {
2170         error_setg(errp, "No medium inserted");
2171         return -ENOMEDIUM;
2172     }
2173 
2174     return bdrv_truncate(blk->root, offset, exact, prealloc, flags, errp);
2175 }
2176 
2177 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
2178                      int64_t pos, int size)
2179 {
2180     int ret;
2181 
2182     if (!blk_is_available(blk)) {
2183         return -ENOMEDIUM;
2184     }
2185 
2186     ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
2187     if (ret < 0) {
2188         return ret;
2189     }
2190 
2191     if (ret == size && !blk->enable_write_cache) {
2192         ret = bdrv_flush(blk_bs(blk));
2193     }
2194 
2195     return ret < 0 ? ret : size;
2196 }
2197 
2198 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
2199 {
2200     if (!blk_is_available(blk)) {
2201         return -ENOMEDIUM;
2202     }
2203 
2204     return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
2205 }
2206 
2207 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
2208 {
2209     if (!blk_is_available(blk)) {
2210         return -ENOMEDIUM;
2211     }
2212 
2213     return bdrv_probe_blocksizes(blk_bs(blk), bsz);
2214 }
2215 
2216 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
2217 {
2218     if (!blk_is_available(blk)) {
2219         return -ENOMEDIUM;
2220     }
2221 
2222     return bdrv_probe_geometry(blk_bs(blk), geo);
2223 }
2224 
2225 /*
2226  * Updates the BlockBackendRootState object with data from the currently
2227  * attached BlockDriverState.
2228  */
2229 void blk_update_root_state(BlockBackend *blk)
2230 {
2231     assert(blk->root);
2232 
2233     blk->root_state.open_flags    = blk->root->bs->open_flags;
2234     blk->root_state.read_only     = blk->root->bs->read_only;
2235     blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
2236 }
2237 
2238 /*
2239  * Returns the detect-zeroes setting to be used for bdrv_open() of a
2240  * BlockDriverState which is supposed to inherit the root state.
2241  */
2242 bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)
2243 {
2244     return blk->root_state.detect_zeroes;
2245 }
2246 
2247 /*
2248  * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
2249  * supposed to inherit the root state.
2250  */
2251 int blk_get_open_flags_from_root_state(BlockBackend *blk)
2252 {
2253     int bs_flags;
2254 
2255     bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
2256     bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
2257 
2258     return bs_flags;
2259 }
2260 
2261 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
2262 {
2263     return &blk->root_state;
2264 }
2265 
2266 int blk_commit_all(void)
2267 {
2268     BlockBackend *blk = NULL;
2269 
2270     while ((blk = blk_all_next(blk)) != NULL) {
2271         AioContext *aio_context = blk_get_aio_context(blk);
2272 
2273         aio_context_acquire(aio_context);
2274         if (blk_is_inserted(blk) && blk->root->bs->backing) {
2275             int ret = bdrv_commit(blk->root->bs);
2276             if (ret < 0) {
2277                 aio_context_release(aio_context);
2278                 return ret;
2279             }
2280         }
2281         aio_context_release(aio_context);
2282     }
2283     return 0;
2284 }
2285 
2286 
2287 /* throttling disk I/O limits */
2288 void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
2289 {
2290     throttle_group_config(&blk->public.throttle_group_member, cfg);
2291 }
2292 
2293 void blk_io_limits_disable(BlockBackend *blk)
2294 {
2295     BlockDriverState *bs = blk_bs(blk);
2296     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
2297     assert(tgm->throttle_state);
2298     if (bs) {
2299         bdrv_drained_begin(bs);
2300     }
2301     throttle_group_unregister_tgm(tgm);
2302     if (bs) {
2303         bdrv_drained_end(bs);
2304     }
2305 }
2306 
2307 /* should be called before blk_set_io_limits if a limit is set */
2308 void blk_io_limits_enable(BlockBackend *blk, const char *group)
2309 {
2310     assert(!blk->public.throttle_group_member.throttle_state);
2311     throttle_group_register_tgm(&blk->public.throttle_group_member,
2312                                 group, blk_get_aio_context(blk));
2313 }
2314 
2315 void blk_io_limits_update_group(BlockBackend *blk, const char *group)
2316 {
2317     /* this BB is not part of any group */
2318     if (!blk->public.throttle_group_member.throttle_state) {
2319         return;
2320     }
2321 
2322     /* this BB is a part of the same group than the one we want */
2323     if (!g_strcmp0(throttle_group_get_name(&blk->public.throttle_group_member),
2324                 group)) {
2325         return;
2326     }
2327 
2328     /* need to change the group this bs belong to */
2329     blk_io_limits_disable(blk);
2330     blk_io_limits_enable(blk, group);
2331 }
2332 
2333 static void blk_root_drained_begin(BdrvChild *child)
2334 {
2335     BlockBackend *blk = child->opaque;
2336 
2337     if (++blk->quiesce_counter == 1) {
2338         if (blk->dev_ops && blk->dev_ops->drained_begin) {
2339             blk->dev_ops->drained_begin(blk->dev_opaque);
2340         }
2341     }
2342 
2343     /* Note that blk->root may not be accessible here yet if we are just
2344      * attaching to a BlockDriverState that is drained. Use child instead. */
2345 
2346     if (atomic_fetch_inc(&blk->public.throttle_group_member.io_limits_disabled) == 0) {
2347         throttle_group_restart_tgm(&blk->public.throttle_group_member);
2348     }
2349 }
2350 
2351 static bool blk_root_drained_poll(BdrvChild *child)
2352 {
2353     BlockBackend *blk = child->opaque;
2354     assert(blk->quiesce_counter);
2355     return !!blk->in_flight;
2356 }
2357 
2358 static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter)
2359 {
2360     BlockBackend *blk = child->opaque;
2361     assert(blk->quiesce_counter);
2362 
2363     assert(blk->public.throttle_group_member.io_limits_disabled);
2364     atomic_dec(&blk->public.throttle_group_member.io_limits_disabled);
2365 
2366     if (--blk->quiesce_counter == 0) {
2367         if (blk->dev_ops && blk->dev_ops->drained_end) {
2368             blk->dev_ops->drained_end(blk->dev_opaque);
2369         }
2370         while (qemu_co_enter_next(&blk->queued_requests, NULL)) {
2371             /* Resume all queued requests */
2372         }
2373     }
2374 }
2375 
2376 void blk_register_buf(BlockBackend *blk, void *host, size_t size)
2377 {
2378     bdrv_register_buf(blk_bs(blk), host, size);
2379 }
2380 
2381 void blk_unregister_buf(BlockBackend *blk, void *host)
2382 {
2383     bdrv_unregister_buf(blk_bs(blk), host);
2384 }
2385 
2386 int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in,
2387                                    BlockBackend *blk_out, int64_t off_out,
2388                                    int bytes, BdrvRequestFlags read_flags,
2389                                    BdrvRequestFlags write_flags)
2390 {
2391     int r;
2392     r = blk_check_byte_request(blk_in, off_in, bytes);
2393     if (r) {
2394         return r;
2395     }
2396     r = blk_check_byte_request(blk_out, off_out, bytes);
2397     if (r) {
2398         return r;
2399     }
2400     return bdrv_co_copy_range(blk_in->root, off_in,
2401                               blk_out->root, off_out,
2402                               bytes, read_flags, write_flags);
2403 }
2404 
2405 const BdrvChild *blk_root(BlockBackend *blk)
2406 {
2407     return blk->root;
2408 }
2409 
2410 int blk_make_empty(BlockBackend *blk, Error **errp)
2411 {
2412     if (!blk_is_available(blk)) {
2413         error_setg(errp, "No medium inserted");
2414         return -ENOMEDIUM;
2415     }
2416 
2417     return bdrv_make_empty(blk->root, errp);
2418 }
2419