]> git.proxmox.com Git - mirror_qemu.git/blame - block/mirror.c
block: explicitly acquire aiocontext in aio callbacks that need it
[mirror_qemu.git] / block / mirror.c
CommitLineData
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"
893f7eba 15#include "trace.h"
c87621ea 16#include "block/blockjob_int.h"
737e150e 17#include "block/block_int.h"
373340b2 18#include "sysemu/block-backend.h"
da34e65c 19#include "qapi/error.h"
cc7a8ea7 20#include "qapi/qmp/qerror.h"
893f7eba 21#include "qemu/ratelimit.h"
b812f671 22#include "qemu/bitmap.h"
893f7eba 23
402a4741
PB
24#define SLICE_TIME 100000000ULL /* ns */
25#define MAX_IN_FLIGHT 16
0965a41e
VSO
26#define MAX_IO_SECTORS ((1 << 20) >> BDRV_SECTOR_BITS) /* 1 Mb */
27#define DEFAULT_MIRROR_BUF_SIZE \
28 (MAX_IN_FLIGHT * MAX_IO_SECTORS * BDRV_SECTOR_SIZE)
402a4741
PB
29
30/* The mirroring buffer is a list of granularity-sized chunks.
31 * Free chunks are organized in a list.
32 */
33typedef struct MirrorBuffer {
34 QSIMPLEQ_ENTRY(MirrorBuffer) next;
35} MirrorBuffer;
893f7eba
PB
36
37typedef struct MirrorBlockJob {
38 BlockJob common;
39 RateLimit limit;
e253f4b8 40 BlockBackend *target;
5bc361b8 41 BlockDriverState *base;
09158f00
BC
42 /* The name of the graph node to replace */
43 char *replaces;
44 /* The BDS to replace */
45 BlockDriverState *to_replace;
46 /* Used to block operations on the drive-mirror-replace target */
47 Error *replace_blocker;
03544a6e 48 bool is_none_mode;
274fccee 49 BlockMirrorBackingMode backing_mode;
b952b558 50 BlockdevOnError on_source_error, on_target_error;
d63ffd87
PB
51 bool synced;
52 bool should_complete;
eee13dfe 53 int64_t granularity;
b812f671 54 size_t buf_size;
b21c7652 55 int64_t bdev_length;
b812f671 56 unsigned long *cow_bitmap;
e4654d2d 57 BdrvDirtyBitmap *dirty_bitmap;
dc162c8e 58 BdrvDirtyBitmapIter *dbi;
893f7eba 59 uint8_t *buf;
402a4741
PB
60 QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
61 int buf_free_count;
bd48bde8 62
49efb1f5 63 uint64_t last_pause_ns;
402a4741 64 unsigned long *in_flight_bitmap;
bd48bde8 65 int in_flight;
531509ba 66 int64_t sectors_in_flight;
bd48bde8 67 int ret;
0fc9f8ea 68 bool unmap;
e424aff5 69 bool waiting_for_io;
e5b43573
FZ
70 int target_cluster_sectors;
71 int max_iov;
893f7eba
PB
72} MirrorBlockJob;
73
bd48bde8
PB
74typedef struct MirrorOp {
75 MirrorBlockJob *s;
76 QEMUIOVector qiov;
bd48bde8
PB
77 int64_t sector_num;
78 int nb_sectors;
79} MirrorOp;
80
b952b558
PB
81static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
82 int error)
83{
84 s->synced = false;
85 if (read) {
81e254dc
KW
86 return block_job_error_action(&s->common, s->on_source_error,
87 true, error);
b952b558 88 } else {
81e254dc
KW
89 return block_job_error_action(&s->common, s->on_target_error,
90 false, error);
b952b558
PB
91 }
92}
93
bd48bde8
PB
94static void mirror_iteration_done(MirrorOp *op, int ret)
95{
96 MirrorBlockJob *s = op->s;
402a4741 97 struct iovec *iov;
bd48bde8 98 int64_t chunk_num;
402a4741 99 int i, nb_chunks, sectors_per_chunk;
bd48bde8
PB
100
101 trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
102
103 s->in_flight--;
b21c7652 104 s->sectors_in_flight -= op->nb_sectors;
402a4741
PB
105 iov = op->qiov.iov;
106 for (i = 0; i < op->qiov.niov; i++) {
107 MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
108 QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
109 s->buf_free_count++;
110 }
111
bd48bde8
PB
112 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
113 chunk_num = op->sector_num / sectors_per_chunk;
4150ae60 114 nb_chunks = DIV_ROUND_UP(op->nb_sectors, sectors_per_chunk);
402a4741 115 bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
b21c7652
HR
116 if (ret >= 0) {
117 if (s->cow_bitmap) {
118 bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
119 }
120 s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE;
bd48bde8
PB
121 }
122
6df3bf8e 123 qemu_iovec_destroy(&op->qiov);
c84b3192 124 g_free(op);
7b770c72 125
e424aff5 126 if (s->waiting_for_io) {
0b8b8753 127 qemu_coroutine_enter(s->common.co);
7b770c72 128 }
bd48bde8
PB
129}
130
131static void mirror_write_complete(void *opaque, int ret)
132{
133 MirrorOp *op = opaque;
134 MirrorBlockJob *s = op->s;
b9e413dd
PB
135
136 aio_context_acquire(blk_get_aio_context(s->common.blk));
bd48bde8 137 if (ret < 0) {
bd48bde8
PB
138 BlockErrorAction action;
139
20dca810 140 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
bd48bde8 141 action = mirror_error_action(s, false, -ret);
a589569f 142 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
bd48bde8
PB
143 s->ret = ret;
144 }
145 }
146 mirror_iteration_done(op, ret);
b9e413dd 147 aio_context_release(blk_get_aio_context(s->common.blk));
bd48bde8
PB
148}
149
150static void mirror_read_complete(void *opaque, int ret)
151{
152 MirrorOp *op = opaque;
153 MirrorBlockJob *s = op->s;
b9e413dd
PB
154
155 aio_context_acquire(blk_get_aio_context(s->common.blk));
bd48bde8 156 if (ret < 0) {
bd48bde8
PB
157 BlockErrorAction action;
158
20dca810 159 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
bd48bde8 160 action = mirror_error_action(s, true, -ret);
a589569f 161 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
bd48bde8
PB
162 s->ret = ret;
163 }
164
165 mirror_iteration_done(op, ret);
b9e413dd
PB
166 } else {
167 blk_aio_pwritev(s->target, op->sector_num * BDRV_SECTOR_SIZE, &op->qiov,
168 0, mirror_write_complete, op);
bd48bde8 169 }
b9e413dd 170 aio_context_release(blk_get_aio_context(s->common.blk));
bd48bde8
PB
171}
172
4150ae60
FZ
173static inline void mirror_clip_sectors(MirrorBlockJob *s,
174 int64_t sector_num,
175 int *nb_sectors)
176{
177 *nb_sectors = MIN(*nb_sectors,
178 s->bdev_length / BDRV_SECTOR_SIZE - sector_num);
179}
180
e5b43573
FZ
181/* Round sector_num and/or nb_sectors to target cluster if COW is needed, and
182 * return the offset of the adjusted tail sector against original. */
183static int mirror_cow_align(MirrorBlockJob *s,
184 int64_t *sector_num,
185 int *nb_sectors)
893f7eba 186{
e5b43573
FZ
187 bool need_cow;
188 int ret = 0;
189 int chunk_sectors = s->granularity >> BDRV_SECTOR_BITS;
190 int64_t align_sector_num = *sector_num;
191 int align_nb_sectors = *nb_sectors;
192 int max_sectors = chunk_sectors * s->max_iov;
193
194 need_cow = !test_bit(*sector_num / chunk_sectors, s->cow_bitmap);
195 need_cow |= !test_bit((*sector_num + *nb_sectors - 1) / chunk_sectors,
196 s->cow_bitmap);
197 if (need_cow) {
244483e6
KW
198 bdrv_round_sectors_to_clusters(blk_bs(s->target), *sector_num,
199 *nb_sectors, &align_sector_num,
200 &align_nb_sectors);
e5b43573 201 }
3515727f 202
e5b43573
FZ
203 if (align_nb_sectors > max_sectors) {
204 align_nb_sectors = max_sectors;
205 if (need_cow) {
206 align_nb_sectors = QEMU_ALIGN_DOWN(align_nb_sectors,
207 s->target_cluster_sectors);
208 }
8f0720ec 209 }
4150ae60
FZ
210 /* Clipping may result in align_nb_sectors unaligned to chunk boundary, but
211 * that doesn't matter because it's already the end of source image. */
212 mirror_clip_sectors(s, align_sector_num, &align_nb_sectors);
8f0720ec 213
e5b43573
FZ
214 ret = align_sector_num + align_nb_sectors - (*sector_num + *nb_sectors);
215 *sector_num = align_sector_num;
216 *nb_sectors = align_nb_sectors;
217 assert(ret >= 0);
218 return ret;
219}
220
21cd917f
FZ
221static inline void mirror_wait_for_io(MirrorBlockJob *s)
222{
223 assert(!s->waiting_for_io);
224 s->waiting_for_io = true;
225 qemu_coroutine_yield();
226 s->waiting_for_io = false;
227}
228
e5b43573 229/* Submit async read while handling COW.
17612955
JS
230 * Returns: The number of sectors copied after and including sector_num,
231 * excluding any sectors copied prior to sector_num due to alignment.
232 * This will be nb_sectors if no alignment is necessary, or
e5b43573
FZ
233 * (new_end - sector_num) if tail is rounded up or down due to
234 * alignment or buffer limit.
235 */
236static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num,
237 int nb_sectors)
238{
e253f4b8 239 BlockBackend *source = s->common.blk;
e5b43573 240 int sectors_per_chunk, nb_chunks;
17612955 241 int ret;
e5b43573 242 MirrorOp *op;
e4808881 243 int max_sectors;
e5b43573 244
884fea4e 245 sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
e4808881 246 max_sectors = sectors_per_chunk * s->max_iov;
402a4741 247
e5b43573
FZ
248 /* We can only handle as much as buf_size at a time. */
249 nb_sectors = MIN(s->buf_size >> BDRV_SECTOR_BITS, nb_sectors);
e4808881 250 nb_sectors = MIN(max_sectors, nb_sectors);
e5b43573 251 assert(nb_sectors);
17612955 252 ret = nb_sectors;
402a4741 253
e5b43573
FZ
254 if (s->cow_bitmap) {
255 ret += mirror_cow_align(s, &sector_num, &nb_sectors);
256 }
257 assert(nb_sectors << BDRV_SECTOR_BITS <= s->buf_size);
258 /* The sector range must meet granularity because:
259 * 1) Caller passes in aligned values;
260 * 2) mirror_cow_align is used only when target cluster is larger. */
e5b43573 261 assert(!(sector_num % sectors_per_chunk));
4150ae60 262 nb_chunks = DIV_ROUND_UP(nb_sectors, sectors_per_chunk);
e5b43573
FZ
263
264 while (s->buf_free_count < nb_chunks) {
402a4741 265 trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
21cd917f 266 mirror_wait_for_io(s);
b812f671
PB
267 }
268
bd48bde8 269 /* Allocate a MirrorOp that is used as an AIO callback. */
c84b3192 270 op = g_new(MirrorOp, 1);
bd48bde8 271 op->s = s;
bd48bde8
PB
272 op->sector_num = sector_num;
273 op->nb_sectors = nb_sectors;
402a4741
PB
274
275 /* Now make a QEMUIOVector taking enough granularity-sized chunks
276 * from s->buf_free.
277 */
278 qemu_iovec_init(&op->qiov, nb_chunks);
402a4741
PB
279 while (nb_chunks-- > 0) {
280 MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
e5b43573 281 size_t remaining = nb_sectors * BDRV_SECTOR_SIZE - op->qiov.size;
5a0f6fd5 282
402a4741
PB
283 QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
284 s->buf_free_count--;
5a0f6fd5 285 qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
402a4741 286 }
bd48bde8 287
893f7eba 288 /* Copy the dirty cluster. */
bd48bde8 289 s->in_flight++;
b21c7652 290 s->sectors_in_flight += nb_sectors;
b812f671 291 trace_mirror_one_iteration(s, sector_num, nb_sectors);
dcfb3beb 292
73698c30 293 blk_aio_preadv(source, sector_num * BDRV_SECTOR_SIZE, &op->qiov, 0,
e5b43573
FZ
294 mirror_read_complete, op);
295 return ret;
296}
297
298static void mirror_do_zero_or_discard(MirrorBlockJob *s,
299 int64_t sector_num,
300 int nb_sectors,
301 bool is_discard)
302{
303 MirrorOp *op;
304
305 /* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed
306 * so the freeing in mirror_iteration_done is nop. */
307 op = g_new0(MirrorOp, 1);
308 op->s = s;
309 op->sector_num = sector_num;
310 op->nb_sectors = nb_sectors;
311
312 s->in_flight++;
313 s->sectors_in_flight += nb_sectors;
314 if (is_discard) {
1c6c4bb7
EB
315 blk_aio_pdiscard(s->target, sector_num << BDRV_SECTOR_BITS,
316 op->nb_sectors << BDRV_SECTOR_BITS,
317 mirror_write_complete, op);
e5b43573 318 } else {
e253f4b8
KW
319 blk_aio_pwrite_zeroes(s->target, sector_num * BDRV_SECTOR_SIZE,
320 op->nb_sectors * BDRV_SECTOR_SIZE,
dcfb3beb
FZ
321 s->unmap ? BDRV_REQ_MAY_UNMAP : 0,
322 mirror_write_complete, op);
e5b43573
FZ
323 }
324}
325
326static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
327{
e253f4b8 328 BlockDriverState *source = blk_bs(s->common.blk);
9c83625b 329 int64_t sector_num, first_chunk;
e5b43573
FZ
330 uint64_t delay_ns = 0;
331 /* At least the first dirty chunk is mirrored in one iteration. */
332 int nb_chunks = 1;
333 int64_t end = s->bdev_length / BDRV_SECTOR_SIZE;
334 int sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
4b5004d9 335 bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target));
0965a41e
VSO
336 int max_io_sectors = MAX((s->buf_size >> BDRV_SECTOR_BITS) / MAX_IN_FLIGHT,
337 MAX_IO_SECTORS);
e5b43573 338
dc162c8e 339 sector_num = bdrv_dirty_iter_next(s->dbi);
e5b43573 340 if (sector_num < 0) {
dc162c8e
FZ
341 bdrv_set_dirty_iter(s->dbi, 0);
342 sector_num = bdrv_dirty_iter_next(s->dbi);
e5b43573
FZ
343 trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
344 assert(sector_num >= 0);
345 }
346
9c83625b
HR
347 first_chunk = sector_num / sectors_per_chunk;
348 while (test_bit(first_chunk, s->in_flight_bitmap)) {
ff04198b 349 trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
9c83625b
HR
350 mirror_wait_for_io(s);
351 }
352
565ac01f
SH
353 block_job_pause_point(&s->common);
354
e5b43573
FZ
355 /* Find the number of consective dirty chunks following the first dirty
356 * one, and wait for in flight requests in them. */
357 while (nb_chunks * sectors_per_chunk < (s->buf_size >> BDRV_SECTOR_BITS)) {
dc162c8e 358 int64_t next_dirty;
e5b43573
FZ
359 int64_t next_sector = sector_num + nb_chunks * sectors_per_chunk;
360 int64_t next_chunk = next_sector / sectors_per_chunk;
361 if (next_sector >= end ||
362 !bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) {
363 break;
364 }
365 if (test_bit(next_chunk, s->in_flight_bitmap)) {
9c83625b 366 break;
e5b43573 367 }
9c83625b 368
dc162c8e
FZ
369 next_dirty = bdrv_dirty_iter_next(s->dbi);
370 if (next_dirty > next_sector || next_dirty < 0) {
f27a2742 371 /* The bitmap iterator's cache is stale, refresh it */
dc162c8e
FZ
372 bdrv_set_dirty_iter(s->dbi, next_sector);
373 next_dirty = bdrv_dirty_iter_next(s->dbi);
f27a2742 374 }
dc162c8e 375 assert(next_dirty == next_sector);
9c83625b 376 nb_chunks++;
e5b43573
FZ
377 }
378
379 /* Clear dirty bits before querying the block status, because
380 * calling bdrv_get_block_status_above could yield - if some blocks are
381 * marked dirty in this window, we need to know.
382 */
383 bdrv_reset_dirty_bitmap(s->dirty_bitmap, sector_num,
384 nb_chunks * sectors_per_chunk);
385 bitmap_set(s->in_flight_bitmap, sector_num / sectors_per_chunk, nb_chunks);
386 while (nb_chunks > 0 && sector_num < end) {
387 int ret;
4b5004d9 388 int io_sectors, io_sectors_acct;
e5b43573
FZ
389 BlockDriverState *file;
390 enum MirrorMethod {
391 MIRROR_METHOD_COPY,
392 MIRROR_METHOD_ZERO,
393 MIRROR_METHOD_DISCARD
394 } mirror_method = MIRROR_METHOD_COPY;
395
396 assert(!(sector_num % sectors_per_chunk));
397 ret = bdrv_get_block_status_above(source, NULL, sector_num,
398 nb_chunks * sectors_per_chunk,
399 &io_sectors, &file);
400 if (ret < 0) {
0965a41e
VSO
401 io_sectors = MIN(nb_chunks * sectors_per_chunk, max_io_sectors);
402 } else if (ret & BDRV_BLOCK_DATA) {
403 io_sectors = MIN(io_sectors, max_io_sectors);
e5b43573
FZ
404 }
405
406 io_sectors -= io_sectors % sectors_per_chunk;
407 if (io_sectors < sectors_per_chunk) {
408 io_sectors = sectors_per_chunk;
409 } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) {
410 int64_t target_sector_num;
411 int target_nb_sectors;
244483e6
KW
412 bdrv_round_sectors_to_clusters(blk_bs(s->target), sector_num,
413 io_sectors, &target_sector_num,
414 &target_nb_sectors);
e5b43573
FZ
415 if (target_sector_num == sector_num &&
416 target_nb_sectors == io_sectors) {
417 mirror_method = ret & BDRV_BLOCK_ZERO ?
418 MIRROR_METHOD_ZERO :
419 MIRROR_METHOD_DISCARD;
420 }
421 }
422
cf56a3c6
DL
423 while (s->in_flight >= MAX_IN_FLIGHT) {
424 trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
425 mirror_wait_for_io(s);
426 }
427
dbaa7b57
VSO
428 if (s->ret < 0) {
429 return 0;
430 }
431
4150ae60 432 mirror_clip_sectors(s, sector_num, &io_sectors);
e5b43573
FZ
433 switch (mirror_method) {
434 case MIRROR_METHOD_COPY:
435 io_sectors = mirror_do_read(s, sector_num, io_sectors);
4b5004d9 436 io_sectors_acct = io_sectors;
e5b43573
FZ
437 break;
438 case MIRROR_METHOD_ZERO:
e5b43573 439 case MIRROR_METHOD_DISCARD:
4b5004d9
DL
440 mirror_do_zero_or_discard(s, sector_num, io_sectors,
441 mirror_method == MIRROR_METHOD_DISCARD);
442 if (write_zeroes_ok) {
443 io_sectors_acct = 0;
444 } else {
445 io_sectors_acct = io_sectors;
446 }
e5b43573
FZ
447 break;
448 default:
449 abort();
450 }
451 assert(io_sectors);
452 sector_num += io_sectors;
4150ae60 453 nb_chunks -= DIV_ROUND_UP(io_sectors, sectors_per_chunk);
f14a39cc 454 if (s->common.speed) {
4b5004d9 455 delay_ns = ratelimit_calculate_delay(&s->limit, io_sectors_acct);
f14a39cc 456 }
dcfb3beb 457 }
cc8c9d6c 458 return delay_ns;
bd48bde8 459}
b952b558 460
402a4741
PB
461static void mirror_free_init(MirrorBlockJob *s)
462{
463 int granularity = s->granularity;
464 size_t buf_size = s->buf_size;
465 uint8_t *buf = s->buf;
466
467 assert(s->buf_free_count == 0);
468 QSIMPLEQ_INIT(&s->buf_free);
469 while (buf_size != 0) {
470 MirrorBuffer *cur = (MirrorBuffer *)buf;
471 QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
472 s->buf_free_count++;
473 buf_size -= granularity;
474 buf += granularity;
475 }
476}
477
bae8196d
PB
478/* This is also used for the .pause callback. There is no matching
479 * mirror_resume() because mirror_run() will begin iterating again
480 * when the job is resumed.
481 */
482static void mirror_wait_for_all_io(MirrorBlockJob *s)
bd48bde8
PB
483{
484 while (s->in_flight > 0) {
21cd917f 485 mirror_wait_for_io(s);
bd48bde8 486 }
893f7eba
PB
487}
488
5a7e7a0b
SH
489typedef struct {
490 int ret;
491} MirrorExitData;
492
493static void mirror_exit(BlockJob *job, void *opaque)
494{
495 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
496 MirrorExitData *data = opaque;
497 AioContext *replace_aio_context = NULL;
e253f4b8
KW
498 BlockDriverState *src = blk_bs(s->common.blk);
499 BlockDriverState *target_bs = blk_bs(s->target);
3f09bfbc
KW
500
501 /* Make sure that the source BDS doesn't go away before we called
502 * block_job_completed(). */
503 bdrv_ref(src);
5a7e7a0b
SH
504
505 if (s->to_replace) {
506 replace_aio_context = bdrv_get_aio_context(s->to_replace);
507 aio_context_acquire(replace_aio_context);
508 }
509
510 if (s->should_complete && data->ret == 0) {
e253f4b8 511 BlockDriverState *to_replace = src;
5a7e7a0b
SH
512 if (s->to_replace) {
513 to_replace = s->to_replace;
514 }
40365552 515
e253f4b8
KW
516 if (bdrv_get_flags(target_bs) != bdrv_get_flags(to_replace)) {
517 bdrv_reopen(target_bs, bdrv_get_flags(to_replace), NULL);
5a7e7a0b 518 }
b8804815
KW
519
520 /* The mirror job has no requests in flight any more, but we need to
521 * drain potential other users of the BDS before changing the graph. */
e253f4b8
KW
522 bdrv_drained_begin(target_bs);
523 bdrv_replace_in_backing_chain(to_replace, target_bs);
524 bdrv_drained_end(target_bs);
b8804815 525
b6d2e599
KW
526 /* We just changed the BDS the job BB refers to */
527 blk_remove_bs(job->blk);
528 blk_insert_bs(job->blk, src);
5a7e7a0b
SH
529 }
530 if (s->to_replace) {
531 bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
532 error_free(s->replace_blocker);
533 bdrv_unref(s->to_replace);
534 }
535 if (replace_aio_context) {
536 aio_context_release(replace_aio_context);
537 }
538 g_free(s->replaces);
e253f4b8 539 blk_unref(s->target);
bae8196d 540 s->target = NULL;
5a7e7a0b
SH
541 block_job_completed(&s->common, data->ret);
542 g_free(data);
176c3699 543 bdrv_drained_end(src);
3f09bfbc 544 bdrv_unref(src);
5a7e7a0b
SH
545}
546
49efb1f5
DL
547static void mirror_throttle(MirrorBlockJob *s)
548{
549 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
550
551 if (now - s->last_pause_ns > SLICE_TIME) {
552 s->last_pause_ns = now;
553 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, 0);
554 } else {
555 block_job_pause_point(&s->common);
556 }
557}
558
c0b363ad
DL
559static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
560{
561 int64_t sector_num, end;
562 BlockDriverState *base = s->base;
563 BlockDriverState *bs = blk_bs(s->common.blk);
564 BlockDriverState *target_bs = blk_bs(s->target);
c0b363ad
DL
565 int ret, n;
566
567 end = s->bdev_length / BDRV_SECTOR_SIZE;
568
b7d5062c 569 if (base == NULL && !bdrv_has_zero_init(target_bs)) {
c7c2769c
DL
570 if (!bdrv_can_write_zeroes_with_unmap(target_bs)) {
571 bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, end);
572 return 0;
573 }
574
575 for (sector_num = 0; sector_num < end; ) {
576 int nb_sectors = MIN(end - sector_num,
577 QEMU_ALIGN_DOWN(INT_MAX, s->granularity) >> BDRV_SECTOR_BITS);
578
579 mirror_throttle(s);
580
581 if (block_job_is_cancelled(&s->common)) {
582 return 0;
583 }
584
585 if (s->in_flight >= MAX_IN_FLIGHT) {
586 trace_mirror_yield(s, s->in_flight, s->buf_free_count, -1);
587 mirror_wait_for_io(s);
588 continue;
589 }
590
591 mirror_do_zero_or_discard(s, sector_num, nb_sectors, false);
592 sector_num += nb_sectors;
593 }
594
bae8196d 595 mirror_wait_for_all_io(s);
b7d5062c
DL
596 }
597
c0b363ad
DL
598 /* First part, loop on the sectors and initialize the dirty bitmap. */
599 for (sector_num = 0; sector_num < end; ) {
600 /* Just to make sure we are not exceeding int limit. */
601 int nb_sectors = MIN(INT_MAX >> BDRV_SECTOR_BITS,
602 end - sector_num);
603
604 mirror_throttle(s);
605
606 if (block_job_is_cancelled(&s->common)) {
607 return 0;
608 }
609
610 ret = bdrv_is_allocated_above(bs, base, sector_num, nb_sectors, &n);
611 if (ret < 0) {
612 return ret;
613 }
614
615 assert(n > 0);
b7d5062c 616 if (ret == 1) {
c0b363ad
DL
617 bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n);
618 }
619 sector_num += n;
620 }
621 return 0;
622}
623
bdffb31d
PB
624/* Called when going out of the streaming phase to flush the bulk of the
625 * data to the medium, or just before completing.
626 */
627static int mirror_flush(MirrorBlockJob *s)
628{
629 int ret = blk_flush(s->target);
630 if (ret < 0) {
631 if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) {
632 s->ret = ret;
633 }
634 }
635 return ret;
636}
637
893f7eba
PB
638static void coroutine_fn mirror_run(void *opaque)
639{
640 MirrorBlockJob *s = opaque;
5a7e7a0b 641 MirrorExitData *data;
e253f4b8
KW
642 BlockDriverState *bs = blk_bs(s->common.blk);
643 BlockDriverState *target_bs = blk_bs(s->target);
9a0cec66 644 bool need_drain = true;
c0b363ad 645 int64_t length;
b812f671 646 BlockDriverInfo bdi;
1d33936e
JC
647 char backing_filename[2]; /* we only need 2 characters because we are only
648 checking for a NULL string */
893f7eba 649 int ret = 0;
e5b43573 650 int target_cluster_size = BDRV_SECTOR_SIZE;
893f7eba
PB
651
652 if (block_job_is_cancelled(&s->common)) {
653 goto immediate_exit;
654 }
655
b21c7652
HR
656 s->bdev_length = bdrv_getlength(bs);
657 if (s->bdev_length < 0) {
658 ret = s->bdev_length;
373df5b1 659 goto immediate_exit;
b21c7652 660 } else if (s->bdev_length == 0) {
9e48b025
FZ
661 /* Report BLOCK_JOB_READY and wait for complete. */
662 block_job_event_ready(&s->common);
663 s->synced = true;
664 while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
665 block_job_yield(&s->common);
666 }
667 s->common.cancelled = false;
668 goto immediate_exit;
893f7eba
PB
669 }
670
b21c7652 671 length = DIV_ROUND_UP(s->bdev_length, s->granularity);
402a4741
PB
672 s->in_flight_bitmap = bitmap_new(length);
673
b812f671
PB
674 /* If we have no backing file yet in the destination, we cannot let
675 * the destination do COW. Instead, we copy sectors around the
676 * dirty data if needed. We need a bitmap to do that.
677 */
e253f4b8 678 bdrv_get_backing_filename(target_bs, backing_filename,
b812f671 679 sizeof(backing_filename));
e253f4b8 680 if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) {
e5b43573
FZ
681 target_cluster_size = bdi.cluster_size;
682 }
e253f4b8 683 if (backing_filename[0] && !target_bs->backing
e5b43573
FZ
684 && s->granularity < target_cluster_size) {
685 s->buf_size = MAX(s->buf_size, target_cluster_size);
686 s->cow_bitmap = bitmap_new(length);
b812f671 687 }
e5b43573 688 s->target_cluster_sectors = target_cluster_size >> BDRV_SECTOR_BITS;
e253f4b8 689 s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov);
b812f671 690
7504edf4
KW
691 s->buf = qemu_try_blockalign(bs, s->buf_size);
692 if (s->buf == NULL) {
693 ret = -ENOMEM;
694 goto immediate_exit;
695 }
696
402a4741 697 mirror_free_init(s);
893f7eba 698
49efb1f5 699 s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
03544a6e 700 if (!s->is_none_mode) {
c0b363ad
DL
701 ret = mirror_dirty_init(s);
702 if (ret < 0 || block_job_is_cancelled(&s->common)) {
703 goto immediate_exit;
893f7eba
PB
704 }
705 }
706
dc162c8e
FZ
707 assert(!s->dbi);
708 s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap, 0);
893f7eba 709 for (;;) {
cc8c9d6c 710 uint64_t delay_ns = 0;
49efb1f5 711 int64_t cnt, delta;
893f7eba
PB
712 bool should_complete;
713
bd48bde8
PB
714 if (s->ret < 0) {
715 ret = s->ret;
716 goto immediate_exit;
717 }
718
565ac01f
SH
719 block_job_pause_point(&s->common);
720
20dca810 721 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
b21c7652
HR
722 /* s->common.offset contains the number of bytes already processed so
723 * far, cnt is the number of dirty sectors remaining and
724 * s->sectors_in_flight is the number of sectors currently being
725 * processed; together those are the current total operation length */
726 s->common.len = s->common.offset +
727 (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE;
bd48bde8
PB
728
729 /* Note that even when no rate limit is applied we need to yield
a7282330 730 * periodically with no pending I/O so that bdrv_drain_all() returns.
bd48bde8
PB
731 * We do so every SLICE_TIME nanoseconds, or when there is an error,
732 * or when the source is clean, whichever comes first.
733 */
49efb1f5
DL
734 delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns;
735 if (delta < SLICE_TIME &&
bd48bde8 736 s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
cf56a3c6 737 if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 ||
402a4741
PB
738 (cnt == 0 && s->in_flight > 0)) {
739 trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt);
21cd917f 740 mirror_wait_for_io(s);
bd48bde8
PB
741 continue;
742 } else if (cnt != 0) {
cc8c9d6c 743 delay_ns = mirror_iteration(s);
893f7eba 744 }
893f7eba
PB
745 }
746
747 should_complete = false;
bd48bde8 748 if (s->in_flight == 0 && cnt == 0) {
893f7eba 749 trace_mirror_before_flush(s);
bdffb31d
PB
750 if (!s->synced) {
751 if (mirror_flush(s) < 0) {
752 /* Go check s->ret. */
753 continue;
b952b558 754 }
b952b558
PB
755 /* We're out of the streaming phase. From now on, if the job
756 * is cancelled we will actually complete all pending I/O and
757 * report completion. This way, block-job-cancel will leave
758 * the target in a consistent state.
759 */
bdffb31d
PB
760 block_job_event_ready(&s->common);
761 s->synced = true;
d63ffd87 762 }
bdffb31d
PB
763
764 should_complete = s->should_complete ||
765 block_job_is_cancelled(&s->common);
766 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
893f7eba
PB
767 }
768
769 if (cnt == 0 && should_complete) {
770 /* The dirty bitmap is not updated while operations are pending.
771 * If we're about to exit, wait for pending operations before
772 * calling bdrv_get_dirty_count(bs), or we may exit while the
773 * source has dirty data to copy!
774 *
775 * Note that I/O can be submitted by the guest while
9a0cec66
PB
776 * mirror_populate runs, so pause it now. Before deciding
777 * whether to switch to target check one last time if I/O has
778 * come in the meanwhile, and if not flush the data to disk.
893f7eba
PB
779 */
780 trace_mirror_before_drain(s, cnt);
9a0cec66
PB
781
782 bdrv_drained_begin(bs);
20dca810 783 cnt = bdrv_get_dirty_count(s->dirty_bitmap);
bdffb31d 784 if (cnt > 0 || mirror_flush(s) < 0) {
9a0cec66
PB
785 bdrv_drained_end(bs);
786 continue;
787 }
788
789 /* The two disks are in sync. Exit and report successful
790 * completion.
791 */
792 assert(QLIST_EMPTY(&bs->tracked_requests));
793 s->common.cancelled = false;
794 need_drain = false;
795 break;
893f7eba
PB
796 }
797
798 ret = 0;
cc8c9d6c 799 trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
d63ffd87 800 if (!s->synced) {
7483d1e5 801 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
893f7eba
PB
802 if (block_job_is_cancelled(&s->common)) {
803 break;
804 }
805 } else if (!should_complete) {
bd48bde8 806 delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
7483d1e5 807 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
893f7eba 808 }
49efb1f5 809 s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
893f7eba
PB
810 }
811
812immediate_exit:
bd48bde8
PB
813 if (s->in_flight > 0) {
814 /* We get here only if something went wrong. Either the job failed,
815 * or it was cancelled prematurely so that we do not guarantee that
816 * the target is a copy of the source.
817 */
818 assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
9a0cec66 819 assert(need_drain);
bae8196d 820 mirror_wait_for_all_io(s);
bd48bde8
PB
821 }
822
823 assert(s->in_flight == 0);
7191bf31 824 qemu_vfree(s->buf);
b812f671 825 g_free(s->cow_bitmap);
402a4741 826 g_free(s->in_flight_bitmap);
dc162c8e 827 bdrv_dirty_iter_free(s->dbi);
e4654d2d 828 bdrv_release_dirty_bitmap(bs, s->dirty_bitmap);
5a7e7a0b
SH
829
830 data = g_malloc(sizeof(*data));
831 data->ret = ret;
9a0cec66
PB
832
833 if (need_drain) {
834 bdrv_drained_begin(bs);
835 }
5a7e7a0b 836 block_job_defer_to_main_loop(&s->common, mirror_exit, data);
893f7eba
PB
837}
838
839static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
840{
841 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
842
843 if (speed < 0) {
c6bd8c70 844 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
893f7eba
PB
845 return;
846 }
847 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
848}
849
d63ffd87
PB
850static void mirror_complete(BlockJob *job, Error **errp)
851{
852 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
274fccee
HR
853 BlockDriverState *src, *target;
854
855 src = blk_bs(job->blk);
856 target = blk_bs(s->target);
d63ffd87 857
d63ffd87 858 if (!s->synced) {
9df229c3
AG
859 error_setg(errp, "The active block job '%s' cannot be completed",
860 job->id);
d63ffd87
PB
861 return;
862 }
863
274fccee
HR
864 if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) {
865 int ret;
866
867 assert(!target->backing);
868 ret = bdrv_open_backing_file(target, NULL, "backing", errp);
869 if (ret < 0) {
870 return;
871 }
872 }
873
15d67298 874 /* block all operations on to_replace bs */
09158f00 875 if (s->replaces) {
5a7e7a0b
SH
876 AioContext *replace_aio_context;
877
e12f3784 878 s->to_replace = bdrv_find_node(s->replaces);
09158f00 879 if (!s->to_replace) {
e12f3784 880 error_setg(errp, "Node name '%s' not found", s->replaces);
09158f00
BC
881 return;
882 }
883
5a7e7a0b
SH
884 replace_aio_context = bdrv_get_aio_context(s->to_replace);
885 aio_context_acquire(replace_aio_context);
886
09158f00
BC
887 error_setg(&s->replace_blocker,
888 "block device is in use by block-job-complete");
889 bdrv_op_block_all(s->to_replace, s->replace_blocker);
890 bdrv_ref(s->to_replace);
5a7e7a0b
SH
891
892 aio_context_release(replace_aio_context);
09158f00
BC
893 }
894
274fccee
HR
895 if (s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) {
896 BlockDriverState *backing = s->is_none_mode ? src : s->base;
897 if (backing_bs(target) != backing) {
898 bdrv_set_backing_hd(target, backing);
899 }
900 }
901
d63ffd87 902 s->should_complete = true;
751ebd76 903 block_job_enter(&s->common);
d63ffd87
PB
904}
905
bae8196d 906static void mirror_pause(BlockJob *job)
565ac01f
SH
907{
908 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
909
bae8196d 910 mirror_wait_for_all_io(s);
565ac01f
SH
911}
912
913static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context)
914{
915 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
916
917 blk_set_aio_context(s->target, new_context);
918}
919
bae8196d
PB
920static void mirror_drain(BlockJob *job)
921{
922 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
923
924 /* Need to keep a reference in case blk_drain triggers execution
925 * of mirror_complete...
926 */
927 if (s->target) {
928 BlockBackend *target = s->target;
929 blk_ref(target);
930 blk_drain(target);
931 blk_unref(target);
932 }
933}
934
3fc4b10a 935static const BlockJobDriver mirror_job_driver = {
565ac01f
SH
936 .instance_size = sizeof(MirrorBlockJob),
937 .job_type = BLOCK_JOB_TYPE_MIRROR,
938 .set_speed = mirror_set_speed,
a7815a76 939 .start = mirror_run,
565ac01f
SH
940 .complete = mirror_complete,
941 .pause = mirror_pause,
942 .attached_aio_context = mirror_attached_aio_context,
bae8196d 943 .drain = mirror_drain,
893f7eba
PB
944};
945
03544a6e 946static const BlockJobDriver commit_active_job_driver = {
565ac01f
SH
947 .instance_size = sizeof(MirrorBlockJob),
948 .job_type = BLOCK_JOB_TYPE_COMMIT,
949 .set_speed = mirror_set_speed,
a7815a76 950 .start = mirror_run,
565ac01f
SH
951 .complete = mirror_complete,
952 .pause = mirror_pause,
953 .attached_aio_context = mirror_attached_aio_context,
bae8196d 954 .drain = mirror_drain,
03544a6e
FZ
955};
956
71aa9867 957static void mirror_start_job(const char *job_id, BlockDriverState *bs,
47970dfb
JS
958 int creation_flags, BlockDriverState *target,
959 const char *replaces, int64_t speed,
960 uint32_t granularity, int64_t buf_size,
274fccee 961 BlockMirrorBackingMode backing_mode,
09158f00
BC
962 BlockdevOnError on_source_error,
963 BlockdevOnError on_target_error,
0fc9f8ea 964 bool unmap,
097310b5 965 BlockCompletionFunc *cb,
09158f00
BC
966 void *opaque, Error **errp,
967 const BlockJobDriver *driver,
b49f7ead
WC
968 bool is_none_mode, BlockDriverState *base,
969 bool auto_complete)
893f7eba
PB
970{
971 MirrorBlockJob *s;
972
eee13dfe 973 if (granularity == 0) {
341ebc2f 974 granularity = bdrv_get_default_bitmap_granularity(target);
eee13dfe
PB
975 }
976
977 assert ((granularity & (granularity - 1)) == 0);
978
48ac0a4d
WC
979 if (buf_size < 0) {
980 error_setg(errp, "Invalid parameter 'buf-size'");
981 return;
982 }
983
984 if (buf_size == 0) {
985 buf_size = DEFAULT_MIRROR_BUF_SIZE;
986 }
5bc361b8 987
47970dfb
JS
988 s = block_job_create(job_id, driver, bs, speed, creation_flags,
989 cb, opaque, errp);
893f7eba
PB
990 if (!s) {
991 return;
992 }
993
e253f4b8
KW
994 s->target = blk_new();
995 blk_insert_bs(s->target, target);
996
09158f00 997 s->replaces = g_strdup(replaces);
b952b558
PB
998 s->on_source_error = on_source_error;
999 s->on_target_error = on_target_error;
03544a6e 1000 s->is_none_mode = is_none_mode;
274fccee 1001 s->backing_mode = backing_mode;
5bc361b8 1002 s->base = base;
eee13dfe 1003 s->granularity = granularity;
48ac0a4d 1004 s->buf_size = ROUND_UP(buf_size, granularity);
0fc9f8ea 1005 s->unmap = unmap;
b49f7ead
WC
1006 if (auto_complete) {
1007 s->should_complete = true;
1008 }
b812f671 1009
0db6e54a 1010 s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
b8afb520 1011 if (!s->dirty_bitmap) {
97031164 1012 g_free(s->replaces);
e253f4b8 1013 blk_unref(s->target);
18930ba3 1014 block_job_unref(&s->common);
b8afb520
FZ
1015 return;
1016 }
10f3cd15 1017
cee3c6b5 1018 block_job_add_bdrv(&s->common, target);
f3ede4b0
AG
1019 /* In commit_active_start() all intermediate nodes disappear, so
1020 * any jobs in them must be blocked */
1021 if (bdrv_chain_contains(bs, target)) {
1022 BlockDriverState *iter;
1023 for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) {
1024 block_job_add_bdrv(&s->common, iter);
1025 }
1026 }
10f3cd15 1027
5ccac6f1
JS
1028 trace_mirror_start(bs, s, opaque);
1029 block_job_start(&s->common);
893f7eba 1030}
03544a6e 1031
71aa9867
AG
1032void mirror_start(const char *job_id, BlockDriverState *bs,
1033 BlockDriverState *target, const char *replaces,
5fba6c0e 1034 int64_t speed, uint32_t granularity, int64_t buf_size,
274fccee
HR
1035 MirrorSyncMode mode, BlockMirrorBackingMode backing_mode,
1036 BlockdevOnError on_source_error,
03544a6e 1037 BlockdevOnError on_target_error,
8254b6d9 1038 bool unmap, Error **errp)
03544a6e
FZ
1039{
1040 bool is_none_mode;
1041 BlockDriverState *base;
1042
4b80ab2b
JS
1043 if (mode == MIRROR_SYNC_MODE_INCREMENTAL) {
1044 error_setg(errp, "Sync mode 'incremental' not supported");
d58d8453
JS
1045 return;
1046 }
03544a6e 1047 is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
760e0063 1048 base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL;
47970dfb 1049 mirror_start_job(job_id, bs, BLOCK_JOB_DEFAULT, target, replaces,
274fccee 1050 speed, granularity, buf_size, backing_mode,
8254b6d9 1051 on_source_error, on_target_error, unmap, NULL, NULL, errp,
b49f7ead 1052 &mirror_job_driver, is_none_mode, base, false);
03544a6e
FZ
1053}
1054
fd62c609 1055void commit_active_start(const char *job_id, BlockDriverState *bs,
47970dfb
JS
1056 BlockDriverState *base, int creation_flags,
1057 int64_t speed, BlockdevOnError on_error,
1058 BlockCompletionFunc *cb, void *opaque, Error **errp,
b49f7ead 1059 bool auto_complete)
03544a6e 1060{
4da83585
JC
1061 int64_t length, base_length;
1062 int orig_base_flags;
39a611a3 1063 int ret;
cc67f4d1 1064 Error *local_err = NULL;
4da83585
JC
1065
1066 orig_base_flags = bdrv_get_flags(base);
1067
20a63d2c
FZ
1068 if (bdrv_reopen(base, bs->open_flags, errp)) {
1069 return;
1070 }
4da83585
JC
1071
1072 length = bdrv_getlength(bs);
1073 if (length < 0) {
39a611a3
JC
1074 error_setg_errno(errp, -length,
1075 "Unable to determine length of %s", bs->filename);
4da83585
JC
1076 goto error_restore_flags;
1077 }
1078
1079 base_length = bdrv_getlength(base);
1080 if (base_length < 0) {
39a611a3
JC
1081 error_setg_errno(errp, -base_length,
1082 "Unable to determine length of %s", base->filename);
4da83585
JC
1083 goto error_restore_flags;
1084 }
1085
1086 if (length > base_length) {
39a611a3
JC
1087 ret = bdrv_truncate(base, length);
1088 if (ret < 0) {
1089 error_setg_errno(errp, -ret,
1090 "Top image %s is larger than base image %s, and "
4da83585
JC
1091 "resize of base image failed",
1092 bs->filename, base->filename);
1093 goto error_restore_flags;
1094 }
1095 }
1096
47970dfb 1097 mirror_start_job(job_id, bs, creation_flags, base, NULL, speed, 0, 0,
71aa9867 1098 MIRROR_LEAVE_BACKING_CHAIN,
6f13acf9 1099 on_error, on_error, true, cb, opaque, &local_err,
b49f7ead 1100 &commit_active_job_driver, false, base, auto_complete);
0fb6395c 1101 if (local_err) {
cc67f4d1 1102 error_propagate(errp, local_err);
4da83585
JC
1103 goto error_restore_flags;
1104 }
1105
1106 return;
1107
1108error_restore_flags:
1109 /* ignore error and errp for bdrv_reopen, because we want to propagate
1110 * the original error */
1111 bdrv_reopen(base, orig_base_flags, NULL);
1112 return;
03544a6e 1113}