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 static void drive_info_del(DriveInfo
*dinfo
);
36 /* All the BlockBackends (except for hidden ones) */
37 static QTAILQ_HEAD(, BlockBackend
) blk_backends
=
38 QTAILQ_HEAD_INITIALIZER(blk_backends
);
41 * Create a new BlockBackend with @name, with a reference count of one.
42 * @name must not be null or empty.
43 * Fail if a BlockBackend with this name already exists.
44 * Store an error through @errp on failure, unless it's null.
45 * Return the new BlockBackend on success, null on failure.
47 BlockBackend
*blk_new(const char *name
, Error
**errp
)
51 assert(name
&& name
[0]);
52 if (!id_wellformed(name
)) {
53 error_setg(errp
, "Invalid device name");
56 if (blk_by_name(name
)) {
57 error_setg(errp
, "Device with id '%s' already exists", name
);
60 if (bdrv_find_node(name
)) {
62 "Device name '%s' conflicts with an existing node name",
67 blk
= g_new0(BlockBackend
, 1);
68 blk
->name
= g_strdup(name
);
70 QTAILQ_INSERT_TAIL(&blk_backends
, blk
, link
);
75 * Create a new BlockBackend with a new BlockDriverState attached.
76 * Otherwise just like blk_new(), which see.
78 BlockBackend
*blk_new_with_bs(const char *name
, Error
**errp
)
83 blk
= blk_new(name
, errp
);
95 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
97 * Just as with bdrv_open(), after having called this function the reference to
98 * @options belongs to the block layer (even on failure).
100 * TODO: Remove @filename and @flags; it should be possible to specify a whole
101 * BDS tree just by specifying the @options QDict (or @reference,
102 * alternatively). At the time of adding this function, this is not possible,
103 * though, so callers of this function have to be able to specify @filename and
106 BlockBackend
*blk_new_open(const char *name
, const char *filename
,
107 const char *reference
, QDict
*options
, int flags
,
113 blk
= blk_new_with_bs(name
, errp
);
119 ret
= bdrv_open(&blk
->bs
, filename
, reference
, options
, flags
, NULL
, errp
);
128 static void blk_delete(BlockBackend
*blk
)
130 assert(!blk
->refcnt
);
133 assert(blk
->bs
->blk
== blk
);
138 /* Avoid double-remove after blk_hide_on_behalf_of_do_drive_del() */
140 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
143 drive_info_del(blk
->legacy_dinfo
);
147 static void drive_info_del(DriveInfo
*dinfo
)
152 qemu_opts_del(dinfo
->opts
);
153 g_free(dinfo
->serial
);
158 * Increment @blk's reference count.
159 * @blk must not be null.
161 void blk_ref(BlockBackend
*blk
)
167 * Decrement @blk's reference count.
168 * If this drops it to zero, destroy @blk.
169 * For convenience, do nothing if @blk is null.
171 void blk_unref(BlockBackend
*blk
)
174 assert(blk
->refcnt
> 0);
175 if (!--blk
->refcnt
) {
182 * Return the BlockBackend after @blk.
183 * If @blk is null, return the first one.
184 * Else, return @blk's next sibling, which may be null.
186 * To iterate over all BlockBackends, do
187 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
191 BlockBackend
*blk_next(BlockBackend
*blk
)
193 return blk
? QTAILQ_NEXT(blk
, link
) : QTAILQ_FIRST(&blk_backends
);
197 * Return @blk's name, a non-null string.
198 * Wart: the name is empty iff @blk has been hidden with
199 * blk_hide_on_behalf_of_do_drive_del().
201 const char *blk_name(BlockBackend
*blk
)
207 * Return the BlockBackend with name @name if it exists, else null.
208 * @name must not be null.
210 BlockBackend
*blk_by_name(const char *name
)
215 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
216 if (!strcmp(name
, blk
->name
)) {
224 * Return the BlockDriverState attached to @blk if any, else null.
226 BlockDriverState
*blk_bs(BlockBackend
*blk
)
232 * Return @blk's DriveInfo if any, else null.
234 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
236 return blk
->legacy_dinfo
;
240 * Set @blk's DriveInfo to @dinfo, and return it.
241 * @blk must not have a DriveInfo set already.
242 * No other BlockBackend may have the same DriveInfo set.
244 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
246 assert(!blk
->legacy_dinfo
);
247 return blk
->legacy_dinfo
= dinfo
;
251 * Return the BlockBackend with DriveInfo @dinfo.
254 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
258 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
259 if (blk
->legacy_dinfo
== dinfo
) {
268 * @blk must not have been hidden already.
269 * Make attached BlockDriverState, if any, anonymous.
270 * Once hidden, @blk is invisible to all functions that don't receive
271 * it as argument. For example, blk_by_name() won't return it.
272 * Strictly for use by do_drive_del().
273 * TODO get rid of it!
275 void blk_hide_on_behalf_of_do_drive_del(BlockBackend
*blk
)
277 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
280 bdrv_make_anon(blk
->bs
);
285 * Attach device model @dev to @blk.
286 * Return 0 on success, -EBUSY when a device model is attached already.
288 int blk_attach_dev(BlockBackend
*blk
, void *dev
)
289 /* TODO change to DeviceState *dev when all users are qdevified */
296 bdrv_iostatus_reset(blk
->bs
);
301 * Attach device model @dev to @blk.
302 * @blk must not have a device model attached already.
303 * TODO qdevified devices don't use this, remove when devices are qdevified
305 void blk_attach_dev_nofail(BlockBackend
*blk
, void *dev
)
307 if (blk_attach_dev(blk
, dev
) < 0) {
313 * Detach device model @dev from @blk.
314 * @dev must be currently attached to @blk.
316 void blk_detach_dev(BlockBackend
*blk
, void *dev
)
317 /* TODO change to DeviceState *dev when all users are qdevified */
319 assert(blk
->dev
== dev
);
322 blk
->dev_opaque
= NULL
;
323 bdrv_set_guest_block_size(blk
->bs
, 512);
328 * Return the device model attached to @blk if any, else null.
330 void *blk_get_attached_dev(BlockBackend
*blk
)
331 /* TODO change to return DeviceState * when all users are qdevified */
337 * Set @blk's device model callbacks to @ops.
338 * @opaque is the opaque argument to pass to the callbacks.
339 * This is for use by device models.
341 void blk_set_dev_ops(BlockBackend
*blk
, const BlockDevOps
*ops
,
345 blk
->dev_opaque
= opaque
;
349 * Notify @blk's attached device model of media change.
350 * If @load is true, notify of media load.
351 * Else, notify of media eject.
352 * Also send DEVICE_TRAY_MOVED events as appropriate.
354 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
)
356 if (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
) {
357 bool tray_was_closed
= !blk_dev_is_tray_open(blk
);
359 blk
->dev_ops
->change_media_cb(blk
->dev_opaque
, load
);
360 if (tray_was_closed
) {
362 qapi_event_send_device_tray_moved(blk_name(blk
),
367 qapi_event_send_device_tray_moved(blk_name(blk
),
368 false, &error_abort
);
374 * Does @blk's attached device model have removable media?
375 * %true if no device model is attached.
377 bool blk_dev_has_removable_media(BlockBackend
*blk
)
379 return !blk
->dev
|| (blk
->dev_ops
&& blk
->dev_ops
->change_media_cb
);
383 * Notify @blk's attached device model of a media eject request.
384 * If @force is true, the medium is about to be yanked out forcefully.
386 void blk_dev_eject_request(BlockBackend
*blk
, bool force
)
388 if (blk
->dev_ops
&& blk
->dev_ops
->eject_request_cb
) {
389 blk
->dev_ops
->eject_request_cb(blk
->dev_opaque
, force
);
394 * Does @blk's attached device model have a tray, and is it open?
396 bool blk_dev_is_tray_open(BlockBackend
*blk
)
398 if (blk
->dev_ops
&& blk
->dev_ops
->is_tray_open
) {
399 return blk
->dev_ops
->is_tray_open(blk
->dev_opaque
);
405 * Does @blk's attached device model have the medium locked?
406 * %false if the device model has no such lock.
408 bool blk_dev_is_medium_locked(BlockBackend
*blk
)
410 if (blk
->dev_ops
&& blk
->dev_ops
->is_medium_locked
) {
411 return blk
->dev_ops
->is_medium_locked(blk
->dev_opaque
);
417 * Notify @blk's attached device model of a backend size change.
419 void blk_dev_resize_cb(BlockBackend
*blk
)
421 if (blk
->dev_ops
&& blk
->dev_ops
->resize_cb
) {
422 blk
->dev_ops
->resize_cb(blk
->dev_opaque
);
426 void blk_iostatus_enable(BlockBackend
*blk
)
428 bdrv_iostatus_enable(blk
->bs
);
431 int blk_read(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
434 return bdrv_read(blk
->bs
, sector_num
, buf
, nb_sectors
);
437 int blk_read_unthrottled(BlockBackend
*blk
, int64_t sector_num
, uint8_t *buf
,
440 return bdrv_read_unthrottled(blk
->bs
, sector_num
, buf
, nb_sectors
);
443 int blk_write(BlockBackend
*blk
, int64_t sector_num
, const uint8_t *buf
,
446 return bdrv_write(blk
->bs
, sector_num
, buf
, nb_sectors
);
449 BlockAIOCB
*blk_aio_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
450 int nb_sectors
, BdrvRequestFlags flags
,
451 BlockCompletionFunc
*cb
, void *opaque
)
453 return bdrv_aio_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
,
457 int blk_pread(BlockBackend
*blk
, int64_t offset
, void *buf
, int count
)
459 return bdrv_pread(blk
->bs
, offset
, buf
, count
);
462 int blk_pwrite(BlockBackend
*blk
, int64_t offset
, const void *buf
, int count
)
464 return bdrv_pwrite(blk
->bs
, offset
, buf
, count
);
467 int64_t blk_getlength(BlockBackend
*blk
)
469 return bdrv_getlength(blk
->bs
);
472 void blk_get_geometry(BlockBackend
*blk
, uint64_t *nb_sectors_ptr
)
474 bdrv_get_geometry(blk
->bs
, nb_sectors_ptr
);
477 int64_t blk_nb_sectors(BlockBackend
*blk
)
479 return bdrv_nb_sectors(blk
->bs
);
482 BlockAIOCB
*blk_aio_readv(BlockBackend
*blk
, int64_t sector_num
,
483 QEMUIOVector
*iov
, int nb_sectors
,
484 BlockCompletionFunc
*cb
, void *opaque
)
486 return bdrv_aio_readv(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
489 BlockAIOCB
*blk_aio_writev(BlockBackend
*blk
, int64_t sector_num
,
490 QEMUIOVector
*iov
, int nb_sectors
,
491 BlockCompletionFunc
*cb
, void *opaque
)
493 return bdrv_aio_writev(blk
->bs
, sector_num
, iov
, nb_sectors
, cb
, opaque
);
496 BlockAIOCB
*blk_aio_flush(BlockBackend
*blk
,
497 BlockCompletionFunc
*cb
, void *opaque
)
499 return bdrv_aio_flush(blk
->bs
, cb
, opaque
);
502 BlockAIOCB
*blk_aio_discard(BlockBackend
*blk
,
503 int64_t sector_num
, int nb_sectors
,
504 BlockCompletionFunc
*cb
, void *opaque
)
506 return bdrv_aio_discard(blk
->bs
, sector_num
, nb_sectors
, cb
, opaque
);
509 void blk_aio_cancel(BlockAIOCB
*acb
)
511 bdrv_aio_cancel(acb
);
514 void blk_aio_cancel_async(BlockAIOCB
*acb
)
516 bdrv_aio_cancel_async(acb
);
519 int blk_aio_multiwrite(BlockBackend
*blk
, BlockRequest
*reqs
, int num_reqs
)
521 return bdrv_aio_multiwrite(blk
->bs
, reqs
, num_reqs
);
524 int blk_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
)
526 return bdrv_ioctl(blk
->bs
, req
, buf
);
529 BlockAIOCB
*blk_aio_ioctl(BlockBackend
*blk
, unsigned long int req
, void *buf
,
530 BlockCompletionFunc
*cb
, void *opaque
)
532 return bdrv_aio_ioctl(blk
->bs
, req
, buf
, cb
, opaque
);
535 int blk_co_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
537 return bdrv_co_discard(blk
->bs
, sector_num
, nb_sectors
);
540 int blk_co_flush(BlockBackend
*blk
)
542 return bdrv_co_flush(blk
->bs
);
545 int blk_flush(BlockBackend
*blk
)
547 return bdrv_flush(blk
->bs
);
550 int blk_flush_all(void)
552 return bdrv_flush_all();
555 void blk_drain_all(void)
560 BlockdevOnError
blk_get_on_error(BlockBackend
*blk
, bool is_read
)
562 return bdrv_get_on_error(blk
->bs
, is_read
);
565 BlockErrorAction
blk_get_error_action(BlockBackend
*blk
, bool is_read
,
568 return bdrv_get_error_action(blk
->bs
, is_read
, error
);
571 void blk_error_action(BlockBackend
*blk
, BlockErrorAction action
,
572 bool is_read
, int error
)
574 bdrv_error_action(blk
->bs
, action
, is_read
, error
);
577 int blk_is_read_only(BlockBackend
*blk
)
579 return bdrv_is_read_only(blk
->bs
);
582 int blk_is_sg(BlockBackend
*blk
)
584 return bdrv_is_sg(blk
->bs
);
587 int blk_enable_write_cache(BlockBackend
*blk
)
589 return bdrv_enable_write_cache(blk
->bs
);
592 void blk_set_enable_write_cache(BlockBackend
*blk
, bool wce
)
594 bdrv_set_enable_write_cache(blk
->bs
, wce
);
597 void blk_invalidate_cache(BlockBackend
*blk
, Error
**errp
)
599 bdrv_invalidate_cache(blk
->bs
, errp
);
602 int blk_is_inserted(BlockBackend
*blk
)
604 return bdrv_is_inserted(blk
->bs
);
607 void blk_lock_medium(BlockBackend
*blk
, bool locked
)
609 bdrv_lock_medium(blk
->bs
, locked
);
612 void blk_eject(BlockBackend
*blk
, bool eject_flag
)
614 bdrv_eject(blk
->bs
, eject_flag
);
617 int blk_get_flags(BlockBackend
*blk
)
619 return bdrv_get_flags(blk
->bs
);
622 int blk_get_max_transfer_length(BlockBackend
*blk
)
624 return blk
->bs
->bl
.max_transfer_length
;
627 void blk_set_guest_block_size(BlockBackend
*blk
, int align
)
629 bdrv_set_guest_block_size(blk
->bs
, align
);
632 void *blk_blockalign(BlockBackend
*blk
, size_t size
)
634 return qemu_blockalign(blk
? blk
->bs
: NULL
, size
);
637 bool blk_op_is_blocked(BlockBackend
*blk
, BlockOpType op
, Error
**errp
)
639 return bdrv_op_is_blocked(blk
->bs
, op
, errp
);
642 void blk_op_unblock(BlockBackend
*blk
, BlockOpType op
, Error
*reason
)
644 bdrv_op_unblock(blk
->bs
, op
, reason
);
647 void blk_op_block_all(BlockBackend
*blk
, Error
*reason
)
649 bdrv_op_block_all(blk
->bs
, reason
);
652 void blk_op_unblock_all(BlockBackend
*blk
, Error
*reason
)
654 bdrv_op_unblock_all(blk
->bs
, reason
);
657 AioContext
*blk_get_aio_context(BlockBackend
*blk
)
659 return bdrv_get_aio_context(blk
->bs
);
662 void blk_set_aio_context(BlockBackend
*blk
, AioContext
*new_context
)
664 bdrv_set_aio_context(blk
->bs
, new_context
);
667 void blk_add_aio_context_notifier(BlockBackend
*blk
,
668 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
669 void (*detach_aio_context
)(void *opaque
), void *opaque
)
671 bdrv_add_aio_context_notifier(blk
->bs
, attached_aio_context
,
672 detach_aio_context
, opaque
);
675 void blk_remove_aio_context_notifier(BlockBackend
*blk
,
676 void (*attached_aio_context
)(AioContext
*,
678 void (*detach_aio_context
)(void *),
681 bdrv_remove_aio_context_notifier(blk
->bs
, attached_aio_context
,
682 detach_aio_context
, opaque
);
685 void blk_add_close_notifier(BlockBackend
*blk
, Notifier
*notify
)
687 bdrv_add_close_notifier(blk
->bs
, notify
);
690 void blk_io_plug(BlockBackend
*blk
)
692 bdrv_io_plug(blk
->bs
);
695 void blk_io_unplug(BlockBackend
*blk
)
697 bdrv_io_unplug(blk
->bs
);
700 BlockAcctStats
*blk_get_stats(BlockBackend
*blk
)
702 return bdrv_get_stats(blk
->bs
);
705 void *blk_aio_get(const AIOCBInfo
*aiocb_info
, BlockBackend
*blk
,
706 BlockCompletionFunc
*cb
, void *opaque
)
708 return qemu_aio_get(aiocb_info
, blk_bs(blk
), cb
, opaque
);
711 int coroutine_fn
blk_co_write_zeroes(BlockBackend
*blk
, int64_t sector_num
,
712 int nb_sectors
, BdrvRequestFlags flags
)
714 return bdrv_co_write_zeroes(blk
->bs
, sector_num
, nb_sectors
, flags
);
717 int blk_write_compressed(BlockBackend
*blk
, int64_t sector_num
,
718 const uint8_t *buf
, int nb_sectors
)
720 return bdrv_write_compressed(blk
->bs
, sector_num
, buf
, nb_sectors
);
723 int blk_truncate(BlockBackend
*blk
, int64_t offset
)
725 return bdrv_truncate(blk
->bs
, offset
);
728 int blk_discard(BlockBackend
*blk
, int64_t sector_num
, int nb_sectors
)
730 return bdrv_discard(blk
->bs
, sector_num
, nb_sectors
);
733 int blk_save_vmstate(BlockBackend
*blk
, const uint8_t *buf
,
734 int64_t pos
, int size
)
736 return bdrv_save_vmstate(blk
->bs
, buf
, pos
, size
);
739 int blk_load_vmstate(BlockBackend
*blk
, uint8_t *buf
, int64_t pos
, int size
)
741 return bdrv_load_vmstate(blk
->bs
, buf
, pos
, size
);