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