]> git.proxmox.com Git - mirror_qemu.git/blob - block/block-copy.c
block: Mark bdrv_skip_filters() and callers GRAPH_RDLOCK
[mirror_qemu.git] / block / block-copy.c
1 /*
2 * block_copy API
3 *
4 * Copyright (C) 2013 Proxmox Server Solutions
5 * Copyright (c) 2019 Virtuozzo International GmbH.
6 *
7 * Authors:
8 * Dietmar Maurer (dietmar@proxmox.com)
9 * Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 */
14
15 #include "qemu/osdep.h"
16
17 #include "trace.h"
18 #include "qapi/error.h"
19 #include "block/block-copy.h"
20 #include "block/block_int-io.h"
21 #include "block/dirty-bitmap.h"
22 #include "block/reqlist.h"
23 #include "sysemu/block-backend.h"
24 #include "qemu/units.h"
25 #include "qemu/co-shared-resource.h"
26 #include "qemu/coroutine.h"
27 #include "qemu/ratelimit.h"
28 #include "block/aio_task.h"
29 #include "qemu/error-report.h"
30 #include "qemu/memalign.h"
31
32 #define BLOCK_COPY_MAX_COPY_RANGE (16 * MiB)
33 #define BLOCK_COPY_MAX_BUFFER (1 * MiB)
34 #define BLOCK_COPY_MAX_MEM (128 * MiB)
35 #define BLOCK_COPY_MAX_WORKERS 64
36 #define BLOCK_COPY_SLICE_TIME 100000000ULL /* ns */
37 #define BLOCK_COPY_CLUSTER_SIZE_DEFAULT (1 << 16)
38
39 typedef enum {
40 COPY_READ_WRITE_CLUSTER,
41 COPY_READ_WRITE,
42 COPY_WRITE_ZEROES,
43 COPY_RANGE_SMALL,
44 COPY_RANGE_FULL
45 } BlockCopyMethod;
46
47 static coroutine_fn int block_copy_task_entry(AioTask *task);
48
49 typedef struct BlockCopyCallState {
50 /* Fields initialized in block_copy_async() and never changed. */
51 BlockCopyState *s;
52 int64_t offset;
53 int64_t bytes;
54 int max_workers;
55 int64_t max_chunk;
56 bool ignore_ratelimit;
57 BlockCopyAsyncCallbackFunc cb;
58 void *cb_opaque;
59 /* Coroutine where async block-copy is running */
60 Coroutine *co;
61
62 /* Fields whose state changes throughout the execution */
63 bool finished; /* atomic */
64 QemuCoSleep sleep; /* TODO: protect API with a lock */
65 bool cancelled; /* atomic */
66 /* To reference all call states from BlockCopyState */
67 QLIST_ENTRY(BlockCopyCallState) list;
68
69 /*
70 * Fields that report information about return values and errors.
71 * Protected by lock in BlockCopyState.
72 */
73 bool error_is_read;
74 /*
75 * @ret is set concurrently by tasks under mutex. Only set once by first
76 * failed task (and untouched if no task failed).
77 * After finishing (call_state->finished is true), it is not modified
78 * anymore and may be safely read without mutex.
79 */
80 int ret;
81 } BlockCopyCallState;
82
83 typedef struct BlockCopyTask {
84 AioTask task;
85
86 /*
87 * Fields initialized in block_copy_task_create()
88 * and never changed.
89 */
90 BlockCopyState *s;
91 BlockCopyCallState *call_state;
92 /*
93 * @method can also be set again in the while loop of
94 * block_copy_dirty_clusters(), but it is never accessed concurrently
95 * because the only other function that reads it is
96 * block_copy_task_entry() and it is invoked afterwards in the same
97 * iteration.
98 */
99 BlockCopyMethod method;
100
101 /*
102 * Generally, req is protected by lock in BlockCopyState, Still req.offset
103 * is only set on task creation, so may be read concurrently after creation.
104 * req.bytes is changed at most once, and need only protecting the case of
105 * parallel read while updating @bytes value in block_copy_task_shrink().
106 */
107 BlockReq req;
108 } BlockCopyTask;
109
110 static int64_t task_end(BlockCopyTask *task)
111 {
112 return task->req.offset + task->req.bytes;
113 }
114
115 typedef struct BlockCopyState {
116 /*
117 * BdrvChild objects are not owned or managed by block-copy. They are
118 * provided by block-copy user and user is responsible for appropriate
119 * permissions on these children.
120 */
121 BdrvChild *source;
122 BdrvChild *target;
123
124 /*
125 * Fields initialized in block_copy_state_new()
126 * and never changed.
127 */
128 int64_t cluster_size;
129 int64_t max_transfer;
130 uint64_t len;
131 BdrvRequestFlags write_flags;
132
133 /*
134 * Fields whose state changes throughout the execution
135 * Protected by lock.
136 */
137 CoMutex lock;
138 int64_t in_flight_bytes;
139 BlockCopyMethod method;
140 BlockReqList reqs;
141 QLIST_HEAD(, BlockCopyCallState) calls;
142 /*
143 * skip_unallocated:
144 *
145 * Used by sync=top jobs, which first scan the source node for unallocated
146 * areas and clear them in the copy_bitmap. During this process, the bitmap
147 * is thus not fully initialized: It may still have bits set for areas that
148 * are unallocated and should actually not be copied.
149 *
150 * This is indicated by skip_unallocated.
151 *
152 * In this case, block_copy() will query the source’s allocation status,
153 * skip unallocated regions, clear them in the copy_bitmap, and invoke
154 * block_copy_reset_unallocated() every time it does.
155 */
156 bool skip_unallocated; /* atomic */
157 /* State fields that use a thread-safe API */
158 BdrvDirtyBitmap *copy_bitmap;
159 ProgressMeter *progress;
160 SharedResource *mem;
161 RateLimit rate_limit;
162 } BlockCopyState;
163
164 /* Called with lock held */
165 static int64_t block_copy_chunk_size(BlockCopyState *s)
166 {
167 switch (s->method) {
168 case COPY_READ_WRITE_CLUSTER:
169 return s->cluster_size;
170 case COPY_READ_WRITE:
171 case COPY_RANGE_SMALL:
172 return MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_BUFFER),
173 s->max_transfer);
174 case COPY_RANGE_FULL:
175 return MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_COPY_RANGE),
176 s->max_transfer);
177 default:
178 /* Cannot have COPY_WRITE_ZEROES here. */
179 abort();
180 }
181 }
182
183 /*
184 * Search for the first dirty area in offset/bytes range and create task at
185 * the beginning of it.
186 */
187 static coroutine_fn BlockCopyTask *
188 block_copy_task_create(BlockCopyState *s, BlockCopyCallState *call_state,
189 int64_t offset, int64_t bytes)
190 {
191 BlockCopyTask *task;
192 int64_t max_chunk;
193
194 QEMU_LOCK_GUARD(&s->lock);
195 max_chunk = MIN_NON_ZERO(block_copy_chunk_size(s), call_state->max_chunk);
196 if (!bdrv_dirty_bitmap_next_dirty_area(s->copy_bitmap,
197 offset, offset + bytes,
198 max_chunk, &offset, &bytes))
199 {
200 return NULL;
201 }
202
203 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
204 bytes = QEMU_ALIGN_UP(bytes, s->cluster_size);
205
206 /* region is dirty, so no existent tasks possible in it */
207 assert(!reqlist_find_conflict(&s->reqs, offset, bytes));
208
209 bdrv_reset_dirty_bitmap(s->copy_bitmap, offset, bytes);
210 s->in_flight_bytes += bytes;
211
212 task = g_new(BlockCopyTask, 1);
213 *task = (BlockCopyTask) {
214 .task.func = block_copy_task_entry,
215 .s = s,
216 .call_state = call_state,
217 .method = s->method,
218 };
219 reqlist_init_req(&s->reqs, &task->req, offset, bytes);
220
221 return task;
222 }
223
224 /*
225 * block_copy_task_shrink
226 *
227 * Drop the tail of the task to be handled later. Set dirty bits back and
228 * wake up all tasks waiting for us (may be some of them are not intersecting
229 * with shrunk task)
230 */
231 static void coroutine_fn block_copy_task_shrink(BlockCopyTask *task,
232 int64_t new_bytes)
233 {
234 QEMU_LOCK_GUARD(&task->s->lock);
235 if (new_bytes == task->req.bytes) {
236 return;
237 }
238
239 assert(new_bytes > 0 && new_bytes < task->req.bytes);
240
241 task->s->in_flight_bytes -= task->req.bytes - new_bytes;
242 bdrv_set_dirty_bitmap(task->s->copy_bitmap,
243 task->req.offset + new_bytes,
244 task->req.bytes - new_bytes);
245
246 reqlist_shrink_req(&task->req, new_bytes);
247 }
248
249 static void coroutine_fn block_copy_task_end(BlockCopyTask *task, int ret)
250 {
251 QEMU_LOCK_GUARD(&task->s->lock);
252 task->s->in_flight_bytes -= task->req.bytes;
253 if (ret < 0) {
254 bdrv_set_dirty_bitmap(task->s->copy_bitmap, task->req.offset,
255 task->req.bytes);
256 }
257 if (task->s->progress) {
258 progress_set_remaining(task->s->progress,
259 bdrv_get_dirty_count(task->s->copy_bitmap) +
260 task->s->in_flight_bytes);
261 }
262 reqlist_remove_req(&task->req);
263 }
264
265 void block_copy_state_free(BlockCopyState *s)
266 {
267 if (!s) {
268 return;
269 }
270
271 ratelimit_destroy(&s->rate_limit);
272 bdrv_release_dirty_bitmap(s->copy_bitmap);
273 shres_destroy(s->mem);
274 g_free(s);
275 }
276
277 static uint32_t block_copy_max_transfer(BdrvChild *source, BdrvChild *target)
278 {
279 return MIN_NON_ZERO(INT_MAX,
280 MIN_NON_ZERO(source->bs->bl.max_transfer,
281 target->bs->bl.max_transfer));
282 }
283
284 void block_copy_set_copy_opts(BlockCopyState *s, bool use_copy_range,
285 bool compress)
286 {
287 /* Keep BDRV_REQ_SERIALISING set (or not set) in block_copy_state_new() */
288 s->write_flags = (s->write_flags & BDRV_REQ_SERIALISING) |
289 (compress ? BDRV_REQ_WRITE_COMPRESSED : 0);
290
291 if (s->max_transfer < s->cluster_size) {
292 /*
293 * copy_range does not respect max_transfer. We don't want to bother
294 * with requests smaller than block-copy cluster size, so fallback to
295 * buffered copying (read and write respect max_transfer on their
296 * behalf).
297 */
298 s->method = COPY_READ_WRITE_CLUSTER;
299 } else if (compress) {
300 /* Compression supports only cluster-size writes and no copy-range. */
301 s->method = COPY_READ_WRITE_CLUSTER;
302 } else {
303 /*
304 * If copy range enabled, start with COPY_RANGE_SMALL, until first
305 * successful copy_range (look at block_copy_do_copy).
306 */
307 s->method = use_copy_range ? COPY_RANGE_SMALL : COPY_READ_WRITE;
308 }
309 }
310
311 static int64_t block_copy_calculate_cluster_size(BlockDriverState *target,
312 Error **errp)
313 {
314 int ret;
315 BlockDriverInfo bdi;
316 bool target_does_cow;
317
318 GLOBAL_STATE_CODE();
319 GRAPH_RDLOCK_GUARD_MAINLOOP();
320
321 target_does_cow = bdrv_backing_chain_next(target);
322
323 /*
324 * If there is no backing file on the target, we cannot rely on COW if our
325 * backup cluster size is smaller than the target cluster size. Even for
326 * targets with a backing file, try to avoid COW if possible.
327 */
328 ret = bdrv_get_info(target, &bdi);
329 if (ret == -ENOTSUP && !target_does_cow) {
330 /* Cluster size is not defined */
331 warn_report("The target block device doesn't provide "
332 "information about the block size and it doesn't have a "
333 "backing file. The default block size of %u bytes is "
334 "used. If the actual block size of the target exceeds "
335 "this default, the backup may be unusable",
336 BLOCK_COPY_CLUSTER_SIZE_DEFAULT);
337 return BLOCK_COPY_CLUSTER_SIZE_DEFAULT;
338 } else if (ret < 0 && !target_does_cow) {
339 error_setg_errno(errp, -ret,
340 "Couldn't determine the cluster size of the target image, "
341 "which has no backing file");
342 error_append_hint(errp,
343 "Aborting, since this may create an unusable destination image\n");
344 return ret;
345 } else if (ret < 0 && target_does_cow) {
346 /* Not fatal; just trudge on ahead. */
347 return BLOCK_COPY_CLUSTER_SIZE_DEFAULT;
348 }
349
350 return MAX(BLOCK_COPY_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
351 }
352
353 BlockCopyState *block_copy_state_new(BdrvChild *source, BdrvChild *target,
354 const BdrvDirtyBitmap *bitmap,
355 Error **errp)
356 {
357 ERRP_GUARD();
358 BlockCopyState *s;
359 int64_t cluster_size;
360 BdrvDirtyBitmap *copy_bitmap;
361 bool is_fleecing;
362
363 GLOBAL_STATE_CODE();
364
365 cluster_size = block_copy_calculate_cluster_size(target->bs, errp);
366 if (cluster_size < 0) {
367 return NULL;
368 }
369
370 copy_bitmap = bdrv_create_dirty_bitmap(source->bs, cluster_size, NULL,
371 errp);
372 if (!copy_bitmap) {
373 return NULL;
374 }
375 bdrv_disable_dirty_bitmap(copy_bitmap);
376 if (bitmap) {
377 if (!bdrv_merge_dirty_bitmap(copy_bitmap, bitmap, NULL, errp)) {
378 error_prepend(errp, "Failed to merge bitmap '%s' to internal "
379 "copy-bitmap: ", bdrv_dirty_bitmap_name(bitmap));
380 bdrv_release_dirty_bitmap(copy_bitmap);
381 return NULL;
382 }
383 } else {
384 bdrv_set_dirty_bitmap(copy_bitmap, 0,
385 bdrv_dirty_bitmap_size(copy_bitmap));
386 }
387
388 /*
389 * If source is in backing chain of target assume that target is going to be
390 * used for "image fleecing", i.e. it should represent a kind of snapshot of
391 * source at backup-start point in time. And target is going to be read by
392 * somebody (for example, used as NBD export) during backup job.
393 *
394 * In this case, we need to add BDRV_REQ_SERIALISING write flag to avoid
395 * intersection of backup writes and third party reads from target,
396 * otherwise reading from target we may occasionally read already updated by
397 * guest data.
398 *
399 * For more information see commit f8d59dfb40bb and test
400 * tests/qemu-iotests/222
401 */
402 is_fleecing = bdrv_chain_contains(target->bs, source->bs);
403
404 s = g_new(BlockCopyState, 1);
405 *s = (BlockCopyState) {
406 .source = source,
407 .target = target,
408 .copy_bitmap = copy_bitmap,
409 .cluster_size = cluster_size,
410 .len = bdrv_dirty_bitmap_size(copy_bitmap),
411 .write_flags = (is_fleecing ? BDRV_REQ_SERIALISING : 0),
412 .mem = shres_create(BLOCK_COPY_MAX_MEM),
413 .max_transfer = QEMU_ALIGN_DOWN(
414 block_copy_max_transfer(source, target),
415 cluster_size),
416 };
417
418 block_copy_set_copy_opts(s, false, false);
419
420 ratelimit_init(&s->rate_limit);
421 qemu_co_mutex_init(&s->lock);
422 QLIST_INIT(&s->reqs);
423 QLIST_INIT(&s->calls);
424
425 return s;
426 }
427
428 /* Only set before running the job, no need for locking. */
429 void block_copy_set_progress_meter(BlockCopyState *s, ProgressMeter *pm)
430 {
431 s->progress = pm;
432 }
433
434 /*
435 * Takes ownership of @task
436 *
437 * If pool is NULL directly run the task, otherwise schedule it into the pool.
438 *
439 * Returns: task.func return code if pool is NULL
440 * otherwise -ECANCELED if pool status is bad
441 * otherwise 0 (successfully scheduled)
442 */
443 static coroutine_fn int block_copy_task_run(AioTaskPool *pool,
444 BlockCopyTask *task)
445 {
446 if (!pool) {
447 int ret = task->task.func(&task->task);
448
449 g_free(task);
450 return ret;
451 }
452
453 aio_task_pool_wait_slot(pool);
454 if (aio_task_pool_status(pool) < 0) {
455 co_put_to_shres(task->s->mem, task->req.bytes);
456 block_copy_task_end(task, -ECANCELED);
457 g_free(task);
458 return -ECANCELED;
459 }
460
461 aio_task_pool_start_task(pool, &task->task);
462
463 return 0;
464 }
465
466 /*
467 * block_copy_do_copy
468 *
469 * Do copy of cluster-aligned chunk. Requested region is allowed to exceed
470 * s->len only to cover last cluster when s->len is not aligned to clusters.
471 *
472 * No sync here: neither bitmap nor intersecting requests handling, only copy.
473 *
474 * @method is an in-out argument, so that copy_range can be either extended to
475 * a full-size buffer or disabled if the copy_range attempt fails. The output
476 * value of @method should be used for subsequent tasks.
477 * Returns 0 on success.
478 */
479 static int coroutine_fn GRAPH_RDLOCK
480 block_copy_do_copy(BlockCopyState *s, int64_t offset, int64_t bytes,
481 BlockCopyMethod *method, bool *error_is_read)
482 {
483 int ret;
484 int64_t nbytes = MIN(offset + bytes, s->len) - offset;
485 void *bounce_buffer = NULL;
486
487 assert(offset >= 0 && bytes > 0 && INT64_MAX - offset >= bytes);
488 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
489 assert(QEMU_IS_ALIGNED(bytes, s->cluster_size));
490 assert(offset < s->len);
491 assert(offset + bytes <= s->len ||
492 offset + bytes == QEMU_ALIGN_UP(s->len, s->cluster_size));
493 assert(nbytes < INT_MAX);
494
495 switch (*method) {
496 case COPY_WRITE_ZEROES:
497 ret = bdrv_co_pwrite_zeroes(s->target, offset, nbytes, s->write_flags &
498 ~BDRV_REQ_WRITE_COMPRESSED);
499 if (ret < 0) {
500 trace_block_copy_write_zeroes_fail(s, offset, ret);
501 *error_is_read = false;
502 }
503 return ret;
504
505 case COPY_RANGE_SMALL:
506 case COPY_RANGE_FULL:
507 ret = bdrv_co_copy_range(s->source, offset, s->target, offset, nbytes,
508 0, s->write_flags);
509 if (ret >= 0) {
510 /* Successful copy-range, increase chunk size. */
511 *method = COPY_RANGE_FULL;
512 return 0;
513 }
514
515 trace_block_copy_copy_range_fail(s, offset, ret);
516 *method = COPY_READ_WRITE;
517 /* Fall through to read+write with allocated buffer */
518
519 case COPY_READ_WRITE_CLUSTER:
520 case COPY_READ_WRITE:
521 /*
522 * In case of failed copy_range request above, we may proceed with
523 * buffered request larger than BLOCK_COPY_MAX_BUFFER.
524 * Still, further requests will be properly limited, so don't care too
525 * much. Moreover the most likely case (copy_range is unsupported for
526 * the configuration, so the very first copy_range request fails)
527 * is handled by setting large copy_size only after first successful
528 * copy_range.
529 */
530
531 bounce_buffer = qemu_blockalign(s->source->bs, nbytes);
532
533 ret = bdrv_co_pread(s->source, offset, nbytes, bounce_buffer, 0);
534 if (ret < 0) {
535 trace_block_copy_read_fail(s, offset, ret);
536 *error_is_read = true;
537 goto out;
538 }
539
540 ret = bdrv_co_pwrite(s->target, offset, nbytes, bounce_buffer,
541 s->write_flags);
542 if (ret < 0) {
543 trace_block_copy_write_fail(s, offset, ret);
544 *error_is_read = false;
545 goto out;
546 }
547
548 out:
549 qemu_vfree(bounce_buffer);
550 break;
551
552 default:
553 abort();
554 }
555
556 return ret;
557 }
558
559 static coroutine_fn int block_copy_task_entry(AioTask *task)
560 {
561 BlockCopyTask *t = container_of(task, BlockCopyTask, task);
562 BlockCopyState *s = t->s;
563 bool error_is_read = false;
564 BlockCopyMethod method = t->method;
565 int ret;
566
567 WITH_GRAPH_RDLOCK_GUARD() {
568 ret = block_copy_do_copy(s, t->req.offset, t->req.bytes, &method,
569 &error_is_read);
570 }
571
572 WITH_QEMU_LOCK_GUARD(&s->lock) {
573 if (s->method == t->method) {
574 s->method = method;
575 }
576
577 if (ret < 0) {
578 if (!t->call_state->ret) {
579 t->call_state->ret = ret;
580 t->call_state->error_is_read = error_is_read;
581 }
582 } else if (s->progress) {
583 progress_work_done(s->progress, t->req.bytes);
584 }
585 }
586 co_put_to_shres(s->mem, t->req.bytes);
587 block_copy_task_end(t, ret);
588
589 return ret;
590 }
591
592 static coroutine_fn GRAPH_RDLOCK
593 int block_copy_block_status(BlockCopyState *s, int64_t offset, int64_t bytes,
594 int64_t *pnum)
595 {
596 int64_t num;
597 BlockDriverState *base;
598 int ret;
599
600 if (qatomic_read(&s->skip_unallocated)) {
601 base = bdrv_backing_chain_next(s->source->bs);
602 } else {
603 base = NULL;
604 }
605
606 ret = bdrv_co_block_status_above(s->source->bs, base, offset, bytes, &num,
607 NULL, NULL);
608 if (ret < 0 || num < s->cluster_size) {
609 /*
610 * On error or if failed to obtain large enough chunk just fallback to
611 * copy one cluster.
612 */
613 num = s->cluster_size;
614 ret = BDRV_BLOCK_ALLOCATED | BDRV_BLOCK_DATA;
615 } else if (offset + num == s->len) {
616 num = QEMU_ALIGN_UP(num, s->cluster_size);
617 } else {
618 num = QEMU_ALIGN_DOWN(num, s->cluster_size);
619 }
620
621 *pnum = num;
622 return ret;
623 }
624
625 /*
626 * Check if the cluster starting at offset is allocated or not.
627 * return via pnum the number of contiguous clusters sharing this allocation.
628 */
629 static int coroutine_fn GRAPH_RDLOCK
630 block_copy_is_cluster_allocated(BlockCopyState *s, int64_t offset,
631 int64_t *pnum)
632 {
633 BlockDriverState *bs = s->source->bs;
634 int64_t count, total_count = 0;
635 int64_t bytes = s->len - offset;
636 int ret;
637
638 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
639
640 while (true) {
641 /* protected in backup_run() */
642 ret = bdrv_co_is_allocated(bs, offset, bytes, &count);
643 if (ret < 0) {
644 return ret;
645 }
646
647 total_count += count;
648
649 if (ret || count == 0) {
650 /*
651 * ret: partial segment(s) are considered allocated.
652 * otherwise: unallocated tail is treated as an entire segment.
653 */
654 *pnum = DIV_ROUND_UP(total_count, s->cluster_size);
655 return ret;
656 }
657
658 /* Unallocated segment(s) with uncertain following segment(s) */
659 if (total_count >= s->cluster_size) {
660 *pnum = total_count / s->cluster_size;
661 return 0;
662 }
663
664 offset += count;
665 bytes -= count;
666 }
667 }
668
669 void block_copy_reset(BlockCopyState *s, int64_t offset, int64_t bytes)
670 {
671 QEMU_LOCK_GUARD(&s->lock);
672
673 bdrv_reset_dirty_bitmap(s->copy_bitmap, offset, bytes);
674 if (s->progress) {
675 progress_set_remaining(s->progress,
676 bdrv_get_dirty_count(s->copy_bitmap) +
677 s->in_flight_bytes);
678 }
679 }
680
681 /*
682 * Reset bits in copy_bitmap starting at offset if they represent unallocated
683 * data in the image. May reset subsequent contiguous bits.
684 * @return 0 when the cluster at @offset was unallocated,
685 * 1 otherwise, and -ret on error.
686 */
687 int64_t coroutine_fn block_copy_reset_unallocated(BlockCopyState *s,
688 int64_t offset,
689 int64_t *count)
690 {
691 int ret;
692 int64_t clusters, bytes;
693
694 ret = block_copy_is_cluster_allocated(s, offset, &clusters);
695 if (ret < 0) {
696 return ret;
697 }
698
699 bytes = clusters * s->cluster_size;
700
701 if (!ret) {
702 block_copy_reset(s, offset, bytes);
703 }
704
705 *count = bytes;
706 return ret;
707 }
708
709 /*
710 * block_copy_dirty_clusters
711 *
712 * Copy dirty clusters in @offset/@bytes range.
713 * Returns 1 if dirty clusters found and successfully copied, 0 if no dirty
714 * clusters found and -errno on failure.
715 */
716 static int coroutine_fn GRAPH_RDLOCK
717 block_copy_dirty_clusters(BlockCopyCallState *call_state)
718 {
719 BlockCopyState *s = call_state->s;
720 int64_t offset = call_state->offset;
721 int64_t bytes = call_state->bytes;
722
723 int ret = 0;
724 bool found_dirty = false;
725 int64_t end = offset + bytes;
726 AioTaskPool *aio = NULL;
727
728 /*
729 * block_copy() user is responsible for keeping source and target in same
730 * aio context
731 */
732 assert(bdrv_get_aio_context(s->source->bs) ==
733 bdrv_get_aio_context(s->target->bs));
734
735 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
736 assert(QEMU_IS_ALIGNED(bytes, s->cluster_size));
737
738 while (bytes && aio_task_pool_status(aio) == 0 &&
739 !qatomic_read(&call_state->cancelled)) {
740 BlockCopyTask *task;
741 int64_t status_bytes;
742
743 task = block_copy_task_create(s, call_state, offset, bytes);
744 if (!task) {
745 /* No more dirty bits in the bitmap */
746 trace_block_copy_skip_range(s, offset, bytes);
747 break;
748 }
749 if (task->req.offset > offset) {
750 trace_block_copy_skip_range(s, offset, task->req.offset - offset);
751 }
752
753 found_dirty = true;
754
755 ret = block_copy_block_status(s, task->req.offset, task->req.bytes,
756 &status_bytes);
757 assert(ret >= 0); /* never fail */
758 if (status_bytes < task->req.bytes) {
759 block_copy_task_shrink(task, status_bytes);
760 }
761 if (qatomic_read(&s->skip_unallocated) &&
762 !(ret & BDRV_BLOCK_ALLOCATED)) {
763 block_copy_task_end(task, 0);
764 trace_block_copy_skip_range(s, task->req.offset, task->req.bytes);
765 offset = task_end(task);
766 bytes = end - offset;
767 g_free(task);
768 continue;
769 }
770 if (ret & BDRV_BLOCK_ZERO) {
771 task->method = COPY_WRITE_ZEROES;
772 }
773
774 if (!call_state->ignore_ratelimit) {
775 uint64_t ns = ratelimit_calculate_delay(&s->rate_limit, 0);
776 if (ns > 0) {
777 block_copy_task_end(task, -EAGAIN);
778 g_free(task);
779 qemu_co_sleep_ns_wakeable(&call_state->sleep,
780 QEMU_CLOCK_REALTIME, ns);
781 continue;
782 }
783 }
784
785 ratelimit_calculate_delay(&s->rate_limit, task->req.bytes);
786
787 trace_block_copy_process(s, task->req.offset);
788
789 co_get_from_shres(s->mem, task->req.bytes);
790
791 offset = task_end(task);
792 bytes = end - offset;
793
794 if (!aio && bytes) {
795 aio = aio_task_pool_new(call_state->max_workers);
796 }
797
798 ret = block_copy_task_run(aio, task);
799 if (ret < 0) {
800 goto out;
801 }
802 }
803
804 out:
805 if (aio) {
806 aio_task_pool_wait_all(aio);
807
808 /*
809 * We are not really interested in -ECANCELED returned from
810 * block_copy_task_run. If it fails, it means some task already failed
811 * for real reason, let's return first failure.
812 * Still, assert that we don't rewrite failure by success.
813 *
814 * Note: ret may be positive here because of block-status result.
815 */
816 assert(ret >= 0 || aio_task_pool_status(aio) < 0);
817 ret = aio_task_pool_status(aio);
818
819 aio_task_pool_free(aio);
820 }
821
822 return ret < 0 ? ret : found_dirty;
823 }
824
825 void block_copy_kick(BlockCopyCallState *call_state)
826 {
827 qemu_co_sleep_wake(&call_state->sleep);
828 }
829
830 /*
831 * block_copy_common
832 *
833 * Copy requested region, accordingly to dirty bitmap.
834 * Collaborate with parallel block_copy requests: if they succeed it will help
835 * us. If they fail, we will retry not-copied regions. So, if we return error,
836 * it means that some I/O operation failed in context of _this_ block_copy call,
837 * not some parallel operation.
838 */
839 static int coroutine_fn GRAPH_RDLOCK
840 block_copy_common(BlockCopyCallState *call_state)
841 {
842 int ret;
843 BlockCopyState *s = call_state->s;
844
845 qemu_co_mutex_lock(&s->lock);
846 QLIST_INSERT_HEAD(&s->calls, call_state, list);
847 qemu_co_mutex_unlock(&s->lock);
848
849 do {
850 ret = block_copy_dirty_clusters(call_state);
851
852 if (ret == 0 && !qatomic_read(&call_state->cancelled)) {
853 WITH_QEMU_LOCK_GUARD(&s->lock) {
854 /*
855 * Check that there is no task we still need to
856 * wait to complete
857 */
858 ret = reqlist_wait_one(&s->reqs, call_state->offset,
859 call_state->bytes, &s->lock);
860 if (ret == 0) {
861 /*
862 * No pending tasks, but check again the bitmap in this
863 * same critical section, since a task might have failed
864 * between this and the critical section in
865 * block_copy_dirty_clusters().
866 *
867 * reqlist_wait_one return value 0 also means that it
868 * didn't release the lock. So, we are still in the same
869 * critical section, not interrupted by any concurrent
870 * access to state.
871 */
872 ret = bdrv_dirty_bitmap_next_dirty(s->copy_bitmap,
873 call_state->offset,
874 call_state->bytes) >= 0;
875 }
876 }
877 }
878
879 /*
880 * We retry in two cases:
881 * 1. Some progress done
882 * Something was copied, which means that there were yield points
883 * and some new dirty bits may have appeared (due to failed parallel
884 * block-copy requests).
885 * 2. We have waited for some intersecting block-copy request
886 * It may have failed and produced new dirty bits.
887 */
888 } while (ret > 0 && !qatomic_read(&call_state->cancelled));
889
890 qatomic_store_release(&call_state->finished, true);
891
892 if (call_state->cb) {
893 call_state->cb(call_state->cb_opaque);
894 }
895
896 qemu_co_mutex_lock(&s->lock);
897 QLIST_REMOVE(call_state, list);
898 qemu_co_mutex_unlock(&s->lock);
899
900 return ret;
901 }
902
903 static void coroutine_fn block_copy_async_co_entry(void *opaque)
904 {
905 GRAPH_RDLOCK_GUARD();
906 block_copy_common(opaque);
907 }
908
909 int coroutine_fn block_copy(BlockCopyState *s, int64_t start, int64_t bytes,
910 bool ignore_ratelimit, uint64_t timeout_ns,
911 BlockCopyAsyncCallbackFunc cb,
912 void *cb_opaque)
913 {
914 int ret;
915 BlockCopyCallState *call_state = g_new(BlockCopyCallState, 1);
916
917 *call_state = (BlockCopyCallState) {
918 .s = s,
919 .offset = start,
920 .bytes = bytes,
921 .ignore_ratelimit = ignore_ratelimit,
922 .max_workers = BLOCK_COPY_MAX_WORKERS,
923 .cb = cb,
924 .cb_opaque = cb_opaque,
925 };
926
927 ret = qemu_co_timeout(block_copy_async_co_entry, call_state, timeout_ns,
928 g_free);
929 if (ret < 0) {
930 assert(ret == -ETIMEDOUT);
931 block_copy_call_cancel(call_state);
932 /* call_state will be freed by running coroutine. */
933 return ret;
934 }
935
936 ret = call_state->ret;
937 g_free(call_state);
938
939 return ret;
940 }
941
942 BlockCopyCallState *block_copy_async(BlockCopyState *s,
943 int64_t offset, int64_t bytes,
944 int max_workers, int64_t max_chunk,
945 BlockCopyAsyncCallbackFunc cb,
946 void *cb_opaque)
947 {
948 BlockCopyCallState *call_state = g_new(BlockCopyCallState, 1);
949
950 *call_state = (BlockCopyCallState) {
951 .s = s,
952 .offset = offset,
953 .bytes = bytes,
954 .max_workers = max_workers,
955 .max_chunk = max_chunk,
956 .cb = cb,
957 .cb_opaque = cb_opaque,
958
959 .co = qemu_coroutine_create(block_copy_async_co_entry, call_state),
960 };
961
962 qemu_coroutine_enter(call_state->co);
963
964 return call_state;
965 }
966
967 void block_copy_call_free(BlockCopyCallState *call_state)
968 {
969 if (!call_state) {
970 return;
971 }
972
973 assert(qatomic_read(&call_state->finished));
974 g_free(call_state);
975 }
976
977 bool block_copy_call_finished(BlockCopyCallState *call_state)
978 {
979 return qatomic_read(&call_state->finished);
980 }
981
982 bool block_copy_call_succeeded(BlockCopyCallState *call_state)
983 {
984 return qatomic_load_acquire(&call_state->finished) &&
985 !qatomic_read(&call_state->cancelled) &&
986 call_state->ret == 0;
987 }
988
989 bool block_copy_call_failed(BlockCopyCallState *call_state)
990 {
991 return qatomic_load_acquire(&call_state->finished) &&
992 !qatomic_read(&call_state->cancelled) &&
993 call_state->ret < 0;
994 }
995
996 bool block_copy_call_cancelled(BlockCopyCallState *call_state)
997 {
998 return qatomic_read(&call_state->cancelled);
999 }
1000
1001 int block_copy_call_status(BlockCopyCallState *call_state, bool *error_is_read)
1002 {
1003 assert(qatomic_load_acquire(&call_state->finished));
1004 if (error_is_read) {
1005 *error_is_read = call_state->error_is_read;
1006 }
1007 return call_state->ret;
1008 }
1009
1010 /*
1011 * Note that cancelling and finishing are racy.
1012 * User can cancel a block-copy that is already finished.
1013 */
1014 void block_copy_call_cancel(BlockCopyCallState *call_state)
1015 {
1016 qatomic_set(&call_state->cancelled, true);
1017 block_copy_kick(call_state);
1018 }
1019
1020 BdrvDirtyBitmap *block_copy_dirty_bitmap(BlockCopyState *s)
1021 {
1022 return s->copy_bitmap;
1023 }
1024
1025 int64_t block_copy_cluster_size(BlockCopyState *s)
1026 {
1027 return s->cluster_size;
1028 }
1029
1030 void block_copy_set_skip_unallocated(BlockCopyState *s, bool skip)
1031 {
1032 qatomic_set(&s->skip_unallocated, skip);
1033 }
1034
1035 void block_copy_set_speed(BlockCopyState *s, uint64_t speed)
1036 {
1037 ratelimit_set_speed(&s->rate_limit, speed, BLOCK_COPY_SLICE_TIME);
1038
1039 /*
1040 * Note: it's good to kick all call states from here, but it should be done
1041 * only from a coroutine, to not crash if s->calls list changed while
1042 * entering one call. So for now, the only user of this function kicks its
1043 * only one call_state by hand.
1044 */
1045 }