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