block: Add blk_remove_bs()
[qemu/ar7.git] / block / block-backend.c
blob878c448855f5bbde6c1d135d06ea69d69f36aa0f
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 "sysemu/block-backend.h"
14 #include "block/block_int.h"
15 #include "block/blockjob.h"
16 #include "block/throttle-groups.h"
17 #include "sysemu/blockdev.h"
18 #include "sysemu/sysemu.h"
19 #include "qapi-event.h"
21 /* Number of coroutines to reserve per attached device model */
22 #define COROUTINE_POOL_RESERVATION 64
24 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
26 struct BlockBackend {
27 char *name;
28 int refcnt;
29 BlockDriverState *bs;
30 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
31 QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */
33 void *dev; /* attached device model, if any */
34 /* TODO change to DeviceState when all users are qdevified */
35 const BlockDevOps *dev_ops;
36 void *dev_opaque;
38 /* the block size for which the guest device expects atomicity */
39 int guest_block_size;
41 /* If the BDS tree is removed, some of its options are stored here (which
42 * can be used to restore those options in the new BDS on insert) */
43 BlockBackendRootState root_state;
45 /* I/O stats (display with "info blockstats"). */
46 BlockAcctStats stats;
48 BlockdevOnError on_read_error, on_write_error;
49 bool iostatus_enabled;
50 BlockDeviceIoStatus iostatus;
53 typedef struct BlockBackendAIOCB {
54 BlockAIOCB common;
55 QEMUBH *bh;
56 BlockBackend *blk;
57 int ret;
58 } BlockBackendAIOCB;
60 static const AIOCBInfo block_backend_aiocb_info = {
61 .get_aio_context = blk_aiocb_get_aio_context,
62 .aiocb_size = sizeof(BlockBackendAIOCB),
65 static void drive_info_del(DriveInfo *dinfo);
67 /* All the BlockBackends (except for hidden ones) */
68 static QTAILQ_HEAD(, BlockBackend) blk_backends =
69 QTAILQ_HEAD_INITIALIZER(blk_backends);
72 * Create a new BlockBackend with @name, with a reference count of one.
73 * @name must not be null or empty.
74 * Fail if a BlockBackend with this name already exists.
75 * Store an error through @errp on failure, unless it's null.
76 * Return the new BlockBackend on success, null on failure.
78 BlockBackend *blk_new(const char *name, Error **errp)
80 BlockBackend *blk;
82 assert(name && name[0]);
83 if (!id_wellformed(name)) {
84 error_setg(errp, "Invalid device name");
85 return NULL;
87 if (blk_by_name(name)) {
88 error_setg(errp, "Device with id '%s' already exists", name);
89 return NULL;
91 if (bdrv_find_node(name)) {
92 error_setg(errp,
93 "Device name '%s' conflicts with an existing node name",
94 name);
95 return NULL;
98 blk = g_new0(BlockBackend, 1);
99 blk->name = g_strdup(name);
100 blk->refcnt = 1;
101 QTAILQ_INSERT_TAIL(&blk_backends, blk, link);
102 return blk;
106 * Create a new BlockBackend with a new BlockDriverState attached.
107 * Otherwise just like blk_new(), which see.
109 BlockBackend *blk_new_with_bs(const char *name, Error **errp)
111 BlockBackend *blk;
112 BlockDriverState *bs;
114 blk = blk_new(name, errp);
115 if (!blk) {
116 return NULL;
119 bs = bdrv_new_root();
120 blk->bs = bs;
121 bs->blk = blk;
122 return blk;
126 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
128 * Just as with bdrv_open(), after having called this function the reference to
129 * @options belongs to the block layer (even on failure).
131 * TODO: Remove @filename and @flags; it should be possible to specify a whole
132 * BDS tree just by specifying the @options QDict (or @reference,
133 * alternatively). At the time of adding this function, this is not possible,
134 * though, so callers of this function have to be able to specify @filename and
135 * @flags.
137 BlockBackend *blk_new_open(const char *name, const char *filename,
138 const char *reference, QDict *options, int flags,
139 Error **errp)
141 BlockBackend *blk;
142 int ret;
144 blk = blk_new_with_bs(name, errp);
145 if (!blk) {
146 QDECREF(options);
147 return NULL;
150 ret = bdrv_open(&blk->bs, filename, reference, options, flags, errp);
151 if (ret < 0) {
152 blk_unref(blk);
153 return NULL;
156 return blk;
159 static void blk_delete(BlockBackend *blk)
161 assert(!blk->refcnt);
162 assert(!blk->dev);
163 if (blk->bs) {
164 assert(blk->bs->blk == blk);
165 blk->bs->blk = NULL;
166 bdrv_unref(blk->bs);
167 blk->bs = NULL;
169 if (blk->root_state.throttle_state) {
170 g_free(blk->root_state.throttle_group);
171 throttle_group_unref(blk->root_state.throttle_state);
173 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
174 if (blk->name[0]) {
175 QTAILQ_REMOVE(&blk_backends, blk, link);
177 g_free(blk->name);
178 drive_info_del(blk->legacy_dinfo);
179 g_free(blk);
182 static void drive_info_del(DriveInfo *dinfo)
184 if (!dinfo) {
185 return;
187 qemu_opts_del(dinfo->opts);
188 g_free(dinfo->serial);
189 g_free(dinfo);
193 * Increment @blk's reference count.
194 * @blk must not be null.
196 void blk_ref(BlockBackend *blk)
198 blk->refcnt++;
202 * Decrement @blk's reference count.
203 * If this drops it to zero, destroy @blk.
204 * For convenience, do nothing if @blk is null.
206 void blk_unref(BlockBackend *blk)
208 if (blk) {
209 assert(blk->refcnt > 0);
210 if (!--blk->refcnt) {
211 blk_delete(blk);
217 * Return the BlockBackend after @blk.
218 * If @blk is null, return the first one.
219 * Else, return @blk's next sibling, which may be null.
221 * To iterate over all BlockBackends, do
222 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
223 * ...
226 BlockBackend *blk_next(BlockBackend *blk)
228 return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
232 * Return @blk's name, a non-null string.
233 * Wart: the name is empty iff @blk has been hidden with
234 * blk_hide_on_behalf_of_hmp_drive_del().
236 const char *blk_name(BlockBackend *blk)
238 return blk->name;
242 * Return the BlockBackend with name @name if it exists, else null.
243 * @name must not be null.
245 BlockBackend *blk_by_name(const char *name)
247 BlockBackend *blk;
249 assert(name);
250 QTAILQ_FOREACH(blk, &blk_backends, link) {
251 if (!strcmp(name, blk->name)) {
252 return blk;
255 return NULL;
259 * Return the BlockDriverState attached to @blk if any, else null.
261 BlockDriverState *blk_bs(BlockBackend *blk)
263 return blk->bs;
267 * Changes the BlockDriverState attached to @blk
269 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs)
271 bdrv_ref(bs);
273 if (blk->bs) {
274 blk->bs->blk = NULL;
275 bdrv_unref(blk->bs);
277 assert(bs->blk == NULL);
279 blk->bs = bs;
280 bs->blk = blk;
284 * Return @blk's DriveInfo if any, else null.
286 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
288 return blk->legacy_dinfo;
292 * Set @blk's DriveInfo to @dinfo, and return it.
293 * @blk must not have a DriveInfo set already.
294 * No other BlockBackend may have the same DriveInfo set.
296 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
298 assert(!blk->legacy_dinfo);
299 return blk->legacy_dinfo = dinfo;
303 * Return the BlockBackend with DriveInfo @dinfo.
304 * It must exist.
306 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
308 BlockBackend *blk;
310 QTAILQ_FOREACH(blk, &blk_backends, link) {
311 if (blk->legacy_dinfo == dinfo) {
312 return blk;
315 abort();
319 * Hide @blk.
320 * @blk must not have been hidden already.
321 * Make attached BlockDriverState, if any, anonymous.
322 * Once hidden, @blk is invisible to all functions that don't receive
323 * it as argument. For example, blk_by_name() won't return it.
324 * Strictly for use by do_drive_del().
325 * TODO get rid of it!
327 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
329 QTAILQ_REMOVE(&blk_backends, blk, link);
330 blk->name[0] = 0;
331 if (blk->bs) {
332 bdrv_make_anon(blk->bs);
337 * Disassociates the currently associated BlockDriverState from @blk.
339 void blk_remove_bs(BlockBackend *blk)
341 blk_update_root_state(blk);
343 blk->bs->blk = NULL;
344 bdrv_unref(blk->bs);
345 blk->bs = NULL;
349 * Associates a new BlockDriverState with @blk.
351 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
353 assert(!blk->bs && !bs->blk);
354 bdrv_ref(bs);
355 blk->bs = bs;
356 bs->blk = blk;
360 * Attach device model @dev to @blk.
361 * Return 0 on success, -EBUSY when a device model is attached already.
363 int blk_attach_dev(BlockBackend *blk, void *dev)
364 /* TODO change to DeviceState *dev when all users are qdevified */
366 if (blk->dev) {
367 return -EBUSY;
369 blk_ref(blk);
370 blk->dev = dev;
371 blk_iostatus_reset(blk);
372 return 0;
376 * Attach device model @dev to @blk.
377 * @blk must not have a device model attached already.
378 * TODO qdevified devices don't use this, remove when devices are qdevified
380 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
382 if (blk_attach_dev(blk, dev) < 0) {
383 abort();
388 * Detach device model @dev from @blk.
389 * @dev must be currently attached to @blk.
391 void blk_detach_dev(BlockBackend *blk, void *dev)
392 /* TODO change to DeviceState *dev when all users are qdevified */
394 assert(blk->dev == dev);
395 blk->dev = NULL;
396 blk->dev_ops = NULL;
397 blk->dev_opaque = NULL;
398 blk->guest_block_size = 512;
399 blk_unref(blk);
403 * Return the device model attached to @blk if any, else null.
405 void *blk_get_attached_dev(BlockBackend *blk)
406 /* TODO change to return DeviceState * when all users are qdevified */
408 return blk->dev;
412 * Set @blk's device model callbacks to @ops.
413 * @opaque is the opaque argument to pass to the callbacks.
414 * This is for use by device models.
416 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
417 void *opaque)
419 blk->dev_ops = ops;
420 blk->dev_opaque = opaque;
424 * Notify @blk's attached device model of media change.
425 * If @load is true, notify of media load.
426 * Else, notify of media eject.
427 * Also send DEVICE_TRAY_MOVED events as appropriate.
429 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
431 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
432 bool tray_was_closed = !blk_dev_is_tray_open(blk);
434 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
435 if (tray_was_closed) {
436 /* tray open */
437 qapi_event_send_device_tray_moved(blk_name(blk),
438 true, &error_abort);
440 if (load) {
441 /* tray close */
442 qapi_event_send_device_tray_moved(blk_name(blk),
443 false, &error_abort);
449 * Does @blk's attached device model have removable media?
450 * %true if no device model is attached.
452 bool blk_dev_has_removable_media(BlockBackend *blk)
454 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
458 * Notify @blk's attached device model of a media eject request.
459 * If @force is true, the medium is about to be yanked out forcefully.
461 void blk_dev_eject_request(BlockBackend *blk, bool force)
463 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
464 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
469 * Does @blk's attached device model have a tray, and is it open?
471 bool blk_dev_is_tray_open(BlockBackend *blk)
473 if (blk->dev_ops && blk->dev_ops->is_tray_open) {
474 return blk->dev_ops->is_tray_open(blk->dev_opaque);
476 return false;
480 * Does @blk's attached device model have the medium locked?
481 * %false if the device model has no such lock.
483 bool blk_dev_is_medium_locked(BlockBackend *blk)
485 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
486 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
488 return false;
492 * Notify @blk's attached device model of a backend size change.
494 void blk_dev_resize_cb(BlockBackend *blk)
496 if (blk->dev_ops && blk->dev_ops->resize_cb) {
497 blk->dev_ops->resize_cb(blk->dev_opaque);
501 void blk_iostatus_enable(BlockBackend *blk)
503 blk->iostatus_enabled = true;
504 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
507 /* The I/O status is only enabled if the drive explicitly
508 * enables it _and_ the VM is configured to stop on errors */
509 bool blk_iostatus_is_enabled(const BlockBackend *blk)
511 return (blk->iostatus_enabled &&
512 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
513 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
514 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
517 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
519 return blk->iostatus;
522 void blk_iostatus_disable(BlockBackend *blk)
524 blk->iostatus_enabled = false;
527 void blk_iostatus_reset(BlockBackend *blk)
529 if (blk_iostatus_is_enabled(blk)) {
530 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
531 if (blk->bs && blk->bs->job) {
532 block_job_iostatus_reset(blk->bs->job);
537 void blk_iostatus_set_err(BlockBackend *blk, int error)
539 assert(blk_iostatus_is_enabled(blk));
540 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
541 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
542 BLOCK_DEVICE_IO_STATUS_FAILED;
546 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
547 size_t size)
549 int64_t len;
551 if (size > INT_MAX) {
552 return -EIO;
555 if (!blk_is_available(blk)) {
556 return -ENOMEDIUM;
559 len = blk_getlength(blk);
560 if (len < 0) {
561 return len;
564 if (offset < 0) {
565 return -EIO;
568 if (offset > len || len - offset < size) {
569 return -EIO;
572 return 0;
575 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
576 int nb_sectors)
578 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
579 return -EIO;
582 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
583 return -EIO;
586 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
587 nb_sectors * BDRV_SECTOR_SIZE);
590 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
591 int nb_sectors)
593 int ret = blk_check_request(blk, sector_num, nb_sectors);
594 if (ret < 0) {
595 return ret;
598 return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
601 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
602 int nb_sectors)
604 int ret = blk_check_request(blk, sector_num, nb_sectors);
605 if (ret < 0) {
606 return ret;
609 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
612 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
613 int nb_sectors)
615 int ret = blk_check_request(blk, sector_num, nb_sectors);
616 if (ret < 0) {
617 return ret;
620 return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
623 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
624 int nb_sectors, BdrvRequestFlags flags)
626 int ret = blk_check_request(blk, sector_num, nb_sectors);
627 if (ret < 0) {
628 return ret;
631 return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
634 static void error_callback_bh(void *opaque)
636 struct BlockBackendAIOCB *acb = opaque;
637 qemu_bh_delete(acb->bh);
638 acb->common.cb(acb->common.opaque, acb->ret);
639 qemu_aio_unref(acb);
642 static BlockAIOCB *abort_aio_request(BlockBackend *blk, BlockCompletionFunc *cb,
643 void *opaque, int ret)
645 struct BlockBackendAIOCB *acb;
646 QEMUBH *bh;
648 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
649 acb->blk = blk;
650 acb->ret = ret;
652 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
653 acb->bh = bh;
654 qemu_bh_schedule(bh);
656 return &acb->common;
659 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
660 int nb_sectors, BdrvRequestFlags flags,
661 BlockCompletionFunc *cb, void *opaque)
663 int ret = blk_check_request(blk, sector_num, nb_sectors);
664 if (ret < 0) {
665 return abort_aio_request(blk, cb, opaque, ret);
668 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
669 cb, opaque);
672 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
674 int ret = blk_check_byte_request(blk, offset, count);
675 if (ret < 0) {
676 return ret;
679 return bdrv_pread(blk->bs, offset, buf, count);
682 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
684 int ret = blk_check_byte_request(blk, offset, count);
685 if (ret < 0) {
686 return ret;
689 return bdrv_pwrite(blk->bs, offset, buf, count);
692 int64_t blk_getlength(BlockBackend *blk)
694 if (!blk_is_available(blk)) {
695 return -ENOMEDIUM;
698 return bdrv_getlength(blk->bs);
701 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
703 if (!blk->bs) {
704 *nb_sectors_ptr = 0;
705 } else {
706 bdrv_get_geometry(blk->bs, nb_sectors_ptr);
710 int64_t blk_nb_sectors(BlockBackend *blk)
712 if (!blk_is_available(blk)) {
713 return -ENOMEDIUM;
716 return bdrv_nb_sectors(blk->bs);
719 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
720 QEMUIOVector *iov, int nb_sectors,
721 BlockCompletionFunc *cb, void *opaque)
723 int ret = blk_check_request(blk, sector_num, nb_sectors);
724 if (ret < 0) {
725 return abort_aio_request(blk, cb, opaque, ret);
728 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
731 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
732 QEMUIOVector *iov, int nb_sectors,
733 BlockCompletionFunc *cb, void *opaque)
735 int ret = blk_check_request(blk, sector_num, nb_sectors);
736 if (ret < 0) {
737 return abort_aio_request(blk, cb, opaque, ret);
740 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
743 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
744 BlockCompletionFunc *cb, void *opaque)
746 if (!blk_is_available(blk)) {
747 return abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
750 return bdrv_aio_flush(blk->bs, cb, opaque);
753 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
754 int64_t sector_num, int nb_sectors,
755 BlockCompletionFunc *cb, void *opaque)
757 int ret = blk_check_request(blk, sector_num, nb_sectors);
758 if (ret < 0) {
759 return abort_aio_request(blk, cb, opaque, ret);
762 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
765 void blk_aio_cancel(BlockAIOCB *acb)
767 bdrv_aio_cancel(acb);
770 void blk_aio_cancel_async(BlockAIOCB *acb)
772 bdrv_aio_cancel_async(acb);
775 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
777 int i, ret;
779 for (i = 0; i < num_reqs; i++) {
780 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
781 if (ret < 0) {
782 return ret;
786 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
789 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
791 if (!blk_is_available(blk)) {
792 return -ENOMEDIUM;
795 return bdrv_ioctl(blk->bs, req, buf);
798 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
799 BlockCompletionFunc *cb, void *opaque)
801 if (!blk_is_available(blk)) {
802 return abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
805 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
808 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
810 int ret = blk_check_request(blk, sector_num, nb_sectors);
811 if (ret < 0) {
812 return ret;
815 return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
818 int blk_co_flush(BlockBackend *blk)
820 if (!blk_is_available(blk)) {
821 return -ENOMEDIUM;
824 return bdrv_co_flush(blk->bs);
827 int blk_flush(BlockBackend *blk)
829 if (!blk_is_available(blk)) {
830 return -ENOMEDIUM;
833 return bdrv_flush(blk->bs);
836 int blk_flush_all(void)
838 return bdrv_flush_all();
841 void blk_drain(BlockBackend *blk)
843 if (blk->bs) {
844 bdrv_drain(blk->bs);
848 void blk_drain_all(void)
850 bdrv_drain_all();
853 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
854 BlockdevOnError on_write_error)
856 blk->on_read_error = on_read_error;
857 blk->on_write_error = on_write_error;
860 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
862 return is_read ? blk->on_read_error : blk->on_write_error;
865 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
866 int error)
868 BlockdevOnError on_err = blk_get_on_error(blk, is_read);
870 switch (on_err) {
871 case BLOCKDEV_ON_ERROR_ENOSPC:
872 return (error == ENOSPC) ?
873 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
874 case BLOCKDEV_ON_ERROR_STOP:
875 return BLOCK_ERROR_ACTION_STOP;
876 case BLOCKDEV_ON_ERROR_REPORT:
877 return BLOCK_ERROR_ACTION_REPORT;
878 case BLOCKDEV_ON_ERROR_IGNORE:
879 return BLOCK_ERROR_ACTION_IGNORE;
880 default:
881 abort();
885 static void send_qmp_error_event(BlockBackend *blk,
886 BlockErrorAction action,
887 bool is_read, int error)
889 IoOperationType optype;
891 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
892 qapi_event_send_block_io_error(blk_name(blk), optype, action,
893 blk_iostatus_is_enabled(blk),
894 error == ENOSPC, strerror(error),
895 &error_abort);
898 /* This is done by device models because, while the block layer knows
899 * about the error, it does not know whether an operation comes from
900 * the device or the block layer (from a job, for example).
902 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
903 bool is_read, int error)
905 assert(error >= 0);
907 if (action == BLOCK_ERROR_ACTION_STOP) {
908 /* First set the iostatus, so that "info block" returns an iostatus
909 * that matches the events raised so far (an additional error iostatus
910 * is fine, but not a lost one).
912 blk_iostatus_set_err(blk, error);
914 /* Then raise the request to stop the VM and the event.
915 * qemu_system_vmstop_request_prepare has two effects. First,
916 * it ensures that the STOP event always comes after the
917 * BLOCK_IO_ERROR event. Second, it ensures that even if management
918 * can observe the STOP event and do a "cont" before the STOP
919 * event is issued, the VM will not stop. In this case, vm_start()
920 * also ensures that the STOP/RESUME pair of events is emitted.
922 qemu_system_vmstop_request_prepare();
923 send_qmp_error_event(blk, action, is_read, error);
924 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
925 } else {
926 send_qmp_error_event(blk, action, is_read, error);
930 int blk_is_read_only(BlockBackend *blk)
932 if (blk->bs) {
933 return bdrv_is_read_only(blk->bs);
934 } else {
935 return blk->root_state.read_only;
939 int blk_is_sg(BlockBackend *blk)
941 if (!blk->bs) {
942 return 0;
945 return bdrv_is_sg(blk->bs);
948 int blk_enable_write_cache(BlockBackend *blk)
950 if (blk->bs) {
951 return bdrv_enable_write_cache(blk->bs);
952 } else {
953 return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB);
957 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
959 if (blk->bs) {
960 bdrv_set_enable_write_cache(blk->bs, wce);
961 } else {
962 if (wce) {
963 blk->root_state.open_flags |= BDRV_O_CACHE_WB;
964 } else {
965 blk->root_state.open_flags &= ~BDRV_O_CACHE_WB;
970 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
972 if (!blk->bs) {
973 error_setg(errp, "Device '%s' has no medium", blk->name);
974 return;
977 bdrv_invalidate_cache(blk->bs, errp);
980 bool blk_is_inserted(BlockBackend *blk)
982 return blk->bs && bdrv_is_inserted(blk->bs);
985 bool blk_is_available(BlockBackend *blk)
987 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
990 void blk_lock_medium(BlockBackend *blk, bool locked)
992 if (blk->bs) {
993 bdrv_lock_medium(blk->bs, locked);
997 void blk_eject(BlockBackend *blk, bool eject_flag)
999 if (blk->bs) {
1000 bdrv_eject(blk->bs, eject_flag);
1004 int blk_get_flags(BlockBackend *blk)
1006 if (blk->bs) {
1007 return bdrv_get_flags(blk->bs);
1008 } else {
1009 return blk->root_state.open_flags;
1013 int blk_get_max_transfer_length(BlockBackend *blk)
1015 if (blk->bs) {
1016 return blk->bs->bl.max_transfer_length;
1017 } else {
1018 return 0;
1022 void blk_set_guest_block_size(BlockBackend *blk, int align)
1024 blk->guest_block_size = align;
1027 void *blk_blockalign(BlockBackend *blk, size_t size)
1029 return qemu_blockalign(blk ? blk->bs : NULL, size);
1032 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1034 if (!blk->bs) {
1035 return false;
1038 return bdrv_op_is_blocked(blk->bs, op, errp);
1041 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1043 if (blk->bs) {
1044 bdrv_op_unblock(blk->bs, op, reason);
1048 void blk_op_block_all(BlockBackend *blk, Error *reason)
1050 if (blk->bs) {
1051 bdrv_op_block_all(blk->bs, reason);
1055 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1057 if (blk->bs) {
1058 bdrv_op_unblock_all(blk->bs, reason);
1062 AioContext *blk_get_aio_context(BlockBackend *blk)
1064 if (blk->bs) {
1065 return bdrv_get_aio_context(blk->bs);
1066 } else {
1067 return qemu_get_aio_context();
1071 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1073 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1074 return blk_get_aio_context(blk_acb->blk);
1077 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1079 if (blk->bs) {
1080 bdrv_set_aio_context(blk->bs, new_context);
1084 void blk_add_aio_context_notifier(BlockBackend *blk,
1085 void (*attached_aio_context)(AioContext *new_context, void *opaque),
1086 void (*detach_aio_context)(void *opaque), void *opaque)
1088 if (blk->bs) {
1089 bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
1090 detach_aio_context, opaque);
1094 void blk_remove_aio_context_notifier(BlockBackend *blk,
1095 void (*attached_aio_context)(AioContext *,
1096 void *),
1097 void (*detach_aio_context)(void *),
1098 void *opaque)
1100 if (blk->bs) {
1101 bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
1102 detach_aio_context, opaque);
1106 void blk_add_close_notifier(BlockBackend *blk, Notifier *notify)
1108 if (blk->bs) {
1109 bdrv_add_close_notifier(blk->bs, notify);
1113 void blk_io_plug(BlockBackend *blk)
1115 if (blk->bs) {
1116 bdrv_io_plug(blk->bs);
1120 void blk_io_unplug(BlockBackend *blk)
1122 if (blk->bs) {
1123 bdrv_io_unplug(blk->bs);
1127 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1129 return &blk->stats;
1132 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1133 BlockCompletionFunc *cb, void *opaque)
1135 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1138 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
1139 int nb_sectors, BdrvRequestFlags flags)
1141 int ret = blk_check_request(blk, sector_num, nb_sectors);
1142 if (ret < 0) {
1143 return ret;
1146 return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
1149 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1150 const uint8_t *buf, int nb_sectors)
1152 int ret = blk_check_request(blk, sector_num, nb_sectors);
1153 if (ret < 0) {
1154 return ret;
1157 return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
1160 int blk_truncate(BlockBackend *blk, int64_t offset)
1162 if (!blk_is_available(blk)) {
1163 return -ENOMEDIUM;
1166 return bdrv_truncate(blk->bs, offset);
1169 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1171 int ret = blk_check_request(blk, sector_num, nb_sectors);
1172 if (ret < 0) {
1173 return ret;
1176 return bdrv_discard(blk->bs, sector_num, nb_sectors);
1179 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1180 int64_t pos, int size)
1182 if (!blk_is_available(blk)) {
1183 return -ENOMEDIUM;
1186 return bdrv_save_vmstate(blk->bs, buf, pos, size);
1189 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1191 if (!blk_is_available(blk)) {
1192 return -ENOMEDIUM;
1195 return bdrv_load_vmstate(blk->bs, buf, pos, size);
1198 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1200 if (!blk_is_available(blk)) {
1201 return -ENOMEDIUM;
1204 return bdrv_probe_blocksizes(blk->bs, bsz);
1207 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1209 if (!blk_is_available(blk)) {
1210 return -ENOMEDIUM;
1213 return bdrv_probe_geometry(blk->bs, geo);
1217 * Updates the BlockBackendRootState object with data from the currently
1218 * attached BlockDriverState.
1220 void blk_update_root_state(BlockBackend *blk)
1222 assert(blk->bs);
1224 blk->root_state.open_flags = blk->bs->open_flags;
1225 blk->root_state.read_only = blk->bs->read_only;
1226 blk->root_state.detect_zeroes = blk->bs->detect_zeroes;
1228 if (blk->root_state.throttle_group) {
1229 g_free(blk->root_state.throttle_group);
1230 throttle_group_unref(blk->root_state.throttle_state);
1232 if (blk->bs->throttle_state) {
1233 const char *name = throttle_group_get_name(blk->bs);
1234 blk->root_state.throttle_group = g_strdup(name);
1235 blk->root_state.throttle_state = throttle_group_incref(name);
1236 } else {
1237 blk->root_state.throttle_group = NULL;
1238 blk->root_state.throttle_state = NULL;
1242 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1244 return &blk->root_state;