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