xref: /openbmc/qemu/block/io.c (revision daff9f7f)
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 "block/coroutines.h"
33 #include "block/dirty-bitmap.h"
34 #include "block/write-threshold.h"
35 #include "qemu/cutils.h"
36 #include "qemu/memalign.h"
37 #include "qapi/error.h"
38 #include "qemu/error-report.h"
39 #include "qemu/main-loop.h"
40 #include "sysemu/replay.h"
41 
42 /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
43 #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
44 
45 static void coroutine_fn GRAPH_RDLOCK
46 bdrv_parent_cb_resize(BlockDriverState *bs);
47 
48 static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
49     int64_t offset, int64_t bytes, BdrvRequestFlags flags);
50 
51 static void GRAPH_RDLOCK
52 bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore)
53 {
54     BdrvChild *c, *next;
55     IO_OR_GS_CODE();
56     assert_bdrv_graph_readable();
57 
58     QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
59         if (c == ignore) {
60             continue;
61         }
62         bdrv_parent_drained_begin_single(c);
63     }
64 }
65 
66 void bdrv_parent_drained_end_single(BdrvChild *c)
67 {
68     GLOBAL_STATE_CODE();
69 
70     assert(c->quiesced_parent);
71     c->quiesced_parent = false;
72 
73     if (c->klass->drained_end) {
74         c->klass->drained_end(c);
75     }
76 }
77 
78 static void GRAPH_RDLOCK
79 bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore)
80 {
81     BdrvChild *c;
82     IO_OR_GS_CODE();
83     assert_bdrv_graph_readable();
84 
85     QLIST_FOREACH(c, &bs->parents, next_parent) {
86         if (c == ignore) {
87             continue;
88         }
89         bdrv_parent_drained_end_single(c);
90     }
91 }
92 
93 bool bdrv_parent_drained_poll_single(BdrvChild *c)
94 {
95     IO_OR_GS_CODE();
96 
97     if (c->klass->drained_poll) {
98         return c->klass->drained_poll(c);
99     }
100     return false;
101 }
102 
103 static bool GRAPH_RDLOCK
104 bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,
105                          bool ignore_bds_parents)
106 {
107     BdrvChild *c, *next;
108     bool busy = false;
109     IO_OR_GS_CODE();
110     assert_bdrv_graph_readable();
111 
112     QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
113         if (c == ignore || (ignore_bds_parents && c->klass->parent_is_bds)) {
114             continue;
115         }
116         busy |= bdrv_parent_drained_poll_single(c);
117     }
118 
119     return busy;
120 }
121 
122 void bdrv_parent_drained_begin_single(BdrvChild *c)
123 {
124     GLOBAL_STATE_CODE();
125 
126     assert(!c->quiesced_parent);
127     c->quiesced_parent = true;
128 
129     if (c->klass->drained_begin) {
130         /* called with rdlock taken, but it doesn't really need it. */
131         c->klass->drained_begin(c);
132     }
133 }
134 
135 static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src)
136 {
137     dst->pdiscard_alignment = MAX(dst->pdiscard_alignment,
138                                   src->pdiscard_alignment);
139     dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer);
140     dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer);
141     dst->max_hw_transfer = MIN_NON_ZERO(dst->max_hw_transfer,
142                                         src->max_hw_transfer);
143     dst->opt_mem_alignment = MAX(dst->opt_mem_alignment,
144                                  src->opt_mem_alignment);
145     dst->min_mem_alignment = MAX(dst->min_mem_alignment,
146                                  src->min_mem_alignment);
147     dst->max_iov = MIN_NON_ZERO(dst->max_iov, src->max_iov);
148     dst->max_hw_iov = MIN_NON_ZERO(dst->max_hw_iov, src->max_hw_iov);
149 }
150 
151 typedef struct BdrvRefreshLimitsState {
152     BlockDriverState *bs;
153     BlockLimits old_bl;
154 } BdrvRefreshLimitsState;
155 
156 static void bdrv_refresh_limits_abort(void *opaque)
157 {
158     BdrvRefreshLimitsState *s = opaque;
159 
160     s->bs->bl = s->old_bl;
161 }
162 
163 static TransactionActionDrv bdrv_refresh_limits_drv = {
164     .abort = bdrv_refresh_limits_abort,
165     .clean = g_free,
166 };
167 
168 /* @tran is allowed to be NULL, in this case no rollback is possible. */
169 void bdrv_refresh_limits(BlockDriverState *bs, Transaction *tran, Error **errp)
170 {
171     ERRP_GUARD();
172     BlockDriver *drv = bs->drv;
173     BdrvChild *c;
174     bool have_limits;
175 
176     GLOBAL_STATE_CODE();
177 
178     if (tran) {
179         BdrvRefreshLimitsState *s = g_new(BdrvRefreshLimitsState, 1);
180         *s = (BdrvRefreshLimitsState) {
181             .bs = bs,
182             .old_bl = bs->bl,
183         };
184         tran_add(tran, &bdrv_refresh_limits_drv, s);
185     }
186 
187     memset(&bs->bl, 0, sizeof(bs->bl));
188 
189     if (!drv) {
190         return;
191     }
192 
193     /* Default alignment based on whether driver has byte interface */
194     bs->bl.request_alignment = (drv->bdrv_co_preadv ||
195                                 drv->bdrv_aio_preadv ||
196                                 drv->bdrv_co_preadv_part) ? 1 : 512;
197 
198     /* Take some limits from the children as a default */
199     have_limits = false;
200     QLIST_FOREACH(c, &bs->children, next) {
201         if (c->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED | BDRV_CHILD_COW))
202         {
203             bdrv_merge_limits(&bs->bl, &c->bs->bl);
204             have_limits = true;
205         }
206 
207         if (c->role & BDRV_CHILD_FILTERED) {
208             bs->bl.has_variable_length |= c->bs->bl.has_variable_length;
209         }
210     }
211 
212     if (!have_limits) {
213         bs->bl.min_mem_alignment = 512;
214         bs->bl.opt_mem_alignment = qemu_real_host_page_size();
215 
216         /* Safe default since most protocols use readv()/writev()/etc */
217         bs->bl.max_iov = IOV_MAX;
218     }
219 
220     /* Then let the driver override it */
221     if (drv->bdrv_refresh_limits) {
222         drv->bdrv_refresh_limits(bs, errp);
223         if (*errp) {
224             return;
225         }
226     }
227 
228     if (bs->bl.request_alignment > BDRV_MAX_ALIGNMENT) {
229         error_setg(errp, "Driver requires too large request alignment");
230     }
231 }
232 
233 /**
234  * The copy-on-read flag is actually a reference count so multiple users may
235  * use the feature without worrying about clobbering its previous state.
236  * Copy-on-read stays enabled until all users have called to disable it.
237  */
238 void bdrv_enable_copy_on_read(BlockDriverState *bs)
239 {
240     IO_CODE();
241     qatomic_inc(&bs->copy_on_read);
242 }
243 
244 void bdrv_disable_copy_on_read(BlockDriverState *bs)
245 {
246     int old = qatomic_fetch_dec(&bs->copy_on_read);
247     IO_CODE();
248     assert(old >= 1);
249 }
250 
251 typedef struct {
252     Coroutine *co;
253     BlockDriverState *bs;
254     bool done;
255     bool begin;
256     bool poll;
257     BdrvChild *parent;
258 } BdrvCoDrainData;
259 
260 /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
261 bool bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent,
262                      bool ignore_bds_parents)
263 {
264     GLOBAL_STATE_CODE();
265 
266     if (bdrv_parent_drained_poll(bs, ignore_parent, ignore_bds_parents)) {
267         return true;
268     }
269 
270     if (qatomic_read(&bs->in_flight)) {
271         return true;
272     }
273 
274     return false;
275 }
276 
277 static bool bdrv_drain_poll_top_level(BlockDriverState *bs,
278                                       BdrvChild *ignore_parent)
279 {
280     GLOBAL_STATE_CODE();
281     GRAPH_RDLOCK_GUARD_MAINLOOP();
282 
283     return bdrv_drain_poll(bs, ignore_parent, false);
284 }
285 
286 static void bdrv_do_drained_begin(BlockDriverState *bs, BdrvChild *parent,
287                                   bool poll);
288 static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent);
289 
290 static void bdrv_co_drain_bh_cb(void *opaque)
291 {
292     BdrvCoDrainData *data = opaque;
293     Coroutine *co = data->co;
294     BlockDriverState *bs = data->bs;
295 
296     if (bs) {
297         bdrv_dec_in_flight(bs);
298         if (data->begin) {
299             bdrv_do_drained_begin(bs, data->parent, data->poll);
300         } else {
301             assert(!data->poll);
302             bdrv_do_drained_end(bs, data->parent);
303         }
304     } else {
305         assert(data->begin);
306         bdrv_drain_all_begin();
307     }
308 
309     data->done = true;
310     aio_co_wake(co);
311 }
312 
313 static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
314                                                 bool begin,
315                                                 BdrvChild *parent,
316                                                 bool poll)
317 {
318     BdrvCoDrainData data;
319     Coroutine *self = qemu_coroutine_self();
320 
321     /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
322      * other coroutines run if they were queued by aio_co_enter(). */
323 
324     assert(qemu_in_coroutine());
325     data = (BdrvCoDrainData) {
326         .co = self,
327         .bs = bs,
328         .done = false,
329         .begin = begin,
330         .parent = parent,
331         .poll = poll,
332     };
333 
334     if (bs) {
335         bdrv_inc_in_flight(bs);
336     }
337 
338     replay_bh_schedule_oneshot_event(qemu_get_aio_context(),
339                                      bdrv_co_drain_bh_cb, &data);
340 
341     qemu_coroutine_yield();
342     /* If we are resumed from some other event (such as an aio completion or a
343      * timer callback), it is a bug in the caller that should be fixed. */
344     assert(data.done);
345 }
346 
347 static void bdrv_do_drained_begin(BlockDriverState *bs, BdrvChild *parent,
348                                   bool poll)
349 {
350     IO_OR_GS_CODE();
351 
352     if (qemu_in_coroutine()) {
353         bdrv_co_yield_to_drain(bs, true, parent, poll);
354         return;
355     }
356 
357     GLOBAL_STATE_CODE();
358 
359     /* Stop things in parent-to-child order */
360     if (qatomic_fetch_inc(&bs->quiesce_counter) == 0) {
361         GRAPH_RDLOCK_GUARD_MAINLOOP();
362         bdrv_parent_drained_begin(bs, parent);
363         if (bs->drv && bs->drv->bdrv_drain_begin) {
364             bs->drv->bdrv_drain_begin(bs);
365         }
366     }
367 
368     /*
369      * Wait for drained requests to finish.
370      *
371      * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
372      * call is needed so things in this AioContext can make progress even
373      * though we don't return to the main AioContext loop - this automatically
374      * includes other nodes in the same AioContext and therefore all child
375      * nodes.
376      */
377     if (poll) {
378         BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, parent));
379     }
380 }
381 
382 void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent)
383 {
384     bdrv_do_drained_begin(bs, parent, false);
385 }
386 
387 void coroutine_mixed_fn
388 bdrv_drained_begin(BlockDriverState *bs)
389 {
390     IO_OR_GS_CODE();
391     bdrv_do_drained_begin(bs, NULL, true);
392 }
393 
394 /**
395  * This function does not poll, nor must any of its recursively called
396  * functions.
397  */
398 static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent)
399 {
400     int old_quiesce_counter;
401 
402     IO_OR_GS_CODE();
403 
404     if (qemu_in_coroutine()) {
405         bdrv_co_yield_to_drain(bs, false, parent, false);
406         return;
407     }
408 
409     /* At this point, we should be always running in the main loop. */
410     GLOBAL_STATE_CODE();
411     assert(bs->quiesce_counter > 0);
412     GLOBAL_STATE_CODE();
413 
414     /* Re-enable things in child-to-parent order */
415     old_quiesce_counter = qatomic_fetch_dec(&bs->quiesce_counter);
416     if (old_quiesce_counter == 1) {
417         GRAPH_RDLOCK_GUARD_MAINLOOP();
418         if (bs->drv && bs->drv->bdrv_drain_end) {
419             bs->drv->bdrv_drain_end(bs);
420         }
421         bdrv_parent_drained_end(bs, parent);
422     }
423 }
424 
425 void bdrv_drained_end(BlockDriverState *bs)
426 {
427     IO_OR_GS_CODE();
428     bdrv_do_drained_end(bs, NULL);
429 }
430 
431 void bdrv_drain(BlockDriverState *bs)
432 {
433     IO_OR_GS_CODE();
434     bdrv_drained_begin(bs);
435     bdrv_drained_end(bs);
436 }
437 
438 static void bdrv_drain_assert_idle(BlockDriverState *bs)
439 {
440     BdrvChild *child, *next;
441     GLOBAL_STATE_CODE();
442     GRAPH_RDLOCK_GUARD_MAINLOOP();
443 
444     assert(qatomic_read(&bs->in_flight) == 0);
445     QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
446         bdrv_drain_assert_idle(child->bs);
447     }
448 }
449 
450 unsigned int bdrv_drain_all_count = 0;
451 
452 static bool bdrv_drain_all_poll(void)
453 {
454     BlockDriverState *bs = NULL;
455     bool result = false;
456 
457     GLOBAL_STATE_CODE();
458     GRAPH_RDLOCK_GUARD_MAINLOOP();
459 
460     /*
461      * bdrv_drain_poll() can't make changes to the graph and we hold the BQL,
462      * so iterating bdrv_next_all_states() is safe.
463      */
464     while ((bs = bdrv_next_all_states(bs))) {
465         result |= bdrv_drain_poll(bs, NULL, true);
466     }
467 
468     return result;
469 }
470 
471 /*
472  * Wait for pending requests to complete across all BlockDriverStates
473  *
474  * This function does not flush data to disk, use bdrv_flush_all() for that
475  * after calling this function.
476  *
477  * This pauses all block jobs and disables external clients. It must
478  * be paired with bdrv_drain_all_end().
479  *
480  * NOTE: no new block jobs or BlockDriverStates can be created between
481  * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
482  */
483 void bdrv_drain_all_begin_nopoll(void)
484 {
485     BlockDriverState *bs = NULL;
486     GLOBAL_STATE_CODE();
487 
488     /*
489      * bdrv queue is managed by record/replay,
490      * waiting for finishing the I/O requests may
491      * be infinite
492      */
493     if (replay_events_enabled()) {
494         return;
495     }
496 
497     /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
498      * loop AioContext, so make sure we're in the main context. */
499     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
500     assert(bdrv_drain_all_count < INT_MAX);
501     bdrv_drain_all_count++;
502 
503     /* Quiesce all nodes, without polling in-flight requests yet. The graph
504      * cannot change during this loop. */
505     while ((bs = bdrv_next_all_states(bs))) {
506         bdrv_do_drained_begin(bs, NULL, false);
507     }
508 }
509 
510 void coroutine_mixed_fn bdrv_drain_all_begin(void)
511 {
512     BlockDriverState *bs = NULL;
513 
514     if (qemu_in_coroutine()) {
515         bdrv_co_yield_to_drain(NULL, true, NULL, true);
516         return;
517     }
518 
519     /*
520      * bdrv queue is managed by record/replay,
521      * waiting for finishing the I/O requests may
522      * be infinite
523      */
524     if (replay_events_enabled()) {
525         return;
526     }
527 
528     bdrv_drain_all_begin_nopoll();
529 
530     /* Now poll the in-flight requests */
531     AIO_WAIT_WHILE_UNLOCKED(NULL, bdrv_drain_all_poll());
532 
533     while ((bs = bdrv_next_all_states(bs))) {
534         bdrv_drain_assert_idle(bs);
535     }
536 }
537 
538 void bdrv_drain_all_end_quiesce(BlockDriverState *bs)
539 {
540     GLOBAL_STATE_CODE();
541 
542     g_assert(bs->quiesce_counter > 0);
543     g_assert(!bs->refcnt);
544 
545     while (bs->quiesce_counter) {
546         bdrv_do_drained_end(bs, NULL);
547     }
548 }
549 
550 void bdrv_drain_all_end(void)
551 {
552     BlockDriverState *bs = NULL;
553     GLOBAL_STATE_CODE();
554 
555     /*
556      * bdrv queue is managed by record/replay,
557      * waiting for finishing the I/O requests may
558      * be endless
559      */
560     if (replay_events_enabled()) {
561         return;
562     }
563 
564     while ((bs = bdrv_next_all_states(bs))) {
565         bdrv_do_drained_end(bs, NULL);
566     }
567 
568     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
569     assert(bdrv_drain_all_count > 0);
570     bdrv_drain_all_count--;
571 }
572 
573 void bdrv_drain_all(void)
574 {
575     GLOBAL_STATE_CODE();
576     bdrv_drain_all_begin();
577     bdrv_drain_all_end();
578 }
579 
580 /**
581  * Remove an active request from the tracked requests list
582  *
583  * This function should be called when a tracked request is completing.
584  */
585 static void coroutine_fn tracked_request_end(BdrvTrackedRequest *req)
586 {
587     if (req->serialising) {
588         qatomic_dec(&req->bs->serialising_in_flight);
589     }
590 
591     qemu_mutex_lock(&req->bs->reqs_lock);
592     QLIST_REMOVE(req, list);
593     qemu_mutex_unlock(&req->bs->reqs_lock);
594 
595     /*
596      * At this point qemu_co_queue_wait(&req->wait_queue, ...) won't be called
597      * anymore because the request has been removed from the list, so it's safe
598      * to restart the queue outside reqs_lock to minimize the critical section.
599      */
600     qemu_co_queue_restart_all(&req->wait_queue);
601 }
602 
603 /**
604  * Add an active request to the tracked requests list
605  */
606 static void coroutine_fn tracked_request_begin(BdrvTrackedRequest *req,
607                                                BlockDriverState *bs,
608                                                int64_t offset,
609                                                int64_t bytes,
610                                                enum BdrvTrackedRequestType type)
611 {
612     bdrv_check_request(offset, bytes, &error_abort);
613 
614     *req = (BdrvTrackedRequest){
615         .bs = bs,
616         .offset         = offset,
617         .bytes          = bytes,
618         .type           = type,
619         .co             = qemu_coroutine_self(),
620         .serialising    = false,
621         .overlap_offset = offset,
622         .overlap_bytes  = bytes,
623     };
624 
625     qemu_co_queue_init(&req->wait_queue);
626 
627     qemu_mutex_lock(&bs->reqs_lock);
628     QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
629     qemu_mutex_unlock(&bs->reqs_lock);
630 }
631 
632 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
633                                      int64_t offset, int64_t bytes)
634 {
635     bdrv_check_request(offset, bytes, &error_abort);
636 
637     /*        aaaa   bbbb */
638     if (offset >= req->overlap_offset + req->overlap_bytes) {
639         return false;
640     }
641     /* bbbb   aaaa        */
642     if (req->overlap_offset >= offset + bytes) {
643         return false;
644     }
645     return true;
646 }
647 
648 /* Called with self->bs->reqs_lock held */
649 static coroutine_fn BdrvTrackedRequest *
650 bdrv_find_conflicting_request(BdrvTrackedRequest *self)
651 {
652     BdrvTrackedRequest *req;
653 
654     QLIST_FOREACH(req, &self->bs->tracked_requests, list) {
655         if (req == self || (!req->serialising && !self->serialising)) {
656             continue;
657         }
658         if (tracked_request_overlaps(req, self->overlap_offset,
659                                      self->overlap_bytes))
660         {
661             /*
662              * Hitting this means there was a reentrant request, for
663              * example, a block driver issuing nested requests.  This must
664              * never happen since it means deadlock.
665              */
666             assert(qemu_coroutine_self() != req->co);
667 
668             /*
669              * If the request is already (indirectly) waiting for us, or
670              * will wait for us as soon as it wakes up, then just go on
671              * (instead of producing a deadlock in the former case).
672              */
673             if (!req->waiting_for) {
674                 return req;
675             }
676         }
677     }
678 
679     return NULL;
680 }
681 
682 /* Called with self->bs->reqs_lock held */
683 static void coroutine_fn
684 bdrv_wait_serialising_requests_locked(BdrvTrackedRequest *self)
685 {
686     BdrvTrackedRequest *req;
687 
688     while ((req = bdrv_find_conflicting_request(self))) {
689         self->waiting_for = req;
690         qemu_co_queue_wait(&req->wait_queue, &self->bs->reqs_lock);
691         self->waiting_for = NULL;
692     }
693 }
694 
695 /* Called with req->bs->reqs_lock held */
696 static void tracked_request_set_serialising(BdrvTrackedRequest *req,
697                                             uint64_t align)
698 {
699     int64_t overlap_offset = req->offset & ~(align - 1);
700     int64_t overlap_bytes =
701         ROUND_UP(req->offset + req->bytes, align) - overlap_offset;
702 
703     bdrv_check_request(req->offset, req->bytes, &error_abort);
704 
705     if (!req->serialising) {
706         qatomic_inc(&req->bs->serialising_in_flight);
707         req->serialising = true;
708     }
709 
710     req->overlap_offset = MIN(req->overlap_offset, overlap_offset);
711     req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes);
712 }
713 
714 /**
715  * Return the tracked request on @bs for the current coroutine, or
716  * NULL if there is none.
717  */
718 BdrvTrackedRequest *coroutine_fn bdrv_co_get_self_request(BlockDriverState *bs)
719 {
720     BdrvTrackedRequest *req;
721     Coroutine *self = qemu_coroutine_self();
722     IO_CODE();
723 
724     QLIST_FOREACH(req, &bs->tracked_requests, list) {
725         if (req->co == self) {
726             return req;
727         }
728     }
729 
730     return NULL;
731 }
732 
733 /**
734  * Round a region to subcluster (if supported) or cluster boundaries
735  */
736 void coroutine_fn GRAPH_RDLOCK
737 bdrv_round_to_subclusters(BlockDriverState *bs, int64_t offset, int64_t bytes,
738                           int64_t *align_offset, int64_t *align_bytes)
739 {
740     BlockDriverInfo bdi;
741     IO_CODE();
742     if (bdrv_co_get_info(bs, &bdi) < 0 || bdi.subcluster_size == 0) {
743         *align_offset = offset;
744         *align_bytes = bytes;
745     } else {
746         int64_t c = bdi.subcluster_size;
747         *align_offset = QEMU_ALIGN_DOWN(offset, c);
748         *align_bytes = QEMU_ALIGN_UP(offset - *align_offset + bytes, c);
749     }
750 }
751 
752 static int coroutine_fn GRAPH_RDLOCK bdrv_get_cluster_size(BlockDriverState *bs)
753 {
754     BlockDriverInfo bdi;
755     int ret;
756 
757     ret = bdrv_co_get_info(bs, &bdi);
758     if (ret < 0 || bdi.cluster_size == 0) {
759         return bs->bl.request_alignment;
760     } else {
761         return bdi.cluster_size;
762     }
763 }
764 
765 void bdrv_inc_in_flight(BlockDriverState *bs)
766 {
767     IO_CODE();
768     qatomic_inc(&bs->in_flight);
769 }
770 
771 void bdrv_wakeup(BlockDriverState *bs)
772 {
773     IO_CODE();
774     aio_wait_kick();
775 }
776 
777 void bdrv_dec_in_flight(BlockDriverState *bs)
778 {
779     IO_CODE();
780     qatomic_dec(&bs->in_flight);
781     bdrv_wakeup(bs);
782 }
783 
784 static void coroutine_fn
785 bdrv_wait_serialising_requests(BdrvTrackedRequest *self)
786 {
787     BlockDriverState *bs = self->bs;
788 
789     if (!qatomic_read(&bs->serialising_in_flight)) {
790         return;
791     }
792 
793     qemu_mutex_lock(&bs->reqs_lock);
794     bdrv_wait_serialising_requests_locked(self);
795     qemu_mutex_unlock(&bs->reqs_lock);
796 }
797 
798 void coroutine_fn bdrv_make_request_serialising(BdrvTrackedRequest *req,
799                                                 uint64_t align)
800 {
801     IO_CODE();
802 
803     qemu_mutex_lock(&req->bs->reqs_lock);
804 
805     tracked_request_set_serialising(req, align);
806     bdrv_wait_serialising_requests_locked(req);
807 
808     qemu_mutex_unlock(&req->bs->reqs_lock);
809 }
810 
811 int bdrv_check_qiov_request(int64_t offset, int64_t bytes,
812                             QEMUIOVector *qiov, size_t qiov_offset,
813                             Error **errp)
814 {
815     /*
816      * Check generic offset/bytes correctness
817      */
818 
819     if (offset < 0) {
820         error_setg(errp, "offset is negative: %" PRIi64, offset);
821         return -EIO;
822     }
823 
824     if (bytes < 0) {
825         error_setg(errp, "bytes is negative: %" PRIi64, bytes);
826         return -EIO;
827     }
828 
829     if (bytes > BDRV_MAX_LENGTH) {
830         error_setg(errp, "bytes(%" PRIi64 ") exceeds maximum(%" PRIi64 ")",
831                    bytes, BDRV_MAX_LENGTH);
832         return -EIO;
833     }
834 
835     if (offset > BDRV_MAX_LENGTH) {
836         error_setg(errp, "offset(%" PRIi64 ") exceeds maximum(%" PRIi64 ")",
837                    offset, BDRV_MAX_LENGTH);
838         return -EIO;
839     }
840 
841     if (offset > BDRV_MAX_LENGTH - bytes) {
842         error_setg(errp, "sum of offset(%" PRIi64 ") and bytes(%" PRIi64 ") "
843                    "exceeds maximum(%" PRIi64 ")", offset, bytes,
844                    BDRV_MAX_LENGTH);
845         return -EIO;
846     }
847 
848     if (!qiov) {
849         return 0;
850     }
851 
852     /*
853      * Check qiov and qiov_offset
854      */
855 
856     if (qiov_offset > qiov->size) {
857         error_setg(errp, "qiov_offset(%zu) overflow io vector size(%zu)",
858                    qiov_offset, qiov->size);
859         return -EIO;
860     }
861 
862     if (bytes > qiov->size - qiov_offset) {
863         error_setg(errp, "bytes(%" PRIi64 ") + qiov_offset(%zu) overflow io "
864                    "vector size(%zu)", bytes, qiov_offset, qiov->size);
865         return -EIO;
866     }
867 
868     return 0;
869 }
870 
871 int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp)
872 {
873     return bdrv_check_qiov_request(offset, bytes, NULL, 0, errp);
874 }
875 
876 static int bdrv_check_request32(int64_t offset, int64_t bytes,
877                                 QEMUIOVector *qiov, size_t qiov_offset)
878 {
879     int ret = bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, NULL);
880     if (ret < 0) {
881         return ret;
882     }
883 
884     if (bytes > BDRV_REQUEST_MAX_BYTES) {
885         return -EIO;
886     }
887 
888     return 0;
889 }
890 
891 /*
892  * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
893  * The operation is sped up by checking the block status and only writing
894  * zeroes to the device if they currently do not return zeroes. Optional
895  * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
896  * BDRV_REQ_FUA).
897  *
898  * Returns < 0 on error, 0 on success. For error codes see bdrv_pwrite().
899  */
900 int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
901 {
902     int ret;
903     int64_t target_size, bytes, offset = 0;
904     BlockDriverState *bs = child->bs;
905     IO_CODE();
906 
907     target_size = bdrv_getlength(bs);
908     if (target_size < 0) {
909         return target_size;
910     }
911 
912     for (;;) {
913         bytes = MIN(target_size - offset, BDRV_REQUEST_MAX_BYTES);
914         if (bytes <= 0) {
915             return 0;
916         }
917         ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
918         if (ret < 0) {
919             return ret;
920         }
921         if (ret & BDRV_BLOCK_ZERO) {
922             offset += bytes;
923             continue;
924         }
925         ret = bdrv_pwrite_zeroes(child, offset, bytes, flags);
926         if (ret < 0) {
927             return ret;
928         }
929         offset += bytes;
930     }
931 }
932 
933 /*
934  * Writes to the file and ensures that no writes are reordered across this
935  * request (acts as a barrier)
936  *
937  * Returns 0 on success, -errno in error cases.
938  */
939 int coroutine_fn bdrv_co_pwrite_sync(BdrvChild *child, int64_t offset,
940                                      int64_t bytes, const void *buf,
941                                      BdrvRequestFlags flags)
942 {
943     int ret;
944     IO_CODE();
945     assert_bdrv_graph_readable();
946 
947     ret = bdrv_co_pwrite(child, offset, bytes, buf, flags);
948     if (ret < 0) {
949         return ret;
950     }
951 
952     ret = bdrv_co_flush(child->bs);
953     if (ret < 0) {
954         return ret;
955     }
956 
957     return 0;
958 }
959 
960 typedef struct CoroutineIOCompletion {
961     Coroutine *coroutine;
962     int ret;
963 } CoroutineIOCompletion;
964 
965 static void bdrv_co_io_em_complete(void *opaque, int ret)
966 {
967     CoroutineIOCompletion *co = opaque;
968 
969     co->ret = ret;
970     aio_co_wake(co->coroutine);
971 }
972 
973 static int coroutine_fn GRAPH_RDLOCK
974 bdrv_driver_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
975                    QEMUIOVector *qiov, size_t qiov_offset, int flags)
976 {
977     BlockDriver *drv = bs->drv;
978     int64_t sector_num;
979     unsigned int nb_sectors;
980     QEMUIOVector local_qiov;
981     int ret;
982     assert_bdrv_graph_readable();
983 
984     bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
985     assert(!(flags & ~bs->supported_read_flags));
986 
987     if (!drv) {
988         return -ENOMEDIUM;
989     }
990 
991     if (drv->bdrv_co_preadv_part) {
992         return drv->bdrv_co_preadv_part(bs, offset, bytes, qiov, qiov_offset,
993                                         flags);
994     }
995 
996     if (qiov_offset > 0 || bytes != qiov->size) {
997         qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes);
998         qiov = &local_qiov;
999     }
1000 
1001     if (drv->bdrv_co_preadv) {
1002         ret = drv->bdrv_co_preadv(bs, offset, bytes, qiov, flags);
1003         goto out;
1004     }
1005 
1006     if (drv->bdrv_aio_preadv) {
1007         BlockAIOCB *acb;
1008         CoroutineIOCompletion co = {
1009             .coroutine = qemu_coroutine_self(),
1010         };
1011 
1012         acb = drv->bdrv_aio_preadv(bs, offset, bytes, qiov, flags,
1013                                    bdrv_co_io_em_complete, &co);
1014         if (acb == NULL) {
1015             ret = -EIO;
1016             goto out;
1017         } else {
1018             qemu_coroutine_yield();
1019             ret = co.ret;
1020             goto out;
1021         }
1022     }
1023 
1024     sector_num = offset >> BDRV_SECTOR_BITS;
1025     nb_sectors = bytes >> BDRV_SECTOR_BITS;
1026 
1027     assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
1028     assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
1029     assert(bytes <= BDRV_REQUEST_MAX_BYTES);
1030     assert(drv->bdrv_co_readv);
1031 
1032     ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1033 
1034 out:
1035     if (qiov == &local_qiov) {
1036         qemu_iovec_destroy(&local_qiov);
1037     }
1038 
1039     return ret;
1040 }
1041 
1042 static int coroutine_fn GRAPH_RDLOCK
1043 bdrv_driver_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
1044                     QEMUIOVector *qiov, size_t qiov_offset,
1045                     BdrvRequestFlags flags)
1046 {
1047     BlockDriver *drv = bs->drv;
1048     bool emulate_fua = false;
1049     int64_t sector_num;
1050     unsigned int nb_sectors;
1051     QEMUIOVector local_qiov;
1052     int ret;
1053     assert_bdrv_graph_readable();
1054 
1055     bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
1056 
1057     if (!drv) {
1058         return -ENOMEDIUM;
1059     }
1060 
1061     if ((flags & BDRV_REQ_FUA) &&
1062         (~bs->supported_write_flags & BDRV_REQ_FUA)) {
1063         flags &= ~BDRV_REQ_FUA;
1064         emulate_fua = true;
1065     }
1066 
1067     flags &= bs->supported_write_flags;
1068 
1069     if (drv->bdrv_co_pwritev_part) {
1070         ret = drv->bdrv_co_pwritev_part(bs, offset, bytes, qiov, qiov_offset,
1071                                         flags);
1072         goto emulate_flags;
1073     }
1074 
1075     if (qiov_offset > 0 || bytes != qiov->size) {
1076         qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes);
1077         qiov = &local_qiov;
1078     }
1079 
1080     if (drv->bdrv_co_pwritev) {
1081         ret = drv->bdrv_co_pwritev(bs, offset, bytes, qiov, flags);
1082         goto emulate_flags;
1083     }
1084 
1085     if (drv->bdrv_aio_pwritev) {
1086         BlockAIOCB *acb;
1087         CoroutineIOCompletion co = {
1088             .coroutine = qemu_coroutine_self(),
1089         };
1090 
1091         acb = drv->bdrv_aio_pwritev(bs, offset, bytes, qiov, flags,
1092                                     bdrv_co_io_em_complete, &co);
1093         if (acb == NULL) {
1094             ret = -EIO;
1095         } else {
1096             qemu_coroutine_yield();
1097             ret = co.ret;
1098         }
1099         goto emulate_flags;
1100     }
1101 
1102     sector_num = offset >> BDRV_SECTOR_BITS;
1103     nb_sectors = bytes >> BDRV_SECTOR_BITS;
1104 
1105     assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
1106     assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
1107     assert(bytes <= BDRV_REQUEST_MAX_BYTES);
1108 
1109     assert(drv->bdrv_co_writev);
1110     ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov, flags);
1111 
1112 emulate_flags:
1113     if (ret == 0 && emulate_fua) {
1114         ret = bdrv_co_flush(bs);
1115     }
1116 
1117     if (qiov == &local_qiov) {
1118         qemu_iovec_destroy(&local_qiov);
1119     }
1120 
1121     return ret;
1122 }
1123 
1124 static int coroutine_fn GRAPH_RDLOCK
1125 bdrv_driver_pwritev_compressed(BlockDriverState *bs, int64_t offset,
1126                                int64_t bytes, QEMUIOVector *qiov,
1127                                size_t qiov_offset)
1128 {
1129     BlockDriver *drv = bs->drv;
1130     QEMUIOVector local_qiov;
1131     int ret;
1132     assert_bdrv_graph_readable();
1133 
1134     bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
1135 
1136     if (!drv) {
1137         return -ENOMEDIUM;
1138     }
1139 
1140     if (!block_driver_can_compress(drv)) {
1141         return -ENOTSUP;
1142     }
1143 
1144     if (drv->bdrv_co_pwritev_compressed_part) {
1145         return drv->bdrv_co_pwritev_compressed_part(bs, offset, bytes,
1146                                                     qiov, qiov_offset);
1147     }
1148 
1149     if (qiov_offset == 0) {
1150         return drv->bdrv_co_pwritev_compressed(bs, offset, bytes, qiov);
1151     }
1152 
1153     qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes);
1154     ret = drv->bdrv_co_pwritev_compressed(bs, offset, bytes, &local_qiov);
1155     qemu_iovec_destroy(&local_qiov);
1156 
1157     return ret;
1158 }
1159 
1160 static int coroutine_fn GRAPH_RDLOCK
1161 bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t offset, int64_t bytes,
1162                          QEMUIOVector *qiov, size_t qiov_offset, int flags)
1163 {
1164     BlockDriverState *bs = child->bs;
1165 
1166     /* Perform I/O through a temporary buffer so that users who scribble over
1167      * their read buffer while the operation is in progress do not end up
1168      * modifying the image file.  This is critical for zero-copy guest I/O
1169      * where anything might happen inside guest memory.
1170      */
1171     void *bounce_buffer = NULL;
1172 
1173     BlockDriver *drv = bs->drv;
1174     int64_t align_offset;
1175     int64_t align_bytes;
1176     int64_t skip_bytes;
1177     int ret;
1178     int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer,
1179                                     BDRV_REQUEST_MAX_BYTES);
1180     int64_t progress = 0;
1181     bool skip_write;
1182 
1183     bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
1184 
1185     if (!drv) {
1186         return -ENOMEDIUM;
1187     }
1188 
1189     /*
1190      * Do not write anything when the BDS is inactive.  That is not
1191      * allowed, and it would not help.
1192      */
1193     skip_write = (bs->open_flags & BDRV_O_INACTIVE);
1194 
1195     /* FIXME We cannot require callers to have write permissions when all they
1196      * are doing is a read request. If we did things right, write permissions
1197      * would be obtained anyway, but internally by the copy-on-read code. As
1198      * long as it is implemented here rather than in a separate filter driver,
1199      * the copy-on-read code doesn't have its own BdrvChild, however, for which
1200      * it could request permissions. Therefore we have to bypass the permission
1201      * system for the moment. */
1202     // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1203 
1204     /* Cover entire cluster so no additional backing file I/O is required when
1205      * allocating cluster in the image file.  Note that this value may exceed
1206      * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1207      * is one reason we loop rather than doing it all at once.
1208      */
1209     bdrv_round_to_subclusters(bs, offset, bytes, &align_offset, &align_bytes);
1210     skip_bytes = offset - align_offset;
1211 
1212     trace_bdrv_co_do_copy_on_readv(bs, offset, bytes,
1213                                    align_offset, align_bytes);
1214 
1215     while (align_bytes) {
1216         int64_t pnum;
1217 
1218         if (skip_write) {
1219             ret = 1; /* "already allocated", so nothing will be copied */
1220             pnum = MIN(align_bytes, max_transfer);
1221         } else {
1222             ret = bdrv_co_is_allocated(bs, align_offset,
1223                                        MIN(align_bytes, max_transfer), &pnum);
1224             if (ret < 0) {
1225                 /*
1226                  * Safe to treat errors in querying allocation as if
1227                  * unallocated; we'll probably fail again soon on the
1228                  * read, but at least that will set a decent errno.
1229                  */
1230                 pnum = MIN(align_bytes, max_transfer);
1231             }
1232 
1233             /* Stop at EOF if the image ends in the middle of the cluster */
1234             if (ret == 0 && pnum == 0) {
1235                 assert(progress >= bytes);
1236                 break;
1237             }
1238 
1239             assert(skip_bytes < pnum);
1240         }
1241 
1242         if (ret <= 0) {
1243             QEMUIOVector local_qiov;
1244 
1245             /* Must copy-on-read; use the bounce buffer */
1246             pnum = MIN(pnum, MAX_BOUNCE_BUFFER);
1247             if (!bounce_buffer) {
1248                 int64_t max_we_need = MAX(pnum, align_bytes - pnum);
1249                 int64_t max_allowed = MIN(max_transfer, MAX_BOUNCE_BUFFER);
1250                 int64_t bounce_buffer_len = MIN(max_we_need, max_allowed);
1251 
1252                 bounce_buffer = qemu_try_blockalign(bs, bounce_buffer_len);
1253                 if (!bounce_buffer) {
1254                     ret = -ENOMEM;
1255                     goto err;
1256                 }
1257             }
1258             qemu_iovec_init_buf(&local_qiov, bounce_buffer, pnum);
1259 
1260             ret = bdrv_driver_preadv(bs, align_offset, pnum,
1261                                      &local_qiov, 0, 0);
1262             if (ret < 0) {
1263                 goto err;
1264             }
1265 
1266             bdrv_co_debug_event(bs, BLKDBG_COR_WRITE);
1267             if (drv->bdrv_co_pwrite_zeroes &&
1268                 buffer_is_zero(bounce_buffer, pnum)) {
1269                 /* FIXME: Should we (perhaps conditionally) be setting
1270                  * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1271                  * that still correctly reads as zero? */
1272                 ret = bdrv_co_do_pwrite_zeroes(bs, align_offset, pnum,
1273                                                BDRV_REQ_WRITE_UNCHANGED);
1274             } else {
1275                 /* This does not change the data on the disk, it is not
1276                  * necessary to flush even in cache=writethrough mode.
1277                  */
1278                 ret = bdrv_driver_pwritev(bs, align_offset, pnum,
1279                                           &local_qiov, 0,
1280                                           BDRV_REQ_WRITE_UNCHANGED);
1281             }
1282 
1283             if (ret < 0) {
1284                 /* It might be okay to ignore write errors for guest
1285                  * requests.  If this is a deliberate copy-on-read
1286                  * then we don't want to ignore the error.  Simply
1287                  * report it in all cases.
1288                  */
1289                 goto err;
1290             }
1291 
1292             if (!(flags & BDRV_REQ_PREFETCH)) {
1293                 qemu_iovec_from_buf(qiov, qiov_offset + progress,
1294                                     bounce_buffer + skip_bytes,
1295                                     MIN(pnum - skip_bytes, bytes - progress));
1296             }
1297         } else if (!(flags & BDRV_REQ_PREFETCH)) {
1298             /* Read directly into the destination */
1299             ret = bdrv_driver_preadv(bs, offset + progress,
1300                                      MIN(pnum - skip_bytes, bytes - progress),
1301                                      qiov, qiov_offset + progress, 0);
1302             if (ret < 0) {
1303                 goto err;
1304             }
1305         }
1306 
1307         align_offset += pnum;
1308         align_bytes -= pnum;
1309         progress += pnum - skip_bytes;
1310         skip_bytes = 0;
1311     }
1312     ret = 0;
1313 
1314 err:
1315     qemu_vfree(bounce_buffer);
1316     return ret;
1317 }
1318 
1319 /*
1320  * Forwards an already correctly aligned request to the BlockDriver. This
1321  * handles copy on read, zeroing after EOF, and fragmentation of large
1322  * reads; any other features must be implemented by the caller.
1323  */
1324 static int coroutine_fn GRAPH_RDLOCK
1325 bdrv_aligned_preadv(BdrvChild *child, BdrvTrackedRequest *req,
1326                     int64_t offset, int64_t bytes, int64_t align,
1327                     QEMUIOVector *qiov, size_t qiov_offset, int flags)
1328 {
1329     BlockDriverState *bs = child->bs;
1330     int64_t total_bytes, max_bytes;
1331     int ret = 0;
1332     int64_t bytes_remaining = bytes;
1333     int max_transfer;
1334 
1335     bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
1336     assert(is_power_of_2(align));
1337     assert((offset & (align - 1)) == 0);
1338     assert((bytes & (align - 1)) == 0);
1339     assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1340     max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
1341                                    align);
1342 
1343     /*
1344      * TODO: We would need a per-BDS .supported_read_flags and
1345      * potential fallback support, if we ever implement any read flags
1346      * to pass through to drivers.  For now, there aren't any
1347      * passthrough flags except the BDRV_REQ_REGISTERED_BUF optimization hint.
1348      */
1349     assert(!(flags & ~(BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH |
1350                        BDRV_REQ_REGISTERED_BUF)));
1351 
1352     /* Handle Copy on Read and associated serialisation */
1353     if (flags & BDRV_REQ_COPY_ON_READ) {
1354         /* If we touch the same cluster it counts as an overlap.  This
1355          * guarantees that allocating writes will be serialized and not race
1356          * with each other for the same cluster.  For example, in copy-on-read
1357          * it ensures that the CoR read and write operations are atomic and
1358          * guest writes cannot interleave between them. */
1359         bdrv_make_request_serialising(req, bdrv_get_cluster_size(bs));
1360     } else {
1361         bdrv_wait_serialising_requests(req);
1362     }
1363 
1364     if (flags & BDRV_REQ_COPY_ON_READ) {
1365         int64_t pnum;
1366 
1367         /* The flag BDRV_REQ_COPY_ON_READ has reached its addressee */
1368         flags &= ~BDRV_REQ_COPY_ON_READ;
1369 
1370         ret = bdrv_co_is_allocated(bs, offset, bytes, &pnum);
1371         if (ret < 0) {
1372             goto out;
1373         }
1374 
1375         if (!ret || pnum != bytes) {
1376             ret = bdrv_co_do_copy_on_readv(child, offset, bytes,
1377                                            qiov, qiov_offset, flags);
1378             goto out;
1379         } else if (flags & BDRV_REQ_PREFETCH) {
1380             goto out;
1381         }
1382     }
1383 
1384     /* Forward the request to the BlockDriver, possibly fragmenting it */
1385     total_bytes = bdrv_co_getlength(bs);
1386     if (total_bytes < 0) {
1387         ret = total_bytes;
1388         goto out;
1389     }
1390 
1391     assert(!(flags & ~(bs->supported_read_flags | BDRV_REQ_REGISTERED_BUF)));
1392 
1393     max_bytes = ROUND_UP(MAX(0, total_bytes - offset), align);
1394     if (bytes <= max_bytes && bytes <= max_transfer) {
1395         ret = bdrv_driver_preadv(bs, offset, bytes, qiov, qiov_offset, flags);
1396         goto out;
1397     }
1398 
1399     while (bytes_remaining) {
1400         int64_t num;
1401 
1402         if (max_bytes) {
1403             num = MIN(bytes_remaining, MIN(max_bytes, max_transfer));
1404             assert(num);
1405 
1406             ret = bdrv_driver_preadv(bs, offset + bytes - bytes_remaining,
1407                                      num, qiov,
1408                                      qiov_offset + bytes - bytes_remaining,
1409                                      flags);
1410             max_bytes -= num;
1411         } else {
1412             num = bytes_remaining;
1413             ret = qemu_iovec_memset(qiov, qiov_offset + bytes - bytes_remaining,
1414                                     0, bytes_remaining);
1415         }
1416         if (ret < 0) {
1417             goto out;
1418         }
1419         bytes_remaining -= num;
1420     }
1421 
1422 out:
1423     return ret < 0 ? ret : 0;
1424 }
1425 
1426 /*
1427  * Request padding
1428  *
1429  *  |<---- align ----->|                     |<----- align ---->|
1430  *  |<- head ->|<------------- bytes ------------->|<-- tail -->|
1431  *  |          |       |                     |     |            |
1432  * -*----------$-------*-------- ... --------*-----$------------*---
1433  *  |          |       |                     |     |            |
1434  *  |          offset  |                     |     end          |
1435  *  ALIGN_DOWN(offset) ALIGN_UP(offset)      ALIGN_DOWN(end)   ALIGN_UP(end)
1436  *  [buf   ... )                             [tail_buf          )
1437  *
1438  * @buf is an aligned allocation needed to store @head and @tail paddings. @head
1439  * is placed at the beginning of @buf and @tail at the @end.
1440  *
1441  * @tail_buf is a pointer to sub-buffer, corresponding to align-sized chunk
1442  * around tail, if tail exists.
1443  *
1444  * @merge_reads is true for small requests,
1445  * if @buf_len == @head + bytes + @tail. In this case it is possible that both
1446  * head and tail exist but @buf_len == align and @tail_buf == @buf.
1447  *
1448  * @write is true for write requests, false for read requests.
1449  *
1450  * If padding makes the vector too long (exceeding IOV_MAX), then we need to
1451  * merge existing vector elements into a single one.  @collapse_bounce_buf acts
1452  * as the bounce buffer in such cases.  @pre_collapse_qiov has the pre-collapse
1453  * I/O vector elements so for read requests, the data can be copied back after
1454  * the read is done.
1455  */
1456 typedef struct BdrvRequestPadding {
1457     uint8_t *buf;
1458     size_t buf_len;
1459     uint8_t *tail_buf;
1460     size_t head;
1461     size_t tail;
1462     bool merge_reads;
1463     bool write;
1464     QEMUIOVector local_qiov;
1465 
1466     uint8_t *collapse_bounce_buf;
1467     size_t collapse_len;
1468     QEMUIOVector pre_collapse_qiov;
1469 } BdrvRequestPadding;
1470 
1471 static bool bdrv_init_padding(BlockDriverState *bs,
1472                               int64_t offset, int64_t bytes,
1473                               bool write,
1474                               BdrvRequestPadding *pad)
1475 {
1476     int64_t align = bs->bl.request_alignment;
1477     int64_t sum;
1478 
1479     bdrv_check_request(offset, bytes, &error_abort);
1480     assert(align <= INT_MAX); /* documented in block/block_int.h */
1481     assert(align <= SIZE_MAX / 2); /* so we can allocate the buffer */
1482 
1483     memset(pad, 0, sizeof(*pad));
1484 
1485     pad->head = offset & (align - 1);
1486     pad->tail = ((offset + bytes) & (align - 1));
1487     if (pad->tail) {
1488         pad->tail = align - pad->tail;
1489     }
1490 
1491     if (!pad->head && !pad->tail) {
1492         return false;
1493     }
1494 
1495     assert(bytes); /* Nothing good in aligning zero-length requests */
1496 
1497     sum = pad->head + bytes + pad->tail;
1498     pad->buf_len = (sum > align && pad->head && pad->tail) ? 2 * align : align;
1499     pad->buf = qemu_blockalign(bs, pad->buf_len);
1500     pad->merge_reads = sum == pad->buf_len;
1501     if (pad->tail) {
1502         pad->tail_buf = pad->buf + pad->buf_len - align;
1503     }
1504 
1505     pad->write = write;
1506 
1507     return true;
1508 }
1509 
1510 static int coroutine_fn GRAPH_RDLOCK
1511 bdrv_padding_rmw_read(BdrvChild *child, BdrvTrackedRequest *req,
1512                       BdrvRequestPadding *pad, bool zero_middle)
1513 {
1514     QEMUIOVector local_qiov;
1515     BlockDriverState *bs = child->bs;
1516     uint64_t align = bs->bl.request_alignment;
1517     int ret;
1518 
1519     assert(req->serialising && pad->buf);
1520 
1521     if (pad->head || pad->merge_reads) {
1522         int64_t bytes = pad->merge_reads ? pad->buf_len : align;
1523 
1524         qemu_iovec_init_buf(&local_qiov, pad->buf, bytes);
1525 
1526         if (pad->head) {
1527             bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
1528         }
1529         if (pad->merge_reads && pad->tail) {
1530             bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
1531         }
1532         ret = bdrv_aligned_preadv(child, req, req->overlap_offset, bytes,
1533                                   align, &local_qiov, 0, 0);
1534         if (ret < 0) {
1535             return ret;
1536         }
1537         if (pad->head) {
1538             bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
1539         }
1540         if (pad->merge_reads && pad->tail) {
1541             bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
1542         }
1543 
1544         if (pad->merge_reads) {
1545             goto zero_mem;
1546         }
1547     }
1548 
1549     if (pad->tail) {
1550         qemu_iovec_init_buf(&local_qiov, pad->tail_buf, align);
1551 
1552         bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
1553         ret = bdrv_aligned_preadv(
1554                 child, req,
1555                 req->overlap_offset + req->overlap_bytes - align,
1556                 align, align, &local_qiov, 0, 0);
1557         if (ret < 0) {
1558             return ret;
1559         }
1560         bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
1561     }
1562 
1563 zero_mem:
1564     if (zero_middle) {
1565         memset(pad->buf + pad->head, 0, pad->buf_len - pad->head - pad->tail);
1566     }
1567 
1568     return 0;
1569 }
1570 
1571 /**
1572  * Free *pad's associated buffers, and perform any necessary finalization steps.
1573  */
1574 static void bdrv_padding_finalize(BdrvRequestPadding *pad)
1575 {
1576     if (pad->collapse_bounce_buf) {
1577         if (!pad->write) {
1578             /*
1579              * If padding required elements in the vector to be collapsed into a
1580              * bounce buffer, copy the bounce buffer content back
1581              */
1582             qemu_iovec_from_buf(&pad->pre_collapse_qiov, 0,
1583                                 pad->collapse_bounce_buf, pad->collapse_len);
1584         }
1585         qemu_vfree(pad->collapse_bounce_buf);
1586         qemu_iovec_destroy(&pad->pre_collapse_qiov);
1587     }
1588     if (pad->buf) {
1589         qemu_vfree(pad->buf);
1590         qemu_iovec_destroy(&pad->local_qiov);
1591     }
1592     memset(pad, 0, sizeof(*pad));
1593 }
1594 
1595 /*
1596  * Create pad->local_qiov by wrapping @iov in the padding head and tail, while
1597  * ensuring that the resulting vector will not exceed IOV_MAX elements.
1598  *
1599  * To ensure this, when necessary, the first two or three elements of @iov are
1600  * merged into pad->collapse_bounce_buf and replaced by a reference to that
1601  * bounce buffer in pad->local_qiov.
1602  *
1603  * After performing a read request, the data from the bounce buffer must be
1604  * copied back into pad->pre_collapse_qiov (e.g. by bdrv_padding_finalize()).
1605  */
1606 static int bdrv_create_padded_qiov(BlockDriverState *bs,
1607                                    BdrvRequestPadding *pad,
1608                                    struct iovec *iov, int niov,
1609                                    size_t iov_offset, size_t bytes)
1610 {
1611     int padded_niov, surplus_count, collapse_count;
1612 
1613     /* Assert this invariant */
1614     assert(niov <= IOV_MAX);
1615 
1616     /*
1617      * Cannot pad if resulting length would exceed SIZE_MAX.  Returning an error
1618      * to the guest is not ideal, but there is little else we can do.  At least
1619      * this will practically never happen on 64-bit systems.
1620      */
1621     if (SIZE_MAX - pad->head < bytes ||
1622         SIZE_MAX - pad->head - bytes < pad->tail)
1623     {
1624         return -EINVAL;
1625     }
1626 
1627     /* Length of the resulting IOV if we just concatenated everything */
1628     padded_niov = !!pad->head + niov + !!pad->tail;
1629 
1630     qemu_iovec_init(&pad->local_qiov, MIN(padded_niov, IOV_MAX));
1631 
1632     if (pad->head) {
1633         qemu_iovec_add(&pad->local_qiov, pad->buf, pad->head);
1634     }
1635 
1636     /*
1637      * If padded_niov > IOV_MAX, we cannot just concatenate everything.
1638      * Instead, merge the first two or three elements of @iov to reduce the
1639      * number of vector elements as necessary.
1640      */
1641     if (padded_niov > IOV_MAX) {
1642         /*
1643          * Only head and tail can have lead to the number of entries exceeding
1644          * IOV_MAX, so we can exceed it by the head and tail at most.  We need
1645          * to reduce the number of elements by `surplus_count`, so we merge that
1646          * many elements plus one into one element.
1647          */
1648         surplus_count = padded_niov - IOV_MAX;
1649         assert(surplus_count <= !!pad->head + !!pad->tail);
1650         collapse_count = surplus_count + 1;
1651 
1652         /*
1653          * Move the elements to collapse into `pad->pre_collapse_qiov`, then
1654          * advance `iov` (and associated variables) by those elements.
1655          */
1656         qemu_iovec_init(&pad->pre_collapse_qiov, collapse_count);
1657         qemu_iovec_concat_iov(&pad->pre_collapse_qiov, iov,
1658                               collapse_count, iov_offset, SIZE_MAX);
1659         iov += collapse_count;
1660         iov_offset = 0;
1661         niov -= collapse_count;
1662         bytes -= pad->pre_collapse_qiov.size;
1663 
1664         /*
1665          * Construct the bounce buffer to match the length of the to-collapse
1666          * vector elements, and for write requests, initialize it with the data
1667          * from those elements.  Then add it to `pad->local_qiov`.
1668          */
1669         pad->collapse_len = pad->pre_collapse_qiov.size;
1670         pad->collapse_bounce_buf = qemu_blockalign(bs, pad->collapse_len);
1671         if (pad->write) {
1672             qemu_iovec_to_buf(&pad->pre_collapse_qiov, 0,
1673                               pad->collapse_bounce_buf, pad->collapse_len);
1674         }
1675         qemu_iovec_add(&pad->local_qiov,
1676                        pad->collapse_bounce_buf, pad->collapse_len);
1677     }
1678 
1679     qemu_iovec_concat_iov(&pad->local_qiov, iov, niov, iov_offset, bytes);
1680 
1681     if (pad->tail) {
1682         qemu_iovec_add(&pad->local_qiov,
1683                        pad->buf + pad->buf_len - pad->tail, pad->tail);
1684     }
1685 
1686     assert(pad->local_qiov.niov == MIN(padded_niov, IOV_MAX));
1687     return 0;
1688 }
1689 
1690 /*
1691  * bdrv_pad_request
1692  *
1693  * Exchange request parameters with padded request if needed. Don't include RMW
1694  * read of padding, bdrv_padding_rmw_read() should be called separately if
1695  * needed.
1696  *
1697  * @write is true for write requests, false for read requests.
1698  *
1699  * Request parameters (@qiov, &qiov_offset, &offset, &bytes) are in-out:
1700  *  - on function start they represent original request
1701  *  - on failure or when padding is not needed they are unchanged
1702  *  - on success when padding is needed they represent padded request
1703  */
1704 static int bdrv_pad_request(BlockDriverState *bs,
1705                             QEMUIOVector **qiov, size_t *qiov_offset,
1706                             int64_t *offset, int64_t *bytes,
1707                             bool write,
1708                             BdrvRequestPadding *pad, bool *padded,
1709                             BdrvRequestFlags *flags)
1710 {
1711     int ret;
1712     struct iovec *sliced_iov;
1713     int sliced_niov;
1714     size_t sliced_head, sliced_tail;
1715 
1716     /* Should have been checked by the caller already */
1717     ret = bdrv_check_request32(*offset, *bytes, *qiov, *qiov_offset);
1718     if (ret < 0) {
1719         return ret;
1720     }
1721 
1722     if (!bdrv_init_padding(bs, *offset, *bytes, write, pad)) {
1723         if (padded) {
1724             *padded = false;
1725         }
1726         return 0;
1727     }
1728 
1729     /*
1730      * For prefetching in stream_populate(), no qiov is passed along, because
1731      * only copy-on-read matters.
1732      */
1733     if (*qiov) {
1734         sliced_iov = qemu_iovec_slice(*qiov, *qiov_offset, *bytes,
1735                                       &sliced_head, &sliced_tail,
1736                                       &sliced_niov);
1737 
1738         /* Guaranteed by bdrv_check_request32() */
1739         assert(*bytes <= SIZE_MAX);
1740         ret = bdrv_create_padded_qiov(bs, pad, sliced_iov, sliced_niov,
1741                                       sliced_head, *bytes);
1742         if (ret < 0) {
1743             bdrv_padding_finalize(pad);
1744             return ret;
1745         }
1746         *qiov = &pad->local_qiov;
1747         *qiov_offset = 0;
1748     }
1749 
1750     *bytes += pad->head + pad->tail;
1751     *offset -= pad->head;
1752     if (padded) {
1753         *padded = true;
1754     }
1755     if (flags) {
1756         /* Can't use optimization hint with bounce buffer */
1757         *flags &= ~BDRV_REQ_REGISTERED_BUF;
1758     }
1759 
1760     return 0;
1761 }
1762 
1763 int coroutine_fn bdrv_co_preadv(BdrvChild *child,
1764     int64_t offset, int64_t bytes, QEMUIOVector *qiov,
1765     BdrvRequestFlags flags)
1766 {
1767     IO_CODE();
1768     return bdrv_co_preadv_part(child, offset, bytes, qiov, 0, flags);
1769 }
1770 
1771 int coroutine_fn bdrv_co_preadv_part(BdrvChild *child,
1772     int64_t offset, int64_t bytes,
1773     QEMUIOVector *qiov, size_t qiov_offset,
1774     BdrvRequestFlags flags)
1775 {
1776     BlockDriverState *bs = child->bs;
1777     BdrvTrackedRequest req;
1778     BdrvRequestPadding pad;
1779     int ret;
1780     IO_CODE();
1781 
1782     trace_bdrv_co_preadv_part(bs, offset, bytes, flags);
1783 
1784     if (!bdrv_co_is_inserted(bs)) {
1785         return -ENOMEDIUM;
1786     }
1787 
1788     ret = bdrv_check_request32(offset, bytes, qiov, qiov_offset);
1789     if (ret < 0) {
1790         return ret;
1791     }
1792 
1793     if (bytes == 0 && !QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)) {
1794         /*
1795          * Aligning zero request is nonsense. Even if driver has special meaning
1796          * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
1797          * it to driver due to request_alignment.
1798          *
1799          * Still, no reason to return an error if someone do unaligned
1800          * zero-length read occasionally.
1801          */
1802         return 0;
1803     }
1804 
1805     bdrv_inc_in_flight(bs);
1806 
1807     /* Don't do copy-on-read if we read data before write operation */
1808     if (qatomic_read(&bs->copy_on_read)) {
1809         flags |= BDRV_REQ_COPY_ON_READ;
1810     }
1811 
1812     ret = bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes, false,
1813                            &pad, NULL, &flags);
1814     if (ret < 0) {
1815         goto fail;
1816     }
1817 
1818     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_READ);
1819     ret = bdrv_aligned_preadv(child, &req, offset, bytes,
1820                               bs->bl.request_alignment,
1821                               qiov, qiov_offset, flags);
1822     tracked_request_end(&req);
1823     bdrv_padding_finalize(&pad);
1824 
1825 fail:
1826     bdrv_dec_in_flight(bs);
1827 
1828     return ret;
1829 }
1830 
1831 static int coroutine_fn GRAPH_RDLOCK
1832 bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
1833                          BdrvRequestFlags flags)
1834 {
1835     BlockDriver *drv = bs->drv;
1836     QEMUIOVector qiov;
1837     void *buf = NULL;
1838     int ret = 0;
1839     bool need_flush = false;
1840     int head = 0;
1841     int tail = 0;
1842 
1843     int64_t max_write_zeroes = MIN_NON_ZERO(bs->bl.max_pwrite_zeroes,
1844                                             INT64_MAX);
1845     int alignment = MAX(bs->bl.pwrite_zeroes_alignment,
1846                         bs->bl.request_alignment);
1847     int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer, MAX_BOUNCE_BUFFER);
1848 
1849     assert_bdrv_graph_readable();
1850     bdrv_check_request(offset, bytes, &error_abort);
1851 
1852     if (!drv) {
1853         return -ENOMEDIUM;
1854     }
1855 
1856     if ((flags & ~bs->supported_zero_flags) & BDRV_REQ_NO_FALLBACK) {
1857         return -ENOTSUP;
1858     }
1859 
1860     /* By definition there is no user buffer so this flag doesn't make sense */
1861     if (flags & BDRV_REQ_REGISTERED_BUF) {
1862         return -EINVAL;
1863     }
1864 
1865     /* If opened with discard=off we should never unmap. */
1866     if (!(bs->open_flags & BDRV_O_UNMAP)) {
1867         flags &= ~BDRV_REQ_MAY_UNMAP;
1868     }
1869 
1870     /* Invalidate the cached block-status data range if this write overlaps */
1871     bdrv_bsc_invalidate_range(bs, offset, bytes);
1872 
1873     assert(alignment % bs->bl.request_alignment == 0);
1874     head = offset % alignment;
1875     tail = (offset + bytes) % alignment;
1876     max_write_zeroes = QEMU_ALIGN_DOWN(max_write_zeroes, alignment);
1877     assert(max_write_zeroes >= bs->bl.request_alignment);
1878 
1879     while (bytes > 0 && !ret) {
1880         int64_t num = bytes;
1881 
1882         /* Align request.  Block drivers can expect the "bulk" of the request
1883          * to be aligned, and that unaligned requests do not cross cluster
1884          * boundaries.
1885          */
1886         if (head) {
1887             /* Make a small request up to the first aligned sector. For
1888              * convenience, limit this request to max_transfer even if
1889              * we don't need to fall back to writes.  */
1890             num = MIN(MIN(bytes, max_transfer), alignment - head);
1891             head = (head + num) % alignment;
1892             assert(num < max_write_zeroes);
1893         } else if (tail && num > alignment) {
1894             /* Shorten the request to the last aligned sector.  */
1895             num -= tail;
1896         }
1897 
1898         /* limit request size */
1899         if (num > max_write_zeroes) {
1900             num = max_write_zeroes;
1901         }
1902 
1903         ret = -ENOTSUP;
1904         /* First try the efficient write zeroes operation */
1905         if (drv->bdrv_co_pwrite_zeroes) {
1906             ret = drv->bdrv_co_pwrite_zeroes(bs, offset, num,
1907                                              flags & bs->supported_zero_flags);
1908             if (ret != -ENOTSUP && (flags & BDRV_REQ_FUA) &&
1909                 !(bs->supported_zero_flags & BDRV_REQ_FUA)) {
1910                 need_flush = true;
1911             }
1912         } else {
1913             assert(!bs->supported_zero_flags);
1914         }
1915 
1916         if (ret == -ENOTSUP && !(flags & BDRV_REQ_NO_FALLBACK)) {
1917             /* Fall back to bounce buffer if write zeroes is unsupported */
1918             BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE;
1919 
1920             if ((flags & BDRV_REQ_FUA) &&
1921                 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
1922                 /* No need for bdrv_driver_pwrite() to do a fallback
1923                  * flush on each chunk; use just one at the end */
1924                 write_flags &= ~BDRV_REQ_FUA;
1925                 need_flush = true;
1926             }
1927             num = MIN(num, max_transfer);
1928             if (buf == NULL) {
1929                 buf = qemu_try_blockalign0(bs, num);
1930                 if (buf == NULL) {
1931                     ret = -ENOMEM;
1932                     goto fail;
1933                 }
1934             }
1935             qemu_iovec_init_buf(&qiov, buf, num);
1936 
1937             ret = bdrv_driver_pwritev(bs, offset, num, &qiov, 0, write_flags);
1938 
1939             /* Keep bounce buffer around if it is big enough for all
1940              * all future requests.
1941              */
1942             if (num < max_transfer) {
1943                 qemu_vfree(buf);
1944                 buf = NULL;
1945             }
1946         }
1947 
1948         offset += num;
1949         bytes -= num;
1950     }
1951 
1952 fail:
1953     if (ret == 0 && need_flush) {
1954         ret = bdrv_co_flush(bs);
1955     }
1956     qemu_vfree(buf);
1957     return ret;
1958 }
1959 
1960 static inline int coroutine_fn GRAPH_RDLOCK
1961 bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, int64_t bytes,
1962                           BdrvTrackedRequest *req, int flags)
1963 {
1964     BlockDriverState *bs = child->bs;
1965 
1966     bdrv_check_request(offset, bytes, &error_abort);
1967 
1968     if (bdrv_is_read_only(bs)) {
1969         return -EPERM;
1970     }
1971 
1972     assert(!(bs->open_flags & BDRV_O_INACTIVE));
1973     assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1974     assert(!(flags & ~BDRV_REQ_MASK));
1975     assert(!((flags & BDRV_REQ_NO_WAIT) && !(flags & BDRV_REQ_SERIALISING)));
1976 
1977     if (flags & BDRV_REQ_SERIALISING) {
1978         QEMU_LOCK_GUARD(&bs->reqs_lock);
1979 
1980         tracked_request_set_serialising(req, bdrv_get_cluster_size(bs));
1981 
1982         if ((flags & BDRV_REQ_NO_WAIT) && bdrv_find_conflicting_request(req)) {
1983             return -EBUSY;
1984         }
1985 
1986         bdrv_wait_serialising_requests_locked(req);
1987     } else {
1988         bdrv_wait_serialising_requests(req);
1989     }
1990 
1991     assert(req->overlap_offset <= offset);
1992     assert(offset + bytes <= req->overlap_offset + req->overlap_bytes);
1993     assert(offset + bytes <= bs->total_sectors * BDRV_SECTOR_SIZE ||
1994            child->perm & BLK_PERM_RESIZE);
1995 
1996     switch (req->type) {
1997     case BDRV_TRACKED_WRITE:
1998     case BDRV_TRACKED_DISCARD:
1999         if (flags & BDRV_REQ_WRITE_UNCHANGED) {
2000             assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
2001         } else {
2002             assert(child->perm & BLK_PERM_WRITE);
2003         }
2004         bdrv_write_threshold_check_write(bs, offset, bytes);
2005         return 0;
2006     case BDRV_TRACKED_TRUNCATE:
2007         assert(child->perm & BLK_PERM_RESIZE);
2008         return 0;
2009     default:
2010         abort();
2011     }
2012 }
2013 
2014 static inline void coroutine_fn GRAPH_RDLOCK
2015 bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, int64_t bytes,
2016                          BdrvTrackedRequest *req, int ret)
2017 {
2018     int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
2019     BlockDriverState *bs = child->bs;
2020 
2021     bdrv_check_request(offset, bytes, &error_abort);
2022 
2023     qatomic_inc(&bs->write_gen);
2024 
2025     /*
2026      * Discard cannot extend the image, but in error handling cases, such as
2027      * when reverting a qcow2 cluster allocation, the discarded range can pass
2028      * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
2029      * here. Instead, just skip it, since semantically a discard request
2030      * beyond EOF cannot expand the image anyway.
2031      */
2032     if (ret == 0 &&
2033         (req->type == BDRV_TRACKED_TRUNCATE ||
2034          end_sector > bs->total_sectors) &&
2035         req->type != BDRV_TRACKED_DISCARD) {
2036         bs->total_sectors = end_sector;
2037         bdrv_parent_cb_resize(bs);
2038         bdrv_dirty_bitmap_truncate(bs, end_sector << BDRV_SECTOR_BITS);
2039     }
2040     if (req->bytes) {
2041         switch (req->type) {
2042         case BDRV_TRACKED_WRITE:
2043             stat64_max(&bs->wr_highest_offset, offset + bytes);
2044             /* fall through, to set dirty bits */
2045         case BDRV_TRACKED_DISCARD:
2046             bdrv_set_dirty(bs, offset, bytes);
2047             break;
2048         default:
2049             break;
2050         }
2051     }
2052 }
2053 
2054 /*
2055  * Forwards an already correctly aligned write request to the BlockDriver,
2056  * after possibly fragmenting it.
2057  */
2058 static int coroutine_fn GRAPH_RDLOCK
2059 bdrv_aligned_pwritev(BdrvChild *child, BdrvTrackedRequest *req,
2060                      int64_t offset, int64_t bytes, int64_t align,
2061                      QEMUIOVector *qiov, size_t qiov_offset,
2062                      BdrvRequestFlags flags)
2063 {
2064     BlockDriverState *bs = child->bs;
2065     BlockDriver *drv = bs->drv;
2066     int ret;
2067 
2068     int64_t bytes_remaining = bytes;
2069     int max_transfer;
2070 
2071     bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
2072 
2073     if (!drv) {
2074         return -ENOMEDIUM;
2075     }
2076 
2077     if (bdrv_has_readonly_bitmaps(bs)) {
2078         return -EPERM;
2079     }
2080 
2081     assert(is_power_of_2(align));
2082     assert((offset & (align - 1)) == 0);
2083     assert((bytes & (align - 1)) == 0);
2084     max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
2085                                    align);
2086 
2087     ret = bdrv_co_write_req_prepare(child, offset, bytes, req, flags);
2088 
2089     if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF &&
2090         !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes &&
2091         qemu_iovec_is_zero(qiov, qiov_offset, bytes)) {
2092         flags |= BDRV_REQ_ZERO_WRITE;
2093         if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) {
2094             flags |= BDRV_REQ_MAY_UNMAP;
2095         }
2096 
2097         /* Can't use optimization hint with bufferless zero write */
2098         flags &= ~BDRV_REQ_REGISTERED_BUF;
2099     }
2100 
2101     if (ret < 0) {
2102         /* Do nothing, write notifier decided to fail this request */
2103     } else if (flags & BDRV_REQ_ZERO_WRITE) {
2104         bdrv_co_debug_event(bs, BLKDBG_PWRITEV_ZERO);
2105         ret = bdrv_co_do_pwrite_zeroes(bs, offset, bytes, flags);
2106     } else if (flags & BDRV_REQ_WRITE_COMPRESSED) {
2107         ret = bdrv_driver_pwritev_compressed(bs, offset, bytes,
2108                                              qiov, qiov_offset);
2109     } else if (bytes <= max_transfer) {
2110         bdrv_co_debug_event(bs, BLKDBG_PWRITEV);
2111         ret = bdrv_driver_pwritev(bs, offset, bytes, qiov, qiov_offset, flags);
2112     } else {
2113         bdrv_co_debug_event(bs, BLKDBG_PWRITEV);
2114         while (bytes_remaining) {
2115             int num = MIN(bytes_remaining, max_transfer);
2116             int local_flags = flags;
2117 
2118             assert(num);
2119             if (num < bytes_remaining && (flags & BDRV_REQ_FUA) &&
2120                 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
2121                 /* If FUA is going to be emulated by flush, we only
2122                  * need to flush on the last iteration */
2123                 local_flags &= ~BDRV_REQ_FUA;
2124             }
2125 
2126             ret = bdrv_driver_pwritev(bs, offset + bytes - bytes_remaining,
2127                                       num, qiov,
2128                                       qiov_offset + bytes - bytes_remaining,
2129                                       local_flags);
2130             if (ret < 0) {
2131                 break;
2132             }
2133             bytes_remaining -= num;
2134         }
2135     }
2136     bdrv_co_debug_event(bs, BLKDBG_PWRITEV_DONE);
2137 
2138     if (ret >= 0) {
2139         ret = 0;
2140     }
2141     bdrv_co_write_req_finish(child, offset, bytes, req, ret);
2142 
2143     return ret;
2144 }
2145 
2146 static int coroutine_fn GRAPH_RDLOCK
2147 bdrv_co_do_zero_pwritev(BdrvChild *child, int64_t offset, int64_t bytes,
2148                         BdrvRequestFlags flags, BdrvTrackedRequest *req)
2149 {
2150     BlockDriverState *bs = child->bs;
2151     QEMUIOVector local_qiov;
2152     uint64_t align = bs->bl.request_alignment;
2153     int ret = 0;
2154     bool padding;
2155     BdrvRequestPadding pad;
2156 
2157     /* This flag doesn't make sense for padding or zero writes */
2158     flags &= ~BDRV_REQ_REGISTERED_BUF;
2159 
2160     padding = bdrv_init_padding(bs, offset, bytes, true, &pad);
2161     if (padding) {
2162         assert(!(flags & BDRV_REQ_NO_WAIT));
2163         bdrv_make_request_serialising(req, align);
2164 
2165         bdrv_padding_rmw_read(child, req, &pad, true);
2166 
2167         if (pad.head || pad.merge_reads) {
2168             int64_t aligned_offset = offset & ~(align - 1);
2169             int64_t write_bytes = pad.merge_reads ? pad.buf_len : align;
2170 
2171             qemu_iovec_init_buf(&local_qiov, pad.buf, write_bytes);
2172             ret = bdrv_aligned_pwritev(child, req, aligned_offset, write_bytes,
2173                                        align, &local_qiov, 0,
2174                                        flags & ~BDRV_REQ_ZERO_WRITE);
2175             if (ret < 0 || pad.merge_reads) {
2176                 /* Error or all work is done */
2177                 goto out;
2178             }
2179             offset += write_bytes - pad.head;
2180             bytes -= write_bytes - pad.head;
2181         }
2182     }
2183 
2184     assert(!bytes || (offset & (align - 1)) == 0);
2185     if (bytes >= align) {
2186         /* Write the aligned part in the middle. */
2187         int64_t aligned_bytes = bytes & ~(align - 1);
2188         ret = bdrv_aligned_pwritev(child, req, offset, aligned_bytes, align,
2189                                    NULL, 0, flags);
2190         if (ret < 0) {
2191             goto out;
2192         }
2193         bytes -= aligned_bytes;
2194         offset += aligned_bytes;
2195     }
2196 
2197     assert(!bytes || (offset & (align - 1)) == 0);
2198     if (bytes) {
2199         assert(align == pad.tail + bytes);
2200 
2201         qemu_iovec_init_buf(&local_qiov, pad.tail_buf, align);
2202         ret = bdrv_aligned_pwritev(child, req, offset, align, align,
2203                                    &local_qiov, 0,
2204                                    flags & ~BDRV_REQ_ZERO_WRITE);
2205     }
2206 
2207 out:
2208     bdrv_padding_finalize(&pad);
2209 
2210     return ret;
2211 }
2212 
2213 /*
2214  * Handle a write request in coroutine context
2215  */
2216 int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
2217     int64_t offset, int64_t bytes, QEMUIOVector *qiov,
2218     BdrvRequestFlags flags)
2219 {
2220     IO_CODE();
2221     return bdrv_co_pwritev_part(child, offset, bytes, qiov, 0, flags);
2222 }
2223 
2224 int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child,
2225     int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset,
2226     BdrvRequestFlags flags)
2227 {
2228     BlockDriverState *bs = child->bs;
2229     BdrvTrackedRequest req;
2230     uint64_t align = bs->bl.request_alignment;
2231     BdrvRequestPadding pad;
2232     int ret;
2233     bool padded = false;
2234     IO_CODE();
2235 
2236     trace_bdrv_co_pwritev_part(child->bs, offset, bytes, flags);
2237 
2238     if (!bdrv_co_is_inserted(bs)) {
2239         return -ENOMEDIUM;
2240     }
2241 
2242     if (flags & BDRV_REQ_ZERO_WRITE) {
2243         ret = bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, NULL);
2244     } else {
2245         ret = bdrv_check_request32(offset, bytes, qiov, qiov_offset);
2246     }
2247     if (ret < 0) {
2248         return ret;
2249     }
2250 
2251     /* If the request is misaligned then we can't make it efficient */
2252     if ((flags & BDRV_REQ_NO_FALLBACK) &&
2253         !QEMU_IS_ALIGNED(offset | bytes, align))
2254     {
2255         return -ENOTSUP;
2256     }
2257 
2258     if (bytes == 0 && !QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)) {
2259         /*
2260          * Aligning zero request is nonsense. Even if driver has special meaning
2261          * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
2262          * it to driver due to request_alignment.
2263          *
2264          * Still, no reason to return an error if someone do unaligned
2265          * zero-length write occasionally.
2266          */
2267         return 0;
2268     }
2269 
2270     if (!(flags & BDRV_REQ_ZERO_WRITE)) {
2271         /*
2272          * Pad request for following read-modify-write cycle.
2273          * bdrv_co_do_zero_pwritev() does aligning by itself, so, we do
2274          * alignment only if there is no ZERO flag.
2275          */
2276         ret = bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes, true,
2277                                &pad, &padded, &flags);
2278         if (ret < 0) {
2279             return ret;
2280         }
2281     }
2282 
2283     bdrv_inc_in_flight(bs);
2284     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
2285 
2286     if (flags & BDRV_REQ_ZERO_WRITE) {
2287         assert(!padded);
2288         ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);
2289         goto out;
2290     }
2291 
2292     if (padded) {
2293         /*
2294          * Request was unaligned to request_alignment and therefore
2295          * padded.  We are going to do read-modify-write, and must
2296          * serialize the request to prevent interactions of the
2297          * widened region with other transactions.
2298          */
2299         assert(!(flags & BDRV_REQ_NO_WAIT));
2300         bdrv_make_request_serialising(&req, align);
2301         bdrv_padding_rmw_read(child, &req, &pad, false);
2302     }
2303 
2304     ret = bdrv_aligned_pwritev(child, &req, offset, bytes, align,
2305                                qiov, qiov_offset, flags);
2306 
2307     bdrv_padding_finalize(&pad);
2308 
2309 out:
2310     tracked_request_end(&req);
2311     bdrv_dec_in_flight(bs);
2312 
2313     return ret;
2314 }
2315 
2316 int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset,
2317                                        int64_t bytes, BdrvRequestFlags flags)
2318 {
2319     IO_CODE();
2320     trace_bdrv_co_pwrite_zeroes(child->bs, offset, bytes, flags);
2321     assert_bdrv_graph_readable();
2322 
2323     return bdrv_co_pwritev(child, offset, bytes, NULL,
2324                            BDRV_REQ_ZERO_WRITE | flags);
2325 }
2326 
2327 /*
2328  * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
2329  */
2330 int bdrv_flush_all(void)
2331 {
2332     BdrvNextIterator it;
2333     BlockDriverState *bs = NULL;
2334     int result = 0;
2335 
2336     GLOBAL_STATE_CODE();
2337     GRAPH_RDLOCK_GUARD_MAINLOOP();
2338 
2339     /*
2340      * bdrv queue is managed by record/replay,
2341      * creating new flush request for stopping
2342      * the VM may break the determinism
2343      */
2344     if (replay_events_enabled()) {
2345         return result;
2346     }
2347 
2348     for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
2349         int ret = bdrv_flush(bs);
2350         if (ret < 0 && !result) {
2351             result = ret;
2352         }
2353     }
2354 
2355     return result;
2356 }
2357 
2358 /*
2359  * Returns the allocation status of the specified sectors.
2360  * Drivers not implementing the functionality are assumed to not support
2361  * backing files, hence all their sectors are reported as allocated.
2362  *
2363  * If 'want_zero' is true, the caller is querying for mapping
2364  * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and
2365  * _ZERO where possible; otherwise, the result favors larger 'pnum',
2366  * with a focus on accurate BDRV_BLOCK_ALLOCATED.
2367  *
2368  * If 'offset' is beyond the end of the disk image the return value is
2369  * BDRV_BLOCK_EOF and 'pnum' is set to 0.
2370  *
2371  * 'bytes' is the max value 'pnum' should be set to.  If bytes goes
2372  * beyond the end of the disk image it will be clamped; if 'pnum' is set to
2373  * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
2374  *
2375  * 'pnum' is set to the number of bytes (including and immediately
2376  * following the specified offset) that are easily known to be in the
2377  * same allocated/unallocated state.  Note that a second call starting
2378  * at the original offset plus returned pnum may have the same status.
2379  * The returned value is non-zero on success except at end-of-file.
2380  *
2381  * Returns negative errno on failure.  Otherwise, if the
2382  * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are
2383  * set to the host mapping and BDS corresponding to the guest offset.
2384  */
2385 static int coroutine_fn GRAPH_RDLOCK
2386 bdrv_co_do_block_status(BlockDriverState *bs, bool want_zero,
2387                         int64_t offset, int64_t bytes,
2388                         int64_t *pnum, int64_t *map, BlockDriverState **file)
2389 {
2390     int64_t total_size;
2391     int64_t n; /* bytes */
2392     int ret;
2393     int64_t local_map = 0;
2394     BlockDriverState *local_file = NULL;
2395     int64_t aligned_offset, aligned_bytes;
2396     uint32_t align;
2397     bool has_filtered_child;
2398 
2399     assert(pnum);
2400     assert_bdrv_graph_readable();
2401     *pnum = 0;
2402     total_size = bdrv_co_getlength(bs);
2403     if (total_size < 0) {
2404         ret = total_size;
2405         goto early_out;
2406     }
2407 
2408     if (offset >= total_size) {
2409         ret = BDRV_BLOCK_EOF;
2410         goto early_out;
2411     }
2412     if (!bytes) {
2413         ret = 0;
2414         goto early_out;
2415     }
2416 
2417     n = total_size - offset;
2418     if (n < bytes) {
2419         bytes = n;
2420     }
2421 
2422     /* Must be non-NULL or bdrv_co_getlength() would have failed */
2423     assert(bs->drv);
2424     has_filtered_child = bdrv_filter_child(bs);
2425     if (!bs->drv->bdrv_co_block_status && !has_filtered_child) {
2426         *pnum = bytes;
2427         ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED;
2428         if (offset + bytes == total_size) {
2429             ret |= BDRV_BLOCK_EOF;
2430         }
2431         if (bs->drv->protocol_name) {
2432             ret |= BDRV_BLOCK_OFFSET_VALID;
2433             local_map = offset;
2434             local_file = bs;
2435         }
2436         goto early_out;
2437     }
2438 
2439     bdrv_inc_in_flight(bs);
2440 
2441     /* Round out to request_alignment boundaries */
2442     align = bs->bl.request_alignment;
2443     aligned_offset = QEMU_ALIGN_DOWN(offset, align);
2444     aligned_bytes = ROUND_UP(offset + bytes, align) - aligned_offset;
2445 
2446     if (bs->drv->bdrv_co_block_status) {
2447         /*
2448          * Use the block-status cache only for protocol nodes: Format
2449          * drivers are generally quick to inquire the status, but protocol
2450          * drivers often need to get information from outside of qemu, so
2451          * we do not have control over the actual implementation.  There
2452          * have been cases where inquiring the status took an unreasonably
2453          * long time, and we can do nothing in qemu to fix it.
2454          * This is especially problematic for images with large data areas,
2455          * because finding the few holes in them and giving them special
2456          * treatment does not gain much performance.  Therefore, we try to
2457          * cache the last-identified data region.
2458          *
2459          * Second, limiting ourselves to protocol nodes allows us to assume
2460          * the block status for data regions to be DATA | OFFSET_VALID, and
2461          * that the host offset is the same as the guest offset.
2462          *
2463          * Note that it is possible that external writers zero parts of
2464          * the cached regions without the cache being invalidated, and so
2465          * we may report zeroes as data.  This is not catastrophic,
2466          * however, because reporting zeroes as data is fine.
2467          */
2468         if (QLIST_EMPTY(&bs->children) &&
2469             bdrv_bsc_is_data(bs, aligned_offset, pnum))
2470         {
2471             ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
2472             local_file = bs;
2473             local_map = aligned_offset;
2474         } else {
2475             ret = bs->drv->bdrv_co_block_status(bs, want_zero, aligned_offset,
2476                                                 aligned_bytes, pnum, &local_map,
2477                                                 &local_file);
2478 
2479             /*
2480              * Note that checking QLIST_EMPTY(&bs->children) is also done when
2481              * the cache is queried above.  Technically, we do not need to check
2482              * it here; the worst that can happen is that we fill the cache for
2483              * non-protocol nodes, and then it is never used.  However, filling
2484              * the cache requires an RCU update, so double check here to avoid
2485              * such an update if possible.
2486              *
2487              * Check want_zero, because we only want to update the cache when we
2488              * have accurate information about what is zero and what is data.
2489              */
2490             if (want_zero &&
2491                 ret == (BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID) &&
2492                 QLIST_EMPTY(&bs->children))
2493             {
2494                 /*
2495                  * When a protocol driver reports BLOCK_OFFSET_VALID, the
2496                  * returned local_map value must be the same as the offset we
2497                  * have passed (aligned_offset), and local_bs must be the node
2498                  * itself.
2499                  * Assert this, because we follow this rule when reading from
2500                  * the cache (see the `local_file = bs` and
2501                  * `local_map = aligned_offset` assignments above), and the
2502                  * result the cache delivers must be the same as the driver
2503                  * would deliver.
2504                  */
2505                 assert(local_file == bs);
2506                 assert(local_map == aligned_offset);
2507                 bdrv_bsc_fill(bs, aligned_offset, *pnum);
2508             }
2509         }
2510     } else {
2511         /* Default code for filters */
2512 
2513         local_file = bdrv_filter_bs(bs);
2514         assert(local_file);
2515 
2516         *pnum = aligned_bytes;
2517         local_map = aligned_offset;
2518         ret = BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
2519     }
2520     if (ret < 0) {
2521         *pnum = 0;
2522         goto out;
2523     }
2524 
2525     /*
2526      * The driver's result must be a non-zero multiple of request_alignment.
2527      * Clamp pnum and adjust map to original request.
2528      */
2529     assert(*pnum && QEMU_IS_ALIGNED(*pnum, align) &&
2530            align > offset - aligned_offset);
2531     if (ret & BDRV_BLOCK_RECURSE) {
2532         assert(ret & BDRV_BLOCK_DATA);
2533         assert(ret & BDRV_BLOCK_OFFSET_VALID);
2534         assert(!(ret & BDRV_BLOCK_ZERO));
2535     }
2536 
2537     *pnum -= offset - aligned_offset;
2538     if (*pnum > bytes) {
2539         *pnum = bytes;
2540     }
2541     if (ret & BDRV_BLOCK_OFFSET_VALID) {
2542         local_map += offset - aligned_offset;
2543     }
2544 
2545     if (ret & BDRV_BLOCK_RAW) {
2546         assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file);
2547         ret = bdrv_co_do_block_status(local_file, want_zero, local_map,
2548                                       *pnum, pnum, &local_map, &local_file);
2549         goto out;
2550     }
2551 
2552     if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
2553         ret |= BDRV_BLOCK_ALLOCATED;
2554     } else if (bs->drv->supports_backing) {
2555         BlockDriverState *cow_bs = bdrv_cow_bs(bs);
2556 
2557         if (!cow_bs) {
2558             ret |= BDRV_BLOCK_ZERO;
2559         } else if (want_zero) {
2560             int64_t size2 = bdrv_co_getlength(cow_bs);
2561 
2562             if (size2 >= 0 && offset >= size2) {
2563                 ret |= BDRV_BLOCK_ZERO;
2564             }
2565         }
2566     }
2567 
2568     if (want_zero && ret & BDRV_BLOCK_RECURSE &&
2569         local_file && local_file != bs &&
2570         (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
2571         (ret & BDRV_BLOCK_OFFSET_VALID)) {
2572         int64_t file_pnum;
2573         int ret2;
2574 
2575         ret2 = bdrv_co_do_block_status(local_file, want_zero, local_map,
2576                                        *pnum, &file_pnum, NULL, NULL);
2577         if (ret2 >= 0) {
2578             /* Ignore errors.  This is just providing extra information, it
2579              * is useful but not necessary.
2580              */
2581             if (ret2 & BDRV_BLOCK_EOF &&
2582                 (!file_pnum || ret2 & BDRV_BLOCK_ZERO)) {
2583                 /*
2584                  * It is valid for the format block driver to read
2585                  * beyond the end of the underlying file's current
2586                  * size; such areas read as zero.
2587                  */
2588                 ret |= BDRV_BLOCK_ZERO;
2589             } else {
2590                 /* Limit request to the range reported by the protocol driver */
2591                 *pnum = file_pnum;
2592                 ret |= (ret2 & BDRV_BLOCK_ZERO);
2593             }
2594         }
2595 
2596         /*
2597          * Now that the recursive search was done, clear the flag. Otherwise,
2598          * with more complicated block graphs like snapshot-access ->
2599          * copy-before-write -> qcow2, where the return value will be propagated
2600          * further up to a parent bdrv_co_do_block_status() call, both the
2601          * BDRV_BLOCK_RECURSE and BDRV_BLOCK_ZERO flags would be set, which is
2602          * not allowed.
2603          */
2604         ret &= ~BDRV_BLOCK_RECURSE;
2605     }
2606 
2607 out:
2608     bdrv_dec_in_flight(bs);
2609     if (ret >= 0 && offset + *pnum == total_size) {
2610         ret |= BDRV_BLOCK_EOF;
2611     }
2612 early_out:
2613     if (file) {
2614         *file = local_file;
2615     }
2616     if (map) {
2617         *map = local_map;
2618     }
2619     return ret;
2620 }
2621 
2622 int coroutine_fn
2623 bdrv_co_common_block_status_above(BlockDriverState *bs,
2624                                   BlockDriverState *base,
2625                                   bool include_base,
2626                                   bool want_zero,
2627                                   int64_t offset,
2628                                   int64_t bytes,
2629                                   int64_t *pnum,
2630                                   int64_t *map,
2631                                   BlockDriverState **file,
2632                                   int *depth)
2633 {
2634     int ret;
2635     BlockDriverState *p;
2636     int64_t eof = 0;
2637     int dummy;
2638     IO_CODE();
2639 
2640     assert(!include_base || base); /* Can't include NULL base */
2641     assert_bdrv_graph_readable();
2642 
2643     if (!depth) {
2644         depth = &dummy;
2645     }
2646     *depth = 0;
2647 
2648     if (!include_base && bs == base) {
2649         *pnum = bytes;
2650         return 0;
2651     }
2652 
2653     ret = bdrv_co_do_block_status(bs, want_zero, offset, bytes, pnum,
2654                                   map, file);
2655     ++*depth;
2656     if (ret < 0 || *pnum == 0 || ret & BDRV_BLOCK_ALLOCATED || bs == base) {
2657         return ret;
2658     }
2659 
2660     if (ret & BDRV_BLOCK_EOF) {
2661         eof = offset + *pnum;
2662     }
2663 
2664     assert(*pnum <= bytes);
2665     bytes = *pnum;
2666 
2667     for (p = bdrv_filter_or_cow_bs(bs); include_base || p != base;
2668          p = bdrv_filter_or_cow_bs(p))
2669     {
2670         ret = bdrv_co_do_block_status(p, want_zero, offset, bytes, pnum,
2671                                       map, file);
2672         ++*depth;
2673         if (ret < 0) {
2674             return ret;
2675         }
2676         if (*pnum == 0) {
2677             /*
2678              * The top layer deferred to this layer, and because this layer is
2679              * short, any zeroes that we synthesize beyond EOF behave as if they
2680              * were allocated at this layer.
2681              *
2682              * We don't include BDRV_BLOCK_EOF into ret, as upper layer may be
2683              * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2684              * below.
2685              */
2686             assert(ret & BDRV_BLOCK_EOF);
2687             *pnum = bytes;
2688             if (file) {
2689                 *file = p;
2690             }
2691             ret = BDRV_BLOCK_ZERO | BDRV_BLOCK_ALLOCATED;
2692             break;
2693         }
2694         if (ret & BDRV_BLOCK_ALLOCATED) {
2695             /*
2696              * We've found the node and the status, we must break.
2697              *
2698              * Drop BDRV_BLOCK_EOF, as it's not for upper layer, which may be
2699              * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2700              * below.
2701              */
2702             ret &= ~BDRV_BLOCK_EOF;
2703             break;
2704         }
2705 
2706         if (p == base) {
2707             assert(include_base);
2708             break;
2709         }
2710 
2711         /*
2712          * OK, [offset, offset + *pnum) region is unallocated on this layer,
2713          * let's continue the diving.
2714          */
2715         assert(*pnum <= bytes);
2716         bytes = *pnum;
2717     }
2718 
2719     if (offset + *pnum == eof) {
2720         ret |= BDRV_BLOCK_EOF;
2721     }
2722 
2723     return ret;
2724 }
2725 
2726 int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs,
2727                                             BlockDriverState *base,
2728                                             int64_t offset, int64_t bytes,
2729                                             int64_t *pnum, int64_t *map,
2730                                             BlockDriverState **file)
2731 {
2732     IO_CODE();
2733     return bdrv_co_common_block_status_above(bs, base, false, true, offset,
2734                                              bytes, pnum, map, file, NULL);
2735 }
2736 
2737 int coroutine_fn bdrv_co_block_status(BlockDriverState *bs, int64_t offset,
2738                                       int64_t bytes, int64_t *pnum,
2739                                       int64_t *map, BlockDriverState **file)
2740 {
2741     IO_CODE();
2742     return bdrv_co_block_status_above(bs, bdrv_filter_or_cow_bs(bs),
2743                                       offset, bytes, pnum, map, file);
2744 }
2745 
2746 /*
2747  * Check @bs (and its backing chain) to see if the range defined
2748  * by @offset and @bytes is known to read as zeroes.
2749  * Return 1 if that is the case, 0 otherwise and -errno on error.
2750  * This test is meant to be fast rather than accurate so returning 0
2751  * does not guarantee non-zero data.
2752  */
2753 int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset,
2754                                       int64_t bytes)
2755 {
2756     int ret;
2757     int64_t pnum = bytes;
2758     IO_CODE();
2759 
2760     if (!bytes) {
2761         return 1;
2762     }
2763 
2764     ret = bdrv_co_common_block_status_above(bs, NULL, false, false, offset,
2765                                             bytes, &pnum, NULL, NULL, NULL);
2766 
2767     if (ret < 0) {
2768         return ret;
2769     }
2770 
2771     return (pnum == bytes) && (ret & BDRV_BLOCK_ZERO);
2772 }
2773 
2774 int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t offset,
2775                                       int64_t bytes, int64_t *pnum)
2776 {
2777     int ret;
2778     int64_t dummy;
2779     IO_CODE();
2780 
2781     ret = bdrv_co_common_block_status_above(bs, bs, true, false, offset,
2782                                             bytes, pnum ? pnum : &dummy, NULL,
2783                                             NULL, NULL);
2784     if (ret < 0) {
2785         return ret;
2786     }
2787     return !!(ret & BDRV_BLOCK_ALLOCATED);
2788 }
2789 
2790 /*
2791  * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2792  *
2793  * Return a positive depth if (a prefix of) the given range is allocated
2794  * in any image between BASE and TOP (BASE is only included if include_base
2795  * is set).  Depth 1 is TOP, 2 is the first backing layer, and so forth.
2796  * BASE can be NULL to check if the given offset is allocated in any
2797  * image of the chain.  Return 0 otherwise, or negative errno on
2798  * failure.
2799  *
2800  * 'pnum' is set to the number of bytes (including and immediately
2801  * following the specified offset) that are known to be in the same
2802  * allocated/unallocated state.  Note that a subsequent call starting
2803  * at 'offset + *pnum' may return the same allocation status (in other
2804  * words, the result is not necessarily the maximum possible range);
2805  * but 'pnum' will only be 0 when end of file is reached.
2806  */
2807 int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *bs,
2808                                             BlockDriverState *base,
2809                                             bool include_base, int64_t offset,
2810                                             int64_t bytes, int64_t *pnum)
2811 {
2812     int depth;
2813     int ret;
2814     IO_CODE();
2815 
2816     ret = bdrv_co_common_block_status_above(bs, base, include_base, false,
2817                                             offset, bytes, pnum, NULL, NULL,
2818                                             &depth);
2819     if (ret < 0) {
2820         return ret;
2821     }
2822 
2823     if (ret & BDRV_BLOCK_ALLOCATED) {
2824         return depth;
2825     }
2826     return 0;
2827 }
2828 
2829 int coroutine_fn
2830 bdrv_co_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
2831 {
2832     BlockDriver *drv = bs->drv;
2833     BlockDriverState *child_bs = bdrv_primary_bs(bs);
2834     int ret;
2835     IO_CODE();
2836     assert_bdrv_graph_readable();
2837 
2838     ret = bdrv_check_qiov_request(pos, qiov->size, qiov, 0, NULL);
2839     if (ret < 0) {
2840         return ret;
2841     }
2842 
2843     if (!drv) {
2844         return -ENOMEDIUM;
2845     }
2846 
2847     bdrv_inc_in_flight(bs);
2848 
2849     if (drv->bdrv_co_load_vmstate) {
2850         ret = drv->bdrv_co_load_vmstate(bs, qiov, pos);
2851     } else if (child_bs) {
2852         ret = bdrv_co_readv_vmstate(child_bs, qiov, pos);
2853     } else {
2854         ret = -ENOTSUP;
2855     }
2856 
2857     bdrv_dec_in_flight(bs);
2858 
2859     return ret;
2860 }
2861 
2862 int coroutine_fn
2863 bdrv_co_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
2864 {
2865     BlockDriver *drv = bs->drv;
2866     BlockDriverState *child_bs = bdrv_primary_bs(bs);
2867     int ret;
2868     IO_CODE();
2869     assert_bdrv_graph_readable();
2870 
2871     ret = bdrv_check_qiov_request(pos, qiov->size, qiov, 0, NULL);
2872     if (ret < 0) {
2873         return ret;
2874     }
2875 
2876     if (!drv) {
2877         return -ENOMEDIUM;
2878     }
2879 
2880     bdrv_inc_in_flight(bs);
2881 
2882     if (drv->bdrv_co_save_vmstate) {
2883         ret = drv->bdrv_co_save_vmstate(bs, qiov, pos);
2884     } else if (child_bs) {
2885         ret = bdrv_co_writev_vmstate(child_bs, qiov, pos);
2886     } else {
2887         ret = -ENOTSUP;
2888     }
2889 
2890     bdrv_dec_in_flight(bs);
2891 
2892     return ret;
2893 }
2894 
2895 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2896                       int64_t pos, int size)
2897 {
2898     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
2899     int ret = bdrv_writev_vmstate(bs, &qiov, pos);
2900     IO_CODE();
2901 
2902     return ret < 0 ? ret : size;
2903 }
2904 
2905 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2906                       int64_t pos, int size)
2907 {
2908     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
2909     int ret = bdrv_readv_vmstate(bs, &qiov, pos);
2910     IO_CODE();
2911 
2912     return ret < 0 ? ret : size;
2913 }
2914 
2915 /**************************************************************/
2916 /* async I/Os */
2917 
2918 /**
2919  * Synchronously cancels an acb. Must be called with the BQL held and the acb
2920  * must be processed with the BQL held too (IOThreads are not allowed).
2921  *
2922  * Use bdrv_aio_cancel_async() instead when possible.
2923  */
2924 void bdrv_aio_cancel(BlockAIOCB *acb)
2925 {
2926     GLOBAL_STATE_CODE();
2927     qemu_aio_ref(acb);
2928     bdrv_aio_cancel_async(acb);
2929     AIO_WAIT_WHILE_UNLOCKED(NULL, acb->refcnt > 1);
2930     qemu_aio_unref(acb);
2931 }
2932 
2933 /* Async version of aio cancel. The caller is not blocked if the acb implements
2934  * cancel_async, otherwise we do nothing and let the request normally complete.
2935  * In either case the completion callback must be called. */
2936 void bdrv_aio_cancel_async(BlockAIOCB *acb)
2937 {
2938     IO_CODE();
2939     if (acb->aiocb_info->cancel_async) {
2940         acb->aiocb_info->cancel_async(acb);
2941     }
2942 }
2943 
2944 /**************************************************************/
2945 /* Coroutine block device emulation */
2946 
2947 int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2948 {
2949     BdrvChild *primary_child = bdrv_primary_child(bs);
2950     BdrvChild *child;
2951     int current_gen;
2952     int ret = 0;
2953     IO_CODE();
2954 
2955     assert_bdrv_graph_readable();
2956     bdrv_inc_in_flight(bs);
2957 
2958     if (!bdrv_co_is_inserted(bs) || bdrv_is_read_only(bs) ||
2959         bdrv_is_sg(bs)) {
2960         goto early_exit;
2961     }
2962 
2963     qemu_mutex_lock(&bs->reqs_lock);
2964     current_gen = qatomic_read(&bs->write_gen);
2965 
2966     /* Wait until any previous flushes are completed */
2967     while (bs->active_flush_req) {
2968         qemu_co_queue_wait(&bs->flush_queue, &bs->reqs_lock);
2969     }
2970 
2971     /* Flushes reach this point in nondecreasing current_gen order.  */
2972     bs->active_flush_req = true;
2973     qemu_mutex_unlock(&bs->reqs_lock);
2974 
2975     /* Write back all layers by calling one driver function */
2976     if (bs->drv->bdrv_co_flush) {
2977         ret = bs->drv->bdrv_co_flush(bs);
2978         goto out;
2979     }
2980 
2981     /* Write back cached data to the OS even with cache=unsafe */
2982     BLKDBG_CO_EVENT(primary_child, BLKDBG_FLUSH_TO_OS);
2983     if (bs->drv->bdrv_co_flush_to_os) {
2984         ret = bs->drv->bdrv_co_flush_to_os(bs);
2985         if (ret < 0) {
2986             goto out;
2987         }
2988     }
2989 
2990     /* But don't actually force it to the disk with cache=unsafe */
2991     if (bs->open_flags & BDRV_O_NO_FLUSH) {
2992         goto flush_children;
2993     }
2994 
2995     /* Check if we really need to flush anything */
2996     if (bs->flushed_gen == current_gen) {
2997         goto flush_children;
2998     }
2999 
3000     BLKDBG_CO_EVENT(primary_child, BLKDBG_FLUSH_TO_DISK);
3001     if (!bs->drv) {
3002         /* bs->drv->bdrv_co_flush() might have ejected the BDS
3003          * (even in case of apparent success) */
3004         ret = -ENOMEDIUM;
3005         goto out;
3006     }
3007     if (bs->drv->bdrv_co_flush_to_disk) {
3008         ret = bs->drv->bdrv_co_flush_to_disk(bs);
3009     } else if (bs->drv->bdrv_aio_flush) {
3010         BlockAIOCB *acb;
3011         CoroutineIOCompletion co = {
3012             .coroutine = qemu_coroutine_self(),
3013         };
3014 
3015         acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
3016         if (acb == NULL) {
3017             ret = -EIO;
3018         } else {
3019             qemu_coroutine_yield();
3020             ret = co.ret;
3021         }
3022     } else {
3023         /*
3024          * Some block drivers always operate in either writethrough or unsafe
3025          * mode and don't support bdrv_flush therefore. Usually qemu doesn't
3026          * know how the server works (because the behaviour is hardcoded or
3027          * depends on server-side configuration), so we can't ensure that
3028          * everything is safe on disk. Returning an error doesn't work because
3029          * that would break guests even if the server operates in writethrough
3030          * mode.
3031          *
3032          * Let's hope the user knows what he's doing.
3033          */
3034         ret = 0;
3035     }
3036 
3037     if (ret < 0) {
3038         goto out;
3039     }
3040 
3041     /* Now flush the underlying protocol.  It will also have BDRV_O_NO_FLUSH
3042      * in the case of cache=unsafe, so there are no useless flushes.
3043      */
3044 flush_children:
3045     ret = 0;
3046     QLIST_FOREACH(child, &bs->children, next) {
3047         if (child->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) {
3048             int this_child_ret = bdrv_co_flush(child->bs);
3049             if (!ret) {
3050                 ret = this_child_ret;
3051             }
3052         }
3053     }
3054 
3055 out:
3056     /* Notify any pending flushes that we have completed */
3057     if (ret == 0) {
3058         bs->flushed_gen = current_gen;
3059     }
3060 
3061     qemu_mutex_lock(&bs->reqs_lock);
3062     bs->active_flush_req = false;
3063     /* Return value is ignored - it's ok if wait queue is empty */
3064     qemu_co_queue_next(&bs->flush_queue);
3065     qemu_mutex_unlock(&bs->reqs_lock);
3066 
3067 early_exit:
3068     bdrv_dec_in_flight(bs);
3069     return ret;
3070 }
3071 
3072 int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
3073                                   int64_t bytes)
3074 {
3075     BdrvTrackedRequest req;
3076     int ret;
3077     int64_t max_pdiscard;
3078     int head, tail, align;
3079     BlockDriverState *bs = child->bs;
3080     IO_CODE();
3081     assert_bdrv_graph_readable();
3082 
3083     if (!bs || !bs->drv || !bdrv_co_is_inserted(bs)) {
3084         return -ENOMEDIUM;
3085     }
3086 
3087     if (bdrv_has_readonly_bitmaps(bs)) {
3088         return -EPERM;
3089     }
3090 
3091     ret = bdrv_check_request(offset, bytes, NULL);
3092     if (ret < 0) {
3093         return ret;
3094     }
3095 
3096     /* Do nothing if disabled.  */
3097     if (!(bs->open_flags & BDRV_O_UNMAP)) {
3098         return 0;
3099     }
3100 
3101     if (!bs->drv->bdrv_co_pdiscard && !bs->drv->bdrv_aio_pdiscard) {
3102         return 0;
3103     }
3104 
3105     /* Invalidate the cached block-status data range if this discard overlaps */
3106     bdrv_bsc_invalidate_range(bs, offset, bytes);
3107 
3108     /* Discard is advisory, but some devices track and coalesce
3109      * unaligned requests, so we must pass everything down rather than
3110      * round here.  Still, most devices will just silently ignore
3111      * unaligned requests (by returning -ENOTSUP), so we must fragment
3112      * the request accordingly.  */
3113     align = MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment);
3114     assert(align % bs->bl.request_alignment == 0);
3115     head = offset % align;
3116     tail = (offset + bytes) % align;
3117 
3118     bdrv_inc_in_flight(bs);
3119     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_DISCARD);
3120 
3121     ret = bdrv_co_write_req_prepare(child, offset, bytes, &req, 0);
3122     if (ret < 0) {
3123         goto out;
3124     }
3125 
3126     max_pdiscard = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard, INT64_MAX),
3127                                    align);
3128     assert(max_pdiscard >= bs->bl.request_alignment);
3129 
3130     while (bytes > 0) {
3131         int64_t num = bytes;
3132 
3133         if (head) {
3134             /* Make small requests to get to alignment boundaries. */
3135             num = MIN(bytes, align - head);
3136             if (!QEMU_IS_ALIGNED(num, bs->bl.request_alignment)) {
3137                 num %= bs->bl.request_alignment;
3138             }
3139             head = (head + num) % align;
3140             assert(num < max_pdiscard);
3141         } else if (tail) {
3142             if (num > align) {
3143                 /* Shorten the request to the last aligned cluster.  */
3144                 num -= tail;
3145             } else if (!QEMU_IS_ALIGNED(tail, bs->bl.request_alignment) &&
3146                        tail > bs->bl.request_alignment) {
3147                 tail %= bs->bl.request_alignment;
3148                 num -= tail;
3149             }
3150         }
3151         /* limit request size */
3152         if (num > max_pdiscard) {
3153             num = max_pdiscard;
3154         }
3155 
3156         if (!bs->drv) {
3157             ret = -ENOMEDIUM;
3158             goto out;
3159         }
3160         if (bs->drv->bdrv_co_pdiscard) {
3161             ret = bs->drv->bdrv_co_pdiscard(bs, offset, num);
3162         } else {
3163             BlockAIOCB *acb;
3164             CoroutineIOCompletion co = {
3165                 .coroutine = qemu_coroutine_self(),
3166             };
3167 
3168             acb = bs->drv->bdrv_aio_pdiscard(bs, offset, num,
3169                                              bdrv_co_io_em_complete, &co);
3170             if (acb == NULL) {
3171                 ret = -EIO;
3172                 goto out;
3173             } else {
3174                 qemu_coroutine_yield();
3175                 ret = co.ret;
3176             }
3177         }
3178         if (ret && ret != -ENOTSUP) {
3179             goto out;
3180         }
3181 
3182         offset += num;
3183         bytes -= num;
3184     }
3185     ret = 0;
3186 out:
3187     bdrv_co_write_req_finish(child, req.offset, req.bytes, &req, ret);
3188     tracked_request_end(&req);
3189     bdrv_dec_in_flight(bs);
3190     return ret;
3191 }
3192 
3193 int coroutine_fn bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf)
3194 {
3195     BlockDriver *drv = bs->drv;
3196     CoroutineIOCompletion co = {
3197         .coroutine = qemu_coroutine_self(),
3198     };
3199     BlockAIOCB *acb;
3200     IO_CODE();
3201     assert_bdrv_graph_readable();
3202 
3203     bdrv_inc_in_flight(bs);
3204     if (!drv || (!drv->bdrv_aio_ioctl && !drv->bdrv_co_ioctl)) {
3205         co.ret = -ENOTSUP;
3206         goto out;
3207     }
3208 
3209     if (drv->bdrv_co_ioctl) {
3210         co.ret = drv->bdrv_co_ioctl(bs, req, buf);
3211     } else {
3212         acb = drv->bdrv_aio_ioctl(bs, req, buf, bdrv_co_io_em_complete, &co);
3213         if (!acb) {
3214             co.ret = -ENOTSUP;
3215             goto out;
3216         }
3217         qemu_coroutine_yield();
3218     }
3219 out:
3220     bdrv_dec_in_flight(bs);
3221     return co.ret;
3222 }
3223 
3224 int coroutine_fn bdrv_co_zone_report(BlockDriverState *bs, int64_t offset,
3225                         unsigned int *nr_zones,
3226                         BlockZoneDescriptor *zones)
3227 {
3228     BlockDriver *drv = bs->drv;
3229     CoroutineIOCompletion co = {
3230             .coroutine = qemu_coroutine_self(),
3231     };
3232     IO_CODE();
3233 
3234     bdrv_inc_in_flight(bs);
3235     if (!drv || !drv->bdrv_co_zone_report || bs->bl.zoned == BLK_Z_NONE) {
3236         co.ret = -ENOTSUP;
3237         goto out;
3238     }
3239     co.ret = drv->bdrv_co_zone_report(bs, offset, nr_zones, zones);
3240 out:
3241     bdrv_dec_in_flight(bs);
3242     return co.ret;
3243 }
3244 
3245 int coroutine_fn bdrv_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
3246         int64_t offset, int64_t len)
3247 {
3248     BlockDriver *drv = bs->drv;
3249     CoroutineIOCompletion co = {
3250             .coroutine = qemu_coroutine_self(),
3251     };
3252     IO_CODE();
3253 
3254     bdrv_inc_in_flight(bs);
3255     if (!drv || !drv->bdrv_co_zone_mgmt || bs->bl.zoned == BLK_Z_NONE) {
3256         co.ret = -ENOTSUP;
3257         goto out;
3258     }
3259     co.ret = drv->bdrv_co_zone_mgmt(bs, op, offset, len);
3260 out:
3261     bdrv_dec_in_flight(bs);
3262     return co.ret;
3263 }
3264 
3265 int coroutine_fn bdrv_co_zone_append(BlockDriverState *bs, int64_t *offset,
3266                         QEMUIOVector *qiov,
3267                         BdrvRequestFlags flags)
3268 {
3269     int ret;
3270     BlockDriver *drv = bs->drv;
3271     CoroutineIOCompletion co = {
3272             .coroutine = qemu_coroutine_self(),
3273     };
3274     IO_CODE();
3275 
3276     ret = bdrv_check_qiov_request(*offset, qiov->size, qiov, 0, NULL);
3277     if (ret < 0) {
3278         return ret;
3279     }
3280 
3281     bdrv_inc_in_flight(bs);
3282     if (!drv || !drv->bdrv_co_zone_append || bs->bl.zoned == BLK_Z_NONE) {
3283         co.ret = -ENOTSUP;
3284         goto out;
3285     }
3286     co.ret = drv->bdrv_co_zone_append(bs, offset, qiov, flags);
3287 out:
3288     bdrv_dec_in_flight(bs);
3289     return co.ret;
3290 }
3291 
3292 void *qemu_blockalign(BlockDriverState *bs, size_t size)
3293 {
3294     IO_CODE();
3295     return qemu_memalign(bdrv_opt_mem_align(bs), size);
3296 }
3297 
3298 void *qemu_blockalign0(BlockDriverState *bs, size_t size)
3299 {
3300     IO_CODE();
3301     return memset(qemu_blockalign(bs, size), 0, size);
3302 }
3303 
3304 void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
3305 {
3306     size_t align = bdrv_opt_mem_align(bs);
3307     IO_CODE();
3308 
3309     /* Ensure that NULL is never returned on success */
3310     assert(align > 0);
3311     if (size == 0) {
3312         size = align;
3313     }
3314 
3315     return qemu_try_memalign(align, size);
3316 }
3317 
3318 void *qemu_try_blockalign0(BlockDriverState *bs, size_t size)
3319 {
3320     void *mem = qemu_try_blockalign(bs, size);
3321     IO_CODE();
3322 
3323     if (mem) {
3324         memset(mem, 0, size);
3325     }
3326 
3327     return mem;
3328 }
3329 
3330 /* Helper that undoes bdrv_register_buf() when it fails partway through */
3331 static void GRAPH_RDLOCK
3332 bdrv_register_buf_rollback(BlockDriverState *bs, void *host, size_t size,
3333                            BdrvChild *final_child)
3334 {
3335     BdrvChild *child;
3336 
3337     GLOBAL_STATE_CODE();
3338     assert_bdrv_graph_readable();
3339 
3340     QLIST_FOREACH(child, &bs->children, next) {
3341         if (child == final_child) {
3342             break;
3343         }
3344 
3345         bdrv_unregister_buf(child->bs, host, size);
3346     }
3347 
3348     if (bs->drv && bs->drv->bdrv_unregister_buf) {
3349         bs->drv->bdrv_unregister_buf(bs, host, size);
3350     }
3351 }
3352 
3353 bool bdrv_register_buf(BlockDriverState *bs, void *host, size_t size,
3354                        Error **errp)
3355 {
3356     BdrvChild *child;
3357 
3358     GLOBAL_STATE_CODE();
3359     GRAPH_RDLOCK_GUARD_MAINLOOP();
3360 
3361     if (bs->drv && bs->drv->bdrv_register_buf) {
3362         if (!bs->drv->bdrv_register_buf(bs, host, size, errp)) {
3363             return false;
3364         }
3365     }
3366     QLIST_FOREACH(child, &bs->children, next) {
3367         if (!bdrv_register_buf(child->bs, host, size, errp)) {
3368             bdrv_register_buf_rollback(bs, host, size, child);
3369             return false;
3370         }
3371     }
3372     return true;
3373 }
3374 
3375 void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size)
3376 {
3377     BdrvChild *child;
3378 
3379     GLOBAL_STATE_CODE();
3380     GRAPH_RDLOCK_GUARD_MAINLOOP();
3381 
3382     if (bs->drv && bs->drv->bdrv_unregister_buf) {
3383         bs->drv->bdrv_unregister_buf(bs, host, size);
3384     }
3385     QLIST_FOREACH(child, &bs->children, next) {
3386         bdrv_unregister_buf(child->bs, host, size);
3387     }
3388 }
3389 
3390 static int coroutine_fn GRAPH_RDLOCK bdrv_co_copy_range_internal(
3391         BdrvChild *src, int64_t src_offset, BdrvChild *dst,
3392         int64_t dst_offset, int64_t bytes,
3393         BdrvRequestFlags read_flags, BdrvRequestFlags write_flags,
3394         bool recurse_src)
3395 {
3396     BdrvTrackedRequest req;
3397     int ret;
3398     assert_bdrv_graph_readable();
3399 
3400     /* TODO We can support BDRV_REQ_NO_FALLBACK here */
3401     assert(!(read_flags & BDRV_REQ_NO_FALLBACK));
3402     assert(!(write_flags & BDRV_REQ_NO_FALLBACK));
3403     assert(!(read_flags & BDRV_REQ_NO_WAIT));
3404     assert(!(write_flags & BDRV_REQ_NO_WAIT));
3405 
3406     if (!dst || !dst->bs || !bdrv_co_is_inserted(dst->bs)) {
3407         return -ENOMEDIUM;
3408     }
3409     ret = bdrv_check_request32(dst_offset, bytes, NULL, 0);
3410     if (ret) {
3411         return ret;
3412     }
3413     if (write_flags & BDRV_REQ_ZERO_WRITE) {
3414         return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, write_flags);
3415     }
3416 
3417     if (!src || !src->bs || !bdrv_co_is_inserted(src->bs)) {
3418         return -ENOMEDIUM;
3419     }
3420     ret = bdrv_check_request32(src_offset, bytes, NULL, 0);
3421     if (ret) {
3422         return ret;
3423     }
3424 
3425     if (!src->bs->drv->bdrv_co_copy_range_from
3426         || !dst->bs->drv->bdrv_co_copy_range_to
3427         || src->bs->encrypted || dst->bs->encrypted) {
3428         return -ENOTSUP;
3429     }
3430 
3431     if (recurse_src) {
3432         bdrv_inc_in_flight(src->bs);
3433         tracked_request_begin(&req, src->bs, src_offset, bytes,
3434                               BDRV_TRACKED_READ);
3435 
3436         /* BDRV_REQ_SERIALISING is only for write operation */
3437         assert(!(read_flags & BDRV_REQ_SERIALISING));
3438         bdrv_wait_serialising_requests(&req);
3439 
3440         ret = src->bs->drv->bdrv_co_copy_range_from(src->bs,
3441                                                     src, src_offset,
3442                                                     dst, dst_offset,
3443                                                     bytes,
3444                                                     read_flags, write_flags);
3445 
3446         tracked_request_end(&req);
3447         bdrv_dec_in_flight(src->bs);
3448     } else {
3449         bdrv_inc_in_flight(dst->bs);
3450         tracked_request_begin(&req, dst->bs, dst_offset, bytes,
3451                               BDRV_TRACKED_WRITE);
3452         ret = bdrv_co_write_req_prepare(dst, dst_offset, bytes, &req,
3453                                         write_flags);
3454         if (!ret) {
3455             ret = dst->bs->drv->bdrv_co_copy_range_to(dst->bs,
3456                                                       src, src_offset,
3457                                                       dst, dst_offset,
3458                                                       bytes,
3459                                                       read_flags, write_flags);
3460         }
3461         bdrv_co_write_req_finish(dst, dst_offset, bytes, &req, ret);
3462         tracked_request_end(&req);
3463         bdrv_dec_in_flight(dst->bs);
3464     }
3465 
3466     return ret;
3467 }
3468 
3469 /* Copy range from @src to @dst.
3470  *
3471  * See the comment of bdrv_co_copy_range for the parameter and return value
3472  * semantics. */
3473 int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, int64_t src_offset,
3474                                          BdrvChild *dst, int64_t dst_offset,
3475                                          int64_t bytes,
3476                                          BdrvRequestFlags read_flags,
3477                                          BdrvRequestFlags write_flags)
3478 {
3479     IO_CODE();
3480     assert_bdrv_graph_readable();
3481     trace_bdrv_co_copy_range_from(src, src_offset, dst, dst_offset, bytes,
3482                                   read_flags, write_flags);
3483     return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
3484                                        bytes, read_flags, write_flags, true);
3485 }
3486 
3487 /* Copy range from @src to @dst.
3488  *
3489  * See the comment of bdrv_co_copy_range for the parameter and return value
3490  * semantics. */
3491 int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, int64_t src_offset,
3492                                        BdrvChild *dst, int64_t dst_offset,
3493                                        int64_t bytes,
3494                                        BdrvRequestFlags read_flags,
3495                                        BdrvRequestFlags write_flags)
3496 {
3497     IO_CODE();
3498     assert_bdrv_graph_readable();
3499     trace_bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
3500                                 read_flags, write_flags);
3501     return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
3502                                        bytes, read_flags, write_flags, false);
3503 }
3504 
3505 int coroutine_fn bdrv_co_copy_range(BdrvChild *src, int64_t src_offset,
3506                                     BdrvChild *dst, int64_t dst_offset,
3507                                     int64_t bytes, BdrvRequestFlags read_flags,
3508                                     BdrvRequestFlags write_flags)
3509 {
3510     IO_CODE();
3511     assert_bdrv_graph_readable();
3512 
3513     return bdrv_co_copy_range_from(src, src_offset,
3514                                    dst, dst_offset,
3515                                    bytes, read_flags, write_flags);
3516 }
3517 
3518 static void coroutine_fn GRAPH_RDLOCK
3519 bdrv_parent_cb_resize(BlockDriverState *bs)
3520 {
3521     BdrvChild *c;
3522 
3523     assert_bdrv_graph_readable();
3524 
3525     QLIST_FOREACH(c, &bs->parents, next_parent) {
3526         if (c->klass->resize) {
3527             c->klass->resize(c);
3528         }
3529     }
3530 }
3531 
3532 /**
3533  * Truncate file to 'offset' bytes (needed only for file protocols)
3534  *
3535  * If 'exact' is true, the file must be resized to exactly the given
3536  * 'offset'.  Otherwise, it is sufficient for the node to be at least
3537  * 'offset' bytes in length.
3538  */
3539 int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
3540                                   PreallocMode prealloc, BdrvRequestFlags flags,
3541                                   Error **errp)
3542 {
3543     BlockDriverState *bs = child->bs;
3544     BdrvChild *filtered, *backing;
3545     BlockDriver *drv = bs->drv;
3546     BdrvTrackedRequest req;
3547     int64_t old_size, new_bytes;
3548     int ret;
3549     IO_CODE();
3550     assert_bdrv_graph_readable();
3551 
3552     /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3553     if (!drv) {
3554         error_setg(errp, "No medium inserted");
3555         return -ENOMEDIUM;
3556     }
3557     if (offset < 0) {
3558         error_setg(errp, "Image size cannot be negative");
3559         return -EINVAL;
3560     }
3561 
3562     ret = bdrv_check_request(offset, 0, errp);
3563     if (ret < 0) {
3564         return ret;
3565     }
3566 
3567     old_size = bdrv_co_getlength(bs);
3568     if (old_size < 0) {
3569         error_setg_errno(errp, -old_size, "Failed to get old image size");
3570         return old_size;
3571     }
3572 
3573     if (bdrv_is_read_only(bs)) {
3574         error_setg(errp, "Image is read-only");
3575         return -EACCES;
3576     }
3577 
3578     if (offset > old_size) {
3579         new_bytes = offset - old_size;
3580     } else {
3581         new_bytes = 0;
3582     }
3583 
3584     bdrv_inc_in_flight(bs);
3585     tracked_request_begin(&req, bs, offset - new_bytes, new_bytes,
3586                           BDRV_TRACKED_TRUNCATE);
3587 
3588     /* If we are growing the image and potentially using preallocation for the
3589      * new area, we need to make sure that no write requests are made to it
3590      * concurrently or they might be overwritten by preallocation. */
3591     if (new_bytes) {
3592         bdrv_make_request_serialising(&req, 1);
3593     }
3594     ret = bdrv_co_write_req_prepare(child, offset - new_bytes, new_bytes, &req,
3595                                     0);
3596     if (ret < 0) {
3597         error_setg_errno(errp, -ret,
3598                          "Failed to prepare request for truncation");
3599         goto out;
3600     }
3601 
3602     filtered = bdrv_filter_child(bs);
3603     backing = bdrv_cow_child(bs);
3604 
3605     /*
3606      * If the image has a backing file that is large enough that it would
3607      * provide data for the new area, we cannot leave it unallocated because
3608      * then the backing file content would become visible. Instead, zero-fill
3609      * the new area.
3610      *
3611      * Note that if the image has a backing file, but was opened without the
3612      * backing file, taking care of keeping things consistent with that backing
3613      * file is the user's responsibility.
3614      */
3615     if (new_bytes && backing) {
3616         int64_t backing_len;
3617 
3618         backing_len = bdrv_co_getlength(backing->bs);
3619         if (backing_len < 0) {
3620             ret = backing_len;
3621             error_setg_errno(errp, -ret, "Could not get backing file size");
3622             goto out;
3623         }
3624 
3625         if (backing_len > old_size) {
3626             flags |= BDRV_REQ_ZERO_WRITE;
3627         }
3628     }
3629 
3630     if (drv->bdrv_co_truncate) {
3631         if (flags & ~bs->supported_truncate_flags) {
3632             error_setg(errp, "Block driver does not support requested flags");
3633             ret = -ENOTSUP;
3634             goto out;
3635         }
3636         ret = drv->bdrv_co_truncate(bs, offset, exact, prealloc, flags, errp);
3637     } else if (filtered) {
3638         ret = bdrv_co_truncate(filtered, offset, exact, prealloc, flags, errp);
3639     } else {
3640         error_setg(errp, "Image format driver does not support resize");
3641         ret = -ENOTSUP;
3642         goto out;
3643     }
3644     if (ret < 0) {
3645         goto out;
3646     }
3647 
3648     ret = bdrv_co_refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
3649     if (ret < 0) {
3650         error_setg_errno(errp, -ret, "Could not refresh total sector count");
3651     } else {
3652         offset = bs->total_sectors * BDRV_SECTOR_SIZE;
3653     }
3654     /*
3655      * It's possible that truncation succeeded but bdrv_refresh_total_sectors
3656      * failed, but the latter doesn't affect how we should finish the request.
3657      * Pass 0 as the last parameter so that dirty bitmaps etc. are handled.
3658      */
3659     bdrv_co_write_req_finish(child, offset - new_bytes, new_bytes, &req, 0);
3660 
3661 out:
3662     tracked_request_end(&req);
3663     bdrv_dec_in_flight(bs);
3664 
3665     return ret;
3666 }
3667 
3668 void bdrv_cancel_in_flight(BlockDriverState *bs)
3669 {
3670     GLOBAL_STATE_CODE();
3671     GRAPH_RDLOCK_GUARD_MAINLOOP();
3672 
3673     if (!bs || !bs->drv) {
3674         return;
3675     }
3676 
3677     if (bs->drv->bdrv_cancel_in_flight) {
3678         bs->drv->bdrv_cancel_in_flight(bs);
3679     }
3680 }
3681 
3682 int coroutine_fn
3683 bdrv_co_preadv_snapshot(BdrvChild *child, int64_t offset, int64_t bytes,
3684                         QEMUIOVector *qiov, size_t qiov_offset)
3685 {
3686     BlockDriverState *bs = child->bs;
3687     BlockDriver *drv = bs->drv;
3688     int ret;
3689     IO_CODE();
3690     assert_bdrv_graph_readable();
3691 
3692     if (!drv) {
3693         return -ENOMEDIUM;
3694     }
3695 
3696     if (!drv->bdrv_co_preadv_snapshot) {
3697         return -ENOTSUP;
3698     }
3699 
3700     bdrv_inc_in_flight(bs);
3701     ret = drv->bdrv_co_preadv_snapshot(bs, offset, bytes, qiov, qiov_offset);
3702     bdrv_dec_in_flight(bs);
3703 
3704     return ret;
3705 }
3706 
3707 int coroutine_fn
3708 bdrv_co_snapshot_block_status(BlockDriverState *bs,
3709                               bool want_zero, int64_t offset, int64_t bytes,
3710                               int64_t *pnum, int64_t *map,
3711                               BlockDriverState **file)
3712 {
3713     BlockDriver *drv = bs->drv;
3714     int ret;
3715     IO_CODE();
3716     assert_bdrv_graph_readable();
3717 
3718     if (!drv) {
3719         return -ENOMEDIUM;
3720     }
3721 
3722     if (!drv->bdrv_co_snapshot_block_status) {
3723         return -ENOTSUP;
3724     }
3725 
3726     bdrv_inc_in_flight(bs);
3727     ret = drv->bdrv_co_snapshot_block_status(bs, want_zero, offset, bytes,
3728                                              pnum, map, file);
3729     bdrv_dec_in_flight(bs);
3730 
3731     return ret;
3732 }
3733 
3734 int coroutine_fn
3735 bdrv_co_pdiscard_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes)
3736 {
3737     BlockDriver *drv = bs->drv;
3738     int ret;
3739     IO_CODE();
3740     assert_bdrv_graph_readable();
3741 
3742     if (!drv) {
3743         return -ENOMEDIUM;
3744     }
3745 
3746     if (!drv->bdrv_co_pdiscard_snapshot) {
3747         return -ENOTSUP;
3748     }
3749 
3750     bdrv_inc_in_flight(bs);
3751     ret = drv->bdrv_co_pdiscard_snapshot(bs, offset, bytes);
3752     bdrv_dec_in_flight(bs);
3753 
3754     return ret;
3755 }
3756