xref: /openbmc/qemu/block/block-backend.c (revision 60915dc4691768c4dc62458bb3e16c843fab091d)
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 "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"
20 
21 /* Number of coroutines to reserve per attached device model */
22 #define COROUTINE_POOL_RESERVATION 64
23 
24 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
25 
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 */
32 
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;
37 
38     /* the block size for which the guest device expects atomicity */
39     int guest_block_size;
40 
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;
44 
45     /* I/O stats (display with "info blockstats"). */
46     BlockAcctStats stats;
47 
48     BlockdevOnError on_read_error, on_write_error;
49     bool iostatus_enabled;
50     BlockDeviceIoStatus iostatus;
51 };
52 
53 typedef struct BlockBackendAIOCB {
54     BlockAIOCB common;
55     QEMUBH *bh;
56     BlockBackend *blk;
57     int ret;
58 } BlockBackendAIOCB;
59 
60 static const AIOCBInfo block_backend_aiocb_info = {
61     .get_aio_context = blk_aiocb_get_aio_context,
62     .aiocb_size = sizeof(BlockBackendAIOCB),
63 };
64 
65 static void drive_info_del(DriveInfo *dinfo);
66 
67 /* All the BlockBackends (except for hidden ones) */
68 static QTAILQ_HEAD(, BlockBackend) blk_backends =
69     QTAILQ_HEAD_INITIALIZER(blk_backends);
70 
71 /*
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.
77  */
78 BlockBackend *blk_new(const char *name, Error **errp)
79 {
80     BlockBackend *blk;
81 
82     assert(name && name[0]);
83     if (!id_wellformed(name)) {
84         error_setg(errp, "Invalid device name");
85         return NULL;
86     }
87     if (blk_by_name(name)) {
88         error_setg(errp, "Device with id '%s' already exists", name);
89         return NULL;
90     }
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;
96     }
97 
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;
103 }
104 
105 /*
106  * Create a new BlockBackend with a new BlockDriverState attached.
107  * Otherwise just like blk_new(), which see.
108  */
109 BlockBackend *blk_new_with_bs(const char *name, Error **errp)
110 {
111     BlockBackend *blk;
112     BlockDriverState *bs;
113 
114     blk = blk_new(name, errp);
115     if (!blk) {
116         return NULL;
117     }
118 
119     bs = bdrv_new_root();
120     blk->bs = bs;
121     bs->blk = blk;
122     return blk;
123 }
124 
125 /*
126  * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
127  *
128  * Just as with bdrv_open(), after having called this function the reference to
129  * @options belongs to the block layer (even on failure).
130  *
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.
136  */
137 BlockBackend *blk_new_open(const char *name, const char *filename,
138                            const char *reference, QDict *options, int flags,
139                            Error **errp)
140 {
141     BlockBackend *blk;
142     int ret;
143 
144     blk = blk_new_with_bs(name, errp);
145     if (!blk) {
146         QDECREF(options);
147         return NULL;
148     }
149 
150     ret = bdrv_open(&blk->bs, filename, reference, options, flags, errp);
151     if (ret < 0) {
152         blk_unref(blk);
153         return NULL;
154     }
155 
156     return blk;
157 }
158 
159 static void blk_delete(BlockBackend *blk)
160 {
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;
168     }
169     if (blk->root_state.throttle_state) {
170         g_free(blk->root_state.throttle_group);
171         throttle_group_unref(blk->root_state.throttle_state);
172     }
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);
176     }
177     g_free(blk->name);
178     drive_info_del(blk->legacy_dinfo);
179     g_free(blk);
180 }
181 
182 static void drive_info_del(DriveInfo *dinfo)
183 {
184     if (!dinfo) {
185         return;
186     }
187     qemu_opts_del(dinfo->opts);
188     g_free(dinfo->serial);
189     g_free(dinfo);
190 }
191 
192 int blk_get_refcnt(BlockBackend *blk)
193 {
194     return blk ? blk->refcnt : 0;
195 }
196 
197 /*
198  * Increment @blk's reference count.
199  * @blk must not be null.
200  */
201 void blk_ref(BlockBackend *blk)
202 {
203     blk->refcnt++;
204 }
205 
206 /*
207  * Decrement @blk's reference count.
208  * If this drops it to zero, destroy @blk.
209  * For convenience, do nothing if @blk is null.
210  */
211 void blk_unref(BlockBackend *blk)
212 {
213     if (blk) {
214         assert(blk->refcnt > 0);
215         if (!--blk->refcnt) {
216             blk_delete(blk);
217         }
218     }
219 }
220 
221 /*
222  * Return the BlockBackend after @blk.
223  * If @blk is null, return the first one.
224  * Else, return @blk's next sibling, which may be null.
225  *
226  * To iterate over all BlockBackends, do
227  * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
228  *     ...
229  * }
230  */
231 BlockBackend *blk_next(BlockBackend *blk)
232 {
233     return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
234 }
235 
236 /*
237  * Return @blk's name, a non-null string.
238  * Wart: the name is empty iff @blk has been hidden with
239  * blk_hide_on_behalf_of_hmp_drive_del().
240  */
241 const char *blk_name(BlockBackend *blk)
242 {
243     return blk->name;
244 }
245 
246 /*
247  * Return the BlockBackend with name @name if it exists, else null.
248  * @name must not be null.
249  */
250 BlockBackend *blk_by_name(const char *name)
251 {
252     BlockBackend *blk;
253 
254     assert(name);
255     QTAILQ_FOREACH(blk, &blk_backends, link) {
256         if (!strcmp(name, blk->name)) {
257             return blk;
258         }
259     }
260     return NULL;
261 }
262 
263 /*
264  * Return the BlockDriverState attached to @blk if any, else null.
265  */
266 BlockDriverState *blk_bs(BlockBackend *blk)
267 {
268     return blk->bs;
269 }
270 
271 /*
272  * Changes the BlockDriverState attached to @blk
273  */
274 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs)
275 {
276     bdrv_ref(bs);
277 
278     if (blk->bs) {
279         blk->bs->blk = NULL;
280         bdrv_unref(blk->bs);
281     }
282     assert(bs->blk == NULL);
283 
284     blk->bs = bs;
285     bs->blk = blk;
286 }
287 
288 /*
289  * Return @blk's DriveInfo if any, else null.
290  */
291 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
292 {
293     return blk->legacy_dinfo;
294 }
295 
296 /*
297  * Set @blk's DriveInfo to @dinfo, and return it.
298  * @blk must not have a DriveInfo set already.
299  * No other BlockBackend may have the same DriveInfo set.
300  */
301 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
302 {
303     assert(!blk->legacy_dinfo);
304     return blk->legacy_dinfo = dinfo;
305 }
306 
307 /*
308  * Return the BlockBackend with DriveInfo @dinfo.
309  * It must exist.
310  */
311 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
312 {
313     BlockBackend *blk;
314 
315     QTAILQ_FOREACH(blk, &blk_backends, link) {
316         if (blk->legacy_dinfo == dinfo) {
317             return blk;
318         }
319     }
320     abort();
321 }
322 
323 /*
324  * Hide @blk.
325  * @blk must not have been hidden already.
326  * Make attached BlockDriverState, if any, anonymous.
327  * Once hidden, @blk is invisible to all functions that don't receive
328  * it as argument.  For example, blk_by_name() won't return it.
329  * Strictly for use by do_drive_del().
330  * TODO get rid of it!
331  */
332 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
333 {
334     QTAILQ_REMOVE(&blk_backends, blk, link);
335     blk->name[0] = 0;
336     if (blk->bs) {
337         bdrv_make_anon(blk->bs);
338     }
339 }
340 
341 /*
342  * Disassociates the currently associated BlockDriverState from @blk.
343  */
344 void blk_remove_bs(BlockBackend *blk)
345 {
346     blk_update_root_state(blk);
347 
348     blk->bs->blk = NULL;
349     bdrv_unref(blk->bs);
350     blk->bs = NULL;
351 }
352 
353 /*
354  * Associates a new BlockDriverState with @blk.
355  */
356 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
357 {
358     assert(!blk->bs && !bs->blk);
359     bdrv_ref(bs);
360     blk->bs = bs;
361     bs->blk = blk;
362 }
363 
364 /*
365  * Attach device model @dev to @blk.
366  * Return 0 on success, -EBUSY when a device model is attached already.
367  */
368 int blk_attach_dev(BlockBackend *blk, void *dev)
369 /* TODO change to DeviceState *dev when all users are qdevified */
370 {
371     if (blk->dev) {
372         return -EBUSY;
373     }
374     blk_ref(blk);
375     blk->dev = dev;
376     blk_iostatus_reset(blk);
377     return 0;
378 }
379 
380 /*
381  * Attach device model @dev to @blk.
382  * @blk must not have a device model attached already.
383  * TODO qdevified devices don't use this, remove when devices are qdevified
384  */
385 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
386 {
387     if (blk_attach_dev(blk, dev) < 0) {
388         abort();
389     }
390 }
391 
392 /*
393  * Detach device model @dev from @blk.
394  * @dev must be currently attached to @blk.
395  */
396 void blk_detach_dev(BlockBackend *blk, void *dev)
397 /* TODO change to DeviceState *dev when all users are qdevified */
398 {
399     assert(blk->dev == dev);
400     blk->dev = NULL;
401     blk->dev_ops = NULL;
402     blk->dev_opaque = NULL;
403     blk->guest_block_size = 512;
404     blk_unref(blk);
405 }
406 
407 /*
408  * Return the device model attached to @blk if any, else null.
409  */
410 void *blk_get_attached_dev(BlockBackend *blk)
411 /* TODO change to return DeviceState * when all users are qdevified */
412 {
413     return blk->dev;
414 }
415 
416 /*
417  * Set @blk's device model callbacks to @ops.
418  * @opaque is the opaque argument to pass to the callbacks.
419  * This is for use by device models.
420  */
421 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
422                      void *opaque)
423 {
424     blk->dev_ops = ops;
425     blk->dev_opaque = opaque;
426 }
427 
428 /*
429  * Notify @blk's attached device model of media change.
430  * If @load is true, notify of media load.
431  * Else, notify of media eject.
432  * Also send DEVICE_TRAY_MOVED events as appropriate.
433  */
434 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
435 {
436     if (blk->dev_ops && blk->dev_ops->change_media_cb) {
437         bool tray_was_open, tray_is_open;
438 
439         tray_was_open = blk_dev_is_tray_open(blk);
440         blk->dev_ops->change_media_cb(blk->dev_opaque, load);
441         tray_is_open = blk_dev_is_tray_open(blk);
442 
443         if (tray_was_open != tray_is_open) {
444             qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
445                                               &error_abort);
446         }
447     }
448 }
449 
450 /*
451  * Does @blk's attached device model have removable media?
452  * %true if no device model is attached.
453  */
454 bool blk_dev_has_removable_media(BlockBackend *blk)
455 {
456     return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
457 }
458 
459 /*
460  * Notify @blk's attached device model of a media eject request.
461  * If @force is true, the medium is about to be yanked out forcefully.
462  */
463 void blk_dev_eject_request(BlockBackend *blk, bool force)
464 {
465     if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
466         blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
467     }
468 }
469 
470 /*
471  * Does @blk's attached device model have a tray, and is it open?
472  */
473 bool blk_dev_is_tray_open(BlockBackend *blk)
474 {
475     if (blk->dev_ops && blk->dev_ops->is_tray_open) {
476         return blk->dev_ops->is_tray_open(blk->dev_opaque);
477     }
478     return false;
479 }
480 
481 /*
482  * Does @blk's attached device model have the medium locked?
483  * %false if the device model has no such lock.
484  */
485 bool blk_dev_is_medium_locked(BlockBackend *blk)
486 {
487     if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
488         return blk->dev_ops->is_medium_locked(blk->dev_opaque);
489     }
490     return false;
491 }
492 
493 /*
494  * Notify @blk's attached device model of a backend size change.
495  */
496 void blk_dev_resize_cb(BlockBackend *blk)
497 {
498     if (blk->dev_ops && blk->dev_ops->resize_cb) {
499         blk->dev_ops->resize_cb(blk->dev_opaque);
500     }
501 }
502 
503 void blk_iostatus_enable(BlockBackend *blk)
504 {
505     blk->iostatus_enabled = true;
506     blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
507 }
508 
509 /* The I/O status is only enabled if the drive explicitly
510  * enables it _and_ the VM is configured to stop on errors */
511 bool blk_iostatus_is_enabled(const BlockBackend *blk)
512 {
513     return (blk->iostatus_enabled &&
514            (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
515             blk->on_write_error == BLOCKDEV_ON_ERROR_STOP   ||
516             blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
517 }
518 
519 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
520 {
521     return blk->iostatus;
522 }
523 
524 void blk_iostatus_disable(BlockBackend *blk)
525 {
526     blk->iostatus_enabled = false;
527 }
528 
529 void blk_iostatus_reset(BlockBackend *blk)
530 {
531     if (blk_iostatus_is_enabled(blk)) {
532         blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
533         if (blk->bs && blk->bs->job) {
534             block_job_iostatus_reset(blk->bs->job);
535         }
536     }
537 }
538 
539 void blk_iostatus_set_err(BlockBackend *blk, int error)
540 {
541     assert(blk_iostatus_is_enabled(blk));
542     if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
543         blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
544                                           BLOCK_DEVICE_IO_STATUS_FAILED;
545     }
546 }
547 
548 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
549                                   size_t size)
550 {
551     int64_t len;
552 
553     if (size > INT_MAX) {
554         return -EIO;
555     }
556 
557     if (!blk_is_available(blk)) {
558         return -ENOMEDIUM;
559     }
560 
561     len = blk_getlength(blk);
562     if (len < 0) {
563         return len;
564     }
565 
566     if (offset < 0) {
567         return -EIO;
568     }
569 
570     if (offset > len || len - offset < size) {
571         return -EIO;
572     }
573 
574     return 0;
575 }
576 
577 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
578                              int nb_sectors)
579 {
580     if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
581         return -EIO;
582     }
583 
584     if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
585         return -EIO;
586     }
587 
588     return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
589                                   nb_sectors * BDRV_SECTOR_SIZE);
590 }
591 
592 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
593              int nb_sectors)
594 {
595     int ret = blk_check_request(blk, sector_num, nb_sectors);
596     if (ret < 0) {
597         return ret;
598     }
599 
600     return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
601 }
602 
603 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
604                          int nb_sectors)
605 {
606     int ret = blk_check_request(blk, sector_num, nb_sectors);
607     if (ret < 0) {
608         return ret;
609     }
610 
611     return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
612 }
613 
614 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
615               int nb_sectors)
616 {
617     int ret = blk_check_request(blk, sector_num, nb_sectors);
618     if (ret < 0) {
619         return ret;
620     }
621 
622     return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
623 }
624 
625 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
626                      int nb_sectors, BdrvRequestFlags flags)
627 {
628     int ret = blk_check_request(blk, sector_num, nb_sectors);
629     if (ret < 0) {
630         return ret;
631     }
632 
633     return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
634 }
635 
636 static void error_callback_bh(void *opaque)
637 {
638     struct BlockBackendAIOCB *acb = opaque;
639     qemu_bh_delete(acb->bh);
640     acb->common.cb(acb->common.opaque, acb->ret);
641     qemu_aio_unref(acb);
642 }
643 
644 static BlockAIOCB *abort_aio_request(BlockBackend *blk, BlockCompletionFunc *cb,
645                                      void *opaque, int ret)
646 {
647     struct BlockBackendAIOCB *acb;
648     QEMUBH *bh;
649 
650     acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
651     acb->blk = blk;
652     acb->ret = ret;
653 
654     bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
655     acb->bh = bh;
656     qemu_bh_schedule(bh);
657 
658     return &acb->common;
659 }
660 
661 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
662                                  int nb_sectors, BdrvRequestFlags flags,
663                                  BlockCompletionFunc *cb, void *opaque)
664 {
665     int ret = blk_check_request(blk, sector_num, nb_sectors);
666     if (ret < 0) {
667         return abort_aio_request(blk, cb, opaque, ret);
668     }
669 
670     return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
671                                  cb, opaque);
672 }
673 
674 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
675 {
676     int ret = blk_check_byte_request(blk, offset, count);
677     if (ret < 0) {
678         return ret;
679     }
680 
681     return bdrv_pread(blk->bs, offset, buf, count);
682 }
683 
684 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
685 {
686     int ret = blk_check_byte_request(blk, offset, count);
687     if (ret < 0) {
688         return ret;
689     }
690 
691     return bdrv_pwrite(blk->bs, offset, buf, count);
692 }
693 
694 int64_t blk_getlength(BlockBackend *blk)
695 {
696     if (!blk_is_available(blk)) {
697         return -ENOMEDIUM;
698     }
699 
700     return bdrv_getlength(blk->bs);
701 }
702 
703 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
704 {
705     if (!blk->bs) {
706         *nb_sectors_ptr = 0;
707     } else {
708         bdrv_get_geometry(blk->bs, nb_sectors_ptr);
709     }
710 }
711 
712 int64_t blk_nb_sectors(BlockBackend *blk)
713 {
714     if (!blk_is_available(blk)) {
715         return -ENOMEDIUM;
716     }
717 
718     return bdrv_nb_sectors(blk->bs);
719 }
720 
721 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
722                           QEMUIOVector *iov, int nb_sectors,
723                           BlockCompletionFunc *cb, void *opaque)
724 {
725     int ret = blk_check_request(blk, sector_num, nb_sectors);
726     if (ret < 0) {
727         return abort_aio_request(blk, cb, opaque, ret);
728     }
729 
730     return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
731 }
732 
733 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
734                            QEMUIOVector *iov, int nb_sectors,
735                            BlockCompletionFunc *cb, void *opaque)
736 {
737     int ret = blk_check_request(blk, sector_num, nb_sectors);
738     if (ret < 0) {
739         return abort_aio_request(blk, cb, opaque, ret);
740     }
741 
742     return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
743 }
744 
745 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
746                           BlockCompletionFunc *cb, void *opaque)
747 {
748     if (!blk_is_available(blk)) {
749         return abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
750     }
751 
752     return bdrv_aio_flush(blk->bs, cb, opaque);
753 }
754 
755 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
756                             int64_t sector_num, int nb_sectors,
757                             BlockCompletionFunc *cb, void *opaque)
758 {
759     int ret = blk_check_request(blk, sector_num, nb_sectors);
760     if (ret < 0) {
761         return abort_aio_request(blk, cb, opaque, ret);
762     }
763 
764     return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
765 }
766 
767 void blk_aio_cancel(BlockAIOCB *acb)
768 {
769     bdrv_aio_cancel(acb);
770 }
771 
772 void blk_aio_cancel_async(BlockAIOCB *acb)
773 {
774     bdrv_aio_cancel_async(acb);
775 }
776 
777 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
778 {
779     int i, ret;
780 
781     for (i = 0; i < num_reqs; i++) {
782         ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
783         if (ret < 0) {
784             return ret;
785         }
786     }
787 
788     return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
789 }
790 
791 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
792 {
793     if (!blk_is_available(blk)) {
794         return -ENOMEDIUM;
795     }
796 
797     return bdrv_ioctl(blk->bs, req, buf);
798 }
799 
800 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
801                           BlockCompletionFunc *cb, void *opaque)
802 {
803     if (!blk_is_available(blk)) {
804         return abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
805     }
806 
807     return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
808 }
809 
810 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
811 {
812     int ret = blk_check_request(blk, sector_num, nb_sectors);
813     if (ret < 0) {
814         return ret;
815     }
816 
817     return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
818 }
819 
820 int blk_co_flush(BlockBackend *blk)
821 {
822     if (!blk_is_available(blk)) {
823         return -ENOMEDIUM;
824     }
825 
826     return bdrv_co_flush(blk->bs);
827 }
828 
829 int blk_flush(BlockBackend *blk)
830 {
831     if (!blk_is_available(blk)) {
832         return -ENOMEDIUM;
833     }
834 
835     return bdrv_flush(blk->bs);
836 }
837 
838 int blk_flush_all(void)
839 {
840     return bdrv_flush_all();
841 }
842 
843 void blk_drain(BlockBackend *blk)
844 {
845     if (blk->bs) {
846         bdrv_drain(blk->bs);
847     }
848 }
849 
850 void blk_drain_all(void)
851 {
852     bdrv_drain_all();
853 }
854 
855 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
856                       BlockdevOnError on_write_error)
857 {
858     blk->on_read_error = on_read_error;
859     blk->on_write_error = on_write_error;
860 }
861 
862 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
863 {
864     return is_read ? blk->on_read_error : blk->on_write_error;
865 }
866 
867 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
868                                       int error)
869 {
870     BlockdevOnError on_err = blk_get_on_error(blk, is_read);
871 
872     switch (on_err) {
873     case BLOCKDEV_ON_ERROR_ENOSPC:
874         return (error == ENOSPC) ?
875                BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
876     case BLOCKDEV_ON_ERROR_STOP:
877         return BLOCK_ERROR_ACTION_STOP;
878     case BLOCKDEV_ON_ERROR_REPORT:
879         return BLOCK_ERROR_ACTION_REPORT;
880     case BLOCKDEV_ON_ERROR_IGNORE:
881         return BLOCK_ERROR_ACTION_IGNORE;
882     default:
883         abort();
884     }
885 }
886 
887 static void send_qmp_error_event(BlockBackend *blk,
888                                  BlockErrorAction action,
889                                  bool is_read, int error)
890 {
891     IoOperationType optype;
892 
893     optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
894     qapi_event_send_block_io_error(blk_name(blk), optype, action,
895                                    blk_iostatus_is_enabled(blk),
896                                    error == ENOSPC, strerror(error),
897                                    &error_abort);
898 }
899 
900 /* This is done by device models because, while the block layer knows
901  * about the error, it does not know whether an operation comes from
902  * the device or the block layer (from a job, for example).
903  */
904 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
905                       bool is_read, int error)
906 {
907     assert(error >= 0);
908 
909     if (action == BLOCK_ERROR_ACTION_STOP) {
910         /* First set the iostatus, so that "info block" returns an iostatus
911          * that matches the events raised so far (an additional error iostatus
912          * is fine, but not a lost one).
913          */
914         blk_iostatus_set_err(blk, error);
915 
916         /* Then raise the request to stop the VM and the event.
917          * qemu_system_vmstop_request_prepare has two effects.  First,
918          * it ensures that the STOP event always comes after the
919          * BLOCK_IO_ERROR event.  Second, it ensures that even if management
920          * can observe the STOP event and do a "cont" before the STOP
921          * event is issued, the VM will not stop.  In this case, vm_start()
922          * also ensures that the STOP/RESUME pair of events is emitted.
923          */
924         qemu_system_vmstop_request_prepare();
925         send_qmp_error_event(blk, action, is_read, error);
926         qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
927     } else {
928         send_qmp_error_event(blk, action, is_read, error);
929     }
930 }
931 
932 int blk_is_read_only(BlockBackend *blk)
933 {
934     if (blk->bs) {
935         return bdrv_is_read_only(blk->bs);
936     } else {
937         return blk->root_state.read_only;
938     }
939 }
940 
941 int blk_is_sg(BlockBackend *blk)
942 {
943     if (!blk->bs) {
944         return 0;
945     }
946 
947     return bdrv_is_sg(blk->bs);
948 }
949 
950 int blk_enable_write_cache(BlockBackend *blk)
951 {
952     if (blk->bs) {
953         return bdrv_enable_write_cache(blk->bs);
954     } else {
955         return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB);
956     }
957 }
958 
959 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
960 {
961     if (blk->bs) {
962         bdrv_set_enable_write_cache(blk->bs, wce);
963     } else {
964         if (wce) {
965             blk->root_state.open_flags |= BDRV_O_CACHE_WB;
966         } else {
967             blk->root_state.open_flags &= ~BDRV_O_CACHE_WB;
968         }
969     }
970 }
971 
972 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
973 {
974     if (!blk->bs) {
975         error_setg(errp, "Device '%s' has no medium", blk->name);
976         return;
977     }
978 
979     bdrv_invalidate_cache(blk->bs, errp);
980 }
981 
982 bool blk_is_inserted(BlockBackend *blk)
983 {
984     return blk->bs && bdrv_is_inserted(blk->bs);
985 }
986 
987 bool blk_is_available(BlockBackend *blk)
988 {
989     return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
990 }
991 
992 void blk_lock_medium(BlockBackend *blk, bool locked)
993 {
994     if (blk->bs) {
995         bdrv_lock_medium(blk->bs, locked);
996     }
997 }
998 
999 void blk_eject(BlockBackend *blk, bool eject_flag)
1000 {
1001     if (blk->bs) {
1002         bdrv_eject(blk->bs, eject_flag);
1003     }
1004 }
1005 
1006 int blk_get_flags(BlockBackend *blk)
1007 {
1008     if (blk->bs) {
1009         return bdrv_get_flags(blk->bs);
1010     } else {
1011         return blk->root_state.open_flags;
1012     }
1013 }
1014 
1015 int blk_get_max_transfer_length(BlockBackend *blk)
1016 {
1017     if (blk->bs) {
1018         return blk->bs->bl.max_transfer_length;
1019     } else {
1020         return 0;
1021     }
1022 }
1023 
1024 void blk_set_guest_block_size(BlockBackend *blk, int align)
1025 {
1026     blk->guest_block_size = align;
1027 }
1028 
1029 void *blk_blockalign(BlockBackend *blk, size_t size)
1030 {
1031     return qemu_blockalign(blk ? blk->bs : NULL, size);
1032 }
1033 
1034 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1035 {
1036     if (!blk->bs) {
1037         return false;
1038     }
1039 
1040     return bdrv_op_is_blocked(blk->bs, op, errp);
1041 }
1042 
1043 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1044 {
1045     if (blk->bs) {
1046         bdrv_op_unblock(blk->bs, op, reason);
1047     }
1048 }
1049 
1050 void blk_op_block_all(BlockBackend *blk, Error *reason)
1051 {
1052     if (blk->bs) {
1053         bdrv_op_block_all(blk->bs, reason);
1054     }
1055 }
1056 
1057 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1058 {
1059     if (blk->bs) {
1060         bdrv_op_unblock_all(blk->bs, reason);
1061     }
1062 }
1063 
1064 AioContext *blk_get_aio_context(BlockBackend *blk)
1065 {
1066     if (blk->bs) {
1067         return bdrv_get_aio_context(blk->bs);
1068     } else {
1069         return qemu_get_aio_context();
1070     }
1071 }
1072 
1073 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1074 {
1075     BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1076     return blk_get_aio_context(blk_acb->blk);
1077 }
1078 
1079 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1080 {
1081     if (blk->bs) {
1082         bdrv_set_aio_context(blk->bs, new_context);
1083     }
1084 }
1085 
1086 void blk_add_aio_context_notifier(BlockBackend *blk,
1087         void (*attached_aio_context)(AioContext *new_context, void *opaque),
1088         void (*detach_aio_context)(void *opaque), void *opaque)
1089 {
1090     if (blk->bs) {
1091         bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
1092                                       detach_aio_context, opaque);
1093     }
1094 }
1095 
1096 void blk_remove_aio_context_notifier(BlockBackend *blk,
1097                                      void (*attached_aio_context)(AioContext *,
1098                                                                   void *),
1099                                      void (*detach_aio_context)(void *),
1100                                      void *opaque)
1101 {
1102     if (blk->bs) {
1103         bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
1104                                          detach_aio_context, opaque);
1105     }
1106 }
1107 
1108 void blk_add_close_notifier(BlockBackend *blk, Notifier *notify)
1109 {
1110     if (blk->bs) {
1111         bdrv_add_close_notifier(blk->bs, notify);
1112     }
1113 }
1114 
1115 void blk_io_plug(BlockBackend *blk)
1116 {
1117     if (blk->bs) {
1118         bdrv_io_plug(blk->bs);
1119     }
1120 }
1121 
1122 void blk_io_unplug(BlockBackend *blk)
1123 {
1124     if (blk->bs) {
1125         bdrv_io_unplug(blk->bs);
1126     }
1127 }
1128 
1129 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1130 {
1131     return &blk->stats;
1132 }
1133 
1134 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1135                   BlockCompletionFunc *cb, void *opaque)
1136 {
1137     return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1138 }
1139 
1140 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
1141                                      int nb_sectors, BdrvRequestFlags flags)
1142 {
1143     int ret = blk_check_request(blk, sector_num, nb_sectors);
1144     if (ret < 0) {
1145         return ret;
1146     }
1147 
1148     return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
1149 }
1150 
1151 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1152                          const uint8_t *buf, int nb_sectors)
1153 {
1154     int ret = blk_check_request(blk, sector_num, nb_sectors);
1155     if (ret < 0) {
1156         return ret;
1157     }
1158 
1159     return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
1160 }
1161 
1162 int blk_truncate(BlockBackend *blk, int64_t offset)
1163 {
1164     if (!blk_is_available(blk)) {
1165         return -ENOMEDIUM;
1166     }
1167 
1168     return bdrv_truncate(blk->bs, offset);
1169 }
1170 
1171 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1172 {
1173     int ret = blk_check_request(blk, sector_num, nb_sectors);
1174     if (ret < 0) {
1175         return ret;
1176     }
1177 
1178     return bdrv_discard(blk->bs, sector_num, nb_sectors);
1179 }
1180 
1181 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1182                      int64_t pos, int size)
1183 {
1184     if (!blk_is_available(blk)) {
1185         return -ENOMEDIUM;
1186     }
1187 
1188     return bdrv_save_vmstate(blk->bs, buf, pos, size);
1189 }
1190 
1191 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1192 {
1193     if (!blk_is_available(blk)) {
1194         return -ENOMEDIUM;
1195     }
1196 
1197     return bdrv_load_vmstate(blk->bs, buf, pos, size);
1198 }
1199 
1200 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1201 {
1202     if (!blk_is_available(blk)) {
1203         return -ENOMEDIUM;
1204     }
1205 
1206     return bdrv_probe_blocksizes(blk->bs, bsz);
1207 }
1208 
1209 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1210 {
1211     if (!blk_is_available(blk)) {
1212         return -ENOMEDIUM;
1213     }
1214 
1215     return bdrv_probe_geometry(blk->bs, geo);
1216 }
1217 
1218 /*
1219  * Updates the BlockBackendRootState object with data from the currently
1220  * attached BlockDriverState.
1221  */
1222 void blk_update_root_state(BlockBackend *blk)
1223 {
1224     assert(blk->bs);
1225 
1226     blk->root_state.open_flags    = blk->bs->open_flags;
1227     blk->root_state.read_only     = blk->bs->read_only;
1228     blk->root_state.detect_zeroes = blk->bs->detect_zeroes;
1229 
1230     if (blk->root_state.throttle_group) {
1231         g_free(blk->root_state.throttle_group);
1232         throttle_group_unref(blk->root_state.throttle_state);
1233     }
1234     if (blk->bs->throttle_state) {
1235         const char *name = throttle_group_get_name(blk->bs);
1236         blk->root_state.throttle_group = g_strdup(name);
1237         blk->root_state.throttle_state = throttle_group_incref(name);
1238     } else {
1239         blk->root_state.throttle_group = NULL;
1240         blk->root_state.throttle_state = NULL;
1241     }
1242 }
1243 
1244 /*
1245  * Applies the information in the root state to the given BlockDriverState. This
1246  * does not include the flags which have to be specified for bdrv_open(), use
1247  * blk_get_open_flags_from_root_state() to inquire them.
1248  */
1249 void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1250 {
1251     bs->detect_zeroes = blk->root_state.detect_zeroes;
1252     if (blk->root_state.throttle_group) {
1253         bdrv_io_limits_enable(bs, blk->root_state.throttle_group);
1254     }
1255 }
1256 
1257 /*
1258  * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1259  * supposed to inherit the root state.
1260  */
1261 int blk_get_open_flags_from_root_state(BlockBackend *blk)
1262 {
1263     int bs_flags;
1264 
1265     bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1266     bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1267 
1268     return bs_flags;
1269 }
1270 
1271 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1272 {
1273     return &blk->root_state;
1274 }
1275