xref: /openbmc/qemu/block/block-backend.c (revision 2800637a)
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"
1813d4ff07SMarkus Armbruster #include "hw/qdev-core.h"
1918e46a03SMarkus Armbruster #include "sysemu/blockdev.h"
2054d31236SMarkus Armbruster #include "sysemu/runstate.h"
21e4ec5ad4SPavel Dovgalyuk #include "sysemu/replay.h"
22e688df6bSMarkus Armbruster #include "qapi/error.h"
239af23989SMarkus Armbruster #include "qapi/qapi-events-block.h"
24f348b6d1SVeronia Bahaa #include "qemu/id.h"
25db725815SMarkus Armbruster #include "qemu/main-loop.h"
26922a01a0SMarkus Armbruster #include "qemu/option.h"
271e98fefdSKevin Wolf #include "trace.h"
285f7772c4SFam Zheng #include "migration/misc.h"
29a7f53e26SMarkus Armbruster 
30a7f53e26SMarkus Armbruster /* Number of coroutines to reserve per attached device model */
31a7f53e26SMarkus Armbruster #define COROUTINE_POOL_RESERVATION 64
3226f54e9aSMarkus Armbruster 
331bf1cbc9SKevin Wolf #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
341bf1cbc9SKevin Wolf 
354981bdecSMax Reitz static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
364981bdecSMax Reitz 
37d03654eaSStefan Hajnoczi typedef struct BlockBackendAioNotifier {
38d03654eaSStefan Hajnoczi     void (*attached_aio_context)(AioContext *new_context, void *opaque);
39d03654eaSStefan Hajnoczi     void (*detach_aio_context)(void *opaque);
40d03654eaSStefan Hajnoczi     void *opaque;
41d03654eaSStefan Hajnoczi     QLIST_ENTRY(BlockBackendAioNotifier) list;
42d03654eaSStefan Hajnoczi } BlockBackendAioNotifier;
43d03654eaSStefan Hajnoczi 
4426f54e9aSMarkus Armbruster struct BlockBackend {
4526f54e9aSMarkus Armbruster     char *name;
4626f54e9aSMarkus Armbruster     int refcnt;
47f21d96d0SKevin Wolf     BdrvChild *root;
48d861ab3aSKevin Wolf     AioContext *ctx;
4926f8b3a8SMarkus Armbruster     DriveInfo *legacy_dinfo;    /* null unless created by drive_new() */
502cf22d6aSMax Reitz     QTAILQ_ENTRY(BlockBackend) link;         /* for block_backends */
519492b0b9SMax Reitz     QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
52f2cd875dSKevin Wolf     BlockBackendPublic public;
53a7f53e26SMarkus Armbruster 
54d09ea2d2SThomas Huth     DeviceState *dev;           /* attached device model, if any */
55a7f53e26SMarkus Armbruster     const BlockDevOps *dev_ops;
56a7f53e26SMarkus Armbruster     void *dev_opaque;
5768e9ec01SMax Reitz 
5868e9ec01SMax Reitz     /* the block size for which the guest device expects atomicity */
5968e9ec01SMax Reitz     int guest_block_size;
607f0e9da6SMax Reitz 
61281d22d8SMax Reitz     /* If the BDS tree is removed, some of its options are stored here (which
62281d22d8SMax Reitz      * can be used to restore those options in the new BDS on insert) */
63281d22d8SMax Reitz     BlockBackendRootState root_state;
64281d22d8SMax Reitz 
65bfd18d1eSKevin Wolf     bool enable_write_cache;
66bfd18d1eSKevin Wolf 
677f0e9da6SMax Reitz     /* I/O stats (display with "info blockstats"). */
687f0e9da6SMax Reitz     BlockAcctStats stats;
69373340b2SMax Reitz 
70373340b2SMax Reitz     BlockdevOnError on_read_error, on_write_error;
71373340b2SMax Reitz     bool iostatus_enabled;
72373340b2SMax Reitz     BlockDeviceIoStatus iostatus;
733301f6c6SMax Reitz 
74981776b3SKevin Wolf     uint64_t perm;
75981776b3SKevin Wolf     uint64_t shared_perm;
76d35ff5e6SKevin Wolf     bool disable_perm;
77981776b3SKevin Wolf 
78980b0f94SKevin Wolf     bool allow_aio_context_change;
79c10c9d96SKevin Wolf     bool allow_write_beyond_eof;
80c10c9d96SKevin Wolf 
813301f6c6SMax Reitz     NotifierList remove_bs_notifiers, insert_bs_notifiers;
82d03654eaSStefan Hajnoczi     QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers;
83f4d9cc88SJohn Snow 
84f4d9cc88SJohn Snow     int quiesce_counter;
85cf312932SKevin Wolf     CoQueue queued_requests;
86cf312932SKevin Wolf     bool disable_request_queuing;
87cf312932SKevin Wolf 
885f7772c4SFam Zheng     VMChangeStateEntry *vmsh;
89ca2e2144SFam Zheng     bool force_allow_inactivate;
9033f2a757SStefan Hajnoczi 
9133f2a757SStefan Hajnoczi     /* Number of in-flight aio requests.  BlockDriverState also counts
9233f2a757SStefan Hajnoczi      * in-flight requests but aio requests can exist even when blk->root is
9333f2a757SStefan Hajnoczi      * NULL, so we cannot rely on its counter for that case.
9433f2a757SStefan Hajnoczi      * Accessed with atomic ops.
9533f2a757SStefan Hajnoczi      */
9633f2a757SStefan Hajnoczi     unsigned int in_flight;
9726f54e9aSMarkus Armbruster };
9826f54e9aSMarkus Armbruster 
99e7f7d676SMax Reitz typedef struct BlockBackendAIOCB {
100e7f7d676SMax Reitz     BlockAIOCB common;
1014981bdecSMax Reitz     BlockBackend *blk;
102e7f7d676SMax Reitz     int ret;
103e7f7d676SMax Reitz } BlockBackendAIOCB;
104e7f7d676SMax Reitz 
105e7f7d676SMax Reitz static const AIOCBInfo block_backend_aiocb_info = {
1064981bdecSMax Reitz     .get_aio_context = blk_aiocb_get_aio_context,
107e7f7d676SMax Reitz     .aiocb_size = sizeof(BlockBackendAIOCB),
108e7f7d676SMax Reitz };
109e7f7d676SMax Reitz 
1108fb3c76cSMarkus Armbruster static void drive_info_del(DriveInfo *dinfo);
1117c8eece4SKevin Wolf static BlockBackend *bdrv_first_blk(BlockDriverState *bs);
1128fb3c76cSMarkus Armbruster 
1132cf22d6aSMax Reitz /* All BlockBackends */
1142cf22d6aSMax Reitz static QTAILQ_HEAD(, BlockBackend) block_backends =
1152cf22d6aSMax Reitz     QTAILQ_HEAD_INITIALIZER(block_backends);
1162cf22d6aSMax Reitz 
1179492b0b9SMax Reitz /* All BlockBackends referenced by the monitor and which are iterated through by
1189492b0b9SMax Reitz  * blk_next() */
1199492b0b9SMax Reitz static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
1209492b0b9SMax Reitz     QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
12126f54e9aSMarkus Armbruster 
1223cdc69d3SMax Reitz static void blk_root_inherit_options(BdrvChildRole role, bool parent_is_format,
123272c02eaSMax Reitz                                      int *child_flags, QDict *child_options,
124f21d96d0SKevin Wolf                                      int parent_flags, QDict *parent_options)
125f21d96d0SKevin Wolf {
126f21d96d0SKevin Wolf     /* We're not supposed to call this function for root nodes */
127f21d96d0SKevin Wolf     abort();
128f21d96d0SKevin Wolf }
129c2066af0SKevin Wolf static void blk_root_drained_begin(BdrvChild *child);
130fe5258a5SKevin Wolf static bool blk_root_drained_poll(BdrvChild *child);
131e037c09cSMax Reitz static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter);
132f21d96d0SKevin Wolf 
1335c8cab48SKevin Wolf static void blk_root_change_media(BdrvChild *child, bool load);
1345c8cab48SKevin Wolf static void blk_root_resize(BdrvChild *child);
1355c8cab48SKevin Wolf 
13638475269SKevin Wolf static bool blk_root_can_set_aio_ctx(BdrvChild *child, AioContext *ctx,
13738475269SKevin Wolf                                      GSList **ignore, Error **errp);
13838475269SKevin Wolf static void blk_root_set_aio_ctx(BdrvChild *child, AioContext *ctx,
13938475269SKevin Wolf                                  GSList **ignore);
14038475269SKevin Wolf 
141b5411555SKevin Wolf static char *blk_root_get_parent_desc(BdrvChild *child)
142b5411555SKevin Wolf {
143b5411555SKevin Wolf     BlockBackend *blk = child->opaque;
144fd240a18SVladimir Sementsov-Ogievskiy     g_autofree char *dev_id = NULL;
145b5411555SKevin Wolf 
146b5411555SKevin Wolf     if (blk->name) {
147fd240a18SVladimir Sementsov-Ogievskiy         return g_strdup_printf("block device '%s'", blk->name);
148b5411555SKevin Wolf     }
149b5411555SKevin Wolf 
150b5411555SKevin Wolf     dev_id = blk_get_attached_dev_id(blk);
151b5411555SKevin Wolf     if (*dev_id) {
152fd240a18SVladimir Sementsov-Ogievskiy         return g_strdup_printf("block device '%s'", dev_id);
153b5411555SKevin Wolf     } else {
154b5411555SKevin Wolf         /* TODO Callback into the BB owner for something more detailed */
155fd240a18SVladimir Sementsov-Ogievskiy         return g_strdup("an unnamed block device");
156b5411555SKevin Wolf     }
157b5411555SKevin Wolf }
158b5411555SKevin Wolf 
1594c265bf9SKevin Wolf static const char *blk_root_get_name(BdrvChild *child)
1604c265bf9SKevin Wolf {
1614c265bf9SKevin Wolf     return blk_name(child->opaque);
1624c265bf9SKevin Wolf }
1634c265bf9SKevin Wolf 
164538f0497SPhilippe Mathieu-Daudé static void blk_vm_state_changed(void *opaque, bool running, RunState state)
1655f7772c4SFam Zheng {
1665f7772c4SFam Zheng     Error *local_err = NULL;
1675f7772c4SFam Zheng     BlockBackend *blk = opaque;
1685f7772c4SFam Zheng 
1695f7772c4SFam Zheng     if (state == RUN_STATE_INMIGRATE) {
1705f7772c4SFam Zheng         return;
1715f7772c4SFam Zheng     }
1725f7772c4SFam Zheng 
1735f7772c4SFam Zheng     qemu_del_vm_change_state_handler(blk->vmsh);
1745f7772c4SFam Zheng     blk->vmsh = NULL;
1755f7772c4SFam Zheng     blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
1765f7772c4SFam Zheng     if (local_err) {
1775f7772c4SFam Zheng         error_report_err(local_err);
1785f7772c4SFam Zheng     }
1795f7772c4SFam Zheng }
1805f7772c4SFam Zheng 
1814417ab7aSKevin Wolf /*
1824417ab7aSKevin Wolf  * Notifies the user of the BlockBackend that migration has completed. qdev
1834417ab7aSKevin Wolf  * devices can tighten their permissions in response (specifically revoke
1844417ab7aSKevin Wolf  * shared write permissions that we needed for storage migration).
1854417ab7aSKevin Wolf  *
1864417ab7aSKevin Wolf  * If an error is returned, the VM cannot be allowed to be resumed.
1874417ab7aSKevin Wolf  */
1884417ab7aSKevin Wolf static void blk_root_activate(BdrvChild *child, Error **errp)
1894417ab7aSKevin Wolf {
1904417ab7aSKevin Wolf     BlockBackend *blk = child->opaque;
1914417ab7aSKevin Wolf     Error *local_err = NULL;
1924417ab7aSKevin Wolf 
1934417ab7aSKevin Wolf     if (!blk->disable_perm) {
1944417ab7aSKevin Wolf         return;
1954417ab7aSKevin Wolf     }
1964417ab7aSKevin Wolf 
1974417ab7aSKevin Wolf     blk->disable_perm = false;
1984417ab7aSKevin Wolf 
1995f7772c4SFam Zheng     blk_set_perm(blk, blk->perm, BLK_PERM_ALL, &local_err);
2005f7772c4SFam Zheng     if (local_err) {
2015f7772c4SFam Zheng         error_propagate(errp, local_err);
2025f7772c4SFam Zheng         blk->disable_perm = true;
2035f7772c4SFam Zheng         return;
2045f7772c4SFam Zheng     }
2055f7772c4SFam Zheng 
2065f7772c4SFam Zheng     if (runstate_check(RUN_STATE_INMIGRATE)) {
2075f7772c4SFam Zheng         /* Activation can happen when migration process is still active, for
2085f7772c4SFam Zheng          * example when nbd_server_add is called during non-shared storage
2095f7772c4SFam Zheng          * migration. Defer the shared_perm update to migration completion. */
2105f7772c4SFam Zheng         if (!blk->vmsh) {
2115f7772c4SFam Zheng             blk->vmsh = qemu_add_vm_change_state_handler(blk_vm_state_changed,
2125f7772c4SFam Zheng                                                          blk);
2135f7772c4SFam Zheng         }
2145f7772c4SFam Zheng         return;
2155f7772c4SFam Zheng     }
2165f7772c4SFam Zheng 
2174417ab7aSKevin Wolf     blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
2184417ab7aSKevin Wolf     if (local_err) {
2194417ab7aSKevin Wolf         error_propagate(errp, local_err);
2204417ab7aSKevin Wolf         blk->disable_perm = true;
2214417ab7aSKevin Wolf         return;
2224417ab7aSKevin Wolf     }
2234417ab7aSKevin Wolf }
2244417ab7aSKevin Wolf 
225ca2e2144SFam Zheng void blk_set_force_allow_inactivate(BlockBackend *blk)
226ca2e2144SFam Zheng {
227ca2e2144SFam Zheng     blk->force_allow_inactivate = true;
228ca2e2144SFam Zheng }
229ca2e2144SFam Zheng 
230c16de8f5SFam Zheng static bool blk_can_inactivate(BlockBackend *blk)
231c16de8f5SFam Zheng {
232ca2e2144SFam Zheng     /* If it is a guest device, inactivate is ok. */
233c16de8f5SFam Zheng     if (blk->dev || blk_name(blk)[0]) {
234c16de8f5SFam Zheng         return true;
235c16de8f5SFam Zheng     }
236c16de8f5SFam Zheng 
237ca2e2144SFam Zheng     /* Inactivating means no more writes to the image can be done,
238ca2e2144SFam Zheng      * even if those writes would be changes invisible to the
239ca2e2144SFam Zheng      * guest.  For block job BBs that satisfy this, we can just allow
240ca2e2144SFam Zheng      * it.  This is the case for mirror job source, which is required
241ca2e2144SFam Zheng      * by libvirt non-shared block migration. */
242ca2e2144SFam Zheng     if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) {
243ca2e2144SFam Zheng         return true;
244ca2e2144SFam Zheng     }
245ca2e2144SFam Zheng 
246ca2e2144SFam Zheng     return blk->force_allow_inactivate;
247c16de8f5SFam Zheng }
248c16de8f5SFam Zheng 
249cfa1a572SKevin Wolf static int blk_root_inactivate(BdrvChild *child)
250cfa1a572SKevin Wolf {
251cfa1a572SKevin Wolf     BlockBackend *blk = child->opaque;
252cfa1a572SKevin Wolf 
253cfa1a572SKevin Wolf     if (blk->disable_perm) {
254cfa1a572SKevin Wolf         return 0;
255cfa1a572SKevin Wolf     }
256cfa1a572SKevin Wolf 
257c16de8f5SFam Zheng     if (!blk_can_inactivate(blk)) {
258cfa1a572SKevin Wolf         return -EPERM;
259cfa1a572SKevin Wolf     }
260cfa1a572SKevin Wolf 
261cfa1a572SKevin Wolf     blk->disable_perm = true;
262cfa1a572SKevin Wolf     if (blk->root) {
263cfa1a572SKevin Wolf         bdrv_child_try_set_perm(blk->root, 0, BLK_PERM_ALL, &error_abort);
264cfa1a572SKevin Wolf     }
265cfa1a572SKevin Wolf 
266cfa1a572SKevin Wolf     return 0;
267cfa1a572SKevin Wolf }
268cfa1a572SKevin Wolf 
269d03654eaSStefan Hajnoczi static void blk_root_attach(BdrvChild *child)
270d03654eaSStefan Hajnoczi {
271d03654eaSStefan Hajnoczi     BlockBackend *blk = child->opaque;
272d03654eaSStefan Hajnoczi     BlockBackendAioNotifier *notifier;
273d03654eaSStefan Hajnoczi 
274d03654eaSStefan Hajnoczi     trace_blk_root_attach(child, blk, child->bs);
275d03654eaSStefan Hajnoczi 
276d03654eaSStefan Hajnoczi     QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
277d03654eaSStefan Hajnoczi         bdrv_add_aio_context_notifier(child->bs,
278d03654eaSStefan Hajnoczi                 notifier->attached_aio_context,
279d03654eaSStefan Hajnoczi                 notifier->detach_aio_context,
280d03654eaSStefan Hajnoczi                 notifier->opaque);
281d03654eaSStefan Hajnoczi     }
282d03654eaSStefan Hajnoczi }
283d03654eaSStefan Hajnoczi 
284d03654eaSStefan Hajnoczi static void blk_root_detach(BdrvChild *child)
285d03654eaSStefan Hajnoczi {
286d03654eaSStefan Hajnoczi     BlockBackend *blk = child->opaque;
287d03654eaSStefan Hajnoczi     BlockBackendAioNotifier *notifier;
288d03654eaSStefan Hajnoczi 
289d03654eaSStefan Hajnoczi     trace_blk_root_detach(child, blk, child->bs);
290d03654eaSStefan Hajnoczi 
291d03654eaSStefan Hajnoczi     QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
292d03654eaSStefan Hajnoczi         bdrv_remove_aio_context_notifier(child->bs,
293d03654eaSStefan Hajnoczi                 notifier->attached_aio_context,
294d03654eaSStefan Hajnoczi                 notifier->detach_aio_context,
295d03654eaSStefan Hajnoczi                 notifier->opaque);
296d03654eaSStefan Hajnoczi     }
297d03654eaSStefan Hajnoczi }
298d03654eaSStefan Hajnoczi 
2993ca1f322SVladimir Sementsov-Ogievskiy static AioContext *blk_root_get_parent_aio_context(BdrvChild *c)
3003ca1f322SVladimir Sementsov-Ogievskiy {
3013ca1f322SVladimir Sementsov-Ogievskiy     BlockBackend *blk = c->opaque;
3023ca1f322SVladimir Sementsov-Ogievskiy 
3033ca1f322SVladimir Sementsov-Ogievskiy     return blk_get_aio_context(blk);
3043ca1f322SVladimir Sementsov-Ogievskiy }
3053ca1f322SVladimir Sementsov-Ogievskiy 
306bd86fb99SMax Reitz static const BdrvChildClass child_root = {
307f21d96d0SKevin Wolf     .inherit_options    = blk_root_inherit_options,
308c2066af0SKevin Wolf 
3095c8cab48SKevin Wolf     .change_media       = blk_root_change_media,
3105c8cab48SKevin Wolf     .resize             = blk_root_resize,
3114c265bf9SKevin Wolf     .get_name           = blk_root_get_name,
312b5411555SKevin Wolf     .get_parent_desc    = blk_root_get_parent_desc,
3135c8cab48SKevin Wolf 
314c2066af0SKevin Wolf     .drained_begin      = blk_root_drained_begin,
315fe5258a5SKevin Wolf     .drained_poll       = blk_root_drained_poll,
316c2066af0SKevin Wolf     .drained_end        = blk_root_drained_end,
3174417ab7aSKevin Wolf 
3184417ab7aSKevin Wolf     .activate           = blk_root_activate,
319cfa1a572SKevin Wolf     .inactivate         = blk_root_inactivate,
320d03654eaSStefan Hajnoczi 
321d03654eaSStefan Hajnoczi     .attach             = blk_root_attach,
322d03654eaSStefan Hajnoczi     .detach             = blk_root_detach,
32338475269SKevin Wolf 
32438475269SKevin Wolf     .can_set_aio_ctx    = blk_root_can_set_aio_ctx,
32538475269SKevin Wolf     .set_aio_ctx        = blk_root_set_aio_ctx,
3263ca1f322SVladimir Sementsov-Ogievskiy 
3273ca1f322SVladimir Sementsov-Ogievskiy     .get_parent_aio_context = blk_root_get_parent_aio_context,
328f21d96d0SKevin Wolf };
329f21d96d0SKevin Wolf 
33026f54e9aSMarkus Armbruster /*
331efaa7c4eSMax Reitz  * Create a new BlockBackend with a reference count of one.
3326d0eb64dSKevin Wolf  *
3336d0eb64dSKevin Wolf  * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
3346d0eb64dSKevin Wolf  * to request for a block driver node that is attached to this BlockBackend.
3356d0eb64dSKevin Wolf  * @shared_perm is a bitmask which describes which permissions may be granted
3366d0eb64dSKevin Wolf  * to other users of the attached node.
3376d0eb64dSKevin Wolf  * Both sets of permissions can be changed later using blk_set_perm().
3386d0eb64dSKevin Wolf  *
33926f54e9aSMarkus Armbruster  * Return the new BlockBackend on success, null on failure.
34026f54e9aSMarkus Armbruster  */
341d861ab3aSKevin Wolf BlockBackend *blk_new(AioContext *ctx, uint64_t perm, uint64_t shared_perm)
34226f54e9aSMarkus Armbruster {
34326f54e9aSMarkus Armbruster     BlockBackend *blk;
34426f54e9aSMarkus Armbruster 
34526f54e9aSMarkus Armbruster     blk = g_new0(BlockBackend, 1);
34626f54e9aSMarkus Armbruster     blk->refcnt = 1;
347d861ab3aSKevin Wolf     blk->ctx = ctx;
3486d0eb64dSKevin Wolf     blk->perm = perm;
3496d0eb64dSKevin Wolf     blk->shared_perm = shared_perm;
3500c3169dfSKevin Wolf     blk_set_enable_write_cache(blk, true);
3510c3169dfSKevin Wolf 
352cb53460bSKevin Wolf     blk->on_read_error = BLOCKDEV_ON_ERROR_REPORT;
353cb53460bSKevin Wolf     blk->on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
354cb53460bSKevin Wolf 
3559caa6f3dSPaolo Bonzini     block_acct_init(&blk->stats);
35627ccdd52SKevin Wolf 
357cf312932SKevin Wolf     qemu_co_queue_init(&blk->queued_requests);
3583301f6c6SMax Reitz     notifier_list_init(&blk->remove_bs_notifiers);
3593301f6c6SMax Reitz     notifier_list_init(&blk->insert_bs_notifiers);
360d03654eaSStefan Hajnoczi     QLIST_INIT(&blk->aio_notifiers);
36127ccdd52SKevin Wolf 
3622cf22d6aSMax Reitz     QTAILQ_INSERT_TAIL(&block_backends, blk, link);
36326f54e9aSMarkus Armbruster     return blk;
36426f54e9aSMarkus Armbruster }
36526f54e9aSMarkus Armbruster 
3667e7d56d9SMarkus Armbruster /*
367a3aeeab5SEric Blake  * Create a new BlockBackend connected to an existing BlockDriverState.
368a3aeeab5SEric Blake  *
369a3aeeab5SEric Blake  * @perm is a bitmasks of BLK_PERM_* constants which describes the
370a3aeeab5SEric Blake  * permissions to request for @bs that is attached to this
371a3aeeab5SEric Blake  * BlockBackend.  @shared_perm is a bitmask which describes which
372a3aeeab5SEric Blake  * permissions may be granted to other users of the attached node.
373a3aeeab5SEric Blake  * Both sets of permissions can be changed later using blk_set_perm().
374a3aeeab5SEric Blake  *
375a3aeeab5SEric Blake  * Return the new BlockBackend on success, null on failure.
376a3aeeab5SEric Blake  */
377a3aeeab5SEric Blake BlockBackend *blk_new_with_bs(BlockDriverState *bs, uint64_t perm,
378a3aeeab5SEric Blake                               uint64_t shared_perm, Error **errp)
379a3aeeab5SEric Blake {
380a3aeeab5SEric Blake     BlockBackend *blk = blk_new(bdrv_get_aio_context(bs), perm, shared_perm);
381a3aeeab5SEric Blake 
382a3aeeab5SEric Blake     if (blk_insert_bs(blk, bs, errp) < 0) {
383a3aeeab5SEric Blake         blk_unref(blk);
384a3aeeab5SEric Blake         return NULL;
385a3aeeab5SEric Blake     }
386a3aeeab5SEric Blake     return blk;
387a3aeeab5SEric Blake }
388a3aeeab5SEric Blake 
389a3aeeab5SEric Blake /*
39028eb9b12SMax Reitz  * Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
391d861ab3aSKevin Wolf  * The new BlockBackend is in the main AioContext.
392ca49a4fdSMax Reitz  *
393ca49a4fdSMax Reitz  * Just as with bdrv_open(), after having called this function the reference to
394ca49a4fdSMax Reitz  * @options belongs to the block layer (even on failure).
395ca49a4fdSMax Reitz  *
396ca49a4fdSMax Reitz  * TODO: Remove @filename and @flags; it should be possible to specify a whole
397ca49a4fdSMax Reitz  * BDS tree just by specifying the @options QDict (or @reference,
398ca49a4fdSMax Reitz  * alternatively). At the time of adding this function, this is not possible,
399ca49a4fdSMax Reitz  * though, so callers of this function have to be able to specify @filename and
400ca49a4fdSMax Reitz  * @flags.
401ca49a4fdSMax Reitz  */
402efaa7c4eSMax Reitz BlockBackend *blk_new_open(const char *filename, const char *reference,
403efaa7c4eSMax Reitz                            QDict *options, int flags, Error **errp)
404ca49a4fdSMax Reitz {
405ca49a4fdSMax Reitz     BlockBackend *blk;
40628eb9b12SMax Reitz     BlockDriverState *bs;
4071f4ad7d3SKevin Wolf     uint64_t perm = 0;
40835b7f4abSKevin Wolf     uint64_t shared = BLK_PERM_ALL;
409ca49a4fdSMax Reitz 
41035b7f4abSKevin Wolf     /*
41135b7f4abSKevin Wolf      * blk_new_open() is mainly used in .bdrv_create implementations and the
41235b7f4abSKevin Wolf      * tools where sharing isn't a major concern because the BDS stays private
41335b7f4abSKevin Wolf      * and the file is generally not supposed to be used by a second process,
41435b7f4abSKevin Wolf      * so we just request permission according to the flags.
415c62d32f5SKevin Wolf      *
416c62d32f5SKevin Wolf      * The exceptions are xen_disk and blockdev_init(); in these cases, the
417c62d32f5SKevin Wolf      * caller of blk_new_open() doesn't make use of the permissions, but they
418c62d32f5SKevin Wolf      * shouldn't hurt either. We can still share everything here because the
41935b7f4abSKevin Wolf      * guest devices will add their own blockers if they can't share.
42035b7f4abSKevin Wolf      */
4211f4ad7d3SKevin Wolf     if ((flags & BDRV_O_NO_IO) == 0) {
4221f4ad7d3SKevin Wolf         perm |= BLK_PERM_CONSISTENT_READ;
423c62d32f5SKevin Wolf         if (flags & BDRV_O_RDWR) {
424c62d32f5SKevin Wolf             perm |= BLK_PERM_WRITE;
425c62d32f5SKevin Wolf         }
4261f4ad7d3SKevin Wolf     }
427c62d32f5SKevin Wolf     if (flags & BDRV_O_RESIZE) {
428c62d32f5SKevin Wolf         perm |= BLK_PERM_RESIZE;
429c62d32f5SKevin Wolf     }
43035b7f4abSKevin Wolf     if (flags & BDRV_O_NO_SHARE) {
43135b7f4abSKevin Wolf         shared = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
43235b7f4abSKevin Wolf     }
433c62d32f5SKevin Wolf 
43435b7f4abSKevin Wolf     blk = blk_new(qemu_get_aio_context(), perm, shared);
4355b363937SMax Reitz     bs = bdrv_open(filename, reference, options, flags, errp);
4365b363937SMax Reitz     if (!bs) {
437ca49a4fdSMax Reitz         blk_unref(blk);
438ca49a4fdSMax Reitz         return NULL;
439ca49a4fdSMax Reitz     }
440ca49a4fdSMax Reitz 
4411f38f04eSMax Reitz     blk->root = bdrv_root_attach_child(bs, "root", &child_root,
4421f38f04eSMax Reitz                                        BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
44335b7f4abSKevin Wolf                                        perm, shared, blk, errp);
44450bfbe93SFam Zheng     if (!blk->root) {
44550bfbe93SFam Zheng         blk_unref(blk);
44650bfbe93SFam Zheng         return NULL;
44750bfbe93SFam Zheng     }
44872e775c7SKevin Wolf 
449ca49a4fdSMax Reitz     return blk;
450ca49a4fdSMax Reitz }
451ca49a4fdSMax Reitz 
45226f54e9aSMarkus Armbruster static void blk_delete(BlockBackend *blk)
45326f54e9aSMarkus Armbruster {
45426f54e9aSMarkus Armbruster     assert(!blk->refcnt);
455e5e78550SMax Reitz     assert(!blk->name);
456a7f53e26SMarkus Armbruster     assert(!blk->dev);
457022cdc9fSManos Pitsidianakis     if (blk->public.throttle_group_member.throttle_state) {
4581606e4cfSEric Blake         blk_io_limits_disable(blk);
4591606e4cfSEric Blake     }
460f21d96d0SKevin Wolf     if (blk->root) {
46113855c6bSMax Reitz         blk_remove_bs(blk);
4627e7d56d9SMarkus Armbruster     }
4635f7772c4SFam Zheng     if (blk->vmsh) {
4645f7772c4SFam Zheng         qemu_del_vm_change_state_handler(blk->vmsh);
4655f7772c4SFam Zheng         blk->vmsh = NULL;
4665f7772c4SFam Zheng     }
4673301f6c6SMax Reitz     assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
4683301f6c6SMax Reitz     assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
469d03654eaSStefan Hajnoczi     assert(QLIST_EMPTY(&blk->aio_notifiers));
4702cf22d6aSMax Reitz     QTAILQ_REMOVE(&block_backends, blk, link);
47118e46a03SMarkus Armbruster     drive_info_del(blk->legacy_dinfo);
472979e9b03SAlberto Garcia     block_acct_cleanup(&blk->stats);
47326f54e9aSMarkus Armbruster     g_free(blk);
47426f54e9aSMarkus Armbruster }
47526f54e9aSMarkus Armbruster 
4768fb3c76cSMarkus Armbruster static void drive_info_del(DriveInfo *dinfo)
4778fb3c76cSMarkus Armbruster {
4788fb3c76cSMarkus Armbruster     if (!dinfo) {
4798fb3c76cSMarkus Armbruster         return;
4808fb3c76cSMarkus Armbruster     }
4818fb3c76cSMarkus Armbruster     qemu_opts_del(dinfo->opts);
4828fb3c76cSMarkus Armbruster     g_free(dinfo);
4838fb3c76cSMarkus Armbruster }
4848fb3c76cSMarkus Armbruster 
485f636ae85SAlberto Garcia int blk_get_refcnt(BlockBackend *blk)
486f636ae85SAlberto Garcia {
487f636ae85SAlberto Garcia     return blk ? blk->refcnt : 0;
488f636ae85SAlberto Garcia }
489f636ae85SAlberto Garcia 
49026f54e9aSMarkus Armbruster /*
49126f54e9aSMarkus Armbruster  * Increment @blk's reference count.
49226f54e9aSMarkus Armbruster  * @blk must not be null.
49326f54e9aSMarkus Armbruster  */
49426f54e9aSMarkus Armbruster void blk_ref(BlockBackend *blk)
49526f54e9aSMarkus Armbruster {
4965ca9d21bSKevin Wolf     assert(blk->refcnt > 0);
49726f54e9aSMarkus Armbruster     blk->refcnt++;
49826f54e9aSMarkus Armbruster }
49926f54e9aSMarkus Armbruster 
50026f54e9aSMarkus Armbruster /*
50126f54e9aSMarkus Armbruster  * Decrement @blk's reference count.
50226f54e9aSMarkus Armbruster  * If this drops it to zero, destroy @blk.
50326f54e9aSMarkus Armbruster  * For convenience, do nothing if @blk is null.
50426f54e9aSMarkus Armbruster  */
50526f54e9aSMarkus Armbruster void blk_unref(BlockBackend *blk)
50626f54e9aSMarkus Armbruster {
50726f54e9aSMarkus Armbruster     if (blk) {
50826f54e9aSMarkus Armbruster         assert(blk->refcnt > 0);
5095ca9d21bSKevin Wolf         if (blk->refcnt > 1) {
5105ca9d21bSKevin Wolf             blk->refcnt--;
5115ca9d21bSKevin Wolf         } else {
5125ca9d21bSKevin Wolf             blk_drain(blk);
5135ca9d21bSKevin Wolf             /* blk_drain() cannot resurrect blk, nobody held a reference */
5145ca9d21bSKevin Wolf             assert(blk->refcnt == 1);
5155ca9d21bSKevin Wolf             blk->refcnt = 0;
51626f54e9aSMarkus Armbruster             blk_delete(blk);
51726f54e9aSMarkus Armbruster         }
51826f54e9aSMarkus Armbruster     }
51926f54e9aSMarkus Armbruster }
52026f54e9aSMarkus Armbruster 
5212cf22d6aSMax Reitz /*
5222cf22d6aSMax Reitz  * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
5232cf22d6aSMax Reitz  * ones which are hidden (i.e. are not referenced by the monitor).
5242cf22d6aSMax Reitz  */
525a429b9b5SKevin Wolf BlockBackend *blk_all_next(BlockBackend *blk)
5262cf22d6aSMax Reitz {
5272cf22d6aSMax Reitz     return blk ? QTAILQ_NEXT(blk, link)
5282cf22d6aSMax Reitz                : QTAILQ_FIRST(&block_backends);
5292cf22d6aSMax Reitz }
5302cf22d6aSMax Reitz 
531d8da3cefSMax Reitz void blk_remove_all_bs(void)
532d8da3cefSMax Reitz {
53374d1b8fcSMax Reitz     BlockBackend *blk = NULL;
534d8da3cefSMax Reitz 
5352cf22d6aSMax Reitz     while ((blk = blk_all_next(blk)) != NULL) {
536d8da3cefSMax Reitz         AioContext *ctx = blk_get_aio_context(blk);
537d8da3cefSMax Reitz 
538d8da3cefSMax Reitz         aio_context_acquire(ctx);
539f21d96d0SKevin Wolf         if (blk->root) {
540d8da3cefSMax Reitz             blk_remove_bs(blk);
541d8da3cefSMax Reitz         }
542d8da3cefSMax Reitz         aio_context_release(ctx);
543d8da3cefSMax Reitz     }
544d8da3cefSMax Reitz }
545d8da3cefSMax Reitz 
54626f54e9aSMarkus Armbruster /*
5479492b0b9SMax Reitz  * Return the monitor-owned BlockBackend after @blk.
54826f54e9aSMarkus Armbruster  * If @blk is null, return the first one.
54926f54e9aSMarkus Armbruster  * Else, return @blk's next sibling, which may be null.
55026f54e9aSMarkus Armbruster  *
55126f54e9aSMarkus Armbruster  * To iterate over all BlockBackends, do
55226f54e9aSMarkus Armbruster  * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
55326f54e9aSMarkus Armbruster  *     ...
55426f54e9aSMarkus Armbruster  * }
55526f54e9aSMarkus Armbruster  */
55626f54e9aSMarkus Armbruster BlockBackend *blk_next(BlockBackend *blk)
55726f54e9aSMarkus Armbruster {
5589492b0b9SMax Reitz     return blk ? QTAILQ_NEXT(blk, monitor_link)
5599492b0b9SMax Reitz                : QTAILQ_FIRST(&monitor_block_backends);
56026f54e9aSMarkus Armbruster }
56126f54e9aSMarkus Armbruster 
5627c8eece4SKevin Wolf /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by
5637c8eece4SKevin Wolf  * the monitor or attached to a BlockBackend */
56488be7b4bSKevin Wolf BlockDriverState *bdrv_next(BdrvNextIterator *it)
5657c8eece4SKevin Wolf {
5665e003f17SMax Reitz     BlockDriverState *bs, *old_bs;
5675e003f17SMax Reitz 
5685e003f17SMax Reitz     /* Must be called from the main loop */
5695e003f17SMax Reitz     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
570981f4f57SMax Reitz 
5717c8eece4SKevin Wolf     /* First, return all root nodes of BlockBackends. In order to avoid
5727c8eece4SKevin Wolf      * returning a BDS twice when multiple BBs refer to it, we only return it
5737c8eece4SKevin Wolf      * if the BB is the first one in the parent list of the BDS. */
5747c8eece4SKevin Wolf     if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
5755e003f17SMax Reitz         BlockBackend *old_blk = it->blk;
5765e003f17SMax Reitz 
5775e003f17SMax Reitz         old_bs = old_blk ? blk_bs(old_blk) : NULL;
5785e003f17SMax Reitz 
579981f4f57SMax Reitz         do {
5807c8eece4SKevin Wolf             it->blk = blk_all_next(it->blk);
58188be7b4bSKevin Wolf             bs = it->blk ? blk_bs(it->blk) : NULL;
58288be7b4bSKevin Wolf         } while (it->blk && (bs == NULL || bdrv_first_blk(bs) != it->blk));
583981f4f57SMax Reitz 
5845e003f17SMax Reitz         if (it->blk) {
5855e003f17SMax Reitz             blk_ref(it->blk);
5865e003f17SMax Reitz         }
5875e003f17SMax Reitz         blk_unref(old_blk);
5885e003f17SMax Reitz 
58988be7b4bSKevin Wolf         if (bs) {
5905e003f17SMax Reitz             bdrv_ref(bs);
5915e003f17SMax Reitz             bdrv_unref(old_bs);
59288be7b4bSKevin Wolf             return bs;
5937c8eece4SKevin Wolf         }
5947c8eece4SKevin Wolf         it->phase = BDRV_NEXT_MONITOR_OWNED;
5955e003f17SMax Reitz     } else {
5965e003f17SMax Reitz         old_bs = it->bs;
5977c8eece4SKevin Wolf     }
5987c8eece4SKevin Wolf 
5997c8eece4SKevin Wolf     /* Then return the monitor-owned BDSes without a BB attached. Ignore all
6007c8eece4SKevin Wolf      * BDSes that are attached to a BlockBackend here; they have been handled
6017c8eece4SKevin Wolf      * by the above block already */
6027c8eece4SKevin Wolf     do {
6037c8eece4SKevin Wolf         it->bs = bdrv_next_monitor_owned(it->bs);
60488be7b4bSKevin Wolf         bs = it->bs;
60588be7b4bSKevin Wolf     } while (bs && bdrv_has_blk(bs));
6067c8eece4SKevin Wolf 
6075e003f17SMax Reitz     if (bs) {
6085e003f17SMax Reitz         bdrv_ref(bs);
6095e003f17SMax Reitz     }
6105e003f17SMax Reitz     bdrv_unref(old_bs);
6115e003f17SMax Reitz 
61288be7b4bSKevin Wolf     return bs;
61388be7b4bSKevin Wolf }
61488be7b4bSKevin Wolf 
6155e003f17SMax Reitz static void bdrv_next_reset(BdrvNextIterator *it)
61688be7b4bSKevin Wolf {
61788be7b4bSKevin Wolf     *it = (BdrvNextIterator) {
61888be7b4bSKevin Wolf         .phase = BDRV_NEXT_BACKEND_ROOTS,
61988be7b4bSKevin Wolf     };
6205e003f17SMax Reitz }
62188be7b4bSKevin Wolf 
6225e003f17SMax Reitz BlockDriverState *bdrv_first(BdrvNextIterator *it)
6235e003f17SMax Reitz {
6245e003f17SMax Reitz     bdrv_next_reset(it);
62588be7b4bSKevin Wolf     return bdrv_next(it);
626981f4f57SMax Reitz }
627981f4f57SMax Reitz 
6285e003f17SMax Reitz /* Must be called when aborting a bdrv_next() iteration before
6295e003f17SMax Reitz  * bdrv_next() returns NULL */
6305e003f17SMax Reitz void bdrv_next_cleanup(BdrvNextIterator *it)
6315e003f17SMax Reitz {
6325e003f17SMax Reitz     /* Must be called from the main loop */
6335e003f17SMax Reitz     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
6345e003f17SMax Reitz 
6355e003f17SMax Reitz     if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
6365e003f17SMax Reitz         if (it->blk) {
6375e003f17SMax Reitz             bdrv_unref(blk_bs(it->blk));
6385e003f17SMax Reitz             blk_unref(it->blk);
6395e003f17SMax Reitz         }
6405e003f17SMax Reitz     } else {
6415e003f17SMax Reitz         bdrv_unref(it->bs);
6425e003f17SMax Reitz     }
6435e003f17SMax Reitz 
6445e003f17SMax Reitz     bdrv_next_reset(it);
6455e003f17SMax Reitz }
6465e003f17SMax Reitz 
647981f4f57SMax Reitz /*
648e5e78550SMax Reitz  * Add a BlockBackend into the list of backends referenced by the monitor, with
649e5e78550SMax Reitz  * the given @name acting as the handle for the monitor.
650e5e78550SMax Reitz  * Strictly for use by blockdev.c.
651e5e78550SMax Reitz  *
652e5e78550SMax Reitz  * @name must not be null or empty.
653e5e78550SMax Reitz  *
654e5e78550SMax Reitz  * Returns true on success and false on failure. In the latter case, an Error
655e5e78550SMax Reitz  * object is returned through @errp.
656e5e78550SMax Reitz  */
657e5e78550SMax Reitz bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
658e5e78550SMax Reitz {
659e5e78550SMax Reitz     assert(!blk->name);
660e5e78550SMax Reitz     assert(name && name[0]);
661e5e78550SMax Reitz 
662e5e78550SMax Reitz     if (!id_wellformed(name)) {
663e5e78550SMax Reitz         error_setg(errp, "Invalid device name");
664e5e78550SMax Reitz         return false;
665e5e78550SMax Reitz     }
666e5e78550SMax Reitz     if (blk_by_name(name)) {
667e5e78550SMax Reitz         error_setg(errp, "Device with id '%s' already exists", name);
668e5e78550SMax Reitz         return false;
669e5e78550SMax Reitz     }
670e5e78550SMax Reitz     if (bdrv_find_node(name)) {
671e5e78550SMax Reitz         error_setg(errp,
672e5e78550SMax Reitz                    "Device name '%s' conflicts with an existing node name",
673e5e78550SMax Reitz                    name);
674e5e78550SMax Reitz         return false;
675e5e78550SMax Reitz     }
676e5e78550SMax Reitz 
677e5e78550SMax Reitz     blk->name = g_strdup(name);
678e5e78550SMax Reitz     QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
679e5e78550SMax Reitz     return true;
680e5e78550SMax Reitz }
681e5e78550SMax Reitz 
682e5e78550SMax Reitz /*
683e5e78550SMax Reitz  * Remove a BlockBackend from the list of backends referenced by the monitor.
684e5e78550SMax Reitz  * Strictly for use by blockdev.c.
685e5e78550SMax Reitz  */
686e5e78550SMax Reitz void monitor_remove_blk(BlockBackend *blk)
687e5e78550SMax Reitz {
688e5e78550SMax Reitz     if (!blk->name) {
689e5e78550SMax Reitz         return;
690e5e78550SMax Reitz     }
691e5e78550SMax Reitz 
692e5e78550SMax Reitz     QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
693e5e78550SMax Reitz     g_free(blk->name);
694e5e78550SMax Reitz     blk->name = NULL;
695e5e78550SMax Reitz }
696e5e78550SMax Reitz 
697e5e78550SMax Reitz /*
6987e7d56d9SMarkus Armbruster  * Return @blk's name, a non-null string.
699e5e78550SMax Reitz  * Returns an empty string iff @blk is not referenced by the monitor.
70026f54e9aSMarkus Armbruster  */
7010731a50fSKrzysztof Kozlowski const char *blk_name(const BlockBackend *blk)
70226f54e9aSMarkus Armbruster {
703e5e78550SMax Reitz     return blk->name ?: "";
70426f54e9aSMarkus Armbruster }
70526f54e9aSMarkus Armbruster 
70626f54e9aSMarkus Armbruster /*
70726f54e9aSMarkus Armbruster  * Return the BlockBackend with name @name if it exists, else null.
70826f54e9aSMarkus Armbruster  * @name must not be null.
70926f54e9aSMarkus Armbruster  */
71026f54e9aSMarkus Armbruster BlockBackend *blk_by_name(const char *name)
71126f54e9aSMarkus Armbruster {
71274d1b8fcSMax Reitz     BlockBackend *blk = NULL;
71326f54e9aSMarkus Armbruster 
71426f54e9aSMarkus Armbruster     assert(name);
71574d1b8fcSMax Reitz     while ((blk = blk_next(blk)) != NULL) {
71626f54e9aSMarkus Armbruster         if (!strcmp(name, blk->name)) {
71726f54e9aSMarkus Armbruster             return blk;
71826f54e9aSMarkus Armbruster         }
71926f54e9aSMarkus Armbruster     }
72026f54e9aSMarkus Armbruster     return NULL;
72126f54e9aSMarkus Armbruster }
7227e7d56d9SMarkus Armbruster 
7237e7d56d9SMarkus Armbruster /*
7247e7d56d9SMarkus Armbruster  * Return the BlockDriverState attached to @blk if any, else null.
7257e7d56d9SMarkus Armbruster  */
7267e7d56d9SMarkus Armbruster BlockDriverState *blk_bs(BlockBackend *blk)
7277e7d56d9SMarkus Armbruster {
728f21d96d0SKevin Wolf     return blk->root ? blk->root->bs : NULL;
7297e7d56d9SMarkus Armbruster }
7307e7d56d9SMarkus Armbruster 
7317c8eece4SKevin Wolf static BlockBackend *bdrv_first_blk(BlockDriverState *bs)
732dde33812SKevin Wolf {
733dde33812SKevin Wolf     BdrvChild *child;
734dde33812SKevin Wolf     QLIST_FOREACH(child, &bs->parents, next_parent) {
735bd86fb99SMax Reitz         if (child->klass == &child_root) {
7367c8eece4SKevin Wolf             return child->opaque;
737dde33812SKevin Wolf         }
738dde33812SKevin Wolf     }
739dde33812SKevin Wolf 
7407c8eece4SKevin Wolf     return NULL;
7417c8eece4SKevin Wolf }
7427c8eece4SKevin Wolf 
7437c8eece4SKevin Wolf /*
7447c8eece4SKevin Wolf  * Returns true if @bs has an associated BlockBackend.
7457c8eece4SKevin Wolf  */
7467c8eece4SKevin Wolf bool bdrv_has_blk(BlockDriverState *bs)
7477c8eece4SKevin Wolf {
7487c8eece4SKevin Wolf     return bdrv_first_blk(bs) != NULL;
749dde33812SKevin Wolf }
750dde33812SKevin Wolf 
751dde33812SKevin Wolf /*
752b6c1bae5SKevin Wolf  * Returns true if @bs has only BlockBackends as parents.
753b6c1bae5SKevin Wolf  */
754b6c1bae5SKevin Wolf bool bdrv_is_root_node(BlockDriverState *bs)
755b6c1bae5SKevin Wolf {
756b6c1bae5SKevin Wolf     BdrvChild *c;
757b6c1bae5SKevin Wolf 
758b6c1bae5SKevin Wolf     QLIST_FOREACH(c, &bs->parents, next_parent) {
759bd86fb99SMax Reitz         if (c->klass != &child_root) {
760b6c1bae5SKevin Wolf             return false;
761b6c1bae5SKevin Wolf         }
762b6c1bae5SKevin Wolf     }
763b6c1bae5SKevin Wolf 
764b6c1bae5SKevin Wolf     return true;
765b6c1bae5SKevin Wolf }
766b6c1bae5SKevin Wolf 
767b6c1bae5SKevin Wolf /*
76818e46a03SMarkus Armbruster  * Return @blk's DriveInfo if any, else null.
76918e46a03SMarkus Armbruster  */
77018e46a03SMarkus Armbruster DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
77118e46a03SMarkus Armbruster {
77218e46a03SMarkus Armbruster     return blk->legacy_dinfo;
77318e46a03SMarkus Armbruster }
77418e46a03SMarkus Armbruster 
77518e46a03SMarkus Armbruster /*
77618e46a03SMarkus Armbruster  * Set @blk's DriveInfo to @dinfo, and return it.
77718e46a03SMarkus Armbruster  * @blk must not have a DriveInfo set already.
77818e46a03SMarkus Armbruster  * No other BlockBackend may have the same DriveInfo set.
77918e46a03SMarkus Armbruster  */
78018e46a03SMarkus Armbruster DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
78118e46a03SMarkus Armbruster {
78218e46a03SMarkus Armbruster     assert(!blk->legacy_dinfo);
78318e46a03SMarkus Armbruster     return blk->legacy_dinfo = dinfo;
78418e46a03SMarkus Armbruster }
78518e46a03SMarkus Armbruster 
78618e46a03SMarkus Armbruster /*
78718e46a03SMarkus Armbruster  * Return the BlockBackend with DriveInfo @dinfo.
78818e46a03SMarkus Armbruster  * It must exist.
78918e46a03SMarkus Armbruster  */
79018e46a03SMarkus Armbruster BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
79118e46a03SMarkus Armbruster {
79274d1b8fcSMax Reitz     BlockBackend *blk = NULL;
79318e46a03SMarkus Armbruster 
79474d1b8fcSMax Reitz     while ((blk = blk_next(blk)) != NULL) {
79518e46a03SMarkus Armbruster         if (blk->legacy_dinfo == dinfo) {
79618e46a03SMarkus Armbruster             return blk;
79718e46a03SMarkus Armbruster         }
79818e46a03SMarkus Armbruster     }
79918e46a03SMarkus Armbruster     abort();
80018e46a03SMarkus Armbruster }
80118e46a03SMarkus Armbruster 
80218e46a03SMarkus Armbruster /*
803f2cd875dSKevin Wolf  * Returns a pointer to the publicly accessible fields of @blk.
804f2cd875dSKevin Wolf  */
805f2cd875dSKevin Wolf BlockBackendPublic *blk_get_public(BlockBackend *blk)
806f2cd875dSKevin Wolf {
807f2cd875dSKevin Wolf     return &blk->public;
808f2cd875dSKevin Wolf }
809f2cd875dSKevin Wolf 
810f2cd875dSKevin Wolf /*
811f2cd875dSKevin Wolf  * Returns a BlockBackend given the associated @public fields.
812f2cd875dSKevin Wolf  */
813f2cd875dSKevin Wolf BlockBackend *blk_by_public(BlockBackendPublic *public)
814f2cd875dSKevin Wolf {
815f2cd875dSKevin Wolf     return container_of(public, BlockBackend, public);
816f2cd875dSKevin Wolf }
817f2cd875dSKevin Wolf 
818f2cd875dSKevin Wolf /*
8191c95f7e1SMax Reitz  * Disassociates the currently associated BlockDriverState from @blk.
8201c95f7e1SMax Reitz  */
8211c95f7e1SMax Reitz void blk_remove_bs(BlockBackend *blk)
8221c95f7e1SMax Reitz {
823c89bcf3aSAlberto Garcia     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
824632a7735SZhengui     BlockDriverState *bs;
825e6cada92SGreg Kurz     BdrvChild *root;
826022cdc9fSManos Pitsidianakis 
8273301f6c6SMax Reitz     notifier_list_notify(&blk->remove_bs_notifiers, blk);
828c89bcf3aSAlberto Garcia     if (tgm->throttle_state) {
829632a7735SZhengui         bs = blk_bs(blk);
830632a7735SZhengui         bdrv_drained_begin(bs);
831c89bcf3aSAlberto Garcia         throttle_group_detach_aio_context(tgm);
832c89bcf3aSAlberto Garcia         throttle_group_attach_aio_context(tgm, qemu_get_aio_context());
833632a7735SZhengui         bdrv_drained_end(bs);
8347ca7f0f6SKevin Wolf     }
8353301f6c6SMax Reitz 
8361c95f7e1SMax Reitz     blk_update_root_state(blk);
8371c95f7e1SMax Reitz 
838f45280cbSGreg Kurz     /* bdrv_root_unref_child() will cause blk->root to become stale and may
839f45280cbSGreg Kurz      * switch to a completion coroutine later on. Let's drain all I/O here
840f45280cbSGreg Kurz      * to avoid that and a potential QEMU crash.
841f45280cbSGreg Kurz      */
842f45280cbSGreg Kurz     blk_drain(blk);
843e6cada92SGreg Kurz     root = blk->root;
844f21d96d0SKevin Wolf     blk->root = NULL;
845e6cada92SGreg Kurz     bdrv_root_unref_child(root);
8461c95f7e1SMax Reitz }
8471c95f7e1SMax Reitz 
8481c95f7e1SMax Reitz /*
8490c3c36d6SMax Reitz  * Associates a new BlockDriverState with @blk.
8500c3c36d6SMax Reitz  */
851d7086422SKevin Wolf int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
8520c3c36d6SMax Reitz {
853c89bcf3aSAlberto Garcia     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
854b441dc71SAlberto Garcia     bdrv_ref(bs);
8551f38f04eSMax Reitz     blk->root = bdrv_root_attach_child(bs, "root", &child_root,
8561f38f04eSMax Reitz                                        BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
857228ca37eSVladimir Sementsov-Ogievskiy                                        blk->perm, blk->shared_perm,
8581f38f04eSMax Reitz                                        blk, errp);
859d7086422SKevin Wolf     if (blk->root == NULL) {
860d7086422SKevin Wolf         return -EPERM;
861d7086422SKevin Wolf     }
8623301f6c6SMax Reitz 
8633301f6c6SMax Reitz     notifier_list_notify(&blk->insert_bs_notifiers, blk);
864c89bcf3aSAlberto Garcia     if (tgm->throttle_state) {
865c89bcf3aSAlberto Garcia         throttle_group_detach_aio_context(tgm);
866c89bcf3aSAlberto Garcia         throttle_group_attach_aio_context(tgm, bdrv_get_aio_context(bs));
8677ca7f0f6SKevin Wolf     }
868d7086422SKevin Wolf 
869d7086422SKevin Wolf     return 0;
8700c3c36d6SMax Reitz }
8710c3c36d6SMax Reitz 
872981776b3SKevin Wolf /*
873ed089506SVladimir Sementsov-Ogievskiy  * Change BlockDriverState associated with @blk.
874ed089506SVladimir Sementsov-Ogievskiy  */
875ed089506SVladimir Sementsov-Ogievskiy int blk_replace_bs(BlockBackend *blk, BlockDriverState *new_bs, Error **errp)
876ed089506SVladimir Sementsov-Ogievskiy {
877ed089506SVladimir Sementsov-Ogievskiy     return bdrv_replace_child_bs(blk->root, new_bs, errp);
878ed089506SVladimir Sementsov-Ogievskiy }
879ed089506SVladimir Sementsov-Ogievskiy 
880ed089506SVladimir Sementsov-Ogievskiy /*
881981776b3SKevin Wolf  * Sets the permission bitmasks that the user of the BlockBackend needs.
882981776b3SKevin Wolf  */
883981776b3SKevin Wolf int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,
884981776b3SKevin Wolf                  Error **errp)
885981776b3SKevin Wolf {
886981776b3SKevin Wolf     int ret;
887981776b3SKevin Wolf 
888d35ff5e6SKevin Wolf     if (blk->root && !blk->disable_perm) {
889981776b3SKevin Wolf         ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp);
890981776b3SKevin Wolf         if (ret < 0) {
891981776b3SKevin Wolf             return ret;
892981776b3SKevin Wolf         }
893981776b3SKevin Wolf     }
894981776b3SKevin Wolf 
895981776b3SKevin Wolf     blk->perm = perm;
896981776b3SKevin Wolf     blk->shared_perm = shared_perm;
897981776b3SKevin Wolf 
898981776b3SKevin Wolf     return 0;
899981776b3SKevin Wolf }
900981776b3SKevin Wolf 
901887354bdSKevin Wolf void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
902887354bdSKevin Wolf {
903887354bdSKevin Wolf     *perm = blk->perm;
904887354bdSKevin Wolf     *shared_perm = blk->shared_perm;
905887354bdSKevin Wolf }
906887354bdSKevin Wolf 
907d09ea2d2SThomas Huth /*
908d09ea2d2SThomas Huth  * Attach device model @dev to @blk.
909d09ea2d2SThomas Huth  * Return 0 on success, -EBUSY when a device model is attached already.
910d09ea2d2SThomas Huth  */
911d09ea2d2SThomas Huth int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
912a7f53e26SMarkus Armbruster {
913a7f53e26SMarkus Armbruster     if (blk->dev) {
914a7f53e26SMarkus Armbruster         return -EBUSY;
915a7f53e26SMarkus Armbruster     }
916d35ff5e6SKevin Wolf 
917d35ff5e6SKevin Wolf     /* While migration is still incoming, we don't need to apply the
918d35ff5e6SKevin Wolf      * permissions of guest device BlockBackends. We might still have a block
919d35ff5e6SKevin Wolf      * job or NBD server writing to the image for storage migration. */
920d35ff5e6SKevin Wolf     if (runstate_check(RUN_STATE_INMIGRATE)) {
921d35ff5e6SKevin Wolf         blk->disable_perm = true;
922d35ff5e6SKevin Wolf     }
923d35ff5e6SKevin Wolf 
92484ebe375SMarkus Armbruster     blk_ref(blk);
925a7f53e26SMarkus Armbruster     blk->dev = dev;
926373340b2SMax Reitz     blk_iostatus_reset(blk);
927d35ff5e6SKevin Wolf 
928a7f53e26SMarkus Armbruster     return 0;
929a7f53e26SMarkus Armbruster }
930a7f53e26SMarkus Armbruster 
931a7f53e26SMarkus Armbruster /*
932a7f53e26SMarkus Armbruster  * Detach device model @dev from @blk.
933a7f53e26SMarkus Armbruster  * @dev must be currently attached to @blk.
934a7f53e26SMarkus Armbruster  */
935d09ea2d2SThomas Huth void blk_detach_dev(BlockBackend *blk, DeviceState *dev)
936a7f53e26SMarkus Armbruster {
937a7f53e26SMarkus Armbruster     assert(blk->dev == dev);
938a7f53e26SMarkus Armbruster     blk->dev = NULL;
939a7f53e26SMarkus Armbruster     blk->dev_ops = NULL;
940a7f53e26SMarkus Armbruster     blk->dev_opaque = NULL;
94168e9ec01SMax Reitz     blk->guest_block_size = 512;
942981776b3SKevin Wolf     blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
94384ebe375SMarkus Armbruster     blk_unref(blk);
944a7f53e26SMarkus Armbruster }
945a7f53e26SMarkus Armbruster 
946a7f53e26SMarkus Armbruster /*
947a7f53e26SMarkus Armbruster  * Return the device model attached to @blk if any, else null.
948a7f53e26SMarkus Armbruster  */
949d09ea2d2SThomas Huth DeviceState *blk_get_attached_dev(BlockBackend *blk)
950a7f53e26SMarkus Armbruster {
951a7f53e26SMarkus Armbruster     return blk->dev;
952a7f53e26SMarkus Armbruster }
953a7f53e26SMarkus Armbruster 
9542d76e724SKevin Wolf /* Return the qdev ID, or if no ID is assigned the QOM path, of the block
9552d76e724SKevin Wolf  * device attached to the BlockBackend. */
95677beef83SKevin Wolf char *blk_get_attached_dev_id(BlockBackend *blk)
9572d76e724SKevin Wolf {
958d09ea2d2SThomas Huth     DeviceState *dev = blk->dev;
9592d76e724SKevin Wolf 
9602d76e724SKevin Wolf     if (!dev) {
9612d76e724SKevin Wolf         return g_strdup("");
9622d76e724SKevin Wolf     } else if (dev->id) {
9632d76e724SKevin Wolf         return g_strdup(dev->id);
9642d76e724SKevin Wolf     }
965602414d1SLiam Merwick 
966602414d1SLiam Merwick     return object_get_canonical_path(OBJECT(dev)) ?: g_strdup("");
9672d76e724SKevin Wolf }
9682d76e724SKevin Wolf 
969a7f53e26SMarkus Armbruster /*
9701c89e1faSKevin Wolf  * Return the BlockBackend which has the device model @dev attached if it
9711c89e1faSKevin Wolf  * exists, else null.
9721c89e1faSKevin Wolf  *
9731c89e1faSKevin Wolf  * @dev must not be null.
9741c89e1faSKevin Wolf  */
9751c89e1faSKevin Wolf BlockBackend *blk_by_dev(void *dev)
9761c89e1faSKevin Wolf {
9771c89e1faSKevin Wolf     BlockBackend *blk = NULL;
9781c89e1faSKevin Wolf 
9791c89e1faSKevin Wolf     assert(dev != NULL);
9801c89e1faSKevin Wolf     while ((blk = blk_all_next(blk)) != NULL) {
9811c89e1faSKevin Wolf         if (blk->dev == dev) {
9821c89e1faSKevin Wolf             return blk;
9831c89e1faSKevin Wolf         }
9841c89e1faSKevin Wolf     }
9851c89e1faSKevin Wolf     return NULL;
9861c89e1faSKevin Wolf }
9871c89e1faSKevin Wolf 
9881c89e1faSKevin Wolf /*
989a7f53e26SMarkus Armbruster  * Set @blk's device model callbacks to @ops.
990a7f53e26SMarkus Armbruster  * @opaque is the opaque argument to pass to the callbacks.
991a7f53e26SMarkus Armbruster  * This is for use by device models.
992a7f53e26SMarkus Armbruster  */
993a7f53e26SMarkus Armbruster void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
994a7f53e26SMarkus Armbruster                      void *opaque)
995a7f53e26SMarkus Armbruster {
996a7f53e26SMarkus Armbruster     blk->dev_ops = ops;
997a7f53e26SMarkus Armbruster     blk->dev_opaque = opaque;
998f4d9cc88SJohn Snow 
999f4d9cc88SJohn Snow     /* Are we currently quiesced? Should we enforce this right now? */
1000f4d9cc88SJohn Snow     if (blk->quiesce_counter && ops->drained_begin) {
1001f4d9cc88SJohn Snow         ops->drained_begin(opaque);
1002f4d9cc88SJohn Snow     }
1003a7f53e26SMarkus Armbruster }
1004a7f53e26SMarkus Armbruster 
1005a7f53e26SMarkus Armbruster /*
1006a7f53e26SMarkus Armbruster  * Notify @blk's attached device model of media change.
100739829a01SKevin Wolf  *
100839829a01SKevin Wolf  * If @load is true, notify of media load. This action can fail, meaning that
100939829a01SKevin Wolf  * the medium cannot be loaded. @errp is set then.
101039829a01SKevin Wolf  *
101139829a01SKevin Wolf  * If @load is false, notify of media eject. This can never fail.
101239829a01SKevin Wolf  *
1013a7f53e26SMarkus Armbruster  * Also send DEVICE_TRAY_MOVED events as appropriate.
1014a7f53e26SMarkus Armbruster  */
101539829a01SKevin Wolf void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp)
1016a7f53e26SMarkus Armbruster {
1017a7f53e26SMarkus Armbruster     if (blk->dev_ops && blk->dev_ops->change_media_cb) {
1018f1f57066SMax Reitz         bool tray_was_open, tray_is_open;
101939829a01SKevin Wolf         Error *local_err = NULL;
1020a7f53e26SMarkus Armbruster 
1021f1f57066SMax Reitz         tray_was_open = blk_dev_is_tray_open(blk);
102239829a01SKevin Wolf         blk->dev_ops->change_media_cb(blk->dev_opaque, load, &local_err);
102339829a01SKevin Wolf         if (local_err) {
102439829a01SKevin Wolf             assert(load == true);
102539829a01SKevin Wolf             error_propagate(errp, local_err);
102639829a01SKevin Wolf             return;
102739829a01SKevin Wolf         }
1028f1f57066SMax Reitz         tray_is_open = blk_dev_is_tray_open(blk);
1029f1f57066SMax Reitz 
1030f1f57066SMax Reitz         if (tray_was_open != tray_is_open) {
10312d76e724SKevin Wolf             char *id = blk_get_attached_dev_id(blk);
10323ab72385SPeter Xu             qapi_event_send_device_tray_moved(blk_name(blk), id, tray_is_open);
10332d76e724SKevin Wolf             g_free(id);
1034a7f53e26SMarkus Armbruster         }
1035a7f53e26SMarkus Armbruster     }
1036a7f53e26SMarkus Armbruster }
1037a7f53e26SMarkus Armbruster 
10385c8cab48SKevin Wolf static void blk_root_change_media(BdrvChild *child, bool load)
10395c8cab48SKevin Wolf {
104039829a01SKevin Wolf     blk_dev_change_media_cb(child->opaque, load, NULL);
10415c8cab48SKevin Wolf }
10425c8cab48SKevin Wolf 
1043a7f53e26SMarkus Armbruster /*
1044a7f53e26SMarkus Armbruster  * Does @blk's attached device model have removable media?
1045a7f53e26SMarkus Armbruster  * %true if no device model is attached.
1046a7f53e26SMarkus Armbruster  */
1047a7f53e26SMarkus Armbruster bool blk_dev_has_removable_media(BlockBackend *blk)
1048a7f53e26SMarkus Armbruster {
1049a7f53e26SMarkus Armbruster     return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
1050a7f53e26SMarkus Armbruster }
1051a7f53e26SMarkus Armbruster 
1052a7f53e26SMarkus Armbruster /*
10538f3a73bcSMax Reitz  * Does @blk's attached device model have a tray?
10548f3a73bcSMax Reitz  */
10558f3a73bcSMax Reitz bool blk_dev_has_tray(BlockBackend *blk)
10568f3a73bcSMax Reitz {
10578f3a73bcSMax Reitz     return blk->dev_ops && blk->dev_ops->is_tray_open;
10588f3a73bcSMax Reitz }
10598f3a73bcSMax Reitz 
10608f3a73bcSMax Reitz /*
1061a7f53e26SMarkus Armbruster  * Notify @blk's attached device model of a media eject request.
1062a7f53e26SMarkus Armbruster  * If @force is true, the medium is about to be yanked out forcefully.
1063a7f53e26SMarkus Armbruster  */
1064a7f53e26SMarkus Armbruster void blk_dev_eject_request(BlockBackend *blk, bool force)
1065a7f53e26SMarkus Armbruster {
1066a7f53e26SMarkus Armbruster     if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
1067a7f53e26SMarkus Armbruster         blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
1068a7f53e26SMarkus Armbruster     }
1069a7f53e26SMarkus Armbruster }
1070a7f53e26SMarkus Armbruster 
1071a7f53e26SMarkus Armbruster /*
1072a7f53e26SMarkus Armbruster  * Does @blk's attached device model have a tray, and is it open?
1073a7f53e26SMarkus Armbruster  */
1074a7f53e26SMarkus Armbruster bool blk_dev_is_tray_open(BlockBackend *blk)
1075a7f53e26SMarkus Armbruster {
10768f3a73bcSMax Reitz     if (blk_dev_has_tray(blk)) {
1077a7f53e26SMarkus Armbruster         return blk->dev_ops->is_tray_open(blk->dev_opaque);
1078a7f53e26SMarkus Armbruster     }
1079a7f53e26SMarkus Armbruster     return false;
1080a7f53e26SMarkus Armbruster }
1081a7f53e26SMarkus Armbruster 
1082a7f53e26SMarkus Armbruster /*
1083a7f53e26SMarkus Armbruster  * Does @blk's attached device model have the medium locked?
1084a7f53e26SMarkus Armbruster  * %false if the device model has no such lock.
1085a7f53e26SMarkus Armbruster  */
1086a7f53e26SMarkus Armbruster bool blk_dev_is_medium_locked(BlockBackend *blk)
1087a7f53e26SMarkus Armbruster {
1088a7f53e26SMarkus Armbruster     if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
1089a7f53e26SMarkus Armbruster         return blk->dev_ops->is_medium_locked(blk->dev_opaque);
1090a7f53e26SMarkus Armbruster     }
1091a7f53e26SMarkus Armbruster     return false;
1092a7f53e26SMarkus Armbruster }
1093a7f53e26SMarkus Armbruster 
1094a7f53e26SMarkus Armbruster /*
1095a7f53e26SMarkus Armbruster  * Notify @blk's attached device model of a backend size change.
1096a7f53e26SMarkus Armbruster  */
10975c8cab48SKevin Wolf static void blk_root_resize(BdrvChild *child)
1098a7f53e26SMarkus Armbruster {
10995c8cab48SKevin Wolf     BlockBackend *blk = child->opaque;
11005c8cab48SKevin Wolf 
1101a7f53e26SMarkus Armbruster     if (blk->dev_ops && blk->dev_ops->resize_cb) {
1102a7f53e26SMarkus Armbruster         blk->dev_ops->resize_cb(blk->dev_opaque);
1103a7f53e26SMarkus Armbruster     }
1104a7f53e26SMarkus Armbruster }
1105a7f53e26SMarkus Armbruster 
11064be74634SMarkus Armbruster void blk_iostatus_enable(BlockBackend *blk)
11074be74634SMarkus Armbruster {
1108373340b2SMax Reitz     blk->iostatus_enabled = true;
1109373340b2SMax Reitz     blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1110373340b2SMax Reitz }
1111373340b2SMax Reitz 
1112373340b2SMax Reitz /* The I/O status is only enabled if the drive explicitly
1113373340b2SMax Reitz  * enables it _and_ the VM is configured to stop on errors */
1114373340b2SMax Reitz bool blk_iostatus_is_enabled(const BlockBackend *blk)
1115373340b2SMax Reitz {
1116373340b2SMax Reitz     return (blk->iostatus_enabled &&
1117373340b2SMax Reitz            (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
1118373340b2SMax Reitz             blk->on_write_error == BLOCKDEV_ON_ERROR_STOP   ||
1119373340b2SMax Reitz             blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
1120373340b2SMax Reitz }
1121373340b2SMax Reitz 
1122373340b2SMax Reitz BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
1123373340b2SMax Reitz {
1124373340b2SMax Reitz     return blk->iostatus;
1125373340b2SMax Reitz }
1126373340b2SMax Reitz 
1127373340b2SMax Reitz void blk_iostatus_disable(BlockBackend *blk)
1128373340b2SMax Reitz {
1129373340b2SMax Reitz     blk->iostatus_enabled = false;
1130373340b2SMax Reitz }
1131373340b2SMax Reitz 
1132373340b2SMax Reitz void blk_iostatus_reset(BlockBackend *blk)
1133373340b2SMax Reitz {
1134373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
1135373340b2SMax Reitz         blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1136373340b2SMax Reitz     }
1137373340b2SMax Reitz }
1138373340b2SMax Reitz 
1139373340b2SMax Reitz void blk_iostatus_set_err(BlockBackend *blk, int error)
1140373340b2SMax Reitz {
1141373340b2SMax Reitz     assert(blk_iostatus_is_enabled(blk));
1142373340b2SMax Reitz     if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
1143373340b2SMax Reitz         blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
1144373340b2SMax Reitz                                           BLOCK_DEVICE_IO_STATUS_FAILED;
1145373340b2SMax Reitz     }
11464be74634SMarkus Armbruster }
11474be74634SMarkus Armbruster 
1148c10c9d96SKevin Wolf void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
1149c10c9d96SKevin Wolf {
1150c10c9d96SKevin Wolf     blk->allow_write_beyond_eof = allow;
1151c10c9d96SKevin Wolf }
1152c10c9d96SKevin Wolf 
1153980b0f94SKevin Wolf void blk_set_allow_aio_context_change(BlockBackend *blk, bool allow)
1154980b0f94SKevin Wolf {
1155980b0f94SKevin Wolf     blk->allow_aio_context_change = allow;
1156980b0f94SKevin Wolf }
1157980b0f94SKevin Wolf 
1158cf312932SKevin Wolf void blk_set_disable_request_queuing(BlockBackend *blk, bool disable)
1159cf312932SKevin Wolf {
1160cf312932SKevin Wolf     blk->disable_request_queuing = disable;
1161cf312932SKevin Wolf }
1162cf312932SKevin Wolf 
1163e7f7d676SMax Reitz static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
11647242db63SVladimir Sementsov-Ogievskiy                                   int64_t bytes)
1165e7f7d676SMax Reitz {
1166e7f7d676SMax Reitz     int64_t len;
1167e7f7d676SMax Reitz 
11687242db63SVladimir Sementsov-Ogievskiy     if (bytes < 0 || bytes > INT_MAX) {
1169e7f7d676SMax Reitz         return -EIO;
1170e7f7d676SMax Reitz     }
1171e7f7d676SMax Reitz 
1172c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1173e7f7d676SMax Reitz         return -ENOMEDIUM;
1174e7f7d676SMax Reitz     }
1175e7f7d676SMax Reitz 
1176c10c9d96SKevin Wolf     if (offset < 0) {
1177c10c9d96SKevin Wolf         return -EIO;
1178c10c9d96SKevin Wolf     }
1179c10c9d96SKevin Wolf 
1180c10c9d96SKevin Wolf     if (!blk->allow_write_beyond_eof) {
1181e7f7d676SMax Reitz         len = blk_getlength(blk);
1182e7f7d676SMax Reitz         if (len < 0) {
1183e7f7d676SMax Reitz             return len;
1184e7f7d676SMax Reitz         }
1185e7f7d676SMax Reitz 
11867242db63SVladimir Sementsov-Ogievskiy         if (offset > len || len - offset < bytes) {
1187e7f7d676SMax Reitz             return -EIO;
1188e7f7d676SMax Reitz         }
1189c10c9d96SKevin Wolf     }
1190e7f7d676SMax Reitz 
1191e7f7d676SMax Reitz     return 0;
1192e7f7d676SMax Reitz }
1193e7f7d676SMax Reitz 
11947f16476fSKevin Wolf /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1195cf312932SKevin Wolf static void coroutine_fn blk_wait_while_drained(BlockBackend *blk)
1196cf312932SKevin Wolf {
11977f16476fSKevin Wolf     assert(blk->in_flight > 0);
11987f16476fSKevin Wolf 
1199cf312932SKevin Wolf     if (blk->quiesce_counter && !blk->disable_request_queuing) {
12007f16476fSKevin Wolf         blk_dec_in_flight(blk);
1201cf312932SKevin Wolf         qemu_co_queue_wait(&blk->queued_requests, NULL);
12027f16476fSKevin Wolf         blk_inc_in_flight(blk);
1203cf312932SKevin Wolf     }
1204cf312932SKevin Wolf }
1205cf312932SKevin Wolf 
1206fbb92b67SKevin Wolf /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1207fbb92b67SKevin Wolf static int coroutine_fn
120895479077SVladimir Sementsov-Ogievskiy blk_do_preadv(BlockBackend *blk, int64_t offset, int64_t bytes,
1209fbb92b67SKevin Wolf               QEMUIOVector *qiov, BdrvRequestFlags flags)
12104be74634SMarkus Armbruster {
12111e98fefdSKevin Wolf     int ret;
1212cf312932SKevin Wolf     BlockDriverState *bs;
12131e98fefdSKevin Wolf 
1214cf312932SKevin Wolf     blk_wait_while_drained(blk);
1215cf312932SKevin Wolf 
1216cf312932SKevin Wolf     /* Call blk_bs() only after waiting, the graph may have changed */
1217cf312932SKevin Wolf     bs = blk_bs(blk);
121899723548SPaolo Bonzini     trace_blk_co_preadv(blk, bs, offset, bytes, flags);
12191e98fefdSKevin Wolf 
12201e98fefdSKevin Wolf     ret = blk_check_byte_request(blk, offset, bytes);
1221e7f7d676SMax Reitz     if (ret < 0) {
1222e7f7d676SMax Reitz         return ret;
1223e7f7d676SMax Reitz     }
1224e7f7d676SMax Reitz 
122599723548SPaolo Bonzini     bdrv_inc_in_flight(bs);
122699723548SPaolo Bonzini 
1227441565b2SKevin Wolf     /* throttling disk I/O */
1228022cdc9fSManos Pitsidianakis     if (blk->public.throttle_group_member.throttle_state) {
1229022cdc9fSManos Pitsidianakis         throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1230022cdc9fSManos Pitsidianakis                 bytes, false);
1231441565b2SKevin Wolf     }
1232441565b2SKevin Wolf 
123399723548SPaolo Bonzini     ret = bdrv_co_preadv(blk->root, offset, bytes, qiov, flags);
123499723548SPaolo Bonzini     bdrv_dec_in_flight(bs);
123599723548SPaolo Bonzini     return ret;
12361bf1cbc9SKevin Wolf }
12371bf1cbc9SKevin Wolf 
1238fbb92b67SKevin Wolf int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
123995479077SVladimir Sementsov-Ogievskiy                                int64_t bytes, QEMUIOVector *qiov,
1240fbb92b67SKevin Wolf                                BdrvRequestFlags flags)
1241fbb92b67SKevin Wolf {
1242fbb92b67SKevin Wolf     int ret;
1243fbb92b67SKevin Wolf 
1244fbb92b67SKevin Wolf     blk_inc_in_flight(blk);
1245fbb92b67SKevin Wolf     ret = blk_do_preadv(blk, offset, bytes, qiov, flags);
1246fbb92b67SKevin Wolf     blk_dec_in_flight(blk);
1247fbb92b67SKevin Wolf 
1248fbb92b67SKevin Wolf     return ret;
1249fbb92b67SKevin Wolf }
1250fbb92b67SKevin Wolf 
1251fbb92b67SKevin Wolf /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1252fbb92b67SKevin Wolf static int coroutine_fn
125334460febSVladimir Sementsov-Ogievskiy blk_do_pwritev_part(BlockBackend *blk, int64_t offset, int64_t bytes,
1254b3016864SVladimir Sementsov-Ogievskiy                     QEMUIOVector *qiov, size_t qiov_offset,
1255a8823a3bSKevin Wolf                     BdrvRequestFlags flags)
1256a8823a3bSKevin Wolf {
1257bfd18d1eSKevin Wolf     int ret;
1258cf312932SKevin Wolf     BlockDriverState *bs;
1259bfd18d1eSKevin Wolf 
1260cf312932SKevin Wolf     blk_wait_while_drained(blk);
1261cf312932SKevin Wolf 
1262cf312932SKevin Wolf     /* Call blk_bs() only after waiting, the graph may have changed */
1263cf312932SKevin Wolf     bs = blk_bs(blk);
126499723548SPaolo Bonzini     trace_blk_co_pwritev(blk, bs, offset, bytes, flags);
12651e98fefdSKevin Wolf 
1266bfd18d1eSKevin Wolf     ret = blk_check_byte_request(blk, offset, bytes);
1267a8823a3bSKevin Wolf     if (ret < 0) {
1268a8823a3bSKevin Wolf         return ret;
1269a8823a3bSKevin Wolf     }
1270a8823a3bSKevin Wolf 
127199723548SPaolo Bonzini     bdrv_inc_in_flight(bs);
1272441565b2SKevin Wolf     /* throttling disk I/O */
1273022cdc9fSManos Pitsidianakis     if (blk->public.throttle_group_member.throttle_state) {
1274022cdc9fSManos Pitsidianakis         throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1275022cdc9fSManos Pitsidianakis                 bytes, true);
1276441565b2SKevin Wolf     }
1277441565b2SKevin Wolf 
1278bfd18d1eSKevin Wolf     if (!blk->enable_write_cache) {
1279bfd18d1eSKevin Wolf         flags |= BDRV_REQ_FUA;
1280bfd18d1eSKevin Wolf     }
1281bfd18d1eSKevin Wolf 
1282b3016864SVladimir Sementsov-Ogievskiy     ret = bdrv_co_pwritev_part(blk->root, offset, bytes, qiov, qiov_offset,
1283b3016864SVladimir Sementsov-Ogievskiy                                flags);
128499723548SPaolo Bonzini     bdrv_dec_in_flight(bs);
128599723548SPaolo Bonzini     return ret;
1286a8823a3bSKevin Wolf }
1287a8823a3bSKevin Wolf 
1288fbb92b67SKevin Wolf int coroutine_fn blk_co_pwritev_part(BlockBackend *blk, int64_t offset,
128934460febSVladimir Sementsov-Ogievskiy                                      int64_t bytes,
1290fbb92b67SKevin Wolf                                      QEMUIOVector *qiov, size_t qiov_offset,
1291fbb92b67SKevin Wolf                                      BdrvRequestFlags flags)
1292fbb92b67SKevin Wolf {
1293fbb92b67SKevin Wolf     int ret;
1294fbb92b67SKevin Wolf 
1295fbb92b67SKevin Wolf     blk_inc_in_flight(blk);
1296fbb92b67SKevin Wolf     ret = blk_do_pwritev_part(blk, offset, bytes, qiov, qiov_offset, flags);
1297fbb92b67SKevin Wolf     blk_dec_in_flight(blk);
1298fbb92b67SKevin Wolf 
1299fbb92b67SKevin Wolf     return ret;
1300fbb92b67SKevin Wolf }
1301fbb92b67SKevin Wolf 
1302b3016864SVladimir Sementsov-Ogievskiy int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
130334460febSVladimir Sementsov-Ogievskiy                                 int64_t bytes, QEMUIOVector *qiov,
1304b3016864SVladimir Sementsov-Ogievskiy                                 BdrvRequestFlags flags)
1305b3016864SVladimir Sementsov-Ogievskiy {
1306b3016864SVladimir Sementsov-Ogievskiy     return blk_co_pwritev_part(blk, offset, bytes, qiov, 0, flags);
1307b3016864SVladimir Sementsov-Ogievskiy }
1308b3016864SVladimir Sementsov-Ogievskiy 
13091bf1cbc9SKevin Wolf typedef struct BlkRwCo {
13101bf1cbc9SKevin Wolf     BlockBackend *blk;
13111bf1cbc9SKevin Wolf     int64_t offset;
1312c060332cSDeepa Srinivasan     void *iobuf;
13131bf1cbc9SKevin Wolf     int ret;
13141bf1cbc9SKevin Wolf     BdrvRequestFlags flags;
13151bf1cbc9SKevin Wolf } BlkRwCo;
13161bf1cbc9SKevin Wolf 
13171bf1cbc9SKevin Wolf static void blk_read_entry(void *opaque)
13181bf1cbc9SKevin Wolf {
13191bf1cbc9SKevin Wolf     BlkRwCo *rwco = opaque;
1320c060332cSDeepa Srinivasan     QEMUIOVector *qiov = rwco->iobuf;
13211bf1cbc9SKevin Wolf 
1322fbb92b67SKevin Wolf     rwco->ret = blk_do_preadv(rwco->blk, rwco->offset, qiov->size,
1323c060332cSDeepa Srinivasan                               qiov, rwco->flags);
13244720cbeeSKevin Wolf     aio_wait_kick();
13251bf1cbc9SKevin Wolf }
13261bf1cbc9SKevin Wolf 
1327a8823a3bSKevin Wolf static void blk_write_entry(void *opaque)
1328a8823a3bSKevin Wolf {
1329a8823a3bSKevin Wolf     BlkRwCo *rwco = opaque;
1330c060332cSDeepa Srinivasan     QEMUIOVector *qiov = rwco->iobuf;
1331a8823a3bSKevin Wolf 
1332fbb92b67SKevin Wolf     rwco->ret = blk_do_pwritev_part(rwco->blk, rwco->offset, qiov->size,
1333fbb92b67SKevin Wolf                                     qiov, 0, rwco->flags);
13344720cbeeSKevin Wolf     aio_wait_kick();
1335a8823a3bSKevin Wolf }
1336a8823a3bSKevin Wolf 
1337a55d3fbaSKevin Wolf static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
1338a55d3fbaSKevin Wolf                    int64_t bytes, CoroutineEntry co_entry,
1339fc1453cdSKevin Wolf                    BdrvRequestFlags flags)
13401bf1cbc9SKevin Wolf {
1341ae5a9592SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
1342ae5a9592SVladimir Sementsov-Ogievskiy     BlkRwCo rwco = {
13431bf1cbc9SKevin Wolf         .blk    = blk,
1344a55d3fbaSKevin Wolf         .offset = offset,
1345c060332cSDeepa Srinivasan         .iobuf  = &qiov,
1346fc1453cdSKevin Wolf         .flags  = flags,
13471bf1cbc9SKevin Wolf         .ret    = NOT_DONE,
13481bf1cbc9SKevin Wolf     };
13491bf1cbc9SKevin Wolf 
1350fbb92b67SKevin Wolf     blk_inc_in_flight(blk);
135135f106e6SPaolo Bonzini     if (qemu_in_coroutine()) {
135235f106e6SPaolo Bonzini         /* Fast-path if already in coroutine context */
135335f106e6SPaolo Bonzini         co_entry(&rwco);
135435f106e6SPaolo Bonzini     } else {
135535f106e6SPaolo Bonzini         Coroutine *co = qemu_coroutine_create(co_entry, &rwco);
1356e92f0e19SFam Zheng         bdrv_coroutine_enter(blk_bs(blk), co);
135788b062c2SPaolo Bonzini         BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE);
135835f106e6SPaolo Bonzini     }
1359fbb92b67SKevin Wolf     blk_dec_in_flight(blk);
13601bf1cbc9SKevin Wolf 
13611bf1cbc9SKevin Wolf     return rwco.ret;
13624be74634SMarkus Armbruster }
13634be74634SMarkus Armbruster 
1364d004bd52SEric Blake int blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1365f5a5ca79SManos Pitsidianakis                       int bytes, BdrvRequestFlags flags)
13660df89e8eSKevin Wolf {
1367f5a5ca79SManos Pitsidianakis     return blk_prw(blk, offset, NULL, bytes, blk_write_entry,
136816aaf975SKevin Wolf                    flags | BDRV_REQ_ZERO_WRITE);
13690df89e8eSKevin Wolf }
13700df89e8eSKevin Wolf 
1371720ff280SKevin Wolf int blk_make_zero(BlockBackend *blk, BdrvRequestFlags flags)
1372720ff280SKevin Wolf {
1373720ff280SKevin Wolf     return bdrv_make_zero(blk->root, flags);
1374720ff280SKevin Wolf }
1375720ff280SKevin Wolf 
1376c90e2a9cSKevin Wolf void blk_inc_in_flight(BlockBackend *blk)
137733f2a757SStefan Hajnoczi {
1378d73415a3SStefan Hajnoczi     qatomic_inc(&blk->in_flight);
137933f2a757SStefan Hajnoczi }
138033f2a757SStefan Hajnoczi 
1381c90e2a9cSKevin Wolf void blk_dec_in_flight(BlockBackend *blk)
138233f2a757SStefan Hajnoczi {
1383d73415a3SStefan Hajnoczi     qatomic_dec(&blk->in_flight);
1384cfe29d82SKevin Wolf     aio_wait_kick();
138533f2a757SStefan Hajnoczi }
138633f2a757SStefan Hajnoczi 
1387e7f7d676SMax Reitz static void error_callback_bh(void *opaque)
1388e7f7d676SMax Reitz {
1389e7f7d676SMax Reitz     struct BlockBackendAIOCB *acb = opaque;
139099723548SPaolo Bonzini 
139133f2a757SStefan Hajnoczi     blk_dec_in_flight(acb->blk);
1392e7f7d676SMax Reitz     acb->common.cb(acb->common.opaque, acb->ret);
1393e7f7d676SMax Reitz     qemu_aio_unref(acb);
1394e7f7d676SMax Reitz }
1395e7f7d676SMax Reitz 
1396ca78ecfaSPeter Lieven BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
1397ca78ecfaSPeter Lieven                                   BlockCompletionFunc *cb,
1398e7f7d676SMax Reitz                                   void *opaque, int ret)
1399e7f7d676SMax Reitz {
1400e7f7d676SMax Reitz     struct BlockBackendAIOCB *acb;
1401e7f7d676SMax Reitz 
140233f2a757SStefan Hajnoczi     blk_inc_in_flight(blk);
1403e7f7d676SMax Reitz     acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
14044981bdecSMax Reitz     acb->blk = blk;
1405e7f7d676SMax Reitz     acb->ret = ret;
1406e7f7d676SMax Reitz 
1407e4ec5ad4SPavel Dovgalyuk     replay_bh_schedule_oneshot_event(blk_get_aio_context(blk),
1408e4ec5ad4SPavel Dovgalyuk                                      error_callback_bh, acb);
1409e7f7d676SMax Reitz     return &acb->common;
1410e7f7d676SMax Reitz }
1411e7f7d676SMax Reitz 
141257d6a428SKevin Wolf typedef struct BlkAioEmAIOCB {
141357d6a428SKevin Wolf     BlockAIOCB common;
141457d6a428SKevin Wolf     BlkRwCo rwco;
14157fa84cd8SKevin Wolf     int bytes;
141657d6a428SKevin Wolf     bool has_returned;
141757d6a428SKevin Wolf } BlkAioEmAIOCB;
141857d6a428SKevin Wolf 
14191d719ddcSStefan Hajnoczi static AioContext *blk_aio_em_aiocb_get_aio_context(BlockAIOCB *acb_)
14201d719ddcSStefan Hajnoczi {
14211d719ddcSStefan Hajnoczi     BlkAioEmAIOCB *acb = container_of(acb_, BlkAioEmAIOCB, common);
14221d719ddcSStefan Hajnoczi 
14231d719ddcSStefan Hajnoczi     return blk_get_aio_context(acb->rwco.blk);
14241d719ddcSStefan Hajnoczi }
14251d719ddcSStefan Hajnoczi 
142657d6a428SKevin Wolf static const AIOCBInfo blk_aio_em_aiocb_info = {
142757d6a428SKevin Wolf     .aiocb_size         = sizeof(BlkAioEmAIOCB),
14281d719ddcSStefan Hajnoczi     .get_aio_context    = blk_aio_em_aiocb_get_aio_context,
142957d6a428SKevin Wolf };
143057d6a428SKevin Wolf 
143157d6a428SKevin Wolf static void blk_aio_complete(BlkAioEmAIOCB *acb)
143257d6a428SKevin Wolf {
143357d6a428SKevin Wolf     if (acb->has_returned) {
143457d6a428SKevin Wolf         acb->common.cb(acb->common.opaque, acb->rwco.ret);
143546aaf2a5SKevin Wolf         blk_dec_in_flight(acb->rwco.blk);
143657d6a428SKevin Wolf         qemu_aio_unref(acb);
143757d6a428SKevin Wolf     }
143857d6a428SKevin Wolf }
143957d6a428SKevin Wolf 
144057d6a428SKevin Wolf static void blk_aio_complete_bh(void *opaque)
144157d6a428SKevin Wolf {
1442fffb6e12SPaolo Bonzini     BlkAioEmAIOCB *acb = opaque;
1443fffb6e12SPaolo Bonzini     assert(acb->has_returned);
1444fffb6e12SPaolo Bonzini     blk_aio_complete(acb);
144557d6a428SKevin Wolf }
144657d6a428SKevin Wolf 
14477fa84cd8SKevin Wolf static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
1448c060332cSDeepa Srinivasan                                 void *iobuf, CoroutineEntry co_entry,
144957d6a428SKevin Wolf                                 BdrvRequestFlags flags,
145057d6a428SKevin Wolf                                 BlockCompletionFunc *cb, void *opaque)
145157d6a428SKevin Wolf {
145257d6a428SKevin Wolf     BlkAioEmAIOCB *acb;
145357d6a428SKevin Wolf     Coroutine *co;
145457d6a428SKevin Wolf 
145533f2a757SStefan Hajnoczi     blk_inc_in_flight(blk);
145657d6a428SKevin Wolf     acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
145757d6a428SKevin Wolf     acb->rwco = (BlkRwCo) {
145857d6a428SKevin Wolf         .blk    = blk,
145957d6a428SKevin Wolf         .offset = offset,
1460c060332cSDeepa Srinivasan         .iobuf  = iobuf,
146157d6a428SKevin Wolf         .flags  = flags,
146257d6a428SKevin Wolf         .ret    = NOT_DONE,
146357d6a428SKevin Wolf     };
14647fa84cd8SKevin Wolf     acb->bytes = bytes;
146557d6a428SKevin Wolf     acb->has_returned = false;
146657d6a428SKevin Wolf 
14670b8b8753SPaolo Bonzini     co = qemu_coroutine_create(co_entry, acb);
1468e92f0e19SFam Zheng     bdrv_coroutine_enter(blk_bs(blk), co);
146957d6a428SKevin Wolf 
147057d6a428SKevin Wolf     acb->has_returned = true;
147157d6a428SKevin Wolf     if (acb->rwco.ret != NOT_DONE) {
1472e4ec5ad4SPavel Dovgalyuk         replay_bh_schedule_oneshot_event(blk_get_aio_context(blk),
1473fffb6e12SPaolo Bonzini                                          blk_aio_complete_bh, acb);
147457d6a428SKevin Wolf     }
147557d6a428SKevin Wolf 
147657d6a428SKevin Wolf     return &acb->common;
147757d6a428SKevin Wolf }
147857d6a428SKevin Wolf 
147957d6a428SKevin Wolf static void blk_aio_read_entry(void *opaque)
148057d6a428SKevin Wolf {
148157d6a428SKevin Wolf     BlkAioEmAIOCB *acb = opaque;
148257d6a428SKevin Wolf     BlkRwCo *rwco = &acb->rwco;
1483c060332cSDeepa Srinivasan     QEMUIOVector *qiov = rwco->iobuf;
148457d6a428SKevin Wolf 
1485c060332cSDeepa Srinivasan     assert(qiov->size == acb->bytes);
1486fbb92b67SKevin Wolf     rwco->ret = blk_do_preadv(rwco->blk, rwco->offset, acb->bytes,
1487c060332cSDeepa Srinivasan                               qiov, rwco->flags);
148857d6a428SKevin Wolf     blk_aio_complete(acb);
148957d6a428SKevin Wolf }
149057d6a428SKevin Wolf 
149157d6a428SKevin Wolf static void blk_aio_write_entry(void *opaque)
149257d6a428SKevin Wolf {
149357d6a428SKevin Wolf     BlkAioEmAIOCB *acb = opaque;
149457d6a428SKevin Wolf     BlkRwCo *rwco = &acb->rwco;
1495c060332cSDeepa Srinivasan     QEMUIOVector *qiov = rwco->iobuf;
149657d6a428SKevin Wolf 
1497c060332cSDeepa Srinivasan     assert(!qiov || qiov->size == acb->bytes);
1498fbb92b67SKevin Wolf     rwco->ret = blk_do_pwritev_part(rwco->blk, rwco->offset, acb->bytes,
1499fbb92b67SKevin Wolf                                     qiov, 0, rwco->flags);
150057d6a428SKevin Wolf     blk_aio_complete(acb);
150157d6a428SKevin Wolf }
150257d6a428SKevin Wolf 
1503d004bd52SEric Blake BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1504983a1600SEric Blake                                   int count, BdrvRequestFlags flags,
15054be74634SMarkus Armbruster                                   BlockCompletionFunc *cb, void *opaque)
15064be74634SMarkus Armbruster {
1507983a1600SEric Blake     return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
1508983a1600SEric Blake                         flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
15094be74634SMarkus Armbruster }
15104be74634SMarkus Armbruster 
15114be74634SMarkus Armbruster int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
15124be74634SMarkus Armbruster {
1513a55d3fbaSKevin Wolf     int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
1514e7f7d676SMax Reitz     if (ret < 0) {
1515e7f7d676SMax Reitz         return ret;
1516e7f7d676SMax Reitz     }
1517a55d3fbaSKevin Wolf     return count;
15184be74634SMarkus Armbruster }
15194be74634SMarkus Armbruster 
15208341f00dSEric Blake int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
15218341f00dSEric Blake                BdrvRequestFlags flags)
15224be74634SMarkus Armbruster {
15238341f00dSEric Blake     int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
15248341f00dSEric Blake                       flags);
1525e7f7d676SMax Reitz     if (ret < 0) {
1526e7f7d676SMax Reitz         return ret;
1527e7f7d676SMax Reitz     }
1528a55d3fbaSKevin Wolf     return count;
15294be74634SMarkus Armbruster }
15304be74634SMarkus Armbruster 
15314be74634SMarkus Armbruster int64_t blk_getlength(BlockBackend *blk)
15324be74634SMarkus Armbruster {
1533c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1534c09ba36cSMax Reitz         return -ENOMEDIUM;
1535c09ba36cSMax Reitz     }
1536c09ba36cSMax Reitz 
1537f21d96d0SKevin Wolf     return bdrv_getlength(blk_bs(blk));
15384be74634SMarkus Armbruster }
15394be74634SMarkus Armbruster 
15404be74634SMarkus Armbruster void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
15414be74634SMarkus Armbruster {
1542f21d96d0SKevin Wolf     if (!blk_bs(blk)) {
1543a46fc9c9SMax Reitz         *nb_sectors_ptr = 0;
1544a46fc9c9SMax Reitz     } else {
1545f21d96d0SKevin Wolf         bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
15464be74634SMarkus Armbruster     }
1547a46fc9c9SMax Reitz }
15484be74634SMarkus Armbruster 
15491ef01253SMax Reitz int64_t blk_nb_sectors(BlockBackend *blk)
15501ef01253SMax Reitz {
1551c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1552c09ba36cSMax Reitz         return -ENOMEDIUM;
1553c09ba36cSMax Reitz     }
1554c09ba36cSMax Reitz 
1555f21d96d0SKevin Wolf     return bdrv_nb_sectors(blk_bs(blk));
15561ef01253SMax Reitz }
15571ef01253SMax Reitz 
155860cb2fa7SEric Blake BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
155960cb2fa7SEric Blake                            QEMUIOVector *qiov, BdrvRequestFlags flags,
156060cb2fa7SEric Blake                            BlockCompletionFunc *cb, void *opaque)
156160cb2fa7SEric Blake {
156260cb2fa7SEric Blake     return blk_aio_prwv(blk, offset, qiov->size, qiov,
156360cb2fa7SEric Blake                         blk_aio_read_entry, flags, cb, opaque);
156460cb2fa7SEric Blake }
156560cb2fa7SEric Blake 
156660cb2fa7SEric Blake BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
156760cb2fa7SEric Blake                             QEMUIOVector *qiov, BdrvRequestFlags flags,
156860cb2fa7SEric Blake                             BlockCompletionFunc *cb, void *opaque)
156960cb2fa7SEric Blake {
157060cb2fa7SEric Blake     return blk_aio_prwv(blk, offset, qiov->size, qiov,
157160cb2fa7SEric Blake                         blk_aio_write_entry, flags, cb, opaque);
157260cb2fa7SEric Blake }
157360cb2fa7SEric Blake 
15744be74634SMarkus Armbruster void blk_aio_cancel(BlockAIOCB *acb)
15754be74634SMarkus Armbruster {
15764be74634SMarkus Armbruster     bdrv_aio_cancel(acb);
15774be74634SMarkus Armbruster }
15784be74634SMarkus Armbruster 
15794be74634SMarkus Armbruster void blk_aio_cancel_async(BlockAIOCB *acb)
15804be74634SMarkus Armbruster {
15814be74634SMarkus Armbruster     bdrv_aio_cancel_async(acb);
15824be74634SMarkus Armbruster }
15834be74634SMarkus Armbruster 
1584fbb92b67SKevin Wolf /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1585fbb92b67SKevin Wolf static int coroutine_fn
1586fbb92b67SKevin Wolf blk_do_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
15874be74634SMarkus Armbruster {
1588cf312932SKevin Wolf     blk_wait_while_drained(blk);
1589cf312932SKevin Wolf 
1590c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1591c09ba36cSMax Reitz         return -ENOMEDIUM;
1592c09ba36cSMax Reitz     }
1593c09ba36cSMax Reitz 
159448af776aSKevin Wolf     return bdrv_co_ioctl(blk_bs(blk), req, buf);
159548af776aSKevin Wolf }
159648af776aSKevin Wolf 
159748af776aSKevin Wolf static void blk_ioctl_entry(void *opaque)
159848af776aSKevin Wolf {
159948af776aSKevin Wolf     BlkRwCo *rwco = opaque;
1600c060332cSDeepa Srinivasan     QEMUIOVector *qiov = rwco->iobuf;
1601c060332cSDeepa Srinivasan 
1602fbb92b67SKevin Wolf     rwco->ret = blk_do_ioctl(rwco->blk, rwco->offset, qiov->iov[0].iov_base);
16034720cbeeSKevin Wolf     aio_wait_kick();
160448af776aSKevin Wolf }
160548af776aSKevin Wolf 
160648af776aSKevin Wolf int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
160748af776aSKevin Wolf {
160848af776aSKevin Wolf     return blk_prw(blk, req, buf, 0, blk_ioctl_entry, 0);
160948af776aSKevin Wolf }
161048af776aSKevin Wolf 
161148af776aSKevin Wolf static void blk_aio_ioctl_entry(void *opaque)
161248af776aSKevin Wolf {
161348af776aSKevin Wolf     BlkAioEmAIOCB *acb = opaque;
161448af776aSKevin Wolf     BlkRwCo *rwco = &acb->rwco;
161548af776aSKevin Wolf 
1616fbb92b67SKevin Wolf     rwco->ret = blk_do_ioctl(rwco->blk, rwco->offset, rwco->iobuf);
1617c060332cSDeepa Srinivasan 
161848af776aSKevin Wolf     blk_aio_complete(acb);
16194be74634SMarkus Armbruster }
16204be74634SMarkus Armbruster 
16214be74634SMarkus Armbruster BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
16224be74634SMarkus Armbruster                           BlockCompletionFunc *cb, void *opaque)
16234be74634SMarkus Armbruster {
1624c060332cSDeepa Srinivasan     return blk_aio_prwv(blk, req, 0, buf, blk_aio_ioctl_entry, 0, cb, opaque);
16254be74634SMarkus Armbruster }
16264be74634SMarkus Armbruster 
1627fbb92b67SKevin Wolf /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1628fbb92b67SKevin Wolf static int coroutine_fn
1629*2800637aSVladimir Sementsov-Ogievskiy blk_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes)
16302bb0dce7SMax Reitz {
1631cf312932SKevin Wolf     int ret;
1632cf312932SKevin Wolf 
1633cf312932SKevin Wolf     blk_wait_while_drained(blk);
1634cf312932SKevin Wolf 
1635cf312932SKevin Wolf     ret = blk_check_byte_request(blk, offset, bytes);
1636e7f7d676SMax Reitz     if (ret < 0) {
1637e7f7d676SMax Reitz         return ret;
1638e7f7d676SMax Reitz     }
1639e7f7d676SMax Reitz 
16400b9fd3f4SFam Zheng     return bdrv_co_pdiscard(blk->root, offset, bytes);
16412bb0dce7SMax Reitz }
16422bb0dce7SMax Reitz 
1643564806c5SKevin Wolf static void blk_aio_pdiscard_entry(void *opaque)
1644564806c5SKevin Wolf {
1645564806c5SKevin Wolf     BlkAioEmAIOCB *acb = opaque;
1646564806c5SKevin Wolf     BlkRwCo *rwco = &acb->rwco;
1647564806c5SKevin Wolf 
1648fbb92b67SKevin Wolf     rwco->ret = blk_do_pdiscard(rwco->blk, rwco->offset, acb->bytes);
1649564806c5SKevin Wolf     blk_aio_complete(acb);
1650564806c5SKevin Wolf }
1651564806c5SKevin Wolf 
1652564806c5SKevin Wolf BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk,
1653564806c5SKevin Wolf                              int64_t offset, int bytes,
1654564806c5SKevin Wolf                              BlockCompletionFunc *cb, void *opaque)
1655564806c5SKevin Wolf {
1656564806c5SKevin Wolf     return blk_aio_prwv(blk, offset, bytes, NULL, blk_aio_pdiscard_entry, 0,
1657564806c5SKevin Wolf                         cb, opaque);
1658564806c5SKevin Wolf }
1659564806c5SKevin Wolf 
1660*2800637aSVladimir Sementsov-Ogievskiy int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset,
1661*2800637aSVladimir Sementsov-Ogievskiy                                  int64_t bytes)
1662fbb92b67SKevin Wolf {
1663fbb92b67SKevin Wolf     int ret;
1664fbb92b67SKevin Wolf 
1665fbb92b67SKevin Wolf     blk_inc_in_flight(blk);
1666fbb92b67SKevin Wolf     ret = blk_do_pdiscard(blk, offset, bytes);
1667fbb92b67SKevin Wolf     blk_dec_in_flight(blk);
1668fbb92b67SKevin Wolf 
1669fbb92b67SKevin Wolf     return ret;
1670fbb92b67SKevin Wolf }
1671fbb92b67SKevin Wolf 
1672564806c5SKevin Wolf static void blk_pdiscard_entry(void *opaque)
1673564806c5SKevin Wolf {
1674564806c5SKevin Wolf     BlkRwCo *rwco = opaque;
1675564806c5SKevin Wolf     QEMUIOVector *qiov = rwco->iobuf;
1676564806c5SKevin Wolf 
1677fbb92b67SKevin Wolf     rwco->ret = blk_do_pdiscard(rwco->blk, rwco->offset, qiov->size);
1678564806c5SKevin Wolf     aio_wait_kick();
1679564806c5SKevin Wolf }
1680564806c5SKevin Wolf 
1681564806c5SKevin Wolf int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1682564806c5SKevin Wolf {
1683564806c5SKevin Wolf     return blk_prw(blk, offset, NULL, bytes, blk_pdiscard_entry, 0);
1684564806c5SKevin Wolf }
1685564806c5SKevin Wolf 
1686fbb92b67SKevin Wolf /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1687fbb92b67SKevin Wolf static int coroutine_fn blk_do_flush(BlockBackend *blk)
16882bb0dce7SMax Reitz {
1689cf312932SKevin Wolf     blk_wait_while_drained(blk);
1690cf312932SKevin Wolf 
1691c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
1692c09ba36cSMax Reitz         return -ENOMEDIUM;
1693c09ba36cSMax Reitz     }
1694c09ba36cSMax Reitz 
1695f21d96d0SKevin Wolf     return bdrv_co_flush(blk_bs(blk));
16962bb0dce7SMax Reitz }
16972bb0dce7SMax Reitz 
1698564806c5SKevin Wolf static void blk_aio_flush_entry(void *opaque)
1699564806c5SKevin Wolf {
1700564806c5SKevin Wolf     BlkAioEmAIOCB *acb = opaque;
1701564806c5SKevin Wolf     BlkRwCo *rwco = &acb->rwco;
1702564806c5SKevin Wolf 
1703fbb92b67SKevin Wolf     rwco->ret = blk_do_flush(rwco->blk);
1704564806c5SKevin Wolf     blk_aio_complete(acb);
1705564806c5SKevin Wolf }
1706564806c5SKevin Wolf 
1707564806c5SKevin Wolf BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1708564806c5SKevin Wolf                           BlockCompletionFunc *cb, void *opaque)
1709564806c5SKevin Wolf {
1710564806c5SKevin Wolf     return blk_aio_prwv(blk, 0, 0, NULL, blk_aio_flush_entry, 0, cb, opaque);
1711564806c5SKevin Wolf }
1712564806c5SKevin Wolf 
1713fbb92b67SKevin Wolf int coroutine_fn blk_co_flush(BlockBackend *blk)
1714fbb92b67SKevin Wolf {
1715fbb92b67SKevin Wolf     int ret;
1716fbb92b67SKevin Wolf 
1717fbb92b67SKevin Wolf     blk_inc_in_flight(blk);
1718fbb92b67SKevin Wolf     ret = blk_do_flush(blk);
1719fbb92b67SKevin Wolf     blk_dec_in_flight(blk);
1720fbb92b67SKevin Wolf 
1721fbb92b67SKevin Wolf     return ret;
1722fbb92b67SKevin Wolf }
1723fbb92b67SKevin Wolf 
1724be07a889SKevin Wolf static void blk_flush_entry(void *opaque)
17254be74634SMarkus Armbruster {
1726be07a889SKevin Wolf     BlkRwCo *rwco = opaque;
1727fbb92b67SKevin Wolf     rwco->ret = blk_do_flush(rwco->blk);
17284720cbeeSKevin Wolf     aio_wait_kick();
1729c09ba36cSMax Reitz }
1730c09ba36cSMax Reitz 
1731be07a889SKevin Wolf int blk_flush(BlockBackend *blk)
1732be07a889SKevin Wolf {
1733be07a889SKevin Wolf     return blk_prw(blk, 0, NULL, 0, blk_flush_entry, 0);
17344be74634SMarkus Armbruster }
17354be74634SMarkus Armbruster 
173697b0385aSAlexander Yarygin void blk_drain(BlockBackend *blk)
173797b0385aSAlexander Yarygin {
173833f2a757SStefan Hajnoczi     BlockDriverState *bs = blk_bs(blk);
173933f2a757SStefan Hajnoczi 
174033f2a757SStefan Hajnoczi     if (bs) {
174133f2a757SStefan Hajnoczi         bdrv_drained_begin(bs);
174233f2a757SStefan Hajnoczi     }
174333f2a757SStefan Hajnoczi 
174433f2a757SStefan Hajnoczi     /* We may have -ENOMEDIUM completions in flight */
1745cfe29d82SKevin Wolf     AIO_WAIT_WHILE(blk_get_aio_context(blk),
1746d73415a3SStefan Hajnoczi                    qatomic_mb_read(&blk->in_flight) > 0);
174733f2a757SStefan Hajnoczi 
174833f2a757SStefan Hajnoczi     if (bs) {
174933f2a757SStefan Hajnoczi         bdrv_drained_end(bs);
175097b0385aSAlexander Yarygin     }
1751a46fc9c9SMax Reitz }
175297b0385aSAlexander Yarygin 
17534be74634SMarkus Armbruster void blk_drain_all(void)
17544be74634SMarkus Armbruster {
175533f2a757SStefan Hajnoczi     BlockBackend *blk = NULL;
175633f2a757SStefan Hajnoczi 
175733f2a757SStefan Hajnoczi     bdrv_drain_all_begin();
175833f2a757SStefan Hajnoczi 
175933f2a757SStefan Hajnoczi     while ((blk = blk_all_next(blk)) != NULL) {
176033f2a757SStefan Hajnoczi         AioContext *ctx = blk_get_aio_context(blk);
176133f2a757SStefan Hajnoczi 
176233f2a757SStefan Hajnoczi         aio_context_acquire(ctx);
176333f2a757SStefan Hajnoczi 
176433f2a757SStefan Hajnoczi         /* We may have -ENOMEDIUM completions in flight */
1765d73415a3SStefan Hajnoczi         AIO_WAIT_WHILE(ctx, qatomic_mb_read(&blk->in_flight) > 0);
176633f2a757SStefan Hajnoczi 
176733f2a757SStefan Hajnoczi         aio_context_release(ctx);
176833f2a757SStefan Hajnoczi     }
176933f2a757SStefan Hajnoczi 
177033f2a757SStefan Hajnoczi     bdrv_drain_all_end();
17714be74634SMarkus Armbruster }
17724be74634SMarkus Armbruster 
1773373340b2SMax Reitz void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1774373340b2SMax Reitz                       BlockdevOnError on_write_error)
1775373340b2SMax Reitz {
1776373340b2SMax Reitz     blk->on_read_error = on_read_error;
1777373340b2SMax Reitz     blk->on_write_error = on_write_error;
1778373340b2SMax Reitz }
1779373340b2SMax Reitz 
17804be74634SMarkus Armbruster BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
17814be74634SMarkus Armbruster {
1782373340b2SMax Reitz     return is_read ? blk->on_read_error : blk->on_write_error;
17834be74634SMarkus Armbruster }
17844be74634SMarkus Armbruster 
17854be74634SMarkus Armbruster BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
17864be74634SMarkus Armbruster                                       int error)
17874be74634SMarkus Armbruster {
1788373340b2SMax Reitz     BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1789373340b2SMax Reitz 
1790373340b2SMax Reitz     switch (on_err) {
1791373340b2SMax Reitz     case BLOCKDEV_ON_ERROR_ENOSPC:
1792373340b2SMax Reitz         return (error == ENOSPC) ?
1793373340b2SMax Reitz                BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1794373340b2SMax Reitz     case BLOCKDEV_ON_ERROR_STOP:
1795373340b2SMax Reitz         return BLOCK_ERROR_ACTION_STOP;
1796373340b2SMax Reitz     case BLOCKDEV_ON_ERROR_REPORT:
1797373340b2SMax Reitz         return BLOCK_ERROR_ACTION_REPORT;
1798373340b2SMax Reitz     case BLOCKDEV_ON_ERROR_IGNORE:
1799373340b2SMax Reitz         return BLOCK_ERROR_ACTION_IGNORE;
18008c398252SKevin Wolf     case BLOCKDEV_ON_ERROR_AUTO:
1801373340b2SMax Reitz     default:
1802373340b2SMax Reitz         abort();
1803373340b2SMax Reitz     }
18044be74634SMarkus Armbruster }
18054be74634SMarkus Armbruster 
1806373340b2SMax Reitz static void send_qmp_error_event(BlockBackend *blk,
1807373340b2SMax Reitz                                  BlockErrorAction action,
1808373340b2SMax Reitz                                  bool is_read, int error)
1809373340b2SMax Reitz {
1810373340b2SMax Reitz     IoOperationType optype;
1811bfe1a14cSKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1812373340b2SMax Reitz 
1813373340b2SMax Reitz     optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
1814bfe1a14cSKevin Wolf     qapi_event_send_block_io_error(blk_name(blk), !!bs,
1815bfe1a14cSKevin Wolf                                    bs ? bdrv_get_node_name(bs) : NULL, optype,
18162bf7e10fSKevin Wolf                                    action, blk_iostatus_is_enabled(blk),
18173ab72385SPeter Xu                                    error == ENOSPC, strerror(error));
1818373340b2SMax Reitz }
1819373340b2SMax Reitz 
1820373340b2SMax Reitz /* This is done by device models because, while the block layer knows
1821373340b2SMax Reitz  * about the error, it does not know whether an operation comes from
1822373340b2SMax Reitz  * the device or the block layer (from a job, for example).
1823373340b2SMax Reitz  */
18244be74634SMarkus Armbruster void blk_error_action(BlockBackend *blk, BlockErrorAction action,
18254be74634SMarkus Armbruster                       bool is_read, int error)
18264be74634SMarkus Armbruster {
1827373340b2SMax Reitz     assert(error >= 0);
1828373340b2SMax Reitz 
1829373340b2SMax Reitz     if (action == BLOCK_ERROR_ACTION_STOP) {
1830373340b2SMax Reitz         /* First set the iostatus, so that "info block" returns an iostatus
1831373340b2SMax Reitz          * that matches the events raised so far (an additional error iostatus
1832373340b2SMax Reitz          * is fine, but not a lost one).
1833373340b2SMax Reitz          */
1834373340b2SMax Reitz         blk_iostatus_set_err(blk, error);
1835373340b2SMax Reitz 
1836373340b2SMax Reitz         /* Then raise the request to stop the VM and the event.
1837373340b2SMax Reitz          * qemu_system_vmstop_request_prepare has two effects.  First,
1838373340b2SMax Reitz          * it ensures that the STOP event always comes after the
1839373340b2SMax Reitz          * BLOCK_IO_ERROR event.  Second, it ensures that even if management
1840373340b2SMax Reitz          * can observe the STOP event and do a "cont" before the STOP
1841373340b2SMax Reitz          * event is issued, the VM will not stop.  In this case, vm_start()
1842373340b2SMax Reitz          * also ensures that the STOP/RESUME pair of events is emitted.
1843373340b2SMax Reitz          */
1844373340b2SMax Reitz         qemu_system_vmstop_request_prepare();
1845373340b2SMax Reitz         send_qmp_error_event(blk, action, is_read, error);
1846373340b2SMax Reitz         qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1847373340b2SMax Reitz     } else {
1848373340b2SMax Reitz         send_qmp_error_event(blk, action, is_read, error);
1849373340b2SMax Reitz     }
18504be74634SMarkus Armbruster }
18514be74634SMarkus Armbruster 
185286b1cf32SKevin Wolf /*
185386b1cf32SKevin Wolf  * Returns true if the BlockBackend can support taking write permissions
185486b1cf32SKevin Wolf  * (because its root node is not read-only).
185586b1cf32SKevin Wolf  */
185686b1cf32SKevin Wolf bool blk_supports_write_perm(BlockBackend *blk)
18574be74634SMarkus Armbruster {
1858f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1859f21d96d0SKevin Wolf 
1860f21d96d0SKevin Wolf     if (bs) {
186186b1cf32SKevin Wolf         return !bdrv_is_read_only(bs);
1862061959e8SMax Reitz     } else {
1863260242a8SVladimir Sementsov-Ogievskiy         return blk->root_state.open_flags & BDRV_O_RDWR;
1864061959e8SMax Reitz     }
18654be74634SMarkus Armbruster }
18664be74634SMarkus Armbruster 
186786b1cf32SKevin Wolf /*
186886b1cf32SKevin Wolf  * Returns true if the BlockBackend can be written to in its current
186986b1cf32SKevin Wolf  * configuration (i.e. if write permission have been requested)
187086b1cf32SKevin Wolf  */
187186b1cf32SKevin Wolf bool blk_is_writable(BlockBackend *blk)
187286b1cf32SKevin Wolf {
187386b1cf32SKevin Wolf     return blk->perm & BLK_PERM_WRITE;
187486b1cf32SKevin Wolf }
187586b1cf32SKevin Wolf 
187696710565SLi Qiang bool blk_is_sg(BlockBackend *blk)
18774be74634SMarkus Armbruster {
1878f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1879f21d96d0SKevin Wolf 
1880f21d96d0SKevin Wolf     if (!bs) {
188196710565SLi Qiang         return false;
1882a46fc9c9SMax Reitz     }
1883a46fc9c9SMax Reitz 
1884f21d96d0SKevin Wolf     return bdrv_is_sg(bs);
18854be74634SMarkus Armbruster }
18864be74634SMarkus Armbruster 
188796710565SLi Qiang bool blk_enable_write_cache(BlockBackend *blk)
18884be74634SMarkus Armbruster {
1889bfd18d1eSKevin Wolf     return blk->enable_write_cache;
18904be74634SMarkus Armbruster }
18914be74634SMarkus Armbruster 
18924be74634SMarkus Armbruster void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
18934be74634SMarkus Armbruster {
1894bfd18d1eSKevin Wolf     blk->enable_write_cache = wce;
18954be74634SMarkus Armbruster }
18964be74634SMarkus Armbruster 
18972bb0dce7SMax Reitz void blk_invalidate_cache(BlockBackend *blk, Error **errp)
18982bb0dce7SMax Reitz {
1899f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1900f21d96d0SKevin Wolf 
1901f21d96d0SKevin Wolf     if (!bs) {
1902c09ba36cSMax Reitz         error_setg(errp, "Device '%s' has no medium", blk->name);
1903c09ba36cSMax Reitz         return;
1904c09ba36cSMax Reitz     }
1905c09ba36cSMax Reitz 
1906f21d96d0SKevin Wolf     bdrv_invalidate_cache(bs, errp);
19072bb0dce7SMax Reitz }
19082bb0dce7SMax Reitz 
1909e031f750SMax Reitz bool blk_is_inserted(BlockBackend *blk)
19104be74634SMarkus Armbruster {
1911f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1912f21d96d0SKevin Wolf 
1913f21d96d0SKevin Wolf     return bs && bdrv_is_inserted(bs);
1914db0284f8SMax Reitz }
1915db0284f8SMax Reitz 
1916db0284f8SMax Reitz bool blk_is_available(BlockBackend *blk)
1917db0284f8SMax Reitz {
1918db0284f8SMax Reitz     return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
19194be74634SMarkus Armbruster }
19204be74634SMarkus Armbruster 
19214be74634SMarkus Armbruster void blk_lock_medium(BlockBackend *blk, bool locked)
19224be74634SMarkus Armbruster {
1923f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1924f21d96d0SKevin Wolf 
1925f21d96d0SKevin Wolf     if (bs) {
1926f21d96d0SKevin Wolf         bdrv_lock_medium(bs, locked);
19274be74634SMarkus Armbruster     }
1928a46fc9c9SMax Reitz }
19294be74634SMarkus Armbruster 
19304be74634SMarkus Armbruster void blk_eject(BlockBackend *blk, bool eject_flag)
19314be74634SMarkus Armbruster {
1932f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
19332d76e724SKevin Wolf     char *id;
19342d76e724SKevin Wolf 
1935f21d96d0SKevin Wolf     if (bs) {
1936f21d96d0SKevin Wolf         bdrv_eject(bs, eject_flag);
1937c47ee043SJohn Snow     }
19382d76e724SKevin Wolf 
1939c47ee043SJohn Snow     /* Whether or not we ejected on the backend,
1940c47ee043SJohn Snow      * the frontend experienced a tray event. */
19412d76e724SKevin Wolf     id = blk_get_attached_dev_id(blk);
19422d76e724SKevin Wolf     qapi_event_send_device_tray_moved(blk_name(blk), id,
19433ab72385SPeter Xu                                       eject_flag);
19442d76e724SKevin Wolf     g_free(id);
1945a46fc9c9SMax Reitz }
19464be74634SMarkus Armbruster 
19474be74634SMarkus Armbruster int blk_get_flags(BlockBackend *blk)
19484be74634SMarkus Armbruster {
1949f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1950f21d96d0SKevin Wolf 
1951f21d96d0SKevin Wolf     if (bs) {
1952f21d96d0SKevin Wolf         return bdrv_get_flags(bs);
1953061959e8SMax Reitz     } else {
1954061959e8SMax Reitz         return blk->root_state.open_flags;
1955061959e8SMax Reitz     }
19564be74634SMarkus Armbruster }
19574be74634SMarkus Armbruster 
19584841211eSEric Blake /* Returns the minimum request alignment, in bytes; guaranteed nonzero */
19594841211eSEric Blake uint32_t blk_get_request_alignment(BlockBackend *blk)
19604841211eSEric Blake {
19614841211eSEric Blake     BlockDriverState *bs = blk_bs(blk);
19624841211eSEric Blake     return bs ? bs->bl.request_alignment : BDRV_SECTOR_SIZE;
19634841211eSEric Blake }
19644841211eSEric Blake 
196524b36e98SPaolo Bonzini /* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */
196624b36e98SPaolo Bonzini uint64_t blk_get_max_hw_transfer(BlockBackend *blk)
196724b36e98SPaolo Bonzini {
196824b36e98SPaolo Bonzini     BlockDriverState *bs = blk_bs(blk);
196924b36e98SPaolo Bonzini     uint64_t max = INT_MAX;
197024b36e98SPaolo Bonzini 
197124b36e98SPaolo Bonzini     if (bs) {
197224b36e98SPaolo Bonzini         max = MIN_NON_ZERO(max, bs->bl.max_hw_transfer);
197324b36e98SPaolo Bonzini         max = MIN_NON_ZERO(max, bs->bl.max_transfer);
197424b36e98SPaolo Bonzini     }
197524b36e98SPaolo Bonzini     return ROUND_DOWN(max, blk_get_request_alignment(blk));
197624b36e98SPaolo Bonzini }
197724b36e98SPaolo Bonzini 
19785def6b80SEric Blake /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
19795def6b80SEric Blake uint32_t blk_get_max_transfer(BlockBackend *blk)
1980454057b7SPeter Lieven {
1981f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
1982b99f7fa0SPaolo Bonzini     uint32_t max = INT_MAX;
1983f21d96d0SKevin Wolf 
1984f21d96d0SKevin Wolf     if (bs) {
1985b99f7fa0SPaolo Bonzini         max = MIN_NON_ZERO(max, bs->bl.max_transfer);
1986a46fc9c9SMax Reitz     }
1987b99f7fa0SPaolo Bonzini     return ROUND_DOWN(max, blk_get_request_alignment(blk));
1988454057b7SPeter Lieven }
1989454057b7SPeter Lieven 
1990cc071629SPaolo Bonzini int blk_get_max_hw_iov(BlockBackend *blk)
1991cc071629SPaolo Bonzini {
1992cc071629SPaolo Bonzini     return MIN_NON_ZERO(blk->root->bs->bl.max_hw_iov,
1993cc071629SPaolo Bonzini                         blk->root->bs->bl.max_iov);
1994cc071629SPaolo Bonzini }
1995cc071629SPaolo Bonzini 
1996648296e0SStefan Hajnoczi int blk_get_max_iov(BlockBackend *blk)
1997648296e0SStefan Hajnoczi {
1998f21d96d0SKevin Wolf     return blk->root->bs->bl.max_iov;
1999648296e0SStefan Hajnoczi }
2000648296e0SStefan Hajnoczi 
20014be74634SMarkus Armbruster void blk_set_guest_block_size(BlockBackend *blk, int align)
20024be74634SMarkus Armbruster {
200368e9ec01SMax Reitz     blk->guest_block_size = align;
20044be74634SMarkus Armbruster }
20054be74634SMarkus Armbruster 
2006f1c17521SPaolo Bonzini void *blk_try_blockalign(BlockBackend *blk, size_t size)
2007f1c17521SPaolo Bonzini {
2008f21d96d0SKevin Wolf     return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
2009f1c17521SPaolo Bonzini }
2010f1c17521SPaolo Bonzini 
20114be74634SMarkus Armbruster void *blk_blockalign(BlockBackend *blk, size_t size)
20124be74634SMarkus Armbruster {
2013f21d96d0SKevin Wolf     return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
20144be74634SMarkus Armbruster }
20154be74634SMarkus Armbruster 
20164be74634SMarkus Armbruster bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
20174be74634SMarkus Armbruster {
2018f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2019f21d96d0SKevin Wolf 
2020f21d96d0SKevin Wolf     if (!bs) {
2021a46fc9c9SMax Reitz         return false;
2022a46fc9c9SMax Reitz     }
2023a46fc9c9SMax Reitz 
2024f21d96d0SKevin Wolf     return bdrv_op_is_blocked(bs, op, errp);
20254be74634SMarkus Armbruster }
20264be74634SMarkus Armbruster 
20274be74634SMarkus Armbruster void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
20284be74634SMarkus Armbruster {
2029f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2030f21d96d0SKevin Wolf 
2031f21d96d0SKevin Wolf     if (bs) {
2032f21d96d0SKevin Wolf         bdrv_op_unblock(bs, op, reason);
20334be74634SMarkus Armbruster     }
2034a46fc9c9SMax Reitz }
20354be74634SMarkus Armbruster 
20364be74634SMarkus Armbruster void blk_op_block_all(BlockBackend *blk, Error *reason)
20374be74634SMarkus Armbruster {
2038f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2039f21d96d0SKevin Wolf 
2040f21d96d0SKevin Wolf     if (bs) {
2041f21d96d0SKevin Wolf         bdrv_op_block_all(bs, reason);
20424be74634SMarkus Armbruster     }
2043a46fc9c9SMax Reitz }
20444be74634SMarkus Armbruster 
20454be74634SMarkus Armbruster void blk_op_unblock_all(BlockBackend *blk, Error *reason)
20464be74634SMarkus Armbruster {
2047f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2048f21d96d0SKevin Wolf 
2049f21d96d0SKevin Wolf     if (bs) {
2050f21d96d0SKevin Wolf         bdrv_op_unblock_all(bs, reason);
20514be74634SMarkus Armbruster     }
2052a46fc9c9SMax Reitz }
20534be74634SMarkus Armbruster 
20544be74634SMarkus Armbruster AioContext *blk_get_aio_context(BlockBackend *blk)
20554be74634SMarkus Armbruster {
2056d861ab3aSKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2057d861ab3aSKevin Wolf 
2058d861ab3aSKevin Wolf     if (bs) {
2059132ada80SKevin Wolf         AioContext *ctx = bdrv_get_aio_context(blk_bs(blk));
2060132ada80SKevin Wolf         assert(ctx == blk->ctx);
20614981bdecSMax Reitz     }
20624981bdecSMax Reitz 
2063d861ab3aSKevin Wolf     return blk->ctx;
2064d861ab3aSKevin Wolf }
2065d861ab3aSKevin Wolf 
20664981bdecSMax Reitz static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
20674981bdecSMax Reitz {
20684981bdecSMax Reitz     BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
20694981bdecSMax Reitz     return blk_get_aio_context(blk_acb->blk);
20704be74634SMarkus Armbruster }
20714be74634SMarkus Armbruster 
207297896a48SKevin Wolf static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context,
207397896a48SKevin Wolf                                   bool update_root_node, Error **errp)
20744be74634SMarkus Armbruster {
2075f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2076c61791fcSManos Pitsidianakis     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
207797896a48SKevin Wolf     int ret;
2078f21d96d0SKevin Wolf 
2079f21d96d0SKevin Wolf     if (bs) {
208097896a48SKevin Wolf         if (update_root_node) {
208197896a48SKevin Wolf             ret = bdrv_child_try_set_aio_context(bs, new_context, blk->root,
208297896a48SKevin Wolf                                                  errp);
208397896a48SKevin Wolf             if (ret < 0) {
208497896a48SKevin Wolf                 return ret;
208597896a48SKevin Wolf             }
208697896a48SKevin Wolf         }
2087c61791fcSManos Pitsidianakis         if (tgm->throttle_state) {
2088dc868fb0SStefan Hajnoczi             bdrv_drained_begin(bs);
2089c61791fcSManos Pitsidianakis             throttle_group_detach_aio_context(tgm);
2090c61791fcSManos Pitsidianakis             throttle_group_attach_aio_context(tgm, new_context);
2091dc868fb0SStefan Hajnoczi             bdrv_drained_end(bs);
20927ca7f0f6SKevin Wolf         }
209338475269SKevin Wolf     }
209438475269SKevin Wolf 
2095d861ab3aSKevin Wolf     blk->ctx = new_context;
209697896a48SKevin Wolf     return 0;
209797896a48SKevin Wolf }
209897896a48SKevin Wolf 
209997896a48SKevin Wolf int blk_set_aio_context(BlockBackend *blk, AioContext *new_context,
210097896a48SKevin Wolf                         Error **errp)
210138475269SKevin Wolf {
210297896a48SKevin Wolf     return blk_do_set_aio_context(blk, new_context, true, errp);
210338475269SKevin Wolf }
210438475269SKevin Wolf 
210538475269SKevin Wolf static bool blk_root_can_set_aio_ctx(BdrvChild *child, AioContext *ctx,
210638475269SKevin Wolf                                      GSList **ignore, Error **errp)
210738475269SKevin Wolf {
210838475269SKevin Wolf     BlockBackend *blk = child->opaque;
210938475269SKevin Wolf 
2110980b0f94SKevin Wolf     if (blk->allow_aio_context_change) {
2111980b0f94SKevin Wolf         return true;
2112980b0f94SKevin Wolf     }
2113980b0f94SKevin Wolf 
211438475269SKevin Wolf     /* Only manually created BlockBackends that are not attached to anything
211538475269SKevin Wolf      * can change their AioContext without updating their user. */
211638475269SKevin Wolf     if (!blk->name || blk->dev) {
211738475269SKevin Wolf         /* TODO Add BB name/QOM path */
211838475269SKevin Wolf         error_setg(errp, "Cannot change iothread of active block backend");
211938475269SKevin Wolf         return false;
212038475269SKevin Wolf     }
212138475269SKevin Wolf 
212238475269SKevin Wolf     return true;
212338475269SKevin Wolf }
212438475269SKevin Wolf 
212538475269SKevin Wolf static void blk_root_set_aio_ctx(BdrvChild *child, AioContext *ctx,
212638475269SKevin Wolf                                  GSList **ignore)
212738475269SKevin Wolf {
212838475269SKevin Wolf     BlockBackend *blk = child->opaque;
212997896a48SKevin Wolf     blk_do_set_aio_context(blk, ctx, false, &error_abort);
213038475269SKevin Wolf }
21314be74634SMarkus Armbruster 
21322019ba0aSMax Reitz void blk_add_aio_context_notifier(BlockBackend *blk,
21332019ba0aSMax Reitz         void (*attached_aio_context)(AioContext *new_context, void *opaque),
21342019ba0aSMax Reitz         void (*detach_aio_context)(void *opaque), void *opaque)
21352019ba0aSMax Reitz {
2136d03654eaSStefan Hajnoczi     BlockBackendAioNotifier *notifier;
2137f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2138f21d96d0SKevin Wolf 
2139d03654eaSStefan Hajnoczi     notifier = g_new(BlockBackendAioNotifier, 1);
2140d03654eaSStefan Hajnoczi     notifier->attached_aio_context = attached_aio_context;
2141d03654eaSStefan Hajnoczi     notifier->detach_aio_context = detach_aio_context;
2142d03654eaSStefan Hajnoczi     notifier->opaque = opaque;
2143d03654eaSStefan Hajnoczi     QLIST_INSERT_HEAD(&blk->aio_notifiers, notifier, list);
2144d03654eaSStefan Hajnoczi 
2145f21d96d0SKevin Wolf     if (bs) {
2146f21d96d0SKevin Wolf         bdrv_add_aio_context_notifier(bs, attached_aio_context,
21472019ba0aSMax Reitz                                       detach_aio_context, opaque);
21482019ba0aSMax Reitz     }
2149a46fc9c9SMax Reitz }
21502019ba0aSMax Reitz 
21512019ba0aSMax Reitz void blk_remove_aio_context_notifier(BlockBackend *blk,
21522019ba0aSMax Reitz                                      void (*attached_aio_context)(AioContext *,
21532019ba0aSMax Reitz                                                                   void *),
21542019ba0aSMax Reitz                                      void (*detach_aio_context)(void *),
21552019ba0aSMax Reitz                                      void *opaque)
21562019ba0aSMax Reitz {
2157d03654eaSStefan Hajnoczi     BlockBackendAioNotifier *notifier;
2158f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2159f21d96d0SKevin Wolf 
2160f21d96d0SKevin Wolf     if (bs) {
2161f21d96d0SKevin Wolf         bdrv_remove_aio_context_notifier(bs, attached_aio_context,
21622019ba0aSMax Reitz                                          detach_aio_context, opaque);
21632019ba0aSMax Reitz     }
2164d03654eaSStefan Hajnoczi 
2165d03654eaSStefan Hajnoczi     QLIST_FOREACH(notifier, &blk->aio_notifiers, list) {
2166d03654eaSStefan Hajnoczi         if (notifier->attached_aio_context == attached_aio_context &&
2167d03654eaSStefan Hajnoczi             notifier->detach_aio_context == detach_aio_context &&
2168d03654eaSStefan Hajnoczi             notifier->opaque == opaque) {
2169d03654eaSStefan Hajnoczi             QLIST_REMOVE(notifier, list);
2170d03654eaSStefan Hajnoczi             g_free(notifier);
2171d03654eaSStefan Hajnoczi             return;
2172d03654eaSStefan Hajnoczi         }
2173d03654eaSStefan Hajnoczi     }
2174d03654eaSStefan Hajnoczi 
2175d03654eaSStefan Hajnoczi     abort();
2176a46fc9c9SMax Reitz }
21772019ba0aSMax Reitz 
21783301f6c6SMax Reitz void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
21793301f6c6SMax Reitz {
21803301f6c6SMax Reitz     notifier_list_add(&blk->remove_bs_notifiers, notify);
21813301f6c6SMax Reitz }
21823301f6c6SMax Reitz 
21833301f6c6SMax Reitz void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
21843301f6c6SMax Reitz {
21853301f6c6SMax Reitz     notifier_list_add(&blk->insert_bs_notifiers, notify);
21863301f6c6SMax Reitz }
21873301f6c6SMax Reitz 
21884be74634SMarkus Armbruster void blk_io_plug(BlockBackend *blk)
21894be74634SMarkus Armbruster {
2190f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2191f21d96d0SKevin Wolf 
2192f21d96d0SKevin Wolf     if (bs) {
2193f21d96d0SKevin Wolf         bdrv_io_plug(bs);
21944be74634SMarkus Armbruster     }
2195a46fc9c9SMax Reitz }
21964be74634SMarkus Armbruster 
21974be74634SMarkus Armbruster void blk_io_unplug(BlockBackend *blk)
21984be74634SMarkus Armbruster {
2199f21d96d0SKevin Wolf     BlockDriverState *bs = blk_bs(blk);
2200f21d96d0SKevin Wolf 
2201f21d96d0SKevin Wolf     if (bs) {
2202f21d96d0SKevin Wolf         bdrv_io_unplug(bs);
22034be74634SMarkus Armbruster     }
2204a46fc9c9SMax Reitz }
22054be74634SMarkus Armbruster 
22064be74634SMarkus Armbruster BlockAcctStats *blk_get_stats(BlockBackend *blk)
22074be74634SMarkus Armbruster {
22087f0e9da6SMax Reitz     return &blk->stats;
22094be74634SMarkus Armbruster }
22104be74634SMarkus Armbruster 
22114be74634SMarkus Armbruster void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
22124be74634SMarkus Armbruster                   BlockCompletionFunc *cb, void *opaque)
22134be74634SMarkus Armbruster {
22144be74634SMarkus Armbruster     return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
22154be74634SMarkus Armbruster }
22161ef01253SMax Reitz 
2217d004bd52SEric Blake int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
221834460febSVladimir Sementsov-Ogievskiy                                       int64_t bytes, BdrvRequestFlags flags)
22191ef01253SMax Reitz {
2220f5a5ca79SManos Pitsidianakis     return blk_co_pwritev(blk, offset, bytes, NULL,
222116aaf975SKevin Wolf                           flags | BDRV_REQ_ZERO_WRITE);
22221ef01253SMax Reitz }
22231ef01253SMax Reitz 
2224fe5c1355SPavel Butsykin int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
2225fe5c1355SPavel Butsykin                           int count)
22261ef01253SMax Reitz {
222735fadca8SPavel Butsykin     return blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
222835fadca8SPavel Butsykin                    BDRV_REQ_WRITE_COMPRESSED);
22291ef01253SMax Reitz }
22301ef01253SMax Reitz 
2231c80d8b06SMax Reitz int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,
22328c6242b6SKevin Wolf                  PreallocMode prealloc, BdrvRequestFlags flags, Error **errp)
22331ef01253SMax Reitz {
2234c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
2235ed3d2ec9SMax Reitz         error_setg(errp, "No medium inserted");
2236c09ba36cSMax Reitz         return -ENOMEDIUM;
2237c09ba36cSMax Reitz     }
2238c09ba36cSMax Reitz 
22398c6242b6SKevin Wolf     return bdrv_truncate(blk->root, offset, exact, prealloc, flags, errp);
22401ef01253SMax Reitz }
22411ef01253SMax Reitz 
22421ef01253SMax Reitz int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
22431ef01253SMax Reitz                      int64_t pos, int size)
22441ef01253SMax Reitz {
2245bfd18d1eSKevin Wolf     int ret;
2246bfd18d1eSKevin Wolf 
2247c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
2248c09ba36cSMax Reitz         return -ENOMEDIUM;
2249c09ba36cSMax Reitz     }
2250c09ba36cSMax Reitz 
2251bfd18d1eSKevin Wolf     ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
2252bfd18d1eSKevin Wolf     if (ret < 0) {
2253bfd18d1eSKevin Wolf         return ret;
2254bfd18d1eSKevin Wolf     }
2255bfd18d1eSKevin Wolf 
2256bfd18d1eSKevin Wolf     if (ret == size && !blk->enable_write_cache) {
2257bfd18d1eSKevin Wolf         ret = bdrv_flush(blk_bs(blk));
2258bfd18d1eSKevin Wolf     }
2259bfd18d1eSKevin Wolf 
2260bfd18d1eSKevin Wolf     return ret < 0 ? ret : size;
22611ef01253SMax Reitz }
22621ef01253SMax Reitz 
22631ef01253SMax Reitz int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
22641ef01253SMax Reitz {
2265c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
2266c09ba36cSMax Reitz         return -ENOMEDIUM;
2267c09ba36cSMax Reitz     }
2268c09ba36cSMax Reitz 
2269f21d96d0SKevin Wolf     return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
22701ef01253SMax Reitz }
2271f0272c4dSEkaterina Tumanova 
2272f0272c4dSEkaterina Tumanova int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
2273f0272c4dSEkaterina Tumanova {
2274c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
2275c09ba36cSMax Reitz         return -ENOMEDIUM;
2276c09ba36cSMax Reitz     }
2277c09ba36cSMax Reitz 
2278f21d96d0SKevin Wolf     return bdrv_probe_blocksizes(blk_bs(blk), bsz);
2279f0272c4dSEkaterina Tumanova }
2280f0272c4dSEkaterina Tumanova 
2281f0272c4dSEkaterina Tumanova int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
2282f0272c4dSEkaterina Tumanova {
2283c09ba36cSMax Reitz     if (!blk_is_available(blk)) {
2284c09ba36cSMax Reitz         return -ENOMEDIUM;
2285c09ba36cSMax Reitz     }
2286c09ba36cSMax Reitz 
2287f21d96d0SKevin Wolf     return bdrv_probe_geometry(blk_bs(blk), geo);
2288f0272c4dSEkaterina Tumanova }
2289281d22d8SMax Reitz 
2290281d22d8SMax Reitz /*
2291281d22d8SMax Reitz  * Updates the BlockBackendRootState object with data from the currently
2292281d22d8SMax Reitz  * attached BlockDriverState.
2293281d22d8SMax Reitz  */
2294281d22d8SMax Reitz void blk_update_root_state(BlockBackend *blk)
2295281d22d8SMax Reitz {
2296f21d96d0SKevin Wolf     assert(blk->root);
2297281d22d8SMax Reitz 
2298f21d96d0SKevin Wolf     blk->root_state.open_flags    = blk->root->bs->open_flags;
2299f21d96d0SKevin Wolf     blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
2300281d22d8SMax Reitz }
2301281d22d8SMax Reitz 
230238cb18f5SMax Reitz /*
2303b85114f8SKevin Wolf  * Returns the detect-zeroes setting to be used for bdrv_open() of a
2304b85114f8SKevin Wolf  * BlockDriverState which is supposed to inherit the root state.
230538cb18f5SMax Reitz  */
2306b85114f8SKevin Wolf bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)
230738cb18f5SMax Reitz {
2308b85114f8SKevin Wolf     return blk->root_state.detect_zeroes;
230938cb18f5SMax Reitz }
231038cb18f5SMax Reitz 
231138cb18f5SMax Reitz /*
231238cb18f5SMax Reitz  * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
231338cb18f5SMax Reitz  * supposed to inherit the root state.
231438cb18f5SMax Reitz  */
231538cb18f5SMax Reitz int blk_get_open_flags_from_root_state(BlockBackend *blk)
231638cb18f5SMax Reitz {
2317260242a8SVladimir Sementsov-Ogievskiy     return blk->root_state.open_flags;
231838cb18f5SMax Reitz }
231938cb18f5SMax Reitz 
2320281d22d8SMax Reitz BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
2321281d22d8SMax Reitz {
2322281d22d8SMax Reitz     return &blk->root_state;
2323281d22d8SMax Reitz }
23241393f212SMax Reitz 
23251393f212SMax Reitz int blk_commit_all(void)
23261393f212SMax Reitz {
2327fe1a9cbcSMax Reitz     BlockBackend *blk = NULL;
2328fe1a9cbcSMax Reitz 
2329fe1a9cbcSMax Reitz     while ((blk = blk_all_next(blk)) != NULL) {
2330fe1a9cbcSMax Reitz         AioContext *aio_context = blk_get_aio_context(blk);
23319a71b9deSMax Reitz         BlockDriverState *unfiltered_bs = bdrv_skip_filters(blk_bs(blk));
2332fe1a9cbcSMax Reitz 
2333fe1a9cbcSMax Reitz         aio_context_acquire(aio_context);
23349a71b9deSMax Reitz         if (blk_is_inserted(blk) && bdrv_cow_child(unfiltered_bs)) {
23359a71b9deSMax Reitz             int ret;
23369a71b9deSMax Reitz 
23379a71b9deSMax Reitz             ret = bdrv_commit(unfiltered_bs);
2338fe1a9cbcSMax Reitz             if (ret < 0) {
2339fe1a9cbcSMax Reitz                 aio_context_release(aio_context);
2340fe1a9cbcSMax Reitz                 return ret;
2341fe1a9cbcSMax Reitz             }
2342fe1a9cbcSMax Reitz         }
2343fe1a9cbcSMax Reitz         aio_context_release(aio_context);
2344fe1a9cbcSMax Reitz     }
2345fe1a9cbcSMax Reitz     return 0;
2346fe1a9cbcSMax Reitz }
2347fe1a9cbcSMax Reitz 
234897148076SKevin Wolf 
234997148076SKevin Wolf /* throttling disk I/O limits */
235097148076SKevin Wolf void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
235197148076SKevin Wolf {
2352022cdc9fSManos Pitsidianakis     throttle_group_config(&blk->public.throttle_group_member, cfg);
235397148076SKevin Wolf }
235497148076SKevin Wolf 
235597148076SKevin Wolf void blk_io_limits_disable(BlockBackend *blk)
235697148076SKevin Wolf {
235748bf7ea8SAlberto Garcia     BlockDriverState *bs = blk_bs(blk);
235848bf7ea8SAlberto Garcia     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
235948bf7ea8SAlberto Garcia     assert(tgm->throttle_state);
236048bf7ea8SAlberto Garcia     if (bs) {
236148bf7ea8SAlberto Garcia         bdrv_drained_begin(bs);
236248bf7ea8SAlberto Garcia     }
236348bf7ea8SAlberto Garcia     throttle_group_unregister_tgm(tgm);
236448bf7ea8SAlberto Garcia     if (bs) {
236548bf7ea8SAlberto Garcia         bdrv_drained_end(bs);
236648bf7ea8SAlberto Garcia     }
236797148076SKevin Wolf }
236897148076SKevin Wolf 
236997148076SKevin Wolf /* should be called before blk_set_io_limits if a limit is set */
237097148076SKevin Wolf void blk_io_limits_enable(BlockBackend *blk, const char *group)
237197148076SKevin Wolf {
2372022cdc9fSManos Pitsidianakis     assert(!blk->public.throttle_group_member.throttle_state);
2373c61791fcSManos Pitsidianakis     throttle_group_register_tgm(&blk->public.throttle_group_member,
2374c61791fcSManos Pitsidianakis                                 group, blk_get_aio_context(blk));
237597148076SKevin Wolf }
237697148076SKevin Wolf 
237797148076SKevin Wolf void blk_io_limits_update_group(BlockBackend *blk, const char *group)
237897148076SKevin Wolf {
237997148076SKevin Wolf     /* this BB is not part of any group */
2380022cdc9fSManos Pitsidianakis     if (!blk->public.throttle_group_member.throttle_state) {
238197148076SKevin Wolf         return;
238297148076SKevin Wolf     }
238397148076SKevin Wolf 
238497148076SKevin Wolf     /* this BB is a part of the same group than the one we want */
2385022cdc9fSManos Pitsidianakis     if (!g_strcmp0(throttle_group_get_name(&blk->public.throttle_group_member),
2386022cdc9fSManos Pitsidianakis                 group)) {
238797148076SKevin Wolf         return;
238897148076SKevin Wolf     }
238997148076SKevin Wolf 
239097148076SKevin Wolf     /* need to change the group this bs belong to */
239197148076SKevin Wolf     blk_io_limits_disable(blk);
239297148076SKevin Wolf     blk_io_limits_enable(blk, group);
239397148076SKevin Wolf }
2394c2066af0SKevin Wolf 
2395c2066af0SKevin Wolf static void blk_root_drained_begin(BdrvChild *child)
2396c2066af0SKevin Wolf {
2397c2066af0SKevin Wolf     BlockBackend *blk = child->opaque;
2398d73415a3SStefan Hajnoczi     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
2399c2066af0SKevin Wolf 
2400f4d9cc88SJohn Snow     if (++blk->quiesce_counter == 1) {
2401f4d9cc88SJohn Snow         if (blk->dev_ops && blk->dev_ops->drained_begin) {
2402f4d9cc88SJohn Snow             blk->dev_ops->drained_begin(blk->dev_opaque);
2403f4d9cc88SJohn Snow         }
2404f4d9cc88SJohn Snow     }
2405f4d9cc88SJohn Snow 
240636fe1331SKevin Wolf     /* Note that blk->root may not be accessible here yet if we are just
240736fe1331SKevin Wolf      * attaching to a BlockDriverState that is drained. Use child instead. */
240836fe1331SKevin Wolf 
2409d73415a3SStefan Hajnoczi     if (qatomic_fetch_inc(&tgm->io_limits_disabled) == 0) {
2410d73415a3SStefan Hajnoczi         throttle_group_restart_tgm(tgm);
2411c2066af0SKevin Wolf     }
2412c2066af0SKevin Wolf }
2413c2066af0SKevin Wolf 
2414fe5258a5SKevin Wolf static bool blk_root_drained_poll(BdrvChild *child)
2415fe5258a5SKevin Wolf {
2416fe5258a5SKevin Wolf     BlockBackend *blk = child->opaque;
2417095cc4d0SSergio Lopez     bool busy = false;
2418fe5258a5SKevin Wolf     assert(blk->quiesce_counter);
2419095cc4d0SSergio Lopez 
2420095cc4d0SSergio Lopez     if (blk->dev_ops && blk->dev_ops->drained_poll) {
2421095cc4d0SSergio Lopez         busy = blk->dev_ops->drained_poll(blk->dev_opaque);
2422095cc4d0SSergio Lopez     }
2423095cc4d0SSergio Lopez     return busy || !!blk->in_flight;
2424fe5258a5SKevin Wolf }
2425fe5258a5SKevin Wolf 
2426e037c09cSMax Reitz static void blk_root_drained_end(BdrvChild *child, int *drained_end_counter)
2427c2066af0SKevin Wolf {
2428c2066af0SKevin Wolf     BlockBackend *blk = child->opaque;
2429f4d9cc88SJohn Snow     assert(blk->quiesce_counter);
2430c2066af0SKevin Wolf 
2431022cdc9fSManos Pitsidianakis     assert(blk->public.throttle_group_member.io_limits_disabled);
2432d73415a3SStefan Hajnoczi     qatomic_dec(&blk->public.throttle_group_member.io_limits_disabled);
2433f4d9cc88SJohn Snow 
2434f4d9cc88SJohn Snow     if (--blk->quiesce_counter == 0) {
2435f4d9cc88SJohn Snow         if (blk->dev_ops && blk->dev_ops->drained_end) {
2436f4d9cc88SJohn Snow             blk->dev_ops->drained_end(blk->dev_opaque);
2437f4d9cc88SJohn Snow         }
2438cf312932SKevin Wolf         while (qemu_co_enter_next(&blk->queued_requests, NULL)) {
2439cf312932SKevin Wolf             /* Resume all queued requests */
2440cf312932SKevin Wolf         }
2441f4d9cc88SJohn Snow     }
2442c2066af0SKevin Wolf }
244323d0ba93SFam Zheng 
244423d0ba93SFam Zheng void blk_register_buf(BlockBackend *blk, void *host, size_t size)
244523d0ba93SFam Zheng {
244623d0ba93SFam Zheng     bdrv_register_buf(blk_bs(blk), host, size);
244723d0ba93SFam Zheng }
244823d0ba93SFam Zheng 
244923d0ba93SFam Zheng void blk_unregister_buf(BlockBackend *blk, void *host)
245023d0ba93SFam Zheng {
245123d0ba93SFam Zheng     bdrv_unregister_buf(blk_bs(blk), host);
245223d0ba93SFam Zheng }
2453b5679fa4SFam Zheng 
2454b5679fa4SFam Zheng int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in,
2455b5679fa4SFam Zheng                                    BlockBackend *blk_out, int64_t off_out,
245667b51fb9SVladimir Sementsov-Ogievskiy                                    int bytes, BdrvRequestFlags read_flags,
245767b51fb9SVladimir Sementsov-Ogievskiy                                    BdrvRequestFlags write_flags)
2458b5679fa4SFam Zheng {
2459b5679fa4SFam Zheng     int r;
2460b5679fa4SFam Zheng     r = blk_check_byte_request(blk_in, off_in, bytes);
2461b5679fa4SFam Zheng     if (r) {
2462b5679fa4SFam Zheng         return r;
2463b5679fa4SFam Zheng     }
2464b5679fa4SFam Zheng     r = blk_check_byte_request(blk_out, off_out, bytes);
2465b5679fa4SFam Zheng     if (r) {
2466b5679fa4SFam Zheng         return r;
2467b5679fa4SFam Zheng     }
2468b5679fa4SFam Zheng     return bdrv_co_copy_range(blk_in->root, off_in,
2469b5679fa4SFam Zheng                               blk_out->root, off_out,
247067b51fb9SVladimir Sementsov-Ogievskiy                               bytes, read_flags, write_flags);
2471b5679fa4SFam Zheng }
24725d3b4e99SVladimir Sementsov-Ogievskiy 
24735d3b4e99SVladimir Sementsov-Ogievskiy const BdrvChild *blk_root(BlockBackend *blk)
24745d3b4e99SVladimir Sementsov-Ogievskiy {
24755d3b4e99SVladimir Sementsov-Ogievskiy     return blk->root;
24765d3b4e99SVladimir Sementsov-Ogievskiy }
24772b7bbdbdSMax Reitz 
24782b7bbdbdSMax Reitz int blk_make_empty(BlockBackend *blk, Error **errp)
24792b7bbdbdSMax Reitz {
24802b7bbdbdSMax Reitz     if (!blk_is_available(blk)) {
24812b7bbdbdSMax Reitz         error_setg(errp, "No medium inserted");
24822b7bbdbdSMax Reitz         return -ENOMEDIUM;
24832b7bbdbdSMax Reitz     }
24842b7bbdbdSMax Reitz 
24852b7bbdbdSMax Reitz     return bdrv_make_empty(blk->root, errp);
24862b7bbdbdSMax Reitz }
2487