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