xref: /openbmc/qemu/block/io.c (revision 5cc8767d)
1 /*
2  * Block layer I/O functions
3  *
4  * Copyright (c) 2003 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "trace.h"
27 #include "sysemu/block-backend.h"
28 #include "block/aio-wait.h"
29 #include "block/blockjob.h"
30 #include "block/blockjob_int.h"
31 #include "block/block_int.h"
32 #include "qemu/cutils.h"
33 #include "qapi/error.h"
34 #include "qemu/error-report.h"
35 
36 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
37 
38 /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
39 #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
40 
41 static void bdrv_parent_cb_resize(BlockDriverState *bs);
42 static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
43     int64_t offset, int bytes, BdrvRequestFlags flags);
44 
45 void bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore,
46                                bool ignore_bds_parents)
47 {
48     BdrvChild *c, *next;
49 
50     QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
51         if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
52             continue;
53         }
54         bdrv_parent_drained_begin_single(c, false);
55     }
56 }
57 
58 void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore,
59                              bool ignore_bds_parents)
60 {
61     BdrvChild *c, *next;
62 
63     QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
64         if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
65             continue;
66         }
67         if (c->role->drained_end) {
68             c->role->drained_end(c);
69         }
70     }
71 }
72 
73 static bool bdrv_parent_drained_poll_single(BdrvChild *c)
74 {
75     if (c->role->drained_poll) {
76         return c->role->drained_poll(c);
77     }
78     return false;
79 }
80 
81 static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,
82                                      bool ignore_bds_parents)
83 {
84     BdrvChild *c, *next;
85     bool busy = false;
86 
87     QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
88         if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
89             continue;
90         }
91         busy |= bdrv_parent_drained_poll_single(c);
92     }
93 
94     return busy;
95 }
96 
97 void bdrv_parent_drained_begin_single(BdrvChild *c, bool poll)
98 {
99     if (c->role->drained_begin) {
100         c->role->drained_begin(c);
101     }
102     if (poll) {
103         BDRV_POLL_WHILE(c->bs, bdrv_parent_drained_poll_single(c));
104     }
105 }
106 
107 static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src)
108 {
109     dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer);
110     dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer);
111     dst->opt_mem_alignment = MAX(dst->opt_mem_alignment,
112                                  src->opt_mem_alignment);
113     dst->min_mem_alignment = MAX(dst->min_mem_alignment,
114                                  src->min_mem_alignment);
115     dst->max_iov = MIN_NON_ZERO(dst->max_iov, src->max_iov);
116 }
117 
118 void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
119 {
120     BlockDriver *drv = bs->drv;
121     Error *local_err = NULL;
122 
123     memset(&bs->bl, 0, sizeof(bs->bl));
124 
125     if (!drv) {
126         return;
127     }
128 
129     /* Default alignment based on whether driver has byte interface */
130     bs->bl.request_alignment = (drv->bdrv_co_preadv ||
131                                 drv->bdrv_aio_preadv) ? 1 : 512;
132 
133     /* Take some limits from the children as a default */
134     if (bs->file) {
135         bdrv_refresh_limits(bs->file->bs, &local_err);
136         if (local_err) {
137             error_propagate(errp, local_err);
138             return;
139         }
140         bdrv_merge_limits(&bs->bl, &bs->file->bs->bl);
141     } else {
142         bs->bl.min_mem_alignment = 512;
143         bs->bl.opt_mem_alignment = getpagesize();
144 
145         /* Safe default since most protocols use readv()/writev()/etc */
146         bs->bl.max_iov = IOV_MAX;
147     }
148 
149     if (bs->backing) {
150         bdrv_refresh_limits(bs->backing->bs, &local_err);
151         if (local_err) {
152             error_propagate(errp, local_err);
153             return;
154         }
155         bdrv_merge_limits(&bs->bl, &bs->backing->bs->bl);
156     }
157 
158     /* Then let the driver override it */
159     if (drv->bdrv_refresh_limits) {
160         drv->bdrv_refresh_limits(bs, errp);
161     }
162 }
163 
164 /**
165  * The copy-on-read flag is actually a reference count so multiple users may
166  * use the feature without worrying about clobbering its previous state.
167  * Copy-on-read stays enabled until all users have called to disable it.
168  */
169 void bdrv_enable_copy_on_read(BlockDriverState *bs)
170 {
171     atomic_inc(&bs->copy_on_read);
172 }
173 
174 void bdrv_disable_copy_on_read(BlockDriverState *bs)
175 {
176     int old = atomic_fetch_dec(&bs->copy_on_read);
177     assert(old >= 1);
178 }
179 
180 typedef struct {
181     Coroutine *co;
182     BlockDriverState *bs;
183     bool done;
184     bool begin;
185     bool recursive;
186     bool poll;
187     BdrvChild *parent;
188     bool ignore_bds_parents;
189 } BdrvCoDrainData;
190 
191 static void coroutine_fn bdrv_drain_invoke_entry(void *opaque)
192 {
193     BdrvCoDrainData *data = opaque;
194     BlockDriverState *bs = data->bs;
195 
196     if (data->begin) {
197         bs->drv->bdrv_co_drain_begin(bs);
198     } else {
199         bs->drv->bdrv_co_drain_end(bs);
200     }
201 
202     /* Set data->done before reading bs->wakeup.  */
203     atomic_mb_set(&data->done, true);
204     bdrv_dec_in_flight(bs);
205 
206     if (data->begin) {
207         g_free(data);
208     }
209 }
210 
211 /* Recursively call BlockDriver.bdrv_co_drain_begin/end callbacks */
212 static void bdrv_drain_invoke(BlockDriverState *bs, bool begin)
213 {
214     BdrvCoDrainData *data;
215 
216     if (!bs->drv || (begin && !bs->drv->bdrv_co_drain_begin) ||
217             (!begin && !bs->drv->bdrv_co_drain_end)) {
218         return;
219     }
220 
221     data = g_new(BdrvCoDrainData, 1);
222     *data = (BdrvCoDrainData) {
223         .bs = bs,
224         .done = false,
225         .begin = begin
226     };
227 
228     /* Make sure the driver callback completes during the polling phase for
229      * drain_begin. */
230     bdrv_inc_in_flight(bs);
231     data->co = qemu_coroutine_create(bdrv_drain_invoke_entry, data);
232     aio_co_schedule(bdrv_get_aio_context(bs), data->co);
233 
234     if (!begin) {
235         BDRV_POLL_WHILE(bs, !data->done);
236         g_free(data);
237     }
238 }
239 
240 /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
241 bool bdrv_drain_poll(BlockDriverState *bs, bool recursive,
242                      BdrvChild *ignore_parent, bool ignore_bds_parents)
243 {
244     BdrvChild *child, *next;
245 
246     if (bdrv_parent_drained_poll(bs, ignore_parent, ignore_bds_parents)) {
247         return true;
248     }
249 
250     if (atomic_read(&bs->in_flight)) {
251         return true;
252     }
253 
254     if (recursive) {
255         assert(!ignore_bds_parents);
256         QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
257             if (bdrv_drain_poll(child->bs, recursive, child, false)) {
258                 return true;
259             }
260         }
261     }
262 
263     return false;
264 }
265 
266 static bool bdrv_drain_poll_top_level(BlockDriverState *bs, bool recursive,
267                                       BdrvChild *ignore_parent)
268 {
269     return bdrv_drain_poll(bs, recursive, ignore_parent, false);
270 }
271 
272 static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
273                                   BdrvChild *parent, bool ignore_bds_parents,
274                                   bool poll);
275 static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
276                                 BdrvChild *parent, bool ignore_bds_parents);
277 
278 static void bdrv_co_drain_bh_cb(void *opaque)
279 {
280     BdrvCoDrainData *data = opaque;
281     Coroutine *co = data->co;
282     BlockDriverState *bs = data->bs;
283 
284     if (bs) {
285         AioContext *ctx = bdrv_get_aio_context(bs);
286         AioContext *co_ctx = qemu_coroutine_get_aio_context(co);
287 
288         /*
289          * When the coroutine yielded, the lock for its home context was
290          * released, so we need to re-acquire it here. If it explicitly
291          * acquired a different context, the lock is still held and we don't
292          * want to lock it a second time (or AIO_WAIT_WHILE() would hang).
293          */
294         if (ctx == co_ctx) {
295             aio_context_acquire(ctx);
296         }
297         bdrv_dec_in_flight(bs);
298         if (data->begin) {
299             bdrv_do_drained_begin(bs, data->recursive, data->parent,
300                                   data->ignore_bds_parents, data->poll);
301         } else {
302             bdrv_do_drained_end(bs, data->recursive, data->parent,
303                                 data->ignore_bds_parents);
304         }
305         if (ctx == co_ctx) {
306             aio_context_release(ctx);
307         }
308     } else {
309         assert(data->begin);
310         bdrv_drain_all_begin();
311     }
312 
313     data->done = true;
314     aio_co_wake(co);
315 }
316 
317 static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
318                                                 bool begin, bool recursive,
319                                                 BdrvChild *parent,
320                                                 bool ignore_bds_parents,
321                                                 bool poll)
322 {
323     BdrvCoDrainData data;
324 
325     /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
326      * other coroutines run if they were queued by aio_co_enter(). */
327 
328     assert(qemu_in_coroutine());
329     data = (BdrvCoDrainData) {
330         .co = qemu_coroutine_self(),
331         .bs = bs,
332         .done = false,
333         .begin = begin,
334         .recursive = recursive,
335         .parent = parent,
336         .ignore_bds_parents = ignore_bds_parents,
337         .poll = poll,
338     };
339     if (bs) {
340         bdrv_inc_in_flight(bs);
341     }
342     aio_bh_schedule_oneshot(bdrv_get_aio_context(bs),
343                             bdrv_co_drain_bh_cb, &data);
344 
345     qemu_coroutine_yield();
346     /* If we are resumed from some other event (such as an aio completion or a
347      * timer callback), it is a bug in the caller that should be fixed. */
348     assert(data.done);
349 }
350 
351 void bdrv_do_drained_begin_quiesce(BlockDriverState *bs,
352                                    BdrvChild *parent, bool ignore_bds_parents)
353 {
354     assert(!qemu_in_coroutine());
355 
356     /* Stop things in parent-to-child order */
357     if (atomic_fetch_inc(&bs->quiesce_counter) == 0) {
358         aio_disable_external(bdrv_get_aio_context(bs));
359     }
360 
361     bdrv_parent_drained_begin(bs, parent, ignore_bds_parents);
362     bdrv_drain_invoke(bs, true);
363 }
364 
365 static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
366                                   BdrvChild *parent, bool ignore_bds_parents,
367                                   bool poll)
368 {
369     BdrvChild *child, *next;
370 
371     if (qemu_in_coroutine()) {
372         bdrv_co_yield_to_drain(bs, true, recursive, parent, ignore_bds_parents,
373                                poll);
374         return;
375     }
376 
377     bdrv_do_drained_begin_quiesce(bs, parent, ignore_bds_parents);
378 
379     if (recursive) {
380         assert(!ignore_bds_parents);
381         bs->recursive_quiesce_counter++;
382         QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
383             bdrv_do_drained_begin(child->bs, true, child, ignore_bds_parents,
384                                   false);
385         }
386     }
387 
388     /*
389      * Wait for drained requests to finish.
390      *
391      * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
392      * call is needed so things in this AioContext can make progress even
393      * though we don't return to the main AioContext loop - this automatically
394      * includes other nodes in the same AioContext and therefore all child
395      * nodes.
396      */
397     if (poll) {
398         assert(!ignore_bds_parents);
399         BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, recursive, parent));
400     }
401 }
402 
403 void bdrv_drained_begin(BlockDriverState *bs)
404 {
405     bdrv_do_drained_begin(bs, false, NULL, false, true);
406 }
407 
408 void bdrv_subtree_drained_begin(BlockDriverState *bs)
409 {
410     bdrv_do_drained_begin(bs, true, NULL, false, true);
411 }
412 
413 static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
414                                 BdrvChild *parent, bool ignore_bds_parents)
415 {
416     BdrvChild *child, *next;
417     int old_quiesce_counter;
418 
419     if (qemu_in_coroutine()) {
420         bdrv_co_yield_to_drain(bs, false, recursive, parent, ignore_bds_parents,
421                                false);
422         return;
423     }
424     assert(bs->quiesce_counter > 0);
425 
426     /* Re-enable things in child-to-parent order */
427     bdrv_drain_invoke(bs, false);
428     bdrv_parent_drained_end(bs, parent, ignore_bds_parents);
429 
430     old_quiesce_counter = atomic_fetch_dec(&bs->quiesce_counter);
431     if (old_quiesce_counter == 1) {
432         aio_enable_external(bdrv_get_aio_context(bs));
433     }
434 
435     if (recursive) {
436         assert(!ignore_bds_parents);
437         bs->recursive_quiesce_counter--;
438         QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
439             bdrv_do_drained_end(child->bs, true, child, ignore_bds_parents);
440         }
441     }
442 }
443 
444 void bdrv_drained_end(BlockDriverState *bs)
445 {
446     bdrv_do_drained_end(bs, false, NULL, false);
447 }
448 
449 void bdrv_subtree_drained_end(BlockDriverState *bs)
450 {
451     bdrv_do_drained_end(bs, true, NULL, false);
452 }
453 
454 void bdrv_apply_subtree_drain(BdrvChild *child, BlockDriverState *new_parent)
455 {
456     int i;
457 
458     for (i = 0; i < new_parent->recursive_quiesce_counter; i++) {
459         bdrv_do_drained_begin(child->bs, true, child, false, true);
460     }
461 }
462 
463 void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_parent)
464 {
465     int i;
466 
467     for (i = 0; i < old_parent->recursive_quiesce_counter; i++) {
468         bdrv_do_drained_end(child->bs, true, child, false);
469     }
470 }
471 
472 /*
473  * Wait for pending requests to complete on a single BlockDriverState subtree,
474  * and suspend block driver's internal I/O until next request arrives.
475  *
476  * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
477  * AioContext.
478  */
479 void coroutine_fn bdrv_co_drain(BlockDriverState *bs)
480 {
481     assert(qemu_in_coroutine());
482     bdrv_drained_begin(bs);
483     bdrv_drained_end(bs);
484 }
485 
486 void bdrv_drain(BlockDriverState *bs)
487 {
488     bdrv_drained_begin(bs);
489     bdrv_drained_end(bs);
490 }
491 
492 static void bdrv_drain_assert_idle(BlockDriverState *bs)
493 {
494     BdrvChild *child, *next;
495 
496     assert(atomic_read(&bs->in_flight) == 0);
497     QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
498         bdrv_drain_assert_idle(child->bs);
499     }
500 }
501 
502 unsigned int bdrv_drain_all_count = 0;
503 
504 static bool bdrv_drain_all_poll(void)
505 {
506     BlockDriverState *bs = NULL;
507     bool result = false;
508 
509     /* bdrv_drain_poll() can't make changes to the graph and we are holding the
510      * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
511     while ((bs = bdrv_next_all_states(bs))) {
512         AioContext *aio_context = bdrv_get_aio_context(bs);
513         aio_context_acquire(aio_context);
514         result |= bdrv_drain_poll(bs, false, NULL, true);
515         aio_context_release(aio_context);
516     }
517 
518     return result;
519 }
520 
521 /*
522  * Wait for pending requests to complete across all BlockDriverStates
523  *
524  * This function does not flush data to disk, use bdrv_flush_all() for that
525  * after calling this function.
526  *
527  * This pauses all block jobs and disables external clients. It must
528  * be paired with bdrv_drain_all_end().
529  *
530  * NOTE: no new block jobs or BlockDriverStates can be created between
531  * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
532  */
533 void bdrv_drain_all_begin(void)
534 {
535     BlockDriverState *bs = NULL;
536 
537     if (qemu_in_coroutine()) {
538         bdrv_co_yield_to_drain(NULL, true, false, NULL, true, true);
539         return;
540     }
541 
542     /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
543      * loop AioContext, so make sure we're in the main context. */
544     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
545     assert(bdrv_drain_all_count < INT_MAX);
546     bdrv_drain_all_count++;
547 
548     /* Quiesce all nodes, without polling in-flight requests yet. The graph
549      * cannot change during this loop. */
550     while ((bs = bdrv_next_all_states(bs))) {
551         AioContext *aio_context = bdrv_get_aio_context(bs);
552 
553         aio_context_acquire(aio_context);
554         bdrv_do_drained_begin(bs, false, NULL, true, false);
555         aio_context_release(aio_context);
556     }
557 
558     /* Now poll the in-flight requests */
559     AIO_WAIT_WHILE(NULL, bdrv_drain_all_poll());
560 
561     while ((bs = bdrv_next_all_states(bs))) {
562         bdrv_drain_assert_idle(bs);
563     }
564 }
565 
566 void bdrv_drain_all_end(void)
567 {
568     BlockDriverState *bs = NULL;
569 
570     while ((bs = bdrv_next_all_states(bs))) {
571         AioContext *aio_context = bdrv_get_aio_context(bs);
572 
573         aio_context_acquire(aio_context);
574         bdrv_do_drained_end(bs, false, NULL, true);
575         aio_context_release(aio_context);
576     }
577 
578     assert(bdrv_drain_all_count > 0);
579     bdrv_drain_all_count--;
580 }
581 
582 void bdrv_drain_all(void)
583 {
584     bdrv_drain_all_begin();
585     bdrv_drain_all_end();
586 }
587 
588 /**
589  * Remove an active request from the tracked requests list
590  *
591  * This function should be called when a tracked request is completing.
592  */
593 static void tracked_request_end(BdrvTrackedRequest *req)
594 {
595     if (req->serialising) {
596         atomic_dec(&req->bs->serialising_in_flight);
597     }
598 
599     qemu_co_mutex_lock(&req->bs->reqs_lock);
600     QLIST_REMOVE(req, list);
601     qemu_co_queue_restart_all(&req->wait_queue);
602     qemu_co_mutex_unlock(&req->bs->reqs_lock);
603 }
604 
605 /**
606  * Add an active request to the tracked requests list
607  */
608 static void tracked_request_begin(BdrvTrackedRequest *req,
609                                   BlockDriverState *bs,
610                                   int64_t offset,
611                                   uint64_t bytes,
612                                   enum BdrvTrackedRequestType type)
613 {
614     assert(bytes <= INT64_MAX && offset <= INT64_MAX - bytes);
615 
616     *req = (BdrvTrackedRequest){
617         .bs = bs,
618         .offset         = offset,
619         .bytes          = bytes,
620         .type           = type,
621         .co             = qemu_coroutine_self(),
622         .serialising    = false,
623         .overlap_offset = offset,
624         .overlap_bytes  = bytes,
625     };
626 
627     qemu_co_queue_init(&req->wait_queue);
628 
629     qemu_co_mutex_lock(&bs->reqs_lock);
630     QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
631     qemu_co_mutex_unlock(&bs->reqs_lock);
632 }
633 
634 static void mark_request_serialising(BdrvTrackedRequest *req, uint64_t align)
635 {
636     int64_t overlap_offset = req->offset & ~(align - 1);
637     uint64_t overlap_bytes = ROUND_UP(req->offset + req->bytes, align)
638                                - overlap_offset;
639 
640     if (!req->serialising) {
641         atomic_inc(&req->bs->serialising_in_flight);
642         req->serialising = true;
643     }
644 
645     req->overlap_offset = MIN(req->overlap_offset, overlap_offset);
646     req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes);
647 }
648 
649 static bool is_request_serialising_and_aligned(BdrvTrackedRequest *req)
650 {
651     /*
652      * If the request is serialising, overlap_offset and overlap_bytes are set,
653      * so we can check if the request is aligned. Otherwise, don't care and
654      * return false.
655      */
656 
657     return req->serialising && (req->offset == req->overlap_offset) &&
658            (req->bytes == req->overlap_bytes);
659 }
660 
661 /**
662  * Round a region to cluster boundaries
663  */
664 void bdrv_round_to_clusters(BlockDriverState *bs,
665                             int64_t offset, int64_t bytes,
666                             int64_t *cluster_offset,
667                             int64_t *cluster_bytes)
668 {
669     BlockDriverInfo bdi;
670 
671     if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
672         *cluster_offset = offset;
673         *cluster_bytes = bytes;
674     } else {
675         int64_t c = bdi.cluster_size;
676         *cluster_offset = QEMU_ALIGN_DOWN(offset, c);
677         *cluster_bytes = QEMU_ALIGN_UP(offset - *cluster_offset + bytes, c);
678     }
679 }
680 
681 static int bdrv_get_cluster_size(BlockDriverState *bs)
682 {
683     BlockDriverInfo bdi;
684     int ret;
685 
686     ret = bdrv_get_info(bs, &bdi);
687     if (ret < 0 || bdi.cluster_size == 0) {
688         return bs->bl.request_alignment;
689     } else {
690         return bdi.cluster_size;
691     }
692 }
693 
694 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
695                                      int64_t offset, uint64_t bytes)
696 {
697     /*        aaaa   bbbb */
698     if (offset >= req->overlap_offset + req->overlap_bytes) {
699         return false;
700     }
701     /* bbbb   aaaa        */
702     if (req->overlap_offset >= offset + bytes) {
703         return false;
704     }
705     return true;
706 }
707 
708 void bdrv_inc_in_flight(BlockDriverState *bs)
709 {
710     atomic_inc(&bs->in_flight);
711 }
712 
713 void bdrv_wakeup(BlockDriverState *bs)
714 {
715     aio_wait_kick();
716 }
717 
718 void bdrv_dec_in_flight(BlockDriverState *bs)
719 {
720     atomic_dec(&bs->in_flight);
721     bdrv_wakeup(bs);
722 }
723 
724 static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
725 {
726     BlockDriverState *bs = self->bs;
727     BdrvTrackedRequest *req;
728     bool retry;
729     bool waited = false;
730 
731     if (!atomic_read(&bs->serialising_in_flight)) {
732         return false;
733     }
734 
735     do {
736         retry = false;
737         qemu_co_mutex_lock(&bs->reqs_lock);
738         QLIST_FOREACH(req, &bs->tracked_requests, list) {
739             if (req == self || (!req->serialising && !self->serialising)) {
740                 continue;
741             }
742             if (tracked_request_overlaps(req, self->overlap_offset,
743                                          self->overlap_bytes))
744             {
745                 /* Hitting this means there was a reentrant request, for
746                  * example, a block driver issuing nested requests.  This must
747                  * never happen since it means deadlock.
748                  */
749                 assert(qemu_coroutine_self() != req->co);
750 
751                 /* If the request is already (indirectly) waiting for us, or
752                  * will wait for us as soon as it wakes up, then just go on
753                  * (instead of producing a deadlock in the former case). */
754                 if (!req->waiting_for) {
755                     self->waiting_for = req;
756                     qemu_co_queue_wait(&req->wait_queue, &bs->reqs_lock);
757                     self->waiting_for = NULL;
758                     retry = true;
759                     waited = true;
760                     break;
761                 }
762             }
763         }
764         qemu_co_mutex_unlock(&bs->reqs_lock);
765     } while (retry);
766 
767     return waited;
768 }
769 
770 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
771                                    size_t size)
772 {
773     if (size > BDRV_REQUEST_MAX_BYTES) {
774         return -EIO;
775     }
776 
777     if (!bdrv_is_inserted(bs)) {
778         return -ENOMEDIUM;
779     }
780 
781     if (offset < 0) {
782         return -EIO;
783     }
784 
785     return 0;
786 }
787 
788 typedef struct RwCo {
789     BdrvChild *child;
790     int64_t offset;
791     QEMUIOVector *qiov;
792     bool is_write;
793     int ret;
794     BdrvRequestFlags flags;
795 } RwCo;
796 
797 static void coroutine_fn bdrv_rw_co_entry(void *opaque)
798 {
799     RwCo *rwco = opaque;
800 
801     if (!rwco->is_write) {
802         rwco->ret = bdrv_co_preadv(rwco->child, rwco->offset,
803                                    rwco->qiov->size, rwco->qiov,
804                                    rwco->flags);
805     } else {
806         rwco->ret = bdrv_co_pwritev(rwco->child, rwco->offset,
807                                     rwco->qiov->size, rwco->qiov,
808                                     rwco->flags);
809     }
810     aio_wait_kick();
811 }
812 
813 /*
814  * Process a vectored synchronous request using coroutines
815  */
816 static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
817                         QEMUIOVector *qiov, bool is_write,
818                         BdrvRequestFlags flags)
819 {
820     Coroutine *co;
821     RwCo rwco = {
822         .child = child,
823         .offset = offset,
824         .qiov = qiov,
825         .is_write = is_write,
826         .ret = NOT_DONE,
827         .flags = flags,
828     };
829 
830     if (qemu_in_coroutine()) {
831         /* Fast-path if already in coroutine context */
832         bdrv_rw_co_entry(&rwco);
833     } else {
834         co = qemu_coroutine_create(bdrv_rw_co_entry, &rwco);
835         bdrv_coroutine_enter(child->bs, co);
836         BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE);
837     }
838     return rwco.ret;
839 }
840 
841 int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
842                        int bytes, BdrvRequestFlags flags)
843 {
844     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
845 
846     return bdrv_prwv_co(child, offset, &qiov, true,
847                         BDRV_REQ_ZERO_WRITE | flags);
848 }
849 
850 /*
851  * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
852  * The operation is sped up by checking the block status and only writing
853  * zeroes to the device if they currently do not return zeroes. Optional
854  * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
855  * BDRV_REQ_FUA).
856  *
857  * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
858  */
859 int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
860 {
861     int ret;
862     int64_t target_size, bytes, offset = 0;
863     BlockDriverState *bs = child->bs;
864 
865     target_size = bdrv_getlength(bs);
866     if (target_size < 0) {
867         return target_size;
868     }
869 
870     for (;;) {
871         bytes = MIN(target_size - offset, BDRV_REQUEST_MAX_BYTES);
872         if (bytes <= 0) {
873             return 0;
874         }
875         ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
876         if (ret < 0) {
877             return ret;
878         }
879         if (ret & BDRV_BLOCK_ZERO) {
880             offset += bytes;
881             continue;
882         }
883         ret = bdrv_pwrite_zeroes(child, offset, bytes, flags);
884         if (ret < 0) {
885             return ret;
886         }
887         offset += bytes;
888     }
889 }
890 
891 int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
892 {
893     int ret;
894 
895     ret = bdrv_prwv_co(child, offset, qiov, false, 0);
896     if (ret < 0) {
897         return ret;
898     }
899 
900     return qiov->size;
901 }
902 
903 /* See bdrv_pwrite() for the return codes */
904 int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes)
905 {
906     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
907 
908     if (bytes < 0) {
909         return -EINVAL;
910     }
911 
912     return bdrv_preadv(child, offset, &qiov);
913 }
914 
915 int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
916 {
917     int ret;
918 
919     ret = bdrv_prwv_co(child, offset, qiov, true, 0);
920     if (ret < 0) {
921         return ret;
922     }
923 
924     return qiov->size;
925 }
926 
927 /* Return no. of bytes on success or < 0 on error. Important errors are:
928   -EIO         generic I/O error (may happen for all errors)
929   -ENOMEDIUM   No media inserted.
930   -EINVAL      Invalid offset or number of bytes
931   -EACCES      Trying to write a read-only device
932 */
933 int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes)
934 {
935     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
936 
937     if (bytes < 0) {
938         return -EINVAL;
939     }
940 
941     return bdrv_pwritev(child, offset, &qiov);
942 }
943 
944 /*
945  * Writes to the file and ensures that no writes are reordered across this
946  * request (acts as a barrier)
947  *
948  * Returns 0 on success, -errno in error cases.
949  */
950 int bdrv_pwrite_sync(BdrvChild *child, int64_t offset,
951                      const void *buf, int count)
952 {
953     int ret;
954 
955     ret = bdrv_pwrite(child, offset, buf, count);
956     if (ret < 0) {
957         return ret;
958     }
959 
960     ret = bdrv_flush(child->bs);
961     if (ret < 0) {
962         return ret;
963     }
964 
965     return 0;
966 }
967 
968 typedef struct CoroutineIOCompletion {
969     Coroutine *coroutine;
970     int ret;
971 } CoroutineIOCompletion;
972 
973 static void bdrv_co_io_em_complete(void *opaque, int ret)
974 {
975     CoroutineIOCompletion *co = opaque;
976 
977     co->ret = ret;
978     aio_co_wake(co->coroutine);
979 }
980 
981 static int coroutine_fn bdrv_driver_preadv(BlockDriverState *bs,
982                                            uint64_t offset, uint64_t bytes,
983                                            QEMUIOVector *qiov, int flags)
984 {
985     BlockDriver *drv = bs->drv;
986     int64_t sector_num;
987     unsigned int nb_sectors;
988 
989     assert(!(flags & ~BDRV_REQ_MASK));
990     assert(!(flags & BDRV_REQ_NO_FALLBACK));
991 
992     if (!drv) {
993         return -ENOMEDIUM;
994     }
995 
996     if (drv->bdrv_co_preadv) {
997         return drv->bdrv_co_preadv(bs, offset, bytes, qiov, flags);
998     }
999 
1000     if (drv->bdrv_aio_preadv) {
1001         BlockAIOCB *acb;
1002         CoroutineIOCompletion co = {
1003             .coroutine = qemu_coroutine_self(),
1004         };
1005 
1006         acb = drv->bdrv_aio_preadv(bs, offset, bytes, qiov, flags,
1007                                    bdrv_co_io_em_complete, &co);
1008         if (acb == NULL) {
1009             return -EIO;
1010         } else {
1011             qemu_coroutine_yield();
1012             return co.ret;
1013         }
1014     }
1015 
1016     sector_num = offset >> BDRV_SECTOR_BITS;
1017     nb_sectors = bytes >> BDRV_SECTOR_BITS;
1018 
1019     assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1020     assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1021     assert(bytes <= BDRV_REQUEST_MAX_BYTES);
1022     assert(drv->bdrv_co_readv);
1023 
1024     return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1025 }
1026 
1027 static int coroutine_fn bdrv_driver_pwritev(BlockDriverState *bs,
1028                                             uint64_t offset, uint64_t bytes,
1029                                             QEMUIOVector *qiov, int flags)
1030 {
1031     BlockDriver *drv = bs->drv;
1032     int64_t sector_num;
1033     unsigned int nb_sectors;
1034     int ret;
1035 
1036     assert(!(flags & ~BDRV_REQ_MASK));
1037     assert(!(flags & BDRV_REQ_NO_FALLBACK));
1038 
1039     if (!drv) {
1040         return -ENOMEDIUM;
1041     }
1042 
1043     if (drv->bdrv_co_pwritev) {
1044         ret = drv->bdrv_co_pwritev(bs, offset, bytes, qiov,
1045                                    flags & bs->supported_write_flags);
1046         flags &= ~bs->supported_write_flags;
1047         goto emulate_flags;
1048     }
1049 
1050     if (drv->bdrv_aio_pwritev) {
1051         BlockAIOCB *acb;
1052         CoroutineIOCompletion co = {
1053             .coroutine = qemu_coroutine_self(),
1054         };
1055 
1056         acb = drv->bdrv_aio_pwritev(bs, offset, bytes, qiov,
1057                                     flags & bs->supported_write_flags,
1058                                     bdrv_co_io_em_complete, &co);
1059         flags &= ~bs->supported_write_flags;
1060         if (acb == NULL) {
1061             ret = -EIO;
1062         } else {
1063             qemu_coroutine_yield();
1064             ret = co.ret;
1065         }
1066         goto emulate_flags;
1067     }
1068 
1069     sector_num = offset >> BDRV_SECTOR_BITS;
1070     nb_sectors = bytes >> BDRV_SECTOR_BITS;
1071 
1072     assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1073     assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1074     assert(bytes <= BDRV_REQUEST_MAX_BYTES);
1075 
1076     assert(drv->bdrv_co_writev);
1077     ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov,
1078                               flags & bs->supported_write_flags);
1079     flags &= ~bs->supported_write_flags;
1080 
1081 emulate_flags:
1082     if (ret == 0 && (flags & BDRV_REQ_FUA)) {
1083         ret = bdrv_co_flush(bs);
1084     }
1085 
1086     return ret;
1087 }
1088 
1089 static int coroutine_fn
1090 bdrv_driver_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
1091                                uint64_t bytes, QEMUIOVector *qiov)
1092 {
1093     BlockDriver *drv = bs->drv;
1094 
1095     if (!drv) {
1096         return -ENOMEDIUM;
1097     }
1098 
1099     if (!drv->bdrv_co_pwritev_compressed) {
1100         return -ENOTSUP;
1101     }
1102 
1103     return drv->bdrv_co_pwritev_compressed(bs, offset, bytes, qiov);
1104 }
1105 
1106 static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child,
1107         int64_t offset, unsigned int bytes, QEMUIOVector *qiov)
1108 {
1109     BlockDriverState *bs = child->bs;
1110 
1111     /* Perform I/O through a temporary buffer so that users who scribble over
1112      * their read buffer while the operation is in progress do not end up
1113      * modifying the image file.  This is critical for zero-copy guest I/O
1114      * where anything might happen inside guest memory.
1115      */
1116     void *bounce_buffer;
1117 
1118     BlockDriver *drv = bs->drv;
1119     QEMUIOVector local_qiov;
1120     int64_t cluster_offset;
1121     int64_t cluster_bytes;
1122     size_t skip_bytes;
1123     int ret;
1124     int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer,
1125                                     BDRV_REQUEST_MAX_BYTES);
1126     unsigned int progress = 0;
1127 
1128     if (!drv) {
1129         return -ENOMEDIUM;
1130     }
1131 
1132     /* FIXME We cannot require callers to have write permissions when all they
1133      * are doing is a read request. If we did things right, write permissions
1134      * would be obtained anyway, but internally by the copy-on-read code. As
1135      * long as it is implemented here rather than in a separate filter driver,
1136      * the copy-on-read code doesn't have its own BdrvChild, however, for which
1137      * it could request permissions. Therefore we have to bypass the permission
1138      * system for the moment. */
1139     // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1140 
1141     /* Cover entire cluster so no additional backing file I/O is required when
1142      * allocating cluster in the image file.  Note that this value may exceed
1143      * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1144      * is one reason we loop rather than doing it all at once.
1145      */
1146     bdrv_round_to_clusters(bs, offset, bytes, &cluster_offset, &cluster_bytes);
1147     skip_bytes = offset - cluster_offset;
1148 
1149     trace_bdrv_co_do_copy_on_readv(bs, offset, bytes,
1150                                    cluster_offset, cluster_bytes);
1151 
1152     bounce_buffer = qemu_try_blockalign(bs,
1153                                         MIN(MIN(max_transfer, cluster_bytes),
1154                                             MAX_BOUNCE_BUFFER));
1155     if (bounce_buffer == NULL) {
1156         ret = -ENOMEM;
1157         goto err;
1158     }
1159 
1160     while (cluster_bytes) {
1161         int64_t pnum;
1162 
1163         ret = bdrv_is_allocated(bs, cluster_offset,
1164                                 MIN(cluster_bytes, max_transfer), &pnum);
1165         if (ret < 0) {
1166             /* Safe to treat errors in querying allocation as if
1167              * unallocated; we'll probably fail again soon on the
1168              * read, but at least that will set a decent errno.
1169              */
1170             pnum = MIN(cluster_bytes, max_transfer);
1171         }
1172 
1173         /* Stop at EOF if the image ends in the middle of the cluster */
1174         if (ret == 0 && pnum == 0) {
1175             assert(progress >= bytes);
1176             break;
1177         }
1178 
1179         assert(skip_bytes < pnum);
1180 
1181         if (ret <= 0) {
1182             /* Must copy-on-read; use the bounce buffer */
1183             pnum = MIN(pnum, MAX_BOUNCE_BUFFER);
1184             qemu_iovec_init_buf(&local_qiov, bounce_buffer, pnum);
1185 
1186             ret = bdrv_driver_preadv(bs, cluster_offset, pnum,
1187                                      &local_qiov, 0);
1188             if (ret < 0) {
1189                 goto err;
1190             }
1191 
1192             bdrv_debug_event(bs, BLKDBG_COR_WRITE);
1193             if (drv->bdrv_co_pwrite_zeroes &&
1194                 buffer_is_zero(bounce_buffer, pnum)) {
1195                 /* FIXME: Should we (perhaps conditionally) be setting
1196                  * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1197                  * that still correctly reads as zero? */
1198                 ret = bdrv_co_do_pwrite_zeroes(bs, cluster_offset, pnum,
1199                                                BDRV_REQ_WRITE_UNCHANGED);
1200             } else {
1201                 /* This does not change the data on the disk, it is not
1202                  * necessary to flush even in cache=writethrough mode.
1203                  */
1204                 ret = bdrv_driver_pwritev(bs, cluster_offset, pnum,
1205                                           &local_qiov,
1206                                           BDRV_REQ_WRITE_UNCHANGED);
1207             }
1208 
1209             if (ret < 0) {
1210                 /* It might be okay to ignore write errors for guest
1211                  * requests.  If this is a deliberate copy-on-read
1212                  * then we don't want to ignore the error.  Simply
1213                  * report it in all cases.
1214                  */
1215                 goto err;
1216             }
1217 
1218             qemu_iovec_from_buf(qiov, progress, bounce_buffer + skip_bytes,
1219                                 pnum - skip_bytes);
1220         } else {
1221             /* Read directly into the destination */
1222             qemu_iovec_init(&local_qiov, qiov->niov);
1223             qemu_iovec_concat(&local_qiov, qiov, progress, pnum - skip_bytes);
1224             ret = bdrv_driver_preadv(bs, offset + progress, local_qiov.size,
1225                                      &local_qiov, 0);
1226             qemu_iovec_destroy(&local_qiov);
1227             if (ret < 0) {
1228                 goto err;
1229             }
1230         }
1231 
1232         cluster_offset += pnum;
1233         cluster_bytes -= pnum;
1234         progress += pnum - skip_bytes;
1235         skip_bytes = 0;
1236     }
1237     ret = 0;
1238 
1239 err:
1240     qemu_vfree(bounce_buffer);
1241     return ret;
1242 }
1243 
1244 /*
1245  * Forwards an already correctly aligned request to the BlockDriver. This
1246  * handles copy on read, zeroing after EOF, and fragmentation of large
1247  * reads; any other features must be implemented by the caller.
1248  */
1249 static int coroutine_fn bdrv_aligned_preadv(BdrvChild *child,
1250     BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
1251     int64_t align, QEMUIOVector *qiov, int flags)
1252 {
1253     BlockDriverState *bs = child->bs;
1254     int64_t total_bytes, max_bytes;
1255     int ret = 0;
1256     uint64_t bytes_remaining = bytes;
1257     int max_transfer;
1258 
1259     assert(is_power_of_2(align));
1260     assert((offset & (align - 1)) == 0);
1261     assert((bytes & (align - 1)) == 0);
1262     assert(!qiov || bytes == qiov->size);
1263     assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1264     max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
1265                                    align);
1266 
1267     /* TODO: We would need a per-BDS .supported_read_flags and
1268      * potential fallback support, if we ever implement any read flags
1269      * to pass through to drivers.  For now, there aren't any
1270      * passthrough flags.  */
1271     assert(!(flags & ~(BDRV_REQ_NO_SERIALISING | BDRV_REQ_COPY_ON_READ)));
1272 
1273     /* Handle Copy on Read and associated serialisation */
1274     if (flags & BDRV_REQ_COPY_ON_READ) {
1275         /* If we touch the same cluster it counts as an overlap.  This
1276          * guarantees that allocating writes will be serialized and not race
1277          * with each other for the same cluster.  For example, in copy-on-read
1278          * it ensures that the CoR read and write operations are atomic and
1279          * guest writes cannot interleave between them. */
1280         mark_request_serialising(req, bdrv_get_cluster_size(bs));
1281     }
1282 
1283     /* BDRV_REQ_SERIALISING is only for write operation */
1284     assert(!(flags & BDRV_REQ_SERIALISING));
1285 
1286     if (!(flags & BDRV_REQ_NO_SERIALISING)) {
1287         wait_serialising_requests(req);
1288     }
1289 
1290     if (flags & BDRV_REQ_COPY_ON_READ) {
1291         int64_t pnum;
1292 
1293         ret = bdrv_is_allocated(bs, offset, bytes, &pnum);
1294         if (ret < 0) {
1295             goto out;
1296         }
1297 
1298         if (!ret || pnum != bytes) {
1299             ret = bdrv_co_do_copy_on_readv(child, offset, bytes, qiov);
1300             goto out;
1301         }
1302     }
1303 
1304     /* Forward the request to the BlockDriver, possibly fragmenting it */
1305     total_bytes = bdrv_getlength(bs);
1306     if (total_bytes < 0) {
1307         ret = total_bytes;
1308         goto out;
1309     }
1310 
1311     max_bytes = ROUND_UP(MAX(0, total_bytes - offset), align);
1312     if (bytes <= max_bytes && bytes <= max_transfer) {
1313         ret = bdrv_driver_preadv(bs, offset, bytes, qiov, 0);
1314         goto out;
1315     }
1316 
1317     while (bytes_remaining) {
1318         int num;
1319 
1320         if (max_bytes) {
1321             QEMUIOVector local_qiov;
1322 
1323             num = MIN(bytes_remaining, MIN(max_bytes, max_transfer));
1324             assert(num);
1325             qemu_iovec_init(&local_qiov, qiov->niov);
1326             qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num);
1327 
1328             ret = bdrv_driver_preadv(bs, offset + bytes - bytes_remaining,
1329                                      num, &local_qiov, 0);
1330             max_bytes -= num;
1331             qemu_iovec_destroy(&local_qiov);
1332         } else {
1333             num = bytes_remaining;
1334             ret = qemu_iovec_memset(qiov, bytes - bytes_remaining, 0,
1335                                     bytes_remaining);
1336         }
1337         if (ret < 0) {
1338             goto out;
1339         }
1340         bytes_remaining -= num;
1341     }
1342 
1343 out:
1344     return ret < 0 ? ret : 0;
1345 }
1346 
1347 /*
1348  * Handle a read request in coroutine context
1349  */
1350 int coroutine_fn bdrv_co_preadv(BdrvChild *child,
1351     int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
1352     BdrvRequestFlags flags)
1353 {
1354     BlockDriverState *bs = child->bs;
1355     BlockDriver *drv = bs->drv;
1356     BdrvTrackedRequest req;
1357 
1358     uint64_t align = bs->bl.request_alignment;
1359     uint8_t *head_buf = NULL;
1360     uint8_t *tail_buf = NULL;
1361     QEMUIOVector local_qiov;
1362     bool use_local_qiov = false;
1363     int ret;
1364 
1365     trace_bdrv_co_preadv(child->bs, offset, bytes, flags);
1366 
1367     if (!drv) {
1368         return -ENOMEDIUM;
1369     }
1370 
1371     ret = bdrv_check_byte_request(bs, offset, bytes);
1372     if (ret < 0) {
1373         return ret;
1374     }
1375 
1376     bdrv_inc_in_flight(bs);
1377 
1378     /* Don't do copy-on-read if we read data before write operation */
1379     if (atomic_read(&bs->copy_on_read) && !(flags & BDRV_REQ_NO_SERIALISING)) {
1380         flags |= BDRV_REQ_COPY_ON_READ;
1381     }
1382 
1383     /* Align read if necessary by padding qiov */
1384     if (offset & (align - 1)) {
1385         head_buf = qemu_blockalign(bs, align);
1386         qemu_iovec_init(&local_qiov, qiov->niov + 2);
1387         qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
1388         qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1389         use_local_qiov = true;
1390 
1391         bytes += offset & (align - 1);
1392         offset = offset & ~(align - 1);
1393     }
1394 
1395     if ((offset + bytes) & (align - 1)) {
1396         if (!use_local_qiov) {
1397             qemu_iovec_init(&local_qiov, qiov->niov + 1);
1398             qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1399             use_local_qiov = true;
1400         }
1401         tail_buf = qemu_blockalign(bs, align);
1402         qemu_iovec_add(&local_qiov, tail_buf,
1403                        align - ((offset + bytes) & (align - 1)));
1404 
1405         bytes = ROUND_UP(bytes, align);
1406     }
1407 
1408     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_READ);
1409     ret = bdrv_aligned_preadv(child, &req, offset, bytes, align,
1410                               use_local_qiov ? &local_qiov : qiov,
1411                               flags);
1412     tracked_request_end(&req);
1413     bdrv_dec_in_flight(bs);
1414 
1415     if (use_local_qiov) {
1416         qemu_iovec_destroy(&local_qiov);
1417         qemu_vfree(head_buf);
1418         qemu_vfree(tail_buf);
1419     }
1420 
1421     return ret;
1422 }
1423 
1424 static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
1425     int64_t offset, int bytes, BdrvRequestFlags flags)
1426 {
1427     BlockDriver *drv = bs->drv;
1428     QEMUIOVector qiov;
1429     void *buf = NULL;
1430     int ret = 0;
1431     bool need_flush = false;
1432     int head = 0;
1433     int tail = 0;
1434 
1435     int max_write_zeroes = MIN_NON_ZERO(bs->bl.max_pwrite_zeroes, INT_MAX);
1436     int alignment = MAX(bs->bl.pwrite_zeroes_alignment,
1437                         bs->bl.request_alignment);
1438     int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer, MAX_BOUNCE_BUFFER);
1439 
1440     if (!drv) {
1441         return -ENOMEDIUM;
1442     }
1443 
1444     if ((flags & ~bs->supported_zero_flags) & BDRV_REQ_NO_FALLBACK) {
1445         return -ENOTSUP;
1446     }
1447 
1448     assert(alignment % bs->bl.request_alignment == 0);
1449     head = offset % alignment;
1450     tail = (offset + bytes) % alignment;
1451     max_write_zeroes = QEMU_ALIGN_DOWN(max_write_zeroes, alignment);
1452     assert(max_write_zeroes >= bs->bl.request_alignment);
1453 
1454     while (bytes > 0 && !ret) {
1455         int num = bytes;
1456 
1457         /* Align request.  Block drivers can expect the "bulk" of the request
1458          * to be aligned, and that unaligned requests do not cross cluster
1459          * boundaries.
1460          */
1461         if (head) {
1462             /* Make a small request up to the first aligned sector. For
1463              * convenience, limit this request to max_transfer even if
1464              * we don't need to fall back to writes.  */
1465             num = MIN(MIN(bytes, max_transfer), alignment - head);
1466             head = (head + num) % alignment;
1467             assert(num < max_write_zeroes);
1468         } else if (tail && num > alignment) {
1469             /* Shorten the request to the last aligned sector.  */
1470             num -= tail;
1471         }
1472 
1473         /* limit request size */
1474         if (num > max_write_zeroes) {
1475             num = max_write_zeroes;
1476         }
1477 
1478         ret = -ENOTSUP;
1479         /* First try the efficient write zeroes operation */
1480         if (drv->bdrv_co_pwrite_zeroes) {
1481             ret = drv->bdrv_co_pwrite_zeroes(bs, offset, num,
1482                                              flags & bs->supported_zero_flags);
1483             if (ret != -ENOTSUP && (flags & BDRV_REQ_FUA) &&
1484                 !(bs->supported_zero_flags & BDRV_REQ_FUA)) {
1485                 need_flush = true;
1486             }
1487         } else {
1488             assert(!bs->supported_zero_flags);
1489         }
1490 
1491         if (ret < 0 && !(flags & BDRV_REQ_NO_FALLBACK)) {
1492             /* Fall back to bounce buffer if write zeroes is unsupported */
1493             BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE;
1494 
1495             if ((flags & BDRV_REQ_FUA) &&
1496                 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
1497                 /* No need for bdrv_driver_pwrite() to do a fallback
1498                  * flush on each chunk; use just one at the end */
1499                 write_flags &= ~BDRV_REQ_FUA;
1500                 need_flush = true;
1501             }
1502             num = MIN(num, max_transfer);
1503             if (buf == NULL) {
1504                 buf = qemu_try_blockalign0(bs, num);
1505                 if (buf == NULL) {
1506                     ret = -ENOMEM;
1507                     goto fail;
1508                 }
1509             }
1510             qemu_iovec_init_buf(&qiov, buf, num);
1511 
1512             ret = bdrv_driver_pwritev(bs, offset, num, &qiov, write_flags);
1513 
1514             /* Keep bounce buffer around if it is big enough for all
1515              * all future requests.
1516              */
1517             if (num < max_transfer) {
1518                 qemu_vfree(buf);
1519                 buf = NULL;
1520             }
1521         }
1522 
1523         offset += num;
1524         bytes -= num;
1525     }
1526 
1527 fail:
1528     if (ret == 0 && need_flush) {
1529         ret = bdrv_co_flush(bs);
1530     }
1531     qemu_vfree(buf);
1532     return ret;
1533 }
1534 
1535 static inline int coroutine_fn
1536 bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes,
1537                           BdrvTrackedRequest *req, int flags)
1538 {
1539     BlockDriverState *bs = child->bs;
1540     bool waited;
1541     int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
1542 
1543     if (bs->read_only) {
1544         return -EPERM;
1545     }
1546 
1547     /* BDRV_REQ_NO_SERIALISING is only for read operation */
1548     assert(!(flags & BDRV_REQ_NO_SERIALISING));
1549     assert(!(bs->open_flags & BDRV_O_INACTIVE));
1550     assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1551     assert(!(flags & ~BDRV_REQ_MASK));
1552 
1553     if (flags & BDRV_REQ_SERIALISING) {
1554         mark_request_serialising(req, bdrv_get_cluster_size(bs));
1555     }
1556 
1557     waited = wait_serialising_requests(req);
1558 
1559     assert(!waited || !req->serialising ||
1560            is_request_serialising_and_aligned(req));
1561     assert(req->overlap_offset <= offset);
1562     assert(offset + bytes <= req->overlap_offset + req->overlap_bytes);
1563     assert(end_sector <= bs->total_sectors || child->perm & BLK_PERM_RESIZE);
1564 
1565     switch (req->type) {
1566     case BDRV_TRACKED_WRITE:
1567     case BDRV_TRACKED_DISCARD:
1568         if (flags & BDRV_REQ_WRITE_UNCHANGED) {
1569             assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1570         } else {
1571             assert(child->perm & BLK_PERM_WRITE);
1572         }
1573         return notifier_with_return_list_notify(&bs->before_write_notifiers,
1574                                                 req);
1575     case BDRV_TRACKED_TRUNCATE:
1576         assert(child->perm & BLK_PERM_RESIZE);
1577         return 0;
1578     default:
1579         abort();
1580     }
1581 }
1582 
1583 static inline void coroutine_fn
1584 bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, uint64_t bytes,
1585                          BdrvTrackedRequest *req, int ret)
1586 {
1587     int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
1588     BlockDriverState *bs = child->bs;
1589 
1590     atomic_inc(&bs->write_gen);
1591 
1592     /*
1593      * Discard cannot extend the image, but in error handling cases, such as
1594      * when reverting a qcow2 cluster allocation, the discarded range can pass
1595      * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
1596      * here. Instead, just skip it, since semantically a discard request
1597      * beyond EOF cannot expand the image anyway.
1598      */
1599     if (ret == 0 &&
1600         (req->type == BDRV_TRACKED_TRUNCATE ||
1601          end_sector > bs->total_sectors) &&
1602         req->type != BDRV_TRACKED_DISCARD) {
1603         bs->total_sectors = end_sector;
1604         bdrv_parent_cb_resize(bs);
1605         bdrv_dirty_bitmap_truncate(bs, end_sector << BDRV_SECTOR_BITS);
1606     }
1607     if (req->bytes) {
1608         switch (req->type) {
1609         case BDRV_TRACKED_WRITE:
1610             stat64_max(&bs->wr_highest_offset, offset + bytes);
1611             /* fall through, to set dirty bits */
1612         case BDRV_TRACKED_DISCARD:
1613             bdrv_set_dirty(bs, offset, bytes);
1614             break;
1615         default:
1616             break;
1617         }
1618     }
1619 }
1620 
1621 /*
1622  * Forwards an already correctly aligned write request to the BlockDriver,
1623  * after possibly fragmenting it.
1624  */
1625 static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child,
1626     BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
1627     int64_t align, QEMUIOVector *qiov, int flags)
1628 {
1629     BlockDriverState *bs = child->bs;
1630     BlockDriver *drv = bs->drv;
1631     int ret;
1632 
1633     uint64_t bytes_remaining = bytes;
1634     int max_transfer;
1635 
1636     if (!drv) {
1637         return -ENOMEDIUM;
1638     }
1639 
1640     if (bdrv_has_readonly_bitmaps(bs)) {
1641         return -EPERM;
1642     }
1643 
1644     assert(is_power_of_2(align));
1645     assert((offset & (align - 1)) == 0);
1646     assert((bytes & (align - 1)) == 0);
1647     assert(!qiov || bytes == qiov->size);
1648     max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
1649                                    align);
1650 
1651     ret = bdrv_co_write_req_prepare(child, offset, bytes, req, flags);
1652 
1653     if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF &&
1654         !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes &&
1655         qemu_iovec_is_zero(qiov)) {
1656         flags |= BDRV_REQ_ZERO_WRITE;
1657         if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) {
1658             flags |= BDRV_REQ_MAY_UNMAP;
1659         }
1660     }
1661 
1662     if (ret < 0) {
1663         /* Do nothing, write notifier decided to fail this request */
1664     } else if (flags & BDRV_REQ_ZERO_WRITE) {
1665         bdrv_debug_event(bs, BLKDBG_PWRITEV_ZERO);
1666         ret = bdrv_co_do_pwrite_zeroes(bs, offset, bytes, flags);
1667     } else if (flags & BDRV_REQ_WRITE_COMPRESSED) {
1668         ret = bdrv_driver_pwritev_compressed(bs, offset, bytes, qiov);
1669     } else if (bytes <= max_transfer) {
1670         bdrv_debug_event(bs, BLKDBG_PWRITEV);
1671         ret = bdrv_driver_pwritev(bs, offset, bytes, qiov, flags);
1672     } else {
1673         bdrv_debug_event(bs, BLKDBG_PWRITEV);
1674         while (bytes_remaining) {
1675             int num = MIN(bytes_remaining, max_transfer);
1676             QEMUIOVector local_qiov;
1677             int local_flags = flags;
1678 
1679             assert(num);
1680             if (num < bytes_remaining && (flags & BDRV_REQ_FUA) &&
1681                 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
1682                 /* If FUA is going to be emulated by flush, we only
1683                  * need to flush on the last iteration */
1684                 local_flags &= ~BDRV_REQ_FUA;
1685             }
1686             qemu_iovec_init(&local_qiov, qiov->niov);
1687             qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num);
1688 
1689             ret = bdrv_driver_pwritev(bs, offset + bytes - bytes_remaining,
1690                                       num, &local_qiov, local_flags);
1691             qemu_iovec_destroy(&local_qiov);
1692             if (ret < 0) {
1693                 break;
1694             }
1695             bytes_remaining -= num;
1696         }
1697     }
1698     bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
1699 
1700     if (ret >= 0) {
1701         ret = 0;
1702     }
1703     bdrv_co_write_req_finish(child, offset, bytes, req, ret);
1704 
1705     return ret;
1706 }
1707 
1708 static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvChild *child,
1709                                                 int64_t offset,
1710                                                 unsigned int bytes,
1711                                                 BdrvRequestFlags flags,
1712                                                 BdrvTrackedRequest *req)
1713 {
1714     BlockDriverState *bs = child->bs;
1715     uint8_t *buf = NULL;
1716     QEMUIOVector local_qiov;
1717     uint64_t align = bs->bl.request_alignment;
1718     unsigned int head_padding_bytes, tail_padding_bytes;
1719     int ret = 0;
1720 
1721     head_padding_bytes = offset & (align - 1);
1722     tail_padding_bytes = (align - (offset + bytes)) & (align - 1);
1723 
1724 
1725     assert(flags & BDRV_REQ_ZERO_WRITE);
1726     if (head_padding_bytes || tail_padding_bytes) {
1727         buf = qemu_blockalign(bs, align);
1728         qemu_iovec_init_buf(&local_qiov, buf, align);
1729     }
1730     if (head_padding_bytes) {
1731         uint64_t zero_bytes = MIN(bytes, align - head_padding_bytes);
1732 
1733         /* RMW the unaligned part before head. */
1734         mark_request_serialising(req, align);
1735         wait_serialising_requests(req);
1736         bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
1737         ret = bdrv_aligned_preadv(child, req, offset & ~(align - 1), align,
1738                                   align, &local_qiov, 0);
1739         if (ret < 0) {
1740             goto fail;
1741         }
1742         bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
1743 
1744         memset(buf + head_padding_bytes, 0, zero_bytes);
1745         ret = bdrv_aligned_pwritev(child, req, offset & ~(align - 1), align,
1746                                    align, &local_qiov,
1747                                    flags & ~BDRV_REQ_ZERO_WRITE);
1748         if (ret < 0) {
1749             goto fail;
1750         }
1751         offset += zero_bytes;
1752         bytes -= zero_bytes;
1753     }
1754 
1755     assert(!bytes || (offset & (align - 1)) == 0);
1756     if (bytes >= align) {
1757         /* Write the aligned part in the middle. */
1758         uint64_t aligned_bytes = bytes & ~(align - 1);
1759         ret = bdrv_aligned_pwritev(child, req, offset, aligned_bytes, align,
1760                                    NULL, flags);
1761         if (ret < 0) {
1762             goto fail;
1763         }
1764         bytes -= aligned_bytes;
1765         offset += aligned_bytes;
1766     }
1767 
1768     assert(!bytes || (offset & (align - 1)) == 0);
1769     if (bytes) {
1770         assert(align == tail_padding_bytes + bytes);
1771         /* RMW the unaligned part after tail. */
1772         mark_request_serialising(req, align);
1773         wait_serialising_requests(req);
1774         bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
1775         ret = bdrv_aligned_preadv(child, req, offset, align,
1776                                   align, &local_qiov, 0);
1777         if (ret < 0) {
1778             goto fail;
1779         }
1780         bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
1781 
1782         memset(buf, 0, bytes);
1783         ret = bdrv_aligned_pwritev(child, req, offset, align, align,
1784                                    &local_qiov, flags & ~BDRV_REQ_ZERO_WRITE);
1785     }
1786 fail:
1787     qemu_vfree(buf);
1788     return ret;
1789 
1790 }
1791 
1792 /*
1793  * Handle a write request in coroutine context
1794  */
1795 int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
1796     int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
1797     BdrvRequestFlags flags)
1798 {
1799     BlockDriverState *bs = child->bs;
1800     BdrvTrackedRequest req;
1801     uint64_t align = bs->bl.request_alignment;
1802     uint8_t *head_buf = NULL;
1803     uint8_t *tail_buf = NULL;
1804     QEMUIOVector local_qiov;
1805     bool use_local_qiov = false;
1806     int ret;
1807 
1808     trace_bdrv_co_pwritev(child->bs, offset, bytes, flags);
1809 
1810     if (!bs->drv) {
1811         return -ENOMEDIUM;
1812     }
1813 
1814     ret = bdrv_check_byte_request(bs, offset, bytes);
1815     if (ret < 0) {
1816         return ret;
1817     }
1818 
1819     bdrv_inc_in_flight(bs);
1820     /*
1821      * Align write if necessary by performing a read-modify-write cycle.
1822      * Pad qiov with the read parts and be sure to have a tracked request not
1823      * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
1824      */
1825     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
1826 
1827     if (flags & BDRV_REQ_ZERO_WRITE) {
1828         ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);
1829         goto out;
1830     }
1831 
1832     if (offset & (align - 1)) {
1833         QEMUIOVector head_qiov;
1834 
1835         mark_request_serialising(&req, align);
1836         wait_serialising_requests(&req);
1837 
1838         head_buf = qemu_blockalign(bs, align);
1839         qemu_iovec_init_buf(&head_qiov, head_buf, align);
1840 
1841         bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
1842         ret = bdrv_aligned_preadv(child, &req, offset & ~(align - 1), align,
1843                                   align, &head_qiov, 0);
1844         if (ret < 0) {
1845             goto fail;
1846         }
1847         bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
1848 
1849         qemu_iovec_init(&local_qiov, qiov->niov + 2);
1850         qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
1851         qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1852         use_local_qiov = true;
1853 
1854         bytes += offset & (align - 1);
1855         offset = offset & ~(align - 1);
1856 
1857         /* We have read the tail already if the request is smaller
1858          * than one aligned block.
1859          */
1860         if (bytes < align) {
1861             qemu_iovec_add(&local_qiov, head_buf + bytes, align - bytes);
1862             bytes = align;
1863         }
1864     }
1865 
1866     if ((offset + bytes) & (align - 1)) {
1867         QEMUIOVector tail_qiov;
1868         size_t tail_bytes;
1869         bool waited;
1870 
1871         mark_request_serialising(&req, align);
1872         waited = wait_serialising_requests(&req);
1873         assert(!waited || !use_local_qiov);
1874 
1875         tail_buf = qemu_blockalign(bs, align);
1876         qemu_iovec_init_buf(&tail_qiov, tail_buf, align);
1877 
1878         bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
1879         ret = bdrv_aligned_preadv(child, &req, (offset + bytes) & ~(align - 1),
1880                                   align, align, &tail_qiov, 0);
1881         if (ret < 0) {
1882             goto fail;
1883         }
1884         bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
1885 
1886         if (!use_local_qiov) {
1887             qemu_iovec_init(&local_qiov, qiov->niov + 1);
1888             qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1889             use_local_qiov = true;
1890         }
1891 
1892         tail_bytes = (offset + bytes) & (align - 1);
1893         qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, align - tail_bytes);
1894 
1895         bytes = ROUND_UP(bytes, align);
1896     }
1897 
1898     ret = bdrv_aligned_pwritev(child, &req, offset, bytes, align,
1899                                use_local_qiov ? &local_qiov : qiov,
1900                                flags);
1901 
1902 fail:
1903 
1904     if (use_local_qiov) {
1905         qemu_iovec_destroy(&local_qiov);
1906     }
1907     qemu_vfree(head_buf);
1908     qemu_vfree(tail_buf);
1909 out:
1910     tracked_request_end(&req);
1911     bdrv_dec_in_flight(bs);
1912     return ret;
1913 }
1914 
1915 int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset,
1916                                        int bytes, BdrvRequestFlags flags)
1917 {
1918     trace_bdrv_co_pwrite_zeroes(child->bs, offset, bytes, flags);
1919 
1920     if (!(child->bs->open_flags & BDRV_O_UNMAP)) {
1921         flags &= ~BDRV_REQ_MAY_UNMAP;
1922     }
1923 
1924     return bdrv_co_pwritev(child, offset, bytes, NULL,
1925                            BDRV_REQ_ZERO_WRITE | flags);
1926 }
1927 
1928 /*
1929  * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
1930  */
1931 int bdrv_flush_all(void)
1932 {
1933     BdrvNextIterator it;
1934     BlockDriverState *bs = NULL;
1935     int result = 0;
1936 
1937     for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1938         AioContext *aio_context = bdrv_get_aio_context(bs);
1939         int ret;
1940 
1941         aio_context_acquire(aio_context);
1942         ret = bdrv_flush(bs);
1943         if (ret < 0 && !result) {
1944             result = ret;
1945         }
1946         aio_context_release(aio_context);
1947     }
1948 
1949     return result;
1950 }
1951 
1952 
1953 typedef struct BdrvCoBlockStatusData {
1954     BlockDriverState *bs;
1955     BlockDriverState *base;
1956     bool want_zero;
1957     int64_t offset;
1958     int64_t bytes;
1959     int64_t *pnum;
1960     int64_t *map;
1961     BlockDriverState **file;
1962     int ret;
1963     bool done;
1964 } BdrvCoBlockStatusData;
1965 
1966 int coroutine_fn bdrv_co_block_status_from_file(BlockDriverState *bs,
1967                                                 bool want_zero,
1968                                                 int64_t offset,
1969                                                 int64_t bytes,
1970                                                 int64_t *pnum,
1971                                                 int64_t *map,
1972                                                 BlockDriverState **file)
1973 {
1974     assert(bs->file && bs->file->bs);
1975     *pnum = bytes;
1976     *map = offset;
1977     *file = bs->file->bs;
1978     return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
1979 }
1980 
1981 int coroutine_fn bdrv_co_block_status_from_backing(BlockDriverState *bs,
1982                                                    bool want_zero,
1983                                                    int64_t offset,
1984                                                    int64_t bytes,
1985                                                    int64_t *pnum,
1986                                                    int64_t *map,
1987                                                    BlockDriverState **file)
1988 {
1989     assert(bs->backing && bs->backing->bs);
1990     *pnum = bytes;
1991     *map = offset;
1992     *file = bs->backing->bs;
1993     return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
1994 }
1995 
1996 /*
1997  * Returns the allocation status of the specified sectors.
1998  * Drivers not implementing the functionality are assumed to not support
1999  * backing files, hence all their sectors are reported as allocated.
2000  *
2001  * If 'want_zero' is true, the caller is querying for mapping
2002  * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and
2003  * _ZERO where possible; otherwise, the result favors larger 'pnum',
2004  * with a focus on accurate BDRV_BLOCK_ALLOCATED.
2005  *
2006  * If 'offset' is beyond the end of the disk image the return value is
2007  * BDRV_BLOCK_EOF and 'pnum' is set to 0.
2008  *
2009  * 'bytes' is the max value 'pnum' should be set to.  If bytes goes
2010  * beyond the end of the disk image it will be clamped; if 'pnum' is set to
2011  * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
2012  *
2013  * 'pnum' is set to the number of bytes (including and immediately
2014  * following the specified offset) that are easily known to be in the
2015  * same allocated/unallocated state.  Note that a second call starting
2016  * at the original offset plus returned pnum may have the same status.
2017  * The returned value is non-zero on success except at end-of-file.
2018  *
2019  * Returns negative errno on failure.  Otherwise, if the
2020  * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are
2021  * set to the host mapping and BDS corresponding to the guest offset.
2022  */
2023 static int coroutine_fn bdrv_co_block_status(BlockDriverState *bs,
2024                                              bool want_zero,
2025                                              int64_t offset, int64_t bytes,
2026                                              int64_t *pnum, int64_t *map,
2027                                              BlockDriverState **file)
2028 {
2029     int64_t total_size;
2030     int64_t n; /* bytes */
2031     int ret;
2032     int64_t local_map = 0;
2033     BlockDriverState *local_file = NULL;
2034     int64_t aligned_offset, aligned_bytes;
2035     uint32_t align;
2036 
2037     assert(pnum);
2038     *pnum = 0;
2039     total_size = bdrv_getlength(bs);
2040     if (total_size < 0) {
2041         ret = total_size;
2042         goto early_out;
2043     }
2044 
2045     if (offset >= total_size) {
2046         ret = BDRV_BLOCK_EOF;
2047         goto early_out;
2048     }
2049     if (!bytes) {
2050         ret = 0;
2051         goto early_out;
2052     }
2053 
2054     n = total_size - offset;
2055     if (n < bytes) {
2056         bytes = n;
2057     }
2058 
2059     /* Must be non-NULL or bdrv_getlength() would have failed */
2060     assert(bs->drv);
2061     if (!bs->drv->bdrv_co_block_status) {
2062         *pnum = bytes;
2063         ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED;
2064         if (offset + bytes == total_size) {
2065             ret |= BDRV_BLOCK_EOF;
2066         }
2067         if (bs->drv->protocol_name) {
2068             ret |= BDRV_BLOCK_OFFSET_VALID;
2069             local_map = offset;
2070             local_file = bs;
2071         }
2072         goto early_out;
2073     }
2074 
2075     bdrv_inc_in_flight(bs);
2076 
2077     /* Round out to request_alignment boundaries */
2078     align = bs->bl.request_alignment;
2079     aligned_offset = QEMU_ALIGN_DOWN(offset, align);
2080     aligned_bytes = ROUND_UP(offset + bytes, align) - aligned_offset;
2081 
2082     ret = bs->drv->bdrv_co_block_status(bs, want_zero, aligned_offset,
2083                                         aligned_bytes, pnum, &local_map,
2084                                         &local_file);
2085     if (ret < 0) {
2086         *pnum = 0;
2087         goto out;
2088     }
2089 
2090     /*
2091      * The driver's result must be a non-zero multiple of request_alignment.
2092      * Clamp pnum and adjust map to original request.
2093      */
2094     assert(*pnum && QEMU_IS_ALIGNED(*pnum, align) &&
2095            align > offset - aligned_offset);
2096     if (ret & BDRV_BLOCK_RECURSE) {
2097         assert(ret & BDRV_BLOCK_DATA);
2098         assert(ret & BDRV_BLOCK_OFFSET_VALID);
2099         assert(!(ret & BDRV_BLOCK_ZERO));
2100     }
2101 
2102     *pnum -= offset - aligned_offset;
2103     if (*pnum > bytes) {
2104         *pnum = bytes;
2105     }
2106     if (ret & BDRV_BLOCK_OFFSET_VALID) {
2107         local_map += offset - aligned_offset;
2108     }
2109 
2110     if (ret & BDRV_BLOCK_RAW) {
2111         assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file);
2112         ret = bdrv_co_block_status(local_file, want_zero, local_map,
2113                                    *pnum, pnum, &local_map, &local_file);
2114         goto out;
2115     }
2116 
2117     if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
2118         ret |= BDRV_BLOCK_ALLOCATED;
2119     } else if (want_zero) {
2120         if (bdrv_unallocated_blocks_are_zero(bs)) {
2121             ret |= BDRV_BLOCK_ZERO;
2122         } else if (bs->backing) {
2123             BlockDriverState *bs2 = bs->backing->bs;
2124             int64_t size2 = bdrv_getlength(bs2);
2125 
2126             if (size2 >= 0 && offset >= size2) {
2127                 ret |= BDRV_BLOCK_ZERO;
2128             }
2129         }
2130     }
2131 
2132     if (want_zero && ret & BDRV_BLOCK_RECURSE &&
2133         local_file && local_file != bs &&
2134         (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
2135         (ret & BDRV_BLOCK_OFFSET_VALID)) {
2136         int64_t file_pnum;
2137         int ret2;
2138 
2139         ret2 = bdrv_co_block_status(local_file, want_zero, local_map,
2140                                     *pnum, &file_pnum, NULL, NULL);
2141         if (ret2 >= 0) {
2142             /* Ignore errors.  This is just providing extra information, it
2143              * is useful but not necessary.
2144              */
2145             if (ret2 & BDRV_BLOCK_EOF &&
2146                 (!file_pnum || ret2 & BDRV_BLOCK_ZERO)) {
2147                 /*
2148                  * It is valid for the format block driver to read
2149                  * beyond the end of the underlying file's current
2150                  * size; such areas read as zero.
2151                  */
2152                 ret |= BDRV_BLOCK_ZERO;
2153             } else {
2154                 /* Limit request to the range reported by the protocol driver */
2155                 *pnum = file_pnum;
2156                 ret |= (ret2 & BDRV_BLOCK_ZERO);
2157             }
2158         }
2159     }
2160 
2161 out:
2162     bdrv_dec_in_flight(bs);
2163     if (ret >= 0 && offset + *pnum == total_size) {
2164         ret |= BDRV_BLOCK_EOF;
2165     }
2166 early_out:
2167     if (file) {
2168         *file = local_file;
2169     }
2170     if (map) {
2171         *map = local_map;
2172     }
2173     return ret;
2174 }
2175 
2176 static int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs,
2177                                                    BlockDriverState *base,
2178                                                    bool want_zero,
2179                                                    int64_t offset,
2180                                                    int64_t bytes,
2181                                                    int64_t *pnum,
2182                                                    int64_t *map,
2183                                                    BlockDriverState **file)
2184 {
2185     BlockDriverState *p;
2186     int ret = 0;
2187     bool first = true;
2188 
2189     assert(bs != base);
2190     for (p = bs; p != base; p = backing_bs(p)) {
2191         ret = bdrv_co_block_status(p, want_zero, offset, bytes, pnum, map,
2192                                    file);
2193         if (ret < 0) {
2194             break;
2195         }
2196         if (ret & BDRV_BLOCK_ZERO && ret & BDRV_BLOCK_EOF && !first) {
2197             /*
2198              * Reading beyond the end of the file continues to read
2199              * zeroes, but we can only widen the result to the
2200              * unallocated length we learned from an earlier
2201              * iteration.
2202              */
2203             *pnum = bytes;
2204         }
2205         if (ret & (BDRV_BLOCK_ZERO | BDRV_BLOCK_DATA)) {
2206             break;
2207         }
2208         /* [offset, pnum] unallocated on this layer, which could be only
2209          * the first part of [offset, bytes].  */
2210         bytes = MIN(bytes, *pnum);
2211         first = false;
2212     }
2213     return ret;
2214 }
2215 
2216 /* Coroutine wrapper for bdrv_block_status_above() */
2217 static void coroutine_fn bdrv_block_status_above_co_entry(void *opaque)
2218 {
2219     BdrvCoBlockStatusData *data = opaque;
2220 
2221     data->ret = bdrv_co_block_status_above(data->bs, data->base,
2222                                            data->want_zero,
2223                                            data->offset, data->bytes,
2224                                            data->pnum, data->map, data->file);
2225     data->done = true;
2226     aio_wait_kick();
2227 }
2228 
2229 /*
2230  * Synchronous wrapper around bdrv_co_block_status_above().
2231  *
2232  * See bdrv_co_block_status_above() for details.
2233  */
2234 static int bdrv_common_block_status_above(BlockDriverState *bs,
2235                                           BlockDriverState *base,
2236                                           bool want_zero, int64_t offset,
2237                                           int64_t bytes, int64_t *pnum,
2238                                           int64_t *map,
2239                                           BlockDriverState **file)
2240 {
2241     Coroutine *co;
2242     BdrvCoBlockStatusData data = {
2243         .bs = bs,
2244         .base = base,
2245         .want_zero = want_zero,
2246         .offset = offset,
2247         .bytes = bytes,
2248         .pnum = pnum,
2249         .map = map,
2250         .file = file,
2251         .done = false,
2252     };
2253 
2254     if (qemu_in_coroutine()) {
2255         /* Fast-path if already in coroutine context */
2256         bdrv_block_status_above_co_entry(&data);
2257     } else {
2258         co = qemu_coroutine_create(bdrv_block_status_above_co_entry, &data);
2259         bdrv_coroutine_enter(bs, co);
2260         BDRV_POLL_WHILE(bs, !data.done);
2261     }
2262     return data.ret;
2263 }
2264 
2265 int bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base,
2266                             int64_t offset, int64_t bytes, int64_t *pnum,
2267                             int64_t *map, BlockDriverState **file)
2268 {
2269     return bdrv_common_block_status_above(bs, base, true, offset, bytes,
2270                                           pnum, map, file);
2271 }
2272 
2273 int bdrv_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes,
2274                       int64_t *pnum, int64_t *map, BlockDriverState **file)
2275 {
2276     return bdrv_block_status_above(bs, backing_bs(bs),
2277                                    offset, bytes, pnum, map, file);
2278 }
2279 
2280 int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,
2281                                    int64_t bytes, int64_t *pnum)
2282 {
2283     int ret;
2284     int64_t dummy;
2285 
2286     ret = bdrv_common_block_status_above(bs, backing_bs(bs), false, offset,
2287                                          bytes, pnum ? pnum : &dummy, NULL,
2288                                          NULL);
2289     if (ret < 0) {
2290         return ret;
2291     }
2292     return !!(ret & BDRV_BLOCK_ALLOCATED);
2293 }
2294 
2295 /*
2296  * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2297  *
2298  * Return 1 if (a prefix of) the given range is allocated in any image
2299  * between BASE and TOP (BASE is only included if include_base is set).
2300  * BASE can be NULL to check if the given offset is allocated in any
2301  * image of the chain.  Return 0 otherwise, or negative errno on
2302  * failure.
2303  *
2304  * 'pnum' is set to the number of bytes (including and immediately
2305  * following the specified offset) that are known to be in the same
2306  * allocated/unallocated state.  Note that a subsequent call starting
2307  * at 'offset + *pnum' may return the same allocation status (in other
2308  * words, the result is not necessarily the maximum possible range);
2309  * but 'pnum' will only be 0 when end of file is reached.
2310  *
2311  */
2312 int bdrv_is_allocated_above(BlockDriverState *top,
2313                             BlockDriverState *base,
2314                             bool include_base, int64_t offset,
2315                             int64_t bytes, int64_t *pnum)
2316 {
2317     BlockDriverState *intermediate;
2318     int ret;
2319     int64_t n = bytes;
2320 
2321     assert(base || !include_base);
2322 
2323     intermediate = top;
2324     while (include_base || intermediate != base) {
2325         int64_t pnum_inter;
2326         int64_t size_inter;
2327 
2328         assert(intermediate);
2329         ret = bdrv_is_allocated(intermediate, offset, bytes, &pnum_inter);
2330         if (ret < 0) {
2331             return ret;
2332         }
2333         if (ret) {
2334             *pnum = pnum_inter;
2335             return 1;
2336         }
2337 
2338         size_inter = bdrv_getlength(intermediate);
2339         if (size_inter < 0) {
2340             return size_inter;
2341         }
2342         if (n > pnum_inter &&
2343             (intermediate == top || offset + pnum_inter < size_inter)) {
2344             n = pnum_inter;
2345         }
2346 
2347         if (intermediate == base) {
2348             break;
2349         }
2350 
2351         intermediate = backing_bs(intermediate);
2352     }
2353 
2354     *pnum = n;
2355     return 0;
2356 }
2357 
2358 typedef struct BdrvVmstateCo {
2359     BlockDriverState    *bs;
2360     QEMUIOVector        *qiov;
2361     int64_t             pos;
2362     bool                is_read;
2363     int                 ret;
2364 } BdrvVmstateCo;
2365 
2366 static int coroutine_fn
2367 bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
2368                    bool is_read)
2369 {
2370     BlockDriver *drv = bs->drv;
2371     int ret = -ENOTSUP;
2372 
2373     bdrv_inc_in_flight(bs);
2374 
2375     if (!drv) {
2376         ret = -ENOMEDIUM;
2377     } else if (drv->bdrv_load_vmstate) {
2378         if (is_read) {
2379             ret = drv->bdrv_load_vmstate(bs, qiov, pos);
2380         } else {
2381             ret = drv->bdrv_save_vmstate(bs, qiov, pos);
2382         }
2383     } else if (bs->file) {
2384         ret = bdrv_co_rw_vmstate(bs->file->bs, qiov, pos, is_read);
2385     }
2386 
2387     bdrv_dec_in_flight(bs);
2388     return ret;
2389 }
2390 
2391 static void coroutine_fn bdrv_co_rw_vmstate_entry(void *opaque)
2392 {
2393     BdrvVmstateCo *co = opaque;
2394     co->ret = bdrv_co_rw_vmstate(co->bs, co->qiov, co->pos, co->is_read);
2395     aio_wait_kick();
2396 }
2397 
2398 static inline int
2399 bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
2400                 bool is_read)
2401 {
2402     if (qemu_in_coroutine()) {
2403         return bdrv_co_rw_vmstate(bs, qiov, pos, is_read);
2404     } else {
2405         BdrvVmstateCo data = {
2406             .bs         = bs,
2407             .qiov       = qiov,
2408             .pos        = pos,
2409             .is_read    = is_read,
2410             .ret        = -EINPROGRESS,
2411         };
2412         Coroutine *co = qemu_coroutine_create(bdrv_co_rw_vmstate_entry, &data);
2413 
2414         bdrv_coroutine_enter(bs, co);
2415         BDRV_POLL_WHILE(bs, data.ret == -EINPROGRESS);
2416         return data.ret;
2417     }
2418 }
2419 
2420 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2421                       int64_t pos, int size)
2422 {
2423     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
2424     int ret;
2425 
2426     ret = bdrv_writev_vmstate(bs, &qiov, pos);
2427     if (ret < 0) {
2428         return ret;
2429     }
2430 
2431     return size;
2432 }
2433 
2434 int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
2435 {
2436     return bdrv_rw_vmstate(bs, qiov, pos, false);
2437 }
2438 
2439 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2440                       int64_t pos, int size)
2441 {
2442     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
2443     int ret;
2444 
2445     ret = bdrv_readv_vmstate(bs, &qiov, pos);
2446     if (ret < 0) {
2447         return ret;
2448     }
2449 
2450     return size;
2451 }
2452 
2453 int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
2454 {
2455     return bdrv_rw_vmstate(bs, qiov, pos, true);
2456 }
2457 
2458 /**************************************************************/
2459 /* async I/Os */
2460 
2461 void bdrv_aio_cancel(BlockAIOCB *acb)
2462 {
2463     qemu_aio_ref(acb);
2464     bdrv_aio_cancel_async(acb);
2465     while (acb->refcnt > 1) {
2466         if (acb->aiocb_info->get_aio_context) {
2467             aio_poll(acb->aiocb_info->get_aio_context(acb), true);
2468         } else if (acb->bs) {
2469             /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2470              * assert that we're not using an I/O thread.  Thread-safe
2471              * code should use bdrv_aio_cancel_async exclusively.
2472              */
2473             assert(bdrv_get_aio_context(acb->bs) == qemu_get_aio_context());
2474             aio_poll(bdrv_get_aio_context(acb->bs), true);
2475         } else {
2476             abort();
2477         }
2478     }
2479     qemu_aio_unref(acb);
2480 }
2481 
2482 /* Async version of aio cancel. The caller is not blocked if the acb implements
2483  * cancel_async, otherwise we do nothing and let the request normally complete.
2484  * In either case the completion callback must be called. */
2485 void bdrv_aio_cancel_async(BlockAIOCB *acb)
2486 {
2487     if (acb->aiocb_info->cancel_async) {
2488         acb->aiocb_info->cancel_async(acb);
2489     }
2490 }
2491 
2492 /**************************************************************/
2493 /* Coroutine block device emulation */
2494 
2495 typedef struct FlushCo {
2496     BlockDriverState *bs;
2497     int ret;
2498 } FlushCo;
2499 
2500 
2501 static void coroutine_fn bdrv_flush_co_entry(void *opaque)
2502 {
2503     FlushCo *rwco = opaque;
2504 
2505     rwco->ret = bdrv_co_flush(rwco->bs);
2506     aio_wait_kick();
2507 }
2508 
2509 int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2510 {
2511     int current_gen;
2512     int ret = 0;
2513 
2514     bdrv_inc_in_flight(bs);
2515 
2516     if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
2517         bdrv_is_sg(bs)) {
2518         goto early_exit;
2519     }
2520 
2521     qemu_co_mutex_lock(&bs->reqs_lock);
2522     current_gen = atomic_read(&bs->write_gen);
2523 
2524     /* Wait until any previous flushes are completed */
2525     while (bs->active_flush_req) {
2526         qemu_co_queue_wait(&bs->flush_queue, &bs->reqs_lock);
2527     }
2528 
2529     /* Flushes reach this point in nondecreasing current_gen order.  */
2530     bs->active_flush_req = true;
2531     qemu_co_mutex_unlock(&bs->reqs_lock);
2532 
2533     /* Write back all layers by calling one driver function */
2534     if (bs->drv->bdrv_co_flush) {
2535         ret = bs->drv->bdrv_co_flush(bs);
2536         goto out;
2537     }
2538 
2539     /* Write back cached data to the OS even with cache=unsafe */
2540     BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS);
2541     if (bs->drv->bdrv_co_flush_to_os) {
2542         ret = bs->drv->bdrv_co_flush_to_os(bs);
2543         if (ret < 0) {
2544             goto out;
2545         }
2546     }
2547 
2548     /* But don't actually force it to the disk with cache=unsafe */
2549     if (bs->open_flags & BDRV_O_NO_FLUSH) {
2550         goto flush_parent;
2551     }
2552 
2553     /* Check if we really need to flush anything */
2554     if (bs->flushed_gen == current_gen) {
2555         goto flush_parent;
2556     }
2557 
2558     BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
2559     if (!bs->drv) {
2560         /* bs->drv->bdrv_co_flush() might have ejected the BDS
2561          * (even in case of apparent success) */
2562         ret = -ENOMEDIUM;
2563         goto out;
2564     }
2565     if (bs->drv->bdrv_co_flush_to_disk) {
2566         ret = bs->drv->bdrv_co_flush_to_disk(bs);
2567     } else if (bs->drv->bdrv_aio_flush) {
2568         BlockAIOCB *acb;
2569         CoroutineIOCompletion co = {
2570             .coroutine = qemu_coroutine_self(),
2571         };
2572 
2573         acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
2574         if (acb == NULL) {
2575             ret = -EIO;
2576         } else {
2577             qemu_coroutine_yield();
2578             ret = co.ret;
2579         }
2580     } else {
2581         /*
2582          * Some block drivers always operate in either writethrough or unsafe
2583          * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2584          * know how the server works (because the behaviour is hardcoded or
2585          * depends on server-side configuration), so we can't ensure that
2586          * everything is safe on disk. Returning an error doesn't work because
2587          * that would break guests even if the server operates in writethrough
2588          * mode.
2589          *
2590          * Let's hope the user knows what he's doing.
2591          */
2592         ret = 0;
2593     }
2594 
2595     if (ret < 0) {
2596         goto out;
2597     }
2598 
2599     /* Now flush the underlying protocol.  It will also have BDRV_O_NO_FLUSH
2600      * in the case of cache=unsafe, so there are no useless flushes.
2601      */
2602 flush_parent:
2603     ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0;
2604 out:
2605     /* Notify any pending flushes that we have completed */
2606     if (ret == 0) {
2607         bs->flushed_gen = current_gen;
2608     }
2609 
2610     qemu_co_mutex_lock(&bs->reqs_lock);
2611     bs->active_flush_req = false;
2612     /* Return value is ignored - it's ok if wait queue is empty */
2613     qemu_co_queue_next(&bs->flush_queue);
2614     qemu_co_mutex_unlock(&bs->reqs_lock);
2615 
2616 early_exit:
2617     bdrv_dec_in_flight(bs);
2618     return ret;
2619 }
2620 
2621 int bdrv_flush(BlockDriverState *bs)
2622 {
2623     Coroutine *co;
2624     FlushCo flush_co = {
2625         .bs = bs,
2626         .ret = NOT_DONE,
2627     };
2628 
2629     if (qemu_in_coroutine()) {
2630         /* Fast-path if already in coroutine context */
2631         bdrv_flush_co_entry(&flush_co);
2632     } else {
2633         co = qemu_coroutine_create(bdrv_flush_co_entry, &flush_co);
2634         bdrv_coroutine_enter(bs, co);
2635         BDRV_POLL_WHILE(bs, flush_co.ret == NOT_DONE);
2636     }
2637 
2638     return flush_co.ret;
2639 }
2640 
2641 typedef struct DiscardCo {
2642     BdrvChild *child;
2643     int64_t offset;
2644     int64_t bytes;
2645     int ret;
2646 } DiscardCo;
2647 static void coroutine_fn bdrv_pdiscard_co_entry(void *opaque)
2648 {
2649     DiscardCo *rwco = opaque;
2650 
2651     rwco->ret = bdrv_co_pdiscard(rwco->child, rwco->offset, rwco->bytes);
2652     aio_wait_kick();
2653 }
2654 
2655 int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
2656                                   int64_t bytes)
2657 {
2658     BdrvTrackedRequest req;
2659     int max_pdiscard, ret;
2660     int head, tail, align;
2661     BlockDriverState *bs = child->bs;
2662 
2663     if (!bs || !bs->drv || !bdrv_is_inserted(bs)) {
2664         return -ENOMEDIUM;
2665     }
2666 
2667     if (bdrv_has_readonly_bitmaps(bs)) {
2668         return -EPERM;
2669     }
2670 
2671     if (offset < 0 || bytes < 0 || bytes > INT64_MAX - offset) {
2672         return -EIO;
2673     }
2674 
2675     /* Do nothing if disabled.  */
2676     if (!(bs->open_flags & BDRV_O_UNMAP)) {
2677         return 0;
2678     }
2679 
2680     if (!bs->drv->bdrv_co_pdiscard && !bs->drv->bdrv_aio_pdiscard) {
2681         return 0;
2682     }
2683 
2684     /* Discard is advisory, but some devices track and coalesce
2685      * unaligned requests, so we must pass everything down rather than
2686      * round here.  Still, most devices will just silently ignore
2687      * unaligned requests (by returning -ENOTSUP), so we must fragment
2688      * the request accordingly.  */
2689     align = MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment);
2690     assert(align % bs->bl.request_alignment == 0);
2691     head = offset % align;
2692     tail = (offset + bytes) % align;
2693 
2694     bdrv_inc_in_flight(bs);
2695     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_DISCARD);
2696 
2697     ret = bdrv_co_write_req_prepare(child, offset, bytes, &req, 0);
2698     if (ret < 0) {
2699         goto out;
2700     }
2701 
2702     max_pdiscard = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard, INT_MAX),
2703                                    align);
2704     assert(max_pdiscard >= bs->bl.request_alignment);
2705 
2706     while (bytes > 0) {
2707         int64_t num = bytes;
2708 
2709         if (head) {
2710             /* Make small requests to get to alignment boundaries. */
2711             num = MIN(bytes, align - head);
2712             if (!QEMU_IS_ALIGNED(num, bs->bl.request_alignment)) {
2713                 num %= bs->bl.request_alignment;
2714             }
2715             head = (head + num) % align;
2716             assert(num < max_pdiscard);
2717         } else if (tail) {
2718             if (num > align) {
2719                 /* Shorten the request to the last aligned cluster.  */
2720                 num -= tail;
2721             } else if (!QEMU_IS_ALIGNED(tail, bs->bl.request_alignment) &&
2722                        tail > bs->bl.request_alignment) {
2723                 tail %= bs->bl.request_alignment;
2724                 num -= tail;
2725             }
2726         }
2727         /* limit request size */
2728         if (num > max_pdiscard) {
2729             num = max_pdiscard;
2730         }
2731 
2732         if (!bs->drv) {
2733             ret = -ENOMEDIUM;
2734             goto out;
2735         }
2736         if (bs->drv->bdrv_co_pdiscard) {
2737             ret = bs->drv->bdrv_co_pdiscard(bs, offset, num);
2738         } else {
2739             BlockAIOCB *acb;
2740             CoroutineIOCompletion co = {
2741                 .coroutine = qemu_coroutine_self(),
2742             };
2743 
2744             acb = bs->drv->bdrv_aio_pdiscard(bs, offset, num,
2745                                              bdrv_co_io_em_complete, &co);
2746             if (acb == NULL) {
2747                 ret = -EIO;
2748                 goto out;
2749             } else {
2750                 qemu_coroutine_yield();
2751                 ret = co.ret;
2752             }
2753         }
2754         if (ret && ret != -ENOTSUP) {
2755             goto out;
2756         }
2757 
2758         offset += num;
2759         bytes -= num;
2760     }
2761     ret = 0;
2762 out:
2763     bdrv_co_write_req_finish(child, req.offset, req.bytes, &req, ret);
2764     tracked_request_end(&req);
2765     bdrv_dec_in_flight(bs);
2766     return ret;
2767 }
2768 
2769 int bdrv_pdiscard(BdrvChild *child, int64_t offset, int64_t bytes)
2770 {
2771     Coroutine *co;
2772     DiscardCo rwco = {
2773         .child = child,
2774         .offset = offset,
2775         .bytes = bytes,
2776         .ret = NOT_DONE,
2777     };
2778 
2779     if (qemu_in_coroutine()) {
2780         /* Fast-path if already in coroutine context */
2781         bdrv_pdiscard_co_entry(&rwco);
2782     } else {
2783         co = qemu_coroutine_create(bdrv_pdiscard_co_entry, &rwco);
2784         bdrv_coroutine_enter(child->bs, co);
2785         BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE);
2786     }
2787 
2788     return rwco.ret;
2789 }
2790 
2791 int bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf)
2792 {
2793     BlockDriver *drv = bs->drv;
2794     CoroutineIOCompletion co = {
2795         .coroutine = qemu_coroutine_self(),
2796     };
2797     BlockAIOCB *acb;
2798 
2799     bdrv_inc_in_flight(bs);
2800     if (!drv || (!drv->bdrv_aio_ioctl && !drv->bdrv_co_ioctl)) {
2801         co.ret = -ENOTSUP;
2802         goto out;
2803     }
2804 
2805     if (drv->bdrv_co_ioctl) {
2806         co.ret = drv->bdrv_co_ioctl(bs, req, buf);
2807     } else {
2808         acb = drv->bdrv_aio_ioctl(bs, req, buf, bdrv_co_io_em_complete, &co);
2809         if (!acb) {
2810             co.ret = -ENOTSUP;
2811             goto out;
2812         }
2813         qemu_coroutine_yield();
2814     }
2815 out:
2816     bdrv_dec_in_flight(bs);
2817     return co.ret;
2818 }
2819 
2820 void *qemu_blockalign(BlockDriverState *bs, size_t size)
2821 {
2822     return qemu_memalign(bdrv_opt_mem_align(bs), size);
2823 }
2824 
2825 void *qemu_blockalign0(BlockDriverState *bs, size_t size)
2826 {
2827     return memset(qemu_blockalign(bs, size), 0, size);
2828 }
2829 
2830 void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
2831 {
2832     size_t align = bdrv_opt_mem_align(bs);
2833 
2834     /* Ensure that NULL is never returned on success */
2835     assert(align > 0);
2836     if (size == 0) {
2837         size = align;
2838     }
2839 
2840     return qemu_try_memalign(align, size);
2841 }
2842 
2843 void *qemu_try_blockalign0(BlockDriverState *bs, size_t size)
2844 {
2845     void *mem = qemu_try_blockalign(bs, size);
2846 
2847     if (mem) {
2848         memset(mem, 0, size);
2849     }
2850 
2851     return mem;
2852 }
2853 
2854 /*
2855  * Check if all memory in this vector is sector aligned.
2856  */
2857 bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
2858 {
2859     int i;
2860     size_t alignment = bdrv_min_mem_align(bs);
2861 
2862     for (i = 0; i < qiov->niov; i++) {
2863         if ((uintptr_t) qiov->iov[i].iov_base % alignment) {
2864             return false;
2865         }
2866         if (qiov->iov[i].iov_len % alignment) {
2867             return false;
2868         }
2869     }
2870 
2871     return true;
2872 }
2873 
2874 void bdrv_add_before_write_notifier(BlockDriverState *bs,
2875                                     NotifierWithReturn *notifier)
2876 {
2877     notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
2878 }
2879 
2880 void bdrv_io_plug(BlockDriverState *bs)
2881 {
2882     BdrvChild *child;
2883 
2884     QLIST_FOREACH(child, &bs->children, next) {
2885         bdrv_io_plug(child->bs);
2886     }
2887 
2888     if (atomic_fetch_inc(&bs->io_plugged) == 0) {
2889         BlockDriver *drv = bs->drv;
2890         if (drv && drv->bdrv_io_plug) {
2891             drv->bdrv_io_plug(bs);
2892         }
2893     }
2894 }
2895 
2896 void bdrv_io_unplug(BlockDriverState *bs)
2897 {
2898     BdrvChild *child;
2899 
2900     assert(bs->io_plugged);
2901     if (atomic_fetch_dec(&bs->io_plugged) == 1) {
2902         BlockDriver *drv = bs->drv;
2903         if (drv && drv->bdrv_io_unplug) {
2904             drv->bdrv_io_unplug(bs);
2905         }
2906     }
2907 
2908     QLIST_FOREACH(child, &bs->children, next) {
2909         bdrv_io_unplug(child->bs);
2910     }
2911 }
2912 
2913 void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size)
2914 {
2915     BdrvChild *child;
2916 
2917     if (bs->drv && bs->drv->bdrv_register_buf) {
2918         bs->drv->bdrv_register_buf(bs, host, size);
2919     }
2920     QLIST_FOREACH(child, &bs->children, next) {
2921         bdrv_register_buf(child->bs, host, size);
2922     }
2923 }
2924 
2925 void bdrv_unregister_buf(BlockDriverState *bs, void *host)
2926 {
2927     BdrvChild *child;
2928 
2929     if (bs->drv && bs->drv->bdrv_unregister_buf) {
2930         bs->drv->bdrv_unregister_buf(bs, host);
2931     }
2932     QLIST_FOREACH(child, &bs->children, next) {
2933         bdrv_unregister_buf(child->bs, host);
2934     }
2935 }
2936 
2937 static int coroutine_fn bdrv_co_copy_range_internal(
2938         BdrvChild *src, uint64_t src_offset, BdrvChild *dst,
2939         uint64_t dst_offset, uint64_t bytes,
2940         BdrvRequestFlags read_flags, BdrvRequestFlags write_flags,
2941         bool recurse_src)
2942 {
2943     BdrvTrackedRequest req;
2944     int ret;
2945 
2946     /* TODO We can support BDRV_REQ_NO_FALLBACK here */
2947     assert(!(read_flags & BDRV_REQ_NO_FALLBACK));
2948     assert(!(write_flags & BDRV_REQ_NO_FALLBACK));
2949 
2950     if (!dst || !dst->bs) {
2951         return -ENOMEDIUM;
2952     }
2953     ret = bdrv_check_byte_request(dst->bs, dst_offset, bytes);
2954     if (ret) {
2955         return ret;
2956     }
2957     if (write_flags & BDRV_REQ_ZERO_WRITE) {
2958         return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, write_flags);
2959     }
2960 
2961     if (!src || !src->bs) {
2962         return -ENOMEDIUM;
2963     }
2964     ret = bdrv_check_byte_request(src->bs, src_offset, bytes);
2965     if (ret) {
2966         return ret;
2967     }
2968 
2969     if (!src->bs->drv->bdrv_co_copy_range_from
2970         || !dst->bs->drv->bdrv_co_copy_range_to
2971         || src->bs->encrypted || dst->bs->encrypted) {
2972         return -ENOTSUP;
2973     }
2974 
2975     if (recurse_src) {
2976         bdrv_inc_in_flight(src->bs);
2977         tracked_request_begin(&req, src->bs, src_offset, bytes,
2978                               BDRV_TRACKED_READ);
2979 
2980         /* BDRV_REQ_SERIALISING is only for write operation */
2981         assert(!(read_flags & BDRV_REQ_SERIALISING));
2982         if (!(read_flags & BDRV_REQ_NO_SERIALISING)) {
2983             wait_serialising_requests(&req);
2984         }
2985 
2986         ret = src->bs->drv->bdrv_co_copy_range_from(src->bs,
2987                                                     src, src_offset,
2988                                                     dst, dst_offset,
2989                                                     bytes,
2990                                                     read_flags, write_flags);
2991 
2992         tracked_request_end(&req);
2993         bdrv_dec_in_flight(src->bs);
2994     } else {
2995         bdrv_inc_in_flight(dst->bs);
2996         tracked_request_begin(&req, dst->bs, dst_offset, bytes,
2997                               BDRV_TRACKED_WRITE);
2998         ret = bdrv_co_write_req_prepare(dst, dst_offset, bytes, &req,
2999                                         write_flags);
3000         if (!ret) {
3001             ret = dst->bs->drv->bdrv_co_copy_range_to(dst->bs,
3002                                                       src, src_offset,
3003                                                       dst, dst_offset,
3004                                                       bytes,
3005                                                       read_flags, write_flags);
3006         }
3007         bdrv_co_write_req_finish(dst, dst_offset, bytes, &req, ret);
3008         tracked_request_end(&req);
3009         bdrv_dec_in_flight(dst->bs);
3010     }
3011 
3012     return ret;
3013 }
3014 
3015 /* Copy range from @src to @dst.
3016  *
3017  * See the comment of bdrv_co_copy_range for the parameter and return value
3018  * semantics. */
3019 int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, uint64_t src_offset,
3020                                          BdrvChild *dst, uint64_t dst_offset,
3021                                          uint64_t bytes,
3022                                          BdrvRequestFlags read_flags,
3023                                          BdrvRequestFlags write_flags)
3024 {
3025     trace_bdrv_co_copy_range_from(src, src_offset, dst, dst_offset, bytes,
3026                                   read_flags, write_flags);
3027     return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
3028                                        bytes, read_flags, write_flags, true);
3029 }
3030 
3031 /* Copy range from @src to @dst.
3032  *
3033  * See the comment of bdrv_co_copy_range for the parameter and return value
3034  * semantics. */
3035 int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, uint64_t src_offset,
3036                                        BdrvChild *dst, uint64_t dst_offset,
3037                                        uint64_t bytes,
3038                                        BdrvRequestFlags read_flags,
3039                                        BdrvRequestFlags write_flags)
3040 {
3041     trace_bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
3042                                 read_flags, write_flags);
3043     return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
3044                                        bytes, read_flags, write_flags, false);
3045 }
3046 
3047 int coroutine_fn bdrv_co_copy_range(BdrvChild *src, uint64_t src_offset,
3048                                     BdrvChild *dst, uint64_t dst_offset,
3049                                     uint64_t bytes, BdrvRequestFlags read_flags,
3050                                     BdrvRequestFlags write_flags)
3051 {
3052     return bdrv_co_copy_range_from(src, src_offset,
3053                                    dst, dst_offset,
3054                                    bytes, read_flags, write_flags);
3055 }
3056 
3057 static void bdrv_parent_cb_resize(BlockDriverState *bs)
3058 {
3059     BdrvChild *c;
3060     QLIST_FOREACH(c, &bs->parents, next_parent) {
3061         if (c->role->resize) {
3062             c->role->resize(c);
3063         }
3064     }
3065 }
3066 
3067 /**
3068  * Truncate file to 'offset' bytes (needed only for file protocols)
3069  */
3070 int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset,
3071                                   PreallocMode prealloc, Error **errp)
3072 {
3073     BlockDriverState *bs = child->bs;
3074     BlockDriver *drv = bs->drv;
3075     BdrvTrackedRequest req;
3076     int64_t old_size, new_bytes;
3077     int ret;
3078 
3079 
3080     /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3081     if (!drv) {
3082         error_setg(errp, "No medium inserted");
3083         return -ENOMEDIUM;
3084     }
3085     if (offset < 0) {
3086         error_setg(errp, "Image size cannot be negative");
3087         return -EINVAL;
3088     }
3089 
3090     old_size = bdrv_getlength(bs);
3091     if (old_size < 0) {
3092         error_setg_errno(errp, -old_size, "Failed to get old image size");
3093         return old_size;
3094     }
3095 
3096     if (offset > old_size) {
3097         new_bytes = offset - old_size;
3098     } else {
3099         new_bytes = 0;
3100     }
3101 
3102     bdrv_inc_in_flight(bs);
3103     tracked_request_begin(&req, bs, offset - new_bytes, new_bytes,
3104                           BDRV_TRACKED_TRUNCATE);
3105 
3106     /* If we are growing the image and potentially using preallocation for the
3107      * new area, we need to make sure that no write requests are made to it
3108      * concurrently or they might be overwritten by preallocation. */
3109     if (new_bytes) {
3110         mark_request_serialising(&req, 1);
3111     }
3112     if (bs->read_only) {
3113         error_setg(errp, "Image is read-only");
3114         ret = -EACCES;
3115         goto out;
3116     }
3117     ret = bdrv_co_write_req_prepare(child, offset - new_bytes, new_bytes, &req,
3118                                     0);
3119     if (ret < 0) {
3120         error_setg_errno(errp, -ret,
3121                          "Failed to prepare request for truncation");
3122         goto out;
3123     }
3124 
3125     if (!drv->bdrv_co_truncate) {
3126         if (bs->file && drv->is_filter) {
3127             ret = bdrv_co_truncate(bs->file, offset, prealloc, errp);
3128             goto out;
3129         }
3130         error_setg(errp, "Image format driver does not support resize");
3131         ret = -ENOTSUP;
3132         goto out;
3133     }
3134 
3135     ret = drv->bdrv_co_truncate(bs, offset, prealloc, errp);
3136     if (ret < 0) {
3137         goto out;
3138     }
3139     ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
3140     if (ret < 0) {
3141         error_setg_errno(errp, -ret, "Could not refresh total sector count");
3142     } else {
3143         offset = bs->total_sectors * BDRV_SECTOR_SIZE;
3144     }
3145     /* It's possible that truncation succeeded but refresh_total_sectors
3146      * failed, but the latter doesn't affect how we should finish the request.
3147      * Pass 0 as the last parameter so that dirty bitmaps etc. are handled. */
3148     bdrv_co_write_req_finish(child, offset - new_bytes, new_bytes, &req, 0);
3149 
3150 out:
3151     tracked_request_end(&req);
3152     bdrv_dec_in_flight(bs);
3153 
3154     return ret;
3155 }
3156 
3157 typedef struct TruncateCo {
3158     BdrvChild *child;
3159     int64_t offset;
3160     PreallocMode prealloc;
3161     Error **errp;
3162     int ret;
3163 } TruncateCo;
3164 
3165 static void coroutine_fn bdrv_truncate_co_entry(void *opaque)
3166 {
3167     TruncateCo *tco = opaque;
3168     tco->ret = bdrv_co_truncate(tco->child, tco->offset, tco->prealloc,
3169                                 tco->errp);
3170     aio_wait_kick();
3171 }
3172 
3173 int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,
3174                   Error **errp)
3175 {
3176     Coroutine *co;
3177     TruncateCo tco = {
3178         .child      = child,
3179         .offset     = offset,
3180         .prealloc   = prealloc,
3181         .errp       = errp,
3182         .ret        = NOT_DONE,
3183     };
3184 
3185     if (qemu_in_coroutine()) {
3186         /* Fast-path if already in coroutine context */
3187         bdrv_truncate_co_entry(&tco);
3188     } else {
3189         co = qemu_coroutine_create(bdrv_truncate_co_entry, &tco);
3190         bdrv_coroutine_enter(child->bs, co);
3191         BDRV_POLL_WHILE(child->bs, tco.ret == NOT_DONE);
3192     }
3193 
3194     return tco.ret;
3195 }
3196