blockdev: Add list of all BlockBackends
[qemu/ar7.git] / block / block-backend.c
blob35206be84503e93da22c70fe8a3c0733a3c3d143
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 assert(name && name[0]);
94 if (!id_wellformed(name)) {
95 error_setg(errp, "Invalid device name");
96 return NULL;
98 if (blk_by_name(name)) {
99 error_setg(errp, "Device with id '%s' already exists", name);
100 return NULL;
102 if (bdrv_find_node(name)) {
103 error_setg(errp,
104 "Device name '%s' conflicts with an existing node name",
105 name);
106 return NULL;
109 blk = g_new0(BlockBackend, 1);
110 blk->name = g_strdup(name);
111 blk->refcnt = 1;
112 notifier_list_init(&blk->remove_bs_notifiers);
113 notifier_list_init(&blk->insert_bs_notifiers);
115 QTAILQ_INSERT_TAIL(&block_backends, blk, link);
116 QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
118 return blk;
122 * Create a new BlockBackend with a new BlockDriverState attached.
123 * Otherwise just like blk_new(), which see.
125 BlockBackend *blk_new_with_bs(const char *name, Error **errp)
127 BlockBackend *blk;
128 BlockDriverState *bs;
130 blk = blk_new(name, errp);
131 if (!blk) {
132 return NULL;
135 bs = bdrv_new_root();
136 blk->bs = bs;
137 bs->blk = blk;
138 return 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
151 * @flags.
153 BlockBackend *blk_new_open(const char *name, const char *filename,
154 const char *reference, QDict *options, int flags,
155 Error **errp)
157 BlockBackend *blk;
158 int ret;
160 blk = blk_new_with_bs(name, errp);
161 if (!blk) {
162 QDECREF(options);
163 return NULL;
166 ret = bdrv_open(&blk->bs, filename, reference, options, flags, errp);
167 if (ret < 0) {
168 blk_unref(blk);
169 return NULL;
172 return blk;
175 static void blk_delete(BlockBackend *blk)
177 assert(!blk->refcnt);
178 assert(!blk->dev);
179 if (blk->bs) {
180 blk_remove_bs(blk);
182 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
183 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
184 if (blk->root_state.throttle_state) {
185 g_free(blk->root_state.throttle_group);
186 throttle_group_unref(blk->root_state.throttle_state);
189 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
190 if (blk->name[0]) {
191 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
193 g_free(blk->name);
195 QTAILQ_REMOVE(&block_backends, blk, link);
197 drive_info_del(blk->legacy_dinfo);
198 block_acct_cleanup(&blk->stats);
199 g_free(blk);
202 static void drive_info_del(DriveInfo *dinfo)
204 if (!dinfo) {
205 return;
207 qemu_opts_del(dinfo->opts);
208 g_free(dinfo->serial);
209 g_free(dinfo);
212 int blk_get_refcnt(BlockBackend *blk)
214 return blk ? blk->refcnt : 0;
218 * Increment @blk's reference count.
219 * @blk must not be null.
221 void blk_ref(BlockBackend *blk)
223 blk->refcnt++;
227 * Decrement @blk's reference count.
228 * If this drops it to zero, destroy @blk.
229 * For convenience, do nothing if @blk is null.
231 void blk_unref(BlockBackend *blk)
233 if (blk) {
234 assert(blk->refcnt > 0);
235 if (!--blk->refcnt) {
236 blk_delete(blk);
242 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
243 * ones which are hidden (i.e. are not referenced by the monitor).
245 static BlockBackend *blk_all_next(BlockBackend *blk)
247 return blk ? QTAILQ_NEXT(blk, link)
248 : QTAILQ_FIRST(&block_backends);
251 void blk_remove_all_bs(void)
253 BlockBackend *blk = NULL;
255 while ((blk = blk_all_next(blk)) != NULL) {
256 AioContext *ctx = blk_get_aio_context(blk);
258 aio_context_acquire(ctx);
259 if (blk->bs) {
260 blk_remove_bs(blk);
262 aio_context_release(ctx);
267 * Return the monitor-owned BlockBackend after @blk.
268 * If @blk is null, return the first one.
269 * Else, return @blk's next sibling, which may be null.
271 * To iterate over all BlockBackends, do
272 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
273 * ...
276 BlockBackend *blk_next(BlockBackend *blk)
278 return blk ? QTAILQ_NEXT(blk, monitor_link)
279 : QTAILQ_FIRST(&monitor_block_backends);
283 * Return @blk's name, a non-null string.
284 * Wart: the name is empty iff @blk has been hidden with
285 * blk_hide_on_behalf_of_hmp_drive_del().
287 const char *blk_name(BlockBackend *blk)
289 return blk->name;
293 * Return the BlockBackend with name @name if it exists, else null.
294 * @name must not be null.
296 BlockBackend *blk_by_name(const char *name)
298 BlockBackend *blk = NULL;
300 assert(name);
301 while ((blk = blk_next(blk)) != NULL) {
302 if (!strcmp(name, blk->name)) {
303 return blk;
306 return NULL;
310 * Return the BlockDriverState attached to @blk if any, else null.
312 BlockDriverState *blk_bs(BlockBackend *blk)
314 return blk->bs;
318 * Changes the BlockDriverState attached to @blk
320 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs)
322 bdrv_ref(bs);
324 if (blk->bs) {
325 blk->bs->blk = NULL;
326 bdrv_unref(blk->bs);
328 assert(bs->blk == NULL);
330 blk->bs = bs;
331 bs->blk = blk;
335 * Return @blk's DriveInfo if any, else null.
337 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
339 return blk->legacy_dinfo;
343 * Set @blk's DriveInfo to @dinfo, and return it.
344 * @blk must not have a DriveInfo set already.
345 * No other BlockBackend may have the same DriveInfo set.
347 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
349 assert(!blk->legacy_dinfo);
350 return blk->legacy_dinfo = dinfo;
354 * Return the BlockBackend with DriveInfo @dinfo.
355 * It must exist.
357 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
359 BlockBackend *blk = NULL;
361 while ((blk = blk_next(blk)) != NULL) {
362 if (blk->legacy_dinfo == dinfo) {
363 return blk;
366 abort();
370 * Hide @blk.
371 * @blk must not have been hidden already.
372 * Make attached BlockDriverState, if any, anonymous.
373 * Once hidden, @blk is invisible to all functions that don't receive
374 * it as argument. For example, blk_by_name() won't return it.
375 * Strictly for use by do_drive_del().
376 * TODO get rid of it!
378 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
380 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
381 blk->name[0] = 0;
382 if (blk->bs) {
383 bdrv_make_anon(blk->bs);
388 * Disassociates the currently associated BlockDriverState from @blk.
390 void blk_remove_bs(BlockBackend *blk)
392 assert(blk->bs->blk == blk);
394 notifier_list_notify(&blk->remove_bs_notifiers, blk);
396 blk_update_root_state(blk);
398 blk->bs->blk = NULL;
399 bdrv_unref(blk->bs);
400 blk->bs = NULL;
404 * Associates a new BlockDriverState with @blk.
406 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
408 assert(!blk->bs && !bs->blk);
409 bdrv_ref(bs);
410 blk->bs = bs;
411 bs->blk = blk;
413 notifier_list_notify(&blk->insert_bs_notifiers, blk);
417 * Attach device model @dev to @blk.
418 * Return 0 on success, -EBUSY when a device model is attached already.
420 int blk_attach_dev(BlockBackend *blk, void *dev)
421 /* TODO change to DeviceState *dev when all users are qdevified */
423 if (blk->dev) {
424 return -EBUSY;
426 blk_ref(blk);
427 blk->dev = dev;
428 blk_iostatus_reset(blk);
429 return 0;
433 * Attach device model @dev to @blk.
434 * @blk must not have a device model attached already.
435 * TODO qdevified devices don't use this, remove when devices are qdevified
437 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
439 if (blk_attach_dev(blk, dev) < 0) {
440 abort();
445 * Detach device model @dev from @blk.
446 * @dev must be currently attached to @blk.
448 void blk_detach_dev(BlockBackend *blk, void *dev)
449 /* TODO change to DeviceState *dev when all users are qdevified */
451 assert(blk->dev == dev);
452 blk->dev = NULL;
453 blk->dev_ops = NULL;
454 blk->dev_opaque = NULL;
455 blk->guest_block_size = 512;
456 blk_unref(blk);
460 * Return the device model attached to @blk if any, else null.
462 void *blk_get_attached_dev(BlockBackend *blk)
463 /* TODO change to return DeviceState * when all users are qdevified */
465 return blk->dev;
469 * Set @blk's device model callbacks to @ops.
470 * @opaque is the opaque argument to pass to the callbacks.
471 * This is for use by device models.
473 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
474 void *opaque)
476 blk->dev_ops = ops;
477 blk->dev_opaque = opaque;
481 * Notify @blk's attached device model of media change.
482 * If @load is true, notify of media load.
483 * Else, notify of media eject.
484 * Also send DEVICE_TRAY_MOVED events as appropriate.
486 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
488 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
489 bool tray_was_open, tray_is_open;
491 tray_was_open = blk_dev_is_tray_open(blk);
492 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
493 tray_is_open = blk_dev_is_tray_open(blk);
495 if (tray_was_open != tray_is_open) {
496 qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
497 &error_abort);
503 * Does @blk's attached device model have removable media?
504 * %true if no device model is attached.
506 bool blk_dev_has_removable_media(BlockBackend *blk)
508 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
512 * Does @blk's attached device model have a tray?
514 bool blk_dev_has_tray(BlockBackend *blk)
516 return blk->dev_ops && blk->dev_ops->is_tray_open;
520 * Notify @blk's attached device model of a media eject request.
521 * If @force is true, the medium is about to be yanked out forcefully.
523 void blk_dev_eject_request(BlockBackend *blk, bool force)
525 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
526 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
531 * Does @blk's attached device model have a tray, and is it open?
533 bool blk_dev_is_tray_open(BlockBackend *blk)
535 if (blk_dev_has_tray(blk)) {
536 return blk->dev_ops->is_tray_open(blk->dev_opaque);
538 return false;
542 * Does @blk's attached device model have the medium locked?
543 * %false if the device model has no such lock.
545 bool blk_dev_is_medium_locked(BlockBackend *blk)
547 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
548 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
550 return false;
554 * Notify @blk's attached device model of a backend size change.
556 void blk_dev_resize_cb(BlockBackend *blk)
558 if (blk->dev_ops && blk->dev_ops->resize_cb) {
559 blk->dev_ops->resize_cb(blk->dev_opaque);
563 void blk_iostatus_enable(BlockBackend *blk)
565 blk->iostatus_enabled = true;
566 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
569 /* The I/O status is only enabled if the drive explicitly
570 * enables it _and_ the VM is configured to stop on errors */
571 bool blk_iostatus_is_enabled(const BlockBackend *blk)
573 return (blk->iostatus_enabled &&
574 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
575 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
576 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
579 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
581 return blk->iostatus;
584 void blk_iostatus_disable(BlockBackend *blk)
586 blk->iostatus_enabled = false;
589 void blk_iostatus_reset(BlockBackend *blk)
591 if (blk_iostatus_is_enabled(blk)) {
592 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
593 if (blk->bs && blk->bs->job) {
594 block_job_iostatus_reset(blk->bs->job);
599 void blk_iostatus_set_err(BlockBackend *blk, int error)
601 assert(blk_iostatus_is_enabled(blk));
602 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
603 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
604 BLOCK_DEVICE_IO_STATUS_FAILED;
608 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
610 blk->allow_write_beyond_eof = allow;
613 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
614 size_t size)
616 int64_t len;
618 if (size > INT_MAX) {
619 return -EIO;
622 if (!blk_is_available(blk)) {
623 return -ENOMEDIUM;
626 if (offset < 0) {
627 return -EIO;
630 if (!blk->allow_write_beyond_eof) {
631 len = blk_getlength(blk);
632 if (len < 0) {
633 return len;
636 if (offset > len || len - offset < size) {
637 return -EIO;
641 return 0;
644 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
645 int nb_sectors)
647 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
648 return -EIO;
651 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
652 return -EIO;
655 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
656 nb_sectors * BDRV_SECTOR_SIZE);
659 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
660 int nb_sectors)
662 int ret = blk_check_request(blk, sector_num, nb_sectors);
663 if (ret < 0) {
664 return ret;
667 return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
670 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
671 int nb_sectors)
673 int ret = blk_check_request(blk, sector_num, nb_sectors);
674 if (ret < 0) {
675 return ret;
678 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
681 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
682 int nb_sectors)
684 int ret = blk_check_request(blk, sector_num, nb_sectors);
685 if (ret < 0) {
686 return ret;
689 return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
692 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
693 int nb_sectors, BdrvRequestFlags flags)
695 int ret = blk_check_request(blk, sector_num, nb_sectors);
696 if (ret < 0) {
697 return ret;
700 return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
703 static void error_callback_bh(void *opaque)
705 struct BlockBackendAIOCB *acb = opaque;
706 qemu_bh_delete(acb->bh);
707 acb->common.cb(acb->common.opaque, acb->ret);
708 qemu_aio_unref(acb);
711 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
712 BlockCompletionFunc *cb,
713 void *opaque, int ret)
715 struct BlockBackendAIOCB *acb;
716 QEMUBH *bh;
718 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
719 acb->blk = blk;
720 acb->ret = ret;
722 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
723 acb->bh = bh;
724 qemu_bh_schedule(bh);
726 return &acb->common;
729 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
730 int nb_sectors, BdrvRequestFlags flags,
731 BlockCompletionFunc *cb, void *opaque)
733 int ret = blk_check_request(blk, sector_num, nb_sectors);
734 if (ret < 0) {
735 return blk_abort_aio_request(blk, cb, opaque, ret);
738 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
739 cb, opaque);
742 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
744 int ret = blk_check_byte_request(blk, offset, count);
745 if (ret < 0) {
746 return ret;
749 return bdrv_pread(blk->bs, offset, buf, count);
752 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
754 int ret = blk_check_byte_request(blk, offset, count);
755 if (ret < 0) {
756 return ret;
759 return bdrv_pwrite(blk->bs, offset, buf, count);
762 int64_t blk_getlength(BlockBackend *blk)
764 if (!blk_is_available(blk)) {
765 return -ENOMEDIUM;
768 return bdrv_getlength(blk->bs);
771 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
773 if (!blk->bs) {
774 *nb_sectors_ptr = 0;
775 } else {
776 bdrv_get_geometry(blk->bs, nb_sectors_ptr);
780 int64_t blk_nb_sectors(BlockBackend *blk)
782 if (!blk_is_available(blk)) {
783 return -ENOMEDIUM;
786 return bdrv_nb_sectors(blk->bs);
789 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
790 QEMUIOVector *iov, int nb_sectors,
791 BlockCompletionFunc *cb, void *opaque)
793 int ret = blk_check_request(blk, sector_num, nb_sectors);
794 if (ret < 0) {
795 return blk_abort_aio_request(blk, cb, opaque, ret);
798 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
801 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
802 QEMUIOVector *iov, int nb_sectors,
803 BlockCompletionFunc *cb, void *opaque)
805 int ret = blk_check_request(blk, sector_num, nb_sectors);
806 if (ret < 0) {
807 return blk_abort_aio_request(blk, cb, opaque, ret);
810 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
813 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
814 BlockCompletionFunc *cb, void *opaque)
816 if (!blk_is_available(blk)) {
817 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
820 return bdrv_aio_flush(blk->bs, cb, opaque);
823 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
824 int64_t sector_num, int nb_sectors,
825 BlockCompletionFunc *cb, void *opaque)
827 int ret = blk_check_request(blk, sector_num, nb_sectors);
828 if (ret < 0) {
829 return blk_abort_aio_request(blk, cb, opaque, ret);
832 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
835 void blk_aio_cancel(BlockAIOCB *acb)
837 bdrv_aio_cancel(acb);
840 void blk_aio_cancel_async(BlockAIOCB *acb)
842 bdrv_aio_cancel_async(acb);
845 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
847 int i, ret;
849 for (i = 0; i < num_reqs; i++) {
850 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
851 if (ret < 0) {
852 return ret;
856 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
859 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
861 if (!blk_is_available(blk)) {
862 return -ENOMEDIUM;
865 return bdrv_ioctl(blk->bs, req, buf);
868 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
869 BlockCompletionFunc *cb, void *opaque)
871 if (!blk_is_available(blk)) {
872 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
875 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
878 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
880 int ret = blk_check_request(blk, sector_num, nb_sectors);
881 if (ret < 0) {
882 return ret;
885 return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
888 int blk_co_flush(BlockBackend *blk)
890 if (!blk_is_available(blk)) {
891 return -ENOMEDIUM;
894 return bdrv_co_flush(blk->bs);
897 int blk_flush(BlockBackend *blk)
899 if (!blk_is_available(blk)) {
900 return -ENOMEDIUM;
903 return bdrv_flush(blk->bs);
906 int blk_flush_all(void)
908 return bdrv_flush_all();
911 void blk_drain(BlockBackend *blk)
913 if (blk->bs) {
914 bdrv_drain(blk->bs);
918 void blk_drain_all(void)
920 bdrv_drain_all();
923 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
924 BlockdevOnError on_write_error)
926 blk->on_read_error = on_read_error;
927 blk->on_write_error = on_write_error;
930 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
932 return is_read ? blk->on_read_error : blk->on_write_error;
935 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
936 int error)
938 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
940 switch (on_err) {
941 case BLOCKDEV_ON_ERROR_ENOSPC:
942 return (error == ENOSPC) ?
943 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
944 case BLOCKDEV_ON_ERROR_STOP:
945 return BLOCK_ERROR_ACTION_STOP;
946 case BLOCKDEV_ON_ERROR_REPORT:
947 return BLOCK_ERROR_ACTION_REPORT;
948 case BLOCKDEV_ON_ERROR_IGNORE:
949 return BLOCK_ERROR_ACTION_IGNORE;
950 default:
951 abort();
955 static void send_qmp_error_event(BlockBackend *blk,
956 BlockErrorAction action,
957 bool is_read, int error)
959 IoOperationType optype;
961 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
962 qapi_event_send_block_io_error(blk_name(blk), optype, action,
963 blk_iostatus_is_enabled(blk),
964 error == ENOSPC, strerror(error),
965 &error_abort);
968 /* This is done by device models because, while the block layer knows
969 * about the error, it does not know whether an operation comes from
970 * the device or the block layer (from a job, for example).
972 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
973 bool is_read, int error)
975 assert(error >= 0);
977 if (action == BLOCK_ERROR_ACTION_STOP) {
978 /* First set the iostatus, so that "info block" returns an iostatus
979 * that matches the events raised so far (an additional error iostatus
980 * is fine, but not a lost one).
982 blk_iostatus_set_err(blk, error);
984 /* Then raise the request to stop the VM and the event.
985 * qemu_system_vmstop_request_prepare has two effects. First,
986 * it ensures that the STOP event always comes after the
987 * BLOCK_IO_ERROR event. Second, it ensures that even if management
988 * can observe the STOP event and do a "cont" before the STOP
989 * event is issued, the VM will not stop. In this case, vm_start()
990 * also ensures that the STOP/RESUME pair of events is emitted.
992 qemu_system_vmstop_request_prepare();
993 send_qmp_error_event(blk, action, is_read, error);
994 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
995 } else {
996 send_qmp_error_event(blk, action, is_read, error);
1000 int blk_is_read_only(BlockBackend *blk)
1002 if (blk->bs) {
1003 return bdrv_is_read_only(blk->bs);
1004 } else {
1005 return blk->root_state.read_only;
1009 int blk_is_sg(BlockBackend *blk)
1011 if (!blk->bs) {
1012 return 0;
1015 return bdrv_is_sg(blk->bs);
1018 int blk_enable_write_cache(BlockBackend *blk)
1020 if (blk->bs) {
1021 return bdrv_enable_write_cache(blk->bs);
1022 } else {
1023 return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB);
1027 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1029 if (blk->bs) {
1030 bdrv_set_enable_write_cache(blk->bs, wce);
1031 } else {
1032 if (wce) {
1033 blk->root_state.open_flags |= BDRV_O_CACHE_WB;
1034 } else {
1035 blk->root_state.open_flags &= ~BDRV_O_CACHE_WB;
1040 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1042 if (!blk->bs) {
1043 error_setg(errp, "Device '%s' has no medium", blk->name);
1044 return;
1047 bdrv_invalidate_cache(blk->bs, errp);
1050 bool blk_is_inserted(BlockBackend *blk)
1052 return blk->bs && bdrv_is_inserted(blk->bs);
1055 bool blk_is_available(BlockBackend *blk)
1057 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1060 void blk_lock_medium(BlockBackend *blk, bool locked)
1062 if (blk->bs) {
1063 bdrv_lock_medium(blk->bs, locked);
1067 void blk_eject(BlockBackend *blk, bool eject_flag)
1069 if (blk->bs) {
1070 bdrv_eject(blk->bs, eject_flag);
1074 int blk_get_flags(BlockBackend *blk)
1076 if (blk->bs) {
1077 return bdrv_get_flags(blk->bs);
1078 } else {
1079 return blk->root_state.open_flags;
1083 int blk_get_max_transfer_length(BlockBackend *blk)
1085 if (blk->bs) {
1086 return blk->bs->bl.max_transfer_length;
1087 } else {
1088 return 0;
1092 int blk_get_max_iov(BlockBackend *blk)
1094 return blk->bs->bl.max_iov;
1097 void blk_set_guest_block_size(BlockBackend *blk, int align)
1099 blk->guest_block_size = align;
1102 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1104 return qemu_try_blockalign(blk ? blk->bs : NULL, size);
1107 void *blk_blockalign(BlockBackend *blk, size_t size)
1109 return qemu_blockalign(blk ? blk->bs : NULL, size);
1112 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1114 if (!blk->bs) {
1115 return false;
1118 return bdrv_op_is_blocked(blk->bs, op, errp);
1121 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1123 if (blk->bs) {
1124 bdrv_op_unblock(blk->bs, op, reason);
1128 void blk_op_block_all(BlockBackend *blk, Error *reason)
1130 if (blk->bs) {
1131 bdrv_op_block_all(blk->bs, reason);
1135 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1137 if (blk->bs) {
1138 bdrv_op_unblock_all(blk->bs, reason);
1142 AioContext *blk_get_aio_context(BlockBackend *blk)
1144 if (blk->bs) {
1145 return bdrv_get_aio_context(blk->bs);
1146 } else {
1147 return qemu_get_aio_context();
1151 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1153 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1154 return blk_get_aio_context(blk_acb->blk);
1157 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1159 if (blk->bs) {
1160 bdrv_set_aio_context(blk->bs, new_context);
1164 void blk_add_aio_context_notifier(BlockBackend *blk,
1165 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1166 void (*detach_aio_context)(void *opaque), void *opaque)
1168 if (blk->bs) {
1169 bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
1170 detach_aio_context, opaque);
1174 void blk_remove_aio_context_notifier(BlockBackend *blk,
1175 void (*attached_aio_context)(AioContext *,
1176 void *),
1177 void (*detach_aio_context)(void *),
1178 void *opaque)
1180 if (blk->bs) {
1181 bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
1182 detach_aio_context, opaque);
1186 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1188 notifier_list_add(&blk->remove_bs_notifiers, notify);
1191 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1193 notifier_list_add(&blk->insert_bs_notifiers, notify);
1196 void blk_io_plug(BlockBackend *blk)
1198 if (blk->bs) {
1199 bdrv_io_plug(blk->bs);
1203 void blk_io_unplug(BlockBackend *blk)
1205 if (blk->bs) {
1206 bdrv_io_unplug(blk->bs);
1210 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1212 return &blk->stats;
1215 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1216 BlockCompletionFunc *cb, void *opaque)
1218 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1221 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
1222 int nb_sectors, BdrvRequestFlags flags)
1224 int ret = blk_check_request(blk, sector_num, nb_sectors);
1225 if (ret < 0) {
1226 return ret;
1229 return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
1232 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1233 const uint8_t *buf, int nb_sectors)
1235 int ret = blk_check_request(blk, sector_num, nb_sectors);
1236 if (ret < 0) {
1237 return ret;
1240 return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
1243 int blk_truncate(BlockBackend *blk, int64_t offset)
1245 if (!blk_is_available(blk)) {
1246 return -ENOMEDIUM;
1249 return bdrv_truncate(blk->bs, offset);
1252 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1254 int ret = blk_check_request(blk, sector_num, nb_sectors);
1255 if (ret < 0) {
1256 return ret;
1259 return bdrv_discard(blk->bs, sector_num, nb_sectors);
1262 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1263 int64_t pos, int size)
1265 if (!blk_is_available(blk)) {
1266 return -ENOMEDIUM;
1269 return bdrv_save_vmstate(blk->bs, buf, pos, size);
1272 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1274 if (!blk_is_available(blk)) {
1275 return -ENOMEDIUM;
1278 return bdrv_load_vmstate(blk->bs, buf, pos, size);
1281 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1283 if (!blk_is_available(blk)) {
1284 return -ENOMEDIUM;
1287 return bdrv_probe_blocksizes(blk->bs, bsz);
1290 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1292 if (!blk_is_available(blk)) {
1293 return -ENOMEDIUM;
1296 return bdrv_probe_geometry(blk->bs, geo);
1300 * Updates the BlockBackendRootState object with data from the currently
1301 * attached BlockDriverState.
1303 void blk_update_root_state(BlockBackend *blk)
1305 assert(blk->bs);
1307 blk->root_state.open_flags = blk->bs->open_flags;
1308 blk->root_state.read_only = blk->bs->read_only;
1309 blk->root_state.detect_zeroes = blk->bs->detect_zeroes;
1311 if (blk->root_state.throttle_group) {
1312 g_free(blk->root_state.throttle_group);
1313 throttle_group_unref(blk->root_state.throttle_state);
1315 if (blk->bs->throttle_state) {
1316 const char *name = throttle_group_get_name(blk->bs);
1317 blk->root_state.throttle_group = g_strdup(name);
1318 blk->root_state.throttle_state = throttle_group_incref(name);
1319 } else {
1320 blk->root_state.throttle_group = NULL;
1321 blk->root_state.throttle_state = NULL;
1326 * Applies the information in the root state to the given BlockDriverState. This
1327 * does not include the flags which have to be specified for bdrv_open(), use
1328 * blk_get_open_flags_from_root_state() to inquire them.
1330 void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1332 bs->detect_zeroes = blk->root_state.detect_zeroes;
1333 if (blk->root_state.throttle_group) {
1334 bdrv_io_limits_enable(bs, blk->root_state.throttle_group);
1339 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1340 * supposed to inherit the root state.
1342 int blk_get_open_flags_from_root_state(BlockBackend *blk)
1344 int bs_flags;
1346 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1347 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1349 return bs_flags;
1352 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1354 return &blk->root_state;
1357 int blk_commit_all(void)
1359 return bdrv_commit_all();