qcow2: Add function for refcount order amendment
[qemu.git] / block / mirror.c
blobfc34a9c491fc5556e1643ee012a9e7d1da8b67ea
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 "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;
32 } MirrorBuffer;
34 typedef struct MirrorBlockJob {
35 BlockJob common;
36 RateLimit limit;
37 BlockDriverState *target;
38 BlockDriverState *base;
39 /* The name of the graph node to replace */
40 char *replaces;
41 /* The BDS to replace */
42 BlockDriverState *to_replace;
43 /* Used to block operations on the drive-mirror-replace target */
44 Error *replace_blocker;
45 bool is_none_mode;
46 BlockdevOnError on_source_error, on_target_error;
47 bool synced;
48 bool should_complete;
49 int64_t sector_num;
50 int64_t granularity;
51 size_t buf_size;
52 int64_t bdev_length;
53 unsigned long *cow_bitmap;
54 BdrvDirtyBitmap *dirty_bitmap;
55 HBitmapIter hbi;
56 uint8_t *buf;
57 QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
58 int buf_free_count;
60 unsigned long *in_flight_bitmap;
61 int in_flight;
62 int sectors_in_flight;
63 int ret;
64 bool unmap;
65 bool waiting_for_io;
66 } MirrorBlockJob;
68 typedef struct MirrorOp {
69 MirrorBlockJob *s;
70 QEMUIOVector qiov;
71 int64_t sector_num;
72 int nb_sectors;
73 } MirrorOp;
75 static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
76 int error)
78 s->synced = false;
79 if (read) {
80 return block_job_error_action(&s->common, s->common.bs,
81 s->on_source_error, true, error);
82 } else {
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;
91 struct iovec *iov;
92 int64_t chunk_num;
93 int i, nb_chunks, sectors_per_chunk;
95 trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
97 s->in_flight--;
98 s->sectors_in_flight -= op->nb_sectors;
99 iov = op->qiov.iov;
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);
103 s->buf_free_count++;
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);
110 if (ret >= 0) {
111 if (s->cow_bitmap) {
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);
118 g_free(op);
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;
129 if (ret < 0) {
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) {
135 s->ret = ret;
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;
145 if (ret < 0) {
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) {
151 s->ret = ret;
154 mirror_iteration_done(op, ret);
155 return;
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;
167 MirrorOp *op;
168 int pnum;
169 int64_t ret;
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.
197 nb_chunks = 0;
198 nb_sectors = 0;
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;
210 do {
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);
216 break;
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);
249 break;
251 if (IOV_MAX < nb_chunks + added_chunks) {
252 trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
253 break;
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);
270 op->s = s;
271 op->sector_num = sector_num;
272 op->nb_sectors = nb_sectors;
274 /* Now make a QEMUIOVector taking enough granularity-sized chunks
275 * from s->buf_free.
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);
284 s->buf_free_count--;
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. */
301 s->in_flight++;
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,
306 nb_sectors, &pnum);
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);
315 } else {
316 assert(!(ret & BDRV_BLOCK_DATA));
317 bdrv_aio_discard(s->target, sector_num, op->nb_sectors,
318 mirror_write_complete, op);
320 return delay_ns;
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);
334 s->buf_free_count++;
335 buf_size -= granularity;
336 buf += 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;
349 typedef struct {
350 int ret;
351 } MirrorExitData;
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(). */
362 bdrv_ref(src);
364 if (s->to_replace) {
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;
371 if (s->to_replace) {
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");
379 data->ret = -EINVAL;
380 goto out;
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);
389 out:
390 if (s->to_replace) {
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);
398 g_free(s->replaces);
399 bdrv_op_unblock_all(s->target, s->common.blocker);
400 bdrv_unref(s->target);
401 block_job_completed(&s->common, data->ret);
402 g_free(data);
403 bdrv_drained_end(src);
404 bdrv_unref(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;
414 BlockDriverInfo bdi;
415 char backing_filename[2]; /* we only need 2 characters because we are only
416 checking for a NULL string */
417 int ret = 0;
418 int n;
420 if (block_job_is_cancelled(&s->common)) {
421 goto immediate_exit;
424 s->bdev_length = bdrv_getlength(bs);
425 if (s->bdev_length < 0) {
426 ret = s->bdev_length;
427 goto immediate_exit;
428 } else if (s->bdev_length == 0) {
429 /* Report BLOCK_JOB_READY and wait for complete. */
430 block_job_event_ready(&s->common);
431 s->synced = true;
432 while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
433 block_job_yield(&s->common);
435 s->common.cancelled = false;
436 goto immediate_exit;
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);
450 if (ret < 0) {
451 goto immediate_exit;
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) {
462 ret = -ENOMEM;
463 goto immediate_exit;
466 mirror_free_init(s);
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,
477 end - sector_num);
478 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
480 if (now - last_pause_ns > SLICE_TIME) {
481 last_pause_ns = now;
482 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, 0);
485 if (block_job_is_cancelled(&s->common)) {
486 goto immediate_exit;
489 ret = bdrv_is_allocated_above(bs, base, sector_num, nb_sectors, &n);
491 if (ret < 0) {
492 goto immediate_exit;
495 assert(n > 0);
496 if (ret == 1 || mark_all_dirty) {
497 bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n);
499 sector_num += n;
503 bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
504 for (;;) {
505 uint64_t delay_ns = 0;
506 int64_t cnt;
507 bool should_complete;
509 if (s->ret < 0) {
510 ret = s->ret;
511 goto immediate_exit;
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;
535 continue;
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);
545 if (ret < 0) {
546 if (mirror_error_action(s, false, -ret) ==
547 BLOCK_ERROR_ACTION_REPORT) {
548 goto immediate_exit;
550 } else {
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.
556 if (!s->synced) {
557 block_job_event_ready(&s->common);
558 s->synced = true;
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);
577 bdrv_drain(bs);
578 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
581 ret = 0;
582 trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
583 if (!s->synced) {
584 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
585 if (block_job_is_cancelled(&s->common)) {
586 break;
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
593 * completion.
595 assert(QLIST_EMPTY(&bs->tracked_requests));
596 s->common.cancelled = false;
597 break;
599 last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
602 immediate_exit:
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)));
609 mirror_drain(s);
612 assert(s->in_flight == 0);
613 qemu_vfree(s->buf);
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));
622 data->ret = ret;
623 /* Before we switch to target in mirror_exit, make sure data doesn't
624 * change. */
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);
633 if (speed < 0) {
634 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
635 return;
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;
653 int ret;
655 ret = bdrv_open_backing_file(s->target, NULL, "backing", &local_err);
656 if (ret < 0) {
657 error_propagate(errp, local_err);
658 return;
660 if (!s->synced) {
661 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
662 return;
665 /* check the target bs is not blocked and block all operations on it */
666 if (s->replaces) {
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);
672 return;
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,
702 .iostatus_reset
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,
710 int64_t buf_size,
711 BlockdevOnError on_source_error,
712 BlockdevOnError on_target_error,
713 bool unmap,
714 BlockCompletionFunc *cb,
715 void *opaque, Error **errp,
716 const BlockJobDriver *driver,
717 bool is_none_mode, BlockDriverState *base)
719 MirrorBlockJob *s;
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");
732 return;
735 if (buf_size < 0) {
736 error_setg(errp, "Invalid parameter 'buf-size'");
737 return;
740 if (buf_size == 0) {
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. */
746 if (replaces) {
747 replaced_bs = bdrv_lookup_bs(replaces, replaces, errp);
748 if (replaced_bs == NULL) {
749 return;
751 } else {
752 replaced_bs = bs;
754 if (replaced_bs->blk && target->blk) {
755 error_setg(errp, "Can't create node with two BlockBackends");
756 return;
759 s = block_job_create(driver, bs, speed, cb, opaque, errp);
760 if (!s) {
761 return;
764 s->replaces = g_strdup(replaces);
765 s->on_source_error = on_source_error;
766 s->on_target_error = on_target_error;
767 s->target = target;
768 s->is_none_mode = is_none_mode;
769 s->base = base;
770 s->granularity = granularity;
771 s->buf_size = ROUND_UP(buf_size, granularity);
772 s->unmap = unmap;
774 s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
775 if (!s->dirty_bitmap) {
776 g_free(s->replaces);
777 block_job_unref(&s->common);
778 return;
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,
798 bool unmap,
799 BlockCompletionFunc *cb,
800 void *opaque, Error **errp)
802 bool is_none_mode;
803 BlockDriverState *base;
805 if (mode == MIRROR_SYNC_MODE_INCREMENTAL) {
806 error_setg(errp, "Sync mode 'incremental' not supported");
807 return;
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,
818 int64_t speed,
819 BlockdevOnError on_error,
820 BlockCompletionFunc *cb,
821 void *opaque, Error **errp)
823 int64_t length, base_length;
824 int orig_base_flags;
825 int ret;
826 Error *local_err = NULL;
828 orig_base_flags = bdrv_get_flags(base);
830 if (bdrv_reopen(base, bs->open_flags, errp)) {
831 return;
834 length = bdrv_getlength(bs);
835 if (length < 0) {
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);
850 if (ret < 0) {
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;
859 bdrv_ref(base);
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);
863 if (local_err) {
864 error_propagate(errp, local_err);
865 goto error_restore_flags;
868 return;
870 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);
874 return;