]> git.proxmox.com Git - mirror_qemu.git/blob - block/mirror.c
blockjob: Store device name at job creation
[mirror_qemu.git] / block / mirror.c
1 /*
2 * Image mirroring
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
8 *
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.
11 *
12 */
13
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"
20
21 #define SLICE_TIME 100000000ULL /* ns */
22 #define MAX_IN_FLIGHT 16
23 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
24
25 /* The mirroring buffer is a list of granularity-sized chunks.
26 * Free chunks are organized in a list.
27 */
28 typedef struct MirrorBuffer {
29 QSIMPLEQ_ENTRY(MirrorBuffer) next;
30 } MirrorBuffer;
31
32 typedef struct MirrorBlockJob {
33 BlockJob common;
34 RateLimit limit;
35 BlockDriverState *target;
36 BlockDriverState *base;
37 /* The name of the graph node to replace */
38 char *replaces;
39 /* The BDS to replace */
40 BlockDriverState *to_replace;
41 /* Used to block operations on the drive-mirror-replace target */
42 Error *replace_blocker;
43 bool is_none_mode;
44 BlockdevOnError on_source_error, on_target_error;
45 bool synced;
46 bool should_complete;
47 int64_t sector_num;
48 int64_t granularity;
49 size_t buf_size;
50 int64_t bdev_length;
51 unsigned long *cow_bitmap;
52 BdrvDirtyBitmap *dirty_bitmap;
53 HBitmapIter hbi;
54 uint8_t *buf;
55 QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
56 int buf_free_count;
57
58 unsigned long *in_flight_bitmap;
59 int in_flight;
60 int sectors_in_flight;
61 int ret;
62 bool unmap;
63 bool waiting_for_io;
64 } MirrorBlockJob;
65
66 typedef struct MirrorOp {
67 MirrorBlockJob *s;
68 QEMUIOVector qiov;
69 int64_t sector_num;
70 int nb_sectors;
71 } MirrorOp;
72
73 static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
74 int error)
75 {
76 s->synced = false;
77 if (read) {
78 return block_job_error_action(&s->common, s->common.bs,
79 s->on_source_error, true, error);
80 } else {
81 return block_job_error_action(&s->common, s->target,
82 s->on_target_error, false, error);
83 }
84 }
85
86 static void mirror_iteration_done(MirrorOp *op, int ret)
87 {
88 MirrorBlockJob *s = op->s;
89 struct iovec *iov;
90 int64_t chunk_num;
91 int i, nb_chunks, sectors_per_chunk;
92
93 trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
94
95 s->in_flight--;
96 s->sectors_in_flight -= op->nb_sectors;
97 iov = op->qiov.iov;
98 for (i = 0; i < op->qiov.niov; i++) {
99 MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
100 QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
101 s->buf_free_count++;
102 }
103
104 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
105 chunk_num = op->sector_num / sectors_per_chunk;
106 nb_chunks = op->nb_sectors / sectors_per_chunk;
107 bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
108 if (ret >= 0) {
109 if (s->cow_bitmap) {
110 bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
111 }
112 s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE;
113 }
114
115 qemu_iovec_destroy(&op->qiov);
116 g_free(op);
117
118 if (s->waiting_for_io) {
119 qemu_coroutine_enter(s->common.co, NULL);
120 }
121 }
122
123 static void mirror_write_complete(void *opaque, int ret)
124 {
125 MirrorOp *op = opaque;
126 MirrorBlockJob *s = op->s;
127 if (ret < 0) {
128 BlockErrorAction action;
129
130 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
131 action = mirror_error_action(s, false, -ret);
132 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
133 s->ret = ret;
134 }
135 }
136 mirror_iteration_done(op, ret);
137 }
138
139 static void mirror_read_complete(void *opaque, int ret)
140 {
141 MirrorOp *op = opaque;
142 MirrorBlockJob *s = op->s;
143 if (ret < 0) {
144 BlockErrorAction action;
145
146 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
147 action = mirror_error_action(s, true, -ret);
148 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
149 s->ret = ret;
150 }
151
152 mirror_iteration_done(op, ret);
153 return;
154 }
155 bdrv_aio_writev(s->target, op->sector_num, &op->qiov, op->nb_sectors,
156 mirror_write_complete, op);
157 }
158
159 static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
160 {
161 BlockDriverState *source = s->common.bs;
162 int nb_sectors, sectors_per_chunk, nb_chunks;
163 int64_t end, sector_num, next_chunk, next_sector, hbitmap_next_sector;
164 uint64_t delay_ns = 0;
165 MirrorOp *op;
166 int pnum;
167 int64_t ret;
168
169 s->sector_num = hbitmap_iter_next(&s->hbi);
170 if (s->sector_num < 0) {
171 bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
172 s->sector_num = hbitmap_iter_next(&s->hbi);
173 trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
174 assert(s->sector_num >= 0);
175 }
176
177 hbitmap_next_sector = s->sector_num;
178 sector_num = s->sector_num;
179 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
180 end = s->bdev_length / BDRV_SECTOR_SIZE;
181
182 /* Extend the QEMUIOVector to include all adjacent blocks that will
183 * be copied in this operation.
184 *
185 * We have to do this if we have no backing file yet in the destination,
186 * and the cluster size is very large. Then we need to do COW ourselves.
187 * The first time a cluster is copied, copy it entirely. Note that,
188 * because both the granularity and the cluster size are powers of two,
189 * the number of sectors to copy cannot exceed one cluster.
190 *
191 * We also want to extend the QEMUIOVector to include more adjacent
192 * dirty blocks if possible, to limit the number of I/O operations and
193 * run efficiently even with a small granularity.
194 */
195 nb_chunks = 0;
196 nb_sectors = 0;
197 next_sector = sector_num;
198 next_chunk = sector_num / sectors_per_chunk;
199
200 /* Wait for I/O to this cluster (from a previous iteration) to be done. */
201 while (test_bit(next_chunk, s->in_flight_bitmap)) {
202 trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
203 s->waiting_for_io = true;
204 qemu_coroutine_yield();
205 s->waiting_for_io = false;
206 }
207
208 do {
209 int added_sectors, added_chunks;
210
211 if (!bdrv_get_dirty(source, s->dirty_bitmap, next_sector) ||
212 test_bit(next_chunk, s->in_flight_bitmap)) {
213 assert(nb_sectors > 0);
214 break;
215 }
216
217 added_sectors = sectors_per_chunk;
218 if (s->cow_bitmap && !test_bit(next_chunk, s->cow_bitmap)) {
219 bdrv_round_to_clusters(s->target,
220 next_sector, added_sectors,
221 &next_sector, &added_sectors);
222
223 /* On the first iteration, the rounding may make us copy
224 * sectors before the first dirty one.
225 */
226 if (next_sector < sector_num) {
227 assert(nb_sectors == 0);
228 sector_num = next_sector;
229 next_chunk = next_sector / sectors_per_chunk;
230 }
231 }
232
233 added_sectors = MIN(added_sectors, end - (sector_num + nb_sectors));
234 added_chunks = (added_sectors + sectors_per_chunk - 1) / sectors_per_chunk;
235
236 /* When doing COW, it may happen that there is not enough space for
237 * a full cluster. Wait if that is the case.
238 */
239 while (nb_chunks == 0 && s->buf_free_count < added_chunks) {
240 trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight);
241 s->waiting_for_io = true;
242 qemu_coroutine_yield();
243 s->waiting_for_io = false;
244 }
245 if (s->buf_free_count < nb_chunks + added_chunks) {
246 trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
247 break;
248 }
249 if (IOV_MAX < nb_chunks + added_chunks) {
250 trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
251 break;
252 }
253
254 /* We have enough free space to copy these sectors. */
255 bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
256
257 nb_sectors += added_sectors;
258 nb_chunks += added_chunks;
259 next_sector += added_sectors;
260 next_chunk += added_chunks;
261 if (!s->synced && s->common.speed) {
262 delay_ns = ratelimit_calculate_delay(&s->limit, added_sectors);
263 }
264 } while (delay_ns == 0 && next_sector < end);
265
266 /* Allocate a MirrorOp that is used as an AIO callback. */
267 op = g_new(MirrorOp, 1);
268 op->s = s;
269 op->sector_num = sector_num;
270 op->nb_sectors = nb_sectors;
271
272 /* Now make a QEMUIOVector taking enough granularity-sized chunks
273 * from s->buf_free.
274 */
275 qemu_iovec_init(&op->qiov, nb_chunks);
276 next_sector = sector_num;
277 while (nb_chunks-- > 0) {
278 MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
279 size_t remaining = (nb_sectors * BDRV_SECTOR_SIZE) - op->qiov.size;
280
281 QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
282 s->buf_free_count--;
283 qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
284
285 /* Advance the HBitmapIter in parallel, so that we do not examine
286 * the same sector twice.
287 */
288 if (next_sector > hbitmap_next_sector
289 && bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) {
290 hbitmap_next_sector = hbitmap_iter_next(&s->hbi);
291 }
292
293 next_sector += sectors_per_chunk;
294 }
295
296 bdrv_reset_dirty_bitmap(s->dirty_bitmap, sector_num, nb_sectors);
297
298 /* Copy the dirty cluster. */
299 s->in_flight++;
300 s->sectors_in_flight += nb_sectors;
301 trace_mirror_one_iteration(s, sector_num, nb_sectors);
302
303 ret = bdrv_get_block_status_above(source, NULL, sector_num,
304 nb_sectors, &pnum);
305 if (ret < 0 || pnum < nb_sectors ||
306 (ret & BDRV_BLOCK_DATA && !(ret & BDRV_BLOCK_ZERO))) {
307 bdrv_aio_readv(source, sector_num, &op->qiov, nb_sectors,
308 mirror_read_complete, op);
309 } else if (ret & BDRV_BLOCK_ZERO) {
310 bdrv_aio_write_zeroes(s->target, sector_num, op->nb_sectors,
311 s->unmap ? BDRV_REQ_MAY_UNMAP : 0,
312 mirror_write_complete, op);
313 } else {
314 assert(!(ret & BDRV_BLOCK_DATA));
315 bdrv_aio_discard(s->target, sector_num, op->nb_sectors,
316 mirror_write_complete, op);
317 }
318 return delay_ns;
319 }
320
321 static void mirror_free_init(MirrorBlockJob *s)
322 {
323 int granularity = s->granularity;
324 size_t buf_size = s->buf_size;
325 uint8_t *buf = s->buf;
326
327 assert(s->buf_free_count == 0);
328 QSIMPLEQ_INIT(&s->buf_free);
329 while (buf_size != 0) {
330 MirrorBuffer *cur = (MirrorBuffer *)buf;
331 QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
332 s->buf_free_count++;
333 buf_size -= granularity;
334 buf += granularity;
335 }
336 }
337
338 static void mirror_drain(MirrorBlockJob *s)
339 {
340 while (s->in_flight > 0) {
341 s->waiting_for_io = true;
342 qemu_coroutine_yield();
343 s->waiting_for_io = false;
344 }
345 }
346
347 typedef struct {
348 int ret;
349 } MirrorExitData;
350
351 static void mirror_exit(BlockJob *job, void *opaque)
352 {
353 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
354 MirrorExitData *data = opaque;
355 AioContext *replace_aio_context = NULL;
356
357 if (s->to_replace) {
358 replace_aio_context = bdrv_get_aio_context(s->to_replace);
359 aio_context_acquire(replace_aio_context);
360 }
361
362 if (s->should_complete && data->ret == 0) {
363 BlockDriverState *to_replace = s->common.bs;
364 if (s->to_replace) {
365 to_replace = s->to_replace;
366 }
367 if (bdrv_get_flags(s->target) != bdrv_get_flags(to_replace)) {
368 bdrv_reopen(s->target, bdrv_get_flags(to_replace), NULL);
369 }
370 bdrv_swap(s->target, to_replace);
371 if (s->common.driver->job_type == BLOCK_JOB_TYPE_COMMIT) {
372 /* drop the bs loop chain formed by the swap: break the loop then
373 * trigger the unref */
374 /* FIXME This duplicates bdrv_set_backing_hd(), except for the
375 * actual detach/unref so that the loop can be broken. When
376 * bdrv_swap() gets replaced, this will become sane again. */
377 BlockDriverState *backing = s->base->backing->bs;
378 assert(s->base->backing_blocker);
379 bdrv_op_unblock_all(backing, s->base->backing_blocker);
380 error_free(s->base->backing_blocker);
381 s->base->backing_blocker = NULL;
382 bdrv_detach_child(s->base->backing);
383 s->base->backing = NULL;
384 bdrv_unref(backing);
385 }
386 }
387 if (s->to_replace) {
388 bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
389 error_free(s->replace_blocker);
390 bdrv_unref(s->to_replace);
391 }
392 if (replace_aio_context) {
393 aio_context_release(replace_aio_context);
394 }
395 g_free(s->replaces);
396 bdrv_unref(s->target);
397 block_job_completed(&s->common, data->ret);
398 g_free(data);
399 }
400
401 static void coroutine_fn mirror_run(void *opaque)
402 {
403 MirrorBlockJob *s = opaque;
404 MirrorExitData *data;
405 BlockDriverState *bs = s->common.bs;
406 int64_t sector_num, end, length;
407 uint64_t last_pause_ns;
408 BlockDriverInfo bdi;
409 char backing_filename[2]; /* we only need 2 characters because we are only
410 checking for a NULL string */
411 int ret = 0;
412 int n;
413
414 if (block_job_is_cancelled(&s->common)) {
415 goto immediate_exit;
416 }
417
418 s->bdev_length = bdrv_getlength(bs);
419 if (s->bdev_length < 0) {
420 ret = s->bdev_length;
421 goto immediate_exit;
422 } else if (s->bdev_length == 0) {
423 /* Report BLOCK_JOB_READY and wait for complete. */
424 block_job_event_ready(&s->common);
425 s->synced = true;
426 while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
427 block_job_yield(&s->common);
428 }
429 s->common.cancelled = false;
430 goto immediate_exit;
431 }
432
433 length = DIV_ROUND_UP(s->bdev_length, s->granularity);
434 s->in_flight_bitmap = bitmap_new(length);
435
436 /* If we have no backing file yet in the destination, we cannot let
437 * the destination do COW. Instead, we copy sectors around the
438 * dirty data if needed. We need a bitmap to do that.
439 */
440 bdrv_get_backing_filename(s->target, backing_filename,
441 sizeof(backing_filename));
442 if (backing_filename[0] && !s->target->backing) {
443 ret = bdrv_get_info(s->target, &bdi);
444 if (ret < 0) {
445 goto immediate_exit;
446 }
447 if (s->granularity < bdi.cluster_size) {
448 s->buf_size = MAX(s->buf_size, bdi.cluster_size);
449 s->cow_bitmap = bitmap_new(length);
450 }
451 }
452
453 end = s->bdev_length / BDRV_SECTOR_SIZE;
454 s->buf = qemu_try_blockalign(bs, s->buf_size);
455 if (s->buf == NULL) {
456 ret = -ENOMEM;
457 goto immediate_exit;
458 }
459
460 mirror_free_init(s);
461
462 last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
463 if (!s->is_none_mode) {
464 /* First part, loop on the sectors and initialize the dirty bitmap. */
465 BlockDriverState *base = s->base;
466 bool mark_all_dirty = s->base == NULL && !bdrv_has_zero_init(s->target);
467
468 for (sector_num = 0; sector_num < end; ) {
469 /* Just to make sure we are not exceeding int limit. */
470 int nb_sectors = MIN(INT_MAX >> BDRV_SECTOR_BITS,
471 end - sector_num);
472 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
473
474 if (now - last_pause_ns > SLICE_TIME) {
475 last_pause_ns = now;
476 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, 0);
477 }
478
479 if (block_job_is_cancelled(&s->common)) {
480 goto immediate_exit;
481 }
482
483 ret = bdrv_is_allocated_above(bs, base, sector_num, nb_sectors, &n);
484
485 if (ret < 0) {
486 goto immediate_exit;
487 }
488
489 assert(n > 0);
490 if (ret == 1 || mark_all_dirty) {
491 bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n);
492 }
493 sector_num += n;
494 }
495 }
496
497 bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
498 for (;;) {
499 uint64_t delay_ns = 0;
500 int64_t cnt;
501 bool should_complete;
502
503 if (s->ret < 0) {
504 ret = s->ret;
505 goto immediate_exit;
506 }
507
508 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
509 /* s->common.offset contains the number of bytes already processed so
510 * far, cnt is the number of dirty sectors remaining and
511 * s->sectors_in_flight is the number of sectors currently being
512 * processed; together those are the current total operation length */
513 s->common.len = s->common.offset +
514 (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE;
515
516 /* Note that even when no rate limit is applied we need to yield
517 * periodically with no pending I/O so that bdrv_drain_all() returns.
518 * We do so every SLICE_TIME nanoseconds, or when there is an error,
519 * or when the source is clean, whichever comes first.
520 */
521 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - last_pause_ns < SLICE_TIME &&
522 s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
523 if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 ||
524 (cnt == 0 && s->in_flight > 0)) {
525 trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt);
526 s->waiting_for_io = true;
527 qemu_coroutine_yield();
528 s->waiting_for_io = false;
529 continue;
530 } else if (cnt != 0) {
531 delay_ns = mirror_iteration(s);
532 }
533 }
534
535 should_complete = false;
536 if (s->in_flight == 0 && cnt == 0) {
537 trace_mirror_before_flush(s);
538 ret = bdrv_flush(s->target);
539 if (ret < 0) {
540 if (mirror_error_action(s, false, -ret) ==
541 BLOCK_ERROR_ACTION_REPORT) {
542 goto immediate_exit;
543 }
544 } else {
545 /* We're out of the streaming phase. From now on, if the job
546 * is cancelled we will actually complete all pending I/O and
547 * report completion. This way, block-job-cancel will leave
548 * the target in a consistent state.
549 */
550 if (!s->synced) {
551 block_job_event_ready(&s->common);
552 s->synced = true;
553 }
554
555 should_complete = s->should_complete ||
556 block_job_is_cancelled(&s->common);
557 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
558 }
559 }
560
561 if (cnt == 0 && should_complete) {
562 /* The dirty bitmap is not updated while operations are pending.
563 * If we're about to exit, wait for pending operations before
564 * calling bdrv_get_dirty_count(bs), or we may exit while the
565 * source has dirty data to copy!
566 *
567 * Note that I/O can be submitted by the guest while
568 * mirror_populate runs.
569 */
570 trace_mirror_before_drain(s, cnt);
571 bdrv_drain(bs);
572 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
573 }
574
575 ret = 0;
576 trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
577 if (!s->synced) {
578 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
579 if (block_job_is_cancelled(&s->common)) {
580 break;
581 }
582 } else if (!should_complete) {
583 delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
584 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
585 } else if (cnt == 0) {
586 /* The two disks are in sync. Exit and report successful
587 * completion.
588 */
589 assert(QLIST_EMPTY(&bs->tracked_requests));
590 s->common.cancelled = false;
591 break;
592 }
593 last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
594 }
595
596 immediate_exit:
597 if (s->in_flight > 0) {
598 /* We get here only if something went wrong. Either the job failed,
599 * or it was cancelled prematurely so that we do not guarantee that
600 * the target is a copy of the source.
601 */
602 assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
603 mirror_drain(s);
604 }
605
606 assert(s->in_flight == 0);
607 qemu_vfree(s->buf);
608 g_free(s->cow_bitmap);
609 g_free(s->in_flight_bitmap);
610 bdrv_release_dirty_bitmap(bs, s->dirty_bitmap);
611 bdrv_iostatus_disable(s->target);
612
613 data = g_malloc(sizeof(*data));
614 data->ret = ret;
615 block_job_defer_to_main_loop(&s->common, mirror_exit, data);
616 }
617
618 static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
619 {
620 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
621
622 if (speed < 0) {
623 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
624 return;
625 }
626 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
627 }
628
629 static void mirror_iostatus_reset(BlockJob *job)
630 {
631 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
632
633 bdrv_iostatus_reset(s->target);
634 }
635
636 static void mirror_complete(BlockJob *job, Error **errp)
637 {
638 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
639 Error *local_err = NULL;
640 int ret;
641
642 ret = bdrv_open_backing_file(s->target, NULL, &local_err);
643 if (ret < 0) {
644 error_propagate(errp, local_err);
645 return;
646 }
647 if (!s->synced) {
648 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
649 return;
650 }
651
652 /* check the target bs is not blocked and block all operations on it */
653 if (s->replaces) {
654 AioContext *replace_aio_context;
655
656 s->to_replace = bdrv_find_node(s->replaces);
657 if (!s->to_replace) {
658 error_setg(errp, "Node name '%s' not found", s->replaces);
659 return;
660 }
661
662 replace_aio_context = bdrv_get_aio_context(s->to_replace);
663 aio_context_acquire(replace_aio_context);
664
665 error_setg(&s->replace_blocker,
666 "block device is in use by block-job-complete");
667 bdrv_op_block_all(s->to_replace, s->replace_blocker);
668 bdrv_ref(s->to_replace);
669
670 aio_context_release(replace_aio_context);
671 }
672
673 s->should_complete = true;
674 block_job_enter(&s->common);
675 }
676
677 static const BlockJobDriver mirror_job_driver = {
678 .instance_size = sizeof(MirrorBlockJob),
679 .job_type = BLOCK_JOB_TYPE_MIRROR,
680 .set_speed = mirror_set_speed,
681 .iostatus_reset= mirror_iostatus_reset,
682 .complete = mirror_complete,
683 };
684
685 static const BlockJobDriver commit_active_job_driver = {
686 .instance_size = sizeof(MirrorBlockJob),
687 .job_type = BLOCK_JOB_TYPE_COMMIT,
688 .set_speed = mirror_set_speed,
689 .iostatus_reset
690 = mirror_iostatus_reset,
691 .complete = mirror_complete,
692 };
693
694 static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
695 const char *replaces,
696 int64_t speed, uint32_t granularity,
697 int64_t buf_size,
698 BlockdevOnError on_source_error,
699 BlockdevOnError on_target_error,
700 bool unmap,
701 BlockCompletionFunc *cb,
702 void *opaque, Error **errp,
703 const BlockJobDriver *driver,
704 bool is_none_mode, BlockDriverState *base)
705 {
706 MirrorBlockJob *s;
707
708 if (granularity == 0) {
709 granularity = bdrv_get_default_bitmap_granularity(target);
710 }
711
712 assert ((granularity & (granularity - 1)) == 0);
713
714 if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
715 on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
716 !bdrv_iostatus_is_enabled(bs)) {
717 error_setg(errp, QERR_INVALID_PARAMETER, "on-source-error");
718 return;
719 }
720
721 if (buf_size < 0) {
722 error_setg(errp, "Invalid parameter 'buf-size'");
723 return;
724 }
725
726 if (buf_size == 0) {
727 buf_size = DEFAULT_MIRROR_BUF_SIZE;
728 }
729
730 s = block_job_create(driver, bs, speed, cb, opaque, errp);
731 if (!s) {
732 return;
733 }
734
735 s->replaces = g_strdup(replaces);
736 s->on_source_error = on_source_error;
737 s->on_target_error = on_target_error;
738 s->target = target;
739 s->is_none_mode = is_none_mode;
740 s->base = base;
741 s->granularity = granularity;
742 s->buf_size = ROUND_UP(buf_size, granularity);
743 s->unmap = unmap;
744
745 s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
746 if (!s->dirty_bitmap) {
747 g_free(s->replaces);
748 block_job_release(bs);
749 return;
750 }
751 bdrv_set_enable_write_cache(s->target, true);
752 bdrv_set_on_error(s->target, on_target_error, on_target_error);
753 bdrv_iostatus_enable(s->target);
754 s->common.co = qemu_coroutine_create(mirror_run);
755 trace_mirror_start(bs, s, s->common.co, opaque);
756 qemu_coroutine_enter(s->common.co, s);
757 }
758
759 void mirror_start(BlockDriverState *bs, BlockDriverState *target,
760 const char *replaces,
761 int64_t speed, uint32_t granularity, int64_t buf_size,
762 MirrorSyncMode mode, BlockdevOnError on_source_error,
763 BlockdevOnError on_target_error,
764 bool unmap,
765 BlockCompletionFunc *cb,
766 void *opaque, Error **errp)
767 {
768 bool is_none_mode;
769 BlockDriverState *base;
770
771 if (mode == MIRROR_SYNC_MODE_INCREMENTAL) {
772 error_setg(errp, "Sync mode 'incremental' not supported");
773 return;
774 }
775 is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
776 base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL;
777 mirror_start_job(bs, target, replaces,
778 speed, granularity, buf_size,
779 on_source_error, on_target_error, unmap, cb, opaque, errp,
780 &mirror_job_driver, is_none_mode, base);
781 }
782
783 void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
784 int64_t speed,
785 BlockdevOnError on_error,
786 BlockCompletionFunc *cb,
787 void *opaque, Error **errp)
788 {
789 int64_t length, base_length;
790 int orig_base_flags;
791 int ret;
792 Error *local_err = NULL;
793
794 orig_base_flags = bdrv_get_flags(base);
795
796 if (bdrv_reopen(base, bs->open_flags, errp)) {
797 return;
798 }
799
800 length = bdrv_getlength(bs);
801 if (length < 0) {
802 error_setg_errno(errp, -length,
803 "Unable to determine length of %s", bs->filename);
804 goto error_restore_flags;
805 }
806
807 base_length = bdrv_getlength(base);
808 if (base_length < 0) {
809 error_setg_errno(errp, -base_length,
810 "Unable to determine length of %s", base->filename);
811 goto error_restore_flags;
812 }
813
814 if (length > base_length) {
815 ret = bdrv_truncate(base, length);
816 if (ret < 0) {
817 error_setg_errno(errp, -ret,
818 "Top image %s is larger than base image %s, and "
819 "resize of base image failed",
820 bs->filename, base->filename);
821 goto error_restore_flags;
822 }
823 }
824
825 bdrv_ref(base);
826 mirror_start_job(bs, base, NULL, speed, 0, 0,
827 on_error, on_error, false, cb, opaque, &local_err,
828 &commit_active_job_driver, false, base);
829 if (local_err) {
830 error_propagate(errp, local_err);
831 goto error_restore_flags;
832 }
833
834 return;
835
836 error_restore_flags:
837 /* ignore error and errp for bdrv_reopen, because we want to propagate
838 * the original error */
839 bdrv_reopen(base, orig_base_flags, NULL);
840 return;
841 }