1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "qemu/osdep.h"
15#include "qemu/cutils.h"
16#include "trace.h"
17#include "block/blockjob_int.h"
18#include "block/block_int.h"
19#include "sysemu/block-backend.h"
20#include "qapi/error.h"
21#include "qapi/qmp/qerror.h"
22#include "qemu/ratelimit.h"
23#include "qemu/bitmap.h"
24
25#define SLICE_TIME 100000000ULL
26#define MAX_IN_FLIGHT 16
27#define MAX_IO_BYTES (1 << 20)
28#define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES)
29
30
31
32
33typedef struct MirrorBuffer {
34 QSIMPLEQ_ENTRY(MirrorBuffer) next;
35} MirrorBuffer;
36
37typedef struct MirrorBlockJob {
38 BlockJob common;
39 RateLimit limit;
40 BlockBackend *target;
41 BlockDriverState *mirror_top_bs;
42 BlockDriverState *source;
43 BlockDriverState *base;
44
45
46 char *replaces;
47
48 BlockDriverState *to_replace;
49
50 Error *replace_blocker;
51 bool is_none_mode;
52 BlockMirrorBackingMode backing_mode;
53 BlockdevOnError on_source_error, on_target_error;
54 bool synced;
55 bool should_complete;
56 int64_t granularity;
57 size_t buf_size;
58 int64_t bdev_length;
59 unsigned long *cow_bitmap;
60 BdrvDirtyBitmap *dirty_bitmap;
61 BdrvDirtyBitmapIter *dbi;
62 uint8_t *buf;
63 QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
64 int buf_free_count;
65
66 uint64_t last_pause_ns;
67 unsigned long *in_flight_bitmap;
68 int in_flight;
69 int64_t bytes_in_flight;
70 int ret;
71 bool unmap;
72 bool waiting_for_io;
73 int target_cluster_size;
74 int max_iov;
75 bool initial_zeroing_ongoing;
76} MirrorBlockJob;
77
78typedef struct MirrorOp {
79 MirrorBlockJob *s;
80 QEMUIOVector qiov;
81 int64_t offset;
82 uint64_t bytes;
83} MirrorOp;
84
85static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
86 int error)
87{
88 s->synced = false;
89 if (read) {
90 return block_job_error_action(&s->common, s->on_source_error,
91 true, error);
92 } else {
93 return block_job_error_action(&s->common, s->on_target_error,
94 false, error);
95 }
96}
97
98static void mirror_iteration_done(MirrorOp *op, int ret)
99{
100 MirrorBlockJob *s = op->s;
101 struct iovec *iov;
102 int64_t chunk_num;
103 int i, nb_chunks;
104
105 trace_mirror_iteration_done(s, op->offset, op->bytes, ret);
106
107 s->in_flight--;
108 s->bytes_in_flight -= op->bytes;
109 iov = op->qiov.iov;
110 for (i = 0; i < op->qiov.niov; i++) {
111 MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
112 QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
113 s->buf_free_count++;
114 }
115
116 chunk_num = op->offset / s->granularity;
117 nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity);
118 bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
119 if (ret >= 0) {
120 if (s->cow_bitmap) {
121 bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
122 }
123 if (!s->initial_zeroing_ongoing) {
124 s->common.offset += op->bytes;
125 }
126 }
127 qemu_iovec_destroy(&op->qiov);
128 g_free(op);
129
130 if (s->waiting_for_io) {
131 qemu_coroutine_enter(s->common.co);
132 }
133}
134
135static void mirror_write_complete(void *opaque, int ret)
136{
137 MirrorOp *op = opaque;
138 MirrorBlockJob *s = op->s;
139
140 aio_context_acquire(blk_get_aio_context(s->common.blk));
141 if (ret < 0) {
142 BlockErrorAction action;
143
144 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes);
145 action = mirror_error_action(s, false, -ret);
146 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
147 s->ret = ret;
148 }
149 }
150 mirror_iteration_done(op, ret);
151 aio_context_release(blk_get_aio_context(s->common.blk));
152}
153
154static void mirror_read_complete(void *opaque, int ret)
155{
156 MirrorOp *op = opaque;
157 MirrorBlockJob *s = op->s;
158
159 aio_context_acquire(blk_get_aio_context(s->common.blk));
160 if (ret < 0) {
161 BlockErrorAction action;
162
163 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes);
164 action = mirror_error_action(s, true, -ret);
165 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
166 s->ret = ret;
167 }
168
169 mirror_iteration_done(op, ret);
170 } else {
171 blk_aio_pwritev(s->target, op->offset, &op->qiov,
172 0, mirror_write_complete, op);
173 }
174 aio_context_release(blk_get_aio_context(s->common.blk));
175}
176
177
178static inline int64_t mirror_clip_bytes(MirrorBlockJob *s,
179 int64_t offset,
180 int64_t bytes)
181{
182 return MIN(bytes, s->bdev_length - offset);
183}
184
185
186
187static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset,
188 uint64_t *bytes)
189{
190 bool need_cow;
191 int ret = 0;
192 int64_t align_offset = *offset;
193 int64_t align_bytes = *bytes;
194 int max_bytes = s->granularity * s->max_iov;
195
196 need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap);
197 need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity,
198 s->cow_bitmap);
199 if (need_cow) {
200 bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes,
201 &align_offset, &align_bytes);
202 }
203
204 if (align_bytes > max_bytes) {
205 align_bytes = max_bytes;
206 if (need_cow) {
207 align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size);
208 }
209 }
210
211
212 align_bytes = mirror_clip_bytes(s, align_offset, align_bytes);
213
214 ret = align_offset + align_bytes - (*offset + *bytes);
215 *offset = align_offset;
216 *bytes = align_bytes;
217 assert(ret >= 0);
218 return ret;
219}
220
221static inline void mirror_wait_for_io(MirrorBlockJob *s)
222{
223 assert(!s->waiting_for_io);
224 s->waiting_for_io = true;
225 qemu_coroutine_yield();
226 s->waiting_for_io = false;
227}
228
229
230
231
232
233
234
235
236static uint64_t mirror_do_read(MirrorBlockJob *s, int64_t offset,
237 uint64_t bytes)
238{
239 BlockBackend *source = s->common.blk;
240 int nb_chunks;
241 uint64_t ret;
242 MirrorOp *op;
243 uint64_t max_bytes;
244
245 max_bytes = s->granularity * s->max_iov;
246
247
248 bytes = MIN(s->buf_size, MIN(max_bytes, bytes));
249 assert(bytes);
250 assert(bytes < BDRV_REQUEST_MAX_BYTES);
251 ret = bytes;
252
253 if (s->cow_bitmap) {
254 ret += mirror_cow_align(s, &offset, &bytes);
255 }
256 assert(bytes <= s->buf_size);
257
258
259
260 assert(QEMU_IS_ALIGNED(offset, s->granularity));
261
262 assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
263 nb_chunks = DIV_ROUND_UP(bytes, s->granularity);
264
265 while (s->buf_free_count < nb_chunks) {
266 trace_mirror_yield_in_flight(s, offset, s->in_flight);
267 mirror_wait_for_io(s);
268 }
269
270
271 op = g_new(MirrorOp, 1);
272 op->s = s;
273 op->offset = offset;
274 op->bytes = bytes;
275
276
277
278
279 qemu_iovec_init(&op->qiov, nb_chunks);
280 while (nb_chunks-- > 0) {
281 MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
282 size_t remaining = bytes - op->qiov.size;
283
284 QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
285 s->buf_free_count--;
286 qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
287 }
288
289
290 s->in_flight++;
291 s->bytes_in_flight += bytes;
292 trace_mirror_one_iteration(s, offset, bytes);
293
294 blk_aio_preadv(source, offset, &op->qiov, 0, mirror_read_complete, op);
295 return ret;
296}
297
298static void mirror_do_zero_or_discard(MirrorBlockJob *s,
299 int64_t offset,
300 uint64_t bytes,
301 bool is_discard)
302{
303 MirrorOp *op;
304
305
306
307 op = g_new0(MirrorOp, 1);
308 op->s = s;
309 op->offset = offset;
310 op->bytes = bytes;
311
312 s->in_flight++;
313 s->bytes_in_flight += bytes;
314 if (is_discard) {
315 blk_aio_pdiscard(s->target, offset,
316 op->bytes, mirror_write_complete, op);
317 } else {
318 blk_aio_pwrite_zeroes(s->target, offset,
319 op->bytes, s->unmap ? BDRV_REQ_MAY_UNMAP : 0,
320 mirror_write_complete, op);
321 }
322}
323
324static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
325{
326 BlockDriverState *source = s->source;
327 int64_t offset, first_chunk;
328 uint64_t delay_ns = 0;
329
330 int nb_chunks = 1;
331 bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target));
332 int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES);
333
334 bdrv_dirty_bitmap_lock(s->dirty_bitmap);
335 offset = bdrv_dirty_iter_next(s->dbi);
336 if (offset < 0) {
337 bdrv_set_dirty_iter(s->dbi, 0);
338 offset = bdrv_dirty_iter_next(s->dbi);
339 trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
340 assert(offset >= 0);
341 }
342 bdrv_dirty_bitmap_unlock(s->dirty_bitmap);
343
344 first_chunk = offset / s->granularity;
345 while (test_bit(first_chunk, s->in_flight_bitmap)) {
346 trace_mirror_yield_in_flight(s, offset, s->in_flight);
347 mirror_wait_for_io(s);
348 }
349
350 block_job_pause_point(&s->common);
351
352
353
354 bdrv_dirty_bitmap_lock(s->dirty_bitmap);
355 while (nb_chunks * s->granularity < s->buf_size) {
356 int64_t next_dirty;
357 int64_t next_offset = offset + nb_chunks * s->granularity;
358 int64_t next_chunk = next_offset / s->granularity;
359 if (next_offset >= s->bdev_length ||
360 !bdrv_get_dirty_locked(source, s->dirty_bitmap, next_offset)) {
361 break;
362 }
363 if (test_bit(next_chunk, s->in_flight_bitmap)) {
364 break;
365 }
366
367 next_dirty = bdrv_dirty_iter_next(s->dbi);
368 if (next_dirty > next_offset || next_dirty < 0) {
369
370 bdrv_set_dirty_iter(s->dbi, next_offset);
371 next_dirty = bdrv_dirty_iter_next(s->dbi);
372 }
373 assert(next_dirty == next_offset);
374 nb_chunks++;
375 }
376
377
378
379
380
381 bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset,
382 nb_chunks * s->granularity);
383 bdrv_dirty_bitmap_unlock(s->dirty_bitmap);
384
385 bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks);
386 while (nb_chunks > 0 && offset < s->bdev_length) {
387 int ret;
388 int64_t io_bytes;
389 int64_t io_bytes_acct;
390 enum MirrorMethod {
391 MIRROR_METHOD_COPY,
392 MIRROR_METHOD_ZERO,
393 MIRROR_METHOD_DISCARD
394 } mirror_method = MIRROR_METHOD_COPY;
395
396 assert(!(offset % s->granularity));
397 ret = bdrv_block_status_above(source, NULL, offset,
398 nb_chunks * s->granularity,
399 &io_bytes, NULL, NULL);
400 if (ret < 0) {
401 io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes);
402 } else if (ret & BDRV_BLOCK_DATA) {
403 io_bytes = MIN(io_bytes, max_io_bytes);
404 }
405
406 io_bytes -= io_bytes % s->granularity;
407 if (io_bytes < s->granularity) {
408 io_bytes = s->granularity;
409 } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) {
410 int64_t target_offset;
411 int64_t target_bytes;
412 bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes,
413 &target_offset, &target_bytes);
414 if (target_offset == offset &&
415 target_bytes == io_bytes) {
416 mirror_method = ret & BDRV_BLOCK_ZERO ?
417 MIRROR_METHOD_ZERO :
418 MIRROR_METHOD_DISCARD;
419 }
420 }
421
422 while (s->in_flight >= MAX_IN_FLIGHT) {
423 trace_mirror_yield_in_flight(s, offset, s->in_flight);
424 mirror_wait_for_io(s);
425 }
426
427 if (s->ret < 0) {
428 return 0;
429 }
430
431 io_bytes = mirror_clip_bytes(s, offset, io_bytes);
432 switch (mirror_method) {
433 case MIRROR_METHOD_COPY:
434 io_bytes = io_bytes_acct = mirror_do_read(s, offset, io_bytes);
435 break;
436 case MIRROR_METHOD_ZERO:
437 case MIRROR_METHOD_DISCARD:
438 mirror_do_zero_or_discard(s, offset, io_bytes,
439 mirror_method == MIRROR_METHOD_DISCARD);
440 if (write_zeroes_ok) {
441 io_bytes_acct = 0;
442 } else {
443 io_bytes_acct = io_bytes;
444 }
445 break;
446 default:
447 abort();
448 }
449 assert(io_bytes);
450 offset += io_bytes;
451 nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity);
452 if (s->common.speed) {
453 delay_ns = ratelimit_calculate_delay(&s->limit, io_bytes_acct);
454 }
455 }
456 return delay_ns;
457}
458
459static void mirror_free_init(MirrorBlockJob *s)
460{
461 int granularity = s->granularity;
462 size_t buf_size = s->buf_size;
463 uint8_t *buf = s->buf;
464
465 assert(s->buf_free_count == 0);
466 QSIMPLEQ_INIT(&s->buf_free);
467 while (buf_size != 0) {
468 MirrorBuffer *cur = (MirrorBuffer *)buf;
469 QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
470 s->buf_free_count++;
471 buf_size -= granularity;
472 buf += granularity;
473 }
474}
475
476
477
478
479
480static void mirror_wait_for_all_io(MirrorBlockJob *s)
481{
482 while (s->in_flight > 0) {
483 mirror_wait_for_io(s);
484 }
485}
486
487typedef struct {
488 int ret;
489} MirrorExitData;
490
491static void mirror_exit(BlockJob *job, void *opaque)
492{
493 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
494 MirrorExitData *data = opaque;
495 AioContext *replace_aio_context = NULL;
496 BlockDriverState *src = s->source;
497 BlockDriverState *target_bs = blk_bs(s->target);
498 BlockDriverState *mirror_top_bs = s->mirror_top_bs;
499 Error *local_err = NULL;
500
501 bdrv_release_dirty_bitmap(src, s->dirty_bitmap);
502
503
504
505 bdrv_ref(src);
506 bdrv_ref(mirror_top_bs);
507 bdrv_ref(target_bs);
508
509
510
511
512
513
514
515
516 blk_set_perm(s->target, 0, BLK_PERM_ALL, &error_abort);
517 blk_unref(s->target);
518 s->target = NULL;
519
520
521
522 bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
523 &error_abort);
524 if (s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) {
525 BlockDriverState *backing = s->is_none_mode ? src : s->base;
526 if (backing_bs(target_bs) != backing) {
527 bdrv_set_backing_hd(target_bs, backing, &local_err);
528 if (local_err) {
529 error_report_err(local_err);
530 data->ret = -EPERM;
531 }
532 }
533 }
534
535 if (s->to_replace) {
536 replace_aio_context = bdrv_get_aio_context(s->to_replace);
537 aio_context_acquire(replace_aio_context);
538 }
539
540 if (s->should_complete && data->ret == 0) {
541 BlockDriverState *to_replace = src;
542 if (s->to_replace) {
543 to_replace = s->to_replace;
544 }
545
546 if (bdrv_get_flags(target_bs) != bdrv_get_flags(to_replace)) {
547 bdrv_reopen(target_bs, bdrv_get_flags(to_replace), NULL);
548 }
549
550
551
552 bdrv_drained_begin(target_bs);
553 bdrv_replace_node(to_replace, target_bs, &local_err);
554 bdrv_drained_end(target_bs);
555 if (local_err) {
556 error_report_err(local_err);
557 data->ret = -EPERM;
558 }
559 }
560 if (s->to_replace) {
561 bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
562 error_free(s->replace_blocker);
563 bdrv_unref(s->to_replace);
564 }
565 if (replace_aio_context) {
566 aio_context_release(replace_aio_context);
567 }
568 g_free(s->replaces);
569 bdrv_unref(target_bs);
570
571
572
573
574
575 block_job_remove_all_bdrv(job);
576 bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
577 &error_abort);
578 bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort);
579
580
581
582
583 blk_remove_bs(job->blk);
584 blk_set_perm(job->blk, 0, BLK_PERM_ALL, &error_abort);
585 blk_insert_bs(job->blk, mirror_top_bs, &error_abort);
586
587 block_job_completed(&s->common, data->ret);
588
589 g_free(data);
590 bdrv_drained_end(src);
591 bdrv_unref(mirror_top_bs);
592 bdrv_unref(src);
593}
594
595static void mirror_throttle(MirrorBlockJob *s)
596{
597 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
598
599 if (now - s->last_pause_ns > SLICE_TIME) {
600 s->last_pause_ns = now;
601 block_job_sleep_ns(&s->common, 0);
602 } else {
603 block_job_pause_point(&s->common);
604 }
605}
606
607static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
608{
609 int64_t offset;
610 BlockDriverState *base = s->base;
611 BlockDriverState *bs = s->source;
612 BlockDriverState *target_bs = blk_bs(s->target);
613 int ret;
614 int64_t count;
615
616 if (base == NULL && !bdrv_has_zero_init(target_bs)) {
617 if (!bdrv_can_write_zeroes_with_unmap(target_bs)) {
618 bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length);
619 return 0;
620 }
621
622 s->initial_zeroing_ongoing = true;
623 for (offset = 0; offset < s->bdev_length; ) {
624 int bytes = MIN(s->bdev_length - offset,
625 QEMU_ALIGN_DOWN(INT_MAX, s->granularity));
626
627 mirror_throttle(s);
628
629 if (block_job_is_cancelled(&s->common)) {
630 s->initial_zeroing_ongoing = false;
631 return 0;
632 }
633
634 if (s->in_flight >= MAX_IN_FLIGHT) {
635 trace_mirror_yield(s, UINT64_MAX, s->buf_free_count,
636 s->in_flight);
637 mirror_wait_for_io(s);
638 continue;
639 }
640
641 mirror_do_zero_or_discard(s, offset, bytes, false);
642 offset += bytes;
643 }
644
645 mirror_wait_for_all_io(s);
646 s->initial_zeroing_ongoing = false;
647 }
648
649
650 for (offset = 0; offset < s->bdev_length; ) {
651
652 int bytes = MIN(s->bdev_length - offset,
653 QEMU_ALIGN_DOWN(INT_MAX, s->granularity));
654
655 mirror_throttle(s);
656
657 if (block_job_is_cancelled(&s->common)) {
658 return 0;
659 }
660
661 ret = bdrv_is_allocated_above(bs, base, offset, bytes, &count);
662 if (ret < 0) {
663 return ret;
664 }
665
666 assert(count);
667 if (ret == 1) {
668 bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count);
669 }
670 offset += count;
671 }
672 return 0;
673}
674
675
676
677
678static int mirror_flush(MirrorBlockJob *s)
679{
680 int ret = blk_flush(s->target);
681 if (ret < 0) {
682 if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) {
683 s->ret = ret;
684 }
685 }
686 return ret;
687}
688
689static void coroutine_fn mirror_run(void *opaque)
690{
691 MirrorBlockJob *s = opaque;
692 MirrorExitData *data;
693 BlockDriverState *bs = s->source;
694 BlockDriverState *target_bs = blk_bs(s->target);
695 bool need_drain = true;
696 int64_t length;
697 BlockDriverInfo bdi;
698 char backing_filename[2];
699
700 int ret = 0;
701
702 if (block_job_is_cancelled(&s->common)) {
703 goto immediate_exit;
704 }
705
706 s->bdev_length = bdrv_getlength(bs);
707 if (s->bdev_length < 0) {
708 ret = s->bdev_length;
709 goto immediate_exit;
710 }
711
712
713
714 if (s->base == blk_bs(s->target)) {
715 int64_t base_length;
716
717 base_length = blk_getlength(s->target);
718 if (base_length < 0) {
719 ret = base_length;
720 goto immediate_exit;
721 }
722
723 if (s->bdev_length > base_length) {
724 ret = blk_truncate(s->target, s->bdev_length, PREALLOC_MODE_OFF,
725 NULL);
726 if (ret < 0) {
727 goto immediate_exit;
728 }
729 }
730 }
731
732 if (s->bdev_length == 0) {
733
734 block_job_event_ready(&s->common);
735 s->synced = true;
736 while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
737 block_job_yield(&s->common);
738 }
739 s->common.cancelled = false;
740 goto immediate_exit;
741 }
742
743 length = DIV_ROUND_UP(s->bdev_length, s->granularity);
744 s->in_flight_bitmap = bitmap_new(length);
745
746
747
748
749
750 bdrv_get_backing_filename(target_bs, backing_filename,
751 sizeof(backing_filename));
752 if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) {
753 s->target_cluster_size = bdi.cluster_size;
754 } else {
755 s->target_cluster_size = BDRV_SECTOR_SIZE;
756 }
757 if (backing_filename[0] && !target_bs->backing &&
758 s->granularity < s->target_cluster_size) {
759 s->buf_size = MAX(s->buf_size, s->target_cluster_size);
760 s->cow_bitmap = bitmap_new(length);
761 }
762 s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov);
763
764 s->buf = qemu_try_blockalign(bs, s->buf_size);
765 if (s->buf == NULL) {
766 ret = -ENOMEM;
767 goto immediate_exit;
768 }
769
770 mirror_free_init(s);
771
772 s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
773 if (!s->is_none_mode) {
774 ret = mirror_dirty_init(s);
775 if (ret < 0 || block_job_is_cancelled(&s->common)) {
776 goto immediate_exit;
777 }
778 }
779
780 assert(!s->dbi);
781 s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap);
782 for (;;) {
783 uint64_t delay_ns = 0;
784 int64_t cnt, delta;
785 bool should_complete;
786
787 if (s->ret < 0) {
788 ret = s->ret;
789 goto immediate_exit;
790 }
791
792 block_job_pause_point(&s->common);
793
794 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
795
796
797
798
799 s->common.len = s->common.offset + s->bytes_in_flight + cnt;
800
801
802
803
804
805
806 delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns;
807 if (delta < SLICE_TIME &&
808 s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
809 if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 ||
810 (cnt == 0 && s->in_flight > 0)) {
811 trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight);
812 mirror_wait_for_io(s);
813 continue;
814 } else if (cnt != 0) {
815 delay_ns = mirror_iteration(s);
816 }
817 }
818
819 should_complete = false;
820 if (s->in_flight == 0 && cnt == 0) {
821 trace_mirror_before_flush(s);
822 if (!s->synced) {
823 if (mirror_flush(s) < 0) {
824
825 continue;
826 }
827
828
829
830
831
832 block_job_event_ready(&s->common);
833 s->synced = true;
834 }
835
836 should_complete = s->should_complete ||
837 block_job_is_cancelled(&s->common);
838 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
839 }
840
841 if (cnt == 0 && should_complete) {
842
843
844
845
846
847
848
849
850
851
852 trace_mirror_before_drain(s, cnt);
853
854 bdrv_drained_begin(bs);
855 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
856 if (cnt > 0 || mirror_flush(s) < 0) {
857 bdrv_drained_end(bs);
858 continue;
859 }
860
861
862
863
864 assert(QLIST_EMPTY(&bs->tracked_requests));
865 s->common.cancelled = false;
866 need_drain = false;
867 break;
868 }
869
870 ret = 0;
871 trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
872 if (!s->synced) {
873 block_job_sleep_ns(&s->common, delay_ns);
874 if (block_job_is_cancelled(&s->common)) {
875 break;
876 }
877 } else if (!should_complete) {
878 delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
879 block_job_sleep_ns(&s->common, delay_ns);
880 }
881 s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
882 }
883
884immediate_exit:
885 if (s->in_flight > 0) {
886
887
888
889
890 assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
891 assert(need_drain);
892 mirror_wait_for_all_io(s);
893 }
894
895 assert(s->in_flight == 0);
896 qemu_vfree(s->buf);
897 g_free(s->cow_bitmap);
898 g_free(s->in_flight_bitmap);
899 bdrv_dirty_iter_free(s->dbi);
900
901 data = g_malloc(sizeof(*data));
902 data->ret = ret;
903
904 if (need_drain) {
905 bdrv_drained_begin(bs);
906 }
907 block_job_defer_to_main_loop(&s->common, mirror_exit, data);
908}
909
910static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
911{
912 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
913
914 if (speed < 0) {
915 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
916 return;
917 }
918 ratelimit_set_speed(&s->limit, speed, SLICE_TIME);
919}
920
921static void mirror_complete(BlockJob *job, Error **errp)
922{
923 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
924 BlockDriverState *target;
925
926 target = blk_bs(s->target);
927
928 if (!s->synced) {
929 error_setg(errp, "The active block job '%s' cannot be completed",
930 job->id);
931 return;
932 }
933
934 if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) {
935 int ret;
936
937 assert(!target->backing);
938 ret = bdrv_open_backing_file(target, NULL, "backing", errp);
939 if (ret < 0) {
940 return;
941 }
942 }
943
944
945 if (s->replaces) {
946 AioContext *replace_aio_context;
947
948 s->to_replace = bdrv_find_node(s->replaces);
949 if (!s->to_replace) {
950 error_setg(errp, "Node name '%s' not found", s->replaces);
951 return;
952 }
953
954 replace_aio_context = bdrv_get_aio_context(s->to_replace);
955 aio_context_acquire(replace_aio_context);
956
957
958
959
960
961 error_setg(&s->replace_blocker,
962 "block device is in use by block-job-complete");
963 bdrv_op_block_all(s->to_replace, s->replace_blocker);
964 bdrv_ref(s->to_replace);
965
966 aio_context_release(replace_aio_context);
967 }
968
969 s->should_complete = true;
970 block_job_enter(&s->common);
971}
972
973static void mirror_pause(BlockJob *job)
974{
975 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
976
977 mirror_wait_for_all_io(s);
978}
979
980static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context)
981{
982 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
983
984 blk_set_aio_context(s->target, new_context);
985}
986
987static void mirror_drain(BlockJob *job)
988{
989 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
990
991
992
993
994 if (s->target) {
995 BlockBackend *target = s->target;
996 blk_ref(target);
997 blk_drain(target);
998 blk_unref(target);
999 }
1000}
1001
1002static const BlockJobDriver mirror_job_driver = {
1003 .instance_size = sizeof(MirrorBlockJob),
1004 .job_type = BLOCK_JOB_TYPE_MIRROR,
1005 .set_speed = mirror_set_speed,
1006 .start = mirror_run,
1007 .complete = mirror_complete,
1008 .pause = mirror_pause,
1009 .attached_aio_context = mirror_attached_aio_context,
1010 .drain = mirror_drain,
1011};
1012
1013static const BlockJobDriver commit_active_job_driver = {
1014 .instance_size = sizeof(MirrorBlockJob),
1015 .job_type = BLOCK_JOB_TYPE_COMMIT,
1016 .set_speed = mirror_set_speed,
1017 .start = mirror_run,
1018 .complete = mirror_complete,
1019 .pause = mirror_pause,
1020 .attached_aio_context = mirror_attached_aio_context,
1021 .drain = mirror_drain,
1022};
1023
1024static int coroutine_fn bdrv_mirror_top_preadv(BlockDriverState *bs,
1025 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
1026{
1027 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
1028}
1029
1030static int coroutine_fn bdrv_mirror_top_pwritev(BlockDriverState *bs,
1031 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
1032{
1033 return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
1034}
1035
1036static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs)
1037{
1038 if (bs->backing == NULL) {
1039
1040 return 0;
1041 }
1042 return bdrv_co_flush(bs->backing->bs);
1043}
1044
1045static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs,
1046 int64_t offset, int bytes, BdrvRequestFlags flags)
1047{
1048 return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
1049}
1050
1051static int coroutine_fn bdrv_mirror_top_pdiscard(BlockDriverState *bs,
1052 int64_t offset, int bytes)
1053{
1054 return bdrv_co_pdiscard(bs->backing->bs, offset, bytes);
1055}
1056
1057static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs, QDict *opts)
1058{
1059 if (bs->backing == NULL) {
1060
1061
1062 return;
1063 }
1064 bdrv_refresh_filename(bs->backing->bs);
1065 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
1066 bs->backing->bs->filename);
1067}
1068
1069static void bdrv_mirror_top_close(BlockDriverState *bs)
1070{
1071}
1072
1073static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c,
1074 const BdrvChildRole *role,
1075 BlockReopenQueue *reopen_queue,
1076 uint64_t perm, uint64_t shared,
1077 uint64_t *nperm, uint64_t *nshared)
1078{
1079
1080 *nperm = 0;
1081 if (perm & BLK_PERM_WRITE) {
1082 *nperm |= BLK_PERM_WRITE;
1083 }
1084
1085 *nshared = BLK_PERM_ALL;
1086}
1087
1088
1089
1090static BlockDriver bdrv_mirror_top = {
1091 .format_name = "mirror_top",
1092 .bdrv_co_preadv = bdrv_mirror_top_preadv,
1093 .bdrv_co_pwritev = bdrv_mirror_top_pwritev,
1094 .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes,
1095 .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard,
1096 .bdrv_co_flush = bdrv_mirror_top_flush,
1097 .bdrv_co_get_block_status = bdrv_co_get_block_status_from_backing,
1098 .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename,
1099 .bdrv_close = bdrv_mirror_top_close,
1100 .bdrv_child_perm = bdrv_mirror_top_child_perm,
1101};
1102
1103static void mirror_start_job(const char *job_id, BlockDriverState *bs,
1104 int creation_flags, BlockDriverState *target,
1105 const char *replaces, int64_t speed,
1106 uint32_t granularity, int64_t buf_size,
1107 BlockMirrorBackingMode backing_mode,
1108 BlockdevOnError on_source_error,
1109 BlockdevOnError on_target_error,
1110 bool unmap,
1111 BlockCompletionFunc *cb,
1112 void *opaque,
1113 const BlockJobDriver *driver,
1114 bool is_none_mode, BlockDriverState *base,
1115 bool auto_complete, const char *filter_node_name,
1116 bool is_mirror,
1117 Error **errp)
1118{
1119 MirrorBlockJob *s;
1120 BlockDriverState *mirror_top_bs;
1121 bool target_graph_mod;
1122 bool target_is_backing;
1123 Error *local_err = NULL;
1124 int ret;
1125
1126 if (granularity == 0) {
1127 granularity = bdrv_get_default_bitmap_granularity(target);
1128 }
1129
1130 assert(is_power_of_2(granularity));
1131
1132 if (buf_size < 0) {
1133 error_setg(errp, "Invalid parameter 'buf-size'");
1134 return;
1135 }
1136
1137 if (buf_size == 0) {
1138 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1139 }
1140
1141
1142
1143
1144 mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name,
1145 BDRV_O_RDWR, errp);
1146 if (mirror_top_bs == NULL) {
1147 return;
1148 }
1149 if (!filter_node_name) {
1150 mirror_top_bs->implicit = true;
1151 }
1152 mirror_top_bs->total_sectors = bs->total_sectors;
1153 bdrv_set_aio_context(mirror_top_bs, bdrv_get_aio_context(bs));
1154
1155
1156
1157 bdrv_ref(mirror_top_bs);
1158 bdrv_drained_begin(bs);
1159 bdrv_append(mirror_top_bs, bs, &local_err);
1160 bdrv_drained_end(bs);
1161
1162 if (local_err) {
1163 bdrv_unref(mirror_top_bs);
1164 error_propagate(errp, local_err);
1165 return;
1166 }
1167
1168
1169 s = block_job_create(job_id, driver, mirror_top_bs,
1170 BLK_PERM_CONSISTENT_READ,
1171 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1172 BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed,
1173 creation_flags, cb, opaque, errp);
1174 if (!s) {
1175 goto fail;
1176 }
1177
1178 bdrv_unref(mirror_top_bs);
1179
1180 s->source = bs;
1181 s->mirror_top_bs = mirror_top_bs;
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191 target_is_backing = bdrv_chain_contains(bs, target);
1192 target_graph_mod = (backing_mode != MIRROR_LEAVE_BACKING_CHAIN);
1193 s->target = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE |
1194 (target_graph_mod ? BLK_PERM_GRAPH_MOD : 0),
1195 BLK_PERM_WRITE_UNCHANGED |
1196 (target_is_backing ? BLK_PERM_CONSISTENT_READ |
1197 BLK_PERM_WRITE |
1198 BLK_PERM_GRAPH_MOD : 0));
1199 ret = blk_insert_bs(s->target, target, errp);
1200 if (ret < 0) {
1201 goto fail;
1202 }
1203 if (is_mirror) {
1204
1205
1206
1207
1208
1209
1210 blk_set_force_allow_inactivate(s->target);
1211 }
1212
1213 s->replaces = g_strdup(replaces);
1214 s->on_source_error = on_source_error;
1215 s->on_target_error = on_target_error;
1216 s->is_none_mode = is_none_mode;
1217 s->backing_mode = backing_mode;
1218 s->base = base;
1219 s->granularity = granularity;
1220 s->buf_size = ROUND_UP(buf_size, granularity);
1221 s->unmap = unmap;
1222 if (auto_complete) {
1223 s->should_complete = true;
1224 }
1225
1226 s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
1227 if (!s->dirty_bitmap) {
1228 goto fail;
1229 }
1230
1231
1232 block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL,
1233 &error_abort);
1234
1235
1236
1237 if (target_is_backing) {
1238 BlockDriverState *iter;
1239 for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) {
1240
1241
1242
1243
1244 ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
1245 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,
1246 errp);
1247 if (ret < 0) {
1248 goto fail;
1249 }
1250 }
1251 }
1252
1253 trace_mirror_start(bs, s, opaque);
1254 block_job_start(&s->common);
1255 return;
1256
1257fail:
1258 if (s) {
1259
1260
1261 bdrv_ref(mirror_top_bs);
1262
1263 g_free(s->replaces);
1264 blk_unref(s->target);
1265 block_job_early_fail(&s->common);
1266 }
1267
1268 bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
1269 &error_abort);
1270 bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort);
1271
1272 bdrv_unref(mirror_top_bs);
1273}
1274
1275void mirror_start(const char *job_id, BlockDriverState *bs,
1276 BlockDriverState *target, const char *replaces,
1277 int64_t speed, uint32_t granularity, int64_t buf_size,
1278 MirrorSyncMode mode, BlockMirrorBackingMode backing_mode,
1279 BlockdevOnError on_source_error,
1280 BlockdevOnError on_target_error,
1281 bool unmap, const char *filter_node_name, Error **errp)
1282{
1283 bool is_none_mode;
1284 BlockDriverState *base;
1285
1286 if (mode == MIRROR_SYNC_MODE_INCREMENTAL) {
1287 error_setg(errp, "Sync mode 'incremental' not supported");
1288 return;
1289 }
1290 is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
1291 base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL;
1292 mirror_start_job(job_id, bs, BLOCK_JOB_DEFAULT, target, replaces,
1293 speed, granularity, buf_size, backing_mode,
1294 on_source_error, on_target_error, unmap, NULL, NULL,
1295 &mirror_job_driver, is_none_mode, base, false,
1296 filter_node_name, true, errp);
1297}
1298
1299void commit_active_start(const char *job_id, BlockDriverState *bs,
1300 BlockDriverState *base, int creation_flags,
1301 int64_t speed, BlockdevOnError on_error,
1302 const char *filter_node_name,
1303 BlockCompletionFunc *cb, void *opaque,
1304 bool auto_complete, Error **errp)
1305{
1306 int orig_base_flags;
1307 Error *local_err = NULL;
1308
1309 orig_base_flags = bdrv_get_flags(base);
1310
1311 if (bdrv_reopen(base, bs->open_flags, errp)) {
1312 return;
1313 }
1314
1315 mirror_start_job(job_id, bs, creation_flags, base, NULL, speed, 0, 0,
1316 MIRROR_LEAVE_BACKING_CHAIN,
1317 on_error, on_error, true, cb, opaque,
1318 &commit_active_job_driver, false, base, auto_complete,
1319 filter_node_name, false, &local_err);
1320 if (local_err) {
1321 error_propagate(errp, local_err);
1322 goto error_restore_flags;
1323 }
1324
1325 return;
1326
1327error_restore_flags:
1328
1329
1330 bdrv_reopen(base, orig_base_flags, NULL);
1331 return;
1332}
1333