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