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