block/block-copy: More explicit call_state
[qemu/ar7.git] / block / block-copy.c
blob6ea55f1f9a7d59feead541d649b5d547c663171a
1 /*
2 * block_copy API
4 * Copyright (C) 2013 Proxmox Server Solutions
5 * Copyright (c) 2019 Virtuozzo International GmbH.
7 * Authors:
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"
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"
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 {
33 /* IN parameters */
34 BlockCopyState *s;
35 int64_t offset;
36 int64_t bytes;
38 /* State */
39 bool failed;
41 /* OUT parameters */
42 bool error_is_read;
43 } BlockCopyCallState;
45 typedef struct BlockCopyTask {
46 AioTask task;
48 BlockCopyState *s;
49 BlockCopyCallState *call_state;
50 int64_t offset;
51 int64_t bytes;
52 bool zeroes;
53 QLIST_ENTRY(BlockCopyTask) list;
54 CoQueue wait_queue; /* coroutines blocked on this task */
55 } BlockCopyTask;
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.
68 BdrvChild *source;
69 BdrvChild *target;
70 BdrvDirtyBitmap *copy_bitmap;
71 int64_t in_flight_bytes;
72 int64_t cluster_size;
73 bool use_copy_range;
74 int64_t copy_size;
75 uint64_t len;
76 QLIST_HEAD(, BlockCopyTask) tasks;
78 BdrvRequestFlags write_flags;
81 * skip_unallocated:
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;
101 SharedResource *mem;
102 } BlockCopyState;
104 static BlockCopyTask *find_conflicting_task(BlockCopyState *s,
105 int64_t offset, int64_t bytes)
107 BlockCopyTask *t;
109 QLIST_FOREACH(t, &s->tasks, list) {
110 if (offset + bytes > t->offset && offset < t->offset + t->bytes) {
111 return t;
115 return NULL;
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,
123 int64_t bytes)
125 BlockCopyTask *task = find_conflicting_task(s, offset, bytes);
127 if (!task) {
128 return false;
131 qemu_co_queue_wait(&task->wait_queue, NULL);
133 return true;
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)
144 BlockCopyTask *task;
146 if (!bdrv_dirty_bitmap_next_dirty_area(s->copy_bitmap,
147 offset, offset + bytes,
148 s->copy_size, &offset, &bytes))
150 return NULL;
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,
165 .s = s,
166 .call_state = call_state,
167 .offset = offset,
168 .bytes = bytes,
170 qemu_co_queue_init(&task->wait_queue);
171 QLIST_INSERT_HEAD(&s->tasks, task, list);
173 return task;
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
181 * with shrunk task)
183 static void coroutine_fn block_copy_task_shrink(BlockCopyTask *task,
184 int64_t new_bytes)
186 if (new_bytes == task->bytes) {
187 return;
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;
203 if (ret < 0) {
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)
212 if (!s) {
213 return;
216 bdrv_release_dirty_bitmap(s->copy_bitmap);
217 shres_destroy(s->mem);
218 g_free(s);
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)
232 BlockCopyState *s;
233 BdrvDirtyBitmap *copy_bitmap;
235 copy_bitmap = bdrv_create_dirty_bitmap(source->bs, cluster_size, NULL,
236 errp);
237 if (!copy_bitmap) {
238 return NULL;
240 bdrv_disable_dirty_bitmap(copy_bitmap);
242 s = g_new(BlockCopyState, 1);
243 *s = (BlockCopyState) {
244 .source = source,
245 .target = target,
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
258 * behalf).
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;
266 } else {
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);
277 return s;
280 void block_copy_set_progress_callback(
281 BlockCopyState *s,
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)
291 s->progress = 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,
304 BlockCopyTask *task)
306 if (!pool) {
307 int ret = task->task.func(&task->task);
309 g_free(task);
310 return ret;
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);
317 g_free(task);
318 return -ECANCELED;
321 aio_task_pool_start_task(pool, &task->task);
323 return 0;
327 * block_copy_do_copy
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)
340 int ret;
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);
352 if (zeroes) {
353 ret = bdrv_co_pwrite_zeroes(s->target, offset, nbytes, s->write_flags &
354 ~BDRV_REQ_WRITE_COMPRESSED);
355 if (ret < 0) {
356 trace_block_copy_write_zeroes_fail(s, offset, ret);
357 *error_is_read = false;
359 return ret;
362 if (s->use_copy_range) {
363 ret = bdrv_co_copy_range(s->source, offset, s->target, offset, nbytes,
364 0, s->write_flags);
365 if (ret < 0) {
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 */
370 } else {
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
375 * that in here.
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.
381 s->copy_size =
382 MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_COPY_RANGE),
383 QEMU_ALIGN_DOWN(block_copy_max_transfer(s->source,
384 s->target),
385 s->cluster_size));
387 goto out;
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);
403 if (ret < 0) {
404 trace_block_copy_read_fail(s, offset, ret);
405 *error_is_read = true;
406 goto out;
409 ret = bdrv_co_pwrite(s->target, offset, nbytes, bounce_buffer,
410 s->write_flags);
411 if (ret < 0) {
412 trace_block_copy_write_fail(s, offset, ret);
413 *error_is_read = false;
414 goto out;
417 out:
418 qemu_vfree(bounce_buffer);
420 return ret;
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;
427 int ret;
429 ret = block_copy_do_copy(t->s, t->offset, t->bytes, t->zeroes,
430 &error_is_read);
431 if (ret < 0 && !t->call_state->failed) {
432 t->call_state->failed = true;
433 t->call_state->error_is_read = error_is_read;
434 } else {
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);
441 return ret;
444 static int block_copy_block_status(BlockCopyState *s, int64_t offset,
445 int64_t bytes, int64_t *pnum)
447 int64_t num;
448 BlockDriverState *base;
449 int ret;
451 if (s->skip_unallocated) {
452 base = bdrv_backing_chain_next(s->source->bs);
453 } else {
454 base = NULL;
457 ret = bdrv_block_status_above(s->source->bs, base, offset, bytes, &num,
458 NULL, NULL);
459 if (ret < 0 || num < s->cluster_size) {
461 * On error or if failed to obtain large enough chunk just fallback to
462 * copy one cluster.
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);
468 } else {
469 num = QEMU_ALIGN_DOWN(num, s->cluster_size);
472 *pnum = num;
473 return ret;
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,
481 int64_t *pnum)
483 BlockDriverState *bs = s->source->bs;
484 int64_t count, total_count = 0;
485 int64_t bytes = s->len - offset;
486 int ret;
488 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
490 while (true) {
491 ret = bdrv_is_allocated(bs, offset, bytes, &count);
492 if (ret < 0) {
493 return ret;
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);
504 return ret;
507 /* Unallocated segment(s) with uncertain following segment(s) */
508 if (total_count >= s->cluster_size) {
509 *pnum = total_count / s->cluster_size;
510 return 0;
513 offset += count;
514 bytes -= count;
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)
527 int ret;
528 int64_t clusters, bytes;
530 ret = block_copy_is_cluster_allocated(s, offset, &clusters);
531 if (ret < 0) {
532 return ret;
535 bytes = clusters * s->cluster_size;
537 if (!ret) {
538 bdrv_reset_dirty_bitmap(s->copy_bitmap, offset, bytes);
539 progress_set_remaining(s->progress,
540 bdrv_get_dirty_count(s->copy_bitmap) +
541 s->in_flight_bytes);
544 *count = bytes;
545 return ret;
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;
562 int ret = 0;
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
569 * aio context
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) {
578 BlockCopyTask *task;
579 int64_t status_bytes;
581 task = block_copy_task_create(s, call_state, offset, bytes);
582 if (!task) {
583 /* No more dirty bits in the bitmap */
584 trace_block_copy_skip_range(s, offset, bytes);
585 break;
587 if (task->offset > offset) {
588 trace_block_copy_skip_range(s, offset, task->offset - offset);
591 found_dirty = true;
593 ret = block_copy_block_status(s, task->offset, task->bytes,
594 &status_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) +
603 s->in_flight_bytes);
604 trace_block_copy_skip_range(s, task->offset, task->bytes);
605 offset = task_end(task);
606 bytes = end - offset;
607 g_free(task);
608 continue;
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;
619 if (!aio && bytes) {
620 aio = aio_task_pool_new(BLOCK_COPY_MAX_WORKERS);
623 ret = block_copy_task_run(aio, task);
624 if (ret < 0) {
625 goto out;
629 out:
630 if (aio) {
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;
651 * block_copy_common
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)
661 int ret;
663 do {
664 ret = block_copy_dirty_clusters(call_state);
666 if (ret == 0) {
667 ret = block_copy_wait_one(call_state->s, call_state->offset,
668 call_state->bytes);
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.
680 } while (ret > 0);
682 return ret;
685 int coroutine_fn block_copy(BlockCopyState *s, int64_t start, int64_t bytes,
686 bool *error_is_read)
688 BlockCopyCallState call_state = {
689 .s = s,
690 .offset = start,
691 .bytes = bytes,
694 int ret = block_copy_common(&call_state);
696 if (error_is_read && ret < 0) {
697 *error_is_read = call_state.error_is_read;
700 return ret;
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;