4 * Copyright (C) 2014-2016 Red Hat, Inc.
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-event.h"
21 #include "qapi/error.h"
24 #include "migration/misc.h"
26 /* Number of coroutines to reserve per attached device model */
27 #define COROUTINE_POOL_RESERVATION 64
29 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
31 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
37 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
38 QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */
39 QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
40 BlockBackendPublic public;
42 void *dev; /* attached device model, if any */
43 bool legacy_dev; /* true if dev is not a DeviceState */
44 /* TODO change to DeviceState when all users are qdevified */
45 const BlockDevOps *dev_ops;
48 /* the block size for which the guest device expects atomicity */
51 /* If the BDS tree is removed, some of its options are stored here (which
52 * can be used to restore those options in the new BDS on insert) */
53 BlockBackendRootState root_state;
55 bool enable_write_cache;
57 /* I/O stats (display with "info blockstats"). */
60 BlockdevOnError on_read_error, on_write_error;
61 bool iostatus_enabled;
62 BlockDeviceIoStatus iostatus;
68 bool allow_write_beyond_eof;
70 NotifierList remove_bs_notifiers, insert_bs_notifiers;
73 VMChangeStateEntry *vmsh;
74 bool force_allow_inactivate;
77 typedef struct BlockBackendAIOCB {
83 static const AIOCBInfo block_backend_aiocb_info = {
84 .get_aio_context = blk_aiocb_get_aio_context,
85 .aiocb_size = sizeof(BlockBackendAIOCB),
88 static void drive_info_del(DriveInfo *dinfo);
89 static BlockBackend *bdrv_first_blk(BlockDriverState *bs);
91 /* All BlockBackends */
92 static QTAILQ_HEAD(, BlockBackend) block_backends =
93 QTAILQ_HEAD_INITIALIZER(block_backends);
95 /* All BlockBackends referenced by the monitor and which are iterated through by
97 static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
98 QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
100 static void blk_root_inherit_options(int *child_flags, QDict *child_options,
101 int parent_flags, QDict *parent_options)
103 /* We're not supposed to call this function for root nodes */
106 static void blk_root_drained_begin(BdrvChild *child);
107 static void blk_root_drained_end(BdrvChild *child);
109 static void blk_root_change_media(BdrvChild *child, bool load);
110 static void blk_root_resize(BdrvChild *child);
112 static char *blk_root_get_parent_desc(BdrvChild *child)
114 BlockBackend *blk = child->opaque;
118 return g_strdup(blk->name);
121 dev_id = blk_get_attached_dev_id(blk);
125 /* TODO Callback into the BB owner for something more detailed */
127 return g_strdup("a block device");
131 static const char *blk_root_get_name(BdrvChild *child)
133 return blk_name(child->opaque);
136 static void blk_vm_state_changed(void *opaque, int running, RunState state)
138 Error *local_err = NULL;
139 BlockBackend *blk = opaque;
141 if (state == RUN_STATE_INMIGRATE) {
145 qemu_del_vm_change_state_handler(blk->vmsh);
147 blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
149 error_report_err(local_err);
154 * Notifies the user of the BlockBackend that migration has completed. qdev
155 * devices can tighten their permissions in response (specifically revoke
156 * shared write permissions that we needed for storage migration).
158 * If an error is returned, the VM cannot be allowed to be resumed.
160 static void blk_root_activate(BdrvChild *child, Error **errp)
162 BlockBackend *blk = child->opaque;
163 Error *local_err = NULL;
165 if (!blk->disable_perm) {
169 blk->disable_perm = false;
171 blk_set_perm(blk, blk->perm, BLK_PERM_ALL, &local_err);
173 error_propagate(errp, local_err);
174 blk->disable_perm = true;
178 if (runstate_check(RUN_STATE_INMIGRATE)) {
179 /* Activation can happen when migration process is still active, for
180 * example when nbd_server_add is called during non-shared storage
181 * migration. Defer the shared_perm update to migration completion. */
183 blk->vmsh = qemu_add_vm_change_state_handler(blk_vm_state_changed,
189 blk_set_perm(blk, blk->perm, blk->shared_perm, &local_err);
191 error_propagate(errp, local_err);
192 blk->disable_perm = true;
197 void blk_set_force_allow_inactivate(BlockBackend *blk)
199 blk->force_allow_inactivate = true;
202 static bool blk_can_inactivate(BlockBackend *blk)
204 /* If it is a guest device, inactivate is ok. */
205 if (blk->dev || blk_name(blk)[0]) {
209 /* Inactivating means no more writes to the image can be done,
210 * even if those writes would be changes invisible to the
211 * guest. For block job BBs that satisfy this, we can just allow
212 * it. This is the case for mirror job source, which is required
213 * by libvirt non-shared block migration. */
214 if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) {
218 return blk->force_allow_inactivate;
221 static int blk_root_inactivate(BdrvChild *child)
223 BlockBackend *blk = child->opaque;
225 if (blk->disable_perm) {
229 if (!blk_can_inactivate(blk)) {
233 blk->disable_perm = true;
235 bdrv_child_try_set_perm(blk->root, 0, BLK_PERM_ALL, &error_abort);
241 static const BdrvChildRole child_root = {
242 .inherit_options = blk_root_inherit_options,
244 .change_media = blk_root_change_media,
245 .resize = blk_root_resize,
246 .get_name = blk_root_get_name,
247 .get_parent_desc = blk_root_get_parent_desc,
249 .drained_begin = blk_root_drained_begin,
250 .drained_end = blk_root_drained_end,
252 .activate = blk_root_activate,
253 .inactivate = blk_root_inactivate,
257 * Create a new BlockBackend with a reference count of one.
259 * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
260 * to request for a block driver node that is attached to this BlockBackend.
261 * @shared_perm is a bitmask which describes which permissions may be granted
262 * to other users of the attached node.
263 * Both sets of permissions can be changed later using blk_set_perm().
265 * Return the new BlockBackend on success, null on failure.
267 BlockBackend *blk_new(uint64_t perm, uint64_t shared_perm)
271 blk = g_new0(BlockBackend, 1);
274 blk->shared_perm = shared_perm;
275 blk_set_enable_write_cache(blk, true);
277 block_acct_init(&blk->stats);
279 notifier_list_init(&blk->remove_bs_notifiers);
280 notifier_list_init(&blk->insert_bs_notifiers);
282 QTAILQ_INSERT_TAIL(&block_backends, blk, link);
287 * Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
289 * Just as with bdrv_open(), after having called this function the reference to
290 * @options belongs to the block layer (even on failure).
292 * TODO: Remove @filename and @flags; it should be possible to specify a whole
293 * BDS tree just by specifying the @options QDict (or @reference,
294 * alternatively). At the time of adding this function, this is not possible,
295 * though, so callers of this function have to be able to specify @filename and
298 BlockBackend *blk_new_open(const char *filename, const char *reference,
299 QDict *options, int flags, Error **errp)
302 BlockDriverState *bs;
305 /* blk_new_open() is mainly used in .bdrv_create implementations and the
306 * tools where sharing isn't a concern because the BDS stays private, so we
307 * just request permission according to the flags.
309 * The exceptions are xen_disk and blockdev_init(); in these cases, the
310 * caller of blk_new_open() doesn't make use of the permissions, but they
311 * shouldn't hurt either. We can still share everything here because the
312 * guest devices will add their own blockers if they can't share. */
313 if ((flags & BDRV_O_NO_IO) == 0) {
314 perm |= BLK_PERM_CONSISTENT_READ;
315 if (flags & BDRV_O_RDWR) {
316 perm |= BLK_PERM_WRITE;
319 if (flags & BDRV_O_RESIZE) {
320 perm |= BLK_PERM_RESIZE;
323 blk = blk_new(perm, BLK_PERM_ALL);
324 bs = bdrv_open(filename, reference, options, flags, errp);
330 blk->root = bdrv_root_attach_child(bs, "root", &child_root,
331 perm, BLK_PERM_ALL, blk, errp);
341 static void blk_delete(BlockBackend *blk)
343 assert(!blk->refcnt);
346 if (blk->public.throttle_group_member.throttle_state) {
347 blk_io_limits_disable(blk);
353 qemu_del_vm_change_state_handler(blk->vmsh);
356 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
357 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
358 QTAILQ_REMOVE(&block_backends, blk, link);
359 drive_info_del(blk->legacy_dinfo);
360 block_acct_cleanup(&blk->stats);
364 static void drive_info_del(DriveInfo *dinfo)
369 qemu_opts_del(dinfo->opts);
370 g_free(dinfo->serial);
374 int blk_get_refcnt(BlockBackend *blk)
376 return blk ? blk->refcnt : 0;
380 * Increment @blk's reference count.
381 * @blk must not be null.
383 void blk_ref(BlockBackend *blk)
389 * Decrement @blk's reference count.
390 * If this drops it to zero, destroy @blk.
391 * For convenience, do nothing if @blk is null.
393 void blk_unref(BlockBackend *blk)
396 assert(blk->refcnt > 0);
397 if (!--blk->refcnt) {
404 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
405 * ones which are hidden (i.e. are not referenced by the monitor).
407 BlockBackend *blk_all_next(BlockBackend *blk)
409 return blk ? QTAILQ_NEXT(blk, link)
410 : QTAILQ_FIRST(&block_backends);
413 void blk_remove_all_bs(void)
415 BlockBackend *blk = NULL;
417 while ((blk = blk_all_next(blk)) != NULL) {
418 AioContext *ctx = blk_get_aio_context(blk);
420 aio_context_acquire(ctx);
424 aio_context_release(ctx);
429 * Return the monitor-owned BlockBackend after @blk.
430 * If @blk is null, return the first one.
431 * Else, return @blk's next sibling, which may be null.
433 * To iterate over all BlockBackends, do
434 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
438 BlockBackend *blk_next(BlockBackend *blk)
440 return blk ? QTAILQ_NEXT(blk, monitor_link)
441 : QTAILQ_FIRST(&monitor_block_backends);
444 /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by
445 * the monitor or attached to a BlockBackend */
446 BlockDriverState *bdrv_next(BdrvNextIterator *it)
448 BlockDriverState *bs, *old_bs;
450 /* Must be called from the main loop */
451 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
453 /* First, return all root nodes of BlockBackends. In order to avoid
454 * returning a BDS twice when multiple BBs refer to it, we only return it
455 * if the BB is the first one in the parent list of the BDS. */
456 if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
457 BlockBackend *old_blk = it->blk;
459 old_bs = old_blk ? blk_bs(old_blk) : NULL;
462 it->blk = blk_all_next(it->blk);
463 bs = it->blk ? blk_bs(it->blk) : NULL;
464 } while (it->blk && (bs == NULL || bdrv_first_blk(bs) != it->blk));
476 it->phase = BDRV_NEXT_MONITOR_OWNED;
481 /* Then return the monitor-owned BDSes without a BB attached. Ignore all
482 * BDSes that are attached to a BlockBackend here; they have been handled
483 * by the above block already */
485 it->bs = bdrv_next_monitor_owned(it->bs);
487 } while (bs && bdrv_has_blk(bs));
497 static void bdrv_next_reset(BdrvNextIterator *it)
499 *it = (BdrvNextIterator) {
500 .phase = BDRV_NEXT_BACKEND_ROOTS,
504 BlockDriverState *bdrv_first(BdrvNextIterator *it)
507 return bdrv_next(it);
510 /* Must be called when aborting a bdrv_next() iteration before
511 * bdrv_next() returns NULL */
512 void bdrv_next_cleanup(BdrvNextIterator *it)
514 /* Must be called from the main loop */
515 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
517 if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
519 bdrv_unref(blk_bs(it->blk));
530 * Add a BlockBackend into the list of backends referenced by the monitor, with
531 * the given @name acting as the handle for the monitor.
532 * Strictly for use by blockdev.c.
534 * @name must not be null or empty.
536 * Returns true on success and false on failure. In the latter case, an Error
537 * object is returned through @errp.
539 bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
542 assert(name && name[0]);
544 if (!id_wellformed(name)) {
545 error_setg(errp, "Invalid device name");
548 if (blk_by_name(name)) {
549 error_setg(errp, "Device with id '%s' already exists", name);
552 if (bdrv_find_node(name)) {
554 "Device name '%s' conflicts with an existing node name",
559 blk->name = g_strdup(name);
560 QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
565 * Remove a BlockBackend from the list of backends referenced by the monitor.
566 * Strictly for use by blockdev.c.
568 void monitor_remove_blk(BlockBackend *blk)
574 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
580 * Return @blk's name, a non-null string.
581 * Returns an empty string iff @blk is not referenced by the monitor.
583 const char *blk_name(const BlockBackend *blk)
585 return blk->name ?: "";
589 * Return the BlockBackend with name @name if it exists, else null.
590 * @name must not be null.
592 BlockBackend *blk_by_name(const char *name)
594 BlockBackend *blk = NULL;
597 while ((blk = blk_next(blk)) != NULL) {
598 if (!strcmp(name, blk->name)) {
606 * Return the BlockDriverState attached to @blk if any, else null.
608 BlockDriverState *blk_bs(BlockBackend *blk)
610 return blk->root ? blk->root->bs : NULL;
613 static BlockBackend *bdrv_first_blk(BlockDriverState *bs)
616 QLIST_FOREACH(child, &bs->parents, next_parent) {
617 if (child->role == &child_root) {
618 return child->opaque;
626 * Returns true if @bs has an associated BlockBackend.
628 bool bdrv_has_blk(BlockDriverState *bs)
630 return bdrv_first_blk(bs) != NULL;
634 * Returns true if @bs has only BlockBackends as parents.
636 bool bdrv_is_root_node(BlockDriverState *bs)
640 QLIST_FOREACH(c, &bs->parents, next_parent) {
641 if (c->role != &child_root) {
650 * Return @blk's DriveInfo if any, else null.
652 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
654 return blk->legacy_dinfo;
658 * Set @blk's DriveInfo to @dinfo, and return it.
659 * @blk must not have a DriveInfo set already.
660 * No other BlockBackend may have the same DriveInfo set.
662 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
664 assert(!blk->legacy_dinfo);
665 return blk->legacy_dinfo = dinfo;
669 * Return the BlockBackend with DriveInfo @dinfo.
672 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
674 BlockBackend *blk = NULL;
676 while ((blk = blk_next(blk)) != NULL) {
677 if (blk->legacy_dinfo == dinfo) {
685 * Returns a pointer to the publicly accessible fields of @blk.
687 BlockBackendPublic *blk_get_public(BlockBackend *blk)
693 * Returns a BlockBackend given the associated @public fields.
695 BlockBackend *blk_by_public(BlockBackendPublic *public)
697 return container_of(public, BlockBackend, public);
701 * Disassociates the currently associated BlockDriverState from @blk.
703 void blk_remove_bs(BlockBackend *blk)
705 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
706 BlockDriverState *bs;
708 notifier_list_notify(&blk->remove_bs_notifiers, blk);
709 if (tgm->throttle_state) {
711 bdrv_drained_begin(bs);
712 throttle_group_detach_aio_context(tgm);
713 throttle_group_attach_aio_context(tgm, qemu_get_aio_context());
714 bdrv_drained_end(bs);
717 blk_update_root_state(blk);
719 bdrv_root_unref_child(blk->root);
724 * Associates a new BlockDriverState with @blk.
726 int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
728 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
729 blk->root = bdrv_root_attach_child(bs, "root", &child_root,
730 blk->perm, blk->shared_perm, blk, errp);
731 if (blk->root == NULL) {
736 notifier_list_notify(&blk->insert_bs_notifiers, blk);
737 if (tgm->throttle_state) {
738 throttle_group_detach_aio_context(tgm);
739 throttle_group_attach_aio_context(tgm, bdrv_get_aio_context(bs));
746 * Sets the permission bitmasks that the user of the BlockBackend needs.
748 int blk_set_perm(BlockBackend *blk, uint64_t perm, uint64_t shared_perm,
753 if (blk->root && !blk->disable_perm) {
754 ret = bdrv_child_try_set_perm(blk->root, perm, shared_perm, errp);
761 blk->shared_perm = shared_perm;
766 void blk_get_perm(BlockBackend *blk, uint64_t *perm, uint64_t *shared_perm)
769 *shared_perm = blk->shared_perm;
772 static int blk_do_attach_dev(BlockBackend *blk, void *dev)
778 /* While migration is still incoming, we don't need to apply the
779 * permissions of guest device BlockBackends. We might still have a block
780 * job or NBD server writing to the image for storage migration. */
781 if (runstate_check(RUN_STATE_INMIGRATE)) {
782 blk->disable_perm = true;
787 blk->legacy_dev = false;
788 blk_iostatus_reset(blk);
794 * Attach device model @dev to @blk.
795 * Return 0 on success, -EBUSY when a device model is attached already.
797 int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
799 return blk_do_attach_dev(blk, dev);
803 * Attach device model @dev to @blk.
804 * @blk must not have a device model attached already.
805 * TODO qdevified devices don't use this, remove when devices are qdevified
807 void blk_attach_dev_legacy(BlockBackend *blk, void *dev)
809 if (blk_do_attach_dev(blk, dev) < 0) {
812 blk->legacy_dev = true;
816 * Detach device model @dev from @blk.
817 * @dev must be currently attached to @blk.
819 void blk_detach_dev(BlockBackend *blk, void *dev)
820 /* TODO change to DeviceState *dev when all users are qdevified */
822 assert(blk->dev == dev);
825 blk->dev_opaque = NULL;
826 blk->guest_block_size = 512;
827 blk_set_perm(blk, 0, BLK_PERM_ALL, &error_abort);
832 * Return the device model attached to @blk if any, else null.
834 void *blk_get_attached_dev(BlockBackend *blk)
835 /* TODO change to return DeviceState * when all users are qdevified */
840 /* Return the qdev ID, or if no ID is assigned the QOM path, of the block
841 * device attached to the BlockBackend. */
842 char *blk_get_attached_dev_id(BlockBackend *blk)
846 assert(!blk->legacy_dev);
851 } else if (dev->id) {
852 return g_strdup(dev->id);
854 return object_get_canonical_path(OBJECT(dev));
858 * Return the BlockBackend which has the device model @dev attached if it
861 * @dev must not be null.
863 BlockBackend *blk_by_dev(void *dev)
865 BlockBackend *blk = NULL;
868 while ((blk = blk_all_next(blk)) != NULL) {
869 if (blk->dev == dev) {
877 * Set @blk's device model callbacks to @ops.
878 * @opaque is the opaque argument to pass to the callbacks.
879 * This is for use by device models.
881 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
884 /* All drivers that use blk_set_dev_ops() are qdevified and we want to keep
885 * it that way, so we can assume blk->dev, if present, is a DeviceState if
886 * blk->dev_ops is set. Non-device users may use dev_ops without device. */
887 assert(!blk->legacy_dev);
890 blk->dev_opaque = opaque;
892 /* Are we currently quiesced? Should we enforce this right now? */
893 if (blk->quiesce_counter && ops->drained_begin) {
894 ops->drained_begin(opaque);
899 * Notify @blk's attached device model of media change.
901 * If @load is true, notify of media load. This action can fail, meaning that
902 * the medium cannot be loaded. @errp is set then.
904 * If @load is false, notify of media eject. This can never fail.
906 * Also send DEVICE_TRAY_MOVED events as appropriate.
908 void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp)
910 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
911 bool tray_was_open, tray_is_open;
912 Error *local_err = NULL;
914 assert(!blk->legacy_dev);
916 tray_was_open = blk_dev_is_tray_open(blk);
917 blk->dev_ops->change_media_cb(blk->dev_opaque, load, &local_err);
919 assert(load == true);
920 error_propagate(errp, local_err);
923 tray_is_open = blk_dev_is_tray_open(blk);
925 if (tray_was_open != tray_is_open) {
926 char *id = blk_get_attached_dev_id(blk);
927 qapi_event_send_device_tray_moved(blk_name(blk), id, tray_is_open,
934 static void blk_root_change_media(BdrvChild *child, bool load)
936 blk_dev_change_media_cb(child->opaque, load, NULL);
940 * Does @blk's attached device model have removable media?
941 * %true if no device model is attached.
943 bool blk_dev_has_removable_media(BlockBackend *blk)
945 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
949 * Does @blk's attached device model have a tray?
951 bool blk_dev_has_tray(BlockBackend *blk)
953 return blk->dev_ops && blk->dev_ops->is_tray_open;
957 * Notify @blk's attached device model of a media eject request.
958 * If @force is true, the medium is about to be yanked out forcefully.
960 void blk_dev_eject_request(BlockBackend *blk, bool force)
962 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
963 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
968 * Does @blk's attached device model have a tray, and is it open?
970 bool blk_dev_is_tray_open(BlockBackend *blk)
972 if (blk_dev_has_tray(blk)) {
973 return blk->dev_ops->is_tray_open(blk->dev_opaque);
979 * Does @blk's attached device model have the medium locked?
980 * %false if the device model has no such lock.
982 bool blk_dev_is_medium_locked(BlockBackend *blk)
984 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
985 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
991 * Notify @blk's attached device model of a backend size change.
993 static void blk_root_resize(BdrvChild *child)
995 BlockBackend *blk = child->opaque;
997 if (blk->dev_ops && blk->dev_ops->resize_cb) {
998 blk->dev_ops->resize_cb(blk->dev_opaque);
1002 void blk_iostatus_enable(BlockBackend *blk)
1004 blk->iostatus_enabled = true;
1005 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1008 /* The I/O status is only enabled if the drive explicitly
1009 * enables it _and_ the VM is configured to stop on errors */
1010 bool blk_iostatus_is_enabled(const BlockBackend *blk)
1012 return (blk->iostatus_enabled &&
1013 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
1014 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
1015 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
1018 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
1020 return blk->iostatus;
1023 void blk_iostatus_disable(BlockBackend *blk)
1025 blk->iostatus_enabled = false;
1028 void blk_iostatus_reset(BlockBackend *blk)
1030 if (blk_iostatus_is_enabled(blk)) {
1031 BlockDriverState *bs = blk_bs(blk);
1032 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
1033 if (bs && bs->job) {
1034 block_job_iostatus_reset(bs->job);
1039 void blk_iostatus_set_err(BlockBackend *blk, int error)
1041 assert(blk_iostatus_is_enabled(blk));
1042 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
1043 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
1044 BLOCK_DEVICE_IO_STATUS_FAILED;
1048 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
1050 blk->allow_write_beyond_eof = allow;
1053 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
1058 if (size > INT_MAX) {
1062 if (!blk_is_available(blk)) {
1070 if (!blk->allow_write_beyond_eof) {
1071 len = blk_getlength(blk);
1076 if (offset > len || len - offset < size) {
1084 int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
1085 unsigned int bytes, QEMUIOVector *qiov,
1086 BdrvRequestFlags flags)
1089 BlockDriverState *bs = blk_bs(blk);
1091 trace_blk_co_preadv(blk, bs, offset, bytes, flags);
1093 ret = blk_check_byte_request(blk, offset, bytes);
1098 bdrv_inc_in_flight(bs);
1100 /* throttling disk I/O */
1101 if (blk->public.throttle_group_member.throttle_state) {
1102 throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1106 ret = bdrv_co_preadv(blk->root, offset, bytes, qiov, flags);
1107 bdrv_dec_in_flight(bs);
1111 int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
1112 unsigned int bytes, QEMUIOVector *qiov,
1113 BdrvRequestFlags flags)
1116 BlockDriverState *bs = blk_bs(blk);
1118 trace_blk_co_pwritev(blk, bs, offset, bytes, flags);
1120 ret = blk_check_byte_request(blk, offset, bytes);
1125 bdrv_inc_in_flight(bs);
1126 /* throttling disk I/O */
1127 if (blk->public.throttle_group_member.throttle_state) {
1128 throttle_group_co_io_limits_intercept(&blk->public.throttle_group_member,
1132 if (!blk->enable_write_cache) {
1133 flags |= BDRV_REQ_FUA;
1136 ret = bdrv_co_pwritev(blk->root, offset, bytes, qiov, flags);
1137 bdrv_dec_in_flight(bs);
1141 typedef struct BlkRwCo {
1146 BdrvRequestFlags flags;
1149 static void blk_read_entry(void *opaque)
1151 BlkRwCo *rwco = opaque;
1153 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
1154 rwco->qiov, rwco->flags);
1157 static void blk_write_entry(void *opaque)
1159 BlkRwCo *rwco = opaque;
1161 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
1162 rwco->qiov, rwco->flags);
1165 static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
1166 int64_t bytes, CoroutineEntry co_entry,
1167 BdrvRequestFlags flags)
1173 iov = (struct iovec) {
1177 qemu_iovec_init_external(&qiov, &iov, 1);
1187 if (qemu_in_coroutine()) {
1188 /* Fast-path if already in coroutine context */
1191 Coroutine *co = qemu_coroutine_create(co_entry, &rwco);
1192 bdrv_coroutine_enter(blk_bs(blk), co);
1193 BDRV_POLL_WHILE(blk_bs(blk), rwco.ret == NOT_DONE);
1199 int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf,
1204 ret = blk_check_byte_request(blk, offset, count);
1209 blk_root_drained_begin(blk->root);
1210 ret = blk_pread(blk, offset, buf, count);
1211 blk_root_drained_end(blk->root);
1215 int blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1216 int bytes, BdrvRequestFlags flags)
1218 return blk_prw(blk, offset, NULL, bytes, blk_write_entry,
1219 flags | BDRV_REQ_ZERO_WRITE);
1222 int blk_make_zero(BlockBackend *blk, BdrvRequestFlags flags)
1224 return bdrv_make_zero(blk->root, flags);
1227 static void error_callback_bh(void *opaque)
1229 struct BlockBackendAIOCB *acb = opaque;
1231 bdrv_dec_in_flight(acb->common.bs);
1232 acb->common.cb(acb->common.opaque, acb->ret);
1233 qemu_aio_unref(acb);
1236 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
1237 BlockCompletionFunc *cb,
1238 void *opaque, int ret)
1240 struct BlockBackendAIOCB *acb;
1242 bdrv_inc_in_flight(blk_bs(blk));
1243 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
1247 aio_bh_schedule_oneshot(blk_get_aio_context(blk), error_callback_bh, acb);
1248 return &acb->common;
1251 typedef struct BlkAioEmAIOCB {
1258 static const AIOCBInfo blk_aio_em_aiocb_info = {
1259 .aiocb_size = sizeof(BlkAioEmAIOCB),
1262 static void blk_aio_complete(BlkAioEmAIOCB *acb)
1264 if (acb->has_returned) {
1265 bdrv_dec_in_flight(acb->common.bs);
1266 acb->common.cb(acb->common.opaque, acb->rwco.ret);
1267 qemu_aio_unref(acb);
1271 static void blk_aio_complete_bh(void *opaque)
1273 BlkAioEmAIOCB *acb = opaque;
1274 assert(acb->has_returned);
1275 blk_aio_complete(acb);
1278 static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
1279 QEMUIOVector *qiov, CoroutineEntry co_entry,
1280 BdrvRequestFlags flags,
1281 BlockCompletionFunc *cb, void *opaque)
1286 bdrv_inc_in_flight(blk_bs(blk));
1287 acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
1288 acb->rwco = (BlkRwCo) {
1296 acb->has_returned = false;
1298 co = qemu_coroutine_create(co_entry, acb);
1299 bdrv_coroutine_enter(blk_bs(blk), co);
1301 acb->has_returned = true;
1302 if (acb->rwco.ret != NOT_DONE) {
1303 aio_bh_schedule_oneshot(blk_get_aio_context(blk),
1304 blk_aio_complete_bh, acb);
1307 return &acb->common;
1310 static void blk_aio_read_entry(void *opaque)
1312 BlkAioEmAIOCB *acb = opaque;
1313 BlkRwCo *rwco = &acb->rwco;
1315 assert(rwco->qiov->size == acb->bytes);
1316 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes,
1317 rwco->qiov, rwco->flags);
1318 blk_aio_complete(acb);
1321 static void blk_aio_write_entry(void *opaque)
1323 BlkAioEmAIOCB *acb = opaque;
1324 BlkRwCo *rwco = &acb->rwco;
1326 assert(!rwco->qiov || rwco->qiov->size == acb->bytes);
1327 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes,
1328 rwco->qiov, rwco->flags);
1329 blk_aio_complete(acb);
1332 BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1333 int count, BdrvRequestFlags flags,
1334 BlockCompletionFunc *cb, void *opaque)
1336 return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
1337 flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
1340 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
1342 int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
1349 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
1350 BdrvRequestFlags flags)
1352 int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
1360 int64_t blk_getlength(BlockBackend *blk)
1362 if (!blk_is_available(blk)) {
1366 return bdrv_getlength(blk_bs(blk));
1369 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
1372 *nb_sectors_ptr = 0;
1374 bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
1378 int64_t blk_nb_sectors(BlockBackend *blk)
1380 if (!blk_is_available(blk)) {
1384 return bdrv_nb_sectors(blk_bs(blk));
1387 BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
1388 QEMUIOVector *qiov, BdrvRequestFlags flags,
1389 BlockCompletionFunc *cb, void *opaque)
1391 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1392 blk_aio_read_entry, flags, cb, opaque);
1395 BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
1396 QEMUIOVector *qiov, BdrvRequestFlags flags,
1397 BlockCompletionFunc *cb, void *opaque)
1399 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1400 blk_aio_write_entry, flags, cb, opaque);
1403 static void blk_aio_flush_entry(void *opaque)
1405 BlkAioEmAIOCB *acb = opaque;
1406 BlkRwCo *rwco = &acb->rwco;
1408 rwco->ret = blk_co_flush(rwco->blk);
1409 blk_aio_complete(acb);
1412 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1413 BlockCompletionFunc *cb, void *opaque)
1415 return blk_aio_prwv(blk, 0, 0, NULL, blk_aio_flush_entry, 0, cb, opaque);
1418 static void blk_aio_pdiscard_entry(void *opaque)
1420 BlkAioEmAIOCB *acb = opaque;
1421 BlkRwCo *rwco = &acb->rwco;
1423 rwco->ret = blk_co_pdiscard(rwco->blk, rwco->offset, acb->bytes);
1424 blk_aio_complete(acb);
1427 BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk,
1428 int64_t offset, int bytes,
1429 BlockCompletionFunc *cb, void *opaque)
1431 return blk_aio_prwv(blk, offset, bytes, NULL, blk_aio_pdiscard_entry, 0,
1435 void blk_aio_cancel(BlockAIOCB *acb)
1437 bdrv_aio_cancel(acb);
1440 void blk_aio_cancel_async(BlockAIOCB *acb)
1442 bdrv_aio_cancel_async(acb);
1445 int blk_co_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1447 if (!blk_is_available(blk)) {
1451 return bdrv_co_ioctl(blk_bs(blk), req, buf);
1454 static void blk_ioctl_entry(void *opaque)
1456 BlkRwCo *rwco = opaque;
1457 rwco->ret = blk_co_ioctl(rwco->blk, rwco->offset,
1458 rwco->qiov->iov[0].iov_base);
1461 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1463 return blk_prw(blk, req, buf, 0, blk_ioctl_entry, 0);
1466 static void blk_aio_ioctl_entry(void *opaque)
1468 BlkAioEmAIOCB *acb = opaque;
1469 BlkRwCo *rwco = &acb->rwco;
1471 rwco->ret = blk_co_ioctl(rwco->blk, rwco->offset,
1472 rwco->qiov->iov[0].iov_base);
1473 blk_aio_complete(acb);
1476 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
1477 BlockCompletionFunc *cb, void *opaque)
1482 iov = (struct iovec) {
1486 qemu_iovec_init_external(&qiov, &iov, 1);
1488 return blk_aio_prwv(blk, req, 0, &qiov, blk_aio_ioctl_entry, 0, cb, opaque);
1491 int blk_co_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1493 int ret = blk_check_byte_request(blk, offset, bytes);
1498 return bdrv_co_pdiscard(blk_bs(blk), offset, bytes);
1501 int blk_co_flush(BlockBackend *blk)
1503 if (!blk_is_available(blk)) {
1507 return bdrv_co_flush(blk_bs(blk));
1510 static void blk_flush_entry(void *opaque)
1512 BlkRwCo *rwco = opaque;
1513 rwco->ret = blk_co_flush(rwco->blk);
1516 int blk_flush(BlockBackend *blk)
1518 return blk_prw(blk, 0, NULL, 0, blk_flush_entry, 0);
1521 void blk_drain(BlockBackend *blk)
1524 bdrv_drain(blk_bs(blk));
1528 void blk_drain_all(void)
1533 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1534 BlockdevOnError on_write_error)
1536 blk->on_read_error = on_read_error;
1537 blk->on_write_error = on_write_error;
1540 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
1542 return is_read ? blk->on_read_error : blk->on_write_error;
1545 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
1548 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1551 case BLOCKDEV_ON_ERROR_ENOSPC:
1552 return (error == ENOSPC) ?
1553 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1554 case BLOCKDEV_ON_ERROR_STOP:
1555 return BLOCK_ERROR_ACTION_STOP;
1556 case BLOCKDEV_ON_ERROR_REPORT:
1557 return BLOCK_ERROR_ACTION_REPORT;
1558 case BLOCKDEV_ON_ERROR_IGNORE:
1559 return BLOCK_ERROR_ACTION_IGNORE;
1560 case BLOCKDEV_ON_ERROR_AUTO:
1566 static void send_qmp_error_event(BlockBackend *blk,
1567 BlockErrorAction action,
1568 bool is_read, int error)
1570 IoOperationType optype;
1572 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
1573 qapi_event_send_block_io_error(blk_name(blk),
1574 bdrv_get_node_name(blk_bs(blk)), optype,
1575 action, blk_iostatus_is_enabled(blk),
1576 error == ENOSPC, strerror(error),
1580 /* This is done by device models because, while the block layer knows
1581 * about the error, it does not know whether an operation comes from
1582 * the device or the block layer (from a job, for example).
1584 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1585 bool is_read, int error)
1589 if (action == BLOCK_ERROR_ACTION_STOP) {
1590 /* First set the iostatus, so that "info block" returns an iostatus
1591 * that matches the events raised so far (an additional error iostatus
1592 * is fine, but not a lost one).
1594 blk_iostatus_set_err(blk, error);
1596 /* Then raise the request to stop the VM and the event.
1597 * qemu_system_vmstop_request_prepare has two effects. First,
1598 * it ensures that the STOP event always comes after the
1599 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1600 * can observe the STOP event and do a "cont" before the STOP
1601 * event is issued, the VM will not stop. In this case, vm_start()
1602 * also ensures that the STOP/RESUME pair of events is emitted.
1604 qemu_system_vmstop_request_prepare();
1605 send_qmp_error_event(blk, action, is_read, error);
1606 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1608 send_qmp_error_event(blk, action, is_read, error);
1612 int blk_is_read_only(BlockBackend *blk)
1614 BlockDriverState *bs = blk_bs(blk);
1617 return bdrv_is_read_only(bs);
1619 return blk->root_state.read_only;
1623 int blk_is_sg(BlockBackend *blk)
1625 BlockDriverState *bs = blk_bs(blk);
1631 return bdrv_is_sg(bs);
1634 int blk_enable_write_cache(BlockBackend *blk)
1636 return blk->enable_write_cache;
1639 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1641 blk->enable_write_cache = wce;
1644 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1646 BlockDriverState *bs = blk_bs(blk);
1649 error_setg(errp, "Device '%s' has no medium", blk->name);
1653 bdrv_invalidate_cache(bs, errp);
1656 bool blk_is_inserted(BlockBackend *blk)
1658 BlockDriverState *bs = blk_bs(blk);
1660 return bs && bdrv_is_inserted(bs);
1663 bool blk_is_available(BlockBackend *blk)
1665 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1668 void blk_lock_medium(BlockBackend *blk, bool locked)
1670 BlockDriverState *bs = blk_bs(blk);
1673 bdrv_lock_medium(bs, locked);
1677 void blk_eject(BlockBackend *blk, bool eject_flag)
1679 BlockDriverState *bs = blk_bs(blk);
1682 /* blk_eject is only called by qdevified devices */
1683 assert(!blk->legacy_dev);
1686 bdrv_eject(bs, eject_flag);
1689 /* Whether or not we ejected on the backend,
1690 * the frontend experienced a tray event. */
1691 id = blk_get_attached_dev_id(blk);
1692 qapi_event_send_device_tray_moved(blk_name(blk), id,
1693 eject_flag, &error_abort);
1697 int blk_get_flags(BlockBackend *blk)
1699 BlockDriverState *bs = blk_bs(blk);
1702 return bdrv_get_flags(bs);
1704 return blk->root_state.open_flags;
1708 /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
1709 uint32_t blk_get_max_transfer(BlockBackend *blk)
1711 BlockDriverState *bs = blk_bs(blk);
1715 max = bs->bl.max_transfer;
1717 return MIN_NON_ZERO(max, INT_MAX);
1720 int blk_get_max_iov(BlockBackend *blk)
1722 return blk->root->bs->bl.max_iov;
1725 void blk_set_guest_block_size(BlockBackend *blk, int align)
1727 blk->guest_block_size = align;
1730 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1732 return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
1735 void *blk_blockalign(BlockBackend *blk, size_t size)
1737 return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
1740 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1742 BlockDriverState *bs = blk_bs(blk);
1748 return bdrv_op_is_blocked(bs, op, errp);
1751 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1753 BlockDriverState *bs = blk_bs(blk);
1756 bdrv_op_unblock(bs, op, reason);
1760 void blk_op_block_all(BlockBackend *blk, Error *reason)
1762 BlockDriverState *bs = blk_bs(blk);
1765 bdrv_op_block_all(bs, reason);
1769 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1771 BlockDriverState *bs = blk_bs(blk);
1774 bdrv_op_unblock_all(bs, reason);
1778 AioContext *blk_get_aio_context(BlockBackend *blk)
1780 BlockDriverState *bs = blk_bs(blk);
1783 return bdrv_get_aio_context(bs);
1785 return qemu_get_aio_context();
1789 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1791 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1792 return blk_get_aio_context(blk_acb->blk);
1795 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1797 BlockDriverState *bs = blk_bs(blk);
1798 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
1801 if (tgm->throttle_state) {
1802 bdrv_drained_begin(bs);
1803 throttle_group_detach_aio_context(tgm);
1804 throttle_group_attach_aio_context(tgm, new_context);
1805 bdrv_drained_end(bs);
1807 bdrv_set_aio_context(bs, new_context);
1811 void blk_add_aio_context_notifier(BlockBackend *blk,
1812 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1813 void (*detach_aio_context)(void *opaque), void *opaque)
1815 BlockDriverState *bs = blk_bs(blk);
1818 bdrv_add_aio_context_notifier(bs, attached_aio_context,
1819 detach_aio_context, opaque);
1823 void blk_remove_aio_context_notifier(BlockBackend *blk,
1824 void (*attached_aio_context)(AioContext *,
1826 void (*detach_aio_context)(void *),
1829 BlockDriverState *bs = blk_bs(blk);
1832 bdrv_remove_aio_context_notifier(bs, attached_aio_context,
1833 detach_aio_context, opaque);
1837 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1839 notifier_list_add(&blk->remove_bs_notifiers, notify);
1842 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1844 notifier_list_add(&blk->insert_bs_notifiers, notify);
1847 void blk_io_plug(BlockBackend *blk)
1849 BlockDriverState *bs = blk_bs(blk);
1856 void blk_io_unplug(BlockBackend *blk)
1858 BlockDriverState *bs = blk_bs(blk);
1865 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1870 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1871 BlockCompletionFunc *cb, void *opaque)
1873 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1876 int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1877 int bytes, BdrvRequestFlags flags)
1879 return blk_co_pwritev(blk, offset, bytes, NULL,
1880 flags | BDRV_REQ_ZERO_WRITE);
1883 int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,
1886 return blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
1887 BDRV_REQ_WRITE_COMPRESSED);
1890 int blk_truncate(BlockBackend *blk, int64_t offset, PreallocMode prealloc,
1893 if (!blk_is_available(blk)) {
1894 error_setg(errp, "No medium inserted");
1898 return bdrv_truncate(blk->root, offset, prealloc, errp);
1901 static void blk_pdiscard_entry(void *opaque)
1903 BlkRwCo *rwco = opaque;
1904 rwco->ret = blk_co_pdiscard(rwco->blk, rwco->offset, rwco->qiov->size);
1907 int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes)
1909 return blk_prw(blk, offset, NULL, bytes, blk_pdiscard_entry, 0);
1912 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1913 int64_t pos, int size)
1917 if (!blk_is_available(blk)) {
1921 ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
1926 if (ret == size && !blk->enable_write_cache) {
1927 ret = bdrv_flush(blk_bs(blk));
1930 return ret < 0 ? ret : size;
1933 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1935 if (!blk_is_available(blk)) {
1939 return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
1942 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1944 if (!blk_is_available(blk)) {
1948 return bdrv_probe_blocksizes(blk_bs(blk), bsz);
1951 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1953 if (!blk_is_available(blk)) {
1957 return bdrv_probe_geometry(blk_bs(blk), geo);
1961 * Updates the BlockBackendRootState object with data from the currently
1962 * attached BlockDriverState.
1964 void blk_update_root_state(BlockBackend *blk)
1968 blk->root_state.open_flags = blk->root->bs->open_flags;
1969 blk->root_state.read_only = blk->root->bs->read_only;
1970 blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
1974 * Returns the detect-zeroes setting to be used for bdrv_open() of a
1975 * BlockDriverState which is supposed to inherit the root state.
1977 bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)
1979 return blk->root_state.detect_zeroes;
1983 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1984 * supposed to inherit the root state.
1986 int blk_get_open_flags_from_root_state(BlockBackend *blk)
1990 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1991 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1996 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1998 return &blk->root_state;
2001 int blk_commit_all(void)
2003 BlockBackend *blk = NULL;
2005 while ((blk = blk_all_next(blk)) != NULL) {
2006 AioContext *aio_context = blk_get_aio_context(blk);
2008 aio_context_acquire(aio_context);
2009 if (blk_is_inserted(blk) && blk->root->bs->backing) {
2010 int ret = bdrv_commit(blk->root->bs);
2012 aio_context_release(aio_context);
2016 aio_context_release(aio_context);
2022 /* throttling disk I/O limits */
2023 void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
2025 throttle_group_config(&blk->public.throttle_group_member, cfg);
2028 void blk_io_limits_disable(BlockBackend *blk)
2030 BlockDriverState *bs = blk_bs(blk);
2031 ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
2032 assert(tgm->throttle_state);
2034 bdrv_drained_begin(bs);
2036 throttle_group_unregister_tgm(tgm);
2038 bdrv_drained_end(bs);
2042 /* should be called before blk_set_io_limits if a limit is set */
2043 void blk_io_limits_enable(BlockBackend *blk, const char *group)
2045 assert(!blk->public.throttle_group_member.throttle_state);
2046 throttle_group_register_tgm(&blk->public.throttle_group_member,
2047 group, blk_get_aio_context(blk));
2050 void blk_io_limits_update_group(BlockBackend *blk, const char *group)
2052 /* this BB is not part of any group */
2053 if (!blk->public.throttle_group_member.throttle_state) {
2057 /* this BB is a part of the same group than the one we want */
2058 if (!g_strcmp0(throttle_group_get_name(&blk->public.throttle_group_member),
2063 /* need to change the group this bs belong to */
2064 blk_io_limits_disable(blk);
2065 blk_io_limits_enable(blk, group);
2068 static void blk_root_drained_begin(BdrvChild *child)
2070 BlockBackend *blk = child->opaque;
2072 if (++blk->quiesce_counter == 1) {
2073 if (blk->dev_ops && blk->dev_ops->drained_begin) {
2074 blk->dev_ops->drained_begin(blk->dev_opaque);
2078 /* Note that blk->root may not be accessible here yet if we are just
2079 * attaching to a BlockDriverState that is drained. Use child instead. */
2081 if (atomic_fetch_inc(&blk->public.throttle_group_member.io_limits_disabled) == 0) {
2082 throttle_group_restart_tgm(&blk->public.throttle_group_member);
2086 static void blk_root_drained_end(BdrvChild *child)
2088 BlockBackend *blk = child->opaque;
2089 assert(blk->quiesce_counter);
2091 assert(blk->public.throttle_group_member.io_limits_disabled);
2092 atomic_dec(&blk->public.throttle_group_member.io_limits_disabled);
2094 if (--blk->quiesce_counter == 0) {
2095 if (blk->dev_ops && blk->dev_ops->drained_end) {
2096 blk->dev_ops->drained_end(blk->dev_opaque);
2101 void blk_register_buf(BlockBackend *blk, void *host, size_t size)
2103 bdrv_register_buf(blk_bs(blk), host, size);
2106 void blk_unregister_buf(BlockBackend *blk, void *host)
2108 bdrv_unregister_buf(blk_bs(blk), host);