4 * Copyright Red Hat, Inc. 2012
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
15 #include "block/blockjob.h"
16 #include "block/block_int.h"
17 #include "qemu/ratelimit.h"
18 #include "qemu/bitmap.h"
20 #define SLICE_TIME 100000000ULL /* ns */
21 #define MAX_IN_FLIGHT 16
23 /* The mirroring buffer is a list of granularity-sized chunks.
24 * Free chunks are organized in a list.
26 typedef struct MirrorBuffer
{
27 QSIMPLEQ_ENTRY(MirrorBuffer
) next
;
30 typedef struct MirrorBlockJob
{
33 BlockDriverState
*target
;
34 BlockDriverState
*base
;
36 BlockdevOnError on_source_error
, on_target_error
;
42 unsigned long *cow_bitmap
;
43 BdrvDirtyBitmap
*dirty_bitmap
;
46 QSIMPLEQ_HEAD(, MirrorBuffer
) buf_free
;
49 unsigned long *in_flight_bitmap
;
54 typedef struct MirrorOp
{
61 static BlockErrorAction
mirror_error_action(MirrorBlockJob
*s
, bool read
,
66 return block_job_error_action(&s
->common
, s
->common
.bs
,
67 s
->on_source_error
, true, error
);
69 return block_job_error_action(&s
->common
, s
->target
,
70 s
->on_target_error
, false, error
);
74 static void mirror_iteration_done(MirrorOp
*op
, int ret
)
76 MirrorBlockJob
*s
= op
->s
;
79 int i
, nb_chunks
, sectors_per_chunk
;
81 trace_mirror_iteration_done(s
, op
->sector_num
, op
->nb_sectors
, ret
);
85 for (i
= 0; i
< op
->qiov
.niov
; i
++) {
86 MirrorBuffer
*buf
= (MirrorBuffer
*) iov
[i
].iov_base
;
87 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, buf
, next
);
91 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
92 chunk_num
= op
->sector_num
/ sectors_per_chunk
;
93 nb_chunks
= op
->nb_sectors
/ sectors_per_chunk
;
94 bitmap_clear(s
->in_flight_bitmap
, chunk_num
, nb_chunks
);
95 if (s
->cow_bitmap
&& ret
>= 0) {
96 bitmap_set(s
->cow_bitmap
, chunk_num
, nb_chunks
);
99 qemu_iovec_destroy(&op
->qiov
);
100 g_slice_free(MirrorOp
, op
);
102 /* Enter coroutine when it is not sleeping. The coroutine sleeps to
103 * rate-limit itself. The coroutine will eventually resume since there is
104 * a sleep timeout so don't wake it early.
106 if (s
->common
.busy
) {
107 qemu_coroutine_enter(s
->common
.co
, NULL
);
111 static void mirror_write_complete(void *opaque
, int ret
)
113 MirrorOp
*op
= opaque
;
114 MirrorBlockJob
*s
= op
->s
;
116 BlockDriverState
*source
= s
->common
.bs
;
117 BlockErrorAction action
;
119 bdrv_set_dirty(source
, op
->sector_num
, op
->nb_sectors
);
120 action
= mirror_error_action(s
, false, -ret
);
121 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
125 mirror_iteration_done(op
, ret
);
128 static void mirror_read_complete(void *opaque
, int ret
)
130 MirrorOp
*op
= opaque
;
131 MirrorBlockJob
*s
= op
->s
;
133 BlockDriverState
*source
= s
->common
.bs
;
134 BlockErrorAction action
;
136 bdrv_set_dirty(source
, op
->sector_num
, op
->nb_sectors
);
137 action
= mirror_error_action(s
, true, -ret
);
138 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
142 mirror_iteration_done(op
, ret
);
145 bdrv_aio_writev(s
->target
, op
->sector_num
, &op
->qiov
, op
->nb_sectors
,
146 mirror_write_complete
, op
);
149 static uint64_t coroutine_fn
mirror_iteration(MirrorBlockJob
*s
)
151 BlockDriverState
*source
= s
->common
.bs
;
152 int nb_sectors
, sectors_per_chunk
, nb_chunks
;
153 int64_t end
, sector_num
, next_chunk
, next_sector
, hbitmap_next_sector
;
157 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
158 if (s
->sector_num
< 0) {
159 bdrv_dirty_iter_init(source
, s
->dirty_bitmap
, &s
->hbi
);
160 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
161 trace_mirror_restart_iter(s
,
162 bdrv_get_dirty_count(source
, s
->dirty_bitmap
));
163 assert(s
->sector_num
>= 0);
166 hbitmap_next_sector
= s
->sector_num
;
167 sector_num
= s
->sector_num
;
168 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
169 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
171 /* Extend the QEMUIOVector to include all adjacent blocks that will
172 * be copied in this operation.
174 * We have to do this if we have no backing file yet in the destination,
175 * and the cluster size is very large. Then we need to do COW ourselves.
176 * The first time a cluster is copied, copy it entirely. Note that,
177 * because both the granularity and the cluster size are powers of two,
178 * the number of sectors to copy cannot exceed one cluster.
180 * We also want to extend the QEMUIOVector to include more adjacent
181 * dirty blocks if possible, to limit the number of I/O operations and
182 * run efficiently even with a small granularity.
186 next_sector
= sector_num
;
187 next_chunk
= sector_num
/ sectors_per_chunk
;
189 /* Wait for I/O to this cluster (from a previous iteration) to be done. */
190 while (test_bit(next_chunk
, s
->in_flight_bitmap
)) {
191 trace_mirror_yield_in_flight(s
, sector_num
, s
->in_flight
);
192 qemu_coroutine_yield();
196 int added_sectors
, added_chunks
;
198 if (!bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
) ||
199 test_bit(next_chunk
, s
->in_flight_bitmap
)) {
200 assert(nb_sectors
> 0);
204 added_sectors
= sectors_per_chunk
;
205 if (s
->cow_bitmap
&& !test_bit(next_chunk
, s
->cow_bitmap
)) {
206 bdrv_round_to_clusters(s
->target
,
207 next_sector
, added_sectors
,
208 &next_sector
, &added_sectors
);
210 /* On the first iteration, the rounding may make us copy
211 * sectors before the first dirty one.
213 if (next_sector
< sector_num
) {
214 assert(nb_sectors
== 0);
215 sector_num
= next_sector
;
216 next_chunk
= next_sector
/ sectors_per_chunk
;
220 added_sectors
= MIN(added_sectors
, end
- (sector_num
+ nb_sectors
));
221 added_chunks
= (added_sectors
+ sectors_per_chunk
- 1) / sectors_per_chunk
;
223 /* When doing COW, it may happen that there is not enough space for
224 * a full cluster. Wait if that is the case.
226 while (nb_chunks
== 0 && s
->buf_free_count
< added_chunks
) {
227 trace_mirror_yield_buf_busy(s
, nb_chunks
, s
->in_flight
);
228 qemu_coroutine_yield();
230 if (s
->buf_free_count
< nb_chunks
+ added_chunks
) {
231 trace_mirror_break_buf_busy(s
, nb_chunks
, s
->in_flight
);
235 /* We have enough free space to copy these sectors. */
236 bitmap_set(s
->in_flight_bitmap
, next_chunk
, added_chunks
);
238 nb_sectors
+= added_sectors
;
239 nb_chunks
+= added_chunks
;
240 next_sector
+= added_sectors
;
241 next_chunk
+= added_chunks
;
242 if (!s
->synced
&& s
->common
.speed
) {
243 delay_ns
= ratelimit_calculate_delay(&s
->limit
, added_sectors
);
247 } while (delay_ns
== 0 && next_sector
< end
);
249 /* Allocate a MirrorOp that is used as an AIO callback. */
250 op
= g_slice_new(MirrorOp
);
252 op
->sector_num
= sector_num
;
253 op
->nb_sectors
= nb_sectors
;
255 /* Now make a QEMUIOVector taking enough granularity-sized chunks
258 qemu_iovec_init(&op
->qiov
, nb_chunks
);
259 next_sector
= sector_num
;
260 while (nb_chunks
-- > 0) {
261 MirrorBuffer
*buf
= QSIMPLEQ_FIRST(&s
->buf_free
);
262 QSIMPLEQ_REMOVE_HEAD(&s
->buf_free
, next
);
264 qemu_iovec_add(&op
->qiov
, buf
, s
->granularity
);
266 /* Advance the HBitmapIter in parallel, so that we do not examine
267 * the same sector twice.
269 if (next_sector
> hbitmap_next_sector
270 && bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
)) {
271 hbitmap_next_sector
= hbitmap_iter_next(&s
->hbi
);
274 next_sector
+= sectors_per_chunk
;
277 bdrv_reset_dirty(source
, sector_num
, nb_sectors
);
279 /* Copy the dirty cluster. */
281 trace_mirror_one_iteration(s
, sector_num
, nb_sectors
);
282 bdrv_aio_readv(source
, sector_num
, &op
->qiov
, nb_sectors
,
283 mirror_read_complete
, op
);
287 static void mirror_free_init(MirrorBlockJob
*s
)
289 int granularity
= s
->granularity
;
290 size_t buf_size
= s
->buf_size
;
291 uint8_t *buf
= s
->buf
;
293 assert(s
->buf_free_count
== 0);
294 QSIMPLEQ_INIT(&s
->buf_free
);
295 while (buf_size
!= 0) {
296 MirrorBuffer
*cur
= (MirrorBuffer
*)buf
;
297 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, cur
, next
);
299 buf_size
-= granularity
;
304 static void mirror_drain(MirrorBlockJob
*s
)
306 while (s
->in_flight
> 0) {
307 qemu_coroutine_yield();
311 static void coroutine_fn
mirror_run(void *opaque
)
313 MirrorBlockJob
*s
= opaque
;
314 BlockDriverState
*bs
= s
->common
.bs
;
315 int64_t sector_num
, end
, sectors_per_chunk
, length
;
316 uint64_t last_pause_ns
;
318 char backing_filename
[1024];
322 if (block_job_is_cancelled(&s
->common
)) {
326 s
->common
.len
= bdrv_getlength(bs
);
327 if (s
->common
.len
<= 0) {
332 length
= DIV_ROUND_UP(s
->common
.len
, s
->granularity
);
333 s
->in_flight_bitmap
= bitmap_new(length
);
335 /* If we have no backing file yet in the destination, we cannot let
336 * the destination do COW. Instead, we copy sectors around the
337 * dirty data if needed. We need a bitmap to do that.
339 bdrv_get_backing_filename(s
->target
, backing_filename
,
340 sizeof(backing_filename
));
341 if (backing_filename
[0] && !s
->target
->backing_hd
) {
342 ret
= bdrv_get_info(s
->target
, &bdi
);
346 if (s
->granularity
< bdi
.cluster_size
) {
347 s
->buf_size
= MAX(s
->buf_size
, bdi
.cluster_size
);
348 s
->cow_bitmap
= bitmap_new(length
);
352 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
353 s
->buf
= qemu_blockalign(bs
, s
->buf_size
);
354 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
357 if (!s
->is_none_mode
) {
358 /* First part, loop on the sectors and initialize the dirty bitmap. */
359 BlockDriverState
*base
= s
->base
;
360 for (sector_num
= 0; sector_num
< end
; ) {
361 int64_t next
= (sector_num
| (sectors_per_chunk
- 1)) + 1;
362 ret
= bdrv_is_allocated_above(bs
, base
,
363 sector_num
, next
- sector_num
, &n
);
371 bdrv_set_dirty(bs
, sector_num
, n
);
379 bdrv_dirty_iter_init(bs
, s
->dirty_bitmap
, &s
->hbi
);
380 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
382 uint64_t delay_ns
= 0;
384 bool should_complete
;
391 cnt
= bdrv_get_dirty_count(bs
, s
->dirty_bitmap
);
393 /* Note that even when no rate limit is applied we need to yield
394 * periodically with no pending I/O so that qemu_aio_flush() returns.
395 * We do so every SLICE_TIME nanoseconds, or when there is an error,
396 * or when the source is clean, whichever comes first.
398 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - last_pause_ns
< SLICE_TIME
&&
399 s
->common
.iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
400 if (s
->in_flight
== MAX_IN_FLIGHT
|| s
->buf_free_count
== 0 ||
401 (cnt
== 0 && s
->in_flight
> 0)) {
402 trace_mirror_yield(s
, s
->in_flight
, s
->buf_free_count
, cnt
);
403 qemu_coroutine_yield();
405 } else if (cnt
!= 0) {
406 delay_ns
= mirror_iteration(s
);
413 should_complete
= false;
414 if (s
->in_flight
== 0 && cnt
== 0) {
415 trace_mirror_before_flush(s
);
416 ret
= bdrv_flush(s
->target
);
418 if (mirror_error_action(s
, false, -ret
) ==
419 BLOCK_ERROR_ACTION_REPORT
) {
423 /* We're out of the streaming phase. From now on, if the job
424 * is cancelled we will actually complete all pending I/O and
425 * report completion. This way, block-job-cancel will leave
426 * the target in a consistent state.
428 s
->common
.offset
= end
* BDRV_SECTOR_SIZE
;
430 block_job_event_ready(&s
->common
);
434 should_complete
= s
->should_complete
||
435 block_job_is_cancelled(&s
->common
);
436 cnt
= bdrv_get_dirty_count(bs
, s
->dirty_bitmap
);
440 if (cnt
== 0 && should_complete
) {
441 /* The dirty bitmap is not updated while operations are pending.
442 * If we're about to exit, wait for pending operations before
443 * calling bdrv_get_dirty_count(bs), or we may exit while the
444 * source has dirty data to copy!
446 * Note that I/O can be submitted by the guest while
447 * mirror_populate runs.
449 trace_mirror_before_drain(s
, cnt
);
451 cnt
= bdrv_get_dirty_count(bs
, s
->dirty_bitmap
);
455 trace_mirror_before_sleep(s
, cnt
, s
->synced
, delay_ns
);
457 /* Publish progress */
458 s
->common
.offset
= (end
- cnt
) * BDRV_SECTOR_SIZE
;
459 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
460 if (block_job_is_cancelled(&s
->common
)) {
463 } else if (!should_complete
) {
464 delay_ns
= (s
->in_flight
== 0 && cnt
== 0 ? SLICE_TIME
: 0);
465 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
466 } else if (cnt
== 0) {
467 /* The two disks are in sync. Exit and report successful
470 assert(QLIST_EMPTY(&bs
->tracked_requests
));
471 s
->common
.cancelled
= false;
474 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
478 if (s
->in_flight
> 0) {
479 /* We get here only if something went wrong. Either the job failed,
480 * or it was cancelled prematurely so that we do not guarantee that
481 * the target is a copy of the source.
483 assert(ret
< 0 || (!s
->synced
&& block_job_is_cancelled(&s
->common
)));
487 assert(s
->in_flight
== 0);
489 g_free(s
->cow_bitmap
);
490 g_free(s
->in_flight_bitmap
);
491 bdrv_release_dirty_bitmap(bs
, s
->dirty_bitmap
);
492 bdrv_iostatus_disable(s
->target
);
493 if (s
->should_complete
&& ret
== 0) {
494 if (bdrv_get_flags(s
->target
) != bdrv_get_flags(s
->common
.bs
)) {
495 bdrv_reopen(s
->target
, bdrv_get_flags(s
->common
.bs
), NULL
);
497 bdrv_swap(s
->target
, s
->common
.bs
);
498 if (s
->common
.driver
->job_type
== BLOCK_JOB_TYPE_COMMIT
) {
499 /* drop the bs loop chain formed by the swap: break the loop then
500 * trigger the unref from the top one */
501 BlockDriverState
*p
= s
->base
->backing_hd
;
502 bdrv_set_backing_hd(s
->base
, NULL
);
506 bdrv_unref(s
->target
);
507 block_job_completed(&s
->common
, ret
);
510 static void mirror_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
512 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
515 error_set(errp
, QERR_INVALID_PARAMETER
, "speed");
518 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
521 static void mirror_iostatus_reset(BlockJob
*job
)
523 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
525 bdrv_iostatus_reset(s
->target
);
528 static void mirror_complete(BlockJob
*job
, Error
**errp
)
530 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
531 Error
*local_err
= NULL
;
534 ret
= bdrv_open_backing_file(s
->target
, NULL
, &local_err
);
536 error_propagate(errp
, local_err
);
540 error_set(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->bs
->device_name
);
544 s
->should_complete
= true;
545 block_job_resume(job
);
548 static const BlockJobDriver mirror_job_driver
= {
549 .instance_size
= sizeof(MirrorBlockJob
),
550 .job_type
= BLOCK_JOB_TYPE_MIRROR
,
551 .set_speed
= mirror_set_speed
,
552 .iostatus_reset
= mirror_iostatus_reset
,
553 .complete
= mirror_complete
,
556 static const BlockJobDriver commit_active_job_driver
= {
557 .instance_size
= sizeof(MirrorBlockJob
),
558 .job_type
= BLOCK_JOB_TYPE_COMMIT
,
559 .set_speed
= mirror_set_speed
,
561 = mirror_iostatus_reset
,
562 .complete
= mirror_complete
,
565 static void mirror_start_job(BlockDriverState
*bs
, BlockDriverState
*target
,
566 int64_t speed
, int64_t granularity
,
568 BlockdevOnError on_source_error
,
569 BlockdevOnError on_target_error
,
570 BlockDriverCompletionFunc
*cb
,
571 void *opaque
, Error
**errp
,
572 const BlockJobDriver
*driver
,
573 bool is_none_mode
, BlockDriverState
*base
)
577 if (granularity
== 0) {
578 /* Choose the default granularity based on the target file's cluster
579 * size, clamped between 4k and 64k. */
581 if (bdrv_get_info(target
, &bdi
) >= 0 && bdi
.cluster_size
!= 0) {
582 granularity
= MAX(4096, bdi
.cluster_size
);
583 granularity
= MIN(65536, granularity
);
589 assert ((granularity
& (granularity
- 1)) == 0);
591 if ((on_source_error
== BLOCKDEV_ON_ERROR_STOP
||
592 on_source_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
593 !bdrv_iostatus_is_enabled(bs
)) {
594 error_set(errp
, QERR_INVALID_PARAMETER
, "on-source-error");
599 s
= block_job_create(driver
, bs
, speed
, cb
, opaque
, errp
);
604 s
->on_source_error
= on_source_error
;
605 s
->on_target_error
= on_target_error
;
607 s
->is_none_mode
= is_none_mode
;
609 s
->granularity
= granularity
;
610 s
->buf_size
= MAX(buf_size
, granularity
);
612 s
->dirty_bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, errp
);
613 if (!s
->dirty_bitmap
) {
616 bdrv_set_enable_write_cache(s
->target
, true);
617 bdrv_set_on_error(s
->target
, on_target_error
, on_target_error
);
618 bdrv_iostatus_enable(s
->target
);
619 s
->common
.co
= qemu_coroutine_create(mirror_run
);
620 trace_mirror_start(bs
, s
, s
->common
.co
, opaque
);
621 qemu_coroutine_enter(s
->common
.co
, s
);
624 void mirror_start(BlockDriverState
*bs
, BlockDriverState
*target
,
625 int64_t speed
, int64_t granularity
, int64_t buf_size
,
626 MirrorSyncMode mode
, BlockdevOnError on_source_error
,
627 BlockdevOnError on_target_error
,
628 BlockDriverCompletionFunc
*cb
,
629 void *opaque
, Error
**errp
)
632 BlockDriverState
*base
;
634 is_none_mode
= mode
== MIRROR_SYNC_MODE_NONE
;
635 base
= mode
== MIRROR_SYNC_MODE_TOP
? bs
->backing_hd
: NULL
;
636 mirror_start_job(bs
, target
, speed
, granularity
, buf_size
,
637 on_source_error
, on_target_error
, cb
, opaque
, errp
,
638 &mirror_job_driver
, is_none_mode
, base
);
641 void commit_active_start(BlockDriverState
*bs
, BlockDriverState
*base
,
643 BlockdevOnError on_error
,
644 BlockDriverCompletionFunc
*cb
,
645 void *opaque
, Error
**errp
)
647 int64_t length
, base_length
;
650 Error
*local_err
= NULL
;
652 orig_base_flags
= bdrv_get_flags(base
);
654 if (bdrv_reopen(base
, bs
->open_flags
, errp
)) {
658 length
= bdrv_getlength(bs
);
660 error_setg_errno(errp
, -length
,
661 "Unable to determine length of %s", bs
->filename
);
662 goto error_restore_flags
;
665 base_length
= bdrv_getlength(base
);
666 if (base_length
< 0) {
667 error_setg_errno(errp
, -base_length
,
668 "Unable to determine length of %s", base
->filename
);
669 goto error_restore_flags
;
672 if (length
> base_length
) {
673 ret
= bdrv_truncate(base
, length
);
675 error_setg_errno(errp
, -ret
,
676 "Top image %s is larger than base image %s, and "
677 "resize of base image failed",
678 bs
->filename
, base
->filename
);
679 goto error_restore_flags
;
684 mirror_start_job(bs
, base
, speed
, 0, 0,
685 on_error
, on_error
, cb
, opaque
, &local_err
,
686 &commit_active_job_driver
, false, base
);
688 error_propagate(errp
, local_err
);
689 goto error_restore_flags
;
695 /* ignore error and errp for bdrv_reopen, because we want to propagate
696 * the original error */
697 bdrv_reopen(base
, orig_base_flags
, NULL
);