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