blockdev: Separate BB name management
[qemu/ar7.git] / block / block-backend.c
bloba5b950c77133cda802bdbf991deba247e07134e2
1 /*
2 * QEMU Block backends
4 * Copyright (C) 2014 Red Hat, Inc.
6 * Authors:
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 "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"
22 /* Number of coroutines to reserve per attached device model */
23 #define COROUTINE_POOL_RESERVATION 64
25 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
27 struct BlockBackend {
28 char *name;
29 int refcnt;
30 BlockDriverState *bs;
31 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
32 QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */
33 QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
35 void *dev; /* attached device model, if any */
36 /* TODO change to DeviceState when all users are qdevified */
37 const BlockDevOps *dev_ops;
38 void *dev_opaque;
40 /* the block size for which the guest device expects atomicity */
41 int guest_block_size;
43 /* If the BDS tree is removed, some of its options are stored here (which
44 * can be used to restore those options in the new BDS on insert) */
45 BlockBackendRootState root_state;
47 /* I/O stats (display with "info blockstats"). */
48 BlockAcctStats stats;
50 BlockdevOnError on_read_error, on_write_error;
51 bool iostatus_enabled;
52 BlockDeviceIoStatus iostatus;
54 bool allow_write_beyond_eof;
56 NotifierList remove_bs_notifiers, insert_bs_notifiers;
59 typedef struct BlockBackendAIOCB {
60 BlockAIOCB common;
61 QEMUBH *bh;
62 BlockBackend *blk;
63 int ret;
64 } BlockBackendAIOCB;
66 static const AIOCBInfo block_backend_aiocb_info = {
67 .get_aio_context = blk_aiocb_get_aio_context,
68 .aiocb_size = sizeof(BlockBackendAIOCB),
71 static void drive_info_del(DriveInfo *dinfo);
73 /* All BlockBackends */
74 static QTAILQ_HEAD(, BlockBackend) block_backends =
75 QTAILQ_HEAD_INITIALIZER(block_backends);
77 /* All BlockBackends referenced by the monitor and which are iterated through by
78 * blk_next() */
79 static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
80 QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
83 * Create a new BlockBackend with @name, with a reference count of one.
84 * @name must not be null or empty.
85 * Fail if a BlockBackend with this name already exists.
86 * Store an error through @errp on failure, unless it's null.
87 * Return the new BlockBackend on success, null on failure.
89 BlockBackend *blk_new(const char *name, Error **errp)
91 BlockBackend *blk;
93 blk = g_new0(BlockBackend, 1);
94 blk->refcnt = 1;
95 notifier_list_init(&blk->remove_bs_notifiers);
96 notifier_list_init(&blk->insert_bs_notifiers);
98 QTAILQ_INSERT_TAIL(&block_backends, blk, link);
100 if (!monitor_add_blk(blk, name, errp)) {
101 blk_unref(blk);
102 return NULL;
105 return blk;
109 * Create a new BlockBackend with a new BlockDriverState attached.
110 * Otherwise just like blk_new(), which see.
112 BlockBackend *blk_new_with_bs(const char *name, Error **errp)
114 BlockBackend *blk;
115 BlockDriverState *bs;
117 blk = blk_new(name, errp);
118 if (!blk) {
119 return NULL;
122 bs = bdrv_new_root();
123 blk->bs = bs;
124 bs->blk = blk;
125 return blk;
129 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
131 * Just as with bdrv_open(), after having called this function the reference to
132 * @options belongs to the block layer (even on failure).
134 * TODO: Remove @filename and @flags; it should be possible to specify a whole
135 * BDS tree just by specifying the @options QDict (or @reference,
136 * alternatively). At the time of adding this function, this is not possible,
137 * though, so callers of this function have to be able to specify @filename and
138 * @flags.
140 BlockBackend *blk_new_open(const char *name, const char *filename,
141 const char *reference, QDict *options, int flags,
142 Error **errp)
144 BlockBackend *blk;
145 int ret;
147 blk = blk_new_with_bs(name, errp);
148 if (!blk) {
149 QDECREF(options);
150 return NULL;
153 ret = bdrv_open(&blk->bs, filename, reference, options, flags, errp);
154 if (ret < 0) {
155 blk_unref(blk);
156 return NULL;
159 return blk;
162 static void blk_delete(BlockBackend *blk)
164 monitor_remove_blk(blk);
166 assert(!blk->refcnt);
167 assert(!blk->name);
168 assert(!blk->dev);
169 if (blk->bs) {
170 blk_remove_bs(blk);
172 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
173 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
174 if (blk->root_state.throttle_state) {
175 g_free(blk->root_state.throttle_group);
176 throttle_group_unref(blk->root_state.throttle_state);
178 QTAILQ_REMOVE(&block_backends, blk, link);
179 drive_info_del(blk->legacy_dinfo);
180 block_acct_cleanup(&blk->stats);
181 g_free(blk);
184 static void drive_info_del(DriveInfo *dinfo)
186 if (!dinfo) {
187 return;
189 qemu_opts_del(dinfo->opts);
190 g_free(dinfo->serial);
191 g_free(dinfo);
194 int blk_get_refcnt(BlockBackend *blk)
196 return blk ? blk->refcnt : 0;
200 * Increment @blk's reference count.
201 * @blk must not be null.
203 void blk_ref(BlockBackend *blk)
205 blk->refcnt++;
209 * Decrement @blk's reference count.
210 * If this drops it to zero, destroy @blk.
211 * For convenience, do nothing if @blk is null.
213 void blk_unref(BlockBackend *blk)
215 if (blk) {
216 assert(blk->refcnt > 0);
217 if (!--blk->refcnt) {
218 blk_delete(blk);
224 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
225 * ones which are hidden (i.e. are not referenced by the monitor).
227 static BlockBackend *blk_all_next(BlockBackend *blk)
229 return blk ? QTAILQ_NEXT(blk, link)
230 : QTAILQ_FIRST(&block_backends);
233 void blk_remove_all_bs(void)
235 BlockBackend *blk = NULL;
237 while ((blk = blk_all_next(blk)) != NULL) {
238 AioContext *ctx = blk_get_aio_context(blk);
240 aio_context_acquire(ctx);
241 if (blk->bs) {
242 blk_remove_bs(blk);
244 aio_context_release(ctx);
249 * Return the monitor-owned BlockBackend after @blk.
250 * If @blk is null, return the first one.
251 * Else, return @blk's next sibling, which may be null.
253 * To iterate over all BlockBackends, do
254 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
255 * ...
258 BlockBackend *blk_next(BlockBackend *blk)
260 return blk ? QTAILQ_NEXT(blk, monitor_link)
261 : QTAILQ_FIRST(&monitor_block_backends);
265 * Add a BlockBackend into the list of backends referenced by the monitor, with
266 * the given @name acting as the handle for the monitor.
267 * Strictly for use by blockdev.c.
269 * @name must not be null or empty.
271 * Returns true on success and false on failure. In the latter case, an Error
272 * object is returned through @errp.
274 bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
276 assert(!blk->name);
277 assert(name && name[0]);
279 if (!id_wellformed(name)) {
280 error_setg(errp, "Invalid device name");
281 return false;
283 if (blk_by_name(name)) {
284 error_setg(errp, "Device with id '%s' already exists", name);
285 return false;
287 if (bdrv_find_node(name)) {
288 error_setg(errp,
289 "Device name '%s' conflicts with an existing node name",
290 name);
291 return false;
294 blk->name = g_strdup(name);
295 QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
296 return true;
300 * Remove a BlockBackend from the list of backends referenced by the monitor.
301 * Strictly for use by blockdev.c.
303 void monitor_remove_blk(BlockBackend *blk)
305 if (!blk->name) {
306 return;
309 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
310 g_free(blk->name);
311 blk->name = NULL;
315 * Return @blk's name, a non-null string.
316 * Returns an empty string iff @blk is not referenced by the monitor.
318 const char *blk_name(BlockBackend *blk)
320 return blk->name ?: "";
324 * Return the BlockBackend with name @name if it exists, else null.
325 * @name must not be null.
327 BlockBackend *blk_by_name(const char *name)
329 BlockBackend *blk = NULL;
331 assert(name);
332 while ((blk = blk_next(blk)) != NULL) {
333 if (!strcmp(name, blk->name)) {
334 return blk;
337 return NULL;
341 * Return the BlockDriverState attached to @blk if any, else null.
343 BlockDriverState *blk_bs(BlockBackend *blk)
345 return blk->bs;
349 * Changes the BlockDriverState attached to @blk
351 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs)
353 bdrv_ref(bs);
355 if (blk->bs) {
356 blk->bs->blk = NULL;
357 bdrv_unref(blk->bs);
359 assert(bs->blk == NULL);
361 blk->bs = bs;
362 bs->blk = blk;
366 * Return @blk's DriveInfo if any, else null.
368 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
370 return blk->legacy_dinfo;
374 * Set @blk's DriveInfo to @dinfo, and return it.
375 * @blk must not have a DriveInfo set already.
376 * No other BlockBackend may have the same DriveInfo set.
378 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
380 assert(!blk->legacy_dinfo);
381 return blk->legacy_dinfo = dinfo;
385 * Return the BlockBackend with DriveInfo @dinfo.
386 * It must exist.
388 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
390 BlockBackend *blk = NULL;
392 while ((blk = blk_next(blk)) != NULL) {
393 if (blk->legacy_dinfo == dinfo) {
394 return blk;
397 abort();
401 * Hide @blk.
402 * @blk must not have been hidden already.
403 * Make attached BlockDriverState, if any, anonymous.
404 * Once hidden, @blk is invisible to all functions that don't receive
405 * it as argument. For example, blk_by_name() won't return it.
406 * Strictly for use by do_drive_del().
407 * TODO get rid of it!
409 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
411 monitor_remove_blk(blk);
412 if (blk->bs) {
413 bdrv_make_anon(blk->bs);
418 * Disassociates the currently associated BlockDriverState from @blk.
420 void blk_remove_bs(BlockBackend *blk)
422 assert(blk->bs->blk == blk);
424 notifier_list_notify(&blk->remove_bs_notifiers, blk);
426 blk_update_root_state(blk);
428 blk->bs->blk = NULL;
429 bdrv_unref(blk->bs);
430 blk->bs = NULL;
434 * Associates a new BlockDriverState with @blk.
436 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
438 assert(!blk->bs && !bs->blk);
439 bdrv_ref(bs);
440 blk->bs = bs;
441 bs->blk = blk;
443 notifier_list_notify(&blk->insert_bs_notifiers, blk);
447 * Attach device model @dev to @blk.
448 * Return 0 on success, -EBUSY when a device model is attached already.
450 int blk_attach_dev(BlockBackend *blk, void *dev)
451 /* TODO change to DeviceState *dev when all users are qdevified */
453 if (blk->dev) {
454 return -EBUSY;
456 blk_ref(blk);
457 blk->dev = dev;
458 blk_iostatus_reset(blk);
459 return 0;
463 * Attach device model @dev to @blk.
464 * @blk must not have a device model attached already.
465 * TODO qdevified devices don't use this, remove when devices are qdevified
467 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
469 if (blk_attach_dev(blk, dev) < 0) {
470 abort();
475 * Detach device model @dev from @blk.
476 * @dev must be currently attached to @blk.
478 void blk_detach_dev(BlockBackend *blk, void *dev)
479 /* TODO change to DeviceState *dev when all users are qdevified */
481 assert(blk->dev == dev);
482 blk->dev = NULL;
483 blk->dev_ops = NULL;
484 blk->dev_opaque = NULL;
485 blk->guest_block_size = 512;
486 blk_unref(blk);
490 * Return the device model attached to @blk if any, else null.
492 void *blk_get_attached_dev(BlockBackend *blk)
493 /* TODO change to return DeviceState * when all users are qdevified */
495 return blk->dev;
499 * Set @blk's device model callbacks to @ops.
500 * @opaque is the opaque argument to pass to the callbacks.
501 * This is for use by device models.
503 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
504 void *opaque)
506 blk->dev_ops = ops;
507 blk->dev_opaque = opaque;
511 * Notify @blk's attached device model of media change.
512 * If @load is true, notify of media load.
513 * Else, notify of media eject.
514 * Also send DEVICE_TRAY_MOVED events as appropriate.
516 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
518 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
519 bool tray_was_open, tray_is_open;
521 tray_was_open = blk_dev_is_tray_open(blk);
522 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
523 tray_is_open = blk_dev_is_tray_open(blk);
525 if (tray_was_open != tray_is_open) {
526 qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
527 &error_abort);
533 * Does @blk's attached device model have removable media?
534 * %true if no device model is attached.
536 bool blk_dev_has_removable_media(BlockBackend *blk)
538 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
542 * Does @blk's attached device model have a tray?
544 bool blk_dev_has_tray(BlockBackend *blk)
546 return blk->dev_ops && blk->dev_ops->is_tray_open;
550 * Notify @blk's attached device model of a media eject request.
551 * If @force is true, the medium is about to be yanked out forcefully.
553 void blk_dev_eject_request(BlockBackend *blk, bool force)
555 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
556 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
561 * Does @blk's attached device model have a tray, and is it open?
563 bool blk_dev_is_tray_open(BlockBackend *blk)
565 if (blk_dev_has_tray(blk)) {
566 return blk->dev_ops->is_tray_open(blk->dev_opaque);
568 return false;
572 * Does @blk's attached device model have the medium locked?
573 * %false if the device model has no such lock.
575 bool blk_dev_is_medium_locked(BlockBackend *blk)
577 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
578 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
580 return false;
584 * Notify @blk's attached device model of a backend size change.
586 void blk_dev_resize_cb(BlockBackend *blk)
588 if (blk->dev_ops && blk->dev_ops->resize_cb) {
589 blk->dev_ops->resize_cb(blk->dev_opaque);
593 void blk_iostatus_enable(BlockBackend *blk)
595 blk->iostatus_enabled = true;
596 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
599 /* The I/O status is only enabled if the drive explicitly
600 * enables it _and_ the VM is configured to stop on errors */
601 bool blk_iostatus_is_enabled(const BlockBackend *blk)
603 return (blk->iostatus_enabled &&
604 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
605 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
606 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
609 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
611 return blk->iostatus;
614 void blk_iostatus_disable(BlockBackend *blk)
616 blk->iostatus_enabled = false;
619 void blk_iostatus_reset(BlockBackend *blk)
621 if (blk_iostatus_is_enabled(blk)) {
622 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
623 if (blk->bs && blk->bs->job) {
624 block_job_iostatus_reset(blk->bs->job);
629 void blk_iostatus_set_err(BlockBackend *blk, int error)
631 assert(blk_iostatus_is_enabled(blk));
632 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
633 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
634 BLOCK_DEVICE_IO_STATUS_FAILED;
638 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
640 blk->allow_write_beyond_eof = allow;
643 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
644 size_t size)
646 int64_t len;
648 if (size > INT_MAX) {
649 return -EIO;
652 if (!blk_is_available(blk)) {
653 return -ENOMEDIUM;
656 if (offset < 0) {
657 return -EIO;
660 if (!blk->allow_write_beyond_eof) {
661 len = blk_getlength(blk);
662 if (len < 0) {
663 return len;
666 if (offset > len || len - offset < size) {
667 return -EIO;
671 return 0;
674 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
675 int nb_sectors)
677 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
678 return -EIO;
681 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
682 return -EIO;
685 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
686 nb_sectors * BDRV_SECTOR_SIZE);
689 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
690 int nb_sectors)
692 int ret = blk_check_request(blk, sector_num, nb_sectors);
693 if (ret < 0) {
694 return ret;
697 return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
700 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
701 int nb_sectors)
703 int ret = blk_check_request(blk, sector_num, nb_sectors);
704 if (ret < 0) {
705 return ret;
708 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
711 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
712 int nb_sectors)
714 int ret = blk_check_request(blk, sector_num, nb_sectors);
715 if (ret < 0) {
716 return ret;
719 return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
722 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
723 int nb_sectors, BdrvRequestFlags flags)
725 int ret = blk_check_request(blk, sector_num, nb_sectors);
726 if (ret < 0) {
727 return ret;
730 return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
733 static void error_callback_bh(void *opaque)
735 struct BlockBackendAIOCB *acb = opaque;
736 qemu_bh_delete(acb->bh);
737 acb->common.cb(acb->common.opaque, acb->ret);
738 qemu_aio_unref(acb);
741 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
742 BlockCompletionFunc *cb,
743 void *opaque, int ret)
745 struct BlockBackendAIOCB *acb;
746 QEMUBH *bh;
748 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
749 acb->blk = blk;
750 acb->ret = ret;
752 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
753 acb->bh = bh;
754 qemu_bh_schedule(bh);
756 return &acb->common;
759 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
760 int nb_sectors, BdrvRequestFlags flags,
761 BlockCompletionFunc *cb, void *opaque)
763 int ret = blk_check_request(blk, sector_num, nb_sectors);
764 if (ret < 0) {
765 return blk_abort_aio_request(blk, cb, opaque, ret);
768 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
769 cb, opaque);
772 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
774 int ret = blk_check_byte_request(blk, offset, count);
775 if (ret < 0) {
776 return ret;
779 return bdrv_pread(blk->bs, offset, buf, count);
782 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
784 int ret = blk_check_byte_request(blk, offset, count);
785 if (ret < 0) {
786 return ret;
789 return bdrv_pwrite(blk->bs, offset, buf, count);
792 int64_t blk_getlength(BlockBackend *blk)
794 if (!blk_is_available(blk)) {
795 return -ENOMEDIUM;
798 return bdrv_getlength(blk->bs);
801 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
803 if (!blk->bs) {
804 *nb_sectors_ptr = 0;
805 } else {
806 bdrv_get_geometry(blk->bs, nb_sectors_ptr);
810 int64_t blk_nb_sectors(BlockBackend *blk)
812 if (!blk_is_available(blk)) {
813 return -ENOMEDIUM;
816 return bdrv_nb_sectors(blk->bs);
819 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
820 QEMUIOVector *iov, int nb_sectors,
821 BlockCompletionFunc *cb, void *opaque)
823 int ret = blk_check_request(blk, sector_num, nb_sectors);
824 if (ret < 0) {
825 return blk_abort_aio_request(blk, cb, opaque, ret);
828 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
831 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
832 QEMUIOVector *iov, int nb_sectors,
833 BlockCompletionFunc *cb, void *opaque)
835 int ret = blk_check_request(blk, sector_num, nb_sectors);
836 if (ret < 0) {
837 return blk_abort_aio_request(blk, cb, opaque, ret);
840 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
843 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
844 BlockCompletionFunc *cb, void *opaque)
846 if (!blk_is_available(blk)) {
847 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
850 return bdrv_aio_flush(blk->bs, cb, opaque);
853 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
854 int64_t sector_num, int nb_sectors,
855 BlockCompletionFunc *cb, void *opaque)
857 int ret = blk_check_request(blk, sector_num, nb_sectors);
858 if (ret < 0) {
859 return blk_abort_aio_request(blk, cb, opaque, ret);
862 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
865 void blk_aio_cancel(BlockAIOCB *acb)
867 bdrv_aio_cancel(acb);
870 void blk_aio_cancel_async(BlockAIOCB *acb)
872 bdrv_aio_cancel_async(acb);
875 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
877 int i, ret;
879 for (i = 0; i < num_reqs; i++) {
880 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
881 if (ret < 0) {
882 return ret;
886 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
889 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
891 if (!blk_is_available(blk)) {
892 return -ENOMEDIUM;
895 return bdrv_ioctl(blk->bs, req, buf);
898 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
899 BlockCompletionFunc *cb, void *opaque)
901 if (!blk_is_available(blk)) {
902 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
905 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
908 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
910 int ret = blk_check_request(blk, sector_num, nb_sectors);
911 if (ret < 0) {
912 return ret;
915 return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
918 int blk_co_flush(BlockBackend *blk)
920 if (!blk_is_available(blk)) {
921 return -ENOMEDIUM;
924 return bdrv_co_flush(blk->bs);
927 int blk_flush(BlockBackend *blk)
929 if (!blk_is_available(blk)) {
930 return -ENOMEDIUM;
933 return bdrv_flush(blk->bs);
936 int blk_flush_all(void)
938 return bdrv_flush_all();
941 void blk_drain(BlockBackend *blk)
943 if (blk->bs) {
944 bdrv_drain(blk->bs);
948 void blk_drain_all(void)
950 bdrv_drain_all();
953 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
954 BlockdevOnError on_write_error)
956 blk->on_read_error = on_read_error;
957 blk->on_write_error = on_write_error;
960 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
962 return is_read ? blk->on_read_error : blk->on_write_error;
965 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
966 int error)
968 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
970 switch (on_err) {
971 case BLOCKDEV_ON_ERROR_ENOSPC:
972 return (error == ENOSPC) ?
973 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
974 case BLOCKDEV_ON_ERROR_STOP:
975 return BLOCK_ERROR_ACTION_STOP;
976 case BLOCKDEV_ON_ERROR_REPORT:
977 return BLOCK_ERROR_ACTION_REPORT;
978 case BLOCKDEV_ON_ERROR_IGNORE:
979 return BLOCK_ERROR_ACTION_IGNORE;
980 default:
981 abort();
985 static void send_qmp_error_event(BlockBackend *blk,
986 BlockErrorAction action,
987 bool is_read, int error)
989 IoOperationType optype;
991 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
992 qapi_event_send_block_io_error(blk_name(blk), optype, action,
993 blk_iostatus_is_enabled(blk),
994 error == ENOSPC, strerror(error),
995 &error_abort);
998 /* This is done by device models because, while the block layer knows
999 * about the error, it does not know whether an operation comes from
1000 * the device or the block layer (from a job, for example).
1002 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1003 bool is_read, int error)
1005 assert(error >= 0);
1007 if (action == BLOCK_ERROR_ACTION_STOP) {
1008 /* First set the iostatus, so that "info block" returns an iostatus
1009 * that matches the events raised so far (an additional error iostatus
1010 * is fine, but not a lost one).
1012 blk_iostatus_set_err(blk, error);
1014 /* Then raise the request to stop the VM and the event.
1015 * qemu_system_vmstop_request_prepare has two effects. First,
1016 * it ensures that the STOP event always comes after the
1017 * BLOCK_IO_ERROR event. Second, it ensures that even if management
1018 * can observe the STOP event and do a "cont" before the STOP
1019 * event is issued, the VM will not stop. In this case, vm_start()
1020 * also ensures that the STOP/RESUME pair of events is emitted.
1022 qemu_system_vmstop_request_prepare();
1023 send_qmp_error_event(blk, action, is_read, error);
1024 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1025 } else {
1026 send_qmp_error_event(blk, action, is_read, error);
1030 int blk_is_read_only(BlockBackend *blk)
1032 if (blk->bs) {
1033 return bdrv_is_read_only(blk->bs);
1034 } else {
1035 return blk->root_state.read_only;
1039 int blk_is_sg(BlockBackend *blk)
1041 if (!blk->bs) {
1042 return 0;
1045 return bdrv_is_sg(blk->bs);
1048 int blk_enable_write_cache(BlockBackend *blk)
1050 if (blk->bs) {
1051 return bdrv_enable_write_cache(blk->bs);
1052 } else {
1053 return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB);
1057 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1059 if (blk->bs) {
1060 bdrv_set_enable_write_cache(blk->bs, wce);
1061 } else {
1062 if (wce) {
1063 blk->root_state.open_flags |= BDRV_O_CACHE_WB;
1064 } else {
1065 blk->root_state.open_flags &= ~BDRV_O_CACHE_WB;
1070 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1072 if (!blk->bs) {
1073 error_setg(errp, "Device '%s' has no medium", blk->name);
1074 return;
1077 bdrv_invalidate_cache(blk->bs, errp);
1080 bool blk_is_inserted(BlockBackend *blk)
1082 return blk->bs && bdrv_is_inserted(blk->bs);
1085 bool blk_is_available(BlockBackend *blk)
1087 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1090 void blk_lock_medium(BlockBackend *blk, bool locked)
1092 if (blk->bs) {
1093 bdrv_lock_medium(blk->bs, locked);
1097 void blk_eject(BlockBackend *blk, bool eject_flag)
1099 if (blk->bs) {
1100 bdrv_eject(blk->bs, eject_flag);
1104 int blk_get_flags(BlockBackend *blk)
1106 if (blk->bs) {
1107 return bdrv_get_flags(blk->bs);
1108 } else {
1109 return blk->root_state.open_flags;
1113 int blk_get_max_transfer_length(BlockBackend *blk)
1115 if (blk->bs) {
1116 return blk->bs->bl.max_transfer_length;
1117 } else {
1118 return 0;
1122 int blk_get_max_iov(BlockBackend *blk)
1124 return blk->bs->bl.max_iov;
1127 void blk_set_guest_block_size(BlockBackend *blk, int align)
1129 blk->guest_block_size = align;
1132 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1134 return qemu_try_blockalign(blk ? blk->bs : NULL, size);
1137 void *blk_blockalign(BlockBackend *blk, size_t size)
1139 return qemu_blockalign(blk ? blk->bs : NULL, size);
1142 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1144 if (!blk->bs) {
1145 return false;
1148 return bdrv_op_is_blocked(blk->bs, op, errp);
1151 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1153 if (blk->bs) {
1154 bdrv_op_unblock(blk->bs, op, reason);
1158 void blk_op_block_all(BlockBackend *blk, Error *reason)
1160 if (blk->bs) {
1161 bdrv_op_block_all(blk->bs, reason);
1165 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1167 if (blk->bs) {
1168 bdrv_op_unblock_all(blk->bs, reason);
1172 AioContext *blk_get_aio_context(BlockBackend *blk)
1174 if (blk->bs) {
1175 return bdrv_get_aio_context(blk->bs);
1176 } else {
1177 return qemu_get_aio_context();
1181 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1183 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1184 return blk_get_aio_context(blk_acb->blk);
1187 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1189 if (blk->bs) {
1190 bdrv_set_aio_context(blk->bs, new_context);
1194 void blk_add_aio_context_notifier(BlockBackend *blk,
1195 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1196 void (*detach_aio_context)(void *opaque), void *opaque)
1198 if (blk->bs) {
1199 bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
1200 detach_aio_context, opaque);
1204 void blk_remove_aio_context_notifier(BlockBackend *blk,
1205 void (*attached_aio_context)(AioContext *,
1206 void *),
1207 void (*detach_aio_context)(void *),
1208 void *opaque)
1210 if (blk->bs) {
1211 bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
1212 detach_aio_context, opaque);
1216 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1218 notifier_list_add(&blk->remove_bs_notifiers, notify);
1221 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1223 notifier_list_add(&blk->insert_bs_notifiers, notify);
1226 void blk_io_plug(BlockBackend *blk)
1228 if (blk->bs) {
1229 bdrv_io_plug(blk->bs);
1233 void blk_io_unplug(BlockBackend *blk)
1235 if (blk->bs) {
1236 bdrv_io_unplug(blk->bs);
1240 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1242 return &blk->stats;
1245 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1246 BlockCompletionFunc *cb, void *opaque)
1248 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1251 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
1252 int nb_sectors, BdrvRequestFlags flags)
1254 int ret = blk_check_request(blk, sector_num, nb_sectors);
1255 if (ret < 0) {
1256 return ret;
1259 return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
1262 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1263 const uint8_t *buf, int nb_sectors)
1265 int ret = blk_check_request(blk, sector_num, nb_sectors);
1266 if (ret < 0) {
1267 return ret;
1270 return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
1273 int blk_truncate(BlockBackend *blk, int64_t offset)
1275 if (!blk_is_available(blk)) {
1276 return -ENOMEDIUM;
1279 return bdrv_truncate(blk->bs, offset);
1282 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1284 int ret = blk_check_request(blk, sector_num, nb_sectors);
1285 if (ret < 0) {
1286 return ret;
1289 return bdrv_discard(blk->bs, sector_num, nb_sectors);
1292 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1293 int64_t pos, int size)
1295 if (!blk_is_available(blk)) {
1296 return -ENOMEDIUM;
1299 return bdrv_save_vmstate(blk->bs, buf, pos, size);
1302 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1304 if (!blk_is_available(blk)) {
1305 return -ENOMEDIUM;
1308 return bdrv_load_vmstate(blk->bs, buf, pos, size);
1311 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1313 if (!blk_is_available(blk)) {
1314 return -ENOMEDIUM;
1317 return bdrv_probe_blocksizes(blk->bs, bsz);
1320 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1322 if (!blk_is_available(blk)) {
1323 return -ENOMEDIUM;
1326 return bdrv_probe_geometry(blk->bs, geo);
1330 * Updates the BlockBackendRootState object with data from the currently
1331 * attached BlockDriverState.
1333 void blk_update_root_state(BlockBackend *blk)
1335 assert(blk->bs);
1337 blk->root_state.open_flags = blk->bs->open_flags;
1338 blk->root_state.read_only = blk->bs->read_only;
1339 blk->root_state.detect_zeroes = blk->bs->detect_zeroes;
1341 if (blk->root_state.throttle_group) {
1342 g_free(blk->root_state.throttle_group);
1343 throttle_group_unref(blk->root_state.throttle_state);
1345 if (blk->bs->throttle_state) {
1346 const char *name = throttle_group_get_name(blk->bs);
1347 blk->root_state.throttle_group = g_strdup(name);
1348 blk->root_state.throttle_state = throttle_group_incref(name);
1349 } else {
1350 blk->root_state.throttle_group = NULL;
1351 blk->root_state.throttle_state = NULL;
1356 * Applies the information in the root state to the given BlockDriverState. This
1357 * does not include the flags which have to be specified for bdrv_open(), use
1358 * blk_get_open_flags_from_root_state() to inquire them.
1360 void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1362 bs->detect_zeroes = blk->root_state.detect_zeroes;
1363 if (blk->root_state.throttle_group) {
1364 bdrv_io_limits_enable(bs, blk->root_state.throttle_group);
1369 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1370 * supposed to inherit the root state.
1372 int blk_get_open_flags_from_root_state(BlockBackend *blk)
1374 int bs_flags;
1376 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1377 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1379 return bs_flags;
1382 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1384 return &blk->root_state;
1387 int blk_commit_all(void)
1389 return bdrv_commit_all();