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 "sysemu/blockdev.h"
17 #include "sysemu/sysemu.h"
18 #include "qapi-event.h"
20 /* Number of coroutines to reserve per attached device model */
21 #define COROUTINE_POOL_RESERVATION 64
23 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
);
29 DriveInfo
*legacy_dinfo
; /* null unless created by drive_new() */
30 QTAILQ_ENTRY(BlockBackend
) link
; /* for blk_backends */
32 void *dev
; /* attached device model, if any */
33 /* TODO change to DeviceState when all users are qdevified */
34 const BlockDevOps
*dev_ops
;
37 /* the block size for which the guest device expects atomicity */
40 /* I/O stats (display with "info blockstats"). */
43 BlockdevOnError on_read_error
, on_write_error
;
44 bool iostatus_enabled
;
45 BlockDeviceIoStatus iostatus
;
48 typedef struct BlockBackendAIOCB
{
55 static const AIOCBInfo block_backend_aiocb_info
= {
56 .get_aio_context
= blk_aiocb_get_aio_context
,
57 .aiocb_size
= sizeof(BlockBackendAIOCB
),
60 static void drive_info_del(DriveInfo
*dinfo
);
62 /* All the BlockBackends (except for hidden ones) */
63 static QTAILQ_HEAD(, BlockBackend
) blk_backends
=
64 QTAILQ_HEAD_INITIALIZER(blk_backends
);
67 * Create a new BlockBackend with @name, with a reference count of one.
68 * @name must not be null or empty.
69 * Fail if a BlockBackend with this name already exists.
70 * Store an error through @errp on failure, unless it's null.
71 * Return the new BlockBackend on success, null on failure.
73 BlockBackend
*blk_new(const char *name
, Error
**errp
)
77 assert(name
&& name
[0]);
78 if (!id_wellformed(name
)) {
79 error_setg(errp
, "Invalid device name");
82 if (blk_by_name(name
)) {
83 error_setg(errp
, "Device with id '%s' already exists", name
);
86 if (bdrv_find_node(name
)) {
88 "Device name '%s' conflicts with an existing node name",
93 blk
= g_new0(BlockBackend
, 1);
94 blk
->name
= g_strdup(name
);
96 QTAILQ_INSERT_TAIL(&blk_backends
, blk
, link
);
101 * Create a new BlockBackend with a new BlockDriverState attached.
102 * Otherwise just like blk_new(), which see.
104 BlockBackend
*blk_new_with_bs(const char *name
, Error
**errp
)
107 BlockDriverState
*bs
;
109 blk
= blk_new(name
, errp
);
114 bs
= bdrv_new_root();
121 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
123 * Just as with bdrv_open(), after having called this function the reference to
124 * @options belongs to the block layer (even on failure).
126 * TODO: Remove @filename and @flags; it should be possible to specify a whole
127 * BDS tree just by specifying the @options QDict (or @reference,
128 * alternatively). At the time of adding this function, this is not possible,
129 * though, so callers of this function have to be able to specify @filename and
132 BlockBackend
*blk_new_open(const char *name
, const char *filename
,
133 const char *reference
, QDict
*options
, int flags
,
139 blk
= blk_new_with_bs(name
, errp
);
145 ret
= bdrv_open(&blk
->bs
, filename
, reference
, options
, flags
, errp
);
154 static void blk_delete(BlockBackend
*blk
)
156 assert(!blk
->refcnt
);
159 assert(blk
->bs
->blk
== blk
);
164 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
166 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
169 drive_info_del(blk
->legacy_dinfo
);
173 static void drive_info_del(DriveInfo
*dinfo
)
178 qemu_opts_del(dinfo
->opts
);
179 g_free(dinfo
->serial
);
184 * Increment @blk's reference count.
185 * @blk must not be null.
187 void blk_ref(BlockBackend
*blk
)
193 * Decrement @blk's reference count.
194 * If this drops it to zero, destroy @blk.
195 * For convenience, do nothing if @blk is null.
197 void blk_unref(BlockBackend
*blk
)
200 assert(blk
->refcnt
> 0);
201 if (!--blk
->refcnt
) {
208 * Return the BlockBackend after @blk.
209 * If @blk is null, return the first one.
210 * Else, return @blk's next sibling, which may be null.
212 * To iterate over all BlockBackends, do
213 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
217 BlockBackend
*blk_next(BlockBackend
*blk
)
219 return blk
? QTAILQ_NEXT(blk
, link
) : QTAILQ_FIRST(&blk_backends
);
223 * Return @blk's name, a non-null string.
224 * Wart: the name is empty iff @blk has been hidden with
225 * blk_hide_on_behalf_of_hmp_drive_del().
227 const char *blk_name(BlockBackend
*blk
)
233 * Return the BlockBackend with name @name if it exists, else null.
234 * @name must not be null.
236 BlockBackend
*blk_by_name(const char *name
)
241 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
242 if (!strcmp(name
, blk
->name
)) {
250 * Return the BlockDriverState attached to @blk if any, else null.
252 BlockDriverState
*blk_bs(BlockBackend
*blk
)
258 * Changes the BlockDriverState attached to @blk
260 void blk_set_bs(BlockBackend
*blk
, BlockDriverState
*bs
)
268 assert(bs
->blk
== NULL
);
275 * Return @blk's DriveInfo if any, else null.
277 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
279 return blk
->legacy_dinfo
;
283 * Set @blk's DriveInfo to @dinfo, and return it.
284 * @blk must not have a DriveInfo set already.
285 * No other BlockBackend may have the same DriveInfo set.
287 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
289 assert(!blk
->legacy_dinfo
);
290 return blk
->legacy_dinfo
= dinfo
;
294 * Return the BlockBackend with DriveInfo @dinfo.
297 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
301 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
302 if (blk
->legacy_dinfo
== dinfo
) {
311 * @blk must not have been hidden already.
312 * Make attached BlockDriverState, if any, anonymous.
313 * Once hidden, @blk is invisible to all functions that don't receive
314 * it as argument. For example, blk_by_name() won't return it.
315 * Strictly for use by do_drive_del().
316 * TODO get rid of it!
318 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend
*blk
)
320 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
323 bdrv_make_anon(blk
->bs
);
328 * Attach device model @dev to @blk.
329 * Return 0 on success, -EBUSY when a device model is attached already.
331 int blk_attach_dev(BlockBackend
*blk
, void *dev
)
332 /* TODO change to DeviceState *dev when all users are qdevified */
339 blk_iostatus_reset(blk
);
344 * Attach device model @dev to @blk.
345 * @blk must not have a device model attached already.
346 * TODO qdevified devices don't use this, remove when devices are qdevified
348 void blk_attach_dev_nofail(BlockBackend
*blk
, void *dev
)
350 if (blk_attach_dev(blk
, dev
) < 0) {
356 * Detach device model @dev from @blk.
357 * @dev must be currently attached to @blk.
359 void blk_detach_dev(BlockBackend
*blk
, void *dev
)
360 /* TODO change to DeviceState *dev when all users are qdevified */
362 assert(blk
->dev
== dev
);
365 blk
->dev_opaque
= NULL
;
366 blk
->guest_block_size
= 512;
371 * Return the device model attached to @blk if any, else null.
373 void *blk_get_attached_dev(BlockBackend
*blk
)
374 /* TODO change to return DeviceState * when all users are qdevified */
380 * Set @blk's device model callbacks to @ops.
381 * @opaque is the opaque argument to pass to the callbacks.
382 * This is for use by device models.
384 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
388 blk
->dev_opaque
= opaque
;
392 * Notify @blk's attached device model of media change.
393 * If @load is true, notify of media load.
394 * Else, notify of media eject.
395 * Also send DEVICE_TRAY_MOVED events as appropriate.
397 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
)
399 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
400 bool tray_was_closed
= !blk_dev_is_tray_open(blk
);
402 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
);
403 if (tray_was_closed
) {
405 qapi_event_send_device_tray_moved(blk_name(blk
),
410 qapi_event_send_device_tray_moved(blk_name(blk
),
411 false, &error_abort
);
417 * Does @blk's attached device model have removable media?
418 * %true if no device model is attached.
420 bool blk_dev_has_removable_media(BlockBackend
*blk
)
422 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
426 * Notify @blk's attached device model of a media eject request.
427 * If @force is true, the medium is about to be yanked out forcefully.
429 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
431 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
432 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
437 * Does @blk's attached device model have a tray, and is it open?
439 bool blk_dev_is_tray_open(BlockBackend
*blk
)
441 if (blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
) {
442 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
448 * Does @blk's attached device model have the medium locked?
449 * %false if the device model has no such lock.
451 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
453 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
454 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
460 * Notify @blk's attached device model of a backend size change.
462 void blk_dev_resize_cb(BlockBackend
*blk
)
464 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
465 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
469 void blk_iostatus_enable(BlockBackend
*blk
)
471 blk
->iostatus_enabled
= true;
472 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
475 /* The I/O status is only enabled if the drive explicitly
476 * enables it _and_ the VM is configured to stop on errors */
477 bool blk_iostatus_is_enabled(const BlockBackend
*blk
)
479 return (blk
->iostatus_enabled
&&
480 (blk
->on_write_error
== BLOCKDEV_ON_ERROR_ENOSPC
||
481 blk
->on_write_error
== BLOCKDEV_ON_ERROR_STOP
||
482 blk
->on_read_error
== BLOCKDEV_ON_ERROR_STOP
));
485 BlockDeviceIoStatus
blk_iostatus(const BlockBackend
*blk
)
487 return blk
->iostatus
;
490 void blk_iostatus_disable(BlockBackend
*blk
)
492 blk
->iostatus_enabled
= false;
495 void blk_iostatus_reset(BlockBackend
*blk
)
497 if (blk_iostatus_is_enabled(blk
)) {
498 blk
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
499 if (blk
->bs
&& blk
->bs
->job
) {
500 block_job_iostatus_reset(blk
->bs
->job
);
505 void blk_iostatus_set_err(BlockBackend
*blk
, int error
)
507 assert(blk_iostatus_is_enabled(blk
));
508 if (blk
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
509 blk
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
510 BLOCK_DEVICE_IO_STATUS_FAILED
;
514 static int blk_check_byte_request(BlockBackend
*blk
, int64_t offset
,
519 if (size
> INT_MAX
) {
523 if (!blk_is_inserted(blk
)) {
527 len
= blk_getlength(blk
);
536 if (offset
> len
|| len
- offset
< size
) {
543 static int blk_check_request(BlockBackend
*blk
, int64_t sector_num
,
546 if (sector_num
< 0 || sector_num
> INT64_MAX
/ BDRV_SECTOR_SIZE
) {
550 if (nb_sectors
< 0 || nb_sectors
> INT_MAX
/ BDRV_SECTOR_SIZE
) {
554 return blk_check_byte_request(blk
, sector_num
* BDRV_SECTOR_SIZE
,
555 nb_sectors
* BDRV_SECTOR_SIZE
);
558 int blk_read(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
561 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
566 return bdrv_read(blk
->bs
, sector_num
, buf
, nb_sectors
);
569 int blk_read_unthrottled(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
572 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
577 return bdrv_read_unthrottled(blk
->bs
, sector_num
, buf
, nb_sectors
);
580 int blk_write(BlockBackend
*blk
, int64_t sector_num
, const uint8_t *buf
,
583 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
588 return bdrv_write(blk
->bs
, sector_num
, buf
, nb_sectors
);
591 int blk_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
592 int nb_sectors
, BdrvRequestFlags flags
)
594 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
599 return bdrv_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
602 static void error_callback_bh(void *opaque
)
604 struct BlockBackendAIOCB
*acb
= opaque
;
605 qemu_bh_delete(acb
->bh
);
606 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
610 static BlockAIOCB
*abort_aio_request(BlockBackend
*blk
, BlockCompletionFunc
*cb
,
611 void *opaque
, int ret
)
613 struct BlockBackendAIOCB
*acb
;
616 acb
= blk_aio_get(&block_backend_aiocb_info
, blk
, cb
, opaque
);
620 bh
= aio_bh_new(blk_get_aio_context(blk
), error_callback_bh
, acb
);
622 qemu_bh_schedule(bh
);
627 BlockAIOCB
*blk_aio_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
628 int nb_sectors
, BdrvRequestFlags flags
,
629 BlockCompletionFunc
*cb
, void *opaque
)
631 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
633 return abort_aio_request(blk
, cb
, opaque
, ret
);
636 return bdrv_aio_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
,
640 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int count
)
642 int ret
= blk_check_byte_request(blk
, offset
, count
);
647 return bdrv_pread(blk
->bs
, offset
, buf
, count
);
650 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int count
)
652 int ret
= blk_check_byte_request(blk
, offset
, count
);
657 return bdrv_pwrite(blk
->bs
, offset
, buf
, count
);
660 int64_t blk_getlength(BlockBackend
*blk
)
662 return bdrv_getlength(blk
->bs
);
665 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
667 bdrv_get_geometry(blk
->bs
, nb_sectors_ptr
);
670 int64_t blk_nb_sectors(BlockBackend
*blk
)
672 return bdrv_nb_sectors(blk
->bs
);
675 BlockAIOCB
*blk_aio_readv(BlockBackend
*blk
, int64_t sector_num
,
676 QEMUIOVector
*iov
, int nb_sectors
,
677 BlockCompletionFunc
*cb
, void *opaque
)
679 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
681 return abort_aio_request(blk
, cb
, opaque
, ret
);
684 return bdrv_aio_readv(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
687 BlockAIOCB
*blk_aio_writev(BlockBackend
*blk
, int64_t sector_num
,
688 QEMUIOVector
*iov
, int nb_sectors
,
689 BlockCompletionFunc
*cb
, void *opaque
)
691 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
693 return abort_aio_request(blk
, cb
, opaque
, ret
);
696 return bdrv_aio_writev(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
699 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
700 BlockCompletionFunc
*cb
, void *opaque
)
702 return bdrv_aio_flush(blk
->bs
, cb
, opaque
);
705 BlockAIOCB
*blk_aio_discard(BlockBackend
*blk
,
706 int64_t sector_num
, int nb_sectors
,
707 BlockCompletionFunc
*cb
, void *opaque
)
709 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
711 return abort_aio_request(blk
, cb
, opaque
, ret
);
714 return bdrv_aio_discard(blk
->bs
, sector_num
, nb_sectors
, cb
, opaque
);
717 void blk_aio_cancel(BlockAIOCB
*acb
)
719 bdrv_aio_cancel(acb
);
722 void blk_aio_cancel_async(BlockAIOCB
*acb
)
724 bdrv_aio_cancel_async(acb
);
727 int blk_aio_multiwrite(BlockBackend
*blk
, BlockRequest
*reqs
, int num_reqs
)
731 for (i
= 0; i
< num_reqs
; i
++) {
732 ret
= blk_check_request(blk
, reqs
[i
].sector
, reqs
[i
].nb_sectors
);
738 return bdrv_aio_multiwrite(blk
->bs
, reqs
, num_reqs
);
741 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
743 return bdrv_ioctl(blk
->bs
, req
, buf
);
746 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
747 BlockCompletionFunc
*cb
, void *opaque
)
749 return bdrv_aio_ioctl(blk
->bs
, req
, buf
, cb
, opaque
);
752 int blk_co_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
754 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
759 return bdrv_co_discard(blk
->bs
, sector_num
, nb_sectors
);
762 int blk_co_flush(BlockBackend
*blk
)
764 return bdrv_co_flush(blk
->bs
);
767 int blk_flush(BlockBackend
*blk
)
769 return bdrv_flush(blk
->bs
);
772 int blk_flush_all(void)
774 return bdrv_flush_all();
777 void blk_drain(BlockBackend
*blk
)
782 void blk_drain_all(void)
787 void blk_set_on_error(BlockBackend
*blk
, BlockdevOnError on_read_error
,
788 BlockdevOnError on_write_error
)
790 blk
->on_read_error
= on_read_error
;
791 blk
->on_write_error
= on_write_error
;
794 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
796 return is_read
? blk
->on_read_error
: blk
->on_write_error
;
799 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
802 BlockdevOnError on_err
= blk_get_on_error(blk
, is_read
);
805 case BLOCKDEV_ON_ERROR_ENOSPC
:
806 return (error
== ENOSPC
) ?
807 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
808 case BLOCKDEV_ON_ERROR_STOP
:
809 return BLOCK_ERROR_ACTION_STOP
;
810 case BLOCKDEV_ON_ERROR_REPORT
:
811 return BLOCK_ERROR_ACTION_REPORT
;
812 case BLOCKDEV_ON_ERROR_IGNORE
:
813 return BLOCK_ERROR_ACTION_IGNORE
;
819 static void send_qmp_error_event(BlockBackend
*blk
,
820 BlockErrorAction action
,
821 bool is_read
, int error
)
823 IoOperationType optype
;
825 optype
= is_read
? IO_OPERATION_TYPE_READ
: IO_OPERATION_TYPE_WRITE
;
826 qapi_event_send_block_io_error(blk_name(blk
), optype
, action
,
827 blk_iostatus_is_enabled(blk
),
828 error
== ENOSPC
, strerror(error
),
832 /* This is done by device models because, while the block layer knows
833 * about the error, it does not know whether an operation comes from
834 * the device or the block layer (from a job, for example).
836 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
837 bool is_read
, int error
)
841 if (action
== BLOCK_ERROR_ACTION_STOP
) {
842 /* First set the iostatus, so that "info block" returns an iostatus
843 * that matches the events raised so far (an additional error iostatus
844 * is fine, but not a lost one).
846 blk_iostatus_set_err(blk
, error
);
848 /* Then raise the request to stop the VM and the event.
849 * qemu_system_vmstop_request_prepare has two effects. First,
850 * it ensures that the STOP event always comes after the
851 * BLOCK_IO_ERROR event. Second, it ensures that even if management
852 * can observe the STOP event and do a "cont" before the STOP
853 * event is issued, the VM will not stop. In this case, vm_start()
854 * also ensures that the STOP/RESUME pair of events is emitted.
856 qemu_system_vmstop_request_prepare();
857 send_qmp_error_event(blk
, action
, is_read
, error
);
858 qemu_system_vmstop_request(RUN_STATE_IO_ERROR
);
860 send_qmp_error_event(blk
, action
, is_read
, error
);
864 int blk_is_read_only(BlockBackend
*blk
)
866 return bdrv_is_read_only(blk
->bs
);
869 int blk_is_sg(BlockBackend
*blk
)
871 return bdrv_is_sg(blk
->bs
);
874 int blk_enable_write_cache(BlockBackend
*blk
)
876 return bdrv_enable_write_cache(blk
->bs
);
879 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
881 bdrv_set_enable_write_cache(blk
->bs
, wce
);
884 void blk_invalidate_cache(BlockBackend
*blk
, Error
**errp
)
886 bdrv_invalidate_cache(blk
->bs
, errp
);
889 bool blk_is_inserted(BlockBackend
*blk
)
891 return blk
->bs
&& bdrv_is_inserted(blk
->bs
);
894 bool blk_is_available(BlockBackend
*blk
)
896 return blk_is_inserted(blk
) && !blk_dev_is_tray_open(blk
);
899 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
901 bdrv_lock_medium(blk
->bs
, locked
);
904 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
906 bdrv_eject(blk
->bs
, eject_flag
);
909 int blk_get_flags(BlockBackend
*blk
)
911 return bdrv_get_flags(blk
->bs
);
914 int blk_get_max_transfer_length(BlockBackend
*blk
)
916 return blk
->bs
->bl
.max_transfer_length
;
919 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
921 blk
->guest_block_size
= align
;
924 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
926 return qemu_blockalign(blk
? blk
->bs
: NULL
, size
);
929 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
931 return bdrv_op_is_blocked(blk
->bs
, op
, errp
);
934 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
936 bdrv_op_unblock(blk
->bs
, op
, reason
);
939 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
941 bdrv_op_block_all(blk
->bs
, reason
);
944 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
946 bdrv_op_unblock_all(blk
->bs
, reason
);
949 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
952 return bdrv_get_aio_context(blk
->bs
);
954 return qemu_get_aio_context();
958 static AioContext
*blk_aiocb_get_aio_context(BlockAIOCB
*acb
)
960 BlockBackendAIOCB
*blk_acb
= DO_UPCAST(BlockBackendAIOCB
, common
, acb
);
961 return blk_get_aio_context(blk_acb
->blk
);
964 void blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
)
966 bdrv_set_aio_context(blk
->bs
, new_context
);
969 void blk_add_aio_context_notifier(BlockBackend
*blk
,
970 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
971 void (*detach_aio_context
)(void *opaque
), void *opaque
)
973 bdrv_add_aio_context_notifier(blk
->bs
, attached_aio_context
,
974 detach_aio_context
, opaque
);
977 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
978 void (*attached_aio_context
)(AioContext
*,
980 void (*detach_aio_context
)(void *),
983 bdrv_remove_aio_context_notifier(blk
->bs
, attached_aio_context
,
984 detach_aio_context
, opaque
);
987 void blk_add_close_notifier(BlockBackend
*blk
, Notifier
*notify
)
989 bdrv_add_close_notifier(blk
->bs
, notify
);
992 void blk_io_plug(BlockBackend
*blk
)
994 bdrv_io_plug(blk
->bs
);
997 void blk_io_unplug(BlockBackend
*blk
)
999 bdrv_io_unplug(blk
->bs
);
1002 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
1007 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
1008 BlockCompletionFunc
*cb
, void *opaque
)
1010 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
1013 int coroutine_fn
blk_co_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
1014 int nb_sectors
, BdrvRequestFlags flags
)
1016 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1021 return bdrv_co_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
1024 int blk_write_compressed(BlockBackend
*blk
, int64_t sector_num
,
1025 const uint8_t *buf
, int nb_sectors
)
1027 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1032 return bdrv_write_compressed(blk
->bs
, sector_num
, buf
, nb_sectors
);
1035 int blk_truncate(BlockBackend
*blk
, int64_t offset
)
1037 return bdrv_truncate(blk
->bs
, offset
);
1040 int blk_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
1042 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
1047 return bdrv_discard(blk
->bs
, sector_num
, nb_sectors
);
1050 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
1051 int64_t pos
, int size
)
1053 return bdrv_save_vmstate(blk
->bs
, buf
, pos
, size
);
1056 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
1058 return bdrv_load_vmstate(blk
->bs
, buf
, pos
, size
);
1061 int blk_probe_blocksizes(BlockBackend
*blk
, BlockSizes
*bsz
)
1063 return bdrv_probe_blocksizes(blk
->bs
, bsz
);
1066 int blk_probe_geometry(BlockBackend
*blk
, HDGeometry
*geo
)
1068 return bdrv_probe_geometry(blk
->bs
, geo
);