xref: /qemu/block/mirror.c (revision 533c6e4ee8885cb9e7c6ac36e8e9fa92bea64f97)
1893f7ebaSPaolo Bonzini /*
2893f7ebaSPaolo Bonzini  * Image mirroring
3893f7ebaSPaolo Bonzini  *
4893f7ebaSPaolo Bonzini  * Copyright Red Hat, Inc. 2012
5893f7ebaSPaolo Bonzini  *
6893f7ebaSPaolo Bonzini  * Authors:
7893f7ebaSPaolo Bonzini  *  Paolo Bonzini  <pbonzini@redhat.com>
8893f7ebaSPaolo Bonzini  *
9893f7ebaSPaolo Bonzini  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10893f7ebaSPaolo Bonzini  * See the COPYING.LIB file in the top-level directory.
11893f7ebaSPaolo Bonzini  *
12893f7ebaSPaolo Bonzini  */
13893f7ebaSPaolo Bonzini 
1480c71a24SPeter Maydell #include "qemu/osdep.h"
15fd4a6493SKevin Wolf #include "qemu/cutils.h"
1612aa4082SMax Reitz #include "qemu/coroutine.h"
171181e19aSMax Reitz #include "qemu/range.h"
18893f7ebaSPaolo Bonzini #include "trace.h"
19c87621eaSJohn Snow #include "block/blockjob_int.h"
20737e150eSPaolo Bonzini #include "block/block_int.h"
21e2c1c34fSMarkus Armbruster #include "block/dirty-bitmap.h"
22373340b2SMax Reitz #include "sysemu/block-backend.h"
23da34e65cSMarkus Armbruster #include "qapi/error.h"
24893f7ebaSPaolo Bonzini #include "qemu/ratelimit.h"
25b812f671SPaolo Bonzini #include "qemu/bitmap.h"
265df022cfSPeter Maydell #include "qemu/memalign.h"
27893f7ebaSPaolo Bonzini 
28402a4741SPaolo Bonzini #define MAX_IN_FLIGHT 16
29b436982fSEric Blake #define MAX_IO_BYTES (1 << 20) /* 1 Mb */
30b436982fSEric Blake #define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES)
31402a4741SPaolo Bonzini 
32402a4741SPaolo Bonzini /* The mirroring buffer is a list of granularity-sized chunks.
33402a4741SPaolo Bonzini  * Free chunks are organized in a list.
34402a4741SPaolo Bonzini  */
35402a4741SPaolo Bonzini typedef struct MirrorBuffer {
36402a4741SPaolo Bonzini     QSIMPLEQ_ENTRY(MirrorBuffer) next;
37402a4741SPaolo Bonzini } MirrorBuffer;
38893f7ebaSPaolo Bonzini 
3912aa4082SMax Reitz typedef struct MirrorOp MirrorOp;
4012aa4082SMax Reitz 
41893f7ebaSPaolo Bonzini typedef struct MirrorBlockJob {
42893f7ebaSPaolo Bonzini     BlockJob common;
43e253f4b8SKevin Wolf     BlockBackend *target;
444ef85a9cSKevin Wolf     BlockDriverState *mirror_top_bs;
455bc361b8SFam Zheng     BlockDriverState *base;
463f072a7fSMax Reitz     BlockDriverState *base_overlay;
474ef85a9cSKevin Wolf 
4809158f00SBenoît Canet     /* The name of the graph node to replace */
4909158f00SBenoît Canet     char *replaces;
5009158f00SBenoît Canet     /* The BDS to replace */
5109158f00SBenoît Canet     BlockDriverState *to_replace;
5209158f00SBenoît Canet     /* Used to block operations on the drive-mirror-replace target */
5309158f00SBenoît Canet     Error *replace_blocker;
5403544a6eSFam Zheng     bool is_none_mode;
55274fcceeSMax Reitz     BlockMirrorBackingMode backing_mode;
56cdf3bc93SMax Reitz     /* Whether the target image requires explicit zero-initialization */
57cdf3bc93SMax Reitz     bool zero_target;
58d06107adSMax Reitz     MirrorCopyMode copy_mode;
59b952b558SPaolo Bonzini     BlockdevOnError on_source_error, on_target_error;
60d06107adSMax Reitz     /* Set when the target is synced (dirty bitmap is clean, nothing
61d06107adSMax Reitz      * in flight) and the job is running in active mode */
62d06107adSMax Reitz     bool actively_synced;
63d63ffd87SPaolo Bonzini     bool should_complete;
64eee13dfeSPaolo Bonzini     int64_t granularity;
65b812f671SPaolo Bonzini     size_t buf_size;
66b21c7652SMax Reitz     int64_t bdev_length;
67b812f671SPaolo Bonzini     unsigned long *cow_bitmap;
68e4654d2dSFam Zheng     BdrvDirtyBitmap *dirty_bitmap;
69dc162c8eSFam Zheng     BdrvDirtyBitmapIter *dbi;
70893f7ebaSPaolo Bonzini     uint8_t *buf;
71402a4741SPaolo Bonzini     QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
72402a4741SPaolo Bonzini     int buf_free_count;
73bd48bde8SPaolo Bonzini 
7449efb1f5SDenis V. Lunev     uint64_t last_pause_ns;
75402a4741SPaolo Bonzini     unsigned long *in_flight_bitmap;
761b8f7776SDenis V. Lunev     unsigned in_flight;
77b436982fSEric Blake     int64_t bytes_in_flight;
78b58deb34SPaolo Bonzini     QTAILQ_HEAD(, MirrorOp) ops_in_flight;
79bd48bde8SPaolo Bonzini     int ret;
800fc9f8eaSFam Zheng     bool unmap;
81b436982fSEric Blake     int target_cluster_size;
82e5b43573SFam Zheng     int max_iov;
8390ab48ebSAnton Nefedov     bool initial_zeroing_ongoing;
84d06107adSMax Reitz     int in_active_write_counter;
85d69a879bSHanna Reitz     int64_t active_write_bytes_in_flight;
86737efc1eSJohn Snow     bool prepared;
875e771752SSergio Lopez     bool in_drain;
88893f7ebaSPaolo Bonzini } MirrorBlockJob;
89893f7ebaSPaolo Bonzini 
90429076e8SMax Reitz typedef struct MirrorBDSOpaque {
91429076e8SMax Reitz     MirrorBlockJob *job;
92f94dc3b4SMax Reitz     bool stop;
9353431b90SMax Reitz     bool is_commit;
94429076e8SMax Reitz } MirrorBDSOpaque;
95429076e8SMax Reitz 
9612aa4082SMax Reitz struct MirrorOp {
97bd48bde8SPaolo Bonzini     MirrorBlockJob *s;
98bd48bde8SPaolo Bonzini     QEMUIOVector qiov;
99b436982fSEric Blake     int64_t offset;
100b436982fSEric Blake     uint64_t bytes;
1012e1990b2SMax Reitz 
1022e1990b2SMax Reitz     /* The pointee is set by mirror_co_read(), mirror_co_zero(), and
1032e1990b2SMax Reitz      * mirror_co_discard() before yielding for the first time */
1042e1990b2SMax Reitz     int64_t *bytes_handled;
10512aa4082SMax Reitz 
1061181e19aSMax Reitz     bool is_pseudo_op;
107d06107adSMax Reitz     bool is_active_write;
108ce8cabbdSKevin Wolf     bool is_in_flight;
10912aa4082SMax Reitz     CoQueue waiting_requests;
110eed325b9SKevin Wolf     Coroutine *co;
111d44dae1aSVladimir Sementsov-Ogievskiy     MirrorOp *waiting_for_op;
11212aa4082SMax Reitz 
11312aa4082SMax Reitz     QTAILQ_ENTRY(MirrorOp) next;
11412aa4082SMax Reitz };
115bd48bde8SPaolo Bonzini 
1164295c5fcSMax Reitz typedef enum MirrorMethod {
1174295c5fcSMax Reitz     MIRROR_METHOD_COPY,
1184295c5fcSMax Reitz     MIRROR_METHOD_ZERO,
1194295c5fcSMax Reitz     MIRROR_METHOD_DISCARD,
1204295c5fcSMax Reitz } MirrorMethod;
1214295c5fcSMax Reitz 
122b952b558SPaolo Bonzini static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
123b952b558SPaolo Bonzini                                             int error)
124b952b558SPaolo Bonzini {
125d06107adSMax Reitz     s->actively_synced = false;
126b952b558SPaolo Bonzini     if (read) {
12781e254dcSKevin Wolf         return block_job_error_action(&s->common, s->on_source_error,
12881e254dcSKevin Wolf                                       true, error);
129b952b558SPaolo Bonzini     } else {
13081e254dcSKevin Wolf         return block_job_error_action(&s->common, s->on_target_error,
13181e254dcSKevin Wolf                                       false, error);
132b952b558SPaolo Bonzini     }
133b952b558SPaolo Bonzini }
134b952b558SPaolo Bonzini 
1351181e19aSMax Reitz static void coroutine_fn mirror_wait_on_conflicts(MirrorOp *self,
1361181e19aSMax Reitz                                                   MirrorBlockJob *s,
1371181e19aSMax Reitz                                                   uint64_t offset,
1381181e19aSMax Reitz                                                   uint64_t bytes)
1391181e19aSMax Reitz {
1401181e19aSMax Reitz     uint64_t self_start_chunk = offset / s->granularity;
1411181e19aSMax Reitz     uint64_t self_end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity);
1421181e19aSMax Reitz     uint64_t self_nb_chunks = self_end_chunk - self_start_chunk;
1431181e19aSMax Reitz 
1441181e19aSMax Reitz     while (find_next_bit(s->in_flight_bitmap, self_end_chunk,
1451181e19aSMax Reitz                          self_start_chunk) < self_end_chunk &&
1461181e19aSMax Reitz            s->ret >= 0)
1471181e19aSMax Reitz     {
1481181e19aSMax Reitz         MirrorOp *op;
1491181e19aSMax Reitz 
1501181e19aSMax Reitz         QTAILQ_FOREACH(op, &s->ops_in_flight, next) {
1511181e19aSMax Reitz             uint64_t op_start_chunk = op->offset / s->granularity;
1521181e19aSMax Reitz             uint64_t op_nb_chunks = DIV_ROUND_UP(op->offset + op->bytes,
1531181e19aSMax Reitz                                                  s->granularity) -
1541181e19aSMax Reitz                                     op_start_chunk;
1551181e19aSMax Reitz 
1561181e19aSMax Reitz             if (op == self) {
1571181e19aSMax Reitz                 continue;
1581181e19aSMax Reitz             }
1591181e19aSMax Reitz 
1601181e19aSMax Reitz             if (ranges_overlap(self_start_chunk, self_nb_chunks,
1611181e19aSMax Reitz                                op_start_chunk, op_nb_chunks))
1621181e19aSMax Reitz             {
16366fed30cSStefano Garzarella                 if (self) {
164d44dae1aSVladimir Sementsov-Ogievskiy                     /*
16566fed30cSStefano Garzarella                      * If the operation is already (indirectly) waiting for us,
16666fed30cSStefano Garzarella                      * or will wait for us as soon as it wakes up, then just go
16766fed30cSStefano Garzarella                      * on (instead of producing a deadlock in the former case).
168d44dae1aSVladimir Sementsov-Ogievskiy                      */
169d44dae1aSVladimir Sementsov-Ogievskiy                     if (op->waiting_for_op) {
170d44dae1aSVladimir Sementsov-Ogievskiy                         continue;
171d44dae1aSVladimir Sementsov-Ogievskiy                     }
172d44dae1aSVladimir Sementsov-Ogievskiy 
173d44dae1aSVladimir Sementsov-Ogievskiy                     self->waiting_for_op = op;
17466fed30cSStefano Garzarella                 }
17566fed30cSStefano Garzarella 
1761181e19aSMax Reitz                 qemu_co_queue_wait(&op->waiting_requests, NULL);
17766fed30cSStefano Garzarella 
17866fed30cSStefano Garzarella                 if (self) {
179d44dae1aSVladimir Sementsov-Ogievskiy                     self->waiting_for_op = NULL;
18066fed30cSStefano Garzarella                 }
18166fed30cSStefano Garzarella 
1821181e19aSMax Reitz                 break;
1831181e19aSMax Reitz             }
1841181e19aSMax Reitz         }
1851181e19aSMax Reitz     }
1861181e19aSMax Reitz }
1871181e19aSMax Reitz 
1882e1990b2SMax Reitz static void coroutine_fn mirror_iteration_done(MirrorOp *op, int ret)
189bd48bde8SPaolo Bonzini {
190bd48bde8SPaolo Bonzini     MirrorBlockJob *s = op->s;
191402a4741SPaolo Bonzini     struct iovec *iov;
192bd48bde8SPaolo Bonzini     int64_t chunk_num;
193b436982fSEric Blake     int i, nb_chunks;
194bd48bde8SPaolo Bonzini 
195b436982fSEric Blake     trace_mirror_iteration_done(s, op->offset, op->bytes, ret);
196bd48bde8SPaolo Bonzini 
197bd48bde8SPaolo Bonzini     s->in_flight--;
198b436982fSEric Blake     s->bytes_in_flight -= op->bytes;
199402a4741SPaolo Bonzini     iov = op->qiov.iov;
200402a4741SPaolo Bonzini     for (i = 0; i < op->qiov.niov; i++) {
201402a4741SPaolo Bonzini         MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
202402a4741SPaolo Bonzini         QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
203402a4741SPaolo Bonzini         s->buf_free_count++;
204402a4741SPaolo Bonzini     }
205402a4741SPaolo Bonzini 
206b436982fSEric Blake     chunk_num = op->offset / s->granularity;
207b436982fSEric Blake     nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity);
20812aa4082SMax Reitz 
209402a4741SPaolo Bonzini     bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
21012aa4082SMax Reitz     QTAILQ_REMOVE(&s->ops_in_flight, op, next);
211b21c7652SMax Reitz     if (ret >= 0) {
212b21c7652SMax Reitz         if (s->cow_bitmap) {
213bd48bde8SPaolo Bonzini             bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
214bd48bde8SPaolo Bonzini         }
21590ab48ebSAnton Nefedov         if (!s->initial_zeroing_ongoing) {
21630a5c887SKevin Wolf             job_progress_update(&s->common.job, op->bytes);
217b21c7652SMax Reitz         }
21890ab48ebSAnton Nefedov     }
2196df3bf8eSZhang Min     qemu_iovec_destroy(&op->qiov);
2207b770c72SStefan Hajnoczi 
22112aa4082SMax Reitz     qemu_co_queue_restart_all(&op->waiting_requests);
22212aa4082SMax Reitz     g_free(op);
2237b770c72SStefan Hajnoczi }
224bd48bde8SPaolo Bonzini 
2252e1990b2SMax Reitz static void coroutine_fn mirror_write_complete(MirrorOp *op, int ret)
226bd48bde8SPaolo Bonzini {
227bd48bde8SPaolo Bonzini     MirrorBlockJob *s = op->s;
228b9e413ddSPaolo Bonzini 
229bd48bde8SPaolo Bonzini     if (ret < 0) {
230bd48bde8SPaolo Bonzini         BlockErrorAction action;
231bd48bde8SPaolo Bonzini 
232e0d7f73eSEric Blake         bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes);
233bd48bde8SPaolo Bonzini         action = mirror_error_action(s, false, -ret);
234a589569fSWenchao Xia         if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
235bd48bde8SPaolo Bonzini             s->ret = ret;
236bd48bde8SPaolo Bonzini         }
237bd48bde8SPaolo Bonzini     }
238d12ade57SVladimir Sementsov-Ogievskiy 
239bd48bde8SPaolo Bonzini     mirror_iteration_done(op, ret);
240bd48bde8SPaolo Bonzini }
241bd48bde8SPaolo Bonzini 
2422e1990b2SMax Reitz static void coroutine_fn mirror_read_complete(MirrorOp *op, int ret)
243bd48bde8SPaolo Bonzini {
244bd48bde8SPaolo Bonzini     MirrorBlockJob *s = op->s;
245b9e413ddSPaolo Bonzini 
246bd48bde8SPaolo Bonzini     if (ret < 0) {
247bd48bde8SPaolo Bonzini         BlockErrorAction action;
248bd48bde8SPaolo Bonzini 
249e0d7f73eSEric Blake         bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes);
250bd48bde8SPaolo Bonzini         action = mirror_error_action(s, true, -ret);
251a589569fSWenchao Xia         if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
252bd48bde8SPaolo Bonzini             s->ret = ret;
253bd48bde8SPaolo Bonzini         }
254bd48bde8SPaolo Bonzini 
255bd48bde8SPaolo Bonzini         mirror_iteration_done(op, ret);
256d12ade57SVladimir Sementsov-Ogievskiy         return;
257bd48bde8SPaolo Bonzini     }
258d12ade57SVladimir Sementsov-Ogievskiy 
259d12ade57SVladimir Sementsov-Ogievskiy     ret = blk_co_pwritev(s->target, op->offset, op->qiov.size, &op->qiov, 0);
260d12ade57SVladimir Sementsov-Ogievskiy     mirror_write_complete(op, ret);
261b9e413ddSPaolo Bonzini }
262bd48bde8SPaolo Bonzini 
263782d97efSEric Blake /* Clip bytes relative to offset to not exceed end-of-file */
264782d97efSEric Blake static inline int64_t mirror_clip_bytes(MirrorBlockJob *s,
265782d97efSEric Blake                                         int64_t offset,
266782d97efSEric Blake                                         int64_t bytes)
267782d97efSEric Blake {
268782d97efSEric Blake     return MIN(bytes, s->bdev_length - offset);
269782d97efSEric Blake }
270782d97efSEric Blake 
271782d97efSEric Blake /* Round offset and/or bytes to target cluster if COW is needed, and
272782d97efSEric Blake  * return the offset of the adjusted tail against original. */
27317ac39c3SPaolo Bonzini static int coroutine_fn mirror_cow_align(MirrorBlockJob *s, int64_t *offset,
274ae4cc877SEric Blake                                          uint64_t *bytes)
275893f7ebaSPaolo Bonzini {
276e5b43573SFam Zheng     bool need_cow;
277e5b43573SFam Zheng     int ret = 0;
278782d97efSEric Blake     int64_t align_offset = *offset;
2797cfd5275SEric Blake     int64_t align_bytes = *bytes;
280782d97efSEric Blake     int max_bytes = s->granularity * s->max_iov;
281893f7ebaSPaolo Bonzini 
282782d97efSEric Blake     need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap);
283782d97efSEric Blake     need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity,
284e5b43573SFam Zheng                           s->cow_bitmap);
285e5b43573SFam Zheng     if (need_cow) {
286782d97efSEric Blake         bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes,
287782d97efSEric Blake                                &align_offset, &align_bytes);
2888f0720ecSPaolo Bonzini     }
2898f0720ecSPaolo Bonzini 
290782d97efSEric Blake     if (align_bytes > max_bytes) {
291782d97efSEric Blake         align_bytes = max_bytes;
292e5b43573SFam Zheng         if (need_cow) {
293782d97efSEric Blake             align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size);
294e5b43573SFam Zheng         }
295e5b43573SFam Zheng     }
296782d97efSEric Blake     /* Clipping may result in align_bytes unaligned to chunk boundary, but
2974150ae60SFam Zheng      * that doesn't matter because it's already the end of source image. */
298782d97efSEric Blake     align_bytes = mirror_clip_bytes(s, align_offset, align_bytes);
299402a4741SPaolo Bonzini 
300782d97efSEric Blake     ret = align_offset + align_bytes - (*offset + *bytes);
301782d97efSEric Blake     *offset = align_offset;
302782d97efSEric Blake     *bytes = align_bytes;
303e5b43573SFam Zheng     assert(ret >= 0);
304e5b43573SFam Zheng     return ret;
305e5b43573SFam Zheng }
306e5b43573SFam Zheng 
307537c3d4fSStefan Hajnoczi static inline void coroutine_fn
308eb994912SHanna Reitz mirror_wait_for_free_in_flight_slot(MirrorBlockJob *s)
30921cd917fSFam Zheng {
31012aa4082SMax Reitz     MirrorOp *op;
31112aa4082SMax Reitz 
3121181e19aSMax Reitz     QTAILQ_FOREACH(op, &s->ops_in_flight, next) {
313eb994912SHanna Reitz         /*
314eb994912SHanna Reitz          * Do not wait on pseudo ops, because it may in turn wait on
3151181e19aSMax Reitz          * some other operation to start, which may in fact be the
3161181e19aSMax Reitz          * caller of this function.  Since there is only one pseudo op
3171181e19aSMax Reitz          * at any given time, we will always find some real operation
318eb994912SHanna Reitz          * to wait on.
319eb994912SHanna Reitz          * Also, do not wait on active operations, because they do not
320eb994912SHanna Reitz          * use up in-flight slots.
321eb994912SHanna Reitz          */
322eb994912SHanna Reitz         if (!op->is_pseudo_op && op->is_in_flight && !op->is_active_write) {
32312aa4082SMax Reitz             qemu_co_queue_wait(&op->waiting_requests, NULL);
3241181e19aSMax Reitz             return;
3251181e19aSMax Reitz         }
3261181e19aSMax Reitz     }
3271181e19aSMax Reitz     abort();
32821cd917fSFam Zheng }
32921cd917fSFam Zheng 
3302e1990b2SMax Reitz /* Perform a mirror copy operation.
3312e1990b2SMax Reitz  *
3322e1990b2SMax Reitz  * *op->bytes_handled is set to the number of bytes copied after and
3332e1990b2SMax Reitz  * including offset, excluding any bytes copied prior to offset due
3342e1990b2SMax Reitz  * to alignment.  This will be op->bytes if no alignment is necessary,
3352e1990b2SMax Reitz  * or (new_end - op->offset) if the tail is rounded up or down due to
336e5b43573SFam Zheng  * alignment or buffer limit.
337402a4741SPaolo Bonzini  */
3382e1990b2SMax Reitz static void coroutine_fn mirror_co_read(void *opaque)
339e5b43573SFam Zheng {
3402e1990b2SMax Reitz     MirrorOp *op = opaque;
3412e1990b2SMax Reitz     MirrorBlockJob *s = op->s;
342ae4cc877SEric Blake     int nb_chunks;
343ae4cc877SEric Blake     uint64_t ret;
344ae4cc877SEric Blake     uint64_t max_bytes;
345402a4741SPaolo Bonzini 
346ae4cc877SEric Blake     max_bytes = s->granularity * s->max_iov;
347e5b43573SFam Zheng 
348e5b43573SFam Zheng     /* We can only handle as much as buf_size at a time. */
3492e1990b2SMax Reitz     op->bytes = MIN(s->buf_size, MIN(max_bytes, op->bytes));
3502e1990b2SMax Reitz     assert(op->bytes);
3512e1990b2SMax Reitz     assert(op->bytes < BDRV_REQUEST_MAX_BYTES);
3522e1990b2SMax Reitz     *op->bytes_handled = op->bytes;
353e5b43573SFam Zheng 
354e5b43573SFam Zheng     if (s->cow_bitmap) {
3552e1990b2SMax Reitz         *op->bytes_handled += mirror_cow_align(s, &op->offset, &op->bytes);
356e5b43573SFam Zheng     }
3572e1990b2SMax Reitz     /* Cannot exceed BDRV_REQUEST_MAX_BYTES + INT_MAX */
3582e1990b2SMax Reitz     assert(*op->bytes_handled <= UINT_MAX);
3592e1990b2SMax Reitz     assert(op->bytes <= s->buf_size);
360ae4cc877SEric Blake     /* The offset is granularity-aligned because:
361e5b43573SFam Zheng      * 1) Caller passes in aligned values;
362e5b43573SFam Zheng      * 2) mirror_cow_align is used only when target cluster is larger. */
3632e1990b2SMax Reitz     assert(QEMU_IS_ALIGNED(op->offset, s->granularity));
364ae4cc877SEric Blake     /* The range is sector-aligned, since bdrv_getlength() rounds up. */
3652e1990b2SMax Reitz     assert(QEMU_IS_ALIGNED(op->bytes, BDRV_SECTOR_SIZE));
3662e1990b2SMax Reitz     nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity);
367e5b43573SFam Zheng 
368e5b43573SFam Zheng     while (s->buf_free_count < nb_chunks) {
3692e1990b2SMax Reitz         trace_mirror_yield_in_flight(s, op->offset, s->in_flight);
3709178f4feSKevin Wolf         mirror_wait_for_free_in_flight_slot(s);
371b812f671SPaolo Bonzini     }
372b812f671SPaolo Bonzini 
373402a4741SPaolo Bonzini     /* Now make a QEMUIOVector taking enough granularity-sized chunks
374402a4741SPaolo Bonzini      * from s->buf_free.
375402a4741SPaolo Bonzini      */
376402a4741SPaolo Bonzini     qemu_iovec_init(&op->qiov, nb_chunks);
377402a4741SPaolo Bonzini     while (nb_chunks-- > 0) {
378402a4741SPaolo Bonzini         MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
3792e1990b2SMax Reitz         size_t remaining = op->bytes - op->qiov.size;
3805a0f6fd5SKevin Wolf 
381402a4741SPaolo Bonzini         QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
382402a4741SPaolo Bonzini         s->buf_free_count--;
3835a0f6fd5SKevin Wolf         qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
384402a4741SPaolo Bonzini     }
385402a4741SPaolo Bonzini 
386893f7ebaSPaolo Bonzini     /* Copy the dirty cluster.  */
387bd48bde8SPaolo Bonzini     s->in_flight++;
3882e1990b2SMax Reitz     s->bytes_in_flight += op->bytes;
389ce8cabbdSKevin Wolf     op->is_in_flight = true;
3902e1990b2SMax Reitz     trace_mirror_one_iteration(s, op->offset, op->bytes);
391dcfb3bebSFam Zheng 
392b9b10c35SKevin Wolf     WITH_GRAPH_RDLOCK_GUARD() {
393138f9fffSMax Reitz         ret = bdrv_co_preadv(s->mirror_top_bs->backing, op->offset, op->bytes,
394138f9fffSMax Reitz                              &op->qiov, 0);
395b9b10c35SKevin Wolf     }
3962e1990b2SMax Reitz     mirror_read_complete(op, ret);
397e5b43573SFam Zheng }
398e5b43573SFam Zheng 
3992e1990b2SMax Reitz static void coroutine_fn mirror_co_zero(void *opaque)
400e5b43573SFam Zheng {
4012e1990b2SMax Reitz     MirrorOp *op = opaque;
4022e1990b2SMax Reitz     int ret;
403e5b43573SFam Zheng 
4042e1990b2SMax Reitz     op->s->in_flight++;
4052e1990b2SMax Reitz     op->s->bytes_in_flight += op->bytes;
4062e1990b2SMax Reitz     *op->bytes_handled = op->bytes;
407ce8cabbdSKevin Wolf     op->is_in_flight = true;
408e5b43573SFam Zheng 
4092e1990b2SMax Reitz     ret = blk_co_pwrite_zeroes(op->s->target, op->offset, op->bytes,
4102e1990b2SMax Reitz                                op->s->unmap ? BDRV_REQ_MAY_UNMAP : 0);
4112e1990b2SMax Reitz     mirror_write_complete(op, ret);
412e5b43573SFam Zheng }
4132e1990b2SMax Reitz 
4142e1990b2SMax Reitz static void coroutine_fn mirror_co_discard(void *opaque)
4152e1990b2SMax Reitz {
4162e1990b2SMax Reitz     MirrorOp *op = opaque;
4172e1990b2SMax Reitz     int ret;
4182e1990b2SMax Reitz 
4192e1990b2SMax Reitz     op->s->in_flight++;
4202e1990b2SMax Reitz     op->s->bytes_in_flight += op->bytes;
4212e1990b2SMax Reitz     *op->bytes_handled = op->bytes;
422ce8cabbdSKevin Wolf     op->is_in_flight = true;
4232e1990b2SMax Reitz 
4242e1990b2SMax Reitz     ret = blk_co_pdiscard(op->s->target, op->offset, op->bytes);
4252e1990b2SMax Reitz     mirror_write_complete(op, ret);
426e5b43573SFam Zheng }
427e5b43573SFam Zheng 
4284295c5fcSMax Reitz static unsigned mirror_perform(MirrorBlockJob *s, int64_t offset,
4294295c5fcSMax Reitz                                unsigned bytes, MirrorMethod mirror_method)
4304295c5fcSMax Reitz {
4312e1990b2SMax Reitz     MirrorOp *op;
4322e1990b2SMax Reitz     Coroutine *co;
4332e1990b2SMax Reitz     int64_t bytes_handled = -1;
4342e1990b2SMax Reitz 
4352e1990b2SMax Reitz     op = g_new(MirrorOp, 1);
4362e1990b2SMax Reitz     *op = (MirrorOp){
4372e1990b2SMax Reitz         .s              = s,
4382e1990b2SMax Reitz         .offset         = offset,
4392e1990b2SMax Reitz         .bytes          = bytes,
4402e1990b2SMax Reitz         .bytes_handled  = &bytes_handled,
4412e1990b2SMax Reitz     };
44212aa4082SMax Reitz     qemu_co_queue_init(&op->waiting_requests);
4432e1990b2SMax Reitz 
4444295c5fcSMax Reitz     switch (mirror_method) {
4454295c5fcSMax Reitz     case MIRROR_METHOD_COPY:
4462e1990b2SMax Reitz         co = qemu_coroutine_create(mirror_co_read, op);
4472e1990b2SMax Reitz         break;
4484295c5fcSMax Reitz     case MIRROR_METHOD_ZERO:
4492e1990b2SMax Reitz         co = qemu_coroutine_create(mirror_co_zero, op);
4502e1990b2SMax Reitz         break;
4514295c5fcSMax Reitz     case MIRROR_METHOD_DISCARD:
4522e1990b2SMax Reitz         co = qemu_coroutine_create(mirror_co_discard, op);
4532e1990b2SMax Reitz         break;
4544295c5fcSMax Reitz     default:
4554295c5fcSMax Reitz         abort();
4564295c5fcSMax Reitz     }
457eed325b9SKevin Wolf     op->co = co;
4582e1990b2SMax Reitz 
45912aa4082SMax Reitz     QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next);
4602e1990b2SMax Reitz     qemu_coroutine_enter(co);
4612e1990b2SMax Reitz     /* At this point, ownership of op has been moved to the coroutine
4622e1990b2SMax Reitz      * and the object may already be freed */
4632e1990b2SMax Reitz 
4642e1990b2SMax Reitz     /* Assert that this value has been set */
4652e1990b2SMax Reitz     assert(bytes_handled >= 0);
4662e1990b2SMax Reitz 
4672e1990b2SMax Reitz     /* Same assertion as in mirror_co_read() (and for mirror_co_read()
4682e1990b2SMax Reitz      * and mirror_co_discard(), bytes_handled == op->bytes, which
4692e1990b2SMax Reitz      * is the @bytes parameter given to this function) */
4702e1990b2SMax Reitz     assert(bytes_handled <= UINT_MAX);
4712e1990b2SMax Reitz     return bytes_handled;
4724295c5fcSMax Reitz }
4734295c5fcSMax Reitz 
474e5b43573SFam Zheng static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
475e5b43573SFam Zheng {
476138f9fffSMax Reitz     BlockDriverState *source = s->mirror_top_bs->backing->bs;
4771181e19aSMax Reitz     MirrorOp *pseudo_op;
4781181e19aSMax Reitz     int64_t offset;
4791181e19aSMax Reitz     uint64_t delay_ns = 0, ret = 0;
480e5b43573SFam Zheng     /* At least the first dirty chunk is mirrored in one iteration. */
481e5b43573SFam Zheng     int nb_chunks = 1;
4824b5004d9SDenis V. Lunev     bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target));
483b436982fSEric Blake     int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES);
484e5b43573SFam Zheng 
485b64bd51eSPaolo Bonzini     bdrv_dirty_bitmap_lock(s->dirty_bitmap);
486f798184cSEric Blake     offset = bdrv_dirty_iter_next(s->dbi);
487fb2ef791SEric Blake     if (offset < 0) {
488dc162c8eSFam Zheng         bdrv_set_dirty_iter(s->dbi, 0);
489f798184cSEric Blake         offset = bdrv_dirty_iter_next(s->dbi);
4909a46dba7SEric Blake         trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
491fb2ef791SEric Blake         assert(offset >= 0);
492e5b43573SFam Zheng     }
493b64bd51eSPaolo Bonzini     bdrv_dirty_bitmap_unlock(s->dirty_bitmap);
494e5b43573SFam Zheng 
495d69a879bSHanna Reitz     /*
496d69a879bSHanna Reitz      * Wait for concurrent requests to @offset.  The next loop will limit the
497d69a879bSHanna Reitz      * copied area based on in_flight_bitmap so we only copy an area that does
498d69a879bSHanna Reitz      * not overlap with concurrent in-flight requests.  Still, we would like to
499d69a879bSHanna Reitz      * copy something, so wait until there are at least no more requests to the
500d69a879bSHanna Reitz      * very beginning of the area.
501d69a879bSHanna Reitz      */
5021181e19aSMax Reitz     mirror_wait_on_conflicts(NULL, s, offset, 1);
5039c83625bSMax Reitz 
504da01ff7fSKevin Wolf     job_pause_point(&s->common.job);
505565ac01fSStefan Hajnoczi 
506e5b43573SFam Zheng     /* Find the number of consective dirty chunks following the first dirty
507e5b43573SFam Zheng      * one, and wait for in flight requests in them. */
508b64bd51eSPaolo Bonzini     bdrv_dirty_bitmap_lock(s->dirty_bitmap);
509fb2ef791SEric Blake     while (nb_chunks * s->granularity < s->buf_size) {
510dc162c8eSFam Zheng         int64_t next_dirty;
511fb2ef791SEric Blake         int64_t next_offset = offset + nb_chunks * s->granularity;
512fb2ef791SEric Blake         int64_t next_chunk = next_offset / s->granularity;
513fb2ef791SEric Blake         if (next_offset >= s->bdev_length ||
51428636b82SJohn Snow             !bdrv_dirty_bitmap_get_locked(s->dirty_bitmap, next_offset)) {
515e5b43573SFam Zheng             break;
516e5b43573SFam Zheng         }
517e5b43573SFam Zheng         if (test_bit(next_chunk, s->in_flight_bitmap)) {
518e5b43573SFam Zheng             break;
519e5b43573SFam Zheng         }
5209c83625bSMax Reitz 
521f798184cSEric Blake         next_dirty = bdrv_dirty_iter_next(s->dbi);
522fb2ef791SEric Blake         if (next_dirty > next_offset || next_dirty < 0) {
523f27a2742SMax Reitz             /* The bitmap iterator's cache is stale, refresh it */
524715a74d8SEric Blake             bdrv_set_dirty_iter(s->dbi, next_offset);
525f798184cSEric Blake             next_dirty = bdrv_dirty_iter_next(s->dbi);
526f27a2742SMax Reitz         }
527fb2ef791SEric Blake         assert(next_dirty == next_offset);
528e5b43573SFam Zheng         nb_chunks++;
529e5b43573SFam Zheng     }
530e5b43573SFam Zheng 
531e5b43573SFam Zheng     /* Clear dirty bits before querying the block status, because
53231826642SEric Blake      * calling bdrv_block_status_above could yield - if some blocks are
533e5b43573SFam Zheng      * marked dirty in this window, we need to know.
534e5b43573SFam Zheng      */
535e0d7f73eSEric Blake     bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset,
536e0d7f73eSEric Blake                                    nb_chunks * s->granularity);
537b64bd51eSPaolo Bonzini     bdrv_dirty_bitmap_unlock(s->dirty_bitmap);
538b64bd51eSPaolo Bonzini 
5391181e19aSMax Reitz     /* Before claiming an area in the in-flight bitmap, we have to
5401181e19aSMax Reitz      * create a MirrorOp for it so that conflicting requests can wait
5411181e19aSMax Reitz      * for it.  mirror_perform() will create the real MirrorOps later,
5421181e19aSMax Reitz      * for now we just create a pseudo operation that will wake up all
5431181e19aSMax Reitz      * conflicting requests once all real operations have been
5441181e19aSMax Reitz      * launched. */
5451181e19aSMax Reitz     pseudo_op = g_new(MirrorOp, 1);
5461181e19aSMax Reitz     *pseudo_op = (MirrorOp){
5471181e19aSMax Reitz         .offset         = offset,
5481181e19aSMax Reitz         .bytes          = nb_chunks * s->granularity,
5491181e19aSMax Reitz         .is_pseudo_op   = true,
5501181e19aSMax Reitz     };
5511181e19aSMax Reitz     qemu_co_queue_init(&pseudo_op->waiting_requests);
5521181e19aSMax Reitz     QTAILQ_INSERT_TAIL(&s->ops_in_flight, pseudo_op, next);
5531181e19aSMax Reitz 
554fb2ef791SEric Blake     bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks);
555fb2ef791SEric Blake     while (nb_chunks > 0 && offset < s->bdev_length) {
55631826642SEric Blake         int ret;
5577cfd5275SEric Blake         int64_t io_bytes;
558f3e4ce4aSEric Blake         int64_t io_bytes_acct;
5594295c5fcSMax Reitz         MirrorMethod mirror_method = MIRROR_METHOD_COPY;
560e5b43573SFam Zheng 
561fb2ef791SEric Blake         assert(!(offset % s->granularity));
5627ff9579eSKevin Wolf         WITH_GRAPH_RDLOCK_GUARD() {
56331826642SEric Blake             ret = bdrv_block_status_above(source, NULL, offset,
56431826642SEric Blake                                         nb_chunks * s->granularity,
56531826642SEric Blake                                         &io_bytes, NULL, NULL);
5667ff9579eSKevin Wolf         }
567e5b43573SFam Zheng         if (ret < 0) {
568fb2ef791SEric Blake             io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes);
5690965a41eSVladimir Sementsov-Ogievskiy         } else if (ret & BDRV_BLOCK_DATA) {
570fb2ef791SEric Blake             io_bytes = MIN(io_bytes, max_io_bytes);
571e5b43573SFam Zheng         }
572e5b43573SFam Zheng 
573fb2ef791SEric Blake         io_bytes -= io_bytes % s->granularity;
574fb2ef791SEric Blake         if (io_bytes < s->granularity) {
575fb2ef791SEric Blake             io_bytes = s->granularity;
576e5b43573SFam Zheng         } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) {
577fb2ef791SEric Blake             int64_t target_offset;
5787cfd5275SEric Blake             int64_t target_bytes;
579a00e70c0SEmanuele Giuseppe Esposito             WITH_GRAPH_RDLOCK_GUARD() {
580fb2ef791SEric Blake                 bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes,
581fb2ef791SEric Blake                                        &target_offset, &target_bytes);
582a00e70c0SEmanuele Giuseppe Esposito             }
583fb2ef791SEric Blake             if (target_offset == offset &&
584fb2ef791SEric Blake                 target_bytes == io_bytes) {
585e5b43573SFam Zheng                 mirror_method = ret & BDRV_BLOCK_ZERO ?
586e5b43573SFam Zheng                                     MIRROR_METHOD_ZERO :
587e5b43573SFam Zheng                                     MIRROR_METHOD_DISCARD;
588e5b43573SFam Zheng             }
589e5b43573SFam Zheng         }
590e5b43573SFam Zheng 
591cf56a3c6SDenis V. Lunev         while (s->in_flight >= MAX_IN_FLIGHT) {
592fb2ef791SEric Blake             trace_mirror_yield_in_flight(s, offset, s->in_flight);
5939178f4feSKevin Wolf             mirror_wait_for_free_in_flight_slot(s);
594cf56a3c6SDenis V. Lunev         }
595cf56a3c6SDenis V. Lunev 
596dbaa7b57SVladimir Sementsov-Ogievskiy         if (s->ret < 0) {
5971181e19aSMax Reitz             ret = 0;
5981181e19aSMax Reitz             goto fail;
599dbaa7b57SVladimir Sementsov-Ogievskiy         }
600dbaa7b57SVladimir Sementsov-Ogievskiy 
601fb2ef791SEric Blake         io_bytes = mirror_clip_bytes(s, offset, io_bytes);
6024295c5fcSMax Reitz         io_bytes = mirror_perform(s, offset, io_bytes, mirror_method);
6034295c5fcSMax Reitz         if (mirror_method != MIRROR_METHOD_COPY && write_zeroes_ok) {
604f3e4ce4aSEric Blake             io_bytes_acct = 0;
6054b5004d9SDenis V. Lunev         } else {
606fb2ef791SEric Blake             io_bytes_acct = io_bytes;
6074b5004d9SDenis V. Lunev         }
608fb2ef791SEric Blake         assert(io_bytes);
609fb2ef791SEric Blake         offset += io_bytes;
610fb2ef791SEric Blake         nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity);
611dee81d51SKevin Wolf         delay_ns = block_job_ratelimit_get_delay(&s->common, io_bytes_acct);
612dcfb3bebSFam Zheng     }
6131181e19aSMax Reitz 
6141181e19aSMax Reitz     ret = delay_ns;
6151181e19aSMax Reitz fail:
6161181e19aSMax Reitz     QTAILQ_REMOVE(&s->ops_in_flight, pseudo_op, next);
6171181e19aSMax Reitz     qemu_co_queue_restart_all(&pseudo_op->waiting_requests);
6181181e19aSMax Reitz     g_free(pseudo_op);
6191181e19aSMax Reitz 
6201181e19aSMax Reitz     return ret;
621893f7ebaSPaolo Bonzini }
622b952b558SPaolo Bonzini 
623402a4741SPaolo Bonzini static void mirror_free_init(MirrorBlockJob *s)
624402a4741SPaolo Bonzini {
625402a4741SPaolo Bonzini     int granularity = s->granularity;
626402a4741SPaolo Bonzini     size_t buf_size = s->buf_size;
627402a4741SPaolo Bonzini     uint8_t *buf = s->buf;
628402a4741SPaolo Bonzini 
629402a4741SPaolo Bonzini     assert(s->buf_free_count == 0);
630402a4741SPaolo Bonzini     QSIMPLEQ_INIT(&s->buf_free);
631402a4741SPaolo Bonzini     while (buf_size != 0) {
632402a4741SPaolo Bonzini         MirrorBuffer *cur = (MirrorBuffer *)buf;
633402a4741SPaolo Bonzini         QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
634402a4741SPaolo Bonzini         s->buf_free_count++;
635402a4741SPaolo Bonzini         buf_size -= granularity;
636402a4741SPaolo Bonzini         buf += granularity;
637402a4741SPaolo Bonzini     }
638402a4741SPaolo Bonzini }
639402a4741SPaolo Bonzini 
640bae8196dSPaolo Bonzini /* This is also used for the .pause callback. There is no matching
641bae8196dSPaolo Bonzini  * mirror_resume() because mirror_run() will begin iterating again
642bae8196dSPaolo Bonzini  * when the job is resumed.
643bae8196dSPaolo Bonzini  */
644537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_wait_for_all_io(MirrorBlockJob *s)
645bd48bde8SPaolo Bonzini {
646bd48bde8SPaolo Bonzini     while (s->in_flight > 0) {
6479178f4feSKevin Wolf         mirror_wait_for_free_in_flight_slot(s);
648bd48bde8SPaolo Bonzini     }
649893f7ebaSPaolo Bonzini }
650893f7ebaSPaolo Bonzini 
651737efc1eSJohn Snow /**
652737efc1eSJohn Snow  * mirror_exit_common: handle both abort() and prepare() cases.
653737efc1eSJohn Snow  * for .prepare, returns 0 on success and -errno on failure.
654737efc1eSJohn Snow  * for .abort cases, denoted by abort = true, MUST return 0.
655737efc1eSJohn Snow  */
656737efc1eSJohn Snow static int mirror_exit_common(Job *job)
6575a7e7a0bSStefan Hajnoczi {
6581908a559SKevin Wolf     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
6591908a559SKevin Wolf     BlockJob *bjob = &s->common;
660f93c3addSMax Reitz     MirrorBDSOpaque *bs_opaque;
6615a7e7a0bSStefan Hajnoczi     AioContext *replace_aio_context = NULL;
662f93c3addSMax Reitz     BlockDriverState *src;
663f93c3addSMax Reitz     BlockDriverState *target_bs;
664f93c3addSMax Reitz     BlockDriverState *mirror_top_bs;
66512fa4af6SKevin Wolf     Error *local_err = NULL;
666737efc1eSJohn Snow     bool abort = job->ret < 0;
667737efc1eSJohn Snow     int ret = 0;
668737efc1eSJohn Snow 
669737efc1eSJohn Snow     if (s->prepared) {
670737efc1eSJohn Snow         return 0;
671737efc1eSJohn Snow     }
672737efc1eSJohn Snow     s->prepared = true;
6733f09bfbcSKevin Wolf 
674f93c3addSMax Reitz     mirror_top_bs = s->mirror_top_bs;
675f93c3addSMax Reitz     bs_opaque = mirror_top_bs->opaque;
676f93c3addSMax Reitz     src = mirror_top_bs->backing->bs;
677f93c3addSMax Reitz     target_bs = blk_bs(s->target);
678f93c3addSMax Reitz 
679ef53dc09SAlberto Garcia     if (bdrv_chain_contains(src, target_bs)) {
680ef53dc09SAlberto Garcia         bdrv_unfreeze_backing_chain(mirror_top_bs, target_bs);
681ef53dc09SAlberto Garcia     }
682ef53dc09SAlberto Garcia 
6835deb6cbdSVladimir Sementsov-Ogievskiy     bdrv_release_dirty_bitmap(s->dirty_bitmap);
6842119882cSPaolo Bonzini 
6857b508f6bSJohn Snow     /* Make sure that the source BDS doesn't go away during bdrv_replace_node,
6867b508f6bSJohn Snow      * before we can call bdrv_drained_end */
6873f09bfbcSKevin Wolf     bdrv_ref(src);
6884ef85a9cSKevin Wolf     bdrv_ref(mirror_top_bs);
6897d9fcb39SKevin Wolf     bdrv_ref(target_bs);
6907d9fcb39SKevin Wolf 
691bb0c9409SVladimir Sementsov-Ogievskiy     /*
692bb0c9409SVladimir Sementsov-Ogievskiy      * Remove target parent that still uses BLK_PERM_WRITE/RESIZE before
6937d9fcb39SKevin Wolf      * inserting target_bs at s->to_replace, where we might not be able to get
69463c8ef28SKevin Wolf      * these permissions.
695bb0c9409SVladimir Sementsov-Ogievskiy      */
6967d9fcb39SKevin Wolf     blk_unref(s->target);
6977d9fcb39SKevin Wolf     s->target = NULL;
6984ef85a9cSKevin Wolf 
6994ef85a9cSKevin Wolf     /* We don't access the source any more. Dropping any WRITE/RESIZE is
700d2da5e28SKevin Wolf      * required before it could become a backing file of target_bs. Not having
701d2da5e28SKevin Wolf      * these permissions any more means that we can't allow any new requests on
702d2da5e28SKevin Wolf      * mirror_top_bs from now on, so keep it drained. */
703d2da5e28SKevin Wolf     bdrv_drained_begin(mirror_top_bs);
704f94dc3b4SMax Reitz     bs_opaque->stop = true;
705f94dc3b4SMax Reitz     bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing,
7064ef85a9cSKevin Wolf                              &error_abort);
707737efc1eSJohn Snow     if (!abort && s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) {
7084ef85a9cSKevin Wolf         BlockDriverState *backing = s->is_none_mode ? src : s->base;
7093f072a7fSMax Reitz         BlockDriverState *unfiltered_target = bdrv_skip_filters(target_bs);
7103f072a7fSMax Reitz 
7113f072a7fSMax Reitz         if (bdrv_cow_bs(unfiltered_target) != backing) {
7123f072a7fSMax Reitz             bdrv_set_backing_hd(unfiltered_target, backing, &local_err);
71312fa4af6SKevin Wolf             if (local_err) {
71412fa4af6SKevin Wolf                 error_report_err(local_err);
71566c8672dSVladimir Sementsov-Ogievskiy                 local_err = NULL;
7167b508f6bSJohn Snow                 ret = -EPERM;
71712fa4af6SKevin Wolf             }
7184ef85a9cSKevin Wolf         }
719c41f5b96SMax Reitz     } else if (!abort && s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) {
720c41f5b96SMax Reitz         assert(!bdrv_backing_chain_next(target_bs));
721c41f5b96SMax Reitz         ret = bdrv_open_backing_file(bdrv_skip_filters(target_bs), NULL,
722c41f5b96SMax Reitz                                      "backing", &local_err);
723c41f5b96SMax Reitz         if (ret < 0) {
724c41f5b96SMax Reitz             error_report_err(local_err);
725c41f5b96SMax Reitz             local_err = NULL;
726c41f5b96SMax Reitz         }
7274ef85a9cSKevin Wolf     }
7285a7e7a0bSStefan Hajnoczi 
7295a7e7a0bSStefan Hajnoczi     if (s->to_replace) {
7305a7e7a0bSStefan Hajnoczi         replace_aio_context = bdrv_get_aio_context(s->to_replace);
7315a7e7a0bSStefan Hajnoczi         aio_context_acquire(replace_aio_context);
7325a7e7a0bSStefan Hajnoczi     }
7335a7e7a0bSStefan Hajnoczi 
734737efc1eSJohn Snow     if (s->should_complete && !abort) {
735737efc1eSJohn Snow         BlockDriverState *to_replace = s->to_replace ?: src;
7361ba79388SAlberto Garcia         bool ro = bdrv_is_read_only(to_replace);
73740365552SKevin Wolf 
7381ba79388SAlberto Garcia         if (ro != bdrv_is_read_only(target_bs)) {
7391ba79388SAlberto Garcia             bdrv_reopen_set_read_only(target_bs, ro, NULL);
7405a7e7a0bSStefan Hajnoczi         }
741b8804815SKevin Wolf 
742b8804815SKevin Wolf         /* The mirror job has no requests in flight any more, but we need to
743b8804815SKevin Wolf          * drain potential other users of the BDS before changing the graph. */
7445e771752SSergio Lopez         assert(s->in_drain);
745e253f4b8SKevin Wolf         bdrv_drained_begin(target_bs);
7466e9cc051SMax Reitz         /*
7476e9cc051SMax Reitz          * Cannot use check_to_replace_node() here, because that would
7486e9cc051SMax Reitz          * check for an op blocker on @to_replace, and we have our own
7496e9cc051SMax Reitz          * there.
750*533c6e4eSKevin Wolf          *
751*533c6e4eSKevin Wolf          * TODO Pull out the writer lock from bdrv_replace_node() to here
7526e9cc051SMax Reitz          */
753*533c6e4eSKevin Wolf         bdrv_graph_rdlock_main_loop();
7546e9cc051SMax Reitz         if (bdrv_recurse_can_replace(src, to_replace)) {
7555fe31c25SKevin Wolf             bdrv_replace_node(to_replace, target_bs, &local_err);
7566e9cc051SMax Reitz         } else {
7576e9cc051SMax Reitz             error_setg(&local_err, "Can no longer replace '%s' by '%s', "
7586e9cc051SMax Reitz                        "because it can no longer be guaranteed that doing so "
7596e9cc051SMax Reitz                        "would not lead to an abrupt change of visible data",
7606e9cc051SMax Reitz                        to_replace->node_name, target_bs->node_name);
7616e9cc051SMax Reitz         }
762*533c6e4eSKevin Wolf         bdrv_graph_rdunlock_main_loop();
763e253f4b8SKevin Wolf         bdrv_drained_end(target_bs);
7645fe31c25SKevin Wolf         if (local_err) {
7655fe31c25SKevin Wolf             error_report_err(local_err);
7667b508f6bSJohn Snow             ret = -EPERM;
7675fe31c25SKevin Wolf         }
7685a7e7a0bSStefan Hajnoczi     }
7695a7e7a0bSStefan Hajnoczi     if (s->to_replace) {
7705a7e7a0bSStefan Hajnoczi         bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
7715a7e7a0bSStefan Hajnoczi         error_free(s->replace_blocker);
7725a7e7a0bSStefan Hajnoczi         bdrv_unref(s->to_replace);
7735a7e7a0bSStefan Hajnoczi     }
7745a7e7a0bSStefan Hajnoczi     if (replace_aio_context) {
7755a7e7a0bSStefan Hajnoczi         aio_context_release(replace_aio_context);
7765a7e7a0bSStefan Hajnoczi     }
7775a7e7a0bSStefan Hajnoczi     g_free(s->replaces);
7787d9fcb39SKevin Wolf     bdrv_unref(target_bs);
7794ef85a9cSKevin Wolf 
780f94dc3b4SMax Reitz     /*
781f94dc3b4SMax Reitz      * Remove the mirror filter driver from the graph. Before this, get rid of
7824ef85a9cSKevin Wolf      * the blockers on the intermediate nodes so that the resulting state is
783f94dc3b4SMax Reitz      * valid.
784f94dc3b4SMax Reitz      */
7851908a559SKevin Wolf     block_job_remove_all_bdrv(bjob);
7863f072a7fSMax Reitz     bdrv_replace_node(mirror_top_bs, mirror_top_bs->backing->bs, &error_abort);
7874ef85a9cSKevin Wolf 
788429076e8SMax Reitz     bs_opaque->job = NULL;
7894ef85a9cSKevin Wolf 
790176c3699SFam Zheng     bdrv_drained_end(src);
791d2da5e28SKevin Wolf     bdrv_drained_end(mirror_top_bs);
7925e771752SSergio Lopez     s->in_drain = false;
7934ef85a9cSKevin Wolf     bdrv_unref(mirror_top_bs);
7943f09bfbcSKevin Wolf     bdrv_unref(src);
7957b508f6bSJohn Snow 
796737efc1eSJohn Snow     return ret;
797737efc1eSJohn Snow }
798737efc1eSJohn Snow 
799737efc1eSJohn Snow static int mirror_prepare(Job *job)
800737efc1eSJohn Snow {
801737efc1eSJohn Snow     return mirror_exit_common(job);
802737efc1eSJohn Snow }
803737efc1eSJohn Snow 
804737efc1eSJohn Snow static void mirror_abort(Job *job)
805737efc1eSJohn Snow {
806737efc1eSJohn Snow     int ret = mirror_exit_common(job);
807737efc1eSJohn Snow     assert(ret == 0);
8085a7e7a0bSStefan Hajnoczi }
8095a7e7a0bSStefan Hajnoczi 
810537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_throttle(MirrorBlockJob *s)
81149efb1f5SDenis V. Lunev {
81249efb1f5SDenis V. Lunev     int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
81349efb1f5SDenis V. Lunev 
81418bb6928SKevin Wolf     if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) {
81549efb1f5SDenis V. Lunev         s->last_pause_ns = now;
8165d43e86eSKevin Wolf         job_sleep_ns(&s->common.job, 0);
81749efb1f5SDenis V. Lunev     } else {
818da01ff7fSKevin Wolf         job_pause_point(&s->common.job);
81949efb1f5SDenis V. Lunev     }
82049efb1f5SDenis V. Lunev }
82149efb1f5SDenis V. Lunev 
822c0b363adSDenis V. Lunev static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
823c0b363adSDenis V. Lunev {
82423ca459aSEric Blake     int64_t offset;
825138f9fffSMax Reitz     BlockDriverState *bs = s->mirror_top_bs->backing->bs;
826c0b363adSDenis V. Lunev     BlockDriverState *target_bs = blk_bs(s->target);
82723ca459aSEric Blake     int ret;
82851b0a488SEric Blake     int64_t count;
829c0b363adSDenis V. Lunev 
830cdf3bc93SMax Reitz     if (s->zero_target) {
831c7c2769cSDenis V. Lunev         if (!bdrv_can_write_zeroes_with_unmap(target_bs)) {
832e0d7f73eSEric Blake             bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length);
833b7d5062cSDenis V. Lunev             return 0;
834b7d5062cSDenis V. Lunev         }
835b7d5062cSDenis V. Lunev 
83690ab48ebSAnton Nefedov         s->initial_zeroing_ongoing = true;
83723ca459aSEric Blake         for (offset = 0; offset < s->bdev_length; ) {
83823ca459aSEric Blake             int bytes = MIN(s->bdev_length - offset,
83923ca459aSEric Blake                             QEMU_ALIGN_DOWN(INT_MAX, s->granularity));
840c7c2769cSDenis V. Lunev 
841c7c2769cSDenis V. Lunev             mirror_throttle(s);
842c7c2769cSDenis V. Lunev 
843daa7f2f9SKevin Wolf             if (job_is_cancelled(&s->common.job)) {
84490ab48ebSAnton Nefedov                 s->initial_zeroing_ongoing = false;
845c7c2769cSDenis V. Lunev                 return 0;
846c7c2769cSDenis V. Lunev             }
847c7c2769cSDenis V. Lunev 
848c7c2769cSDenis V. Lunev             if (s->in_flight >= MAX_IN_FLIGHT) {
84967adf4b3SEric Blake                 trace_mirror_yield(s, UINT64_MAX, s->buf_free_count,
85067adf4b3SEric Blake                                    s->in_flight);
8519178f4feSKevin Wolf                 mirror_wait_for_free_in_flight_slot(s);
852c7c2769cSDenis V. Lunev                 continue;
853c7c2769cSDenis V. Lunev             }
854c7c2769cSDenis V. Lunev 
8554295c5fcSMax Reitz             mirror_perform(s, offset, bytes, MIRROR_METHOD_ZERO);
85623ca459aSEric Blake             offset += bytes;
857c7c2769cSDenis V. Lunev         }
858c7c2769cSDenis V. Lunev 
859bae8196dSPaolo Bonzini         mirror_wait_for_all_io(s);
86090ab48ebSAnton Nefedov         s->initial_zeroing_ongoing = false;
861c7c2769cSDenis V. Lunev     }
862c7c2769cSDenis V. Lunev 
863c0b363adSDenis V. Lunev     /* First part, loop on the sectors and initialize the dirty bitmap.  */
86423ca459aSEric Blake     for (offset = 0; offset < s->bdev_length; ) {
865c0b363adSDenis V. Lunev         /* Just to make sure we are not exceeding int limit. */
86623ca459aSEric Blake         int bytes = MIN(s->bdev_length - offset,
86723ca459aSEric Blake                         QEMU_ALIGN_DOWN(INT_MAX, s->granularity));
868c0b363adSDenis V. Lunev 
869c0b363adSDenis V. Lunev         mirror_throttle(s);
870c0b363adSDenis V. Lunev 
871daa7f2f9SKevin Wolf         if (job_is_cancelled(&s->common.job)) {
872c0b363adSDenis V. Lunev             return 0;
873c0b363adSDenis V. Lunev         }
874c0b363adSDenis V. Lunev 
8757ff9579eSKevin Wolf         WITH_GRAPH_RDLOCK_GUARD() {
8767ff9579eSKevin Wolf             ret = bdrv_is_allocated_above(bs, s->base_overlay, true, offset,
8777ff9579eSKevin Wolf                                           bytes, &count);
8787ff9579eSKevin Wolf         }
879c0b363adSDenis V. Lunev         if (ret < 0) {
880c0b363adSDenis V. Lunev             return ret;
881c0b363adSDenis V. Lunev         }
882c0b363adSDenis V. Lunev 
88323ca459aSEric Blake         assert(count);
884a92b1b06SEric Blake         if (ret > 0) {
88523ca459aSEric Blake             bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count);
886c0b363adSDenis V. Lunev         }
88723ca459aSEric Blake         offset += count;
888c0b363adSDenis V. Lunev     }
889c0b363adSDenis V. Lunev     return 0;
890c0b363adSDenis V. Lunev }
891c0b363adSDenis V. Lunev 
892bdffb31dSPaolo Bonzini /* Called when going out of the streaming phase to flush the bulk of the
893bdffb31dSPaolo Bonzini  * data to the medium, or just before completing.
894bdffb31dSPaolo Bonzini  */
89526bef102SPaolo Bonzini static int coroutine_fn mirror_flush(MirrorBlockJob *s)
896bdffb31dSPaolo Bonzini {
89726bef102SPaolo Bonzini     int ret = blk_co_flush(s->target);
898bdffb31dSPaolo Bonzini     if (ret < 0) {
899bdffb31dSPaolo Bonzini         if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) {
900bdffb31dSPaolo Bonzini             s->ret = ret;
901bdffb31dSPaolo Bonzini         }
902bdffb31dSPaolo Bonzini     }
903bdffb31dSPaolo Bonzini     return ret;
904bdffb31dSPaolo Bonzini }
905bdffb31dSPaolo Bonzini 
906f67432a2SJohn Snow static int coroutine_fn mirror_run(Job *job, Error **errp)
907893f7ebaSPaolo Bonzini {
908f67432a2SJohn Snow     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
909138f9fffSMax Reitz     BlockDriverState *bs = s->mirror_top_bs->backing->bs;
91032125b14SKevin Wolf     MirrorBDSOpaque *mirror_top_opaque = s->mirror_top_bs->opaque;
911e253f4b8SKevin Wolf     BlockDriverState *target_bs = blk_bs(s->target);
9129a0cec66SPaolo Bonzini     bool need_drain = true;
913d59cb66dSEmanuele Giuseppe Esposito     BlockDeviceIoStatus iostatus;
914c0b363adSDenis V. Lunev     int64_t length;
915e83dd680SKevin Wolf     int64_t target_length;
916b812f671SPaolo Bonzini     BlockDriverInfo bdi;
9171d33936eSJeff Cody     char backing_filename[2]; /* we only need 2 characters because we are only
9181d33936eSJeff Cody                                  checking for a NULL string */
919893f7ebaSPaolo Bonzini     int ret = 0;
920893f7ebaSPaolo Bonzini 
921daa7f2f9SKevin Wolf     if (job_is_cancelled(&s->common.job)) {
922893f7ebaSPaolo Bonzini         goto immediate_exit;
923893f7ebaSPaolo Bonzini     }
924893f7ebaSPaolo Bonzini 
9258ab8140aSKevin Wolf     bdrv_graph_co_rdlock();
926c86422c5SEmanuele Giuseppe Esposito     s->bdev_length = bdrv_co_getlength(bs);
9278ab8140aSKevin Wolf     bdrv_graph_co_rdunlock();
9288ab8140aSKevin Wolf 
929b21c7652SMax Reitz     if (s->bdev_length < 0) {
930b21c7652SMax Reitz         ret = s->bdev_length;
931373df5b1SFam Zheng         goto immediate_exit;
932becc347eSKevin Wolf     }
933becc347eSKevin Wolf 
934c86422c5SEmanuele Giuseppe Esposito     target_length = blk_co_getlength(s->target);
935e83dd680SKevin Wolf     if (target_length < 0) {
936e83dd680SKevin Wolf         ret = target_length;
937becc347eSKevin Wolf         goto immediate_exit;
938becc347eSKevin Wolf     }
939becc347eSKevin Wolf 
940e83dd680SKevin Wolf     /* Active commit must resize the base image if its size differs from the
941e83dd680SKevin Wolf      * active layer. */
942e83dd680SKevin Wolf     if (s->base == blk_bs(s->target)) {
943e83dd680SKevin Wolf         if (s->bdev_length > target_length) {
94488276216SAlberto Faria             ret = blk_co_truncate(s->target, s->bdev_length, false,
9458c6242b6SKevin Wolf                                   PREALLOC_MODE_OFF, 0, NULL);
946becc347eSKevin Wolf             if (ret < 0) {
947becc347eSKevin Wolf                 goto immediate_exit;
948becc347eSKevin Wolf             }
949becc347eSKevin Wolf         }
950e83dd680SKevin Wolf     } else if (s->bdev_length != target_length) {
951e83dd680SKevin Wolf         error_setg(errp, "Source and target image have different sizes");
952e83dd680SKevin Wolf         ret = -EINVAL;
953e83dd680SKevin Wolf         goto immediate_exit;
954becc347eSKevin Wolf     }
955becc347eSKevin Wolf 
956becc347eSKevin Wolf     if (s->bdev_length == 0) {
9572e1795b5SKevin Wolf         /* Transition to the READY state and wait for complete. */
9582e1795b5SKevin Wolf         job_transition_to_ready(&s->common.job);
959d06107adSMax Reitz         s->actively_synced = true;
96008b83bffSHanna Reitz         while (!job_cancel_requested(&s->common.job) && !s->should_complete) {
961198c49ccSKevin Wolf             job_yield(&s->common.job);
9629e48b025SFam Zheng         }
9639e48b025SFam Zheng         goto immediate_exit;
964893f7ebaSPaolo Bonzini     }
965893f7ebaSPaolo Bonzini 
966b21c7652SMax Reitz     length = DIV_ROUND_UP(s->bdev_length, s->granularity);
967402a4741SPaolo Bonzini     s->in_flight_bitmap = bitmap_new(length);
968402a4741SPaolo Bonzini 
969b812f671SPaolo Bonzini     /* If we have no backing file yet in the destination, we cannot let
970b812f671SPaolo Bonzini      * the destination do COW.  Instead, we copy sectors around the
971b812f671SPaolo Bonzini      * dirty data if needed.  We need a bitmap to do that.
972b812f671SPaolo Bonzini      */
973e253f4b8SKevin Wolf     bdrv_get_backing_filename(target_bs, backing_filename,
974b812f671SPaolo Bonzini                               sizeof(backing_filename));
975a00e70c0SEmanuele Giuseppe Esposito     bdrv_graph_co_rdlock();
9763d47eb0aSEmanuele Giuseppe Esposito     if (!bdrv_co_get_info(target_bs, &bdi) && bdi.cluster_size) {
977b436982fSEric Blake         s->target_cluster_size = bdi.cluster_size;
978b436982fSEric Blake     } else {
979b436982fSEric Blake         s->target_cluster_size = BDRV_SECTOR_SIZE;
980c3cc95bdSFam Zheng     }
981a00e70c0SEmanuele Giuseppe Esposito     bdrv_graph_co_rdunlock();
9823f072a7fSMax Reitz     if (backing_filename[0] && !bdrv_backing_chain_next(target_bs) &&
983b436982fSEric Blake         s->granularity < s->target_cluster_size) {
984b436982fSEric Blake         s->buf_size = MAX(s->buf_size, s->target_cluster_size);
985b812f671SPaolo Bonzini         s->cow_bitmap = bitmap_new(length);
986b812f671SPaolo Bonzini     }
987e253f4b8SKevin Wolf     s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov);
988b812f671SPaolo Bonzini 
9897504edf4SKevin Wolf     s->buf = qemu_try_blockalign(bs, s->buf_size);
9907504edf4SKevin Wolf     if (s->buf == NULL) {
9917504edf4SKevin Wolf         ret = -ENOMEM;
9927504edf4SKevin Wolf         goto immediate_exit;
9937504edf4SKevin Wolf     }
9947504edf4SKevin Wolf 
995402a4741SPaolo Bonzini     mirror_free_init(s);
996893f7ebaSPaolo Bonzini 
99749efb1f5SDenis V. Lunev     s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
99803544a6eSFam Zheng     if (!s->is_none_mode) {
999c0b363adSDenis V. Lunev         ret = mirror_dirty_init(s);
1000daa7f2f9SKevin Wolf         if (ret < 0 || job_is_cancelled(&s->common.job)) {
10014c0cbd6fSFam Zheng             goto immediate_exit;
10024c0cbd6fSFam Zheng         }
1003893f7ebaSPaolo Bonzini     }
1004893f7ebaSPaolo Bonzini 
100532125b14SKevin Wolf     /*
100632125b14SKevin Wolf      * Only now the job is fully initialised and mirror_top_bs should start
100732125b14SKevin Wolf      * accessing it.
100832125b14SKevin Wolf      */
100932125b14SKevin Wolf     mirror_top_opaque->job = s;
101032125b14SKevin Wolf 
1011dc162c8eSFam Zheng     assert(!s->dbi);
1012715a74d8SEric Blake     s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap);
1013893f7ebaSPaolo Bonzini     for (;;) {
1014cc8c9d6cSPaolo Bonzini         uint64_t delay_ns = 0;
101549efb1f5SDenis V. Lunev         int64_t cnt, delta;
1016893f7ebaSPaolo Bonzini         bool should_complete;
1017893f7ebaSPaolo Bonzini 
1018bd48bde8SPaolo Bonzini         if (s->ret < 0) {
1019bd48bde8SPaolo Bonzini             ret = s->ret;
1020893f7ebaSPaolo Bonzini             goto immediate_exit;
1021893f7ebaSPaolo Bonzini         }
1022bd48bde8SPaolo Bonzini 
1023da01ff7fSKevin Wolf         job_pause_point(&s->common.job);
1024565ac01fSStefan Hajnoczi 
10254feeec7eSHanna Reitz         if (job_is_cancelled(&s->common.job)) {
10264feeec7eSHanna Reitz             ret = 0;
10274feeec7eSHanna Reitz             goto immediate_exit;
10284feeec7eSHanna Reitz         }
10294feeec7eSHanna Reitz 
103020dca810SJohn Snow         cnt = bdrv_get_dirty_count(s->dirty_bitmap);
103105df8a6aSKevin Wolf         /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is
103205df8a6aSKevin Wolf          * the number of bytes currently being processed; together those are
103305df8a6aSKevin Wolf          * the current remaining operation length */
1034d69a879bSHanna Reitz         job_progress_set_remaining(&s->common.job,
1035d69a879bSHanna Reitz                                    s->bytes_in_flight + cnt +
1036d69a879bSHanna Reitz                                    s->active_write_bytes_in_flight);
1037bd48bde8SPaolo Bonzini 
1038bd48bde8SPaolo Bonzini         /* Note that even when no rate limit is applied we need to yield
1039a7282330SFam Zheng          * periodically with no pending I/O so that bdrv_drain_all() returns.
104018bb6928SKevin Wolf          * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is
104118bb6928SKevin Wolf          * an error, or when the source is clean, whichever comes first. */
104249efb1f5SDenis V. Lunev         delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns;
1043d59cb66dSEmanuele Giuseppe Esposito         WITH_JOB_LOCK_GUARD() {
1044d59cb66dSEmanuele Giuseppe Esposito             iostatus = s->common.iostatus;
1045d59cb66dSEmanuele Giuseppe Esposito         }
104618bb6928SKevin Wolf         if (delta < BLOCK_JOB_SLICE_TIME &&
1047d59cb66dSEmanuele Giuseppe Esposito             iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
1048cf56a3c6SDenis V. Lunev             if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 ||
1049402a4741SPaolo Bonzini                 (cnt == 0 && s->in_flight > 0)) {
10509a46dba7SEric Blake                 trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight);
10519178f4feSKevin Wolf                 mirror_wait_for_free_in_flight_slot(s);
1052bd48bde8SPaolo Bonzini                 continue;
1053bd48bde8SPaolo Bonzini             } else if (cnt != 0) {
1054cc8c9d6cSPaolo Bonzini                 delay_ns = mirror_iteration(s);
1055893f7ebaSPaolo Bonzini             }
1056cc8c9d6cSPaolo Bonzini         }
1057893f7ebaSPaolo Bonzini 
1058893f7ebaSPaolo Bonzini         should_complete = false;
1059bd48bde8SPaolo Bonzini         if (s->in_flight == 0 && cnt == 0) {
1060893f7ebaSPaolo Bonzini             trace_mirror_before_flush(s);
106144716224SHanna Reitz             if (!job_is_ready(&s->common.job)) {
1062bdffb31dSPaolo Bonzini                 if (mirror_flush(s) < 0) {
1063bdffb31dSPaolo Bonzini                     /* Go check s->ret.  */
1064bdffb31dSPaolo Bonzini                     continue;
1065893f7ebaSPaolo Bonzini                 }
1066893f7ebaSPaolo Bonzini                 /* We're out of the streaming phase.  From now on, if the job
1067893f7ebaSPaolo Bonzini                  * is cancelled we will actually complete all pending I/O and
1068893f7ebaSPaolo Bonzini                  * report completion.  This way, block-job-cancel will leave
1069893f7ebaSPaolo Bonzini                  * the target in a consistent state.
1070893f7ebaSPaolo Bonzini                  */
10712e1795b5SKevin Wolf                 job_transition_to_ready(&s->common.job);
1072d06107adSMax Reitz                 if (s->copy_mode != MIRROR_COPY_MODE_BACKGROUND) {
1073d06107adSMax Reitz                     s->actively_synced = true;
1074d06107adSMax Reitz                 }
1075d63ffd87SPaolo Bonzini             }
1076d63ffd87SPaolo Bonzini 
1077d63ffd87SPaolo Bonzini             should_complete = s->should_complete ||
107808b83bffSHanna Reitz                 job_cancel_requested(&s->common.job);
107920dca810SJohn Snow             cnt = bdrv_get_dirty_count(s->dirty_bitmap);
1080893f7ebaSPaolo Bonzini         }
1081893f7ebaSPaolo Bonzini 
1082893f7ebaSPaolo Bonzini         if (cnt == 0 && should_complete) {
1083893f7ebaSPaolo Bonzini             /* The dirty bitmap is not updated while operations are pending.
1084893f7ebaSPaolo Bonzini              * If we're about to exit, wait for pending operations before
1085893f7ebaSPaolo Bonzini              * calling bdrv_get_dirty_count(bs), or we may exit while the
1086893f7ebaSPaolo Bonzini              * source has dirty data to copy!
1087893f7ebaSPaolo Bonzini              *
1088893f7ebaSPaolo Bonzini              * Note that I/O can be submitted by the guest while
10899a0cec66SPaolo Bonzini              * mirror_populate runs, so pause it now.  Before deciding
10909a0cec66SPaolo Bonzini              * whether to switch to target check one last time if I/O has
10919a0cec66SPaolo Bonzini              * come in the meanwhile, and if not flush the data to disk.
1092893f7ebaSPaolo Bonzini              */
10939a46dba7SEric Blake             trace_mirror_before_drain(s, cnt);
10949a0cec66SPaolo Bonzini 
10955e771752SSergio Lopez             s->in_drain = true;
10969a0cec66SPaolo Bonzini             bdrv_drained_begin(bs);
1097d69a879bSHanna Reitz 
1098d69a879bSHanna Reitz             /* Must be zero because we are drained */
1099d69a879bSHanna Reitz             assert(s->in_active_write_counter == 0);
1100d69a879bSHanna Reitz 
110120dca810SJohn Snow             cnt = bdrv_get_dirty_count(s->dirty_bitmap);
1102bdffb31dSPaolo Bonzini             if (cnt > 0 || mirror_flush(s) < 0) {
11039a0cec66SPaolo Bonzini                 bdrv_drained_end(bs);
11045e771752SSergio Lopez                 s->in_drain = false;
11059a0cec66SPaolo Bonzini                 continue;
11069a0cec66SPaolo Bonzini             }
11079a0cec66SPaolo Bonzini 
11089a0cec66SPaolo Bonzini             /* The two disks are in sync.  Exit and report successful
11099a0cec66SPaolo Bonzini              * completion.
11109a0cec66SPaolo Bonzini              */
11119a0cec66SPaolo Bonzini             assert(QLIST_EMPTY(&bs->tracked_requests));
11129a0cec66SPaolo Bonzini             need_drain = false;
11139a0cec66SPaolo Bonzini             break;
1114893f7ebaSPaolo Bonzini         }
1115893f7ebaSPaolo Bonzini 
111644716224SHanna Reitz         if (job_is_ready(&s->common.job) && !should_complete) {
111718bb6928SKevin Wolf             delay_ns = (s->in_flight == 0 &&
111818bb6928SKevin Wolf                         cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0);
1119ddc4115eSStefan Hajnoczi         }
112044716224SHanna Reitz         trace_mirror_before_sleep(s, cnt, job_is_ready(&s->common.job),
112144716224SHanna Reitz                                   delay_ns);
11225d43e86eSKevin Wolf         job_sleep_ns(&s->common.job, delay_ns);
112349efb1f5SDenis V. Lunev         s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1124893f7ebaSPaolo Bonzini     }
1125893f7ebaSPaolo Bonzini 
1126893f7ebaSPaolo Bonzini immediate_exit:
1127bd48bde8SPaolo Bonzini     if (s->in_flight > 0) {
1128bd48bde8SPaolo Bonzini         /* We get here only if something went wrong.  Either the job failed,
1129bd48bde8SPaolo Bonzini          * or it was cancelled prematurely so that we do not guarantee that
1130bd48bde8SPaolo Bonzini          * the target is a copy of the source.
1131bd48bde8SPaolo Bonzini          */
113208b83bffSHanna Reitz         assert(ret < 0 || job_is_cancelled(&s->common.job));
11339a0cec66SPaolo Bonzini         assert(need_drain);
1134bae8196dSPaolo Bonzini         mirror_wait_for_all_io(s);
1135bd48bde8SPaolo Bonzini     }
1136bd48bde8SPaolo Bonzini 
1137bd48bde8SPaolo Bonzini     assert(s->in_flight == 0);
11387191bf31SMarkus Armbruster     qemu_vfree(s->buf);
1139b812f671SPaolo Bonzini     g_free(s->cow_bitmap);
1140402a4741SPaolo Bonzini     g_free(s->in_flight_bitmap);
1141dc162c8eSFam Zheng     bdrv_dirty_iter_free(s->dbi);
11425a7e7a0bSStefan Hajnoczi 
11439a0cec66SPaolo Bonzini     if (need_drain) {
11445e771752SSergio Lopez         s->in_drain = true;
1145e253f4b8SKevin Wolf         bdrv_drained_begin(bs);
11469a0cec66SPaolo Bonzini     }
1147f67432a2SJohn Snow 
1148f67432a2SJohn Snow     return ret;
1149893f7ebaSPaolo Bonzini }
1150893f7ebaSPaolo Bonzini 
11513453d972SKevin Wolf static void mirror_complete(Job *job, Error **errp)
1152d63ffd87SPaolo Bonzini {
11533453d972SKevin Wolf     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
1154274fcceeSMax Reitz 
115544716224SHanna Reitz     if (!job_is_ready(job)) {
11569df229c3SAlberto Garcia         error_setg(errp, "The active block job '%s' cannot be completed",
11573453d972SKevin Wolf                    job->id);
1158d63ffd87SPaolo Bonzini         return;
1159d63ffd87SPaolo Bonzini     }
1160d63ffd87SPaolo Bonzini 
116115d67298SChanglong Xie     /* block all operations on to_replace bs */
116209158f00SBenoît Canet     if (s->replaces) {
11635a7e7a0bSStefan Hajnoczi         AioContext *replace_aio_context;
11645a7e7a0bSStefan Hajnoczi 
1165e12f3784SWen Congyang         s->to_replace = bdrv_find_node(s->replaces);
116609158f00SBenoît Canet         if (!s->to_replace) {
1167e12f3784SWen Congyang             error_setg(errp, "Node name '%s' not found", s->replaces);
116809158f00SBenoît Canet             return;
116909158f00SBenoît Canet         }
117009158f00SBenoît Canet 
11715a7e7a0bSStefan Hajnoczi         replace_aio_context = bdrv_get_aio_context(s->to_replace);
11725a7e7a0bSStefan Hajnoczi         aio_context_acquire(replace_aio_context);
11735a7e7a0bSStefan Hajnoczi 
117464631f36SVladimir Sementsov-Ogievskiy         /* TODO Translate this into child freeze system. */
117509158f00SBenoît Canet         error_setg(&s->replace_blocker,
117609158f00SBenoît Canet                    "block device is in use by block-job-complete");
117709158f00SBenoît Canet         bdrv_op_block_all(s->to_replace, s->replace_blocker);
117809158f00SBenoît Canet         bdrv_ref(s->to_replace);
11795a7e7a0bSStefan Hajnoczi 
11805a7e7a0bSStefan Hajnoczi         aio_context_release(replace_aio_context);
118109158f00SBenoît Canet     }
118209158f00SBenoît Canet 
1183d63ffd87SPaolo Bonzini     s->should_complete = true;
118400769414SMax Reitz 
118500769414SMax Reitz     /* If the job is paused, it will be re-entered when it is resumed */
1186279ac06eSEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
118700769414SMax Reitz         if (!job->paused) {
1188279ac06eSEmanuele Giuseppe Esposito             job_enter_cond_locked(job, NULL);
1189279ac06eSEmanuele Giuseppe Esposito         }
1190d63ffd87SPaolo Bonzini     }
119100769414SMax Reitz }
1192d63ffd87SPaolo Bonzini 
1193537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_pause(Job *job)
1194565ac01fSStefan Hajnoczi {
1195da01ff7fSKevin Wolf     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
1196565ac01fSStefan Hajnoczi 
1197bae8196dSPaolo Bonzini     mirror_wait_for_all_io(s);
1198565ac01fSStefan Hajnoczi }
1199565ac01fSStefan Hajnoczi 
120089bd0305SKevin Wolf static bool mirror_drained_poll(BlockJob *job)
120189bd0305SKevin Wolf {
120289bd0305SKevin Wolf     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
12035e771752SSergio Lopez 
12045e771752SSergio Lopez     /* If the job isn't paused nor cancelled, we can't be sure that it won't
12055e771752SSergio Lopez      * issue more requests. We make an exception if we've reached this point
12065e771752SSergio Lopez      * from one of our own drain sections, to avoid a deadlock waiting for
12075e771752SSergio Lopez      * ourselves.
12085e771752SSergio Lopez      */
1209279ac06eSEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
1210279ac06eSEmanuele Giuseppe Esposito         if (!s->common.job.paused && !job_is_cancelled_locked(&job->job)
1211279ac06eSEmanuele Giuseppe Esposito             && !s->in_drain) {
12125e771752SSergio Lopez             return true;
12135e771752SSergio Lopez         }
1214279ac06eSEmanuele Giuseppe Esposito     }
12155e771752SSergio Lopez 
121689bd0305SKevin Wolf     return !!s->in_flight;
121789bd0305SKevin Wolf }
121889bd0305SKevin Wolf 
121973895f38SHanna Reitz static bool mirror_cancel(Job *job, bool force)
1220521ff8b7SVladimir Sementsov-Ogievskiy {
1221521ff8b7SVladimir Sementsov-Ogievskiy     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
1222521ff8b7SVladimir Sementsov-Ogievskiy     BlockDriverState *target = blk_bs(s->target);
1223521ff8b7SVladimir Sementsov-Ogievskiy 
122473895f38SHanna Reitz     /*
122573895f38SHanna Reitz      * Before the job is READY, we treat any cancellation like a
122673895f38SHanna Reitz      * force-cancellation.
122773895f38SHanna Reitz      */
122873895f38SHanna Reitz     force = force || !job_is_ready(job);
122973895f38SHanna Reitz 
123073895f38SHanna Reitz     if (force) {
1231521ff8b7SVladimir Sementsov-Ogievskiy         bdrv_cancel_in_flight(target);
1232521ff8b7SVladimir Sementsov-Ogievskiy     }
123373895f38SHanna Reitz     return force;
123473895f38SHanna Reitz }
123573895f38SHanna Reitz 
123673895f38SHanna Reitz static bool commit_active_cancel(Job *job, bool force)
123773895f38SHanna Reitz {
123873895f38SHanna Reitz     /* Same as above in mirror_cancel() */
123973895f38SHanna Reitz     return force || !job_is_ready(job);
12409c785cd7SVladimir Sementsov-Ogievskiy }
1241521ff8b7SVladimir Sementsov-Ogievskiy 
12423fc4b10aSFam Zheng static const BlockJobDriver mirror_job_driver = {
124333e9e9bdSKevin Wolf     .job_driver = {
1244893f7ebaSPaolo Bonzini         .instance_size          = sizeof(MirrorBlockJob),
12458e4c8700SKevin Wolf         .job_type               = JOB_TYPE_MIRROR,
124680fa2c75SKevin Wolf         .free                   = block_job_free,
1247b15de828SKevin Wolf         .user_resume            = block_job_user_resume,
1248f67432a2SJohn Snow         .run                    = mirror_run,
1249737efc1eSJohn Snow         .prepare                = mirror_prepare,
1250737efc1eSJohn Snow         .abort                  = mirror_abort,
1251565ac01fSStefan Hajnoczi         .pause                  = mirror_pause,
1252da01ff7fSKevin Wolf         .complete               = mirror_complete,
1253521ff8b7SVladimir Sementsov-Ogievskiy         .cancel                 = mirror_cancel,
12543453d972SKevin Wolf     },
125589bd0305SKevin Wolf     .drained_poll           = mirror_drained_poll,
1256893f7ebaSPaolo Bonzini };
1257893f7ebaSPaolo Bonzini 
125803544a6eSFam Zheng static const BlockJobDriver commit_active_job_driver = {
125933e9e9bdSKevin Wolf     .job_driver = {
126003544a6eSFam Zheng         .instance_size          = sizeof(MirrorBlockJob),
12618e4c8700SKevin Wolf         .job_type               = JOB_TYPE_COMMIT,
126280fa2c75SKevin Wolf         .free                   = block_job_free,
1263b15de828SKevin Wolf         .user_resume            = block_job_user_resume,
1264f67432a2SJohn Snow         .run                    = mirror_run,
1265737efc1eSJohn Snow         .prepare                = mirror_prepare,
1266737efc1eSJohn Snow         .abort                  = mirror_abort,
1267565ac01fSStefan Hajnoczi         .pause                  = mirror_pause,
1268da01ff7fSKevin Wolf         .complete               = mirror_complete,
126973895f38SHanna Reitz         .cancel                 = commit_active_cancel,
12703453d972SKevin Wolf     },
127189bd0305SKevin Wolf     .drained_poll           = mirror_drained_poll,
127203544a6eSFam Zheng };
127303544a6eSFam Zheng 
1274537c3d4fSStefan Hajnoczi static void coroutine_fn
1275537c3d4fSStefan Hajnoczi do_sync_target_write(MirrorBlockJob *job, MirrorMethod method,
1276d06107adSMax Reitz                      uint64_t offset, uint64_t bytes,
1277d06107adSMax Reitz                      QEMUIOVector *qiov, int flags)
1278d06107adSMax Reitz {
1279d06107adSMax Reitz     int ret;
1280dbdf699cSVladimir Sementsov-Ogievskiy     size_t qiov_offset = 0;
1281dbdf699cSVladimir Sementsov-Ogievskiy     int64_t bitmap_offset, bitmap_end;
1282d06107adSMax Reitz 
1283dbdf699cSVladimir Sementsov-Ogievskiy     if (!QEMU_IS_ALIGNED(offset, job->granularity) &&
1284dbdf699cSVladimir Sementsov-Ogievskiy         bdrv_dirty_bitmap_get(job->dirty_bitmap, offset))
1285dbdf699cSVladimir Sementsov-Ogievskiy     {
1286dbdf699cSVladimir Sementsov-Ogievskiy             /*
1287dbdf699cSVladimir Sementsov-Ogievskiy              * Dirty unaligned padding: ignore it.
1288dbdf699cSVladimir Sementsov-Ogievskiy              *
1289dbdf699cSVladimir Sementsov-Ogievskiy              * Reasoning:
1290dbdf699cSVladimir Sementsov-Ogievskiy              * 1. If we copy it, we can't reset corresponding bit in
1291dbdf699cSVladimir Sementsov-Ogievskiy              *    dirty_bitmap as there may be some "dirty" bytes still not
1292dbdf699cSVladimir Sementsov-Ogievskiy              *    copied.
1293dbdf699cSVladimir Sementsov-Ogievskiy              * 2. It's already dirty, so skipping it we don't diverge mirror
1294dbdf699cSVladimir Sementsov-Ogievskiy              *    progress.
1295dbdf699cSVladimir Sementsov-Ogievskiy              *
1296dbdf699cSVladimir Sementsov-Ogievskiy              * Note, that because of this, guest write may have no contribution
1297dbdf699cSVladimir Sementsov-Ogievskiy              * into mirror converge, but that's not bad, as we have background
1298dbdf699cSVladimir Sementsov-Ogievskiy              * process of mirroring. If under some bad circumstances (high guest
1299dbdf699cSVladimir Sementsov-Ogievskiy              * IO load) background process starve, we will not converge anyway,
1300dbdf699cSVladimir Sementsov-Ogievskiy              * even if each write will contribute, as guest is not guaranteed to
1301dbdf699cSVladimir Sementsov-Ogievskiy              * rewrite the whole disk.
1302dbdf699cSVladimir Sementsov-Ogievskiy              */
1303dbdf699cSVladimir Sementsov-Ogievskiy             qiov_offset = QEMU_ALIGN_UP(offset, job->granularity) - offset;
1304dbdf699cSVladimir Sementsov-Ogievskiy             if (bytes <= qiov_offset) {
1305dbdf699cSVladimir Sementsov-Ogievskiy                 /* nothing to do after shrink */
1306dbdf699cSVladimir Sementsov-Ogievskiy                 return;
1307dbdf699cSVladimir Sementsov-Ogievskiy             }
1308dbdf699cSVladimir Sementsov-Ogievskiy             offset += qiov_offset;
1309dbdf699cSVladimir Sementsov-Ogievskiy             bytes -= qiov_offset;
1310dbdf699cSVladimir Sementsov-Ogievskiy     }
1311dbdf699cSVladimir Sementsov-Ogievskiy 
1312dbdf699cSVladimir Sementsov-Ogievskiy     if (!QEMU_IS_ALIGNED(offset + bytes, job->granularity) &&
1313dbdf699cSVladimir Sementsov-Ogievskiy         bdrv_dirty_bitmap_get(job->dirty_bitmap, offset + bytes - 1))
1314dbdf699cSVladimir Sementsov-Ogievskiy     {
1315dbdf699cSVladimir Sementsov-Ogievskiy         uint64_t tail = (offset + bytes) % job->granularity;
1316dbdf699cSVladimir Sementsov-Ogievskiy 
1317dbdf699cSVladimir Sementsov-Ogievskiy         if (bytes <= tail) {
1318dbdf699cSVladimir Sementsov-Ogievskiy             /* nothing to do after shrink */
1319dbdf699cSVladimir Sementsov-Ogievskiy             return;
1320dbdf699cSVladimir Sementsov-Ogievskiy         }
1321dbdf699cSVladimir Sementsov-Ogievskiy         bytes -= tail;
1322dbdf699cSVladimir Sementsov-Ogievskiy     }
1323dbdf699cSVladimir Sementsov-Ogievskiy 
1324dbdf699cSVladimir Sementsov-Ogievskiy     /*
1325dbdf699cSVladimir Sementsov-Ogievskiy      * Tails are either clean or shrunk, so for bitmap resetting
1326dbdf699cSVladimir Sementsov-Ogievskiy      * we safely align the range down.
1327dbdf699cSVladimir Sementsov-Ogievskiy      */
1328dbdf699cSVladimir Sementsov-Ogievskiy     bitmap_offset = QEMU_ALIGN_UP(offset, job->granularity);
1329dbdf699cSVladimir Sementsov-Ogievskiy     bitmap_end = QEMU_ALIGN_DOWN(offset + bytes, job->granularity);
1330dbdf699cSVladimir Sementsov-Ogievskiy     if (bitmap_offset < bitmap_end) {
1331dbdf699cSVladimir Sementsov-Ogievskiy         bdrv_reset_dirty_bitmap(job->dirty_bitmap, bitmap_offset,
1332dbdf699cSVladimir Sementsov-Ogievskiy                                 bitmap_end - bitmap_offset);
1333dbdf699cSVladimir Sementsov-Ogievskiy     }
1334d06107adSMax Reitz 
13355c511ac3SVladimir Sementsov-Ogievskiy     job_progress_increase_remaining(&job->common.job, bytes);
1336d69a879bSHanna Reitz     job->active_write_bytes_in_flight += bytes;
1337d06107adSMax Reitz 
1338d06107adSMax Reitz     switch (method) {
1339d06107adSMax Reitz     case MIRROR_METHOD_COPY:
1340dbdf699cSVladimir Sementsov-Ogievskiy         ret = blk_co_pwritev_part(job->target, offset, bytes,
1341dbdf699cSVladimir Sementsov-Ogievskiy                                   qiov, qiov_offset, flags);
1342d06107adSMax Reitz         break;
1343d06107adSMax Reitz 
1344d06107adSMax Reitz     case MIRROR_METHOD_ZERO:
1345d06107adSMax Reitz         assert(!qiov);
13465c511ac3SVladimir Sementsov-Ogievskiy         ret = blk_co_pwrite_zeroes(job->target, offset, bytes, flags);
1347d06107adSMax Reitz         break;
1348d06107adSMax Reitz 
1349d06107adSMax Reitz     case MIRROR_METHOD_DISCARD:
1350d06107adSMax Reitz         assert(!qiov);
13515c511ac3SVladimir Sementsov-Ogievskiy         ret = blk_co_pdiscard(job->target, offset, bytes);
1352d06107adSMax Reitz         break;
1353d06107adSMax Reitz 
1354d06107adSMax Reitz     default:
1355d06107adSMax Reitz         abort();
1356d06107adSMax Reitz     }
1357d06107adSMax Reitz 
1358d69a879bSHanna Reitz     job->active_write_bytes_in_flight -= bytes;
1359d06107adSMax Reitz     if (ret >= 0) {
13605c511ac3SVladimir Sementsov-Ogievskiy         job_progress_update(&job->common.job, bytes);
1361d06107adSMax Reitz     } else {
1362d06107adSMax Reitz         BlockErrorAction action;
1363d06107adSMax Reitz 
1364dbdf699cSVladimir Sementsov-Ogievskiy         /*
1365dbdf699cSVladimir Sementsov-Ogievskiy          * We failed, so we should mark dirty the whole area, aligned up.
1366dbdf699cSVladimir Sementsov-Ogievskiy          * Note that we don't care about shrunk tails if any: they were dirty
1367dbdf699cSVladimir Sementsov-Ogievskiy          * at function start, and they must be still dirty, as we've locked
1368dbdf699cSVladimir Sementsov-Ogievskiy          * the region for in-flight op.
1369dbdf699cSVladimir Sementsov-Ogievskiy          */
1370dbdf699cSVladimir Sementsov-Ogievskiy         bitmap_offset = QEMU_ALIGN_DOWN(offset, job->granularity);
1371dbdf699cSVladimir Sementsov-Ogievskiy         bitmap_end = QEMU_ALIGN_UP(offset + bytes, job->granularity);
1372dbdf699cSVladimir Sementsov-Ogievskiy         bdrv_set_dirty_bitmap(job->dirty_bitmap, bitmap_offset,
1373dbdf699cSVladimir Sementsov-Ogievskiy                               bitmap_end - bitmap_offset);
1374d06107adSMax Reitz         job->actively_synced = false;
1375d06107adSMax Reitz 
1376d06107adSMax Reitz         action = mirror_error_action(job, false, -ret);
1377d06107adSMax Reitz         if (action == BLOCK_ERROR_ACTION_REPORT) {
1378d06107adSMax Reitz             if (!job->ret) {
1379d06107adSMax Reitz                 job->ret = ret;
1380d06107adSMax Reitz             }
1381d06107adSMax Reitz         }
1382d06107adSMax Reitz     }
1383d06107adSMax Reitz }
1384d06107adSMax Reitz 
1385d06107adSMax Reitz static MirrorOp *coroutine_fn active_write_prepare(MirrorBlockJob *s,
1386d06107adSMax Reitz                                                    uint64_t offset,
1387d06107adSMax Reitz                                                    uint64_t bytes)
1388d06107adSMax Reitz {
1389d06107adSMax Reitz     MirrorOp *op;
1390d06107adSMax Reitz     uint64_t start_chunk = offset / s->granularity;
1391d06107adSMax Reitz     uint64_t end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity);
1392d06107adSMax Reitz 
1393d06107adSMax Reitz     op = g_new(MirrorOp, 1);
1394d06107adSMax Reitz     *op = (MirrorOp){
1395d06107adSMax Reitz         .s                  = s,
1396d06107adSMax Reitz         .offset             = offset,
1397d06107adSMax Reitz         .bytes              = bytes,
1398d06107adSMax Reitz         .is_active_write    = true,
1399ce8cabbdSKevin Wolf         .is_in_flight       = true,
1400ead3f1bfSVladimir Sementsov-Ogievskiy         .co                 = qemu_coroutine_self(),
1401d06107adSMax Reitz     };
1402d06107adSMax Reitz     qemu_co_queue_init(&op->waiting_requests);
1403d06107adSMax Reitz     QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next);
1404d06107adSMax Reitz 
1405d06107adSMax Reitz     s->in_active_write_counter++;
1406d06107adSMax Reitz 
1407d69a879bSHanna Reitz     /*
1408d69a879bSHanna Reitz      * Wait for concurrent requests affecting the area.  If there are already
1409d69a879bSHanna Reitz      * running requests that are copying off now-to-be stale data in the area,
1410d69a879bSHanna Reitz      * we must wait for them to finish before we begin writing fresh data to the
1411d69a879bSHanna Reitz      * target so that the write operations appear in the correct order.
1412d69a879bSHanna Reitz      * Note that background requests (see mirror_iteration()) in contrast only
1413d69a879bSHanna Reitz      * wait for conflicting requests at the start of the dirty area, and then
1414d69a879bSHanna Reitz      * (based on the in_flight_bitmap) truncate the area to copy so it will not
1415d69a879bSHanna Reitz      * conflict with any requests beyond that.  For active writes, however, we
1416d69a879bSHanna Reitz      * cannot truncate that area.  The request from our parent must be blocked
1417d69a879bSHanna Reitz      * until the area is copied in full.  Therefore, we must wait for the whole
1418d69a879bSHanna Reitz      * area to become free of concurrent requests.
1419d69a879bSHanna Reitz      */
1420d06107adSMax Reitz     mirror_wait_on_conflicts(op, s, offset, bytes);
1421d06107adSMax Reitz 
1422d06107adSMax Reitz     bitmap_set(s->in_flight_bitmap, start_chunk, end_chunk - start_chunk);
1423d06107adSMax Reitz 
1424d06107adSMax Reitz     return op;
1425d06107adSMax Reitz }
1426d06107adSMax Reitz 
14279c93652dSKevin Wolf static void coroutine_fn GRAPH_RDLOCK active_write_settle(MirrorOp *op)
1428d06107adSMax Reitz {
1429d06107adSMax Reitz     uint64_t start_chunk = op->offset / op->s->granularity;
1430d06107adSMax Reitz     uint64_t end_chunk = DIV_ROUND_UP(op->offset + op->bytes,
1431d06107adSMax Reitz                                       op->s->granularity);
1432d06107adSMax Reitz 
1433d06107adSMax Reitz     if (!--op->s->in_active_write_counter && op->s->actively_synced) {
1434d06107adSMax Reitz         BdrvChild *source = op->s->mirror_top_bs->backing;
1435d06107adSMax Reitz 
1436d06107adSMax Reitz         if (QLIST_FIRST(&source->bs->parents) == source &&
1437d06107adSMax Reitz             QLIST_NEXT(source, next_parent) == NULL)
1438d06107adSMax Reitz         {
1439d06107adSMax Reitz             /* Assert that we are back in sync once all active write
1440d06107adSMax Reitz              * operations are settled.
1441d06107adSMax Reitz              * Note that we can only assert this if the mirror node
1442d06107adSMax Reitz              * is the source node's only parent. */
1443d06107adSMax Reitz             assert(!bdrv_get_dirty_count(op->s->dirty_bitmap));
1444d06107adSMax Reitz         }
1445d06107adSMax Reitz     }
1446d06107adSMax Reitz     bitmap_clear(op->s->in_flight_bitmap, start_chunk, end_chunk - start_chunk);
1447d06107adSMax Reitz     QTAILQ_REMOVE(&op->s->ops_in_flight, op, next);
1448d06107adSMax Reitz     qemu_co_queue_restart_all(&op->waiting_requests);
1449d06107adSMax Reitz     g_free(op);
1450d06107adSMax Reitz }
1451d06107adSMax Reitz 
1452b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
1453b9b10c35SKevin Wolf bdrv_mirror_top_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1454b9b10c35SKevin Wolf                        QEMUIOVector *qiov, BdrvRequestFlags flags)
14554ef85a9cSKevin Wolf {
14564ef85a9cSKevin Wolf     return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
14574ef85a9cSKevin Wolf }
14584ef85a9cSKevin Wolf 
14599a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
14609a5a1c62SEmanuele Giuseppe Esposito bdrv_mirror_top_do_write(BlockDriverState *bs, MirrorMethod method,
14619a5a1c62SEmanuele Giuseppe Esposito                          uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
1462d06107adSMax Reitz                          int flags)
1463d06107adSMax Reitz {
1464d06107adSMax Reitz     MirrorOp *op = NULL;
1465d06107adSMax Reitz     MirrorBDSOpaque *s = bs->opaque;
1466d06107adSMax Reitz     int ret = 0;
1467da93d5c8SHanna Reitz     bool copy_to_target = false;
1468d06107adSMax Reitz 
1469da93d5c8SHanna Reitz     if (s->job) {
1470d06107adSMax Reitz         copy_to_target = s->job->ret >= 0 &&
14719b230ef9SHanna Reitz                          !job_is_cancelled(&s->job->common.job) &&
1472d06107adSMax Reitz                          s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING;
1473da93d5c8SHanna Reitz     }
1474d06107adSMax Reitz 
1475d06107adSMax Reitz     if (copy_to_target) {
1476d06107adSMax Reitz         op = active_write_prepare(s->job, offset, bytes);
1477d06107adSMax Reitz     }
1478d06107adSMax Reitz 
1479d06107adSMax Reitz     switch (method) {
1480d06107adSMax Reitz     case MIRROR_METHOD_COPY:
1481d06107adSMax Reitz         ret = bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
1482d06107adSMax Reitz         break;
1483d06107adSMax Reitz 
1484d06107adSMax Reitz     case MIRROR_METHOD_ZERO:
1485d06107adSMax Reitz         ret = bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
1486d06107adSMax Reitz         break;
1487d06107adSMax Reitz 
1488d06107adSMax Reitz     case MIRROR_METHOD_DISCARD:
14890b9fd3f4SFam Zheng         ret = bdrv_co_pdiscard(bs->backing, offset, bytes);
1490d06107adSMax Reitz         break;
1491d06107adSMax Reitz 
1492d06107adSMax Reitz     default:
1493d06107adSMax Reitz         abort();
1494d06107adSMax Reitz     }
1495d06107adSMax Reitz 
1496d06107adSMax Reitz     if (ret < 0) {
1497d06107adSMax Reitz         goto out;
1498d06107adSMax Reitz     }
1499d06107adSMax Reitz 
1500d06107adSMax Reitz     if (copy_to_target) {
1501d06107adSMax Reitz         do_sync_target_write(s->job, method, offset, bytes, qiov, flags);
1502d06107adSMax Reitz     }
1503d06107adSMax Reitz 
1504d06107adSMax Reitz out:
1505d06107adSMax Reitz     if (copy_to_target) {
1506d06107adSMax Reitz         active_write_settle(op);
1507d06107adSMax Reitz     }
1508d06107adSMax Reitz     return ret;
1509d06107adSMax Reitz }
1510d06107adSMax Reitz 
1511b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
1512b9b10c35SKevin Wolf bdrv_mirror_top_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
1513b9b10c35SKevin Wolf                         QEMUIOVector *qiov, BdrvRequestFlags flags)
15144ef85a9cSKevin Wolf {
1515d06107adSMax Reitz     MirrorBDSOpaque *s = bs->opaque;
1516d06107adSMax Reitz     QEMUIOVector bounce_qiov;
1517d06107adSMax Reitz     void *bounce_buf;
1518d06107adSMax Reitz     int ret = 0;
1519da93d5c8SHanna Reitz     bool copy_to_target = false;
1520d06107adSMax Reitz 
1521da93d5c8SHanna Reitz     if (s->job) {
1522d06107adSMax Reitz         copy_to_target = s->job->ret >= 0 &&
15239b230ef9SHanna Reitz                          !job_is_cancelled(&s->job->common.job) &&
1524d06107adSMax Reitz                          s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING;
1525da93d5c8SHanna Reitz     }
1526d06107adSMax Reitz 
1527d06107adSMax Reitz     if (copy_to_target) {
1528d06107adSMax Reitz         /* The guest might concurrently modify the data to write; but
1529d06107adSMax Reitz          * the data on source and destination must match, so we have
1530d06107adSMax Reitz          * to use a bounce buffer if we are going to write to the
1531d06107adSMax Reitz          * target now. */
1532d06107adSMax Reitz         bounce_buf = qemu_blockalign(bs, bytes);
1533d06107adSMax Reitz         iov_to_buf_full(qiov->iov, qiov->niov, 0, bounce_buf, bytes);
1534d06107adSMax Reitz 
1535d06107adSMax Reitz         qemu_iovec_init(&bounce_qiov, 1);
1536d06107adSMax Reitz         qemu_iovec_add(&bounce_qiov, bounce_buf, bytes);
1537d06107adSMax Reitz         qiov = &bounce_qiov;
1538e8b65355SStefan Hajnoczi 
1539e8b65355SStefan Hajnoczi         flags &= ~BDRV_REQ_REGISTERED_BUF;
1540d06107adSMax Reitz     }
1541d06107adSMax Reitz 
1542d06107adSMax Reitz     ret = bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, offset, bytes, qiov,
1543d06107adSMax Reitz                                    flags);
1544d06107adSMax Reitz 
1545d06107adSMax Reitz     if (copy_to_target) {
1546d06107adSMax Reitz         qemu_iovec_destroy(&bounce_qiov);
1547d06107adSMax Reitz         qemu_vfree(bounce_buf);
1548d06107adSMax Reitz     }
1549d06107adSMax Reitz 
1550d06107adSMax Reitz     return ret;
15514ef85a9cSKevin Wolf }
15524ef85a9cSKevin Wolf 
155388095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK bdrv_mirror_top_flush(BlockDriverState *bs)
15544ef85a9cSKevin Wolf {
1555ce960aa9SVladimir Sementsov-Ogievskiy     if (bs->backing == NULL) {
1556ce960aa9SVladimir Sementsov-Ogievskiy         /* we can be here after failed bdrv_append in mirror_start_job */
1557ce960aa9SVladimir Sementsov-Ogievskiy         return 0;
1558ce960aa9SVladimir Sementsov-Ogievskiy     }
15594ef85a9cSKevin Wolf     return bdrv_co_flush(bs->backing->bs);
15604ef85a9cSKevin Wolf }
15614ef85a9cSKevin Wolf 
1562abaf8b75SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
1563abaf8b75SKevin Wolf bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1564abaf8b75SKevin Wolf                               int64_t bytes, BdrvRequestFlags flags)
15654ef85a9cSKevin Wolf {
1566d06107adSMax Reitz     return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_ZERO, offset, bytes, NULL,
1567d06107adSMax Reitz                                     flags);
15684ef85a9cSKevin Wolf }
15694ef85a9cSKevin Wolf 
15709a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
15719a5a1c62SEmanuele Giuseppe Esposito bdrv_mirror_top_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
15724ef85a9cSKevin Wolf {
1573d06107adSMax Reitz     return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_DISCARD, offset, bytes,
1574d06107adSMax Reitz                                     NULL, 0);
15754ef85a9cSKevin Wolf }
15764ef85a9cSKevin Wolf 
1577998b3a1eSMax Reitz static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs)
1578fd4a6493SKevin Wolf {
157918775ff3SVladimir Sementsov-Ogievskiy     if (bs->backing == NULL) {
158018775ff3SVladimir Sementsov-Ogievskiy         /* we can be here after failed bdrv_attach_child in
158118775ff3SVladimir Sementsov-Ogievskiy          * bdrv_set_backing_hd */
158218775ff3SVladimir Sementsov-Ogievskiy         return;
158318775ff3SVladimir Sementsov-Ogievskiy     }
1584fd4a6493SKevin Wolf     pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
1585fd4a6493SKevin Wolf             bs->backing->bs->filename);
1586fd4a6493SKevin Wolf }
1587fd4a6493SKevin Wolf 
15884ef85a9cSKevin Wolf static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c,
1589bf8e925eSMax Reitz                                        BdrvChildRole role,
1590e0995dc3SKevin Wolf                                        BlockReopenQueue *reopen_queue,
15914ef85a9cSKevin Wolf                                        uint64_t perm, uint64_t shared,
15924ef85a9cSKevin Wolf                                        uint64_t *nperm, uint64_t *nshared)
15934ef85a9cSKevin Wolf {
1594f94dc3b4SMax Reitz     MirrorBDSOpaque *s = bs->opaque;
1595f94dc3b4SMax Reitz 
1596f94dc3b4SMax Reitz     if (s->stop) {
1597f94dc3b4SMax Reitz         /*
1598f94dc3b4SMax Reitz          * If the job is to be stopped, we do not need to forward
1599f94dc3b4SMax Reitz          * anything to the real image.
1600f94dc3b4SMax Reitz          */
1601f94dc3b4SMax Reitz         *nperm = 0;
1602f94dc3b4SMax Reitz         *nshared = BLK_PERM_ALL;
1603f94dc3b4SMax Reitz         return;
1604f94dc3b4SMax Reitz     }
1605f94dc3b4SMax Reitz 
160653431b90SMax Reitz     bdrv_default_perms(bs, c, role, reopen_queue,
160753431b90SMax Reitz                        perm, shared, nperm, nshared);
16084ef85a9cSKevin Wolf 
160953431b90SMax Reitz     if (s->is_commit) {
161053431b90SMax Reitz         /*
161153431b90SMax Reitz          * For commit jobs, we cannot take CONSISTENT_READ, because
161253431b90SMax Reitz          * that permission is unshared for everything above the base
161353431b90SMax Reitz          * node (except for filters on the base node).
161453431b90SMax Reitz          * We also have to force-share the WRITE permission, or
161553431b90SMax Reitz          * otherwise we would block ourselves at the base node (if
161653431b90SMax Reitz          * writes are blocked for a node, they are also blocked for
161753431b90SMax Reitz          * its backing file).
161853431b90SMax Reitz          * (We could also share RESIZE, because it may be needed for
161953431b90SMax Reitz          * the target if its size is less than the top node's; but
162053431b90SMax Reitz          * bdrv_default_perms_for_cow() automatically shares RESIZE
162153431b90SMax Reitz          * for backing nodes if WRITE is shared, so there is no need
162253431b90SMax Reitz          * to do it here.)
162353431b90SMax Reitz          */
162453431b90SMax Reitz         *nperm &= ~BLK_PERM_CONSISTENT_READ;
162553431b90SMax Reitz         *nshared |= BLK_PERM_WRITE;
162653431b90SMax Reitz     }
16274ef85a9cSKevin Wolf }
16284ef85a9cSKevin Wolf 
16294ef85a9cSKevin Wolf /* Dummy node that provides consistent read to its users without requiring it
16304ef85a9cSKevin Wolf  * from its backing file and that allows writes on the backing file chain. */
16314ef85a9cSKevin Wolf static BlockDriver bdrv_mirror_top = {
16324ef85a9cSKevin Wolf     .format_name                = "mirror_top",
16334ef85a9cSKevin Wolf     .bdrv_co_preadv             = bdrv_mirror_top_preadv,
16344ef85a9cSKevin Wolf     .bdrv_co_pwritev            = bdrv_mirror_top_pwritev,
16354ef85a9cSKevin Wolf     .bdrv_co_pwrite_zeroes      = bdrv_mirror_top_pwrite_zeroes,
16364ef85a9cSKevin Wolf     .bdrv_co_pdiscard           = bdrv_mirror_top_pdiscard,
16374ef85a9cSKevin Wolf     .bdrv_co_flush              = bdrv_mirror_top_flush,
1638fd4a6493SKevin Wolf     .bdrv_refresh_filename      = bdrv_mirror_top_refresh_filename,
16394ef85a9cSKevin Wolf     .bdrv_child_perm            = bdrv_mirror_top_child_perm,
16406540fd15SMax Reitz 
16416540fd15SMax Reitz     .is_filter                  = true,
1642046fd84fSVladimir Sementsov-Ogievskiy     .filtered_child_is_backing  = true,
16434ef85a9cSKevin Wolf };
16444ef85a9cSKevin Wolf 
1645cc19f177SVladimir Sementsov-Ogievskiy static BlockJob *mirror_start_job(
1646cc19f177SVladimir Sementsov-Ogievskiy                              const char *job_id, BlockDriverState *bs,
164747970dfbSJohn Snow                              int creation_flags, BlockDriverState *target,
164847970dfbSJohn Snow                              const char *replaces, int64_t speed,
164947970dfbSJohn Snow                              uint32_t granularity, int64_t buf_size,
1650274fcceeSMax Reitz                              BlockMirrorBackingMode backing_mode,
1651cdf3bc93SMax Reitz                              bool zero_target,
165203544a6eSFam Zheng                              BlockdevOnError on_source_error,
1653b952b558SPaolo Bonzini                              BlockdevOnError on_target_error,
16540fc9f8eaSFam Zheng                              bool unmap,
1655097310b5SMarkus Armbruster                              BlockCompletionFunc *cb,
165651ccfa2dSFam Zheng                              void *opaque,
165703544a6eSFam Zheng                              const BlockJobDriver *driver,
1658b49f7eadSWen Congyang                              bool is_none_mode, BlockDriverState *base,
165951ccfa2dSFam Zheng                              bool auto_complete, const char *filter_node_name,
1660481debaaSMax Reitz                              bool is_mirror, MirrorCopyMode copy_mode,
166151ccfa2dSFam Zheng                              Error **errp)
1662893f7ebaSPaolo Bonzini {
1663893f7ebaSPaolo Bonzini     MirrorBlockJob *s;
1664429076e8SMax Reitz     MirrorBDSOpaque *bs_opaque;
16654ef85a9cSKevin Wolf     BlockDriverState *mirror_top_bs;
16664ef85a9cSKevin Wolf     bool target_is_backing;
16673f072a7fSMax Reitz     uint64_t target_perms, target_shared_perms;
1668d7086422SKevin Wolf     int ret;
1669893f7ebaSPaolo Bonzini 
1670eee13dfeSPaolo Bonzini     if (granularity == 0) {
1671341ebc2fSJohn Snow         granularity = bdrv_get_default_bitmap_granularity(target);
1672eee13dfeSPaolo Bonzini     }
1673eee13dfeSPaolo Bonzini 
167431826642SEric Blake     assert(is_power_of_2(granularity));
1675eee13dfeSPaolo Bonzini 
167648ac0a4dSWen Congyang     if (buf_size < 0) {
167748ac0a4dSWen Congyang         error_setg(errp, "Invalid parameter 'buf-size'");
1678cc19f177SVladimir Sementsov-Ogievskiy         return NULL;
167948ac0a4dSWen Congyang     }
168048ac0a4dSWen Congyang 
168148ac0a4dSWen Congyang     if (buf_size == 0) {
168248ac0a4dSWen Congyang         buf_size = DEFAULT_MIRROR_BUF_SIZE;
168348ac0a4dSWen Congyang     }
16845bc361b8SFam Zheng 
16853f072a7fSMax Reitz     if (bdrv_skip_filters(bs) == bdrv_skip_filters(target)) {
168686fae10cSKevin Wolf         error_setg(errp, "Can't mirror node into itself");
1687cc19f177SVladimir Sementsov-Ogievskiy         return NULL;
168886fae10cSKevin Wolf     }
168986fae10cSKevin Wolf 
169053431b90SMax Reitz     target_is_backing = bdrv_chain_contains(bs, target);
169153431b90SMax Reitz 
16924ef85a9cSKevin Wolf     /* In the case of active commit, add dummy driver to provide consistent
16934ef85a9cSKevin Wolf      * reads on the top, while disabling it in the intermediate nodes, and make
16944ef85a9cSKevin Wolf      * the backing chain writable. */
16956cdbceb1SKevin Wolf     mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name,
16966cdbceb1SKevin Wolf                                          BDRV_O_RDWR, errp);
16974ef85a9cSKevin Wolf     if (mirror_top_bs == NULL) {
1698cc19f177SVladimir Sementsov-Ogievskiy         return NULL;
1699893f7ebaSPaolo Bonzini     }
1700d3c8c674SKevin Wolf     if (!filter_node_name) {
1701d3c8c674SKevin Wolf         mirror_top_bs->implicit = true;
1702d3c8c674SKevin Wolf     }
1703e5182c1cSMax Reitz 
1704e5182c1cSMax Reitz     /* So that we can always drop this node */
1705e5182c1cSMax Reitz     mirror_top_bs->never_freeze = true;
1706e5182c1cSMax Reitz 
17074ef85a9cSKevin Wolf     mirror_top_bs->total_sectors = bs->total_sectors;
1708228345bfSMax Reitz     mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
170980f5c33fSKevin Wolf     mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
171080f5c33fSKevin Wolf                                           BDRV_REQ_NO_FALLBACK;
1711429076e8SMax Reitz     bs_opaque = g_new0(MirrorBDSOpaque, 1);
1712429076e8SMax Reitz     mirror_top_bs->opaque = bs_opaque;
1713893f7ebaSPaolo Bonzini 
171453431b90SMax Reitz     bs_opaque->is_commit = target_is_backing;
171553431b90SMax Reitz 
17164ef85a9cSKevin Wolf     bdrv_drained_begin(bs);
1717934aee14SVladimir Sementsov-Ogievskiy     ret = bdrv_append(mirror_top_bs, bs, errp);
17184ef85a9cSKevin Wolf     bdrv_drained_end(bs);
17194ef85a9cSKevin Wolf 
1720934aee14SVladimir Sementsov-Ogievskiy     if (ret < 0) {
1721b2c2832cSKevin Wolf         bdrv_unref(mirror_top_bs);
1722cc19f177SVladimir Sementsov-Ogievskiy         return NULL;
1723b2c2832cSKevin Wolf     }
1724b2c2832cSKevin Wolf 
17254ef85a9cSKevin Wolf     /* Make sure that the source is not resized while the job is running */
172675859b94SJohn Snow     s = block_job_create(job_id, driver, NULL, mirror_top_bs,
17274ef85a9cSKevin Wolf                          BLK_PERM_CONSISTENT_READ,
17284ef85a9cSKevin Wolf                          BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
172964631f36SVladimir Sementsov-Ogievskiy                          BLK_PERM_WRITE, speed,
17304ef85a9cSKevin Wolf                          creation_flags, cb, opaque, errp);
17314ef85a9cSKevin Wolf     if (!s) {
17324ef85a9cSKevin Wolf         goto fail;
17334ef85a9cSKevin Wolf     }
1734429076e8SMax Reitz 
17357a25fcd0SMax Reitz     /* The block job now has a reference to this node */
17367a25fcd0SMax Reitz     bdrv_unref(mirror_top_bs);
17377a25fcd0SMax Reitz 
17384ef85a9cSKevin Wolf     s->mirror_top_bs = mirror_top_bs;
17394ef85a9cSKevin Wolf 
17404ef85a9cSKevin Wolf     /* No resize for the target either; while the mirror is still running, a
17414ef85a9cSKevin Wolf      * consistent read isn't necessarily possible. We could possibly allow
17424ef85a9cSKevin Wolf      * writes and graph modifications, though it would likely defeat the
17434ef85a9cSKevin Wolf      * purpose of a mirror, so leave them blocked for now.
17444ef85a9cSKevin Wolf      *
17454ef85a9cSKevin Wolf      * In the case of active commit, things look a bit different, though,
17464ef85a9cSKevin Wolf      * because the target is an already populated backing file in active use.
17474ef85a9cSKevin Wolf      * We can allow anything except resize there.*/
17483f072a7fSMax Reitz 
17493f072a7fSMax Reitz     target_perms = BLK_PERM_WRITE;
17503f072a7fSMax Reitz     target_shared_perms = BLK_PERM_WRITE_UNCHANGED;
17513f072a7fSMax Reitz 
17523f072a7fSMax Reitz     if (target_is_backing) {
17533f072a7fSMax Reitz         int64_t bs_size, target_size;
17543f072a7fSMax Reitz         bs_size = bdrv_getlength(bs);
17553f072a7fSMax Reitz         if (bs_size < 0) {
17563f072a7fSMax Reitz             error_setg_errno(errp, -bs_size,
17573f072a7fSMax Reitz                              "Could not inquire top image size");
17583f072a7fSMax Reitz             goto fail;
17593f072a7fSMax Reitz         }
17603f072a7fSMax Reitz 
17613f072a7fSMax Reitz         target_size = bdrv_getlength(target);
17623f072a7fSMax Reitz         if (target_size < 0) {
17633f072a7fSMax Reitz             error_setg_errno(errp, -target_size,
17643f072a7fSMax Reitz                              "Could not inquire base image size");
17653f072a7fSMax Reitz             goto fail;
17663f072a7fSMax Reitz         }
17673f072a7fSMax Reitz 
17683f072a7fSMax Reitz         if (target_size < bs_size) {
17693f072a7fSMax Reitz             target_perms |= BLK_PERM_RESIZE;
17703f072a7fSMax Reitz         }
17713f072a7fSMax Reitz 
177264631f36SVladimir Sementsov-Ogievskiy         target_shared_perms |= BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE;
17733f072a7fSMax Reitz     } else if (bdrv_chain_contains(bs, bdrv_skip_filters(target))) {
17743f072a7fSMax Reitz         /*
17753f072a7fSMax Reitz          * We may want to allow this in the future, but it would
17763f072a7fSMax Reitz          * require taking some extra care.
17773f072a7fSMax Reitz          */
17783f072a7fSMax Reitz         error_setg(errp, "Cannot mirror to a filter on top of a node in the "
17793f072a7fSMax Reitz                    "source's backing chain");
17803f072a7fSMax Reitz         goto fail;
17813f072a7fSMax Reitz     }
17823f072a7fSMax Reitz 
1783d861ab3aSKevin Wolf     s->target = blk_new(s->common.job.aio_context,
17843f072a7fSMax Reitz                         target_perms, target_shared_perms);
1785d7086422SKevin Wolf     ret = blk_insert_bs(s->target, target, errp);
1786d7086422SKevin Wolf     if (ret < 0) {
17874ef85a9cSKevin Wolf         goto fail;
1788d7086422SKevin Wolf     }
1789045a2f82SFam Zheng     if (is_mirror) {
1790045a2f82SFam Zheng         /* XXX: Mirror target could be a NBD server of target QEMU in the case
1791045a2f82SFam Zheng          * of non-shared block migration. To allow migration completion, we
1792045a2f82SFam Zheng          * have to allow "inactivate" of the target BB.  When that happens, we
1793045a2f82SFam Zheng          * know the job is drained, and the vcpus are stopped, so no write
1794045a2f82SFam Zheng          * operation will be performed. Block layer already has assertions to
1795045a2f82SFam Zheng          * ensure that. */
1796045a2f82SFam Zheng         blk_set_force_allow_inactivate(s->target);
1797045a2f82SFam Zheng     }
17989ff7f0dfSKevin Wolf     blk_set_allow_aio_context_change(s->target, true);
1799cf312932SKevin Wolf     blk_set_disable_request_queuing(s->target, true);
1800e253f4b8SKevin Wolf 
180109158f00SBenoît Canet     s->replaces = g_strdup(replaces);
1802b952b558SPaolo Bonzini     s->on_source_error = on_source_error;
1803b952b558SPaolo Bonzini     s->on_target_error = on_target_error;
180403544a6eSFam Zheng     s->is_none_mode = is_none_mode;
1805274fcceeSMax Reitz     s->backing_mode = backing_mode;
1806cdf3bc93SMax Reitz     s->zero_target = zero_target;
1807481debaaSMax Reitz     s->copy_mode = copy_mode;
18085bc361b8SFam Zheng     s->base = base;
18093f072a7fSMax Reitz     s->base_overlay = bdrv_find_overlay(bs, base);
1810eee13dfeSPaolo Bonzini     s->granularity = granularity;
181148ac0a4dSWen Congyang     s->buf_size = ROUND_UP(buf_size, granularity);
18120fc9f8eaSFam Zheng     s->unmap = unmap;
1813b49f7eadSWen Congyang     if (auto_complete) {
1814b49f7eadSWen Congyang         s->should_complete = true;
1815b49f7eadSWen Congyang     }
1816b812f671SPaolo Bonzini 
18170db6e54aSFam Zheng     s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
1818b8afb520SFam Zheng     if (!s->dirty_bitmap) {
181988f9d1b3SKevin Wolf         goto fail;
1820b8afb520SFam Zheng     }
1821dbdf699cSVladimir Sementsov-Ogievskiy     if (s->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING) {
1822dbdf699cSVladimir Sementsov-Ogievskiy         bdrv_disable_dirty_bitmap(s->dirty_bitmap);
1823dbdf699cSVladimir Sementsov-Ogievskiy     }
182410f3cd15SAlberto Garcia 
182567b24427SAlberto Garcia     ret = block_job_add_bdrv(&s->common, "source", bs, 0,
182667b24427SAlberto Garcia                              BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE |
182767b24427SAlberto Garcia                              BLK_PERM_CONSISTENT_READ,
182867b24427SAlberto Garcia                              errp);
182967b24427SAlberto Garcia     if (ret < 0) {
183067b24427SAlberto Garcia         goto fail;
183167b24427SAlberto Garcia     }
183267b24427SAlberto Garcia 
18334ef85a9cSKevin Wolf     /* Required permissions are already taken with blk_new() */
183476d554e2SKevin Wolf     block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL,
183576d554e2SKevin Wolf                        &error_abort);
183676d554e2SKevin Wolf 
1837f3ede4b0SAlberto Garcia     /* In commit_active_start() all intermediate nodes disappear, so
1838f3ede4b0SAlberto Garcia      * any jobs in them must be blocked */
18394ef85a9cSKevin Wolf     if (target_is_backing) {
18403f072a7fSMax Reitz         BlockDriverState *iter, *filtered_target;
18413f072a7fSMax Reitz         uint64_t iter_shared_perms;
18423f072a7fSMax Reitz 
18433f072a7fSMax Reitz         /*
18443f072a7fSMax Reitz          * The topmost node with
18453f072a7fSMax Reitz          * bdrv_skip_filters(filtered_target) == bdrv_skip_filters(target)
18463f072a7fSMax Reitz          */
18473f072a7fSMax Reitz         filtered_target = bdrv_cow_bs(bdrv_find_overlay(bs, target));
18483f072a7fSMax Reitz 
18493f072a7fSMax Reitz         assert(bdrv_skip_filters(filtered_target) ==
18503f072a7fSMax Reitz                bdrv_skip_filters(target));
18513f072a7fSMax Reitz 
18523f072a7fSMax Reitz         /*
18533f072a7fSMax Reitz          * XXX BLK_PERM_WRITE needs to be allowed so we don't block
18544ef85a9cSKevin Wolf          * ourselves at s->base (if writes are blocked for a node, they are
18554ef85a9cSKevin Wolf          * also blocked for its backing file). The other options would be a
18563f072a7fSMax Reitz          * second filter driver above s->base (== target).
18573f072a7fSMax Reitz          */
18583f072a7fSMax Reitz         iter_shared_perms = BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE;
18593f072a7fSMax Reitz 
18603f072a7fSMax Reitz         for (iter = bdrv_filter_or_cow_bs(bs); iter != target;
18613f072a7fSMax Reitz              iter = bdrv_filter_or_cow_bs(iter))
18623f072a7fSMax Reitz         {
18633f072a7fSMax Reitz             if (iter == filtered_target) {
18643f072a7fSMax Reitz                 /*
18653f072a7fSMax Reitz                  * From here on, all nodes are filters on the base.
18663f072a7fSMax Reitz                  * This allows us to share BLK_PERM_CONSISTENT_READ.
18673f072a7fSMax Reitz                  */
18683f072a7fSMax Reitz                 iter_shared_perms |= BLK_PERM_CONSISTENT_READ;
18693f072a7fSMax Reitz             }
18703f072a7fSMax Reitz 
18714ef85a9cSKevin Wolf             ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
18723f072a7fSMax Reitz                                      iter_shared_perms, errp);
18734ef85a9cSKevin Wolf             if (ret < 0) {
18744ef85a9cSKevin Wolf                 goto fail;
18754ef85a9cSKevin Wolf             }
1876f3ede4b0SAlberto Garcia         }
1877ef53dc09SAlberto Garcia 
1878ef53dc09SAlberto Garcia         if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) {
1879ef53dc09SAlberto Garcia             goto fail;
1880ef53dc09SAlberto Garcia         }
1881f3ede4b0SAlberto Garcia     }
188210f3cd15SAlberto Garcia 
188312aa4082SMax Reitz     QTAILQ_INIT(&s->ops_in_flight);
188412aa4082SMax Reitz 
18855ccac6f1SJohn Snow     trace_mirror_start(bs, s, opaque);
1886da01ff7fSKevin Wolf     job_start(&s->common.job);
1887cc19f177SVladimir Sementsov-Ogievskiy 
1888cc19f177SVladimir Sementsov-Ogievskiy     return &s->common;
18894ef85a9cSKevin Wolf 
18904ef85a9cSKevin Wolf fail:
18914ef85a9cSKevin Wolf     if (s) {
18927a25fcd0SMax Reitz         /* Make sure this BDS does not go away until we have completed the graph
18937a25fcd0SMax Reitz          * changes below */
18947a25fcd0SMax Reitz         bdrv_ref(mirror_top_bs);
18957a25fcd0SMax Reitz 
18964ef85a9cSKevin Wolf         g_free(s->replaces);
18974ef85a9cSKevin Wolf         blk_unref(s->target);
1898429076e8SMax Reitz         bs_opaque->job = NULL;
1899e917e2cbSAlberto Garcia         if (s->dirty_bitmap) {
19005deb6cbdSVladimir Sementsov-Ogievskiy             bdrv_release_dirty_bitmap(s->dirty_bitmap);
1901e917e2cbSAlberto Garcia         }
19024ad35181SKevin Wolf         job_early_fail(&s->common.job);
19034ef85a9cSKevin Wolf     }
19044ef85a9cSKevin Wolf 
1905f94dc3b4SMax Reitz     bs_opaque->stop = true;
1906f94dc3b4SMax Reitz     bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing,
1907c1cef672SFam Zheng                              &error_abort);
19083f072a7fSMax Reitz     bdrv_replace_node(mirror_top_bs, mirror_top_bs->backing->bs, &error_abort);
19097a25fcd0SMax Reitz 
19107a25fcd0SMax Reitz     bdrv_unref(mirror_top_bs);
1911cc19f177SVladimir Sementsov-Ogievskiy 
1912cc19f177SVladimir Sementsov-Ogievskiy     return NULL;
1913893f7ebaSPaolo Bonzini }
191403544a6eSFam Zheng 
191571aa9867SAlberto Garcia void mirror_start(const char *job_id, BlockDriverState *bs,
191671aa9867SAlberto Garcia                   BlockDriverState *target, const char *replaces,
1917a1999b33SJohn Snow                   int creation_flags, int64_t speed,
1918a1999b33SJohn Snow                   uint32_t granularity, int64_t buf_size,
1919274fcceeSMax Reitz                   MirrorSyncMode mode, BlockMirrorBackingMode backing_mode,
1920cdf3bc93SMax Reitz                   bool zero_target,
1921274fcceeSMax Reitz                   BlockdevOnError on_source_error,
192203544a6eSFam Zheng                   BlockdevOnError on_target_error,
1923481debaaSMax Reitz                   bool unmap, const char *filter_node_name,
1924481debaaSMax Reitz                   MirrorCopyMode copy_mode, Error **errp)
192503544a6eSFam Zheng {
192603544a6eSFam Zheng     bool is_none_mode;
192703544a6eSFam Zheng     BlockDriverState *base;
192803544a6eSFam Zheng 
1929b4ad82aaSEmanuele Giuseppe Esposito     GLOBAL_STATE_CODE();
1930b4ad82aaSEmanuele Giuseppe Esposito 
1931c8b56501SJohn Snow     if ((mode == MIRROR_SYNC_MODE_INCREMENTAL) ||
1932c8b56501SJohn Snow         (mode == MIRROR_SYNC_MODE_BITMAP)) {
1933c8b56501SJohn Snow         error_setg(errp, "Sync mode '%s' not supported",
1934c8b56501SJohn Snow                    MirrorSyncMode_str(mode));
1935d58d8453SJohn Snow         return;
1936d58d8453SJohn Snow     }
193703544a6eSFam Zheng     is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
19383f072a7fSMax Reitz     base = mode == MIRROR_SYNC_MODE_TOP ? bdrv_backing_chain_next(bs) : NULL;
1939a1999b33SJohn Snow     mirror_start_job(job_id, bs, creation_flags, target, replaces,
1940cdf3bc93SMax Reitz                      speed, granularity, buf_size, backing_mode, zero_target,
194151ccfa2dSFam Zheng                      on_source_error, on_target_error, unmap, NULL, NULL,
19426cdbceb1SKevin Wolf                      &mirror_job_driver, is_none_mode, base, false,
1943481debaaSMax Reitz                      filter_node_name, true, copy_mode, errp);
194403544a6eSFam Zheng }
194503544a6eSFam Zheng 
1946cc19f177SVladimir Sementsov-Ogievskiy BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs,
194747970dfbSJohn Snow                               BlockDriverState *base, int creation_flags,
194847970dfbSJohn Snow                               int64_t speed, BlockdevOnError on_error,
19490db832f4SKevin Wolf                               const char *filter_node_name,
195078bbd910SFam Zheng                               BlockCompletionFunc *cb, void *opaque,
195178bbd910SFam Zheng                               bool auto_complete, Error **errp)
195203544a6eSFam Zheng {
19531ba79388SAlberto Garcia     bool base_read_only;
1954eb5becc1SVladimir Sementsov-Ogievskiy     BlockJob *job;
19554da83585SJeff Cody 
1956b4ad82aaSEmanuele Giuseppe Esposito     GLOBAL_STATE_CODE();
1957b4ad82aaSEmanuele Giuseppe Esposito 
19581ba79388SAlberto Garcia     base_read_only = bdrv_is_read_only(base);
19594da83585SJeff Cody 
19601ba79388SAlberto Garcia     if (base_read_only) {
19611ba79388SAlberto Garcia         if (bdrv_reopen_set_read_only(base, false, errp) < 0) {
1962cc19f177SVladimir Sementsov-Ogievskiy             return NULL;
196320a63d2cSFam Zheng         }
19641ba79388SAlberto Garcia     }
19654da83585SJeff Cody 
1966eb5becc1SVladimir Sementsov-Ogievskiy     job = mirror_start_job(
1967cc19f177SVladimir Sementsov-Ogievskiy                      job_id, bs, creation_flags, base, NULL, speed, 0, 0,
1968cdf3bc93SMax Reitz                      MIRROR_LEAVE_BACKING_CHAIN, false,
196951ccfa2dSFam Zheng                      on_error, on_error, true, cb, opaque,
19706cdbceb1SKevin Wolf                      &commit_active_job_driver, false, base, auto_complete,
1971481debaaSMax Reitz                      filter_node_name, false, MIRROR_COPY_MODE_BACKGROUND,
1972eb5becc1SVladimir Sementsov-Ogievskiy                      errp);
1973eb5becc1SVladimir Sementsov-Ogievskiy     if (!job) {
19744da83585SJeff Cody         goto error_restore_flags;
19754da83585SJeff Cody     }
19764da83585SJeff Cody 
1977eb5becc1SVladimir Sementsov-Ogievskiy     return job;
19784da83585SJeff Cody 
19794da83585SJeff Cody error_restore_flags:
19804da83585SJeff Cody     /* ignore error and errp for bdrv_reopen, because we want to propagate
19814da83585SJeff Cody      * the original error */
19821ba79388SAlberto Garcia     if (base_read_only) {
19831ba79388SAlberto Garcia         bdrv_reopen_set_read_only(base, true, NULL);
19841ba79388SAlberto Garcia     }
1985cc19f177SVladimir Sementsov-Ogievskiy     return NULL;
198603544a6eSFam Zheng }
1987