4 * Copyright (C) 2014 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"
22 /* Number of coroutines to reserve per attached device model */
23 #define COROUTINE_POOL_RESERVATION 64
25 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
);
31 DriveInfo
*legacy_dinfo
; /* null unless created by drive_new() */
32 QTAILQ_ENTRY(BlockBackend
) link
; /* for blk_backends */
34 void *dev
; /* attached device model, if any */
35 /* TODO change to DeviceState when all users are qdevified */
36 const BlockDevOps
*dev_ops
;
39 /* the block size for which the guest device expects atomicity */
42 /* If the BDS tree is removed, some of its options are stored here (which
43 * can be used to restore those options in the new BDS on insert) */
44 BlockBackendRootState root_state
;
46 /* I/O stats (display with "info blockstats"). */
49 BlockdevOnError on_read_error
, on_write_error
;
50 bool iostatus_enabled
;
51 BlockDeviceIoStatus iostatus
;
53 NotifierList remove_bs_notifiers
, insert_bs_notifiers
;
56 typedef struct BlockBackendAIOCB
{
63 static const AIOCBInfo block_backend_aiocb_info
= {
64 .get_aio_context
= blk_aiocb_get_aio_context
,
65 .aiocb_size
= sizeof(BlockBackendAIOCB
),
68 static void drive_info_del(DriveInfo
*dinfo
);
70 /* All the BlockBackends (except for hidden ones) */
71 static QTAILQ_HEAD(, BlockBackend
) blk_backends
=
72 QTAILQ_HEAD_INITIALIZER(blk_backends
);
75 * Create a new BlockBackend with @name, with a reference count of one.
76 * @name must not be null or empty.
77 * Fail if a BlockBackend with this name already exists.
78 * Store an error through @errp on failure, unless it's null.
79 * Return the new BlockBackend on success, null on failure.
81 BlockBackend
*blk_new(const char *name
, Error
**errp
)
85 assert(name
&& name
[0]);
86 if (!id_wellformed(name
)) {
87 error_setg(errp
, "Invalid device name");
90 if (blk_by_name(name
)) {
91 error_setg(errp
, "Device with id '%s' already exists", name
);
94 if (bdrv_find_node(name
)) {
96 "Device name '%s' conflicts with an existing node name",
101 blk
= g_new0(BlockBackend
, 1);
102 blk
->name
= g_strdup(name
);
104 notifier_list_init(&blk
->remove_bs_notifiers
);
105 notifier_list_init(&blk
->insert_bs_notifiers
);
106 QTAILQ_INSERT_TAIL(&blk_backends
, blk
, link
);
111 * Create a new BlockBackend with a new BlockDriverState attached.
112 * Otherwise just like blk_new(), which see.
114 BlockBackend
*blk_new_with_bs(const char *name
, Error
**errp
)
117 BlockDriverState
*bs
;
119 blk
= blk_new(name
, errp
);
124 bs
= bdrv_new_root();
131 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
133 * Just as with bdrv_open(), after having called this function the reference to
134 * @options belongs to the block layer (even on failure).
136 * TODO: Remove @filename and @flags; it should be possible to specify a whole
137 * BDS tree just by specifying the @options QDict (or @reference,
138 * alternatively). At the time of adding this function, this is not possible,
139 * though, so callers of this function have to be able to specify @filename and
142 BlockBackend
*blk_new_open(const char *name
, const char *filename
,
143 const char *reference
, QDict
*options
, int flags
,
149 blk
= blk_new_with_bs(name
, errp
);
155 ret
= bdrv_open(&blk
->bs
, filename
, reference
, options
, flags
, errp
);
164 static void blk_delete(BlockBackend
*blk
)
166 assert(!blk
->refcnt
);
169 assert(blk
->bs
->blk
== blk
);
174 assert(QLIST_EMPTY(&blk
->remove_bs_notifiers
.notifiers
));
175 assert(QLIST_EMPTY(&blk
->insert_bs_notifiers
.notifiers
));
176 if (blk
->root_state
.throttle_state
) {
177 g_free(blk
->root_state
.throttle_group
);
178 throttle_group_unref(blk
->root_state
.throttle_state
);
180 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
182 QTAILQ_REMOVE(&blk_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 * Return the BlockBackend after @blk.
231 * If @blk is null, return the first one.
232 * Else, return @blk's next sibling, which may be null.
234 * To iterate over all BlockBackends, do
235 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
239 BlockBackend
*blk_next(BlockBackend
*blk
)
241 return blk
? QTAILQ_NEXT(blk
, link
) : QTAILQ_FIRST(&blk_backends
);
245 * Return @blk's name, a non-null string.
246 * Wart: the name is empty iff @blk has been hidden with
247 * blk_hide_on_behalf_of_hmp_drive_del().
249 const char *blk_name(BlockBackend
*blk
)
255 * Return the BlockBackend with name @name if it exists, else null.
256 * @name must not be null.
258 BlockBackend
*blk_by_name(const char *name
)
263 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
264 if (!strcmp(name
, blk
->name
)) {
272 * Return the BlockDriverState attached to @blk if any, else null.
274 BlockDriverState
*blk_bs(BlockBackend
*blk
)
280 * Changes the BlockDriverState attached to @blk
282 void blk_set_bs(BlockBackend
*blk
, BlockDriverState
*bs
)
290 assert(bs
->blk
== NULL
);
297 * Return @blk's DriveInfo if any, else null.
299 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
301 return blk
->legacy_dinfo
;
305 * Set @blk's DriveInfo to @dinfo, and return it.
306 * @blk must not have a DriveInfo set already.
307 * No other BlockBackend may have the same DriveInfo set.
309 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
311 assert(!blk
->legacy_dinfo
);
312 return blk
->legacy_dinfo
= dinfo
;
316 * Return the BlockBackend with DriveInfo @dinfo.
319 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
323 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
324 if (blk
->legacy_dinfo
== dinfo
) {
333 * @blk must not have been hidden already.
334 * Make attached BlockDriverState, if any, anonymous.
335 * Once hidden, @blk is invisible to all functions that don't receive
336 * it as argument. For example, blk_by_name() won't return it.
337 * Strictly for use by do_drive_del().
338 * TODO get rid of it!
340 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend
*blk
)
342 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
345 bdrv_make_anon(blk
->bs
);
350 * Disassociates the currently associated BlockDriverState from @blk.
352 void blk_remove_bs(BlockBackend
*blk
)
354 notifier_list_notify(&blk
->remove_bs_notifiers
, blk
);
356 blk_update_root_state(blk
);
364 * Associates a new BlockDriverState with @blk.
366 void blk_insert_bs(BlockBackend
*blk
, BlockDriverState
*bs
)
368 assert(!blk
->bs
&& !bs
->blk
);
373 notifier_list_notify(&blk
->insert_bs_notifiers
, blk
);
377 * Attach device model @dev to @blk.
378 * Return 0 on success, -EBUSY when a device model is attached already.
380 int blk_attach_dev(BlockBackend
*blk
, void *dev
)
381 /* TODO change to DeviceState *dev when all users are qdevified */
388 blk_iostatus_reset(blk
);
393 * Attach device model @dev to @blk.
394 * @blk must not have a device model attached already.
395 * TODO qdevified devices don't use this, remove when devices are qdevified
397 void blk_attach_dev_nofail(BlockBackend
*blk
, void *dev
)
399 if (blk_attach_dev(blk
, dev
) < 0) {
405 * Detach device model @dev from @blk.
406 * @dev must be currently attached to @blk.
408 void blk_detach_dev(BlockBackend
*blk
, void *dev
)
409 /* TODO change to DeviceState *dev when all users are qdevified */
411 assert(blk
->dev
== dev
);
414 blk
->dev_opaque
= NULL
;
415 blk
->guest_block_size
= 512;
420 * Return the device model attached to @blk if any, else null.
422 void *blk_get_attached_dev(BlockBackend
*blk
)
423 /* TODO change to return DeviceState * when all users are qdevified */
429 * Set @blk's device model callbacks to @ops.
430 * @opaque is the opaque argument to pass to the callbacks.
431 * This is for use by device models.
433 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
437 blk
->dev_opaque
= opaque
;
441 * Notify @blk's attached device model of media change.
442 * If @load is true, notify of media load.
443 * Else, notify of media eject.
444 * Also send DEVICE_TRAY_MOVED events as appropriate.
446 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
)
448 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
449 bool tray_was_open
, tray_is_open
;
451 tray_was_open
= blk_dev_is_tray_open(blk
);
452 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
);
453 tray_is_open
= blk_dev_is_tray_open(blk
);
455 if (tray_was_open
!= tray_is_open
) {
456 qapi_event_send_device_tray_moved(blk_name(blk
), tray_is_open
,
463 * Does @blk's attached device model have removable media?
464 * %true if no device model is attached.
466 bool blk_dev_has_removable_media(BlockBackend
*blk
)
468 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
472 * Does @blk's attached device model have a tray?
474 bool blk_dev_has_tray(BlockBackend
*blk
)
476 return blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
;
480 * Notify @blk's attached device model of a media eject request.
481 * If @force is true, the medium is about to be yanked out forcefully.
483 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
485 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
486 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
491 * Does @blk's attached device model have a tray, and is it open?
493 bool blk_dev_is_tray_open(BlockBackend
*blk
)
495 if (blk_dev_has_tray(blk
)) {
496 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
502 * Does @blk's attached device model have the medium locked?
503 * %false if the device model has no such lock.
505 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
507 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
508 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
514 * Notify @blk's attached device model of a backend size change.
516 void blk_dev_resize_cb(BlockBackend
*blk
)
518 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
519 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
523 void blk_iostatus_enable(BlockBackend
*blk
)
525 blk
->iostatus_enabled
= true;
526 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
529 /* The I/O status is only enabled if the drive explicitly
530 * enables it _and_ the VM is configured to stop on errors */
531 bool blk_iostatus_is_enabled(const BlockBackend
*blk
)
533 return (blk
->iostatus_enabled
&&
534 (blk
->on_write_error
== BLOCKDEV_ON_ERROR_ENOSPC
||
535 blk
->on_write_error
== BLOCKDEV_ON_ERROR_STOP
||
536 blk
->on_read_error
== BLOCKDEV_ON_ERROR_STOP
));
539 BlockDeviceIoStatus
blk_iostatus(const BlockBackend
*blk
)
541 return blk
->iostatus
;
544 void blk_iostatus_disable(BlockBackend
*blk
)
546 blk
->iostatus_enabled
= false;
549 void blk_iostatus_reset(BlockBackend
*blk
)
551 if (blk_iostatus_is_enabled(blk
)) {
552 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
553 if (blk
->bs
&& blk
->bs
->job
) {
554 block_job_iostatus_reset(blk
->bs
->job
);
559 void blk_iostatus_set_err(BlockBackend
*blk
, int error
)
561 assert(blk_iostatus_is_enabled(blk
));
562 if (blk
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
563 blk
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
564 BLOCK_DEVICE_IO_STATUS_FAILED
;
568 static int blk_check_byte_request(BlockBackend
*blk
, int64_t offset
,
573 if (size
> INT_MAX
) {
577 if (!blk_is_available(blk
)) {
581 len
= blk_getlength(blk
);
590 if (offset
> len
|| len
- offset
< size
) {
597 static int blk_check_request(BlockBackend
*blk
, int64_t sector_num
,
600 if (sector_num
< 0 || sector_num
> INT64_MAX
/ BDRV_SECTOR_SIZE
) {
604 if (nb_sectors
< 0 || nb_sectors
> INT_MAX
/ BDRV_SECTOR_SIZE
) {
608 return blk_check_byte_request(blk
, sector_num
* BDRV_SECTOR_SIZE
,
609 nb_sectors
* BDRV_SECTOR_SIZE
);
612 int blk_read(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
615 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
620 return bdrv_read(blk
->bs
, sector_num
, buf
, nb_sectors
);
623 int blk_read_unthrottled(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
626 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
631 return bdrv_read_unthrottled(blk
->bs
, sector_num
, buf
, nb_sectors
);
634 int blk_write(BlockBackend
*blk
, int64_t sector_num
, const uint8_t *buf
,
637 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
642 return bdrv_write(blk
->bs
, sector_num
, buf
, nb_sectors
);
645 int blk_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
646 int nb_sectors
, BdrvRequestFlags flags
)
648 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
653 return bdrv_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
656 static void error_callback_bh(void *opaque
)
658 struct BlockBackendAIOCB
*acb
= opaque
;
659 qemu_bh_delete(acb
->bh
);
660 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
664 BlockAIOCB
*blk_abort_aio_request(BlockBackend
*blk
,
665 BlockCompletionFunc
*cb
,
666 void *opaque
, int ret
)
668 struct BlockBackendAIOCB
*acb
;
671 acb
= blk_aio_get(&block_backend_aiocb_info
, blk
, cb
, opaque
);
675 bh
= aio_bh_new(blk_get_aio_context(blk
), error_callback_bh
, acb
);
677 qemu_bh_schedule(bh
);
682 BlockAIOCB
*blk_aio_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
683 int nb_sectors
, BdrvRequestFlags flags
,
684 BlockCompletionFunc
*cb
, void *opaque
)
686 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
688 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
691 return bdrv_aio_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
,
695 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int count
)
697 int ret
= blk_check_byte_request(blk
, offset
, count
);
702 return bdrv_pread(blk
->bs
, offset
, buf
, count
);
705 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int count
)
707 int ret
= blk_check_byte_request(blk
, offset
, count
);
712 return bdrv_pwrite(blk
->bs
, offset
, buf
, count
);
715 int64_t blk_getlength(BlockBackend
*blk
)
717 if (!blk_is_available(blk
)) {
721 return bdrv_getlength(blk
->bs
);
724 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
729 bdrv_get_geometry(blk
->bs
, nb_sectors_ptr
);
733 int64_t blk_nb_sectors(BlockBackend
*blk
)
735 if (!blk_is_available(blk
)) {
739 return bdrv_nb_sectors(blk
->bs
);
742 BlockAIOCB
*blk_aio_readv(BlockBackend
*blk
, int64_t sector_num
,
743 QEMUIOVector
*iov
, int nb_sectors
,
744 BlockCompletionFunc
*cb
, void *opaque
)
746 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
748 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
751 return bdrv_aio_readv(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
754 BlockAIOCB
*blk_aio_writev(BlockBackend
*blk
, int64_t sector_num
,
755 QEMUIOVector
*iov
, int nb_sectors
,
756 BlockCompletionFunc
*cb
, void *opaque
)
758 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
760 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
763 return bdrv_aio_writev(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
766 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
767 BlockCompletionFunc
*cb
, void *opaque
)
769 if (!blk_is_available(blk
)) {
770 return blk_abort_aio_request(blk
, cb
, opaque
, -ENOMEDIUM
);
773 return bdrv_aio_flush(blk
->bs
, cb
, opaque
);
776 BlockAIOCB
*blk_aio_discard(BlockBackend
*blk
,
777 int64_t sector_num
, int nb_sectors
,
778 BlockCompletionFunc
*cb
, void *opaque
)
780 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
782 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
785 return bdrv_aio_discard(blk
->bs
, sector_num
, nb_sectors
, cb
, opaque
);
788 void blk_aio_cancel(BlockAIOCB
*acb
)
790 bdrv_aio_cancel(acb
);
793 void blk_aio_cancel_async(BlockAIOCB
*acb
)
795 bdrv_aio_cancel_async(acb
);
798 int blk_aio_multiwrite(BlockBackend
*blk
, BlockRequest
*reqs
, int num_reqs
)
802 for (i
= 0; i
< num_reqs
; i
++) {
803 ret
= blk_check_request(blk
, reqs
[i
].sector
, reqs
[i
].nb_sectors
);
809 return bdrv_aio_multiwrite(blk
->bs
, reqs
, num_reqs
);
812 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
814 if (!blk_is_available(blk
)) {
818 return bdrv_ioctl(blk
->bs
, req
, buf
);
821 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
822 BlockCompletionFunc
*cb
, void *opaque
)
824 if (!blk_is_available(blk
)) {
825 return blk_abort_aio_request(blk
, cb
, opaque
, -ENOMEDIUM
);
828 return bdrv_aio_ioctl(blk
->bs
, req
, buf
, cb
, opaque
);
831 int blk_co_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
833 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
838 return bdrv_co_discard(blk
->bs
, sector_num
, nb_sectors
);
841 int blk_co_flush(BlockBackend
*blk
)
843 if (!blk_is_available(blk
)) {
847 return bdrv_co_flush(blk
->bs
);
850 int blk_flush(BlockBackend
*blk
)
852 if (!blk_is_available(blk
)) {
856 return bdrv_flush(blk
->bs
);
859 int blk_flush_all(void)
861 return bdrv_flush_all();
864 void blk_drain(BlockBackend
*blk
)
871 void blk_drain_all(void)
876 void blk_set_on_error(BlockBackend
*blk
, BlockdevOnError on_read_error
,
877 BlockdevOnError on_write_error
)
879 blk
->on_read_error
= on_read_error
;
880 blk
->on_write_error
= on_write_error
;
883 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
885 return is_read
? blk
->on_read_error
: blk
->on_write_error
;
888 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
891 BlockdevOnError on_err
= blk_get_on_error(blk
, is_read
);
894 case BLOCKDEV_ON_ERROR_ENOSPC
:
895 return (error
== ENOSPC
) ?
896 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
897 case BLOCKDEV_ON_ERROR_STOP
:
898 return BLOCK_ERROR_ACTION_STOP
;
899 case BLOCKDEV_ON_ERROR_REPORT
:
900 return BLOCK_ERROR_ACTION_REPORT
;
901 case BLOCKDEV_ON_ERROR_IGNORE
:
902 return BLOCK_ERROR_ACTION_IGNORE
;
908 static void send_qmp_error_event(BlockBackend
*blk
,
909 BlockErrorAction action
,
910 bool is_read
, int error
)
912 IoOperationType optype
;
914 optype
= is_read
? IO_OPERATION_TYPE_READ
: IO_OPERATION_TYPE_WRITE
;
915 qapi_event_send_block_io_error(blk_name(blk
), optype
, action
,
916 blk_iostatus_is_enabled(blk
),
917 error
== ENOSPC
, strerror(error
),
921 /* This is done by device models because, while the block layer knows
922 * about the error, it does not know whether an operation comes from
923 * the device or the block layer (from a job, for example).
925 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
926 bool is_read
, int error
)
930 if (action
== BLOCK_ERROR_ACTION_STOP
) {
931 /* First set the iostatus, so that "info block" returns an iostatus
932 * that matches the events raised so far (an additional error iostatus
933 * is fine, but not a lost one).
935 blk_iostatus_set_err(blk
, error
);
937 /* Then raise the request to stop the VM and the event.
938 * qemu_system_vmstop_request_prepare has two effects. First,
939 * it ensures that the STOP event always comes after the
940 * BLOCK_IO_ERROR event. Second, it ensures that even if management
941 * can observe the STOP event and do a "cont" before the STOP
942 * event is issued, the VM will not stop. In this case, vm_start()
943 * also ensures that the STOP/RESUME pair of events is emitted.
945 qemu_system_vmstop_request_prepare();
946 send_qmp_error_event(blk
, action
, is_read
, error
);
947 qemu_system_vmstop_request(RUN_STATE_IO_ERROR
);
949 send_qmp_error_event(blk
, action
, is_read
, error
);
953 int blk_is_read_only(BlockBackend
*blk
)
956 return bdrv_is_read_only(blk
->bs
);
958 return blk
->root_state
.read_only
;
962 int blk_is_sg(BlockBackend
*blk
)
968 return bdrv_is_sg(blk
->bs
);
971 int blk_enable_write_cache(BlockBackend
*blk
)
974 return bdrv_enable_write_cache(blk
->bs
);
976 return !!(blk
->root_state
.open_flags
& BDRV_O_CACHE_WB
);
980 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
983 bdrv_set_enable_write_cache(blk
->bs
, wce
);
986 blk
->root_state
.open_flags
|= BDRV_O_CACHE_WB
;
988 blk
->root_state
.open_flags
&= ~BDRV_O_CACHE_WB
;
993 void blk_invalidate_cache(BlockBackend
*blk
, Error
**errp
)
996 error_setg(errp
, "Device '%s' has no medium", blk
->name
);
1000 bdrv_invalidate_cache(blk
->bs
, errp
);
1003 bool blk_is_inserted(BlockBackend
*blk
)
1005 return blk
->bs
&& bdrv_is_inserted(blk
->bs
);
1008 bool blk_is_available(BlockBackend
*blk
)
1010 return blk_is_inserted(blk
) && !blk_dev_is_tray_open(blk
);
1013 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
1016 bdrv_lock_medium(blk
->bs
, locked
);
1020 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
1023 bdrv_eject(blk
->bs
, eject_flag
);
1027 int blk_get_flags(BlockBackend
*blk
)
1030 return bdrv_get_flags(blk
->bs
);
1032 return blk
->root_state
.open_flags
;
1036 int blk_get_max_transfer_length(BlockBackend
*blk
)
1039 return blk
->bs
->bl
.max_transfer_length
;
1045 int blk_get_max_iov(BlockBackend
*blk
)
1047 return blk
->bs
->bl
.max_iov
;
1050 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
1052 blk
->guest_block_size
= align
;
1055 void *blk_try_blockalign(BlockBackend
*blk
, size_t size
)
1057 return qemu_try_blockalign(blk
? blk
->bs
: NULL
, size
);
1060 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
1062 return qemu_blockalign(blk
? blk
->bs
: NULL
, size
);
1065 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
1071 return bdrv_op_is_blocked(blk
->bs
, op
, errp
);
1074 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
1077 bdrv_op_unblock(blk
->bs
, op
, reason
);
1081 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
1084 bdrv_op_block_all(blk
->bs
, reason
);
1088 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
1091 bdrv_op_unblock_all(blk
->bs
, reason
);
1095 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
1098 return bdrv_get_aio_context(blk
->bs
);
1100 return qemu_get_aio_context();
1104 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
)
1106 BlockBackendAIOCB
*blk_acb
= DO_UPCAST(BlockBackendAIOCB
, common
, acb
);
1107 return blk_get_aio_context(blk_acb
->blk
);
1110 void blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
)
1113 bdrv_set_aio_context(blk
->bs
, new_context
);
1117 void blk_add_aio_context_notifier(BlockBackend
*blk
,
1118 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
1119 void (*detach_aio_context
)(void *opaque
), void *opaque
)
1122 bdrv_add_aio_context_notifier(blk
->bs
, attached_aio_context
,
1123 detach_aio_context
, opaque
);
1127 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
1128 void (*attached_aio_context
)(AioContext
*,
1130 void (*detach_aio_context
)(void *),
1134 bdrv_remove_aio_context_notifier(blk
->bs
, attached_aio_context
,
1135 detach_aio_context
, opaque
);
1139 void blk_add_remove_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
1141 notifier_list_add(&blk
->remove_bs_notifiers
, notify
);
1144 void blk_add_insert_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
1146 notifier_list_add(&blk
->insert_bs_notifiers
, notify
);
1149 void blk_io_plug(BlockBackend
*blk
)
1152 bdrv_io_plug(blk
->bs
);
1156 void blk_io_unplug(BlockBackend
*blk
)
1159 bdrv_io_unplug(blk
->bs
);
1163 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
1168 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
1169 BlockCompletionFunc
*cb
, void *opaque
)
1171 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
1174 int coroutine_fn
blk_co_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
1175 int nb_sectors
, BdrvRequestFlags flags
)
1177 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1182 return bdrv_co_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
1185 int blk_write_compressed(BlockBackend
*blk
, int64_t sector_num
,
1186 const uint8_t *buf
, int nb_sectors
)
1188 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1193 return bdrv_write_compressed(blk
->bs
, sector_num
, buf
, nb_sectors
);
1196 int blk_truncate(BlockBackend
*blk
, int64_t offset
)
1198 if (!blk_is_available(blk
)) {
1202 return bdrv_truncate(blk
->bs
, offset
);
1205 int blk_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
1207 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1212 return bdrv_discard(blk
->bs
, sector_num
, nb_sectors
);
1215 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
1216 int64_t pos
, int size
)
1218 if (!blk_is_available(blk
)) {
1222 return bdrv_save_vmstate(blk
->bs
, buf
, pos
, size
);
1225 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
1227 if (!blk_is_available(blk
)) {
1231 return bdrv_load_vmstate(blk
->bs
, buf
, pos
, size
);
1234 int blk_probe_blocksizes(BlockBackend
*blk
, BlockSizes
*bsz
)
1236 if (!blk_is_available(blk
)) {
1240 return bdrv_probe_blocksizes(blk
->bs
, bsz
);
1243 int blk_probe_geometry(BlockBackend
*blk
, HDGeometry
*geo
)
1245 if (!blk_is_available(blk
)) {
1249 return bdrv_probe_geometry(blk
->bs
, geo
);
1253 * Updates the BlockBackendRootState object with data from the currently
1254 * attached BlockDriverState.
1256 void blk_update_root_state(BlockBackend
*blk
)
1260 blk
->root_state
.open_flags
= blk
->bs
->open_flags
;
1261 blk
->root_state
.read_only
= blk
->bs
->read_only
;
1262 blk
->root_state
.detect_zeroes
= blk
->bs
->detect_zeroes
;
1264 if (blk
->root_state
.throttle_group
) {
1265 g_free(blk
->root_state
.throttle_group
);
1266 throttle_group_unref(blk
->root_state
.throttle_state
);
1268 if (blk
->bs
->throttle_state
) {
1269 const char *name
= throttle_group_get_name(blk
->bs
);
1270 blk
->root_state
.throttle_group
= g_strdup(name
);
1271 blk
->root_state
.throttle_state
= throttle_group_incref(name
);
1273 blk
->root_state
.throttle_group
= NULL
;
1274 blk
->root_state
.throttle_state
= NULL
;
1279 * Applies the information in the root state to the given BlockDriverState. This
1280 * does not include the flags which have to be specified for bdrv_open(), use
1281 * blk_get_open_flags_from_root_state() to inquire them.
1283 void blk_apply_root_state(BlockBackend
*blk
, BlockDriverState
*bs
)
1285 bs
->detect_zeroes
= blk
->root_state
.detect_zeroes
;
1286 if (blk
->root_state
.throttle_group
) {
1287 bdrv_io_limits_enable(bs
, blk
->root_state
.throttle_group
);
1292 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1293 * supposed to inherit the root state.
1295 int blk_get_open_flags_from_root_state(BlockBackend
*blk
)
1299 bs_flags
= blk
->root_state
.read_only
? 0 : BDRV_O_RDWR
;
1300 bs_flags
|= blk
->root_state
.open_flags
& ~BDRV_O_RDWR
;
1305 BlockBackendRootState
*blk_get_root_state(BlockBackend
*blk
)
1307 return &blk
->root_state
;