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
30 static coroutine_fn
int block_copy_task_entry(AioTask
*task
);
32 typedef struct BlockCopyCallState
{
45 typedef struct BlockCopyTask
{
49 BlockCopyCallState
*call_state
;
53 QLIST_ENTRY(BlockCopyTask
) list
;
54 CoQueue wait_queue
; /* coroutines blocked on this task */
57 static int64_t task_end(BlockCopyTask
*task
)
59 return task
->offset
+ task
->bytes
;
62 typedef struct BlockCopyState
{
64 * BdrvChild objects are not owned or managed by block-copy. They are
65 * provided by block-copy user and user is responsible for appropriate
66 * permissions on these children.
70 BdrvDirtyBitmap
*copy_bitmap
;
71 int64_t in_flight_bytes
;
76 QLIST_HEAD(, BlockCopyTask
) tasks
;
78 BdrvRequestFlags write_flags
;
83 * Used by sync=top jobs, which first scan the source node for unallocated
84 * areas and clear them in the copy_bitmap. During this process, the bitmap
85 * is thus not fully initialized: It may still have bits set for areas that
86 * are unallocated and should actually not be copied.
88 * This is indicated by skip_unallocated.
90 * In this case, block_copy() will query the source’s allocation status,
91 * skip unallocated regions, clear them in the copy_bitmap, and invoke
92 * block_copy_reset_unallocated() every time it does.
94 bool skip_unallocated
;
96 ProgressMeter
*progress
;
97 /* progress_bytes_callback: called when some copying progress is done. */
98 ProgressBytesCallbackFunc progress_bytes_callback
;
99 void *progress_opaque
;
104 static BlockCopyTask
*find_conflicting_task(BlockCopyState
*s
,
105 int64_t offset
, int64_t bytes
)
109 QLIST_FOREACH(t
, &s
->tasks
, list
) {
110 if (offset
+ bytes
> t
->offset
&& offset
< t
->offset
+ t
->bytes
) {
119 * If there are no intersecting tasks return false. Otherwise, wait for the
120 * first found intersecting tasks to finish and return true.
122 static bool coroutine_fn
block_copy_wait_one(BlockCopyState
*s
, int64_t offset
,
125 BlockCopyTask
*task
= find_conflicting_task(s
, offset
, bytes
);
131 qemu_co_queue_wait(&task
->wait_queue
, NULL
);
137 * Search for the first dirty area in offset/bytes range and create task at
138 * the beginning of it.
140 static BlockCopyTask
*block_copy_task_create(BlockCopyState
*s
,
141 BlockCopyCallState
*call_state
,
142 int64_t offset
, int64_t bytes
)
146 if (!bdrv_dirty_bitmap_next_dirty_area(s
->copy_bitmap
,
147 offset
, offset
+ bytes
,
148 s
->copy_size
, &offset
, &bytes
))
153 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
154 bytes
= QEMU_ALIGN_UP(bytes
, s
->cluster_size
);
156 /* region is dirty, so no existent tasks possible in it */
157 assert(!find_conflicting_task(s
, offset
, bytes
));
159 bdrv_reset_dirty_bitmap(s
->copy_bitmap
, offset
, bytes
);
160 s
->in_flight_bytes
+= bytes
;
162 task
= g_new(BlockCopyTask
, 1);
163 *task
= (BlockCopyTask
) {
164 .task
.func
= block_copy_task_entry
,
166 .call_state
= call_state
,
170 qemu_co_queue_init(&task
->wait_queue
);
171 QLIST_INSERT_HEAD(&s
->tasks
, task
, list
);
177 * block_copy_task_shrink
179 * Drop the tail of the task to be handled later. Set dirty bits back and
180 * wake up all tasks waiting for us (may be some of them are not intersecting
183 static void coroutine_fn
block_copy_task_shrink(BlockCopyTask
*task
,
186 if (new_bytes
== task
->bytes
) {
190 assert(new_bytes
> 0 && new_bytes
< task
->bytes
);
192 task
->s
->in_flight_bytes
-= task
->bytes
- new_bytes
;
193 bdrv_set_dirty_bitmap(task
->s
->copy_bitmap
,
194 task
->offset
+ new_bytes
, task
->bytes
- new_bytes
);
196 task
->bytes
= new_bytes
;
197 qemu_co_queue_restart_all(&task
->wait_queue
);
200 static void coroutine_fn
block_copy_task_end(BlockCopyTask
*task
, int ret
)
202 task
->s
->in_flight_bytes
-= task
->bytes
;
204 bdrv_set_dirty_bitmap(task
->s
->copy_bitmap
, task
->offset
, task
->bytes
);
206 QLIST_REMOVE(task
, list
);
207 qemu_co_queue_restart_all(&task
->wait_queue
);
210 void block_copy_state_free(BlockCopyState
*s
)
216 bdrv_release_dirty_bitmap(s
->copy_bitmap
);
217 shres_destroy(s
->mem
);
221 static uint32_t block_copy_max_transfer(BdrvChild
*source
, BdrvChild
*target
)
223 return MIN_NON_ZERO(INT_MAX
,
224 MIN_NON_ZERO(source
->bs
->bl
.max_transfer
,
225 target
->bs
->bl
.max_transfer
));
228 BlockCopyState
*block_copy_state_new(BdrvChild
*source
, BdrvChild
*target
,
229 int64_t cluster_size
, bool use_copy_range
,
230 BdrvRequestFlags write_flags
, Error
**errp
)
233 BdrvDirtyBitmap
*copy_bitmap
;
235 copy_bitmap
= bdrv_create_dirty_bitmap(source
->bs
, cluster_size
, NULL
,
240 bdrv_disable_dirty_bitmap(copy_bitmap
);
242 s
= g_new(BlockCopyState
, 1);
243 *s
= (BlockCopyState
) {
246 .copy_bitmap
= copy_bitmap
,
247 .cluster_size
= cluster_size
,
248 .len
= bdrv_dirty_bitmap_size(copy_bitmap
),
249 .write_flags
= write_flags
,
250 .mem
= shres_create(BLOCK_COPY_MAX_MEM
),
253 if (block_copy_max_transfer(source
, target
) < cluster_size
) {
255 * copy_range does not respect max_transfer. We don't want to bother
256 * with requests smaller than block-copy cluster size, so fallback to
257 * buffered copying (read and write respect max_transfer on their
260 s
->use_copy_range
= false;
261 s
->copy_size
= cluster_size
;
262 } else if (write_flags
& BDRV_REQ_WRITE_COMPRESSED
) {
263 /* Compression supports only cluster-size writes and no copy-range. */
264 s
->use_copy_range
= false;
265 s
->copy_size
= cluster_size
;
268 * We enable copy-range, but keep small copy_size, until first
269 * successful copy_range (look at block_copy_do_copy).
271 s
->use_copy_range
= use_copy_range
;
272 s
->copy_size
= MAX(s
->cluster_size
, BLOCK_COPY_MAX_BUFFER
);
275 QLIST_INIT(&s
->tasks
);
280 void block_copy_set_progress_callback(
282 ProgressBytesCallbackFunc progress_bytes_callback
,
283 void *progress_opaque
)
285 s
->progress_bytes_callback
= progress_bytes_callback
;
286 s
->progress_opaque
= progress_opaque
;
289 void block_copy_set_progress_meter(BlockCopyState
*s
, ProgressMeter
*pm
)
295 * Takes ownership of @task
297 * If pool is NULL directly run the task, otherwise schedule it into the pool.
299 * Returns: task.func return code if pool is NULL
300 * otherwise -ECANCELED if pool status is bad
301 * otherwise 0 (successfully scheduled)
303 static coroutine_fn
int block_copy_task_run(AioTaskPool
*pool
,
307 int ret
= task
->task
.func(&task
->task
);
313 aio_task_pool_wait_slot(pool
);
314 if (aio_task_pool_status(pool
) < 0) {
315 co_put_to_shres(task
->s
->mem
, task
->bytes
);
316 block_copy_task_end(task
, -ECANCELED
);
321 aio_task_pool_start_task(pool
, &task
->task
);
329 * Do copy of cluster-aligned chunk. Requested region is allowed to exceed
330 * s->len only to cover last cluster when s->len is not aligned to clusters.
332 * No sync here: nor bitmap neighter intersecting requests handling, only copy.
334 * Returns 0 on success.
336 static int coroutine_fn
block_copy_do_copy(BlockCopyState
*s
,
337 int64_t offset
, int64_t bytes
,
338 bool zeroes
, bool *error_is_read
)
341 int64_t nbytes
= MIN(offset
+ bytes
, s
->len
) - offset
;
342 void *bounce_buffer
= NULL
;
344 assert(offset
>= 0 && bytes
> 0 && INT64_MAX
- offset
>= bytes
);
345 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
346 assert(QEMU_IS_ALIGNED(bytes
, s
->cluster_size
));
347 assert(offset
< s
->len
);
348 assert(offset
+ bytes
<= s
->len
||
349 offset
+ bytes
== QEMU_ALIGN_UP(s
->len
, s
->cluster_size
));
350 assert(nbytes
< INT_MAX
);
353 ret
= bdrv_co_pwrite_zeroes(s
->target
, offset
, nbytes
, s
->write_flags
&
354 ~BDRV_REQ_WRITE_COMPRESSED
);
356 trace_block_copy_write_zeroes_fail(s
, offset
, ret
);
357 *error_is_read
= false;
362 if (s
->use_copy_range
) {
363 ret
= bdrv_co_copy_range(s
->source
, offset
, s
->target
, offset
, nbytes
,
366 trace_block_copy_copy_range_fail(s
, offset
, ret
);
367 s
->use_copy_range
= false;
368 s
->copy_size
= MAX(s
->cluster_size
, BLOCK_COPY_MAX_BUFFER
);
369 /* Fallback to read+write with allocated buffer */
371 if (s
->use_copy_range
) {
373 * Successful copy-range. Now increase copy_size. copy_range
374 * does not respect max_transfer (it's a TODO), so we factor
377 * Note: we double-check s->use_copy_range for the case when
378 * parallel block-copy request unsets it during previous
379 * bdrv_co_copy_range call.
382 MIN(MAX(s
->cluster_size
, BLOCK_COPY_MAX_COPY_RANGE
),
383 QEMU_ALIGN_DOWN(block_copy_max_transfer(s
->source
,
392 * In case of failed copy_range request above, we may proceed with buffered
393 * request larger than BLOCK_COPY_MAX_BUFFER. Still, further requests will
394 * be properly limited, so don't care too much. Moreover the most likely
395 * case (copy_range is unsupported for the configuration, so the very first
396 * copy_range request fails) is handled by setting large copy_size only
397 * after first successful copy_range.
400 bounce_buffer
= qemu_blockalign(s
->source
->bs
, nbytes
);
402 ret
= bdrv_co_pread(s
->source
, offset
, nbytes
, bounce_buffer
, 0);
404 trace_block_copy_read_fail(s
, offset
, ret
);
405 *error_is_read
= true;
409 ret
= bdrv_co_pwrite(s
->target
, offset
, nbytes
, bounce_buffer
,
412 trace_block_copy_write_fail(s
, offset
, ret
);
413 *error_is_read
= false;
418 qemu_vfree(bounce_buffer
);
423 static coroutine_fn
int block_copy_task_entry(AioTask
*task
)
425 BlockCopyTask
*t
= container_of(task
, BlockCopyTask
, task
);
426 bool error_is_read
= false;
429 ret
= block_copy_do_copy(t
->s
, t
->offset
, t
->bytes
, t
->zeroes
,
431 if (ret
< 0 && !t
->call_state
->failed
) {
432 t
->call_state
->failed
= true;
433 t
->call_state
->error_is_read
= error_is_read
;
435 progress_work_done(t
->s
->progress
, t
->bytes
);
436 t
->s
->progress_bytes_callback(t
->bytes
, t
->s
->progress_opaque
);
438 co_put_to_shres(t
->s
->mem
, t
->bytes
);
439 block_copy_task_end(t
, ret
);
444 static int block_copy_block_status(BlockCopyState
*s
, int64_t offset
,
445 int64_t bytes
, int64_t *pnum
)
448 BlockDriverState
*base
;
451 if (s
->skip_unallocated
) {
452 base
= bdrv_backing_chain_next(s
->source
->bs
);
457 ret
= bdrv_block_status_above(s
->source
->bs
, base
, offset
, bytes
, &num
,
459 if (ret
< 0 || num
< s
->cluster_size
) {
461 * On error or if failed to obtain large enough chunk just fallback to
464 num
= s
->cluster_size
;
465 ret
= BDRV_BLOCK_ALLOCATED
| BDRV_BLOCK_DATA
;
466 } else if (offset
+ num
== s
->len
) {
467 num
= QEMU_ALIGN_UP(num
, s
->cluster_size
);
469 num
= QEMU_ALIGN_DOWN(num
, s
->cluster_size
);
477 * Check if the cluster starting at offset is allocated or not.
478 * return via pnum the number of contiguous clusters sharing this allocation.
480 static int block_copy_is_cluster_allocated(BlockCopyState
*s
, int64_t offset
,
483 BlockDriverState
*bs
= s
->source
->bs
;
484 int64_t count
, total_count
= 0;
485 int64_t bytes
= s
->len
- offset
;
488 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
491 ret
= bdrv_is_allocated(bs
, offset
, bytes
, &count
);
496 total_count
+= count
;
498 if (ret
|| count
== 0) {
500 * ret: partial segment(s) are considered allocated.
501 * otherwise: unallocated tail is treated as an entire segment.
503 *pnum
= DIV_ROUND_UP(total_count
, s
->cluster_size
);
507 /* Unallocated segment(s) with uncertain following segment(s) */
508 if (total_count
>= s
->cluster_size
) {
509 *pnum
= total_count
/ s
->cluster_size
;
519 * Reset bits in copy_bitmap starting at offset if they represent unallocated
520 * data in the image. May reset subsequent contiguous bits.
521 * @return 0 when the cluster at @offset was unallocated,
522 * 1 otherwise, and -ret on error.
524 int64_t block_copy_reset_unallocated(BlockCopyState
*s
,
525 int64_t offset
, int64_t *count
)
528 int64_t clusters
, bytes
;
530 ret
= block_copy_is_cluster_allocated(s
, offset
, &clusters
);
535 bytes
= clusters
* s
->cluster_size
;
538 bdrv_reset_dirty_bitmap(s
->copy_bitmap
, offset
, bytes
);
539 progress_set_remaining(s
->progress
,
540 bdrv_get_dirty_count(s
->copy_bitmap
) +
549 * block_copy_dirty_clusters
551 * Copy dirty clusters in @offset/@bytes range.
552 * Returns 1 if dirty clusters found and successfully copied, 0 if no dirty
553 * clusters found and -errno on failure.
555 static int coroutine_fn
556 block_copy_dirty_clusters(BlockCopyCallState
*call_state
)
558 BlockCopyState
*s
= call_state
->s
;
559 int64_t offset
= call_state
->offset
;
560 int64_t bytes
= call_state
->bytes
;
563 bool found_dirty
= false;
564 int64_t end
= offset
+ bytes
;
565 AioTaskPool
*aio
= NULL
;
568 * block_copy() user is responsible for keeping source and target in same
571 assert(bdrv_get_aio_context(s
->source
->bs
) ==
572 bdrv_get_aio_context(s
->target
->bs
));
574 assert(QEMU_IS_ALIGNED(offset
, s
->cluster_size
));
575 assert(QEMU_IS_ALIGNED(bytes
, s
->cluster_size
));
577 while (bytes
&& aio_task_pool_status(aio
) == 0) {
579 int64_t status_bytes
;
581 task
= block_copy_task_create(s
, call_state
, offset
, bytes
);
583 /* No more dirty bits in the bitmap */
584 trace_block_copy_skip_range(s
, offset
, bytes
);
587 if (task
->offset
> offset
) {
588 trace_block_copy_skip_range(s
, offset
, task
->offset
- offset
);
593 ret
= block_copy_block_status(s
, task
->offset
, task
->bytes
,
595 assert(ret
>= 0); /* never fail */
596 if (status_bytes
< task
->bytes
) {
597 block_copy_task_shrink(task
, status_bytes
);
599 if (s
->skip_unallocated
&& !(ret
& BDRV_BLOCK_ALLOCATED
)) {
600 block_copy_task_end(task
, 0);
601 progress_set_remaining(s
->progress
,
602 bdrv_get_dirty_count(s
->copy_bitmap
) +
604 trace_block_copy_skip_range(s
, task
->offset
, task
->bytes
);
605 offset
= task_end(task
);
606 bytes
= end
- offset
;
610 task
->zeroes
= ret
& BDRV_BLOCK_ZERO
;
612 trace_block_copy_process(s
, task
->offset
);
614 co_get_from_shres(s
->mem
, task
->bytes
);
616 offset
= task_end(task
);
617 bytes
= end
- offset
;
620 aio
= aio_task_pool_new(BLOCK_COPY_MAX_WORKERS
);
623 ret
= block_copy_task_run(aio
, task
);
631 aio_task_pool_wait_all(aio
);
634 * We are not really interested in -ECANCELED returned from
635 * block_copy_task_run. If it fails, it means some task already failed
636 * for real reason, let's return first failure.
637 * Still, assert that we don't rewrite failure by success.
639 * Note: ret may be positive here because of block-status result.
641 assert(ret
>= 0 || aio_task_pool_status(aio
) < 0);
642 ret
= aio_task_pool_status(aio
);
644 aio_task_pool_free(aio
);
647 return ret
< 0 ? ret
: found_dirty
;
653 * Copy requested region, accordingly to dirty bitmap.
654 * Collaborate with parallel block_copy requests: if they succeed it will help
655 * us. If they fail, we will retry not-copied regions. So, if we return error,
656 * it means that some I/O operation failed in context of _this_ block_copy call,
657 * not some parallel operation.
659 static int coroutine_fn
block_copy_common(BlockCopyCallState
*call_state
)
664 ret
= block_copy_dirty_clusters(call_state
);
667 ret
= block_copy_wait_one(call_state
->s
, call_state
->offset
,
672 * We retry in two cases:
673 * 1. Some progress done
674 * Something was copied, which means that there were yield points
675 * and some new dirty bits may have appeared (due to failed parallel
676 * block-copy requests).
677 * 2. We have waited for some intersecting block-copy request
678 * It may have failed and produced new dirty bits.
685 int coroutine_fn
block_copy(BlockCopyState
*s
, int64_t start
, int64_t bytes
,
688 BlockCopyCallState call_state
= {
694 int ret
= block_copy_common(&call_state
);
696 if (error_is_read
&& ret
< 0) {
697 *error_is_read
= call_state
.error_is_read
;
703 BdrvDirtyBitmap
*block_copy_dirty_bitmap(BlockCopyState
*s
)
705 return s
->copy_bitmap
;
708 void block_copy_set_skip_unallocated(BlockCopyState
*s
, bool skip
)
710 s
->skip_unallocated
= skip
;