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 bool allow_write_beyond_eof
;
55 NotifierList remove_bs_notifiers
, insert_bs_notifiers
;
58 typedef struct BlockBackendAIOCB
{
65 static const AIOCBInfo block_backend_aiocb_info
= {
66 .get_aio_context
= blk_aiocb_get_aio_context
,
67 .aiocb_size
= sizeof(BlockBackendAIOCB
),
70 static void drive_info_del(DriveInfo
*dinfo
);
72 /* All the BlockBackends (except for hidden ones) */
73 static QTAILQ_HEAD(, BlockBackend
) blk_backends
=
74 QTAILQ_HEAD_INITIALIZER(blk_backends
);
77 * Create a new BlockBackend with @name, with a reference count of one.
78 * @name must not be null or empty.
79 * Fail if a BlockBackend with this name already exists.
80 * Store an error through @errp on failure, unless it's null.
81 * Return the new BlockBackend on success, null on failure.
83 BlockBackend
*blk_new(const char *name
, Error
**errp
)
87 assert(name
&& name
[0]);
88 if (!id_wellformed(name
)) {
89 error_setg(errp
, "Invalid device name");
92 if (blk_by_name(name
)) {
93 error_setg(errp
, "Device with id '%s' already exists", name
);
96 if (bdrv_find_node(name
)) {
98 "Device name '%s' conflicts with an existing node name",
103 blk
= g_new0(BlockBackend
, 1);
104 blk
->name
= g_strdup(name
);
106 notifier_list_init(&blk
->remove_bs_notifiers
);
107 notifier_list_init(&blk
->insert_bs_notifiers
);
108 QTAILQ_INSERT_TAIL(&blk_backends
, blk
, link
);
113 * Create a new BlockBackend with a new BlockDriverState attached.
114 * Otherwise just like blk_new(), which see.
116 BlockBackend
*blk_new_with_bs(const char *name
, Error
**errp
)
119 BlockDriverState
*bs
;
121 blk
= blk_new(name
, errp
);
126 bs
= bdrv_new_root();
133 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
135 * Just as with bdrv_open(), after having called this function the reference to
136 * @options belongs to the block layer (even on failure).
138 * TODO: Remove @filename and @flags; it should be possible to specify a whole
139 * BDS tree just by specifying the @options QDict (or @reference,
140 * alternatively). At the time of adding this function, this is not possible,
141 * though, so callers of this function have to be able to specify @filename and
144 BlockBackend
*blk_new_open(const char *name
, const char *filename
,
145 const char *reference
, QDict
*options
, int flags
,
151 blk
= blk_new_with_bs(name
, errp
);
157 ret
= bdrv_open(&blk
->bs
, filename
, reference
, options
, flags
, errp
);
166 static void blk_delete(BlockBackend
*blk
)
168 assert(!blk
->refcnt
);
173 assert(QLIST_EMPTY(&blk
->remove_bs_notifiers
.notifiers
));
174 assert(QLIST_EMPTY(&blk
->insert_bs_notifiers
.notifiers
));
175 if (blk
->root_state
.throttle_state
) {
176 g_free(blk
->root_state
.throttle_group
);
177 throttle_group_unref(blk
->root_state
.throttle_state
);
179 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
181 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
184 drive_info_del(blk
->legacy_dinfo
);
185 block_acct_cleanup(&blk
->stats
);
189 static void drive_info_del(DriveInfo
*dinfo
)
194 qemu_opts_del(dinfo
->opts
);
195 g_free(dinfo
->serial
);
199 int blk_get_refcnt(BlockBackend
*blk
)
201 return blk
? blk
->refcnt
: 0;
205 * Increment @blk's reference count.
206 * @blk must not be null.
208 void blk_ref(BlockBackend
*blk
)
214 * Decrement @blk's reference count.
215 * If this drops it to zero, destroy @blk.
216 * For convenience, do nothing if @blk is null.
218 void blk_unref(BlockBackend
*blk
)
221 assert(blk
->refcnt
> 0);
222 if (!--blk
->refcnt
) {
228 void blk_remove_all_bs(void)
232 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
233 AioContext
*ctx
= blk_get_aio_context(blk
);
235 aio_context_acquire(ctx
);
239 aio_context_release(ctx
);
244 * Return the BlockBackend after @blk.
245 * If @blk is null, return the first one.
246 * Else, return @blk's next sibling, which may be null.
248 * To iterate over all BlockBackends, do
249 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
253 BlockBackend
*blk_next(BlockBackend
*blk
)
255 return blk
? QTAILQ_NEXT(blk
, link
) : QTAILQ_FIRST(&blk_backends
);
259 * Return @blk's name, a non-null string.
260 * Wart: the name is empty iff @blk has been hidden with
261 * blk_hide_on_behalf_of_hmp_drive_del().
263 const char *blk_name(BlockBackend
*blk
)
269 * Return the BlockBackend with name @name if it exists, else null.
270 * @name must not be null.
272 BlockBackend
*blk_by_name(const char *name
)
277 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
278 if (!strcmp(name
, blk
->name
)) {
286 * Return the BlockDriverState attached to @blk if any, else null.
288 BlockDriverState
*blk_bs(BlockBackend
*blk
)
294 * Changes the BlockDriverState attached to @blk
296 void blk_set_bs(BlockBackend
*blk
, BlockDriverState
*bs
)
304 assert(bs
->blk
== NULL
);
311 * Return @blk's DriveInfo if any, else null.
313 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
315 return blk
->legacy_dinfo
;
319 * Set @blk's DriveInfo to @dinfo, and return it.
320 * @blk must not have a DriveInfo set already.
321 * No other BlockBackend may have the same DriveInfo set.
323 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
325 assert(!blk
->legacy_dinfo
);
326 return blk
->legacy_dinfo
= dinfo
;
330 * Return the BlockBackend with DriveInfo @dinfo.
333 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
337 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
338 if (blk
->legacy_dinfo
== dinfo
) {
347 * @blk must not have been hidden already.
348 * Make attached BlockDriverState, if any, anonymous.
349 * Once hidden, @blk is invisible to all functions that don't receive
350 * it as argument. For example, blk_by_name() won't return it.
351 * Strictly for use by do_drive_del().
352 * TODO get rid of it!
354 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend
*blk
)
356 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
359 bdrv_make_anon(blk
->bs
);
364 * Disassociates the currently associated BlockDriverState from @blk.
366 void blk_remove_bs(BlockBackend
*blk
)
368 assert(blk
->bs
->blk
== blk
);
370 notifier_list_notify(&blk
->remove_bs_notifiers
, blk
);
372 blk_update_root_state(blk
);
380 * Associates a new BlockDriverState with @blk.
382 void blk_insert_bs(BlockBackend
*blk
, BlockDriverState
*bs
)
384 assert(!blk
->bs
&& !bs
->blk
);
389 notifier_list_notify(&blk
->insert_bs_notifiers
, blk
);
393 * Attach device model @dev to @blk.
394 * Return 0 on success, -EBUSY when a device model is attached already.
396 int blk_attach_dev(BlockBackend
*blk
, void *dev
)
397 /* TODO change to DeviceState *dev when all users are qdevified */
404 blk_iostatus_reset(blk
);
409 * Attach device model @dev to @blk.
410 * @blk must not have a device model attached already.
411 * TODO qdevified devices don't use this, remove when devices are qdevified
413 void blk_attach_dev_nofail(BlockBackend
*blk
, void *dev
)
415 if (blk_attach_dev(blk
, dev
) < 0) {
421 * Detach device model @dev from @blk.
422 * @dev must be currently attached to @blk.
424 void blk_detach_dev(BlockBackend
*blk
, void *dev
)
425 /* TODO change to DeviceState *dev when all users are qdevified */
427 assert(blk
->dev
== dev
);
430 blk
->dev_opaque
= NULL
;
431 blk
->guest_block_size
= 512;
436 * Return the device model attached to @blk if any, else null.
438 void *blk_get_attached_dev(BlockBackend
*blk
)
439 /* TODO change to return DeviceState * when all users are qdevified */
445 * Set @blk's device model callbacks to @ops.
446 * @opaque is the opaque argument to pass to the callbacks.
447 * This is for use by device models.
449 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
453 blk
->dev_opaque
= opaque
;
457 * Notify @blk's attached device model of media change.
458 * If @load is true, notify of media load.
459 * Else, notify of media eject.
460 * Also send DEVICE_TRAY_MOVED events as appropriate.
462 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
)
464 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
465 bool tray_was_open
, tray_is_open
;
467 tray_was_open
= blk_dev_is_tray_open(blk
);
468 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
);
469 tray_is_open
= blk_dev_is_tray_open(blk
);
471 if (tray_was_open
!= tray_is_open
) {
472 qapi_event_send_device_tray_moved(blk_name(blk
), tray_is_open
,
479 * Does @blk's attached device model have removable media?
480 * %true if no device model is attached.
482 bool blk_dev_has_removable_media(BlockBackend
*blk
)
484 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
488 * Does @blk's attached device model have a tray?
490 bool blk_dev_has_tray(BlockBackend
*blk
)
492 return blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
;
496 * Notify @blk's attached device model of a media eject request.
497 * If @force is true, the medium is about to be yanked out forcefully.
499 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
501 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
502 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
507 * Does @blk's attached device model have a tray, and is it open?
509 bool blk_dev_is_tray_open(BlockBackend
*blk
)
511 if (blk_dev_has_tray(blk
)) {
512 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
518 * Does @blk's attached device model have the medium locked?
519 * %false if the device model has no such lock.
521 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
523 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
524 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
530 * Notify @blk's attached device model of a backend size change.
532 void blk_dev_resize_cb(BlockBackend
*blk
)
534 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
535 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
539 void blk_iostatus_enable(BlockBackend
*blk
)
541 blk
->iostatus_enabled
= true;
542 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
545 /* The I/O status is only enabled if the drive explicitly
546 * enables it _and_ the VM is configured to stop on errors */
547 bool blk_iostatus_is_enabled(const BlockBackend
*blk
)
549 return (blk
->iostatus_enabled
&&
550 (blk
->on_write_error
== BLOCKDEV_ON_ERROR_ENOSPC
||
551 blk
->on_write_error
== BLOCKDEV_ON_ERROR_STOP
||
552 blk
->on_read_error
== BLOCKDEV_ON_ERROR_STOP
));
555 BlockDeviceIoStatus
blk_iostatus(const BlockBackend
*blk
)
557 return blk
->iostatus
;
560 void blk_iostatus_disable(BlockBackend
*blk
)
562 blk
->iostatus_enabled
= false;
565 void blk_iostatus_reset(BlockBackend
*blk
)
567 if (blk_iostatus_is_enabled(blk
)) {
568 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
569 if (blk
->bs
&& blk
->bs
->job
) {
570 block_job_iostatus_reset(blk
->bs
->job
);
575 void blk_iostatus_set_err(BlockBackend
*blk
, int error
)
577 assert(blk_iostatus_is_enabled(blk
));
578 if (blk
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
579 blk
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
580 BLOCK_DEVICE_IO_STATUS_FAILED
;
584 void blk_set_allow_write_beyond_eof(BlockBackend
*blk
, bool allow
)
586 blk
->allow_write_beyond_eof
= allow
;
589 static int blk_check_byte_request(BlockBackend
*blk
, int64_t offset
,
594 if (size
> INT_MAX
) {
598 if (!blk_is_available(blk
)) {
606 if (!blk
->allow_write_beyond_eof
) {
607 len
= blk_getlength(blk
);
612 if (offset
> len
|| len
- offset
< size
) {
620 static int blk_check_request(BlockBackend
*blk
, int64_t sector_num
,
623 if (sector_num
< 0 || sector_num
> INT64_MAX
/ BDRV_SECTOR_SIZE
) {
627 if (nb_sectors
< 0 || nb_sectors
> INT_MAX
/ BDRV_SECTOR_SIZE
) {
631 return blk_check_byte_request(blk
, sector_num
* BDRV_SECTOR_SIZE
,
632 nb_sectors
* BDRV_SECTOR_SIZE
);
635 int blk_read(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
638 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
643 return bdrv_read(blk
->bs
, sector_num
, buf
, nb_sectors
);
646 int blk_read_unthrottled(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
649 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
654 return bdrv_read_unthrottled(blk
->bs
, sector_num
, buf
, nb_sectors
);
657 int blk_write(BlockBackend
*blk
, int64_t sector_num
, const uint8_t *buf
,
660 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
665 return bdrv_write(blk
->bs
, sector_num
, buf
, nb_sectors
);
668 int blk_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
669 int nb_sectors
, BdrvRequestFlags flags
)
671 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
676 return bdrv_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
679 static void error_callback_bh(void *opaque
)
681 struct BlockBackendAIOCB
*acb
= opaque
;
682 qemu_bh_delete(acb
->bh
);
683 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
687 BlockAIOCB
*blk_abort_aio_request(BlockBackend
*blk
,
688 BlockCompletionFunc
*cb
,
689 void *opaque
, int ret
)
691 struct BlockBackendAIOCB
*acb
;
694 acb
= blk_aio_get(&block_backend_aiocb_info
, blk
, cb
, opaque
);
698 bh
= aio_bh_new(blk_get_aio_context(blk
), error_callback_bh
, acb
);
700 qemu_bh_schedule(bh
);
705 BlockAIOCB
*blk_aio_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
706 int nb_sectors
, BdrvRequestFlags flags
,
707 BlockCompletionFunc
*cb
, void *opaque
)
709 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
711 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
714 return bdrv_aio_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
,
718 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int count
)
720 int ret
= blk_check_byte_request(blk
, offset
, count
);
725 return bdrv_pread(blk
->bs
, offset
, buf
, count
);
728 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int count
)
730 int ret
= blk_check_byte_request(blk
, offset
, count
);
735 return bdrv_pwrite(blk
->bs
, offset
, buf
, count
);
738 int64_t blk_getlength(BlockBackend
*blk
)
740 if (!blk_is_available(blk
)) {
744 return bdrv_getlength(blk
->bs
);
747 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
752 bdrv_get_geometry(blk
->bs
, nb_sectors_ptr
);
756 int64_t blk_nb_sectors(BlockBackend
*blk
)
758 if (!blk_is_available(blk
)) {
762 return bdrv_nb_sectors(blk
->bs
);
765 BlockAIOCB
*blk_aio_readv(BlockBackend
*blk
, int64_t sector_num
,
766 QEMUIOVector
*iov
, int nb_sectors
,
767 BlockCompletionFunc
*cb
, void *opaque
)
769 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
771 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
774 return bdrv_aio_readv(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
777 BlockAIOCB
*blk_aio_writev(BlockBackend
*blk
, int64_t sector_num
,
778 QEMUIOVector
*iov
, int nb_sectors
,
779 BlockCompletionFunc
*cb
, void *opaque
)
781 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
783 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
786 return bdrv_aio_writev(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
789 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
790 BlockCompletionFunc
*cb
, void *opaque
)
792 if (!blk_is_available(blk
)) {
793 return blk_abort_aio_request(blk
, cb
, opaque
, -ENOMEDIUM
);
796 return bdrv_aio_flush(blk
->bs
, cb
, opaque
);
799 BlockAIOCB
*blk_aio_discard(BlockBackend
*blk
,
800 int64_t sector_num
, int nb_sectors
,
801 BlockCompletionFunc
*cb
, void *opaque
)
803 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
805 return blk_abort_aio_request(blk
, cb
, opaque
, ret
);
808 return bdrv_aio_discard(blk
->bs
, sector_num
, nb_sectors
, cb
, opaque
);
811 void blk_aio_cancel(BlockAIOCB
*acb
)
813 bdrv_aio_cancel(acb
);
816 void blk_aio_cancel_async(BlockAIOCB
*acb
)
818 bdrv_aio_cancel_async(acb
);
821 int blk_aio_multiwrite(BlockBackend
*blk
, BlockRequest
*reqs
, int num_reqs
)
825 for (i
= 0; i
< num_reqs
; i
++) {
826 ret
= blk_check_request(blk
, reqs
[i
].sector
, reqs
[i
].nb_sectors
);
832 return bdrv_aio_multiwrite(blk
->bs
, reqs
, num_reqs
);
835 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
837 if (!blk_is_available(blk
)) {
841 return bdrv_ioctl(blk
->bs
, req
, buf
);
844 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
845 BlockCompletionFunc
*cb
, void *opaque
)
847 if (!blk_is_available(blk
)) {
848 return blk_abort_aio_request(blk
, cb
, opaque
, -ENOMEDIUM
);
851 return bdrv_aio_ioctl(blk
->bs
, req
, buf
, cb
, opaque
);
854 int blk_co_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
856 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
861 return bdrv_co_discard(blk
->bs
, sector_num
, nb_sectors
);
864 int blk_co_flush(BlockBackend
*blk
)
866 if (!blk_is_available(blk
)) {
870 return bdrv_co_flush(blk
->bs
);
873 int blk_flush(BlockBackend
*blk
)
875 if (!blk_is_available(blk
)) {
879 return bdrv_flush(blk
->bs
);
882 int blk_flush_all(void)
884 return bdrv_flush_all();
887 void blk_drain(BlockBackend
*blk
)
894 void blk_drain_all(void)
899 void blk_set_on_error(BlockBackend
*blk
, BlockdevOnError on_read_error
,
900 BlockdevOnError on_write_error
)
902 blk
->on_read_error
= on_read_error
;
903 blk
->on_write_error
= on_write_error
;
906 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
908 return is_read
? blk
->on_read_error
: blk
->on_write_error
;
911 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
914 BlockdevOnError on_err
= blk_get_on_error(blk
, is_read
);
917 case BLOCKDEV_ON_ERROR_ENOSPC
:
918 return (error
== ENOSPC
) ?
919 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
920 case BLOCKDEV_ON_ERROR_STOP
:
921 return BLOCK_ERROR_ACTION_STOP
;
922 case BLOCKDEV_ON_ERROR_REPORT
:
923 return BLOCK_ERROR_ACTION_REPORT
;
924 case BLOCKDEV_ON_ERROR_IGNORE
:
925 return BLOCK_ERROR_ACTION_IGNORE
;
931 static void send_qmp_error_event(BlockBackend
*blk
,
932 BlockErrorAction action
,
933 bool is_read
, int error
)
935 IoOperationType optype
;
937 optype
= is_read
? IO_OPERATION_TYPE_READ
: IO_OPERATION_TYPE_WRITE
;
938 qapi_event_send_block_io_error(blk_name(blk
), optype
, action
,
939 blk_iostatus_is_enabled(blk
),
940 error
== ENOSPC
, strerror(error
),
944 /* This is done by device models because, while the block layer knows
945 * about the error, it does not know whether an operation comes from
946 * the device or the block layer (from a job, for example).
948 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
949 bool is_read
, int error
)
953 if (action
== BLOCK_ERROR_ACTION_STOP
) {
954 /* First set the iostatus, so that "info block" returns an iostatus
955 * that matches the events raised so far (an additional error iostatus
956 * is fine, but not a lost one).
958 blk_iostatus_set_err(blk
, error
);
960 /* Then raise the request to stop the VM and the event.
961 * qemu_system_vmstop_request_prepare has two effects. First,
962 * it ensures that the STOP event always comes after the
963 * BLOCK_IO_ERROR event. Second, it ensures that even if management
964 * can observe the STOP event and do a "cont" before the STOP
965 * event is issued, the VM will not stop. In this case, vm_start()
966 * also ensures that the STOP/RESUME pair of events is emitted.
968 qemu_system_vmstop_request_prepare();
969 send_qmp_error_event(blk
, action
, is_read
, error
);
970 qemu_system_vmstop_request(RUN_STATE_IO_ERROR
);
972 send_qmp_error_event(blk
, action
, is_read
, error
);
976 int blk_is_read_only(BlockBackend
*blk
)
979 return bdrv_is_read_only(blk
->bs
);
981 return blk
->root_state
.read_only
;
985 int blk_is_sg(BlockBackend
*blk
)
991 return bdrv_is_sg(blk
->bs
);
994 int blk_enable_write_cache(BlockBackend
*blk
)
997 return bdrv_enable_write_cache(blk
->bs
);
999 return !!(blk
->root_state
.open_flags
& BDRV_O_CACHE_WB
);
1003 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
1006 bdrv_set_enable_write_cache(blk
->bs
, wce
);
1009 blk
->root_state
.open_flags
|= BDRV_O_CACHE_WB
;
1011 blk
->root_state
.open_flags
&= ~BDRV_O_CACHE_WB
;
1016 void blk_invalidate_cache(BlockBackend
*blk
, Error
**errp
)
1019 error_setg(errp
, "Device '%s' has no medium", blk
->name
);
1023 bdrv_invalidate_cache(blk
->bs
, errp
);
1026 bool blk_is_inserted(BlockBackend
*blk
)
1028 return blk
->bs
&& bdrv_is_inserted(blk
->bs
);
1031 bool blk_is_available(BlockBackend
*blk
)
1033 return blk_is_inserted(blk
) && !blk_dev_is_tray_open(blk
);
1036 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
1039 bdrv_lock_medium(blk
->bs
, locked
);
1043 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
1046 bdrv_eject(blk
->bs
, eject_flag
);
1050 int blk_get_flags(BlockBackend
*blk
)
1053 return bdrv_get_flags(blk
->bs
);
1055 return blk
->root_state
.open_flags
;
1059 int blk_get_max_transfer_length(BlockBackend
*blk
)
1062 return blk
->bs
->bl
.max_transfer_length
;
1068 int blk_get_max_iov(BlockBackend
*blk
)
1070 return blk
->bs
->bl
.max_iov
;
1073 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
1075 blk
->guest_block_size
= align
;
1078 void *blk_try_blockalign(BlockBackend
*blk
, size_t size
)
1080 return qemu_try_blockalign(blk
? blk
->bs
: NULL
, size
);
1083 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
1085 return qemu_blockalign(blk
? blk
->bs
: NULL
, size
);
1088 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
1094 return bdrv_op_is_blocked(blk
->bs
, op
, errp
);
1097 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
1100 bdrv_op_unblock(blk
->bs
, op
, reason
);
1104 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
1107 bdrv_op_block_all(blk
->bs
, reason
);
1111 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
1114 bdrv_op_unblock_all(blk
->bs
, reason
);
1118 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
1121 return bdrv_get_aio_context(blk
->bs
);
1123 return qemu_get_aio_context();
1127 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
)
1129 BlockBackendAIOCB
*blk_acb
= DO_UPCAST(BlockBackendAIOCB
, common
, acb
);
1130 return blk_get_aio_context(blk_acb
->blk
);
1133 void blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
)
1136 bdrv_set_aio_context(blk
->bs
, new_context
);
1140 void blk_add_aio_context_notifier(BlockBackend
*blk
,
1141 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
1142 void (*detach_aio_context
)(void *opaque
), void *opaque
)
1145 bdrv_add_aio_context_notifier(blk
->bs
, attached_aio_context
,
1146 detach_aio_context
, opaque
);
1150 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
1151 void (*attached_aio_context
)(AioContext
*,
1153 void (*detach_aio_context
)(void *),
1157 bdrv_remove_aio_context_notifier(blk
->bs
, attached_aio_context
,
1158 detach_aio_context
, opaque
);
1162 void blk_add_remove_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
1164 notifier_list_add(&blk
->remove_bs_notifiers
, notify
);
1167 void blk_add_insert_bs_notifier(BlockBackend
*blk
, Notifier
*notify
)
1169 notifier_list_add(&blk
->insert_bs_notifiers
, notify
);
1172 void blk_io_plug(BlockBackend
*blk
)
1175 bdrv_io_plug(blk
->bs
);
1179 void blk_io_unplug(BlockBackend
*blk
)
1182 bdrv_io_unplug(blk
->bs
);
1186 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
1191 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
1192 BlockCompletionFunc
*cb
, void *opaque
)
1194 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
1197 int coroutine_fn
blk_co_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
1198 int nb_sectors
, BdrvRequestFlags flags
)
1200 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1205 return bdrv_co_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
1208 int blk_write_compressed(BlockBackend
*blk
, int64_t sector_num
,
1209 const uint8_t *buf
, int nb_sectors
)
1211 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1216 return bdrv_write_compressed(blk
->bs
, sector_num
, buf
, nb_sectors
);
1219 int blk_truncate(BlockBackend
*blk
, int64_t offset
)
1221 if (!blk_is_available(blk
)) {
1225 return bdrv_truncate(blk
->bs
, offset
);
1228 int blk_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
1230 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1235 return bdrv_discard(blk
->bs
, sector_num
, nb_sectors
);
1238 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
1239 int64_t pos
, int size
)
1241 if (!blk_is_available(blk
)) {
1245 return bdrv_save_vmstate(blk
->bs
, buf
, pos
, size
);
1248 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
1250 if (!blk_is_available(blk
)) {
1254 return bdrv_load_vmstate(blk
->bs
, buf
, pos
, size
);
1257 int blk_probe_blocksizes(BlockBackend
*blk
, BlockSizes
*bsz
)
1259 if (!blk_is_available(blk
)) {
1263 return bdrv_probe_blocksizes(blk
->bs
, bsz
);
1266 int blk_probe_geometry(BlockBackend
*blk
, HDGeometry
*geo
)
1268 if (!blk_is_available(blk
)) {
1272 return bdrv_probe_geometry(blk
->bs
, geo
);
1276 * Updates the BlockBackendRootState object with data from the currently
1277 * attached BlockDriverState.
1279 void blk_update_root_state(BlockBackend
*blk
)
1283 blk
->root_state
.open_flags
= blk
->bs
->open_flags
;
1284 blk
->root_state
.read_only
= blk
->bs
->read_only
;
1285 blk
->root_state
.detect_zeroes
= blk
->bs
->detect_zeroes
;
1287 if (blk
->root_state
.throttle_group
) {
1288 g_free(blk
->root_state
.throttle_group
);
1289 throttle_group_unref(blk
->root_state
.throttle_state
);
1291 if (blk
->bs
->throttle_state
) {
1292 const char *name
= throttle_group_get_name(blk
->bs
);
1293 blk
->root_state
.throttle_group
= g_strdup(name
);
1294 blk
->root_state
.throttle_state
= throttle_group_incref(name
);
1296 blk
->root_state
.throttle_group
= NULL
;
1297 blk
->root_state
.throttle_state
= NULL
;
1302 * Applies the information in the root state to the given BlockDriverState. This
1303 * does not include the flags which have to be specified for bdrv_open(), use
1304 * blk_get_open_flags_from_root_state() to inquire them.
1306 void blk_apply_root_state(BlockBackend
*blk
, BlockDriverState
*bs
)
1308 bs
->detect_zeroes
= blk
->root_state
.detect_zeroes
;
1309 if (blk
->root_state
.throttle_group
) {
1310 bdrv_io_limits_enable(bs
, blk
->root_state
.throttle_group
);
1315 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1316 * supposed to inherit the root state.
1318 int blk_get_open_flags_from_root_state(BlockBackend
*blk
)
1322 bs_flags
= blk
->root_state
.read_only
? 0 : BDRV_O_RDWR
;
1323 bs_flags
|= blk
->root_state
.open_flags
& ~BDRV_O_RDWR
;
1328 BlockBackendRootState
*blk_get_root_state(BlockBackend
*blk
)
1330 return &blk
->root_state
;