]> Git Repo - qemu.git/blob - block/block-backend.c
block: Introduce BdrvChild.opaque
[qemu.git] / block / block-backend.c
1 /*
2  * QEMU Block backends
3  *
4  * Copyright (C) 2014-2016 Red Hat, Inc.
5  *
6  * Authors:
7  *  Markus Armbruster <[email protected]>,
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 #include "qemu/id.h"
22
23 /* Number of coroutines to reserve per attached device model */
24 #define COROUTINE_POOL_RESERVATION 64
25
26 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
27
28 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
29
30 struct BlockBackend {
31     char *name;
32     int refcnt;
33     BdrvChild *root;
34     DriveInfo *legacy_dinfo;    /* null unless created by drive_new() */
35     QTAILQ_ENTRY(BlockBackend) link;         /* for block_backends */
36     QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */
37     BlockBackendPublic public;
38
39     void *dev;                  /* attached device model, if any */
40     /* TODO change to DeviceState when all users are qdevified */
41     const BlockDevOps *dev_ops;
42     void *dev_opaque;
43
44     /* the block size for which the guest device expects atomicity */
45     int guest_block_size;
46
47     /* If the BDS tree is removed, some of its options are stored here (which
48      * can be used to restore those options in the new BDS on insert) */
49     BlockBackendRootState root_state;
50
51     bool enable_write_cache;
52
53     /* I/O stats (display with "info blockstats"). */
54     BlockAcctStats stats;
55
56     BlockdevOnError on_read_error, on_write_error;
57     bool iostatus_enabled;
58     BlockDeviceIoStatus iostatus;
59
60     bool allow_write_beyond_eof;
61
62     NotifierList remove_bs_notifiers, insert_bs_notifiers;
63 };
64
65 typedef struct BlockBackendAIOCB {
66     BlockAIOCB common;
67     QEMUBH *bh;
68     BlockBackend *blk;
69     int ret;
70 } BlockBackendAIOCB;
71
72 static const AIOCBInfo block_backend_aiocb_info = {
73     .get_aio_context = blk_aiocb_get_aio_context,
74     .aiocb_size = sizeof(BlockBackendAIOCB),
75 };
76
77 static void drive_info_del(DriveInfo *dinfo);
78
79 /* All BlockBackends */
80 static QTAILQ_HEAD(, BlockBackend) block_backends =
81     QTAILQ_HEAD_INITIALIZER(block_backends);
82
83 /* All BlockBackends referenced by the monitor and which are iterated through by
84  * blk_next() */
85 static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
86     QTAILQ_HEAD_INITIALIZER(monitor_block_backends);
87
88 static void blk_root_inherit_options(int *child_flags, QDict *child_options,
89                                      int parent_flags, QDict *parent_options)
90 {
91     /* We're not supposed to call this function for root nodes */
92     abort();
93 }
94
95 static const BdrvChildRole child_root = {
96     .inherit_options = blk_root_inherit_options,
97 };
98
99 /*
100  * Create a new BlockBackend with a reference count of one.
101  * Store an error through @errp on failure, unless it's null.
102  * Return the new BlockBackend on success, null on failure.
103  */
104 BlockBackend *blk_new(Error **errp)
105 {
106     BlockBackend *blk;
107
108     blk = g_new0(BlockBackend, 1);
109     blk->refcnt = 1;
110     qemu_co_queue_init(&blk->public.throttled_reqs[0]);
111     qemu_co_queue_init(&blk->public.throttled_reqs[1]);
112
113     notifier_list_init(&blk->remove_bs_notifiers);
114     notifier_list_init(&blk->insert_bs_notifiers);
115
116     QTAILQ_INSERT_TAIL(&block_backends, blk, link);
117     return blk;
118 }
119
120 /*
121  * Create a new BlockBackend with a new BlockDriverState attached.
122  * Otherwise just like blk_new(), which see.
123  */
124 BlockBackend *blk_new_with_bs(Error **errp)
125 {
126     BlockBackend *blk;
127     BlockDriverState *bs;
128
129     blk = blk_new(errp);
130     if (!blk) {
131         return NULL;
132     }
133
134     bs = bdrv_new_root();
135     blk->root = bdrv_root_attach_child(bs, "root", &child_root);
136     blk->root->opaque = blk;
137     bs->blk = blk;
138     return blk;
139 }
140
141 /*
142  * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
143  *
144  * Just as with bdrv_open(), after having called this function the reference to
145  * @options belongs to the block layer (even on failure).
146  *
147  * TODO: Remove @filename and @flags; it should be possible to specify a whole
148  * BDS tree just by specifying the @options QDict (or @reference,
149  * alternatively). At the time of adding this function, this is not possible,
150  * though, so callers of this function have to be able to specify @filename and
151  * @flags.
152  */
153 BlockBackend *blk_new_open(const char *filename, const char *reference,
154                            QDict *options, int flags, Error **errp)
155 {
156     BlockBackend *blk;
157     int ret;
158
159     blk = blk_new_with_bs(errp);
160     if (!blk) {
161         QDECREF(options);
162         return NULL;
163     }
164
165     ret = bdrv_open(&blk->root->bs, filename, reference, options, flags, errp);
166     if (ret < 0) {
167         blk_unref(blk);
168         return NULL;
169     }
170
171     blk_set_enable_write_cache(blk, true);
172
173     return blk;
174 }
175
176 static void blk_delete(BlockBackend *blk)
177 {
178     assert(!blk->refcnt);
179     assert(!blk->name);
180     assert(!blk->dev);
181     if (blk->root) {
182         blk_remove_bs(blk);
183     }
184     assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
185     assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
186     if (blk->root_state.throttle_state) {
187         g_free(blk->root_state.throttle_group);
188         throttle_group_unref(blk->root_state.throttle_state);
189     }
190     QTAILQ_REMOVE(&block_backends, blk, link);
191     drive_info_del(blk->legacy_dinfo);
192     block_acct_cleanup(&blk->stats);
193     g_free(blk);
194 }
195
196 static void drive_info_del(DriveInfo *dinfo)
197 {
198     if (!dinfo) {
199         return;
200     }
201     qemu_opts_del(dinfo->opts);
202     g_free(dinfo->serial);
203     g_free(dinfo);
204 }
205
206 int blk_get_refcnt(BlockBackend *blk)
207 {
208     return blk ? blk->refcnt : 0;
209 }
210
211 /*
212  * Increment @blk's reference count.
213  * @blk must not be null.
214  */
215 void blk_ref(BlockBackend *blk)
216 {
217     blk->refcnt++;
218 }
219
220 /*
221  * Decrement @blk's reference count.
222  * If this drops it to zero, destroy @blk.
223  * For convenience, do nothing if @blk is null.
224  */
225 void blk_unref(BlockBackend *blk)
226 {
227     if (blk) {
228         assert(blk->refcnt > 0);
229         if (!--blk->refcnt) {
230             blk_delete(blk);
231         }
232     }
233 }
234
235 /*
236  * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
237  * ones which are hidden (i.e. are not referenced by the monitor).
238  */
239 static BlockBackend *blk_all_next(BlockBackend *blk)
240 {
241     return blk ? QTAILQ_NEXT(blk, link)
242                : QTAILQ_FIRST(&block_backends);
243 }
244
245 void blk_remove_all_bs(void)
246 {
247     BlockBackend *blk = NULL;
248
249     while ((blk = blk_all_next(blk)) != NULL) {
250         AioContext *ctx = blk_get_aio_context(blk);
251
252         aio_context_acquire(ctx);
253         if (blk->root) {
254             blk_remove_bs(blk);
255         }
256         aio_context_release(ctx);
257     }
258 }
259
260 /*
261  * Return the monitor-owned BlockBackend after @blk.
262  * If @blk is null, return the first one.
263  * Else, return @blk's next sibling, which may be null.
264  *
265  * To iterate over all BlockBackends, do
266  * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
267  *     ...
268  * }
269  */
270 BlockBackend *blk_next(BlockBackend *blk)
271 {
272     return blk ? QTAILQ_NEXT(blk, monitor_link)
273                : QTAILQ_FIRST(&monitor_block_backends);
274 }
275
276 /*
277  * Iterates over all BlockDriverStates which are attached to a BlockBackend.
278  * This function is for use by bdrv_next().
279  *
280  * @bs must be NULL or a BDS that is attached to a BB.
281  */
282 BlockDriverState *blk_next_root_bs(BlockDriverState *bs)
283 {
284     BlockBackend *blk;
285
286     if (bs) {
287         assert(bs->blk);
288         blk = bs->blk;
289     } else {
290         blk = NULL;
291     }
292
293     do {
294         blk = blk_all_next(blk);
295     } while (blk && !blk->root);
296
297     return blk ? blk->root->bs : NULL;
298 }
299
300 /*
301  * Add a BlockBackend into the list of backends referenced by the monitor, with
302  * the given @name acting as the handle for the monitor.
303  * Strictly for use by blockdev.c.
304  *
305  * @name must not be null or empty.
306  *
307  * Returns true on success and false on failure. In the latter case, an Error
308  * object is returned through @errp.
309  */
310 bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp)
311 {
312     assert(!blk->name);
313     assert(name && name[0]);
314
315     if (!id_wellformed(name)) {
316         error_setg(errp, "Invalid device name");
317         return false;
318     }
319     if (blk_by_name(name)) {
320         error_setg(errp, "Device with id '%s' already exists", name);
321         return false;
322     }
323     if (bdrv_find_node(name)) {
324         error_setg(errp,
325                    "Device name '%s' conflicts with an existing node name",
326                    name);
327         return false;
328     }
329
330     blk->name = g_strdup(name);
331     QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link);
332     return true;
333 }
334
335 /*
336  * Remove a BlockBackend from the list of backends referenced by the monitor.
337  * Strictly for use by blockdev.c.
338  */
339 void monitor_remove_blk(BlockBackend *blk)
340 {
341     if (!blk->name) {
342         return;
343     }
344
345     QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link);
346     g_free(blk->name);
347     blk->name = NULL;
348 }
349
350 /*
351  * Return @blk's name, a non-null string.
352  * Returns an empty string iff @blk is not referenced by the monitor.
353  */
354 const char *blk_name(BlockBackend *blk)
355 {
356     return blk->name ?: "";
357 }
358
359 /*
360  * Return the BlockBackend with name @name if it exists, else null.
361  * @name must not be null.
362  */
363 BlockBackend *blk_by_name(const char *name)
364 {
365     BlockBackend *blk = NULL;
366
367     assert(name);
368     while ((blk = blk_next(blk)) != NULL) {
369         if (!strcmp(name, blk->name)) {
370             return blk;
371         }
372     }
373     return NULL;
374 }
375
376 /*
377  * Return the BlockDriverState attached to @blk if any, else null.
378  */
379 BlockDriverState *blk_bs(BlockBackend *blk)
380 {
381     return blk->root ? blk->root->bs : NULL;
382 }
383
384 /*
385  * Return @blk's DriveInfo if any, else null.
386  */
387 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
388 {
389     return blk->legacy_dinfo;
390 }
391
392 /*
393  * Set @blk's DriveInfo to @dinfo, and return it.
394  * @blk must not have a DriveInfo set already.
395  * No other BlockBackend may have the same DriveInfo set.
396  */
397 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
398 {
399     assert(!blk->legacy_dinfo);
400     return blk->legacy_dinfo = dinfo;
401 }
402
403 /*
404  * Return the BlockBackend with DriveInfo @dinfo.
405  * It must exist.
406  */
407 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
408 {
409     BlockBackend *blk = NULL;
410
411     while ((blk = blk_next(blk)) != NULL) {
412         if (blk->legacy_dinfo == dinfo) {
413             return blk;
414         }
415     }
416     abort();
417 }
418
419 /*
420  * Returns a pointer to the publicly accessible fields of @blk.
421  */
422 BlockBackendPublic *blk_get_public(BlockBackend *blk)
423 {
424     return &blk->public;
425 }
426
427 /*
428  * Returns a BlockBackend given the associated @public fields.
429  */
430 BlockBackend *blk_by_public(BlockBackendPublic *public)
431 {
432     return container_of(public, BlockBackend, public);
433 }
434
435 /*
436  * Disassociates the currently associated BlockDriverState from @blk.
437  */
438 void blk_remove_bs(BlockBackend *blk)
439 {
440     assert(blk->root->bs->blk == blk);
441
442     notifier_list_notify(&blk->remove_bs_notifiers, blk);
443
444     blk_update_root_state(blk);
445     if (blk->public.throttle_state) {
446         blk_io_limits_disable(blk);
447     }
448
449     blk->root->bs->blk = NULL;
450     bdrv_root_unref_child(blk->root);
451     blk->root = NULL;
452 }
453
454 /*
455  * Associates a new BlockDriverState with @blk.
456  */
457 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
458 {
459     assert(!blk->root && !bs->blk);
460     bdrv_ref(bs);
461     blk->root = bdrv_root_attach_child(bs, "root", &child_root);
462     blk->root->opaque = blk;
463     bs->blk = blk;
464
465     notifier_list_notify(&blk->insert_bs_notifiers, blk);
466 }
467
468 /*
469  * Attach device model @dev to @blk.
470  * Return 0 on success, -EBUSY when a device model is attached already.
471  */
472 int blk_attach_dev(BlockBackend *blk, void *dev)
473 /* TODO change to DeviceState *dev when all users are qdevified */
474 {
475     if (blk->dev) {
476         return -EBUSY;
477     }
478     blk_ref(blk);
479     blk->dev = dev;
480     blk_iostatus_reset(blk);
481     return 0;
482 }
483
484 /*
485  * Attach device model @dev to @blk.
486  * @blk must not have a device model attached already.
487  * TODO qdevified devices don't use this, remove when devices are qdevified
488  */
489 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
490 {
491     if (blk_attach_dev(blk, dev) < 0) {
492         abort();
493     }
494 }
495
496 /*
497  * Detach device model @dev from @blk.
498  * @dev must be currently attached to @blk.
499  */
500 void blk_detach_dev(BlockBackend *blk, void *dev)
501 /* TODO change to DeviceState *dev when all users are qdevified */
502 {
503     assert(blk->dev == dev);
504     blk->dev = NULL;
505     blk->dev_ops = NULL;
506     blk->dev_opaque = NULL;
507     blk->guest_block_size = 512;
508     blk_unref(blk);
509 }
510
511 /*
512  * Return the device model attached to @blk if any, else null.
513  */
514 void *blk_get_attached_dev(BlockBackend *blk)
515 /* TODO change to return DeviceState * when all users are qdevified */
516 {
517     return blk->dev;
518 }
519
520 /*
521  * Set @blk's device model callbacks to @ops.
522  * @opaque is the opaque argument to pass to the callbacks.
523  * This is for use by device models.
524  */
525 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
526                      void *opaque)
527 {
528     blk->dev_ops = ops;
529     blk->dev_opaque = opaque;
530 }
531
532 /*
533  * Notify @blk's attached device model of media change.
534  * If @load is true, notify of media load.
535  * Else, notify of media eject.
536  * Also send DEVICE_TRAY_MOVED events as appropriate.
537  */
538 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
539 {
540     if (blk->dev_ops && blk->dev_ops->change_media_cb) {
541         bool tray_was_open, tray_is_open;
542
543         tray_was_open = blk_dev_is_tray_open(blk);
544         blk->dev_ops->change_media_cb(blk->dev_opaque, load);
545         tray_is_open = blk_dev_is_tray_open(blk);
546
547         if (tray_was_open != tray_is_open) {
548             qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
549                                               &error_abort);
550         }
551     }
552 }
553
554 /*
555  * Does @blk's attached device model have removable media?
556  * %true if no device model is attached.
557  */
558 bool blk_dev_has_removable_media(BlockBackend *blk)
559 {
560     return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
561 }
562
563 /*
564  * Does @blk's attached device model have a tray?
565  */
566 bool blk_dev_has_tray(BlockBackend *blk)
567 {
568     return blk->dev_ops && blk->dev_ops->is_tray_open;
569 }
570
571 /*
572  * Notify @blk's attached device model of a media eject request.
573  * If @force is true, the medium is about to be yanked out forcefully.
574  */
575 void blk_dev_eject_request(BlockBackend *blk, bool force)
576 {
577     if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
578         blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
579     }
580 }
581
582 /*
583  * Does @blk's attached device model have a tray, and is it open?
584  */
585 bool blk_dev_is_tray_open(BlockBackend *blk)
586 {
587     if (blk_dev_has_tray(blk)) {
588         return blk->dev_ops->is_tray_open(blk->dev_opaque);
589     }
590     return false;
591 }
592
593 /*
594  * Does @blk's attached device model have the medium locked?
595  * %false if the device model has no such lock.
596  */
597 bool blk_dev_is_medium_locked(BlockBackend *blk)
598 {
599     if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
600         return blk->dev_ops->is_medium_locked(blk->dev_opaque);
601     }
602     return false;
603 }
604
605 /*
606  * Notify @blk's attached device model of a backend size change.
607  */
608 void blk_dev_resize_cb(BlockBackend *blk)
609 {
610     if (blk->dev_ops && blk->dev_ops->resize_cb) {
611         blk->dev_ops->resize_cb(blk->dev_opaque);
612     }
613 }
614
615 void blk_iostatus_enable(BlockBackend *blk)
616 {
617     blk->iostatus_enabled = true;
618     blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
619 }
620
621 /* The I/O status is only enabled if the drive explicitly
622  * enables it _and_ the VM is configured to stop on errors */
623 bool blk_iostatus_is_enabled(const BlockBackend *blk)
624 {
625     return (blk->iostatus_enabled &&
626            (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
627             blk->on_write_error == BLOCKDEV_ON_ERROR_STOP   ||
628             blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
629 }
630
631 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
632 {
633     return blk->iostatus;
634 }
635
636 void blk_iostatus_disable(BlockBackend *blk)
637 {
638     blk->iostatus_enabled = false;
639 }
640
641 void blk_iostatus_reset(BlockBackend *blk)
642 {
643     if (blk_iostatus_is_enabled(blk)) {
644         BlockDriverState *bs = blk_bs(blk);
645         blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
646         if (bs && bs->job) {
647             block_job_iostatus_reset(bs->job);
648         }
649     }
650 }
651
652 void blk_iostatus_set_err(BlockBackend *blk, int error)
653 {
654     assert(blk_iostatus_is_enabled(blk));
655     if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
656         blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
657                                           BLOCK_DEVICE_IO_STATUS_FAILED;
658     }
659 }
660
661 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
662 {
663     blk->allow_write_beyond_eof = allow;
664 }
665
666 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
667                                   size_t size)
668 {
669     int64_t len;
670
671     if (size > INT_MAX) {
672         return -EIO;
673     }
674
675     if (!blk_is_available(blk)) {
676         return -ENOMEDIUM;
677     }
678
679     if (offset < 0) {
680         return -EIO;
681     }
682
683     if (!blk->allow_write_beyond_eof) {
684         len = blk_getlength(blk);
685         if (len < 0) {
686             return len;
687         }
688
689         if (offset > len || len - offset < size) {
690             return -EIO;
691         }
692     }
693
694     return 0;
695 }
696
697 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
698                              int nb_sectors)
699 {
700     if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
701         return -EIO;
702     }
703
704     if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
705         return -EIO;
706     }
707
708     return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
709                                   nb_sectors * BDRV_SECTOR_SIZE);
710 }
711
712 static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
713                                       unsigned int bytes, QEMUIOVector *qiov,
714                                       BdrvRequestFlags flags)
715 {
716     int ret = blk_check_byte_request(blk, offset, bytes);
717     if (ret < 0) {
718         return ret;
719     }
720
721     /* throttling disk I/O */
722     if (blk->public.throttle_state) {
723         throttle_group_co_io_limits_intercept(blk, bytes, false);
724     }
725
726     return bdrv_co_preadv(blk_bs(blk), offset, bytes, qiov, flags);
727 }
728
729 static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
730                                       unsigned int bytes, QEMUIOVector *qiov,
731                                       BdrvRequestFlags flags)
732 {
733     int ret;
734
735     ret = blk_check_byte_request(blk, offset, bytes);
736     if (ret < 0) {
737         return ret;
738     }
739
740     /* throttling disk I/O */
741     if (blk->public.throttle_state) {
742         throttle_group_co_io_limits_intercept(blk, bytes, true);
743     }
744
745     if (!blk->enable_write_cache) {
746         flags |= BDRV_REQ_FUA;
747     }
748
749     return bdrv_co_pwritev(blk_bs(blk), offset, bytes, qiov, flags);
750 }
751
752 typedef struct BlkRwCo {
753     BlockBackend *blk;
754     int64_t offset;
755     QEMUIOVector *qiov;
756     int ret;
757     BdrvRequestFlags flags;
758 } BlkRwCo;
759
760 static void blk_read_entry(void *opaque)
761 {
762     BlkRwCo *rwco = opaque;
763
764     rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
765                               rwco->qiov, rwco->flags);
766 }
767
768 static void blk_write_entry(void *opaque)
769 {
770     BlkRwCo *rwco = opaque;
771
772     rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
773                                rwco->qiov, rwco->flags);
774 }
775
776 static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
777                    int64_t bytes, CoroutineEntry co_entry,
778                    BdrvRequestFlags flags)
779 {
780     AioContext *aio_context;
781     QEMUIOVector qiov;
782     struct iovec iov;
783     Coroutine *co;
784     BlkRwCo rwco;
785
786     iov = (struct iovec) {
787         .iov_base = buf,
788         .iov_len = bytes,
789     };
790     qemu_iovec_init_external(&qiov, &iov, 1);
791
792     rwco = (BlkRwCo) {
793         .blk    = blk,
794         .offset = offset,
795         .qiov   = &qiov,
796         .flags  = flags,
797         .ret    = NOT_DONE,
798     };
799
800     co = qemu_coroutine_create(co_entry);
801     qemu_coroutine_enter(co, &rwco);
802
803     aio_context = blk_get_aio_context(blk);
804     while (rwco.ret == NOT_DONE) {
805         aio_poll(aio_context, true);
806     }
807
808     return rwco.ret;
809 }
810
811 int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf,
812                           int count)
813 {
814     int ret;
815
816     ret = blk_check_byte_request(blk, offset, count);
817     if (ret < 0) {
818         return ret;
819     }
820
821     bdrv_no_throttling_begin(blk_bs(blk));
822     ret = blk_pread(blk, offset, buf, count);
823     bdrv_no_throttling_end(blk_bs(blk));
824     return ret;
825 }
826
827 int blk_write_zeroes(BlockBackend *blk, int64_t offset,
828                      int count, BdrvRequestFlags flags)
829 {
830     return blk_prw(blk, offset, NULL, count, blk_write_entry,
831                    flags | BDRV_REQ_ZERO_WRITE);
832 }
833
834 static void error_callback_bh(void *opaque)
835 {
836     struct BlockBackendAIOCB *acb = opaque;
837     qemu_bh_delete(acb->bh);
838     acb->common.cb(acb->common.opaque, acb->ret);
839     qemu_aio_unref(acb);
840 }
841
842 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
843                                   BlockCompletionFunc *cb,
844                                   void *opaque, int ret)
845 {
846     struct BlockBackendAIOCB *acb;
847     QEMUBH *bh;
848
849     acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
850     acb->blk = blk;
851     acb->ret = ret;
852
853     bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
854     acb->bh = bh;
855     qemu_bh_schedule(bh);
856
857     return &acb->common;
858 }
859
860 typedef struct BlkAioEmAIOCB {
861     BlockAIOCB common;
862     BlkRwCo rwco;
863     int bytes;
864     bool has_returned;
865     QEMUBH* bh;
866 } BlkAioEmAIOCB;
867
868 static const AIOCBInfo blk_aio_em_aiocb_info = {
869     .aiocb_size         = sizeof(BlkAioEmAIOCB),
870 };
871
872 static void blk_aio_complete(BlkAioEmAIOCB *acb)
873 {
874     if (acb->bh) {
875         assert(acb->has_returned);
876         qemu_bh_delete(acb->bh);
877     }
878     if (acb->has_returned) {
879         acb->common.cb(acb->common.opaque, acb->rwco.ret);
880         qemu_aio_unref(acb);
881     }
882 }
883
884 static void blk_aio_complete_bh(void *opaque)
885 {
886     blk_aio_complete(opaque);
887 }
888
889 static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
890                                 QEMUIOVector *qiov, CoroutineEntry co_entry,
891                                 BdrvRequestFlags flags,
892                                 BlockCompletionFunc *cb, void *opaque)
893 {
894     BlkAioEmAIOCB *acb;
895     Coroutine *co;
896
897     acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
898     acb->rwco = (BlkRwCo) {
899         .blk    = blk,
900         .offset = offset,
901         .qiov   = qiov,
902         .flags  = flags,
903         .ret    = NOT_DONE,
904     };
905     acb->bytes = bytes;
906     acb->bh = NULL;
907     acb->has_returned = false;
908
909     co = qemu_coroutine_create(co_entry);
910     qemu_coroutine_enter(co, acb);
911
912     acb->has_returned = true;
913     if (acb->rwco.ret != NOT_DONE) {
914         acb->bh = aio_bh_new(blk_get_aio_context(blk), blk_aio_complete_bh, acb);
915         qemu_bh_schedule(acb->bh);
916     }
917
918     return &acb->common;
919 }
920
921 static void blk_aio_read_entry(void *opaque)
922 {
923     BlkAioEmAIOCB *acb = opaque;
924     BlkRwCo *rwco = &acb->rwco;
925
926     assert(rwco->qiov->size == acb->bytes);
927     rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes,
928                               rwco->qiov, rwco->flags);
929     blk_aio_complete(acb);
930 }
931
932 static void blk_aio_write_entry(void *opaque)
933 {
934     BlkAioEmAIOCB *acb = opaque;
935     BlkRwCo *rwco = &acb->rwco;
936
937     assert(!rwco->qiov || rwco->qiov->size == acb->bytes);
938     rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes,
939                                rwco->qiov, rwco->flags);
940     blk_aio_complete(acb);
941 }
942
943 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t offset,
944                                  int count, BdrvRequestFlags flags,
945                                  BlockCompletionFunc *cb, void *opaque)
946 {
947     return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
948                         flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
949 }
950
951 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
952 {
953     int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
954     if (ret < 0) {
955         return ret;
956     }
957     return count;
958 }
959
960 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
961                BdrvRequestFlags flags)
962 {
963     int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
964                       flags);
965     if (ret < 0) {
966         return ret;
967     }
968     return count;
969 }
970
971 int64_t blk_getlength(BlockBackend *blk)
972 {
973     if (!blk_is_available(blk)) {
974         return -ENOMEDIUM;
975     }
976
977     return bdrv_getlength(blk_bs(blk));
978 }
979
980 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
981 {
982     if (!blk_bs(blk)) {
983         *nb_sectors_ptr = 0;
984     } else {
985         bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
986     }
987 }
988
989 int64_t blk_nb_sectors(BlockBackend *blk)
990 {
991     if (!blk_is_available(blk)) {
992         return -ENOMEDIUM;
993     }
994
995     return bdrv_nb_sectors(blk_bs(blk));
996 }
997
998 BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
999                            QEMUIOVector *qiov, BdrvRequestFlags flags,
1000                            BlockCompletionFunc *cb, void *opaque)
1001 {
1002     return blk_aio_prwv(blk, offset, qiov->size, qiov,
1003                         blk_aio_read_entry, flags, cb, opaque);
1004 }
1005
1006 BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
1007                             QEMUIOVector *qiov, BdrvRequestFlags flags,
1008                             BlockCompletionFunc *cb, void *opaque)
1009 {
1010     return blk_aio_prwv(blk, offset, qiov->size, qiov,
1011                         blk_aio_write_entry, flags, cb, opaque);
1012 }
1013
1014 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1015                           BlockCompletionFunc *cb, void *opaque)
1016 {
1017     if (!blk_is_available(blk)) {
1018         return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
1019     }
1020
1021     return bdrv_aio_flush(blk_bs(blk), cb, opaque);
1022 }
1023
1024 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
1025                             int64_t sector_num, int nb_sectors,
1026                             BlockCompletionFunc *cb, void *opaque)
1027 {
1028     int ret = blk_check_request(blk, sector_num, nb_sectors);
1029     if (ret < 0) {
1030         return blk_abort_aio_request(blk, cb, opaque, ret);
1031     }
1032
1033     return bdrv_aio_discard(blk_bs(blk), sector_num, nb_sectors, cb, opaque);
1034 }
1035
1036 void blk_aio_cancel(BlockAIOCB *acb)
1037 {
1038     bdrv_aio_cancel(acb);
1039 }
1040
1041 void blk_aio_cancel_async(BlockAIOCB *acb)
1042 {
1043     bdrv_aio_cancel_async(acb);
1044 }
1045
1046 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
1047 {
1048     int i, ret;
1049
1050     for (i = 0; i < num_reqs; i++) {
1051         ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
1052         if (ret < 0) {
1053             return ret;
1054         }
1055     }
1056
1057     return bdrv_aio_multiwrite(blk_bs(blk), reqs, num_reqs);
1058 }
1059
1060 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
1061 {
1062     if (!blk_is_available(blk)) {
1063         return -ENOMEDIUM;
1064     }
1065
1066     return bdrv_ioctl(blk_bs(blk), req, buf);
1067 }
1068
1069 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
1070                           BlockCompletionFunc *cb, void *opaque)
1071 {
1072     if (!blk_is_available(blk)) {
1073         return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
1074     }
1075
1076     return bdrv_aio_ioctl(blk_bs(blk), req, buf, cb, opaque);
1077 }
1078
1079 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1080 {
1081     int ret = blk_check_request(blk, sector_num, nb_sectors);
1082     if (ret < 0) {
1083         return ret;
1084     }
1085
1086     return bdrv_co_discard(blk_bs(blk), sector_num, nb_sectors);
1087 }
1088
1089 int blk_co_flush(BlockBackend *blk)
1090 {
1091     if (!blk_is_available(blk)) {
1092         return -ENOMEDIUM;
1093     }
1094
1095     return bdrv_co_flush(blk_bs(blk));
1096 }
1097
1098 int blk_flush(BlockBackend *blk)
1099 {
1100     if (!blk_is_available(blk)) {
1101         return -ENOMEDIUM;
1102     }
1103
1104     return bdrv_flush(blk_bs(blk));
1105 }
1106
1107 void blk_drain(BlockBackend *blk)
1108 {
1109     if (blk_bs(blk)) {
1110         bdrv_drain(blk_bs(blk));
1111     }
1112 }
1113
1114 void blk_drain_all(void)
1115 {
1116     bdrv_drain_all();
1117 }
1118
1119 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error,
1120                       BlockdevOnError on_write_error)
1121 {
1122     blk->on_read_error = on_read_error;
1123     blk->on_write_error = on_write_error;
1124 }
1125
1126 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
1127 {
1128     return is_read ? blk->on_read_error : blk->on_write_error;
1129 }
1130
1131 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
1132                                       int error)
1133 {
1134     BlockdevOnError on_err = blk_get_on_error(blk, is_read);
1135
1136     switch (on_err) {
1137     case BLOCKDEV_ON_ERROR_ENOSPC:
1138         return (error == ENOSPC) ?
1139                BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
1140     case BLOCKDEV_ON_ERROR_STOP:
1141         return BLOCK_ERROR_ACTION_STOP;
1142     case BLOCKDEV_ON_ERROR_REPORT:
1143         return BLOCK_ERROR_ACTION_REPORT;
1144     case BLOCKDEV_ON_ERROR_IGNORE:
1145         return BLOCK_ERROR_ACTION_IGNORE;
1146     default:
1147         abort();
1148     }
1149 }
1150
1151 static void send_qmp_error_event(BlockBackend *blk,
1152                                  BlockErrorAction action,
1153                                  bool is_read, int error)
1154 {
1155     IoOperationType optype;
1156
1157     optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
1158     qapi_event_send_block_io_error(blk_name(blk), optype, action,
1159                                    blk_iostatus_is_enabled(blk),
1160                                    error == ENOSPC, strerror(error),
1161                                    &error_abort);
1162 }
1163
1164 /* This is done by device models because, while the block layer knows
1165  * about the error, it does not know whether an operation comes from
1166  * the device or the block layer (from a job, for example).
1167  */
1168 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
1169                       bool is_read, int error)
1170 {
1171     assert(error >= 0);
1172
1173     if (action == BLOCK_ERROR_ACTION_STOP) {
1174         /* First set the iostatus, so that "info block" returns an iostatus
1175          * that matches the events raised so far (an additional error iostatus
1176          * is fine, but not a lost one).
1177          */
1178         blk_iostatus_set_err(blk, error);
1179
1180         /* Then raise the request to stop the VM and the event.
1181          * qemu_system_vmstop_request_prepare has two effects.  First,
1182          * it ensures that the STOP event always comes after the
1183          * BLOCK_IO_ERROR event.  Second, it ensures that even if management
1184          * can observe the STOP event and do a "cont" before the STOP
1185          * event is issued, the VM will not stop.  In this case, vm_start()
1186          * also ensures that the STOP/RESUME pair of events is emitted.
1187          */
1188         qemu_system_vmstop_request_prepare();
1189         send_qmp_error_event(blk, action, is_read, error);
1190         qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
1191     } else {
1192         send_qmp_error_event(blk, action, is_read, error);
1193     }
1194 }
1195
1196 int blk_is_read_only(BlockBackend *blk)
1197 {
1198     BlockDriverState *bs = blk_bs(blk);
1199
1200     if (bs) {
1201         return bdrv_is_read_only(bs);
1202     } else {
1203         return blk->root_state.read_only;
1204     }
1205 }
1206
1207 int blk_is_sg(BlockBackend *blk)
1208 {
1209     BlockDriverState *bs = blk_bs(blk);
1210
1211     if (!bs) {
1212         return 0;
1213     }
1214
1215     return bdrv_is_sg(bs);
1216 }
1217
1218 int blk_enable_write_cache(BlockBackend *blk)
1219 {
1220     return blk->enable_write_cache;
1221 }
1222
1223 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
1224 {
1225     blk->enable_write_cache = wce;
1226 }
1227
1228 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
1229 {
1230     BlockDriverState *bs = blk_bs(blk);
1231
1232     if (!bs) {
1233         error_setg(errp, "Device '%s' has no medium", blk->name);
1234         return;
1235     }
1236
1237     bdrv_invalidate_cache(bs, errp);
1238 }
1239
1240 bool blk_is_inserted(BlockBackend *blk)
1241 {
1242     BlockDriverState *bs = blk_bs(blk);
1243
1244     return bs && bdrv_is_inserted(bs);
1245 }
1246
1247 bool blk_is_available(BlockBackend *blk)
1248 {
1249     return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk);
1250 }
1251
1252 void blk_lock_medium(BlockBackend *blk, bool locked)
1253 {
1254     BlockDriverState *bs = blk_bs(blk);
1255
1256     if (bs) {
1257         bdrv_lock_medium(bs, locked);
1258     }
1259 }
1260
1261 void blk_eject(BlockBackend *blk, bool eject_flag)
1262 {
1263     BlockDriverState *bs = blk_bs(blk);
1264
1265     if (bs) {
1266         bdrv_eject(bs, eject_flag);
1267     }
1268 }
1269
1270 int blk_get_flags(BlockBackend *blk)
1271 {
1272     BlockDriverState *bs = blk_bs(blk);
1273
1274     if (bs) {
1275         return bdrv_get_flags(bs);
1276     } else {
1277         return blk->root_state.open_flags;
1278     }
1279 }
1280
1281 int blk_get_max_transfer_length(BlockBackend *blk)
1282 {
1283     BlockDriverState *bs = blk_bs(blk);
1284
1285     if (bs) {
1286         return bs->bl.max_transfer_length;
1287     } else {
1288         return 0;
1289     }
1290 }
1291
1292 int blk_get_max_iov(BlockBackend *blk)
1293 {
1294     return blk->root->bs->bl.max_iov;
1295 }
1296
1297 void blk_set_guest_block_size(BlockBackend *blk, int align)
1298 {
1299     blk->guest_block_size = align;
1300 }
1301
1302 void *blk_try_blockalign(BlockBackend *blk, size_t size)
1303 {
1304     return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size);
1305 }
1306
1307 void *blk_blockalign(BlockBackend *blk, size_t size)
1308 {
1309     return qemu_blockalign(blk ? blk_bs(blk) : NULL, size);
1310 }
1311
1312 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
1313 {
1314     BlockDriverState *bs = blk_bs(blk);
1315
1316     if (!bs) {
1317         return false;
1318     }
1319
1320     return bdrv_op_is_blocked(bs, op, errp);
1321 }
1322
1323 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
1324 {
1325     BlockDriverState *bs = blk_bs(blk);
1326
1327     if (bs) {
1328         bdrv_op_unblock(bs, op, reason);
1329     }
1330 }
1331
1332 void blk_op_block_all(BlockBackend *blk, Error *reason)
1333 {
1334     BlockDriverState *bs = blk_bs(blk);
1335
1336     if (bs) {
1337         bdrv_op_block_all(bs, reason);
1338     }
1339 }
1340
1341 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
1342 {
1343     BlockDriverState *bs = blk_bs(blk);
1344
1345     if (bs) {
1346         bdrv_op_unblock_all(bs, reason);
1347     }
1348 }
1349
1350 AioContext *blk_get_aio_context(BlockBackend *blk)
1351 {
1352     BlockDriverState *bs = blk_bs(blk);
1353
1354     if (bs) {
1355         return bdrv_get_aio_context(bs);
1356     } else {
1357         return qemu_get_aio_context();
1358     }
1359 }
1360
1361 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb)
1362 {
1363     BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb);
1364     return blk_get_aio_context(blk_acb->blk);
1365 }
1366
1367 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
1368 {
1369     BlockDriverState *bs = blk_bs(blk);
1370
1371     if (bs) {
1372         bdrv_set_aio_context(bs, new_context);
1373     }
1374 }
1375
1376 void blk_add_aio_context_notifier(BlockBackend *blk,
1377         void (*attached_aio_context)(AioContext *new_context, void *opaque),
1378         void (*detach_aio_context)(void *opaque), void *opaque)
1379 {
1380     BlockDriverState *bs = blk_bs(blk);
1381
1382     if (bs) {
1383         bdrv_add_aio_context_notifier(bs, attached_aio_context,
1384                                       detach_aio_context, opaque);
1385     }
1386 }
1387
1388 void blk_remove_aio_context_notifier(BlockBackend *blk,
1389                                      void (*attached_aio_context)(AioContext *,
1390                                                                   void *),
1391                                      void (*detach_aio_context)(void *),
1392                                      void *opaque)
1393 {
1394     BlockDriverState *bs = blk_bs(blk);
1395
1396     if (bs) {
1397         bdrv_remove_aio_context_notifier(bs, attached_aio_context,
1398                                          detach_aio_context, opaque);
1399     }
1400 }
1401
1402 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify)
1403 {
1404     notifier_list_add(&blk->remove_bs_notifiers, notify);
1405 }
1406
1407 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
1408 {
1409     notifier_list_add(&blk->insert_bs_notifiers, notify);
1410 }
1411
1412 void blk_io_plug(BlockBackend *blk)
1413 {
1414     BlockDriverState *bs = blk_bs(blk);
1415
1416     if (bs) {
1417         bdrv_io_plug(bs);
1418     }
1419 }
1420
1421 void blk_io_unplug(BlockBackend *blk)
1422 {
1423     BlockDriverState *bs = blk_bs(blk);
1424
1425     if (bs) {
1426         bdrv_io_unplug(bs);
1427     }
1428 }
1429
1430 BlockAcctStats *blk_get_stats(BlockBackend *blk)
1431 {
1432     return &blk->stats;
1433 }
1434
1435 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
1436                   BlockCompletionFunc *cb, void *opaque)
1437 {
1438     return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
1439 }
1440
1441 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t offset,
1442                                      int count, BdrvRequestFlags flags)
1443 {
1444     return blk_co_pwritev(blk, offset, count, NULL,
1445                           flags | BDRV_REQ_ZERO_WRITE);
1446 }
1447
1448 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
1449                          const uint8_t *buf, int nb_sectors)
1450 {
1451     int ret = blk_check_request(blk, sector_num, nb_sectors);
1452     if (ret < 0) {
1453         return ret;
1454     }
1455
1456     return bdrv_write_compressed(blk_bs(blk), sector_num, buf, nb_sectors);
1457 }
1458
1459 int blk_truncate(BlockBackend *blk, int64_t offset)
1460 {
1461     if (!blk_is_available(blk)) {
1462         return -ENOMEDIUM;
1463     }
1464
1465     return bdrv_truncate(blk_bs(blk), offset);
1466 }
1467
1468 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
1469 {
1470     int ret = blk_check_request(blk, sector_num, nb_sectors);
1471     if (ret < 0) {
1472         return ret;
1473     }
1474
1475     return bdrv_discard(blk_bs(blk), sector_num, nb_sectors);
1476 }
1477
1478 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
1479                      int64_t pos, int size)
1480 {
1481     int ret;
1482
1483     if (!blk_is_available(blk)) {
1484         return -ENOMEDIUM;
1485     }
1486
1487     ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size);
1488     if (ret < 0) {
1489         return ret;
1490     }
1491
1492     if (ret == size && !blk->enable_write_cache) {
1493         ret = bdrv_flush(blk_bs(blk));
1494     }
1495
1496     return ret < 0 ? ret : size;
1497 }
1498
1499 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
1500 {
1501     if (!blk_is_available(blk)) {
1502         return -ENOMEDIUM;
1503     }
1504
1505     return bdrv_load_vmstate(blk_bs(blk), buf, pos, size);
1506 }
1507
1508 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz)
1509 {
1510     if (!blk_is_available(blk)) {
1511         return -ENOMEDIUM;
1512     }
1513
1514     return bdrv_probe_blocksizes(blk_bs(blk), bsz);
1515 }
1516
1517 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo)
1518 {
1519     if (!blk_is_available(blk)) {
1520         return -ENOMEDIUM;
1521     }
1522
1523     return bdrv_probe_geometry(blk_bs(blk), geo);
1524 }
1525
1526 /*
1527  * Updates the BlockBackendRootState object with data from the currently
1528  * attached BlockDriverState.
1529  */
1530 void blk_update_root_state(BlockBackend *blk)
1531 {
1532     assert(blk->root);
1533
1534     blk->root_state.open_flags    = blk->root->bs->open_flags;
1535     blk->root_state.read_only     = blk->root->bs->read_only;
1536     blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
1537
1538     if (blk->root_state.throttle_group) {
1539         g_free(blk->root_state.throttle_group);
1540         throttle_group_unref(blk->root_state.throttle_state);
1541     }
1542     if (blk->public.throttle_state) {
1543         const char *name = throttle_group_get_name(blk);
1544         blk->root_state.throttle_group = g_strdup(name);
1545         blk->root_state.throttle_state = throttle_group_incref(name);
1546     } else {
1547         blk->root_state.throttle_group = NULL;
1548         blk->root_state.throttle_state = NULL;
1549     }
1550 }
1551
1552 /*
1553  * Applies the information in the root state to the given BlockDriverState. This
1554  * does not include the flags which have to be specified for bdrv_open(), use
1555  * blk_get_open_flags_from_root_state() to inquire them.
1556  */
1557 void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
1558 {
1559     bs->detect_zeroes = blk->root_state.detect_zeroes;
1560     if (blk->root_state.throttle_group) {
1561         blk_io_limits_enable(blk, blk->root_state.throttle_group);
1562     }
1563 }
1564
1565 /*
1566  * Returns the flags to be used for bdrv_open() of a BlockDriverState which is
1567  * supposed to inherit the root state.
1568  */
1569 int blk_get_open_flags_from_root_state(BlockBackend *blk)
1570 {
1571     int bs_flags;
1572
1573     bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR;
1574     bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR;
1575
1576     return bs_flags;
1577 }
1578
1579 BlockBackendRootState *blk_get_root_state(BlockBackend *blk)
1580 {
1581     return &blk->root_state;
1582 }
1583
1584 int blk_commit_all(void)
1585 {
1586     BlockBackend *blk = NULL;
1587
1588     while ((blk = blk_all_next(blk)) != NULL) {
1589         AioContext *aio_context = blk_get_aio_context(blk);
1590
1591         aio_context_acquire(aio_context);
1592         if (blk_is_inserted(blk) && blk->root->bs->backing) {
1593             int ret = bdrv_commit(blk->root->bs);
1594             if (ret < 0) {
1595                 aio_context_release(aio_context);
1596                 return ret;
1597             }
1598         }
1599         aio_context_release(aio_context);
1600     }
1601     return 0;
1602 }
1603
1604 int blk_flush_all(void)
1605 {
1606     BlockBackend *blk = NULL;
1607     int result = 0;
1608
1609     while ((blk = blk_all_next(blk)) != NULL) {
1610         AioContext *aio_context = blk_get_aio_context(blk);
1611         int ret;
1612
1613         aio_context_acquire(aio_context);
1614         if (blk_is_inserted(blk)) {
1615             ret = blk_flush(blk);
1616             if (ret < 0 && !result) {
1617                 result = ret;
1618             }
1619         }
1620         aio_context_release(aio_context);
1621     }
1622
1623     return result;
1624 }
1625
1626
1627 /* throttling disk I/O limits */
1628 void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
1629 {
1630     throttle_group_config(blk, cfg);
1631 }
1632
1633 void blk_io_limits_disable(BlockBackend *blk)
1634 {
1635     assert(blk->public.throttle_state);
1636     bdrv_no_throttling_begin(blk_bs(blk));
1637     throttle_group_unregister_blk(blk);
1638     bdrv_no_throttling_end(blk_bs(blk));
1639 }
1640
1641 /* should be called before blk_set_io_limits if a limit is set */
1642 void blk_io_limits_enable(BlockBackend *blk, const char *group)
1643 {
1644     assert(!blk->public.throttle_state);
1645     throttle_group_register_blk(blk, group);
1646 }
1647
1648 void blk_io_limits_update_group(BlockBackend *blk, const char *group)
1649 {
1650     /* this BB is not part of any group */
1651     if (!blk->public.throttle_state) {
1652         return;
1653     }
1654
1655     /* this BB is a part of the same group than the one we want */
1656     if (!g_strcmp0(throttle_group_get_name(blk), group)) {
1657         return;
1658     }
1659
1660     /* need to change the group this bs belong to */
1661     blk_io_limits_disable(blk);
1662     blk_io_limits_enable(blk, group);
1663 }
This page took 0.110005 seconds and 4 git commands to generate.