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