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/blockjob.h"
29 #include "block/block_int.h"
30 #include "qemu/cutils.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
34 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
36 static BlockAIOCB
*bdrv_co_aio_prw_vector(BdrvChild
*child
,
39 BdrvRequestFlags flags
,
40 BlockCompletionFunc
*cb
,
43 static void coroutine_fn
bdrv_co_do_rw(void *opaque
);
44 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
45 int64_t offset
, int count
, BdrvRequestFlags flags
);
47 void bdrv_parent_drained_begin(BlockDriverState
*bs
)
51 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
52 if (c
->role
->drained_begin
) {
53 c
->role
->drained_begin(c
);
58 void bdrv_parent_drained_end(BlockDriverState
*bs
)
62 QLIST_FOREACH(c
, &bs
->parents
, next_parent
) {
63 if (c
->role
->drained_end
) {
64 c
->role
->drained_end(c
);
69 static void bdrv_merge_limits(BlockLimits
*dst
, const BlockLimits
*src
)
71 dst
->opt_transfer
= MAX(dst
->opt_transfer
, src
->opt_transfer
);
72 dst
->max_transfer
= MIN_NON_ZERO(dst
->max_transfer
, src
->max_transfer
);
73 dst
->opt_mem_alignment
= MAX(dst
->opt_mem_alignment
,
74 src
->opt_mem_alignment
);
75 dst
->min_mem_alignment
= MAX(dst
->min_mem_alignment
,
76 src
->min_mem_alignment
);
77 dst
->max_iov
= MIN_NON_ZERO(dst
->max_iov
, src
->max_iov
);
80 void bdrv_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
82 BlockDriver
*drv
= bs
->drv
;
83 Error
*local_err
= NULL
;
85 memset(&bs
->bl
, 0, sizeof(bs
->bl
));
91 /* Default alignment based on whether driver has byte interface */
92 bs
->bl
.request_alignment
= drv
->bdrv_co_preadv
? 1 : 512;
94 /* Take some limits from the children as a default */
96 bdrv_refresh_limits(bs
->file
->bs
, &local_err
);
98 error_propagate(errp
, local_err
);
101 bdrv_merge_limits(&bs
->bl
, &bs
->file
->bs
->bl
);
103 bs
->bl
.min_mem_alignment
= 512;
104 bs
->bl
.opt_mem_alignment
= getpagesize();
106 /* Safe default since most protocols use readv()/writev()/etc */
107 bs
->bl
.max_iov
= IOV_MAX
;
111 bdrv_refresh_limits(bs
->backing
->bs
, &local_err
);
113 error_propagate(errp
, local_err
);
116 bdrv_merge_limits(&bs
->bl
, &bs
->backing
->bs
->bl
);
119 /* Then let the driver override it */
120 if (drv
->bdrv_refresh_limits
) {
121 drv
->bdrv_refresh_limits(bs
, errp
);
126 * The copy-on-read flag is actually a reference count so multiple users may
127 * use the feature without worrying about clobbering its previous state.
128 * Copy-on-read stays enabled until all users have called to disable it.
130 void bdrv_enable_copy_on_read(BlockDriverState
*bs
)
135 void bdrv_disable_copy_on_read(BlockDriverState
*bs
)
137 assert(bs
->copy_on_read
> 0);
141 /* Check if any requests are in-flight (including throttled requests) */
142 bool bdrv_requests_pending(BlockDriverState
*bs
)
146 if (atomic_read(&bs
->in_flight
)) {
150 QLIST_FOREACH(child
, &bs
->children
, next
) {
151 if (bdrv_requests_pending(child
->bs
)) {
159 static bool bdrv_drain_recurse(BlockDriverState
*bs
)
161 BdrvChild
*child
, *tmp
;
164 waited
= BDRV_POLL_WHILE(bs
, atomic_read(&bs
->in_flight
) > 0);
166 if (bs
->drv
&& bs
->drv
->bdrv_drain
) {
167 bs
->drv
->bdrv_drain(bs
);
170 QLIST_FOREACH_SAFE(child
, &bs
->children
, next
, tmp
) {
171 BlockDriverState
*bs
= child
->bs
;
173 qemu_get_current_aio_context() == qemu_get_aio_context();
174 assert(bs
->refcnt
> 0);
176 /* In case the recursive bdrv_drain_recurse processes a
177 * block_job_defer_to_main_loop BH and modifies the graph,
178 * let's hold a reference to bs until we are done.
180 * IOThread doesn't have such a BH, and it is not safe to call
181 * bdrv_unref without BQL, so skip doing it there.
185 waited
|= bdrv_drain_recurse(bs
);
196 BlockDriverState
*bs
;
200 static void bdrv_co_drain_bh_cb(void *opaque
)
202 BdrvCoDrainData
*data
= opaque
;
203 Coroutine
*co
= data
->co
;
204 BlockDriverState
*bs
= data
->bs
;
206 bdrv_dec_in_flight(bs
);
207 bdrv_drained_begin(bs
);
212 static void coroutine_fn
bdrv_co_yield_to_drain(BlockDriverState
*bs
)
214 BdrvCoDrainData data
;
216 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
217 * other coroutines run if they were queued from
218 * qemu_co_queue_run_restart(). */
220 assert(qemu_in_coroutine());
221 data
= (BdrvCoDrainData
) {
222 .co
= qemu_coroutine_self(),
226 bdrv_inc_in_flight(bs
);
227 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
),
228 bdrv_co_drain_bh_cb
, &data
);
230 qemu_coroutine_yield();
231 /* If we are resumed from some other event (such as an aio completion or a
232 * timer callback), it is a bug in the caller that should be fixed. */
236 void bdrv_drained_begin(BlockDriverState
*bs
)
238 if (qemu_in_coroutine()) {
239 bdrv_co_yield_to_drain(bs
);
243 if (!bs
->quiesce_counter
++) {
244 aio_disable_external(bdrv_get_aio_context(bs
));
245 bdrv_parent_drained_begin(bs
);
248 bdrv_drain_recurse(bs
);
251 void bdrv_drained_end(BlockDriverState
*bs
)
253 assert(bs
->quiesce_counter
> 0);
254 if (--bs
->quiesce_counter
> 0) {
258 bdrv_parent_drained_end(bs
);
259 aio_enable_external(bdrv_get_aio_context(bs
));
263 * Wait for pending requests to complete on a single BlockDriverState subtree,
264 * and suspend block driver's internal I/O until next request arrives.
266 * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
269 * Only this BlockDriverState's AioContext is run, so in-flight requests must
270 * not depend on events in other AioContexts. In that case, use
271 * bdrv_drain_all() instead.
273 void coroutine_fn
bdrv_co_drain(BlockDriverState
*bs
)
275 assert(qemu_in_coroutine());
276 bdrv_drained_begin(bs
);
277 bdrv_drained_end(bs
);
280 void bdrv_drain(BlockDriverState
*bs
)
282 bdrv_drained_begin(bs
);
283 bdrv_drained_end(bs
);
287 * Wait for pending requests to complete across all BlockDriverStates
289 * This function does not flush data to disk, use bdrv_flush_all() for that
290 * after calling this function.
292 * This pauses all block jobs and disables external clients. It must
293 * be paired with bdrv_drain_all_end().
295 * NOTE: no new block jobs or BlockDriverStates can be created between
296 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
298 void bdrv_drain_all_begin(void)
300 /* Always run first iteration so any pending completion BHs run */
302 BlockDriverState
*bs
;
304 BlockJob
*job
= NULL
;
305 GSList
*aio_ctxs
= NULL
, *ctx
;
307 while ((job
= block_job_next(job
))) {
308 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
310 aio_context_acquire(aio_context
);
311 block_job_pause(job
);
312 aio_context_release(aio_context
);
315 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
316 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
318 aio_context_acquire(aio_context
);
319 bdrv_parent_drained_begin(bs
);
320 aio_disable_external(aio_context
);
321 aio_context_release(aio_context
);
323 if (!g_slist_find(aio_ctxs
, aio_context
)) {
324 aio_ctxs
= g_slist_prepend(aio_ctxs
, aio_context
);
328 /* Note that completion of an asynchronous I/O operation can trigger any
329 * number of other I/O operations on other devices---for example a
330 * coroutine can submit an I/O request to another device in response to
331 * request completion. Therefore we must keep looping until there was no
332 * more activity rather than simply draining each device independently.
337 for (ctx
= aio_ctxs
; ctx
!= NULL
; ctx
= ctx
->next
) {
338 AioContext
*aio_context
= ctx
->data
;
340 aio_context_acquire(aio_context
);
341 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
342 if (aio_context
== bdrv_get_aio_context(bs
)) {
343 waited
|= bdrv_drain_recurse(bs
);
346 aio_context_release(aio_context
);
350 g_slist_free(aio_ctxs
);
353 void bdrv_drain_all_end(void)
355 BlockDriverState
*bs
;
357 BlockJob
*job
= NULL
;
359 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
360 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
362 aio_context_acquire(aio_context
);
363 aio_enable_external(aio_context
);
364 bdrv_parent_drained_end(bs
);
365 aio_context_release(aio_context
);
368 while ((job
= block_job_next(job
))) {
369 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
371 aio_context_acquire(aio_context
);
372 block_job_resume(job
);
373 aio_context_release(aio_context
);
377 void bdrv_drain_all(void)
379 bdrv_drain_all_begin();
380 bdrv_drain_all_end();
384 * Remove an active request from the tracked requests list
386 * This function should be called when a tracked request is completing.
388 static void tracked_request_end(BdrvTrackedRequest
*req
)
390 if (req
->serialising
) {
391 req
->bs
->serialising_in_flight
--;
394 QLIST_REMOVE(req
, list
);
395 qemu_co_queue_restart_all(&req
->wait_queue
);
399 * Add an active request to the tracked requests list
401 static void tracked_request_begin(BdrvTrackedRequest
*req
,
402 BlockDriverState
*bs
,
405 enum BdrvTrackedRequestType type
)
407 *req
= (BdrvTrackedRequest
){
412 .co
= qemu_coroutine_self(),
413 .serialising
= false,
414 .overlap_offset
= offset
,
415 .overlap_bytes
= bytes
,
418 qemu_co_queue_init(&req
->wait_queue
);
420 QLIST_INSERT_HEAD(&bs
->tracked_requests
, req
, list
);
423 static void mark_request_serialising(BdrvTrackedRequest
*req
, uint64_t align
)
425 int64_t overlap_offset
= req
->offset
& ~(align
- 1);
426 unsigned int overlap_bytes
= ROUND_UP(req
->offset
+ req
->bytes
, align
)
429 if (!req
->serialising
) {
430 req
->bs
->serialising_in_flight
++;
431 req
->serialising
= true;
434 req
->overlap_offset
= MIN(req
->overlap_offset
, overlap_offset
);
435 req
->overlap_bytes
= MAX(req
->overlap_bytes
, overlap_bytes
);
439 * Round a region to cluster boundaries (sector-based)
441 void bdrv_round_sectors_to_clusters(BlockDriverState
*bs
,
442 int64_t sector_num
, int nb_sectors
,
443 int64_t *cluster_sector_num
,
444 int *cluster_nb_sectors
)
448 if (bdrv_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
449 *cluster_sector_num
= sector_num
;
450 *cluster_nb_sectors
= nb_sectors
;
452 int64_t c
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
453 *cluster_sector_num
= QEMU_ALIGN_DOWN(sector_num
, c
);
454 *cluster_nb_sectors
= QEMU_ALIGN_UP(sector_num
- *cluster_sector_num
+
460 * Round a region to cluster boundaries
462 void bdrv_round_to_clusters(BlockDriverState
*bs
,
463 int64_t offset
, unsigned int bytes
,
464 int64_t *cluster_offset
,
465 unsigned int *cluster_bytes
)
469 if (bdrv_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
470 *cluster_offset
= offset
;
471 *cluster_bytes
= bytes
;
473 int64_t c
= bdi
.cluster_size
;
474 *cluster_offset
= QEMU_ALIGN_DOWN(offset
, c
);
475 *cluster_bytes
= QEMU_ALIGN_UP(offset
- *cluster_offset
+ bytes
, c
);
479 static int bdrv_get_cluster_size(BlockDriverState
*bs
)
484 ret
= bdrv_get_info(bs
, &bdi
);
485 if (ret
< 0 || bdi
.cluster_size
== 0) {
486 return bs
->bl
.request_alignment
;
488 return bdi
.cluster_size
;
492 static bool tracked_request_overlaps(BdrvTrackedRequest
*req
,
493 int64_t offset
, unsigned int bytes
)
496 if (offset
>= req
->overlap_offset
+ req
->overlap_bytes
) {
500 if (req
->overlap_offset
>= offset
+ bytes
) {
506 void bdrv_inc_in_flight(BlockDriverState
*bs
)
508 atomic_inc(&bs
->in_flight
);
511 static void dummy_bh_cb(void *opaque
)
515 void bdrv_wakeup(BlockDriverState
*bs
)
518 aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb
, NULL
);
522 void bdrv_dec_in_flight(BlockDriverState
*bs
)
524 atomic_dec(&bs
->in_flight
);
528 static bool coroutine_fn
wait_serialising_requests(BdrvTrackedRequest
*self
)
530 BlockDriverState
*bs
= self
->bs
;
531 BdrvTrackedRequest
*req
;
535 if (!bs
->serialising_in_flight
) {
541 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
542 if (req
== self
|| (!req
->serialising
&& !self
->serialising
)) {
545 if (tracked_request_overlaps(req
, self
->overlap_offset
,
546 self
->overlap_bytes
))
548 /* Hitting this means there was a reentrant request, for
549 * example, a block driver issuing nested requests. This must
550 * never happen since it means deadlock.
552 assert(qemu_coroutine_self() != req
->co
);
554 /* If the request is already (indirectly) waiting for us, or
555 * will wait for us as soon as it wakes up, then just go on
556 * (instead of producing a deadlock in the former case). */
557 if (!req
->waiting_for
) {
558 self
->waiting_for
= req
;
559 qemu_co_queue_wait(&req
->wait_queue
, NULL
);
560 self
->waiting_for
= NULL
;
572 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
575 if (size
> BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
) {
579 if (!bdrv_is_inserted(bs
)) {
590 typedef struct RwCo
{
596 BdrvRequestFlags flags
;
599 static void coroutine_fn
bdrv_rw_co_entry(void *opaque
)
603 if (!rwco
->is_write
) {
604 rwco
->ret
= bdrv_co_preadv(rwco
->child
, rwco
->offset
,
605 rwco
->qiov
->size
, rwco
->qiov
,
608 rwco
->ret
= bdrv_co_pwritev(rwco
->child
, rwco
->offset
,
609 rwco
->qiov
->size
, rwco
->qiov
,
615 * Process a vectored synchronous request using coroutines
617 static int bdrv_prwv_co(BdrvChild
*child
, int64_t offset
,
618 QEMUIOVector
*qiov
, bool is_write
,
619 BdrvRequestFlags flags
)
626 .is_write
= is_write
,
631 if (qemu_in_coroutine()) {
632 /* Fast-path if already in coroutine context */
633 bdrv_rw_co_entry(&rwco
);
635 co
= qemu_coroutine_create(bdrv_rw_co_entry
, &rwco
);
636 bdrv_coroutine_enter(child
->bs
, co
);
637 BDRV_POLL_WHILE(child
->bs
, rwco
.ret
== NOT_DONE
);
643 * Process a synchronous request using coroutines
645 static int bdrv_rw_co(BdrvChild
*child
, int64_t sector_num
, uint8_t *buf
,
646 int nb_sectors
, bool is_write
, BdrvRequestFlags flags
)
650 .iov_base
= (void *)buf
,
651 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
654 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
658 qemu_iovec_init_external(&qiov
, &iov
, 1);
659 return bdrv_prwv_co(child
, sector_num
<< BDRV_SECTOR_BITS
,
660 &qiov
, is_write
, flags
);
663 /* return < 0 if error. See bdrv_write() for the return codes */
664 int bdrv_read(BdrvChild
*child
, int64_t sector_num
,
665 uint8_t *buf
, int nb_sectors
)
667 return bdrv_rw_co(child
, sector_num
, buf
, nb_sectors
, false, 0);
670 /* Return < 0 if error. Important errors are:
671 -EIO generic I/O error (may happen for all errors)
672 -ENOMEDIUM No media inserted.
673 -EINVAL Invalid sector number or nb_sectors
674 -EACCES Trying to write a read-only device
676 int bdrv_write(BdrvChild
*child
, int64_t sector_num
,
677 const uint8_t *buf
, int nb_sectors
)
679 return bdrv_rw_co(child
, sector_num
, (uint8_t *)buf
, nb_sectors
, true, 0);
682 int bdrv_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
683 int count
, BdrvRequestFlags flags
)
691 qemu_iovec_init_external(&qiov
, &iov
, 1);
692 return bdrv_prwv_co(child
, offset
, &qiov
, true,
693 BDRV_REQ_ZERO_WRITE
| flags
);
697 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
698 * The operation is sped up by checking the block status and only writing
699 * zeroes to the device if they currently do not return zeroes. Optional
700 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
703 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
705 int bdrv_make_zero(BdrvChild
*child
, BdrvRequestFlags flags
)
707 int64_t target_sectors
, ret
, nb_sectors
, sector_num
= 0;
708 BlockDriverState
*bs
= child
->bs
;
709 BlockDriverState
*file
;
712 target_sectors
= bdrv_nb_sectors(bs
);
713 if (target_sectors
< 0) {
714 return target_sectors
;
718 nb_sectors
= MIN(target_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
719 if (nb_sectors
<= 0) {
722 ret
= bdrv_get_block_status(bs
, sector_num
, nb_sectors
, &n
, &file
);
724 error_report("error getting block status at sector %" PRId64
": %s",
725 sector_num
, strerror(-ret
));
728 if (ret
& BDRV_BLOCK_ZERO
) {
732 ret
= bdrv_pwrite_zeroes(child
, sector_num
<< BDRV_SECTOR_BITS
,
733 n
<< BDRV_SECTOR_BITS
, flags
);
735 error_report("error writing zeroes at sector %" PRId64
": %s",
736 sector_num
, strerror(-ret
));
743 int bdrv_preadv(BdrvChild
*child
, int64_t offset
, QEMUIOVector
*qiov
)
747 ret
= bdrv_prwv_co(child
, offset
, qiov
, false, 0);
755 int bdrv_pread(BdrvChild
*child
, int64_t offset
, void *buf
, int bytes
)
759 .iov_base
= (void *)buf
,
767 qemu_iovec_init_external(&qiov
, &iov
, 1);
768 return bdrv_preadv(child
, offset
, &qiov
);
771 int bdrv_pwritev(BdrvChild
*child
, int64_t offset
, QEMUIOVector
*qiov
)
775 ret
= bdrv_prwv_co(child
, offset
, qiov
, true, 0);
783 int bdrv_pwrite(BdrvChild
*child
, int64_t offset
, const void *buf
, int bytes
)
787 .iov_base
= (void *) buf
,
795 qemu_iovec_init_external(&qiov
, &iov
, 1);
796 return bdrv_pwritev(child
, offset
, &qiov
);
800 * Writes to the file and ensures that no writes are reordered across this
801 * request (acts as a barrier)
803 * Returns 0 on success, -errno in error cases.
805 int bdrv_pwrite_sync(BdrvChild
*child
, int64_t offset
,
806 const void *buf
, int count
)
810 ret
= bdrv_pwrite(child
, offset
, buf
, count
);
815 ret
= bdrv_flush(child
->bs
);
823 typedef struct CoroutineIOCompletion
{
824 Coroutine
*coroutine
;
826 } CoroutineIOCompletion
;
828 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
830 CoroutineIOCompletion
*co
= opaque
;
833 aio_co_wake(co
->coroutine
);
836 static int coroutine_fn
bdrv_driver_preadv(BlockDriverState
*bs
,
837 uint64_t offset
, uint64_t bytes
,
838 QEMUIOVector
*qiov
, int flags
)
840 BlockDriver
*drv
= bs
->drv
;
842 unsigned int nb_sectors
;
844 assert(!(flags
& ~BDRV_REQ_MASK
));
846 if (drv
->bdrv_co_preadv
) {
847 return drv
->bdrv_co_preadv(bs
, offset
, bytes
, qiov
, flags
);
850 sector_num
= offset
>> BDRV_SECTOR_BITS
;
851 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
853 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
854 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
855 assert((bytes
>> BDRV_SECTOR_BITS
) <= BDRV_REQUEST_MAX_SECTORS
);
857 if (drv
->bdrv_co_readv
) {
858 return drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
861 CoroutineIOCompletion co
= {
862 .coroutine
= qemu_coroutine_self(),
865 acb
= bs
->drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
866 bdrv_co_io_em_complete
, &co
);
870 qemu_coroutine_yield();
876 static int coroutine_fn
bdrv_driver_pwritev(BlockDriverState
*bs
,
877 uint64_t offset
, uint64_t bytes
,
878 QEMUIOVector
*qiov
, int flags
)
880 BlockDriver
*drv
= bs
->drv
;
882 unsigned int nb_sectors
;
885 assert(!(flags
& ~BDRV_REQ_MASK
));
887 if (drv
->bdrv_co_pwritev
) {
888 ret
= drv
->bdrv_co_pwritev(bs
, offset
, bytes
, qiov
,
889 flags
& bs
->supported_write_flags
);
890 flags
&= ~bs
->supported_write_flags
;
894 sector_num
= offset
>> BDRV_SECTOR_BITS
;
895 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
897 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
898 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
899 assert((bytes
>> BDRV_SECTOR_BITS
) <= BDRV_REQUEST_MAX_SECTORS
);
901 if (drv
->bdrv_co_writev_flags
) {
902 ret
= drv
->bdrv_co_writev_flags(bs
, sector_num
, nb_sectors
, qiov
,
903 flags
& bs
->supported_write_flags
);
904 flags
&= ~bs
->supported_write_flags
;
905 } else if (drv
->bdrv_co_writev
) {
906 assert(!bs
->supported_write_flags
);
907 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
);
910 CoroutineIOCompletion co
= {
911 .coroutine
= qemu_coroutine_self(),
914 acb
= bs
->drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
915 bdrv_co_io_em_complete
, &co
);
919 qemu_coroutine_yield();
925 if (ret
== 0 && (flags
& BDRV_REQ_FUA
)) {
926 ret
= bdrv_co_flush(bs
);
932 static int coroutine_fn
933 bdrv_driver_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
934 uint64_t bytes
, QEMUIOVector
*qiov
)
936 BlockDriver
*drv
= bs
->drv
;
938 if (!drv
->bdrv_co_pwritev_compressed
) {
942 return drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, qiov
);
945 static int coroutine_fn
bdrv_co_do_copy_on_readv(BdrvChild
*child
,
946 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
)
948 BlockDriverState
*bs
= child
->bs
;
950 /* Perform I/O through a temporary buffer so that users who scribble over
951 * their read buffer while the operation is in progress do not end up
952 * modifying the image file. This is critical for zero-copy guest I/O
953 * where anything might happen inside guest memory.
957 BlockDriver
*drv
= bs
->drv
;
959 QEMUIOVector bounce_qiov
;
960 int64_t cluster_offset
;
961 unsigned int cluster_bytes
;
965 /* FIXME We cannot require callers to have write permissions when all they
966 * are doing is a read request. If we did things right, write permissions
967 * would be obtained anyway, but internally by the copy-on-read code. As
968 * long as it is implemented here rather than in a separat filter driver,
969 * the copy-on-read code doesn't have its own BdrvChild, however, for which
970 * it could request permissions. Therefore we have to bypass the permission
971 * system for the moment. */
972 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
974 /* Cover entire cluster so no additional backing file I/O is required when
975 * allocating cluster in the image file.
977 bdrv_round_to_clusters(bs
, offset
, bytes
, &cluster_offset
, &cluster_bytes
);
979 trace_bdrv_co_do_copy_on_readv(bs
, offset
, bytes
,
980 cluster_offset
, cluster_bytes
);
982 iov
.iov_len
= cluster_bytes
;
983 iov
.iov_base
= bounce_buffer
= qemu_try_blockalign(bs
, iov
.iov_len
);
984 if (bounce_buffer
== NULL
) {
989 qemu_iovec_init_external(&bounce_qiov
, &iov
, 1);
991 ret
= bdrv_driver_preadv(bs
, cluster_offset
, cluster_bytes
,
997 if (drv
->bdrv_co_pwrite_zeroes
&&
998 buffer_is_zero(bounce_buffer
, iov
.iov_len
)) {
999 /* FIXME: Should we (perhaps conditionally) be setting
1000 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1001 * that still correctly reads as zero? */
1002 ret
= bdrv_co_do_pwrite_zeroes(bs
, cluster_offset
, cluster_bytes
, 0);
1004 /* This does not change the data on the disk, it is not necessary
1005 * to flush even in cache=writethrough mode.
1007 ret
= bdrv_driver_pwritev(bs
, cluster_offset
, cluster_bytes
,
1012 /* It might be okay to ignore write errors for guest requests. If this
1013 * is a deliberate copy-on-read then we don't want to ignore the error.
1014 * Simply report it in all cases.
1019 skip_bytes
= offset
- cluster_offset
;
1020 qemu_iovec_from_buf(qiov
, 0, bounce_buffer
+ skip_bytes
, bytes
);
1023 qemu_vfree(bounce_buffer
);
1028 * Forwards an already correctly aligned request to the BlockDriver. This
1029 * handles copy on read, zeroing after EOF, and fragmentation of large
1030 * reads; any other features must be implemented by the caller.
1032 static int coroutine_fn
bdrv_aligned_preadv(BdrvChild
*child
,
1033 BdrvTrackedRequest
*req
, int64_t offset
, unsigned int bytes
,
1034 int64_t align
, QEMUIOVector
*qiov
, int flags
)
1036 BlockDriverState
*bs
= child
->bs
;
1037 int64_t total_bytes
, max_bytes
;
1039 uint64_t bytes_remaining
= bytes
;
1042 assert(is_power_of_2(align
));
1043 assert((offset
& (align
- 1)) == 0);
1044 assert((bytes
& (align
- 1)) == 0);
1045 assert(!qiov
|| bytes
== qiov
->size
);
1046 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1047 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1050 /* TODO: We would need a per-BDS .supported_read_flags and
1051 * potential fallback support, if we ever implement any read flags
1052 * to pass through to drivers. For now, there aren't any
1053 * passthrough flags. */
1054 assert(!(flags
& ~(BDRV_REQ_NO_SERIALISING
| BDRV_REQ_COPY_ON_READ
)));
1056 /* Handle Copy on Read and associated serialisation */
1057 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1058 /* If we touch the same cluster it counts as an overlap. This
1059 * guarantees that allocating writes will be serialized and not race
1060 * with each other for the same cluster. For example, in copy-on-read
1061 * it ensures that the CoR read and write operations are atomic and
1062 * guest writes cannot interleave between them. */
1063 mark_request_serialising(req
, bdrv_get_cluster_size(bs
));
1066 if (!(flags
& BDRV_REQ_NO_SERIALISING
)) {
1067 wait_serialising_requests(req
);
1070 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1071 int64_t start_sector
= offset
>> BDRV_SECTOR_BITS
;
1072 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
1073 unsigned int nb_sectors
= end_sector
- start_sector
;
1076 ret
= bdrv_is_allocated(bs
, start_sector
, nb_sectors
, &pnum
);
1081 if (!ret
|| pnum
!= nb_sectors
) {
1082 ret
= bdrv_co_do_copy_on_readv(child
, offset
, bytes
, qiov
);
1087 /* Forward the request to the BlockDriver, possibly fragmenting it */
1088 total_bytes
= bdrv_getlength(bs
);
1089 if (total_bytes
< 0) {
1094 max_bytes
= ROUND_UP(MAX(0, total_bytes
- offset
), align
);
1095 if (bytes
<= max_bytes
&& bytes
<= max_transfer
) {
1096 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, 0);
1100 while (bytes_remaining
) {
1104 QEMUIOVector local_qiov
;
1106 num
= MIN(bytes_remaining
, MIN(max_bytes
, max_transfer
));
1108 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1109 qemu_iovec_concat(&local_qiov
, qiov
, bytes
- bytes_remaining
, num
);
1111 ret
= bdrv_driver_preadv(bs
, offset
+ bytes
- bytes_remaining
,
1112 num
, &local_qiov
, 0);
1114 qemu_iovec_destroy(&local_qiov
);
1116 num
= bytes_remaining
;
1117 ret
= qemu_iovec_memset(qiov
, bytes
- bytes_remaining
, 0,
1123 bytes_remaining
-= num
;
1127 return ret
< 0 ? ret
: 0;
1131 * Handle a read request in coroutine context
1133 int coroutine_fn
bdrv_co_preadv(BdrvChild
*child
,
1134 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1135 BdrvRequestFlags flags
)
1137 BlockDriverState
*bs
= child
->bs
;
1138 BlockDriver
*drv
= bs
->drv
;
1139 BdrvTrackedRequest req
;
1141 uint64_t align
= bs
->bl
.request_alignment
;
1142 uint8_t *head_buf
= NULL
;
1143 uint8_t *tail_buf
= NULL
;
1144 QEMUIOVector local_qiov
;
1145 bool use_local_qiov
= false;
1152 ret
= bdrv_check_byte_request(bs
, offset
, bytes
);
1157 bdrv_inc_in_flight(bs
);
1159 /* Don't do copy-on-read if we read data before write operation */
1160 if (bs
->copy_on_read
&& !(flags
& BDRV_REQ_NO_SERIALISING
)) {
1161 flags
|= BDRV_REQ_COPY_ON_READ
;
1164 /* Align read if necessary by padding qiov */
1165 if (offset
& (align
- 1)) {
1166 head_buf
= qemu_blockalign(bs
, align
);
1167 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 2);
1168 qemu_iovec_add(&local_qiov
, head_buf
, offset
& (align
- 1));
1169 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1170 use_local_qiov
= true;
1172 bytes
+= offset
& (align
- 1);
1173 offset
= offset
& ~(align
- 1);
1176 if ((offset
+ bytes
) & (align
- 1)) {
1177 if (!use_local_qiov
) {
1178 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 1);
1179 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1180 use_local_qiov
= true;
1182 tail_buf
= qemu_blockalign(bs
, align
);
1183 qemu_iovec_add(&local_qiov
, tail_buf
,
1184 align
- ((offset
+ bytes
) & (align
- 1)));
1186 bytes
= ROUND_UP(bytes
, align
);
1189 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_READ
);
1190 ret
= bdrv_aligned_preadv(child
, &req
, offset
, bytes
, align
,
1191 use_local_qiov
? &local_qiov
: qiov
,
1193 tracked_request_end(&req
);
1194 bdrv_dec_in_flight(bs
);
1196 if (use_local_qiov
) {
1197 qemu_iovec_destroy(&local_qiov
);
1198 qemu_vfree(head_buf
);
1199 qemu_vfree(tail_buf
);
1205 static int coroutine_fn
bdrv_co_do_readv(BdrvChild
*child
,
1206 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
,
1207 BdrvRequestFlags flags
)
1209 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
1213 return bdrv_co_preadv(child
, sector_num
<< BDRV_SECTOR_BITS
,
1214 nb_sectors
<< BDRV_SECTOR_BITS
, qiov
, flags
);
1217 int coroutine_fn
bdrv_co_readv(BdrvChild
*child
, int64_t sector_num
,
1218 int nb_sectors
, QEMUIOVector
*qiov
)
1220 trace_bdrv_co_readv(child
->bs
, sector_num
, nb_sectors
);
1222 return bdrv_co_do_readv(child
, sector_num
, nb_sectors
, qiov
, 0);
1225 /* Maximum buffer for write zeroes fallback, in bytes */
1226 #define MAX_WRITE_ZEROES_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
1228 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
1229 int64_t offset
, int count
, BdrvRequestFlags flags
)
1231 BlockDriver
*drv
= bs
->drv
;
1233 struct iovec iov
= {0};
1235 bool need_flush
= false;
1239 int max_write_zeroes
= MIN_NON_ZERO(bs
->bl
.max_pwrite_zeroes
, INT_MAX
);
1240 int alignment
= MAX(bs
->bl
.pwrite_zeroes_alignment
,
1241 bs
->bl
.request_alignment
);
1242 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
,
1243 MAX_WRITE_ZEROES_BOUNCE_BUFFER
);
1245 assert(alignment
% bs
->bl
.request_alignment
== 0);
1246 head
= offset
% alignment
;
1247 tail
= (offset
+ count
) % alignment
;
1248 max_write_zeroes
= QEMU_ALIGN_DOWN(max_write_zeroes
, alignment
);
1249 assert(max_write_zeroes
>= bs
->bl
.request_alignment
);
1251 while (count
> 0 && !ret
) {
1254 /* Align request. Block drivers can expect the "bulk" of the request
1255 * to be aligned, and that unaligned requests do not cross cluster
1259 /* Make a small request up to the first aligned sector. For
1260 * convenience, limit this request to max_transfer even if
1261 * we don't need to fall back to writes. */
1262 num
= MIN(MIN(count
, max_transfer
), alignment
- head
);
1263 head
= (head
+ num
) % alignment
;
1264 assert(num
< max_write_zeroes
);
1265 } else if (tail
&& num
> alignment
) {
1266 /* Shorten the request to the last aligned sector. */
1270 /* limit request size */
1271 if (num
> max_write_zeroes
) {
1272 num
= max_write_zeroes
;
1276 /* First try the efficient write zeroes operation */
1277 if (drv
->bdrv_co_pwrite_zeroes
) {
1278 ret
= drv
->bdrv_co_pwrite_zeroes(bs
, offset
, num
,
1279 flags
& bs
->supported_zero_flags
);
1280 if (ret
!= -ENOTSUP
&& (flags
& BDRV_REQ_FUA
) &&
1281 !(bs
->supported_zero_flags
& BDRV_REQ_FUA
)) {
1285 assert(!bs
->supported_zero_flags
);
1288 if (ret
== -ENOTSUP
) {
1289 /* Fall back to bounce buffer if write zeroes is unsupported */
1290 BdrvRequestFlags write_flags
= flags
& ~BDRV_REQ_ZERO_WRITE
;
1292 if ((flags
& BDRV_REQ_FUA
) &&
1293 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1294 /* No need for bdrv_driver_pwrite() to do a fallback
1295 * flush on each chunk; use just one at the end */
1296 write_flags
&= ~BDRV_REQ_FUA
;
1299 num
= MIN(num
, max_transfer
);
1301 if (iov
.iov_base
== NULL
) {
1302 iov
.iov_base
= qemu_try_blockalign(bs
, num
);
1303 if (iov
.iov_base
== NULL
) {
1307 memset(iov
.iov_base
, 0, num
);
1309 qemu_iovec_init_external(&qiov
, &iov
, 1);
1311 ret
= bdrv_driver_pwritev(bs
, offset
, num
, &qiov
, write_flags
);
1313 /* Keep bounce buffer around if it is big enough for all
1314 * all future requests.
1316 if (num
< max_transfer
) {
1317 qemu_vfree(iov
.iov_base
);
1318 iov
.iov_base
= NULL
;
1327 if (ret
== 0 && need_flush
) {
1328 ret
= bdrv_co_flush(bs
);
1330 qemu_vfree(iov
.iov_base
);
1335 * Forwards an already correctly aligned write request to the BlockDriver,
1336 * after possibly fragmenting it.
1338 static int coroutine_fn
bdrv_aligned_pwritev(BdrvChild
*child
,
1339 BdrvTrackedRequest
*req
, int64_t offset
, unsigned int bytes
,
1340 int64_t align
, QEMUIOVector
*qiov
, int flags
)
1342 BlockDriverState
*bs
= child
->bs
;
1343 BlockDriver
*drv
= bs
->drv
;
1347 int64_t start_sector
= offset
>> BDRV_SECTOR_BITS
;
1348 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
1349 uint64_t bytes_remaining
= bytes
;
1352 assert(is_power_of_2(align
));
1353 assert((offset
& (align
- 1)) == 0);
1354 assert((bytes
& (align
- 1)) == 0);
1355 assert(!qiov
|| bytes
== qiov
->size
);
1356 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1357 assert(!(flags
& ~BDRV_REQ_MASK
));
1358 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1361 waited
= wait_serialising_requests(req
);
1362 assert(!waited
|| !req
->serialising
);
1363 assert(req
->overlap_offset
<= offset
);
1364 assert(offset
+ bytes
<= req
->overlap_offset
+ req
->overlap_bytes
);
1365 /* FIXME: Block migration uses the BlockBackend of the guest device at a
1366 * point when it has not yet taken write permissions. This will be
1367 * fixed by a future patch, but for now we have to bypass this
1368 * assertion for block migration to work. */
1369 // assert(child->perm & BLK_PERM_WRITE);
1370 /* FIXME: Because of the above, we also cannot guarantee that all format
1371 * BDS take the BLK_PERM_RESIZE permission on their file BDS, since
1372 * they are not obligated to do so if they do not have any parent
1373 * that has taken the permission to write to them. */
1374 // assert(end_sector <= bs->total_sectors || child->perm & BLK_PERM_RESIZE);
1376 ret
= notifier_with_return_list_notify(&bs
->before_write_notifiers
, req
);
1378 if (!ret
&& bs
->detect_zeroes
!= BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
&&
1379 !(flags
& BDRV_REQ_ZERO_WRITE
) && drv
->bdrv_co_pwrite_zeroes
&&
1380 qemu_iovec_is_zero(qiov
)) {
1381 flags
|= BDRV_REQ_ZERO_WRITE
;
1382 if (bs
->detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
) {
1383 flags
|= BDRV_REQ_MAY_UNMAP
;
1388 /* Do nothing, write notifier decided to fail this request */
1389 } else if (flags
& BDRV_REQ_ZERO_WRITE
) {
1390 bdrv_debug_event(bs
, BLKDBG_PWRITEV_ZERO
);
1391 ret
= bdrv_co_do_pwrite_zeroes(bs
, offset
, bytes
, flags
);
1392 } else if (flags
& BDRV_REQ_WRITE_COMPRESSED
) {
1393 ret
= bdrv_driver_pwritev_compressed(bs
, offset
, bytes
, qiov
);
1394 } else if (bytes
<= max_transfer
) {
1395 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
1396 ret
= bdrv_driver_pwritev(bs
, offset
, bytes
, qiov
, flags
);
1398 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
1399 while (bytes_remaining
) {
1400 int num
= MIN(bytes_remaining
, max_transfer
);
1401 QEMUIOVector local_qiov
;
1402 int local_flags
= flags
;
1405 if (num
< bytes_remaining
&& (flags
& BDRV_REQ_FUA
) &&
1406 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1407 /* If FUA is going to be emulated by flush, we only
1408 * need to flush on the last iteration */
1409 local_flags
&= ~BDRV_REQ_FUA
;
1411 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1412 qemu_iovec_concat(&local_qiov
, qiov
, bytes
- bytes_remaining
, num
);
1414 ret
= bdrv_driver_pwritev(bs
, offset
+ bytes
- bytes_remaining
,
1415 num
, &local_qiov
, local_flags
);
1416 qemu_iovec_destroy(&local_qiov
);
1420 bytes_remaining
-= num
;
1423 bdrv_debug_event(bs
, BLKDBG_PWRITEV_DONE
);
1426 bdrv_set_dirty(bs
, start_sector
, end_sector
- start_sector
);
1428 if (bs
->wr_highest_offset
< offset
+ bytes
) {
1429 bs
->wr_highest_offset
= offset
+ bytes
;
1433 bs
->total_sectors
= MAX(bs
->total_sectors
, end_sector
);
1440 static int coroutine_fn
bdrv_co_do_zero_pwritev(BdrvChild
*child
,
1443 BdrvRequestFlags flags
,
1444 BdrvTrackedRequest
*req
)
1446 BlockDriverState
*bs
= child
->bs
;
1447 uint8_t *buf
= NULL
;
1448 QEMUIOVector local_qiov
;
1450 uint64_t align
= bs
->bl
.request_alignment
;
1451 unsigned int head_padding_bytes
, tail_padding_bytes
;
1454 head_padding_bytes
= offset
& (align
- 1);
1455 tail_padding_bytes
= align
- ((offset
+ bytes
) & (align
- 1));
1458 assert(flags
& BDRV_REQ_ZERO_WRITE
);
1459 if (head_padding_bytes
|| tail_padding_bytes
) {
1460 buf
= qemu_blockalign(bs
, align
);
1461 iov
= (struct iovec
) {
1465 qemu_iovec_init_external(&local_qiov
, &iov
, 1);
1467 if (head_padding_bytes
) {
1468 uint64_t zero_bytes
= MIN(bytes
, align
- head_padding_bytes
);
1470 /* RMW the unaligned part before head. */
1471 mark_request_serialising(req
, align
);
1472 wait_serialising_requests(req
);
1473 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1474 ret
= bdrv_aligned_preadv(child
, req
, offset
& ~(align
- 1), align
,
1475 align
, &local_qiov
, 0);
1479 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1481 memset(buf
+ head_padding_bytes
, 0, zero_bytes
);
1482 ret
= bdrv_aligned_pwritev(child
, req
, offset
& ~(align
- 1), align
,
1484 flags
& ~BDRV_REQ_ZERO_WRITE
);
1488 offset
+= zero_bytes
;
1489 bytes
-= zero_bytes
;
1492 assert(!bytes
|| (offset
& (align
- 1)) == 0);
1493 if (bytes
>= align
) {
1494 /* Write the aligned part in the middle. */
1495 uint64_t aligned_bytes
= bytes
& ~(align
- 1);
1496 ret
= bdrv_aligned_pwritev(child
, req
, offset
, aligned_bytes
, align
,
1501 bytes
-= aligned_bytes
;
1502 offset
+= aligned_bytes
;
1505 assert(!bytes
|| (offset
& (align
- 1)) == 0);
1507 assert(align
== tail_padding_bytes
+ bytes
);
1508 /* RMW the unaligned part after tail. */
1509 mark_request_serialising(req
, align
);
1510 wait_serialising_requests(req
);
1511 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1512 ret
= bdrv_aligned_preadv(child
, req
, offset
, align
,
1513 align
, &local_qiov
, 0);
1517 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1519 memset(buf
, 0, bytes
);
1520 ret
= bdrv_aligned_pwritev(child
, req
, offset
, align
, align
,
1521 &local_qiov
, flags
& ~BDRV_REQ_ZERO_WRITE
);
1530 * Handle a write request in coroutine context
1532 int coroutine_fn
bdrv_co_pwritev(BdrvChild
*child
,
1533 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1534 BdrvRequestFlags flags
)
1536 BlockDriverState
*bs
= child
->bs
;
1537 BdrvTrackedRequest req
;
1538 uint64_t align
= bs
->bl
.request_alignment
;
1539 uint8_t *head_buf
= NULL
;
1540 uint8_t *tail_buf
= NULL
;
1541 QEMUIOVector local_qiov
;
1542 bool use_local_qiov
= false;
1548 if (bs
->read_only
) {
1551 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
1553 ret
= bdrv_check_byte_request(bs
, offset
, bytes
);
1558 bdrv_inc_in_flight(bs
);
1560 * Align write if necessary by performing a read-modify-write cycle.
1561 * Pad qiov with the read parts and be sure to have a tracked request not
1562 * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
1564 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_WRITE
);
1567 ret
= bdrv_co_do_zero_pwritev(child
, offset
, bytes
, flags
, &req
);
1571 if (offset
& (align
- 1)) {
1572 QEMUIOVector head_qiov
;
1573 struct iovec head_iov
;
1575 mark_request_serialising(&req
, align
);
1576 wait_serialising_requests(&req
);
1578 head_buf
= qemu_blockalign(bs
, align
);
1579 head_iov
= (struct iovec
) {
1580 .iov_base
= head_buf
,
1583 qemu_iovec_init_external(&head_qiov
, &head_iov
, 1);
1585 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1586 ret
= bdrv_aligned_preadv(child
, &req
, offset
& ~(align
- 1), align
,
1587 align
, &head_qiov
, 0);
1591 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1593 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 2);
1594 qemu_iovec_add(&local_qiov
, head_buf
, offset
& (align
- 1));
1595 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1596 use_local_qiov
= true;
1598 bytes
+= offset
& (align
- 1);
1599 offset
= offset
& ~(align
- 1);
1601 /* We have read the tail already if the request is smaller
1602 * than one aligned block.
1604 if (bytes
< align
) {
1605 qemu_iovec_add(&local_qiov
, head_buf
+ bytes
, align
- bytes
);
1610 if ((offset
+ bytes
) & (align
- 1)) {
1611 QEMUIOVector tail_qiov
;
1612 struct iovec tail_iov
;
1616 mark_request_serialising(&req
, align
);
1617 waited
= wait_serialising_requests(&req
);
1618 assert(!waited
|| !use_local_qiov
);
1620 tail_buf
= qemu_blockalign(bs
, align
);
1621 tail_iov
= (struct iovec
) {
1622 .iov_base
= tail_buf
,
1625 qemu_iovec_init_external(&tail_qiov
, &tail_iov
, 1);
1627 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1628 ret
= bdrv_aligned_preadv(child
, &req
, (offset
+ bytes
) & ~(align
- 1),
1629 align
, align
, &tail_qiov
, 0);
1633 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1635 if (!use_local_qiov
) {
1636 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 1);
1637 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1638 use_local_qiov
= true;
1641 tail_bytes
= (offset
+ bytes
) & (align
- 1);
1642 qemu_iovec_add(&local_qiov
, tail_buf
+ tail_bytes
, align
- tail_bytes
);
1644 bytes
= ROUND_UP(bytes
, align
);
1647 ret
= bdrv_aligned_pwritev(child
, &req
, offset
, bytes
, align
,
1648 use_local_qiov
? &local_qiov
: qiov
,
1653 if (use_local_qiov
) {
1654 qemu_iovec_destroy(&local_qiov
);
1656 qemu_vfree(head_buf
);
1657 qemu_vfree(tail_buf
);
1659 tracked_request_end(&req
);
1660 bdrv_dec_in_flight(bs
);
1664 static int coroutine_fn
bdrv_co_do_writev(BdrvChild
*child
,
1665 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
,
1666 BdrvRequestFlags flags
)
1668 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
1672 return bdrv_co_pwritev(child
, sector_num
<< BDRV_SECTOR_BITS
,
1673 nb_sectors
<< BDRV_SECTOR_BITS
, qiov
, flags
);
1676 int coroutine_fn
bdrv_co_writev(BdrvChild
*child
, int64_t sector_num
,
1677 int nb_sectors
, QEMUIOVector
*qiov
)
1679 trace_bdrv_co_writev(child
->bs
, sector_num
, nb_sectors
);
1681 return bdrv_co_do_writev(child
, sector_num
, nb_sectors
, qiov
, 0);
1684 int coroutine_fn
bdrv_co_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
1685 int count
, BdrvRequestFlags flags
)
1687 trace_bdrv_co_pwrite_zeroes(child
->bs
, offset
, count
, flags
);
1689 if (!(child
->bs
->open_flags
& BDRV_O_UNMAP
)) {
1690 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1693 return bdrv_co_pwritev(child
, offset
, count
, NULL
,
1694 BDRV_REQ_ZERO_WRITE
| flags
);
1698 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
1700 int bdrv_flush_all(void)
1702 BdrvNextIterator it
;
1703 BlockDriverState
*bs
= NULL
;
1706 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1707 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
1710 aio_context_acquire(aio_context
);
1711 ret
= bdrv_flush(bs
);
1712 if (ret
< 0 && !result
) {
1715 aio_context_release(aio_context
);
1722 typedef struct BdrvCoGetBlockStatusData
{
1723 BlockDriverState
*bs
;
1724 BlockDriverState
*base
;
1725 BlockDriverState
**file
;
1731 } BdrvCoGetBlockStatusData
;
1734 * Returns the allocation status of the specified sectors.
1735 * Drivers not implementing the functionality are assumed to not support
1736 * backing files, hence all their sectors are reported as allocated.
1738 * If 'sector_num' is beyond the end of the disk image the return value is 0
1739 * and 'pnum' is set to 0.
1741 * 'pnum' is set to the number of sectors (including and immediately following
1742 * the specified sector) that are known to be in the same
1743 * allocated/unallocated state.
1745 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1746 * beyond the end of the disk image it will be clamped.
1748 * If returned value is positive and BDRV_BLOCK_OFFSET_VALID bit is set, 'file'
1749 * points to the BDS which the sector range is allocated in.
1751 static int64_t coroutine_fn
bdrv_co_get_block_status(BlockDriverState
*bs
,
1753 int nb_sectors
, int *pnum
,
1754 BlockDriverState
**file
)
1756 int64_t total_sectors
;
1760 total_sectors
= bdrv_nb_sectors(bs
);
1761 if (total_sectors
< 0) {
1762 return total_sectors
;
1765 if (sector_num
>= total_sectors
) {
1770 n
= total_sectors
- sector_num
;
1771 if (n
< nb_sectors
) {
1775 if (!bs
->drv
->bdrv_co_get_block_status
) {
1777 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
;
1778 if (bs
->drv
->protocol_name
) {
1779 ret
|= BDRV_BLOCK_OFFSET_VALID
| (sector_num
* BDRV_SECTOR_SIZE
);
1785 bdrv_inc_in_flight(bs
);
1786 ret
= bs
->drv
->bdrv_co_get_block_status(bs
, sector_num
, nb_sectors
, pnum
,
1793 if (ret
& BDRV_BLOCK_RAW
) {
1794 assert(ret
& BDRV_BLOCK_OFFSET_VALID
);
1795 ret
= bdrv_get_block_status(*file
, ret
>> BDRV_SECTOR_BITS
,
1800 if (ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ZERO
)) {
1801 ret
|= BDRV_BLOCK_ALLOCATED
;
1803 if (bdrv_unallocated_blocks_are_zero(bs
)) {
1804 ret
|= BDRV_BLOCK_ZERO
;
1805 } else if (bs
->backing
) {
1806 BlockDriverState
*bs2
= bs
->backing
->bs
;
1807 int64_t nb_sectors2
= bdrv_nb_sectors(bs2
);
1808 if (nb_sectors2
>= 0 && sector_num
>= nb_sectors2
) {
1809 ret
|= BDRV_BLOCK_ZERO
;
1814 if (*file
&& *file
!= bs
&&
1815 (ret
& BDRV_BLOCK_DATA
) && !(ret
& BDRV_BLOCK_ZERO
) &&
1816 (ret
& BDRV_BLOCK_OFFSET_VALID
)) {
1817 BlockDriverState
*file2
;
1820 ret2
= bdrv_co_get_block_status(*file
, ret
>> BDRV_SECTOR_BITS
,
1821 *pnum
, &file_pnum
, &file2
);
1823 /* Ignore errors. This is just providing extra information, it
1824 * is useful but not necessary.
1827 /* !file_pnum indicates an offset at or beyond the EOF; it is
1828 * perfectly valid for the format block driver to point to such
1829 * offsets, so catch it and mark everything as zero */
1830 ret
|= BDRV_BLOCK_ZERO
;
1832 /* Limit request to the range reported by the protocol driver */
1834 ret
|= (ret2
& BDRV_BLOCK_ZERO
);
1840 bdrv_dec_in_flight(bs
);
1844 static int64_t coroutine_fn
bdrv_co_get_block_status_above(BlockDriverState
*bs
,
1845 BlockDriverState
*base
,
1849 BlockDriverState
**file
)
1851 BlockDriverState
*p
;
1855 for (p
= bs
; p
!= base
; p
= backing_bs(p
)) {
1856 ret
= bdrv_co_get_block_status(p
, sector_num
, nb_sectors
, pnum
, file
);
1857 if (ret
< 0 || ret
& BDRV_BLOCK_ALLOCATED
) {
1860 /* [sector_num, pnum] unallocated on this layer, which could be only
1861 * the first part of [sector_num, nb_sectors]. */
1862 nb_sectors
= MIN(nb_sectors
, *pnum
);
1867 /* Coroutine wrapper for bdrv_get_block_status_above() */
1868 static void coroutine_fn
bdrv_get_block_status_above_co_entry(void *opaque
)
1870 BdrvCoGetBlockStatusData
*data
= opaque
;
1872 data
->ret
= bdrv_co_get_block_status_above(data
->bs
, data
->base
,
1881 * Synchronous wrapper around bdrv_co_get_block_status_above().
1883 * See bdrv_co_get_block_status_above() for details.
1885 int64_t bdrv_get_block_status_above(BlockDriverState
*bs
,
1886 BlockDriverState
*base
,
1888 int nb_sectors
, int *pnum
,
1889 BlockDriverState
**file
)
1892 BdrvCoGetBlockStatusData data
= {
1896 .sector_num
= sector_num
,
1897 .nb_sectors
= nb_sectors
,
1902 if (qemu_in_coroutine()) {
1903 /* Fast-path if already in coroutine context */
1904 bdrv_get_block_status_above_co_entry(&data
);
1906 co
= qemu_coroutine_create(bdrv_get_block_status_above_co_entry
,
1908 bdrv_coroutine_enter(bs
, co
);
1909 BDRV_POLL_WHILE(bs
, !data
.done
);
1914 int64_t bdrv_get_block_status(BlockDriverState
*bs
,
1916 int nb_sectors
, int *pnum
,
1917 BlockDriverState
**file
)
1919 return bdrv_get_block_status_above(bs
, backing_bs(bs
),
1920 sector_num
, nb_sectors
, pnum
, file
);
1923 int coroutine_fn
bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1924 int nb_sectors
, int *pnum
)
1926 BlockDriverState
*file
;
1927 int64_t ret
= bdrv_get_block_status(bs
, sector_num
, nb_sectors
, pnum
,
1932 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
1936 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
1938 * Return true if the given sector is allocated in any image between
1939 * BASE and TOP (inclusive). BASE can be NULL to check if the given
1940 * sector is allocated in any image of the chain. Return false otherwise.
1942 * 'pnum' is set to the number of sectors (including and immediately following
1943 * the specified sector) that are known to be in the same
1944 * allocated/unallocated state.
1947 int bdrv_is_allocated_above(BlockDriverState
*top
,
1948 BlockDriverState
*base
,
1950 int nb_sectors
, int *pnum
)
1952 BlockDriverState
*intermediate
;
1953 int ret
, n
= nb_sectors
;
1956 while (intermediate
&& intermediate
!= base
) {
1958 ret
= bdrv_is_allocated(intermediate
, sector_num
, nb_sectors
,
1968 * [sector_num, nb_sectors] is unallocated on top but intermediate
1971 * [sector_num+x, nr_sectors] allocated.
1973 if (n
> pnum_inter
&&
1974 (intermediate
== top
||
1975 sector_num
+ pnum_inter
< intermediate
->total_sectors
)) {
1979 intermediate
= backing_bs(intermediate
);
1986 typedef struct BdrvVmstateCo
{
1987 BlockDriverState
*bs
;
1994 static int coroutine_fn
1995 bdrv_co_rw_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
,
1998 BlockDriver
*drv
= bs
->drv
;
2002 } else if (drv
->bdrv_load_vmstate
) {
2003 return is_read
? drv
->bdrv_load_vmstate(bs
, qiov
, pos
)
2004 : drv
->bdrv_save_vmstate(bs
, qiov
, pos
);
2005 } else if (bs
->file
) {
2006 return bdrv_co_rw_vmstate(bs
->file
->bs
, qiov
, pos
, is_read
);
2012 static void coroutine_fn
bdrv_co_rw_vmstate_entry(void *opaque
)
2014 BdrvVmstateCo
*co
= opaque
;
2015 co
->ret
= bdrv_co_rw_vmstate(co
->bs
, co
->qiov
, co
->pos
, co
->is_read
);
2019 bdrv_rw_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
,
2022 if (qemu_in_coroutine()) {
2023 return bdrv_co_rw_vmstate(bs
, qiov
, pos
, is_read
);
2025 BdrvVmstateCo data
= {
2030 .ret
= -EINPROGRESS
,
2032 Coroutine
*co
= qemu_coroutine_create(bdrv_co_rw_vmstate_entry
, &data
);
2034 bdrv_coroutine_enter(bs
, co
);
2035 while (data
.ret
== -EINPROGRESS
) {
2036 aio_poll(bdrv_get_aio_context(bs
), true);
2042 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2043 int64_t pos
, int size
)
2046 struct iovec iov
= {
2047 .iov_base
= (void *) buf
,
2052 qemu_iovec_init_external(&qiov
, &iov
, 1);
2054 ret
= bdrv_writev_vmstate(bs
, &qiov
, pos
);
2062 int bdrv_writev_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2064 return bdrv_rw_vmstate(bs
, qiov
, pos
, false);
2067 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2068 int64_t pos
, int size
)
2071 struct iovec iov
= {
2077 qemu_iovec_init_external(&qiov
, &iov
, 1);
2078 ret
= bdrv_readv_vmstate(bs
, &qiov
, pos
);
2086 int bdrv_readv_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2088 return bdrv_rw_vmstate(bs
, qiov
, pos
, true);
2091 /**************************************************************/
2094 BlockAIOCB
*bdrv_aio_readv(BdrvChild
*child
, int64_t sector_num
,
2095 QEMUIOVector
*qiov
, int nb_sectors
,
2096 BlockCompletionFunc
*cb
, void *opaque
)
2098 trace_bdrv_aio_readv(child
->bs
, sector_num
, nb_sectors
, opaque
);
2100 assert(nb_sectors
<< BDRV_SECTOR_BITS
== qiov
->size
);
2101 return bdrv_co_aio_prw_vector(child
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
2102 0, cb
, opaque
, false);
2105 BlockAIOCB
*bdrv_aio_writev(BdrvChild
*child
, int64_t sector_num
,
2106 QEMUIOVector
*qiov
, int nb_sectors
,
2107 BlockCompletionFunc
*cb
, void *opaque
)
2109 trace_bdrv_aio_writev(child
->bs
, sector_num
, nb_sectors
, opaque
);
2111 assert(nb_sectors
<< BDRV_SECTOR_BITS
== qiov
->size
);
2112 return bdrv_co_aio_prw_vector(child
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
2113 0, cb
, opaque
, true);
2116 void bdrv_aio_cancel(BlockAIOCB
*acb
)
2119 bdrv_aio_cancel_async(acb
);
2120 while (acb
->refcnt
> 1) {
2121 if (acb
->aiocb_info
->get_aio_context
) {
2122 aio_poll(acb
->aiocb_info
->get_aio_context(acb
), true);
2123 } else if (acb
->bs
) {
2124 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2125 * assert that we're not using an I/O thread. Thread-safe
2126 * code should use bdrv_aio_cancel_async exclusively.
2128 assert(bdrv_get_aio_context(acb
->bs
) == qemu_get_aio_context());
2129 aio_poll(bdrv_get_aio_context(acb
->bs
), true);
2134 qemu_aio_unref(acb
);
2137 /* Async version of aio cancel. The caller is not blocked if the acb implements
2138 * cancel_async, otherwise we do nothing and let the request normally complete.
2139 * In either case the completion callback must be called. */
2140 void bdrv_aio_cancel_async(BlockAIOCB
*acb
)
2142 if (acb
->aiocb_info
->cancel_async
) {
2143 acb
->aiocb_info
->cancel_async(acb
);
2147 /**************************************************************/
2148 /* async block device emulation */
2150 typedef struct BlockRequest
{
2152 /* Used during read, write, trim */
2159 /* Used during ioctl */
2165 BlockCompletionFunc
*cb
;
2171 typedef struct BlockAIOCBCoroutine
{
2178 } BlockAIOCBCoroutine
;
2180 static const AIOCBInfo bdrv_em_co_aiocb_info
= {
2181 .aiocb_size
= sizeof(BlockAIOCBCoroutine
),
2184 static void bdrv_co_complete(BlockAIOCBCoroutine
*acb
)
2186 if (!acb
->need_bh
) {
2187 bdrv_dec_in_flight(acb
->common
.bs
);
2188 acb
->common
.cb(acb
->common
.opaque
, acb
->req
.error
);
2189 qemu_aio_unref(acb
);
2193 static void bdrv_co_em_bh(void *opaque
)
2195 BlockAIOCBCoroutine
*acb
= opaque
;
2197 assert(!acb
->need_bh
);
2198 bdrv_co_complete(acb
);
2201 static void bdrv_co_maybe_schedule_bh(BlockAIOCBCoroutine
*acb
)
2203 acb
->need_bh
= false;
2204 if (acb
->req
.error
!= -EINPROGRESS
) {
2205 BlockDriverState
*bs
= acb
->common
.bs
;
2207 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
), bdrv_co_em_bh
, acb
);
2211 /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2212 static void coroutine_fn
bdrv_co_do_rw(void *opaque
)
2214 BlockAIOCBCoroutine
*acb
= opaque
;
2216 if (!acb
->is_write
) {
2217 acb
->req
.error
= bdrv_co_preadv(acb
->child
, acb
->req
.offset
,
2218 acb
->req
.qiov
->size
, acb
->req
.qiov
, acb
->req
.flags
);
2220 acb
->req
.error
= bdrv_co_pwritev(acb
->child
, acb
->req
.offset
,
2221 acb
->req
.qiov
->size
, acb
->req
.qiov
, acb
->req
.flags
);
2224 bdrv_co_complete(acb
);
2227 static BlockAIOCB
*bdrv_co_aio_prw_vector(BdrvChild
*child
,
2230 BdrvRequestFlags flags
,
2231 BlockCompletionFunc
*cb
,
2236 BlockAIOCBCoroutine
*acb
;
2238 /* Matched by bdrv_co_complete's bdrv_dec_in_flight. */
2239 bdrv_inc_in_flight(child
->bs
);
2241 acb
= qemu_aio_get(&bdrv_em_co_aiocb_info
, child
->bs
, cb
, opaque
);
2243 acb
->need_bh
= true;
2244 acb
->req
.error
= -EINPROGRESS
;
2245 acb
->req
.offset
= offset
;
2246 acb
->req
.qiov
= qiov
;
2247 acb
->req
.flags
= flags
;
2248 acb
->is_write
= is_write
;
2250 co
= qemu_coroutine_create(bdrv_co_do_rw
, acb
);
2251 bdrv_coroutine_enter(child
->bs
, co
);
2253 bdrv_co_maybe_schedule_bh(acb
);
2254 return &acb
->common
;
2257 static void coroutine_fn
bdrv_aio_flush_co_entry(void *opaque
)
2259 BlockAIOCBCoroutine
*acb
= opaque
;
2260 BlockDriverState
*bs
= acb
->common
.bs
;
2262 acb
->req
.error
= bdrv_co_flush(bs
);
2263 bdrv_co_complete(acb
);
2266 BlockAIOCB
*bdrv_aio_flush(BlockDriverState
*bs
,
2267 BlockCompletionFunc
*cb
, void *opaque
)
2269 trace_bdrv_aio_flush(bs
, opaque
);
2272 BlockAIOCBCoroutine
*acb
;
2274 /* Matched by bdrv_co_complete's bdrv_dec_in_flight. */
2275 bdrv_inc_in_flight(bs
);
2277 acb
= qemu_aio_get(&bdrv_em_co_aiocb_info
, bs
, cb
, opaque
);
2278 acb
->need_bh
= true;
2279 acb
->req
.error
= -EINPROGRESS
;
2281 co
= qemu_coroutine_create(bdrv_aio_flush_co_entry
, acb
);
2282 bdrv_coroutine_enter(bs
, co
);
2284 bdrv_co_maybe_schedule_bh(acb
);
2285 return &acb
->common
;
2288 /**************************************************************/
2289 /* Coroutine block device emulation */
2291 typedef struct FlushCo
{
2292 BlockDriverState
*bs
;
2297 static void coroutine_fn
bdrv_flush_co_entry(void *opaque
)
2299 FlushCo
*rwco
= opaque
;
2301 rwco
->ret
= bdrv_co_flush(rwco
->bs
);
2304 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2309 bdrv_inc_in_flight(bs
);
2311 if (!bs
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
) ||
2316 current_gen
= bs
->write_gen
;
2318 /* Wait until any previous flushes are completed */
2319 while (bs
->active_flush_req
) {
2320 qemu_co_queue_wait(&bs
->flush_queue
, NULL
);
2323 bs
->active_flush_req
= true;
2325 /* Write back all layers by calling one driver function */
2326 if (bs
->drv
->bdrv_co_flush
) {
2327 ret
= bs
->drv
->bdrv_co_flush(bs
);
2331 /* Write back cached data to the OS even with cache=unsafe */
2332 BLKDBG_EVENT(bs
->file
, BLKDBG_FLUSH_TO_OS
);
2333 if (bs
->drv
->bdrv_co_flush_to_os
) {
2334 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
2340 /* But don't actually force it to the disk with cache=unsafe */
2341 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2345 /* Check if we really need to flush anything */
2346 if (bs
->flushed_gen
== current_gen
) {
2350 BLKDBG_EVENT(bs
->file
, BLKDBG_FLUSH_TO_DISK
);
2351 if (bs
->drv
->bdrv_co_flush_to_disk
) {
2352 ret
= bs
->drv
->bdrv_co_flush_to_disk(bs
);
2353 } else if (bs
->drv
->bdrv_aio_flush
) {
2355 CoroutineIOCompletion co
= {
2356 .coroutine
= qemu_coroutine_self(),
2359 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
2363 qemu_coroutine_yield();
2368 * Some block drivers always operate in either writethrough or unsafe
2369 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2370 * know how the server works (because the behaviour is hardcoded or
2371 * depends on server-side configuration), so we can't ensure that
2372 * everything is safe on disk. Returning an error doesn't work because
2373 * that would break guests even if the server operates in writethrough
2376 * Let's hope the user knows what he's doing.
2385 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2386 * in the case of cache=unsafe, so there are no useless flushes.
2389 ret
= bs
->file
? bdrv_co_flush(bs
->file
->bs
) : 0;
2391 /* Notify any pending flushes that we have completed */
2393 bs
->flushed_gen
= current_gen
;
2395 bs
->active_flush_req
= false;
2396 /* Return value is ignored - it's ok if wait queue is empty */
2397 qemu_co_queue_next(&bs
->flush_queue
);
2400 bdrv_dec_in_flight(bs
);
2404 int bdrv_flush(BlockDriverState
*bs
)
2407 FlushCo flush_co
= {
2412 if (qemu_in_coroutine()) {
2413 /* Fast-path if already in coroutine context */
2414 bdrv_flush_co_entry(&flush_co
);
2416 co
= qemu_coroutine_create(bdrv_flush_co_entry
, &flush_co
);
2417 bdrv_coroutine_enter(bs
, co
);
2418 BDRV_POLL_WHILE(bs
, flush_co
.ret
== NOT_DONE
);
2421 return flush_co
.ret
;
2424 typedef struct DiscardCo
{
2425 BlockDriverState
*bs
;
2430 static void coroutine_fn
bdrv_pdiscard_co_entry(void *opaque
)
2432 DiscardCo
*rwco
= opaque
;
2434 rwco
->ret
= bdrv_co_pdiscard(rwco
->bs
, rwco
->offset
, rwco
->count
);
2437 int coroutine_fn
bdrv_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
2440 BdrvTrackedRequest req
;
2441 int max_pdiscard
, ret
;
2442 int head
, tail
, align
;
2448 ret
= bdrv_check_byte_request(bs
, offset
, count
);
2451 } else if (bs
->read_only
) {
2454 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
2456 /* Do nothing if disabled. */
2457 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
2461 if (!bs
->drv
->bdrv_co_pdiscard
&& !bs
->drv
->bdrv_aio_pdiscard
) {
2465 /* Discard is advisory, but some devices track and coalesce
2466 * unaligned requests, so we must pass everything down rather than
2467 * round here. Still, most devices will just silently ignore
2468 * unaligned requests (by returning -ENOTSUP), so we must fragment
2469 * the request accordingly. */
2470 align
= MAX(bs
->bl
.pdiscard_alignment
, bs
->bl
.request_alignment
);
2471 assert(align
% bs
->bl
.request_alignment
== 0);
2472 head
= offset
% align
;
2473 tail
= (offset
+ count
) % align
;
2475 bdrv_inc_in_flight(bs
);
2476 tracked_request_begin(&req
, bs
, offset
, count
, BDRV_TRACKED_DISCARD
);
2478 ret
= notifier_with_return_list_notify(&bs
->before_write_notifiers
, &req
);
2483 max_pdiscard
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_pdiscard
, INT_MAX
),
2485 assert(max_pdiscard
>= bs
->bl
.request_alignment
);
2492 /* Make small requests to get to alignment boundaries. */
2493 num
= MIN(count
, align
- head
);
2494 if (!QEMU_IS_ALIGNED(num
, bs
->bl
.request_alignment
)) {
2495 num
%= bs
->bl
.request_alignment
;
2497 head
= (head
+ num
) % align
;
2498 assert(num
< max_pdiscard
);
2501 /* Shorten the request to the last aligned cluster. */
2503 } else if (!QEMU_IS_ALIGNED(tail
, bs
->bl
.request_alignment
) &&
2504 tail
> bs
->bl
.request_alignment
) {
2505 tail
%= bs
->bl
.request_alignment
;
2509 /* limit request size */
2510 if (num
> max_pdiscard
) {
2514 if (bs
->drv
->bdrv_co_pdiscard
) {
2515 ret
= bs
->drv
->bdrv_co_pdiscard(bs
, offset
, num
);
2518 CoroutineIOCompletion co
= {
2519 .coroutine
= qemu_coroutine_self(),
2522 acb
= bs
->drv
->bdrv_aio_pdiscard(bs
, offset
, num
,
2523 bdrv_co_io_em_complete
, &co
);
2528 qemu_coroutine_yield();
2532 if (ret
&& ret
!= -ENOTSUP
) {
2542 bdrv_set_dirty(bs
, req
.offset
>> BDRV_SECTOR_BITS
,
2543 req
.bytes
>> BDRV_SECTOR_BITS
);
2544 tracked_request_end(&req
);
2545 bdrv_dec_in_flight(bs
);
2549 int bdrv_pdiscard(BlockDriverState
*bs
, int64_t offset
, int count
)
2559 if (qemu_in_coroutine()) {
2560 /* Fast-path if already in coroutine context */
2561 bdrv_pdiscard_co_entry(&rwco
);
2563 co
= qemu_coroutine_create(bdrv_pdiscard_co_entry
, &rwco
);
2564 bdrv_coroutine_enter(bs
, co
);
2565 BDRV_POLL_WHILE(bs
, rwco
.ret
== NOT_DONE
);
2571 int bdrv_co_ioctl(BlockDriverState
*bs
, int req
, void *buf
)
2573 BlockDriver
*drv
= bs
->drv
;
2574 CoroutineIOCompletion co
= {
2575 .coroutine
= qemu_coroutine_self(),
2579 bdrv_inc_in_flight(bs
);
2580 if (!drv
|| (!drv
->bdrv_aio_ioctl
&& !drv
->bdrv_co_ioctl
)) {
2585 if (drv
->bdrv_co_ioctl
) {
2586 co
.ret
= drv
->bdrv_co_ioctl(bs
, req
, buf
);
2588 acb
= drv
->bdrv_aio_ioctl(bs
, req
, buf
, bdrv_co_io_em_complete
, &co
);
2593 qemu_coroutine_yield();
2596 bdrv_dec_in_flight(bs
);
2600 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
2602 return qemu_memalign(bdrv_opt_mem_align(bs
), size
);
2605 void *qemu_blockalign0(BlockDriverState
*bs
, size_t size
)
2607 return memset(qemu_blockalign(bs
, size
), 0, size
);
2610 void *qemu_try_blockalign(BlockDriverState
*bs
, size_t size
)
2612 size_t align
= bdrv_opt_mem_align(bs
);
2614 /* Ensure that NULL is never returned on success */
2620 return qemu_try_memalign(align
, size
);
2623 void *qemu_try_blockalign0(BlockDriverState
*bs
, size_t size
)
2625 void *mem
= qemu_try_blockalign(bs
, size
);
2628 memset(mem
, 0, size
);
2635 * Check if all memory in this vector is sector aligned.
2637 bool bdrv_qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
2640 size_t alignment
= bdrv_min_mem_align(bs
);
2642 for (i
= 0; i
< qiov
->niov
; i
++) {
2643 if ((uintptr_t) qiov
->iov
[i
].iov_base
% alignment
) {
2646 if (qiov
->iov
[i
].iov_len
% alignment
) {
2654 void bdrv_add_before_write_notifier(BlockDriverState
*bs
,
2655 NotifierWithReturn
*notifier
)
2657 notifier_with_return_list_add(&bs
->before_write_notifiers
, notifier
);
2660 void bdrv_io_plug(BlockDriverState
*bs
)
2664 QLIST_FOREACH(child
, &bs
->children
, next
) {
2665 bdrv_io_plug(child
->bs
);
2668 if (bs
->io_plugged
++ == 0) {
2669 BlockDriver
*drv
= bs
->drv
;
2670 if (drv
&& drv
->bdrv_io_plug
) {
2671 drv
->bdrv_io_plug(bs
);
2676 void bdrv_io_unplug(BlockDriverState
*bs
)
2680 assert(bs
->io_plugged
);
2681 if (--bs
->io_plugged
== 0) {
2682 BlockDriver
*drv
= bs
->drv
;
2683 if (drv
&& drv
->bdrv_io_unplug
) {
2684 drv
->bdrv_io_unplug(bs
);
2688 QLIST_FOREACH(child
, &bs
->children
, next
) {
2689 bdrv_io_unplug(child
->bs
);