]> Git Repo - qemu.git/blob - block/mirror.c
block: Really pause block jobs on drain
[qemu.git] / block / mirror.c
1 /*
2  * Image mirroring
3  *
4  * Copyright Red Hat, Inc. 2012
5  *
6  * Authors:
7  *  Paolo Bonzini  <[email protected]>
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 "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 MAX_IN_FLIGHT 16
26 #define MAX_IO_BYTES (1 << 20) /* 1 Mb */
27 #define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES)
28
29 /* The mirroring buffer is a list of granularity-sized chunks.
30  * Free chunks are organized in a list.
31  */
32 typedef struct MirrorBuffer {
33     QSIMPLEQ_ENTRY(MirrorBuffer) next;
34 } MirrorBuffer;
35
36 typedef struct MirrorBlockJob {
37     BlockJob common;
38     BlockBackend *target;
39     BlockDriverState *mirror_top_bs;
40     BlockDriverState *source;
41     BlockDriverState *base;
42
43     /* The name of the graph node to replace */
44     char *replaces;
45     /* The BDS to replace */
46     BlockDriverState *to_replace;
47     /* Used to block operations on the drive-mirror-replace target */
48     Error *replace_blocker;
49     bool is_none_mode;
50     BlockMirrorBackingMode backing_mode;
51     BlockdevOnError on_source_error, on_target_error;
52     bool synced;
53     bool should_complete;
54     int64_t granularity;
55     size_t buf_size;
56     int64_t bdev_length;
57     unsigned long *cow_bitmap;
58     BdrvDirtyBitmap *dirty_bitmap;
59     BdrvDirtyBitmapIter *dbi;
60     uint8_t *buf;
61     QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
62     int buf_free_count;
63
64     uint64_t last_pause_ns;
65     unsigned long *in_flight_bitmap;
66     int in_flight;
67     int64_t bytes_in_flight;
68     int ret;
69     bool unmap;
70     bool waiting_for_io;
71     int target_cluster_size;
72     int max_iov;
73     bool initial_zeroing_ongoing;
74 } MirrorBlockJob;
75
76 typedef struct MirrorOp {
77     MirrorBlockJob *s;
78     QEMUIOVector qiov;
79     int64_t offset;
80     uint64_t bytes;
81 } MirrorOp;
82
83 static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
84                                             int error)
85 {
86     s->synced = false;
87     if (read) {
88         return block_job_error_action(&s->common, s->on_source_error,
89                                       true, error);
90     } else {
91         return block_job_error_action(&s->common, s->on_target_error,
92                                       false, error);
93     }
94 }
95
96 static void mirror_iteration_done(MirrorOp *op, int ret)
97 {
98     MirrorBlockJob *s = op->s;
99     struct iovec *iov;
100     int64_t chunk_num;
101     int i, nb_chunks;
102
103     trace_mirror_iteration_done(s, op->offset, op->bytes, ret);
104
105     s->in_flight--;
106     s->bytes_in_flight -= op->bytes;
107     iov = op->qiov.iov;
108     for (i = 0; i < op->qiov.niov; i++) {
109         MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
110         QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
111         s->buf_free_count++;
112     }
113
114     chunk_num = op->offset / s->granularity;
115     nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity);
116     bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
117     if (ret >= 0) {
118         if (s->cow_bitmap) {
119             bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
120         }
121         if (!s->initial_zeroing_ongoing) {
122             job_progress_update(&s->common.job, op->bytes);
123         }
124     }
125     qemu_iovec_destroy(&op->qiov);
126     g_free(op);
127
128     if (s->waiting_for_io) {
129         qemu_coroutine_enter(s->common.job.co);
130     }
131 }
132
133 static void mirror_write_complete(void *opaque, int ret)
134 {
135     MirrorOp *op = opaque;
136     MirrorBlockJob *s = op->s;
137
138     aio_context_acquire(blk_get_aio_context(s->common.blk));
139     if (ret < 0) {
140         BlockErrorAction action;
141
142         bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes);
143         action = mirror_error_action(s, false, -ret);
144         if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
145             s->ret = ret;
146         }
147     }
148     mirror_iteration_done(op, ret);
149     aio_context_release(blk_get_aio_context(s->common.blk));
150 }
151
152 static void mirror_read_complete(void *opaque, int ret)
153 {
154     MirrorOp *op = opaque;
155     MirrorBlockJob *s = op->s;
156
157     aio_context_acquire(blk_get_aio_context(s->common.blk));
158     if (ret < 0) {
159         BlockErrorAction action;
160
161         bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes);
162         action = mirror_error_action(s, true, -ret);
163         if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
164             s->ret = ret;
165         }
166
167         mirror_iteration_done(op, ret);
168     } else {
169         blk_aio_pwritev(s->target, op->offset, &op->qiov,
170                         0, mirror_write_complete, op);
171     }
172     aio_context_release(blk_get_aio_context(s->common.blk));
173 }
174
175 /* Clip bytes relative to offset to not exceed end-of-file */
176 static inline int64_t mirror_clip_bytes(MirrorBlockJob *s,
177                                         int64_t offset,
178                                         int64_t bytes)
179 {
180     return MIN(bytes, s->bdev_length - offset);
181 }
182
183 /* Round offset and/or bytes to target cluster if COW is needed, and
184  * return the offset of the adjusted tail against original. */
185 static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset,
186                             uint64_t *bytes)
187 {
188     bool need_cow;
189     int ret = 0;
190     int64_t align_offset = *offset;
191     int64_t align_bytes = *bytes;
192     int max_bytes = s->granularity * s->max_iov;
193
194     need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap);
195     need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity,
196                           s->cow_bitmap);
197     if (need_cow) {
198         bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes,
199                                &align_offset, &align_bytes);
200     }
201
202     if (align_bytes > max_bytes) {
203         align_bytes = max_bytes;
204         if (need_cow) {
205             align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size);
206         }
207     }
208     /* Clipping may result in align_bytes unaligned to chunk boundary, but
209      * that doesn't matter because it's already the end of source image. */
210     align_bytes = mirror_clip_bytes(s, align_offset, align_bytes);
211
212     ret = align_offset + align_bytes - (*offset + *bytes);
213     *offset = align_offset;
214     *bytes = align_bytes;
215     assert(ret >= 0);
216     return ret;
217 }
218
219 static inline void mirror_wait_for_io(MirrorBlockJob *s)
220 {
221     assert(!s->waiting_for_io);
222     s->waiting_for_io = true;
223     qemu_coroutine_yield();
224     s->waiting_for_io = false;
225 }
226
227 /* Submit async read while handling COW.
228  * Returns: The number of bytes copied after and including offset,
229  *          excluding any bytes copied prior to offset due to alignment.
230  *          This will be @bytes if no alignment is necessary, or
231  *          (new_end - offset) if tail is rounded up or down due to
232  *          alignment or buffer limit.
233  */
234 static uint64_t mirror_do_read(MirrorBlockJob *s, int64_t offset,
235                                uint64_t bytes)
236 {
237     BlockBackend *source = s->common.blk;
238     int nb_chunks;
239     uint64_t ret;
240     MirrorOp *op;
241     uint64_t max_bytes;
242
243     max_bytes = s->granularity * s->max_iov;
244
245     /* We can only handle as much as buf_size at a time. */
246     bytes = MIN(s->buf_size, MIN(max_bytes, bytes));
247     assert(bytes);
248     assert(bytes < BDRV_REQUEST_MAX_BYTES);
249     ret = bytes;
250
251     if (s->cow_bitmap) {
252         ret += mirror_cow_align(s, &offset, &bytes);
253     }
254     assert(bytes <= s->buf_size);
255     /* The offset is granularity-aligned because:
256      * 1) Caller passes in aligned values;
257      * 2) mirror_cow_align is used only when target cluster is larger. */
258     assert(QEMU_IS_ALIGNED(offset, s->granularity));
259     /* The range is sector-aligned, since bdrv_getlength() rounds up. */
260     assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
261     nb_chunks = DIV_ROUND_UP(bytes, s->granularity);
262
263     while (s->buf_free_count < nb_chunks) {
264         trace_mirror_yield_in_flight(s, offset, s->in_flight);
265         mirror_wait_for_io(s);
266     }
267
268     /* Allocate a MirrorOp that is used as an AIO callback.  */
269     op = g_new(MirrorOp, 1);
270     op->s = s;
271     op->offset = offset;
272     op->bytes = bytes;
273
274     /* Now make a QEMUIOVector taking enough granularity-sized chunks
275      * from s->buf_free.
276      */
277     qemu_iovec_init(&op->qiov, nb_chunks);
278     while (nb_chunks-- > 0) {
279         MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
280         size_t remaining = bytes - op->qiov.size;
281
282         QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
283         s->buf_free_count--;
284         qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
285     }
286
287     /* Copy the dirty cluster.  */
288     s->in_flight++;
289     s->bytes_in_flight += bytes;
290     trace_mirror_one_iteration(s, offset, bytes);
291
292     blk_aio_preadv(source, offset, &op->qiov, 0, mirror_read_complete, op);
293     return ret;
294 }
295
296 static void mirror_do_zero_or_discard(MirrorBlockJob *s,
297                                       int64_t offset,
298                                       uint64_t bytes,
299                                       bool is_discard)
300 {
301     MirrorOp *op;
302
303     /* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed
304      * so the freeing in mirror_iteration_done is nop. */
305     op = g_new0(MirrorOp, 1);
306     op->s = s;
307     op->offset = offset;
308     op->bytes = bytes;
309
310     s->in_flight++;
311     s->bytes_in_flight += bytes;
312     if (is_discard) {
313         blk_aio_pdiscard(s->target, offset,
314                          op->bytes, mirror_write_complete, op);
315     } else {
316         blk_aio_pwrite_zeroes(s->target, offset,
317                               op->bytes, s->unmap ? BDRV_REQ_MAY_UNMAP : 0,
318                               mirror_write_complete, op);
319     }
320 }
321
322 static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
323 {
324     BlockDriverState *source = s->source;
325     int64_t offset, first_chunk;
326     uint64_t delay_ns = 0;
327     /* At least the first dirty chunk is mirrored in one iteration. */
328     int nb_chunks = 1;
329     bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target));
330     int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES);
331
332     bdrv_dirty_bitmap_lock(s->dirty_bitmap);
333     offset = bdrv_dirty_iter_next(s->dbi);
334     if (offset < 0) {
335         bdrv_set_dirty_iter(s->dbi, 0);
336         offset = bdrv_dirty_iter_next(s->dbi);
337         trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
338         assert(offset >= 0);
339     }
340     bdrv_dirty_bitmap_unlock(s->dirty_bitmap);
341
342     first_chunk = offset / s->granularity;
343     while (test_bit(first_chunk, s->in_flight_bitmap)) {
344         trace_mirror_yield_in_flight(s, offset, s->in_flight);
345         mirror_wait_for_io(s);
346     }
347
348     job_pause_point(&s->common.job);
349
350     /* Find the number of consective dirty chunks following the first dirty
351      * one, and wait for in flight requests in them. */
352     bdrv_dirty_bitmap_lock(s->dirty_bitmap);
353     while (nb_chunks * s->granularity < s->buf_size) {
354         int64_t next_dirty;
355         int64_t next_offset = offset + nb_chunks * s->granularity;
356         int64_t next_chunk = next_offset / s->granularity;
357         if (next_offset >= s->bdev_length ||
358             !bdrv_get_dirty_locked(source, s->dirty_bitmap, next_offset)) {
359             break;
360         }
361         if (test_bit(next_chunk, s->in_flight_bitmap)) {
362             break;
363         }
364
365         next_dirty = bdrv_dirty_iter_next(s->dbi);
366         if (next_dirty > next_offset || next_dirty < 0) {
367             /* The bitmap iterator's cache is stale, refresh it */
368             bdrv_set_dirty_iter(s->dbi, next_offset);
369             next_dirty = bdrv_dirty_iter_next(s->dbi);
370         }
371         assert(next_dirty == next_offset);
372         nb_chunks++;
373     }
374
375     /* Clear dirty bits before querying the block status, because
376      * calling bdrv_block_status_above could yield - if some blocks are
377      * marked dirty in this window, we need to know.
378      */
379     bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset,
380                                    nb_chunks * s->granularity);
381     bdrv_dirty_bitmap_unlock(s->dirty_bitmap);
382
383     bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks);
384     while (nb_chunks > 0 && offset < s->bdev_length) {
385         int ret;
386         int64_t io_bytes;
387         int64_t io_bytes_acct;
388         enum MirrorMethod {
389             MIRROR_METHOD_COPY,
390             MIRROR_METHOD_ZERO,
391             MIRROR_METHOD_DISCARD
392         } mirror_method = MIRROR_METHOD_COPY;
393
394         assert(!(offset % s->granularity));
395         ret = bdrv_block_status_above(source, NULL, offset,
396                                       nb_chunks * s->granularity,
397                                       &io_bytes, NULL, NULL);
398         if (ret < 0) {
399             io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes);
400         } else if (ret & BDRV_BLOCK_DATA) {
401             io_bytes = MIN(io_bytes, max_io_bytes);
402         }
403
404         io_bytes -= io_bytes % s->granularity;
405         if (io_bytes < s->granularity) {
406             io_bytes = s->granularity;
407         } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) {
408             int64_t target_offset;
409             int64_t target_bytes;
410             bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes,
411                                    &target_offset, &target_bytes);
412             if (target_offset == offset &&
413                 target_bytes == io_bytes) {
414                 mirror_method = ret & BDRV_BLOCK_ZERO ?
415                                     MIRROR_METHOD_ZERO :
416                                     MIRROR_METHOD_DISCARD;
417             }
418         }
419
420         while (s->in_flight >= MAX_IN_FLIGHT) {
421             trace_mirror_yield_in_flight(s, offset, s->in_flight);
422             mirror_wait_for_io(s);
423         }
424
425         if (s->ret < 0) {
426             return 0;
427         }
428
429         io_bytes = mirror_clip_bytes(s, offset, io_bytes);
430         switch (mirror_method) {
431         case MIRROR_METHOD_COPY:
432             io_bytes = io_bytes_acct = mirror_do_read(s, offset, io_bytes);
433             break;
434         case MIRROR_METHOD_ZERO:
435         case MIRROR_METHOD_DISCARD:
436             mirror_do_zero_or_discard(s, offset, io_bytes,
437                                       mirror_method == MIRROR_METHOD_DISCARD);
438             if (write_zeroes_ok) {
439                 io_bytes_acct = 0;
440             } else {
441                 io_bytes_acct = io_bytes;
442             }
443             break;
444         default:
445             abort();
446         }
447         assert(io_bytes);
448         offset += io_bytes;
449         nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity);
450         delay_ns = block_job_ratelimit_get_delay(&s->common, io_bytes_acct);
451     }
452     return delay_ns;
453 }
454
455 static void mirror_free_init(MirrorBlockJob *s)
456 {
457     int granularity = s->granularity;
458     size_t buf_size = s->buf_size;
459     uint8_t *buf = s->buf;
460
461     assert(s->buf_free_count == 0);
462     QSIMPLEQ_INIT(&s->buf_free);
463     while (buf_size != 0) {
464         MirrorBuffer *cur = (MirrorBuffer *)buf;
465         QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
466         s->buf_free_count++;
467         buf_size -= granularity;
468         buf += granularity;
469     }
470 }
471
472 /* This is also used for the .pause callback. There is no matching
473  * mirror_resume() because mirror_run() will begin iterating again
474  * when the job is resumed.
475  */
476 static void mirror_wait_for_all_io(MirrorBlockJob *s)
477 {
478     while (s->in_flight > 0) {
479         mirror_wait_for_io(s);
480     }
481 }
482
483 typedef struct {
484     int ret;
485 } MirrorExitData;
486
487 static void mirror_exit(Job *job, void *opaque)
488 {
489     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
490     BlockJob *bjob = &s->common;
491     MirrorExitData *data = opaque;
492     AioContext *replace_aio_context = NULL;
493     BlockDriverState *src = s->source;
494     BlockDriverState *target_bs = blk_bs(s->target);
495     BlockDriverState *mirror_top_bs = s->mirror_top_bs;
496     Error *local_err = NULL;
497
498     bdrv_release_dirty_bitmap(src, s->dirty_bitmap);
499
500     /* Make sure that the source BDS doesn't go away before we called
501      * job_completed(). */
502     bdrv_ref(src);
503     bdrv_ref(mirror_top_bs);
504     bdrv_ref(target_bs);
505
506     /* Remove target parent that still uses BLK_PERM_WRITE/RESIZE before
507      * inserting target_bs at s->to_replace, where we might not be able to get
508      * these permissions.
509      *
510      * Note that blk_unref() alone doesn't necessarily drop permissions because
511      * we might be running nested inside mirror_drain(), which takes an extra
512      * reference, so use an explicit blk_set_perm() first. */
513     blk_set_perm(s->target, 0, BLK_PERM_ALL, &error_abort);
514     blk_unref(s->target);
515     s->target = NULL;
516
517     /* We don't access the source any more. Dropping any WRITE/RESIZE is
518      * required before it could become a backing file of target_bs. */
519     bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
520                             &error_abort);
521     if (s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) {
522         BlockDriverState *backing = s->is_none_mode ? src : s->base;
523         if (backing_bs(target_bs) != backing) {
524             bdrv_set_backing_hd(target_bs, backing, &local_err);
525             if (local_err) {
526                 error_report_err(local_err);
527                 data->ret = -EPERM;
528             }
529         }
530     }
531
532     if (s->to_replace) {
533         replace_aio_context = bdrv_get_aio_context(s->to_replace);
534         aio_context_acquire(replace_aio_context);
535     }
536
537     if (s->should_complete && data->ret == 0) {
538         BlockDriverState *to_replace = src;
539         if (s->to_replace) {
540             to_replace = s->to_replace;
541         }
542
543         if (bdrv_get_flags(target_bs) != bdrv_get_flags(to_replace)) {
544             bdrv_reopen(target_bs, bdrv_get_flags(to_replace), NULL);
545         }
546
547         /* The mirror job has no requests in flight any more, but we need to
548          * drain potential other users of the BDS before changing the graph. */
549         bdrv_drained_begin(target_bs);
550         bdrv_replace_node(to_replace, target_bs, &local_err);
551         bdrv_drained_end(target_bs);
552         if (local_err) {
553             error_report_err(local_err);
554             data->ret = -EPERM;
555         }
556     }
557     if (s->to_replace) {
558         bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
559         error_free(s->replace_blocker);
560         bdrv_unref(s->to_replace);
561     }
562     if (replace_aio_context) {
563         aio_context_release(replace_aio_context);
564     }
565     g_free(s->replaces);
566     bdrv_unref(target_bs);
567
568     /* Remove the mirror filter driver from the graph. Before this, get rid of
569      * the blockers on the intermediate nodes so that the resulting state is
570      * valid. Also give up permissions on mirror_top_bs->backing, which might
571      * block the removal. */
572     block_job_remove_all_bdrv(bjob);
573     bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
574                             &error_abort);
575     bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort);
576
577     /* We just changed the BDS the job BB refers to (with either or both of the
578      * bdrv_replace_node() calls), so switch the BB back so the cleanup does
579      * the right thing. We don't need any permissions any more now. */
580     blk_remove_bs(bjob->blk);
581     blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort);
582     blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort);
583
584     job_completed(job, data->ret, NULL);
585
586     g_free(data);
587     bdrv_drained_end(src);
588     bdrv_unref(mirror_top_bs);
589     bdrv_unref(src);
590 }
591
592 static void mirror_throttle(MirrorBlockJob *s)
593 {
594     int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
595
596     if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) {
597         s->last_pause_ns = now;
598         job_sleep_ns(&s->common.job, 0);
599     } else {
600         job_pause_point(&s->common.job);
601     }
602 }
603
604 static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
605 {
606     int64_t offset;
607     BlockDriverState *base = s->base;
608     BlockDriverState *bs = s->source;
609     BlockDriverState *target_bs = blk_bs(s->target);
610     int ret;
611     int64_t count;
612
613     if (base == NULL && !bdrv_has_zero_init(target_bs)) {
614         if (!bdrv_can_write_zeroes_with_unmap(target_bs)) {
615             bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length);
616             return 0;
617         }
618
619         s->initial_zeroing_ongoing = true;
620         for (offset = 0; offset < s->bdev_length; ) {
621             int bytes = MIN(s->bdev_length - offset,
622                             QEMU_ALIGN_DOWN(INT_MAX, s->granularity));
623
624             mirror_throttle(s);
625
626             if (job_is_cancelled(&s->common.job)) {
627                 s->initial_zeroing_ongoing = false;
628                 return 0;
629             }
630
631             if (s->in_flight >= MAX_IN_FLIGHT) {
632                 trace_mirror_yield(s, UINT64_MAX, s->buf_free_count,
633                                    s->in_flight);
634                 mirror_wait_for_io(s);
635                 continue;
636             }
637
638             mirror_do_zero_or_discard(s, offset, bytes, false);
639             offset += bytes;
640         }
641
642         mirror_wait_for_all_io(s);
643         s->initial_zeroing_ongoing = false;
644     }
645
646     /* First part, loop on the sectors and initialize the dirty bitmap.  */
647     for (offset = 0; offset < s->bdev_length; ) {
648         /* Just to make sure we are not exceeding int limit. */
649         int bytes = MIN(s->bdev_length - offset,
650                         QEMU_ALIGN_DOWN(INT_MAX, s->granularity));
651
652         mirror_throttle(s);
653
654         if (job_is_cancelled(&s->common.job)) {
655             return 0;
656         }
657
658         ret = bdrv_is_allocated_above(bs, base, offset, bytes, &count);
659         if (ret < 0) {
660             return ret;
661         }
662
663         assert(count);
664         if (ret == 1) {
665             bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count);
666         }
667         offset += count;
668     }
669     return 0;
670 }
671
672 /* Called when going out of the streaming phase to flush the bulk of the
673  * data to the medium, or just before completing.
674  */
675 static int mirror_flush(MirrorBlockJob *s)
676 {
677     int ret = blk_flush(s->target);
678     if (ret < 0) {
679         if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) {
680             s->ret = ret;
681         }
682     }
683     return ret;
684 }
685
686 static void coroutine_fn mirror_run(void *opaque)
687 {
688     MirrorBlockJob *s = opaque;
689     MirrorExitData *data;
690     BlockDriverState *bs = s->source;
691     BlockDriverState *target_bs = blk_bs(s->target);
692     bool need_drain = true;
693     int64_t length;
694     BlockDriverInfo bdi;
695     char backing_filename[2]; /* we only need 2 characters because we are only
696                                  checking for a NULL string */
697     int ret = 0;
698
699     if (job_is_cancelled(&s->common.job)) {
700         goto immediate_exit;
701     }
702
703     s->bdev_length = bdrv_getlength(bs);
704     if (s->bdev_length < 0) {
705         ret = s->bdev_length;
706         goto immediate_exit;
707     }
708
709     /* Active commit must resize the base image if its size differs from the
710      * active layer. */
711     if (s->base == blk_bs(s->target)) {
712         int64_t base_length;
713
714         base_length = blk_getlength(s->target);
715         if (base_length < 0) {
716             ret = base_length;
717             goto immediate_exit;
718         }
719
720         if (s->bdev_length > base_length) {
721             ret = blk_truncate(s->target, s->bdev_length, PREALLOC_MODE_OFF,
722                                NULL);
723             if (ret < 0) {
724                 goto immediate_exit;
725             }
726         }
727     }
728
729     if (s->bdev_length == 0) {
730         /* Transition to the READY state and wait for complete. */
731         job_transition_to_ready(&s->common.job);
732         s->synced = true;
733         while (!job_is_cancelled(&s->common.job) && !s->should_complete) {
734             job_yield(&s->common.job);
735         }
736         s->common.job.cancelled = false;
737         goto immediate_exit;
738     }
739
740     length = DIV_ROUND_UP(s->bdev_length, s->granularity);
741     s->in_flight_bitmap = bitmap_new(length);
742
743     /* If we have no backing file yet in the destination, we cannot let
744      * the destination do COW.  Instead, we copy sectors around the
745      * dirty data if needed.  We need a bitmap to do that.
746      */
747     bdrv_get_backing_filename(target_bs, backing_filename,
748                               sizeof(backing_filename));
749     if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) {
750         s->target_cluster_size = bdi.cluster_size;
751     } else {
752         s->target_cluster_size = BDRV_SECTOR_SIZE;
753     }
754     if (backing_filename[0] && !target_bs->backing &&
755         s->granularity < s->target_cluster_size) {
756         s->buf_size = MAX(s->buf_size, s->target_cluster_size);
757         s->cow_bitmap = bitmap_new(length);
758     }
759     s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov);
760
761     s->buf = qemu_try_blockalign(bs, s->buf_size);
762     if (s->buf == NULL) {
763         ret = -ENOMEM;
764         goto immediate_exit;
765     }
766
767     mirror_free_init(s);
768
769     s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
770     if (!s->is_none_mode) {
771         ret = mirror_dirty_init(s);
772         if (ret < 0 || job_is_cancelled(&s->common.job)) {
773             goto immediate_exit;
774         }
775     }
776
777     assert(!s->dbi);
778     s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap);
779     for (;;) {
780         uint64_t delay_ns = 0;
781         int64_t cnt, delta;
782         bool should_complete;
783
784         if (s->ret < 0) {
785             ret = s->ret;
786             goto immediate_exit;
787         }
788
789         job_pause_point(&s->common.job);
790
791         cnt = bdrv_get_dirty_count(s->dirty_bitmap);
792         /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is
793          * the number of bytes currently being processed; together those are
794          * the current remaining operation length */
795         job_progress_set_remaining(&s->common.job, s->bytes_in_flight + cnt);
796
797         /* Note that even when no rate limit is applied we need to yield
798          * periodically with no pending I/O so that bdrv_drain_all() returns.
799          * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is
800          * an error, or when the source is clean, whichever comes first. */
801         delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns;
802         if (delta < BLOCK_JOB_SLICE_TIME &&
803             s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
804             if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 ||
805                 (cnt == 0 && s->in_flight > 0)) {
806                 trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight);
807                 mirror_wait_for_io(s);
808                 continue;
809             } else if (cnt != 0) {
810                 delay_ns = mirror_iteration(s);
811             }
812         }
813
814         should_complete = false;
815         if (s->in_flight == 0 && cnt == 0) {
816             trace_mirror_before_flush(s);
817             if (!s->synced) {
818                 if (mirror_flush(s) < 0) {
819                     /* Go check s->ret.  */
820                     continue;
821                 }
822                 /* We're out of the streaming phase.  From now on, if the job
823                  * is cancelled we will actually complete all pending I/O and
824                  * report completion.  This way, block-job-cancel will leave
825                  * the target in a consistent state.
826                  */
827                 job_transition_to_ready(&s->common.job);
828                 s->synced = true;
829             }
830
831             should_complete = s->should_complete ||
832                 job_is_cancelled(&s->common.job);
833             cnt = bdrv_get_dirty_count(s->dirty_bitmap);
834         }
835
836         if (cnt == 0 && should_complete) {
837             /* The dirty bitmap is not updated while operations are pending.
838              * If we're about to exit, wait for pending operations before
839              * calling bdrv_get_dirty_count(bs), or we may exit while the
840              * source has dirty data to copy!
841              *
842              * Note that I/O can be submitted by the guest while
843              * mirror_populate runs, so pause it now.  Before deciding
844              * whether to switch to target check one last time if I/O has
845              * come in the meanwhile, and if not flush the data to disk.
846              */
847             trace_mirror_before_drain(s, cnt);
848
849             bdrv_drained_begin(bs);
850             cnt = bdrv_get_dirty_count(s->dirty_bitmap);
851             if (cnt > 0 || mirror_flush(s) < 0) {
852                 bdrv_drained_end(bs);
853                 continue;
854             }
855
856             /* The two disks are in sync.  Exit and report successful
857              * completion.
858              */
859             assert(QLIST_EMPTY(&bs->tracked_requests));
860             s->common.job.cancelled = false;
861             need_drain = false;
862             break;
863         }
864
865         ret = 0;
866
867         if (s->synced && !should_complete) {
868             delay_ns = (s->in_flight == 0 &&
869                         cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0);
870         }
871         trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
872         job_sleep_ns(&s->common.job, delay_ns);
873         if (job_is_cancelled(&s->common.job) &&
874             (!s->synced || s->common.job.force_cancel))
875         {
876             break;
877         }
878         s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
879     }
880
881 immediate_exit:
882     if (s->in_flight > 0) {
883         /* We get here only if something went wrong.  Either the job failed,
884          * or it was cancelled prematurely so that we do not guarantee that
885          * the target is a copy of the source.
886          */
887         assert(ret < 0 || ((s->common.job.force_cancel || !s->synced) &&
888                job_is_cancelled(&s->common.job)));
889         assert(need_drain);
890         mirror_wait_for_all_io(s);
891     }
892
893     assert(s->in_flight == 0);
894     qemu_vfree(s->buf);
895     g_free(s->cow_bitmap);
896     g_free(s->in_flight_bitmap);
897     bdrv_dirty_iter_free(s->dbi);
898
899     data = g_malloc(sizeof(*data));
900     data->ret = ret;
901
902     if (need_drain) {
903         bdrv_drained_begin(bs);
904     }
905     job_defer_to_main_loop(&s->common.job, mirror_exit, data);
906 }
907
908 static void mirror_complete(Job *job, Error **errp)
909 {
910     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
911     BlockDriverState *target;
912
913     target = blk_bs(s->target);
914
915     if (!s->synced) {
916         error_setg(errp, "The active block job '%s' cannot be completed",
917                    job->id);
918         return;
919     }
920
921     if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) {
922         int ret;
923
924         assert(!target->backing);
925         ret = bdrv_open_backing_file(target, NULL, "backing", errp);
926         if (ret < 0) {
927             return;
928         }
929     }
930
931     /* block all operations on to_replace bs */
932     if (s->replaces) {
933         AioContext *replace_aio_context;
934
935         s->to_replace = bdrv_find_node(s->replaces);
936         if (!s->to_replace) {
937             error_setg(errp, "Node name '%s' not found", s->replaces);
938             return;
939         }
940
941         replace_aio_context = bdrv_get_aio_context(s->to_replace);
942         aio_context_acquire(replace_aio_context);
943
944         /* TODO Translate this into permission system. Current definition of
945          * GRAPH_MOD would require to request it for the parents; they might
946          * not even be BlockDriverStates, however, so a BdrvChild can't address
947          * them. May need redefinition of GRAPH_MOD. */
948         error_setg(&s->replace_blocker,
949                    "block device is in use by block-job-complete");
950         bdrv_op_block_all(s->to_replace, s->replace_blocker);
951         bdrv_ref(s->to_replace);
952
953         aio_context_release(replace_aio_context);
954     }
955
956     s->should_complete = true;
957     job_enter(job);
958 }
959
960 static void mirror_pause(Job *job)
961 {
962     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
963
964     mirror_wait_for_all_io(s);
965 }
966
967 static bool mirror_drained_poll(BlockJob *job)
968 {
969     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
970     return !!s->in_flight;
971 }
972
973 static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context)
974 {
975     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
976
977     blk_set_aio_context(s->target, new_context);
978 }
979
980 static void mirror_drain(BlockJob *job)
981 {
982     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
983
984     /* Need to keep a reference in case blk_drain triggers execution
985      * of mirror_complete...
986      */
987     if (s->target) {
988         BlockBackend *target = s->target;
989         blk_ref(target);
990         blk_drain(target);
991         blk_unref(target);
992     }
993 }
994
995 static const BlockJobDriver mirror_job_driver = {
996     .job_driver = {
997         .instance_size          = sizeof(MirrorBlockJob),
998         .job_type               = JOB_TYPE_MIRROR,
999         .free                   = block_job_free,
1000         .user_resume            = block_job_user_resume,
1001         .drain                  = block_job_drain,
1002         .start                  = mirror_run,
1003         .pause                  = mirror_pause,
1004         .complete               = mirror_complete,
1005     },
1006     .drained_poll           = mirror_drained_poll,
1007     .attached_aio_context   = mirror_attached_aio_context,
1008     .drain                  = mirror_drain,
1009 };
1010
1011 static const BlockJobDriver commit_active_job_driver = {
1012     .job_driver = {
1013         .instance_size          = sizeof(MirrorBlockJob),
1014         .job_type               = JOB_TYPE_COMMIT,
1015         .free                   = block_job_free,
1016         .user_resume            = block_job_user_resume,
1017         .drain                  = block_job_drain,
1018         .start                  = mirror_run,
1019         .pause                  = mirror_pause,
1020         .complete               = mirror_complete,
1021     },
1022     .drained_poll           = mirror_drained_poll,
1023     .attached_aio_context   = mirror_attached_aio_context,
1024     .drain                  = mirror_drain,
1025 };
1026
1027 static int coroutine_fn bdrv_mirror_top_preadv(BlockDriverState *bs,
1028     uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
1029 {
1030     return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
1031 }
1032
1033 static int coroutine_fn bdrv_mirror_top_pwritev(BlockDriverState *bs,
1034     uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
1035 {
1036     return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
1037 }
1038
1039 static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs)
1040 {
1041     if (bs->backing == NULL) {
1042         /* we can be here after failed bdrv_append in mirror_start_job */
1043         return 0;
1044     }
1045     return bdrv_co_flush(bs->backing->bs);
1046 }
1047
1048 static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs,
1049     int64_t offset, int bytes, BdrvRequestFlags flags)
1050 {
1051     return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
1052 }
1053
1054 static int coroutine_fn bdrv_mirror_top_pdiscard(BlockDriverState *bs,
1055     int64_t offset, int bytes)
1056 {
1057     return bdrv_co_pdiscard(bs->backing->bs, offset, bytes);
1058 }
1059
1060 static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs, QDict *opts)
1061 {
1062     if (bs->backing == NULL) {
1063         /* we can be here after failed bdrv_attach_child in
1064          * bdrv_set_backing_hd */
1065         return;
1066     }
1067     bdrv_refresh_filename(bs->backing->bs);
1068     pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
1069             bs->backing->bs->filename);
1070 }
1071
1072 static void bdrv_mirror_top_close(BlockDriverState *bs)
1073 {
1074 }
1075
1076 static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c,
1077                                        const BdrvChildRole *role,
1078                                        BlockReopenQueue *reopen_queue,
1079                                        uint64_t perm, uint64_t shared,
1080                                        uint64_t *nperm, uint64_t *nshared)
1081 {
1082     /* Must be able to forward guest writes to the real image */
1083     *nperm = 0;
1084     if (perm & BLK_PERM_WRITE) {
1085         *nperm |= BLK_PERM_WRITE;
1086     }
1087
1088     *nshared = BLK_PERM_ALL;
1089 }
1090
1091 /* Dummy node that provides consistent read to its users without requiring it
1092  * from its backing file and that allows writes on the backing file chain. */
1093 static BlockDriver bdrv_mirror_top = {
1094     .format_name                = "mirror_top",
1095     .bdrv_co_preadv             = bdrv_mirror_top_preadv,
1096     .bdrv_co_pwritev            = bdrv_mirror_top_pwritev,
1097     .bdrv_co_pwrite_zeroes      = bdrv_mirror_top_pwrite_zeroes,
1098     .bdrv_co_pdiscard           = bdrv_mirror_top_pdiscard,
1099     .bdrv_co_flush              = bdrv_mirror_top_flush,
1100     .bdrv_co_block_status       = bdrv_co_block_status_from_backing,
1101     .bdrv_refresh_filename      = bdrv_mirror_top_refresh_filename,
1102     .bdrv_close                 = bdrv_mirror_top_close,
1103     .bdrv_child_perm            = bdrv_mirror_top_child_perm,
1104 };
1105
1106 static void mirror_start_job(const char *job_id, BlockDriverState *bs,
1107                              int creation_flags, BlockDriverState *target,
1108                              const char *replaces, int64_t speed,
1109                              uint32_t granularity, int64_t buf_size,
1110                              BlockMirrorBackingMode backing_mode,
1111                              BlockdevOnError on_source_error,
1112                              BlockdevOnError on_target_error,
1113                              bool unmap,
1114                              BlockCompletionFunc *cb,
1115                              void *opaque,
1116                              const BlockJobDriver *driver,
1117                              bool is_none_mode, BlockDriverState *base,
1118                              bool auto_complete, const char *filter_node_name,
1119                              bool is_mirror,
1120                              Error **errp)
1121 {
1122     MirrorBlockJob *s;
1123     BlockDriverState *mirror_top_bs;
1124     bool target_graph_mod;
1125     bool target_is_backing;
1126     Error *local_err = NULL;
1127     int ret;
1128
1129     if (granularity == 0) {
1130         granularity = bdrv_get_default_bitmap_granularity(target);
1131     }
1132
1133     assert(is_power_of_2(granularity));
1134
1135     if (buf_size < 0) {
1136         error_setg(errp, "Invalid parameter 'buf-size'");
1137         return;
1138     }
1139
1140     if (buf_size == 0) {
1141         buf_size = DEFAULT_MIRROR_BUF_SIZE;
1142     }
1143
1144     /* In the case of active commit, add dummy driver to provide consistent
1145      * reads on the top, while disabling it in the intermediate nodes, and make
1146      * the backing chain writable. */
1147     mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name,
1148                                          BDRV_O_RDWR, errp);
1149     if (mirror_top_bs == NULL) {
1150         return;
1151     }
1152     if (!filter_node_name) {
1153         mirror_top_bs->implicit = true;
1154     }
1155     mirror_top_bs->total_sectors = bs->total_sectors;
1156     mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
1157     mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
1158     bdrv_set_aio_context(mirror_top_bs, bdrv_get_aio_context(bs));
1159
1160     /* bdrv_append takes ownership of the mirror_top_bs reference, need to keep
1161      * it alive until block_job_create() succeeds even if bs has no parent. */
1162     bdrv_ref(mirror_top_bs);
1163     bdrv_drained_begin(bs);
1164     bdrv_append(mirror_top_bs, bs, &local_err);
1165     bdrv_drained_end(bs);
1166
1167     if (local_err) {
1168         bdrv_unref(mirror_top_bs);
1169         error_propagate(errp, local_err);
1170         return;
1171     }
1172
1173     /* Make sure that the source is not resized while the job is running */
1174     s = block_job_create(job_id, driver, NULL, mirror_top_bs,
1175                          BLK_PERM_CONSISTENT_READ,
1176                          BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1177                          BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed,
1178                          creation_flags, cb, opaque, errp);
1179     if (!s) {
1180         goto fail;
1181     }
1182     /* The block job now has a reference to this node */
1183     bdrv_unref(mirror_top_bs);
1184
1185     s->source = bs;
1186     s->mirror_top_bs = mirror_top_bs;
1187
1188     /* No resize for the target either; while the mirror is still running, a
1189      * consistent read isn't necessarily possible. We could possibly allow
1190      * writes and graph modifications, though it would likely defeat the
1191      * purpose of a mirror, so leave them blocked for now.
1192      *
1193      * In the case of active commit, things look a bit different, though,
1194      * because the target is an already populated backing file in active use.
1195      * We can allow anything except resize there.*/
1196     target_is_backing = bdrv_chain_contains(bs, target);
1197     target_graph_mod = (backing_mode != MIRROR_LEAVE_BACKING_CHAIN);
1198     s->target = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE |
1199                         (target_graph_mod ? BLK_PERM_GRAPH_MOD : 0),
1200                         BLK_PERM_WRITE_UNCHANGED |
1201                         (target_is_backing ? BLK_PERM_CONSISTENT_READ |
1202                                              BLK_PERM_WRITE |
1203                                              BLK_PERM_GRAPH_MOD : 0));
1204     ret = blk_insert_bs(s->target, target, errp);
1205     if (ret < 0) {
1206         goto fail;
1207     }
1208     if (is_mirror) {
1209         /* XXX: Mirror target could be a NBD server of target QEMU in the case
1210          * of non-shared block migration. To allow migration completion, we
1211          * have to allow "inactivate" of the target BB.  When that happens, we
1212          * know the job is drained, and the vcpus are stopped, so no write
1213          * operation will be performed. Block layer already has assertions to
1214          * ensure that. */
1215         blk_set_force_allow_inactivate(s->target);
1216     }
1217
1218     s->replaces = g_strdup(replaces);
1219     s->on_source_error = on_source_error;
1220     s->on_target_error = on_target_error;
1221     s->is_none_mode = is_none_mode;
1222     s->backing_mode = backing_mode;
1223     s->base = base;
1224     s->granularity = granularity;
1225     s->buf_size = ROUND_UP(buf_size, granularity);
1226     s->unmap = unmap;
1227     if (auto_complete) {
1228         s->should_complete = true;
1229     }
1230
1231     s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
1232     if (!s->dirty_bitmap) {
1233         goto fail;
1234     }
1235
1236     /* Required permissions are already taken with blk_new() */
1237     block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL,
1238                        &error_abort);
1239
1240     /* In commit_active_start() all intermediate nodes disappear, so
1241      * any jobs in them must be blocked */
1242     if (target_is_backing) {
1243         BlockDriverState *iter;
1244         for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) {
1245             /* XXX BLK_PERM_WRITE needs to be allowed so we don't block
1246              * ourselves at s->base (if writes are blocked for a node, they are
1247              * also blocked for its backing file). The other options would be a
1248              * second filter driver above s->base (== target). */
1249             ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
1250                                      BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,
1251                                      errp);
1252             if (ret < 0) {
1253                 goto fail;
1254             }
1255         }
1256     }
1257
1258     trace_mirror_start(bs, s, opaque);
1259     job_start(&s->common.job);
1260     return;
1261
1262 fail:
1263     if (s) {
1264         /* Make sure this BDS does not go away until we have completed the graph
1265          * changes below */
1266         bdrv_ref(mirror_top_bs);
1267
1268         g_free(s->replaces);
1269         blk_unref(s->target);
1270         job_early_fail(&s->common.job);
1271     }
1272
1273     bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
1274                             &error_abort);
1275     bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort);
1276
1277     bdrv_unref(mirror_top_bs);
1278 }
1279
1280 void mirror_start(const char *job_id, BlockDriverState *bs,
1281                   BlockDriverState *target, const char *replaces,
1282                   int64_t speed, uint32_t granularity, int64_t buf_size,
1283                   MirrorSyncMode mode, BlockMirrorBackingMode backing_mode,
1284                   BlockdevOnError on_source_error,
1285                   BlockdevOnError on_target_error,
1286                   bool unmap, const char *filter_node_name, Error **errp)
1287 {
1288     bool is_none_mode;
1289     BlockDriverState *base;
1290
1291     if (mode == MIRROR_SYNC_MODE_INCREMENTAL) {
1292         error_setg(errp, "Sync mode 'incremental' not supported");
1293         return;
1294     }
1295     is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
1296     base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL;
1297     mirror_start_job(job_id, bs, JOB_DEFAULT, target, replaces,
1298                      speed, granularity, buf_size, backing_mode,
1299                      on_source_error, on_target_error, unmap, NULL, NULL,
1300                      &mirror_job_driver, is_none_mode, base, false,
1301                      filter_node_name, true, errp);
1302 }
1303
1304 void commit_active_start(const char *job_id, BlockDriverState *bs,
1305                          BlockDriverState *base, int creation_flags,
1306                          int64_t speed, BlockdevOnError on_error,
1307                          const char *filter_node_name,
1308                          BlockCompletionFunc *cb, void *opaque,
1309                          bool auto_complete, Error **errp)
1310 {
1311     int orig_base_flags;
1312     Error *local_err = NULL;
1313
1314     orig_base_flags = bdrv_get_flags(base);
1315
1316     if (bdrv_reopen(base, bs->open_flags, errp)) {
1317         return;
1318     }
1319
1320     mirror_start_job(job_id, bs, creation_flags, base, NULL, speed, 0, 0,
1321                      MIRROR_LEAVE_BACKING_CHAIN,
1322                      on_error, on_error, true, cb, opaque,
1323                      &commit_active_job_driver, false, base, auto_complete,
1324                      filter_node_name, false, &local_err);
1325     if (local_err) {
1326         error_propagate(errp, local_err);
1327         goto error_restore_flags;
1328     }
1329
1330     return;
1331
1332 error_restore_flags:
1333     /* ignore error and errp for bdrv_reopen, because we want to propagate
1334      * the original error */
1335     bdrv_reopen(base, orig_base_flags, NULL);
1336     return;
1337 }
This page took 0.102264 seconds and 4 git commands to generate.