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 "sysemu/blockdev.h"
19 #include "sysemu/sysemu.h"
20 #include "qapi-event.h"
23 /* Number of coroutines to reserve per attached device model */
24 #define COROUTINE_POOL_RESERVATION 64
26 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
28 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
);
34 DriveInfo
*legacy_dinfo
; /* null unless created by drive_new() */
35 QTAILQ_ENTRY(BlockBackend
) link
; /* for block_backends */
36 QTAILQ_ENTRY(BlockBackend
) monitor_link
; /* for monitor_block_backends */
38 void *dev
; /* attached device model, if any */
39 /* TODO change to DeviceState when all users are qdevified */
40 const BlockDevOps
*dev_ops
;
43 /* the block size for which the guest device expects atomicity */
46 /* If the BDS tree is removed, some of its options are stored here (which
47 * can be used to restore those options in the new BDS on insert) */
48 BlockBackendRootState root_state
;
50 bool enable_write_cache
;
52 /* I/O stats (display with "info blockstats"). */
55 BlockdevOnError on_read_error
, on_write_error
;
56 bool iostatus_enabled
;
57 BlockDeviceIoStatus iostatus
;
59 bool allow_write_beyond_eof
;
61 NotifierList remove_bs_notifiers
, insert_bs_notifiers
;
64 typedef struct BlockBackendAIOCB
{
71 static const AIOCBInfo block_backend_aiocb_info
= {
72 .get_aio_context
= blk_aiocb_get_aio_context
,
73 .aiocb_size
= sizeof(BlockBackendAIOCB
),
76 static void drive_info_del(DriveInfo
*dinfo
);
78 /* All BlockBackends */
79 static QTAILQ_HEAD(, BlockBackend
) block_backends
=
80 QTAILQ_HEAD_INITIALIZER(block_backends
);
82 /* All BlockBackends referenced by the monitor and which are iterated through by
84 static QTAILQ_HEAD(, BlockBackend
) monitor_block_backends
=
85 QTAILQ_HEAD_INITIALIZER(monitor_block_backends
);
87 static void blk_root_inherit_options(int *child_flags
, QDict
*child_options
,
88 int parent_flags
, QDict
*parent_options
)
90 /* We're not supposed to call this function for root nodes */
94 static const BdrvChildRole child_root
= {
95 .inherit_options
= blk_root_inherit_options
,
99 * Create a new BlockBackend with a reference count of one.
100 * Store an error through @errp on failure, unless it's null.
101 * Return the new BlockBackend on success, null on failure.
103 BlockBackend
*blk_new(Error
**errp
)
107 blk
= g_new0(BlockBackend
, 1);
109 notifier_list_init(&blk
->remove_bs_notifiers
);
110 notifier_list_init(&blk
->insert_bs_notifiers
);
111 QTAILQ_INSERT_TAIL(&block_backends
, blk
, link
);
116 * Create a new BlockBackend with a new BlockDriverState attached.
117 * Otherwise just like blk_new(), which see.
119 BlockBackend
*blk_new_with_bs(Error
**errp
)
122 BlockDriverState
*bs
;
129 bs
= bdrv_new_root();
130 blk
->root
= bdrv_root_attach_child(bs
, "root", &child_root
);
136 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
138 * Just as with bdrv_open(), after having called this function the reference to
139 * @options belongs to the block layer (even on failure).
141 * TODO: Remove @filename and @flags; it should be possible to specify a whole
142 * BDS tree just by specifying the @options QDict (or @reference,
143 * alternatively). At the time of adding this function, this is not possible,
144 * though, so callers of this function have to be able to specify @filename and
147 BlockBackend
*blk_new_open(const char *filename
, const char *reference
,
148 QDict
*options
, int flags
, Error
**errp
)
153 blk
= blk_new_with_bs(errp
);
159 ret
= bdrv_open(&blk
->root
->bs
, filename
, reference
, options
, flags
, errp
);
165 blk_set_enable_write_cache(blk
, true);
170 static void blk_delete(BlockBackend
*blk
)
172 assert(!blk
->refcnt
);
178 assert(QLIST_EMPTY(&blk
->remove_bs_notifiers
.notifiers
));
179 assert(QLIST_EMPTY(&blk
->insert_bs_notifiers
.notifiers
));
180 if (blk
->root_state
.throttle_state
) {
181 g_free(blk
->root_state
.throttle_group
);
182 throttle_group_unref(blk
->root_state
.throttle_state
);
184 QTAILQ_REMOVE(&block_backends
, blk
, link
);
185 drive_info_del(blk
->legacy_dinfo
);
186 block_acct_cleanup(&blk
->stats
);
190 static void drive_info_del(DriveInfo
*dinfo
)
195 qemu_opts_del(dinfo
->opts
);
196 g_free(dinfo
->serial
);
200 int blk_get_refcnt(BlockBackend
*blk
)
202 return blk
? blk
->refcnt
: 0;
206 * Increment @blk's reference count.
207 * @blk must not be null.
209 void blk_ref(BlockBackend
*blk
)
215 * Decrement @blk's reference count.
216 * If this drops it to zero, destroy @blk.
217 * For convenience, do nothing if @blk is null.
219 void blk_unref(BlockBackend
*blk
)
222 assert(blk
->refcnt
> 0);
223 if (!--blk
->refcnt
) {
230 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
231 * ones which are hidden (i.e. are not referenced by the monitor).
233 static BlockBackend
*blk_all_next(BlockBackend
*blk
)
235 return blk
? QTAILQ_NEXT(blk
, link
)
236 : QTAILQ_FIRST(&block_backends
);
239 void blk_remove_all_bs(void)
241 BlockBackend
*blk
= NULL
;
243 while ((blk
= blk_all_next(blk
)) != NULL
) {
244 AioContext
*ctx
= blk_get_aio_context(blk
);
246 aio_context_acquire(ctx
);
250 aio_context_release(ctx
);
255 * Return the monitor-owned BlockBackend after @blk.
256 * If @blk is null, return the first one.
257 * Else, return @blk's next sibling, which may be null.
259 * To iterate over all BlockBackends, do
260 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
264 BlockBackend
*blk_next(BlockBackend
*blk
)
266 return blk
? QTAILQ_NEXT(blk
, monitor_link
)
267 : QTAILQ_FIRST(&monitor_block_backends
);
271 * Iterates over all BlockDriverStates which are attached to a BlockBackend.
272 * This function is for use by bdrv_next().
274 * @bs must be NULL or a BDS that is attached to a BB.
276 BlockDriverState
*blk_next_root_bs(BlockDriverState
*bs
)
288 blk
= blk_all_next(blk
);
289 } while (blk
&& !blk
->root
);
291 return blk
? blk
->root
->bs
: NULL
;
295 * Add a BlockBackend into the list of backends referenced by the monitor, with
296 * the given @name acting as the handle for the monitor.
297 * Strictly for use by blockdev.c.
299 * @name must not be null or empty.
301 * Returns true on success and false on failure. In the latter case, an Error
302 * object is returned through @errp.
304 bool monitor_add_blk(BlockBackend
*blk
, const char *name
, Error
**errp
)
307 assert(name
&& name
[0]);
309 if (!id_wellformed(name
)) {
310 error_setg(errp
, "Invalid device name");
313 if (blk_by_name(name
)) {
314 error_setg(errp
, "Device with id '%s' already exists", name
);
317 if (bdrv_find_node(name
)) {
319 "Device name '%s' conflicts with an existing node name",
324 blk
->name
= g_strdup(name
);
325 QTAILQ_INSERT_TAIL(&monitor_block_backends
, blk
, monitor_link
);
330 * Remove a BlockBackend from the list of backends referenced by the monitor.
331 * Strictly for use by blockdev.c.
333 void monitor_remove_blk(BlockBackend
*blk
)
339 QTAILQ_REMOVE(&monitor_block_backends
, blk
, monitor_link
);
345 * Return @blk's name, a non-null string.
346 * Returns an empty string iff @blk is not referenced by the monitor.
348 const char *blk_name(BlockBackend
*blk
)
350 return blk
->name
?: "";
354 * Return the BlockBackend with name @name if it exists, else null.
355 * @name must not be null.
357 BlockBackend
*blk_by_name(const char *name
)
359 BlockBackend
*blk
= NULL
;
362 while ((blk
= blk_next(blk
)) != NULL
) {
363 if (!strcmp(name
, blk
->name
)) {
371 * Return the BlockDriverState attached to @blk if any, else null.
373 BlockDriverState
*blk_bs(BlockBackend
*blk
)
375 return blk
->root
? blk
->root
->bs
: NULL
;
379 * Return @blk's DriveInfo if any, else null.
381 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
383 return blk
->legacy_dinfo
;
387 * Set @blk's DriveInfo to @dinfo, and return it.
388 * @blk must not have a DriveInfo set already.
389 * No other BlockBackend may have the same DriveInfo set.
391 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
393 assert(!blk
->legacy_dinfo
);
394 return blk
->legacy_dinfo
= dinfo
;
398 * Return the BlockBackend with DriveInfo @dinfo.
401 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
403 BlockBackend
*blk
= NULL
;
405 while ((blk
= blk_next(blk
)) != NULL
) {
406 if (blk
->legacy_dinfo
== dinfo
) {
414 * Disassociates the currently associated BlockDriverState from @blk.
416 void blk_remove_bs(BlockBackend
*blk
)
418 assert(blk
->root
->bs
->blk
== blk
);
420 notifier_list_notify(&blk
->remove_bs_notifiers
, blk
);
422 blk_update_root_state(blk
);
424 blk
->root
->bs
->blk
= NULL
;
425 bdrv_root_unref_child(blk
->root
);
430 * Associates a new BlockDriverState with @blk.
432 void blk_insert_bs(BlockBackend
*blk
, BlockDriverState
*bs
)
434 assert(!blk
->root
&& !bs
->blk
);
436 blk
->root
= bdrv_root_attach_child(bs
, "root", &child_root
);
439 notifier_list_notify(&blk
->insert_bs_notifiers
, blk
);
443 * Attach device model @dev to @blk.
444 * Return 0 on success, -EBUSY when a device model is attached already.
446 int blk_attach_dev(BlockBackend
*blk
, void *dev
)
447 /* TODO change to DeviceState *dev when all users are qdevified */
454 blk_iostatus_reset(blk
);
459 * Attach device model @dev to @blk.
460 * @blk must not have a device model attached already.
461 * TODO qdevified devices don't use this, remove when devices are qdevified
463 void blk_attach_dev_nofail(BlockBackend
*blk
, void *dev
)
465 if (blk_attach_dev(blk
, dev
) < 0) {
471 * Detach device model @dev from @blk.
472 * @dev must be currently attached to @blk.
474 void blk_detach_dev(BlockBackend
*blk
, void *dev
)
475 /* TODO change to DeviceState *dev when all users are qdevified */
477 assert(blk
->dev
== dev
);
480 blk
->dev_opaque
= NULL
;
481 blk
->guest_block_size
= 512;
486 * Return the device model attached to @blk if any, else null.
488 void *blk_get_attached_dev(BlockBackend
*blk
)
489 /* TODO change to return DeviceState * when all users are qdevified */
495 * Set @blk's device model callbacks to @ops.
496 * @opaque is the opaque argument to pass to the callbacks.
497 * This is for use by device models.
499 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
503 blk
->dev_opaque
= opaque
;
507 * Notify @blk's attached device model of media change.
508 * If @load is true, notify of media load.
509 * Else, notify of media eject.
510 * Also send DEVICE_TRAY_MOVED events as appropriate.
512 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
)
514 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
515 bool tray_was_open
, tray_is_open
;
517 tray_was_open
= blk_dev_is_tray_open(blk
);
518 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
);
519 tray_is_open
= blk_dev_is_tray_open(blk
);
521 if (tray_was_open
!= tray_is_open
) {
522 qapi_event_send_device_tray_moved(blk_name(blk
), tray_is_open
,
529 * Does @blk's attached device model have removable media?
530 * %true if no device model is attached.
532 bool blk_dev_has_removable_media(BlockBackend
*blk
)
534 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
538 * Does @blk's attached device model have a tray?
540 bool blk_dev_has_tray(BlockBackend
*blk
)
542 return blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
;
546 * Notify @blk's attached device model of a media eject request.
547 * If @force is true, the medium is about to be yanked out forcefully.
549 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
551 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
552 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
557 * Does @blk's attached device model have a tray, and is it open?
559 bool blk_dev_is_tray_open(BlockBackend
*blk
)
561 if (blk_dev_has_tray(blk
)) {
562 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
568 * Does @blk's attached device model have the medium locked?
569 * %false if the device model has no such lock.
571 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
573 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
574 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
580 * Notify @blk's attached device model of a backend size change.
582 void blk_dev_resize_cb(BlockBackend
*blk
)
584 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
585 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
589 void blk_iostatus_enable(BlockBackend
*blk
)
591 blk
->iostatus_enabled
= true;
592 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
595 /* The I/O status is only enabled if the drive explicitly
596 * enables it _and_ the VM is configured to stop on errors */
597 bool blk_iostatus_is_enabled(const BlockBackend
*blk
)
599 return (blk
->iostatus_enabled
&&
600 (blk
->on_write_error
== BLOCKDEV_ON_ERROR_ENOSPC
||
601 blk
->on_write_error
== BLOCKDEV_ON_ERROR_STOP
||
602 blk
->on_read_error
== BLOCKDEV_ON_ERROR_STOP
));
605 BlockDeviceIoStatus
blk_iostatus(const BlockBackend
*blk
)
607 return blk
->iostatus
;
610 void blk_iostatus_disable(BlockBackend
*blk
)
612 blk
->iostatus_enabled
= false;
615 void blk_iostatus_reset(BlockBackend
*blk
)
617 if (blk_iostatus_is_enabled(blk
)) {
618 BlockDriverState
*bs
= blk_bs(blk
);
619 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
621 block_job_iostatus_reset(bs
->job
);
626 void blk_iostatus_set_err(BlockBackend
*blk
, int error
)
628 assert(blk_iostatus_is_enabled(blk
));
629 if (blk
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
630 blk
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
631 BLOCK_DEVICE_IO_STATUS_FAILED
;
635 void blk_set_allow_write_beyond_eof(BlockBackend
*blk
, bool allow
)
637 blk
->allow_write_beyond_eof
= allow
;
640 static int blk_check_byte_request(BlockBackend
*blk
, int64_t offset
,
645 if (size
> INT_MAX
) {
649 if (!blk_is_available(blk
)) {
657 if (!blk
->allow_write_beyond_eof
) {
658 len
= blk_getlength(blk
);
663 if (offset
> len
|| len
- offset
< size
) {
671 static int blk_check_request(BlockBackend
*blk
, int64_t sector_num
,
674 if (sector_num
< 0 || sector_num
> INT64_MAX
/ BDRV_SECTOR_SIZE
) {
678 if (nb_sectors
< 0 || nb_sectors
> INT_MAX
/ BDRV_SECTOR_SIZE
) {
682 return blk_check_byte_request(blk
, sector_num
* BDRV_SECTOR_SIZE
,
683 nb_sectors
* BDRV_SECTOR_SIZE
);
686 static int coroutine_fn
blk_co_preadv(BlockBackend
*blk
, int64_t offset
,
687 unsigned int bytes
, QEMUIOVector
*qiov
,
688 BdrvRequestFlags flags
)
690 int ret
= blk_check_byte_request(blk
, offset
, bytes
);
695 return bdrv_co_preadv(blk_bs(blk
), offset
, bytes
, qiov
, flags
);
698 static int coroutine_fn
blk_co_pwritev(BlockBackend
*blk
, int64_t offset
,
699 unsigned int bytes
, QEMUIOVector
*qiov
,
700 BdrvRequestFlags flags
)
704 ret
= blk_check_byte_request(blk
, offset
, bytes
);
709 if (!blk
->enable_write_cache
) {
710 flags
|= BDRV_REQ_FUA
;
713 return bdrv_co_pwritev(blk_bs(blk
), offset
, bytes
, qiov
, flags
);
716 typedef struct BlkRwCo
{
721 BdrvRequestFlags flags
;
724 static void blk_read_entry(void *opaque
)
726 BlkRwCo
*rwco
= opaque
;
728 rwco
->ret
= blk_co_preadv(rwco
->blk
, rwco
->offset
, rwco
->qiov
->size
,
729 rwco
->qiov
, rwco
->flags
);
732 static void blk_write_entry(void *opaque
)
734 BlkRwCo
*rwco
= opaque
;
736 rwco
->ret
= blk_co_pwritev(rwco
->blk
, rwco
->offset
, rwco
->qiov
->size
,
737 rwco
->qiov
, rwco
->flags
);
740 static int blk_prw(BlockBackend
*blk
, int64_t offset
, uint8_t *buf
,
741 int64_t bytes
, CoroutineEntry co_entry
,
742 BdrvRequestFlags flags
)
744 AioContext
*aio_context
;
750 iov
= (struct iovec
) {
754 qemu_iovec_init_external(&qiov
, &iov
, 1);
764 co
= qemu_coroutine_create(co_entry
);
765 qemu_coroutine_enter(co
, &rwco
);
767 aio_context
= blk_get_aio_context(blk
);
768 while (rwco
.ret
== NOT_DONE
) {
769 aio_poll(aio_context
, true);
775 int blk_pread_unthrottled(BlockBackend
*blk
, int64_t offset
, uint8_t *buf
,
778 BlockDriverState
*bs
= blk_bs(blk
);
781 ret
= blk_check_byte_request(blk
, offset
, count
);
786 bdrv_no_throttling_begin(bs
);
787 ret
= blk_pread(blk
, offset
, buf
, count
);
788 bdrv_no_throttling_end(bs
);
792 int blk_write_zeroes(BlockBackend
*blk
, int64_t offset
,
793 int count
, BdrvRequestFlags flags
)
795 return blk_prw(blk
, offset
, NULL
, count
, blk_write_entry
,
796 flags
| BDRV_REQ_ZERO_WRITE
);
799 static void error_callback_bh(void *opaque
)
801 struct BlockBackendAIOCB
*acb
= opaque
;
802 qemu_bh_delete(acb
->bh
);
803 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
807 BlockAIOCB
*blk_abort_aio_request(BlockBackend
*blk
,
808 BlockCompletionFunc
*cb
,
809 void *opaque
, int ret
)
811 struct BlockBackendAIOCB
*acb
;
814 acb
= blk_aio_get(&block_backend_aiocb_info
, blk
, cb
, opaque
);
818 bh
= aio_bh_new(blk_get_aio_context(blk
), error_callback_bh
, acb
);
820 qemu_bh_schedule(bh
);
825 typedef struct BlkAioEmAIOCB
{
833 static const AIOCBInfo blk_aio_em_aiocb_info
= {
834 .aiocb_size
= sizeof(BlkAioEmAIOCB
),
837 static void blk_aio_complete(BlkAioEmAIOCB
*acb
)
840 assert(acb
->has_returned
);
841 qemu_bh_delete(acb
->bh
);
843 if (acb
->has_returned
) {
844 acb
->common
.cb(acb
->common
.opaque
, acb
->rwco
.ret
);
849 static void blk_aio_complete_bh(void *opaque
)
851 blk_aio_complete(opaque
);
854 static BlockAIOCB
*blk_aio_prwv(BlockBackend
*blk
, int64_t offset
, int bytes
,
855 QEMUIOVector
*qiov
, CoroutineEntry co_entry
,
856 BdrvRequestFlags flags
,
857 BlockCompletionFunc
*cb
, void *opaque
)
862 acb
= blk_aio_get(&blk_aio_em_aiocb_info
, blk
, cb
, opaque
);
863 acb
->rwco
= (BlkRwCo
) {
872 acb
->has_returned
= false;
874 co
= qemu_coroutine_create(co_entry
);
875 qemu_coroutine_enter(co
, acb
);
877 acb
->has_returned
= true;
878 if (acb
->rwco
.ret
!= NOT_DONE
) {
879 acb
->bh
= aio_bh_new(blk_get_aio_context(blk
), blk_aio_complete_bh
, acb
);
880 qemu_bh_schedule(acb
->bh
);
886 static void blk_aio_read_entry(void *opaque
)
888 BlkAioEmAIOCB
*acb
= opaque
;
889 BlkRwCo
*rwco
= &acb
->rwco
;
891 assert(rwco
->qiov
->size
== acb
->bytes
);
892 rwco
->ret
= blk_co_preadv(rwco
->blk
, rwco
->offset
, acb
->bytes
,
893 rwco
->qiov
, rwco
->flags
);
894 blk_aio_complete(acb
);
897 static void blk_aio_write_entry(void *opaque
)
899 BlkAioEmAIOCB
*acb
= opaque
;
900 BlkRwCo
*rwco
= &acb
->rwco
;
902 assert(!rwco
->qiov
|| rwco
->qiov
->size
== acb
->bytes
);
903 rwco
->ret
= blk_co_pwritev(rwco
->blk
, rwco
->offset
, acb
->bytes
,
904 rwco
->qiov
, rwco
->flags
);
905 blk_aio_complete(acb
);
908 BlockAIOCB
*blk_aio_write_zeroes(BlockBackend
*blk
, int64_t offset
,
909 int count
, BdrvRequestFlags flags
,
910 BlockCompletionFunc
*cb
, void *opaque
)
912 return blk_aio_prwv(blk
, offset
, count
, NULL
, blk_aio_write_entry
,
913 flags
| BDRV_REQ_ZERO_WRITE
, cb
, opaque
);
916 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int count
)
918 int ret
= blk_prw(blk
, offset
, buf
, count
, blk_read_entry
, 0);
925 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int count
,
926 BdrvRequestFlags flags
)
928 int ret
= blk_prw(blk
, offset
, (void *) buf
, count
, blk_write_entry
,
936 int64_t blk_getlength(BlockBackend
*blk
)
938 if (!blk_is_available(blk
)) {
942 return bdrv_getlength(blk_bs(blk
));
945 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
950 bdrv_get_geometry(blk_bs(blk
), nb_sectors_ptr
);
954 int64_t blk_nb_sectors(BlockBackend
*blk
)
956 if (!blk_is_available(blk
)) {
960 return bdrv_nb_sectors(blk_bs(blk
));
963 BlockAIOCB
*blk_aio_preadv(BlockBackend
*blk
, int64_t offset
,
964 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
965 BlockCompletionFunc
*cb
, void *opaque
)
967 return blk_aio_prwv(blk
, offset
, qiov
->size
, qiov
,
968 blk_aio_read_entry
, flags
, cb
, opaque
);
971 BlockAIOCB
*blk_aio_pwritev(BlockBackend
*blk
, int64_t offset
,
972 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
973 BlockCompletionFunc
*cb
, void *opaque
)
975 return blk_aio_prwv(blk
, offset
, qiov
->size
, qiov
,
976 blk_aio_write_entry
, flags
, cb
, opaque
);
979 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
980 BlockCompletionFunc
*cb
, void *opaque
)
982 if (!blk_is_available(blk
)) {
983 return blk_abort_aio_request(blk
, cb
, opaque
, -ENOMEDIUM
);
986 return bdrv_aio_flush(blk_bs(blk
), cb
, opaque
);
989 BlockAIOCB
*blk_aio_discard(BlockBackend
*blk
,
990 int64_t sector_num
, int nb_sectors
,
991 BlockCompletionFunc
*cb
, void *opaque
)
993 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
995 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
998 return bdrv_aio_discard(blk_bs(blk
), sector_num
, nb_sectors
, cb
, opaque
);
1001 void blk_aio_cancel(BlockAIOCB
*acb
)
1003 bdrv_aio_cancel(acb
);
1006 void blk_aio_cancel_async(BlockAIOCB
*acb
)
1008 bdrv_aio_cancel_async(acb
);
1011 int blk_aio_multiwrite(BlockBackend
*blk
, BlockRequest
*reqs
, int num_reqs
)
1015 for (i
= 0; i
< num_reqs
; i
++) {
1016 ret
= blk_check_request(blk
, reqs
[i
].sector
, reqs
[i
].nb_sectors
);
1022 return bdrv_aio_multiwrite(blk_bs(blk
), reqs
, num_reqs
);
1025 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
1027 if (!blk_is_available(blk
)) {
1031 return bdrv_ioctl(blk_bs(blk
), req
, buf
);
1034 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
1035 BlockCompletionFunc
*cb
, void *opaque
)
1037 if (!blk_is_available(blk
)) {
1038 return blk_abort_aio_request(blk
, cb
, opaque
, -ENOMEDIUM
);
1041 return bdrv_aio_ioctl(blk_bs(blk
), req
, buf
, cb
, opaque
);
1044 int blk_co_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
1046 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1051 return bdrv_co_discard(blk_bs(blk
), sector_num
, nb_sectors
);
1054 int blk_co_flush(BlockBackend
*blk
)
1056 if (!blk_is_available(blk
)) {
1060 return bdrv_co_flush(blk_bs(blk
));
1063 int blk_flush(BlockBackend
*blk
)
1065 if (!blk_is_available(blk
)) {
1069 return bdrv_flush(blk_bs(blk
));
1072 void blk_drain(BlockBackend
*blk
)
1075 bdrv_drain(blk_bs(blk
));
1079 void blk_drain_all(void)
1084 void blk_set_on_error(BlockBackend
*blk
, BlockdevOnError on_read_error
,
1085 BlockdevOnError on_write_error
)
1087 blk
->on_read_error
= on_read_error
;
1088 blk
->on_write_error
= on_write_error
;
1091 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
1093 return is_read
? blk
->on_read_error
: blk
->on_write_error
;
1096 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
1099 BlockdevOnError on_err
= blk_get_on_error(blk
, is_read
);
1102 case BLOCKDEV_ON_ERROR_ENOSPC
:
1103 return (error
== ENOSPC
) ?
1104 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
1105 case BLOCKDEV_ON_ERROR_STOP
:
1106 return BLOCK_ERROR_ACTION_STOP
;
1107 case BLOCKDEV_ON_ERROR_REPORT
:
1108 return BLOCK_ERROR_ACTION_REPORT
;
1109 case BLOCKDEV_ON_ERROR_IGNORE
:
1110 return BLOCK_ERROR_ACTION_IGNORE
;
1116 static void send_qmp_error_event(BlockBackend
*blk
,
1117 BlockErrorAction action
,
1118 bool is_read
, int error
)
1120 IoOperationType optype
;
1122 optype
= is_read
? IO_OPERATION_TYPE_READ
: IO_OPERATION_TYPE_WRITE
;
1123 qapi_event_send_block_io_error(blk_name(blk
), optype
, action
,
1124 blk_iostatus_is_enabled(blk
),
1125 error
== ENOSPC
, strerror(error
),
1129 /* This is done by device models because, while the block layer knows
1130 * about the error, it does not know whether an operation comes from
1131 * the device or the block layer (from a job, for example).
1133 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
1134 bool is_read
, int error
)
1138 if (action
== BLOCK_ERROR_ACTION_STOP
) {
1139 /* First set the iostatus, so that "info block" returns an iostatus
1140 * that matches the events raised so far (an additional error iostatus
1141 * is fine, but not a lost one).
1143 blk_iostatus_set_err(blk
, error
);
1145 /* Then raise the request to stop the VM and the event.
1146 * qemu_system_vmstop_request_prepare has two effects. First,
1147 * it ensures that the STOP event always comes after the
1148 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1149 * can observe the STOP event and do a "cont" before the STOP
1150 * event is issued, the VM will not stop. In this case, vm_start()
1151 * also ensures that the STOP/RESUME pair of events is emitted.
1153 qemu_system_vmstop_request_prepare();
1154 send_qmp_error_event(blk
, action
, is_read
, error
);
1155 qemu_system_vmstop_request(RUN_STATE_IO_ERROR
);
1157 send_qmp_error_event(blk
, action
, is_read
, error
);
1161 int blk_is_read_only(BlockBackend
*blk
)
1163 BlockDriverState
*bs
= blk_bs(blk
);
1166 return bdrv_is_read_only(bs
);
1168 return blk
->root_state
.read_only
;
1172 int blk_is_sg(BlockBackend
*blk
)
1174 BlockDriverState
*bs
= blk_bs(blk
);
1180 return bdrv_is_sg(bs
);
1183 int blk_enable_write_cache(BlockBackend
*blk
)
1185 return blk
->enable_write_cache
;
1188 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
1190 blk
->enable_write_cache
= wce
;
1193 void blk_invalidate_cache(BlockBackend
*blk
, Error
**errp
)
1195 BlockDriverState
*bs
= blk_bs(blk
);
1198 error_setg(errp
, "Device '%s' has no medium", blk
->name
);
1202 bdrv_invalidate_cache(bs
, errp
);
1205 bool blk_is_inserted(BlockBackend
*blk
)
1207 BlockDriverState
*bs
= blk_bs(blk
);
1209 return bs
&& bdrv_is_inserted(bs
);
1212 bool blk_is_available(BlockBackend
*blk
)
1214 return blk_is_inserted(blk
) && !blk_dev_is_tray_open(blk
);
1217 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
1219 BlockDriverState
*bs
= blk_bs(blk
);
1222 bdrv_lock_medium(bs
, locked
);
1226 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
1228 BlockDriverState
*bs
= blk_bs(blk
);
1231 bdrv_eject(bs
, eject_flag
);
1235 int blk_get_flags(BlockBackend
*blk
)
1237 BlockDriverState
*bs
= blk_bs(blk
);
1240 return bdrv_get_flags(bs
);
1242 return blk
->root_state
.open_flags
;
1246 int blk_get_max_transfer_length(BlockBackend
*blk
)
1248 BlockDriverState
*bs
= blk_bs(blk
);
1251 return bs
->bl
.max_transfer_length
;
1257 int blk_get_max_iov(BlockBackend
*blk
)
1259 return blk
->root
->bs
->bl
.max_iov
;
1262 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
1264 blk
->guest_block_size
= align
;
1267 void *blk_try_blockalign(BlockBackend
*blk
, size_t size
)
1269 return qemu_try_blockalign(blk
? blk_bs(blk
) : NULL
, size
);
1272 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
1274 return qemu_blockalign(blk
? blk_bs(blk
) : NULL
, size
);
1277 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
1279 BlockDriverState
*bs
= blk_bs(blk
);
1285 return bdrv_op_is_blocked(bs
, op
, errp
);
1288 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
1290 BlockDriverState
*bs
= blk_bs(blk
);
1293 bdrv_op_unblock(bs
, op
, reason
);
1297 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
1299 BlockDriverState
*bs
= blk_bs(blk
);
1302 bdrv_op_block_all(bs
, reason
);
1306 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
1308 BlockDriverState
*bs
= blk_bs(blk
);
1311 bdrv_op_unblock_all(bs
, reason
);
1315 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
1317 BlockDriverState
*bs
= blk_bs(blk
);
1320 return bdrv_get_aio_context(bs
);
1322 return qemu_get_aio_context();
1326 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
)
1328 BlockBackendAIOCB
*blk_acb
= DO_UPCAST(BlockBackendAIOCB
, common
, acb
);
1329 return blk_get_aio_context(blk_acb
->blk
);
1332 void blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
)
1334 BlockDriverState
*bs
= blk_bs(blk
);
1337 bdrv_set_aio_context(bs
, new_context
);
1341 void blk_add_aio_context_notifier(BlockBackend
*blk
,
1342 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
1343 void (*detach_aio_context
)(void *opaque
), void *opaque
)
1345 BlockDriverState
*bs
= blk_bs(blk
);
1348 bdrv_add_aio_context_notifier(bs
, attached_aio_context
,
1349 detach_aio_context
, opaque
);
1353 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
1354 void (*attached_aio_context
)(AioContext
*,
1356 void (*detach_aio_context
)(void *),
1359 BlockDriverState
*bs
= blk_bs(blk
);
1362 bdrv_remove_aio_context_notifier(bs
, attached_aio_context
,
1363 detach_aio_context
, opaque
);
1367 void blk_add_remove_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
1369 notifier_list_add(&blk
->remove_bs_notifiers
, notify
);
1372 void blk_add_insert_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
1374 notifier_list_add(&blk
->insert_bs_notifiers
, notify
);
1377 void blk_io_plug(BlockBackend
*blk
)
1379 BlockDriverState
*bs
= blk_bs(blk
);
1386 void blk_io_unplug(BlockBackend
*blk
)
1388 BlockDriverState
*bs
= blk_bs(blk
);
1395 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
1400 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
1401 BlockCompletionFunc
*cb
, void *opaque
)
1403 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
1406 int coroutine_fn
blk_co_write_zeroes(BlockBackend
*blk
, int64_t offset
,
1407 int count
, BdrvRequestFlags flags
)
1409 return blk_co_pwritev(blk
, offset
, count
, NULL
,
1410 flags
| BDRV_REQ_ZERO_WRITE
);
1413 int blk_write_compressed(BlockBackend
*blk
, int64_t sector_num
,
1414 const uint8_t *buf
, int nb_sectors
)
1416 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1421 return bdrv_write_compressed(blk_bs(blk
), sector_num
, buf
, nb_sectors
);
1424 int blk_truncate(BlockBackend
*blk
, int64_t offset
)
1426 if (!blk_is_available(blk
)) {
1430 return bdrv_truncate(blk_bs(blk
), offset
);
1433 int blk_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
1435 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1440 return bdrv_discard(blk_bs(blk
), sector_num
, nb_sectors
);
1443 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
1444 int64_t pos
, int size
)
1448 if (!blk_is_available(blk
)) {
1452 ret
= bdrv_save_vmstate(blk_bs(blk
), buf
, pos
, size
);
1457 if (ret
== size
&& !blk
->enable_write_cache
) {
1458 ret
= bdrv_flush(blk_bs(blk
));
1461 return ret
< 0 ? ret
: size
;
1464 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
1466 if (!blk_is_available(blk
)) {
1470 return bdrv_load_vmstate(blk_bs(blk
), buf
, pos
, size
);
1473 int blk_probe_blocksizes(BlockBackend
*blk
, BlockSizes
*bsz
)
1475 if (!blk_is_available(blk
)) {
1479 return bdrv_probe_blocksizes(blk_bs(blk
), bsz
);
1482 int blk_probe_geometry(BlockBackend
*blk
, HDGeometry
*geo
)
1484 if (!blk_is_available(blk
)) {
1488 return bdrv_probe_geometry(blk_bs(blk
), geo
);
1492 * Updates the BlockBackendRootState object with data from the currently
1493 * attached BlockDriverState.
1495 void blk_update_root_state(BlockBackend
*blk
)
1499 blk
->root_state
.open_flags
= blk
->root
->bs
->open_flags
;
1500 blk
->root_state
.read_only
= blk
->root
->bs
->read_only
;
1501 blk
->root_state
.detect_zeroes
= blk
->root
->bs
->detect_zeroes
;
1503 if (blk
->root_state
.throttle_group
) {
1504 g_free(blk
->root_state
.throttle_group
);
1505 throttle_group_unref(blk
->root_state
.throttle_state
);
1507 if (blk
->root
->bs
->throttle_state
) {
1508 const char *name
= throttle_group_get_name(blk
->root
->bs
);
1509 blk
->root_state
.throttle_group
= g_strdup(name
);
1510 blk
->root_state
.throttle_state
= throttle_group_incref(name
);
1512 blk
->root_state
.throttle_group
= NULL
;
1513 blk
->root_state
.throttle_state
= NULL
;
1518 * Applies the information in the root state to the given BlockDriverState. This
1519 * does not include the flags which have to be specified for bdrv_open(), use
1520 * blk_get_open_flags_from_root_state() to inquire them.
1522 void blk_apply_root_state(BlockBackend
*blk
, BlockDriverState
*bs
)
1524 bs
->detect_zeroes
= blk
->root_state
.detect_zeroes
;
1525 if (blk
->root_state
.throttle_group
) {
1526 bdrv_io_limits_enable(bs
, blk
->root_state
.throttle_group
);
1531 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1532 * supposed to inherit the root state.
1534 int blk_get_open_flags_from_root_state(BlockBackend
*blk
)
1538 bs_flags
= blk
->root_state
.read_only
? 0 : BDRV_O_RDWR
;
1539 bs_flags
|= blk
->root_state
.open_flags
& ~BDRV_O_RDWR
;
1544 BlockBackendRootState
*blk_get_root_state(BlockBackend
*blk
)
1546 return &blk
->root_state
;
1549 int blk_commit_all(void)
1551 BlockBackend
*blk
= NULL
;
1553 while ((blk
= blk_all_next(blk
)) != NULL
) {
1554 AioContext
*aio_context
= blk_get_aio_context(blk
);
1556 aio_context_acquire(aio_context
);
1557 if (blk_is_inserted(blk
) && blk
->root
->bs
->backing
) {
1558 int ret
= bdrv_commit(blk
->root
->bs
);
1560 aio_context_release(aio_context
);
1564 aio_context_release(aio_context
);
1569 int blk_flush_all(void)
1571 BlockBackend
*blk
= NULL
;
1574 while ((blk
= blk_all_next(blk
)) != NULL
) {
1575 AioContext
*aio_context
= blk_get_aio_context(blk
);
1578 aio_context_acquire(aio_context
);
1579 if (blk_is_inserted(blk
)) {
1580 ret
= blk_flush(blk
);
1581 if (ret
< 0 && !result
) {
1585 aio_context_release(aio_context
);