qcow2: Check snapshot L1 table in qcow2_snapshot_goto()
[qemu/ar7.git] / block / block-backend.c
blobb3c790e2bd44e2a77a9c7092e9d31a87e7850624
1 /*
2 * QEMU Block backends
4 * Copyright (C) 2014-2016 Red Hat, Inc.
6 * Authors:
7 * Markus Armbruster <armbru@redhat.com>,
9 * This work is licensed under the terms of the GNU LGPL, version 2.1
10 * or later. See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "sysemu/block-backend.h"
15 #include "block/block_int.h"
16 #include "block/blockjob.h"
17 #include "block/throttle-groups.h"
18 #include "sysemu/blockdev.h"
19 #include "sysemu/sysemu.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-events-block.h"
22 #include "qemu/id.h"
23 #include "qemu/option.h"
24 #include "trace.h"
25 #include "migration/misc.h"
27 /* Number of coroutines to reserve per attached device model */
28 #define COROUTINE_POOL_RESERVATION 64
30 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
32 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
34 struct BlockBackend {
35 char *name;
36 int refcnt;
37 BdrvChild *root;
38 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
39 QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */
40 QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
41 BlockBackendPublic public;
43 void *dev; /* attached device model, if any */
44 bool legacy_dev; /* true if dev is not a DeviceState */
45 /* TODO change to DeviceState when all users are qdevified */
46 const BlockDevOps *dev_ops;
47 void *dev_opaque;
49 /* the block size for which the guest device expects atomicity */
50 int guest_block_size;
52 /* If the BDS tree is removed, some of its options are stored here (which
53 * can be used to restore those options in the new BDS on insert) */
54 BlockBackendRootState root_state;
56 bool enable_write_cache;
58 /* I/O stats (display with "info blockstats"). */
59 BlockAcctStats stats;
61 BlockdevOnError on_read_error, on_write_error;
62 bool iostatus_enabled;
63 BlockDeviceIoStatus iostatus;
65 uint64_t perm;
66 uint64_t shared_perm;
67 bool disable_perm;
69 bool allow_write_beyond_eof;
71 NotifierList remove_bs_notifiers, insert_bs_notifiers;
73 int quiesce_counter;
74 VMChangeStateEntry *vmsh;
75 bool force_allow_inactivate;
77 /* Number of in-flight aio requests. BlockDriverState also counts
78 * in-flight requests but aio requests can exist even when blk->root is
79 * NULL, so we cannot rely on its counter for that case.
80 * Accessed with atomic ops.
82 unsigned int in_flight;
83 AioWait wait;
86 typedef struct BlockBackendAIOCB {
87 BlockAIOCB common;
88 BlockBackend *blk;
89 int ret;
90 } BlockBackendAIOCB;
92 static const AIOCBInfo block_backend_aiocb_info = {
93 .get_aio_context = blk_aiocb_get_aio_context,
94 .aiocb_size = sizeof(BlockBackendAIOCB),
97 static void drive_info_del(DriveInfo *dinfo);
98 static BlockBackend *bdrv_first_blk(BlockDriverState *bs);
100 /* All BlockBackends */
101 static QTAILQ_HEAD(, BlockBackend) block_backends =
102 QTAILQ_HEAD_INITIALIZER(block_backends);
104 /* All BlockBackends referenced by the monitor and which are iterated through by
105 * blk_next() */
106 static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
107 QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
109 static void blk_root_inherit_options(int *child_flags, QDict *child_options,
110 int parent_flags, QDict *parent_options)
112 /* We're not supposed to call this function for root nodes */
113 abort();
115 static void blk_root_drained_begin(BdrvChild *child);
116 static void blk_root_drained_end(BdrvChild *child);
118 static void blk_root_change_media(BdrvChild *child, bool load);
119 static void blk_root_resize(BdrvChild *child);
121 static char *blk_root_get_parent_desc(BdrvChild *child)
123 BlockBackend *blk = child->opaque;
124 char *dev_id;
126 if (blk->name) {
127 return g_strdup(blk->name);
130 dev_id = blk_get_attached_dev_id(blk);
131 if (*dev_id) {
132 return dev_id;
133 } else {
134 /* TODO Callback into the BB owner for something more detailed */
135 g_free(dev_id);
136 return g_strdup("a block device");
140 static const char *blk_root_get_name(BdrvChild *child)
142 return blk_name(child->opaque);
145 static void blk_vm_state_changed(void *opaque, int running, RunState state)
147 Error *local_err = NULL;
148 BlockBackend *blk = opaque;
150 if (state == RUN_STATE_INMIGRATE) {
151 return;
154 qemu_del_vm_change_state_handler(blk->vmsh);
155 blk->vmsh = NULL;
156 blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
157 if (local_err) {
158 error_report_err(local_err);
163 * Notifies the user of the BlockBackend that migration has completed. qdev
164 * devices can tighten their permissions in response (specifically revoke
165 * shared write permissions that we needed for storage migration).
167 * If an error is returned, the VM cannot be allowed to be resumed.
169 static void blk_root_activate(BdrvChild *child, Error **errp)
171 BlockBackend *blk = child->opaque;
172 Error *local_err = NULL;
174 if (!blk->disable_perm) {
175 return;
178 blk->disable_perm = false;
180 blk_set_perm(blk, blk->perm, BLK_PERM_ALL, &local_err);
181 if (local_err) {
182 error_propagate(errp, local_err);
183 blk->disable_perm = true;
184 return;
187 if (runstate_check(RUN_STATE_INMIGRATE)) {
188 /* Activation can happen when migration process is still active, for
189 * example when nbd_server_add is called during non-shared storage
190 * migration. Defer the shared_perm update to migration completion. */
191 if (!blk->vmsh) {
192 blk->vmsh = qemu_add_vm_change_state_handler(blk_vm_state_changed,
193 blk);
195 return;
198 blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
199 if (local_err) {
200 error_propagate(errp, local_err);
201 blk->disable_perm = true;
202 return;
206 void blk_set_force_allow_inactivate(BlockBackend *blk)
208 blk->force_allow_inactivate = true;
211 static bool blk_can_inactivate(BlockBackend *blk)
213 /* If it is a guest device, inactivate is ok. */
214 if (blk->dev || blk_name(blk)[0]) {
215 return true;
218 /* Inactivating means no more writes to the image can be done,
219 * even if those writes would be changes invisible to the
220 * guest. For block job BBs that satisfy this, we can just allow
221 * it. This is the case for mirror job source, which is required
222 * by libvirt non-shared block migration. */
223 if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) {
224 return true;
227 return blk->force_allow_inactivate;
230 static int blk_root_inactivate(BdrvChild *child)
232 BlockBackend *blk = child->opaque;
234 if (blk->disable_perm) {
235 return 0;
238 if (!blk_can_inactivate(blk)) {
239 return -EPERM;
242 blk->disable_perm = true;
243 if (blk->root) {
244 bdrv_child_try_set_perm(blk->root, 0, BLK_PERM_ALL, &error_abort);
247 return 0;
250 static const BdrvChildRole child_root = {
251 .inherit_options = blk_root_inherit_options,
253 .change_media = blk_root_change_media,
254 .resize = blk_root_resize,
255 .get_name = blk_root_get_name,
256 .get_parent_desc = blk_root_get_parent_desc,
258 .drained_begin = blk_root_drained_begin,
259 .drained_end = blk_root_drained_end,
261 .activate = blk_root_activate,
262 .inactivate = blk_root_inactivate,
266 * Create a new BlockBackend with a reference count of one.
268 * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
269 * to request for a block driver node that is attached to this BlockBackend.
270 * @shared_perm is a bitmask which describes which permissions may be granted
271 * to other users of the attached node.
272 * Both sets of permissions can be changed later using blk_set_perm().
274 * Return the new BlockBackend on success, null on failure.
276 BlockBackend *blk_new(uint64_t perm, uint64_t shared_perm)
278 BlockBackend *blk;
280 blk = g_new0(BlockBackend, 1);
281 blk->refcnt = 1;
282 blk->perm = perm;
283 blk->shared_perm = shared_perm;
284 blk_set_enable_write_cache(blk, true);
286 block_acct_init(&blk->stats);
288 notifier_list_init(&blk->remove_bs_notifiers);
289 notifier_list_init(&blk->insert_bs_notifiers);
291 QTAILQ_INSERT_TAIL(&block_backends, blk, link);
292 return blk;
296 * Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
298 * Just as with bdrv_open(), after having called this function the reference to
299 * @options belongs to the block layer (even on failure).
301 * TODO: Remove @filename and @flags; it should be possible to specify a whole
302 * BDS tree just by specifying the @options QDict (or @reference,
303 * alternatively). At the time of adding this function, this is not possible,
304 * though, so callers of this function have to be able to specify @filename and
305 * @flags.
307 BlockBackend *blk_new_open(const char *filename, const char *reference,
308 QDict *options, int flags, Error **errp)
310 BlockBackend *blk;
311 BlockDriverState *bs;
312 uint64_t perm = 0;
314 /* blk_new_open() is mainly used in .bdrv_create implementations and the
315 * tools where sharing isn't a concern because the BDS stays private, so we
316 * just request permission according to the flags.
318 * The exceptions are xen_disk and blockdev_init(); in these cases, the
319 * caller of blk_new_open() doesn't make use of the permissions, but they
320 * shouldn't hurt either. We can still share everything here because the
321 * guest devices will add their own blockers if they can't share. */
322 if ((flags & BDRV_O_NO_IO) == 0) {
323 perm |= BLK_PERM_CONSISTENT_READ;
324 if (flags & BDRV_O_RDWR) {
325 perm |= BLK_PERM_WRITE;
328 if (flags & BDRV_O_RESIZE) {
329 perm |= BLK_PERM_RESIZE;
332 blk = blk_new(perm, BLK_PERM_ALL);
333 bs = bdrv_open(filename, reference, options, flags, errp);
334 if (!bs) {
335 blk_unref(blk);
336 return NULL;
339 blk->root = bdrv_root_attach_child(bs, "root", &child_root,
340 perm, BLK_PERM_ALL, blk, errp);
341 if (!blk->root) {
342 bdrv_unref(bs);
343 blk_unref(blk);
344 return NULL;
347 return blk;
350 static void blk_delete(BlockBackend *blk)
352 assert(!blk->refcnt);
353 assert(!blk->name);
354 assert(!blk->dev);
355 if (blk->public.throttle_group_member.throttle_state) {
356 blk_io_limits_disable(blk);
358 if (blk->root) {
359 blk_remove_bs(blk);
361 if (blk->vmsh) {
362 qemu_del_vm_change_state_handler(blk->vmsh);
363 blk->vmsh = NULL;
365 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
366 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
367 QTAILQ_REMOVE(&block_backends, blk, link);
368 drive_info_del(blk->legacy_dinfo);
369 block_acct_cleanup(&blk->stats);
370 g_free(blk);
373 static void drive_info_del(DriveInfo *dinfo)
375 if (!dinfo) {
376 return;
378 qemu_opts_del(dinfo->opts);
379 g_free(dinfo->serial);
380 g_free(dinfo);
383 int blk_get_refcnt(BlockBackend *blk)
385 return blk ? blk->refcnt : 0;
389 * Increment @blk's reference count.
390 * @blk must not be null.
392 void blk_ref(BlockBackend *blk)
394 blk->refcnt++;
398 * Decrement @blk's reference count.
399 * If this drops it to zero, destroy @blk.
400 * For convenience, do nothing if @blk is null.
402 void blk_unref(BlockBackend *blk)
404 if (blk) {
405 assert(blk->refcnt > 0);
406 if (!--blk->refcnt) {
407 blk_delete(blk);
413 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
414 * ones which are hidden (i.e. are not referenced by the monitor).
416 BlockBackend *blk_all_next(BlockBackend *blk)
418 return blk ? QTAILQ_NEXT(blk, link)
419 : QTAILQ_FIRST(&block_backends);
422 void blk_remove_all_bs(void)
424 BlockBackend *blk = NULL;
426 while ((blk = blk_all_next(blk)) != NULL) {
427 AioContext *ctx = blk_get_aio_context(blk);
429 aio_context_acquire(ctx);
430 if (blk->root) {
431 blk_remove_bs(blk);
433 aio_context_release(ctx);
438 * Return the monitor-owned BlockBackend after @blk.
439 * If @blk is null, return the first one.
440 * Else, return @blk's next sibling, which may be null.
442 * To iterate over all BlockBackends, do
443 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
444 * ...
447 BlockBackend *blk_next(BlockBackend *blk)
449 return blk ? QTAILQ_NEXT(blk, monitor_link)
450 : QTAILQ_FIRST(&monitor_block_backends);
453 /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by
454 * the monitor or attached to a BlockBackend */
455 BlockDriverState *bdrv_next(BdrvNextIterator *it)
457 BlockDriverState *bs, *old_bs;
459 /* Must be called from the main loop */
460 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
462 /* First, return all root nodes of BlockBackends. In order to avoid
463 * returning a BDS twice when multiple BBs refer to it, we only return it
464 * if the BB is the first one in the parent list of the BDS. */
465 if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
466 BlockBackend *old_blk = it->blk;
468 old_bs = old_blk ? blk_bs(old_blk) : NULL;
470 do {
471 it->blk = blk_all_next(it->blk);
472 bs = it->blk ? blk_bs(it->blk) : NULL;
473 } while (it->blk && (bs == NULL || bdrv_first_blk(bs) != it->blk));
475 if (it->blk) {
476 blk_ref(it->blk);
478 blk_unref(old_blk);
480 if (bs) {
481 bdrv_ref(bs);
482 bdrv_unref(old_bs);
483 return bs;
485 it->phase = BDRV_NEXT_MONITOR_OWNED;
486 } else {
487 old_bs = it->bs;
490 /* Then return the monitor-owned BDSes without a BB attached. Ignore all
491 * BDSes that are attached to a BlockBackend here; they have been handled
492 * by the above block already */
493 do {
494 it->bs = bdrv_next_monitor_owned(it->bs);
495 bs = it->bs;
496 } while (bs && bdrv_has_blk(bs));
498 if (bs) {
499 bdrv_ref(bs);
501 bdrv_unref(old_bs);
503 return bs;
506 static void bdrv_next_reset(BdrvNextIterator *it)
508 *it = (BdrvNextIterator) {
509 .phase = BDRV_NEXT_BACKEND_ROOTS,
513 BlockDriverState *bdrv_first(BdrvNextIterator *it)
515 bdrv_next_reset(it);
516 return bdrv_next(it);
519 /* Must be called when aborting a bdrv_next() iteration before
520 * bdrv_next() returns NULL */
521 void bdrv_next_cleanup(BdrvNextIterator *it)
523 /* Must be called from the main loop */
524 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
526 if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
527 if (it->blk) {
528 bdrv_unref(blk_bs(it->blk));
529 blk_unref(it->blk);
531 } else {
532 bdrv_unref(it->bs);
535 bdrv_next_reset(it);
539 * Add a BlockBackend into the list of backends referenced by the monitor, with
540 * the given @name acting as the handle for the monitor.
541 * Strictly for use by blockdev.c.
543 * @name must not be null or empty.
545 * Returns true on success and false on failure. In the latter case, an Error
546 * object is returned through @errp.
548 bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
550 assert(!blk->name);
551 assert(name && name[0]);
553 if (!id_wellformed(name)) {
554 error_setg(errp, "Invalid device name");
555 return false;
557 if (blk_by_name(name)) {
558 error_setg(errp, "Device with id '%s' already exists", name);
559 return false;
561 if (bdrv_find_node(name)) {
562 error_setg(errp,
563 "Device name '%s' conflicts with an existing node name",
564 name);
565 return false;
568 blk->name = g_strdup(name);
569 QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
570 return true;
574 * Remove a BlockBackend from the list of backends referenced by the monitor.
575 * Strictly for use by blockdev.c.
577 void monitor_remove_blk(BlockBackend *blk)
579 if (!blk->name) {
580 return;
583 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
584 g_free(blk->name);
585 blk->name = NULL;
589 * Return @blk's name, a non-null string.
590 * Returns an empty string iff @blk is not referenced by the monitor.
592 const char *blk_name(const BlockBackend *blk)
594 return blk->name ?: "";
598 * Return the BlockBackend with name @name if it exists, else null.
599 * @name must not be null.
601 BlockBackend *blk_by_name(const char *name)
603 BlockBackend *blk = NULL;
605 assert(name);
606 while ((blk = blk_next(blk)) != NULL) {
607 if (!strcmp(name, blk->name)) {
608 return blk;
611 return NULL;
615 * Return the BlockDriverState attached to @blk if any, else null.
617 BlockDriverState *blk_bs(BlockBackend *blk)
619 return blk->root ? blk->root->bs : NULL;
622 static BlockBackend *bdrv_first_blk(BlockDriverState *bs)
624 BdrvChild *child;
625 QLIST_FOREACH(child, &bs->parents, next_parent) {
626 if (child->role == &child_root) {
627 return child->opaque;
631 return NULL;
635 * Returns true if @bs has an associated BlockBackend.
637 bool bdrv_has_blk(BlockDriverState *bs)
639 return bdrv_first_blk(bs) != NULL;
643 * Returns true if @bs has only BlockBackends as parents.
645 bool bdrv_is_root_node(BlockDriverState *bs)
647 BdrvChild *c;
649 QLIST_FOREACH(c, &bs->parents, next_parent) {
650 if (c->role != &child_root) {
651 return false;
655 return true;
659 * Return @blk's DriveInfo if any, else null.
661 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
663 return blk->legacy_dinfo;
667 * Set @blk's DriveInfo to @dinfo, and return it.
668 * @blk must not have a DriveInfo set already.
669 * No other BlockBackend may have the same DriveInfo set.
671 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
673 assert(!blk->legacy_dinfo);
674 return blk->legacy_dinfo = dinfo;
678 * Return the BlockBackend with DriveInfo @dinfo.
679 * It must exist.
681 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
683 BlockBackend *blk = NULL;
685 while ((blk = blk_next(blk)) != NULL) {
686 if (blk->legacy_dinfo == dinfo) {
687 return blk;
690 abort();
694 * Returns a pointer to the publicly accessible fields of @blk.
696 BlockBackendPublic *blk_get_public(BlockBackend *blk)
698 return &blk->public;
702 * Returns a BlockBackend given the associated @public fields.
704 BlockBackend *blk_by_public(BlockBackendPublic *public)
706 return container_of(public, BlockBackend, public);
710 * Disassociates the currently associated BlockDriverState from @blk.
712 void blk_remove_bs(BlockBackend *blk)
714 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
715 BlockDriverState *bs;
717 notifier_list_notify(&blk->remove_bs_notifiers, blk);
718 if (tgm->throttle_state) {
719 bs = blk_bs(blk);
720 bdrv_drained_begin(bs);
721 throttle_group_detach_aio_context(tgm);
722 throttle_group_attach_aio_context(tgm, qemu_get_aio_context());
723 bdrv_drained_end(bs);
726 blk_update_root_state(blk);
728 bdrv_root_unref_child(blk->root);
729 blk->root = NULL;
733 * Associates a new BlockDriverState with @blk.
735 int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
737 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
738 blk->root = bdrv_root_attach_child(bs, "root", &child_root,
739 blk->perm, blk->shared_perm, blk, errp);
740 if (blk->root == NULL) {
741 return -EPERM;
743 bdrv_ref(bs);
745 notifier_list_notify(&blk->insert_bs_notifiers, blk);
746 if (tgm->throttle_state) {
747 throttle_group_detach_aio_context(tgm);
748 throttle_group_attach_aio_context(tgm, bdrv_get_aio_context(bs));
751 return 0;
755 * Sets the permission bitmasks that the user of the BlockBackend needs.
757 int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,
758 Error **errp)
760 int ret;
762 if (blk->root && !blk->disable_perm) {
763 ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp);
764 if (ret < 0) {
765 return ret;
769 blk->perm = perm;
770 blk->shared_perm = shared_perm;
772 return 0;
775 void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
777 *perm = blk->perm;
778 *shared_perm = blk->shared_perm;
781 static int blk_do_attach_dev(BlockBackend *blk, void *dev)
783 if (blk->dev) {
784 return -EBUSY;
787 /* While migration is still incoming, we don't need to apply the
788 * permissions of guest device BlockBackends. We might still have a block
789 * job or NBD server writing to the image for storage migration. */
790 if (runstate_check(RUN_STATE_INMIGRATE)) {
791 blk->disable_perm = true;
794 blk_ref(blk);
795 blk->dev = dev;
796 blk->legacy_dev = false;
797 blk_iostatus_reset(blk);
799 return 0;
803 * Attach device model @dev to @blk.
804 * Return 0 on success, -EBUSY when a device model is attached already.
806 int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
808 return blk_do_attach_dev(blk, dev);
812 * Attach device model @dev to @blk.
813 * @blk must not have a device model attached already.
814 * TODO qdevified devices don't use this, remove when devices are qdevified
816 void blk_attach_dev_legacy(BlockBackend *blk, void *dev)
818 if (blk_do_attach_dev(blk, dev) < 0) {
819 abort();
821 blk->legacy_dev = true;
825 * Detach device model @dev from @blk.
826 * @dev must be currently attached to @blk.
828 void blk_detach_dev(BlockBackend *blk, void *dev)
829 /* TODO change to DeviceState *dev when all users are qdevified */
831 assert(blk->dev == dev);
832 blk->dev = NULL;
833 blk->dev_ops = NULL;
834 blk->dev_opaque = NULL;
835 blk->guest_block_size = 512;
836 blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
837 blk_unref(blk);
841 * Return the device model attached to @blk if any, else null.
843 void *blk_get_attached_dev(BlockBackend *blk)
844 /* TODO change to return DeviceState * when all users are qdevified */
846 return blk->dev;
849 /* Return the qdev ID, or if no ID is assigned the QOM path, of the block
850 * device attached to the BlockBackend. */
851 char *blk_get_attached_dev_id(BlockBackend *blk)
853 DeviceState *dev;
855 assert(!blk->legacy_dev);
856 dev = blk->dev;
858 if (!dev) {
859 return g_strdup("");
860 } else if (dev->id) {
861 return g_strdup(dev->id);
863 return object_get_canonical_path(OBJECT(dev));
867 * Return the BlockBackend which has the device model @dev attached if it
868 * exists, else null.
870 * @dev must not be null.
872 BlockBackend *blk_by_dev(void *dev)
874 BlockBackend *blk = NULL;
876 assert(dev != NULL);
877 while ((blk = blk_all_next(blk)) != NULL) {
878 if (blk->dev == dev) {
879 return blk;
882 return NULL;
886 * Set @blk's device model callbacks to @ops.
887 * @opaque is the opaque argument to pass to the callbacks.
888 * This is for use by device models.
890 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
891 void *opaque)
893 /* All drivers that use blk_set_dev_ops() are qdevified and we want to keep
894 * it that way, so we can assume blk->dev, if present, is a DeviceState if
895 * blk->dev_ops is set. Non-device users may use dev_ops without device. */
896 assert(!blk->legacy_dev);
898 blk->dev_ops = ops;
899 blk->dev_opaque = opaque;
901 /* Are we currently quiesced? Should we enforce this right now? */
902 if (blk->quiesce_counter && ops->drained_begin) {
903 ops->drained_begin(opaque);
908 * Notify @blk's attached device model of media change.
910 * If @load is true, notify of media load. This action can fail, meaning that
911 * the medium cannot be loaded. @errp is set then.
913 * If @load is false, notify of media eject. This can never fail.
915 * Also send DEVICE_TRAY_MOVED events as appropriate.
917 void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp)
919 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
920 bool tray_was_open, tray_is_open;
921 Error *local_err = NULL;
923 assert(!blk->legacy_dev);
925 tray_was_open = blk_dev_is_tray_open(blk);
926 blk->dev_ops->change_media_cb(blk->dev_opaque, load, &local_err);
927 if (local_err) {
928 assert(load == true);
929 error_propagate(errp, local_err);
930 return;
932 tray_is_open = blk_dev_is_tray_open(blk);
934 if (tray_was_open != tray_is_open) {
935 char *id = blk_get_attached_dev_id(blk);
936 qapi_event_send_device_tray_moved(blk_name(blk), id, tray_is_open,
937 &error_abort);
938 g_free(id);
943 static void blk_root_change_media(BdrvChild *child, bool load)
945 blk_dev_change_media_cb(child->opaque, load, NULL);
949 * Does @blk's attached device model have removable media?
950 * %true if no device model is attached.
952 bool blk_dev_has_removable_media(BlockBackend *blk)
954 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
958 * Does @blk's attached device model have a tray?
960 bool blk_dev_has_tray(BlockBackend *blk)
962 return blk->dev_ops && blk->dev_ops->is_tray_open;
966 * Notify @blk's attached device model of a media eject request.
967 * If @force is true, the medium is about to be yanked out forcefully.
969 void blk_dev_eject_request(BlockBackend *blk, bool force)
971 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
972 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
977 * Does @blk's attached device model have a tray, and is it open?
979 bool blk_dev_is_tray_open(BlockBackend *blk)
981 if (blk_dev_has_tray(blk)) {
982 return blk->dev_ops->is_tray_open(blk->dev_opaque);
984 return false;
988 * Does @blk's attached device model have the medium locked?
989 * %false if the device model has no such lock.
991 bool blk_dev_is_medium_locked(BlockBackend *blk)
993 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
994 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
996 return false;
1000 * Notify @blk's attached device model of a backend size change.
1002 static void blk_root_resize(BdrvChild *child)
1004 BlockBackend *blk = child->opaque;
1006 if (blk->dev_ops && blk->dev_ops->resize_cb) {
1007 blk->dev_ops->resize_cb(blk->dev_opaque);
1011 void blk_iostatus_enable(BlockBackend *blk)
1013 blk->iostatus_enabled = true;
1014 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1017 /* The I/O status is only enabled if the drive explicitly
1018 * enables it _and_ the VM is configured to stop on errors */
1019 bool blk_iostatus_is_enabled(const BlockBackend *blk)
1021 return (blk->iostatus_enabled &&
1022 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
1023 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
1024 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
1027 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
1029 return blk->iostatus;
1032 void blk_iostatus_disable(BlockBackend *blk)
1034 blk->iostatus_enabled = false;
1037 void blk_iostatus_reset(BlockBackend *blk)
1039 if (blk_iostatus_is_enabled(blk)) {
1040 BlockDriverState *bs = blk_bs(blk);
1041 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1042 if (bs && bs->job) {
1043 block_job_iostatus_reset(bs->job);
1048 void blk_iostatus_set_err(BlockBackend *blk, int error)
1050 assert(blk_iostatus_is_enabled(blk));
1051 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
1052 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
1053 BLOCK_DEVICE_IO_STATUS_FAILED;
1057 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
1059 blk->allow_write_beyond_eof = allow;
1062 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
1063 size_t size)
1065 int64_t len;
1067 if (size > INT_MAX) {
1068 return -EIO;
1071 if (!blk_is_available(blk)) {
1072 return -ENOMEDIUM;
1075 if (offset < 0) {
1076 return -EIO;
1079 if (!blk->allow_write_beyond_eof) {
1080 len = blk_getlength(blk);
1081 if (len < 0) {
1082 return len;
1085 if (offset > len || len - offset < size) {
1086 return -EIO;
1090 return 0;
1093 int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
1094 unsigned int bytes, QEMUIOVector *qiov,
1095 BdrvRequestFlags flags)
1097 int ret;
1098 BlockDriverState *bs = blk_bs(blk);
1100 trace_blk_co_preadv(blk, bs, offset, bytes, flags);
1102 ret = blk_check_byte_request(blk, offset, bytes);
1103 if (ret < 0) {
1104 return ret;
1107 bdrv_inc_in_flight(bs);
1109 /* throttling disk I/O */
1110 if (blk->public.throttle_group_member.throttle_state) {
1111 throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1112 bytes, false);
1115 ret = bdrv_co_preadv(blk->root, offset, bytes, qiov, flags);
1116 bdrv_dec_in_flight(bs);
1117 return ret;
1120 int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
1121 unsigned int bytes, QEMUIOVector *qiov,
1122 BdrvRequestFlags flags)
1124 int ret;
1125 BlockDriverState *bs = blk_bs(blk);
1127 trace_blk_co_pwritev(blk, bs, offset, bytes, flags);
1129 ret = blk_check_byte_request(blk, offset, bytes);
1130 if (ret < 0) {
1131 return ret;
1134 bdrv_inc_in_flight(bs);
1135 /* throttling disk I/O */
1136 if (blk->public.throttle_group_member.throttle_state) {
1137 throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1138 bytes, true);
1141 if (!blk->enable_write_cache) {
1142 flags |= BDRV_REQ_FUA;
1145 ret = bdrv_co_pwritev(blk->root, offset, bytes, qiov, flags);
1146 bdrv_dec_in_flight(bs);
1147 return ret;
1150 typedef struct BlkRwCo {
1151 BlockBackend *blk;
1152 int64_t offset;
1153 QEMUIOVector *qiov;
1154 int ret;
1155 BdrvRequestFlags flags;
1156 } BlkRwCo;
1158 static void blk_read_entry(void *opaque)
1160 BlkRwCo *rwco = opaque;
1162 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
1163 rwco->qiov, rwco->flags);
1166 static void blk_write_entry(void *opaque)
1168 BlkRwCo *rwco = opaque;
1170 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
1171 rwco->qiov, rwco->flags);
1174 static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
1175 int64_t bytes, CoroutineEntry co_entry,
1176 BdrvRequestFlags flags)
1178 QEMUIOVector qiov;
1179 struct iovec iov;
1180 BlkRwCo rwco;
1182 iov = (struct iovec) {
1183 .iov_base = buf,
1184 .iov_len = bytes,
1186 qemu_iovec_init_external(&qiov, &iov, 1);
1188 rwco = (BlkRwCo) {
1189 .blk = blk,
1190 .offset = offset,
1191 .qiov = &qiov,
1192 .flags = flags,
1193 .ret = NOT_DONE,
1196 if (qemu_in_coroutine()) {
1197 /* Fast-path if already in coroutine context */
1198 co_entry(&rwco);
1199 } else {
1200 Coroutine *co = qemu_coroutine_create(co_entry, &rwco);
1201 bdrv_coroutine_enter(blk_bs(blk), co);
1202 BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE);
1205 return rwco.ret;
1208 int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf,
1209 int count)
1211 int ret;
1213 ret = blk_check_byte_request(blk, offset, count);
1214 if (ret < 0) {
1215 return ret;
1218 blk_root_drained_begin(blk->root);
1219 ret = blk_pread(blk, offset, buf, count);
1220 blk_root_drained_end(blk->root);
1221 return ret;
1224 int blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1225 int bytes, BdrvRequestFlags flags)
1227 return blk_prw(blk, offset, NULL, bytes, blk_write_entry,
1228 flags | BDRV_REQ_ZERO_WRITE);
1231 int blk_make_zero(BlockBackend *blk, BdrvRequestFlags flags)
1233 return bdrv_make_zero(blk->root, flags);
1236 static void blk_inc_in_flight(BlockBackend *blk)
1238 atomic_inc(&blk->in_flight);
1241 static void blk_dec_in_flight(BlockBackend *blk)
1243 atomic_dec(&blk->in_flight);
1244 aio_wait_kick(&blk->wait);
1247 static void error_callback_bh(void *opaque)
1249 struct BlockBackendAIOCB *acb = opaque;
1251 blk_dec_in_flight(acb->blk);
1252 acb->common.cb(acb->common.opaque, acb->ret);
1253 qemu_aio_unref(acb);
1256 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
1257 BlockCompletionFunc *cb,
1258 void *opaque, int ret)
1260 struct BlockBackendAIOCB *acb;
1262 blk_inc_in_flight(blk);
1263 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
1264 acb->blk = blk;
1265 acb->ret = ret;
1267 aio_bh_schedule_oneshot(blk_get_aio_context(blk), error_callback_bh, acb);
1268 return &acb->common;
1271 typedef struct BlkAioEmAIOCB {
1272 BlockAIOCB common;
1273 BlkRwCo rwco;
1274 int bytes;
1275 bool has_returned;
1276 } BlkAioEmAIOCB;
1278 static const AIOCBInfo blk_aio_em_aiocb_info = {
1279 .aiocb_size = sizeof(BlkAioEmAIOCB),
1282 static void blk_aio_complete(BlkAioEmAIOCB *acb)
1284 if (acb->has_returned) {
1285 blk_dec_in_flight(acb->rwco.blk);
1286 acb->common.cb(acb->common.opaque, acb->rwco.ret);
1287 qemu_aio_unref(acb);
1291 static void blk_aio_complete_bh(void *opaque)
1293 BlkAioEmAIOCB *acb = opaque;
1294 assert(acb->has_returned);
1295 blk_aio_complete(acb);
1298 static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
1299 QEMUIOVector *qiov, CoroutineEntry co_entry,
1300 BdrvRequestFlags flags,
1301 BlockCompletionFunc *cb, void *opaque)
1303 BlkAioEmAIOCB *acb;
1304 Coroutine *co;
1306 blk_inc_in_flight(blk);
1307 acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
1308 acb->rwco = (BlkRwCo) {
1309 .blk = blk,
1310 .offset = offset,
1311 .qiov = qiov,
1312 .flags = flags,
1313 .ret = NOT_DONE,
1315 acb->bytes = bytes;
1316 acb->has_returned = false;
1318 co = qemu_coroutine_create(co_entry, acb);
1319 bdrv_coroutine_enter(blk_bs(blk), co);
1321 acb->has_returned = true;
1322 if (acb->rwco.ret != NOT_DONE) {
1323 aio_bh_schedule_oneshot(blk_get_aio_context(blk),
1324 blk_aio_complete_bh, acb);
1327 return &acb->common;
1330 static void blk_aio_read_entry(void *opaque)
1332 BlkAioEmAIOCB *acb = opaque;
1333 BlkRwCo *rwco = &acb->rwco;
1335 assert(rwco->qiov->size == acb->bytes);
1336 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes,
1337 rwco->qiov, rwco->flags);
1338 blk_aio_complete(acb);
1341 static void blk_aio_write_entry(void *opaque)
1343 BlkAioEmAIOCB *acb = opaque;
1344 BlkRwCo *rwco = &acb->rwco;
1346 assert(!rwco->qiov || rwco->qiov->size == acb->bytes);
1347 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes,
1348 rwco->qiov, rwco->flags);
1349 blk_aio_complete(acb);
1352 BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1353 int count, BdrvRequestFlags flags,
1354 BlockCompletionFunc *cb, void *opaque)
1356 return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
1357 flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
1360 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
1362 int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
1363 if (ret < 0) {
1364 return ret;
1366 return count;
1369 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
1370 BdrvRequestFlags flags)
1372 int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
1373 flags);
1374 if (ret < 0) {
1375 return ret;
1377 return count;
1380 int64_t blk_getlength(BlockBackend *blk)
1382 if (!blk_is_available(blk)) {
1383 return -ENOMEDIUM;
1386 return bdrv_getlength(blk_bs(blk));
1389 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
1391 if (!blk_bs(blk)) {
1392 *nb_sectors_ptr = 0;
1393 } else {
1394 bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
1398 int64_t blk_nb_sectors(BlockBackend *blk)
1400 if (!blk_is_available(blk)) {
1401 return -ENOMEDIUM;
1404 return bdrv_nb_sectors(blk_bs(blk));
1407 BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
1408 QEMUIOVector *qiov, BdrvRequestFlags flags,
1409 BlockCompletionFunc *cb, void *opaque)
1411 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1412 blk_aio_read_entry, flags, cb, opaque);
1415 BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
1416 QEMUIOVector *qiov, BdrvRequestFlags flags,
1417 BlockCompletionFunc *cb, void *opaque)
1419 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1420 blk_aio_write_entry, flags, cb, opaque);
1423 static void blk_aio_flush_entry(void *opaque)
1425 BlkAioEmAIOCB *acb = opaque;
1426 BlkRwCo *rwco = &acb->rwco;
1428 rwco->ret = blk_co_flush(rwco->blk);
1429 blk_aio_complete(acb);
1432 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1433 BlockCompletionFunc *cb, void *opaque)
1435 return blk_aio_prwv(blk, 0, 0, NULL, blk_aio_flush_entry, 0, cb, opaque);
1438 static void blk_aio_pdiscard_entry(void *opaque)
1440 BlkAioEmAIOCB *acb = opaque;
1441 BlkRwCo *rwco = &acb->rwco;
1443 rwco->ret = blk_co_pdiscard(rwco->blk, rwco->offset, acb->bytes);
1444 blk_aio_complete(acb);
1447 BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk,
1448 int64_t offset, int bytes,
1449 BlockCompletionFunc *cb, void *opaque)
1451 return blk_aio_prwv(blk, offset, bytes, NULL, blk_aio_pdiscard_entry, 0,
1452 cb, opaque);
1455 void blk_aio_cancel(BlockAIOCB *acb)
1457 bdrv_aio_cancel(acb);
1460 void blk_aio_cancel_async(BlockAIOCB *acb)
1462 bdrv_aio_cancel_async(acb);
1465 int blk_co_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1467 if (!blk_is_available(blk)) {
1468 return -ENOMEDIUM;
1471 return bdrv_co_ioctl(blk_bs(blk), req, buf);
1474 static void blk_ioctl_entry(void *opaque)
1476 BlkRwCo *rwco = opaque;
1477 rwco->ret = blk_co_ioctl(rwco->blk, rwco->offset,
1478 rwco->qiov->iov[0].iov_base);
1481 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1483 return blk_prw(blk, req, buf, 0, blk_ioctl_entry, 0);
1486 static void blk_aio_ioctl_entry(void *opaque)
1488 BlkAioEmAIOCB *acb = opaque;
1489 BlkRwCo *rwco = &acb->rwco;
1491 rwco->ret = blk_co_ioctl(rwco->blk, rwco->offset,
1492 rwco->qiov->iov[0].iov_base);
1493 blk_aio_complete(acb);
1496 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
1497 BlockCompletionFunc *cb, void *opaque)
1499 QEMUIOVector qiov;
1500 struct iovec iov;
1502 iov = (struct iovec) {
1503 .iov_base = buf,
1504 .iov_len = 0,
1506 qemu_iovec_init_external(&qiov, &iov, 1);
1508 return blk_aio_prwv(blk, req, 0, &qiov, blk_aio_ioctl_entry, 0, cb, opaque);
1511 int blk_co_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1513 int ret = blk_check_byte_request(blk, offset, bytes);
1514 if (ret < 0) {
1515 return ret;
1518 return bdrv_co_pdiscard(blk_bs(blk), offset, bytes);
1521 int blk_co_flush(BlockBackend *blk)
1523 if (!blk_is_available(blk)) {
1524 return -ENOMEDIUM;
1527 return bdrv_co_flush(blk_bs(blk));
1530 static void blk_flush_entry(void *opaque)
1532 BlkRwCo *rwco = opaque;
1533 rwco->ret = blk_co_flush(rwco->blk);
1536 int blk_flush(BlockBackend *blk)
1538 return blk_prw(blk, 0, NULL, 0, blk_flush_entry, 0);
1541 void blk_drain(BlockBackend *blk)
1543 BlockDriverState *bs = blk_bs(blk);
1545 if (bs) {
1546 bdrv_drained_begin(bs);
1549 /* We may have -ENOMEDIUM completions in flight */
1550 AIO_WAIT_WHILE(&blk->wait,
1551 blk_get_aio_context(blk),
1552 atomic_mb_read(&blk->in_flight) > 0);
1554 if (bs) {
1555 bdrv_drained_end(bs);
1559 void blk_drain_all(void)
1561 BlockBackend *blk = NULL;
1563 bdrv_drain_all_begin();
1565 while ((blk = blk_all_next(blk)) != NULL) {
1566 AioContext *ctx = blk_get_aio_context(blk);
1568 aio_context_acquire(ctx);
1570 /* We may have -ENOMEDIUM completions in flight */
1571 AIO_WAIT_WHILE(&blk->wait, ctx,
1572 atomic_mb_read(&blk->in_flight) > 0);
1574 aio_context_release(ctx);
1577 bdrv_drain_all_end();
1580 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1581 BlockdevOnError on_write_error)
1583 blk->on_read_error = on_read_error;
1584 blk->on_write_error = on_write_error;
1587 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
1589 return is_read ? blk->on_read_error : blk->on_write_error;
1592 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
1593 int error)
1595 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1597 switch (on_err) {
1598 case BLOCKDEV_ON_ERROR_ENOSPC:
1599 return (error == ENOSPC) ?
1600 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1601 case BLOCKDEV_ON_ERROR_STOP:
1602 return BLOCK_ERROR_ACTION_STOP;
1603 case BLOCKDEV_ON_ERROR_REPORT:
1604 return BLOCK_ERROR_ACTION_REPORT;
1605 case BLOCKDEV_ON_ERROR_IGNORE:
1606 return BLOCK_ERROR_ACTION_IGNORE;
1607 case BLOCKDEV_ON_ERROR_AUTO:
1608 default:
1609 abort();
1613 static void send_qmp_error_event(BlockBackend *blk,
1614 BlockErrorAction action,
1615 bool is_read, int error)
1617 IoOperationType optype;
1618 BlockDriverState *bs = blk_bs(blk);
1620 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
1621 qapi_event_send_block_io_error(blk_name(blk), !!bs,
1622 bs ? bdrv_get_node_name(bs) : NULL, optype,
1623 action, blk_iostatus_is_enabled(blk),
1624 error == ENOSPC, strerror(error),
1625 &error_abort);
1628 /* This is done by device models because, while the block layer knows
1629 * about the error, it does not know whether an operation comes from
1630 * the device or the block layer (from a job, for example).
1632 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1633 bool is_read, int error)
1635 assert(error >= 0);
1637 if (action == BLOCK_ERROR_ACTION_STOP) {
1638 /* First set the iostatus, so that "info block" returns an iostatus
1639 * that matches the events raised so far (an additional error iostatus
1640 * is fine, but not a lost one).
1642 blk_iostatus_set_err(blk, error);
1644 /* Then raise the request to stop the VM and the event.
1645 * qemu_system_vmstop_request_prepare has two effects. First,
1646 * it ensures that the STOP event always comes after the
1647 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1648 * can observe the STOP event and do a "cont" before the STOP
1649 * event is issued, the VM will not stop. In this case, vm_start()
1650 * also ensures that the STOP/RESUME pair of events is emitted.
1652 qemu_system_vmstop_request_prepare();
1653 send_qmp_error_event(blk, action, is_read, error);
1654 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1655 } else {
1656 send_qmp_error_event(blk, action, is_read, error);
1660 int blk_is_read_only(BlockBackend *blk)
1662 BlockDriverState *bs = blk_bs(blk);
1664 if (bs) {
1665 return bdrv_is_read_only(bs);
1666 } else {
1667 return blk->root_state.read_only;
1671 int blk_is_sg(BlockBackend *blk)
1673 BlockDriverState *bs = blk_bs(blk);
1675 if (!bs) {
1676 return 0;
1679 return bdrv_is_sg(bs);
1682 int blk_enable_write_cache(BlockBackend *blk)
1684 return blk->enable_write_cache;
1687 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1689 blk->enable_write_cache = wce;
1692 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1694 BlockDriverState *bs = blk_bs(blk);
1696 if (!bs) {
1697 error_setg(errp, "Device '%s' has no medium", blk->name);
1698 return;
1701 bdrv_invalidate_cache(bs, errp);
1704 bool blk_is_inserted(BlockBackend *blk)
1706 BlockDriverState *bs = blk_bs(blk);
1708 return bs && bdrv_is_inserted(bs);
1711 bool blk_is_available(BlockBackend *blk)
1713 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1716 void blk_lock_medium(BlockBackend *blk, bool locked)
1718 BlockDriverState *bs = blk_bs(blk);
1720 if (bs) {
1721 bdrv_lock_medium(bs, locked);
1725 void blk_eject(BlockBackend *blk, bool eject_flag)
1727 BlockDriverState *bs = blk_bs(blk);
1728 char *id;
1730 /* blk_eject is only called by qdevified devices */
1731 assert(!blk->legacy_dev);
1733 if (bs) {
1734 bdrv_eject(bs, eject_flag);
1737 /* Whether or not we ejected on the backend,
1738 * the frontend experienced a tray event. */
1739 id = blk_get_attached_dev_id(blk);
1740 qapi_event_send_device_tray_moved(blk_name(blk), id,
1741 eject_flag, &error_abort);
1742 g_free(id);
1745 int blk_get_flags(BlockBackend *blk)
1747 BlockDriverState *bs = blk_bs(blk);
1749 if (bs) {
1750 return bdrv_get_flags(bs);
1751 } else {
1752 return blk->root_state.open_flags;
1756 /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
1757 uint32_t blk_get_max_transfer(BlockBackend *blk)
1759 BlockDriverState *bs = blk_bs(blk);
1760 uint32_t max = 0;
1762 if (bs) {
1763 max = bs->bl.max_transfer;
1765 return MIN_NON_ZERO(max, INT_MAX);
1768 int blk_get_max_iov(BlockBackend *blk)
1770 return blk->root->bs->bl.max_iov;
1773 void blk_set_guest_block_size(BlockBackend *blk, int align)
1775 blk->guest_block_size = align;
1778 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1780 return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
1783 void *blk_blockalign(BlockBackend *blk, size_t size)
1785 return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
1788 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1790 BlockDriverState *bs = blk_bs(blk);
1792 if (!bs) {
1793 return false;
1796 return bdrv_op_is_blocked(bs, op, errp);
1799 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1801 BlockDriverState *bs = blk_bs(blk);
1803 if (bs) {
1804 bdrv_op_unblock(bs, op, reason);
1808 void blk_op_block_all(BlockBackend *blk, Error *reason)
1810 BlockDriverState *bs = blk_bs(blk);
1812 if (bs) {
1813 bdrv_op_block_all(bs, reason);
1817 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1819 BlockDriverState *bs = blk_bs(blk);
1821 if (bs) {
1822 bdrv_op_unblock_all(bs, reason);
1826 AioContext *blk_get_aio_context(BlockBackend *blk)
1828 BlockDriverState *bs = blk_bs(blk);
1830 if (bs) {
1831 return bdrv_get_aio_context(bs);
1832 } else {
1833 return qemu_get_aio_context();
1837 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1839 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1840 return blk_get_aio_context(blk_acb->blk);
1843 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1845 BlockDriverState *bs = blk_bs(blk);
1846 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
1848 if (bs) {
1849 if (tgm->throttle_state) {
1850 bdrv_drained_begin(bs);
1851 throttle_group_detach_aio_context(tgm);
1852 throttle_group_attach_aio_context(tgm, new_context);
1853 bdrv_drained_end(bs);
1855 bdrv_set_aio_context(bs, new_context);
1859 void blk_add_aio_context_notifier(BlockBackend *blk,
1860 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1861 void (*detach_aio_context)(void *opaque), void *opaque)
1863 BlockDriverState *bs = blk_bs(blk);
1865 if (bs) {
1866 bdrv_add_aio_context_notifier(bs, attached_aio_context,
1867 detach_aio_context, opaque);
1871 void blk_remove_aio_context_notifier(BlockBackend *blk,
1872 void (*attached_aio_context)(AioContext *,
1873 void *),
1874 void (*detach_aio_context)(void *),
1875 void *opaque)
1877 BlockDriverState *bs = blk_bs(blk);
1879 if (bs) {
1880 bdrv_remove_aio_context_notifier(bs, attached_aio_context,
1881 detach_aio_context, opaque);
1885 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1887 notifier_list_add(&blk->remove_bs_notifiers, notify);
1890 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1892 notifier_list_add(&blk->insert_bs_notifiers, notify);
1895 void blk_io_plug(BlockBackend *blk)
1897 BlockDriverState *bs = blk_bs(blk);
1899 if (bs) {
1900 bdrv_io_plug(bs);
1904 void blk_io_unplug(BlockBackend *blk)
1906 BlockDriverState *bs = blk_bs(blk);
1908 if (bs) {
1909 bdrv_io_unplug(bs);
1913 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1915 return &blk->stats;
1918 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1919 BlockCompletionFunc *cb, void *opaque)
1921 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1924 int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1925 int bytes, BdrvRequestFlags flags)
1927 return blk_co_pwritev(blk, offset, bytes, NULL,
1928 flags | BDRV_REQ_ZERO_WRITE);
1931 int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
1932 int count)
1934 return blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
1935 BDRV_REQ_WRITE_COMPRESSED);
1938 int blk_truncate(BlockBackend *blk, int64_t offset, PreallocMode prealloc,
1939 Error **errp)
1941 if (!blk_is_available(blk)) {
1942 error_setg(errp, "No medium inserted");
1943 return -ENOMEDIUM;
1946 return bdrv_truncate(blk->root, offset, prealloc, errp);
1949 static void blk_pdiscard_entry(void *opaque)
1951 BlkRwCo *rwco = opaque;
1952 rwco->ret = blk_co_pdiscard(rwco->blk, rwco->offset, rwco->qiov->size);
1955 int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1957 return blk_prw(blk, offset, NULL, bytes, blk_pdiscard_entry, 0);
1960 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1961 int64_t pos, int size)
1963 int ret;
1965 if (!blk_is_available(blk)) {
1966 return -ENOMEDIUM;
1969 ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
1970 if (ret < 0) {
1971 return ret;
1974 if (ret == size && !blk->enable_write_cache) {
1975 ret = bdrv_flush(blk_bs(blk));
1978 return ret < 0 ? ret : size;
1981 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1983 if (!blk_is_available(blk)) {
1984 return -ENOMEDIUM;
1987 return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
1990 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1992 if (!blk_is_available(blk)) {
1993 return -ENOMEDIUM;
1996 return bdrv_probe_blocksizes(blk_bs(blk), bsz);
1999 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
2001 if (!blk_is_available(blk)) {
2002 return -ENOMEDIUM;
2005 return bdrv_probe_geometry(blk_bs(blk), geo);
2009 * Updates the BlockBackendRootState object with data from the currently
2010 * attached BlockDriverState.
2012 void blk_update_root_state(BlockBackend *blk)
2014 assert(blk->root);
2016 blk->root_state.open_flags = blk->root->bs->open_flags;
2017 blk->root_state.read_only = blk->root->bs->read_only;
2018 blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
2022 * Returns the detect-zeroes setting to be used for bdrv_open() of a
2023 * BlockDriverState which is supposed to inherit the root state.
2025 bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)
2027 return blk->root_state.detect_zeroes;
2031 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
2032 * supposed to inherit the root state.
2034 int blk_get_open_flags_from_root_state(BlockBackend *blk)
2036 int bs_flags;
2038 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
2039 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
2041 return bs_flags;
2044 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
2046 return &blk->root_state;
2049 int blk_commit_all(void)
2051 BlockBackend *blk = NULL;
2053 while ((blk = blk_all_next(blk)) != NULL) {
2054 AioContext *aio_context = blk_get_aio_context(blk);
2056 aio_context_acquire(aio_context);
2057 if (blk_is_inserted(blk) && blk->root->bs->backing) {
2058 int ret = bdrv_commit(blk->root->bs);
2059 if (ret < 0) {
2060 aio_context_release(aio_context);
2061 return ret;
2064 aio_context_release(aio_context);
2066 return 0;
2070 /* throttling disk I/O limits */
2071 void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
2073 throttle_group_config(&blk->public.throttle_group_member, cfg);
2076 void blk_io_limits_disable(BlockBackend *blk)
2078 BlockDriverState *bs = blk_bs(blk);
2079 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
2080 assert(tgm->throttle_state);
2081 if (bs) {
2082 bdrv_drained_begin(bs);
2084 throttle_group_unregister_tgm(tgm);
2085 if (bs) {
2086 bdrv_drained_end(bs);
2090 /* should be called before blk_set_io_limits if a limit is set */
2091 void blk_io_limits_enable(BlockBackend *blk, const char *group)
2093 assert(!blk->public.throttle_group_member.throttle_state);
2094 throttle_group_register_tgm(&blk->public.throttle_group_member,
2095 group, blk_get_aio_context(blk));
2098 void blk_io_limits_update_group(BlockBackend *blk, const char *group)
2100 /* this BB is not part of any group */
2101 if (!blk->public.throttle_group_member.throttle_state) {
2102 return;
2105 /* this BB is a part of the same group than the one we want */
2106 if (!g_strcmp0(throttle_group_get_name(&blk->public.throttle_group_member),
2107 group)) {
2108 return;
2111 /* need to change the group this bs belong to */
2112 blk_io_limits_disable(blk);
2113 blk_io_limits_enable(blk, group);
2116 static void blk_root_drained_begin(BdrvChild *child)
2118 BlockBackend *blk = child->opaque;
2120 if (++blk->quiesce_counter == 1) {
2121 if (blk->dev_ops && blk->dev_ops->drained_begin) {
2122 blk->dev_ops->drained_begin(blk->dev_opaque);
2126 /* Note that blk->root may not be accessible here yet if we are just
2127 * attaching to a BlockDriverState that is drained. Use child instead. */
2129 if (atomic_fetch_inc(&blk->public.throttle_group_member.io_limits_disabled) == 0) {
2130 throttle_group_restart_tgm(&blk->public.throttle_group_member);
2134 static void blk_root_drained_end(BdrvChild *child)
2136 BlockBackend *blk = child->opaque;
2137 assert(blk->quiesce_counter);
2139 assert(blk->public.throttle_group_member.io_limits_disabled);
2140 atomic_dec(&blk->public.throttle_group_member.io_limits_disabled);
2142 if (--blk->quiesce_counter == 0) {
2143 if (blk->dev_ops && blk->dev_ops->drained_end) {
2144 blk->dev_ops->drained_end(blk->dev_opaque);
2149 void blk_register_buf(BlockBackend *blk, void *host, size_t size)
2151 bdrv_register_buf(blk_bs(blk), host, size);
2154 void blk_unregister_buf(BlockBackend *blk, void *host)
2156 bdrv_unregister_buf(blk_bs(blk), host);