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/coroutines.h"
18 #include "block/throttle-groups.h"
19 #include "hw/qdev-core.h"
20 #include "sysemu/blockdev.h"
21 #include "sysemu/runstate.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 /* Protected by BQL */
83 NotifierList remove_bs_notifiers
, insert_bs_notifiers
;
84 QLIST_HEAD(, BlockBackendAioNotifier
) aio_notifiers
;
87 CoQueue queued_requests
;
88 bool disable_request_queuing
;
90 VMChangeStateEntry
*vmsh
;
91 bool force_allow_inactivate
;
93 /* Number of in-flight aio requests. BlockDriverState also counts
94 * in-flight requests but aio requests can exist even when blk->root is
95 * NULL, so we cannot rely on its counter for that case.
96 * Accessed with atomic ops.
98 unsigned int in_flight
;
101 typedef struct BlockBackendAIOCB
{
107 static const AIOCBInfo block_backend_aiocb_info
= {
108 .get_aio_context
= blk_aiocb_get_aio_context
,
109 .aiocb_size
= sizeof(BlockBackendAIOCB
),
112 static void drive_info_del(DriveInfo
*dinfo
);
113 static BlockBackend
*bdrv_first_blk(BlockDriverState
*bs
);
115 /* All BlockBackends. Protected by BQL. */
116 static QTAILQ_HEAD(, BlockBackend
) block_backends
=
117 QTAILQ_HEAD_INITIALIZER(block_backends
);
120 * All BlockBackends referenced by the monitor and which are iterated through by
121 * blk_next(). Protected by BQL.
123 static QTAILQ_HEAD(, BlockBackend
) monitor_block_backends
=
124 QTAILQ_HEAD_INITIALIZER(monitor_block_backends
);
126 static void blk_root_inherit_options(BdrvChildRole role
, bool parent_is_format
,
127 int *child_flags
, QDict
*child_options
,
128 int parent_flags
, QDict
*parent_options
)
130 /* We're not supposed to call this function for root nodes */
133 static void blk_root_drained_begin(BdrvChild
*child
);
134 static bool blk_root_drained_poll(BdrvChild
*child
);
135 static void blk_root_drained_end(BdrvChild
*child
, int *drained_end_counter
);
137 static void blk_root_change_media(BdrvChild
*child
, bool load
);
138 static void blk_root_resize(BdrvChild
*child
);
140 static bool blk_root_can_set_aio_ctx(BdrvChild
*child
, AioContext
*ctx
,
141 GSList
**ignore
, Error
**errp
);
142 static void blk_root_set_aio_ctx(BdrvChild
*child
, AioContext
*ctx
,
145 static char *blk_root_get_parent_desc(BdrvChild
*child
)
147 BlockBackend
*blk
= child
->opaque
;
148 g_autofree
char *dev_id
= NULL
;
151 return g_strdup_printf("block device '%s'", blk
->name
);
154 dev_id
= blk_get_attached_dev_id(blk
);
156 return g_strdup_printf("block device '%s'", dev_id
);
158 /* TODO Callback into the BB owner for something more detailed */
159 return g_strdup("an unnamed block device");
163 static const char *blk_root_get_name(BdrvChild
*child
)
165 return blk_name(child
->opaque
);
168 static void blk_vm_state_changed(void *opaque
, bool running
, RunState state
)
170 Error
*local_err
= NULL
;
171 BlockBackend
*blk
= opaque
;
173 if (state
== RUN_STATE_INMIGRATE
) {
177 qemu_del_vm_change_state_handler(blk
->vmsh
);
179 blk_set_perm(blk
, blk
->perm
, blk
->shared_perm
, &local_err
);
181 error_report_err(local_err
);
186 * Notifies the user of the BlockBackend that migration has completed. qdev
187 * devices can tighten their permissions in response (specifically revoke
188 * shared write permissions that we needed for storage migration).
190 * If an error is returned, the VM cannot be allowed to be resumed.
192 static void blk_root_activate(BdrvChild
*child
, Error
**errp
)
194 BlockBackend
*blk
= child
->opaque
;
195 Error
*local_err
= NULL
;
196 uint64_t saved_shared_perm
;
198 if (!blk
->disable_perm
) {
202 blk
->disable_perm
= false;
205 * blk->shared_perm contains the permissions we want to share once
206 * migration is really completely done. For now, we need to share
207 * all; but we also need to retain blk->shared_perm, which is
208 * overwritten by a successful blk_set_perm() call. Save it and
211 saved_shared_perm
= blk
->shared_perm
;
213 blk_set_perm(blk
, blk
->perm
, BLK_PERM_ALL
, &local_err
);
215 error_propagate(errp
, local_err
);
216 blk
->disable_perm
= true;
219 blk
->shared_perm
= saved_shared_perm
;
221 if (runstate_check(RUN_STATE_INMIGRATE
)) {
222 /* Activation can happen when migration process is still active, for
223 * example when nbd_server_add is called during non-shared storage
224 * migration. Defer the shared_perm update to migration completion. */
226 blk
->vmsh
= qemu_add_vm_change_state_handler(blk_vm_state_changed
,
232 blk_set_perm(blk
, blk
->perm
, blk
->shared_perm
, &local_err
);
234 error_propagate(errp
, local_err
);
235 blk
->disable_perm
= true;
240 void blk_set_force_allow_inactivate(BlockBackend
*blk
)
243 blk
->force_allow_inactivate
= true;
246 static bool blk_can_inactivate(BlockBackend
*blk
)
248 /* If it is a guest device, inactivate is ok. */
249 if (blk
->dev
|| blk_name(blk
)[0]) {
253 /* Inactivating means no more writes to the image can be done,
254 * even if those writes would be changes invisible to the
255 * guest. For block job BBs that satisfy this, we can just allow
256 * it. This is the case for mirror job source, which is required
257 * by libvirt non-shared block migration. */
258 if (!(blk
->perm
& (BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
))) {
262 return blk
->force_allow_inactivate
;
265 static int blk_root_inactivate(BdrvChild
*child
)
267 BlockBackend
*blk
= child
->opaque
;
269 if (blk
->disable_perm
) {
273 if (!blk_can_inactivate(blk
)) {
277 blk
->disable_perm
= true;
279 bdrv_child_try_set_perm(blk
->root
, 0, BLK_PERM_ALL
, &error_abort
);
285 static void blk_root_attach(BdrvChild
*child
)
287 BlockBackend
*blk
= child
->opaque
;
288 BlockBackendAioNotifier
*notifier
;
290 trace_blk_root_attach(child
, blk
, child
->bs
);
292 QLIST_FOREACH(notifier
, &blk
->aio_notifiers
, list
) {
293 bdrv_add_aio_context_notifier(child
->bs
,
294 notifier
->attached_aio_context
,
295 notifier
->detach_aio_context
,
300 static void blk_root_detach(BdrvChild
*child
)
302 BlockBackend
*blk
= child
->opaque
;
303 BlockBackendAioNotifier
*notifier
;
305 trace_blk_root_detach(child
, blk
, child
->bs
);
307 QLIST_FOREACH(notifier
, &blk
->aio_notifiers
, list
) {
308 bdrv_remove_aio_context_notifier(child
->bs
,
309 notifier
->attached_aio_context
,
310 notifier
->detach_aio_context
,
315 static AioContext
*blk_root_get_parent_aio_context(BdrvChild
*c
)
317 BlockBackend
*blk
= c
->opaque
;
319 return blk_get_aio_context(blk
);
322 static const BdrvChildClass child_root
= {
323 .inherit_options
= blk_root_inherit_options
,
325 .change_media
= blk_root_change_media
,
326 .resize
= blk_root_resize
,
327 .get_name
= blk_root_get_name
,
328 .get_parent_desc
= blk_root_get_parent_desc
,
330 .drained_begin
= blk_root_drained_begin
,
331 .drained_poll
= blk_root_drained_poll
,
332 .drained_end
= blk_root_drained_end
,
334 .activate
= blk_root_activate
,
335 .inactivate
= blk_root_inactivate
,
337 .attach
= blk_root_attach
,
338 .detach
= blk_root_detach
,
340 .can_set_aio_ctx
= blk_root_can_set_aio_ctx
,
341 .set_aio_ctx
= blk_root_set_aio_ctx
,
343 .get_parent_aio_context
= blk_root_get_parent_aio_context
,
347 * Create a new BlockBackend with a reference count of one.
349 * @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
350 * to request for a block driver node that is attached to this BlockBackend.
351 * @shared_perm is a bitmask which describes which permissions may be granted
352 * to other users of the attached node.
353 * Both sets of permissions can be changed later using blk_set_perm().
355 * Return the new BlockBackend on success, null on failure.
357 BlockBackend
*blk_new(AioContext
*ctx
, uint64_t perm
, uint64_t shared_perm
)
363 blk
= g_new0(BlockBackend
, 1);
367 blk
->shared_perm
= shared_perm
;
368 blk_set_enable_write_cache(blk
, true);
370 blk
->on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
371 blk
->on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
373 block_acct_init(&blk
->stats
);
375 qemu_co_queue_init(&blk
->queued_requests
);
376 notifier_list_init(&blk
->remove_bs_notifiers
);
377 notifier_list_init(&blk
->insert_bs_notifiers
);
378 QLIST_INIT(&blk
->aio_notifiers
);
380 QTAILQ_INSERT_TAIL(&block_backends
, blk
, link
);
385 * Create a new BlockBackend connected to an existing BlockDriverState.
387 * @perm is a bitmasks of BLK_PERM_* constants which describes the
388 * permissions to request for @bs that is attached to this
389 * BlockBackend. @shared_perm is a bitmask which describes which
390 * permissions may be granted to other users of the attached node.
391 * Both sets of permissions can be changed later using blk_set_perm().
393 * Return the new BlockBackend on success, null on failure.
395 BlockBackend
*blk_new_with_bs(BlockDriverState
*bs
, uint64_t perm
,
396 uint64_t shared_perm
, Error
**errp
)
398 BlockBackend
*blk
= blk_new(bdrv_get_aio_context(bs
), perm
, shared_perm
);
402 if (blk_insert_bs(blk
, bs
, errp
) < 0) {
410 * Creates a new BlockBackend, opens a new BlockDriverState, and connects both.
411 * The new BlockBackend is in the main AioContext.
413 * Just as with bdrv_open(), after having called this function the reference to
414 * @options belongs to the block layer (even on failure).
416 * TODO: Remove @filename and @flags; it should be possible to specify a whole
417 * BDS tree just by specifying the @options QDict (or @reference,
418 * alternatively). At the time of adding this function, this is not possible,
419 * though, so callers of this function have to be able to specify @filename and
422 BlockBackend
*blk_new_open(const char *filename
, const char *reference
,
423 QDict
*options
, int flags
, Error
**errp
)
426 BlockDriverState
*bs
;
428 uint64_t shared
= BLK_PERM_ALL
;
433 * blk_new_open() is mainly used in .bdrv_create implementations and the
434 * tools where sharing isn't a major concern because the BDS stays private
435 * and the file is generally not supposed to be used by a second process,
436 * so we just request permission according to the flags.
438 * The exceptions are xen_disk and blockdev_init(); in these cases, the
439 * caller of blk_new_open() doesn't make use of the permissions, but they
440 * shouldn't hurt either. We can still share everything here because the
441 * guest devices will add their own blockers if they can't share.
443 if ((flags
& BDRV_O_NO_IO
) == 0) {
444 perm
|= BLK_PERM_CONSISTENT_READ
;
445 if (flags
& BDRV_O_RDWR
) {
446 perm
|= BLK_PERM_WRITE
;
449 if (flags
& BDRV_O_RESIZE
) {
450 perm
|= BLK_PERM_RESIZE
;
452 if (flags
& BDRV_O_NO_SHARE
) {
453 shared
= BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE_UNCHANGED
;
456 blk
= blk_new(qemu_get_aio_context(), perm
, shared
);
457 bs
= bdrv_open(filename
, reference
, options
, flags
, errp
);
463 blk
->root
= bdrv_root_attach_child(bs
, "root", &child_root
,
464 BDRV_CHILD_FILTERED
| BDRV_CHILD_PRIMARY
,
465 perm
, shared
, blk
, errp
);
474 static void blk_delete(BlockBackend
*blk
)
476 assert(!blk
->refcnt
);
479 if (blk
->public.throttle_group_member
.throttle_state
) {
480 blk_io_limits_disable(blk
);
486 qemu_del_vm_change_state_handler(blk
->vmsh
);
489 assert(QLIST_EMPTY(&blk
->remove_bs_notifiers
.notifiers
));
490 assert(QLIST_EMPTY(&blk
->insert_bs_notifiers
.notifiers
));
491 assert(QLIST_EMPTY(&blk
->aio_notifiers
));
492 QTAILQ_REMOVE(&block_backends
, blk
, link
);
493 drive_info_del(blk
->legacy_dinfo
);
494 block_acct_cleanup(&blk
->stats
);
498 static void drive_info_del(DriveInfo
*dinfo
)
503 qemu_opts_del(dinfo
->opts
);
507 int blk_get_refcnt(BlockBackend
*blk
)
510 return blk
? blk
->refcnt
: 0;
514 * Increment @blk's reference count.
515 * @blk must not be null.
517 void blk_ref(BlockBackend
*blk
)
519 assert(blk
->refcnt
> 0);
525 * Decrement @blk's reference count.
526 * If this drops it to zero, destroy @blk.
527 * For convenience, do nothing if @blk is null.
529 void blk_unref(BlockBackend
*blk
)
533 assert(blk
->refcnt
> 0);
534 if (blk
->refcnt
> 1) {
538 /* blk_drain() cannot resurrect blk, nobody held a reference */
539 assert(blk
->refcnt
== 1);
547 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
548 * ones which are hidden (i.e. are not referenced by the monitor).
550 BlockBackend
*blk_all_next(BlockBackend
*blk
)
553 return blk
? QTAILQ_NEXT(blk
, link
)
554 : QTAILQ_FIRST(&block_backends
);
557 void blk_remove_all_bs(void)
559 BlockBackend
*blk
= NULL
;
563 while ((blk
= blk_all_next(blk
)) != NULL
) {
564 AioContext
*ctx
= blk_get_aio_context(blk
);
566 aio_context_acquire(ctx
);
570 aio_context_release(ctx
);
575 * Return the monitor-owned BlockBackend after @blk.
576 * If @blk is null, return the first one.
577 * Else, return @blk's next sibling, which may be null.
579 * To iterate over all BlockBackends, do
580 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
584 BlockBackend
*blk_next(BlockBackend
*blk
)
587 return blk
? QTAILQ_NEXT(blk
, monitor_link
)
588 : QTAILQ_FIRST(&monitor_block_backends
);
591 /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by
592 * the monitor or attached to a BlockBackend */
593 BlockDriverState
*bdrv_next(BdrvNextIterator
*it
)
595 BlockDriverState
*bs
, *old_bs
;
597 /* Must be called from the main loop */
598 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
600 /* First, return all root nodes of BlockBackends. In order to avoid
601 * returning a BDS twice when multiple BBs refer to it, we only return it
602 * if the BB is the first one in the parent list of the BDS. */
603 if (it
->phase
== BDRV_NEXT_BACKEND_ROOTS
) {
604 BlockBackend
*old_blk
= it
->blk
;
606 old_bs
= old_blk
? blk_bs(old_blk
) : NULL
;
609 it
->blk
= blk_all_next(it
->blk
);
610 bs
= it
->blk
? blk_bs(it
->blk
) : NULL
;
611 } while (it
->blk
&& (bs
== NULL
|| bdrv_first_blk(bs
) != it
->blk
));
623 it
->phase
= BDRV_NEXT_MONITOR_OWNED
;
628 /* Then return the monitor-owned BDSes without a BB attached. Ignore all
629 * BDSes that are attached to a BlockBackend here; they have been handled
630 * by the above block already */
632 it
->bs
= bdrv_next_monitor_owned(it
->bs
);
634 } while (bs
&& bdrv_has_blk(bs
));
644 static void bdrv_next_reset(BdrvNextIterator
*it
)
646 *it
= (BdrvNextIterator
) {
647 .phase
= BDRV_NEXT_BACKEND_ROOTS
,
651 BlockDriverState
*bdrv_first(BdrvNextIterator
*it
)
655 return bdrv_next(it
);
658 /* Must be called when aborting a bdrv_next() iteration before
659 * bdrv_next() returns NULL */
660 void bdrv_next_cleanup(BdrvNextIterator
*it
)
662 /* Must be called from the main loop */
663 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
665 if (it
->phase
== BDRV_NEXT_BACKEND_ROOTS
) {
667 bdrv_unref(blk_bs(it
->blk
));
678 * Add a BlockBackend into the list of backends referenced by the monitor, with
679 * the given @name acting as the handle for the monitor.
680 * Strictly for use by blockdev.c.
682 * @name must not be null or empty.
684 * Returns true on success and false on failure. In the latter case, an Error
685 * object is returned through @errp.
687 bool monitor_add_blk(BlockBackend
*blk
, const char *name
, Error
**errp
)
690 assert(name
&& name
[0]);
693 if (!id_wellformed(name
)) {
694 error_setg(errp
, "Invalid device name");
697 if (blk_by_name(name
)) {
698 error_setg(errp
, "Device with id '%s' already exists", name
);
701 if (bdrv_find_node(name
)) {
703 "Device name '%s' conflicts with an existing node name",
708 blk
->name
= g_strdup(name
);
709 QTAILQ_INSERT_TAIL(&monitor_block_backends
, blk
, monitor_link
);
714 * Remove a BlockBackend from the list of backends referenced by the monitor.
715 * Strictly for use by blockdev.c.
717 void monitor_remove_blk(BlockBackend
*blk
)
725 QTAILQ_REMOVE(&monitor_block_backends
, blk
, monitor_link
);
731 * Return @blk's name, a non-null string.
732 * Returns an empty string iff @blk is not referenced by the monitor.
734 const char *blk_name(const BlockBackend
*blk
)
737 return blk
->name
?: "";
741 * Return the BlockBackend with name @name if it exists, else null.
742 * @name must not be null.
744 BlockBackend
*blk_by_name(const char *name
)
746 BlockBackend
*blk
= NULL
;
750 while ((blk
= blk_next(blk
)) != NULL
) {
751 if (!strcmp(name
, blk
->name
)) {
759 * Return the BlockDriverState attached to @blk if any, else null.
761 BlockDriverState
*blk_bs(BlockBackend
*blk
)
764 return blk
->root
? blk
->root
->bs
: NULL
;
767 static BlockBackend
*bdrv_first_blk(BlockDriverState
*bs
)
770 QLIST_FOREACH(child
, &bs
->parents
, next_parent
) {
771 if (child
->klass
== &child_root
) {
772 return child
->opaque
;
780 * Returns true if @bs has an associated BlockBackend.
782 bool bdrv_has_blk(BlockDriverState
*bs
)
785 return bdrv_first_blk(bs
) != NULL
;
789 * Returns true if @bs has only BlockBackends as parents.
791 bool bdrv_is_root_node(BlockDriverState
*bs
)
796 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
797 if (c
->klass
!= &child_root
) {
806 * Return @blk's DriveInfo if any, else null.
808 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
810 return blk
->legacy_dinfo
;
814 * Set @blk's DriveInfo to @dinfo, and return it.
815 * @blk must not have a DriveInfo set already.
816 * No other BlockBackend may have the same DriveInfo set.
818 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
820 assert(!blk
->legacy_dinfo
);
821 return blk
->legacy_dinfo
= dinfo
;
825 * Return the BlockBackend with DriveInfo @dinfo.
828 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
830 BlockBackend
*blk
= NULL
;
832 while ((blk
= blk_next(blk
)) != NULL
) {
833 if (blk
->legacy_dinfo
== dinfo
) {
841 * Returns a pointer to the publicly accessible fields of @blk.
843 BlockBackendPublic
*blk_get_public(BlockBackend
*blk
)
850 * Returns a BlockBackend given the associated @public fields.
852 BlockBackend
*blk_by_public(BlockBackendPublic
*public)
855 return container_of(public, BlockBackend
, public);
859 * Disassociates the currently associated BlockDriverState from @blk.
861 void blk_remove_bs(BlockBackend
*blk
)
863 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
868 notifier_list_notify(&blk
->remove_bs_notifiers
, blk
);
869 if (tgm
->throttle_state
) {
870 BlockDriverState
*bs
= blk_bs(blk
);
873 * Take a ref in case blk_bs() changes across bdrv_drained_begin(), for
874 * example, if a temporary filter node is removed by a blockjob.
877 bdrv_drained_begin(bs
);
878 throttle_group_detach_aio_context(tgm
);
879 throttle_group_attach_aio_context(tgm
, qemu_get_aio_context());
880 bdrv_drained_end(bs
);
884 blk_update_root_state(blk
);
886 /* bdrv_root_unref_child() will cause blk->root to become stale and may
887 * switch to a completion coroutine later on. Let's drain all I/O here
888 * to avoid that and a potential QEMU crash.
893 bdrv_root_unref_child(root
);
897 * Associates a new BlockDriverState with @blk.
899 int blk_insert_bs(BlockBackend
*blk
, BlockDriverState
*bs
, Error
**errp
)
901 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
904 blk
->root
= bdrv_root_attach_child(bs
, "root", &child_root
,
905 BDRV_CHILD_FILTERED
| BDRV_CHILD_PRIMARY
,
906 blk
->perm
, blk
->shared_perm
,
908 if (blk
->root
== NULL
) {
912 notifier_list_notify(&blk
->insert_bs_notifiers
, blk
);
913 if (tgm
->throttle_state
) {
914 throttle_group_detach_aio_context(tgm
);
915 throttle_group_attach_aio_context(tgm
, bdrv_get_aio_context(bs
));
922 * Change BlockDriverState associated with @blk.
924 int blk_replace_bs(BlockBackend
*blk
, BlockDriverState
*new_bs
, Error
**errp
)
927 return bdrv_replace_child_bs(blk
->root
, new_bs
, errp
);
931 * Sets the permission bitmasks that the user of the BlockBackend needs.
933 int blk_set_perm(BlockBackend
*blk
, uint64_t perm
, uint64_t shared_perm
,
939 if (blk
->root
&& !blk
->disable_perm
) {
940 ret
= bdrv_child_try_set_perm(blk
->root
, perm
, shared_perm
, errp
);
947 blk
->shared_perm
= shared_perm
;
952 void blk_get_perm(BlockBackend
*blk
, uint64_t *perm
, uint64_t *shared_perm
)
956 *shared_perm
= blk
->shared_perm
;
960 * Attach device model @dev to @blk.
961 * Return 0 on success, -EBUSY when a device model is attached already.
963 int blk_attach_dev(BlockBackend
*blk
, DeviceState
*dev
)
970 /* While migration is still incoming, we don't need to apply the
971 * permissions of guest device BlockBackends. We might still have a block
972 * job or NBD server writing to the image for storage migration. */
973 if (runstate_check(RUN_STATE_INMIGRATE
)) {
974 blk
->disable_perm
= true;
979 blk_iostatus_reset(blk
);
985 * Detach device model @dev from @blk.
986 * @dev must be currently attached to @blk.
988 void blk_detach_dev(BlockBackend
*blk
, DeviceState
*dev
)
990 assert(blk
->dev
== dev
);
994 blk
->dev_opaque
= NULL
;
995 blk
->guest_block_size
= 512;
996 blk_set_perm(blk
, 0, BLK_PERM_ALL
, &error_abort
);
1001 * Return the device model attached to @blk if any, else null.
1003 DeviceState
*blk_get_attached_dev(BlockBackend
*blk
)
1005 GLOBAL_STATE_CODE();
1009 /* Return the qdev ID, or if no ID is assigned the QOM path, of the block
1010 * device attached to the BlockBackend. */
1011 char *blk_get_attached_dev_id(BlockBackend
*blk
)
1013 DeviceState
*dev
= blk
->dev
;
1017 return g_strdup("");
1018 } else if (dev
->id
) {
1019 return g_strdup(dev
->id
);
1022 return object_get_canonical_path(OBJECT(dev
)) ?: g_strdup("");
1026 * Return the BlockBackend which has the device model @dev attached if it
1027 * exists, else null.
1029 * @dev must not be null.
1031 BlockBackend
*blk_by_dev(void *dev
)
1033 BlockBackend
*blk
= NULL
;
1035 GLOBAL_STATE_CODE();
1037 assert(dev
!= NULL
);
1038 while ((blk
= blk_all_next(blk
)) != NULL
) {
1039 if (blk
->dev
== dev
) {
1047 * Set @blk's device model callbacks to @ops.
1048 * @opaque is the opaque argument to pass to the callbacks.
1049 * This is for use by device models.
1051 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
1054 GLOBAL_STATE_CODE();
1056 blk
->dev_opaque
= opaque
;
1058 /* Are we currently quiesced? Should we enforce this right now? */
1059 if (blk
->quiesce_counter
&& ops
->drained_begin
) {
1060 ops
->drained_begin(opaque
);
1065 * Notify @blk's attached device model of media change.
1067 * If @load is true, notify of media load. This action can fail, meaning that
1068 * the medium cannot be loaded. @errp is set then.
1070 * If @load is false, notify of media eject. This can never fail.
1072 * Also send DEVICE_TRAY_MOVED events as appropriate.
1074 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
, Error
**errp
)
1076 GLOBAL_STATE_CODE();
1077 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
1078 bool tray_was_open
, tray_is_open
;
1079 Error
*local_err
= NULL
;
1081 tray_was_open
= blk_dev_is_tray_open(blk
);
1082 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
, &local_err
);
1084 assert(load
== true);
1085 error_propagate(errp
, local_err
);
1088 tray_is_open
= blk_dev_is_tray_open(blk
);
1090 if (tray_was_open
!= tray_is_open
) {
1091 char *id
= blk_get_attached_dev_id(blk
);
1092 qapi_event_send_device_tray_moved(blk_name(blk
), id
, tray_is_open
);
1098 static void blk_root_change_media(BdrvChild
*child
, bool load
)
1100 blk_dev_change_media_cb(child
->opaque
, load
, NULL
);
1104 * Does @blk's attached device model have removable media?
1105 * %true if no device model is attached.
1107 bool blk_dev_has_removable_media(BlockBackend
*blk
)
1109 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
1113 * Does @blk's attached device model have a tray?
1115 bool blk_dev_has_tray(BlockBackend
*blk
)
1117 return blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
;
1121 * Notify @blk's attached device model of a media eject request.
1122 * If @force is true, the medium is about to be yanked out forcefully.
1124 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
1126 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
1127 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
1132 * Does @blk's attached device model have a tray, and is it open?
1134 bool blk_dev_is_tray_open(BlockBackend
*blk
)
1136 if (blk_dev_has_tray(blk
)) {
1137 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
1143 * Does @blk's attached device model have the medium locked?
1144 * %false if the device model has no such lock.
1146 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
1148 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
1149 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
1155 * Notify @blk's attached device model of a backend size change.
1157 static void blk_root_resize(BdrvChild
*child
)
1159 BlockBackend
*blk
= child
->opaque
;
1161 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
1162 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
1166 void blk_iostatus_enable(BlockBackend
*blk
)
1168 GLOBAL_STATE_CODE();
1169 blk
->iostatus_enabled
= true;
1170 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
1173 /* The I/O status is only enabled if the drive explicitly
1174 * enables it _and_ the VM is configured to stop on errors */
1175 bool blk_iostatus_is_enabled(const BlockBackend
*blk
)
1178 return (blk
->iostatus_enabled
&&
1179 (blk
->on_write_error
== BLOCKDEV_ON_ERROR_ENOSPC
||
1180 blk
->on_write_error
== BLOCKDEV_ON_ERROR_STOP
||
1181 blk
->on_read_error
== BLOCKDEV_ON_ERROR_STOP
));
1184 BlockDeviceIoStatus
blk_iostatus(const BlockBackend
*blk
)
1186 GLOBAL_STATE_CODE();
1187 return blk
->iostatus
;
1190 void blk_iostatus_disable(BlockBackend
*blk
)
1192 GLOBAL_STATE_CODE();
1193 blk
->iostatus_enabled
= false;
1196 void blk_iostatus_reset(BlockBackend
*blk
)
1198 GLOBAL_STATE_CODE();
1199 if (blk_iostatus_is_enabled(blk
)) {
1200 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
1204 void blk_iostatus_set_err(BlockBackend
*blk
, int error
)
1207 assert(blk_iostatus_is_enabled(blk
));
1208 if (blk
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
1209 blk
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
1210 BLOCK_DEVICE_IO_STATUS_FAILED
;
1214 void blk_set_allow_write_beyond_eof(BlockBackend
*blk
, bool allow
)
1217 blk
->allow_write_beyond_eof
= allow
;
1220 void blk_set_allow_aio_context_change(BlockBackend
*blk
, bool allow
)
1223 blk
->allow_aio_context_change
= allow
;
1226 void blk_set_disable_request_queuing(BlockBackend
*blk
, bool disable
)
1229 blk
->disable_request_queuing
= disable
;
1232 static int blk_check_byte_request(BlockBackend
*blk
, int64_t offset
,
1241 if (!blk_is_available(blk
)) {
1249 if (!blk
->allow_write_beyond_eof
) {
1250 len
= blk_getlength(blk
);
1255 if (offset
> len
|| len
- offset
< bytes
) {
1263 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1264 static void coroutine_fn
blk_wait_while_drained(BlockBackend
*blk
)
1266 assert(blk
->in_flight
> 0);
1268 if (blk
->quiesce_counter
&& !blk
->disable_request_queuing
) {
1269 blk_dec_in_flight(blk
);
1270 qemu_co_queue_wait(&blk
->queued_requests
, NULL
);
1271 blk_inc_in_flight(blk
);
1275 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1277 blk_co_do_preadv(BlockBackend
*blk
, int64_t offset
, int64_t bytes
,
1278 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
1281 BlockDriverState
*bs
;
1283 blk_wait_while_drained(blk
);
1285 /* Call blk_bs() only after waiting, the graph may have changed */
1287 trace_blk_co_preadv(blk
, bs
, offset
, bytes
, flags
);
1289 ret
= blk_check_byte_request(blk
, offset
, bytes
);
1294 bdrv_inc_in_flight(bs
);
1296 /* throttling disk I/O */
1297 if (blk
->public.throttle_group_member
.throttle_state
) {
1298 throttle_group_co_io_limits_intercept(&blk
->public.throttle_group_member
,
1302 ret
= bdrv_co_preadv(blk
->root
, offset
, bytes
, qiov
, flags
);
1303 bdrv_dec_in_flight(bs
);
1307 int coroutine_fn
blk_co_preadv(BlockBackend
*blk
, int64_t offset
,
1308 int64_t bytes
, QEMUIOVector
*qiov
,
1309 BdrvRequestFlags flags
)
1314 blk_inc_in_flight(blk
);
1315 ret
= blk_co_do_preadv(blk
, offset
, bytes
, qiov
, flags
);
1316 blk_dec_in_flight(blk
);
1321 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1323 blk_co_do_pwritev_part(BlockBackend
*blk
, int64_t offset
, int64_t bytes
,
1324 QEMUIOVector
*qiov
, size_t qiov_offset
,
1325 BdrvRequestFlags flags
)
1328 BlockDriverState
*bs
;
1330 blk_wait_while_drained(blk
);
1332 /* Call blk_bs() only after waiting, the graph may have changed */
1334 trace_blk_co_pwritev(blk
, bs
, offset
, bytes
, flags
);
1336 ret
= blk_check_byte_request(blk
, offset
, bytes
);
1341 bdrv_inc_in_flight(bs
);
1342 /* throttling disk I/O */
1343 if (blk
->public.throttle_group_member
.throttle_state
) {
1344 throttle_group_co_io_limits_intercept(&blk
->public.throttle_group_member
,
1348 if (!blk
->enable_write_cache
) {
1349 flags
|= BDRV_REQ_FUA
;
1352 ret
= bdrv_co_pwritev_part(blk
->root
, offset
, bytes
, qiov
, qiov_offset
,
1354 bdrv_dec_in_flight(bs
);
1358 int coroutine_fn
blk_co_pwritev_part(BlockBackend
*blk
, int64_t offset
,
1360 QEMUIOVector
*qiov
, size_t qiov_offset
,
1361 BdrvRequestFlags flags
)
1366 blk_inc_in_flight(blk
);
1367 ret
= blk_co_do_pwritev_part(blk
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1368 blk_dec_in_flight(blk
);
1373 int coroutine_fn
blk_co_pwritev(BlockBackend
*blk
, int64_t offset
,
1374 int64_t bytes
, QEMUIOVector
*qiov
,
1375 BdrvRequestFlags flags
)
1378 return blk_co_pwritev_part(blk
, offset
, bytes
, qiov
, 0, flags
);
1381 static int coroutine_fn
blk_pwritev_part(BlockBackend
*blk
, int64_t offset
,
1383 QEMUIOVector
*qiov
, size_t qiov_offset
,
1384 BdrvRequestFlags flags
)
1388 blk_inc_in_flight(blk
);
1389 ret
= blk_do_pwritev_part(blk
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1390 blk_dec_in_flight(blk
);
1395 typedef struct BlkRwCo
{
1400 BdrvRequestFlags flags
;
1403 int blk_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
1404 int64_t bytes
, BdrvRequestFlags flags
)
1407 return blk_pwritev_part(blk
, offset
, bytes
, NULL
, 0,
1408 flags
| BDRV_REQ_ZERO_WRITE
);
1411 int blk_make_zero(BlockBackend
*blk
, BdrvRequestFlags flags
)
1413 GLOBAL_STATE_CODE();
1414 return bdrv_make_zero(blk
->root
, flags
);
1417 void blk_inc_in_flight(BlockBackend
*blk
)
1420 qatomic_inc(&blk
->in_flight
);
1423 void blk_dec_in_flight(BlockBackend
*blk
)
1426 qatomic_dec(&blk
->in_flight
);
1430 static void error_callback_bh(void *opaque
)
1432 struct BlockBackendAIOCB
*acb
= opaque
;
1434 blk_dec_in_flight(acb
->blk
);
1435 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1436 qemu_aio_unref(acb
);
1439 BlockAIOCB
*blk_abort_aio_request(BlockBackend
*blk
,
1440 BlockCompletionFunc
*cb
,
1441 void *opaque
, int ret
)
1443 struct BlockBackendAIOCB
*acb
;
1446 blk_inc_in_flight(blk
);
1447 acb
= blk_aio_get(&block_backend_aiocb_info
, blk
, cb
, opaque
);
1451 replay_bh_schedule_oneshot_event(blk_get_aio_context(blk
),
1452 error_callback_bh
, acb
);
1453 return &acb
->common
;
1456 typedef struct BlkAioEmAIOCB
{
1463 static AioContext
*blk_aio_em_aiocb_get_aio_context(BlockAIOCB
*acb_
)
1465 BlkAioEmAIOCB
*acb
= container_of(acb_
, BlkAioEmAIOCB
, common
);
1467 return blk_get_aio_context(acb
->rwco
.blk
);
1470 static const AIOCBInfo blk_aio_em_aiocb_info
= {
1471 .aiocb_size
= sizeof(BlkAioEmAIOCB
),
1472 .get_aio_context
= blk_aio_em_aiocb_get_aio_context
,
1475 static void blk_aio_complete(BlkAioEmAIOCB
*acb
)
1477 if (acb
->has_returned
) {
1478 acb
->common
.cb(acb
->common
.opaque
, acb
->rwco
.ret
);
1479 blk_dec_in_flight(acb
->rwco
.blk
);
1480 qemu_aio_unref(acb
);
1484 static void blk_aio_complete_bh(void *opaque
)
1486 BlkAioEmAIOCB
*acb
= opaque
;
1487 assert(acb
->has_returned
);
1488 blk_aio_complete(acb
);
1491 static BlockAIOCB
*blk_aio_prwv(BlockBackend
*blk
, int64_t offset
,
1493 void *iobuf
, CoroutineEntry co_entry
,
1494 BdrvRequestFlags flags
,
1495 BlockCompletionFunc
*cb
, void *opaque
)
1500 blk_inc_in_flight(blk
);
1501 acb
= blk_aio_get(&blk_aio_em_aiocb_info
, blk
, cb
, opaque
);
1502 acb
->rwco
= (BlkRwCo
) {
1510 acb
->has_returned
= false;
1512 co
= qemu_coroutine_create(co_entry
, acb
);
1513 bdrv_coroutine_enter(blk_bs(blk
), co
);
1515 acb
->has_returned
= true;
1516 if (acb
->rwco
.ret
!= NOT_DONE
) {
1517 replay_bh_schedule_oneshot_event(blk_get_aio_context(blk
),
1518 blk_aio_complete_bh
, acb
);
1521 return &acb
->common
;
1524 static void blk_aio_read_entry(void *opaque
)
1526 BlkAioEmAIOCB
*acb
= opaque
;
1527 BlkRwCo
*rwco
= &acb
->rwco
;
1528 QEMUIOVector
*qiov
= rwco
->iobuf
;
1530 assert(qiov
->size
== acb
->bytes
);
1531 rwco
->ret
= blk_co_do_preadv(rwco
->blk
, rwco
->offset
, acb
->bytes
,
1533 blk_aio_complete(acb
);
1536 static void blk_aio_write_entry(void *opaque
)
1538 BlkAioEmAIOCB
*acb
= opaque
;
1539 BlkRwCo
*rwco
= &acb
->rwco
;
1540 QEMUIOVector
*qiov
= rwco
->iobuf
;
1542 assert(!qiov
|| qiov
->size
== acb
->bytes
);
1543 rwco
->ret
= blk_co_do_pwritev_part(rwco
->blk
, rwco
->offset
, acb
->bytes
,
1544 qiov
, 0, rwco
->flags
);
1545 blk_aio_complete(acb
);
1548 BlockAIOCB
*blk_aio_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
1549 int64_t bytes
, BdrvRequestFlags flags
,
1550 BlockCompletionFunc
*cb
, void *opaque
)
1553 return blk_aio_prwv(blk
, offset
, bytes
, NULL
, blk_aio_write_entry
,
1554 flags
| BDRV_REQ_ZERO_WRITE
, cb
, opaque
);
1557 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int bytes
)
1560 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, bytes
);
1563 blk_inc_in_flight(blk
);
1564 ret
= blk_do_preadv(blk
, offset
, bytes
, &qiov
, 0);
1565 blk_dec_in_flight(blk
);
1567 return ret
< 0 ? ret
: bytes
;
1570 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int bytes
,
1571 BdrvRequestFlags flags
)
1574 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, bytes
);
1577 ret
= blk_pwritev_part(blk
, offset
, bytes
, &qiov
, 0, flags
);
1579 return ret
< 0 ? ret
: bytes
;
1582 int64_t blk_getlength(BlockBackend
*blk
)
1585 if (!blk_is_available(blk
)) {
1589 return bdrv_getlength(blk_bs(blk
));
1592 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
1596 *nb_sectors_ptr
= 0;
1598 bdrv_get_geometry(blk_bs(blk
), nb_sectors_ptr
);
1602 int64_t blk_nb_sectors(BlockBackend
*blk
)
1605 if (!blk_is_available(blk
)) {
1609 return bdrv_nb_sectors(blk_bs(blk
));
1612 BlockAIOCB
*blk_aio_preadv(BlockBackend
*blk
, int64_t offset
,
1613 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
1614 BlockCompletionFunc
*cb
, void *opaque
)
1617 assert((uint64_t)qiov
->size
<= INT64_MAX
);
1618 return blk_aio_prwv(blk
, offset
, qiov
->size
, qiov
,
1619 blk_aio_read_entry
, flags
, cb
, opaque
);
1622 BlockAIOCB
*blk_aio_pwritev(BlockBackend
*blk
, int64_t offset
,
1623 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
1624 BlockCompletionFunc
*cb
, void *opaque
)
1627 assert((uint64_t)qiov
->size
<= INT64_MAX
);
1628 return blk_aio_prwv(blk
, offset
, qiov
->size
, qiov
,
1629 blk_aio_write_entry
, flags
, cb
, opaque
);
1632 void blk_aio_cancel(BlockAIOCB
*acb
)
1634 GLOBAL_STATE_CODE();
1635 bdrv_aio_cancel(acb
);
1638 void blk_aio_cancel_async(BlockAIOCB
*acb
)
1641 bdrv_aio_cancel_async(acb
);
1644 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1646 blk_co_do_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
1648 blk_wait_while_drained(blk
);
1650 if (!blk_is_available(blk
)) {
1654 return bdrv_co_ioctl(blk_bs(blk
), req
, buf
);
1657 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
1662 blk_inc_in_flight(blk
);
1663 ret
= blk_do_ioctl(blk
, req
, buf
);
1664 blk_dec_in_flight(blk
);
1669 static void blk_aio_ioctl_entry(void *opaque
)
1671 BlkAioEmAIOCB
*acb
= opaque
;
1672 BlkRwCo
*rwco
= &acb
->rwco
;
1674 rwco
->ret
= blk_co_do_ioctl(rwco
->blk
, rwco
->offset
, rwco
->iobuf
);
1676 blk_aio_complete(acb
);
1679 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
1680 BlockCompletionFunc
*cb
, void *opaque
)
1683 return blk_aio_prwv(blk
, req
, 0, buf
, blk_aio_ioctl_entry
, 0, cb
, opaque
);
1686 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1688 blk_co_do_pdiscard(BlockBackend
*blk
, int64_t offset
, int64_t bytes
)
1692 blk_wait_while_drained(blk
);
1694 ret
= blk_check_byte_request(blk
, offset
, bytes
);
1699 return bdrv_co_pdiscard(blk
->root
, offset
, bytes
);
1702 static void blk_aio_pdiscard_entry(void *opaque
)
1704 BlkAioEmAIOCB
*acb
= opaque
;
1705 BlkRwCo
*rwco
= &acb
->rwco
;
1707 rwco
->ret
= blk_co_do_pdiscard(rwco
->blk
, rwco
->offset
, acb
->bytes
);
1708 blk_aio_complete(acb
);
1711 BlockAIOCB
*blk_aio_pdiscard(BlockBackend
*blk
,
1712 int64_t offset
, int64_t bytes
,
1713 BlockCompletionFunc
*cb
, void *opaque
)
1716 return blk_aio_prwv(blk
, offset
, bytes
, NULL
, blk_aio_pdiscard_entry
, 0,
1720 int coroutine_fn
blk_co_pdiscard(BlockBackend
*blk
, int64_t offset
,
1726 blk_inc_in_flight(blk
);
1727 ret
= blk_co_do_pdiscard(blk
, offset
, bytes
);
1728 blk_dec_in_flight(blk
);
1733 int blk_pdiscard(BlockBackend
*blk
, int64_t offset
, int64_t bytes
)
1738 blk_inc_in_flight(blk
);
1739 ret
= blk_do_pdiscard(blk
, offset
, bytes
);
1740 blk_dec_in_flight(blk
);
1745 /* To be called between exactly one pair of blk_inc/dec_in_flight() */
1746 int coroutine_fn
blk_co_do_flush(BlockBackend
*blk
)
1748 blk_wait_while_drained(blk
);
1750 if (!blk_is_available(blk
)) {
1754 return bdrv_co_flush(blk_bs(blk
));
1757 static void blk_aio_flush_entry(void *opaque
)
1759 BlkAioEmAIOCB
*acb
= opaque
;
1760 BlkRwCo
*rwco
= &acb
->rwco
;
1762 rwco
->ret
= blk_co_do_flush(rwco
->blk
);
1763 blk_aio_complete(acb
);
1766 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
1767 BlockCompletionFunc
*cb
, void *opaque
)
1770 return blk_aio_prwv(blk
, 0, 0, NULL
, blk_aio_flush_entry
, 0, cb
, opaque
);
1773 int coroutine_fn
blk_co_flush(BlockBackend
*blk
)
1778 blk_inc_in_flight(blk
);
1779 ret
= blk_co_do_flush(blk
);
1780 blk_dec_in_flight(blk
);
1785 int blk_flush(BlockBackend
*blk
)
1789 blk_inc_in_flight(blk
);
1790 ret
= blk_do_flush(blk
);
1791 blk_dec_in_flight(blk
);
1796 void blk_drain(BlockBackend
*blk
)
1798 BlockDriverState
*bs
= blk_bs(blk
);
1799 GLOBAL_STATE_CODE();
1803 bdrv_drained_begin(bs
);
1806 /* We may have -ENOMEDIUM completions in flight */
1807 AIO_WAIT_WHILE(blk_get_aio_context(blk
),
1808 qatomic_mb_read(&blk
->in_flight
) > 0);
1811 bdrv_drained_end(bs
);
1816 void blk_drain_all(void)
1818 BlockBackend
*blk
= NULL
;
1820 GLOBAL_STATE_CODE();
1822 bdrv_drain_all_begin();
1824 while ((blk
= blk_all_next(blk
)) != NULL
) {
1825 AioContext
*ctx
= blk_get_aio_context(blk
);
1827 aio_context_acquire(ctx
);
1829 /* We may have -ENOMEDIUM completions in flight */
1830 AIO_WAIT_WHILE(ctx
, qatomic_mb_read(&blk
->in_flight
) > 0);
1832 aio_context_release(ctx
);
1835 bdrv_drain_all_end();
1838 void blk_set_on_error(BlockBackend
*blk
, BlockdevOnError on_read_error
,
1839 BlockdevOnError on_write_error
)
1841 GLOBAL_STATE_CODE();
1842 blk
->on_read_error
= on_read_error
;
1843 blk
->on_write_error
= on_write_error
;
1846 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
1849 return is_read
? blk
->on_read_error
: blk
->on_write_error
;
1852 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
1855 BlockdevOnError on_err
= blk_get_on_error(blk
, is_read
);
1859 case BLOCKDEV_ON_ERROR_ENOSPC
:
1860 return (error
== ENOSPC
) ?
1861 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
1862 case BLOCKDEV_ON_ERROR_STOP
:
1863 return BLOCK_ERROR_ACTION_STOP
;
1864 case BLOCKDEV_ON_ERROR_REPORT
:
1865 return BLOCK_ERROR_ACTION_REPORT
;
1866 case BLOCKDEV_ON_ERROR_IGNORE
:
1867 return BLOCK_ERROR_ACTION_IGNORE
;
1868 case BLOCKDEV_ON_ERROR_AUTO
:
1874 static void send_qmp_error_event(BlockBackend
*blk
,
1875 BlockErrorAction action
,
1876 bool is_read
, int error
)
1878 IoOperationType optype
;
1879 BlockDriverState
*bs
= blk_bs(blk
);
1881 optype
= is_read
? IO_OPERATION_TYPE_READ
: IO_OPERATION_TYPE_WRITE
;
1882 qapi_event_send_block_io_error(blk_name(blk
), !!bs
,
1883 bs
? bdrv_get_node_name(bs
) : NULL
, optype
,
1884 action
, blk_iostatus_is_enabled(blk
),
1885 error
== ENOSPC
, strerror(error
));
1888 /* This is done by device models because, while the block layer knows
1889 * about the error, it does not know whether an operation comes from
1890 * the device or the block layer (from a job, for example).
1892 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
1893 bool is_read
, int error
)
1898 if (action
== BLOCK_ERROR_ACTION_STOP
) {
1899 /* First set the iostatus, so that "info block" returns an iostatus
1900 * that matches the events raised so far (an additional error iostatus
1901 * is fine, but not a lost one).
1903 blk_iostatus_set_err(blk
, error
);
1905 /* Then raise the request to stop the VM and the event.
1906 * qemu_system_vmstop_request_prepare has two effects. First,
1907 * it ensures that the STOP event always comes after the
1908 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1909 * can observe the STOP event and do a "cont" before the STOP
1910 * event is issued, the VM will not stop. In this case, vm_start()
1911 * also ensures that the STOP/RESUME pair of events is emitted.
1913 qemu_system_vmstop_request_prepare();
1914 send_qmp_error_event(blk
, action
, is_read
, error
);
1915 qemu_system_vmstop_request(RUN_STATE_IO_ERROR
);
1917 send_qmp_error_event(blk
, action
, is_read
, error
);
1922 * Returns true if the BlockBackend can support taking write permissions
1923 * (because its root node is not read-only).
1925 bool blk_supports_write_perm(BlockBackend
*blk
)
1927 BlockDriverState
*bs
= blk_bs(blk
);
1928 GLOBAL_STATE_CODE();
1931 return !bdrv_is_read_only(bs
);
1933 return blk
->root_state
.open_flags
& BDRV_O_RDWR
;
1938 * Returns true if the BlockBackend can be written to in its current
1939 * configuration (i.e. if write permission have been requested)
1941 bool blk_is_writable(BlockBackend
*blk
)
1944 return blk
->perm
& BLK_PERM_WRITE
;
1947 bool blk_is_sg(BlockBackend
*blk
)
1949 BlockDriverState
*bs
= blk_bs(blk
);
1950 GLOBAL_STATE_CODE();
1956 return bdrv_is_sg(bs
);
1959 bool blk_enable_write_cache(BlockBackend
*blk
)
1962 return blk
->enable_write_cache
;
1965 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
1967 GLOBAL_STATE_CODE();
1968 blk
->enable_write_cache
= wce
;
1971 void blk_activate(BlockBackend
*blk
, Error
**errp
)
1973 BlockDriverState
*bs
= blk_bs(blk
);
1974 GLOBAL_STATE_CODE();
1977 error_setg(errp
, "Device '%s' has no medium", blk
->name
);
1981 bdrv_activate(bs
, errp
);
1984 bool blk_is_inserted(BlockBackend
*blk
)
1986 BlockDriverState
*bs
= blk_bs(blk
);
1989 return bs
&& bdrv_is_inserted(bs
);
1992 bool blk_is_available(BlockBackend
*blk
)
1995 return blk_is_inserted(blk
) && !blk_dev_is_tray_open(blk
);
1998 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
2000 BlockDriverState
*bs
= blk_bs(blk
);
2004 bdrv_lock_medium(bs
, locked
);
2008 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
2010 BlockDriverState
*bs
= blk_bs(blk
);
2015 bdrv_eject(bs
, eject_flag
);
2018 /* Whether or not we ejected on the backend,
2019 * the frontend experienced a tray event. */
2020 id
= blk_get_attached_dev_id(blk
);
2021 qapi_event_send_device_tray_moved(blk_name(blk
), id
,
2026 int blk_get_flags(BlockBackend
*blk
)
2028 BlockDriverState
*bs
= blk_bs(blk
);
2029 GLOBAL_STATE_CODE();
2032 return bdrv_get_flags(bs
);
2034 return blk
->root_state
.open_flags
;
2038 /* Returns the minimum request alignment, in bytes; guaranteed nonzero */
2039 uint32_t blk_get_request_alignment(BlockBackend
*blk
)
2041 BlockDriverState
*bs
= blk_bs(blk
);
2043 return bs
? bs
->bl
.request_alignment
: BDRV_SECTOR_SIZE
;
2046 /* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */
2047 uint64_t blk_get_max_hw_transfer(BlockBackend
*blk
)
2049 BlockDriverState
*bs
= blk_bs(blk
);
2050 uint64_t max
= INT_MAX
;
2054 max
= MIN_NON_ZERO(max
, bs
->bl
.max_hw_transfer
);
2055 max
= MIN_NON_ZERO(max
, bs
->bl
.max_transfer
);
2057 return ROUND_DOWN(max
, blk_get_request_alignment(blk
));
2060 /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
2061 uint32_t blk_get_max_transfer(BlockBackend
*blk
)
2063 BlockDriverState
*bs
= blk_bs(blk
);
2064 uint32_t max
= INT_MAX
;
2068 max
= MIN_NON_ZERO(max
, bs
->bl
.max_transfer
);
2070 return ROUND_DOWN(max
, blk_get_request_alignment(blk
));
2073 int blk_get_max_hw_iov(BlockBackend
*blk
)
2076 return MIN_NON_ZERO(blk
->root
->bs
->bl
.max_hw_iov
,
2077 blk
->root
->bs
->bl
.max_iov
);
2080 int blk_get_max_iov(BlockBackend
*blk
)
2083 return blk
->root
->bs
->bl
.max_iov
;
2086 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
2089 blk
->guest_block_size
= align
;
2092 void *blk_try_blockalign(BlockBackend
*blk
, size_t size
)
2095 return qemu_try_blockalign(blk
? blk_bs(blk
) : NULL
, size
);
2098 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
2101 return qemu_blockalign(blk
? blk_bs(blk
) : NULL
, size
);
2104 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
2106 BlockDriverState
*bs
= blk_bs(blk
);
2107 GLOBAL_STATE_CODE();
2113 return bdrv_op_is_blocked(bs
, op
, errp
);
2116 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
2118 BlockDriverState
*bs
= blk_bs(blk
);
2119 GLOBAL_STATE_CODE();
2122 bdrv_op_unblock(bs
, op
, reason
);
2126 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
2128 BlockDriverState
*bs
= blk_bs(blk
);
2129 GLOBAL_STATE_CODE();
2132 bdrv_op_block_all(bs
, reason
);
2136 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
2138 BlockDriverState
*bs
= blk_bs(blk
);
2139 GLOBAL_STATE_CODE();
2142 bdrv_op_unblock_all(bs
, reason
);
2146 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
2148 BlockDriverState
*bs
= blk_bs(blk
);
2152 AioContext
*ctx
= bdrv_get_aio_context(blk_bs(blk
));
2153 assert(ctx
== blk
->ctx
);
2159 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
)
2161 BlockBackendAIOCB
*blk_acb
= DO_UPCAST(BlockBackendAIOCB
, common
, acb
);
2162 return blk_get_aio_context(blk_acb
->blk
);
2165 static int blk_do_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
,
2166 bool update_root_node
, Error
**errp
)
2168 BlockDriverState
*bs
= blk_bs(blk
);
2169 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
2175 if (update_root_node
) {
2176 ret
= bdrv_child_try_set_aio_context(bs
, new_context
, blk
->root
,
2183 if (tgm
->throttle_state
) {
2184 bdrv_drained_begin(bs
);
2185 throttle_group_detach_aio_context(tgm
);
2186 throttle_group_attach_aio_context(tgm
, new_context
);
2187 bdrv_drained_end(bs
);
2193 blk
->ctx
= new_context
;
2197 int blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
,
2200 GLOBAL_STATE_CODE();
2201 return blk_do_set_aio_context(blk
, new_context
, true, errp
);
2204 static bool blk_root_can_set_aio_ctx(BdrvChild
*child
, AioContext
*ctx
,
2205 GSList
**ignore
, Error
**errp
)
2207 BlockBackend
*blk
= child
->opaque
;
2209 if (blk
->allow_aio_context_change
) {
2213 /* Only manually created BlockBackends that are not attached to anything
2214 * can change their AioContext without updating their user. */
2215 if (!blk
->name
|| blk
->dev
) {
2216 /* TODO Add BB name/QOM path */
2217 error_setg(errp
, "Cannot change iothread of active block backend");
2224 static void blk_root_set_aio_ctx(BdrvChild
*child
, AioContext
*ctx
,
2227 BlockBackend
*blk
= child
->opaque
;
2228 blk_do_set_aio_context(blk
, ctx
, false, &error_abort
);
2231 void blk_add_aio_context_notifier(BlockBackend
*blk
,
2232 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
2233 void (*detach_aio_context
)(void *opaque
), void *opaque
)
2235 BlockBackendAioNotifier
*notifier
;
2236 BlockDriverState
*bs
= blk_bs(blk
);
2237 GLOBAL_STATE_CODE();
2239 notifier
= g_new(BlockBackendAioNotifier
, 1);
2240 notifier
->attached_aio_context
= attached_aio_context
;
2241 notifier
->detach_aio_context
= detach_aio_context
;
2242 notifier
->opaque
= opaque
;
2243 QLIST_INSERT_HEAD(&blk
->aio_notifiers
, notifier
, list
);
2246 bdrv_add_aio_context_notifier(bs
, attached_aio_context
,
2247 detach_aio_context
, opaque
);
2251 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
2252 void (*attached_aio_context
)(AioContext
*,
2254 void (*detach_aio_context
)(void *),
2257 BlockBackendAioNotifier
*notifier
;
2258 BlockDriverState
*bs
= blk_bs(blk
);
2260 GLOBAL_STATE_CODE();
2263 bdrv_remove_aio_context_notifier(bs
, attached_aio_context
,
2264 detach_aio_context
, opaque
);
2267 QLIST_FOREACH(notifier
, &blk
->aio_notifiers
, list
) {
2268 if (notifier
->attached_aio_context
== attached_aio_context
&&
2269 notifier
->detach_aio_context
== detach_aio_context
&&
2270 notifier
->opaque
== opaque
) {
2271 QLIST_REMOVE(notifier
, list
);
2280 void blk_add_remove_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
2282 GLOBAL_STATE_CODE();
2283 notifier_list_add(&blk
->remove_bs_notifiers
, notify
);
2286 void blk_add_insert_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
2288 GLOBAL_STATE_CODE();
2289 notifier_list_add(&blk
->insert_bs_notifiers
, notify
);
2292 void blk_io_plug(BlockBackend
*blk
)
2294 BlockDriverState
*bs
= blk_bs(blk
);
2302 void blk_io_unplug(BlockBackend
*blk
)
2304 BlockDriverState
*bs
= blk_bs(blk
);
2312 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
2318 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
2319 BlockCompletionFunc
*cb
, void *opaque
)
2322 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
2325 int coroutine_fn
blk_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
2326 int64_t bytes
, BdrvRequestFlags flags
)
2329 return blk_co_pwritev(blk
, offset
, bytes
, NULL
,
2330 flags
| BDRV_REQ_ZERO_WRITE
);
2333 int blk_pwrite_compressed(BlockBackend
*blk
, int64_t offset
, const void *buf
,
2336 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, bytes
);
2338 return blk_pwritev_part(blk
, offset
, bytes
, &qiov
, 0,
2339 BDRV_REQ_WRITE_COMPRESSED
);
2342 int blk_truncate(BlockBackend
*blk
, int64_t offset
, bool exact
,
2343 PreallocMode prealloc
, BdrvRequestFlags flags
, Error
**errp
)
2346 if (!blk_is_available(blk
)) {
2347 error_setg(errp
, "No medium inserted");
2351 return bdrv_truncate(blk
->root
, offset
, exact
, prealloc
, flags
, errp
);
2354 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
2355 int64_t pos
, int size
)
2358 GLOBAL_STATE_CODE();
2360 if (!blk_is_available(blk
)) {
2364 ret
= bdrv_save_vmstate(blk_bs(blk
), buf
, pos
, size
);
2369 if (ret
== size
&& !blk
->enable_write_cache
) {
2370 ret
= bdrv_flush(blk_bs(blk
));
2373 return ret
< 0 ? ret
: size
;
2376 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
2378 GLOBAL_STATE_CODE();
2379 if (!blk_is_available(blk
)) {
2383 return bdrv_load_vmstate(blk_bs(blk
), buf
, pos
, size
);
2386 int blk_probe_blocksizes(BlockBackend
*blk
, BlockSizes
*bsz
)
2388 GLOBAL_STATE_CODE();
2389 if (!blk_is_available(blk
)) {
2393 return bdrv_probe_blocksizes(blk_bs(blk
), bsz
);
2396 int blk_probe_geometry(BlockBackend
*blk
, HDGeometry
*geo
)
2398 GLOBAL_STATE_CODE();
2399 if (!blk_is_available(blk
)) {
2403 return bdrv_probe_geometry(blk_bs(blk
), geo
);
2407 * Updates the BlockBackendRootState object with data from the currently
2408 * attached BlockDriverState.
2410 void blk_update_root_state(BlockBackend
*blk
)
2412 GLOBAL_STATE_CODE();
2415 blk
->root_state
.open_flags
= blk
->root
->bs
->open_flags
;
2416 blk
->root_state
.detect_zeroes
= blk
->root
->bs
->detect_zeroes
;
2420 * Returns the detect-zeroes setting to be used for bdrv_open() of a
2421 * BlockDriverState which is supposed to inherit the root state.
2423 bool blk_get_detect_zeroes_from_root_state(BlockBackend
*blk
)
2425 GLOBAL_STATE_CODE();
2426 return blk
->root_state
.detect_zeroes
;
2430 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
2431 * supposed to inherit the root state.
2433 int blk_get_open_flags_from_root_state(BlockBackend
*blk
)
2435 GLOBAL_STATE_CODE();
2436 return blk
->root_state
.open_flags
;
2439 BlockBackendRootState
*blk_get_root_state(BlockBackend
*blk
)
2441 GLOBAL_STATE_CODE();
2442 return &blk
->root_state
;
2445 int blk_commit_all(void)
2447 BlockBackend
*blk
= NULL
;
2448 GLOBAL_STATE_CODE();
2450 while ((blk
= blk_all_next(blk
)) != NULL
) {
2451 AioContext
*aio_context
= blk_get_aio_context(blk
);
2452 BlockDriverState
*unfiltered_bs
= bdrv_skip_filters(blk_bs(blk
));
2454 aio_context_acquire(aio_context
);
2455 if (blk_is_inserted(blk
) && bdrv_cow_child(unfiltered_bs
)) {
2458 ret
= bdrv_commit(unfiltered_bs
);
2460 aio_context_release(aio_context
);
2464 aio_context_release(aio_context
);
2470 /* throttling disk I/O limits */
2471 void blk_set_io_limits(BlockBackend
*blk
, ThrottleConfig
*cfg
)
2473 GLOBAL_STATE_CODE();
2474 throttle_group_config(&blk
->public.throttle_group_member
, cfg
);
2477 void blk_io_limits_disable(BlockBackend
*blk
)
2479 BlockDriverState
*bs
= blk_bs(blk
);
2480 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
2481 assert(tgm
->throttle_state
);
2482 GLOBAL_STATE_CODE();
2485 bdrv_drained_begin(bs
);
2487 throttle_group_unregister_tgm(tgm
);
2489 bdrv_drained_end(bs
);
2494 /* should be called before blk_set_io_limits if a limit is set */
2495 void blk_io_limits_enable(BlockBackend
*blk
, const char *group
)
2497 assert(!blk
->public.throttle_group_member
.throttle_state
);
2498 GLOBAL_STATE_CODE();
2499 throttle_group_register_tgm(&blk
->public.throttle_group_member
,
2500 group
, blk_get_aio_context(blk
));
2503 void blk_io_limits_update_group(BlockBackend
*blk
, const char *group
)
2505 GLOBAL_STATE_CODE();
2506 /* this BB is not part of any group */
2507 if (!blk
->public.throttle_group_member
.throttle_state
) {
2511 /* this BB is a part of the same group than the one we want */
2512 if (!g_strcmp0(throttle_group_get_name(&blk
->public.throttle_group_member
),
2517 /* need to change the group this bs belong to */
2518 blk_io_limits_disable(blk
);
2519 blk_io_limits_enable(blk
, group
);
2522 static void blk_root_drained_begin(BdrvChild
*child
)
2524 BlockBackend
*blk
= child
->opaque
;
2525 ThrottleGroupMember
*tgm
= &blk
->public.throttle_group_member
;
2527 if (++blk
->quiesce_counter
== 1) {
2528 if (blk
->dev_ops
&& blk
->dev_ops
->drained_begin
) {
2529 blk
->dev_ops
->drained_begin(blk
->dev_opaque
);
2533 /* Note that blk->root may not be accessible here yet if we are just
2534 * attaching to a BlockDriverState that is drained. Use child instead. */
2536 if (qatomic_fetch_inc(&tgm
->io_limits_disabled
) == 0) {
2537 throttle_group_restart_tgm(tgm
);
2541 static bool blk_root_drained_poll(BdrvChild
*child
)
2543 BlockBackend
*blk
= child
->opaque
;
2545 assert(blk
->quiesce_counter
);
2547 if (blk
->dev_ops
&& blk
->dev_ops
->drained_poll
) {
2548 busy
= blk
->dev_ops
->drained_poll(blk
->dev_opaque
);
2550 return busy
|| !!blk
->in_flight
;
2553 static void blk_root_drained_end(BdrvChild
*child
, int *drained_end_counter
)
2555 BlockBackend
*blk
= child
->opaque
;
2556 assert(blk
->quiesce_counter
);
2558 assert(blk
->public.throttle_group_member
.io_limits_disabled
);
2559 qatomic_dec(&blk
->public.throttle_group_member
.io_limits_disabled
);
2561 if (--blk
->quiesce_counter
== 0) {
2562 if (blk
->dev_ops
&& blk
->dev_ops
->drained_end
) {
2563 blk
->dev_ops
->drained_end(blk
->dev_opaque
);
2565 while (qemu_co_enter_next(&blk
->queued_requests
, NULL
)) {
2566 /* Resume all queued requests */
2571 void blk_register_buf(BlockBackend
*blk
, void *host
, size_t size
)
2573 GLOBAL_STATE_CODE();
2574 bdrv_register_buf(blk_bs(blk
), host
, size
);
2577 void blk_unregister_buf(BlockBackend
*blk
, void *host
)
2579 GLOBAL_STATE_CODE();
2580 bdrv_unregister_buf(blk_bs(blk
), host
);
2583 int coroutine_fn
blk_co_copy_range(BlockBackend
*blk_in
, int64_t off_in
,
2584 BlockBackend
*blk_out
, int64_t off_out
,
2585 int64_t bytes
, BdrvRequestFlags read_flags
,
2586 BdrvRequestFlags write_flags
)
2591 r
= blk_check_byte_request(blk_in
, off_in
, bytes
);
2595 r
= blk_check_byte_request(blk_out
, off_out
, bytes
);
2599 return bdrv_co_copy_range(blk_in
->root
, off_in
,
2600 blk_out
->root
, off_out
,
2601 bytes
, read_flags
, write_flags
);
2604 const BdrvChild
*blk_root(BlockBackend
*blk
)
2606 GLOBAL_STATE_CODE();
2610 int blk_make_empty(BlockBackend
*blk
, Error
**errp
)
2612 GLOBAL_STATE_CODE();
2613 if (!blk_is_available(blk
)) {
2614 error_setg(errp
, "No medium inserted");
2618 return bdrv_make_empty(blk
->root
, errp
);