4 * Copyright (C) 2013 Proxmox Server Solutions
5 * Copyright (c) 2019 Virtuozzo International GmbH.
8 * Dietmar Maurer (dietmar@proxmox.com)
9 * Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
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.
15 #include "qemu/osdep.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"
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 */
31 static coroutine_fn
int block_copy_task_entry(AioTask
*task
);
33 typedef struct BlockCopyCallState
{
34 /* IN parameters. Initialized in block_copy_async() and never changed. */
40 bool ignore_ratelimit
;
41 BlockCopyAsyncCallbackFunc cb
;
44 /* Coroutine where async block-copy is running */
47 /* To reference all call states from BlockCopyState */
48 QLIST_ENTRY(BlockCopyCallState
) list
;
60 typedef struct BlockCopyTask
{
64 BlockCopyCallState
*call_state
;
69 QLIST_ENTRY(BlockCopyTask
) list
;
70 CoQueue wait_queue
; /* coroutines blocked on this task */
73 static int64_t task_end(BlockCopyTask
*task
)
75 return task
->offset
+ task
->bytes
;
78 typedef struct BlockCopyState
{
80 * BdrvChild objects are not owned or managed by block-copy. They are
81 * provided by block-copy user and user is responsible for appropriate
82 * permissions on these children.
86 BdrvDirtyBitmap
*copy_bitmap
;
87 int64_t in_flight_bytes
;
92 QLIST_HEAD(, BlockCopyTask
) tasks
; /* All tasks from all block-copy calls */
93 QLIST_HEAD(, BlockCopyCallState
) calls
;
95 BdrvRequestFlags write_flags
;
100 * Used by sync=top jobs, which first scan the source node for unallocated
101 * areas and clear them in the copy_bitmap. During this process, the bitmap
102 * is thus not fully initialized: It may still have bits set for areas that
103 * are unallocated and should actually not be copied.
105 * This is indicated by skip_unallocated.
107 * In this case, block_copy() will query the source’s allocation status,
108 * skip unallocated regions, clear them in the copy_bitmap, and invoke
109 * block_copy_reset_unallocated() every time it does.
111 bool skip_unallocated
;
113 ProgressMeter
*progress
;
118 RateLimit rate_limit
;
121 static BlockCopyTask
*find_conflicting_task(BlockCopyState
*s
,
122 int64_t offset
, int64_t bytes
)
126 QLIST_FOREACH(t
, &s
->tasks
, list
) {
127 if (offset
+ bytes
> t
->offset
&& offset
< t
->offset
+ t
->bytes
) {
136 * If there are no intersecting tasks return false. Otherwise, wait for the
137 * first found intersecting tasks to finish and return true.
139 static bool coroutine_fn
block_copy_wait_one(BlockCopyState
*s
, int64_t offset
,
142 BlockCopyTask
*task
= find_conflicting_task(s
, offset
, bytes
);
148 qemu_co_queue_wait(&task
->wait_queue
, NULL
);
154 * Search for the first dirty area in offset/bytes range and create task at
155 * the beginning of it.
157 static BlockCopyTask
*block_copy_task_create(BlockCopyState
*s
,
158 BlockCopyCallState
*call_state
,
159 int64_t offset
, int64_t bytes
)
162 int64_t max_chunk
= MIN_NON_ZERO(s
->copy_size
, call_state
->max_chunk
);
164 if (!bdrv_dirty_bitmap_next_dirty_area(s
->copy_bitmap
,
165 offset
, offset
+ bytes
,
166 max_chunk
, &offset
, &bytes
))
171 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
172 bytes
= QEMU_ALIGN_UP(bytes
, s
->cluster_size
);
174 /* region is dirty, so no existent tasks possible in it */
175 assert(!find_conflicting_task(s
, offset
, bytes
));
177 bdrv_reset_dirty_bitmap(s
->copy_bitmap
, offset
, bytes
);
178 s
->in_flight_bytes
+= bytes
;
180 task
= g_new(BlockCopyTask
, 1);
181 *task
= (BlockCopyTask
) {
182 .task
.func
= block_copy_task_entry
,
184 .call_state
= call_state
,
187 .copy_range
= s
->use_copy_range
,
189 qemu_co_queue_init(&task
->wait_queue
);
190 QLIST_INSERT_HEAD(&s
->tasks
, task
, list
);
196 * block_copy_task_shrink
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
202 static void coroutine_fn
block_copy_task_shrink(BlockCopyTask
*task
,
205 if (new_bytes
== task
->bytes
) {
209 assert(new_bytes
> 0 && new_bytes
< task
->bytes
);
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
);
215 task
->bytes
= new_bytes
;
216 qemu_co_queue_restart_all(&task
->wait_queue
);
219 static void coroutine_fn
block_copy_task_end(BlockCopyTask
*task
, int ret
)
221 task
->s
->in_flight_bytes
-= task
->bytes
;
223 bdrv_set_dirty_bitmap(task
->s
->copy_bitmap
, task
->offset
, task
->bytes
);
225 QLIST_REMOVE(task
, list
);
226 qemu_co_queue_restart_all(&task
->wait_queue
);
229 void block_copy_state_free(BlockCopyState
*s
)
235 ratelimit_destroy(&s
->rate_limit
);
236 bdrv_release_dirty_bitmap(s
->copy_bitmap
);
237 shres_destroy(s
->mem
);
241 static uint32_t block_copy_max_transfer(BdrvChild
*source
, BdrvChild
*target
)
243 return MIN_NON_ZERO(INT_MAX
,
244 MIN_NON_ZERO(source
->bs
->bl
.max_transfer
,
245 target
->bs
->bl
.max_transfer
));
248 BlockCopyState
*block_copy_state_new(BdrvChild
*source
, BdrvChild
*target
,
249 int64_t cluster_size
, bool use_copy_range
,
250 BdrvRequestFlags write_flags
, Error
**errp
)
253 BdrvDirtyBitmap
*copy_bitmap
;
255 copy_bitmap
= bdrv_create_dirty_bitmap(source
->bs
, cluster_size
, NULL
,
260 bdrv_disable_dirty_bitmap(copy_bitmap
);
262 s
= g_new(BlockCopyState
, 1);
263 *s
= (BlockCopyState
) {
266 .copy_bitmap
= copy_bitmap
,
267 .cluster_size
= cluster_size
,
268 .len
= bdrv_dirty_bitmap_size(copy_bitmap
),
269 .write_flags
= write_flags
,
270 .mem
= shres_create(BLOCK_COPY_MAX_MEM
),
273 if (block_copy_max_transfer(source
, target
) < cluster_size
) {
275 * copy_range does not respect max_transfer. We don't want to bother
276 * with requests smaller than block-copy cluster size, so fallback to
277 * buffered copying (read and write respect max_transfer on their
280 s
->use_copy_range
= false;
281 s
->copy_size
= cluster_size
;
282 } else if (write_flags
& BDRV_REQ_WRITE_COMPRESSED
) {
283 /* Compression supports only cluster-size writes and no copy-range. */
284 s
->use_copy_range
= false;
285 s
->copy_size
= cluster_size
;
288 * We enable copy-range, but keep small copy_size, until first
289 * successful copy_range (look at block_copy_do_copy).
291 s
->use_copy_range
= use_copy_range
;
292 s
->copy_size
= MAX(s
->cluster_size
, BLOCK_COPY_MAX_BUFFER
);
295 ratelimit_init(&s
->rate_limit
);
296 QLIST_INIT(&s
->tasks
);
297 QLIST_INIT(&s
->calls
);
302 void block_copy_set_progress_meter(BlockCopyState
*s
, ProgressMeter
*pm
)
308 * Takes ownership of @task
310 * If pool is NULL directly run the task, otherwise schedule it into the pool.
312 * Returns: task.func return code if pool is NULL
313 * otherwise -ECANCELED if pool status is bad
314 * otherwise 0 (successfully scheduled)
316 static coroutine_fn
int block_copy_task_run(AioTaskPool
*pool
,
320 int ret
= task
->task
.func(&task
->task
);
326 aio_task_pool_wait_slot(pool
);
327 if (aio_task_pool_status(pool
) < 0) {
328 co_put_to_shres(task
->s
->mem
, task
->bytes
);
329 block_copy_task_end(task
, -ECANCELED
);
334 aio_task_pool_start_task(pool
, &task
->task
);
342 * Do copy of cluster-aligned chunk. Requested region is allowed to exceed
343 * s->len only to cover last cluster when s->len is not aligned to clusters.
345 * No sync here: nor bitmap neighter intersecting requests handling, only copy.
347 * @copy_range is an in-out argument: if *copy_range is false, copy_range is not
348 * done. If *copy_range is true, copy_range is attempted. If the copy_range
349 * attempt fails, the function falls back to the usual read+write and
350 * *copy_range is set to false. *copy_range and zeroes must not be true
353 * Returns 0 on success.
355 static int coroutine_fn
block_copy_do_copy(BlockCopyState
*s
,
356 int64_t offset
, int64_t bytes
,
357 bool zeroes
, bool *copy_range
,
361 int64_t nbytes
= MIN(offset
+ bytes
, s
->len
) - offset
;
362 void *bounce_buffer
= NULL
;
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 assert(!(*copy_range
&& zeroes
));
374 ret
= bdrv_co_pwrite_zeroes(s
->target
, offset
, nbytes
, s
->write_flags
&
375 ~BDRV_REQ_WRITE_COMPRESSED
);
377 trace_block_copy_write_zeroes_fail(s
, offset
, ret
);
378 *error_is_read
= false;
384 ret
= bdrv_co_copy_range(s
->source
, offset
, s
->target
, offset
, nbytes
,
387 trace_block_copy_copy_range_fail(s
, offset
, ret
);
389 /* Fallback to read+write with allocated buffer */
396 * In case of failed copy_range request above, we may proceed with buffered
397 * request larger than BLOCK_COPY_MAX_BUFFER. Still, further requests will
398 * be properly limited, so don't care too much. Moreover the most likely
399 * case (copy_range is unsupported for the configuration, so the very first
400 * copy_range request fails) is handled by setting large copy_size only
401 * after first successful copy_range.
404 bounce_buffer
= qemu_blockalign(s
->source
->bs
, nbytes
);
406 ret
= bdrv_co_pread(s
->source
, offset
, nbytes
, bounce_buffer
, 0);
408 trace_block_copy_read_fail(s
, offset
, ret
);
409 *error_is_read
= true;
413 ret
= bdrv_co_pwrite(s
->target
, offset
, nbytes
, bounce_buffer
,
416 trace_block_copy_write_fail(s
, offset
, ret
);
417 *error_is_read
= false;
422 qemu_vfree(bounce_buffer
);
427 static void block_copy_handle_copy_range_result(BlockCopyState
*s
,
430 if (!s
->use_copy_range
) {
431 /* already disabled */
437 * Successful copy-range. Now increase copy_size. copy_range
438 * does not respect max_transfer (it's a TODO), so we factor
442 MIN(MAX(s
->cluster_size
, BLOCK_COPY_MAX_COPY_RANGE
),
443 QEMU_ALIGN_DOWN(block_copy_max_transfer(s
->source
,
447 /* Copy-range failed, disable it. */
448 s
->use_copy_range
= false;
449 s
->copy_size
= MAX(s
->cluster_size
, BLOCK_COPY_MAX_BUFFER
);
453 static coroutine_fn
int block_copy_task_entry(AioTask
*task
)
455 BlockCopyTask
*t
= container_of(task
, BlockCopyTask
, task
);
456 bool error_is_read
= false;
457 bool copy_range
= t
->copy_range
;
460 ret
= block_copy_do_copy(t
->s
, t
->offset
, t
->bytes
, t
->zeroes
,
461 ©_range
, &error_is_read
);
463 block_copy_handle_copy_range_result(t
->s
, copy_range
);
466 if (!t
->call_state
->ret
) {
467 t
->call_state
->ret
= ret
;
468 t
->call_state
->error_is_read
= error_is_read
;
471 progress_work_done(t
->s
->progress
, t
->bytes
);
473 co_put_to_shres(t
->s
->mem
, t
->bytes
);
474 block_copy_task_end(t
, ret
);
479 static int block_copy_block_status(BlockCopyState
*s
, int64_t offset
,
480 int64_t bytes
, int64_t *pnum
)
483 BlockDriverState
*base
;
486 if (s
->skip_unallocated
) {
487 base
= bdrv_backing_chain_next(s
->source
->bs
);
492 ret
= bdrv_block_status_above(s
->source
->bs
, base
, offset
, bytes
, &num
,
494 if (ret
< 0 || num
< s
->cluster_size
) {
496 * On error or if failed to obtain large enough chunk just fallback to
499 num
= s
->cluster_size
;
500 ret
= BDRV_BLOCK_ALLOCATED
| BDRV_BLOCK_DATA
;
501 } else if (offset
+ num
== s
->len
) {
502 num
= QEMU_ALIGN_UP(num
, s
->cluster_size
);
504 num
= QEMU_ALIGN_DOWN(num
, s
->cluster_size
);
512 * Check if the cluster starting at offset is allocated or not.
513 * return via pnum the number of contiguous clusters sharing this allocation.
515 static int block_copy_is_cluster_allocated(BlockCopyState
*s
, int64_t offset
,
518 BlockDriverState
*bs
= s
->source
->bs
;
519 int64_t count
, total_count
= 0;
520 int64_t bytes
= s
->len
- offset
;
523 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
526 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &count
);
531 total_count
+= count
;
533 if (ret
|| count
== 0) {
535 * ret: partial segment(s) are considered allocated.
536 * otherwise: unallocated tail is treated as an entire segment.
538 *pnum
= DIV_ROUND_UP(total_count
, s
->cluster_size
);
542 /* Unallocated segment(s) with uncertain following segment(s) */
543 if (total_count
>= s
->cluster_size
) {
544 *pnum
= total_count
/ s
->cluster_size
;
554 * Reset bits in copy_bitmap starting at offset if they represent unallocated
555 * data in the image. May reset subsequent contiguous bits.
556 * @return 0 when the cluster at @offset was unallocated,
557 * 1 otherwise, and -ret on error.
559 int64_t block_copy_reset_unallocated(BlockCopyState
*s
,
560 int64_t offset
, int64_t *count
)
563 int64_t clusters
, bytes
;
565 ret
= block_copy_is_cluster_allocated(s
, offset
, &clusters
);
570 bytes
= clusters
* s
->cluster_size
;
573 bdrv_reset_dirty_bitmap(s
->copy_bitmap
, offset
, bytes
);
574 progress_set_remaining(s
->progress
,
575 bdrv_get_dirty_count(s
->copy_bitmap
) +
584 * block_copy_dirty_clusters
586 * Copy dirty clusters in @offset/@bytes range.
587 * Returns 1 if dirty clusters found and successfully copied, 0 if no dirty
588 * clusters found and -errno on failure.
590 static int coroutine_fn
591 block_copy_dirty_clusters(BlockCopyCallState
*call_state
)
593 BlockCopyState
*s
= call_state
->s
;
594 int64_t offset
= call_state
->offset
;
595 int64_t bytes
= call_state
->bytes
;
598 bool found_dirty
= false;
599 int64_t end
= offset
+ bytes
;
600 AioTaskPool
*aio
= NULL
;
603 * block_copy() user is responsible for keeping source and target in same
606 assert(bdrv_get_aio_context(s
->source
->bs
) ==
607 bdrv_get_aio_context(s
->target
->bs
));
609 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
610 assert(QEMU_IS_ALIGNED(bytes
, s
->cluster_size
));
612 while (bytes
&& aio_task_pool_status(aio
) == 0 && !call_state
->cancelled
) {
614 int64_t status_bytes
;
616 task
= block_copy_task_create(s
, call_state
, offset
, bytes
);
618 /* No more dirty bits in the bitmap */
619 trace_block_copy_skip_range(s
, offset
, bytes
);
622 if (task
->offset
> offset
) {
623 trace_block_copy_skip_range(s
, offset
, task
->offset
- offset
);
628 ret
= block_copy_block_status(s
, task
->offset
, task
->bytes
,
630 assert(ret
>= 0); /* never fail */
631 if (status_bytes
< task
->bytes
) {
632 block_copy_task_shrink(task
, status_bytes
);
634 if (s
->skip_unallocated
&& !(ret
& BDRV_BLOCK_ALLOCATED
)) {
635 block_copy_task_end(task
, 0);
636 progress_set_remaining(s
->progress
,
637 bdrv_get_dirty_count(s
->copy_bitmap
) +
639 trace_block_copy_skip_range(s
, task
->offset
, task
->bytes
);
640 offset
= task_end(task
);
641 bytes
= end
- offset
;
645 if (ret
& BDRV_BLOCK_ZERO
) {
647 task
->copy_range
= false;
651 if (!call_state
->ignore_ratelimit
) {
652 uint64_t ns
= ratelimit_calculate_delay(&s
->rate_limit
, 0);
654 block_copy_task_end(task
, -EAGAIN
);
656 qemu_co_sleep_ns_wakeable(&call_state
->sleep
,
657 QEMU_CLOCK_REALTIME
, ns
);
662 ratelimit_calculate_delay(&s
->rate_limit
, task
->bytes
);
665 trace_block_copy_process(s
, task
->offset
);
667 co_get_from_shres(s
->mem
, task
->bytes
);
669 offset
= task_end(task
);
670 bytes
= end
- offset
;
673 aio
= aio_task_pool_new(call_state
->max_workers
);
676 ret
= block_copy_task_run(aio
, task
);
684 aio_task_pool_wait_all(aio
);
687 * We are not really interested in -ECANCELED returned from
688 * block_copy_task_run. If it fails, it means some task already failed
689 * for real reason, let's return first failure.
690 * Still, assert that we don't rewrite failure by success.
692 * Note: ret may be positive here because of block-status result.
694 assert(ret
>= 0 || aio_task_pool_status(aio
) < 0);
695 ret
= aio_task_pool_status(aio
);
697 aio_task_pool_free(aio
);
700 return ret
< 0 ? ret
: found_dirty
;
703 void block_copy_kick(BlockCopyCallState
*call_state
)
705 qemu_co_sleep_wake(&call_state
->sleep
);
711 * Copy requested region, accordingly to dirty bitmap.
712 * Collaborate with parallel block_copy requests: if they succeed it will help
713 * us. If they fail, we will retry not-copied regions. So, if we return error,
714 * it means that some I/O operation failed in context of _this_ block_copy call,
715 * not some parallel operation.
717 static int coroutine_fn
block_copy_common(BlockCopyCallState
*call_state
)
721 QLIST_INSERT_HEAD(&call_state
->s
->calls
, call_state
, list
);
724 ret
= block_copy_dirty_clusters(call_state
);
726 if (ret
== 0 && !call_state
->cancelled
) {
727 ret
= block_copy_wait_one(call_state
->s
, call_state
->offset
,
732 * We retry in two cases:
733 * 1. Some progress done
734 * Something was copied, which means that there were yield points
735 * and some new dirty bits may have appeared (due to failed parallel
736 * block-copy requests).
737 * 2. We have waited for some intersecting block-copy request
738 * It may have failed and produced new dirty bits.
740 } while (ret
> 0 && !call_state
->cancelled
);
742 call_state
->finished
= true;
744 if (call_state
->cb
) {
745 call_state
->cb(call_state
->cb_opaque
);
748 QLIST_REMOVE(call_state
, list
);
753 int coroutine_fn
block_copy(BlockCopyState
*s
, int64_t start
, int64_t bytes
,
754 bool ignore_ratelimit
)
756 BlockCopyCallState call_state
= {
760 .ignore_ratelimit
= ignore_ratelimit
,
761 .max_workers
= BLOCK_COPY_MAX_WORKERS
,
764 return block_copy_common(&call_state
);
767 static void coroutine_fn
block_copy_async_co_entry(void *opaque
)
769 block_copy_common(opaque
);
772 BlockCopyCallState
*block_copy_async(BlockCopyState
*s
,
773 int64_t offset
, int64_t bytes
,
774 int max_workers
, int64_t max_chunk
,
775 BlockCopyAsyncCallbackFunc cb
,
778 BlockCopyCallState
*call_state
= g_new(BlockCopyCallState
, 1);
780 *call_state
= (BlockCopyCallState
) {
784 .max_workers
= max_workers
,
785 .max_chunk
= max_chunk
,
787 .cb_opaque
= cb_opaque
,
789 .co
= qemu_coroutine_create(block_copy_async_co_entry
, call_state
),
792 qemu_coroutine_enter(call_state
->co
);
797 void block_copy_call_free(BlockCopyCallState
*call_state
)
803 assert(call_state
->finished
);
807 bool block_copy_call_finished(BlockCopyCallState
*call_state
)
809 return call_state
->finished
;
812 bool block_copy_call_succeeded(BlockCopyCallState
*call_state
)
814 return call_state
->finished
&& !call_state
->cancelled
&&
815 call_state
->ret
== 0;
818 bool block_copy_call_failed(BlockCopyCallState
*call_state
)
820 return call_state
->finished
&& !call_state
->cancelled
&&
824 bool block_copy_call_cancelled(BlockCopyCallState
*call_state
)
826 return call_state
->cancelled
;
829 int block_copy_call_status(BlockCopyCallState
*call_state
, bool *error_is_read
)
831 assert(call_state
->finished
);
833 *error_is_read
= call_state
->error_is_read
;
835 return call_state
->ret
;
838 void block_copy_call_cancel(BlockCopyCallState
*call_state
)
840 call_state
->cancelled
= true;
841 block_copy_kick(call_state
);
844 BdrvDirtyBitmap
*block_copy_dirty_bitmap(BlockCopyState
*s
)
846 return s
->copy_bitmap
;
849 void block_copy_set_skip_unallocated(BlockCopyState
*s
, bool skip
)
851 s
->skip_unallocated
= skip
;
854 void block_copy_set_speed(BlockCopyState
*s
, uint64_t speed
)
858 ratelimit_set_speed(&s
->rate_limit
, speed
, BLOCK_COPY_SLICE_TIME
);
862 * Note: it's good to kick all call states from here, but it should be done
863 * only from a coroutine, to not crash if s->calls list changed while
864 * entering one call. So for now, the only user of this function kicks its
865 * only one call_state by hand.