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