mirror: create mirror_throttle helper
[qemu/kevin.git] / block / mirror.c
blob5fde13b1f5dd952599f9fb2b92edd1fd2fc30cea
1 /*
2 * Image mirroring
4 * Copyright Red Hat, Inc. 2012
6 * Authors:
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"
15 #include "trace.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"
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;
33 } MirrorBuffer;
35 typedef struct MirrorBlockJob {
36 BlockJob common;
37 RateLimit limit;
38 BlockBackend *target;
39 BlockDriverState *base;
40 /* The name of the graph node to replace */
41 char *replaces;
42 /* The BDS to replace */
43 BlockDriverState *to_replace;
44 /* Used to block operations on the drive-mirror-replace target */
45 Error *replace_blocker;
46 bool is_none_mode;
47 BlockMirrorBackingMode backing_mode;
48 BlockdevOnError on_source_error, on_target_error;
49 bool synced;
50 bool should_complete;
51 int64_t granularity;
52 size_t buf_size;
53 int64_t bdev_length;
54 unsigned long *cow_bitmap;
55 BdrvDirtyBitmap *dirty_bitmap;
56 HBitmapIter hbi;
57 uint8_t *buf;
58 QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
59 int buf_free_count;
61 uint64_t last_pause_ns;
62 unsigned long *in_flight_bitmap;
63 int in_flight;
64 int64_t sectors_in_flight;
65 int ret;
66 bool unmap;
67 bool waiting_for_io;
68 int target_cluster_sectors;
69 int max_iov;
70 } MirrorBlockJob;
72 typedef struct MirrorOp {
73 MirrorBlockJob *s;
74 QEMUIOVector qiov;
75 int64_t sector_num;
76 int nb_sectors;
77 } MirrorOp;
79 static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
80 int error)
82 s->synced = false;
83 if (read) {
84 return block_job_error_action(&s->common, s->on_source_error,
85 true, error);
86 } else {
87 return block_job_error_action(&s->common, s->on_target_error,
88 false, error);
92 static void mirror_iteration_done(MirrorOp *op, int ret)
94 MirrorBlockJob *s = op->s;
95 struct iovec *iov;
96 int64_t chunk_num;
97 int i, nb_chunks, sectors_per_chunk;
99 trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
101 s->in_flight--;
102 s->sectors_in_flight -= op->nb_sectors;
103 iov = op->qiov.iov;
104 for (i = 0; i < op->qiov.niov; i++) {
105 MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
106 QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
107 s->buf_free_count++;
110 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
111 chunk_num = op->sector_num / sectors_per_chunk;
112 nb_chunks = DIV_ROUND_UP(op->nb_sectors, sectors_per_chunk);
113 bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
114 if (ret >= 0) {
115 if (s->cow_bitmap) {
116 bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
118 s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE;
121 qemu_iovec_destroy(&op->qiov);
122 g_free(op);
124 if (s->waiting_for_io) {
125 qemu_coroutine_enter(s->common.co);
129 static void mirror_write_complete(void *opaque, int ret)
131 MirrorOp *op = opaque;
132 MirrorBlockJob *s = op->s;
133 if (ret < 0) {
134 BlockErrorAction action;
136 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
137 action = mirror_error_action(s, false, -ret);
138 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
139 s->ret = ret;
142 mirror_iteration_done(op, ret);
145 static void mirror_read_complete(void *opaque, int ret)
147 MirrorOp *op = opaque;
148 MirrorBlockJob *s = op->s;
149 if (ret < 0) {
150 BlockErrorAction action;
152 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
153 action = mirror_error_action(s, true, -ret);
154 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
155 s->ret = ret;
158 mirror_iteration_done(op, ret);
159 return;
161 blk_aio_pwritev(s->target, op->sector_num * BDRV_SECTOR_SIZE, &op->qiov,
162 0, mirror_write_complete, op);
165 static inline void mirror_clip_sectors(MirrorBlockJob *s,
166 int64_t sector_num,
167 int *nb_sectors)
169 *nb_sectors = MIN(*nb_sectors,
170 s->bdev_length / BDRV_SECTOR_SIZE - sector_num);
173 /* Round sector_num and/or nb_sectors to target cluster if COW is needed, and
174 * return the offset of the adjusted tail sector against original. */
175 static int mirror_cow_align(MirrorBlockJob *s,
176 int64_t *sector_num,
177 int *nb_sectors)
179 bool need_cow;
180 int ret = 0;
181 int chunk_sectors = s->granularity >> BDRV_SECTOR_BITS;
182 int64_t align_sector_num = *sector_num;
183 int align_nb_sectors = *nb_sectors;
184 int max_sectors = chunk_sectors * s->max_iov;
186 need_cow = !test_bit(*sector_num / chunk_sectors, s->cow_bitmap);
187 need_cow |= !test_bit((*sector_num + *nb_sectors - 1) / chunk_sectors,
188 s->cow_bitmap);
189 if (need_cow) {
190 bdrv_round_sectors_to_clusters(blk_bs(s->target), *sector_num,
191 *nb_sectors, &align_sector_num,
192 &align_nb_sectors);
195 if (align_nb_sectors > max_sectors) {
196 align_nb_sectors = max_sectors;
197 if (need_cow) {
198 align_nb_sectors = QEMU_ALIGN_DOWN(align_nb_sectors,
199 s->target_cluster_sectors);
202 /* Clipping may result in align_nb_sectors unaligned to chunk boundary, but
203 * that doesn't matter because it's already the end of source image. */
204 mirror_clip_sectors(s, align_sector_num, &align_nb_sectors);
206 ret = align_sector_num + align_nb_sectors - (*sector_num + *nb_sectors);
207 *sector_num = align_sector_num;
208 *nb_sectors = align_nb_sectors;
209 assert(ret >= 0);
210 return ret;
213 static inline void mirror_wait_for_io(MirrorBlockJob *s)
215 assert(!s->waiting_for_io);
216 s->waiting_for_io = true;
217 qemu_coroutine_yield();
218 s->waiting_for_io = false;
221 /* Submit async read while handling COW.
222 * Returns: The number of sectors copied after and including sector_num,
223 * excluding any sectors copied prior to sector_num due to alignment.
224 * This will be nb_sectors if no alignment is necessary, or
225 * (new_end - sector_num) if tail is rounded up or down due to
226 * alignment or buffer limit.
228 static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num,
229 int nb_sectors)
231 BlockBackend *source = s->common.blk;
232 int sectors_per_chunk, nb_chunks;
233 int ret;
234 MirrorOp *op;
235 int max_sectors;
237 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
238 max_sectors = sectors_per_chunk * s->max_iov;
240 /* We can only handle as much as buf_size at a time. */
241 nb_sectors = MIN(s->buf_size >> BDRV_SECTOR_BITS, nb_sectors);
242 nb_sectors = MIN(max_sectors, nb_sectors);
243 assert(nb_sectors);
244 ret = nb_sectors;
246 if (s->cow_bitmap) {
247 ret += mirror_cow_align(s, &sector_num, &nb_sectors);
249 assert(nb_sectors << BDRV_SECTOR_BITS <= s->buf_size);
250 /* The sector range must meet granularity because:
251 * 1) Caller passes in aligned values;
252 * 2) mirror_cow_align is used only when target cluster is larger. */
253 assert(!(sector_num % sectors_per_chunk));
254 nb_chunks = DIV_ROUND_UP(nb_sectors, sectors_per_chunk);
256 while (s->buf_free_count < nb_chunks) {
257 trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
258 mirror_wait_for_io(s);
261 /* Allocate a MirrorOp that is used as an AIO callback. */
262 op = g_new(MirrorOp, 1);
263 op->s = s;
264 op->sector_num = sector_num;
265 op->nb_sectors = nb_sectors;
267 /* Now make a QEMUIOVector taking enough granularity-sized chunks
268 * from s->buf_free.
270 qemu_iovec_init(&op->qiov, nb_chunks);
271 while (nb_chunks-- > 0) {
272 MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
273 size_t remaining = nb_sectors * BDRV_SECTOR_SIZE - op->qiov.size;
275 QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
276 s->buf_free_count--;
277 qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
280 /* Copy the dirty cluster. */
281 s->in_flight++;
282 s->sectors_in_flight += nb_sectors;
283 trace_mirror_one_iteration(s, sector_num, nb_sectors);
285 blk_aio_preadv(source, sector_num * BDRV_SECTOR_SIZE, &op->qiov, 0,
286 mirror_read_complete, op);
287 return ret;
290 static void mirror_do_zero_or_discard(MirrorBlockJob *s,
291 int64_t sector_num,
292 int nb_sectors,
293 bool is_discard)
295 MirrorOp *op;
297 /* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed
298 * so the freeing in mirror_iteration_done is nop. */
299 op = g_new0(MirrorOp, 1);
300 op->s = s;
301 op->sector_num = sector_num;
302 op->nb_sectors = nb_sectors;
304 s->in_flight++;
305 s->sectors_in_flight += nb_sectors;
306 if (is_discard) {
307 blk_aio_discard(s->target, sector_num, op->nb_sectors,
308 mirror_write_complete, op);
309 } else {
310 blk_aio_pwrite_zeroes(s->target, sector_num * BDRV_SECTOR_SIZE,
311 op->nb_sectors * BDRV_SECTOR_SIZE,
312 s->unmap ? BDRV_REQ_MAY_UNMAP : 0,
313 mirror_write_complete, op);
317 static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
319 BlockDriverState *source = blk_bs(s->common.blk);
320 int64_t sector_num, first_chunk;
321 uint64_t delay_ns = 0;
322 /* At least the first dirty chunk is mirrored in one iteration. */
323 int nb_chunks = 1;
324 int64_t end = s->bdev_length / BDRV_SECTOR_SIZE;
325 int sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
327 sector_num = hbitmap_iter_next(&s->hbi);
328 if (sector_num < 0) {
329 bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
330 sector_num = hbitmap_iter_next(&s->hbi);
331 trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
332 assert(sector_num >= 0);
335 first_chunk = sector_num / sectors_per_chunk;
336 while (test_bit(first_chunk, s->in_flight_bitmap)) {
337 trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
338 mirror_wait_for_io(s);
341 block_job_pause_point(&s->common);
343 /* Find the number of consective dirty chunks following the first dirty
344 * one, and wait for in flight requests in them. */
345 while (nb_chunks * sectors_per_chunk < (s->buf_size >> BDRV_SECTOR_BITS)) {
346 int64_t hbitmap_next;
347 int64_t next_sector = sector_num + nb_chunks * sectors_per_chunk;
348 int64_t next_chunk = next_sector / sectors_per_chunk;
349 if (next_sector >= end ||
350 !bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) {
351 break;
353 if (test_bit(next_chunk, s->in_flight_bitmap)) {
354 break;
357 hbitmap_next = hbitmap_iter_next(&s->hbi);
358 if (hbitmap_next > next_sector || hbitmap_next < 0) {
359 /* The bitmap iterator's cache is stale, refresh it */
360 bdrv_set_dirty_iter(&s->hbi, next_sector);
361 hbitmap_next = hbitmap_iter_next(&s->hbi);
363 assert(hbitmap_next == next_sector);
364 nb_chunks++;
367 /* Clear dirty bits before querying the block status, because
368 * calling bdrv_get_block_status_above could yield - if some blocks are
369 * marked dirty in this window, we need to know.
371 bdrv_reset_dirty_bitmap(s->dirty_bitmap, sector_num,
372 nb_chunks * sectors_per_chunk);
373 bitmap_set(s->in_flight_bitmap, sector_num / sectors_per_chunk, nb_chunks);
374 while (nb_chunks > 0 && sector_num < end) {
375 int ret;
376 int io_sectors;
377 BlockDriverState *file;
378 enum MirrorMethod {
379 MIRROR_METHOD_COPY,
380 MIRROR_METHOD_ZERO,
381 MIRROR_METHOD_DISCARD
382 } mirror_method = MIRROR_METHOD_COPY;
384 assert(!(sector_num % sectors_per_chunk));
385 ret = bdrv_get_block_status_above(source, NULL, sector_num,
386 nb_chunks * sectors_per_chunk,
387 &io_sectors, &file);
388 if (ret < 0) {
389 io_sectors = nb_chunks * sectors_per_chunk;
392 io_sectors -= io_sectors % sectors_per_chunk;
393 if (io_sectors < sectors_per_chunk) {
394 io_sectors = sectors_per_chunk;
395 } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) {
396 int64_t target_sector_num;
397 int target_nb_sectors;
398 bdrv_round_sectors_to_clusters(blk_bs(s->target), sector_num,
399 io_sectors, &target_sector_num,
400 &target_nb_sectors);
401 if (target_sector_num == sector_num &&
402 target_nb_sectors == io_sectors) {
403 mirror_method = ret & BDRV_BLOCK_ZERO ?
404 MIRROR_METHOD_ZERO :
405 MIRROR_METHOD_DISCARD;
409 mirror_clip_sectors(s, sector_num, &io_sectors);
410 switch (mirror_method) {
411 case MIRROR_METHOD_COPY:
412 io_sectors = mirror_do_read(s, sector_num, io_sectors);
413 break;
414 case MIRROR_METHOD_ZERO:
415 mirror_do_zero_or_discard(s, sector_num, io_sectors, false);
416 break;
417 case MIRROR_METHOD_DISCARD:
418 mirror_do_zero_or_discard(s, sector_num, io_sectors, true);
419 break;
420 default:
421 abort();
423 assert(io_sectors);
424 sector_num += io_sectors;
425 nb_chunks -= DIV_ROUND_UP(io_sectors, sectors_per_chunk);
426 if (s->common.speed) {
427 delay_ns = ratelimit_calculate_delay(&s->limit, io_sectors);
430 return delay_ns;
433 static void mirror_free_init(MirrorBlockJob *s)
435 int granularity = s->granularity;
436 size_t buf_size = s->buf_size;
437 uint8_t *buf = s->buf;
439 assert(s->buf_free_count == 0);
440 QSIMPLEQ_INIT(&s->buf_free);
441 while (buf_size != 0) {
442 MirrorBuffer *cur = (MirrorBuffer *)buf;
443 QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
444 s->buf_free_count++;
445 buf_size -= granularity;
446 buf += granularity;
450 static void mirror_drain(MirrorBlockJob *s)
452 while (s->in_flight > 0) {
453 mirror_wait_for_io(s);
457 typedef struct {
458 int ret;
459 } MirrorExitData;
461 static void mirror_exit(BlockJob *job, void *opaque)
463 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
464 MirrorExitData *data = opaque;
465 AioContext *replace_aio_context = NULL;
466 BlockDriverState *src = blk_bs(s->common.blk);
467 BlockDriverState *target_bs = blk_bs(s->target);
469 /* Make sure that the source BDS doesn't go away before we called
470 * block_job_completed(). */
471 bdrv_ref(src);
473 if (s->to_replace) {
474 replace_aio_context = bdrv_get_aio_context(s->to_replace);
475 aio_context_acquire(replace_aio_context);
478 if (s->should_complete && data->ret == 0) {
479 BlockDriverState *to_replace = src;
480 if (s->to_replace) {
481 to_replace = s->to_replace;
484 if (bdrv_get_flags(target_bs) != bdrv_get_flags(to_replace)) {
485 bdrv_reopen(target_bs, bdrv_get_flags(to_replace), NULL);
488 /* The mirror job has no requests in flight any more, but we need to
489 * drain potential other users of the BDS before changing the graph. */
490 bdrv_drained_begin(target_bs);
491 bdrv_replace_in_backing_chain(to_replace, target_bs);
492 bdrv_drained_end(target_bs);
494 /* We just changed the BDS the job BB refers to */
495 blk_remove_bs(job->blk);
496 blk_insert_bs(job->blk, src);
498 if (s->to_replace) {
499 bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
500 error_free(s->replace_blocker);
501 bdrv_unref(s->to_replace);
503 if (replace_aio_context) {
504 aio_context_release(replace_aio_context);
506 g_free(s->replaces);
507 bdrv_op_unblock_all(target_bs, s->common.blocker);
508 blk_unref(s->target);
509 block_job_completed(&s->common, data->ret);
510 g_free(data);
511 bdrv_drained_end(src);
512 if (qemu_get_aio_context() == bdrv_get_aio_context(src)) {
513 aio_enable_external(iohandler_get_aio_context());
515 bdrv_unref(src);
518 static void mirror_throttle(MirrorBlockJob *s)
520 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
522 if (now - s->last_pause_ns > SLICE_TIME) {
523 s->last_pause_ns = now;
524 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, 0);
525 } else {
526 block_job_pause_point(&s->common);
530 static void coroutine_fn mirror_run(void *opaque)
532 MirrorBlockJob *s = opaque;
533 MirrorExitData *data;
534 BlockDriverState *bs = blk_bs(s->common.blk);
535 BlockDriverState *target_bs = blk_bs(s->target);
536 int64_t sector_num, end, length;
537 BlockDriverInfo bdi;
538 char backing_filename[2]; /* we only need 2 characters because we are only
539 checking for a NULL string */
540 int ret = 0;
541 int n;
542 int target_cluster_size = BDRV_SECTOR_SIZE;
544 if (block_job_is_cancelled(&s->common)) {
545 goto immediate_exit;
548 s->bdev_length = bdrv_getlength(bs);
549 if (s->bdev_length < 0) {
550 ret = s->bdev_length;
551 goto immediate_exit;
552 } else if (s->bdev_length == 0) {
553 /* Report BLOCK_JOB_READY and wait for complete. */
554 block_job_event_ready(&s->common);
555 s->synced = true;
556 while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
557 block_job_yield(&s->common);
559 s->common.cancelled = false;
560 goto immediate_exit;
563 length = DIV_ROUND_UP(s->bdev_length, s->granularity);
564 s->in_flight_bitmap = bitmap_new(length);
566 /* If we have no backing file yet in the destination, we cannot let
567 * the destination do COW. Instead, we copy sectors around the
568 * dirty data if needed. We need a bitmap to do that.
570 bdrv_get_backing_filename(target_bs, backing_filename,
571 sizeof(backing_filename));
572 if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) {
573 target_cluster_size = bdi.cluster_size;
575 if (backing_filename[0] && !target_bs->backing
576 && s->granularity < target_cluster_size) {
577 s->buf_size = MAX(s->buf_size, target_cluster_size);
578 s->cow_bitmap = bitmap_new(length);
580 s->target_cluster_sectors = target_cluster_size >> BDRV_SECTOR_BITS;
581 s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov);
583 end = s->bdev_length / BDRV_SECTOR_SIZE;
584 s->buf = qemu_try_blockalign(bs, s->buf_size);
585 if (s->buf == NULL) {
586 ret = -ENOMEM;
587 goto immediate_exit;
590 mirror_free_init(s);
592 s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
593 if (!s->is_none_mode) {
594 /* First part, loop on the sectors and initialize the dirty bitmap. */
595 BlockDriverState *base = s->base;
596 bool mark_all_dirty = s->base == NULL && !bdrv_has_zero_init(target_bs);
598 for (sector_num = 0; sector_num < end; ) {
599 /* Just to make sure we are not exceeding int limit. */
600 int nb_sectors = MIN(INT_MAX >> BDRV_SECTOR_BITS,
601 end - sector_num);
603 mirror_throttle(s);
605 if (block_job_is_cancelled(&s->common)) {
606 goto immediate_exit;
609 ret = bdrv_is_allocated_above(bs, base, sector_num, nb_sectors, &n);
611 if (ret < 0) {
612 goto immediate_exit;
615 assert(n > 0);
616 if (ret == 1 || mark_all_dirty) {
617 bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n);
619 sector_num += n;
623 bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
624 for (;;) {
625 uint64_t delay_ns = 0;
626 int64_t cnt, delta;
627 bool should_complete;
629 if (s->ret < 0) {
630 ret = s->ret;
631 goto immediate_exit;
634 block_job_pause_point(&s->common);
636 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
637 /* s->common.offset contains the number of bytes already processed so
638 * far, cnt is the number of dirty sectors remaining and
639 * s->sectors_in_flight is the number of sectors currently being
640 * processed; together those are the current total operation length */
641 s->common.len = s->common.offset +
642 (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE;
644 /* Note that even when no rate limit is applied we need to yield
645 * periodically with no pending I/O so that bdrv_drain_all() returns.
646 * We do so every SLICE_TIME nanoseconds, or when there is an error,
647 * or when the source is clean, whichever comes first.
649 delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns;
650 if (delta < SLICE_TIME &&
651 s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
652 if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 ||
653 (cnt == 0 && s->in_flight > 0)) {
654 trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt);
655 mirror_wait_for_io(s);
656 continue;
657 } else if (cnt != 0) {
658 delay_ns = mirror_iteration(s);
662 should_complete = false;
663 if (s->in_flight == 0 && cnt == 0) {
664 trace_mirror_before_flush(s);
665 ret = blk_flush(s->target);
666 if (ret < 0) {
667 if (mirror_error_action(s, false, -ret) ==
668 BLOCK_ERROR_ACTION_REPORT) {
669 goto immediate_exit;
671 } else {
672 /* We're out of the streaming phase. From now on, if the job
673 * is cancelled we will actually complete all pending I/O and
674 * report completion. This way, block-job-cancel will leave
675 * the target in a consistent state.
677 if (!s->synced) {
678 block_job_event_ready(&s->common);
679 s->synced = true;
682 should_complete = s->should_complete ||
683 block_job_is_cancelled(&s->common);
684 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
688 if (cnt == 0 && should_complete) {
689 /* The dirty bitmap is not updated while operations are pending.
690 * If we're about to exit, wait for pending operations before
691 * calling bdrv_get_dirty_count(bs), or we may exit while the
692 * source has dirty data to copy!
694 * Note that I/O can be submitted by the guest while
695 * mirror_populate runs.
697 trace_mirror_before_drain(s, cnt);
698 bdrv_co_drain(bs);
699 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
702 ret = 0;
703 trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
704 if (!s->synced) {
705 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
706 if (block_job_is_cancelled(&s->common)) {
707 break;
709 } else if (!should_complete) {
710 delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
711 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
712 } else if (cnt == 0) {
713 /* The two disks are in sync. Exit and report successful
714 * completion.
716 assert(QLIST_EMPTY(&bs->tracked_requests));
717 s->common.cancelled = false;
718 break;
720 s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
723 immediate_exit:
724 if (s->in_flight > 0) {
725 /* We get here only if something went wrong. Either the job failed,
726 * or it was cancelled prematurely so that we do not guarantee that
727 * the target is a copy of the source.
729 assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
730 mirror_drain(s);
733 assert(s->in_flight == 0);
734 qemu_vfree(s->buf);
735 g_free(s->cow_bitmap);
736 g_free(s->in_flight_bitmap);
737 bdrv_release_dirty_bitmap(bs, s->dirty_bitmap);
739 data = g_malloc(sizeof(*data));
740 data->ret = ret;
741 /* Before we switch to target in mirror_exit, make sure data doesn't
742 * change. */
743 bdrv_drained_begin(bs);
744 if (qemu_get_aio_context() == bdrv_get_aio_context(bs)) {
745 /* FIXME: virtio host notifiers run on iohandler_ctx, therefore the
746 * above bdrv_drained_end isn't enough to quiesce it. This is ugly, we
747 * need a block layer API change to achieve this. */
748 aio_disable_external(iohandler_get_aio_context());
750 block_job_defer_to_main_loop(&s->common, mirror_exit, data);
753 static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
755 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
757 if (speed < 0) {
758 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
759 return;
761 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
764 static void mirror_complete(BlockJob *job, Error **errp)
766 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
767 BlockDriverState *src, *target;
769 src = blk_bs(job->blk);
770 target = blk_bs(s->target);
772 if (!s->synced) {
773 error_setg(errp, "The active block job '%s' cannot be completed",
774 job->id);
775 return;
778 if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) {
779 int ret;
781 assert(!target->backing);
782 ret = bdrv_open_backing_file(target, NULL, "backing", errp);
783 if (ret < 0) {
784 return;
788 /* block all operations on to_replace bs */
789 if (s->replaces) {
790 AioContext *replace_aio_context;
792 s->to_replace = bdrv_find_node(s->replaces);
793 if (!s->to_replace) {
794 error_setg(errp, "Node name '%s' not found", s->replaces);
795 return;
798 replace_aio_context = bdrv_get_aio_context(s->to_replace);
799 aio_context_acquire(replace_aio_context);
801 error_setg(&s->replace_blocker,
802 "block device is in use by block-job-complete");
803 bdrv_op_block_all(s->to_replace, s->replace_blocker);
804 bdrv_ref(s->to_replace);
806 aio_context_release(replace_aio_context);
809 if (s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) {
810 BlockDriverState *backing = s->is_none_mode ? src : s->base;
811 if (backing_bs(target) != backing) {
812 bdrv_set_backing_hd(target, backing);
816 s->should_complete = true;
817 block_job_enter(&s->common);
820 /* There is no matching mirror_resume() because mirror_run() will begin
821 * iterating again when the job is resumed.
823 static void coroutine_fn mirror_pause(BlockJob *job)
825 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
827 mirror_drain(s);
830 static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context)
832 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
834 blk_set_aio_context(s->target, new_context);
837 static const BlockJobDriver mirror_job_driver = {
838 .instance_size = sizeof(MirrorBlockJob),
839 .job_type = BLOCK_JOB_TYPE_MIRROR,
840 .set_speed = mirror_set_speed,
841 .complete = mirror_complete,
842 .pause = mirror_pause,
843 .attached_aio_context = mirror_attached_aio_context,
846 static const BlockJobDriver commit_active_job_driver = {
847 .instance_size = sizeof(MirrorBlockJob),
848 .job_type = BLOCK_JOB_TYPE_COMMIT,
849 .set_speed = mirror_set_speed,
850 .complete = mirror_complete,
851 .pause = mirror_pause,
852 .attached_aio_context = mirror_attached_aio_context,
855 static void mirror_start_job(const char *job_id, BlockDriverState *bs,
856 BlockDriverState *target, const char *replaces,
857 int64_t speed, uint32_t granularity,
858 int64_t buf_size,
859 BlockMirrorBackingMode backing_mode,
860 BlockdevOnError on_source_error,
861 BlockdevOnError on_target_error,
862 bool unmap,
863 BlockCompletionFunc *cb,
864 void *opaque, Error **errp,
865 const BlockJobDriver *driver,
866 bool is_none_mode, BlockDriverState *base)
868 MirrorBlockJob *s;
870 if (granularity == 0) {
871 granularity = bdrv_get_default_bitmap_granularity(target);
874 assert ((granularity & (granularity - 1)) == 0);
876 if (buf_size < 0) {
877 error_setg(errp, "Invalid parameter 'buf-size'");
878 return;
881 if (buf_size == 0) {
882 buf_size = DEFAULT_MIRROR_BUF_SIZE;
885 s = block_job_create(job_id, driver, bs, speed, cb, opaque, errp);
886 if (!s) {
887 return;
890 s->target = blk_new();
891 blk_insert_bs(s->target, target);
893 s->replaces = g_strdup(replaces);
894 s->on_source_error = on_source_error;
895 s->on_target_error = on_target_error;
896 s->is_none_mode = is_none_mode;
897 s->backing_mode = backing_mode;
898 s->base = base;
899 s->granularity = granularity;
900 s->buf_size = ROUND_UP(buf_size, granularity);
901 s->unmap = unmap;
903 s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
904 if (!s->dirty_bitmap) {
905 g_free(s->replaces);
906 blk_unref(s->target);
907 block_job_unref(&s->common);
908 return;
911 bdrv_op_block_all(target, s->common.blocker);
913 s->common.co = qemu_coroutine_create(mirror_run, s);
914 trace_mirror_start(bs, s, s->common.co, opaque);
915 qemu_coroutine_enter(s->common.co);
918 void mirror_start(const char *job_id, BlockDriverState *bs,
919 BlockDriverState *target, const char *replaces,
920 int64_t speed, uint32_t granularity, int64_t buf_size,
921 MirrorSyncMode mode, BlockMirrorBackingMode backing_mode,
922 BlockdevOnError on_source_error,
923 BlockdevOnError on_target_error,
924 bool unmap,
925 BlockCompletionFunc *cb,
926 void *opaque, Error **errp)
928 bool is_none_mode;
929 BlockDriverState *base;
931 if (mode == MIRROR_SYNC_MODE_INCREMENTAL) {
932 error_setg(errp, "Sync mode 'incremental' not supported");
933 return;
935 is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
936 base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL;
937 mirror_start_job(job_id, bs, target, replaces,
938 speed, granularity, buf_size, backing_mode,
939 on_source_error, on_target_error, unmap, cb, opaque, errp,
940 &mirror_job_driver, is_none_mode, base);
943 void commit_active_start(const char *job_id, BlockDriverState *bs,
944 BlockDriverState *base, int64_t speed,
945 BlockdevOnError on_error,
946 BlockCompletionFunc *cb,
947 void *opaque, Error **errp)
949 int64_t length, base_length;
950 int orig_base_flags;
951 int ret;
952 Error *local_err = NULL;
954 orig_base_flags = bdrv_get_flags(base);
956 if (bdrv_reopen(base, bs->open_flags, errp)) {
957 return;
960 length = bdrv_getlength(bs);
961 if (length < 0) {
962 error_setg_errno(errp, -length,
963 "Unable to determine length of %s", bs->filename);
964 goto error_restore_flags;
967 base_length = bdrv_getlength(base);
968 if (base_length < 0) {
969 error_setg_errno(errp, -base_length,
970 "Unable to determine length of %s", base->filename);
971 goto error_restore_flags;
974 if (length > base_length) {
975 ret = bdrv_truncate(base, length);
976 if (ret < 0) {
977 error_setg_errno(errp, -ret,
978 "Top image %s is larger than base image %s, and "
979 "resize of base image failed",
980 bs->filename, base->filename);
981 goto error_restore_flags;
985 mirror_start_job(job_id, bs, base, NULL, speed, 0, 0,
986 MIRROR_LEAVE_BACKING_CHAIN,
987 on_error, on_error, false, cb, opaque, &local_err,
988 &commit_active_job_driver, false, base);
989 if (local_err) {
990 error_propagate(errp, local_err);
991 goto error_restore_flags;
994 return;
996 error_restore_flags:
997 /* ignore error and errp for bdrv_reopen, because we want to propagate
998 * the original error */
999 bdrv_reopen(base, orig_base_flags, NULL);
1000 return;