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