target-ppc: Eliminate kvmppc_kern_htab global
[qemu/kevin.git] / block / block-backend.c
blob03e71b43687fbba0fdc1390e0f0fd6fae68c51a0
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 blk_backends */
34 void *dev; /* attached device model, if any */
35 /* TODO change to DeviceState when all users are qdevified */
36 const BlockDevOps *dev_ops;
37 void *dev_opaque;
39 /* the block size for which the guest device expects atomicity */
40 int guest_block_size;
42 /* If the BDS tree is removed, some of its options are stored here (which
43 * can be used to restore those options in the new BDS on insert) */
44 BlockBackendRootState root_state;
46 /* I/O stats (display with "info blockstats"). */
47 BlockAcctStats stats;
49 BlockdevOnError on_read_error, on_write_error;
50 bool iostatus_enabled;
51 BlockDeviceIoStatus iostatus;
53 bool allow_write_beyond_eof;
55 NotifierList remove_bs_notifiers, insert_bs_notifiers;
58 typedef struct BlockBackendAIOCB {
59 BlockAIOCB common;
60 QEMUBH *bh;
61 BlockBackend *blk;
62 int ret;
63 } BlockBackendAIOCB;
65 static const AIOCBInfo block_backend_aiocb_info = {
66 .get_aio_context = blk_aiocb_get_aio_context,
67 .aiocb_size = sizeof(BlockBackendAIOCB),
70 static void drive_info_del(DriveInfo *dinfo);
72 /* All the BlockBackends (except for hidden ones) */
73 static QTAILQ_HEAD(, BlockBackend) blk_backends =
74 QTAILQ_HEAD_INITIALIZER(blk_backends);
77 * Create a new BlockBackend with @name, with a reference count of one.
78 * @name must not be null or empty.
79 * Fail if a BlockBackend with this name already exists.
80 * Store an error through @errp on failure, unless it's null.
81 * Return the new BlockBackend on success, null on failure.
83 BlockBackend *blk_new(const char *name, Error **errp)
85 BlockBackend *blk;
87 assert(name && name[0]);
88 if (!id_wellformed(name)) {
89 error_setg(errp, "Invalid device name");
90 return NULL;
92 if (blk_by_name(name)) {
93 error_setg(errp, "Device with id '%s' already exists", name);
94 return NULL;
96 if (bdrv_find_node(name)) {
97 error_setg(errp,
98 "Device name '%s' conflicts with an existing node name",
99 name);
100 return NULL;
103 blk = g_new0(BlockBackend, 1);
104 blk->name = g_strdup(name);
105 blk->refcnt = 1;
106 notifier_list_init(&blk->remove_bs_notifiers);
107 notifier_list_init(&blk->insert_bs_notifiers);
108 QTAILQ_INSERT_TAIL(&blk_backends, blk, link);
109 return blk;
113 * Create a new BlockBackend with a new BlockDriverState attached.
114 * Otherwise just like blk_new(), which see.
116 BlockBackend *blk_new_with_bs(const char *name, Error **errp)
118 BlockBackend *blk;
119 BlockDriverState *bs;
121 blk = blk_new(name, errp);
122 if (!blk) {
123 return NULL;
126 bs = bdrv_new_root();
127 blk->bs = bs;
128 bs->blk = blk;
129 return blk;
133 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
135 * Just as with bdrv_open(), after having called this function the reference to
136 * @options belongs to the block layer (even on failure).
138 * TODO: Remove @filename and @flags; it should be possible to specify a whole
139 * BDS tree just by specifying the @options QDict (or @reference,
140 * alternatively). At the time of adding this function, this is not possible,
141 * though, so callers of this function have to be able to specify @filename and
142 * @flags.
144 BlockBackend *blk_new_open(const char *name, const char *filename,
145 const char *reference, QDict *options, int flags,
146 Error **errp)
148 BlockBackend *blk;
149 int ret;
151 blk = blk_new_with_bs(name, errp);
152 if (!blk) {
153 QDECREF(options);
154 return NULL;
157 ret = bdrv_open(&blk->bs, filename, reference, options, flags, errp);
158 if (ret < 0) {
159 blk_unref(blk);
160 return NULL;
163 return blk;
166 static void blk_delete(BlockBackend *blk)
168 assert(!blk->refcnt);
169 assert(!blk->dev);
170 if (blk->bs) {
171 blk_remove_bs(blk);
173 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
174 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
175 if (blk->root_state.throttle_state) {
176 g_free(blk->root_state.throttle_group);
177 throttle_group_unref(blk->root_state.throttle_state);
179 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
180 if (blk->name[0]) {
181 QTAILQ_REMOVE(&blk_backends, blk, link);
183 g_free(blk->name);
184 drive_info_del(blk->legacy_dinfo);
185 block_acct_cleanup(&blk->stats);
186 g_free(blk);
189 static void drive_info_del(DriveInfo *dinfo)
191 if (!dinfo) {
192 return;
194 qemu_opts_del(dinfo->opts);
195 g_free(dinfo->serial);
196 g_free(dinfo);
199 int blk_get_refcnt(BlockBackend *blk)
201 return blk ? blk->refcnt : 0;
205 * Increment @blk's reference count.
206 * @blk must not be null.
208 void blk_ref(BlockBackend *blk)
210 blk->refcnt++;
214 * Decrement @blk's reference count.
215 * If this drops it to zero, destroy @blk.
216 * For convenience, do nothing if @blk is null.
218 void blk_unref(BlockBackend *blk)
220 if (blk) {
221 assert(blk->refcnt > 0);
222 if (!--blk->refcnt) {
223 blk_delete(blk);
228 void blk_remove_all_bs(void)
230 BlockBackend *blk;
232 QTAILQ_FOREACH(blk, &blk_backends, link) {
233 AioContext *ctx = blk_get_aio_context(blk);
235 aio_context_acquire(ctx);
236 if (blk->bs) {
237 blk_remove_bs(blk);
239 aio_context_release(ctx);
244 * Return the BlockBackend after @blk.
245 * If @blk is null, return the first one.
246 * Else, return @blk's next sibling, which may be null.
248 * To iterate over all BlockBackends, do
249 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
250 * ...
253 BlockBackend *blk_next(BlockBackend *blk)
255 return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
259 * Return @blk's name, a non-null string.
260 * Wart: the name is empty iff @blk has been hidden with
261 * blk_hide_on_behalf_of_hmp_drive_del().
263 const char *blk_name(BlockBackend *blk)
265 return blk->name;
269 * Return the BlockBackend with name @name if it exists, else null.
270 * @name must not be null.
272 BlockBackend *blk_by_name(const char *name)
274 BlockBackend *blk;
276 assert(name);
277 QTAILQ_FOREACH(blk, &blk_backends, link) {
278 if (!strcmp(name, blk->name)) {
279 return blk;
282 return NULL;
286 * Return the BlockDriverState attached to @blk if any, else null.
288 BlockDriverState *blk_bs(BlockBackend *blk)
290 return blk->bs;
294 * Changes the BlockDriverState attached to @blk
296 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs)
298 bdrv_ref(bs);
300 if (blk->bs) {
301 blk->bs->blk = NULL;
302 bdrv_unref(blk->bs);
304 assert(bs->blk == NULL);
306 blk->bs = bs;
307 bs->blk = blk;
311 * Return @blk's DriveInfo if any, else null.
313 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
315 return blk->legacy_dinfo;
319 * Set @blk's DriveInfo to @dinfo, and return it.
320 * @blk must not have a DriveInfo set already.
321 * No other BlockBackend may have the same DriveInfo set.
323 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
325 assert(!blk->legacy_dinfo);
326 return blk->legacy_dinfo = dinfo;
330 * Return the BlockBackend with DriveInfo @dinfo.
331 * It must exist.
333 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
335 BlockBackend *blk;
337 QTAILQ_FOREACH(blk, &blk_backends, link) {
338 if (blk->legacy_dinfo == dinfo) {
339 return blk;
342 abort();
346 * Hide @blk.
347 * @blk must not have been hidden already.
348 * Make attached BlockDriverState, if any, anonymous.
349 * Once hidden, @blk is invisible to all functions that don't receive
350 * it as argument. For example, blk_by_name() won't return it.
351 * Strictly for use by do_drive_del().
352 * TODO get rid of it!
354 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
356 QTAILQ_REMOVE(&blk_backends, blk, link);
357 blk->name[0] = 0;
358 if (blk->bs) {
359 bdrv_make_anon(blk->bs);
364 * Disassociates the currently associated BlockDriverState from @blk.
366 void blk_remove_bs(BlockBackend *blk)
368 assert(blk->bs->blk == blk);
370 notifier_list_notify(&blk->remove_bs_notifiers, blk);
372 blk_update_root_state(blk);
374 blk->bs->blk = NULL;
375 bdrv_unref(blk->bs);
376 blk->bs = NULL;
380 * Associates a new BlockDriverState with @blk.
382 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
384 assert(!blk->bs && !bs->blk);
385 bdrv_ref(bs);
386 blk->bs = bs;
387 bs->blk = blk;
389 notifier_list_notify(&blk->insert_bs_notifiers, blk);
393 * Attach device model @dev to @blk.
394 * Return 0 on success, -EBUSY when a device model is attached already.
396 int blk_attach_dev(BlockBackend *blk, void *dev)
397 /* TODO change to DeviceState *dev when all users are qdevified */
399 if (blk->dev) {
400 return -EBUSY;
402 blk_ref(blk);
403 blk->dev = dev;
404 blk_iostatus_reset(blk);
405 return 0;
409 * Attach device model @dev to @blk.
410 * @blk must not have a device model attached already.
411 * TODO qdevified devices don't use this, remove when devices are qdevified
413 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
415 if (blk_attach_dev(blk, dev) < 0) {
416 abort();
421 * Detach device model @dev from @blk.
422 * @dev must be currently attached to @blk.
424 void blk_detach_dev(BlockBackend *blk, void *dev)
425 /* TODO change to DeviceState *dev when all users are qdevified */
427 assert(blk->dev == dev);
428 blk->dev = NULL;
429 blk->dev_ops = NULL;
430 blk->dev_opaque = NULL;
431 blk->guest_block_size = 512;
432 blk_unref(blk);
436 * Return the device model attached to @blk if any, else null.
438 void *blk_get_attached_dev(BlockBackend *blk)
439 /* TODO change to return DeviceState * when all users are qdevified */
441 return blk->dev;
445 * Set @blk's device model callbacks to @ops.
446 * @opaque is the opaque argument to pass to the callbacks.
447 * This is for use by device models.
449 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
450 void *opaque)
452 blk->dev_ops = ops;
453 blk->dev_opaque = opaque;
457 * Notify @blk's attached device model of media change.
458 * If @load is true, notify of media load.
459 * Else, notify of media eject.
460 * Also send DEVICE_TRAY_MOVED events as appropriate.
462 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
464 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
465 bool tray_was_open, tray_is_open;
467 tray_was_open = blk_dev_is_tray_open(blk);
468 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
469 tray_is_open = blk_dev_is_tray_open(blk);
471 if (tray_was_open != tray_is_open) {
472 qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
473 &error_abort);
479 * Does @blk's attached device model have removable media?
480 * %true if no device model is attached.
482 bool blk_dev_has_removable_media(BlockBackend *blk)
484 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
488 * Does @blk's attached device model have a tray?
490 bool blk_dev_has_tray(BlockBackend *blk)
492 return blk->dev_ops && blk->dev_ops->is_tray_open;
496 * Notify @blk's attached device model of a media eject request.
497 * If @force is true, the medium is about to be yanked out forcefully.
499 void blk_dev_eject_request(BlockBackend *blk, bool force)
501 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
502 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
507 * Does @blk's attached device model have a tray, and is it open?
509 bool blk_dev_is_tray_open(BlockBackend *blk)
511 if (blk_dev_has_tray(blk)) {
512 return blk->dev_ops->is_tray_open(blk->dev_opaque);
514 return false;
518 * Does @blk's attached device model have the medium locked?
519 * %false if the device model has no such lock.
521 bool blk_dev_is_medium_locked(BlockBackend *blk)
523 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
524 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
526 return false;
530 * Notify @blk's attached device model of a backend size change.
532 void blk_dev_resize_cb(BlockBackend *blk)
534 if (blk->dev_ops && blk->dev_ops->resize_cb) {
535 blk->dev_ops->resize_cb(blk->dev_opaque);
539 void blk_iostatus_enable(BlockBackend *blk)
541 blk->iostatus_enabled = true;
542 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
545 /* The I/O status is only enabled if the drive explicitly
546 * enables it _and_ the VM is configured to stop on errors */
547 bool blk_iostatus_is_enabled(const BlockBackend *blk)
549 return (blk->iostatus_enabled &&
550 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
551 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
552 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
555 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
557 return blk->iostatus;
560 void blk_iostatus_disable(BlockBackend *blk)
562 blk->iostatus_enabled = false;
565 void blk_iostatus_reset(BlockBackend *blk)
567 if (blk_iostatus_is_enabled(blk)) {
568 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
569 if (blk->bs && blk->bs->job) {
570 block_job_iostatus_reset(blk->bs->job);
575 void blk_iostatus_set_err(BlockBackend *blk, int error)
577 assert(blk_iostatus_is_enabled(blk));
578 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
579 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
580 BLOCK_DEVICE_IO_STATUS_FAILED;
584 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
586 blk->allow_write_beyond_eof = allow;
589 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
590 size_t size)
592 int64_t len;
594 if (size > INT_MAX) {
595 return -EIO;
598 if (!blk_is_available(blk)) {
599 return -ENOMEDIUM;
602 if (offset < 0) {
603 return -EIO;
606 if (!blk->allow_write_beyond_eof) {
607 len = blk_getlength(blk);
608 if (len < 0) {
609 return len;
612 if (offset > len || len - offset < size) {
613 return -EIO;
617 return 0;
620 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
621 int nb_sectors)
623 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
624 return -EIO;
627 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
628 return -EIO;
631 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
632 nb_sectors * BDRV_SECTOR_SIZE);
635 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
636 int nb_sectors)
638 int ret = blk_check_request(blk, sector_num, nb_sectors);
639 if (ret < 0) {
640 return ret;
643 return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
646 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
647 int nb_sectors)
649 int ret = blk_check_request(blk, sector_num, nb_sectors);
650 if (ret < 0) {
651 return ret;
654 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
657 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
658 int nb_sectors)
660 int ret = blk_check_request(blk, sector_num, nb_sectors);
661 if (ret < 0) {
662 return ret;
665 return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
668 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
669 int nb_sectors, BdrvRequestFlags flags)
671 int ret = blk_check_request(blk, sector_num, nb_sectors);
672 if (ret < 0) {
673 return ret;
676 return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
679 static void error_callback_bh(void *opaque)
681 struct BlockBackendAIOCB *acb = opaque;
682 qemu_bh_delete(acb->bh);
683 acb->common.cb(acb->common.opaque, acb->ret);
684 qemu_aio_unref(acb);
687 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
688 BlockCompletionFunc *cb,
689 void *opaque, int ret)
691 struct BlockBackendAIOCB *acb;
692 QEMUBH *bh;
694 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
695 acb->blk = blk;
696 acb->ret = ret;
698 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
699 acb->bh = bh;
700 qemu_bh_schedule(bh);
702 return &acb->common;
705 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
706 int nb_sectors, BdrvRequestFlags flags,
707 BlockCompletionFunc *cb, void *opaque)
709 int ret = blk_check_request(blk, sector_num, nb_sectors);
710 if (ret < 0) {
711 return blk_abort_aio_request(blk, cb, opaque, ret);
714 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
715 cb, opaque);
718 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
720 int ret = blk_check_byte_request(blk, offset, count);
721 if (ret < 0) {
722 return ret;
725 return bdrv_pread(blk->bs, offset, buf, count);
728 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
730 int ret = blk_check_byte_request(blk, offset, count);
731 if (ret < 0) {
732 return ret;
735 return bdrv_pwrite(blk->bs, offset, buf, count);
738 int64_t blk_getlength(BlockBackend *blk)
740 if (!blk_is_available(blk)) {
741 return -ENOMEDIUM;
744 return bdrv_getlength(blk->bs);
747 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
749 if (!blk->bs) {
750 *nb_sectors_ptr = 0;
751 } else {
752 bdrv_get_geometry(blk->bs, nb_sectors_ptr);
756 int64_t blk_nb_sectors(BlockBackend *blk)
758 if (!blk_is_available(blk)) {
759 return -ENOMEDIUM;
762 return bdrv_nb_sectors(blk->bs);
765 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
766 QEMUIOVector *iov, int nb_sectors,
767 BlockCompletionFunc *cb, void *opaque)
769 int ret = blk_check_request(blk, sector_num, nb_sectors);
770 if (ret < 0) {
771 return blk_abort_aio_request(blk, cb, opaque, ret);
774 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
777 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
778 QEMUIOVector *iov, int nb_sectors,
779 BlockCompletionFunc *cb, void *opaque)
781 int ret = blk_check_request(blk, sector_num, nb_sectors);
782 if (ret < 0) {
783 return blk_abort_aio_request(blk, cb, opaque, ret);
786 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
789 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
790 BlockCompletionFunc *cb, void *opaque)
792 if (!blk_is_available(blk)) {
793 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
796 return bdrv_aio_flush(blk->bs, cb, opaque);
799 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
800 int64_t sector_num, int nb_sectors,
801 BlockCompletionFunc *cb, void *opaque)
803 int ret = blk_check_request(blk, sector_num, nb_sectors);
804 if (ret < 0) {
805 return blk_abort_aio_request(blk, cb, opaque, ret);
808 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
811 void blk_aio_cancel(BlockAIOCB *acb)
813 bdrv_aio_cancel(acb);
816 void blk_aio_cancel_async(BlockAIOCB *acb)
818 bdrv_aio_cancel_async(acb);
821 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
823 int i, ret;
825 for (i = 0; i < num_reqs; i++) {
826 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
827 if (ret < 0) {
828 return ret;
832 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
835 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
837 if (!blk_is_available(blk)) {
838 return -ENOMEDIUM;
841 return bdrv_ioctl(blk->bs, req, buf);
844 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
845 BlockCompletionFunc *cb, void *opaque)
847 if (!blk_is_available(blk)) {
848 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
851 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
854 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
856 int ret = blk_check_request(blk, sector_num, nb_sectors);
857 if (ret < 0) {
858 return ret;
861 return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
864 int blk_co_flush(BlockBackend *blk)
866 if (!blk_is_available(blk)) {
867 return -ENOMEDIUM;
870 return bdrv_co_flush(blk->bs);
873 int blk_flush(BlockBackend *blk)
875 if (!blk_is_available(blk)) {
876 return -ENOMEDIUM;
879 return bdrv_flush(blk->bs);
882 int blk_flush_all(void)
884 return bdrv_flush_all();
887 void blk_drain(BlockBackend *blk)
889 if (blk->bs) {
890 bdrv_drain(blk->bs);
894 void blk_drain_all(void)
896 bdrv_drain_all();
899 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
900 BlockdevOnError on_write_error)
902 blk->on_read_error = on_read_error;
903 blk->on_write_error = on_write_error;
906 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
908 return is_read ? blk->on_read_error : blk->on_write_error;
911 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
912 int error)
914 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
916 switch (on_err) {
917 case BLOCKDEV_ON_ERROR_ENOSPC:
918 return (error == ENOSPC) ?
919 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
920 case BLOCKDEV_ON_ERROR_STOP:
921 return BLOCK_ERROR_ACTION_STOP;
922 case BLOCKDEV_ON_ERROR_REPORT:
923 return BLOCK_ERROR_ACTION_REPORT;
924 case BLOCKDEV_ON_ERROR_IGNORE:
925 return BLOCK_ERROR_ACTION_IGNORE;
926 default:
927 abort();
931 static void send_qmp_error_event(BlockBackend *blk,
932 BlockErrorAction action,
933 bool is_read, int error)
935 IoOperationType optype;
937 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
938 qapi_event_send_block_io_error(blk_name(blk), optype, action,
939 blk_iostatus_is_enabled(blk),
940 error == ENOSPC, strerror(error),
941 &error_abort);
944 /* This is done by device models because, while the block layer knows
945 * about the error, it does not know whether an operation comes from
946 * the device or the block layer (from a job, for example).
948 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
949 bool is_read, int error)
951 assert(error >= 0);
953 if (action == BLOCK_ERROR_ACTION_STOP) {
954 /* First set the iostatus, so that "info block" returns an iostatus
955 * that matches the events raised so far (an additional error iostatus
956 * is fine, but not a lost one).
958 blk_iostatus_set_err(blk, error);
960 /* Then raise the request to stop the VM and the event.
961 * qemu_system_vmstop_request_prepare has two effects. First,
962 * it ensures that the STOP event always comes after the
963 * BLOCK_IO_ERROR event. Second, it ensures that even if management
964 * can observe the STOP event and do a "cont" before the STOP
965 * event is issued, the VM will not stop. In this case, vm_start()
966 * also ensures that the STOP/RESUME pair of events is emitted.
968 qemu_system_vmstop_request_prepare();
969 send_qmp_error_event(blk, action, is_read, error);
970 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
971 } else {
972 send_qmp_error_event(blk, action, is_read, error);
976 int blk_is_read_only(BlockBackend *blk)
978 if (blk->bs) {
979 return bdrv_is_read_only(blk->bs);
980 } else {
981 return blk->root_state.read_only;
985 int blk_is_sg(BlockBackend *blk)
987 if (!blk->bs) {
988 return 0;
991 return bdrv_is_sg(blk->bs);
994 int blk_enable_write_cache(BlockBackend *blk)
996 if (blk->bs) {
997 return bdrv_enable_write_cache(blk->bs);
998 } else {
999 return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB);
1003 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1005 if (blk->bs) {
1006 bdrv_set_enable_write_cache(blk->bs, wce);
1007 } else {
1008 if (wce) {
1009 blk->root_state.open_flags |= BDRV_O_CACHE_WB;
1010 } else {
1011 blk->root_state.open_flags &= ~BDRV_O_CACHE_WB;
1016 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1018 if (!blk->bs) {
1019 error_setg(errp, "Device '%s' has no medium", blk->name);
1020 return;
1023 bdrv_invalidate_cache(blk->bs, errp);
1026 bool blk_is_inserted(BlockBackend *blk)
1028 return blk->bs && bdrv_is_inserted(blk->bs);
1031 bool blk_is_available(BlockBackend *blk)
1033 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1036 void blk_lock_medium(BlockBackend *blk, bool locked)
1038 if (blk->bs) {
1039 bdrv_lock_medium(blk->bs, locked);
1043 void blk_eject(BlockBackend *blk, bool eject_flag)
1045 if (blk->bs) {
1046 bdrv_eject(blk->bs, eject_flag);
1050 int blk_get_flags(BlockBackend *blk)
1052 if (blk->bs) {
1053 return bdrv_get_flags(blk->bs);
1054 } else {
1055 return blk->root_state.open_flags;
1059 int blk_get_max_transfer_length(BlockBackend *blk)
1061 if (blk->bs) {
1062 return blk->bs->bl.max_transfer_length;
1063 } else {
1064 return 0;
1068 int blk_get_max_iov(BlockBackend *blk)
1070 return blk->bs->bl.max_iov;
1073 void blk_set_guest_block_size(BlockBackend *blk, int align)
1075 blk->guest_block_size = align;
1078 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1080 return qemu_try_blockalign(blk ? blk->bs : NULL, size);
1083 void *blk_blockalign(BlockBackend *blk, size_t size)
1085 return qemu_blockalign(blk ? blk->bs : NULL, size);
1088 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1090 if (!blk->bs) {
1091 return false;
1094 return bdrv_op_is_blocked(blk->bs, op, errp);
1097 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1099 if (blk->bs) {
1100 bdrv_op_unblock(blk->bs, op, reason);
1104 void blk_op_block_all(BlockBackend *blk, Error *reason)
1106 if (blk->bs) {
1107 bdrv_op_block_all(blk->bs, reason);
1111 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1113 if (blk->bs) {
1114 bdrv_op_unblock_all(blk->bs, reason);
1118 AioContext *blk_get_aio_context(BlockBackend *blk)
1120 if (blk->bs) {
1121 return bdrv_get_aio_context(blk->bs);
1122 } else {
1123 return qemu_get_aio_context();
1127 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1129 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1130 return blk_get_aio_context(blk_acb->blk);
1133 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1135 if (blk->bs) {
1136 bdrv_set_aio_context(blk->bs, new_context);
1140 void blk_add_aio_context_notifier(BlockBackend *blk,
1141 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1142 void (*detach_aio_context)(void *opaque), void *opaque)
1144 if (blk->bs) {
1145 bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
1146 detach_aio_context, opaque);
1150 void blk_remove_aio_context_notifier(BlockBackend *blk,
1151 void (*attached_aio_context)(AioContext *,
1152 void *),
1153 void (*detach_aio_context)(void *),
1154 void *opaque)
1156 if (blk->bs) {
1157 bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
1158 detach_aio_context, opaque);
1162 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1164 notifier_list_add(&blk->remove_bs_notifiers, notify);
1167 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1169 notifier_list_add(&blk->insert_bs_notifiers, notify);
1172 void blk_io_plug(BlockBackend *blk)
1174 if (blk->bs) {
1175 bdrv_io_plug(blk->bs);
1179 void blk_io_unplug(BlockBackend *blk)
1181 if (blk->bs) {
1182 bdrv_io_unplug(blk->bs);
1186 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1188 return &blk->stats;
1191 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1192 BlockCompletionFunc *cb, void *opaque)
1194 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1197 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
1198 int nb_sectors, BdrvRequestFlags flags)
1200 int ret = blk_check_request(blk, sector_num, nb_sectors);
1201 if (ret < 0) {
1202 return ret;
1205 return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
1208 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1209 const uint8_t *buf, int nb_sectors)
1211 int ret = blk_check_request(blk, sector_num, nb_sectors);
1212 if (ret < 0) {
1213 return ret;
1216 return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
1219 int blk_truncate(BlockBackend *blk, int64_t offset)
1221 if (!blk_is_available(blk)) {
1222 return -ENOMEDIUM;
1225 return bdrv_truncate(blk->bs, offset);
1228 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1230 int ret = blk_check_request(blk, sector_num, nb_sectors);
1231 if (ret < 0) {
1232 return ret;
1235 return bdrv_discard(blk->bs, sector_num, nb_sectors);
1238 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1239 int64_t pos, int size)
1241 if (!blk_is_available(blk)) {
1242 return -ENOMEDIUM;
1245 return bdrv_save_vmstate(blk->bs, buf, pos, size);
1248 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1250 if (!blk_is_available(blk)) {
1251 return -ENOMEDIUM;
1254 return bdrv_load_vmstate(blk->bs, buf, pos, size);
1257 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1259 if (!blk_is_available(blk)) {
1260 return -ENOMEDIUM;
1263 return bdrv_probe_blocksizes(blk->bs, bsz);
1266 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1268 if (!blk_is_available(blk)) {
1269 return -ENOMEDIUM;
1272 return bdrv_probe_geometry(blk->bs, geo);
1276 * Updates the BlockBackendRootState object with data from the currently
1277 * attached BlockDriverState.
1279 void blk_update_root_state(BlockBackend *blk)
1281 assert(blk->bs);
1283 blk->root_state.open_flags = blk->bs->open_flags;
1284 blk->root_state.read_only = blk->bs->read_only;
1285 blk->root_state.detect_zeroes = blk->bs->detect_zeroes;
1287 if (blk->root_state.throttle_group) {
1288 g_free(blk->root_state.throttle_group);
1289 throttle_group_unref(blk->root_state.throttle_state);
1291 if (blk->bs->throttle_state) {
1292 const char *name = throttle_group_get_name(blk->bs);
1293 blk->root_state.throttle_group = g_strdup(name);
1294 blk->root_state.throttle_state = throttle_group_incref(name);
1295 } else {
1296 blk->root_state.throttle_group = NULL;
1297 blk->root_state.throttle_state = NULL;
1302 * Applies the information in the root state to the given BlockDriverState. This
1303 * does not include the flags which have to be specified for bdrv_open(), use
1304 * blk_get_open_flags_from_root_state() to inquire them.
1306 void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1308 bs->detect_zeroes = blk->root_state.detect_zeroes;
1309 if (blk->root_state.throttle_group) {
1310 bdrv_io_limits_enable(bs, blk->root_state.throttle_group);
1315 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1316 * supposed to inherit the root state.
1318 int blk_get_open_flags_from_root_state(BlockBackend *blk)
1320 int bs_flags;
1322 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1323 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1325 return bs_flags;
1328 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1330 return &blk->root_state;