block: Use BdrvChild callbacks for change_media/resize
[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
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 static void blk_root_drained_begin(BdrvChild *child);
95 static void blk_root_drained_end(BdrvChild *child);
96
97 static void blk_root_change_media(BdrvChild *child, bool load);
98 static void blk_root_resize(BdrvChild *child);
99
100 static const BdrvChildRole child_root = {
101 .inherit_options = blk_root_inherit_options,
102
103 .change_media = blk_root_change_media,
104 .resize = blk_root_resize,
105
106 .drained_begin = blk_root_drained_begin,
107 .drained_end = blk_root_drained_end,
108 };
109
110 /*
111 * Create a new BlockBackend with a reference count of one.
112 * Store an error through @errp on failure, unless it's null.
113 * Return the new BlockBackend on success, null on failure.
114 */
115 BlockBackend *blk_new(Error **errp)
116 {
117 BlockBackend *blk;
118
119 blk = g_new0(BlockBackend, 1);
120 blk->refcnt = 1;
121 qemu_co_queue_init(&blk->public.throttled_reqs[0]);
122 qemu_co_queue_init(&blk->public.throttled_reqs[1]);
123
124 notifier_list_init(&blk->remove_bs_notifiers);
125 notifier_list_init(&blk->insert_bs_notifiers);
126
127 QTAILQ_INSERT_TAIL(&block_backends, blk, link);
128 return blk;
129 }
130
131 /*
132 * Create a new BlockBackend with a new BlockDriverState attached.
133 * Otherwise just like blk_new(), which see.
134 */
135 BlockBackend *blk_new_with_bs(Error **errp)
136 {
137 BlockBackend *blk;
138 BlockDriverState *bs;
139
140 blk = blk_new(errp);
141 if (!blk) {
142 return NULL;
143 }
144
145 bs = bdrv_new_root();
146 blk->root = bdrv_root_attach_child(bs, "root", &child_root);
147 blk->root->opaque = blk;
148 bs->blk = blk;
149 return blk;
150 }
151
152 /*
153 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
154 *
155 * Just as with bdrv_open(), after having called this function the reference to
156 * @options belongs to the block layer (even on failure).
157 *
158 * TODO: Remove @filename and @flags; it should be possible to specify a whole
159 * BDS tree just by specifying the @options QDict (or @reference,
160 * alternatively). At the time of adding this function, this is not possible,
161 * though, so callers of this function have to be able to specify @filename and
162 * @flags.
163 */
164 BlockBackend *blk_new_open(const char *filename, const char *reference,
165 QDict *options, int flags, Error **errp)
166 {
167 BlockBackend *blk;
168 int ret;
169
170 blk = blk_new_with_bs(errp);
171 if (!blk) {
172 QDECREF(options);
173 return NULL;
174 }
175
176 ret = bdrv_open(&blk->root->bs, filename, reference, options, flags, errp);
177 if (ret < 0) {
178 blk_unref(blk);
179 return NULL;
180 }
181
182 blk_set_enable_write_cache(blk, true);
183
184 return blk;
185 }
186
187 static void blk_delete(BlockBackend *blk)
188 {
189 assert(!blk->refcnt);
190 assert(!blk->name);
191 assert(!blk->dev);
192 if (blk->root) {
193 blk_remove_bs(blk);
194 }
195 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers));
196 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers));
197 QTAILQ_REMOVE(&block_backends, blk, link);
198 drive_info_del(blk->legacy_dinfo);
199 block_acct_cleanup(&blk->stats);
200 g_free(blk);
201 }
202
203 static void drive_info_del(DriveInfo *dinfo)
204 {
205 if (!dinfo) {
206 return;
207 }
208 qemu_opts_del(dinfo->opts);
209 g_free(dinfo->serial);
210 g_free(dinfo);
211 }
212
213 int blk_get_refcnt(BlockBackend *blk)
214 {
215 return blk ? blk->refcnt : 0;
216 }
217
218 /*
219 * Increment @blk's reference count.
220 * @blk must not be null.
221 */
222 void blk_ref(BlockBackend *blk)
223 {
224 blk->refcnt++;
225 }
226
227 /*
228 * Decrement @blk's reference count.
229 * If this drops it to zero, destroy @blk.
230 * For convenience, do nothing if @blk is null.
231 */
232 void blk_unref(BlockBackend *blk)
233 {
234 if (blk) {
235 assert(blk->refcnt > 0);
236 if (!--blk->refcnt) {
237 blk_delete(blk);
238 }
239 }
240 }
241
242 /*
243 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the
244 * ones which are hidden (i.e. are not referenced by the monitor).
245 */
246 static BlockBackend *blk_all_next(BlockBackend *blk)
247 {
248 return blk ? QTAILQ_NEXT(blk, link)
249 : QTAILQ_FIRST(&block_backends);
250 }
251
252 void blk_remove_all_bs(void)
253 {
254 BlockBackend *blk = NULL;
255
256 while ((blk = blk_all_next(blk)) != NULL) {
257 AioContext *ctx = blk_get_aio_context(blk);
258
259 aio_context_acquire(ctx);
260 if (blk->root) {
261 blk_remove_bs(blk);
262 }
263 aio_context_release(ctx);
264 }
265 }
266
267 /*
268 * Return the monitor-owned BlockBackend after @blk.
269 * If @blk is null, return the first one.
270 * Else, return @blk's next sibling, which may be null.
271 *
272 * To iterate over all BlockBackends, do
273 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
274 * ...
275 * }
276 */
277 BlockBackend *blk_next(BlockBackend *blk)
278 {
279 return blk ? QTAILQ_NEXT(blk, monitor_link)
280 : QTAILQ_FIRST(&monitor_block_backends);
281 }
282
283 /*
284 * Iterates over all BlockDriverStates which are attached to a BlockBackend.
285 * This function is for use by bdrv_next().
286 *
287 * @bs must be NULL or a BDS that is attached to a BB.
288 */
289 BlockDriverState *blk_next_root_bs(BlockDriverState *bs)
290 {
291 BlockBackend *blk;
292
293 if (bs) {
294 assert(bs->blk);
295 blk = bs->blk;
296 } else {
297 blk = NULL;
298 }
299
300 do {
301 blk = blk_all_next(blk);
302 } while (blk && !blk->root);
303
304 return blk ? blk->root->bs : NULL;
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 /*
392 * Return @blk's DriveInfo if any, else null.
393 */
394 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
395 {
396 return blk->legacy_dinfo;
397 }
398
399 /*
400 * Set @blk's DriveInfo to @dinfo, and return it.
401 * @blk must not have a DriveInfo set already.
402 * No other BlockBackend may have the same DriveInfo set.
403 */
404 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
405 {
406 assert(!blk->legacy_dinfo);
407 return blk->legacy_dinfo = dinfo;
408 }
409
410 /*
411 * Return the BlockBackend with DriveInfo @dinfo.
412 * It must exist.
413 */
414 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
415 {
416 BlockBackend *blk = NULL;
417
418 while ((blk = blk_next(blk)) != NULL) {
419 if (blk->legacy_dinfo == dinfo) {
420 return blk;
421 }
422 }
423 abort();
424 }
425
426 /*
427 * Returns a pointer to the publicly accessible fields of @blk.
428 */
429 BlockBackendPublic *blk_get_public(BlockBackend *blk)
430 {
431 return &blk->public;
432 }
433
434 /*
435 * Returns a BlockBackend given the associated @public fields.
436 */
437 BlockBackend *blk_by_public(BlockBackendPublic *public)
438 {
439 return container_of(public, BlockBackend, public);
440 }
441
442 /*
443 * Disassociates the currently associated BlockDriverState from @blk.
444 */
445 void blk_remove_bs(BlockBackend *blk)
446 {
447 assert(blk->root->bs->blk == blk);
448
449 notifier_list_notify(&blk->remove_bs_notifiers, blk);
450 if (blk->public.throttle_state) {
451 throttle_timers_detach_aio_context(&blk->public.throttle_timers);
452 }
453
454 blk_update_root_state(blk);
455
456 blk->root->bs->blk = NULL;
457 bdrv_root_unref_child(blk->root);
458 blk->root = NULL;
459 }
460
461 /*
462 * Associates a new BlockDriverState with @blk.
463 */
464 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
465 {
466 assert(!blk->root && !bs->blk);
467 bdrv_ref(bs);
468 blk->root = bdrv_root_attach_child(bs, "root", &child_root);
469 blk->root->opaque = blk;
470 bs->blk = blk;
471
472 notifier_list_notify(&blk->insert_bs_notifiers, blk);
473 if (blk->public.throttle_state) {
474 throttle_timers_attach_aio_context(
475 &blk->public.throttle_timers, bdrv_get_aio_context(bs));
476 }
477 }
478
479 /*
480 * Attach device model @dev to @blk.
481 * Return 0 on success, -EBUSY when a device model is attached already.
482 */
483 int blk_attach_dev(BlockBackend *blk, void *dev)
484 /* TODO change to DeviceState *dev when all users are qdevified */
485 {
486 if (blk->dev) {
487 return -EBUSY;
488 }
489 blk_ref(blk);
490 blk->dev = dev;
491 blk_iostatus_reset(blk);
492 return 0;
493 }
494
495 /*
496 * Attach device model @dev to @blk.
497 * @blk must not have a device model attached already.
498 * TODO qdevified devices don't use this, remove when devices are qdevified
499 */
500 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
501 {
502 if (blk_attach_dev(blk, dev) < 0) {
503 abort();
504 }
505 }
506
507 /*
508 * Detach device model @dev from @blk.
509 * @dev must be currently attached to @blk.
510 */
511 void blk_detach_dev(BlockBackend *blk, void *dev)
512 /* TODO change to DeviceState *dev when all users are qdevified */
513 {
514 assert(blk->dev == dev);
515 blk->dev = NULL;
516 blk->dev_ops = NULL;
517 blk->dev_opaque = NULL;
518 blk->guest_block_size = 512;
519 blk_unref(blk);
520 }
521
522 /*
523 * Return the device model attached to @blk if any, else null.
524 */
525 void *blk_get_attached_dev(BlockBackend *blk)
526 /* TODO change to return DeviceState * when all users are qdevified */
527 {
528 return blk->dev;
529 }
530
531 /*
532 * Set @blk's device model callbacks to @ops.
533 * @opaque is the opaque argument to pass to the callbacks.
534 * This is for use by device models.
535 */
536 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
537 void *opaque)
538 {
539 blk->dev_ops = ops;
540 blk->dev_opaque = opaque;
541 }
542
543 /*
544 * Notify @blk's attached device model of media change.
545 * If @load is true, notify of media load.
546 * Else, notify of media eject.
547 * Also send DEVICE_TRAY_MOVED events as appropriate.
548 */
549 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
550 {
551 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
552 bool tray_was_open, tray_is_open;
553
554 tray_was_open = blk_dev_is_tray_open(blk);
555 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
556 tray_is_open = blk_dev_is_tray_open(blk);
557
558 if (tray_was_open != tray_is_open) {
559 qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
560 &error_abort);
561 }
562 }
563 }
564
565 static void blk_root_change_media(BdrvChild *child, bool load)
566 {
567 blk_dev_change_media_cb(child->opaque, load);
568 }
569
570 /*
571 * Does @blk's attached device model have removable media?
572 * %true if no device model is attached.
573 */
574 bool blk_dev_has_removable_media(BlockBackend *blk)
575 {
576 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
577 }
578
579 /*
580 * Does @blk's attached device model have a tray?
581 */
582 bool blk_dev_has_tray(BlockBackend *blk)
583 {
584 return blk->dev_ops && blk->dev_ops->is_tray_open;
585 }
586
587 /*
588 * Notify @blk's attached device model of a media eject request.
589 * If @force is true, the medium is about to be yanked out forcefully.
590 */
591 void blk_dev_eject_request(BlockBackend *blk, bool force)
592 {
593 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
594 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
595 }
596 }
597
598 /*
599 * Does @blk's attached device model have a tray, and is it open?
600 */
601 bool blk_dev_is_tray_open(BlockBackend *blk)
602 {
603 if (blk_dev_has_tray(blk)) {
604 return blk->dev_ops->is_tray_open(blk->dev_opaque);
605 }
606 return false;
607 }
608
609 /*
610 * Does @blk's attached device model have the medium locked?
611 * %false if the device model has no such lock.
612 */
613 bool blk_dev_is_medium_locked(BlockBackend *blk)
614 {
615 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
616 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
617 }
618 return false;
619 }
620
621 /*
622 * Notify @blk's attached device model of a backend size change.
623 */
624 static void blk_root_resize(BdrvChild *child)
625 {
626 BlockBackend *blk = child->opaque;
627
628 if (blk->dev_ops && blk->dev_ops->resize_cb) {
629 blk->dev_ops->resize_cb(blk->dev_opaque);
630 }
631 }
632
633 void blk_iostatus_enable(BlockBackend *blk)
634 {
635 blk->iostatus_enabled = true;
636 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
637 }
638
639 /* The I/O status is only enabled if the drive explicitly
640 * enables it _and_ the VM is configured to stop on errors */
641 bool blk_iostatus_is_enabled(const BlockBackend *blk)
642 {
643 return (blk->iostatus_enabled &&
644 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
645 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
646 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP));
647 }
648
649 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk)
650 {
651 return blk->iostatus;
652 }
653
654 void blk_iostatus_disable(BlockBackend *blk)
655 {
656 blk->iostatus_enabled = false;
657 }
658
659 void blk_iostatus_reset(BlockBackend *blk)
660 {
661 if (blk_iostatus_is_enabled(blk)) {
662 BlockDriverState *bs = blk_bs(blk);
663 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
664 if (bs && bs->job) {
665 block_job_iostatus_reset(bs->job);
666 }
667 }
668 }
669
670 void blk_iostatus_set_err(BlockBackend *blk, int error)
671 {
672 assert(blk_iostatus_is_enabled(blk));
673 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
674 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
675 BLOCK_DEVICE_IO_STATUS_FAILED;
676 }
677 }
678
679 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow)
680 {
681 blk->allow_write_beyond_eof = allow;
682 }
683
684 static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
685 size_t size)
686 {
687 int64_t len;
688
689 if (size > INT_MAX) {
690 return -EIO;
691 }
692
693 if (!blk_is_available(blk)) {
694 return -ENOMEDIUM;
695 }
696
697 if (offset < 0) {
698 return -EIO;
699 }
700
701 if (!blk->allow_write_beyond_eof) {
702 len = blk_getlength(blk);
703 if (len < 0) {
704 return len;
705 }
706
707 if (offset > len || len - offset < size) {
708 return -EIO;
709 }
710 }
711
712 return 0;
713 }
714
715 static int blk_check_request(BlockBackend *blk, int64_t sector_num,
716 int nb_sectors)
717 {
718 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
719 return -EIO;
720 }
721
722 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
723 return -EIO;
724 }
725
726 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
727 nb_sectors * BDRV_SECTOR_SIZE);
728 }
729
730 static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
731 unsigned int bytes, QEMUIOVector *qiov,
732 BdrvRequestFlags flags)
733 {
734 int ret = blk_check_byte_request(blk, offset, bytes);
735 if (ret < 0) {
736 return ret;
737 }
738
739 /* throttling disk I/O */
740 if (blk->public.throttle_state) {
741 throttle_group_co_io_limits_intercept(blk, bytes, false);
742 }
743
744 return bdrv_co_preadv(blk_bs(blk), offset, bytes, qiov, flags);
745 }
746
747 static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
748 unsigned int bytes, QEMUIOVector *qiov,
749 BdrvRequestFlags flags)
750 {
751 int ret;
752
753 ret = blk_check_byte_request(blk, offset, bytes);
754 if (ret < 0) {
755 return ret;
756 }
757
758 /* throttling disk I/O */
759 if (blk->public.throttle_state) {
760 throttle_group_co_io_limits_intercept(blk, bytes, true);
761 }
762
763 if (!blk->enable_write_cache) {
764 flags |= BDRV_REQ_FUA;
765 }
766
767 return bdrv_co_pwritev(blk_bs(blk), offset, bytes, qiov, flags);
768 }
769
770 typedef struct BlkRwCo {
771 BlockBackend *blk;
772 int64_t offset;
773 QEMUIOVector *qiov;
774 int ret;
775 BdrvRequestFlags flags;
776 } BlkRwCo;
777
778 static void blk_read_entry(void *opaque)
779 {
780 BlkRwCo *rwco = opaque;
781
782 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
783 rwco->qiov, rwco->flags);
784 }
785
786 static void blk_write_entry(void *opaque)
787 {
788 BlkRwCo *rwco = opaque;
789
790 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
791 rwco->qiov, rwco->flags);
792 }
793
794 static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
795 int64_t bytes, CoroutineEntry co_entry,
796 BdrvRequestFlags flags)
797 {
798 AioContext *aio_context;
799 QEMUIOVector qiov;
800 struct iovec iov;
801 Coroutine *co;
802 BlkRwCo rwco;
803
804 iov = (struct iovec) {
805 .iov_base = buf,
806 .iov_len = bytes,
807 };
808 qemu_iovec_init_external(&qiov, &iov, 1);
809
810 rwco = (BlkRwCo) {
811 .blk = blk,
812 .offset = offset,
813 .qiov = &qiov,
814 .flags = flags,
815 .ret = NOT_DONE,
816 };
817
818 co = qemu_coroutine_create(co_entry);
819 qemu_coroutine_enter(co, &rwco);
820
821 aio_context = blk_get_aio_context(blk);
822 while (rwco.ret == NOT_DONE) {
823 aio_poll(aio_context, true);
824 }
825
826 return rwco.ret;
827 }
828
829 int blk_pread_unthrottled(BlockBackend *blk, int64_t offset, uint8_t *buf,
830 int count)
831 {
832 int ret;
833
834 ret = blk_check_byte_request(blk, offset, count);
835 if (ret < 0) {
836 return ret;
837 }
838
839 blk_root_drained_begin(blk->root);
840 ret = blk_pread(blk, offset, buf, count);
841 blk_root_drained_end(blk->root);
842 return ret;
843 }
844
845 int blk_write_zeroes(BlockBackend *blk, int64_t offset,
846 int count, BdrvRequestFlags flags)
847 {
848 return blk_prw(blk, offset, NULL, count, blk_write_entry,
849 flags | BDRV_REQ_ZERO_WRITE);
850 }
851
852 static void error_callback_bh(void *opaque)
853 {
854 struct BlockBackendAIOCB *acb = opaque;
855 qemu_bh_delete(acb->bh);
856 acb->common.cb(acb->common.opaque, acb->ret);
857 qemu_aio_unref(acb);
858 }
859
860 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
861 BlockCompletionFunc *cb,
862 void *opaque, int ret)
863 {
864 struct BlockBackendAIOCB *acb;
865 QEMUBH *bh;
866
867 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
868 acb->blk = blk;
869 acb->ret = ret;
870
871 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
872 acb->bh = bh;
873 qemu_bh_schedule(bh);
874
875 return &acb->common;
876 }
877
878 typedef struct BlkAioEmAIOCB {
879 BlockAIOCB common;
880 BlkRwCo rwco;
881 int bytes;
882 bool has_returned;
883 QEMUBH* bh;
884 } BlkAioEmAIOCB;
885
886 static const AIOCBInfo blk_aio_em_aiocb_info = {
887 .aiocb_size = sizeof(BlkAioEmAIOCB),
888 };
889
890 static void blk_aio_complete(BlkAioEmAIOCB *acb)
891 {
892 if (acb->bh) {
893 assert(acb->has_returned);
894 qemu_bh_delete(acb->bh);
895 }
896 if (acb->has_returned) {
897 acb->common.cb(acb->common.opaque, acb->rwco.ret);
898 qemu_aio_unref(acb);
899 }
900 }
901
902 static void blk_aio_complete_bh(void *opaque)
903 {
904 blk_aio_complete(opaque);
905 }
906
907 static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes,
908 QEMUIOVector *qiov, CoroutineEntry co_entry,
909 BdrvRequestFlags flags,
910 BlockCompletionFunc *cb, void *opaque)
911 {
912 BlkAioEmAIOCB *acb;
913 Coroutine *co;
914
915 acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
916 acb->rwco = (BlkRwCo) {
917 .blk = blk,
918 .offset = offset,
919 .qiov = qiov,
920 .flags = flags,
921 .ret = NOT_DONE,
922 };
923 acb->bytes = bytes;
924 acb->bh = NULL;
925 acb->has_returned = false;
926
927 co = qemu_coroutine_create(co_entry);
928 qemu_coroutine_enter(co, acb);
929
930 acb->has_returned = true;
931 if (acb->rwco.ret != NOT_DONE) {
932 acb->bh = aio_bh_new(blk_get_aio_context(blk), blk_aio_complete_bh, acb);
933 qemu_bh_schedule(acb->bh);
934 }
935
936 return &acb->common;
937 }
938
939 static void blk_aio_read_entry(void *opaque)
940 {
941 BlkAioEmAIOCB *acb = opaque;
942 BlkRwCo *rwco = &acb->rwco;
943
944 assert(rwco->qiov->size == acb->bytes);
945 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, acb->bytes,
946 rwco->qiov, rwco->flags);
947 blk_aio_complete(acb);
948 }
949
950 static void blk_aio_write_entry(void *opaque)
951 {
952 BlkAioEmAIOCB *acb = opaque;
953 BlkRwCo *rwco = &acb->rwco;
954
955 assert(!rwco->qiov || rwco->qiov->size == acb->bytes);
956 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, acb->bytes,
957 rwco->qiov, rwco->flags);
958 blk_aio_complete(acb);
959 }
960
961 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t offset,
962 int count, BdrvRequestFlags flags,
963 BlockCompletionFunc *cb, void *opaque)
964 {
965 return blk_aio_prwv(blk, offset, count, NULL, blk_aio_write_entry,
966 flags | BDRV_REQ_ZERO_WRITE, cb, opaque);
967 }
968
969 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
970 {
971 int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
972 if (ret < 0) {
973 return ret;
974 }
975 return count;
976 }
977
978 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count,
979 BdrvRequestFlags flags)
980 {
981 int ret = blk_prw(blk, offset, (void *) buf, count, blk_write_entry,
982 flags);
983 if (ret < 0) {
984 return ret;
985 }
986 return count;
987 }
988
989 int64_t blk_getlength(BlockBackend *blk)
990 {
991 if (!blk_is_available(blk)) {
992 return -ENOMEDIUM;
993 }
994
995 return bdrv_getlength(blk_bs(blk));
996 }
997
998 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
999 {
1000 if (!blk_bs(blk)) {
1001 *nb_sectors_ptr = 0;
1002 } else {
1003 bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr);
1004 }
1005 }
1006
1007 int64_t blk_nb_sectors(BlockBackend *blk)
1008 {
1009 if (!blk_is_available(blk)) {
1010 return -ENOMEDIUM;
1011 }
1012
1013 return bdrv_nb_sectors(blk_bs(blk));
1014 }
1015
1016 BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
1017 QEMUIOVector *qiov, BdrvRequestFlags flags,
1018 BlockCompletionFunc *cb, void *opaque)
1019 {
1020 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1021 blk_aio_read_entry, flags, cb, opaque);
1022 }
1023
1024 BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
1025 QEMUIOVector *qiov, BdrvRequestFlags flags,
1026 BlockCompletionFunc *cb, void *opaque)
1027 {
1028 return blk_aio_prwv(blk, offset, qiov->size, qiov,
1029 blk_aio_write_entry, flags, cb, opaque);
1030 }
1031
1032 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
1033 BlockCompletionFunc *cb, void *opaque)
1034 {
1035 if (!blk_is_available(blk)) {
1036 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM);
1037 }
1038
1039 return bdrv_aio_flush(blk_bs(blk), cb, opaque);
1040 }
1041
1042 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
1043 int64_t sector_num, int nb_sectors,
1044 BlockCompletionFunc *cb, void *opaque)
1045 {
1046 int ret = blk_check_request(blk, sector_num, nb_sectors);
1047 if (ret < 0) {
1048 return blk_abort_aio_request(blk, cb, opaque, ret);
1049 }
1050
1051 return bdrv_aio_discard(blk_bs(blk), sector_num, nb_sectors, cb, opaque);
1052 }
1053
1054 void blk_aio_cancel(BlockAIOCB *acb)
1055 {
1056 bdrv_aio_cancel(acb);
1057 }
1058
1059 void blk_aio_cancel_async(BlockAIOCB *acb)
1060 {
1061 bdrv_aio_cancel_async(acb);
1062 }
1063
1064 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
1065 {
1066 int i, ret;
1067
1068 for (i = 0; i < num_reqs; i++) {
1069 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
1070 if (ret < 0) {
1071 return ret;
1072 }
1073 }
1074
1075 return bdrv_aio_multiwrite(blk_bs(blk), reqs, num_reqs);
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_write_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 if (blk->public.io_limits_disabled++ == 0) {
1679 throttle_group_restart_blk(blk);
1680 }
1681 }
1682
1683 static void blk_root_drained_end(BdrvChild *child)
1684 {
1685 BlockBackend *blk = child->opaque;
1686
1687 assert(blk->public.io_limits_disabled);
1688 --blk->public.io_limits_disabled;
1689 }