]> git.proxmox.com Git - mirror_qemu.git/blame - block/io.c
block: Mark bdrv_first_blk() and bdrv_is_root_node() GRAPH_RDLOCK
[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"
21c2283e 32#include "block/coroutines.h"
e2c1c34f 33#include "block/dirty-bitmap.h"
94783301 34#include "block/write-threshold.h"
f348b6d1 35#include "qemu/cutils.h"
5df022cf 36#include "qemu/memalign.h"
da34e65c 37#include "qapi/error.h"
d49b6836 38#include "qemu/error-report.h"
db725815 39#include "qemu/main-loop.h"
c8aa7895 40#include "sysemu/replay.h"
61007b31 41
cb2e2878
EB
42/* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
43#define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
44
7f8f03ef 45static void bdrv_parent_cb_resize(BlockDriverState *bs);
d05aa8bb 46static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
5ae07b14 47 int64_t offset, int64_t bytes, BdrvRequestFlags flags);
61007b31 48
a82a3bd1 49static void bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore)
61007b31 50{
02d21300 51 BdrvChild *c, *next;
27ccdd52 52
02d21300 53 QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
a82a3bd1 54 if (c == ignore) {
0152bf40
KW
55 continue;
56 }
606ed756 57 bdrv_parent_drained_begin_single(c);
ce0f1412
PB
58 }
59}
61007b31 60
2f65df6e 61void bdrv_parent_drained_end_single(BdrvChild *c)
804db8ea 62{
ab613350 63 GLOBAL_STATE_CODE();
2f65df6e 64
57e05be3
KW
65 assert(c->quiesced_parent);
66 c->quiesced_parent = false;
67
bd86fb99 68 if (c->klass->drained_end) {
2f65df6e 69 c->klass->drained_end(c);
804db8ea
HR
70 }
71}
72
a82a3bd1 73static void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore)
ce0f1412 74{
61ad631c 75 BdrvChild *c;
27ccdd52 76
61ad631c 77 QLIST_FOREACH(c, &bs->parents, next_parent) {
a82a3bd1 78 if (c == ignore) {
0152bf40
KW
79 continue;
80 }
2f65df6e 81 bdrv_parent_drained_end_single(c);
27ccdd52 82 }
61007b31
SH
83}
84
23987471 85bool bdrv_parent_drained_poll_single(BdrvChild *c)
4be6a6d1 86{
bd86fb99
HR
87 if (c->klass->drained_poll) {
88 return c->klass->drained_poll(c);
4be6a6d1
KW
89 }
90 return false;
91}
92
6cd5c9d7
KW
93static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,
94 bool ignore_bds_parents)
89bd0305
KW
95{
96 BdrvChild *c, *next;
97 bool busy = false;
98
99 QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
bd86fb99 100 if (c == ignore || (ignore_bds_parents && c->klass->parent_is_bds)) {
89bd0305
KW
101 continue;
102 }
4be6a6d1 103 busy |= bdrv_parent_drained_poll_single(c);
89bd0305
KW
104 }
105
106 return busy;
107}
108
606ed756 109void bdrv_parent_drained_begin_single(BdrvChild *c)
4be6a6d1 110{
ab613350 111 GLOBAL_STATE_CODE();
57e05be3
KW
112
113 assert(!c->quiesced_parent);
114 c->quiesced_parent = true;
115
bd86fb99
HR
116 if (c->klass->drained_begin) {
117 c->klass->drained_begin(c);
4be6a6d1 118 }
4be6a6d1
KW
119}
120
d9e0dfa2
EB
121static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src)
122{
9f460c64
AO
123 dst->pdiscard_alignment = MAX(dst->pdiscard_alignment,
124 src->pdiscard_alignment);
d9e0dfa2
EB
125 dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer);
126 dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer);
24b36e98
PB
127 dst->max_hw_transfer = MIN_NON_ZERO(dst->max_hw_transfer,
128 src->max_hw_transfer);
d9e0dfa2
EB
129 dst->opt_mem_alignment = MAX(dst->opt_mem_alignment,
130 src->opt_mem_alignment);
131 dst->min_mem_alignment = MAX(dst->min_mem_alignment,
132 src->min_mem_alignment);
133 dst->max_iov = MIN_NON_ZERO(dst->max_iov, src->max_iov);
cc071629 134 dst->max_hw_iov = MIN_NON_ZERO(dst->max_hw_iov, src->max_hw_iov);
d9e0dfa2
EB
135}
136
1e4c797c
VSO
137typedef struct BdrvRefreshLimitsState {
138 BlockDriverState *bs;
139 BlockLimits old_bl;
140} BdrvRefreshLimitsState;
141
142static void bdrv_refresh_limits_abort(void *opaque)
143{
144 BdrvRefreshLimitsState *s = opaque;
145
146 s->bs->bl = s->old_bl;
147}
148
149static TransactionActionDrv bdrv_refresh_limits_drv = {
150 .abort = bdrv_refresh_limits_abort,
151 .clean = g_free,
152};
153
154/* @tran is allowed to be NULL, in this case no rollback is possible. */
155void bdrv_refresh_limits(BlockDriverState *bs, Transaction *tran, Error **errp)
61007b31 156{
33985614 157 ERRP_GUARD();
61007b31 158 BlockDriver *drv = bs->drv;
66b129ac
HR
159 BdrvChild *c;
160 bool have_limits;
61007b31 161
f791bf7f
EGE
162 GLOBAL_STATE_CODE();
163
1e4c797c
VSO
164 if (tran) {
165 BdrvRefreshLimitsState *s = g_new(BdrvRefreshLimitsState, 1);
166 *s = (BdrvRefreshLimitsState) {
167 .bs = bs,
168 .old_bl = bs->bl,
169 };
170 tran_add(tran, &bdrv_refresh_limits_drv, s);
171 }
172
61007b31
SH
173 memset(&bs->bl, 0, sizeof(bs->bl));
174
175 if (!drv) {
176 return;
177 }
178
79ba8c98 179 /* Default alignment based on whether driver has byte interface */
e31f6864 180 bs->bl.request_alignment = (drv->bdrv_co_preadv ||
ac850bf0
VSO
181 drv->bdrv_aio_preadv ||
182 drv->bdrv_co_preadv_part) ? 1 : 512;
79ba8c98 183
61007b31 184 /* Take some limits from the children as a default */
66b129ac
HR
185 have_limits = false;
186 QLIST_FOREACH(c, &bs->children, next) {
187 if (c->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED | BDRV_CHILD_COW))
188 {
66b129ac
HR
189 bdrv_merge_limits(&bs->bl, &c->bs->bl);
190 have_limits = true;
61007b31 191 }
160a29e2
PB
192
193 if (c->role & BDRV_CHILD_FILTERED) {
194 bs->bl.has_variable_length |= c->bs->bl.has_variable_length;
195 }
66b129ac
HR
196 }
197
198 if (!have_limits) {
4196d2f0 199 bs->bl.min_mem_alignment = 512;
8e3b0cbb 200 bs->bl.opt_mem_alignment = qemu_real_host_page_size();
bd44feb7
SH
201
202 /* Safe default since most protocols use readv()/writev()/etc */
203 bs->bl.max_iov = IOV_MAX;
61007b31
SH
204 }
205
61007b31
SH
206 /* Then let the driver override it */
207 if (drv->bdrv_refresh_limits) {
208 drv->bdrv_refresh_limits(bs, errp);
8b117001
VSO
209 if (*errp) {
210 return;
211 }
212 }
213
214 if (bs->bl.request_alignment > BDRV_MAX_ALIGNMENT) {
215 error_setg(errp, "Driver requires too large request alignment");
61007b31
SH
216 }
217}
218
219/**
220 * The copy-on-read flag is actually a reference count so multiple users may
221 * use the feature without worrying about clobbering its previous state.
222 * Copy-on-read stays enabled until all users have called to disable it.
223 */
224void bdrv_enable_copy_on_read(BlockDriverState *bs)
225{
384a48fb 226 IO_CODE();
d73415a3 227 qatomic_inc(&bs->copy_on_read);
61007b31
SH
228}
229
230void bdrv_disable_copy_on_read(BlockDriverState *bs)
231{
d73415a3 232 int old = qatomic_fetch_dec(&bs->copy_on_read);
384a48fb 233 IO_CODE();
d3faa13e 234 assert(old >= 1);
61007b31
SH
235}
236
61124f03
PB
237typedef struct {
238 Coroutine *co;
239 BlockDriverState *bs;
240 bool done;
481cad48 241 bool begin;
fe4f0614 242 bool poll;
0152bf40 243 BdrvChild *parent;
61124f03
PB
244} BdrvCoDrainData;
245
1cc8e54a 246/* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
299403ae
KW
247bool bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent,
248 bool ignore_bds_parents)
89bd0305 249{
ab613350 250 GLOBAL_STATE_CODE();
fe4f0614 251
6cd5c9d7 252 if (bdrv_parent_drained_poll(bs, ignore_parent, ignore_bds_parents)) {
89bd0305
KW
253 return true;
254 }
255
d73415a3 256 if (qatomic_read(&bs->in_flight)) {
fe4f0614
KW
257 return true;
258 }
259
fe4f0614 260 return false;
89bd0305
KW
261}
262
299403ae 263static bool bdrv_drain_poll_top_level(BlockDriverState *bs,
89bd0305 264 BdrvChild *ignore_parent)
1cc8e54a 265{
299403ae 266 return bdrv_drain_poll(bs, ignore_parent, false);
1cc8e54a
KW
267}
268
299403ae 269static void bdrv_do_drained_begin(BlockDriverState *bs, BdrvChild *parent,
a82a3bd1
KW
270 bool poll);
271static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent);
0152bf40 272
a77fd4bb
FZ
273static void bdrv_co_drain_bh_cb(void *opaque)
274{
275 BdrvCoDrainData *data = opaque;
276 Coroutine *co = data->co;
99723548 277 BlockDriverState *bs = data->bs;
a77fd4bb 278
c8ca33d0 279 if (bs) {
aa1361d5 280 AioContext *ctx = bdrv_get_aio_context(bs);
960d5fb3 281 aio_context_acquire(ctx);
c8ca33d0
KW
282 bdrv_dec_in_flight(bs);
283 if (data->begin) {
a82a3bd1 284 bdrv_do_drained_begin(bs, data->parent, data->poll);
c8ca33d0 285 } else {
e037c09c 286 assert(!data->poll);
a82a3bd1 287 bdrv_do_drained_end(bs, data->parent);
c8ca33d0 288 }
960d5fb3 289 aio_context_release(ctx);
481cad48 290 } else {
c8ca33d0
KW
291 assert(data->begin);
292 bdrv_drain_all_begin();
481cad48
MP
293 }
294
a77fd4bb 295 data->done = true;
1919631e 296 aio_co_wake(co);
a77fd4bb
FZ
297}
298
481cad48 299static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
299403ae 300 bool begin,
6cd5c9d7 301 BdrvChild *parent,
2f65df6e 302 bool poll)
a77fd4bb
FZ
303{
304 BdrvCoDrainData data;
960d5fb3
KW
305 Coroutine *self = qemu_coroutine_self();
306 AioContext *ctx = bdrv_get_aio_context(bs);
307 AioContext *co_ctx = qemu_coroutine_get_aio_context(self);
a77fd4bb
FZ
308
309 /* Calling bdrv_drain() from a BH ensures the current coroutine yields and
c40a2545 310 * other coroutines run if they were queued by aio_co_enter(). */
a77fd4bb
FZ
311
312 assert(qemu_in_coroutine());
313 data = (BdrvCoDrainData) {
960d5fb3 314 .co = self,
a77fd4bb
FZ
315 .bs = bs,
316 .done = false,
481cad48 317 .begin = begin,
0152bf40 318 .parent = parent,
fe4f0614 319 .poll = poll,
a77fd4bb 320 };
8e1da77e 321
c8ca33d0
KW
322 if (bs) {
323 bdrv_inc_in_flight(bs);
324 }
960d5fb3
KW
325
326 /*
327 * Temporarily drop the lock across yield or we would get deadlocks.
328 * bdrv_co_drain_bh_cb() reaquires the lock as needed.
329 *
330 * When we yield below, the lock for the current context will be
331 * released, so if this is actually the lock that protects bs, don't drop
332 * it a second time.
333 */
334 if (ctx != co_ctx) {
335 aio_context_release(ctx);
336 }
ab613350
SH
337 replay_bh_schedule_oneshot_event(qemu_get_aio_context(),
338 bdrv_co_drain_bh_cb, &data);
a77fd4bb
FZ
339
340 qemu_coroutine_yield();
341 /* If we are resumed from some other event (such as an aio completion or a
342 * timer callback), it is a bug in the caller that should be fixed. */
343 assert(data.done);
960d5fb3 344
3202d8e4 345 /* Reacquire the AioContext of bs if we dropped it */
960d5fb3
KW
346 if (ctx != co_ctx) {
347 aio_context_acquire(ctx);
348 }
a77fd4bb
FZ
349}
350
05c272ff
KW
351static void bdrv_do_drained_begin(BlockDriverState *bs, BdrvChild *parent,
352 bool poll)
6820643f 353{
384a48fb 354 IO_OR_GS_CODE();
05c272ff
KW
355
356 if (qemu_in_coroutine()) {
357 bdrv_co_yield_to_drain(bs, true, parent, poll);
358 return;
359 }
d42cf288 360
ab613350
SH
361 GLOBAL_STATE_CODE();
362
60369b86 363 /* Stop things in parent-to-child order */
d73415a3 364 if (qatomic_fetch_inc(&bs->quiesce_counter) == 0) {
a82a3bd1 365 bdrv_parent_drained_begin(bs, parent);
57e05be3
KW
366 if (bs->drv && bs->drv->bdrv_drain_begin) {
367 bs->drv->bdrv_drain_begin(bs);
368 }
c7bc05f7 369 }
d30b8e64 370
fe4f0614
KW
371 /*
372 * Wait for drained requests to finish.
373 *
374 * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
375 * call is needed so things in this AioContext can make progress even
376 * though we don't return to the main AioContext loop - this automatically
377 * includes other nodes in the same AioContext and therefore all child
378 * nodes.
379 */
380 if (poll) {
299403ae 381 BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, parent));
fe4f0614 382 }
6820643f
KW
383}
384
05c272ff
KW
385void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent)
386{
387 bdrv_do_drained_begin(bs, parent, false);
388}
389
e2dbca03
PB
390void coroutine_mixed_fn
391bdrv_drained_begin(BlockDriverState *bs)
0152bf40 392{
384a48fb 393 IO_OR_GS_CODE();
a82a3bd1 394 bdrv_do_drained_begin(bs, NULL, true);
0152bf40
KW
395}
396
e037c09c
HR
397/**
398 * This function does not poll, nor must any of its recursively called
2f65df6e 399 * functions.
e037c09c 400 */
a82a3bd1 401static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent)
6820643f 402{
0f115168
KW
403 int old_quiesce_counter;
404
ab613350
SH
405 IO_OR_GS_CODE();
406
481cad48 407 if (qemu_in_coroutine()) {
a82a3bd1 408 bdrv_co_yield_to_drain(bs, false, parent, false);
481cad48
MP
409 return;
410 }
6820643f 411 assert(bs->quiesce_counter > 0);
ab613350 412 GLOBAL_STATE_CODE();
6820643f 413
60369b86 414 /* Re-enable things in child-to-parent order */
d73415a3 415 old_quiesce_counter = qatomic_fetch_dec(&bs->quiesce_counter);
0f115168 416 if (old_quiesce_counter == 1) {
57e05be3
KW
417 if (bs->drv && bs->drv->bdrv_drain_end) {
418 bs->drv->bdrv_drain_end(bs);
419 }
a82a3bd1 420 bdrv_parent_drained_end(bs, parent);
0f115168 421 }
6820643f
KW
422}
423
0152bf40
KW
424void bdrv_drained_end(BlockDriverState *bs)
425{
384a48fb 426 IO_OR_GS_CODE();
a82a3bd1 427 bdrv_do_drained_end(bs, NULL);
d736f119
KW
428}
429
b6e84c97
PB
430void bdrv_drain(BlockDriverState *bs)
431{
384a48fb 432 IO_OR_GS_CODE();
6820643f
KW
433 bdrv_drained_begin(bs);
434 bdrv_drained_end(bs);
61007b31
SH
435}
436
c13ad59f
KW
437static void bdrv_drain_assert_idle(BlockDriverState *bs)
438{
439 BdrvChild *child, *next;
440
d73415a3 441 assert(qatomic_read(&bs->in_flight) == 0);
c13ad59f
KW
442 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
443 bdrv_drain_assert_idle(child->bs);
444 }
445}
446
0f12264e
KW
447unsigned int bdrv_drain_all_count = 0;
448
449static bool bdrv_drain_all_poll(void)
450{
451 BlockDriverState *bs = NULL;
452 bool result = false;
f791bf7f 453 GLOBAL_STATE_CODE();
0f12264e 454
0f12264e
KW
455 /* bdrv_drain_poll() can't make changes to the graph and we are holding the
456 * main AioContext lock, so iterating bdrv_next_all_states() is safe. */
457 while ((bs = bdrv_next_all_states(bs))) {
458 AioContext *aio_context = bdrv_get_aio_context(bs);
459 aio_context_acquire(aio_context);
299403ae 460 result |= bdrv_drain_poll(bs, NULL, true);
0f12264e
KW
461 aio_context_release(aio_context);
462 }
463
464 return result;
465}
466
61007b31
SH
467/*
468 * Wait for pending requests to complete across all BlockDriverStates
469 *
470 * This function does not flush data to disk, use bdrv_flush_all() for that
471 * after calling this function.
c0778f66
AG
472 *
473 * This pauses all block jobs and disables external clients. It must
474 * be paired with bdrv_drain_all_end().
475 *
476 * NOTE: no new block jobs or BlockDriverStates can be created between
477 * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls.
61007b31 478 */
da0bd744 479void bdrv_drain_all_begin_nopoll(void)
61007b31 480{
0f12264e 481 BlockDriverState *bs = NULL;
f791bf7f 482 GLOBAL_STATE_CODE();
61007b31 483
c8aa7895
PD
484 /*
485 * bdrv queue is managed by record/replay,
486 * waiting for finishing the I/O requests may
487 * be infinite
488 */
489 if (replay_events_enabled()) {
490 return;
491 }
492
0f12264e
KW
493 /* AIO_WAIT_WHILE() with a NULL context can only be called from the main
494 * loop AioContext, so make sure we're in the main context. */
9a7e86c8 495 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
0f12264e
KW
496 assert(bdrv_drain_all_count < INT_MAX);
497 bdrv_drain_all_count++;
9a7e86c8 498
0f12264e
KW
499 /* Quiesce all nodes, without polling in-flight requests yet. The graph
500 * cannot change during this loop. */
501 while ((bs = bdrv_next_all_states(bs))) {
61007b31
SH
502 AioContext *aio_context = bdrv_get_aio_context(bs);
503
504 aio_context_acquire(aio_context);
a82a3bd1 505 bdrv_do_drained_begin(bs, NULL, false);
61007b31
SH
506 aio_context_release(aio_context);
507 }
da0bd744
KW
508}
509
e2dbca03 510void coroutine_mixed_fn bdrv_drain_all_begin(void)
da0bd744
KW
511{
512 BlockDriverState *bs = NULL;
513
514 if (qemu_in_coroutine()) {
515 bdrv_co_yield_to_drain(NULL, true, NULL, true);
516 return;
517 }
518
63945789
PM
519 /*
520 * bdrv queue is managed by record/replay,
521 * waiting for finishing the I/O requests may
522 * be infinite
523 */
524 if (replay_events_enabled()) {
525 return;
526 }
527
da0bd744 528 bdrv_drain_all_begin_nopoll();
61007b31 529
0f12264e 530 /* Now poll the in-flight requests */
263d5e12 531 AIO_WAIT_WHILE_UNLOCKED(NULL, bdrv_drain_all_poll());
0f12264e
KW
532
533 while ((bs = bdrv_next_all_states(bs))) {
c13ad59f 534 bdrv_drain_assert_idle(bs);
61007b31 535 }
c0778f66
AG
536}
537
1a6d3bd2
GK
538void bdrv_drain_all_end_quiesce(BlockDriverState *bs)
539{
b4ad82aa 540 GLOBAL_STATE_CODE();
1a6d3bd2
GK
541
542 g_assert(bs->quiesce_counter > 0);
543 g_assert(!bs->refcnt);
544
545 while (bs->quiesce_counter) {
a82a3bd1 546 bdrv_do_drained_end(bs, NULL);
1a6d3bd2 547 }
1a6d3bd2
GK
548}
549
c0778f66
AG
550void bdrv_drain_all_end(void)
551{
0f12264e 552 BlockDriverState *bs = NULL;
f791bf7f 553 GLOBAL_STATE_CODE();
c0778f66 554
c8aa7895
PD
555 /*
556 * bdrv queue is managed by record/replay,
557 * waiting for finishing the I/O requests may
558 * be endless
559 */
560 if (replay_events_enabled()) {
561 return;
562 }
563
0f12264e 564 while ((bs = bdrv_next_all_states(bs))) {
61007b31
SH
565 AioContext *aio_context = bdrv_get_aio_context(bs);
566
567 aio_context_acquire(aio_context);
a82a3bd1 568 bdrv_do_drained_end(bs, NULL);
61007b31
SH
569 aio_context_release(aio_context);
570 }
0f12264e 571
e037c09c 572 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
0f12264e
KW
573 assert(bdrv_drain_all_count > 0);
574 bdrv_drain_all_count--;
61007b31
SH
575}
576
c0778f66
AG
577void bdrv_drain_all(void)
578{
f791bf7f 579 GLOBAL_STATE_CODE();
c0778f66
AG
580 bdrv_drain_all_begin();
581 bdrv_drain_all_end();
582}
583
61007b31
SH
584/**
585 * Remove an active request from the tracked requests list
586 *
587 * This function should be called when a tracked request is completing.
588 */
f0d43b1e 589static void coroutine_fn tracked_request_end(BdrvTrackedRequest *req)
61007b31
SH
590{
591 if (req->serialising) {
d73415a3 592 qatomic_dec(&req->bs->serialising_in_flight);
61007b31
SH
593 }
594
fa9185fc 595 qemu_mutex_lock(&req->bs->reqs_lock);
61007b31 596 QLIST_REMOVE(req, list);
fa9185fc 597 qemu_mutex_unlock(&req->bs->reqs_lock);
3480ce69
SH
598
599 /*
600 * At this point qemu_co_queue_wait(&req->wait_queue, ...) won't be called
601 * anymore because the request has been removed from the list, so it's safe
602 * to restart the queue outside reqs_lock to minimize the critical section.
603 */
61007b31
SH
604 qemu_co_queue_restart_all(&req->wait_queue);
605}
606
607/**
608 * Add an active request to the tracked requests list
609 */
881a4c55
PB
610static void coroutine_fn tracked_request_begin(BdrvTrackedRequest *req,
611 BlockDriverState *bs,
612 int64_t offset,
613 int64_t bytes,
614 enum BdrvTrackedRequestType type)
61007b31 615{
80247264 616 bdrv_check_request(offset, bytes, &error_abort);
22931a15 617
61007b31
SH
618 *req = (BdrvTrackedRequest){
619 .bs = bs,
620 .offset = offset,
621 .bytes = bytes,
ebde595c 622 .type = type,
61007b31
SH
623 .co = qemu_coroutine_self(),
624 .serialising = false,
625 .overlap_offset = offset,
626 .overlap_bytes = bytes,
627 };
628
629 qemu_co_queue_init(&req->wait_queue);
630
fa9185fc 631 qemu_mutex_lock(&bs->reqs_lock);
61007b31 632 QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
fa9185fc 633 qemu_mutex_unlock(&bs->reqs_lock);
61007b31
SH
634}
635
3ba0e1a0 636static bool tracked_request_overlaps(BdrvTrackedRequest *req,
80247264 637 int64_t offset, int64_t bytes)
3ba0e1a0 638{
80247264
EB
639 bdrv_check_request(offset, bytes, &error_abort);
640
3ba0e1a0
PB
641 /* aaaa bbbb */
642 if (offset >= req->overlap_offset + req->overlap_bytes) {
643 return false;
644 }
645 /* bbbb aaaa */
646 if (req->overlap_offset >= offset + bytes) {
647 return false;
648 }
649 return true;
650}
651
3183937f 652/* Called with self->bs->reqs_lock held */
881a4c55 653static coroutine_fn BdrvTrackedRequest *
3183937f
VSO
654bdrv_find_conflicting_request(BdrvTrackedRequest *self)
655{
656 BdrvTrackedRequest *req;
657
658 QLIST_FOREACH(req, &self->bs->tracked_requests, list) {
659 if (req == self || (!req->serialising && !self->serialising)) {
660 continue;
661 }
662 if (tracked_request_overlaps(req, self->overlap_offset,
663 self->overlap_bytes))
664 {
665 /*
666 * Hitting this means there was a reentrant request, for
667 * example, a block driver issuing nested requests. This must
668 * never happen since it means deadlock.
669 */
670 assert(qemu_coroutine_self() != req->co);
671
672 /*
673 * If the request is already (indirectly) waiting for us, or
674 * will wait for us as soon as it wakes up, then just go on
675 * (instead of producing a deadlock in the former case).
676 */
677 if (!req->waiting_for) {
678 return req;
679 }
680 }
681 }
682
683 return NULL;
684}
685
ec1c8868 686/* Called with self->bs->reqs_lock held */
131498f7 687static void coroutine_fn
ec1c8868 688bdrv_wait_serialising_requests_locked(BdrvTrackedRequest *self)
3ba0e1a0
PB
689{
690 BdrvTrackedRequest *req;
3ba0e1a0 691
3183937f
VSO
692 while ((req = bdrv_find_conflicting_request(self))) {
693 self->waiting_for = req;
ec1c8868 694 qemu_co_queue_wait(&req->wait_queue, &self->bs->reqs_lock);
3183937f 695 self->waiting_for = NULL;
3183937f 696 }
3ba0e1a0
PB
697}
698
8ac5aab2
VSO
699/* Called with req->bs->reqs_lock held */
700static void tracked_request_set_serialising(BdrvTrackedRequest *req,
701 uint64_t align)
61007b31
SH
702{
703 int64_t overlap_offset = req->offset & ~(align - 1);
80247264
EB
704 int64_t overlap_bytes =
705 ROUND_UP(req->offset + req->bytes, align) - overlap_offset;
706
707 bdrv_check_request(req->offset, req->bytes, &error_abort);
61007b31
SH
708
709 if (!req->serialising) {
d73415a3 710 qatomic_inc(&req->bs->serialising_in_flight);
61007b31
SH
711 req->serialising = true;
712 }
713
714 req->overlap_offset = MIN(req->overlap_offset, overlap_offset);
715 req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes);
09d2f948
VSO
716}
717
c28107e9
HR
718/**
719 * Return the tracked request on @bs for the current coroutine, or
720 * NULL if there is none.
721 */
722BdrvTrackedRequest *coroutine_fn bdrv_co_get_self_request(BlockDriverState *bs)
723{
724 BdrvTrackedRequest *req;
725 Coroutine *self = qemu_coroutine_self();
967d7905 726 IO_CODE();
c28107e9
HR
727
728 QLIST_FOREACH(req, &bs->tracked_requests, list) {
729 if (req->co == self) {
730 return req;
731 }
732 }
733
734 return NULL;
735}
736
244483e6 737/**
fc6b211f 738 * Round a region to subcluster (if supported) or cluster boundaries
244483e6 739 */
a00e70c0 740void coroutine_fn GRAPH_RDLOCK
fc6b211f
AD
741bdrv_round_to_subclusters(BlockDriverState *bs, int64_t offset, int64_t bytes,
742 int64_t *align_offset, int64_t *align_bytes)
244483e6
KW
743{
744 BlockDriverInfo bdi;
384a48fb 745 IO_CODE();
fc6b211f
AD
746 if (bdrv_co_get_info(bs, &bdi) < 0 || bdi.subcluster_size == 0) {
747 *align_offset = offset;
748 *align_bytes = bytes;
244483e6 749 } else {
fc6b211f
AD
750 int64_t c = bdi.subcluster_size;
751 *align_offset = QEMU_ALIGN_DOWN(offset, c);
752 *align_bytes = QEMU_ALIGN_UP(offset - *align_offset + bytes, c);
244483e6
KW
753 }
754}
755
a00e70c0 756static int coroutine_fn GRAPH_RDLOCK bdrv_get_cluster_size(BlockDriverState *bs)
61007b31
SH
757{
758 BlockDriverInfo bdi;
759 int ret;
760
3d47eb0a 761 ret = bdrv_co_get_info(bs, &bdi);
61007b31 762 if (ret < 0 || bdi.cluster_size == 0) {
a5b8dd2c 763 return bs->bl.request_alignment;
61007b31
SH
764 } else {
765 return bdi.cluster_size;
766 }
767}
768
99723548
PB
769void bdrv_inc_in_flight(BlockDriverState *bs)
770{
967d7905 771 IO_CODE();
d73415a3 772 qatomic_inc(&bs->in_flight);
99723548
PB
773}
774
c9d1a561
PB
775void bdrv_wakeup(BlockDriverState *bs)
776{
967d7905 777 IO_CODE();
cfe29d82 778 aio_wait_kick();
c9d1a561
PB
779}
780
99723548
PB
781void bdrv_dec_in_flight(BlockDriverState *bs)
782{
967d7905 783 IO_CODE();
d73415a3 784 qatomic_dec(&bs->in_flight);
c9d1a561 785 bdrv_wakeup(bs);
99723548
PB
786}
787
131498f7
DL
788static void coroutine_fn
789bdrv_wait_serialising_requests(BdrvTrackedRequest *self)
61007b31
SH
790{
791 BlockDriverState *bs = self->bs;
61007b31 792
d73415a3 793 if (!qatomic_read(&bs->serialising_in_flight)) {
131498f7 794 return;
61007b31
SH
795 }
796
fa9185fc 797 qemu_mutex_lock(&bs->reqs_lock);
131498f7 798 bdrv_wait_serialising_requests_locked(self);
fa9185fc 799 qemu_mutex_unlock(&bs->reqs_lock);
61007b31
SH
800}
801
131498f7 802void coroutine_fn bdrv_make_request_serialising(BdrvTrackedRequest *req,
8ac5aab2
VSO
803 uint64_t align)
804{
967d7905 805 IO_CODE();
8ac5aab2 806
fa9185fc 807 qemu_mutex_lock(&req->bs->reqs_lock);
8ac5aab2
VSO
808
809 tracked_request_set_serialising(req, align);
131498f7 810 bdrv_wait_serialising_requests_locked(req);
8ac5aab2 811
fa9185fc 812 qemu_mutex_unlock(&req->bs->reqs_lock);
8ac5aab2
VSO
813}
814
558902cc
VSO
815int bdrv_check_qiov_request(int64_t offset, int64_t bytes,
816 QEMUIOVector *qiov, size_t qiov_offset,
817 Error **errp)
61007b31 818{
63f4ad11
VSO
819 /*
820 * Check generic offset/bytes correctness
821 */
822
69b55e03
VSO
823 if (offset < 0) {
824 error_setg(errp, "offset is negative: %" PRIi64, offset);
825 return -EIO;
826 }
827
828 if (bytes < 0) {
829 error_setg(errp, "bytes is negative: %" PRIi64, bytes);
61007b31
SH
830 return -EIO;
831 }
832
8b117001 833 if (bytes > BDRV_MAX_LENGTH) {
69b55e03
VSO
834 error_setg(errp, "bytes(%" PRIi64 ") exceeds maximum(%" PRIi64 ")",
835 bytes, BDRV_MAX_LENGTH);
836 return -EIO;
837 }
838
839 if (offset > BDRV_MAX_LENGTH) {
840 error_setg(errp, "offset(%" PRIi64 ") exceeds maximum(%" PRIi64 ")",
841 offset, BDRV_MAX_LENGTH);
8b117001
VSO
842 return -EIO;
843 }
844
845 if (offset > BDRV_MAX_LENGTH - bytes) {
69b55e03
VSO
846 error_setg(errp, "sum of offset(%" PRIi64 ") and bytes(%" PRIi64 ") "
847 "exceeds maximum(%" PRIi64 ")", offset, bytes,
848 BDRV_MAX_LENGTH);
8b117001
VSO
849 return -EIO;
850 }
851
63f4ad11
VSO
852 if (!qiov) {
853 return 0;
854 }
855
856 /*
857 * Check qiov and qiov_offset
858 */
859
860 if (qiov_offset > qiov->size) {
861 error_setg(errp, "qiov_offset(%zu) overflow io vector size(%zu)",
862 qiov_offset, qiov->size);
863 return -EIO;
864 }
865
866 if (bytes > qiov->size - qiov_offset) {
867 error_setg(errp, "bytes(%" PRIi64 ") + qiov_offset(%zu) overflow io "
868 "vector size(%zu)", bytes, qiov_offset, qiov->size);
869 return -EIO;
870 }
871
8b117001
VSO
872 return 0;
873}
874
63f4ad11
VSO
875int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp)
876{
877 return bdrv_check_qiov_request(offset, bytes, NULL, 0, errp);
878}
879
880static int bdrv_check_request32(int64_t offset, int64_t bytes,
881 QEMUIOVector *qiov, size_t qiov_offset)
8b117001 882{
63f4ad11 883 int ret = bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, NULL);
8b117001
VSO
884 if (ret < 0) {
885 return ret;
886 }
887
888 if (bytes > BDRV_REQUEST_MAX_BYTES) {
61007b31
SH
889 return -EIO;
890 }
891
892 return 0;
893}
894
61007b31 895/*
74021bc4 896 * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
61007b31
SH
897 * The operation is sped up by checking the block status and only writing
898 * zeroes to the device if they currently do not return zeroes. Optional
74021bc4 899 * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
465fe887 900 * BDRV_REQ_FUA).
61007b31 901 *
f4649069 902 * Returns < 0 on error, 0 on success. For error codes see bdrv_pwrite().
61007b31 903 */
720ff280 904int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
61007b31 905{
237d78f8
EB
906 int ret;
907 int64_t target_size, bytes, offset = 0;
720ff280 908 BlockDriverState *bs = child->bs;
384a48fb 909 IO_CODE();
61007b31 910
7286d610
EB
911 target_size = bdrv_getlength(bs);
912 if (target_size < 0) {
913 return target_size;
61007b31
SH
914 }
915
916 for (;;) {
7286d610
EB
917 bytes = MIN(target_size - offset, BDRV_REQUEST_MAX_BYTES);
918 if (bytes <= 0) {
61007b31
SH
919 return 0;
920 }
237d78f8 921 ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
61007b31 922 if (ret < 0) {
61007b31
SH
923 return ret;
924 }
925 if (ret & BDRV_BLOCK_ZERO) {
237d78f8 926 offset += bytes;
61007b31
SH
927 continue;
928 }
237d78f8 929 ret = bdrv_pwrite_zeroes(child, offset, bytes, flags);
61007b31 930 if (ret < 0) {
61007b31
SH
931 return ret;
932 }
237d78f8 933 offset += bytes;
61007b31
SH
934 }
935}
936
61007b31
SH
937/*
938 * Writes to the file and ensures that no writes are reordered across this
939 * request (acts as a barrier)
940 *
941 * Returns 0 on success, -errno in error cases.
942 */
e97190a4
AF
943int coroutine_fn bdrv_co_pwrite_sync(BdrvChild *child, int64_t offset,
944 int64_t bytes, const void *buf,
945 BdrvRequestFlags flags)
61007b31
SH
946{
947 int ret;
384a48fb 948 IO_CODE();
b24a4c41 949 assert_bdrv_graph_readable();
88095349 950
e97190a4 951 ret = bdrv_co_pwrite(child, offset, bytes, buf, flags);
61007b31
SH
952 if (ret < 0) {
953 return ret;
954 }
955
e97190a4 956 ret = bdrv_co_flush(child->bs);
855a6a93
KW
957 if (ret < 0) {
958 return ret;
61007b31
SH
959 }
960
961 return 0;
962}
963
08844473
KW
964typedef struct CoroutineIOCompletion {
965 Coroutine *coroutine;
966 int ret;
967} CoroutineIOCompletion;
968
969static void bdrv_co_io_em_complete(void *opaque, int ret)
970{
971 CoroutineIOCompletion *co = opaque;
972
973 co->ret = ret;
b9e413dd 974 aio_co_wake(co->coroutine);
08844473
KW
975}
976
7b1fb72e
KW
977static int coroutine_fn GRAPH_RDLOCK
978bdrv_driver_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
979 QEMUIOVector *qiov, size_t qiov_offset, int flags)
166fe960
KW
980{
981 BlockDriver *drv = bs->drv;
3fb06697
KW
982 int64_t sector_num;
983 unsigned int nb_sectors;
ac850bf0
VSO
984 QEMUIOVector local_qiov;
985 int ret;
b9b10c35 986 assert_bdrv_graph_readable();
3fb06697 987
17abcbee 988 bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
e8b65355 989 assert(!(flags & ~bs->supported_read_flags));
fa166538 990
d470ad42
HR
991 if (!drv) {
992 return -ENOMEDIUM;
993 }
994
ac850bf0
VSO
995 if (drv->bdrv_co_preadv_part) {
996 return drv->bdrv_co_preadv_part(bs, offset, bytes, qiov, qiov_offset,
997 flags);
998 }
999
1000 if (qiov_offset > 0 || bytes != qiov->size) {
1001 qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes);
1002 qiov = &local_qiov;
1003 }
1004
3fb06697 1005 if (drv->bdrv_co_preadv) {
ac850bf0
VSO
1006 ret = drv->bdrv_co_preadv(bs, offset, bytes, qiov, flags);
1007 goto out;
3fb06697
KW
1008 }
1009
edfab6a0 1010 if (drv->bdrv_aio_preadv) {
08844473
KW
1011 BlockAIOCB *acb;
1012 CoroutineIOCompletion co = {
1013 .coroutine = qemu_coroutine_self(),
1014 };
1015
edfab6a0
EB
1016 acb = drv->bdrv_aio_preadv(bs, offset, bytes, qiov, flags,
1017 bdrv_co_io_em_complete, &co);
08844473 1018 if (acb == NULL) {
ac850bf0
VSO
1019 ret = -EIO;
1020 goto out;
08844473
KW
1021 } else {
1022 qemu_coroutine_yield();
ac850bf0
VSO
1023 ret = co.ret;
1024 goto out;
08844473
KW
1025 }
1026 }
edfab6a0
EB
1027
1028 sector_num = offset >> BDRV_SECTOR_BITS;
1029 nb_sectors = bytes >> BDRV_SECTOR_BITS;
1030
1bbbf32d
NS
1031 assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
1032 assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
41ae31e3 1033 assert(bytes <= BDRV_REQUEST_MAX_BYTES);
edfab6a0
EB
1034 assert(drv->bdrv_co_readv);
1035
ac850bf0
VSO
1036 ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1037
1038out:
1039 if (qiov == &local_qiov) {
1040 qemu_iovec_destroy(&local_qiov);
1041 }
1042
1043 return ret;
166fe960
KW
1044}
1045
7b1fb72e
KW
1046static int coroutine_fn GRAPH_RDLOCK
1047bdrv_driver_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
1048 QEMUIOVector *qiov, size_t qiov_offset,
1049 BdrvRequestFlags flags)
78a07294
KW
1050{
1051 BlockDriver *drv = bs->drv;
e8b65355 1052 bool emulate_fua = false;
3fb06697
KW
1053 int64_t sector_num;
1054 unsigned int nb_sectors;
ac850bf0 1055 QEMUIOVector local_qiov;
78a07294 1056 int ret;
b9b10c35 1057 assert_bdrv_graph_readable();
78a07294 1058
17abcbee 1059 bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
fa166538 1060
d470ad42
HR
1061 if (!drv) {
1062 return -ENOMEDIUM;
1063 }
1064
e8b65355
SH
1065 if ((flags & BDRV_REQ_FUA) &&
1066 (~bs->supported_write_flags & BDRV_REQ_FUA)) {
1067 flags &= ~BDRV_REQ_FUA;
1068 emulate_fua = true;
1069 }
1070
1071 flags &= bs->supported_write_flags;
1072
ac850bf0
VSO
1073 if (drv->bdrv_co_pwritev_part) {
1074 ret = drv->bdrv_co_pwritev_part(bs, offset, bytes, qiov, qiov_offset,
e8b65355 1075 flags);
ac850bf0
VSO
1076 goto emulate_flags;
1077 }
1078
1079 if (qiov_offset > 0 || bytes != qiov->size) {
1080 qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes);
1081 qiov = &local_qiov;
1082 }
1083
3fb06697 1084 if (drv->bdrv_co_pwritev) {
e8b65355 1085 ret = drv->bdrv_co_pwritev(bs, offset, bytes, qiov, flags);
3fb06697
KW
1086 goto emulate_flags;
1087 }
1088
edfab6a0 1089 if (drv->bdrv_aio_pwritev) {
08844473
KW
1090 BlockAIOCB *acb;
1091 CoroutineIOCompletion co = {
1092 .coroutine = qemu_coroutine_self(),
1093 };
1094
e8b65355 1095 acb = drv->bdrv_aio_pwritev(bs, offset, bytes, qiov, flags,
edfab6a0 1096 bdrv_co_io_em_complete, &co);
08844473 1097 if (acb == NULL) {
3fb06697 1098 ret = -EIO;
08844473
KW
1099 } else {
1100 qemu_coroutine_yield();
3fb06697 1101 ret = co.ret;
08844473 1102 }
edfab6a0
EB
1103 goto emulate_flags;
1104 }
1105
1106 sector_num = offset >> BDRV_SECTOR_BITS;
1107 nb_sectors = bytes >> BDRV_SECTOR_BITS;
1108
1bbbf32d
NS
1109 assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
1110 assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
41ae31e3 1111 assert(bytes <= BDRV_REQUEST_MAX_BYTES);
edfab6a0 1112
e18a58b4 1113 assert(drv->bdrv_co_writev);
e8b65355 1114 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov, flags);
78a07294 1115
3fb06697 1116emulate_flags:
e8b65355 1117 if (ret == 0 && emulate_fua) {
78a07294
KW
1118 ret = bdrv_co_flush(bs);
1119 }
1120
ac850bf0
VSO
1121 if (qiov == &local_qiov) {
1122 qemu_iovec_destroy(&local_qiov);
1123 }
1124
78a07294
KW
1125 return ret;
1126}
1127
7b1fb72e 1128static int coroutine_fn GRAPH_RDLOCK
17abcbee
VSO
1129bdrv_driver_pwritev_compressed(BlockDriverState *bs, int64_t offset,
1130 int64_t bytes, QEMUIOVector *qiov,
ac850bf0 1131 size_t qiov_offset)
29a298af
PB
1132{
1133 BlockDriver *drv = bs->drv;
ac850bf0
VSO
1134 QEMUIOVector local_qiov;
1135 int ret;
b9b10c35 1136 assert_bdrv_graph_readable();
29a298af 1137
17abcbee
VSO
1138 bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
1139
d470ad42
HR
1140 if (!drv) {
1141 return -ENOMEDIUM;
1142 }
1143
ac850bf0 1144 if (!block_driver_can_compress(drv)) {
29a298af
PB
1145 return -ENOTSUP;
1146 }
1147
ac850bf0
VSO
1148 if (drv->bdrv_co_pwritev_compressed_part) {
1149 return drv->bdrv_co_pwritev_compressed_part(bs, offset, bytes,
1150 qiov, qiov_offset);
1151 }
1152
1153 if (qiov_offset == 0) {
1154 return drv->bdrv_co_pwritev_compressed(bs, offset, bytes, qiov);
1155 }
1156
1157 qemu_iovec_init_slice(&local_qiov, qiov, qiov_offset, bytes);
1158 ret = drv->bdrv_co_pwritev_compressed(bs, offset, bytes, &local_qiov);
1159 qemu_iovec_destroy(&local_qiov);
1160
1161 return ret;
29a298af
PB
1162}
1163
7b1fb72e
KW
1164static int coroutine_fn GRAPH_RDLOCK
1165bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t offset, int64_t bytes,
1166 QEMUIOVector *qiov, size_t qiov_offset, int flags)
61007b31 1167{
85c97ca7
KW
1168 BlockDriverState *bs = child->bs;
1169
61007b31
SH
1170 /* Perform I/O through a temporary buffer so that users who scribble over
1171 * their read buffer while the operation is in progress do not end up
1172 * modifying the image file. This is critical for zero-copy guest I/O
1173 * where anything might happen inside guest memory.
1174 */
2275cc90 1175 void *bounce_buffer = NULL;
61007b31
SH
1176
1177 BlockDriver *drv = bs->drv;
fc6b211f
AD
1178 int64_t align_offset;
1179 int64_t align_bytes;
9df5afbd 1180 int64_t skip_bytes;
61007b31 1181 int ret;
cb2e2878
EB
1182 int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer,
1183 BDRV_REQUEST_MAX_BYTES);
9df5afbd 1184 int64_t progress = 0;
8644476e 1185 bool skip_write;
61007b31 1186
9df5afbd
VSO
1187 bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
1188
d470ad42
HR
1189 if (!drv) {
1190 return -ENOMEDIUM;
1191 }
1192
8644476e
HR
1193 /*
1194 * Do not write anything when the BDS is inactive. That is not
1195 * allowed, and it would not help.
1196 */
1197 skip_write = (bs->open_flags & BDRV_O_INACTIVE);
1198
1bf03e66
KW
1199 /* FIXME We cannot require callers to have write permissions when all they
1200 * are doing is a read request. If we did things right, write permissions
1201 * would be obtained anyway, but internally by the copy-on-read code. As
765d9df9 1202 * long as it is implemented here rather than in a separate filter driver,
1bf03e66
KW
1203 * the copy-on-read code doesn't have its own BdrvChild, however, for which
1204 * it could request permissions. Therefore we have to bypass the permission
1205 * system for the moment. */
1206 // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
afa4b293 1207
61007b31 1208 /* Cover entire cluster so no additional backing file I/O is required when
cb2e2878
EB
1209 * allocating cluster in the image file. Note that this value may exceed
1210 * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which
1211 * is one reason we loop rather than doing it all at once.
61007b31 1212 */
fc6b211f
AD
1213 bdrv_round_to_subclusters(bs, offset, bytes, &align_offset, &align_bytes);
1214 skip_bytes = offset - align_offset;
61007b31 1215
244483e6 1216 trace_bdrv_co_do_copy_on_readv(bs, offset, bytes,
fc6b211f 1217 align_offset, align_bytes);
61007b31 1218
fc6b211f 1219 while (align_bytes) {
cb2e2878 1220 int64_t pnum;
61007b31 1221
8644476e
HR
1222 if (skip_write) {
1223 ret = 1; /* "already allocated", so nothing will be copied */
fc6b211f 1224 pnum = MIN(align_bytes, max_transfer);
8644476e 1225 } else {
cc323997
PB
1226 ret = bdrv_co_is_allocated(bs, align_offset,
1227 MIN(align_bytes, max_transfer), &pnum);
8644476e
HR
1228 if (ret < 0) {
1229 /*
1230 * Safe to treat errors in querying allocation as if
1231 * unallocated; we'll probably fail again soon on the
1232 * read, but at least that will set a decent errno.
1233 */
fc6b211f 1234 pnum = MIN(align_bytes, max_transfer);
8644476e 1235 }
61007b31 1236
8644476e
HR
1237 /* Stop at EOF if the image ends in the middle of the cluster */
1238 if (ret == 0 && pnum == 0) {
1239 assert(progress >= bytes);
1240 break;
1241 }
b0ddcbbb 1242
8644476e
HR
1243 assert(skip_bytes < pnum);
1244 }
61007b31 1245
cb2e2878 1246 if (ret <= 0) {
1143ec5e
VSO
1247 QEMUIOVector local_qiov;
1248
cb2e2878 1249 /* Must copy-on-read; use the bounce buffer */
0d93ed08 1250 pnum = MIN(pnum, MAX_BOUNCE_BUFFER);
2275cc90 1251 if (!bounce_buffer) {
fc6b211f 1252 int64_t max_we_need = MAX(pnum, align_bytes - pnum);
2275cc90
VSO
1253 int64_t max_allowed = MIN(max_transfer, MAX_BOUNCE_BUFFER);
1254 int64_t bounce_buffer_len = MIN(max_we_need, max_allowed);
1255
1256 bounce_buffer = qemu_try_blockalign(bs, bounce_buffer_len);
1257 if (!bounce_buffer) {
1258 ret = -ENOMEM;
1259 goto err;
1260 }
1261 }
0d93ed08 1262 qemu_iovec_init_buf(&local_qiov, bounce_buffer, pnum);
61007b31 1263
fc6b211f 1264 ret = bdrv_driver_preadv(bs, align_offset, pnum,
ac850bf0 1265 &local_qiov, 0, 0);
cb2e2878
EB
1266 if (ret < 0) {
1267 goto err;
1268 }
1269
c834dc05 1270 bdrv_co_debug_event(bs, BLKDBG_COR_WRITE);
cb2e2878
EB
1271 if (drv->bdrv_co_pwrite_zeroes &&
1272 buffer_is_zero(bounce_buffer, pnum)) {
1273 /* FIXME: Should we (perhaps conditionally) be setting
1274 * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy
1275 * that still correctly reads as zero? */
fc6b211f 1276 ret = bdrv_co_do_pwrite_zeroes(bs, align_offset, pnum,
7adcf59f 1277 BDRV_REQ_WRITE_UNCHANGED);
cb2e2878
EB
1278 } else {
1279 /* This does not change the data on the disk, it is not
1280 * necessary to flush even in cache=writethrough mode.
1281 */
fc6b211f 1282 ret = bdrv_driver_pwritev(bs, align_offset, pnum,
ac850bf0 1283 &local_qiov, 0,
7adcf59f 1284 BDRV_REQ_WRITE_UNCHANGED);
cb2e2878
EB
1285 }
1286
1287 if (ret < 0) {
1288 /* It might be okay to ignore write errors for guest
1289 * requests. If this is a deliberate copy-on-read
1290 * then we don't want to ignore the error. Simply
1291 * report it in all cases.
1292 */
1293 goto err;
1294 }
1295
3299e5ec 1296 if (!(flags & BDRV_REQ_PREFETCH)) {
1143ec5e
VSO
1297 qemu_iovec_from_buf(qiov, qiov_offset + progress,
1298 bounce_buffer + skip_bytes,
4ab78b19 1299 MIN(pnum - skip_bytes, bytes - progress));
3299e5ec
VSO
1300 }
1301 } else if (!(flags & BDRV_REQ_PREFETCH)) {
cb2e2878 1302 /* Read directly into the destination */
1143ec5e
VSO
1303 ret = bdrv_driver_preadv(bs, offset + progress,
1304 MIN(pnum - skip_bytes, bytes - progress),
1305 qiov, qiov_offset + progress, 0);
cb2e2878
EB
1306 if (ret < 0) {
1307 goto err;
1308 }
1309 }
1310
fc6b211f
AD
1311 align_offset += pnum;
1312 align_bytes -= pnum;
cb2e2878
EB
1313 progress += pnum - skip_bytes;
1314 skip_bytes = 0;
1315 }
1316 ret = 0;
61007b31
SH
1317
1318err:
1319 qemu_vfree(bounce_buffer);
1320 return ret;
1321}
1322
1323/*
1324 * Forwards an already correctly aligned request to the BlockDriver. This
1a62d0ac
EB
1325 * handles copy on read, zeroing after EOF, and fragmentation of large
1326 * reads; any other features must be implemented by the caller.
61007b31 1327 */
7b1fb72e
KW
1328static int coroutine_fn GRAPH_RDLOCK
1329bdrv_aligned_preadv(BdrvChild *child, BdrvTrackedRequest *req,
1330 int64_t offset, int64_t bytes, int64_t align,
1331 QEMUIOVector *qiov, size_t qiov_offset, int flags)
61007b31 1332{
85c97ca7 1333 BlockDriverState *bs = child->bs;
c9d20029 1334 int64_t total_bytes, max_bytes;
1a62d0ac 1335 int ret = 0;
8b0c5d76 1336 int64_t bytes_remaining = bytes;
1a62d0ac 1337 int max_transfer;
61007b31 1338
8b0c5d76 1339 bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
49c07526
KW
1340 assert(is_power_of_2(align));
1341 assert((offset & (align - 1)) == 0);
1342 assert((bytes & (align - 1)) == 0);
abb06c5a 1343 assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1a62d0ac
EB
1344 max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
1345 align);
a604fa2b 1346
e8b65355
SH
1347 /*
1348 * TODO: We would need a per-BDS .supported_read_flags and
a604fa2b
EB
1349 * potential fallback support, if we ever implement any read flags
1350 * to pass through to drivers. For now, there aren't any
e8b65355
SH
1351 * passthrough flags except the BDRV_REQ_REGISTERED_BUF optimization hint.
1352 */
1353 assert(!(flags & ~(BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH |
1354 BDRV_REQ_REGISTERED_BUF)));
61007b31
SH
1355
1356 /* Handle Copy on Read and associated serialisation */
1357 if (flags & BDRV_REQ_COPY_ON_READ) {
1358 /* If we touch the same cluster it counts as an overlap. This
1359 * guarantees that allocating writes will be serialized and not race
1360 * with each other for the same cluster. For example, in copy-on-read
1361 * it ensures that the CoR read and write operations are atomic and
1362 * guest writes cannot interleave between them. */
8ac5aab2 1363 bdrv_make_request_serialising(req, bdrv_get_cluster_size(bs));
18fbd0de
PB
1364 } else {
1365 bdrv_wait_serialising_requests(req);
61007b31
SH
1366 }
1367
61007b31 1368 if (flags & BDRV_REQ_COPY_ON_READ) {
d6a644bb 1369 int64_t pnum;
61007b31 1370
897dd0ec
AS
1371 /* The flag BDRV_REQ_COPY_ON_READ has reached its addressee */
1372 flags &= ~BDRV_REQ_COPY_ON_READ;
1373
cc323997 1374 ret = bdrv_co_is_allocated(bs, offset, bytes, &pnum);
61007b31
SH
1375 if (ret < 0) {
1376 goto out;
1377 }
1378
88e63df2 1379 if (!ret || pnum != bytes) {
65cd4424
VSO
1380 ret = bdrv_co_do_copy_on_readv(child, offset, bytes,
1381 qiov, qiov_offset, flags);
3299e5ec
VSO
1382 goto out;
1383 } else if (flags & BDRV_REQ_PREFETCH) {
61007b31
SH
1384 goto out;
1385 }
1386 }
1387
1a62d0ac 1388 /* Forward the request to the BlockDriver, possibly fragmenting it */
0af02bd1 1389 total_bytes = bdrv_co_getlength(bs);
c9d20029
KW
1390 if (total_bytes < 0) {
1391 ret = total_bytes;
1392 goto out;
1393 }
61007b31 1394
e8b65355 1395 assert(!(flags & ~(bs->supported_read_flags | BDRV_REQ_REGISTERED_BUF)));
897dd0ec 1396
c9d20029 1397 max_bytes = ROUND_UP(MAX(0, total_bytes - offset), align);
1a62d0ac 1398 if (bytes <= max_bytes && bytes <= max_transfer) {
897dd0ec 1399 ret = bdrv_driver_preadv(bs, offset, bytes, qiov, qiov_offset, flags);
1a62d0ac
EB
1400 goto out;
1401 }
61007b31 1402
1a62d0ac 1403 while (bytes_remaining) {
8b0c5d76 1404 int64_t num;
61007b31 1405
1a62d0ac 1406 if (max_bytes) {
1a62d0ac
EB
1407 num = MIN(bytes_remaining, MIN(max_bytes, max_transfer));
1408 assert(num);
61007b31 1409
1a62d0ac 1410 ret = bdrv_driver_preadv(bs, offset + bytes - bytes_remaining,
134b7dec 1411 num, qiov,
897dd0ec
AS
1412 qiov_offset + bytes - bytes_remaining,
1413 flags);
1a62d0ac 1414 max_bytes -= num;
1a62d0ac
EB
1415 } else {
1416 num = bytes_remaining;
134b7dec
HR
1417 ret = qemu_iovec_memset(qiov, qiov_offset + bytes - bytes_remaining,
1418 0, bytes_remaining);
1a62d0ac
EB
1419 }
1420 if (ret < 0) {
1421 goto out;
1422 }
1423 bytes_remaining -= num;
61007b31
SH
1424 }
1425
1426out:
1a62d0ac 1427 return ret < 0 ? ret : 0;
61007b31
SH
1428}
1429
61007b31 1430/*
7a3f542f
VSO
1431 * Request padding
1432 *
1433 * |<---- align ----->| |<----- align ---->|
1434 * |<- head ->|<------------- bytes ------------->|<-- tail -->|
1435 * | | | | | |
1436 * -*----------$-------*-------- ... --------*-----$------------*---
1437 * | | | | | |
1438 * | offset | | end |
1439 * ALIGN_DOWN(offset) ALIGN_UP(offset) ALIGN_DOWN(end) ALIGN_UP(end)
1440 * [buf ... ) [tail_buf )
1441 *
1442 * @buf is an aligned allocation needed to store @head and @tail paddings. @head
1443 * is placed at the beginning of @buf and @tail at the @end.
1444 *
1445 * @tail_buf is a pointer to sub-buffer, corresponding to align-sized chunk
1446 * around tail, if tail exists.
1447 *
1448 * @merge_reads is true for small requests,
1449 * if @buf_len == @head + bytes + @tail. In this case it is possible that both
1450 * head and tail exist but @buf_len == align and @tail_buf == @buf.
18743311
HC
1451 *
1452 * @write is true for write requests, false for read requests.
1453 *
1454 * If padding makes the vector too long (exceeding IOV_MAX), then we need to
1455 * merge existing vector elements into a single one. @collapse_bounce_buf acts
1456 * as the bounce buffer in such cases. @pre_collapse_qiov has the pre-collapse
1457 * I/O vector elements so for read requests, the data can be copied back after
1458 * the read is done.
7a3f542f
VSO
1459 */
1460typedef struct BdrvRequestPadding {
1461 uint8_t *buf;
1462 size_t buf_len;
1463 uint8_t *tail_buf;
1464 size_t head;
1465 size_t tail;
1466 bool merge_reads;
18743311 1467 bool write;
7a3f542f 1468 QEMUIOVector local_qiov;
18743311
HC
1469
1470 uint8_t *collapse_bounce_buf;
1471 size_t collapse_len;
1472 QEMUIOVector pre_collapse_qiov;
7a3f542f
VSO
1473} BdrvRequestPadding;
1474
1475static bool bdrv_init_padding(BlockDriverState *bs,
1476 int64_t offset, int64_t bytes,
18743311 1477 bool write,
7a3f542f
VSO
1478 BdrvRequestPadding *pad)
1479{
a56ed80c
VSO
1480 int64_t align = bs->bl.request_alignment;
1481 int64_t sum;
1482
1483 bdrv_check_request(offset, bytes, &error_abort);
1484 assert(align <= INT_MAX); /* documented in block/block_int.h */
1485 assert(align <= SIZE_MAX / 2); /* so we can allocate the buffer */
7a3f542f
VSO
1486
1487 memset(pad, 0, sizeof(*pad));
1488
1489 pad->head = offset & (align - 1);
1490 pad->tail = ((offset + bytes) & (align - 1));
1491 if (pad->tail) {
1492 pad->tail = align - pad->tail;
1493 }
1494
ac9d00bf 1495 if (!pad->head && !pad->tail) {
7a3f542f
VSO
1496 return false;
1497 }
1498
ac9d00bf
VSO
1499 assert(bytes); /* Nothing good in aligning zero-length requests */
1500
7a3f542f
VSO
1501 sum = pad->head + bytes + pad->tail;
1502 pad->buf_len = (sum > align && pad->head && pad->tail) ? 2 * align : align;
1503 pad->buf = qemu_blockalign(bs, pad->buf_len);
1504 pad->merge_reads = sum == pad->buf_len;
1505 if (pad->tail) {
1506 pad->tail_buf = pad->buf + pad->buf_len - align;
1507 }
1508
18743311
HC
1509 pad->write = write;
1510
7a3f542f
VSO
1511 return true;
1512}
1513
7b1fb72e
KW
1514static int coroutine_fn GRAPH_RDLOCK
1515bdrv_padding_rmw_read(BdrvChild *child, BdrvTrackedRequest *req,
1516 BdrvRequestPadding *pad, bool zero_middle)
7a3f542f
VSO
1517{
1518 QEMUIOVector local_qiov;
1519 BlockDriverState *bs = child->bs;
1520 uint64_t align = bs->bl.request_alignment;
1521 int ret;
1522
1523 assert(req->serialising && pad->buf);
1524
1525 if (pad->head || pad->merge_reads) {
8b0c5d76 1526 int64_t bytes = pad->merge_reads ? pad->buf_len : align;
7a3f542f
VSO
1527
1528 qemu_iovec_init_buf(&local_qiov, pad->buf, bytes);
1529
1530 if (pad->head) {
c834dc05 1531 bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD);
7a3f542f
VSO
1532 }
1533 if (pad->merge_reads && pad->tail) {
c834dc05 1534 bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
7a3f542f
VSO
1535 }
1536 ret = bdrv_aligned_preadv(child, req, req->overlap_offset, bytes,
65cd4424 1537 align, &local_qiov, 0, 0);
7a3f542f
VSO
1538 if (ret < 0) {
1539 return ret;
1540 }
1541 if (pad->head) {
c834dc05 1542 bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
7a3f542f
VSO
1543 }
1544 if (pad->merge_reads && pad->tail) {
c834dc05 1545 bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
7a3f542f
VSO
1546 }
1547
1548 if (pad->merge_reads) {
1549 goto zero_mem;
1550 }
1551 }
1552
1553 if (pad->tail) {
1554 qemu_iovec_init_buf(&local_qiov, pad->tail_buf, align);
1555
c834dc05 1556 bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL);
7a3f542f
VSO
1557 ret = bdrv_aligned_preadv(
1558 child, req,
1559 req->overlap_offset + req->overlap_bytes - align,
65cd4424 1560 align, align, &local_qiov, 0, 0);
7a3f542f
VSO
1561 if (ret < 0) {
1562 return ret;
1563 }
c834dc05 1564 bdrv_co_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
7a3f542f
VSO
1565 }
1566
1567zero_mem:
1568 if (zero_middle) {
1569 memset(pad->buf + pad->head, 0, pad->buf_len - pad->head - pad->tail);
1570 }
1571
1572 return 0;
1573}
1574
18743311
HC
1575/**
1576 * Free *pad's associated buffers, and perform any necessary finalization steps.
1577 */
1578static void bdrv_padding_finalize(BdrvRequestPadding *pad)
7a3f542f 1579{
18743311
HC
1580 if (pad->collapse_bounce_buf) {
1581 if (!pad->write) {
1582 /*
1583 * If padding required elements in the vector to be collapsed into a
1584 * bounce buffer, copy the bounce buffer content back
1585 */
1586 qemu_iovec_from_buf(&pad->pre_collapse_qiov, 0,
1587 pad->collapse_bounce_buf, pad->collapse_len);
1588 }
1589 qemu_vfree(pad->collapse_bounce_buf);
1590 qemu_iovec_destroy(&pad->pre_collapse_qiov);
1591 }
7a3f542f
VSO
1592 if (pad->buf) {
1593 qemu_vfree(pad->buf);
1594 qemu_iovec_destroy(&pad->local_qiov);
1595 }
98ca4549 1596 memset(pad, 0, sizeof(*pad));
7a3f542f
VSO
1597}
1598
18743311
HC
1599/*
1600 * Create pad->local_qiov by wrapping @iov in the padding head and tail, while
1601 * ensuring that the resulting vector will not exceed IOV_MAX elements.
1602 *
1603 * To ensure this, when necessary, the first two or three elements of @iov are
1604 * merged into pad->collapse_bounce_buf and replaced by a reference to that
1605 * bounce buffer in pad->local_qiov.
1606 *
1607 * After performing a read request, the data from the bounce buffer must be
1608 * copied back into pad->pre_collapse_qiov (e.g. by bdrv_padding_finalize()).
1609 */
1610static int bdrv_create_padded_qiov(BlockDriverState *bs,
1611 BdrvRequestPadding *pad,
1612 struct iovec *iov, int niov,
1613 size_t iov_offset, size_t bytes)
1614{
1615 int padded_niov, surplus_count, collapse_count;
1616
1617 /* Assert this invariant */
1618 assert(niov <= IOV_MAX);
1619
1620 /*
1621 * Cannot pad if resulting length would exceed SIZE_MAX. Returning an error
1622 * to the guest is not ideal, but there is little else we can do. At least
1623 * this will practically never happen on 64-bit systems.
1624 */
1625 if (SIZE_MAX - pad->head < bytes ||
1626 SIZE_MAX - pad->head - bytes < pad->tail)
1627 {
1628 return -EINVAL;
1629 }
1630
1631 /* Length of the resulting IOV if we just concatenated everything */
1632 padded_niov = !!pad->head + niov + !!pad->tail;
1633
1634 qemu_iovec_init(&pad->local_qiov, MIN(padded_niov, IOV_MAX));
1635
1636 if (pad->head) {
1637 qemu_iovec_add(&pad->local_qiov, pad->buf, pad->head);
1638 }
1639
1640 /*
1641 * If padded_niov > IOV_MAX, we cannot just concatenate everything.
1642 * Instead, merge the first two or three elements of @iov to reduce the
1643 * number of vector elements as necessary.
1644 */
1645 if (padded_niov > IOV_MAX) {
1646 /*
1647 * Only head and tail can have lead to the number of entries exceeding
1648 * IOV_MAX, so we can exceed it by the head and tail at most. We need
1649 * to reduce the number of elements by `surplus_count`, so we merge that
1650 * many elements plus one into one element.
1651 */
1652 surplus_count = padded_niov - IOV_MAX;
1653 assert(surplus_count <= !!pad->head + !!pad->tail);
1654 collapse_count = surplus_count + 1;
1655
1656 /*
1657 * Move the elements to collapse into `pad->pre_collapse_qiov`, then
1658 * advance `iov` (and associated variables) by those elements.
1659 */
1660 qemu_iovec_init(&pad->pre_collapse_qiov, collapse_count);
1661 qemu_iovec_concat_iov(&pad->pre_collapse_qiov, iov,
1662 collapse_count, iov_offset, SIZE_MAX);
1663 iov += collapse_count;
1664 iov_offset = 0;
1665 niov -= collapse_count;
1666 bytes -= pad->pre_collapse_qiov.size;
1667
1668 /*
1669 * Construct the bounce buffer to match the length of the to-collapse
1670 * vector elements, and for write requests, initialize it with the data
1671 * from those elements. Then add it to `pad->local_qiov`.
1672 */
1673 pad->collapse_len = pad->pre_collapse_qiov.size;
1674 pad->collapse_bounce_buf = qemu_blockalign(bs, pad->collapse_len);
1675 if (pad->write) {
1676 qemu_iovec_to_buf(&pad->pre_collapse_qiov, 0,
1677 pad->collapse_bounce_buf, pad->collapse_len);
1678 }
1679 qemu_iovec_add(&pad->local_qiov,
1680 pad->collapse_bounce_buf, pad->collapse_len);
1681 }
1682
1683 qemu_iovec_concat_iov(&pad->local_qiov, iov, niov, iov_offset, bytes);
1684
1685 if (pad->tail) {
1686 qemu_iovec_add(&pad->local_qiov,
1687 pad->buf + pad->buf_len - pad->tail, pad->tail);
1688 }
1689
1690 assert(pad->local_qiov.niov == MIN(padded_niov, IOV_MAX));
1691 return 0;
1692}
1693
7a3f542f
VSO
1694/*
1695 * bdrv_pad_request
1696 *
1697 * Exchange request parameters with padded request if needed. Don't include RMW
1698 * read of padding, bdrv_padding_rmw_read() should be called separately if
1699 * needed.
1700 *
18743311
HC
1701 * @write is true for write requests, false for read requests.
1702 *
98ca4549
VSO
1703 * Request parameters (@qiov, &qiov_offset, &offset, &bytes) are in-out:
1704 * - on function start they represent original request
1705 * - on failure or when padding is not needed they are unchanged
1706 * - on success when padding is needed they represent padded request
61007b31 1707 */
98ca4549
VSO
1708static int bdrv_pad_request(BlockDriverState *bs,
1709 QEMUIOVector **qiov, size_t *qiov_offset,
37e9403e 1710 int64_t *offset, int64_t *bytes,
18743311 1711 bool write,
e8b65355
SH
1712 BdrvRequestPadding *pad, bool *padded,
1713 BdrvRequestFlags *flags)
7a3f542f 1714{
4c002cef 1715 int ret;
18743311
HC
1716 struct iovec *sliced_iov;
1717 int sliced_niov;
1718 size_t sliced_head, sliced_tail;
4c002cef 1719
ef256751
HC
1720 /* Should have been checked by the caller already */
1721 ret = bdrv_check_request32(*offset, *bytes, *qiov, *qiov_offset);
1722 if (ret < 0) {
1723 return ret;
1724 }
37e9403e 1725
18743311 1726 if (!bdrv_init_padding(bs, *offset, *bytes, write, pad)) {
98ca4549
VSO
1727 if (padded) {
1728 *padded = false;
1729 }
1730 return 0;
7a3f542f
VSO
1731 }
1732
18743311
HC
1733 sliced_iov = qemu_iovec_slice(*qiov, *qiov_offset, *bytes,
1734 &sliced_head, &sliced_tail,
1735 &sliced_niov);
1736
ef256751 1737 /* Guaranteed by bdrv_check_request32() */
18743311
HC
1738 assert(*bytes <= SIZE_MAX);
1739 ret = bdrv_create_padded_qiov(bs, pad, sliced_iov, sliced_niov,
1740 sliced_head, *bytes);
98ca4549 1741 if (ret < 0) {
18743311 1742 bdrv_padding_finalize(pad);
98ca4549
VSO
1743 return ret;
1744 }
7a3f542f
VSO
1745 *bytes += pad->head + pad->tail;
1746 *offset -= pad->head;
1747 *qiov = &pad->local_qiov;
1acc3466 1748 *qiov_offset = 0;
98ca4549
VSO
1749 if (padded) {
1750 *padded = true;
1751 }
e8b65355
SH
1752 if (flags) {
1753 /* Can't use optimization hint with bounce buffer */
1754 *flags &= ~BDRV_REQ_REGISTERED_BUF;
1755 }
7a3f542f 1756
98ca4549 1757 return 0;
7a3f542f
VSO
1758}
1759
a03ef88f 1760int coroutine_fn bdrv_co_preadv(BdrvChild *child,
e9e52efd 1761 int64_t offset, int64_t bytes, QEMUIOVector *qiov,
61007b31 1762 BdrvRequestFlags flags)
1acc3466 1763{
967d7905 1764 IO_CODE();
1acc3466
VSO
1765 return bdrv_co_preadv_part(child, offset, bytes, qiov, 0, flags);
1766}
1767
1768int coroutine_fn bdrv_co_preadv_part(BdrvChild *child,
37e9403e 1769 int64_t offset, int64_t bytes,
1acc3466
VSO
1770 QEMUIOVector *qiov, size_t qiov_offset,
1771 BdrvRequestFlags flags)
61007b31 1772{
a03ef88f 1773 BlockDriverState *bs = child->bs;
61007b31 1774 BdrvTrackedRequest req;
7a3f542f 1775 BdrvRequestPadding pad;
61007b31 1776 int ret;
967d7905 1777 IO_CODE();
61007b31 1778
37e9403e 1779 trace_bdrv_co_preadv_part(bs, offset, bytes, flags);
61007b31 1780
1e97be91 1781 if (!bdrv_co_is_inserted(bs)) {
f4dad307
VSO
1782 return -ENOMEDIUM;
1783 }
1784
63f4ad11 1785 ret = bdrv_check_request32(offset, bytes, qiov, qiov_offset);
61007b31
SH
1786 if (ret < 0) {
1787 return ret;
1788 }
1789
ac9d00bf
VSO
1790 if (bytes == 0 && !QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)) {
1791 /*
1792 * Aligning zero request is nonsense. Even if driver has special meaning
1793 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
1794 * it to driver due to request_alignment.
1795 *
1796 * Still, no reason to return an error if someone do unaligned
1797 * zero-length read occasionally.
1798 */
1799 return 0;
1800 }
1801
99723548
PB
1802 bdrv_inc_in_flight(bs);
1803
9568b511 1804 /* Don't do copy-on-read if we read data before write operation */
d73415a3 1805 if (qatomic_read(&bs->copy_on_read)) {
61007b31
SH
1806 flags |= BDRV_REQ_COPY_ON_READ;
1807 }
1808
18743311
HC
1809 ret = bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes, false,
1810 &pad, NULL, &flags);
98ca4549 1811 if (ret < 0) {
87ab8802 1812 goto fail;
98ca4549 1813 }
61007b31 1814
ebde595c 1815 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_READ);
7a3f542f
VSO
1816 ret = bdrv_aligned_preadv(child, &req, offset, bytes,
1817 bs->bl.request_alignment,
1acc3466 1818 qiov, qiov_offset, flags);
61007b31 1819 tracked_request_end(&req);
18743311 1820 bdrv_padding_finalize(&pad);
61007b31 1821
87ab8802
KW
1822fail:
1823 bdrv_dec_in_flight(bs);
1824
61007b31
SH
1825 return ret;
1826}
1827
eeb47775
KW
1828static int coroutine_fn GRAPH_RDLOCK
1829bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
1830 BdrvRequestFlags flags)
61007b31
SH
1831{
1832 BlockDriver *drv = bs->drv;
1833 QEMUIOVector qiov;
0d93ed08 1834 void *buf = NULL;
61007b31 1835 int ret = 0;
465fe887 1836 bool need_flush = false;
443668ca
DL
1837 int head = 0;
1838 int tail = 0;
61007b31 1839
2aaa3f9b
VSO
1840 int64_t max_write_zeroes = MIN_NON_ZERO(bs->bl.max_pwrite_zeroes,
1841 INT64_MAX);
a5b8dd2c
EB
1842 int alignment = MAX(bs->bl.pwrite_zeroes_alignment,
1843 bs->bl.request_alignment);
cb2e2878 1844 int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer, MAX_BOUNCE_BUFFER);
d05aa8bb 1845
abaf8b75 1846 assert_bdrv_graph_readable();
5ae07b14
VSO
1847 bdrv_check_request(offset, bytes, &error_abort);
1848
d470ad42
HR
1849 if (!drv) {
1850 return -ENOMEDIUM;
1851 }
1852
fe0480d6
KW
1853 if ((flags & ~bs->supported_zero_flags) & BDRV_REQ_NO_FALLBACK) {
1854 return -ENOTSUP;
1855 }
1856
e8b65355
SH
1857 /* By definition there is no user buffer so this flag doesn't make sense */
1858 if (flags & BDRV_REQ_REGISTERED_BUF) {
1859 return -EINVAL;
1860 }
1861
0bc329fb
HR
1862 /* Invalidate the cached block-status data range if this write overlaps */
1863 bdrv_bsc_invalidate_range(bs, offset, bytes);
1864
b8d0a980
EB
1865 assert(alignment % bs->bl.request_alignment == 0);
1866 head = offset % alignment;
f5a5ca79 1867 tail = (offset + bytes) % alignment;
b8d0a980
EB
1868 max_write_zeroes = QEMU_ALIGN_DOWN(max_write_zeroes, alignment);
1869 assert(max_write_zeroes >= bs->bl.request_alignment);
61007b31 1870
f5a5ca79 1871 while (bytes > 0 && !ret) {
5ae07b14 1872 int64_t num = bytes;
61007b31
SH
1873
1874 /* Align request. Block drivers can expect the "bulk" of the request
443668ca
DL
1875 * to be aligned, and that unaligned requests do not cross cluster
1876 * boundaries.
61007b31 1877 */
443668ca 1878 if (head) {
b2f95fee
EB
1879 /* Make a small request up to the first aligned sector. For
1880 * convenience, limit this request to max_transfer even if
1881 * we don't need to fall back to writes. */
f5a5ca79 1882 num = MIN(MIN(bytes, max_transfer), alignment - head);
b2f95fee
EB
1883 head = (head + num) % alignment;
1884 assert(num < max_write_zeroes);
d05aa8bb 1885 } else if (tail && num > alignment) {
443668ca
DL
1886 /* Shorten the request to the last aligned sector. */
1887 num -= tail;
61007b31
SH
1888 }
1889
1890 /* limit request size */
1891 if (num > max_write_zeroes) {
1892 num = max_write_zeroes;
1893 }
1894
1895 ret = -ENOTSUP;
1896 /* First try the efficient write zeroes operation */
d05aa8bb
EB
1897 if (drv->bdrv_co_pwrite_zeroes) {
1898 ret = drv->bdrv_co_pwrite_zeroes(bs, offset, num,
1899 flags & bs->supported_zero_flags);
1900 if (ret != -ENOTSUP && (flags & BDRV_REQ_FUA) &&
1901 !(bs->supported_zero_flags & BDRV_REQ_FUA)) {
1902 need_flush = true;
1903 }
465fe887
EB
1904 } else {
1905 assert(!bs->supported_zero_flags);
61007b31
SH
1906 }
1907
294682cc 1908 if (ret == -ENOTSUP && !(flags & BDRV_REQ_NO_FALLBACK)) {
61007b31 1909 /* Fall back to bounce buffer if write zeroes is unsupported */
465fe887
EB
1910 BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE;
1911
1912 if ((flags & BDRV_REQ_FUA) &&
1913 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
1914 /* No need for bdrv_driver_pwrite() to do a fallback
1915 * flush on each chunk; use just one at the end */
1916 write_flags &= ~BDRV_REQ_FUA;
1917 need_flush = true;
1918 }
5def6b80 1919 num = MIN(num, max_transfer);
0d93ed08
VSO
1920 if (buf == NULL) {
1921 buf = qemu_try_blockalign0(bs, num);
1922 if (buf == NULL) {
61007b31
SH
1923 ret = -ENOMEM;
1924 goto fail;
1925 }
61007b31 1926 }
0d93ed08 1927 qemu_iovec_init_buf(&qiov, buf, num);
61007b31 1928
ac850bf0 1929 ret = bdrv_driver_pwritev(bs, offset, num, &qiov, 0, write_flags);
61007b31
SH
1930
1931 /* Keep bounce buffer around if it is big enough for all
1932 * all future requests.
1933 */
5def6b80 1934 if (num < max_transfer) {
0d93ed08
VSO
1935 qemu_vfree(buf);
1936 buf = NULL;
61007b31
SH
1937 }
1938 }
1939
d05aa8bb 1940 offset += num;
f5a5ca79 1941 bytes -= num;
61007b31
SH
1942 }
1943
1944fail:
465fe887
EB
1945 if (ret == 0 && need_flush) {
1946 ret = bdrv_co_flush(bs);
1947 }
0d93ed08 1948 qemu_vfree(buf);
61007b31
SH
1949 return ret;
1950}
1951
a00e70c0 1952static inline int coroutine_fn GRAPH_RDLOCK
fcfd9ade 1953bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, int64_t bytes,
85fe2479
FZ
1954 BdrvTrackedRequest *req, int flags)
1955{
1956 BlockDriverState *bs = child->bs;
fcfd9ade
VSO
1957
1958 bdrv_check_request(offset, bytes, &error_abort);
85fe2479 1959
307261b2 1960 if (bdrv_is_read_only(bs)) {
85fe2479
FZ
1961 return -EPERM;
1962 }
1963
85fe2479
FZ
1964 assert(!(bs->open_flags & BDRV_O_INACTIVE));
1965 assert((bs->open_flags & BDRV_O_NO_IO) == 0);
1966 assert(!(flags & ~BDRV_REQ_MASK));
d1a764d1 1967 assert(!((flags & BDRV_REQ_NO_WAIT) && !(flags & BDRV_REQ_SERIALISING)));
85fe2479
FZ
1968
1969 if (flags & BDRV_REQ_SERIALISING) {
d1a764d1
VSO
1970 QEMU_LOCK_GUARD(&bs->reqs_lock);
1971
1972 tracked_request_set_serialising(req, bdrv_get_cluster_size(bs));
1973
1974 if ((flags & BDRV_REQ_NO_WAIT) && bdrv_find_conflicting_request(req)) {
1975 return -EBUSY;
1976 }
1977
1978 bdrv_wait_serialising_requests_locked(req);
18fbd0de
PB
1979 } else {
1980 bdrv_wait_serialising_requests(req);
85fe2479
FZ
1981 }
1982
85fe2479
FZ
1983 assert(req->overlap_offset <= offset);
1984 assert(offset + bytes <= req->overlap_offset + req->overlap_bytes);
fcfd9ade
VSO
1985 assert(offset + bytes <= bs->total_sectors * BDRV_SECTOR_SIZE ||
1986 child->perm & BLK_PERM_RESIZE);
85fe2479 1987
cd47d792
FZ
1988 switch (req->type) {
1989 case BDRV_TRACKED_WRITE:
1990 case BDRV_TRACKED_DISCARD:
1991 if (flags & BDRV_REQ_WRITE_UNCHANGED) {
1992 assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE));
1993 } else {
1994 assert(child->perm & BLK_PERM_WRITE);
1995 }
94783301
VSO
1996 bdrv_write_threshold_check_write(bs, offset, bytes);
1997 return 0;
cd47d792
FZ
1998 case BDRV_TRACKED_TRUNCATE:
1999 assert(child->perm & BLK_PERM_RESIZE);
2000 return 0;
2001 default:
2002 abort();
85fe2479 2003 }
85fe2479
FZ
2004}
2005
2006static inline void coroutine_fn
fcfd9ade 2007bdrv_co_write_req_finish(BdrvChild *child, int64_t offset, int64_t bytes,
85fe2479
FZ
2008 BdrvTrackedRequest *req, int ret)
2009{
2010 int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
2011 BlockDriverState *bs = child->bs;
2012
fcfd9ade
VSO
2013 bdrv_check_request(offset, bytes, &error_abort);
2014
d73415a3 2015 qatomic_inc(&bs->write_gen);
85fe2479 2016
00695c27
FZ
2017 /*
2018 * Discard cannot extend the image, but in error handling cases, such as
2019 * when reverting a qcow2 cluster allocation, the discarded range can pass
2020 * the end of image file, so we cannot assert about BDRV_TRACKED_DISCARD
2021 * here. Instead, just skip it, since semantically a discard request
2022 * beyond EOF cannot expand the image anyway.
2023 */
7f8f03ef 2024 if (ret == 0 &&
cd47d792
FZ
2025 (req->type == BDRV_TRACKED_TRUNCATE ||
2026 end_sector > bs->total_sectors) &&
2027 req->type != BDRV_TRACKED_DISCARD) {
7f8f03ef
FZ
2028 bs->total_sectors = end_sector;
2029 bdrv_parent_cb_resize(bs);
2030 bdrv_dirty_bitmap_truncate(bs, end_sector << BDRV_SECTOR_BITS);
85fe2479 2031 }
00695c27
FZ
2032 if (req->bytes) {
2033 switch (req->type) {
2034 case BDRV_TRACKED_WRITE:
2035 stat64_max(&bs->wr_highest_offset, offset + bytes);
2036 /* fall through, to set dirty bits */
2037 case BDRV_TRACKED_DISCARD:
2038 bdrv_set_dirty(bs, offset, bytes);
2039 break;
2040 default:
2041 break;
2042 }
2043 }
85fe2479
FZ
2044}
2045
61007b31 2046/*
04ed95f4
EB
2047 * Forwards an already correctly aligned write request to the BlockDriver,
2048 * after possibly fragmenting it.
61007b31 2049 */
7b1fb72e
KW
2050static int coroutine_fn GRAPH_RDLOCK
2051bdrv_aligned_pwritev(BdrvChild *child, BdrvTrackedRequest *req,
2052 int64_t offset, int64_t bytes, int64_t align,
2053 QEMUIOVector *qiov, size_t qiov_offset,
2054 BdrvRequestFlags flags)
61007b31 2055{
85c97ca7 2056 BlockDriverState *bs = child->bs;
61007b31 2057 BlockDriver *drv = bs->drv;
61007b31
SH
2058 int ret;
2059
fcfd9ade 2060 int64_t bytes_remaining = bytes;
04ed95f4 2061 int max_transfer;
61007b31 2062
fcfd9ade
VSO
2063 bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort);
2064
d470ad42
HR
2065 if (!drv) {
2066 return -ENOMEDIUM;
2067 }
2068
d6883bc9
VSO
2069 if (bdrv_has_readonly_bitmaps(bs)) {
2070 return -EPERM;
2071 }
2072
cff86b38
EB
2073 assert(is_power_of_2(align));
2074 assert((offset & (align - 1)) == 0);
2075 assert((bytes & (align - 1)) == 0);
04ed95f4
EB
2076 max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX),
2077 align);
61007b31 2078
85fe2479 2079 ret = bdrv_co_write_req_prepare(child, offset, bytes, req, flags);
61007b31
SH
2080
2081 if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF &&
c1499a5e 2082 !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes &&
28c4da28 2083 qemu_iovec_is_zero(qiov, qiov_offset, bytes)) {
61007b31
SH
2084 flags |= BDRV_REQ_ZERO_WRITE;
2085 if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) {
2086 flags |= BDRV_REQ_MAY_UNMAP;
2087 }
3c586715
SH
2088
2089 /* Can't use optimization hint with bufferless zero write */
2090 flags &= ~BDRV_REQ_REGISTERED_BUF;
61007b31
SH
2091 }
2092
2093 if (ret < 0) {
2094 /* Do nothing, write notifier decided to fail this request */
2095 } else if (flags & BDRV_REQ_ZERO_WRITE) {
c834dc05 2096 bdrv_co_debug_event(bs, BLKDBG_PWRITEV_ZERO);
9896c876 2097 ret = bdrv_co_do_pwrite_zeroes(bs, offset, bytes, flags);
3ea1a091 2098 } else if (flags & BDRV_REQ_WRITE_COMPRESSED) {
28c4da28
VSO
2099 ret = bdrv_driver_pwritev_compressed(bs, offset, bytes,
2100 qiov, qiov_offset);
04ed95f4 2101 } else if (bytes <= max_transfer) {
c834dc05 2102 bdrv_co_debug_event(bs, BLKDBG_PWRITEV);
28c4da28 2103 ret = bdrv_driver_pwritev(bs, offset, bytes, qiov, qiov_offset, flags);
04ed95f4 2104 } else {
c834dc05 2105 bdrv_co_debug_event(bs, BLKDBG_PWRITEV);
04ed95f4
EB
2106 while (bytes_remaining) {
2107 int num = MIN(bytes_remaining, max_transfer);
04ed95f4
EB
2108 int local_flags = flags;
2109
2110 assert(num);
2111 if (num < bytes_remaining && (flags & BDRV_REQ_FUA) &&
2112 !(bs->supported_write_flags & BDRV_REQ_FUA)) {
2113 /* If FUA is going to be emulated by flush, we only
2114 * need to flush on the last iteration */
2115 local_flags &= ~BDRV_REQ_FUA;
2116 }
04ed95f4
EB
2117
2118 ret = bdrv_driver_pwritev(bs, offset + bytes - bytes_remaining,
134b7dec
HR
2119 num, qiov,
2120 qiov_offset + bytes - bytes_remaining,
28c4da28 2121 local_flags);
04ed95f4
EB
2122 if (ret < 0) {
2123 break;
2124 }
2125 bytes_remaining -= num;
2126 }
61007b31 2127 }
c834dc05 2128 bdrv_co_debug_event(bs, BLKDBG_PWRITEV_DONE);
61007b31 2129
61007b31 2130 if (ret >= 0) {
04ed95f4 2131 ret = 0;
61007b31 2132 }
85fe2479 2133 bdrv_co_write_req_finish(child, offset, bytes, req, ret);
61007b31
SH
2134
2135 return ret;
2136}
2137
7b1fb72e
KW
2138static int coroutine_fn GRAPH_RDLOCK
2139bdrv_co_do_zero_pwritev(BdrvChild *child, int64_t offset, int64_t bytes,
2140 BdrvRequestFlags flags, BdrvTrackedRequest *req)
9eeb6dd1 2141{
85c97ca7 2142 BlockDriverState *bs = child->bs;
9eeb6dd1 2143 QEMUIOVector local_qiov;
a5b8dd2c 2144 uint64_t align = bs->bl.request_alignment;
9eeb6dd1 2145 int ret = 0;
7a3f542f
VSO
2146 bool padding;
2147 BdrvRequestPadding pad;
9eeb6dd1 2148
e8b65355
SH
2149 /* This flag doesn't make sense for padding or zero writes */
2150 flags &= ~BDRV_REQ_REGISTERED_BUF;
2151
18743311 2152 padding = bdrv_init_padding(bs, offset, bytes, true, &pad);
7a3f542f 2153 if (padding) {
45e62b46 2154 assert(!(flags & BDRV_REQ_NO_WAIT));
8ac5aab2 2155 bdrv_make_request_serialising(req, align);
9eeb6dd1 2156
7a3f542f
VSO
2157 bdrv_padding_rmw_read(child, req, &pad, true);
2158
2159 if (pad.head || pad.merge_reads) {
2160 int64_t aligned_offset = offset & ~(align - 1);
2161 int64_t write_bytes = pad.merge_reads ? pad.buf_len : align;
2162
2163 qemu_iovec_init_buf(&local_qiov, pad.buf, write_bytes);
2164 ret = bdrv_aligned_pwritev(child, req, aligned_offset, write_bytes,
28c4da28 2165 align, &local_qiov, 0,
7a3f542f
VSO
2166 flags & ~BDRV_REQ_ZERO_WRITE);
2167 if (ret < 0 || pad.merge_reads) {
2168 /* Error or all work is done */
2169 goto out;
2170 }
2171 offset += write_bytes - pad.head;
2172 bytes -= write_bytes - pad.head;
9eeb6dd1 2173 }
9eeb6dd1
FZ
2174 }
2175
2176 assert(!bytes || (offset & (align - 1)) == 0);
2177 if (bytes >= align) {
2178 /* Write the aligned part in the middle. */
fcfd9ade 2179 int64_t aligned_bytes = bytes & ~(align - 1);
85c97ca7 2180 ret = bdrv_aligned_pwritev(child, req, offset, aligned_bytes, align,
28c4da28 2181 NULL, 0, flags);
9eeb6dd1 2182 if (ret < 0) {
7a3f542f 2183 goto out;
9eeb6dd1
FZ
2184 }
2185 bytes -= aligned_bytes;
2186 offset += aligned_bytes;
2187 }
2188
2189 assert(!bytes || (offset & (align - 1)) == 0);
2190 if (bytes) {
7a3f542f 2191 assert(align == pad.tail + bytes);
9eeb6dd1 2192
7a3f542f 2193 qemu_iovec_init_buf(&local_qiov, pad.tail_buf, align);
85c97ca7 2194 ret = bdrv_aligned_pwritev(child, req, offset, align, align,
28c4da28
VSO
2195 &local_qiov, 0,
2196 flags & ~BDRV_REQ_ZERO_WRITE);
9eeb6dd1 2197 }
9eeb6dd1 2198
7a3f542f 2199out:
18743311 2200 bdrv_padding_finalize(&pad);
7a3f542f
VSO
2201
2202 return ret;
9eeb6dd1
FZ
2203}
2204
61007b31
SH
2205/*
2206 * Handle a write request in coroutine context
2207 */
a03ef88f 2208int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
e9e52efd 2209 int64_t offset, int64_t bytes, QEMUIOVector *qiov,
61007b31 2210 BdrvRequestFlags flags)
1acc3466 2211{
967d7905 2212 IO_CODE();
1acc3466
VSO
2213 return bdrv_co_pwritev_part(child, offset, bytes, qiov, 0, flags);
2214}
2215
2216int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child,
37e9403e 2217 int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset,
1acc3466 2218 BdrvRequestFlags flags)
61007b31 2219{
a03ef88f 2220 BlockDriverState *bs = child->bs;
61007b31 2221 BdrvTrackedRequest req;
a5b8dd2c 2222 uint64_t align = bs->bl.request_alignment;
7a3f542f 2223 BdrvRequestPadding pad;
61007b31 2224 int ret;
f0deecff 2225 bool padded = false;
967d7905 2226 IO_CODE();
61007b31 2227
37e9403e 2228 trace_bdrv_co_pwritev_part(child->bs, offset, bytes, flags);
f42cf447 2229
1e97be91 2230 if (!bdrv_co_is_inserted(bs)) {
61007b31
SH
2231 return -ENOMEDIUM;
2232 }
61007b31 2233
2aaa3f9b
VSO
2234 if (flags & BDRV_REQ_ZERO_WRITE) {
2235 ret = bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, NULL);
2236 } else {
2237 ret = bdrv_check_request32(offset, bytes, qiov, qiov_offset);
2238 }
61007b31
SH
2239 if (ret < 0) {
2240 return ret;
2241 }
2242
f2208fdc
AG
2243 /* If the request is misaligned then we can't make it efficient */
2244 if ((flags & BDRV_REQ_NO_FALLBACK) &&
2245 !QEMU_IS_ALIGNED(offset | bytes, align))
2246 {
2247 return -ENOTSUP;
2248 }
2249
ac9d00bf
VSO
2250 if (bytes == 0 && !QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)) {
2251 /*
2252 * Aligning zero request is nonsense. Even if driver has special meaning
2253 * of zero-length (like qcow2_co_pwritev_compressed_part), we can't pass
2254 * it to driver due to request_alignment.
2255 *
2256 * Still, no reason to return an error if someone do unaligned
2257 * zero-length write occasionally.
2258 */
2259 return 0;
2260 }
2261
f0deecff
VSO
2262 if (!(flags & BDRV_REQ_ZERO_WRITE)) {
2263 /*
2264 * Pad request for following read-modify-write cycle.
2265 * bdrv_co_do_zero_pwritev() does aligning by itself, so, we do
2266 * alignment only if there is no ZERO flag.
2267 */
18743311
HC
2268 ret = bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes, true,
2269 &pad, &padded, &flags);
98ca4549
VSO
2270 if (ret < 0) {
2271 return ret;
2272 }
f0deecff
VSO
2273 }
2274
99723548 2275 bdrv_inc_in_flight(bs);
ebde595c 2276 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
61007b31 2277
18a59f03 2278 if (flags & BDRV_REQ_ZERO_WRITE) {
f0deecff 2279 assert(!padded);
85c97ca7 2280 ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);
9eeb6dd1
FZ
2281 goto out;
2282 }
2283
f0deecff
VSO
2284 if (padded) {
2285 /*
2286 * Request was unaligned to request_alignment and therefore
2287 * padded. We are going to do read-modify-write, and must
2288 * serialize the request to prevent interactions of the
2289 * widened region with other transactions.
2290 */
45e62b46 2291 assert(!(flags & BDRV_REQ_NO_WAIT));
8ac5aab2 2292 bdrv_make_request_serialising(&req, align);
7a3f542f 2293 bdrv_padding_rmw_read(child, &req, &pad, false);
61007b31
SH
2294 }
2295
85c97ca7 2296 ret = bdrv_aligned_pwritev(child, &req, offset, bytes, align,
1acc3466 2297 qiov, qiov_offset, flags);
61007b31 2298
18743311 2299 bdrv_padding_finalize(&pad);
61007b31 2300
9eeb6dd1
FZ
2301out:
2302 tracked_request_end(&req);
99723548 2303 bdrv_dec_in_flight(bs);
7a3f542f 2304
61007b31
SH
2305 return ret;
2306}
2307
a03ef88f 2308int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset,
e9e52efd 2309 int64_t bytes, BdrvRequestFlags flags)
61007b31 2310{
384a48fb 2311 IO_CODE();
f5a5ca79 2312 trace_bdrv_co_pwrite_zeroes(child->bs, offset, bytes, flags);
abaf8b75 2313 assert_bdrv_graph_readable();
61007b31 2314
a03ef88f 2315 if (!(child->bs->open_flags & BDRV_O_UNMAP)) {
61007b31
SH
2316 flags &= ~BDRV_REQ_MAY_UNMAP;
2317 }
61007b31 2318
f5a5ca79 2319 return bdrv_co_pwritev(child, offset, bytes, NULL,
74021bc4 2320 BDRV_REQ_ZERO_WRITE | flags);
61007b31
SH
2321}
2322
4085f5c7
JS
2323/*
2324 * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
2325 */
2326int bdrv_flush_all(void)
2327{
2328 BdrvNextIterator it;
2329 BlockDriverState *bs = NULL;
2330 int result = 0;
2331
f791bf7f 2332 GLOBAL_STATE_CODE();
2b3912f1 2333 GRAPH_RDLOCK_GUARD_MAINLOOP();
f791bf7f 2334
c8aa7895
PD
2335 /*
2336 * bdrv queue is managed by record/replay,
2337 * creating new flush request for stopping
2338 * the VM may break the determinism
2339 */
2340 if (replay_events_enabled()) {
2341 return result;
2342 }
2343
4085f5c7
JS
2344 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
2345 AioContext *aio_context = bdrv_get_aio_context(bs);
2346 int ret;
2347
2348 aio_context_acquire(aio_context);
2349 ret = bdrv_flush(bs);
2350 if (ret < 0 && !result) {
2351 result = ret;
2352 }
2353 aio_context_release(aio_context);
2354 }
2355
2356 return result;
2357}
2358
61007b31
SH
2359/*
2360 * Returns the allocation status of the specified sectors.
2361 * Drivers not implementing the functionality are assumed to not support
2362 * backing files, hence all their sectors are reported as allocated.
2363 *
86a3d5c6
EB
2364 * If 'want_zero' is true, the caller is querying for mapping
2365 * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and
2366 * _ZERO where possible; otherwise, the result favors larger 'pnum',
2367 * with a focus on accurate BDRV_BLOCK_ALLOCATED.
c9ce8c4d 2368 *
2e8bc787 2369 * If 'offset' is beyond the end of the disk image the return value is
fb0d8654 2370 * BDRV_BLOCK_EOF and 'pnum' is set to 0.
61007b31 2371 *
2e8bc787 2372 * 'bytes' is the max value 'pnum' should be set to. If bytes goes
fb0d8654
EB
2373 * beyond the end of the disk image it will be clamped; if 'pnum' is set to
2374 * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
67a0fd2a 2375 *
2e8bc787
EB
2376 * 'pnum' is set to the number of bytes (including and immediately
2377 * following the specified offset) that are easily known to be in the
2378 * same allocated/unallocated state. Note that a second call starting
2379 * at the original offset plus returned pnum may have the same status.
2380 * The returned value is non-zero on success except at end-of-file.
2381 *
2382 * Returns negative errno on failure. Otherwise, if the
2383 * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are
2384 * set to the host mapping and BDS corresponding to the guest offset.
61007b31 2385 */
7ff9579e 2386static int coroutine_fn GRAPH_RDLOCK
b170e929
PB
2387bdrv_co_do_block_status(BlockDriverState *bs, bool want_zero,
2388 int64_t offset, int64_t bytes,
2389 int64_t *pnum, int64_t *map, BlockDriverState **file)
2e8bc787
EB
2390{
2391 int64_t total_size;
2392 int64_t n; /* bytes */
efa6e2ed 2393 int ret;
2e8bc787 2394 int64_t local_map = 0;
298a1665 2395 BlockDriverState *local_file = NULL;
efa6e2ed
EB
2396 int64_t aligned_offset, aligned_bytes;
2397 uint32_t align;
549ec0d9 2398 bool has_filtered_child;
61007b31 2399
298a1665 2400 assert(pnum);
7ff9579e 2401 assert_bdrv_graph_readable();
298a1665 2402 *pnum = 0;
0af02bd1 2403 total_size = bdrv_co_getlength(bs);
2e8bc787
EB
2404 if (total_size < 0) {
2405 ret = total_size;
298a1665 2406 goto early_out;
61007b31
SH
2407 }
2408
2e8bc787 2409 if (offset >= total_size) {
298a1665
EB
2410 ret = BDRV_BLOCK_EOF;
2411 goto early_out;
61007b31 2412 }
2e8bc787 2413 if (!bytes) {
298a1665
EB
2414 ret = 0;
2415 goto early_out;
9cdcfd9f 2416 }
61007b31 2417
2e8bc787
EB
2418 n = total_size - offset;
2419 if (n < bytes) {
2420 bytes = n;
61007b31
SH
2421 }
2422
0af02bd1 2423 /* Must be non-NULL or bdrv_co_getlength() would have failed */
d470ad42 2424 assert(bs->drv);
549ec0d9
HR
2425 has_filtered_child = bdrv_filter_child(bs);
2426 if (!bs->drv->bdrv_co_block_status && !has_filtered_child) {
2e8bc787 2427 *pnum = bytes;
61007b31 2428 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED;
2e8bc787 2429 if (offset + bytes == total_size) {
fb0d8654
EB
2430 ret |= BDRV_BLOCK_EOF;
2431 }
61007b31 2432 if (bs->drv->protocol_name) {
2e8bc787
EB
2433 ret |= BDRV_BLOCK_OFFSET_VALID;
2434 local_map = offset;
298a1665 2435 local_file = bs;
61007b31 2436 }
298a1665 2437 goto early_out;
61007b31
SH
2438 }
2439
99723548 2440 bdrv_inc_in_flight(bs);
efa6e2ed
EB
2441
2442 /* Round out to request_alignment boundaries */
86a3d5c6 2443 align = bs->bl.request_alignment;
efa6e2ed
EB
2444 aligned_offset = QEMU_ALIGN_DOWN(offset, align);
2445 aligned_bytes = ROUND_UP(offset + bytes, align) - aligned_offset;
2446
549ec0d9 2447 if (bs->drv->bdrv_co_block_status) {
0bc329fb
HR
2448 /*
2449 * Use the block-status cache only for protocol nodes: Format
2450 * drivers are generally quick to inquire the status, but protocol
2451 * drivers often need to get information from outside of qemu, so
2452 * we do not have control over the actual implementation. There
2453 * have been cases where inquiring the status took an unreasonably
2454 * long time, and we can do nothing in qemu to fix it.
2455 * This is especially problematic for images with large data areas,
2456 * because finding the few holes in them and giving them special
2457 * treatment does not gain much performance. Therefore, we try to
2458 * cache the last-identified data region.
2459 *
2460 * Second, limiting ourselves to protocol nodes allows us to assume
2461 * the block status for data regions to be DATA | OFFSET_VALID, and
2462 * that the host offset is the same as the guest offset.
2463 *
2464 * Note that it is possible that external writers zero parts of
2465 * the cached regions without the cache being invalidated, and so
2466 * we may report zeroes as data. This is not catastrophic,
2467 * however, because reporting zeroes as data is fine.
2468 */
2469 if (QLIST_EMPTY(&bs->children) &&
2470 bdrv_bsc_is_data(bs, aligned_offset, pnum))
2471 {
2472 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
2473 local_file = bs;
2474 local_map = aligned_offset;
2475 } else {
2476 ret = bs->drv->bdrv_co_block_status(bs, want_zero, aligned_offset,
2477 aligned_bytes, pnum, &local_map,
2478 &local_file);
2479
2480 /*
2481 * Note that checking QLIST_EMPTY(&bs->children) is also done when
2482 * the cache is queried above. Technically, we do not need to check
2483 * it here; the worst that can happen is that we fill the cache for
2484 * non-protocol nodes, and then it is never used. However, filling
2485 * the cache requires an RCU update, so double check here to avoid
2486 * such an update if possible.
113b727c
HR
2487 *
2488 * Check want_zero, because we only want to update the cache when we
2489 * have accurate information about what is zero and what is data.
0bc329fb 2490 */
113b727c
HR
2491 if (want_zero &&
2492 ret == (BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID) &&
0bc329fb
HR
2493 QLIST_EMPTY(&bs->children))
2494 {
2495 /*
2496 * When a protocol driver reports BLOCK_OFFSET_VALID, the
2497 * returned local_map value must be the same as the offset we
2498 * have passed (aligned_offset), and local_bs must be the node
2499 * itself.
2500 * Assert this, because we follow this rule when reading from
2501 * the cache (see the `local_file = bs` and
2502 * `local_map = aligned_offset` assignments above), and the
2503 * result the cache delivers must be the same as the driver
2504 * would deliver.
2505 */
2506 assert(local_file == bs);
2507 assert(local_map == aligned_offset);
2508 bdrv_bsc_fill(bs, aligned_offset, *pnum);
2509 }
2510 }
549ec0d9
HR
2511 } else {
2512 /* Default code for filters */
2513
2514 local_file = bdrv_filter_bs(bs);
2515 assert(local_file);
2516
2517 *pnum = aligned_bytes;
2518 local_map = aligned_offset;
2519 ret = BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
2520 }
636cb512
EB
2521 if (ret < 0) {
2522 *pnum = 0;
2523 goto out;
efa6e2ed
EB
2524 }
2525
2e8bc787 2526 /*
636cb512 2527 * The driver's result must be a non-zero multiple of request_alignment.
efa6e2ed 2528 * Clamp pnum and adjust map to original request.
2e8bc787 2529 */
636cb512
EB
2530 assert(*pnum && QEMU_IS_ALIGNED(*pnum, align) &&
2531 align > offset - aligned_offset);
69f47505
VSO
2532 if (ret & BDRV_BLOCK_RECURSE) {
2533 assert(ret & BDRV_BLOCK_DATA);
2534 assert(ret & BDRV_BLOCK_OFFSET_VALID);
2535 assert(!(ret & BDRV_BLOCK_ZERO));
2536 }
2537
efa6e2ed
EB
2538 *pnum -= offset - aligned_offset;
2539 if (*pnum > bytes) {
2540 *pnum = bytes;
61007b31 2541 }
2e8bc787 2542 if (ret & BDRV_BLOCK_OFFSET_VALID) {
efa6e2ed 2543 local_map += offset - aligned_offset;
2e8bc787 2544 }
61007b31
SH
2545
2546 if (ret & BDRV_BLOCK_RAW) {
298a1665 2547 assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file);
b170e929
PB
2548 ret = bdrv_co_do_block_status(local_file, want_zero, local_map,
2549 *pnum, pnum, &local_map, &local_file);
99723548 2550 goto out;
61007b31
SH
2551 }
2552
2553 if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
2554 ret |= BDRV_BLOCK_ALLOCATED;
d40f4a56 2555 } else if (bs->drv->supports_backing) {
cb850315
HR
2556 BlockDriverState *cow_bs = bdrv_cow_bs(bs);
2557
d40f4a56
AG
2558 if (!cow_bs) {
2559 ret |= BDRV_BLOCK_ZERO;
2560 } else if (want_zero) {
0af02bd1 2561 int64_t size2 = bdrv_co_getlength(cow_bs);
c9ce8c4d 2562
2e8bc787 2563 if (size2 >= 0 && offset >= size2) {
61007b31
SH
2564 ret |= BDRV_BLOCK_ZERO;
2565 }
2566 }
2567 }
2568
69f47505
VSO
2569 if (want_zero && ret & BDRV_BLOCK_RECURSE &&
2570 local_file && local_file != bs &&
61007b31
SH
2571 (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
2572 (ret & BDRV_BLOCK_OFFSET_VALID)) {
2e8bc787
EB
2573 int64_t file_pnum;
2574 int ret2;
61007b31 2575
b170e929
PB
2576 ret2 = bdrv_co_do_block_status(local_file, want_zero, local_map,
2577 *pnum, &file_pnum, NULL, NULL);
61007b31
SH
2578 if (ret2 >= 0) {
2579 /* Ignore errors. This is just providing extra information, it
2580 * is useful but not necessary.
2581 */
c61e684e
EB
2582 if (ret2 & BDRV_BLOCK_EOF &&
2583 (!file_pnum || ret2 & BDRV_BLOCK_ZERO)) {
2584 /*
2585 * It is valid for the format block driver to read
2586 * beyond the end of the underlying file's current
2587 * size; such areas read as zero.
2588 */
61007b31
SH
2589 ret |= BDRV_BLOCK_ZERO;
2590 } else {
2591 /* Limit request to the range reported by the protocol driver */
2592 *pnum = file_pnum;
2593 ret |= (ret2 & BDRV_BLOCK_ZERO);
2594 }
2595 }
2596 }
2597
99723548
PB
2598out:
2599 bdrv_dec_in_flight(bs);
2e8bc787 2600 if (ret >= 0 && offset + *pnum == total_size) {
fb0d8654
EB
2601 ret |= BDRV_BLOCK_EOF;
2602 }
298a1665
EB
2603early_out:
2604 if (file) {
2605 *file = local_file;
2606 }
2e8bc787
EB
2607 if (map) {
2608 *map = local_map;
2609 }
61007b31
SH
2610 return ret;
2611}
2612
21c2283e 2613int coroutine_fn
f9e694cb
VSO
2614bdrv_co_common_block_status_above(BlockDriverState *bs,
2615 BlockDriverState *base,
3555a432 2616 bool include_base,
f9e694cb
VSO
2617 bool want_zero,
2618 int64_t offset,
2619 int64_t bytes,
2620 int64_t *pnum,
2621 int64_t *map,
a92b1b06
EB
2622 BlockDriverState **file,
2623 int *depth)
ba3f0e25 2624{
67c095c8 2625 int ret;
ba3f0e25 2626 BlockDriverState *p;
67c095c8 2627 int64_t eof = 0;
a92b1b06 2628 int dummy;
1581a70d 2629 IO_CODE();
ba3f0e25 2630
3555a432 2631 assert(!include_base || base); /* Can't include NULL base */
7ff9579e 2632 assert_bdrv_graph_readable();
67c095c8 2633
a92b1b06
EB
2634 if (!depth) {
2635 depth = &dummy;
2636 }
2637 *depth = 0;
2638
624f27bb
VSO
2639 if (!include_base && bs == base) {
2640 *pnum = bytes;
2641 return 0;
2642 }
2643
b170e929
PB
2644 ret = bdrv_co_do_block_status(bs, want_zero, offset, bytes, pnum,
2645 map, file);
a92b1b06 2646 ++*depth;
3555a432 2647 if (ret < 0 || *pnum == 0 || ret & BDRV_BLOCK_ALLOCATED || bs == base) {
67c095c8
VSO
2648 return ret;
2649 }
2650
2651 if (ret & BDRV_BLOCK_EOF) {
2652 eof = offset + *pnum;
2653 }
2654
2655 assert(*pnum <= bytes);
2656 bytes = *pnum;
2657
3555a432 2658 for (p = bdrv_filter_or_cow_bs(bs); include_base || p != base;
67c095c8
VSO
2659 p = bdrv_filter_or_cow_bs(p))
2660 {
b170e929
PB
2661 ret = bdrv_co_do_block_status(p, want_zero, offset, bytes, pnum,
2662 map, file);
a92b1b06 2663 ++*depth;
c61e684e 2664 if (ret < 0) {
67c095c8 2665 return ret;
c61e684e 2666 }
67c095c8 2667 if (*pnum == 0) {
c61e684e 2668 /*
67c095c8
VSO
2669 * The top layer deferred to this layer, and because this layer is
2670 * short, any zeroes that we synthesize beyond EOF behave as if they
2671 * were allocated at this layer.
2672 *
2673 * We don't include BDRV_BLOCK_EOF into ret, as upper layer may be
2674 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2675 * below.
c61e684e 2676 */
67c095c8 2677 assert(ret & BDRV_BLOCK_EOF);
5b648c67 2678 *pnum = bytes;
67c095c8
VSO
2679 if (file) {
2680 *file = p;
2681 }
2682 ret = BDRV_BLOCK_ZERO | BDRV_BLOCK_ALLOCATED;
2683 break;
c61e684e 2684 }
67c095c8
VSO
2685 if (ret & BDRV_BLOCK_ALLOCATED) {
2686 /*
2687 * We've found the node and the status, we must break.
2688 *
2689 * Drop BDRV_BLOCK_EOF, as it's not for upper layer, which may be
2690 * larger. We'll add BDRV_BLOCK_EOF if needed at function end, see
2691 * below.
2692 */
2693 ret &= ~BDRV_BLOCK_EOF;
ba3f0e25
FZ
2694 break;
2695 }
67c095c8 2696
3555a432
VSO
2697 if (p == base) {
2698 assert(include_base);
2699 break;
2700 }
2701
67c095c8
VSO
2702 /*
2703 * OK, [offset, offset + *pnum) region is unallocated on this layer,
2704 * let's continue the diving.
2705 */
2706 assert(*pnum <= bytes);
2707 bytes = *pnum;
ba3f0e25 2708 }
67c095c8
VSO
2709
2710 if (offset + *pnum == eof) {
2711 ret |= BDRV_BLOCK_EOF;
2712 }
2713
ba3f0e25
FZ
2714 return ret;
2715}
2716
7b52a921
EGE
2717int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs,
2718 BlockDriverState *base,
2719 int64_t offset, int64_t bytes,
2720 int64_t *pnum, int64_t *map,
2721 BlockDriverState **file)
2722{
2723 IO_CODE();
2724 return bdrv_co_common_block_status_above(bs, base, false, true, offset,
2725 bytes, pnum, map, file, NULL);
2726}
2727
1b88457e
PB
2728int coroutine_fn bdrv_co_block_status(BlockDriverState *bs, int64_t offset,
2729 int64_t bytes, int64_t *pnum,
2730 int64_t *map, BlockDriverState **file)
c9ce8c4d 2731{
384a48fb 2732 IO_CODE();
1b88457e
PB
2733 return bdrv_co_block_status_above(bs, bdrv_filter_or_cow_bs(bs),
2734 offset, bytes, pnum, map, file);
ba3f0e25
FZ
2735}
2736
46cd1e8a
AG
2737/*
2738 * Check @bs (and its backing chain) to see if the range defined
2739 * by @offset and @bytes is known to read as zeroes.
2740 * Return 1 if that is the case, 0 otherwise and -errno on error.
2741 * This test is meant to be fast rather than accurate so returning 0
2742 * does not guarantee non-zero data.
2743 */
2744int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset,
2745 int64_t bytes)
2746{
2747 int ret;
2748 int64_t pnum = bytes;
384a48fb 2749 IO_CODE();
46cd1e8a
AG
2750
2751 if (!bytes) {
2752 return 1;
2753 }
2754
ce47ff20
AF
2755 ret = bdrv_co_common_block_status_above(bs, NULL, false, false, offset,
2756 bytes, &pnum, NULL, NULL, NULL);
46cd1e8a
AG
2757
2758 if (ret < 0) {
2759 return ret;
2760 }
2761
2762 return (pnum == bytes) && (ret & BDRV_BLOCK_ZERO);
2763}
2764
7b52a921
EGE
2765int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t offset,
2766 int64_t bytes, int64_t *pnum)
2767{
2768 int ret;
2769 int64_t dummy;
2770 IO_CODE();
2771
2772 ret = bdrv_co_common_block_status_above(bs, bs, true, false, offset,
2773 bytes, pnum ? pnum : &dummy, NULL,
2774 NULL, NULL);
2775 if (ret < 0) {
2776 return ret;
2777 }
2778 return !!(ret & BDRV_BLOCK_ALLOCATED);
2779}
2780
61007b31
SH
2781/*
2782 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
2783 *
a92b1b06
EB
2784 * Return a positive depth if (a prefix of) the given range is allocated
2785 * in any image between BASE and TOP (BASE is only included if include_base
2786 * is set). Depth 1 is TOP, 2 is the first backing layer, and so forth.
170d3bd3
AS
2787 * BASE can be NULL to check if the given offset is allocated in any
2788 * image of the chain. Return 0 otherwise, or negative errno on
2789 * failure.
61007b31 2790 *
51b0a488
EB
2791 * 'pnum' is set to the number of bytes (including and immediately
2792 * following the specified offset) that are known to be in the same
2793 * allocated/unallocated state. Note that a subsequent call starting
2794 * at 'offset + *pnum' may return the same allocation status (in other
2795 * words, the result is not necessarily the maximum possible range);
2796 * but 'pnum' will only be 0 when end of file is reached.
61007b31 2797 */
578ffa9f
PB
2798int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *bs,
2799 BlockDriverState *base,
2800 bool include_base, int64_t offset,
2801 int64_t bytes, int64_t *pnum)
61007b31 2802{
a92b1b06 2803 int depth;
7b52a921 2804 int ret;
384a48fb 2805 IO_CODE();
7b52a921 2806
578ffa9f
PB
2807 ret = bdrv_co_common_block_status_above(bs, base, include_base, false,
2808 offset, bytes, pnum, NULL, NULL,
2809 &depth);
7e7e5100
VSO
2810 if (ret < 0) {
2811 return ret;
61007b31
SH
2812 }
2813
a92b1b06
EB
2814 if (ret & BDRV_BLOCK_ALLOCATED) {
2815 return depth;
2816 }
2817 return 0;
61007b31
SH
2818}
2819
21c2283e 2820int coroutine_fn
b33b354f 2821bdrv_co_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
1a8ae822
KW
2822{
2823 BlockDriver *drv = bs->drv;
c4db2e25 2824 BlockDriverState *child_bs = bdrv_primary_bs(bs);
b984b296 2825 int ret;
1581a70d 2826 IO_CODE();
1b3ff9fe 2827 assert_bdrv_graph_readable();
b984b296
VSO
2828
2829 ret = bdrv_check_qiov_request(pos, qiov->size, qiov, 0, NULL);
2830 if (ret < 0) {
2831 return ret;
2832 }
dc88a467 2833
b33b354f
VSO
2834 if (!drv) {
2835 return -ENOMEDIUM;
2836 }
2837
dc88a467 2838 bdrv_inc_in_flight(bs);
1a8ae822 2839
ca5e2ad9
EGE
2840 if (drv->bdrv_co_load_vmstate) {
2841 ret = drv->bdrv_co_load_vmstate(bs, qiov, pos);
c4db2e25 2842 } else if (child_bs) {
b33b354f 2843 ret = bdrv_co_readv_vmstate(child_bs, qiov, pos);
b984b296
VSO
2844 } else {
2845 ret = -ENOTSUP;
1a8ae822
KW
2846 }
2847
dc88a467 2848 bdrv_dec_in_flight(bs);
b33b354f 2849
dc88a467 2850 return ret;
1a8ae822
KW
2851}
2852
b33b354f
VSO
2853int coroutine_fn
2854bdrv_co_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
61007b31 2855{
b33b354f
VSO
2856 BlockDriver *drv = bs->drv;
2857 BlockDriverState *child_bs = bdrv_primary_bs(bs);
b984b296 2858 int ret;
1581a70d 2859 IO_CODE();
1b3ff9fe 2860 assert_bdrv_graph_readable();
b984b296
VSO
2861
2862 ret = bdrv_check_qiov_request(pos, qiov->size, qiov, 0, NULL);
2863 if (ret < 0) {
2864 return ret;
2865 }
61007b31 2866
b33b354f
VSO
2867 if (!drv) {
2868 return -ENOMEDIUM;
b433d942
KW
2869 }
2870
b33b354f 2871 bdrv_inc_in_flight(bs);
61007b31 2872
ca5e2ad9
EGE
2873 if (drv->bdrv_co_save_vmstate) {
2874 ret = drv->bdrv_co_save_vmstate(bs, qiov, pos);
b33b354f
VSO
2875 } else if (child_bs) {
2876 ret = bdrv_co_writev_vmstate(child_bs, qiov, pos);
b984b296
VSO
2877 } else {
2878 ret = -ENOTSUP;
b33b354f
VSO
2879 }
2880
2881 bdrv_dec_in_flight(bs);
2882
2883 return ret;
61007b31
SH
2884}
2885
b33b354f 2886int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
61007b31 2887 int64_t pos, int size)
5ddda0b8 2888{
0d93ed08 2889 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
b33b354f 2890 int ret = bdrv_writev_vmstate(bs, &qiov, pos);
384a48fb 2891 IO_CODE();
b433d942 2892
b33b354f 2893 return ret < 0 ? ret : size;
5ddda0b8
KW
2894}
2895
b33b354f
VSO
2896int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2897 int64_t pos, int size)
61007b31 2898{
b33b354f
VSO
2899 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
2900 int ret = bdrv_readv_vmstate(bs, &qiov, pos);
384a48fb 2901 IO_CODE();
b33b354f
VSO
2902
2903 return ret < 0 ? ret : size;
61007b31
SH
2904}
2905
2906/**************************************************************/
2907/* async I/Os */
2908
652b0dd8
SH
2909/**
2910 * Synchronously cancels an acb. Must be called with the BQL held and the acb
2911 * must be processed with the BQL held too (IOThreads are not allowed).
2912 *
2913 * Use bdrv_aio_cancel_async() instead when possible.
2914 */
61007b31
SH
2915void bdrv_aio_cancel(BlockAIOCB *acb)
2916{
652b0dd8 2917 GLOBAL_STATE_CODE();
61007b31
SH
2918 qemu_aio_ref(acb);
2919 bdrv_aio_cancel_async(acb);
652b0dd8 2920 AIO_WAIT_WHILE_UNLOCKED(NULL, acb->refcnt > 1);
61007b31
SH
2921 qemu_aio_unref(acb);
2922}
2923
2924/* Async version of aio cancel. The caller is not blocked if the acb implements
2925 * cancel_async, otherwise we do nothing and let the request normally complete.
2926 * In either case the completion callback must be called. */
2927void bdrv_aio_cancel_async(BlockAIOCB *acb)
2928{
384a48fb 2929 IO_CODE();
61007b31
SH
2930 if (acb->aiocb_info->cancel_async) {
2931 acb->aiocb_info->cancel_async(acb);
2932 }
2933}
2934
61007b31
SH
2935/**************************************************************/
2936/* Coroutine block device emulation */
2937
61007b31
SH
2938int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2939{
883833e2
HR
2940 BdrvChild *primary_child = bdrv_primary_child(bs);
2941 BdrvChild *child;
49ca6259
FZ
2942 int current_gen;
2943 int ret = 0;
384a48fb 2944 IO_CODE();
49ca6259 2945
88095349 2946 assert_bdrv_graph_readable();
49ca6259 2947 bdrv_inc_in_flight(bs);
61007b31 2948
1e97be91 2949 if (!bdrv_co_is_inserted(bs) || bdrv_is_read_only(bs) ||
1b6bc94d 2950 bdrv_is_sg(bs)) {
49ca6259 2951 goto early_exit;
61007b31
SH
2952 }
2953
fa9185fc 2954 qemu_mutex_lock(&bs->reqs_lock);
d73415a3 2955 current_gen = qatomic_read(&bs->write_gen);
3ff2f67a
EY
2956
2957 /* Wait until any previous flushes are completed */
99723548 2958 while (bs->active_flush_req) {
3783fa3d 2959 qemu_co_queue_wait(&bs->flush_queue, &bs->reqs_lock);
3ff2f67a
EY
2960 }
2961
3783fa3d 2962 /* Flushes reach this point in nondecreasing current_gen order. */
99723548 2963 bs->active_flush_req = true;
fa9185fc 2964 qemu_mutex_unlock(&bs->reqs_lock);
3ff2f67a 2965
c32b82af
PD
2966 /* Write back all layers by calling one driver function */
2967 if (bs->drv->bdrv_co_flush) {
2968 ret = bs->drv->bdrv_co_flush(bs);
2969 goto out;
2970 }
2971
61007b31 2972 /* Write back cached data to the OS even with cache=unsafe */
17362398 2973 BLKDBG_CO_EVENT(primary_child, BLKDBG_FLUSH_TO_OS);
61007b31
SH
2974 if (bs->drv->bdrv_co_flush_to_os) {
2975 ret = bs->drv->bdrv_co_flush_to_os(bs);
2976 if (ret < 0) {
cdb5e315 2977 goto out;
61007b31
SH
2978 }
2979 }
2980
2981 /* But don't actually force it to the disk with cache=unsafe */
2982 if (bs->open_flags & BDRV_O_NO_FLUSH) {
883833e2 2983 goto flush_children;
61007b31
SH
2984 }
2985
3ff2f67a
EY
2986 /* Check if we really need to flush anything */
2987 if (bs->flushed_gen == current_gen) {
883833e2 2988 goto flush_children;
3ff2f67a
EY
2989 }
2990
17362398 2991 BLKDBG_CO_EVENT(primary_child, BLKDBG_FLUSH_TO_DISK);
d470ad42
HR
2992 if (!bs->drv) {
2993 /* bs->drv->bdrv_co_flush() might have ejected the BDS
2994 * (even in case of apparent success) */
2995 ret = -ENOMEDIUM;
2996 goto out;
2997 }
61007b31
SH
2998 if (bs->drv->bdrv_co_flush_to_disk) {
2999 ret = bs->drv->bdrv_co_flush_to_disk(bs);
3000 } else if (bs->drv->bdrv_aio_flush) {
3001 BlockAIOCB *acb;
3002 CoroutineIOCompletion co = {
3003 .coroutine = qemu_coroutine_self(),
3004 };
3005
3006 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
3007 if (acb == NULL) {
3008 ret = -EIO;
3009 } else {
3010 qemu_coroutine_yield();
3011 ret = co.ret;
3012 }
3013 } else {
3014 /*
3015 * Some block drivers always operate in either writethrough or unsafe
3016 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
3017 * know how the server works (because the behaviour is hardcoded or
3018 * depends on server-side configuration), so we can't ensure that
3019 * everything is safe on disk. Returning an error doesn't work because
3020 * that would break guests even if the server operates in writethrough
3021 * mode.
3022 *
3023 * Let's hope the user knows what he's doing.
3024 */
3025 ret = 0;
3026 }
3ff2f67a 3027
61007b31 3028 if (ret < 0) {
cdb5e315 3029 goto out;
61007b31
SH
3030 }
3031
3032 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
3033 * in the case of cache=unsafe, so there are no useless flushes.
3034 */
883833e2
HR
3035flush_children:
3036 ret = 0;
3037 QLIST_FOREACH(child, &bs->children, next) {
3038 if (child->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) {
3039 int this_child_ret = bdrv_co_flush(child->bs);
3040 if (!ret) {
3041 ret = this_child_ret;
3042 }
3043 }
3044 }
3045
cdb5e315 3046out:
3ff2f67a 3047 /* Notify any pending flushes that we have completed */
e6af1e08
KW
3048 if (ret == 0) {
3049 bs->flushed_gen = current_gen;
3050 }
3783fa3d 3051
fa9185fc 3052 qemu_mutex_lock(&bs->reqs_lock);
99723548 3053 bs->active_flush_req = false;
156af3ac
DL
3054 /* Return value is ignored - it's ok if wait queue is empty */
3055 qemu_co_queue_next(&bs->flush_queue);
fa9185fc 3056 qemu_mutex_unlock(&bs->reqs_lock);
3ff2f67a 3057
49ca6259 3058early_exit:
99723548 3059 bdrv_dec_in_flight(bs);
cdb5e315 3060 return ret;
61007b31
SH
3061}
3062
d93e5726
VSO
3063int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
3064 int64_t bytes)
61007b31 3065{
b1066c87 3066 BdrvTrackedRequest req;
39af49c0
VSO
3067 int ret;
3068 int64_t max_pdiscard;
3482b9bc 3069 int head, tail, align;
0b9fd3f4 3070 BlockDriverState *bs = child->bs;
384a48fb 3071 IO_CODE();
9a5a1c62 3072 assert_bdrv_graph_readable();
61007b31 3073
1e97be91 3074 if (!bs || !bs->drv || !bdrv_co_is_inserted(bs)) {
61007b31
SH
3075 return -ENOMEDIUM;
3076 }
3077
d6883bc9
VSO
3078 if (bdrv_has_readonly_bitmaps(bs)) {
3079 return -EPERM;
3080 }
3081
69b55e03 3082 ret = bdrv_check_request(offset, bytes, NULL);
8b117001
VSO
3083 if (ret < 0) {
3084 return ret;
61007b31
SH
3085 }
3086
61007b31
SH
3087 /* Do nothing if disabled. */
3088 if (!(bs->open_flags & BDRV_O_UNMAP)) {
3089 return 0;
3090 }
3091
02aefe43 3092 if (!bs->drv->bdrv_co_pdiscard && !bs->drv->bdrv_aio_pdiscard) {
61007b31
SH
3093 return 0;
3094 }
3095
0bc329fb
HR
3096 /* Invalidate the cached block-status data range if this discard overlaps */
3097 bdrv_bsc_invalidate_range(bs, offset, bytes);
3098
3482b9bc
EB
3099 /* Discard is advisory, but some devices track and coalesce
3100 * unaligned requests, so we must pass everything down rather than
3101 * round here. Still, most devices will just silently ignore
3102 * unaligned requests (by returning -ENOTSUP), so we must fragment
3103 * the request accordingly. */
02aefe43 3104 align = MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment);
b8d0a980
EB
3105 assert(align % bs->bl.request_alignment == 0);
3106 head = offset % align;
f5a5ca79 3107 tail = (offset + bytes) % align;
9f1963b3 3108
99723548 3109 bdrv_inc_in_flight(bs);
f5a5ca79 3110 tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_DISCARD);
50824995 3111
00695c27 3112 ret = bdrv_co_write_req_prepare(child, offset, bytes, &req, 0);
ec050f77
DL
3113 if (ret < 0) {
3114 goto out;
3115 }
3116
6a8f3dbb 3117 max_pdiscard = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard, INT64_MAX),
9f1963b3 3118 align);
3482b9bc 3119 assert(max_pdiscard >= bs->bl.request_alignment);
61007b31 3120
f5a5ca79 3121 while (bytes > 0) {
d93e5726 3122 int64_t num = bytes;
3482b9bc
EB
3123
3124 if (head) {
3125 /* Make small requests to get to alignment boundaries. */
f5a5ca79 3126 num = MIN(bytes, align - head);
3482b9bc
EB
3127 if (!QEMU_IS_ALIGNED(num, bs->bl.request_alignment)) {
3128 num %= bs->bl.request_alignment;
3129 }
3130 head = (head + num) % align;
3131 assert(num < max_pdiscard);
3132 } else if (tail) {
3133 if (num > align) {
3134 /* Shorten the request to the last aligned cluster. */
3135 num -= tail;
3136 } else if (!QEMU_IS_ALIGNED(tail, bs->bl.request_alignment) &&
3137 tail > bs->bl.request_alignment) {
3138 tail %= bs->bl.request_alignment;
3139 num -= tail;
3140 }
3141 }
3142 /* limit request size */
3143 if (num > max_pdiscard) {
3144 num = max_pdiscard;
3145 }
61007b31 3146
d470ad42
HR
3147 if (!bs->drv) {
3148 ret = -ENOMEDIUM;
3149 goto out;
3150 }
47a5486d
EB
3151 if (bs->drv->bdrv_co_pdiscard) {
3152 ret = bs->drv->bdrv_co_pdiscard(bs, offset, num);
61007b31
SH
3153 } else {
3154 BlockAIOCB *acb;
3155 CoroutineIOCompletion co = {
3156 .coroutine = qemu_coroutine_self(),
3157 };
3158
4da444a0
EB
3159 acb = bs->drv->bdrv_aio_pdiscard(bs, offset, num,
3160 bdrv_co_io_em_complete, &co);
61007b31 3161 if (acb == NULL) {
b1066c87
FZ
3162 ret = -EIO;
3163 goto out;
61007b31
SH
3164 } else {
3165 qemu_coroutine_yield();
3166 ret = co.ret;
3167 }
3168 }
3169 if (ret && ret != -ENOTSUP) {
b1066c87 3170 goto out;
61007b31
SH
3171 }
3172
9f1963b3 3173 offset += num;
f5a5ca79 3174 bytes -= num;
61007b31 3175 }
b1066c87
FZ
3176 ret = 0;
3177out:
00695c27 3178 bdrv_co_write_req_finish(child, req.offset, req.bytes, &req, ret);
b1066c87 3179 tracked_request_end(&req);
99723548 3180 bdrv_dec_in_flight(bs);
b1066c87 3181 return ret;
61007b31
SH
3182}
3183
881a4c55 3184int coroutine_fn bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf)
61007b31
SH
3185{
3186 BlockDriver *drv = bs->drv;
5c5ae76a
FZ
3187 CoroutineIOCompletion co = {
3188 .coroutine = qemu_coroutine_self(),
3189 };
3190 BlockAIOCB *acb;
384a48fb 3191 IO_CODE();
26c518ab 3192 assert_bdrv_graph_readable();
61007b31 3193
99723548 3194 bdrv_inc_in_flight(bs);
16a389dc 3195 if (!drv || (!drv->bdrv_aio_ioctl && !drv->bdrv_co_ioctl)) {
5c5ae76a
FZ
3196 co.ret = -ENOTSUP;
3197 goto out;
3198 }
3199
16a389dc
KW
3200 if (drv->bdrv_co_ioctl) {
3201 co.ret = drv->bdrv_co_ioctl(bs, req, buf);
3202 } else {
3203 acb = drv->bdrv_aio_ioctl(bs, req, buf, bdrv_co_io_em_complete, &co);
3204 if (!acb) {
3205 co.ret = -ENOTSUP;
3206 goto out;
3207 }
3208 qemu_coroutine_yield();
5c5ae76a 3209 }
5c5ae76a 3210out:
99723548 3211 bdrv_dec_in_flight(bs);
5c5ae76a
FZ
3212 return co.ret;
3213}
3214
6d43eaa3
SL
3215int coroutine_fn bdrv_co_zone_report(BlockDriverState *bs, int64_t offset,
3216 unsigned int *nr_zones,
3217 BlockZoneDescriptor *zones)
3218{
3219 BlockDriver *drv = bs->drv;
3220 CoroutineIOCompletion co = {
3221 .coroutine = qemu_coroutine_self(),
3222 };
3223 IO_CODE();
3224
3225 bdrv_inc_in_flight(bs);
3226 if (!drv || !drv->bdrv_co_zone_report || bs->bl.zoned == BLK_Z_NONE) {
3227 co.ret = -ENOTSUP;
3228 goto out;
3229 }
3230 co.ret = drv->bdrv_co_zone_report(bs, offset, nr_zones, zones);
3231out:
3232 bdrv_dec_in_flight(bs);
3233 return co.ret;
3234}
3235
3236int coroutine_fn bdrv_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
3237 int64_t offset, int64_t len)
3238{
3239 BlockDriver *drv = bs->drv;
3240 CoroutineIOCompletion co = {
3241 .coroutine = qemu_coroutine_self(),
3242 };
3243 IO_CODE();
3244
3245 bdrv_inc_in_flight(bs);
3246 if (!drv || !drv->bdrv_co_zone_mgmt || bs->bl.zoned == BLK_Z_NONE) {
3247 co.ret = -ENOTSUP;
3248 goto out;
3249 }
3250 co.ret = drv->bdrv_co_zone_mgmt(bs, op, offset, len);
3251out:
3252 bdrv_dec_in_flight(bs);
3253 return co.ret;
3254}
3255
4751d09a
SL
3256int coroutine_fn bdrv_co_zone_append(BlockDriverState *bs, int64_t *offset,
3257 QEMUIOVector *qiov,
3258 BdrvRequestFlags flags)
3259{
3260 int ret;
3261 BlockDriver *drv = bs->drv;
3262 CoroutineIOCompletion co = {
3263 .coroutine = qemu_coroutine_self(),
3264 };
3265 IO_CODE();
3266
3267 ret = bdrv_check_qiov_request(*offset, qiov->size, qiov, 0, NULL);
3268 if (ret < 0) {
3269 return ret;
3270 }
3271
3272 bdrv_inc_in_flight(bs);
3273 if (!drv || !drv->bdrv_co_zone_append || bs->bl.zoned == BLK_Z_NONE) {
3274 co.ret = -ENOTSUP;
3275 goto out;
3276 }
3277 co.ret = drv->bdrv_co_zone_append(bs, offset, qiov, flags);
3278out:
3279 bdrv_dec_in_flight(bs);
3280 return co.ret;
3281}
3282
61007b31
SH
3283void *qemu_blockalign(BlockDriverState *bs, size_t size)
3284{
384a48fb 3285 IO_CODE();
61007b31
SH
3286 return qemu_memalign(bdrv_opt_mem_align(bs), size);
3287}
3288
3289void *qemu_blockalign0(BlockDriverState *bs, size_t size)
3290{
384a48fb 3291 IO_CODE();
61007b31
SH
3292 return memset(qemu_blockalign(bs, size), 0, size);
3293}
3294
3295void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
3296{
3297 size_t align = bdrv_opt_mem_align(bs);
384a48fb 3298 IO_CODE();
61007b31
SH
3299
3300 /* Ensure that NULL is never returned on success */
3301 assert(align > 0);
3302 if (size == 0) {
3303 size = align;
3304 }
3305
3306 return qemu_try_memalign(align, size);
3307}
3308
3309void *qemu_try_blockalign0(BlockDriverState *bs, size_t size)
3310{
3311 void *mem = qemu_try_blockalign(bs, size);
384a48fb 3312 IO_CODE();
61007b31
SH
3313
3314 if (mem) {
3315 memset(mem, 0, size);
3316 }
3317
3318 return mem;
3319}
3320
f4ec04ba 3321/* Helper that undoes bdrv_register_buf() when it fails partway through */
d9249c25
KW
3322static void GRAPH_RDLOCK
3323bdrv_register_buf_rollback(BlockDriverState *bs, void *host, size_t size,
3324 BdrvChild *final_child)
f4ec04ba
SH
3325{
3326 BdrvChild *child;
3327
d9249c25
KW
3328 GLOBAL_STATE_CODE();
3329 assert_bdrv_graph_readable();
3330
f4ec04ba
SH
3331 QLIST_FOREACH(child, &bs->children, next) {
3332 if (child == final_child) {
3333 break;
3334 }
3335
3336 bdrv_unregister_buf(child->bs, host, size);
3337 }
3338
3339 if (bs->drv && bs->drv->bdrv_unregister_buf) {
3340 bs->drv->bdrv_unregister_buf(bs, host, size);
3341 }
3342}
3343
3344bool bdrv_register_buf(BlockDriverState *bs, void *host, size_t size,
3345 Error **errp)
23d0ba93
FZ
3346{
3347 BdrvChild *child;
3348
f791bf7f 3349 GLOBAL_STATE_CODE();
d9249c25
KW
3350 GRAPH_RDLOCK_GUARD_MAINLOOP();
3351
23d0ba93 3352 if (bs->drv && bs->drv->bdrv_register_buf) {
f4ec04ba
SH
3353 if (!bs->drv->bdrv_register_buf(bs, host, size, errp)) {
3354 return false;
3355 }
23d0ba93
FZ
3356 }
3357 QLIST_FOREACH(child, &bs->children, next) {
f4ec04ba
SH
3358 if (!bdrv_register_buf(child->bs, host, size, errp)) {
3359 bdrv_register_buf_rollback(bs, host, size, child);
3360 return false;
3361 }
23d0ba93 3362 }
f4ec04ba 3363 return true;
23d0ba93
FZ
3364}
3365
4f384011 3366void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size)
23d0ba93
FZ
3367{
3368 BdrvChild *child;
3369
f791bf7f 3370 GLOBAL_STATE_CODE();
d9249c25
KW
3371 GRAPH_RDLOCK_GUARD_MAINLOOP();
3372
23d0ba93 3373 if (bs->drv && bs->drv->bdrv_unregister_buf) {
4f384011 3374 bs->drv->bdrv_unregister_buf(bs, host, size);
23d0ba93
FZ
3375 }
3376 QLIST_FOREACH(child, &bs->children, next) {
4f384011 3377 bdrv_unregister_buf(child->bs, host, size);
23d0ba93
FZ
3378 }
3379}
fcc67678 3380
abaf8b75 3381static int coroutine_fn GRAPH_RDLOCK bdrv_co_copy_range_internal(
a5215b8f
VSO
3382 BdrvChild *src, int64_t src_offset, BdrvChild *dst,
3383 int64_t dst_offset, int64_t bytes,
67b51fb9
VSO
3384 BdrvRequestFlags read_flags, BdrvRequestFlags write_flags,
3385 bool recurse_src)
fcc67678 3386{
999658a0 3387 BdrvTrackedRequest req;
fcc67678 3388 int ret;
742bf09b 3389 assert_bdrv_graph_readable();
fcc67678 3390
fe0480d6
KW
3391 /* TODO We can support BDRV_REQ_NO_FALLBACK here */
3392 assert(!(read_flags & BDRV_REQ_NO_FALLBACK));
3393 assert(!(write_flags & BDRV_REQ_NO_FALLBACK));
45e62b46
VSO
3394 assert(!(read_flags & BDRV_REQ_NO_WAIT));
3395 assert(!(write_flags & BDRV_REQ_NO_WAIT));
fe0480d6 3396
1e97be91 3397 if (!dst || !dst->bs || !bdrv_co_is_inserted(dst->bs)) {
fcc67678
FZ
3398 return -ENOMEDIUM;
3399 }
63f4ad11 3400 ret = bdrv_check_request32(dst_offset, bytes, NULL, 0);
fcc67678
FZ
3401 if (ret) {
3402 return ret;
3403 }
67b51fb9
VSO
3404 if (write_flags & BDRV_REQ_ZERO_WRITE) {
3405 return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, write_flags);
fcc67678
FZ
3406 }
3407
1e97be91 3408 if (!src || !src->bs || !bdrv_co_is_inserted(src->bs)) {
d4d3e5a0
FZ
3409 return -ENOMEDIUM;
3410 }
63f4ad11 3411 ret = bdrv_check_request32(src_offset, bytes, NULL, 0);
d4d3e5a0
FZ
3412 if (ret) {
3413 return ret;
3414 }
3415
fcc67678
FZ
3416 if (!src->bs->drv->bdrv_co_copy_range_from
3417 || !dst->bs->drv->bdrv_co_copy_range_to
3418 || src->bs->encrypted || dst->bs->encrypted) {
3419 return -ENOTSUP;
3420 }
37aec7d7 3421
fcc67678 3422 if (recurse_src) {
999658a0
VSO
3423 bdrv_inc_in_flight(src->bs);
3424 tracked_request_begin(&req, src->bs, src_offset, bytes,
3425 BDRV_TRACKED_READ);
3426
09d2f948
VSO
3427 /* BDRV_REQ_SERIALISING is only for write operation */
3428 assert(!(read_flags & BDRV_REQ_SERIALISING));
c53cb427 3429 bdrv_wait_serialising_requests(&req);
999658a0 3430
37aec7d7
FZ
3431 ret = src->bs->drv->bdrv_co_copy_range_from(src->bs,
3432 src, src_offset,
3433 dst, dst_offset,
67b51fb9
VSO
3434 bytes,
3435 read_flags, write_flags);
999658a0
VSO
3436
3437 tracked_request_end(&req);
3438 bdrv_dec_in_flight(src->bs);
fcc67678 3439 } else {
999658a0
VSO
3440 bdrv_inc_in_flight(dst->bs);
3441 tracked_request_begin(&req, dst->bs, dst_offset, bytes,
3442 BDRV_TRACKED_WRITE);
0eb1e891
FZ
3443 ret = bdrv_co_write_req_prepare(dst, dst_offset, bytes, &req,
3444 write_flags);
3445 if (!ret) {
3446 ret = dst->bs->drv->bdrv_co_copy_range_to(dst->bs,
3447 src, src_offset,
3448 dst, dst_offset,
3449 bytes,
3450 read_flags, write_flags);
3451 }
3452 bdrv_co_write_req_finish(dst, dst_offset, bytes, &req, ret);
999658a0
VSO
3453 tracked_request_end(&req);
3454 bdrv_dec_in_flight(dst->bs);
fcc67678 3455 }
999658a0 3456
37aec7d7 3457 return ret;
fcc67678
FZ
3458}
3459
3460/* Copy range from @src to @dst.
3461 *
3462 * See the comment of bdrv_co_copy_range for the parameter and return value
3463 * semantics. */
a5215b8f
VSO
3464int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, int64_t src_offset,
3465 BdrvChild *dst, int64_t dst_offset,
3466 int64_t bytes,
67b51fb9
VSO
3467 BdrvRequestFlags read_flags,
3468 BdrvRequestFlags write_flags)
fcc67678 3469{
967d7905 3470 IO_CODE();
742bf09b 3471 assert_bdrv_graph_readable();
ecc983a5
FZ
3472 trace_bdrv_co_copy_range_from(src, src_offset, dst, dst_offset, bytes,
3473 read_flags, write_flags);
fcc67678 3474 return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
67b51fb9 3475 bytes, read_flags, write_flags, true);
fcc67678
FZ
3476}
3477
3478/* Copy range from @src to @dst.
3479 *
3480 * See the comment of bdrv_co_copy_range for the parameter and return value
3481 * semantics. */
a5215b8f
VSO
3482int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, int64_t src_offset,
3483 BdrvChild *dst, int64_t dst_offset,
3484 int64_t bytes,
67b51fb9
VSO
3485 BdrvRequestFlags read_flags,
3486 BdrvRequestFlags write_flags)
fcc67678 3487{
967d7905 3488 IO_CODE();
742bf09b 3489 assert_bdrv_graph_readable();
ecc983a5
FZ
3490 trace_bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
3491 read_flags, write_flags);
fcc67678 3492 return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
67b51fb9 3493 bytes, read_flags, write_flags, false);
fcc67678
FZ
3494}
3495
a5215b8f
VSO
3496int coroutine_fn bdrv_co_copy_range(BdrvChild *src, int64_t src_offset,
3497 BdrvChild *dst, int64_t dst_offset,
3498 int64_t bytes, BdrvRequestFlags read_flags,
67b51fb9 3499 BdrvRequestFlags write_flags)
fcc67678 3500{
384a48fb 3501 IO_CODE();
742bf09b
EGE
3502 assert_bdrv_graph_readable();
3503
37aec7d7
FZ
3504 return bdrv_co_copy_range_from(src, src_offset,
3505 dst, dst_offset,
67b51fb9 3506 bytes, read_flags, write_flags);
fcc67678 3507}
3d9f2d2a
KW
3508
3509static void bdrv_parent_cb_resize(BlockDriverState *bs)
3510{
3511 BdrvChild *c;
3512 QLIST_FOREACH(c, &bs->parents, next_parent) {
bd86fb99
HR
3513 if (c->klass->resize) {
3514 c->klass->resize(c);
3d9f2d2a
KW
3515 }
3516 }
3517}
3518
3519/**
3520 * Truncate file to 'offset' bytes (needed only for file protocols)
c80d8b06
HR
3521 *
3522 * If 'exact' is true, the file must be resized to exactly the given
3523 * 'offset'. Otherwise, it is sufficient for the node to be at least
3524 * 'offset' bytes in length.
3d9f2d2a 3525 */
c80d8b06 3526int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
7b8e4857
KW
3527 PreallocMode prealloc, BdrvRequestFlags flags,
3528 Error **errp)
3d9f2d2a
KW
3529{
3530 BlockDriverState *bs = child->bs;
23b93525 3531 BdrvChild *filtered, *backing;
3d9f2d2a 3532 BlockDriver *drv = bs->drv;
1bc5f09f
KW
3533 BdrvTrackedRequest req;
3534 int64_t old_size, new_bytes;
3d9f2d2a 3535 int ret;
384a48fb 3536 IO_CODE();
c2b8e315 3537 assert_bdrv_graph_readable();
3d9f2d2a
KW
3538
3539 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
3540 if (!drv) {
3541 error_setg(errp, "No medium inserted");
3542 return -ENOMEDIUM;
3543 }
3544 if (offset < 0) {
3545 error_setg(errp, "Image size cannot be negative");
3546 return -EINVAL;
3547 }
3548
69b55e03 3549 ret = bdrv_check_request(offset, 0, errp);
8b117001 3550 if (ret < 0) {
8b117001
VSO
3551 return ret;
3552 }
3553
0af02bd1 3554 old_size = bdrv_co_getlength(bs);
1bc5f09f
KW
3555 if (old_size < 0) {
3556 error_setg_errno(errp, -old_size, "Failed to get old image size");
3557 return old_size;
3558 }
3559
97efa869
EB
3560 if (bdrv_is_read_only(bs)) {
3561 error_setg(errp, "Image is read-only");
3562 return -EACCES;
3563 }
3564
1bc5f09f
KW
3565 if (offset > old_size) {
3566 new_bytes = offset - old_size;
3567 } else {
3568 new_bytes = 0;
3569 }
3570
3d9f2d2a 3571 bdrv_inc_in_flight(bs);
5416a11e
FZ
3572 tracked_request_begin(&req, bs, offset - new_bytes, new_bytes,
3573 BDRV_TRACKED_TRUNCATE);
1bc5f09f
KW
3574
3575 /* If we are growing the image and potentially using preallocation for the
3576 * new area, we need to make sure that no write requests are made to it
3577 * concurrently or they might be overwritten by preallocation. */
3578 if (new_bytes) {
8ac5aab2 3579 bdrv_make_request_serialising(&req, 1);
cd47d792 3580 }
cd47d792
FZ
3581 ret = bdrv_co_write_req_prepare(child, offset - new_bytes, new_bytes, &req,
3582 0);
3583 if (ret < 0) {
3584 error_setg_errno(errp, -ret,
3585 "Failed to prepare request for truncation");
3586 goto out;
1bc5f09f 3587 }
3d9f2d2a 3588
93393e69 3589 filtered = bdrv_filter_child(bs);
23b93525 3590 backing = bdrv_cow_child(bs);
93393e69 3591
955c7d66
KW
3592 /*
3593 * If the image has a backing file that is large enough that it would
3594 * provide data for the new area, we cannot leave it unallocated because
3595 * then the backing file content would become visible. Instead, zero-fill
3596 * the new area.
3597 *
3598 * Note that if the image has a backing file, but was opened without the
3599 * backing file, taking care of keeping things consistent with that backing
3600 * file is the user's responsibility.
3601 */
23b93525 3602 if (new_bytes && backing) {
955c7d66
KW
3603 int64_t backing_len;
3604
bd53086e 3605 backing_len = bdrv_co_getlength(backing->bs);
955c7d66
KW
3606 if (backing_len < 0) {
3607 ret = backing_len;
3608 error_setg_errno(errp, -ret, "Could not get backing file size");
3609 goto out;
3610 }
3611
3612 if (backing_len > old_size) {
3613 flags |= BDRV_REQ_ZERO_WRITE;
3614 }
3615 }
3616
6b7e8f8b 3617 if (drv->bdrv_co_truncate) {
92b92799
KW
3618 if (flags & ~bs->supported_truncate_flags) {
3619 error_setg(errp, "Block driver does not support requested flags");
3620 ret = -ENOTSUP;
3621 goto out;
3622 }
3623 ret = drv->bdrv_co_truncate(bs, offset, exact, prealloc, flags, errp);
93393e69
HR
3624 } else if (filtered) {
3625 ret = bdrv_co_truncate(filtered, offset, exact, prealloc, flags, errp);
6b7e8f8b 3626 } else {
3d9f2d2a
KW
3627 error_setg(errp, "Image format driver does not support resize");
3628 ret = -ENOTSUP;
3629 goto out;
3630 }
3d9f2d2a
KW
3631 if (ret < 0) {
3632 goto out;
3633 }
6b7e8f8b 3634
bd53086e 3635 ret = bdrv_co_refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
3d9f2d2a
KW
3636 if (ret < 0) {
3637 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3638 } else {
3639 offset = bs->total_sectors * BDRV_SECTOR_SIZE;
3640 }
c057960c
EGE
3641 /*
3642 * It's possible that truncation succeeded but bdrv_refresh_total_sectors
cd47d792 3643 * failed, but the latter doesn't affect how we should finish the request.
c057960c
EGE
3644 * Pass 0 as the last parameter so that dirty bitmaps etc. are handled.
3645 */
cd47d792 3646 bdrv_co_write_req_finish(child, offset - new_bytes, new_bytes, &req, 0);
3d9f2d2a
KW
3647
3648out:
1bc5f09f 3649 tracked_request_end(&req);
3d9f2d2a 3650 bdrv_dec_in_flight(bs);
1bc5f09f 3651
3d9f2d2a
KW
3652 return ret;
3653}
bd54669a
VSO
3654
3655void bdrv_cancel_in_flight(BlockDriverState *bs)
3656{
f791bf7f 3657 GLOBAL_STATE_CODE();
bd54669a
VSO
3658 if (!bs || !bs->drv) {
3659 return;
3660 }
3661
3662 if (bs->drv->bdrv_cancel_in_flight) {
3663 bs->drv->bdrv_cancel_in_flight(bs);
3664 }
3665}
ce14f3b4
VSO
3666
3667int coroutine_fn
3668bdrv_co_preadv_snapshot(BdrvChild *child, int64_t offset, int64_t bytes,
3669 QEMUIOVector *qiov, size_t qiov_offset)
3670{
3671 BlockDriverState *bs = child->bs;
3672 BlockDriver *drv = bs->drv;
3673 int ret;
3674 IO_CODE();
7b9e8b22 3675 assert_bdrv_graph_readable();
ce14f3b4
VSO
3676
3677 if (!drv) {
3678 return -ENOMEDIUM;
3679 }
3680
3681 if (!drv->bdrv_co_preadv_snapshot) {
3682 return -ENOTSUP;
3683 }
3684
3685 bdrv_inc_in_flight(bs);
3686 ret = drv->bdrv_co_preadv_snapshot(bs, offset, bytes, qiov, qiov_offset);
3687 bdrv_dec_in_flight(bs);
3688
3689 return ret;
3690}
3691
3692int coroutine_fn
3693bdrv_co_snapshot_block_status(BlockDriverState *bs,
3694 bool want_zero, int64_t offset, int64_t bytes,
3695 int64_t *pnum, int64_t *map,
3696 BlockDriverState **file)
3697{
3698 BlockDriver *drv = bs->drv;
3699 int ret;
3700 IO_CODE();
7b9e8b22 3701 assert_bdrv_graph_readable();
ce14f3b4
VSO
3702
3703 if (!drv) {
3704 return -ENOMEDIUM;
3705 }
3706
3707 if (!drv->bdrv_co_snapshot_block_status) {
3708 return -ENOTSUP;
3709 }
3710
3711 bdrv_inc_in_flight(bs);
3712 ret = drv->bdrv_co_snapshot_block_status(bs, want_zero, offset, bytes,
3713 pnum, map, file);
3714 bdrv_dec_in_flight(bs);
3715
3716 return ret;
3717}
3718
3719int coroutine_fn
3720bdrv_co_pdiscard_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes)
3721{
3722 BlockDriver *drv = bs->drv;
3723 int ret;
3724 IO_CODE();
9a5a1c62 3725 assert_bdrv_graph_readable();
ce14f3b4
VSO
3726
3727 if (!drv) {
3728 return -ENOMEDIUM;
3729 }
3730
3731 if (!drv->bdrv_co_pdiscard_snapshot) {
3732 return -ENOTSUP;
3733 }
3734
3735 bdrv_inc_in_flight(bs);
3736 ret = drv->bdrv_co_pdiscard_snapshot(bs, offset, bytes);
3737 bdrv_dec_in_flight(bs);
3738
3739 return ret;
3740}