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