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 "qapi/qmp/qerror.h"
18 #include "qemu/ratelimit.h"
19 #include "qemu/bitmap.h"
21 #define SLICE_TIME 100000000ULL /* ns */
22 #define MAX_IN_FLIGHT 16
23 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
25 /* The mirroring buffer is a list of granularity-sized chunks.
26 * Free chunks are organized in a list.
28 typedef struct MirrorBuffer
{
29 QSIMPLEQ_ENTRY(MirrorBuffer
) next
;
32 typedef struct MirrorBlockJob
{
35 BlockDriverState
*target
;
36 BlockDriverState
*base
;
37 /* The name of the graph node to replace */
39 /* The BDS to replace */
40 BlockDriverState
*to_replace
;
41 /* Used to block operations on the drive-mirror-replace target */
42 Error
*replace_blocker
;
44 BlockdevOnError on_source_error
, on_target_error
;
51 unsigned long *cow_bitmap
;
52 BdrvDirtyBitmap
*dirty_bitmap
;
55 QSIMPLEQ_HEAD(, MirrorBuffer
) buf_free
;
58 unsigned long *in_flight_bitmap
;
60 int sectors_in_flight
;
66 typedef struct MirrorOp
{
73 static BlockErrorAction
mirror_error_action(MirrorBlockJob
*s
, bool read
,
78 return block_job_error_action(&s
->common
, s
->common
.bs
,
79 s
->on_source_error
, true, error
);
81 return block_job_error_action(&s
->common
, s
->target
,
82 s
->on_target_error
, false, error
);
86 static void mirror_iteration_done(MirrorOp
*op
, int ret
)
88 MirrorBlockJob
*s
= op
->s
;
91 int i
, nb_chunks
, sectors_per_chunk
;
93 trace_mirror_iteration_done(s
, op
->sector_num
, op
->nb_sectors
, ret
);
96 s
->sectors_in_flight
-= op
->nb_sectors
;
98 for (i
= 0; i
< op
->qiov
.niov
; i
++) {
99 MirrorBuffer
*buf
= (MirrorBuffer
*) iov
[i
].iov_base
;
100 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, buf
, next
);
104 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
105 chunk_num
= op
->sector_num
/ sectors_per_chunk
;
106 nb_chunks
= op
->nb_sectors
/ sectors_per_chunk
;
107 bitmap_clear(s
->in_flight_bitmap
, chunk_num
, nb_chunks
);
110 bitmap_set(s
->cow_bitmap
, chunk_num
, nb_chunks
);
112 s
->common
.offset
+= (uint64_t)op
->nb_sectors
* BDRV_SECTOR_SIZE
;
115 qemu_iovec_destroy(&op
->qiov
);
116 g_slice_free(MirrorOp
, op
);
118 if (s
->waiting_for_io
) {
119 qemu_coroutine_enter(s
->common
.co
, NULL
);
123 static void mirror_write_complete(void *opaque
, int ret
)
125 MirrorOp
*op
= opaque
;
126 MirrorBlockJob
*s
= op
->s
;
128 BlockErrorAction action
;
130 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, op
->sector_num
, op
->nb_sectors
);
131 action
= mirror_error_action(s
, false, -ret
);
132 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
136 mirror_iteration_done(op
, ret
);
139 static void mirror_read_complete(void *opaque
, int ret
)
141 MirrorOp
*op
= opaque
;
142 MirrorBlockJob
*s
= op
->s
;
144 BlockErrorAction action
;
146 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, op
->sector_num
, op
->nb_sectors
);
147 action
= mirror_error_action(s
, true, -ret
);
148 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
152 mirror_iteration_done(op
, ret
);
155 bdrv_aio_writev(s
->target
, op
->sector_num
, &op
->qiov
, op
->nb_sectors
,
156 mirror_write_complete
, op
);
159 static uint64_t coroutine_fn
mirror_iteration(MirrorBlockJob
*s
)
161 BlockDriverState
*source
= s
->common
.bs
;
162 int nb_sectors
, sectors_per_chunk
, nb_chunks
;
163 int64_t end
, sector_num
, next_chunk
, next_sector
, hbitmap_next_sector
;
164 uint64_t delay_ns
= 0;
169 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
170 if (s
->sector_num
< 0) {
171 bdrv_dirty_iter_init(s
->dirty_bitmap
, &s
->hbi
);
172 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
173 trace_mirror_restart_iter(s
, bdrv_get_dirty_count(s
->dirty_bitmap
));
174 assert(s
->sector_num
>= 0);
177 hbitmap_next_sector
= s
->sector_num
;
178 sector_num
= s
->sector_num
;
179 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
180 end
= s
->bdev_length
/ BDRV_SECTOR_SIZE
;
182 /* Extend the QEMUIOVector to include all adjacent blocks that will
183 * be copied in this operation.
185 * We have to do this if we have no backing file yet in the destination,
186 * and the cluster size is very large. Then we need to do COW ourselves.
187 * The first time a cluster is copied, copy it entirely. Note that,
188 * because both the granularity and the cluster size are powers of two,
189 * the number of sectors to copy cannot exceed one cluster.
191 * We also want to extend the QEMUIOVector to include more adjacent
192 * dirty blocks if possible, to limit the number of I/O operations and
193 * run efficiently even with a small granularity.
197 next_sector
= sector_num
;
198 next_chunk
= sector_num
/ sectors_per_chunk
;
200 /* Wait for I/O to this cluster (from a previous iteration) to be done. */
201 while (test_bit(next_chunk
, s
->in_flight_bitmap
)) {
202 trace_mirror_yield_in_flight(s
, sector_num
, s
->in_flight
);
203 s
->waiting_for_io
= true;
204 qemu_coroutine_yield();
205 s
->waiting_for_io
= false;
209 int added_sectors
, added_chunks
;
211 if (!bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
) ||
212 test_bit(next_chunk
, s
->in_flight_bitmap
)) {
213 assert(nb_sectors
> 0);
217 added_sectors
= sectors_per_chunk
;
218 if (s
->cow_bitmap
&& !test_bit(next_chunk
, s
->cow_bitmap
)) {
219 bdrv_round_to_clusters(s
->target
,
220 next_sector
, added_sectors
,
221 &next_sector
, &added_sectors
);
223 /* On the first iteration, the rounding may make us copy
224 * sectors before the first dirty one.
226 if (next_sector
< sector_num
) {
227 assert(nb_sectors
== 0);
228 sector_num
= next_sector
;
229 next_chunk
= next_sector
/ sectors_per_chunk
;
233 added_sectors
= MIN(added_sectors
, end
- (sector_num
+ nb_sectors
));
234 added_chunks
= (added_sectors
+ sectors_per_chunk
- 1) / sectors_per_chunk
;
236 /* When doing COW, it may happen that there is not enough space for
237 * a full cluster. Wait if that is the case.
239 while (nb_chunks
== 0 && s
->buf_free_count
< added_chunks
) {
240 trace_mirror_yield_buf_busy(s
, nb_chunks
, s
->in_flight
);
241 s
->waiting_for_io
= true;
242 qemu_coroutine_yield();
243 s
->waiting_for_io
= false;
245 if (s
->buf_free_count
< nb_chunks
+ added_chunks
) {
246 trace_mirror_break_buf_busy(s
, nb_chunks
, s
->in_flight
);
250 /* We have enough free space to copy these sectors. */
251 bitmap_set(s
->in_flight_bitmap
, next_chunk
, added_chunks
);
253 nb_sectors
+= added_sectors
;
254 nb_chunks
+= added_chunks
;
255 next_sector
+= added_sectors
;
256 next_chunk
+= added_chunks
;
257 if (!s
->synced
&& s
->common
.speed
) {
258 delay_ns
= ratelimit_calculate_delay(&s
->limit
, added_sectors
);
260 } while (delay_ns
== 0 && next_sector
< end
);
262 /* Allocate a MirrorOp that is used as an AIO callback. */
263 op
= g_slice_new(MirrorOp
);
265 op
->sector_num
= sector_num
;
266 op
->nb_sectors
= nb_sectors
;
268 /* Now make a QEMUIOVector taking enough granularity-sized chunks
271 qemu_iovec_init(&op
->qiov
, nb_chunks
);
272 next_sector
= sector_num
;
273 while (nb_chunks
-- > 0) {
274 MirrorBuffer
*buf
= QSIMPLEQ_FIRST(&s
->buf_free
);
275 size_t remaining
= (nb_sectors
* BDRV_SECTOR_SIZE
) - op
->qiov
.size
;
277 QSIMPLEQ_REMOVE_HEAD(&s
->buf_free
, next
);
279 qemu_iovec_add(&op
->qiov
, buf
, MIN(s
->granularity
, remaining
));
281 /* Advance the HBitmapIter in parallel, so that we do not examine
282 * the same sector twice.
284 if (next_sector
> hbitmap_next_sector
285 && bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
)) {
286 hbitmap_next_sector
= hbitmap_iter_next(&s
->hbi
);
289 next_sector
+= sectors_per_chunk
;
292 bdrv_reset_dirty_bitmap(s
->dirty_bitmap
, sector_num
, nb_sectors
);
294 /* Copy the dirty cluster. */
296 s
->sectors_in_flight
+= nb_sectors
;
297 trace_mirror_one_iteration(s
, sector_num
, nb_sectors
);
299 ret
= bdrv_get_block_status_above(source
, NULL
, sector_num
,
301 if (ret
< 0 || pnum
< nb_sectors
||
302 (ret
& BDRV_BLOCK_DATA
&& !(ret
& BDRV_BLOCK_ZERO
))) {
303 bdrv_aio_readv(source
, sector_num
, &op
->qiov
, nb_sectors
,
304 mirror_read_complete
, op
);
305 } else if (ret
& BDRV_BLOCK_ZERO
) {
306 bdrv_aio_write_zeroes(s
->target
, sector_num
, op
->nb_sectors
,
307 s
->unmap
? BDRV_REQ_MAY_UNMAP
: 0,
308 mirror_write_complete
, op
);
310 assert(!(ret
& BDRV_BLOCK_DATA
));
311 bdrv_aio_discard(s
->target
, sector_num
, op
->nb_sectors
,
312 mirror_write_complete
, op
);
317 static void mirror_free_init(MirrorBlockJob
*s
)
319 int granularity
= s
->granularity
;
320 size_t buf_size
= s
->buf_size
;
321 uint8_t *buf
= s
->buf
;
323 assert(s
->buf_free_count
== 0);
324 QSIMPLEQ_INIT(&s
->buf_free
);
325 while (buf_size
!= 0) {
326 MirrorBuffer
*cur
= (MirrorBuffer
*)buf
;
327 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, cur
, next
);
329 buf_size
-= granularity
;
334 static void mirror_drain(MirrorBlockJob
*s
)
336 while (s
->in_flight
> 0) {
337 s
->waiting_for_io
= true;
338 qemu_coroutine_yield();
339 s
->waiting_for_io
= false;
347 static void mirror_exit(BlockJob
*job
, void *opaque
)
349 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
350 MirrorExitData
*data
= opaque
;
351 AioContext
*replace_aio_context
= NULL
;
354 replace_aio_context
= bdrv_get_aio_context(s
->to_replace
);
355 aio_context_acquire(replace_aio_context
);
358 if (s
->should_complete
&& data
->ret
== 0) {
359 BlockDriverState
*to_replace
= s
->common
.bs
;
361 to_replace
= s
->to_replace
;
363 if (bdrv_get_flags(s
->target
) != bdrv_get_flags(to_replace
)) {
364 bdrv_reopen(s
->target
, bdrv_get_flags(to_replace
), NULL
);
366 bdrv_swap(s
->target
, to_replace
);
367 if (s
->common
.driver
->job_type
== BLOCK_JOB_TYPE_COMMIT
) {
368 /* drop the bs loop chain formed by the swap: break the loop then
369 * trigger the unref from the top one */
370 BlockDriverState
*p
= s
->base
->backing_hd
;
371 bdrv_set_backing_hd(s
->base
, NULL
);
376 bdrv_op_unblock_all(s
->to_replace
, s
->replace_blocker
);
377 error_free(s
->replace_blocker
);
378 bdrv_unref(s
->to_replace
);
380 if (replace_aio_context
) {
381 aio_context_release(replace_aio_context
);
384 bdrv_unref(s
->target
);
385 block_job_completed(&s
->common
, data
->ret
);
389 static void coroutine_fn
mirror_run(void *opaque
)
391 MirrorBlockJob
*s
= opaque
;
392 MirrorExitData
*data
;
393 BlockDriverState
*bs
= s
->common
.bs
;
394 int64_t sector_num
, end
, length
;
395 uint64_t last_pause_ns
;
397 char backing_filename
[2]; /* we only need 2 characters because we are only
398 checking for a NULL string */
402 if (block_job_is_cancelled(&s
->common
)) {
406 s
->bdev_length
= bdrv_getlength(bs
);
407 if (s
->bdev_length
< 0) {
408 ret
= s
->bdev_length
;
410 } else if (s
->bdev_length
== 0) {
411 /* Report BLOCK_JOB_READY and wait for complete. */
412 block_job_event_ready(&s
->common
);
414 while (!block_job_is_cancelled(&s
->common
) && !s
->should_complete
) {
415 block_job_yield(&s
->common
);
417 s
->common
.cancelled
= false;
421 length
= DIV_ROUND_UP(s
->bdev_length
, s
->granularity
);
422 s
->in_flight_bitmap
= bitmap_new(length
);
424 /* If we have no backing file yet in the destination, we cannot let
425 * the destination do COW. Instead, we copy sectors around the
426 * dirty data if needed. We need a bitmap to do that.
428 bdrv_get_backing_filename(s
->target
, backing_filename
,
429 sizeof(backing_filename
));
430 if (backing_filename
[0] && !s
->target
->backing_hd
) {
431 ret
= bdrv_get_info(s
->target
, &bdi
);
435 if (s
->granularity
< bdi
.cluster_size
) {
436 s
->buf_size
= MAX(s
->buf_size
, bdi
.cluster_size
);
437 s
->cow_bitmap
= bitmap_new(length
);
441 end
= s
->bdev_length
/ BDRV_SECTOR_SIZE
;
442 s
->buf
= qemu_try_blockalign(bs
, s
->buf_size
);
443 if (s
->buf
== NULL
) {
450 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
451 if (!s
->is_none_mode
) {
452 /* First part, loop on the sectors and initialize the dirty bitmap. */
453 BlockDriverState
*base
= s
->base
;
454 for (sector_num
= 0; sector_num
< end
; ) {
455 /* Just to make sure we are not exceeding int limit. */
456 int nb_sectors
= MIN(INT_MAX
>> BDRV_SECTOR_BITS
,
458 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
460 if (now
- last_pause_ns
> SLICE_TIME
) {
462 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, 0);
465 if (block_job_is_cancelled(&s
->common
)) {
469 ret
= bdrv_is_allocated_above(bs
, base
, sector_num
, nb_sectors
, &n
);
477 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, sector_num
, n
);
483 bdrv_dirty_iter_init(s
->dirty_bitmap
, &s
->hbi
);
485 uint64_t delay_ns
= 0;
487 bool should_complete
;
494 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
495 /* s->common.offset contains the number of bytes already processed so
496 * far, cnt is the number of dirty sectors remaining and
497 * s->sectors_in_flight is the number of sectors currently being
498 * processed; together those are the current total operation length */
499 s
->common
.len
= s
->common
.offset
+
500 (cnt
+ s
->sectors_in_flight
) * BDRV_SECTOR_SIZE
;
502 /* Note that even when no rate limit is applied we need to yield
503 * periodically with no pending I/O so that bdrv_drain_all() returns.
504 * We do so every SLICE_TIME nanoseconds, or when there is an error,
505 * or when the source is clean, whichever comes first.
507 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - last_pause_ns
< SLICE_TIME
&&
508 s
->common
.iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
509 if (s
->in_flight
== MAX_IN_FLIGHT
|| s
->buf_free_count
== 0 ||
510 (cnt
== 0 && s
->in_flight
> 0)) {
511 trace_mirror_yield(s
, s
->in_flight
, s
->buf_free_count
, cnt
);
512 s
->waiting_for_io
= true;
513 qemu_coroutine_yield();
514 s
->waiting_for_io
= false;
516 } else if (cnt
!= 0) {
517 delay_ns
= mirror_iteration(s
);
521 should_complete
= false;
522 if (s
->in_flight
== 0 && cnt
== 0) {
523 trace_mirror_before_flush(s
);
524 ret
= bdrv_flush(s
->target
);
526 if (mirror_error_action(s
, false, -ret
) ==
527 BLOCK_ERROR_ACTION_REPORT
) {
531 /* We're out of the streaming phase. From now on, if the job
532 * is cancelled we will actually complete all pending I/O and
533 * report completion. This way, block-job-cancel will leave
534 * the target in a consistent state.
537 block_job_event_ready(&s
->common
);
541 should_complete
= s
->should_complete
||
542 block_job_is_cancelled(&s
->common
);
543 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
547 if (cnt
== 0 && should_complete
) {
548 /* The dirty bitmap is not updated while operations are pending.
549 * If we're about to exit, wait for pending operations before
550 * calling bdrv_get_dirty_count(bs), or we may exit while the
551 * source has dirty data to copy!
553 * Note that I/O can be submitted by the guest while
554 * mirror_populate runs.
556 trace_mirror_before_drain(s
, cnt
);
558 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
562 trace_mirror_before_sleep(s
, cnt
, s
->synced
, delay_ns
);
564 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
565 if (block_job_is_cancelled(&s
->common
)) {
568 } else if (!should_complete
) {
569 delay_ns
= (s
->in_flight
== 0 && cnt
== 0 ? SLICE_TIME
: 0);
570 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
571 } else if (cnt
== 0) {
572 /* The two disks are in sync. Exit and report successful
575 assert(QLIST_EMPTY(&bs
->tracked_requests
));
576 s
->common
.cancelled
= false;
579 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
583 if (s
->in_flight
> 0) {
584 /* We get here only if something went wrong. Either the job failed,
585 * or it was cancelled prematurely so that we do not guarantee that
586 * the target is a copy of the source.
588 assert(ret
< 0 || (!s
->synced
&& block_job_is_cancelled(&s
->common
)));
592 assert(s
->in_flight
== 0);
594 g_free(s
->cow_bitmap
);
595 g_free(s
->in_flight_bitmap
);
596 bdrv_release_dirty_bitmap(bs
, s
->dirty_bitmap
);
597 bdrv_iostatus_disable(s
->target
);
599 data
= g_malloc(sizeof(*data
));
601 block_job_defer_to_main_loop(&s
->common
, mirror_exit
, data
);
604 static void mirror_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
606 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
609 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
612 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
615 static void mirror_iostatus_reset(BlockJob
*job
)
617 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
619 bdrv_iostatus_reset(s
->target
);
622 static void mirror_complete(BlockJob
*job
, Error
**errp
)
624 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
625 Error
*local_err
= NULL
;
628 ret
= bdrv_open_backing_file(s
->target
, NULL
, &local_err
);
630 error_propagate(errp
, local_err
);
634 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
,
635 bdrv_get_device_name(job
->bs
));
639 /* check the target bs is not blocked and block all operations on it */
641 AioContext
*replace_aio_context
;
643 s
->to_replace
= check_to_replace_node(s
->replaces
, &local_err
);
644 if (!s
->to_replace
) {
645 error_propagate(errp
, local_err
);
649 replace_aio_context
= bdrv_get_aio_context(s
->to_replace
);
650 aio_context_acquire(replace_aio_context
);
652 error_setg(&s
->replace_blocker
,
653 "block device is in use by block-job-complete");
654 bdrv_op_block_all(s
->to_replace
, s
->replace_blocker
);
655 bdrv_ref(s
->to_replace
);
657 aio_context_release(replace_aio_context
);
660 s
->should_complete
= true;
661 block_job_enter(&s
->common
);
664 static const BlockJobDriver mirror_job_driver
= {
665 .instance_size
= sizeof(MirrorBlockJob
),
666 .job_type
= BLOCK_JOB_TYPE_MIRROR
,
667 .set_speed
= mirror_set_speed
,
668 .iostatus_reset
= mirror_iostatus_reset
,
669 .complete
= mirror_complete
,
672 static const BlockJobDriver commit_active_job_driver
= {
673 .instance_size
= sizeof(MirrorBlockJob
),
674 .job_type
= BLOCK_JOB_TYPE_COMMIT
,
675 .set_speed
= mirror_set_speed
,
677 = mirror_iostatus_reset
,
678 .complete
= mirror_complete
,
681 static void mirror_start_job(BlockDriverState
*bs
, BlockDriverState
*target
,
682 const char *replaces
,
683 int64_t speed
, uint32_t granularity
,
685 BlockdevOnError on_source_error
,
686 BlockdevOnError on_target_error
,
688 BlockCompletionFunc
*cb
,
689 void *opaque
, Error
**errp
,
690 const BlockJobDriver
*driver
,
691 bool is_none_mode
, BlockDriverState
*base
)
695 if (granularity
== 0) {
696 granularity
= bdrv_get_default_bitmap_granularity(target
);
699 assert ((granularity
& (granularity
- 1)) == 0);
701 if ((on_source_error
== BLOCKDEV_ON_ERROR_STOP
||
702 on_source_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
703 !bdrv_iostatus_is_enabled(bs
)) {
704 error_setg(errp
, QERR_INVALID_PARAMETER
, "on-source-error");
709 error_setg(errp
, "Invalid parameter 'buf-size'");
714 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
717 s
= block_job_create(driver
, bs
, speed
, cb
, opaque
, errp
);
722 s
->replaces
= g_strdup(replaces
);
723 s
->on_source_error
= on_source_error
;
724 s
->on_target_error
= on_target_error
;
726 s
->is_none_mode
= is_none_mode
;
728 s
->granularity
= granularity
;
729 s
->buf_size
= ROUND_UP(buf_size
, granularity
);
732 s
->dirty_bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
733 if (!s
->dirty_bitmap
) {
735 block_job_release(bs
);
738 bdrv_set_enable_write_cache(s
->target
, true);
739 bdrv_set_on_error(s
->target
, on_target_error
, on_target_error
);
740 bdrv_iostatus_enable(s
->target
);
741 s
->common
.co
= qemu_coroutine_create(mirror_run
);
742 trace_mirror_start(bs
, s
, s
->common
.co
, opaque
);
743 qemu_coroutine_enter(s
->common
.co
, s
);
746 void mirror_start(BlockDriverState
*bs
, BlockDriverState
*target
,
747 const char *replaces
,
748 int64_t speed
, uint32_t granularity
, int64_t buf_size
,
749 MirrorSyncMode mode
, BlockdevOnError on_source_error
,
750 BlockdevOnError on_target_error
,
752 BlockCompletionFunc
*cb
,
753 void *opaque
, Error
**errp
)
756 BlockDriverState
*base
;
758 if (mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
759 error_setg(errp
, "Sync mode 'incremental' not supported");
762 is_none_mode
= mode
== MIRROR_SYNC_MODE_NONE
;
763 base
= mode
== MIRROR_SYNC_MODE_TOP
? bs
->backing_hd
: NULL
;
764 mirror_start_job(bs
, target
, replaces
,
765 speed
, granularity
, buf_size
,
766 on_source_error
, on_target_error
, unmap
, cb
, opaque
, errp
,
767 &mirror_job_driver
, is_none_mode
, base
);
770 void commit_active_start(BlockDriverState
*bs
, BlockDriverState
*base
,
772 BlockdevOnError on_error
,
773 BlockCompletionFunc
*cb
,
774 void *opaque
, Error
**errp
)
776 int64_t length
, base_length
;
779 Error
*local_err
= NULL
;
781 orig_base_flags
= bdrv_get_flags(base
);
783 if (bdrv_reopen(base
, bs
->open_flags
, errp
)) {
787 length
= bdrv_getlength(bs
);
789 error_setg_errno(errp
, -length
,
790 "Unable to determine length of %s", bs
->filename
);
791 goto error_restore_flags
;
794 base_length
= bdrv_getlength(base
);
795 if (base_length
< 0) {
796 error_setg_errno(errp
, -base_length
,
797 "Unable to determine length of %s", base
->filename
);
798 goto error_restore_flags
;
801 if (length
> base_length
) {
802 ret
= bdrv_truncate(base
, length
);
804 error_setg_errno(errp
, -ret
,
805 "Top image %s is larger than base image %s, and "
806 "resize of base image failed",
807 bs
->filename
, base
->filename
);
808 goto error_restore_flags
;
813 mirror_start_job(bs
, base
, NULL
, speed
, 0, 0,
814 on_error
, on_error
, false, cb
, opaque
, &local_err
,
815 &commit_active_job_driver
, false, base
);
817 error_propagate(errp
, local_err
);
818 goto error_restore_flags
;
824 /* ignore error and errp for bdrv_reopen, because we want to propagate
825 * the original error */
826 bdrv_reopen(base
, orig_base_flags
, NULL
);