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