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;
74 AioContext
*ctx
= bdrv_child_get_parent_aio_context(c
);
76 bdrv_parent_drained_end_single_no_poll(c
, &drained_end_counter
);
77 AIO_WAIT_WHILE(ctx
, qatomic_read(&drained_end_counter
) > 0);
80 static void bdrv_parent_drained_end(BlockDriverState
*bs
, BdrvChild
*ignore
,
81 bool ignore_bds_parents
,
82 int *drained_end_counter
)
86 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
87 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
90 bdrv_parent_drained_end_single_no_poll(c
, drained_end_counter
);
94 static bool bdrv_parent_drained_poll_single(BdrvChild
*c
)
96 if (c
->klass
->drained_poll
) {
97 return c
->klass
->drained_poll(c
);
102 static bool bdrv_parent_drained_poll(BlockDriverState
*bs
, BdrvChild
*ignore
,
103 bool ignore_bds_parents
)
108 QLIST_FOREACH_SAFE(c
, &bs
->parents
, next_parent
, next
) {
109 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
112 busy
|= bdrv_parent_drained_poll_single(c
);
118 void bdrv_parent_drained_begin_single(BdrvChild
*c
, bool poll
)
120 AioContext
*ctx
= bdrv_child_get_parent_aio_context(c
);
122 c
->parent_quiesce_counter
++;
123 if (c
->klass
->drained_begin
) {
124 c
->klass
->drained_begin(c
);
127 AIO_WAIT_WHILE(ctx
, bdrv_parent_drained_poll_single(c
));
131 static void bdrv_merge_limits(BlockLimits
*dst
, const BlockLimits
*src
)
133 dst
->pdiscard_alignment
= MAX(dst
->pdiscard_alignment
,
134 src
->pdiscard_alignment
);
135 dst
->opt_transfer
= MAX(dst
->opt_transfer
, src
->opt_transfer
);
136 dst
->max_transfer
= MIN_NON_ZERO(dst
->max_transfer
, src
->max_transfer
);
137 dst
->max_hw_transfer
= MIN_NON_ZERO(dst
->max_hw_transfer
,
138 src
->max_hw_transfer
);
139 dst
->opt_mem_alignment
= MAX(dst
->opt_mem_alignment
,
140 src
->opt_mem_alignment
);
141 dst
->min_mem_alignment
= MAX(dst
->min_mem_alignment
,
142 src
->min_mem_alignment
);
143 dst
->max_iov
= MIN_NON_ZERO(dst
->max_iov
, src
->max_iov
);
144 dst
->max_hw_iov
= MIN_NON_ZERO(dst
->max_hw_iov
, src
->max_hw_iov
);
147 typedef struct BdrvRefreshLimitsState
{
148 BlockDriverState
*bs
;
150 } BdrvRefreshLimitsState
;
152 static void bdrv_refresh_limits_abort(void *opaque
)
154 BdrvRefreshLimitsState
*s
= opaque
;
156 s
->bs
->bl
= s
->old_bl
;
159 static TransactionActionDrv bdrv_refresh_limits_drv
= {
160 .abort
= bdrv_refresh_limits_abort
,
164 /* @tran is allowed to be NULL, in this case no rollback is possible. */
165 void bdrv_refresh_limits(BlockDriverState
*bs
, Transaction
*tran
, Error
**errp
)
168 BlockDriver
*drv
= bs
->drv
;
175 BdrvRefreshLimitsState
*s
= g_new(BdrvRefreshLimitsState
, 1);
176 *s
= (BdrvRefreshLimitsState
) {
180 tran_add(tran
, &bdrv_refresh_limits_drv
, s
);
183 memset(&bs
->bl
, 0, sizeof(bs
->bl
));
189 /* Default alignment based on whether driver has byte interface */
190 bs
->bl
.request_alignment
= (drv
->bdrv_co_preadv
||
191 drv
->bdrv_aio_preadv
||
192 drv
->bdrv_co_preadv_part
) ? 1 : 512;
194 /* Take some limits from the children as a default */
196 QLIST_FOREACH(c
, &bs
->children
, next
) {
197 if (c
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_FILTERED
| BDRV_CHILD_COW
))
199 bdrv_merge_limits(&bs
->bl
, &c
->bs
->bl
);
205 bs
->bl
.min_mem_alignment
= 512;
206 bs
->bl
.opt_mem_alignment
= qemu_real_host_page_size();
208 /* Safe default since most protocols use readv()/writev()/etc */
209 bs
->bl
.max_iov
= IOV_MAX
;
212 /* Then let the driver override it */
213 if (drv
->bdrv_refresh_limits
) {
214 drv
->bdrv_refresh_limits(bs
, errp
);
220 if (bs
->bl
.request_alignment
> BDRV_MAX_ALIGNMENT
) {
221 error_setg(errp
, "Driver requires too large request alignment");
226 * The copy-on-read flag is actually a reference count so multiple users may
227 * use the feature without worrying about clobbering its previous state.
228 * Copy-on-read stays enabled until all users have called to disable it.
230 void bdrv_enable_copy_on_read(BlockDriverState
*bs
)
233 qatomic_inc(&bs
->copy_on_read
);
236 void bdrv_disable_copy_on_read(BlockDriverState
*bs
)
238 int old
= qatomic_fetch_dec(&bs
->copy_on_read
);
245 BlockDriverState
*bs
;
251 bool ignore_bds_parents
;
252 int *drained_end_counter
;
255 static void coroutine_fn
bdrv_drain_invoke_entry(void *opaque
)
257 BdrvCoDrainData
*data
= opaque
;
258 BlockDriverState
*bs
= data
->bs
;
261 bs
->drv
->bdrv_co_drain_begin(bs
);
263 bs
->drv
->bdrv_co_drain_end(bs
);
266 /* Set data->done and decrement drained_end_counter before bdrv_wakeup() */
267 qatomic_mb_set(&data
->done
, true);
269 qatomic_dec(data
->drained_end_counter
);
271 bdrv_dec_in_flight(bs
);
276 /* Recursively call BlockDriver.bdrv_co_drain_begin/end callbacks */
277 static void bdrv_drain_invoke(BlockDriverState
*bs
, bool begin
,
278 int *drained_end_counter
)
280 BdrvCoDrainData
*data
;
282 if (!bs
->drv
|| (begin
&& !bs
->drv
->bdrv_co_drain_begin
) ||
283 (!begin
&& !bs
->drv
->bdrv_co_drain_end
)) {
287 data
= g_new(BdrvCoDrainData
, 1);
288 *data
= (BdrvCoDrainData
) {
292 .drained_end_counter
= drained_end_counter
,
296 qatomic_inc(drained_end_counter
);
299 /* Make sure the driver callback completes during the polling phase for
301 bdrv_inc_in_flight(bs
);
302 data
->co
= qemu_coroutine_create(bdrv_drain_invoke_entry
, data
);
303 aio_co_schedule(bdrv_get_aio_context(bs
), data
->co
);
306 /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
307 bool bdrv_drain_poll(BlockDriverState
*bs
, bool recursive
,
308 BdrvChild
*ignore_parent
, bool ignore_bds_parents
)
310 BdrvChild
*child
, *next
;
313 if (bdrv_parent_drained_poll(bs
, ignore_parent
, ignore_bds_parents
)) {
317 if (qatomic_read(&bs
->in_flight
)) {
322 assert(!ignore_bds_parents
);
323 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
324 if (bdrv_drain_poll(child
->bs
, recursive
, child
, false)) {
333 static bool bdrv_drain_poll_top_level(BlockDriverState
*bs
, bool recursive
,
334 BdrvChild
*ignore_parent
)
336 return bdrv_drain_poll(bs
, recursive
, ignore_parent
, false);
339 static void bdrv_do_drained_begin(BlockDriverState
*bs
, bool recursive
,
340 BdrvChild
*parent
, bool ignore_bds_parents
,
342 static void bdrv_do_drained_end(BlockDriverState
*bs
, bool recursive
,
343 BdrvChild
*parent
, bool ignore_bds_parents
,
344 int *drained_end_counter
);
346 static void bdrv_co_drain_bh_cb(void *opaque
)
348 BdrvCoDrainData
*data
= opaque
;
349 Coroutine
*co
= data
->co
;
350 BlockDriverState
*bs
= data
->bs
;
353 AioContext
*ctx
= bdrv_get_aio_context(bs
);
354 aio_context_acquire(ctx
);
355 bdrv_dec_in_flight(bs
);
357 assert(!data
->drained_end_counter
);
358 bdrv_do_drained_begin(bs
, data
->recursive
, data
->parent
,
359 data
->ignore_bds_parents
, data
->poll
);
362 bdrv_do_drained_end(bs
, data
->recursive
, data
->parent
,
363 data
->ignore_bds_parents
,
364 data
->drained_end_counter
);
366 aio_context_release(ctx
);
369 bdrv_drain_all_begin();
376 static void coroutine_fn
bdrv_co_yield_to_drain(BlockDriverState
*bs
,
377 bool begin
, bool recursive
,
379 bool ignore_bds_parents
,
381 int *drained_end_counter
)
383 BdrvCoDrainData data
;
384 Coroutine
*self
= qemu_coroutine_self();
385 AioContext
*ctx
= bdrv_get_aio_context(bs
);
386 AioContext
*co_ctx
= qemu_coroutine_get_aio_context(self
);
388 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
389 * other coroutines run if they were queued by aio_co_enter(). */
391 assert(qemu_in_coroutine());
392 data
= (BdrvCoDrainData
) {
397 .recursive
= recursive
,
399 .ignore_bds_parents
= ignore_bds_parents
,
401 .drained_end_counter
= drained_end_counter
,
405 bdrv_inc_in_flight(bs
);
409 * Temporarily drop the lock across yield or we would get deadlocks.
410 * bdrv_co_drain_bh_cb() reaquires the lock as needed.
412 * When we yield below, the lock for the current context will be
413 * released, so if this is actually the lock that protects bs, don't drop
417 aio_context_release(ctx
);
419 replay_bh_schedule_oneshot_event(ctx
, bdrv_co_drain_bh_cb
, &data
);
421 qemu_coroutine_yield();
422 /* If we are resumed from some other event (such as an aio completion or a
423 * timer callback), it is a bug in the caller that should be fixed. */
426 /* Reaquire the AioContext of bs if we dropped it */
428 aio_context_acquire(ctx
);
432 void bdrv_do_drained_begin_quiesce(BlockDriverState
*bs
,
433 BdrvChild
*parent
, bool ignore_bds_parents
)
436 assert(!qemu_in_coroutine());
438 /* Stop things in parent-to-child order */
439 if (qatomic_fetch_inc(&bs
->quiesce_counter
) == 0) {
440 aio_disable_external(bdrv_get_aio_context(bs
));
443 bdrv_parent_drained_begin(bs
, parent
, ignore_bds_parents
);
444 bdrv_drain_invoke(bs
, true, NULL
);
447 static void bdrv_do_drained_begin(BlockDriverState
*bs
, bool recursive
,
448 BdrvChild
*parent
, bool ignore_bds_parents
,
451 BdrvChild
*child
, *next
;
453 if (qemu_in_coroutine()) {
454 bdrv_co_yield_to_drain(bs
, true, recursive
, parent
, ignore_bds_parents
,
459 bdrv_do_drained_begin_quiesce(bs
, parent
, ignore_bds_parents
);
462 assert(!ignore_bds_parents
);
463 bs
->recursive_quiesce_counter
++;
464 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
465 bdrv_do_drained_begin(child
->bs
, true, child
, ignore_bds_parents
,
471 * Wait for drained requests to finish.
473 * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
474 * call is needed so things in this AioContext can make progress even
475 * though we don't return to the main AioContext loop - this automatically
476 * includes other nodes in the same AioContext and therefore all child
480 assert(!ignore_bds_parents
);
481 BDRV_POLL_WHILE(bs
, bdrv_drain_poll_top_level(bs
, recursive
, parent
));
485 void bdrv_drained_begin(BlockDriverState
*bs
)
488 bdrv_do_drained_begin(bs
, false, NULL
, false, true);
491 void bdrv_subtree_drained_begin(BlockDriverState
*bs
)
494 bdrv_do_drained_begin(bs
, true, NULL
, false, true);
498 * This function does not poll, nor must any of its recursively called
499 * functions. The *drained_end_counter pointee will be incremented
500 * once for every background operation scheduled, and decremented once
501 * the operation settles. Therefore, the pointer must remain valid
502 * until the pointee reaches 0. That implies that whoever sets up the
503 * pointee has to poll until it is 0.
505 * We use atomic operations to access *drained_end_counter, because
506 * (1) when called from bdrv_set_aio_context_ignore(), the subgraph of
507 * @bs may contain nodes in different AioContexts,
508 * (2) bdrv_drain_all_end() uses the same counter for all nodes,
509 * regardless of which AioContext they are in.
511 static void bdrv_do_drained_end(BlockDriverState
*bs
, bool recursive
,
512 BdrvChild
*parent
, bool ignore_bds_parents
,
513 int *drained_end_counter
)
516 int old_quiesce_counter
;
518 assert(drained_end_counter
!= NULL
);
520 if (qemu_in_coroutine()) {
521 bdrv_co_yield_to_drain(bs
, false, recursive
, parent
, ignore_bds_parents
,
522 false, drained_end_counter
);
525 assert(bs
->quiesce_counter
> 0);
527 /* Re-enable things in child-to-parent order */
528 bdrv_drain_invoke(bs
, false, drained_end_counter
);
529 bdrv_parent_drained_end(bs
, parent
, ignore_bds_parents
,
530 drained_end_counter
);
532 old_quiesce_counter
= qatomic_fetch_dec(&bs
->quiesce_counter
);
533 if (old_quiesce_counter
== 1) {
534 aio_enable_external(bdrv_get_aio_context(bs
));
538 assert(!ignore_bds_parents
);
539 bs
->recursive_quiesce_counter
--;
540 QLIST_FOREACH(child
, &bs
->children
, next
) {
541 bdrv_do_drained_end(child
->bs
, true, child
, ignore_bds_parents
,
542 drained_end_counter
);
547 void bdrv_drained_end(BlockDriverState
*bs
)
549 int drained_end_counter
= 0;
551 bdrv_do_drained_end(bs
, false, NULL
, false, &drained_end_counter
);
552 BDRV_POLL_WHILE(bs
, qatomic_read(&drained_end_counter
) > 0);
555 void bdrv_drained_end_no_poll(BlockDriverState
*bs
, int *drained_end_counter
)
558 bdrv_do_drained_end(bs
, false, NULL
, false, drained_end_counter
);
561 void bdrv_subtree_drained_end(BlockDriverState
*bs
)
563 int drained_end_counter
= 0;
565 bdrv_do_drained_end(bs
, true, NULL
, false, &drained_end_counter
);
566 BDRV_POLL_WHILE(bs
, qatomic_read(&drained_end_counter
) > 0);
569 void bdrv_apply_subtree_drain(BdrvChild
*child
, BlockDriverState
*new_parent
)
574 for (i
= 0; i
< new_parent
->recursive_quiesce_counter
; i
++) {
575 bdrv_do_drained_begin(child
->bs
, true, child
, false, true);
579 void bdrv_unapply_subtree_drain(BdrvChild
*child
, BlockDriverState
*old_parent
)
581 int drained_end_counter
= 0;
585 for (i
= 0; i
< old_parent
->recursive_quiesce_counter
; i
++) {
586 bdrv_do_drained_end(child
->bs
, true, child
, false,
587 &drained_end_counter
);
590 BDRV_POLL_WHILE(child
->bs
, qatomic_read(&drained_end_counter
) > 0);
593 void bdrv_drain(BlockDriverState
*bs
)
596 bdrv_drained_begin(bs
);
597 bdrv_drained_end(bs
);
600 static void bdrv_drain_assert_idle(BlockDriverState
*bs
)
602 BdrvChild
*child
, *next
;
604 assert(qatomic_read(&bs
->in_flight
) == 0);
605 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
606 bdrv_drain_assert_idle(child
->bs
);
610 unsigned int bdrv_drain_all_count
= 0;
612 static bool bdrv_drain_all_poll(void)
614 BlockDriverState
*bs
= NULL
;
618 /* bdrv_drain_poll() can't make changes to the graph and we are holding the
619 * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
620 while ((bs
= bdrv_next_all_states(bs
))) {
621 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
622 aio_context_acquire(aio_context
);
623 result
|= bdrv_drain_poll(bs
, false, NULL
, true);
624 aio_context_release(aio_context
);
631 * Wait for pending requests to complete across all BlockDriverStates
633 * This function does not flush data to disk, use bdrv_flush_all() for that
634 * after calling this function.
636 * This pauses all block jobs and disables external clients. It must
637 * be paired with bdrv_drain_all_end().
639 * NOTE: no new block jobs or BlockDriverStates can be created between
640 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
642 void bdrv_drain_all_begin(void)
644 BlockDriverState
*bs
= NULL
;
647 if (qemu_in_coroutine()) {
648 bdrv_co_yield_to_drain(NULL
, true, false, NULL
, true, true, NULL
);
653 * bdrv queue is managed by record/replay,
654 * waiting for finishing the I/O requests may
657 if (replay_events_enabled()) {
661 /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
662 * loop AioContext, so make sure we're in the main context. */
663 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
664 assert(bdrv_drain_all_count
< INT_MAX
);
665 bdrv_drain_all_count
++;
667 /* Quiesce all nodes, without polling in-flight requests yet. The graph
668 * cannot change during this loop. */
669 while ((bs
= bdrv_next_all_states(bs
))) {
670 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
672 aio_context_acquire(aio_context
);
673 bdrv_do_drained_begin(bs
, false, NULL
, true, false);
674 aio_context_release(aio_context
);
677 /* Now poll the in-flight requests */
678 AIO_WAIT_WHILE(NULL
, bdrv_drain_all_poll());
680 while ((bs
= bdrv_next_all_states(bs
))) {
681 bdrv_drain_assert_idle(bs
);
685 void bdrv_drain_all_end_quiesce(BlockDriverState
*bs
)
687 int drained_end_counter
= 0;
690 g_assert(bs
->quiesce_counter
> 0);
691 g_assert(!bs
->refcnt
);
693 while (bs
->quiesce_counter
) {
694 bdrv_do_drained_end(bs
, false, NULL
, true, &drained_end_counter
);
696 BDRV_POLL_WHILE(bs
, qatomic_read(&drained_end_counter
) > 0);
699 void bdrv_drain_all_end(void)
701 BlockDriverState
*bs
= NULL
;
702 int drained_end_counter
= 0;
706 * bdrv queue is managed by record/replay,
707 * waiting for finishing the I/O requests may
710 if (replay_events_enabled()) {
714 while ((bs
= bdrv_next_all_states(bs
))) {
715 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
717 aio_context_acquire(aio_context
);
718 bdrv_do_drained_end(bs
, false, NULL
, true, &drained_end_counter
);
719 aio_context_release(aio_context
);
722 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
723 AIO_WAIT_WHILE(NULL
, qatomic_read(&drained_end_counter
) > 0);
725 assert(bdrv_drain_all_count
> 0);
726 bdrv_drain_all_count
--;
729 void bdrv_drain_all(void)
732 bdrv_drain_all_begin();
733 bdrv_drain_all_end();
737 * Remove an active request from the tracked requests list
739 * This function should be called when a tracked request is completing.
741 static void coroutine_fn
tracked_request_end(BdrvTrackedRequest
*req
)
743 if (req
->serialising
) {
744 qatomic_dec(&req
->bs
->serialising_in_flight
);
747 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
748 QLIST_REMOVE(req
, list
);
749 qemu_co_queue_restart_all(&req
->wait_queue
);
750 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
754 * Add an active request to the tracked requests list
756 static void coroutine_fn
tracked_request_begin(BdrvTrackedRequest
*req
,
757 BlockDriverState
*bs
,
760 enum BdrvTrackedRequestType type
)
762 bdrv_check_request(offset
, bytes
, &error_abort
);
764 *req
= (BdrvTrackedRequest
){
769 .co
= qemu_coroutine_self(),
770 .serialising
= false,
771 .overlap_offset
= offset
,
772 .overlap_bytes
= bytes
,
775 qemu_co_queue_init(&req
->wait_queue
);
777 qemu_co_mutex_lock(&bs
->reqs_lock
);
778 QLIST_INSERT_HEAD(&bs
->tracked_requests
, req
, list
);
779 qemu_co_mutex_unlock(&bs
->reqs_lock
);
782 static bool tracked_request_overlaps(BdrvTrackedRequest
*req
,
783 int64_t offset
, int64_t bytes
)
785 bdrv_check_request(offset
, bytes
, &error_abort
);
788 if (offset
>= req
->overlap_offset
+ req
->overlap_bytes
) {
792 if (req
->overlap_offset
>= offset
+ bytes
) {
798 /* Called with self->bs->reqs_lock held */
799 static coroutine_fn BdrvTrackedRequest
*
800 bdrv_find_conflicting_request(BdrvTrackedRequest
*self
)
802 BdrvTrackedRequest
*req
;
804 QLIST_FOREACH(req
, &self
->bs
->tracked_requests
, list
) {
805 if (req
== self
|| (!req
->serialising
&& !self
->serialising
)) {
808 if (tracked_request_overlaps(req
, self
->overlap_offset
,
809 self
->overlap_bytes
))
812 * Hitting this means there was a reentrant request, for
813 * example, a block driver issuing nested requests. This must
814 * never happen since it means deadlock.
816 assert(qemu_coroutine_self() != req
->co
);
819 * If the request is already (indirectly) waiting for us, or
820 * will wait for us as soon as it wakes up, then just go on
821 * (instead of producing a deadlock in the former case).
823 if (!req
->waiting_for
) {
832 /* Called with self->bs->reqs_lock held */
833 static void coroutine_fn
834 bdrv_wait_serialising_requests_locked(BdrvTrackedRequest
*self
)
836 BdrvTrackedRequest
*req
;
838 while ((req
= bdrv_find_conflicting_request(self
))) {
839 self
->waiting_for
= req
;
840 qemu_co_queue_wait(&req
->wait_queue
, &self
->bs
->reqs_lock
);
841 self
->waiting_for
= NULL
;
845 /* Called with req->bs->reqs_lock held */
846 static void tracked_request_set_serialising(BdrvTrackedRequest
*req
,
849 int64_t overlap_offset
= req
->offset
& ~(align
- 1);
850 int64_t overlap_bytes
=
851 ROUND_UP(req
->offset
+ req
->bytes
, align
) - overlap_offset
;
853 bdrv_check_request(req
->offset
, req
->bytes
, &error_abort
);
855 if (!req
->serialising
) {
856 qatomic_inc(&req
->bs
->serialising_in_flight
);
857 req
->serialising
= true;
860 req
->overlap_offset
= MIN(req
->overlap_offset
, overlap_offset
);
861 req
->overlap_bytes
= MAX(req
->overlap_bytes
, overlap_bytes
);
865 * Return the tracked request on @bs for the current coroutine, or
866 * NULL if there is none.
868 BdrvTrackedRequest
*coroutine_fn
bdrv_co_get_self_request(BlockDriverState
*bs
)
870 BdrvTrackedRequest
*req
;
871 Coroutine
*self
= qemu_coroutine_self();
874 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
875 if (req
->co
== self
) {
884 * Round a region to cluster boundaries
886 void bdrv_round_to_clusters(BlockDriverState
*bs
,
887 int64_t offset
, int64_t bytes
,
888 int64_t *cluster_offset
,
889 int64_t *cluster_bytes
)
893 if (bdrv_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
894 *cluster_offset
= offset
;
895 *cluster_bytes
= bytes
;
897 int64_t c
= bdi
.cluster_size
;
898 *cluster_offset
= QEMU_ALIGN_DOWN(offset
, c
);
899 *cluster_bytes
= QEMU_ALIGN_UP(offset
- *cluster_offset
+ bytes
, c
);
903 static int bdrv_get_cluster_size(BlockDriverState
*bs
)
908 ret
= bdrv_get_info(bs
, &bdi
);
909 if (ret
< 0 || bdi
.cluster_size
== 0) {
910 return bs
->bl
.request_alignment
;
912 return bdi
.cluster_size
;
916 void bdrv_inc_in_flight(BlockDriverState
*bs
)
919 qatomic_inc(&bs
->in_flight
);
922 void bdrv_wakeup(BlockDriverState
*bs
)
928 void bdrv_dec_in_flight(BlockDriverState
*bs
)
931 qatomic_dec(&bs
->in_flight
);
935 static void coroutine_fn
936 bdrv_wait_serialising_requests(BdrvTrackedRequest
*self
)
938 BlockDriverState
*bs
= self
->bs
;
940 if (!qatomic_read(&bs
->serialising_in_flight
)) {
944 qemu_co_mutex_lock(&bs
->reqs_lock
);
945 bdrv_wait_serialising_requests_locked(self
);
946 qemu_co_mutex_unlock(&bs
->reqs_lock
);
949 void coroutine_fn
bdrv_make_request_serialising(BdrvTrackedRequest
*req
,
954 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
956 tracked_request_set_serialising(req
, align
);
957 bdrv_wait_serialising_requests_locked(req
);
959 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
962 int bdrv_check_qiov_request(int64_t offset
, int64_t bytes
,
963 QEMUIOVector
*qiov
, size_t qiov_offset
,
967 * Check generic offset/bytes correctness
971 error_setg(errp
, "offset is negative: %" PRIi64
, offset
);
976 error_setg(errp
, "bytes is negative: %" PRIi64
, bytes
);
980 if (bytes
> BDRV_MAX_LENGTH
) {
981 error_setg(errp
, "bytes(%" PRIi64
") exceeds maximum(%" PRIi64
")",
982 bytes
, BDRV_MAX_LENGTH
);
986 if (offset
> BDRV_MAX_LENGTH
) {
987 error_setg(errp
, "offset(%" PRIi64
") exceeds maximum(%" PRIi64
")",
988 offset
, BDRV_MAX_LENGTH
);
992 if (offset
> BDRV_MAX_LENGTH
- bytes
) {
993 error_setg(errp
, "sum of offset(%" PRIi64
") and bytes(%" PRIi64
") "
994 "exceeds maximum(%" PRIi64
")", offset
, bytes
,
1004 * Check qiov and qiov_offset
1007 if (qiov_offset
> qiov
->size
) {
1008 error_setg(errp
, "qiov_offset(%zu) overflow io vector size(%zu)",
1009 qiov_offset
, qiov
->size
);
1013 if (bytes
> qiov
->size
- qiov_offset
) {
1014 error_setg(errp
, "bytes(%" PRIi64
") + qiov_offset(%zu) overflow io "
1015 "vector size(%zu)", bytes
, qiov_offset
, qiov
->size
);
1022 int bdrv_check_request(int64_t offset
, int64_t bytes
, Error
**errp
)
1024 return bdrv_check_qiov_request(offset
, bytes
, NULL
, 0, errp
);
1027 static int bdrv_check_request32(int64_t offset
, int64_t bytes
,
1028 QEMUIOVector
*qiov
, size_t qiov_offset
)
1030 int ret
= bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, NULL
);
1035 if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
1043 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
1044 * The operation is sped up by checking the block status and only writing
1045 * zeroes to the device if they currently do not return zeroes. Optional
1046 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
1049 * Returns < 0 on error, 0 on success. For error codes see bdrv_pwrite().
1051 int bdrv_make_zero(BdrvChild
*child
, BdrvRequestFlags flags
)
1054 int64_t target_size
, bytes
, offset
= 0;
1055 BlockDriverState
*bs
= child
->bs
;
1058 target_size
= bdrv_getlength(bs
);
1059 if (target_size
< 0) {
1064 bytes
= MIN(target_size
- offset
, BDRV_REQUEST_MAX_BYTES
);
1068 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, NULL
, NULL
);
1072 if (ret
& BDRV_BLOCK_ZERO
) {
1076 ret
= bdrv_pwrite_zeroes(child
, offset
, bytes
, flags
);
1085 * Writes to the file and ensures that no writes are reordered across this
1086 * request (acts as a barrier)
1088 * Returns 0 on success, -errno in error cases.
1090 int coroutine_fn
bdrv_co_pwrite_sync(BdrvChild
*child
, int64_t offset
,
1091 int64_t bytes
, const void *buf
,
1092 BdrvRequestFlags flags
)
1097 ret
= bdrv_co_pwrite(child
, offset
, bytes
, buf
, flags
);
1102 ret
= bdrv_co_flush(child
->bs
);
1110 typedef struct CoroutineIOCompletion
{
1111 Coroutine
*coroutine
;
1113 } CoroutineIOCompletion
;
1115 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
1117 CoroutineIOCompletion
*co
= opaque
;
1120 aio_co_wake(co
->coroutine
);
1123 static int coroutine_fn
bdrv_driver_preadv(BlockDriverState
*bs
,
1124 int64_t offset
, int64_t bytes
,
1126 size_t qiov_offset
, int flags
)
1128 BlockDriver
*drv
= bs
->drv
;
1130 unsigned int nb_sectors
;
1131 QEMUIOVector local_qiov
;
1134 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1135 assert(!(flags
& ~bs
->supported_read_flags
));
1141 if (drv
->bdrv_co_preadv_part
) {
1142 return drv
->bdrv_co_preadv_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
1146 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
1147 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1151 if (drv
->bdrv_co_preadv
) {
1152 ret
= drv
->bdrv_co_preadv(bs
, offset
, bytes
, qiov
, flags
);
1156 if (drv
->bdrv_aio_preadv
) {
1158 CoroutineIOCompletion co
= {
1159 .coroutine
= qemu_coroutine_self(),
1162 acb
= drv
->bdrv_aio_preadv(bs
, offset
, bytes
, qiov
, flags
,
1163 bdrv_co_io_em_complete
, &co
);
1168 qemu_coroutine_yield();
1174 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1175 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1177 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1178 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1179 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1180 assert(drv
->bdrv_co_readv
);
1182 ret
= drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
1185 if (qiov
== &local_qiov
) {
1186 qemu_iovec_destroy(&local_qiov
);
1192 static int coroutine_fn
bdrv_driver_pwritev(BlockDriverState
*bs
,
1193 int64_t offset
, int64_t bytes
,
1196 BdrvRequestFlags flags
)
1198 BlockDriver
*drv
= bs
->drv
;
1199 bool emulate_fua
= false;
1201 unsigned int nb_sectors
;
1202 QEMUIOVector local_qiov
;
1205 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1211 if ((flags
& BDRV_REQ_FUA
) &&
1212 (~bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1213 flags
&= ~BDRV_REQ_FUA
;
1217 flags
&= bs
->supported_write_flags
;
1219 if (drv
->bdrv_co_pwritev_part
) {
1220 ret
= drv
->bdrv_co_pwritev_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
1225 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
1226 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1230 if (drv
->bdrv_co_pwritev
) {
1231 ret
= drv
->bdrv_co_pwritev(bs
, offset
, bytes
, qiov
, flags
);
1235 if (drv
->bdrv_aio_pwritev
) {
1237 CoroutineIOCompletion co
= {
1238 .coroutine
= qemu_coroutine_self(),
1241 acb
= drv
->bdrv_aio_pwritev(bs
, offset
, bytes
, qiov
, flags
,
1242 bdrv_co_io_em_complete
, &co
);
1246 qemu_coroutine_yield();
1252 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1253 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1255 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1256 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1257 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1259 assert(drv
->bdrv_co_writev
);
1260 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
, flags
);
1263 if (ret
== 0 && emulate_fua
) {
1264 ret
= bdrv_co_flush(bs
);
1267 if (qiov
== &local_qiov
) {
1268 qemu_iovec_destroy(&local_qiov
);
1274 static int coroutine_fn
1275 bdrv_driver_pwritev_compressed(BlockDriverState
*bs
, int64_t offset
,
1276 int64_t bytes
, QEMUIOVector
*qiov
,
1279 BlockDriver
*drv
= bs
->drv
;
1280 QEMUIOVector local_qiov
;
1283 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1289 if (!block_driver_can_compress(drv
)) {
1293 if (drv
->bdrv_co_pwritev_compressed_part
) {
1294 return drv
->bdrv_co_pwritev_compressed_part(bs
, offset
, bytes
,
1298 if (qiov_offset
== 0) {
1299 return drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, qiov
);
1302 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1303 ret
= drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, &local_qiov
);
1304 qemu_iovec_destroy(&local_qiov
);
1309 static int coroutine_fn
bdrv_co_do_copy_on_readv(BdrvChild
*child
,
1310 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
1311 size_t qiov_offset
, int flags
)
1313 BlockDriverState
*bs
= child
->bs
;
1315 /* Perform I/O through a temporary buffer so that users who scribble over
1316 * their read buffer while the operation is in progress do not end up
1317 * modifying the image file. This is critical for zero-copy guest I/O
1318 * where anything might happen inside guest memory.
1320 void *bounce_buffer
= NULL
;
1322 BlockDriver
*drv
= bs
->drv
;
1323 int64_t cluster_offset
;
1324 int64_t cluster_bytes
;
1327 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
,
1328 BDRV_REQUEST_MAX_BYTES
);
1329 int64_t progress
= 0;
1332 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1339 * Do not write anything when the BDS is inactive. That is not
1340 * allowed, and it would not help.
1342 skip_write
= (bs
->open_flags
& BDRV_O_INACTIVE
);
1344 /* FIXME We cannot require callers to have write permissions when all they
1345 * are doing is a read request. If we did things right, write permissions
1346 * would be obtained anyway, but internally by the copy-on-read code. As
1347 * long as it is implemented here rather than in a separate filter driver,
1348 * the copy-on-read code doesn't have its own BdrvChild, however, for which
1349 * it could request permissions. Therefore we have to bypass the permission
1350 * system for the moment. */
1351 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1353 /* Cover entire cluster so no additional backing file I/O is required when
1354 * allocating cluster in the image file. Note that this value may exceed
1355 * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1356 * is one reason we loop rather than doing it all at once.
1358 bdrv_round_to_clusters(bs
, offset
, bytes
, &cluster_offset
, &cluster_bytes
);
1359 skip_bytes
= offset
- cluster_offset
;
1361 trace_bdrv_co_do_copy_on_readv(bs
, offset
, bytes
,
1362 cluster_offset
, cluster_bytes
);
1364 while (cluster_bytes
) {
1368 ret
= 1; /* "already allocated", so nothing will be copied */
1369 pnum
= MIN(cluster_bytes
, max_transfer
);
1371 ret
= bdrv_is_allocated(bs
, cluster_offset
,
1372 MIN(cluster_bytes
, max_transfer
), &pnum
);
1375 * Safe to treat errors in querying allocation as if
1376 * unallocated; we'll probably fail again soon on the
1377 * read, but at least that will set a decent errno.
1379 pnum
= MIN(cluster_bytes
, max_transfer
);
1382 /* Stop at EOF if the image ends in the middle of the cluster */
1383 if (ret
== 0 && pnum
== 0) {
1384 assert(progress
>= bytes
);
1388 assert(skip_bytes
< pnum
);
1392 QEMUIOVector local_qiov
;
1394 /* Must copy-on-read; use the bounce buffer */
1395 pnum
= MIN(pnum
, MAX_BOUNCE_BUFFER
);
1396 if (!bounce_buffer
) {
1397 int64_t max_we_need
= MAX(pnum
, cluster_bytes
- pnum
);
1398 int64_t max_allowed
= MIN(max_transfer
, MAX_BOUNCE_BUFFER
);
1399 int64_t bounce_buffer_len
= MIN(max_we_need
, max_allowed
);
1401 bounce_buffer
= qemu_try_blockalign(bs
, bounce_buffer_len
);
1402 if (!bounce_buffer
) {
1407 qemu_iovec_init_buf(&local_qiov
, bounce_buffer
, pnum
);
1409 ret
= bdrv_driver_preadv(bs
, cluster_offset
, pnum
,
1415 bdrv_debug_event(bs
, BLKDBG_COR_WRITE
);
1416 if (drv
->bdrv_co_pwrite_zeroes
&&
1417 buffer_is_zero(bounce_buffer
, pnum
)) {
1418 /* FIXME: Should we (perhaps conditionally) be setting
1419 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1420 * that still correctly reads as zero? */
1421 ret
= bdrv_co_do_pwrite_zeroes(bs
, cluster_offset
, pnum
,
1422 BDRV_REQ_WRITE_UNCHANGED
);
1424 /* This does not change the data on the disk, it is not
1425 * necessary to flush even in cache=writethrough mode.
1427 ret
= bdrv_driver_pwritev(bs
, cluster_offset
, pnum
,
1429 BDRV_REQ_WRITE_UNCHANGED
);
1433 /* It might be okay to ignore write errors for guest
1434 * requests. If this is a deliberate copy-on-read
1435 * then we don't want to ignore the error. Simply
1436 * report it in all cases.
1441 if (!(flags
& BDRV_REQ_PREFETCH
)) {
1442 qemu_iovec_from_buf(qiov
, qiov_offset
+ progress
,
1443 bounce_buffer
+ skip_bytes
,
1444 MIN(pnum
- skip_bytes
, bytes
- progress
));
1446 } else if (!(flags
& BDRV_REQ_PREFETCH
)) {
1447 /* Read directly into the destination */
1448 ret
= bdrv_driver_preadv(bs
, offset
+ progress
,
1449 MIN(pnum
- skip_bytes
, bytes
- progress
),
1450 qiov
, qiov_offset
+ progress
, 0);
1456 cluster_offset
+= pnum
;
1457 cluster_bytes
-= pnum
;
1458 progress
+= pnum
- skip_bytes
;
1464 qemu_vfree(bounce_buffer
);
1469 * Forwards an already correctly aligned request to the BlockDriver. This
1470 * handles copy on read, zeroing after EOF, and fragmentation of large
1471 * reads; any other features must be implemented by the caller.
1473 static int coroutine_fn
bdrv_aligned_preadv(BdrvChild
*child
,
1474 BdrvTrackedRequest
*req
, int64_t offset
, int64_t bytes
,
1475 int64_t align
, QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
1477 BlockDriverState
*bs
= child
->bs
;
1478 int64_t total_bytes
, max_bytes
;
1480 int64_t bytes_remaining
= bytes
;
1483 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1484 assert(is_power_of_2(align
));
1485 assert((offset
& (align
- 1)) == 0);
1486 assert((bytes
& (align
- 1)) == 0);
1487 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1488 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1492 * TODO: We would need a per-BDS .supported_read_flags and
1493 * potential fallback support, if we ever implement any read flags
1494 * to pass through to drivers. For now, there aren't any
1495 * passthrough flags except the BDRV_REQ_REGISTERED_BUF optimization hint.
1497 assert(!(flags
& ~(BDRV_REQ_COPY_ON_READ
| BDRV_REQ_PREFETCH
|
1498 BDRV_REQ_REGISTERED_BUF
)));
1500 /* Handle Copy on Read and associated serialisation */
1501 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1502 /* If we touch the same cluster it counts as an overlap. This
1503 * guarantees that allocating writes will be serialized and not race
1504 * with each other for the same cluster. For example, in copy-on-read
1505 * it ensures that the CoR read and write operations are atomic and
1506 * guest writes cannot interleave between them. */
1507 bdrv_make_request_serialising(req
, bdrv_get_cluster_size(bs
));
1509 bdrv_wait_serialising_requests(req
);
1512 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1515 /* The flag BDRV_REQ_COPY_ON_READ has reached its addressee */
1516 flags
&= ~BDRV_REQ_COPY_ON_READ
;
1518 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &pnum
);
1523 if (!ret
|| pnum
!= bytes
) {
1524 ret
= bdrv_co_do_copy_on_readv(child
, offset
, bytes
,
1525 qiov
, qiov_offset
, flags
);
1527 } else if (flags
& BDRV_REQ_PREFETCH
) {
1532 /* Forward the request to the BlockDriver, possibly fragmenting it */
1533 total_bytes
= bdrv_getlength(bs
);
1534 if (total_bytes
< 0) {
1539 assert(!(flags
& ~(bs
->supported_read_flags
| BDRV_REQ_REGISTERED_BUF
)));
1541 max_bytes
= ROUND_UP(MAX(0, total_bytes
- offset
), align
);
1542 if (bytes
<= max_bytes
&& bytes
<= max_transfer
) {
1543 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1547 while (bytes_remaining
) {
1551 num
= MIN(bytes_remaining
, MIN(max_bytes
, max_transfer
));
1554 ret
= bdrv_driver_preadv(bs
, offset
+ bytes
- bytes_remaining
,
1556 qiov_offset
+ bytes
- bytes_remaining
,
1560 num
= bytes_remaining
;
1561 ret
= qemu_iovec_memset(qiov
, qiov_offset
+ bytes
- bytes_remaining
,
1562 0, bytes_remaining
);
1567 bytes_remaining
-= num
;
1571 return ret
< 0 ? ret
: 0;
1577 * |<---- align ----->| |<----- align ---->|
1578 * |<- head ->|<------------- bytes ------------->|<-- tail -->|
1580 * -*----------$-------*-------- ... --------*-----$------------*---
1582 * | offset | | end |
1583 * ALIGN_DOWN(offset) ALIGN_UP(offset) ALIGN_DOWN(end) ALIGN_UP(end)
1584 * [buf ... ) [tail_buf )
1586 * @buf is an aligned allocation needed to store @head and @tail paddings. @head
1587 * is placed at the beginning of @buf and @tail at the @end.
1589 * @tail_buf is a pointer to sub-buffer, corresponding to align-sized chunk
1590 * around tail, if tail exists.
1592 * @merge_reads is true for small requests,
1593 * if @buf_len == @head + bytes + @tail. In this case it is possible that both
1594 * head and tail exist but @buf_len == align and @tail_buf == @buf.
1596 typedef struct BdrvRequestPadding
{
1603 QEMUIOVector local_qiov
;
1604 } BdrvRequestPadding
;
1606 static bool bdrv_init_padding(BlockDriverState
*bs
,
1607 int64_t offset
, int64_t bytes
,
1608 BdrvRequestPadding
*pad
)
1610 int64_t align
= bs
->bl
.request_alignment
;
1613 bdrv_check_request(offset
, bytes
, &error_abort
);
1614 assert(align
<= INT_MAX
); /* documented in block/block_int.h */
1615 assert(align
<= SIZE_MAX
/ 2); /* so we can allocate the buffer */
1617 memset(pad
, 0, sizeof(*pad
));
1619 pad
->head
= offset
& (align
- 1);
1620 pad
->tail
= ((offset
+ bytes
) & (align
- 1));
1622 pad
->tail
= align
- pad
->tail
;
1625 if (!pad
->head
&& !pad
->tail
) {
1629 assert(bytes
); /* Nothing good in aligning zero-length requests */
1631 sum
= pad
->head
+ bytes
+ pad
->tail
;
1632 pad
->buf_len
= (sum
> align
&& pad
->head
&& pad
->tail
) ? 2 * align
: align
;
1633 pad
->buf
= qemu_blockalign(bs
, pad
->buf_len
);
1634 pad
->merge_reads
= sum
== pad
->buf_len
;
1636 pad
->tail_buf
= pad
->buf
+ pad
->buf_len
- align
;
1642 static coroutine_fn
int bdrv_padding_rmw_read(BdrvChild
*child
,
1643 BdrvTrackedRequest
*req
,
1644 BdrvRequestPadding
*pad
,
1647 QEMUIOVector local_qiov
;
1648 BlockDriverState
*bs
= child
->bs
;
1649 uint64_t align
= bs
->bl
.request_alignment
;
1652 assert(req
->serialising
&& pad
->buf
);
1654 if (pad
->head
|| pad
->merge_reads
) {
1655 int64_t bytes
= pad
->merge_reads
? pad
->buf_len
: align
;
1657 qemu_iovec_init_buf(&local_qiov
, pad
->buf
, bytes
);
1660 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1662 if (pad
->merge_reads
&& pad
->tail
) {
1663 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1665 ret
= bdrv_aligned_preadv(child
, req
, req
->overlap_offset
, bytes
,
1666 align
, &local_qiov
, 0, 0);
1671 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1673 if (pad
->merge_reads
&& pad
->tail
) {
1674 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1677 if (pad
->merge_reads
) {
1683 qemu_iovec_init_buf(&local_qiov
, pad
->tail_buf
, align
);
1685 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1686 ret
= bdrv_aligned_preadv(
1688 req
->overlap_offset
+ req
->overlap_bytes
- align
,
1689 align
, align
, &local_qiov
, 0, 0);
1693 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1698 memset(pad
->buf
+ pad
->head
, 0, pad
->buf_len
- pad
->head
- pad
->tail
);
1704 static void bdrv_padding_destroy(BdrvRequestPadding
*pad
)
1707 qemu_vfree(pad
->buf
);
1708 qemu_iovec_destroy(&pad
->local_qiov
);
1710 memset(pad
, 0, sizeof(*pad
));
1716 * Exchange request parameters with padded request if needed. Don't include RMW
1717 * read of padding, bdrv_padding_rmw_read() should be called separately if
1720 * Request parameters (@qiov, &qiov_offset, &offset, &bytes) are in-out:
1721 * - on function start they represent original request
1722 * - on failure or when padding is not needed they are unchanged
1723 * - on success when padding is needed they represent padded request
1725 static int bdrv_pad_request(BlockDriverState
*bs
,
1726 QEMUIOVector
**qiov
, size_t *qiov_offset
,
1727 int64_t *offset
, int64_t *bytes
,
1728 BdrvRequestPadding
*pad
, bool *padded
,
1729 BdrvRequestFlags
*flags
)
1733 bdrv_check_qiov_request(*offset
, *bytes
, *qiov
, *qiov_offset
, &error_abort
);
1735 if (!bdrv_init_padding(bs
, *offset
, *bytes
, pad
)) {
1742 ret
= qemu_iovec_init_extended(&pad
->local_qiov
, pad
->buf
, pad
->head
,
1743 *qiov
, *qiov_offset
, *bytes
,
1744 pad
->buf
+ pad
->buf_len
- pad
->tail
,
1747 bdrv_padding_destroy(pad
);
1750 *bytes
+= pad
->head
+ pad
->tail
;
1751 *offset
-= pad
->head
;
1752 *qiov
= &pad
->local_qiov
;
1758 /* Can't use optimization hint with bounce buffer */
1759 *flags
&= ~BDRV_REQ_REGISTERED_BUF
;
1765 int coroutine_fn
bdrv_co_preadv(BdrvChild
*child
,
1766 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
1767 BdrvRequestFlags flags
)
1770 return bdrv_co_preadv_part(child
, offset
, bytes
, qiov
, 0, flags
);
1773 int coroutine_fn
bdrv_co_preadv_part(BdrvChild
*child
,
1774 int64_t offset
, int64_t bytes
,
1775 QEMUIOVector
*qiov
, size_t qiov_offset
,
1776 BdrvRequestFlags flags
)
1778 BlockDriverState
*bs
= child
->bs
;
1779 BdrvTrackedRequest req
;
1780 BdrvRequestPadding pad
;
1784 trace_bdrv_co_preadv_part(bs
, offset
, bytes
, flags
);
1786 if (!bdrv_is_inserted(bs
)) {
1790 ret
= bdrv_check_request32(offset
, bytes
, qiov
, qiov_offset
);
1795 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
1797 * Aligning zero request is nonsense. Even if driver has special meaning
1798 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
1799 * it to driver due to request_alignment.
1801 * Still, no reason to return an error if someone do unaligned
1802 * zero-length read occasionally.
1807 bdrv_inc_in_flight(bs
);
1809 /* Don't do copy-on-read if we read data before write operation */
1810 if (qatomic_read(&bs
->copy_on_read
)) {
1811 flags
|= BDRV_REQ_COPY_ON_READ
;
1814 ret
= bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
,
1820 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_READ
);
1821 ret
= bdrv_aligned_preadv(child
, &req
, offset
, bytes
,
1822 bs
->bl
.request_alignment
,
1823 qiov
, qiov_offset
, flags
);
1824 tracked_request_end(&req
);
1825 bdrv_padding_destroy(&pad
);
1828 bdrv_dec_in_flight(bs
);
1833 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
1834 int64_t offset
, int64_t bytes
, BdrvRequestFlags flags
)
1836 BlockDriver
*drv
= bs
->drv
;
1840 bool need_flush
= false;
1844 int64_t max_write_zeroes
= MIN_NON_ZERO(bs
->bl
.max_pwrite_zeroes
,
1846 int alignment
= MAX(bs
->bl
.pwrite_zeroes_alignment
,
1847 bs
->bl
.request_alignment
);
1848 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
, MAX_BOUNCE_BUFFER
);
1850 bdrv_check_request(offset
, bytes
, &error_abort
);
1856 if ((flags
& ~bs
->supported_zero_flags
) & BDRV_REQ_NO_FALLBACK
) {
1860 /* By definition there is no user buffer so this flag doesn't make sense */
1861 if (flags
& BDRV_REQ_REGISTERED_BUF
) {
1865 /* Invalidate the cached block-status data range if this write overlaps */
1866 bdrv_bsc_invalidate_range(bs
, offset
, bytes
);
1868 assert(alignment
% bs
->bl
.request_alignment
== 0);
1869 head
= offset
% alignment
;
1870 tail
= (offset
+ bytes
) % alignment
;
1871 max_write_zeroes
= QEMU_ALIGN_DOWN(max_write_zeroes
, alignment
);
1872 assert(max_write_zeroes
>= bs
->bl
.request_alignment
);
1874 while (bytes
> 0 && !ret
) {
1875 int64_t num
= bytes
;
1877 /* Align request. Block drivers can expect the "bulk" of the request
1878 * to be aligned, and that unaligned requests do not cross cluster
1882 /* Make a small request up to the first aligned sector. For
1883 * convenience, limit this request to max_transfer even if
1884 * we don't need to fall back to writes. */
1885 num
= MIN(MIN(bytes
, max_transfer
), alignment
- head
);
1886 head
= (head
+ num
) % alignment
;
1887 assert(num
< max_write_zeroes
);
1888 } else if (tail
&& num
> alignment
) {
1889 /* Shorten the request to the last aligned sector. */
1893 /* limit request size */
1894 if (num
> max_write_zeroes
) {
1895 num
= max_write_zeroes
;
1899 /* First try the efficient write zeroes operation */
1900 if (drv
->bdrv_co_pwrite_zeroes
) {
1901 ret
= drv
->bdrv_co_pwrite_zeroes(bs
, offset
, num
,
1902 flags
& bs
->supported_zero_flags
);
1903 if (ret
!= -ENOTSUP
&& (flags
& BDRV_REQ_FUA
) &&
1904 !(bs
->supported_zero_flags
& BDRV_REQ_FUA
)) {
1908 assert(!bs
->supported_zero_flags
);
1911 if (ret
== -ENOTSUP
&& !(flags
& BDRV_REQ_NO_FALLBACK
)) {
1912 /* Fall back to bounce buffer if write zeroes is unsupported */
1913 BdrvRequestFlags write_flags
= flags
& ~BDRV_REQ_ZERO_WRITE
;
1915 if ((flags
& BDRV_REQ_FUA
) &&
1916 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1917 /* No need for bdrv_driver_pwrite() to do a fallback
1918 * flush on each chunk; use just one at the end */
1919 write_flags
&= ~BDRV_REQ_FUA
;
1922 num
= MIN(num
, max_transfer
);
1924 buf
= qemu_try_blockalign0(bs
, num
);
1930 qemu_iovec_init_buf(&qiov
, buf
, num
);
1932 ret
= bdrv_driver_pwritev(bs
, offset
, num
, &qiov
, 0, write_flags
);
1934 /* Keep bounce buffer around if it is big enough for all
1935 * all future requests.
1937 if (num
< max_transfer
) {
1948 if (ret
== 0 && need_flush
) {
1949 ret
= bdrv_co_flush(bs
);
1955 static inline int coroutine_fn
1956 bdrv_co_write_req_prepare(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1957 BdrvTrackedRequest
*req
, int flags
)
1959 BlockDriverState
*bs
= child
->bs
;
1961 bdrv_check_request(offset
, bytes
, &error_abort
);
1963 if (bdrv_is_read_only(bs
)) {
1967 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
1968 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1969 assert(!(flags
& ~BDRV_REQ_MASK
));
1970 assert(!((flags
& BDRV_REQ_NO_WAIT
) && !(flags
& BDRV_REQ_SERIALISING
)));
1972 if (flags
& BDRV_REQ_SERIALISING
) {
1973 QEMU_LOCK_GUARD(&bs
->reqs_lock
);
1975 tracked_request_set_serialising(req
, bdrv_get_cluster_size(bs
));
1977 if ((flags
& BDRV_REQ_NO_WAIT
) && bdrv_find_conflicting_request(req
)) {
1981 bdrv_wait_serialising_requests_locked(req
);
1983 bdrv_wait_serialising_requests(req
);
1986 assert(req
->overlap_offset
<= offset
);
1987 assert(offset
+ bytes
<= req
->overlap_offset
+ req
->overlap_bytes
);
1988 assert(offset
+ bytes
<= bs
->total_sectors
* BDRV_SECTOR_SIZE
||
1989 child
->perm
& BLK_PERM_RESIZE
);
1991 switch (req
->type
) {
1992 case BDRV_TRACKED_WRITE
:
1993 case BDRV_TRACKED_DISCARD
:
1994 if (flags
& BDRV_REQ_WRITE_UNCHANGED
) {
1995 assert(child
->perm
& (BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_WRITE
));
1997 assert(child
->perm
& BLK_PERM_WRITE
);
1999 bdrv_write_threshold_check_write(bs
, offset
, bytes
);
2001 case BDRV_TRACKED_TRUNCATE
:
2002 assert(child
->perm
& BLK_PERM_RESIZE
);
2009 static inline void coroutine_fn
2010 bdrv_co_write_req_finish(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
2011 BdrvTrackedRequest
*req
, int ret
)
2013 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
2014 BlockDriverState
*bs
= child
->bs
;
2016 bdrv_check_request(offset
, bytes
, &error_abort
);
2018 qatomic_inc(&bs
->write_gen
);
2021 * Discard cannot extend the image, but in error handling cases, such as
2022 * when reverting a qcow2 cluster allocation, the discarded range can pass
2023 * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
2024 * here. Instead, just skip it, since semantically a discard request
2025 * beyond EOF cannot expand the image anyway.
2028 (req
->type
== BDRV_TRACKED_TRUNCATE
||
2029 end_sector
> bs
->total_sectors
) &&
2030 req
->type
!= BDRV_TRACKED_DISCARD
) {
2031 bs
->total_sectors
= end_sector
;
2032 bdrv_parent_cb_resize(bs
);
2033 bdrv_dirty_bitmap_truncate(bs
, end_sector
<< BDRV_SECTOR_BITS
);
2036 switch (req
->type
) {
2037 case BDRV_TRACKED_WRITE
:
2038 stat64_max(&bs
->wr_highest_offset
, offset
+ bytes
);
2039 /* fall through, to set dirty bits */
2040 case BDRV_TRACKED_DISCARD
:
2041 bdrv_set_dirty(bs
, offset
, bytes
);
2050 * Forwards an already correctly aligned write request to the BlockDriver,
2051 * after possibly fragmenting it.
2053 static int coroutine_fn
bdrv_aligned_pwritev(BdrvChild
*child
,
2054 BdrvTrackedRequest
*req
, int64_t offset
, int64_t bytes
,
2055 int64_t align
, QEMUIOVector
*qiov
, size_t qiov_offset
,
2056 BdrvRequestFlags flags
)
2058 BlockDriverState
*bs
= child
->bs
;
2059 BlockDriver
*drv
= bs
->drv
;
2062 int64_t bytes_remaining
= bytes
;
2065 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
2071 if (bdrv_has_readonly_bitmaps(bs
)) {
2075 assert(is_power_of_2(align
));
2076 assert((offset
& (align
- 1)) == 0);
2077 assert((bytes
& (align
- 1)) == 0);
2078 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
2081 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, req
, flags
);
2083 if (!ret
&& bs
->detect_zeroes
!= BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
&&
2084 !(flags
& BDRV_REQ_ZERO_WRITE
) && drv
->bdrv_co_pwrite_zeroes
&&
2085 qemu_iovec_is_zero(qiov
, qiov_offset
, bytes
)) {
2086 flags
|= BDRV_REQ_ZERO_WRITE
;
2087 if (bs
->detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
) {
2088 flags
|= BDRV_REQ_MAY_UNMAP
;
2093 /* Do nothing, write notifier decided to fail this request */
2094 } else if (flags
& BDRV_REQ_ZERO_WRITE
) {
2095 bdrv_debug_event(bs
, BLKDBG_PWRITEV_ZERO
);
2096 ret
= bdrv_co_do_pwrite_zeroes(bs
, offset
, bytes
, flags
);
2097 } else if (flags
& BDRV_REQ_WRITE_COMPRESSED
) {
2098 ret
= bdrv_driver_pwritev_compressed(bs
, offset
, bytes
,
2100 } else if (bytes
<= max_transfer
) {
2101 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
2102 ret
= bdrv_driver_pwritev(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
2104 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
2105 while (bytes_remaining
) {
2106 int num
= MIN(bytes_remaining
, max_transfer
);
2107 int local_flags
= flags
;
2110 if (num
< bytes_remaining
&& (flags
& BDRV_REQ_FUA
) &&
2111 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
2112 /* If FUA is going to be emulated by flush, we only
2113 * need to flush on the last iteration */
2114 local_flags
&= ~BDRV_REQ_FUA
;
2117 ret
= bdrv_driver_pwritev(bs
, offset
+ bytes
- bytes_remaining
,
2119 qiov_offset
+ bytes
- bytes_remaining
,
2124 bytes_remaining
-= num
;
2127 bdrv_debug_event(bs
, BLKDBG_PWRITEV_DONE
);
2132 bdrv_co_write_req_finish(child
, offset
, bytes
, req
, ret
);
2137 static int coroutine_fn
bdrv_co_do_zero_pwritev(BdrvChild
*child
,
2140 BdrvRequestFlags flags
,
2141 BdrvTrackedRequest
*req
)
2143 BlockDriverState
*bs
= child
->bs
;
2144 QEMUIOVector local_qiov
;
2145 uint64_t align
= bs
->bl
.request_alignment
;
2148 BdrvRequestPadding pad
;
2150 /* This flag doesn't make sense for padding or zero writes */
2151 flags
&= ~BDRV_REQ_REGISTERED_BUF
;
2153 padding
= bdrv_init_padding(bs
, offset
, bytes
, &pad
);
2155 assert(!(flags
& BDRV_REQ_NO_WAIT
));
2156 bdrv_make_request_serialising(req
, align
);
2158 bdrv_padding_rmw_read(child
, req
, &pad
, true);
2160 if (pad
.head
|| pad
.merge_reads
) {
2161 int64_t aligned_offset
= offset
& ~(align
- 1);
2162 int64_t write_bytes
= pad
.merge_reads
? pad
.buf_len
: align
;
2164 qemu_iovec_init_buf(&local_qiov
, pad
.buf
, write_bytes
);
2165 ret
= bdrv_aligned_pwritev(child
, req
, aligned_offset
, write_bytes
,
2166 align
, &local_qiov
, 0,
2167 flags
& ~BDRV_REQ_ZERO_WRITE
);
2168 if (ret
< 0 || pad
.merge_reads
) {
2169 /* Error or all work is done */
2172 offset
+= write_bytes
- pad
.head
;
2173 bytes
-= write_bytes
- pad
.head
;
2177 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2178 if (bytes
>= align
) {
2179 /* Write the aligned part in the middle. */
2180 int64_t aligned_bytes
= bytes
& ~(align
- 1);
2181 ret
= bdrv_aligned_pwritev(child
, req
, offset
, aligned_bytes
, align
,
2186 bytes
-= aligned_bytes
;
2187 offset
+= aligned_bytes
;
2190 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2192 assert(align
== pad
.tail
+ bytes
);
2194 qemu_iovec_init_buf(&local_qiov
, pad
.tail_buf
, align
);
2195 ret
= bdrv_aligned_pwritev(child
, req
, offset
, align
, align
,
2197 flags
& ~BDRV_REQ_ZERO_WRITE
);
2201 bdrv_padding_destroy(&pad
);
2207 * Handle a write request in coroutine context
2209 int coroutine_fn
bdrv_co_pwritev(BdrvChild
*child
,
2210 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
2211 BdrvRequestFlags flags
)
2214 return bdrv_co_pwritev_part(child
, offset
, bytes
, qiov
, 0, flags
);
2217 int coroutine_fn
bdrv_co_pwritev_part(BdrvChild
*child
,
2218 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
, size_t qiov_offset
,
2219 BdrvRequestFlags flags
)
2221 BlockDriverState
*bs
= child
->bs
;
2222 BdrvTrackedRequest req
;
2223 uint64_t align
= bs
->bl
.request_alignment
;
2224 BdrvRequestPadding pad
;
2226 bool padded
= false;
2229 trace_bdrv_co_pwritev_part(child
->bs
, offset
, bytes
, flags
);
2231 if (!bdrv_is_inserted(bs
)) {
2235 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2236 ret
= bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, NULL
);
2238 ret
= bdrv_check_request32(offset
, bytes
, qiov
, qiov_offset
);
2244 /* If the request is misaligned then we can't make it efficient */
2245 if ((flags
& BDRV_REQ_NO_FALLBACK
) &&
2246 !QEMU_IS_ALIGNED(offset
| bytes
, align
))
2251 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
2253 * Aligning zero request is nonsense. Even if driver has special meaning
2254 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
2255 * it to driver due to request_alignment.
2257 * Still, no reason to return an error if someone do unaligned
2258 * zero-length write occasionally.
2263 if (!(flags
& BDRV_REQ_ZERO_WRITE
)) {
2265 * Pad request for following read-modify-write cycle.
2266 * bdrv_co_do_zero_pwritev() does aligning by itself, so, we do
2267 * alignment only if there is no ZERO flag.
2269 ret
= bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
,
2276 bdrv_inc_in_flight(bs
);
2277 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_WRITE
);
2279 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2281 ret
= bdrv_co_do_zero_pwritev(child
, offset
, bytes
, flags
, &req
);
2287 * Request was unaligned to request_alignment and therefore
2288 * padded. We are going to do read-modify-write, and must
2289 * serialize the request to prevent interactions of the
2290 * widened region with other transactions.
2292 assert(!(flags
& BDRV_REQ_NO_WAIT
));
2293 bdrv_make_request_serialising(&req
, align
);
2294 bdrv_padding_rmw_read(child
, &req
, &pad
, false);
2297 ret
= bdrv_aligned_pwritev(child
, &req
, offset
, bytes
, align
,
2298 qiov
, qiov_offset
, flags
);
2300 bdrv_padding_destroy(&pad
);
2303 tracked_request_end(&req
);
2304 bdrv_dec_in_flight(bs
);
2309 int coroutine_fn
bdrv_co_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
2310 int64_t bytes
, BdrvRequestFlags flags
)
2313 trace_bdrv_co_pwrite_zeroes(child
->bs
, offset
, bytes
, flags
);
2315 if (!(child
->bs
->open_flags
& BDRV_O_UNMAP
)) {
2316 flags
&= ~BDRV_REQ_MAY_UNMAP
;
2319 return bdrv_co_pwritev(child
, offset
, bytes
, NULL
,
2320 BDRV_REQ_ZERO_WRITE
| flags
);
2324 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
2326 int bdrv_flush_all(void)
2328 BdrvNextIterator it
;
2329 BlockDriverState
*bs
= NULL
;
2332 GLOBAL_STATE_CODE();
2335 * bdrv queue is managed by record/replay,
2336 * creating new flush request for stopping
2337 * the VM may break the determinism
2339 if (replay_events_enabled()) {
2343 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2344 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
2347 aio_context_acquire(aio_context
);
2348 ret
= bdrv_flush(bs
);
2349 if (ret
< 0 && !result
) {
2352 aio_context_release(aio_context
);
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.
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.
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.
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.
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.
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.
2385 static int coroutine_fn
bdrv_co_block_status(BlockDriverState
*bs
,
2387 int64_t offset
, int64_t bytes
,
2388 int64_t *pnum
, int64_t *map
,
2389 BlockDriverState
**file
)
2392 int64_t n
; /* bytes */
2394 int64_t local_map
= 0;
2395 BlockDriverState
*local_file
= NULL
;
2396 int64_t aligned_offset
, aligned_bytes
;
2398 bool has_filtered_child
;
2402 total_size
= bdrv_getlength(bs
);
2403 if (total_size
< 0) {
2408 if (offset
>= total_size
) {
2409 ret
= BDRV_BLOCK_EOF
;
2417 n
= total_size
- offset
;
2422 /* Must be non-NULL or bdrv_getlength() would have failed */
2424 has_filtered_child
= bdrv_filter_child(bs
);
2425 if (!bs
->drv
->bdrv_co_block_status
&& !has_filtered_child
) {
2427 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
;
2428 if (offset
+ bytes
== total_size
) {
2429 ret
|= BDRV_BLOCK_EOF
;
2431 if (bs
->drv
->protocol_name
) {
2432 ret
|= BDRV_BLOCK_OFFSET_VALID
;
2439 bdrv_inc_in_flight(bs
);
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
;
2446 if (bs
->drv
->bdrv_co_block_status
) {
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.
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.
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.
2468 if (QLIST_EMPTY(&bs
->children
) &&
2469 bdrv_bsc_is_data(bs
, aligned_offset
, pnum
))
2471 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
2473 local_map
= aligned_offset
;
2475 ret
= bs
->drv
->bdrv_co_block_status(bs
, want_zero
, aligned_offset
,
2476 aligned_bytes
, pnum
, &local_map
,
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.
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.
2491 ret
== (BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
) &&
2492 QLIST_EMPTY(&bs
->children
))
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
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
2505 assert(local_file
== bs
);
2506 assert(local_map
== aligned_offset
);
2507 bdrv_bsc_fill(bs
, aligned_offset
, *pnum
);
2511 /* Default code for filters */
2513 local_file
= bdrv_filter_bs(bs
);
2516 *pnum
= aligned_bytes
;
2517 local_map
= aligned_offset
;
2518 ret
= BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
;
2526 * The driver's result must be a non-zero multiple of request_alignment.
2527 * Clamp pnum and adjust map to original request.
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
));
2537 *pnum
-= offset
- aligned_offset
;
2538 if (*pnum
> bytes
) {
2541 if (ret
& BDRV_BLOCK_OFFSET_VALID
) {
2542 local_map
+= offset
- aligned_offset
;
2545 if (ret
& BDRV_BLOCK_RAW
) {
2546 assert(ret
& BDRV_BLOCK_OFFSET_VALID
&& local_file
);
2547 ret
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2548 *pnum
, pnum
, &local_map
, &local_file
);
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
);
2558 ret
|= BDRV_BLOCK_ZERO
;
2559 } else if (want_zero
) {
2560 int64_t size2
= bdrv_getlength(cow_bs
);
2562 if (size2
>= 0 && offset
>= size2
) {
2563 ret
|= BDRV_BLOCK_ZERO
;
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
)) {
2575 ret2
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2576 *pnum
, &file_pnum
, NULL
, NULL
);
2578 /* Ignore errors. This is just providing extra information, it
2579 * is useful but not necessary.
2581 if (ret2
& BDRV_BLOCK_EOF
&&
2582 (!file_pnum
|| ret2
& BDRV_BLOCK_ZERO
)) {
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.
2588 ret
|= BDRV_BLOCK_ZERO
;
2590 /* Limit request to the range reported by the protocol driver */
2592 ret
|= (ret2
& BDRV_BLOCK_ZERO
);
2598 bdrv_dec_in_flight(bs
);
2599 if (ret
>= 0 && offset
+ *pnum
== total_size
) {
2600 ret
|= BDRV_BLOCK_EOF
;
2613 bdrv_co_common_block_status_above(BlockDriverState
*bs
,
2614 BlockDriverState
*base
,
2621 BlockDriverState
**file
,
2625 BlockDriverState
*p
;
2630 assert(!include_base
|| base
); /* Can't include NULL base */
2637 if (!include_base
&& bs
== base
) {
2642 ret
= bdrv_co_block_status(bs
, want_zero
, offset
, bytes
, pnum
, map
, file
);
2644 if (ret
< 0 || *pnum
== 0 || ret
& BDRV_BLOCK_ALLOCATED
|| bs
== base
) {
2648 if (ret
& BDRV_BLOCK_EOF
) {
2649 eof
= offset
+ *pnum
;
2652 assert(*pnum
<= bytes
);
2655 for (p
= bdrv_filter_or_cow_bs(bs
); include_base
|| p
!= base
;
2656 p
= bdrv_filter_or_cow_bs(p
))
2658 ret
= bdrv_co_block_status(p
, want_zero
, offset
, bytes
, pnum
, map
,
2666 * The top layer deferred to this layer, and because this layer is
2667 * short, any zeroes that we synthesize beyond EOF behave as if they
2668 * were allocated at this layer.
2670 * We don't include BDRV_BLOCK_EOF into ret, as upper layer may be
2671 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2674 assert(ret
& BDRV_BLOCK_EOF
);
2679 ret
= BDRV_BLOCK_ZERO
| BDRV_BLOCK_ALLOCATED
;
2682 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2684 * We've found the node and the status, we must break.
2686 * Drop BDRV_BLOCK_EOF, as it's not for upper layer, which may be
2687 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2690 ret
&= ~BDRV_BLOCK_EOF
;
2695 assert(include_base
);
2700 * OK, [offset, offset + *pnum) region is unallocated on this layer,
2701 * let's continue the diving.
2703 assert(*pnum
<= bytes
);
2707 if (offset
+ *pnum
== eof
) {
2708 ret
|= BDRV_BLOCK_EOF
;
2714 int bdrv_block_status_above(BlockDriverState
*bs
, BlockDriverState
*base
,
2715 int64_t offset
, int64_t bytes
, int64_t *pnum
,
2716 int64_t *map
, BlockDriverState
**file
)
2719 return bdrv_common_block_status_above(bs
, base
, false, true, offset
, bytes
,
2720 pnum
, map
, file
, NULL
);
2723 int bdrv_block_status(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2724 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
2727 return bdrv_block_status_above(bs
, bdrv_filter_or_cow_bs(bs
),
2728 offset
, bytes
, pnum
, map
, file
);
2732 * Check @bs (and its backing chain) to see if the range defined
2733 * by @offset and @bytes is known to read as zeroes.
2734 * Return 1 if that is the case, 0 otherwise and -errno on error.
2735 * This test is meant to be fast rather than accurate so returning 0
2736 * does not guarantee non-zero data.
2738 int coroutine_fn
bdrv_co_is_zero_fast(BlockDriverState
*bs
, int64_t offset
,
2742 int64_t pnum
= bytes
;
2749 ret
= bdrv_co_common_block_status_above(bs
, NULL
, false, false, offset
,
2750 bytes
, &pnum
, NULL
, NULL
, NULL
);
2756 return (pnum
== bytes
) && (ret
& BDRV_BLOCK_ZERO
);
2759 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2766 ret
= bdrv_common_block_status_above(bs
, bs
, true, false, offset
,
2767 bytes
, pnum
? pnum
: &dummy
, NULL
,
2772 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
2776 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2778 * Return a positive depth if (a prefix of) the given range is allocated
2779 * in any image between BASE and TOP (BASE is only included if include_base
2780 * is set). Depth 1 is TOP, 2 is the first backing layer, and so forth.
2781 * BASE can be NULL to check if the given offset is allocated in any
2782 * image of the chain. Return 0 otherwise, or negative errno on
2785 * 'pnum' is set to the number of bytes (including and immediately
2786 * following the specified offset) that are known to be in the same
2787 * allocated/unallocated state. Note that a subsequent call starting
2788 * at 'offset + *pnum' may return the same allocation status (in other
2789 * words, the result is not necessarily the maximum possible range);
2790 * but 'pnum' will only be 0 when end of file is reached.
2792 int bdrv_is_allocated_above(BlockDriverState
*top
,
2793 BlockDriverState
*base
,
2794 bool include_base
, int64_t offset
,
2795 int64_t bytes
, int64_t *pnum
)
2798 int ret
= bdrv_common_block_status_above(top
, base
, include_base
, false,
2799 offset
, bytes
, pnum
, NULL
, NULL
,
2806 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2813 bdrv_co_readv_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2815 BlockDriver
*drv
= bs
->drv
;
2816 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2820 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
2829 bdrv_inc_in_flight(bs
);
2831 if (drv
->bdrv_load_vmstate
) {
2832 ret
= drv
->bdrv_load_vmstate(bs
, qiov
, pos
);
2833 } else if (child_bs
) {
2834 ret
= bdrv_co_readv_vmstate(child_bs
, qiov
, pos
);
2839 bdrv_dec_in_flight(bs
);
2845 bdrv_co_writev_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2847 BlockDriver
*drv
= bs
->drv
;
2848 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2852 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
2861 bdrv_inc_in_flight(bs
);
2863 if (drv
->bdrv_save_vmstate
) {
2864 ret
= drv
->bdrv_save_vmstate(bs
, qiov
, pos
);
2865 } else if (child_bs
) {
2866 ret
= bdrv_co_writev_vmstate(child_bs
, qiov
, pos
);
2871 bdrv_dec_in_flight(bs
);
2876 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2877 int64_t pos
, int size
)
2879 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2880 int ret
= bdrv_writev_vmstate(bs
, &qiov
, pos
);
2883 return ret
< 0 ? ret
: size
;
2886 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2887 int64_t pos
, int size
)
2889 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2890 int ret
= bdrv_readv_vmstate(bs
, &qiov
, pos
);
2893 return ret
< 0 ? ret
: size
;
2896 /**************************************************************/
2899 void bdrv_aio_cancel(BlockAIOCB
*acb
)
2903 bdrv_aio_cancel_async(acb
);
2904 while (acb
->refcnt
> 1) {
2905 if (acb
->aiocb_info
->get_aio_context
) {
2906 aio_poll(acb
->aiocb_info
->get_aio_context(acb
), true);
2907 } else if (acb
->bs
) {
2908 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2909 * assert that we're not using an I/O thread. Thread-safe
2910 * code should use bdrv_aio_cancel_async exclusively.
2912 assert(bdrv_get_aio_context(acb
->bs
) == qemu_get_aio_context());
2913 aio_poll(bdrv_get_aio_context(acb
->bs
), true);
2918 qemu_aio_unref(acb
);
2921 /* Async version of aio cancel. The caller is not blocked if the acb implements
2922 * cancel_async, otherwise we do nothing and let the request normally complete.
2923 * In either case the completion callback must be called. */
2924 void bdrv_aio_cancel_async(BlockAIOCB
*acb
)
2927 if (acb
->aiocb_info
->cancel_async
) {
2928 acb
->aiocb_info
->cancel_async(acb
);
2932 /**************************************************************/
2933 /* Coroutine block device emulation */
2935 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2937 BdrvChild
*primary_child
= bdrv_primary_child(bs
);
2943 bdrv_inc_in_flight(bs
);
2945 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
) ||
2950 qemu_co_mutex_lock(&bs
->reqs_lock
);
2951 current_gen
= qatomic_read(&bs
->write_gen
);
2953 /* Wait until any previous flushes are completed */
2954 while (bs
->active_flush_req
) {
2955 qemu_co_queue_wait(&bs
->flush_queue
, &bs
->reqs_lock
);
2958 /* Flushes reach this point in nondecreasing current_gen order. */
2959 bs
->active_flush_req
= true;
2960 qemu_co_mutex_unlock(&bs
->reqs_lock
);
2962 /* Write back all layers by calling one driver function */
2963 if (bs
->drv
->bdrv_co_flush
) {
2964 ret
= bs
->drv
->bdrv_co_flush(bs
);
2968 /* Write back cached data to the OS even with cache=unsafe */
2969 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_OS
);
2970 if (bs
->drv
->bdrv_co_flush_to_os
) {
2971 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
2977 /* But don't actually force it to the disk with cache=unsafe */
2978 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2979 goto flush_children
;
2982 /* Check if we really need to flush anything */
2983 if (bs
->flushed_gen
== current_gen
) {
2984 goto flush_children
;
2987 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_DISK
);
2989 /* bs->drv->bdrv_co_flush() might have ejected the BDS
2990 * (even in case of apparent success) */
2994 if (bs
->drv
->bdrv_co_flush_to_disk
) {
2995 ret
= bs
->drv
->bdrv_co_flush_to_disk(bs
);
2996 } else if (bs
->drv
->bdrv_aio_flush
) {
2998 CoroutineIOCompletion co
= {
2999 .coroutine
= qemu_coroutine_self(),
3002 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
3006 qemu_coroutine_yield();
3011 * Some block drivers always operate in either writethrough or unsafe
3012 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
3013 * know how the server works (because the behaviour is hardcoded or
3014 * depends on server-side configuration), so we can't ensure that
3015 * everything is safe on disk. Returning an error doesn't work because
3016 * that would break guests even if the server operates in writethrough
3019 * Let's hope the user knows what he's doing.
3028 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
3029 * in the case of cache=unsafe, so there are no useless flushes.
3033 QLIST_FOREACH(child
, &bs
->children
, next
) {
3034 if (child
->perm
& (BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
)) {
3035 int this_child_ret
= bdrv_co_flush(child
->bs
);
3037 ret
= this_child_ret
;
3043 /* Notify any pending flushes that we have completed */
3045 bs
->flushed_gen
= current_gen
;
3048 qemu_co_mutex_lock(&bs
->reqs_lock
);
3049 bs
->active_flush_req
= false;
3050 /* Return value is ignored - it's ok if wait queue is empty */
3051 qemu_co_queue_next(&bs
->flush_queue
);
3052 qemu_co_mutex_unlock(&bs
->reqs_lock
);
3055 bdrv_dec_in_flight(bs
);
3059 int coroutine_fn
bdrv_co_pdiscard(BdrvChild
*child
, int64_t offset
,
3062 BdrvTrackedRequest req
;
3064 int64_t max_pdiscard
;
3065 int head
, tail
, align
;
3066 BlockDriverState
*bs
= child
->bs
;
3069 if (!bs
|| !bs
->drv
|| !bdrv_is_inserted(bs
)) {
3073 if (bdrv_has_readonly_bitmaps(bs
)) {
3077 ret
= bdrv_check_request(offset
, bytes
, NULL
);
3082 /* Do nothing if disabled. */
3083 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
3087 if (!bs
->drv
->bdrv_co_pdiscard
&& !bs
->drv
->bdrv_aio_pdiscard
) {
3091 /* Invalidate the cached block-status data range if this discard overlaps */
3092 bdrv_bsc_invalidate_range(bs
, offset
, bytes
);
3094 /* Discard is advisory, but some devices track and coalesce
3095 * unaligned requests, so we must pass everything down rather than
3096 * round here. Still, most devices will just silently ignore
3097 * unaligned requests (by returning -ENOTSUP), so we must fragment
3098 * the request accordingly. */
3099 align
= MAX(bs
->bl
.pdiscard_alignment
, bs
->bl
.request_alignment
);
3100 assert(align
% bs
->bl
.request_alignment
== 0);
3101 head
= offset
% align
;
3102 tail
= (offset
+ bytes
) % align
;
3104 bdrv_inc_in_flight(bs
);
3105 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_DISCARD
);
3107 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, &req
, 0);
3112 max_pdiscard
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_pdiscard
, INT64_MAX
),
3114 assert(max_pdiscard
>= bs
->bl
.request_alignment
);
3117 int64_t num
= bytes
;
3120 /* Make small requests to get to alignment boundaries. */
3121 num
= MIN(bytes
, align
- head
);
3122 if (!QEMU_IS_ALIGNED(num
, bs
->bl
.request_alignment
)) {
3123 num
%= bs
->bl
.request_alignment
;
3125 head
= (head
+ num
) % align
;
3126 assert(num
< max_pdiscard
);
3129 /* Shorten the request to the last aligned cluster. */
3131 } else if (!QEMU_IS_ALIGNED(tail
, bs
->bl
.request_alignment
) &&
3132 tail
> bs
->bl
.request_alignment
) {
3133 tail
%= bs
->bl
.request_alignment
;
3137 /* limit request size */
3138 if (num
> max_pdiscard
) {
3146 if (bs
->drv
->bdrv_co_pdiscard
) {
3147 ret
= bs
->drv
->bdrv_co_pdiscard(bs
, offset
, num
);
3150 CoroutineIOCompletion co
= {
3151 .coroutine
= qemu_coroutine_self(),
3154 acb
= bs
->drv
->bdrv_aio_pdiscard(bs
, offset
, num
,
3155 bdrv_co_io_em_complete
, &co
);
3160 qemu_coroutine_yield();
3164 if (ret
&& ret
!= -ENOTSUP
) {
3173 bdrv_co_write_req_finish(child
, req
.offset
, req
.bytes
, &req
, ret
);
3174 tracked_request_end(&req
);
3175 bdrv_dec_in_flight(bs
);
3179 int coroutine_fn
bdrv_co_ioctl(BlockDriverState
*bs
, int req
, void *buf
)
3181 BlockDriver
*drv
= bs
->drv
;
3182 CoroutineIOCompletion co
= {
3183 .coroutine
= qemu_coroutine_self(),
3188 bdrv_inc_in_flight(bs
);
3189 if (!drv
|| (!drv
->bdrv_aio_ioctl
&& !drv
->bdrv_co_ioctl
)) {
3194 if (drv
->bdrv_co_ioctl
) {
3195 co
.ret
= drv
->bdrv_co_ioctl(bs
, req
, buf
);
3197 acb
= drv
->bdrv_aio_ioctl(bs
, req
, buf
, bdrv_co_io_em_complete
, &co
);
3202 qemu_coroutine_yield();
3205 bdrv_dec_in_flight(bs
);
3209 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
3212 return qemu_memalign(bdrv_opt_mem_align(bs
), size
);
3215 void *qemu_blockalign0(BlockDriverState
*bs
, size_t size
)
3218 return memset(qemu_blockalign(bs
, size
), 0, size
);
3221 void *qemu_try_blockalign(BlockDriverState
*bs
, size_t size
)
3223 size_t align
= bdrv_opt_mem_align(bs
);
3226 /* Ensure that NULL is never returned on success */
3232 return qemu_try_memalign(align
, size
);
3235 void *qemu_try_blockalign0(BlockDriverState
*bs
, size_t size
)
3237 void *mem
= qemu_try_blockalign(bs
, size
);
3241 memset(mem
, 0, size
);
3247 void bdrv_io_plug(BlockDriverState
*bs
)
3252 QLIST_FOREACH(child
, &bs
->children
, next
) {
3253 bdrv_io_plug(child
->bs
);
3256 if (qatomic_fetch_inc(&bs
->io_plugged
) == 0) {
3257 BlockDriver
*drv
= bs
->drv
;
3258 if (drv
&& drv
->bdrv_io_plug
) {
3259 drv
->bdrv_io_plug(bs
);
3264 void bdrv_io_unplug(BlockDriverState
*bs
)
3269 assert(bs
->io_plugged
);
3270 if (qatomic_fetch_dec(&bs
->io_plugged
) == 1) {
3271 BlockDriver
*drv
= bs
->drv
;
3272 if (drv
&& drv
->bdrv_io_unplug
) {
3273 drv
->bdrv_io_unplug(bs
);
3277 QLIST_FOREACH(child
, &bs
->children
, next
) {
3278 bdrv_io_unplug(child
->bs
);
3282 /* Helper that undoes bdrv_register_buf() when it fails partway through */
3283 static void bdrv_register_buf_rollback(BlockDriverState
*bs
,
3286 BdrvChild
*final_child
)
3290 QLIST_FOREACH(child
, &bs
->children
, next
) {
3291 if (child
== final_child
) {
3295 bdrv_unregister_buf(child
->bs
, host
, size
);
3298 if (bs
->drv
&& bs
->drv
->bdrv_unregister_buf
) {
3299 bs
->drv
->bdrv_unregister_buf(bs
, host
, size
);
3303 bool bdrv_register_buf(BlockDriverState
*bs
, void *host
, size_t size
,
3308 GLOBAL_STATE_CODE();
3309 if (bs
->drv
&& bs
->drv
->bdrv_register_buf
) {
3310 if (!bs
->drv
->bdrv_register_buf(bs
, host
, size
, errp
)) {
3314 QLIST_FOREACH(child
, &bs
->children
, next
) {
3315 if (!bdrv_register_buf(child
->bs
, host
, size
, errp
)) {
3316 bdrv_register_buf_rollback(bs
, host
, size
, child
);
3323 void bdrv_unregister_buf(BlockDriverState
*bs
, void *host
, size_t size
)
3327 GLOBAL_STATE_CODE();
3328 if (bs
->drv
&& bs
->drv
->bdrv_unregister_buf
) {
3329 bs
->drv
->bdrv_unregister_buf(bs
, host
, size
);
3331 QLIST_FOREACH(child
, &bs
->children
, next
) {
3332 bdrv_unregister_buf(child
->bs
, host
, size
);
3336 static int coroutine_fn
bdrv_co_copy_range_internal(
3337 BdrvChild
*src
, int64_t src_offset
, BdrvChild
*dst
,
3338 int64_t dst_offset
, int64_t bytes
,
3339 BdrvRequestFlags read_flags
, BdrvRequestFlags write_flags
,
3342 BdrvTrackedRequest req
;
3345 /* TODO We can support BDRV_REQ_NO_FALLBACK here */
3346 assert(!(read_flags
& BDRV_REQ_NO_FALLBACK
));
3347 assert(!(write_flags
& BDRV_REQ_NO_FALLBACK
));
3348 assert(!(read_flags
& BDRV_REQ_NO_WAIT
));
3349 assert(!(write_flags
& BDRV_REQ_NO_WAIT
));
3351 if (!dst
|| !dst
->bs
|| !bdrv_is_inserted(dst
->bs
)) {
3354 ret
= bdrv_check_request32(dst_offset
, bytes
, NULL
, 0);
3358 if (write_flags
& BDRV_REQ_ZERO_WRITE
) {
3359 return bdrv_co_pwrite_zeroes(dst
, dst_offset
, bytes
, write_flags
);
3362 if (!src
|| !src
->bs
|| !bdrv_is_inserted(src
->bs
)) {
3365 ret
= bdrv_check_request32(src_offset
, bytes
, NULL
, 0);
3370 if (!src
->bs
->drv
->bdrv_co_copy_range_from
3371 || !dst
->bs
->drv
->bdrv_co_copy_range_to
3372 || src
->bs
->encrypted
|| dst
->bs
->encrypted
) {
3377 bdrv_inc_in_flight(src
->bs
);
3378 tracked_request_begin(&req
, src
->bs
, src_offset
, bytes
,
3381 /* BDRV_REQ_SERIALISING is only for write operation */
3382 assert(!(read_flags
& BDRV_REQ_SERIALISING
));
3383 bdrv_wait_serialising_requests(&req
);
3385 ret
= src
->bs
->drv
->bdrv_co_copy_range_from(src
->bs
,
3389 read_flags
, write_flags
);
3391 tracked_request_end(&req
);
3392 bdrv_dec_in_flight(src
->bs
);
3394 bdrv_inc_in_flight(dst
->bs
);
3395 tracked_request_begin(&req
, dst
->bs
, dst_offset
, bytes
,
3396 BDRV_TRACKED_WRITE
);
3397 ret
= bdrv_co_write_req_prepare(dst
, dst_offset
, bytes
, &req
,
3400 ret
= dst
->bs
->drv
->bdrv_co_copy_range_to(dst
->bs
,
3404 read_flags
, write_flags
);
3406 bdrv_co_write_req_finish(dst
, dst_offset
, bytes
, &req
, ret
);
3407 tracked_request_end(&req
);
3408 bdrv_dec_in_flight(dst
->bs
);
3414 /* Copy range from @src to @dst.
3416 * See the comment of bdrv_co_copy_range for the parameter and return value
3418 int coroutine_fn
bdrv_co_copy_range_from(BdrvChild
*src
, int64_t src_offset
,
3419 BdrvChild
*dst
, int64_t dst_offset
,
3421 BdrvRequestFlags read_flags
,
3422 BdrvRequestFlags write_flags
)
3425 trace_bdrv_co_copy_range_from(src
, src_offset
, dst
, dst_offset
, bytes
,
3426 read_flags
, write_flags
);
3427 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3428 bytes
, read_flags
, write_flags
, true);
3431 /* Copy range from @src to @dst.
3433 * See the comment of bdrv_co_copy_range for the parameter and return value
3435 int coroutine_fn
bdrv_co_copy_range_to(BdrvChild
*src
, int64_t src_offset
,
3436 BdrvChild
*dst
, int64_t dst_offset
,
3438 BdrvRequestFlags read_flags
,
3439 BdrvRequestFlags write_flags
)
3442 trace_bdrv_co_copy_range_to(src
, src_offset
, dst
, dst_offset
, bytes
,
3443 read_flags
, write_flags
);
3444 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3445 bytes
, read_flags
, write_flags
, false);
3448 int coroutine_fn
bdrv_co_copy_range(BdrvChild
*src
, int64_t src_offset
,
3449 BdrvChild
*dst
, int64_t dst_offset
,
3450 int64_t bytes
, BdrvRequestFlags read_flags
,
3451 BdrvRequestFlags write_flags
)
3454 return bdrv_co_copy_range_from(src
, src_offset
,
3456 bytes
, read_flags
, write_flags
);
3459 static void bdrv_parent_cb_resize(BlockDriverState
*bs
)
3462 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
3463 if (c
->klass
->resize
) {
3464 c
->klass
->resize(c
);
3470 * Truncate file to 'offset' bytes (needed only for file protocols)
3472 * If 'exact' is true, the file must be resized to exactly the given
3473 * 'offset'. Otherwise, it is sufficient for the node to be at least
3474 * 'offset' bytes in length.
3476 int coroutine_fn
bdrv_co_truncate(BdrvChild
*child
, int64_t offset
, bool exact
,
3477 PreallocMode prealloc
, BdrvRequestFlags flags
,
3480 BlockDriverState
*bs
= child
->bs
;
3481 BdrvChild
*filtered
, *backing
;
3482 BlockDriver
*drv
= bs
->drv
;
3483 BdrvTrackedRequest req
;
3484 int64_t old_size
, new_bytes
;
3488 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3490 error_setg(errp
, "No medium inserted");
3494 error_setg(errp
, "Image size cannot be negative");
3498 ret
= bdrv_check_request(offset
, 0, errp
);
3503 old_size
= bdrv_getlength(bs
);
3505 error_setg_errno(errp
, -old_size
, "Failed to get old image size");
3509 if (bdrv_is_read_only(bs
)) {
3510 error_setg(errp
, "Image is read-only");
3514 if (offset
> old_size
) {
3515 new_bytes
= offset
- old_size
;
3520 bdrv_inc_in_flight(bs
);
3521 tracked_request_begin(&req
, bs
, offset
- new_bytes
, new_bytes
,
3522 BDRV_TRACKED_TRUNCATE
);
3524 /* If we are growing the image and potentially using preallocation for the
3525 * new area, we need to make sure that no write requests are made to it
3526 * concurrently or they might be overwritten by preallocation. */
3528 bdrv_make_request_serialising(&req
, 1);
3530 ret
= bdrv_co_write_req_prepare(child
, offset
- new_bytes
, new_bytes
, &req
,
3533 error_setg_errno(errp
, -ret
,
3534 "Failed to prepare request for truncation");
3538 filtered
= bdrv_filter_child(bs
);
3539 backing
= bdrv_cow_child(bs
);
3542 * If the image has a backing file that is large enough that it would
3543 * provide data for the new area, we cannot leave it unallocated because
3544 * then the backing file content would become visible. Instead, zero-fill
3547 * Note that if the image has a backing file, but was opened without the
3548 * backing file, taking care of keeping things consistent with that backing
3549 * file is the user's responsibility.
3551 if (new_bytes
&& backing
) {
3552 int64_t backing_len
;
3554 backing_len
= bdrv_getlength(backing
->bs
);
3555 if (backing_len
< 0) {
3557 error_setg_errno(errp
, -ret
, "Could not get backing file size");
3561 if (backing_len
> old_size
) {
3562 flags
|= BDRV_REQ_ZERO_WRITE
;
3566 if (drv
->bdrv_co_truncate
) {
3567 if (flags
& ~bs
->supported_truncate_flags
) {
3568 error_setg(errp
, "Block driver does not support requested flags");
3572 ret
= drv
->bdrv_co_truncate(bs
, offset
, exact
, prealloc
, flags
, errp
);
3573 } else if (filtered
) {
3574 ret
= bdrv_co_truncate(filtered
, offset
, exact
, prealloc
, flags
, errp
);
3576 error_setg(errp
, "Image format driver does not support resize");
3584 ret
= refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
3586 error_setg_errno(errp
, -ret
, "Could not refresh total sector count");
3588 offset
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
3590 /* It's possible that truncation succeeded but refresh_total_sectors
3591 * failed, but the latter doesn't affect how we should finish the request.
3592 * Pass 0 as the last parameter so that dirty bitmaps etc. are handled. */
3593 bdrv_co_write_req_finish(child
, offset
- new_bytes
, new_bytes
, &req
, 0);
3596 tracked_request_end(&req
);
3597 bdrv_dec_in_flight(bs
);
3602 void bdrv_cancel_in_flight(BlockDriverState
*bs
)
3604 GLOBAL_STATE_CODE();
3605 if (!bs
|| !bs
->drv
) {
3609 if (bs
->drv
->bdrv_cancel_in_flight
) {
3610 bs
->drv
->bdrv_cancel_in_flight(bs
);
3615 bdrv_co_preadv_snapshot(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
3616 QEMUIOVector
*qiov
, size_t qiov_offset
)
3618 BlockDriverState
*bs
= child
->bs
;
3619 BlockDriver
*drv
= bs
->drv
;
3627 if (!drv
->bdrv_co_preadv_snapshot
) {
3631 bdrv_inc_in_flight(bs
);
3632 ret
= drv
->bdrv_co_preadv_snapshot(bs
, offset
, bytes
, qiov
, qiov_offset
);
3633 bdrv_dec_in_flight(bs
);
3639 bdrv_co_snapshot_block_status(BlockDriverState
*bs
,
3640 bool want_zero
, int64_t offset
, int64_t bytes
,
3641 int64_t *pnum
, int64_t *map
,
3642 BlockDriverState
**file
)
3644 BlockDriver
*drv
= bs
->drv
;
3652 if (!drv
->bdrv_co_snapshot_block_status
) {
3656 bdrv_inc_in_flight(bs
);
3657 ret
= drv
->bdrv_co_snapshot_block_status(bs
, want_zero
, offset
, bytes
,
3659 bdrv_dec_in_flight(bs
);
3665 bdrv_co_pdiscard_snapshot(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
3667 BlockDriver
*drv
= bs
->drv
;
3675 if (!drv
->bdrv_co_pdiscard_snapshot
) {
3679 bdrv_inc_in_flight(bs
);
3680 ret
= drv
->bdrv_co_pdiscard_snapshot(bs
, offset
, bytes
);
3681 bdrv_dec_in_flight(bs
);