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