vt82c686: QOMify
[qemu/ar7.git] / block / mirror.c
blob048e452d17c1570731410a23f468789241218a81
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 "trace.h"
15 #include "block/blockjob.h"
16 #include "block/block_int.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qemu/ratelimit.h"
19 #include "qemu/bitmap.h"
21 #define SLICE_TIME 100000000ULL /* ns */
22 #define MAX_IN_FLIGHT 16
24 /* The mirroring buffer is a list of granularity-sized chunks.
25 * Free chunks are organized in a list.
27 typedef struct MirrorBuffer {
28 QSIMPLEQ_ENTRY(MirrorBuffer) next;
29 } MirrorBuffer;
31 typedef struct MirrorBlockJob {
32 BlockJob common;
33 RateLimit limit;
34 BlockDriverState *target;
35 BlockDriverState *base;
36 /* The name of the graph node to replace */
37 char *replaces;
38 /* The BDS to replace */
39 BlockDriverState *to_replace;
40 /* Used to block operations on the drive-mirror-replace target */
41 Error *replace_blocker;
42 bool is_none_mode;
43 BlockdevOnError on_source_error, on_target_error;
44 bool synced;
45 bool should_complete;
46 int64_t sector_num;
47 int64_t granularity;
48 size_t buf_size;
49 int64_t bdev_length;
50 unsigned long *cow_bitmap;
51 BdrvDirtyBitmap *dirty_bitmap;
52 HBitmapIter hbi;
53 uint8_t *buf;
54 QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
55 int buf_free_count;
57 unsigned long *in_flight_bitmap;
58 int in_flight;
59 int sectors_in_flight;
60 int ret;
61 } MirrorBlockJob;
63 typedef struct MirrorOp {
64 MirrorBlockJob *s;
65 QEMUIOVector qiov;
66 int64_t sector_num;
67 int nb_sectors;
68 } MirrorOp;
70 static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
71 int error)
73 s->synced = false;
74 if (read) {
75 return block_job_error_action(&s->common, s->common.bs,
76 s->on_source_error, true, error);
77 } else {
78 return block_job_error_action(&s->common, s->target,
79 s->on_target_error, false, error);
83 static void mirror_iteration_done(MirrorOp *op, int ret)
85 MirrorBlockJob *s = op->s;
86 struct iovec *iov;
87 int64_t chunk_num;
88 int i, nb_chunks, sectors_per_chunk;
90 trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
92 s->in_flight--;
93 s->sectors_in_flight -= op->nb_sectors;
94 iov = op->qiov.iov;
95 for (i = 0; i < op->qiov.niov; i++) {
96 MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
97 QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
98 s->buf_free_count++;
101 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
102 chunk_num = op->sector_num / sectors_per_chunk;
103 nb_chunks = op->nb_sectors / sectors_per_chunk;
104 bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
105 if (ret >= 0) {
106 if (s->cow_bitmap) {
107 bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
109 s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE;
112 qemu_iovec_destroy(&op->qiov);
113 g_slice_free(MirrorOp, op);
115 /* Enter coroutine when it is not sleeping. The coroutine sleeps to
116 * rate-limit itself. The coroutine will eventually resume since there is
117 * a sleep timeout so don't wake it early.
119 if (s->common.busy) {
120 qemu_coroutine_enter(s->common.co, NULL);
124 static void mirror_write_complete(void *opaque, int ret)
126 MirrorOp *op = opaque;
127 MirrorBlockJob *s = op->s;
128 if (ret < 0) {
129 BlockErrorAction action;
131 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
132 action = mirror_error_action(s, false, -ret);
133 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
134 s->ret = ret;
137 mirror_iteration_done(op, ret);
140 static void mirror_read_complete(void *opaque, int ret)
142 MirrorOp *op = opaque;
143 MirrorBlockJob *s = op->s;
144 if (ret < 0) {
145 BlockErrorAction action;
147 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
148 action = mirror_error_action(s, true, -ret);
149 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
150 s->ret = ret;
153 mirror_iteration_done(op, ret);
154 return;
156 bdrv_aio_writev(s->target, op->sector_num, &op->qiov, op->nb_sectors,
157 mirror_write_complete, op);
160 static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
162 BlockDriverState *source = s->common.bs;
163 int nb_sectors, sectors_per_chunk, nb_chunks;
164 int64_t end, sector_num, next_chunk, next_sector, hbitmap_next_sector;
165 uint64_t delay_ns = 0;
166 MirrorOp *op;
168 s->sector_num = hbitmap_iter_next(&s->hbi);
169 if (s->sector_num < 0) {
170 bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
171 s->sector_num = hbitmap_iter_next(&s->hbi);
172 trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
173 assert(s->sector_num >= 0);
176 hbitmap_next_sector = s->sector_num;
177 sector_num = s->sector_num;
178 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
179 end = s->bdev_length / BDRV_SECTOR_SIZE;
181 /* Extend the QEMUIOVector to include all adjacent blocks that will
182 * be copied in this operation.
184 * We have to do this if we have no backing file yet in the destination,
185 * and the cluster size is very large. Then we need to do COW ourselves.
186 * The first time a cluster is copied, copy it entirely. Note that,
187 * because both the granularity and the cluster size are powers of two,
188 * the number of sectors to copy cannot exceed one cluster.
190 * We also want to extend the QEMUIOVector to include more adjacent
191 * dirty blocks if possible, to limit the number of I/O operations and
192 * run efficiently even with a small granularity.
194 nb_chunks = 0;
195 nb_sectors = 0;
196 next_sector = sector_num;
197 next_chunk = sector_num / sectors_per_chunk;
199 /* Wait for I/O to this cluster (from a previous iteration) to be done. */
200 while (test_bit(next_chunk, s->in_flight_bitmap)) {
201 trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
202 qemu_coroutine_yield();
205 do {
206 int added_sectors, added_chunks;
208 if (!bdrv_get_dirty(source, s->dirty_bitmap, next_sector) ||
209 test_bit(next_chunk, s->in_flight_bitmap)) {
210 assert(nb_sectors > 0);
211 break;
214 added_sectors = sectors_per_chunk;
215 if (s->cow_bitmap && !test_bit(next_chunk, s->cow_bitmap)) {
216 bdrv_round_to_clusters(s->target,
217 next_sector, added_sectors,
218 &next_sector, &added_sectors);
220 /* On the first iteration, the rounding may make us copy
221 * sectors before the first dirty one.
223 if (next_sector < sector_num) {
224 assert(nb_sectors == 0);
225 sector_num = next_sector;
226 next_chunk = next_sector / sectors_per_chunk;
230 added_sectors = MIN(added_sectors, end - (sector_num + nb_sectors));
231 added_chunks = (added_sectors + sectors_per_chunk - 1) / sectors_per_chunk;
233 /* When doing COW, it may happen that there is not enough space for
234 * a full cluster. Wait if that is the case.
236 while (nb_chunks == 0 && s->buf_free_count < added_chunks) {
237 trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight);
238 qemu_coroutine_yield();
240 if (s->buf_free_count < nb_chunks + added_chunks) {
241 trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
242 break;
245 /* We have enough free space to copy these sectors. */
246 bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
248 nb_sectors += added_sectors;
249 nb_chunks += added_chunks;
250 next_sector += added_sectors;
251 next_chunk += added_chunks;
252 if (!s->synced && s->common.speed) {
253 delay_ns = ratelimit_calculate_delay(&s->limit, added_sectors);
255 } while (delay_ns == 0 && next_sector < end);
257 /* Allocate a MirrorOp that is used as an AIO callback. */
258 op = g_slice_new(MirrorOp);
259 op->s = s;
260 op->sector_num = sector_num;
261 op->nb_sectors = nb_sectors;
263 /* Now make a QEMUIOVector taking enough granularity-sized chunks
264 * from s->buf_free.
266 qemu_iovec_init(&op->qiov, nb_chunks);
267 next_sector = sector_num;
268 while (nb_chunks-- > 0) {
269 MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
270 size_t remaining = (nb_sectors * BDRV_SECTOR_SIZE) - op->qiov.size;
272 QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
273 s->buf_free_count--;
274 qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
276 /* Advance the HBitmapIter in parallel, so that we do not examine
277 * the same sector twice.
279 if (next_sector > hbitmap_next_sector
280 && bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) {
281 hbitmap_next_sector = hbitmap_iter_next(&s->hbi);
284 next_sector += sectors_per_chunk;
287 bdrv_reset_dirty_bitmap(s->dirty_bitmap, sector_num, nb_sectors);
289 /* Copy the dirty cluster. */
290 s->in_flight++;
291 s->sectors_in_flight += nb_sectors;
292 trace_mirror_one_iteration(s, sector_num, nb_sectors);
293 bdrv_aio_readv(source, sector_num, &op->qiov, nb_sectors,
294 mirror_read_complete, op);
295 return delay_ns;
298 static void mirror_free_init(MirrorBlockJob *s)
300 int granularity = s->granularity;
301 size_t buf_size = s->buf_size;
302 uint8_t *buf = s->buf;
304 assert(s->buf_free_count == 0);
305 QSIMPLEQ_INIT(&s->buf_free);
306 while (buf_size != 0) {
307 MirrorBuffer *cur = (MirrorBuffer *)buf;
308 QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
309 s->buf_free_count++;
310 buf_size -= granularity;
311 buf += granularity;
315 static void mirror_drain(MirrorBlockJob *s)
317 while (s->in_flight > 0) {
318 qemu_coroutine_yield();
322 typedef struct {
323 int ret;
324 } MirrorExitData;
326 static void mirror_exit(BlockJob *job, void *opaque)
328 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
329 MirrorExitData *data = opaque;
330 AioContext *replace_aio_context = NULL;
332 if (s->to_replace) {
333 replace_aio_context = bdrv_get_aio_context(s->to_replace);
334 aio_context_acquire(replace_aio_context);
337 if (s->should_complete && data->ret == 0) {
338 BlockDriverState *to_replace = s->common.bs;
339 if (s->to_replace) {
340 to_replace = s->to_replace;
342 if (bdrv_get_flags(s->target) != bdrv_get_flags(to_replace)) {
343 bdrv_reopen(s->target, bdrv_get_flags(to_replace), NULL);
345 bdrv_swap(s->target, to_replace);
346 if (s->common.driver->job_type == BLOCK_JOB_TYPE_COMMIT) {
347 /* drop the bs loop chain formed by the swap: break the loop then
348 * trigger the unref from the top one */
349 BlockDriverState *p = s->base->backing_hd;
350 bdrv_set_backing_hd(s->base, NULL);
351 bdrv_unref(p);
354 if (s->to_replace) {
355 bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
356 error_free(s->replace_blocker);
357 bdrv_unref(s->to_replace);
359 if (replace_aio_context) {
360 aio_context_release(replace_aio_context);
362 g_free(s->replaces);
363 bdrv_unref(s->target);
364 block_job_completed(&s->common, data->ret);
365 g_free(data);
368 static void coroutine_fn mirror_run(void *opaque)
370 MirrorBlockJob *s = opaque;
371 MirrorExitData *data;
372 BlockDriverState *bs = s->common.bs;
373 int64_t sector_num, end, sectors_per_chunk, length;
374 uint64_t last_pause_ns;
375 BlockDriverInfo bdi;
376 char backing_filename[2]; /* we only need 2 characters because we are only
377 checking for a NULL string */
378 int ret = 0;
379 int n;
381 if (block_job_is_cancelled(&s->common)) {
382 goto immediate_exit;
385 s->bdev_length = bdrv_getlength(bs);
386 if (s->bdev_length < 0) {
387 ret = s->bdev_length;
388 goto immediate_exit;
389 } else if (s->bdev_length == 0) {
390 /* Report BLOCK_JOB_READY and wait for complete. */
391 block_job_event_ready(&s->common);
392 s->synced = true;
393 while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
394 block_job_yield(&s->common);
396 s->common.cancelled = false;
397 goto immediate_exit;
400 length = DIV_ROUND_UP(s->bdev_length, s->granularity);
401 s->in_flight_bitmap = bitmap_new(length);
403 /* If we have no backing file yet in the destination, we cannot let
404 * the destination do COW. Instead, we copy sectors around the
405 * dirty data if needed. We need a bitmap to do that.
407 bdrv_get_backing_filename(s->target, backing_filename,
408 sizeof(backing_filename));
409 if (backing_filename[0] && !s->target->backing_hd) {
410 ret = bdrv_get_info(s->target, &bdi);
411 if (ret < 0) {
412 goto immediate_exit;
414 if (s->granularity < bdi.cluster_size) {
415 s->buf_size = MAX(s->buf_size, bdi.cluster_size);
416 s->cow_bitmap = bitmap_new(length);
420 end = s->bdev_length / BDRV_SECTOR_SIZE;
421 s->buf = qemu_try_blockalign(bs, s->buf_size);
422 if (s->buf == NULL) {
423 ret = -ENOMEM;
424 goto immediate_exit;
427 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
428 mirror_free_init(s);
430 if (!s->is_none_mode) {
431 /* First part, loop on the sectors and initialize the dirty bitmap. */
432 BlockDriverState *base = s->base;
433 for (sector_num = 0; sector_num < end; ) {
434 int64_t next = (sector_num | (sectors_per_chunk - 1)) + 1;
435 ret = bdrv_is_allocated_above(bs, base,
436 sector_num, next - sector_num, &n);
438 if (ret < 0) {
439 goto immediate_exit;
442 assert(n > 0);
443 if (ret == 1) {
444 bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n);
445 sector_num = next;
446 } else {
447 sector_num += n;
452 bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
453 last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
454 for (;;) {
455 uint64_t delay_ns = 0;
456 int64_t cnt;
457 bool should_complete;
459 if (s->ret < 0) {
460 ret = s->ret;
461 goto immediate_exit;
464 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
465 /* s->common.offset contains the number of bytes already processed so
466 * far, cnt is the number of dirty sectors remaining and
467 * s->sectors_in_flight is the number of sectors currently being
468 * processed; together those are the current total operation length */
469 s->common.len = s->common.offset +
470 (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE;
472 /* Note that even when no rate limit is applied we need to yield
473 * periodically with no pending I/O so that bdrv_drain_all() returns.
474 * We do so every SLICE_TIME nanoseconds, or when there is an error,
475 * or when the source is clean, whichever comes first.
477 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - last_pause_ns < SLICE_TIME &&
478 s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
479 if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 ||
480 (cnt == 0 && s->in_flight > 0)) {
481 trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt);
482 qemu_coroutine_yield();
483 continue;
484 } else if (cnt != 0) {
485 delay_ns = mirror_iteration(s);
489 should_complete = false;
490 if (s->in_flight == 0 && cnt == 0) {
491 trace_mirror_before_flush(s);
492 ret = bdrv_flush(s->target);
493 if (ret < 0) {
494 if (mirror_error_action(s, false, -ret) ==
495 BLOCK_ERROR_ACTION_REPORT) {
496 goto immediate_exit;
498 } else {
499 /* We're out of the streaming phase. From now on, if the job
500 * is cancelled we will actually complete all pending I/O and
501 * report completion. This way, block-job-cancel will leave
502 * the target in a consistent state.
504 if (!s->synced) {
505 block_job_event_ready(&s->common);
506 s->synced = true;
509 should_complete = s->should_complete ||
510 block_job_is_cancelled(&s->common);
511 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
515 if (cnt == 0 && should_complete) {
516 /* The dirty bitmap is not updated while operations are pending.
517 * If we're about to exit, wait for pending operations before
518 * calling bdrv_get_dirty_count(bs), or we may exit while the
519 * source has dirty data to copy!
521 * Note that I/O can be submitted by the guest while
522 * mirror_populate runs.
524 trace_mirror_before_drain(s, cnt);
525 bdrv_drain(bs);
526 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
529 ret = 0;
530 trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
531 if (!s->synced) {
532 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
533 if (block_job_is_cancelled(&s->common)) {
534 break;
536 } else if (!should_complete) {
537 delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
538 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
539 } else if (cnt == 0) {
540 /* The two disks are in sync. Exit and report successful
541 * completion.
543 assert(QLIST_EMPTY(&bs->tracked_requests));
544 s->common.cancelled = false;
545 break;
547 last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
550 immediate_exit:
551 if (s->in_flight > 0) {
552 /* We get here only if something went wrong. Either the job failed,
553 * or it was cancelled prematurely so that we do not guarantee that
554 * the target is a copy of the source.
556 assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
557 mirror_drain(s);
560 assert(s->in_flight == 0);
561 qemu_vfree(s->buf);
562 g_free(s->cow_bitmap);
563 g_free(s->in_flight_bitmap);
564 bdrv_release_dirty_bitmap(bs, s->dirty_bitmap);
565 bdrv_iostatus_disable(s->target);
567 data = g_malloc(sizeof(*data));
568 data->ret = ret;
569 block_job_defer_to_main_loop(&s->common, mirror_exit, data);
572 static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
574 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
576 if (speed < 0) {
577 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
578 return;
580 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
583 static void mirror_iostatus_reset(BlockJob *job)
585 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
587 bdrv_iostatus_reset(s->target);
590 static void mirror_complete(BlockJob *job, Error **errp)
592 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
593 Error *local_err = NULL;
594 int ret;
596 ret = bdrv_open_backing_file(s->target, NULL, &local_err);
597 if (ret < 0) {
598 error_propagate(errp, local_err);
599 return;
601 if (!s->synced) {
602 error_setg(errp, QERR_BLOCK_JOB_NOT_READY,
603 bdrv_get_device_name(job->bs));
604 return;
607 /* check the target bs is not blocked and block all operations on it */
608 if (s->replaces) {
609 AioContext *replace_aio_context;
611 s->to_replace = check_to_replace_node(s->replaces, &local_err);
612 if (!s->to_replace) {
613 error_propagate(errp, local_err);
614 return;
617 replace_aio_context = bdrv_get_aio_context(s->to_replace);
618 aio_context_acquire(replace_aio_context);
620 error_setg(&s->replace_blocker,
621 "block device is in use by block-job-complete");
622 bdrv_op_block_all(s->to_replace, s->replace_blocker);
623 bdrv_ref(s->to_replace);
625 aio_context_release(replace_aio_context);
628 s->should_complete = true;
629 block_job_enter(&s->common);
632 static const BlockJobDriver mirror_job_driver = {
633 .instance_size = sizeof(MirrorBlockJob),
634 .job_type = BLOCK_JOB_TYPE_MIRROR,
635 .set_speed = mirror_set_speed,
636 .iostatus_reset= mirror_iostatus_reset,
637 .complete = mirror_complete,
640 static const BlockJobDriver commit_active_job_driver = {
641 .instance_size = sizeof(MirrorBlockJob),
642 .job_type = BLOCK_JOB_TYPE_COMMIT,
643 .set_speed = mirror_set_speed,
644 .iostatus_reset
645 = mirror_iostatus_reset,
646 .complete = mirror_complete,
649 static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
650 const char *replaces,
651 int64_t speed, uint32_t granularity,
652 int64_t buf_size,
653 BlockdevOnError on_source_error,
654 BlockdevOnError on_target_error,
655 BlockCompletionFunc *cb,
656 void *opaque, Error **errp,
657 const BlockJobDriver *driver,
658 bool is_none_mode, BlockDriverState *base)
660 MirrorBlockJob *s;
662 if (granularity == 0) {
663 granularity = bdrv_get_default_bitmap_granularity(target);
666 assert ((granularity & (granularity - 1)) == 0);
668 if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
669 on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
670 !bdrv_iostatus_is_enabled(bs)) {
671 error_setg(errp, QERR_INVALID_PARAMETER, "on-source-error");
672 return;
676 s = block_job_create(driver, bs, speed, cb, opaque, errp);
677 if (!s) {
678 return;
681 s->replaces = g_strdup(replaces);
682 s->on_source_error = on_source_error;
683 s->on_target_error = on_target_error;
684 s->target = target;
685 s->is_none_mode = is_none_mode;
686 s->base = base;
687 s->granularity = granularity;
688 s->buf_size = MAX(buf_size, granularity);
690 s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
691 if (!s->dirty_bitmap) {
692 return;
694 bdrv_set_enable_write_cache(s->target, true);
695 bdrv_set_on_error(s->target, on_target_error, on_target_error);
696 bdrv_iostatus_enable(s->target);
697 s->common.co = qemu_coroutine_create(mirror_run);
698 trace_mirror_start(bs, s, s->common.co, opaque);
699 qemu_coroutine_enter(s->common.co, s);
702 void mirror_start(BlockDriverState *bs, BlockDriverState *target,
703 const char *replaces,
704 int64_t speed, uint32_t granularity, int64_t buf_size,
705 MirrorSyncMode mode, BlockdevOnError on_source_error,
706 BlockdevOnError on_target_error,
707 BlockCompletionFunc *cb,
708 void *opaque, Error **errp)
710 bool is_none_mode;
711 BlockDriverState *base;
713 if (mode == MIRROR_SYNC_MODE_DIRTY_BITMAP) {
714 error_setg(errp, "Sync mode 'dirty-bitmap' not supported");
715 return;
717 is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
718 base = mode == MIRROR_SYNC_MODE_TOP ? bs->backing_hd : NULL;
719 mirror_start_job(bs, target, replaces,
720 speed, granularity, buf_size,
721 on_source_error, on_target_error, cb, opaque, errp,
722 &mirror_job_driver, is_none_mode, base);
725 void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
726 int64_t speed,
727 BlockdevOnError on_error,
728 BlockCompletionFunc *cb,
729 void *opaque, Error **errp)
731 int64_t length, base_length;
732 int orig_base_flags;
733 int ret;
734 Error *local_err = NULL;
736 orig_base_flags = bdrv_get_flags(base);
738 if (bdrv_reopen(base, bs->open_flags, errp)) {
739 return;
742 length = bdrv_getlength(bs);
743 if (length < 0) {
744 error_setg_errno(errp, -length,
745 "Unable to determine length of %s", bs->filename);
746 goto error_restore_flags;
749 base_length = bdrv_getlength(base);
750 if (base_length < 0) {
751 error_setg_errno(errp, -base_length,
752 "Unable to determine length of %s", base->filename);
753 goto error_restore_flags;
756 if (length > base_length) {
757 ret = bdrv_truncate(base, length);
758 if (ret < 0) {
759 error_setg_errno(errp, -ret,
760 "Top image %s is larger than base image %s, and "
761 "resize of base image failed",
762 bs->filename, base->filename);
763 goto error_restore_flags;
767 bdrv_ref(base);
768 mirror_start_job(bs, base, NULL, speed, 0, 0,
769 on_error, on_error, cb, opaque, &local_err,
770 &commit_active_job_driver, false, base);
771 if (local_err) {
772 error_propagate(errp, local_err);
773 goto error_restore_flags;
776 return;
778 error_restore_flags:
779 /* ignore error and errp for bdrv_reopen, because we want to propagate
780 * the original error */
781 bdrv_reopen(base, orig_base_flags, NULL);
782 return;