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 "sysemu/blockdev.h"
16 #include "qapi-event.h"
18 /* Number of coroutines to reserve per attached device model */
19 #define COROUTINE_POOL_RESERVATION 64
25 DriveInfo
*legacy_dinfo
; /* null unless created by drive_new() */
26 QTAILQ_ENTRY(BlockBackend
) link
; /* for blk_backends */
28 void *dev
; /* attached device model, if any */
29 /* TODO change to DeviceState when all users are qdevified */
30 const BlockDevOps
*dev_ops
;
34 typedef struct BlockBackendAIOCB
{
40 static const AIOCBInfo block_backend_aiocb_info
= {
41 .aiocb_size
= sizeof(BlockBackendAIOCB
),
44 static void drive_info_del(DriveInfo
*dinfo
);
46 /* All the BlockBackends (except for hidden ones) */
47 static QTAILQ_HEAD(, BlockBackend
) blk_backends
=
48 QTAILQ_HEAD_INITIALIZER(blk_backends
);
51 * Create a new BlockBackend with @name, with a reference count of one.
52 * @name must not be null or empty.
53 * Fail if a BlockBackend with this name already exists.
54 * Store an error through @errp on failure, unless it's null.
55 * Return the new BlockBackend on success, null on failure.
57 BlockBackend
*blk_new(const char *name
, Error
**errp
)
61 assert(name
&& name
[0]);
62 if (!id_wellformed(name
)) {
63 error_setg(errp
, "Invalid device name");
66 if (blk_by_name(name
)) {
67 error_setg(errp
, "Device with id '%s' already exists", name
);
70 if (bdrv_find_node(name
)) {
72 "Device name '%s' conflicts with an existing node name",
77 blk
= g_new0(BlockBackend
, 1);
78 blk
->name
= g_strdup(name
);
80 QTAILQ_INSERT_TAIL(&blk_backends
, blk
, link
);
85 * Create a new BlockBackend with a new BlockDriverState attached.
86 * Otherwise just like blk_new(), which see.
88 BlockBackend
*blk_new_with_bs(const char *name
, Error
**errp
)
93 blk
= blk_new(name
, errp
);
105 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
107 * Just as with bdrv_open(), after having called this function the reference to
108 * @options belongs to the block layer (even on failure).
110 * TODO: Remove @filename and @flags; it should be possible to specify a whole
111 * BDS tree just by specifying the @options QDict (or @reference,
112 * alternatively). At the time of adding this function, this is not possible,
113 * though, so callers of this function have to be able to specify @filename and
116 BlockBackend
*blk_new_open(const char *name
, const char *filename
,
117 const char *reference
, QDict
*options
, int flags
,
123 blk
= blk_new_with_bs(name
, errp
);
129 ret
= bdrv_open(&blk
->bs
, filename
, reference
, options
, flags
, NULL
, errp
);
138 static void blk_delete(BlockBackend
*blk
)
140 assert(!blk
->refcnt
);
143 assert(blk
->bs
->blk
== blk
);
148 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
150 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
153 drive_info_del(blk
->legacy_dinfo
);
157 static void drive_info_del(DriveInfo
*dinfo
)
162 qemu_opts_del(dinfo
->opts
);
163 g_free(dinfo
->serial
);
168 * Increment @blk's reference count.
169 * @blk must not be null.
171 void blk_ref(BlockBackend
*blk
)
177 * Decrement @blk's reference count.
178 * If this drops it to zero, destroy @blk.
179 * For convenience, do nothing if @blk is null.
181 void blk_unref(BlockBackend
*blk
)
184 assert(blk
->refcnt
> 0);
185 if (!--blk
->refcnt
) {
192 * Return the BlockBackend after @blk.
193 * If @blk is null, return the first one.
194 * Else, return @blk's next sibling, which may be null.
196 * To iterate over all BlockBackends, do
197 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
201 BlockBackend
*blk_next(BlockBackend
*blk
)
203 return blk
? QTAILQ_NEXT(blk
, link
) : QTAILQ_FIRST(&blk_backends
);
207 * Return @blk's name, a non-null string.
208 * Wart: the name is empty iff @blk has been hidden with
209 * blk_hide_on_behalf_of_hmp_drive_del().
211 const char *blk_name(BlockBackend
*blk
)
217 * Return the BlockBackend with name @name if it exists, else null.
218 * @name must not be null.
220 BlockBackend
*blk_by_name(const char *name
)
225 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
226 if (!strcmp(name
, blk
->name
)) {
234 * Return the BlockDriverState attached to @blk if any, else null.
236 BlockDriverState
*blk_bs(BlockBackend
*blk
)
242 * Return @blk's DriveInfo if any, else null.
244 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
246 return blk
->legacy_dinfo
;
250 * Set @blk's DriveInfo to @dinfo, and return it.
251 * @blk must not have a DriveInfo set already.
252 * No other BlockBackend may have the same DriveInfo set.
254 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
256 assert(!blk
->legacy_dinfo
);
257 return blk
->legacy_dinfo
= dinfo
;
261 * Return the BlockBackend with DriveInfo @dinfo.
264 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
268 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
269 if (blk
->legacy_dinfo
== dinfo
) {
278 * @blk must not have been hidden already.
279 * Make attached BlockDriverState, if any, anonymous.
280 * Once hidden, @blk is invisible to all functions that don't receive
281 * it as argument. For example, blk_by_name() won't return it.
282 * Strictly for use by do_drive_del().
283 * TODO get rid of it!
285 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend
*blk
)
287 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
290 bdrv_make_anon(blk
->bs
);
295 * Attach device model @dev to @blk.
296 * Return 0 on success, -EBUSY when a device model is attached already.
298 int blk_attach_dev(BlockBackend
*blk
, void *dev
)
299 /* TODO change to DeviceState *dev when all users are qdevified */
306 bdrv_iostatus_reset(blk
->bs
);
311 * Attach device model @dev to @blk.
312 * @blk must not have a device model attached already.
313 * TODO qdevified devices don't use this, remove when devices are qdevified
315 void blk_attach_dev_nofail(BlockBackend
*blk
, void *dev
)
317 if (blk_attach_dev(blk
, dev
) < 0) {
323 * Detach device model @dev from @blk.
324 * @dev must be currently attached to @blk.
326 void blk_detach_dev(BlockBackend
*blk
, void *dev
)
327 /* TODO change to DeviceState *dev when all users are qdevified */
329 assert(blk
->dev
== dev
);
332 blk
->dev_opaque
= NULL
;
333 bdrv_set_guest_block_size(blk
->bs
, 512);
338 * Return the device model attached to @blk if any, else null.
340 void *blk_get_attached_dev(BlockBackend
*blk
)
341 /* TODO change to return DeviceState * when all users are qdevified */
347 * Set @blk's device model callbacks to @ops.
348 * @opaque is the opaque argument to pass to the callbacks.
349 * This is for use by device models.
351 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
355 blk
->dev_opaque
= opaque
;
359 * Notify @blk's attached device model of media change.
360 * If @load is true, notify of media load.
361 * Else, notify of media eject.
362 * Also send DEVICE_TRAY_MOVED events as appropriate.
364 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
)
366 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
367 bool tray_was_closed
= !blk_dev_is_tray_open(blk
);
369 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
);
370 if (tray_was_closed
) {
372 qapi_event_send_device_tray_moved(blk_name(blk
),
377 qapi_event_send_device_tray_moved(blk_name(blk
),
378 false, &error_abort
);
384 * Does @blk's attached device model have removable media?
385 * %true if no device model is attached.
387 bool blk_dev_has_removable_media(BlockBackend
*blk
)
389 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
393 * Notify @blk's attached device model of a media eject request.
394 * If @force is true, the medium is about to be yanked out forcefully.
396 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
398 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
399 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
404 * Does @blk's attached device model have a tray, and is it open?
406 bool blk_dev_is_tray_open(BlockBackend
*blk
)
408 if (blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
) {
409 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
415 * Does @blk's attached device model have the medium locked?
416 * %false if the device model has no such lock.
418 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
420 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
421 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
427 * Notify @blk's attached device model of a backend size change.
429 void blk_dev_resize_cb(BlockBackend
*blk
)
431 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
432 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
436 void blk_iostatus_enable(BlockBackend
*blk
)
438 bdrv_iostatus_enable(blk
->bs
);
441 static int blk_check_byte_request(BlockBackend
*blk
, int64_t offset
,
446 if (size
> INT_MAX
) {
450 if (!blk_is_inserted(blk
)) {
454 len
= blk_getlength(blk
);
463 if (offset
> len
|| len
- offset
< size
) {
470 static int blk_check_request(BlockBackend
*blk
, int64_t sector_num
,
473 if (sector_num
< 0 || sector_num
> INT64_MAX
/ BDRV_SECTOR_SIZE
) {
477 if (nb_sectors
< 0 || nb_sectors
> INT_MAX
/ BDRV_SECTOR_SIZE
) {
481 return blk_check_byte_request(blk
, sector_num
* BDRV_SECTOR_SIZE
,
482 nb_sectors
* BDRV_SECTOR_SIZE
);
485 int blk_read(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
488 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
493 return bdrv_read(blk
->bs
, sector_num
, buf
, nb_sectors
);
496 int blk_read_unthrottled(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
499 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
504 return bdrv_read_unthrottled(blk
->bs
, sector_num
, buf
, nb_sectors
);
507 int blk_write(BlockBackend
*blk
, int64_t sector_num
, const uint8_t *buf
,
510 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
515 return bdrv_write(blk
->bs
, sector_num
, buf
, nb_sectors
);
518 static void error_callback_bh(void *opaque
)
520 struct BlockBackendAIOCB
*acb
= opaque
;
521 qemu_bh_delete(acb
->bh
);
522 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
526 static BlockAIOCB
*abort_aio_request(BlockBackend
*blk
, BlockCompletionFunc
*cb
,
527 void *opaque
, int ret
)
529 struct BlockBackendAIOCB
*acb
;
532 acb
= blk_aio_get(&block_backend_aiocb_info
, blk
, cb
, opaque
);
535 bh
= aio_bh_new(blk_get_aio_context(blk
), error_callback_bh
, acb
);
537 qemu_bh_schedule(bh
);
542 BlockAIOCB
*blk_aio_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
543 int nb_sectors
, BdrvRequestFlags flags
,
544 BlockCompletionFunc
*cb
, void *opaque
)
546 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
548 return abort_aio_request(blk
, cb
, opaque
, ret
);
551 return bdrv_aio_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
,
555 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int count
)
557 int ret
= blk_check_byte_request(blk
, offset
, count
);
562 return bdrv_pread(blk
->bs
, offset
, buf
, count
);
565 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int count
)
567 int ret
= blk_check_byte_request(blk
, offset
, count
);
572 return bdrv_pwrite(blk
->bs
, offset
, buf
, count
);
575 int64_t blk_getlength(BlockBackend
*blk
)
577 return bdrv_getlength(blk
->bs
);
580 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
582 bdrv_get_geometry(blk
->bs
, nb_sectors_ptr
);
585 int64_t blk_nb_sectors(BlockBackend
*blk
)
587 return bdrv_nb_sectors(blk
->bs
);
590 BlockAIOCB
*blk_aio_readv(BlockBackend
*blk
, int64_t sector_num
,
591 QEMUIOVector
*iov
, int nb_sectors
,
592 BlockCompletionFunc
*cb
, void *opaque
)
594 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
596 return abort_aio_request(blk
, cb
, opaque
, ret
);
599 return bdrv_aio_readv(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
602 BlockAIOCB
*blk_aio_writev(BlockBackend
*blk
, int64_t sector_num
,
603 QEMUIOVector
*iov
, int nb_sectors
,
604 BlockCompletionFunc
*cb
, void *opaque
)
606 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
608 return abort_aio_request(blk
, cb
, opaque
, ret
);
611 return bdrv_aio_writev(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
614 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
615 BlockCompletionFunc
*cb
, void *opaque
)
617 return bdrv_aio_flush(blk
->bs
, cb
, opaque
);
620 BlockAIOCB
*blk_aio_discard(BlockBackend
*blk
,
621 int64_t sector_num
, int nb_sectors
,
622 BlockCompletionFunc
*cb
, void *opaque
)
624 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
626 return abort_aio_request(blk
, cb
, opaque
, ret
);
629 return bdrv_aio_discard(blk
->bs
, sector_num
, nb_sectors
, cb
, opaque
);
632 void blk_aio_cancel(BlockAIOCB
*acb
)
634 bdrv_aio_cancel(acb
);
637 void blk_aio_cancel_async(BlockAIOCB
*acb
)
639 bdrv_aio_cancel_async(acb
);
642 int blk_aio_multiwrite(BlockBackend
*blk
, BlockRequest
*reqs
, int num_reqs
)
646 for (i
= 0; i
< num_reqs
; i
++) {
647 ret
= blk_check_request(blk
, reqs
[i
].sector
, reqs
[i
].nb_sectors
);
653 return bdrv_aio_multiwrite(blk
->bs
, reqs
, num_reqs
);
656 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
658 return bdrv_ioctl(blk
->bs
, req
, buf
);
661 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
662 BlockCompletionFunc
*cb
, void *opaque
)
664 return bdrv_aio_ioctl(blk
->bs
, req
, buf
, cb
, opaque
);
667 int blk_co_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
669 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
674 return bdrv_co_discard(blk
->bs
, sector_num
, nb_sectors
);
677 int blk_co_flush(BlockBackend
*blk
)
679 return bdrv_co_flush(blk
->bs
);
682 int blk_flush(BlockBackend
*blk
)
684 return bdrv_flush(blk
->bs
);
687 int blk_flush_all(void)
689 return bdrv_flush_all();
692 void blk_drain_all(void)
697 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
699 return bdrv_get_on_error(blk
->bs
, is_read
);
702 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
705 return bdrv_get_error_action(blk
->bs
, is_read
, error
);
708 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
709 bool is_read
, int error
)
711 bdrv_error_action(blk
->bs
, action
, is_read
, error
);
714 int blk_is_read_only(BlockBackend
*blk
)
716 return bdrv_is_read_only(blk
->bs
);
719 int blk_is_sg(BlockBackend
*blk
)
721 return bdrv_is_sg(blk
->bs
);
724 int blk_enable_write_cache(BlockBackend
*blk
)
726 return bdrv_enable_write_cache(blk
->bs
);
729 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
731 bdrv_set_enable_write_cache(blk
->bs
, wce
);
734 void blk_invalidate_cache(BlockBackend
*blk
, Error
**errp
)
736 bdrv_invalidate_cache(blk
->bs
, errp
);
739 int blk_is_inserted(BlockBackend
*blk
)
741 return bdrv_is_inserted(blk
->bs
);
744 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
746 bdrv_lock_medium(blk
->bs
, locked
);
749 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
751 bdrv_eject(blk
->bs
, eject_flag
);
754 int blk_get_flags(BlockBackend
*blk
)
756 return bdrv_get_flags(blk
->bs
);
759 int blk_get_max_transfer_length(BlockBackend
*blk
)
761 return blk
->bs
->bl
.max_transfer_length
;
764 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
766 bdrv_set_guest_block_size(blk
->bs
, align
);
769 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
771 return qemu_blockalign(blk
? blk
->bs
: NULL
, size
);
774 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
776 return bdrv_op_is_blocked(blk
->bs
, op
, errp
);
779 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
781 bdrv_op_unblock(blk
->bs
, op
, reason
);
784 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
786 bdrv_op_block_all(blk
->bs
, reason
);
789 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
791 bdrv_op_unblock_all(blk
->bs
, reason
);
794 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
796 return bdrv_get_aio_context(blk
->bs
);
799 void blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
)
801 bdrv_set_aio_context(blk
->bs
, new_context
);
804 void blk_add_aio_context_notifier(BlockBackend
*blk
,
805 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
806 void (*detach_aio_context
)(void *opaque
), void *opaque
)
808 bdrv_add_aio_context_notifier(blk
->bs
, attached_aio_context
,
809 detach_aio_context
, opaque
);
812 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
813 void (*attached_aio_context
)(AioContext
*,
815 void (*detach_aio_context
)(void *),
818 bdrv_remove_aio_context_notifier(blk
->bs
, attached_aio_context
,
819 detach_aio_context
, opaque
);
822 void blk_add_close_notifier(BlockBackend
*blk
, Notifier
*notify
)
824 bdrv_add_close_notifier(blk
->bs
, notify
);
827 void blk_io_plug(BlockBackend
*blk
)
829 bdrv_io_plug(blk
->bs
);
832 void blk_io_unplug(BlockBackend
*blk
)
834 bdrv_io_unplug(blk
->bs
);
837 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
839 return bdrv_get_stats(blk
->bs
);
842 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
843 BlockCompletionFunc
*cb
, void *opaque
)
845 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
848 int coroutine_fn
blk_co_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
849 int nb_sectors
, BdrvRequestFlags flags
)
851 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
856 return bdrv_co_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
859 int blk_write_compressed(BlockBackend
*blk
, int64_t sector_num
,
860 const uint8_t *buf
, int nb_sectors
)
862 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
867 return bdrv_write_compressed(blk
->bs
, sector_num
, buf
, nb_sectors
);
870 int blk_truncate(BlockBackend
*blk
, int64_t offset
)
872 return bdrv_truncate(blk
->bs
, offset
);
875 int blk_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
877 int ret
= blk_check_request(blk
, sector_num
, nb_sectors
);
882 return bdrv_discard(blk
->bs
, sector_num
, nb_sectors
);
885 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
886 int64_t pos
, int size
)
888 return bdrv_save_vmstate(blk
->bs
, buf
, pos
, size
);
891 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
893 return bdrv_load_vmstate(blk
->bs
, buf
, pos
, size
);
896 int blk_probe_blocksizes(BlockBackend
*blk
, BlockSizes
*bsz
)
898 return bdrv_probe_blocksizes(blk
->bs
, bsz
);
901 int blk_probe_geometry(BlockBackend
*blk
, HDGeometry
*geo
)
903 return bdrv_probe_geometry(blk
->bs
, geo
);