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/dirty-bitmap.h"
34 #include "block/write-threshold.h"
35 #include "qemu/cutils.h"
36 #include "qemu/memalign.h"
37 #include "qapi/error.h"
38 #include "qemu/error-report.h"
39 #include "qemu/main-loop.h"
40 #include "sysemu/replay.h"
42 /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
43 #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
45 static void bdrv_parent_cb_resize(BlockDriverState
*bs
);
46 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
47 int64_t offset
, int64_t bytes
, BdrvRequestFlags flags
);
49 static void bdrv_parent_drained_begin(BlockDriverState
*bs
, BdrvChild
*ignore
)
53 QLIST_FOREACH_SAFE(c
, &bs
->parents
, next_parent
, next
) {
57 bdrv_parent_drained_begin_single(c
);
61 void bdrv_parent_drained_end_single(BdrvChild
*c
)
65 assert(c
->quiesced_parent
);
66 c
->quiesced_parent
= false;
68 if (c
->klass
->drained_end
) {
69 c
->klass
->drained_end(c
);
73 static void bdrv_parent_drained_end(BlockDriverState
*bs
, BdrvChild
*ignore
)
77 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
81 bdrv_parent_drained_end_single(c
);
85 bool bdrv_parent_drained_poll_single(BdrvChild
*c
)
87 if (c
->klass
->drained_poll
) {
88 return c
->klass
->drained_poll(c
);
93 static bool bdrv_parent_drained_poll(BlockDriverState
*bs
, BdrvChild
*ignore
,
94 bool ignore_bds_parents
)
99 QLIST_FOREACH_SAFE(c
, &bs
->parents
, next_parent
, next
) {
100 if (c
== ignore
|| (ignore_bds_parents
&& c
->klass
->parent_is_bds
)) {
103 busy
|= bdrv_parent_drained_poll_single(c
);
109 void bdrv_parent_drained_begin_single(BdrvChild
*c
)
113 assert(!c
->quiesced_parent
);
114 c
->quiesced_parent
= true;
116 if (c
->klass
->drained_begin
) {
117 c
->klass
->drained_begin(c
);
121 static void bdrv_merge_limits(BlockLimits
*dst
, const BlockLimits
*src
)
123 dst
->pdiscard_alignment
= MAX(dst
->pdiscard_alignment
,
124 src
->pdiscard_alignment
);
125 dst
->opt_transfer
= MAX(dst
->opt_transfer
, src
->opt_transfer
);
126 dst
->max_transfer
= MIN_NON_ZERO(dst
->max_transfer
, src
->max_transfer
);
127 dst
->max_hw_transfer
= MIN_NON_ZERO(dst
->max_hw_transfer
,
128 src
->max_hw_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
);
134 dst
->max_hw_iov
= MIN_NON_ZERO(dst
->max_hw_iov
, src
->max_hw_iov
);
137 typedef struct BdrvRefreshLimitsState
{
138 BlockDriverState
*bs
;
140 } BdrvRefreshLimitsState
;
142 static void bdrv_refresh_limits_abort(void *opaque
)
144 BdrvRefreshLimitsState
*s
= opaque
;
146 s
->bs
->bl
= s
->old_bl
;
149 static TransactionActionDrv bdrv_refresh_limits_drv
= {
150 .abort
= bdrv_refresh_limits_abort
,
154 /* @tran is allowed to be NULL, in this case no rollback is possible. */
155 void bdrv_refresh_limits(BlockDriverState
*bs
, Transaction
*tran
, Error
**errp
)
158 BlockDriver
*drv
= bs
->drv
;
163 assume_graph_lock(); /* FIXME */
166 BdrvRefreshLimitsState
*s
= g_new(BdrvRefreshLimitsState
, 1);
167 *s
= (BdrvRefreshLimitsState
) {
171 tran_add(tran
, &bdrv_refresh_limits_drv
, s
);
174 memset(&bs
->bl
, 0, sizeof(bs
->bl
));
180 /* Default alignment based on whether driver has byte interface */
181 bs
->bl
.request_alignment
= (drv
->bdrv_co_preadv
||
182 drv
->bdrv_aio_preadv
||
183 drv
->bdrv_co_preadv_part
) ? 1 : 512;
185 /* Take some limits from the children as a default */
187 QLIST_FOREACH(c
, &bs
->children
, next
) {
188 if (c
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_FILTERED
| BDRV_CHILD_COW
))
190 bdrv_merge_limits(&bs
->bl
, &c
->bs
->bl
);
194 if (c
->role
& BDRV_CHILD_FILTERED
) {
195 bs
->bl
.has_variable_length
|= c
->bs
->bl
.has_variable_length
;
200 bs
->bl
.min_mem_alignment
= 512;
201 bs
->bl
.opt_mem_alignment
= qemu_real_host_page_size();
203 /* Safe default since most protocols use readv()/writev()/etc */
204 bs
->bl
.max_iov
= IOV_MAX
;
207 /* Then let the driver override it */
208 if (drv
->bdrv_refresh_limits
) {
209 drv
->bdrv_refresh_limits(bs
, errp
);
215 if (bs
->bl
.request_alignment
> BDRV_MAX_ALIGNMENT
) {
216 error_setg(errp
, "Driver requires too large request alignment");
221 * The copy-on-read flag is actually a reference count so multiple users may
222 * use the feature without worrying about clobbering its previous state.
223 * Copy-on-read stays enabled until all users have called to disable it.
225 void bdrv_enable_copy_on_read(BlockDriverState
*bs
)
228 qatomic_inc(&bs
->copy_on_read
);
231 void bdrv_disable_copy_on_read(BlockDriverState
*bs
)
233 int old
= qatomic_fetch_dec(&bs
->copy_on_read
);
240 BlockDriverState
*bs
;
247 /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
248 bool bdrv_drain_poll(BlockDriverState
*bs
, BdrvChild
*ignore_parent
,
249 bool ignore_bds_parents
)
253 if (bdrv_parent_drained_poll(bs
, ignore_parent
, ignore_bds_parents
)) {
257 if (qatomic_read(&bs
->in_flight
)) {
264 static bool bdrv_drain_poll_top_level(BlockDriverState
*bs
,
265 BdrvChild
*ignore_parent
)
267 return bdrv_drain_poll(bs
, ignore_parent
, false);
270 static void bdrv_do_drained_begin(BlockDriverState
*bs
, BdrvChild
*parent
,
272 static void bdrv_do_drained_end(BlockDriverState
*bs
, BdrvChild
*parent
);
274 static void bdrv_co_drain_bh_cb(void *opaque
)
276 BdrvCoDrainData
*data
= opaque
;
277 Coroutine
*co
= data
->co
;
278 BlockDriverState
*bs
= data
->bs
;
281 AioContext
*ctx
= bdrv_get_aio_context(bs
);
282 aio_context_acquire(ctx
);
283 bdrv_dec_in_flight(bs
);
285 bdrv_do_drained_begin(bs
, data
->parent
, data
->poll
);
288 bdrv_do_drained_end(bs
, data
->parent
);
290 aio_context_release(ctx
);
293 bdrv_drain_all_begin();
300 static void coroutine_fn
bdrv_co_yield_to_drain(BlockDriverState
*bs
,
305 BdrvCoDrainData data
;
306 Coroutine
*self
= qemu_coroutine_self();
307 AioContext
*ctx
= bdrv_get_aio_context(bs
);
308 AioContext
*co_ctx
= qemu_coroutine_get_aio_context(self
);
310 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
311 * other coroutines run if they were queued by aio_co_enter(). */
313 assert(qemu_in_coroutine());
314 data
= (BdrvCoDrainData
) {
324 bdrv_inc_in_flight(bs
);
328 * Temporarily drop the lock across yield or we would get deadlocks.
329 * bdrv_co_drain_bh_cb() reaquires the lock as needed.
331 * When we yield below, the lock for the current context will be
332 * released, so if this is actually the lock that protects bs, don't drop
336 aio_context_release(ctx
);
338 replay_bh_schedule_oneshot_event(ctx
, bdrv_co_drain_bh_cb
, &data
);
340 qemu_coroutine_yield();
341 /* If we are resumed from some other event (such as an aio completion or a
342 * timer callback), it is a bug in the caller that should be fixed. */
345 /* Reaquire the AioContext of bs if we dropped it */
347 aio_context_acquire(ctx
);
351 static void bdrv_do_drained_begin(BlockDriverState
*bs
, BdrvChild
*parent
,
356 if (qemu_in_coroutine()) {
357 bdrv_co_yield_to_drain(bs
, true, parent
, poll
);
361 /* Stop things in parent-to-child order */
362 if (qatomic_fetch_inc(&bs
->quiesce_counter
) == 0) {
363 aio_disable_external(bdrv_get_aio_context(bs
));
364 bdrv_parent_drained_begin(bs
, parent
);
365 if (bs
->drv
&& bs
->drv
->bdrv_drain_begin
) {
366 bs
->drv
->bdrv_drain_begin(bs
);
371 * Wait for drained requests to finish.
373 * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
374 * call is needed so things in this AioContext can make progress even
375 * though we don't return to the main AioContext loop - this automatically
376 * includes other nodes in the same AioContext and therefore all child
380 BDRV_POLL_WHILE(bs
, bdrv_drain_poll_top_level(bs
, parent
));
384 void bdrv_do_drained_begin_quiesce(BlockDriverState
*bs
, BdrvChild
*parent
)
386 bdrv_do_drained_begin(bs
, parent
, false);
389 void bdrv_drained_begin(BlockDriverState
*bs
)
392 bdrv_do_drained_begin(bs
, NULL
, true);
396 * This function does not poll, nor must any of its recursively called
399 static void bdrv_do_drained_end(BlockDriverState
*bs
, BdrvChild
*parent
)
401 int old_quiesce_counter
;
403 if (qemu_in_coroutine()) {
404 bdrv_co_yield_to_drain(bs
, false, parent
, false);
407 assert(bs
->quiesce_counter
> 0);
409 /* Re-enable things in child-to-parent order */
410 old_quiesce_counter
= qatomic_fetch_dec(&bs
->quiesce_counter
);
411 if (old_quiesce_counter
== 1) {
412 if (bs
->drv
&& bs
->drv
->bdrv_drain_end
) {
413 bs
->drv
->bdrv_drain_end(bs
);
415 bdrv_parent_drained_end(bs
, parent
);
416 aio_enable_external(bdrv_get_aio_context(bs
));
420 void bdrv_drained_end(BlockDriverState
*bs
)
423 bdrv_do_drained_end(bs
, NULL
);
426 void bdrv_drain(BlockDriverState
*bs
)
429 bdrv_drained_begin(bs
);
430 bdrv_drained_end(bs
);
433 static void bdrv_drain_assert_idle(BlockDriverState
*bs
)
435 BdrvChild
*child
, *next
;
437 assert(qatomic_read(&bs
->in_flight
) == 0);
438 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
439 bdrv_drain_assert_idle(child
->bs
);
443 unsigned int bdrv_drain_all_count
= 0;
445 static bool bdrv_drain_all_poll(void)
447 BlockDriverState
*bs
= NULL
;
451 /* bdrv_drain_poll() can't make changes to the graph and we are holding the
452 * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
453 while ((bs
= bdrv_next_all_states(bs
))) {
454 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
455 aio_context_acquire(aio_context
);
456 result
|= bdrv_drain_poll(bs
, NULL
, true);
457 aio_context_release(aio_context
);
464 * Wait for pending requests to complete across all BlockDriverStates
466 * This function does not flush data to disk, use bdrv_flush_all() for that
467 * after calling this function.
469 * This pauses all block jobs and disables external clients. It must
470 * be paired with bdrv_drain_all_end().
472 * NOTE: no new block jobs or BlockDriverStates can be created between
473 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
475 void bdrv_drain_all_begin_nopoll(void)
477 BlockDriverState
*bs
= NULL
;
481 * bdrv queue is managed by record/replay,
482 * waiting for finishing the I/O requests may
485 if (replay_events_enabled()) {
489 /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
490 * loop AioContext, so make sure we're in the main context. */
491 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
492 assert(bdrv_drain_all_count
< INT_MAX
);
493 bdrv_drain_all_count
++;
495 /* Quiesce all nodes, without polling in-flight requests yet. The graph
496 * cannot change during this loop. */
497 while ((bs
= bdrv_next_all_states(bs
))) {
498 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
500 aio_context_acquire(aio_context
);
501 bdrv_do_drained_begin(bs
, NULL
, false);
502 aio_context_release(aio_context
);
506 void bdrv_drain_all_begin(void)
508 BlockDriverState
*bs
= NULL
;
510 if (qemu_in_coroutine()) {
511 bdrv_co_yield_to_drain(NULL
, true, NULL
, true);
516 * bdrv queue is managed by record/replay,
517 * waiting for finishing the I/O requests may
520 if (replay_events_enabled()) {
524 bdrv_drain_all_begin_nopoll();
526 /* Now poll the in-flight requests */
527 AIO_WAIT_WHILE_UNLOCKED(NULL
, bdrv_drain_all_poll());
529 while ((bs
= bdrv_next_all_states(bs
))) {
530 bdrv_drain_assert_idle(bs
);
534 void bdrv_drain_all_end_quiesce(BlockDriverState
*bs
)
538 g_assert(bs
->quiesce_counter
> 0);
539 g_assert(!bs
->refcnt
);
541 while (bs
->quiesce_counter
) {
542 bdrv_do_drained_end(bs
, NULL
);
546 void bdrv_drain_all_end(void)
548 BlockDriverState
*bs
= NULL
;
552 * bdrv queue is managed by record/replay,
553 * waiting for finishing the I/O requests may
556 if (replay_events_enabled()) {
560 while ((bs
= bdrv_next_all_states(bs
))) {
561 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
563 aio_context_acquire(aio_context
);
564 bdrv_do_drained_end(bs
, NULL
);
565 aio_context_release(aio_context
);
568 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
569 assert(bdrv_drain_all_count
> 0);
570 bdrv_drain_all_count
--;
573 void bdrv_drain_all(void)
576 bdrv_drain_all_begin();
577 bdrv_drain_all_end();
581 * Remove an active request from the tracked requests list
583 * This function should be called when a tracked request is completing.
585 static void coroutine_fn
tracked_request_end(BdrvTrackedRequest
*req
)
587 if (req
->serialising
) {
588 qatomic_dec(&req
->bs
->serialising_in_flight
);
591 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
592 QLIST_REMOVE(req
, list
);
593 qemu_co_queue_restart_all(&req
->wait_queue
);
594 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
598 * Add an active request to the tracked requests list
600 static void coroutine_fn
tracked_request_begin(BdrvTrackedRequest
*req
,
601 BlockDriverState
*bs
,
604 enum BdrvTrackedRequestType type
)
606 bdrv_check_request(offset
, bytes
, &error_abort
);
608 *req
= (BdrvTrackedRequest
){
613 .co
= qemu_coroutine_self(),
614 .serialising
= false,
615 .overlap_offset
= offset
,
616 .overlap_bytes
= bytes
,
619 qemu_co_queue_init(&req
->wait_queue
);
621 qemu_co_mutex_lock(&bs
->reqs_lock
);
622 QLIST_INSERT_HEAD(&bs
->tracked_requests
, req
, list
);
623 qemu_co_mutex_unlock(&bs
->reqs_lock
);
626 static bool tracked_request_overlaps(BdrvTrackedRequest
*req
,
627 int64_t offset
, int64_t bytes
)
629 bdrv_check_request(offset
, bytes
, &error_abort
);
632 if (offset
>= req
->overlap_offset
+ req
->overlap_bytes
) {
636 if (req
->overlap_offset
>= offset
+ bytes
) {
642 /* Called with self->bs->reqs_lock held */
643 static coroutine_fn BdrvTrackedRequest
*
644 bdrv_find_conflicting_request(BdrvTrackedRequest
*self
)
646 BdrvTrackedRequest
*req
;
648 QLIST_FOREACH(req
, &self
->bs
->tracked_requests
, list
) {
649 if (req
== self
|| (!req
->serialising
&& !self
->serialising
)) {
652 if (tracked_request_overlaps(req
, self
->overlap_offset
,
653 self
->overlap_bytes
))
656 * Hitting this means there was a reentrant request, for
657 * example, a block driver issuing nested requests. This must
658 * never happen since it means deadlock.
660 assert(qemu_coroutine_self() != req
->co
);
663 * If the request is already (indirectly) waiting for us, or
664 * will wait for us as soon as it wakes up, then just go on
665 * (instead of producing a deadlock in the former case).
667 if (!req
->waiting_for
) {
676 /* Called with self->bs->reqs_lock held */
677 static void coroutine_fn
678 bdrv_wait_serialising_requests_locked(BdrvTrackedRequest
*self
)
680 BdrvTrackedRequest
*req
;
682 while ((req
= bdrv_find_conflicting_request(self
))) {
683 self
->waiting_for
= req
;
684 qemu_co_queue_wait(&req
->wait_queue
, &self
->bs
->reqs_lock
);
685 self
->waiting_for
= NULL
;
689 /* Called with req->bs->reqs_lock held */
690 static void tracked_request_set_serialising(BdrvTrackedRequest
*req
,
693 int64_t overlap_offset
= req
->offset
& ~(align
- 1);
694 int64_t overlap_bytes
=
695 ROUND_UP(req
->offset
+ req
->bytes
, align
) - overlap_offset
;
697 bdrv_check_request(req
->offset
, req
->bytes
, &error_abort
);
699 if (!req
->serialising
) {
700 qatomic_inc(&req
->bs
->serialising_in_flight
);
701 req
->serialising
= true;
704 req
->overlap_offset
= MIN(req
->overlap_offset
, overlap_offset
);
705 req
->overlap_bytes
= MAX(req
->overlap_bytes
, overlap_bytes
);
709 * Return the tracked request on @bs for the current coroutine, or
710 * NULL if there is none.
712 BdrvTrackedRequest
*coroutine_fn
bdrv_co_get_self_request(BlockDriverState
*bs
)
714 BdrvTrackedRequest
*req
;
715 Coroutine
*self
= qemu_coroutine_self();
718 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
719 if (req
->co
== self
) {
728 * Round a region to cluster boundaries
730 void coroutine_fn
bdrv_round_to_clusters(BlockDriverState
*bs
,
731 int64_t offset
, int64_t bytes
,
732 int64_t *cluster_offset
,
733 int64_t *cluster_bytes
)
737 if (bdrv_co_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
738 *cluster_offset
= offset
;
739 *cluster_bytes
= bytes
;
741 int64_t c
= bdi
.cluster_size
;
742 *cluster_offset
= QEMU_ALIGN_DOWN(offset
, c
);
743 *cluster_bytes
= QEMU_ALIGN_UP(offset
- *cluster_offset
+ bytes
, c
);
747 static coroutine_fn
int bdrv_get_cluster_size(BlockDriverState
*bs
)
752 ret
= bdrv_co_get_info(bs
, &bdi
);
753 if (ret
< 0 || bdi
.cluster_size
== 0) {
754 return bs
->bl
.request_alignment
;
756 return bdi
.cluster_size
;
760 void bdrv_inc_in_flight(BlockDriverState
*bs
)
763 qatomic_inc(&bs
->in_flight
);
766 void bdrv_wakeup(BlockDriverState
*bs
)
772 void bdrv_dec_in_flight(BlockDriverState
*bs
)
775 qatomic_dec(&bs
->in_flight
);
779 static void coroutine_fn
780 bdrv_wait_serialising_requests(BdrvTrackedRequest
*self
)
782 BlockDriverState
*bs
= self
->bs
;
784 if (!qatomic_read(&bs
->serialising_in_flight
)) {
788 qemu_co_mutex_lock(&bs
->reqs_lock
);
789 bdrv_wait_serialising_requests_locked(self
);
790 qemu_co_mutex_unlock(&bs
->reqs_lock
);
793 void coroutine_fn
bdrv_make_request_serialising(BdrvTrackedRequest
*req
,
798 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
800 tracked_request_set_serialising(req
, align
);
801 bdrv_wait_serialising_requests_locked(req
);
803 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
806 int bdrv_check_qiov_request(int64_t offset
, int64_t bytes
,
807 QEMUIOVector
*qiov
, size_t qiov_offset
,
811 * Check generic offset/bytes correctness
815 error_setg(errp
, "offset is negative: %" PRIi64
, offset
);
820 error_setg(errp
, "bytes is negative: %" PRIi64
, bytes
);
824 if (bytes
> BDRV_MAX_LENGTH
) {
825 error_setg(errp
, "bytes(%" PRIi64
") exceeds maximum(%" PRIi64
")",
826 bytes
, BDRV_MAX_LENGTH
);
830 if (offset
> BDRV_MAX_LENGTH
) {
831 error_setg(errp
, "offset(%" PRIi64
") exceeds maximum(%" PRIi64
")",
832 offset
, BDRV_MAX_LENGTH
);
836 if (offset
> BDRV_MAX_LENGTH
- bytes
) {
837 error_setg(errp
, "sum of offset(%" PRIi64
") and bytes(%" PRIi64
") "
838 "exceeds maximum(%" PRIi64
")", offset
, bytes
,
848 * Check qiov and qiov_offset
851 if (qiov_offset
> qiov
->size
) {
852 error_setg(errp
, "qiov_offset(%zu) overflow io vector size(%zu)",
853 qiov_offset
, qiov
->size
);
857 if (bytes
> qiov
->size
- qiov_offset
) {
858 error_setg(errp
, "bytes(%" PRIi64
") + qiov_offset(%zu) overflow io "
859 "vector size(%zu)", bytes
, qiov_offset
, qiov
->size
);
866 int bdrv_check_request(int64_t offset
, int64_t bytes
, Error
**errp
)
868 return bdrv_check_qiov_request(offset
, bytes
, NULL
, 0, errp
);
871 static int bdrv_check_request32(int64_t offset
, int64_t bytes
,
872 QEMUIOVector
*qiov
, size_t qiov_offset
)
874 int ret
= bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, NULL
);
879 if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
887 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
888 * The operation is sped up by checking the block status and only writing
889 * zeroes to the device if they currently do not return zeroes. Optional
890 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
893 * Returns < 0 on error, 0 on success. For error codes see bdrv_pwrite().
895 int bdrv_make_zero(BdrvChild
*child
, BdrvRequestFlags flags
)
898 int64_t target_size
, bytes
, offset
= 0;
899 BlockDriverState
*bs
= child
->bs
;
902 target_size
= bdrv_getlength(bs
);
903 if (target_size
< 0) {
908 bytes
= MIN(target_size
- offset
, BDRV_REQUEST_MAX_BYTES
);
912 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, NULL
, NULL
);
916 if (ret
& BDRV_BLOCK_ZERO
) {
920 ret
= bdrv_pwrite_zeroes(child
, offset
, bytes
, flags
);
929 * Writes to the file and ensures that no writes are reordered across this
930 * request (acts as a barrier)
932 * Returns 0 on success, -errno in error cases.
934 int coroutine_fn
bdrv_co_pwrite_sync(BdrvChild
*child
, int64_t offset
,
935 int64_t bytes
, const void *buf
,
936 BdrvRequestFlags flags
)
940 assert_bdrv_graph_readable();
942 ret
= bdrv_co_pwrite(child
, offset
, bytes
, buf
, flags
);
947 ret
= bdrv_co_flush(child
->bs
);
955 typedef struct CoroutineIOCompletion
{
956 Coroutine
*coroutine
;
958 } CoroutineIOCompletion
;
960 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
962 CoroutineIOCompletion
*co
= opaque
;
965 aio_co_wake(co
->coroutine
);
968 static int coroutine_fn GRAPH_RDLOCK
969 bdrv_driver_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
970 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
972 BlockDriver
*drv
= bs
->drv
;
974 unsigned int nb_sectors
;
975 QEMUIOVector local_qiov
;
977 assert_bdrv_graph_readable();
979 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
980 assert(!(flags
& ~bs
->supported_read_flags
));
986 if (drv
->bdrv_co_preadv_part
) {
987 return drv
->bdrv_co_preadv_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
991 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
992 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
996 if (drv
->bdrv_co_preadv
) {
997 ret
= drv
->bdrv_co_preadv(bs
, offset
, bytes
, qiov
, flags
);
1001 if (drv
->bdrv_aio_preadv
) {
1003 CoroutineIOCompletion co
= {
1004 .coroutine
= qemu_coroutine_self(),
1007 acb
= drv
->bdrv_aio_preadv(bs
, offset
, bytes
, qiov
, flags
,
1008 bdrv_co_io_em_complete
, &co
);
1013 qemu_coroutine_yield();
1019 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1020 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1022 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1023 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1024 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1025 assert(drv
->bdrv_co_readv
);
1027 ret
= drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
1030 if (qiov
== &local_qiov
) {
1031 qemu_iovec_destroy(&local_qiov
);
1037 static int coroutine_fn GRAPH_RDLOCK
1038 bdrv_driver_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1039 QEMUIOVector
*qiov
, size_t qiov_offset
,
1040 BdrvRequestFlags flags
)
1042 BlockDriver
*drv
= bs
->drv
;
1043 bool emulate_fua
= false;
1045 unsigned int nb_sectors
;
1046 QEMUIOVector local_qiov
;
1048 assert_bdrv_graph_readable();
1050 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1056 if ((flags
& BDRV_REQ_FUA
) &&
1057 (~bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1058 flags
&= ~BDRV_REQ_FUA
;
1062 flags
&= bs
->supported_write_flags
;
1064 if (drv
->bdrv_co_pwritev_part
) {
1065 ret
= drv
->bdrv_co_pwritev_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
1070 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
1071 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1075 if (drv
->bdrv_co_pwritev
) {
1076 ret
= drv
->bdrv_co_pwritev(bs
, offset
, bytes
, qiov
, flags
);
1080 if (drv
->bdrv_aio_pwritev
) {
1082 CoroutineIOCompletion co
= {
1083 .coroutine
= qemu_coroutine_self(),
1086 acb
= drv
->bdrv_aio_pwritev(bs
, offset
, bytes
, qiov
, flags
,
1087 bdrv_co_io_em_complete
, &co
);
1091 qemu_coroutine_yield();
1097 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1098 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1100 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1101 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1102 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1104 assert(drv
->bdrv_co_writev
);
1105 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
, flags
);
1108 if (ret
== 0 && emulate_fua
) {
1109 ret
= bdrv_co_flush(bs
);
1112 if (qiov
== &local_qiov
) {
1113 qemu_iovec_destroy(&local_qiov
);
1119 static int coroutine_fn GRAPH_RDLOCK
1120 bdrv_driver_pwritev_compressed(BlockDriverState
*bs
, int64_t offset
,
1121 int64_t bytes
, QEMUIOVector
*qiov
,
1124 BlockDriver
*drv
= bs
->drv
;
1125 QEMUIOVector local_qiov
;
1127 assert_bdrv_graph_readable();
1129 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1135 if (!block_driver_can_compress(drv
)) {
1139 if (drv
->bdrv_co_pwritev_compressed_part
) {
1140 return drv
->bdrv_co_pwritev_compressed_part(bs
, offset
, bytes
,
1144 if (qiov_offset
== 0) {
1145 return drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, qiov
);
1148 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1149 ret
= drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, &local_qiov
);
1150 qemu_iovec_destroy(&local_qiov
);
1155 static int coroutine_fn GRAPH_RDLOCK
1156 bdrv_co_do_copy_on_readv(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1157 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
1159 BlockDriverState
*bs
= child
->bs
;
1161 /* Perform I/O through a temporary buffer so that users who scribble over
1162 * their read buffer while the operation is in progress do not end up
1163 * modifying the image file. This is critical for zero-copy guest I/O
1164 * where anything might happen inside guest memory.
1166 void *bounce_buffer
= NULL
;
1168 BlockDriver
*drv
= bs
->drv
;
1169 int64_t cluster_offset
;
1170 int64_t cluster_bytes
;
1173 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
,
1174 BDRV_REQUEST_MAX_BYTES
);
1175 int64_t progress
= 0;
1178 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1185 * Do not write anything when the BDS is inactive. That is not
1186 * allowed, and it would not help.
1188 skip_write
= (bs
->open_flags
& BDRV_O_INACTIVE
);
1190 /* FIXME We cannot require callers to have write permissions when all they
1191 * are doing is a read request. If we did things right, write permissions
1192 * would be obtained anyway, but internally by the copy-on-read code. As
1193 * long as it is implemented here rather than in a separate filter driver,
1194 * the copy-on-read code doesn't have its own BdrvChild, however, for which
1195 * it could request permissions. Therefore we have to bypass the permission
1196 * system for the moment. */
1197 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1199 /* Cover entire cluster so no additional backing file I/O is required when
1200 * allocating cluster in the image file. Note that this value may exceed
1201 * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1202 * is one reason we loop rather than doing it all at once.
1204 bdrv_round_to_clusters(bs
, offset
, bytes
, &cluster_offset
, &cluster_bytes
);
1205 skip_bytes
= offset
- cluster_offset
;
1207 trace_bdrv_co_do_copy_on_readv(bs
, offset
, bytes
,
1208 cluster_offset
, cluster_bytes
);
1210 while (cluster_bytes
) {
1214 ret
= 1; /* "already allocated", so nothing will be copied */
1215 pnum
= MIN(cluster_bytes
, max_transfer
);
1217 ret
= bdrv_is_allocated(bs
, cluster_offset
,
1218 MIN(cluster_bytes
, max_transfer
), &pnum
);
1221 * Safe to treat errors in querying allocation as if
1222 * unallocated; we'll probably fail again soon on the
1223 * read, but at least that will set a decent errno.
1225 pnum
= MIN(cluster_bytes
, max_transfer
);
1228 /* Stop at EOF if the image ends in the middle of the cluster */
1229 if (ret
== 0 && pnum
== 0) {
1230 assert(progress
>= bytes
);
1234 assert(skip_bytes
< pnum
);
1238 QEMUIOVector local_qiov
;
1240 /* Must copy-on-read; use the bounce buffer */
1241 pnum
= MIN(pnum
, MAX_BOUNCE_BUFFER
);
1242 if (!bounce_buffer
) {
1243 int64_t max_we_need
= MAX(pnum
, cluster_bytes
- pnum
);
1244 int64_t max_allowed
= MIN(max_transfer
, MAX_BOUNCE_BUFFER
);
1245 int64_t bounce_buffer_len
= MIN(max_we_need
, max_allowed
);
1247 bounce_buffer
= qemu_try_blockalign(bs
, bounce_buffer_len
);
1248 if (!bounce_buffer
) {
1253 qemu_iovec_init_buf(&local_qiov
, bounce_buffer
, pnum
);
1255 ret
= bdrv_driver_preadv(bs
, cluster_offset
, pnum
,
1261 bdrv_co_debug_event(bs
, BLKDBG_COR_WRITE
);
1262 if (drv
->bdrv_co_pwrite_zeroes
&&
1263 buffer_is_zero(bounce_buffer
, pnum
)) {
1264 /* FIXME: Should we (perhaps conditionally) be setting
1265 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1266 * that still correctly reads as zero? */
1267 ret
= bdrv_co_do_pwrite_zeroes(bs
, cluster_offset
, pnum
,
1268 BDRV_REQ_WRITE_UNCHANGED
);
1270 /* This does not change the data on the disk, it is not
1271 * necessary to flush even in cache=writethrough mode.
1273 ret
= bdrv_driver_pwritev(bs
, cluster_offset
, pnum
,
1275 BDRV_REQ_WRITE_UNCHANGED
);
1279 /* It might be okay to ignore write errors for guest
1280 * requests. If this is a deliberate copy-on-read
1281 * then we don't want to ignore the error. Simply
1282 * report it in all cases.
1287 if (!(flags
& BDRV_REQ_PREFETCH
)) {
1288 qemu_iovec_from_buf(qiov
, qiov_offset
+ progress
,
1289 bounce_buffer
+ skip_bytes
,
1290 MIN(pnum
- skip_bytes
, bytes
- progress
));
1292 } else if (!(flags
& BDRV_REQ_PREFETCH
)) {
1293 /* Read directly into the destination */
1294 ret
= bdrv_driver_preadv(bs
, offset
+ progress
,
1295 MIN(pnum
- skip_bytes
, bytes
- progress
),
1296 qiov
, qiov_offset
+ progress
, 0);
1302 cluster_offset
+= pnum
;
1303 cluster_bytes
-= pnum
;
1304 progress
+= pnum
- skip_bytes
;
1310 qemu_vfree(bounce_buffer
);
1315 * Forwards an already correctly aligned request to the BlockDriver. This
1316 * handles copy on read, zeroing after EOF, and fragmentation of large
1317 * reads; any other features must be implemented by the caller.
1319 static int coroutine_fn GRAPH_RDLOCK
1320 bdrv_aligned_preadv(BdrvChild
*child
, BdrvTrackedRequest
*req
,
1321 int64_t offset
, int64_t bytes
, int64_t align
,
1322 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
1324 BlockDriverState
*bs
= child
->bs
;
1325 int64_t total_bytes
, max_bytes
;
1327 int64_t bytes_remaining
= bytes
;
1330 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1331 assert(is_power_of_2(align
));
1332 assert((offset
& (align
- 1)) == 0);
1333 assert((bytes
& (align
- 1)) == 0);
1334 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1335 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1339 * TODO: We would need a per-BDS .supported_read_flags and
1340 * potential fallback support, if we ever implement any read flags
1341 * to pass through to drivers. For now, there aren't any
1342 * passthrough flags except the BDRV_REQ_REGISTERED_BUF optimization hint.
1344 assert(!(flags
& ~(BDRV_REQ_COPY_ON_READ
| BDRV_REQ_PREFETCH
|
1345 BDRV_REQ_REGISTERED_BUF
)));
1347 /* Handle Copy on Read and associated serialisation */
1348 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1349 /* If we touch the same cluster it counts as an overlap. This
1350 * guarantees that allocating writes will be serialized and not race
1351 * with each other for the same cluster. For example, in copy-on-read
1352 * it ensures that the CoR read and write operations are atomic and
1353 * guest writes cannot interleave between them. */
1354 bdrv_make_request_serialising(req
, bdrv_get_cluster_size(bs
));
1356 bdrv_wait_serialising_requests(req
);
1359 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1362 /* The flag BDRV_REQ_COPY_ON_READ has reached its addressee */
1363 flags
&= ~BDRV_REQ_COPY_ON_READ
;
1365 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &pnum
);
1370 if (!ret
|| pnum
!= bytes
) {
1371 ret
= bdrv_co_do_copy_on_readv(child
, offset
, bytes
,
1372 qiov
, qiov_offset
, flags
);
1374 } else if (flags
& BDRV_REQ_PREFETCH
) {
1379 /* Forward the request to the BlockDriver, possibly fragmenting it */
1380 total_bytes
= bdrv_getlength(bs
);
1381 if (total_bytes
< 0) {
1386 assert(!(flags
& ~(bs
->supported_read_flags
| BDRV_REQ_REGISTERED_BUF
)));
1388 max_bytes
= ROUND_UP(MAX(0, total_bytes
- offset
), align
);
1389 if (bytes
<= max_bytes
&& bytes
<= max_transfer
) {
1390 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1394 while (bytes_remaining
) {
1398 num
= MIN(bytes_remaining
, MIN(max_bytes
, max_transfer
));
1401 ret
= bdrv_driver_preadv(bs
, offset
+ bytes
- bytes_remaining
,
1403 qiov_offset
+ bytes
- bytes_remaining
,
1407 num
= bytes_remaining
;
1408 ret
= qemu_iovec_memset(qiov
, qiov_offset
+ bytes
- bytes_remaining
,
1409 0, bytes_remaining
);
1414 bytes_remaining
-= num
;
1418 return ret
< 0 ? ret
: 0;
1424 * |<---- align ----->| |<----- align ---->|
1425 * |<- head ->|<------------- bytes ------------->|<-- tail -->|
1427 * -*----------$-------*-------- ... --------*-----$------------*---
1429 * | offset | | end |
1430 * ALIGN_DOWN(offset) ALIGN_UP(offset) ALIGN_DOWN(end) ALIGN_UP(end)
1431 * [buf ... ) [tail_buf )
1433 * @buf is an aligned allocation needed to store @head and @tail paddings. @head
1434 * is placed at the beginning of @buf and @tail at the @end.
1436 * @tail_buf is a pointer to sub-buffer, corresponding to align-sized chunk
1437 * around tail, if tail exists.
1439 * @merge_reads is true for small requests,
1440 * if @buf_len == @head + bytes + @tail. In this case it is possible that both
1441 * head and tail exist but @buf_len == align and @tail_buf == @buf.
1443 typedef struct BdrvRequestPadding
{
1450 QEMUIOVector local_qiov
;
1451 } BdrvRequestPadding
;
1453 static bool bdrv_init_padding(BlockDriverState
*bs
,
1454 int64_t offset
, int64_t bytes
,
1455 BdrvRequestPadding
*pad
)
1457 int64_t align
= bs
->bl
.request_alignment
;
1460 bdrv_check_request(offset
, bytes
, &error_abort
);
1461 assert(align
<= INT_MAX
); /* documented in block/block_int.h */
1462 assert(align
<= SIZE_MAX
/ 2); /* so we can allocate the buffer */
1464 memset(pad
, 0, sizeof(*pad
));
1466 pad
->head
= offset
& (align
- 1);
1467 pad
->tail
= ((offset
+ bytes
) & (align
- 1));
1469 pad
->tail
= align
- pad
->tail
;
1472 if (!pad
->head
&& !pad
->tail
) {
1476 assert(bytes
); /* Nothing good in aligning zero-length requests */
1478 sum
= pad
->head
+ bytes
+ pad
->tail
;
1479 pad
->buf_len
= (sum
> align
&& pad
->head
&& pad
->tail
) ? 2 * align
: align
;
1480 pad
->buf
= qemu_blockalign(bs
, pad
->buf_len
);
1481 pad
->merge_reads
= sum
== pad
->buf_len
;
1483 pad
->tail_buf
= pad
->buf
+ pad
->buf_len
- align
;
1489 static int coroutine_fn GRAPH_RDLOCK
1490 bdrv_padding_rmw_read(BdrvChild
*child
, BdrvTrackedRequest
*req
,
1491 BdrvRequestPadding
*pad
, bool zero_middle
)
1493 QEMUIOVector local_qiov
;
1494 BlockDriverState
*bs
= child
->bs
;
1495 uint64_t align
= bs
->bl
.request_alignment
;
1498 assert(req
->serialising
&& pad
->buf
);
1500 if (pad
->head
|| pad
->merge_reads
) {
1501 int64_t bytes
= pad
->merge_reads
? pad
->buf_len
: align
;
1503 qemu_iovec_init_buf(&local_qiov
, pad
->buf
, bytes
);
1506 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1508 if (pad
->merge_reads
&& pad
->tail
) {
1509 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1511 ret
= bdrv_aligned_preadv(child
, req
, req
->overlap_offset
, bytes
,
1512 align
, &local_qiov
, 0, 0);
1517 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1519 if (pad
->merge_reads
&& pad
->tail
) {
1520 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1523 if (pad
->merge_reads
) {
1529 qemu_iovec_init_buf(&local_qiov
, pad
->tail_buf
, align
);
1531 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1532 ret
= bdrv_aligned_preadv(
1534 req
->overlap_offset
+ req
->overlap_bytes
- align
,
1535 align
, align
, &local_qiov
, 0, 0);
1539 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1544 memset(pad
->buf
+ pad
->head
, 0, pad
->buf_len
- pad
->head
- pad
->tail
);
1550 static void bdrv_padding_destroy(BdrvRequestPadding
*pad
)
1553 qemu_vfree(pad
->buf
);
1554 qemu_iovec_destroy(&pad
->local_qiov
);
1556 memset(pad
, 0, sizeof(*pad
));
1562 * Exchange request parameters with padded request if needed. Don't include RMW
1563 * read of padding, bdrv_padding_rmw_read() should be called separately if
1566 * Request parameters (@qiov, &qiov_offset, &offset, &bytes) are in-out:
1567 * - on function start they represent original request
1568 * - on failure or when padding is not needed they are unchanged
1569 * - on success when padding is needed they represent padded request
1571 static int bdrv_pad_request(BlockDriverState
*bs
,
1572 QEMUIOVector
**qiov
, size_t *qiov_offset
,
1573 int64_t *offset
, int64_t *bytes
,
1574 BdrvRequestPadding
*pad
, bool *padded
,
1575 BdrvRequestFlags
*flags
)
1579 bdrv_check_qiov_request(*offset
, *bytes
, *qiov
, *qiov_offset
, &error_abort
);
1581 if (!bdrv_init_padding(bs
, *offset
, *bytes
, pad
)) {
1588 ret
= qemu_iovec_init_extended(&pad
->local_qiov
, pad
->buf
, pad
->head
,
1589 *qiov
, *qiov_offset
, *bytes
,
1590 pad
->buf
+ pad
->buf_len
- pad
->tail
,
1593 bdrv_padding_destroy(pad
);
1596 *bytes
+= pad
->head
+ pad
->tail
;
1597 *offset
-= pad
->head
;
1598 *qiov
= &pad
->local_qiov
;
1604 /* Can't use optimization hint with bounce buffer */
1605 *flags
&= ~BDRV_REQ_REGISTERED_BUF
;
1611 int coroutine_fn
bdrv_co_preadv(BdrvChild
*child
,
1612 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
1613 BdrvRequestFlags flags
)
1616 return bdrv_co_preadv_part(child
, offset
, bytes
, qiov
, 0, flags
);
1619 int coroutine_fn
bdrv_co_preadv_part(BdrvChild
*child
,
1620 int64_t offset
, int64_t bytes
,
1621 QEMUIOVector
*qiov
, size_t qiov_offset
,
1622 BdrvRequestFlags flags
)
1624 BlockDriverState
*bs
= child
->bs
;
1625 BdrvTrackedRequest req
;
1626 BdrvRequestPadding pad
;
1630 trace_bdrv_co_preadv_part(bs
, offset
, bytes
, flags
);
1632 if (!bdrv_co_is_inserted(bs
)) {
1636 ret
= bdrv_check_request32(offset
, bytes
, qiov
, qiov_offset
);
1641 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
1643 * Aligning zero request is nonsense. Even if driver has special meaning
1644 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
1645 * it to driver due to request_alignment.
1647 * Still, no reason to return an error if someone do unaligned
1648 * zero-length read occasionally.
1653 bdrv_inc_in_flight(bs
);
1655 /* Don't do copy-on-read if we read data before write operation */
1656 if (qatomic_read(&bs
->copy_on_read
)) {
1657 flags
|= BDRV_REQ_COPY_ON_READ
;
1660 ret
= bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
,
1666 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_READ
);
1667 ret
= bdrv_aligned_preadv(child
, &req
, offset
, bytes
,
1668 bs
->bl
.request_alignment
,
1669 qiov
, qiov_offset
, flags
);
1670 tracked_request_end(&req
);
1671 bdrv_padding_destroy(&pad
);
1674 bdrv_dec_in_flight(bs
);
1679 static int coroutine_fn GRAPH_RDLOCK
1680 bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1681 BdrvRequestFlags flags
)
1683 BlockDriver
*drv
= bs
->drv
;
1687 bool need_flush
= false;
1691 int64_t max_write_zeroes
= MIN_NON_ZERO(bs
->bl
.max_pwrite_zeroes
,
1693 int alignment
= MAX(bs
->bl
.pwrite_zeroes_alignment
,
1694 bs
->bl
.request_alignment
);
1695 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
, MAX_BOUNCE_BUFFER
);
1697 assert_bdrv_graph_readable();
1698 bdrv_check_request(offset
, bytes
, &error_abort
);
1704 if ((flags
& ~bs
->supported_zero_flags
) & BDRV_REQ_NO_FALLBACK
) {
1708 /* By definition there is no user buffer so this flag doesn't make sense */
1709 if (flags
& BDRV_REQ_REGISTERED_BUF
) {
1713 /* Invalidate the cached block-status data range if this write overlaps */
1714 bdrv_bsc_invalidate_range(bs
, offset
, bytes
);
1716 assert(alignment
% bs
->bl
.request_alignment
== 0);
1717 head
= offset
% alignment
;
1718 tail
= (offset
+ bytes
) % alignment
;
1719 max_write_zeroes
= QEMU_ALIGN_DOWN(max_write_zeroes
, alignment
);
1720 assert(max_write_zeroes
>= bs
->bl
.request_alignment
);
1722 while (bytes
> 0 && !ret
) {
1723 int64_t num
= bytes
;
1725 /* Align request. Block drivers can expect the "bulk" of the request
1726 * to be aligned, and that unaligned requests do not cross cluster
1730 /* Make a small request up to the first aligned sector. For
1731 * convenience, limit this request to max_transfer even if
1732 * we don't need to fall back to writes. */
1733 num
= MIN(MIN(bytes
, max_transfer
), alignment
- head
);
1734 head
= (head
+ num
) % alignment
;
1735 assert(num
< max_write_zeroes
);
1736 } else if (tail
&& num
> alignment
) {
1737 /* Shorten the request to the last aligned sector. */
1741 /* limit request size */
1742 if (num
> max_write_zeroes
) {
1743 num
= max_write_zeroes
;
1747 /* First try the efficient write zeroes operation */
1748 if (drv
->bdrv_co_pwrite_zeroes
) {
1749 ret
= drv
->bdrv_co_pwrite_zeroes(bs
, offset
, num
,
1750 flags
& bs
->supported_zero_flags
);
1751 if (ret
!= -ENOTSUP
&& (flags
& BDRV_REQ_FUA
) &&
1752 !(bs
->supported_zero_flags
& BDRV_REQ_FUA
)) {
1756 assert(!bs
->supported_zero_flags
);
1759 if (ret
== -ENOTSUP
&& !(flags
& BDRV_REQ_NO_FALLBACK
)) {
1760 /* Fall back to bounce buffer if write zeroes is unsupported */
1761 BdrvRequestFlags write_flags
= flags
& ~BDRV_REQ_ZERO_WRITE
;
1763 if ((flags
& BDRV_REQ_FUA
) &&
1764 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1765 /* No need for bdrv_driver_pwrite() to do a fallback
1766 * flush on each chunk; use just one at the end */
1767 write_flags
&= ~BDRV_REQ_FUA
;
1770 num
= MIN(num
, max_transfer
);
1772 buf
= qemu_try_blockalign0(bs
, num
);
1778 qemu_iovec_init_buf(&qiov
, buf
, num
);
1780 ret
= bdrv_driver_pwritev(bs
, offset
, num
, &qiov
, 0, write_flags
);
1782 /* Keep bounce buffer around if it is big enough for all
1783 * all future requests.
1785 if (num
< max_transfer
) {
1796 if (ret
== 0 && need_flush
) {
1797 ret
= bdrv_co_flush(bs
);
1803 static inline int coroutine_fn
1804 bdrv_co_write_req_prepare(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1805 BdrvTrackedRequest
*req
, int flags
)
1807 BlockDriverState
*bs
= child
->bs
;
1809 bdrv_check_request(offset
, bytes
, &error_abort
);
1811 if (bdrv_is_read_only(bs
)) {
1815 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
1816 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1817 assert(!(flags
& ~BDRV_REQ_MASK
));
1818 assert(!((flags
& BDRV_REQ_NO_WAIT
) && !(flags
& BDRV_REQ_SERIALISING
)));
1820 if (flags
& BDRV_REQ_SERIALISING
) {
1821 QEMU_LOCK_GUARD(&bs
->reqs_lock
);
1823 tracked_request_set_serialising(req
, bdrv_get_cluster_size(bs
));
1825 if ((flags
& BDRV_REQ_NO_WAIT
) && bdrv_find_conflicting_request(req
)) {
1829 bdrv_wait_serialising_requests_locked(req
);
1831 bdrv_wait_serialising_requests(req
);
1834 assert(req
->overlap_offset
<= offset
);
1835 assert(offset
+ bytes
<= req
->overlap_offset
+ req
->overlap_bytes
);
1836 assert(offset
+ bytes
<= bs
->total_sectors
* BDRV_SECTOR_SIZE
||
1837 child
->perm
& BLK_PERM_RESIZE
);
1839 switch (req
->type
) {
1840 case BDRV_TRACKED_WRITE
:
1841 case BDRV_TRACKED_DISCARD
:
1842 if (flags
& BDRV_REQ_WRITE_UNCHANGED
) {
1843 assert(child
->perm
& (BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_WRITE
));
1845 assert(child
->perm
& BLK_PERM_WRITE
);
1847 bdrv_write_threshold_check_write(bs
, offset
, bytes
);
1849 case BDRV_TRACKED_TRUNCATE
:
1850 assert(child
->perm
& BLK_PERM_RESIZE
);
1857 static inline void coroutine_fn
1858 bdrv_co_write_req_finish(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1859 BdrvTrackedRequest
*req
, int ret
)
1861 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
1862 BlockDriverState
*bs
= child
->bs
;
1864 bdrv_check_request(offset
, bytes
, &error_abort
);
1866 qatomic_inc(&bs
->write_gen
);
1869 * Discard cannot extend the image, but in error handling cases, such as
1870 * when reverting a qcow2 cluster allocation, the discarded range can pass
1871 * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
1872 * here. Instead, just skip it, since semantically a discard request
1873 * beyond EOF cannot expand the image anyway.
1876 (req
->type
== BDRV_TRACKED_TRUNCATE
||
1877 end_sector
> bs
->total_sectors
) &&
1878 req
->type
!= BDRV_TRACKED_DISCARD
) {
1879 bs
->total_sectors
= end_sector
;
1880 bdrv_parent_cb_resize(bs
);
1881 bdrv_dirty_bitmap_truncate(bs
, end_sector
<< BDRV_SECTOR_BITS
);
1884 switch (req
->type
) {
1885 case BDRV_TRACKED_WRITE
:
1886 stat64_max(&bs
->wr_highest_offset
, offset
+ bytes
);
1887 /* fall through, to set dirty bits */
1888 case BDRV_TRACKED_DISCARD
:
1889 bdrv_set_dirty(bs
, offset
, bytes
);
1898 * Forwards an already correctly aligned write request to the BlockDriver,
1899 * after possibly fragmenting it.
1901 static int coroutine_fn GRAPH_RDLOCK
1902 bdrv_aligned_pwritev(BdrvChild
*child
, BdrvTrackedRequest
*req
,
1903 int64_t offset
, int64_t bytes
, int64_t align
,
1904 QEMUIOVector
*qiov
, size_t qiov_offset
,
1905 BdrvRequestFlags flags
)
1907 BlockDriverState
*bs
= child
->bs
;
1908 BlockDriver
*drv
= bs
->drv
;
1911 int64_t bytes_remaining
= bytes
;
1914 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1920 if (bdrv_has_readonly_bitmaps(bs
)) {
1924 assert(is_power_of_2(align
));
1925 assert((offset
& (align
- 1)) == 0);
1926 assert((bytes
& (align
- 1)) == 0);
1927 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1930 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, req
, flags
);
1932 if (!ret
&& bs
->detect_zeroes
!= BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
&&
1933 !(flags
& BDRV_REQ_ZERO_WRITE
) && drv
->bdrv_co_pwrite_zeroes
&&
1934 qemu_iovec_is_zero(qiov
, qiov_offset
, bytes
)) {
1935 flags
|= BDRV_REQ_ZERO_WRITE
;
1936 if (bs
->detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
) {
1937 flags
|= BDRV_REQ_MAY_UNMAP
;
1940 /* Can't use optimization hint with bufferless zero write */
1941 flags
&= ~BDRV_REQ_REGISTERED_BUF
;
1945 /* Do nothing, write notifier decided to fail this request */
1946 } else if (flags
& BDRV_REQ_ZERO_WRITE
) {
1947 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_ZERO
);
1948 ret
= bdrv_co_do_pwrite_zeroes(bs
, offset
, bytes
, flags
);
1949 } else if (flags
& BDRV_REQ_WRITE_COMPRESSED
) {
1950 ret
= bdrv_driver_pwritev_compressed(bs
, offset
, bytes
,
1952 } else if (bytes
<= max_transfer
) {
1953 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV
);
1954 ret
= bdrv_driver_pwritev(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1956 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV
);
1957 while (bytes_remaining
) {
1958 int num
= MIN(bytes_remaining
, max_transfer
);
1959 int local_flags
= flags
;
1962 if (num
< bytes_remaining
&& (flags
& BDRV_REQ_FUA
) &&
1963 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1964 /* If FUA is going to be emulated by flush, we only
1965 * need to flush on the last iteration */
1966 local_flags
&= ~BDRV_REQ_FUA
;
1969 ret
= bdrv_driver_pwritev(bs
, offset
+ bytes
- bytes_remaining
,
1971 qiov_offset
+ bytes
- bytes_remaining
,
1976 bytes_remaining
-= num
;
1979 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_DONE
);
1984 bdrv_co_write_req_finish(child
, offset
, bytes
, req
, ret
);
1989 static int coroutine_fn GRAPH_RDLOCK
1990 bdrv_co_do_zero_pwritev(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1991 BdrvRequestFlags flags
, BdrvTrackedRequest
*req
)
1993 BlockDriverState
*bs
= child
->bs
;
1994 QEMUIOVector local_qiov
;
1995 uint64_t align
= bs
->bl
.request_alignment
;
1998 BdrvRequestPadding pad
;
2000 /* This flag doesn't make sense for padding or zero writes */
2001 flags
&= ~BDRV_REQ_REGISTERED_BUF
;
2003 padding
= bdrv_init_padding(bs
, offset
, bytes
, &pad
);
2005 assert(!(flags
& BDRV_REQ_NO_WAIT
));
2006 bdrv_make_request_serialising(req
, align
);
2008 bdrv_padding_rmw_read(child
, req
, &pad
, true);
2010 if (pad
.head
|| pad
.merge_reads
) {
2011 int64_t aligned_offset
= offset
& ~(align
- 1);
2012 int64_t write_bytes
= pad
.merge_reads
? pad
.buf_len
: align
;
2014 qemu_iovec_init_buf(&local_qiov
, pad
.buf
, write_bytes
);
2015 ret
= bdrv_aligned_pwritev(child
, req
, aligned_offset
, write_bytes
,
2016 align
, &local_qiov
, 0,
2017 flags
& ~BDRV_REQ_ZERO_WRITE
);
2018 if (ret
< 0 || pad
.merge_reads
) {
2019 /* Error or all work is done */
2022 offset
+= write_bytes
- pad
.head
;
2023 bytes
-= write_bytes
- pad
.head
;
2027 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2028 if (bytes
>= align
) {
2029 /* Write the aligned part in the middle. */
2030 int64_t aligned_bytes
= bytes
& ~(align
- 1);
2031 ret
= bdrv_aligned_pwritev(child
, req
, offset
, aligned_bytes
, align
,
2036 bytes
-= aligned_bytes
;
2037 offset
+= aligned_bytes
;
2040 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2042 assert(align
== pad
.tail
+ bytes
);
2044 qemu_iovec_init_buf(&local_qiov
, pad
.tail_buf
, align
);
2045 ret
= bdrv_aligned_pwritev(child
, req
, offset
, align
, align
,
2047 flags
& ~BDRV_REQ_ZERO_WRITE
);
2051 bdrv_padding_destroy(&pad
);
2057 * Handle a write request in coroutine context
2059 int coroutine_fn
bdrv_co_pwritev(BdrvChild
*child
,
2060 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
2061 BdrvRequestFlags flags
)
2064 return bdrv_co_pwritev_part(child
, offset
, bytes
, qiov
, 0, flags
);
2067 int coroutine_fn
bdrv_co_pwritev_part(BdrvChild
*child
,
2068 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
, size_t qiov_offset
,
2069 BdrvRequestFlags flags
)
2071 BlockDriverState
*bs
= child
->bs
;
2072 BdrvTrackedRequest req
;
2073 uint64_t align
= bs
->bl
.request_alignment
;
2074 BdrvRequestPadding pad
;
2076 bool padded
= false;
2079 trace_bdrv_co_pwritev_part(child
->bs
, offset
, bytes
, flags
);
2081 if (!bdrv_co_is_inserted(bs
)) {
2085 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2086 ret
= bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, NULL
);
2088 ret
= bdrv_check_request32(offset
, bytes
, qiov
, qiov_offset
);
2094 /* If the request is misaligned then we can't make it efficient */
2095 if ((flags
& BDRV_REQ_NO_FALLBACK
) &&
2096 !QEMU_IS_ALIGNED(offset
| bytes
, align
))
2101 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
2103 * Aligning zero request is nonsense. Even if driver has special meaning
2104 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
2105 * it to driver due to request_alignment.
2107 * Still, no reason to return an error if someone do unaligned
2108 * zero-length write occasionally.
2113 if (!(flags
& BDRV_REQ_ZERO_WRITE
)) {
2115 * Pad request for following read-modify-write cycle.
2116 * bdrv_co_do_zero_pwritev() does aligning by itself, so, we do
2117 * alignment only if there is no ZERO flag.
2119 ret
= bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
,
2126 bdrv_inc_in_flight(bs
);
2127 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_WRITE
);
2129 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2131 ret
= bdrv_co_do_zero_pwritev(child
, offset
, bytes
, flags
, &req
);
2137 * Request was unaligned to request_alignment and therefore
2138 * padded. We are going to do read-modify-write, and must
2139 * serialize the request to prevent interactions of the
2140 * widened region with other transactions.
2142 assert(!(flags
& BDRV_REQ_NO_WAIT
));
2143 bdrv_make_request_serialising(&req
, align
);
2144 bdrv_padding_rmw_read(child
, &req
, &pad
, false);
2147 ret
= bdrv_aligned_pwritev(child
, &req
, offset
, bytes
, align
,
2148 qiov
, qiov_offset
, flags
);
2150 bdrv_padding_destroy(&pad
);
2153 tracked_request_end(&req
);
2154 bdrv_dec_in_flight(bs
);
2159 int coroutine_fn
bdrv_co_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
2160 int64_t bytes
, BdrvRequestFlags flags
)
2163 trace_bdrv_co_pwrite_zeroes(child
->bs
, offset
, bytes
, flags
);
2164 assert_bdrv_graph_readable();
2166 if (!(child
->bs
->open_flags
& BDRV_O_UNMAP
)) {
2167 flags
&= ~BDRV_REQ_MAY_UNMAP
;
2170 return bdrv_co_pwritev(child
, offset
, bytes
, NULL
,
2171 BDRV_REQ_ZERO_WRITE
| flags
);
2175 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
2177 int bdrv_flush_all(void)
2179 BdrvNextIterator it
;
2180 BlockDriverState
*bs
= NULL
;
2183 GLOBAL_STATE_CODE();
2186 * bdrv queue is managed by record/replay,
2187 * creating new flush request for stopping
2188 * the VM may break the determinism
2190 if (replay_events_enabled()) {
2194 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2195 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
2198 aio_context_acquire(aio_context
);
2199 ret
= bdrv_flush(bs
);
2200 if (ret
< 0 && !result
) {
2203 aio_context_release(aio_context
);
2210 * Returns the allocation status of the specified sectors.
2211 * Drivers not implementing the functionality are assumed to not support
2212 * backing files, hence all their sectors are reported as allocated.
2214 * If 'want_zero' is true, the caller is querying for mapping
2215 * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and
2216 * _ZERO where possible; otherwise, the result favors larger 'pnum',
2217 * with a focus on accurate BDRV_BLOCK_ALLOCATED.
2219 * If 'offset' is beyond the end of the disk image the return value is
2220 * BDRV_BLOCK_EOF and 'pnum' is set to 0.
2222 * 'bytes' is the max value 'pnum' should be set to. If bytes goes
2223 * beyond the end of the disk image it will be clamped; if 'pnum' is set to
2224 * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
2226 * 'pnum' is set to the number of bytes (including and immediately
2227 * following the specified offset) that are easily known to be in the
2228 * same allocated/unallocated state. Note that a second call starting
2229 * at the original offset plus returned pnum may have the same status.
2230 * The returned value is non-zero on success except at end-of-file.
2232 * Returns negative errno on failure. Otherwise, if the
2233 * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are
2234 * set to the host mapping and BDS corresponding to the guest offset.
2236 static int coroutine_fn GRAPH_RDLOCK
2237 bdrv_co_block_status(BlockDriverState
*bs
, bool want_zero
,
2238 int64_t offset
, int64_t bytes
,
2239 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
2242 int64_t n
; /* bytes */
2244 int64_t local_map
= 0;
2245 BlockDriverState
*local_file
= NULL
;
2246 int64_t aligned_offset
, aligned_bytes
;
2248 bool has_filtered_child
;
2251 assert_bdrv_graph_readable();
2253 total_size
= bdrv_getlength(bs
);
2254 if (total_size
< 0) {
2259 if (offset
>= total_size
) {
2260 ret
= BDRV_BLOCK_EOF
;
2268 n
= total_size
- offset
;
2273 /* Must be non-NULL or bdrv_getlength() would have failed */
2275 has_filtered_child
= bdrv_filter_child(bs
);
2276 if (!bs
->drv
->bdrv_co_block_status
&& !has_filtered_child
) {
2278 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
;
2279 if (offset
+ bytes
== total_size
) {
2280 ret
|= BDRV_BLOCK_EOF
;
2282 if (bs
->drv
->protocol_name
) {
2283 ret
|= BDRV_BLOCK_OFFSET_VALID
;
2290 bdrv_inc_in_flight(bs
);
2292 /* Round out to request_alignment boundaries */
2293 align
= bs
->bl
.request_alignment
;
2294 aligned_offset
= QEMU_ALIGN_DOWN(offset
, align
);
2295 aligned_bytes
= ROUND_UP(offset
+ bytes
, align
) - aligned_offset
;
2297 if (bs
->drv
->bdrv_co_block_status
) {
2299 * Use the block-status cache only for protocol nodes: Format
2300 * drivers are generally quick to inquire the status, but protocol
2301 * drivers often need to get information from outside of qemu, so
2302 * we do not have control over the actual implementation. There
2303 * have been cases where inquiring the status took an unreasonably
2304 * long time, and we can do nothing in qemu to fix it.
2305 * This is especially problematic for images with large data areas,
2306 * because finding the few holes in them and giving them special
2307 * treatment does not gain much performance. Therefore, we try to
2308 * cache the last-identified data region.
2310 * Second, limiting ourselves to protocol nodes allows us to assume
2311 * the block status for data regions to be DATA | OFFSET_VALID, and
2312 * that the host offset is the same as the guest offset.
2314 * Note that it is possible that external writers zero parts of
2315 * the cached regions without the cache being invalidated, and so
2316 * we may report zeroes as data. This is not catastrophic,
2317 * however, because reporting zeroes as data is fine.
2319 if (QLIST_EMPTY(&bs
->children
) &&
2320 bdrv_bsc_is_data(bs
, aligned_offset
, pnum
))
2322 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
2324 local_map
= aligned_offset
;
2326 ret
= bs
->drv
->bdrv_co_block_status(bs
, want_zero
, aligned_offset
,
2327 aligned_bytes
, pnum
, &local_map
,
2331 * Note that checking QLIST_EMPTY(&bs->children) is also done when
2332 * the cache is queried above. Technically, we do not need to check
2333 * it here; the worst that can happen is that we fill the cache for
2334 * non-protocol nodes, and then it is never used. However, filling
2335 * the cache requires an RCU update, so double check here to avoid
2336 * such an update if possible.
2338 * Check want_zero, because we only want to update the cache when we
2339 * have accurate information about what is zero and what is data.
2342 ret
== (BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
) &&
2343 QLIST_EMPTY(&bs
->children
))
2346 * When a protocol driver reports BLOCK_OFFSET_VALID, the
2347 * returned local_map value must be the same as the offset we
2348 * have passed (aligned_offset), and local_bs must be the node
2350 * Assert this, because we follow this rule when reading from
2351 * the cache (see the `local_file = bs` and
2352 * `local_map = aligned_offset` assignments above), and the
2353 * result the cache delivers must be the same as the driver
2356 assert(local_file
== bs
);
2357 assert(local_map
== aligned_offset
);
2358 bdrv_bsc_fill(bs
, aligned_offset
, *pnum
);
2362 /* Default code for filters */
2364 local_file
= bdrv_filter_bs(bs
);
2367 *pnum
= aligned_bytes
;
2368 local_map
= aligned_offset
;
2369 ret
= BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
;
2377 * The driver's result must be a non-zero multiple of request_alignment.
2378 * Clamp pnum and adjust map to original request.
2380 assert(*pnum
&& QEMU_IS_ALIGNED(*pnum
, align
) &&
2381 align
> offset
- aligned_offset
);
2382 if (ret
& BDRV_BLOCK_RECURSE
) {
2383 assert(ret
& BDRV_BLOCK_DATA
);
2384 assert(ret
& BDRV_BLOCK_OFFSET_VALID
);
2385 assert(!(ret
& BDRV_BLOCK_ZERO
));
2388 *pnum
-= offset
- aligned_offset
;
2389 if (*pnum
> bytes
) {
2392 if (ret
& BDRV_BLOCK_OFFSET_VALID
) {
2393 local_map
+= offset
- aligned_offset
;
2396 if (ret
& BDRV_BLOCK_RAW
) {
2397 assert(ret
& BDRV_BLOCK_OFFSET_VALID
&& local_file
);
2398 ret
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2399 *pnum
, pnum
, &local_map
, &local_file
);
2403 if (ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ZERO
)) {
2404 ret
|= BDRV_BLOCK_ALLOCATED
;
2405 } else if (bs
->drv
->supports_backing
) {
2406 BlockDriverState
*cow_bs
= bdrv_cow_bs(bs
);
2409 ret
|= BDRV_BLOCK_ZERO
;
2410 } else if (want_zero
) {
2411 int64_t size2
= bdrv_getlength(cow_bs
);
2413 if (size2
>= 0 && offset
>= size2
) {
2414 ret
|= BDRV_BLOCK_ZERO
;
2419 if (want_zero
&& ret
& BDRV_BLOCK_RECURSE
&&
2420 local_file
&& local_file
!= bs
&&
2421 (ret
& BDRV_BLOCK_DATA
) && !(ret
& BDRV_BLOCK_ZERO
) &&
2422 (ret
& BDRV_BLOCK_OFFSET_VALID
)) {
2426 ret2
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2427 *pnum
, &file_pnum
, NULL
, NULL
);
2429 /* Ignore errors. This is just providing extra information, it
2430 * is useful but not necessary.
2432 if (ret2
& BDRV_BLOCK_EOF
&&
2433 (!file_pnum
|| ret2
& BDRV_BLOCK_ZERO
)) {
2435 * It is valid for the format block driver to read
2436 * beyond the end of the underlying file's current
2437 * size; such areas read as zero.
2439 ret
|= BDRV_BLOCK_ZERO
;
2441 /* Limit request to the range reported by the protocol driver */
2443 ret
|= (ret2
& BDRV_BLOCK_ZERO
);
2449 bdrv_dec_in_flight(bs
);
2450 if (ret
>= 0 && offset
+ *pnum
== total_size
) {
2451 ret
|= BDRV_BLOCK_EOF
;
2464 bdrv_co_common_block_status_above(BlockDriverState
*bs
,
2465 BlockDriverState
*base
,
2472 BlockDriverState
**file
,
2476 BlockDriverState
*p
;
2481 assert(!include_base
|| base
); /* Can't include NULL base */
2482 assert_bdrv_graph_readable();
2489 if (!include_base
&& bs
== base
) {
2494 ret
= bdrv_co_block_status(bs
, want_zero
, offset
, bytes
, pnum
, map
, file
);
2496 if (ret
< 0 || *pnum
== 0 || ret
& BDRV_BLOCK_ALLOCATED
|| bs
== base
) {
2500 if (ret
& BDRV_BLOCK_EOF
) {
2501 eof
= offset
+ *pnum
;
2504 assert(*pnum
<= bytes
);
2507 for (p
= bdrv_filter_or_cow_bs(bs
); include_base
|| p
!= base
;
2508 p
= bdrv_filter_or_cow_bs(p
))
2510 ret
= bdrv_co_block_status(p
, want_zero
, offset
, bytes
, pnum
, map
,
2518 * The top layer deferred to this layer, and because this layer is
2519 * short, any zeroes that we synthesize beyond EOF behave as if they
2520 * were allocated at this layer.
2522 * We don't include BDRV_BLOCK_EOF into ret, as upper layer may be
2523 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2526 assert(ret
& BDRV_BLOCK_EOF
);
2531 ret
= BDRV_BLOCK_ZERO
| BDRV_BLOCK_ALLOCATED
;
2534 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2536 * We've found the node and the status, we must break.
2538 * Drop BDRV_BLOCK_EOF, as it's not for upper layer, which may be
2539 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2542 ret
&= ~BDRV_BLOCK_EOF
;
2547 assert(include_base
);
2552 * OK, [offset, offset + *pnum) region is unallocated on this layer,
2553 * let's continue the diving.
2555 assert(*pnum
<= bytes
);
2559 if (offset
+ *pnum
== eof
) {
2560 ret
|= BDRV_BLOCK_EOF
;
2566 int coroutine_fn
bdrv_co_block_status_above(BlockDriverState
*bs
,
2567 BlockDriverState
*base
,
2568 int64_t offset
, int64_t bytes
,
2569 int64_t *pnum
, int64_t *map
,
2570 BlockDriverState
**file
)
2573 return bdrv_co_common_block_status_above(bs
, base
, false, true, offset
,
2574 bytes
, pnum
, map
, file
, NULL
);
2577 int bdrv_block_status_above(BlockDriverState
*bs
, BlockDriverState
*base
,
2578 int64_t offset
, int64_t bytes
, int64_t *pnum
,
2579 int64_t *map
, BlockDriverState
**file
)
2582 return bdrv_common_block_status_above(bs
, base
, false, true, offset
, bytes
,
2583 pnum
, map
, file
, NULL
);
2586 int bdrv_block_status(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2587 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
2590 return bdrv_block_status_above(bs
, bdrv_filter_or_cow_bs(bs
),
2591 offset
, bytes
, pnum
, map
, file
);
2595 * Check @bs (and its backing chain) to see if the range defined
2596 * by @offset and @bytes is known to read as zeroes.
2597 * Return 1 if that is the case, 0 otherwise and -errno on error.
2598 * This test is meant to be fast rather than accurate so returning 0
2599 * does not guarantee non-zero data.
2601 int coroutine_fn
bdrv_co_is_zero_fast(BlockDriverState
*bs
, int64_t offset
,
2605 int64_t pnum
= bytes
;
2612 ret
= bdrv_co_common_block_status_above(bs
, NULL
, false, false, offset
,
2613 bytes
, &pnum
, NULL
, NULL
, NULL
);
2619 return (pnum
== bytes
) && (ret
& BDRV_BLOCK_ZERO
);
2622 int coroutine_fn
bdrv_co_is_allocated(BlockDriverState
*bs
, int64_t offset
,
2623 int64_t bytes
, int64_t *pnum
)
2629 ret
= bdrv_co_common_block_status_above(bs
, bs
, true, false, offset
,
2630 bytes
, pnum
? pnum
: &dummy
, NULL
,
2635 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
2638 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2645 ret
= bdrv_common_block_status_above(bs
, bs
, true, false, offset
,
2646 bytes
, pnum
? pnum
: &dummy
, NULL
,
2651 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
2654 /* See bdrv_is_allocated_above for documentation */
2655 int coroutine_fn
bdrv_co_is_allocated_above(BlockDriverState
*top
,
2656 BlockDriverState
*base
,
2657 bool include_base
, int64_t offset
,
2658 int64_t bytes
, int64_t *pnum
)
2664 ret
= bdrv_co_common_block_status_above(top
, base
, include_base
, false,
2665 offset
, bytes
, pnum
, NULL
, NULL
,
2671 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2678 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2680 * Return a positive depth if (a prefix of) the given range is allocated
2681 * in any image between BASE and TOP (BASE is only included if include_base
2682 * is set). Depth 1 is TOP, 2 is the first backing layer, and so forth.
2683 * BASE can be NULL to check if the given offset is allocated in any
2684 * image of the chain. Return 0 otherwise, or negative errno on
2687 * 'pnum' is set to the number of bytes (including and immediately
2688 * following the specified offset) that are known to be in the same
2689 * allocated/unallocated state. Note that a subsequent call starting
2690 * at 'offset + *pnum' may return the same allocation status (in other
2691 * words, the result is not necessarily the maximum possible range);
2692 * but 'pnum' will only be 0 when end of file is reached.
2694 int bdrv_is_allocated_above(BlockDriverState
*top
,
2695 BlockDriverState
*base
,
2696 bool include_base
, int64_t offset
,
2697 int64_t bytes
, int64_t *pnum
)
2703 ret
= bdrv_common_block_status_above(top
, base
, include_base
, false,
2704 offset
, bytes
, pnum
, NULL
, NULL
,
2710 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2717 bdrv_co_readv_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2719 BlockDriver
*drv
= bs
->drv
;
2720 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2723 assert_bdrv_graph_readable();
2725 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
2734 bdrv_inc_in_flight(bs
);
2736 if (drv
->bdrv_co_load_vmstate
) {
2737 ret
= drv
->bdrv_co_load_vmstate(bs
, qiov
, pos
);
2738 } else if (child_bs
) {
2739 ret
= bdrv_co_readv_vmstate(child_bs
, qiov
, pos
);
2744 bdrv_dec_in_flight(bs
);
2750 bdrv_co_writev_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2752 BlockDriver
*drv
= bs
->drv
;
2753 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2756 assert_bdrv_graph_readable();
2758 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
2767 bdrv_inc_in_flight(bs
);
2769 if (drv
->bdrv_co_save_vmstate
) {
2770 ret
= drv
->bdrv_co_save_vmstate(bs
, qiov
, pos
);
2771 } else if (child_bs
) {
2772 ret
= bdrv_co_writev_vmstate(child_bs
, qiov
, pos
);
2777 bdrv_dec_in_flight(bs
);
2782 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2783 int64_t pos
, int size
)
2785 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2786 int ret
= bdrv_writev_vmstate(bs
, &qiov
, pos
);
2789 return ret
< 0 ? ret
: size
;
2792 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2793 int64_t pos
, int size
)
2795 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2796 int ret
= bdrv_readv_vmstate(bs
, &qiov
, pos
);
2799 return ret
< 0 ? ret
: size
;
2802 /**************************************************************/
2805 void bdrv_aio_cancel(BlockAIOCB
*acb
)
2809 bdrv_aio_cancel_async(acb
);
2810 while (acb
->refcnt
> 1) {
2811 if (acb
->aiocb_info
->get_aio_context
) {
2812 aio_poll(acb
->aiocb_info
->get_aio_context(acb
), true);
2813 } else if (acb
->bs
) {
2814 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2815 * assert that we're not using an I/O thread. Thread-safe
2816 * code should use bdrv_aio_cancel_async exclusively.
2818 assert(bdrv_get_aio_context(acb
->bs
) == qemu_get_aio_context());
2819 aio_poll(bdrv_get_aio_context(acb
->bs
), true);
2824 qemu_aio_unref(acb
);
2827 /* Async version of aio cancel. The caller is not blocked if the acb implements
2828 * cancel_async, otherwise we do nothing and let the request normally complete.
2829 * In either case the completion callback must be called. */
2830 void bdrv_aio_cancel_async(BlockAIOCB
*acb
)
2833 if (acb
->aiocb_info
->cancel_async
) {
2834 acb
->aiocb_info
->cancel_async(acb
);
2838 /**************************************************************/
2839 /* Coroutine block device emulation */
2841 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2843 BdrvChild
*primary_child
= bdrv_primary_child(bs
);
2849 assert_bdrv_graph_readable();
2850 bdrv_inc_in_flight(bs
);
2852 if (!bdrv_co_is_inserted(bs
) || bdrv_is_read_only(bs
) ||
2857 qemu_co_mutex_lock(&bs
->reqs_lock
);
2858 current_gen
= qatomic_read(&bs
->write_gen
);
2860 /* Wait until any previous flushes are completed */
2861 while (bs
->active_flush_req
) {
2862 qemu_co_queue_wait(&bs
->flush_queue
, &bs
->reqs_lock
);
2865 /* Flushes reach this point in nondecreasing current_gen order. */
2866 bs
->active_flush_req
= true;
2867 qemu_co_mutex_unlock(&bs
->reqs_lock
);
2869 /* Write back all layers by calling one driver function */
2870 if (bs
->drv
->bdrv_co_flush
) {
2871 ret
= bs
->drv
->bdrv_co_flush(bs
);
2875 /* Write back cached data to the OS even with cache=unsafe */
2876 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_OS
);
2877 if (bs
->drv
->bdrv_co_flush_to_os
) {
2878 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
2884 /* But don't actually force it to the disk with cache=unsafe */
2885 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2886 goto flush_children
;
2889 /* Check if we really need to flush anything */
2890 if (bs
->flushed_gen
== current_gen
) {
2891 goto flush_children
;
2894 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_DISK
);
2896 /* bs->drv->bdrv_co_flush() might have ejected the BDS
2897 * (even in case of apparent success) */
2901 if (bs
->drv
->bdrv_co_flush_to_disk
) {
2902 ret
= bs
->drv
->bdrv_co_flush_to_disk(bs
);
2903 } else if (bs
->drv
->bdrv_aio_flush
) {
2905 CoroutineIOCompletion co
= {
2906 .coroutine
= qemu_coroutine_self(),
2909 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
2913 qemu_coroutine_yield();
2918 * Some block drivers always operate in either writethrough or unsafe
2919 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2920 * know how the server works (because the behaviour is hardcoded or
2921 * depends on server-side configuration), so we can't ensure that
2922 * everything is safe on disk. Returning an error doesn't work because
2923 * that would break guests even if the server operates in writethrough
2926 * Let's hope the user knows what he's doing.
2935 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2936 * in the case of cache=unsafe, so there are no useless flushes.
2940 QLIST_FOREACH(child
, &bs
->children
, next
) {
2941 if (child
->perm
& (BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
)) {
2942 int this_child_ret
= bdrv_co_flush(child
->bs
);
2944 ret
= this_child_ret
;
2950 /* Notify any pending flushes that we have completed */
2952 bs
->flushed_gen
= current_gen
;
2955 qemu_co_mutex_lock(&bs
->reqs_lock
);
2956 bs
->active_flush_req
= false;
2957 /* Return value is ignored - it's ok if wait queue is empty */
2958 qemu_co_queue_next(&bs
->flush_queue
);
2959 qemu_co_mutex_unlock(&bs
->reqs_lock
);
2962 bdrv_dec_in_flight(bs
);
2966 int coroutine_fn
bdrv_co_pdiscard(BdrvChild
*child
, int64_t offset
,
2969 BdrvTrackedRequest req
;
2971 int64_t max_pdiscard
;
2972 int head
, tail
, align
;
2973 BlockDriverState
*bs
= child
->bs
;
2975 assert_bdrv_graph_readable();
2977 if (!bs
|| !bs
->drv
|| !bdrv_co_is_inserted(bs
)) {
2981 if (bdrv_has_readonly_bitmaps(bs
)) {
2985 ret
= bdrv_check_request(offset
, bytes
, NULL
);
2990 /* Do nothing if disabled. */
2991 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
2995 if (!bs
->drv
->bdrv_co_pdiscard
&& !bs
->drv
->bdrv_aio_pdiscard
) {
2999 /* Invalidate the cached block-status data range if this discard overlaps */
3000 bdrv_bsc_invalidate_range(bs
, offset
, bytes
);
3002 /* Discard is advisory, but some devices track and coalesce
3003 * unaligned requests, so we must pass everything down rather than
3004 * round here. Still, most devices will just silently ignore
3005 * unaligned requests (by returning -ENOTSUP), so we must fragment
3006 * the request accordingly. */
3007 align
= MAX(bs
->bl
.pdiscard_alignment
, bs
->bl
.request_alignment
);
3008 assert(align
% bs
->bl
.request_alignment
== 0);
3009 head
= offset
% align
;
3010 tail
= (offset
+ bytes
) % align
;
3012 bdrv_inc_in_flight(bs
);
3013 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_DISCARD
);
3015 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, &req
, 0);
3020 max_pdiscard
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_pdiscard
, INT64_MAX
),
3022 assert(max_pdiscard
>= bs
->bl
.request_alignment
);
3025 int64_t num
= bytes
;
3028 /* Make small requests to get to alignment boundaries. */
3029 num
= MIN(bytes
, align
- head
);
3030 if (!QEMU_IS_ALIGNED(num
, bs
->bl
.request_alignment
)) {
3031 num
%= bs
->bl
.request_alignment
;
3033 head
= (head
+ num
) % align
;
3034 assert(num
< max_pdiscard
);
3037 /* Shorten the request to the last aligned cluster. */
3039 } else if (!QEMU_IS_ALIGNED(tail
, bs
->bl
.request_alignment
) &&
3040 tail
> bs
->bl
.request_alignment
) {
3041 tail
%= bs
->bl
.request_alignment
;
3045 /* limit request size */
3046 if (num
> max_pdiscard
) {
3054 if (bs
->drv
->bdrv_co_pdiscard
) {
3055 ret
= bs
->drv
->bdrv_co_pdiscard(bs
, offset
, num
);
3058 CoroutineIOCompletion co
= {
3059 .coroutine
= qemu_coroutine_self(),
3062 acb
= bs
->drv
->bdrv_aio_pdiscard(bs
, offset
, num
,
3063 bdrv_co_io_em_complete
, &co
);
3068 qemu_coroutine_yield();
3072 if (ret
&& ret
!= -ENOTSUP
) {
3081 bdrv_co_write_req_finish(child
, req
.offset
, req
.bytes
, &req
, ret
);
3082 tracked_request_end(&req
);
3083 bdrv_dec_in_flight(bs
);
3087 int coroutine_fn
bdrv_co_ioctl(BlockDriverState
*bs
, int req
, void *buf
)
3089 BlockDriver
*drv
= bs
->drv
;
3090 CoroutineIOCompletion co
= {
3091 .coroutine
= qemu_coroutine_self(),
3095 assert_bdrv_graph_readable();
3097 bdrv_inc_in_flight(bs
);
3098 if (!drv
|| (!drv
->bdrv_aio_ioctl
&& !drv
->bdrv_co_ioctl
)) {
3103 if (drv
->bdrv_co_ioctl
) {
3104 co
.ret
= drv
->bdrv_co_ioctl(bs
, req
, buf
);
3106 acb
= drv
->bdrv_aio_ioctl(bs
, req
, buf
, bdrv_co_io_em_complete
, &co
);
3111 qemu_coroutine_yield();
3114 bdrv_dec_in_flight(bs
);
3118 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
3121 return qemu_memalign(bdrv_opt_mem_align(bs
), size
);
3124 void *qemu_blockalign0(BlockDriverState
*bs
, size_t size
)
3127 return memset(qemu_blockalign(bs
, size
), 0, size
);
3130 void *qemu_try_blockalign(BlockDriverState
*bs
, size_t size
)
3132 size_t align
= bdrv_opt_mem_align(bs
);
3135 /* Ensure that NULL is never returned on success */
3141 return qemu_try_memalign(align
, size
);
3144 void *qemu_try_blockalign0(BlockDriverState
*bs
, size_t size
)
3146 void *mem
= qemu_try_blockalign(bs
, size
);
3150 memset(mem
, 0, size
);
3156 void coroutine_fn
bdrv_co_io_plug(BlockDriverState
*bs
)
3160 assert_bdrv_graph_readable();
3162 QLIST_FOREACH(child
, &bs
->children
, next
) {
3163 bdrv_co_io_plug(child
->bs
);
3166 if (qatomic_fetch_inc(&bs
->io_plugged
) == 0) {
3167 BlockDriver
*drv
= bs
->drv
;
3168 if (drv
&& drv
->bdrv_co_io_plug
) {
3169 drv
->bdrv_co_io_plug(bs
);
3174 void coroutine_fn
bdrv_co_io_unplug(BlockDriverState
*bs
)
3178 assert_bdrv_graph_readable();
3180 assert(bs
->io_plugged
);
3181 if (qatomic_fetch_dec(&bs
->io_plugged
) == 1) {
3182 BlockDriver
*drv
= bs
->drv
;
3183 if (drv
&& drv
->bdrv_co_io_unplug
) {
3184 drv
->bdrv_co_io_unplug(bs
);
3188 QLIST_FOREACH(child
, &bs
->children
, next
) {
3189 bdrv_co_io_unplug(child
->bs
);
3193 /* Helper that undoes bdrv_register_buf() when it fails partway through */
3194 static void GRAPH_RDLOCK
3195 bdrv_register_buf_rollback(BlockDriverState
*bs
, void *host
, size_t size
,
3196 BdrvChild
*final_child
)
3200 GLOBAL_STATE_CODE();
3201 assert_bdrv_graph_readable();
3203 QLIST_FOREACH(child
, &bs
->children
, next
) {
3204 if (child
== final_child
) {
3208 bdrv_unregister_buf(child
->bs
, host
, size
);
3211 if (bs
->drv
&& bs
->drv
->bdrv_unregister_buf
) {
3212 bs
->drv
->bdrv_unregister_buf(bs
, host
, size
);
3216 bool bdrv_register_buf(BlockDriverState
*bs
, void *host
, size_t size
,
3221 GLOBAL_STATE_CODE();
3222 GRAPH_RDLOCK_GUARD_MAINLOOP();
3224 if (bs
->drv
&& bs
->drv
->bdrv_register_buf
) {
3225 if (!bs
->drv
->bdrv_register_buf(bs
, host
, size
, errp
)) {
3229 QLIST_FOREACH(child
, &bs
->children
, next
) {
3230 if (!bdrv_register_buf(child
->bs
, host
, size
, errp
)) {
3231 bdrv_register_buf_rollback(bs
, host
, size
, child
);
3238 void bdrv_unregister_buf(BlockDriverState
*bs
, void *host
, size_t size
)
3242 GLOBAL_STATE_CODE();
3243 GRAPH_RDLOCK_GUARD_MAINLOOP();
3245 if (bs
->drv
&& bs
->drv
->bdrv_unregister_buf
) {
3246 bs
->drv
->bdrv_unregister_buf(bs
, host
, size
);
3248 QLIST_FOREACH(child
, &bs
->children
, next
) {
3249 bdrv_unregister_buf(child
->bs
, host
, size
);
3253 static int coroutine_fn GRAPH_RDLOCK
bdrv_co_copy_range_internal(
3254 BdrvChild
*src
, int64_t src_offset
, BdrvChild
*dst
,
3255 int64_t dst_offset
, int64_t bytes
,
3256 BdrvRequestFlags read_flags
, BdrvRequestFlags write_flags
,
3259 BdrvTrackedRequest req
;
3261 assert_bdrv_graph_readable();
3263 /* TODO We can support BDRV_REQ_NO_FALLBACK here */
3264 assert(!(read_flags
& BDRV_REQ_NO_FALLBACK
));
3265 assert(!(write_flags
& BDRV_REQ_NO_FALLBACK
));
3266 assert(!(read_flags
& BDRV_REQ_NO_WAIT
));
3267 assert(!(write_flags
& BDRV_REQ_NO_WAIT
));
3269 if (!dst
|| !dst
->bs
|| !bdrv_co_is_inserted(dst
->bs
)) {
3272 ret
= bdrv_check_request32(dst_offset
, bytes
, NULL
, 0);
3276 if (write_flags
& BDRV_REQ_ZERO_WRITE
) {
3277 return bdrv_co_pwrite_zeroes(dst
, dst_offset
, bytes
, write_flags
);
3280 if (!src
|| !src
->bs
|| !bdrv_co_is_inserted(src
->bs
)) {
3283 ret
= bdrv_check_request32(src_offset
, bytes
, NULL
, 0);
3288 if (!src
->bs
->drv
->bdrv_co_copy_range_from
3289 || !dst
->bs
->drv
->bdrv_co_copy_range_to
3290 || src
->bs
->encrypted
|| dst
->bs
->encrypted
) {
3295 bdrv_inc_in_flight(src
->bs
);
3296 tracked_request_begin(&req
, src
->bs
, src_offset
, bytes
,
3299 /* BDRV_REQ_SERIALISING is only for write operation */
3300 assert(!(read_flags
& BDRV_REQ_SERIALISING
));
3301 bdrv_wait_serialising_requests(&req
);
3303 ret
= src
->bs
->drv
->bdrv_co_copy_range_from(src
->bs
,
3307 read_flags
, write_flags
);
3309 tracked_request_end(&req
);
3310 bdrv_dec_in_flight(src
->bs
);
3312 bdrv_inc_in_flight(dst
->bs
);
3313 tracked_request_begin(&req
, dst
->bs
, dst_offset
, bytes
,
3314 BDRV_TRACKED_WRITE
);
3315 ret
= bdrv_co_write_req_prepare(dst
, dst_offset
, bytes
, &req
,
3318 ret
= dst
->bs
->drv
->bdrv_co_copy_range_to(dst
->bs
,
3322 read_flags
, write_flags
);
3324 bdrv_co_write_req_finish(dst
, dst_offset
, bytes
, &req
, ret
);
3325 tracked_request_end(&req
);
3326 bdrv_dec_in_flight(dst
->bs
);
3332 /* Copy range from @src to @dst.
3334 * See the comment of bdrv_co_copy_range for the parameter and return value
3336 int coroutine_fn
bdrv_co_copy_range_from(BdrvChild
*src
, int64_t src_offset
,
3337 BdrvChild
*dst
, int64_t dst_offset
,
3339 BdrvRequestFlags read_flags
,
3340 BdrvRequestFlags write_flags
)
3343 assert_bdrv_graph_readable();
3344 trace_bdrv_co_copy_range_from(src
, src_offset
, dst
, dst_offset
, bytes
,
3345 read_flags
, write_flags
);
3346 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3347 bytes
, read_flags
, write_flags
, true);
3350 /* Copy range from @src to @dst.
3352 * See the comment of bdrv_co_copy_range for the parameter and return value
3354 int coroutine_fn
bdrv_co_copy_range_to(BdrvChild
*src
, int64_t src_offset
,
3355 BdrvChild
*dst
, int64_t dst_offset
,
3357 BdrvRequestFlags read_flags
,
3358 BdrvRequestFlags write_flags
)
3361 assert_bdrv_graph_readable();
3362 trace_bdrv_co_copy_range_to(src
, src_offset
, dst
, dst_offset
, bytes
,
3363 read_flags
, write_flags
);
3364 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3365 bytes
, read_flags
, write_flags
, false);
3368 int coroutine_fn
bdrv_co_copy_range(BdrvChild
*src
, int64_t src_offset
,
3369 BdrvChild
*dst
, int64_t dst_offset
,
3370 int64_t bytes
, BdrvRequestFlags read_flags
,
3371 BdrvRequestFlags write_flags
)
3374 assert_bdrv_graph_readable();
3376 return bdrv_co_copy_range_from(src
, src_offset
,
3378 bytes
, read_flags
, write_flags
);
3381 static void bdrv_parent_cb_resize(BlockDriverState
*bs
)
3384 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
3385 if (c
->klass
->resize
) {
3386 c
->klass
->resize(c
);
3392 * Truncate file to 'offset' bytes (needed only for file protocols)
3394 * If 'exact' is true, the file must be resized to exactly the given
3395 * 'offset'. Otherwise, it is sufficient for the node to be at least
3396 * 'offset' bytes in length.
3398 int coroutine_fn
bdrv_co_truncate(BdrvChild
*child
, int64_t offset
, bool exact
,
3399 PreallocMode prealloc
, BdrvRequestFlags flags
,
3402 BlockDriverState
*bs
= child
->bs
;
3403 BdrvChild
*filtered
, *backing
;
3404 BlockDriver
*drv
= bs
->drv
;
3405 BdrvTrackedRequest req
;
3406 int64_t old_size
, new_bytes
;
3409 assert_bdrv_graph_readable();
3411 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3413 error_setg(errp
, "No medium inserted");
3417 error_setg(errp
, "Image size cannot be negative");
3421 ret
= bdrv_check_request(offset
, 0, errp
);
3426 old_size
= bdrv_getlength(bs
);
3428 error_setg_errno(errp
, -old_size
, "Failed to get old image size");
3432 if (bdrv_is_read_only(bs
)) {
3433 error_setg(errp
, "Image is read-only");
3437 if (offset
> old_size
) {
3438 new_bytes
= offset
- old_size
;
3443 bdrv_inc_in_flight(bs
);
3444 tracked_request_begin(&req
, bs
, offset
- new_bytes
, new_bytes
,
3445 BDRV_TRACKED_TRUNCATE
);
3447 /* If we are growing the image and potentially using preallocation for the
3448 * new area, we need to make sure that no write requests are made to it
3449 * concurrently or they might be overwritten by preallocation. */
3451 bdrv_make_request_serialising(&req
, 1);
3453 ret
= bdrv_co_write_req_prepare(child
, offset
- new_bytes
, new_bytes
, &req
,
3456 error_setg_errno(errp
, -ret
,
3457 "Failed to prepare request for truncation");
3461 filtered
= bdrv_filter_child(bs
);
3462 backing
= bdrv_cow_child(bs
);
3465 * If the image has a backing file that is large enough that it would
3466 * provide data for the new area, we cannot leave it unallocated because
3467 * then the backing file content would become visible. Instead, zero-fill
3470 * Note that if the image has a backing file, but was opened without the
3471 * backing file, taking care of keeping things consistent with that backing
3472 * file is the user's responsibility.
3474 if (new_bytes
&& backing
) {
3475 int64_t backing_len
;
3477 backing_len
= bdrv_co_getlength(backing
->bs
);
3478 if (backing_len
< 0) {
3480 error_setg_errno(errp
, -ret
, "Could not get backing file size");
3484 if (backing_len
> old_size
) {
3485 flags
|= BDRV_REQ_ZERO_WRITE
;
3489 if (drv
->bdrv_co_truncate
) {
3490 if (flags
& ~bs
->supported_truncate_flags
) {
3491 error_setg(errp
, "Block driver does not support requested flags");
3495 ret
= drv
->bdrv_co_truncate(bs
, offset
, exact
, prealloc
, flags
, errp
);
3496 } else if (filtered
) {
3497 ret
= bdrv_co_truncate(filtered
, offset
, exact
, prealloc
, flags
, errp
);
3499 error_setg(errp
, "Image format driver does not support resize");
3507 ret
= bdrv_co_refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
3509 error_setg_errno(errp
, -ret
, "Could not refresh total sector count");
3511 offset
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
3514 * It's possible that truncation succeeded but bdrv_refresh_total_sectors
3515 * failed, but the latter doesn't affect how we should finish the request.
3516 * Pass 0 as the last parameter so that dirty bitmaps etc. are handled.
3518 bdrv_co_write_req_finish(child
, offset
- new_bytes
, new_bytes
, &req
, 0);
3521 tracked_request_end(&req
);
3522 bdrv_dec_in_flight(bs
);
3527 void bdrv_cancel_in_flight(BlockDriverState
*bs
)
3529 GLOBAL_STATE_CODE();
3530 if (!bs
|| !bs
->drv
) {
3534 if (bs
->drv
->bdrv_cancel_in_flight
) {
3535 bs
->drv
->bdrv_cancel_in_flight(bs
);
3540 bdrv_co_preadv_snapshot(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
3541 QEMUIOVector
*qiov
, size_t qiov_offset
)
3543 BlockDriverState
*bs
= child
->bs
;
3544 BlockDriver
*drv
= bs
->drv
;
3547 assert_bdrv_graph_readable();
3553 if (!drv
->bdrv_co_preadv_snapshot
) {
3557 bdrv_inc_in_flight(bs
);
3558 ret
= drv
->bdrv_co_preadv_snapshot(bs
, offset
, bytes
, qiov
, qiov_offset
);
3559 bdrv_dec_in_flight(bs
);
3565 bdrv_co_snapshot_block_status(BlockDriverState
*bs
,
3566 bool want_zero
, int64_t offset
, int64_t bytes
,
3567 int64_t *pnum
, int64_t *map
,
3568 BlockDriverState
**file
)
3570 BlockDriver
*drv
= bs
->drv
;
3573 assert_bdrv_graph_readable();
3579 if (!drv
->bdrv_co_snapshot_block_status
) {
3583 bdrv_inc_in_flight(bs
);
3584 ret
= drv
->bdrv_co_snapshot_block_status(bs
, want_zero
, offset
, bytes
,
3586 bdrv_dec_in_flight(bs
);
3592 bdrv_co_pdiscard_snapshot(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
3594 BlockDriver
*drv
= bs
->drv
;
3597 assert_bdrv_graph_readable();
3603 if (!drv
->bdrv_co_pdiscard_snapshot
) {
3607 bdrv_inc_in_flight(bs
);
3608 ret
= drv
->bdrv_co_pdiscard_snapshot(bs
, offset
, bytes
);
3609 bdrv_dec_in_flight(bs
);