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