]>
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" |
f348b6d1 | 32 | #include "qemu/cutils.h" |
da34e65c | 33 | #include "qapi/error.h" |
d49b6836 | 34 | #include "qemu/error-report.h" |
61007b31 SH |
35 | |
36 | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ | |
37 | ||
cb2e2878 EB |
38 | /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */ |
39 | #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS) | |
40 | ||
d05aa8bb | 41 | static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 42 | int64_t offset, int bytes, BdrvRequestFlags flags); |
61007b31 | 43 | |
0152bf40 | 44 | void bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore) |
61007b31 | 45 | { |
02d21300 | 46 | BdrvChild *c, *next; |
27ccdd52 | 47 | |
02d21300 | 48 | QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) { |
0152bf40 KW |
49 | if (c == ignore) { |
50 | continue; | |
51 | } | |
c2066af0 KW |
52 | if (c->role->drained_begin) { |
53 | c->role->drained_begin(c); | |
54 | } | |
ce0f1412 PB |
55 | } |
56 | } | |
61007b31 | 57 | |
0152bf40 | 58 | void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore) |
ce0f1412 | 59 | { |
02d21300 | 60 | BdrvChild *c, *next; |
27ccdd52 | 61 | |
02d21300 | 62 | QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) { |
0152bf40 KW |
63 | if (c == ignore) { |
64 | continue; | |
65 | } | |
c2066af0 KW |
66 | if (c->role->drained_end) { |
67 | c->role->drained_end(c); | |
68 | } | |
27ccdd52 | 69 | } |
61007b31 SH |
70 | } |
71 | ||
89bd0305 KW |
72 | static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore) |
73 | { | |
74 | BdrvChild *c, *next; | |
75 | bool busy = false; | |
76 | ||
77 | QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) { | |
78 | if (c == ignore) { | |
79 | continue; | |
80 | } | |
81 | if (c->role->drained_poll) { | |
82 | busy |= c->role->drained_poll(c); | |
83 | } | |
84 | } | |
85 | ||
86 | return busy; | |
87 | } | |
88 | ||
d9e0dfa2 EB |
89 | static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src) |
90 | { | |
91 | dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer); | |
92 | dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer); | |
93 | dst->opt_mem_alignment = MAX(dst->opt_mem_alignment, | |
94 | src->opt_mem_alignment); | |
95 | dst->min_mem_alignment = MAX(dst->min_mem_alignment, | |
96 | src->min_mem_alignment); | |
97 | dst->max_iov = MIN_NON_ZERO(dst->max_iov, src->max_iov); | |
98 | } | |
99 | ||
61007b31 SH |
100 | void bdrv_refresh_limits(BlockDriverState *bs, Error **errp) |
101 | { | |
102 | BlockDriver *drv = bs->drv; | |
103 | Error *local_err = NULL; | |
104 | ||
105 | memset(&bs->bl, 0, sizeof(bs->bl)); | |
106 | ||
107 | if (!drv) { | |
108 | return; | |
109 | } | |
110 | ||
79ba8c98 | 111 | /* Default alignment based on whether driver has byte interface */ |
e31f6864 EB |
112 | bs->bl.request_alignment = (drv->bdrv_co_preadv || |
113 | drv->bdrv_aio_preadv) ? 1 : 512; | |
79ba8c98 | 114 | |
61007b31 SH |
115 | /* Take some limits from the children as a default */ |
116 | if (bs->file) { | |
9a4f4c31 | 117 | bdrv_refresh_limits(bs->file->bs, &local_err); |
61007b31 SH |
118 | if (local_err) { |
119 | error_propagate(errp, local_err); | |
120 | return; | |
121 | } | |
d9e0dfa2 | 122 | bdrv_merge_limits(&bs->bl, &bs->file->bs->bl); |
61007b31 | 123 | } else { |
4196d2f0 | 124 | bs->bl.min_mem_alignment = 512; |
459b4e66 | 125 | bs->bl.opt_mem_alignment = getpagesize(); |
bd44feb7 SH |
126 | |
127 | /* Safe default since most protocols use readv()/writev()/etc */ | |
128 | bs->bl.max_iov = IOV_MAX; | |
61007b31 SH |
129 | } |
130 | ||
760e0063 KW |
131 | if (bs->backing) { |
132 | bdrv_refresh_limits(bs->backing->bs, &local_err); | |
61007b31 SH |
133 | if (local_err) { |
134 | error_propagate(errp, local_err); | |
135 | return; | |
136 | } | |
d9e0dfa2 | 137 | bdrv_merge_limits(&bs->bl, &bs->backing->bs->bl); |
61007b31 SH |
138 | } |
139 | ||
140 | /* Then let the driver override it */ | |
141 | if (drv->bdrv_refresh_limits) { | |
142 | drv->bdrv_refresh_limits(bs, errp); | |
143 | } | |
144 | } | |
145 | ||
146 | /** | |
147 | * The copy-on-read flag is actually a reference count so multiple users may | |
148 | * use the feature without worrying about clobbering its previous state. | |
149 | * Copy-on-read stays enabled until all users have called to disable it. | |
150 | */ | |
151 | void bdrv_enable_copy_on_read(BlockDriverState *bs) | |
152 | { | |
d3faa13e | 153 | atomic_inc(&bs->copy_on_read); |
61007b31 SH |
154 | } |
155 | ||
156 | void bdrv_disable_copy_on_read(BlockDriverState *bs) | |
157 | { | |
d3faa13e PB |
158 | int old = atomic_fetch_dec(&bs->copy_on_read); |
159 | assert(old >= 1); | |
61007b31 SH |
160 | } |
161 | ||
61124f03 PB |
162 | typedef struct { |
163 | Coroutine *co; | |
164 | BlockDriverState *bs; | |
165 | bool done; | |
481cad48 | 166 | bool begin; |
b0165585 | 167 | bool recursive; |
0152bf40 | 168 | BdrvChild *parent; |
61124f03 PB |
169 | } BdrvCoDrainData; |
170 | ||
171 | static void coroutine_fn bdrv_drain_invoke_entry(void *opaque) | |
172 | { | |
173 | BdrvCoDrainData *data = opaque; | |
174 | BlockDriverState *bs = data->bs; | |
175 | ||
481cad48 | 176 | if (data->begin) { |
f8ea8dac | 177 | bs->drv->bdrv_co_drain_begin(bs); |
481cad48 MP |
178 | } else { |
179 | bs->drv->bdrv_co_drain_end(bs); | |
180 | } | |
61124f03 PB |
181 | |
182 | /* Set data->done before reading bs->wakeup. */ | |
183 | atomic_mb_set(&data->done, true); | |
184 | bdrv_wakeup(bs); | |
185 | } | |
186 | ||
db0289b9 | 187 | /* Recursively call BlockDriver.bdrv_co_drain_begin/end callbacks */ |
7d40d9ef | 188 | static void bdrv_drain_invoke(BlockDriverState *bs, bool begin) |
61124f03 | 189 | { |
481cad48 | 190 | BdrvCoDrainData data = { .bs = bs, .done = false, .begin = begin}; |
61124f03 | 191 | |
f8ea8dac | 192 | if (!bs->drv || (begin && !bs->drv->bdrv_co_drain_begin) || |
481cad48 | 193 | (!begin && !bs->drv->bdrv_co_drain_end)) { |
61124f03 PB |
194 | return; |
195 | } | |
196 | ||
197 | data.co = qemu_coroutine_create(bdrv_drain_invoke_entry, &data); | |
198 | bdrv_coroutine_enter(bs, data.co); | |
199 | BDRV_POLL_WHILE(bs, !data.done); | |
200 | } | |
201 | ||
1cc8e54a | 202 | /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */ |
89bd0305 KW |
203 | bool bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent) |
204 | { | |
205 | if (bdrv_parent_drained_poll(bs, ignore_parent)) { | |
206 | return true; | |
207 | } | |
208 | ||
209 | return atomic_read(&bs->in_flight); | |
210 | } | |
211 | ||
212 | static bool bdrv_drain_poll_top_level(BlockDriverState *bs, | |
213 | BdrvChild *ignore_parent) | |
1cc8e54a KW |
214 | { |
215 | /* Execute pending BHs first and check everything else only after the BHs | |
216 | * have executed. */ | |
217 | while (aio_poll(bs->aio_context, false)); | |
89bd0305 KW |
218 | |
219 | return bdrv_drain_poll(bs, ignore_parent); | |
1cc8e54a KW |
220 | } |
221 | ||
89bd0305 | 222 | static bool bdrv_drain_recurse(BlockDriverState *bs, BdrvChild *parent) |
67da1dc5 | 223 | { |
178bd438 | 224 | BdrvChild *child, *tmp; |
d42cf288 PB |
225 | bool waited; |
226 | ||
481cad48 | 227 | /* Wait for drained requests to finish */ |
89bd0305 | 228 | waited = BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, parent)); |
d42cf288 | 229 | |
178bd438 FZ |
230 | QLIST_FOREACH_SAFE(child, &bs->children, next, tmp) { |
231 | BlockDriverState *bs = child->bs; | |
232 | bool in_main_loop = | |
233 | qemu_get_current_aio_context() == qemu_get_aio_context(); | |
234 | assert(bs->refcnt > 0); | |
235 | if (in_main_loop) { | |
236 | /* In case the recursive bdrv_drain_recurse processes a | |
237 | * block_job_defer_to_main_loop BH and modifies the graph, | |
238 | * let's hold a reference to bs until we are done. | |
239 | * | |
240 | * IOThread doesn't have such a BH, and it is not safe to call | |
241 | * bdrv_unref without BQL, so skip doing it there. | |
242 | */ | |
243 | bdrv_ref(bs); | |
244 | } | |
89bd0305 | 245 | waited |= bdrv_drain_recurse(bs, child); |
178bd438 FZ |
246 | if (in_main_loop) { |
247 | bdrv_unref(bs); | |
248 | } | |
67da1dc5 | 249 | } |
d42cf288 PB |
250 | |
251 | return waited; | |
67da1dc5 FZ |
252 | } |
253 | ||
b0165585 KW |
254 | static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive, |
255 | BdrvChild *parent); | |
256 | static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive, | |
257 | BdrvChild *parent); | |
0152bf40 | 258 | |
a77fd4bb FZ |
259 | static void bdrv_co_drain_bh_cb(void *opaque) |
260 | { | |
261 | BdrvCoDrainData *data = opaque; | |
262 | Coroutine *co = data->co; | |
99723548 | 263 | BlockDriverState *bs = data->bs; |
a77fd4bb | 264 | |
99723548 | 265 | bdrv_dec_in_flight(bs); |
481cad48 | 266 | if (data->begin) { |
b0165585 | 267 | bdrv_do_drained_begin(bs, data->recursive, data->parent); |
481cad48 | 268 | } else { |
b0165585 | 269 | bdrv_do_drained_end(bs, data->recursive, data->parent); |
481cad48 MP |
270 | } |
271 | ||
a77fd4bb | 272 | data->done = true; |
1919631e | 273 | aio_co_wake(co); |
a77fd4bb FZ |
274 | } |
275 | ||
481cad48 | 276 | static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs, |
b0165585 KW |
277 | bool begin, bool recursive, |
278 | BdrvChild *parent) | |
a77fd4bb FZ |
279 | { |
280 | BdrvCoDrainData data; | |
281 | ||
282 | /* Calling bdrv_drain() from a BH ensures the current coroutine yields and | |
c40a2545 | 283 | * other coroutines run if they were queued by aio_co_enter(). */ |
a77fd4bb FZ |
284 | |
285 | assert(qemu_in_coroutine()); | |
286 | data = (BdrvCoDrainData) { | |
287 | .co = qemu_coroutine_self(), | |
288 | .bs = bs, | |
289 | .done = false, | |
481cad48 | 290 | .begin = begin, |
b0165585 | 291 | .recursive = recursive, |
0152bf40 | 292 | .parent = parent, |
a77fd4bb | 293 | }; |
99723548 | 294 | bdrv_inc_in_flight(bs); |
fffb6e12 PB |
295 | aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), |
296 | bdrv_co_drain_bh_cb, &data); | |
a77fd4bb FZ |
297 | |
298 | qemu_coroutine_yield(); | |
299 | /* If we are resumed from some other event (such as an aio completion or a | |
300 | * timer callback), it is a bug in the caller that should be fixed. */ | |
301 | assert(data.done); | |
302 | } | |
303 | ||
d736f119 KW |
304 | void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive, |
305 | BdrvChild *parent) | |
6820643f | 306 | { |
b0165585 KW |
307 | BdrvChild *child, *next; |
308 | ||
d42cf288 | 309 | if (qemu_in_coroutine()) { |
b0165585 | 310 | bdrv_co_yield_to_drain(bs, true, recursive, parent); |
d42cf288 PB |
311 | return; |
312 | } | |
313 | ||
60369b86 | 314 | /* Stop things in parent-to-child order */ |
414c2ec3 | 315 | if (atomic_fetch_inc(&bs->quiesce_counter) == 0) { |
6820643f | 316 | aio_disable_external(bdrv_get_aio_context(bs)); |
6820643f KW |
317 | } |
318 | ||
0152bf40 | 319 | bdrv_parent_drained_begin(bs, parent); |
7d40d9ef | 320 | bdrv_drain_invoke(bs, true); |
89bd0305 | 321 | bdrv_drain_recurse(bs, parent); |
b0165585 KW |
322 | |
323 | if (recursive) { | |
d736f119 | 324 | bs->recursive_quiesce_counter++; |
b0165585 KW |
325 | QLIST_FOREACH_SAFE(child, &bs->children, next, next) { |
326 | bdrv_do_drained_begin(child->bs, true, child); | |
327 | } | |
328 | } | |
6820643f KW |
329 | } |
330 | ||
0152bf40 KW |
331 | void bdrv_drained_begin(BlockDriverState *bs) |
332 | { | |
b0165585 KW |
333 | bdrv_do_drained_begin(bs, false, NULL); |
334 | } | |
335 | ||
336 | void bdrv_subtree_drained_begin(BlockDriverState *bs) | |
337 | { | |
338 | bdrv_do_drained_begin(bs, true, NULL); | |
0152bf40 KW |
339 | } |
340 | ||
d736f119 KW |
341 | void bdrv_do_drained_end(BlockDriverState *bs, bool recursive, |
342 | BdrvChild *parent) | |
6820643f | 343 | { |
b0165585 | 344 | BdrvChild *child, *next; |
0f115168 KW |
345 | int old_quiesce_counter; |
346 | ||
481cad48 | 347 | if (qemu_in_coroutine()) { |
b0165585 | 348 | bdrv_co_yield_to_drain(bs, false, recursive, parent); |
481cad48 MP |
349 | return; |
350 | } | |
6820643f | 351 | assert(bs->quiesce_counter > 0); |
0f115168 | 352 | old_quiesce_counter = atomic_fetch_dec(&bs->quiesce_counter); |
6820643f | 353 | |
60369b86 | 354 | /* Re-enable things in child-to-parent order */ |
7d40d9ef | 355 | bdrv_drain_invoke(bs, false); |
0152bf40 | 356 | bdrv_parent_drained_end(bs, parent); |
0f115168 | 357 | if (old_quiesce_counter == 1) { |
0f115168 KW |
358 | aio_enable_external(bdrv_get_aio_context(bs)); |
359 | } | |
b0165585 KW |
360 | |
361 | if (recursive) { | |
d736f119 | 362 | bs->recursive_quiesce_counter--; |
b0165585 KW |
363 | QLIST_FOREACH_SAFE(child, &bs->children, next, next) { |
364 | bdrv_do_drained_end(child->bs, true, child); | |
365 | } | |
366 | } | |
6820643f KW |
367 | } |
368 | ||
0152bf40 KW |
369 | void bdrv_drained_end(BlockDriverState *bs) |
370 | { | |
b0165585 KW |
371 | bdrv_do_drained_end(bs, false, NULL); |
372 | } | |
373 | ||
374 | void bdrv_subtree_drained_end(BlockDriverState *bs) | |
375 | { | |
376 | bdrv_do_drained_end(bs, true, NULL); | |
0152bf40 KW |
377 | } |
378 | ||
d736f119 KW |
379 | void bdrv_apply_subtree_drain(BdrvChild *child, BlockDriverState *new_parent) |
380 | { | |
381 | int i; | |
382 | ||
383 | for (i = 0; i < new_parent->recursive_quiesce_counter; i++) { | |
384 | bdrv_do_drained_begin(child->bs, true, child); | |
385 | } | |
386 | } | |
387 | ||
388 | void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_parent) | |
389 | { | |
390 | int i; | |
391 | ||
392 | for (i = 0; i < old_parent->recursive_quiesce_counter; i++) { | |
393 | bdrv_do_drained_end(child->bs, true, child); | |
394 | } | |
395 | } | |
396 | ||
61007b31 | 397 | /* |
67da1dc5 FZ |
398 | * Wait for pending requests to complete on a single BlockDriverState subtree, |
399 | * and suspend block driver's internal I/O until next request arrives. | |
61007b31 | 400 | * |
61007b31 SH |
401 | * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState |
402 | * AioContext. | |
403 | */ | |
b6e84c97 | 404 | void coroutine_fn bdrv_co_drain(BlockDriverState *bs) |
61007b31 | 405 | { |
6820643f KW |
406 | assert(qemu_in_coroutine()); |
407 | bdrv_drained_begin(bs); | |
408 | bdrv_drained_end(bs); | |
b6e84c97 | 409 | } |
f406c03c | 410 | |
b6e84c97 PB |
411 | void bdrv_drain(BlockDriverState *bs) |
412 | { | |
6820643f KW |
413 | bdrv_drained_begin(bs); |
414 | bdrv_drained_end(bs); | |
61007b31 SH |
415 | } |
416 | ||
c13ad59f KW |
417 | static void bdrv_drain_assert_idle(BlockDriverState *bs) |
418 | { | |
419 | BdrvChild *child, *next; | |
420 | ||
421 | assert(atomic_read(&bs->in_flight) == 0); | |
422 | QLIST_FOREACH_SAFE(child, &bs->children, next, next) { | |
423 | bdrv_drain_assert_idle(child->bs); | |
424 | } | |
425 | } | |
426 | ||
61007b31 SH |
427 | /* |
428 | * Wait for pending requests to complete across all BlockDriverStates | |
429 | * | |
430 | * This function does not flush data to disk, use bdrv_flush_all() for that | |
431 | * after calling this function. | |
c0778f66 AG |
432 | * |
433 | * This pauses all block jobs and disables external clients. It must | |
434 | * be paired with bdrv_drain_all_end(). | |
435 | * | |
436 | * NOTE: no new block jobs or BlockDriverStates can be created between | |
437 | * the bdrv_drain_all_begin() and bdrv_drain_all_end() calls. | |
61007b31 | 438 | */ |
c0778f66 | 439 | void bdrv_drain_all_begin(void) |
61007b31 | 440 | { |
7c8eece4 | 441 | BlockDriverState *bs; |
88be7b4b | 442 | BdrvNextIterator it; |
61007b31 | 443 | |
9a7e86c8 KW |
444 | /* BDRV_POLL_WHILE() for a node can only be called from its own I/O thread |
445 | * or the main loop AioContext. We potentially use BDRV_POLL_WHILE() on | |
446 | * nodes in several different AioContexts, so make sure we're in the main | |
447 | * context. */ | |
448 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); | |
449 | ||
88be7b4b | 450 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
61007b31 SH |
451 | AioContext *aio_context = bdrv_get_aio_context(bs); |
452 | ||
453 | aio_context_acquire(aio_context); | |
79ab8b21 | 454 | bdrv_do_drained_begin(bs, true, NULL); |
61007b31 SH |
455 | aio_context_release(aio_context); |
456 | } | |
457 | ||
c13ad59f KW |
458 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
459 | bdrv_drain_assert_idle(bs); | |
61007b31 | 460 | } |
c0778f66 AG |
461 | } |
462 | ||
463 | void bdrv_drain_all_end(void) | |
464 | { | |
465 | BlockDriverState *bs; | |
466 | BdrvNextIterator it; | |
c0778f66 | 467 | |
88be7b4b | 468 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
61007b31 SH |
469 | AioContext *aio_context = bdrv_get_aio_context(bs); |
470 | ||
471 | aio_context_acquire(aio_context); | |
79ab8b21 | 472 | bdrv_do_drained_end(bs, true, NULL); |
61007b31 SH |
473 | aio_context_release(aio_context); |
474 | } | |
475 | } | |
476 | ||
c0778f66 AG |
477 | void bdrv_drain_all(void) |
478 | { | |
479 | bdrv_drain_all_begin(); | |
480 | bdrv_drain_all_end(); | |
481 | } | |
482 | ||
61007b31 SH |
483 | /** |
484 | * Remove an active request from the tracked requests list | |
485 | * | |
486 | * This function should be called when a tracked request is completing. | |
487 | */ | |
488 | static void tracked_request_end(BdrvTrackedRequest *req) | |
489 | { | |
490 | if (req->serialising) { | |
20fc71b2 | 491 | atomic_dec(&req->bs->serialising_in_flight); |
61007b31 SH |
492 | } |
493 | ||
3783fa3d | 494 | qemu_co_mutex_lock(&req->bs->reqs_lock); |
61007b31 SH |
495 | QLIST_REMOVE(req, list); |
496 | qemu_co_queue_restart_all(&req->wait_queue); | |
3783fa3d | 497 | qemu_co_mutex_unlock(&req->bs->reqs_lock); |
61007b31 SH |
498 | } |
499 | ||
500 | /** | |
501 | * Add an active request to the tracked requests list | |
502 | */ | |
503 | static void tracked_request_begin(BdrvTrackedRequest *req, | |
504 | BlockDriverState *bs, | |
505 | int64_t offset, | |
ebde595c FZ |
506 | unsigned int bytes, |
507 | enum BdrvTrackedRequestType type) | |
61007b31 SH |
508 | { |
509 | *req = (BdrvTrackedRequest){ | |
510 | .bs = bs, | |
511 | .offset = offset, | |
512 | .bytes = bytes, | |
ebde595c | 513 | .type = type, |
61007b31 SH |
514 | .co = qemu_coroutine_self(), |
515 | .serialising = false, | |
516 | .overlap_offset = offset, | |
517 | .overlap_bytes = bytes, | |
518 | }; | |
519 | ||
520 | qemu_co_queue_init(&req->wait_queue); | |
521 | ||
3783fa3d | 522 | qemu_co_mutex_lock(&bs->reqs_lock); |
61007b31 | 523 | QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); |
3783fa3d | 524 | qemu_co_mutex_unlock(&bs->reqs_lock); |
61007b31 SH |
525 | } |
526 | ||
527 | static void mark_request_serialising(BdrvTrackedRequest *req, uint64_t align) | |
528 | { | |
529 | int64_t overlap_offset = req->offset & ~(align - 1); | |
530 | unsigned int overlap_bytes = ROUND_UP(req->offset + req->bytes, align) | |
531 | - overlap_offset; | |
532 | ||
533 | if (!req->serialising) { | |
20fc71b2 | 534 | atomic_inc(&req->bs->serialising_in_flight); |
61007b31 SH |
535 | req->serialising = true; |
536 | } | |
537 | ||
538 | req->overlap_offset = MIN(req->overlap_offset, overlap_offset); | |
539 | req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes); | |
540 | } | |
541 | ||
244483e6 KW |
542 | /** |
543 | * Round a region to cluster boundaries | |
544 | */ | |
545 | void bdrv_round_to_clusters(BlockDriverState *bs, | |
7cfd5275 | 546 | int64_t offset, int64_t bytes, |
244483e6 | 547 | int64_t *cluster_offset, |
7cfd5275 | 548 | int64_t *cluster_bytes) |
244483e6 KW |
549 | { |
550 | BlockDriverInfo bdi; | |
551 | ||
552 | if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { | |
553 | *cluster_offset = offset; | |
554 | *cluster_bytes = bytes; | |
555 | } else { | |
556 | int64_t c = bdi.cluster_size; | |
557 | *cluster_offset = QEMU_ALIGN_DOWN(offset, c); | |
558 | *cluster_bytes = QEMU_ALIGN_UP(offset - *cluster_offset + bytes, c); | |
559 | } | |
560 | } | |
561 | ||
61007b31 SH |
562 | static int bdrv_get_cluster_size(BlockDriverState *bs) |
563 | { | |
564 | BlockDriverInfo bdi; | |
565 | int ret; | |
566 | ||
567 | ret = bdrv_get_info(bs, &bdi); | |
568 | if (ret < 0 || bdi.cluster_size == 0) { | |
a5b8dd2c | 569 | return bs->bl.request_alignment; |
61007b31 SH |
570 | } else { |
571 | return bdi.cluster_size; | |
572 | } | |
573 | } | |
574 | ||
575 | static bool tracked_request_overlaps(BdrvTrackedRequest *req, | |
576 | int64_t offset, unsigned int bytes) | |
577 | { | |
578 | /* aaaa bbbb */ | |
579 | if (offset >= req->overlap_offset + req->overlap_bytes) { | |
580 | return false; | |
581 | } | |
582 | /* bbbb aaaa */ | |
583 | if (req->overlap_offset >= offset + bytes) { | |
584 | return false; | |
585 | } | |
586 | return true; | |
587 | } | |
588 | ||
99723548 PB |
589 | void bdrv_inc_in_flight(BlockDriverState *bs) |
590 | { | |
591 | atomic_inc(&bs->in_flight); | |
592 | } | |
593 | ||
c9d1a561 PB |
594 | void bdrv_wakeup(BlockDriverState *bs) |
595 | { | |
7719f3c9 | 596 | aio_wait_kick(bdrv_get_aio_wait(bs)); |
c9d1a561 PB |
597 | } |
598 | ||
99723548 PB |
599 | void bdrv_dec_in_flight(BlockDriverState *bs) |
600 | { | |
601 | atomic_dec(&bs->in_flight); | |
c9d1a561 | 602 | bdrv_wakeup(bs); |
99723548 PB |
603 | } |
604 | ||
61007b31 SH |
605 | static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self) |
606 | { | |
607 | BlockDriverState *bs = self->bs; | |
608 | BdrvTrackedRequest *req; | |
609 | bool retry; | |
610 | bool waited = false; | |
611 | ||
20fc71b2 | 612 | if (!atomic_read(&bs->serialising_in_flight)) { |
61007b31 SH |
613 | return false; |
614 | } | |
615 | ||
616 | do { | |
617 | retry = false; | |
3783fa3d | 618 | qemu_co_mutex_lock(&bs->reqs_lock); |
61007b31 SH |
619 | QLIST_FOREACH(req, &bs->tracked_requests, list) { |
620 | if (req == self || (!req->serialising && !self->serialising)) { | |
621 | continue; | |
622 | } | |
623 | if (tracked_request_overlaps(req, self->overlap_offset, | |
624 | self->overlap_bytes)) | |
625 | { | |
626 | /* Hitting this means there was a reentrant request, for | |
627 | * example, a block driver issuing nested requests. This must | |
628 | * never happen since it means deadlock. | |
629 | */ | |
630 | assert(qemu_coroutine_self() != req->co); | |
631 | ||
632 | /* If the request is already (indirectly) waiting for us, or | |
633 | * will wait for us as soon as it wakes up, then just go on | |
634 | * (instead of producing a deadlock in the former case). */ | |
635 | if (!req->waiting_for) { | |
636 | self->waiting_for = req; | |
3783fa3d | 637 | qemu_co_queue_wait(&req->wait_queue, &bs->reqs_lock); |
61007b31 SH |
638 | self->waiting_for = NULL; |
639 | retry = true; | |
640 | waited = true; | |
641 | break; | |
642 | } | |
643 | } | |
644 | } | |
3783fa3d | 645 | qemu_co_mutex_unlock(&bs->reqs_lock); |
61007b31 SH |
646 | } while (retry); |
647 | ||
648 | return waited; | |
649 | } | |
650 | ||
651 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, | |
652 | size_t size) | |
653 | { | |
654 | if (size > BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS) { | |
655 | return -EIO; | |
656 | } | |
657 | ||
658 | if (!bdrv_is_inserted(bs)) { | |
659 | return -ENOMEDIUM; | |
660 | } | |
661 | ||
662 | if (offset < 0) { | |
663 | return -EIO; | |
664 | } | |
665 | ||
666 | return 0; | |
667 | } | |
668 | ||
61007b31 | 669 | typedef struct RwCo { |
e293b7a3 | 670 | BdrvChild *child; |
61007b31 SH |
671 | int64_t offset; |
672 | QEMUIOVector *qiov; | |
673 | bool is_write; | |
674 | int ret; | |
675 | BdrvRequestFlags flags; | |
676 | } RwCo; | |
677 | ||
678 | static void coroutine_fn bdrv_rw_co_entry(void *opaque) | |
679 | { | |
680 | RwCo *rwco = opaque; | |
681 | ||
682 | if (!rwco->is_write) { | |
a03ef88f | 683 | rwco->ret = bdrv_co_preadv(rwco->child, rwco->offset, |
cab3a356 KW |
684 | rwco->qiov->size, rwco->qiov, |
685 | rwco->flags); | |
61007b31 | 686 | } else { |
a03ef88f | 687 | rwco->ret = bdrv_co_pwritev(rwco->child, rwco->offset, |
cab3a356 KW |
688 | rwco->qiov->size, rwco->qiov, |
689 | rwco->flags); | |
61007b31 SH |
690 | } |
691 | } | |
692 | ||
693 | /* | |
694 | * Process a vectored synchronous request using coroutines | |
695 | */ | |
e293b7a3 | 696 | static int bdrv_prwv_co(BdrvChild *child, int64_t offset, |
61007b31 SH |
697 | QEMUIOVector *qiov, bool is_write, |
698 | BdrvRequestFlags flags) | |
699 | { | |
700 | Coroutine *co; | |
701 | RwCo rwco = { | |
e293b7a3 | 702 | .child = child, |
61007b31 SH |
703 | .offset = offset, |
704 | .qiov = qiov, | |
705 | .is_write = is_write, | |
706 | .ret = NOT_DONE, | |
707 | .flags = flags, | |
708 | }; | |
709 | ||
61007b31 SH |
710 | if (qemu_in_coroutine()) { |
711 | /* Fast-path if already in coroutine context */ | |
712 | bdrv_rw_co_entry(&rwco); | |
713 | } else { | |
0b8b8753 | 714 | co = qemu_coroutine_create(bdrv_rw_co_entry, &rwco); |
e92f0e19 | 715 | bdrv_coroutine_enter(child->bs, co); |
88b062c2 | 716 | BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE); |
61007b31 SH |
717 | } |
718 | return rwco.ret; | |
719 | } | |
720 | ||
721 | /* | |
722 | * Process a synchronous request using coroutines | |
723 | */ | |
e293b7a3 | 724 | static int bdrv_rw_co(BdrvChild *child, int64_t sector_num, uint8_t *buf, |
61007b31 SH |
725 | int nb_sectors, bool is_write, BdrvRequestFlags flags) |
726 | { | |
727 | QEMUIOVector qiov; | |
728 | struct iovec iov = { | |
729 | .iov_base = (void *)buf, | |
730 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, | |
731 | }; | |
732 | ||
733 | if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { | |
734 | return -EINVAL; | |
735 | } | |
736 | ||
737 | qemu_iovec_init_external(&qiov, &iov, 1); | |
e293b7a3 | 738 | return bdrv_prwv_co(child, sector_num << BDRV_SECTOR_BITS, |
61007b31 SH |
739 | &qiov, is_write, flags); |
740 | } | |
741 | ||
742 | /* return < 0 if error. See bdrv_write() for the return codes */ | |
fbcbbf4e | 743 | int bdrv_read(BdrvChild *child, int64_t sector_num, |
61007b31 SH |
744 | uint8_t *buf, int nb_sectors) |
745 | { | |
e293b7a3 | 746 | return bdrv_rw_co(child, sector_num, buf, nb_sectors, false, 0); |
61007b31 SH |
747 | } |
748 | ||
61007b31 SH |
749 | /* Return < 0 if error. Important errors are: |
750 | -EIO generic I/O error (may happen for all errors) | |
751 | -ENOMEDIUM No media inserted. | |
752 | -EINVAL Invalid sector number or nb_sectors | |
753 | -EACCES Trying to write a read-only device | |
754 | */ | |
18d51c4b | 755 | int bdrv_write(BdrvChild *child, int64_t sector_num, |
61007b31 SH |
756 | const uint8_t *buf, int nb_sectors) |
757 | { | |
e293b7a3 | 758 | return bdrv_rw_co(child, sector_num, (uint8_t *)buf, nb_sectors, true, 0); |
61007b31 SH |
759 | } |
760 | ||
720ff280 | 761 | int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset, |
f5a5ca79 | 762 | int bytes, BdrvRequestFlags flags) |
61007b31 | 763 | { |
74021bc4 EB |
764 | QEMUIOVector qiov; |
765 | struct iovec iov = { | |
766 | .iov_base = NULL, | |
f5a5ca79 | 767 | .iov_len = bytes, |
74021bc4 EB |
768 | }; |
769 | ||
770 | qemu_iovec_init_external(&qiov, &iov, 1); | |
e293b7a3 | 771 | return bdrv_prwv_co(child, offset, &qiov, true, |
74021bc4 | 772 | BDRV_REQ_ZERO_WRITE | flags); |
61007b31 SH |
773 | } |
774 | ||
775 | /* | |
74021bc4 | 776 | * Completely zero out a block device with the help of bdrv_pwrite_zeroes. |
61007b31 SH |
777 | * The operation is sped up by checking the block status and only writing |
778 | * zeroes to the device if they currently do not return zeroes. Optional | |
74021bc4 | 779 | * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP, |
465fe887 | 780 | * BDRV_REQ_FUA). |
61007b31 SH |
781 | * |
782 | * Returns < 0 on error, 0 on success. For error codes see bdrv_write(). | |
783 | */ | |
720ff280 | 784 | int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags) |
61007b31 | 785 | { |
237d78f8 EB |
786 | int ret; |
787 | int64_t target_size, bytes, offset = 0; | |
720ff280 | 788 | BlockDriverState *bs = child->bs; |
61007b31 | 789 | |
7286d610 EB |
790 | target_size = bdrv_getlength(bs); |
791 | if (target_size < 0) { | |
792 | return target_size; | |
61007b31 SH |
793 | } |
794 | ||
795 | for (;;) { | |
7286d610 EB |
796 | bytes = MIN(target_size - offset, BDRV_REQUEST_MAX_BYTES); |
797 | if (bytes <= 0) { | |
61007b31 SH |
798 | return 0; |
799 | } | |
237d78f8 | 800 | ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL); |
61007b31 | 801 | if (ret < 0) { |
7286d610 EB |
802 | error_report("error getting block status at offset %" PRId64 ": %s", |
803 | offset, strerror(-ret)); | |
61007b31 SH |
804 | return ret; |
805 | } | |
806 | if (ret & BDRV_BLOCK_ZERO) { | |
237d78f8 | 807 | offset += bytes; |
61007b31 SH |
808 | continue; |
809 | } | |
237d78f8 | 810 | ret = bdrv_pwrite_zeroes(child, offset, bytes, flags); |
61007b31 | 811 | if (ret < 0) { |
7286d610 EB |
812 | error_report("error writing zeroes at offset %" PRId64 ": %s", |
813 | offset, strerror(-ret)); | |
61007b31 SH |
814 | return ret; |
815 | } | |
237d78f8 | 816 | offset += bytes; |
61007b31 SH |
817 | } |
818 | } | |
819 | ||
cf2ab8fc | 820 | int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov) |
f1e84741 KW |
821 | { |
822 | int ret; | |
823 | ||
e293b7a3 | 824 | ret = bdrv_prwv_co(child, offset, qiov, false, 0); |
f1e84741 KW |
825 | if (ret < 0) { |
826 | return ret; | |
827 | } | |
828 | ||
829 | return qiov->size; | |
830 | } | |
831 | ||
cf2ab8fc | 832 | int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes) |
61007b31 SH |
833 | { |
834 | QEMUIOVector qiov; | |
835 | struct iovec iov = { | |
836 | .iov_base = (void *)buf, | |
837 | .iov_len = bytes, | |
838 | }; | |
61007b31 SH |
839 | |
840 | if (bytes < 0) { | |
841 | return -EINVAL; | |
842 | } | |
843 | ||
844 | qemu_iovec_init_external(&qiov, &iov, 1); | |
cf2ab8fc | 845 | return bdrv_preadv(child, offset, &qiov); |
61007b31 SH |
846 | } |
847 | ||
d9ca2ea2 | 848 | int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov) |
61007b31 SH |
849 | { |
850 | int ret; | |
851 | ||
e293b7a3 | 852 | ret = bdrv_prwv_co(child, offset, qiov, true, 0); |
61007b31 SH |
853 | if (ret < 0) { |
854 | return ret; | |
855 | } | |
856 | ||
857 | return qiov->size; | |
858 | } | |
859 | ||
d9ca2ea2 | 860 | int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes) |
61007b31 SH |
861 | { |
862 | QEMUIOVector qiov; | |
863 | struct iovec iov = { | |
864 | .iov_base = (void *) buf, | |
865 | .iov_len = bytes, | |
866 | }; | |
867 | ||
868 | if (bytes < 0) { | |
869 | return -EINVAL; | |
870 | } | |
871 | ||
872 | qemu_iovec_init_external(&qiov, &iov, 1); | |
d9ca2ea2 | 873 | return bdrv_pwritev(child, offset, &qiov); |
61007b31 SH |
874 | } |
875 | ||
876 | /* | |
877 | * Writes to the file and ensures that no writes are reordered across this | |
878 | * request (acts as a barrier) | |
879 | * | |
880 | * Returns 0 on success, -errno in error cases. | |
881 | */ | |
d9ca2ea2 KW |
882 | int bdrv_pwrite_sync(BdrvChild *child, int64_t offset, |
883 | const void *buf, int count) | |
61007b31 SH |
884 | { |
885 | int ret; | |
886 | ||
d9ca2ea2 | 887 | ret = bdrv_pwrite(child, offset, buf, count); |
61007b31 SH |
888 | if (ret < 0) { |
889 | return ret; | |
890 | } | |
891 | ||
d9ca2ea2 | 892 | ret = bdrv_flush(child->bs); |
855a6a93 KW |
893 | if (ret < 0) { |
894 | return ret; | |
61007b31 SH |
895 | } |
896 | ||
897 | return 0; | |
898 | } | |
899 | ||
08844473 KW |
900 | typedef struct CoroutineIOCompletion { |
901 | Coroutine *coroutine; | |
902 | int ret; | |
903 | } CoroutineIOCompletion; | |
904 | ||
905 | static void bdrv_co_io_em_complete(void *opaque, int ret) | |
906 | { | |
907 | CoroutineIOCompletion *co = opaque; | |
908 | ||
909 | co->ret = ret; | |
b9e413dd | 910 | aio_co_wake(co->coroutine); |
08844473 KW |
911 | } |
912 | ||
166fe960 KW |
913 | static int coroutine_fn bdrv_driver_preadv(BlockDriverState *bs, |
914 | uint64_t offset, uint64_t bytes, | |
915 | QEMUIOVector *qiov, int flags) | |
916 | { | |
917 | BlockDriver *drv = bs->drv; | |
3fb06697 KW |
918 | int64_t sector_num; |
919 | unsigned int nb_sectors; | |
920 | ||
fa166538 EB |
921 | assert(!(flags & ~BDRV_REQ_MASK)); |
922 | ||
d470ad42 HR |
923 | if (!drv) { |
924 | return -ENOMEDIUM; | |
925 | } | |
926 | ||
3fb06697 KW |
927 | if (drv->bdrv_co_preadv) { |
928 | return drv->bdrv_co_preadv(bs, offset, bytes, qiov, flags); | |
929 | } | |
930 | ||
edfab6a0 | 931 | if (drv->bdrv_aio_preadv) { |
08844473 KW |
932 | BlockAIOCB *acb; |
933 | CoroutineIOCompletion co = { | |
934 | .coroutine = qemu_coroutine_self(), | |
935 | }; | |
936 | ||
edfab6a0 EB |
937 | acb = drv->bdrv_aio_preadv(bs, offset, bytes, qiov, flags, |
938 | bdrv_co_io_em_complete, &co); | |
08844473 KW |
939 | if (acb == NULL) { |
940 | return -EIO; | |
941 | } else { | |
942 | qemu_coroutine_yield(); | |
943 | return co.ret; | |
944 | } | |
945 | } | |
edfab6a0 EB |
946 | |
947 | sector_num = offset >> BDRV_SECTOR_BITS; | |
948 | nb_sectors = bytes >> BDRV_SECTOR_BITS; | |
949 | ||
950 | assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0); | |
951 | assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0); | |
952 | assert((bytes >> BDRV_SECTOR_BITS) <= BDRV_REQUEST_MAX_SECTORS); | |
953 | assert(drv->bdrv_co_readv); | |
954 | ||
955 | return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); | |
166fe960 KW |
956 | } |
957 | ||
78a07294 KW |
958 | static int coroutine_fn bdrv_driver_pwritev(BlockDriverState *bs, |
959 | uint64_t offset, uint64_t bytes, | |
960 | QEMUIOVector *qiov, int flags) | |
961 | { | |
962 | BlockDriver *drv = bs->drv; | |
3fb06697 KW |
963 | int64_t sector_num; |
964 | unsigned int nb_sectors; | |
78a07294 KW |
965 | int ret; |
966 | ||
fa166538 EB |
967 | assert(!(flags & ~BDRV_REQ_MASK)); |
968 | ||
d470ad42 HR |
969 | if (!drv) { |
970 | return -ENOMEDIUM; | |
971 | } | |
972 | ||
3fb06697 | 973 | if (drv->bdrv_co_pwritev) { |
515c2f43 KW |
974 | ret = drv->bdrv_co_pwritev(bs, offset, bytes, qiov, |
975 | flags & bs->supported_write_flags); | |
976 | flags &= ~bs->supported_write_flags; | |
3fb06697 KW |
977 | goto emulate_flags; |
978 | } | |
979 | ||
edfab6a0 | 980 | if (drv->bdrv_aio_pwritev) { |
08844473 KW |
981 | BlockAIOCB *acb; |
982 | CoroutineIOCompletion co = { | |
983 | .coroutine = qemu_coroutine_self(), | |
984 | }; | |
985 | ||
edfab6a0 EB |
986 | acb = drv->bdrv_aio_pwritev(bs, offset, bytes, qiov, |
987 | flags & bs->supported_write_flags, | |
988 | bdrv_co_io_em_complete, &co); | |
989 | flags &= ~bs->supported_write_flags; | |
08844473 | 990 | if (acb == NULL) { |
3fb06697 | 991 | ret = -EIO; |
08844473 KW |
992 | } else { |
993 | qemu_coroutine_yield(); | |
3fb06697 | 994 | ret = co.ret; |
08844473 | 995 | } |
edfab6a0 EB |
996 | goto emulate_flags; |
997 | } | |
998 | ||
999 | sector_num = offset >> BDRV_SECTOR_BITS; | |
1000 | nb_sectors = bytes >> BDRV_SECTOR_BITS; | |
1001 | ||
1002 | assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0); | |
1003 | assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0); | |
1004 | assert((bytes >> BDRV_SECTOR_BITS) <= BDRV_REQUEST_MAX_SECTORS); | |
1005 | ||
e18a58b4 EB |
1006 | assert(drv->bdrv_co_writev); |
1007 | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov, | |
1008 | flags & bs->supported_write_flags); | |
1009 | flags &= ~bs->supported_write_flags; | |
78a07294 | 1010 | |
3fb06697 | 1011 | emulate_flags: |
4df863f3 | 1012 | if (ret == 0 && (flags & BDRV_REQ_FUA)) { |
78a07294 KW |
1013 | ret = bdrv_co_flush(bs); |
1014 | } | |
1015 | ||
1016 | return ret; | |
1017 | } | |
1018 | ||
29a298af PB |
1019 | static int coroutine_fn |
1020 | bdrv_driver_pwritev_compressed(BlockDriverState *bs, uint64_t offset, | |
1021 | uint64_t bytes, QEMUIOVector *qiov) | |
1022 | { | |
1023 | BlockDriver *drv = bs->drv; | |
1024 | ||
d470ad42 HR |
1025 | if (!drv) { |
1026 | return -ENOMEDIUM; | |
1027 | } | |
1028 | ||
29a298af PB |
1029 | if (!drv->bdrv_co_pwritev_compressed) { |
1030 | return -ENOTSUP; | |
1031 | } | |
1032 | ||
29a298af PB |
1033 | return drv->bdrv_co_pwritev_compressed(bs, offset, bytes, qiov); |
1034 | } | |
1035 | ||
85c97ca7 | 1036 | static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child, |
244483e6 | 1037 | int64_t offset, unsigned int bytes, QEMUIOVector *qiov) |
61007b31 | 1038 | { |
85c97ca7 KW |
1039 | BlockDriverState *bs = child->bs; |
1040 | ||
61007b31 SH |
1041 | /* Perform I/O through a temporary buffer so that users who scribble over |
1042 | * their read buffer while the operation is in progress do not end up | |
1043 | * modifying the image file. This is critical for zero-copy guest I/O | |
1044 | * where anything might happen inside guest memory. | |
1045 | */ | |
1046 | void *bounce_buffer; | |
1047 | ||
1048 | BlockDriver *drv = bs->drv; | |
1049 | struct iovec iov; | |
cb2e2878 | 1050 | QEMUIOVector local_qiov; |
244483e6 | 1051 | int64_t cluster_offset; |
7cfd5275 | 1052 | int64_t cluster_bytes; |
61007b31 SH |
1053 | size_t skip_bytes; |
1054 | int ret; | |
cb2e2878 EB |
1055 | int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer, |
1056 | BDRV_REQUEST_MAX_BYTES); | |
1057 | unsigned int progress = 0; | |
61007b31 | 1058 | |
d470ad42 HR |
1059 | if (!drv) { |
1060 | return -ENOMEDIUM; | |
1061 | } | |
1062 | ||
1bf03e66 KW |
1063 | /* FIXME We cannot require callers to have write permissions when all they |
1064 | * are doing is a read request. If we did things right, write permissions | |
1065 | * would be obtained anyway, but internally by the copy-on-read code. As | |
765d9df9 | 1066 | * long as it is implemented here rather than in a separate filter driver, |
1bf03e66 KW |
1067 | * the copy-on-read code doesn't have its own BdrvChild, however, for which |
1068 | * it could request permissions. Therefore we have to bypass the permission | |
1069 | * system for the moment. */ | |
1070 | // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE)); | |
afa4b293 | 1071 | |
61007b31 | 1072 | /* Cover entire cluster so no additional backing file I/O is required when |
cb2e2878 EB |
1073 | * allocating cluster in the image file. Note that this value may exceed |
1074 | * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which | |
1075 | * is one reason we loop rather than doing it all at once. | |
61007b31 | 1076 | */ |
244483e6 | 1077 | bdrv_round_to_clusters(bs, offset, bytes, &cluster_offset, &cluster_bytes); |
cb2e2878 | 1078 | skip_bytes = offset - cluster_offset; |
61007b31 | 1079 | |
244483e6 KW |
1080 | trace_bdrv_co_do_copy_on_readv(bs, offset, bytes, |
1081 | cluster_offset, cluster_bytes); | |
61007b31 | 1082 | |
cb2e2878 EB |
1083 | bounce_buffer = qemu_try_blockalign(bs, |
1084 | MIN(MIN(max_transfer, cluster_bytes), | |
1085 | MAX_BOUNCE_BUFFER)); | |
61007b31 SH |
1086 | if (bounce_buffer == NULL) { |
1087 | ret = -ENOMEM; | |
1088 | goto err; | |
1089 | } | |
1090 | ||
cb2e2878 EB |
1091 | while (cluster_bytes) { |
1092 | int64_t pnum; | |
61007b31 | 1093 | |
cb2e2878 EB |
1094 | ret = bdrv_is_allocated(bs, cluster_offset, |
1095 | MIN(cluster_bytes, max_transfer), &pnum); | |
1096 | if (ret < 0) { | |
1097 | /* Safe to treat errors in querying allocation as if | |
1098 | * unallocated; we'll probably fail again soon on the | |
1099 | * read, but at least that will set a decent errno. | |
1100 | */ | |
1101 | pnum = MIN(cluster_bytes, max_transfer); | |
1102 | } | |
61007b31 | 1103 | |
cb2e2878 | 1104 | assert(skip_bytes < pnum); |
61007b31 | 1105 | |
cb2e2878 EB |
1106 | if (ret <= 0) { |
1107 | /* Must copy-on-read; use the bounce buffer */ | |
1108 | iov.iov_base = bounce_buffer; | |
1109 | iov.iov_len = pnum = MIN(pnum, MAX_BOUNCE_BUFFER); | |
1110 | qemu_iovec_init_external(&local_qiov, &iov, 1); | |
61007b31 | 1111 | |
cb2e2878 EB |
1112 | ret = bdrv_driver_preadv(bs, cluster_offset, pnum, |
1113 | &local_qiov, 0); | |
1114 | if (ret < 0) { | |
1115 | goto err; | |
1116 | } | |
1117 | ||
1118 | bdrv_debug_event(bs, BLKDBG_COR_WRITE); | |
1119 | if (drv->bdrv_co_pwrite_zeroes && | |
1120 | buffer_is_zero(bounce_buffer, pnum)) { | |
1121 | /* FIXME: Should we (perhaps conditionally) be setting | |
1122 | * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy | |
1123 | * that still correctly reads as zero? */ | |
7adcf59f HR |
1124 | ret = bdrv_co_do_pwrite_zeroes(bs, cluster_offset, pnum, |
1125 | BDRV_REQ_WRITE_UNCHANGED); | |
cb2e2878 EB |
1126 | } else { |
1127 | /* This does not change the data on the disk, it is not | |
1128 | * necessary to flush even in cache=writethrough mode. | |
1129 | */ | |
1130 | ret = bdrv_driver_pwritev(bs, cluster_offset, pnum, | |
7adcf59f HR |
1131 | &local_qiov, |
1132 | BDRV_REQ_WRITE_UNCHANGED); | |
cb2e2878 EB |
1133 | } |
1134 | ||
1135 | if (ret < 0) { | |
1136 | /* It might be okay to ignore write errors for guest | |
1137 | * requests. If this is a deliberate copy-on-read | |
1138 | * then we don't want to ignore the error. Simply | |
1139 | * report it in all cases. | |
1140 | */ | |
1141 | goto err; | |
1142 | } | |
1143 | ||
1144 | qemu_iovec_from_buf(qiov, progress, bounce_buffer + skip_bytes, | |
1145 | pnum - skip_bytes); | |
1146 | } else { | |
1147 | /* Read directly into the destination */ | |
1148 | qemu_iovec_init(&local_qiov, qiov->niov); | |
1149 | qemu_iovec_concat(&local_qiov, qiov, progress, pnum - skip_bytes); | |
1150 | ret = bdrv_driver_preadv(bs, offset + progress, local_qiov.size, | |
1151 | &local_qiov, 0); | |
1152 | qemu_iovec_destroy(&local_qiov); | |
1153 | if (ret < 0) { | |
1154 | goto err; | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | cluster_offset += pnum; | |
1159 | cluster_bytes -= pnum; | |
1160 | progress += pnum - skip_bytes; | |
1161 | skip_bytes = 0; | |
1162 | } | |
1163 | ret = 0; | |
61007b31 SH |
1164 | |
1165 | err: | |
1166 | qemu_vfree(bounce_buffer); | |
1167 | return ret; | |
1168 | } | |
1169 | ||
1170 | /* | |
1171 | * Forwards an already correctly aligned request to the BlockDriver. This | |
1a62d0ac EB |
1172 | * handles copy on read, zeroing after EOF, and fragmentation of large |
1173 | * reads; any other features must be implemented by the caller. | |
61007b31 | 1174 | */ |
85c97ca7 | 1175 | static int coroutine_fn bdrv_aligned_preadv(BdrvChild *child, |
61007b31 SH |
1176 | BdrvTrackedRequest *req, int64_t offset, unsigned int bytes, |
1177 | int64_t align, QEMUIOVector *qiov, int flags) | |
1178 | { | |
85c97ca7 | 1179 | BlockDriverState *bs = child->bs; |
c9d20029 | 1180 | int64_t total_bytes, max_bytes; |
1a62d0ac EB |
1181 | int ret = 0; |
1182 | uint64_t bytes_remaining = bytes; | |
1183 | int max_transfer; | |
61007b31 | 1184 | |
49c07526 KW |
1185 | assert(is_power_of_2(align)); |
1186 | assert((offset & (align - 1)) == 0); | |
1187 | assert((bytes & (align - 1)) == 0); | |
61007b31 | 1188 | assert(!qiov || bytes == qiov->size); |
abb06c5a | 1189 | assert((bs->open_flags & BDRV_O_NO_IO) == 0); |
1a62d0ac EB |
1190 | max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX), |
1191 | align); | |
a604fa2b EB |
1192 | |
1193 | /* TODO: We would need a per-BDS .supported_read_flags and | |
1194 | * potential fallback support, if we ever implement any read flags | |
1195 | * to pass through to drivers. For now, there aren't any | |
1196 | * passthrough flags. */ | |
1197 | assert(!(flags & ~(BDRV_REQ_NO_SERIALISING | BDRV_REQ_COPY_ON_READ))); | |
61007b31 SH |
1198 | |
1199 | /* Handle Copy on Read and associated serialisation */ | |
1200 | if (flags & BDRV_REQ_COPY_ON_READ) { | |
1201 | /* If we touch the same cluster it counts as an overlap. This | |
1202 | * guarantees that allocating writes will be serialized and not race | |
1203 | * with each other for the same cluster. For example, in copy-on-read | |
1204 | * it ensures that the CoR read and write operations are atomic and | |
1205 | * guest writes cannot interleave between them. */ | |
1206 | mark_request_serialising(req, bdrv_get_cluster_size(bs)); | |
1207 | } | |
1208 | ||
61408b25 FZ |
1209 | if (!(flags & BDRV_REQ_NO_SERIALISING)) { |
1210 | wait_serialising_requests(req); | |
1211 | } | |
61007b31 SH |
1212 | |
1213 | if (flags & BDRV_REQ_COPY_ON_READ) { | |
d6a644bb | 1214 | int64_t pnum; |
61007b31 | 1215 | |
88e63df2 | 1216 | ret = bdrv_is_allocated(bs, offset, bytes, &pnum); |
61007b31 SH |
1217 | if (ret < 0) { |
1218 | goto out; | |
1219 | } | |
1220 | ||
88e63df2 | 1221 | if (!ret || pnum != bytes) { |
85c97ca7 | 1222 | ret = bdrv_co_do_copy_on_readv(child, offset, bytes, qiov); |
61007b31 SH |
1223 | goto out; |
1224 | } | |
1225 | } | |
1226 | ||
1a62d0ac | 1227 | /* Forward the request to the BlockDriver, possibly fragmenting it */ |
c9d20029 KW |
1228 | total_bytes = bdrv_getlength(bs); |
1229 | if (total_bytes < 0) { | |
1230 | ret = total_bytes; | |
1231 | goto out; | |
1232 | } | |
61007b31 | 1233 | |
c9d20029 | 1234 | max_bytes = ROUND_UP(MAX(0, total_bytes - offset), align); |
1a62d0ac | 1235 | if (bytes <= max_bytes && bytes <= max_transfer) { |
c9d20029 | 1236 | ret = bdrv_driver_preadv(bs, offset, bytes, qiov, 0); |
1a62d0ac EB |
1237 | goto out; |
1238 | } | |
61007b31 | 1239 | |
1a62d0ac EB |
1240 | while (bytes_remaining) { |
1241 | int num; | |
61007b31 | 1242 | |
1a62d0ac EB |
1243 | if (max_bytes) { |
1244 | QEMUIOVector local_qiov; | |
61007b31 | 1245 | |
1a62d0ac EB |
1246 | num = MIN(bytes_remaining, MIN(max_bytes, max_transfer)); |
1247 | assert(num); | |
1248 | qemu_iovec_init(&local_qiov, qiov->niov); | |
1249 | qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num); | |
61007b31 | 1250 | |
1a62d0ac EB |
1251 | ret = bdrv_driver_preadv(bs, offset + bytes - bytes_remaining, |
1252 | num, &local_qiov, 0); | |
1253 | max_bytes -= num; | |
1254 | qemu_iovec_destroy(&local_qiov); | |
1255 | } else { | |
1256 | num = bytes_remaining; | |
1257 | ret = qemu_iovec_memset(qiov, bytes - bytes_remaining, 0, | |
1258 | bytes_remaining); | |
1259 | } | |
1260 | if (ret < 0) { | |
1261 | goto out; | |
1262 | } | |
1263 | bytes_remaining -= num; | |
61007b31 SH |
1264 | } |
1265 | ||
1266 | out: | |
1a62d0ac | 1267 | return ret < 0 ? ret : 0; |
61007b31 SH |
1268 | } |
1269 | ||
61007b31 SH |
1270 | /* |
1271 | * Handle a read request in coroutine context | |
1272 | */ | |
a03ef88f | 1273 | int coroutine_fn bdrv_co_preadv(BdrvChild *child, |
61007b31 SH |
1274 | int64_t offset, unsigned int bytes, QEMUIOVector *qiov, |
1275 | BdrvRequestFlags flags) | |
1276 | { | |
a03ef88f | 1277 | BlockDriverState *bs = child->bs; |
61007b31 SH |
1278 | BlockDriver *drv = bs->drv; |
1279 | BdrvTrackedRequest req; | |
1280 | ||
a5b8dd2c | 1281 | uint64_t align = bs->bl.request_alignment; |
61007b31 SH |
1282 | uint8_t *head_buf = NULL; |
1283 | uint8_t *tail_buf = NULL; | |
1284 | QEMUIOVector local_qiov; | |
1285 | bool use_local_qiov = false; | |
1286 | int ret; | |
1287 | ||
f42cf447 DB |
1288 | trace_bdrv_co_preadv(child->bs, offset, bytes, flags); |
1289 | ||
61007b31 SH |
1290 | if (!drv) { |
1291 | return -ENOMEDIUM; | |
1292 | } | |
1293 | ||
1294 | ret = bdrv_check_byte_request(bs, offset, bytes); | |
1295 | if (ret < 0) { | |
1296 | return ret; | |
1297 | } | |
1298 | ||
99723548 PB |
1299 | bdrv_inc_in_flight(bs); |
1300 | ||
9568b511 | 1301 | /* Don't do copy-on-read if we read data before write operation */ |
d3faa13e | 1302 | if (atomic_read(&bs->copy_on_read) && !(flags & BDRV_REQ_NO_SERIALISING)) { |
61007b31 SH |
1303 | flags |= BDRV_REQ_COPY_ON_READ; |
1304 | } | |
1305 | ||
61007b31 SH |
1306 | /* Align read if necessary by padding qiov */ |
1307 | if (offset & (align - 1)) { | |
1308 | head_buf = qemu_blockalign(bs, align); | |
1309 | qemu_iovec_init(&local_qiov, qiov->niov + 2); | |
1310 | qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1)); | |
1311 | qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); | |
1312 | use_local_qiov = true; | |
1313 | ||
1314 | bytes += offset & (align - 1); | |
1315 | offset = offset & ~(align - 1); | |
1316 | } | |
1317 | ||
1318 | if ((offset + bytes) & (align - 1)) { | |
1319 | if (!use_local_qiov) { | |
1320 | qemu_iovec_init(&local_qiov, qiov->niov + 1); | |
1321 | qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); | |
1322 | use_local_qiov = true; | |
1323 | } | |
1324 | tail_buf = qemu_blockalign(bs, align); | |
1325 | qemu_iovec_add(&local_qiov, tail_buf, | |
1326 | align - ((offset + bytes) & (align - 1))); | |
1327 | ||
1328 | bytes = ROUND_UP(bytes, align); | |
1329 | } | |
1330 | ||
ebde595c | 1331 | tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_READ); |
85c97ca7 | 1332 | ret = bdrv_aligned_preadv(child, &req, offset, bytes, align, |
61007b31 SH |
1333 | use_local_qiov ? &local_qiov : qiov, |
1334 | flags); | |
1335 | tracked_request_end(&req); | |
99723548 | 1336 | bdrv_dec_in_flight(bs); |
61007b31 SH |
1337 | |
1338 | if (use_local_qiov) { | |
1339 | qemu_iovec_destroy(&local_qiov); | |
1340 | qemu_vfree(head_buf); | |
1341 | qemu_vfree(tail_buf); | |
1342 | } | |
1343 | ||
1344 | return ret; | |
1345 | } | |
1346 | ||
adad6496 | 1347 | static int coroutine_fn bdrv_co_do_readv(BdrvChild *child, |
61007b31 SH |
1348 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
1349 | BdrvRequestFlags flags) | |
1350 | { | |
1351 | if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { | |
1352 | return -EINVAL; | |
1353 | } | |
1354 | ||
a03ef88f | 1355 | return bdrv_co_preadv(child, sector_num << BDRV_SECTOR_BITS, |
cab3a356 | 1356 | nb_sectors << BDRV_SECTOR_BITS, qiov, flags); |
61007b31 SH |
1357 | } |
1358 | ||
28b04a8f KW |
1359 | int coroutine_fn bdrv_co_readv(BdrvChild *child, int64_t sector_num, |
1360 | int nb_sectors, QEMUIOVector *qiov) | |
61007b31 | 1361 | { |
adad6496 | 1362 | return bdrv_co_do_readv(child, sector_num, nb_sectors, qiov, 0); |
61007b31 SH |
1363 | } |
1364 | ||
d05aa8bb | 1365 | static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 1366 | int64_t offset, int bytes, BdrvRequestFlags flags) |
61007b31 SH |
1367 | { |
1368 | BlockDriver *drv = bs->drv; | |
1369 | QEMUIOVector qiov; | |
1370 | struct iovec iov = {0}; | |
1371 | int ret = 0; | |
465fe887 | 1372 | bool need_flush = false; |
443668ca DL |
1373 | int head = 0; |
1374 | int tail = 0; | |
61007b31 | 1375 | |
cf081fca | 1376 | int max_write_zeroes = MIN_NON_ZERO(bs->bl.max_pwrite_zeroes, INT_MAX); |
a5b8dd2c EB |
1377 | int alignment = MAX(bs->bl.pwrite_zeroes_alignment, |
1378 | bs->bl.request_alignment); | |
cb2e2878 | 1379 | int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer, MAX_BOUNCE_BUFFER); |
d05aa8bb | 1380 | |
d470ad42 HR |
1381 | if (!drv) { |
1382 | return -ENOMEDIUM; | |
1383 | } | |
1384 | ||
b8d0a980 EB |
1385 | assert(alignment % bs->bl.request_alignment == 0); |
1386 | head = offset % alignment; | |
f5a5ca79 | 1387 | tail = (offset + bytes) % alignment; |
b8d0a980 EB |
1388 | max_write_zeroes = QEMU_ALIGN_DOWN(max_write_zeroes, alignment); |
1389 | assert(max_write_zeroes >= bs->bl.request_alignment); | |
61007b31 | 1390 | |
f5a5ca79 MP |
1391 | while (bytes > 0 && !ret) { |
1392 | int num = bytes; | |
61007b31 SH |
1393 | |
1394 | /* Align request. Block drivers can expect the "bulk" of the request | |
443668ca DL |
1395 | * to be aligned, and that unaligned requests do not cross cluster |
1396 | * boundaries. | |
61007b31 | 1397 | */ |
443668ca | 1398 | if (head) { |
b2f95fee EB |
1399 | /* Make a small request up to the first aligned sector. For |
1400 | * convenience, limit this request to max_transfer even if | |
1401 | * we don't need to fall back to writes. */ | |
f5a5ca79 | 1402 | num = MIN(MIN(bytes, max_transfer), alignment - head); |
b2f95fee EB |
1403 | head = (head + num) % alignment; |
1404 | assert(num < max_write_zeroes); | |
d05aa8bb | 1405 | } else if (tail && num > alignment) { |
443668ca DL |
1406 | /* Shorten the request to the last aligned sector. */ |
1407 | num -= tail; | |
61007b31 SH |
1408 | } |
1409 | ||
1410 | /* limit request size */ | |
1411 | if (num > max_write_zeroes) { | |
1412 | num = max_write_zeroes; | |
1413 | } | |
1414 | ||
1415 | ret = -ENOTSUP; | |
1416 | /* First try the efficient write zeroes operation */ | |
d05aa8bb EB |
1417 | if (drv->bdrv_co_pwrite_zeroes) { |
1418 | ret = drv->bdrv_co_pwrite_zeroes(bs, offset, num, | |
1419 | flags & bs->supported_zero_flags); | |
1420 | if (ret != -ENOTSUP && (flags & BDRV_REQ_FUA) && | |
1421 | !(bs->supported_zero_flags & BDRV_REQ_FUA)) { | |
1422 | need_flush = true; | |
1423 | } | |
465fe887 EB |
1424 | } else { |
1425 | assert(!bs->supported_zero_flags); | |
61007b31 SH |
1426 | } |
1427 | ||
1428 | if (ret == -ENOTSUP) { | |
1429 | /* Fall back to bounce buffer if write zeroes is unsupported */ | |
465fe887 EB |
1430 | BdrvRequestFlags write_flags = flags & ~BDRV_REQ_ZERO_WRITE; |
1431 | ||
1432 | if ((flags & BDRV_REQ_FUA) && | |
1433 | !(bs->supported_write_flags & BDRV_REQ_FUA)) { | |
1434 | /* No need for bdrv_driver_pwrite() to do a fallback | |
1435 | * flush on each chunk; use just one at the end */ | |
1436 | write_flags &= ~BDRV_REQ_FUA; | |
1437 | need_flush = true; | |
1438 | } | |
5def6b80 | 1439 | num = MIN(num, max_transfer); |
d05aa8bb | 1440 | iov.iov_len = num; |
61007b31 | 1441 | if (iov.iov_base == NULL) { |
d05aa8bb | 1442 | iov.iov_base = qemu_try_blockalign(bs, num); |
61007b31 SH |
1443 | if (iov.iov_base == NULL) { |
1444 | ret = -ENOMEM; | |
1445 | goto fail; | |
1446 | } | |
d05aa8bb | 1447 | memset(iov.iov_base, 0, num); |
61007b31 SH |
1448 | } |
1449 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1450 | ||
d05aa8bb | 1451 | ret = bdrv_driver_pwritev(bs, offset, num, &qiov, write_flags); |
61007b31 SH |
1452 | |
1453 | /* Keep bounce buffer around if it is big enough for all | |
1454 | * all future requests. | |
1455 | */ | |
5def6b80 | 1456 | if (num < max_transfer) { |
61007b31 SH |
1457 | qemu_vfree(iov.iov_base); |
1458 | iov.iov_base = NULL; | |
1459 | } | |
1460 | } | |
1461 | ||
d05aa8bb | 1462 | offset += num; |
f5a5ca79 | 1463 | bytes -= num; |
61007b31 SH |
1464 | } |
1465 | ||
1466 | fail: | |
465fe887 EB |
1467 | if (ret == 0 && need_flush) { |
1468 | ret = bdrv_co_flush(bs); | |
1469 | } | |
61007b31 SH |
1470 | qemu_vfree(iov.iov_base); |
1471 | return ret; | |
1472 | } | |
1473 | ||
1474 | /* | |
04ed95f4 EB |
1475 | * Forwards an already correctly aligned write request to the BlockDriver, |
1476 | * after possibly fragmenting it. | |
61007b31 | 1477 | */ |
85c97ca7 | 1478 | static int coroutine_fn bdrv_aligned_pwritev(BdrvChild *child, |
61007b31 | 1479 | BdrvTrackedRequest *req, int64_t offset, unsigned int bytes, |
cff86b38 | 1480 | int64_t align, QEMUIOVector *qiov, int flags) |
61007b31 | 1481 | { |
85c97ca7 | 1482 | BlockDriverState *bs = child->bs; |
61007b31 SH |
1483 | BlockDriver *drv = bs->drv; |
1484 | bool waited; | |
1485 | int ret; | |
1486 | ||
9896c876 | 1487 | int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE); |
04ed95f4 EB |
1488 | uint64_t bytes_remaining = bytes; |
1489 | int max_transfer; | |
61007b31 | 1490 | |
d470ad42 HR |
1491 | if (!drv) { |
1492 | return -ENOMEDIUM; | |
1493 | } | |
1494 | ||
d6883bc9 VSO |
1495 | if (bdrv_has_readonly_bitmaps(bs)) { |
1496 | return -EPERM; | |
1497 | } | |
1498 | ||
cff86b38 EB |
1499 | assert(is_power_of_2(align)); |
1500 | assert((offset & (align - 1)) == 0); | |
1501 | assert((bytes & (align - 1)) == 0); | |
61007b31 | 1502 | assert(!qiov || bytes == qiov->size); |
abb06c5a | 1503 | assert((bs->open_flags & BDRV_O_NO_IO) == 0); |
fa166538 | 1504 | assert(!(flags & ~BDRV_REQ_MASK)); |
04ed95f4 EB |
1505 | max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX), |
1506 | align); | |
61007b31 SH |
1507 | |
1508 | waited = wait_serialising_requests(req); | |
1509 | assert(!waited || !req->serialising); | |
1510 | assert(req->overlap_offset <= offset); | |
1511 | assert(offset + bytes <= req->overlap_offset + req->overlap_bytes); | |
c6035964 HR |
1512 | if (flags & BDRV_REQ_WRITE_UNCHANGED) { |
1513 | assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE)); | |
1514 | } else { | |
1515 | assert(child->perm & BLK_PERM_WRITE); | |
1516 | } | |
362b3786 | 1517 | assert(end_sector <= bs->total_sectors || child->perm & BLK_PERM_RESIZE); |
61007b31 SH |
1518 | |
1519 | ret = notifier_with_return_list_notify(&bs->before_write_notifiers, req); | |
1520 | ||
1521 | if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF && | |
c1499a5e | 1522 | !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_pwrite_zeroes && |
61007b31 SH |
1523 | qemu_iovec_is_zero(qiov)) { |
1524 | flags |= BDRV_REQ_ZERO_WRITE; | |
1525 | if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) { | |
1526 | flags |= BDRV_REQ_MAY_UNMAP; | |
1527 | } | |
1528 | } | |
1529 | ||
1530 | if (ret < 0) { | |
1531 | /* Do nothing, write notifier decided to fail this request */ | |
1532 | } else if (flags & BDRV_REQ_ZERO_WRITE) { | |
9a4f4c31 | 1533 | bdrv_debug_event(bs, BLKDBG_PWRITEV_ZERO); |
9896c876 | 1534 | ret = bdrv_co_do_pwrite_zeroes(bs, offset, bytes, flags); |
3ea1a091 PB |
1535 | } else if (flags & BDRV_REQ_WRITE_COMPRESSED) { |
1536 | ret = bdrv_driver_pwritev_compressed(bs, offset, bytes, qiov); | |
04ed95f4 | 1537 | } else if (bytes <= max_transfer) { |
9a4f4c31 | 1538 | bdrv_debug_event(bs, BLKDBG_PWRITEV); |
78a07294 | 1539 | ret = bdrv_driver_pwritev(bs, offset, bytes, qiov, flags); |
04ed95f4 EB |
1540 | } else { |
1541 | bdrv_debug_event(bs, BLKDBG_PWRITEV); | |
1542 | while (bytes_remaining) { | |
1543 | int num = MIN(bytes_remaining, max_transfer); | |
1544 | QEMUIOVector local_qiov; | |
1545 | int local_flags = flags; | |
1546 | ||
1547 | assert(num); | |
1548 | if (num < bytes_remaining && (flags & BDRV_REQ_FUA) && | |
1549 | !(bs->supported_write_flags & BDRV_REQ_FUA)) { | |
1550 | /* If FUA is going to be emulated by flush, we only | |
1551 | * need to flush on the last iteration */ | |
1552 | local_flags &= ~BDRV_REQ_FUA; | |
1553 | } | |
1554 | qemu_iovec_init(&local_qiov, qiov->niov); | |
1555 | qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num); | |
1556 | ||
1557 | ret = bdrv_driver_pwritev(bs, offset + bytes - bytes_remaining, | |
1558 | num, &local_qiov, local_flags); | |
1559 | qemu_iovec_destroy(&local_qiov); | |
1560 | if (ret < 0) { | |
1561 | break; | |
1562 | } | |
1563 | bytes_remaining -= num; | |
1564 | } | |
61007b31 | 1565 | } |
9a4f4c31 | 1566 | bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE); |
61007b31 | 1567 | |
47fec599 | 1568 | atomic_inc(&bs->write_gen); |
0fdf1a4f | 1569 | bdrv_set_dirty(bs, offset, bytes); |
61007b31 | 1570 | |
f7946da2 | 1571 | stat64_max(&bs->wr_highest_offset, offset + bytes); |
61007b31 SH |
1572 | |
1573 | if (ret >= 0) { | |
9896c876 | 1574 | bs->total_sectors = MAX(bs->total_sectors, end_sector); |
04ed95f4 | 1575 | ret = 0; |
61007b31 SH |
1576 | } |
1577 | ||
1578 | return ret; | |
1579 | } | |
1580 | ||
85c97ca7 | 1581 | static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvChild *child, |
9eeb6dd1 FZ |
1582 | int64_t offset, |
1583 | unsigned int bytes, | |
1584 | BdrvRequestFlags flags, | |
1585 | BdrvTrackedRequest *req) | |
1586 | { | |
85c97ca7 | 1587 | BlockDriverState *bs = child->bs; |
9eeb6dd1 FZ |
1588 | uint8_t *buf = NULL; |
1589 | QEMUIOVector local_qiov; | |
1590 | struct iovec iov; | |
a5b8dd2c | 1591 | uint64_t align = bs->bl.request_alignment; |
9eeb6dd1 FZ |
1592 | unsigned int head_padding_bytes, tail_padding_bytes; |
1593 | int ret = 0; | |
1594 | ||
1595 | head_padding_bytes = offset & (align - 1); | |
f13ce1be | 1596 | tail_padding_bytes = (align - (offset + bytes)) & (align - 1); |
9eeb6dd1 FZ |
1597 | |
1598 | ||
1599 | assert(flags & BDRV_REQ_ZERO_WRITE); | |
1600 | if (head_padding_bytes || tail_padding_bytes) { | |
1601 | buf = qemu_blockalign(bs, align); | |
1602 | iov = (struct iovec) { | |
1603 | .iov_base = buf, | |
1604 | .iov_len = align, | |
1605 | }; | |
1606 | qemu_iovec_init_external(&local_qiov, &iov, 1); | |
1607 | } | |
1608 | if (head_padding_bytes) { | |
1609 | uint64_t zero_bytes = MIN(bytes, align - head_padding_bytes); | |
1610 | ||
1611 | /* RMW the unaligned part before head. */ | |
1612 | mark_request_serialising(req, align); | |
1613 | wait_serialising_requests(req); | |
9a4f4c31 | 1614 | bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD); |
85c97ca7 | 1615 | ret = bdrv_aligned_preadv(child, req, offset & ~(align - 1), align, |
9eeb6dd1 FZ |
1616 | align, &local_qiov, 0); |
1617 | if (ret < 0) { | |
1618 | goto fail; | |
1619 | } | |
9a4f4c31 | 1620 | bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD); |
9eeb6dd1 FZ |
1621 | |
1622 | memset(buf + head_padding_bytes, 0, zero_bytes); | |
85c97ca7 | 1623 | ret = bdrv_aligned_pwritev(child, req, offset & ~(align - 1), align, |
cff86b38 | 1624 | align, &local_qiov, |
9eeb6dd1 FZ |
1625 | flags & ~BDRV_REQ_ZERO_WRITE); |
1626 | if (ret < 0) { | |
1627 | goto fail; | |
1628 | } | |
1629 | offset += zero_bytes; | |
1630 | bytes -= zero_bytes; | |
1631 | } | |
1632 | ||
1633 | assert(!bytes || (offset & (align - 1)) == 0); | |
1634 | if (bytes >= align) { | |
1635 | /* Write the aligned part in the middle. */ | |
1636 | uint64_t aligned_bytes = bytes & ~(align - 1); | |
85c97ca7 | 1637 | ret = bdrv_aligned_pwritev(child, req, offset, aligned_bytes, align, |
9eeb6dd1 FZ |
1638 | NULL, flags); |
1639 | if (ret < 0) { | |
1640 | goto fail; | |
1641 | } | |
1642 | bytes -= aligned_bytes; | |
1643 | offset += aligned_bytes; | |
1644 | } | |
1645 | ||
1646 | assert(!bytes || (offset & (align - 1)) == 0); | |
1647 | if (bytes) { | |
1648 | assert(align == tail_padding_bytes + bytes); | |
1649 | /* RMW the unaligned part after tail. */ | |
1650 | mark_request_serialising(req, align); | |
1651 | wait_serialising_requests(req); | |
9a4f4c31 | 1652 | bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL); |
85c97ca7 | 1653 | ret = bdrv_aligned_preadv(child, req, offset, align, |
9eeb6dd1 FZ |
1654 | align, &local_qiov, 0); |
1655 | if (ret < 0) { | |
1656 | goto fail; | |
1657 | } | |
9a4f4c31 | 1658 | bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL); |
9eeb6dd1 FZ |
1659 | |
1660 | memset(buf, 0, bytes); | |
85c97ca7 | 1661 | ret = bdrv_aligned_pwritev(child, req, offset, align, align, |
9eeb6dd1 FZ |
1662 | &local_qiov, flags & ~BDRV_REQ_ZERO_WRITE); |
1663 | } | |
1664 | fail: | |
1665 | qemu_vfree(buf); | |
1666 | return ret; | |
1667 | ||
1668 | } | |
1669 | ||
61007b31 SH |
1670 | /* |
1671 | * Handle a write request in coroutine context | |
1672 | */ | |
a03ef88f | 1673 | int coroutine_fn bdrv_co_pwritev(BdrvChild *child, |
61007b31 SH |
1674 | int64_t offset, unsigned int bytes, QEMUIOVector *qiov, |
1675 | BdrvRequestFlags flags) | |
1676 | { | |
a03ef88f | 1677 | BlockDriverState *bs = child->bs; |
61007b31 | 1678 | BdrvTrackedRequest req; |
a5b8dd2c | 1679 | uint64_t align = bs->bl.request_alignment; |
61007b31 SH |
1680 | uint8_t *head_buf = NULL; |
1681 | uint8_t *tail_buf = NULL; | |
1682 | QEMUIOVector local_qiov; | |
1683 | bool use_local_qiov = false; | |
1684 | int ret; | |
1685 | ||
f42cf447 DB |
1686 | trace_bdrv_co_pwritev(child->bs, offset, bytes, flags); |
1687 | ||
61007b31 SH |
1688 | if (!bs->drv) { |
1689 | return -ENOMEDIUM; | |
1690 | } | |
1691 | if (bs->read_only) { | |
eaf5fe2d | 1692 | return -EPERM; |
61007b31 | 1693 | } |
04c01a5c | 1694 | assert(!(bs->open_flags & BDRV_O_INACTIVE)); |
61007b31 SH |
1695 | |
1696 | ret = bdrv_check_byte_request(bs, offset, bytes); | |
1697 | if (ret < 0) { | |
1698 | return ret; | |
1699 | } | |
1700 | ||
99723548 | 1701 | bdrv_inc_in_flight(bs); |
61007b31 SH |
1702 | /* |
1703 | * Align write if necessary by performing a read-modify-write cycle. | |
1704 | * Pad qiov with the read parts and be sure to have a tracked request not | |
1705 | * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle. | |
1706 | */ | |
ebde595c | 1707 | tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE); |
61007b31 | 1708 | |
18a59f03 | 1709 | if (flags & BDRV_REQ_ZERO_WRITE) { |
85c97ca7 | 1710 | ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req); |
9eeb6dd1 FZ |
1711 | goto out; |
1712 | } | |
1713 | ||
61007b31 SH |
1714 | if (offset & (align - 1)) { |
1715 | QEMUIOVector head_qiov; | |
1716 | struct iovec head_iov; | |
1717 | ||
1718 | mark_request_serialising(&req, align); | |
1719 | wait_serialising_requests(&req); | |
1720 | ||
1721 | head_buf = qemu_blockalign(bs, align); | |
1722 | head_iov = (struct iovec) { | |
1723 | .iov_base = head_buf, | |
1724 | .iov_len = align, | |
1725 | }; | |
1726 | qemu_iovec_init_external(&head_qiov, &head_iov, 1); | |
1727 | ||
9a4f4c31 | 1728 | bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_HEAD); |
85c97ca7 | 1729 | ret = bdrv_aligned_preadv(child, &req, offset & ~(align - 1), align, |
61007b31 SH |
1730 | align, &head_qiov, 0); |
1731 | if (ret < 0) { | |
1732 | goto fail; | |
1733 | } | |
9a4f4c31 | 1734 | bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD); |
61007b31 SH |
1735 | |
1736 | qemu_iovec_init(&local_qiov, qiov->niov + 2); | |
1737 | qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1)); | |
1738 | qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); | |
1739 | use_local_qiov = true; | |
1740 | ||
1741 | bytes += offset & (align - 1); | |
1742 | offset = offset & ~(align - 1); | |
117bc3fa PL |
1743 | |
1744 | /* We have read the tail already if the request is smaller | |
1745 | * than one aligned block. | |
1746 | */ | |
1747 | if (bytes < align) { | |
1748 | qemu_iovec_add(&local_qiov, head_buf + bytes, align - bytes); | |
1749 | bytes = align; | |
1750 | } | |
61007b31 SH |
1751 | } |
1752 | ||
1753 | if ((offset + bytes) & (align - 1)) { | |
1754 | QEMUIOVector tail_qiov; | |
1755 | struct iovec tail_iov; | |
1756 | size_t tail_bytes; | |
1757 | bool waited; | |
1758 | ||
1759 | mark_request_serialising(&req, align); | |
1760 | waited = wait_serialising_requests(&req); | |
1761 | assert(!waited || !use_local_qiov); | |
1762 | ||
1763 | tail_buf = qemu_blockalign(bs, align); | |
1764 | tail_iov = (struct iovec) { | |
1765 | .iov_base = tail_buf, | |
1766 | .iov_len = align, | |
1767 | }; | |
1768 | qemu_iovec_init_external(&tail_qiov, &tail_iov, 1); | |
1769 | ||
9a4f4c31 | 1770 | bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_TAIL); |
85c97ca7 KW |
1771 | ret = bdrv_aligned_preadv(child, &req, (offset + bytes) & ~(align - 1), |
1772 | align, align, &tail_qiov, 0); | |
61007b31 SH |
1773 | if (ret < 0) { |
1774 | goto fail; | |
1775 | } | |
9a4f4c31 | 1776 | bdrv_debug_event(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL); |
61007b31 SH |
1777 | |
1778 | if (!use_local_qiov) { | |
1779 | qemu_iovec_init(&local_qiov, qiov->niov + 1); | |
1780 | qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); | |
1781 | use_local_qiov = true; | |
1782 | } | |
1783 | ||
1784 | tail_bytes = (offset + bytes) & (align - 1); | |
1785 | qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, align - tail_bytes); | |
1786 | ||
1787 | bytes = ROUND_UP(bytes, align); | |
1788 | } | |
1789 | ||
85c97ca7 | 1790 | ret = bdrv_aligned_pwritev(child, &req, offset, bytes, align, |
3ea1a091 PB |
1791 | use_local_qiov ? &local_qiov : qiov, |
1792 | flags); | |
61007b31 SH |
1793 | |
1794 | fail: | |
61007b31 SH |
1795 | |
1796 | if (use_local_qiov) { | |
1797 | qemu_iovec_destroy(&local_qiov); | |
1798 | } | |
1799 | qemu_vfree(head_buf); | |
1800 | qemu_vfree(tail_buf); | |
9eeb6dd1 FZ |
1801 | out: |
1802 | tracked_request_end(&req); | |
99723548 | 1803 | bdrv_dec_in_flight(bs); |
61007b31 SH |
1804 | return ret; |
1805 | } | |
1806 | ||
adad6496 | 1807 | static int coroutine_fn bdrv_co_do_writev(BdrvChild *child, |
61007b31 SH |
1808 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
1809 | BdrvRequestFlags flags) | |
1810 | { | |
1811 | if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { | |
1812 | return -EINVAL; | |
1813 | } | |
1814 | ||
a03ef88f | 1815 | return bdrv_co_pwritev(child, sector_num << BDRV_SECTOR_BITS, |
cab3a356 | 1816 | nb_sectors << BDRV_SECTOR_BITS, qiov, flags); |
61007b31 SH |
1817 | } |
1818 | ||
25ec177d | 1819 | int coroutine_fn bdrv_co_writev(BdrvChild *child, int64_t sector_num, |
61007b31 SH |
1820 | int nb_sectors, QEMUIOVector *qiov) |
1821 | { | |
adad6496 | 1822 | return bdrv_co_do_writev(child, sector_num, nb_sectors, qiov, 0); |
61007b31 SH |
1823 | } |
1824 | ||
a03ef88f | 1825 | int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, |
f5a5ca79 | 1826 | int bytes, BdrvRequestFlags flags) |
61007b31 | 1827 | { |
f5a5ca79 | 1828 | trace_bdrv_co_pwrite_zeroes(child->bs, offset, bytes, flags); |
61007b31 | 1829 | |
a03ef88f | 1830 | if (!(child->bs->open_flags & BDRV_O_UNMAP)) { |
61007b31 SH |
1831 | flags &= ~BDRV_REQ_MAY_UNMAP; |
1832 | } | |
61007b31 | 1833 | |
f5a5ca79 | 1834 | return bdrv_co_pwritev(child, offset, bytes, NULL, |
74021bc4 | 1835 | BDRV_REQ_ZERO_WRITE | flags); |
61007b31 SH |
1836 | } |
1837 | ||
4085f5c7 JS |
1838 | /* |
1839 | * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not. | |
1840 | */ | |
1841 | int bdrv_flush_all(void) | |
1842 | { | |
1843 | BdrvNextIterator it; | |
1844 | BlockDriverState *bs = NULL; | |
1845 | int result = 0; | |
1846 | ||
1847 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { | |
1848 | AioContext *aio_context = bdrv_get_aio_context(bs); | |
1849 | int ret; | |
1850 | ||
1851 | aio_context_acquire(aio_context); | |
1852 | ret = bdrv_flush(bs); | |
1853 | if (ret < 0 && !result) { | |
1854 | result = ret; | |
1855 | } | |
1856 | aio_context_release(aio_context); | |
1857 | } | |
1858 | ||
1859 | return result; | |
1860 | } | |
1861 | ||
1862 | ||
4bcd936e | 1863 | typedef struct BdrvCoBlockStatusData { |
61007b31 SH |
1864 | BlockDriverState *bs; |
1865 | BlockDriverState *base; | |
c9ce8c4d | 1866 | bool want_zero; |
4bcd936e EB |
1867 | int64_t offset; |
1868 | int64_t bytes; | |
1869 | int64_t *pnum; | |
1870 | int64_t *map; | |
c9ce8c4d | 1871 | BlockDriverState **file; |
4bcd936e | 1872 | int ret; |
61007b31 | 1873 | bool done; |
4bcd936e | 1874 | } BdrvCoBlockStatusData; |
61007b31 | 1875 | |
3e4d0e72 EB |
1876 | int coroutine_fn bdrv_co_block_status_from_file(BlockDriverState *bs, |
1877 | bool want_zero, | |
1878 | int64_t offset, | |
1879 | int64_t bytes, | |
1880 | int64_t *pnum, | |
1881 | int64_t *map, | |
1882 | BlockDriverState **file) | |
f7cc69b3 MP |
1883 | { |
1884 | assert(bs->file && bs->file->bs); | |
3e4d0e72 EB |
1885 | *pnum = bytes; |
1886 | *map = offset; | |
f7cc69b3 | 1887 | *file = bs->file->bs; |
3e4d0e72 | 1888 | return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID; |
f7cc69b3 MP |
1889 | } |
1890 | ||
3e4d0e72 EB |
1891 | int coroutine_fn bdrv_co_block_status_from_backing(BlockDriverState *bs, |
1892 | bool want_zero, | |
1893 | int64_t offset, | |
1894 | int64_t bytes, | |
1895 | int64_t *pnum, | |
1896 | int64_t *map, | |
1897 | BlockDriverState **file) | |
f7cc69b3 MP |
1898 | { |
1899 | assert(bs->backing && bs->backing->bs); | |
3e4d0e72 EB |
1900 | *pnum = bytes; |
1901 | *map = offset; | |
f7cc69b3 | 1902 | *file = bs->backing->bs; |
3e4d0e72 | 1903 | return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID; |
f7cc69b3 MP |
1904 | } |
1905 | ||
61007b31 SH |
1906 | /* |
1907 | * Returns the allocation status of the specified sectors. | |
1908 | * Drivers not implementing the functionality are assumed to not support | |
1909 | * backing files, hence all their sectors are reported as allocated. | |
1910 | * | |
86a3d5c6 EB |
1911 | * If 'want_zero' is true, the caller is querying for mapping |
1912 | * purposes, with a focus on valid BDRV_BLOCK_OFFSET_VALID, _DATA, and | |
1913 | * _ZERO where possible; otherwise, the result favors larger 'pnum', | |
1914 | * with a focus on accurate BDRV_BLOCK_ALLOCATED. | |
c9ce8c4d | 1915 | * |
2e8bc787 | 1916 | * If 'offset' is beyond the end of the disk image the return value is |
fb0d8654 | 1917 | * BDRV_BLOCK_EOF and 'pnum' is set to 0. |
61007b31 | 1918 | * |
2e8bc787 | 1919 | * 'bytes' is the max value 'pnum' should be set to. If bytes goes |
fb0d8654 EB |
1920 | * beyond the end of the disk image it will be clamped; if 'pnum' is set to |
1921 | * the end of the image, then the returned value will include BDRV_BLOCK_EOF. | |
67a0fd2a | 1922 | * |
2e8bc787 EB |
1923 | * 'pnum' is set to the number of bytes (including and immediately |
1924 | * following the specified offset) that are easily known to be in the | |
1925 | * same allocated/unallocated state. Note that a second call starting | |
1926 | * at the original offset plus returned pnum may have the same status. | |
1927 | * The returned value is non-zero on success except at end-of-file. | |
1928 | * | |
1929 | * Returns negative errno on failure. Otherwise, if the | |
1930 | * BDRV_BLOCK_OFFSET_VALID bit is set, 'map' and 'file' (if non-NULL) are | |
1931 | * set to the host mapping and BDS corresponding to the guest offset. | |
61007b31 | 1932 | */ |
2e8bc787 EB |
1933 | static int coroutine_fn bdrv_co_block_status(BlockDriverState *bs, |
1934 | bool want_zero, | |
1935 | int64_t offset, int64_t bytes, | |
1936 | int64_t *pnum, int64_t *map, | |
1937 | BlockDriverState **file) | |
1938 | { | |
1939 | int64_t total_size; | |
1940 | int64_t n; /* bytes */ | |
efa6e2ed | 1941 | int ret; |
2e8bc787 | 1942 | int64_t local_map = 0; |
298a1665 | 1943 | BlockDriverState *local_file = NULL; |
efa6e2ed EB |
1944 | int64_t aligned_offset, aligned_bytes; |
1945 | uint32_t align; | |
61007b31 | 1946 | |
298a1665 EB |
1947 | assert(pnum); |
1948 | *pnum = 0; | |
2e8bc787 EB |
1949 | total_size = bdrv_getlength(bs); |
1950 | if (total_size < 0) { | |
1951 | ret = total_size; | |
298a1665 | 1952 | goto early_out; |
61007b31 SH |
1953 | } |
1954 | ||
2e8bc787 | 1955 | if (offset >= total_size) { |
298a1665 EB |
1956 | ret = BDRV_BLOCK_EOF; |
1957 | goto early_out; | |
61007b31 | 1958 | } |
2e8bc787 | 1959 | if (!bytes) { |
298a1665 EB |
1960 | ret = 0; |
1961 | goto early_out; | |
9cdcfd9f | 1962 | } |
61007b31 | 1963 | |
2e8bc787 EB |
1964 | n = total_size - offset; |
1965 | if (n < bytes) { | |
1966 | bytes = n; | |
61007b31 SH |
1967 | } |
1968 | ||
d470ad42 HR |
1969 | /* Must be non-NULL or bdrv_getlength() would have failed */ |
1970 | assert(bs->drv); | |
636cb512 | 1971 | if (!bs->drv->bdrv_co_block_status) { |
2e8bc787 | 1972 | *pnum = bytes; |
61007b31 | 1973 | ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED; |
2e8bc787 | 1974 | if (offset + bytes == total_size) { |
fb0d8654 EB |
1975 | ret |= BDRV_BLOCK_EOF; |
1976 | } | |
61007b31 | 1977 | if (bs->drv->protocol_name) { |
2e8bc787 EB |
1978 | ret |= BDRV_BLOCK_OFFSET_VALID; |
1979 | local_map = offset; | |
298a1665 | 1980 | local_file = bs; |
61007b31 | 1981 | } |
298a1665 | 1982 | goto early_out; |
61007b31 SH |
1983 | } |
1984 | ||
99723548 | 1985 | bdrv_inc_in_flight(bs); |
efa6e2ed EB |
1986 | |
1987 | /* Round out to request_alignment boundaries */ | |
86a3d5c6 | 1988 | align = bs->bl.request_alignment; |
efa6e2ed EB |
1989 | aligned_offset = QEMU_ALIGN_DOWN(offset, align); |
1990 | aligned_bytes = ROUND_UP(offset + bytes, align) - aligned_offset; | |
1991 | ||
636cb512 EB |
1992 | ret = bs->drv->bdrv_co_block_status(bs, want_zero, aligned_offset, |
1993 | aligned_bytes, pnum, &local_map, | |
1994 | &local_file); | |
1995 | if (ret < 0) { | |
1996 | *pnum = 0; | |
1997 | goto out; | |
efa6e2ed EB |
1998 | } |
1999 | ||
2e8bc787 | 2000 | /* |
636cb512 | 2001 | * The driver's result must be a non-zero multiple of request_alignment. |
efa6e2ed | 2002 | * Clamp pnum and adjust map to original request. |
2e8bc787 | 2003 | */ |
636cb512 EB |
2004 | assert(*pnum && QEMU_IS_ALIGNED(*pnum, align) && |
2005 | align > offset - aligned_offset); | |
efa6e2ed EB |
2006 | *pnum -= offset - aligned_offset; |
2007 | if (*pnum > bytes) { | |
2008 | *pnum = bytes; | |
61007b31 | 2009 | } |
2e8bc787 | 2010 | if (ret & BDRV_BLOCK_OFFSET_VALID) { |
efa6e2ed | 2011 | local_map += offset - aligned_offset; |
2e8bc787 | 2012 | } |
61007b31 SH |
2013 | |
2014 | if (ret & BDRV_BLOCK_RAW) { | |
298a1665 | 2015 | assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file); |
2e8bc787 EB |
2016 | ret = bdrv_co_block_status(local_file, want_zero, local_map, |
2017 | *pnum, pnum, &local_map, &local_file); | |
99723548 | 2018 | goto out; |
61007b31 SH |
2019 | } |
2020 | ||
2021 | if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) { | |
2022 | ret |= BDRV_BLOCK_ALLOCATED; | |
c9ce8c4d | 2023 | } else if (want_zero) { |
61007b31 SH |
2024 | if (bdrv_unallocated_blocks_are_zero(bs)) { |
2025 | ret |= BDRV_BLOCK_ZERO; | |
760e0063 KW |
2026 | } else if (bs->backing) { |
2027 | BlockDriverState *bs2 = bs->backing->bs; | |
2e8bc787 | 2028 | int64_t size2 = bdrv_getlength(bs2); |
c9ce8c4d | 2029 | |
2e8bc787 | 2030 | if (size2 >= 0 && offset >= size2) { |
61007b31 SH |
2031 | ret |= BDRV_BLOCK_ZERO; |
2032 | } | |
2033 | } | |
2034 | } | |
2035 | ||
c9ce8c4d | 2036 | if (want_zero && local_file && local_file != bs && |
61007b31 SH |
2037 | (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && |
2038 | (ret & BDRV_BLOCK_OFFSET_VALID)) { | |
2e8bc787 EB |
2039 | int64_t file_pnum; |
2040 | int ret2; | |
61007b31 | 2041 | |
2e8bc787 EB |
2042 | ret2 = bdrv_co_block_status(local_file, want_zero, local_map, |
2043 | *pnum, &file_pnum, NULL, NULL); | |
61007b31 SH |
2044 | if (ret2 >= 0) { |
2045 | /* Ignore errors. This is just providing extra information, it | |
2046 | * is useful but not necessary. | |
2047 | */ | |
c61e684e EB |
2048 | if (ret2 & BDRV_BLOCK_EOF && |
2049 | (!file_pnum || ret2 & BDRV_BLOCK_ZERO)) { | |
2050 | /* | |
2051 | * It is valid for the format block driver to read | |
2052 | * beyond the end of the underlying file's current | |
2053 | * size; such areas read as zero. | |
2054 | */ | |
61007b31 SH |
2055 | ret |= BDRV_BLOCK_ZERO; |
2056 | } else { | |
2057 | /* Limit request to the range reported by the protocol driver */ | |
2058 | *pnum = file_pnum; | |
2059 | ret |= (ret2 & BDRV_BLOCK_ZERO); | |
2060 | } | |
2061 | } | |
2062 | } | |
2063 | ||
99723548 PB |
2064 | out: |
2065 | bdrv_dec_in_flight(bs); | |
2e8bc787 | 2066 | if (ret >= 0 && offset + *pnum == total_size) { |
fb0d8654 EB |
2067 | ret |= BDRV_BLOCK_EOF; |
2068 | } | |
298a1665 EB |
2069 | early_out: |
2070 | if (file) { | |
2071 | *file = local_file; | |
2072 | } | |
2e8bc787 EB |
2073 | if (map) { |
2074 | *map = local_map; | |
2075 | } | |
61007b31 SH |
2076 | return ret; |
2077 | } | |
2078 | ||
5b648c67 EB |
2079 | static int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs, |
2080 | BlockDriverState *base, | |
2081 | bool want_zero, | |
2082 | int64_t offset, | |
2083 | int64_t bytes, | |
2084 | int64_t *pnum, | |
2085 | int64_t *map, | |
2086 | BlockDriverState **file) | |
ba3f0e25 FZ |
2087 | { |
2088 | BlockDriverState *p; | |
5b648c67 | 2089 | int ret = 0; |
c61e684e | 2090 | bool first = true; |
ba3f0e25 FZ |
2091 | |
2092 | assert(bs != base); | |
760e0063 | 2093 | for (p = bs; p != base; p = backing_bs(p)) { |
5b648c67 EB |
2094 | ret = bdrv_co_block_status(p, want_zero, offset, bytes, pnum, map, |
2095 | file); | |
c61e684e EB |
2096 | if (ret < 0) { |
2097 | break; | |
2098 | } | |
2099 | if (ret & BDRV_BLOCK_ZERO && ret & BDRV_BLOCK_EOF && !first) { | |
2100 | /* | |
2101 | * Reading beyond the end of the file continues to read | |
2102 | * zeroes, but we can only widen the result to the | |
2103 | * unallocated length we learned from an earlier | |
2104 | * iteration. | |
2105 | */ | |
5b648c67 | 2106 | *pnum = bytes; |
c61e684e EB |
2107 | } |
2108 | if (ret & (BDRV_BLOCK_ZERO | BDRV_BLOCK_DATA)) { | |
ba3f0e25 FZ |
2109 | break; |
2110 | } | |
5b648c67 EB |
2111 | /* [offset, pnum] unallocated on this layer, which could be only |
2112 | * the first part of [offset, bytes]. */ | |
2113 | bytes = MIN(bytes, *pnum); | |
c61e684e | 2114 | first = false; |
ba3f0e25 FZ |
2115 | } |
2116 | return ret; | |
2117 | } | |
2118 | ||
31826642 | 2119 | /* Coroutine wrapper for bdrv_block_status_above() */ |
5b648c67 | 2120 | static void coroutine_fn bdrv_block_status_above_co_entry(void *opaque) |
61007b31 | 2121 | { |
4bcd936e | 2122 | BdrvCoBlockStatusData *data = opaque; |
61007b31 | 2123 | |
5b648c67 EB |
2124 | data->ret = bdrv_co_block_status_above(data->bs, data->base, |
2125 | data->want_zero, | |
2126 | data->offset, data->bytes, | |
2127 | data->pnum, data->map, data->file); | |
61007b31 SH |
2128 | data->done = true; |
2129 | } | |
2130 | ||
2131 | /* | |
5b648c67 | 2132 | * Synchronous wrapper around bdrv_co_block_status_above(). |
61007b31 | 2133 | * |
5b648c67 | 2134 | * See bdrv_co_block_status_above() for details. |
61007b31 | 2135 | */ |
7ddb99b9 EB |
2136 | static int bdrv_common_block_status_above(BlockDriverState *bs, |
2137 | BlockDriverState *base, | |
2138 | bool want_zero, int64_t offset, | |
2139 | int64_t bytes, int64_t *pnum, | |
2140 | int64_t *map, | |
2141 | BlockDriverState **file) | |
61007b31 SH |
2142 | { |
2143 | Coroutine *co; | |
4bcd936e | 2144 | BdrvCoBlockStatusData data = { |
61007b31 | 2145 | .bs = bs, |
ba3f0e25 | 2146 | .base = base, |
c9ce8c4d | 2147 | .want_zero = want_zero, |
7ddb99b9 EB |
2148 | .offset = offset, |
2149 | .bytes = bytes, | |
2150 | .pnum = pnum, | |
2151 | .map = map, | |
c9ce8c4d | 2152 | .file = file, |
61007b31 SH |
2153 | .done = false, |
2154 | }; | |
2155 | ||
2156 | if (qemu_in_coroutine()) { | |
2157 | /* Fast-path if already in coroutine context */ | |
5b648c67 | 2158 | bdrv_block_status_above_co_entry(&data); |
61007b31 | 2159 | } else { |
5b648c67 | 2160 | co = qemu_coroutine_create(bdrv_block_status_above_co_entry, &data); |
e92f0e19 | 2161 | bdrv_coroutine_enter(bs, co); |
88b062c2 | 2162 | BDRV_POLL_WHILE(bs, !data.done); |
61007b31 | 2163 | } |
7ddb99b9 | 2164 | return data.ret; |
61007b31 SH |
2165 | } |
2166 | ||
31826642 EB |
2167 | int bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base, |
2168 | int64_t offset, int64_t bytes, int64_t *pnum, | |
2169 | int64_t *map, BlockDriverState **file) | |
c9ce8c4d | 2170 | { |
31826642 EB |
2171 | return bdrv_common_block_status_above(bs, base, true, offset, bytes, |
2172 | pnum, map, file); | |
c9ce8c4d EB |
2173 | } |
2174 | ||
237d78f8 EB |
2175 | int bdrv_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes, |
2176 | int64_t *pnum, int64_t *map, BlockDriverState **file) | |
ba3f0e25 | 2177 | { |
31826642 EB |
2178 | return bdrv_block_status_above(bs, backing_bs(bs), |
2179 | offset, bytes, pnum, map, file); | |
ba3f0e25 FZ |
2180 | } |
2181 | ||
d6a644bb EB |
2182 | int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset, |
2183 | int64_t bytes, int64_t *pnum) | |
61007b31 | 2184 | { |
7ddb99b9 EB |
2185 | int ret; |
2186 | int64_t dummy; | |
d6a644bb | 2187 | |
7ddb99b9 EB |
2188 | ret = bdrv_common_block_status_above(bs, backing_bs(bs), false, offset, |
2189 | bytes, pnum ? pnum : &dummy, NULL, | |
c9ce8c4d | 2190 | NULL); |
61007b31 SH |
2191 | if (ret < 0) { |
2192 | return ret; | |
2193 | } | |
2194 | return !!(ret & BDRV_BLOCK_ALLOCATED); | |
2195 | } | |
2196 | ||
2197 | /* | |
2198 | * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] | |
2199 | * | |
51b0a488 EB |
2200 | * Return true if (a prefix of) the given range is allocated in any image |
2201 | * between BASE and TOP (inclusive). BASE can be NULL to check if the given | |
2202 | * offset is allocated in any image of the chain. Return false otherwise, | |
d6a644bb | 2203 | * or negative errno on failure. |
61007b31 | 2204 | * |
51b0a488 EB |
2205 | * 'pnum' is set to the number of bytes (including and immediately |
2206 | * following the specified offset) that are known to be in the same | |
2207 | * allocated/unallocated state. Note that a subsequent call starting | |
2208 | * at 'offset + *pnum' may return the same allocation status (in other | |
2209 | * words, the result is not necessarily the maximum possible range); | |
2210 | * but 'pnum' will only be 0 when end of file is reached. | |
61007b31 SH |
2211 | * |
2212 | */ | |
2213 | int bdrv_is_allocated_above(BlockDriverState *top, | |
2214 | BlockDriverState *base, | |
51b0a488 | 2215 | int64_t offset, int64_t bytes, int64_t *pnum) |
61007b31 SH |
2216 | { |
2217 | BlockDriverState *intermediate; | |
51b0a488 EB |
2218 | int ret; |
2219 | int64_t n = bytes; | |
61007b31 SH |
2220 | |
2221 | intermediate = top; | |
2222 | while (intermediate && intermediate != base) { | |
d6a644bb | 2223 | int64_t pnum_inter; |
c00716be | 2224 | int64_t size_inter; |
d6a644bb | 2225 | |
51b0a488 | 2226 | ret = bdrv_is_allocated(intermediate, offset, bytes, &pnum_inter); |
61007b31 SH |
2227 | if (ret < 0) { |
2228 | return ret; | |
d6a644bb | 2229 | } |
d6a644bb | 2230 | if (ret) { |
51b0a488 | 2231 | *pnum = pnum_inter; |
61007b31 SH |
2232 | return 1; |
2233 | } | |
2234 | ||
51b0a488 | 2235 | size_inter = bdrv_getlength(intermediate); |
c00716be EB |
2236 | if (size_inter < 0) { |
2237 | return size_inter; | |
2238 | } | |
51b0a488 EB |
2239 | if (n > pnum_inter && |
2240 | (intermediate == top || offset + pnum_inter < size_inter)) { | |
2241 | n = pnum_inter; | |
61007b31 SH |
2242 | } |
2243 | ||
760e0063 | 2244 | intermediate = backing_bs(intermediate); |
61007b31 SH |
2245 | } |
2246 | ||
2247 | *pnum = n; | |
2248 | return 0; | |
2249 | } | |
2250 | ||
1a8ae822 KW |
2251 | typedef struct BdrvVmstateCo { |
2252 | BlockDriverState *bs; | |
2253 | QEMUIOVector *qiov; | |
2254 | int64_t pos; | |
2255 | bool is_read; | |
2256 | int ret; | |
2257 | } BdrvVmstateCo; | |
2258 | ||
2259 | static int coroutine_fn | |
2260 | bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos, | |
2261 | bool is_read) | |
2262 | { | |
2263 | BlockDriver *drv = bs->drv; | |
dc88a467 SH |
2264 | int ret = -ENOTSUP; |
2265 | ||
2266 | bdrv_inc_in_flight(bs); | |
1a8ae822 KW |
2267 | |
2268 | if (!drv) { | |
dc88a467 | 2269 | ret = -ENOMEDIUM; |
1a8ae822 | 2270 | } else if (drv->bdrv_load_vmstate) { |
dc88a467 SH |
2271 | if (is_read) { |
2272 | ret = drv->bdrv_load_vmstate(bs, qiov, pos); | |
2273 | } else { | |
2274 | ret = drv->bdrv_save_vmstate(bs, qiov, pos); | |
2275 | } | |
1a8ae822 | 2276 | } else if (bs->file) { |
dc88a467 | 2277 | ret = bdrv_co_rw_vmstate(bs->file->bs, qiov, pos, is_read); |
1a8ae822 KW |
2278 | } |
2279 | ||
dc88a467 SH |
2280 | bdrv_dec_in_flight(bs); |
2281 | return ret; | |
1a8ae822 KW |
2282 | } |
2283 | ||
2284 | static void coroutine_fn bdrv_co_rw_vmstate_entry(void *opaque) | |
2285 | { | |
2286 | BdrvVmstateCo *co = opaque; | |
2287 | co->ret = bdrv_co_rw_vmstate(co->bs, co->qiov, co->pos, co->is_read); | |
2288 | } | |
2289 | ||
2290 | static inline int | |
2291 | bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos, | |
2292 | bool is_read) | |
2293 | { | |
2294 | if (qemu_in_coroutine()) { | |
2295 | return bdrv_co_rw_vmstate(bs, qiov, pos, is_read); | |
2296 | } else { | |
2297 | BdrvVmstateCo data = { | |
2298 | .bs = bs, | |
2299 | .qiov = qiov, | |
2300 | .pos = pos, | |
2301 | .is_read = is_read, | |
2302 | .ret = -EINPROGRESS, | |
2303 | }; | |
0b8b8753 | 2304 | Coroutine *co = qemu_coroutine_create(bdrv_co_rw_vmstate_entry, &data); |
1a8ae822 | 2305 | |
e92f0e19 | 2306 | bdrv_coroutine_enter(bs, co); |
ea17c9d2 | 2307 | BDRV_POLL_WHILE(bs, data.ret == -EINPROGRESS); |
1a8ae822 KW |
2308 | return data.ret; |
2309 | } | |
2310 | } | |
2311 | ||
61007b31 SH |
2312 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
2313 | int64_t pos, int size) | |
2314 | { | |
2315 | QEMUIOVector qiov; | |
2316 | struct iovec iov = { | |
2317 | .iov_base = (void *) buf, | |
2318 | .iov_len = size, | |
2319 | }; | |
b433d942 | 2320 | int ret; |
61007b31 SH |
2321 | |
2322 | qemu_iovec_init_external(&qiov, &iov, 1); | |
b433d942 KW |
2323 | |
2324 | ret = bdrv_writev_vmstate(bs, &qiov, pos); | |
2325 | if (ret < 0) { | |
2326 | return ret; | |
2327 | } | |
2328 | ||
2329 | return size; | |
61007b31 SH |
2330 | } |
2331 | ||
2332 | int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) | |
2333 | { | |
1a8ae822 | 2334 | return bdrv_rw_vmstate(bs, qiov, pos, false); |
61007b31 SH |
2335 | } |
2336 | ||
2337 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, | |
2338 | int64_t pos, int size) | |
5ddda0b8 KW |
2339 | { |
2340 | QEMUIOVector qiov; | |
2341 | struct iovec iov = { | |
2342 | .iov_base = buf, | |
2343 | .iov_len = size, | |
2344 | }; | |
b433d942 | 2345 | int ret; |
5ddda0b8 KW |
2346 | |
2347 | qemu_iovec_init_external(&qiov, &iov, 1); | |
b433d942 KW |
2348 | ret = bdrv_readv_vmstate(bs, &qiov, pos); |
2349 | if (ret < 0) { | |
2350 | return ret; | |
2351 | } | |
2352 | ||
2353 | return size; | |
5ddda0b8 KW |
2354 | } |
2355 | ||
2356 | int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) | |
61007b31 | 2357 | { |
1a8ae822 | 2358 | return bdrv_rw_vmstate(bs, qiov, pos, true); |
61007b31 SH |
2359 | } |
2360 | ||
2361 | /**************************************************************/ | |
2362 | /* async I/Os */ | |
2363 | ||
61007b31 SH |
2364 | void bdrv_aio_cancel(BlockAIOCB *acb) |
2365 | { | |
2366 | qemu_aio_ref(acb); | |
2367 | bdrv_aio_cancel_async(acb); | |
2368 | while (acb->refcnt > 1) { | |
2369 | if (acb->aiocb_info->get_aio_context) { | |
2370 | aio_poll(acb->aiocb_info->get_aio_context(acb), true); | |
2371 | } else if (acb->bs) { | |
2f47da5f PB |
2372 | /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so |
2373 | * assert that we're not using an I/O thread. Thread-safe | |
2374 | * code should use bdrv_aio_cancel_async exclusively. | |
2375 | */ | |
2376 | assert(bdrv_get_aio_context(acb->bs) == qemu_get_aio_context()); | |
61007b31 SH |
2377 | aio_poll(bdrv_get_aio_context(acb->bs), true); |
2378 | } else { | |
2379 | abort(); | |
2380 | } | |
2381 | } | |
2382 | qemu_aio_unref(acb); | |
2383 | } | |
2384 | ||
2385 | /* Async version of aio cancel. The caller is not blocked if the acb implements | |
2386 | * cancel_async, otherwise we do nothing and let the request normally complete. | |
2387 | * In either case the completion callback must be called. */ | |
2388 | void bdrv_aio_cancel_async(BlockAIOCB *acb) | |
2389 | { | |
2390 | if (acb->aiocb_info->cancel_async) { | |
2391 | acb->aiocb_info->cancel_async(acb); | |
2392 | } | |
2393 | } | |
2394 | ||
61007b31 SH |
2395 | /**************************************************************/ |
2396 | /* Coroutine block device emulation */ | |
2397 | ||
e293b7a3 KW |
2398 | typedef struct FlushCo { |
2399 | BlockDriverState *bs; | |
2400 | int ret; | |
2401 | } FlushCo; | |
2402 | ||
2403 | ||
61007b31 SH |
2404 | static void coroutine_fn bdrv_flush_co_entry(void *opaque) |
2405 | { | |
e293b7a3 | 2406 | FlushCo *rwco = opaque; |
61007b31 SH |
2407 | |
2408 | rwco->ret = bdrv_co_flush(rwco->bs); | |
2409 | } | |
2410 | ||
2411 | int coroutine_fn bdrv_co_flush(BlockDriverState *bs) | |
2412 | { | |
49ca6259 FZ |
2413 | int current_gen; |
2414 | int ret = 0; | |
2415 | ||
2416 | bdrv_inc_in_flight(bs); | |
61007b31 | 2417 | |
e914404e | 2418 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs) || |
1b6bc94d | 2419 | bdrv_is_sg(bs)) { |
49ca6259 | 2420 | goto early_exit; |
61007b31 SH |
2421 | } |
2422 | ||
3783fa3d | 2423 | qemu_co_mutex_lock(&bs->reqs_lock); |
47fec599 | 2424 | current_gen = atomic_read(&bs->write_gen); |
3ff2f67a EY |
2425 | |
2426 | /* Wait until any previous flushes are completed */ | |
99723548 | 2427 | while (bs->active_flush_req) { |
3783fa3d | 2428 | qemu_co_queue_wait(&bs->flush_queue, &bs->reqs_lock); |
3ff2f67a EY |
2429 | } |
2430 | ||
3783fa3d | 2431 | /* Flushes reach this point in nondecreasing current_gen order. */ |
99723548 | 2432 | bs->active_flush_req = true; |
3783fa3d | 2433 | qemu_co_mutex_unlock(&bs->reqs_lock); |
3ff2f67a | 2434 | |
c32b82af PD |
2435 | /* Write back all layers by calling one driver function */ |
2436 | if (bs->drv->bdrv_co_flush) { | |
2437 | ret = bs->drv->bdrv_co_flush(bs); | |
2438 | goto out; | |
2439 | } | |
2440 | ||
61007b31 SH |
2441 | /* Write back cached data to the OS even with cache=unsafe */ |
2442 | BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); | |
2443 | if (bs->drv->bdrv_co_flush_to_os) { | |
2444 | ret = bs->drv->bdrv_co_flush_to_os(bs); | |
2445 | if (ret < 0) { | |
cdb5e315 | 2446 | goto out; |
61007b31 SH |
2447 | } |
2448 | } | |
2449 | ||
2450 | /* But don't actually force it to the disk with cache=unsafe */ | |
2451 | if (bs->open_flags & BDRV_O_NO_FLUSH) { | |
2452 | goto flush_parent; | |
2453 | } | |
2454 | ||
3ff2f67a EY |
2455 | /* Check if we really need to flush anything */ |
2456 | if (bs->flushed_gen == current_gen) { | |
2457 | goto flush_parent; | |
2458 | } | |
2459 | ||
61007b31 | 2460 | BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); |
d470ad42 HR |
2461 | if (!bs->drv) { |
2462 | /* bs->drv->bdrv_co_flush() might have ejected the BDS | |
2463 | * (even in case of apparent success) */ | |
2464 | ret = -ENOMEDIUM; | |
2465 | goto out; | |
2466 | } | |
61007b31 SH |
2467 | if (bs->drv->bdrv_co_flush_to_disk) { |
2468 | ret = bs->drv->bdrv_co_flush_to_disk(bs); | |
2469 | } else if (bs->drv->bdrv_aio_flush) { | |
2470 | BlockAIOCB *acb; | |
2471 | CoroutineIOCompletion co = { | |
2472 | .coroutine = qemu_coroutine_self(), | |
2473 | }; | |
2474 | ||
2475 | acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); | |
2476 | if (acb == NULL) { | |
2477 | ret = -EIO; | |
2478 | } else { | |
2479 | qemu_coroutine_yield(); | |
2480 | ret = co.ret; | |
2481 | } | |
2482 | } else { | |
2483 | /* | |
2484 | * Some block drivers always operate in either writethrough or unsafe | |
2485 | * mode and don't support bdrv_flush therefore. Usually qemu doesn't | |
2486 | * know how the server works (because the behaviour is hardcoded or | |
2487 | * depends on server-side configuration), so we can't ensure that | |
2488 | * everything is safe on disk. Returning an error doesn't work because | |
2489 | * that would break guests even if the server operates in writethrough | |
2490 | * mode. | |
2491 | * | |
2492 | * Let's hope the user knows what he's doing. | |
2493 | */ | |
2494 | ret = 0; | |
2495 | } | |
3ff2f67a | 2496 | |
61007b31 | 2497 | if (ret < 0) { |
cdb5e315 | 2498 | goto out; |
61007b31 SH |
2499 | } |
2500 | ||
2501 | /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH | |
2502 | * in the case of cache=unsafe, so there are no useless flushes. | |
2503 | */ | |
2504 | flush_parent: | |
cdb5e315 FZ |
2505 | ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0; |
2506 | out: | |
3ff2f67a | 2507 | /* Notify any pending flushes that we have completed */ |
e6af1e08 KW |
2508 | if (ret == 0) { |
2509 | bs->flushed_gen = current_gen; | |
2510 | } | |
3783fa3d PB |
2511 | |
2512 | qemu_co_mutex_lock(&bs->reqs_lock); | |
99723548 | 2513 | bs->active_flush_req = false; |
156af3ac DL |
2514 | /* Return value is ignored - it's ok if wait queue is empty */ |
2515 | qemu_co_queue_next(&bs->flush_queue); | |
3783fa3d | 2516 | qemu_co_mutex_unlock(&bs->reqs_lock); |
3ff2f67a | 2517 | |
49ca6259 | 2518 | early_exit: |
99723548 | 2519 | bdrv_dec_in_flight(bs); |
cdb5e315 | 2520 | return ret; |
61007b31 SH |
2521 | } |
2522 | ||
2523 | int bdrv_flush(BlockDriverState *bs) | |
2524 | { | |
2525 | Coroutine *co; | |
e293b7a3 | 2526 | FlushCo flush_co = { |
61007b31 SH |
2527 | .bs = bs, |
2528 | .ret = NOT_DONE, | |
2529 | }; | |
2530 | ||
2531 | if (qemu_in_coroutine()) { | |
2532 | /* Fast-path if already in coroutine context */ | |
e293b7a3 | 2533 | bdrv_flush_co_entry(&flush_co); |
61007b31 | 2534 | } else { |
0b8b8753 | 2535 | co = qemu_coroutine_create(bdrv_flush_co_entry, &flush_co); |
e92f0e19 | 2536 | bdrv_coroutine_enter(bs, co); |
88b062c2 | 2537 | BDRV_POLL_WHILE(bs, flush_co.ret == NOT_DONE); |
61007b31 SH |
2538 | } |
2539 | ||
e293b7a3 | 2540 | return flush_co.ret; |
61007b31 SH |
2541 | } |
2542 | ||
2543 | typedef struct DiscardCo { | |
2544 | BlockDriverState *bs; | |
0c51a893 | 2545 | int64_t offset; |
f5a5ca79 | 2546 | int bytes; |
61007b31 SH |
2547 | int ret; |
2548 | } DiscardCo; | |
0c51a893 | 2549 | static void coroutine_fn bdrv_pdiscard_co_entry(void *opaque) |
61007b31 SH |
2550 | { |
2551 | DiscardCo *rwco = opaque; | |
2552 | ||
f5a5ca79 | 2553 | rwco->ret = bdrv_co_pdiscard(rwco->bs, rwco->offset, rwco->bytes); |
61007b31 SH |
2554 | } |
2555 | ||
9f1963b3 | 2556 | int coroutine_fn bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset, |
f5a5ca79 | 2557 | int bytes) |
61007b31 | 2558 | { |
b1066c87 | 2559 | BdrvTrackedRequest req; |
9f1963b3 | 2560 | int max_pdiscard, ret; |
3482b9bc | 2561 | int head, tail, align; |
61007b31 SH |
2562 | |
2563 | if (!bs->drv) { | |
2564 | return -ENOMEDIUM; | |
2565 | } | |
2566 | ||
d6883bc9 VSO |
2567 | if (bdrv_has_readonly_bitmaps(bs)) { |
2568 | return -EPERM; | |
2569 | } | |
2570 | ||
f5a5ca79 | 2571 | ret = bdrv_check_byte_request(bs, offset, bytes); |
61007b31 SH |
2572 | if (ret < 0) { |
2573 | return ret; | |
2574 | } else if (bs->read_only) { | |
eaf5fe2d | 2575 | return -EPERM; |
61007b31 | 2576 | } |
04c01a5c | 2577 | assert(!(bs->open_flags & BDRV_O_INACTIVE)); |
61007b31 | 2578 | |
61007b31 SH |
2579 | /* Do nothing if disabled. */ |
2580 | if (!(bs->open_flags & BDRV_O_UNMAP)) { | |
2581 | return 0; | |
2582 | } | |
2583 | ||
02aefe43 | 2584 | if (!bs->drv->bdrv_co_pdiscard && !bs->drv->bdrv_aio_pdiscard) { |
61007b31 SH |
2585 | return 0; |
2586 | } | |
2587 | ||
3482b9bc EB |
2588 | /* Discard is advisory, but some devices track and coalesce |
2589 | * unaligned requests, so we must pass everything down rather than | |
2590 | * round here. Still, most devices will just silently ignore | |
2591 | * unaligned requests (by returning -ENOTSUP), so we must fragment | |
2592 | * the request accordingly. */ | |
02aefe43 | 2593 | align = MAX(bs->bl.pdiscard_alignment, bs->bl.request_alignment); |
b8d0a980 EB |
2594 | assert(align % bs->bl.request_alignment == 0); |
2595 | head = offset % align; | |
f5a5ca79 | 2596 | tail = (offset + bytes) % align; |
9f1963b3 | 2597 | |
99723548 | 2598 | bdrv_inc_in_flight(bs); |
f5a5ca79 | 2599 | tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_DISCARD); |
50824995 | 2600 | |
ec050f77 DL |
2601 | ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); |
2602 | if (ret < 0) { | |
2603 | goto out; | |
2604 | } | |
2605 | ||
9f1963b3 EB |
2606 | max_pdiscard = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_pdiscard, INT_MAX), |
2607 | align); | |
3482b9bc | 2608 | assert(max_pdiscard >= bs->bl.request_alignment); |
61007b31 | 2609 | |
f5a5ca79 | 2610 | while (bytes > 0) { |
f5a5ca79 | 2611 | int num = bytes; |
3482b9bc EB |
2612 | |
2613 | if (head) { | |
2614 | /* Make small requests to get to alignment boundaries. */ | |
f5a5ca79 | 2615 | num = MIN(bytes, align - head); |
3482b9bc EB |
2616 | if (!QEMU_IS_ALIGNED(num, bs->bl.request_alignment)) { |
2617 | num %= bs->bl.request_alignment; | |
2618 | } | |
2619 | head = (head + num) % align; | |
2620 | assert(num < max_pdiscard); | |
2621 | } else if (tail) { | |
2622 | if (num > align) { | |
2623 | /* Shorten the request to the last aligned cluster. */ | |
2624 | num -= tail; | |
2625 | } else if (!QEMU_IS_ALIGNED(tail, bs->bl.request_alignment) && | |
2626 | tail > bs->bl.request_alignment) { | |
2627 | tail %= bs->bl.request_alignment; | |
2628 | num -= tail; | |
2629 | } | |
2630 | } | |
2631 | /* limit request size */ | |
2632 | if (num > max_pdiscard) { | |
2633 | num = max_pdiscard; | |
2634 | } | |
61007b31 | 2635 | |
d470ad42 HR |
2636 | if (!bs->drv) { |
2637 | ret = -ENOMEDIUM; | |
2638 | goto out; | |
2639 | } | |
47a5486d EB |
2640 | if (bs->drv->bdrv_co_pdiscard) { |
2641 | ret = bs->drv->bdrv_co_pdiscard(bs, offset, num); | |
61007b31 SH |
2642 | } else { |
2643 | BlockAIOCB *acb; | |
2644 | CoroutineIOCompletion co = { | |
2645 | .coroutine = qemu_coroutine_self(), | |
2646 | }; | |
2647 | ||
4da444a0 EB |
2648 | acb = bs->drv->bdrv_aio_pdiscard(bs, offset, num, |
2649 | bdrv_co_io_em_complete, &co); | |
61007b31 | 2650 | if (acb == NULL) { |
b1066c87 FZ |
2651 | ret = -EIO; |
2652 | goto out; | |
61007b31 SH |
2653 | } else { |
2654 | qemu_coroutine_yield(); | |
2655 | ret = co.ret; | |
2656 | } | |
2657 | } | |
2658 | if (ret && ret != -ENOTSUP) { | |
b1066c87 | 2659 | goto out; |
61007b31 SH |
2660 | } |
2661 | ||
9f1963b3 | 2662 | offset += num; |
f5a5ca79 | 2663 | bytes -= num; |
61007b31 | 2664 | } |
b1066c87 FZ |
2665 | ret = 0; |
2666 | out: | |
47fec599 | 2667 | atomic_inc(&bs->write_gen); |
0fdf1a4f | 2668 | bdrv_set_dirty(bs, req.offset, req.bytes); |
b1066c87 | 2669 | tracked_request_end(&req); |
99723548 | 2670 | bdrv_dec_in_flight(bs); |
b1066c87 | 2671 | return ret; |
61007b31 SH |
2672 | } |
2673 | ||
f5a5ca79 | 2674 | int bdrv_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) |
61007b31 SH |
2675 | { |
2676 | Coroutine *co; | |
2677 | DiscardCo rwco = { | |
2678 | .bs = bs, | |
0c51a893 | 2679 | .offset = offset, |
f5a5ca79 | 2680 | .bytes = bytes, |
61007b31 SH |
2681 | .ret = NOT_DONE, |
2682 | }; | |
2683 | ||
2684 | if (qemu_in_coroutine()) { | |
2685 | /* Fast-path if already in coroutine context */ | |
0c51a893 | 2686 | bdrv_pdiscard_co_entry(&rwco); |
61007b31 | 2687 | } else { |
0c51a893 | 2688 | co = qemu_coroutine_create(bdrv_pdiscard_co_entry, &rwco); |
e92f0e19 | 2689 | bdrv_coroutine_enter(bs, co); |
88b062c2 | 2690 | BDRV_POLL_WHILE(bs, rwco.ret == NOT_DONE); |
61007b31 SH |
2691 | } |
2692 | ||
2693 | return rwco.ret; | |
2694 | } | |
2695 | ||
48af776a | 2696 | int bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf) |
61007b31 SH |
2697 | { |
2698 | BlockDriver *drv = bs->drv; | |
5c5ae76a FZ |
2699 | CoroutineIOCompletion co = { |
2700 | .coroutine = qemu_coroutine_self(), | |
2701 | }; | |
2702 | BlockAIOCB *acb; | |
61007b31 | 2703 | |
99723548 | 2704 | bdrv_inc_in_flight(bs); |
16a389dc | 2705 | if (!drv || (!drv->bdrv_aio_ioctl && !drv->bdrv_co_ioctl)) { |
5c5ae76a FZ |
2706 | co.ret = -ENOTSUP; |
2707 | goto out; | |
2708 | } | |
2709 | ||
16a389dc KW |
2710 | if (drv->bdrv_co_ioctl) { |
2711 | co.ret = drv->bdrv_co_ioctl(bs, req, buf); | |
2712 | } else { | |
2713 | acb = drv->bdrv_aio_ioctl(bs, req, buf, bdrv_co_io_em_complete, &co); | |
2714 | if (!acb) { | |
2715 | co.ret = -ENOTSUP; | |
2716 | goto out; | |
2717 | } | |
2718 | qemu_coroutine_yield(); | |
5c5ae76a | 2719 | } |
5c5ae76a | 2720 | out: |
99723548 | 2721 | bdrv_dec_in_flight(bs); |
5c5ae76a FZ |
2722 | return co.ret; |
2723 | } | |
2724 | ||
61007b31 SH |
2725 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
2726 | { | |
2727 | return qemu_memalign(bdrv_opt_mem_align(bs), size); | |
2728 | } | |
2729 | ||
2730 | void *qemu_blockalign0(BlockDriverState *bs, size_t size) | |
2731 | { | |
2732 | return memset(qemu_blockalign(bs, size), 0, size); | |
2733 | } | |
2734 | ||
2735 | void *qemu_try_blockalign(BlockDriverState *bs, size_t size) | |
2736 | { | |
2737 | size_t align = bdrv_opt_mem_align(bs); | |
2738 | ||
2739 | /* Ensure that NULL is never returned on success */ | |
2740 | assert(align > 0); | |
2741 | if (size == 0) { | |
2742 | size = align; | |
2743 | } | |
2744 | ||
2745 | return qemu_try_memalign(align, size); | |
2746 | } | |
2747 | ||
2748 | void *qemu_try_blockalign0(BlockDriverState *bs, size_t size) | |
2749 | { | |
2750 | void *mem = qemu_try_blockalign(bs, size); | |
2751 | ||
2752 | if (mem) { | |
2753 | memset(mem, 0, size); | |
2754 | } | |
2755 | ||
2756 | return mem; | |
2757 | } | |
2758 | ||
2759 | /* | |
2760 | * Check if all memory in this vector is sector aligned. | |
2761 | */ | |
2762 | bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) | |
2763 | { | |
2764 | int i; | |
4196d2f0 | 2765 | size_t alignment = bdrv_min_mem_align(bs); |
61007b31 SH |
2766 | |
2767 | for (i = 0; i < qiov->niov; i++) { | |
2768 | if ((uintptr_t) qiov->iov[i].iov_base % alignment) { | |
2769 | return false; | |
2770 | } | |
2771 | if (qiov->iov[i].iov_len % alignment) { | |
2772 | return false; | |
2773 | } | |
2774 | } | |
2775 | ||
2776 | return true; | |
2777 | } | |
2778 | ||
2779 | void bdrv_add_before_write_notifier(BlockDriverState *bs, | |
2780 | NotifierWithReturn *notifier) | |
2781 | { | |
2782 | notifier_with_return_list_add(&bs->before_write_notifiers, notifier); | |
2783 | } | |
2784 | ||
2785 | void bdrv_io_plug(BlockDriverState *bs) | |
2786 | { | |
6b98bd64 PB |
2787 | BdrvChild *child; |
2788 | ||
2789 | QLIST_FOREACH(child, &bs->children, next) { | |
2790 | bdrv_io_plug(child->bs); | |
2791 | } | |
2792 | ||
850d54a2 | 2793 | if (atomic_fetch_inc(&bs->io_plugged) == 0) { |
6b98bd64 PB |
2794 | BlockDriver *drv = bs->drv; |
2795 | if (drv && drv->bdrv_io_plug) { | |
2796 | drv->bdrv_io_plug(bs); | |
2797 | } | |
61007b31 SH |
2798 | } |
2799 | } | |
2800 | ||
2801 | void bdrv_io_unplug(BlockDriverState *bs) | |
2802 | { | |
6b98bd64 PB |
2803 | BdrvChild *child; |
2804 | ||
2805 | assert(bs->io_plugged); | |
850d54a2 | 2806 | if (atomic_fetch_dec(&bs->io_plugged) == 1) { |
6b98bd64 PB |
2807 | BlockDriver *drv = bs->drv; |
2808 | if (drv && drv->bdrv_io_unplug) { | |
2809 | drv->bdrv_io_unplug(bs); | |
2810 | } | |
2811 | } | |
2812 | ||
2813 | QLIST_FOREACH(child, &bs->children, next) { | |
2814 | bdrv_io_unplug(child->bs); | |
61007b31 SH |
2815 | } |
2816 | } | |
23d0ba93 FZ |
2817 | |
2818 | void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size) | |
2819 | { | |
2820 | BdrvChild *child; | |
2821 | ||
2822 | if (bs->drv && bs->drv->bdrv_register_buf) { | |
2823 | bs->drv->bdrv_register_buf(bs, host, size); | |
2824 | } | |
2825 | QLIST_FOREACH(child, &bs->children, next) { | |
2826 | bdrv_register_buf(child->bs, host, size); | |
2827 | } | |
2828 | } | |
2829 | ||
2830 | void bdrv_unregister_buf(BlockDriverState *bs, void *host) | |
2831 | { | |
2832 | BdrvChild *child; | |
2833 | ||
2834 | if (bs->drv && bs->drv->bdrv_unregister_buf) { | |
2835 | bs->drv->bdrv_unregister_buf(bs, host); | |
2836 | } | |
2837 | QLIST_FOREACH(child, &bs->children, next) { | |
2838 | bdrv_unregister_buf(child->bs, host); | |
2839 | } | |
2840 | } | |
fcc67678 FZ |
2841 | |
2842 | static int coroutine_fn bdrv_co_copy_range_internal(BdrvChild *src, | |
2843 | uint64_t src_offset, | |
2844 | BdrvChild *dst, | |
2845 | uint64_t dst_offset, | |
2846 | uint64_t bytes, | |
2847 | BdrvRequestFlags flags, | |
2848 | bool recurse_src) | |
2849 | { | |
2850 | int ret; | |
2851 | ||
2852 | if (!src || !dst || !src->bs || !dst->bs) { | |
2853 | return -ENOMEDIUM; | |
2854 | } | |
2855 | ret = bdrv_check_byte_request(src->bs, src_offset, bytes); | |
2856 | if (ret) { | |
2857 | return ret; | |
2858 | } | |
2859 | ||
2860 | ret = bdrv_check_byte_request(dst->bs, dst_offset, bytes); | |
2861 | if (ret) { | |
2862 | return ret; | |
2863 | } | |
2864 | if (flags & BDRV_REQ_ZERO_WRITE) { | |
2865 | return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, flags); | |
2866 | } | |
2867 | ||
2868 | if (!src->bs->drv->bdrv_co_copy_range_from | |
2869 | || !dst->bs->drv->bdrv_co_copy_range_to | |
2870 | || src->bs->encrypted || dst->bs->encrypted) { | |
2871 | return -ENOTSUP; | |
2872 | } | |
2873 | if (recurse_src) { | |
2874 | return src->bs->drv->bdrv_co_copy_range_from(src->bs, | |
2875 | src, src_offset, | |
2876 | dst, dst_offset, | |
2877 | bytes, flags); | |
2878 | } else { | |
2879 | return dst->bs->drv->bdrv_co_copy_range_to(dst->bs, | |
2880 | src, src_offset, | |
2881 | dst, dst_offset, | |
2882 | bytes, flags); | |
2883 | } | |
2884 | } | |
2885 | ||
2886 | /* Copy range from @src to @dst. | |
2887 | * | |
2888 | * See the comment of bdrv_co_copy_range for the parameter and return value | |
2889 | * semantics. */ | |
2890 | int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, uint64_t src_offset, | |
2891 | BdrvChild *dst, uint64_t dst_offset, | |
2892 | uint64_t bytes, BdrvRequestFlags flags) | |
2893 | { | |
2894 | return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset, | |
2895 | bytes, flags, true); | |
2896 | } | |
2897 | ||
2898 | /* Copy range from @src to @dst. | |
2899 | * | |
2900 | * See the comment of bdrv_co_copy_range for the parameter and return value | |
2901 | * semantics. */ | |
2902 | int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, uint64_t src_offset, | |
2903 | BdrvChild *dst, uint64_t dst_offset, | |
2904 | uint64_t bytes, BdrvRequestFlags flags) | |
2905 | { | |
2906 | return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset, | |
2907 | bytes, flags, false); | |
2908 | } | |
2909 | ||
2910 | int coroutine_fn bdrv_co_copy_range(BdrvChild *src, uint64_t src_offset, | |
2911 | BdrvChild *dst, uint64_t dst_offset, | |
2912 | uint64_t bytes, BdrvRequestFlags flags) | |
2913 | { | |
2914 | BdrvTrackedRequest src_req, dst_req; | |
2915 | BlockDriverState *src_bs = src->bs; | |
2916 | BlockDriverState *dst_bs = dst->bs; | |
2917 | int ret; | |
2918 | ||
2919 | bdrv_inc_in_flight(src_bs); | |
2920 | bdrv_inc_in_flight(dst_bs); | |
2921 | tracked_request_begin(&src_req, src_bs, src_offset, | |
2922 | bytes, BDRV_TRACKED_READ); | |
2923 | tracked_request_begin(&dst_req, dst_bs, dst_offset, | |
2924 | bytes, BDRV_TRACKED_WRITE); | |
2925 | ||
2926 | wait_serialising_requests(&src_req); | |
2927 | wait_serialising_requests(&dst_req); | |
2928 | ret = bdrv_co_copy_range_from(src, src_offset, | |
2929 | dst, dst_offset, | |
2930 | bytes, flags); | |
2931 | ||
2932 | tracked_request_end(&src_req); | |
2933 | tracked_request_end(&dst_req); | |
2934 | bdrv_dec_in_flight(src_bs); | |
2935 | bdrv_dec_in_flight(dst_bs); | |
2936 | return ret; | |
2937 | } |