]> git.proxmox.com Git - mirror_qemu.git/blob - block/block-copy.c
block/block-copy: add ratelimit to block-copy
[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 "sysemu/block-backend.h"
21 #include "qemu/units.h"
22 #include "qemu/coroutine.h"
23 #include "block/aio_task.h"
24
25 #define BLOCK_COPY_MAX_COPY_RANGE (16 * MiB)
26 #define BLOCK_COPY_MAX_BUFFER (1 * MiB)
27 #define BLOCK_COPY_MAX_MEM (128 * MiB)
28 #define BLOCK_COPY_MAX_WORKERS 64
29 #define BLOCK_COPY_SLICE_TIME 100000000ULL /* ns */
30
31 static coroutine_fn int block_copy_task_entry(AioTask *task);
32
33 typedef struct BlockCopyCallState {
34 /* IN parameters. Initialized in block_copy_async() and never changed. */
35 BlockCopyState *s;
36 int64_t offset;
37 int64_t bytes;
38 int max_workers;
39 int64_t max_chunk;
40 bool ignore_ratelimit;
41 BlockCopyAsyncCallbackFunc cb;
42 void *cb_opaque;
43
44 /* Coroutine where async block-copy is running */
45 Coroutine *co;
46
47 /* To reference all call states from BlockCopyState */
48 QLIST_ENTRY(BlockCopyCallState) list;
49
50 /* State */
51 int ret;
52 bool finished;
53 QemuCoSleepState *sleep_state;
54
55 /* OUT parameters */
56 bool error_is_read;
57 } BlockCopyCallState;
58
59 typedef struct BlockCopyTask {
60 AioTask task;
61
62 BlockCopyState *s;
63 BlockCopyCallState *call_state;
64 int64_t offset;
65 int64_t bytes;
66 bool zeroes;
67 QLIST_ENTRY(BlockCopyTask) list;
68 CoQueue wait_queue; /* coroutines blocked on this task */
69 } BlockCopyTask;
70
71 static int64_t task_end(BlockCopyTask *task)
72 {
73 return task->offset + task->bytes;
74 }
75
76 typedef struct BlockCopyState {
77 /*
78 * BdrvChild objects are not owned or managed by block-copy. They are
79 * provided by block-copy user and user is responsible for appropriate
80 * permissions on these children.
81 */
82 BdrvChild *source;
83 BdrvChild *target;
84 BdrvDirtyBitmap *copy_bitmap;
85 int64_t in_flight_bytes;
86 int64_t cluster_size;
87 bool use_copy_range;
88 int64_t copy_size;
89 uint64_t len;
90 QLIST_HEAD(, BlockCopyTask) tasks; /* All tasks from all block-copy calls */
91 QLIST_HEAD(, BlockCopyCallState) calls;
92
93 BdrvRequestFlags write_flags;
94
95 /*
96 * skip_unallocated:
97 *
98 * Used by sync=top jobs, which first scan the source node for unallocated
99 * areas and clear them in the copy_bitmap. During this process, the bitmap
100 * is thus not fully initialized: It may still have bits set for areas that
101 * are unallocated and should actually not be copied.
102 *
103 * This is indicated by skip_unallocated.
104 *
105 * In this case, block_copy() will query the source’s allocation status,
106 * skip unallocated regions, clear them in the copy_bitmap, and invoke
107 * block_copy_reset_unallocated() every time it does.
108 */
109 bool skip_unallocated;
110
111 ProgressMeter *progress;
112 /* progress_bytes_callback: called when some copying progress is done. */
113 ProgressBytesCallbackFunc progress_bytes_callback;
114 void *progress_opaque;
115
116 SharedResource *mem;
117
118 uint64_t speed;
119 RateLimit rate_limit;
120 } BlockCopyState;
121
122 static BlockCopyTask *find_conflicting_task(BlockCopyState *s,
123 int64_t offset, int64_t bytes)
124 {
125 BlockCopyTask *t;
126
127 QLIST_FOREACH(t, &s->tasks, list) {
128 if (offset + bytes > t->offset && offset < t->offset + t->bytes) {
129 return t;
130 }
131 }
132
133 return NULL;
134 }
135
136 /*
137 * If there are no intersecting tasks return false. Otherwise, wait for the
138 * first found intersecting tasks to finish and return true.
139 */
140 static bool coroutine_fn block_copy_wait_one(BlockCopyState *s, int64_t offset,
141 int64_t bytes)
142 {
143 BlockCopyTask *task = find_conflicting_task(s, offset, bytes);
144
145 if (!task) {
146 return false;
147 }
148
149 qemu_co_queue_wait(&task->wait_queue, NULL);
150
151 return true;
152 }
153
154 /*
155 * Search for the first dirty area in offset/bytes range and create task at
156 * the beginning of it.
157 */
158 static BlockCopyTask *block_copy_task_create(BlockCopyState *s,
159 BlockCopyCallState *call_state,
160 int64_t offset, int64_t bytes)
161 {
162 BlockCopyTask *task;
163 int64_t max_chunk = MIN_NON_ZERO(s->copy_size, call_state->max_chunk);
164
165 if (!bdrv_dirty_bitmap_next_dirty_area(s->copy_bitmap,
166 offset, offset + bytes,
167 max_chunk, &offset, &bytes))
168 {
169 return NULL;
170 }
171
172 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
173 bytes = QEMU_ALIGN_UP(bytes, s->cluster_size);
174
175 /* region is dirty, so no existent tasks possible in it */
176 assert(!find_conflicting_task(s, offset, bytes));
177
178 bdrv_reset_dirty_bitmap(s->copy_bitmap, offset, bytes);
179 s->in_flight_bytes += bytes;
180
181 task = g_new(BlockCopyTask, 1);
182 *task = (BlockCopyTask) {
183 .task.func = block_copy_task_entry,
184 .s = s,
185 .call_state = call_state,
186 .offset = offset,
187 .bytes = bytes,
188 };
189 qemu_co_queue_init(&task->wait_queue);
190 QLIST_INSERT_HEAD(&s->tasks, task, list);
191
192 return task;
193 }
194
195 /*
196 * block_copy_task_shrink
197 *
198 * Drop the tail of the task to be handled later. Set dirty bits back and
199 * wake up all tasks waiting for us (may be some of them are not intersecting
200 * with shrunk task)
201 */
202 static void coroutine_fn block_copy_task_shrink(BlockCopyTask *task,
203 int64_t new_bytes)
204 {
205 if (new_bytes == task->bytes) {
206 return;
207 }
208
209 assert(new_bytes > 0 && new_bytes < task->bytes);
210
211 task->s->in_flight_bytes -= task->bytes - new_bytes;
212 bdrv_set_dirty_bitmap(task->s->copy_bitmap,
213 task->offset + new_bytes, task->bytes - new_bytes);
214
215 task->bytes = new_bytes;
216 qemu_co_queue_restart_all(&task->wait_queue);
217 }
218
219 static void coroutine_fn block_copy_task_end(BlockCopyTask *task, int ret)
220 {
221 task->s->in_flight_bytes -= task->bytes;
222 if (ret < 0) {
223 bdrv_set_dirty_bitmap(task->s->copy_bitmap, task->offset, task->bytes);
224 }
225 QLIST_REMOVE(task, list);
226 qemu_co_queue_restart_all(&task->wait_queue);
227 }
228
229 void block_copy_state_free(BlockCopyState *s)
230 {
231 if (!s) {
232 return;
233 }
234
235 bdrv_release_dirty_bitmap(s->copy_bitmap);
236 shres_destroy(s->mem);
237 g_free(s);
238 }
239
240 static uint32_t block_copy_max_transfer(BdrvChild *source, BdrvChild *target)
241 {
242 return MIN_NON_ZERO(INT_MAX,
243 MIN_NON_ZERO(source->bs->bl.max_transfer,
244 target->bs->bl.max_transfer));
245 }
246
247 BlockCopyState *block_copy_state_new(BdrvChild *source, BdrvChild *target,
248 int64_t cluster_size, bool use_copy_range,
249 BdrvRequestFlags write_flags, Error **errp)
250 {
251 BlockCopyState *s;
252 BdrvDirtyBitmap *copy_bitmap;
253
254 copy_bitmap = bdrv_create_dirty_bitmap(source->bs, cluster_size, NULL,
255 errp);
256 if (!copy_bitmap) {
257 return NULL;
258 }
259 bdrv_disable_dirty_bitmap(copy_bitmap);
260
261 s = g_new(BlockCopyState, 1);
262 *s = (BlockCopyState) {
263 .source = source,
264 .target = target,
265 .copy_bitmap = copy_bitmap,
266 .cluster_size = cluster_size,
267 .len = bdrv_dirty_bitmap_size(copy_bitmap),
268 .write_flags = write_flags,
269 .mem = shres_create(BLOCK_COPY_MAX_MEM),
270 };
271
272 if (block_copy_max_transfer(source, target) < cluster_size) {
273 /*
274 * copy_range does not respect max_transfer. We don't want to bother
275 * with requests smaller than block-copy cluster size, so fallback to
276 * buffered copying (read and write respect max_transfer on their
277 * behalf).
278 */
279 s->use_copy_range = false;
280 s->copy_size = cluster_size;
281 } else if (write_flags & BDRV_REQ_WRITE_COMPRESSED) {
282 /* Compression supports only cluster-size writes and no copy-range. */
283 s->use_copy_range = false;
284 s->copy_size = cluster_size;
285 } else {
286 /*
287 * We enable copy-range, but keep small copy_size, until first
288 * successful copy_range (look at block_copy_do_copy).
289 */
290 s->use_copy_range = use_copy_range;
291 s->copy_size = MAX(s->cluster_size, BLOCK_COPY_MAX_BUFFER);
292 }
293
294 QLIST_INIT(&s->tasks);
295 QLIST_INIT(&s->calls);
296
297 return s;
298 }
299
300 void block_copy_set_progress_callback(
301 BlockCopyState *s,
302 ProgressBytesCallbackFunc progress_bytes_callback,
303 void *progress_opaque)
304 {
305 s->progress_bytes_callback = progress_bytes_callback;
306 s->progress_opaque = progress_opaque;
307 }
308
309 void block_copy_set_progress_meter(BlockCopyState *s, ProgressMeter *pm)
310 {
311 s->progress = pm;
312 }
313
314 /*
315 * Takes ownership of @task
316 *
317 * If pool is NULL directly run the task, otherwise schedule it into the pool.
318 *
319 * Returns: task.func return code if pool is NULL
320 * otherwise -ECANCELED if pool status is bad
321 * otherwise 0 (successfully scheduled)
322 */
323 static coroutine_fn int block_copy_task_run(AioTaskPool *pool,
324 BlockCopyTask *task)
325 {
326 if (!pool) {
327 int ret = task->task.func(&task->task);
328
329 g_free(task);
330 return ret;
331 }
332
333 aio_task_pool_wait_slot(pool);
334 if (aio_task_pool_status(pool) < 0) {
335 co_put_to_shres(task->s->mem, task->bytes);
336 block_copy_task_end(task, -ECANCELED);
337 g_free(task);
338 return -ECANCELED;
339 }
340
341 aio_task_pool_start_task(pool, &task->task);
342
343 return 0;
344 }
345
346 /*
347 * block_copy_do_copy
348 *
349 * Do copy of cluster-aligned chunk. Requested region is allowed to exceed
350 * s->len only to cover last cluster when s->len is not aligned to clusters.
351 *
352 * No sync here: nor bitmap neighter intersecting requests handling, only copy.
353 *
354 * Returns 0 on success.
355 */
356 static int coroutine_fn block_copy_do_copy(BlockCopyState *s,
357 int64_t offset, int64_t bytes,
358 bool zeroes, bool *error_is_read)
359 {
360 int ret;
361 int64_t nbytes = MIN(offset + bytes, s->len) - offset;
362 void *bounce_buffer = NULL;
363
364 assert(offset >= 0 && bytes > 0 && INT64_MAX - offset >= bytes);
365 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
366 assert(QEMU_IS_ALIGNED(bytes, s->cluster_size));
367 assert(offset < s->len);
368 assert(offset + bytes <= s->len ||
369 offset + bytes == QEMU_ALIGN_UP(s->len, s->cluster_size));
370 assert(nbytes < INT_MAX);
371
372 if (zeroes) {
373 ret = bdrv_co_pwrite_zeroes(s->target, offset, nbytes, s->write_flags &
374 ~BDRV_REQ_WRITE_COMPRESSED);
375 if (ret < 0) {
376 trace_block_copy_write_zeroes_fail(s, offset, ret);
377 *error_is_read = false;
378 }
379 return ret;
380 }
381
382 if (s->use_copy_range) {
383 ret = bdrv_co_copy_range(s->source, offset, s->target, offset, nbytes,
384 0, s->write_flags);
385 if (ret < 0) {
386 trace_block_copy_copy_range_fail(s, offset, ret);
387 s->use_copy_range = false;
388 s->copy_size = MAX(s->cluster_size, BLOCK_COPY_MAX_BUFFER);
389 /* Fallback to read+write with allocated buffer */
390 } else {
391 if (s->use_copy_range) {
392 /*
393 * Successful copy-range. Now increase copy_size. copy_range
394 * does not respect max_transfer (it's a TODO), so we factor
395 * that in here.
396 *
397 * Note: we double-check s->use_copy_range for the case when
398 * parallel block-copy request unsets it during previous
399 * bdrv_co_copy_range call.
400 */
401 s->copy_size =
402 MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_COPY_RANGE),
403 QEMU_ALIGN_DOWN(block_copy_max_transfer(s->source,
404 s->target),
405 s->cluster_size));
406 }
407 goto out;
408 }
409 }
410
411 /*
412 * In case of failed copy_range request above, we may proceed with buffered
413 * request larger than BLOCK_COPY_MAX_BUFFER. Still, further requests will
414 * be properly limited, so don't care too much. Moreover the most likely
415 * case (copy_range is unsupported for the configuration, so the very first
416 * copy_range request fails) is handled by setting large copy_size only
417 * after first successful copy_range.
418 */
419
420 bounce_buffer = qemu_blockalign(s->source->bs, nbytes);
421
422 ret = bdrv_co_pread(s->source, offset, nbytes, bounce_buffer, 0);
423 if (ret < 0) {
424 trace_block_copy_read_fail(s, offset, ret);
425 *error_is_read = true;
426 goto out;
427 }
428
429 ret = bdrv_co_pwrite(s->target, offset, nbytes, bounce_buffer,
430 s->write_flags);
431 if (ret < 0) {
432 trace_block_copy_write_fail(s, offset, ret);
433 *error_is_read = false;
434 goto out;
435 }
436
437 out:
438 qemu_vfree(bounce_buffer);
439
440 return ret;
441 }
442
443 static coroutine_fn int block_copy_task_entry(AioTask *task)
444 {
445 BlockCopyTask *t = container_of(task, BlockCopyTask, task);
446 bool error_is_read = false;
447 int ret;
448
449 ret = block_copy_do_copy(t->s, t->offset, t->bytes, t->zeroes,
450 &error_is_read);
451 if (ret < 0 && !t->call_state->ret) {
452 t->call_state->ret = ret;
453 t->call_state->error_is_read = error_is_read;
454 } else {
455 progress_work_done(t->s->progress, t->bytes);
456 t->s->progress_bytes_callback(t->bytes, t->s->progress_opaque);
457 }
458 co_put_to_shres(t->s->mem, t->bytes);
459 block_copy_task_end(t, ret);
460
461 return ret;
462 }
463
464 static int block_copy_block_status(BlockCopyState *s, int64_t offset,
465 int64_t bytes, int64_t *pnum)
466 {
467 int64_t num;
468 BlockDriverState *base;
469 int ret;
470
471 if (s->skip_unallocated) {
472 base = bdrv_backing_chain_next(s->source->bs);
473 } else {
474 base = NULL;
475 }
476
477 ret = bdrv_block_status_above(s->source->bs, base, offset, bytes, &num,
478 NULL, NULL);
479 if (ret < 0 || num < s->cluster_size) {
480 /*
481 * On error or if failed to obtain large enough chunk just fallback to
482 * copy one cluster.
483 */
484 num = s->cluster_size;
485 ret = BDRV_BLOCK_ALLOCATED | BDRV_BLOCK_DATA;
486 } else if (offset + num == s->len) {
487 num = QEMU_ALIGN_UP(num, s->cluster_size);
488 } else {
489 num = QEMU_ALIGN_DOWN(num, s->cluster_size);
490 }
491
492 *pnum = num;
493 return ret;
494 }
495
496 /*
497 * Check if the cluster starting at offset is allocated or not.
498 * return via pnum the number of contiguous clusters sharing this allocation.
499 */
500 static int block_copy_is_cluster_allocated(BlockCopyState *s, int64_t offset,
501 int64_t *pnum)
502 {
503 BlockDriverState *bs = s->source->bs;
504 int64_t count, total_count = 0;
505 int64_t bytes = s->len - offset;
506 int ret;
507
508 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
509
510 while (true) {
511 ret = bdrv_is_allocated(bs, offset, bytes, &count);
512 if (ret < 0) {
513 return ret;
514 }
515
516 total_count += count;
517
518 if (ret || count == 0) {
519 /*
520 * ret: partial segment(s) are considered allocated.
521 * otherwise: unallocated tail is treated as an entire segment.
522 */
523 *pnum = DIV_ROUND_UP(total_count, s->cluster_size);
524 return ret;
525 }
526
527 /* Unallocated segment(s) with uncertain following segment(s) */
528 if (total_count >= s->cluster_size) {
529 *pnum = total_count / s->cluster_size;
530 return 0;
531 }
532
533 offset += count;
534 bytes -= count;
535 }
536 }
537
538 /*
539 * Reset bits in copy_bitmap starting at offset if they represent unallocated
540 * data in the image. May reset subsequent contiguous bits.
541 * @return 0 when the cluster at @offset was unallocated,
542 * 1 otherwise, and -ret on error.
543 */
544 int64_t block_copy_reset_unallocated(BlockCopyState *s,
545 int64_t offset, int64_t *count)
546 {
547 int ret;
548 int64_t clusters, bytes;
549
550 ret = block_copy_is_cluster_allocated(s, offset, &clusters);
551 if (ret < 0) {
552 return ret;
553 }
554
555 bytes = clusters * s->cluster_size;
556
557 if (!ret) {
558 bdrv_reset_dirty_bitmap(s->copy_bitmap, offset, bytes);
559 progress_set_remaining(s->progress,
560 bdrv_get_dirty_count(s->copy_bitmap) +
561 s->in_flight_bytes);
562 }
563
564 *count = bytes;
565 return ret;
566 }
567
568 /*
569 * block_copy_dirty_clusters
570 *
571 * Copy dirty clusters in @offset/@bytes range.
572 * Returns 1 if dirty clusters found and successfully copied, 0 if no dirty
573 * clusters found and -errno on failure.
574 */
575 static int coroutine_fn
576 block_copy_dirty_clusters(BlockCopyCallState *call_state)
577 {
578 BlockCopyState *s = call_state->s;
579 int64_t offset = call_state->offset;
580 int64_t bytes = call_state->bytes;
581
582 int ret = 0;
583 bool found_dirty = false;
584 int64_t end = offset + bytes;
585 AioTaskPool *aio = NULL;
586
587 /*
588 * block_copy() user is responsible for keeping source and target in same
589 * aio context
590 */
591 assert(bdrv_get_aio_context(s->source->bs) ==
592 bdrv_get_aio_context(s->target->bs));
593
594 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
595 assert(QEMU_IS_ALIGNED(bytes, s->cluster_size));
596
597 while (bytes && aio_task_pool_status(aio) == 0) {
598 BlockCopyTask *task;
599 int64_t status_bytes;
600
601 task = block_copy_task_create(s, call_state, offset, bytes);
602 if (!task) {
603 /* No more dirty bits in the bitmap */
604 trace_block_copy_skip_range(s, offset, bytes);
605 break;
606 }
607 if (task->offset > offset) {
608 trace_block_copy_skip_range(s, offset, task->offset - offset);
609 }
610
611 found_dirty = true;
612
613 ret = block_copy_block_status(s, task->offset, task->bytes,
614 &status_bytes);
615 assert(ret >= 0); /* never fail */
616 if (status_bytes < task->bytes) {
617 block_copy_task_shrink(task, status_bytes);
618 }
619 if (s->skip_unallocated && !(ret & BDRV_BLOCK_ALLOCATED)) {
620 block_copy_task_end(task, 0);
621 progress_set_remaining(s->progress,
622 bdrv_get_dirty_count(s->copy_bitmap) +
623 s->in_flight_bytes);
624 trace_block_copy_skip_range(s, task->offset, task->bytes);
625 offset = task_end(task);
626 bytes = end - offset;
627 g_free(task);
628 continue;
629 }
630 task->zeroes = ret & BDRV_BLOCK_ZERO;
631
632 if (s->speed) {
633 if (!call_state->ignore_ratelimit) {
634 uint64_t ns = ratelimit_calculate_delay(&s->rate_limit, 0);
635 if (ns > 0) {
636 block_copy_task_end(task, -EAGAIN);
637 g_free(task);
638 qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, ns,
639 &call_state->sleep_state);
640 continue;
641 }
642 }
643
644 ratelimit_calculate_delay(&s->rate_limit, task->bytes);
645 }
646
647 trace_block_copy_process(s, task->offset);
648
649 co_get_from_shres(s->mem, task->bytes);
650
651 offset = task_end(task);
652 bytes = end - offset;
653
654 if (!aio && bytes) {
655 aio = aio_task_pool_new(call_state->max_workers);
656 }
657
658 ret = block_copy_task_run(aio, task);
659 if (ret < 0) {
660 goto out;
661 }
662 }
663
664 out:
665 if (aio) {
666 aio_task_pool_wait_all(aio);
667
668 /*
669 * We are not really interested in -ECANCELED returned from
670 * block_copy_task_run. If it fails, it means some task already failed
671 * for real reason, let's return first failure.
672 * Still, assert that we don't rewrite failure by success.
673 *
674 * Note: ret may be positive here because of block-status result.
675 */
676 assert(ret >= 0 || aio_task_pool_status(aio) < 0);
677 ret = aio_task_pool_status(aio);
678
679 aio_task_pool_free(aio);
680 }
681
682 return ret < 0 ? ret : found_dirty;
683 }
684
685 void block_copy_kick(BlockCopyCallState *call_state)
686 {
687 if (call_state->sleep_state) {
688 qemu_co_sleep_wake(call_state->sleep_state);
689 }
690 }
691
692 /*
693 * block_copy_common
694 *
695 * Copy requested region, accordingly to dirty bitmap.
696 * Collaborate with parallel block_copy requests: if they succeed it will help
697 * us. If they fail, we will retry not-copied regions. So, if we return error,
698 * it means that some I/O operation failed in context of _this_ block_copy call,
699 * not some parallel operation.
700 */
701 static int coroutine_fn block_copy_common(BlockCopyCallState *call_state)
702 {
703 int ret;
704
705 QLIST_INSERT_HEAD(&call_state->s->calls, call_state, list);
706
707 do {
708 ret = block_copy_dirty_clusters(call_state);
709
710 if (ret == 0) {
711 ret = block_copy_wait_one(call_state->s, call_state->offset,
712 call_state->bytes);
713 }
714
715 /*
716 * We retry in two cases:
717 * 1. Some progress done
718 * Something was copied, which means that there were yield points
719 * and some new dirty bits may have appeared (due to failed parallel
720 * block-copy requests).
721 * 2. We have waited for some intersecting block-copy request
722 * It may have failed and produced new dirty bits.
723 */
724 } while (ret > 0);
725
726 call_state->finished = true;
727
728 if (call_state->cb) {
729 call_state->cb(call_state->cb_opaque);
730 }
731
732 QLIST_REMOVE(call_state, list);
733
734 return ret;
735 }
736
737 int coroutine_fn block_copy(BlockCopyState *s, int64_t start, int64_t bytes,
738 bool ignore_ratelimit, bool *error_is_read)
739 {
740 BlockCopyCallState call_state = {
741 .s = s,
742 .offset = start,
743 .bytes = bytes,
744 .ignore_ratelimit = ignore_ratelimit,
745 .max_workers = BLOCK_COPY_MAX_WORKERS,
746 };
747
748 int ret = block_copy_common(&call_state);
749
750 if (error_is_read && ret < 0) {
751 *error_is_read = call_state.error_is_read;
752 }
753
754 return ret;
755 }
756
757 static void coroutine_fn block_copy_async_co_entry(void *opaque)
758 {
759 block_copy_common(opaque);
760 }
761
762 BlockCopyCallState *block_copy_async(BlockCopyState *s,
763 int64_t offset, int64_t bytes,
764 int max_workers, int64_t max_chunk,
765 BlockCopyAsyncCallbackFunc cb,
766 void *cb_opaque)
767 {
768 BlockCopyCallState *call_state = g_new(BlockCopyCallState, 1);
769
770 *call_state = (BlockCopyCallState) {
771 .s = s,
772 .offset = offset,
773 .bytes = bytes,
774 .max_workers = max_workers,
775 .max_chunk = max_chunk,
776 .cb = cb,
777 .cb_opaque = cb_opaque,
778
779 .co = qemu_coroutine_create(block_copy_async_co_entry, call_state),
780 };
781
782 qemu_coroutine_enter(call_state->co);
783
784 return call_state;
785 }
786
787 void block_copy_call_free(BlockCopyCallState *call_state)
788 {
789 if (!call_state) {
790 return;
791 }
792
793 assert(call_state->finished);
794 g_free(call_state);
795 }
796
797 bool block_copy_call_finished(BlockCopyCallState *call_state)
798 {
799 return call_state->finished;
800 }
801
802 bool block_copy_call_succeeded(BlockCopyCallState *call_state)
803 {
804 return call_state->finished && call_state->ret == 0;
805 }
806
807 bool block_copy_call_failed(BlockCopyCallState *call_state)
808 {
809 return call_state->finished && call_state->ret < 0;
810 }
811
812 int block_copy_call_status(BlockCopyCallState *call_state, bool *error_is_read)
813 {
814 assert(call_state->finished);
815 if (error_is_read) {
816 *error_is_read = call_state->error_is_read;
817 }
818 return call_state->ret;
819 }
820
821 BdrvDirtyBitmap *block_copy_dirty_bitmap(BlockCopyState *s)
822 {
823 return s->copy_bitmap;
824 }
825
826 void block_copy_set_skip_unallocated(BlockCopyState *s, bool skip)
827 {
828 s->skip_unallocated = skip;
829 }
830
831 void block_copy_set_speed(BlockCopyState *s, uint64_t speed)
832 {
833 s->speed = speed;
834 if (speed > 0) {
835 ratelimit_set_speed(&s->rate_limit, speed, BLOCK_COPY_SLICE_TIME);
836 }
837
838 /*
839 * Note: it's good to kick all call states from here, but it should be done
840 * only from a coroutine, to not crash if s->calls list changed while
841 * entering one call. So for now, the only user of this function kicks its
842 * only one call_state by hand.
843 */
844 }