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