4 * Copyright (C) 2014-2016 Red Hat, Inc.
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 "hw/qdev-core.h"
19 #include "sysemu/blockdev.h"
20 #include "sysemu/runstate.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/replay.h"
23 #include "qapi/error.h"
24 #include "qapi/qapi-events-block.h"
26 #include "qemu/main-loop.h"
27 #include "qemu/option.h"
29 #include "migration/misc.h"
31 /* Number of coroutines to reserve per attached device model */
32 #define COROUTINE_POOL_RESERVATION 64
34 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
36 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
);
38 typedef struct BlockBackendAioNotifier
{
39 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
);
40 void (*detach_aio_context
)(void *opaque
);
42 QLIST_ENTRY(BlockBackendAioNotifier
) list
;
43 } BlockBackendAioNotifier
;
50 DriveInfo
*legacy_dinfo
; /* null unless created by drive_new() */
51 QTAILQ_ENTRY(BlockBackend
) link
; /* for block_backends */
52 QTAILQ_ENTRY(BlockBackend
) monitor_link
; /* for monitor_block_backends */
53 BlockBackendPublic
public;
55 DeviceState
*dev
; /* attached device model, if any */
56 const BlockDevOps
*dev_ops
;
59 /* the block size for which the guest device expects atomicity */
62 /* If the BDS tree is removed, some of its options are stored here (which
63 * can be used to restore those options in the new BDS on insert) */
64 BlockBackendRootState root_state
;
66 bool enable_write_cache
;
68 /* I/O stats (display with "info blockstats"). */
71 BlockdevOnError on_read_error
, on_write_error
;
72 bool iostatus_enabled
;
73 BlockDeviceIoStatus iostatus
;
79 bool allow_aio_context_change
;
80 bool allow_write_beyond_eof
;
82 NotifierList remove_bs_notifiers
, insert_bs_notifiers
;
83 QLIST_HEAD(, BlockBackendAioNotifier
) aio_notifiers
;
86 CoQueue queued_requests
;
87 bool disable_request_queuing
;
89 VMChangeStateEntry
*vmsh
;
90 bool force_allow_inactivate
;
92 /* Number of in-flight aio requests. BlockDriverState also counts
93 * in-flight requests but aio requests can exist even when blk->root is
94 * NULL, so we cannot rely on its counter for that case.
95 * Accessed with atomic ops.
97 unsigned int in_flight
;
100 typedef struct BlockBackendAIOCB
{
106 static const AIOCBInfo block_backend_aiocb_info
= {
107 .get_aio_context
= blk_aiocb_get_aio_context
,
108 .aiocb_size
= sizeof(BlockBackendAIOCB
),
111 static void drive_info_del(DriveInfo
*dinfo
);
112 static BlockBackend
*bdrv_first_blk(BlockDriverState
*bs
);
114 /* All BlockBackends */
115 static QTAILQ_HEAD(, BlockBackend
) block_backends
=
116 QTAILQ_HEAD_INITIALIZER(block_backends
);
118 /* All BlockBackends referenced by the monitor and which are iterated through by
120 static QTAILQ_HEAD(, BlockBackend
) monitor_block_backends
=
121 QTAILQ_HEAD_INITIALIZER(monitor_block_backends
);
123 static void blk_root_inherit_options(BdrvChildRole role
, bool parent_is_format
,
124 int *child_flags
, QDict
*child_options
,
125 int parent_flags
, QDict
*parent_options
)
127 /* We're not supposed to call this function for root nodes */
130 static void blk_root_drained_begin(BdrvChild
*child
);
131 static bool blk_root_drained_poll(BdrvChild
*child
);
132 static void blk_root_drained_end(BdrvChild
*child
, int *drained_end_counter
);
134 static void blk_root_change_media(BdrvChild
*child
, bool load
);
135 static void blk_root_resize(BdrvChild
*child
);
137 static bool blk_root_can_set_aio_ctx(BdrvChild
*child
, AioContext
*ctx
,
138 GSList
**ignore
, Error
**errp
);
139 static void blk_root_set_aio_ctx(BdrvChild
*child
, AioContext
*ctx
,
142 static char *blk_root_get_parent_desc(BdrvChild
*child
)
144 BlockBackend
*blk
= child
->opaque
;
148 return g_strdup(blk
->name
);
151 dev_id
= blk_get_attached_dev_id(blk
);
155 /* TODO Callback into the BB owner for something more detailed */
157 return g_strdup("a block device");
161 static const char *blk_root_get_name(BdrvChild
*child
)
163 return blk_name(child
->opaque
);
166 static void blk_vm_state_changed(void *opaque
, int running
, RunState state
)
168 Error
*local_err
= NULL
;
169 BlockBackend
*blk
= opaque
;
171 if (state
== RUN_STATE_INMIGRATE
) {
175 qemu_del_vm_change_state_handler(blk
->vmsh
);
177 blk_set_perm(blk
, blk
->perm
, blk
->shared_perm
, &local_err
);
179 error_report_err(local_err
);
184 * Notifies the user of the BlockBackend that migration has completed. qdev
185 * devices can tighten their permissions in response (specifically revoke
186 * shared write permissions that we needed for storage migration).
188 * If an error is returned, the VM cannot be allowed to be resumed.
190 static void blk_root_activate(BdrvChild
*child
, Error
**errp
)
192 BlockBackend
*blk
= child
->opaque
;
193 Error
*local_err
= NULL
;
195 if (!blk
->disable_perm
) {
199 blk
->disable_perm
= false;
201 blk_set_perm(blk
, blk
->perm
, BLK_PERM_ALL
, &local_err
);
203 error_propagate(errp
, local_err
);
204 blk
->disable_perm
= true;
208 if (runstate_check(RUN_STATE_INMIGRATE
)) {
209 /* Activation can happen when migration process is still active, for
210 * example when nbd_server_add is called during non-shared storage
211 * migration. Defer the shared_perm update to migration completion. */
213 blk
->vmsh
= qemu_add_vm_change_state_handler(blk_vm_state_changed
,
219 blk_set_perm(blk
, blk
->perm
, blk
->shared_perm
, &local_err
);
221 error_propagate(errp
, local_err
);
222 blk
->disable_perm
= true;
227 void blk_set_force_allow_inactivate(BlockBackend
*blk
)
229 blk
->force_allow_inactivate
= true;
232 static bool blk_can_inactivate(BlockBackend
*blk
)
234 /* If it is a guest device, inactivate is ok. */
235 if (blk
->dev
|| blk_name(blk
)[0]) {
239 /* Inactivating means no more writes to the image can be done,
240 * even if those writes would be changes invisible to the
241 * guest. For block job BBs that satisfy this, we can just allow
242 * it. This is the case for mirror job source, which is required
243 * by libvirt non-shared block migration. */
244 if (!(blk
->perm
& (BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
))) {
248 return blk
->force_allow_inactivate
;
251 static int blk_root_inactivate(BdrvChild
*child
)
253 BlockBackend
*blk
= child
->opaque
;
255 if (blk
->disable_perm
) {
259 if (!blk_can_inactivate(blk
)) {
263 blk
->disable_perm
= true;
265 bdrv_child_try_set_perm(blk
->root
, 0, BLK_PERM_ALL
, &error_abort
);
271 static void blk_root_attach(BdrvChild
*child
)
273 BlockBackend
*blk
= child
->opaque
;
274 BlockBackendAioNotifier
*notifier
;
276 trace_blk_root_attach(child
, blk
, child
->bs
);
278 QLIST_FOREACH(notifier
, &blk
->aio_notifiers
, list
) {
279 bdrv_add_aio_context_notifier(child
->bs
,
280 notifier
->attached_aio_context
,
281 notifier
->detach_aio_context
,
286 static void blk_root_detach(BdrvChild
*child
)
288 BlockBackend
*blk
= child
->opaque
;
289 BlockBackendAioNotifier
*notifier
;
291 trace_blk_root_detach(child
, blk
, child
->bs
);
293 QLIST_FOREACH(notifier
, &blk
->aio_notifiers
, list
) {
294 bdrv_remove_aio_context_notifier(child
->bs
,
295 notifier
->attached_aio_context
,
296 notifier
->detach_aio_context
,
301 static const BdrvChildClass child_root
= {
302 .inherit_options
= blk_root_inherit_options
,
304 .change_media
= blk_root_change_media
,
305 .resize
= blk_root_resize
,
306 .get_name
= blk_root_get_name
,
307 .get_parent_desc
= blk_root_get_parent_desc
,
309 .drained_begin
= blk_root_drained_begin
,
310 .drained_poll
= blk_root_drained_poll
,
311 .drained_end
= blk_root_drained_end
,
313 .activate
= blk_root_activate
,
314 .inactivate
= blk_root_inactivate
,
316 .attach
= blk_root_attach
,
317 .detach
= blk_root_detach
,
319 .can_set_aio_ctx
= blk_root_can_set_aio_ctx
,
320 .set_aio_ctx
= blk_root_set_aio_ctx
,
324 * Create a new BlockBackend with a reference count of one.
326 * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
327 * to request for a block driver node that is attached to this BlockBackend.
328 * @shared_perm is a bitmask which describes which permissions may be granted
329 * to other users of the attached node.
330 * Both sets of permissions can be changed later using blk_set_perm().
332 * Return the new BlockBackend on success, null on failure.
334 BlockBackend
*blk_new(AioContext
*ctx
, uint64_t perm
, uint64_t shared_perm
)
338 blk
= g_new0(BlockBackend
, 1);
342 blk
->shared_perm
= shared_perm
;
343 blk_set_enable_write_cache(blk
, true);
345 blk
->on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
346 blk
->on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
348 block_acct_init(&blk
->stats
);
350 qemu_co_queue_init(&blk
->queued_requests
);
351 notifier_list_init(&blk
->remove_bs_notifiers
);
352 notifier_list_init(&blk
->insert_bs_notifiers
);
353 QLIST_INIT(&blk
->aio_notifiers
);
355 QTAILQ_INSERT_TAIL(&block_backends
, blk
, link
);
360 * Create a new BlockBackend connected to an existing BlockDriverState.
362 * @perm is a bitmasks of BLK_PERM_* constants which describes the
363 * permissions to request for @bs that is attached to this
364 * BlockBackend. @shared_perm is a bitmask which describes which
365 * permissions may be granted to other users of the attached node.
366 * Both sets of permissions can be changed later using blk_set_perm().
368 * Return the new BlockBackend on success, null on failure.
370 BlockBackend
*blk_new_with_bs(BlockDriverState
*bs
, uint64_t perm
,
371 uint64_t shared_perm
, Error
**errp
)
373 BlockBackend
*blk
= blk_new(bdrv_get_aio_context(bs
), perm
, shared_perm
);
375 if (blk_insert_bs(blk
, bs
, errp
) < 0) {
383 * Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
384 * The new BlockBackend is in the main AioContext.
386 * Just as with bdrv_open(), after having called this function the reference to
387 * @options belongs to the block layer (even on failure).
389 * TODO: Remove @filename and @flags; it should be possible to specify a whole
390 * BDS tree just by specifying the @options QDict (or @reference,
391 * alternatively). At the time of adding this function, this is not possible,
392 * though, so callers of this function have to be able to specify @filename and
395 BlockBackend
*blk_new_open(const char *filename
, const char *reference
,
396 QDict
*options
, int flags
, Error
**errp
)
399 BlockDriverState
*bs
;
402 /* blk_new_open() is mainly used in .bdrv_create implementations and the
403 * tools where sharing isn't a concern because the BDS stays private, so we
404 * just request permission according to the flags.
406 * The exceptions are xen_disk and blockdev_init(); in these cases, the
407 * caller of blk_new_open() doesn't make use of the permissions, but they
408 * shouldn't hurt either. We can still share everything here because the
409 * guest devices will add their own blockers if they can't share. */
410 if ((flags
& BDRV_O_NO_IO
) == 0) {
411 perm
|= BLK_PERM_CONSISTENT_READ
;
412 if (flags
& BDRV_O_RDWR
) {
413 perm
|= BLK_PERM_WRITE
;
416 if (flags
& BDRV_O_RESIZE
) {
417 perm
|= BLK_PERM_RESIZE
;
420 blk
= blk_new(qemu_get_aio_context(), perm
, BLK_PERM_ALL
);
421 bs
= bdrv_open(filename
, reference
, options
, flags
, errp
);
427 blk
->root
= bdrv_root_attach_child(bs
, "root", &child_root
, 0, blk
->ctx
,
428 perm
, BLK_PERM_ALL
, blk
, errp
);
437 static void blk_delete(BlockBackend
*blk
)
439 assert(!blk
->refcnt
);
442 if (blk
->public.throttle_group_member
.throttle_state
) {
443 blk_io_limits_disable(blk
);
449 qemu_del_vm_change_state_handler(blk
->vmsh
);
452 assert(QLIST_EMPTY(&blk
->remove_bs_notifiers
.notifiers
));
453 assert(QLIST_EMPTY(&blk
->insert_bs_notifiers
.notifiers
));
454 assert(QLIST_EMPTY(&blk
->aio_notifiers
));
455 QTAILQ_REMOVE(&block_backends
, blk
, link
);
456 drive_info_del(blk
->legacy_dinfo
);
457 block_acct_cleanup(&blk
->stats
);
461 static void drive_info_del(DriveInfo
*dinfo
)
466 qemu_opts_del(dinfo
->opts
);
470 int blk_get_refcnt(BlockBackend
*blk
)
472 return blk
? blk
->refcnt
: 0;
476 * Increment @blk's reference count.
477 * @blk must not be null.
479 void blk_ref(BlockBackend
*blk
)
481 assert(blk
->refcnt
> 0);
486 * Decrement @blk's reference count.
487 * If this drops it to zero, destroy @blk.
488 * For convenience, do nothing if @blk is null.
490 void blk_unref(BlockBackend
*blk
)
493 assert(blk
->refcnt
> 0);
494 if (blk
->refcnt
> 1) {
498 /* blk_drain() cannot resurrect blk, nobody held a reference */
499 assert(blk
->refcnt
== 1);
507 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
508 * ones which are hidden (i.e. are not referenced by the monitor).
510 BlockBackend
*blk_all_next(BlockBackend
*blk
)
512 return blk
? QTAILQ_NEXT(blk
, link
)
513 : QTAILQ_FIRST(&block_backends
);
516 void blk_remove_all_bs(void)
518 BlockBackend
*blk
= NULL
;
520 while ((blk
= blk_all_next(blk
)) != NULL
) {
521 AioContext
*ctx
= blk_get_aio_context(blk
);
523 aio_context_acquire(ctx
);
527 aio_context_release(ctx
);
532 * Return the monitor-owned BlockBackend after @blk.
533 * If @blk is null, return the first one.
534 * Else, return @blk's next sibling, which may be null.
536 * To iterate over all BlockBackends, do
537 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
541 BlockBackend
*blk_next(BlockBackend
*blk
)
543 return blk
? QTAILQ_NEXT(blk
, monitor_link
)
544 : QTAILQ_FIRST(&monitor_block_backends
);
547 /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by
548 * the monitor or attached to a BlockBackend */
549 BlockDriverState
*bdrv_next(BdrvNextIterator
*it
)
551 BlockDriverState
*bs
, *old_bs
;
553 /* Must be called from the main loop */
554 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
556 /* First, return all root nodes of BlockBackends. In order to avoid
557 * returning a BDS twice when multiple BBs refer to it, we only return it
558 * if the BB is the first one in the parent list of the BDS. */
559 if (it
->phase
== BDRV_NEXT_BACKEND_ROOTS
) {
560 BlockBackend
*old_blk
= it
->blk
;
562 old_bs
= old_blk
? blk_bs(old_blk
) : NULL
;
565 it
->blk
= blk_all_next(it
->blk
);
566 bs
= it
->blk
? blk_bs(it
->blk
) : NULL
;
567 } while (it
->blk
&& (bs
== NULL
|| bdrv_first_blk(bs
) != it
->blk
));
579 it
->phase
= BDRV_NEXT_MONITOR_OWNED
;
584 /* Then return the monitor-owned BDSes without a BB attached. Ignore all
585 * BDSes that are attached to a BlockBackend here; they have been handled
586 * by the above block already */
588 it
->bs
= bdrv_next_monitor_owned(it
->bs
);
590 } while (bs
&& bdrv_has_blk(bs
));
600 static void bdrv_next_reset(BdrvNextIterator
*it
)
602 *it
= (BdrvNextIterator
) {
603 .phase
= BDRV_NEXT_BACKEND_ROOTS
,
607 BlockDriverState
*bdrv_first(BdrvNextIterator
*it
)
610 return bdrv_next(it
);
613 /* Must be called when aborting a bdrv_next() iteration before
614 * bdrv_next() returns NULL */
615 void bdrv_next_cleanup(BdrvNextIterator
*it
)
617 /* Must be called from the main loop */
618 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
620 if (it
->phase
== BDRV_NEXT_BACKEND_ROOTS
) {
622 bdrv_unref(blk_bs(it
->blk
));
633 * Add a BlockBackend into the list of backends referenced by the monitor, with
634 * the given @name acting as the handle for the monitor.
635 * Strictly for use by blockdev.c.
637 * @name must not be null or empty.
639 * Returns true on success and false on failure. In the latter case, an Error
640 * object is returned through @errp.
642 bool monitor_add_blk(BlockBackend
*blk
, const char *name
, Error
**errp
)
645 assert(name
&& name
[0]);
647 if (!id_wellformed(name
)) {
648 error_setg(errp
, "Invalid device name");
651 if (blk_by_name(name
)) {
652 error_setg(errp
, "Device with id '%s' already exists", name
);
655 if (bdrv_find_node(name
)) {
657 "Device name '%s' conflicts with an existing node name",
662 blk
->name
= g_strdup(name
);
663 QTAILQ_INSERT_TAIL(&monitor_block_backends
, blk
, monitor_link
);
668 * Remove a BlockBackend from the list of backends referenced by the monitor.
669 * Strictly for use by blockdev.c.
671 void monitor_remove_blk(BlockBackend
*blk
)
677 QTAILQ_REMOVE(&monitor_block_backends
, blk
, monitor_link
);
683 * Return @blk's name, a non-null string.
684 * Returns an empty string iff @blk is not referenced by the monitor.
686 const char *blk_name(const BlockBackend
*blk
)
688 return blk
->name
?: "";
692 * Return the BlockBackend with name @name if it exists, else null.
693 * @name must not be null.
695 BlockBackend
*blk_by_name(const char *name
)
697 BlockBackend
*blk
= NULL
;
700 while ((blk
= blk_next(blk
)) != NULL
) {
701 if (!strcmp(name
, blk
->name
)) {
709 * Return the BlockDriverState attached to @blk if any, else null.
711 BlockDriverState
*blk_bs(BlockBackend
*blk
)
713 return blk
->root
? blk
->root
->bs
: NULL
;
716 static BlockBackend
*bdrv_first_blk(BlockDriverState
*bs
)
719 QLIST_FOREACH(child
, &bs
->parents
, next_parent
) {
720 if (child
->klass
== &child_root
) {
721 return child
->opaque
;
729 * Returns true if @bs has an associated BlockBackend.
731 bool bdrv_has_blk(BlockDriverState
*bs
)
733 return bdrv_first_blk(bs
) != NULL
;
737 * Returns true if @bs has only BlockBackends as parents.
739 bool bdrv_is_root_node(BlockDriverState
*bs
)
743 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
744 if (c
->klass
!= &child_root
) {
753 * Return @blk's DriveInfo if any, else null.
755 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
757 return blk
->legacy_dinfo
;
761 * Set @blk's DriveInfo to @dinfo, and return it.
762 * @blk must not have a DriveInfo set already.
763 * No other BlockBackend may have the same DriveInfo set.
765 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
767 assert(!blk
->legacy_dinfo
);
768 return blk
->legacy_dinfo
= dinfo
;
772 * Return the BlockBackend with DriveInfo @dinfo.
775 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
777 BlockBackend
*blk
= NULL
;
779 while ((blk
= blk_next(blk
)) != NULL
) {
780 if (blk
->legacy_dinfo
== dinfo
) {
788 * Returns a pointer to the publicly accessible fields of @blk.
790 BlockBackendPublic
*blk_get_public(BlockBackend
*blk
)
796 * Returns a BlockBackend given the associated @public fields.
798 BlockBackend
*blk_by_public(BlockBackendPublic
*public)
800 return container_of(public, BlockBackend
, public);
804 * Disassociates the currently associated BlockDriverState from @blk.
806 void blk_remove_bs(BlockBackend
*blk
)
808 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
809 BlockDriverState
*bs
;
811 notifier_list_notify(&blk
->remove_bs_notifiers
, blk
);
812 if (tgm
->throttle_state
) {
814 bdrv_drained_begin(bs
);
815 throttle_group_detach_aio_context(tgm
);
816 throttle_group_attach_aio_context(tgm
, qemu_get_aio_context());
817 bdrv_drained_end(bs
);
820 blk_update_root_state(blk
);
822 /* bdrv_root_unref_child() will cause blk->root to become stale and may
823 * switch to a completion coroutine later on. Let's drain all I/O here
824 * to avoid that and a potential QEMU crash.
827 bdrv_root_unref_child(blk
->root
);
832 * Associates a new BlockDriverState with @blk.
834 int blk_insert_bs(BlockBackend
*blk
, BlockDriverState
*bs
, Error
**errp
)
836 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
838 blk
->root
= bdrv_root_attach_child(bs
, "root", &child_root
, 0, blk
->ctx
,
839 blk
->perm
, blk
->shared_perm
, blk
, errp
);
840 if (blk
->root
== NULL
) {
844 notifier_list_notify(&blk
->insert_bs_notifiers
, blk
);
845 if (tgm
->throttle_state
) {
846 throttle_group_detach_aio_context(tgm
);
847 throttle_group_attach_aio_context(tgm
, bdrv_get_aio_context(bs
));
854 * Sets the permission bitmasks that the user of the BlockBackend needs.
856 int blk_set_perm(BlockBackend
*blk
, uint64_t perm
, uint64_t shared_perm
,
861 if (blk
->root
&& !blk
->disable_perm
) {
862 ret
= bdrv_child_try_set_perm(blk
->root
, perm
, shared_perm
, errp
);
869 blk
->shared_perm
= shared_perm
;
874 void blk_get_perm(BlockBackend
*blk
, uint64_t *perm
, uint64_t *shared_perm
)
877 *shared_perm
= blk
->shared_perm
;
881 * Attach device model @dev to @blk.
882 * Return 0 on success, -EBUSY when a device model is attached already.
884 int blk_attach_dev(BlockBackend
*blk
, DeviceState
*dev
)
890 /* While migration is still incoming, we don't need to apply the
891 * permissions of guest device BlockBackends. We might still have a block
892 * job or NBD server writing to the image for storage migration. */
893 if (runstate_check(RUN_STATE_INMIGRATE
)) {
894 blk
->disable_perm
= true;
899 blk_iostatus_reset(blk
);
905 * Detach device model @dev from @blk.
906 * @dev must be currently attached to @blk.
908 void blk_detach_dev(BlockBackend
*blk
, DeviceState
*dev
)
910 assert(blk
->dev
== dev
);
913 blk
->dev_opaque
= NULL
;
914 blk
->guest_block_size
= 512;
915 blk_set_perm(blk
, 0, BLK_PERM_ALL
, &error_abort
);
920 * Return the device model attached to @blk if any, else null.
922 DeviceState
*blk_get_attached_dev(BlockBackend
*blk
)
927 /* Return the qdev ID, or if no ID is assigned the QOM path, of the block
928 * device attached to the BlockBackend. */
929 char *blk_get_attached_dev_id(BlockBackend
*blk
)
931 DeviceState
*dev
= blk
->dev
;
935 } else if (dev
->id
) {
936 return g_strdup(dev
->id
);
939 return object_get_canonical_path(OBJECT(dev
)) ?: g_strdup("");
943 * Return the BlockBackend which has the device model @dev attached if it
946 * @dev must not be null.
948 BlockBackend
*blk_by_dev(void *dev
)
950 BlockBackend
*blk
= NULL
;
953 while ((blk
= blk_all_next(blk
)) != NULL
) {
954 if (blk
->dev
== dev
) {
962 * Set @blk's device model callbacks to @ops.
963 * @opaque is the opaque argument to pass to the callbacks.
964 * This is for use by device models.
966 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
970 blk
->dev_opaque
= opaque
;
972 /* Are we currently quiesced? Should we enforce this right now? */
973 if (blk
->quiesce_counter
&& ops
->drained_begin
) {
974 ops
->drained_begin(opaque
);
979 * Notify @blk's attached device model of media change.
981 * If @load is true, notify of media load. This action can fail, meaning that
982 * the medium cannot be loaded. @errp is set then.
984 * If @load is false, notify of media eject. This can never fail.
986 * Also send DEVICE_TRAY_MOVED events as appropriate.
988 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
, Error
**errp
)
990 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
991 bool tray_was_open
, tray_is_open
;
992 Error
*local_err
= NULL
;
994 tray_was_open
= blk_dev_is_tray_open(blk
);
995 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
, &local_err
);
997 assert(load
== true);
998 error_propagate(errp
, local_err
);
1001 tray_is_open
= blk_dev_is_tray_open(blk
);
1003 if (tray_was_open
!= tray_is_open
) {
1004 char *id
= blk_get_attached_dev_id(blk
);
1005 qapi_event_send_device_tray_moved(blk_name(blk
), id
, tray_is_open
);
1011 static void blk_root_change_media(BdrvChild
*child
, bool load
)
1013 blk_dev_change_media_cb(child
->opaque
, load
, NULL
);
1017 * Does @blk's attached device model have removable media?
1018 * %true if no device model is attached.
1020 bool blk_dev_has_removable_media(BlockBackend
*blk
)
1022 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
1026 * Does @blk's attached device model have a tray?
1028 bool blk_dev_has_tray(BlockBackend
*blk
)
1030 return blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
;
1034 * Notify @blk's attached device model of a media eject request.
1035 * If @force is true, the medium is about to be yanked out forcefully.
1037 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
1039 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
1040 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
1045 * Does @blk's attached device model have a tray, and is it open?
1047 bool blk_dev_is_tray_open(BlockBackend
*blk
)
1049 if (blk_dev_has_tray(blk
)) {
1050 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
1056 * Does @blk's attached device model have the medium locked?
1057 * %false if the device model has no such lock.
1059 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
1061 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
1062 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
1068 * Notify @blk's attached device model of a backend size change.
1070 static void blk_root_resize(BdrvChild
*child
)
1072 BlockBackend
*blk
= child
->opaque
;
1074 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
1075 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
1079 void blk_iostatus_enable(BlockBackend
*blk
)
1081 blk
->iostatus_enabled
= true;
1082 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
1085 /* The I/O status is only enabled if the drive explicitly
1086 * enables it _and_ the VM is configured to stop on errors */
1087 bool blk_iostatus_is_enabled(const BlockBackend
*blk
)
1089 return (blk
->iostatus_enabled
&&
1090 (blk
->on_write_error
== BLOCKDEV_ON_ERROR_ENOSPC
||
1091 blk
->on_write_error
== BLOCKDEV_ON_ERROR_STOP
||
1092 blk
->on_read_error
== BLOCKDEV_ON_ERROR_STOP
));
1095 BlockDeviceIoStatus
blk_iostatus(const BlockBackend
*blk
)
1097 return blk
->iostatus
;
1100 void blk_iostatus_disable(BlockBackend
*blk
)
1102 blk
->iostatus_enabled
= false;
1105 void blk_iostatus_reset(BlockBackend
*blk
)
1107 if (blk_iostatus_is_enabled(blk
)) {
1108 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
1112 void blk_iostatus_set_err(BlockBackend
*blk
, int error
)
1114 assert(blk_iostatus_is_enabled(blk
));
1115 if (blk
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
1116 blk
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
1117 BLOCK_DEVICE_IO_STATUS_FAILED
;
1121 void blk_set_allow_write_beyond_eof(BlockBackend
*blk
, bool allow
)
1123 blk
->allow_write_beyond_eof
= allow
;
1126 void blk_set_allow_aio_context_change(BlockBackend
*blk
, bool allow
)
1128 blk
->allow_aio_context_change
= allow
;
1131 void blk_set_disable_request_queuing(BlockBackend
*blk
, bool disable
)
1133 blk
->disable_request_queuing
= disable
;
1136 static int blk_check_byte_request(BlockBackend
*blk
, int64_t offset
,
1141 if (size
> INT_MAX
) {
1145 if (!blk_is_available(blk
)) {
1153 if (!blk
->allow_write_beyond_eof
) {
1154 len
= blk_getlength(blk
);
1159 if (offset
> len
|| len
- offset
< size
) {
1167 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1168 static void coroutine_fn
blk_wait_while_drained(BlockBackend
*blk
)
1170 assert(blk
->in_flight
> 0);
1172 if (blk
->quiesce_counter
&& !blk
->disable_request_queuing
) {
1173 blk_dec_in_flight(blk
);
1174 qemu_co_queue_wait(&blk
->queued_requests
, NULL
);
1175 blk_inc_in_flight(blk
);
1179 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1180 static int coroutine_fn
1181 blk_do_preadv(BlockBackend
*blk
, int64_t offset
, unsigned int bytes
,
1182 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
1185 BlockDriverState
*bs
;
1187 blk_wait_while_drained(blk
);
1189 /* Call blk_bs() only after waiting, the graph may have changed */
1191 trace_blk_co_preadv(blk
, bs
, offset
, bytes
, flags
);
1193 ret
= blk_check_byte_request(blk
, offset
, bytes
);
1198 bdrv_inc_in_flight(bs
);
1200 /* throttling disk I/O */
1201 if (blk
->public.throttle_group_member
.throttle_state
) {
1202 throttle_group_co_io_limits_intercept(&blk
->public.throttle_group_member
,
1206 ret
= bdrv_co_preadv(blk
->root
, offset
, bytes
, qiov
, flags
);
1207 bdrv_dec_in_flight(bs
);
1211 int coroutine_fn
blk_co_preadv(BlockBackend
*blk
, int64_t offset
,
1212 unsigned int bytes
, QEMUIOVector
*qiov
,
1213 BdrvRequestFlags flags
)
1217 blk_inc_in_flight(blk
);
1218 ret
= blk_do_preadv(blk
, offset
, bytes
, qiov
, flags
);
1219 blk_dec_in_flight(blk
);
1224 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1225 static int coroutine_fn
1226 blk_do_pwritev_part(BlockBackend
*blk
, int64_t offset
, unsigned int bytes
,
1227 QEMUIOVector
*qiov
, size_t qiov_offset
,
1228 BdrvRequestFlags flags
)
1231 BlockDriverState
*bs
;
1233 blk_wait_while_drained(blk
);
1235 /* Call blk_bs() only after waiting, the graph may have changed */
1237 trace_blk_co_pwritev(blk
, bs
, offset
, bytes
, flags
);
1239 ret
= blk_check_byte_request(blk
, offset
, bytes
);
1244 bdrv_inc_in_flight(bs
);
1245 /* throttling disk I/O */
1246 if (blk
->public.throttle_group_member
.throttle_state
) {
1247 throttle_group_co_io_limits_intercept(&blk
->public.throttle_group_member
,
1251 if (!blk
->enable_write_cache
) {
1252 flags
|= BDRV_REQ_FUA
;
1255 ret
= bdrv_co_pwritev_part(blk
->root
, offset
, bytes
, qiov
, qiov_offset
,
1257 bdrv_dec_in_flight(bs
);
1261 int coroutine_fn
blk_co_pwritev_part(BlockBackend
*blk
, int64_t offset
,
1263 QEMUIOVector
*qiov
, size_t qiov_offset
,
1264 BdrvRequestFlags flags
)
1268 blk_inc_in_flight(blk
);
1269 ret
= blk_do_pwritev_part(blk
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1270 blk_dec_in_flight(blk
);
1275 int coroutine_fn
blk_co_pwritev(BlockBackend
*blk
, int64_t offset
,
1276 unsigned int bytes
, QEMUIOVector
*qiov
,
1277 BdrvRequestFlags flags
)
1279 return blk_co_pwritev_part(blk
, offset
, bytes
, qiov
, 0, flags
);
1282 typedef struct BlkRwCo
{
1287 BdrvRequestFlags flags
;
1290 static void blk_read_entry(void *opaque
)
1292 BlkRwCo
*rwco
= opaque
;
1293 QEMUIOVector
*qiov
= rwco
->iobuf
;
1295 rwco
->ret
= blk_do_preadv(rwco
->blk
, rwco
->offset
, qiov
->size
,
1300 static void blk_write_entry(void *opaque
)
1302 BlkRwCo
*rwco
= opaque
;
1303 QEMUIOVector
*qiov
= rwco
->iobuf
;
1305 rwco
->ret
= blk_do_pwritev_part(rwco
->blk
, rwco
->offset
, qiov
->size
,
1306 qiov
, 0, rwco
->flags
);
1310 static int blk_prw(BlockBackend
*blk
, int64_t offset
, uint8_t *buf
,
1311 int64_t bytes
, CoroutineEntry co_entry
,
1312 BdrvRequestFlags flags
)
1314 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, bytes
);
1323 blk_inc_in_flight(blk
);
1324 if (qemu_in_coroutine()) {
1325 /* Fast-path if already in coroutine context */
1328 Coroutine
*co
= qemu_coroutine_create(co_entry
, &rwco
);
1329 bdrv_coroutine_enter(blk_bs(blk
), co
);
1330 BDRV_POLL_WHILE(blk_bs(blk
), rwco
.ret
== NOT_DONE
);
1332 blk_dec_in_flight(blk
);
1337 int blk_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
1338 int bytes
, BdrvRequestFlags flags
)
1340 return blk_prw(blk
, offset
, NULL
, bytes
, blk_write_entry
,
1341 flags
| BDRV_REQ_ZERO_WRITE
);
1344 int blk_make_zero(BlockBackend
*blk
, BdrvRequestFlags flags
)
1346 return bdrv_make_zero(blk
->root
, flags
);
1349 void blk_inc_in_flight(BlockBackend
*blk
)
1351 atomic_inc(&blk
->in_flight
);
1354 void blk_dec_in_flight(BlockBackend
*blk
)
1356 atomic_dec(&blk
->in_flight
);
1360 static void error_callback_bh(void *opaque
)
1362 struct BlockBackendAIOCB
*acb
= opaque
;
1364 blk_dec_in_flight(acb
->blk
);
1365 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1366 qemu_aio_unref(acb
);
1369 BlockAIOCB
*blk_abort_aio_request(BlockBackend
*blk
,
1370 BlockCompletionFunc
*cb
,
1371 void *opaque
, int ret
)
1373 struct BlockBackendAIOCB
*acb
;
1375 blk_inc_in_flight(blk
);
1376 acb
= blk_aio_get(&block_backend_aiocb_info
, blk
, cb
, opaque
);
1380 replay_bh_schedule_oneshot_event(blk_get_aio_context(blk
),
1381 error_callback_bh
, acb
);
1382 return &acb
->common
;
1385 typedef struct BlkAioEmAIOCB
{
1392 static const AIOCBInfo blk_aio_em_aiocb_info
= {
1393 .aiocb_size
= sizeof(BlkAioEmAIOCB
),
1396 static void blk_aio_complete(BlkAioEmAIOCB
*acb
)
1398 if (acb
->has_returned
) {
1399 acb
->common
.cb(acb
->common
.opaque
, acb
->rwco
.ret
);
1400 blk_dec_in_flight(acb
->rwco
.blk
);
1401 qemu_aio_unref(acb
);
1405 static void blk_aio_complete_bh(void *opaque
)
1407 BlkAioEmAIOCB
*acb
= opaque
;
1408 assert(acb
->has_returned
);
1409 blk_aio_complete(acb
);
1412 static BlockAIOCB
*blk_aio_prwv(BlockBackend
*blk
, int64_t offset
, int bytes
,
1413 void *iobuf
, CoroutineEntry co_entry
,
1414 BdrvRequestFlags flags
,
1415 BlockCompletionFunc
*cb
, void *opaque
)
1420 blk_inc_in_flight(blk
);
1421 acb
= blk_aio_get(&blk_aio_em_aiocb_info
, blk
, cb
, opaque
);
1422 acb
->rwco
= (BlkRwCo
) {
1430 acb
->has_returned
= false;
1432 co
= qemu_coroutine_create(co_entry
, acb
);
1433 bdrv_coroutine_enter(blk_bs(blk
), co
);
1435 acb
->has_returned
= true;
1436 if (acb
->rwco
.ret
!= NOT_DONE
) {
1437 replay_bh_schedule_oneshot_event(blk_get_aio_context(blk
),
1438 blk_aio_complete_bh
, acb
);
1441 return &acb
->common
;
1444 static void blk_aio_read_entry(void *opaque
)
1446 BlkAioEmAIOCB
*acb
= opaque
;
1447 BlkRwCo
*rwco
= &acb
->rwco
;
1448 QEMUIOVector
*qiov
= rwco
->iobuf
;
1450 assert(qiov
->size
== acb
->bytes
);
1451 rwco
->ret
= blk_do_preadv(rwco
->blk
, rwco
->offset
, acb
->bytes
,
1453 blk_aio_complete(acb
);
1456 static void blk_aio_write_entry(void *opaque
)
1458 BlkAioEmAIOCB
*acb
= opaque
;
1459 BlkRwCo
*rwco
= &acb
->rwco
;
1460 QEMUIOVector
*qiov
= rwco
->iobuf
;
1462 assert(!qiov
|| qiov
->size
== acb
->bytes
);
1463 rwco
->ret
= blk_do_pwritev_part(rwco
->blk
, rwco
->offset
, acb
->bytes
,
1464 qiov
, 0, rwco
->flags
);
1465 blk_aio_complete(acb
);
1468 BlockAIOCB
*blk_aio_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
1469 int count
, BdrvRequestFlags flags
,
1470 BlockCompletionFunc
*cb
, void *opaque
)
1472 return blk_aio_prwv(blk
, offset
, count
, NULL
, blk_aio_write_entry
,
1473 flags
| BDRV_REQ_ZERO_WRITE
, cb
, opaque
);
1476 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int count
)
1478 int ret
= blk_prw(blk
, offset
, buf
, count
, blk_read_entry
, 0);
1485 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int count
,
1486 BdrvRequestFlags flags
)
1488 int ret
= blk_prw(blk
, offset
, (void *) buf
, count
, blk_write_entry
,
1496 int64_t blk_getlength(BlockBackend
*blk
)
1498 if (!blk_is_available(blk
)) {
1502 return bdrv_getlength(blk_bs(blk
));
1505 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
1508 *nb_sectors_ptr
= 0;
1510 bdrv_get_geometry(blk_bs(blk
), nb_sectors_ptr
);
1514 int64_t blk_nb_sectors(BlockBackend
*blk
)
1516 if (!blk_is_available(blk
)) {
1520 return bdrv_nb_sectors(blk_bs(blk
));
1523 BlockAIOCB
*blk_aio_preadv(BlockBackend
*blk
, int64_t offset
,
1524 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
1525 BlockCompletionFunc
*cb
, void *opaque
)
1527 return blk_aio_prwv(blk
, offset
, qiov
->size
, qiov
,
1528 blk_aio_read_entry
, flags
, cb
, opaque
);
1531 BlockAIOCB
*blk_aio_pwritev(BlockBackend
*blk
, int64_t offset
,
1532 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
1533 BlockCompletionFunc
*cb
, void *opaque
)
1535 return blk_aio_prwv(blk
, offset
, qiov
->size
, qiov
,
1536 blk_aio_write_entry
, flags
, cb
, opaque
);
1539 void blk_aio_cancel(BlockAIOCB
*acb
)
1541 bdrv_aio_cancel(acb
);
1544 void blk_aio_cancel_async(BlockAIOCB
*acb
)
1546 bdrv_aio_cancel_async(acb
);
1549 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1550 static int coroutine_fn
1551 blk_do_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
1553 blk_wait_while_drained(blk
);
1555 if (!blk_is_available(blk
)) {
1559 return bdrv_co_ioctl(blk_bs(blk
), req
, buf
);
1562 static void blk_ioctl_entry(void *opaque
)
1564 BlkRwCo
*rwco
= opaque
;
1565 QEMUIOVector
*qiov
= rwco
->iobuf
;
1567 rwco
->ret
= blk_do_ioctl(rwco
->blk
, rwco
->offset
, qiov
->iov
[0].iov_base
);
1571 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
1573 return blk_prw(blk
, req
, buf
, 0, blk_ioctl_entry
, 0);
1576 static void blk_aio_ioctl_entry(void *opaque
)
1578 BlkAioEmAIOCB
*acb
= opaque
;
1579 BlkRwCo
*rwco
= &acb
->rwco
;
1581 rwco
->ret
= blk_do_ioctl(rwco
->blk
, rwco
->offset
, rwco
->iobuf
);
1583 blk_aio_complete(acb
);
1586 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
1587 BlockCompletionFunc
*cb
, void *opaque
)
1589 return blk_aio_prwv(blk
, req
, 0, buf
, blk_aio_ioctl_entry
, 0, cb
, opaque
);
1592 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1593 static int coroutine_fn
1594 blk_do_pdiscard(BlockBackend
*blk
, int64_t offset
, int bytes
)
1598 blk_wait_while_drained(blk
);
1600 ret
= blk_check_byte_request(blk
, offset
, bytes
);
1605 return bdrv_co_pdiscard(blk
->root
, offset
, bytes
);
1608 static void blk_aio_pdiscard_entry(void *opaque
)
1610 BlkAioEmAIOCB
*acb
= opaque
;
1611 BlkRwCo
*rwco
= &acb
->rwco
;
1613 rwco
->ret
= blk_do_pdiscard(rwco
->blk
, rwco
->offset
, acb
->bytes
);
1614 blk_aio_complete(acb
);
1617 BlockAIOCB
*blk_aio_pdiscard(BlockBackend
*blk
,
1618 int64_t offset
, int bytes
,
1619 BlockCompletionFunc
*cb
, void *opaque
)
1621 return blk_aio_prwv(blk
, offset
, bytes
, NULL
, blk_aio_pdiscard_entry
, 0,
1625 int coroutine_fn
blk_co_pdiscard(BlockBackend
*blk
, int64_t offset
, int bytes
)
1629 blk_inc_in_flight(blk
);
1630 ret
= blk_do_pdiscard(blk
, offset
, bytes
);
1631 blk_dec_in_flight(blk
);
1636 static void blk_pdiscard_entry(void *opaque
)
1638 BlkRwCo
*rwco
= opaque
;
1639 QEMUIOVector
*qiov
= rwco
->iobuf
;
1641 rwco
->ret
= blk_do_pdiscard(rwco
->blk
, rwco
->offset
, qiov
->size
);
1645 int blk_pdiscard(BlockBackend
*blk
, int64_t offset
, int bytes
)
1647 return blk_prw(blk
, offset
, NULL
, bytes
, blk_pdiscard_entry
, 0);
1650 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1651 static int coroutine_fn
blk_do_flush(BlockBackend
*blk
)
1653 blk_wait_while_drained(blk
);
1655 if (!blk_is_available(blk
)) {
1659 return bdrv_co_flush(blk_bs(blk
));
1662 static void blk_aio_flush_entry(void *opaque
)
1664 BlkAioEmAIOCB
*acb
= opaque
;
1665 BlkRwCo
*rwco
= &acb
->rwco
;
1667 rwco
->ret
= blk_do_flush(rwco
->blk
);
1668 blk_aio_complete(acb
);
1671 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
1672 BlockCompletionFunc
*cb
, void *opaque
)
1674 return blk_aio_prwv(blk
, 0, 0, NULL
, blk_aio_flush_entry
, 0, cb
, opaque
);
1677 int coroutine_fn
blk_co_flush(BlockBackend
*blk
)
1681 blk_inc_in_flight(blk
);
1682 ret
= blk_do_flush(blk
);
1683 blk_dec_in_flight(blk
);
1688 static void blk_flush_entry(void *opaque
)
1690 BlkRwCo
*rwco
= opaque
;
1691 rwco
->ret
= blk_do_flush(rwco
->blk
);
1695 int blk_flush(BlockBackend
*blk
)
1697 return blk_prw(blk
, 0, NULL
, 0, blk_flush_entry
, 0);
1700 void blk_drain(BlockBackend
*blk
)
1702 BlockDriverState
*bs
= blk_bs(blk
);
1705 bdrv_drained_begin(bs
);
1708 /* We may have -ENOMEDIUM completions in flight */
1709 AIO_WAIT_WHILE(blk_get_aio_context(blk
),
1710 atomic_mb_read(&blk
->in_flight
) > 0);
1713 bdrv_drained_end(bs
);
1717 void blk_drain_all(void)
1719 BlockBackend
*blk
= NULL
;
1721 bdrv_drain_all_begin();
1723 while ((blk
= blk_all_next(blk
)) != NULL
) {
1724 AioContext
*ctx
= blk_get_aio_context(blk
);
1726 aio_context_acquire(ctx
);
1728 /* We may have -ENOMEDIUM completions in flight */
1729 AIO_WAIT_WHILE(ctx
, atomic_mb_read(&blk
->in_flight
) > 0);
1731 aio_context_release(ctx
);
1734 bdrv_drain_all_end();
1737 void blk_set_on_error(BlockBackend
*blk
, BlockdevOnError on_read_error
,
1738 BlockdevOnError on_write_error
)
1740 blk
->on_read_error
= on_read_error
;
1741 blk
->on_write_error
= on_write_error
;
1744 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
1746 return is_read
? blk
->on_read_error
: blk
->on_write_error
;
1749 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
1752 BlockdevOnError on_err
= blk_get_on_error(blk
, is_read
);
1755 case BLOCKDEV_ON_ERROR_ENOSPC
:
1756 return (error
== ENOSPC
) ?
1757 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
1758 case BLOCKDEV_ON_ERROR_STOP
:
1759 return BLOCK_ERROR_ACTION_STOP
;
1760 case BLOCKDEV_ON_ERROR_REPORT
:
1761 return BLOCK_ERROR_ACTION_REPORT
;
1762 case BLOCKDEV_ON_ERROR_IGNORE
:
1763 return BLOCK_ERROR_ACTION_IGNORE
;
1764 case BLOCKDEV_ON_ERROR_AUTO
:
1770 static void send_qmp_error_event(BlockBackend
*blk
,
1771 BlockErrorAction action
,
1772 bool is_read
, int error
)
1774 IoOperationType optype
;
1775 BlockDriverState
*bs
= blk_bs(blk
);
1777 optype
= is_read
? IO_OPERATION_TYPE_READ
: IO_OPERATION_TYPE_WRITE
;
1778 qapi_event_send_block_io_error(blk_name(blk
), !!bs
,
1779 bs
? bdrv_get_node_name(bs
) : NULL
, optype
,
1780 action
, blk_iostatus_is_enabled(blk
),
1781 error
== ENOSPC
, strerror(error
));
1784 /* This is done by device models because, while the block layer knows
1785 * about the error, it does not know whether an operation comes from
1786 * the device or the block layer (from a job, for example).
1788 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
1789 bool is_read
, int error
)
1793 if (action
== BLOCK_ERROR_ACTION_STOP
) {
1794 /* First set the iostatus, so that "info block" returns an iostatus
1795 * that matches the events raised so far (an additional error iostatus
1796 * is fine, but not a lost one).
1798 blk_iostatus_set_err(blk
, error
);
1800 /* Then raise the request to stop the VM and the event.
1801 * qemu_system_vmstop_request_prepare has two effects. First,
1802 * it ensures that the STOP event always comes after the
1803 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1804 * can observe the STOP event and do a "cont" before the STOP
1805 * event is issued, the VM will not stop. In this case, vm_start()
1806 * also ensures that the STOP/RESUME pair of events is emitted.
1808 qemu_system_vmstop_request_prepare();
1809 send_qmp_error_event(blk
, action
, is_read
, error
);
1810 qemu_system_vmstop_request(RUN_STATE_IO_ERROR
);
1812 send_qmp_error_event(blk
, action
, is_read
, error
);
1816 bool blk_is_read_only(BlockBackend
*blk
)
1818 BlockDriverState
*bs
= blk_bs(blk
);
1821 return bdrv_is_read_only(bs
);
1823 return blk
->root_state
.read_only
;
1827 bool blk_is_sg(BlockBackend
*blk
)
1829 BlockDriverState
*bs
= blk_bs(blk
);
1835 return bdrv_is_sg(bs
);
1838 bool blk_enable_write_cache(BlockBackend
*blk
)
1840 return blk
->enable_write_cache
;
1843 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
1845 blk
->enable_write_cache
= wce
;
1848 void blk_invalidate_cache(BlockBackend
*blk
, Error
**errp
)
1850 BlockDriverState
*bs
= blk_bs(blk
);
1853 error_setg(errp
, "Device '%s' has no medium", blk
->name
);
1857 bdrv_invalidate_cache(bs
, errp
);
1860 bool blk_is_inserted(BlockBackend
*blk
)
1862 BlockDriverState
*bs
= blk_bs(blk
);
1864 return bs
&& bdrv_is_inserted(bs
);
1867 bool blk_is_available(BlockBackend
*blk
)
1869 return blk_is_inserted(blk
) && !blk_dev_is_tray_open(blk
);
1872 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
1874 BlockDriverState
*bs
= blk_bs(blk
);
1877 bdrv_lock_medium(bs
, locked
);
1881 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
1883 BlockDriverState
*bs
= blk_bs(blk
);
1887 bdrv_eject(bs
, eject_flag
);
1890 /* Whether or not we ejected on the backend,
1891 * the frontend experienced a tray event. */
1892 id
= blk_get_attached_dev_id(blk
);
1893 qapi_event_send_device_tray_moved(blk_name(blk
), id
,
1898 int blk_get_flags(BlockBackend
*blk
)
1900 BlockDriverState
*bs
= blk_bs(blk
);
1903 return bdrv_get_flags(bs
);
1905 return blk
->root_state
.open_flags
;
1909 /* Returns the minimum request alignment, in bytes; guaranteed nonzero */
1910 uint32_t blk_get_request_alignment(BlockBackend
*blk
)
1912 BlockDriverState
*bs
= blk_bs(blk
);
1913 return bs
? bs
->bl
.request_alignment
: BDRV_SECTOR_SIZE
;
1916 /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
1917 uint32_t blk_get_max_transfer(BlockBackend
*blk
)
1919 BlockDriverState
*bs
= blk_bs(blk
);
1923 max
= bs
->bl
.max_transfer
;
1925 return MIN_NON_ZERO(max
, INT_MAX
);
1928 int blk_get_max_iov(BlockBackend
*blk
)
1930 return blk
->root
->bs
->bl
.max_iov
;
1933 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
1935 blk
->guest_block_size
= align
;
1938 void *blk_try_blockalign(BlockBackend
*blk
, size_t size
)
1940 return qemu_try_blockalign(blk
? blk_bs(blk
) : NULL
, size
);
1943 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
1945 return qemu_blockalign(blk
? blk_bs(blk
) : NULL
, size
);
1948 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
1950 BlockDriverState
*bs
= blk_bs(blk
);
1956 return bdrv_op_is_blocked(bs
, op
, errp
);
1959 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
1961 BlockDriverState
*bs
= blk_bs(blk
);
1964 bdrv_op_unblock(bs
, op
, reason
);
1968 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
1970 BlockDriverState
*bs
= blk_bs(blk
);
1973 bdrv_op_block_all(bs
, reason
);
1977 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
1979 BlockDriverState
*bs
= blk_bs(blk
);
1982 bdrv_op_unblock_all(bs
, reason
);
1986 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
1988 BlockDriverState
*bs
= blk_bs(blk
);
1991 AioContext
*ctx
= bdrv_get_aio_context(blk_bs(blk
));
1992 assert(ctx
== blk
->ctx
);
1998 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
)
2000 BlockBackendAIOCB
*blk_acb
= DO_UPCAST(BlockBackendAIOCB
, common
, acb
);
2001 return blk_get_aio_context(blk_acb
->blk
);
2004 static int blk_do_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
,
2005 bool update_root_node
, Error
**errp
)
2007 BlockDriverState
*bs
= blk_bs(blk
);
2008 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
2012 if (update_root_node
) {
2013 ret
= bdrv_child_try_set_aio_context(bs
, new_context
, blk
->root
,
2019 if (tgm
->throttle_state
) {
2020 bdrv_drained_begin(bs
);
2021 throttle_group_detach_aio_context(tgm
);
2022 throttle_group_attach_aio_context(tgm
, new_context
);
2023 bdrv_drained_end(bs
);
2027 blk
->ctx
= new_context
;
2031 int blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
,
2034 return blk_do_set_aio_context(blk
, new_context
, true, errp
);
2037 static bool blk_root_can_set_aio_ctx(BdrvChild
*child
, AioContext
*ctx
,
2038 GSList
**ignore
, Error
**errp
)
2040 BlockBackend
*blk
= child
->opaque
;
2042 if (blk
->allow_aio_context_change
) {
2046 /* Only manually created BlockBackends that are not attached to anything
2047 * can change their AioContext without updating their user. */
2048 if (!blk
->name
|| blk
->dev
) {
2049 /* TODO Add BB name/QOM path */
2050 error_setg(errp
, "Cannot change iothread of active block backend");
2057 static void blk_root_set_aio_ctx(BdrvChild
*child
, AioContext
*ctx
,
2060 BlockBackend
*blk
= child
->opaque
;
2061 blk_do_set_aio_context(blk
, ctx
, false, &error_abort
);
2064 void blk_add_aio_context_notifier(BlockBackend
*blk
,
2065 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
2066 void (*detach_aio_context
)(void *opaque
), void *opaque
)
2068 BlockBackendAioNotifier
*notifier
;
2069 BlockDriverState
*bs
= blk_bs(blk
);
2071 notifier
= g_new(BlockBackendAioNotifier
, 1);
2072 notifier
->attached_aio_context
= attached_aio_context
;
2073 notifier
->detach_aio_context
= detach_aio_context
;
2074 notifier
->opaque
= opaque
;
2075 QLIST_INSERT_HEAD(&blk
->aio_notifiers
, notifier
, list
);
2078 bdrv_add_aio_context_notifier(bs
, attached_aio_context
,
2079 detach_aio_context
, opaque
);
2083 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
2084 void (*attached_aio_context
)(AioContext
*,
2086 void (*detach_aio_context
)(void *),
2089 BlockBackendAioNotifier
*notifier
;
2090 BlockDriverState
*bs
= blk_bs(blk
);
2093 bdrv_remove_aio_context_notifier(bs
, attached_aio_context
,
2094 detach_aio_context
, opaque
);
2097 QLIST_FOREACH(notifier
, &blk
->aio_notifiers
, list
) {
2098 if (notifier
->attached_aio_context
== attached_aio_context
&&
2099 notifier
->detach_aio_context
== detach_aio_context
&&
2100 notifier
->opaque
== opaque
) {
2101 QLIST_REMOVE(notifier
, list
);
2110 void blk_add_remove_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
2112 notifier_list_add(&blk
->remove_bs_notifiers
, notify
);
2115 void blk_add_insert_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
2117 notifier_list_add(&blk
->insert_bs_notifiers
, notify
);
2120 void blk_io_plug(BlockBackend
*blk
)
2122 BlockDriverState
*bs
= blk_bs(blk
);
2129 void blk_io_unplug(BlockBackend
*blk
)
2131 BlockDriverState
*bs
= blk_bs(blk
);
2138 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
2143 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
2144 BlockCompletionFunc
*cb
, void *opaque
)
2146 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
2149 int coroutine_fn
blk_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
2150 int bytes
, BdrvRequestFlags flags
)
2152 return blk_co_pwritev(blk
, offset
, bytes
, NULL
,
2153 flags
| BDRV_REQ_ZERO_WRITE
);
2156 int blk_pwrite_compressed(BlockBackend
*blk
, int64_t offset
, const void *buf
,
2159 return blk_prw(blk
, offset
, (void *) buf
, count
, blk_write_entry
,
2160 BDRV_REQ_WRITE_COMPRESSED
);
2163 int blk_truncate(BlockBackend
*blk
, int64_t offset
, bool exact
,
2164 PreallocMode prealloc
, BdrvRequestFlags flags
, Error
**errp
)
2166 if (!blk_is_available(blk
)) {
2167 error_setg(errp
, "No medium inserted");
2171 return bdrv_truncate(blk
->root
, offset
, exact
, prealloc
, flags
, errp
);
2174 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
2175 int64_t pos
, int size
)
2179 if (!blk_is_available(blk
)) {
2183 ret
= bdrv_save_vmstate(blk_bs(blk
), buf
, pos
, size
);
2188 if (ret
== size
&& !blk
->enable_write_cache
) {
2189 ret
= bdrv_flush(blk_bs(blk
));
2192 return ret
< 0 ? ret
: size
;
2195 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
2197 if (!blk_is_available(blk
)) {
2201 return bdrv_load_vmstate(blk_bs(blk
), buf
, pos
, size
);
2204 int blk_probe_blocksizes(BlockBackend
*blk
, BlockSizes
*bsz
)
2206 if (!blk_is_available(blk
)) {
2210 return bdrv_probe_blocksizes(blk_bs(blk
), bsz
);
2213 int blk_probe_geometry(BlockBackend
*blk
, HDGeometry
*geo
)
2215 if (!blk_is_available(blk
)) {
2219 return bdrv_probe_geometry(blk_bs(blk
), geo
);
2223 * Updates the BlockBackendRootState object with data from the currently
2224 * attached BlockDriverState.
2226 void blk_update_root_state(BlockBackend
*blk
)
2230 blk
->root_state
.open_flags
= blk
->root
->bs
->open_flags
;
2231 blk
->root_state
.read_only
= blk
->root
->bs
->read_only
;
2232 blk
->root_state
.detect_zeroes
= blk
->root
->bs
->detect_zeroes
;
2236 * Returns the detect-zeroes setting to be used for bdrv_open() of a
2237 * BlockDriverState which is supposed to inherit the root state.
2239 bool blk_get_detect_zeroes_from_root_state(BlockBackend
*blk
)
2241 return blk
->root_state
.detect_zeroes
;
2245 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
2246 * supposed to inherit the root state.
2248 int blk_get_open_flags_from_root_state(BlockBackend
*blk
)
2252 bs_flags
= blk
->root_state
.read_only
? 0 : BDRV_O_RDWR
;
2253 bs_flags
|= blk
->root_state
.open_flags
& ~BDRV_O_RDWR
;
2258 BlockBackendRootState
*blk_get_root_state(BlockBackend
*blk
)
2260 return &blk
->root_state
;
2263 int blk_commit_all(void)
2265 BlockBackend
*blk
= NULL
;
2267 while ((blk
= blk_all_next(blk
)) != NULL
) {
2268 AioContext
*aio_context
= blk_get_aio_context(blk
);
2270 aio_context_acquire(aio_context
);
2271 if (blk_is_inserted(blk
) && blk
->root
->bs
->backing
) {
2272 int ret
= bdrv_commit(blk
->root
->bs
);
2274 aio_context_release(aio_context
);
2278 aio_context_release(aio_context
);
2284 /* throttling disk I/O limits */
2285 void blk_set_io_limits(BlockBackend
*blk
, ThrottleConfig
*cfg
)
2287 throttle_group_config(&blk
->public.throttle_group_member
, cfg
);
2290 void blk_io_limits_disable(BlockBackend
*blk
)
2292 BlockDriverState
*bs
= blk_bs(blk
);
2293 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
2294 assert(tgm
->throttle_state
);
2296 bdrv_drained_begin(bs
);
2298 throttle_group_unregister_tgm(tgm
);
2300 bdrv_drained_end(bs
);
2304 /* should be called before blk_set_io_limits if a limit is set */
2305 void blk_io_limits_enable(BlockBackend
*blk
, const char *group
)
2307 assert(!blk
->public.throttle_group_member
.throttle_state
);
2308 throttle_group_register_tgm(&blk
->public.throttle_group_member
,
2309 group
, blk_get_aio_context(blk
));
2312 void blk_io_limits_update_group(BlockBackend
*blk
, const char *group
)
2314 /* this BB is not part of any group */
2315 if (!blk
->public.throttle_group_member
.throttle_state
) {
2319 /* this BB is a part of the same group than the one we want */
2320 if (!g_strcmp0(throttle_group_get_name(&blk
->public.throttle_group_member
),
2325 /* need to change the group this bs belong to */
2326 blk_io_limits_disable(blk
);
2327 blk_io_limits_enable(blk
, group
);
2330 static void blk_root_drained_begin(BdrvChild
*child
)
2332 BlockBackend
*blk
= child
->opaque
;
2334 if (++blk
->quiesce_counter
== 1) {
2335 if (blk
->dev_ops
&& blk
->dev_ops
->drained_begin
) {
2336 blk
->dev_ops
->drained_begin(blk
->dev_opaque
);
2340 /* Note that blk->root may not be accessible here yet if we are just
2341 * attaching to a BlockDriverState that is drained. Use child instead. */
2343 if (atomic_fetch_inc(&blk
->public.throttle_group_member
.io_limits_disabled
) == 0) {
2344 throttle_group_restart_tgm(&blk
->public.throttle_group_member
);
2348 static bool blk_root_drained_poll(BdrvChild
*child
)
2350 BlockBackend
*blk
= child
->opaque
;
2351 assert(blk
->quiesce_counter
);
2352 return !!blk
->in_flight
;
2355 static void blk_root_drained_end(BdrvChild
*child
, int *drained_end_counter
)
2357 BlockBackend
*blk
= child
->opaque
;
2358 assert(blk
->quiesce_counter
);
2360 assert(blk
->public.throttle_group_member
.io_limits_disabled
);
2361 atomic_dec(&blk
->public.throttle_group_member
.io_limits_disabled
);
2363 if (--blk
->quiesce_counter
== 0) {
2364 if (blk
->dev_ops
&& blk
->dev_ops
->drained_end
) {
2365 blk
->dev_ops
->drained_end(blk
->dev_opaque
);
2367 while (qemu_co_enter_next(&blk
->queued_requests
, NULL
)) {
2368 /* Resume all queued requests */
2373 void blk_register_buf(BlockBackend
*blk
, void *host
, size_t size
)
2375 bdrv_register_buf(blk_bs(blk
), host
, size
);
2378 void blk_unregister_buf(BlockBackend
*blk
, void *host
)
2380 bdrv_unregister_buf(blk_bs(blk
), host
);
2383 int coroutine_fn
blk_co_copy_range(BlockBackend
*blk_in
, int64_t off_in
,
2384 BlockBackend
*blk_out
, int64_t off_out
,
2385 int bytes
, BdrvRequestFlags read_flags
,
2386 BdrvRequestFlags write_flags
)
2389 r
= blk_check_byte_request(blk_in
, off_in
, bytes
);
2393 r
= blk_check_byte_request(blk_out
, off_out
, bytes
);
2397 return bdrv_co_copy_range(blk_in
->root
, off_in
,
2398 blk_out
->root
, off_out
,
2399 bytes
, read_flags
, write_flags
);
2402 const BdrvChild
*blk_root(BlockBackend
*blk
)
2407 int blk_make_empty(BlockBackend
*blk
, Error
**errp
)
2409 if (!blk_is_available(blk
)) {
2410 error_setg(errp
, "No medium inserted");
2414 return bdrv_make_empty(blk
->root
, errp
);