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
;
165 BdrvRefreshLimitsState
*s
= g_new(BdrvRefreshLimitsState
, 1);
166 *s
= (BdrvRefreshLimitsState
) {
170 tran_add(tran
, &bdrv_refresh_limits_drv
, s
);
173 memset(&bs
->bl
, 0, sizeof(bs
->bl
));
179 /* Default alignment based on whether driver has byte interface */
180 bs
->bl
.request_alignment
= (drv
->bdrv_co_preadv
||
181 drv
->bdrv_aio_preadv
||
182 drv
->bdrv_co_preadv_part
) ? 1 : 512;
184 /* Take some limits from the children as a default */
186 QLIST_FOREACH(c
, &bs
->children
, next
) {
187 if (c
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_FILTERED
| BDRV_CHILD_COW
))
189 bdrv_merge_limits(&bs
->bl
, &c
->bs
->bl
);
193 if (c
->role
& BDRV_CHILD_FILTERED
) {
194 bs
->bl
.has_variable_length
|= c
->bs
->bl
.has_variable_length
;
199 bs
->bl
.min_mem_alignment
= 512;
200 bs
->bl
.opt_mem_alignment
= qemu_real_host_page_size();
202 /* Safe default since most protocols use readv()/writev()/etc */
203 bs
->bl
.max_iov
= IOV_MAX
;
206 /* Then let the driver override it */
207 if (drv
->bdrv_refresh_limits
) {
208 drv
->bdrv_refresh_limits(bs
, errp
);
214 if (bs
->bl
.request_alignment
> BDRV_MAX_ALIGNMENT
) {
215 error_setg(errp
, "Driver requires too large request alignment");
220 * The copy-on-read flag is actually a reference count so multiple users may
221 * use the feature without worrying about clobbering its previous state.
222 * Copy-on-read stays enabled until all users have called to disable it.
224 void bdrv_enable_copy_on_read(BlockDriverState
*bs
)
227 qatomic_inc(&bs
->copy_on_read
);
230 void bdrv_disable_copy_on_read(BlockDriverState
*bs
)
232 int old
= qatomic_fetch_dec(&bs
->copy_on_read
);
239 BlockDriverState
*bs
;
246 /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
247 bool bdrv_drain_poll(BlockDriverState
*bs
, BdrvChild
*ignore_parent
,
248 bool ignore_bds_parents
)
252 if (bdrv_parent_drained_poll(bs
, ignore_parent
, ignore_bds_parents
)) {
256 if (qatomic_read(&bs
->in_flight
)) {
263 static bool bdrv_drain_poll_top_level(BlockDriverState
*bs
,
264 BdrvChild
*ignore_parent
)
266 return bdrv_drain_poll(bs
, ignore_parent
, false);
269 static void bdrv_do_drained_begin(BlockDriverState
*bs
, BdrvChild
*parent
,
271 static void bdrv_do_drained_end(BlockDriverState
*bs
, BdrvChild
*parent
);
273 static void bdrv_co_drain_bh_cb(void *opaque
)
275 BdrvCoDrainData
*data
= opaque
;
276 Coroutine
*co
= data
->co
;
277 BlockDriverState
*bs
= data
->bs
;
280 AioContext
*ctx
= bdrv_get_aio_context(bs
);
281 aio_context_acquire(ctx
);
282 bdrv_dec_in_flight(bs
);
284 bdrv_do_drained_begin(bs
, data
->parent
, data
->poll
);
287 bdrv_do_drained_end(bs
, data
->parent
);
289 aio_context_release(ctx
);
292 bdrv_drain_all_begin();
299 static void coroutine_fn
bdrv_co_yield_to_drain(BlockDriverState
*bs
,
304 BdrvCoDrainData data
;
305 Coroutine
*self
= qemu_coroutine_self();
306 AioContext
*ctx
= bdrv_get_aio_context(bs
);
307 AioContext
*co_ctx
= qemu_coroutine_get_aio_context(self
);
309 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
310 * other coroutines run if they were queued by aio_co_enter(). */
312 assert(qemu_in_coroutine());
313 data
= (BdrvCoDrainData
) {
323 bdrv_inc_in_flight(bs
);
327 * Temporarily drop the lock across yield or we would get deadlocks.
328 * bdrv_co_drain_bh_cb() reaquires the lock as needed.
330 * When we yield below, the lock for the current context will be
331 * released, so if this is actually the lock that protects bs, don't drop
335 aio_context_release(ctx
);
337 replay_bh_schedule_oneshot_event(qemu_get_aio_context(),
338 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
);
363 /* Stop things in parent-to-child order */
364 if (qatomic_fetch_inc(&bs
->quiesce_counter
) == 0) {
365 aio_disable_external(bdrv_get_aio_context(bs
));
366 bdrv_parent_drained_begin(bs
, parent
);
367 if (bs
->drv
&& bs
->drv
->bdrv_drain_begin
) {
368 bs
->drv
->bdrv_drain_begin(bs
);
373 * Wait for drained requests to finish.
375 * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
376 * call is needed so things in this AioContext can make progress even
377 * though we don't return to the main AioContext loop - this automatically
378 * includes other nodes in the same AioContext and therefore all child
382 BDRV_POLL_WHILE(bs
, bdrv_drain_poll_top_level(bs
, parent
));
386 void bdrv_do_drained_begin_quiesce(BlockDriverState
*bs
, BdrvChild
*parent
)
388 bdrv_do_drained_begin(bs
, parent
, false);
391 void bdrv_drained_begin(BlockDriverState
*bs
)
394 bdrv_do_drained_begin(bs
, NULL
, true);
398 * This function does not poll, nor must any of its recursively called
401 static void bdrv_do_drained_end(BlockDriverState
*bs
, BdrvChild
*parent
)
403 int old_quiesce_counter
;
407 if (qemu_in_coroutine()) {
408 bdrv_co_yield_to_drain(bs
, false, parent
, false);
411 assert(bs
->quiesce_counter
> 0);
414 /* Re-enable things in child-to-parent order */
415 old_quiesce_counter
= qatomic_fetch_dec(&bs
->quiesce_counter
);
416 if (old_quiesce_counter
== 1) {
417 if (bs
->drv
&& bs
->drv
->bdrv_drain_end
) {
418 bs
->drv
->bdrv_drain_end(bs
);
420 bdrv_parent_drained_end(bs
, parent
);
421 aio_enable_external(bdrv_get_aio_context(bs
));
425 void bdrv_drained_end(BlockDriverState
*bs
)
428 bdrv_do_drained_end(bs
, NULL
);
431 void bdrv_drain(BlockDriverState
*bs
)
434 bdrv_drained_begin(bs
);
435 bdrv_drained_end(bs
);
438 static void bdrv_drain_assert_idle(BlockDriverState
*bs
)
440 BdrvChild
*child
, *next
;
442 assert(qatomic_read(&bs
->in_flight
) == 0);
443 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, next
) {
444 bdrv_drain_assert_idle(child
->bs
);
448 unsigned int bdrv_drain_all_count
= 0;
450 static bool bdrv_drain_all_poll(void)
452 BlockDriverState
*bs
= NULL
;
456 /* bdrv_drain_poll() can't make changes to the graph and we are holding the
457 * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
458 while ((bs
= bdrv_next_all_states(bs
))) {
459 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
460 aio_context_acquire(aio_context
);
461 result
|= bdrv_drain_poll(bs
, NULL
, true);
462 aio_context_release(aio_context
);
469 * Wait for pending requests to complete across all BlockDriverStates
471 * This function does not flush data to disk, use bdrv_flush_all() for that
472 * after calling this function.
474 * This pauses all block jobs and disables external clients. It must
475 * be paired with bdrv_drain_all_end().
477 * NOTE: no new block jobs or BlockDriverStates can be created between
478 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
480 void bdrv_drain_all_begin_nopoll(void)
482 BlockDriverState
*bs
= NULL
;
486 * bdrv queue is managed by record/replay,
487 * waiting for finishing the I/O requests may
490 if (replay_events_enabled()) {
494 /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
495 * loop AioContext, so make sure we're in the main context. */
496 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
497 assert(bdrv_drain_all_count
< INT_MAX
);
498 bdrv_drain_all_count
++;
500 /* Quiesce all nodes, without polling in-flight requests yet. The graph
501 * cannot change during this loop. */
502 while ((bs
= bdrv_next_all_states(bs
))) {
503 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
505 aio_context_acquire(aio_context
);
506 bdrv_do_drained_begin(bs
, NULL
, false);
507 aio_context_release(aio_context
);
511 void bdrv_drain_all_begin(void)
513 BlockDriverState
*bs
= NULL
;
515 if (qemu_in_coroutine()) {
516 bdrv_co_yield_to_drain(NULL
, true, NULL
, true);
521 * bdrv queue is managed by record/replay,
522 * waiting for finishing the I/O requests may
525 if (replay_events_enabled()) {
529 bdrv_drain_all_begin_nopoll();
531 /* Now poll the in-flight requests */
532 AIO_WAIT_WHILE_UNLOCKED(NULL
, bdrv_drain_all_poll());
534 while ((bs
= bdrv_next_all_states(bs
))) {
535 bdrv_drain_assert_idle(bs
);
539 void bdrv_drain_all_end_quiesce(BlockDriverState
*bs
)
543 g_assert(bs
->quiesce_counter
> 0);
544 g_assert(!bs
->refcnt
);
546 while (bs
->quiesce_counter
) {
547 bdrv_do_drained_end(bs
, NULL
);
551 void bdrv_drain_all_end(void)
553 BlockDriverState
*bs
= NULL
;
557 * bdrv queue is managed by record/replay,
558 * waiting for finishing the I/O requests may
561 if (replay_events_enabled()) {
565 while ((bs
= bdrv_next_all_states(bs
))) {
566 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
568 aio_context_acquire(aio_context
);
569 bdrv_do_drained_end(bs
, NULL
);
570 aio_context_release(aio_context
);
573 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
574 assert(bdrv_drain_all_count
> 0);
575 bdrv_drain_all_count
--;
578 void bdrv_drain_all(void)
581 bdrv_drain_all_begin();
582 bdrv_drain_all_end();
586 * Remove an active request from the tracked requests list
588 * This function should be called when a tracked request is completing.
590 static void coroutine_fn
tracked_request_end(BdrvTrackedRequest
*req
)
592 if (req
->serialising
) {
593 qatomic_dec(&req
->bs
->serialising_in_flight
);
596 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
597 QLIST_REMOVE(req
, list
);
598 qemu_co_queue_restart_all(&req
->wait_queue
);
599 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
603 * Add an active request to the tracked requests list
605 static void coroutine_fn
tracked_request_begin(BdrvTrackedRequest
*req
,
606 BlockDriverState
*bs
,
609 enum BdrvTrackedRequestType type
)
611 bdrv_check_request(offset
, bytes
, &error_abort
);
613 *req
= (BdrvTrackedRequest
){
618 .co
= qemu_coroutine_self(),
619 .serialising
= false,
620 .overlap_offset
= offset
,
621 .overlap_bytes
= bytes
,
624 qemu_co_queue_init(&req
->wait_queue
);
626 qemu_co_mutex_lock(&bs
->reqs_lock
);
627 QLIST_INSERT_HEAD(&bs
->tracked_requests
, req
, list
);
628 qemu_co_mutex_unlock(&bs
->reqs_lock
);
631 static bool tracked_request_overlaps(BdrvTrackedRequest
*req
,
632 int64_t offset
, int64_t bytes
)
634 bdrv_check_request(offset
, bytes
, &error_abort
);
637 if (offset
>= req
->overlap_offset
+ req
->overlap_bytes
) {
641 if (req
->overlap_offset
>= offset
+ bytes
) {
647 /* Called with self->bs->reqs_lock held */
648 static coroutine_fn BdrvTrackedRequest
*
649 bdrv_find_conflicting_request(BdrvTrackedRequest
*self
)
651 BdrvTrackedRequest
*req
;
653 QLIST_FOREACH(req
, &self
->bs
->tracked_requests
, list
) {
654 if (req
== self
|| (!req
->serialising
&& !self
->serialising
)) {
657 if (tracked_request_overlaps(req
, self
->overlap_offset
,
658 self
->overlap_bytes
))
661 * Hitting this means there was a reentrant request, for
662 * example, a block driver issuing nested requests. This must
663 * never happen since it means deadlock.
665 assert(qemu_coroutine_self() != req
->co
);
668 * If the request is already (indirectly) waiting for us, or
669 * will wait for us as soon as it wakes up, then just go on
670 * (instead of producing a deadlock in the former case).
672 if (!req
->waiting_for
) {
681 /* Called with self->bs->reqs_lock held */
682 static void coroutine_fn
683 bdrv_wait_serialising_requests_locked(BdrvTrackedRequest
*self
)
685 BdrvTrackedRequest
*req
;
687 while ((req
= bdrv_find_conflicting_request(self
))) {
688 self
->waiting_for
= req
;
689 qemu_co_queue_wait(&req
->wait_queue
, &self
->bs
->reqs_lock
);
690 self
->waiting_for
= NULL
;
694 /* Called with req->bs->reqs_lock held */
695 static void tracked_request_set_serialising(BdrvTrackedRequest
*req
,
698 int64_t overlap_offset
= req
->offset
& ~(align
- 1);
699 int64_t overlap_bytes
=
700 ROUND_UP(req
->offset
+ req
->bytes
, align
) - overlap_offset
;
702 bdrv_check_request(req
->offset
, req
->bytes
, &error_abort
);
704 if (!req
->serialising
) {
705 qatomic_inc(&req
->bs
->serialising_in_flight
);
706 req
->serialising
= true;
709 req
->overlap_offset
= MIN(req
->overlap_offset
, overlap_offset
);
710 req
->overlap_bytes
= MAX(req
->overlap_bytes
, overlap_bytes
);
714 * Return the tracked request on @bs for the current coroutine, or
715 * NULL if there is none.
717 BdrvTrackedRequest
*coroutine_fn
bdrv_co_get_self_request(BlockDriverState
*bs
)
719 BdrvTrackedRequest
*req
;
720 Coroutine
*self
= qemu_coroutine_self();
723 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
724 if (req
->co
== self
) {
733 * Round a region to cluster boundaries
735 void coroutine_fn GRAPH_RDLOCK
736 bdrv_round_to_clusters(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
737 int64_t *cluster_offset
, int64_t *cluster_bytes
)
741 if (bdrv_co_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
742 *cluster_offset
= offset
;
743 *cluster_bytes
= bytes
;
745 int64_t c
= bdi
.cluster_size
;
746 *cluster_offset
= QEMU_ALIGN_DOWN(offset
, c
);
747 *cluster_bytes
= QEMU_ALIGN_UP(offset
- *cluster_offset
+ bytes
, c
);
751 static int coroutine_fn GRAPH_RDLOCK
bdrv_get_cluster_size(BlockDriverState
*bs
)
756 ret
= bdrv_co_get_info(bs
, &bdi
);
757 if (ret
< 0 || bdi
.cluster_size
== 0) {
758 return bs
->bl
.request_alignment
;
760 return bdi
.cluster_size
;
764 void bdrv_inc_in_flight(BlockDriverState
*bs
)
767 qatomic_inc(&bs
->in_flight
);
770 void bdrv_wakeup(BlockDriverState
*bs
)
776 void bdrv_dec_in_flight(BlockDriverState
*bs
)
779 qatomic_dec(&bs
->in_flight
);
783 static void coroutine_fn
784 bdrv_wait_serialising_requests(BdrvTrackedRequest
*self
)
786 BlockDriverState
*bs
= self
->bs
;
788 if (!qatomic_read(&bs
->serialising_in_flight
)) {
792 qemu_co_mutex_lock(&bs
->reqs_lock
);
793 bdrv_wait_serialising_requests_locked(self
);
794 qemu_co_mutex_unlock(&bs
->reqs_lock
);
797 void coroutine_fn
bdrv_make_request_serialising(BdrvTrackedRequest
*req
,
802 qemu_co_mutex_lock(&req
->bs
->reqs_lock
);
804 tracked_request_set_serialising(req
, align
);
805 bdrv_wait_serialising_requests_locked(req
);
807 qemu_co_mutex_unlock(&req
->bs
->reqs_lock
);
810 int bdrv_check_qiov_request(int64_t offset
, int64_t bytes
,
811 QEMUIOVector
*qiov
, size_t qiov_offset
,
815 * Check generic offset/bytes correctness
819 error_setg(errp
, "offset is negative: %" PRIi64
, offset
);
824 error_setg(errp
, "bytes is negative: %" PRIi64
, bytes
);
828 if (bytes
> BDRV_MAX_LENGTH
) {
829 error_setg(errp
, "bytes(%" PRIi64
") exceeds maximum(%" PRIi64
")",
830 bytes
, BDRV_MAX_LENGTH
);
834 if (offset
> BDRV_MAX_LENGTH
) {
835 error_setg(errp
, "offset(%" PRIi64
") exceeds maximum(%" PRIi64
")",
836 offset
, BDRV_MAX_LENGTH
);
840 if (offset
> BDRV_MAX_LENGTH
- bytes
) {
841 error_setg(errp
, "sum of offset(%" PRIi64
") and bytes(%" PRIi64
") "
842 "exceeds maximum(%" PRIi64
")", offset
, bytes
,
852 * Check qiov and qiov_offset
855 if (qiov_offset
> qiov
->size
) {
856 error_setg(errp
, "qiov_offset(%zu) overflow io vector size(%zu)",
857 qiov_offset
, qiov
->size
);
861 if (bytes
> qiov
->size
- qiov_offset
) {
862 error_setg(errp
, "bytes(%" PRIi64
") + qiov_offset(%zu) overflow io "
863 "vector size(%zu)", bytes
, qiov_offset
, qiov
->size
);
870 int bdrv_check_request(int64_t offset
, int64_t bytes
, Error
**errp
)
872 return bdrv_check_qiov_request(offset
, bytes
, NULL
, 0, errp
);
875 static int bdrv_check_request32(int64_t offset
, int64_t bytes
,
876 QEMUIOVector
*qiov
, size_t qiov_offset
)
878 int ret
= bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, NULL
);
883 if (bytes
> BDRV_REQUEST_MAX_BYTES
) {
891 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
892 * The operation is sped up by checking the block status and only writing
893 * zeroes to the device if they currently do not return zeroes. Optional
894 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
897 * Returns < 0 on error, 0 on success. For error codes see bdrv_pwrite().
899 int bdrv_make_zero(BdrvChild
*child
, BdrvRequestFlags flags
)
902 int64_t target_size
, bytes
, offset
= 0;
903 BlockDriverState
*bs
= child
->bs
;
906 target_size
= bdrv_getlength(bs
);
907 if (target_size
< 0) {
912 bytes
= MIN(target_size
- offset
, BDRV_REQUEST_MAX_BYTES
);
916 ret
= bdrv_block_status(bs
, offset
, bytes
, &bytes
, NULL
, NULL
);
920 if (ret
& BDRV_BLOCK_ZERO
) {
924 ret
= bdrv_pwrite_zeroes(child
, offset
, bytes
, flags
);
933 * Writes to the file and ensures that no writes are reordered across this
934 * request (acts as a barrier)
936 * Returns 0 on success, -errno in error cases.
938 int coroutine_fn
bdrv_co_pwrite_sync(BdrvChild
*child
, int64_t offset
,
939 int64_t bytes
, const void *buf
,
940 BdrvRequestFlags flags
)
944 assert_bdrv_graph_readable();
946 ret
= bdrv_co_pwrite(child
, offset
, bytes
, buf
, flags
);
951 ret
= bdrv_co_flush(child
->bs
);
959 typedef struct CoroutineIOCompletion
{
960 Coroutine
*coroutine
;
962 } CoroutineIOCompletion
;
964 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
966 CoroutineIOCompletion
*co
= opaque
;
969 aio_co_wake(co
->coroutine
);
972 static int coroutine_fn GRAPH_RDLOCK
973 bdrv_driver_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
974 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
976 BlockDriver
*drv
= bs
->drv
;
978 unsigned int nb_sectors
;
979 QEMUIOVector local_qiov
;
981 assert_bdrv_graph_readable();
983 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
984 assert(!(flags
& ~bs
->supported_read_flags
));
990 if (drv
->bdrv_co_preadv_part
) {
991 return drv
->bdrv_co_preadv_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
995 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
996 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1000 if (drv
->bdrv_co_preadv
) {
1001 ret
= drv
->bdrv_co_preadv(bs
, offset
, bytes
, qiov
, flags
);
1005 if (drv
->bdrv_aio_preadv
) {
1007 CoroutineIOCompletion co
= {
1008 .coroutine
= qemu_coroutine_self(),
1011 acb
= drv
->bdrv_aio_preadv(bs
, offset
, bytes
, qiov
, flags
,
1012 bdrv_co_io_em_complete
, &co
);
1017 qemu_coroutine_yield();
1023 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1024 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1026 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1027 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1028 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1029 assert(drv
->bdrv_co_readv
);
1031 ret
= drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
1034 if (qiov
== &local_qiov
) {
1035 qemu_iovec_destroy(&local_qiov
);
1041 static int coroutine_fn GRAPH_RDLOCK
1042 bdrv_driver_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1043 QEMUIOVector
*qiov
, size_t qiov_offset
,
1044 BdrvRequestFlags flags
)
1046 BlockDriver
*drv
= bs
->drv
;
1047 bool emulate_fua
= false;
1049 unsigned int nb_sectors
;
1050 QEMUIOVector local_qiov
;
1052 assert_bdrv_graph_readable();
1054 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1060 if ((flags
& BDRV_REQ_FUA
) &&
1061 (~bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1062 flags
&= ~BDRV_REQ_FUA
;
1066 flags
&= bs
->supported_write_flags
;
1068 if (drv
->bdrv_co_pwritev_part
) {
1069 ret
= drv
->bdrv_co_pwritev_part(bs
, offset
, bytes
, qiov
, qiov_offset
,
1074 if (qiov_offset
> 0 || bytes
!= qiov
->size
) {
1075 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1079 if (drv
->bdrv_co_pwritev
) {
1080 ret
= drv
->bdrv_co_pwritev(bs
, offset
, bytes
, qiov
, flags
);
1084 if (drv
->bdrv_aio_pwritev
) {
1086 CoroutineIOCompletion co
= {
1087 .coroutine
= qemu_coroutine_self(),
1090 acb
= drv
->bdrv_aio_pwritev(bs
, offset
, bytes
, qiov
, flags
,
1091 bdrv_co_io_em_complete
, &co
);
1095 qemu_coroutine_yield();
1101 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1102 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1104 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
1105 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
1106 assert(bytes
<= BDRV_REQUEST_MAX_BYTES
);
1108 assert(drv
->bdrv_co_writev
);
1109 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
, flags
);
1112 if (ret
== 0 && emulate_fua
) {
1113 ret
= bdrv_co_flush(bs
);
1116 if (qiov
== &local_qiov
) {
1117 qemu_iovec_destroy(&local_qiov
);
1123 static int coroutine_fn GRAPH_RDLOCK
1124 bdrv_driver_pwritev_compressed(BlockDriverState
*bs
, int64_t offset
,
1125 int64_t bytes
, QEMUIOVector
*qiov
,
1128 BlockDriver
*drv
= bs
->drv
;
1129 QEMUIOVector local_qiov
;
1131 assert_bdrv_graph_readable();
1133 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1139 if (!block_driver_can_compress(drv
)) {
1143 if (drv
->bdrv_co_pwritev_compressed_part
) {
1144 return drv
->bdrv_co_pwritev_compressed_part(bs
, offset
, bytes
,
1148 if (qiov_offset
== 0) {
1149 return drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, qiov
);
1152 qemu_iovec_init_slice(&local_qiov
, qiov
, qiov_offset
, bytes
);
1153 ret
= drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, &local_qiov
);
1154 qemu_iovec_destroy(&local_qiov
);
1159 static int coroutine_fn GRAPH_RDLOCK
1160 bdrv_co_do_copy_on_readv(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1161 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
1163 BlockDriverState
*bs
= child
->bs
;
1165 /* Perform I/O through a temporary buffer so that users who scribble over
1166 * their read buffer while the operation is in progress do not end up
1167 * modifying the image file. This is critical for zero-copy guest I/O
1168 * where anything might happen inside guest memory.
1170 void *bounce_buffer
= NULL
;
1172 BlockDriver
*drv
= bs
->drv
;
1173 int64_t cluster_offset
;
1174 int64_t cluster_bytes
;
1177 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
,
1178 BDRV_REQUEST_MAX_BYTES
);
1179 int64_t progress
= 0;
1182 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1189 * Do not write anything when the BDS is inactive. That is not
1190 * allowed, and it would not help.
1192 skip_write
= (bs
->open_flags
& BDRV_O_INACTIVE
);
1194 /* FIXME We cannot require callers to have write permissions when all they
1195 * are doing is a read request. If we did things right, write permissions
1196 * would be obtained anyway, but internally by the copy-on-read code. As
1197 * long as it is implemented here rather than in a separate filter driver,
1198 * the copy-on-read code doesn't have its own BdrvChild, however, for which
1199 * it could request permissions. Therefore we have to bypass the permission
1200 * system for the moment. */
1201 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1203 /* Cover entire cluster so no additional backing file I/O is required when
1204 * allocating cluster in the image file. Note that this value may exceed
1205 * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1206 * is one reason we loop rather than doing it all at once.
1208 bdrv_round_to_clusters(bs
, offset
, bytes
, &cluster_offset
, &cluster_bytes
);
1209 skip_bytes
= offset
- cluster_offset
;
1211 trace_bdrv_co_do_copy_on_readv(bs
, offset
, bytes
,
1212 cluster_offset
, cluster_bytes
);
1214 while (cluster_bytes
) {
1218 ret
= 1; /* "already allocated", so nothing will be copied */
1219 pnum
= MIN(cluster_bytes
, max_transfer
);
1221 ret
= bdrv_is_allocated(bs
, cluster_offset
,
1222 MIN(cluster_bytes
, max_transfer
), &pnum
);
1225 * Safe to treat errors in querying allocation as if
1226 * unallocated; we'll probably fail again soon on the
1227 * read, but at least that will set a decent errno.
1229 pnum
= MIN(cluster_bytes
, max_transfer
);
1232 /* Stop at EOF if the image ends in the middle of the cluster */
1233 if (ret
== 0 && pnum
== 0) {
1234 assert(progress
>= bytes
);
1238 assert(skip_bytes
< pnum
);
1242 QEMUIOVector local_qiov
;
1244 /* Must copy-on-read; use the bounce buffer */
1245 pnum
= MIN(pnum
, MAX_BOUNCE_BUFFER
);
1246 if (!bounce_buffer
) {
1247 int64_t max_we_need
= MAX(pnum
, cluster_bytes
- pnum
);
1248 int64_t max_allowed
= MIN(max_transfer
, MAX_BOUNCE_BUFFER
);
1249 int64_t bounce_buffer_len
= MIN(max_we_need
, max_allowed
);
1251 bounce_buffer
= qemu_try_blockalign(bs
, bounce_buffer_len
);
1252 if (!bounce_buffer
) {
1257 qemu_iovec_init_buf(&local_qiov
, bounce_buffer
, pnum
);
1259 ret
= bdrv_driver_preadv(bs
, cluster_offset
, pnum
,
1265 bdrv_co_debug_event(bs
, BLKDBG_COR_WRITE
);
1266 if (drv
->bdrv_co_pwrite_zeroes
&&
1267 buffer_is_zero(bounce_buffer
, pnum
)) {
1268 /* FIXME: Should we (perhaps conditionally) be setting
1269 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1270 * that still correctly reads as zero? */
1271 ret
= bdrv_co_do_pwrite_zeroes(bs
, cluster_offset
, pnum
,
1272 BDRV_REQ_WRITE_UNCHANGED
);
1274 /* This does not change the data on the disk, it is not
1275 * necessary to flush even in cache=writethrough mode.
1277 ret
= bdrv_driver_pwritev(bs
, cluster_offset
, pnum
,
1279 BDRV_REQ_WRITE_UNCHANGED
);
1283 /* It might be okay to ignore write errors for guest
1284 * requests. If this is a deliberate copy-on-read
1285 * then we don't want to ignore the error. Simply
1286 * report it in all cases.
1291 if (!(flags
& BDRV_REQ_PREFETCH
)) {
1292 qemu_iovec_from_buf(qiov
, qiov_offset
+ progress
,
1293 bounce_buffer
+ skip_bytes
,
1294 MIN(pnum
- skip_bytes
, bytes
- progress
));
1296 } else if (!(flags
& BDRV_REQ_PREFETCH
)) {
1297 /* Read directly into the destination */
1298 ret
= bdrv_driver_preadv(bs
, offset
+ progress
,
1299 MIN(pnum
- skip_bytes
, bytes
- progress
),
1300 qiov
, qiov_offset
+ progress
, 0);
1306 cluster_offset
+= pnum
;
1307 cluster_bytes
-= pnum
;
1308 progress
+= pnum
- skip_bytes
;
1314 qemu_vfree(bounce_buffer
);
1319 * Forwards an already correctly aligned request to the BlockDriver. This
1320 * handles copy on read, zeroing after EOF, and fragmentation of large
1321 * reads; any other features must be implemented by the caller.
1323 static int coroutine_fn GRAPH_RDLOCK
1324 bdrv_aligned_preadv(BdrvChild
*child
, BdrvTrackedRequest
*req
,
1325 int64_t offset
, int64_t bytes
, int64_t align
,
1326 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
1328 BlockDriverState
*bs
= child
->bs
;
1329 int64_t total_bytes
, max_bytes
;
1331 int64_t bytes_remaining
= bytes
;
1334 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1335 assert(is_power_of_2(align
));
1336 assert((offset
& (align
- 1)) == 0);
1337 assert((bytes
& (align
- 1)) == 0);
1338 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1339 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1343 * TODO: We would need a per-BDS .supported_read_flags and
1344 * potential fallback support, if we ever implement any read flags
1345 * to pass through to drivers. For now, there aren't any
1346 * passthrough flags except the BDRV_REQ_REGISTERED_BUF optimization hint.
1348 assert(!(flags
& ~(BDRV_REQ_COPY_ON_READ
| BDRV_REQ_PREFETCH
|
1349 BDRV_REQ_REGISTERED_BUF
)));
1351 /* Handle Copy on Read and associated serialisation */
1352 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1353 /* If we touch the same cluster it counts as an overlap. This
1354 * guarantees that allocating writes will be serialized and not race
1355 * with each other for the same cluster. For example, in copy-on-read
1356 * it ensures that the CoR read and write operations are atomic and
1357 * guest writes cannot interleave between them. */
1358 bdrv_make_request_serialising(req
, bdrv_get_cluster_size(bs
));
1360 bdrv_wait_serialising_requests(req
);
1363 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1366 /* The flag BDRV_REQ_COPY_ON_READ has reached its addressee */
1367 flags
&= ~BDRV_REQ_COPY_ON_READ
;
1369 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &pnum
);
1374 if (!ret
|| pnum
!= bytes
) {
1375 ret
= bdrv_co_do_copy_on_readv(child
, offset
, bytes
,
1376 qiov
, qiov_offset
, flags
);
1378 } else if (flags
& BDRV_REQ_PREFETCH
) {
1383 /* Forward the request to the BlockDriver, possibly fragmenting it */
1384 total_bytes
= bdrv_getlength(bs
);
1385 if (total_bytes
< 0) {
1390 assert(!(flags
& ~(bs
->supported_read_flags
| BDRV_REQ_REGISTERED_BUF
)));
1392 max_bytes
= ROUND_UP(MAX(0, total_bytes
- offset
), align
);
1393 if (bytes
<= max_bytes
&& bytes
<= max_transfer
) {
1394 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1398 while (bytes_remaining
) {
1402 num
= MIN(bytes_remaining
, MIN(max_bytes
, max_transfer
));
1405 ret
= bdrv_driver_preadv(bs
, offset
+ bytes
- bytes_remaining
,
1407 qiov_offset
+ bytes
- bytes_remaining
,
1411 num
= bytes_remaining
;
1412 ret
= qemu_iovec_memset(qiov
, qiov_offset
+ bytes
- bytes_remaining
,
1413 0, bytes_remaining
);
1418 bytes_remaining
-= num
;
1422 return ret
< 0 ? ret
: 0;
1428 * |<---- align ----->| |<----- align ---->|
1429 * |<- head ->|<------------- bytes ------------->|<-- tail -->|
1431 * -*----------$-------*-------- ... --------*-----$------------*---
1433 * | offset | | end |
1434 * ALIGN_DOWN(offset) ALIGN_UP(offset) ALIGN_DOWN(end) ALIGN_UP(end)
1435 * [buf ... ) [tail_buf )
1437 * @buf is an aligned allocation needed to store @head and @tail paddings. @head
1438 * is placed at the beginning of @buf and @tail at the @end.
1440 * @tail_buf is a pointer to sub-buffer, corresponding to align-sized chunk
1441 * around tail, if tail exists.
1443 * @merge_reads is true for small requests,
1444 * if @buf_len == @head + bytes + @tail. In this case it is possible that both
1445 * head and tail exist but @buf_len == align and @tail_buf == @buf.
1447 typedef struct BdrvRequestPadding
{
1454 QEMUIOVector local_qiov
;
1455 } BdrvRequestPadding
;
1457 static bool bdrv_init_padding(BlockDriverState
*bs
,
1458 int64_t offset
, int64_t bytes
,
1459 BdrvRequestPadding
*pad
)
1461 int64_t align
= bs
->bl
.request_alignment
;
1464 bdrv_check_request(offset
, bytes
, &error_abort
);
1465 assert(align
<= INT_MAX
); /* documented in block/block_int.h */
1466 assert(align
<= SIZE_MAX
/ 2); /* so we can allocate the buffer */
1468 memset(pad
, 0, sizeof(*pad
));
1470 pad
->head
= offset
& (align
- 1);
1471 pad
->tail
= ((offset
+ bytes
) & (align
- 1));
1473 pad
->tail
= align
- pad
->tail
;
1476 if (!pad
->head
&& !pad
->tail
) {
1480 assert(bytes
); /* Nothing good in aligning zero-length requests */
1482 sum
= pad
->head
+ bytes
+ pad
->tail
;
1483 pad
->buf_len
= (sum
> align
&& pad
->head
&& pad
->tail
) ? 2 * align
: align
;
1484 pad
->buf
= qemu_blockalign(bs
, pad
->buf_len
);
1485 pad
->merge_reads
= sum
== pad
->buf_len
;
1487 pad
->tail_buf
= pad
->buf
+ pad
->buf_len
- align
;
1493 static int coroutine_fn GRAPH_RDLOCK
1494 bdrv_padding_rmw_read(BdrvChild
*child
, BdrvTrackedRequest
*req
,
1495 BdrvRequestPadding
*pad
, bool zero_middle
)
1497 QEMUIOVector local_qiov
;
1498 BlockDriverState
*bs
= child
->bs
;
1499 uint64_t align
= bs
->bl
.request_alignment
;
1502 assert(req
->serialising
&& pad
->buf
);
1504 if (pad
->head
|| pad
->merge_reads
) {
1505 int64_t bytes
= pad
->merge_reads
? pad
->buf_len
: align
;
1507 qemu_iovec_init_buf(&local_qiov
, pad
->buf
, bytes
);
1510 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1512 if (pad
->merge_reads
&& pad
->tail
) {
1513 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1515 ret
= bdrv_aligned_preadv(child
, req
, req
->overlap_offset
, bytes
,
1516 align
, &local_qiov
, 0, 0);
1521 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1523 if (pad
->merge_reads
&& pad
->tail
) {
1524 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1527 if (pad
->merge_reads
) {
1533 qemu_iovec_init_buf(&local_qiov
, pad
->tail_buf
, align
);
1535 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1536 ret
= bdrv_aligned_preadv(
1538 req
->overlap_offset
+ req
->overlap_bytes
- align
,
1539 align
, align
, &local_qiov
, 0, 0);
1543 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1548 memset(pad
->buf
+ pad
->head
, 0, pad
->buf_len
- pad
->head
- pad
->tail
);
1554 static void bdrv_padding_destroy(BdrvRequestPadding
*pad
)
1557 qemu_vfree(pad
->buf
);
1558 qemu_iovec_destroy(&pad
->local_qiov
);
1560 memset(pad
, 0, sizeof(*pad
));
1566 * Exchange request parameters with padded request if needed. Don't include RMW
1567 * read of padding, bdrv_padding_rmw_read() should be called separately if
1570 * Request parameters (@qiov, &qiov_offset, &offset, &bytes) are in-out:
1571 * - on function start they represent original request
1572 * - on failure or when padding is not needed they are unchanged
1573 * - on success when padding is needed they represent padded request
1575 static int bdrv_pad_request(BlockDriverState
*bs
,
1576 QEMUIOVector
**qiov
, size_t *qiov_offset
,
1577 int64_t *offset
, int64_t *bytes
,
1578 BdrvRequestPadding
*pad
, bool *padded
,
1579 BdrvRequestFlags
*flags
)
1583 bdrv_check_qiov_request(*offset
, *bytes
, *qiov
, *qiov_offset
, &error_abort
);
1585 if (!bdrv_init_padding(bs
, *offset
, *bytes
, pad
)) {
1592 ret
= qemu_iovec_init_extended(&pad
->local_qiov
, pad
->buf
, pad
->head
,
1593 *qiov
, *qiov_offset
, *bytes
,
1594 pad
->buf
+ pad
->buf_len
- pad
->tail
,
1597 bdrv_padding_destroy(pad
);
1600 *bytes
+= pad
->head
+ pad
->tail
;
1601 *offset
-= pad
->head
;
1602 *qiov
= &pad
->local_qiov
;
1608 /* Can't use optimization hint with bounce buffer */
1609 *flags
&= ~BDRV_REQ_REGISTERED_BUF
;
1615 int coroutine_fn
bdrv_co_preadv(BdrvChild
*child
,
1616 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
1617 BdrvRequestFlags flags
)
1620 return bdrv_co_preadv_part(child
, offset
, bytes
, qiov
, 0, flags
);
1623 int coroutine_fn
bdrv_co_preadv_part(BdrvChild
*child
,
1624 int64_t offset
, int64_t bytes
,
1625 QEMUIOVector
*qiov
, size_t qiov_offset
,
1626 BdrvRequestFlags flags
)
1628 BlockDriverState
*bs
= child
->bs
;
1629 BdrvTrackedRequest req
;
1630 BdrvRequestPadding pad
;
1634 trace_bdrv_co_preadv_part(bs
, offset
, bytes
, flags
);
1636 if (!bdrv_co_is_inserted(bs
)) {
1640 ret
= bdrv_check_request32(offset
, bytes
, qiov
, qiov_offset
);
1645 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
1647 * Aligning zero request is nonsense. Even if driver has special meaning
1648 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
1649 * it to driver due to request_alignment.
1651 * Still, no reason to return an error if someone do unaligned
1652 * zero-length read occasionally.
1657 bdrv_inc_in_flight(bs
);
1659 /* Don't do copy-on-read if we read data before write operation */
1660 if (qatomic_read(&bs
->copy_on_read
)) {
1661 flags
|= BDRV_REQ_COPY_ON_READ
;
1664 ret
= bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
,
1670 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_READ
);
1671 ret
= bdrv_aligned_preadv(child
, &req
, offset
, bytes
,
1672 bs
->bl
.request_alignment
,
1673 qiov
, qiov_offset
, flags
);
1674 tracked_request_end(&req
);
1675 bdrv_padding_destroy(&pad
);
1678 bdrv_dec_in_flight(bs
);
1683 static int coroutine_fn GRAPH_RDLOCK
1684 bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1685 BdrvRequestFlags flags
)
1687 BlockDriver
*drv
= bs
->drv
;
1691 bool need_flush
= false;
1695 int64_t max_write_zeroes
= MIN_NON_ZERO(bs
->bl
.max_pwrite_zeroes
,
1697 int alignment
= MAX(bs
->bl
.pwrite_zeroes_alignment
,
1698 bs
->bl
.request_alignment
);
1699 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
, MAX_BOUNCE_BUFFER
);
1701 assert_bdrv_graph_readable();
1702 bdrv_check_request(offset
, bytes
, &error_abort
);
1708 if ((flags
& ~bs
->supported_zero_flags
) & BDRV_REQ_NO_FALLBACK
) {
1712 /* By definition there is no user buffer so this flag doesn't make sense */
1713 if (flags
& BDRV_REQ_REGISTERED_BUF
) {
1717 /* Invalidate the cached block-status data range if this write overlaps */
1718 bdrv_bsc_invalidate_range(bs
, offset
, bytes
);
1720 assert(alignment
% bs
->bl
.request_alignment
== 0);
1721 head
= offset
% alignment
;
1722 tail
= (offset
+ bytes
) % alignment
;
1723 max_write_zeroes
= QEMU_ALIGN_DOWN(max_write_zeroes
, alignment
);
1724 assert(max_write_zeroes
>= bs
->bl
.request_alignment
);
1726 while (bytes
> 0 && !ret
) {
1727 int64_t num
= bytes
;
1729 /* Align request. Block drivers can expect the "bulk" of the request
1730 * to be aligned, and that unaligned requests do not cross cluster
1734 /* Make a small request up to the first aligned sector. For
1735 * convenience, limit this request to max_transfer even if
1736 * we don't need to fall back to writes. */
1737 num
= MIN(MIN(bytes
, max_transfer
), alignment
- head
);
1738 head
= (head
+ num
) % alignment
;
1739 assert(num
< max_write_zeroes
);
1740 } else if (tail
&& num
> alignment
) {
1741 /* Shorten the request to the last aligned sector. */
1745 /* limit request size */
1746 if (num
> max_write_zeroes
) {
1747 num
= max_write_zeroes
;
1751 /* First try the efficient write zeroes operation */
1752 if (drv
->bdrv_co_pwrite_zeroes
) {
1753 ret
= drv
->bdrv_co_pwrite_zeroes(bs
, offset
, num
,
1754 flags
& bs
->supported_zero_flags
);
1755 if (ret
!= -ENOTSUP
&& (flags
& BDRV_REQ_FUA
) &&
1756 !(bs
->supported_zero_flags
& BDRV_REQ_FUA
)) {
1760 assert(!bs
->supported_zero_flags
);
1763 if (ret
== -ENOTSUP
&& !(flags
& BDRV_REQ_NO_FALLBACK
)) {
1764 /* Fall back to bounce buffer if write zeroes is unsupported */
1765 BdrvRequestFlags write_flags
= flags
& ~BDRV_REQ_ZERO_WRITE
;
1767 if ((flags
& BDRV_REQ_FUA
) &&
1768 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1769 /* No need for bdrv_driver_pwrite() to do a fallback
1770 * flush on each chunk; use just one at the end */
1771 write_flags
&= ~BDRV_REQ_FUA
;
1774 num
= MIN(num
, max_transfer
);
1776 buf
= qemu_try_blockalign0(bs
, num
);
1782 qemu_iovec_init_buf(&qiov
, buf
, num
);
1784 ret
= bdrv_driver_pwritev(bs
, offset
, num
, &qiov
, 0, write_flags
);
1786 /* Keep bounce buffer around if it is big enough for all
1787 * all future requests.
1789 if (num
< max_transfer
) {
1800 if (ret
== 0 && need_flush
) {
1801 ret
= bdrv_co_flush(bs
);
1807 static inline int coroutine_fn GRAPH_RDLOCK
1808 bdrv_co_write_req_prepare(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1809 BdrvTrackedRequest
*req
, int flags
)
1811 BlockDriverState
*bs
= child
->bs
;
1813 bdrv_check_request(offset
, bytes
, &error_abort
);
1815 if (bdrv_is_read_only(bs
)) {
1819 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
1820 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1821 assert(!(flags
& ~BDRV_REQ_MASK
));
1822 assert(!((flags
& BDRV_REQ_NO_WAIT
) && !(flags
& BDRV_REQ_SERIALISING
)));
1824 if (flags
& BDRV_REQ_SERIALISING
) {
1825 QEMU_LOCK_GUARD(&bs
->reqs_lock
);
1827 tracked_request_set_serialising(req
, bdrv_get_cluster_size(bs
));
1829 if ((flags
& BDRV_REQ_NO_WAIT
) && bdrv_find_conflicting_request(req
)) {
1833 bdrv_wait_serialising_requests_locked(req
);
1835 bdrv_wait_serialising_requests(req
);
1838 assert(req
->overlap_offset
<= offset
);
1839 assert(offset
+ bytes
<= req
->overlap_offset
+ req
->overlap_bytes
);
1840 assert(offset
+ bytes
<= bs
->total_sectors
* BDRV_SECTOR_SIZE
||
1841 child
->perm
& BLK_PERM_RESIZE
);
1843 switch (req
->type
) {
1844 case BDRV_TRACKED_WRITE
:
1845 case BDRV_TRACKED_DISCARD
:
1846 if (flags
& BDRV_REQ_WRITE_UNCHANGED
) {
1847 assert(child
->perm
& (BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_WRITE
));
1849 assert(child
->perm
& BLK_PERM_WRITE
);
1851 bdrv_write_threshold_check_write(bs
, offset
, bytes
);
1853 case BDRV_TRACKED_TRUNCATE
:
1854 assert(child
->perm
& BLK_PERM_RESIZE
);
1861 static inline void coroutine_fn
1862 bdrv_co_write_req_finish(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1863 BdrvTrackedRequest
*req
, int ret
)
1865 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
1866 BlockDriverState
*bs
= child
->bs
;
1868 bdrv_check_request(offset
, bytes
, &error_abort
);
1870 qatomic_inc(&bs
->write_gen
);
1873 * Discard cannot extend the image, but in error handling cases, such as
1874 * when reverting a qcow2 cluster allocation, the discarded range can pass
1875 * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
1876 * here. Instead, just skip it, since semantically a discard request
1877 * beyond EOF cannot expand the image anyway.
1880 (req
->type
== BDRV_TRACKED_TRUNCATE
||
1881 end_sector
> bs
->total_sectors
) &&
1882 req
->type
!= BDRV_TRACKED_DISCARD
) {
1883 bs
->total_sectors
= end_sector
;
1884 bdrv_parent_cb_resize(bs
);
1885 bdrv_dirty_bitmap_truncate(bs
, end_sector
<< BDRV_SECTOR_BITS
);
1888 switch (req
->type
) {
1889 case BDRV_TRACKED_WRITE
:
1890 stat64_max(&bs
->wr_highest_offset
, offset
+ bytes
);
1891 /* fall through, to set dirty bits */
1892 case BDRV_TRACKED_DISCARD
:
1893 bdrv_set_dirty(bs
, offset
, bytes
);
1902 * Forwards an already correctly aligned write request to the BlockDriver,
1903 * after possibly fragmenting it.
1905 static int coroutine_fn GRAPH_RDLOCK
1906 bdrv_aligned_pwritev(BdrvChild
*child
, BdrvTrackedRequest
*req
,
1907 int64_t offset
, int64_t bytes
, int64_t align
,
1908 QEMUIOVector
*qiov
, size_t qiov_offset
,
1909 BdrvRequestFlags flags
)
1911 BlockDriverState
*bs
= child
->bs
;
1912 BlockDriver
*drv
= bs
->drv
;
1915 int64_t bytes_remaining
= bytes
;
1918 bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, &error_abort
);
1924 if (bdrv_has_readonly_bitmaps(bs
)) {
1928 assert(is_power_of_2(align
));
1929 assert((offset
& (align
- 1)) == 0);
1930 assert((bytes
& (align
- 1)) == 0);
1931 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1934 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, req
, flags
);
1936 if (!ret
&& bs
->detect_zeroes
!= BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
&&
1937 !(flags
& BDRV_REQ_ZERO_WRITE
) && drv
->bdrv_co_pwrite_zeroes
&&
1938 qemu_iovec_is_zero(qiov
, qiov_offset
, bytes
)) {
1939 flags
|= BDRV_REQ_ZERO_WRITE
;
1940 if (bs
->detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
) {
1941 flags
|= BDRV_REQ_MAY_UNMAP
;
1944 /* Can't use optimization hint with bufferless zero write */
1945 flags
&= ~BDRV_REQ_REGISTERED_BUF
;
1949 /* Do nothing, write notifier decided to fail this request */
1950 } else if (flags
& BDRV_REQ_ZERO_WRITE
) {
1951 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_ZERO
);
1952 ret
= bdrv_co_do_pwrite_zeroes(bs
, offset
, bytes
, flags
);
1953 } else if (flags
& BDRV_REQ_WRITE_COMPRESSED
) {
1954 ret
= bdrv_driver_pwritev_compressed(bs
, offset
, bytes
,
1956 } else if (bytes
<= max_transfer
) {
1957 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV
);
1958 ret
= bdrv_driver_pwritev(bs
, offset
, bytes
, qiov
, qiov_offset
, flags
);
1960 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV
);
1961 while (bytes_remaining
) {
1962 int num
= MIN(bytes_remaining
, max_transfer
);
1963 int local_flags
= flags
;
1966 if (num
< bytes_remaining
&& (flags
& BDRV_REQ_FUA
) &&
1967 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1968 /* If FUA is going to be emulated by flush, we only
1969 * need to flush on the last iteration */
1970 local_flags
&= ~BDRV_REQ_FUA
;
1973 ret
= bdrv_driver_pwritev(bs
, offset
+ bytes
- bytes_remaining
,
1975 qiov_offset
+ bytes
- bytes_remaining
,
1980 bytes_remaining
-= num
;
1983 bdrv_co_debug_event(bs
, BLKDBG_PWRITEV_DONE
);
1988 bdrv_co_write_req_finish(child
, offset
, bytes
, req
, ret
);
1993 static int coroutine_fn GRAPH_RDLOCK
1994 bdrv_co_do_zero_pwritev(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
1995 BdrvRequestFlags flags
, BdrvTrackedRequest
*req
)
1997 BlockDriverState
*bs
= child
->bs
;
1998 QEMUIOVector local_qiov
;
1999 uint64_t align
= bs
->bl
.request_alignment
;
2002 BdrvRequestPadding pad
;
2004 /* This flag doesn't make sense for padding or zero writes */
2005 flags
&= ~BDRV_REQ_REGISTERED_BUF
;
2007 padding
= bdrv_init_padding(bs
, offset
, bytes
, &pad
);
2009 assert(!(flags
& BDRV_REQ_NO_WAIT
));
2010 bdrv_make_request_serialising(req
, align
);
2012 bdrv_padding_rmw_read(child
, req
, &pad
, true);
2014 if (pad
.head
|| pad
.merge_reads
) {
2015 int64_t aligned_offset
= offset
& ~(align
- 1);
2016 int64_t write_bytes
= pad
.merge_reads
? pad
.buf_len
: align
;
2018 qemu_iovec_init_buf(&local_qiov
, pad
.buf
, write_bytes
);
2019 ret
= bdrv_aligned_pwritev(child
, req
, aligned_offset
, write_bytes
,
2020 align
, &local_qiov
, 0,
2021 flags
& ~BDRV_REQ_ZERO_WRITE
);
2022 if (ret
< 0 || pad
.merge_reads
) {
2023 /* Error or all work is done */
2026 offset
+= write_bytes
- pad
.head
;
2027 bytes
-= write_bytes
- pad
.head
;
2031 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2032 if (bytes
>= align
) {
2033 /* Write the aligned part in the middle. */
2034 int64_t aligned_bytes
= bytes
& ~(align
- 1);
2035 ret
= bdrv_aligned_pwritev(child
, req
, offset
, aligned_bytes
, align
,
2040 bytes
-= aligned_bytes
;
2041 offset
+= aligned_bytes
;
2044 assert(!bytes
|| (offset
& (align
- 1)) == 0);
2046 assert(align
== pad
.tail
+ bytes
);
2048 qemu_iovec_init_buf(&local_qiov
, pad
.tail_buf
, align
);
2049 ret
= bdrv_aligned_pwritev(child
, req
, offset
, align
, align
,
2051 flags
& ~BDRV_REQ_ZERO_WRITE
);
2055 bdrv_padding_destroy(&pad
);
2061 * Handle a write request in coroutine context
2063 int coroutine_fn
bdrv_co_pwritev(BdrvChild
*child
,
2064 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
2065 BdrvRequestFlags flags
)
2068 return bdrv_co_pwritev_part(child
, offset
, bytes
, qiov
, 0, flags
);
2071 int coroutine_fn
bdrv_co_pwritev_part(BdrvChild
*child
,
2072 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
, size_t qiov_offset
,
2073 BdrvRequestFlags flags
)
2075 BlockDriverState
*bs
= child
->bs
;
2076 BdrvTrackedRequest req
;
2077 uint64_t align
= bs
->bl
.request_alignment
;
2078 BdrvRequestPadding pad
;
2080 bool padded
= false;
2083 trace_bdrv_co_pwritev_part(child
->bs
, offset
, bytes
, flags
);
2085 if (!bdrv_co_is_inserted(bs
)) {
2089 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2090 ret
= bdrv_check_qiov_request(offset
, bytes
, qiov
, qiov_offset
, NULL
);
2092 ret
= bdrv_check_request32(offset
, bytes
, qiov
, qiov_offset
);
2098 /* If the request is misaligned then we can't make it efficient */
2099 if ((flags
& BDRV_REQ_NO_FALLBACK
) &&
2100 !QEMU_IS_ALIGNED(offset
| bytes
, align
))
2105 if (bytes
== 0 && !QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
)) {
2107 * Aligning zero request is nonsense. Even if driver has special meaning
2108 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
2109 * it to driver due to request_alignment.
2111 * Still, no reason to return an error if someone do unaligned
2112 * zero-length write occasionally.
2117 if (!(flags
& BDRV_REQ_ZERO_WRITE
)) {
2119 * Pad request for following read-modify-write cycle.
2120 * bdrv_co_do_zero_pwritev() does aligning by itself, so, we do
2121 * alignment only if there is no ZERO flag.
2123 ret
= bdrv_pad_request(bs
, &qiov
, &qiov_offset
, &offset
, &bytes
, &pad
,
2130 bdrv_inc_in_flight(bs
);
2131 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_WRITE
);
2133 if (flags
& BDRV_REQ_ZERO_WRITE
) {
2135 ret
= bdrv_co_do_zero_pwritev(child
, offset
, bytes
, flags
, &req
);
2141 * Request was unaligned to request_alignment and therefore
2142 * padded. We are going to do read-modify-write, and must
2143 * serialize the request to prevent interactions of the
2144 * widened region with other transactions.
2146 assert(!(flags
& BDRV_REQ_NO_WAIT
));
2147 bdrv_make_request_serialising(&req
, align
);
2148 bdrv_padding_rmw_read(child
, &req
, &pad
, false);
2151 ret
= bdrv_aligned_pwritev(child
, &req
, offset
, bytes
, align
,
2152 qiov
, qiov_offset
, flags
);
2154 bdrv_padding_destroy(&pad
);
2157 tracked_request_end(&req
);
2158 bdrv_dec_in_flight(bs
);
2163 int coroutine_fn
bdrv_co_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
2164 int64_t bytes
, BdrvRequestFlags flags
)
2167 trace_bdrv_co_pwrite_zeroes(child
->bs
, offset
, bytes
, flags
);
2168 assert_bdrv_graph_readable();
2170 if (!(child
->bs
->open_flags
& BDRV_O_UNMAP
)) {
2171 flags
&= ~BDRV_REQ_MAY_UNMAP
;
2174 return bdrv_co_pwritev(child
, offset
, bytes
, NULL
,
2175 BDRV_REQ_ZERO_WRITE
| flags
);
2179 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
2181 int bdrv_flush_all(void)
2183 BdrvNextIterator it
;
2184 BlockDriverState
*bs
= NULL
;
2187 GLOBAL_STATE_CODE();
2190 * bdrv queue is managed by record/replay,
2191 * creating new flush request for stopping
2192 * the VM may break the determinism
2194 if (replay_events_enabled()) {
2198 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2199 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
2202 aio_context_acquire(aio_context
);
2203 ret
= bdrv_flush(bs
);
2204 if (ret
< 0 && !result
) {
2207 aio_context_release(aio_context
);
2214 * Returns the allocation status of the specified sectors.
2215 * Drivers not implementing the functionality are assumed to not support
2216 * backing files, hence all their sectors are reported as allocated.
2218 * If 'want_zero' is true, the caller is querying for mapping
2219 * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and
2220 * _ZERO where possible; otherwise, the result favors larger 'pnum',
2221 * with a focus on accurate BDRV_BLOCK_ALLOCATED.
2223 * If 'offset' is beyond the end of the disk image the return value is
2224 * BDRV_BLOCK_EOF and 'pnum' is set to 0.
2226 * 'bytes' is the max value 'pnum' should be set to. If bytes goes
2227 * beyond the end of the disk image it will be clamped; if 'pnum' is set to
2228 * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
2230 * 'pnum' is set to the number of bytes (including and immediately
2231 * following the specified offset) that are easily known to be in the
2232 * same allocated/unallocated state. Note that a second call starting
2233 * at the original offset plus returned pnum may have the same status.
2234 * The returned value is non-zero on success except at end-of-file.
2236 * Returns negative errno on failure. Otherwise, if the
2237 * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are
2238 * set to the host mapping and BDS corresponding to the guest offset.
2240 static int coroutine_fn GRAPH_RDLOCK
2241 bdrv_co_block_status(BlockDriverState
*bs
, bool want_zero
,
2242 int64_t offset
, int64_t bytes
,
2243 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
2246 int64_t n
; /* bytes */
2248 int64_t local_map
= 0;
2249 BlockDriverState
*local_file
= NULL
;
2250 int64_t aligned_offset
, aligned_bytes
;
2252 bool has_filtered_child
;
2255 assert_bdrv_graph_readable();
2257 total_size
= bdrv_getlength(bs
);
2258 if (total_size
< 0) {
2263 if (offset
>= total_size
) {
2264 ret
= BDRV_BLOCK_EOF
;
2272 n
= total_size
- offset
;
2277 /* Must be non-NULL or bdrv_getlength() would have failed */
2279 has_filtered_child
= bdrv_filter_child(bs
);
2280 if (!bs
->drv
->bdrv_co_block_status
&& !has_filtered_child
) {
2282 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
;
2283 if (offset
+ bytes
== total_size
) {
2284 ret
|= BDRV_BLOCK_EOF
;
2286 if (bs
->drv
->protocol_name
) {
2287 ret
|= BDRV_BLOCK_OFFSET_VALID
;
2294 bdrv_inc_in_flight(bs
);
2296 /* Round out to request_alignment boundaries */
2297 align
= bs
->bl
.request_alignment
;
2298 aligned_offset
= QEMU_ALIGN_DOWN(offset
, align
);
2299 aligned_bytes
= ROUND_UP(offset
+ bytes
, align
) - aligned_offset
;
2301 if (bs
->drv
->bdrv_co_block_status
) {
2303 * Use the block-status cache only for protocol nodes: Format
2304 * drivers are generally quick to inquire the status, but protocol
2305 * drivers often need to get information from outside of qemu, so
2306 * we do not have control over the actual implementation. There
2307 * have been cases where inquiring the status took an unreasonably
2308 * long time, and we can do nothing in qemu to fix it.
2309 * This is especially problematic for images with large data areas,
2310 * because finding the few holes in them and giving them special
2311 * treatment does not gain much performance. Therefore, we try to
2312 * cache the last-identified data region.
2314 * Second, limiting ourselves to protocol nodes allows us to assume
2315 * the block status for data regions to be DATA | OFFSET_VALID, and
2316 * that the host offset is the same as the guest offset.
2318 * Note that it is possible that external writers zero parts of
2319 * the cached regions without the cache being invalidated, and so
2320 * we may report zeroes as data. This is not catastrophic,
2321 * however, because reporting zeroes as data is fine.
2323 if (QLIST_EMPTY(&bs
->children
) &&
2324 bdrv_bsc_is_data(bs
, aligned_offset
, pnum
))
2326 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
2328 local_map
= aligned_offset
;
2330 ret
= bs
->drv
->bdrv_co_block_status(bs
, want_zero
, aligned_offset
,
2331 aligned_bytes
, pnum
, &local_map
,
2335 * Note that checking QLIST_EMPTY(&bs->children) is also done when
2336 * the cache is queried above. Technically, we do not need to check
2337 * it here; the worst that can happen is that we fill the cache for
2338 * non-protocol nodes, and then it is never used. However, filling
2339 * the cache requires an RCU update, so double check here to avoid
2340 * such an update if possible.
2342 * Check want_zero, because we only want to update the cache when we
2343 * have accurate information about what is zero and what is data.
2346 ret
== (BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
) &&
2347 QLIST_EMPTY(&bs
->children
))
2350 * When a protocol driver reports BLOCK_OFFSET_VALID, the
2351 * returned local_map value must be the same as the offset we
2352 * have passed (aligned_offset), and local_bs must be the node
2354 * Assert this, because we follow this rule when reading from
2355 * the cache (see the `local_file = bs` and
2356 * `local_map = aligned_offset` assignments above), and the
2357 * result the cache delivers must be the same as the driver
2360 assert(local_file
== bs
);
2361 assert(local_map
== aligned_offset
);
2362 bdrv_bsc_fill(bs
, aligned_offset
, *pnum
);
2366 /* Default code for filters */
2368 local_file
= bdrv_filter_bs(bs
);
2371 *pnum
= aligned_bytes
;
2372 local_map
= aligned_offset
;
2373 ret
= BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
;
2381 * The driver's result must be a non-zero multiple of request_alignment.
2382 * Clamp pnum and adjust map to original request.
2384 assert(*pnum
&& QEMU_IS_ALIGNED(*pnum
, align
) &&
2385 align
> offset
- aligned_offset
);
2386 if (ret
& BDRV_BLOCK_RECURSE
) {
2387 assert(ret
& BDRV_BLOCK_DATA
);
2388 assert(ret
& BDRV_BLOCK_OFFSET_VALID
);
2389 assert(!(ret
& BDRV_BLOCK_ZERO
));
2392 *pnum
-= offset
- aligned_offset
;
2393 if (*pnum
> bytes
) {
2396 if (ret
& BDRV_BLOCK_OFFSET_VALID
) {
2397 local_map
+= offset
- aligned_offset
;
2400 if (ret
& BDRV_BLOCK_RAW
) {
2401 assert(ret
& BDRV_BLOCK_OFFSET_VALID
&& local_file
);
2402 ret
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2403 *pnum
, pnum
, &local_map
, &local_file
);
2407 if (ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ZERO
)) {
2408 ret
|= BDRV_BLOCK_ALLOCATED
;
2409 } else if (bs
->drv
->supports_backing
) {
2410 BlockDriverState
*cow_bs
= bdrv_cow_bs(bs
);
2413 ret
|= BDRV_BLOCK_ZERO
;
2414 } else if (want_zero
) {
2415 int64_t size2
= bdrv_getlength(cow_bs
);
2417 if (size2
>= 0 && offset
>= size2
) {
2418 ret
|= BDRV_BLOCK_ZERO
;
2423 if (want_zero
&& ret
& BDRV_BLOCK_RECURSE
&&
2424 local_file
&& local_file
!= bs
&&
2425 (ret
& BDRV_BLOCK_DATA
) && !(ret
& BDRV_BLOCK_ZERO
) &&
2426 (ret
& BDRV_BLOCK_OFFSET_VALID
)) {
2430 ret2
= bdrv_co_block_status(local_file
, want_zero
, local_map
,
2431 *pnum
, &file_pnum
, NULL
, NULL
);
2433 /* Ignore errors. This is just providing extra information, it
2434 * is useful but not necessary.
2436 if (ret2
& BDRV_BLOCK_EOF
&&
2437 (!file_pnum
|| ret2
& BDRV_BLOCK_ZERO
)) {
2439 * It is valid for the format block driver to read
2440 * beyond the end of the underlying file's current
2441 * size; such areas read as zero.
2443 ret
|= BDRV_BLOCK_ZERO
;
2445 /* Limit request to the range reported by the protocol driver */
2447 ret
|= (ret2
& BDRV_BLOCK_ZERO
);
2453 bdrv_dec_in_flight(bs
);
2454 if (ret
>= 0 && offset
+ *pnum
== total_size
) {
2455 ret
|= BDRV_BLOCK_EOF
;
2468 bdrv_co_common_block_status_above(BlockDriverState
*bs
,
2469 BlockDriverState
*base
,
2476 BlockDriverState
**file
,
2480 BlockDriverState
*p
;
2485 assert(!include_base
|| base
); /* Can't include NULL base */
2486 assert_bdrv_graph_readable();
2493 if (!include_base
&& bs
== base
) {
2498 ret
= bdrv_co_block_status(bs
, want_zero
, offset
, bytes
, pnum
, map
, file
);
2500 if (ret
< 0 || *pnum
== 0 || ret
& BDRV_BLOCK_ALLOCATED
|| bs
== base
) {
2504 if (ret
& BDRV_BLOCK_EOF
) {
2505 eof
= offset
+ *pnum
;
2508 assert(*pnum
<= bytes
);
2511 for (p
= bdrv_filter_or_cow_bs(bs
); include_base
|| p
!= base
;
2512 p
= bdrv_filter_or_cow_bs(p
))
2514 ret
= bdrv_co_block_status(p
, want_zero
, offset
, bytes
, pnum
, map
,
2522 * The top layer deferred to this layer, and because this layer is
2523 * short, any zeroes that we synthesize beyond EOF behave as if they
2524 * were allocated at this layer.
2526 * We don't include BDRV_BLOCK_EOF into ret, as upper layer may be
2527 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2530 assert(ret
& BDRV_BLOCK_EOF
);
2535 ret
= BDRV_BLOCK_ZERO
| BDRV_BLOCK_ALLOCATED
;
2538 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2540 * We've found the node and the status, we must break.
2542 * Drop BDRV_BLOCK_EOF, as it's not for upper layer, which may be
2543 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2546 ret
&= ~BDRV_BLOCK_EOF
;
2551 assert(include_base
);
2556 * OK, [offset, offset + *pnum) region is unallocated on this layer,
2557 * let's continue the diving.
2559 assert(*pnum
<= bytes
);
2563 if (offset
+ *pnum
== eof
) {
2564 ret
|= BDRV_BLOCK_EOF
;
2570 int coroutine_fn
bdrv_co_block_status_above(BlockDriverState
*bs
,
2571 BlockDriverState
*base
,
2572 int64_t offset
, int64_t bytes
,
2573 int64_t *pnum
, int64_t *map
,
2574 BlockDriverState
**file
)
2577 return bdrv_co_common_block_status_above(bs
, base
, false, true, offset
,
2578 bytes
, pnum
, map
, file
, NULL
);
2581 int bdrv_block_status_above(BlockDriverState
*bs
, BlockDriverState
*base
,
2582 int64_t offset
, int64_t bytes
, int64_t *pnum
,
2583 int64_t *map
, BlockDriverState
**file
)
2586 return bdrv_common_block_status_above(bs
, base
, false, true, offset
, bytes
,
2587 pnum
, map
, file
, NULL
);
2590 int bdrv_block_status(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2591 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
2594 return bdrv_block_status_above(bs
, bdrv_filter_or_cow_bs(bs
),
2595 offset
, bytes
, pnum
, map
, file
);
2599 * Check @bs (and its backing chain) to see if the range defined
2600 * by @offset and @bytes is known to read as zeroes.
2601 * Return 1 if that is the case, 0 otherwise and -errno on error.
2602 * This test is meant to be fast rather than accurate so returning 0
2603 * does not guarantee non-zero data.
2605 int coroutine_fn
bdrv_co_is_zero_fast(BlockDriverState
*bs
, int64_t offset
,
2609 int64_t pnum
= bytes
;
2616 ret
= bdrv_co_common_block_status_above(bs
, NULL
, false, false, offset
,
2617 bytes
, &pnum
, NULL
, NULL
, NULL
);
2623 return (pnum
== bytes
) && (ret
& BDRV_BLOCK_ZERO
);
2626 int coroutine_fn
bdrv_co_is_allocated(BlockDriverState
*bs
, int64_t offset
,
2627 int64_t bytes
, int64_t *pnum
)
2633 ret
= bdrv_co_common_block_status_above(bs
, bs
, true, false, offset
,
2634 bytes
, pnum
? pnum
: &dummy
, NULL
,
2639 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
2642 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2649 ret
= bdrv_common_block_status_above(bs
, bs
, true, false, offset
,
2650 bytes
, pnum
? pnum
: &dummy
, NULL
,
2655 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
2658 /* See bdrv_is_allocated_above for documentation */
2659 int coroutine_fn
bdrv_co_is_allocated_above(BlockDriverState
*top
,
2660 BlockDriverState
*base
,
2661 bool include_base
, int64_t offset
,
2662 int64_t bytes
, int64_t *pnum
)
2668 ret
= bdrv_co_common_block_status_above(top
, base
, include_base
, false,
2669 offset
, bytes
, pnum
, NULL
, NULL
,
2675 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2682 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2684 * Return a positive depth if (a prefix of) the given range is allocated
2685 * in any image between BASE and TOP (BASE is only included if include_base
2686 * is set). Depth 1 is TOP, 2 is the first backing layer, and so forth.
2687 * BASE can be NULL to check if the given offset is allocated in any
2688 * image of the chain. Return 0 otherwise, or negative errno on
2691 * 'pnum' is set to the number of bytes (including and immediately
2692 * following the specified offset) that are known to be in the same
2693 * allocated/unallocated state. Note that a subsequent call starting
2694 * at 'offset + *pnum' may return the same allocation status (in other
2695 * words, the result is not necessarily the maximum possible range);
2696 * but 'pnum' will only be 0 when end of file is reached.
2698 int bdrv_is_allocated_above(BlockDriverState
*top
,
2699 BlockDriverState
*base
,
2700 bool include_base
, int64_t offset
,
2701 int64_t bytes
, int64_t *pnum
)
2707 ret
= bdrv_common_block_status_above(top
, base
, include_base
, false,
2708 offset
, bytes
, pnum
, NULL
, NULL
,
2714 if (ret
& BDRV_BLOCK_ALLOCATED
) {
2721 bdrv_co_readv_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2723 BlockDriver
*drv
= bs
->drv
;
2724 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2727 assert_bdrv_graph_readable();
2729 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
2738 bdrv_inc_in_flight(bs
);
2740 if (drv
->bdrv_co_load_vmstate
) {
2741 ret
= drv
->bdrv_co_load_vmstate(bs
, qiov
, pos
);
2742 } else if (child_bs
) {
2743 ret
= bdrv_co_readv_vmstate(child_bs
, qiov
, pos
);
2748 bdrv_dec_in_flight(bs
);
2754 bdrv_co_writev_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2756 BlockDriver
*drv
= bs
->drv
;
2757 BlockDriverState
*child_bs
= bdrv_primary_bs(bs
);
2760 assert_bdrv_graph_readable();
2762 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
2771 bdrv_inc_in_flight(bs
);
2773 if (drv
->bdrv_co_save_vmstate
) {
2774 ret
= drv
->bdrv_co_save_vmstate(bs
, qiov
, pos
);
2775 } else if (child_bs
) {
2776 ret
= bdrv_co_writev_vmstate(child_bs
, qiov
, pos
);
2781 bdrv_dec_in_flight(bs
);
2786 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2787 int64_t pos
, int size
)
2789 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2790 int ret
= bdrv_writev_vmstate(bs
, &qiov
, pos
);
2793 return ret
< 0 ? ret
: size
;
2796 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2797 int64_t pos
, int size
)
2799 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, size
);
2800 int ret
= bdrv_readv_vmstate(bs
, &qiov
, pos
);
2803 return ret
< 0 ? ret
: size
;
2806 /**************************************************************/
2809 void bdrv_aio_cancel(BlockAIOCB
*acb
)
2813 bdrv_aio_cancel_async(acb
);
2814 while (acb
->refcnt
> 1) {
2815 if (acb
->aiocb_info
->get_aio_context
) {
2816 aio_poll(acb
->aiocb_info
->get_aio_context(acb
), true);
2817 } else if (acb
->bs
) {
2818 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2819 * assert that we're not using an I/O thread. Thread-safe
2820 * code should use bdrv_aio_cancel_async exclusively.
2822 assert(bdrv_get_aio_context(acb
->bs
) == qemu_get_aio_context());
2823 aio_poll(bdrv_get_aio_context(acb
->bs
), true);
2828 qemu_aio_unref(acb
);
2831 /* Async version of aio cancel. The caller is not blocked if the acb implements
2832 * cancel_async, otherwise we do nothing and let the request normally complete.
2833 * In either case the completion callback must be called. */
2834 void bdrv_aio_cancel_async(BlockAIOCB
*acb
)
2837 if (acb
->aiocb_info
->cancel_async
) {
2838 acb
->aiocb_info
->cancel_async(acb
);
2842 /**************************************************************/
2843 /* Coroutine block device emulation */
2845 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2847 BdrvChild
*primary_child
= bdrv_primary_child(bs
);
2853 assert_bdrv_graph_readable();
2854 bdrv_inc_in_flight(bs
);
2856 if (!bdrv_co_is_inserted(bs
) || bdrv_is_read_only(bs
) ||
2861 qemu_co_mutex_lock(&bs
->reqs_lock
);
2862 current_gen
= qatomic_read(&bs
->write_gen
);
2864 /* Wait until any previous flushes are completed */
2865 while (bs
->active_flush_req
) {
2866 qemu_co_queue_wait(&bs
->flush_queue
, &bs
->reqs_lock
);
2869 /* Flushes reach this point in nondecreasing current_gen order. */
2870 bs
->active_flush_req
= true;
2871 qemu_co_mutex_unlock(&bs
->reqs_lock
);
2873 /* Write back all layers by calling one driver function */
2874 if (bs
->drv
->bdrv_co_flush
) {
2875 ret
= bs
->drv
->bdrv_co_flush(bs
);
2879 /* Write back cached data to the OS even with cache=unsafe */
2880 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_OS
);
2881 if (bs
->drv
->bdrv_co_flush_to_os
) {
2882 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
2888 /* But don't actually force it to the disk with cache=unsafe */
2889 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2890 goto flush_children
;
2893 /* Check if we really need to flush anything */
2894 if (bs
->flushed_gen
== current_gen
) {
2895 goto flush_children
;
2898 BLKDBG_EVENT(primary_child
, BLKDBG_FLUSH_TO_DISK
);
2900 /* bs->drv->bdrv_co_flush() might have ejected the BDS
2901 * (even in case of apparent success) */
2905 if (bs
->drv
->bdrv_co_flush_to_disk
) {
2906 ret
= bs
->drv
->bdrv_co_flush_to_disk(bs
);
2907 } else if (bs
->drv
->bdrv_aio_flush
) {
2909 CoroutineIOCompletion co
= {
2910 .coroutine
= qemu_coroutine_self(),
2913 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
2917 qemu_coroutine_yield();
2922 * Some block drivers always operate in either writethrough or unsafe
2923 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2924 * know how the server works (because the behaviour is hardcoded or
2925 * depends on server-side configuration), so we can't ensure that
2926 * everything is safe on disk. Returning an error doesn't work because
2927 * that would break guests even if the server operates in writethrough
2930 * Let's hope the user knows what he's doing.
2939 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2940 * in the case of cache=unsafe, so there are no useless flushes.
2944 QLIST_FOREACH(child
, &bs
->children
, next
) {
2945 if (child
->perm
& (BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
)) {
2946 int this_child_ret
= bdrv_co_flush(child
->bs
);
2948 ret
= this_child_ret
;
2954 /* Notify any pending flushes that we have completed */
2956 bs
->flushed_gen
= current_gen
;
2959 qemu_co_mutex_lock(&bs
->reqs_lock
);
2960 bs
->active_flush_req
= false;
2961 /* Return value is ignored - it's ok if wait queue is empty */
2962 qemu_co_queue_next(&bs
->flush_queue
);
2963 qemu_co_mutex_unlock(&bs
->reqs_lock
);
2966 bdrv_dec_in_flight(bs
);
2970 int coroutine_fn
bdrv_co_pdiscard(BdrvChild
*child
, int64_t offset
,
2973 BdrvTrackedRequest req
;
2975 int64_t max_pdiscard
;
2976 int head
, tail
, align
;
2977 BlockDriverState
*bs
= child
->bs
;
2979 assert_bdrv_graph_readable();
2981 if (!bs
|| !bs
->drv
|| !bdrv_co_is_inserted(bs
)) {
2985 if (bdrv_has_readonly_bitmaps(bs
)) {
2989 ret
= bdrv_check_request(offset
, bytes
, NULL
);
2994 /* Do nothing if disabled. */
2995 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
2999 if (!bs
->drv
->bdrv_co_pdiscard
&& !bs
->drv
->bdrv_aio_pdiscard
) {
3003 /* Invalidate the cached block-status data range if this discard overlaps */
3004 bdrv_bsc_invalidate_range(bs
, offset
, bytes
);
3006 /* Discard is advisory, but some devices track and coalesce
3007 * unaligned requests, so we must pass everything down rather than
3008 * round here. Still, most devices will just silently ignore
3009 * unaligned requests (by returning -ENOTSUP), so we must fragment
3010 * the request accordingly. */
3011 align
= MAX(bs
->bl
.pdiscard_alignment
, bs
->bl
.request_alignment
);
3012 assert(align
% bs
->bl
.request_alignment
== 0);
3013 head
= offset
% align
;
3014 tail
= (offset
+ bytes
) % align
;
3016 bdrv_inc_in_flight(bs
);
3017 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_DISCARD
);
3019 ret
= bdrv_co_write_req_prepare(child
, offset
, bytes
, &req
, 0);
3024 max_pdiscard
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_pdiscard
, INT64_MAX
),
3026 assert(max_pdiscard
>= bs
->bl
.request_alignment
);
3029 int64_t num
= bytes
;
3032 /* Make small requests to get to alignment boundaries. */
3033 num
= MIN(bytes
, align
- head
);
3034 if (!QEMU_IS_ALIGNED(num
, bs
->bl
.request_alignment
)) {
3035 num
%= bs
->bl
.request_alignment
;
3037 head
= (head
+ num
) % align
;
3038 assert(num
< max_pdiscard
);
3041 /* Shorten the request to the last aligned cluster. */
3043 } else if (!QEMU_IS_ALIGNED(tail
, bs
->bl
.request_alignment
) &&
3044 tail
> bs
->bl
.request_alignment
) {
3045 tail
%= bs
->bl
.request_alignment
;
3049 /* limit request size */
3050 if (num
> max_pdiscard
) {
3058 if (bs
->drv
->bdrv_co_pdiscard
) {
3059 ret
= bs
->drv
->bdrv_co_pdiscard(bs
, offset
, num
);
3062 CoroutineIOCompletion co
= {
3063 .coroutine
= qemu_coroutine_self(),
3066 acb
= bs
->drv
->bdrv_aio_pdiscard(bs
, offset
, num
,
3067 bdrv_co_io_em_complete
, &co
);
3072 qemu_coroutine_yield();
3076 if (ret
&& ret
!= -ENOTSUP
) {
3085 bdrv_co_write_req_finish(child
, req
.offset
, req
.bytes
, &req
, ret
);
3086 tracked_request_end(&req
);
3087 bdrv_dec_in_flight(bs
);
3091 int coroutine_fn
bdrv_co_ioctl(BlockDriverState
*bs
, int req
, void *buf
)
3093 BlockDriver
*drv
= bs
->drv
;
3094 CoroutineIOCompletion co
= {
3095 .coroutine
= qemu_coroutine_self(),
3099 assert_bdrv_graph_readable();
3101 bdrv_inc_in_flight(bs
);
3102 if (!drv
|| (!drv
->bdrv_aio_ioctl
&& !drv
->bdrv_co_ioctl
)) {
3107 if (drv
->bdrv_co_ioctl
) {
3108 co
.ret
= drv
->bdrv_co_ioctl(bs
, req
, buf
);
3110 acb
= drv
->bdrv_aio_ioctl(bs
, req
, buf
, bdrv_co_io_em_complete
, &co
);
3115 qemu_coroutine_yield();
3118 bdrv_dec_in_flight(bs
);
3122 int coroutine_fn
bdrv_co_zone_report(BlockDriverState
*bs
, int64_t offset
,
3123 unsigned int *nr_zones
,
3124 BlockZoneDescriptor
*zones
)
3126 BlockDriver
*drv
= bs
->drv
;
3127 CoroutineIOCompletion co
= {
3128 .coroutine
= qemu_coroutine_self(),
3132 bdrv_inc_in_flight(bs
);
3133 if (!drv
|| !drv
->bdrv_co_zone_report
|| bs
->bl
.zoned
== BLK_Z_NONE
) {
3137 co
.ret
= drv
->bdrv_co_zone_report(bs
, offset
, nr_zones
, zones
);
3139 bdrv_dec_in_flight(bs
);
3143 int coroutine_fn
bdrv_co_zone_mgmt(BlockDriverState
*bs
, BlockZoneOp op
,
3144 int64_t offset
, int64_t len
)
3146 BlockDriver
*drv
= bs
->drv
;
3147 CoroutineIOCompletion co
= {
3148 .coroutine
= qemu_coroutine_self(),
3152 bdrv_inc_in_flight(bs
);
3153 if (!drv
|| !drv
->bdrv_co_zone_mgmt
|| bs
->bl
.zoned
== BLK_Z_NONE
) {
3157 co
.ret
= drv
->bdrv_co_zone_mgmt(bs
, op
, offset
, len
);
3159 bdrv_dec_in_flight(bs
);
3163 int coroutine_fn
bdrv_co_zone_append(BlockDriverState
*bs
, int64_t *offset
,
3165 BdrvRequestFlags flags
)
3168 BlockDriver
*drv
= bs
->drv
;
3169 CoroutineIOCompletion co
= {
3170 .coroutine
= qemu_coroutine_self(),
3174 ret
= bdrv_check_qiov_request(*offset
, qiov
->size
, qiov
, 0, NULL
);
3179 bdrv_inc_in_flight(bs
);
3180 if (!drv
|| !drv
->bdrv_co_zone_append
|| bs
->bl
.zoned
== BLK_Z_NONE
) {
3184 co
.ret
= drv
->bdrv_co_zone_append(bs
, offset
, qiov
, flags
);
3186 bdrv_dec_in_flight(bs
);
3190 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
3193 return qemu_memalign(bdrv_opt_mem_align(bs
), size
);
3196 void *qemu_blockalign0(BlockDriverState
*bs
, size_t size
)
3199 return memset(qemu_blockalign(bs
, size
), 0, size
);
3202 void *qemu_try_blockalign(BlockDriverState
*bs
, size_t size
)
3204 size_t align
= bdrv_opt_mem_align(bs
);
3207 /* Ensure that NULL is never returned on success */
3213 return qemu_try_memalign(align
, size
);
3216 void *qemu_try_blockalign0(BlockDriverState
*bs
, size_t size
)
3218 void *mem
= qemu_try_blockalign(bs
, size
);
3222 memset(mem
, 0, size
);
3228 void coroutine_fn
bdrv_co_io_plug(BlockDriverState
*bs
)
3232 assert_bdrv_graph_readable();
3234 QLIST_FOREACH(child
, &bs
->children
, next
) {
3235 bdrv_co_io_plug(child
->bs
);
3238 if (qatomic_fetch_inc(&bs
->io_plugged
) == 0) {
3239 BlockDriver
*drv
= bs
->drv
;
3240 if (drv
&& drv
->bdrv_co_io_plug
) {
3241 drv
->bdrv_co_io_plug(bs
);
3246 void coroutine_fn
bdrv_co_io_unplug(BlockDriverState
*bs
)
3250 assert_bdrv_graph_readable();
3252 assert(bs
->io_plugged
);
3253 if (qatomic_fetch_dec(&bs
->io_plugged
) == 1) {
3254 BlockDriver
*drv
= bs
->drv
;
3255 if (drv
&& drv
->bdrv_co_io_unplug
) {
3256 drv
->bdrv_co_io_unplug(bs
);
3260 QLIST_FOREACH(child
, &bs
->children
, next
) {
3261 bdrv_co_io_unplug(child
->bs
);
3265 /* Helper that undoes bdrv_register_buf() when it fails partway through */
3266 static void GRAPH_RDLOCK
3267 bdrv_register_buf_rollback(BlockDriverState
*bs
, void *host
, size_t size
,
3268 BdrvChild
*final_child
)
3272 GLOBAL_STATE_CODE();
3273 assert_bdrv_graph_readable();
3275 QLIST_FOREACH(child
, &bs
->children
, next
) {
3276 if (child
== final_child
) {
3280 bdrv_unregister_buf(child
->bs
, host
, size
);
3283 if (bs
->drv
&& bs
->drv
->bdrv_unregister_buf
) {
3284 bs
->drv
->bdrv_unregister_buf(bs
, host
, size
);
3288 bool bdrv_register_buf(BlockDriverState
*bs
, void *host
, size_t size
,
3293 GLOBAL_STATE_CODE();
3294 GRAPH_RDLOCK_GUARD_MAINLOOP();
3296 if (bs
->drv
&& bs
->drv
->bdrv_register_buf
) {
3297 if (!bs
->drv
->bdrv_register_buf(bs
, host
, size
, errp
)) {
3301 QLIST_FOREACH(child
, &bs
->children
, next
) {
3302 if (!bdrv_register_buf(child
->bs
, host
, size
, errp
)) {
3303 bdrv_register_buf_rollback(bs
, host
, size
, child
);
3310 void bdrv_unregister_buf(BlockDriverState
*bs
, void *host
, size_t size
)
3314 GLOBAL_STATE_CODE();
3315 GRAPH_RDLOCK_GUARD_MAINLOOP();
3317 if (bs
->drv
&& bs
->drv
->bdrv_unregister_buf
) {
3318 bs
->drv
->bdrv_unregister_buf(bs
, host
, size
);
3320 QLIST_FOREACH(child
, &bs
->children
, next
) {
3321 bdrv_unregister_buf(child
->bs
, host
, size
);
3325 static int coroutine_fn GRAPH_RDLOCK
bdrv_co_copy_range_internal(
3326 BdrvChild
*src
, int64_t src_offset
, BdrvChild
*dst
,
3327 int64_t dst_offset
, int64_t bytes
,
3328 BdrvRequestFlags read_flags
, BdrvRequestFlags write_flags
,
3331 BdrvTrackedRequest req
;
3333 assert_bdrv_graph_readable();
3335 /* TODO We can support BDRV_REQ_NO_FALLBACK here */
3336 assert(!(read_flags
& BDRV_REQ_NO_FALLBACK
));
3337 assert(!(write_flags
& BDRV_REQ_NO_FALLBACK
));
3338 assert(!(read_flags
& BDRV_REQ_NO_WAIT
));
3339 assert(!(write_flags
& BDRV_REQ_NO_WAIT
));
3341 if (!dst
|| !dst
->bs
|| !bdrv_co_is_inserted(dst
->bs
)) {
3344 ret
= bdrv_check_request32(dst_offset
, bytes
, NULL
, 0);
3348 if (write_flags
& BDRV_REQ_ZERO_WRITE
) {
3349 return bdrv_co_pwrite_zeroes(dst
, dst_offset
, bytes
, write_flags
);
3352 if (!src
|| !src
->bs
|| !bdrv_co_is_inserted(src
->bs
)) {
3355 ret
= bdrv_check_request32(src_offset
, bytes
, NULL
, 0);
3360 if (!src
->bs
->drv
->bdrv_co_copy_range_from
3361 || !dst
->bs
->drv
->bdrv_co_copy_range_to
3362 || src
->bs
->encrypted
|| dst
->bs
->encrypted
) {
3367 bdrv_inc_in_flight(src
->bs
);
3368 tracked_request_begin(&req
, src
->bs
, src_offset
, bytes
,
3371 /* BDRV_REQ_SERIALISING is only for write operation */
3372 assert(!(read_flags
& BDRV_REQ_SERIALISING
));
3373 bdrv_wait_serialising_requests(&req
);
3375 ret
= src
->bs
->drv
->bdrv_co_copy_range_from(src
->bs
,
3379 read_flags
, write_flags
);
3381 tracked_request_end(&req
);
3382 bdrv_dec_in_flight(src
->bs
);
3384 bdrv_inc_in_flight(dst
->bs
);
3385 tracked_request_begin(&req
, dst
->bs
, dst_offset
, bytes
,
3386 BDRV_TRACKED_WRITE
);
3387 ret
= bdrv_co_write_req_prepare(dst
, dst_offset
, bytes
, &req
,
3390 ret
= dst
->bs
->drv
->bdrv_co_copy_range_to(dst
->bs
,
3394 read_flags
, write_flags
);
3396 bdrv_co_write_req_finish(dst
, dst_offset
, bytes
, &req
, ret
);
3397 tracked_request_end(&req
);
3398 bdrv_dec_in_flight(dst
->bs
);
3404 /* Copy range from @src to @dst.
3406 * See the comment of bdrv_co_copy_range for the parameter and return value
3408 int coroutine_fn
bdrv_co_copy_range_from(BdrvChild
*src
, int64_t src_offset
,
3409 BdrvChild
*dst
, int64_t dst_offset
,
3411 BdrvRequestFlags read_flags
,
3412 BdrvRequestFlags write_flags
)
3415 assert_bdrv_graph_readable();
3416 trace_bdrv_co_copy_range_from(src
, src_offset
, dst
, dst_offset
, bytes
,
3417 read_flags
, write_flags
);
3418 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3419 bytes
, read_flags
, write_flags
, true);
3422 /* Copy range from @src to @dst.
3424 * See the comment of bdrv_co_copy_range for the parameter and return value
3426 int coroutine_fn
bdrv_co_copy_range_to(BdrvChild
*src
, int64_t src_offset
,
3427 BdrvChild
*dst
, int64_t dst_offset
,
3429 BdrvRequestFlags read_flags
,
3430 BdrvRequestFlags write_flags
)
3433 assert_bdrv_graph_readable();
3434 trace_bdrv_co_copy_range_to(src
, src_offset
, dst
, dst_offset
, bytes
,
3435 read_flags
, write_flags
);
3436 return bdrv_co_copy_range_internal(src
, src_offset
, dst
, dst_offset
,
3437 bytes
, read_flags
, write_flags
, false);
3440 int coroutine_fn
bdrv_co_copy_range(BdrvChild
*src
, int64_t src_offset
,
3441 BdrvChild
*dst
, int64_t dst_offset
,
3442 int64_t bytes
, BdrvRequestFlags read_flags
,
3443 BdrvRequestFlags write_flags
)
3446 assert_bdrv_graph_readable();
3448 return bdrv_co_copy_range_from(src
, src_offset
,
3450 bytes
, read_flags
, write_flags
);
3453 static void bdrv_parent_cb_resize(BlockDriverState
*bs
)
3456 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
3457 if (c
->klass
->resize
) {
3458 c
->klass
->resize(c
);
3464 * Truncate file to 'offset' bytes (needed only for file protocols)
3466 * If 'exact' is true, the file must be resized to exactly the given
3467 * 'offset'. Otherwise, it is sufficient for the node to be at least
3468 * 'offset' bytes in length.
3470 int coroutine_fn
bdrv_co_truncate(BdrvChild
*child
, int64_t offset
, bool exact
,
3471 PreallocMode prealloc
, BdrvRequestFlags flags
,
3474 BlockDriverState
*bs
= child
->bs
;
3475 BdrvChild
*filtered
, *backing
;
3476 BlockDriver
*drv
= bs
->drv
;
3477 BdrvTrackedRequest req
;
3478 int64_t old_size
, new_bytes
;
3481 assert_bdrv_graph_readable();
3483 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3485 error_setg(errp
, "No medium inserted");
3489 error_setg(errp
, "Image size cannot be negative");
3493 ret
= bdrv_check_request(offset
, 0, errp
);
3498 old_size
= bdrv_getlength(bs
);
3500 error_setg_errno(errp
, -old_size
, "Failed to get old image size");
3504 if (bdrv_is_read_only(bs
)) {
3505 error_setg(errp
, "Image is read-only");
3509 if (offset
> old_size
) {
3510 new_bytes
= offset
- old_size
;
3515 bdrv_inc_in_flight(bs
);
3516 tracked_request_begin(&req
, bs
, offset
- new_bytes
, new_bytes
,
3517 BDRV_TRACKED_TRUNCATE
);
3519 /* If we are growing the image and potentially using preallocation for the
3520 * new area, we need to make sure that no write requests are made to it
3521 * concurrently or they might be overwritten by preallocation. */
3523 bdrv_make_request_serialising(&req
, 1);
3525 ret
= bdrv_co_write_req_prepare(child
, offset
- new_bytes
, new_bytes
, &req
,
3528 error_setg_errno(errp
, -ret
,
3529 "Failed to prepare request for truncation");
3533 filtered
= bdrv_filter_child(bs
);
3534 backing
= bdrv_cow_child(bs
);
3537 * If the image has a backing file that is large enough that it would
3538 * provide data for the new area, we cannot leave it unallocated because
3539 * then the backing file content would become visible. Instead, zero-fill
3542 * Note that if the image has a backing file, but was opened without the
3543 * backing file, taking care of keeping things consistent with that backing
3544 * file is the user's responsibility.
3546 if (new_bytes
&& backing
) {
3547 int64_t backing_len
;
3549 backing_len
= bdrv_co_getlength(backing
->bs
);
3550 if (backing_len
< 0) {
3552 error_setg_errno(errp
, -ret
, "Could not get backing file size");
3556 if (backing_len
> old_size
) {
3557 flags
|= BDRV_REQ_ZERO_WRITE
;
3561 if (drv
->bdrv_co_truncate
) {
3562 if (flags
& ~bs
->supported_truncate_flags
) {
3563 error_setg(errp
, "Block driver does not support requested flags");
3567 ret
= drv
->bdrv_co_truncate(bs
, offset
, exact
, prealloc
, flags
, errp
);
3568 } else if (filtered
) {
3569 ret
= bdrv_co_truncate(filtered
, offset
, exact
, prealloc
, flags
, errp
);
3571 error_setg(errp
, "Image format driver does not support resize");
3579 ret
= bdrv_co_refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
3581 error_setg_errno(errp
, -ret
, "Could not refresh total sector count");
3583 offset
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
3586 * It's possible that truncation succeeded but bdrv_refresh_total_sectors
3587 * failed, but the latter doesn't affect how we should finish the request.
3588 * Pass 0 as the last parameter so that dirty bitmaps etc. are handled.
3590 bdrv_co_write_req_finish(child
, offset
- new_bytes
, new_bytes
, &req
, 0);
3593 tracked_request_end(&req
);
3594 bdrv_dec_in_flight(bs
);
3599 void bdrv_cancel_in_flight(BlockDriverState
*bs
)
3601 GLOBAL_STATE_CODE();
3602 if (!bs
|| !bs
->drv
) {
3606 if (bs
->drv
->bdrv_cancel_in_flight
) {
3607 bs
->drv
->bdrv_cancel_in_flight(bs
);
3612 bdrv_co_preadv_snapshot(BdrvChild
*child
, int64_t offset
, int64_t bytes
,
3613 QEMUIOVector
*qiov
, size_t qiov_offset
)
3615 BlockDriverState
*bs
= child
->bs
;
3616 BlockDriver
*drv
= bs
->drv
;
3619 assert_bdrv_graph_readable();
3625 if (!drv
->bdrv_co_preadv_snapshot
) {
3629 bdrv_inc_in_flight(bs
);
3630 ret
= drv
->bdrv_co_preadv_snapshot(bs
, offset
, bytes
, qiov
, qiov_offset
);
3631 bdrv_dec_in_flight(bs
);
3637 bdrv_co_snapshot_block_status(BlockDriverState
*bs
,
3638 bool want_zero
, int64_t offset
, int64_t bytes
,
3639 int64_t *pnum
, int64_t *map
,
3640 BlockDriverState
**file
)
3642 BlockDriver
*drv
= bs
->drv
;
3645 assert_bdrv_graph_readable();
3651 if (!drv
->bdrv_co_snapshot_block_status
) {
3655 bdrv_inc_in_flight(bs
);
3656 ret
= drv
->bdrv_co_snapshot_block_status(bs
, want_zero
, offset
, bytes
,
3658 bdrv_dec_in_flight(bs
);
3664 bdrv_co_pdiscard_snapshot(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
3666 BlockDriver
*drv
= bs
->drv
;
3669 assert_bdrv_graph_readable();
3675 if (!drv
->bdrv_co_pdiscard_snapshot
) {
3679 bdrv_inc_in_flight(bs
);
3680 ret
= drv
->bdrv_co_pdiscard_snapshot(bs
, offset
, bytes
);
3681 bdrv_dec_in_flight(bs
);