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