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 "qemu/cutils.h"
34 #include "qapi/error.h"
35 #include "qemu/error-report.h"
36 #include "qemu/main-loop.h"
37 #include "sysemu/replay.h"
39 /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
40 #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
42 static void bdrv_parent_cb_resize(BlockDriverState
*bs
);
43 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
44 int64_t offset
, int bytes
, BdrvRequestFlags flags
);
46 static void bdrv_parent_drained_begin(BlockDriverState
*bs
, BdrvChild
*ignore
,
47 bool ignore_bds_parents
)
51 QLIST_FOREACH_SAFE(c
, &bs
->parents
, next_parent
, next
) {
52 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
55 bdrv_parent_drained_begin_single(c
, false);
59 static void bdrv_parent_drained_end_single_no_poll(BdrvChild
*c
,
60 int *drained_end_counter
)
62 assert(c
->parent_quiesce_counter
> 0);
63 c
->parent_quiesce_counter
--;
64 if (c
->klass
->drained_end
) {
65 c
->klass
->drained_end(c
, drained_end_counter
);
69 void bdrv_parent_drained_end_single(BdrvChild
*c
)
71 int drained_end_counter
= 0;
72 bdrv_parent_drained_end_single_no_poll(c
, &drained_end_counter
);
73 BDRV_POLL_WHILE(c
->bs
, qatomic_read(&drained_end_counter
) > 0);
76 static void bdrv_parent_drained_end(BlockDriverState
*bs
, BdrvChild
*ignore
,
77 bool ignore_bds_parents
,
78 int *drained_end_counter
)
82 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
83 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
86 bdrv_parent_drained_end_single_no_poll(c
, drained_end_counter
);
90 static bool bdrv_parent_drained_poll_single(BdrvChild
*c
)
92 if (c
->klass
->drained_poll
) {
93 return c
->klass
->drained_poll(c
);
98 static bool bdrv_parent_drained_poll(BlockDriverState
*bs
, BdrvChild
*ignore
,
99 bool ignore_bds_parents
)
104 QLIST_FOREACH_SAFE(c
, &bs
->parents
, next_parent
, next
) {
105 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
108 busy
|= bdrv_parent_drained_poll_single(c
);
114 void bdrv_parent_drained_begin_single(BdrvChild
*c
, bool poll
)
116 c
->parent_quiesce_counter
++;
117 if (c
->klass
->drained_begin
) {
118 c
->klass
->drained_begin(c
);
121 BDRV_POLL_WHILE(c
->bs
, bdrv_parent_drained_poll_single(c
));
125 static void bdrv_merge_limits(BlockLimits
*dst
, const BlockLimits
*src
)
127 dst
->opt_transfer
= MAX(dst
->opt_transfer
, src
->opt_transfer
);
128 dst
->max_transfer
= MIN_NON_ZERO(dst
->max_transfer
, src
->max_transfer
);
129 dst
->opt_mem_alignment
= MAX(dst
->opt_mem_alignment
,
130 src
->opt_mem_alignment
);
131 dst
->min_mem_alignment
= MAX(dst
->min_mem_alignment
,
132 src
->min_mem_alignment
);
133 dst
->max_iov
= MIN_NON_ZERO(dst
->max_iov
, src
->max_iov
);
136 void bdrv_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
138 BlockDriver
*drv
= bs
->drv
;
141 Error
*local_err
= NULL
;
143 memset(&bs
->bl
, 0, sizeof(bs
->bl
));
149 /* Default alignment based on whether driver has byte interface */
150 bs
->bl
.request_alignment
= (drv
->bdrv_co_preadv
||
151 drv
->bdrv_aio_preadv
||
152 drv
->bdrv_co_preadv_part
) ? 1 : 512;
154 /* Take some limits from the children as a default */
156 QLIST_FOREACH(c
, &bs
->children
, next
) {
157 if (c
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_FILTERED
| BDRV_CHILD_COW
))
159 bdrv_refresh_limits(c
->bs
, &local_err
);
161 error_propagate(errp
, local_err
);
164 bdrv_merge_limits(&bs
->bl
, &c
->bs
->bl
);
170 bs
->bl
.min_mem_alignment
= 512;
171 bs
->bl
.opt_mem_alignment
= qemu_real_host_page_size
;
173 /* Safe default since most protocols use readv()/writev()/etc */
174 bs
->bl
.max_iov
= IOV_MAX
;
177 /* Then let the driver override it */
178 if (drv
->bdrv_refresh_limits
) {
179 drv
->bdrv_refresh_limits(bs
, errp
);
184 * The copy-on-read flag is actually a reference count so multiple users may
185 * use the feature without worrying about clobbering its previous state.
186 * Copy-on-read stays enabled until all users have called to disable it.
188 void bdrv_enable_copy_on_read(BlockDriverState
*bs
)
190 qatomic_inc(&bs
->copy_on_read
);
193 void bdrv_disable_copy_on_read(BlockDriverState
*bs
)
195 int old
= qatomic_fetch_dec(&bs
->copy_on_read
);
201 BlockDriverState
*bs
;
207 bool ignore_bds_parents
;
208 int *drained_end_counter
;
211 static void coroutine_fn
bdrv_drain_invoke_entry(void *opaque
)
213 BdrvCoDrainData
*data
= opaque
;
214 BlockDriverState
*bs
= data
->bs
;
217 bs
->drv
->bdrv_co_drain_begin(bs
);
219 bs
->drv
->bdrv_co_drain_end(bs
);
222 /* Set data->done and decrement drained_end_counter before bdrv_wakeup() */
223 qatomic_mb_set(&data
->done
, true);
225 qatomic_dec(data
->drained_end_counter
);
227 bdrv_dec_in_flight(bs
);
232 /* Recursively call BlockDriver.bdrv_co_drain_begin/end callbacks */
233 static void bdrv_drain_invoke(BlockDriverState
*bs
, bool begin
,
234 int *drained_end_counter
)
236 BdrvCoDrainData
*data
;
238 if (!bs
->drv
|| (begin
&& !bs
->drv
->bdrv_co_drain_begin
) ||
239 (!begin
&& !bs
->drv
->bdrv_co_drain_end
)) {
243 data
= g_new(BdrvCoDrainData
, 1);
244 *data
= (BdrvCoDrainData
) {
248 .drained_end_counter
= drained_end_counter
,
252 qatomic_inc(drained_end_counter
);
255 /* Make sure the driver callback completes during the polling phase for
257 bdrv_inc_in_flight(bs
);
258 data
->co
= qemu_coroutine_create(bdrv_drain_invoke_entry
, data
);
259 aio_co_schedule(bdrv_get_aio_context(bs
), data
->co
);
262 /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
263 bool bdrv_drain_poll(BlockDriverState
*bs
, bool recursive
,
264 BdrvChild
*ignore_parent
, bool ignore_bds_parents
)
266 BdrvChild
*child
, *next
;
268 if (bdrv_parent_drained_poll(bs
, ignore_parent
, ignore_bds_parents
)) {
272 if (qatomic_read(&bs
->in_flight
)) {
277 assert(!ignore_bds_parents
);
278 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
279 if (bdrv_drain_poll(child
->bs
, recursive
, child
, false)) {
288 static bool bdrv_drain_poll_top_level(BlockDriverState
*bs
, bool recursive
,
289 BdrvChild
*ignore_parent
)
291 return bdrv_drain_poll(bs
, recursive
, ignore_parent
, false);
294 static void bdrv_do_drained_begin(BlockDriverState
*bs
, bool recursive
,
295 BdrvChild
*parent
, bool ignore_bds_parents
,
297 static void bdrv_do_drained_end(BlockDriverState
*bs
, bool recursive
,
298 BdrvChild
*parent
, bool ignore_bds_parents
,
299 int *drained_end_counter
);
301 static void bdrv_co_drain_bh_cb(void *opaque
)
303 BdrvCoDrainData
*data
= opaque
;
304 Coroutine
*co
= data
->co
;
305 BlockDriverState
*bs
= data
->bs
;
308 AioContext
*ctx
= bdrv_get_aio_context(bs
);
309 AioContext
*co_ctx
= qemu_coroutine_get_aio_context(co
);
312 * When the coroutine yielded, the lock for its home context was
313 * released, so we need to re-acquire it here. If it explicitly
314 * acquired a different context, the lock is still held and we don't
315 * want to lock it a second time (or AIO_WAIT_WHILE() would hang).
318 aio_context_acquire(ctx
);
320 bdrv_dec_in_flight(bs
);
322 assert(!data
->drained_end_counter
);
323 bdrv_do_drained_begin(bs
, data
->recursive
, data
->parent
,
324 data
->ignore_bds_parents
, data
->poll
);
327 bdrv_do_drained_end(bs
, data
->recursive
, data
->parent
,
328 data
->ignore_bds_parents
,
329 data
->drained_end_counter
);
332 aio_context_release(ctx
);
336 bdrv_drain_all_begin();
343 static void coroutine_fn
bdrv_co_yield_to_drain(BlockDriverState
*bs
,
344 bool begin
, bool recursive
,
346 bool ignore_bds_parents
,
348 int *drained_end_counter
)
350 BdrvCoDrainData data
;
352 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
353 * other coroutines run if they were queued by aio_co_enter(). */
355 assert(qemu_in_coroutine());
356 data
= (BdrvCoDrainData
) {
357 .co
= qemu_coroutine_self(),
361 .recursive
= recursive
,
363 .ignore_bds_parents
= ignore_bds_parents
,
365 .drained_end_counter
= drained_end_counter
,
369 bdrv_inc_in_flight(bs
);
371 replay_bh_schedule_oneshot_event(bdrv_get_aio_context(bs
),
372 bdrv_co_drain_bh_cb
, &data
);
374 qemu_coroutine_yield();
375 /* If we are resumed from some other event (such as an aio completion or a
376 * timer callback), it is a bug in the caller that should be fixed. */
380 void bdrv_do_drained_begin_quiesce(BlockDriverState
*bs
,
381 BdrvChild
*parent
, bool ignore_bds_parents
)
383 assert(!qemu_in_coroutine());
385 /* Stop things in parent-to-child order */
386 if (qatomic_fetch_inc(&bs
->quiesce_counter
) == 0) {
387 aio_disable_external(bdrv_get_aio_context(bs
));
390 bdrv_parent_drained_begin(bs
, parent
, ignore_bds_parents
);
391 bdrv_drain_invoke(bs
, true, NULL
);
394 static void bdrv_do_drained_begin(BlockDriverState
*bs
, bool recursive
,
395 BdrvChild
*parent
, bool ignore_bds_parents
,
398 BdrvChild
*child
, *next
;
400 if (qemu_in_coroutine()) {
401 bdrv_co_yield_to_drain(bs
, true, recursive
, parent
, ignore_bds_parents
,
406 bdrv_do_drained_begin_quiesce(bs
, parent
, ignore_bds_parents
);
409 assert(!ignore_bds_parents
);
410 bs
->recursive_quiesce_counter
++;
411 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
412 bdrv_do_drained_begin(child
->bs
, true, child
, ignore_bds_parents
,
418 * Wait for drained requests to finish.
420 * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
421 * call is needed so things in this AioContext can make progress even
422 * though we don't return to the main AioContext loop - this automatically
423 * includes other nodes in the same AioContext and therefore all child
427 assert(!ignore_bds_parents
);
428 BDRV_POLL_WHILE(bs
, bdrv_drain_poll_top_level(bs
, recursive
, parent
));
432 void bdrv_drained_begin(BlockDriverState
*bs
)
434 bdrv_do_drained_begin(bs
, false, NULL
, false, true);
437 void bdrv_subtree_drained_begin(BlockDriverState
*bs
)
439 bdrv_do_drained_begin(bs
, true, NULL
, false, true);
443 * This function does not poll, nor must any of its recursively called
444 * functions. The *drained_end_counter pointee will be incremented
445 * once for every background operation scheduled, and decremented once
446 * the operation settles. Therefore, the pointer must remain valid
447 * until the pointee reaches 0. That implies that whoever sets up the
448 * pointee has to poll until it is 0.
450 * We use atomic operations to access *drained_end_counter, because
451 * (1) when called from bdrv_set_aio_context_ignore(), the subgraph of
452 * @bs may contain nodes in different AioContexts,
453 * (2) bdrv_drain_all_end() uses the same counter for all nodes,
454 * regardless of which AioContext they are in.
456 static void bdrv_do_drained_end(BlockDriverState
*bs
, bool recursive
,
457 BdrvChild
*parent
, bool ignore_bds_parents
,
458 int *drained_end_counter
)
461 int old_quiesce_counter
;
463 assert(drained_end_counter
!= NULL
);
465 if (qemu_in_coroutine()) {
466 bdrv_co_yield_to_drain(bs
, false, recursive
, parent
, ignore_bds_parents
,
467 false, drained_end_counter
);
470 assert(bs
->quiesce_counter
> 0);
472 /* Re-enable things in child-to-parent order */
473 bdrv_drain_invoke(bs
, false, drained_end_counter
);
474 bdrv_parent_drained_end(bs
, parent
, ignore_bds_parents
,
475 drained_end_counter
);
477 old_quiesce_counter
= qatomic_fetch_dec(&bs
->quiesce_counter
);
478 if (old_quiesce_counter
== 1) {
479 aio_enable_external(bdrv_get_aio_context(bs
));
483 assert(!ignore_bds_parents
);
484 bs
->recursive_quiesce_counter
--;
485 QLIST_FOREACH(child
, &bs
->children
, next
) {
486 bdrv_do_drained_end(child
->bs
, true, child
, ignore_bds_parents
,
487 drained_end_counter
);
492 void bdrv_drained_end(BlockDriverState
*bs
)
494 int drained_end_counter
= 0;
495 bdrv_do_drained_end(bs
, false, NULL
, false, &drained_end_counter
);
496 BDRV_POLL_WHILE(bs
, qatomic_read(&drained_end_counter
) > 0);
499 void bdrv_drained_end_no_poll(BlockDriverState
*bs
, int *drained_end_counter
)
501 bdrv_do_drained_end(bs
, false, NULL
, false, drained_end_counter
);
504 void bdrv_subtree_drained_end(BlockDriverState
*bs
)
506 int drained_end_counter
= 0;
507 bdrv_do_drained_end(bs
, true, NULL
, false, &drained_end_counter
);
508 BDRV_POLL_WHILE(bs
, qatomic_read(&drained_end_counter
) > 0);
511 void bdrv_apply_subtree_drain(BdrvChild
*child
, BlockDriverState
*new_parent
)
515 for (i
= 0; i
< new_parent
->recursive_quiesce_counter
; i
++) {
516 bdrv_do_drained_begin(child
->bs
, true, child
, false, true);
520 void bdrv_unapply_subtree_drain(BdrvChild
*child
, BlockDriverState
*old_parent
)
522 int drained_end_counter
= 0;
525 for (i
= 0; i
< old_parent
->recursive_quiesce_counter
; i
++) {
526 bdrv_do_drained_end(child
->bs
, true, child
, false,
527 &drained_end_counter
);
530 BDRV_POLL_WHILE(child
->bs
, qatomic_read(&drained_end_counter
) > 0);
534 * Wait for pending requests to complete on a single BlockDriverState subtree,
535 * and suspend block driver's internal I/O until next request arrives.
537 * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
540 void coroutine_fn
bdrv_co_drain(BlockDriverState
*bs
)
542 assert(qemu_in_coroutine());
543 bdrv_drained_begin(bs
);
544 bdrv_drained_end(bs
);
547 void bdrv_drain(BlockDriverState
*bs
)
549 bdrv_drained_begin(bs
);
550 bdrv_drained_end(bs
);
553 static void bdrv_drain_assert_idle(BlockDriverState
*bs
)
555 BdrvChild
*child
, *next
;
557 assert(qatomic_read(&bs
->in_flight
) == 0);
558 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
559 bdrv_drain_assert_idle(child
->bs
);
563 unsigned int bdrv_drain_all_count
= 0;
565 static bool bdrv_drain_all_poll(void)
567 BlockDriverState
*bs
= NULL
;
570 /* bdrv_drain_poll() can't make changes to the graph and we are holding the
571 * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
572 while ((bs
= bdrv_next_all_states(bs
))) {
573 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
574 aio_context_acquire(aio_context
);
575 result
|= bdrv_drain_poll(bs
, false, NULL
, true);
576 aio_context_release(aio_context
);
583 * Wait for pending requests to complete across all BlockDriverStates
585 * This function does not flush data to disk, use bdrv_flush_all() for that
586 * after calling this function.
588 * This pauses all block jobs and disables external clients. It must
589 * be paired with bdrv_drain_all_end().
591 * NOTE: no new block jobs or BlockDriverStates can be created between
592 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
594 void bdrv_drain_all_begin(void)
596 BlockDriverState
*bs
= NULL
;
598 if (qemu_in_coroutine()) {
599 bdrv_co_yield_to_drain(NULL
, true, false, NULL
, true, true, NULL
);
604 * bdrv queue is managed by record/replay,
605 * waiting for finishing the I/O requests may
608 if (replay_events_enabled()) {
612 /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
613 * loop AioContext, so make sure we're in the main context. */
614 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
615 assert(bdrv_drain_all_count
< INT_MAX
);
616 bdrv_drain_all_count
++;
618 /* Quiesce all nodes, without polling in-flight requests yet. The graph
619 * cannot change during this loop. */
620 while ((bs
= bdrv_next_all_states(bs
))) {
621 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
623 aio_context_acquire(aio_context
);
624 bdrv_do_drained_begin(bs
, false, NULL
, true, false);
625 aio_context_release(aio_context
);
628 /* Now poll the in-flight requests */
629 AIO_WAIT_WHILE(NULL
, bdrv_drain_all_poll());
631 while ((bs
= bdrv_next_all_states(bs
))) {
632 bdrv_drain_assert_idle(bs
);
636 void bdrv_drain_all_end(void)
638 BlockDriverState
*bs
= NULL
;
639 int drained_end_counter
= 0;
642 * bdrv queue is managed by record/replay,
643 * waiting for finishing the I/O requests may
646 if (replay_events_enabled()) {
650 while ((bs
= bdrv_next_all_states(bs
))) {
651 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
653 aio_context_acquire(aio_context
);
654 bdrv_do_drained_end(bs
, false, NULL
, true, &drained_end_counter
);
655 aio_context_release(aio_context
);
658 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
659 AIO_WAIT_WHILE(NULL
, qatomic_read(&drained_end_counter
) > 0);
661 assert(bdrv_drain_all_count
> 0);
662 bdrv_drain_all_count
--;
665 void bdrv_drain_all(void)
667 bdrv_drain_all_begin();
668 bdrv_drain_all_end();
672 * Remove an active request from the tracked requests list
674 * This function should be called when a tracked request is completing.
676 static void tracked_request_end(BdrvTrackedRequest
*req
)
678 if (req
->serialising
) {
679 qatomic_dec(&req
->bs
->serialising_in_flight
);
682 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
683 QLIST_REMOVE(req
, list
);
684 qemu_co_queue_restart_all(&req
->wait_queue
);
685 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
689 * Add an active request to the tracked requests list
691 static void tracked_request_begin(BdrvTrackedRequest
*req
,
692 BlockDriverState
*bs
,
695 enum BdrvTrackedRequestType type
)
697 assert(bytes
<= INT64_MAX
&& offset
<= INT64_MAX
- bytes
);
699 *req
= (BdrvTrackedRequest
){
704 .co
= qemu_coroutine_self(),
705 .serialising
= false,
706 .overlap_offset
= offset
,
707 .overlap_bytes
= bytes
,
710 qemu_co_queue_init(&req
->wait_queue
);
712 qemu_co_mutex_lock(&bs
->reqs_lock
);
713 QLIST_INSERT_HEAD(&bs
->tracked_requests
, req
, list
);
714 qemu_co_mutex_unlock(&bs
->reqs_lock
);
717 static bool tracked_request_overlaps(BdrvTrackedRequest
*req
,
718 int64_t offset
, uint64_t bytes
)
721 if (offset
>= req
->overlap_offset
+ req
->overlap_bytes
) {
725 if (req
->overlap_offset
>= offset
+ bytes
) {
731 static bool coroutine_fn
732 bdrv_wait_serialising_requests_locked(BlockDriverState
*bs
,
733 BdrvTrackedRequest
*self
)
735 BdrvTrackedRequest
*req
;
741 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
742 if (req
== self
|| (!req
->serialising
&& !self
->serialising
)) {
745 if (tracked_request_overlaps(req
, self
->overlap_offset
,
746 self
->overlap_bytes
))
748 /* Hitting this means there was a reentrant request, for
749 * example, a block driver issuing nested requests. This must
750 * never happen since it means deadlock.
752 assert(qemu_coroutine_self() != req
->co
);
754 /* If the request is already (indirectly) waiting for us, or
755 * will wait for us as soon as it wakes up, then just go on
756 * (instead of producing a deadlock in the former case). */
757 if (!req
->waiting_for
) {
758 self
->waiting_for
= req
;
759 qemu_co_queue_wait(&req
->wait_queue
, &bs
->reqs_lock
);
760 self
->waiting_for
= NULL
;
771 bool bdrv_mark_request_serialising(BdrvTrackedRequest
*req
, uint64_t align
)
773 BlockDriverState
*bs
= req
->bs
;
774 int64_t overlap_offset
= req
->offset
& ~(align
- 1);
775 uint64_t overlap_bytes
= ROUND_UP(req
->offset
+ req
->bytes
, align
)
779 qemu_co_mutex_lock(&bs
->reqs_lock
);
780 if (!req
->serialising
) {
781 qatomic_inc(&req
->bs
->serialising_in_flight
);
782 req
->serialising
= true;
785 req
->overlap_offset
= MIN(req
->overlap_offset
, overlap_offset
);
786 req
->overlap_bytes
= MAX(req
->overlap_bytes
, overlap_bytes
);
787 waited
= bdrv_wait_serialising_requests_locked(bs
, req
);
788 qemu_co_mutex_unlock(&bs
->reqs_lock
);
793 * Return the tracked request on @bs for the current coroutine, or
794 * NULL if there is none.
796 BdrvTrackedRequest
*coroutine_fn
bdrv_co_get_self_request(BlockDriverState
*bs
)
798 BdrvTrackedRequest
*req
;
799 Coroutine
*self
= qemu_coroutine_self();
801 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
802 if (req
->co
== self
) {
811 * Round a region to cluster boundaries
813 void bdrv_round_to_clusters(BlockDriverState
*bs
,
814 int64_t offset
, int64_t bytes
,
815 int64_t *cluster_offset
,
816 int64_t *cluster_bytes
)
820 if (bdrv_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
821 *cluster_offset
= offset
;
822 *cluster_bytes
= bytes
;
824 int64_t c
= bdi
.cluster_size
;
825 *cluster_offset
= QEMU_ALIGN_DOWN(offset
, c
);
826 *cluster_bytes
= QEMU_ALIGN_UP(offset
- *cluster_offset
+ bytes
, c
);
830 static int bdrv_get_cluster_size(BlockDriverState
*bs
)
835 ret
= bdrv_get_info(bs
, &bdi
);
836 if (ret
< 0 || bdi
.cluster_size
== 0) {
837 return bs
->bl
.request_alignment
;
839 return bdi
.cluster_size
;
843 void bdrv_inc_in_flight(BlockDriverState
*bs
)
845 qatomic_inc(&bs
->in_flight
);
848 void bdrv_wakeup(BlockDriverState
*bs
)
853 void bdrv_dec_in_flight(BlockDriverState
*bs
)
855 qatomic_dec(&bs
->in_flight
);
859 static bool coroutine_fn
bdrv_wait_serialising_requests(BdrvTrackedRequest
*self
)
861 BlockDriverState
*bs
= self
->bs
;
864 if (!qatomic_read(&bs
->serialising_in_flight
)) {
868 qemu_co_mutex_lock(&bs
->reqs_lock
);
869 waited
= bdrv_wait_serialising_requests_locked(bs
, self
);
870 qemu_co_mutex_unlock(&bs
->reqs_lock
);
875 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
878 if (size
> BDRV_REQUEST_MAX_BYTES
) {
882 if (!bdrv_is_inserted(bs
)) {
893 int bdrv_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
894 int bytes
, BdrvRequestFlags flags
)
896 return bdrv_pwritev(child
, offset
, bytes
, NULL
,
897 BDRV_REQ_ZERO_WRITE
| flags
);
901 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
902 * The operation is sped up by checking the block status and only writing
903 * zeroes to the device if they currently do not return zeroes. Optional
904 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
907 * Returns < 0 on error, 0 on success. For error codes see bdrv_pwrite().
909 int bdrv_make_zero(BdrvChild
*child
, BdrvRequestFlags flags
)
912 int64_t target_size
, bytes
, offset
= 0;
913 BlockDriverState
*bs
= child
->bs
;
915 target_size
= bdrv_getlength(bs
);
916 if (target_size
< 0) {
921 bytes
= MIN(target_size
- offset
, BDRV_REQUEST_MAX_BYTES
);
925 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, NULL
, NULL
);
929 if (ret
& BDRV_BLOCK_ZERO
) {
933 ret
= bdrv_pwrite_zeroes(child
, offset
, bytes
, flags
);
941 /* See bdrv_pwrite() for the return codes */
942 int bdrv_pread(BdrvChild
*child
, int64_t offset
, void *buf
, int bytes
)
945 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, bytes
);
951 ret
= bdrv_preadv(child
, offset
, bytes
, &qiov
, 0);
953 return ret
< 0 ? ret
: bytes
;
956 /* Return no. of bytes on success or < 0 on error. Important errors are:
957 -EIO generic I/O error (may happen for all errors)
958 -ENOMEDIUM No media inserted.
959 -EINVAL Invalid offset or number of bytes
960 -EACCES Trying to write a read-only device
962 int bdrv_pwrite(BdrvChild
*child
, int64_t offset
, const void *buf
, int bytes
)
965 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, bytes
);
971 ret
= bdrv_pwritev(child
, offset
, bytes
, &qiov
, 0);
973 return ret
< 0 ? ret
: bytes
;
977 * Writes to the file and ensures that no writes are reordered across this
978 * request (acts as a barrier)
980 * Returns 0 on success, -errno in error cases.
982 int bdrv_pwrite_sync(BdrvChild
*child
, int64_t offset
,
983 const void *buf
, int count
)
987 ret
= bdrv_pwrite(child
, offset
, buf
, count
);
992 ret
= bdrv_flush(child
->bs
);
1000 typedef struct CoroutineIOCompletion
{
1001 Coroutine
*coroutine
;
1003 } CoroutineIOCompletion
;
1005 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
1007 CoroutineIOCompletion
*co
= opaque
;
1010 aio_co_wake(co
->coroutine
);
1013 static int coroutine_fn
bdrv_driver_preadv(BlockDriverState
*bs
,
1014 uint64_t offset
, uint64_t bytes
,
1016 size_t qiov_offset
, int flags
)
1018 BlockDriver
*drv
= bs
->drv
;
1020 unsigned int nb_sectors
;
1021 QEMUIOVector local_qiov
;
1024 assert(!(flags
& ~BDRV_REQ_MASK
));
1025 assert(!(flags
& BDRV_REQ_NO_FALLBACK
));
1031 if (drv
->bdrv_co_preadv_part
) {
1032 return drv
->bdrv_co_preadv_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
1036 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
1037 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1041 if (drv
->bdrv_co_preadv
) {
1042 ret
= drv
->bdrv_co_preadv(bs
, offset
, bytes
, qiov
, flags
);
1046 if (drv
->bdrv_aio_preadv
) {
1048 CoroutineIOCompletion co
= {
1049 .coroutine
= qemu_coroutine_self(),
1052 acb
= drv
->bdrv_aio_preadv(bs
, offset
, bytes
, qiov
, flags
,
1053 bdrv_co_io_em_complete
, &co
);
1058 qemu_coroutine_yield();
1064 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1065 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1067 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1068 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1069 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1070 assert(drv
->bdrv_co_readv
);
1072 ret
= drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
1075 if (qiov
== &local_qiov
) {
1076 qemu_iovec_destroy(&local_qiov
);
1082 static int coroutine_fn
bdrv_driver_pwritev(BlockDriverState
*bs
,
1083 uint64_t offset
, uint64_t bytes
,
1085 size_t qiov_offset
, int flags
)
1087 BlockDriver
*drv
= bs
->drv
;
1089 unsigned int nb_sectors
;
1090 QEMUIOVector local_qiov
;
1093 assert(!(flags
& ~BDRV_REQ_MASK
));
1094 assert(!(flags
& BDRV_REQ_NO_FALLBACK
));
1100 if (drv
->bdrv_co_pwritev_part
) {
1101 ret
= drv
->bdrv_co_pwritev_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
1102 flags
& bs
->supported_write_flags
);
1103 flags
&= ~bs
->supported_write_flags
;
1107 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
1108 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1112 if (drv
->bdrv_co_pwritev
) {
1113 ret
= drv
->bdrv_co_pwritev(bs
, offset
, bytes
, qiov
,
1114 flags
& bs
->supported_write_flags
);
1115 flags
&= ~bs
->supported_write_flags
;
1119 if (drv
->bdrv_aio_pwritev
) {
1121 CoroutineIOCompletion co
= {
1122 .coroutine
= qemu_coroutine_self(),
1125 acb
= drv
->bdrv_aio_pwritev(bs
, offset
, bytes
, qiov
,
1126 flags
& bs
->supported_write_flags
,
1127 bdrv_co_io_em_complete
, &co
);
1128 flags
&= ~bs
->supported_write_flags
;
1132 qemu_coroutine_yield();
1138 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1139 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1141 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1142 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1143 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1145 assert(drv
->bdrv_co_writev
);
1146 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
,
1147 flags
& bs
->supported_write_flags
);
1148 flags
&= ~bs
->supported_write_flags
;
1151 if (ret
== 0 && (flags
& BDRV_REQ_FUA
)) {
1152 ret
= bdrv_co_flush(bs
);
1155 if (qiov
== &local_qiov
) {
1156 qemu_iovec_destroy(&local_qiov
);
1162 static int coroutine_fn
1163 bdrv_driver_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
1164 uint64_t bytes
, QEMUIOVector
*qiov
,
1167 BlockDriver
*drv
= bs
->drv
;
1168 QEMUIOVector local_qiov
;
1175 if (!block_driver_can_compress(drv
)) {
1179 if (drv
->bdrv_co_pwritev_compressed_part
) {
1180 return drv
->bdrv_co_pwritev_compressed_part(bs
, offset
, bytes
,
1184 if (qiov_offset
== 0) {
1185 return drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, qiov
);
1188 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1189 ret
= drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, &local_qiov
);
1190 qemu_iovec_destroy(&local_qiov
);
1195 static int coroutine_fn
bdrv_co_do_copy_on_readv(BdrvChild
*child
,
1196 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1197 size_t qiov_offset
, int flags
)
1199 BlockDriverState
*bs
= child
->bs
;
1201 /* Perform I/O through a temporary buffer so that users who scribble over
1202 * their read buffer while the operation is in progress do not end up
1203 * modifying the image file. This is critical for zero-copy guest I/O
1204 * where anything might happen inside guest memory.
1206 void *bounce_buffer
= NULL
;
1208 BlockDriver
*drv
= bs
->drv
;
1209 int64_t cluster_offset
;
1210 int64_t cluster_bytes
;
1213 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
,
1214 BDRV_REQUEST_MAX_BYTES
);
1215 unsigned int progress
= 0;
1223 * Do not write anything when the BDS is inactive. That is not
1224 * allowed, and it would not help.
1226 skip_write
= (bs
->open_flags
& BDRV_O_INACTIVE
);
1228 /* FIXME We cannot require callers to have write permissions when all they
1229 * are doing is a read request. If we did things right, write permissions
1230 * would be obtained anyway, but internally by the copy-on-read code. As
1231 * long as it is implemented here rather than in a separate filter driver,
1232 * the copy-on-read code doesn't have its own BdrvChild, however, for which
1233 * it could request permissions. Therefore we have to bypass the permission
1234 * system for the moment. */
1235 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1237 /* Cover entire cluster so no additional backing file I/O is required when
1238 * allocating cluster in the image file. Note that this value may exceed
1239 * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1240 * is one reason we loop rather than doing it all at once.
1242 bdrv_round_to_clusters(bs
, offset
, bytes
, &cluster_offset
, &cluster_bytes
);
1243 skip_bytes
= offset
- cluster_offset
;
1245 trace_bdrv_co_do_copy_on_readv(bs
, offset
, bytes
,
1246 cluster_offset
, cluster_bytes
);
1248 while (cluster_bytes
) {
1252 ret
= 1; /* "already allocated", so nothing will be copied */
1253 pnum
= MIN(cluster_bytes
, max_transfer
);
1255 ret
= bdrv_is_allocated(bs
, cluster_offset
,
1256 MIN(cluster_bytes
, max_transfer
), &pnum
);
1259 * Safe to treat errors in querying allocation as if
1260 * unallocated; we'll probably fail again soon on the
1261 * read, but at least that will set a decent errno.
1263 pnum
= MIN(cluster_bytes
, max_transfer
);
1266 /* Stop at EOF if the image ends in the middle of the cluster */
1267 if (ret
== 0 && pnum
== 0) {
1268 assert(progress
>= bytes
);
1272 assert(skip_bytes
< pnum
);
1276 QEMUIOVector local_qiov
;
1278 /* Must copy-on-read; use the bounce buffer */
1279 pnum
= MIN(pnum
, MAX_BOUNCE_BUFFER
);
1280 if (!bounce_buffer
) {
1281 int64_t max_we_need
= MAX(pnum
, cluster_bytes
- pnum
);
1282 int64_t max_allowed
= MIN(max_transfer
, MAX_BOUNCE_BUFFER
);
1283 int64_t bounce_buffer_len
= MIN(max_we_need
, max_allowed
);
1285 bounce_buffer
= qemu_try_blockalign(bs
, bounce_buffer_len
);
1286 if (!bounce_buffer
) {
1291 qemu_iovec_init_buf(&local_qiov
, bounce_buffer
, pnum
);
1293 ret
= bdrv_driver_preadv(bs
, cluster_offset
, pnum
,
1299 bdrv_debug_event(bs
, BLKDBG_COR_WRITE
);
1300 if (drv
->bdrv_co_pwrite_zeroes
&&
1301 buffer_is_zero(bounce_buffer
, pnum
)) {
1302 /* FIXME: Should we (perhaps conditionally) be setting
1303 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1304 * that still correctly reads as zero? */
1305 ret
= bdrv_co_do_pwrite_zeroes(bs
, cluster_offset
, pnum
,
1306 BDRV_REQ_WRITE_UNCHANGED
);
1308 /* This does not change the data on the disk, it is not
1309 * necessary to flush even in cache=writethrough mode.
1311 ret
= bdrv_driver_pwritev(bs
, cluster_offset
, pnum
,
1313 BDRV_REQ_WRITE_UNCHANGED
);
1317 /* It might be okay to ignore write errors for guest
1318 * requests. If this is a deliberate copy-on-read
1319 * then we don't want to ignore the error. Simply
1320 * report it in all cases.
1325 if (!(flags
& BDRV_REQ_PREFETCH
)) {
1326 qemu_iovec_from_buf(qiov
, qiov_offset
+ progress
,
1327 bounce_buffer
+ skip_bytes
,
1328 MIN(pnum
- skip_bytes
, bytes
- progress
));
1330 } else if (!(flags
& BDRV_REQ_PREFETCH
)) {
1331 /* Read directly into the destination */
1332 ret
= bdrv_driver_preadv(bs
, offset
+ progress
,
1333 MIN(pnum
- skip_bytes
, bytes
- progress
),
1334 qiov
, qiov_offset
+ progress
, 0);
1340 cluster_offset
+= pnum
;
1341 cluster_bytes
-= pnum
;
1342 progress
+= pnum
- skip_bytes
;
1348 qemu_vfree(bounce_buffer
);
1353 * Forwards an already correctly aligned request to the BlockDriver. This
1354 * handles copy on read, zeroing after EOF, and fragmentation of large
1355 * reads; any other features must be implemented by the caller.
1357 static int coroutine_fn
bdrv_aligned_preadv(BdrvChild
*child
,
1358 BdrvTrackedRequest
*req
, int64_t offset
, unsigned int bytes
,
1359 int64_t align
, QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
1361 BlockDriverState
*bs
= child
->bs
;
1362 int64_t total_bytes
, max_bytes
;
1364 uint64_t bytes_remaining
= bytes
;
1367 assert(is_power_of_2(align
));
1368 assert((offset
& (align
- 1)) == 0);
1369 assert((bytes
& (align
- 1)) == 0);
1370 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1371 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1374 /* TODO: We would need a per-BDS .supported_read_flags and
1375 * potential fallback support, if we ever implement any read flags
1376 * to pass through to drivers. For now, there aren't any
1377 * passthrough flags. */
1378 assert(!(flags
& ~(BDRV_REQ_COPY_ON_READ
| BDRV_REQ_PREFETCH
)));
1380 /* Handle Copy on Read and associated serialisation */
1381 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1382 /* If we touch the same cluster it counts as an overlap. This
1383 * guarantees that allocating writes will be serialized and not race
1384 * with each other for the same cluster. For example, in copy-on-read
1385 * it ensures that the CoR read and write operations are atomic and
1386 * guest writes cannot interleave between them. */
1387 bdrv_mark_request_serialising(req
, bdrv_get_cluster_size(bs
));
1389 bdrv_wait_serialising_requests(req
);
1392 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1395 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &pnum
);
1400 if (!ret
|| pnum
!= bytes
) {
1401 ret
= bdrv_co_do_copy_on_readv(child
, offset
, bytes
,
1402 qiov
, qiov_offset
, flags
);
1404 } else if (flags
& BDRV_REQ_PREFETCH
) {
1409 /* Forward the request to the BlockDriver, possibly fragmenting it */
1410 total_bytes
= bdrv_getlength(bs
);
1411 if (total_bytes
< 0) {
1416 max_bytes
= ROUND_UP(MAX(0, total_bytes
- offset
), align
);
1417 if (bytes
<= max_bytes
&& bytes
<= max_transfer
) {
1418 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, qiov_offset
, 0);
1422 while (bytes_remaining
) {
1426 num
= MIN(bytes_remaining
, MIN(max_bytes
, max_transfer
));
1429 ret
= bdrv_driver_preadv(bs
, offset
+ bytes
- bytes_remaining
,
1431 qiov_offset
+ bytes
- bytes_remaining
, 0);
1434 num
= bytes_remaining
;
1435 ret
= qemu_iovec_memset(qiov
, qiov_offset
+ bytes
- bytes_remaining
,
1436 0, bytes_remaining
);
1441 bytes_remaining
-= num
;
1445 return ret
< 0 ? ret
: 0;
1451 * |<---- align ----->| |<----- align ---->|
1452 * |<- head ->|<------------- bytes ------------->|<-- tail -->|
1454 * -*----------$-------*-------- ... --------*-----$------------*---
1456 * | offset | | end |
1457 * ALIGN_DOWN(offset) ALIGN_UP(offset) ALIGN_DOWN(end) ALIGN_UP(end)
1458 * [buf ... ) [tail_buf )
1460 * @buf is an aligned allocation needed to store @head and @tail paddings. @head
1461 * is placed at the beginning of @buf and @tail at the @end.
1463 * @tail_buf is a pointer to sub-buffer, corresponding to align-sized chunk
1464 * around tail, if tail exists.
1466 * @merge_reads is true for small requests,
1467 * if @buf_len == @head + bytes + @tail. In this case it is possible that both
1468 * head and tail exist but @buf_len == align and @tail_buf == @buf.
1470 typedef struct BdrvRequestPadding
{
1477 QEMUIOVector local_qiov
;
1478 } BdrvRequestPadding
;
1480 static bool bdrv_init_padding(BlockDriverState
*bs
,
1481 int64_t offset
, int64_t bytes
,
1482 BdrvRequestPadding
*pad
)
1484 uint64_t align
= bs
->bl
.request_alignment
;
1487 memset(pad
, 0, sizeof(*pad
));
1489 pad
->head
= offset
& (align
- 1);
1490 pad
->tail
= ((offset
+ bytes
) & (align
- 1));
1492 pad
->tail
= align
- pad
->tail
;
1495 if (!pad
->head
&& !pad
->tail
) {
1499 assert(bytes
); /* Nothing good in aligning zero-length requests */
1501 sum
= pad
->head
+ bytes
+ pad
->tail
;
1502 pad
->buf_len
= (sum
> align
&& pad
->head
&& pad
->tail
) ? 2 * align
: align
;
1503 pad
->buf
= qemu_blockalign(bs
, pad
->buf_len
);
1504 pad
->merge_reads
= sum
== pad
->buf_len
;
1506 pad
->tail_buf
= pad
->buf
+ pad
->buf_len
- align
;
1512 static int bdrv_padding_rmw_read(BdrvChild
*child
,
1513 BdrvTrackedRequest
*req
,
1514 BdrvRequestPadding
*pad
,
1517 QEMUIOVector local_qiov
;
1518 BlockDriverState
*bs
= child
->bs
;
1519 uint64_t align
= bs
->bl
.request_alignment
;
1522 assert(req
->serialising
&& pad
->buf
);
1524 if (pad
->head
|| pad
->merge_reads
) {
1525 uint64_t bytes
= pad
->merge_reads
? pad
->buf_len
: align
;
1527 qemu_iovec_init_buf(&local_qiov
, pad
->buf
, bytes
);
1530 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1532 if (pad
->merge_reads
&& pad
->tail
) {
1533 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1535 ret
= bdrv_aligned_preadv(child
, req
, req
->overlap_offset
, bytes
,
1536 align
, &local_qiov
, 0, 0);
1541 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1543 if (pad
->merge_reads
&& pad
->tail
) {
1544 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1547 if (pad
->merge_reads
) {
1553 qemu_iovec_init_buf(&local_qiov
, pad
->tail_buf
, align
);
1555 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1556 ret
= bdrv_aligned_preadv(
1558 req
->overlap_offset
+ req
->overlap_bytes
- align
,
1559 align
, align
, &local_qiov
, 0, 0);
1563 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1568 memset(pad
->buf
+ pad
->head
, 0, pad
->buf_len
- pad
->head
- pad
->tail
);
1574 static void bdrv_padding_destroy(BdrvRequestPadding
*pad
)
1577 qemu_vfree(pad
->buf
);
1578 qemu_iovec_destroy(&pad
->local_qiov
);
1585 * Exchange request parameters with padded request if needed. Don't include RMW
1586 * read of padding, bdrv_padding_rmw_read() should be called separately if
1589 * All parameters except @bs are in-out: they represent original request at
1590 * function call and padded (if padding needed) at function finish.
1592 * Function always succeeds.
1594 static bool bdrv_pad_request(BlockDriverState
*bs
,
1595 QEMUIOVector
**qiov
, size_t *qiov_offset
,
1596 int64_t *offset
, unsigned int *bytes
,
1597 BdrvRequestPadding
*pad
)
1599 if (!bdrv_init_padding(bs
, *offset
, *bytes
, pad
)) {
1603 qemu_iovec_init_extended(&pad
->local_qiov
, pad
->buf
, pad
->head
,
1604 *qiov
, *qiov_offset
, *bytes
,
1605 pad
->buf
+ pad
->buf_len
- pad
->tail
, pad
->tail
);
1606 *bytes
+= pad
->head
+ pad
->tail
;
1607 *offset
-= pad
->head
;
1608 *qiov
= &pad
->local_qiov
;
1614 int coroutine_fn
bdrv_co_preadv(BdrvChild
*child
,
1615 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1616 BdrvRequestFlags flags
)
1618 return bdrv_co_preadv_part(child
, offset
, bytes
, qiov
, 0, flags
);
1621 int coroutine_fn
bdrv_co_preadv_part(BdrvChild
*child
,
1622 int64_t offset
, unsigned int bytes
,
1623 QEMUIOVector
*qiov
, size_t qiov_offset
,
1624 BdrvRequestFlags flags
)
1626 BlockDriverState
*bs
= child
->bs
;
1627 BdrvTrackedRequest req
;
1628 BdrvRequestPadding pad
;
1631 trace_bdrv_co_preadv(bs
, offset
, bytes
, flags
);
1633 ret
= bdrv_check_byte_request(bs
, offset
, bytes
);
1638 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
1640 * Aligning zero request is nonsense. Even if driver has special meaning
1641 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
1642 * it to driver due to request_alignment.
1644 * Still, no reason to return an error if someone do unaligned
1645 * zero-length read occasionally.
1650 bdrv_inc_in_flight(bs
);
1652 /* Don't do copy-on-read if we read data before write operation */
1653 if (qatomic_read(&bs
->copy_on_read
)) {
1654 flags
|= BDRV_REQ_COPY_ON_READ
;
1657 bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
);
1659 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_READ
);
1660 ret
= bdrv_aligned_preadv(child
, &req
, offset
, bytes
,
1661 bs
->bl
.request_alignment
,
1662 qiov
, qiov_offset
, flags
);
1663 tracked_request_end(&req
);
1664 bdrv_dec_in_flight(bs
);
1666 bdrv_padding_destroy(&pad
);
1671 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
1672 int64_t offset
, int bytes
, BdrvRequestFlags flags
)
1674 BlockDriver
*drv
= bs
->drv
;
1678 bool need_flush
= false;
1682 int max_write_zeroes
= MIN_NON_ZERO(bs
->bl
.max_pwrite_zeroes
, INT_MAX
);
1683 int alignment
= MAX(bs
->bl
.pwrite_zeroes_alignment
,
1684 bs
->bl
.request_alignment
);
1685 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
, MAX_BOUNCE_BUFFER
);
1691 if ((flags
& ~bs
->supported_zero_flags
) & BDRV_REQ_NO_FALLBACK
) {
1695 assert(alignment
% bs
->bl
.request_alignment
== 0);
1696 head
= offset
% alignment
;
1697 tail
= (offset
+ bytes
) % alignment
;
1698 max_write_zeroes
= QEMU_ALIGN_DOWN(max_write_zeroes
, alignment
);
1699 assert(max_write_zeroes
>= bs
->bl
.request_alignment
);
1701 while (bytes
> 0 && !ret
) {
1704 /* Align request. Block drivers can expect the "bulk" of the request
1705 * to be aligned, and that unaligned requests do not cross cluster
1709 /* Make a small request up to the first aligned sector. For
1710 * convenience, limit this request to max_transfer even if
1711 * we don't need to fall back to writes. */
1712 num
= MIN(MIN(bytes
, max_transfer
), alignment
- head
);
1713 head
= (head
+ num
) % alignment
;
1714 assert(num
< max_write_zeroes
);
1715 } else if (tail
&& num
> alignment
) {
1716 /* Shorten the request to the last aligned sector. */
1720 /* limit request size */
1721 if (num
> max_write_zeroes
) {
1722 num
= max_write_zeroes
;
1726 /* First try the efficient write zeroes operation */
1727 if (drv
->bdrv_co_pwrite_zeroes
) {
1728 ret
= drv
->bdrv_co_pwrite_zeroes(bs
, offset
, num
,
1729 flags
& bs
->supported_zero_flags
);
1730 if (ret
!= -ENOTSUP
&& (flags
& BDRV_REQ_FUA
) &&
1731 !(bs
->supported_zero_flags
& BDRV_REQ_FUA
)) {
1735 assert(!bs
->supported_zero_flags
);
1738 if (ret
== -ENOTSUP
&& !(flags
& BDRV_REQ_NO_FALLBACK
)) {
1739 /* Fall back to bounce buffer if write zeroes is unsupported */
1740 BdrvRequestFlags write_flags
= flags
& ~BDRV_REQ_ZERO_WRITE
;
1742 if ((flags
& BDRV_REQ_FUA
) &&
1743 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1744 /* No need for bdrv_driver_pwrite() to do a fallback
1745 * flush on each chunk; use just one at the end */
1746 write_flags
&= ~BDRV_REQ_FUA
;
1749 num
= MIN(num
, max_transfer
);
1751 buf
= qemu_try_blockalign0(bs
, num
);
1757 qemu_iovec_init_buf(&qiov
, buf
, num
);
1759 ret
= bdrv_driver_pwritev(bs
, offset
, num
, &qiov
, 0, write_flags
);
1761 /* Keep bounce buffer around if it is big enough for all
1762 * all future requests.
1764 if (num
< max_transfer
) {
1775 if (ret
== 0 && need_flush
) {
1776 ret
= bdrv_co_flush(bs
);
1782 static inline int coroutine_fn
1783 bdrv_co_write_req_prepare(BdrvChild
*child
, int64_t offset
, uint64_t bytes
,
1784 BdrvTrackedRequest
*req
, int flags
)
1786 BlockDriverState
*bs
= child
->bs
;
1788 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
1790 if (bs
->read_only
) {
1794 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
1795 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1796 assert(!(flags
& ~BDRV_REQ_MASK
));
1798 if (flags
& BDRV_REQ_SERIALISING
) {
1799 waited
= bdrv_mark_request_serialising(req
, bdrv_get_cluster_size(bs
));
1801 * For a misaligned request we should have already waited earlier,
1802 * because we come after bdrv_padding_rmw_read which must be called
1803 * with the request already marked as serialising.
1806 (req
->offset
== req
->overlap_offset
&&
1807 req
->bytes
== req
->overlap_bytes
));
1809 bdrv_wait_serialising_requests(req
);
1812 assert(req
->overlap_offset
<= offset
);
1813 assert(offset
+ bytes
<= req
->overlap_offset
+ req
->overlap_bytes
);
1814 assert(end_sector
<= bs
->total_sectors
|| child
->perm
& BLK_PERM_RESIZE
);
1816 switch (req
->type
) {
1817 case BDRV_TRACKED_WRITE
:
1818 case BDRV_TRACKED_DISCARD
:
1819 if (flags
& BDRV_REQ_WRITE_UNCHANGED
) {
1820 assert(child
->perm
& (BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_WRITE
));
1822 assert(child
->perm
& BLK_PERM_WRITE
);
1824 return notifier_with_return_list_notify(&bs
->before_write_notifiers
,
1826 case BDRV_TRACKED_TRUNCATE
:
1827 assert(child
->perm
& BLK_PERM_RESIZE
);
1834 static inline void coroutine_fn
1835 bdrv_co_write_req_finish(BdrvChild
*child
, int64_t offset
, uint64_t bytes
,
1836 BdrvTrackedRequest
*req
, int ret
)
1838 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
1839 BlockDriverState
*bs
= child
->bs
;
1841 qatomic_inc(&bs
->write_gen
);
1844 * Discard cannot extend the image, but in error handling cases, such as
1845 * when reverting a qcow2 cluster allocation, the discarded range can pass
1846 * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
1847 * here. Instead, just skip it, since semantically a discard request
1848 * beyond EOF cannot expand the image anyway.
1851 (req
->type
== BDRV_TRACKED_TRUNCATE
||
1852 end_sector
> bs
->total_sectors
) &&
1853 req
->type
!= BDRV_TRACKED_DISCARD
) {
1854 bs
->total_sectors
= end_sector
;
1855 bdrv_parent_cb_resize(bs
);
1856 bdrv_dirty_bitmap_truncate(bs
, end_sector
<< BDRV_SECTOR_BITS
);
1859 switch (req
->type
) {
1860 case BDRV_TRACKED_WRITE
:
1861 stat64_max(&bs
->wr_highest_offset
, offset
+ bytes
);
1862 /* fall through, to set dirty bits */
1863 case BDRV_TRACKED_DISCARD
:
1864 bdrv_set_dirty(bs
, offset
, bytes
);
1873 * Forwards an already correctly aligned write request to the BlockDriver,
1874 * after possibly fragmenting it.
1876 static int coroutine_fn
bdrv_aligned_pwritev(BdrvChild
*child
,
1877 BdrvTrackedRequest
*req
, int64_t offset
, unsigned int bytes
,
1878 int64_t align
, QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
1880 BlockDriverState
*bs
= child
->bs
;
1881 BlockDriver
*drv
= bs
->drv
;
1884 uint64_t bytes_remaining
= bytes
;
1891 if (bdrv_has_readonly_bitmaps(bs
)) {
1895 assert(is_power_of_2(align
));
1896 assert((offset
& (align
- 1)) == 0);
1897 assert((bytes
& (align
- 1)) == 0);
1898 assert(!qiov
|| qiov_offset
+ bytes
<= qiov
->size
);
1899 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1902 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, req
, flags
);
1904 if (!ret
&& bs
->detect_zeroes
!= BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
&&
1905 !(flags
& BDRV_REQ_ZERO_WRITE
) && drv
->bdrv_co_pwrite_zeroes
&&
1906 qemu_iovec_is_zero(qiov
, qiov_offset
, bytes
)) {
1907 flags
|= BDRV_REQ_ZERO_WRITE
;
1908 if (bs
->detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
) {
1909 flags
|= BDRV_REQ_MAY_UNMAP
;
1914 /* Do nothing, write notifier decided to fail this request */
1915 } else if (flags
& BDRV_REQ_ZERO_WRITE
) {
1916 bdrv_debug_event(bs
, BLKDBG_PWRITEV_ZERO
);
1917 ret
= bdrv_co_do_pwrite_zeroes(bs
, offset
, bytes
, flags
);
1918 } else if (flags
& BDRV_REQ_WRITE_COMPRESSED
) {
1919 ret
= bdrv_driver_pwritev_compressed(bs
, offset
, bytes
,
1921 } else if (bytes
<= max_transfer
) {
1922 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
1923 ret
= bdrv_driver_pwritev(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1925 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
1926 while (bytes_remaining
) {
1927 int num
= MIN(bytes_remaining
, max_transfer
);
1928 int local_flags
= flags
;
1931 if (num
< bytes_remaining
&& (flags
& BDRV_REQ_FUA
) &&
1932 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1933 /* If FUA is going to be emulated by flush, we only
1934 * need to flush on the last iteration */
1935 local_flags
&= ~BDRV_REQ_FUA
;
1938 ret
= bdrv_driver_pwritev(bs
, offset
+ bytes
- bytes_remaining
,
1940 qiov_offset
+ bytes
- bytes_remaining
,
1945 bytes_remaining
-= num
;
1948 bdrv_debug_event(bs
, BLKDBG_PWRITEV_DONE
);
1953 bdrv_co_write_req_finish(child
, offset
, bytes
, req
, ret
);
1958 static int coroutine_fn
bdrv_co_do_zero_pwritev(BdrvChild
*child
,
1961 BdrvRequestFlags flags
,
1962 BdrvTrackedRequest
*req
)
1964 BlockDriverState
*bs
= child
->bs
;
1965 QEMUIOVector local_qiov
;
1966 uint64_t align
= bs
->bl
.request_alignment
;
1969 BdrvRequestPadding pad
;
1971 padding
= bdrv_init_padding(bs
, offset
, bytes
, &pad
);
1973 bdrv_mark_request_serialising(req
, align
);
1975 bdrv_padding_rmw_read(child
, req
, &pad
, true);
1977 if (pad
.head
|| pad
.merge_reads
) {
1978 int64_t aligned_offset
= offset
& ~(align
- 1);
1979 int64_t write_bytes
= pad
.merge_reads
? pad
.buf_len
: align
;
1981 qemu_iovec_init_buf(&local_qiov
, pad
.buf
, write_bytes
);
1982 ret
= bdrv_aligned_pwritev(child
, req
, aligned_offset
, write_bytes
,
1983 align
, &local_qiov
, 0,
1984 flags
& ~BDRV_REQ_ZERO_WRITE
);
1985 if (ret
< 0 || pad
.merge_reads
) {
1986 /* Error or all work is done */
1989 offset
+= write_bytes
- pad
.head
;
1990 bytes
-= write_bytes
- pad
.head
;
1994 assert(!bytes
|| (offset
& (align
- 1)) == 0);
1995 if (bytes
>= align
) {
1996 /* Write the aligned part in the middle. */
1997 uint64_t aligned_bytes
= bytes
& ~(align
- 1);
1998 ret
= bdrv_aligned_pwritev(child
, req
, offset
, aligned_bytes
, align
,
2003 bytes
-= aligned_bytes
;
2004 offset
+= aligned_bytes
;
2007 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2009 assert(align
== pad
.tail
+ bytes
);
2011 qemu_iovec_init_buf(&local_qiov
, pad
.tail_buf
, align
);
2012 ret
= bdrv_aligned_pwritev(child
, req
, offset
, align
, align
,
2014 flags
& ~BDRV_REQ_ZERO_WRITE
);
2018 bdrv_padding_destroy(&pad
);
2024 * Handle a write request in coroutine context
2026 int coroutine_fn
bdrv_co_pwritev(BdrvChild
*child
,
2027 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
2028 BdrvRequestFlags flags
)
2030 return bdrv_co_pwritev_part(child
, offset
, bytes
, qiov
, 0, flags
);
2033 int coroutine_fn
bdrv_co_pwritev_part(BdrvChild
*child
,
2034 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
, size_t qiov_offset
,
2035 BdrvRequestFlags flags
)
2037 BlockDriverState
*bs
= child
->bs
;
2038 BdrvTrackedRequest req
;
2039 uint64_t align
= bs
->bl
.request_alignment
;
2040 BdrvRequestPadding pad
;
2043 trace_bdrv_co_pwritev(child
->bs
, offset
, bytes
, flags
);
2049 ret
= bdrv_check_byte_request(bs
, offset
, bytes
);
2054 /* If the request is misaligned then we can't make it efficient */
2055 if ((flags
& BDRV_REQ_NO_FALLBACK
) &&
2056 !QEMU_IS_ALIGNED(offset
| bytes
, align
))
2061 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
2063 * Aligning zero request is nonsense. Even if driver has special meaning
2064 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
2065 * it to driver due to request_alignment.
2067 * Still, no reason to return an error if someone do unaligned
2068 * zero-length write occasionally.
2073 bdrv_inc_in_flight(bs
);
2075 * Align write if necessary by performing a read-modify-write cycle.
2076 * Pad qiov with the read parts and be sure to have a tracked request not
2077 * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
2079 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_WRITE
);
2081 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2082 ret
= bdrv_co_do_zero_pwritev(child
, offset
, bytes
, flags
, &req
);
2086 if (bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
)) {
2087 bdrv_mark_request_serialising(&req
, align
);
2088 bdrv_padding_rmw_read(child
, &req
, &pad
, false);
2091 ret
= bdrv_aligned_pwritev(child
, &req
, offset
, bytes
, align
,
2092 qiov
, qiov_offset
, flags
);
2094 bdrv_padding_destroy(&pad
);
2097 tracked_request_end(&req
);
2098 bdrv_dec_in_flight(bs
);
2103 int coroutine_fn
bdrv_co_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
2104 int bytes
, BdrvRequestFlags flags
)
2106 trace_bdrv_co_pwrite_zeroes(child
->bs
, offset
, bytes
, flags
);
2108 if (!(child
->bs
->open_flags
& BDRV_O_UNMAP
)) {
2109 flags
&= ~BDRV_REQ_MAY_UNMAP
;
2112 return bdrv_co_pwritev(child
, offset
, bytes
, NULL
,
2113 BDRV_REQ_ZERO_WRITE
| flags
);
2117 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
2119 int bdrv_flush_all(void)
2121 BdrvNextIterator it
;
2122 BlockDriverState
*bs
= NULL
;
2126 * bdrv queue is managed by record/replay,
2127 * creating new flush request for stopping
2128 * the VM may break the determinism
2130 if (replay_events_enabled()) {
2134 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2135 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
2138 aio_context_acquire(aio_context
);
2139 ret
= bdrv_flush(bs
);
2140 if (ret
< 0 && !result
) {
2143 aio_context_release(aio_context
);
2150 * Returns the allocation status of the specified sectors.
2151 * Drivers not implementing the functionality are assumed to not support
2152 * backing files, hence all their sectors are reported as allocated.
2154 * If 'want_zero' is true, the caller is querying for mapping
2155 * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and
2156 * _ZERO where possible; otherwise, the result favors larger 'pnum',
2157 * with a focus on accurate BDRV_BLOCK_ALLOCATED.
2159 * If 'offset' is beyond the end of the disk image the return value is
2160 * BDRV_BLOCK_EOF and 'pnum' is set to 0.
2162 * 'bytes' is the max value 'pnum' should be set to. If bytes goes
2163 * beyond the end of the disk image it will be clamped; if 'pnum' is set to
2164 * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
2166 * 'pnum' is set to the number of bytes (including and immediately
2167 * following the specified offset) that are easily known to be in the
2168 * same allocated/unallocated state. Note that a second call starting
2169 * at the original offset plus returned pnum may have the same status.
2170 * The returned value is non-zero on success except at end-of-file.
2172 * Returns negative errno on failure. Otherwise, if the
2173 * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are
2174 * set to the host mapping and BDS corresponding to the guest offset.
2176 static int coroutine_fn
bdrv_co_block_status(BlockDriverState
*bs
,
2178 int64_t offset
, int64_t bytes
,
2179 int64_t *pnum
, int64_t *map
,
2180 BlockDriverState
**file
)
2183 int64_t n
; /* bytes */
2185 int64_t local_map
= 0;
2186 BlockDriverState
*local_file
= NULL
;
2187 int64_t aligned_offset
, aligned_bytes
;
2189 bool has_filtered_child
;
2193 total_size
= bdrv_getlength(bs
);
2194 if (total_size
< 0) {
2199 if (offset
>= total_size
) {
2200 ret
= BDRV_BLOCK_EOF
;
2208 n
= total_size
- offset
;
2213 /* Must be non-NULL or bdrv_getlength() would have failed */
2215 has_filtered_child
= bdrv_filter_child(bs
);
2216 if (!bs
->drv
->bdrv_co_block_status
&& !has_filtered_child
) {
2218 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
;
2219 if (offset
+ bytes
== total_size
) {
2220 ret
|= BDRV_BLOCK_EOF
;
2222 if (bs
->drv
->protocol_name
) {
2223 ret
|= BDRV_BLOCK_OFFSET_VALID
;
2230 bdrv_inc_in_flight(bs
);
2232 /* Round out to request_alignment boundaries */
2233 align
= bs
->bl
.request_alignment
;
2234 aligned_offset
= QEMU_ALIGN_DOWN(offset
, align
);
2235 aligned_bytes
= ROUND_UP(offset
+ bytes
, align
) - aligned_offset
;
2237 if (bs
->drv
->bdrv_co_block_status
) {
2238 ret
= bs
->drv
->bdrv_co_block_status(bs
, want_zero
, aligned_offset
,
2239 aligned_bytes
, pnum
, &local_map
,
2242 /* Default code for filters */
2244 local_file
= bdrv_filter_bs(bs
);
2247 *pnum
= aligned_bytes
;
2248 local_map
= aligned_offset
;
2249 ret
= BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
;
2257 * The driver's result must be a non-zero multiple of request_alignment.
2258 * Clamp pnum and adjust map to original request.
2260 assert(*pnum
&& QEMU_IS_ALIGNED(*pnum
, align
) &&
2261 align
> offset
- aligned_offset
);
2262 if (ret
& BDRV_BLOCK_RECURSE
) {
2263 assert(ret
& BDRV_BLOCK_DATA
);
2264 assert(ret
& BDRV_BLOCK_OFFSET_VALID
);
2265 assert(!(ret
& BDRV_BLOCK_ZERO
));
2268 *pnum
-= offset
- aligned_offset
;
2269 if (*pnum
> bytes
) {
2272 if (ret
& BDRV_BLOCK_OFFSET_VALID
) {
2273 local_map
+= offset
- aligned_offset
;
2276 if (ret
& BDRV_BLOCK_RAW
) {
2277 assert(ret
& BDRV_BLOCK_OFFSET_VALID
&& local_file
);
2278 ret
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2279 *pnum
, pnum
, &local_map
, &local_file
);
2283 if (ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ZERO
)) {
2284 ret
|= BDRV_BLOCK_ALLOCATED
;
2285 } else if (want_zero
&& bs
->drv
->supports_backing
) {
2286 BlockDriverState
*cow_bs
= bdrv_cow_bs(bs
);
2289 int64_t size2
= bdrv_getlength(cow_bs
);
2291 if (size2
>= 0 && offset
>= size2
) {
2292 ret
|= BDRV_BLOCK_ZERO
;
2295 ret
|= BDRV_BLOCK_ZERO
;
2299 if (want_zero
&& ret
& BDRV_BLOCK_RECURSE
&&
2300 local_file
&& local_file
!= bs
&&
2301 (ret
& BDRV_BLOCK_DATA
) && !(ret
& BDRV_BLOCK_ZERO
) &&
2302 (ret
& BDRV_BLOCK_OFFSET_VALID
)) {
2306 ret2
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2307 *pnum
, &file_pnum
, NULL
, NULL
);
2309 /* Ignore errors. This is just providing extra information, it
2310 * is useful but not necessary.
2312 if (ret2
& BDRV_BLOCK_EOF
&&
2313 (!file_pnum
|| ret2
& BDRV_BLOCK_ZERO
)) {
2315 * It is valid for the format block driver to read
2316 * beyond the end of the underlying file's current
2317 * size; such areas read as zero.
2319 ret
|= BDRV_BLOCK_ZERO
;
2321 /* Limit request to the range reported by the protocol driver */
2323 ret
|= (ret2
& BDRV_BLOCK_ZERO
);
2329 bdrv_dec_in_flight(bs
);
2330 if (ret
>= 0 && offset
+ *pnum
== total_size
) {
2331 ret
|= BDRV_BLOCK_EOF
;
2344 bdrv_co_common_block_status_above(BlockDriverState
*bs
,
2345 BlockDriverState
*base
,
2352 BlockDriverState
**file
)
2355 BlockDriverState
*p
;
2358 assert(!include_base
|| base
); /* Can't include NULL base */
2360 if (!include_base
&& bs
== base
) {
2365 ret
= bdrv_co_block_status(bs
, want_zero
, offset
, bytes
, pnum
, map
, file
);
2366 if (ret
< 0 || *pnum
== 0 || ret
& BDRV_BLOCK_ALLOCATED
|| bs
== base
) {
2370 if (ret
& BDRV_BLOCK_EOF
) {
2371 eof
= offset
+ *pnum
;
2374 assert(*pnum
<= bytes
);
2377 for (p
= bdrv_filter_or_cow_bs(bs
); include_base
|| p
!= base
;
2378 p
= bdrv_filter_or_cow_bs(p
))
2380 ret
= bdrv_co_block_status(p
, want_zero
, offset
, bytes
, pnum
, map
,
2387 * The top layer deferred to this layer, and because this layer is
2388 * short, any zeroes that we synthesize beyond EOF behave as if they
2389 * were allocated at this layer.
2391 * We don't include BDRV_BLOCK_EOF into ret, as upper layer may be
2392 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2395 assert(ret
& BDRV_BLOCK_EOF
);
2400 ret
= BDRV_BLOCK_ZERO
| BDRV_BLOCK_ALLOCATED
;
2403 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2405 * We've found the node and the status, we must break.
2407 * Drop BDRV_BLOCK_EOF, as it's not for upper layer, which may be
2408 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2411 ret
&= ~BDRV_BLOCK_EOF
;
2416 assert(include_base
);
2421 * OK, [offset, offset + *pnum) region is unallocated on this layer,
2422 * let's continue the diving.
2424 assert(*pnum
<= bytes
);
2428 if (offset
+ *pnum
== eof
) {
2429 ret
|= BDRV_BLOCK_EOF
;
2435 int bdrv_block_status_above(BlockDriverState
*bs
, BlockDriverState
*base
,
2436 int64_t offset
, int64_t bytes
, int64_t *pnum
,
2437 int64_t *map
, BlockDriverState
**file
)
2439 return bdrv_common_block_status_above(bs
, base
, false, true, offset
, bytes
,
2443 int bdrv_block_status(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2444 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
2446 return bdrv_block_status_above(bs
, bdrv_filter_or_cow_bs(bs
),
2447 offset
, bytes
, pnum
, map
, file
);
2450 int coroutine_fn
bdrv_is_allocated(BlockDriverState
*bs
, int64_t offset
,
2451 int64_t bytes
, int64_t *pnum
)
2456 ret
= bdrv_common_block_status_above(bs
, bs
, true, false, offset
,
2457 bytes
, pnum
? pnum
: &dummy
, NULL
,
2462 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
2466 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2468 * Return 1 if (a prefix of) the given range is allocated in any image
2469 * between BASE and TOP (BASE is only included if include_base is set).
2470 * BASE can be NULL to check if the given offset is allocated in any
2471 * image of the chain. Return 0 otherwise, or negative errno on
2474 * 'pnum' is set to the number of bytes (including and immediately
2475 * following the specified offset) that are known to be in the same
2476 * allocated/unallocated state. Note that a subsequent call starting
2477 * at 'offset + *pnum' may return the same allocation status (in other
2478 * words, the result is not necessarily the maximum possible range);
2479 * but 'pnum' will only be 0 when end of file is reached.
2481 int bdrv_is_allocated_above(BlockDriverState
*top
,
2482 BlockDriverState
*base
,
2483 bool include_base
, int64_t offset
,
2484 int64_t bytes
, int64_t *pnum
)
2486 int ret
= bdrv_common_block_status_above(top
, base
, include_base
, false,
2487 offset
, bytes
, pnum
, NULL
, NULL
);
2492 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
2496 bdrv_co_readv_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2498 BlockDriver
*drv
= bs
->drv
;
2499 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2506 bdrv_inc_in_flight(bs
);
2508 if (drv
->bdrv_load_vmstate
) {
2509 ret
= drv
->bdrv_load_vmstate(bs
, qiov
, pos
);
2510 } else if (child_bs
) {
2511 ret
= bdrv_co_readv_vmstate(child_bs
, qiov
, pos
);
2514 bdrv_dec_in_flight(bs
);
2520 bdrv_co_writev_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2522 BlockDriver
*drv
= bs
->drv
;
2523 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2530 bdrv_inc_in_flight(bs
);
2532 if (drv
->bdrv_save_vmstate
) {
2533 ret
= drv
->bdrv_save_vmstate(bs
, qiov
, pos
);
2534 } else if (child_bs
) {
2535 ret
= bdrv_co_writev_vmstate(child_bs
, qiov
, pos
);
2538 bdrv_dec_in_flight(bs
);
2543 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2544 int64_t pos
, int size
)
2546 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2547 int ret
= bdrv_writev_vmstate(bs
, &qiov
, pos
);
2549 return ret
< 0 ? ret
: size
;
2552 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2553 int64_t pos
, int size
)
2555 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2556 int ret
= bdrv_readv_vmstate(bs
, &qiov
, pos
);
2558 return ret
< 0 ? ret
: size
;
2561 /**************************************************************/
2564 void bdrv_aio_cancel(BlockAIOCB
*acb
)
2567 bdrv_aio_cancel_async(acb
);
2568 while (acb
->refcnt
> 1) {
2569 if (acb
->aiocb_info
->get_aio_context
) {
2570 aio_poll(acb
->aiocb_info
->get_aio_context(acb
), true);
2571 } else if (acb
->bs
) {
2572 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2573 * assert that we're not using an I/O thread. Thread-safe
2574 * code should use bdrv_aio_cancel_async exclusively.
2576 assert(bdrv_get_aio_context(acb
->bs
) == qemu_get_aio_context());
2577 aio_poll(bdrv_get_aio_context(acb
->bs
), true);
2582 qemu_aio_unref(acb
);
2585 /* Async version of aio cancel. The caller is not blocked if the acb implements
2586 * cancel_async, otherwise we do nothing and let the request normally complete.
2587 * In either case the completion callback must be called. */
2588 void bdrv_aio_cancel_async(BlockAIOCB
*acb
)
2590 if (acb
->aiocb_info
->cancel_async
) {
2591 acb
->aiocb_info
->cancel_async(acb
);
2595 /**************************************************************/
2596 /* Coroutine block device emulation */
2598 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2600 BdrvChild
*primary_child
= bdrv_primary_child(bs
);
2605 bdrv_inc_in_flight(bs
);
2607 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
) ||
2612 qemu_co_mutex_lock(&bs
->reqs_lock
);
2613 current_gen
= qatomic_read(&bs
->write_gen
);
2615 /* Wait until any previous flushes are completed */
2616 while (bs
->active_flush_req
) {
2617 qemu_co_queue_wait(&bs
->flush_queue
, &bs
->reqs_lock
);
2620 /* Flushes reach this point in nondecreasing current_gen order. */
2621 bs
->active_flush_req
= true;
2622 qemu_co_mutex_unlock(&bs
->reqs_lock
);
2624 /* Write back all layers by calling one driver function */
2625 if (bs
->drv
->bdrv_co_flush
) {
2626 ret
= bs
->drv
->bdrv_co_flush(bs
);
2630 /* Write back cached data to the OS even with cache=unsafe */
2631 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_OS
);
2632 if (bs
->drv
->bdrv_co_flush_to_os
) {
2633 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
2639 /* But don't actually force it to the disk with cache=unsafe */
2640 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2641 goto flush_children
;
2644 /* Check if we really need to flush anything */
2645 if (bs
->flushed_gen
== current_gen
) {
2646 goto flush_children
;
2649 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_DISK
);
2651 /* bs->drv->bdrv_co_flush() might have ejected the BDS
2652 * (even in case of apparent success) */
2656 if (bs
->drv
->bdrv_co_flush_to_disk
) {
2657 ret
= bs
->drv
->bdrv_co_flush_to_disk(bs
);
2658 } else if (bs
->drv
->bdrv_aio_flush
) {
2660 CoroutineIOCompletion co
= {
2661 .coroutine
= qemu_coroutine_self(),
2664 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
2668 qemu_coroutine_yield();
2673 * Some block drivers always operate in either writethrough or unsafe
2674 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2675 * know how the server works (because the behaviour is hardcoded or
2676 * depends on server-side configuration), so we can't ensure that
2677 * everything is safe on disk. Returning an error doesn't work because
2678 * that would break guests even if the server operates in writethrough
2681 * Let's hope the user knows what he's doing.
2690 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2691 * in the case of cache=unsafe, so there are no useless flushes.
2695 QLIST_FOREACH(child
, &bs
->children
, next
) {
2696 if (child
->perm
& (BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
)) {
2697 int this_child_ret
= bdrv_co_flush(child
->bs
);
2699 ret
= this_child_ret
;
2705 /* Notify any pending flushes that we have completed */
2707 bs
->flushed_gen
= current_gen
;
2710 qemu_co_mutex_lock(&bs
->reqs_lock
);
2711 bs
->active_flush_req
= false;
2712 /* Return value is ignored - it's ok if wait queue is empty */
2713 qemu_co_queue_next(&bs
->flush_queue
);
2714 qemu_co_mutex_unlock(&bs
->reqs_lock
);
2717 bdrv_dec_in_flight(bs
);
2721 int coroutine_fn
bdrv_co_pdiscard(BdrvChild
*child
, int64_t offset
,
2724 BdrvTrackedRequest req
;
2725 int max_pdiscard
, ret
;
2726 int head
, tail
, align
;
2727 BlockDriverState
*bs
= child
->bs
;
2729 if (!bs
|| !bs
->drv
|| !bdrv_is_inserted(bs
)) {
2733 if (bdrv_has_readonly_bitmaps(bs
)) {
2737 if (offset
< 0 || bytes
< 0 || bytes
> INT64_MAX
- offset
) {
2741 /* Do nothing if disabled. */
2742 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
2746 if (!bs
->drv
->bdrv_co_pdiscard
&& !bs
->drv
->bdrv_aio_pdiscard
) {
2750 /* Discard is advisory, but some devices track and coalesce
2751 * unaligned requests, so we must pass everything down rather than
2752 * round here. Still, most devices will just silently ignore
2753 * unaligned requests (by returning -ENOTSUP), so we must fragment
2754 * the request accordingly. */
2755 align
= MAX(bs
->bl
.pdiscard_alignment
, bs
->bl
.request_alignment
);
2756 assert(align
% bs
->bl
.request_alignment
== 0);
2757 head
= offset
% align
;
2758 tail
= (offset
+ bytes
) % align
;
2760 bdrv_inc_in_flight(bs
);
2761 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_DISCARD
);
2763 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, &req
, 0);
2768 max_pdiscard
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_pdiscard
, INT_MAX
),
2770 assert(max_pdiscard
>= bs
->bl
.request_alignment
);
2773 int64_t num
= bytes
;
2776 /* Make small requests to get to alignment boundaries. */
2777 num
= MIN(bytes
, align
- head
);
2778 if (!QEMU_IS_ALIGNED(num
, bs
->bl
.request_alignment
)) {
2779 num
%= bs
->bl
.request_alignment
;
2781 head
= (head
+ num
) % align
;
2782 assert(num
< max_pdiscard
);
2785 /* Shorten the request to the last aligned cluster. */
2787 } else if (!QEMU_IS_ALIGNED(tail
, bs
->bl
.request_alignment
) &&
2788 tail
> bs
->bl
.request_alignment
) {
2789 tail
%= bs
->bl
.request_alignment
;
2793 /* limit request size */
2794 if (num
> max_pdiscard
) {
2802 if (bs
->drv
->bdrv_co_pdiscard
) {
2803 ret
= bs
->drv
->bdrv_co_pdiscard(bs
, offset
, num
);
2806 CoroutineIOCompletion co
= {
2807 .coroutine
= qemu_coroutine_self(),
2810 acb
= bs
->drv
->bdrv_aio_pdiscard(bs
, offset
, num
,
2811 bdrv_co_io_em_complete
, &co
);
2816 qemu_coroutine_yield();
2820 if (ret
&& ret
!= -ENOTSUP
) {
2829 bdrv_co_write_req_finish(child
, req
.offset
, req
.bytes
, &req
, ret
);
2830 tracked_request_end(&req
);
2831 bdrv_dec_in_flight(bs
);
2835 int bdrv_co_ioctl(BlockDriverState
*bs
, int req
, void *buf
)
2837 BlockDriver
*drv
= bs
->drv
;
2838 CoroutineIOCompletion co
= {
2839 .coroutine
= qemu_coroutine_self(),
2843 bdrv_inc_in_flight(bs
);
2844 if (!drv
|| (!drv
->bdrv_aio_ioctl
&& !drv
->bdrv_co_ioctl
)) {
2849 if (drv
->bdrv_co_ioctl
) {
2850 co
.ret
= drv
->bdrv_co_ioctl(bs
, req
, buf
);
2852 acb
= drv
->bdrv_aio_ioctl(bs
, req
, buf
, bdrv_co_io_em_complete
, &co
);
2857 qemu_coroutine_yield();
2860 bdrv_dec_in_flight(bs
);
2864 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
2866 return qemu_memalign(bdrv_opt_mem_align(bs
), size
);
2869 void *qemu_blockalign0(BlockDriverState
*bs
, size_t size
)
2871 return memset(qemu_blockalign(bs
, size
), 0, size
);
2874 void *qemu_try_blockalign(BlockDriverState
*bs
, size_t size
)
2876 size_t align
= bdrv_opt_mem_align(bs
);
2878 /* Ensure that NULL is never returned on success */
2884 return qemu_try_memalign(align
, size
);
2887 void *qemu_try_blockalign0(BlockDriverState
*bs
, size_t size
)
2889 void *mem
= qemu_try_blockalign(bs
, size
);
2892 memset(mem
, 0, size
);
2899 * Check if all memory in this vector is sector aligned.
2901 bool bdrv_qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
2904 size_t alignment
= bdrv_min_mem_align(bs
);
2906 for (i
= 0; i
< qiov
->niov
; i
++) {
2907 if ((uintptr_t) qiov
->iov
[i
].iov_base
% alignment
) {
2910 if (qiov
->iov
[i
].iov_len
% alignment
) {
2918 void bdrv_add_before_write_notifier(BlockDriverState
*bs
,
2919 NotifierWithReturn
*notifier
)
2921 notifier_with_return_list_add(&bs
->before_write_notifiers
, notifier
);
2924 void bdrv_io_plug(BlockDriverState
*bs
)
2928 QLIST_FOREACH(child
, &bs
->children
, next
) {
2929 bdrv_io_plug(child
->bs
);
2932 if (qatomic_fetch_inc(&bs
->io_plugged
) == 0) {
2933 BlockDriver
*drv
= bs
->drv
;
2934 if (drv
&& drv
->bdrv_io_plug
) {
2935 drv
->bdrv_io_plug(bs
);
2940 void bdrv_io_unplug(BlockDriverState
*bs
)
2944 assert(bs
->io_plugged
);
2945 if (qatomic_fetch_dec(&bs
->io_plugged
) == 1) {
2946 BlockDriver
*drv
= bs
->drv
;
2947 if (drv
&& drv
->bdrv_io_unplug
) {
2948 drv
->bdrv_io_unplug(bs
);
2952 QLIST_FOREACH(child
, &bs
->children
, next
) {
2953 bdrv_io_unplug(child
->bs
);
2957 void bdrv_register_buf(BlockDriverState
*bs
, void *host
, size_t size
)
2961 if (bs
->drv
&& bs
->drv
->bdrv_register_buf
) {
2962 bs
->drv
->bdrv_register_buf(bs
, host
, size
);
2964 QLIST_FOREACH(child
, &bs
->children
, next
) {
2965 bdrv_register_buf(child
->bs
, host
, size
);
2969 void bdrv_unregister_buf(BlockDriverState
*bs
, void *host
)
2973 if (bs
->drv
&& bs
->drv
->bdrv_unregister_buf
) {
2974 bs
->drv
->bdrv_unregister_buf(bs
, host
);
2976 QLIST_FOREACH(child
, &bs
->children
, next
) {
2977 bdrv_unregister_buf(child
->bs
, host
);
2981 static int coroutine_fn
bdrv_co_copy_range_internal(
2982 BdrvChild
*src
, uint64_t src_offset
, BdrvChild
*dst
,
2983 uint64_t dst_offset
, uint64_t bytes
,
2984 BdrvRequestFlags read_flags
, BdrvRequestFlags write_flags
,
2987 BdrvTrackedRequest req
;
2990 /* TODO We can support BDRV_REQ_NO_FALLBACK here */
2991 assert(!(read_flags
& BDRV_REQ_NO_FALLBACK
));
2992 assert(!(write_flags
& BDRV_REQ_NO_FALLBACK
));
2994 if (!dst
|| !dst
->bs
) {
2997 ret
= bdrv_check_byte_request(dst
->bs
, dst_offset
, bytes
);
3001 if (write_flags
& BDRV_REQ_ZERO_WRITE
) {
3002 return bdrv_co_pwrite_zeroes(dst
, dst_offset
, bytes
, write_flags
);
3005 if (!src
|| !src
->bs
) {
3008 ret
= bdrv_check_byte_request(src
->bs
, src_offset
, bytes
);
3013 if (!src
->bs
->drv
->bdrv_co_copy_range_from
3014 || !dst
->bs
->drv
->bdrv_co_copy_range_to
3015 || src
->bs
->encrypted
|| dst
->bs
->encrypted
) {
3020 bdrv_inc_in_flight(src
->bs
);
3021 tracked_request_begin(&req
, src
->bs
, src_offset
, bytes
,
3024 /* BDRV_REQ_SERIALISING is only for write operation */
3025 assert(!(read_flags
& BDRV_REQ_SERIALISING
));
3026 bdrv_wait_serialising_requests(&req
);
3028 ret
= src
->bs
->drv
->bdrv_co_copy_range_from(src
->bs
,
3032 read_flags
, write_flags
);
3034 tracked_request_end(&req
);
3035 bdrv_dec_in_flight(src
->bs
);
3037 bdrv_inc_in_flight(dst
->bs
);
3038 tracked_request_begin(&req
, dst
->bs
, dst_offset
, bytes
,
3039 BDRV_TRACKED_WRITE
);
3040 ret
= bdrv_co_write_req_prepare(dst
, dst_offset
, bytes
, &req
,
3043 ret
= dst
->bs
->drv
->bdrv_co_copy_range_to(dst
->bs
,
3047 read_flags
, write_flags
);
3049 bdrv_co_write_req_finish(dst
, dst_offset
, bytes
, &req
, ret
);
3050 tracked_request_end(&req
);
3051 bdrv_dec_in_flight(dst
->bs
);
3057 /* Copy range from @src to @dst.
3059 * See the comment of bdrv_co_copy_range for the parameter and return value
3061 int coroutine_fn
bdrv_co_copy_range_from(BdrvChild
*src
, uint64_t src_offset
,
3062 BdrvChild
*dst
, uint64_t dst_offset
,
3064 BdrvRequestFlags read_flags
,
3065 BdrvRequestFlags write_flags
)
3067 trace_bdrv_co_copy_range_from(src
, src_offset
, dst
, dst_offset
, bytes
,
3068 read_flags
, write_flags
);
3069 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3070 bytes
, read_flags
, write_flags
, true);
3073 /* Copy range from @src to @dst.
3075 * See the comment of bdrv_co_copy_range for the parameter and return value
3077 int coroutine_fn
bdrv_co_copy_range_to(BdrvChild
*src
, uint64_t src_offset
,
3078 BdrvChild
*dst
, uint64_t dst_offset
,
3080 BdrvRequestFlags read_flags
,
3081 BdrvRequestFlags write_flags
)
3083 trace_bdrv_co_copy_range_to(src
, src_offset
, dst
, dst_offset
, bytes
,
3084 read_flags
, write_flags
);
3085 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3086 bytes
, read_flags
, write_flags
, false);
3089 int coroutine_fn
bdrv_co_copy_range(BdrvChild
*src
, uint64_t src_offset
,
3090 BdrvChild
*dst
, uint64_t dst_offset
,
3091 uint64_t bytes
, BdrvRequestFlags read_flags
,
3092 BdrvRequestFlags write_flags
)
3094 return bdrv_co_copy_range_from(src
, src_offset
,
3096 bytes
, read_flags
, write_flags
);
3099 static void bdrv_parent_cb_resize(BlockDriverState
*bs
)
3102 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
3103 if (c
->klass
->resize
) {
3104 c
->klass
->resize(c
);
3110 * Truncate file to 'offset' bytes (needed only for file protocols)
3112 * If 'exact' is true, the file must be resized to exactly the given
3113 * 'offset'. Otherwise, it is sufficient for the node to be at least
3114 * 'offset' bytes in length.
3116 int coroutine_fn
bdrv_co_truncate(BdrvChild
*child
, int64_t offset
, bool exact
,
3117 PreallocMode prealloc
, BdrvRequestFlags flags
,
3120 BlockDriverState
*bs
= child
->bs
;
3121 BdrvChild
*filtered
, *backing
;
3122 BlockDriver
*drv
= bs
->drv
;
3123 BdrvTrackedRequest req
;
3124 int64_t old_size
, new_bytes
;
3128 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3130 error_setg(errp
, "No medium inserted");
3134 error_setg(errp
, "Image size cannot be negative");
3138 old_size
= bdrv_getlength(bs
);
3140 error_setg_errno(errp
, -old_size
, "Failed to get old image size");
3144 if (offset
> old_size
) {
3145 new_bytes
= offset
- old_size
;
3150 bdrv_inc_in_flight(bs
);
3151 tracked_request_begin(&req
, bs
, offset
- new_bytes
, new_bytes
,
3152 BDRV_TRACKED_TRUNCATE
);
3154 /* If we are growing the image and potentially using preallocation for the
3155 * new area, we need to make sure that no write requests are made to it
3156 * concurrently or they might be overwritten by preallocation. */
3158 bdrv_mark_request_serialising(&req
, 1);
3160 if (bs
->read_only
) {
3161 error_setg(errp
, "Image is read-only");
3165 ret
= bdrv_co_write_req_prepare(child
, offset
- new_bytes
, new_bytes
, &req
,
3168 error_setg_errno(errp
, -ret
,
3169 "Failed to prepare request for truncation");
3173 filtered
= bdrv_filter_child(bs
);
3174 backing
= bdrv_cow_child(bs
);
3177 * If the image has a backing file that is large enough that it would
3178 * provide data for the new area, we cannot leave it unallocated because
3179 * then the backing file content would become visible. Instead, zero-fill
3182 * Note that if the image has a backing file, but was opened without the
3183 * backing file, taking care of keeping things consistent with that backing
3184 * file is the user's responsibility.
3186 if (new_bytes
&& backing
) {
3187 int64_t backing_len
;
3189 backing_len
= bdrv_getlength(backing
->bs
);
3190 if (backing_len
< 0) {
3192 error_setg_errno(errp
, -ret
, "Could not get backing file size");
3196 if (backing_len
> old_size
) {
3197 flags
|= BDRV_REQ_ZERO_WRITE
;
3201 if (drv
->bdrv_co_truncate
) {
3202 if (flags
& ~bs
->supported_truncate_flags
) {
3203 error_setg(errp
, "Block driver does not support requested flags");
3207 ret
= drv
->bdrv_co_truncate(bs
, offset
, exact
, prealloc
, flags
, errp
);
3208 } else if (filtered
) {
3209 ret
= bdrv_co_truncate(filtered
, offset
, exact
, prealloc
, flags
, errp
);
3211 error_setg(errp
, "Image format driver does not support resize");
3219 ret
= refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
3221 error_setg_errno(errp
, -ret
, "Could not refresh total sector count");
3223 offset
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
3225 /* It's possible that truncation succeeded but refresh_total_sectors
3226 * failed, but the latter doesn't affect how we should finish the request.
3227 * Pass 0 as the last parameter so that dirty bitmaps etc. are handled. */
3228 bdrv_co_write_req_finish(child
, offset
- new_bytes
, new_bytes
, &req
, 0);
3231 tracked_request_end(&req
);
3232 bdrv_dec_in_flight(bs
);