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