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