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
)
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(child
, &bs
->children
, next
) {
171 waited
|= bdrv_drain_recurse(child
->bs
);
179 BlockDriverState
*bs
;
183 static void bdrv_co_drain_bh_cb(void *opaque
)
185 BdrvCoDrainData
*data
= opaque
;
186 Coroutine
*co
= data
->co
;
187 BlockDriverState
*bs
= data
->bs
;
189 bdrv_dec_in_flight(bs
);
190 bdrv_drained_begin(bs
);
195 static void coroutine_fn
bdrv_co_yield_to_drain(BlockDriverState
*bs
)
197 BdrvCoDrainData data
;
199 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
200 * other coroutines run if they were queued from
201 * qemu_co_queue_run_restart(). */
203 assert(qemu_in_coroutine());
204 data
= (BdrvCoDrainData
) {
205 .co
= qemu_coroutine_self(),
209 bdrv_inc_in_flight(bs
);
210 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
),
211 bdrv_co_drain_bh_cb
, &data
);
213 qemu_coroutine_yield();
214 /* If we are resumed from some other event (such as an aio completion or a
215 * timer callback), it is a bug in the caller that should be fixed. */
219 void bdrv_drained_begin(BlockDriverState
*bs
)
221 if (qemu_in_coroutine()) {
222 bdrv_co_yield_to_drain(bs
);
226 if (!bs
->quiesce_counter
++) {
227 aio_disable_external(bdrv_get_aio_context(bs
));
228 bdrv_parent_drained_begin(bs
);
231 bdrv_drain_recurse(bs
);
234 void bdrv_drained_end(BlockDriverState
*bs
)
236 assert(bs
->quiesce_counter
> 0);
237 if (--bs
->quiesce_counter
> 0) {
241 bdrv_parent_drained_end(bs
);
242 aio_enable_external(bdrv_get_aio_context(bs
));
246 * Wait for pending requests to complete on a single BlockDriverState subtree,
247 * and suspend block driver's internal I/O until next request arrives.
249 * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
252 * Only this BlockDriverState's AioContext is run, so in-flight requests must
253 * not depend on events in other AioContexts. In that case, use
254 * bdrv_drain_all() instead.
256 void coroutine_fn
bdrv_co_drain(BlockDriverState
*bs
)
258 assert(qemu_in_coroutine());
259 bdrv_drained_begin(bs
);
260 bdrv_drained_end(bs
);
263 void bdrv_drain(BlockDriverState
*bs
)
265 bdrv_drained_begin(bs
);
266 bdrv_drained_end(bs
);
270 * Wait for pending requests to complete across all BlockDriverStates
272 * This function does not flush data to disk, use bdrv_flush_all() for that
273 * after calling this function.
275 * This pauses all block jobs and disables external clients. It must
276 * be paired with bdrv_drain_all_end().
278 * NOTE: no new block jobs or BlockDriverStates can be created between
279 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
281 void bdrv_drain_all_begin(void)
283 /* Always run first iteration so any pending completion BHs run */
285 BlockDriverState
*bs
;
287 BlockJob
*job
= NULL
;
288 GSList
*aio_ctxs
= NULL
, *ctx
;
290 while ((job
= block_job_next(job
))) {
291 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
293 aio_context_acquire(aio_context
);
294 block_job_pause(job
);
295 aio_context_release(aio_context
);
298 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
299 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
301 aio_context_acquire(aio_context
);
302 bdrv_parent_drained_begin(bs
);
303 aio_disable_external(aio_context
);
304 aio_context_release(aio_context
);
306 if (!g_slist_find(aio_ctxs
, aio_context
)) {
307 aio_ctxs
= g_slist_prepend(aio_ctxs
, aio_context
);
311 /* Note that completion of an asynchronous I/O operation can trigger any
312 * number of other I/O operations on other devices---for example a
313 * coroutine can submit an I/O request to another device in response to
314 * request completion. Therefore we must keep looping until there was no
315 * more activity rather than simply draining each device independently.
320 for (ctx
= aio_ctxs
; ctx
!= NULL
; ctx
= ctx
->next
) {
321 AioContext
*aio_context
= ctx
->data
;
323 aio_context_acquire(aio_context
);
324 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
325 if (aio_context
== bdrv_get_aio_context(bs
)) {
326 waited
|= bdrv_drain_recurse(bs
);
329 aio_context_release(aio_context
);
333 g_slist_free(aio_ctxs
);
336 void bdrv_drain_all_end(void)
338 BlockDriverState
*bs
;
340 BlockJob
*job
= NULL
;
342 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
343 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
345 aio_context_acquire(aio_context
);
346 aio_enable_external(aio_context
);
347 bdrv_parent_drained_end(bs
);
348 aio_context_release(aio_context
);
351 while ((job
= block_job_next(job
))) {
352 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
354 aio_context_acquire(aio_context
);
355 block_job_resume(job
);
356 aio_context_release(aio_context
);
360 void bdrv_drain_all(void)
362 bdrv_drain_all_begin();
363 bdrv_drain_all_end();
367 * Remove an active request from the tracked requests list
369 * This function should be called when a tracked request is completing.
371 static void tracked_request_end(BdrvTrackedRequest
*req
)
373 if (req
->serialising
) {
374 req
->bs
->serialising_in_flight
--;
377 QLIST_REMOVE(req
, list
);
378 qemu_co_queue_restart_all(&req
->wait_queue
);
382 * Add an active request to the tracked requests list
384 static void tracked_request_begin(BdrvTrackedRequest
*req
,
385 BlockDriverState
*bs
,
388 enum BdrvTrackedRequestType type
)
390 *req
= (BdrvTrackedRequest
){
395 .co
= qemu_coroutine_self(),
396 .serialising
= false,
397 .overlap_offset
= offset
,
398 .overlap_bytes
= bytes
,
401 qemu_co_queue_init(&req
->wait_queue
);
403 QLIST_INSERT_HEAD(&bs
->tracked_requests
, req
, list
);
406 static void mark_request_serialising(BdrvTrackedRequest
*req
, uint64_t align
)
408 int64_t overlap_offset
= req
->offset
& ~(align
- 1);
409 unsigned int overlap_bytes
= ROUND_UP(req
->offset
+ req
->bytes
, align
)
412 if (!req
->serialising
) {
413 req
->bs
->serialising_in_flight
++;
414 req
->serialising
= true;
417 req
->overlap_offset
= MIN(req
->overlap_offset
, overlap_offset
);
418 req
->overlap_bytes
= MAX(req
->overlap_bytes
, overlap_bytes
);
422 * Round a region to cluster boundaries (sector-based)
424 void bdrv_round_sectors_to_clusters(BlockDriverState
*bs
,
425 int64_t sector_num
, int nb_sectors
,
426 int64_t *cluster_sector_num
,
427 int *cluster_nb_sectors
)
431 if (bdrv_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
432 *cluster_sector_num
= sector_num
;
433 *cluster_nb_sectors
= nb_sectors
;
435 int64_t c
= bdi
.cluster_size
/ BDRV_SECTOR_SIZE
;
436 *cluster_sector_num
= QEMU_ALIGN_DOWN(sector_num
, c
);
437 *cluster_nb_sectors
= QEMU_ALIGN_UP(sector_num
- *cluster_sector_num
+
443 * Round a region to cluster boundaries
445 void bdrv_round_to_clusters(BlockDriverState
*bs
,
446 int64_t offset
, unsigned int bytes
,
447 int64_t *cluster_offset
,
448 unsigned int *cluster_bytes
)
452 if (bdrv_get_info(bs
, &bdi
) < 0 || bdi
.cluster_size
== 0) {
453 *cluster_offset
= offset
;
454 *cluster_bytes
= bytes
;
456 int64_t c
= bdi
.cluster_size
;
457 *cluster_offset
= QEMU_ALIGN_DOWN(offset
, c
);
458 *cluster_bytes
= QEMU_ALIGN_UP(offset
- *cluster_offset
+ bytes
, c
);
462 static int bdrv_get_cluster_size(BlockDriverState
*bs
)
467 ret
= bdrv_get_info(bs
, &bdi
);
468 if (ret
< 0 || bdi
.cluster_size
== 0) {
469 return bs
->bl
.request_alignment
;
471 return bdi
.cluster_size
;
475 static bool tracked_request_overlaps(BdrvTrackedRequest
*req
,
476 int64_t offset
, unsigned int bytes
)
479 if (offset
>= req
->overlap_offset
+ req
->overlap_bytes
) {
483 if (req
->overlap_offset
>= offset
+ bytes
) {
489 void bdrv_inc_in_flight(BlockDriverState
*bs
)
491 atomic_inc(&bs
->in_flight
);
494 static void dummy_bh_cb(void *opaque
)
498 void bdrv_wakeup(BlockDriverState
*bs
)
501 aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb
, NULL
);
505 void bdrv_dec_in_flight(BlockDriverState
*bs
)
507 atomic_dec(&bs
->in_flight
);
511 static bool coroutine_fn
wait_serialising_requests(BdrvTrackedRequest
*self
)
513 BlockDriverState
*bs
= self
->bs
;
514 BdrvTrackedRequest
*req
;
518 if (!bs
->serialising_in_flight
) {
524 QLIST_FOREACH(req
, &bs
->tracked_requests
, list
) {
525 if (req
== self
|| (!req
->serialising
&& !self
->serialising
)) {
528 if (tracked_request_overlaps(req
, self
->overlap_offset
,
529 self
->overlap_bytes
))
531 /* Hitting this means there was a reentrant request, for
532 * example, a block driver issuing nested requests. This must
533 * never happen since it means deadlock.
535 assert(qemu_coroutine_self() != req
->co
);
537 /* If the request is already (indirectly) waiting for us, or
538 * will wait for us as soon as it wakes up, then just go on
539 * (instead of producing a deadlock in the former case). */
540 if (!req
->waiting_for
) {
541 self
->waiting_for
= req
;
542 qemu_co_queue_wait(&req
->wait_queue
, NULL
);
543 self
->waiting_for
= NULL
;
555 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
558 if (size
> BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
) {
562 if (!bdrv_is_inserted(bs
)) {
573 typedef struct RwCo
{
579 BdrvRequestFlags flags
;
582 static void coroutine_fn
bdrv_rw_co_entry(void *opaque
)
586 if (!rwco
->is_write
) {
587 rwco
->ret
= bdrv_co_preadv(rwco
->child
, rwco
->offset
,
588 rwco
->qiov
->size
, rwco
->qiov
,
591 rwco
->ret
= bdrv_co_pwritev(rwco
->child
, rwco
->offset
,
592 rwco
->qiov
->size
, rwco
->qiov
,
598 * Process a vectored synchronous request using coroutines
600 static int bdrv_prwv_co(BdrvChild
*child
, int64_t offset
,
601 QEMUIOVector
*qiov
, bool is_write
,
602 BdrvRequestFlags flags
)
609 .is_write
= is_write
,
614 if (qemu_in_coroutine()) {
615 /* Fast-path if already in coroutine context */
616 bdrv_rw_co_entry(&rwco
);
618 co
= qemu_coroutine_create(bdrv_rw_co_entry
, &rwco
);
619 bdrv_coroutine_enter(child
->bs
, co
);
620 BDRV_POLL_WHILE(child
->bs
, rwco
.ret
== NOT_DONE
);
626 * Process a synchronous request using coroutines
628 static int bdrv_rw_co(BdrvChild
*child
, int64_t sector_num
, uint8_t *buf
,
629 int nb_sectors
, bool is_write
, BdrvRequestFlags flags
)
633 .iov_base
= (void *)buf
,
634 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
637 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
641 qemu_iovec_init_external(&qiov
, &iov
, 1);
642 return bdrv_prwv_co(child
, sector_num
<< BDRV_SECTOR_BITS
,
643 &qiov
, is_write
, flags
);
646 /* return < 0 if error. See bdrv_write() for the return codes */
647 int bdrv_read(BdrvChild
*child
, int64_t sector_num
,
648 uint8_t *buf
, int nb_sectors
)
650 return bdrv_rw_co(child
, sector_num
, buf
, nb_sectors
, false, 0);
653 /* Return < 0 if error. Important errors are:
654 -EIO generic I/O error (may happen for all errors)
655 -ENOMEDIUM No media inserted.
656 -EINVAL Invalid sector number or nb_sectors
657 -EACCES Trying to write a read-only device
659 int bdrv_write(BdrvChild
*child
, int64_t sector_num
,
660 const uint8_t *buf
, int nb_sectors
)
662 return bdrv_rw_co(child
, sector_num
, (uint8_t *)buf
, nb_sectors
, true, 0);
665 int bdrv_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
666 int count
, BdrvRequestFlags flags
)
674 qemu_iovec_init_external(&qiov
, &iov
, 1);
675 return bdrv_prwv_co(child
, offset
, &qiov
, true,
676 BDRV_REQ_ZERO_WRITE
| flags
);
680 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
681 * The operation is sped up by checking the block status and only writing
682 * zeroes to the device if they currently do not return zeroes. Optional
683 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
686 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
688 int bdrv_make_zero(BdrvChild
*child
, BdrvRequestFlags flags
)
690 int64_t target_sectors
, ret
, nb_sectors
, sector_num
= 0;
691 BlockDriverState
*bs
= child
->bs
;
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_pwrite_zeroes(child
, sector_num
<< BDRV_SECTOR_BITS
,
716 n
<< BDRV_SECTOR_BITS
, flags
);
718 error_report("error writing zeroes at sector %" PRId64
": %s",
719 sector_num
, strerror(-ret
));
726 int bdrv_preadv(BdrvChild
*child
, int64_t offset
, QEMUIOVector
*qiov
)
730 ret
= bdrv_prwv_co(child
, offset
, qiov
, false, 0);
738 int bdrv_pread(BdrvChild
*child
, int64_t offset
, void *buf
, int bytes
)
742 .iov_base
= (void *)buf
,
750 qemu_iovec_init_external(&qiov
, &iov
, 1);
751 return bdrv_preadv(child
, offset
, &qiov
);
754 int bdrv_pwritev(BdrvChild
*child
, int64_t offset
, QEMUIOVector
*qiov
)
758 ret
= bdrv_prwv_co(child
, offset
, qiov
, true, 0);
766 int bdrv_pwrite(BdrvChild
*child
, int64_t offset
, const void *buf
, int bytes
)
770 .iov_base
= (void *) buf
,
778 qemu_iovec_init_external(&qiov
, &iov
, 1);
779 return bdrv_pwritev(child
, offset
, &qiov
);
783 * Writes to the file and ensures that no writes are reordered across this
784 * request (acts as a barrier)
786 * Returns 0 on success, -errno in error cases.
788 int bdrv_pwrite_sync(BdrvChild
*child
, int64_t offset
,
789 const void *buf
, int count
)
793 ret
= bdrv_pwrite(child
, offset
, buf
, count
);
798 ret
= bdrv_flush(child
->bs
);
806 typedef struct CoroutineIOCompletion
{
807 Coroutine
*coroutine
;
809 } CoroutineIOCompletion
;
811 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
813 CoroutineIOCompletion
*co
= opaque
;
816 aio_co_wake(co
->coroutine
);
819 static int coroutine_fn
bdrv_driver_preadv(BlockDriverState
*bs
,
820 uint64_t offset
, uint64_t bytes
,
821 QEMUIOVector
*qiov
, int flags
)
823 BlockDriver
*drv
= bs
->drv
;
825 unsigned int nb_sectors
;
827 assert(!(flags
& ~BDRV_REQ_MASK
));
829 if (drv
->bdrv_co_preadv
) {
830 return drv
->bdrv_co_preadv(bs
, offset
, bytes
, qiov
, flags
);
833 sector_num
= offset
>> BDRV_SECTOR_BITS
;
834 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
836 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
837 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
838 assert((bytes
>> BDRV_SECTOR_BITS
) <= BDRV_REQUEST_MAX_SECTORS
);
840 if (drv
->bdrv_co_readv
) {
841 return drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
844 CoroutineIOCompletion co
= {
845 .coroutine
= qemu_coroutine_self(),
848 acb
= bs
->drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
849 bdrv_co_io_em_complete
, &co
);
853 qemu_coroutine_yield();
859 static int coroutine_fn
bdrv_driver_pwritev(BlockDriverState
*bs
,
860 uint64_t offset
, uint64_t bytes
,
861 QEMUIOVector
*qiov
, int flags
)
863 BlockDriver
*drv
= bs
->drv
;
865 unsigned int nb_sectors
;
868 assert(!(flags
& ~BDRV_REQ_MASK
));
870 if (drv
->bdrv_co_pwritev
) {
871 ret
= drv
->bdrv_co_pwritev(bs
, offset
, bytes
, qiov
,
872 flags
& bs
->supported_write_flags
);
873 flags
&= ~bs
->supported_write_flags
;
877 sector_num
= offset
>> BDRV_SECTOR_BITS
;
878 nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
880 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
881 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
882 assert((bytes
>> BDRV_SECTOR_BITS
) <= BDRV_REQUEST_MAX_SECTORS
);
884 if (drv
->bdrv_co_writev_flags
) {
885 ret
= drv
->bdrv_co_writev_flags(bs
, sector_num
, nb_sectors
, qiov
,
886 flags
& bs
->supported_write_flags
);
887 flags
&= ~bs
->supported_write_flags
;
888 } else if (drv
->bdrv_co_writev
) {
889 assert(!bs
->supported_write_flags
);
890 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
);
893 CoroutineIOCompletion co
= {
894 .coroutine
= qemu_coroutine_self(),
897 acb
= bs
->drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
898 bdrv_co_io_em_complete
, &co
);
902 qemu_coroutine_yield();
908 if (ret
== 0 && (flags
& BDRV_REQ_FUA
)) {
909 ret
= bdrv_co_flush(bs
);
915 static int coroutine_fn
916 bdrv_driver_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
917 uint64_t bytes
, QEMUIOVector
*qiov
)
919 BlockDriver
*drv
= bs
->drv
;
921 if (!drv
->bdrv_co_pwritev_compressed
) {
925 return drv
->bdrv_co_pwritev_compressed(bs
, offset
, bytes
, qiov
);
928 static int coroutine_fn
bdrv_co_do_copy_on_readv(BdrvChild
*child
,
929 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
)
931 BlockDriverState
*bs
= child
->bs
;
933 /* Perform I/O through a temporary buffer so that users who scribble over
934 * their read buffer while the operation is in progress do not end up
935 * modifying the image file. This is critical for zero-copy guest I/O
936 * where anything might happen inside guest memory.
940 BlockDriver
*drv
= bs
->drv
;
942 QEMUIOVector bounce_qiov
;
943 int64_t cluster_offset
;
944 unsigned int cluster_bytes
;
948 /* FIXME We cannot require callers to have write permissions when all they
949 * are doing is a read request. If we did things right, write permissions
950 * would be obtained anyway, but internally by the copy-on-read code. As
951 * long as it is implemented here rather than in a separat filter driver,
952 * the copy-on-read code doesn't have its own BdrvChild, however, for which
953 * it could request permissions. Therefore we have to bypass the permission
954 * system for the moment. */
955 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
957 /* Cover entire cluster so no additional backing file I/O is required when
958 * allocating cluster in the image file.
960 bdrv_round_to_clusters(bs
, offset
, bytes
, &cluster_offset
, &cluster_bytes
);
962 trace_bdrv_co_do_copy_on_readv(bs
, offset
, bytes
,
963 cluster_offset
, cluster_bytes
);
965 iov
.iov_len
= cluster_bytes
;
966 iov
.iov_base
= bounce_buffer
= qemu_try_blockalign(bs
, iov
.iov_len
);
967 if (bounce_buffer
== NULL
) {
972 qemu_iovec_init_external(&bounce_qiov
, &iov
, 1);
974 ret
= bdrv_driver_preadv(bs
, cluster_offset
, cluster_bytes
,
980 if (drv
->bdrv_co_pwrite_zeroes
&&
981 buffer_is_zero(bounce_buffer
, iov
.iov_len
)) {
982 /* FIXME: Should we (perhaps conditionally) be setting
983 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
984 * that still correctly reads as zero? */
985 ret
= bdrv_co_do_pwrite_zeroes(bs
, cluster_offset
, cluster_bytes
, 0);
987 /* This does not change the data on the disk, it is not necessary
988 * to flush even in cache=writethrough mode.
990 ret
= bdrv_driver_pwritev(bs
, cluster_offset
, cluster_bytes
,
995 /* It might be okay to ignore write errors for guest requests. If this
996 * is a deliberate copy-on-read then we don't want to ignore the error.
997 * Simply report it in all cases.
1002 skip_bytes
= offset
- cluster_offset
;
1003 qemu_iovec_from_buf(qiov
, 0, bounce_buffer
+ skip_bytes
, bytes
);
1006 qemu_vfree(bounce_buffer
);
1011 * Forwards an already correctly aligned request to the BlockDriver. This
1012 * handles copy on read, zeroing after EOF, and fragmentation of large
1013 * reads; any other features must be implemented by the caller.
1015 static int coroutine_fn
bdrv_aligned_preadv(BdrvChild
*child
,
1016 BdrvTrackedRequest
*req
, int64_t offset
, unsigned int bytes
,
1017 int64_t align
, QEMUIOVector
*qiov
, int flags
)
1019 BlockDriverState
*bs
= child
->bs
;
1020 int64_t total_bytes
, max_bytes
;
1022 uint64_t bytes_remaining
= bytes
;
1025 assert(is_power_of_2(align
));
1026 assert((offset
& (align
- 1)) == 0);
1027 assert((bytes
& (align
- 1)) == 0);
1028 assert(!qiov
|| bytes
== qiov
->size
);
1029 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1030 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1033 /* TODO: We would need a per-BDS .supported_read_flags and
1034 * potential fallback support, if we ever implement any read flags
1035 * to pass through to drivers. For now, there aren't any
1036 * passthrough flags. */
1037 assert(!(flags
& ~(BDRV_REQ_NO_SERIALISING
| BDRV_REQ_COPY_ON_READ
)));
1039 /* Handle Copy on Read and associated serialisation */
1040 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1041 /* If we touch the same cluster it counts as an overlap. This
1042 * guarantees that allocating writes will be serialized and not race
1043 * with each other for the same cluster. For example, in copy-on-read
1044 * it ensures that the CoR read and write operations are atomic and
1045 * guest writes cannot interleave between them. */
1046 mark_request_serialising(req
, bdrv_get_cluster_size(bs
));
1049 if (!(flags
& BDRV_REQ_NO_SERIALISING
)) {
1050 wait_serialising_requests(req
);
1053 if (flags
& BDRV_REQ_COPY_ON_READ
) {
1054 int64_t start_sector
= offset
>> BDRV_SECTOR_BITS
;
1055 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
1056 unsigned int nb_sectors
= end_sector
- start_sector
;
1059 ret
= bdrv_is_allocated(bs
, start_sector
, nb_sectors
, &pnum
);
1064 if (!ret
|| pnum
!= nb_sectors
) {
1065 ret
= bdrv_co_do_copy_on_readv(child
, offset
, bytes
, qiov
);
1070 /* Forward the request to the BlockDriver, possibly fragmenting it */
1071 total_bytes
= bdrv_getlength(bs
);
1072 if (total_bytes
< 0) {
1077 max_bytes
= ROUND_UP(MAX(0, total_bytes
- offset
), align
);
1078 if (bytes
<= max_bytes
&& bytes
<= max_transfer
) {
1079 ret
= bdrv_driver_preadv(bs
, offset
, bytes
, qiov
, 0);
1083 while (bytes_remaining
) {
1087 QEMUIOVector local_qiov
;
1089 num
= MIN(bytes_remaining
, MIN(max_bytes
, max_transfer
));
1091 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1092 qemu_iovec_concat(&local_qiov
, qiov
, bytes
- bytes_remaining
, num
);
1094 ret
= bdrv_driver_preadv(bs
, offset
+ bytes
- bytes_remaining
,
1095 num
, &local_qiov
, 0);
1097 qemu_iovec_destroy(&local_qiov
);
1099 num
= bytes_remaining
;
1100 ret
= qemu_iovec_memset(qiov
, bytes
- bytes_remaining
, 0,
1106 bytes_remaining
-= num
;
1110 return ret
< 0 ? ret
: 0;
1114 * Handle a read request in coroutine context
1116 int coroutine_fn
bdrv_co_preadv(BdrvChild
*child
,
1117 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1118 BdrvRequestFlags flags
)
1120 BlockDriverState
*bs
= child
->bs
;
1121 BlockDriver
*drv
= bs
->drv
;
1122 BdrvTrackedRequest req
;
1124 uint64_t align
= bs
->bl
.request_alignment
;
1125 uint8_t *head_buf
= NULL
;
1126 uint8_t *tail_buf
= NULL
;
1127 QEMUIOVector local_qiov
;
1128 bool use_local_qiov
= false;
1135 ret
= bdrv_check_byte_request(bs
, offset
, bytes
);
1140 bdrv_inc_in_flight(bs
);
1142 /* Don't do copy-on-read if we read data before write operation */
1143 if (bs
->copy_on_read
&& !(flags
& BDRV_REQ_NO_SERIALISING
)) {
1144 flags
|= BDRV_REQ_COPY_ON_READ
;
1147 /* Align read if necessary by padding qiov */
1148 if (offset
& (align
- 1)) {
1149 head_buf
= qemu_blockalign(bs
, align
);
1150 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 2);
1151 qemu_iovec_add(&local_qiov
, head_buf
, offset
& (align
- 1));
1152 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1153 use_local_qiov
= true;
1155 bytes
+= offset
& (align
- 1);
1156 offset
= offset
& ~(align
- 1);
1159 if ((offset
+ bytes
) & (align
- 1)) {
1160 if (!use_local_qiov
) {
1161 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 1);
1162 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1163 use_local_qiov
= true;
1165 tail_buf
= qemu_blockalign(bs
, align
);
1166 qemu_iovec_add(&local_qiov
, tail_buf
,
1167 align
- ((offset
+ bytes
) & (align
- 1)));
1169 bytes
= ROUND_UP(bytes
, align
);
1172 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_READ
);
1173 ret
= bdrv_aligned_preadv(child
, &req
, offset
, bytes
, align
,
1174 use_local_qiov
? &local_qiov
: qiov
,
1176 tracked_request_end(&req
);
1177 bdrv_dec_in_flight(bs
);
1179 if (use_local_qiov
) {
1180 qemu_iovec_destroy(&local_qiov
);
1181 qemu_vfree(head_buf
);
1182 qemu_vfree(tail_buf
);
1188 static int coroutine_fn
bdrv_co_do_readv(BdrvChild
*child
,
1189 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
,
1190 BdrvRequestFlags flags
)
1192 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
1196 return bdrv_co_preadv(child
, sector_num
<< BDRV_SECTOR_BITS
,
1197 nb_sectors
<< BDRV_SECTOR_BITS
, qiov
, flags
);
1200 int coroutine_fn
bdrv_co_readv(BdrvChild
*child
, int64_t sector_num
,
1201 int nb_sectors
, QEMUIOVector
*qiov
)
1203 trace_bdrv_co_readv(child
->bs
, sector_num
, nb_sectors
);
1205 return bdrv_co_do_readv(child
, sector_num
, nb_sectors
, qiov
, 0);
1208 /* Maximum buffer for write zeroes fallback, in bytes */
1209 #define MAX_WRITE_ZEROES_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
1211 static int coroutine_fn
bdrv_co_do_pwrite_zeroes(BlockDriverState
*bs
,
1212 int64_t offset
, int count
, BdrvRequestFlags flags
)
1214 BlockDriver
*drv
= bs
->drv
;
1216 struct iovec iov
= {0};
1218 bool need_flush
= false;
1222 int max_write_zeroes
= MIN_NON_ZERO(bs
->bl
.max_pwrite_zeroes
, INT_MAX
);
1223 int alignment
= MAX(bs
->bl
.pwrite_zeroes_alignment
,
1224 bs
->bl
.request_alignment
);
1225 int max_transfer
= MIN_NON_ZERO(bs
->bl
.max_transfer
,
1226 MAX_WRITE_ZEROES_BOUNCE_BUFFER
);
1228 assert(alignment
% bs
->bl
.request_alignment
== 0);
1229 head
= offset
% alignment
;
1230 tail
= (offset
+ count
) % alignment
;
1231 max_write_zeroes
= QEMU_ALIGN_DOWN(max_write_zeroes
, alignment
);
1232 assert(max_write_zeroes
>= bs
->bl
.request_alignment
);
1234 while (count
> 0 && !ret
) {
1237 /* Align request. Block drivers can expect the "bulk" of the request
1238 * to be aligned, and that unaligned requests do not cross cluster
1242 /* Make a small request up to the first aligned sector. For
1243 * convenience, limit this request to max_transfer even if
1244 * we don't need to fall back to writes. */
1245 num
= MIN(MIN(count
, max_transfer
), alignment
- head
);
1246 head
= (head
+ num
) % alignment
;
1247 assert(num
< max_write_zeroes
);
1248 } else if (tail
&& num
> alignment
) {
1249 /* Shorten the request to the last aligned sector. */
1253 /* limit request size */
1254 if (num
> max_write_zeroes
) {
1255 num
= max_write_zeroes
;
1259 /* First try the efficient write zeroes operation */
1260 if (drv
->bdrv_co_pwrite_zeroes
) {
1261 ret
= drv
->bdrv_co_pwrite_zeroes(bs
, offset
, num
,
1262 flags
& bs
->supported_zero_flags
);
1263 if (ret
!= -ENOTSUP
&& (flags
& BDRV_REQ_FUA
) &&
1264 !(bs
->supported_zero_flags
& BDRV_REQ_FUA
)) {
1268 assert(!bs
->supported_zero_flags
);
1271 if (ret
== -ENOTSUP
) {
1272 /* Fall back to bounce buffer if write zeroes is unsupported */
1273 BdrvRequestFlags write_flags
= flags
& ~BDRV_REQ_ZERO_WRITE
;
1275 if ((flags
& BDRV_REQ_FUA
) &&
1276 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1277 /* No need for bdrv_driver_pwrite() to do a fallback
1278 * flush on each chunk; use just one at the end */
1279 write_flags
&= ~BDRV_REQ_FUA
;
1282 num
= MIN(num
, max_transfer
);
1284 if (iov
.iov_base
== NULL
) {
1285 iov
.iov_base
= qemu_try_blockalign(bs
, num
);
1286 if (iov
.iov_base
== NULL
) {
1290 memset(iov
.iov_base
, 0, num
);
1292 qemu_iovec_init_external(&qiov
, &iov
, 1);
1294 ret
= bdrv_driver_pwritev(bs
, offset
, num
, &qiov
, write_flags
);
1296 /* Keep bounce buffer around if it is big enough for all
1297 * all future requests.
1299 if (num
< max_transfer
) {
1300 qemu_vfree(iov
.iov_base
);
1301 iov
.iov_base
= NULL
;
1310 if (ret
== 0 && need_flush
) {
1311 ret
= bdrv_co_flush(bs
);
1313 qemu_vfree(iov
.iov_base
);
1318 * Forwards an already correctly aligned write request to the BlockDriver,
1319 * after possibly fragmenting it.
1321 static int coroutine_fn
bdrv_aligned_pwritev(BdrvChild
*child
,
1322 BdrvTrackedRequest
*req
, int64_t offset
, unsigned int bytes
,
1323 int64_t align
, QEMUIOVector
*qiov
, int flags
)
1325 BlockDriverState
*bs
= child
->bs
;
1326 BlockDriver
*drv
= bs
->drv
;
1330 int64_t start_sector
= offset
>> BDRV_SECTOR_BITS
;
1331 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
1332 uint64_t bytes_remaining
= bytes
;
1335 assert(is_power_of_2(align
));
1336 assert((offset
& (align
- 1)) == 0);
1337 assert((bytes
& (align
- 1)) == 0);
1338 assert(!qiov
|| bytes
== qiov
->size
);
1339 assert((bs
->open_flags
& BDRV_O_NO_IO
) == 0);
1340 assert(!(flags
& ~BDRV_REQ_MASK
));
1341 max_transfer
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_transfer
, INT_MAX
),
1344 waited
= wait_serialising_requests(req
);
1345 assert(!waited
|| !req
->serialising
);
1346 assert(req
->overlap_offset
<= offset
);
1347 assert(offset
+ bytes
<= req
->overlap_offset
+ req
->overlap_bytes
);
1348 /* FIXME: Block migration uses the BlockBackend of the guest device at a
1349 * point when it has not yet taken write permissions. This will be
1350 * fixed by a future patch, but for now we have to bypass this
1351 * assertion for block migration to work. */
1352 // assert(child->perm & BLK_PERM_WRITE);
1353 /* FIXME: Because of the above, we also cannot guarantee that all format
1354 * BDS take the BLK_PERM_RESIZE permission on their file BDS, since
1355 * they are not obligated to do so if they do not have any parent
1356 * that has taken the permission to write to them. */
1357 // assert(end_sector <= bs->total_sectors || child->perm & BLK_PERM_RESIZE);
1359 ret
= notifier_with_return_list_notify(&bs
->before_write_notifiers
, req
);
1361 if (!ret
&& bs
->detect_zeroes
!= BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
&&
1362 !(flags
& BDRV_REQ_ZERO_WRITE
) && drv
->bdrv_co_pwrite_zeroes
&&
1363 qemu_iovec_is_zero(qiov
)) {
1364 flags
|= BDRV_REQ_ZERO_WRITE
;
1365 if (bs
->detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
) {
1366 flags
|= BDRV_REQ_MAY_UNMAP
;
1371 /* Do nothing, write notifier decided to fail this request */
1372 } else if (flags
& BDRV_REQ_ZERO_WRITE
) {
1373 bdrv_debug_event(bs
, BLKDBG_PWRITEV_ZERO
);
1374 ret
= bdrv_co_do_pwrite_zeroes(bs
, offset
, bytes
, flags
);
1375 } else if (flags
& BDRV_REQ_WRITE_COMPRESSED
) {
1376 ret
= bdrv_driver_pwritev_compressed(bs
, offset
, bytes
, qiov
);
1377 } else if (bytes
<= max_transfer
) {
1378 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
1379 ret
= bdrv_driver_pwritev(bs
, offset
, bytes
, qiov
, flags
);
1381 bdrv_debug_event(bs
, BLKDBG_PWRITEV
);
1382 while (bytes_remaining
) {
1383 int num
= MIN(bytes_remaining
, max_transfer
);
1384 QEMUIOVector local_qiov
;
1385 int local_flags
= flags
;
1388 if (num
< bytes_remaining
&& (flags
& BDRV_REQ_FUA
) &&
1389 !(bs
->supported_write_flags
& BDRV_REQ_FUA
)) {
1390 /* If FUA is going to be emulated by flush, we only
1391 * need to flush on the last iteration */
1392 local_flags
&= ~BDRV_REQ_FUA
;
1394 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1395 qemu_iovec_concat(&local_qiov
, qiov
, bytes
- bytes_remaining
, num
);
1397 ret
= bdrv_driver_pwritev(bs
, offset
+ bytes
- bytes_remaining
,
1398 num
, &local_qiov
, local_flags
);
1399 qemu_iovec_destroy(&local_qiov
);
1403 bytes_remaining
-= num
;
1406 bdrv_debug_event(bs
, BLKDBG_PWRITEV_DONE
);
1409 bdrv_set_dirty(bs
, start_sector
, end_sector
- start_sector
);
1411 if (bs
->wr_highest_offset
< offset
+ bytes
) {
1412 bs
->wr_highest_offset
= offset
+ bytes
;
1416 bs
->total_sectors
= MAX(bs
->total_sectors
, end_sector
);
1423 static int coroutine_fn
bdrv_co_do_zero_pwritev(BdrvChild
*child
,
1426 BdrvRequestFlags flags
,
1427 BdrvTrackedRequest
*req
)
1429 BlockDriverState
*bs
= child
->bs
;
1430 uint8_t *buf
= NULL
;
1431 QEMUIOVector local_qiov
;
1433 uint64_t align
= bs
->bl
.request_alignment
;
1434 unsigned int head_padding_bytes
, tail_padding_bytes
;
1437 head_padding_bytes
= offset
& (align
- 1);
1438 tail_padding_bytes
= align
- ((offset
+ bytes
) & (align
- 1));
1441 assert(flags
& BDRV_REQ_ZERO_WRITE
);
1442 if (head_padding_bytes
|| tail_padding_bytes
) {
1443 buf
= qemu_blockalign(bs
, align
);
1444 iov
= (struct iovec
) {
1448 qemu_iovec_init_external(&local_qiov
, &iov
, 1);
1450 if (head_padding_bytes
) {
1451 uint64_t zero_bytes
= MIN(bytes
, align
- head_padding_bytes
);
1453 /* RMW the unaligned part before head. */
1454 mark_request_serialising(req
, align
);
1455 wait_serialising_requests(req
);
1456 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1457 ret
= bdrv_aligned_preadv(child
, req
, offset
& ~(align
- 1), align
,
1458 align
, &local_qiov
, 0);
1462 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1464 memset(buf
+ head_padding_bytes
, 0, zero_bytes
);
1465 ret
= bdrv_aligned_pwritev(child
, req
, offset
& ~(align
- 1), align
,
1467 flags
& ~BDRV_REQ_ZERO_WRITE
);
1471 offset
+= zero_bytes
;
1472 bytes
-= zero_bytes
;
1475 assert(!bytes
|| (offset
& (align
- 1)) == 0);
1476 if (bytes
>= align
) {
1477 /* Write the aligned part in the middle. */
1478 uint64_t aligned_bytes
= bytes
& ~(align
- 1);
1479 ret
= bdrv_aligned_pwritev(child
, req
, offset
, aligned_bytes
, align
,
1484 bytes
-= aligned_bytes
;
1485 offset
+= aligned_bytes
;
1488 assert(!bytes
|| (offset
& (align
- 1)) == 0);
1490 assert(align
== tail_padding_bytes
+ bytes
);
1491 /* RMW the unaligned part after tail. */
1492 mark_request_serialising(req
, align
);
1493 wait_serialising_requests(req
);
1494 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1495 ret
= bdrv_aligned_preadv(child
, req
, offset
, align
,
1496 align
, &local_qiov
, 0);
1500 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1502 memset(buf
, 0, bytes
);
1503 ret
= bdrv_aligned_pwritev(child
, req
, offset
, align
, align
,
1504 &local_qiov
, flags
& ~BDRV_REQ_ZERO_WRITE
);
1513 * Handle a write request in coroutine context
1515 int coroutine_fn
bdrv_co_pwritev(BdrvChild
*child
,
1516 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1517 BdrvRequestFlags flags
)
1519 BlockDriverState
*bs
= child
->bs
;
1520 BdrvTrackedRequest req
;
1521 uint64_t align
= bs
->bl
.request_alignment
;
1522 uint8_t *head_buf
= NULL
;
1523 uint8_t *tail_buf
= NULL
;
1524 QEMUIOVector local_qiov
;
1525 bool use_local_qiov
= false;
1531 if (bs
->read_only
) {
1534 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
1536 ret
= bdrv_check_byte_request(bs
, offset
, bytes
);
1541 bdrv_inc_in_flight(bs
);
1543 * Align write if necessary by performing a read-modify-write cycle.
1544 * Pad qiov with the read parts and be sure to have a tracked request not
1545 * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
1547 tracked_request_begin(&req
, bs
, offset
, bytes
, BDRV_TRACKED_WRITE
);
1550 ret
= bdrv_co_do_zero_pwritev(child
, offset
, bytes
, flags
, &req
);
1554 if (offset
& (align
- 1)) {
1555 QEMUIOVector head_qiov
;
1556 struct iovec head_iov
;
1558 mark_request_serialising(&req
, align
);
1559 wait_serialising_requests(&req
);
1561 head_buf
= qemu_blockalign(bs
, align
);
1562 head_iov
= (struct iovec
) {
1563 .iov_base
= head_buf
,
1566 qemu_iovec_init_external(&head_qiov
, &head_iov
, 1);
1568 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_HEAD
);
1569 ret
= bdrv_aligned_preadv(child
, &req
, offset
& ~(align
- 1), align
,
1570 align
, &head_qiov
, 0);
1574 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_HEAD
);
1576 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 2);
1577 qemu_iovec_add(&local_qiov
, head_buf
, offset
& (align
- 1));
1578 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1579 use_local_qiov
= true;
1581 bytes
+= offset
& (align
- 1);
1582 offset
= offset
& ~(align
- 1);
1584 /* We have read the tail already if the request is smaller
1585 * than one aligned block.
1587 if (bytes
< align
) {
1588 qemu_iovec_add(&local_qiov
, head_buf
+ bytes
, align
- bytes
);
1593 if ((offset
+ bytes
) & (align
- 1)) {
1594 QEMUIOVector tail_qiov
;
1595 struct iovec tail_iov
;
1599 mark_request_serialising(&req
, align
);
1600 waited
= wait_serialising_requests(&req
);
1601 assert(!waited
|| !use_local_qiov
);
1603 tail_buf
= qemu_blockalign(bs
, align
);
1604 tail_iov
= (struct iovec
) {
1605 .iov_base
= tail_buf
,
1608 qemu_iovec_init_external(&tail_qiov
, &tail_iov
, 1);
1610 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_TAIL
);
1611 ret
= bdrv_aligned_preadv(child
, &req
, (offset
+ bytes
) & ~(align
- 1),
1612 align
, align
, &tail_qiov
, 0);
1616 bdrv_debug_event(bs
, BLKDBG_PWRITEV_RMW_AFTER_TAIL
);
1618 if (!use_local_qiov
) {
1619 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 1);
1620 qemu_iovec_concat(&local_qiov
, qiov
, 0, qiov
->size
);
1621 use_local_qiov
= true;
1624 tail_bytes
= (offset
+ bytes
) & (align
- 1);
1625 qemu_iovec_add(&local_qiov
, tail_buf
+ tail_bytes
, align
- tail_bytes
);
1627 bytes
= ROUND_UP(bytes
, align
);
1630 ret
= bdrv_aligned_pwritev(child
, &req
, offset
, bytes
, align
,
1631 use_local_qiov
? &local_qiov
: qiov
,
1636 if (use_local_qiov
) {
1637 qemu_iovec_destroy(&local_qiov
);
1639 qemu_vfree(head_buf
);
1640 qemu_vfree(tail_buf
);
1642 tracked_request_end(&req
);
1643 bdrv_dec_in_flight(bs
);
1647 static int coroutine_fn
bdrv_co_do_writev(BdrvChild
*child
,
1648 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
,
1649 BdrvRequestFlags flags
)
1651 if (nb_sectors
< 0 || nb_sectors
> BDRV_REQUEST_MAX_SECTORS
) {
1655 return bdrv_co_pwritev(child
, sector_num
<< BDRV_SECTOR_BITS
,
1656 nb_sectors
<< BDRV_SECTOR_BITS
, qiov
, flags
);
1659 int coroutine_fn
bdrv_co_writev(BdrvChild
*child
, int64_t sector_num
,
1660 int nb_sectors
, QEMUIOVector
*qiov
)
1662 trace_bdrv_co_writev(child
->bs
, sector_num
, nb_sectors
);
1664 return bdrv_co_do_writev(child
, sector_num
, nb_sectors
, qiov
, 0);
1667 int coroutine_fn
bdrv_co_pwrite_zeroes(BdrvChild
*child
, int64_t offset
,
1668 int count
, BdrvRequestFlags flags
)
1670 trace_bdrv_co_pwrite_zeroes(child
->bs
, offset
, count
, flags
);
1672 if (!(child
->bs
->open_flags
& BDRV_O_UNMAP
)) {
1673 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1676 return bdrv_co_pwritev(child
, offset
, count
, NULL
,
1677 BDRV_REQ_ZERO_WRITE
| flags
);
1681 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
1683 int bdrv_flush_all(void)
1685 BdrvNextIterator it
;
1686 BlockDriverState
*bs
= NULL
;
1689 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1690 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
1693 aio_context_acquire(aio_context
);
1694 ret
= bdrv_flush(bs
);
1695 if (ret
< 0 && !result
) {
1698 aio_context_release(aio_context
);
1705 typedef struct BdrvCoGetBlockStatusData
{
1706 BlockDriverState
*bs
;
1707 BlockDriverState
*base
;
1708 BlockDriverState
**file
;
1714 } BdrvCoGetBlockStatusData
;
1717 * Returns the allocation status of the specified sectors.
1718 * Drivers not implementing the functionality are assumed to not support
1719 * backing files, hence all their sectors are reported as allocated.
1721 * If 'sector_num' is beyond the end of the disk image the return value is 0
1722 * and 'pnum' is set to 0.
1724 * 'pnum' is set to the number of sectors (including and immediately following
1725 * the specified sector) that are known to be in the same
1726 * allocated/unallocated state.
1728 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1729 * beyond the end of the disk image it will be clamped.
1731 * If returned value is positive and BDRV_BLOCK_OFFSET_VALID bit is set, 'file'
1732 * points to the BDS which the sector range is allocated in.
1734 static int64_t coroutine_fn
bdrv_co_get_block_status(BlockDriverState
*bs
,
1736 int nb_sectors
, int *pnum
,
1737 BlockDriverState
**file
)
1739 int64_t total_sectors
;
1743 total_sectors
= bdrv_nb_sectors(bs
);
1744 if (total_sectors
< 0) {
1745 return total_sectors
;
1748 if (sector_num
>= total_sectors
) {
1753 n
= total_sectors
- sector_num
;
1754 if (n
< nb_sectors
) {
1758 if (!bs
->drv
->bdrv_co_get_block_status
) {
1760 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
;
1761 if (bs
->drv
->protocol_name
) {
1762 ret
|= BDRV_BLOCK_OFFSET_VALID
| (sector_num
* BDRV_SECTOR_SIZE
);
1768 bdrv_inc_in_flight(bs
);
1769 ret
= bs
->drv
->bdrv_co_get_block_status(bs
, sector_num
, nb_sectors
, pnum
,
1776 if (ret
& BDRV_BLOCK_RAW
) {
1777 assert(ret
& BDRV_BLOCK_OFFSET_VALID
);
1778 ret
= bdrv_get_block_status(*file
, ret
>> BDRV_SECTOR_BITS
,
1783 if (ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ZERO
)) {
1784 ret
|= BDRV_BLOCK_ALLOCATED
;
1786 if (bdrv_unallocated_blocks_are_zero(bs
)) {
1787 ret
|= BDRV_BLOCK_ZERO
;
1788 } else if (bs
->backing
) {
1789 BlockDriverState
*bs2
= bs
->backing
->bs
;
1790 int64_t nb_sectors2
= bdrv_nb_sectors(bs2
);
1791 if (nb_sectors2
>= 0 && sector_num
>= nb_sectors2
) {
1792 ret
|= BDRV_BLOCK_ZERO
;
1797 if (*file
&& *file
!= bs
&&
1798 (ret
& BDRV_BLOCK_DATA
) && !(ret
& BDRV_BLOCK_ZERO
) &&
1799 (ret
& BDRV_BLOCK_OFFSET_VALID
)) {
1800 BlockDriverState
*file2
;
1803 ret2
= bdrv_co_get_block_status(*file
, ret
>> BDRV_SECTOR_BITS
,
1804 *pnum
, &file_pnum
, &file2
);
1806 /* Ignore errors. This is just providing extra information, it
1807 * is useful but not necessary.
1810 /* !file_pnum indicates an offset at or beyond the EOF; it is
1811 * perfectly valid for the format block driver to point to such
1812 * offsets, so catch it and mark everything as zero */
1813 ret
|= BDRV_BLOCK_ZERO
;
1815 /* Limit request to the range reported by the protocol driver */
1817 ret
|= (ret2
& BDRV_BLOCK_ZERO
);
1823 bdrv_dec_in_flight(bs
);
1827 static int64_t coroutine_fn
bdrv_co_get_block_status_above(BlockDriverState
*bs
,
1828 BlockDriverState
*base
,
1832 BlockDriverState
**file
)
1834 BlockDriverState
*p
;
1838 for (p
= bs
; p
!= base
; p
= backing_bs(p
)) {
1839 ret
= bdrv_co_get_block_status(p
, sector_num
, nb_sectors
, pnum
, file
);
1840 if (ret
< 0 || ret
& BDRV_BLOCK_ALLOCATED
) {
1843 /* [sector_num, pnum] unallocated on this layer, which could be only
1844 * the first part of [sector_num, nb_sectors]. */
1845 nb_sectors
= MIN(nb_sectors
, *pnum
);
1850 /* Coroutine wrapper for bdrv_get_block_status_above() */
1851 static void coroutine_fn
bdrv_get_block_status_above_co_entry(void *opaque
)
1853 BdrvCoGetBlockStatusData
*data
= opaque
;
1855 data
->ret
= bdrv_co_get_block_status_above(data
->bs
, data
->base
,
1864 * Synchronous wrapper around bdrv_co_get_block_status_above().
1866 * See bdrv_co_get_block_status_above() for details.
1868 int64_t bdrv_get_block_status_above(BlockDriverState
*bs
,
1869 BlockDriverState
*base
,
1871 int nb_sectors
, int *pnum
,
1872 BlockDriverState
**file
)
1875 BdrvCoGetBlockStatusData data
= {
1879 .sector_num
= sector_num
,
1880 .nb_sectors
= nb_sectors
,
1885 if (qemu_in_coroutine()) {
1886 /* Fast-path if already in coroutine context */
1887 bdrv_get_block_status_above_co_entry(&data
);
1889 co
= qemu_coroutine_create(bdrv_get_block_status_above_co_entry
,
1891 bdrv_coroutine_enter(bs
, co
);
1892 BDRV_POLL_WHILE(bs
, !data
.done
);
1897 int64_t bdrv_get_block_status(BlockDriverState
*bs
,
1899 int nb_sectors
, int *pnum
,
1900 BlockDriverState
**file
)
1902 return bdrv_get_block_status_above(bs
, backing_bs(bs
),
1903 sector_num
, nb_sectors
, pnum
, file
);
1906 int coroutine_fn
bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1907 int nb_sectors
, int *pnum
)
1909 BlockDriverState
*file
;
1910 int64_t ret
= bdrv_get_block_status(bs
, sector_num
, nb_sectors
, pnum
,
1915 return !!(ret
& BDRV_BLOCK_ALLOCATED
);
1919 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
1921 * Return true if the given sector is allocated in any image between
1922 * BASE and TOP (inclusive). BASE can be NULL to check if the given
1923 * sector is allocated in any image of the chain. Return false otherwise.
1925 * 'pnum' is set to the number of sectors (including and immediately following
1926 * the specified sector) that are known to be in the same
1927 * allocated/unallocated state.
1930 int bdrv_is_allocated_above(BlockDriverState
*top
,
1931 BlockDriverState
*base
,
1933 int nb_sectors
, int *pnum
)
1935 BlockDriverState
*intermediate
;
1936 int ret
, n
= nb_sectors
;
1939 while (intermediate
&& intermediate
!= base
) {
1941 ret
= bdrv_is_allocated(intermediate
, sector_num
, nb_sectors
,
1951 * [sector_num, nb_sectors] is unallocated on top but intermediate
1954 * [sector_num+x, nr_sectors] allocated.
1956 if (n
> pnum_inter
&&
1957 (intermediate
== top
||
1958 sector_num
+ pnum_inter
< intermediate
->total_sectors
)) {
1962 intermediate
= backing_bs(intermediate
);
1969 typedef struct BdrvVmstateCo
{
1970 BlockDriverState
*bs
;
1977 static int coroutine_fn
1978 bdrv_co_rw_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
,
1981 BlockDriver
*drv
= bs
->drv
;
1985 } else if (drv
->bdrv_load_vmstate
) {
1986 return is_read
? drv
->bdrv_load_vmstate(bs
, qiov
, pos
)
1987 : drv
->bdrv_save_vmstate(bs
, qiov
, pos
);
1988 } else if (bs
->file
) {
1989 return bdrv_co_rw_vmstate(bs
->file
->bs
, qiov
, pos
, is_read
);
1995 static void coroutine_fn
bdrv_co_rw_vmstate_entry(void *opaque
)
1997 BdrvVmstateCo
*co
= opaque
;
1998 co
->ret
= bdrv_co_rw_vmstate(co
->bs
, co
->qiov
, co
->pos
, co
->is_read
);
2002 bdrv_rw_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
,
2005 if (qemu_in_coroutine()) {
2006 return bdrv_co_rw_vmstate(bs
, qiov
, pos
, is_read
);
2008 BdrvVmstateCo data
= {
2013 .ret
= -EINPROGRESS
,
2015 Coroutine
*co
= qemu_coroutine_create(bdrv_co_rw_vmstate_entry
, &data
);
2017 bdrv_coroutine_enter(bs
, co
);
2018 while (data
.ret
== -EINPROGRESS
) {
2019 aio_poll(bdrv_get_aio_context(bs
), true);
2025 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2026 int64_t pos
, int size
)
2029 struct iovec iov
= {
2030 .iov_base
= (void *) buf
,
2035 qemu_iovec_init_external(&qiov
, &iov
, 1);
2037 ret
= bdrv_writev_vmstate(bs
, &qiov
, pos
);
2045 int bdrv_writev_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2047 return bdrv_rw_vmstate(bs
, qiov
, pos
, false);
2050 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2051 int64_t pos
, int size
)
2054 struct iovec iov
= {
2060 qemu_iovec_init_external(&qiov
, &iov
, 1);
2061 ret
= bdrv_readv_vmstate(bs
, &qiov
, pos
);
2069 int bdrv_readv_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
, int64_t pos
)
2071 return bdrv_rw_vmstate(bs
, qiov
, pos
, true);
2074 /**************************************************************/
2077 BlockAIOCB
*bdrv_aio_readv(BdrvChild
*child
, int64_t sector_num
,
2078 QEMUIOVector
*qiov
, int nb_sectors
,
2079 BlockCompletionFunc
*cb
, void *opaque
)
2081 trace_bdrv_aio_readv(child
->bs
, sector_num
, nb_sectors
, opaque
);
2083 assert(nb_sectors
<< BDRV_SECTOR_BITS
== qiov
->size
);
2084 return bdrv_co_aio_prw_vector(child
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
2085 0, cb
, opaque
, false);
2088 BlockAIOCB
*bdrv_aio_writev(BdrvChild
*child
, int64_t sector_num
,
2089 QEMUIOVector
*qiov
, int nb_sectors
,
2090 BlockCompletionFunc
*cb
, void *opaque
)
2092 trace_bdrv_aio_writev(child
->bs
, sector_num
, nb_sectors
, opaque
);
2094 assert(nb_sectors
<< BDRV_SECTOR_BITS
== qiov
->size
);
2095 return bdrv_co_aio_prw_vector(child
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
2096 0, cb
, opaque
, true);
2099 void bdrv_aio_cancel(BlockAIOCB
*acb
)
2102 bdrv_aio_cancel_async(acb
);
2103 while (acb
->refcnt
> 1) {
2104 if (acb
->aiocb_info
->get_aio_context
) {
2105 aio_poll(acb
->aiocb_info
->get_aio_context(acb
), true);
2106 } else if (acb
->bs
) {
2107 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2108 * assert that we're not using an I/O thread. Thread-safe
2109 * code should use bdrv_aio_cancel_async exclusively.
2111 assert(bdrv_get_aio_context(acb
->bs
) == qemu_get_aio_context());
2112 aio_poll(bdrv_get_aio_context(acb
->bs
), true);
2117 qemu_aio_unref(acb
);
2120 /* Async version of aio cancel. The caller is not blocked if the acb implements
2121 * cancel_async, otherwise we do nothing and let the request normally complete.
2122 * In either case the completion callback must be called. */
2123 void bdrv_aio_cancel_async(BlockAIOCB
*acb
)
2125 if (acb
->aiocb_info
->cancel_async
) {
2126 acb
->aiocb_info
->cancel_async(acb
);
2130 /**************************************************************/
2131 /* async block device emulation */
2133 typedef struct BlockRequest
{
2135 /* Used during read, write, trim */
2142 /* Used during ioctl */
2148 BlockCompletionFunc
*cb
;
2154 typedef struct BlockAIOCBCoroutine
{
2161 } BlockAIOCBCoroutine
;
2163 static const AIOCBInfo bdrv_em_co_aiocb_info
= {
2164 .aiocb_size
= sizeof(BlockAIOCBCoroutine
),
2167 static void bdrv_co_complete(BlockAIOCBCoroutine
*acb
)
2169 if (!acb
->need_bh
) {
2170 bdrv_dec_in_flight(acb
->common
.bs
);
2171 acb
->common
.cb(acb
->common
.opaque
, acb
->req
.error
);
2172 qemu_aio_unref(acb
);
2176 static void bdrv_co_em_bh(void *opaque
)
2178 BlockAIOCBCoroutine
*acb
= opaque
;
2180 assert(!acb
->need_bh
);
2181 bdrv_co_complete(acb
);
2184 static void bdrv_co_maybe_schedule_bh(BlockAIOCBCoroutine
*acb
)
2186 acb
->need_bh
= false;
2187 if (acb
->req
.error
!= -EINPROGRESS
) {
2188 BlockDriverState
*bs
= acb
->common
.bs
;
2190 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
), bdrv_co_em_bh
, acb
);
2194 /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2195 static void coroutine_fn
bdrv_co_do_rw(void *opaque
)
2197 BlockAIOCBCoroutine
*acb
= opaque
;
2199 if (!acb
->is_write
) {
2200 acb
->req
.error
= bdrv_co_preadv(acb
->child
, acb
->req
.offset
,
2201 acb
->req
.qiov
->size
, acb
->req
.qiov
, acb
->req
.flags
);
2203 acb
->req
.error
= bdrv_co_pwritev(acb
->child
, acb
->req
.offset
,
2204 acb
->req
.qiov
->size
, acb
->req
.qiov
, acb
->req
.flags
);
2207 bdrv_co_complete(acb
);
2210 static BlockAIOCB
*bdrv_co_aio_prw_vector(BdrvChild
*child
,
2213 BdrvRequestFlags flags
,
2214 BlockCompletionFunc
*cb
,
2219 BlockAIOCBCoroutine
*acb
;
2221 /* Matched by bdrv_co_complete's bdrv_dec_in_flight. */
2222 bdrv_inc_in_flight(child
->bs
);
2224 acb
= qemu_aio_get(&bdrv_em_co_aiocb_info
, child
->bs
, cb
, opaque
);
2226 acb
->need_bh
= true;
2227 acb
->req
.error
= -EINPROGRESS
;
2228 acb
->req
.offset
= offset
;
2229 acb
->req
.qiov
= qiov
;
2230 acb
->req
.flags
= flags
;
2231 acb
->is_write
= is_write
;
2233 co
= qemu_coroutine_create(bdrv_co_do_rw
, acb
);
2234 bdrv_coroutine_enter(child
->bs
, co
);
2236 bdrv_co_maybe_schedule_bh(acb
);
2237 return &acb
->common
;
2240 static void coroutine_fn
bdrv_aio_flush_co_entry(void *opaque
)
2242 BlockAIOCBCoroutine
*acb
= opaque
;
2243 BlockDriverState
*bs
= acb
->common
.bs
;
2245 acb
->req
.error
= bdrv_co_flush(bs
);
2246 bdrv_co_complete(acb
);
2249 BlockAIOCB
*bdrv_aio_flush(BlockDriverState
*bs
,
2250 BlockCompletionFunc
*cb
, void *opaque
)
2252 trace_bdrv_aio_flush(bs
, opaque
);
2255 BlockAIOCBCoroutine
*acb
;
2257 /* Matched by bdrv_co_complete's bdrv_dec_in_flight. */
2258 bdrv_inc_in_flight(bs
);
2260 acb
= qemu_aio_get(&bdrv_em_co_aiocb_info
, bs
, cb
, opaque
);
2261 acb
->need_bh
= true;
2262 acb
->req
.error
= -EINPROGRESS
;
2264 co
= qemu_coroutine_create(bdrv_aio_flush_co_entry
, acb
);
2265 bdrv_coroutine_enter(bs
, co
);
2267 bdrv_co_maybe_schedule_bh(acb
);
2268 return &acb
->common
;
2271 /**************************************************************/
2272 /* Coroutine block device emulation */
2274 typedef struct FlushCo
{
2275 BlockDriverState
*bs
;
2280 static void coroutine_fn
bdrv_flush_co_entry(void *opaque
)
2282 FlushCo
*rwco
= opaque
;
2284 rwco
->ret
= bdrv_co_flush(rwco
->bs
);
2287 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2292 bdrv_inc_in_flight(bs
);
2294 if (!bs
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
) ||
2299 current_gen
= bs
->write_gen
;
2301 /* Wait until any previous flushes are completed */
2302 while (bs
->active_flush_req
) {
2303 qemu_co_queue_wait(&bs
->flush_queue
, NULL
);
2306 bs
->active_flush_req
= true;
2308 /* Write back all layers by calling one driver function */
2309 if (bs
->drv
->bdrv_co_flush
) {
2310 ret
= bs
->drv
->bdrv_co_flush(bs
);
2314 /* Write back cached data to the OS even with cache=unsafe */
2315 BLKDBG_EVENT(bs
->file
, BLKDBG_FLUSH_TO_OS
);
2316 if (bs
->drv
->bdrv_co_flush_to_os
) {
2317 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
2323 /* But don't actually force it to the disk with cache=unsafe */
2324 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2328 /* Check if we really need to flush anything */
2329 if (bs
->flushed_gen
== current_gen
) {
2333 BLKDBG_EVENT(bs
->file
, BLKDBG_FLUSH_TO_DISK
);
2334 if (bs
->drv
->bdrv_co_flush_to_disk
) {
2335 ret
= bs
->drv
->bdrv_co_flush_to_disk(bs
);
2336 } else if (bs
->drv
->bdrv_aio_flush
) {
2338 CoroutineIOCompletion co
= {
2339 .coroutine
= qemu_coroutine_self(),
2342 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
2346 qemu_coroutine_yield();
2351 * Some block drivers always operate in either writethrough or unsafe
2352 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2353 * know how the server works (because the behaviour is hardcoded or
2354 * depends on server-side configuration), so we can't ensure that
2355 * everything is safe on disk. Returning an error doesn't work because
2356 * that would break guests even if the server operates in writethrough
2359 * Let's hope the user knows what he's doing.
2368 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2369 * in the case of cache=unsafe, so there are no useless flushes.
2372 ret
= bs
->file
? bdrv_co_flush(bs
->file
->bs
) : 0;
2374 /* Notify any pending flushes that we have completed */
2376 bs
->flushed_gen
= current_gen
;
2378 bs
->active_flush_req
= false;
2379 /* Return value is ignored - it's ok if wait queue is empty */
2380 qemu_co_queue_next(&bs
->flush_queue
);
2383 bdrv_dec_in_flight(bs
);
2387 int bdrv_flush(BlockDriverState
*bs
)
2390 FlushCo flush_co
= {
2395 if (qemu_in_coroutine()) {
2396 /* Fast-path if already in coroutine context */
2397 bdrv_flush_co_entry(&flush_co
);
2399 co
= qemu_coroutine_create(bdrv_flush_co_entry
, &flush_co
);
2400 bdrv_coroutine_enter(bs
, co
);
2401 BDRV_POLL_WHILE(bs
, flush_co
.ret
== NOT_DONE
);
2404 return flush_co
.ret
;
2407 typedef struct DiscardCo
{
2408 BlockDriverState
*bs
;
2413 static void coroutine_fn
bdrv_pdiscard_co_entry(void *opaque
)
2415 DiscardCo
*rwco
= opaque
;
2417 rwco
->ret
= bdrv_co_pdiscard(rwco
->bs
, rwco
->offset
, rwco
->count
);
2420 int coroutine_fn
bdrv_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
2423 BdrvTrackedRequest req
;
2424 int max_pdiscard
, ret
;
2425 int head
, tail
, align
;
2431 ret
= bdrv_check_byte_request(bs
, offset
, count
);
2434 } else if (bs
->read_only
) {
2437 assert(!(bs
->open_flags
& BDRV_O_INACTIVE
));
2439 /* Do nothing if disabled. */
2440 if (!(bs
->open_flags
& BDRV_O_UNMAP
)) {
2444 if (!bs
->drv
->bdrv_co_pdiscard
&& !bs
->drv
->bdrv_aio_pdiscard
) {
2448 /* Discard is advisory, but some devices track and coalesce
2449 * unaligned requests, so we must pass everything down rather than
2450 * round here. Still, most devices will just silently ignore
2451 * unaligned requests (by returning -ENOTSUP), so we must fragment
2452 * the request accordingly. */
2453 align
= MAX(bs
->bl
.pdiscard_alignment
, bs
->bl
.request_alignment
);
2454 assert(align
% bs
->bl
.request_alignment
== 0);
2455 head
= offset
% align
;
2456 tail
= (offset
+ count
) % align
;
2458 bdrv_inc_in_flight(bs
);
2459 tracked_request_begin(&req
, bs
, offset
, count
, BDRV_TRACKED_DISCARD
);
2461 ret
= notifier_with_return_list_notify(&bs
->before_write_notifiers
, &req
);
2466 max_pdiscard
= QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs
->bl
.max_pdiscard
, INT_MAX
),
2468 assert(max_pdiscard
>= bs
->bl
.request_alignment
);
2475 /* Make small requests to get to alignment boundaries. */
2476 num
= MIN(count
, align
- head
);
2477 if (!QEMU_IS_ALIGNED(num
, bs
->bl
.request_alignment
)) {
2478 num
%= bs
->bl
.request_alignment
;
2480 head
= (head
+ num
) % align
;
2481 assert(num
< max_pdiscard
);
2484 /* Shorten the request to the last aligned cluster. */
2486 } else if (!QEMU_IS_ALIGNED(tail
, bs
->bl
.request_alignment
) &&
2487 tail
> bs
->bl
.request_alignment
) {
2488 tail
%= bs
->bl
.request_alignment
;
2492 /* limit request size */
2493 if (num
> max_pdiscard
) {
2497 if (bs
->drv
->bdrv_co_pdiscard
) {
2498 ret
= bs
->drv
->bdrv_co_pdiscard(bs
, offset
, num
);
2501 CoroutineIOCompletion co
= {
2502 .coroutine
= qemu_coroutine_self(),
2505 acb
= bs
->drv
->bdrv_aio_pdiscard(bs
, offset
, num
,
2506 bdrv_co_io_em_complete
, &co
);
2511 qemu_coroutine_yield();
2515 if (ret
&& ret
!= -ENOTSUP
) {
2525 bdrv_set_dirty(bs
, req
.offset
>> BDRV_SECTOR_BITS
,
2526 req
.bytes
>> BDRV_SECTOR_BITS
);
2527 tracked_request_end(&req
);
2528 bdrv_dec_in_flight(bs
);
2532 int bdrv_pdiscard(BlockDriverState
*bs
, int64_t offset
, int count
)
2542 if (qemu_in_coroutine()) {
2543 /* Fast-path if already in coroutine context */
2544 bdrv_pdiscard_co_entry(&rwco
);
2546 co
= qemu_coroutine_create(bdrv_pdiscard_co_entry
, &rwco
);
2547 bdrv_coroutine_enter(bs
, co
);
2548 BDRV_POLL_WHILE(bs
, rwco
.ret
== NOT_DONE
);
2554 int bdrv_co_ioctl(BlockDriverState
*bs
, int req
, void *buf
)
2556 BlockDriver
*drv
= bs
->drv
;
2557 CoroutineIOCompletion co
= {
2558 .coroutine
= qemu_coroutine_self(),
2562 bdrv_inc_in_flight(bs
);
2563 if (!drv
|| (!drv
->bdrv_aio_ioctl
&& !drv
->bdrv_co_ioctl
)) {
2568 if (drv
->bdrv_co_ioctl
) {
2569 co
.ret
= drv
->bdrv_co_ioctl(bs
, req
, buf
);
2571 acb
= drv
->bdrv_aio_ioctl(bs
, req
, buf
, bdrv_co_io_em_complete
, &co
);
2576 qemu_coroutine_yield();
2579 bdrv_dec_in_flight(bs
);
2583 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
2585 return qemu_memalign(bdrv_opt_mem_align(bs
), size
);
2588 void *qemu_blockalign0(BlockDriverState
*bs
, size_t size
)
2590 return memset(qemu_blockalign(bs
, size
), 0, size
);
2593 void *qemu_try_blockalign(BlockDriverState
*bs
, size_t size
)
2595 size_t align
= bdrv_opt_mem_align(bs
);
2597 /* Ensure that NULL is never returned on success */
2603 return qemu_try_memalign(align
, size
);
2606 void *qemu_try_blockalign0(BlockDriverState
*bs
, size_t size
)
2608 void *mem
= qemu_try_blockalign(bs
, size
);
2611 memset(mem
, 0, size
);
2618 * Check if all memory in this vector is sector aligned.
2620 bool bdrv_qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
2623 size_t alignment
= bdrv_min_mem_align(bs
);
2625 for (i
= 0; i
< qiov
->niov
; i
++) {
2626 if ((uintptr_t) qiov
->iov
[i
].iov_base
% alignment
) {
2629 if (qiov
->iov
[i
].iov_len
% alignment
) {
2637 void bdrv_add_before_write_notifier(BlockDriverState
*bs
,
2638 NotifierWithReturn
*notifier
)
2640 notifier_with_return_list_add(&bs
->before_write_notifiers
, notifier
);
2643 void bdrv_io_plug(BlockDriverState
*bs
)
2647 QLIST_FOREACH(child
, &bs
->children
, next
) {
2648 bdrv_io_plug(child
->bs
);
2651 if (bs
->io_plugged
++ == 0) {
2652 BlockDriver
*drv
= bs
->drv
;
2653 if (drv
&& drv
->bdrv_io_plug
) {
2654 drv
->bdrv_io_plug(bs
);
2659 void bdrv_io_unplug(BlockDriverState
*bs
)
2663 assert(bs
->io_plugged
);
2664 if (--bs
->io_plugged
== 0) {
2665 BlockDriver
*drv
= bs
->drv
;
2666 if (drv
&& drv
->bdrv_io_unplug
) {
2667 drv
->bdrv_io_unplug(bs
);
2671 QLIST_FOREACH(child
, &bs
->children
, next
) {
2672 bdrv_io_unplug(child
->bs
);