]> git.proxmox.com Git - mirror_qemu.git/blame - block/io.c
iotests: Test qemu-img convert -C --salvage
[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"
7719f3c9 28#include "block/aio-wait.h"
61007b31 29#include "block/blockjob.h"
f321dcb5 30#include "block/blockjob_int.h"
61007b31 31#include "block/block_int.h"
f348b6d1 32#include "qemu/cutils.h"
da34e65c 33#include "qapi/error.h"
d49b6836 34#include "qemu/error-report.h"
61007b31
SH
35
36#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
37
cb2e2878
EB
38/* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
39#define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
40
7f8f03ef 41static void bdrv_parent_cb_resize(BlockDriverState *bs);
d05aa8bb 42static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
f5a5ca79 43 int64_t offset, int bytes, BdrvRequestFlags flags);
61007b31 44
6cd5c9d7
KW
45void bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore,
46 bool ignore_bds_parents)
61007b31 47{
02d21300 48 BdrvChild *c, *next;
27ccdd52 49
02d21300 50 QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
6cd5c9d7 51 if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
0152bf40
KW
52 continue;
53 }
4be6a6d1 54 bdrv_parent_drained_begin_single(c, false);
ce0f1412
PB
55 }
56}
61007b31 57
6cd5c9d7
KW
58void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore,
59 bool ignore_bds_parents)
ce0f1412 60{
02d21300 61 BdrvChild *c, *next;
27ccdd52 62
02d21300 63 QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
6cd5c9d7 64 if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
0152bf40
KW
65 continue;
66 }
c2066af0
KW
67 if (c->role->drained_end) {
68 c->role->drained_end(c);
69 }
27ccdd52 70 }
61007b31
SH
71}
72
4be6a6d1
KW
73static bool bdrv_parent_drained_poll_single(BdrvChild *c)
74{
75 if (c->role->drained_poll) {
76 return c->role->drained_poll(c);
77 }
78 return false;
79}
80
6cd5c9d7
KW
81static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,
82 bool ignore_bds_parents)
89bd0305
KW
83{
84 BdrvChild *c, *next;
85 bool busy = false;
86
87 QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
6cd5c9d7 88 if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
89bd0305
KW
89 continue;
90 }
4be6a6d1 91 busy |= bdrv_parent_drained_poll_single(c);
89bd0305
KW
92 }
93
94 return busy;
95}
96
4be6a6d1
KW
97void bdrv_parent_drained_begin_single(BdrvChild *c, bool poll)
98{
99 if (c->role->drained_begin) {
100 c->role->drained_begin(c);
101 }
102 if (poll) {
103 BDRV_POLL_WHILE(c->bs, bdrv_parent_drained_poll_single(c));
104 }
105}
106
d9e0dfa2
EB
107static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src)
108{
109 dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer);
110 dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer);
111 dst->opt_mem_alignment = MAX(dst->opt_mem_alignment,
112 src->opt_mem_alignment);
113 dst->min_mem_alignment = MAX(dst->min_mem_alignment,
114 src->min_mem_alignment);
115 dst->max_iov = MIN_NON_ZERO(dst->max_iov, src->max_iov);
116}
117
61007b31
SH
118void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
119{
120 BlockDriver *drv = bs->drv;
121 Error *local_err = NULL;
122
123 memset(&bs->bl, 0, sizeof(bs->bl));
124
125 if (!drv) {
126 return;
127 }
128
79ba8c98 129 /* Default alignment based on whether driver has byte interface */
e31f6864
EB
130 bs->bl.request_alignment = (drv->bdrv_co_preadv ||
131 drv->bdrv_aio_preadv) ? 1 : 512;
79ba8c98 132
61007b31
SH
133 /* Take some limits from the children as a default */
134 if (bs->file) {
9a4f4c31 135 bdrv_refresh_limits(bs->file->bs, &local_err);
61007b31
SH
136 if (local_err) {
137 error_propagate(errp, local_err);
138 return;
139 }
d9e0dfa2 140 bdrv_merge_limits(&bs->bl, &bs->file->bs->bl);
61007b31 141 } else {
4196d2f0 142 bs->bl.min_mem_alignment = 512;
459b4e66 143 bs->bl.opt_mem_alignment = getpagesize();
bd44feb7
SH
144
145 /* Safe default since most protocols use readv()/writev()/etc */
146 bs->bl.max_iov = IOV_MAX;
61007b31
SH
147 }
148
760e0063
KW
149 if (bs->backing) {
150 bdrv_refresh_limits(bs->backing->bs, &local_err);
61007b31
SH
151 if (local_err) {
152 error_propagate(errp, local_err);
153 return;
154 }
d9e0dfa2 155 bdrv_merge_limits(&bs->bl, &bs->backing->bs->bl);
61007b31
SH
156 }
157
158 /* Then let the driver override it */
159 if (drv->bdrv_refresh_limits) {
160 drv->bdrv_refresh_limits(bs, errp);
161 }
162}
163
164/**
165 * The copy-on-read flag is actually a reference count so multiple users may
166 * use the feature without worrying about clobbering its previous state.
167 * Copy-on-read stays enabled until all users have called to disable it.
168 */
169void bdrv_enable_copy_on_read(BlockDriverState *bs)
170{
d3faa13e 171 atomic_inc(&bs->copy_on_read);
61007b31
SH
172}
173
174void bdrv_disable_copy_on_read(BlockDriverState *bs)
175{
d3faa13e
PB
176 int old = atomic_fetch_dec(&bs->copy_on_read);
177 assert(old >= 1);
61007b31
SH
178}
179
61124f03
PB
180typedef struct {
181 Coroutine *co;
182 BlockDriverState *bs;
183 bool done;
481cad48 184 bool begin;
b0165585 185 bool recursive;
fe4f0614 186 bool poll;
0152bf40 187 BdrvChild *parent;
6cd5c9d7 188 bool ignore_bds_parents;
61124f03
PB
189} BdrvCoDrainData;
190
191static void coroutine_fn bdrv_drain_invoke_entry(void *opaque)
192{
193 BdrvCoDrainData *data = opaque;
194 BlockDriverState *bs = data->bs;
195
481cad48 196 if (data->begin) {
f8ea8dac 197 bs->drv->bdrv_co_drain_begin(bs);
481cad48
MP
198 } else {
199 bs->drv->bdrv_co_drain_end(bs);
200 }
61124f03
PB
201
202 /* Set data->done before reading bs->wakeup. */
203 atomic_mb_set(&data->done, true);
0109e7e6
KW
204 bdrv_dec_in_flight(bs);
205
206 if (data->begin) {
207 g_free(data);
208 }
61124f03
PB
209}
210
db0289b9 211/* Recursively call BlockDriver.bdrv_co_drain_begin/end callbacks */
7d40d9ef 212static void bdrv_drain_invoke(BlockDriverState *bs, bool begin)
61124f03 213{
0109e7e6 214 BdrvCoDrainData *data;
61124f03 215
f8ea8dac 216 if (!bs->drv || (begin && !bs->drv->bdrv_co_drain_begin) ||
481cad48 217 (!begin && !bs->drv->bdrv_co_drain_end)) {
61124f03
PB
218 return;
219 }
220
0109e7e6
KW
221 data = g_new(BdrvCoDrainData, 1);
222 *data = (BdrvCoDrainData) {
223 .bs = bs,
224 .done = false,
225 .begin = begin
226 };
227
228 /* Make sure the driver callback completes during the polling phase for
229 * drain_begin. */
230 bdrv_inc_in_flight(bs);
231 data->co = qemu_coroutine_create(bdrv_drain_invoke_entry, data);
232 aio_co_schedule(bdrv_get_aio_context(bs), data->co);
233
234 if (!begin) {
235 BDRV_POLL_WHILE(bs, !data->done);
236 g_free(data);
237 }
61124f03
PB
238}
239
1cc8e54a 240/* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
fe4f0614 241bool bdrv_drain_poll(BlockDriverState *bs, bool recursive,
6cd5c9d7 242 BdrvChild *ignore_parent, bool ignore_bds_parents)
89bd0305 243{
fe4f0614
KW
244 BdrvChild *child, *next;
245
6cd5c9d7 246 if (bdrv_parent_drained_poll(bs, ignore_parent, ignore_bds_parents)) {
89bd0305
KW
247 return true;
248 }
249
fe4f0614
KW
250 if (atomic_read(&bs->in_flight)) {
251 return true;
252 }
253
254 if (recursive) {
6cd5c9d7 255 assert(!ignore_bds_parents);
fe4f0614 256 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
6cd5c9d7 257 if (bdrv_drain_poll(child->bs, recursive, child, false)) {
fe4f0614
KW
258 return true;
259 }
260 }
261 }
262
263 return false;
89bd0305
KW
264}
265
fe4f0614 266static bool bdrv_drain_poll_top_level(BlockDriverState *bs, bool recursive,
89bd0305 267 BdrvChild *ignore_parent)
1cc8e54a 268{
6cd5c9d7 269 return bdrv_drain_poll(bs, recursive, ignore_parent, false);
1cc8e54a
KW
270}
271
b0165585 272static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
6cd5c9d7
KW
273 BdrvChild *parent, bool ignore_bds_parents,
274 bool poll);
b0165585 275static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
6cd5c9d7 276 BdrvChild *parent, bool ignore_bds_parents);
0152bf40 277
a77fd4bb
FZ
278static void bdrv_co_drain_bh_cb(void *opaque)
279{
280 BdrvCoDrainData *data = opaque;
281 Coroutine *co = data->co;
99723548 282 BlockDriverState *bs = data->bs;
a77fd4bb 283
c8ca33d0 284 if (bs) {
aa1361d5
KW
285 AioContext *ctx = bdrv_get_aio_context(bs);
286 AioContext *co_ctx = qemu_coroutine_get_aio_context(co);
287
288 /*
289 * When the coroutine yielded, the lock for its home context was
290 * released, so we need to re-acquire it here. If it explicitly
291 * acquired a different context, the lock is still held and we don't
292 * want to lock it a second time (or AIO_WAIT_WHILE() would hang).
293 */
294 if (ctx == co_ctx) {
295 aio_context_acquire(ctx);
296 }
c8ca33d0
KW
297 bdrv_dec_in_flight(bs);
298 if (data->begin) {
6cd5c9d7
KW
299 bdrv_do_drained_begin(bs, data->recursive, data->parent,
300 data->ignore_bds_parents, data->poll);
c8ca33d0 301 } else {
6cd5c9d7
KW
302 bdrv_do_drained_end(bs, data->recursive, data->parent,
303 data->ignore_bds_parents);
c8ca33d0 304 }
aa1361d5
KW
305 if (ctx == co_ctx) {
306 aio_context_release(ctx);
307 }
481cad48 308 } else {
c8ca33d0
KW
309 assert(data->begin);
310 bdrv_drain_all_begin();
481cad48
MP
311 }
312
a77fd4bb 313 data->done = true;
1919631e 314 aio_co_wake(co);
a77fd4bb
FZ
315}
316
481cad48 317static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
b0165585 318 bool begin, bool recursive,
6cd5c9d7
KW
319 BdrvChild *parent,
320 bool ignore_bds_parents,
321 bool poll)
a77fd4bb
FZ
322{
323 BdrvCoDrainData data;
324
325 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
c40a2545 326 * other coroutines run if they were queued by aio_co_enter(). */
a77fd4bb
FZ
327
328 assert(qemu_in_coroutine());
329 data = (BdrvCoDrainData) {
330 .co = qemu_coroutine_self(),
331 .bs = bs,
332 .done = false,
481cad48 333 .begin = begin,
b0165585 334 .recursive = recursive,
0152bf40 335 .parent = parent,
6cd5c9d7 336 .ignore_bds_parents = ignore_bds_parents,
fe4f0614 337 .poll = poll,
a77fd4bb 338 };
c8ca33d0
KW
339 if (bs) {
340 bdrv_inc_in_flight(bs);
341 }
fffb6e12
PB
342 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs),
343 bdrv_co_drain_bh_cb, &data);
a77fd4bb
FZ
344
345 qemu_coroutine_yield();
346 /* If we are resumed from some other event (such as an aio completion or a
347 * timer callback), it is a bug in the caller that should be fixed. */
348 assert(data.done);
349}
350
dcf94a23 351void bdrv_do_drained_begin_quiesce(BlockDriverState *bs,
6cd5c9d7 352 BdrvChild *parent, bool ignore_bds_parents)
6820643f 353{
dcf94a23 354 assert(!qemu_in_coroutine());
d42cf288 355
60369b86 356 /* Stop things in parent-to-child order */
414c2ec3 357 if (atomic_fetch_inc(&bs->quiesce_counter) == 0) {
6820643f 358 aio_disable_external(bdrv_get_aio_context(bs));
6820643f
KW
359 }
360
6cd5c9d7 361 bdrv_parent_drained_begin(bs, parent, ignore_bds_parents);
7d40d9ef 362 bdrv_drain_invoke(bs, true);
dcf94a23
KW
363}
364
365static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
6cd5c9d7
KW
366 BdrvChild *parent, bool ignore_bds_parents,
367 bool poll)
dcf94a23
KW
368{
369 BdrvChild *child, *next;
370
371 if (qemu_in_coroutine()) {
6cd5c9d7
KW
372 bdrv_co_yield_to_drain(bs, true, recursive, parent, ignore_bds_parents,
373 poll);
dcf94a23
KW
374 return;
375 }
376
6cd5c9d7 377 bdrv_do_drained_begin_quiesce(bs, parent, ignore_bds_parents);
d30b8e64 378
b0165585 379 if (recursive) {
6cd5c9d7 380 assert(!ignore_bds_parents);
d736f119 381 bs->recursive_quiesce_counter++;
b0165585 382 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
6cd5c9d7
KW
383 bdrv_do_drained_begin(child->bs, true, child, ignore_bds_parents,
384 false);
b0165585
KW
385 }
386 }
fe4f0614
KW
387
388 /*
389 * Wait for drained requests to finish.
390 *
391 * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
392 * call is needed so things in this AioContext can make progress even
393 * though we don't return to the main AioContext loop - this automatically
394 * includes other nodes in the same AioContext and therefore all child
395 * nodes.
396 */
397 if (poll) {
6cd5c9d7 398 assert(!ignore_bds_parents);
fe4f0614
KW
399 BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, recursive, parent));
400 }
6820643f
KW
401}
402
0152bf40
KW
403void bdrv_drained_begin(BlockDriverState *bs)
404{
6cd5c9d7 405 bdrv_do_drained_begin(bs, false, NULL, false, true);
b0165585
KW
406}
407
408void bdrv_subtree_drained_begin(BlockDriverState *bs)
409{
6cd5c9d7 410 bdrv_do_drained_begin(bs, true, NULL, false, true);
0152bf40
KW
411}
412
6cd5c9d7
KW
413static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
414 BdrvChild *parent, bool ignore_bds_parents)
6820643f 415{
b0165585 416 BdrvChild *child, *next;
0f115168
KW
417 int old_quiesce_counter;
418
481cad48 419 if (qemu_in_coroutine()) {
6cd5c9d7
KW
420 bdrv_co_yield_to_drain(bs, false, recursive, parent, ignore_bds_parents,
421 false);
481cad48
MP
422 return;
423 }
6820643f 424 assert(bs->quiesce_counter > 0);
6820643f 425
60369b86 426 /* Re-enable things in child-to-parent order */
7d40d9ef 427 bdrv_drain_invoke(bs, false);
6cd5c9d7 428 bdrv_parent_drained_end(bs, parent, ignore_bds_parents);
5cb2737e
HR
429
430 old_quiesce_counter = atomic_fetch_dec(&bs->quiesce_counter);
0f115168 431 if (old_quiesce_counter == 1) {
0f115168
KW
432 aio_enable_external(bdrv_get_aio_context(bs));
433 }
b0165585
KW
434
435 if (recursive) {
6cd5c9d7 436 assert(!ignore_bds_parents);
d736f119 437 bs->recursive_quiesce_counter--;
b0165585 438 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
6cd5c9d7 439 bdrv_do_drained_end(child->bs, true, child, ignore_bds_parents);
b0165585
KW
440 }
441 }
6820643f
KW
442}
443
0152bf40
KW
444void bdrv_drained_end(BlockDriverState *bs)
445{
6cd5c9d7 446 bdrv_do_drained_end(bs, false, NULL, false);
b0165585
KW
447}
448
449void bdrv_subtree_drained_end(BlockDriverState *bs)
450{
6cd5c9d7 451 bdrv_do_drained_end(bs, true, NULL, false);
0152bf40
KW
452}
453
d736f119
KW
454void bdrv_apply_subtree_drain(BdrvChild *child, BlockDriverState *new_parent)
455{
456 int i;
457
458 for (i = 0; i < new_parent->recursive_quiesce_counter; i++) {
6cd5c9d7 459 bdrv_do_drained_begin(child->bs, true, child, false, true);
d736f119
KW
460 }
461}
462
463void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_parent)
464{
465 int i;
466
467 for (i = 0; i < old_parent->recursive_quiesce_counter; i++) {
6cd5c9d7 468 bdrv_do_drained_end(child->bs, true, child, false);
d736f119
KW
469 }
470}
471
61007b31 472/*
67da1dc5
FZ
473 * Wait for pending requests to complete on a single BlockDriverState subtree,
474 * and suspend block driver's internal I/O until next request arrives.
61007b31 475 *
61007b31
SH
476 * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
477 * AioContext.
478 */
b6e84c97 479void coroutine_fn bdrv_co_drain(BlockDriverState *bs)
61007b31 480{
6820643f
KW
481 assert(qemu_in_coroutine());
482 bdrv_drained_begin(bs);
483 bdrv_drained_end(bs);
b6e84c97 484}
f406c03c 485
b6e84c97
PB
486void bdrv_drain(BlockDriverState *bs)
487{
6820643f
KW
488 bdrv_drained_begin(bs);
489 bdrv_drained_end(bs);
61007b31
SH
490}
491
c13ad59f
KW
492static void bdrv_drain_assert_idle(BlockDriverState *bs)
493{
494 BdrvChild *child, *next;
495
496 assert(atomic_read(&bs->in_flight) == 0);
497 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
498 bdrv_drain_assert_idle(child->bs);
499 }
500}
501
0f12264e
KW
502unsigned int bdrv_drain_all_count = 0;
503
504static bool bdrv_drain_all_poll(void)
505{
506 BlockDriverState *bs = NULL;
507 bool result = false;
508
0f12264e
KW
509 /* bdrv_drain_poll() can't make changes to the graph and we are holding the
510 * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
511 while ((bs = bdrv_next_all_states(bs))) {
512 AioContext *aio_context = bdrv_get_aio_context(bs);
513 aio_context_acquire(aio_context);
514 result |= bdrv_drain_poll(bs, false, NULL, true);
515 aio_context_release(aio_context);
516 }
517
518 return result;
519}
520
61007b31
SH
521/*
522 * Wait for pending requests to complete across all BlockDriverStates
523 *
524 * This function does not flush data to disk, use bdrv_flush_all() for that
525 * after calling this function.
c0778f66
AG
526 *
527 * This pauses all block jobs and disables external clients. It must
528 * be paired with bdrv_drain_all_end().
529 *
530 * NOTE: no new block jobs or BlockDriverStates can be created between
531 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
61007b31 532 */
c0778f66 533void bdrv_drain_all_begin(void)
61007b31 534{
0f12264e 535 BlockDriverState *bs = NULL;
61007b31 536
c8ca33d0 537 if (qemu_in_coroutine()) {
0f12264e 538 bdrv_co_yield_to_drain(NULL, true, false, NULL, true, true);
c8ca33d0
KW
539 return;
540 }
541
0f12264e
KW
542 /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
543 * loop AioContext, so make sure we're in the main context. */
9a7e86c8 544 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
0f12264e
KW
545 assert(bdrv_drain_all_count < INT_MAX);
546 bdrv_drain_all_count++;
9a7e86c8 547
0f12264e
KW
548 /* Quiesce all nodes, without polling in-flight requests yet. The graph
549 * cannot change during this loop. */
550 while ((bs = bdrv_next_all_states(bs))) {
61007b31
SH
551 AioContext *aio_context = bdrv_get_aio_context(bs);
552
553 aio_context_acquire(aio_context);
0f12264e 554 bdrv_do_drained_begin(bs, false, NULL, true, false);
61007b31
SH
555 aio_context_release(aio_context);
556 }
557
0f12264e 558 /* Now poll the in-flight requests */
cfe29d82 559 AIO_WAIT_WHILE(NULL, bdrv_drain_all_poll());
0f12264e
KW
560
561 while ((bs = bdrv_next_all_states(bs))) {
c13ad59f 562 bdrv_drain_assert_idle(bs);
61007b31 563 }
c0778f66
AG
564}
565
566void bdrv_drain_all_end(void)
567{
0f12264e 568 BlockDriverState *bs = NULL;
c0778f66 569
0f12264e 570 while ((bs = bdrv_next_all_states(bs))) {
61007b31
SH
571 AioContext *aio_context = bdrv_get_aio_context(bs);
572
573 aio_context_acquire(aio_context);
0f12264e 574 bdrv_do_drained_end(bs, false, NULL, true);
61007b31
SH
575 aio_context_release(aio_context);
576 }
0f12264e
KW
577
578 assert(bdrv_drain_all_count > 0);
579 bdrv_drain_all_count--;
61007b31
SH
580}
581
c0778f66
AG
582void bdrv_drain_all(void)
583{
584 bdrv_drain_all_begin();
585 bdrv_drain_all_end();
586}
587
61007b31
SH
588/**
589 * Remove an active request from the tracked requests list
590 *
591 * This function should be called when a tracked request is completing.
592 */
593static void tracked_request_end(BdrvTrackedRequest *req)
594{
595 if (req->serialising) {
20fc71b2 596 atomic_dec(&req->bs->serialising_in_flight);
61007b31
SH
597 }
598
3783fa3d 599 qemu_co_mutex_lock(&req->bs->reqs_lock);
61007b31
SH
600 QLIST_REMOVE(req, list);
601 qemu_co_queue_restart_all(&req->wait_queue);
3783fa3d 602 qemu_co_mutex_unlock(&req->bs->reqs_lock);
61007b31
SH
603}
604
605/**
606 * Add an active request to the tracked requests list
607 */
608static void tracked_request_begin(BdrvTrackedRequest *req,
609 BlockDriverState *bs,
610 int64_t offset,
22931a15 611 uint64_t bytes,
ebde595c 612 enum BdrvTrackedRequestType type)
61007b31 613{
22931a15
FZ
614 assert(bytes <= INT64_MAX && offset <= INT64_MAX - bytes);
615
61007b31
SH
616 *req = (BdrvTrackedRequest){
617 .bs = bs,
618 .offset = offset,
619 .bytes = bytes,
ebde595c 620 .type = type,
61007b31
SH
621 .co = qemu_coroutine_self(),
622 .serialising = false,
623 .overlap_offset = offset,
624 .overlap_bytes = bytes,
625 };
626
627 qemu_co_queue_init(&req->wait_queue);
628
3783fa3d 629 qemu_co_mutex_lock(&bs->reqs_lock);
61007b31 630 QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
3783fa3d 631 qemu_co_mutex_unlock(&bs->reqs_lock);
61007b31
SH
632}
633
634static void mark_request_serialising(BdrvTrackedRequest *req, uint64_t align)
635{
636 int64_t overlap_offset = req->offset & ~(align - 1);
22931a15 637 uint64_t overlap_bytes = ROUND_UP(req->offset + req->bytes, align)
61007b31
SH
638 - overlap_offset;
639
640 if (!req->serialising) {
20fc71b2 641 atomic_inc(&req->bs->serialising_in_flight);
61007b31
SH
642 req->serialising = true;
643 }
644
645 req->overlap_offset = MIN(req->overlap_offset, overlap_offset);
646 req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes);
647}
648
09d2f948
VSO
649static bool is_request_serialising_and_aligned(BdrvTrackedRequest *req)
650{
651 /*
652 * If the request is serialising, overlap_offset and overlap_bytes are set,
653 * so we can check if the request is aligned. Otherwise, don't care and
654 * return false.
655 */
656
657 return req->serialising && (req->offset == req->overlap_offset) &&
658 (req->bytes == req->overlap_bytes);
659}
660
244483e6
KW
661/**
662 * Round a region to cluster boundaries
663 */
664void bdrv_round_to_clusters(BlockDriverState *bs,
7cfd5275 665 int64_t offset, int64_t bytes,
244483e6 666 int64_t *cluster_offset,
7cfd5275 667 int64_t *cluster_bytes)
244483e6
KW
668{
669 BlockDriverInfo bdi;
670
671 if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
672 *cluster_offset = offset;
673 *cluster_bytes = bytes;
674 } else {
675 int64_t c = bdi.cluster_size;
676 *cluster_offset = QEMU_ALIGN_DOWN(offset, c);
677 *cluster_bytes = QEMU_ALIGN_UP(offset - *cluster_offset + bytes, c);
678 }
679}
680
61007b31
SH
681static int bdrv_get_cluster_size(BlockDriverState *bs)
682{
683 BlockDriverInfo bdi;
684 int ret;
685
686 ret = bdrv_get_info(bs, &bdi);
687 if (ret < 0 || bdi.cluster_size == 0) {
a5b8dd2c 688 return bs->bl.request_alignment;
61007b31
SH
689 } else {
690 return bdi.cluster_size;
691 }
692}
693
694static bool tracked_request_overlaps(BdrvTrackedRequest *req,
22931a15 695 int64_t offset, uint64_t bytes)
61007b31
SH
696{
697 /* aaaa bbbb */
698 if (offset >= req->overlap_offset + req->overlap_bytes) {
699 return false;
700 }
701 /* bbbb aaaa */
702 if (req->overlap_offset >= offset + bytes) {
703 return false;
704 }
705 return true;
706}
707
99723548
PB
708void bdrv_inc_in_flight(BlockDriverState *bs)
709{
710 atomic_inc(&bs->in_flight);
711}
712
c9d1a561
PB
713void bdrv_wakeup(BlockDriverState *bs)
714{
cfe29d82 715 aio_wait_kick();
c9d1a561
PB
716}
717
99723548
PB
718void bdrv_dec_in_flight(BlockDriverState *bs)
719{
720 atomic_dec(&bs->in_flight);
c9d1a561 721 bdrv_wakeup(bs);
99723548
PB
722}
723
61007b31
SH
724static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
725{
726 BlockDriverState *bs = self->bs;
727 BdrvTrackedRequest *req;
728 bool retry;
729 bool waited = false;
730
20fc71b2 731 if (!atomic_read(&bs->serialising_in_flight)) {
61007b31
SH
732 return false;
733 }
734
735 do {
736 retry = false;
3783fa3d 737 qemu_co_mutex_lock(&bs->reqs_lock);
61007b31
SH
738 QLIST_FOREACH(req, &bs->tracked_requests, list) {
739 if (req == self || (!req->serialising && !self->serialising)) {
740 continue;
741 }
742 if (tracked_request_overlaps(req, self->overlap_offset,
743 self->overlap_bytes))
744 {
745 /* Hitting this means there was a reentrant request, for
746 * example, a block driver issuing nested requests. This must
747 * never happen since it means deadlock.
748 */
749 assert(qemu_coroutine_self() != req->co);
750
751 /* If the request is already (indirectly) waiting for us, or
752 * will wait for us as soon as it wakes up, then just go on
753 * (instead of producing a deadlock in the former case). */
754 if (!req->waiting_for) {
755 self->waiting_for = req;
3783fa3d 756 qemu_co_queue_wait(&req->wait_queue, &bs->reqs_lock);
61007b31
SH
757 self->waiting_for = NULL;
758 retry = true;
759 waited = true;
760 break;
761 }
762 }
763 }
3783fa3d 764 qemu_co_mutex_unlock(&bs->reqs_lock);
61007b31
SH
765 } while (retry);
766
767 return waited;
768}
769
770static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
771 size_t size)
772{
41ae31e3 773 if (size > BDRV_REQUEST_MAX_BYTES) {
61007b31
SH
774 return -EIO;
775 }
776
777 if (!bdrv_is_inserted(bs)) {
778 return -ENOMEDIUM;
779 }
780
781 if (offset < 0) {
782 return -EIO;
783 }
784
785 return 0;
786}
787
61007b31 788typedef struct RwCo {
e293b7a3 789 BdrvChild *child;
61007b31
SH
790 int64_t offset;
791 QEMUIOVector *qiov;
792 bool is_write;
793 int ret;
794 BdrvRequestFlags flags;
795} RwCo;
796
797static void coroutine_fn bdrv_rw_co_entry(void *opaque)
798{
799 RwCo *rwco = opaque;
800
801 if (!rwco->is_write) {
a03ef88f 802 rwco->ret = bdrv_co_preadv(rwco->child, rwco->offset,
cab3a356
KW
803 rwco->qiov->size, rwco->qiov,
804 rwco->flags);
61007b31 805 } else {
a03ef88f 806 rwco->ret = bdrv_co_pwritev(rwco->child, rwco->offset,
cab3a356
KW
807 rwco->qiov->size, rwco->qiov,
808 rwco->flags);
61007b31 809 }
4720cbee 810 aio_wait_kick();
61007b31
SH
811}
812
813/*
814 * Process a vectored synchronous request using coroutines
815 */
e293b7a3 816static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
61007b31
SH
817 QEMUIOVector *qiov, bool is_write,
818 BdrvRequestFlags flags)
819{
820 Coroutine *co;
821 RwCo rwco = {
e293b7a3 822 .child = child,
61007b31
SH
823 .offset = offset,
824 .qiov = qiov,
825 .is_write = is_write,
826 .ret = NOT_DONE,
827 .flags = flags,
828 };
829
61007b31
SH
830 if (qemu_in_coroutine()) {
831 /* Fast-path if already in coroutine context */
832 bdrv_rw_co_entry(&rwco);
833 } else {
0b8b8753 834 co = qemu_coroutine_create(bdrv_rw_co_entry, &rwco);
e92f0e19 835 bdrv_coroutine_enter(child->bs, co);
88b062c2 836 BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE);
61007b31
SH
837 }
838 return rwco.ret;
839}
840
720ff280 841int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
f5a5ca79 842 int bytes, BdrvRequestFlags flags)
61007b31 843{
0d93ed08 844 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
74021bc4 845
e293b7a3 846 return bdrv_prwv_co(child, offset, &qiov, true,
74021bc4 847 BDRV_REQ_ZERO_WRITE | flags);
61007b31
SH
848}
849
850/*
74021bc4 851 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
61007b31
SH
852 * The operation is sped up by checking the block status and only writing
853 * zeroes to the device if they currently do not return zeroes. Optional
74021bc4 854 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
465fe887 855 * BDRV_REQ_FUA).
61007b31
SH
856 *
857 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
858 */
720ff280 859int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
61007b31 860{
237d78f8
EB
861 int ret;
862 int64_t target_size, bytes, offset = 0;
720ff280 863 BlockDriverState *bs = child->bs;
61007b31 864
7286d610
EB
865 target_size = bdrv_getlength(bs);
866 if (target_size < 0) {
867 return target_size;
61007b31
SH
868 }
869
870 for (;;) {
7286d610
EB
871 bytes = MIN(target_size - offset, BDRV_REQUEST_MAX_BYTES);
872 if (bytes <= 0) {
61007b31
SH
873 return 0;
874 }
237d78f8 875 ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
61007b31 876 if (ret < 0) {
61007b31
SH
877 return ret;
878 }
879 if (ret & BDRV_BLOCK_ZERO) {
237d78f8 880 offset += bytes;
61007b31
SH
881 continue;
882 }
237d78f8 883 ret = bdrv_pwrite_zeroes(child, offset, bytes, flags);
61007b31 884 if (ret < 0) {
61007b31
SH
885 return ret;
886 }
237d78f8 887 offset += bytes;
61007b31
SH
888 }
889}
890
cf2ab8fc 891int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
f1e84741
KW
892{
893 int ret;
894
e293b7a3 895 ret = bdrv_prwv_co(child, offset, qiov, false, 0);
f1e84741
KW
896 if (ret < 0) {
897 return ret;
898 }
899
900 return qiov->size;
901}
902
2e11d756 903/* See bdrv_pwrite() for the return codes */
cf2ab8fc 904int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes)
61007b31 905{
0d93ed08 906 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
61007b31
SH
907
908 if (bytes < 0) {
909 return -EINVAL;
910 }
911
cf2ab8fc 912 return bdrv_preadv(child, offset, &qiov);
61007b31
SH
913}
914
d9ca2ea2 915int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
61007b31
SH
916{
917 int ret;
918
e293b7a3 919 ret = bdrv_prwv_co(child, offset, qiov, true, 0);
61007b31
SH
920 if (ret < 0) {
921 return ret;
922 }
923
924 return qiov->size;
925}
926
2e11d756
AG
927/* Return no. of bytes on success or < 0 on error. Important errors are:
928 -EIO generic I/O error (may happen for all errors)
929 -ENOMEDIUM No media inserted.
930 -EINVAL Invalid offset or number of bytes
931 -EACCES Trying to write a read-only device
932*/
d9ca2ea2 933int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes)
61007b31 934{
0d93ed08 935 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
61007b31
SH
936
937 if (bytes < 0) {
938 return -EINVAL;
939 }
940
d9ca2ea2 941 return bdrv_pwritev(child, offset, &qiov);
61007b31
SH
942}
943
944/*
945 * Writes to the file and ensures that no writes are reordered across this
946 * request (acts as a barrier)
947 *
948 * Returns 0 on success, -errno in error cases.
949 */
d9ca2ea2
KW
950int bdrv_pwrite_sync(BdrvChild *child, int64_t offset,
951 const void *buf, int count)
61007b31
SH
952{
953 int ret;
954
d9ca2ea2 955 ret = bdrv_pwrite(child, offset, buf, count);
61007b31
SH
956 if (ret < 0) {
957 return ret;
958 }
959
d9ca2ea2 960 ret = bdrv_flush(child->bs);
855a6a93
KW
961 if (ret < 0) {
962 return ret;
61007b31
SH
963 }
964
965 return 0;
966}
967
08844473
KW
968typedef struct CoroutineIOCompletion {
969 Coroutine *coroutine;
970 int ret;
971} CoroutineIOCompletion;
972
973static void bdrv_co_io_em_complete(void *opaque, int ret)
974{
975 CoroutineIOCompletion *co = opaque;
976
977 co->ret = ret;
b9e413dd 978 aio_co_wake(co->coroutine);
08844473
KW
979}
980
166fe960
KW
981static int coroutine_fn bdrv_driver_preadv(BlockDriverState *bs,
982 uint64_t offset, uint64_t bytes,
983 QEMUIOVector *qiov, int flags)
984{
985 BlockDriver *drv = bs->drv;
3fb06697
KW
986 int64_t sector_num;
987 unsigned int nb_sectors;
988
fa166538 989 assert(!(flags & ~BDRV_REQ_MASK));
fe0480d6 990 assert(!(flags & BDRV_REQ_NO_FALLBACK));
fa166538 991
d470ad42
HR
992 if (!drv) {
993 return -ENOMEDIUM;
994 }
995
3fb06697
KW
996 if (drv->bdrv_co_preadv) {
997 return drv->bdrv_co_preadv(bs, offset, bytes, qiov, flags);
998 }
999
edfab6a0 1000 if (drv->bdrv_aio_preadv) {
08844473
KW
1001 BlockAIOCB *acb;
1002 CoroutineIOCompletion co = {
1003 .coroutine = qemu_coroutine_self(),
1004 };
1005
edfab6a0
EB
1006 acb = drv->bdrv_aio_preadv(bs, offset, bytes, qiov, flags,
1007 bdrv_co_io_em_complete, &co);
08844473
KW
1008 if (acb == NULL) {
1009 return -EIO;
1010 } else {
1011 qemu_coroutine_yield();
1012 return co.ret;
1013 }
1014 }
edfab6a0
EB
1015
1016 sector_num = offset >> BDRV_SECTOR_BITS;
1017 nb_sectors = bytes >> BDRV_SECTOR_BITS;
1018
1019 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1020 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
41ae31e3 1021 assert(bytes <= BDRV_REQUEST_MAX_BYTES);
edfab6a0
EB
1022 assert(drv->bdrv_co_readv);
1023
1024 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
166fe960
KW
1025}
1026
78a07294
KW
1027static int coroutine_fn bdrv_driver_pwritev(BlockDriverState *bs,
1028 uint64_t offset, uint64_t bytes,
1029 QEMUIOVector *qiov, int flags)
1030{
1031 BlockDriver *drv = bs->drv;
3fb06697
KW
1032 int64_t sector_num;
1033 unsigned int nb_sectors;
78a07294
KW
1034 int ret;
1035
fa166538 1036 assert(!(flags & ~BDRV_REQ_MASK));
fe0480d6 1037 assert(!(flags & BDRV_REQ_NO_FALLBACK));
fa166538 1038
d470ad42
HR
1039 if (!drv) {
1040 return -ENOMEDIUM;
1041 }
1042
3fb06697 1043 if (drv->bdrv_co_pwritev) {
515c2f43
KW
1044 ret = drv->bdrv_co_pwritev(bs, offset, bytes, qiov,
1045 flags & bs->supported_write_flags);
1046 flags &= ~bs->supported_write_flags;
3fb06697
KW
1047 goto emulate_flags;
1048 }
1049
edfab6a0 1050 if (drv->bdrv_aio_pwritev) {
08844473
KW
1051 BlockAIOCB *acb;
1052 CoroutineIOCompletion co = {
1053 .coroutine = qemu_coroutine_self(),
1054 };
1055
edfab6a0
EB
1056 acb = drv->bdrv_aio_pwritev(bs, offset, bytes, qiov,
1057 flags & bs->supported_write_flags,
1058 bdrv_co_io_em_complete, &co);
1059 flags &= ~bs->supported_write_flags;
08844473 1060 if (acb == NULL) {
3fb06697 1061 ret = -EIO;
08844473
KW
1062 } else {
1063 qemu_coroutine_yield();
3fb06697 1064 ret = co.ret;
08844473 1065 }
edfab6a0
EB
1066 goto emulate_flags;
1067 }
1068
1069 sector_num = offset >> BDRV_SECTOR_BITS;
1070 nb_sectors = bytes >> BDRV_SECTOR_BITS;
1071
1072 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1073 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
41ae31e3 1074 assert(bytes <= BDRV_REQUEST_MAX_BYTES);
edfab6a0 1075
e18a58b4
EB
1076 assert(drv->bdrv_co_writev);
1077 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov,
1078 flags & bs->supported_write_flags);
1079 flags &= ~bs->supported_write_flags;
78a07294 1080
3fb06697 1081emulate_flags:
4df863f3 1082 if (ret == 0 && (flags & BDRV_REQ_FUA)) {
78a07294
KW
1083 ret = bdrv_co_flush(bs);
1084 }
1085
1086 return ret;
1087}
1088
29a298af
PB
1089static int coroutine_fn
1090bdrv_driver_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
1091 uint64_t bytes, QEMUIOVector *qiov)
1092{
1093 BlockDriver *drv = bs->drv;
1094
d470ad42
HR
1095 if (!drv) {
1096 return -ENOMEDIUM;
1097 }
1098
29a298af
PB
1099 if (!drv->bdrv_co_pwritev_compressed) {
1100 return -ENOTSUP;
1101 }
1102
29a298af
PB
1103 return drv->bdrv_co_pwritev_compressed(bs, offset, bytes, qiov);
1104}
1105
85c97ca7 1106static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child,
244483e6 1107 int64_t offset, unsigned int bytes, QEMUIOVector *qiov)
61007b31 1108{
85c97ca7
KW
1109 BlockDriverState *bs = child->bs;
1110
61007b31
SH
1111 /* Perform I/O through a temporary buffer so that users who scribble over
1112 * their read buffer while the operation is in progress do not end up
1113 * modifying the image file. This is critical for zero-copy guest I/O
1114 * where anything might happen inside guest memory.
1115 */
1116 void *bounce_buffer;
1117
1118 BlockDriver *drv = bs->drv;
cb2e2878 1119 QEMUIOVector local_qiov;
244483e6 1120 int64_t cluster_offset;
7cfd5275 1121 int64_t cluster_bytes;
61007b31
SH
1122 size_t skip_bytes;
1123 int ret;
cb2e2878
EB
1124 int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer,
1125 BDRV_REQUEST_MAX_BYTES);
1126 unsigned int progress = 0;
61007b31 1127
d470ad42
HR
1128 if (!drv) {
1129 return -ENOMEDIUM;
1130 }
1131
1bf03e66
KW
1132 /* FIXME We cannot require callers to have write permissions when all they
1133 * are doing is a read request. If we did things right, write permissions
1134 * would be obtained anyway, but internally by the copy-on-read code. As
765d9df9 1135 * long as it is implemented here rather than in a separate filter driver,
1bf03e66
KW
1136 * the copy-on-read code doesn't have its own BdrvChild, however, for which
1137 * it could request permissions. Therefore we have to bypass the permission
1138 * system for the moment. */
1139 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
afa4b293 1140
61007b31 1141 /* Cover entire cluster so no additional backing file I/O is required when
cb2e2878
EB
1142 * allocating cluster in the image file. Note that this value may exceed
1143 * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1144 * is one reason we loop rather than doing it all at once.
61007b31 1145 */
244483e6 1146 bdrv_round_to_clusters(bs, offset, bytes, &cluster_offset, &cluster_bytes);
cb2e2878 1147 skip_bytes = offset - cluster_offset;
61007b31 1148
244483e6
KW
1149 trace_bdrv_co_do_copy_on_readv(bs, offset, bytes,
1150 cluster_offset, cluster_bytes);
61007b31 1151
cb2e2878
EB
1152 bounce_buffer = qemu_try_blockalign(bs,
1153 MIN(MIN(max_transfer, cluster_bytes),
1154 MAX_BOUNCE_BUFFER));
61007b31
SH
1155 if (bounce_buffer == NULL) {
1156 ret = -ENOMEM;
1157 goto err;
1158 }
1159
cb2e2878
EB
1160 while (cluster_bytes) {
1161 int64_t pnum;
61007b31 1162
cb2e2878
EB
1163 ret = bdrv_is_allocated(bs, cluster_offset,
1164 MIN(cluster_bytes, max_transfer), &pnum);
1165 if (ret < 0) {
1166 /* Safe to treat errors in querying allocation as if
1167 * unallocated; we'll probably fail again soon on the
1168 * read, but at least that will set a decent errno.
1169 */
1170 pnum = MIN(cluster_bytes, max_transfer);
1171 }
61007b31 1172
b0ddcbbb
KW
1173 /* Stop at EOF if the image ends in the middle of the cluster */
1174 if (ret == 0 && pnum == 0) {
1175 assert(progress >= bytes);
1176 break;
1177 }
1178
cb2e2878 1179 assert(skip_bytes < pnum);
61007b31 1180
cb2e2878
EB
1181 if (ret <= 0) {
1182 /* Must copy-on-read; use the bounce buffer */
0d93ed08
VSO
1183 pnum = MIN(pnum, MAX_BOUNCE_BUFFER);
1184 qemu_iovec_init_buf(&local_qiov, bounce_buffer, pnum);
61007b31 1185
cb2e2878
EB
1186 ret = bdrv_driver_preadv(bs, cluster_offset, pnum,
1187 &local_qiov, 0);
1188 if (ret < 0) {
1189 goto err;
1190 }
1191
1192 bdrv_debug_event(bs, BLKDBG_COR_WRITE);
1193 if (drv->bdrv_co_pwrite_zeroes &&
1194 buffer_is_zero(bounce_buffer, pnum)) {
1195 /* FIXME: Should we (perhaps conditionally) be setting
1196 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1197 * that still correctly reads as zero? */
7adcf59f
HR
1198 ret = bdrv_co_do_pwrite_zeroes(bs, cluster_offset, pnum,
1199 BDRV_REQ_WRITE_UNCHANGED);
cb2e2878
EB
1200 } else {
1201 /* This does not change the data on the disk, it is not
1202 * necessary to flush even in cache=writethrough mode.
1203 */
1204 ret = bdrv_driver_pwritev(bs, cluster_offset, pnum,
7adcf59f
HR
1205 &local_qiov,
1206 BDRV_REQ_WRITE_UNCHANGED);
cb2e2878
EB
1207 }
1208
1209 if (ret < 0) {
1210 /* It might be okay to ignore write errors for guest
1211 * requests. If this is a deliberate copy-on-read
1212 * then we don't want to ignore the error. Simply
1213 * report it in all cases.
1214 */
1215 goto err;
1216 }
1217
1218 qemu_iovec_from_buf(qiov, progress, bounce_buffer + skip_bytes,
1219 pnum - skip_bytes);
1220 } else {
1221 /* Read directly into the destination */
1222 qemu_iovec_init(&local_qiov, qiov->niov);
1223 qemu_iovec_concat(&local_qiov, qiov, progress, pnum - skip_bytes);
1224 ret = bdrv_driver_preadv(bs, offset + progress, local_qiov.size,
1225 &local_qiov, 0);
1226 qemu_iovec_destroy(&local_qiov);
1227 if (ret < 0) {
1228 goto err;
1229 }
1230 }
1231
1232 cluster_offset += pnum;
1233 cluster_bytes -= pnum;
1234 progress += pnum - skip_bytes;
1235 skip_bytes = 0;
1236 }
1237 ret = 0;
61007b31
SH
1238
1239err:
1240 qemu_vfree(bounce_buffer);
1241 return ret;
1242}
1243
1244/*
1245 * Forwards an already correctly aligned request to the BlockDriver. This
1a62d0ac
EB
1246 * handles copy on read, zeroing after EOF, and fragmentation of large
1247 * reads; any other features must be implemented by the caller.
61007b31 1248 */
85c97ca7 1249static int coroutine_fn bdrv_aligned_preadv(BdrvChild *child,
61007b31
SH
1250 BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
1251 int64_t align, QEMUIOVector *qiov, int flags)
1252{
85c97ca7 1253 BlockDriverState *bs = child->bs;
c9d20029 1254 int64_t total_bytes, max_bytes;
1a62d0ac
EB
1255 int ret = 0;
1256 uint64_t bytes_remaining = bytes;
1257 int max_transfer;
61007b31 1258
49c07526
KW
1259 assert(is_power_of_2(align));
1260 assert((offset & (align - 1)) == 0);
1261 assert((bytes & (align - 1)) == 0);
61007b31 1262 assert(!qiov || bytes == qiov->size);
abb06c5a 1263 assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1a62d0ac
EB
1264 max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
1265 align);
a604fa2b
EB
1266
1267 /* TODO: We would need a per-BDS .supported_read_flags and
1268 * potential fallback support, if we ever implement any read flags
1269 * to pass through to drivers. For now, there aren't any
1270 * passthrough flags. */
1271 assert(!(flags & ~(BDRV_REQ_NO_SERIALISING | BDRV_REQ_COPY_ON_READ)));
61007b31
SH
1272
1273 /* Handle Copy on Read and associated serialisation */
1274 if (flags & BDRV_REQ_COPY_ON_READ) {
1275 /* If we touch the same cluster it counts as an overlap. This
1276 * guarantees that allocating writes will be serialized and not race
1277 * with each other for the same cluster. For example, in copy-on-read
1278 * it ensures that the CoR read and write operations are atomic and
1279 * guest writes cannot interleave between them. */
1280 mark_request_serialising(req, bdrv_get_cluster_size(bs));
1281 }
1282
09d2f948
VSO
1283 /* BDRV_REQ_SERIALISING is only for write operation */
1284 assert(!(flags & BDRV_REQ_SERIALISING));
1285
61408b25
FZ
1286 if (!(flags & BDRV_REQ_NO_SERIALISING)) {
1287 wait_serialising_requests(req);
1288 }
61007b31
SH
1289
1290 if (flags & BDRV_REQ_COPY_ON_READ) {
d6a644bb 1291 int64_t pnum;
61007b31 1292
88e63df2 1293 ret = bdrv_is_allocated(bs, offset, bytes, &pnum);
61007b31
SH
1294 if (ret < 0) {
1295 goto out;
1296 }
1297
88e63df2 1298 if (!ret || pnum != bytes) {
85c97ca7 1299 ret = bdrv_co_do_copy_on_readv(child, offset, bytes, qiov);
61007b31
SH
1300 goto out;
1301 }
1302 }
1303
1a62d0ac 1304 /* Forward the request to the BlockDriver, possibly fragmenting it */
c9d20029
KW
1305 total_bytes = bdrv_getlength(bs);
1306 if (total_bytes < 0) {
1307 ret = total_bytes;
1308 goto out;
1309 }
61007b31 1310
c9d20029 1311 max_bytes = ROUND_UP(MAX(0, total_bytes - offset), align);
1a62d0ac 1312 if (bytes <= max_bytes && bytes <= max_transfer) {
c9d20029 1313 ret = bdrv_driver_preadv(bs, offset, bytes, qiov, 0);
1a62d0ac
EB
1314 goto out;
1315 }
61007b31 1316
1a62d0ac
EB
1317 while (bytes_remaining) {
1318 int num;
61007b31 1319
1a62d0ac
EB
1320 if (max_bytes) {
1321 QEMUIOVector local_qiov;
61007b31 1322
1a62d0ac
EB
1323 num = MIN(bytes_remaining, MIN(max_bytes, max_transfer));
1324 assert(num);
1325 qemu_iovec_init(&local_qiov, qiov->niov);
1326 qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num);
61007b31 1327
1a62d0ac
EB
1328 ret = bdrv_driver_preadv(bs, offset + bytes - bytes_remaining,
1329 num, &local_qiov, 0);
1330 max_bytes -= num;
1331 qemu_iovec_destroy(&local_qiov);
1332 } else {
1333 num = bytes_remaining;
1334 ret = qemu_iovec_memset(qiov, bytes - bytes_remaining, 0,
1335 bytes_remaining);
1336 }
1337 if (ret < 0) {
1338 goto out;
1339 }
1340 bytes_remaining -= num;
61007b31
SH
1341 }
1342
1343out:
1a62d0ac 1344 return ret < 0 ? ret : 0;
61007b31
SH
1345}
1346
61007b31
SH
1347/*
1348 * Handle a read request in coroutine context
1349 */
a03ef88f 1350int coroutine_fn bdrv_co_preadv(BdrvChild *child,
61007b31
SH
1351 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
1352 BdrvRequestFlags flags)
1353{
a03ef88f 1354 BlockDriverState *bs = child->bs;
61007b31
SH
1355 BlockDriver *drv = bs->drv;
1356 BdrvTrackedRequest req;
1357
a5b8dd2c 1358 uint64_t align = bs->bl.request_alignment;
61007b31
SH
1359 uint8_t *head_buf = NULL;
1360 uint8_t *tail_buf = NULL;
1361 QEMUIOVector local_qiov;
1362 bool use_local_qiov = false;
1363 int ret;
1364
f42cf447
DB
1365 trace_bdrv_co_preadv(child->bs, offset, bytes, flags);
1366
61007b31
SH
1367 if (!drv) {
1368 return -ENOMEDIUM;
1369 }
1370
1371 ret = bdrv_check_byte_request(bs, offset, bytes);
1372 if (ret < 0) {
1373 return ret;
1374 }
1375
99723548
PB
1376 bdrv_inc_in_flight(bs);
1377
9568b511 1378 /* Don't do copy-on-read if we read data before write operation */
d3faa13e 1379 if (atomic_read(&bs->copy_on_read) && !(flags & BDRV_REQ_NO_SERIALISING)) {
61007b31
SH
1380 flags |= BDRV_REQ_COPY_ON_READ;
1381 }
1382
61007b31
SH
1383 /* Align read if necessary by padding qiov */
1384 if (offset & (align - 1)) {
1385 head_buf = qemu_blockalign(bs, align);
1386 qemu_iovec_init(&local_qiov, qiov->niov + 2);
1387 qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
1388 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1389 use_local_qiov = true;
1390
1391 bytes += offset & (align - 1);
1392 offset = offset & ~(align - 1);
1393 }
1394
1395 if ((offset + bytes) & (align - 1)) {
1396 if (!use_local_qiov) {
1397 qemu_iovec_init(&local_qiov, qiov->niov + 1);
1398 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1399 use_local_qiov = true;
1400 }
1401 tail_buf = qemu_blockalign(bs, align);
1402 qemu_iovec_add(&local_qiov, tail_buf,
1403 align - ((offset + bytes) & (align - 1)));
1404
1405 bytes = ROUND_UP(bytes, align);
1406 }
1407
ebde595c 1408 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_READ);
85c97ca7 1409 ret = bdrv_aligned_preadv(child, &req, offset, bytes, align,
61007b31
SH
1410 use_local_qiov ? &local_qiov : qiov,
1411 flags);
1412 tracked_request_end(&req);
99723548 1413 bdrv_dec_in_flight(bs);
61007b31
SH
1414
1415 if (use_local_qiov) {
1416 qemu_iovec_destroy(&local_qiov);
1417 qemu_vfree(head_buf);
1418 qemu_vfree(tail_buf);
1419 }
1420
1421 return ret;
1422}
1423
d05aa8bb 1424static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
f5a5ca79 1425 int64_t offset, int bytes, BdrvRequestFlags flags)
61007b31
SH
1426{
1427 BlockDriver *drv = bs->drv;
1428 QEMUIOVector qiov;
0d93ed08 1429 void *buf = NULL;
61007b31 1430 int ret = 0;
465fe887 1431 bool need_flush = false;
443668ca
DL
1432 int head = 0;
1433 int tail = 0;
61007b31 1434
cf081fca 1435 int max_write_zeroes = MIN_NON_ZERO(bs->bl.max_pwrite_zeroes, INT_MAX);
a5b8dd2c
EB
1436 int alignment = MAX(bs->bl.pwrite_zeroes_alignment,
1437 bs->bl.request_alignment);
cb2e2878 1438 int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer, MAX_BOUNCE_BUFFER);
d05aa8bb 1439
d470ad42
HR
1440 if (!drv) {
1441 return -ENOMEDIUM;
1442 }
1443
fe0480d6
KW
1444 if ((flags & ~bs->supported_zero_flags) & BDRV_REQ_NO_FALLBACK) {
1445 return -ENOTSUP;
1446 }
1447
b8d0a980
EB
1448 assert(alignment % bs->bl.request_alignment == 0);
1449 head = offset % alignment;
f5a5ca79 1450 tail = (offset + bytes) % alignment;
b8d0a980
EB
1451 max_write_zeroes = QEMU_ALIGN_DOWN(max_write_zeroes, alignment);
1452 assert(max_write_zeroes >= bs->bl.request_alignment);
61007b31 1453
f5a5ca79
MP
1454 while (bytes > 0 && !ret) {
1455 int num = bytes;
61007b31
SH
1456
1457 /* Align request. Block drivers can expect the "bulk" of the request
443668ca
DL
1458 * to be aligned, and that unaligned requests do not cross cluster
1459 * boundaries.
61007b31 1460 */
443668ca 1461 if (head) {
b2f95fee
EB
1462 /* Make a small request up to the first aligned sector. For
1463 * convenience, limit this request to max_transfer even if
1464 * we don't need to fall back to writes. */
f5a5ca79 1465 num = MIN(MIN(bytes, max_transfer), alignment - head);
b2f95fee
EB
1466 head = (head + num) % alignment;
1467 assert(num < max_write_zeroes);
d05aa8bb 1468 } else if (tail && num > alignment) {
443668ca
DL
1469 /* Shorten the request to the last aligned sector. */
1470 num -= tail;
61007b31
SH
1471 }
1472
1473 /* limit request size */
1474 if (num > max_write_zeroes) {
1475 num = max_write_zeroes;
1476 }
1477
1478 ret = -ENOTSUP;
1479 /* First try the efficient write zeroes operation */
d05aa8bb
EB
1480 if (drv->bdrv_co_pwrite_zeroes) {
1481 ret = drv->bdrv_co_pwrite_zeroes(bs, offset, num,
1482 flags & bs->supported_zero_flags);
1483 if (ret != -ENOTSUP && (flags & BDRV_REQ_FUA) &&
1484 !(bs->supported_zero_flags & BDRV_REQ_FUA)) {
1485 need_flush = true;
1486 }
465fe887
EB
1487 } else {
1488 assert(!bs->supported_zero_flags);
61007b31
SH
1489 }
1490
118f9944 1491 if (ret < 0 && !(flags & BDRV_REQ_NO_FALLBACK)) {
61007b31 1492 /* Fall back to bounce buffer if write zeroes is unsupported */
465fe887
EB
1493 BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE;
1494
1495 if ((flags & BDRV_REQ_FUA) &&
1496 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
1497 /* No need for bdrv_driver_pwrite() to do a fallback
1498 * flush on each chunk; use just one at the end */
1499 write_flags &= ~BDRV_REQ_FUA;
1500 need_flush = true;
1501 }
5def6b80 1502 num = MIN(num, max_transfer);
0d93ed08
VSO
1503 if (buf == NULL) {
1504 buf = qemu_try_blockalign0(bs, num);
1505 if (buf == NULL) {
61007b31
SH
1506 ret = -ENOMEM;
1507 goto fail;
1508 }
61007b31 1509 }
0d93ed08 1510 qemu_iovec_init_buf(&qiov, buf, num);
61007b31 1511
d05aa8bb 1512 ret = bdrv_driver_pwritev(bs, offset, num, &qiov, write_flags);
61007b31
SH
1513
1514 /* Keep bounce buffer around if it is big enough for all
1515 * all future requests.
1516 */
5def6b80 1517 if (num < max_transfer) {
0d93ed08
VSO
1518 qemu_vfree(buf);
1519 buf = NULL;
61007b31
SH
1520 }
1521 }
1522
d05aa8bb 1523 offset += num;
f5a5ca79 1524 bytes -= num;
61007b31
SH
1525 }
1526
1527fail:
465fe887
EB
1528 if (ret == 0 && need_flush) {
1529 ret = bdrv_co_flush(bs);
1530 }
0d93ed08 1531 qemu_vfree(buf);
61007b31
SH
1532 return ret;
1533}
1534
85fe2479
FZ
1535static inline int coroutine_fn
1536bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, uint64_t bytes,
1537 BdrvTrackedRequest *req, int flags)
1538{
1539 BlockDriverState *bs = child->bs;
1540 bool waited;
1541 int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
1542
1543 if (bs->read_only) {
1544 return -EPERM;
1545 }
1546
1547 /* BDRV_REQ_NO_SERIALISING is only for read operation */
1548 assert(!(flags & BDRV_REQ_NO_SERIALISING));
1549 assert(!(bs->open_flags & BDRV_O_INACTIVE));
1550 assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1551 assert(!(flags & ~BDRV_REQ_MASK));
1552
1553 if (flags & BDRV_REQ_SERIALISING) {
1554 mark_request_serialising(req, bdrv_get_cluster_size(bs));
1555 }
1556
1557 waited = wait_serialising_requests(req);
1558
1559 assert(!waited || !req->serialising ||
1560 is_request_serialising_and_aligned(req));
1561 assert(req->overlap_offset <= offset);
1562 assert(offset + bytes <= req->overlap_offset + req->overlap_bytes);
cd47d792 1563 assert(end_sector <= bs->total_sectors || child->perm & BLK_PERM_RESIZE);
85fe2479 1564
cd47d792
FZ
1565 switch (req->type) {
1566 case BDRV_TRACKED_WRITE:
1567 case BDRV_TRACKED_DISCARD:
1568 if (flags & BDRV_REQ_WRITE_UNCHANGED) {
1569 assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1570 } else {
1571 assert(child->perm & BLK_PERM_WRITE);
1572 }
1573 return notifier_with_return_list_notify(&bs->before_write_notifiers,
1574 req);
1575 case BDRV_TRACKED_TRUNCATE:
1576 assert(child->perm & BLK_PERM_RESIZE);
1577 return 0;
1578 default:
1579 abort();
85fe2479 1580 }
85fe2479
FZ
1581}
1582
1583static inline void coroutine_fn
1584bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, uint64_t bytes,
1585 BdrvTrackedRequest *req, int ret)
1586{
1587 int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
1588 BlockDriverState *bs = child->bs;
1589
1590 atomic_inc(&bs->write_gen);
85fe2479 1591
00695c27
FZ
1592 /*
1593 * Discard cannot extend the image, but in error handling cases, such as
1594 * when reverting a qcow2 cluster allocation, the discarded range can pass
1595 * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
1596 * here. Instead, just skip it, since semantically a discard request
1597 * beyond EOF cannot expand the image anyway.
1598 */
7f8f03ef 1599 if (ret == 0 &&
cd47d792
FZ
1600 (req->type == BDRV_TRACKED_TRUNCATE ||
1601 end_sector > bs->total_sectors) &&
1602 req->type != BDRV_TRACKED_DISCARD) {
7f8f03ef
FZ
1603 bs->total_sectors = end_sector;
1604 bdrv_parent_cb_resize(bs);
1605 bdrv_dirty_bitmap_truncate(bs, end_sector << BDRV_SECTOR_BITS);
85fe2479 1606 }
00695c27
FZ
1607 if (req->bytes) {
1608 switch (req->type) {
1609 case BDRV_TRACKED_WRITE:
1610 stat64_max(&bs->wr_highest_offset, offset + bytes);
1611 /* fall through, to set dirty bits */
1612 case BDRV_TRACKED_DISCARD:
1613 bdrv_set_dirty(bs, offset, bytes);
1614 break;
1615 default:
1616 break;
1617 }
1618 }
85fe2479
FZ
1619}
1620
61007b31 1621/*
04ed95f4
EB
1622 * Forwards an already correctly aligned write request to the BlockDriver,
1623 * after possibly fragmenting it.
61007b31 1624 */
85c97ca7 1625static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child,
61007b31 1626 BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
cff86b38 1627 int64_t align, QEMUIOVector *qiov, int flags)
61007b31 1628{
85c97ca7 1629 BlockDriverState *bs = child->bs;
61007b31 1630 BlockDriver *drv = bs->drv;
61007b31
SH
1631 int ret;
1632
04ed95f4
EB
1633 uint64_t bytes_remaining = bytes;
1634 int max_transfer;
61007b31 1635
d470ad42
HR
1636 if (!drv) {
1637 return -ENOMEDIUM;
1638 }
1639
d6883bc9
VSO
1640 if (bdrv_has_readonly_bitmaps(bs)) {
1641 return -EPERM;
1642 }
1643
cff86b38
EB
1644 assert(is_power_of_2(align));
1645 assert((offset & (align - 1)) == 0);
1646 assert((bytes & (align - 1)) == 0);
61007b31 1647 assert(!qiov || bytes == qiov->size);
04ed95f4
EB
1648 max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
1649 align);
61007b31 1650
85fe2479 1651 ret = bdrv_co_write_req_prepare(child, offset, bytes, req, flags);
61007b31
SH
1652
1653 if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF &&
c1499a5e 1654 !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes &&
61007b31
SH
1655 qemu_iovec_is_zero(qiov)) {
1656 flags |= BDRV_REQ_ZERO_WRITE;
1657 if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) {
1658 flags |= BDRV_REQ_MAY_UNMAP;
1659 }
1660 }
1661
1662 if (ret < 0) {
1663 /* Do nothing, write notifier decided to fail this request */
1664 } else if (flags & BDRV_REQ_ZERO_WRITE) {
9a4f4c31 1665 bdrv_debug_event(bs, BLKDBG_PWRITEV_ZERO);
9896c876 1666 ret = bdrv_co_do_pwrite_zeroes(bs, offset, bytes, flags);
3ea1a091
PB
1667 } else if (flags & BDRV_REQ_WRITE_COMPRESSED) {
1668 ret = bdrv_driver_pwritev_compressed(bs, offset, bytes, qiov);
04ed95f4 1669 } else if (bytes <= max_transfer) {
9a4f4c31 1670 bdrv_debug_event(bs, BLKDBG_PWRITEV);
78a07294 1671 ret = bdrv_driver_pwritev(bs, offset, bytes, qiov, flags);
04ed95f4
EB
1672 } else {
1673 bdrv_debug_event(bs, BLKDBG_PWRITEV);
1674 while (bytes_remaining) {
1675 int num = MIN(bytes_remaining, max_transfer);
1676 QEMUIOVector local_qiov;
1677 int local_flags = flags;
1678
1679 assert(num);
1680 if (num < bytes_remaining && (flags & BDRV_REQ_FUA) &&
1681 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
1682 /* If FUA is going to be emulated by flush, we only
1683 * need to flush on the last iteration */
1684 local_flags &= ~BDRV_REQ_FUA;
1685 }
1686 qemu_iovec_init(&local_qiov, qiov->niov);
1687 qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num);
1688
1689 ret = bdrv_driver_pwritev(bs, offset + bytes - bytes_remaining,
1690 num, &local_qiov, local_flags);
1691 qemu_iovec_destroy(&local_qiov);
1692 if (ret < 0) {
1693 break;
1694 }
1695 bytes_remaining -= num;
1696 }
61007b31 1697 }
9a4f4c31 1698 bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
61007b31 1699
61007b31 1700 if (ret >= 0) {
04ed95f4 1701 ret = 0;
61007b31 1702 }
85fe2479 1703 bdrv_co_write_req_finish(child, offset, bytes, req, ret);
61007b31
SH
1704
1705 return ret;
1706}
1707
85c97ca7 1708static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvChild *child,
9eeb6dd1
FZ
1709 int64_t offset,
1710 unsigned int bytes,
1711 BdrvRequestFlags flags,
1712 BdrvTrackedRequest *req)
1713{
85c97ca7 1714 BlockDriverState *bs = child->bs;
9eeb6dd1
FZ
1715 uint8_t *buf = NULL;
1716 QEMUIOVector local_qiov;
a5b8dd2c 1717 uint64_t align = bs->bl.request_alignment;
9eeb6dd1
FZ
1718 unsigned int head_padding_bytes, tail_padding_bytes;
1719 int ret = 0;
1720
1721 head_padding_bytes = offset & (align - 1);
f13ce1be 1722 tail_padding_bytes = (align - (offset + bytes)) & (align - 1);
9eeb6dd1
FZ
1723
1724
1725 assert(flags & BDRV_REQ_ZERO_WRITE);
1726 if (head_padding_bytes || tail_padding_bytes) {
1727 buf = qemu_blockalign(bs, align);
0d93ed08 1728 qemu_iovec_init_buf(&local_qiov, buf, align);
9eeb6dd1
FZ
1729 }
1730 if (head_padding_bytes) {
1731 uint64_t zero_bytes = MIN(bytes, align - head_padding_bytes);
1732
1733 /* RMW the unaligned part before head. */
1734 mark_request_serialising(req, align);
1735 wait_serialising_requests(req);
9a4f4c31 1736 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
85c97ca7 1737 ret = bdrv_aligned_preadv(child, req, offset & ~(align - 1), align,
9eeb6dd1
FZ
1738 align, &local_qiov, 0);
1739 if (ret < 0) {
1740 goto fail;
1741 }
9a4f4c31 1742 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
9eeb6dd1
FZ
1743
1744 memset(buf + head_padding_bytes, 0, zero_bytes);
85c97ca7 1745 ret = bdrv_aligned_pwritev(child, req, offset & ~(align - 1), align,
cff86b38 1746 align, &local_qiov,
9eeb6dd1
FZ
1747 flags & ~BDRV_REQ_ZERO_WRITE);
1748 if (ret < 0) {
1749 goto fail;
1750 }
1751 offset += zero_bytes;
1752 bytes -= zero_bytes;
1753 }
1754
1755 assert(!bytes || (offset & (align - 1)) == 0);
1756 if (bytes >= align) {
1757 /* Write the aligned part in the middle. */
1758 uint64_t aligned_bytes = bytes & ~(align - 1);
85c97ca7 1759 ret = bdrv_aligned_pwritev(child, req, offset, aligned_bytes, align,
9eeb6dd1
FZ
1760 NULL, flags);
1761 if (ret < 0) {
1762 goto fail;
1763 }
1764 bytes -= aligned_bytes;
1765 offset += aligned_bytes;
1766 }
1767
1768 assert(!bytes || (offset & (align - 1)) == 0);
1769 if (bytes) {
1770 assert(align == tail_padding_bytes + bytes);
1771 /* RMW the unaligned part after tail. */
1772 mark_request_serialising(req, align);
1773 wait_serialising_requests(req);
9a4f4c31 1774 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
85c97ca7 1775 ret = bdrv_aligned_preadv(child, req, offset, align,
9eeb6dd1
FZ
1776 align, &local_qiov, 0);
1777 if (ret < 0) {
1778 goto fail;
1779 }
9a4f4c31 1780 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
9eeb6dd1
FZ
1781
1782 memset(buf, 0, bytes);
85c97ca7 1783 ret = bdrv_aligned_pwritev(child, req, offset, align, align,
9eeb6dd1
FZ
1784 &local_qiov, flags & ~BDRV_REQ_ZERO_WRITE);
1785 }
1786fail:
1787 qemu_vfree(buf);
1788 return ret;
1789
1790}
1791
61007b31
SH
1792/*
1793 * Handle a write request in coroutine context
1794 */
a03ef88f 1795int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
61007b31
SH
1796 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
1797 BdrvRequestFlags flags)
1798{
a03ef88f 1799 BlockDriverState *bs = child->bs;
61007b31 1800 BdrvTrackedRequest req;
a5b8dd2c 1801 uint64_t align = bs->bl.request_alignment;
61007b31
SH
1802 uint8_t *head_buf = NULL;
1803 uint8_t *tail_buf = NULL;
1804 QEMUIOVector local_qiov;
1805 bool use_local_qiov = false;
1806 int ret;
1807
f42cf447
DB
1808 trace_bdrv_co_pwritev(child->bs, offset, bytes, flags);
1809
61007b31
SH
1810 if (!bs->drv) {
1811 return -ENOMEDIUM;
1812 }
61007b31
SH
1813
1814 ret = bdrv_check_byte_request(bs, offset, bytes);
1815 if (ret < 0) {
1816 return ret;
1817 }
1818
99723548 1819 bdrv_inc_in_flight(bs);
61007b31
SH
1820 /*
1821 * Align write if necessary by performing a read-modify-write cycle.
1822 * Pad qiov with the read parts and be sure to have a tracked request not
1823 * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
1824 */
ebde595c 1825 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
61007b31 1826
18a59f03 1827 if (flags & BDRV_REQ_ZERO_WRITE) {
85c97ca7 1828 ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);
9eeb6dd1
FZ
1829 goto out;
1830 }
1831
61007b31
SH
1832 if (offset & (align - 1)) {
1833 QEMUIOVector head_qiov;
61007b31
SH
1834
1835 mark_request_serialising(&req, align);
1836 wait_serialising_requests(&req);
1837
1838 head_buf = qemu_blockalign(bs, align);
0d93ed08 1839 qemu_iovec_init_buf(&head_qiov, head_buf, align);
61007b31 1840
9a4f4c31 1841 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
85c97ca7 1842 ret = bdrv_aligned_preadv(child, &req, offset & ~(align - 1), align,
61007b31
SH
1843 align, &head_qiov, 0);
1844 if (ret < 0) {
1845 goto fail;
1846 }
9a4f4c31 1847 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
61007b31
SH
1848
1849 qemu_iovec_init(&local_qiov, qiov->niov + 2);
1850 qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
1851 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1852 use_local_qiov = true;
1853
1854 bytes += offset & (align - 1);
1855 offset = offset & ~(align - 1);
117bc3fa
PL
1856
1857 /* We have read the tail already if the request is smaller
1858 * than one aligned block.
1859 */
1860 if (bytes < align) {
1861 qemu_iovec_add(&local_qiov, head_buf + bytes, align - bytes);
1862 bytes = align;
1863 }
61007b31
SH
1864 }
1865
1866 if ((offset + bytes) & (align - 1)) {
1867 QEMUIOVector tail_qiov;
61007b31
SH
1868 size_t tail_bytes;
1869 bool waited;
1870
1871 mark_request_serialising(&req, align);
1872 waited = wait_serialising_requests(&req);
1873 assert(!waited || !use_local_qiov);
1874
1875 tail_buf = qemu_blockalign(bs, align);
0d93ed08 1876 qemu_iovec_init_buf(&tail_qiov, tail_buf, align);
61007b31 1877
9a4f4c31 1878 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
85c97ca7
KW
1879 ret = bdrv_aligned_preadv(child, &req, (offset + bytes) & ~(align - 1),
1880 align, align, &tail_qiov, 0);
61007b31
SH
1881 if (ret < 0) {
1882 goto fail;
1883 }
9a4f4c31 1884 bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
61007b31
SH
1885
1886 if (!use_local_qiov) {
1887 qemu_iovec_init(&local_qiov, qiov->niov + 1);
1888 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
1889 use_local_qiov = true;
1890 }
1891
1892 tail_bytes = (offset + bytes) & (align - 1);
1893 qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, align - tail_bytes);
1894
1895 bytes = ROUND_UP(bytes, align);
1896 }
1897
85c97ca7 1898 ret = bdrv_aligned_pwritev(child, &req, offset, bytes, align,
3ea1a091
PB
1899 use_local_qiov ? &local_qiov : qiov,
1900 flags);
61007b31
SH
1901
1902fail:
61007b31
SH
1903
1904 if (use_local_qiov) {
1905 qemu_iovec_destroy(&local_qiov);
1906 }
1907 qemu_vfree(head_buf);
1908 qemu_vfree(tail_buf);
9eeb6dd1
FZ
1909out:
1910 tracked_request_end(&req);
99723548 1911 bdrv_dec_in_flight(bs);
61007b31
SH
1912 return ret;
1913}
1914
a03ef88f 1915int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset,
f5a5ca79 1916 int bytes, BdrvRequestFlags flags)
61007b31 1917{
f5a5ca79 1918 trace_bdrv_co_pwrite_zeroes(child->bs, offset, bytes, flags);
61007b31 1919
a03ef88f 1920 if (!(child->bs->open_flags & BDRV_O_UNMAP)) {
61007b31
SH
1921 flags &= ~BDRV_REQ_MAY_UNMAP;
1922 }
61007b31 1923
f5a5ca79 1924 return bdrv_co_pwritev(child, offset, bytes, NULL,
74021bc4 1925 BDRV_REQ_ZERO_WRITE | flags);
61007b31
SH
1926}
1927
4085f5c7
JS
1928/*
1929 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
1930 */
1931int bdrv_flush_all(void)
1932{
1933 BdrvNextIterator it;
1934 BlockDriverState *bs = NULL;
1935 int result = 0;
1936
1937 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1938 AioContext *aio_context = bdrv_get_aio_context(bs);
1939 int ret;
1940
1941 aio_context_acquire(aio_context);
1942 ret = bdrv_flush(bs);
1943 if (ret < 0 && !result) {
1944 result = ret;
1945 }
1946 aio_context_release(aio_context);
1947 }
1948
1949 return result;
1950}
1951
1952
4bcd936e 1953typedef struct BdrvCoBlockStatusData {
61007b31
SH
1954 BlockDriverState *bs;
1955 BlockDriverState *base;
c9ce8c4d 1956 bool want_zero;
4bcd936e
EB
1957 int64_t offset;
1958 int64_t bytes;
1959 int64_t *pnum;
1960 int64_t *map;
c9ce8c4d 1961 BlockDriverState **file;
4bcd936e 1962 int ret;
61007b31 1963 bool done;
4bcd936e 1964} BdrvCoBlockStatusData;
61007b31 1965
3e4d0e72
EB
1966int coroutine_fn bdrv_co_block_status_from_file(BlockDriverState *bs,
1967 bool want_zero,
1968 int64_t offset,
1969 int64_t bytes,
1970 int64_t *pnum,
1971 int64_t *map,
1972 BlockDriverState **file)
f7cc69b3
MP
1973{
1974 assert(bs->file && bs->file->bs);
3e4d0e72
EB
1975 *pnum = bytes;
1976 *map = offset;
f7cc69b3 1977 *file = bs->file->bs;
3e4d0e72 1978 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
f7cc69b3
MP
1979}
1980
3e4d0e72
EB
1981int coroutine_fn bdrv_co_block_status_from_backing(BlockDriverState *bs,
1982 bool want_zero,
1983 int64_t offset,
1984 int64_t bytes,
1985 int64_t *pnum,
1986 int64_t *map,
1987 BlockDriverState **file)
f7cc69b3
MP
1988{
1989 assert(bs->backing && bs->backing->bs);
3e4d0e72
EB
1990 *pnum = bytes;
1991 *map = offset;
f7cc69b3 1992 *file = bs->backing->bs;
3e4d0e72 1993 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
f7cc69b3
MP
1994}
1995
61007b31
SH
1996/*
1997 * Returns the allocation status of the specified sectors.
1998 * Drivers not implementing the functionality are assumed to not support
1999 * backing files, hence all their sectors are reported as allocated.
2000 *
86a3d5c6
EB
2001 * If 'want_zero' is true, the caller is querying for mapping
2002 * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and
2003 * _ZERO where possible; otherwise, the result favors larger 'pnum',
2004 * with a focus on accurate BDRV_BLOCK_ALLOCATED.
c9ce8c4d 2005 *
2e8bc787 2006 * If 'offset' is beyond the end of the disk image the return value is
fb0d8654 2007 * BDRV_BLOCK_EOF and 'pnum' is set to 0.
61007b31 2008 *
2e8bc787 2009 * 'bytes' is the max value 'pnum' should be set to. If bytes goes
fb0d8654
EB
2010 * beyond the end of the disk image it will be clamped; if 'pnum' is set to
2011 * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
67a0fd2a 2012 *
2e8bc787
EB
2013 * 'pnum' is set to the number of bytes (including and immediately
2014 * following the specified offset) that are easily known to be in the
2015 * same allocated/unallocated state. Note that a second call starting
2016 * at the original offset plus returned pnum may have the same status.
2017 * The returned value is non-zero on success except at end-of-file.
2018 *
2019 * Returns negative errno on failure. Otherwise, if the
2020 * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are
2021 * set to the host mapping and BDS corresponding to the guest offset.
61007b31 2022 */
2e8bc787
EB
2023static int coroutine_fn bdrv_co_block_status(BlockDriverState *bs,
2024 bool want_zero,
2025 int64_t offset, int64_t bytes,
2026 int64_t *pnum, int64_t *map,
2027 BlockDriverState **file)
2028{
2029 int64_t total_size;
2030 int64_t n; /* bytes */
efa6e2ed 2031 int ret;
2e8bc787 2032 int64_t local_map = 0;
298a1665 2033 BlockDriverState *local_file = NULL;
efa6e2ed
EB
2034 int64_t aligned_offset, aligned_bytes;
2035 uint32_t align;
61007b31 2036
298a1665
EB
2037 assert(pnum);
2038 *pnum = 0;
2e8bc787
EB
2039 total_size = bdrv_getlength(bs);
2040 if (total_size < 0) {
2041 ret = total_size;
298a1665 2042 goto early_out;
61007b31
SH
2043 }
2044
2e8bc787 2045 if (offset >= total_size) {
298a1665
EB
2046 ret = BDRV_BLOCK_EOF;
2047 goto early_out;
61007b31 2048 }
2e8bc787 2049 if (!bytes) {
298a1665
EB
2050 ret = 0;
2051 goto early_out;
9cdcfd9f 2052 }
61007b31 2053
2e8bc787
EB
2054 n = total_size - offset;
2055 if (n < bytes) {
2056 bytes = n;
61007b31
SH
2057 }
2058
d470ad42
HR
2059 /* Must be non-NULL or bdrv_getlength() would have failed */
2060 assert(bs->drv);
636cb512 2061 if (!bs->drv->bdrv_co_block_status) {
2e8bc787 2062 *pnum = bytes;
61007b31 2063 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED;
2e8bc787 2064 if (offset + bytes == total_size) {
fb0d8654
EB
2065 ret |= BDRV_BLOCK_EOF;
2066 }
61007b31 2067 if (bs->drv->protocol_name) {
2e8bc787
EB
2068 ret |= BDRV_BLOCK_OFFSET_VALID;
2069 local_map = offset;
298a1665 2070 local_file = bs;
61007b31 2071 }
298a1665 2072 goto early_out;
61007b31
SH
2073 }
2074
99723548 2075 bdrv_inc_in_flight(bs);
efa6e2ed
EB
2076
2077 /* Round out to request_alignment boundaries */
86a3d5c6 2078 align = bs->bl.request_alignment;
efa6e2ed
EB
2079 aligned_offset = QEMU_ALIGN_DOWN(offset, align);
2080 aligned_bytes = ROUND_UP(offset + bytes, align) - aligned_offset;
2081
636cb512
EB
2082 ret = bs->drv->bdrv_co_block_status(bs, want_zero, aligned_offset,
2083 aligned_bytes, pnum, &local_map,
2084 &local_file);
2085 if (ret < 0) {
2086 *pnum = 0;
2087 goto out;
efa6e2ed
EB
2088 }
2089
2e8bc787 2090 /*
636cb512 2091 * The driver's result must be a non-zero multiple of request_alignment.
efa6e2ed 2092 * Clamp pnum and adjust map to original request.
2e8bc787 2093 */
636cb512
EB
2094 assert(*pnum && QEMU_IS_ALIGNED(*pnum, align) &&
2095 align > offset - aligned_offset);
69f47505
VSO
2096 if (ret & BDRV_BLOCK_RECURSE) {
2097 assert(ret & BDRV_BLOCK_DATA);
2098 assert(ret & BDRV_BLOCK_OFFSET_VALID);
2099 assert(!(ret & BDRV_BLOCK_ZERO));
2100 }
2101
efa6e2ed
EB
2102 *pnum -= offset - aligned_offset;
2103 if (*pnum > bytes) {
2104 *pnum = bytes;
61007b31 2105 }
2e8bc787 2106 if (ret & BDRV_BLOCK_OFFSET_VALID) {
efa6e2ed 2107 local_map += offset - aligned_offset;
2e8bc787 2108 }
61007b31
SH
2109
2110 if (ret & BDRV_BLOCK_RAW) {
298a1665 2111 assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file);
2e8bc787
EB
2112 ret = bdrv_co_block_status(local_file, want_zero, local_map,
2113 *pnum, pnum, &local_map, &local_file);
99723548 2114 goto out;
61007b31
SH
2115 }
2116
2117 if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
2118 ret |= BDRV_BLOCK_ALLOCATED;
c9ce8c4d 2119 } else if (want_zero) {
61007b31
SH
2120 if (bdrv_unallocated_blocks_are_zero(bs)) {
2121 ret |= BDRV_BLOCK_ZERO;
760e0063
KW
2122 } else if (bs->backing) {
2123 BlockDriverState *bs2 = bs->backing->bs;
2e8bc787 2124 int64_t size2 = bdrv_getlength(bs2);
c9ce8c4d 2125
2e8bc787 2126 if (size2 >= 0 && offset >= size2) {
61007b31
SH
2127 ret |= BDRV_BLOCK_ZERO;
2128 }
2129 }
2130 }
2131
69f47505
VSO
2132 if (want_zero && ret & BDRV_BLOCK_RECURSE &&
2133 local_file && local_file != bs &&
61007b31
SH
2134 (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
2135 (ret & BDRV_BLOCK_OFFSET_VALID)) {
2e8bc787
EB
2136 int64_t file_pnum;
2137 int ret2;
61007b31 2138
2e8bc787
EB
2139 ret2 = bdrv_co_block_status(local_file, want_zero, local_map,
2140 *pnum, &file_pnum, NULL, NULL);
61007b31
SH
2141 if (ret2 >= 0) {
2142 /* Ignore errors. This is just providing extra information, it
2143 * is useful but not necessary.
2144 */
c61e684e
EB
2145 if (ret2 & BDRV_BLOCK_EOF &&
2146 (!file_pnum || ret2 & BDRV_BLOCK_ZERO)) {
2147 /*
2148 * It is valid for the format block driver to read
2149 * beyond the end of the underlying file's current
2150 * size; such areas read as zero.
2151 */
61007b31
SH
2152 ret |= BDRV_BLOCK_ZERO;
2153 } else {
2154 /* Limit request to the range reported by the protocol driver */
2155 *pnum = file_pnum;
2156 ret |= (ret2 & BDRV_BLOCK_ZERO);
2157 }
2158 }
2159 }
2160
99723548
PB
2161out:
2162 bdrv_dec_in_flight(bs);
2e8bc787 2163 if (ret >= 0 && offset + *pnum == total_size) {
fb0d8654
EB
2164 ret |= BDRV_BLOCK_EOF;
2165 }
298a1665
EB
2166early_out:
2167 if (file) {
2168 *file = local_file;
2169 }
2e8bc787
EB
2170 if (map) {
2171 *map = local_map;
2172 }
61007b31
SH
2173 return ret;
2174}
2175
5b648c67
EB
2176static int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs,
2177 BlockDriverState *base,
2178 bool want_zero,
2179 int64_t offset,
2180 int64_t bytes,
2181 int64_t *pnum,
2182 int64_t *map,
2183 BlockDriverState **file)
ba3f0e25
FZ
2184{
2185 BlockDriverState *p;
5b648c67 2186 int ret = 0;
c61e684e 2187 bool first = true;
ba3f0e25
FZ
2188
2189 assert(bs != base);
760e0063 2190 for (p = bs; p != base; p = backing_bs(p)) {
5b648c67
EB
2191 ret = bdrv_co_block_status(p, want_zero, offset, bytes, pnum, map,
2192 file);
c61e684e
EB
2193 if (ret < 0) {
2194 break;
2195 }
2196 if (ret & BDRV_BLOCK_ZERO && ret & BDRV_BLOCK_EOF && !first) {
2197 /*
2198 * Reading beyond the end of the file continues to read
2199 * zeroes, but we can only widen the result to the
2200 * unallocated length we learned from an earlier
2201 * iteration.
2202 */
5b648c67 2203 *pnum = bytes;
c61e684e
EB
2204 }
2205 if (ret & (BDRV_BLOCK_ZERO | BDRV_BLOCK_DATA)) {
ba3f0e25
FZ
2206 break;
2207 }
5b648c67
EB
2208 /* [offset, pnum] unallocated on this layer, which could be only
2209 * the first part of [offset, bytes]. */
2210 bytes = MIN(bytes, *pnum);
c61e684e 2211 first = false;
ba3f0e25
FZ
2212 }
2213 return ret;
2214}
2215
31826642 2216/* Coroutine wrapper for bdrv_block_status_above() */
5b648c67 2217static void coroutine_fn bdrv_block_status_above_co_entry(void *opaque)
61007b31 2218{
4bcd936e 2219 BdrvCoBlockStatusData *data = opaque;
61007b31 2220
5b648c67
EB
2221 data->ret = bdrv_co_block_status_above(data->bs, data->base,
2222 data->want_zero,
2223 data->offset, data->bytes,
2224 data->pnum, data->map, data->file);
61007b31 2225 data->done = true;
4720cbee 2226 aio_wait_kick();
61007b31
SH
2227}
2228
2229/*
5b648c67 2230 * Synchronous wrapper around bdrv_co_block_status_above().
61007b31 2231 *
5b648c67 2232 * See bdrv_co_block_status_above() for details.
61007b31 2233 */
7ddb99b9
EB
2234static int bdrv_common_block_status_above(BlockDriverState *bs,
2235 BlockDriverState *base,
2236 bool want_zero, int64_t offset,
2237 int64_t bytes, int64_t *pnum,
2238 int64_t *map,
2239 BlockDriverState **file)
61007b31
SH
2240{
2241 Coroutine *co;
4bcd936e 2242 BdrvCoBlockStatusData data = {
61007b31 2243 .bs = bs,
ba3f0e25 2244 .base = base,
c9ce8c4d 2245 .want_zero = want_zero,
7ddb99b9
EB
2246 .offset = offset,
2247 .bytes = bytes,
2248 .pnum = pnum,
2249 .map = map,
c9ce8c4d 2250 .file = file,
61007b31
SH
2251 .done = false,
2252 };
2253
2254 if (qemu_in_coroutine()) {
2255 /* Fast-path if already in coroutine context */
5b648c67 2256 bdrv_block_status_above_co_entry(&data);
61007b31 2257 } else {
5b648c67 2258 co = qemu_coroutine_create(bdrv_block_status_above_co_entry, &data);
e92f0e19 2259 bdrv_coroutine_enter(bs, co);
88b062c2 2260 BDRV_POLL_WHILE(bs, !data.done);
61007b31 2261 }
7ddb99b9 2262 return data.ret;
61007b31
SH
2263}
2264
31826642
EB
2265int bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base,
2266 int64_t offset, int64_t bytes, int64_t *pnum,
2267 int64_t *map, BlockDriverState **file)
c9ce8c4d 2268{
31826642
EB
2269 return bdrv_common_block_status_above(bs, base, true, offset, bytes,
2270 pnum, map, file);
c9ce8c4d
EB
2271}
2272
237d78f8
EB
2273int bdrv_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes,
2274 int64_t *pnum, int64_t *map, BlockDriverState **file)
ba3f0e25 2275{
31826642
EB
2276 return bdrv_block_status_above(bs, backing_bs(bs),
2277 offset, bytes, pnum, map, file);
ba3f0e25
FZ
2278}
2279
d6a644bb
EB
2280int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,
2281 int64_t bytes, int64_t *pnum)
61007b31 2282{
7ddb99b9
EB
2283 int ret;
2284 int64_t dummy;
d6a644bb 2285
7ddb99b9
EB
2286 ret = bdrv_common_block_status_above(bs, backing_bs(bs), false, offset,
2287 bytes, pnum ? pnum : &dummy, NULL,
c9ce8c4d 2288 NULL);
61007b31
SH
2289 if (ret < 0) {
2290 return ret;
2291 }
2292 return !!(ret & BDRV_BLOCK_ALLOCATED);
2293}
2294
2295/*
2296 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2297 *
51b0a488
EB
2298 * Return true if (a prefix of) the given range is allocated in any image
2299 * between BASE and TOP (inclusive). BASE can be NULL to check if the given
2300 * offset is allocated in any image of the chain. Return false otherwise,
d6a644bb 2301 * or negative errno on failure.
61007b31 2302 *
51b0a488
EB
2303 * 'pnum' is set to the number of bytes (including and immediately
2304 * following the specified offset) that are known to be in the same
2305 * allocated/unallocated state. Note that a subsequent call starting
2306 * at 'offset + *pnum' may return the same allocation status (in other
2307 * words, the result is not necessarily the maximum possible range);
2308 * but 'pnum' will only be 0 when end of file is reached.
61007b31
SH
2309 *
2310 */
2311int bdrv_is_allocated_above(BlockDriverState *top,
2312 BlockDriverState *base,
51b0a488 2313 int64_t offset, int64_t bytes, int64_t *pnum)
61007b31
SH
2314{
2315 BlockDriverState *intermediate;
51b0a488
EB
2316 int ret;
2317 int64_t n = bytes;
61007b31
SH
2318
2319 intermediate = top;
2320 while (intermediate && intermediate != base) {
d6a644bb 2321 int64_t pnum_inter;
c00716be 2322 int64_t size_inter;
d6a644bb 2323
51b0a488 2324 ret = bdrv_is_allocated(intermediate, offset, bytes, &pnum_inter);
61007b31
SH
2325 if (ret < 0) {
2326 return ret;
d6a644bb 2327 }
d6a644bb 2328 if (ret) {
51b0a488 2329 *pnum = pnum_inter;
61007b31
SH
2330 return 1;
2331 }
2332
51b0a488 2333 size_inter = bdrv_getlength(intermediate);
c00716be
EB
2334 if (size_inter < 0) {
2335 return size_inter;
2336 }
51b0a488
EB
2337 if (n > pnum_inter &&
2338 (intermediate == top || offset + pnum_inter < size_inter)) {
2339 n = pnum_inter;
61007b31
SH
2340 }
2341
760e0063 2342 intermediate = backing_bs(intermediate);
61007b31
SH
2343 }
2344
2345 *pnum = n;
2346 return 0;
2347}
2348
1a8ae822
KW
2349typedef struct BdrvVmstateCo {
2350 BlockDriverState *bs;
2351 QEMUIOVector *qiov;
2352 int64_t pos;
2353 bool is_read;
2354 int ret;
2355} BdrvVmstateCo;
2356
2357static int coroutine_fn
2358bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
2359 bool is_read)
2360{
2361 BlockDriver *drv = bs->drv;
dc88a467
SH
2362 int ret = -ENOTSUP;
2363
2364 bdrv_inc_in_flight(bs);
1a8ae822
KW
2365
2366 if (!drv) {
dc88a467 2367 ret = -ENOMEDIUM;
1a8ae822 2368 } else if (drv->bdrv_load_vmstate) {
dc88a467
SH
2369 if (is_read) {
2370 ret = drv->bdrv_load_vmstate(bs, qiov, pos);
2371 } else {
2372 ret = drv->bdrv_save_vmstate(bs, qiov, pos);
2373 }
1a8ae822 2374 } else if (bs->file) {
dc88a467 2375 ret = bdrv_co_rw_vmstate(bs->file->bs, qiov, pos, is_read);
1a8ae822
KW
2376 }
2377
dc88a467
SH
2378 bdrv_dec_in_flight(bs);
2379 return ret;
1a8ae822
KW
2380}
2381
2382static void coroutine_fn bdrv_co_rw_vmstate_entry(void *opaque)
2383{
2384 BdrvVmstateCo *co = opaque;
2385 co->ret = bdrv_co_rw_vmstate(co->bs, co->qiov, co->pos, co->is_read);
4720cbee 2386 aio_wait_kick();
1a8ae822
KW
2387}
2388
2389static inline int
2390bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
2391 bool is_read)
2392{
2393 if (qemu_in_coroutine()) {
2394 return bdrv_co_rw_vmstate(bs, qiov, pos, is_read);
2395 } else {
2396 BdrvVmstateCo data = {
2397 .bs = bs,
2398 .qiov = qiov,
2399 .pos = pos,
2400 .is_read = is_read,
2401 .ret = -EINPROGRESS,
2402 };
0b8b8753 2403 Coroutine *co = qemu_coroutine_create(bdrv_co_rw_vmstate_entry, &data);
1a8ae822 2404
e92f0e19 2405 bdrv_coroutine_enter(bs, co);
ea17c9d2 2406 BDRV_POLL_WHILE(bs, data.ret == -EINPROGRESS);
1a8ae822
KW
2407 return data.ret;
2408 }
2409}
2410
61007b31
SH
2411int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2412 int64_t pos, int size)
2413{
0d93ed08 2414 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
b433d942 2415 int ret;
61007b31 2416
b433d942
KW
2417 ret = bdrv_writev_vmstate(bs, &qiov, pos);
2418 if (ret < 0) {
2419 return ret;
2420 }
2421
2422 return size;
61007b31
SH
2423}
2424
2425int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
2426{
1a8ae822 2427 return bdrv_rw_vmstate(bs, qiov, pos, false);
61007b31
SH
2428}
2429
2430int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2431 int64_t pos, int size)
5ddda0b8 2432{
0d93ed08 2433 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
b433d942 2434 int ret;
5ddda0b8 2435
b433d942
KW
2436 ret = bdrv_readv_vmstate(bs, &qiov, pos);
2437 if (ret < 0) {
2438 return ret;
2439 }
2440
2441 return size;
5ddda0b8
KW
2442}
2443
2444int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
61007b31 2445{
1a8ae822 2446 return bdrv_rw_vmstate(bs, qiov, pos, true);
61007b31
SH
2447}
2448
2449/**************************************************************/
2450/* async I/Os */
2451
61007b31
SH
2452void bdrv_aio_cancel(BlockAIOCB *acb)
2453{
2454 qemu_aio_ref(acb);
2455 bdrv_aio_cancel_async(acb);
2456 while (acb->refcnt > 1) {
2457 if (acb->aiocb_info->get_aio_context) {
2458 aio_poll(acb->aiocb_info->get_aio_context(acb), true);
2459 } else if (acb->bs) {
2f47da5f
PB
2460 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so
2461 * assert that we're not using an I/O thread. Thread-safe
2462 * code should use bdrv_aio_cancel_async exclusively.
2463 */
2464 assert(bdrv_get_aio_context(acb->bs) == qemu_get_aio_context());
61007b31
SH
2465 aio_poll(bdrv_get_aio_context(acb->bs), true);
2466 } else {
2467 abort();
2468 }
2469 }
2470 qemu_aio_unref(acb);
2471}
2472
2473/* Async version of aio cancel. The caller is not blocked if the acb implements
2474 * cancel_async, otherwise we do nothing and let the request normally complete.
2475 * In either case the completion callback must be called. */
2476void bdrv_aio_cancel_async(BlockAIOCB *acb)
2477{
2478 if (acb->aiocb_info->cancel_async) {
2479 acb->aiocb_info->cancel_async(acb);
2480 }
2481}
2482
61007b31
SH
2483/**************************************************************/
2484/* Coroutine block device emulation */
2485
e293b7a3
KW
2486typedef struct FlushCo {
2487 BlockDriverState *bs;
2488 int ret;
2489} FlushCo;
2490
2491
61007b31
SH
2492static void coroutine_fn bdrv_flush_co_entry(void *opaque)
2493{
e293b7a3 2494 FlushCo *rwco = opaque;
61007b31
SH
2495
2496 rwco->ret = bdrv_co_flush(rwco->bs);
4720cbee 2497 aio_wait_kick();
61007b31
SH
2498}
2499
2500int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2501{
49ca6259
FZ
2502 int current_gen;
2503 int ret = 0;
2504
2505 bdrv_inc_in_flight(bs);
61007b31 2506
e914404e 2507 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
1b6bc94d 2508 bdrv_is_sg(bs)) {
49ca6259 2509 goto early_exit;
61007b31
SH
2510 }
2511
3783fa3d 2512 qemu_co_mutex_lock(&bs->reqs_lock);
47fec599 2513 current_gen = atomic_read(&bs->write_gen);
3ff2f67a
EY
2514
2515 /* Wait until any previous flushes are completed */
99723548 2516 while (bs->active_flush_req) {
3783fa3d 2517 qemu_co_queue_wait(&bs->flush_queue, &bs->reqs_lock);
3ff2f67a
EY
2518 }
2519
3783fa3d 2520 /* Flushes reach this point in nondecreasing current_gen order. */
99723548 2521 bs->active_flush_req = true;
3783fa3d 2522 qemu_co_mutex_unlock(&bs->reqs_lock);
3ff2f67a 2523
c32b82af
PD
2524 /* Write back all layers by calling one driver function */
2525 if (bs->drv->bdrv_co_flush) {
2526 ret = bs->drv->bdrv_co_flush(bs);
2527 goto out;
2528 }
2529
61007b31
SH
2530 /* Write back cached data to the OS even with cache=unsafe */
2531 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS);
2532 if (bs->drv->bdrv_co_flush_to_os) {
2533 ret = bs->drv->bdrv_co_flush_to_os(bs);
2534 if (ret < 0) {
cdb5e315 2535 goto out;
61007b31
SH
2536 }
2537 }
2538
2539 /* But don't actually force it to the disk with cache=unsafe */
2540 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2541 goto flush_parent;
2542 }
2543
3ff2f67a
EY
2544 /* Check if we really need to flush anything */
2545 if (bs->flushed_gen == current_gen) {
2546 goto flush_parent;
2547 }
2548
61007b31 2549 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
d470ad42
HR
2550 if (!bs->drv) {
2551 /* bs->drv->bdrv_co_flush() might have ejected the BDS
2552 * (even in case of apparent success) */
2553 ret = -ENOMEDIUM;
2554 goto out;
2555 }
61007b31
SH
2556 if (bs->drv->bdrv_co_flush_to_disk) {
2557 ret = bs->drv->bdrv_co_flush_to_disk(bs);
2558 } else if (bs->drv->bdrv_aio_flush) {
2559 BlockAIOCB *acb;
2560 CoroutineIOCompletion co = {
2561 .coroutine = qemu_coroutine_self(),
2562 };
2563
2564 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
2565 if (acb == NULL) {
2566 ret = -EIO;
2567 } else {
2568 qemu_coroutine_yield();
2569 ret = co.ret;
2570 }
2571 } else {
2572 /*
2573 * Some block drivers always operate in either writethrough or unsafe
2574 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2575 * know how the server works (because the behaviour is hardcoded or
2576 * depends on server-side configuration), so we can't ensure that
2577 * everything is safe on disk. Returning an error doesn't work because
2578 * that would break guests even if the server operates in writethrough
2579 * mode.
2580 *
2581 * Let's hope the user knows what he's doing.
2582 */
2583 ret = 0;
2584 }
3ff2f67a 2585
61007b31 2586 if (ret < 0) {
cdb5e315 2587 goto out;
61007b31
SH
2588 }
2589
2590 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
2591 * in the case of cache=unsafe, so there are no useless flushes.
2592 */
2593flush_parent:
cdb5e315
FZ
2594 ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0;
2595out:
3ff2f67a 2596 /* Notify any pending flushes that we have completed */
e6af1e08
KW
2597 if (ret == 0) {
2598 bs->flushed_gen = current_gen;
2599 }
3783fa3d
PB
2600
2601 qemu_co_mutex_lock(&bs->reqs_lock);
99723548 2602 bs->active_flush_req = false;
156af3ac
DL
2603 /* Return value is ignored - it's ok if wait queue is empty */
2604 qemu_co_queue_next(&bs->flush_queue);
3783fa3d 2605 qemu_co_mutex_unlock(&bs->reqs_lock);
3ff2f67a 2606
49ca6259 2607early_exit:
99723548 2608 bdrv_dec_in_flight(bs);
cdb5e315 2609 return ret;
61007b31
SH
2610}
2611
2612int bdrv_flush(BlockDriverState *bs)
2613{
2614 Coroutine *co;
e293b7a3 2615 FlushCo flush_co = {
61007b31
SH
2616 .bs = bs,
2617 .ret = NOT_DONE,
2618 };
2619
2620 if (qemu_in_coroutine()) {
2621 /* Fast-path if already in coroutine context */
e293b7a3 2622 bdrv_flush_co_entry(&flush_co);
61007b31 2623 } else {
0b8b8753 2624 co = qemu_coroutine_create(bdrv_flush_co_entry, &flush_co);
e92f0e19 2625 bdrv_coroutine_enter(bs, co);
88b062c2 2626 BDRV_POLL_WHILE(bs, flush_co.ret == NOT_DONE);
61007b31
SH
2627 }
2628
e293b7a3 2629 return flush_co.ret;
61007b31
SH
2630}
2631
2632typedef struct DiscardCo {
0b9fd3f4 2633 BdrvChild *child;
0c51a893 2634 int64_t offset;
d93e5726 2635 int64_t bytes;
61007b31
SH
2636 int ret;
2637} DiscardCo;
0c51a893 2638static void coroutine_fn bdrv_pdiscard_co_entry(void *opaque)
61007b31
SH
2639{
2640 DiscardCo *rwco = opaque;
2641
0b9fd3f4 2642 rwco->ret = bdrv_co_pdiscard(rwco->child, rwco->offset, rwco->bytes);
4720cbee 2643 aio_wait_kick();
61007b31
SH
2644}
2645
d93e5726
VSO
2646int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
2647 int64_t bytes)
61007b31 2648{
b1066c87 2649 BdrvTrackedRequest req;
9f1963b3 2650 int max_pdiscard, ret;
3482b9bc 2651 int head, tail, align;
0b9fd3f4 2652 BlockDriverState *bs = child->bs;
61007b31 2653
d93e5726 2654 if (!bs || !bs->drv || !bdrv_is_inserted(bs)) {
61007b31
SH
2655 return -ENOMEDIUM;
2656 }
2657
d6883bc9
VSO
2658 if (bdrv_has_readonly_bitmaps(bs)) {
2659 return -EPERM;
2660 }
2661
d93e5726
VSO
2662 if (offset < 0 || bytes < 0 || bytes > INT64_MAX - offset) {
2663 return -EIO;
61007b31
SH
2664 }
2665
61007b31
SH
2666 /* Do nothing if disabled. */
2667 if (!(bs->open_flags & BDRV_O_UNMAP)) {
2668 return 0;
2669 }
2670
02aefe43 2671 if (!bs->drv->bdrv_co_pdiscard && !bs->drv->bdrv_aio_pdiscard) {
61007b31
SH
2672 return 0;
2673 }
2674
3482b9bc
EB
2675 /* Discard is advisory, but some devices track and coalesce
2676 * unaligned requests, so we must pass everything down rather than
2677 * round here. Still, most devices will just silently ignore
2678 * unaligned requests (by returning -ENOTSUP), so we must fragment
2679 * the request accordingly. */
02aefe43 2680 align = MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment);
b8d0a980
EB
2681 assert(align % bs->bl.request_alignment == 0);
2682 head = offset % align;
f5a5ca79 2683 tail = (offset + bytes) % align;
9f1963b3 2684
99723548 2685 bdrv_inc_in_flight(bs);
f5a5ca79 2686 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_DISCARD);
50824995 2687
00695c27 2688 ret = bdrv_co_write_req_prepare(child, offset, bytes, &req, 0);
ec050f77
DL
2689 if (ret < 0) {
2690 goto out;
2691 }
2692
9f1963b3
EB
2693 max_pdiscard = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard, INT_MAX),
2694 align);
3482b9bc 2695 assert(max_pdiscard >= bs->bl.request_alignment);
61007b31 2696
f5a5ca79 2697 while (bytes > 0) {
d93e5726 2698 int64_t num = bytes;
3482b9bc
EB
2699
2700 if (head) {
2701 /* Make small requests to get to alignment boundaries. */
f5a5ca79 2702 num = MIN(bytes, align - head);
3482b9bc
EB
2703 if (!QEMU_IS_ALIGNED(num, bs->bl.request_alignment)) {
2704 num %= bs->bl.request_alignment;
2705 }
2706 head = (head + num) % align;
2707 assert(num < max_pdiscard);
2708 } else if (tail) {
2709 if (num > align) {
2710 /* Shorten the request to the last aligned cluster. */
2711 num -= tail;
2712 } else if (!QEMU_IS_ALIGNED(tail, bs->bl.request_alignment) &&
2713 tail > bs->bl.request_alignment) {
2714 tail %= bs->bl.request_alignment;
2715 num -= tail;
2716 }
2717 }
2718 /* limit request size */
2719 if (num > max_pdiscard) {
2720 num = max_pdiscard;
2721 }
61007b31 2722
d470ad42
HR
2723 if (!bs->drv) {
2724 ret = -ENOMEDIUM;
2725 goto out;
2726 }
47a5486d
EB
2727 if (bs->drv->bdrv_co_pdiscard) {
2728 ret = bs->drv->bdrv_co_pdiscard(bs, offset, num);
61007b31
SH
2729 } else {
2730 BlockAIOCB *acb;
2731 CoroutineIOCompletion co = {
2732 .coroutine = qemu_coroutine_self(),
2733 };
2734
4da444a0
EB
2735 acb = bs->drv->bdrv_aio_pdiscard(bs, offset, num,
2736 bdrv_co_io_em_complete, &co);
61007b31 2737 if (acb == NULL) {
b1066c87
FZ
2738 ret = -EIO;
2739 goto out;
61007b31
SH
2740 } else {
2741 qemu_coroutine_yield();
2742 ret = co.ret;
2743 }
2744 }
2745 if (ret && ret != -ENOTSUP) {
b1066c87 2746 goto out;
61007b31
SH
2747 }
2748
9f1963b3 2749 offset += num;
f5a5ca79 2750 bytes -= num;
61007b31 2751 }
b1066c87
FZ
2752 ret = 0;
2753out:
00695c27 2754 bdrv_co_write_req_finish(child, req.offset, req.bytes, &req, ret);
b1066c87 2755 tracked_request_end(&req);
99723548 2756 bdrv_dec_in_flight(bs);
b1066c87 2757 return ret;
61007b31
SH
2758}
2759
d93e5726 2760int bdrv_pdiscard(BdrvChild *child, int64_t offset, int64_t bytes)
61007b31
SH
2761{
2762 Coroutine *co;
2763 DiscardCo rwco = {
0b9fd3f4 2764 .child = child,
0c51a893 2765 .offset = offset,
f5a5ca79 2766 .bytes = bytes,
61007b31
SH
2767 .ret = NOT_DONE,
2768 };
2769
2770 if (qemu_in_coroutine()) {
2771 /* Fast-path if already in coroutine context */
0c51a893 2772 bdrv_pdiscard_co_entry(&rwco);
61007b31 2773 } else {
0c51a893 2774 co = qemu_coroutine_create(bdrv_pdiscard_co_entry, &rwco);
0b9fd3f4
FZ
2775 bdrv_coroutine_enter(child->bs, co);
2776 BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE);
61007b31
SH
2777 }
2778
2779 return rwco.ret;
2780}
2781
48af776a 2782int bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf)
61007b31
SH
2783{
2784 BlockDriver *drv = bs->drv;
5c5ae76a
FZ
2785 CoroutineIOCompletion co = {
2786 .coroutine = qemu_coroutine_self(),
2787 };
2788 BlockAIOCB *acb;
61007b31 2789
99723548 2790 bdrv_inc_in_flight(bs);
16a389dc 2791 if (!drv || (!drv->bdrv_aio_ioctl && !drv->bdrv_co_ioctl)) {
5c5ae76a
FZ
2792 co.ret = -ENOTSUP;
2793 goto out;
2794 }
2795
16a389dc
KW
2796 if (drv->bdrv_co_ioctl) {
2797 co.ret = drv->bdrv_co_ioctl(bs, req, buf);
2798 } else {
2799 acb = drv->bdrv_aio_ioctl(bs, req, buf, bdrv_co_io_em_complete, &co);
2800 if (!acb) {
2801 co.ret = -ENOTSUP;
2802 goto out;
2803 }
2804 qemu_coroutine_yield();
5c5ae76a 2805 }
5c5ae76a 2806out:
99723548 2807 bdrv_dec_in_flight(bs);
5c5ae76a
FZ
2808 return co.ret;
2809}
2810
61007b31
SH
2811void *qemu_blockalign(BlockDriverState *bs, size_t size)
2812{
2813 return qemu_memalign(bdrv_opt_mem_align(bs), size);
2814}
2815
2816void *qemu_blockalign0(BlockDriverState *bs, size_t size)
2817{
2818 return memset(qemu_blockalign(bs, size), 0, size);
2819}
2820
2821void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
2822{
2823 size_t align = bdrv_opt_mem_align(bs);
2824
2825 /* Ensure that NULL is never returned on success */
2826 assert(align > 0);
2827 if (size == 0) {
2828 size = align;
2829 }
2830
2831 return qemu_try_memalign(align, size);
2832}
2833
2834void *qemu_try_blockalign0(BlockDriverState *bs, size_t size)
2835{
2836 void *mem = qemu_try_blockalign(bs, size);
2837
2838 if (mem) {
2839 memset(mem, 0, size);
2840 }
2841
2842 return mem;
2843}
2844
2845/*
2846 * Check if all memory in this vector is sector aligned.
2847 */
2848bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
2849{
2850 int i;
4196d2f0 2851 size_t alignment = bdrv_min_mem_align(bs);
61007b31
SH
2852
2853 for (i = 0; i < qiov->niov; i++) {
2854 if ((uintptr_t) qiov->iov[i].iov_base % alignment) {
2855 return false;
2856 }
2857 if (qiov->iov[i].iov_len % alignment) {
2858 return false;
2859 }
2860 }
2861
2862 return true;
2863}
2864
2865void bdrv_add_before_write_notifier(BlockDriverState *bs,
2866 NotifierWithReturn *notifier)
2867{
2868 notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
2869}
2870
2871void bdrv_io_plug(BlockDriverState *bs)
2872{
6b98bd64
PB
2873 BdrvChild *child;
2874
2875 QLIST_FOREACH(child, &bs->children, next) {
2876 bdrv_io_plug(child->bs);
2877 }
2878
850d54a2 2879 if (atomic_fetch_inc(&bs->io_plugged) == 0) {
6b98bd64
PB
2880 BlockDriver *drv = bs->drv;
2881 if (drv && drv->bdrv_io_plug) {
2882 drv->bdrv_io_plug(bs);
2883 }
61007b31
SH
2884 }
2885}
2886
2887void bdrv_io_unplug(BlockDriverState *bs)
2888{
6b98bd64
PB
2889 BdrvChild *child;
2890
2891 assert(bs->io_plugged);
850d54a2 2892 if (atomic_fetch_dec(&bs->io_plugged) == 1) {
6b98bd64
PB
2893 BlockDriver *drv = bs->drv;
2894 if (drv && drv->bdrv_io_unplug) {
2895 drv->bdrv_io_unplug(bs);
2896 }
2897 }
2898
2899 QLIST_FOREACH(child, &bs->children, next) {
2900 bdrv_io_unplug(child->bs);
61007b31
SH
2901 }
2902}
23d0ba93
FZ
2903
2904void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size)
2905{
2906 BdrvChild *child;
2907
2908 if (bs->drv && bs->drv->bdrv_register_buf) {
2909 bs->drv->bdrv_register_buf(bs, host, size);
2910 }
2911 QLIST_FOREACH(child, &bs->children, next) {
2912 bdrv_register_buf(child->bs, host, size);
2913 }
2914}
2915
2916void bdrv_unregister_buf(BlockDriverState *bs, void *host)
2917{
2918 BdrvChild *child;
2919
2920 if (bs->drv && bs->drv->bdrv_unregister_buf) {
2921 bs->drv->bdrv_unregister_buf(bs, host);
2922 }
2923 QLIST_FOREACH(child, &bs->children, next) {
2924 bdrv_unregister_buf(child->bs, host);
2925 }
2926}
fcc67678 2927
67b51fb9
VSO
2928static int coroutine_fn bdrv_co_copy_range_internal(
2929 BdrvChild *src, uint64_t src_offset, BdrvChild *dst,
2930 uint64_t dst_offset, uint64_t bytes,
2931 BdrvRequestFlags read_flags, BdrvRequestFlags write_flags,
2932 bool recurse_src)
fcc67678 2933{
999658a0 2934 BdrvTrackedRequest req;
fcc67678
FZ
2935 int ret;
2936
fe0480d6
KW
2937 /* TODO We can support BDRV_REQ_NO_FALLBACK here */
2938 assert(!(read_flags & BDRV_REQ_NO_FALLBACK));
2939 assert(!(write_flags & BDRV_REQ_NO_FALLBACK));
2940
d4d3e5a0 2941 if (!dst || !dst->bs) {
fcc67678
FZ
2942 return -ENOMEDIUM;
2943 }
fcc67678
FZ
2944 ret = bdrv_check_byte_request(dst->bs, dst_offset, bytes);
2945 if (ret) {
2946 return ret;
2947 }
67b51fb9
VSO
2948 if (write_flags & BDRV_REQ_ZERO_WRITE) {
2949 return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, write_flags);
fcc67678
FZ
2950 }
2951
d4d3e5a0
FZ
2952 if (!src || !src->bs) {
2953 return -ENOMEDIUM;
2954 }
2955 ret = bdrv_check_byte_request(src->bs, src_offset, bytes);
2956 if (ret) {
2957 return ret;
2958 }
2959
fcc67678
FZ
2960 if (!src->bs->drv->bdrv_co_copy_range_from
2961 || !dst->bs->drv->bdrv_co_copy_range_to
2962 || src->bs->encrypted || dst->bs->encrypted) {
2963 return -ENOTSUP;
2964 }
37aec7d7 2965
fcc67678 2966 if (recurse_src) {
999658a0
VSO
2967 bdrv_inc_in_flight(src->bs);
2968 tracked_request_begin(&req, src->bs, src_offset, bytes,
2969 BDRV_TRACKED_READ);
2970
09d2f948
VSO
2971 /* BDRV_REQ_SERIALISING is only for write operation */
2972 assert(!(read_flags & BDRV_REQ_SERIALISING));
67b51fb9 2973 if (!(read_flags & BDRV_REQ_NO_SERIALISING)) {
999658a0
VSO
2974 wait_serialising_requests(&req);
2975 }
2976
37aec7d7
FZ
2977 ret = src->bs->drv->bdrv_co_copy_range_from(src->bs,
2978 src, src_offset,
2979 dst, dst_offset,
67b51fb9
VSO
2980 bytes,
2981 read_flags, write_flags);
999658a0
VSO
2982
2983 tracked_request_end(&req);
2984 bdrv_dec_in_flight(src->bs);
fcc67678 2985 } else {
999658a0
VSO
2986 bdrv_inc_in_flight(dst->bs);
2987 tracked_request_begin(&req, dst->bs, dst_offset, bytes,
2988 BDRV_TRACKED_WRITE);
0eb1e891
FZ
2989 ret = bdrv_co_write_req_prepare(dst, dst_offset, bytes, &req,
2990 write_flags);
2991 if (!ret) {
2992 ret = dst->bs->drv->bdrv_co_copy_range_to(dst->bs,
2993 src, src_offset,
2994 dst, dst_offset,
2995 bytes,
2996 read_flags, write_flags);
2997 }
2998 bdrv_co_write_req_finish(dst, dst_offset, bytes, &req, ret);
999658a0
VSO
2999 tracked_request_end(&req);
3000 bdrv_dec_in_flight(dst->bs);
fcc67678 3001 }
999658a0 3002
37aec7d7 3003 return ret;
fcc67678
FZ
3004}
3005
3006/* Copy range from @src to @dst.
3007 *
3008 * See the comment of bdrv_co_copy_range for the parameter and return value
3009 * semantics. */
3010int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, uint64_t src_offset,
3011 BdrvChild *dst, uint64_t dst_offset,
67b51fb9
VSO
3012 uint64_t bytes,
3013 BdrvRequestFlags read_flags,
3014 BdrvRequestFlags write_flags)
fcc67678 3015{
ecc983a5
FZ
3016 trace_bdrv_co_copy_range_from(src, src_offset, dst, dst_offset, bytes,
3017 read_flags, write_flags);
fcc67678 3018 return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
67b51fb9 3019 bytes, read_flags, write_flags, true);
fcc67678
FZ
3020}
3021
3022/* Copy range from @src to @dst.
3023 *
3024 * See the comment of bdrv_co_copy_range for the parameter and return value
3025 * semantics. */
3026int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, uint64_t src_offset,
3027 BdrvChild *dst, uint64_t dst_offset,
67b51fb9
VSO
3028 uint64_t bytes,
3029 BdrvRequestFlags read_flags,
3030 BdrvRequestFlags write_flags)
fcc67678 3031{
ecc983a5
FZ
3032 trace_bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
3033 read_flags, write_flags);
fcc67678 3034 return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
67b51fb9 3035 bytes, read_flags, write_flags, false);
fcc67678
FZ
3036}
3037
3038int coroutine_fn bdrv_co_copy_range(BdrvChild *src, uint64_t src_offset,
3039 BdrvChild *dst, uint64_t dst_offset,
67b51fb9
VSO
3040 uint64_t bytes, BdrvRequestFlags read_flags,
3041 BdrvRequestFlags write_flags)
fcc67678 3042{
37aec7d7
FZ
3043 return bdrv_co_copy_range_from(src, src_offset,
3044 dst, dst_offset,
67b51fb9 3045 bytes, read_flags, write_flags);
fcc67678 3046}
3d9f2d2a
KW
3047
3048static void bdrv_parent_cb_resize(BlockDriverState *bs)
3049{
3050 BdrvChild *c;
3051 QLIST_FOREACH(c, &bs->parents, next_parent) {
3052 if (c->role->resize) {
3053 c->role->resize(c);
3054 }
3055 }
3056}
3057
3058/**
3059 * Truncate file to 'offset' bytes (needed only for file protocols)
3060 */
3061int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset,
3062 PreallocMode prealloc, Error **errp)
3063{
3064 BlockDriverState *bs = child->bs;
3065 BlockDriver *drv = bs->drv;
1bc5f09f
KW
3066 BdrvTrackedRequest req;
3067 int64_t old_size, new_bytes;
3d9f2d2a
KW
3068 int ret;
3069
3d9f2d2a
KW
3070
3071 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3072 if (!drv) {
3073 error_setg(errp, "No medium inserted");
3074 return -ENOMEDIUM;
3075 }
3076 if (offset < 0) {
3077 error_setg(errp, "Image size cannot be negative");
3078 return -EINVAL;
3079 }
3080
1bc5f09f
KW
3081 old_size = bdrv_getlength(bs);
3082 if (old_size < 0) {
3083 error_setg_errno(errp, -old_size, "Failed to get old image size");
3084 return old_size;
3085 }
3086
3087 if (offset > old_size) {
3088 new_bytes = offset - old_size;
3089 } else {
3090 new_bytes = 0;
3091 }
3092
3d9f2d2a 3093 bdrv_inc_in_flight(bs);
5416a11e
FZ
3094 tracked_request_begin(&req, bs, offset - new_bytes, new_bytes,
3095 BDRV_TRACKED_TRUNCATE);
1bc5f09f
KW
3096
3097 /* If we are growing the image and potentially using preallocation for the
3098 * new area, we need to make sure that no write requests are made to it
3099 * concurrently or they might be overwritten by preallocation. */
3100 if (new_bytes) {
3101 mark_request_serialising(&req, 1);
cd47d792
FZ
3102 }
3103 if (bs->read_only) {
3104 error_setg(errp, "Image is read-only");
3105 ret = -EACCES;
3106 goto out;
3107 }
3108 ret = bdrv_co_write_req_prepare(child, offset - new_bytes, new_bytes, &req,
3109 0);
3110 if (ret < 0) {
3111 error_setg_errno(errp, -ret,
3112 "Failed to prepare request for truncation");
3113 goto out;
1bc5f09f 3114 }
3d9f2d2a
KW
3115
3116 if (!drv->bdrv_co_truncate) {
3117 if (bs->file && drv->is_filter) {
3118 ret = bdrv_co_truncate(bs->file, offset, prealloc, errp);
3119 goto out;
3120 }
3121 error_setg(errp, "Image format driver does not support resize");
3122 ret = -ENOTSUP;
3123 goto out;
3124 }
3d9f2d2a
KW
3125
3126 ret = drv->bdrv_co_truncate(bs, offset, prealloc, errp);
3127 if (ret < 0) {
3128 goto out;
3129 }
3130 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
3131 if (ret < 0) {
3132 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3133 } else {
3134 offset = bs->total_sectors * BDRV_SECTOR_SIZE;
3135 }
cd47d792
FZ
3136 /* It's possible that truncation succeeded but refresh_total_sectors
3137 * failed, but the latter doesn't affect how we should finish the request.
3138 * Pass 0 as the last parameter so that dirty bitmaps etc. are handled. */
3139 bdrv_co_write_req_finish(child, offset - new_bytes, new_bytes, &req, 0);
3d9f2d2a
KW
3140
3141out:
1bc5f09f 3142 tracked_request_end(&req);
3d9f2d2a 3143 bdrv_dec_in_flight(bs);
1bc5f09f 3144
3d9f2d2a
KW
3145 return ret;
3146}
3147
3148typedef struct TruncateCo {
3149 BdrvChild *child;
3150 int64_t offset;
3151 PreallocMode prealloc;
3152 Error **errp;
3153 int ret;
3154} TruncateCo;
3155
3156static void coroutine_fn bdrv_truncate_co_entry(void *opaque)
3157{
3158 TruncateCo *tco = opaque;
3159 tco->ret = bdrv_co_truncate(tco->child, tco->offset, tco->prealloc,
3160 tco->errp);
4720cbee 3161 aio_wait_kick();
3d9f2d2a
KW
3162}
3163
3164int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,
3165 Error **errp)
3166{
3167 Coroutine *co;
3168 TruncateCo tco = {
3169 .child = child,
3170 .offset = offset,
3171 .prealloc = prealloc,
3172 .errp = errp,
3173 .ret = NOT_DONE,
3174 };
3175
3176 if (qemu_in_coroutine()) {
3177 /* Fast-path if already in coroutine context */
3178 bdrv_truncate_co_entry(&tco);
3179 } else {
3180 co = qemu_coroutine_create(bdrv_truncate_co_entry, &tco);
4720cbee 3181 bdrv_coroutine_enter(child->bs, co);
3d9f2d2a
KW
3182 BDRV_POLL_WHILE(child->bs, tco.ret == NOT_DONE);
3183 }
3184
3185 return tco.ret;
3186}