xref: /openbmc/qemu/block/block-backend.c (revision e92f0e19)
126f54e9aSMarkus Armbruster /*
226f54e9aSMarkus Armbruster  * QEMU Block backends
326f54e9aSMarkus Armbruster  *
460cb2fa7SEric Blake  * Copyright (C) 2014-2016 Red Hat, Inc.
526f54e9aSMarkus Armbruster  *
626f54e9aSMarkus Armbruster  * Authors:
726f54e9aSMarkus Armbruster  *  Markus Armbruster <armbru@redhat.com>,
826f54e9aSMarkus Armbruster  *
926f54e9aSMarkus Armbruster  * This work is licensed under the terms of the GNU LGPL, version 2.1
1026f54e9aSMarkus Armbruster  * or later.  See the COPYING.LIB file in the top-level directory.
1126f54e9aSMarkus Armbruster  */
1226f54e9aSMarkus Armbruster 
1380c71a24SPeter Maydell #include "qemu/osdep.h"
1426f54e9aSMarkus Armbruster #include "sysemu/block-backend.h"
1526f54e9aSMarkus Armbruster #include "block/block_int.h"
16373340b2SMax Reitz #include "block/blockjob.h"
17281d22d8SMax Reitz #include "block/throttle-groups.h"
1818e46a03SMarkus Armbruster #include "sysemu/blockdev.h"
19373340b2SMax Reitz #include "sysemu/sysemu.h"
20a7f53e26SMarkus Armbruster #include "qapi-event.h"
21f348b6d1SVeronia Bahaa #include "qemu/id.h"
221e98fefdSKevin Wolf #include "trace.h"
23a7f53e26SMarkus Armbruster 
24a7f53e26SMarkus Armbruster /* Number of coroutines to reserve per attached device model */
25a7f53e26SMarkus Armbruster #define COROUTINE_POOL_RESERVATION 64
2626f54e9aSMarkus Armbruster 
271bf1cbc9SKevin Wolf #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
281bf1cbc9SKevin Wolf 
294981bdecSMax Reitz static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
304981bdecSMax Reitz 
3126f54e9aSMarkus Armbruster struct BlockBackend {
3226f54e9aSMarkus Armbruster     char *name;
3326f54e9aSMarkus Armbruster     int refcnt;
34f21d96d0SKevin Wolf     BdrvChild *root;
3526f8b3a8SMarkus Armbruster     DriveInfo *legacy_dinfo;    /* null unless created by drive_new() */
362cf22d6aSMax Reitz     QTAILQ_ENTRY(BlockBackend) link;         /* for block_backends */
379492b0b9SMax Reitz     QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
38f2cd875dSKevin Wolf     BlockBackendPublic public;
39a7f53e26SMarkus Armbruster 
40a7f53e26SMarkus Armbruster     void *dev;                  /* attached device model, if any */
41bbc8ea98SKevin Wolf     bool legacy_dev;            /* true if dev is not a DeviceState */
42a7f53e26SMarkus Armbruster     /* TODO change to DeviceState when all users are qdevified */
43a7f53e26SMarkus Armbruster     const BlockDevOps *dev_ops;
44a7f53e26SMarkus Armbruster     void *dev_opaque;
4568e9ec01SMax Reitz 
4668e9ec01SMax Reitz     /* the block size for which the guest device expects atomicity */
4768e9ec01SMax Reitz     int guest_block_size;
487f0e9da6SMax Reitz 
49281d22d8SMax Reitz     /* If the BDS tree is removed, some of its options are stored here (which
50281d22d8SMax Reitz      * can be used to restore those options in the new BDS on insert) */
51281d22d8SMax Reitz     BlockBackendRootState root_state;
52281d22d8SMax Reitz 
53bfd18d1eSKevin Wolf     bool enable_write_cache;
54bfd18d1eSKevin Wolf 
557f0e9da6SMax Reitz     /* I/O stats (display with "info blockstats"). */
567f0e9da6SMax Reitz     BlockAcctStats stats;
57373340b2SMax Reitz 
58373340b2SMax Reitz     BlockdevOnError on_read_error, on_write_error;
59373340b2SMax Reitz     bool iostatus_enabled;
60373340b2SMax Reitz     BlockDeviceIoStatus iostatus;
613301f6c6SMax Reitz 
62981776b3SKevin Wolf     uint64_t perm;
63981776b3SKevin Wolf     uint64_t shared_perm;
64d35ff5e6SKevin Wolf     bool disable_perm;
65981776b3SKevin Wolf 
66c10c9d96SKevin Wolf     bool allow_write_beyond_eof;
67c10c9d96SKevin Wolf 
683301f6c6SMax Reitz     NotifierList remove_bs_notifiers, insert_bs_notifiers;
69f4d9cc88SJohn Snow 
70f4d9cc88SJohn Snow     int quiesce_counter;
7126f54e9aSMarkus Armbruster };
7226f54e9aSMarkus Armbruster 
73e7f7d676SMax Reitz typedef struct BlockBackendAIOCB {
74e7f7d676SMax Reitz     BlockAIOCB common;
754981bdecSMax Reitz     BlockBackend *blk;
76e7f7d676SMax Reitz     int ret;
77e7f7d676SMax Reitz } BlockBackendAIOCB;
78e7f7d676SMax Reitz 
79e7f7d676SMax Reitz static const AIOCBInfo block_backend_aiocb_info = {
804981bdecSMax Reitz     .get_aio_context = blk_aiocb_get_aio_context,
81e7f7d676SMax Reitz     .aiocb_size = sizeof(BlockBackendAIOCB),
82e7f7d676SMax Reitz };
83e7f7d676SMax Reitz 
848fb3c76cSMarkus Armbruster static void drive_info_del(DriveInfo *dinfo);
857c8eece4SKevin Wolf static BlockBackend *bdrv_first_blk(BlockDriverState *bs);
86b5411555SKevin Wolf static char *blk_get_attached_dev_id(BlockBackend *blk);
878fb3c76cSMarkus Armbruster 
882cf22d6aSMax Reitz /* All BlockBackends */
892cf22d6aSMax Reitz static QTAILQ_HEAD(, BlockBackend) block_backends =
902cf22d6aSMax Reitz     QTAILQ_HEAD_INITIALIZER(block_backends);
912cf22d6aSMax Reitz 
929492b0b9SMax Reitz /* All BlockBackends referenced by the monitor and which are iterated through by
939492b0b9SMax Reitz  * blk_next() */
949492b0b9SMax Reitz static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
959492b0b9SMax Reitz     QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
9626f54e9aSMarkus Armbruster 
97f21d96d0SKevin Wolf static void blk_root_inherit_options(int *child_flags, QDict *child_options,
98f21d96d0SKevin Wolf                                      int parent_flags, QDict *parent_options)
99f21d96d0SKevin Wolf {
100f21d96d0SKevin Wolf     /* We're not supposed to call this function for root nodes */
101f21d96d0SKevin Wolf     abort();
102f21d96d0SKevin Wolf }
103c2066af0SKevin Wolf static void blk_root_drained_begin(BdrvChild *child);
104c2066af0SKevin Wolf static void blk_root_drained_end(BdrvChild *child);
105f21d96d0SKevin Wolf 
1065c8cab48SKevin Wolf static void blk_root_change_media(BdrvChild *child, bool load);
1075c8cab48SKevin Wolf static void blk_root_resize(BdrvChild *child);
1085c8cab48SKevin Wolf 
109b5411555SKevin Wolf static char *blk_root_get_parent_desc(BdrvChild *child)
110b5411555SKevin Wolf {
111b5411555SKevin Wolf     BlockBackend *blk = child->opaque;
112b5411555SKevin Wolf     char *dev_id;
113b5411555SKevin Wolf 
114b5411555SKevin Wolf     if (blk->name) {
115b5411555SKevin Wolf         return g_strdup(blk->name);
116b5411555SKevin Wolf     }
117b5411555SKevin Wolf 
118b5411555SKevin Wolf     dev_id = blk_get_attached_dev_id(blk);
119b5411555SKevin Wolf     if (*dev_id) {
120b5411555SKevin Wolf         return dev_id;
121b5411555SKevin Wolf     } else {
122b5411555SKevin Wolf         /* TODO Callback into the BB owner for something more detailed */
123b5411555SKevin Wolf         g_free(dev_id);
124b5411555SKevin Wolf         return g_strdup("a block device");
125b5411555SKevin Wolf     }
126b5411555SKevin Wolf }
127b5411555SKevin Wolf 
1284c265bf9SKevin Wolf static const char *blk_root_get_name(BdrvChild *child)
1294c265bf9SKevin Wolf {
1304c265bf9SKevin Wolf     return blk_name(child->opaque);
1314c265bf9SKevin Wolf }
1324c265bf9SKevin Wolf 
133f21d96d0SKevin Wolf static const BdrvChildRole child_root = {
134f21d96d0SKevin Wolf     .inherit_options    = blk_root_inherit_options,
135c2066af0SKevin Wolf 
1365c8cab48SKevin Wolf     .change_media       = blk_root_change_media,
1375c8cab48SKevin Wolf     .resize             = blk_root_resize,
1384c265bf9SKevin Wolf     .get_name           = blk_root_get_name,
139b5411555SKevin Wolf     .get_parent_desc    = blk_root_get_parent_desc,
1405c8cab48SKevin Wolf 
141c2066af0SKevin Wolf     .drained_begin      = blk_root_drained_begin,
142c2066af0SKevin Wolf     .drained_end        = blk_root_drained_end,
143f21d96d0SKevin Wolf };
144f21d96d0SKevin Wolf 
14526f54e9aSMarkus Armbruster /*
146efaa7c4eSMax Reitz  * Create a new BlockBackend with a reference count of one.
1476d0eb64dSKevin Wolf  *
1486d0eb64dSKevin Wolf  * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
1496d0eb64dSKevin Wolf  * to request for a block driver node that is attached to this BlockBackend.
1506d0eb64dSKevin Wolf  * @shared_perm is a bitmask which describes which permissions may be granted
1516d0eb64dSKevin Wolf  * to other users of the attached node.
1526d0eb64dSKevin Wolf  * Both sets of permissions can be changed later using blk_set_perm().
1536d0eb64dSKevin Wolf  *
15426f54e9aSMarkus Armbruster  * Return the new BlockBackend on success, null on failure.
15526f54e9aSMarkus Armbruster  */
1566d0eb64dSKevin Wolf BlockBackend *blk_new(uint64_t perm, uint64_t shared_perm)
15726f54e9aSMarkus Armbruster {
15826f54e9aSMarkus Armbruster     BlockBackend *blk;
15926f54e9aSMarkus Armbruster 
16026f54e9aSMarkus Armbruster     blk = g_new0(BlockBackend, 1);
16126f54e9aSMarkus Armbruster     blk->refcnt = 1;
1626d0eb64dSKevin Wolf     blk->perm = perm;
1636d0eb64dSKevin Wolf     blk->shared_perm = shared_perm;
1640c3169dfSKevin Wolf     blk_set_enable_write_cache(blk, true);
1650c3169dfSKevin Wolf 
16627ccdd52SKevin Wolf     qemu_co_queue_init(&blk->public.throttled_reqs[0]);
16727ccdd52SKevin Wolf     qemu_co_queue_init(&blk->public.throttled_reqs[1]);
16827ccdd52SKevin Wolf 
1693301f6c6SMax Reitz     notifier_list_init(&blk->remove_bs_notifiers);
1703301f6c6SMax Reitz     notifier_list_init(&blk->insert_bs_notifiers);
17127ccdd52SKevin Wolf 
1722cf22d6aSMax Reitz     QTAILQ_INSERT_TAIL(&block_backends, blk, link);
17326f54e9aSMarkus Armbruster     return blk;
17426f54e9aSMarkus Armbruster }
17526f54e9aSMarkus Armbruster 
1767e7d56d9SMarkus Armbruster /*
17728eb9b12SMax Reitz  * Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
178ca49a4fdSMax Reitz  *
179ca49a4fdSMax Reitz  * Just as with bdrv_open(), after having called this function the reference to
180ca49a4fdSMax Reitz  * @options belongs to the block layer (even on failure).
181ca49a4fdSMax Reitz  *
182ca49a4fdSMax Reitz  * TODO: Remove @filename and @flags; it should be possible to specify a whole
183ca49a4fdSMax Reitz  * BDS tree just by specifying the @options QDict (or @reference,
184ca49a4fdSMax Reitz  * alternatively). At the time of adding this function, this is not possible,
185ca49a4fdSMax Reitz  * though, so callers of this function have to be able to specify @filename and
186ca49a4fdSMax Reitz  * @flags.
187ca49a4fdSMax Reitz  */
188efaa7c4eSMax Reitz BlockBackend *blk_new_open(const char *filename, const char *reference,
189efaa7c4eSMax Reitz                            QDict *options, int flags, Error **errp)
190ca49a4fdSMax Reitz {
191ca49a4fdSMax Reitz     BlockBackend *blk;
19228eb9b12SMax Reitz     BlockDriverState *bs;
193c62d32f5SKevin Wolf     uint64_t perm;
194ca49a4fdSMax Reitz 
195c62d32f5SKevin Wolf     /* blk_new_open() is mainly used in .bdrv_create implementations and the
196c62d32f5SKevin Wolf      * tools where sharing isn't a concern because the BDS stays private, so we
197c62d32f5SKevin Wolf      * just request permission according to the flags.
198c62d32f5SKevin Wolf      *
199c62d32f5SKevin Wolf      * The exceptions are xen_disk and blockdev_init(); in these cases, the
200c62d32f5SKevin Wolf      * caller of blk_new_open() doesn't make use of the permissions, but they
201c62d32f5SKevin Wolf      * shouldn't hurt either. We can still share everything here because the
202c62d32f5SKevin Wolf      * guest devices will add their own blockers if they can't share. */
203c62d32f5SKevin Wolf     perm = BLK_PERM_CONSISTENT_READ;
204c62d32f5SKevin Wolf     if (flags & BDRV_O_RDWR) {
205c62d32f5SKevin Wolf         perm |= BLK_PERM_WRITE;
206c62d32f5SKevin Wolf     }
207c62d32f5SKevin Wolf     if (flags & BDRV_O_RESIZE) {
208c62d32f5SKevin Wolf         perm |= BLK_PERM_RESIZE;
209c62d32f5SKevin Wolf     }
210c62d32f5SKevin Wolf 
211c62d32f5SKevin Wolf     blk = blk_new(perm, BLK_PERM_ALL);
2125b363937SMax Reitz     bs = bdrv_open(filename, reference, options, flags, errp);
2135b363937SMax Reitz     if (!bs) {
214ca49a4fdSMax Reitz         blk_unref(blk);
215ca49a4fdSMax Reitz         return NULL;
216ca49a4fdSMax Reitz     }
217ca49a4fdSMax Reitz 
218d5e6f437SKevin Wolf     blk->root = bdrv_root_attach_child(bs, "root", &child_root,
21950bfbe93SFam Zheng                                        perm, BLK_PERM_ALL, blk, errp);
22050bfbe93SFam Zheng     if (!blk->root) {
22150bfbe93SFam Zheng         bdrv_unref(bs);
22250bfbe93SFam Zheng         blk_unref(blk);
22350bfbe93SFam Zheng         return NULL;
22450bfbe93SFam Zheng     }
22572e775c7SKevin Wolf 
226ca49a4fdSMax Reitz     return blk;
227ca49a4fdSMax Reitz }
228ca49a4fdSMax Reitz 
22926f54e9aSMarkus Armbruster static void blk_delete(BlockBackend *blk)
23026f54e9aSMarkus Armbruster {
23126f54e9aSMarkus Armbruster     assert(!blk->refcnt);
232e5e78550SMax Reitz     assert(!blk->name);
233a7f53e26SMarkus Armbruster     assert(!blk->dev);
234f21d96d0SKevin Wolf     if (blk->root) {
23513855c6bSMax Reitz         blk_remove_bs(blk);
2367e7d56d9SMarkus Armbruster     }
2373301f6c6SMax Reitz     assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
2383301f6c6SMax Reitz     assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
2392cf22d6aSMax Reitz     QTAILQ_REMOVE(&block_backends, blk, link);
24018e46a03SMarkus Armbruster     drive_info_del(blk->legacy_dinfo);
241979e9b03SAlberto Garcia     block_acct_cleanup(&blk->stats);
24226f54e9aSMarkus Armbruster     g_free(blk);
24326f54e9aSMarkus Armbruster }
24426f54e9aSMarkus Armbruster 
2458fb3c76cSMarkus Armbruster static void drive_info_del(DriveInfo *dinfo)
2468fb3c76cSMarkus Armbruster {
2478fb3c76cSMarkus Armbruster     if (!dinfo) {
2488fb3c76cSMarkus Armbruster         return;
2498fb3c76cSMarkus Armbruster     }
2508fb3c76cSMarkus Armbruster     qemu_opts_del(dinfo->opts);
2518fb3c76cSMarkus Armbruster     g_free(dinfo->serial);
2528fb3c76cSMarkus Armbruster     g_free(dinfo);
2538fb3c76cSMarkus Armbruster }
2548fb3c76cSMarkus Armbruster 
255f636ae85SAlberto Garcia int blk_get_refcnt(BlockBackend *blk)
256f636ae85SAlberto Garcia {
257f636ae85SAlberto Garcia     return blk ? blk->refcnt : 0;
258f636ae85SAlberto Garcia }
259f636ae85SAlberto Garcia 
26026f54e9aSMarkus Armbruster /*
26126f54e9aSMarkus Armbruster  * Increment @blk's reference count.
26226f54e9aSMarkus Armbruster  * @blk must not be null.
26326f54e9aSMarkus Armbruster  */
26426f54e9aSMarkus Armbruster void blk_ref(BlockBackend *blk)
26526f54e9aSMarkus Armbruster {
26626f54e9aSMarkus Armbruster     blk->refcnt++;
26726f54e9aSMarkus Armbruster }
26826f54e9aSMarkus Armbruster 
26926f54e9aSMarkus Armbruster /*
27026f54e9aSMarkus Armbruster  * Decrement @blk's reference count.
27126f54e9aSMarkus Armbruster  * If this drops it to zero, destroy @blk.
27226f54e9aSMarkus Armbruster  * For convenience, do nothing if @blk is null.
27326f54e9aSMarkus Armbruster  */
27426f54e9aSMarkus Armbruster void blk_unref(BlockBackend *blk)
27526f54e9aSMarkus Armbruster {
27626f54e9aSMarkus Armbruster     if (blk) {
27726f54e9aSMarkus Armbruster         assert(blk->refcnt > 0);
27826f54e9aSMarkus Armbruster         if (!--blk->refcnt) {
27926f54e9aSMarkus Armbruster             blk_delete(blk);
28026f54e9aSMarkus Armbruster         }
28126f54e9aSMarkus Armbruster     }
28226f54e9aSMarkus Armbruster }
28326f54e9aSMarkus Armbruster 
2842cf22d6aSMax Reitz /*
2852cf22d6aSMax Reitz  * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
2862cf22d6aSMax Reitz  * ones which are hidden (i.e. are not referenced by the monitor).
2872cf22d6aSMax Reitz  */
2882cf22d6aSMax Reitz static BlockBackend *blk_all_next(BlockBackend *blk)
2892cf22d6aSMax Reitz {
2902cf22d6aSMax Reitz     return blk ? QTAILQ_NEXT(blk, link)
2912cf22d6aSMax Reitz                : QTAILQ_FIRST(&block_backends);
2922cf22d6aSMax Reitz }
2932cf22d6aSMax Reitz 
294d8da3cefSMax Reitz void blk_remove_all_bs(void)
295d8da3cefSMax Reitz {
29674d1b8fcSMax Reitz     BlockBackend *blk = NULL;
297d8da3cefSMax Reitz 
2982cf22d6aSMax Reitz     while ((blk = blk_all_next(blk)) != NULL) {
299d8da3cefSMax Reitz         AioContext *ctx = blk_get_aio_context(blk);
300d8da3cefSMax Reitz 
301d8da3cefSMax Reitz         aio_context_acquire(ctx);
302f21d96d0SKevin Wolf         if (blk->root) {
303d8da3cefSMax Reitz             blk_remove_bs(blk);
304d8da3cefSMax Reitz         }
305d8da3cefSMax Reitz         aio_context_release(ctx);
306d8da3cefSMax Reitz     }
307d8da3cefSMax Reitz }
308d8da3cefSMax Reitz 
30926f54e9aSMarkus Armbruster /*
3109492b0b9SMax Reitz  * Return the monitor-owned BlockBackend after @blk.
31126f54e9aSMarkus Armbruster  * If @blk is null, return the first one.
31226f54e9aSMarkus Armbruster  * Else, return @blk's next sibling, which may be null.
31326f54e9aSMarkus Armbruster  *
31426f54e9aSMarkus Armbruster  * To iterate over all BlockBackends, do
31526f54e9aSMarkus Armbruster  * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
31626f54e9aSMarkus Armbruster  *     ...
31726f54e9aSMarkus Armbruster  * }
31826f54e9aSMarkus Armbruster  */
31926f54e9aSMarkus Armbruster BlockBackend *blk_next(BlockBackend *blk)
32026f54e9aSMarkus Armbruster {
3219492b0b9SMax Reitz     return blk ? QTAILQ_NEXT(blk, monitor_link)
3229492b0b9SMax Reitz                : QTAILQ_FIRST(&monitor_block_backends);
32326f54e9aSMarkus Armbruster }
32426f54e9aSMarkus Armbruster 
3257c8eece4SKevin Wolf /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by
3267c8eece4SKevin Wolf  * the monitor or attached to a BlockBackend */
32788be7b4bSKevin Wolf BlockDriverState *bdrv_next(BdrvNextIterator *it)
3287c8eece4SKevin Wolf {
32988be7b4bSKevin Wolf     BlockDriverState *bs;
330981f4f57SMax Reitz 
3317c8eece4SKevin Wolf     /* First, return all root nodes of BlockBackends. In order to avoid
3327c8eece4SKevin Wolf      * returning a BDS twice when multiple BBs refer to it, we only return it
3337c8eece4SKevin Wolf      * if the BB is the first one in the parent list of the BDS. */
3347c8eece4SKevin Wolf     if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
335981f4f57SMax Reitz         do {
3367c8eece4SKevin Wolf             it->blk = blk_all_next(it->blk);
33788be7b4bSKevin Wolf             bs = it->blk ? blk_bs(it->blk) : NULL;
33888be7b4bSKevin Wolf         } while (it->blk && (bs == NULL || bdrv_first_blk(bs) != it->blk));
339981f4f57SMax Reitz 
34088be7b4bSKevin Wolf         if (bs) {
34188be7b4bSKevin Wolf             return bs;
3427c8eece4SKevin Wolf         }
3437c8eece4SKevin Wolf         it->phase = BDRV_NEXT_MONITOR_OWNED;
3447c8eece4SKevin Wolf     }
3457c8eece4SKevin Wolf 
3467c8eece4SKevin Wolf     /* Then return the monitor-owned BDSes without a BB attached. Ignore all
3477c8eece4SKevin Wolf      * BDSes that are attached to a BlockBackend here; they have been handled
3487c8eece4SKevin Wolf      * by the above block already */
3497c8eece4SKevin Wolf     do {
3507c8eece4SKevin Wolf         it->bs = bdrv_next_monitor_owned(it->bs);
35188be7b4bSKevin Wolf         bs = it->bs;
35288be7b4bSKevin Wolf     } while (bs && bdrv_has_blk(bs));
3537c8eece4SKevin Wolf 
35488be7b4bSKevin Wolf     return bs;
35588be7b4bSKevin Wolf }
35688be7b4bSKevin Wolf 
35788be7b4bSKevin Wolf BlockDriverState *bdrv_first(BdrvNextIterator *it)
35888be7b4bSKevin Wolf {
35988be7b4bSKevin Wolf     *it = (BdrvNextIterator) {
36088be7b4bSKevin Wolf         .phase = BDRV_NEXT_BACKEND_ROOTS,
36188be7b4bSKevin Wolf     };
36288be7b4bSKevin Wolf 
36388be7b4bSKevin Wolf     return bdrv_next(it);
364981f4f57SMax Reitz }
365981f4f57SMax Reitz 
366981f4f57SMax Reitz /*
367e5e78550SMax Reitz  * Add a BlockBackend into the list of backends referenced by the monitor, with
368e5e78550SMax Reitz  * the given @name acting as the handle for the monitor.
369e5e78550SMax Reitz  * Strictly for use by blockdev.c.
370e5e78550SMax Reitz  *
371e5e78550SMax Reitz  * @name must not be null or empty.
372e5e78550SMax Reitz  *
373e5e78550SMax Reitz  * Returns true on success and false on failure. In the latter case, an Error
374e5e78550SMax Reitz  * object is returned through @errp.
375e5e78550SMax Reitz  */
376e5e78550SMax Reitz bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
377e5e78550SMax Reitz {
378e5e78550SMax Reitz     assert(!blk->name);
379e5e78550SMax Reitz     assert(name && name[0]);
380e5e78550SMax Reitz 
381e5e78550SMax Reitz     if (!id_wellformed(name)) {
382e5e78550SMax Reitz         error_setg(errp, "Invalid device name");
383e5e78550SMax Reitz         return false;
384e5e78550SMax Reitz     }
385e5e78550SMax Reitz     if (blk_by_name(name)) {
386e5e78550SMax Reitz         error_setg(errp, "Device with id '%s' already exists", name);
387e5e78550SMax Reitz         return false;
388e5e78550SMax Reitz     }
389e5e78550SMax Reitz     if (bdrv_find_node(name)) {
390e5e78550SMax Reitz         error_setg(errp,
391e5e78550SMax Reitz                    "Device name '%s' conflicts with an existing node name",
392e5e78550SMax Reitz                    name);
393e5e78550SMax Reitz         return false;
394e5e78550SMax Reitz     }
395e5e78550SMax Reitz 
396e5e78550SMax Reitz     blk->name = g_strdup(name);
397e5e78550SMax Reitz     QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
398e5e78550SMax Reitz     return true;
399e5e78550SMax Reitz }
400e5e78550SMax Reitz 
401e5e78550SMax Reitz /*
402e5e78550SMax Reitz  * Remove a BlockBackend from the list of backends referenced by the monitor.
403e5e78550SMax Reitz  * Strictly for use by blockdev.c.
404e5e78550SMax Reitz  */
405e5e78550SMax Reitz void monitor_remove_blk(BlockBackend *blk)
406e5e78550SMax Reitz {
407e5e78550SMax Reitz     if (!blk->name) {
408e5e78550SMax Reitz         return;
409e5e78550SMax Reitz     }
410e5e78550SMax Reitz 
411e5e78550SMax Reitz     QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
412e5e78550SMax Reitz     g_free(blk->name);
413e5e78550SMax Reitz     blk->name = NULL;
414e5e78550SMax Reitz }
415e5e78550SMax Reitz 
416e5e78550SMax Reitz /*
4177e7d56d9SMarkus Armbruster  * Return @blk's name, a non-null string.
418e5e78550SMax Reitz  * Returns an empty string iff @blk is not referenced by the monitor.
41926f54e9aSMarkus Armbruster  */
42026f54e9aSMarkus Armbruster const char *blk_name(BlockBackend *blk)
42126f54e9aSMarkus Armbruster {
422e5e78550SMax Reitz     return blk->name ?: "";
42326f54e9aSMarkus Armbruster }
42426f54e9aSMarkus Armbruster 
42526f54e9aSMarkus Armbruster /*
42626f54e9aSMarkus Armbruster  * Return the BlockBackend with name @name if it exists, else null.
42726f54e9aSMarkus Armbruster  * @name must not be null.
42826f54e9aSMarkus Armbruster  */
42926f54e9aSMarkus Armbruster BlockBackend *blk_by_name(const char *name)
43026f54e9aSMarkus Armbruster {
43174d1b8fcSMax Reitz     BlockBackend *blk = NULL;
43226f54e9aSMarkus Armbruster 
43326f54e9aSMarkus Armbruster     assert(name);
43474d1b8fcSMax Reitz     while ((blk = blk_next(blk)) != NULL) {
43526f54e9aSMarkus Armbruster         if (!strcmp(name, blk->name)) {
43626f54e9aSMarkus Armbruster             return blk;
43726f54e9aSMarkus Armbruster         }
43826f54e9aSMarkus Armbruster     }
43926f54e9aSMarkus Armbruster     return NULL;
44026f54e9aSMarkus Armbruster }
4417e7d56d9SMarkus Armbruster 
4427e7d56d9SMarkus Armbruster /*
4437e7d56d9SMarkus Armbruster  * Return the BlockDriverState attached to @blk if any, else null.
4447e7d56d9SMarkus Armbruster  */
4457e7d56d9SMarkus Armbruster BlockDriverState *blk_bs(BlockBackend *blk)
4467e7d56d9SMarkus Armbruster {
447f21d96d0SKevin Wolf     return blk->root ? blk->root->bs : NULL;
4487e7d56d9SMarkus Armbruster }
4497e7d56d9SMarkus Armbruster 
4507c8eece4SKevin Wolf static BlockBackend *bdrv_first_blk(BlockDriverState *bs)
451dde33812SKevin Wolf {
452dde33812SKevin Wolf     BdrvChild *child;
453dde33812SKevin Wolf     QLIST_FOREACH(child, &bs->parents, next_parent) {
454dde33812SKevin Wolf         if (child->role == &child_root) {
4557c8eece4SKevin Wolf             return child->opaque;
456dde33812SKevin Wolf         }
457dde33812SKevin Wolf     }
458dde33812SKevin Wolf 
4597c8eece4SKevin Wolf     return NULL;
4607c8eece4SKevin Wolf }
4617c8eece4SKevin Wolf 
4627c8eece4SKevin Wolf /*
4637c8eece4SKevin Wolf  * Returns true if @bs has an associated BlockBackend.
4647c8eece4SKevin Wolf  */
4657c8eece4SKevin Wolf bool bdrv_has_blk(BlockDriverState *bs)
4667c8eece4SKevin Wolf {
4677c8eece4SKevin Wolf     return bdrv_first_blk(bs) != NULL;
468dde33812SKevin Wolf }
469dde33812SKevin Wolf 
470dde33812SKevin Wolf /*
471b6c1bae5SKevin Wolf  * Returns true if @bs has only BlockBackends as parents.
472b6c1bae5SKevin Wolf  */
473b6c1bae5SKevin Wolf bool bdrv_is_root_node(BlockDriverState *bs)
474b6c1bae5SKevin Wolf {
475b6c1bae5SKevin Wolf     BdrvChild *c;
476b6c1bae5SKevin Wolf 
477b6c1bae5SKevin Wolf     QLIST_FOREACH(c, &bs->parents, next_parent) {
478b6c1bae5SKevin Wolf         if (c->role != &child_root) {
479b6c1bae5SKevin Wolf             return false;
480b6c1bae5SKevin Wolf         }
481b6c1bae5SKevin Wolf     }
482b6c1bae5SKevin Wolf 
483b6c1bae5SKevin Wolf     return true;
484b6c1bae5SKevin Wolf }
485b6c1bae5SKevin Wolf 
486b6c1bae5SKevin Wolf /*
48718e46a03SMarkus Armbruster  * Return @blk's DriveInfo if any, else null.
48818e46a03SMarkus Armbruster  */
48918e46a03SMarkus Armbruster DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
49018e46a03SMarkus Armbruster {
49118e46a03SMarkus Armbruster     return blk->legacy_dinfo;
49218e46a03SMarkus Armbruster }
49318e46a03SMarkus Armbruster 
49418e46a03SMarkus Armbruster /*
49518e46a03SMarkus Armbruster  * Set @blk's DriveInfo to @dinfo, and return it.
49618e46a03SMarkus Armbruster  * @blk must not have a DriveInfo set already.
49718e46a03SMarkus Armbruster  * No other BlockBackend may have the same DriveInfo set.
49818e46a03SMarkus Armbruster  */
49918e46a03SMarkus Armbruster DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
50018e46a03SMarkus Armbruster {
50118e46a03SMarkus Armbruster     assert(!blk->legacy_dinfo);
50218e46a03SMarkus Armbruster     return blk->legacy_dinfo = dinfo;
50318e46a03SMarkus Armbruster }
50418e46a03SMarkus Armbruster 
50518e46a03SMarkus Armbruster /*
50618e46a03SMarkus Armbruster  * Return the BlockBackend with DriveInfo @dinfo.
50718e46a03SMarkus Armbruster  * It must exist.
50818e46a03SMarkus Armbruster  */
50918e46a03SMarkus Armbruster BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
51018e46a03SMarkus Armbruster {
51174d1b8fcSMax Reitz     BlockBackend *blk = NULL;
51218e46a03SMarkus Armbruster 
51374d1b8fcSMax Reitz     while ((blk = blk_next(blk)) != NULL) {
51418e46a03SMarkus Armbruster         if (blk->legacy_dinfo == dinfo) {
51518e46a03SMarkus Armbruster             return blk;
51618e46a03SMarkus Armbruster         }
51718e46a03SMarkus Armbruster     }
51818e46a03SMarkus Armbruster     abort();
51918e46a03SMarkus Armbruster }
52018e46a03SMarkus Armbruster 
52118e46a03SMarkus Armbruster /*
522f2cd875dSKevin Wolf  * Returns a pointer to the publicly accessible fields of @blk.
523f2cd875dSKevin Wolf  */
524f2cd875dSKevin Wolf BlockBackendPublic *blk_get_public(BlockBackend *blk)
525f2cd875dSKevin Wolf {
526f2cd875dSKevin Wolf     return &blk->public;
527f2cd875dSKevin Wolf }
528f2cd875dSKevin Wolf 
529f2cd875dSKevin Wolf /*
530f2cd875dSKevin Wolf  * Returns a BlockBackend given the associated @public fields.
531f2cd875dSKevin Wolf  */
532f2cd875dSKevin Wolf BlockBackend *blk_by_public(BlockBackendPublic *public)
533f2cd875dSKevin Wolf {
534f2cd875dSKevin Wolf     return container_of(public, BlockBackend, public);
535f2cd875dSKevin Wolf }
536f2cd875dSKevin Wolf 
537f2cd875dSKevin Wolf /*
5381c95f7e1SMax Reitz  * Disassociates the currently associated BlockDriverState from @blk.
5391c95f7e1SMax Reitz  */
5401c95f7e1SMax Reitz void blk_remove_bs(BlockBackend *blk)
5411c95f7e1SMax Reitz {
5423301f6c6SMax Reitz     notifier_list_notify(&blk->remove_bs_notifiers, blk);
5437ca7f0f6SKevin Wolf     if (blk->public.throttle_state) {
5447ca7f0f6SKevin Wolf         throttle_timers_detach_aio_context(&blk->public.throttle_timers);
5457ca7f0f6SKevin Wolf     }
5463301f6c6SMax Reitz 
5471c95f7e1SMax Reitz     blk_update_root_state(blk);
5481c95f7e1SMax Reitz 
549f21d96d0SKevin Wolf     bdrv_root_unref_child(blk->root);
550f21d96d0SKevin Wolf     blk->root = NULL;
5511c95f7e1SMax Reitz }
5521c95f7e1SMax Reitz 
5531c95f7e1SMax Reitz /*
5540c3c36d6SMax Reitz  * Associates a new BlockDriverState with @blk.
5550c3c36d6SMax Reitz  */
556d7086422SKevin Wolf int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
5570c3c36d6SMax Reitz {
558d5e6f437SKevin Wolf     blk->root = bdrv_root_attach_child(bs, "root", &child_root,
559d7086422SKevin Wolf                                        blk->perm, blk->shared_perm, blk, errp);
560d7086422SKevin Wolf     if (blk->root == NULL) {
561d7086422SKevin Wolf         return -EPERM;
562d7086422SKevin Wolf     }
563d7086422SKevin Wolf     bdrv_ref(bs);
5643301f6c6SMax Reitz 
5653301f6c6SMax Reitz     notifier_list_notify(&blk->insert_bs_notifiers, blk);
5667ca7f0f6SKevin Wolf     if (blk->public.throttle_state) {
5677ca7f0f6SKevin Wolf         throttle_timers_attach_aio_context(
5687ca7f0f6SKevin Wolf             &blk->public.throttle_timers, bdrv_get_aio_context(bs));
5697ca7f0f6SKevin Wolf     }
570d7086422SKevin Wolf 
571d7086422SKevin Wolf     return 0;
5720c3c36d6SMax Reitz }
5730c3c36d6SMax Reitz 
574981776b3SKevin Wolf /*
575981776b3SKevin Wolf  * Sets the permission bitmasks that the user of the BlockBackend needs.
576981776b3SKevin Wolf  */
577981776b3SKevin Wolf int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,
578981776b3SKevin Wolf                  Error **errp)
579981776b3SKevin Wolf {
580981776b3SKevin Wolf     int ret;
581981776b3SKevin Wolf 
582d35ff5e6SKevin Wolf     if (blk->root && !blk->disable_perm) {
583981776b3SKevin Wolf         ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp);
584981776b3SKevin Wolf         if (ret < 0) {
585981776b3SKevin Wolf             return ret;
586981776b3SKevin Wolf         }
587981776b3SKevin Wolf     }
588981776b3SKevin Wolf 
589981776b3SKevin Wolf     blk->perm = perm;
590981776b3SKevin Wolf     blk->shared_perm = shared_perm;
591981776b3SKevin Wolf 
592981776b3SKevin Wolf     return 0;
593981776b3SKevin Wolf }
594981776b3SKevin Wolf 
595887354bdSKevin Wolf void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
596887354bdSKevin Wolf {
597887354bdSKevin Wolf     *perm = blk->perm;
598887354bdSKevin Wolf     *shared_perm = blk->shared_perm;
599887354bdSKevin Wolf }
600887354bdSKevin Wolf 
601d35ff5e6SKevin Wolf /*
602d35ff5e6SKevin Wolf  * Notifies the user of all BlockBackends that migration has completed. qdev
603d35ff5e6SKevin Wolf  * devices can tighten their permissions in response (specifically revoke
604d35ff5e6SKevin Wolf  * shared write permissions that we needed for storage migration).
605d35ff5e6SKevin Wolf  *
606d35ff5e6SKevin Wolf  * If an error is returned, the VM cannot be allowed to be resumed.
607d35ff5e6SKevin Wolf  */
608d35ff5e6SKevin Wolf void blk_resume_after_migration(Error **errp)
609d35ff5e6SKevin Wolf {
610d35ff5e6SKevin Wolf     BlockBackend *blk;
611d35ff5e6SKevin Wolf     Error *local_err = NULL;
612d35ff5e6SKevin Wolf 
613d35ff5e6SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
614d35ff5e6SKevin Wolf         if (!blk->disable_perm) {
615d35ff5e6SKevin Wolf             continue;
616d35ff5e6SKevin Wolf         }
617d35ff5e6SKevin Wolf 
618d35ff5e6SKevin Wolf         blk->disable_perm = false;
619d35ff5e6SKevin Wolf 
620d35ff5e6SKevin Wolf         blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
621d35ff5e6SKevin Wolf         if (local_err) {
622d35ff5e6SKevin Wolf             error_propagate(errp, local_err);
623d35ff5e6SKevin Wolf             blk->disable_perm = true;
624d35ff5e6SKevin Wolf             return;
625d35ff5e6SKevin Wolf         }
626d35ff5e6SKevin Wolf     }
627d35ff5e6SKevin Wolf }
628d35ff5e6SKevin Wolf 
629bbc8ea98SKevin Wolf static int blk_do_attach_dev(BlockBackend *blk, void *dev)
630a7f53e26SMarkus Armbruster {
631a7f53e26SMarkus Armbruster     if (blk->dev) {
632a7f53e26SMarkus Armbruster         return -EBUSY;
633a7f53e26SMarkus Armbruster     }
634d35ff5e6SKevin Wolf 
635d35ff5e6SKevin Wolf     /* While migration is still incoming, we don't need to apply the
636d35ff5e6SKevin Wolf      * permissions of guest device BlockBackends. We might still have a block
637d35ff5e6SKevin Wolf      * job or NBD server writing to the image for storage migration. */
638d35ff5e6SKevin Wolf     if (runstate_check(RUN_STATE_INMIGRATE)) {
639d35ff5e6SKevin Wolf         blk->disable_perm = true;
640d35ff5e6SKevin Wolf     }
641d35ff5e6SKevin Wolf 
64284ebe375SMarkus Armbruster     blk_ref(blk);
643a7f53e26SMarkus Armbruster     blk->dev = dev;
644bbc8ea98SKevin Wolf     blk->legacy_dev = false;
645373340b2SMax Reitz     blk_iostatus_reset(blk);
646d35ff5e6SKevin Wolf 
647a7f53e26SMarkus Armbruster     return 0;
648a7f53e26SMarkus Armbruster }
649a7f53e26SMarkus Armbruster 
650a7f53e26SMarkus Armbruster /*
651a7f53e26SMarkus Armbruster  * Attach device model @dev to @blk.
652bbc8ea98SKevin Wolf  * Return 0 on success, -EBUSY when a device model is attached already.
653bbc8ea98SKevin Wolf  */
654bbc8ea98SKevin Wolf int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
655bbc8ea98SKevin Wolf {
656bbc8ea98SKevin Wolf     return blk_do_attach_dev(blk, dev);
657bbc8ea98SKevin Wolf }
658bbc8ea98SKevin Wolf 
659bbc8ea98SKevin Wolf /*
660bbc8ea98SKevin Wolf  * Attach device model @dev to @blk.
661a7f53e26SMarkus Armbruster  * @blk must not have a device model attached already.
662a7f53e26SMarkus Armbruster  * TODO qdevified devices don't use this, remove when devices are qdevified
663a7f53e26SMarkus Armbruster  */
664bbc8ea98SKevin Wolf void blk_attach_dev_legacy(BlockBackend *blk, void *dev)
665a7f53e26SMarkus Armbruster {
666bbc8ea98SKevin Wolf     if (blk_do_attach_dev(blk, dev) < 0) {
667a7f53e26SMarkus Armbruster         abort();
668a7f53e26SMarkus Armbruster     }
669bbc8ea98SKevin Wolf     blk->legacy_dev = true;
670a7f53e26SMarkus Armbruster }
671a7f53e26SMarkus Armbruster 
672a7f53e26SMarkus Armbruster /*
673a7f53e26SMarkus Armbruster  * Detach device model @dev from @blk.
674a7f53e26SMarkus Armbruster  * @dev must be currently attached to @blk.
675a7f53e26SMarkus Armbruster  */
676a7f53e26SMarkus Armbruster void blk_detach_dev(BlockBackend *blk, void *dev)
677a7f53e26SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */
678a7f53e26SMarkus Armbruster {
679a7f53e26SMarkus Armbruster     assert(blk->dev == dev);
680a7f53e26SMarkus Armbruster     blk->dev = NULL;
681a7f53e26SMarkus Armbruster     blk->dev_ops = NULL;
682a7f53e26SMarkus Armbruster     blk->dev_opaque = NULL;
68368e9ec01SMax Reitz     blk->guest_block_size = 512;
684981776b3SKevin Wolf     blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
68584ebe375SMarkus Armbruster     blk_unref(blk);
686a7f53e26SMarkus Armbruster }
687a7f53e26SMarkus Armbruster 
688a7f53e26SMarkus Armbruster /*
689a7f53e26SMarkus Armbruster  * Return the device model attached to @blk if any, else null.
690a7f53e26SMarkus Armbruster  */
691a7f53e26SMarkus Armbruster void *blk_get_attached_dev(BlockBackend *blk)
692a7f53e26SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */
693a7f53e26SMarkus Armbruster {
694a7f53e26SMarkus Armbruster     return blk->dev;
695a7f53e26SMarkus Armbruster }
696a7f53e26SMarkus Armbruster 
6972d76e724SKevin Wolf /* Return the qdev ID, or if no ID is assigned the QOM path, of the block
6982d76e724SKevin Wolf  * device attached to the BlockBackend. */
6992d76e724SKevin Wolf static char *blk_get_attached_dev_id(BlockBackend *blk)
7002d76e724SKevin Wolf {
7012d76e724SKevin Wolf     DeviceState *dev;
7022d76e724SKevin Wolf 
7032d76e724SKevin Wolf     assert(!blk->legacy_dev);
7042d76e724SKevin Wolf     dev = blk->dev;
7052d76e724SKevin Wolf 
7062d76e724SKevin Wolf     if (!dev) {
7072d76e724SKevin Wolf         return g_strdup("");
7082d76e724SKevin Wolf     } else if (dev->id) {
7092d76e724SKevin Wolf         return g_strdup(dev->id);
7102d76e724SKevin Wolf     }
7112d76e724SKevin Wolf     return object_get_canonical_path(OBJECT(dev));
7122d76e724SKevin Wolf }
7132d76e724SKevin Wolf 
714a7f53e26SMarkus Armbruster /*
7151c89e1faSKevin Wolf  * Return the BlockBackend which has the device model @dev attached if it
7161c89e1faSKevin Wolf  * exists, else null.
7171c89e1faSKevin Wolf  *
7181c89e1faSKevin Wolf  * @dev must not be null.
7191c89e1faSKevin Wolf  */
7201c89e1faSKevin Wolf BlockBackend *blk_by_dev(void *dev)
7211c89e1faSKevin Wolf {
7221c89e1faSKevin Wolf     BlockBackend *blk = NULL;
7231c89e1faSKevin Wolf 
7241c89e1faSKevin Wolf     assert(dev != NULL);
7251c89e1faSKevin Wolf     while ((blk = blk_all_next(blk)) != NULL) {
7261c89e1faSKevin Wolf         if (blk->dev == dev) {
7271c89e1faSKevin Wolf             return blk;
7281c89e1faSKevin Wolf         }
7291c89e1faSKevin Wolf     }
7301c89e1faSKevin Wolf     return NULL;
7311c89e1faSKevin Wolf }
7321c89e1faSKevin Wolf 
7331c89e1faSKevin Wolf /*
734a7f53e26SMarkus Armbruster  * Set @blk's device model callbacks to @ops.
735a7f53e26SMarkus Armbruster  * @opaque is the opaque argument to pass to the callbacks.
736a7f53e26SMarkus Armbruster  * This is for use by device models.
737a7f53e26SMarkus Armbruster  */
738a7f53e26SMarkus Armbruster void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
739a7f53e26SMarkus Armbruster                      void *opaque)
740a7f53e26SMarkus Armbruster {
741bbc8ea98SKevin Wolf     /* All drivers that use blk_set_dev_ops() are qdevified and we want to keep
742f4d9cc88SJohn Snow      * it that way, so we can assume blk->dev, if present, is a DeviceState if
743f4d9cc88SJohn Snow      * blk->dev_ops is set. Non-device users may use dev_ops without device. */
744bbc8ea98SKevin Wolf     assert(!blk->legacy_dev);
745bbc8ea98SKevin Wolf 
746a7f53e26SMarkus Armbruster     blk->dev_ops = ops;
747a7f53e26SMarkus Armbruster     blk->dev_opaque = opaque;
748f4d9cc88SJohn Snow 
749f4d9cc88SJohn Snow     /* Are we currently quiesced? Should we enforce this right now? */
750f4d9cc88SJohn Snow     if (blk->quiesce_counter && ops->drained_begin) {
751f4d9cc88SJohn Snow         ops->drained_begin(opaque);
752f4d9cc88SJohn Snow     }
753a7f53e26SMarkus Armbruster }
754a7f53e26SMarkus Armbruster 
755a7f53e26SMarkus Armbruster /*
756a7f53e26SMarkus Armbruster  * Notify @blk's attached device model of media change.
75739829a01SKevin Wolf  *
75839829a01SKevin Wolf  * If @load is true, notify of media load. This action can fail, meaning that
75939829a01SKevin Wolf  * the medium cannot be loaded. @errp is set then.
76039829a01SKevin Wolf  *
76139829a01SKevin Wolf  * If @load is false, notify of media eject. This can never fail.
76239829a01SKevin Wolf  *
763a7f53e26SMarkus Armbruster  * Also send DEVICE_TRAY_MOVED events as appropriate.
764a7f53e26SMarkus Armbruster  */
76539829a01SKevin Wolf void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp)
766a7f53e26SMarkus Armbruster {
767a7f53e26SMarkus Armbruster     if (blk->dev_ops && blk->dev_ops->change_media_cb) {
768f1f57066SMax Reitz         bool tray_was_open, tray_is_open;
76939829a01SKevin Wolf         Error *local_err = NULL;
770a7f53e26SMarkus Armbruster 
7712d76e724SKevin Wolf         assert(!blk->legacy_dev);
7722d76e724SKevin Wolf 
773f1f57066SMax Reitz         tray_was_open = blk_dev_is_tray_open(blk);
77439829a01SKevin Wolf         blk->dev_ops->change_media_cb(blk->dev_opaque, load, &local_err);
77539829a01SKevin Wolf         if (local_err) {
77639829a01SKevin Wolf             assert(load == true);
77739829a01SKevin Wolf             error_propagate(errp, local_err);
77839829a01SKevin Wolf             return;
77939829a01SKevin Wolf         }
780f1f57066SMax Reitz         tray_is_open = blk_dev_is_tray_open(blk);
781f1f57066SMax Reitz 
782f1f57066SMax Reitz         if (tray_was_open != tray_is_open) {
7832d76e724SKevin Wolf             char *id = blk_get_attached_dev_id(blk);
7842d76e724SKevin Wolf             qapi_event_send_device_tray_moved(blk_name(blk), id, tray_is_open,
785f1f57066SMax Reitz                                               &error_abort);
7862d76e724SKevin Wolf             g_free(id);
787a7f53e26SMarkus Armbruster         }
788a7f53e26SMarkus Armbruster     }
789a7f53e26SMarkus Armbruster }
790a7f53e26SMarkus Armbruster 
7915c8cab48SKevin Wolf static void blk_root_change_media(BdrvChild *child, bool load)
7925c8cab48SKevin Wolf {
79339829a01SKevin Wolf     blk_dev_change_media_cb(child->opaque, load, NULL);
7945c8cab48SKevin Wolf }
7955c8cab48SKevin Wolf 
796a7f53e26SMarkus Armbruster /*
797a7f53e26SMarkus Armbruster  * Does @blk's attached device model have removable media?
798a7f53e26SMarkus Armbruster  * %true if no device model is attached.
799a7f53e26SMarkus Armbruster  */
800a7f53e26SMarkus Armbruster bool blk_dev_has_removable_media(BlockBackend *blk)
801a7f53e26SMarkus Armbruster {
802a7f53e26SMarkus Armbruster     return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
803a7f53e26SMarkus Armbruster }
804a7f53e26SMarkus Armbruster 
805a7f53e26SMarkus Armbruster /*
8068f3a73bcSMax Reitz  * Does @blk's attached device model have a tray?
8078f3a73bcSMax Reitz  */
8088f3a73bcSMax Reitz bool blk_dev_has_tray(BlockBackend *blk)
8098f3a73bcSMax Reitz {
8108f3a73bcSMax Reitz     return blk->dev_ops && blk->dev_ops->is_tray_open;
8118f3a73bcSMax Reitz }
8128f3a73bcSMax Reitz 
8138f3a73bcSMax Reitz /*
814a7f53e26SMarkus Armbruster  * Notify @blk's attached device model of a media eject request.
815a7f53e26SMarkus Armbruster  * If @force is true, the medium is about to be yanked out forcefully.
816a7f53e26SMarkus Armbruster  */
817a7f53e26SMarkus Armbruster void blk_dev_eject_request(BlockBackend *blk, bool force)
818a7f53e26SMarkus Armbruster {
819a7f53e26SMarkus Armbruster     if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
820a7f53e26SMarkus Armbruster         blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
821a7f53e26SMarkus Armbruster     }
822a7f53e26SMarkus Armbruster }
823a7f53e26SMarkus Armbruster 
824a7f53e26SMarkus Armbruster /*
825a7f53e26SMarkus Armbruster  * Does @blk's attached device model have a tray, and is it open?
826a7f53e26SMarkus Armbruster  */
827a7f53e26SMarkus Armbruster bool blk_dev_is_tray_open(BlockBackend *blk)
828a7f53e26SMarkus Armbruster {
8298f3a73bcSMax Reitz     if (blk_dev_has_tray(blk)) {
830a7f53e26SMarkus Armbruster         return blk->dev_ops->is_tray_open(blk->dev_opaque);
831a7f53e26SMarkus Armbruster     }
832a7f53e26SMarkus Armbruster     return false;
833a7f53e26SMarkus Armbruster }
834a7f53e26SMarkus Armbruster 
835a7f53e26SMarkus Armbruster /*
836a7f53e26SMarkus Armbruster  * Does @blk's attached device model have the medium locked?
837a7f53e26SMarkus Armbruster  * %false if the device model has no such lock.
838a7f53e26SMarkus Armbruster  */
839a7f53e26SMarkus Armbruster bool blk_dev_is_medium_locked(BlockBackend *blk)
840a7f53e26SMarkus Armbruster {
841a7f53e26SMarkus Armbruster     if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
842a7f53e26SMarkus Armbruster         return blk->dev_ops->is_medium_locked(blk->dev_opaque);
843a7f53e26SMarkus Armbruster     }
844a7f53e26SMarkus Armbruster     return false;
845a7f53e26SMarkus Armbruster }
846a7f53e26SMarkus Armbruster 
847a7f53e26SMarkus Armbruster /*
848a7f53e26SMarkus Armbruster  * Notify @blk's attached device model of a backend size change.
849a7f53e26SMarkus Armbruster  */
8505c8cab48SKevin Wolf static void blk_root_resize(BdrvChild *child)
851a7f53e26SMarkus Armbruster {
8525c8cab48SKevin Wolf     BlockBackend *blk = child->opaque;
8535c8cab48SKevin Wolf 
854a7f53e26SMarkus Armbruster     if (blk->dev_ops && blk->dev_ops->resize_cb) {
855a7f53e26SMarkus Armbruster         blk->dev_ops->resize_cb(blk->dev_opaque);
856a7f53e26SMarkus Armbruster     }
857a7f53e26SMarkus Armbruster }
858a7f53e26SMarkus Armbruster 
8594be74634SMarkus Armbruster void blk_iostatus_enable(BlockBackend *blk)
8604be74634SMarkus Armbruster {
861373340b2SMax Reitz     blk->iostatus_enabled = true;
862373340b2SMax Reitz     blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
863373340b2SMax Reitz }
864373340b2SMax Reitz 
865373340b2SMax Reitz /* The I/O status is only enabled if the drive explicitly
866373340b2SMax Reitz  * enables it _and_ the VM is configured to stop on errors */
867373340b2SMax Reitz bool blk_iostatus_is_enabled(const BlockBackend *blk)
868373340b2SMax Reitz {
869373340b2SMax Reitz     return (blk->iostatus_enabled &&
870373340b2SMax Reitz            (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
871373340b2SMax Reitz             blk->on_write_error == BLOCKDEV_ON_ERROR_STOP   ||
872373340b2SMax Reitz             blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
873373340b2SMax Reitz }
874373340b2SMax Reitz 
875373340b2SMax Reitz BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
876373340b2SMax Reitz {
877373340b2SMax Reitz     return blk->iostatus;
878373340b2SMax Reitz }
879373340b2SMax Reitz 
880373340b2SMax Reitz void blk_iostatus_disable(BlockBackend *blk)
881373340b2SMax Reitz {
882373340b2SMax Reitz     blk->iostatus_enabled = false;
883373340b2SMax Reitz }
884373340b2SMax Reitz 
885373340b2SMax Reitz void blk_iostatus_reset(BlockBackend *blk)
886373340b2SMax Reitz {
887373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
888f21d96d0SKevin Wolf         BlockDriverState *bs = blk_bs(blk);
889373340b2SMax Reitz         blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
890f21d96d0SKevin Wolf         if (bs && bs->job) {
891f21d96d0SKevin Wolf             block_job_iostatus_reset(bs->job);
892373340b2SMax Reitz         }
893373340b2SMax Reitz     }
894373340b2SMax Reitz }
895373340b2SMax Reitz 
896373340b2SMax Reitz void blk_iostatus_set_err(BlockBackend *blk, int error)
897373340b2SMax Reitz {
898373340b2SMax Reitz     assert(blk_iostatus_is_enabled(blk));
899373340b2SMax Reitz     if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
900373340b2SMax Reitz         blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
901373340b2SMax Reitz                                           BLOCK_DEVICE_IO_STATUS_FAILED;
902373340b2SMax Reitz     }
9034be74634SMarkus Armbruster }
9044be74634SMarkus Armbruster 
905c10c9d96SKevin Wolf void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
906c10c9d96SKevin Wolf {
907c10c9d96SKevin Wolf     blk->allow_write_beyond_eof = allow;
908c10c9d96SKevin Wolf }
909c10c9d96SKevin Wolf 
910e7f7d676SMax Reitz static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
911e7f7d676SMax Reitz                                   size_t size)
912e7f7d676SMax Reitz {
913e7f7d676SMax Reitz     int64_t len;
914e7f7d676SMax Reitz 
915e7f7d676SMax Reitz     if (size > INT_MAX) {
916e7f7d676SMax Reitz         return -EIO;
917e7f7d676SMax Reitz     }
918e7f7d676SMax Reitz 
919c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
920e7f7d676SMax Reitz         return -ENOMEDIUM;
921e7f7d676SMax Reitz     }
922e7f7d676SMax Reitz 
923c10c9d96SKevin Wolf     if (offset < 0) {
924c10c9d96SKevin Wolf         return -EIO;
925c10c9d96SKevin Wolf     }
926c10c9d96SKevin Wolf 
927c10c9d96SKevin Wolf     if (!blk->allow_write_beyond_eof) {
928e7f7d676SMax Reitz         len = blk_getlength(blk);
929e7f7d676SMax Reitz         if (len < 0) {
930e7f7d676SMax Reitz             return len;
931e7f7d676SMax Reitz         }
932e7f7d676SMax Reitz 
933e7f7d676SMax Reitz         if (offset > len || len - offset < size) {
934e7f7d676SMax Reitz             return -EIO;
935e7f7d676SMax Reitz         }
936c10c9d96SKevin Wolf     }
937e7f7d676SMax Reitz 
938e7f7d676SMax Reitz     return 0;
939e7f7d676SMax Reitz }
940e7f7d676SMax Reitz 
9411e98fefdSKevin Wolf int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
9421bf1cbc9SKevin Wolf                                unsigned int bytes, QEMUIOVector *qiov,
9431bf1cbc9SKevin Wolf                                BdrvRequestFlags flags)
9444be74634SMarkus Armbruster {
9451e98fefdSKevin Wolf     int ret;
94699723548SPaolo Bonzini     BlockDriverState *bs = blk_bs(blk);
9471e98fefdSKevin Wolf 
94899723548SPaolo Bonzini     trace_blk_co_preadv(blk, bs, offset, bytes, flags);
9491e98fefdSKevin Wolf 
9501e98fefdSKevin Wolf     ret = blk_check_byte_request(blk, offset, bytes);
951e7f7d676SMax Reitz     if (ret < 0) {
952e7f7d676SMax Reitz         return ret;
953e7f7d676SMax Reitz     }
954e7f7d676SMax Reitz 
95599723548SPaolo Bonzini     bdrv_inc_in_flight(bs);
95699723548SPaolo Bonzini 
957441565b2SKevin Wolf     /* throttling disk I/O */
958441565b2SKevin Wolf     if (blk->public.throttle_state) {
959441565b2SKevin Wolf         throttle_group_co_io_limits_intercept(blk, bytes, false);
960441565b2SKevin Wolf     }
961441565b2SKevin Wolf 
96299723548SPaolo Bonzini     ret = bdrv_co_preadv(blk->root, offset, bytes, qiov, flags);
96399723548SPaolo Bonzini     bdrv_dec_in_flight(bs);
96499723548SPaolo Bonzini     return ret;
9651bf1cbc9SKevin Wolf }
9661bf1cbc9SKevin Wolf 
9671e98fefdSKevin Wolf int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
968a8823a3bSKevin Wolf                                 unsigned int bytes, QEMUIOVector *qiov,
969a8823a3bSKevin Wolf                                 BdrvRequestFlags flags)
970a8823a3bSKevin Wolf {
971bfd18d1eSKevin Wolf     int ret;
97299723548SPaolo Bonzini     BlockDriverState *bs = blk_bs(blk);
973bfd18d1eSKevin Wolf 
97499723548SPaolo Bonzini     trace_blk_co_pwritev(blk, bs, offset, bytes, flags);
9751e98fefdSKevin Wolf 
976bfd18d1eSKevin Wolf     ret = blk_check_byte_request(blk, offset, bytes);
977a8823a3bSKevin Wolf     if (ret < 0) {
978a8823a3bSKevin Wolf         return ret;
979a8823a3bSKevin Wolf     }
980a8823a3bSKevin Wolf 
98199723548SPaolo Bonzini     bdrv_inc_in_flight(bs);
98299723548SPaolo Bonzini 
983441565b2SKevin Wolf     /* throttling disk I/O */
984441565b2SKevin Wolf     if (blk->public.throttle_state) {
985441565b2SKevin Wolf         throttle_group_co_io_limits_intercept(blk, bytes, true);
986441565b2SKevin Wolf     }
987441565b2SKevin Wolf 
988bfd18d1eSKevin Wolf     if (!blk->enable_write_cache) {
989bfd18d1eSKevin Wolf         flags |= BDRV_REQ_FUA;
990bfd18d1eSKevin Wolf     }
991bfd18d1eSKevin Wolf 
99299723548SPaolo Bonzini     ret = bdrv_co_pwritev(blk->root, offset, bytes, qiov, flags);
99399723548SPaolo Bonzini     bdrv_dec_in_flight(bs);
99499723548SPaolo Bonzini     return ret;
995a8823a3bSKevin Wolf }
996a8823a3bSKevin Wolf 
9971bf1cbc9SKevin Wolf typedef struct BlkRwCo {
9981bf1cbc9SKevin Wolf     BlockBackend *blk;
9991bf1cbc9SKevin Wolf     int64_t offset;
10001bf1cbc9SKevin Wolf     QEMUIOVector *qiov;
10011bf1cbc9SKevin Wolf     int ret;
10021bf1cbc9SKevin Wolf     BdrvRequestFlags flags;
10031bf1cbc9SKevin Wolf } BlkRwCo;
10041bf1cbc9SKevin Wolf 
10051bf1cbc9SKevin Wolf static void blk_read_entry(void *opaque)
10061bf1cbc9SKevin Wolf {
10071bf1cbc9SKevin Wolf     BlkRwCo *rwco = opaque;
10081bf1cbc9SKevin Wolf 
10091bf1cbc9SKevin Wolf     rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
10101bf1cbc9SKevin Wolf                               rwco->qiov, rwco->flags);
10111bf1cbc9SKevin Wolf }
10121bf1cbc9SKevin Wolf 
1013a8823a3bSKevin Wolf static void blk_write_entry(void *opaque)
1014a8823a3bSKevin Wolf {
1015a8823a3bSKevin Wolf     BlkRwCo *rwco = opaque;
1016a8823a3bSKevin Wolf 
1017a8823a3bSKevin Wolf     rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
1018a8823a3bSKevin Wolf                                rwco->qiov, rwco->flags);
1019a8823a3bSKevin Wolf }
1020a8823a3bSKevin Wolf 
1021a55d3fbaSKevin Wolf static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
1022a55d3fbaSKevin Wolf                    int64_t bytes, CoroutineEntry co_entry,
1023fc1453cdSKevin Wolf                    BdrvRequestFlags flags)
10241bf1cbc9SKevin Wolf {
10251bf1cbc9SKevin Wolf     QEMUIOVector qiov;
10261bf1cbc9SKevin Wolf     struct iovec iov;
10271bf1cbc9SKevin Wolf     BlkRwCo rwco;
10281bf1cbc9SKevin Wolf 
10291bf1cbc9SKevin Wolf     iov = (struct iovec) {
10301bf1cbc9SKevin Wolf         .iov_base = buf,
1031a55d3fbaSKevin Wolf         .iov_len = bytes,
10321bf1cbc9SKevin Wolf     };
10331bf1cbc9SKevin Wolf     qemu_iovec_init_external(&qiov, &iov, 1);
10341bf1cbc9SKevin Wolf 
10351bf1cbc9SKevin Wolf     rwco = (BlkRwCo) {
10361bf1cbc9SKevin Wolf         .blk    = blk,
1037a55d3fbaSKevin Wolf         .offset = offset,
10381bf1cbc9SKevin Wolf         .qiov   = &qiov,
1039fc1453cdSKevin Wolf         .flags  = flags,
10401bf1cbc9SKevin Wolf         .ret    = NOT_DONE,
10411bf1cbc9SKevin Wolf     };
10421bf1cbc9SKevin Wolf 
104335f106e6SPaolo Bonzini     if (qemu_in_coroutine()) {
104435f106e6SPaolo Bonzini         /* Fast-path if already in coroutine context */
104535f106e6SPaolo Bonzini         co_entry(&rwco);
104635f106e6SPaolo Bonzini     } else {
104735f106e6SPaolo Bonzini         Coroutine *co = qemu_coroutine_create(co_entry, &rwco);
1048*e92f0e19SFam Zheng         bdrv_coroutine_enter(blk_bs(blk), co);
104988b062c2SPaolo Bonzini         BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE);
105035f106e6SPaolo Bonzini     }
10511bf1cbc9SKevin Wolf 
10521bf1cbc9SKevin Wolf     return rwco.ret;
10534be74634SMarkus Armbruster }
10544be74634SMarkus Armbruster 
1055b7d17f9fSEric Blake int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf,
1056b7d17f9fSEric Blake                           int count)
10574be74634SMarkus Armbruster {
10585bd51196SKevin Wolf     int ret;
10595bd51196SKevin Wolf 
1060b7d17f9fSEric Blake     ret = blk_check_byte_request(blk, offset, count);
1061e7f7d676SMax Reitz     if (ret < 0) {
1062e7f7d676SMax Reitz         return ret;
1063e7f7d676SMax Reitz     }
1064e7f7d676SMax Reitz 
1065c2066af0SKevin Wolf     blk_root_drained_begin(blk->root);
1066b7d17f9fSEric Blake     ret = blk_pread(blk, offset, buf, count);
1067c2066af0SKevin Wolf     blk_root_drained_end(blk->root);
10685bd51196SKevin Wolf     return ret;
10694be74634SMarkus Armbruster }
10704be74634SMarkus Armbruster 
1071d004bd52SEric Blake int blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1072983a1600SEric Blake                       int count, BdrvRequestFlags flags)
10730df89e8eSKevin Wolf {
1074983a1600SEric Blake     return blk_prw(blk, offset, NULL, count, blk_write_entry,
107516aaf975SKevin Wolf                    flags | BDRV_REQ_ZERO_WRITE);
10760df89e8eSKevin Wolf }
10770df89e8eSKevin Wolf 
1078720ff280SKevin Wolf int blk_make_zero(BlockBackend *blk, BdrvRequestFlags flags)
1079720ff280SKevin Wolf {
1080720ff280SKevin Wolf     return bdrv_make_zero(blk->root, flags);
1081720ff280SKevin Wolf }
1082720ff280SKevin Wolf 
1083e7f7d676SMax Reitz static void error_callback_bh(void *opaque)
1084e7f7d676SMax Reitz {
1085e7f7d676SMax Reitz     struct BlockBackendAIOCB *acb = opaque;
108699723548SPaolo Bonzini 
108799723548SPaolo Bonzini     bdrv_dec_in_flight(acb->common.bs);
1088e7f7d676SMax Reitz     acb->common.cb(acb->common.opaque, acb->ret);
1089e7f7d676SMax Reitz     qemu_aio_unref(acb);
1090e7f7d676SMax Reitz }
1091e7f7d676SMax Reitz 
1092ca78ecfaSPeter Lieven BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
1093ca78ecfaSPeter Lieven                                   BlockCompletionFunc *cb,
1094e7f7d676SMax Reitz                                   void *opaque, int ret)
1095e7f7d676SMax Reitz {
1096e7f7d676SMax Reitz     struct BlockBackendAIOCB *acb;
1097e7f7d676SMax Reitz 
109899723548SPaolo Bonzini     bdrv_inc_in_flight(blk_bs(blk));
1099e7f7d676SMax Reitz     acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
11004981bdecSMax Reitz     acb->blk = blk;
1101e7f7d676SMax Reitz     acb->ret = ret;
1102e7f7d676SMax Reitz 
1103fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(blk_get_aio_context(blk), error_callback_bh, acb);
1104e7f7d676SMax Reitz     return &acb->common;
1105e7f7d676SMax Reitz }
1106e7f7d676SMax Reitz 
110757d6a428SKevin Wolf typedef struct BlkAioEmAIOCB {
110857d6a428SKevin Wolf     BlockAIOCB common;
110957d6a428SKevin Wolf     BlkRwCo rwco;
11107fa84cd8SKevin Wolf     int bytes;
111157d6a428SKevin Wolf     bool has_returned;
111257d6a428SKevin Wolf } BlkAioEmAIOCB;
111357d6a428SKevin Wolf 
111457d6a428SKevin Wolf static const AIOCBInfo blk_aio_em_aiocb_info = {
111557d6a428SKevin Wolf     .aiocb_size         = sizeof(BlkAioEmAIOCB),
111657d6a428SKevin Wolf };
111757d6a428SKevin Wolf 
111857d6a428SKevin Wolf static void blk_aio_complete(BlkAioEmAIOCB *acb)
111957d6a428SKevin Wolf {
112057d6a428SKevin Wolf     if (acb->has_returned) {
112199723548SPaolo Bonzini         bdrv_dec_in_flight(acb->common.bs);
112257d6a428SKevin Wolf         acb->common.cb(acb->common.opaque, acb->rwco.ret);
112357d6a428SKevin Wolf         qemu_aio_unref(acb);
112457d6a428SKevin Wolf     }
112557d6a428SKevin Wolf }
112657d6a428SKevin Wolf 
112757d6a428SKevin Wolf static void blk_aio_complete_bh(void *opaque)
112857d6a428SKevin Wolf {
1129fffb6e12SPaolo Bonzini     BlkAioEmAIOCB *acb = opaque;
1130fffb6e12SPaolo Bonzini     assert(acb->has_returned);
1131fffb6e12SPaolo Bonzini     blk_aio_complete(acb);
113257d6a428SKevin Wolf }
113357d6a428SKevin Wolf 
11347fa84cd8SKevin Wolf static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
113557d6a428SKevin Wolf                                 QEMUIOVector *qiov, CoroutineEntry co_entry,
113657d6a428SKevin Wolf                                 BdrvRequestFlags flags,
113757d6a428SKevin Wolf                                 BlockCompletionFunc *cb, void *opaque)
113857d6a428SKevin Wolf {
113957d6a428SKevin Wolf     BlkAioEmAIOCB *acb;
114057d6a428SKevin Wolf     Coroutine *co;
114157d6a428SKevin Wolf 
114299723548SPaolo Bonzini     bdrv_inc_in_flight(blk_bs(blk));
114357d6a428SKevin Wolf     acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
114457d6a428SKevin Wolf     acb->rwco = (BlkRwCo) {
114557d6a428SKevin Wolf         .blk    = blk,
114657d6a428SKevin Wolf         .offset = offset,
114757d6a428SKevin Wolf         .qiov   = qiov,
114857d6a428SKevin Wolf         .flags  = flags,
114957d6a428SKevin Wolf         .ret    = NOT_DONE,
115057d6a428SKevin Wolf     };
11517fa84cd8SKevin Wolf     acb->bytes = bytes;
115257d6a428SKevin Wolf     acb->has_returned = false;
115357d6a428SKevin Wolf 
11540b8b8753SPaolo Bonzini     co = qemu_coroutine_create(co_entry, acb);
1155*e92f0e19SFam Zheng     bdrv_coroutine_enter(blk_bs(blk), co);
115657d6a428SKevin Wolf 
115757d6a428SKevin Wolf     acb->has_returned = true;
115857d6a428SKevin Wolf     if (acb->rwco.ret != NOT_DONE) {
1159fffb6e12SPaolo Bonzini         aio_bh_schedule_oneshot(blk_get_aio_context(blk),
1160fffb6e12SPaolo Bonzini                                 blk_aio_complete_bh, acb);
116157d6a428SKevin Wolf     }
116257d6a428SKevin Wolf 
116357d6a428SKevin Wolf     return &acb->common;
116457d6a428SKevin Wolf }
116557d6a428SKevin Wolf 
116657d6a428SKevin Wolf static void blk_aio_read_entry(void *opaque)
116757d6a428SKevin Wolf {
116857d6a428SKevin Wolf     BlkAioEmAIOCB *acb = opaque;
116957d6a428SKevin Wolf     BlkRwCo *rwco = &acb->rwco;
117057d6a428SKevin Wolf 
11717fa84cd8SKevin Wolf     assert(rwco->qiov->size == acb->bytes);
11727fa84cd8SKevin Wolf     rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes,
117357d6a428SKevin Wolf                               rwco->qiov, rwco->flags);
117457d6a428SKevin Wolf     blk_aio_complete(acb);
117557d6a428SKevin Wolf }
117657d6a428SKevin Wolf 
117757d6a428SKevin Wolf static void blk_aio_write_entry(void *opaque)
117857d6a428SKevin Wolf {
117957d6a428SKevin Wolf     BlkAioEmAIOCB *acb = opaque;
118057d6a428SKevin Wolf     BlkRwCo *rwco = &acb->rwco;
118157d6a428SKevin Wolf 
11827fa84cd8SKevin Wolf     assert(!rwco->qiov || rwco->qiov->size == acb->bytes);
11837fa84cd8SKevin Wolf     rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes,
118457d6a428SKevin Wolf                                rwco->qiov, rwco->flags);
118557d6a428SKevin Wolf     blk_aio_complete(acb);
118657d6a428SKevin Wolf }
118757d6a428SKevin Wolf 
1188d004bd52SEric Blake BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1189983a1600SEric Blake                                   int count, BdrvRequestFlags flags,
11904be74634SMarkus Armbruster                                   BlockCompletionFunc *cb, void *opaque)
11914be74634SMarkus Armbruster {
1192983a1600SEric Blake     return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
1193983a1600SEric Blake                         flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
11944be74634SMarkus Armbruster }
11954be74634SMarkus Armbruster 
11964be74634SMarkus Armbruster int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
11974be74634SMarkus Armbruster {
1198a55d3fbaSKevin Wolf     int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
1199e7f7d676SMax Reitz     if (ret < 0) {
1200e7f7d676SMax Reitz         return ret;
1201e7f7d676SMax Reitz     }
1202a55d3fbaSKevin Wolf     return count;
12034be74634SMarkus Armbruster }
12044be74634SMarkus Armbruster 
12058341f00dSEric Blake int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
12068341f00dSEric Blake                BdrvRequestFlags flags)
12074be74634SMarkus Armbruster {
12088341f00dSEric Blake     int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
12098341f00dSEric Blake                       flags);
1210e7f7d676SMax Reitz     if (ret < 0) {
1211e7f7d676SMax Reitz         return ret;
1212e7f7d676SMax Reitz     }
1213a55d3fbaSKevin Wolf     return count;
12144be74634SMarkus Armbruster }
12154be74634SMarkus Armbruster 
12164be74634SMarkus Armbruster int64_t blk_getlength(BlockBackend *blk)
12174be74634SMarkus Armbruster {
1218c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1219c09ba36cSMax Reitz         return -ENOMEDIUM;
1220c09ba36cSMax Reitz     }
1221c09ba36cSMax Reitz 
1222f21d96d0SKevin Wolf     return bdrv_getlength(blk_bs(blk));
12234be74634SMarkus Armbruster }
12244be74634SMarkus Armbruster 
12254be74634SMarkus Armbruster void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
12264be74634SMarkus Armbruster {
1227f21d96d0SKevin Wolf     if (!blk_bs(blk)) {
1228a46fc9c9SMax Reitz         *nb_sectors_ptr = 0;
1229a46fc9c9SMax Reitz     } else {
1230f21d96d0SKevin Wolf         bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
12314be74634SMarkus Armbruster     }
1232a46fc9c9SMax Reitz }
12334be74634SMarkus Armbruster 
12341ef01253SMax Reitz int64_t blk_nb_sectors(BlockBackend *blk)
12351ef01253SMax Reitz {
1236c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1237c09ba36cSMax Reitz         return -ENOMEDIUM;
1238c09ba36cSMax Reitz     }
1239c09ba36cSMax Reitz 
1240f21d96d0SKevin Wolf     return bdrv_nb_sectors(blk_bs(blk));
12411ef01253SMax Reitz }
12421ef01253SMax Reitz 
124360cb2fa7SEric Blake BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
124460cb2fa7SEric Blake                            QEMUIOVector *qiov, BdrvRequestFlags flags,
124560cb2fa7SEric Blake                            BlockCompletionFunc *cb, void *opaque)
124660cb2fa7SEric Blake {
124760cb2fa7SEric Blake     return blk_aio_prwv(blk, offset, qiov->size, qiov,
124860cb2fa7SEric Blake                         blk_aio_read_entry, flags, cb, opaque);
124960cb2fa7SEric Blake }
125060cb2fa7SEric Blake 
125160cb2fa7SEric Blake BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
125260cb2fa7SEric Blake                             QEMUIOVector *qiov, BdrvRequestFlags flags,
125360cb2fa7SEric Blake                             BlockCompletionFunc *cb, void *opaque)
125460cb2fa7SEric Blake {
125560cb2fa7SEric Blake     return blk_aio_prwv(blk, offset, qiov->size, qiov,
125660cb2fa7SEric Blake                         blk_aio_write_entry, flags, cb, opaque);
125760cb2fa7SEric Blake }
125860cb2fa7SEric Blake 
1259be07a889SKevin Wolf static void blk_aio_flush_entry(void *opaque)
1260be07a889SKevin Wolf {
1261be07a889SKevin Wolf     BlkAioEmAIOCB *acb = opaque;
1262be07a889SKevin Wolf     BlkRwCo *rwco = &acb->rwco;
1263be07a889SKevin Wolf 
1264be07a889SKevin Wolf     rwco->ret = blk_co_flush(rwco->blk);
1265be07a889SKevin Wolf     blk_aio_complete(acb);
1266be07a889SKevin Wolf }
1267be07a889SKevin Wolf 
12684be74634SMarkus Armbruster BlockAIOCB *blk_aio_flush(BlockBackend *blk,
12694be74634SMarkus Armbruster                           BlockCompletionFunc *cb, void *opaque)
12704be74634SMarkus Armbruster {
1271be07a889SKevin Wolf     return blk_aio_prwv(blk, 0, 0, NULL, blk_aio_flush_entry, 0, cb, opaque);
12724be74634SMarkus Armbruster }
12734be74634SMarkus Armbruster 
12748c2e3dd5SKevin Wolf static void blk_aio_pdiscard_entry(void *opaque)
12758c2e3dd5SKevin Wolf {
12768c2e3dd5SKevin Wolf     BlkAioEmAIOCB *acb = opaque;
12778c2e3dd5SKevin Wolf     BlkRwCo *rwco = &acb->rwco;
12788c2e3dd5SKevin Wolf 
12798c2e3dd5SKevin Wolf     rwco->ret = blk_co_pdiscard(rwco->blk, rwco->offset, acb->bytes);
12808c2e3dd5SKevin Wolf     blk_aio_complete(acb);
12818c2e3dd5SKevin Wolf }
12828c2e3dd5SKevin Wolf 
12831c6c4bb7SEric Blake BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk,
12841c6c4bb7SEric Blake                              int64_t offset, int count,
12854be74634SMarkus Armbruster                              BlockCompletionFunc *cb, void *opaque)
12864be74634SMarkus Armbruster {
12878c2e3dd5SKevin Wolf     return blk_aio_prwv(blk, offset, count, NULL, blk_aio_pdiscard_entry, 0,
12888c2e3dd5SKevin Wolf                         cb, opaque);
12894be74634SMarkus Armbruster }
12904be74634SMarkus Armbruster 
12914be74634SMarkus Armbruster void blk_aio_cancel(BlockAIOCB *acb)
12924be74634SMarkus Armbruster {
12934be74634SMarkus Armbruster     bdrv_aio_cancel(acb);
12944be74634SMarkus Armbruster }
12954be74634SMarkus Armbruster 
12964be74634SMarkus Armbruster void blk_aio_cancel_async(BlockAIOCB *acb)
12974be74634SMarkus Armbruster {
12984be74634SMarkus Armbruster     bdrv_aio_cancel_async(acb);
12994be74634SMarkus Armbruster }
13004be74634SMarkus Armbruster 
130148af776aSKevin Wolf int blk_co_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
13024be74634SMarkus Armbruster {
1303c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1304c09ba36cSMax Reitz         return -ENOMEDIUM;
1305c09ba36cSMax Reitz     }
1306c09ba36cSMax Reitz 
130748af776aSKevin Wolf     return bdrv_co_ioctl(blk_bs(blk), req, buf);
130848af776aSKevin Wolf }
130948af776aSKevin Wolf 
131048af776aSKevin Wolf static void blk_ioctl_entry(void *opaque)
131148af776aSKevin Wolf {
131248af776aSKevin Wolf     BlkRwCo *rwco = opaque;
131348af776aSKevin Wolf     rwco->ret = blk_co_ioctl(rwco->blk, rwco->offset,
131448af776aSKevin Wolf                              rwco->qiov->iov[0].iov_base);
131548af776aSKevin Wolf }
131648af776aSKevin Wolf 
131748af776aSKevin Wolf int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
131848af776aSKevin Wolf {
131948af776aSKevin Wolf     return blk_prw(blk, req, buf, 0, blk_ioctl_entry, 0);
132048af776aSKevin Wolf }
132148af776aSKevin Wolf 
132248af776aSKevin Wolf static void blk_aio_ioctl_entry(void *opaque)
132348af776aSKevin Wolf {
132448af776aSKevin Wolf     BlkAioEmAIOCB *acb = opaque;
132548af776aSKevin Wolf     BlkRwCo *rwco = &acb->rwco;
132648af776aSKevin Wolf 
132748af776aSKevin Wolf     rwco->ret = blk_co_ioctl(rwco->blk, rwco->offset,
132848af776aSKevin Wolf                              rwco->qiov->iov[0].iov_base);
132948af776aSKevin Wolf     blk_aio_complete(acb);
13304be74634SMarkus Armbruster }
13314be74634SMarkus Armbruster 
13324be74634SMarkus Armbruster BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
13334be74634SMarkus Armbruster                           BlockCompletionFunc *cb, void *opaque)
13344be74634SMarkus Armbruster {
133548af776aSKevin Wolf     QEMUIOVector qiov;
133648af776aSKevin Wolf     struct iovec iov;
1337c09ba36cSMax Reitz 
133848af776aSKevin Wolf     iov = (struct iovec) {
133948af776aSKevin Wolf         .iov_base = buf,
134048af776aSKevin Wolf         .iov_len = 0,
134148af776aSKevin Wolf     };
134248af776aSKevin Wolf     qemu_iovec_init_external(&qiov, &iov, 1);
134348af776aSKevin Wolf 
134448af776aSKevin Wolf     return blk_aio_prwv(blk, req, 0, &qiov, blk_aio_ioctl_entry, 0, cb, opaque);
13454be74634SMarkus Armbruster }
13464be74634SMarkus Armbruster 
13471c6c4bb7SEric Blake int blk_co_pdiscard(BlockBackend *blk, int64_t offset, int count)
13482bb0dce7SMax Reitz {
13491c6c4bb7SEric Blake     int ret = blk_check_byte_request(blk, offset, count);
1350e7f7d676SMax Reitz     if (ret < 0) {
1351e7f7d676SMax Reitz         return ret;
1352e7f7d676SMax Reitz     }
1353e7f7d676SMax Reitz 
13541c6c4bb7SEric Blake     return bdrv_co_pdiscard(blk_bs(blk), offset, count);
13552bb0dce7SMax Reitz }
13562bb0dce7SMax Reitz 
13572bb0dce7SMax Reitz int blk_co_flush(BlockBackend *blk)
13582bb0dce7SMax Reitz {
1359c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1360c09ba36cSMax Reitz         return -ENOMEDIUM;
1361c09ba36cSMax Reitz     }
1362c09ba36cSMax Reitz 
1363f21d96d0SKevin Wolf     return bdrv_co_flush(blk_bs(blk));
13642bb0dce7SMax Reitz }
13652bb0dce7SMax Reitz 
1366be07a889SKevin Wolf static void blk_flush_entry(void *opaque)
13674be74634SMarkus Armbruster {
1368be07a889SKevin Wolf     BlkRwCo *rwco = opaque;
1369be07a889SKevin Wolf     rwco->ret = blk_co_flush(rwco->blk);
1370c09ba36cSMax Reitz }
1371c09ba36cSMax Reitz 
1372be07a889SKevin Wolf int blk_flush(BlockBackend *blk)
1373be07a889SKevin Wolf {
1374be07a889SKevin Wolf     return blk_prw(blk, 0, NULL, 0, blk_flush_entry, 0);
13754be74634SMarkus Armbruster }
13764be74634SMarkus Armbruster 
137797b0385aSAlexander Yarygin void blk_drain(BlockBackend *blk)
137897b0385aSAlexander Yarygin {
1379f21d96d0SKevin Wolf     if (blk_bs(blk)) {
1380f21d96d0SKevin Wolf         bdrv_drain(blk_bs(blk));
138197b0385aSAlexander Yarygin     }
1382a46fc9c9SMax Reitz }
138397b0385aSAlexander Yarygin 
13844be74634SMarkus Armbruster void blk_drain_all(void)
13854be74634SMarkus Armbruster {
13864be74634SMarkus Armbruster     bdrv_drain_all();
13874be74634SMarkus Armbruster }
13884be74634SMarkus Armbruster 
1389373340b2SMax Reitz void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1390373340b2SMax Reitz                       BlockdevOnError on_write_error)
1391373340b2SMax Reitz {
1392373340b2SMax Reitz     blk->on_read_error = on_read_error;
1393373340b2SMax Reitz     blk->on_write_error = on_write_error;
1394373340b2SMax Reitz }
1395373340b2SMax Reitz 
13964be74634SMarkus Armbruster BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
13974be74634SMarkus Armbruster {
1398373340b2SMax Reitz     return is_read ? blk->on_read_error : blk->on_write_error;
13994be74634SMarkus Armbruster }
14004be74634SMarkus Armbruster 
14014be74634SMarkus Armbruster BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
14024be74634SMarkus Armbruster                                       int error)
14034be74634SMarkus Armbruster {
1404373340b2SMax Reitz     BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1405373340b2SMax Reitz 
1406373340b2SMax Reitz     switch (on_err) {
1407373340b2SMax Reitz     case BLOCKDEV_ON_ERROR_ENOSPC:
1408373340b2SMax Reitz         return (error == ENOSPC) ?
1409373340b2SMax Reitz                BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1410373340b2SMax Reitz     case BLOCKDEV_ON_ERROR_STOP:
1411373340b2SMax Reitz         return BLOCK_ERROR_ACTION_STOP;
1412373340b2SMax Reitz     case BLOCKDEV_ON_ERROR_REPORT:
1413373340b2SMax Reitz         return BLOCK_ERROR_ACTION_REPORT;
1414373340b2SMax Reitz     case BLOCKDEV_ON_ERROR_IGNORE:
1415373340b2SMax Reitz         return BLOCK_ERROR_ACTION_IGNORE;
14168c398252SKevin Wolf     case BLOCKDEV_ON_ERROR_AUTO:
1417373340b2SMax Reitz     default:
1418373340b2SMax Reitz         abort();
1419373340b2SMax Reitz     }
14204be74634SMarkus Armbruster }
14214be74634SMarkus Armbruster 
1422373340b2SMax Reitz static void send_qmp_error_event(BlockBackend *blk,
1423373340b2SMax Reitz                                  BlockErrorAction action,
1424373340b2SMax Reitz                                  bool is_read, int error)
1425373340b2SMax Reitz {
1426373340b2SMax Reitz     IoOperationType optype;
1427373340b2SMax Reitz 
1428373340b2SMax Reitz     optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
14292bf7e10fSKevin Wolf     qapi_event_send_block_io_error(blk_name(blk),
14302bf7e10fSKevin Wolf                                    bdrv_get_node_name(blk_bs(blk)), optype,
14312bf7e10fSKevin Wolf                                    action, blk_iostatus_is_enabled(blk),
1432373340b2SMax Reitz                                    error == ENOSPC, strerror(error),
1433373340b2SMax Reitz                                    &error_abort);
1434373340b2SMax Reitz }
1435373340b2SMax Reitz 
1436373340b2SMax Reitz /* This is done by device models because, while the block layer knows
1437373340b2SMax Reitz  * about the error, it does not know whether an operation comes from
1438373340b2SMax Reitz  * the device or the block layer (from a job, for example).
1439373340b2SMax Reitz  */
14404be74634SMarkus Armbruster void blk_error_action(BlockBackend *blk, BlockErrorAction action,
14414be74634SMarkus Armbruster                       bool is_read, int error)
14424be74634SMarkus Armbruster {
1443373340b2SMax Reitz     assert(error >= 0);
1444373340b2SMax Reitz 
1445373340b2SMax Reitz     if (action == BLOCK_ERROR_ACTION_STOP) {
1446373340b2SMax Reitz         /* First set the iostatus, so that "info block" returns an iostatus
1447373340b2SMax Reitz          * that matches the events raised so far (an additional error iostatus
1448373340b2SMax Reitz          * is fine, but not a lost one).
1449373340b2SMax Reitz          */
1450373340b2SMax Reitz         blk_iostatus_set_err(blk, error);
1451373340b2SMax Reitz 
1452373340b2SMax Reitz         /* Then raise the request to stop the VM and the event.
1453373340b2SMax Reitz          * qemu_system_vmstop_request_prepare has two effects.  First,
1454373340b2SMax Reitz          * it ensures that the STOP event always comes after the
1455373340b2SMax Reitz          * BLOCK_IO_ERROR event.  Second, it ensures that even if management
1456373340b2SMax Reitz          * can observe the STOP event and do a "cont" before the STOP
1457373340b2SMax Reitz          * event is issued, the VM will not stop.  In this case, vm_start()
1458373340b2SMax Reitz          * also ensures that the STOP/RESUME pair of events is emitted.
1459373340b2SMax Reitz          */
1460373340b2SMax Reitz         qemu_system_vmstop_request_prepare();
1461373340b2SMax Reitz         send_qmp_error_event(blk, action, is_read, error);
1462373340b2SMax Reitz         qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1463373340b2SMax Reitz     } else {
1464373340b2SMax Reitz         send_qmp_error_event(blk, action, is_read, error);
1465373340b2SMax Reitz     }
14664be74634SMarkus Armbruster }
14674be74634SMarkus Armbruster 
14684be74634SMarkus Armbruster int blk_is_read_only(BlockBackend *blk)
14694be74634SMarkus Armbruster {
1470f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1471f21d96d0SKevin Wolf 
1472f21d96d0SKevin Wolf     if (bs) {
1473f21d96d0SKevin Wolf         return bdrv_is_read_only(bs);
1474061959e8SMax Reitz     } else {
1475061959e8SMax Reitz         return blk->root_state.read_only;
1476061959e8SMax Reitz     }
14774be74634SMarkus Armbruster }
14784be74634SMarkus Armbruster 
14794be74634SMarkus Armbruster int blk_is_sg(BlockBackend *blk)
14804be74634SMarkus Armbruster {
1481f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1482f21d96d0SKevin Wolf 
1483f21d96d0SKevin Wolf     if (!bs) {
1484a46fc9c9SMax Reitz         return 0;
1485a46fc9c9SMax Reitz     }
1486a46fc9c9SMax Reitz 
1487f21d96d0SKevin Wolf     return bdrv_is_sg(bs);
14884be74634SMarkus Armbruster }
14894be74634SMarkus Armbruster 
14904be74634SMarkus Armbruster int blk_enable_write_cache(BlockBackend *blk)
14914be74634SMarkus Armbruster {
1492bfd18d1eSKevin Wolf     return blk->enable_write_cache;
14934be74634SMarkus Armbruster }
14944be74634SMarkus Armbruster 
14954be74634SMarkus Armbruster void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
14964be74634SMarkus Armbruster {
1497bfd18d1eSKevin Wolf     blk->enable_write_cache = wce;
14984be74634SMarkus Armbruster }
14994be74634SMarkus Armbruster 
15002bb0dce7SMax Reitz void blk_invalidate_cache(BlockBackend *blk, Error **errp)
15012bb0dce7SMax Reitz {
1502f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1503f21d96d0SKevin Wolf 
1504f21d96d0SKevin Wolf     if (!bs) {
1505c09ba36cSMax Reitz         error_setg(errp, "Device '%s' has no medium", blk->name);
1506c09ba36cSMax Reitz         return;
1507c09ba36cSMax Reitz     }
1508c09ba36cSMax Reitz 
1509f21d96d0SKevin Wolf     bdrv_invalidate_cache(bs, errp);
15102bb0dce7SMax Reitz }
15112bb0dce7SMax Reitz 
1512e031f750SMax Reitz bool blk_is_inserted(BlockBackend *blk)
15134be74634SMarkus Armbruster {
1514f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1515f21d96d0SKevin Wolf 
1516f21d96d0SKevin Wolf     return bs && bdrv_is_inserted(bs);
1517db0284f8SMax Reitz }
1518db0284f8SMax Reitz 
1519db0284f8SMax Reitz bool blk_is_available(BlockBackend *blk)
1520db0284f8SMax Reitz {
1521db0284f8SMax Reitz     return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
15224be74634SMarkus Armbruster }
15234be74634SMarkus Armbruster 
15244be74634SMarkus Armbruster void blk_lock_medium(BlockBackend *blk, bool locked)
15254be74634SMarkus Armbruster {
1526f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1527f21d96d0SKevin Wolf 
1528f21d96d0SKevin Wolf     if (bs) {
1529f21d96d0SKevin Wolf         bdrv_lock_medium(bs, locked);
15304be74634SMarkus Armbruster     }
1531a46fc9c9SMax Reitz }
15324be74634SMarkus Armbruster 
15334be74634SMarkus Armbruster void blk_eject(BlockBackend *blk, bool eject_flag)
15344be74634SMarkus Armbruster {
1535f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
15362d76e724SKevin Wolf     char *id;
15372d76e724SKevin Wolf 
15382d76e724SKevin Wolf     /* blk_eject is only called by qdevified devices */
15392d76e724SKevin Wolf     assert(!blk->legacy_dev);
1540f21d96d0SKevin Wolf 
1541f21d96d0SKevin Wolf     if (bs) {
1542f21d96d0SKevin Wolf         bdrv_eject(bs, eject_flag);
1543c47ee043SJohn Snow     }
15442d76e724SKevin Wolf 
1545c47ee043SJohn Snow     /* Whether or not we ejected on the backend,
1546c47ee043SJohn Snow      * the frontend experienced a tray event. */
15472d76e724SKevin Wolf     id = blk_get_attached_dev_id(blk);
15482d76e724SKevin Wolf     qapi_event_send_device_tray_moved(blk_name(blk), id,
15492d76e724SKevin Wolf                                       eject_flag, &error_abort);
15502d76e724SKevin Wolf     g_free(id);
1551a46fc9c9SMax Reitz }
15524be74634SMarkus Armbruster 
15534be74634SMarkus Armbruster int blk_get_flags(BlockBackend *blk)
15544be74634SMarkus Armbruster {
1555f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1556f21d96d0SKevin Wolf 
1557f21d96d0SKevin Wolf     if (bs) {
1558f21d96d0SKevin Wolf         return bdrv_get_flags(bs);
1559061959e8SMax Reitz     } else {
1560061959e8SMax Reitz         return blk->root_state.open_flags;
1561061959e8SMax Reitz     }
15624be74634SMarkus Armbruster }
15634be74634SMarkus Armbruster 
15645def6b80SEric Blake /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
15655def6b80SEric Blake uint32_t blk_get_max_transfer(BlockBackend *blk)
1566454057b7SPeter Lieven {
1567f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
15685def6b80SEric Blake     uint32_t max = 0;
1569f21d96d0SKevin Wolf 
1570f21d96d0SKevin Wolf     if (bs) {
15715def6b80SEric Blake         max = bs->bl.max_transfer;
1572a46fc9c9SMax Reitz     }
15735def6b80SEric Blake     return MIN_NON_ZERO(max, INT_MAX);
1574454057b7SPeter Lieven }
1575454057b7SPeter Lieven 
1576648296e0SStefan Hajnoczi int blk_get_max_iov(BlockBackend *blk)
1577648296e0SStefan Hajnoczi {
1578f21d96d0SKevin Wolf     return blk->root->bs->bl.max_iov;
1579648296e0SStefan Hajnoczi }
1580648296e0SStefan Hajnoczi 
15814be74634SMarkus Armbruster void blk_set_guest_block_size(BlockBackend *blk, int align)
15824be74634SMarkus Armbruster {
158368e9ec01SMax Reitz     blk->guest_block_size = align;
15844be74634SMarkus Armbruster }
15854be74634SMarkus Armbruster 
1586f1c17521SPaolo Bonzini void *blk_try_blockalign(BlockBackend *blk, size_t size)
1587f1c17521SPaolo Bonzini {
1588f21d96d0SKevin Wolf     return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
1589f1c17521SPaolo Bonzini }
1590f1c17521SPaolo Bonzini 
15914be74634SMarkus Armbruster void *blk_blockalign(BlockBackend *blk, size_t size)
15924be74634SMarkus Armbruster {
1593f21d96d0SKevin Wolf     return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
15944be74634SMarkus Armbruster }
15954be74634SMarkus Armbruster 
15964be74634SMarkus Armbruster bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
15974be74634SMarkus Armbruster {
1598f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1599f21d96d0SKevin Wolf 
1600f21d96d0SKevin Wolf     if (!bs) {
1601a46fc9c9SMax Reitz         return false;
1602a46fc9c9SMax Reitz     }
1603a46fc9c9SMax Reitz 
1604f21d96d0SKevin Wolf     return bdrv_op_is_blocked(bs, op, errp);
16054be74634SMarkus Armbruster }
16064be74634SMarkus Armbruster 
16074be74634SMarkus Armbruster void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
16084be74634SMarkus Armbruster {
1609f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1610f21d96d0SKevin Wolf 
1611f21d96d0SKevin Wolf     if (bs) {
1612f21d96d0SKevin Wolf         bdrv_op_unblock(bs, op, reason);
16134be74634SMarkus Armbruster     }
1614a46fc9c9SMax Reitz }
16154be74634SMarkus Armbruster 
16164be74634SMarkus Armbruster void blk_op_block_all(BlockBackend *blk, Error *reason)
16174be74634SMarkus Armbruster {
1618f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1619f21d96d0SKevin Wolf 
1620f21d96d0SKevin Wolf     if (bs) {
1621f21d96d0SKevin Wolf         bdrv_op_block_all(bs, reason);
16224be74634SMarkus Armbruster     }
1623a46fc9c9SMax Reitz }
16244be74634SMarkus Armbruster 
16254be74634SMarkus Armbruster void blk_op_unblock_all(BlockBackend *blk, Error *reason)
16264be74634SMarkus Armbruster {
1627f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1628f21d96d0SKevin Wolf 
1629f21d96d0SKevin Wolf     if (bs) {
1630f21d96d0SKevin Wolf         bdrv_op_unblock_all(bs, reason);
16314be74634SMarkus Armbruster     }
1632a46fc9c9SMax Reitz }
16334be74634SMarkus Armbruster 
16344be74634SMarkus Armbruster AioContext *blk_get_aio_context(BlockBackend *blk)
16354be74634SMarkus Armbruster {
1636f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1637f21d96d0SKevin Wolf 
1638f21d96d0SKevin Wolf     if (bs) {
1639f21d96d0SKevin Wolf         return bdrv_get_aio_context(bs);
16404981bdecSMax Reitz     } else {
16414981bdecSMax Reitz         return qemu_get_aio_context();
16424981bdecSMax Reitz     }
16434981bdecSMax Reitz }
16444981bdecSMax Reitz 
16454981bdecSMax Reitz static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
16464981bdecSMax Reitz {
16474981bdecSMax Reitz     BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
16484981bdecSMax Reitz     return blk_get_aio_context(blk_acb->blk);
16494be74634SMarkus Armbruster }
16504be74634SMarkus Armbruster 
16514be74634SMarkus Armbruster void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
16524be74634SMarkus Armbruster {
1653f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1654f21d96d0SKevin Wolf 
1655f21d96d0SKevin Wolf     if (bs) {
16567ca7f0f6SKevin Wolf         if (blk->public.throttle_state) {
16577ca7f0f6SKevin Wolf             throttle_timers_detach_aio_context(&blk->public.throttle_timers);
16587ca7f0f6SKevin Wolf         }
1659f21d96d0SKevin Wolf         bdrv_set_aio_context(bs, new_context);
16607ca7f0f6SKevin Wolf         if (blk->public.throttle_state) {
16617ca7f0f6SKevin Wolf             throttle_timers_attach_aio_context(&blk->public.throttle_timers,
16627ca7f0f6SKevin Wolf                                                new_context);
16637ca7f0f6SKevin Wolf         }
16644be74634SMarkus Armbruster     }
1665a46fc9c9SMax Reitz }
16664be74634SMarkus Armbruster 
16672019ba0aSMax Reitz void blk_add_aio_context_notifier(BlockBackend *blk,
16682019ba0aSMax Reitz         void (*attached_aio_context)(AioContext *new_context, void *opaque),
16692019ba0aSMax Reitz         void (*detach_aio_context)(void *opaque), void *opaque)
16702019ba0aSMax Reitz {
1671f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1672f21d96d0SKevin Wolf 
1673f21d96d0SKevin Wolf     if (bs) {
1674f21d96d0SKevin Wolf         bdrv_add_aio_context_notifier(bs, attached_aio_context,
16752019ba0aSMax Reitz                                       detach_aio_context, opaque);
16762019ba0aSMax Reitz     }
1677a46fc9c9SMax Reitz }
16782019ba0aSMax Reitz 
16792019ba0aSMax Reitz void blk_remove_aio_context_notifier(BlockBackend *blk,
16802019ba0aSMax Reitz                                      void (*attached_aio_context)(AioContext *,
16812019ba0aSMax Reitz                                                                   void *),
16822019ba0aSMax Reitz                                      void (*detach_aio_context)(void *),
16832019ba0aSMax Reitz                                      void *opaque)
16842019ba0aSMax Reitz {
1685f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1686f21d96d0SKevin Wolf 
1687f21d96d0SKevin Wolf     if (bs) {
1688f21d96d0SKevin Wolf         bdrv_remove_aio_context_notifier(bs, attached_aio_context,
16892019ba0aSMax Reitz                                          detach_aio_context, opaque);
16902019ba0aSMax Reitz     }
1691a46fc9c9SMax Reitz }
16922019ba0aSMax Reitz 
16933301f6c6SMax Reitz void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
16943301f6c6SMax Reitz {
16953301f6c6SMax Reitz     notifier_list_add(&blk->remove_bs_notifiers, notify);
16963301f6c6SMax Reitz }
16973301f6c6SMax Reitz 
16983301f6c6SMax Reitz void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
16993301f6c6SMax Reitz {
17003301f6c6SMax Reitz     notifier_list_add(&blk->insert_bs_notifiers, notify);
17013301f6c6SMax Reitz }
17023301f6c6SMax Reitz 
17034be74634SMarkus Armbruster void blk_io_plug(BlockBackend *blk)
17044be74634SMarkus Armbruster {
1705f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1706f21d96d0SKevin Wolf 
1707f21d96d0SKevin Wolf     if (bs) {
1708f21d96d0SKevin Wolf         bdrv_io_plug(bs);
17094be74634SMarkus Armbruster     }
1710a46fc9c9SMax Reitz }
17114be74634SMarkus Armbruster 
17124be74634SMarkus Armbruster void blk_io_unplug(BlockBackend *blk)
17134be74634SMarkus Armbruster {
1714f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1715f21d96d0SKevin Wolf 
1716f21d96d0SKevin Wolf     if (bs) {
1717f21d96d0SKevin Wolf         bdrv_io_unplug(bs);
17184be74634SMarkus Armbruster     }
1719a46fc9c9SMax Reitz }
17204be74634SMarkus Armbruster 
17214be74634SMarkus Armbruster BlockAcctStats *blk_get_stats(BlockBackend *blk)
17224be74634SMarkus Armbruster {
17237f0e9da6SMax Reitz     return &blk->stats;
17244be74634SMarkus Armbruster }
17254be74634SMarkus Armbruster 
17264be74634SMarkus Armbruster void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
17274be74634SMarkus Armbruster                   BlockCompletionFunc *cb, void *opaque)
17284be74634SMarkus Armbruster {
17294be74634SMarkus Armbruster     return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
17304be74634SMarkus Armbruster }
17311ef01253SMax Reitz 
1732d004bd52SEric Blake int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1733983a1600SEric Blake                                       int count, BdrvRequestFlags flags)
17341ef01253SMax Reitz {
1735983a1600SEric Blake     return blk_co_pwritev(blk, offset, count, NULL,
173616aaf975SKevin Wolf                           flags | BDRV_REQ_ZERO_WRITE);
17371ef01253SMax Reitz }
17381ef01253SMax Reitz 
1739fe5c1355SPavel Butsykin int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
1740fe5c1355SPavel Butsykin                           int count)
17411ef01253SMax Reitz {
174235fadca8SPavel Butsykin     return blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
174335fadca8SPavel Butsykin                    BDRV_REQ_WRITE_COMPRESSED);
17441ef01253SMax Reitz }
17451ef01253SMax Reitz 
17461ef01253SMax Reitz int blk_truncate(BlockBackend *blk, int64_t offset)
17471ef01253SMax Reitz {
1748c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1749c09ba36cSMax Reitz         return -ENOMEDIUM;
1750c09ba36cSMax Reitz     }
1751c09ba36cSMax Reitz 
175252cdbc58SKevin Wolf     return bdrv_truncate(blk->root, offset);
17531ef01253SMax Reitz }
17541ef01253SMax Reitz 
17558c2e3dd5SKevin Wolf static void blk_pdiscard_entry(void *opaque)
17561ef01253SMax Reitz {
17578c2e3dd5SKevin Wolf     BlkRwCo *rwco = opaque;
17588c2e3dd5SKevin Wolf     rwco->ret = blk_co_pdiscard(rwco->blk, rwco->offset, rwco->qiov->size);
1759e7f7d676SMax Reitz }
1760e7f7d676SMax Reitz 
17618c2e3dd5SKevin Wolf int blk_pdiscard(BlockBackend *blk, int64_t offset, int count)
17628c2e3dd5SKevin Wolf {
17638c2e3dd5SKevin Wolf     return blk_prw(blk, offset, NULL, count, blk_pdiscard_entry, 0);
17641ef01253SMax Reitz }
17651ef01253SMax Reitz 
17661ef01253SMax Reitz int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
17671ef01253SMax Reitz                      int64_t pos, int size)
17681ef01253SMax Reitz {
1769bfd18d1eSKevin Wolf     int ret;
1770bfd18d1eSKevin Wolf 
1771c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1772c09ba36cSMax Reitz         return -ENOMEDIUM;
1773c09ba36cSMax Reitz     }
1774c09ba36cSMax Reitz 
1775bfd18d1eSKevin Wolf     ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
1776bfd18d1eSKevin Wolf     if (ret < 0) {
1777bfd18d1eSKevin Wolf         return ret;
1778bfd18d1eSKevin Wolf     }
1779bfd18d1eSKevin Wolf 
1780bfd18d1eSKevin Wolf     if (ret == size && !blk->enable_write_cache) {
1781bfd18d1eSKevin Wolf         ret = bdrv_flush(blk_bs(blk));
1782bfd18d1eSKevin Wolf     }
1783bfd18d1eSKevin Wolf 
1784bfd18d1eSKevin Wolf     return ret < 0 ? ret : size;
17851ef01253SMax Reitz }
17861ef01253SMax Reitz 
17871ef01253SMax Reitz int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
17881ef01253SMax Reitz {
1789c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1790c09ba36cSMax Reitz         return -ENOMEDIUM;
1791c09ba36cSMax Reitz     }
1792c09ba36cSMax Reitz 
1793f21d96d0SKevin Wolf     return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
17941ef01253SMax Reitz }
1795f0272c4dSEkaterina Tumanova 
1796f0272c4dSEkaterina Tumanova int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1797f0272c4dSEkaterina Tumanova {
1798c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1799c09ba36cSMax Reitz         return -ENOMEDIUM;
1800c09ba36cSMax Reitz     }
1801c09ba36cSMax Reitz 
1802f21d96d0SKevin Wolf     return bdrv_probe_blocksizes(blk_bs(blk), bsz);
1803f0272c4dSEkaterina Tumanova }
1804f0272c4dSEkaterina Tumanova 
1805f0272c4dSEkaterina Tumanova int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1806f0272c4dSEkaterina Tumanova {
1807c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1808c09ba36cSMax Reitz         return -ENOMEDIUM;
1809c09ba36cSMax Reitz     }
1810c09ba36cSMax Reitz 
1811f21d96d0SKevin Wolf     return bdrv_probe_geometry(blk_bs(blk), geo);
1812f0272c4dSEkaterina Tumanova }
1813281d22d8SMax Reitz 
1814281d22d8SMax Reitz /*
1815281d22d8SMax Reitz  * Updates the BlockBackendRootState object with data from the currently
1816281d22d8SMax Reitz  * attached BlockDriverState.
1817281d22d8SMax Reitz  */
1818281d22d8SMax Reitz void blk_update_root_state(BlockBackend *blk)
1819281d22d8SMax Reitz {
1820f21d96d0SKevin Wolf     assert(blk->root);
1821281d22d8SMax Reitz 
1822f21d96d0SKevin Wolf     blk->root_state.open_flags    = blk->root->bs->open_flags;
1823f21d96d0SKevin Wolf     blk->root_state.read_only     = blk->root->bs->read_only;
1824f21d96d0SKevin Wolf     blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
1825281d22d8SMax Reitz }
1826281d22d8SMax Reitz 
182738cb18f5SMax Reitz /*
1828b85114f8SKevin Wolf  * Returns the detect-zeroes setting to be used for bdrv_open() of a
1829b85114f8SKevin Wolf  * BlockDriverState which is supposed to inherit the root state.
183038cb18f5SMax Reitz  */
1831b85114f8SKevin Wolf bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)
183238cb18f5SMax Reitz {
1833b85114f8SKevin Wolf     return blk->root_state.detect_zeroes;
183438cb18f5SMax Reitz }
183538cb18f5SMax Reitz 
183638cb18f5SMax Reitz /*
183738cb18f5SMax Reitz  * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
183838cb18f5SMax Reitz  * supposed to inherit the root state.
183938cb18f5SMax Reitz  */
184038cb18f5SMax Reitz int blk_get_open_flags_from_root_state(BlockBackend *blk)
184138cb18f5SMax Reitz {
184238cb18f5SMax Reitz     int bs_flags;
184338cb18f5SMax Reitz 
184438cb18f5SMax Reitz     bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
184538cb18f5SMax Reitz     bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
184638cb18f5SMax Reitz 
184738cb18f5SMax Reitz     return bs_flags;
184838cb18f5SMax Reitz }
184938cb18f5SMax Reitz 
1850281d22d8SMax Reitz BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1851281d22d8SMax Reitz {
1852281d22d8SMax Reitz     return &blk->root_state;
1853281d22d8SMax Reitz }
18541393f212SMax Reitz 
18551393f212SMax Reitz int blk_commit_all(void)
18561393f212SMax Reitz {
1857fe1a9cbcSMax Reitz     BlockBackend *blk = NULL;
1858fe1a9cbcSMax Reitz 
1859fe1a9cbcSMax Reitz     while ((blk = blk_all_next(blk)) != NULL) {
1860fe1a9cbcSMax Reitz         AioContext *aio_context = blk_get_aio_context(blk);
1861fe1a9cbcSMax Reitz 
1862fe1a9cbcSMax Reitz         aio_context_acquire(aio_context);
1863f21d96d0SKevin Wolf         if (blk_is_inserted(blk) && blk->root->bs->backing) {
1864f21d96d0SKevin Wolf             int ret = bdrv_commit(blk->root->bs);
1865fe1a9cbcSMax Reitz             if (ret < 0) {
1866fe1a9cbcSMax Reitz                 aio_context_release(aio_context);
1867fe1a9cbcSMax Reitz                 return ret;
1868fe1a9cbcSMax Reitz             }
1869fe1a9cbcSMax Reitz         }
1870fe1a9cbcSMax Reitz         aio_context_release(aio_context);
1871fe1a9cbcSMax Reitz     }
1872fe1a9cbcSMax Reitz     return 0;
1873fe1a9cbcSMax Reitz }
1874fe1a9cbcSMax Reitz 
187597148076SKevin Wolf 
187697148076SKevin Wolf /* throttling disk I/O limits */
187797148076SKevin Wolf void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
187897148076SKevin Wolf {
187997148076SKevin Wolf     throttle_group_config(blk, cfg);
188097148076SKevin Wolf }
188197148076SKevin Wolf 
188297148076SKevin Wolf void blk_io_limits_disable(BlockBackend *blk)
188397148076SKevin Wolf {
188497148076SKevin Wolf     assert(blk->public.throttle_state);
1885c2066af0SKevin Wolf     bdrv_drained_begin(blk_bs(blk));
188697148076SKevin Wolf     throttle_group_unregister_blk(blk);
1887c2066af0SKevin Wolf     bdrv_drained_end(blk_bs(blk));
188897148076SKevin Wolf }
188997148076SKevin Wolf 
189097148076SKevin Wolf /* should be called before blk_set_io_limits if a limit is set */
189197148076SKevin Wolf void blk_io_limits_enable(BlockBackend *blk, const char *group)
189297148076SKevin Wolf {
189397148076SKevin Wolf     assert(!blk->public.throttle_state);
189497148076SKevin Wolf     throttle_group_register_blk(blk, group);
189597148076SKevin Wolf }
189697148076SKevin Wolf 
189797148076SKevin Wolf void blk_io_limits_update_group(BlockBackend *blk, const char *group)
189897148076SKevin Wolf {
189997148076SKevin Wolf     /* this BB is not part of any group */
190097148076SKevin Wolf     if (!blk->public.throttle_state) {
190197148076SKevin Wolf         return;
190297148076SKevin Wolf     }
190397148076SKevin Wolf 
190497148076SKevin Wolf     /* this BB is a part of the same group than the one we want */
190597148076SKevin Wolf     if (!g_strcmp0(throttle_group_get_name(blk), group)) {
190697148076SKevin Wolf         return;
190797148076SKevin Wolf     }
190897148076SKevin Wolf 
190997148076SKevin Wolf     /* need to change the group this bs belong to */
191097148076SKevin Wolf     blk_io_limits_disable(blk);
191197148076SKevin Wolf     blk_io_limits_enable(blk, group);
191297148076SKevin Wolf }
1913c2066af0SKevin Wolf 
1914c2066af0SKevin Wolf static void blk_root_drained_begin(BdrvChild *child)
1915c2066af0SKevin Wolf {
1916c2066af0SKevin Wolf     BlockBackend *blk = child->opaque;
1917c2066af0SKevin Wolf 
1918f4d9cc88SJohn Snow     if (++blk->quiesce_counter == 1) {
1919f4d9cc88SJohn Snow         if (blk->dev_ops && blk->dev_ops->drained_begin) {
1920f4d9cc88SJohn Snow             blk->dev_ops->drained_begin(blk->dev_opaque);
1921f4d9cc88SJohn Snow         }
1922f4d9cc88SJohn Snow     }
1923f4d9cc88SJohn Snow 
192436fe1331SKevin Wolf     /* Note that blk->root may not be accessible here yet if we are just
192536fe1331SKevin Wolf      * attaching to a BlockDriverState that is drained. Use child instead. */
192636fe1331SKevin Wolf 
1927c2066af0SKevin Wolf     if (blk->public.io_limits_disabled++ == 0) {
1928c2066af0SKevin Wolf         throttle_group_restart_blk(blk);
1929c2066af0SKevin Wolf     }
1930c2066af0SKevin Wolf }
1931c2066af0SKevin Wolf 
1932c2066af0SKevin Wolf static void blk_root_drained_end(BdrvChild *child)
1933c2066af0SKevin Wolf {
1934c2066af0SKevin Wolf     BlockBackend *blk = child->opaque;
1935f4d9cc88SJohn Snow     assert(blk->quiesce_counter);
1936c2066af0SKevin Wolf 
1937c2066af0SKevin Wolf     assert(blk->public.io_limits_disabled);
1938c2066af0SKevin Wolf     --blk->public.io_limits_disabled;
1939f4d9cc88SJohn Snow 
1940f4d9cc88SJohn Snow     if (--blk->quiesce_counter == 0) {
1941f4d9cc88SJohn Snow         if (blk->dev_ops && blk->dev_ops->drained_end) {
1942f4d9cc88SJohn Snow             blk->dev_ops->drained_end(blk->dev_opaque);
1943f4d9cc88SJohn Snow         }
1944f4d9cc88SJohn Snow     }
1945c2066af0SKevin Wolf }
1946