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"
21 * Size of data buffer for populating the image file. This should be large
22 * enough to process multiple clusters in a single call, so that populating
23 * contiguous regions of the image is efficient.
25 BLOCK_SIZE
= 512 * BDRV_SECTORS_PER_DIRTY_CHUNK
, /* in bytes */
28 #define SLICE_TIME 100000000ULL /* ns */
30 typedef struct MirrorBlockJob
{
33 BlockDriverState
*target
;
35 BlockdevOnError on_source_error
, on_target_error
;
43 static BlockErrorAction
mirror_error_action(MirrorBlockJob
*s
, bool read
,
48 return block_job_error_action(&s
->common
, s
->common
.bs
,
49 s
->on_source_error
, true, error
);
51 return block_job_error_action(&s
->common
, s
->target
,
52 s
->on_target_error
, false, error
);
56 static int coroutine_fn
mirror_iteration(MirrorBlockJob
*s
,
57 BlockErrorAction
*p_action
)
59 BlockDriverState
*source
= s
->common
.bs
;
60 BlockDriverState
*target
= s
->target
;
66 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
67 if (s
->sector_num
< 0) {
68 bdrv_dirty_iter_init(source
, &s
->hbi
);
69 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
70 trace_mirror_restart_iter(s
, bdrv_get_dirty_count(source
));
71 assert(s
->sector_num
>= 0);
74 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
75 nb_sectors
= MIN(BDRV_SECTORS_PER_DIRTY_CHUNK
, end
- s
->sector_num
);
76 bdrv_reset_dirty(source
, s
->sector_num
, nb_sectors
);
78 /* Copy the dirty cluster. */
79 iov
.iov_base
= s
->buf
;
80 iov
.iov_len
= nb_sectors
* 512;
81 qemu_iovec_init_external(&qiov
, &iov
, 1);
83 trace_mirror_one_iteration(s
, s
->sector_num
, nb_sectors
);
84 ret
= bdrv_co_readv(source
, s
->sector_num
, nb_sectors
, &qiov
);
86 *p_action
= mirror_error_action(s
, true, -ret
);
89 ret
= bdrv_co_writev(target
, s
->sector_num
, nb_sectors
, &qiov
);
91 *p_action
= mirror_error_action(s
, false, -ret
);
98 /* Try again later. */
99 bdrv_set_dirty(source
, s
->sector_num
, nb_sectors
);
103 static void coroutine_fn
mirror_run(void *opaque
)
105 MirrorBlockJob
*s
= opaque
;
106 BlockDriverState
*bs
= s
->common
.bs
;
107 int64_t sector_num
, end
;
111 if (block_job_is_cancelled(&s
->common
)) {
115 s
->common
.len
= bdrv_getlength(bs
);
116 if (s
->common
.len
< 0) {
117 block_job_completed(&s
->common
, s
->common
.len
);
121 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
122 s
->buf
= qemu_blockalign(bs
, BLOCK_SIZE
);
124 if (s
->mode
!= MIRROR_SYNC_MODE_NONE
) {
125 /* First part, loop on the sectors and initialize the dirty bitmap. */
126 BlockDriverState
*base
;
127 base
= s
->mode
== MIRROR_SYNC_MODE_FULL
? NULL
: bs
->backing_hd
;
128 for (sector_num
= 0; sector_num
< end
; ) {
129 int64_t next
= (sector_num
| (BDRV_SECTORS_PER_DIRTY_CHUNK
- 1)) + 1;
130 ret
= bdrv_co_is_allocated_above(bs
, base
,
131 sector_num
, next
- sector_num
, &n
);
139 bdrv_set_dirty(bs
, sector_num
, n
);
147 bdrv_dirty_iter_init(bs
, &s
->hbi
);
151 bool should_complete
;
153 cnt
= bdrv_get_dirty_count(bs
);
155 BlockErrorAction action
= BDRV_ACTION_REPORT
;
156 ret
= mirror_iteration(s
, &action
);
157 if (ret
< 0 && action
== BDRV_ACTION_REPORT
) {
160 cnt
= bdrv_get_dirty_count(bs
);
163 should_complete
= false;
165 trace_mirror_before_flush(s
);
166 ret
= bdrv_flush(s
->target
);
168 if (mirror_error_action(s
, false, -ret
) == BDRV_ACTION_REPORT
) {
172 /* We're out of the streaming phase. From now on, if the job
173 * is cancelled we will actually complete all pending I/O and
174 * report completion. This way, block-job-cancel will leave
175 * the target in a consistent state.
177 s
->common
.offset
= end
* BDRV_SECTOR_SIZE
;
179 block_job_ready(&s
->common
);
183 should_complete
= s
->should_complete
||
184 block_job_is_cancelled(&s
->common
);
185 cnt
= bdrv_get_dirty_count(bs
);
189 if (cnt
== 0 && should_complete
) {
190 /* The dirty bitmap is not updated while operations are pending.
191 * If we're about to exit, wait for pending operations before
192 * calling bdrv_get_dirty_count(bs), or we may exit while the
193 * source has dirty data to copy!
195 * Note that I/O can be submitted by the guest while
196 * mirror_populate runs.
198 trace_mirror_before_drain(s
, cnt
);
200 cnt
= bdrv_get_dirty_count(bs
);
204 trace_mirror_before_sleep(s
, cnt
, s
->synced
);
206 /* Publish progress */
207 s
->common
.offset
= end
* BDRV_SECTOR_SIZE
- cnt
* BLOCK_SIZE
;
209 if (s
->common
.speed
) {
210 delay_ns
= ratelimit_calculate_delay(&s
->limit
, BDRV_SECTORS_PER_DIRTY_CHUNK
);
215 /* Note that even when no rate limit is applied we need to yield
216 * with no pending I/O here so that bdrv_drain_all() returns.
218 block_job_sleep_ns(&s
->common
, rt_clock
, delay_ns
);
219 if (block_job_is_cancelled(&s
->common
)) {
222 } else if (!should_complete
) {
223 delay_ns
= (cnt
== 0 ? SLICE_TIME
: 0);
224 block_job_sleep_ns(&s
->common
, rt_clock
, delay_ns
);
225 } else if (cnt
== 0) {
226 /* The two disks are in sync. Exit and report successful
229 assert(QLIST_EMPTY(&bs
->tracked_requests
));
230 s
->common
.cancelled
= false;
237 bdrv_set_dirty_tracking(bs
, false);
238 bdrv_iostatus_disable(s
->target
);
239 if (s
->should_complete
&& ret
== 0) {
240 if (bdrv_get_flags(s
->target
) != bdrv_get_flags(s
->common
.bs
)) {
241 bdrv_reopen(s
->target
, bdrv_get_flags(s
->common
.bs
), NULL
);
243 bdrv_swap(s
->target
, s
->common
.bs
);
245 bdrv_close(s
->target
);
246 bdrv_delete(s
->target
);
247 block_job_completed(&s
->common
, ret
);
250 static void mirror_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
252 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
255 error_set(errp
, QERR_INVALID_PARAMETER
, "speed");
258 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
261 static void mirror_iostatus_reset(BlockJob
*job
)
263 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
265 bdrv_iostatus_reset(s
->target
);
268 static void mirror_complete(BlockJob
*job
, Error
**errp
)
270 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
273 ret
= bdrv_open_backing_file(s
->target
);
275 char backing_filename
[PATH_MAX
];
276 bdrv_get_full_backing_filename(s
->target
, backing_filename
,
277 sizeof(backing_filename
));
278 error_set(errp
, QERR_OPEN_FILE_FAILED
, backing_filename
);
282 error_set(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->bs
->device_name
);
286 s
->should_complete
= true;
287 block_job_resume(job
);
290 static BlockJobType mirror_job_type
= {
291 .instance_size
= sizeof(MirrorBlockJob
),
292 .job_type
= "mirror",
293 .set_speed
= mirror_set_speed
,
294 .iostatus_reset
= mirror_iostatus_reset
,
295 .complete
= mirror_complete
,
298 void mirror_start(BlockDriverState
*bs
, BlockDriverState
*target
,
299 int64_t speed
, MirrorSyncMode mode
,
300 BlockdevOnError on_source_error
,
301 BlockdevOnError on_target_error
,
302 BlockDriverCompletionFunc
*cb
,
303 void *opaque
, Error
**errp
)
307 if ((on_source_error
== BLOCKDEV_ON_ERROR_STOP
||
308 on_source_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
309 !bdrv_iostatus_is_enabled(bs
)) {
310 error_set(errp
, QERR_INVALID_PARAMETER
, "on-source-error");
314 s
= block_job_create(&mirror_job_type
, bs
, speed
, cb
, opaque
, errp
);
319 s
->on_source_error
= on_source_error
;
320 s
->on_target_error
= on_target_error
;
323 bdrv_set_dirty_tracking(bs
, true);
324 bdrv_set_enable_write_cache(s
->target
, true);
325 bdrv_set_on_error(s
->target
, on_target_error
, on_target_error
);
326 bdrv_iostatus_enable(s
->target
);
327 s
->common
.co
= qemu_coroutine_create(mirror_run
);
328 trace_mirror_start(bs
, s
, s
->common
.co
, opaque
);
329 qemu_coroutine_enter(s
->common
.co
, s
);