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