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