2 * Block layer I/O functions
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.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/write-threshold.h"
34 #include "qemu/cutils.h"
35 #include "qemu/memalign.h"
36 #include "qapi/error.h"
37 #include "qemu/error-report.h"
38 #include "qemu/main-loop.h"
39 #include "sysemu/replay.h"
41 /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
42 #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
44 static void bdrv_parent_cb_resize(BlockDriverState
*bs
);
45 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
46 int64_t offset
, int64_t bytes
, BdrvRequestFlags flags
);
48 static void bdrv_parent_drained_begin(BlockDriverState
*bs
, BdrvChild
*ignore
,
49 bool ignore_bds_parents
)
53 QLIST_FOREACH_SAFE(c
, &bs
->parents
, next_parent
, next
) {
54 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
57 bdrv_parent_drained_begin_single(c
, false);
61 static void bdrv_parent_drained_end_single_no_poll(BdrvChild
*c
,
62 int *drained_end_counter
)
64 assert(c
->parent_quiesce_counter
> 0);
65 c
->parent_quiesce_counter
--;
66 if (c
->klass
->drained_end
) {
67 c
->klass
->drained_end(c
, drained_end_counter
);
71 void bdrv_parent_drained_end_single(BdrvChild
*c
)
73 int drained_end_counter
= 0;
75 bdrv_parent_drained_end_single_no_poll(c
, &drained_end_counter
);
76 BDRV_POLL_WHILE(c
->bs
, qatomic_read(&drained_end_counter
) > 0);
79 static void bdrv_parent_drained_end(BlockDriverState
*bs
, BdrvChild
*ignore
,
80 bool ignore_bds_parents
,
81 int *drained_end_counter
)
85 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
86 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
89 bdrv_parent_drained_end_single_no_poll(c
, drained_end_counter
);
93 static bool bdrv_parent_drained_poll_single(BdrvChild
*c
)
95 if (c
->klass
->drained_poll
) {
96 return c
->klass
->drained_poll(c
);
101 static bool bdrv_parent_drained_poll(BlockDriverState
*bs
, BdrvChild
*ignore
,
102 bool ignore_bds_parents
)
107 QLIST_FOREACH_SAFE(c
, &bs
->parents
, next_parent
, next
) {
108 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
111 busy
|= bdrv_parent_drained_poll_single(c
);
117 void bdrv_parent_drained_begin_single(BdrvChild
*c
, bool poll
)
120 c
->parent_quiesce_counter
++;
121 if (c
->klass
->drained_begin
) {
122 c
->klass
->drained_begin(c
);
125 BDRV_POLL_WHILE(c
->bs
, bdrv_parent_drained_poll_single(c
));
129 static void bdrv_merge_limits(BlockLimits
*dst
, const BlockLimits
*src
)
131 dst
->pdiscard_alignment
= MAX(dst
->pdiscard_alignment
,
132 src
->pdiscard_alignment
);
133 dst
->opt_transfer
= MAX(dst
->opt_transfer
, src
->opt_transfer
);
134 dst
->max_transfer
= MIN_NON_ZERO(dst
->max_transfer
, src
->max_transfer
);
135 dst
->max_hw_transfer
= MIN_NON_ZERO(dst
->max_hw_transfer
,
136 src
->max_hw_transfer
);
137 dst
->opt_mem_alignment
= MAX(dst
->opt_mem_alignment
,
138 src
->opt_mem_alignment
);
139 dst
->min_mem_alignment
= MAX(dst
->min_mem_alignment
,
140 src
->min_mem_alignment
);
141 dst
->max_iov
= MIN_NON_ZERO(dst
->max_iov
, src
->max_iov
);
142 dst
->max_hw_iov
= MIN_NON_ZERO(dst
->max_hw_iov
, src
->max_hw_iov
);
145 typedef struct BdrvRefreshLimitsState
{
146 BlockDriverState
*bs
;
148 } BdrvRefreshLimitsState
;
150 static void bdrv_refresh_limits_abort(void *opaque
)
152 BdrvRefreshLimitsState
*s
= opaque
;
154 s
->bs
->bl
= s
->old_bl
;
157 static TransactionActionDrv bdrv_refresh_limits_drv
= {
158 .abort
= bdrv_refresh_limits_abort
,
162 /* @tran is allowed to be NULL, in this case no rollback is possible. */
163 void bdrv_refresh_limits(BlockDriverState
*bs
, Transaction
*tran
, Error
**errp
)
166 BlockDriver
*drv
= bs
->drv
;
173 BdrvRefreshLimitsState
*s
= g_new(BdrvRefreshLimitsState
, 1);
174 *s
= (BdrvRefreshLimitsState
) {
178 tran_add(tran
, &bdrv_refresh_limits_drv
, s
);
181 memset(&bs
->bl
, 0, sizeof(bs
->bl
));
187 /* Default alignment based on whether driver has byte interface */
188 bs
->bl
.request_alignment
= (drv
->bdrv_co_preadv
||
189 drv
->bdrv_aio_preadv
||
190 drv
->bdrv_co_preadv_part
) ? 1 : 512;
192 /* Take some limits from the children as a default */
194 QLIST_FOREACH(c
, &bs
->children
, next
) {
195 if (c
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_FILTERED
| BDRV_CHILD_COW
))
197 bdrv_merge_limits(&bs
->bl
, &c
->bs
->bl
);
203 bs
->bl
.min_mem_alignment
= 512;
204 bs
->bl
.opt_mem_alignment
= qemu_real_host_page_size();
206 /* Safe default since most protocols use readv()/writev()/etc */
207 bs
->bl
.max_iov
= IOV_MAX
;
210 /* Then let the driver override it */
211 if (drv
->bdrv_refresh_limits
) {
212 drv
->bdrv_refresh_limits(bs
, errp
);
218 if (bs
->bl
.request_alignment
> BDRV_MAX_ALIGNMENT
) {
219 error_setg(errp
, "Driver requires too large request alignment");
224 * The copy-on-read flag is actually a reference count so multiple users may
225 * use the feature without worrying about clobbering its previous state.
226 * Copy-on-read stays enabled until all users have called to disable it.
228 void bdrv_enable_copy_on_read(BlockDriverState
*bs
)
231 qatomic_inc(&bs
->copy_on_read
);
234 void bdrv_disable_copy_on_read(BlockDriverState
*bs
)
236 int old
= qatomic_fetch_dec(&bs
->copy_on_read
);
243 BlockDriverState
*bs
;
249 bool ignore_bds_parents
;
250 int *drained_end_counter
;
253 static void coroutine_fn
bdrv_drain_invoke_entry(void *opaque
)
255 BdrvCoDrainData
*data
= opaque
;
256 BlockDriverState
*bs
= data
->bs
;
259 bs
->drv
->bdrv_co_drain_begin(bs
);
261 bs
->drv
->bdrv_co_drain_end(bs
);
264 /* Set data->done and decrement drained_end_counter before bdrv_wakeup() */
265 qatomic_mb_set(&data
->done
, true);
267 qatomic_dec(data
->drained_end_counter
);
269 bdrv_dec_in_flight(bs
);
274 /* Recursively call BlockDriver.bdrv_co_drain_begin/end callbacks */
275 static void bdrv_drain_invoke(BlockDriverState
*bs
, bool begin
,
276 int *drained_end_counter
)
278 BdrvCoDrainData
*data
;
280 if (!bs
->drv
|| (begin
&& !bs
->drv
->bdrv_co_drain_begin
) ||
281 (!begin
&& !bs
->drv
->bdrv_co_drain_end
)) {
285 data
= g_new(BdrvCoDrainData
, 1);
286 *data
= (BdrvCoDrainData
) {
290 .drained_end_counter
= drained_end_counter
,
294 qatomic_inc(drained_end_counter
);
297 /* Make sure the driver callback completes during the polling phase for
299 bdrv_inc_in_flight(bs
);
300 data
->co
= qemu_coroutine_create(bdrv_drain_invoke_entry
, data
);
301 aio_co_schedule(bdrv_get_aio_context(bs
), data
->co
);
304 /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
305 bool bdrv_drain_poll(BlockDriverState
*bs
, bool recursive
,
306 BdrvChild
*ignore_parent
, bool ignore_bds_parents
)
308 BdrvChild
*child
, *next
;
311 if (bdrv_parent_drained_poll(bs
, ignore_parent
, ignore_bds_parents
)) {
315 if (qatomic_read(&bs
->in_flight
)) {
320 assert(!ignore_bds_parents
);
321 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
322 if (bdrv_drain_poll(child
->bs
, recursive
, child
, false)) {
331 static bool bdrv_drain_poll_top_level(BlockDriverState
*bs
, bool recursive
,
332 BdrvChild
*ignore_parent
)
334 return bdrv_drain_poll(bs
, recursive
, ignore_parent
, false);
337 static void bdrv_do_drained_begin(BlockDriverState
*bs
, bool recursive
,
338 BdrvChild
*parent
, bool ignore_bds_parents
,
340 static void bdrv_do_drained_end(BlockDriverState
*bs
, bool recursive
,
341 BdrvChild
*parent
, bool ignore_bds_parents
,
342 int *drained_end_counter
);
344 static void bdrv_co_drain_bh_cb(void *opaque
)
346 BdrvCoDrainData
*data
= opaque
;
347 Coroutine
*co
= data
->co
;
348 BlockDriverState
*bs
= data
->bs
;
351 AioContext
*ctx
= bdrv_get_aio_context(bs
);
352 aio_context_acquire(ctx
);
353 bdrv_dec_in_flight(bs
);
355 assert(!data
->drained_end_counter
);
356 bdrv_do_drained_begin(bs
, data
->recursive
, data
->parent
,
357 data
->ignore_bds_parents
, data
->poll
);
360 bdrv_do_drained_end(bs
, data
->recursive
, data
->parent
,
361 data
->ignore_bds_parents
,
362 data
->drained_end_counter
);
364 aio_context_release(ctx
);
367 bdrv_drain_all_begin();
374 static void coroutine_fn
bdrv_co_yield_to_drain(BlockDriverState
*bs
,
375 bool begin
, bool recursive
,
377 bool ignore_bds_parents
,
379 int *drained_end_counter
)
381 BdrvCoDrainData data
;
382 Coroutine
*self
= qemu_coroutine_self();
383 AioContext
*ctx
= bdrv_get_aio_context(bs
);
384 AioContext
*co_ctx
= qemu_coroutine_get_aio_context(self
);
386 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
387 * other coroutines run if they were queued by aio_co_enter(). */
389 assert(qemu_in_coroutine());
390 data
= (BdrvCoDrainData
) {
395 .recursive
= recursive
,
397 .ignore_bds_parents
= ignore_bds_parents
,
399 .drained_end_counter
= drained_end_counter
,
403 bdrv_inc_in_flight(bs
);
407 * Temporarily drop the lock across yield or we would get deadlocks.
408 * bdrv_co_drain_bh_cb() reaquires the lock as needed.
410 * When we yield below, the lock for the current context will be
411 * released, so if this is actually the lock that protects bs, don't drop
415 aio_context_release(ctx
);
417 replay_bh_schedule_oneshot_event(ctx
, bdrv_co_drain_bh_cb
, &data
);
419 qemu_coroutine_yield();
420 /* If we are resumed from some other event (such as an aio completion or a
421 * timer callback), it is a bug in the caller that should be fixed. */
424 /* Reaquire the AioContext of bs if we dropped it */
426 aio_context_acquire(ctx
);
430 void bdrv_do_drained_begin_quiesce(BlockDriverState
*bs
,
431 BdrvChild
*parent
, bool ignore_bds_parents
)
434 assert(!qemu_in_coroutine());
436 /* Stop things in parent-to-child order */
437 if (qatomic_fetch_inc(&bs
->quiesce_counter
) == 0) {
438 aio_disable_external(bdrv_get_aio_context(bs
));
441 bdrv_parent_drained_begin(bs
, parent
, ignore_bds_parents
);
442 bdrv_drain_invoke(bs
, true, NULL
);
445 static void bdrv_do_drained_begin(BlockDriverState
*bs
, bool recursive
,
446 BdrvChild
*parent
, bool ignore_bds_parents
,
449 BdrvChild
*child
, *next
;
451 if (qemu_in_coroutine()) {
452 bdrv_co_yield_to_drain(bs
, true, recursive
, parent
, ignore_bds_parents
,
457 bdrv_do_drained_begin_quiesce(bs
, parent
, ignore_bds_parents
);
460 assert(!ignore_bds_parents
);
461 bs
->recursive_quiesce_counter
++;
462 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
463 bdrv_do_drained_begin(child
->bs
, true, child
, ignore_bds_parents
,
469 * Wait for drained requests to finish.
471 * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
472 * call is needed so things in this AioContext can make progress even
473 * though we don't return to the main AioContext loop - this automatically
474 * includes other nodes in the same AioContext and therefore all child
478 assert(!ignore_bds_parents
);
479 BDRV_POLL_WHILE(bs
, bdrv_drain_poll_top_level(bs
, recursive
, parent
));
483 void bdrv_drained_begin(BlockDriverState
*bs
)
486 bdrv_do_drained_begin(bs
, false, NULL
, false, true);
489 void bdrv_subtree_drained_begin(BlockDriverState
*bs
)
492 bdrv_do_drained_begin(bs
, true, NULL
, false, true);
496 * This function does not poll, nor must any of its recursively called
497 * functions. The *drained_end_counter pointee will be incremented
498 * once for every background operation scheduled, and decremented once
499 * the operation settles. Therefore, the pointer must remain valid
500 * until the pointee reaches 0. That implies that whoever sets up the
501 * pointee has to poll until it is 0.
503 * We use atomic operations to access *drained_end_counter, because
504 * (1) when called from bdrv_set_aio_context_ignore(), the subgraph of
505 * @bs may contain nodes in different AioContexts,
506 * (2) bdrv_drain_all_end() uses the same counter for all nodes,
507 * regardless of which AioContext they are in.
509 static void bdrv_do_drained_end(BlockDriverState
*bs
, bool recursive
,
510 BdrvChild
*parent
, bool ignore_bds_parents
,
511 int *drained_end_counter
)
514 int old_quiesce_counter
;
516 assert(drained_end_counter
!= NULL
);
518 if (qemu_in_coroutine()) {
519 bdrv_co_yield_to_drain(bs
, false, recursive
, parent
, ignore_bds_parents
,
520 false, drained_end_counter
);
523 assert(bs
->quiesce_counter
> 0);
525 /* Re-enable things in child-to-parent order */
526 bdrv_drain_invoke(bs
, false, drained_end_counter
);
527 bdrv_parent_drained_end(bs
, parent
, ignore_bds_parents
,
528 drained_end_counter
);
530 old_quiesce_counter
= qatomic_fetch_dec(&bs
->quiesce_counter
);
531 if (old_quiesce_counter
== 1) {
532 aio_enable_external(bdrv_get_aio_context(bs
));
536 assert(!ignore_bds_parents
);
537 bs
->recursive_quiesce_counter
--;
538 QLIST_FOREACH(child
, &bs
->children
, next
) {
539 bdrv_do_drained_end(child
->bs
, true, child
, ignore_bds_parents
,
540 drained_end_counter
);
545 void bdrv_drained_end(BlockDriverState
*bs
)
547 int drained_end_counter
= 0;
549 bdrv_do_drained_end(bs
, false, NULL
, false, &drained_end_counter
);
550 BDRV_POLL_WHILE(bs
, qatomic_read(&drained_end_counter
) > 0);
553 void bdrv_drained_end_no_poll(BlockDriverState
*bs
, int *drained_end_counter
)
556 bdrv_do_drained_end(bs
, false, NULL
, false, drained_end_counter
);
559 void bdrv_subtree_drained_end(BlockDriverState
*bs
)
561 int drained_end_counter
= 0;
563 bdrv_do_drained_end(bs
, true, NULL
, false, &drained_end_counter
);
564 BDRV_POLL_WHILE(bs
, qatomic_read(&drained_end_counter
) > 0);
567 void bdrv_apply_subtree_drain(BdrvChild
*child
, BlockDriverState
*new_parent
)
572 for (i
= 0; i
< new_parent
->recursive_quiesce_counter
; i
++) {
573 bdrv_do_drained_begin(child
->bs
, true, child
, false, true);
577 void bdrv_unapply_subtree_drain(BdrvChild
*child
, BlockDriverState
*old_parent
)
579 int drained_end_counter
= 0;
583 for (i
= 0; i
< old_parent
->recursive_quiesce_counter
; i
++) {
584 bdrv_do_drained_end(child
->bs
, true, child
, false,
585 &drained_end_counter
);
588 BDRV_POLL_WHILE(child
->bs
, qatomic_read(&drained_end_counter
) > 0);
591 void bdrv_drain(BlockDriverState
*bs
)
594 bdrv_drained_begin(bs
);
595 bdrv_drained_end(bs
);
598 static void bdrv_drain_assert_idle(BlockDriverState
*bs
)
600 BdrvChild
*child
, *next
;
602 assert(qatomic_read(&bs
->in_flight
) == 0);
603 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
604 bdrv_drain_assert_idle(child
->bs
);
608 unsigned int bdrv_drain_all_count
= 0;
610 static bool bdrv_drain_all_poll(void)
612 BlockDriverState
*bs
= NULL
;
616 /* bdrv_drain_poll() can't make changes to the graph and we are holding the
617 * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
618 while ((bs
= bdrv_next_all_states(bs
))) {
619 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
620 aio_context_acquire(aio_context
);
621 result
|= bdrv_drain_poll(bs
, false, NULL
, true);
622 aio_context_release(aio_context
);
629 * Wait for pending requests to complete across all BlockDriverStates
631 * This function does not flush data to disk, use bdrv_flush_all() for that
632 * after calling this function.
634 * This pauses all block jobs and disables external clients. It must
635 * be paired with bdrv_drain_all_end().
637 * NOTE: no new block jobs or BlockDriverStates can be created between
638 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
640 void bdrv_drain_all_begin(void)
642 BlockDriverState
*bs
= NULL
;
645 if (qemu_in_coroutine()) {
646 bdrv_co_yield_to_drain(NULL
, true, false, NULL
, true, true, NULL
);
651 * bdrv queue is managed by record/replay,
652 * waiting for finishing the I/O requests may
655 if (replay_events_enabled()) {
659 /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
660 * loop AioContext, so make sure we're in the main context. */
661 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
662 assert(bdrv_drain_all_count
< INT_MAX
);
663 bdrv_drain_all_count
++;
665 /* Quiesce all nodes, without polling in-flight requests yet. The graph
666 * cannot change during this loop. */
667 while ((bs
= bdrv_next_all_states(bs
))) {
668 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
670 aio_context_acquire(aio_context
);
671 bdrv_do_drained_begin(bs
, false, NULL
, true, false);
672 aio_context_release(aio_context
);
675 /* Now poll the in-flight requests */
676 AIO_WAIT_WHILE(NULL
, bdrv_drain_all_poll());
678 while ((bs
= bdrv_next_all_states(bs
))) {
679 bdrv_drain_assert_idle(bs
);
683 void bdrv_drain_all_end_quiesce(BlockDriverState
*bs
)
685 int drained_end_counter
= 0;
688 g_assert(bs
->quiesce_counter
> 0);
689 g_assert(!bs
->refcnt
);
691 while (bs
->quiesce_counter
) {
692 bdrv_do_drained_end(bs
, false, NULL
, true, &drained_end_counter
);
694 BDRV_POLL_WHILE(bs
, qatomic_read(&drained_end_counter
) > 0);
697 void bdrv_drain_all_end(void)
699 BlockDriverState
*bs
= NULL
;
700 int drained_end_counter
= 0;
704 * bdrv queue is managed by record/replay,
705 * waiting for finishing the I/O requests may
708 if (replay_events_enabled()) {
712 while ((bs
= bdrv_next_all_states(bs
))) {
713 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
715 aio_context_acquire(aio_context
);
716 bdrv_do_drained_end(bs
, false, NULL
, true, &drained_end_counter
);
717 aio_context_release(aio_context
);
720 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
721 AIO_WAIT_WHILE(NULL
, qatomic_read(&drained_end_counter
) > 0);
723 assert(bdrv_drain_all_count
> 0);
724 bdrv_drain_all_count
--;
727 void bdrv_drain_all(void)
730 bdrv_drain_all_begin();
731 bdrv_drain_all_end();
735 * Remove an active request from the tracked requests list
737 * This function should be called when a tracked request is completing.
739 static void coroutine_fn
tracked_request_end(BdrvTrackedRequest
*req
)
741 if (req
->serialising
) {
742 qatomic_dec(&req
->bs
->serialising_in_flight
);
745 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
746 QLIST_REMOVE(req
, list
);
747 qemu_co_queue_restart_all(&req
->wait_queue
);
748 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
752 * Add an active request to the tracked requests list
754 static void tracked_request_begin(BdrvTrackedRequest
*req
,
755 BlockDriverState
*bs
,
758 enum BdrvTrackedRequestType type
)
760 bdrv_check_request(offset
, bytes
, &error_abort
);
762 *req
= (BdrvTrackedRequest
){
767 .co
= qemu_coroutine_self(),
768 .serialising
= false,
769 .overlap_offset
= offset
,
770 .overlap_bytes
= bytes
,
773 qemu_co_queue_init(&req
->wait_queue
);
775 qemu_co_mutex_lock(&bs
->reqs_lock
);
776 QLIST_INSERT_HEAD(&bs
->tracked_requests
, req
, list
);
777 qemu_co_mutex_unlock(&bs
->reqs_lock
);
780 static bool tracked_request_overlaps(BdrvTrackedRequest
*req
,
781 int64_t offset
, int64_t bytes
)
783 bdrv_check_request(offset
, bytes
, &error_abort
);
786 if (offset
>= req
->overlap_offset
+ req
->overlap_bytes
) {
790 if (req
->overlap_offset
>= offset
+ bytes
) {
796 /* Called with self->bs->reqs_lock held */
797 static BdrvTrackedRequest
*
798 bdrv_find_conflicting_request(BdrvTrackedRequest
*self
)
800 BdrvTrackedRequest
*req
;
802 QLIST_FOREACH(req
, &self
->bs
->tracked_requests
, list
) {
803 if (req
== self
|| (!req
->serialising
&& !self
->serialising
)) {
806 if (tracked_request_overlaps(req
, self
->overlap_offset
,
807 self
->overlap_bytes
))
810 * Hitting this means there was a reentrant request, for
811 * example, a block driver issuing nested requests. This must
812 * never happen since it means deadlock.
814 assert(qemu_coroutine_self() != req
->co
);
817 * If the request is already (indirectly) waiting for us, or
818 * will wait for us as soon as it wakes up, then just go on
819 * (instead of producing a deadlock in the former case).
821 if (!req
->waiting_for
) {
830 /* Called with self->bs->reqs_lock held */
831 static void coroutine_fn
832 bdrv_wait_serialising_requests_locked(BdrvTrackedRequest
*self
)
834 BdrvTrackedRequest
*req
;
836 while ((req
= bdrv_find_conflicting_request(self
))) {
837 self
->waiting_for
= req
;
838 qemu_co_queue_wait(&req
->wait_queue
, &self
->bs
->reqs_lock
);
839 self
->waiting_for
= NULL
;
843 /* Called with req->bs->reqs_lock held */
844 static void tracked_request_set_serialising(BdrvTrackedRequest
*req
,
847 int64_t overlap_offset
= req
->offset
& ~(align
- 1);
848 int64_t overlap_bytes
=
849 ROUND_UP(req
->offset
+ req
->bytes
, align
) - overlap_offset
;
851 bdrv_check_request(req
->offset
, req
->bytes
, &error_abort
);
853 if (!req
->serialising
) {
854 qatomic_inc(&req
->bs
->serialising_in_flight
);
855 req
->serialising
= true;
858 req
->overlap_offset
= MIN(req
->overlap_offset
, overlap_offset
);
859 req
->overlap_bytes
= MAX(req
->overlap_bytes
, overlap_bytes
);
863 * Return the tracked request on @bs for the current coroutine, or
864 * NULL if there is none.
866 BdrvTrackedRequest
*coroutine_fn
bdrv_co_get_self_request(BlockDriverState
*bs
)
868 BdrvTrackedRequest
*req
;
869 Coroutine
*self
= qemu_coroutine_self();
872 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
873 if (req
->co
== self
) {
882 * Round a region to cluster boundaries
884 void bdrv_round_to_clusters(BlockDriverState
*bs
,
885 int64_t offset
, int64_t bytes
,
886 int64_t *cluster_offset
,
887 int64_t *cluster_bytes
)
891 if (bdrv_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
892 *cluster_offset
= offset
;
893 *cluster_bytes
= bytes
;
895 int64_t c
= bdi
.cluster_size
;
896 *cluster_offset
= QEMU_ALIGN_DOWN(offset
, c
);
897 *cluster_bytes
= QEMU_ALIGN_UP(offset
- *cluster_offset
+ bytes
, c
);
901 static int bdrv_get_cluster_size(BlockDriverState
*bs
)
906 ret
= bdrv_get_info(bs
, &bdi
);
907 if (ret
< 0 || bdi
.cluster_size
== 0) {
908 return bs
->bl
.request_alignment
;
910 return bdi
.cluster_size
;
914 void bdrv_inc_in_flight(BlockDriverState
*bs
)
917 qatomic_inc(&bs
->in_flight
);
920 void bdrv_wakeup(BlockDriverState
*bs
)
926 void bdrv_dec_in_flight(BlockDriverState
*bs
)
929 qatomic_dec(&bs
->in_flight
);
933 static void coroutine_fn
934 bdrv_wait_serialising_requests(BdrvTrackedRequest
*self
)
936 BlockDriverState
*bs
= self
->bs
;
938 if (!qatomic_read(&bs
->serialising_in_flight
)) {
942 qemu_co_mutex_lock(&bs
->reqs_lock
);
943 bdrv_wait_serialising_requests_locked(self
);
944 qemu_co_mutex_unlock(&bs
->reqs_lock
);
947 void coroutine_fn
bdrv_make_request_serialising(BdrvTrackedRequest
*req
,
952 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
954 tracked_request_set_serialising(req
, align
);
955 bdrv_wait_serialising_requests_locked(req
);
957 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
960 int bdrv_check_qiov_request(int64_t offset
, int64_t bytes
,
961 QEMUIOVector
*qiov
, size_t qiov_offset
,
965 * Check generic offset/bytes correctness
969 error_setg(errp
, "offset is negative: %" PRIi64
, offset
);
974 error_setg(errp
, "bytes is negative: %" PRIi64
, bytes
);
978 if (bytes
> BDRV_MAX_LENGTH
) {
979 error_setg(errp
, "bytes(%" PRIi64
") exceeds maximum(%" PRIi64
")",
980 bytes
, BDRV_MAX_LENGTH
);
984 if (offset
> BDRV_MAX_LENGTH
) {
985 error_setg(errp
, "offset(%" PRIi64
") exceeds maximum(%" PRIi64
")",
986 offset
, BDRV_MAX_LENGTH
);
990 if (offset
> BDRV_MAX_LENGTH
- bytes
) {
991 error_setg(errp
, "sum of offset(%" PRIi64
") and bytes(%" PRIi64
") "
992 "exceeds maximum(%" PRIi64
")", offset
, bytes
,
1002 * Check qiov and qiov_offset
1005 if (qiov_offset
> qiov
->size
) {
1006 error_setg(errp
, "qiov_offset(%zu) overflow io vector size(%zu)",
1007 qiov_offset
, qiov
->size
);
1011 if (bytes
> qiov
->size
- qiov_offset
) {
1012 error_setg(errp
, "bytes(%" PRIi64
") + qiov_offset(%zu) overflow io "
1013 "vector size(%zu)", bytes
, qiov_offset
, qiov
->size
);
1020 int bdrv_check_request(int64_t offset
, int64_t bytes
, Error
**errp
)
1022 return bdrv_check_qiov_request(offset
, bytes
, NULL
, 0, errp
);
1025 static int bdrv_check_request32(int64_t offset
, int64_t bytes
,
1026 QEMUIOVector
*qiov
, size_t qiov_offset
)
1028 int ret
= bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, NULL
);
1033 if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
1041 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
1042 * The operation is sped up by checking the block status and only writing
1043 * zeroes to the device if they currently do not return zeroes. Optional
1044 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
1047 * Returns < 0 on error, 0 on success. For error codes see bdrv_pwrite().
1049 int bdrv_make_zero(BdrvChild
*child
, BdrvRequestFlags flags
)
1052 int64_t target_size
, bytes
, offset
= 0;
1053 BlockDriverState
*bs
= child
->bs
;
1056 target_size
= bdrv_getlength(bs
);
1057 if (target_size
< 0) {
1062 bytes
= MIN(target_size
- offset
, BDRV_REQUEST_MAX_BYTES
);
1066 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, NULL
, NULL
);
1070 if (ret
& BDRV_BLOCK_ZERO
) {
1074 ret
= bdrv_pwrite_zeroes(child
, offset
, bytes
, flags
);
1083 * Writes to the file and ensures that no writes are reordered across this
1084 * request (acts as a barrier)
1086 * Returns 0 on success, -errno in error cases.
1088 int coroutine_fn
bdrv_co_pwrite_sync(BdrvChild
*child
, int64_t offset
,
1089 int64_t bytes
, const void *buf
,
1090 BdrvRequestFlags flags
)
1095 ret
= bdrv_co_pwrite(child
, offset
, bytes
, buf
, flags
);
1100 ret
= bdrv_co_flush(child
->bs
);
1108 typedef struct CoroutineIOCompletion
{
1109 Coroutine
*coroutine
;
1111 } CoroutineIOCompletion
;
1113 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
1115 CoroutineIOCompletion
*co
= opaque
;
1118 aio_co_wake(co
->coroutine
);
1121 static int coroutine_fn
bdrv_driver_preadv(BlockDriverState
*bs
,
1122 int64_t offset
, int64_t bytes
,
1124 size_t qiov_offset
, int flags
)
1126 BlockDriver
*drv
= bs
->drv
;
1128 unsigned int nb_sectors
;
1129 QEMUIOVector local_qiov
;
1132 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1133 assert(!(flags
& ~BDRV_REQ_MASK
));
1134 assert(!(flags
& BDRV_REQ_NO_FALLBACK
));
1140 if (drv
->bdrv_co_preadv_part
) {
1141 return drv
->bdrv_co_preadv_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
1145 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
1146 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1150 if (drv
->bdrv_co_preadv
) {
1151 ret
= drv
->bdrv_co_preadv(bs
, offset
, bytes
, qiov
, flags
);
1155 if (drv
->bdrv_aio_preadv
) {
1157 CoroutineIOCompletion co
= {
1158 .coroutine
= qemu_coroutine_self(),
1161 acb
= drv
->bdrv_aio_preadv(bs
, offset
, bytes
, qiov
, flags
,
1162 bdrv_co_io_em_complete
, &co
);
1167 qemu_coroutine_yield();
1173 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1174 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1176 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1177 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1178 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1179 assert(drv
->bdrv_co_readv
);
1181 ret
= drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
1184 if (qiov
== &local_qiov
) {
1185 qemu_iovec_destroy(&local_qiov
);
1191 static int coroutine_fn
bdrv_driver_pwritev(BlockDriverState
*bs
,
1192 int64_t offset
, int64_t bytes
,
1195 BdrvRequestFlags flags
)
1197 BlockDriver
*drv
= bs
->drv
;
1199 unsigned int nb_sectors
;
1200 QEMUIOVector local_qiov
;
1203 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1204 assert(!(flags
& ~BDRV_REQ_MASK
));
1205 assert(!(flags
& BDRV_REQ_NO_FALLBACK
));
1211 if (drv
->bdrv_co_pwritev_part
) {
1212 ret
= drv
->bdrv_co_pwritev_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
1213 flags
& bs
->supported_write_flags
);
1214 flags
&= ~bs
->supported_write_flags
;
1218 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
1219 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1223 if (drv
->bdrv_co_pwritev
) {
1224 ret
= drv
->bdrv_co_pwritev(bs
, offset
, bytes
, qiov
,
1225 flags
& bs
->supported_write_flags
);
1226 flags
&= ~bs
->supported_write_flags
;
1230 if (drv
->bdrv_aio_pwritev
) {
1232 CoroutineIOCompletion co
= {
1233 .coroutine
= qemu_coroutine_self(),
1236 acb
= drv
->bdrv_aio_pwritev(bs
, offset
, bytes
, qiov
,
1237 flags
& bs
->supported_write_flags
,
1238 bdrv_co_io_em_complete
, &co
);
1239 flags
&= ~bs
->supported_write_flags
;
1243 qemu_coroutine_yield();
1249 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1250 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1252 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1253 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1254 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1256 assert(drv
->bdrv_co_writev
);
1257 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
,
1258 flags
& bs
->supported_write_flags
);
1259 flags
&= ~bs
->supported_write_flags
;
1262 if (ret
== 0 && (flags
& BDRV_REQ_FUA
)) {
1263 ret
= bdrv_co_flush(bs
);
1266 if (qiov
== &local_qiov
) {
1267 qemu_iovec_destroy(&local_qiov
);
1273 static int coroutine_fn
1274 bdrv_driver_pwritev_compressed(BlockDriverState
*bs
, int64_t offset
,
1275 int64_t bytes
, QEMUIOVector
*qiov
,
1278 BlockDriver
*drv
= bs
->drv
;
1279 QEMUIOVector local_qiov
;
1282 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1288 if (!block_driver_can_compress(drv
)) {
1292 if (drv
->bdrv_co_pwritev_compressed_part
) {
1293 return drv
->bdrv_co_pwritev_compressed_part(bs
, offset
, bytes
,
1297 if (qiov_offset
== 0) {
1298 return drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, qiov
);
1301 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1302 ret
= drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, &local_qiov
);
1303 qemu_iovec_destroy(&local_qiov
);
1308 static int coroutine_fn
bdrv_co_do_copy_on_readv(BdrvChild
*child
,
1309 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
1310 size_t qiov_offset
, int flags
)
1312 BlockDriverState
*bs
= child
->bs
;
1314 /* Perform I/O through a temporary buffer so that users who scribble over
1315 * their read buffer while the operation is in progress do not end up
1316 * modifying the image file. This is critical for zero-copy guest I/O
1317 * where anything might happen inside guest memory.
1319 void *bounce_buffer
= NULL
;
1321 BlockDriver
*drv
= bs
->drv
;
1322 int64_t cluster_offset
;
1323 int64_t cluster_bytes
;
1326 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
,
1327 BDRV_REQUEST_MAX_BYTES
);
1328 int64_t progress
= 0;
1331 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1338 * Do not write anything when the BDS is inactive. That is not
1339 * allowed, and it would not help.
1341 skip_write
= (bs
->open_flags
& BDRV_O_INACTIVE
);
1343 /* FIXME We cannot require callers to have write permissions when all they
1344 * are doing is a read request. If we did things right, write permissions
1345 * would be obtained anyway, but internally by the copy-on-read code. As
1346 * long as it is implemented here rather than in a separate filter driver,
1347 * the copy-on-read code doesn't have its own BdrvChild, however, for which
1348 * it could request permissions. Therefore we have to bypass the permission
1349 * system for the moment. */
1350 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1352 /* Cover entire cluster so no additional backing file I/O is required when
1353 * allocating cluster in the image file. Note that this value may exceed
1354 * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1355 * is one reason we loop rather than doing it all at once.
1357 bdrv_round_to_clusters(bs
, offset
, bytes
, &cluster_offset
, &cluster_bytes
);
1358 skip_bytes
= offset
- cluster_offset
;
1360 trace_bdrv_co_do_copy_on_readv(bs
, offset
, bytes
,
1361 cluster_offset
, cluster_bytes
);
1363 while (cluster_bytes
) {
1367 ret
= 1; /* "already allocated", so nothing will be copied */
1368 pnum
= MIN(cluster_bytes
, max_transfer
);
1370 ret
= bdrv_is_allocated(bs
, cluster_offset
,
1371 MIN(cluster_bytes
, max_transfer
), &pnum
);
1374 * Safe to treat errors in querying allocation as if
1375 * unallocated; we'll probably fail again soon on the
1376 * read, but at least that will set a decent errno.
1378 pnum
= MIN(cluster_bytes
, max_transfer
);
1381 /* Stop at EOF if the image ends in the middle of the cluster */
1382 if (ret
== 0 && pnum
== 0) {
1383 assert(progress
>= bytes
);
1387 assert(skip_bytes
< pnum
);
1391 QEMUIOVector local_qiov
;
1393 /* Must copy-on-read; use the bounce buffer */
1394 pnum
= MIN(pnum
, MAX_BOUNCE_BUFFER
);
1395 if (!bounce_buffer
) {
1396 int64_t max_we_need
= MAX(pnum
, cluster_bytes
- pnum
);
1397 int64_t max_allowed
= MIN(max_transfer
, MAX_BOUNCE_BUFFER
);
1398 int64_t bounce_buffer_len
= MIN(max_we_need
, max_allowed
);
1400 bounce_buffer
= qemu_try_blockalign(bs
, bounce_buffer_len
);
1401 if (!bounce_buffer
) {
1406 qemu_iovec_init_buf(&local_qiov
, bounce_buffer
, pnum
);
1408 ret
= bdrv_driver_preadv(bs
, cluster_offset
, pnum
,
1414 bdrv_debug_event(bs
, BLKDBG_COR_WRITE
);
1415 if (drv
->bdrv_co_pwrite_zeroes
&&
1416 buffer_is_zero(bounce_buffer
, pnum
)) {
1417 /* FIXME: Should we (perhaps conditionally) be setting
1418 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1419 * that still correctly reads as zero? */
1420 ret
= bdrv_co_do_pwrite_zeroes(bs
, cluster_offset
, pnum
,
1421 BDRV_REQ_WRITE_UNCHANGED
);
1423 /* This does not change the data on the disk, it is not
1424 * necessary to flush even in cache=writethrough mode.
1426 ret
= bdrv_driver_pwritev(bs
, cluster_offset
, pnum
,
1428 BDRV_REQ_WRITE_UNCHANGED
);
1432 /* It might be okay to ignore write errors for guest
1433 * requests. If this is a deliberate copy-on-read
1434 * then we don't want to ignore the error. Simply
1435 * report it in all cases.
1440 if (!(flags
& BDRV_REQ_PREFETCH
)) {
1441 qemu_iovec_from_buf(qiov
, qiov_offset
+ progress
,
1442 bounce_buffer
+ skip_bytes
,
1443 MIN(pnum
- skip_bytes
, bytes
- progress
));
1445 } else if (!(flags
& BDRV_REQ_PREFETCH
)) {
1446 /* Read directly into the destination */
1447 ret
= bdrv_driver_preadv(bs
, offset
+ progress
,
1448 MIN(pnum
- skip_bytes
, bytes
- progress
),
1449 qiov
, qiov_offset
+ progress
, 0);
1455 cluster_offset
+= pnum
;
1456 cluster_bytes
-= pnum
;
1457 progress
+= pnum
- skip_bytes
;
1463 qemu_vfree(bounce_buffer
);
1468 * Forwards an already correctly aligned request to the BlockDriver. This
1469 * handles copy on read, zeroing after EOF, and fragmentation of large
1470 * reads; any other features must be implemented by the caller.
1472 static int coroutine_fn
bdrv_aligned_preadv(BdrvChild
*child
,
1473 BdrvTrackedRequest
*req
, int64_t offset
, int64_t bytes
,
1474 int64_t align
, QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
1476 BlockDriverState
*bs
= child
->bs
;
1477 int64_t total_bytes
, max_bytes
;
1479 int64_t bytes_remaining
= bytes
;
1482 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1483 assert(is_power_of_2(align
));
1484 assert((offset
& (align
- 1)) == 0);
1485 assert((bytes
& (align
- 1)) == 0);
1486 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1487 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1490 /* TODO: We would need a per-BDS .supported_read_flags and
1491 * potential fallback support, if we ever implement any read flags
1492 * to pass through to drivers. For now, there aren't any
1493 * passthrough flags. */
1494 assert(!(flags
& ~(BDRV_REQ_COPY_ON_READ
| BDRV_REQ_PREFETCH
)));
1496 /* Handle Copy on Read and associated serialisation */
1497 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1498 /* If we touch the same cluster it counts as an overlap. This
1499 * guarantees that allocating writes will be serialized and not race
1500 * with each other for the same cluster. For example, in copy-on-read
1501 * it ensures that the CoR read and write operations are atomic and
1502 * guest writes cannot interleave between them. */
1503 bdrv_make_request_serialising(req
, bdrv_get_cluster_size(bs
));
1505 bdrv_wait_serialising_requests(req
);
1508 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1511 /* The flag BDRV_REQ_COPY_ON_READ has reached its addressee */
1512 flags
&= ~BDRV_REQ_COPY_ON_READ
;
1514 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &pnum
);
1519 if (!ret
|| pnum
!= bytes
) {
1520 ret
= bdrv_co_do_copy_on_readv(child
, offset
, bytes
,
1521 qiov
, qiov_offset
, flags
);
1523 } else if (flags
& BDRV_REQ_PREFETCH
) {
1528 /* Forward the request to the BlockDriver, possibly fragmenting it */
1529 total_bytes
= bdrv_getlength(bs
);
1530 if (total_bytes
< 0) {
1535 assert(!(flags
& ~bs
->supported_read_flags
));
1537 max_bytes
= ROUND_UP(MAX(0, total_bytes
- offset
), align
);
1538 if (bytes
<= max_bytes
&& bytes
<= max_transfer
) {
1539 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1543 while (bytes_remaining
) {
1547 num
= MIN(bytes_remaining
, MIN(max_bytes
, max_transfer
));
1550 ret
= bdrv_driver_preadv(bs
, offset
+ bytes
- bytes_remaining
,
1552 qiov_offset
+ bytes
- bytes_remaining
,
1556 num
= bytes_remaining
;
1557 ret
= qemu_iovec_memset(qiov
, qiov_offset
+ bytes
- bytes_remaining
,
1558 0, bytes_remaining
);
1563 bytes_remaining
-= num
;
1567 return ret
< 0 ? ret
: 0;
1573 * |<---- align ----->| |<----- align ---->|
1574 * |<- head ->|<------------- bytes ------------->|<-- tail -->|
1576 * -*----------$-------*-------- ... --------*-----$------------*---
1578 * | offset | | end |
1579 * ALIGN_DOWN(offset) ALIGN_UP(offset) ALIGN_DOWN(end) ALIGN_UP(end)
1580 * [buf ... ) [tail_buf )
1582 * @buf is an aligned allocation needed to store @head and @tail paddings. @head
1583 * is placed at the beginning of @buf and @tail at the @end.
1585 * @tail_buf is a pointer to sub-buffer, corresponding to align-sized chunk
1586 * around tail, if tail exists.
1588 * @merge_reads is true for small requests,
1589 * if @buf_len == @head + bytes + @tail. In this case it is possible that both
1590 * head and tail exist but @buf_len == align and @tail_buf == @buf.
1592 typedef struct BdrvRequestPadding
{
1599 QEMUIOVector local_qiov
;
1600 } BdrvRequestPadding
;
1602 static bool bdrv_init_padding(BlockDriverState
*bs
,
1603 int64_t offset
, int64_t bytes
,
1604 BdrvRequestPadding
*pad
)
1606 int64_t align
= bs
->bl
.request_alignment
;
1609 bdrv_check_request(offset
, bytes
, &error_abort
);
1610 assert(align
<= INT_MAX
); /* documented in block/block_int.h */
1611 assert(align
<= SIZE_MAX
/ 2); /* so we can allocate the buffer */
1613 memset(pad
, 0, sizeof(*pad
));
1615 pad
->head
= offset
& (align
- 1);
1616 pad
->tail
= ((offset
+ bytes
) & (align
- 1));
1618 pad
->tail
= align
- pad
->tail
;
1621 if (!pad
->head
&& !pad
->tail
) {
1625 assert(bytes
); /* Nothing good in aligning zero-length requests */
1627 sum
= pad
->head
+ bytes
+ pad
->tail
;
1628 pad
->buf_len
= (sum
> align
&& pad
->head
&& pad
->tail
) ? 2 * align
: align
;
1629 pad
->buf
= qemu_blockalign(bs
, pad
->buf_len
);
1630 pad
->merge_reads
= sum
== pad
->buf_len
;
1632 pad
->tail_buf
= pad
->buf
+ pad
->buf_len
- align
;
1638 static int bdrv_padding_rmw_read(BdrvChild
*child
,
1639 BdrvTrackedRequest
*req
,
1640 BdrvRequestPadding
*pad
,
1643 QEMUIOVector local_qiov
;
1644 BlockDriverState
*bs
= child
->bs
;
1645 uint64_t align
= bs
->bl
.request_alignment
;
1648 assert(req
->serialising
&& pad
->buf
);
1650 if (pad
->head
|| pad
->merge_reads
) {
1651 int64_t bytes
= pad
->merge_reads
? pad
->buf_len
: align
;
1653 qemu_iovec_init_buf(&local_qiov
, pad
->buf
, bytes
);
1656 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1658 if (pad
->merge_reads
&& pad
->tail
) {
1659 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1661 ret
= bdrv_aligned_preadv(child
, req
, req
->overlap_offset
, bytes
,
1662 align
, &local_qiov
, 0, 0);
1667 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1669 if (pad
->merge_reads
&& pad
->tail
) {
1670 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1673 if (pad
->merge_reads
) {
1679 qemu_iovec_init_buf(&local_qiov
, pad
->tail_buf
, align
);
1681 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1682 ret
= bdrv_aligned_preadv(
1684 req
->overlap_offset
+ req
->overlap_bytes
- align
,
1685 align
, align
, &local_qiov
, 0, 0);
1689 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1694 memset(pad
->buf
+ pad
->head
, 0, pad
->buf_len
- pad
->head
- pad
->tail
);
1700 static void bdrv_padding_destroy(BdrvRequestPadding
*pad
)
1703 qemu_vfree(pad
->buf
);
1704 qemu_iovec_destroy(&pad
->local_qiov
);
1706 memset(pad
, 0, sizeof(*pad
));
1712 * Exchange request parameters with padded request if needed. Don't include RMW
1713 * read of padding, bdrv_padding_rmw_read() should be called separately if
1716 * Request parameters (@qiov, &qiov_offset, &offset, &bytes) are in-out:
1717 * - on function start they represent original request
1718 * - on failure or when padding is not needed they are unchanged
1719 * - on success when padding is needed they represent padded request
1721 static int bdrv_pad_request(BlockDriverState
*bs
,
1722 QEMUIOVector
**qiov
, size_t *qiov_offset
,
1723 int64_t *offset
, int64_t *bytes
,
1724 BdrvRequestPadding
*pad
, bool *padded
)
1728 bdrv_check_qiov_request(*offset
, *bytes
, *qiov
, *qiov_offset
, &error_abort
);
1730 if (!bdrv_init_padding(bs
, *offset
, *bytes
, pad
)) {
1737 ret
= qemu_iovec_init_extended(&pad
->local_qiov
, pad
->buf
, pad
->head
,
1738 *qiov
, *qiov_offset
, *bytes
,
1739 pad
->buf
+ pad
->buf_len
- pad
->tail
,
1742 bdrv_padding_destroy(pad
);
1745 *bytes
+= pad
->head
+ pad
->tail
;
1746 *offset
-= pad
->head
;
1747 *qiov
= &pad
->local_qiov
;
1756 int coroutine_fn
bdrv_co_preadv(BdrvChild
*child
,
1757 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
1758 BdrvRequestFlags flags
)
1761 return bdrv_co_preadv_part(child
, offset
, bytes
, qiov
, 0, flags
);
1764 int coroutine_fn
bdrv_co_preadv_part(BdrvChild
*child
,
1765 int64_t offset
, int64_t bytes
,
1766 QEMUIOVector
*qiov
, size_t qiov_offset
,
1767 BdrvRequestFlags flags
)
1769 BlockDriverState
*bs
= child
->bs
;
1770 BdrvTrackedRequest req
;
1771 BdrvRequestPadding pad
;
1775 trace_bdrv_co_preadv_part(bs
, offset
, bytes
, flags
);
1777 if (!bdrv_is_inserted(bs
)) {
1781 ret
= bdrv_check_request32(offset
, bytes
, qiov
, qiov_offset
);
1786 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
1788 * Aligning zero request is nonsense. Even if driver has special meaning
1789 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
1790 * it to driver due to request_alignment.
1792 * Still, no reason to return an error if someone do unaligned
1793 * zero-length read occasionally.
1798 bdrv_inc_in_flight(bs
);
1800 /* Don't do copy-on-read if we read data before write operation */
1801 if (qatomic_read(&bs
->copy_on_read
)) {
1802 flags
|= BDRV_REQ_COPY_ON_READ
;
1805 ret
= bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
,
1811 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_READ
);
1812 ret
= bdrv_aligned_preadv(child
, &req
, offset
, bytes
,
1813 bs
->bl
.request_alignment
,
1814 qiov
, qiov_offset
, flags
);
1815 tracked_request_end(&req
);
1816 bdrv_padding_destroy(&pad
);
1819 bdrv_dec_in_flight(bs
);
1824 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
1825 int64_t offset
, int64_t bytes
, BdrvRequestFlags flags
)
1827 BlockDriver
*drv
= bs
->drv
;
1831 bool need_flush
= false;
1835 int64_t max_write_zeroes
= MIN_NON_ZERO(bs
->bl
.max_pwrite_zeroes
,
1837 int alignment
= MAX(bs
->bl
.pwrite_zeroes_alignment
,
1838 bs
->bl
.request_alignment
);
1839 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
, MAX_BOUNCE_BUFFER
);
1841 bdrv_check_request(offset
, bytes
, &error_abort
);
1847 if ((flags
& ~bs
->supported_zero_flags
) & BDRV_REQ_NO_FALLBACK
) {
1851 /* Invalidate the cached block-status data range if this write overlaps */
1852 bdrv_bsc_invalidate_range(bs
, offset
, bytes
);
1854 assert(alignment
% bs
->bl
.request_alignment
== 0);
1855 head
= offset
% alignment
;
1856 tail
= (offset
+ bytes
) % alignment
;
1857 max_write_zeroes
= QEMU_ALIGN_DOWN(max_write_zeroes
, alignment
);
1858 assert(max_write_zeroes
>= bs
->bl
.request_alignment
);
1860 while (bytes
> 0 && !ret
) {
1861 int64_t num
= bytes
;
1863 /* Align request. Block drivers can expect the "bulk" of the request
1864 * to be aligned, and that unaligned requests do not cross cluster
1868 /* Make a small request up to the first aligned sector. For
1869 * convenience, limit this request to max_transfer even if
1870 * we don't need to fall back to writes. */
1871 num
= MIN(MIN(bytes
, max_transfer
), alignment
- head
);
1872 head
= (head
+ num
) % alignment
;
1873 assert(num
< max_write_zeroes
);
1874 } else if (tail
&& num
> alignment
) {
1875 /* Shorten the request to the last aligned sector. */
1879 /* limit request size */
1880 if (num
> max_write_zeroes
) {
1881 num
= max_write_zeroes
;
1885 /* First try the efficient write zeroes operation */
1886 if (drv
->bdrv_co_pwrite_zeroes
) {
1887 ret
= drv
->bdrv_co_pwrite_zeroes(bs
, offset
, num
,
1888 flags
& bs
->supported_zero_flags
);
1889 if (ret
!= -ENOTSUP
&& (flags
& BDRV_REQ_FUA
) &&
1890 !(bs
->supported_zero_flags
& BDRV_REQ_FUA
)) {
1894 assert(!bs
->supported_zero_flags
);
1897 if (ret
== -ENOTSUP
&& !(flags
& BDRV_REQ_NO_FALLBACK
)) {
1898 /* Fall back to bounce buffer if write zeroes is unsupported */
1899 BdrvRequestFlags write_flags
= flags
& ~BDRV_REQ_ZERO_WRITE
;
1901 if ((flags
& BDRV_REQ_FUA
) &&
1902 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1903 /* No need for bdrv_driver_pwrite() to do a fallback
1904 * flush on each chunk; use just one at the end */
1905 write_flags
&= ~BDRV_REQ_FUA
;
1908 num
= MIN(num
, max_transfer
);
1910 buf
= qemu_try_blockalign0(bs
, num
);
1916 qemu_iovec_init_buf(&qiov
, buf
, num
);
1918 ret
= bdrv_driver_pwritev(bs
, offset
, num
, &qiov
, 0, write_flags
);
1920 /* Keep bounce buffer around if it is big enough for all
1921 * all future requests.
1923 if (num
< max_transfer
) {
1934 if (ret
== 0 && need_flush
) {
1935 ret
= bdrv_co_flush(bs
);
1941 static inline int coroutine_fn
1942 bdrv_co_write_req_prepare(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1943 BdrvTrackedRequest
*req
, int flags
)
1945 BlockDriverState
*bs
= child
->bs
;
1947 bdrv_check_request(offset
, bytes
, &error_abort
);
1949 if (bdrv_is_read_only(bs
)) {
1953 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
1954 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1955 assert(!(flags
& ~BDRV_REQ_MASK
));
1956 assert(!((flags
& BDRV_REQ_NO_WAIT
) && !(flags
& BDRV_REQ_SERIALISING
)));
1958 if (flags
& BDRV_REQ_SERIALISING
) {
1959 QEMU_LOCK_GUARD(&bs
->reqs_lock
);
1961 tracked_request_set_serialising(req
, bdrv_get_cluster_size(bs
));
1963 if ((flags
& BDRV_REQ_NO_WAIT
) && bdrv_find_conflicting_request(req
)) {
1967 bdrv_wait_serialising_requests_locked(req
);
1969 bdrv_wait_serialising_requests(req
);
1972 assert(req
->overlap_offset
<= offset
);
1973 assert(offset
+ bytes
<= req
->overlap_offset
+ req
->overlap_bytes
);
1974 assert(offset
+ bytes
<= bs
->total_sectors
* BDRV_SECTOR_SIZE
||
1975 child
->perm
& BLK_PERM_RESIZE
);
1977 switch (req
->type
) {
1978 case BDRV_TRACKED_WRITE
:
1979 case BDRV_TRACKED_DISCARD
:
1980 if (flags
& BDRV_REQ_WRITE_UNCHANGED
) {
1981 assert(child
->perm
& (BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_WRITE
));
1983 assert(child
->perm
& BLK_PERM_WRITE
);
1985 bdrv_write_threshold_check_write(bs
, offset
, bytes
);
1987 case BDRV_TRACKED_TRUNCATE
:
1988 assert(child
->perm
& BLK_PERM_RESIZE
);
1995 static inline void coroutine_fn
1996 bdrv_co_write_req_finish(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1997 BdrvTrackedRequest
*req
, int ret
)
1999 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
2000 BlockDriverState
*bs
= child
->bs
;
2002 bdrv_check_request(offset
, bytes
, &error_abort
);
2004 qatomic_inc(&bs
->write_gen
);
2007 * Discard cannot extend the image, but in error handling cases, such as
2008 * when reverting a qcow2 cluster allocation, the discarded range can pass
2009 * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
2010 * here. Instead, just skip it, since semantically a discard request
2011 * beyond EOF cannot expand the image anyway.
2014 (req
->type
== BDRV_TRACKED_TRUNCATE
||
2015 end_sector
> bs
->total_sectors
) &&
2016 req
->type
!= BDRV_TRACKED_DISCARD
) {
2017 bs
->total_sectors
= end_sector
;
2018 bdrv_parent_cb_resize(bs
);
2019 bdrv_dirty_bitmap_truncate(bs
, end_sector
<< BDRV_SECTOR_BITS
);
2022 switch (req
->type
) {
2023 case BDRV_TRACKED_WRITE
:
2024 stat64_max(&bs
->wr_highest_offset
, offset
+ bytes
);
2025 /* fall through, to set dirty bits */
2026 case BDRV_TRACKED_DISCARD
:
2027 bdrv_set_dirty(bs
, offset
, bytes
);
2036 * Forwards an already correctly aligned write request to the BlockDriver,
2037 * after possibly fragmenting it.
2039 static int coroutine_fn
bdrv_aligned_pwritev(BdrvChild
*child
,
2040 BdrvTrackedRequest
*req
, int64_t offset
, int64_t bytes
,
2041 int64_t align
, QEMUIOVector
*qiov
, size_t qiov_offset
,
2042 BdrvRequestFlags flags
)
2044 BlockDriverState
*bs
= child
->bs
;
2045 BlockDriver
*drv
= bs
->drv
;
2048 int64_t bytes_remaining
= bytes
;
2051 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
2057 if (bdrv_has_readonly_bitmaps(bs
)) {
2061 assert(is_power_of_2(align
));
2062 assert((offset
& (align
- 1)) == 0);
2063 assert((bytes
& (align
- 1)) == 0);
2064 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
2067 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, req
, flags
);
2069 if (!ret
&& bs
->detect_zeroes
!= BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
&&
2070 !(flags
& BDRV_REQ_ZERO_WRITE
) && drv
->bdrv_co_pwrite_zeroes
&&
2071 qemu_iovec_is_zero(qiov
, qiov_offset
, bytes
)) {
2072 flags
|= BDRV_REQ_ZERO_WRITE
;
2073 if (bs
->detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
) {
2074 flags
|= BDRV_REQ_MAY_UNMAP
;
2079 /* Do nothing, write notifier decided to fail this request */
2080 } else if (flags
& BDRV_REQ_ZERO_WRITE
) {
2081 bdrv_debug_event(bs
, BLKDBG_PWRITEV_ZERO
);
2082 ret
= bdrv_co_do_pwrite_zeroes(bs
, offset
, bytes
, flags
);
2083 } else if (flags
& BDRV_REQ_WRITE_COMPRESSED
) {
2084 ret
= bdrv_driver_pwritev_compressed(bs
, offset
, bytes
,
2086 } else if (bytes
<= max_transfer
) {
2087 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
2088 ret
= bdrv_driver_pwritev(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
2090 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
2091 while (bytes_remaining
) {
2092 int num
= MIN(bytes_remaining
, max_transfer
);
2093 int local_flags
= flags
;
2096 if (num
< bytes_remaining
&& (flags
& BDRV_REQ_FUA
) &&
2097 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
2098 /* If FUA is going to be emulated by flush, we only
2099 * need to flush on the last iteration */
2100 local_flags
&= ~BDRV_REQ_FUA
;
2103 ret
= bdrv_driver_pwritev(bs
, offset
+ bytes
- bytes_remaining
,
2105 qiov_offset
+ bytes
- bytes_remaining
,
2110 bytes_remaining
-= num
;
2113 bdrv_debug_event(bs
, BLKDBG_PWRITEV_DONE
);
2118 bdrv_co_write_req_finish(child
, offset
, bytes
, req
, ret
);
2123 static int coroutine_fn
bdrv_co_do_zero_pwritev(BdrvChild
*child
,
2126 BdrvRequestFlags flags
,
2127 BdrvTrackedRequest
*req
)
2129 BlockDriverState
*bs
= child
->bs
;
2130 QEMUIOVector local_qiov
;
2131 uint64_t align
= bs
->bl
.request_alignment
;
2134 BdrvRequestPadding pad
;
2136 padding
= bdrv_init_padding(bs
, offset
, bytes
, &pad
);
2138 assert(!(flags
& BDRV_REQ_NO_WAIT
));
2139 bdrv_make_request_serialising(req
, align
);
2141 bdrv_padding_rmw_read(child
, req
, &pad
, true);
2143 if (pad
.head
|| pad
.merge_reads
) {
2144 int64_t aligned_offset
= offset
& ~(align
- 1);
2145 int64_t write_bytes
= pad
.merge_reads
? pad
.buf_len
: align
;
2147 qemu_iovec_init_buf(&local_qiov
, pad
.buf
, write_bytes
);
2148 ret
= bdrv_aligned_pwritev(child
, req
, aligned_offset
, write_bytes
,
2149 align
, &local_qiov
, 0,
2150 flags
& ~BDRV_REQ_ZERO_WRITE
);
2151 if (ret
< 0 || pad
.merge_reads
) {
2152 /* Error or all work is done */
2155 offset
+= write_bytes
- pad
.head
;
2156 bytes
-= write_bytes
- pad
.head
;
2160 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2161 if (bytes
>= align
) {
2162 /* Write the aligned part in the middle. */
2163 int64_t aligned_bytes
= bytes
& ~(align
- 1);
2164 ret
= bdrv_aligned_pwritev(child
, req
, offset
, aligned_bytes
, align
,
2169 bytes
-= aligned_bytes
;
2170 offset
+= aligned_bytes
;
2173 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2175 assert(align
== pad
.tail
+ bytes
);
2177 qemu_iovec_init_buf(&local_qiov
, pad
.tail_buf
, align
);
2178 ret
= bdrv_aligned_pwritev(child
, req
, offset
, align
, align
,
2180 flags
& ~BDRV_REQ_ZERO_WRITE
);
2184 bdrv_padding_destroy(&pad
);
2190 * Handle a write request in coroutine context
2192 int coroutine_fn
bdrv_co_pwritev(BdrvChild
*child
,
2193 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
2194 BdrvRequestFlags flags
)
2197 return bdrv_co_pwritev_part(child
, offset
, bytes
, qiov
, 0, flags
);
2200 int coroutine_fn
bdrv_co_pwritev_part(BdrvChild
*child
,
2201 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
, size_t qiov_offset
,
2202 BdrvRequestFlags flags
)
2204 BlockDriverState
*bs
= child
->bs
;
2205 BdrvTrackedRequest req
;
2206 uint64_t align
= bs
->bl
.request_alignment
;
2207 BdrvRequestPadding pad
;
2209 bool padded
= false;
2212 trace_bdrv_co_pwritev_part(child
->bs
, offset
, bytes
, flags
);
2214 if (!bdrv_is_inserted(bs
)) {
2218 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2219 ret
= bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, NULL
);
2221 ret
= bdrv_check_request32(offset
, bytes
, qiov
, qiov_offset
);
2227 /* If the request is misaligned then we can't make it efficient */
2228 if ((flags
& BDRV_REQ_NO_FALLBACK
) &&
2229 !QEMU_IS_ALIGNED(offset
| bytes
, align
))
2234 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
2236 * Aligning zero request is nonsense. Even if driver has special meaning
2237 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
2238 * it to driver due to request_alignment.
2240 * Still, no reason to return an error if someone do unaligned
2241 * zero-length write occasionally.
2246 if (!(flags
& BDRV_REQ_ZERO_WRITE
)) {
2248 * Pad request for following read-modify-write cycle.
2249 * bdrv_co_do_zero_pwritev() does aligning by itself, so, we do
2250 * alignment only if there is no ZERO flag.
2252 ret
= bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
,
2259 bdrv_inc_in_flight(bs
);
2260 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_WRITE
);
2262 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2264 ret
= bdrv_co_do_zero_pwritev(child
, offset
, bytes
, flags
, &req
);
2270 * Request was unaligned to request_alignment and therefore
2271 * padded. We are going to do read-modify-write, and must
2272 * serialize the request to prevent interactions of the
2273 * widened region with other transactions.
2275 assert(!(flags
& BDRV_REQ_NO_WAIT
));
2276 bdrv_make_request_serialising(&req
, align
);
2277 bdrv_padding_rmw_read(child
, &req
, &pad
, false);
2280 ret
= bdrv_aligned_pwritev(child
, &req
, offset
, bytes
, align
,
2281 qiov
, qiov_offset
, flags
);
2283 bdrv_padding_destroy(&pad
);
2286 tracked_request_end(&req
);
2287 bdrv_dec_in_flight(bs
);
2292 int coroutine_fn
bdrv_co_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
2293 int64_t bytes
, BdrvRequestFlags flags
)
2296 trace_bdrv_co_pwrite_zeroes(child
->bs
, offset
, bytes
, flags
);
2298 if (!(child
->bs
->open_flags
& BDRV_O_UNMAP
)) {
2299 flags
&= ~BDRV_REQ_MAY_UNMAP
;
2302 return bdrv_co_pwritev(child
, offset
, bytes
, NULL
,
2303 BDRV_REQ_ZERO_WRITE
| flags
);
2307 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
2309 int bdrv_flush_all(void)
2311 BdrvNextIterator it
;
2312 BlockDriverState
*bs
= NULL
;
2315 GLOBAL_STATE_CODE();
2318 * bdrv queue is managed by record/replay,
2319 * creating new flush request for stopping
2320 * the VM may break the determinism
2322 if (replay_events_enabled()) {
2326 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2327 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
2330 aio_context_acquire(aio_context
);
2331 ret
= bdrv_flush(bs
);
2332 if (ret
< 0 && !result
) {
2335 aio_context_release(aio_context
);
2342 * Returns the allocation status of the specified sectors.
2343 * Drivers not implementing the functionality are assumed to not support
2344 * backing files, hence all their sectors are reported as allocated.
2346 * If 'want_zero' is true, the caller is querying for mapping
2347 * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and
2348 * _ZERO where possible; otherwise, the result favors larger 'pnum',
2349 * with a focus on accurate BDRV_BLOCK_ALLOCATED.
2351 * If 'offset' is beyond the end of the disk image the return value is
2352 * BDRV_BLOCK_EOF and 'pnum' is set to 0.
2354 * 'bytes' is the max value 'pnum' should be set to. If bytes goes
2355 * beyond the end of the disk image it will be clamped; if 'pnum' is set to
2356 * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
2358 * 'pnum' is set to the number of bytes (including and immediately
2359 * following the specified offset) that are easily known to be in the
2360 * same allocated/unallocated state. Note that a second call starting
2361 * at the original offset plus returned pnum may have the same status.
2362 * The returned value is non-zero on success except at end-of-file.
2364 * Returns negative errno on failure. Otherwise, if the
2365 * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are
2366 * set to the host mapping and BDS corresponding to the guest offset.
2368 static int coroutine_fn
bdrv_co_block_status(BlockDriverState
*bs
,
2370 int64_t offset
, int64_t bytes
,
2371 int64_t *pnum
, int64_t *map
,
2372 BlockDriverState
**file
)
2375 int64_t n
; /* bytes */
2377 int64_t local_map
= 0;
2378 BlockDriverState
*local_file
= NULL
;
2379 int64_t aligned_offset
, aligned_bytes
;
2381 bool has_filtered_child
;
2385 total_size
= bdrv_getlength(bs
);
2386 if (total_size
< 0) {
2391 if (offset
>= total_size
) {
2392 ret
= BDRV_BLOCK_EOF
;
2400 n
= total_size
- offset
;
2405 /* Must be non-NULL or bdrv_getlength() would have failed */
2407 has_filtered_child
= bdrv_filter_child(bs
);
2408 if (!bs
->drv
->bdrv_co_block_status
&& !has_filtered_child
) {
2410 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
;
2411 if (offset
+ bytes
== total_size
) {
2412 ret
|= BDRV_BLOCK_EOF
;
2414 if (bs
->drv
->protocol_name
) {
2415 ret
|= BDRV_BLOCK_OFFSET_VALID
;
2422 bdrv_inc_in_flight(bs
);
2424 /* Round out to request_alignment boundaries */
2425 align
= bs
->bl
.request_alignment
;
2426 aligned_offset
= QEMU_ALIGN_DOWN(offset
, align
);
2427 aligned_bytes
= ROUND_UP(offset
+ bytes
, align
) - aligned_offset
;
2429 if (bs
->drv
->bdrv_co_block_status
) {
2431 * Use the block-status cache only for protocol nodes: Format
2432 * drivers are generally quick to inquire the status, but protocol
2433 * drivers often need to get information from outside of qemu, so
2434 * we do not have control over the actual implementation. There
2435 * have been cases where inquiring the status took an unreasonably
2436 * long time, and we can do nothing in qemu to fix it.
2437 * This is especially problematic for images with large data areas,
2438 * because finding the few holes in them and giving them special
2439 * treatment does not gain much performance. Therefore, we try to
2440 * cache the last-identified data region.
2442 * Second, limiting ourselves to protocol nodes allows us to assume
2443 * the block status for data regions to be DATA | OFFSET_VALID, and
2444 * that the host offset is the same as the guest offset.
2446 * Note that it is possible that external writers zero parts of
2447 * the cached regions without the cache being invalidated, and so
2448 * we may report zeroes as data. This is not catastrophic,
2449 * however, because reporting zeroes as data is fine.
2451 if (QLIST_EMPTY(&bs
->children
) &&
2452 bdrv_bsc_is_data(bs
, aligned_offset
, pnum
))
2454 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
2456 local_map
= aligned_offset
;
2458 ret
= bs
->drv
->bdrv_co_block_status(bs
, want_zero
, aligned_offset
,
2459 aligned_bytes
, pnum
, &local_map
,
2463 * Note that checking QLIST_EMPTY(&bs->children) is also done when
2464 * the cache is queried above. Technically, we do not need to check
2465 * it here; the worst that can happen is that we fill the cache for
2466 * non-protocol nodes, and then it is never used. However, filling
2467 * the cache requires an RCU update, so double check here to avoid
2468 * such an update if possible.
2470 * Check want_zero, because we only want to update the cache when we
2471 * have accurate information about what is zero and what is data.
2474 ret
== (BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
) &&
2475 QLIST_EMPTY(&bs
->children
))
2478 * When a protocol driver reports BLOCK_OFFSET_VALID, the
2479 * returned local_map value must be the same as the offset we
2480 * have passed (aligned_offset), and local_bs must be the node
2482 * Assert this, because we follow this rule when reading from
2483 * the cache (see the `local_file = bs` and
2484 * `local_map = aligned_offset` assignments above), and the
2485 * result the cache delivers must be the same as the driver
2488 assert(local_file
== bs
);
2489 assert(local_map
== aligned_offset
);
2490 bdrv_bsc_fill(bs
, aligned_offset
, *pnum
);
2494 /* Default code for filters */
2496 local_file
= bdrv_filter_bs(bs
);
2499 *pnum
= aligned_bytes
;
2500 local_map
= aligned_offset
;
2501 ret
= BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
;
2509 * The driver's result must be a non-zero multiple of request_alignment.
2510 * Clamp pnum and adjust map to original request.
2512 assert(*pnum
&& QEMU_IS_ALIGNED(*pnum
, align
) &&
2513 align
> offset
- aligned_offset
);
2514 if (ret
& BDRV_BLOCK_RECURSE
) {
2515 assert(ret
& BDRV_BLOCK_DATA
);
2516 assert(ret
& BDRV_BLOCK_OFFSET_VALID
);
2517 assert(!(ret
& BDRV_BLOCK_ZERO
));
2520 *pnum
-= offset
- aligned_offset
;
2521 if (*pnum
> bytes
) {
2524 if (ret
& BDRV_BLOCK_OFFSET_VALID
) {
2525 local_map
+= offset
- aligned_offset
;
2528 if (ret
& BDRV_BLOCK_RAW
) {
2529 assert(ret
& BDRV_BLOCK_OFFSET_VALID
&& local_file
);
2530 ret
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2531 *pnum
, pnum
, &local_map
, &local_file
);
2535 if (ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ZERO
)) {
2536 ret
|= BDRV_BLOCK_ALLOCATED
;
2537 } else if (bs
->drv
->supports_backing
) {
2538 BlockDriverState
*cow_bs
= bdrv_cow_bs(bs
);
2541 ret
|= BDRV_BLOCK_ZERO
;
2542 } else if (want_zero
) {
2543 int64_t size2
= bdrv_getlength(cow_bs
);
2545 if (size2
>= 0 && offset
>= size2
) {
2546 ret
|= BDRV_BLOCK_ZERO
;
2551 if (want_zero
&& ret
& BDRV_BLOCK_RECURSE
&&
2552 local_file
&& local_file
!= bs
&&
2553 (ret
& BDRV_BLOCK_DATA
) && !(ret
& BDRV_BLOCK_ZERO
) &&
2554 (ret
& BDRV_BLOCK_OFFSET_VALID
)) {
2558 ret2
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2559 *pnum
, &file_pnum
, NULL
, NULL
);
2561 /* Ignore errors. This is just providing extra information, it
2562 * is useful but not necessary.
2564 if (ret2
& BDRV_BLOCK_EOF
&&
2565 (!file_pnum
|| ret2
& BDRV_BLOCK_ZERO
)) {
2567 * It is valid for the format block driver to read
2568 * beyond the end of the underlying file's current
2569 * size; such areas read as zero.
2571 ret
|= BDRV_BLOCK_ZERO
;
2573 /* Limit request to the range reported by the protocol driver */
2575 ret
|= (ret2
& BDRV_BLOCK_ZERO
);
2581 bdrv_dec_in_flight(bs
);
2582 if (ret
>= 0 && offset
+ *pnum
== total_size
) {
2583 ret
|= BDRV_BLOCK_EOF
;
2596 bdrv_co_common_block_status_above(BlockDriverState
*bs
,
2597 BlockDriverState
*base
,
2604 BlockDriverState
**file
,
2608 BlockDriverState
*p
;
2613 assert(!include_base
|| base
); /* Can't include NULL base */
2620 if (!include_base
&& bs
== base
) {
2625 ret
= bdrv_co_block_status(bs
, want_zero
, offset
, bytes
, pnum
, map
, file
);
2627 if (ret
< 0 || *pnum
== 0 || ret
& BDRV_BLOCK_ALLOCATED
|| bs
== base
) {
2631 if (ret
& BDRV_BLOCK_EOF
) {
2632 eof
= offset
+ *pnum
;
2635 assert(*pnum
<= bytes
);
2638 for (p
= bdrv_filter_or_cow_bs(bs
); include_base
|| p
!= base
;
2639 p
= bdrv_filter_or_cow_bs(p
))
2641 ret
= bdrv_co_block_status(p
, want_zero
, offset
, bytes
, pnum
, map
,
2649 * The top layer deferred to this layer, and because this layer is
2650 * short, any zeroes that we synthesize beyond EOF behave as if they
2651 * were allocated at this layer.
2653 * We don't include BDRV_BLOCK_EOF into ret, as upper layer may be
2654 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2657 assert(ret
& BDRV_BLOCK_EOF
);
2662 ret
= BDRV_BLOCK_ZERO
| BDRV_BLOCK_ALLOCATED
;
2665 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2667 * We've found the node and the status, we must break.
2669 * Drop BDRV_BLOCK_EOF, as it's not for upper layer, which may be
2670 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2673 ret
&= ~BDRV_BLOCK_EOF
;
2678 assert(include_base
);
2683 * OK, [offset, offset + *pnum) region is unallocated on this layer,
2684 * let's continue the diving.
2686 assert(*pnum
<= bytes
);
2690 if (offset
+ *pnum
== eof
) {
2691 ret
|= BDRV_BLOCK_EOF
;
2697 int bdrv_block_status_above(BlockDriverState
*bs
, BlockDriverState
*base
,
2698 int64_t offset
, int64_t bytes
, int64_t *pnum
,
2699 int64_t *map
, BlockDriverState
**file
)
2702 return bdrv_common_block_status_above(bs
, base
, false, true, offset
, bytes
,
2703 pnum
, map
, file
, NULL
);
2706 int bdrv_block_status(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2707 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
2710 return bdrv_block_status_above(bs
, bdrv_filter_or_cow_bs(bs
),
2711 offset
, bytes
, pnum
, map
, file
);
2715 * Check @bs (and its backing chain) to see if the range defined
2716 * by @offset and @bytes is known to read as zeroes.
2717 * Return 1 if that is the case, 0 otherwise and -errno on error.
2718 * This test is meant to be fast rather than accurate so returning 0
2719 * does not guarantee non-zero data.
2721 int coroutine_fn
bdrv_co_is_zero_fast(BlockDriverState
*bs
, int64_t offset
,
2725 int64_t pnum
= bytes
;
2732 ret
= bdrv_common_block_status_above(bs
, NULL
, false, false, offset
,
2733 bytes
, &pnum
, NULL
, NULL
, NULL
);
2739 return (pnum
== bytes
) && (ret
& BDRV_BLOCK_ZERO
);
2742 int coroutine_fn
bdrv_is_allocated(BlockDriverState
*bs
, int64_t offset
,
2743 int64_t bytes
, int64_t *pnum
)
2749 ret
= bdrv_common_block_status_above(bs
, bs
, true, false, offset
,
2750 bytes
, pnum
? pnum
: &dummy
, NULL
,
2755 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
2759 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2761 * Return a positive depth if (a prefix of) the given range is allocated
2762 * in any image between BASE and TOP (BASE is only included if include_base
2763 * is set). Depth 1 is TOP, 2 is the first backing layer, and so forth.
2764 * BASE can be NULL to check if the given offset is allocated in any
2765 * image of the chain. Return 0 otherwise, or negative errno on
2768 * 'pnum' is set to the number of bytes (including and immediately
2769 * following the specified offset) that are known to be in the same
2770 * allocated/unallocated state. Note that a subsequent call starting
2771 * at 'offset + *pnum' may return the same allocation status (in other
2772 * words, the result is not necessarily the maximum possible range);
2773 * but 'pnum' will only be 0 when end of file is reached.
2775 int bdrv_is_allocated_above(BlockDriverState
*top
,
2776 BlockDriverState
*base
,
2777 bool include_base
, int64_t offset
,
2778 int64_t bytes
, int64_t *pnum
)
2781 int ret
= bdrv_common_block_status_above(top
, base
, include_base
, false,
2782 offset
, bytes
, pnum
, NULL
, NULL
,
2789 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2796 bdrv_co_readv_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2798 BlockDriver
*drv
= bs
->drv
;
2799 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2803 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
2812 bdrv_inc_in_flight(bs
);
2814 if (drv
->bdrv_load_vmstate
) {
2815 ret
= drv
->bdrv_load_vmstate(bs
, qiov
, pos
);
2816 } else if (child_bs
) {
2817 ret
= bdrv_co_readv_vmstate(child_bs
, qiov
, pos
);
2822 bdrv_dec_in_flight(bs
);
2828 bdrv_co_writev_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2830 BlockDriver
*drv
= bs
->drv
;
2831 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2835 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
2844 bdrv_inc_in_flight(bs
);
2846 if (drv
->bdrv_save_vmstate
) {
2847 ret
= drv
->bdrv_save_vmstate(bs
, qiov
, pos
);
2848 } else if (child_bs
) {
2849 ret
= bdrv_co_writev_vmstate(child_bs
, qiov
, pos
);
2854 bdrv_dec_in_flight(bs
);
2859 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2860 int64_t pos
, int size
)
2862 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2863 int ret
= bdrv_writev_vmstate(bs
, &qiov
, pos
);
2866 return ret
< 0 ? ret
: size
;
2869 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2870 int64_t pos
, int size
)
2872 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2873 int ret
= bdrv_readv_vmstate(bs
, &qiov
, pos
);
2876 return ret
< 0 ? ret
: size
;
2879 /**************************************************************/
2882 void bdrv_aio_cancel(BlockAIOCB
*acb
)
2886 bdrv_aio_cancel_async(acb
);
2887 while (acb
->refcnt
> 1) {
2888 if (acb
->aiocb_info
->get_aio_context
) {
2889 aio_poll(acb
->aiocb_info
->get_aio_context(acb
), true);
2890 } else if (acb
->bs
) {
2891 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2892 * assert that we're not using an I/O thread. Thread-safe
2893 * code should use bdrv_aio_cancel_async exclusively.
2895 assert(bdrv_get_aio_context(acb
->bs
) == qemu_get_aio_context());
2896 aio_poll(bdrv_get_aio_context(acb
->bs
), true);
2901 qemu_aio_unref(acb
);
2904 /* Async version of aio cancel. The caller is not blocked if the acb implements
2905 * cancel_async, otherwise we do nothing and let the request normally complete.
2906 * In either case the completion callback must be called. */
2907 void bdrv_aio_cancel_async(BlockAIOCB
*acb
)
2910 if (acb
->aiocb_info
->cancel_async
) {
2911 acb
->aiocb_info
->cancel_async(acb
);
2915 /**************************************************************/
2916 /* Coroutine block device emulation */
2918 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2920 BdrvChild
*primary_child
= bdrv_primary_child(bs
);
2926 bdrv_inc_in_flight(bs
);
2928 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
) ||
2933 qemu_co_mutex_lock(&bs
->reqs_lock
);
2934 current_gen
= qatomic_read(&bs
->write_gen
);
2936 /* Wait until any previous flushes are completed */
2937 while (bs
->active_flush_req
) {
2938 qemu_co_queue_wait(&bs
->flush_queue
, &bs
->reqs_lock
);
2941 /* Flushes reach this point in nondecreasing current_gen order. */
2942 bs
->active_flush_req
= true;
2943 qemu_co_mutex_unlock(&bs
->reqs_lock
);
2945 /* Write back all layers by calling one driver function */
2946 if (bs
->drv
->bdrv_co_flush
) {
2947 ret
= bs
->drv
->bdrv_co_flush(bs
);
2951 /* Write back cached data to the OS even with cache=unsafe */
2952 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_OS
);
2953 if (bs
->drv
->bdrv_co_flush_to_os
) {
2954 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
2960 /* But don't actually force it to the disk with cache=unsafe */
2961 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2962 goto flush_children
;
2965 /* Check if we really need to flush anything */
2966 if (bs
->flushed_gen
== current_gen
) {
2967 goto flush_children
;
2970 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_DISK
);
2972 /* bs->drv->bdrv_co_flush() might have ejected the BDS
2973 * (even in case of apparent success) */
2977 if (bs
->drv
->bdrv_co_flush_to_disk
) {
2978 ret
= bs
->drv
->bdrv_co_flush_to_disk(bs
);
2979 } else if (bs
->drv
->bdrv_aio_flush
) {
2981 CoroutineIOCompletion co
= {
2982 .coroutine
= qemu_coroutine_self(),
2985 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
2989 qemu_coroutine_yield();
2994 * Some block drivers always operate in either writethrough or unsafe
2995 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2996 * know how the server works (because the behaviour is hardcoded or
2997 * depends on server-side configuration), so we can't ensure that
2998 * everything is safe on disk. Returning an error doesn't work because
2999 * that would break guests even if the server operates in writethrough
3002 * Let's hope the user knows what he's doing.
3011 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
3012 * in the case of cache=unsafe, so there are no useless flushes.
3016 QLIST_FOREACH(child
, &bs
->children
, next
) {
3017 if (child
->perm
& (BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
)) {
3018 int this_child_ret
= bdrv_co_flush(child
->bs
);
3020 ret
= this_child_ret
;
3026 /* Notify any pending flushes that we have completed */
3028 bs
->flushed_gen
= current_gen
;
3031 qemu_co_mutex_lock(&bs
->reqs_lock
);
3032 bs
->active_flush_req
= false;
3033 /* Return value is ignored - it's ok if wait queue is empty */
3034 qemu_co_queue_next(&bs
->flush_queue
);
3035 qemu_co_mutex_unlock(&bs
->reqs_lock
);
3038 bdrv_dec_in_flight(bs
);
3042 int coroutine_fn
bdrv_co_pdiscard(BdrvChild
*child
, int64_t offset
,
3045 BdrvTrackedRequest req
;
3047 int64_t max_pdiscard
;
3048 int head
, tail
, align
;
3049 BlockDriverState
*bs
= child
->bs
;
3052 if (!bs
|| !bs
->drv
|| !bdrv_is_inserted(bs
)) {
3056 if (bdrv_has_readonly_bitmaps(bs
)) {
3060 ret
= bdrv_check_request(offset
, bytes
, NULL
);
3065 /* Do nothing if disabled. */
3066 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
3070 if (!bs
->drv
->bdrv_co_pdiscard
&& !bs
->drv
->bdrv_aio_pdiscard
) {
3074 /* Invalidate the cached block-status data range if this discard overlaps */
3075 bdrv_bsc_invalidate_range(bs
, offset
, bytes
);
3077 /* Discard is advisory, but some devices track and coalesce
3078 * unaligned requests, so we must pass everything down rather than
3079 * round here. Still, most devices will just silently ignore
3080 * unaligned requests (by returning -ENOTSUP), so we must fragment
3081 * the request accordingly. */
3082 align
= MAX(bs
->bl
.pdiscard_alignment
, bs
->bl
.request_alignment
);
3083 assert(align
% bs
->bl
.request_alignment
== 0);
3084 head
= offset
% align
;
3085 tail
= (offset
+ bytes
) % align
;
3087 bdrv_inc_in_flight(bs
);
3088 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_DISCARD
);
3090 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, &req
, 0);
3095 max_pdiscard
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_pdiscard
, INT64_MAX
),
3097 assert(max_pdiscard
>= bs
->bl
.request_alignment
);
3100 int64_t num
= bytes
;
3103 /* Make small requests to get to alignment boundaries. */
3104 num
= MIN(bytes
, align
- head
);
3105 if (!QEMU_IS_ALIGNED(num
, bs
->bl
.request_alignment
)) {
3106 num
%= bs
->bl
.request_alignment
;
3108 head
= (head
+ num
) % align
;
3109 assert(num
< max_pdiscard
);
3112 /* Shorten the request to the last aligned cluster. */
3114 } else if (!QEMU_IS_ALIGNED(tail
, bs
->bl
.request_alignment
) &&
3115 tail
> bs
->bl
.request_alignment
) {
3116 tail
%= bs
->bl
.request_alignment
;
3120 /* limit request size */
3121 if (num
> max_pdiscard
) {
3129 if (bs
->drv
->bdrv_co_pdiscard
) {
3130 ret
= bs
->drv
->bdrv_co_pdiscard(bs
, offset
, num
);
3133 CoroutineIOCompletion co
= {
3134 .coroutine
= qemu_coroutine_self(),
3137 acb
= bs
->drv
->bdrv_aio_pdiscard(bs
, offset
, num
,
3138 bdrv_co_io_em_complete
, &co
);
3143 qemu_coroutine_yield();
3147 if (ret
&& ret
!= -ENOTSUP
) {
3156 bdrv_co_write_req_finish(child
, req
.offset
, req
.bytes
, &req
, ret
);
3157 tracked_request_end(&req
);
3158 bdrv_dec_in_flight(bs
);
3162 int bdrv_co_ioctl(BlockDriverState
*bs
, int req
, void *buf
)
3164 BlockDriver
*drv
= bs
->drv
;
3165 CoroutineIOCompletion co
= {
3166 .coroutine
= qemu_coroutine_self(),
3171 bdrv_inc_in_flight(bs
);
3172 if (!drv
|| (!drv
->bdrv_aio_ioctl
&& !drv
->bdrv_co_ioctl
)) {
3177 if (drv
->bdrv_co_ioctl
) {
3178 co
.ret
= drv
->bdrv_co_ioctl(bs
, req
, buf
);
3180 acb
= drv
->bdrv_aio_ioctl(bs
, req
, buf
, bdrv_co_io_em_complete
, &co
);
3185 qemu_coroutine_yield();
3188 bdrv_dec_in_flight(bs
);
3192 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
3195 return qemu_memalign(bdrv_opt_mem_align(bs
), size
);
3198 void *qemu_blockalign0(BlockDriverState
*bs
, size_t size
)
3201 return memset(qemu_blockalign(bs
, size
), 0, size
);
3204 void *qemu_try_blockalign(BlockDriverState
*bs
, size_t size
)
3206 size_t align
= bdrv_opt_mem_align(bs
);
3209 /* Ensure that NULL is never returned on success */
3215 return qemu_try_memalign(align
, size
);
3218 void *qemu_try_blockalign0(BlockDriverState
*bs
, size_t size
)
3220 void *mem
= qemu_try_blockalign(bs
, size
);
3224 memset(mem
, 0, size
);
3230 void bdrv_io_plug(BlockDriverState
*bs
)
3235 QLIST_FOREACH(child
, &bs
->children
, next
) {
3236 bdrv_io_plug(child
->bs
);
3239 if (qatomic_fetch_inc(&bs
->io_plugged
) == 0) {
3240 BlockDriver
*drv
= bs
->drv
;
3241 if (drv
&& drv
->bdrv_io_plug
) {
3242 drv
->bdrv_io_plug(bs
);
3247 void bdrv_io_unplug(BlockDriverState
*bs
)
3252 assert(bs
->io_plugged
);
3253 if (qatomic_fetch_dec(&bs
->io_plugged
) == 1) {
3254 BlockDriver
*drv
= bs
->drv
;
3255 if (drv
&& drv
->bdrv_io_unplug
) {
3256 drv
->bdrv_io_unplug(bs
);
3260 QLIST_FOREACH(child
, &bs
->children
, next
) {
3261 bdrv_io_unplug(child
->bs
);
3265 void bdrv_register_buf(BlockDriverState
*bs
, void *host
, size_t size
)
3269 GLOBAL_STATE_CODE();
3270 if (bs
->drv
&& bs
->drv
->bdrv_register_buf
) {
3271 bs
->drv
->bdrv_register_buf(bs
, host
, size
);
3273 QLIST_FOREACH(child
, &bs
->children
, next
) {
3274 bdrv_register_buf(child
->bs
, host
, size
);
3278 void bdrv_unregister_buf(BlockDriverState
*bs
, void *host
)
3282 GLOBAL_STATE_CODE();
3283 if (bs
->drv
&& bs
->drv
->bdrv_unregister_buf
) {
3284 bs
->drv
->bdrv_unregister_buf(bs
, host
);
3286 QLIST_FOREACH(child
, &bs
->children
, next
) {
3287 bdrv_unregister_buf(child
->bs
, host
);
3291 static int coroutine_fn
bdrv_co_copy_range_internal(
3292 BdrvChild
*src
, int64_t src_offset
, BdrvChild
*dst
,
3293 int64_t dst_offset
, int64_t bytes
,
3294 BdrvRequestFlags read_flags
, BdrvRequestFlags write_flags
,
3297 BdrvTrackedRequest req
;
3300 /* TODO We can support BDRV_REQ_NO_FALLBACK here */
3301 assert(!(read_flags
& BDRV_REQ_NO_FALLBACK
));
3302 assert(!(write_flags
& BDRV_REQ_NO_FALLBACK
));
3303 assert(!(read_flags
& BDRV_REQ_NO_WAIT
));
3304 assert(!(write_flags
& BDRV_REQ_NO_WAIT
));
3306 if (!dst
|| !dst
->bs
|| !bdrv_is_inserted(dst
->bs
)) {
3309 ret
= bdrv_check_request32(dst_offset
, bytes
, NULL
, 0);
3313 if (write_flags
& BDRV_REQ_ZERO_WRITE
) {
3314 return bdrv_co_pwrite_zeroes(dst
, dst_offset
, bytes
, write_flags
);
3317 if (!src
|| !src
->bs
|| !bdrv_is_inserted(src
->bs
)) {
3320 ret
= bdrv_check_request32(src_offset
, bytes
, NULL
, 0);
3325 if (!src
->bs
->drv
->bdrv_co_copy_range_from
3326 || !dst
->bs
->drv
->bdrv_co_copy_range_to
3327 || src
->bs
->encrypted
|| dst
->bs
->encrypted
) {
3332 bdrv_inc_in_flight(src
->bs
);
3333 tracked_request_begin(&req
, src
->bs
, src_offset
, bytes
,
3336 /* BDRV_REQ_SERIALISING is only for write operation */
3337 assert(!(read_flags
& BDRV_REQ_SERIALISING
));
3338 bdrv_wait_serialising_requests(&req
);
3340 ret
= src
->bs
->drv
->bdrv_co_copy_range_from(src
->bs
,
3344 read_flags
, write_flags
);
3346 tracked_request_end(&req
);
3347 bdrv_dec_in_flight(src
->bs
);
3349 bdrv_inc_in_flight(dst
->bs
);
3350 tracked_request_begin(&req
, dst
->bs
, dst_offset
, bytes
,
3351 BDRV_TRACKED_WRITE
);
3352 ret
= bdrv_co_write_req_prepare(dst
, dst_offset
, bytes
, &req
,
3355 ret
= dst
->bs
->drv
->bdrv_co_copy_range_to(dst
->bs
,
3359 read_flags
, write_flags
);
3361 bdrv_co_write_req_finish(dst
, dst_offset
, bytes
, &req
, ret
);
3362 tracked_request_end(&req
);
3363 bdrv_dec_in_flight(dst
->bs
);
3369 /* Copy range from @src to @dst.
3371 * See the comment of bdrv_co_copy_range for the parameter and return value
3373 int coroutine_fn
bdrv_co_copy_range_from(BdrvChild
*src
, int64_t src_offset
,
3374 BdrvChild
*dst
, int64_t dst_offset
,
3376 BdrvRequestFlags read_flags
,
3377 BdrvRequestFlags write_flags
)
3380 trace_bdrv_co_copy_range_from(src
, src_offset
, dst
, dst_offset
, bytes
,
3381 read_flags
, write_flags
);
3382 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3383 bytes
, read_flags
, write_flags
, true);
3386 /* Copy range from @src to @dst.
3388 * See the comment of bdrv_co_copy_range for the parameter and return value
3390 int coroutine_fn
bdrv_co_copy_range_to(BdrvChild
*src
, int64_t src_offset
,
3391 BdrvChild
*dst
, int64_t dst_offset
,
3393 BdrvRequestFlags read_flags
,
3394 BdrvRequestFlags write_flags
)
3397 trace_bdrv_co_copy_range_to(src
, src_offset
, dst
, dst_offset
, bytes
,
3398 read_flags
, write_flags
);
3399 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3400 bytes
, read_flags
, write_flags
, false);
3403 int coroutine_fn
bdrv_co_copy_range(BdrvChild
*src
, int64_t src_offset
,
3404 BdrvChild
*dst
, int64_t dst_offset
,
3405 int64_t bytes
, BdrvRequestFlags read_flags
,
3406 BdrvRequestFlags write_flags
)
3409 return bdrv_co_copy_range_from(src
, src_offset
,
3411 bytes
, read_flags
, write_flags
);
3414 static void bdrv_parent_cb_resize(BlockDriverState
*bs
)
3417 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
3418 if (c
->klass
->resize
) {
3419 c
->klass
->resize(c
);
3425 * Truncate file to 'offset' bytes (needed only for file protocols)
3427 * If 'exact' is true, the file must be resized to exactly the given
3428 * 'offset'. Otherwise, it is sufficient for the node to be at least
3429 * 'offset' bytes in length.
3431 int coroutine_fn
bdrv_co_truncate(BdrvChild
*child
, int64_t offset
, bool exact
,
3432 PreallocMode prealloc
, BdrvRequestFlags flags
,
3435 BlockDriverState
*bs
= child
->bs
;
3436 BdrvChild
*filtered
, *backing
;
3437 BlockDriver
*drv
= bs
->drv
;
3438 BdrvTrackedRequest req
;
3439 int64_t old_size
, new_bytes
;
3443 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3445 error_setg(errp
, "No medium inserted");
3449 error_setg(errp
, "Image size cannot be negative");
3453 ret
= bdrv_check_request(offset
, 0, errp
);
3458 old_size
= bdrv_getlength(bs
);
3460 error_setg_errno(errp
, -old_size
, "Failed to get old image size");
3464 if (bdrv_is_read_only(bs
)) {
3465 error_setg(errp
, "Image is read-only");
3469 if (offset
> old_size
) {
3470 new_bytes
= offset
- old_size
;
3475 bdrv_inc_in_flight(bs
);
3476 tracked_request_begin(&req
, bs
, offset
- new_bytes
, new_bytes
,
3477 BDRV_TRACKED_TRUNCATE
);
3479 /* If we are growing the image and potentially using preallocation for the
3480 * new area, we need to make sure that no write requests are made to it
3481 * concurrently or they might be overwritten by preallocation. */
3483 bdrv_make_request_serialising(&req
, 1);
3485 ret
= bdrv_co_write_req_prepare(child
, offset
- new_bytes
, new_bytes
, &req
,
3488 error_setg_errno(errp
, -ret
,
3489 "Failed to prepare request for truncation");
3493 filtered
= bdrv_filter_child(bs
);
3494 backing
= bdrv_cow_child(bs
);
3497 * If the image has a backing file that is large enough that it would
3498 * provide data for the new area, we cannot leave it unallocated because
3499 * then the backing file content would become visible. Instead, zero-fill
3502 * Note that if the image has a backing file, but was opened without the
3503 * backing file, taking care of keeping things consistent with that backing
3504 * file is the user's responsibility.
3506 if (new_bytes
&& backing
) {
3507 int64_t backing_len
;
3509 backing_len
= bdrv_getlength(backing
->bs
);
3510 if (backing_len
< 0) {
3512 error_setg_errno(errp
, -ret
, "Could not get backing file size");
3516 if (backing_len
> old_size
) {
3517 flags
|= BDRV_REQ_ZERO_WRITE
;
3521 if (drv
->bdrv_co_truncate
) {
3522 if (flags
& ~bs
->supported_truncate_flags
) {
3523 error_setg(errp
, "Block driver does not support requested flags");
3527 ret
= drv
->bdrv_co_truncate(bs
, offset
, exact
, prealloc
, flags
, errp
);
3528 } else if (filtered
) {
3529 ret
= bdrv_co_truncate(filtered
, offset
, exact
, prealloc
, flags
, errp
);
3531 error_setg(errp
, "Image format driver does not support resize");
3539 ret
= refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
3541 error_setg_errno(errp
, -ret
, "Could not refresh total sector count");
3543 offset
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
3545 /* It's possible that truncation succeeded but refresh_total_sectors
3546 * failed, but the latter doesn't affect how we should finish the request.
3547 * Pass 0 as the last parameter so that dirty bitmaps etc. are handled. */
3548 bdrv_co_write_req_finish(child
, offset
- new_bytes
, new_bytes
, &req
, 0);
3551 tracked_request_end(&req
);
3552 bdrv_dec_in_flight(bs
);
3557 void bdrv_cancel_in_flight(BlockDriverState
*bs
)
3559 GLOBAL_STATE_CODE();
3560 if (!bs
|| !bs
->drv
) {
3564 if (bs
->drv
->bdrv_cancel_in_flight
) {
3565 bs
->drv
->bdrv_cancel_in_flight(bs
);
3570 bdrv_co_preadv_snapshot(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
3571 QEMUIOVector
*qiov
, size_t qiov_offset
)
3573 BlockDriverState
*bs
= child
->bs
;
3574 BlockDriver
*drv
= bs
->drv
;
3582 if (!drv
->bdrv_co_preadv_snapshot
) {
3586 bdrv_inc_in_flight(bs
);
3587 ret
= drv
->bdrv_co_preadv_snapshot(bs
, offset
, bytes
, qiov
, qiov_offset
);
3588 bdrv_dec_in_flight(bs
);
3594 bdrv_co_snapshot_block_status(BlockDriverState
*bs
,
3595 bool want_zero
, int64_t offset
, int64_t bytes
,
3596 int64_t *pnum
, int64_t *map
,
3597 BlockDriverState
**file
)
3599 BlockDriver
*drv
= bs
->drv
;
3607 if (!drv
->bdrv_co_snapshot_block_status
) {
3611 bdrv_inc_in_flight(bs
);
3612 ret
= drv
->bdrv_co_snapshot_block_status(bs
, want_zero
, offset
, bytes
,
3614 bdrv_dec_in_flight(bs
);
3620 bdrv_co_pdiscard_snapshot(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
3622 BlockDriver
*drv
= bs
->drv
;
3630 if (!drv
->bdrv_co_pdiscard_snapshot
) {
3634 bdrv_inc_in_flight(bs
);
3635 ret
= drv
->bdrv_co_pdiscard_snapshot(bs
, offset
, bytes
);
3636 bdrv_dec_in_flight(bs
);