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 "sysemu/block-backend.h"
14 #include "block/block_int.h"
15 #include "block/blockjob.h"
16 #include "block/throttle-groups.h"
17 #include "sysemu/blockdev.h"
18 #include "sysemu/sysemu.h"
19 #include "qapi-event.h"
21 /* Number of coroutines to reserve per attached device model */
22 #define COROUTINE_POOL_RESERVATION 64
24 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
);
30 DriveInfo
*legacy_dinfo
; /* null unless created by drive_new() */
31 QTAILQ_ENTRY(BlockBackend
) link
; /* for blk_backends */
33 void *dev
; /* attached device model, if any */
34 /* TODO change to DeviceState when all users are qdevified */
35 const BlockDevOps
*dev_ops
;
38 /* the block size for which the guest device expects atomicity */
41 /* If the BDS tree is removed, some of its options are stored here (which
42 * can be used to restore those options in the new BDS on insert) */
43 BlockBackendRootState root_state
;
45 /* I/O stats (display with "info blockstats"). */
48 BlockdevOnError on_read_error
, on_write_error
;
49 bool iostatus_enabled
;
50 BlockDeviceIoStatus iostatus
;
53 typedef struct BlockBackendAIOCB
{
60 static const AIOCBInfo block_backend_aiocb_info
= {
61 .get_aio_context
= blk_aiocb_get_aio_context
,
62 .aiocb_size
= sizeof(BlockBackendAIOCB
),
65 static void drive_info_del(DriveInfo
*dinfo
);
67 /* All the BlockBackends (except for hidden ones) */
68 static QTAILQ_HEAD(, BlockBackend
) blk_backends
=
69 QTAILQ_HEAD_INITIALIZER(blk_backends
);
72 * Create a new BlockBackend with @name, with a reference count of one.
73 * @name must not be null or empty.
74 * Fail if a BlockBackend with this name already exists.
75 * Store an error through @errp on failure, unless it's null.
76 * Return the new BlockBackend on success, null on failure.
78 BlockBackend
*blk_new(const char *name
, Error
**errp
)
82 assert(name
&& name
[0]);
83 if (!id_wellformed(name
)) {
84 error_setg(errp
, "Invalid device name");
87 if (blk_by_name(name
)) {
88 error_setg(errp
, "Device with id '%s' already exists", name
);
91 if (bdrv_find_node(name
)) {
93 "Device name '%s' conflicts with an existing node name",
98 blk
= g_new0(BlockBackend
, 1);
99 blk
->name
= g_strdup(name
);
101 QTAILQ_INSERT_TAIL(&blk_backends
, blk
, link
);
106 * Create a new BlockBackend with a new BlockDriverState attached.
107 * Otherwise just like blk_new(), which see.
109 BlockBackend
*blk_new_with_bs(const char *name
, Error
**errp
)
112 BlockDriverState
*bs
;
114 blk
= blk_new(name
, errp
);
119 bs
= bdrv_new_root();
126 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
128 * Just as with bdrv_open(), after having called this function the reference to
129 * @options belongs to the block layer (even on failure).
131 * TODO: Remove @filename and @flags; it should be possible to specify a whole
132 * BDS tree just by specifying the @options QDict (or @reference,
133 * alternatively). At the time of adding this function, this is not possible,
134 * though, so callers of this function have to be able to specify @filename and
137 BlockBackend
*blk_new_open(const char *name
, const char *filename
,
138 const char *reference
, QDict
*options
, int flags
,
144 blk
= blk_new_with_bs(name
, errp
);
150 ret
= bdrv_open(&blk
->bs
, filename
, reference
, options
, flags
, errp
);
159 static void blk_delete(BlockBackend
*blk
)
161 assert(!blk
->refcnt
);
164 assert(blk
->bs
->blk
== blk
);
169 if (blk
->root_state
.throttle_state
) {
170 g_free(blk
->root_state
.throttle_group
);
171 throttle_group_unref(blk
->root_state
.throttle_state
);
173 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
175 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
178 drive_info_del(blk
->legacy_dinfo
);
179 block_acct_cleanup(&blk
->stats
);
183 static void drive_info_del(DriveInfo
*dinfo
)
188 qemu_opts_del(dinfo
->opts
);
189 g_free(dinfo
->serial
);
193 int blk_get_refcnt(BlockBackend
*blk
)
195 return blk
? blk
->refcnt
: 0;
199 * Increment @blk's reference count.
200 * @blk must not be null.
202 void blk_ref(BlockBackend
*blk
)
208 * Decrement @blk's reference count.
209 * If this drops it to zero, destroy @blk.
210 * For convenience, do nothing if @blk is null.
212 void blk_unref(BlockBackend
*blk
)
215 assert(blk
->refcnt
> 0);
216 if (!--blk
->refcnt
) {
223 * Return the BlockBackend after @blk.
224 * If @blk is null, return the first one.
225 * Else, return @blk's next sibling, which may be null.
227 * To iterate over all BlockBackends, do
228 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
232 BlockBackend
*blk_next(BlockBackend
*blk
)
234 return blk
? QTAILQ_NEXT(blk
, link
) : QTAILQ_FIRST(&blk_backends
);
238 * Return @blk's name, a non-null string.
239 * Wart: the name is empty iff @blk has been hidden with
240 * blk_hide_on_behalf_of_hmp_drive_del().
242 const char *blk_name(BlockBackend
*blk
)
248 * Return the BlockBackend with name @name if it exists, else null.
249 * @name must not be null.
251 BlockBackend
*blk_by_name(const char *name
)
256 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
257 if (!strcmp(name
, blk
->name
)) {
265 * Return the BlockDriverState attached to @blk if any, else null.
267 BlockDriverState
*blk_bs(BlockBackend
*blk
)
273 * Changes the BlockDriverState attached to @blk
275 void blk_set_bs(BlockBackend
*blk
, BlockDriverState
*bs
)
283 assert(bs
->blk
== NULL
);
290 * Return @blk's DriveInfo if any, else null.
292 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
294 return blk
->legacy_dinfo
;
298 * Set @blk's DriveInfo to @dinfo, and return it.
299 * @blk must not have a DriveInfo set already.
300 * No other BlockBackend may have the same DriveInfo set.
302 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
304 assert(!blk
->legacy_dinfo
);
305 return blk
->legacy_dinfo
= dinfo
;
309 * Return the BlockBackend with DriveInfo @dinfo.
312 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
316 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
317 if (blk
->legacy_dinfo
== dinfo
) {
326 * @blk must not have been hidden already.
327 * Make attached BlockDriverState, if any, anonymous.
328 * Once hidden, @blk is invisible to all functions that don't receive
329 * it as argument. For example, blk_by_name() won't return it.
330 * Strictly for use by do_drive_del().
331 * TODO get rid of it!
333 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend
*blk
)
335 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
338 bdrv_make_anon(blk
->bs
);
343 * Disassociates the currently associated BlockDriverState from @blk.
345 void blk_remove_bs(BlockBackend
*blk
)
347 blk_update_root_state(blk
);
355 * Associates a new BlockDriverState with @blk.
357 void blk_insert_bs(BlockBackend
*blk
, BlockDriverState
*bs
)
359 assert(!blk
->bs
&& !bs
->blk
);
366 * Attach device model @dev to @blk.
367 * Return 0 on success, -EBUSY when a device model is attached already.
369 int blk_attach_dev(BlockBackend
*blk
, void *dev
)
370 /* TODO change to DeviceState *dev when all users are qdevified */
377 blk_iostatus_reset(blk
);
382 * Attach device model @dev to @blk.
383 * @blk must not have a device model attached already.
384 * TODO qdevified devices don't use this, remove when devices are qdevified
386 void blk_attach_dev_nofail(BlockBackend
*blk
, void *dev
)
388 if (blk_attach_dev(blk
, dev
) < 0) {
394 * Detach device model @dev from @blk.
395 * @dev must be currently attached to @blk.
397 void blk_detach_dev(BlockBackend
*blk
, void *dev
)
398 /* TODO change to DeviceState *dev when all users are qdevified */
400 assert(blk
->dev
== dev
);
403 blk
->dev_opaque
= NULL
;
404 blk
->guest_block_size
= 512;
409 * Return the device model attached to @blk if any, else null.
411 void *blk_get_attached_dev(BlockBackend
*blk
)
412 /* TODO change to return DeviceState * when all users are qdevified */
418 * Set @blk's device model callbacks to @ops.
419 * @opaque is the opaque argument to pass to the callbacks.
420 * This is for use by device models.
422 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
426 blk
->dev_opaque
= opaque
;
430 * Notify @blk's attached device model of media change.
431 * If @load is true, notify of media load.
432 * Else, notify of media eject.
433 * Also send DEVICE_TRAY_MOVED events as appropriate.
435 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
)
437 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
438 bool tray_was_open
, tray_is_open
;
440 tray_was_open
= blk_dev_is_tray_open(blk
);
441 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
);
442 tray_is_open
= blk_dev_is_tray_open(blk
);
444 if (tray_was_open
!= tray_is_open
) {
445 qapi_event_send_device_tray_moved(blk_name(blk
), tray_is_open
,
452 * Does @blk's attached device model have removable media?
453 * %true if no device model is attached.
455 bool blk_dev_has_removable_media(BlockBackend
*blk
)
457 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
461 * Notify @blk's attached device model of a media eject request.
462 * If @force is true, the medium is about to be yanked out forcefully.
464 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
466 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
467 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
472 * Does @blk's attached device model have a tray, and is it open?
474 bool blk_dev_is_tray_open(BlockBackend
*blk
)
476 if (blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
) {
477 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
483 * Does @blk's attached device model have the medium locked?
484 * %false if the device model has no such lock.
486 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
488 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
489 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
495 * Notify @blk's attached device model of a backend size change.
497 void blk_dev_resize_cb(BlockBackend
*blk
)
499 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
500 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
504 void blk_iostatus_enable(BlockBackend
*blk
)
506 blk
->iostatus_enabled
= true;
507 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
510 /* The I/O status is only enabled if the drive explicitly
511 * enables it _and_ the VM is configured to stop on errors */
512 bool blk_iostatus_is_enabled(const BlockBackend
*blk
)
514 return (blk
->iostatus_enabled
&&
515 (blk
->on_write_error
== BLOCKDEV_ON_ERROR_ENOSPC
||
516 blk
->on_write_error
== BLOCKDEV_ON_ERROR_STOP
||
517 blk
->on_read_error
== BLOCKDEV_ON_ERROR_STOP
));
520 BlockDeviceIoStatus
blk_iostatus(const BlockBackend
*blk
)
522 return blk
->iostatus
;
525 void blk_iostatus_disable(BlockBackend
*blk
)
527 blk
->iostatus_enabled
= false;
530 void blk_iostatus_reset(BlockBackend
*blk
)
532 if (blk_iostatus_is_enabled(blk
)) {
533 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
534 if (blk
->bs
&& blk
->bs
->job
) {
535 block_job_iostatus_reset(blk
->bs
->job
);
540 void blk_iostatus_set_err(BlockBackend
*blk
, int error
)
542 assert(blk_iostatus_is_enabled(blk
));
543 if (blk
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
544 blk
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
545 BLOCK_DEVICE_IO_STATUS_FAILED
;
549 static int blk_check_byte_request(BlockBackend
*blk
, int64_t offset
,
554 if (size
> INT_MAX
) {
558 if (!blk_is_available(blk
)) {
562 len
= blk_getlength(blk
);
571 if (offset
> len
|| len
- offset
< size
) {
578 static int blk_check_request(BlockBackend
*blk
, int64_t sector_num
,
581 if (sector_num
< 0 || sector_num
> INT64_MAX
/ BDRV_SECTOR_SIZE
) {
585 if (nb_sectors
< 0 || nb_sectors
> INT_MAX
/ BDRV_SECTOR_SIZE
) {
589 return blk_check_byte_request(blk
, sector_num
* BDRV_SECTOR_SIZE
,
590 nb_sectors
* BDRV_SECTOR_SIZE
);
593 int blk_read(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
596 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
601 return bdrv_read(blk
->bs
, sector_num
, buf
, nb_sectors
);
604 int blk_read_unthrottled(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
607 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
612 return bdrv_read_unthrottled(blk
->bs
, sector_num
, buf
, nb_sectors
);
615 int blk_write(BlockBackend
*blk
, int64_t sector_num
, const uint8_t *buf
,
618 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
623 return bdrv_write(blk
->bs
, sector_num
, buf
, nb_sectors
);
626 int blk_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
627 int nb_sectors
, BdrvRequestFlags flags
)
629 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
634 return bdrv_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
637 static void error_callback_bh(void *opaque
)
639 struct BlockBackendAIOCB
*acb
= opaque
;
640 qemu_bh_delete(acb
->bh
);
641 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
645 static BlockAIOCB
*abort_aio_request(BlockBackend
*blk
, BlockCompletionFunc
*cb
,
646 void *opaque
, int ret
)
648 struct BlockBackendAIOCB
*acb
;
651 acb
= blk_aio_get(&block_backend_aiocb_info
, blk
, cb
, opaque
);
655 bh
= aio_bh_new(blk_get_aio_context(blk
), error_callback_bh
, acb
);
657 qemu_bh_schedule(bh
);
662 BlockAIOCB
*blk_aio_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
663 int nb_sectors
, BdrvRequestFlags flags
,
664 BlockCompletionFunc
*cb
, void *opaque
)
666 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
668 return abort_aio_request(blk
, cb
, opaque
, ret
);
671 return bdrv_aio_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
,
675 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int count
)
677 int ret
= blk_check_byte_request(blk
, offset
, count
);
682 return bdrv_pread(blk
->bs
, offset
, buf
, count
);
685 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int count
)
687 int ret
= blk_check_byte_request(blk
, offset
, count
);
692 return bdrv_pwrite(blk
->bs
, offset
, buf
, count
);
695 int64_t blk_getlength(BlockBackend
*blk
)
697 if (!blk_is_available(blk
)) {
701 return bdrv_getlength(blk
->bs
);
704 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
709 bdrv_get_geometry(blk
->bs
, nb_sectors_ptr
);
713 int64_t blk_nb_sectors(BlockBackend
*blk
)
715 if (!blk_is_available(blk
)) {
719 return bdrv_nb_sectors(blk
->bs
);
722 BlockAIOCB
*blk_aio_readv(BlockBackend
*blk
, int64_t sector_num
,
723 QEMUIOVector
*iov
, int nb_sectors
,
724 BlockCompletionFunc
*cb
, void *opaque
)
726 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
728 return abort_aio_request(blk
, cb
, opaque
, ret
);
731 return bdrv_aio_readv(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
734 BlockAIOCB
*blk_aio_writev(BlockBackend
*blk
, int64_t sector_num
,
735 QEMUIOVector
*iov
, int nb_sectors
,
736 BlockCompletionFunc
*cb
, void *opaque
)
738 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
740 return abort_aio_request(blk
, cb
, opaque
, ret
);
743 return bdrv_aio_writev(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
746 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
747 BlockCompletionFunc
*cb
, void *opaque
)
749 if (!blk_is_available(blk
)) {
750 return abort_aio_request(blk
, cb
, opaque
, -ENOMEDIUM
);
753 return bdrv_aio_flush(blk
->bs
, cb
, opaque
);
756 BlockAIOCB
*blk_aio_discard(BlockBackend
*blk
,
757 int64_t sector_num
, int nb_sectors
,
758 BlockCompletionFunc
*cb
, void *opaque
)
760 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
762 return abort_aio_request(blk
, cb
, opaque
, ret
);
765 return bdrv_aio_discard(blk
->bs
, sector_num
, nb_sectors
, cb
, opaque
);
768 void blk_aio_cancel(BlockAIOCB
*acb
)
770 bdrv_aio_cancel(acb
);
773 void blk_aio_cancel_async(BlockAIOCB
*acb
)
775 bdrv_aio_cancel_async(acb
);
778 int blk_aio_multiwrite(BlockBackend
*blk
, BlockRequest
*reqs
, int num_reqs
)
782 for (i
= 0; i
< num_reqs
; i
++) {
783 ret
= blk_check_request(blk
, reqs
[i
].sector
, reqs
[i
].nb_sectors
);
789 return bdrv_aio_multiwrite(blk
->bs
, reqs
, num_reqs
);
792 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
794 if (!blk_is_available(blk
)) {
798 return bdrv_ioctl(blk
->bs
, req
, buf
);
801 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
802 BlockCompletionFunc
*cb
, void *opaque
)
804 if (!blk_is_available(blk
)) {
805 return abort_aio_request(blk
, cb
, opaque
, -ENOMEDIUM
);
808 return bdrv_aio_ioctl(blk
->bs
, req
, buf
, cb
, opaque
);
811 int blk_co_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
813 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
818 return bdrv_co_discard(blk
->bs
, sector_num
, nb_sectors
);
821 int blk_co_flush(BlockBackend
*blk
)
823 if (!blk_is_available(blk
)) {
827 return bdrv_co_flush(blk
->bs
);
830 int blk_flush(BlockBackend
*blk
)
832 if (!blk_is_available(blk
)) {
836 return bdrv_flush(blk
->bs
);
839 int blk_flush_all(void)
841 return bdrv_flush_all();
844 void blk_drain(BlockBackend
*blk
)
851 void blk_drain_all(void)
856 void blk_set_on_error(BlockBackend
*blk
, BlockdevOnError on_read_error
,
857 BlockdevOnError on_write_error
)
859 blk
->on_read_error
= on_read_error
;
860 blk
->on_write_error
= on_write_error
;
863 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
865 return is_read
? blk
->on_read_error
: blk
->on_write_error
;
868 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
871 BlockdevOnError on_err
= blk_get_on_error(blk
, is_read
);
874 case BLOCKDEV_ON_ERROR_ENOSPC
:
875 return (error
== ENOSPC
) ?
876 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
877 case BLOCKDEV_ON_ERROR_STOP
:
878 return BLOCK_ERROR_ACTION_STOP
;
879 case BLOCKDEV_ON_ERROR_REPORT
:
880 return BLOCK_ERROR_ACTION_REPORT
;
881 case BLOCKDEV_ON_ERROR_IGNORE
:
882 return BLOCK_ERROR_ACTION_IGNORE
;
888 static void send_qmp_error_event(BlockBackend
*blk
,
889 BlockErrorAction action
,
890 bool is_read
, int error
)
892 IoOperationType optype
;
894 optype
= is_read
? IO_OPERATION_TYPE_READ
: IO_OPERATION_TYPE_WRITE
;
895 qapi_event_send_block_io_error(blk_name(blk
), optype
, action
,
896 blk_iostatus_is_enabled(blk
),
897 error
== ENOSPC
, strerror(error
),
901 /* This is done by device models because, while the block layer knows
902 * about the error, it does not know whether an operation comes from
903 * the device or the block layer (from a job, for example).
905 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
906 bool is_read
, int error
)
910 if (action
== BLOCK_ERROR_ACTION_STOP
) {
911 /* First set the iostatus, so that "info block" returns an iostatus
912 * that matches the events raised so far (an additional error iostatus
913 * is fine, but not a lost one).
915 blk_iostatus_set_err(blk
, error
);
917 /* Then raise the request to stop the VM and the event.
918 * qemu_system_vmstop_request_prepare has two effects. First,
919 * it ensures that the STOP event always comes after the
920 * BLOCK_IO_ERROR event. Second, it ensures that even if management
921 * can observe the STOP event and do a "cont" before the STOP
922 * event is issued, the VM will not stop. In this case, vm_start()
923 * also ensures that the STOP/RESUME pair of events is emitted.
925 qemu_system_vmstop_request_prepare();
926 send_qmp_error_event(blk
, action
, is_read
, error
);
927 qemu_system_vmstop_request(RUN_STATE_IO_ERROR
);
929 send_qmp_error_event(blk
, action
, is_read
, error
);
933 int blk_is_read_only(BlockBackend
*blk
)
936 return bdrv_is_read_only(blk
->bs
);
938 return blk
->root_state
.read_only
;
942 int blk_is_sg(BlockBackend
*blk
)
948 return bdrv_is_sg(blk
->bs
);
951 int blk_enable_write_cache(BlockBackend
*blk
)
954 return bdrv_enable_write_cache(blk
->bs
);
956 return !!(blk
->root_state
.open_flags
& BDRV_O_CACHE_WB
);
960 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
963 bdrv_set_enable_write_cache(blk
->bs
, wce
);
966 blk
->root_state
.open_flags
|= BDRV_O_CACHE_WB
;
968 blk
->root_state
.open_flags
&= ~BDRV_O_CACHE_WB
;
973 void blk_invalidate_cache(BlockBackend
*blk
, Error
**errp
)
976 error_setg(errp
, "Device '%s' has no medium", blk
->name
);
980 bdrv_invalidate_cache(blk
->bs
, errp
);
983 bool blk_is_inserted(BlockBackend
*blk
)
985 return blk
->bs
&& bdrv_is_inserted(blk
->bs
);
988 bool blk_is_available(BlockBackend
*blk
)
990 return blk_is_inserted(blk
) && !blk_dev_is_tray_open(blk
);
993 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
996 bdrv_lock_medium(blk
->bs
, locked
);
1000 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
1003 bdrv_eject(blk
->bs
, eject_flag
);
1007 int blk_get_flags(BlockBackend
*blk
)
1010 return bdrv_get_flags(blk
->bs
);
1012 return blk
->root_state
.open_flags
;
1016 int blk_get_max_transfer_length(BlockBackend
*blk
)
1019 return blk
->bs
->bl
.max_transfer_length
;
1025 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
1027 blk
->guest_block_size
= align
;
1030 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
1032 return qemu_blockalign(blk
? blk
->bs
: NULL
, size
);
1035 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
1041 return bdrv_op_is_blocked(blk
->bs
, op
, errp
);
1044 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
1047 bdrv_op_unblock(blk
->bs
, op
, reason
);
1051 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
1054 bdrv_op_block_all(blk
->bs
, reason
);
1058 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
1061 bdrv_op_unblock_all(blk
->bs
, reason
);
1065 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
1068 return bdrv_get_aio_context(blk
->bs
);
1070 return qemu_get_aio_context();
1074 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
)
1076 BlockBackendAIOCB
*blk_acb
= DO_UPCAST(BlockBackendAIOCB
, common
, acb
);
1077 return blk_get_aio_context(blk_acb
->blk
);
1080 void blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
)
1083 bdrv_set_aio_context(blk
->bs
, new_context
);
1087 void blk_add_aio_context_notifier(BlockBackend
*blk
,
1088 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
1089 void (*detach_aio_context
)(void *opaque
), void *opaque
)
1092 bdrv_add_aio_context_notifier(blk
->bs
, attached_aio_context
,
1093 detach_aio_context
, opaque
);
1097 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
1098 void (*attached_aio_context
)(AioContext
*,
1100 void (*detach_aio_context
)(void *),
1104 bdrv_remove_aio_context_notifier(blk
->bs
, attached_aio_context
,
1105 detach_aio_context
, opaque
);
1109 void blk_add_close_notifier(BlockBackend
*blk
, Notifier
*notify
)
1112 bdrv_add_close_notifier(blk
->bs
, notify
);
1116 void blk_io_plug(BlockBackend
*blk
)
1119 bdrv_io_plug(blk
->bs
);
1123 void blk_io_unplug(BlockBackend
*blk
)
1126 bdrv_io_unplug(blk
->bs
);
1130 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
1135 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
1136 BlockCompletionFunc
*cb
, void *opaque
)
1138 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
1141 int coroutine_fn
blk_co_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
1142 int nb_sectors
, BdrvRequestFlags flags
)
1144 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1149 return bdrv_co_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
1152 int blk_write_compressed(BlockBackend
*blk
, int64_t sector_num
,
1153 const uint8_t *buf
, int nb_sectors
)
1155 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1160 return bdrv_write_compressed(blk
->bs
, sector_num
, buf
, nb_sectors
);
1163 int blk_truncate(BlockBackend
*blk
, int64_t offset
)
1165 if (!blk_is_available(blk
)) {
1169 return bdrv_truncate(blk
->bs
, offset
);
1172 int blk_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
1174 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1179 return bdrv_discard(blk
->bs
, sector_num
, nb_sectors
);
1182 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
1183 int64_t pos
, int size
)
1185 if (!blk_is_available(blk
)) {
1189 return bdrv_save_vmstate(blk
->bs
, buf
, pos
, size
);
1192 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
1194 if (!blk_is_available(blk
)) {
1198 return bdrv_load_vmstate(blk
->bs
, buf
, pos
, size
);
1201 int blk_probe_blocksizes(BlockBackend
*blk
, BlockSizes
*bsz
)
1203 if (!blk_is_available(blk
)) {
1207 return bdrv_probe_blocksizes(blk
->bs
, bsz
);
1210 int blk_probe_geometry(BlockBackend
*blk
, HDGeometry
*geo
)
1212 if (!blk_is_available(blk
)) {
1216 return bdrv_probe_geometry(blk
->bs
, geo
);
1220 * Updates the BlockBackendRootState object with data from the currently
1221 * attached BlockDriverState.
1223 void blk_update_root_state(BlockBackend
*blk
)
1227 blk
->root_state
.open_flags
= blk
->bs
->open_flags
;
1228 blk
->root_state
.read_only
= blk
->bs
->read_only
;
1229 blk
->root_state
.detect_zeroes
= blk
->bs
->detect_zeroes
;
1231 if (blk
->root_state
.throttle_group
) {
1232 g_free(blk
->root_state
.throttle_group
);
1233 throttle_group_unref(blk
->root_state
.throttle_state
);
1235 if (blk
->bs
->throttle_state
) {
1236 const char *name
= throttle_group_get_name(blk
->bs
);
1237 blk
->root_state
.throttle_group
= g_strdup(name
);
1238 blk
->root_state
.throttle_state
= throttle_group_incref(name
);
1240 blk
->root_state
.throttle_group
= NULL
;
1241 blk
->root_state
.throttle_state
= NULL
;
1246 * Applies the information in the root state to the given BlockDriverState. This
1247 * does not include the flags which have to be specified for bdrv_open(), use
1248 * blk_get_open_flags_from_root_state() to inquire them.
1250 void blk_apply_root_state(BlockBackend
*blk
, BlockDriverState
*bs
)
1252 bs
->detect_zeroes
= blk
->root_state
.detect_zeroes
;
1253 if (blk
->root_state
.throttle_group
) {
1254 bdrv_io_limits_enable(bs
, blk
->root_state
.throttle_group
);
1259 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1260 * supposed to inherit the root state.
1262 int blk_get_open_flags_from_root_state(BlockBackend
*blk
)
1266 bs_flags
= blk
->root_state
.read_only
? 0 : BDRV_O_RDWR
;
1267 bs_flags
|= blk
->root_state
.open_flags
& ~BDRV_O_RDWR
;
1272 BlockBackendRootState
*blk_get_root_state(BlockBackend
*blk
)
1274 return &blk
->root_state
;