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