xref: /openbmc/qemu/block/block-backend.c (revision b917da4c)
1 /*
2  * QEMU Block backends
3  *
4  * Copyright (C) 2014 Red Hat, Inc.
5  *
6  * Authors:
7  *  Markus Armbruster <armbru@redhat.com>,
8  *
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.
11  */
12 
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"
21 
22 /* Number of coroutines to reserve per attached device model */
23 #define COROUTINE_POOL_RESERVATION 64
24 
25 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
26 
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 */
33 
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;
38 
39     /* the block size for which the guest device expects atomicity */
40     int guest_block_size;
41 
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;
45 
46     /* I/O stats (display with "info blockstats"). */
47     BlockAcctStats stats;
48 
49     BlockdevOnError on_read_error, on_write_error;
50     bool iostatus_enabled;
51     BlockDeviceIoStatus iostatus;
52 
53     bool allow_write_beyond_eof;
54 
55     NotifierList remove_bs_notifiers, insert_bs_notifiers;
56 };
57 
58 typedef struct BlockBackendAIOCB {
59     BlockAIOCB common;
60     QEMUBH *bh;
61     BlockBackend *blk;
62     int ret;
63 } BlockBackendAIOCB;
64 
65 static const AIOCBInfo block_backend_aiocb_info = {
66     .get_aio_context = blk_aiocb_get_aio_context,
67     .aiocb_size = sizeof(BlockBackendAIOCB),
68 };
69 
70 static void drive_info_del(DriveInfo *dinfo);
71 
72 /* All the BlockBackends (except for hidden ones) */
73 static QTAILQ_HEAD(, BlockBackend) blk_backends =
74     QTAILQ_HEAD_INITIALIZER(blk_backends);
75 
76 /*
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.
82  */
83 BlockBackend *blk_new(const char *name, Error **errp)
84 {
85     BlockBackend *blk;
86 
87     assert(name && name[0]);
88     if (!id_wellformed(name)) {
89         error_setg(errp, "Invalid device name");
90         return NULL;
91     }
92     if (blk_by_name(name)) {
93         error_setg(errp, "Device with id '%s' already exists", name);
94         return NULL;
95     }
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;
101     }
102 
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;
110 }
111 
112 /*
113  * Create a new BlockBackend with a new BlockDriverState attached.
114  * Otherwise just like blk_new(), which see.
115  */
116 BlockBackend *blk_new_with_bs(const char *name, Error **errp)
117 {
118     BlockBackend *blk;
119     BlockDriverState *bs;
120 
121     blk = blk_new(name, errp);
122     if (!blk) {
123         return NULL;
124     }
125 
126     bs = bdrv_new_root();
127     blk->bs = bs;
128     bs->blk = blk;
129     return blk;
130 }
131 
132 /*
133  * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
134  *
135  * Just as with bdrv_open(), after having called this function the reference to
136  * @options belongs to the block layer (even on failure).
137  *
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.
143  */
144 BlockBackend *blk_new_open(const char *name, const char *filename,
145                            const char *reference, QDict *options, int flags,
146                            Error **errp)
147 {
148     BlockBackend *blk;
149     int ret;
150 
151     blk = blk_new_with_bs(name, errp);
152     if (!blk) {
153         QDECREF(options);
154         return NULL;
155     }
156 
157     ret = bdrv_open(&blk->bs, filename, reference, options, flags, errp);
158     if (ret < 0) {
159         blk_unref(blk);
160         return NULL;
161     }
162 
163     return blk;
164 }
165 
166 static void blk_delete(BlockBackend *blk)
167 {
168     assert(!blk->refcnt);
169     assert(!blk->dev);
170     if (blk->bs) {
171         blk_remove_bs(blk);
172     }
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);
178     }
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);
182     }
183     g_free(blk->name);
184     drive_info_del(blk->legacy_dinfo);
185     block_acct_cleanup(&blk->stats);
186     g_free(blk);
187 }
188 
189 static void drive_info_del(DriveInfo *dinfo)
190 {
191     if (!dinfo) {
192         return;
193     }
194     qemu_opts_del(dinfo->opts);
195     g_free(dinfo->serial);
196     g_free(dinfo);
197 }
198 
199 int blk_get_refcnt(BlockBackend *blk)
200 {
201     return blk ? blk->refcnt : 0;
202 }
203 
204 /*
205  * Increment @blk's reference count.
206  * @blk must not be null.
207  */
208 void blk_ref(BlockBackend *blk)
209 {
210     blk->refcnt++;
211 }
212 
213 /*
214  * Decrement @blk's reference count.
215  * If this drops it to zero, destroy @blk.
216  * For convenience, do nothing if @blk is null.
217  */
218 void blk_unref(BlockBackend *blk)
219 {
220     if (blk) {
221         assert(blk->refcnt > 0);
222         if (!--blk->refcnt) {
223             blk_delete(blk);
224         }
225     }
226 }
227 
228 void blk_remove_all_bs(void)
229 {
230     BlockBackend *blk;
231 
232     QTAILQ_FOREACH(blk, &blk_backends, link) {
233         AioContext *ctx = blk_get_aio_context(blk);
234 
235         aio_context_acquire(ctx);
236         if (blk->bs) {
237             blk_remove_bs(blk);
238         }
239         aio_context_release(ctx);
240     }
241 }
242 
243 /*
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.
247  *
248  * To iterate over all BlockBackends, do
249  * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
250  *     ...
251  * }
252  */
253 BlockBackend *blk_next(BlockBackend *blk)
254 {
255     return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
256 }
257 
258 /*
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().
262  */
263 const char *blk_name(BlockBackend *blk)
264 {
265     return blk->name;
266 }
267 
268 /*
269  * Return the BlockBackend with name @name if it exists, else null.
270  * @name must not be null.
271  */
272 BlockBackend *blk_by_name(const char *name)
273 {
274     BlockBackend *blk;
275 
276     assert(name);
277     QTAILQ_FOREACH(blk, &blk_backends, link) {
278         if (!strcmp(name, blk->name)) {
279             return blk;
280         }
281     }
282     return NULL;
283 }
284 
285 /*
286  * Return the BlockDriverState attached to @blk if any, else null.
287  */
288 BlockDriverState *blk_bs(BlockBackend *blk)
289 {
290     return blk->bs;
291 }
292 
293 /*
294  * Changes the BlockDriverState attached to @blk
295  */
296 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs)
297 {
298     bdrv_ref(bs);
299 
300     if (blk->bs) {
301         blk->bs->blk = NULL;
302         bdrv_unref(blk->bs);
303     }
304     assert(bs->blk == NULL);
305 
306     blk->bs = bs;
307     bs->blk = blk;
308 }
309 
310 /*
311  * Return @blk's DriveInfo if any, else null.
312  */
313 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
314 {
315     return blk->legacy_dinfo;
316 }
317 
318 /*
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.
322  */
323 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
324 {
325     assert(!blk->legacy_dinfo);
326     return blk->legacy_dinfo = dinfo;
327 }
328 
329 /*
330  * Return the BlockBackend with DriveInfo @dinfo.
331  * It must exist.
332  */
333 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
334 {
335     BlockBackend *blk;
336 
337     QTAILQ_FOREACH(blk, &blk_backends, link) {
338         if (blk->legacy_dinfo == dinfo) {
339             return blk;
340         }
341     }
342     abort();
343 }
344 
345 /*
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!
353  */
354 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
355 {
356     QTAILQ_REMOVE(&blk_backends, blk, link);
357     blk->name[0] = 0;
358     if (blk->bs) {
359         bdrv_make_anon(blk->bs);
360     }
361 }
362 
363 /*
364  * Disassociates the currently associated BlockDriverState from @blk.
365  */
366 void blk_remove_bs(BlockBackend *blk)
367 {
368     assert(blk->bs->blk == blk);
369 
370     notifier_list_notify(&blk->remove_bs_notifiers, blk);
371 
372     blk_update_root_state(blk);
373 
374     blk->bs->blk = NULL;
375     bdrv_unref(blk->bs);
376     blk->bs = NULL;
377 }
378 
379 /*
380  * Associates a new BlockDriverState with @blk.
381  */
382 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
383 {
384     assert(!blk->bs && !bs->blk);
385     bdrv_ref(bs);
386     blk->bs = bs;
387     bs->blk = blk;
388 
389     notifier_list_notify(&blk->insert_bs_notifiers, blk);
390 }
391 
392 /*
393  * Attach device model @dev to @blk.
394  * Return 0 on success, -EBUSY when a device model is attached already.
395  */
396 int blk_attach_dev(BlockBackend *blk, void *dev)
397 /* TODO change to DeviceState *dev when all users are qdevified */
398 {
399     if (blk->dev) {
400         return -EBUSY;
401     }
402     blk_ref(blk);
403     blk->dev = dev;
404     blk_iostatus_reset(blk);
405     return 0;
406 }
407 
408 /*
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
412  */
413 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
414 {
415     if (blk_attach_dev(blk, dev) < 0) {
416         abort();
417     }
418 }
419 
420 /*
421  * Detach device model @dev from @blk.
422  * @dev must be currently attached to @blk.
423  */
424 void blk_detach_dev(BlockBackend *blk, void *dev)
425 /* TODO change to DeviceState *dev when all users are qdevified */
426 {
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);
433 }
434 
435 /*
436  * Return the device model attached to @blk if any, else null.
437  */
438 void *blk_get_attached_dev(BlockBackend *blk)
439 /* TODO change to return DeviceState * when all users are qdevified */
440 {
441     return blk->dev;
442 }
443 
444 /*
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.
448  */
449 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
450                      void *opaque)
451 {
452     blk->dev_ops = ops;
453     blk->dev_opaque = opaque;
454 }
455 
456 /*
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.
461  */
462 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
463 {
464     if (blk->dev_ops && blk->dev_ops->change_media_cb) {
465         bool tray_was_open, tray_is_open;
466 
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);
470 
471         if (tray_was_open != tray_is_open) {
472             qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
473                                               &error_abort);
474         }
475     }
476 }
477 
478 /*
479  * Does @blk's attached device model have removable media?
480  * %true if no device model is attached.
481  */
482 bool blk_dev_has_removable_media(BlockBackend *blk)
483 {
484     return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
485 }
486 
487 /*
488  * Does @blk's attached device model have a tray?
489  */
490 bool blk_dev_has_tray(BlockBackend *blk)
491 {
492     return blk->dev_ops && blk->dev_ops->is_tray_open;
493 }
494 
495 /*
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.
498  */
499 void blk_dev_eject_request(BlockBackend *blk, bool force)
500 {
501     if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
502         blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
503     }
504 }
505 
506 /*
507  * Does @blk's attached device model have a tray, and is it open?
508  */
509 bool blk_dev_is_tray_open(BlockBackend *blk)
510 {
511     if (blk_dev_has_tray(blk)) {
512         return blk->dev_ops->is_tray_open(blk->dev_opaque);
513     }
514     return false;
515 }
516 
517 /*
518  * Does @blk's attached device model have the medium locked?
519  * %false if the device model has no such lock.
520  */
521 bool blk_dev_is_medium_locked(BlockBackend *blk)
522 {
523     if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
524         return blk->dev_ops->is_medium_locked(blk->dev_opaque);
525     }
526     return false;
527 }
528 
529 /*
530  * Notify @blk's attached device model of a backend size change.
531  */
532 void blk_dev_resize_cb(BlockBackend *blk)
533 {
534     if (blk->dev_ops && blk->dev_ops->resize_cb) {
535         blk->dev_ops->resize_cb(blk->dev_opaque);
536     }
537 }
538 
539 void blk_iostatus_enable(BlockBackend *blk)
540 {
541     blk->iostatus_enabled = true;
542     blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
543 }
544 
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)
548 {
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));
553 }
554 
555 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
556 {
557     return blk->iostatus;
558 }
559 
560 void blk_iostatus_disable(BlockBackend *blk)
561 {
562     blk->iostatus_enabled = false;
563 }
564 
565 void blk_iostatus_reset(BlockBackend *blk)
566 {
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);
571         }
572     }
573 }
574 
575 void blk_iostatus_set_err(BlockBackend *blk, int error)
576 {
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;
581     }
582 }
583 
584 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
585 {
586     blk->allow_write_beyond_eof = allow;
587 }
588 
589 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
590                                   size_t size)
591 {
592     int64_t len;
593 
594     if (size > INT_MAX) {
595         return -EIO;
596     }
597 
598     if (!blk_is_available(blk)) {
599         return -ENOMEDIUM;
600     }
601 
602     if (offset < 0) {
603         return -EIO;
604     }
605 
606     if (!blk->allow_write_beyond_eof) {
607         len = blk_getlength(blk);
608         if (len < 0) {
609             return len;
610         }
611 
612         if (offset > len || len - offset < size) {
613             return -EIO;
614         }
615     }
616 
617     return 0;
618 }
619 
620 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
621                              int nb_sectors)
622 {
623     if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
624         return -EIO;
625     }
626 
627     if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
628         return -EIO;
629     }
630 
631     return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
632                                   nb_sectors * BDRV_SECTOR_SIZE);
633 }
634 
635 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
636              int nb_sectors)
637 {
638     int ret = blk_check_request(blk, sector_num, nb_sectors);
639     if (ret < 0) {
640         return ret;
641     }
642 
643     return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
644 }
645 
646 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
647                          int nb_sectors)
648 {
649     int ret = blk_check_request(blk, sector_num, nb_sectors);
650     if (ret < 0) {
651         return ret;
652     }
653 
654     return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
655 }
656 
657 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
658               int nb_sectors)
659 {
660     int ret = blk_check_request(blk, sector_num, nb_sectors);
661     if (ret < 0) {
662         return ret;
663     }
664 
665     return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
666 }
667 
668 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
669                      int nb_sectors, BdrvRequestFlags flags)
670 {
671     int ret = blk_check_request(blk, sector_num, nb_sectors);
672     if (ret < 0) {
673         return ret;
674     }
675 
676     return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
677 }
678 
679 static void error_callback_bh(void *opaque)
680 {
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);
685 }
686 
687 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
688                                   BlockCompletionFunc *cb,
689                                   void *opaque, int ret)
690 {
691     struct BlockBackendAIOCB *acb;
692     QEMUBH *bh;
693 
694     acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
695     acb->blk = blk;
696     acb->ret = ret;
697 
698     bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
699     acb->bh = bh;
700     qemu_bh_schedule(bh);
701 
702     return &acb->common;
703 }
704 
705 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
706                                  int nb_sectors, BdrvRequestFlags flags,
707                                  BlockCompletionFunc *cb, void *opaque)
708 {
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);
712     }
713 
714     return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
715                                  cb, opaque);
716 }
717 
718 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
719 {
720     int ret = blk_check_byte_request(blk, offset, count);
721     if (ret < 0) {
722         return ret;
723     }
724 
725     return bdrv_pread(blk->bs, offset, buf, count);
726 }
727 
728 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
729 {
730     int ret = blk_check_byte_request(blk, offset, count);
731     if (ret < 0) {
732         return ret;
733     }
734 
735     return bdrv_pwrite(blk->bs, offset, buf, count);
736 }
737 
738 int64_t blk_getlength(BlockBackend *blk)
739 {
740     if (!blk_is_available(blk)) {
741         return -ENOMEDIUM;
742     }
743 
744     return bdrv_getlength(blk->bs);
745 }
746 
747 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
748 {
749     if (!blk->bs) {
750         *nb_sectors_ptr = 0;
751     } else {
752         bdrv_get_geometry(blk->bs, nb_sectors_ptr);
753     }
754 }
755 
756 int64_t blk_nb_sectors(BlockBackend *blk)
757 {
758     if (!blk_is_available(blk)) {
759         return -ENOMEDIUM;
760     }
761 
762     return bdrv_nb_sectors(blk->bs);
763 }
764 
765 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
766                           QEMUIOVector *iov, int nb_sectors,
767                           BlockCompletionFunc *cb, void *opaque)
768 {
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);
772     }
773 
774     return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
775 }
776 
777 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
778                            QEMUIOVector *iov, int nb_sectors,
779                            BlockCompletionFunc *cb, void *opaque)
780 {
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);
784     }
785 
786     return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
787 }
788 
789 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
790                           BlockCompletionFunc *cb, void *opaque)
791 {
792     if (!blk_is_available(blk)) {
793         return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
794     }
795 
796     return bdrv_aio_flush(blk->bs, cb, opaque);
797 }
798 
799 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
800                             int64_t sector_num, int nb_sectors,
801                             BlockCompletionFunc *cb, void *opaque)
802 {
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);
806     }
807 
808     return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
809 }
810 
811 void blk_aio_cancel(BlockAIOCB *acb)
812 {
813     bdrv_aio_cancel(acb);
814 }
815 
816 void blk_aio_cancel_async(BlockAIOCB *acb)
817 {
818     bdrv_aio_cancel_async(acb);
819 }
820 
821 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
822 {
823     int i, ret;
824 
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;
829         }
830     }
831 
832     return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
833 }
834 
835 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
836 {
837     if (!blk_is_available(blk)) {
838         return -ENOMEDIUM;
839     }
840 
841     return bdrv_ioctl(blk->bs, req, buf);
842 }
843 
844 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
845                           BlockCompletionFunc *cb, void *opaque)
846 {
847     if (!blk_is_available(blk)) {
848         return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
849     }
850 
851     return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
852 }
853 
854 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
855 {
856     int ret = blk_check_request(blk, sector_num, nb_sectors);
857     if (ret < 0) {
858         return ret;
859     }
860 
861     return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
862 }
863 
864 int blk_co_flush(BlockBackend *blk)
865 {
866     if (!blk_is_available(blk)) {
867         return -ENOMEDIUM;
868     }
869 
870     return bdrv_co_flush(blk->bs);
871 }
872 
873 int blk_flush(BlockBackend *blk)
874 {
875     if (!blk_is_available(blk)) {
876         return -ENOMEDIUM;
877     }
878 
879     return bdrv_flush(blk->bs);
880 }
881 
882 int blk_flush_all(void)
883 {
884     return bdrv_flush_all();
885 }
886 
887 void blk_drain(BlockBackend *blk)
888 {
889     if (blk->bs) {
890         bdrv_drain(blk->bs);
891     }
892 }
893 
894 void blk_drain_all(void)
895 {
896     bdrv_drain_all();
897 }
898 
899 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
900                       BlockdevOnError on_write_error)
901 {
902     blk->on_read_error = on_read_error;
903     blk->on_write_error = on_write_error;
904 }
905 
906 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
907 {
908     return is_read ? blk->on_read_error : blk->on_write_error;
909 }
910 
911 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
912                                       int error)
913 {
914     BlockdevOnError on_err = blk_get_on_error(blk, is_read);
915 
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();
928     }
929 }
930 
931 static void send_qmp_error_event(BlockBackend *blk,
932                                  BlockErrorAction action,
933                                  bool is_read, int error)
934 {
935     IoOperationType optype;
936 
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);
942 }
943 
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).
947  */
948 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
949                       bool is_read, int error)
950 {
951     assert(error >= 0);
952 
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).
957          */
958         blk_iostatus_set_err(blk, error);
959 
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.
967          */
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);
973     }
974 }
975 
976 int blk_is_read_only(BlockBackend *blk)
977 {
978     if (blk->bs) {
979         return bdrv_is_read_only(blk->bs);
980     } else {
981         return blk->root_state.read_only;
982     }
983 }
984 
985 int blk_is_sg(BlockBackend *blk)
986 {
987     if (!blk->bs) {
988         return 0;
989     }
990 
991     return bdrv_is_sg(blk->bs);
992 }
993 
994 int blk_enable_write_cache(BlockBackend *blk)
995 {
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);
1000     }
1001 }
1002 
1003 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1004 {
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;
1012         }
1013     }
1014 }
1015 
1016 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1017 {
1018     if (!blk->bs) {
1019         error_setg(errp, "Device '%s' has no medium", blk->name);
1020         return;
1021     }
1022 
1023     bdrv_invalidate_cache(blk->bs, errp);
1024 }
1025 
1026 bool blk_is_inserted(BlockBackend *blk)
1027 {
1028     return blk->bs && bdrv_is_inserted(blk->bs);
1029 }
1030 
1031 bool blk_is_available(BlockBackend *blk)
1032 {
1033     return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1034 }
1035 
1036 void blk_lock_medium(BlockBackend *blk, bool locked)
1037 {
1038     if (blk->bs) {
1039         bdrv_lock_medium(blk->bs, locked);
1040     }
1041 }
1042 
1043 void blk_eject(BlockBackend *blk, bool eject_flag)
1044 {
1045     if (blk->bs) {
1046         bdrv_eject(blk->bs, eject_flag);
1047     }
1048 }
1049 
1050 int blk_get_flags(BlockBackend *blk)
1051 {
1052     if (blk->bs) {
1053         return bdrv_get_flags(blk->bs);
1054     } else {
1055         return blk->root_state.open_flags;
1056     }
1057 }
1058 
1059 int blk_get_max_transfer_length(BlockBackend *blk)
1060 {
1061     if (blk->bs) {
1062         return blk->bs->bl.max_transfer_length;
1063     } else {
1064         return 0;
1065     }
1066 }
1067 
1068 int blk_get_max_iov(BlockBackend *blk)
1069 {
1070     return blk->bs->bl.max_iov;
1071 }
1072 
1073 void blk_set_guest_block_size(BlockBackend *blk, int align)
1074 {
1075     blk->guest_block_size = align;
1076 }
1077 
1078 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1079 {
1080     return qemu_try_blockalign(blk ? blk->bs : NULL, size);
1081 }
1082 
1083 void *blk_blockalign(BlockBackend *blk, size_t size)
1084 {
1085     return qemu_blockalign(blk ? blk->bs : NULL, size);
1086 }
1087 
1088 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1089 {
1090     if (!blk->bs) {
1091         return false;
1092     }
1093 
1094     return bdrv_op_is_blocked(blk->bs, op, errp);
1095 }
1096 
1097 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1098 {
1099     if (blk->bs) {
1100         bdrv_op_unblock(blk->bs, op, reason);
1101     }
1102 }
1103 
1104 void blk_op_block_all(BlockBackend *blk, Error *reason)
1105 {
1106     if (blk->bs) {
1107         bdrv_op_block_all(blk->bs, reason);
1108     }
1109 }
1110 
1111 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1112 {
1113     if (blk->bs) {
1114         bdrv_op_unblock_all(blk->bs, reason);
1115     }
1116 }
1117 
1118 AioContext *blk_get_aio_context(BlockBackend *blk)
1119 {
1120     if (blk->bs) {
1121         return bdrv_get_aio_context(blk->bs);
1122     } else {
1123         return qemu_get_aio_context();
1124     }
1125 }
1126 
1127 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1128 {
1129     BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1130     return blk_get_aio_context(blk_acb->blk);
1131 }
1132 
1133 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1134 {
1135     if (blk->bs) {
1136         bdrv_set_aio_context(blk->bs, new_context);
1137     }
1138 }
1139 
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)
1143 {
1144     if (blk->bs) {
1145         bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
1146                                       detach_aio_context, opaque);
1147     }
1148 }
1149 
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)
1155 {
1156     if (blk->bs) {
1157         bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
1158                                          detach_aio_context, opaque);
1159     }
1160 }
1161 
1162 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1163 {
1164     notifier_list_add(&blk->remove_bs_notifiers, notify);
1165 }
1166 
1167 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1168 {
1169     notifier_list_add(&blk->insert_bs_notifiers, notify);
1170 }
1171 
1172 void blk_io_plug(BlockBackend *blk)
1173 {
1174     if (blk->bs) {
1175         bdrv_io_plug(blk->bs);
1176     }
1177 }
1178 
1179 void blk_io_unplug(BlockBackend *blk)
1180 {
1181     if (blk->bs) {
1182         bdrv_io_unplug(blk->bs);
1183     }
1184 }
1185 
1186 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1187 {
1188     return &blk->stats;
1189 }
1190 
1191 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1192                   BlockCompletionFunc *cb, void *opaque)
1193 {
1194     return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1195 }
1196 
1197 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
1198                                      int nb_sectors, BdrvRequestFlags flags)
1199 {
1200     int ret = blk_check_request(blk, sector_num, nb_sectors);
1201     if (ret < 0) {
1202         return ret;
1203     }
1204 
1205     return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
1206 }
1207 
1208 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1209                          const uint8_t *buf, int nb_sectors)
1210 {
1211     int ret = blk_check_request(blk, sector_num, nb_sectors);
1212     if (ret < 0) {
1213         return ret;
1214     }
1215 
1216     return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
1217 }
1218 
1219 int blk_truncate(BlockBackend *blk, int64_t offset)
1220 {
1221     if (!blk_is_available(blk)) {
1222         return -ENOMEDIUM;
1223     }
1224 
1225     return bdrv_truncate(blk->bs, offset);
1226 }
1227 
1228 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1229 {
1230     int ret = blk_check_request(blk, sector_num, nb_sectors);
1231     if (ret < 0) {
1232         return ret;
1233     }
1234 
1235     return bdrv_discard(blk->bs, sector_num, nb_sectors);
1236 }
1237 
1238 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1239                      int64_t pos, int size)
1240 {
1241     if (!blk_is_available(blk)) {
1242         return -ENOMEDIUM;
1243     }
1244 
1245     return bdrv_save_vmstate(blk->bs, buf, pos, size);
1246 }
1247 
1248 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1249 {
1250     if (!blk_is_available(blk)) {
1251         return -ENOMEDIUM;
1252     }
1253 
1254     return bdrv_load_vmstate(blk->bs, buf, pos, size);
1255 }
1256 
1257 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1258 {
1259     if (!blk_is_available(blk)) {
1260         return -ENOMEDIUM;
1261     }
1262 
1263     return bdrv_probe_blocksizes(blk->bs, bsz);
1264 }
1265 
1266 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1267 {
1268     if (!blk_is_available(blk)) {
1269         return -ENOMEDIUM;
1270     }
1271 
1272     return bdrv_probe_geometry(blk->bs, geo);
1273 }
1274 
1275 /*
1276  * Updates the BlockBackendRootState object with data from the currently
1277  * attached BlockDriverState.
1278  */
1279 void blk_update_root_state(BlockBackend *blk)
1280 {
1281     assert(blk->bs);
1282 
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;
1286 
1287     if (blk->root_state.throttle_group) {
1288         g_free(blk->root_state.throttle_group);
1289         throttle_group_unref(blk->root_state.throttle_state);
1290     }
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;
1298     }
1299 }
1300 
1301 /*
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.
1305  */
1306 void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1307 {
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);
1311     }
1312 }
1313 
1314 /*
1315  * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1316  * supposed to inherit the root state.
1317  */
1318 int blk_get_open_flags_from_root_state(BlockBackend *blk)
1319 {
1320     int bs_flags;
1321 
1322     bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1323     bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1324 
1325     return bs_flags;
1326 }
1327 
1328 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1329 {
1330     return &blk->root_state;
1331 }
1332