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/error.h"
20 #include "qapi/qmp/qerror.h"
21 #include "qemu/ratelimit.h"
22 #include "qemu/bitmap.h"
23 #include "qemu/error-report.h"
25 #define SLICE_TIME 100000000ULL /* ns */
26 #define MAX_IN_FLIGHT 16
27 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
29 /* The mirroring buffer is a list of granularity-sized chunks.
30 * Free chunks are organized in a list.
32 typedef struct MirrorBuffer
{
33 QSIMPLEQ_ENTRY(MirrorBuffer
) next
;
36 typedef struct MirrorBlockJob
{
39 BlockDriverState
*target
;
40 BlockDriverState
*base
;
41 /* The name of the graph node to replace */
43 /* The BDS to replace */
44 BlockDriverState
*to_replace
;
45 /* Used to block operations on the drive-mirror-replace target */
46 Error
*replace_blocker
;
48 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
;
67 int target_cluster_sectors
;
71 typedef struct MirrorOp
{
78 static BlockErrorAction
mirror_error_action(MirrorBlockJob
*s
, bool read
,
83 return block_job_error_action(&s
->common
, s
->common
.bs
,
84 s
->on_source_error
, true, error
);
86 return block_job_error_action(&s
->common
, s
->target
,
87 s
->on_target_error
, false, error
);
91 static void mirror_iteration_done(MirrorOp
*op
, int ret
)
93 MirrorBlockJob
*s
= op
->s
;
96 int i
, nb_chunks
, sectors_per_chunk
;
98 trace_mirror_iteration_done(s
, op
->sector_num
, op
->nb_sectors
, ret
);
101 s
->sectors_in_flight
-= op
->nb_sectors
;
103 for (i
= 0; i
< op
->qiov
.niov
; i
++) {
104 MirrorBuffer
*buf
= (MirrorBuffer
*) iov
[i
].iov_base
;
105 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, buf
, next
);
109 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
110 chunk_num
= op
->sector_num
/ sectors_per_chunk
;
111 nb_chunks
= op
->nb_sectors
/ sectors_per_chunk
;
112 bitmap_clear(s
->in_flight_bitmap
, chunk_num
, nb_chunks
);
115 bitmap_set(s
->cow_bitmap
, chunk_num
, nb_chunks
);
117 s
->common
.offset
+= (uint64_t)op
->nb_sectors
* BDRV_SECTOR_SIZE
;
120 qemu_iovec_destroy(&op
->qiov
);
123 if (s
->waiting_for_io
) {
124 qemu_coroutine_enter(s
->common
.co
, NULL
);
128 static void mirror_write_complete(void *opaque
, int ret
)
130 MirrorOp
*op
= opaque
;
131 MirrorBlockJob
*s
= op
->s
;
133 BlockErrorAction action
;
135 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, op
->sector_num
, op
->nb_sectors
);
136 action
= mirror_error_action(s
, false, -ret
);
137 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
141 mirror_iteration_done(op
, ret
);
144 static void mirror_read_complete(void *opaque
, int ret
)
146 MirrorOp
*op
= opaque
;
147 MirrorBlockJob
*s
= op
->s
;
149 BlockErrorAction action
;
151 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, op
->sector_num
, op
->nb_sectors
);
152 action
= mirror_error_action(s
, true, -ret
);
153 if (action
== BLOCK_ERROR_ACTION_REPORT
&& s
->ret
>= 0) {
157 mirror_iteration_done(op
, ret
);
160 bdrv_aio_writev(s
->target
, op
->sector_num
, &op
->qiov
, op
->nb_sectors
,
161 mirror_write_complete
, op
);
164 /* Round sector_num and/or nb_sectors to target cluster if COW is needed, and
165 * return the offset of the adjusted tail sector against original. */
166 static int mirror_cow_align(MirrorBlockJob
*s
,
172 int chunk_sectors
= s
->granularity
>> BDRV_SECTOR_BITS
;
173 int64_t align_sector_num
= *sector_num
;
174 int align_nb_sectors
= *nb_sectors
;
175 int max_sectors
= chunk_sectors
* s
->max_iov
;
177 need_cow
= !test_bit(*sector_num
/ chunk_sectors
, s
->cow_bitmap
);
178 need_cow
|= !test_bit((*sector_num
+ *nb_sectors
- 1) / chunk_sectors
,
181 bdrv_round_to_clusters(s
->target
, *sector_num
, *nb_sectors
,
182 &align_sector_num
, &align_nb_sectors
);
185 if (align_nb_sectors
> max_sectors
) {
186 align_nb_sectors
= max_sectors
;
188 align_nb_sectors
= QEMU_ALIGN_DOWN(align_nb_sectors
,
189 s
->target_cluster_sectors
);
193 ret
= align_sector_num
+ align_nb_sectors
- (*sector_num
+ *nb_sectors
);
194 *sector_num
= align_sector_num
;
195 *nb_sectors
= align_nb_sectors
;
200 static inline void mirror_wait_for_io(MirrorBlockJob
*s
)
202 assert(!s
->waiting_for_io
);
203 s
->waiting_for_io
= true;
204 qemu_coroutine_yield();
205 s
->waiting_for_io
= false;
208 /* Submit async read while handling COW.
209 * Returns: nb_sectors if no alignment is necessary, or
210 * (new_end - sector_num) if tail is rounded up or down due to
211 * alignment or buffer limit.
213 static int mirror_do_read(MirrorBlockJob
*s
, int64_t sector_num
,
216 BlockDriverState
*source
= s
->common
.bs
;
217 int sectors_per_chunk
, nb_chunks
;
218 int ret
= nb_sectors
;
221 sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
223 /* We can only handle as much as buf_size at a time. */
224 nb_sectors
= MIN(s
->buf_size
>> BDRV_SECTOR_BITS
, nb_sectors
);
228 ret
+= mirror_cow_align(s
, §or_num
, &nb_sectors
);
230 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= s
->buf_size
);
231 /* The sector range must meet granularity because:
232 * 1) Caller passes in aligned values;
233 * 2) mirror_cow_align is used only when target cluster is larger. */
234 assert(!(nb_sectors
% sectors_per_chunk
));
235 assert(!(sector_num
% sectors_per_chunk
));
236 nb_chunks
= nb_sectors
/ sectors_per_chunk
;
238 while (s
->buf_free_count
< nb_chunks
) {
239 trace_mirror_yield_in_flight(s
, sector_num
, s
->in_flight
);
240 mirror_wait_for_io(s
);
243 /* Allocate a MirrorOp that is used as an AIO callback. */
244 op
= g_new(MirrorOp
, 1);
246 op
->sector_num
= sector_num
;
247 op
->nb_sectors
= nb_sectors
;
249 /* Now make a QEMUIOVector taking enough granularity-sized chunks
252 qemu_iovec_init(&op
->qiov
, nb_chunks
);
253 while (nb_chunks
-- > 0) {
254 MirrorBuffer
*buf
= QSIMPLEQ_FIRST(&s
->buf_free
);
255 size_t remaining
= nb_sectors
* BDRV_SECTOR_SIZE
- op
->qiov
.size
;
257 QSIMPLEQ_REMOVE_HEAD(&s
->buf_free
, next
);
259 qemu_iovec_add(&op
->qiov
, buf
, MIN(s
->granularity
, remaining
));
262 /* Copy the dirty cluster. */
264 s
->sectors_in_flight
+= nb_sectors
;
265 trace_mirror_one_iteration(s
, sector_num
, nb_sectors
);
267 bdrv_aio_readv(source
, sector_num
, &op
->qiov
, nb_sectors
,
268 mirror_read_complete
, op
);
272 static void mirror_do_zero_or_discard(MirrorBlockJob
*s
,
279 /* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed
280 * so the freeing in mirror_iteration_done is nop. */
281 op
= g_new0(MirrorOp
, 1);
283 op
->sector_num
= sector_num
;
284 op
->nb_sectors
= nb_sectors
;
287 s
->sectors_in_flight
+= nb_sectors
;
289 bdrv_aio_discard(s
->target
, sector_num
, op
->nb_sectors
,
290 mirror_write_complete
, op
);
292 bdrv_aio_write_zeroes(s
->target
, sector_num
, op
->nb_sectors
,
293 s
->unmap
? BDRV_REQ_MAY_UNMAP
: 0,
294 mirror_write_complete
, op
);
298 static uint64_t coroutine_fn
mirror_iteration(MirrorBlockJob
*s
)
300 BlockDriverState
*source
= s
->common
.bs
;
301 int64_t sector_num
, first_chunk
;
302 uint64_t delay_ns
= 0;
303 /* At least the first dirty chunk is mirrored in one iteration. */
305 int64_t end
= s
->bdev_length
/ BDRV_SECTOR_SIZE
;
306 int sectors_per_chunk
= s
->granularity
>> BDRV_SECTOR_BITS
;
308 sector_num
= hbitmap_iter_next(&s
->hbi
);
309 if (sector_num
< 0) {
310 bdrv_dirty_iter_init(s
->dirty_bitmap
, &s
->hbi
);
311 sector_num
= hbitmap_iter_next(&s
->hbi
);
312 trace_mirror_restart_iter(s
, bdrv_get_dirty_count(s
->dirty_bitmap
));
313 assert(sector_num
>= 0);
316 first_chunk
= sector_num
/ sectors_per_chunk
;
317 while (test_bit(first_chunk
, s
->in_flight_bitmap
)) {
318 trace_mirror_yield_in_flight(s
, first_chunk
, s
->in_flight
);
319 mirror_wait_for_io(s
);
322 /* Find the number of consective dirty chunks following the first dirty
323 * one, and wait for in flight requests in them. */
324 while (nb_chunks
* sectors_per_chunk
< (s
->buf_size
>> BDRV_SECTOR_BITS
)) {
325 int64_t hbitmap_next
;
326 int64_t next_sector
= sector_num
+ nb_chunks
* sectors_per_chunk
;
327 int64_t next_chunk
= next_sector
/ sectors_per_chunk
;
328 if (next_sector
>= end
||
329 !bdrv_get_dirty(source
, s
->dirty_bitmap
, next_sector
)) {
332 if (test_bit(next_chunk
, s
->in_flight_bitmap
)) {
336 hbitmap_next
= hbitmap_iter_next(&s
->hbi
);
337 if (hbitmap_next
> next_sector
|| hbitmap_next
< 0) {
338 /* The bitmap iterator's cache is stale, refresh it */
339 bdrv_set_dirty_iter(&s
->hbi
, next_sector
);
340 hbitmap_next
= hbitmap_iter_next(&s
->hbi
);
342 assert(hbitmap_next
== next_sector
);
346 /* Clear dirty bits before querying the block status, because
347 * calling bdrv_get_block_status_above could yield - if some blocks are
348 * marked dirty in this window, we need to know.
350 bdrv_reset_dirty_bitmap(s
->dirty_bitmap
, sector_num
,
351 nb_chunks
* sectors_per_chunk
);
352 bitmap_set(s
->in_flight_bitmap
, sector_num
/ sectors_per_chunk
, nb_chunks
);
353 while (nb_chunks
> 0 && sector_num
< end
) {
356 BlockDriverState
*file
;
360 MIRROR_METHOD_DISCARD
361 } mirror_method
= MIRROR_METHOD_COPY
;
363 assert(!(sector_num
% sectors_per_chunk
));
364 ret
= bdrv_get_block_status_above(source
, NULL
, sector_num
,
365 nb_chunks
* sectors_per_chunk
,
368 io_sectors
= nb_chunks
* sectors_per_chunk
;
371 io_sectors
-= io_sectors
% sectors_per_chunk
;
372 if (io_sectors
< sectors_per_chunk
) {
373 io_sectors
= sectors_per_chunk
;
374 } else if (ret
>= 0 && !(ret
& BDRV_BLOCK_DATA
)) {
375 int64_t target_sector_num
;
376 int target_nb_sectors
;
377 bdrv_round_to_clusters(s
->target
, sector_num
, io_sectors
,
378 &target_sector_num
, &target_nb_sectors
);
379 if (target_sector_num
== sector_num
&&
380 target_nb_sectors
== io_sectors
) {
381 mirror_method
= ret
& BDRV_BLOCK_ZERO
?
383 MIRROR_METHOD_DISCARD
;
387 switch (mirror_method
) {
388 case MIRROR_METHOD_COPY
:
389 io_sectors
= mirror_do_read(s
, sector_num
, io_sectors
);
391 case MIRROR_METHOD_ZERO
:
392 mirror_do_zero_or_discard(s
, sector_num
, io_sectors
, false);
394 case MIRROR_METHOD_DISCARD
:
395 mirror_do_zero_or_discard(s
, sector_num
, io_sectors
, true);
401 sector_num
+= io_sectors
;
402 nb_chunks
-= io_sectors
/ sectors_per_chunk
;
403 delay_ns
+= ratelimit_calculate_delay(&s
->limit
, io_sectors
);
408 static void mirror_free_init(MirrorBlockJob
*s
)
410 int granularity
= s
->granularity
;
411 size_t buf_size
= s
->buf_size
;
412 uint8_t *buf
= s
->buf
;
414 assert(s
->buf_free_count
== 0);
415 QSIMPLEQ_INIT(&s
->buf_free
);
416 while (buf_size
!= 0) {
417 MirrorBuffer
*cur
= (MirrorBuffer
*)buf
;
418 QSIMPLEQ_INSERT_TAIL(&s
->buf_free
, cur
, next
);
420 buf_size
-= granularity
;
425 static void mirror_drain(MirrorBlockJob
*s
)
427 while (s
->in_flight
> 0) {
428 mirror_wait_for_io(s
);
436 static void mirror_exit(BlockJob
*job
, void *opaque
)
438 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
439 MirrorExitData
*data
= opaque
;
440 AioContext
*replace_aio_context
= NULL
;
441 BlockDriverState
*src
= s
->common
.bs
;
443 /* Make sure that the source BDS doesn't go away before we called
444 * block_job_completed(). */
448 replace_aio_context
= bdrv_get_aio_context(s
->to_replace
);
449 aio_context_acquire(replace_aio_context
);
452 if (s
->should_complete
&& data
->ret
== 0) {
453 BlockDriverState
*to_replace
= s
->common
.bs
;
455 to_replace
= s
->to_replace
;
458 /* This was checked in mirror_start_job(), but meanwhile one of the
459 * nodes could have been newly attached to a BlockBackend. */
460 if (to_replace
->blk
&& s
->target
->blk
) {
461 error_report("block job: Can't create node with two BlockBackends");
466 if (bdrv_get_flags(s
->target
) != bdrv_get_flags(to_replace
)) {
467 bdrv_reopen(s
->target
, bdrv_get_flags(to_replace
), NULL
);
469 bdrv_replace_in_backing_chain(to_replace
, s
->target
);
474 bdrv_op_unblock_all(s
->to_replace
, s
->replace_blocker
);
475 error_free(s
->replace_blocker
);
476 bdrv_unref(s
->to_replace
);
478 if (replace_aio_context
) {
479 aio_context_release(replace_aio_context
);
482 bdrv_op_unblock_all(s
->target
, s
->common
.blocker
);
483 bdrv_unref(s
->target
);
484 block_job_completed(&s
->common
, data
->ret
);
486 bdrv_drained_end(src
);
490 static void coroutine_fn
mirror_run(void *opaque
)
492 MirrorBlockJob
*s
= opaque
;
493 MirrorExitData
*data
;
494 BlockDriverState
*bs
= s
->common
.bs
;
495 int64_t sector_num
, end
, length
;
496 uint64_t last_pause_ns
;
498 char backing_filename
[2]; /* we only need 2 characters because we are only
499 checking for a NULL string */
502 int target_cluster_size
= BDRV_SECTOR_SIZE
;
504 if (block_job_is_cancelled(&s
->common
)) {
508 s
->bdev_length
= bdrv_getlength(bs
);
509 if (s
->bdev_length
< 0) {
510 ret
= s
->bdev_length
;
512 } else if (s
->bdev_length
== 0) {
513 /* Report BLOCK_JOB_READY and wait for complete. */
514 block_job_event_ready(&s
->common
);
516 while (!block_job_is_cancelled(&s
->common
) && !s
->should_complete
) {
517 block_job_yield(&s
->common
);
519 s
->common
.cancelled
= false;
523 length
= DIV_ROUND_UP(s
->bdev_length
, s
->granularity
);
524 s
->in_flight_bitmap
= bitmap_new(length
);
526 /* If we have no backing file yet in the destination, we cannot let
527 * the destination do COW. Instead, we copy sectors around the
528 * dirty data if needed. We need a bitmap to do that.
530 bdrv_get_backing_filename(s
->target
, backing_filename
,
531 sizeof(backing_filename
));
532 if (!bdrv_get_info(s
->target
, &bdi
) && bdi
.cluster_size
) {
533 target_cluster_size
= bdi
.cluster_size
;
535 if (backing_filename
[0] && !s
->target
->backing
536 && s
->granularity
< target_cluster_size
) {
537 s
->buf_size
= MAX(s
->buf_size
, target_cluster_size
);
538 s
->cow_bitmap
= bitmap_new(length
);
540 s
->target_cluster_sectors
= target_cluster_size
>> BDRV_SECTOR_BITS
;
541 s
->max_iov
= MIN(s
->common
.bs
->bl
.max_iov
, s
->target
->bl
.max_iov
);
543 end
= s
->bdev_length
/ BDRV_SECTOR_SIZE
;
544 s
->buf
= qemu_try_blockalign(bs
, s
->buf_size
);
545 if (s
->buf
== NULL
) {
552 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
553 if (!s
->is_none_mode
) {
554 /* First part, loop on the sectors and initialize the dirty bitmap. */
555 BlockDriverState
*base
= s
->base
;
556 bool mark_all_dirty
= s
->base
== NULL
&& !bdrv_has_zero_init(s
->target
);
558 for (sector_num
= 0; sector_num
< end
; ) {
559 /* Just to make sure we are not exceeding int limit. */
560 int nb_sectors
= MIN(INT_MAX
>> BDRV_SECTOR_BITS
,
562 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
564 if (now
- last_pause_ns
> SLICE_TIME
) {
566 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, 0);
569 if (block_job_is_cancelled(&s
->common
)) {
573 ret
= bdrv_is_allocated_above(bs
, base
, sector_num
, nb_sectors
, &n
);
580 if (ret
== 1 || mark_all_dirty
) {
581 bdrv_set_dirty_bitmap(s
->dirty_bitmap
, sector_num
, n
);
587 bdrv_dirty_iter_init(s
->dirty_bitmap
, &s
->hbi
);
589 uint64_t delay_ns
= 0;
591 bool should_complete
;
598 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
599 /* s->common.offset contains the number of bytes already processed so
600 * far, cnt is the number of dirty sectors remaining and
601 * s->sectors_in_flight is the number of sectors currently being
602 * processed; together those are the current total operation length */
603 s
->common
.len
= s
->common
.offset
+
604 (cnt
+ s
->sectors_in_flight
) * BDRV_SECTOR_SIZE
;
606 /* Note that even when no rate limit is applied we need to yield
607 * periodically with no pending I/O so that bdrv_drain_all() returns.
608 * We do so every SLICE_TIME nanoseconds, or when there is an error,
609 * or when the source is clean, whichever comes first.
611 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - last_pause_ns
< SLICE_TIME
&&
612 s
->common
.iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
613 if (s
->in_flight
== MAX_IN_FLIGHT
|| s
->buf_free_count
== 0 ||
614 (cnt
== 0 && s
->in_flight
> 0)) {
615 trace_mirror_yield(s
, s
->in_flight
, s
->buf_free_count
, cnt
);
616 mirror_wait_for_io(s
);
618 } else if (cnt
!= 0) {
619 delay_ns
= mirror_iteration(s
);
623 should_complete
= false;
624 if (s
->in_flight
== 0 && cnt
== 0) {
625 trace_mirror_before_flush(s
);
626 ret
= bdrv_flush(s
->target
);
628 if (mirror_error_action(s
, false, -ret
) ==
629 BLOCK_ERROR_ACTION_REPORT
) {
633 /* We're out of the streaming phase. From now on, if the job
634 * is cancelled we will actually complete all pending I/O and
635 * report completion. This way, block-job-cancel will leave
636 * the target in a consistent state.
639 block_job_event_ready(&s
->common
);
643 should_complete
= s
->should_complete
||
644 block_job_is_cancelled(&s
->common
);
645 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
649 if (cnt
== 0 && should_complete
) {
650 /* The dirty bitmap is not updated while operations are pending.
651 * If we're about to exit, wait for pending operations before
652 * calling bdrv_get_dirty_count(bs), or we may exit while the
653 * source has dirty data to copy!
655 * Note that I/O can be submitted by the guest while
656 * mirror_populate runs.
658 trace_mirror_before_drain(s
, cnt
);
660 cnt
= bdrv_get_dirty_count(s
->dirty_bitmap
);
664 trace_mirror_before_sleep(s
, cnt
, s
->synced
, delay_ns
);
666 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
667 if (block_job_is_cancelled(&s
->common
)) {
670 } else if (!should_complete
) {
671 delay_ns
= (s
->in_flight
== 0 && cnt
== 0 ? SLICE_TIME
: 0);
672 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
673 } else if (cnt
== 0) {
674 /* The two disks are in sync. Exit and report successful
677 assert(QLIST_EMPTY(&bs
->tracked_requests
));
678 s
->common
.cancelled
= false;
681 last_pause_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
685 if (s
->in_flight
> 0) {
686 /* We get here only if something went wrong. Either the job failed,
687 * or it was cancelled prematurely so that we do not guarantee that
688 * the target is a copy of the source.
690 assert(ret
< 0 || (!s
->synced
&& block_job_is_cancelled(&s
->common
)));
694 assert(s
->in_flight
== 0);
696 g_free(s
->cow_bitmap
);
697 g_free(s
->in_flight_bitmap
);
698 bdrv_release_dirty_bitmap(bs
, s
->dirty_bitmap
);
699 if (s
->target
->blk
) {
700 blk_iostatus_disable(s
->target
->blk
);
703 data
= g_malloc(sizeof(*data
));
705 /* Before we switch to target in mirror_exit, make sure data doesn't
707 bdrv_drained_begin(s
->common
.bs
);
708 block_job_defer_to_main_loop(&s
->common
, mirror_exit
, data
);
711 static void mirror_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
713 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
716 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
719 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
722 static void mirror_iostatus_reset(BlockJob
*job
)
724 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
726 if (s
->target
->blk
) {
727 blk_iostatus_reset(s
->target
->blk
);
731 static void mirror_complete(BlockJob
*job
, Error
**errp
)
733 MirrorBlockJob
*s
= container_of(job
, MirrorBlockJob
, common
);
734 Error
*local_err
= NULL
;
737 ret
= bdrv_open_backing_file(s
->target
, NULL
, "backing", &local_err
);
739 error_propagate(errp
, local_err
);
743 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->id
);
747 /* check the target bs is not blocked and block all operations on it */
749 AioContext
*replace_aio_context
;
751 s
->to_replace
= bdrv_find_node(s
->replaces
);
752 if (!s
->to_replace
) {
753 error_setg(errp
, "Node name '%s' not found", s
->replaces
);
757 replace_aio_context
= bdrv_get_aio_context(s
->to_replace
);
758 aio_context_acquire(replace_aio_context
);
760 error_setg(&s
->replace_blocker
,
761 "block device is in use by block-job-complete");
762 bdrv_op_block_all(s
->to_replace
, s
->replace_blocker
);
763 bdrv_ref(s
->to_replace
);
765 aio_context_release(replace_aio_context
);
768 s
->should_complete
= true;
769 block_job_enter(&s
->common
);
772 static const BlockJobDriver mirror_job_driver
= {
773 .instance_size
= sizeof(MirrorBlockJob
),
774 .job_type
= BLOCK_JOB_TYPE_MIRROR
,
775 .set_speed
= mirror_set_speed
,
776 .iostatus_reset
= mirror_iostatus_reset
,
777 .complete
= mirror_complete
,
780 static const BlockJobDriver commit_active_job_driver
= {
781 .instance_size
= sizeof(MirrorBlockJob
),
782 .job_type
= BLOCK_JOB_TYPE_COMMIT
,
783 .set_speed
= mirror_set_speed
,
785 = mirror_iostatus_reset
,
786 .complete
= mirror_complete
,
789 static void mirror_start_job(BlockDriverState
*bs
, BlockDriverState
*target
,
790 const char *replaces
,
791 int64_t speed
, uint32_t granularity
,
793 BlockdevOnError on_source_error
,
794 BlockdevOnError on_target_error
,
796 BlockCompletionFunc
*cb
,
797 void *opaque
, Error
**errp
,
798 const BlockJobDriver
*driver
,
799 bool is_none_mode
, BlockDriverState
*base
)
802 BlockDriverState
*replaced_bs
;
804 if (granularity
== 0) {
805 granularity
= bdrv_get_default_bitmap_granularity(target
);
808 assert ((granularity
& (granularity
- 1)) == 0);
810 if ((on_source_error
== BLOCKDEV_ON_ERROR_STOP
||
811 on_source_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
812 (!bs
->blk
|| !blk_iostatus_is_enabled(bs
->blk
))) {
813 error_setg(errp
, QERR_INVALID_PARAMETER
, "on-source-error");
818 error_setg(errp
, "Invalid parameter 'buf-size'");
823 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
826 /* We can't support this case as long as the block layer can't handle
827 * multiple BlockBackends per BlockDriverState. */
829 replaced_bs
= bdrv_lookup_bs(replaces
, replaces
, errp
);
830 if (replaced_bs
== NULL
) {
836 if (replaced_bs
->blk
&& target
->blk
) {
837 error_setg(errp
, "Can't create node with two BlockBackends");
841 s
= block_job_create(driver
, bs
, speed
, cb
, opaque
, errp
);
846 s
->replaces
= g_strdup(replaces
);
847 s
->on_source_error
= on_source_error
;
848 s
->on_target_error
= on_target_error
;
850 s
->is_none_mode
= is_none_mode
;
852 s
->granularity
= granularity
;
853 s
->buf_size
= ROUND_UP(buf_size
, granularity
);
856 s
->dirty_bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
857 if (!s
->dirty_bitmap
) {
859 block_job_unref(&s
->common
);
863 bdrv_op_block_all(s
->target
, s
->common
.blocker
);
865 if (s
->target
->blk
) {
866 blk_set_on_error(s
->target
->blk
, on_target_error
, on_target_error
);
867 blk_iostatus_enable(s
->target
->blk
);
869 s
->common
.co
= qemu_coroutine_create(mirror_run
);
870 trace_mirror_start(bs
, s
, s
->common
.co
, opaque
);
871 qemu_coroutine_enter(s
->common
.co
, s
);
874 void mirror_start(BlockDriverState
*bs
, BlockDriverState
*target
,
875 const char *replaces
,
876 int64_t speed
, uint32_t granularity
, int64_t buf_size
,
877 MirrorSyncMode mode
, BlockdevOnError on_source_error
,
878 BlockdevOnError on_target_error
,
880 BlockCompletionFunc
*cb
,
881 void *opaque
, Error
**errp
)
884 BlockDriverState
*base
;
886 if (mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
887 error_setg(errp
, "Sync mode 'incremental' not supported");
890 is_none_mode
= mode
== MIRROR_SYNC_MODE_NONE
;
891 base
= mode
== MIRROR_SYNC_MODE_TOP
? backing_bs(bs
) : NULL
;
892 mirror_start_job(bs
, target
, replaces
,
893 speed
, granularity
, buf_size
,
894 on_source_error
, on_target_error
, unmap
, cb
, opaque
, errp
,
895 &mirror_job_driver
, is_none_mode
, base
);
898 void commit_active_start(BlockDriverState
*bs
, BlockDriverState
*base
,
900 BlockdevOnError on_error
,
901 BlockCompletionFunc
*cb
,
902 void *opaque
, Error
**errp
)
904 int64_t length
, base_length
;
907 Error
*local_err
= NULL
;
909 orig_base_flags
= bdrv_get_flags(base
);
911 if (bdrv_reopen(base
, bs
->open_flags
, errp
)) {
915 length
= bdrv_getlength(bs
);
917 error_setg_errno(errp
, -length
,
918 "Unable to determine length of %s", bs
->filename
);
919 goto error_restore_flags
;
922 base_length
= bdrv_getlength(base
);
923 if (base_length
< 0) {
924 error_setg_errno(errp
, -base_length
,
925 "Unable to determine length of %s", base
->filename
);
926 goto error_restore_flags
;
929 if (length
> base_length
) {
930 ret
= bdrv_truncate(base
, length
);
932 error_setg_errno(errp
, -ret
,
933 "Top image %s is larger than base image %s, and "
934 "resize of base image failed",
935 bs
->filename
, base
->filename
);
936 goto error_restore_flags
;
941 mirror_start_job(bs
, base
, NULL
, speed
, 0, 0,
942 on_error
, on_error
, false, cb
, opaque
, &local_err
,
943 &commit_active_job_driver
, false, base
);
945 error_propagate(errp
, local_err
);
946 goto error_restore_flags
;
952 /* ignore error and errp for bdrv_reopen, because we want to propagate
953 * the original error */
954 bdrv_reopen(base
, orig_base_flags
, NULL
);