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.
14 #include "qemu/osdep.h"
16 #include "block/blockjob.h"
17 #include "block/block_int.h"
18 #include "sysemu/block-backend.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qemu/ratelimit.h"
21 #include "qemu/bitmap.h"
22 #include "qemu/error-report.h"
24 #define SLICE_TIME 100000000ULL /* ns */
25 #define MAX_IN_FLIGHT 16
26 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
28 /* The mirroring buffer is a list of granularity-sized chunks.
29 * Free chunks are organized in a list.
31 typedef struct MirrorBuffer
{
32 QSIMPLEQ_ENTRY(MirrorBuffer
) next
;
35 typedef struct MirrorBlockJob
{
38 BlockDriverState
*target
;
39 BlockDriverState
*base
;
40 /* The name of the graph node to replace */
42 /* The BDS to replace */
43 BlockDriverState
*to_replace
;
44 /* Used to block operations on the drive-mirror-replace target */
45 Error
*replace_blocker
;
47 BlockdevOnError on_source_error
, on_target_error
;
54 unsigned long *cow_bitmap
;
55 BdrvDirtyBitmap
*dirty_bitmap
;
58 QSIMPLEQ_HEAD(, MirrorBuffer
) buf_free
;
61 unsigned long *in_flight_bitmap
;
63 int sectors_in_flight
;
69 typedef struct MirrorOp
{
76 static BlockErrorAction
mirror_error_action(MirrorBlockJob
*s
, bool read
,
81 return block_job_error_action(&s
->common
, s
->common
.bs
,
82 s
->on_source_error
, true, error
);
84 return block_job_error_action(&s
->common
, s
->target
,
85 s
->on_target_error
, false, error
);
89 static void mirror_iteration_done(MirrorOp
*op
, int ret
)
91 MirrorBlockJob
*s
= op
->s
;
94 int i
, nb_chunks
, sectors_per_chunk
;
96 trace_mirror_iteration_done(s
, op
->sector_num
, op
->nb_sectors
, ret
);
99 s
->sectors_in_flight
-= op
->nb_sectors
;
101 for (i
= 0; i
< op
->qiov
.niov
; i
++) {
102 MirrorBuffer
*buf
= (MirrorBuffer
*) iov
[i
].iov_base
;
103 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, buf
, next
);
107 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
108 chunk_num
= op
->sector_num
/ sectors_per_chunk
;
109 nb_chunks
= op
->nb_sectors
/ sectors_per_chunk
;
110 bitmap_clear(s
->in_flight_bitmap
, chunk_num
, nb_chunks
);
113 bitmap_set(s
->cow_bitmap
, chunk_num
, nb_chunks
);
115 s
->common
.offset
+= (uint64_t)op
->nb_sectors
* BDRV_SECTOR_SIZE
;
118 qemu_iovec_destroy(&op
->qiov
);
121 if (s
->waiting_for_io
) {
122 qemu_coroutine_enter(s
->common
.co
, NULL
);
126 static void mirror_write_complete(void *opaque
, int ret
)
128 MirrorOp
*op
= opaque
;
129 MirrorBlockJob
*s
= op
->s
;
131 BlockErrorAction action
;
133 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, op
->sector_num
, op
->nb_sectors
);
134 action
= mirror_error_action(s
, false, -ret
);
135 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
139 mirror_iteration_done(op
, ret
);
142 static void mirror_read_complete(void *opaque
, int ret
)
144 MirrorOp
*op
= opaque
;
145 MirrorBlockJob
*s
= op
->s
;
147 BlockErrorAction action
;
149 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, op
->sector_num
, op
->nb_sectors
);
150 action
= mirror_error_action(s
, true, -ret
);
151 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
155 mirror_iteration_done(op
, ret
);
158 bdrv_aio_writev(s
->target
, op
->sector_num
, &op
->qiov
, op
->nb_sectors
,
159 mirror_write_complete
, op
);
162 static uint64_t coroutine_fn
mirror_iteration(MirrorBlockJob
*s
)
164 BlockDriverState
*source
= s
->common
.bs
;
165 int nb_sectors
, sectors_per_chunk
, nb_chunks
, max_iov
;
166 int64_t end
, sector_num
, next_chunk
, next_sector
, hbitmap_next_sector
;
167 uint64_t delay_ns
= 0;
171 BlockDriverState
*file
;
173 max_iov
= MIN(source
->bl
.max_iov
, s
->target
->bl
.max_iov
);
175 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
176 if (s
->sector_num
< 0) {
177 bdrv_dirty_iter_init(s
->dirty_bitmap
, &s
->hbi
);
178 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
179 trace_mirror_restart_iter(s
, bdrv_get_dirty_count(s
->dirty_bitmap
));
180 assert(s
->sector_num
>= 0);
183 hbitmap_next_sector
= s
->sector_num
;
184 sector_num
= s
->sector_num
;
185 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
186 end
= s
->bdev_length
/ BDRV_SECTOR_SIZE
;
188 /* Extend the QEMUIOVector to include all adjacent blocks that will
189 * be copied in this operation.
191 * We have to do this if we have no backing file yet in the destination,
192 * and the cluster size is very large. Then we need to do COW ourselves.
193 * The first time a cluster is copied, copy it entirely. Note that,
194 * because both the granularity and the cluster size are powers of two,
195 * the number of sectors to copy cannot exceed one cluster.
197 * We also want to extend the QEMUIOVector to include more adjacent
198 * dirty blocks if possible, to limit the number of I/O operations and
199 * run efficiently even with a small granularity.
203 next_sector
= sector_num
;
204 next_chunk
= sector_num
/ sectors_per_chunk
;
206 /* Wait for I/O to this cluster (from a previous iteration) to be done. */
207 while (test_bit(next_chunk
, s
->in_flight_bitmap
)) {
208 trace_mirror_yield_in_flight(s
, sector_num
, s
->in_flight
);
209 s
->waiting_for_io
= true;
210 qemu_coroutine_yield();
211 s
->waiting_for_io
= false;
215 int added_sectors
, added_chunks
;
217 if (!bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
) ||
218 test_bit(next_chunk
, s
->in_flight_bitmap
)) {
219 assert(nb_sectors
> 0);
223 added_sectors
= sectors_per_chunk
;
224 if (s
->cow_bitmap
&& !test_bit(next_chunk
, s
->cow_bitmap
)) {
225 bdrv_round_to_clusters(s
->target
,
226 next_sector
, added_sectors
,
227 &next_sector
, &added_sectors
);
229 /* On the first iteration, the rounding may make us copy
230 * sectors before the first dirty one.
232 if (next_sector
< sector_num
) {
233 assert(nb_sectors
== 0);
234 sector_num
= next_sector
;
235 next_chunk
= next_sector
/ sectors_per_chunk
;
239 added_sectors
= MIN(added_sectors
, end
- (sector_num
+ nb_sectors
));
240 added_chunks
= (added_sectors
+ sectors_per_chunk
- 1) / sectors_per_chunk
;
242 /* When doing COW, it may happen that there is not enough space for
243 * a full cluster. Wait if that is the case.
245 while (nb_chunks
== 0 && s
->buf_free_count
< added_chunks
) {
246 trace_mirror_yield_buf_busy(s
, nb_chunks
, s
->in_flight
);
247 s
->waiting_for_io
= true;
248 qemu_coroutine_yield();
249 s
->waiting_for_io
= false;
251 if (s
->buf_free_count
< nb_chunks
+ added_chunks
) {
252 trace_mirror_break_buf_busy(s
, nb_chunks
, s
->in_flight
);
255 if (max_iov
< nb_chunks
+ added_chunks
) {
256 trace_mirror_break_iov_max(s
, nb_chunks
, added_chunks
);
260 /* We have enough free space to copy these sectors. */
261 bitmap_set(s
->in_flight_bitmap
, next_chunk
, added_chunks
);
263 nb_sectors
+= added_sectors
;
264 nb_chunks
+= added_chunks
;
265 next_sector
+= added_sectors
;
266 next_chunk
+= added_chunks
;
267 if (!s
->synced
&& s
->common
.speed
) {
268 delay_ns
= ratelimit_calculate_delay(&s
->limit
, added_sectors
);
270 } while (delay_ns
== 0 && next_sector
< end
);
272 /* Allocate a MirrorOp that is used as an AIO callback. */
273 op
= g_new(MirrorOp
, 1);
275 op
->sector_num
= sector_num
;
276 op
->nb_sectors
= nb_sectors
;
278 /* Now make a QEMUIOVector taking enough granularity-sized chunks
281 qemu_iovec_init(&op
->qiov
, nb_chunks
);
282 next_sector
= sector_num
;
283 while (nb_chunks
-- > 0) {
284 MirrorBuffer
*buf
= QSIMPLEQ_FIRST(&s
->buf_free
);
285 size_t remaining
= (nb_sectors
* BDRV_SECTOR_SIZE
) - op
->qiov
.size
;
287 QSIMPLEQ_REMOVE_HEAD(&s
->buf_free
, next
);
289 qemu_iovec_add(&op
->qiov
, buf
, MIN(s
->granularity
, remaining
));
291 /* Advance the HBitmapIter in parallel, so that we do not examine
292 * the same sector twice.
294 if (next_sector
> hbitmap_next_sector
295 && bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
)) {
296 hbitmap_next_sector
= hbitmap_iter_next(&s
->hbi
);
299 next_sector
+= sectors_per_chunk
;
302 bdrv_reset_dirty_bitmap(s
->dirty_bitmap
, sector_num
, nb_sectors
);
304 /* Copy the dirty cluster. */
306 s
->sectors_in_flight
+= nb_sectors
;
307 trace_mirror_one_iteration(s
, sector_num
, nb_sectors
);
309 ret
= bdrv_get_block_status_above(source
, NULL
, sector_num
,
310 nb_sectors
, &pnum
, &file
);
311 if (ret
< 0 || pnum
< nb_sectors
||
312 (ret
& BDRV_BLOCK_DATA
&& !(ret
& BDRV_BLOCK_ZERO
))) {
313 bdrv_aio_readv(source
, sector_num
, &op
->qiov
, nb_sectors
,
314 mirror_read_complete
, op
);
315 } else if (ret
& BDRV_BLOCK_ZERO
) {
316 bdrv_aio_write_zeroes(s
->target
, sector_num
, op
->nb_sectors
,
317 s
->unmap
? BDRV_REQ_MAY_UNMAP
: 0,
318 mirror_write_complete
, op
);
320 assert(!(ret
& BDRV_BLOCK_DATA
));
321 bdrv_aio_discard(s
->target
, sector_num
, op
->nb_sectors
,
322 mirror_write_complete
, op
);
327 static void mirror_free_init(MirrorBlockJob
*s
)
329 int granularity
= s
->granularity
;
330 size_t buf_size
= s
->buf_size
;
331 uint8_t *buf
= s
->buf
;
333 assert(s
->buf_free_count
== 0);
334 QSIMPLEQ_INIT(&s
->buf_free
);
335 while (buf_size
!= 0) {
336 MirrorBuffer
*cur
= (MirrorBuffer
*)buf
;
337 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, cur
, next
);
339 buf_size
-= granularity
;
344 static void mirror_drain(MirrorBlockJob
*s
)
346 while (s
->in_flight
> 0) {
347 s
->waiting_for_io
= true;
348 qemu_coroutine_yield();
349 s
->waiting_for_io
= false;
357 static void mirror_exit(BlockJob
*job
, void *opaque
)
359 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
360 MirrorExitData
*data
= opaque
;
361 AioContext
*replace_aio_context
= NULL
;
362 BlockDriverState
*src
= s
->common
.bs
;
364 /* Make sure that the source BDS doesn't go away before we called
365 * block_job_completed(). */
369 replace_aio_context
= bdrv_get_aio_context(s
->to_replace
);
370 aio_context_acquire(replace_aio_context
);
373 if (s
->should_complete
&& data
->ret
== 0) {
374 BlockDriverState
*to_replace
= s
->common
.bs
;
376 to_replace
= s
->to_replace
;
379 /* This was checked in mirror_start_job(), but meanwhile one of the
380 * nodes could have been newly attached to a BlockBackend. */
381 if (to_replace
->blk
&& s
->target
->blk
) {
382 error_report("block job: Can't create node with two BlockBackends");
387 if (bdrv_get_flags(s
->target
) != bdrv_get_flags(to_replace
)) {
388 bdrv_reopen(s
->target
, bdrv_get_flags(to_replace
), NULL
);
390 bdrv_replace_in_backing_chain(to_replace
, s
->target
);
395 bdrv_op_unblock_all(s
->to_replace
, s
->replace_blocker
);
396 error_free(s
->replace_blocker
);
397 bdrv_unref(s
->to_replace
);
399 if (replace_aio_context
) {
400 aio_context_release(replace_aio_context
);
403 bdrv_op_unblock_all(s
->target
, s
->common
.blocker
);
404 bdrv_unref(s
->target
);
405 block_job_completed(&s
->common
, data
->ret
);
407 bdrv_drained_end(src
);
411 static void coroutine_fn
mirror_run(void *opaque
)
413 MirrorBlockJob
*s
= opaque
;
414 MirrorExitData
*data
;
415 BlockDriverState
*bs
= s
->common
.bs
;
416 int64_t sector_num
, end
, length
;
417 uint64_t last_pause_ns
;
419 char backing_filename
[2]; /* we only need 2 characters because we are only
420 checking for a NULL string */
424 if (block_job_is_cancelled(&s
->common
)) {
428 s
->bdev_length
= bdrv_getlength(bs
);
429 if (s
->bdev_length
< 0) {
430 ret
= s
->bdev_length
;
432 } else if (s
->bdev_length
== 0) {
433 /* Report BLOCK_JOB_READY and wait for complete. */
434 block_job_event_ready(&s
->common
);
436 while (!block_job_is_cancelled(&s
->common
) && !s
->should_complete
) {
437 block_job_yield(&s
->common
);
439 s
->common
.cancelled
= false;
443 length
= DIV_ROUND_UP(s
->bdev_length
, s
->granularity
);
444 s
->in_flight_bitmap
= bitmap_new(length
);
446 /* If we have no backing file yet in the destination, we cannot let
447 * the destination do COW. Instead, we copy sectors around the
448 * dirty data if needed. We need a bitmap to do that.
450 bdrv_get_backing_filename(s
->target
, backing_filename
,
451 sizeof(backing_filename
));
452 if (backing_filename
[0] && !s
->target
->backing
) {
453 ret
= bdrv_get_info(s
->target
, &bdi
);
457 if (s
->granularity
< bdi
.cluster_size
) {
458 s
->buf_size
= MAX(s
->buf_size
, bdi
.cluster_size
);
459 s
->cow_bitmap
= bitmap_new(length
);
463 end
= s
->bdev_length
/ BDRV_SECTOR_SIZE
;
464 s
->buf
= qemu_try_blockalign(bs
, s
->buf_size
);
465 if (s
->buf
== NULL
) {
472 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
473 if (!s
->is_none_mode
) {
474 /* First part, loop on the sectors and initialize the dirty bitmap. */
475 BlockDriverState
*base
= s
->base
;
476 bool mark_all_dirty
= s
->base
== NULL
&& !bdrv_has_zero_init(s
->target
);
478 for (sector_num
= 0; sector_num
< end
; ) {
479 /* Just to make sure we are not exceeding int limit. */
480 int nb_sectors
= MIN(INT_MAX
>> BDRV_SECTOR_BITS
,
482 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
484 if (now
- last_pause_ns
> SLICE_TIME
) {
486 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, 0);
489 if (block_job_is_cancelled(&s
->common
)) {
493 ret
= bdrv_is_allocated_above(bs
, base
, sector_num
, nb_sectors
, &n
);
500 if (ret
== 1 || mark_all_dirty
) {
501 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, sector_num
, n
);
507 bdrv_dirty_iter_init(s
->dirty_bitmap
, &s
->hbi
);
509 uint64_t delay_ns
= 0;
511 bool should_complete
;
518 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
519 /* s->common.offset contains the number of bytes already processed so
520 * far, cnt is the number of dirty sectors remaining and
521 * s->sectors_in_flight is the number of sectors currently being
522 * processed; together those are the current total operation length */
523 s
->common
.len
= s
->common
.offset
+
524 (cnt
+ s
->sectors_in_flight
) * BDRV_SECTOR_SIZE
;
526 /* Note that even when no rate limit is applied we need to yield
527 * periodically with no pending I/O so that bdrv_drain_all() returns.
528 * We do so every SLICE_TIME nanoseconds, or when there is an error,
529 * or when the source is clean, whichever comes first.
531 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - last_pause_ns
< SLICE_TIME
&&
532 s
->common
.iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
533 if (s
->in_flight
== MAX_IN_FLIGHT
|| s
->buf_free_count
== 0 ||
534 (cnt
== 0 && s
->in_flight
> 0)) {
535 trace_mirror_yield(s
, s
->in_flight
, s
->buf_free_count
, cnt
);
536 s
->waiting_for_io
= true;
537 qemu_coroutine_yield();
538 s
->waiting_for_io
= false;
540 } else if (cnt
!= 0) {
541 delay_ns
= mirror_iteration(s
);
545 should_complete
= false;
546 if (s
->in_flight
== 0 && cnt
== 0) {
547 trace_mirror_before_flush(s
);
548 ret
= bdrv_flush(s
->target
);
550 if (mirror_error_action(s
, false, -ret
) ==
551 BLOCK_ERROR_ACTION_REPORT
) {
555 /* We're out of the streaming phase. From now on, if the job
556 * is cancelled we will actually complete all pending I/O and
557 * report completion. This way, block-job-cancel will leave
558 * the target in a consistent state.
561 block_job_event_ready(&s
->common
);
565 should_complete
= s
->should_complete
||
566 block_job_is_cancelled(&s
->common
);
567 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
571 if (cnt
== 0 && should_complete
) {
572 /* The dirty bitmap is not updated while operations are pending.
573 * If we're about to exit, wait for pending operations before
574 * calling bdrv_get_dirty_count(bs), or we may exit while the
575 * source has dirty data to copy!
577 * Note that I/O can be submitted by the guest while
578 * mirror_populate runs.
580 trace_mirror_before_drain(s
, cnt
);
582 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
586 trace_mirror_before_sleep(s
, cnt
, s
->synced
, delay_ns
);
588 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
589 if (block_job_is_cancelled(&s
->common
)) {
592 } else if (!should_complete
) {
593 delay_ns
= (s
->in_flight
== 0 && cnt
== 0 ? SLICE_TIME
: 0);
594 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
595 } else if (cnt
== 0) {
596 /* The two disks are in sync. Exit and report successful
599 assert(QLIST_EMPTY(&bs
->tracked_requests
));
600 s
->common
.cancelled
= false;
603 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
607 if (s
->in_flight
> 0) {
608 /* We get here only if something went wrong. Either the job failed,
609 * or it was cancelled prematurely so that we do not guarantee that
610 * the target is a copy of the source.
612 assert(ret
< 0 || (!s
->synced
&& block_job_is_cancelled(&s
->common
)));
616 assert(s
->in_flight
== 0);
618 g_free(s
->cow_bitmap
);
619 g_free(s
->in_flight_bitmap
);
620 bdrv_release_dirty_bitmap(bs
, s
->dirty_bitmap
);
621 if (s
->target
->blk
) {
622 blk_iostatus_disable(s
->target
->blk
);
625 data
= g_malloc(sizeof(*data
));
627 /* Before we switch to target in mirror_exit, make sure data doesn't
629 bdrv_drained_begin(s
->common
.bs
);
630 block_job_defer_to_main_loop(&s
->common
, mirror_exit
, data
);
633 static void mirror_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
635 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
638 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
641 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
644 static void mirror_iostatus_reset(BlockJob
*job
)
646 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
648 if (s
->target
->blk
) {
649 blk_iostatus_reset(s
->target
->blk
);
653 static void mirror_complete(BlockJob
*job
, Error
**errp
)
655 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
656 Error
*local_err
= NULL
;
659 ret
= bdrv_open_backing_file(s
->target
, NULL
, "backing", &local_err
);
661 error_propagate(errp
, local_err
);
665 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->id
);
669 /* check the target bs is not blocked and block all operations on it */
671 AioContext
*replace_aio_context
;
673 s
->to_replace
= bdrv_find_node(s
->replaces
);
674 if (!s
->to_replace
) {
675 error_setg(errp
, "Node name '%s' not found", s
->replaces
);
679 replace_aio_context
= bdrv_get_aio_context(s
->to_replace
);
680 aio_context_acquire(replace_aio_context
);
682 error_setg(&s
->replace_blocker
,
683 "block device is in use by block-job-complete");
684 bdrv_op_block_all(s
->to_replace
, s
->replace_blocker
);
685 bdrv_ref(s
->to_replace
);
687 aio_context_release(replace_aio_context
);
690 s
->should_complete
= true;
691 block_job_enter(&s
->common
);
694 static const BlockJobDriver mirror_job_driver
= {
695 .instance_size
= sizeof(MirrorBlockJob
),
696 .job_type
= BLOCK_JOB_TYPE_MIRROR
,
697 .set_speed
= mirror_set_speed
,
698 .iostatus_reset
= mirror_iostatus_reset
,
699 .complete
= mirror_complete
,
702 static const BlockJobDriver commit_active_job_driver
= {
703 .instance_size
= sizeof(MirrorBlockJob
),
704 .job_type
= BLOCK_JOB_TYPE_COMMIT
,
705 .set_speed
= mirror_set_speed
,
707 = mirror_iostatus_reset
,
708 .complete
= mirror_complete
,
711 static void mirror_start_job(BlockDriverState
*bs
, BlockDriverState
*target
,
712 const char *replaces
,
713 int64_t speed
, uint32_t granularity
,
715 BlockdevOnError on_source_error
,
716 BlockdevOnError on_target_error
,
718 BlockCompletionFunc
*cb
,
719 void *opaque
, Error
**errp
,
720 const BlockJobDriver
*driver
,
721 bool is_none_mode
, BlockDriverState
*base
)
724 BlockDriverState
*replaced_bs
;
726 if (granularity
== 0) {
727 granularity
= bdrv_get_default_bitmap_granularity(target
);
730 assert ((granularity
& (granularity
- 1)) == 0);
732 if ((on_source_error
== BLOCKDEV_ON_ERROR_STOP
||
733 on_source_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
734 (!bs
->blk
|| !blk_iostatus_is_enabled(bs
->blk
))) {
735 error_setg(errp
, QERR_INVALID_PARAMETER
, "on-source-error");
740 error_setg(errp
, "Invalid parameter 'buf-size'");
745 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
748 /* We can't support this case as long as the block layer can't handle
749 * multiple BlockBackends per BlockDriverState. */
751 replaced_bs
= bdrv_lookup_bs(replaces
, replaces
, errp
);
752 if (replaced_bs
== NULL
) {
758 if (replaced_bs
->blk
&& target
->blk
) {
759 error_setg(errp
, "Can't create node with two BlockBackends");
763 s
= block_job_create(driver
, bs
, speed
, cb
, opaque
, errp
);
768 s
->replaces
= g_strdup(replaces
);
769 s
->on_source_error
= on_source_error
;
770 s
->on_target_error
= on_target_error
;
772 s
->is_none_mode
= is_none_mode
;
774 s
->granularity
= granularity
;
775 s
->buf_size
= ROUND_UP(buf_size
, granularity
);
778 s
->dirty_bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
779 if (!s
->dirty_bitmap
) {
781 block_job_unref(&s
->common
);
785 bdrv_op_block_all(s
->target
, s
->common
.blocker
);
787 bdrv_set_enable_write_cache(s
->target
, true);
788 if (s
->target
->blk
) {
789 blk_set_on_error(s
->target
->blk
, on_target_error
, on_target_error
);
790 blk_iostatus_enable(s
->target
->blk
);
792 s
->common
.co
= qemu_coroutine_create(mirror_run
);
793 trace_mirror_start(bs
, s
, s
->common
.co
, opaque
);
794 qemu_coroutine_enter(s
->common
.co
, s
);
797 void mirror_start(BlockDriverState
*bs
, BlockDriverState
*target
,
798 const char *replaces
,
799 int64_t speed
, uint32_t granularity
, int64_t buf_size
,
800 MirrorSyncMode mode
, BlockdevOnError on_source_error
,
801 BlockdevOnError on_target_error
,
803 BlockCompletionFunc
*cb
,
804 void *opaque
, Error
**errp
)
807 BlockDriverState
*base
;
809 if (mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
810 error_setg(errp
, "Sync mode 'incremental' not supported");
813 is_none_mode
= mode
== MIRROR_SYNC_MODE_NONE
;
814 base
= mode
== MIRROR_SYNC_MODE_TOP
? backing_bs(bs
) : NULL
;
815 mirror_start_job(bs
, target
, replaces
,
816 speed
, granularity
, buf_size
,
817 on_source_error
, on_target_error
, unmap
, cb
, opaque
, errp
,
818 &mirror_job_driver
, is_none_mode
, base
);
821 void commit_active_start(BlockDriverState
*bs
, BlockDriverState
*base
,
823 BlockdevOnError on_error
,
824 BlockCompletionFunc
*cb
,
825 void *opaque
, Error
**errp
)
827 int64_t length
, base_length
;
830 Error
*local_err
= NULL
;
832 orig_base_flags
= bdrv_get_flags(base
);
834 if (bdrv_reopen(base
, bs
->open_flags
, errp
)) {
838 length
= bdrv_getlength(bs
);
840 error_setg_errno(errp
, -length
,
841 "Unable to determine length of %s", bs
->filename
);
842 goto error_restore_flags
;
845 base_length
= bdrv_getlength(base
);
846 if (base_length
< 0) {
847 error_setg_errno(errp
, -base_length
,
848 "Unable to determine length of %s", base
->filename
);
849 goto error_restore_flags
;
852 if (length
> base_length
) {
853 ret
= bdrv_truncate(base
, length
);
855 error_setg_errno(errp
, -ret
,
856 "Top image %s is larger than base image %s, and "
857 "resize of base image failed",
858 bs
->filename
, base
->filename
);
859 goto error_restore_flags
;
864 mirror_start_job(bs
, base
, NULL
, speed
, 0, 0,
865 on_error
, on_error
, false, cb
, opaque
, &local_err
,
866 &commit_active_job_driver
, false, base
);
868 error_propagate(errp
, local_err
);
869 goto error_restore_flags
;
875 /* ignore error and errp for bdrv_reopen, because we want to propagate
876 * the original error */
877 bdrv_reopen(base
, orig_base_flags
, NULL
);