ivshmem-server: Don't overload POSIX shmem and file name
[qemu/ar7.git] / block / io.c
blob41d954cad2e89a89ab2a63868529459aa5c3f34f
1 /*
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "trace.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/error-report.h"
33 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
35 static BlockAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
36 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
37 BlockCompletionFunc *cb, void *opaque);
38 static BlockAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
39 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
40 BlockCompletionFunc *cb, void *opaque);
41 static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
42 int64_t sector_num, int nb_sectors,
43 QEMUIOVector *iov);
44 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
45 int64_t sector_num, int nb_sectors,
46 QEMUIOVector *iov);
47 static BlockAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
48 int64_t sector_num,
49 QEMUIOVector *qiov,
50 int nb_sectors,
51 BdrvRequestFlags flags,
52 BlockCompletionFunc *cb,
53 void *opaque,
54 bool is_write);
55 static void coroutine_fn bdrv_co_do_rw(void *opaque);
56 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
57 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags);
59 /* throttling disk I/O limits */
60 void bdrv_set_io_limits(BlockDriverState *bs,
61 ThrottleConfig *cfg)
63 int i;
65 throttle_group_config(bs, cfg);
67 for (i = 0; i < 2; i++) {
68 qemu_co_enter_next(&bs->throttled_reqs[i]);
72 /* this function drain all the throttled IOs */
73 static bool bdrv_start_throttled_reqs(BlockDriverState *bs)
75 bool drained = false;
76 bool enabled = bs->io_limits_enabled;
77 int i;
79 bs->io_limits_enabled = false;
81 for (i = 0; i < 2; i++) {
82 while (qemu_co_enter_next(&bs->throttled_reqs[i])) {
83 drained = true;
87 bs->io_limits_enabled = enabled;
89 return drained;
92 void bdrv_io_limits_disable(BlockDriverState *bs)
94 bs->io_limits_enabled = false;
95 bdrv_start_throttled_reqs(bs);
96 throttle_group_unregister_bs(bs);
99 /* should be called before bdrv_set_io_limits if a limit is set */
100 void bdrv_io_limits_enable(BlockDriverState *bs, const char *group)
102 assert(!bs->io_limits_enabled);
103 throttle_group_register_bs(bs, group);
104 bs->io_limits_enabled = true;
107 void bdrv_io_limits_update_group(BlockDriverState *bs, const char *group)
109 /* this bs is not part of any group */
110 if (!bs->throttle_state) {
111 return;
114 /* this bs is a part of the same group than the one we want */
115 if (!g_strcmp0(throttle_group_get_name(bs), group)) {
116 return;
119 /* need to change the group this bs belong to */
120 bdrv_io_limits_disable(bs);
121 bdrv_io_limits_enable(bs, group);
124 void bdrv_setup_io_funcs(BlockDriver *bdrv)
126 /* Block drivers without coroutine functions need emulation */
127 if (!bdrv->bdrv_co_readv) {
128 bdrv->bdrv_co_readv = bdrv_co_readv_em;
129 bdrv->bdrv_co_writev = bdrv_co_writev_em;
131 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
132 * the block driver lacks aio we need to emulate that too.
134 if (!bdrv->bdrv_aio_readv) {
135 /* add AIO emulation layer */
136 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
137 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
142 void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
144 BlockDriver *drv = bs->drv;
145 Error *local_err = NULL;
147 memset(&bs->bl, 0, sizeof(bs->bl));
149 if (!drv) {
150 return;
153 /* Take some limits from the children as a default */
154 if (bs->file) {
155 bdrv_refresh_limits(bs->file->bs, &local_err);
156 if (local_err) {
157 error_propagate(errp, local_err);
158 return;
160 bs->bl.opt_transfer_length = bs->file->bs->bl.opt_transfer_length;
161 bs->bl.max_transfer_length = bs->file->bs->bl.max_transfer_length;
162 bs->bl.min_mem_alignment = bs->file->bs->bl.min_mem_alignment;
163 bs->bl.opt_mem_alignment = bs->file->bs->bl.opt_mem_alignment;
164 bs->bl.max_iov = bs->file->bs->bl.max_iov;
165 } else {
166 bs->bl.min_mem_alignment = 512;
167 bs->bl.opt_mem_alignment = getpagesize();
169 /* Safe default since most protocols use readv()/writev()/etc */
170 bs->bl.max_iov = IOV_MAX;
173 if (bs->backing) {
174 bdrv_refresh_limits(bs->backing->bs, &local_err);
175 if (local_err) {
176 error_propagate(errp, local_err);
177 return;
179 bs->bl.opt_transfer_length =
180 MAX(bs->bl.opt_transfer_length,
181 bs->backing->bs->bl.opt_transfer_length);
182 bs->bl.max_transfer_length =
183 MIN_NON_ZERO(bs->bl.max_transfer_length,
184 bs->backing->bs->bl.max_transfer_length);
185 bs->bl.opt_mem_alignment =
186 MAX(bs->bl.opt_mem_alignment,
187 bs->backing->bs->bl.opt_mem_alignment);
188 bs->bl.min_mem_alignment =
189 MAX(bs->bl.min_mem_alignment,
190 bs->backing->bs->bl.min_mem_alignment);
191 bs->bl.max_iov =
192 MIN(bs->bl.max_iov,
193 bs->backing->bs->bl.max_iov);
196 /* Then let the driver override it */
197 if (drv->bdrv_refresh_limits) {
198 drv->bdrv_refresh_limits(bs, errp);
203 * The copy-on-read flag is actually a reference count so multiple users may
204 * use the feature without worrying about clobbering its previous state.
205 * Copy-on-read stays enabled until all users have called to disable it.
207 void bdrv_enable_copy_on_read(BlockDriverState *bs)
209 bs->copy_on_read++;
212 void bdrv_disable_copy_on_read(BlockDriverState *bs)
214 assert(bs->copy_on_read > 0);
215 bs->copy_on_read--;
218 /* Check if any requests are in-flight (including throttled requests) */
219 bool bdrv_requests_pending(BlockDriverState *bs)
221 BdrvChild *child;
223 if (!QLIST_EMPTY(&bs->tracked_requests)) {
224 return true;
226 if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) {
227 return true;
229 if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) {
230 return true;
233 QLIST_FOREACH(child, &bs->children, next) {
234 if (bdrv_requests_pending(child->bs)) {
235 return true;
239 return false;
242 static void bdrv_drain_recurse(BlockDriverState *bs)
244 BdrvChild *child;
246 if (bs->drv && bs->drv->bdrv_drain) {
247 bs->drv->bdrv_drain(bs);
249 QLIST_FOREACH(child, &bs->children, next) {
250 bdrv_drain_recurse(child->bs);
255 * Wait for pending requests to complete on a single BlockDriverState subtree,
256 * and suspend block driver's internal I/O until next request arrives.
258 * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
259 * AioContext.
261 * Only this BlockDriverState's AioContext is run, so in-flight requests must
262 * not depend on events in other AioContexts. In that case, use
263 * bdrv_drain_all() instead.
265 void bdrv_drain(BlockDriverState *bs)
267 bool busy = true;
269 bdrv_drain_recurse(bs);
270 while (busy) {
271 /* Keep iterating */
272 bdrv_flush_io_queue(bs);
273 busy = bdrv_requests_pending(bs);
274 busy |= aio_poll(bdrv_get_aio_context(bs), busy);
279 * Wait for pending requests to complete across all BlockDriverStates
281 * This function does not flush data to disk, use bdrv_flush_all() for that
282 * after calling this function.
284 void bdrv_drain_all(void)
286 /* Always run first iteration so any pending completion BHs run */
287 bool busy = true;
288 BlockDriverState *bs = NULL;
289 GSList *aio_ctxs = NULL, *ctx;
291 while ((bs = bdrv_next(bs))) {
292 AioContext *aio_context = bdrv_get_aio_context(bs);
294 aio_context_acquire(aio_context);
295 if (bs->job) {
296 block_job_pause(bs->job);
298 bdrv_drain_recurse(bs);
299 aio_context_release(aio_context);
301 if (!g_slist_find(aio_ctxs, aio_context)) {
302 aio_ctxs = g_slist_prepend(aio_ctxs, aio_context);
306 /* Note that completion of an asynchronous I/O operation can trigger any
307 * number of other I/O operations on other devices---for example a
308 * coroutine can submit an I/O request to another device in response to
309 * request completion. Therefore we must keep looping until there was no
310 * more activity rather than simply draining each device independently.
312 while (busy) {
313 busy = false;
315 for (ctx = aio_ctxs; ctx != NULL; ctx = ctx->next) {
316 AioContext *aio_context = ctx->data;
317 bs = NULL;
319 aio_context_acquire(aio_context);
320 while ((bs = bdrv_next(bs))) {
321 if (aio_context == bdrv_get_aio_context(bs)) {
322 bdrv_flush_io_queue(bs);
323 if (bdrv_requests_pending(bs)) {
324 busy = true;
325 aio_poll(aio_context, busy);
329 busy |= aio_poll(aio_context, false);
330 aio_context_release(aio_context);
334 bs = NULL;
335 while ((bs = bdrv_next(bs))) {
336 AioContext *aio_context = bdrv_get_aio_context(bs);
338 aio_context_acquire(aio_context);
339 if (bs->job) {
340 block_job_resume(bs->job);
342 aio_context_release(aio_context);
344 g_slist_free(aio_ctxs);
348 * Remove an active request from the tracked requests list
350 * This function should be called when a tracked request is completing.
352 static void tracked_request_end(BdrvTrackedRequest *req)
354 if (req->serialising) {
355 req->bs->serialising_in_flight--;
358 QLIST_REMOVE(req, list);
359 qemu_co_queue_restart_all(&req->wait_queue);
363 * Add an active request to the tracked requests list
365 static void tracked_request_begin(BdrvTrackedRequest *req,
366 BlockDriverState *bs,
367 int64_t offset,
368 unsigned int bytes,
369 enum BdrvTrackedRequestType type)
371 *req = (BdrvTrackedRequest){
372 .bs = bs,
373 .offset = offset,
374 .bytes = bytes,
375 .type = type,
376 .co = qemu_coroutine_self(),
377 .serialising = false,
378 .overlap_offset = offset,
379 .overlap_bytes = bytes,
382 qemu_co_queue_init(&req->wait_queue);
384 QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
387 static void mark_request_serialising(BdrvTrackedRequest *req, uint64_t align)
389 int64_t overlap_offset = req->offset & ~(align - 1);
390 unsigned int overlap_bytes = ROUND_UP(req->offset + req->bytes, align)
391 - overlap_offset;
393 if (!req->serialising) {
394 req->bs->serialising_in_flight++;
395 req->serialising = true;
398 req->overlap_offset = MIN(req->overlap_offset, overlap_offset);
399 req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes);
403 * Round a region to cluster boundaries
405 void bdrv_round_to_clusters(BlockDriverState *bs,
406 int64_t sector_num, int nb_sectors,
407 int64_t *cluster_sector_num,
408 int *cluster_nb_sectors)
410 BlockDriverInfo bdi;
412 if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
413 *cluster_sector_num = sector_num;
414 *cluster_nb_sectors = nb_sectors;
415 } else {
416 int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE;
417 *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
418 *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
419 nb_sectors, c);
423 static int bdrv_get_cluster_size(BlockDriverState *bs)
425 BlockDriverInfo bdi;
426 int ret;
428 ret = bdrv_get_info(bs, &bdi);
429 if (ret < 0 || bdi.cluster_size == 0) {
430 return bs->request_alignment;
431 } else {
432 return bdi.cluster_size;
436 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
437 int64_t offset, unsigned int bytes)
439 /* aaaa bbbb */
440 if (offset >= req->overlap_offset + req->overlap_bytes) {
441 return false;
443 /* bbbb aaaa */
444 if (req->overlap_offset >= offset + bytes) {
445 return false;
447 return true;
450 static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
452 BlockDriverState *bs = self->bs;
453 BdrvTrackedRequest *req;
454 bool retry;
455 bool waited = false;
457 if (!bs->serialising_in_flight) {
458 return false;
461 do {
462 retry = false;
463 QLIST_FOREACH(req, &bs->tracked_requests, list) {
464 if (req == self || (!req->serialising && !self->serialising)) {
465 continue;
467 if (tracked_request_overlaps(req, self->overlap_offset,
468 self->overlap_bytes))
470 /* Hitting this means there was a reentrant request, for
471 * example, a block driver issuing nested requests. This must
472 * never happen since it means deadlock.
474 assert(qemu_coroutine_self() != req->co);
476 /* If the request is already (indirectly) waiting for us, or
477 * will wait for us as soon as it wakes up, then just go on
478 * (instead of producing a deadlock in the former case). */
479 if (!req->waiting_for) {
480 self->waiting_for = req;
481 qemu_co_queue_wait(&req->wait_queue);
482 self->waiting_for = NULL;
483 retry = true;
484 waited = true;
485 break;
489 } while (retry);
491 return waited;
494 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
495 size_t size)
497 if (size > BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS) {
498 return -EIO;
501 if (!bdrv_is_inserted(bs)) {
502 return -ENOMEDIUM;
505 if (offset < 0) {
506 return -EIO;
509 return 0;
512 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
513 int nb_sectors)
515 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
516 return -EIO;
519 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
520 nb_sectors * BDRV_SECTOR_SIZE);
523 typedef struct RwCo {
524 BlockDriverState *bs;
525 int64_t offset;
526 QEMUIOVector *qiov;
527 bool is_write;
528 int ret;
529 BdrvRequestFlags flags;
530 } RwCo;
532 static void coroutine_fn bdrv_rw_co_entry(void *opaque)
534 RwCo *rwco = opaque;
536 if (!rwco->is_write) {
537 rwco->ret = bdrv_co_do_preadv(rwco->bs, rwco->offset,
538 rwco->qiov->size, rwco->qiov,
539 rwco->flags);
540 } else {
541 rwco->ret = bdrv_co_do_pwritev(rwco->bs, rwco->offset,
542 rwco->qiov->size, rwco->qiov,
543 rwco->flags);
548 * Process a vectored synchronous request using coroutines
550 static int bdrv_prwv_co(BlockDriverState *bs, int64_t offset,
551 QEMUIOVector *qiov, bool is_write,
552 BdrvRequestFlags flags)
554 Coroutine *co;
555 RwCo rwco = {
556 .bs = bs,
557 .offset = offset,
558 .qiov = qiov,
559 .is_write = is_write,
560 .ret = NOT_DONE,
561 .flags = flags,
565 * In sync call context, when the vcpu is blocked, this throttling timer
566 * will not fire; so the I/O throttling function has to be disabled here
567 * if it has been enabled.
569 if (bs->io_limits_enabled) {
570 fprintf(stderr, "Disabling I/O throttling on '%s' due "
571 "to synchronous I/O.\n", bdrv_get_device_name(bs));
572 bdrv_io_limits_disable(bs);
575 if (qemu_in_coroutine()) {
576 /* Fast-path if already in coroutine context */
577 bdrv_rw_co_entry(&rwco);
578 } else {
579 AioContext *aio_context = bdrv_get_aio_context(bs);
581 co = qemu_coroutine_create(bdrv_rw_co_entry);
582 qemu_coroutine_enter(co, &rwco);
583 while (rwco.ret == NOT_DONE) {
584 aio_poll(aio_context, true);
587 return rwco.ret;
591 * Process a synchronous request using coroutines
593 static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
594 int nb_sectors, bool is_write, BdrvRequestFlags flags)
596 QEMUIOVector qiov;
597 struct iovec iov = {
598 .iov_base = (void *)buf,
599 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
602 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
603 return -EINVAL;
606 qemu_iovec_init_external(&qiov, &iov, 1);
607 return bdrv_prwv_co(bs, sector_num << BDRV_SECTOR_BITS,
608 &qiov, is_write, flags);
611 /* return < 0 if error. See bdrv_write() for the return codes */
612 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
613 uint8_t *buf, int nb_sectors)
615 return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0);
618 /* Return < 0 if error. Important errors are:
619 -EIO generic I/O error (may happen for all errors)
620 -ENOMEDIUM No media inserted.
621 -EINVAL Invalid sector number or nb_sectors
622 -EACCES Trying to write a read-only device
624 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
625 const uint8_t *buf, int nb_sectors)
627 return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0);
630 int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
631 int nb_sectors, BdrvRequestFlags flags)
633 return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true,
634 BDRV_REQ_ZERO_WRITE | flags);
638 * Completely zero out a block device with the help of bdrv_write_zeroes.
639 * The operation is sped up by checking the block status and only writing
640 * zeroes to the device if they currently do not return zeroes. Optional
641 * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP).
643 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
645 int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags)
647 int64_t target_sectors, ret, nb_sectors, sector_num = 0;
648 BlockDriverState *file;
649 int n;
651 target_sectors = bdrv_nb_sectors(bs);
652 if (target_sectors < 0) {
653 return target_sectors;
656 for (;;) {
657 nb_sectors = MIN(target_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
658 if (nb_sectors <= 0) {
659 return 0;
661 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n, &file);
662 if (ret < 0) {
663 error_report("error getting block status at sector %" PRId64 ": %s",
664 sector_num, strerror(-ret));
665 return ret;
667 if (ret & BDRV_BLOCK_ZERO) {
668 sector_num += n;
669 continue;
671 ret = bdrv_write_zeroes(bs, sector_num, n, flags);
672 if (ret < 0) {
673 error_report("error writing zeroes at sector %" PRId64 ": %s",
674 sector_num, strerror(-ret));
675 return ret;
677 sector_num += n;
681 int bdrv_pread(BlockDriverState *bs, int64_t offset, void *buf, int bytes)
683 QEMUIOVector qiov;
684 struct iovec iov = {
685 .iov_base = (void *)buf,
686 .iov_len = bytes,
688 int ret;
690 if (bytes < 0) {
691 return -EINVAL;
694 qemu_iovec_init_external(&qiov, &iov, 1);
695 ret = bdrv_prwv_co(bs, offset, &qiov, false, 0);
696 if (ret < 0) {
697 return ret;
700 return bytes;
703 int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov)
705 int ret;
707 ret = bdrv_prwv_co(bs, offset, qiov, true, 0);
708 if (ret < 0) {
709 return ret;
712 return qiov->size;
715 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
716 const void *buf, int bytes)
718 QEMUIOVector qiov;
719 struct iovec iov = {
720 .iov_base = (void *) buf,
721 .iov_len = bytes,
724 if (bytes < 0) {
725 return -EINVAL;
728 qemu_iovec_init_external(&qiov, &iov, 1);
729 return bdrv_pwritev(bs, offset, &qiov);
733 * Writes to the file and ensures that no writes are reordered across this
734 * request (acts as a barrier)
736 * Returns 0 on success, -errno in error cases.
738 int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
739 const void *buf, int count)
741 int ret;
743 ret = bdrv_pwrite(bs, offset, buf, count);
744 if (ret < 0) {
745 return ret;
748 /* No flush needed for cache modes that already do it */
749 if (bs->enable_write_cache) {
750 bdrv_flush(bs);
753 return 0;
756 static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
757 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
759 /* Perform I/O through a temporary buffer so that users who scribble over
760 * their read buffer while the operation is in progress do not end up
761 * modifying the image file. This is critical for zero-copy guest I/O
762 * where anything might happen inside guest memory.
764 void *bounce_buffer;
766 BlockDriver *drv = bs->drv;
767 struct iovec iov;
768 QEMUIOVector bounce_qiov;
769 int64_t cluster_sector_num;
770 int cluster_nb_sectors;
771 size_t skip_bytes;
772 int ret;
774 /* Cover entire cluster so no additional backing file I/O is required when
775 * allocating cluster in the image file.
777 bdrv_round_to_clusters(bs, sector_num, nb_sectors,
778 &cluster_sector_num, &cluster_nb_sectors);
780 trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors,
781 cluster_sector_num, cluster_nb_sectors);
783 iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE;
784 iov.iov_base = bounce_buffer = qemu_try_blockalign(bs, iov.iov_len);
785 if (bounce_buffer == NULL) {
786 ret = -ENOMEM;
787 goto err;
790 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
792 ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors,
793 &bounce_qiov);
794 if (ret < 0) {
795 goto err;
798 if (drv->bdrv_co_write_zeroes &&
799 buffer_is_zero(bounce_buffer, iov.iov_len)) {
800 ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num,
801 cluster_nb_sectors, 0);
802 } else {
803 /* This does not change the data on the disk, it is not necessary
804 * to flush even in cache=writethrough mode.
806 ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors,
807 &bounce_qiov);
810 if (ret < 0) {
811 /* It might be okay to ignore write errors for guest requests. If this
812 * is a deliberate copy-on-read then we don't want to ignore the error.
813 * Simply report it in all cases.
815 goto err;
818 skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE;
819 qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes,
820 nb_sectors * BDRV_SECTOR_SIZE);
822 err:
823 qemu_vfree(bounce_buffer);
824 return ret;
828 * Forwards an already correctly aligned request to the BlockDriver. This
829 * handles copy on read and zeroing after EOF; any other features must be
830 * implemented by the caller.
832 static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs,
833 BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
834 int64_t align, QEMUIOVector *qiov, int flags)
836 BlockDriver *drv = bs->drv;
837 int ret;
839 int64_t sector_num = offset >> BDRV_SECTOR_BITS;
840 unsigned int nb_sectors = bytes >> BDRV_SECTOR_BITS;
842 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
843 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
844 assert(!qiov || bytes == qiov->size);
846 /* Handle Copy on Read and associated serialisation */
847 if (flags & BDRV_REQ_COPY_ON_READ) {
848 /* If we touch the same cluster it counts as an overlap. This
849 * guarantees that allocating writes will be serialized and not race
850 * with each other for the same cluster. For example, in copy-on-read
851 * it ensures that the CoR read and write operations are atomic and
852 * guest writes cannot interleave between them. */
853 mark_request_serialising(req, bdrv_get_cluster_size(bs));
856 if (!(flags & BDRV_REQ_NO_SERIALISING)) {
857 wait_serialising_requests(req);
860 if (flags & BDRV_REQ_COPY_ON_READ) {
861 int pnum;
863 ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum);
864 if (ret < 0) {
865 goto out;
868 if (!ret || pnum != nb_sectors) {
869 ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov);
870 goto out;
874 /* Forward the request to the BlockDriver */
875 if (!bs->zero_beyond_eof) {
876 ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
877 } else {
878 /* Read zeros after EOF */
879 int64_t total_sectors, max_nb_sectors;
881 total_sectors = bdrv_nb_sectors(bs);
882 if (total_sectors < 0) {
883 ret = total_sectors;
884 goto out;
887 max_nb_sectors = ROUND_UP(MAX(0, total_sectors - sector_num),
888 align >> BDRV_SECTOR_BITS);
889 if (nb_sectors < max_nb_sectors) {
890 ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
891 } else if (max_nb_sectors > 0) {
892 QEMUIOVector local_qiov;
894 qemu_iovec_init(&local_qiov, qiov->niov);
895 qemu_iovec_concat(&local_qiov, qiov, 0,
896 max_nb_sectors * BDRV_SECTOR_SIZE);
898 ret = drv->bdrv_co_readv(bs, sector_num, max_nb_sectors,
899 &local_qiov);
901 qemu_iovec_destroy(&local_qiov);
902 } else {
903 ret = 0;
906 /* Reading beyond end of file is supposed to produce zeroes */
907 if (ret == 0 && total_sectors < sector_num + nb_sectors) {
908 uint64_t offset = MAX(0, total_sectors - sector_num);
909 uint64_t bytes = (sector_num + nb_sectors - offset) *
910 BDRV_SECTOR_SIZE;
911 qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes);
915 out:
916 return ret;
920 * Handle a read request in coroutine context
922 int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
923 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
924 BdrvRequestFlags flags)
926 BlockDriver *drv = bs->drv;
927 BdrvTrackedRequest req;
929 /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
930 uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
931 uint8_t *head_buf = NULL;
932 uint8_t *tail_buf = NULL;
933 QEMUIOVector local_qiov;
934 bool use_local_qiov = false;
935 int ret;
937 if (!drv) {
938 return -ENOMEDIUM;
941 ret = bdrv_check_byte_request(bs, offset, bytes);
942 if (ret < 0) {
943 return ret;
946 /* Don't do copy-on-read if we read data before write operation */
947 if (bs->copy_on_read && !(flags & BDRV_REQ_NO_SERIALISING)) {
948 flags |= BDRV_REQ_COPY_ON_READ;
951 /* throttling disk I/O */
952 if (bs->io_limits_enabled) {
953 throttle_group_co_io_limits_intercept(bs, bytes, false);
956 /* Align read if necessary by padding qiov */
957 if (offset & (align - 1)) {
958 head_buf = qemu_blockalign(bs, align);
959 qemu_iovec_init(&local_qiov, qiov->niov + 2);
960 qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
961 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
962 use_local_qiov = true;
964 bytes += offset & (align - 1);
965 offset = offset & ~(align - 1);
968 if ((offset + bytes) & (align - 1)) {
969 if (!use_local_qiov) {
970 qemu_iovec_init(&local_qiov, qiov->niov + 1);
971 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
972 use_local_qiov = true;
974 tail_buf = qemu_blockalign(bs, align);
975 qemu_iovec_add(&local_qiov, tail_buf,
976 align - ((offset + bytes) & (align - 1)));
978 bytes = ROUND_UP(bytes, align);
981 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_READ);
982 ret = bdrv_aligned_preadv(bs, &req, offset, bytes, align,
983 use_local_qiov ? &local_qiov : qiov,
984 flags);
985 tracked_request_end(&req);
987 if (use_local_qiov) {
988 qemu_iovec_destroy(&local_qiov);
989 qemu_vfree(head_buf);
990 qemu_vfree(tail_buf);
993 return ret;
996 static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
997 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
998 BdrvRequestFlags flags)
1000 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
1001 return -EINVAL;
1004 return bdrv_co_do_preadv(bs, sector_num << BDRV_SECTOR_BITS,
1005 nb_sectors << BDRV_SECTOR_BITS, qiov, flags);
1008 int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
1009 int nb_sectors, QEMUIOVector *qiov)
1011 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
1013 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0);
1016 int coroutine_fn bdrv_co_readv_no_serialising(BlockDriverState *bs,
1017 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1019 trace_bdrv_co_readv_no_serialising(bs, sector_num, nb_sectors);
1021 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
1022 BDRV_REQ_NO_SERIALISING);
1025 int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
1026 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1028 trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors);
1030 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
1031 BDRV_REQ_COPY_ON_READ);
1034 #define MAX_WRITE_ZEROES_BOUNCE_BUFFER 32768
1036 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
1037 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
1039 BlockDriver *drv = bs->drv;
1040 QEMUIOVector qiov;
1041 struct iovec iov = {0};
1042 int ret = 0;
1044 int max_write_zeroes = MIN_NON_ZERO(bs->bl.max_write_zeroes,
1045 BDRV_REQUEST_MAX_SECTORS);
1047 while (nb_sectors > 0 && !ret) {
1048 int num = nb_sectors;
1050 /* Align request. Block drivers can expect the "bulk" of the request
1051 * to be aligned.
1053 if (bs->bl.write_zeroes_alignment
1054 && num > bs->bl.write_zeroes_alignment) {
1055 if (sector_num % bs->bl.write_zeroes_alignment != 0) {
1056 /* Make a small request up to the first aligned sector. */
1057 num = bs->bl.write_zeroes_alignment;
1058 num -= sector_num % bs->bl.write_zeroes_alignment;
1059 } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) {
1060 /* Shorten the request to the last aligned sector. num cannot
1061 * underflow because num > bs->bl.write_zeroes_alignment.
1063 num -= (sector_num + num) % bs->bl.write_zeroes_alignment;
1067 /* limit request size */
1068 if (num > max_write_zeroes) {
1069 num = max_write_zeroes;
1072 ret = -ENOTSUP;
1073 /* First try the efficient write zeroes operation */
1074 if (drv->bdrv_co_write_zeroes) {
1075 ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags);
1078 if (ret == -ENOTSUP) {
1079 /* Fall back to bounce buffer if write zeroes is unsupported */
1080 int max_xfer_len = MIN_NON_ZERO(bs->bl.max_transfer_length,
1081 MAX_WRITE_ZEROES_BOUNCE_BUFFER);
1082 num = MIN(num, max_xfer_len);
1083 iov.iov_len = num * BDRV_SECTOR_SIZE;
1084 if (iov.iov_base == NULL) {
1085 iov.iov_base = qemu_try_blockalign(bs, num * BDRV_SECTOR_SIZE);
1086 if (iov.iov_base == NULL) {
1087 ret = -ENOMEM;
1088 goto fail;
1090 memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE);
1092 qemu_iovec_init_external(&qiov, &iov, 1);
1094 ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov);
1096 /* Keep bounce buffer around if it is big enough for all
1097 * all future requests.
1099 if (num < max_xfer_len) {
1100 qemu_vfree(iov.iov_base);
1101 iov.iov_base = NULL;
1105 sector_num += num;
1106 nb_sectors -= num;
1109 fail:
1110 qemu_vfree(iov.iov_base);
1111 return ret;
1115 * Forwards an already correctly aligned write request to the BlockDriver.
1117 static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
1118 BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
1119 QEMUIOVector *qiov, int flags)
1121 BlockDriver *drv = bs->drv;
1122 bool waited;
1123 int ret;
1125 int64_t sector_num = offset >> BDRV_SECTOR_BITS;
1126 unsigned int nb_sectors = bytes >> BDRV_SECTOR_BITS;
1128 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1129 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1130 assert(!qiov || bytes == qiov->size);
1132 waited = wait_serialising_requests(req);
1133 assert(!waited || !req->serialising);
1134 assert(req->overlap_offset <= offset);
1135 assert(offset + bytes <= req->overlap_offset + req->overlap_bytes);
1137 ret = notifier_with_return_list_notify(&bs->before_write_notifiers, req);
1139 if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF &&
1140 !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_write_zeroes &&
1141 qemu_iovec_is_zero(qiov)) {
1142 flags |= BDRV_REQ_ZERO_WRITE;
1143 if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) {
1144 flags |= BDRV_REQ_MAY_UNMAP;
1148 if (ret < 0) {
1149 /* Do nothing, write notifier decided to fail this request */
1150 } else if (flags & BDRV_REQ_ZERO_WRITE) {
1151 bdrv_debug_event(bs, BLKDBG_PWRITEV_ZERO);
1152 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags);
1153 } else {
1154 bdrv_debug_event(bs, BLKDBG_PWRITEV);
1155 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1157 bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
1159 if (ret == 0 && !bs->enable_write_cache) {
1160 ret = bdrv_co_flush(bs);
1163 bdrv_set_dirty(bs, sector_num, nb_sectors);
1165 if (bs->wr_highest_offset < offset + bytes) {
1166 bs->wr_highest_offset = offset + bytes;
1169 if (ret >= 0) {
1170 bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors);
1173 return ret;
1176 static int coroutine_fn bdrv_co_do_zero_pwritev(BlockDriverState *bs,
1177 int64_t offset,
1178 unsigned int bytes,
1179 BdrvRequestFlags flags,
1180 BdrvTrackedRequest *req)
1182 uint8_t *buf = NULL;
1183 QEMUIOVector local_qiov;
1184 struct iovec iov;
1185 uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
1186 unsigned int head_padding_bytes, tail_padding_bytes;
1187 int ret = 0;
1189 head_padding_bytes = offset & (align - 1);
1190 tail_padding_bytes = align - ((offset + bytes) & (align - 1));
1193 assert(flags & BDRV_REQ_ZERO_WRITE);
1194 if (head_padding_bytes || tail_padding_bytes) {
1195 buf = qemu_blockalign(bs, align);
1196 iov = (struct iovec) {
1197 .iov_base = buf,
1198 .iov_len = align,
1200 qemu_iovec_init_external(&local_qiov, &iov, 1);
1202 if (head_padding_bytes) {
1203 uint64_t zero_bytes = MIN(bytes, align - head_padding_bytes);
1205 /* RMW the unaligned part before head. */
1206 mark_request_serialising(req, align);
1207 wait_serialising_requests(req);
1208 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
1209 ret = bdrv_aligned_preadv(bs, req, offset & ~(align - 1), align,
1210 align, &local_qiov, 0);
1211 if (ret < 0) {
1212 goto fail;
1214 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
1216 memset(buf + head_padding_bytes, 0, zero_bytes);
1217 ret = bdrv_aligned_pwritev(bs, req, offset & ~(align - 1), align,
1218 &local_qiov,
1219 flags & ~BDRV_REQ_ZERO_WRITE);
1220 if (ret < 0) {
1221 goto fail;
1223 offset += zero_bytes;
1224 bytes -= zero_bytes;
1227 assert(!bytes || (offset & (align - 1)) == 0);
1228 if (bytes >= align) {
1229 /* Write the aligned part in the middle. */
1230 uint64_t aligned_bytes = bytes & ~(align - 1);
1231 ret = bdrv_aligned_pwritev(bs, req, offset, aligned_bytes,
1232 NULL, flags);
1233 if (ret < 0) {
1234 goto fail;
1236 bytes -= aligned_bytes;
1237 offset += aligned_bytes;
1240 assert(!bytes || (offset & (align - 1)) == 0);
1241 if (bytes) {
1242 assert(align == tail_padding_bytes + bytes);
1243 /* RMW the unaligned part after tail. */
1244 mark_request_serialising(req, align);
1245 wait_serialising_requests(req);
1246 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
1247 ret = bdrv_aligned_preadv(bs, req, offset, align,
1248 align, &local_qiov, 0);
1249 if (ret < 0) {
1250 goto fail;
1252 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
1254 memset(buf, 0, bytes);
1255 ret = bdrv_aligned_pwritev(bs, req, offset, align,
1256 &local_qiov, flags & ~BDRV_REQ_ZERO_WRITE);
1258 fail:
1259 qemu_vfree(buf);
1260 return ret;
1265 * Handle a write request in coroutine context
1267 int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
1268 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
1269 BdrvRequestFlags flags)
1271 BdrvTrackedRequest req;
1272 /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
1273 uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
1274 uint8_t *head_buf = NULL;
1275 uint8_t *tail_buf = NULL;
1276 QEMUIOVector local_qiov;
1277 bool use_local_qiov = false;
1278 int ret;
1280 if (!bs->drv) {
1281 return -ENOMEDIUM;
1283 if (bs->read_only) {
1284 return -EPERM;
1286 assert(!(bs->open_flags & BDRV_O_INACTIVE));
1288 ret = bdrv_check_byte_request(bs, offset, bytes);
1289 if (ret < 0) {
1290 return ret;
1293 /* throttling disk I/O */
1294 if (bs->io_limits_enabled) {
1295 throttle_group_co_io_limits_intercept(bs, bytes, true);
1299 * Align write if necessary by performing a read-modify-write cycle.
1300 * Pad qiov with the read parts and be sure to have a tracked request not
1301 * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
1303 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
1305 if (!qiov) {
1306 ret = bdrv_co_do_zero_pwritev(bs, offset, bytes, flags, &req);
1307 goto out;
1310 if (offset & (align - 1)) {
1311 QEMUIOVector head_qiov;
1312 struct iovec head_iov;
1314 mark_request_serialising(&req, align);
1315 wait_serialising_requests(&req);
1317 head_buf = qemu_blockalign(bs, align);
1318 head_iov = (struct iovec) {
1319 .iov_base = head_buf,
1320 .iov_len = align,
1322 qemu_iovec_init_external(&head_qiov, &head_iov, 1);
1324 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
1325 ret = bdrv_aligned_preadv(bs, &req, offset & ~(align - 1), align,
1326 align, &head_qiov, 0);
1327 if (ret < 0) {
1328 goto fail;
1330 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
1332 qemu_iovec_init(&local_qiov, qiov->niov + 2);
1333 qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
1334 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1335 use_local_qiov = true;
1337 bytes += offset & (align - 1);
1338 offset = offset & ~(align - 1);
1341 if ((offset + bytes) & (align - 1)) {
1342 QEMUIOVector tail_qiov;
1343 struct iovec tail_iov;
1344 size_t tail_bytes;
1345 bool waited;
1347 mark_request_serialising(&req, align);
1348 waited = wait_serialising_requests(&req);
1349 assert(!waited || !use_local_qiov);
1351 tail_buf = qemu_blockalign(bs, align);
1352 tail_iov = (struct iovec) {
1353 .iov_base = tail_buf,
1354 .iov_len = align,
1356 qemu_iovec_init_external(&tail_qiov, &tail_iov, 1);
1358 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
1359 ret = bdrv_aligned_preadv(bs, &req, (offset + bytes) & ~(align - 1), align,
1360 align, &tail_qiov, 0);
1361 if (ret < 0) {
1362 goto fail;
1364 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
1366 if (!use_local_qiov) {
1367 qemu_iovec_init(&local_qiov, qiov->niov + 1);
1368 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1369 use_local_qiov = true;
1372 tail_bytes = (offset + bytes) & (align - 1);
1373 qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, align - tail_bytes);
1375 bytes = ROUND_UP(bytes, align);
1378 ret = bdrv_aligned_pwritev(bs, &req, offset, bytes,
1379 use_local_qiov ? &local_qiov : qiov,
1380 flags);
1382 fail:
1384 if (use_local_qiov) {
1385 qemu_iovec_destroy(&local_qiov);
1387 qemu_vfree(head_buf);
1388 qemu_vfree(tail_buf);
1389 out:
1390 tracked_request_end(&req);
1391 return ret;
1394 static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
1395 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
1396 BdrvRequestFlags flags)
1398 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
1399 return -EINVAL;
1402 return bdrv_co_do_pwritev(bs, sector_num << BDRV_SECTOR_BITS,
1403 nb_sectors << BDRV_SECTOR_BITS, qiov, flags);
1406 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1407 int nb_sectors, QEMUIOVector *qiov)
1409 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1411 return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0);
1414 int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
1415 int64_t sector_num, int nb_sectors,
1416 BdrvRequestFlags flags)
1418 trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
1420 if (!(bs->open_flags & BDRV_O_UNMAP)) {
1421 flags &= ~BDRV_REQ_MAY_UNMAP;
1424 return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
1425 BDRV_REQ_ZERO_WRITE | flags);
1428 typedef struct BdrvCoGetBlockStatusData {
1429 BlockDriverState *bs;
1430 BlockDriverState *base;
1431 BlockDriverState **file;
1432 int64_t sector_num;
1433 int nb_sectors;
1434 int *pnum;
1435 int64_t ret;
1436 bool done;
1437 } BdrvCoGetBlockStatusData;
1440 * Returns the allocation status of the specified sectors.
1441 * Drivers not implementing the functionality are assumed to not support
1442 * backing files, hence all their sectors are reported as allocated.
1444 * If 'sector_num' is beyond the end of the disk image the return value is 0
1445 * and 'pnum' is set to 0.
1447 * 'pnum' is set to the number of sectors (including and immediately following
1448 * the specified sector) that are known to be in the same
1449 * allocated/unallocated state.
1451 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1452 * beyond the end of the disk image it will be clamped.
1454 * If returned value is positive and BDRV_BLOCK_OFFSET_VALID bit is set, 'file'
1455 * points to the BDS which the sector range is allocated in.
1457 static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
1458 int64_t sector_num,
1459 int nb_sectors, int *pnum,
1460 BlockDriverState **file)
1462 int64_t total_sectors;
1463 int64_t n;
1464 int64_t ret, ret2;
1466 total_sectors = bdrv_nb_sectors(bs);
1467 if (total_sectors < 0) {
1468 return total_sectors;
1471 if (sector_num >= total_sectors) {
1472 *pnum = 0;
1473 return 0;
1476 n = total_sectors - sector_num;
1477 if (n < nb_sectors) {
1478 nb_sectors = n;
1481 if (!bs->drv->bdrv_co_get_block_status) {
1482 *pnum = nb_sectors;
1483 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED;
1484 if (bs->drv->protocol_name) {
1485 ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE);
1487 return ret;
1490 *file = NULL;
1491 ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum,
1492 file);
1493 if (ret < 0) {
1494 *pnum = 0;
1495 return ret;
1498 if (ret & BDRV_BLOCK_RAW) {
1499 assert(ret & BDRV_BLOCK_OFFSET_VALID);
1500 return bdrv_get_block_status(bs->file->bs, ret >> BDRV_SECTOR_BITS,
1501 *pnum, pnum, file);
1504 if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
1505 ret |= BDRV_BLOCK_ALLOCATED;
1506 } else {
1507 if (bdrv_unallocated_blocks_are_zero(bs)) {
1508 ret |= BDRV_BLOCK_ZERO;
1509 } else if (bs->backing) {
1510 BlockDriverState *bs2 = bs->backing->bs;
1511 int64_t nb_sectors2 = bdrv_nb_sectors(bs2);
1512 if (nb_sectors2 >= 0 && sector_num >= nb_sectors2) {
1513 ret |= BDRV_BLOCK_ZERO;
1518 if (*file && *file != bs &&
1519 (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
1520 (ret & BDRV_BLOCK_OFFSET_VALID)) {
1521 BlockDriverState *file2;
1522 int file_pnum;
1524 ret2 = bdrv_co_get_block_status(*file, ret >> BDRV_SECTOR_BITS,
1525 *pnum, &file_pnum, &file2);
1526 if (ret2 >= 0) {
1527 /* Ignore errors. This is just providing extra information, it
1528 * is useful but not necessary.
1530 if (!file_pnum) {
1531 /* !file_pnum indicates an offset at or beyond the EOF; it is
1532 * perfectly valid for the format block driver to point to such
1533 * offsets, so catch it and mark everything as zero */
1534 ret |= BDRV_BLOCK_ZERO;
1535 } else {
1536 /* Limit request to the range reported by the protocol driver */
1537 *pnum = file_pnum;
1538 ret |= (ret2 & BDRV_BLOCK_ZERO);
1543 return ret;
1546 static int64_t coroutine_fn bdrv_co_get_block_status_above(BlockDriverState *bs,
1547 BlockDriverState *base,
1548 int64_t sector_num,
1549 int nb_sectors,
1550 int *pnum,
1551 BlockDriverState **file)
1553 BlockDriverState *p;
1554 int64_t ret = 0;
1556 assert(bs != base);
1557 for (p = bs; p != base; p = backing_bs(p)) {
1558 ret = bdrv_co_get_block_status(p, sector_num, nb_sectors, pnum, file);
1559 if (ret < 0 || ret & BDRV_BLOCK_ALLOCATED) {
1560 break;
1562 /* [sector_num, pnum] unallocated on this layer, which could be only
1563 * the first part of [sector_num, nb_sectors]. */
1564 nb_sectors = MIN(nb_sectors, *pnum);
1566 return ret;
1569 /* Coroutine wrapper for bdrv_get_block_status_above() */
1570 static void coroutine_fn bdrv_get_block_status_above_co_entry(void *opaque)
1572 BdrvCoGetBlockStatusData *data = opaque;
1574 data->ret = bdrv_co_get_block_status_above(data->bs, data->base,
1575 data->sector_num,
1576 data->nb_sectors,
1577 data->pnum,
1578 data->file);
1579 data->done = true;
1583 * Synchronous wrapper around bdrv_co_get_block_status_above().
1585 * See bdrv_co_get_block_status_above() for details.
1587 int64_t bdrv_get_block_status_above(BlockDriverState *bs,
1588 BlockDriverState *base,
1589 int64_t sector_num,
1590 int nb_sectors, int *pnum,
1591 BlockDriverState **file)
1593 Coroutine *co;
1594 BdrvCoGetBlockStatusData data = {
1595 .bs = bs,
1596 .base = base,
1597 .file = file,
1598 .sector_num = sector_num,
1599 .nb_sectors = nb_sectors,
1600 .pnum = pnum,
1601 .done = false,
1604 if (qemu_in_coroutine()) {
1605 /* Fast-path if already in coroutine context */
1606 bdrv_get_block_status_above_co_entry(&data);
1607 } else {
1608 AioContext *aio_context = bdrv_get_aio_context(bs);
1610 co = qemu_coroutine_create(bdrv_get_block_status_above_co_entry);
1611 qemu_coroutine_enter(co, &data);
1612 while (!data.done) {
1613 aio_poll(aio_context, true);
1616 return data.ret;
1619 int64_t bdrv_get_block_status(BlockDriverState *bs,
1620 int64_t sector_num,
1621 int nb_sectors, int *pnum,
1622 BlockDriverState **file)
1624 return bdrv_get_block_status_above(bs, backing_bs(bs),
1625 sector_num, nb_sectors, pnum, file);
1628 int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
1629 int nb_sectors, int *pnum)
1631 BlockDriverState *file;
1632 int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum,
1633 &file);
1634 if (ret < 0) {
1635 return ret;
1637 return !!(ret & BDRV_BLOCK_ALLOCATED);
1641 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
1643 * Return true if the given sector is allocated in any image between
1644 * BASE and TOP (inclusive). BASE can be NULL to check if the given
1645 * sector is allocated in any image of the chain. Return false otherwise.
1647 * 'pnum' is set to the number of sectors (including and immediately following
1648 * the specified sector) that are known to be in the same
1649 * allocated/unallocated state.
1652 int bdrv_is_allocated_above(BlockDriverState *top,
1653 BlockDriverState *base,
1654 int64_t sector_num,
1655 int nb_sectors, int *pnum)
1657 BlockDriverState *intermediate;
1658 int ret, n = nb_sectors;
1660 intermediate = top;
1661 while (intermediate && intermediate != base) {
1662 int pnum_inter;
1663 ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors,
1664 &pnum_inter);
1665 if (ret < 0) {
1666 return ret;
1667 } else if (ret) {
1668 *pnum = pnum_inter;
1669 return 1;
1673 * [sector_num, nb_sectors] is unallocated on top but intermediate
1674 * might have
1676 * [sector_num+x, nr_sectors] allocated.
1678 if (n > pnum_inter &&
1679 (intermediate == top ||
1680 sector_num + pnum_inter < intermediate->total_sectors)) {
1681 n = pnum_inter;
1684 intermediate = backing_bs(intermediate);
1687 *pnum = n;
1688 return 0;
1691 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1692 const uint8_t *buf, int nb_sectors)
1694 BlockDriver *drv = bs->drv;
1695 int ret;
1697 if (!drv) {
1698 return -ENOMEDIUM;
1700 if (!drv->bdrv_write_compressed) {
1701 return -ENOTSUP;
1703 ret = bdrv_check_request(bs, sector_num, nb_sectors);
1704 if (ret < 0) {
1705 return ret;
1708 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
1710 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1713 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1714 int64_t pos, int size)
1716 QEMUIOVector qiov;
1717 struct iovec iov = {
1718 .iov_base = (void *) buf,
1719 .iov_len = size,
1722 qemu_iovec_init_external(&qiov, &iov, 1);
1723 return bdrv_writev_vmstate(bs, &qiov, pos);
1726 int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
1728 BlockDriver *drv = bs->drv;
1730 if (!drv) {
1731 return -ENOMEDIUM;
1732 } else if (drv->bdrv_save_vmstate) {
1733 return drv->bdrv_save_vmstate(bs, qiov, pos);
1734 } else if (bs->file) {
1735 return bdrv_writev_vmstate(bs->file->bs, qiov, pos);
1738 return -ENOTSUP;
1741 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1742 int64_t pos, int size)
1744 BlockDriver *drv = bs->drv;
1745 if (!drv)
1746 return -ENOMEDIUM;
1747 if (drv->bdrv_load_vmstate)
1748 return drv->bdrv_load_vmstate(bs, buf, pos, size);
1749 if (bs->file)
1750 return bdrv_load_vmstate(bs->file->bs, buf, pos, size);
1751 return -ENOTSUP;
1754 /**************************************************************/
1755 /* async I/Os */
1757 BlockAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1758 QEMUIOVector *qiov, int nb_sectors,
1759 BlockCompletionFunc *cb, void *opaque)
1761 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
1763 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0,
1764 cb, opaque, false);
1767 BlockAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1768 QEMUIOVector *qiov, int nb_sectors,
1769 BlockCompletionFunc *cb, void *opaque)
1771 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
1773 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0,
1774 cb, opaque, true);
1777 BlockAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs,
1778 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags,
1779 BlockCompletionFunc *cb, void *opaque)
1781 trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque);
1783 return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors,
1784 BDRV_REQ_ZERO_WRITE | flags,
1785 cb, opaque, true);
1789 typedef struct MultiwriteCB {
1790 int error;
1791 int num_requests;
1792 int num_callbacks;
1793 struct {
1794 BlockCompletionFunc *cb;
1795 void *opaque;
1796 QEMUIOVector *free_qiov;
1797 } callbacks[];
1798 } MultiwriteCB;
1800 static void multiwrite_user_cb(MultiwriteCB *mcb)
1802 int i;
1804 for (i = 0; i < mcb->num_callbacks; i++) {
1805 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1806 if (mcb->callbacks[i].free_qiov) {
1807 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
1809 g_free(mcb->callbacks[i].free_qiov);
1813 static void multiwrite_cb(void *opaque, int ret)
1815 MultiwriteCB *mcb = opaque;
1817 trace_multiwrite_cb(mcb, ret);
1819 if (ret < 0 && !mcb->error) {
1820 mcb->error = ret;
1823 mcb->num_requests--;
1824 if (mcb->num_requests == 0) {
1825 multiwrite_user_cb(mcb);
1826 g_free(mcb);
1830 static int multiwrite_req_compare(const void *a, const void *b)
1832 const BlockRequest *req1 = a, *req2 = b;
1835 * Note that we can't simply subtract req2->sector from req1->sector
1836 * here as that could overflow the return value.
1838 if (req1->sector > req2->sector) {
1839 return 1;
1840 } else if (req1->sector < req2->sector) {
1841 return -1;
1842 } else {
1843 return 0;
1848 * Takes a bunch of requests and tries to merge them. Returns the number of
1849 * requests that remain after merging.
1851 static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1852 int num_reqs, MultiwriteCB *mcb)
1854 int i, outidx;
1856 // Sort requests by start sector
1857 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1859 // Check if adjacent requests touch the same clusters. If so, combine them,
1860 // filling up gaps with zero sectors.
1861 outidx = 0;
1862 for (i = 1; i < num_reqs; i++) {
1863 int merge = 0;
1864 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1866 // Handle exactly sequential writes and overlapping writes.
1867 if (reqs[i].sector <= oldreq_last) {
1868 merge = 1;
1871 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 >
1872 bs->bl.max_iov) {
1873 merge = 0;
1876 if (bs->bl.max_transfer_length && reqs[outidx].nb_sectors +
1877 reqs[i].nb_sectors > bs->bl.max_transfer_length) {
1878 merge = 0;
1881 if (merge) {
1882 size_t size;
1883 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
1884 qemu_iovec_init(qiov,
1885 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1887 // Add the first request to the merged one. If the requests are
1888 // overlapping, drop the last sectors of the first request.
1889 size = (reqs[i].sector - reqs[outidx].sector) << 9;
1890 qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size);
1892 // We should need to add any zeros between the two requests
1893 assert (reqs[i].sector <= oldreq_last);
1895 // Add the second request
1896 qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size);
1898 // Add tail of first request, if necessary
1899 if (qiov->size < reqs[outidx].qiov->size) {
1900 qemu_iovec_concat(qiov, reqs[outidx].qiov, qiov->size,
1901 reqs[outidx].qiov->size - qiov->size);
1904 reqs[outidx].nb_sectors = qiov->size >> 9;
1905 reqs[outidx].qiov = qiov;
1907 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1908 } else {
1909 outidx++;
1910 reqs[outidx].sector = reqs[i].sector;
1911 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1912 reqs[outidx].qiov = reqs[i].qiov;
1916 if (bs->blk) {
1917 block_acct_merge_done(blk_get_stats(bs->blk), BLOCK_ACCT_WRITE,
1918 num_reqs - outidx - 1);
1921 return outidx + 1;
1925 * Submit multiple AIO write requests at once.
1927 * On success, the function returns 0 and all requests in the reqs array have
1928 * been submitted. In error case this function returns -1, and any of the
1929 * requests may or may not be submitted yet. In particular, this means that the
1930 * callback will be called for some of the requests, for others it won't. The
1931 * caller must check the error field of the BlockRequest to wait for the right
1932 * callbacks (if error != 0, no callback will be called).
1934 * The implementation may modify the contents of the reqs array, e.g. to merge
1935 * requests. However, the fields opaque and error are left unmodified as they
1936 * are used to signal failure for a single request to the caller.
1938 int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1940 MultiwriteCB *mcb;
1941 int i;
1943 /* don't submit writes if we don't have a medium */
1944 if (bs->drv == NULL) {
1945 for (i = 0; i < num_reqs; i++) {
1946 reqs[i].error = -ENOMEDIUM;
1948 return -1;
1951 if (num_reqs == 0) {
1952 return 0;
1955 // Create MultiwriteCB structure
1956 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1957 mcb->num_requests = 0;
1958 mcb->num_callbacks = num_reqs;
1960 for (i = 0; i < num_reqs; i++) {
1961 mcb->callbacks[i].cb = reqs[i].cb;
1962 mcb->callbacks[i].opaque = reqs[i].opaque;
1965 // Check for mergable requests
1966 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
1968 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
1970 /* Run the aio requests. */
1971 mcb->num_requests = num_reqs;
1972 for (i = 0; i < num_reqs; i++) {
1973 bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov,
1974 reqs[i].nb_sectors, reqs[i].flags,
1975 multiwrite_cb, mcb,
1976 true);
1979 return 0;
1982 void bdrv_aio_cancel(BlockAIOCB *acb)
1984 qemu_aio_ref(acb);
1985 bdrv_aio_cancel_async(acb);
1986 while (acb->refcnt > 1) {
1987 if (acb->aiocb_info->get_aio_context) {
1988 aio_poll(acb->aiocb_info->get_aio_context(acb), true);
1989 } else if (acb->bs) {
1990 aio_poll(bdrv_get_aio_context(acb->bs), true);
1991 } else {
1992 abort();
1995 qemu_aio_unref(acb);
1998 /* Async version of aio cancel. The caller is not blocked if the acb implements
1999 * cancel_async, otherwise we do nothing and let the request normally complete.
2000 * In either case the completion callback must be called. */
2001 void bdrv_aio_cancel_async(BlockAIOCB *acb)
2003 if (acb->aiocb_info->cancel_async) {
2004 acb->aiocb_info->cancel_async(acb);
2008 /**************************************************************/
2009 /* async block device emulation */
2011 typedef struct BlockAIOCBSync {
2012 BlockAIOCB common;
2013 QEMUBH *bh;
2014 int ret;
2015 /* vector translation state */
2016 QEMUIOVector *qiov;
2017 uint8_t *bounce;
2018 int is_write;
2019 } BlockAIOCBSync;
2021 static const AIOCBInfo bdrv_em_aiocb_info = {
2022 .aiocb_size = sizeof(BlockAIOCBSync),
2025 static void bdrv_aio_bh_cb(void *opaque)
2027 BlockAIOCBSync *acb = opaque;
2029 if (!acb->is_write && acb->ret >= 0) {
2030 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
2032 qemu_vfree(acb->bounce);
2033 acb->common.cb(acb->common.opaque, acb->ret);
2034 qemu_bh_delete(acb->bh);
2035 acb->bh = NULL;
2036 qemu_aio_unref(acb);
2039 static BlockAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2040 int64_t sector_num,
2041 QEMUIOVector *qiov,
2042 int nb_sectors,
2043 BlockCompletionFunc *cb,
2044 void *opaque,
2045 int is_write)
2048 BlockAIOCBSync *acb;
2050 acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque);
2051 acb->is_write = is_write;
2052 acb->qiov = qiov;
2053 acb->bounce = qemu_try_blockalign(bs, qiov->size);
2054 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_aio_bh_cb, acb);
2056 if (acb->bounce == NULL) {
2057 acb->ret = -ENOMEM;
2058 } else if (is_write) {
2059 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
2060 acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2061 } else {
2062 acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2065 qemu_bh_schedule(acb->bh);
2067 return &acb->common;
2070 static BlockAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2071 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2072 BlockCompletionFunc *cb, void *opaque)
2074 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2077 static BlockAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2078 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2079 BlockCompletionFunc *cb, void *opaque)
2081 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2085 typedef struct BlockAIOCBCoroutine {
2086 BlockAIOCB common;
2087 BlockRequest req;
2088 bool is_write;
2089 bool need_bh;
2090 bool *done;
2091 QEMUBH* bh;
2092 } BlockAIOCBCoroutine;
2094 static const AIOCBInfo bdrv_em_co_aiocb_info = {
2095 .aiocb_size = sizeof(BlockAIOCBCoroutine),
2098 static void bdrv_co_complete(BlockAIOCBCoroutine *acb)
2100 if (!acb->need_bh) {
2101 acb->common.cb(acb->common.opaque, acb->req.error);
2102 qemu_aio_unref(acb);
2106 static void bdrv_co_em_bh(void *opaque)
2108 BlockAIOCBCoroutine *acb = opaque;
2110 assert(!acb->need_bh);
2111 qemu_bh_delete(acb->bh);
2112 bdrv_co_complete(acb);
2115 static void bdrv_co_maybe_schedule_bh(BlockAIOCBCoroutine *acb)
2117 acb->need_bh = false;
2118 if (acb->req.error != -EINPROGRESS) {
2119 BlockDriverState *bs = acb->common.bs;
2121 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_co_em_bh, acb);
2122 qemu_bh_schedule(acb->bh);
2126 /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2127 static void coroutine_fn bdrv_co_do_rw(void *opaque)
2129 BlockAIOCBCoroutine *acb = opaque;
2130 BlockDriverState *bs = acb->common.bs;
2132 if (!acb->is_write) {
2133 acb->req.error = bdrv_co_do_readv(bs, acb->req.sector,
2134 acb->req.nb_sectors, acb->req.qiov, acb->req.flags);
2135 } else {
2136 acb->req.error = bdrv_co_do_writev(bs, acb->req.sector,
2137 acb->req.nb_sectors, acb->req.qiov, acb->req.flags);
2140 bdrv_co_complete(acb);
2143 static BlockAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
2144 int64_t sector_num,
2145 QEMUIOVector *qiov,
2146 int nb_sectors,
2147 BdrvRequestFlags flags,
2148 BlockCompletionFunc *cb,
2149 void *opaque,
2150 bool is_write)
2152 Coroutine *co;
2153 BlockAIOCBCoroutine *acb;
2155 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
2156 acb->need_bh = true;
2157 acb->req.error = -EINPROGRESS;
2158 acb->req.sector = sector_num;
2159 acb->req.nb_sectors = nb_sectors;
2160 acb->req.qiov = qiov;
2161 acb->req.flags = flags;
2162 acb->is_write = is_write;
2164 co = qemu_coroutine_create(bdrv_co_do_rw);
2165 qemu_coroutine_enter(co, acb);
2167 bdrv_co_maybe_schedule_bh(acb);
2168 return &acb->common;
2171 static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
2173 BlockAIOCBCoroutine *acb = opaque;
2174 BlockDriverState *bs = acb->common.bs;
2176 acb->req.error = bdrv_co_flush(bs);
2177 bdrv_co_complete(acb);
2180 BlockAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2181 BlockCompletionFunc *cb, void *opaque)
2183 trace_bdrv_aio_flush(bs, opaque);
2185 Coroutine *co;
2186 BlockAIOCBCoroutine *acb;
2188 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
2189 acb->need_bh = true;
2190 acb->req.error = -EINPROGRESS;
2192 co = qemu_coroutine_create(bdrv_aio_flush_co_entry);
2193 qemu_coroutine_enter(co, acb);
2195 bdrv_co_maybe_schedule_bh(acb);
2196 return &acb->common;
2199 static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
2201 BlockAIOCBCoroutine *acb = opaque;
2202 BlockDriverState *bs = acb->common.bs;
2204 acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
2205 bdrv_co_complete(acb);
2208 BlockAIOCB *bdrv_aio_discard(BlockDriverState *bs,
2209 int64_t sector_num, int nb_sectors,
2210 BlockCompletionFunc *cb, void *opaque)
2212 Coroutine *co;
2213 BlockAIOCBCoroutine *acb;
2215 trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
2217 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
2218 acb->need_bh = true;
2219 acb->req.error = -EINPROGRESS;
2220 acb->req.sector = sector_num;
2221 acb->req.nb_sectors = nb_sectors;
2222 co = qemu_coroutine_create(bdrv_aio_discard_co_entry);
2223 qemu_coroutine_enter(co, acb);
2225 bdrv_co_maybe_schedule_bh(acb);
2226 return &acb->common;
2229 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
2230 BlockCompletionFunc *cb, void *opaque)
2232 BlockAIOCB *acb;
2234 acb = g_malloc(aiocb_info->aiocb_size);
2235 acb->aiocb_info = aiocb_info;
2236 acb->bs = bs;
2237 acb->cb = cb;
2238 acb->opaque = opaque;
2239 acb->refcnt = 1;
2240 return acb;
2243 void qemu_aio_ref(void *p)
2245 BlockAIOCB *acb = p;
2246 acb->refcnt++;
2249 void qemu_aio_unref(void *p)
2251 BlockAIOCB *acb = p;
2252 assert(acb->refcnt > 0);
2253 if (--acb->refcnt == 0) {
2254 g_free(acb);
2258 /**************************************************************/
2259 /* Coroutine block device emulation */
2261 typedef struct CoroutineIOCompletion {
2262 Coroutine *coroutine;
2263 int ret;
2264 } CoroutineIOCompletion;
2266 static void bdrv_co_io_em_complete(void *opaque, int ret)
2268 CoroutineIOCompletion *co = opaque;
2270 co->ret = ret;
2271 qemu_coroutine_enter(co->coroutine, NULL);
2274 static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
2275 int nb_sectors, QEMUIOVector *iov,
2276 bool is_write)
2278 CoroutineIOCompletion co = {
2279 .coroutine = qemu_coroutine_self(),
2281 BlockAIOCB *acb;
2283 if (is_write) {
2284 acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
2285 bdrv_co_io_em_complete, &co);
2286 } else {
2287 acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
2288 bdrv_co_io_em_complete, &co);
2291 trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
2292 if (!acb) {
2293 return -EIO;
2295 qemu_coroutine_yield();
2297 return co.ret;
2300 static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
2301 int64_t sector_num, int nb_sectors,
2302 QEMUIOVector *iov)
2304 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
2307 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
2308 int64_t sector_num, int nb_sectors,
2309 QEMUIOVector *iov)
2311 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
2314 static void coroutine_fn bdrv_flush_co_entry(void *opaque)
2316 RwCo *rwco = opaque;
2318 rwco->ret = bdrv_co_flush(rwco->bs);
2321 int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2323 int ret;
2324 BdrvTrackedRequest req;
2326 if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
2327 bdrv_is_sg(bs)) {
2328 return 0;
2331 tracked_request_begin(&req, bs, 0, 0, BDRV_TRACKED_FLUSH);
2332 /* Write back cached data to the OS even with cache=unsafe */
2333 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS);
2334 if (bs->drv->bdrv_co_flush_to_os) {
2335 ret = bs->drv->bdrv_co_flush_to_os(bs);
2336 if (ret < 0) {
2337 goto out;
2341 /* But don't actually force it to the disk with cache=unsafe */
2342 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2343 goto flush_parent;
2346 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
2347 if (bs->drv->bdrv_co_flush_to_disk) {
2348 ret = bs->drv->bdrv_co_flush_to_disk(bs);
2349 } else if (bs->drv->bdrv_aio_flush) {
2350 BlockAIOCB *acb;
2351 CoroutineIOCompletion co = {
2352 .coroutine = qemu_coroutine_self(),
2355 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
2356 if (acb == NULL) {
2357 ret = -EIO;
2358 } else {
2359 qemu_coroutine_yield();
2360 ret = co.ret;
2362 } else {
2364 * Some block drivers always operate in either writethrough or unsafe
2365 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2366 * know how the server works (because the behaviour is hardcoded or
2367 * depends on server-side configuration), so we can't ensure that
2368 * everything is safe on disk. Returning an error doesn't work because
2369 * that would break guests even if the server operates in writethrough
2370 * mode.
2372 * Let's hope the user knows what he's doing.
2374 ret = 0;
2376 if (ret < 0) {
2377 goto out;
2380 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2381 * in the case of cache=unsafe, so there are no useless flushes.
2383 flush_parent:
2384 ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0;
2385 out:
2386 tracked_request_end(&req);
2387 return ret;
2390 int bdrv_flush(BlockDriverState *bs)
2392 Coroutine *co;
2393 RwCo rwco = {
2394 .bs = bs,
2395 .ret = NOT_DONE,
2398 if (qemu_in_coroutine()) {
2399 /* Fast-path if already in coroutine context */
2400 bdrv_flush_co_entry(&rwco);
2401 } else {
2402 AioContext *aio_context = bdrv_get_aio_context(bs);
2404 co = qemu_coroutine_create(bdrv_flush_co_entry);
2405 qemu_coroutine_enter(co, &rwco);
2406 while (rwco.ret == NOT_DONE) {
2407 aio_poll(aio_context, true);
2411 return rwco.ret;
2414 typedef struct DiscardCo {
2415 BlockDriverState *bs;
2416 int64_t sector_num;
2417 int nb_sectors;
2418 int ret;
2419 } DiscardCo;
2420 static void coroutine_fn bdrv_discard_co_entry(void *opaque)
2422 DiscardCo *rwco = opaque;
2424 rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
2427 int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
2428 int nb_sectors)
2430 BdrvTrackedRequest req;
2431 int max_discard, ret;
2433 if (!bs->drv) {
2434 return -ENOMEDIUM;
2437 ret = bdrv_check_request(bs, sector_num, nb_sectors);
2438 if (ret < 0) {
2439 return ret;
2440 } else if (bs->read_only) {
2441 return -EPERM;
2443 assert(!(bs->open_flags & BDRV_O_INACTIVE));
2445 /* Do nothing if disabled. */
2446 if (!(bs->open_flags & BDRV_O_UNMAP)) {
2447 return 0;
2450 if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) {
2451 return 0;
2454 tracked_request_begin(&req, bs, sector_num, nb_sectors,
2455 BDRV_TRACKED_DISCARD);
2456 bdrv_set_dirty(bs, sector_num, nb_sectors);
2458 max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
2459 while (nb_sectors > 0) {
2460 int ret;
2461 int num = nb_sectors;
2463 /* align request */
2464 if (bs->bl.discard_alignment &&
2465 num >= bs->bl.discard_alignment &&
2466 sector_num % bs->bl.discard_alignment) {
2467 if (num > bs->bl.discard_alignment) {
2468 num = bs->bl.discard_alignment;
2470 num -= sector_num % bs->bl.discard_alignment;
2473 /* limit request size */
2474 if (num > max_discard) {
2475 num = max_discard;
2478 if (bs->drv->bdrv_co_discard) {
2479 ret = bs->drv->bdrv_co_discard(bs, sector_num, num);
2480 } else {
2481 BlockAIOCB *acb;
2482 CoroutineIOCompletion co = {
2483 .coroutine = qemu_coroutine_self(),
2486 acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
2487 bdrv_co_io_em_complete, &co);
2488 if (acb == NULL) {
2489 ret = -EIO;
2490 goto out;
2491 } else {
2492 qemu_coroutine_yield();
2493 ret = co.ret;
2496 if (ret && ret != -ENOTSUP) {
2497 goto out;
2500 sector_num += num;
2501 nb_sectors -= num;
2503 ret = 0;
2504 out:
2505 tracked_request_end(&req);
2506 return ret;
2509 int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
2511 Coroutine *co;
2512 DiscardCo rwco = {
2513 .bs = bs,
2514 .sector_num = sector_num,
2515 .nb_sectors = nb_sectors,
2516 .ret = NOT_DONE,
2519 if (qemu_in_coroutine()) {
2520 /* Fast-path if already in coroutine context */
2521 bdrv_discard_co_entry(&rwco);
2522 } else {
2523 AioContext *aio_context = bdrv_get_aio_context(bs);
2525 co = qemu_coroutine_create(bdrv_discard_co_entry);
2526 qemu_coroutine_enter(co, &rwco);
2527 while (rwco.ret == NOT_DONE) {
2528 aio_poll(aio_context, true);
2532 return rwco.ret;
2535 typedef struct {
2536 CoroutineIOCompletion *co;
2537 QEMUBH *bh;
2538 } BdrvIoctlCompletionData;
2540 static void bdrv_ioctl_bh_cb(void *opaque)
2542 BdrvIoctlCompletionData *data = opaque;
2544 bdrv_co_io_em_complete(data->co, -ENOTSUP);
2545 qemu_bh_delete(data->bh);
2548 static int bdrv_co_do_ioctl(BlockDriverState *bs, int req, void *buf)
2550 BlockDriver *drv = bs->drv;
2551 BdrvTrackedRequest tracked_req;
2552 CoroutineIOCompletion co = {
2553 .coroutine = qemu_coroutine_self(),
2555 BlockAIOCB *acb;
2557 tracked_request_begin(&tracked_req, bs, 0, 0, BDRV_TRACKED_IOCTL);
2558 if (!drv || !drv->bdrv_aio_ioctl) {
2559 co.ret = -ENOTSUP;
2560 goto out;
2563 acb = drv->bdrv_aio_ioctl(bs, req, buf, bdrv_co_io_em_complete, &co);
2564 if (!acb) {
2565 BdrvIoctlCompletionData *data = g_new(BdrvIoctlCompletionData, 1);
2566 data->bh = aio_bh_new(bdrv_get_aio_context(bs),
2567 bdrv_ioctl_bh_cb, data);
2568 data->co = &co;
2569 qemu_bh_schedule(data->bh);
2571 qemu_coroutine_yield();
2572 out:
2573 tracked_request_end(&tracked_req);
2574 return co.ret;
2577 typedef struct {
2578 BlockDriverState *bs;
2579 int req;
2580 void *buf;
2581 int ret;
2582 } BdrvIoctlCoData;
2584 static void coroutine_fn bdrv_co_ioctl_entry(void *opaque)
2586 BdrvIoctlCoData *data = opaque;
2587 data->ret = bdrv_co_do_ioctl(data->bs, data->req, data->buf);
2590 /* needed for generic scsi interface */
2591 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2593 BdrvIoctlCoData data = {
2594 .bs = bs,
2595 .req = req,
2596 .buf = buf,
2597 .ret = -EINPROGRESS,
2600 if (qemu_in_coroutine()) {
2601 /* Fast-path if already in coroutine context */
2602 bdrv_co_ioctl_entry(&data);
2603 } else {
2604 Coroutine *co = qemu_coroutine_create(bdrv_co_ioctl_entry);
2606 qemu_coroutine_enter(co, &data);
2607 while (data.ret == -EINPROGRESS) {
2608 aio_poll(bdrv_get_aio_context(bs), true);
2611 return data.ret;
2614 static void coroutine_fn bdrv_co_aio_ioctl_entry(void *opaque)
2616 BlockAIOCBCoroutine *acb = opaque;
2617 acb->req.error = bdrv_co_do_ioctl(acb->common.bs,
2618 acb->req.req, acb->req.buf);
2619 bdrv_co_complete(acb);
2622 BlockAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2623 unsigned long int req, void *buf,
2624 BlockCompletionFunc *cb, void *opaque)
2626 BlockAIOCBCoroutine *acb = qemu_aio_get(&bdrv_em_co_aiocb_info,
2627 bs, cb, opaque);
2628 Coroutine *co;
2630 acb->need_bh = true;
2631 acb->req.error = -EINPROGRESS;
2632 acb->req.req = req;
2633 acb->req.buf = buf;
2634 co = qemu_coroutine_create(bdrv_co_aio_ioctl_entry);
2635 qemu_coroutine_enter(co, acb);
2637 bdrv_co_maybe_schedule_bh(acb);
2638 return &acb->common;
2641 void *qemu_blockalign(BlockDriverState *bs, size_t size)
2643 return qemu_memalign(bdrv_opt_mem_align(bs), size);
2646 void *qemu_blockalign0(BlockDriverState *bs, size_t size)
2648 return memset(qemu_blockalign(bs, size), 0, size);
2651 void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
2653 size_t align = bdrv_opt_mem_align(bs);
2655 /* Ensure that NULL is never returned on success */
2656 assert(align > 0);
2657 if (size == 0) {
2658 size = align;
2661 return qemu_try_memalign(align, size);
2664 void *qemu_try_blockalign0(BlockDriverState *bs, size_t size)
2666 void *mem = qemu_try_blockalign(bs, size);
2668 if (mem) {
2669 memset(mem, 0, size);
2672 return mem;
2676 * Check if all memory in this vector is sector aligned.
2678 bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
2680 int i;
2681 size_t alignment = bdrv_min_mem_align(bs);
2683 for (i = 0; i < qiov->niov; i++) {
2684 if ((uintptr_t) qiov->iov[i].iov_base % alignment) {
2685 return false;
2687 if (qiov->iov[i].iov_len % alignment) {
2688 return false;
2692 return true;
2695 void bdrv_add_before_write_notifier(BlockDriverState *bs,
2696 NotifierWithReturn *notifier)
2698 notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
2701 void bdrv_io_plug(BlockDriverState *bs)
2703 BlockDriver *drv = bs->drv;
2704 if (drv && drv->bdrv_io_plug) {
2705 drv->bdrv_io_plug(bs);
2706 } else if (bs->file) {
2707 bdrv_io_plug(bs->file->bs);
2711 void bdrv_io_unplug(BlockDriverState *bs)
2713 BlockDriver *drv = bs->drv;
2714 if (drv && drv->bdrv_io_unplug) {
2715 drv->bdrv_io_unplug(bs);
2716 } else if (bs->file) {
2717 bdrv_io_unplug(bs->file->bs);
2721 void bdrv_flush_io_queue(BlockDriverState *bs)
2723 BlockDriver *drv = bs->drv;
2724 if (drv && drv->bdrv_flush_io_queue) {
2725 drv->bdrv_flush_io_queue(bs);
2726 } else if (bs->file) {
2727 bdrv_flush_io_queue(bs->file->bs);
2729 bdrv_start_throttled_reqs(bs);
2732 void bdrv_drained_begin(BlockDriverState *bs)
2734 if (!bs->quiesce_counter++) {
2735 aio_disable_external(bdrv_get_aio_context(bs));
2737 bdrv_drain(bs);
2740 void bdrv_drained_end(BlockDriverState *bs)
2742 assert(bs->quiesce_counter > 0);
2743 if (--bs->quiesce_counter > 0) {
2744 return;
2746 aio_enable_external(bdrv_get_aio_context(bs));