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 "block/throttle-groups.h"
31 #include "qemu/cutils.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
35 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
37 static BlockAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
38 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
39 BlockCompletionFunc
*cb
, void *opaque
);
40 static BlockAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
41 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
42 BlockCompletionFunc
*cb
, void *opaque
);
43 static int coroutine_fn
bdrv_co_readv_em(BlockDriverState
*bs
,
44 int64_t sector_num
, int nb_sectors
,
46 static int coroutine_fn
bdrv_co_writev_em(BlockDriverState
*bs
,
47 int64_t sector_num
, int nb_sectors
,
49 static BlockAIOCB
*bdrv_co_aio_rw_vector(BlockDriverState
*bs
,
53 BdrvRequestFlags flags
,
54 BlockCompletionFunc
*cb
,
57 static void coroutine_fn
bdrv_co_do_rw(void *opaque
);
58 static int coroutine_fn
bdrv_co_do_write_zeroes(BlockDriverState
*bs
,
59 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
);
61 /* throttling disk I/O limits */
62 void bdrv_set_io_limits(BlockDriverState
*bs
,
65 throttle_group_config(bs
, cfg
);
68 void bdrv_no_throttling_begin(BlockDriverState
*bs
)
70 if (bs
->io_limits_disabled
++ == 0) {
71 throttle_group_restart_bs(bs
);
75 void bdrv_no_throttling_end(BlockDriverState
*bs
)
77 assert(bs
->io_limits_disabled
);
78 --bs
->io_limits_disabled
;
81 void bdrv_io_limits_disable(BlockDriverState
*bs
)
83 assert(bs
->throttle_state
);
84 bdrv_no_throttling_begin(bs
);
85 throttle_group_unregister_bs(bs
);
86 bdrv_no_throttling_end(bs
);
89 /* should be called before bdrv_set_io_limits if a limit is set */
90 void bdrv_io_limits_enable(BlockDriverState
*bs
, const char *group
)
92 assert(!bs
->throttle_state
);
93 throttle_group_register_bs(bs
, group
);
96 void bdrv_io_limits_update_group(BlockDriverState
*bs
, const char *group
)
98 /* this bs is not part of any group */
99 if (!bs
->throttle_state
) {
103 /* this bs is a part of the same group than the one we want */
104 if (!g_strcmp0(throttle_group_get_name(bs
), group
)) {
108 /* need to change the group this bs belong to */
109 bdrv_io_limits_disable(bs
);
110 bdrv_io_limits_enable(bs
, group
);
113 void bdrv_setup_io_funcs(BlockDriver
*bdrv
)
115 /* Block drivers without coroutine functions need emulation */
116 if (!bdrv
->bdrv_co_readv
) {
117 bdrv
->bdrv_co_readv
= bdrv_co_readv_em
;
118 bdrv
->bdrv_co_writev
= bdrv_co_writev_em
;
120 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
121 * the block driver lacks aio we need to emulate that too.
123 if (!bdrv
->bdrv_aio_readv
) {
124 /* add AIO emulation layer */
125 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
126 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
131 void bdrv_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
133 BlockDriver
*drv
= bs
->drv
;
134 Error
*local_err
= NULL
;
136 memset(&bs
->bl
, 0, sizeof(bs
->bl
));
142 /* Take some limits from the children as a default */
144 bdrv_refresh_limits(bs
->file
->bs
, &local_err
);
146 error_propagate(errp
, local_err
);
149 bs
->bl
.opt_transfer_length
= bs
->file
->bs
->bl
.opt_transfer_length
;
150 bs
->bl
.max_transfer_length
= bs
->file
->bs
->bl
.max_transfer_length
;
151 bs
->bl
.min_mem_alignment
= bs
->file
->bs
->bl
.min_mem_alignment
;
152 bs
->bl
.opt_mem_alignment
= bs
->file
->bs
->bl
.opt_mem_alignment
;
153 bs
->bl
.max_iov
= bs
->file
->bs
->bl
.max_iov
;
155 bs
->bl
.min_mem_alignment
= 512;
156 bs
->bl
.opt_mem_alignment
= getpagesize();
158 /* Safe default since most protocols use readv()/writev()/etc */
159 bs
->bl
.max_iov
= IOV_MAX
;
163 bdrv_refresh_limits(bs
->backing
->bs
, &local_err
);
165 error_propagate(errp
, local_err
);
168 bs
->bl
.opt_transfer_length
=
169 MAX(bs
->bl
.opt_transfer_length
,
170 bs
->backing
->bs
->bl
.opt_transfer_length
);
171 bs
->bl
.max_transfer_length
=
172 MIN_NON_ZERO(bs
->bl
.max_transfer_length
,
173 bs
->backing
->bs
->bl
.max_transfer_length
);
174 bs
->bl
.opt_mem_alignment
=
175 MAX(bs
->bl
.opt_mem_alignment
,
176 bs
->backing
->bs
->bl
.opt_mem_alignment
);
177 bs
->bl
.min_mem_alignment
=
178 MAX(bs
->bl
.min_mem_alignment
,
179 bs
->backing
->bs
->bl
.min_mem_alignment
);
182 bs
->backing
->bs
->bl
.max_iov
);
185 /* Then let the driver override it */
186 if (drv
->bdrv_refresh_limits
) {
187 drv
->bdrv_refresh_limits(bs
, errp
);
192 * The copy-on-read flag is actually a reference count so multiple users may
193 * use the feature without worrying about clobbering its previous state.
194 * Copy-on-read stays enabled until all users have called to disable it.
196 void bdrv_enable_copy_on_read(BlockDriverState
*bs
)
201 void bdrv_disable_copy_on_read(BlockDriverState
*bs
)
203 assert(bs
->copy_on_read
> 0);
207 /* Check if any requests are in-flight (including throttled requests) */
208 bool bdrv_requests_pending(BlockDriverState
*bs
)
212 if (!QLIST_EMPTY(&bs
->tracked_requests
)) {
215 if (!qemu_co_queue_empty(&bs
->throttled_reqs
[0])) {
218 if (!qemu_co_queue_empty(&bs
->throttled_reqs
[1])) {
222 QLIST_FOREACH(child
, &bs
->children
, next
) {
223 if (bdrv_requests_pending(child
->bs
)) {
231 static void bdrv_drain_recurse(BlockDriverState
*bs
)
235 if (bs
->drv
&& bs
->drv
->bdrv_drain
) {
236 bs
->drv
->bdrv_drain(bs
);
238 QLIST_FOREACH(child
, &bs
->children
, next
) {
239 bdrv_drain_recurse(child
->bs
);
245 BlockDriverState
*bs
;
250 static void bdrv_drain_poll(BlockDriverState
*bs
)
256 busy
= bdrv_requests_pending(bs
);
257 busy
|= aio_poll(bdrv_get_aio_context(bs
), busy
);
261 static void bdrv_co_drain_bh_cb(void *opaque
)
263 BdrvCoDrainData
*data
= opaque
;
264 Coroutine
*co
= data
->co
;
266 qemu_bh_delete(data
->bh
);
267 bdrv_drain_poll(data
->bs
);
269 qemu_coroutine_enter(co
, NULL
);
272 static void coroutine_fn
bdrv_co_yield_to_drain(BlockDriverState
*bs
)
274 BdrvCoDrainData data
;
276 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
277 * other coroutines run if they were queued from
278 * qemu_co_queue_run_restart(). */
280 assert(qemu_in_coroutine());
281 data
= (BdrvCoDrainData
) {
282 .co
= qemu_coroutine_self(),
285 .bh
= aio_bh_new(bdrv_get_aio_context(bs
), bdrv_co_drain_bh_cb
, &data
),
287 qemu_bh_schedule(data
.bh
);
289 qemu_coroutine_yield();
290 /* If we are resumed from some other event (such as an aio completion or a
291 * timer callback), it is a bug in the caller that should be fixed. */
296 * Wait for pending requests to complete on a single BlockDriverState subtree,
297 * and suspend block driver's internal I/O until next request arrives.
299 * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
302 * Only this BlockDriverState's AioContext is run, so in-flight requests must
303 * not depend on events in other AioContexts. In that case, use
304 * bdrv_drain_all() instead.
306 void coroutine_fn
bdrv_co_drain(BlockDriverState
*bs
)
308 bdrv_no_throttling_begin(bs
);
309 bdrv_io_unplugged_begin(bs
);
310 bdrv_drain_recurse(bs
);
311 bdrv_co_yield_to_drain(bs
);
312 bdrv_io_unplugged_end(bs
);
313 bdrv_no_throttling_end(bs
);
316 void bdrv_drain(BlockDriverState
*bs
)
318 bdrv_no_throttling_begin(bs
);
319 bdrv_io_unplugged_begin(bs
);
320 bdrv_drain_recurse(bs
);
321 if (qemu_in_coroutine()) {
322 bdrv_co_yield_to_drain(bs
);
326 bdrv_io_unplugged_end(bs
);
327 bdrv_no_throttling_end(bs
);
331 * Wait for pending requests to complete across all BlockDriverStates
333 * This function does not flush data to disk, use bdrv_flush_all() for that
334 * after calling this function.
336 void bdrv_drain_all(void)
338 /* Always run first iteration so any pending completion BHs run */
340 BlockDriverState
*bs
= NULL
;
341 GSList
*aio_ctxs
= NULL
, *ctx
;
343 while ((bs
= bdrv_next(bs
))) {
344 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
346 aio_context_acquire(aio_context
);
348 block_job_pause(bs
->job
);
350 bdrv_no_throttling_begin(bs
);
351 bdrv_io_unplugged_begin(bs
);
352 bdrv_drain_recurse(bs
);
353 aio_context_release(aio_context
);
355 if (!g_slist_find(aio_ctxs
, aio_context
)) {
356 aio_ctxs
= g_slist_prepend(aio_ctxs
, aio_context
);
360 /* Note that completion of an asynchronous I/O operation can trigger any
361 * number of other I/O operations on other devices---for example a
362 * coroutine can submit an I/O request to another device in response to
363 * request completion. Therefore we must keep looping until there was no
364 * more activity rather than simply draining each device independently.
369 for (ctx
= aio_ctxs
; ctx
!= NULL
; ctx
= ctx
->next
) {
370 AioContext
*aio_context
= ctx
->data
;
373 aio_context_acquire(aio_context
);
374 while ((bs
= bdrv_next(bs
))) {
375 if (aio_context
== bdrv_get_aio_context(bs
)) {
376 if (bdrv_requests_pending(bs
)) {
378 aio_poll(aio_context
, busy
);
382 busy
|= aio_poll(aio_context
, false);
383 aio_context_release(aio_context
);
388 while ((bs
= bdrv_next(bs
))) {
389 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
391 aio_context_acquire(aio_context
);
392 bdrv_io_unplugged_end(bs
);
393 bdrv_no_throttling_end(bs
);
395 block_job_resume(bs
->job
);
397 aio_context_release(aio_context
);
399 g_slist_free(aio_ctxs
);
403 * Remove an active request from the tracked requests list
405 * This function should be called when a tracked request is completing.
407 static void tracked_request_end(BdrvTrackedRequest
*req
)
409 if (req
->serialising
) {
410 req
->bs
->serialising_in_flight
--;
413 QLIST_REMOVE(req
, list
);
414 qemu_co_queue_restart_all(&req
->wait_queue
);
418 * Add an active request to the tracked requests list
420 static void tracked_request_begin(BdrvTrackedRequest
*req
,
421 BlockDriverState
*bs
,
424 enum BdrvTrackedRequestType type
)
426 *req
= (BdrvTrackedRequest
){
431 .co
= qemu_coroutine_self(),
432 .serialising
= false,
433 .overlap_offset
= offset
,
434 .overlap_bytes
= bytes
,
437 qemu_co_queue_init(&req
->wait_queue
);
439 QLIST_INSERT_HEAD(&bs
->tracked_requests
, req
, list
);
442 static void mark_request_serialising(BdrvTrackedRequest
*req
, uint64_t align
)
444 int64_t overlap_offset
= req
->offset
& ~(align
- 1);
445 unsigned int overlap_bytes
= ROUND_UP(req
->offset
+ req
->bytes
, align
)
448 if (!req
->serialising
) {
449 req
->bs
->serialising_in_flight
++;
450 req
->serialising
= true;
453 req
->overlap_offset
= MIN(req
->overlap_offset
, overlap_offset
);
454 req
->overlap_bytes
= MAX(req
->overlap_bytes
, overlap_bytes
);
458 * Round a region to cluster boundaries
460 void bdrv_round_to_clusters(BlockDriverState
*bs
,
461 int64_t sector_num
, int nb_sectors
,
462 int64_t *cluster_sector_num
,
463 int *cluster_nb_sectors
)
467 if (bdrv_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
468 *cluster_sector_num
= sector_num
;
469 *cluster_nb_sectors
= nb_sectors
;
471 int64_t c
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
472 *cluster_sector_num
= QEMU_ALIGN_DOWN(sector_num
, c
);
473 *cluster_nb_sectors
= QEMU_ALIGN_UP(sector_num
- *cluster_sector_num
+
478 static int bdrv_get_cluster_size(BlockDriverState
*bs
)
483 ret
= bdrv_get_info(bs
, &bdi
);
484 if (ret
< 0 || bdi
.cluster_size
== 0) {
485 return bs
->request_alignment
;
487 return bdi
.cluster_size
;
491 static bool tracked_request_overlaps(BdrvTrackedRequest
*req
,
492 int64_t offset
, unsigned int bytes
)
495 if (offset
>= req
->overlap_offset
+ req
->overlap_bytes
) {
499 if (req
->overlap_offset
>= offset
+ bytes
) {
505 static bool coroutine_fn
wait_serialising_requests(BdrvTrackedRequest
*self
)
507 BlockDriverState
*bs
= self
->bs
;
508 BdrvTrackedRequest
*req
;
512 if (!bs
->serialising_in_flight
) {
518 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
519 if (req
== self
|| (!req
->serialising
&& !self
->serialising
)) {
522 if (tracked_request_overlaps(req
, self
->overlap_offset
,
523 self
->overlap_bytes
))
525 /* Hitting this means there was a reentrant request, for
526 * example, a block driver issuing nested requests. This must
527 * never happen since it means deadlock.
529 assert(qemu_coroutine_self() != req
->co
);
531 /* If the request is already (indirectly) waiting for us, or
532 * will wait for us as soon as it wakes up, then just go on
533 * (instead of producing a deadlock in the former case). */
534 if (!req
->waiting_for
) {
535 self
->waiting_for
= req
;
536 qemu_co_queue_wait(&req
->wait_queue
);
537 self
->waiting_for
= NULL
;
549 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
552 if (size
> BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
) {
556 if (!bdrv_is_inserted(bs
)) {
567 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
570 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
574 return bdrv_check_byte_request(bs
, sector_num
* BDRV_SECTOR_SIZE
,
575 nb_sectors
* BDRV_SECTOR_SIZE
);
578 typedef struct RwCo
{
579 BlockDriverState
*bs
;
584 BdrvRequestFlags flags
;
587 static void coroutine_fn
bdrv_rw_co_entry(void *opaque
)
591 if (!rwco
->is_write
) {
592 rwco
->ret
= bdrv_co_do_preadv(rwco
->bs
, rwco
->offset
,
593 rwco
->qiov
->size
, rwco
->qiov
,
596 rwco
->ret
= bdrv_co_do_pwritev(rwco
->bs
, rwco
->offset
,
597 rwco
->qiov
->size
, rwco
->qiov
,
603 * Process a vectored synchronous request using coroutines
605 static int bdrv_prwv_co(BlockDriverState
*bs
, int64_t offset
,
606 QEMUIOVector
*qiov
, bool is_write
,
607 BdrvRequestFlags flags
)
614 .is_write
= is_write
,
619 if (qemu_in_coroutine()) {
620 /* Fast-path if already in coroutine context */
621 bdrv_rw_co_entry(&rwco
);
623 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
625 co
= qemu_coroutine_create(bdrv_rw_co_entry
);
626 qemu_coroutine_enter(co
, &rwco
);
627 while (rwco
.ret
== NOT_DONE
) {
628 aio_poll(aio_context
, true);
635 * Process a synchronous request using coroutines
637 static int bdrv_rw_co(BlockDriverState
*bs
, int64_t sector_num
, uint8_t *buf
,
638 int nb_sectors
, bool is_write
, BdrvRequestFlags flags
)
642 .iov_base
= (void *)buf
,
643 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
646 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
650 qemu_iovec_init_external(&qiov
, &iov
, 1);
651 return bdrv_prwv_co(bs
, sector_num
<< BDRV_SECTOR_BITS
,
652 &qiov
, is_write
, flags
);
655 /* return < 0 if error. See bdrv_write() for the return codes */
656 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
657 uint8_t *buf
, int nb_sectors
)
659 return bdrv_rw_co(bs
, sector_num
, buf
, nb_sectors
, false, 0);
662 /* Return < 0 if error. Important errors are:
663 -EIO generic I/O error (may happen for all errors)
664 -ENOMEDIUM No media inserted.
665 -EINVAL Invalid sector number or nb_sectors
666 -EACCES Trying to write a read-only device
668 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
669 const uint8_t *buf
, int nb_sectors
)
671 return bdrv_rw_co(bs
, sector_num
, (uint8_t *)buf
, nb_sectors
, true, 0);
674 int bdrv_write_zeroes(BlockDriverState
*bs
, int64_t sector_num
,
675 int nb_sectors
, BdrvRequestFlags flags
)
677 return bdrv_rw_co(bs
, sector_num
, NULL
, nb_sectors
, true,
678 BDRV_REQ_ZERO_WRITE
| flags
);
682 * Completely zero out a block device with the help of bdrv_write_zeroes.
683 * The operation is sped up by checking the block status and only writing
684 * zeroes to the device if they currently do not return zeroes. Optional
685 * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP).
687 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
689 int bdrv_make_zero(BlockDriverState
*bs
, BdrvRequestFlags flags
)
691 int64_t target_sectors
, ret
, nb_sectors
, sector_num
= 0;
692 BlockDriverState
*file
;
695 target_sectors
= bdrv_nb_sectors(bs
);
696 if (target_sectors
< 0) {
697 return target_sectors
;
701 nb_sectors
= MIN(target_sectors
- sector_num
, BDRV_REQUEST_MAX_SECTORS
);
702 if (nb_sectors
<= 0) {
705 ret
= bdrv_get_block_status(bs
, sector_num
, nb_sectors
, &n
, &file
);
707 error_report("error getting block status at sector %" PRId64
": %s",
708 sector_num
, strerror(-ret
));
711 if (ret
& BDRV_BLOCK_ZERO
) {
715 ret
= bdrv_write_zeroes(bs
, sector_num
, n
, flags
);
717 error_report("error writing zeroes at sector %" PRId64
": %s",
718 sector_num
, strerror(-ret
));
725 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
, void *buf
, int bytes
)
729 .iov_base
= (void *)buf
,
738 qemu_iovec_init_external(&qiov
, &iov
, 1);
739 ret
= bdrv_prwv_co(bs
, offset
, &qiov
, false, 0);
747 int bdrv_pwritev(BlockDriverState
*bs
, int64_t offset
, QEMUIOVector
*qiov
)
751 ret
= bdrv_prwv_co(bs
, offset
, qiov
, true, 0);
759 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
760 const void *buf
, int bytes
)
764 .iov_base
= (void *) buf
,
772 qemu_iovec_init_external(&qiov
, &iov
, 1);
773 return bdrv_pwritev(bs
, offset
, &qiov
);
777 * Writes to the file and ensures that no writes are reordered across this
778 * request (acts as a barrier)
780 * Returns 0 on success, -errno in error cases.
782 int bdrv_pwrite_sync(BlockDriverState
*bs
, int64_t offset
,
783 const void *buf
, int count
)
787 ret
= bdrv_pwrite(bs
, offset
, buf
, count
);
792 ret
= bdrv_flush(bs
);
800 static int coroutine_fn
bdrv_driver_preadv(BlockDriverState
*bs
,
801 uint64_t offset
, uint64_t bytes
,
802 QEMUIOVector
*qiov
, int flags
)
804 BlockDriver
*drv
= bs
->drv
;
805 int64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
806 unsigned int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
808 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
809 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
810 assert((bytes
>> BDRV_SECTOR_BITS
) <= BDRV_REQUEST_MAX_SECTORS
);
812 return drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
815 static int coroutine_fn
bdrv_driver_pwritev(BlockDriverState
*bs
,
816 uint64_t offset
, uint64_t bytes
,
817 QEMUIOVector
*qiov
, int flags
)
819 BlockDriver
*drv
= bs
->drv
;
820 int64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
821 unsigned int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
824 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
825 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
826 assert((bytes
>> BDRV_SECTOR_BITS
) <= BDRV_REQUEST_MAX_SECTORS
);
828 if (drv
->bdrv_co_writev_flags
) {
829 ret
= drv
->bdrv_co_writev_flags(bs
, sector_num
, nb_sectors
, qiov
,
832 assert(drv
->supported_write_flags
== 0);
833 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
);
836 if (ret
== 0 && (flags
& BDRV_REQ_FUA
) &&
837 !(drv
->supported_write_flags
& BDRV_REQ_FUA
))
839 ret
= bdrv_co_flush(bs
);
845 static int coroutine_fn
bdrv_co_do_copy_on_readv(BlockDriverState
*bs
,
846 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
848 /* Perform I/O through a temporary buffer so that users who scribble over
849 * their read buffer while the operation is in progress do not end up
850 * modifying the image file. This is critical for zero-copy guest I/O
851 * where anything might happen inside guest memory.
855 BlockDriver
*drv
= bs
->drv
;
857 QEMUIOVector bounce_qiov
;
858 int64_t cluster_sector_num
;
859 int cluster_nb_sectors
;
863 /* Cover entire cluster so no additional backing file I/O is required when
864 * allocating cluster in the image file.
866 bdrv_round_to_clusters(bs
, sector_num
, nb_sectors
,
867 &cluster_sector_num
, &cluster_nb_sectors
);
869 trace_bdrv_co_do_copy_on_readv(bs
, sector_num
, nb_sectors
,
870 cluster_sector_num
, cluster_nb_sectors
);
872 iov
.iov_len
= cluster_nb_sectors
* BDRV_SECTOR_SIZE
;
873 iov
.iov_base
= bounce_buffer
= qemu_try_blockalign(bs
, iov
.iov_len
);
874 if (bounce_buffer
== NULL
) {
879 qemu_iovec_init_external(&bounce_qiov
, &iov
, 1);
881 ret
= bdrv_driver_preadv(bs
, cluster_sector_num
* BDRV_SECTOR_SIZE
,
882 cluster_nb_sectors
* BDRV_SECTOR_SIZE
,
888 if (drv
->bdrv_co_write_zeroes
&&
889 buffer_is_zero(bounce_buffer
, iov
.iov_len
)) {
890 ret
= bdrv_co_do_write_zeroes(bs
, cluster_sector_num
,
891 cluster_nb_sectors
, 0);
893 /* This does not change the data on the disk, it is not necessary
894 * to flush even in cache=writethrough mode.
896 ret
= bdrv_driver_pwritev(bs
, cluster_sector_num
* BDRV_SECTOR_SIZE
,
897 cluster_nb_sectors
* BDRV_SECTOR_SIZE
,
902 /* It might be okay to ignore write errors for guest requests. If this
903 * is a deliberate copy-on-read then we don't want to ignore the error.
904 * Simply report it in all cases.
909 skip_bytes
= (sector_num
- cluster_sector_num
) * BDRV_SECTOR_SIZE
;
910 qemu_iovec_from_buf(qiov
, 0, bounce_buffer
+ skip_bytes
,
911 nb_sectors
* BDRV_SECTOR_SIZE
);
914 qemu_vfree(bounce_buffer
);
919 * Forwards an already correctly aligned request to the BlockDriver. This
920 * handles copy on read and zeroing after EOF; any other features must be
921 * implemented by the caller.
923 static int coroutine_fn
bdrv_aligned_preadv(BlockDriverState
*bs
,
924 BdrvTrackedRequest
*req
, int64_t offset
, unsigned int bytes
,
925 int64_t align
, QEMUIOVector
*qiov
, int flags
)
929 int64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
930 unsigned int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
932 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
933 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
934 assert(!qiov
|| bytes
== qiov
->size
);
935 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
937 /* Handle Copy on Read and associated serialisation */
938 if (flags
& BDRV_REQ_COPY_ON_READ
) {
939 /* If we touch the same cluster it counts as an overlap. This
940 * guarantees that allocating writes will be serialized and not race
941 * with each other for the same cluster. For example, in copy-on-read
942 * it ensures that the CoR read and write operations are atomic and
943 * guest writes cannot interleave between them. */
944 mark_request_serialising(req
, bdrv_get_cluster_size(bs
));
947 if (!(flags
& BDRV_REQ_NO_SERIALISING
)) {
948 wait_serialising_requests(req
);
951 if (flags
& BDRV_REQ_COPY_ON_READ
) {
954 ret
= bdrv_is_allocated(bs
, sector_num
, nb_sectors
, &pnum
);
959 if (!ret
|| pnum
!= nb_sectors
) {
960 ret
= bdrv_co_do_copy_on_readv(bs
, sector_num
, nb_sectors
, qiov
);
965 /* Forward the request to the BlockDriver */
966 if (!bs
->zero_beyond_eof
) {
967 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, 0);
969 /* Read zeros after EOF */
970 int64_t total_sectors
, max_nb_sectors
;
972 total_sectors
= bdrv_nb_sectors(bs
);
973 if (total_sectors
< 0) {
978 max_nb_sectors
= ROUND_UP(MAX(0, total_sectors
- sector_num
),
979 align
>> BDRV_SECTOR_BITS
);
980 if (nb_sectors
< max_nb_sectors
) {
981 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, 0);
982 } else if (max_nb_sectors
> 0) {
983 QEMUIOVector local_qiov
;
985 qemu_iovec_init(&local_qiov
, qiov
->niov
);
986 qemu_iovec_concat(&local_qiov
, qiov
, 0,
987 max_nb_sectors
* BDRV_SECTOR_SIZE
);
989 ret
= bdrv_driver_preadv(bs
, offset
,
990 max_nb_sectors
* BDRV_SECTOR_SIZE
,
993 qemu_iovec_destroy(&local_qiov
);
998 /* Reading beyond end of file is supposed to produce zeroes */
999 if (ret
== 0 && total_sectors
< sector_num
+ nb_sectors
) {
1000 uint64_t offset
= MAX(0, total_sectors
- sector_num
);
1001 uint64_t bytes
= (sector_num
+ nb_sectors
- offset
) *
1003 qemu_iovec_memset(qiov
, offset
* BDRV_SECTOR_SIZE
, 0, bytes
);
1012 * Handle a read request in coroutine context
1014 int coroutine_fn
bdrv_co_do_preadv(BlockDriverState
*bs
,
1015 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1016 BdrvRequestFlags flags
)
1018 BlockDriver
*drv
= bs
->drv
;
1019 BdrvTrackedRequest req
;
1021 /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
1022 uint64_t align
= MAX(BDRV_SECTOR_SIZE
, bs
->request_alignment
);
1023 uint8_t *head_buf
= NULL
;
1024 uint8_t *tail_buf
= NULL
;
1025 QEMUIOVector local_qiov
;
1026 bool use_local_qiov
= false;
1033 ret
= bdrv_check_byte_request(bs
, offset
, bytes
);
1038 /* Don't do copy-on-read if we read data before write operation */
1039 if (bs
->copy_on_read
&& !(flags
& BDRV_REQ_NO_SERIALISING
)) {
1040 flags
|= BDRV_REQ_COPY_ON_READ
;
1043 /* throttling disk I/O */
1044 if (bs
->throttle_state
) {
1045 throttle_group_co_io_limits_intercept(bs
, bytes
, false);
1048 /* Align read if necessary by padding qiov */
1049 if (offset
& (align
- 1)) {
1050 head_buf
= qemu_blockalign(bs
, align
);
1051 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 2);
1052 qemu_iovec_add(&local_qiov
, head_buf
, offset
& (align
- 1));
1053 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1054 use_local_qiov
= true;
1056 bytes
+= offset
& (align
- 1);
1057 offset
= offset
& ~(align
- 1);
1060 if ((offset
+ bytes
) & (align
- 1)) {
1061 if (!use_local_qiov
) {
1062 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 1);
1063 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1064 use_local_qiov
= true;
1066 tail_buf
= qemu_blockalign(bs
, align
);
1067 qemu_iovec_add(&local_qiov
, tail_buf
,
1068 align
- ((offset
+ bytes
) & (align
- 1)));
1070 bytes
= ROUND_UP(bytes
, align
);
1073 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_READ
);
1074 ret
= bdrv_aligned_preadv(bs
, &req
, offset
, bytes
, align
,
1075 use_local_qiov
? &local_qiov
: qiov
,
1077 tracked_request_end(&req
);
1079 if (use_local_qiov
) {
1080 qemu_iovec_destroy(&local_qiov
);
1081 qemu_vfree(head_buf
);
1082 qemu_vfree(tail_buf
);
1088 static int coroutine_fn
bdrv_co_do_readv(BlockDriverState
*bs
,
1089 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
,
1090 BdrvRequestFlags flags
)
1092 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
1096 return bdrv_co_do_preadv(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1097 nb_sectors
<< BDRV_SECTOR_BITS
, qiov
, flags
);
1100 int coroutine_fn
bdrv_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1101 int nb_sectors
, QEMUIOVector
*qiov
)
1103 trace_bdrv_co_readv(bs
, sector_num
, nb_sectors
);
1105 return bdrv_co_do_readv(bs
, sector_num
, nb_sectors
, qiov
, 0);
1108 int coroutine_fn
bdrv_co_readv_no_serialising(BlockDriverState
*bs
,
1109 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
1111 trace_bdrv_co_readv_no_serialising(bs
, sector_num
, nb_sectors
);
1113 return bdrv_co_do_readv(bs
, sector_num
, nb_sectors
, qiov
,
1114 BDRV_REQ_NO_SERIALISING
);
1117 int coroutine_fn
bdrv_co_copy_on_readv(BlockDriverState
*bs
,
1118 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
1120 trace_bdrv_co_copy_on_readv(bs
, sector_num
, nb_sectors
);
1122 return bdrv_co_do_readv(bs
, sector_num
, nb_sectors
, qiov
,
1123 BDRV_REQ_COPY_ON_READ
);
1126 #define MAX_WRITE_ZEROES_BOUNCE_BUFFER 32768
1128 static int coroutine_fn
bdrv_co_do_write_zeroes(BlockDriverState
*bs
,
1129 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
1131 BlockDriver
*drv
= bs
->drv
;
1133 struct iovec iov
= {0};
1136 int max_write_zeroes
= MIN_NON_ZERO(bs
->bl
.max_write_zeroes
,
1137 BDRV_REQUEST_MAX_SECTORS
);
1139 while (nb_sectors
> 0 && !ret
) {
1140 int num
= nb_sectors
;
1142 /* Align request. Block drivers can expect the "bulk" of the request
1145 if (bs
->bl
.write_zeroes_alignment
1146 && num
> bs
->bl
.write_zeroes_alignment
) {
1147 if (sector_num
% bs
->bl
.write_zeroes_alignment
!= 0) {
1148 /* Make a small request up to the first aligned sector. */
1149 num
= bs
->bl
.write_zeroes_alignment
;
1150 num
-= sector_num
% bs
->bl
.write_zeroes_alignment
;
1151 } else if ((sector_num
+ num
) % bs
->bl
.write_zeroes_alignment
!= 0) {
1152 /* Shorten the request to the last aligned sector. num cannot
1153 * underflow because num > bs->bl.write_zeroes_alignment.
1155 num
-= (sector_num
+ num
) % bs
->bl
.write_zeroes_alignment
;
1159 /* limit request size */
1160 if (num
> max_write_zeroes
) {
1161 num
= max_write_zeroes
;
1165 /* First try the efficient write zeroes operation */
1166 if (drv
->bdrv_co_write_zeroes
) {
1167 ret
= drv
->bdrv_co_write_zeroes(bs
, sector_num
, num
, flags
);
1170 if (ret
== -ENOTSUP
) {
1171 /* Fall back to bounce buffer if write zeroes is unsupported */
1172 int max_xfer_len
= MIN_NON_ZERO(bs
->bl
.max_transfer_length
,
1173 MAX_WRITE_ZEROES_BOUNCE_BUFFER
);
1174 num
= MIN(num
, max_xfer_len
);
1175 iov
.iov_len
= num
* BDRV_SECTOR_SIZE
;
1176 if (iov
.iov_base
== NULL
) {
1177 iov
.iov_base
= qemu_try_blockalign(bs
, num
* BDRV_SECTOR_SIZE
);
1178 if (iov
.iov_base
== NULL
) {
1182 memset(iov
.iov_base
, 0, num
* BDRV_SECTOR_SIZE
);
1184 qemu_iovec_init_external(&qiov
, &iov
, 1);
1186 ret
= bdrv_driver_pwritev(bs
, sector_num
* BDRV_SECTOR_SIZE
,
1187 num
* BDRV_SECTOR_SIZE
, &qiov
, 0);
1189 /* Keep bounce buffer around if it is big enough for all
1190 * all future requests.
1192 if (num
< max_xfer_len
) {
1193 qemu_vfree(iov
.iov_base
);
1194 iov
.iov_base
= NULL
;
1203 qemu_vfree(iov
.iov_base
);
1208 * Forwards an already correctly aligned write request to the BlockDriver.
1210 static int coroutine_fn
bdrv_aligned_pwritev(BlockDriverState
*bs
,
1211 BdrvTrackedRequest
*req
, int64_t offset
, unsigned int bytes
,
1212 QEMUIOVector
*qiov
, int flags
)
1214 BlockDriver
*drv
= bs
->drv
;
1218 int64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
1219 unsigned int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1221 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
1222 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
1223 assert(!qiov
|| bytes
== qiov
->size
);
1224 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1226 waited
= wait_serialising_requests(req
);
1227 assert(!waited
|| !req
->serialising
);
1228 assert(req
->overlap_offset
<= offset
);
1229 assert(offset
+ bytes
<= req
->overlap_offset
+ req
->overlap_bytes
);
1231 ret
= notifier_with_return_list_notify(&bs
->before_write_notifiers
, req
);
1233 if (!ret
&& bs
->detect_zeroes
!= BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
&&
1234 !(flags
& BDRV_REQ_ZERO_WRITE
) && drv
->bdrv_co_write_zeroes
&&
1235 qemu_iovec_is_zero(qiov
)) {
1236 flags
|= BDRV_REQ_ZERO_WRITE
;
1237 if (bs
->detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
) {
1238 flags
|= BDRV_REQ_MAY_UNMAP
;
1243 /* Do nothing, write notifier decided to fail this request */
1244 } else if (flags
& BDRV_REQ_ZERO_WRITE
) {
1245 bdrv_debug_event(bs
, BLKDBG_PWRITEV_ZERO
);
1246 ret
= bdrv_co_do_write_zeroes(bs
, sector_num
, nb_sectors
, flags
);
1248 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
1249 ret
= bdrv_driver_pwritev(bs
, offset
, bytes
, qiov
, flags
);
1251 bdrv_debug_event(bs
, BLKDBG_PWRITEV_DONE
);
1253 bdrv_set_dirty(bs
, sector_num
, nb_sectors
);
1255 if (bs
->wr_highest_offset
< offset
+ bytes
) {
1256 bs
->wr_highest_offset
= offset
+ bytes
;
1260 bs
->total_sectors
= MAX(bs
->total_sectors
, sector_num
+ nb_sectors
);
1266 static int coroutine_fn
bdrv_co_do_zero_pwritev(BlockDriverState
*bs
,
1269 BdrvRequestFlags flags
,
1270 BdrvTrackedRequest
*req
)
1272 uint8_t *buf
= NULL
;
1273 QEMUIOVector local_qiov
;
1275 uint64_t align
= MAX(BDRV_SECTOR_SIZE
, bs
->request_alignment
);
1276 unsigned int head_padding_bytes
, tail_padding_bytes
;
1279 head_padding_bytes
= offset
& (align
- 1);
1280 tail_padding_bytes
= align
- ((offset
+ bytes
) & (align
- 1));
1283 assert(flags
& BDRV_REQ_ZERO_WRITE
);
1284 if (head_padding_bytes
|| tail_padding_bytes
) {
1285 buf
= qemu_blockalign(bs
, align
);
1286 iov
= (struct iovec
) {
1290 qemu_iovec_init_external(&local_qiov
, &iov
, 1);
1292 if (head_padding_bytes
) {
1293 uint64_t zero_bytes
= MIN(bytes
, align
- head_padding_bytes
);
1295 /* RMW the unaligned part before head. */
1296 mark_request_serialising(req
, align
);
1297 wait_serialising_requests(req
);
1298 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1299 ret
= bdrv_aligned_preadv(bs
, req
, offset
& ~(align
- 1), align
,
1300 align
, &local_qiov
, 0);
1304 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1306 memset(buf
+ head_padding_bytes
, 0, zero_bytes
);
1307 ret
= bdrv_aligned_pwritev(bs
, req
, offset
& ~(align
- 1), align
,
1309 flags
& ~BDRV_REQ_ZERO_WRITE
);
1313 offset
+= zero_bytes
;
1314 bytes
-= zero_bytes
;
1317 assert(!bytes
|| (offset
& (align
- 1)) == 0);
1318 if (bytes
>= align
) {
1319 /* Write the aligned part in the middle. */
1320 uint64_t aligned_bytes
= bytes
& ~(align
- 1);
1321 ret
= bdrv_aligned_pwritev(bs
, req
, offset
, aligned_bytes
,
1326 bytes
-= aligned_bytes
;
1327 offset
+= aligned_bytes
;
1330 assert(!bytes
|| (offset
& (align
- 1)) == 0);
1332 assert(align
== tail_padding_bytes
+ bytes
);
1333 /* RMW the unaligned part after tail. */
1334 mark_request_serialising(req
, align
);
1335 wait_serialising_requests(req
);
1336 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1337 ret
= bdrv_aligned_preadv(bs
, req
, offset
, align
,
1338 align
, &local_qiov
, 0);
1342 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1344 memset(buf
, 0, bytes
);
1345 ret
= bdrv_aligned_pwritev(bs
, req
, offset
, align
,
1346 &local_qiov
, flags
& ~BDRV_REQ_ZERO_WRITE
);
1355 * Handle a write request in coroutine context
1357 int coroutine_fn
bdrv_co_do_pwritev(BlockDriverState
*bs
,
1358 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1359 BdrvRequestFlags flags
)
1361 BdrvTrackedRequest req
;
1362 /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
1363 uint64_t align
= MAX(BDRV_SECTOR_SIZE
, bs
->request_alignment
);
1364 uint8_t *head_buf
= NULL
;
1365 uint8_t *tail_buf
= NULL
;
1366 QEMUIOVector local_qiov
;
1367 bool use_local_qiov
= false;
1373 if (bs
->read_only
) {
1376 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
1378 ret
= bdrv_check_byte_request(bs
, offset
, bytes
);
1383 /* throttling disk I/O */
1384 if (bs
->throttle_state
) {
1385 throttle_group_co_io_limits_intercept(bs
, bytes
, true);
1389 * Align write if necessary by performing a read-modify-write cycle.
1390 * Pad qiov with the read parts and be sure to have a tracked request not
1391 * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
1393 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_WRITE
);
1396 ret
= bdrv_co_do_zero_pwritev(bs
, offset
, bytes
, flags
, &req
);
1400 if (offset
& (align
- 1)) {
1401 QEMUIOVector head_qiov
;
1402 struct iovec head_iov
;
1404 mark_request_serialising(&req
, align
);
1405 wait_serialising_requests(&req
);
1407 head_buf
= qemu_blockalign(bs
, align
);
1408 head_iov
= (struct iovec
) {
1409 .iov_base
= head_buf
,
1412 qemu_iovec_init_external(&head_qiov
, &head_iov
, 1);
1414 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1415 ret
= bdrv_aligned_preadv(bs
, &req
, offset
& ~(align
- 1), align
,
1416 align
, &head_qiov
, 0);
1420 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1422 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 2);
1423 qemu_iovec_add(&local_qiov
, head_buf
, offset
& (align
- 1));
1424 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1425 use_local_qiov
= true;
1427 bytes
+= offset
& (align
- 1);
1428 offset
= offset
& ~(align
- 1);
1431 if ((offset
+ bytes
) & (align
- 1)) {
1432 QEMUIOVector tail_qiov
;
1433 struct iovec tail_iov
;
1437 mark_request_serialising(&req
, align
);
1438 waited
= wait_serialising_requests(&req
);
1439 assert(!waited
|| !use_local_qiov
);
1441 tail_buf
= qemu_blockalign(bs
, align
);
1442 tail_iov
= (struct iovec
) {
1443 .iov_base
= tail_buf
,
1446 qemu_iovec_init_external(&tail_qiov
, &tail_iov
, 1);
1448 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1449 ret
= bdrv_aligned_preadv(bs
, &req
, (offset
+ bytes
) & ~(align
- 1), align
,
1450 align
, &tail_qiov
, 0);
1454 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1456 if (!use_local_qiov
) {
1457 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 1);
1458 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1459 use_local_qiov
= true;
1462 tail_bytes
= (offset
+ bytes
) & (align
- 1);
1463 qemu_iovec_add(&local_qiov
, tail_buf
+ tail_bytes
, align
- tail_bytes
);
1465 bytes
= ROUND_UP(bytes
, align
);
1468 ret
= bdrv_aligned_pwritev(bs
, &req
, offset
, bytes
,
1469 use_local_qiov
? &local_qiov
: qiov
,
1474 if (use_local_qiov
) {
1475 qemu_iovec_destroy(&local_qiov
);
1477 qemu_vfree(head_buf
);
1478 qemu_vfree(tail_buf
);
1480 tracked_request_end(&req
);
1484 static int coroutine_fn
bdrv_co_do_writev(BlockDriverState
*bs
,
1485 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
,
1486 BdrvRequestFlags flags
)
1488 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
1492 return bdrv_co_do_pwritev(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1493 nb_sectors
<< BDRV_SECTOR_BITS
, qiov
, flags
);
1496 int coroutine_fn
bdrv_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1497 int nb_sectors
, QEMUIOVector
*qiov
)
1499 trace_bdrv_co_writev(bs
, sector_num
, nb_sectors
);
1501 return bdrv_co_do_writev(bs
, sector_num
, nb_sectors
, qiov
, 0);
1504 int coroutine_fn
bdrv_co_write_zeroes(BlockDriverState
*bs
,
1505 int64_t sector_num
, int nb_sectors
,
1506 BdrvRequestFlags flags
)
1508 trace_bdrv_co_write_zeroes(bs
, sector_num
, nb_sectors
, flags
);
1510 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
1511 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1514 return bdrv_co_do_writev(bs
, sector_num
, nb_sectors
, NULL
,
1515 BDRV_REQ_ZERO_WRITE
| flags
);
1518 typedef struct BdrvCoGetBlockStatusData
{
1519 BlockDriverState
*bs
;
1520 BlockDriverState
*base
;
1521 BlockDriverState
**file
;
1527 } BdrvCoGetBlockStatusData
;
1530 * Returns the allocation status of the specified sectors.
1531 * Drivers not implementing the functionality are assumed to not support
1532 * backing files, hence all their sectors are reported as allocated.
1534 * If 'sector_num' is beyond the end of the disk image the return value is 0
1535 * and 'pnum' is set to 0.
1537 * 'pnum' is set to the number of sectors (including and immediately following
1538 * the specified sector) that are known to be in the same
1539 * allocated/unallocated state.
1541 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1542 * beyond the end of the disk image it will be clamped.
1544 * If returned value is positive and BDRV_BLOCK_OFFSET_VALID bit is set, 'file'
1545 * points to the BDS which the sector range is allocated in.
1547 static int64_t coroutine_fn
bdrv_co_get_block_status(BlockDriverState
*bs
,
1549 int nb_sectors
, int *pnum
,
1550 BlockDriverState
**file
)
1552 int64_t total_sectors
;
1556 total_sectors
= bdrv_nb_sectors(bs
);
1557 if (total_sectors
< 0) {
1558 return total_sectors
;
1561 if (sector_num
>= total_sectors
) {
1566 n
= total_sectors
- sector_num
;
1567 if (n
< nb_sectors
) {
1571 if (!bs
->drv
->bdrv_co_get_block_status
) {
1573 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
;
1574 if (bs
->drv
->protocol_name
) {
1575 ret
|= BDRV_BLOCK_OFFSET_VALID
| (sector_num
* BDRV_SECTOR_SIZE
);
1581 ret
= bs
->drv
->bdrv_co_get_block_status(bs
, sector_num
, nb_sectors
, pnum
,
1588 if (ret
& BDRV_BLOCK_RAW
) {
1589 assert(ret
& BDRV_BLOCK_OFFSET_VALID
);
1590 return bdrv_get_block_status(bs
->file
->bs
, ret
>> BDRV_SECTOR_BITS
,
1594 if (ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ZERO
)) {
1595 ret
|= BDRV_BLOCK_ALLOCATED
;
1597 if (bdrv_unallocated_blocks_are_zero(bs
)) {
1598 ret
|= BDRV_BLOCK_ZERO
;
1599 } else if (bs
->backing
) {
1600 BlockDriverState
*bs2
= bs
->backing
->bs
;
1601 int64_t nb_sectors2
= bdrv_nb_sectors(bs2
);
1602 if (nb_sectors2
>= 0 && sector_num
>= nb_sectors2
) {
1603 ret
|= BDRV_BLOCK_ZERO
;
1608 if (*file
&& *file
!= bs
&&
1609 (ret
& BDRV_BLOCK_DATA
) && !(ret
& BDRV_BLOCK_ZERO
) &&
1610 (ret
& BDRV_BLOCK_OFFSET_VALID
)) {
1611 BlockDriverState
*file2
;
1614 ret2
= bdrv_co_get_block_status(*file
, ret
>> BDRV_SECTOR_BITS
,
1615 *pnum
, &file_pnum
, &file2
);
1617 /* Ignore errors. This is just providing extra information, it
1618 * is useful but not necessary.
1621 /* !file_pnum indicates an offset at or beyond the EOF; it is
1622 * perfectly valid for the format block driver to point to such
1623 * offsets, so catch it and mark everything as zero */
1624 ret
|= BDRV_BLOCK_ZERO
;
1626 /* Limit request to the range reported by the protocol driver */
1628 ret
|= (ret2
& BDRV_BLOCK_ZERO
);
1636 static int64_t coroutine_fn
bdrv_co_get_block_status_above(BlockDriverState
*bs
,
1637 BlockDriverState
*base
,
1641 BlockDriverState
**file
)
1643 BlockDriverState
*p
;
1647 for (p
= bs
; p
!= base
; p
= backing_bs(p
)) {
1648 ret
= bdrv_co_get_block_status(p
, sector_num
, nb_sectors
, pnum
, file
);
1649 if (ret
< 0 || ret
& BDRV_BLOCK_ALLOCATED
) {
1652 /* [sector_num, pnum] unallocated on this layer, which could be only
1653 * the first part of [sector_num, nb_sectors]. */
1654 nb_sectors
= MIN(nb_sectors
, *pnum
);
1659 /* Coroutine wrapper for bdrv_get_block_status_above() */
1660 static void coroutine_fn
bdrv_get_block_status_above_co_entry(void *opaque
)
1662 BdrvCoGetBlockStatusData
*data
= opaque
;
1664 data
->ret
= bdrv_co_get_block_status_above(data
->bs
, data
->base
,
1673 * Synchronous wrapper around bdrv_co_get_block_status_above().
1675 * See bdrv_co_get_block_status_above() for details.
1677 int64_t bdrv_get_block_status_above(BlockDriverState
*bs
,
1678 BlockDriverState
*base
,
1680 int nb_sectors
, int *pnum
,
1681 BlockDriverState
**file
)
1684 BdrvCoGetBlockStatusData data
= {
1688 .sector_num
= sector_num
,
1689 .nb_sectors
= nb_sectors
,
1694 if (qemu_in_coroutine()) {
1695 /* Fast-path if already in coroutine context */
1696 bdrv_get_block_status_above_co_entry(&data
);
1698 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
1700 co
= qemu_coroutine_create(bdrv_get_block_status_above_co_entry
);
1701 qemu_coroutine_enter(co
, &data
);
1702 while (!data
.done
) {
1703 aio_poll(aio_context
, true);
1709 int64_t bdrv_get_block_status(BlockDriverState
*bs
,
1711 int nb_sectors
, int *pnum
,
1712 BlockDriverState
**file
)
1714 return bdrv_get_block_status_above(bs
, backing_bs(bs
),
1715 sector_num
, nb_sectors
, pnum
, file
);
1718 int coroutine_fn
bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1719 int nb_sectors
, int *pnum
)
1721 BlockDriverState
*file
;
1722 int64_t ret
= bdrv_get_block_status(bs
, sector_num
, nb_sectors
, pnum
,
1727 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
1731 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
1733 * Return true if the given sector is allocated in any image between
1734 * BASE and TOP (inclusive). BASE can be NULL to check if the given
1735 * sector is allocated in any image of the chain. Return false otherwise.
1737 * 'pnum' is set to the number of sectors (including and immediately following
1738 * the specified sector) that are known to be in the same
1739 * allocated/unallocated state.
1742 int bdrv_is_allocated_above(BlockDriverState
*top
,
1743 BlockDriverState
*base
,
1745 int nb_sectors
, int *pnum
)
1747 BlockDriverState
*intermediate
;
1748 int ret
, n
= nb_sectors
;
1751 while (intermediate
&& intermediate
!= base
) {
1753 ret
= bdrv_is_allocated(intermediate
, sector_num
, nb_sectors
,
1763 * [sector_num, nb_sectors] is unallocated on top but intermediate
1766 * [sector_num+x, nr_sectors] allocated.
1768 if (n
> pnum_inter
&&
1769 (intermediate
== top
||
1770 sector_num
+ pnum_inter
< intermediate
->total_sectors
)) {
1774 intermediate
= backing_bs(intermediate
);
1781 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1782 const uint8_t *buf
, int nb_sectors
)
1784 BlockDriver
*drv
= bs
->drv
;
1790 if (!drv
->bdrv_write_compressed
) {
1793 ret
= bdrv_check_request(bs
, sector_num
, nb_sectors
);
1798 assert(QLIST_EMPTY(&bs
->dirty_bitmaps
));
1800 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1803 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1804 int64_t pos
, int size
)
1807 struct iovec iov
= {
1808 .iov_base
= (void *) buf
,
1812 qemu_iovec_init_external(&qiov
, &iov
, 1);
1813 return bdrv_writev_vmstate(bs
, &qiov
, pos
);
1816 int bdrv_writev_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
1818 BlockDriver
*drv
= bs
->drv
;
1822 } else if (drv
->bdrv_save_vmstate
) {
1823 return drv
->bdrv_save_vmstate(bs
, qiov
, pos
);
1824 } else if (bs
->file
) {
1825 return bdrv_writev_vmstate(bs
->file
->bs
, qiov
, pos
);
1831 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1832 int64_t pos
, int size
)
1834 BlockDriver
*drv
= bs
->drv
;
1837 if (drv
->bdrv_load_vmstate
)
1838 return drv
->bdrv_load_vmstate(bs
, buf
, pos
, size
);
1840 return bdrv_load_vmstate(bs
->file
->bs
, buf
, pos
, size
);
1844 /**************************************************************/
1847 BlockAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1848 QEMUIOVector
*qiov
, int nb_sectors
,
1849 BlockCompletionFunc
*cb
, void *opaque
)
1851 trace_bdrv_aio_readv(bs
, sector_num
, nb_sectors
, opaque
);
1853 return bdrv_co_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, 0,
1857 BlockAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1858 QEMUIOVector
*qiov
, int nb_sectors
,
1859 BlockCompletionFunc
*cb
, void *opaque
)
1861 trace_bdrv_aio_writev(bs
, sector_num
, nb_sectors
, opaque
);
1863 return bdrv_co_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, 0,
1867 BlockAIOCB
*bdrv_aio_write_zeroes(BlockDriverState
*bs
,
1868 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
,
1869 BlockCompletionFunc
*cb
, void *opaque
)
1871 trace_bdrv_aio_write_zeroes(bs
, sector_num
, nb_sectors
, flags
, opaque
);
1873 return bdrv_co_aio_rw_vector(bs
, sector_num
, NULL
, nb_sectors
,
1874 BDRV_REQ_ZERO_WRITE
| flags
,
1879 typedef struct MultiwriteCB
{
1884 BlockCompletionFunc
*cb
;
1886 QEMUIOVector
*free_qiov
;
1890 static void multiwrite_user_cb(MultiwriteCB
*mcb
)
1894 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
1895 mcb
->callbacks
[i
].cb(mcb
->callbacks
[i
].opaque
, mcb
->error
);
1896 if (mcb
->callbacks
[i
].free_qiov
) {
1897 qemu_iovec_destroy(mcb
->callbacks
[i
].free_qiov
);
1899 g_free(mcb
->callbacks
[i
].free_qiov
);
1903 static void multiwrite_cb(void *opaque
, int ret
)
1905 MultiwriteCB
*mcb
= opaque
;
1907 trace_multiwrite_cb(mcb
, ret
);
1909 if (ret
< 0 && !mcb
->error
) {
1913 mcb
->num_requests
--;
1914 if (mcb
->num_requests
== 0) {
1915 multiwrite_user_cb(mcb
);
1920 static int multiwrite_req_compare(const void *a
, const void *b
)
1922 const BlockRequest
*req1
= a
, *req2
= b
;
1925 * Note that we can't simply subtract req2->sector from req1->sector
1926 * here as that could overflow the return value.
1928 if (req1
->sector
> req2
->sector
) {
1930 } else if (req1
->sector
< req2
->sector
) {
1938 * Takes a bunch of requests and tries to merge them. Returns the number of
1939 * requests that remain after merging.
1941 static int multiwrite_merge(BlockDriverState
*bs
, BlockRequest
*reqs
,
1942 int num_reqs
, MultiwriteCB
*mcb
)
1946 // Sort requests by start sector
1947 qsort(reqs
, num_reqs
, sizeof(*reqs
), &multiwrite_req_compare
);
1949 // Check if adjacent requests touch the same clusters. If so, combine them,
1950 // filling up gaps with zero sectors.
1952 for (i
= 1; i
< num_reqs
; i
++) {
1954 int64_t oldreq_last
= reqs
[outidx
].sector
+ reqs
[outidx
].nb_sectors
;
1956 // Handle exactly sequential writes and overlapping writes.
1957 if (reqs
[i
].sector
<= oldreq_last
) {
1961 if (reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1 >
1966 if (bs
->bl
.max_transfer_length
&& reqs
[outidx
].nb_sectors
+
1967 reqs
[i
].nb_sectors
> bs
->bl
.max_transfer_length
) {
1973 QEMUIOVector
*qiov
= g_malloc0(sizeof(*qiov
));
1974 qemu_iovec_init(qiov
,
1975 reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1);
1977 // Add the first request to the merged one. If the requests are
1978 // overlapping, drop the last sectors of the first request.
1979 size
= (reqs
[i
].sector
- reqs
[outidx
].sector
) << 9;
1980 qemu_iovec_concat(qiov
, reqs
[outidx
].qiov
, 0, size
);
1982 // We should need to add any zeros between the two requests
1983 assert (reqs
[i
].sector
<= oldreq_last
);
1985 // Add the second request
1986 qemu_iovec_concat(qiov
, reqs
[i
].qiov
, 0, reqs
[i
].qiov
->size
);
1988 // Add tail of first request, if necessary
1989 if (qiov
->size
< reqs
[outidx
].qiov
->size
) {
1990 qemu_iovec_concat(qiov
, reqs
[outidx
].qiov
, qiov
->size
,
1991 reqs
[outidx
].qiov
->size
- qiov
->size
);
1994 reqs
[outidx
].nb_sectors
= qiov
->size
>> 9;
1995 reqs
[outidx
].qiov
= qiov
;
1997 mcb
->callbacks
[i
].free_qiov
= reqs
[outidx
].qiov
;
2000 reqs
[outidx
].sector
= reqs
[i
].sector
;
2001 reqs
[outidx
].nb_sectors
= reqs
[i
].nb_sectors
;
2002 reqs
[outidx
].qiov
= reqs
[i
].qiov
;
2007 block_acct_merge_done(blk_get_stats(bs
->blk
), BLOCK_ACCT_WRITE
,
2008 num_reqs
- outidx
- 1);
2015 * Submit multiple AIO write requests at once.
2017 * On success, the function returns 0 and all requests in the reqs array have
2018 * been submitted. In error case this function returns -1, and any of the
2019 * requests may or may not be submitted yet. In particular, this means that the
2020 * callback will be called for some of the requests, for others it won't. The
2021 * caller must check the error field of the BlockRequest to wait for the right
2022 * callbacks (if error != 0, no callback will be called).
2024 * The implementation may modify the contents of the reqs array, e.g. to merge
2025 * requests. However, the fields opaque and error are left unmodified as they
2026 * are used to signal failure for a single request to the caller.
2028 int bdrv_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
*reqs
, int num_reqs
)
2033 /* don't submit writes if we don't have a medium */
2034 if (bs
->drv
== NULL
) {
2035 for (i
= 0; i
< num_reqs
; i
++) {
2036 reqs
[i
].error
= -ENOMEDIUM
;
2041 if (num_reqs
== 0) {
2045 // Create MultiwriteCB structure
2046 mcb
= g_malloc0(sizeof(*mcb
) + num_reqs
* sizeof(*mcb
->callbacks
));
2047 mcb
->num_requests
= 0;
2048 mcb
->num_callbacks
= num_reqs
;
2050 for (i
= 0; i
< num_reqs
; i
++) {
2051 mcb
->callbacks
[i
].cb
= reqs
[i
].cb
;
2052 mcb
->callbacks
[i
].opaque
= reqs
[i
].opaque
;
2055 // Check for mergable requests
2056 num_reqs
= multiwrite_merge(bs
, reqs
, num_reqs
, mcb
);
2058 trace_bdrv_aio_multiwrite(mcb
, mcb
->num_callbacks
, num_reqs
);
2060 /* Run the aio requests. */
2061 mcb
->num_requests
= num_reqs
;
2062 for (i
= 0; i
< num_reqs
; i
++) {
2063 bdrv_co_aio_rw_vector(bs
, reqs
[i
].sector
, reqs
[i
].qiov
,
2064 reqs
[i
].nb_sectors
, reqs
[i
].flags
,
2072 void bdrv_aio_cancel(BlockAIOCB
*acb
)
2075 bdrv_aio_cancel_async(acb
);
2076 while (acb
->refcnt
> 1) {
2077 if (acb
->aiocb_info
->get_aio_context
) {
2078 aio_poll(acb
->aiocb_info
->get_aio_context(acb
), true);
2079 } else if (acb
->bs
) {
2080 aio_poll(bdrv_get_aio_context(acb
->bs
), true);
2085 qemu_aio_unref(acb
);
2088 /* Async version of aio cancel. The caller is not blocked if the acb implements
2089 * cancel_async, otherwise we do nothing and let the request normally complete.
2090 * In either case the completion callback must be called. */
2091 void bdrv_aio_cancel_async(BlockAIOCB
*acb
)
2093 if (acb
->aiocb_info
->cancel_async
) {
2094 acb
->aiocb_info
->cancel_async(acb
);
2098 /**************************************************************/
2099 /* async block device emulation */
2101 typedef struct BlockAIOCBSync
{
2105 /* vector translation state */
2111 static const AIOCBInfo bdrv_em_aiocb_info
= {
2112 .aiocb_size
= sizeof(BlockAIOCBSync
),
2115 static void bdrv_aio_bh_cb(void *opaque
)
2117 BlockAIOCBSync
*acb
= opaque
;
2119 if (!acb
->is_write
&& acb
->ret
>= 0) {
2120 qemu_iovec_from_buf(acb
->qiov
, 0, acb
->bounce
, acb
->qiov
->size
);
2122 qemu_vfree(acb
->bounce
);
2123 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
2124 qemu_bh_delete(acb
->bh
);
2126 qemu_aio_unref(acb
);
2129 static BlockAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
2133 BlockCompletionFunc
*cb
,
2138 BlockAIOCBSync
*acb
;
2140 acb
= qemu_aio_get(&bdrv_em_aiocb_info
, bs
, cb
, opaque
);
2141 acb
->is_write
= is_write
;
2143 acb
->bounce
= qemu_try_blockalign(bs
, qiov
->size
);
2144 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), bdrv_aio_bh_cb
, acb
);
2146 if (acb
->bounce
== NULL
) {
2148 } else if (is_write
) {
2149 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
2150 acb
->ret
= bs
->drv
->bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2152 acb
->ret
= bs
->drv
->bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2155 qemu_bh_schedule(acb
->bh
);
2157 return &acb
->common
;
2160 static BlockAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
2161 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2162 BlockCompletionFunc
*cb
, void *opaque
)
2164 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
2167 static BlockAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
2168 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2169 BlockCompletionFunc
*cb
, void *opaque
)
2171 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
2175 typedef struct BlockAIOCBCoroutine
{
2182 } BlockAIOCBCoroutine
;
2184 static const AIOCBInfo bdrv_em_co_aiocb_info
= {
2185 .aiocb_size
= sizeof(BlockAIOCBCoroutine
),
2188 static void bdrv_co_complete(BlockAIOCBCoroutine
*acb
)
2190 if (!acb
->need_bh
) {
2191 acb
->common
.cb(acb
->common
.opaque
, acb
->req
.error
);
2192 qemu_aio_unref(acb
);
2196 static void bdrv_co_em_bh(void *opaque
)
2198 BlockAIOCBCoroutine
*acb
= opaque
;
2200 assert(!acb
->need_bh
);
2201 qemu_bh_delete(acb
->bh
);
2202 bdrv_co_complete(acb
);
2205 static void bdrv_co_maybe_schedule_bh(BlockAIOCBCoroutine
*acb
)
2207 acb
->need_bh
= false;
2208 if (acb
->req
.error
!= -EINPROGRESS
) {
2209 BlockDriverState
*bs
= acb
->common
.bs
;
2211 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), bdrv_co_em_bh
, acb
);
2212 qemu_bh_schedule(acb
->bh
);
2216 /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2217 static void coroutine_fn
bdrv_co_do_rw(void *opaque
)
2219 BlockAIOCBCoroutine
*acb
= opaque
;
2220 BlockDriverState
*bs
= acb
->common
.bs
;
2222 if (!acb
->is_write
) {
2223 acb
->req
.error
= bdrv_co_do_readv(bs
, acb
->req
.sector
,
2224 acb
->req
.nb_sectors
, acb
->req
.qiov
, acb
->req
.flags
);
2226 acb
->req
.error
= bdrv_co_do_writev(bs
, acb
->req
.sector
,
2227 acb
->req
.nb_sectors
, acb
->req
.qiov
, acb
->req
.flags
);
2230 bdrv_co_complete(acb
);
2233 static BlockAIOCB
*bdrv_co_aio_rw_vector(BlockDriverState
*bs
,
2237 BdrvRequestFlags flags
,
2238 BlockCompletionFunc
*cb
,
2243 BlockAIOCBCoroutine
*acb
;
2245 acb
= qemu_aio_get(&bdrv_em_co_aiocb_info
, bs
, cb
, opaque
);
2246 acb
->need_bh
= true;
2247 acb
->req
.error
= -EINPROGRESS
;
2248 acb
->req
.sector
= sector_num
;
2249 acb
->req
.nb_sectors
= nb_sectors
;
2250 acb
->req
.qiov
= qiov
;
2251 acb
->req
.flags
= flags
;
2252 acb
->is_write
= is_write
;
2254 co
= qemu_coroutine_create(bdrv_co_do_rw
);
2255 qemu_coroutine_enter(co
, acb
);
2257 bdrv_co_maybe_schedule_bh(acb
);
2258 return &acb
->common
;
2261 static void coroutine_fn
bdrv_aio_flush_co_entry(void *opaque
)
2263 BlockAIOCBCoroutine
*acb
= opaque
;
2264 BlockDriverState
*bs
= acb
->common
.bs
;
2266 acb
->req
.error
= bdrv_co_flush(bs
);
2267 bdrv_co_complete(acb
);
2270 BlockAIOCB
*bdrv_aio_flush(BlockDriverState
*bs
,
2271 BlockCompletionFunc
*cb
, void *opaque
)
2273 trace_bdrv_aio_flush(bs
, opaque
);
2276 BlockAIOCBCoroutine
*acb
;
2278 acb
= qemu_aio_get(&bdrv_em_co_aiocb_info
, bs
, cb
, opaque
);
2279 acb
->need_bh
= true;
2280 acb
->req
.error
= -EINPROGRESS
;
2282 co
= qemu_coroutine_create(bdrv_aio_flush_co_entry
);
2283 qemu_coroutine_enter(co
, acb
);
2285 bdrv_co_maybe_schedule_bh(acb
);
2286 return &acb
->common
;
2289 static void coroutine_fn
bdrv_aio_discard_co_entry(void *opaque
)
2291 BlockAIOCBCoroutine
*acb
= opaque
;
2292 BlockDriverState
*bs
= acb
->common
.bs
;
2294 acb
->req
.error
= bdrv_co_discard(bs
, acb
->req
.sector
, acb
->req
.nb_sectors
);
2295 bdrv_co_complete(acb
);
2298 BlockAIOCB
*bdrv_aio_discard(BlockDriverState
*bs
,
2299 int64_t sector_num
, int nb_sectors
,
2300 BlockCompletionFunc
*cb
, void *opaque
)
2303 BlockAIOCBCoroutine
*acb
;
2305 trace_bdrv_aio_discard(bs
, sector_num
, nb_sectors
, opaque
);
2307 acb
= qemu_aio_get(&bdrv_em_co_aiocb_info
, bs
, cb
, opaque
);
2308 acb
->need_bh
= true;
2309 acb
->req
.error
= -EINPROGRESS
;
2310 acb
->req
.sector
= sector_num
;
2311 acb
->req
.nb_sectors
= nb_sectors
;
2312 co
= qemu_coroutine_create(bdrv_aio_discard_co_entry
);
2313 qemu_coroutine_enter(co
, acb
);
2315 bdrv_co_maybe_schedule_bh(acb
);
2316 return &acb
->common
;
2319 void *qemu_aio_get(const AIOCBInfo
*aiocb_info
, BlockDriverState
*bs
,
2320 BlockCompletionFunc
*cb
, void *opaque
)
2324 acb
= g_malloc(aiocb_info
->aiocb_size
);
2325 acb
->aiocb_info
= aiocb_info
;
2328 acb
->opaque
= opaque
;
2333 void qemu_aio_ref(void *p
)
2335 BlockAIOCB
*acb
= p
;
2339 void qemu_aio_unref(void *p
)
2341 BlockAIOCB
*acb
= p
;
2342 assert(acb
->refcnt
> 0);
2343 if (--acb
->refcnt
== 0) {
2348 /**************************************************************/
2349 /* Coroutine block device emulation */
2351 typedef struct CoroutineIOCompletion
{
2352 Coroutine
*coroutine
;
2354 } CoroutineIOCompletion
;
2356 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
2358 CoroutineIOCompletion
*co
= opaque
;
2361 qemu_coroutine_enter(co
->coroutine
, NULL
);
2364 static int coroutine_fn
bdrv_co_io_em(BlockDriverState
*bs
, int64_t sector_num
,
2365 int nb_sectors
, QEMUIOVector
*iov
,
2368 CoroutineIOCompletion co
= {
2369 .coroutine
= qemu_coroutine_self(),
2374 acb
= bs
->drv
->bdrv_aio_writev(bs
, sector_num
, iov
, nb_sectors
,
2375 bdrv_co_io_em_complete
, &co
);
2377 acb
= bs
->drv
->bdrv_aio_readv(bs
, sector_num
, iov
, nb_sectors
,
2378 bdrv_co_io_em_complete
, &co
);
2381 trace_bdrv_co_io_em(bs
, sector_num
, nb_sectors
, is_write
, acb
);
2385 qemu_coroutine_yield();
2390 static int coroutine_fn
bdrv_co_readv_em(BlockDriverState
*bs
,
2391 int64_t sector_num
, int nb_sectors
,
2394 return bdrv_co_io_em(bs
, sector_num
, nb_sectors
, iov
, false);
2397 static int coroutine_fn
bdrv_co_writev_em(BlockDriverState
*bs
,
2398 int64_t sector_num
, int nb_sectors
,
2401 return bdrv_co_io_em(bs
, sector_num
, nb_sectors
, iov
, true);
2404 static void coroutine_fn
bdrv_flush_co_entry(void *opaque
)
2406 RwCo
*rwco
= opaque
;
2408 rwco
->ret
= bdrv_co_flush(rwco
->bs
);
2411 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2414 BdrvTrackedRequest req
;
2416 if (!bs
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
) ||
2421 tracked_request_begin(&req
, bs
, 0, 0, BDRV_TRACKED_FLUSH
);
2423 /* Write back all layers by calling one driver function */
2424 if (bs
->drv
->bdrv_co_flush
) {
2425 ret
= bs
->drv
->bdrv_co_flush(bs
);
2429 /* Write back cached data to the OS even with cache=unsafe */
2430 BLKDBG_EVENT(bs
->file
, BLKDBG_FLUSH_TO_OS
);
2431 if (bs
->drv
->bdrv_co_flush_to_os
) {
2432 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
2438 /* But don't actually force it to the disk with cache=unsafe */
2439 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2443 BLKDBG_EVENT(bs
->file
, BLKDBG_FLUSH_TO_DISK
);
2444 if (bs
->drv
->bdrv_co_flush_to_disk
) {
2445 ret
= bs
->drv
->bdrv_co_flush_to_disk(bs
);
2446 } else if (bs
->drv
->bdrv_aio_flush
) {
2448 CoroutineIOCompletion co
= {
2449 .coroutine
= qemu_coroutine_self(),
2452 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
2456 qemu_coroutine_yield();
2461 * Some block drivers always operate in either writethrough or unsafe
2462 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2463 * know how the server works (because the behaviour is hardcoded or
2464 * depends on server-side configuration), so we can't ensure that
2465 * everything is safe on disk. Returning an error doesn't work because
2466 * that would break guests even if the server operates in writethrough
2469 * Let's hope the user knows what he's doing.
2477 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2478 * in the case of cache=unsafe, so there are no useless flushes.
2481 ret
= bs
->file
? bdrv_co_flush(bs
->file
->bs
) : 0;
2483 tracked_request_end(&req
);
2487 int bdrv_flush(BlockDriverState
*bs
)
2495 if (qemu_in_coroutine()) {
2496 /* Fast-path if already in coroutine context */
2497 bdrv_flush_co_entry(&rwco
);
2499 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
2501 co
= qemu_coroutine_create(bdrv_flush_co_entry
);
2502 qemu_coroutine_enter(co
, &rwco
);
2503 while (rwco
.ret
== NOT_DONE
) {
2504 aio_poll(aio_context
, true);
2511 typedef struct DiscardCo
{
2512 BlockDriverState
*bs
;
2517 static void coroutine_fn
bdrv_discard_co_entry(void *opaque
)
2519 DiscardCo
*rwco
= opaque
;
2521 rwco
->ret
= bdrv_co_discard(rwco
->bs
, rwco
->sector_num
, rwco
->nb_sectors
);
2524 int coroutine_fn
bdrv_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
2527 BdrvTrackedRequest req
;
2528 int max_discard
, ret
;
2534 ret
= bdrv_check_request(bs
, sector_num
, nb_sectors
);
2537 } else if (bs
->read_only
) {
2540 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
2542 /* Do nothing if disabled. */
2543 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
2547 if (!bs
->drv
->bdrv_co_discard
&& !bs
->drv
->bdrv_aio_discard
) {
2551 tracked_request_begin(&req
, bs
, sector_num
, nb_sectors
,
2552 BDRV_TRACKED_DISCARD
);
2553 bdrv_set_dirty(bs
, sector_num
, nb_sectors
);
2555 max_discard
= MIN_NON_ZERO(bs
->bl
.max_discard
, BDRV_REQUEST_MAX_SECTORS
);
2556 while (nb_sectors
> 0) {
2558 int num
= nb_sectors
;
2561 if (bs
->bl
.discard_alignment
&&
2562 num
>= bs
->bl
.discard_alignment
&&
2563 sector_num
% bs
->bl
.discard_alignment
) {
2564 if (num
> bs
->bl
.discard_alignment
) {
2565 num
= bs
->bl
.discard_alignment
;
2567 num
-= sector_num
% bs
->bl
.discard_alignment
;
2570 /* limit request size */
2571 if (num
> max_discard
) {
2575 if (bs
->drv
->bdrv_co_discard
) {
2576 ret
= bs
->drv
->bdrv_co_discard(bs
, sector_num
, num
);
2579 CoroutineIOCompletion co
= {
2580 .coroutine
= qemu_coroutine_self(),
2583 acb
= bs
->drv
->bdrv_aio_discard(bs
, sector_num
, nb_sectors
,
2584 bdrv_co_io_em_complete
, &co
);
2589 qemu_coroutine_yield();
2593 if (ret
&& ret
!= -ENOTSUP
) {
2602 tracked_request_end(&req
);
2606 int bdrv_discard(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
)
2611 .sector_num
= sector_num
,
2612 .nb_sectors
= nb_sectors
,
2616 if (qemu_in_coroutine()) {
2617 /* Fast-path if already in coroutine context */
2618 bdrv_discard_co_entry(&rwco
);
2620 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
2622 co
= qemu_coroutine_create(bdrv_discard_co_entry
);
2623 qemu_coroutine_enter(co
, &rwco
);
2624 while (rwco
.ret
== NOT_DONE
) {
2625 aio_poll(aio_context
, true);
2633 CoroutineIOCompletion
*co
;
2635 } BdrvIoctlCompletionData
;
2637 static void bdrv_ioctl_bh_cb(void *opaque
)
2639 BdrvIoctlCompletionData
*data
= opaque
;
2641 bdrv_co_io_em_complete(data
->co
, -ENOTSUP
);
2642 qemu_bh_delete(data
->bh
);
2645 static int bdrv_co_do_ioctl(BlockDriverState
*bs
, int req
, void *buf
)
2647 BlockDriver
*drv
= bs
->drv
;
2648 BdrvTrackedRequest tracked_req
;
2649 CoroutineIOCompletion co
= {
2650 .coroutine
= qemu_coroutine_self(),
2654 tracked_request_begin(&tracked_req
, bs
, 0, 0, BDRV_TRACKED_IOCTL
);
2655 if (!drv
|| !drv
->bdrv_aio_ioctl
) {
2660 acb
= drv
->bdrv_aio_ioctl(bs
, req
, buf
, bdrv_co_io_em_complete
, &co
);
2662 BdrvIoctlCompletionData
*data
= g_new(BdrvIoctlCompletionData
, 1);
2663 data
->bh
= aio_bh_new(bdrv_get_aio_context(bs
),
2664 bdrv_ioctl_bh_cb
, data
);
2666 qemu_bh_schedule(data
->bh
);
2668 qemu_coroutine_yield();
2670 tracked_request_end(&tracked_req
);
2675 BlockDriverState
*bs
;
2681 static void coroutine_fn
bdrv_co_ioctl_entry(void *opaque
)
2683 BdrvIoctlCoData
*data
= opaque
;
2684 data
->ret
= bdrv_co_do_ioctl(data
->bs
, data
->req
, data
->buf
);
2687 /* needed for generic scsi interface */
2688 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
2690 BdrvIoctlCoData data
= {
2694 .ret
= -EINPROGRESS
,
2697 if (qemu_in_coroutine()) {
2698 /* Fast-path if already in coroutine context */
2699 bdrv_co_ioctl_entry(&data
);
2701 Coroutine
*co
= qemu_coroutine_create(bdrv_co_ioctl_entry
);
2703 qemu_coroutine_enter(co
, &data
);
2704 while (data
.ret
== -EINPROGRESS
) {
2705 aio_poll(bdrv_get_aio_context(bs
), true);
2711 static void coroutine_fn
bdrv_co_aio_ioctl_entry(void *opaque
)
2713 BlockAIOCBCoroutine
*acb
= opaque
;
2714 acb
->req
.error
= bdrv_co_do_ioctl(acb
->common
.bs
,
2715 acb
->req
.req
, acb
->req
.buf
);
2716 bdrv_co_complete(acb
);
2719 BlockAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
2720 unsigned long int req
, void *buf
,
2721 BlockCompletionFunc
*cb
, void *opaque
)
2723 BlockAIOCBCoroutine
*acb
= qemu_aio_get(&bdrv_em_co_aiocb_info
,
2727 acb
->need_bh
= true;
2728 acb
->req
.error
= -EINPROGRESS
;
2731 co
= qemu_coroutine_create(bdrv_co_aio_ioctl_entry
);
2732 qemu_coroutine_enter(co
, acb
);
2734 bdrv_co_maybe_schedule_bh(acb
);
2735 return &acb
->common
;
2738 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
2740 return qemu_memalign(bdrv_opt_mem_align(bs
), size
);
2743 void *qemu_blockalign0(BlockDriverState
*bs
, size_t size
)
2745 return memset(qemu_blockalign(bs
, size
), 0, size
);
2748 void *qemu_try_blockalign(BlockDriverState
*bs
, size_t size
)
2750 size_t align
= bdrv_opt_mem_align(bs
);
2752 /* Ensure that NULL is never returned on success */
2758 return qemu_try_memalign(align
, size
);
2761 void *qemu_try_blockalign0(BlockDriverState
*bs
, size_t size
)
2763 void *mem
= qemu_try_blockalign(bs
, size
);
2766 memset(mem
, 0, size
);
2773 * Check if all memory in this vector is sector aligned.
2775 bool bdrv_qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
2778 size_t alignment
= bdrv_min_mem_align(bs
);
2780 for (i
= 0; i
< qiov
->niov
; i
++) {
2781 if ((uintptr_t) qiov
->iov
[i
].iov_base
% alignment
) {
2784 if (qiov
->iov
[i
].iov_len
% alignment
) {
2792 void bdrv_add_before_write_notifier(BlockDriverState
*bs
,
2793 NotifierWithReturn
*notifier
)
2795 notifier_with_return_list_add(&bs
->before_write_notifiers
, notifier
);
2798 void bdrv_io_plug(BlockDriverState
*bs
)
2802 QLIST_FOREACH(child
, &bs
->children
, next
) {
2803 bdrv_io_plug(child
->bs
);
2806 if (bs
->io_plugged
++ == 0 && bs
->io_plug_disabled
== 0) {
2807 BlockDriver
*drv
= bs
->drv
;
2808 if (drv
&& drv
->bdrv_io_plug
) {
2809 drv
->bdrv_io_plug(bs
);
2814 void bdrv_io_unplug(BlockDriverState
*bs
)
2818 assert(bs
->io_plugged
);
2819 if (--bs
->io_plugged
== 0 && bs
->io_plug_disabled
== 0) {
2820 BlockDriver
*drv
= bs
->drv
;
2821 if (drv
&& drv
->bdrv_io_unplug
) {
2822 drv
->bdrv_io_unplug(bs
);
2826 QLIST_FOREACH(child
, &bs
->children
, next
) {
2827 bdrv_io_unplug(child
->bs
);
2831 void bdrv_io_unplugged_begin(BlockDriverState
*bs
)
2835 if (bs
->io_plug_disabled
++ == 0 && bs
->io_plugged
> 0) {
2836 BlockDriver
*drv
= bs
->drv
;
2837 if (drv
&& drv
->bdrv_io_unplug
) {
2838 drv
->bdrv_io_unplug(bs
);
2842 QLIST_FOREACH(child
, &bs
->children
, next
) {
2843 bdrv_io_unplugged_begin(child
->bs
);
2847 void bdrv_io_unplugged_end(BlockDriverState
*bs
)
2851 assert(bs
->io_plug_disabled
);
2852 QLIST_FOREACH(child
, &bs
->children
, next
) {
2853 bdrv_io_unplugged_end(child
->bs
);
2856 if (--bs
->io_plug_disabled
== 0 && bs
->io_plugged
> 0) {
2857 BlockDriver
*drv
= bs
->drv
;
2858 if (drv
&& drv
->bdrv_io_plug
) {
2859 drv
->bdrv_io_plug(bs
);
2864 void bdrv_drained_begin(BlockDriverState
*bs
)
2866 if (!bs
->quiesce_counter
++) {
2867 aio_disable_external(bdrv_get_aio_context(bs
));
2872 void bdrv_drained_end(BlockDriverState
*bs
)
2874 assert(bs
->quiesce_counter
> 0);
2875 if (--bs
->quiesce_counter
> 0) {
2878 aio_enable_external(bdrv_get_aio_context(bs
));