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