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