4 * Copyright (C) 2014-2016 Red Hat, Inc.
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"
23 /* Number of coroutines to reserve per attached device model */
24 #define COROUTINE_POOL_RESERVATION 64
26 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
28 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
34 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
35 QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */
36 QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
37 BlockBackendPublic public;
39 void *dev; /* attached device model, if any */
40 /* TODO change to DeviceState when all users are qdevified */
41 const BlockDevOps *dev_ops;
44 /* the block size for which the guest device expects atomicity */
47 /* If the BDS tree is removed, some of its options are stored here (which
48 * can be used to restore those options in the new BDS on insert) */
49 BlockBackendRootState root_state;
51 bool enable_write_cache;
53 /* I/O stats (display with "info blockstats"). */
56 BlockdevOnError on_read_error, on_write_error;
57 bool iostatus_enabled;
58 BlockDeviceIoStatus iostatus;
60 bool allow_write_beyond_eof;
62 NotifierList remove_bs_notifiers, insert_bs_notifiers;
65 typedef struct BlockBackendAIOCB {
72 static const AIOCBInfo block_backend_aiocb_info = {
73 .get_aio_context = blk_aiocb_get_aio_context,
74 .aiocb_size = sizeof(BlockBackendAIOCB),
77 static void drive_info_del(DriveInfo *dinfo);
79 /* All BlockBackends */
80 static QTAILQ_HEAD(, BlockBackend) block_backends =
81 QTAILQ_HEAD_INITIALIZER(block_backends);
83 /* All BlockBackends referenced by the monitor and which are iterated through by
85 static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
86 QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
88 static void blk_root_inherit_options(int *child_flags, QDict *child_options,
89 int parent_flags, QDict *parent_options)
91 /* We're not supposed to call this function for root nodes */
95 static const BdrvChildRole child_root = {
96 .inherit_options = blk_root_inherit_options,
100 * Create a new BlockBackend with a reference count of one.
101 * Store an error through @errp on failure, unless it's null.
102 * Return the new BlockBackend on success, null on failure.
104 BlockBackend *blk_new(Error **errp)
108 blk = g_new0(BlockBackend, 1);
110 qemu_co_queue_init(&blk->public.throttled_reqs[0]);
111 qemu_co_queue_init(&blk->public.throttled_reqs[1]);
113 notifier_list_init(&blk->remove_bs_notifiers);
114 notifier_list_init(&blk->insert_bs_notifiers);
116 QTAILQ_INSERT_TAIL(&block_backends, blk, link);
121 * Create a new BlockBackend with a new BlockDriverState attached.
122 * Otherwise just like blk_new(), which see.
124 BlockBackend *blk_new_with_bs(Error **errp)
127 BlockDriverState *bs;
134 bs = bdrv_new_root();
135 blk->root = bdrv_root_attach_child(bs, "root", &child_root);
136 blk->root->opaque = blk;
142 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
144 * Just as with bdrv_open(), after having called this function the reference to
145 * @options belongs to the block layer (even on failure).
147 * TODO: Remove @filename and @flags; it should be possible to specify a whole
148 * BDS tree just by specifying the @options QDict (or @reference,
149 * alternatively). At the time of adding this function, this is not possible,
150 * though, so callers of this function have to be able to specify @filename and
153 BlockBackend *blk_new_open(const char *filename, const char *reference,
154 QDict *options, int flags, Error **errp)
159 blk = blk_new_with_bs(errp);
165 ret = bdrv_open(&blk->root->bs, filename, reference, options, flags, errp);
171 blk_set_enable_write_cache(blk, true);
176 static void blk_delete(BlockBackend *blk)
178 assert(!blk->refcnt);
184 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
185 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
186 if (blk->root_state.throttle_state) {
187 g_free(blk->root_state.throttle_group);
188 throttle_group_unref(blk->root_state.throttle_state);
190 QTAILQ_REMOVE(&block_backends, blk, link);
191 drive_info_del(blk->legacy_dinfo);
192 block_acct_cleanup(&blk->stats);
196 static void drive_info_del(DriveInfo *dinfo)
201 qemu_opts_del(dinfo->opts);
202 g_free(dinfo->serial);
206 int blk_get_refcnt(BlockBackend *blk)
208 return blk ? blk->refcnt : 0;
212 * Increment @blk's reference count.
213 * @blk must not be null.
215 void blk_ref(BlockBackend *blk)
221 * Decrement @blk's reference count.
222 * If this drops it to zero, destroy @blk.
223 * For convenience, do nothing if @blk is null.
225 void blk_unref(BlockBackend *blk)
228 assert(blk->refcnt > 0);
229 if (!--blk->refcnt) {
236 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
237 * ones which are hidden (i.e. are not referenced by the monitor).
239 static BlockBackend *blk_all_next(BlockBackend *blk)
241 return blk ? QTAILQ_NEXT(blk, link)
242 : QTAILQ_FIRST(&block_backends);
245 void blk_remove_all_bs(void)
247 BlockBackend *blk = NULL;
249 while ((blk = blk_all_next(blk)) != NULL) {
250 AioContext *ctx = blk_get_aio_context(blk);
252 aio_context_acquire(ctx);
256 aio_context_release(ctx);
261 * Return the monitor-owned BlockBackend after @blk.
262 * If @blk is null, return the first one.
263 * Else, return @blk's next sibling, which may be null.
265 * To iterate over all BlockBackends, do
266 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
270 BlockBackend *blk_next(BlockBackend *blk)
272 return blk ? QTAILQ_NEXT(blk, monitor_link)
273 : QTAILQ_FIRST(&monitor_block_backends);
277 * Iterates over all BlockDriverStates which are attached to a BlockBackend.
278 * This function is for use by bdrv_next().
280 * @bs must be NULL or a BDS that is attached to a BB.
282 BlockDriverState *blk_next_root_bs(BlockDriverState *bs)
294 blk = blk_all_next(blk);
295 } while (blk && !blk->root);
297 return blk ? blk->root->bs : NULL;
301 * Add a BlockBackend into the list of backends referenced by the monitor, with
302 * the given @name acting as the handle for the monitor.
303 * Strictly for use by blockdev.c.
305 * @name must not be null or empty.
307 * Returns true on success and false on failure. In the latter case, an Error
308 * object is returned through @errp.
310 bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
313 assert(name && name[0]);
315 if (!id_wellformed(name)) {
316 error_setg(errp, "Invalid device name");
319 if (blk_by_name(name)) {
320 error_setg(errp, "Device with id '%s' already exists", name);
323 if (bdrv_find_node(name)) {
325 "Device name '%s' conflicts with an existing node name",
330 blk->name = g_strdup(name);
331 QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
336 * Remove a BlockBackend from the list of backends referenced by the monitor.
337 * Strictly for use by blockdev.c.
339 void monitor_remove_blk(BlockBackend *blk)
345 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
351 * Return @blk's name, a non-null string.
352 * Returns an empty string iff @blk is not referenced by the monitor.
354 const char *blk_name(BlockBackend *blk)
356 return blk->name ?: "";
360 * Return the BlockBackend with name @name if it exists, else null.
361 * @name must not be null.
363 BlockBackend *blk_by_name(const char *name)
365 BlockBackend *blk = NULL;
368 while ((blk = blk_next(blk)) != NULL) {
369 if (!strcmp(name, blk->name)) {
377 * Return the BlockDriverState attached to @blk if any, else null.
379 BlockDriverState *blk_bs(BlockBackend *blk)
381 return blk->root ? blk->root->bs : NULL;
385 * Return @blk's DriveInfo if any, else null.
387 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
389 return blk->legacy_dinfo;
393 * Set @blk's DriveInfo to @dinfo, and return it.
394 * @blk must not have a DriveInfo set already.
395 * No other BlockBackend may have the same DriveInfo set.
397 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
399 assert(!blk->legacy_dinfo);
400 return blk->legacy_dinfo = dinfo;
404 * Return the BlockBackend with DriveInfo @dinfo.
407 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
409 BlockBackend *blk = NULL;
411 while ((blk = blk_next(blk)) != NULL) {
412 if (blk->legacy_dinfo == dinfo) {
420 * Returns a pointer to the publicly accessible fields of @blk.
422 BlockBackendPublic *blk_get_public(BlockBackend *blk)
428 * Returns a BlockBackend given the associated @public fields.
430 BlockBackend *blk_by_public(BlockBackendPublic *public)
432 return container_of(public, BlockBackend, public);
436 * Disassociates the currently associated BlockDriverState from @blk.
438 void blk_remove_bs(BlockBackend *blk)
440 assert(blk->root->bs->blk == blk);
442 notifier_list_notify(&blk->remove_bs_notifiers, blk);
444 blk_update_root_state(blk);
445 if (blk->public.throttle_state) {
446 blk_io_limits_disable(blk);
449 blk->root->bs->blk = NULL;
450 bdrv_root_unref_child(blk->root);
455 * Associates a new BlockDriverState with @blk.
457 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
459 assert(!blk->root && !bs->blk);
461 blk->root = bdrv_root_attach_child(bs, "root", &child_root);
462 blk->root->opaque = blk;
465 notifier_list_notify(&blk->insert_bs_notifiers, blk);
469 * Attach device model @dev to @blk.
470 * Return 0 on success, -EBUSY when a device model is attached already.
472 int blk_attach_dev(BlockBackend *blk, void *dev)
473 /* TODO change to DeviceState *dev when all users are qdevified */
480 blk_iostatus_reset(blk);
485 * Attach device model @dev to @blk.
486 * @blk must not have a device model attached already.
487 * TODO qdevified devices don't use this, remove when devices are qdevified
489 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
491 if (blk_attach_dev(blk, dev) < 0) {
497 * Detach device model @dev from @blk.
498 * @dev must be currently attached to @blk.
500 void blk_detach_dev(BlockBackend *blk, void *dev)
501 /* TODO change to DeviceState *dev when all users are qdevified */
503 assert(blk->dev == dev);
506 blk->dev_opaque = NULL;
507 blk->guest_block_size = 512;
512 * Return the device model attached to @blk if any, else null.
514 void *blk_get_attached_dev(BlockBackend *blk)
515 /* TODO change to return DeviceState * when all users are qdevified */
521 * Set @blk's device model callbacks to @ops.
522 * @opaque is the opaque argument to pass to the callbacks.
523 * This is for use by device models.
525 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
529 blk->dev_opaque = opaque;
533 * Notify @blk's attached device model of media change.
534 * If @load is true, notify of media load.
535 * Else, notify of media eject.
536 * Also send DEVICE_TRAY_MOVED events as appropriate.
538 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
540 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
541 bool tray_was_open, tray_is_open;
543 tray_was_open = blk_dev_is_tray_open(blk);
544 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
545 tray_is_open = blk_dev_is_tray_open(blk);
547 if (tray_was_open != tray_is_open) {
548 qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
555 * Does @blk's attached device model have removable media?
556 * %true if no device model is attached.
558 bool blk_dev_has_removable_media(BlockBackend *blk)
560 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
564 * Does @blk's attached device model have a tray?
566 bool blk_dev_has_tray(BlockBackend *blk)
568 return blk->dev_ops && blk->dev_ops->is_tray_open;
572 * Notify @blk's attached device model of a media eject request.
573 * If @force is true, the medium is about to be yanked out forcefully.
575 void blk_dev_eject_request(BlockBackend *blk, bool force)
577 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
578 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
583 * Does @blk's attached device model have a tray, and is it open?
585 bool blk_dev_is_tray_open(BlockBackend *blk)
587 if (blk_dev_has_tray(blk)) {
588 return blk->dev_ops->is_tray_open(blk->dev_opaque);
594 * Does @blk's attached device model have the medium locked?
595 * %false if the device model has no such lock.
597 bool blk_dev_is_medium_locked(BlockBackend *blk)
599 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
600 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
606 * Notify @blk's attached device model of a backend size change.
608 void blk_dev_resize_cb(BlockBackend *blk)
610 if (blk->dev_ops && blk->dev_ops->resize_cb) {
611 blk->dev_ops->resize_cb(blk->dev_opaque);
615 void blk_iostatus_enable(BlockBackend *blk)
617 blk->iostatus_enabled = true;
618 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
621 /* The I/O status is only enabled if the drive explicitly
622 * enables it _and_ the VM is configured to stop on errors */
623 bool blk_iostatus_is_enabled(const BlockBackend *blk)
625 return (blk->iostatus_enabled &&
626 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
627 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
628 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
631 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
633 return blk->iostatus;
636 void blk_iostatus_disable(BlockBackend *blk)
638 blk->iostatus_enabled = false;
641 void blk_iostatus_reset(BlockBackend *blk)
643 if (blk_iostatus_is_enabled(blk)) {
644 BlockDriverState *bs = blk_bs(blk);
645 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
647 block_job_iostatus_reset(bs->job);
652 void blk_iostatus_set_err(BlockBackend *blk, int error)
654 assert(blk_iostatus_is_enabled(blk));
655 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
656 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
657 BLOCK_DEVICE_IO_STATUS_FAILED;
661 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
663 blk->allow_write_beyond_eof = allow;
666 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
671 if (size > INT_MAX) {
675 if (!blk_is_available(blk)) {
683 if (!blk->allow_write_beyond_eof) {
684 len = blk_getlength(blk);
689 if (offset > len || len - offset < size) {
697 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
700 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
704 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
708 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
709 nb_sectors * BDRV_SECTOR_SIZE);
712 static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
713 unsigned int bytes, QEMUIOVector *qiov,
714 BdrvRequestFlags flags)
716 int ret = blk_check_byte_request(blk, offset, bytes);
721 /* throttling disk I/O */
722 if (blk->public.throttle_state) {
723 throttle_group_co_io_limits_intercept(blk, bytes, false);
726 return bdrv_co_preadv(blk_bs(blk), offset, bytes, qiov, flags);
729 static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
730 unsigned int bytes, QEMUIOVector *qiov,
731 BdrvRequestFlags flags)
735 ret = blk_check_byte_request(blk, offset, bytes);
740 /* throttling disk I/O */
741 if (blk->public.throttle_state) {
742 throttle_group_co_io_limits_intercept(blk, bytes, true);
745 if (!blk->enable_write_cache) {
746 flags |= BDRV_REQ_FUA;
749 return bdrv_co_pwritev(blk_bs(blk), offset, bytes, qiov, flags);
752 typedef struct BlkRwCo {
757 BdrvRequestFlags flags;
760 static void blk_read_entry(void *opaque)
762 BlkRwCo *rwco = opaque;
764 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
765 rwco->qiov, rwco->flags);
768 static void blk_write_entry(void *opaque)
770 BlkRwCo *rwco = opaque;
772 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
773 rwco->qiov, rwco->flags);
776 static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
777 int64_t bytes, CoroutineEntry co_entry,
778 BdrvRequestFlags flags)
780 AioContext *aio_context;
786 iov = (struct iovec) {
790 qemu_iovec_init_external(&qiov, &iov, 1);
800 co = qemu_coroutine_create(co_entry);
801 qemu_coroutine_enter(co, &rwco);
803 aio_context = blk_get_aio_context(blk);
804 while (rwco.ret == NOT_DONE) {
805 aio_poll(aio_context, true);
811 int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf,
816 ret = blk_check_byte_request(blk, offset, count);
821 bdrv_no_throttling_begin(blk_bs(blk));
822 ret = blk_pread(blk, offset, buf, count);
823 bdrv_no_throttling_end(blk_bs(blk));
827 int blk_write_zeroes(BlockBackend *blk, int64_t offset,
828 int count, BdrvRequestFlags flags)
830 return blk_prw(blk, offset, NULL, count, blk_write_entry,
831 flags | BDRV_REQ_ZERO_WRITE);
834 static void error_callback_bh(void *opaque)
836 struct BlockBackendAIOCB *acb = opaque;
837 qemu_bh_delete(acb->bh);
838 acb->common.cb(acb->common.opaque, acb->ret);
842 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
843 BlockCompletionFunc *cb,
844 void *opaque, int ret)
846 struct BlockBackendAIOCB *acb;
849 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
853 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
855 qemu_bh_schedule(bh);
860 typedef struct BlkAioEmAIOCB {
868 static const AIOCBInfo blk_aio_em_aiocb_info = {
869 .aiocb_size = sizeof(BlkAioEmAIOCB),
872 static void blk_aio_complete(BlkAioEmAIOCB *acb)
875 assert(acb->has_returned);
876 qemu_bh_delete(acb->bh);
878 if (acb->has_returned) {
879 acb->common.cb(acb->common.opaque, acb->rwco.ret);
884 static void blk_aio_complete_bh(void *opaque)
886 blk_aio_complete(opaque);
889 static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
890 QEMUIOVector *qiov, CoroutineEntry co_entry,
891 BdrvRequestFlags flags,
892 BlockCompletionFunc *cb, void *opaque)
897 acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
898 acb->rwco = (BlkRwCo) {
907 acb->has_returned = false;
909 co = qemu_coroutine_create(co_entry);
910 qemu_coroutine_enter(co, acb);
912 acb->has_returned = true;
913 if (acb->rwco.ret != NOT_DONE) {
914 acb->bh = aio_bh_new(blk_get_aio_context(blk), blk_aio_complete_bh, acb);
915 qemu_bh_schedule(acb->bh);
921 static void blk_aio_read_entry(void *opaque)
923 BlkAioEmAIOCB *acb = opaque;
924 BlkRwCo *rwco = &acb->rwco;
926 assert(rwco->qiov->size == acb->bytes);
927 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes,
928 rwco->qiov, rwco->flags);
929 blk_aio_complete(acb);
932 static void blk_aio_write_entry(void *opaque)
934 BlkAioEmAIOCB *acb = opaque;
935 BlkRwCo *rwco = &acb->rwco;
937 assert(!rwco->qiov || rwco->qiov->size == acb->bytes);
938 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes,
939 rwco->qiov, rwco->flags);
940 blk_aio_complete(acb);
943 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t offset,
944 int count, BdrvRequestFlags flags,
945 BlockCompletionFunc *cb, void *opaque)
947 return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
948 flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
951 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
953 int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
960 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
961 BdrvRequestFlags flags)
963 int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
971 int64_t blk_getlength(BlockBackend *blk)
973 if (!blk_is_available(blk)) {
977 return bdrv_getlength(blk_bs(blk));
980 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
985 bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
989 int64_t blk_nb_sectors(BlockBackend *blk)
991 if (!blk_is_available(blk)) {
995 return bdrv_nb_sectors(blk_bs(blk));
998 BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
999 QEMUIOVector *qiov, BdrvRequestFlags flags,
1000 BlockCompletionFunc *cb, void *opaque)
1002 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1003 blk_aio_read_entry, flags, cb, opaque);
1006 BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
1007 QEMUIOVector *qiov, BdrvRequestFlags flags,
1008 BlockCompletionFunc *cb, void *opaque)
1010 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1011 blk_aio_write_entry, flags, cb, opaque);
1014 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1015 BlockCompletionFunc *cb, void *opaque)
1017 if (!blk_is_available(blk)) {
1018 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
1021 return bdrv_aio_flush(blk_bs(blk), cb, opaque);
1024 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
1025 int64_t sector_num, int nb_sectors,
1026 BlockCompletionFunc *cb, void *opaque)
1028 int ret = blk_check_request(blk, sector_num, nb_sectors);
1030 return blk_abort_aio_request(blk, cb, opaque, ret);
1033 return bdrv_aio_discard(blk_bs(blk), sector_num, nb_sectors, cb, opaque);
1036 void blk_aio_cancel(BlockAIOCB *acb)
1038 bdrv_aio_cancel(acb);
1041 void blk_aio_cancel_async(BlockAIOCB *acb)
1043 bdrv_aio_cancel_async(acb);
1046 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
1050 for (i = 0; i < num_reqs; i++) {
1051 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
1057 return bdrv_aio_multiwrite(blk_bs(blk), reqs, num_reqs);
1060 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1062 if (!blk_is_available(blk)) {
1066 return bdrv_ioctl(blk_bs(blk), req, buf);
1069 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
1070 BlockCompletionFunc *cb, void *opaque)
1072 if (!blk_is_available(blk)) {
1073 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
1076 return bdrv_aio_ioctl(blk_bs(blk), req, buf, cb, opaque);
1079 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1081 int ret = blk_check_request(blk, sector_num, nb_sectors);
1086 return bdrv_co_discard(blk_bs(blk), sector_num, nb_sectors);
1089 int blk_co_flush(BlockBackend *blk)
1091 if (!blk_is_available(blk)) {
1095 return bdrv_co_flush(blk_bs(blk));
1098 int blk_flush(BlockBackend *blk)
1100 if (!blk_is_available(blk)) {
1104 return bdrv_flush(blk_bs(blk));
1107 void blk_drain(BlockBackend *blk)
1110 bdrv_drain(blk_bs(blk));
1114 void blk_drain_all(void)
1119 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1120 BlockdevOnError on_write_error)
1122 blk->on_read_error = on_read_error;
1123 blk->on_write_error = on_write_error;
1126 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
1128 return is_read ? blk->on_read_error : blk->on_write_error;
1131 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
1134 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1137 case BLOCKDEV_ON_ERROR_ENOSPC:
1138 return (error == ENOSPC) ?
1139 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1140 case BLOCKDEV_ON_ERROR_STOP:
1141 return BLOCK_ERROR_ACTION_STOP;
1142 case BLOCKDEV_ON_ERROR_REPORT:
1143 return BLOCK_ERROR_ACTION_REPORT;
1144 case BLOCKDEV_ON_ERROR_IGNORE:
1145 return BLOCK_ERROR_ACTION_IGNORE;
1151 static void send_qmp_error_event(BlockBackend *blk,
1152 BlockErrorAction action,
1153 bool is_read, int error)
1155 IoOperationType optype;
1157 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
1158 qapi_event_send_block_io_error(blk_name(blk), optype, action,
1159 blk_iostatus_is_enabled(blk),
1160 error == ENOSPC, strerror(error),
1164 /* This is done by device models because, while the block layer knows
1165 * about the error, it does not know whether an operation comes from
1166 * the device or the block layer (from a job, for example).
1168 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1169 bool is_read, int error)
1173 if (action == BLOCK_ERROR_ACTION_STOP) {
1174 /* First set the iostatus, so that "info block" returns an iostatus
1175 * that matches the events raised so far (an additional error iostatus
1176 * is fine, but not a lost one).
1178 blk_iostatus_set_err(blk, error);
1180 /* Then raise the request to stop the VM and the event.
1181 * qemu_system_vmstop_request_prepare has two effects. First,
1182 * it ensures that the STOP event always comes after the
1183 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1184 * can observe the STOP event and do a "cont" before the STOP
1185 * event is issued, the VM will not stop. In this case, vm_start()
1186 * also ensures that the STOP/RESUME pair of events is emitted.
1188 qemu_system_vmstop_request_prepare();
1189 send_qmp_error_event(blk, action, is_read, error);
1190 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1192 send_qmp_error_event(blk, action, is_read, error);
1196 int blk_is_read_only(BlockBackend *blk)
1198 BlockDriverState *bs = blk_bs(blk);
1201 return bdrv_is_read_only(bs);
1203 return blk->root_state.read_only;
1207 int blk_is_sg(BlockBackend *blk)
1209 BlockDriverState *bs = blk_bs(blk);
1215 return bdrv_is_sg(bs);
1218 int blk_enable_write_cache(BlockBackend *blk)
1220 return blk->enable_write_cache;
1223 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1225 blk->enable_write_cache = wce;
1228 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1230 BlockDriverState *bs = blk_bs(blk);
1233 error_setg(errp, "Device '%s' has no medium", blk->name);
1237 bdrv_invalidate_cache(bs, errp);
1240 bool blk_is_inserted(BlockBackend *blk)
1242 BlockDriverState *bs = blk_bs(blk);
1244 return bs && bdrv_is_inserted(bs);
1247 bool blk_is_available(BlockBackend *blk)
1249 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1252 void blk_lock_medium(BlockBackend *blk, bool locked)
1254 BlockDriverState *bs = blk_bs(blk);
1257 bdrv_lock_medium(bs, locked);
1261 void blk_eject(BlockBackend *blk, bool eject_flag)
1263 BlockDriverState *bs = blk_bs(blk);
1266 bdrv_eject(bs, eject_flag);
1270 int blk_get_flags(BlockBackend *blk)
1272 BlockDriverState *bs = blk_bs(blk);
1275 return bdrv_get_flags(bs);
1277 return blk->root_state.open_flags;
1281 int blk_get_max_transfer_length(BlockBackend *blk)
1283 BlockDriverState *bs = blk_bs(blk);
1286 return bs->bl.max_transfer_length;
1292 int blk_get_max_iov(BlockBackend *blk)
1294 return blk->root->bs->bl.max_iov;
1297 void blk_set_guest_block_size(BlockBackend *blk, int align)
1299 blk->guest_block_size = align;
1302 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1304 return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
1307 void *blk_blockalign(BlockBackend *blk, size_t size)
1309 return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
1312 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1314 BlockDriverState *bs = blk_bs(blk);
1320 return bdrv_op_is_blocked(bs, op, errp);
1323 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1325 BlockDriverState *bs = blk_bs(blk);
1328 bdrv_op_unblock(bs, op, reason);
1332 void blk_op_block_all(BlockBackend *blk, Error *reason)
1334 BlockDriverState *bs = blk_bs(blk);
1337 bdrv_op_block_all(bs, reason);
1341 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1343 BlockDriverState *bs = blk_bs(blk);
1346 bdrv_op_unblock_all(bs, reason);
1350 AioContext *blk_get_aio_context(BlockBackend *blk)
1352 BlockDriverState *bs = blk_bs(blk);
1355 return bdrv_get_aio_context(bs);
1357 return qemu_get_aio_context();
1361 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1363 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1364 return blk_get_aio_context(blk_acb->blk);
1367 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1369 BlockDriverState *bs = blk_bs(blk);
1372 bdrv_set_aio_context(bs, new_context);
1376 void blk_add_aio_context_notifier(BlockBackend *blk,
1377 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1378 void (*detach_aio_context)(void *opaque), void *opaque)
1380 BlockDriverState *bs = blk_bs(blk);
1383 bdrv_add_aio_context_notifier(bs, attached_aio_context,
1384 detach_aio_context, opaque);
1388 void blk_remove_aio_context_notifier(BlockBackend *blk,
1389 void (*attached_aio_context)(AioContext *,
1391 void (*detach_aio_context)(void *),
1394 BlockDriverState *bs = blk_bs(blk);
1397 bdrv_remove_aio_context_notifier(bs, attached_aio_context,
1398 detach_aio_context, opaque);
1402 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1404 notifier_list_add(&blk->remove_bs_notifiers, notify);
1407 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1409 notifier_list_add(&blk->insert_bs_notifiers, notify);
1412 void blk_io_plug(BlockBackend *blk)
1414 BlockDriverState *bs = blk_bs(blk);
1421 void blk_io_unplug(BlockBackend *blk)
1423 BlockDriverState *bs = blk_bs(blk);
1430 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1435 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1436 BlockCompletionFunc *cb, void *opaque)
1438 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1441 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t offset,
1442 int count, BdrvRequestFlags flags)
1444 return blk_co_pwritev(blk, offset, count, NULL,
1445 flags | BDRV_REQ_ZERO_WRITE);
1448 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1449 const uint8_t *buf, int nb_sectors)
1451 int ret = blk_check_request(blk, sector_num, nb_sectors);
1456 return bdrv_write_compressed(blk_bs(blk), sector_num, buf, nb_sectors);
1459 int blk_truncate(BlockBackend *blk, int64_t offset)
1461 if (!blk_is_available(blk)) {
1465 return bdrv_truncate(blk_bs(blk), offset);
1468 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1470 int ret = blk_check_request(blk, sector_num, nb_sectors);
1475 return bdrv_discard(blk_bs(blk), sector_num, nb_sectors);
1478 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1479 int64_t pos, int size)
1483 if (!blk_is_available(blk)) {
1487 ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
1492 if (ret == size && !blk->enable_write_cache) {
1493 ret = bdrv_flush(blk_bs(blk));
1496 return ret < 0 ? ret : size;
1499 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1501 if (!blk_is_available(blk)) {
1505 return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
1508 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1510 if (!blk_is_available(blk)) {
1514 return bdrv_probe_blocksizes(blk_bs(blk), bsz);
1517 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1519 if (!blk_is_available(blk)) {
1523 return bdrv_probe_geometry(blk_bs(blk), geo);
1527 * Updates the BlockBackendRootState object with data from the currently
1528 * attached BlockDriverState.
1530 void blk_update_root_state(BlockBackend *blk)
1534 blk->root_state.open_flags = blk->root->bs->open_flags;
1535 blk->root_state.read_only = blk->root->bs->read_only;
1536 blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
1538 if (blk->root_state.throttle_group) {
1539 g_free(blk->root_state.throttle_group);
1540 throttle_group_unref(blk->root_state.throttle_state);
1542 if (blk->public.throttle_state) {
1543 const char *name = throttle_group_get_name(blk);
1544 blk->root_state.throttle_group = g_strdup(name);
1545 blk->root_state.throttle_state = throttle_group_incref(name);
1547 blk->root_state.throttle_group = NULL;
1548 blk->root_state.throttle_state = NULL;
1553 * Applies the information in the root state to the given BlockDriverState. This
1554 * does not include the flags which have to be specified for bdrv_open(), use
1555 * blk_get_open_flags_from_root_state() to inquire them.
1557 void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1559 bs->detect_zeroes = blk->root_state.detect_zeroes;
1560 if (blk->root_state.throttle_group) {
1561 blk_io_limits_enable(blk, blk->root_state.throttle_group);
1566 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1567 * supposed to inherit the root state.
1569 int blk_get_open_flags_from_root_state(BlockBackend *blk)
1573 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1574 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1579 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1581 return &blk->root_state;
1584 int blk_commit_all(void)
1586 BlockBackend *blk = NULL;
1588 while ((blk = blk_all_next(blk)) != NULL) {
1589 AioContext *aio_context = blk_get_aio_context(blk);
1591 aio_context_acquire(aio_context);
1592 if (blk_is_inserted(blk) && blk->root->bs->backing) {
1593 int ret = bdrv_commit(blk->root->bs);
1595 aio_context_release(aio_context);
1599 aio_context_release(aio_context);
1604 int blk_flush_all(void)
1606 BlockBackend *blk = NULL;
1609 while ((blk = blk_all_next(blk)) != NULL) {
1610 AioContext *aio_context = blk_get_aio_context(blk);
1613 aio_context_acquire(aio_context);
1614 if (blk_is_inserted(blk)) {
1615 ret = blk_flush(blk);
1616 if (ret < 0 && !result) {
1620 aio_context_release(aio_context);
1627 /* throttling disk I/O limits */
1628 void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
1630 throttle_group_config(blk, cfg);
1633 void blk_io_limits_disable(BlockBackend *blk)
1635 assert(blk->public.throttle_state);
1636 bdrv_no_throttling_begin(blk_bs(blk));
1637 throttle_group_unregister_blk(blk);
1638 bdrv_no_throttling_end(blk_bs(blk));
1641 /* should be called before blk_set_io_limits if a limit is set */
1642 void blk_io_limits_enable(BlockBackend *blk, const char *group)
1644 assert(!blk->public.throttle_state);
1645 throttle_group_register_blk(blk, group);
1648 void blk_io_limits_update_group(BlockBackend *blk, const char *group)
1650 /* this BB is not part of any group */
1651 if (!blk->public.throttle_state) {
1655 /* this BB is a part of the same group than the one we want */
1656 if (!g_strcmp0(throttle_group_get_name(blk), group)) {
1660 /* need to change the group this bs belong to */
1661 blk_io_limits_disable(blk);
1662 blk_io_limits_enable(blk, group);