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 "sysemu/block-backend.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qemu/ratelimit.h"
20 #include "qemu/bitmap.h"
21 #include "qemu/error-report.h"
23 #define SLICE_TIME 100000000ULL /* ns */
24 #define MAX_IN_FLIGHT 16
25 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
27 /* The mirroring buffer is a list of granularity-sized chunks.
28 * Free chunks are organized in a list.
30 typedef struct MirrorBuffer
{
31 QSIMPLEQ_ENTRY(MirrorBuffer
) next
;
34 typedef struct MirrorBlockJob
{
37 BlockDriverState
*target
;
38 BlockDriverState
*base
;
39 /* The name of the graph node to replace */
41 /* The BDS to replace */
42 BlockDriverState
*to_replace
;
43 /* Used to block operations on the drive-mirror-replace target */
44 Error
*replace_blocker
;
46 BlockdevOnError on_source_error
, on_target_error
;
53 unsigned long *cow_bitmap
;
54 BdrvDirtyBitmap
*dirty_bitmap
;
57 QSIMPLEQ_HEAD(, MirrorBuffer
) buf_free
;
60 unsigned long *in_flight_bitmap
;
62 int sectors_in_flight
;
68 typedef struct MirrorOp
{
75 static BlockErrorAction
mirror_error_action(MirrorBlockJob
*s
, bool read
,
80 return block_job_error_action(&s
->common
, s
->common
.bs
,
81 s
->on_source_error
, true, error
);
83 return block_job_error_action(&s
->common
, s
->target
,
84 s
->on_target_error
, false, error
);
88 static void mirror_iteration_done(MirrorOp
*op
, int ret
)
90 MirrorBlockJob
*s
= op
->s
;
93 int i
, nb_chunks
, sectors_per_chunk
;
95 trace_mirror_iteration_done(s
, op
->sector_num
, op
->nb_sectors
, ret
);
98 s
->sectors_in_flight
-= op
->nb_sectors
;
100 for (i
= 0; i
< op
->qiov
.niov
; i
++) {
101 MirrorBuffer
*buf
= (MirrorBuffer
*) iov
[i
].iov_base
;
102 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, buf
, next
);
106 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
107 chunk_num
= op
->sector_num
/ sectors_per_chunk
;
108 nb_chunks
= op
->nb_sectors
/ sectors_per_chunk
;
109 bitmap_clear(s
->in_flight_bitmap
, chunk_num
, nb_chunks
);
112 bitmap_set(s
->cow_bitmap
, chunk_num
, nb_chunks
);
114 s
->common
.offset
+= (uint64_t)op
->nb_sectors
* BDRV_SECTOR_SIZE
;
117 qemu_iovec_destroy(&op
->qiov
);
120 if (s
->waiting_for_io
) {
121 qemu_coroutine_enter(s
->common
.co
, NULL
);
125 static void mirror_write_complete(void *opaque
, int ret
)
127 MirrorOp
*op
= opaque
;
128 MirrorBlockJob
*s
= op
->s
;
130 BlockErrorAction action
;
132 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, op
->sector_num
, op
->nb_sectors
);
133 action
= mirror_error_action(s
, false, -ret
);
134 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
138 mirror_iteration_done(op
, ret
);
141 static void mirror_read_complete(void *opaque
, int ret
)
143 MirrorOp
*op
= opaque
;
144 MirrorBlockJob
*s
= op
->s
;
146 BlockErrorAction action
;
148 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, op
->sector_num
, op
->nb_sectors
);
149 action
= mirror_error_action(s
, true, -ret
);
150 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
154 mirror_iteration_done(op
, ret
);
157 bdrv_aio_writev(s
->target
, op
->sector_num
, &op
->qiov
, op
->nb_sectors
,
158 mirror_write_complete
, op
);
161 static uint64_t coroutine_fn
mirror_iteration(MirrorBlockJob
*s
)
163 BlockDriverState
*source
= s
->common
.bs
;
164 int nb_sectors
, sectors_per_chunk
, nb_chunks
;
165 int64_t end
, sector_num
, next_chunk
, next_sector
, hbitmap_next_sector
;
166 uint64_t delay_ns
= 0;
171 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
172 if (s
->sector_num
< 0) {
173 bdrv_dirty_iter_init(s
->dirty_bitmap
, &s
->hbi
);
174 s
->sector_num
= hbitmap_iter_next(&s
->hbi
);
175 trace_mirror_restart_iter(s
, bdrv_get_dirty_count(s
->dirty_bitmap
));
176 assert(s
->sector_num
>= 0);
179 hbitmap_next_sector
= s
->sector_num
;
180 sector_num
= s
->sector_num
;
181 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
182 end
= s
->bdev_length
/ BDRV_SECTOR_SIZE
;
184 /* Extend the QEMUIOVector to include all adjacent blocks that will
185 * be copied in this operation.
187 * We have to do this if we have no backing file yet in the destination,
188 * and the cluster size is very large. Then we need to do COW ourselves.
189 * The first time a cluster is copied, copy it entirely. Note that,
190 * because both the granularity and the cluster size are powers of two,
191 * the number of sectors to copy cannot exceed one cluster.
193 * We also want to extend the QEMUIOVector to include more adjacent
194 * dirty blocks if possible, to limit the number of I/O operations and
195 * run efficiently even with a small granularity.
199 next_sector
= sector_num
;
200 next_chunk
= sector_num
/ sectors_per_chunk
;
202 /* Wait for I/O to this cluster (from a previous iteration) to be done. */
203 while (test_bit(next_chunk
, s
->in_flight_bitmap
)) {
204 trace_mirror_yield_in_flight(s
, sector_num
, s
->in_flight
);
205 s
->waiting_for_io
= true;
206 qemu_coroutine_yield();
207 s
->waiting_for_io
= false;
211 int added_sectors
, added_chunks
;
213 if (!bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
) ||
214 test_bit(next_chunk
, s
->in_flight_bitmap
)) {
215 assert(nb_sectors
> 0);
219 added_sectors
= sectors_per_chunk
;
220 if (s
->cow_bitmap
&& !test_bit(next_chunk
, s
->cow_bitmap
)) {
221 bdrv_round_to_clusters(s
->target
,
222 next_sector
, added_sectors
,
223 &next_sector
, &added_sectors
);
225 /* On the first iteration, the rounding may make us copy
226 * sectors before the first dirty one.
228 if (next_sector
< sector_num
) {
229 assert(nb_sectors
== 0);
230 sector_num
= next_sector
;
231 next_chunk
= next_sector
/ sectors_per_chunk
;
235 added_sectors
= MIN(added_sectors
, end
- (sector_num
+ nb_sectors
));
236 added_chunks
= (added_sectors
+ sectors_per_chunk
- 1) / sectors_per_chunk
;
238 /* When doing COW, it may happen that there is not enough space for
239 * a full cluster. Wait if that is the case.
241 while (nb_chunks
== 0 && s
->buf_free_count
< added_chunks
) {
242 trace_mirror_yield_buf_busy(s
, nb_chunks
, s
->in_flight
);
243 s
->waiting_for_io
= true;
244 qemu_coroutine_yield();
245 s
->waiting_for_io
= false;
247 if (s
->buf_free_count
< nb_chunks
+ added_chunks
) {
248 trace_mirror_break_buf_busy(s
, nb_chunks
, s
->in_flight
);
251 if (IOV_MAX
< nb_chunks
+ added_chunks
) {
252 trace_mirror_break_iov_max(s
, nb_chunks
, added_chunks
);
256 /* We have enough free space to copy these sectors. */
257 bitmap_set(s
->in_flight_bitmap
, next_chunk
, added_chunks
);
259 nb_sectors
+= added_sectors
;
260 nb_chunks
+= added_chunks
;
261 next_sector
+= added_sectors
;
262 next_chunk
+= added_chunks
;
263 if (!s
->synced
&& s
->common
.speed
) {
264 delay_ns
= ratelimit_calculate_delay(&s
->limit
, added_sectors
);
266 } while (delay_ns
== 0 && next_sector
< end
);
268 /* Allocate a MirrorOp that is used as an AIO callback. */
269 op
= g_new(MirrorOp
, 1);
271 op
->sector_num
= sector_num
;
272 op
->nb_sectors
= nb_sectors
;
274 /* Now make a QEMUIOVector taking enough granularity-sized chunks
277 qemu_iovec_init(&op
->qiov
, nb_chunks
);
278 next_sector
= sector_num
;
279 while (nb_chunks
-- > 0) {
280 MirrorBuffer
*buf
= QSIMPLEQ_FIRST(&s
->buf_free
);
281 size_t remaining
= (nb_sectors
* BDRV_SECTOR_SIZE
) - op
->qiov
.size
;
283 QSIMPLEQ_REMOVE_HEAD(&s
->buf_free
, next
);
285 qemu_iovec_add(&op
->qiov
, buf
, MIN(s
->granularity
, remaining
));
287 /* Advance the HBitmapIter in parallel, so that we do not examine
288 * the same sector twice.
290 if (next_sector
> hbitmap_next_sector
291 && bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
)) {
292 hbitmap_next_sector
= hbitmap_iter_next(&s
->hbi
);
295 next_sector
+= sectors_per_chunk
;
298 bdrv_reset_dirty_bitmap(s
->dirty_bitmap
, sector_num
, nb_sectors
);
300 /* Copy the dirty cluster. */
302 s
->sectors_in_flight
+= nb_sectors
;
303 trace_mirror_one_iteration(s
, sector_num
, nb_sectors
);
305 ret
= bdrv_get_block_status_above(source
, NULL
, sector_num
,
307 if (ret
< 0 || pnum
< nb_sectors
||
308 (ret
& BDRV_BLOCK_DATA
&& !(ret
& BDRV_BLOCK_ZERO
))) {
309 bdrv_aio_readv(source
, sector_num
, &op
->qiov
, nb_sectors
,
310 mirror_read_complete
, op
);
311 } else if (ret
& BDRV_BLOCK_ZERO
) {
312 bdrv_aio_write_zeroes(s
->target
, sector_num
, op
->nb_sectors
,
313 s
->unmap
? BDRV_REQ_MAY_UNMAP
: 0,
314 mirror_write_complete
, op
);
316 assert(!(ret
& BDRV_BLOCK_DATA
));
317 bdrv_aio_discard(s
->target
, sector_num
, op
->nb_sectors
,
318 mirror_write_complete
, op
);
323 static void mirror_free_init(MirrorBlockJob
*s
)
325 int granularity
= s
->granularity
;
326 size_t buf_size
= s
->buf_size
;
327 uint8_t *buf
= s
->buf
;
329 assert(s
->buf_free_count
== 0);
330 QSIMPLEQ_INIT(&s
->buf_free
);
331 while (buf_size
!= 0) {
332 MirrorBuffer
*cur
= (MirrorBuffer
*)buf
;
333 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, cur
, next
);
335 buf_size
-= granularity
;
340 static void mirror_drain(MirrorBlockJob
*s
)
342 while (s
->in_flight
> 0) {
343 s
->waiting_for_io
= true;
344 qemu_coroutine_yield();
345 s
->waiting_for_io
= false;
353 static void mirror_exit(BlockJob
*job
, void *opaque
)
355 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
356 MirrorExitData
*data
= opaque
;
357 AioContext
*replace_aio_context
= NULL
;
358 BlockDriverState
*src
= s
->common
.bs
;
360 /* Make sure that the source BDS doesn't go away before we called
361 * block_job_completed(). */
365 replace_aio_context
= bdrv_get_aio_context(s
->to_replace
);
366 aio_context_acquire(replace_aio_context
);
369 if (s
->should_complete
&& data
->ret
== 0) {
370 BlockDriverState
*to_replace
= s
->common
.bs
;
372 to_replace
= s
->to_replace
;
375 /* This was checked in mirror_start_job(), but meanwhile one of the
376 * nodes could have been newly attached to a BlockBackend. */
377 if (to_replace
->blk
&& s
->target
->blk
) {
378 error_report("block job: Can't create node with two BlockBackends");
383 if (bdrv_get_flags(s
->target
) != bdrv_get_flags(to_replace
)) {
384 bdrv_reopen(s
->target
, bdrv_get_flags(to_replace
), NULL
);
386 bdrv_replace_in_backing_chain(to_replace
, s
->target
);
391 bdrv_op_unblock_all(s
->to_replace
, s
->replace_blocker
);
392 error_free(s
->replace_blocker
);
393 bdrv_unref(s
->to_replace
);
395 if (replace_aio_context
) {
396 aio_context_release(replace_aio_context
);
399 bdrv_op_unblock_all(s
->target
, s
->common
.blocker
);
400 bdrv_unref(s
->target
);
401 block_job_completed(&s
->common
, data
->ret
);
403 bdrv_drained_end(src
);
407 static void coroutine_fn
mirror_run(void *opaque
)
409 MirrorBlockJob
*s
= opaque
;
410 MirrorExitData
*data
;
411 BlockDriverState
*bs
= s
->common
.bs
;
412 int64_t sector_num
, end
, length
;
413 uint64_t last_pause_ns
;
415 char backing_filename
[2]; /* we only need 2 characters because we are only
416 checking for a NULL string */
420 if (block_job_is_cancelled(&s
->common
)) {
424 s
->bdev_length
= bdrv_getlength(bs
);
425 if (s
->bdev_length
< 0) {
426 ret
= s
->bdev_length
;
428 } else if (s
->bdev_length
== 0) {
429 /* Report BLOCK_JOB_READY and wait for complete. */
430 block_job_event_ready(&s
->common
);
432 while (!block_job_is_cancelled(&s
->common
) && !s
->should_complete
) {
433 block_job_yield(&s
->common
);
435 s
->common
.cancelled
= false;
439 length
= DIV_ROUND_UP(s
->bdev_length
, s
->granularity
);
440 s
->in_flight_bitmap
= bitmap_new(length
);
442 /* If we have no backing file yet in the destination, we cannot let
443 * the destination do COW. Instead, we copy sectors around the
444 * dirty data if needed. We need a bitmap to do that.
446 bdrv_get_backing_filename(s
->target
, backing_filename
,
447 sizeof(backing_filename
));
448 if (backing_filename
[0] && !s
->target
->backing
) {
449 ret
= bdrv_get_info(s
->target
, &bdi
);
453 if (s
->granularity
< bdi
.cluster_size
) {
454 s
->buf_size
= MAX(s
->buf_size
, bdi
.cluster_size
);
455 s
->cow_bitmap
= bitmap_new(length
);
459 end
= s
->bdev_length
/ BDRV_SECTOR_SIZE
;
460 s
->buf
= qemu_try_blockalign(bs
, s
->buf_size
);
461 if (s
->buf
== NULL
) {
468 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
469 if (!s
->is_none_mode
) {
470 /* First part, loop on the sectors and initialize the dirty bitmap. */
471 BlockDriverState
*base
= s
->base
;
472 bool mark_all_dirty
= s
->base
== NULL
&& !bdrv_has_zero_init(s
->target
);
474 for (sector_num
= 0; sector_num
< end
; ) {
475 /* Just to make sure we are not exceeding int limit. */
476 int nb_sectors
= MIN(INT_MAX
>> BDRV_SECTOR_BITS
,
478 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
480 if (now
- last_pause_ns
> SLICE_TIME
) {
482 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, 0);
485 if (block_job_is_cancelled(&s
->common
)) {
489 ret
= bdrv_is_allocated_above(bs
, base
, sector_num
, nb_sectors
, &n
);
496 if (ret
== 1 || mark_all_dirty
) {
497 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, sector_num
, n
);
503 bdrv_dirty_iter_init(s
->dirty_bitmap
, &s
->hbi
);
505 uint64_t delay_ns
= 0;
507 bool should_complete
;
514 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
515 /* s->common.offset contains the number of bytes already processed so
516 * far, cnt is the number of dirty sectors remaining and
517 * s->sectors_in_flight is the number of sectors currently being
518 * processed; together those are the current total operation length */
519 s
->common
.len
= s
->common
.offset
+
520 (cnt
+ s
->sectors_in_flight
) * BDRV_SECTOR_SIZE
;
522 /* Note that even when no rate limit is applied we need to yield
523 * periodically with no pending I/O so that bdrv_drain_all() returns.
524 * We do so every SLICE_TIME nanoseconds, or when there is an error,
525 * or when the source is clean, whichever comes first.
527 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - last_pause_ns
< SLICE_TIME
&&
528 s
->common
.iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
529 if (s
->in_flight
== MAX_IN_FLIGHT
|| s
->buf_free_count
== 0 ||
530 (cnt
== 0 && s
->in_flight
> 0)) {
531 trace_mirror_yield(s
, s
->in_flight
, s
->buf_free_count
, cnt
);
532 s
->waiting_for_io
= true;
533 qemu_coroutine_yield();
534 s
->waiting_for_io
= false;
536 } else if (cnt
!= 0) {
537 delay_ns
= mirror_iteration(s
);
541 should_complete
= false;
542 if (s
->in_flight
== 0 && cnt
== 0) {
543 trace_mirror_before_flush(s
);
544 ret
= bdrv_flush(s
->target
);
546 if (mirror_error_action(s
, false, -ret
) ==
547 BLOCK_ERROR_ACTION_REPORT
) {
551 /* We're out of the streaming phase. From now on, if the job
552 * is cancelled we will actually complete all pending I/O and
553 * report completion. This way, block-job-cancel will leave
554 * the target in a consistent state.
557 block_job_event_ready(&s
->common
);
561 should_complete
= s
->should_complete
||
562 block_job_is_cancelled(&s
->common
);
563 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
567 if (cnt
== 0 && should_complete
) {
568 /* The dirty bitmap is not updated while operations are pending.
569 * If we're about to exit, wait for pending operations before
570 * calling bdrv_get_dirty_count(bs), or we may exit while the
571 * source has dirty data to copy!
573 * Note that I/O can be submitted by the guest while
574 * mirror_populate runs.
576 trace_mirror_before_drain(s
, cnt
);
578 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
582 trace_mirror_before_sleep(s
, cnt
, s
->synced
, delay_ns
);
584 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
585 if (block_job_is_cancelled(&s
->common
)) {
588 } else if (!should_complete
) {
589 delay_ns
= (s
->in_flight
== 0 && cnt
== 0 ? SLICE_TIME
: 0);
590 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
591 } else if (cnt
== 0) {
592 /* The two disks are in sync. Exit and report successful
595 assert(QLIST_EMPTY(&bs
->tracked_requests
));
596 s
->common
.cancelled
= false;
599 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
603 if (s
->in_flight
> 0) {
604 /* We get here only if something went wrong. Either the job failed,
605 * or it was cancelled prematurely so that we do not guarantee that
606 * the target is a copy of the source.
608 assert(ret
< 0 || (!s
->synced
&& block_job_is_cancelled(&s
->common
)));
612 assert(s
->in_flight
== 0);
614 g_free(s
->cow_bitmap
);
615 g_free(s
->in_flight_bitmap
);
616 bdrv_release_dirty_bitmap(bs
, s
->dirty_bitmap
);
617 if (s
->target
->blk
) {
618 blk_iostatus_disable(s
->target
->blk
);
621 data
= g_malloc(sizeof(*data
));
623 /* Before we switch to target in mirror_exit, make sure data doesn't
625 bdrv_drained_begin(s
->common
.bs
);
626 block_job_defer_to_main_loop(&s
->common
, mirror_exit
, data
);
629 static void mirror_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
631 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
634 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
637 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
640 static void mirror_iostatus_reset(BlockJob
*job
)
642 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
644 if (s
->target
->blk
) {
645 blk_iostatus_reset(s
->target
->blk
);
649 static void mirror_complete(BlockJob
*job
, Error
**errp
)
651 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
652 Error
*local_err
= NULL
;
655 ret
= bdrv_open_backing_file(s
->target
, NULL
, "backing", &local_err
);
657 error_propagate(errp
, local_err
);
661 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->id
);
665 /* check the target bs is not blocked and block all operations on it */
667 AioContext
*replace_aio_context
;
669 s
->to_replace
= bdrv_find_node(s
->replaces
);
670 if (!s
->to_replace
) {
671 error_setg(errp
, "Node name '%s' not found", s
->replaces
);
675 replace_aio_context
= bdrv_get_aio_context(s
->to_replace
);
676 aio_context_acquire(replace_aio_context
);
678 error_setg(&s
->replace_blocker
,
679 "block device is in use by block-job-complete");
680 bdrv_op_block_all(s
->to_replace
, s
->replace_blocker
);
681 bdrv_ref(s
->to_replace
);
683 aio_context_release(replace_aio_context
);
686 s
->should_complete
= true;
687 block_job_enter(&s
->common
);
690 static const BlockJobDriver mirror_job_driver
= {
691 .instance_size
= sizeof(MirrorBlockJob
),
692 .job_type
= BLOCK_JOB_TYPE_MIRROR
,
693 .set_speed
= mirror_set_speed
,
694 .iostatus_reset
= mirror_iostatus_reset
,
695 .complete
= mirror_complete
,
698 static const BlockJobDriver commit_active_job_driver
= {
699 .instance_size
= sizeof(MirrorBlockJob
),
700 .job_type
= BLOCK_JOB_TYPE_COMMIT
,
701 .set_speed
= mirror_set_speed
,
703 = mirror_iostatus_reset
,
704 .complete
= mirror_complete
,
707 static void mirror_start_job(BlockDriverState
*bs
, BlockDriverState
*target
,
708 const char *replaces
,
709 int64_t speed
, uint32_t granularity
,
711 BlockdevOnError on_source_error
,
712 BlockdevOnError on_target_error
,
714 BlockCompletionFunc
*cb
,
715 void *opaque
, Error
**errp
,
716 const BlockJobDriver
*driver
,
717 bool is_none_mode
, BlockDriverState
*base
)
720 BlockDriverState
*replaced_bs
;
722 if (granularity
== 0) {
723 granularity
= bdrv_get_default_bitmap_granularity(target
);
726 assert ((granularity
& (granularity
- 1)) == 0);
728 if ((on_source_error
== BLOCKDEV_ON_ERROR_STOP
||
729 on_source_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
730 (!bs
->blk
|| !blk_iostatus_is_enabled(bs
->blk
))) {
731 error_setg(errp
, QERR_INVALID_PARAMETER
, "on-source-error");
736 error_setg(errp
, "Invalid parameter 'buf-size'");
741 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
744 /* We can't support this case as long as the block layer can't handle
745 * multiple BlockBackends per BlockDriverState. */
747 replaced_bs
= bdrv_lookup_bs(replaces
, replaces
, errp
);
748 if (replaced_bs
== NULL
) {
754 if (replaced_bs
->blk
&& target
->blk
) {
755 error_setg(errp
, "Can't create node with two BlockBackends");
759 s
= block_job_create(driver
, bs
, speed
, cb
, opaque
, errp
);
764 s
->replaces
= g_strdup(replaces
);
765 s
->on_source_error
= on_source_error
;
766 s
->on_target_error
= on_target_error
;
768 s
->is_none_mode
= is_none_mode
;
770 s
->granularity
= granularity
;
771 s
->buf_size
= ROUND_UP(buf_size
, granularity
);
774 s
->dirty_bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
775 if (!s
->dirty_bitmap
) {
777 block_job_unref(&s
->common
);
781 bdrv_op_block_all(s
->target
, s
->common
.blocker
);
783 bdrv_set_enable_write_cache(s
->target
, true);
784 if (s
->target
->blk
) {
785 blk_set_on_error(s
->target
->blk
, on_target_error
, on_target_error
);
786 blk_iostatus_enable(s
->target
->blk
);
788 s
->common
.co
= qemu_coroutine_create(mirror_run
);
789 trace_mirror_start(bs
, s
, s
->common
.co
, opaque
);
790 qemu_coroutine_enter(s
->common
.co
, s
);
793 void mirror_start(BlockDriverState
*bs
, BlockDriverState
*target
,
794 const char *replaces
,
795 int64_t speed
, uint32_t granularity
, int64_t buf_size
,
796 MirrorSyncMode mode
, BlockdevOnError on_source_error
,
797 BlockdevOnError on_target_error
,
799 BlockCompletionFunc
*cb
,
800 void *opaque
, Error
**errp
)
803 BlockDriverState
*base
;
805 if (mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
806 error_setg(errp
, "Sync mode 'incremental' not supported");
809 is_none_mode
= mode
== MIRROR_SYNC_MODE_NONE
;
810 base
= mode
== MIRROR_SYNC_MODE_TOP
? backing_bs(bs
) : NULL
;
811 mirror_start_job(bs
, target
, replaces
,
812 speed
, granularity
, buf_size
,
813 on_source_error
, on_target_error
, unmap
, cb
, opaque
, errp
,
814 &mirror_job_driver
, is_none_mode
, base
);
817 void commit_active_start(BlockDriverState
*bs
, BlockDriverState
*base
,
819 BlockdevOnError on_error
,
820 BlockCompletionFunc
*cb
,
821 void *opaque
, Error
**errp
)
823 int64_t length
, base_length
;
826 Error
*local_err
= NULL
;
828 orig_base_flags
= bdrv_get_flags(base
);
830 if (bdrv_reopen(base
, bs
->open_flags
, errp
)) {
834 length
= bdrv_getlength(bs
);
836 error_setg_errno(errp
, -length
,
837 "Unable to determine length of %s", bs
->filename
);
838 goto error_restore_flags
;
841 base_length
= bdrv_getlength(base
);
842 if (base_length
< 0) {
843 error_setg_errno(errp
, -base_length
,
844 "Unable to determine length of %s", base
->filename
);
845 goto error_restore_flags
;
848 if (length
> base_length
) {
849 ret
= bdrv_truncate(base
, length
);
851 error_setg_errno(errp
, -ret
,
852 "Top image %s is larger than base image %s, and "
853 "resize of base image failed",
854 bs
->filename
, base
->filename
);
855 goto error_restore_flags
;
860 mirror_start_job(bs
, base
, NULL
, speed
, 0, 0,
861 on_error
, on_error
, false, cb
, opaque
, &local_err
,
862 &commit_active_job_driver
, false, base
);
864 error_propagate(errp
, local_err
);
865 goto error_restore_flags
;
871 /* ignore error and errp for bdrv_reopen, because we want to propagate
872 * the original error */
873 bdrv_reopen(base
, orig_base_flags
, NULL
);