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