]> git.proxmox.com Git - mirror_qemu.git/blob - block/io.c
block: Fragment writes to max transfer length
[mirror_qemu.git] / block / io.c
1 /*
2 * Block layer I/O functions
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
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 "qemu/cutils.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33
34 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
35
36 static BlockAIOCB *bdrv_co_aio_rw_vector(BdrvChild *child,
37 int64_t sector_num,
38 QEMUIOVector *qiov,
39 int nb_sectors,
40 BdrvRequestFlags flags,
41 BlockCompletionFunc *cb,
42 void *opaque,
43 bool is_write);
44 static void coroutine_fn bdrv_co_do_rw(void *opaque);
45 static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
46 int64_t offset, int count, BdrvRequestFlags flags);
47
48 static void bdrv_parent_drained_begin(BlockDriverState *bs)
49 {
50 BdrvChild *c;
51
52 QLIST_FOREACH(c, &bs->parents, next_parent) {
53 if (c->role->drained_begin) {
54 c->role->drained_begin(c);
55 }
56 }
57 }
58
59 static void bdrv_parent_drained_end(BlockDriverState *bs)
60 {
61 BdrvChild *c;
62
63 QLIST_FOREACH(c, &bs->parents, next_parent) {
64 if (c->role->drained_end) {
65 c->role->drained_end(c);
66 }
67 }
68 }
69
70 static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src)
71 {
72 dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer);
73 dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer);
74 dst->opt_mem_alignment = MAX(dst->opt_mem_alignment,
75 src->opt_mem_alignment);
76 dst->min_mem_alignment = MAX(dst->min_mem_alignment,
77 src->min_mem_alignment);
78 dst->max_iov = MIN_NON_ZERO(dst->max_iov, src->max_iov);
79 }
80
81 void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
82 {
83 BlockDriver *drv = bs->drv;
84 Error *local_err = NULL;
85
86 memset(&bs->bl, 0, sizeof(bs->bl));
87
88 if (!drv) {
89 return;
90 }
91
92 /* Default alignment based on whether driver has byte interface */
93 bs->bl.request_alignment = drv->bdrv_co_preadv ? 1 : 512;
94
95 /* Take some limits from the children as a default */
96 if (bs->file) {
97 bdrv_refresh_limits(bs->file->bs, &local_err);
98 if (local_err) {
99 error_propagate(errp, local_err);
100 return;
101 }
102 bdrv_merge_limits(&bs->bl, &bs->file->bs->bl);
103 } else {
104 bs->bl.min_mem_alignment = 512;
105 bs->bl.opt_mem_alignment = getpagesize();
106
107 /* Safe default since most protocols use readv()/writev()/etc */
108 bs->bl.max_iov = IOV_MAX;
109 }
110
111 if (bs->backing) {
112 bdrv_refresh_limits(bs->backing->bs, &local_err);
113 if (local_err) {
114 error_propagate(errp, local_err);
115 return;
116 }
117 bdrv_merge_limits(&bs->bl, &bs->backing->bs->bl);
118 }
119
120 /* Then let the driver override it */
121 if (drv->bdrv_refresh_limits) {
122 drv->bdrv_refresh_limits(bs, errp);
123 }
124 }
125
126 /**
127 * The copy-on-read flag is actually a reference count so multiple users may
128 * use the feature without worrying about clobbering its previous state.
129 * Copy-on-read stays enabled until all users have called to disable it.
130 */
131 void bdrv_enable_copy_on_read(BlockDriverState *bs)
132 {
133 bs->copy_on_read++;
134 }
135
136 void bdrv_disable_copy_on_read(BlockDriverState *bs)
137 {
138 assert(bs->copy_on_read > 0);
139 bs->copy_on_read--;
140 }
141
142 /* Check if any requests are in-flight (including throttled requests) */
143 bool bdrv_requests_pending(BlockDriverState *bs)
144 {
145 BdrvChild *child;
146
147 if (!QLIST_EMPTY(&bs->tracked_requests)) {
148 return true;
149 }
150
151 QLIST_FOREACH(child, &bs->children, next) {
152 if (bdrv_requests_pending(child->bs)) {
153 return true;
154 }
155 }
156
157 return false;
158 }
159
160 static void bdrv_drain_recurse(BlockDriverState *bs)
161 {
162 BdrvChild *child;
163
164 if (bs->drv && bs->drv->bdrv_drain) {
165 bs->drv->bdrv_drain(bs);
166 }
167 QLIST_FOREACH(child, &bs->children, next) {
168 bdrv_drain_recurse(child->bs);
169 }
170 }
171
172 typedef struct {
173 Coroutine *co;
174 BlockDriverState *bs;
175 QEMUBH *bh;
176 bool done;
177 } BdrvCoDrainData;
178
179 static void bdrv_drain_poll(BlockDriverState *bs)
180 {
181 bool busy = true;
182
183 while (busy) {
184 /* Keep iterating */
185 busy = bdrv_requests_pending(bs);
186 busy |= aio_poll(bdrv_get_aio_context(bs), busy);
187 }
188 }
189
190 static void bdrv_co_drain_bh_cb(void *opaque)
191 {
192 BdrvCoDrainData *data = opaque;
193 Coroutine *co = data->co;
194
195 qemu_bh_delete(data->bh);
196 bdrv_drain_poll(data->bs);
197 data->done = true;
198 qemu_coroutine_enter(co);
199 }
200
201 static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs)
202 {
203 BdrvCoDrainData data;
204
205 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
206 * other coroutines run if they were queued from
207 * qemu_co_queue_run_restart(). */
208
209 assert(qemu_in_coroutine());
210 data = (BdrvCoDrainData) {
211 .co = qemu_coroutine_self(),
212 .bs = bs,
213 .done = false,
214 .bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_co_drain_bh_cb, &data),
215 };
216 qemu_bh_schedule(data.bh);
217
218 qemu_coroutine_yield();
219 /* If we are resumed from some other event (such as an aio completion or a
220 * timer callback), it is a bug in the caller that should be fixed. */
221 assert(data.done);
222 }
223
224 void bdrv_drained_begin(BlockDriverState *bs)
225 {
226 if (!bs->quiesce_counter++) {
227 aio_disable_external(bdrv_get_aio_context(bs));
228 bdrv_parent_drained_begin(bs);
229 }
230
231 bdrv_io_unplugged_begin(bs);
232 bdrv_drain_recurse(bs);
233 if (qemu_in_coroutine()) {
234 bdrv_co_yield_to_drain(bs);
235 } else {
236 bdrv_drain_poll(bs);
237 }
238 bdrv_io_unplugged_end(bs);
239 }
240
241 void bdrv_drained_end(BlockDriverState *bs)
242 {
243 assert(bs->quiesce_counter > 0);
244 if (--bs->quiesce_counter > 0) {
245 return;
246 }
247
248 bdrv_parent_drained_end(bs);
249 aio_enable_external(bdrv_get_aio_context(bs));
250 }
251
252 /*
253 * Wait for pending requests to complete on a single BlockDriverState subtree,
254 * and suspend block driver's internal I/O until next request arrives.
255 *
256 * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
257 * AioContext.
258 *
259 * Only this BlockDriverState's AioContext is run, so in-flight requests must
260 * not depend on events in other AioContexts. In that case, use
261 * bdrv_drain_all() instead.
262 */
263 void coroutine_fn bdrv_co_drain(BlockDriverState *bs)
264 {
265 assert(qemu_in_coroutine());
266 bdrv_drained_begin(bs);
267 bdrv_drained_end(bs);
268 }
269
270 void bdrv_drain(BlockDriverState *bs)
271 {
272 bdrv_drained_begin(bs);
273 bdrv_drained_end(bs);
274 }
275
276 /*
277 * Wait for pending requests to complete across all BlockDriverStates
278 *
279 * This function does not flush data to disk, use bdrv_flush_all() for that
280 * after calling this function.
281 */
282 void bdrv_drain_all(void)
283 {
284 /* Always run first iteration so any pending completion BHs run */
285 bool busy = true;
286 BlockDriverState *bs;
287 BdrvNextIterator it;
288 BlockJob *job = NULL;
289 GSList *aio_ctxs = NULL, *ctx;
290
291 while ((job = block_job_next(job))) {
292 AioContext *aio_context = blk_get_aio_context(job->blk);
293
294 aio_context_acquire(aio_context);
295 block_job_pause(job);
296 aio_context_release(aio_context);
297 }
298
299 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
300 AioContext *aio_context = bdrv_get_aio_context(bs);
301
302 aio_context_acquire(aio_context);
303 bdrv_parent_drained_begin(bs);
304 bdrv_io_unplugged_begin(bs);
305 bdrv_drain_recurse(bs);
306 aio_context_release(aio_context);
307
308 if (!g_slist_find(aio_ctxs, aio_context)) {
309 aio_ctxs = g_slist_prepend(aio_ctxs, aio_context);
310 }
311 }
312
313 /* Note that completion of an asynchronous I/O operation can trigger any
314 * number of other I/O operations on other devices---for example a
315 * coroutine can submit an I/O request to another device in response to
316 * request completion. Therefore we must keep looping until there was no
317 * more activity rather than simply draining each device independently.
318 */
319 while (busy) {
320 busy = false;
321
322 for (ctx = aio_ctxs; ctx != NULL; ctx = ctx->next) {
323 AioContext *aio_context = ctx->data;
324
325 aio_context_acquire(aio_context);
326 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
327 if (aio_context == bdrv_get_aio_context(bs)) {
328 if (bdrv_requests_pending(bs)) {
329 busy = true;
330 aio_poll(aio_context, busy);
331 }
332 }
333 }
334 busy |= aio_poll(aio_context, false);
335 aio_context_release(aio_context);
336 }
337 }
338
339 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
340 AioContext *aio_context = bdrv_get_aio_context(bs);
341
342 aio_context_acquire(aio_context);
343 bdrv_io_unplugged_end(bs);
344 bdrv_parent_drained_end(bs);
345 aio_context_release(aio_context);
346 }
347 g_slist_free(aio_ctxs);
348
349 job = NULL;
350 while ((job = block_job_next(job))) {
351 AioContext *aio_context = blk_get_aio_context(job->blk);
352
353 aio_context_acquire(aio_context);
354 block_job_resume(job);
355 aio_context_release(aio_context);
356 }
357 }
358
359 /**
360 * Remove an active request from the tracked requests list
361 *
362 * This function should be called when a tracked request is completing.
363 */
364 static void tracked_request_end(BdrvTrackedRequest *req)
365 {
366 if (req->serialising) {
367 req->bs->serialising_in_flight--;
368 }
369
370 QLIST_REMOVE(req, list);
371 qemu_co_queue_restart_all(&req->wait_queue);
372 }
373
374 /**
375 * Add an active request to the tracked requests list
376 */
377 static void tracked_request_begin(BdrvTrackedRequest *req,
378 BlockDriverState *bs,
379 int64_t offset,
380 unsigned int bytes,
381 enum BdrvTrackedRequestType type)
382 {
383 *req = (BdrvTrackedRequest){
384 .bs = bs,
385 .offset = offset,
386 .bytes = bytes,
387 .type = type,
388 .co = qemu_coroutine_self(),
389 .serialising = false,
390 .overlap_offset = offset,
391 .overlap_bytes = bytes,
392 };
393
394 qemu_co_queue_init(&req->wait_queue);
395
396 QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
397 }
398
399 static void mark_request_serialising(BdrvTrackedRequest *req, uint64_t align)
400 {
401 int64_t overlap_offset = req->offset & ~(align - 1);
402 unsigned int overlap_bytes = ROUND_UP(req->offset + req->bytes, align)
403 - overlap_offset;
404
405 if (!req->serialising) {
406 req->bs->serialising_in_flight++;
407 req->serialising = true;
408 }
409
410 req->overlap_offset = MIN(req->overlap_offset, overlap_offset);
411 req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes);
412 }
413
414 /**
415 * Round a region to cluster boundaries (sector-based)
416 */
417 void bdrv_round_sectors_to_clusters(BlockDriverState *bs,
418 int64_t sector_num, int nb_sectors,
419 int64_t *cluster_sector_num,
420 int *cluster_nb_sectors)
421 {
422 BlockDriverInfo bdi;
423
424 if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
425 *cluster_sector_num = sector_num;
426 *cluster_nb_sectors = nb_sectors;
427 } else {
428 int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE;
429 *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
430 *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
431 nb_sectors, c);
432 }
433 }
434
435 /**
436 * Round a region to cluster boundaries
437 */
438 void bdrv_round_to_clusters(BlockDriverState *bs,
439 int64_t offset, unsigned int bytes,
440 int64_t *cluster_offset,
441 unsigned int *cluster_bytes)
442 {
443 BlockDriverInfo bdi;
444
445 if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
446 *cluster_offset = offset;
447 *cluster_bytes = bytes;
448 } else {
449 int64_t c = bdi.cluster_size;
450 *cluster_offset = QEMU_ALIGN_DOWN(offset, c);
451 *cluster_bytes = QEMU_ALIGN_UP(offset - *cluster_offset + bytes, c);
452 }
453 }
454
455 static int bdrv_get_cluster_size(BlockDriverState *bs)
456 {
457 BlockDriverInfo bdi;
458 int ret;
459
460 ret = bdrv_get_info(bs, &bdi);
461 if (ret < 0 || bdi.cluster_size == 0) {
462 return bs->bl.request_alignment;
463 } else {
464 return bdi.cluster_size;
465 }
466 }
467
468 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
469 int64_t offset, unsigned int bytes)
470 {
471 /* aaaa bbbb */
472 if (offset >= req->overlap_offset + req->overlap_bytes) {
473 return false;
474 }
475 /* bbbb aaaa */
476 if (req->overlap_offset >= offset + bytes) {
477 return false;
478 }
479 return true;
480 }
481
482 static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
483 {
484 BlockDriverState *bs = self->bs;
485 BdrvTrackedRequest *req;
486 bool retry;
487 bool waited = false;
488
489 if (!bs->serialising_in_flight) {
490 return false;
491 }
492
493 do {
494 retry = false;
495 QLIST_FOREACH(req, &bs->tracked_requests, list) {
496 if (req == self || (!req->serialising && !self->serialising)) {
497 continue;
498 }
499 if (tracked_request_overlaps(req, self->overlap_offset,
500 self->overlap_bytes))
501 {
502 /* Hitting this means there was a reentrant request, for
503 * example, a block driver issuing nested requests. This must
504 * never happen since it means deadlock.
505 */
506 assert(qemu_coroutine_self() != req->co);
507
508 /* If the request is already (indirectly) waiting for us, or
509 * will wait for us as soon as it wakes up, then just go on
510 * (instead of producing a deadlock in the former case). */
511 if (!req->waiting_for) {
512 self->waiting_for = req;
513 qemu_co_queue_wait(&req->wait_queue);
514 self->waiting_for = NULL;
515 retry = true;
516 waited = true;
517 break;
518 }
519 }
520 }
521 } while (retry);
522
523 return waited;
524 }
525
526 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
527 size_t size)
528 {
529 if (size > BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS) {
530 return -EIO;
531 }
532
533 if (!bdrv_is_inserted(bs)) {
534 return -ENOMEDIUM;
535 }
536
537 if (offset < 0) {
538 return -EIO;
539 }
540
541 return 0;
542 }
543
544 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
545 int nb_sectors)
546 {
547 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
548 return -EIO;
549 }
550
551 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
552 nb_sectors * BDRV_SECTOR_SIZE);
553 }
554
555 typedef struct RwCo {
556 BdrvChild *child;
557 int64_t offset;
558 QEMUIOVector *qiov;
559 bool is_write;
560 int ret;
561 BdrvRequestFlags flags;
562 } RwCo;
563
564 static void coroutine_fn bdrv_rw_co_entry(void *opaque)
565 {
566 RwCo *rwco = opaque;
567
568 if (!rwco->is_write) {
569 rwco->ret = bdrv_co_preadv(rwco->child, rwco->offset,
570 rwco->qiov->size, rwco->qiov,
571 rwco->flags);
572 } else {
573 rwco->ret = bdrv_co_pwritev(rwco->child, rwco->offset,
574 rwco->qiov->size, rwco->qiov,
575 rwco->flags);
576 }
577 }
578
579 /*
580 * Process a vectored synchronous request using coroutines
581 */
582 static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
583 QEMUIOVector *qiov, bool is_write,
584 BdrvRequestFlags flags)
585 {
586 Coroutine *co;
587 RwCo rwco = {
588 .child = child,
589 .offset = offset,
590 .qiov = qiov,
591 .is_write = is_write,
592 .ret = NOT_DONE,
593 .flags = flags,
594 };
595
596 if (qemu_in_coroutine()) {
597 /* Fast-path if already in coroutine context */
598 bdrv_rw_co_entry(&rwco);
599 } else {
600 AioContext *aio_context = bdrv_get_aio_context(child->bs);
601
602 co = qemu_coroutine_create(bdrv_rw_co_entry, &rwco);
603 qemu_coroutine_enter(co);
604 while (rwco.ret == NOT_DONE) {
605 aio_poll(aio_context, true);
606 }
607 }
608 return rwco.ret;
609 }
610
611 /*
612 * Process a synchronous request using coroutines
613 */
614 static int bdrv_rw_co(BdrvChild *child, int64_t sector_num, uint8_t *buf,
615 int nb_sectors, bool is_write, BdrvRequestFlags flags)
616 {
617 QEMUIOVector qiov;
618 struct iovec iov = {
619 .iov_base = (void *)buf,
620 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
621 };
622
623 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
624 return -EINVAL;
625 }
626
627 qemu_iovec_init_external(&qiov, &iov, 1);
628 return bdrv_prwv_co(child, sector_num << BDRV_SECTOR_BITS,
629 &qiov, is_write, flags);
630 }
631
632 /* return < 0 if error. See bdrv_write() for the return codes */
633 int bdrv_read(BdrvChild *child, int64_t sector_num,
634 uint8_t *buf, int nb_sectors)
635 {
636 return bdrv_rw_co(child, sector_num, buf, nb_sectors, false, 0);
637 }
638
639 /* Return < 0 if error. Important errors are:
640 -EIO generic I/O error (may happen for all errors)
641 -ENOMEDIUM No media inserted.
642 -EINVAL Invalid sector number or nb_sectors
643 -EACCES Trying to write a read-only device
644 */
645 int bdrv_write(BdrvChild *child, int64_t sector_num,
646 const uint8_t *buf, int nb_sectors)
647 {
648 return bdrv_rw_co(child, sector_num, (uint8_t *)buf, nb_sectors, true, 0);
649 }
650
651 int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
652 int count, BdrvRequestFlags flags)
653 {
654 QEMUIOVector qiov;
655 struct iovec iov = {
656 .iov_base = NULL,
657 .iov_len = count,
658 };
659
660 qemu_iovec_init_external(&qiov, &iov, 1);
661 return bdrv_prwv_co(child, offset, &qiov, true,
662 BDRV_REQ_ZERO_WRITE | flags);
663 }
664
665 /*
666 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
667 * The operation is sped up by checking the block status and only writing
668 * zeroes to the device if they currently do not return zeroes. Optional
669 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
670 * BDRV_REQ_FUA).
671 *
672 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
673 */
674 int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
675 {
676 int64_t target_sectors, ret, nb_sectors, sector_num = 0;
677 BlockDriverState *bs = child->bs;
678 BlockDriverState *file;
679 int n;
680
681 target_sectors = bdrv_nb_sectors(bs);
682 if (target_sectors < 0) {
683 return target_sectors;
684 }
685
686 for (;;) {
687 nb_sectors = MIN(target_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
688 if (nb_sectors <= 0) {
689 return 0;
690 }
691 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n, &file);
692 if (ret < 0) {
693 error_report("error getting block status at sector %" PRId64 ": %s",
694 sector_num, strerror(-ret));
695 return ret;
696 }
697 if (ret & BDRV_BLOCK_ZERO) {
698 sector_num += n;
699 continue;
700 }
701 ret = bdrv_pwrite_zeroes(child, sector_num << BDRV_SECTOR_BITS,
702 n << BDRV_SECTOR_BITS, flags);
703 if (ret < 0) {
704 error_report("error writing zeroes at sector %" PRId64 ": %s",
705 sector_num, strerror(-ret));
706 return ret;
707 }
708 sector_num += n;
709 }
710 }
711
712 int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
713 {
714 int ret;
715
716 ret = bdrv_prwv_co(child, offset, qiov, false, 0);
717 if (ret < 0) {
718 return ret;
719 }
720
721 return qiov->size;
722 }
723
724 int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes)
725 {
726 QEMUIOVector qiov;
727 struct iovec iov = {
728 .iov_base = (void *)buf,
729 .iov_len = bytes,
730 };
731
732 if (bytes < 0) {
733 return -EINVAL;
734 }
735
736 qemu_iovec_init_external(&qiov, &iov, 1);
737 return bdrv_preadv(child, offset, &qiov);
738 }
739
740 int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
741 {
742 int ret;
743
744 ret = bdrv_prwv_co(child, offset, qiov, true, 0);
745 if (ret < 0) {
746 return ret;
747 }
748
749 return qiov->size;
750 }
751
752 int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes)
753 {
754 QEMUIOVector qiov;
755 struct iovec iov = {
756 .iov_base = (void *) buf,
757 .iov_len = bytes,
758 };
759
760 if (bytes < 0) {
761 return -EINVAL;
762 }
763
764 qemu_iovec_init_external(&qiov, &iov, 1);
765 return bdrv_pwritev(child, offset, &qiov);
766 }
767
768 /*
769 * Writes to the file and ensures that no writes are reordered across this
770 * request (acts as a barrier)
771 *
772 * Returns 0 on success, -errno in error cases.
773 */
774 int bdrv_pwrite_sync(BdrvChild *child, int64_t offset,
775 const void *buf, int count)
776 {
777 int ret;
778
779 ret = bdrv_pwrite(child, offset, buf, count);
780 if (ret < 0) {
781 return ret;
782 }
783
784 ret = bdrv_flush(child->bs);
785 if (ret < 0) {
786 return ret;
787 }
788
789 return 0;
790 }
791
792 typedef struct CoroutineIOCompletion {
793 Coroutine *coroutine;
794 int ret;
795 } CoroutineIOCompletion;
796
797 static void bdrv_co_io_em_complete(void *opaque, int ret)
798 {
799 CoroutineIOCompletion *co = opaque;
800
801 co->ret = ret;
802 qemu_coroutine_enter(co->coroutine);
803 }
804
805 static int coroutine_fn bdrv_driver_preadv(BlockDriverState *bs,
806 uint64_t offset, uint64_t bytes,
807 QEMUIOVector *qiov, int flags)
808 {
809 BlockDriver *drv = bs->drv;
810 int64_t sector_num;
811 unsigned int nb_sectors;
812
813 assert(!(flags & ~BDRV_REQ_MASK));
814
815 if (drv->bdrv_co_preadv) {
816 return drv->bdrv_co_preadv(bs, offset, bytes, qiov, flags);
817 }
818
819 sector_num = offset >> BDRV_SECTOR_BITS;
820 nb_sectors = bytes >> BDRV_SECTOR_BITS;
821
822 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
823 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
824 assert((bytes >> BDRV_SECTOR_BITS) <= BDRV_REQUEST_MAX_SECTORS);
825
826 if (drv->bdrv_co_readv) {
827 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
828 } else {
829 BlockAIOCB *acb;
830 CoroutineIOCompletion co = {
831 .coroutine = qemu_coroutine_self(),
832 };
833
834 acb = bs->drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
835 bdrv_co_io_em_complete, &co);
836 if (acb == NULL) {
837 return -EIO;
838 } else {
839 qemu_coroutine_yield();
840 return co.ret;
841 }
842 }
843 }
844
845 static int coroutine_fn bdrv_driver_pwritev(BlockDriverState *bs,
846 uint64_t offset, uint64_t bytes,
847 QEMUIOVector *qiov, int flags)
848 {
849 BlockDriver *drv = bs->drv;
850 int64_t sector_num;
851 unsigned int nb_sectors;
852 int ret;
853
854 assert(!(flags & ~BDRV_REQ_MASK));
855
856 if (drv->bdrv_co_pwritev) {
857 ret = drv->bdrv_co_pwritev(bs, offset, bytes, qiov,
858 flags & bs->supported_write_flags);
859 flags &= ~bs->supported_write_flags;
860 goto emulate_flags;
861 }
862
863 sector_num = offset >> BDRV_SECTOR_BITS;
864 nb_sectors = bytes >> BDRV_SECTOR_BITS;
865
866 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
867 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
868 assert((bytes >> BDRV_SECTOR_BITS) <= BDRV_REQUEST_MAX_SECTORS);
869
870 if (drv->bdrv_co_writev_flags) {
871 ret = drv->bdrv_co_writev_flags(bs, sector_num, nb_sectors, qiov,
872 flags & bs->supported_write_flags);
873 flags &= ~bs->supported_write_flags;
874 } else if (drv->bdrv_co_writev) {
875 assert(!bs->supported_write_flags);
876 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
877 } else {
878 BlockAIOCB *acb;
879 CoroutineIOCompletion co = {
880 .coroutine = qemu_coroutine_self(),
881 };
882
883 acb = bs->drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
884 bdrv_co_io_em_complete, &co);
885 if (acb == NULL) {
886 ret = -EIO;
887 } else {
888 qemu_coroutine_yield();
889 ret = co.ret;
890 }
891 }
892
893 emulate_flags:
894 if (ret == 0 && (flags & BDRV_REQ_FUA)) {
895 ret = bdrv_co_flush(bs);
896 }
897
898 return ret;
899 }
900
901 static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
902 int64_t offset, unsigned int bytes, QEMUIOVector *qiov)
903 {
904 /* Perform I/O through a temporary buffer so that users who scribble over
905 * their read buffer while the operation is in progress do not end up
906 * modifying the image file. This is critical for zero-copy guest I/O
907 * where anything might happen inside guest memory.
908 */
909 void *bounce_buffer;
910
911 BlockDriver *drv = bs->drv;
912 struct iovec iov;
913 QEMUIOVector bounce_qiov;
914 int64_t cluster_offset;
915 unsigned int cluster_bytes;
916 size_t skip_bytes;
917 int ret;
918
919 /* Cover entire cluster so no additional backing file I/O is required when
920 * allocating cluster in the image file.
921 */
922 bdrv_round_to_clusters(bs, offset, bytes, &cluster_offset, &cluster_bytes);
923
924 trace_bdrv_co_do_copy_on_readv(bs, offset, bytes,
925 cluster_offset, cluster_bytes);
926
927 iov.iov_len = cluster_bytes;
928 iov.iov_base = bounce_buffer = qemu_try_blockalign(bs, iov.iov_len);
929 if (bounce_buffer == NULL) {
930 ret = -ENOMEM;
931 goto err;
932 }
933
934 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
935
936 ret = bdrv_driver_preadv(bs, cluster_offset, cluster_bytes,
937 &bounce_qiov, 0);
938 if (ret < 0) {
939 goto err;
940 }
941
942 if (drv->bdrv_co_pwrite_zeroes &&
943 buffer_is_zero(bounce_buffer, iov.iov_len)) {
944 /* FIXME: Should we (perhaps conditionally) be setting
945 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
946 * that still correctly reads as zero? */
947 ret = bdrv_co_do_pwrite_zeroes(bs, cluster_offset, cluster_bytes, 0);
948 } else {
949 /* This does not change the data on the disk, it is not necessary
950 * to flush even in cache=writethrough mode.
951 */
952 ret = bdrv_driver_pwritev(bs, cluster_offset, cluster_bytes,
953 &bounce_qiov, 0);
954 }
955
956 if (ret < 0) {
957 /* It might be okay to ignore write errors for guest requests. If this
958 * is a deliberate copy-on-read then we don't want to ignore the error.
959 * Simply report it in all cases.
960 */
961 goto err;
962 }
963
964 skip_bytes = offset - cluster_offset;
965 qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, bytes);
966
967 err:
968 qemu_vfree(bounce_buffer);
969 return ret;
970 }
971
972 /*
973 * Forwards an already correctly aligned request to the BlockDriver. This
974 * handles copy on read, zeroing after EOF, and fragmentation of large
975 * reads; any other features must be implemented by the caller.
976 */
977 static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs,
978 BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
979 int64_t align, QEMUIOVector *qiov, int flags)
980 {
981 int64_t total_bytes, max_bytes;
982 int ret = 0;
983 uint64_t bytes_remaining = bytes;
984 int max_transfer;
985
986 assert(is_power_of_2(align));
987 assert((offset & (align - 1)) == 0);
988 assert((bytes & (align - 1)) == 0);
989 assert(!qiov || bytes == qiov->size);
990 assert((bs->open_flags & BDRV_O_NO_IO) == 0);
991 max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
992 align);
993
994 /* TODO: We would need a per-BDS .supported_read_flags and
995 * potential fallback support, if we ever implement any read flags
996 * to pass through to drivers. For now, there aren't any
997 * passthrough flags. */
998 assert(!(flags & ~(BDRV_REQ_NO_SERIALISING | BDRV_REQ_COPY_ON_READ)));
999
1000 /* Handle Copy on Read and associated serialisation */
1001 if (flags & BDRV_REQ_COPY_ON_READ) {
1002 /* If we touch the same cluster it counts as an overlap. This
1003 * guarantees that allocating writes will be serialized and not race
1004 * with each other for the same cluster. For example, in copy-on-read
1005 * it ensures that the CoR read and write operations are atomic and
1006 * guest writes cannot interleave between them. */
1007 mark_request_serialising(req, bdrv_get_cluster_size(bs));
1008 }
1009
1010 if (!(flags & BDRV_REQ_NO_SERIALISING)) {
1011 wait_serialising_requests(req);
1012 }
1013
1014 if (flags & BDRV_REQ_COPY_ON_READ) {
1015 int64_t start_sector = offset >> BDRV_SECTOR_BITS;
1016 int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
1017 unsigned int nb_sectors = end_sector - start_sector;
1018 int pnum;
1019
1020 ret = bdrv_is_allocated(bs, start_sector, nb_sectors, &pnum);
1021 if (ret < 0) {
1022 goto out;
1023 }
1024
1025 if (!ret || pnum != nb_sectors) {
1026 ret = bdrv_co_do_copy_on_readv(bs, offset, bytes, qiov);
1027 goto out;
1028 }
1029 }
1030
1031 /* Forward the request to the BlockDriver, possibly fragmenting it */
1032 total_bytes = bdrv_getlength(bs);
1033 if (total_bytes < 0) {
1034 ret = total_bytes;
1035 goto out;
1036 }
1037
1038 max_bytes = ROUND_UP(MAX(0, total_bytes - offset), align);
1039 if (bytes <= max_bytes && bytes <= max_transfer) {
1040 ret = bdrv_driver_preadv(bs, offset, bytes, qiov, 0);
1041 goto out;
1042 }
1043
1044 while (bytes_remaining) {
1045 int num;
1046
1047 if (max_bytes) {
1048 QEMUIOVector local_qiov;
1049
1050 num = MIN(bytes_remaining, MIN(max_bytes, max_transfer));
1051 assert(num);
1052 qemu_iovec_init(&local_qiov, qiov->niov);
1053 qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num);
1054
1055 ret = bdrv_driver_preadv(bs, offset + bytes - bytes_remaining,
1056 num, &local_qiov, 0);
1057 max_bytes -= num;
1058 qemu_iovec_destroy(&local_qiov);
1059 } else {
1060 num = bytes_remaining;
1061 ret = qemu_iovec_memset(qiov, bytes - bytes_remaining, 0,
1062 bytes_remaining);
1063 }
1064 if (ret < 0) {
1065 goto out;
1066 }
1067 bytes_remaining -= num;
1068 }
1069
1070 out:
1071 return ret < 0 ? ret : 0;
1072 }
1073
1074 /*
1075 * Handle a read request in coroutine context
1076 */
1077 int coroutine_fn bdrv_co_preadv(BdrvChild *child,
1078 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
1079 BdrvRequestFlags flags)
1080 {
1081 BlockDriverState *bs = child->bs;
1082 BlockDriver *drv = bs->drv;
1083 BdrvTrackedRequest req;
1084
1085 uint64_t align = bs->bl.request_alignment;
1086 uint8_t *head_buf = NULL;
1087 uint8_t *tail_buf = NULL;
1088 QEMUIOVector local_qiov;
1089 bool use_local_qiov = false;
1090 int ret;
1091
1092 if (!drv) {
1093 return -ENOMEDIUM;
1094 }
1095
1096 ret = bdrv_check_byte_request(bs, offset, bytes);
1097 if (ret < 0) {
1098 return ret;
1099 }
1100
1101 /* Don't do copy-on-read if we read data before write operation */
1102 if (bs->copy_on_read && !(flags & BDRV_REQ_NO_SERIALISING)) {
1103 flags |= BDRV_REQ_COPY_ON_READ;
1104 }
1105
1106 /* Align read if necessary by padding qiov */
1107 if (offset & (align - 1)) {
1108 head_buf = qemu_blockalign(bs, align);
1109 qemu_iovec_init(&local_qiov, qiov->niov + 2);
1110 qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
1111 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1112 use_local_qiov = true;
1113
1114 bytes += offset & (align - 1);
1115 offset = offset & ~(align - 1);
1116 }
1117
1118 if ((offset + bytes) & (align - 1)) {
1119 if (!use_local_qiov) {
1120 qemu_iovec_init(&local_qiov, qiov->niov + 1);
1121 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1122 use_local_qiov = true;
1123 }
1124 tail_buf = qemu_blockalign(bs, align);
1125 qemu_iovec_add(&local_qiov, tail_buf,
1126 align - ((offset + bytes) & (align - 1)));
1127
1128 bytes = ROUND_UP(bytes, align);
1129 }
1130
1131 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_READ);
1132 ret = bdrv_aligned_preadv(bs, &req, offset, bytes, align,
1133 use_local_qiov ? &local_qiov : qiov,
1134 flags);
1135 tracked_request_end(&req);
1136
1137 if (use_local_qiov) {
1138 qemu_iovec_destroy(&local_qiov);
1139 qemu_vfree(head_buf);
1140 qemu_vfree(tail_buf);
1141 }
1142
1143 return ret;
1144 }
1145
1146 static int coroutine_fn bdrv_co_do_readv(BdrvChild *child,
1147 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
1148 BdrvRequestFlags flags)
1149 {
1150 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
1151 return -EINVAL;
1152 }
1153
1154 return bdrv_co_preadv(child, sector_num << BDRV_SECTOR_BITS,
1155 nb_sectors << BDRV_SECTOR_BITS, qiov, flags);
1156 }
1157
1158 int coroutine_fn bdrv_co_readv(BdrvChild *child, int64_t sector_num,
1159 int nb_sectors, QEMUIOVector *qiov)
1160 {
1161 trace_bdrv_co_readv(child->bs, sector_num, nb_sectors);
1162
1163 return bdrv_co_do_readv(child, sector_num, nb_sectors, qiov, 0);
1164 }
1165
1166 /* Maximum buffer for write zeroes fallback, in bytes */
1167 #define MAX_WRITE_ZEROES_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
1168
1169 static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
1170 int64_t offset, int count, BdrvRequestFlags flags)
1171 {
1172 BlockDriver *drv = bs->drv;
1173 QEMUIOVector qiov;
1174 struct iovec iov = {0};
1175 int ret = 0;
1176 bool need_flush = false;
1177 int head = 0;
1178 int tail = 0;
1179
1180 int max_write_zeroes = MIN_NON_ZERO(bs->bl.max_pwrite_zeroes, INT_MAX);
1181 int alignment = MAX(bs->bl.pwrite_zeroes_alignment,
1182 bs->bl.request_alignment);
1183
1184 assert(is_power_of_2(alignment));
1185 head = offset & (alignment - 1);
1186 tail = (offset + count) & (alignment - 1);
1187 max_write_zeroes &= ~(alignment - 1);
1188
1189 while (count > 0 && !ret) {
1190 int num = count;
1191
1192 /* Align request. Block drivers can expect the "bulk" of the request
1193 * to be aligned, and that unaligned requests do not cross cluster
1194 * boundaries.
1195 */
1196 if (head) {
1197 /* Make a small request up to the first aligned sector. */
1198 num = MIN(count, alignment - head);
1199 head = 0;
1200 } else if (tail && num > alignment) {
1201 /* Shorten the request to the last aligned sector. */
1202 num -= tail;
1203 }
1204
1205 /* limit request size */
1206 if (num > max_write_zeroes) {
1207 num = max_write_zeroes;
1208 }
1209
1210 ret = -ENOTSUP;
1211 /* First try the efficient write zeroes operation */
1212 if (drv->bdrv_co_pwrite_zeroes) {
1213 ret = drv->bdrv_co_pwrite_zeroes(bs, offset, num,
1214 flags & bs->supported_zero_flags);
1215 if (ret != -ENOTSUP && (flags & BDRV_REQ_FUA) &&
1216 !(bs->supported_zero_flags & BDRV_REQ_FUA)) {
1217 need_flush = true;
1218 }
1219 } else {
1220 assert(!bs->supported_zero_flags);
1221 }
1222
1223 if (ret == -ENOTSUP) {
1224 /* Fall back to bounce buffer if write zeroes is unsupported */
1225 int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer,
1226 MAX_WRITE_ZEROES_BOUNCE_BUFFER);
1227 BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE;
1228
1229 if ((flags & BDRV_REQ_FUA) &&
1230 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
1231 /* No need for bdrv_driver_pwrite() to do a fallback
1232 * flush on each chunk; use just one at the end */
1233 write_flags &= ~BDRV_REQ_FUA;
1234 need_flush = true;
1235 }
1236 num = MIN(num, max_transfer);
1237 iov.iov_len = num;
1238 if (iov.iov_base == NULL) {
1239 iov.iov_base = qemu_try_blockalign(bs, num);
1240 if (iov.iov_base == NULL) {
1241 ret = -ENOMEM;
1242 goto fail;
1243 }
1244 memset(iov.iov_base, 0, num);
1245 }
1246 qemu_iovec_init_external(&qiov, &iov, 1);
1247
1248 ret = bdrv_driver_pwritev(bs, offset, num, &qiov, write_flags);
1249
1250 /* Keep bounce buffer around if it is big enough for all
1251 * all future requests.
1252 */
1253 if (num < max_transfer) {
1254 qemu_vfree(iov.iov_base);
1255 iov.iov_base = NULL;
1256 }
1257 }
1258
1259 offset += num;
1260 count -= num;
1261 }
1262
1263 fail:
1264 if (ret == 0 && need_flush) {
1265 ret = bdrv_co_flush(bs);
1266 }
1267 qemu_vfree(iov.iov_base);
1268 return ret;
1269 }
1270
1271 /*
1272 * Forwards an already correctly aligned write request to the BlockDriver,
1273 * after possibly fragmenting it.
1274 */
1275 static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
1276 BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
1277 int64_t align, QEMUIOVector *qiov, int flags)
1278 {
1279 BlockDriver *drv = bs->drv;
1280 bool waited;
1281 int ret;
1282
1283 int64_t start_sector = offset >> BDRV_SECTOR_BITS;
1284 int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
1285 uint64_t bytes_remaining = bytes;
1286 int max_transfer;
1287
1288 assert(is_power_of_2(align));
1289 assert((offset & (align - 1)) == 0);
1290 assert((bytes & (align - 1)) == 0);
1291 assert(!qiov || bytes == qiov->size);
1292 assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1293 assert(!(flags & ~BDRV_REQ_MASK));
1294 max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
1295 align);
1296
1297 waited = wait_serialising_requests(req);
1298 assert(!waited || !req->serialising);
1299 assert(req->overlap_offset <= offset);
1300 assert(offset + bytes <= req->overlap_offset + req->overlap_bytes);
1301
1302 ret = notifier_with_return_list_notify(&bs->before_write_notifiers, req);
1303
1304 if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF &&
1305 !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes &&
1306 qemu_iovec_is_zero(qiov)) {
1307 flags |= BDRV_REQ_ZERO_WRITE;
1308 if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) {
1309 flags |= BDRV_REQ_MAY_UNMAP;
1310 }
1311 }
1312
1313 if (ret < 0) {
1314 /* Do nothing, write notifier decided to fail this request */
1315 } else if (flags & BDRV_REQ_ZERO_WRITE) {
1316 bdrv_debug_event(bs, BLKDBG_PWRITEV_ZERO);
1317 ret = bdrv_co_do_pwrite_zeroes(bs, offset, bytes, flags);
1318 } else if (bytes <= max_transfer) {
1319 bdrv_debug_event(bs, BLKDBG_PWRITEV);
1320 ret = bdrv_driver_pwritev(bs, offset, bytes, qiov, flags);
1321 } else {
1322 bdrv_debug_event(bs, BLKDBG_PWRITEV);
1323 while (bytes_remaining) {
1324 int num = MIN(bytes_remaining, max_transfer);
1325 QEMUIOVector local_qiov;
1326 int local_flags = flags;
1327
1328 assert(num);
1329 if (num < bytes_remaining && (flags & BDRV_REQ_FUA) &&
1330 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
1331 /* If FUA is going to be emulated by flush, we only
1332 * need to flush on the last iteration */
1333 local_flags &= ~BDRV_REQ_FUA;
1334 }
1335 qemu_iovec_init(&local_qiov, qiov->niov);
1336 qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num);
1337
1338 ret = bdrv_driver_pwritev(bs, offset + bytes - bytes_remaining,
1339 num, &local_qiov, local_flags);
1340 qemu_iovec_destroy(&local_qiov);
1341 if (ret < 0) {
1342 break;
1343 }
1344 bytes_remaining -= num;
1345 }
1346 }
1347 bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
1348
1349 ++bs->write_gen;
1350 bdrv_set_dirty(bs, start_sector, end_sector - start_sector);
1351
1352 if (bs->wr_highest_offset < offset + bytes) {
1353 bs->wr_highest_offset = offset + bytes;
1354 }
1355
1356 if (ret >= 0) {
1357 bs->total_sectors = MAX(bs->total_sectors, end_sector);
1358 ret = 0;
1359 }
1360
1361 return ret;
1362 }
1363
1364 static int coroutine_fn bdrv_co_do_zero_pwritev(BlockDriverState *bs,
1365 int64_t offset,
1366 unsigned int bytes,
1367 BdrvRequestFlags flags,
1368 BdrvTrackedRequest *req)
1369 {
1370 uint8_t *buf = NULL;
1371 QEMUIOVector local_qiov;
1372 struct iovec iov;
1373 uint64_t align = bs->bl.request_alignment;
1374 unsigned int head_padding_bytes, tail_padding_bytes;
1375 int ret = 0;
1376
1377 head_padding_bytes = offset & (align - 1);
1378 tail_padding_bytes = align - ((offset + bytes) & (align - 1));
1379
1380
1381 assert(flags & BDRV_REQ_ZERO_WRITE);
1382 if (head_padding_bytes || tail_padding_bytes) {
1383 buf = qemu_blockalign(bs, align);
1384 iov = (struct iovec) {
1385 .iov_base = buf,
1386 .iov_len = align,
1387 };
1388 qemu_iovec_init_external(&local_qiov, &iov, 1);
1389 }
1390 if (head_padding_bytes) {
1391 uint64_t zero_bytes = MIN(bytes, align - head_padding_bytes);
1392
1393 /* RMW the unaligned part before head. */
1394 mark_request_serialising(req, align);
1395 wait_serialising_requests(req);
1396 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
1397 ret = bdrv_aligned_preadv(bs, req, offset & ~(align - 1), align,
1398 align, &local_qiov, 0);
1399 if (ret < 0) {
1400 goto fail;
1401 }
1402 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
1403
1404 memset(buf + head_padding_bytes, 0, zero_bytes);
1405 ret = bdrv_aligned_pwritev(bs, req, offset & ~(align - 1), align,
1406 align, &local_qiov,
1407 flags & ~BDRV_REQ_ZERO_WRITE);
1408 if (ret < 0) {
1409 goto fail;
1410 }
1411 offset += zero_bytes;
1412 bytes -= zero_bytes;
1413 }
1414
1415 assert(!bytes || (offset & (align - 1)) == 0);
1416 if (bytes >= align) {
1417 /* Write the aligned part in the middle. */
1418 uint64_t aligned_bytes = bytes & ~(align - 1);
1419 ret = bdrv_aligned_pwritev(bs, req, offset, aligned_bytes, align,
1420 NULL, flags);
1421 if (ret < 0) {
1422 goto fail;
1423 }
1424 bytes -= aligned_bytes;
1425 offset += aligned_bytes;
1426 }
1427
1428 assert(!bytes || (offset & (align - 1)) == 0);
1429 if (bytes) {
1430 assert(align == tail_padding_bytes + bytes);
1431 /* RMW the unaligned part after tail. */
1432 mark_request_serialising(req, align);
1433 wait_serialising_requests(req);
1434 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
1435 ret = bdrv_aligned_preadv(bs, req, offset, align,
1436 align, &local_qiov, 0);
1437 if (ret < 0) {
1438 goto fail;
1439 }
1440 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
1441
1442 memset(buf, 0, bytes);
1443 ret = bdrv_aligned_pwritev(bs, req, offset, align, align,
1444 &local_qiov, flags & ~BDRV_REQ_ZERO_WRITE);
1445 }
1446 fail:
1447 qemu_vfree(buf);
1448 return ret;
1449
1450 }
1451
1452 /*
1453 * Handle a write request in coroutine context
1454 */
1455 int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
1456 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
1457 BdrvRequestFlags flags)
1458 {
1459 BlockDriverState *bs = child->bs;
1460 BdrvTrackedRequest req;
1461 uint64_t align = bs->bl.request_alignment;
1462 uint8_t *head_buf = NULL;
1463 uint8_t *tail_buf = NULL;
1464 QEMUIOVector local_qiov;
1465 bool use_local_qiov = false;
1466 int ret;
1467
1468 if (!bs->drv) {
1469 return -ENOMEDIUM;
1470 }
1471 if (bs->read_only) {
1472 return -EPERM;
1473 }
1474 assert(!(bs->open_flags & BDRV_O_INACTIVE));
1475
1476 ret = bdrv_check_byte_request(bs, offset, bytes);
1477 if (ret < 0) {
1478 return ret;
1479 }
1480
1481 /*
1482 * Align write if necessary by performing a read-modify-write cycle.
1483 * Pad qiov with the read parts and be sure to have a tracked request not
1484 * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
1485 */
1486 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
1487
1488 if (!qiov) {
1489 ret = bdrv_co_do_zero_pwritev(bs, offset, bytes, flags, &req);
1490 goto out;
1491 }
1492
1493 if (offset & (align - 1)) {
1494 QEMUIOVector head_qiov;
1495 struct iovec head_iov;
1496
1497 mark_request_serialising(&req, align);
1498 wait_serialising_requests(&req);
1499
1500 head_buf = qemu_blockalign(bs, align);
1501 head_iov = (struct iovec) {
1502 .iov_base = head_buf,
1503 .iov_len = align,
1504 };
1505 qemu_iovec_init_external(&head_qiov, &head_iov, 1);
1506
1507 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
1508 ret = bdrv_aligned_preadv(bs, &req, offset & ~(align - 1), align,
1509 align, &head_qiov, 0);
1510 if (ret < 0) {
1511 goto fail;
1512 }
1513 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
1514
1515 qemu_iovec_init(&local_qiov, qiov->niov + 2);
1516 qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
1517 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1518 use_local_qiov = true;
1519
1520 bytes += offset & (align - 1);
1521 offset = offset & ~(align - 1);
1522
1523 /* We have read the tail already if the request is smaller
1524 * than one aligned block.
1525 */
1526 if (bytes < align) {
1527 qemu_iovec_add(&local_qiov, head_buf + bytes, align - bytes);
1528 bytes = align;
1529 }
1530 }
1531
1532 if ((offset + bytes) & (align - 1)) {
1533 QEMUIOVector tail_qiov;
1534 struct iovec tail_iov;
1535 size_t tail_bytes;
1536 bool waited;
1537
1538 mark_request_serialising(&req, align);
1539 waited = wait_serialising_requests(&req);
1540 assert(!waited || !use_local_qiov);
1541
1542 tail_buf = qemu_blockalign(bs, align);
1543 tail_iov = (struct iovec) {
1544 .iov_base = tail_buf,
1545 .iov_len = align,
1546 };
1547 qemu_iovec_init_external(&tail_qiov, &tail_iov, 1);
1548
1549 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
1550 ret = bdrv_aligned_preadv(bs, &req, (offset + bytes) & ~(align - 1), align,
1551 align, &tail_qiov, 0);
1552 if (ret < 0) {
1553 goto fail;
1554 }
1555 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
1556
1557 if (!use_local_qiov) {
1558 qemu_iovec_init(&local_qiov, qiov->niov + 1);
1559 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1560 use_local_qiov = true;
1561 }
1562
1563 tail_bytes = (offset + bytes) & (align - 1);
1564 qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, align - tail_bytes);
1565
1566 bytes = ROUND_UP(bytes, align);
1567 }
1568
1569 ret = bdrv_aligned_pwritev(bs, &req, offset, bytes, align,
1570 use_local_qiov ? &local_qiov : qiov,
1571 flags);
1572
1573 fail:
1574
1575 if (use_local_qiov) {
1576 qemu_iovec_destroy(&local_qiov);
1577 }
1578 qemu_vfree(head_buf);
1579 qemu_vfree(tail_buf);
1580 out:
1581 tracked_request_end(&req);
1582 return ret;
1583 }
1584
1585 static int coroutine_fn bdrv_co_do_writev(BdrvChild *child,
1586 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
1587 BdrvRequestFlags flags)
1588 {
1589 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
1590 return -EINVAL;
1591 }
1592
1593 return bdrv_co_pwritev(child, sector_num << BDRV_SECTOR_BITS,
1594 nb_sectors << BDRV_SECTOR_BITS, qiov, flags);
1595 }
1596
1597 int coroutine_fn bdrv_co_writev(BdrvChild *child, int64_t sector_num,
1598 int nb_sectors, QEMUIOVector *qiov)
1599 {
1600 trace_bdrv_co_writev(child->bs, sector_num, nb_sectors);
1601
1602 return bdrv_co_do_writev(child, sector_num, nb_sectors, qiov, 0);
1603 }
1604
1605 int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset,
1606 int count, BdrvRequestFlags flags)
1607 {
1608 trace_bdrv_co_pwrite_zeroes(child->bs, offset, count, flags);
1609
1610 if (!(child->bs->open_flags & BDRV_O_UNMAP)) {
1611 flags &= ~BDRV_REQ_MAY_UNMAP;
1612 }
1613
1614 return bdrv_co_pwritev(child, offset, count, NULL,
1615 BDRV_REQ_ZERO_WRITE | flags);
1616 }
1617
1618 typedef struct BdrvCoGetBlockStatusData {
1619 BlockDriverState *bs;
1620 BlockDriverState *base;
1621 BlockDriverState **file;
1622 int64_t sector_num;
1623 int nb_sectors;
1624 int *pnum;
1625 int64_t ret;
1626 bool done;
1627 } BdrvCoGetBlockStatusData;
1628
1629 /*
1630 * Returns the allocation status of the specified sectors.
1631 * Drivers not implementing the functionality are assumed to not support
1632 * backing files, hence all their sectors are reported as allocated.
1633 *
1634 * If 'sector_num' is beyond the end of the disk image the return value is 0
1635 * and 'pnum' is set to 0.
1636 *
1637 * 'pnum' is set to the number of sectors (including and immediately following
1638 * the specified sector) that are known to be in the same
1639 * allocated/unallocated state.
1640 *
1641 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1642 * beyond the end of the disk image it will be clamped.
1643 *
1644 * If returned value is positive and BDRV_BLOCK_OFFSET_VALID bit is set, 'file'
1645 * points to the BDS which the sector range is allocated in.
1646 */
1647 static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
1648 int64_t sector_num,
1649 int nb_sectors, int *pnum,
1650 BlockDriverState **file)
1651 {
1652 int64_t total_sectors;
1653 int64_t n;
1654 int64_t ret, ret2;
1655
1656 total_sectors = bdrv_nb_sectors(bs);
1657 if (total_sectors < 0) {
1658 return total_sectors;
1659 }
1660
1661 if (sector_num >= total_sectors) {
1662 *pnum = 0;
1663 return 0;
1664 }
1665
1666 n = total_sectors - sector_num;
1667 if (n < nb_sectors) {
1668 nb_sectors = n;
1669 }
1670
1671 if (!bs->drv->bdrv_co_get_block_status) {
1672 *pnum = nb_sectors;
1673 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED;
1674 if (bs->drv->protocol_name) {
1675 ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE);
1676 }
1677 return ret;
1678 }
1679
1680 *file = NULL;
1681 ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum,
1682 file);
1683 if (ret < 0) {
1684 *pnum = 0;
1685 return ret;
1686 }
1687
1688 if (ret & BDRV_BLOCK_RAW) {
1689 assert(ret & BDRV_BLOCK_OFFSET_VALID);
1690 return bdrv_get_block_status(bs->file->bs, ret >> BDRV_SECTOR_BITS,
1691 *pnum, pnum, file);
1692 }
1693
1694 if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
1695 ret |= BDRV_BLOCK_ALLOCATED;
1696 } else {
1697 if (bdrv_unallocated_blocks_are_zero(bs)) {
1698 ret |= BDRV_BLOCK_ZERO;
1699 } else if (bs->backing) {
1700 BlockDriverState *bs2 = bs->backing->bs;
1701 int64_t nb_sectors2 = bdrv_nb_sectors(bs2);
1702 if (nb_sectors2 >= 0 && sector_num >= nb_sectors2) {
1703 ret |= BDRV_BLOCK_ZERO;
1704 }
1705 }
1706 }
1707
1708 if (*file && *file != bs &&
1709 (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
1710 (ret & BDRV_BLOCK_OFFSET_VALID)) {
1711 BlockDriverState *file2;
1712 int file_pnum;
1713
1714 ret2 = bdrv_co_get_block_status(*file, ret >> BDRV_SECTOR_BITS,
1715 *pnum, &file_pnum, &file2);
1716 if (ret2 >= 0) {
1717 /* Ignore errors. This is just providing extra information, it
1718 * is useful but not necessary.
1719 */
1720 if (!file_pnum) {
1721 /* !file_pnum indicates an offset at or beyond the EOF; it is
1722 * perfectly valid for the format block driver to point to such
1723 * offsets, so catch it and mark everything as zero */
1724 ret |= BDRV_BLOCK_ZERO;
1725 } else {
1726 /* Limit request to the range reported by the protocol driver */
1727 *pnum = file_pnum;
1728 ret |= (ret2 & BDRV_BLOCK_ZERO);
1729 }
1730 }
1731 }
1732
1733 return ret;
1734 }
1735
1736 static int64_t coroutine_fn bdrv_co_get_block_status_above(BlockDriverState *bs,
1737 BlockDriverState *base,
1738 int64_t sector_num,
1739 int nb_sectors,
1740 int *pnum,
1741 BlockDriverState **file)
1742 {
1743 BlockDriverState *p;
1744 int64_t ret = 0;
1745
1746 assert(bs != base);
1747 for (p = bs; p != base; p = backing_bs(p)) {
1748 ret = bdrv_co_get_block_status(p, sector_num, nb_sectors, pnum, file);
1749 if (ret < 0 || ret & BDRV_BLOCK_ALLOCATED) {
1750 break;
1751 }
1752 /* [sector_num, pnum] unallocated on this layer, which could be only
1753 * the first part of [sector_num, nb_sectors]. */
1754 nb_sectors = MIN(nb_sectors, *pnum);
1755 }
1756 return ret;
1757 }
1758
1759 /* Coroutine wrapper for bdrv_get_block_status_above() */
1760 static void coroutine_fn bdrv_get_block_status_above_co_entry(void *opaque)
1761 {
1762 BdrvCoGetBlockStatusData *data = opaque;
1763
1764 data->ret = bdrv_co_get_block_status_above(data->bs, data->base,
1765 data->sector_num,
1766 data->nb_sectors,
1767 data->pnum,
1768 data->file);
1769 data->done = true;
1770 }
1771
1772 /*
1773 * Synchronous wrapper around bdrv_co_get_block_status_above().
1774 *
1775 * See bdrv_co_get_block_status_above() for details.
1776 */
1777 int64_t bdrv_get_block_status_above(BlockDriverState *bs,
1778 BlockDriverState *base,
1779 int64_t sector_num,
1780 int nb_sectors, int *pnum,
1781 BlockDriverState **file)
1782 {
1783 Coroutine *co;
1784 BdrvCoGetBlockStatusData data = {
1785 .bs = bs,
1786 .base = base,
1787 .file = file,
1788 .sector_num = sector_num,
1789 .nb_sectors = nb_sectors,
1790 .pnum = pnum,
1791 .done = false,
1792 };
1793
1794 if (qemu_in_coroutine()) {
1795 /* Fast-path if already in coroutine context */
1796 bdrv_get_block_status_above_co_entry(&data);
1797 } else {
1798 AioContext *aio_context = bdrv_get_aio_context(bs);
1799
1800 co = qemu_coroutine_create(bdrv_get_block_status_above_co_entry,
1801 &data);
1802 qemu_coroutine_enter(co);
1803 while (!data.done) {
1804 aio_poll(aio_context, true);
1805 }
1806 }
1807 return data.ret;
1808 }
1809
1810 int64_t bdrv_get_block_status(BlockDriverState *bs,
1811 int64_t sector_num,
1812 int nb_sectors, int *pnum,
1813 BlockDriverState **file)
1814 {
1815 return bdrv_get_block_status_above(bs, backing_bs(bs),
1816 sector_num, nb_sectors, pnum, file);
1817 }
1818
1819 int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
1820 int nb_sectors, int *pnum)
1821 {
1822 BlockDriverState *file;
1823 int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum,
1824 &file);
1825 if (ret < 0) {
1826 return ret;
1827 }
1828 return !!(ret & BDRV_BLOCK_ALLOCATED);
1829 }
1830
1831 /*
1832 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
1833 *
1834 * Return true if the given sector is allocated in any image between
1835 * BASE and TOP (inclusive). BASE can be NULL to check if the given
1836 * sector is allocated in any image of the chain. Return false otherwise.
1837 *
1838 * 'pnum' is set to the number of sectors (including and immediately following
1839 * the specified sector) that are known to be in the same
1840 * allocated/unallocated state.
1841 *
1842 */
1843 int bdrv_is_allocated_above(BlockDriverState *top,
1844 BlockDriverState *base,
1845 int64_t sector_num,
1846 int nb_sectors, int *pnum)
1847 {
1848 BlockDriverState *intermediate;
1849 int ret, n = nb_sectors;
1850
1851 intermediate = top;
1852 while (intermediate && intermediate != base) {
1853 int pnum_inter;
1854 ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors,
1855 &pnum_inter);
1856 if (ret < 0) {
1857 return ret;
1858 } else if (ret) {
1859 *pnum = pnum_inter;
1860 return 1;
1861 }
1862
1863 /*
1864 * [sector_num, nb_sectors] is unallocated on top but intermediate
1865 * might have
1866 *
1867 * [sector_num+x, nr_sectors] allocated.
1868 */
1869 if (n > pnum_inter &&
1870 (intermediate == top ||
1871 sector_num + pnum_inter < intermediate->total_sectors)) {
1872 n = pnum_inter;
1873 }
1874
1875 intermediate = backing_bs(intermediate);
1876 }
1877
1878 *pnum = n;
1879 return 0;
1880 }
1881
1882 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1883 const uint8_t *buf, int nb_sectors)
1884 {
1885 BlockDriver *drv = bs->drv;
1886 int ret;
1887
1888 if (!drv) {
1889 return -ENOMEDIUM;
1890 }
1891 if (!drv->bdrv_write_compressed) {
1892 return -ENOTSUP;
1893 }
1894 ret = bdrv_check_request(bs, sector_num, nb_sectors);
1895 if (ret < 0) {
1896 return ret;
1897 }
1898
1899 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
1900
1901 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1902 }
1903
1904 typedef struct BdrvVmstateCo {
1905 BlockDriverState *bs;
1906 QEMUIOVector *qiov;
1907 int64_t pos;
1908 bool is_read;
1909 int ret;
1910 } BdrvVmstateCo;
1911
1912 static int coroutine_fn
1913 bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
1914 bool is_read)
1915 {
1916 BlockDriver *drv = bs->drv;
1917
1918 if (!drv) {
1919 return -ENOMEDIUM;
1920 } else if (drv->bdrv_load_vmstate) {
1921 return is_read ? drv->bdrv_load_vmstate(bs, qiov, pos)
1922 : drv->bdrv_save_vmstate(bs, qiov, pos);
1923 } else if (bs->file) {
1924 return bdrv_co_rw_vmstate(bs->file->bs, qiov, pos, is_read);
1925 }
1926
1927 return -ENOTSUP;
1928 }
1929
1930 static void coroutine_fn bdrv_co_rw_vmstate_entry(void *opaque)
1931 {
1932 BdrvVmstateCo *co = opaque;
1933 co->ret = bdrv_co_rw_vmstate(co->bs, co->qiov, co->pos, co->is_read);
1934 }
1935
1936 static inline int
1937 bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
1938 bool is_read)
1939 {
1940 if (qemu_in_coroutine()) {
1941 return bdrv_co_rw_vmstate(bs, qiov, pos, is_read);
1942 } else {
1943 BdrvVmstateCo data = {
1944 .bs = bs,
1945 .qiov = qiov,
1946 .pos = pos,
1947 .is_read = is_read,
1948 .ret = -EINPROGRESS,
1949 };
1950 Coroutine *co = qemu_coroutine_create(bdrv_co_rw_vmstate_entry, &data);
1951
1952 qemu_coroutine_enter(co);
1953 while (data.ret == -EINPROGRESS) {
1954 aio_poll(bdrv_get_aio_context(bs), true);
1955 }
1956 return data.ret;
1957 }
1958 }
1959
1960 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1961 int64_t pos, int size)
1962 {
1963 QEMUIOVector qiov;
1964 struct iovec iov = {
1965 .iov_base = (void *) buf,
1966 .iov_len = size,
1967 };
1968 int ret;
1969
1970 qemu_iovec_init_external(&qiov, &iov, 1);
1971
1972 ret = bdrv_writev_vmstate(bs, &qiov, pos);
1973 if (ret < 0) {
1974 return ret;
1975 }
1976
1977 return size;
1978 }
1979
1980 int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
1981 {
1982 return bdrv_rw_vmstate(bs, qiov, pos, false);
1983 }
1984
1985 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1986 int64_t pos, int size)
1987 {
1988 QEMUIOVector qiov;
1989 struct iovec iov = {
1990 .iov_base = buf,
1991 .iov_len = size,
1992 };
1993 int ret;
1994
1995 qemu_iovec_init_external(&qiov, &iov, 1);
1996 ret = bdrv_readv_vmstate(bs, &qiov, pos);
1997 if (ret < 0) {
1998 return ret;
1999 }
2000
2001 return size;
2002 }
2003
2004 int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
2005 {
2006 return bdrv_rw_vmstate(bs, qiov, pos, true);
2007 }
2008
2009 /**************************************************************/
2010 /* async I/Os */
2011
2012 BlockAIOCB *bdrv_aio_readv(BdrvChild *child, int64_t sector_num,
2013 QEMUIOVector *qiov, int nb_sectors,
2014 BlockCompletionFunc *cb, void *opaque)
2015 {
2016 trace_bdrv_aio_readv(child->bs, sector_num, nb_sectors, opaque);
2017
2018 return bdrv_co_aio_rw_vector(child, sector_num, qiov, nb_sectors, 0,
2019 cb, opaque, false);
2020 }
2021
2022 BlockAIOCB *bdrv_aio_writev(BdrvChild *child, int64_t sector_num,
2023 QEMUIOVector *qiov, int nb_sectors,
2024 BlockCompletionFunc *cb, void *opaque)
2025 {
2026 trace_bdrv_aio_writev(child->bs, sector_num, nb_sectors, opaque);
2027
2028 return bdrv_co_aio_rw_vector(child, sector_num, qiov, nb_sectors, 0,
2029 cb, opaque, true);
2030 }
2031
2032 void bdrv_aio_cancel(BlockAIOCB *acb)
2033 {
2034 qemu_aio_ref(acb);
2035 bdrv_aio_cancel_async(acb);
2036 while (acb->refcnt > 1) {
2037 if (acb->aiocb_info->get_aio_context) {
2038 aio_poll(acb->aiocb_info->get_aio_context(acb), true);
2039 } else if (acb->bs) {
2040 aio_poll(bdrv_get_aio_context(acb->bs), true);
2041 } else {
2042 abort();
2043 }
2044 }
2045 qemu_aio_unref(acb);
2046 }
2047
2048 /* Async version of aio cancel. The caller is not blocked if the acb implements
2049 * cancel_async, otherwise we do nothing and let the request normally complete.
2050 * In either case the completion callback must be called. */
2051 void bdrv_aio_cancel_async(BlockAIOCB *acb)
2052 {
2053 if (acb->aiocb_info->cancel_async) {
2054 acb->aiocb_info->cancel_async(acb);
2055 }
2056 }
2057
2058 /**************************************************************/
2059 /* async block device emulation */
2060
2061 typedef struct BlockRequest {
2062 union {
2063 /* Used during read, write, trim */
2064 struct {
2065 int64_t sector;
2066 int nb_sectors;
2067 int flags;
2068 QEMUIOVector *qiov;
2069 };
2070 /* Used during ioctl */
2071 struct {
2072 int req;
2073 void *buf;
2074 };
2075 };
2076 BlockCompletionFunc *cb;
2077 void *opaque;
2078
2079 int error;
2080 } BlockRequest;
2081
2082 typedef struct BlockAIOCBCoroutine {
2083 BlockAIOCB common;
2084 BdrvChild *child;
2085 BlockRequest req;
2086 bool is_write;
2087 bool need_bh;
2088 bool *done;
2089 QEMUBH* bh;
2090 } BlockAIOCBCoroutine;
2091
2092 static const AIOCBInfo bdrv_em_co_aiocb_info = {
2093 .aiocb_size = sizeof(BlockAIOCBCoroutine),
2094 };
2095
2096 static void bdrv_co_complete(BlockAIOCBCoroutine *acb)
2097 {
2098 if (!acb->need_bh) {
2099 acb->common.cb(acb->common.opaque, acb->req.error);
2100 qemu_aio_unref(acb);
2101 }
2102 }
2103
2104 static void bdrv_co_em_bh(void *opaque)
2105 {
2106 BlockAIOCBCoroutine *acb = opaque;
2107
2108 assert(!acb->need_bh);
2109 qemu_bh_delete(acb->bh);
2110 bdrv_co_complete(acb);
2111 }
2112
2113 static void bdrv_co_maybe_schedule_bh(BlockAIOCBCoroutine *acb)
2114 {
2115 acb->need_bh = false;
2116 if (acb->req.error != -EINPROGRESS) {
2117 BlockDriverState *bs = acb->common.bs;
2118
2119 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_co_em_bh, acb);
2120 qemu_bh_schedule(acb->bh);
2121 }
2122 }
2123
2124 /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2125 static void coroutine_fn bdrv_co_do_rw(void *opaque)
2126 {
2127 BlockAIOCBCoroutine *acb = opaque;
2128
2129 if (!acb->is_write) {
2130 acb->req.error = bdrv_co_do_readv(acb->child, acb->req.sector,
2131 acb->req.nb_sectors, acb->req.qiov, acb->req.flags);
2132 } else {
2133 acb->req.error = bdrv_co_do_writev(acb->child, acb->req.sector,
2134 acb->req.nb_sectors, acb->req.qiov, acb->req.flags);
2135 }
2136
2137 bdrv_co_complete(acb);
2138 }
2139
2140 static BlockAIOCB *bdrv_co_aio_rw_vector(BdrvChild *child,
2141 int64_t sector_num,
2142 QEMUIOVector *qiov,
2143 int nb_sectors,
2144 BdrvRequestFlags flags,
2145 BlockCompletionFunc *cb,
2146 void *opaque,
2147 bool is_write)
2148 {
2149 Coroutine *co;
2150 BlockAIOCBCoroutine *acb;
2151
2152 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, child->bs, cb, opaque);
2153 acb->child = child;
2154 acb->need_bh = true;
2155 acb->req.error = -EINPROGRESS;
2156 acb->req.sector = sector_num;
2157 acb->req.nb_sectors = nb_sectors;
2158 acb->req.qiov = qiov;
2159 acb->req.flags = flags;
2160 acb->is_write = is_write;
2161
2162 co = qemu_coroutine_create(bdrv_co_do_rw, acb);
2163 qemu_coroutine_enter(co);
2164
2165 bdrv_co_maybe_schedule_bh(acb);
2166 return &acb->common;
2167 }
2168
2169 static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
2170 {
2171 BlockAIOCBCoroutine *acb = opaque;
2172 BlockDriverState *bs = acb->common.bs;
2173
2174 acb->req.error = bdrv_co_flush(bs);
2175 bdrv_co_complete(acb);
2176 }
2177
2178 BlockAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2179 BlockCompletionFunc *cb, void *opaque)
2180 {
2181 trace_bdrv_aio_flush(bs, opaque);
2182
2183 Coroutine *co;
2184 BlockAIOCBCoroutine *acb;
2185
2186 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
2187 acb->need_bh = true;
2188 acb->req.error = -EINPROGRESS;
2189
2190 co = qemu_coroutine_create(bdrv_aio_flush_co_entry, acb);
2191 qemu_coroutine_enter(co);
2192
2193 bdrv_co_maybe_schedule_bh(acb);
2194 return &acb->common;
2195 }
2196
2197 static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
2198 {
2199 BlockAIOCBCoroutine *acb = opaque;
2200 BlockDriverState *bs = acb->common.bs;
2201
2202 acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
2203 bdrv_co_complete(acb);
2204 }
2205
2206 BlockAIOCB *bdrv_aio_discard(BlockDriverState *bs,
2207 int64_t sector_num, int nb_sectors,
2208 BlockCompletionFunc *cb, void *opaque)
2209 {
2210 Coroutine *co;
2211 BlockAIOCBCoroutine *acb;
2212
2213 trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
2214
2215 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
2216 acb->need_bh = true;
2217 acb->req.error = -EINPROGRESS;
2218 acb->req.sector = sector_num;
2219 acb->req.nb_sectors = nb_sectors;
2220 co = qemu_coroutine_create(bdrv_aio_discard_co_entry, acb);
2221 qemu_coroutine_enter(co);
2222
2223 bdrv_co_maybe_schedule_bh(acb);
2224 return &acb->common;
2225 }
2226
2227 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
2228 BlockCompletionFunc *cb, void *opaque)
2229 {
2230 BlockAIOCB *acb;
2231
2232 acb = g_malloc(aiocb_info->aiocb_size);
2233 acb->aiocb_info = aiocb_info;
2234 acb->bs = bs;
2235 acb->cb = cb;
2236 acb->opaque = opaque;
2237 acb->refcnt = 1;
2238 return acb;
2239 }
2240
2241 void qemu_aio_ref(void *p)
2242 {
2243 BlockAIOCB *acb = p;
2244 acb->refcnt++;
2245 }
2246
2247 void qemu_aio_unref(void *p)
2248 {
2249 BlockAIOCB *acb = p;
2250 assert(acb->refcnt > 0);
2251 if (--acb->refcnt == 0) {
2252 g_free(acb);
2253 }
2254 }
2255
2256 /**************************************************************/
2257 /* Coroutine block device emulation */
2258
2259 typedef struct FlushCo {
2260 BlockDriverState *bs;
2261 int ret;
2262 } FlushCo;
2263
2264
2265 static void coroutine_fn bdrv_flush_co_entry(void *opaque)
2266 {
2267 FlushCo *rwco = opaque;
2268
2269 rwco->ret = bdrv_co_flush(rwco->bs);
2270 }
2271
2272 int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2273 {
2274 int ret;
2275 BdrvTrackedRequest req;
2276
2277 if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
2278 bdrv_is_sg(bs)) {
2279 return 0;
2280 }
2281
2282 tracked_request_begin(&req, bs, 0, 0, BDRV_TRACKED_FLUSH);
2283
2284 int current_gen = bs->write_gen;
2285
2286 /* Wait until any previous flushes are completed */
2287 while (bs->flush_started_gen != bs->flushed_gen) {
2288 qemu_co_queue_wait(&bs->flush_queue);
2289 }
2290
2291 bs->flush_started_gen = current_gen;
2292
2293 /* Write back all layers by calling one driver function */
2294 if (bs->drv->bdrv_co_flush) {
2295 ret = bs->drv->bdrv_co_flush(bs);
2296 goto out;
2297 }
2298
2299 /* Write back cached data to the OS even with cache=unsafe */
2300 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS);
2301 if (bs->drv->bdrv_co_flush_to_os) {
2302 ret = bs->drv->bdrv_co_flush_to_os(bs);
2303 if (ret < 0) {
2304 goto out;
2305 }
2306 }
2307
2308 /* But don't actually force it to the disk with cache=unsafe */
2309 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2310 goto flush_parent;
2311 }
2312
2313 /* Check if we really need to flush anything */
2314 if (bs->flushed_gen == current_gen) {
2315 goto flush_parent;
2316 }
2317
2318 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
2319 if (bs->drv->bdrv_co_flush_to_disk) {
2320 ret = bs->drv->bdrv_co_flush_to_disk(bs);
2321 } else if (bs->drv->bdrv_aio_flush) {
2322 BlockAIOCB *acb;
2323 CoroutineIOCompletion co = {
2324 .coroutine = qemu_coroutine_self(),
2325 };
2326
2327 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
2328 if (acb == NULL) {
2329 ret = -EIO;
2330 } else {
2331 qemu_coroutine_yield();
2332 ret = co.ret;
2333 }
2334 } else {
2335 /*
2336 * Some block drivers always operate in either writethrough or unsafe
2337 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2338 * know how the server works (because the behaviour is hardcoded or
2339 * depends on server-side configuration), so we can't ensure that
2340 * everything is safe on disk. Returning an error doesn't work because
2341 * that would break guests even if the server operates in writethrough
2342 * mode.
2343 *
2344 * Let's hope the user knows what he's doing.
2345 */
2346 ret = 0;
2347 }
2348
2349 if (ret < 0) {
2350 goto out;
2351 }
2352
2353 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2354 * in the case of cache=unsafe, so there are no useless flushes.
2355 */
2356 flush_parent:
2357 ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0;
2358 out:
2359 /* Notify any pending flushes that we have completed */
2360 bs->flushed_gen = current_gen;
2361 qemu_co_queue_restart_all(&bs->flush_queue);
2362
2363 tracked_request_end(&req);
2364 return ret;
2365 }
2366
2367 int bdrv_flush(BlockDriverState *bs)
2368 {
2369 Coroutine *co;
2370 FlushCo flush_co = {
2371 .bs = bs,
2372 .ret = NOT_DONE,
2373 };
2374
2375 if (qemu_in_coroutine()) {
2376 /* Fast-path if already in coroutine context */
2377 bdrv_flush_co_entry(&flush_co);
2378 } else {
2379 AioContext *aio_context = bdrv_get_aio_context(bs);
2380
2381 co = qemu_coroutine_create(bdrv_flush_co_entry, &flush_co);
2382 qemu_coroutine_enter(co);
2383 while (flush_co.ret == NOT_DONE) {
2384 aio_poll(aio_context, true);
2385 }
2386 }
2387
2388 return flush_co.ret;
2389 }
2390
2391 typedef struct DiscardCo {
2392 BlockDriverState *bs;
2393 int64_t sector_num;
2394 int nb_sectors;
2395 int ret;
2396 } DiscardCo;
2397 static void coroutine_fn bdrv_discard_co_entry(void *opaque)
2398 {
2399 DiscardCo *rwco = opaque;
2400
2401 rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
2402 }
2403
2404 int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
2405 int nb_sectors)
2406 {
2407 BdrvTrackedRequest req;
2408 int max_discard, ret;
2409
2410 if (!bs->drv) {
2411 return -ENOMEDIUM;
2412 }
2413
2414 ret = bdrv_check_request(bs, sector_num, nb_sectors);
2415 if (ret < 0) {
2416 return ret;
2417 } else if (bs->read_only) {
2418 return -EPERM;
2419 }
2420 assert(!(bs->open_flags & BDRV_O_INACTIVE));
2421
2422 /* Do nothing if disabled. */
2423 if (!(bs->open_flags & BDRV_O_UNMAP)) {
2424 return 0;
2425 }
2426
2427 if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) {
2428 return 0;
2429 }
2430
2431 tracked_request_begin(&req, bs, sector_num << BDRV_SECTOR_BITS,
2432 nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
2433
2434 ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
2435 if (ret < 0) {
2436 goto out;
2437 }
2438
2439 max_discard = MIN_NON_ZERO(bs->bl.max_pdiscard >> BDRV_SECTOR_BITS,
2440 BDRV_REQUEST_MAX_SECTORS);
2441 while (nb_sectors > 0) {
2442 int ret;
2443 int num = nb_sectors;
2444 int discard_alignment = bs->bl.pdiscard_alignment >> BDRV_SECTOR_BITS;
2445
2446 /* align request */
2447 if (discard_alignment &&
2448 num >= discard_alignment &&
2449 sector_num % discard_alignment) {
2450 if (num > discard_alignment) {
2451 num = discard_alignment;
2452 }
2453 num -= sector_num % discard_alignment;
2454 }
2455
2456 /* limit request size */
2457 if (num > max_discard) {
2458 num = max_discard;
2459 }
2460
2461 if (bs->drv->bdrv_co_discard) {
2462 ret = bs->drv->bdrv_co_discard(bs, sector_num, num);
2463 } else {
2464 BlockAIOCB *acb;
2465 CoroutineIOCompletion co = {
2466 .coroutine = qemu_coroutine_self(),
2467 };
2468
2469 acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
2470 bdrv_co_io_em_complete, &co);
2471 if (acb == NULL) {
2472 ret = -EIO;
2473 goto out;
2474 } else {
2475 qemu_coroutine_yield();
2476 ret = co.ret;
2477 }
2478 }
2479 if (ret && ret != -ENOTSUP) {
2480 goto out;
2481 }
2482
2483 sector_num += num;
2484 nb_sectors -= num;
2485 }
2486 ret = 0;
2487 out:
2488 ++bs->write_gen;
2489 bdrv_set_dirty(bs, req.offset >> BDRV_SECTOR_BITS,
2490 req.bytes >> BDRV_SECTOR_BITS);
2491 tracked_request_end(&req);
2492 return ret;
2493 }
2494
2495 int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
2496 {
2497 Coroutine *co;
2498 DiscardCo rwco = {
2499 .bs = bs,
2500 .sector_num = sector_num,
2501 .nb_sectors = nb_sectors,
2502 .ret = NOT_DONE,
2503 };
2504
2505 if (qemu_in_coroutine()) {
2506 /* Fast-path if already in coroutine context */
2507 bdrv_discard_co_entry(&rwco);
2508 } else {
2509 AioContext *aio_context = bdrv_get_aio_context(bs);
2510
2511 co = qemu_coroutine_create(bdrv_discard_co_entry, &rwco);
2512 qemu_coroutine_enter(co);
2513 while (rwco.ret == NOT_DONE) {
2514 aio_poll(aio_context, true);
2515 }
2516 }
2517
2518 return rwco.ret;
2519 }
2520
2521 static int bdrv_co_do_ioctl(BlockDriverState *bs, int req, void *buf)
2522 {
2523 BlockDriver *drv = bs->drv;
2524 BdrvTrackedRequest tracked_req;
2525 CoroutineIOCompletion co = {
2526 .coroutine = qemu_coroutine_self(),
2527 };
2528 BlockAIOCB *acb;
2529
2530 tracked_request_begin(&tracked_req, bs, 0, 0, BDRV_TRACKED_IOCTL);
2531 if (!drv || !drv->bdrv_aio_ioctl) {
2532 co.ret = -ENOTSUP;
2533 goto out;
2534 }
2535
2536 acb = drv->bdrv_aio_ioctl(bs, req, buf, bdrv_co_io_em_complete, &co);
2537 if (!acb) {
2538 co.ret = -ENOTSUP;
2539 goto out;
2540 }
2541 qemu_coroutine_yield();
2542 out:
2543 tracked_request_end(&tracked_req);
2544 return co.ret;
2545 }
2546
2547 typedef struct {
2548 BlockDriverState *bs;
2549 int req;
2550 void *buf;
2551 int ret;
2552 } BdrvIoctlCoData;
2553
2554 static void coroutine_fn bdrv_co_ioctl_entry(void *opaque)
2555 {
2556 BdrvIoctlCoData *data = opaque;
2557 data->ret = bdrv_co_do_ioctl(data->bs, data->req, data->buf);
2558 }
2559
2560 /* needed for generic scsi interface */
2561 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2562 {
2563 BdrvIoctlCoData data = {
2564 .bs = bs,
2565 .req = req,
2566 .buf = buf,
2567 .ret = -EINPROGRESS,
2568 };
2569
2570 if (qemu_in_coroutine()) {
2571 /* Fast-path if already in coroutine context */
2572 bdrv_co_ioctl_entry(&data);
2573 } else {
2574 Coroutine *co = qemu_coroutine_create(bdrv_co_ioctl_entry, &data);
2575
2576 qemu_coroutine_enter(co);
2577 while (data.ret == -EINPROGRESS) {
2578 aio_poll(bdrv_get_aio_context(bs), true);
2579 }
2580 }
2581 return data.ret;
2582 }
2583
2584 static void coroutine_fn bdrv_co_aio_ioctl_entry(void *opaque)
2585 {
2586 BlockAIOCBCoroutine *acb = opaque;
2587 acb->req.error = bdrv_co_do_ioctl(acb->common.bs,
2588 acb->req.req, acb->req.buf);
2589 bdrv_co_complete(acb);
2590 }
2591
2592 BlockAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2593 unsigned long int req, void *buf,
2594 BlockCompletionFunc *cb, void *opaque)
2595 {
2596 BlockAIOCBCoroutine *acb = qemu_aio_get(&bdrv_em_co_aiocb_info,
2597 bs, cb, opaque);
2598 Coroutine *co;
2599
2600 acb->need_bh = true;
2601 acb->req.error = -EINPROGRESS;
2602 acb->req.req = req;
2603 acb->req.buf = buf;
2604 co = qemu_coroutine_create(bdrv_co_aio_ioctl_entry, acb);
2605 qemu_coroutine_enter(co);
2606
2607 bdrv_co_maybe_schedule_bh(acb);
2608 return &acb->common;
2609 }
2610
2611 void *qemu_blockalign(BlockDriverState *bs, size_t size)
2612 {
2613 return qemu_memalign(bdrv_opt_mem_align(bs), size);
2614 }
2615
2616 void *qemu_blockalign0(BlockDriverState *bs, size_t size)
2617 {
2618 return memset(qemu_blockalign(bs, size), 0, size);
2619 }
2620
2621 void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
2622 {
2623 size_t align = bdrv_opt_mem_align(bs);
2624
2625 /* Ensure that NULL is never returned on success */
2626 assert(align > 0);
2627 if (size == 0) {
2628 size = align;
2629 }
2630
2631 return qemu_try_memalign(align, size);
2632 }
2633
2634 void *qemu_try_blockalign0(BlockDriverState *bs, size_t size)
2635 {
2636 void *mem = qemu_try_blockalign(bs, size);
2637
2638 if (mem) {
2639 memset(mem, 0, size);
2640 }
2641
2642 return mem;
2643 }
2644
2645 /*
2646 * Check if all memory in this vector is sector aligned.
2647 */
2648 bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
2649 {
2650 int i;
2651 size_t alignment = bdrv_min_mem_align(bs);
2652
2653 for (i = 0; i < qiov->niov; i++) {
2654 if ((uintptr_t) qiov->iov[i].iov_base % alignment) {
2655 return false;
2656 }
2657 if (qiov->iov[i].iov_len % alignment) {
2658 return false;
2659 }
2660 }
2661
2662 return true;
2663 }
2664
2665 void bdrv_add_before_write_notifier(BlockDriverState *bs,
2666 NotifierWithReturn *notifier)
2667 {
2668 notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
2669 }
2670
2671 void bdrv_io_plug(BlockDriverState *bs)
2672 {
2673 BdrvChild *child;
2674
2675 QLIST_FOREACH(child, &bs->children, next) {
2676 bdrv_io_plug(child->bs);
2677 }
2678
2679 if (bs->io_plugged++ == 0 && bs->io_plug_disabled == 0) {
2680 BlockDriver *drv = bs->drv;
2681 if (drv && drv->bdrv_io_plug) {
2682 drv->bdrv_io_plug(bs);
2683 }
2684 }
2685 }
2686
2687 void bdrv_io_unplug(BlockDriverState *bs)
2688 {
2689 BdrvChild *child;
2690
2691 assert(bs->io_plugged);
2692 if (--bs->io_plugged == 0 && bs->io_plug_disabled == 0) {
2693 BlockDriver *drv = bs->drv;
2694 if (drv && drv->bdrv_io_unplug) {
2695 drv->bdrv_io_unplug(bs);
2696 }
2697 }
2698
2699 QLIST_FOREACH(child, &bs->children, next) {
2700 bdrv_io_unplug(child->bs);
2701 }
2702 }
2703
2704 void bdrv_io_unplugged_begin(BlockDriverState *bs)
2705 {
2706 BdrvChild *child;
2707
2708 if (bs->io_plug_disabled++ == 0 && bs->io_plugged > 0) {
2709 BlockDriver *drv = bs->drv;
2710 if (drv && drv->bdrv_io_unplug) {
2711 drv->bdrv_io_unplug(bs);
2712 }
2713 }
2714
2715 QLIST_FOREACH(child, &bs->children, next) {
2716 bdrv_io_unplugged_begin(child->bs);
2717 }
2718 }
2719
2720 void bdrv_io_unplugged_end(BlockDriverState *bs)
2721 {
2722 BdrvChild *child;
2723
2724 assert(bs->io_plug_disabled);
2725 QLIST_FOREACH(child, &bs->children, next) {
2726 bdrv_io_unplugged_end(child->bs);
2727 }
2728
2729 if (--bs->io_plug_disabled == 0 && bs->io_plugged > 0) {
2730 BlockDriver *drv = bs->drv;
2731 if (drv && drv->bdrv_io_plug) {
2732 drv->bdrv_io_plug(bs);
2733 }
2734 }
2735 }