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 "qemu/coroutine.h" 17 #include "qemu/range.h" 18 #include "trace.h" 19 #include "block/blockjob_int.h" 20 #include "block/block_int.h" 21 #include "sysemu/block-backend.h" 22 #include "qapi/error.h" 23 #include "qapi/qmp/qerror.h" 24 #include "qemu/ratelimit.h" 25 #include "qemu/bitmap.h" 26 27 #define MAX_IN_FLIGHT 16 28 #define MAX_IO_BYTES (1 << 20) /* 1 Mb */ 29 #define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES) 30 31 /* The mirroring buffer is a list of granularity-sized chunks. 32 * Free chunks are organized in a list. 33 */ 34 typedef struct MirrorBuffer { 35 QSIMPLEQ_ENTRY(MirrorBuffer) next; 36 } MirrorBuffer; 37 38 typedef struct MirrorOp MirrorOp; 39 40 typedef struct MirrorBlockJob { 41 BlockJob common; 42 BlockBackend *target; 43 BlockDriverState *mirror_top_bs; 44 BlockDriverState *base; 45 BlockDriverState *base_overlay; 46 47 /* The name of the graph node to replace */ 48 char *replaces; 49 /* The BDS to replace */ 50 BlockDriverState *to_replace; 51 /* Used to block operations on the drive-mirror-replace target */ 52 Error *replace_blocker; 53 bool is_none_mode; 54 BlockMirrorBackingMode backing_mode; 55 /* Whether the target image requires explicit zero-initialization */ 56 bool zero_target; 57 MirrorCopyMode copy_mode; 58 BlockdevOnError on_source_error, on_target_error; 59 bool synced; 60 /* Set when the target is synced (dirty bitmap is clean, nothing 61 * in flight) and the job is running in active mode */ 62 bool actively_synced; 63 bool should_complete; 64 int64_t granularity; 65 size_t buf_size; 66 int64_t bdev_length; 67 unsigned long *cow_bitmap; 68 BdrvDirtyBitmap *dirty_bitmap; 69 BdrvDirtyBitmapIter *dbi; 70 uint8_t *buf; 71 QSIMPLEQ_HEAD(, MirrorBuffer) buf_free; 72 int buf_free_count; 73 74 uint64_t last_pause_ns; 75 unsigned long *in_flight_bitmap; 76 int in_flight; 77 int64_t bytes_in_flight; 78 QTAILQ_HEAD(, MirrorOp) ops_in_flight; 79 int ret; 80 bool unmap; 81 int target_cluster_size; 82 int max_iov; 83 bool initial_zeroing_ongoing; 84 int in_active_write_counter; 85 bool prepared; 86 bool in_drain; 87 } MirrorBlockJob; 88 89 typedef struct MirrorBDSOpaque { 90 MirrorBlockJob *job; 91 bool stop; 92 bool is_commit; 93 } MirrorBDSOpaque; 94 95 struct MirrorOp { 96 MirrorBlockJob *s; 97 QEMUIOVector qiov; 98 int64_t offset; 99 uint64_t bytes; 100 101 /* The pointee is set by mirror_co_read(), mirror_co_zero(), and 102 * mirror_co_discard() before yielding for the first time */ 103 int64_t *bytes_handled; 104 105 bool is_pseudo_op; 106 bool is_active_write; 107 bool is_in_flight; 108 CoQueue waiting_requests; 109 Coroutine *co; 110 MirrorOp *waiting_for_op; 111 112 QTAILQ_ENTRY(MirrorOp) next; 113 }; 114 115 typedef enum MirrorMethod { 116 MIRROR_METHOD_COPY, 117 MIRROR_METHOD_ZERO, 118 MIRROR_METHOD_DISCARD, 119 } MirrorMethod; 120 121 static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, 122 int error) 123 { 124 s->synced = false; 125 s->actively_synced = false; 126 if (read) { 127 return block_job_error_action(&s->common, s->on_source_error, 128 true, error); 129 } else { 130 return block_job_error_action(&s->common, s->on_target_error, 131 false, error); 132 } 133 } 134 135 static void coroutine_fn mirror_wait_on_conflicts(MirrorOp *self, 136 MirrorBlockJob *s, 137 uint64_t offset, 138 uint64_t bytes) 139 { 140 uint64_t self_start_chunk = offset / s->granularity; 141 uint64_t self_end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); 142 uint64_t self_nb_chunks = self_end_chunk - self_start_chunk; 143 144 while (find_next_bit(s->in_flight_bitmap, self_end_chunk, 145 self_start_chunk) < self_end_chunk && 146 s->ret >= 0) 147 { 148 MirrorOp *op; 149 150 QTAILQ_FOREACH(op, &s->ops_in_flight, next) { 151 uint64_t op_start_chunk = op->offset / s->granularity; 152 uint64_t op_nb_chunks = DIV_ROUND_UP(op->offset + op->bytes, 153 s->granularity) - 154 op_start_chunk; 155 156 if (op == self) { 157 continue; 158 } 159 160 if (ranges_overlap(self_start_chunk, self_nb_chunks, 161 op_start_chunk, op_nb_chunks)) 162 { 163 if (self) { 164 /* 165 * If the operation is already (indirectly) waiting for us, 166 * or will wait for us as soon as it wakes up, then just go 167 * on (instead of producing a deadlock in the former case). 168 */ 169 if (op->waiting_for_op) { 170 continue; 171 } 172 173 self->waiting_for_op = op; 174 } 175 176 qemu_co_queue_wait(&op->waiting_requests, NULL); 177 178 if (self) { 179 self->waiting_for_op = NULL; 180 } 181 182 break; 183 } 184 } 185 } 186 } 187 188 static void coroutine_fn mirror_iteration_done(MirrorOp *op, int ret) 189 { 190 MirrorBlockJob *s = op->s; 191 struct iovec *iov; 192 int64_t chunk_num; 193 int i, nb_chunks; 194 195 trace_mirror_iteration_done(s, op->offset, op->bytes, ret); 196 197 s->in_flight--; 198 s->bytes_in_flight -= op->bytes; 199 iov = op->qiov.iov; 200 for (i = 0; i < op->qiov.niov; i++) { 201 MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base; 202 QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next); 203 s->buf_free_count++; 204 } 205 206 chunk_num = op->offset / s->granularity; 207 nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); 208 209 bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); 210 QTAILQ_REMOVE(&s->ops_in_flight, op, next); 211 if (ret >= 0) { 212 if (s->cow_bitmap) { 213 bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); 214 } 215 if (!s->initial_zeroing_ongoing) { 216 job_progress_update(&s->common.job, op->bytes); 217 } 218 } 219 qemu_iovec_destroy(&op->qiov); 220 221 qemu_co_queue_restart_all(&op->waiting_requests); 222 g_free(op); 223 } 224 225 static void coroutine_fn mirror_write_complete(MirrorOp *op, int ret) 226 { 227 MirrorBlockJob *s = op->s; 228 229 if (ret < 0) { 230 BlockErrorAction action; 231 232 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); 233 action = mirror_error_action(s, false, -ret); 234 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { 235 s->ret = ret; 236 } 237 } 238 239 mirror_iteration_done(op, ret); 240 } 241 242 static void coroutine_fn mirror_read_complete(MirrorOp *op, int ret) 243 { 244 MirrorBlockJob *s = op->s; 245 246 if (ret < 0) { 247 BlockErrorAction action; 248 249 bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); 250 action = mirror_error_action(s, true, -ret); 251 if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { 252 s->ret = ret; 253 } 254 255 mirror_iteration_done(op, ret); 256 return; 257 } 258 259 ret = blk_co_pwritev(s->target, op->offset, op->qiov.size, &op->qiov, 0); 260 mirror_write_complete(op, ret); 261 } 262 263 /* Clip bytes relative to offset to not exceed end-of-file */ 264 static inline int64_t mirror_clip_bytes(MirrorBlockJob *s, 265 int64_t offset, 266 int64_t bytes) 267 { 268 return MIN(bytes, s->bdev_length - offset); 269 } 270 271 /* Round offset and/or bytes to target cluster if COW is needed, and 272 * return the offset of the adjusted tail against original. */ 273 static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset, 274 uint64_t *bytes) 275 { 276 bool need_cow; 277 int ret = 0; 278 int64_t align_offset = *offset; 279 int64_t align_bytes = *bytes; 280 int max_bytes = s->granularity * s->max_iov; 281 282 need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap); 283 need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity, 284 s->cow_bitmap); 285 if (need_cow) { 286 bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes, 287 &align_offset, &align_bytes); 288 } 289 290 if (align_bytes > max_bytes) { 291 align_bytes = max_bytes; 292 if (need_cow) { 293 align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size); 294 } 295 } 296 /* Clipping may result in align_bytes unaligned to chunk boundary, but 297 * that doesn't matter because it's already the end of source image. */ 298 align_bytes = mirror_clip_bytes(s, align_offset, align_bytes); 299 300 ret = align_offset + align_bytes - (*offset + *bytes); 301 *offset = align_offset; 302 *bytes = align_bytes; 303 assert(ret >= 0); 304 return ret; 305 } 306 307 static inline void coroutine_fn 308 mirror_wait_for_any_operation(MirrorBlockJob *s, bool active) 309 { 310 MirrorOp *op; 311 312 QTAILQ_FOREACH(op, &s->ops_in_flight, next) { 313 /* Do not wait on pseudo ops, because it may in turn wait on 314 * some other operation to start, which may in fact be the 315 * caller of this function. Since there is only one pseudo op 316 * at any given time, we will always find some real operation 317 * to wait on. */ 318 if (!op->is_pseudo_op && op->is_in_flight && 319 op->is_active_write == active) 320 { 321 qemu_co_queue_wait(&op->waiting_requests, NULL); 322 return; 323 } 324 } 325 abort(); 326 } 327 328 static inline void coroutine_fn 329 mirror_wait_for_free_in_flight_slot(MirrorBlockJob *s) 330 { 331 /* Only non-active operations use up in-flight slots */ 332 mirror_wait_for_any_operation(s, false); 333 } 334 335 /* Perform a mirror copy operation. 336 * 337 * *op->bytes_handled is set to the number of bytes copied after and 338 * including offset, excluding any bytes copied prior to offset due 339 * to alignment. This will be op->bytes if no alignment is necessary, 340 * or (new_end - op->offset) if the tail is rounded up or down due to 341 * alignment or buffer limit. 342 */ 343 static void coroutine_fn mirror_co_read(void *opaque) 344 { 345 MirrorOp *op = opaque; 346 MirrorBlockJob *s = op->s; 347 int nb_chunks; 348 uint64_t ret; 349 uint64_t max_bytes; 350 351 max_bytes = s->granularity * s->max_iov; 352 353 /* We can only handle as much as buf_size at a time. */ 354 op->bytes = MIN(s->buf_size, MIN(max_bytes, op->bytes)); 355 assert(op->bytes); 356 assert(op->bytes < BDRV_REQUEST_MAX_BYTES); 357 *op->bytes_handled = op->bytes; 358 359 if (s->cow_bitmap) { 360 *op->bytes_handled += mirror_cow_align(s, &op->offset, &op->bytes); 361 } 362 /* Cannot exceed BDRV_REQUEST_MAX_BYTES + INT_MAX */ 363 assert(*op->bytes_handled <= UINT_MAX); 364 assert(op->bytes <= s->buf_size); 365 /* The offset is granularity-aligned because: 366 * 1) Caller passes in aligned values; 367 * 2) mirror_cow_align is used only when target cluster is larger. */ 368 assert(QEMU_IS_ALIGNED(op->offset, s->granularity)); 369 /* The range is sector-aligned, since bdrv_getlength() rounds up. */ 370 assert(QEMU_IS_ALIGNED(op->bytes, BDRV_SECTOR_SIZE)); 371 nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); 372 373 while (s->buf_free_count < nb_chunks) { 374 trace_mirror_yield_in_flight(s, op->offset, s->in_flight); 375 mirror_wait_for_free_in_flight_slot(s); 376 } 377 378 /* Now make a QEMUIOVector taking enough granularity-sized chunks 379 * from s->buf_free. 380 */ 381 qemu_iovec_init(&op->qiov, nb_chunks); 382 while (nb_chunks-- > 0) { 383 MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free); 384 size_t remaining = op->bytes - op->qiov.size; 385 386 QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); 387 s->buf_free_count--; 388 qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining)); 389 } 390 391 /* Copy the dirty cluster. */ 392 s->in_flight++; 393 s->bytes_in_flight += op->bytes; 394 op->is_in_flight = true; 395 trace_mirror_one_iteration(s, op->offset, op->bytes); 396 397 ret = bdrv_co_preadv(s->mirror_top_bs->backing, op->offset, op->bytes, 398 &op->qiov, 0); 399 mirror_read_complete(op, ret); 400 } 401 402 static void coroutine_fn mirror_co_zero(void *opaque) 403 { 404 MirrorOp *op = opaque; 405 int ret; 406 407 op->s->in_flight++; 408 op->s->bytes_in_flight += op->bytes; 409 *op->bytes_handled = op->bytes; 410 op->is_in_flight = true; 411 412 ret = blk_co_pwrite_zeroes(op->s->target, op->offset, op->bytes, 413 op->s->unmap ? BDRV_REQ_MAY_UNMAP : 0); 414 mirror_write_complete(op, ret); 415 } 416 417 static void coroutine_fn mirror_co_discard(void *opaque) 418 { 419 MirrorOp *op = opaque; 420 int ret; 421 422 op->s->in_flight++; 423 op->s->bytes_in_flight += op->bytes; 424 *op->bytes_handled = op->bytes; 425 op->is_in_flight = true; 426 427 ret = blk_co_pdiscard(op->s->target, op->offset, op->bytes); 428 mirror_write_complete(op, ret); 429 } 430 431 static unsigned mirror_perform(MirrorBlockJob *s, int64_t offset, 432 unsigned bytes, MirrorMethod mirror_method) 433 { 434 MirrorOp *op; 435 Coroutine *co; 436 int64_t bytes_handled = -1; 437 438 op = g_new(MirrorOp, 1); 439 *op = (MirrorOp){ 440 .s = s, 441 .offset = offset, 442 .bytes = bytes, 443 .bytes_handled = &bytes_handled, 444 }; 445 qemu_co_queue_init(&op->waiting_requests); 446 447 switch (mirror_method) { 448 case MIRROR_METHOD_COPY: 449 co = qemu_coroutine_create(mirror_co_read, op); 450 break; 451 case MIRROR_METHOD_ZERO: 452 co = qemu_coroutine_create(mirror_co_zero, op); 453 break; 454 case MIRROR_METHOD_DISCARD: 455 co = qemu_coroutine_create(mirror_co_discard, op); 456 break; 457 default: 458 abort(); 459 } 460 op->co = co; 461 462 QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); 463 qemu_coroutine_enter(co); 464 /* At this point, ownership of op has been moved to the coroutine 465 * and the object may already be freed */ 466 467 /* Assert that this value has been set */ 468 assert(bytes_handled >= 0); 469 470 /* Same assertion as in mirror_co_read() (and for mirror_co_read() 471 * and mirror_co_discard(), bytes_handled == op->bytes, which 472 * is the @bytes parameter given to this function) */ 473 assert(bytes_handled <= UINT_MAX); 474 return bytes_handled; 475 } 476 477 static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) 478 { 479 BlockDriverState *source = s->mirror_top_bs->backing->bs; 480 MirrorOp *pseudo_op; 481 int64_t offset; 482 uint64_t delay_ns = 0, ret = 0; 483 /* At least the first dirty chunk is mirrored in one iteration. */ 484 int nb_chunks = 1; 485 bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)); 486 int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES); 487 488 bdrv_dirty_bitmap_lock(s->dirty_bitmap); 489 offset = bdrv_dirty_iter_next(s->dbi); 490 if (offset < 0) { 491 bdrv_set_dirty_iter(s->dbi, 0); 492 offset = bdrv_dirty_iter_next(s->dbi); 493 trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)); 494 assert(offset >= 0); 495 } 496 bdrv_dirty_bitmap_unlock(s->dirty_bitmap); 497 498 mirror_wait_on_conflicts(NULL, s, offset, 1); 499 500 job_pause_point(&s->common.job); 501 502 /* Find the number of consective dirty chunks following the first dirty 503 * one, and wait for in flight requests in them. */ 504 bdrv_dirty_bitmap_lock(s->dirty_bitmap); 505 while (nb_chunks * s->granularity < s->buf_size) { 506 int64_t next_dirty; 507 int64_t next_offset = offset + nb_chunks * s->granularity; 508 int64_t next_chunk = next_offset / s->granularity; 509 if (next_offset >= s->bdev_length || 510 !bdrv_dirty_bitmap_get_locked(s->dirty_bitmap, next_offset)) { 511 break; 512 } 513 if (test_bit(next_chunk, s->in_flight_bitmap)) { 514 break; 515 } 516 517 next_dirty = bdrv_dirty_iter_next(s->dbi); 518 if (next_dirty > next_offset || next_dirty < 0) { 519 /* The bitmap iterator's cache is stale, refresh it */ 520 bdrv_set_dirty_iter(s->dbi, next_offset); 521 next_dirty = bdrv_dirty_iter_next(s->dbi); 522 } 523 assert(next_dirty == next_offset); 524 nb_chunks++; 525 } 526 527 /* Clear dirty bits before querying the block status, because 528 * calling bdrv_block_status_above could yield - if some blocks are 529 * marked dirty in this window, we need to know. 530 */ 531 bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset, 532 nb_chunks * s->granularity); 533 bdrv_dirty_bitmap_unlock(s->dirty_bitmap); 534 535 /* Before claiming an area in the in-flight bitmap, we have to 536 * create a MirrorOp for it so that conflicting requests can wait 537 * for it. mirror_perform() will create the real MirrorOps later, 538 * for now we just create a pseudo operation that will wake up all 539 * conflicting requests once all real operations have been 540 * launched. */ 541 pseudo_op = g_new(MirrorOp, 1); 542 *pseudo_op = (MirrorOp){ 543 .offset = offset, 544 .bytes = nb_chunks * s->granularity, 545 .is_pseudo_op = true, 546 }; 547 qemu_co_queue_init(&pseudo_op->waiting_requests); 548 QTAILQ_INSERT_TAIL(&s->ops_in_flight, pseudo_op, next); 549 550 bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks); 551 while (nb_chunks > 0 && offset < s->bdev_length) { 552 int ret; 553 int64_t io_bytes; 554 int64_t io_bytes_acct; 555 MirrorMethod mirror_method = MIRROR_METHOD_COPY; 556 557 assert(!(offset % s->granularity)); 558 ret = bdrv_block_status_above(source, NULL, offset, 559 nb_chunks * s->granularity, 560 &io_bytes, NULL, NULL); 561 if (ret < 0) { 562 io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes); 563 } else if (ret & BDRV_BLOCK_DATA) { 564 io_bytes = MIN(io_bytes, max_io_bytes); 565 } 566 567 io_bytes -= io_bytes % s->granularity; 568 if (io_bytes < s->granularity) { 569 io_bytes = s->granularity; 570 } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) { 571 int64_t target_offset; 572 int64_t target_bytes; 573 bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes, 574 &target_offset, &target_bytes); 575 if (target_offset == offset && 576 target_bytes == io_bytes) { 577 mirror_method = ret & BDRV_BLOCK_ZERO ? 578 MIRROR_METHOD_ZERO : 579 MIRROR_METHOD_DISCARD; 580 } 581 } 582 583 while (s->in_flight >= MAX_IN_FLIGHT) { 584 trace_mirror_yield_in_flight(s, offset, s->in_flight); 585 mirror_wait_for_free_in_flight_slot(s); 586 } 587 588 if (s->ret < 0) { 589 ret = 0; 590 goto fail; 591 } 592 593 io_bytes = mirror_clip_bytes(s, offset, io_bytes); 594 io_bytes = mirror_perform(s, offset, io_bytes, mirror_method); 595 if (mirror_method != MIRROR_METHOD_COPY && write_zeroes_ok) { 596 io_bytes_acct = 0; 597 } else { 598 io_bytes_acct = io_bytes; 599 } 600 assert(io_bytes); 601 offset += io_bytes; 602 nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity); 603 delay_ns = block_job_ratelimit_get_delay(&s->common, io_bytes_acct); 604 } 605 606 ret = delay_ns; 607 fail: 608 QTAILQ_REMOVE(&s->ops_in_flight, pseudo_op, next); 609 qemu_co_queue_restart_all(&pseudo_op->waiting_requests); 610 g_free(pseudo_op); 611 612 return ret; 613 } 614 615 static void mirror_free_init(MirrorBlockJob *s) 616 { 617 int granularity = s->granularity; 618 size_t buf_size = s->buf_size; 619 uint8_t *buf = s->buf; 620 621 assert(s->buf_free_count == 0); 622 QSIMPLEQ_INIT(&s->buf_free); 623 while (buf_size != 0) { 624 MirrorBuffer *cur = (MirrorBuffer *)buf; 625 QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next); 626 s->buf_free_count++; 627 buf_size -= granularity; 628 buf += granularity; 629 } 630 } 631 632 /* This is also used for the .pause callback. There is no matching 633 * mirror_resume() because mirror_run() will begin iterating again 634 * when the job is resumed. 635 */ 636 static void coroutine_fn mirror_wait_for_all_io(MirrorBlockJob *s) 637 { 638 while (s->in_flight > 0) { 639 mirror_wait_for_free_in_flight_slot(s); 640 } 641 } 642 643 /** 644 * mirror_exit_common: handle both abort() and prepare() cases. 645 * for .prepare, returns 0 on success and -errno on failure. 646 * for .abort cases, denoted by abort = true, MUST return 0. 647 */ 648 static int mirror_exit_common(Job *job) 649 { 650 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 651 BlockJob *bjob = &s->common; 652 MirrorBDSOpaque *bs_opaque; 653 AioContext *replace_aio_context = NULL; 654 BlockDriverState *src; 655 BlockDriverState *target_bs; 656 BlockDriverState *mirror_top_bs; 657 Error *local_err = NULL; 658 bool abort = job->ret < 0; 659 int ret = 0; 660 661 if (s->prepared) { 662 return 0; 663 } 664 s->prepared = true; 665 666 mirror_top_bs = s->mirror_top_bs; 667 bs_opaque = mirror_top_bs->opaque; 668 src = mirror_top_bs->backing->bs; 669 target_bs = blk_bs(s->target); 670 671 if (bdrv_chain_contains(src, target_bs)) { 672 bdrv_unfreeze_backing_chain(mirror_top_bs, target_bs); 673 } 674 675 bdrv_release_dirty_bitmap(s->dirty_bitmap); 676 677 /* Make sure that the source BDS doesn't go away during bdrv_replace_node, 678 * before we can call bdrv_drained_end */ 679 bdrv_ref(src); 680 bdrv_ref(mirror_top_bs); 681 bdrv_ref(target_bs); 682 683 /* 684 * Remove target parent that still uses BLK_PERM_WRITE/RESIZE before 685 * inserting target_bs at s->to_replace, where we might not be able to get 686 * these permissions. 687 */ 688 blk_unref(s->target); 689 s->target = NULL; 690 691 /* We don't access the source any more. Dropping any WRITE/RESIZE is 692 * required before it could become a backing file of target_bs. Not having 693 * these permissions any more means that we can't allow any new requests on 694 * mirror_top_bs from now on, so keep it drained. */ 695 bdrv_drained_begin(mirror_top_bs); 696 bs_opaque->stop = true; 697 bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, 698 &error_abort); 699 if (!abort && s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) { 700 BlockDriverState *backing = s->is_none_mode ? src : s->base; 701 BlockDriverState *unfiltered_target = bdrv_skip_filters(target_bs); 702 703 if (bdrv_cow_bs(unfiltered_target) != backing) { 704 bdrv_set_backing_hd(unfiltered_target, backing, &local_err); 705 if (local_err) { 706 error_report_err(local_err); 707 local_err = NULL; 708 ret = -EPERM; 709 } 710 } 711 } else if (!abort && s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) { 712 assert(!bdrv_backing_chain_next(target_bs)); 713 ret = bdrv_open_backing_file(bdrv_skip_filters(target_bs), NULL, 714 "backing", &local_err); 715 if (ret < 0) { 716 error_report_err(local_err); 717 local_err = NULL; 718 } 719 } 720 721 if (s->to_replace) { 722 replace_aio_context = bdrv_get_aio_context(s->to_replace); 723 aio_context_acquire(replace_aio_context); 724 } 725 726 if (s->should_complete && !abort) { 727 BlockDriverState *to_replace = s->to_replace ?: src; 728 bool ro = bdrv_is_read_only(to_replace); 729 730 if (ro != bdrv_is_read_only(target_bs)) { 731 bdrv_reopen_set_read_only(target_bs, ro, NULL); 732 } 733 734 /* The mirror job has no requests in flight any more, but we need to 735 * drain potential other users of the BDS before changing the graph. */ 736 assert(s->in_drain); 737 bdrv_drained_begin(target_bs); 738 /* 739 * Cannot use check_to_replace_node() here, because that would 740 * check for an op blocker on @to_replace, and we have our own 741 * there. 742 */ 743 if (bdrv_recurse_can_replace(src, to_replace)) { 744 bdrv_replace_node(to_replace, target_bs, &local_err); 745 } else { 746 error_setg(&local_err, "Can no longer replace '%s' by '%s', " 747 "because it can no longer be guaranteed that doing so " 748 "would not lead to an abrupt change of visible data", 749 to_replace->node_name, target_bs->node_name); 750 } 751 bdrv_drained_end(target_bs); 752 if (local_err) { 753 error_report_err(local_err); 754 ret = -EPERM; 755 } 756 } 757 if (s->to_replace) { 758 bdrv_op_unblock_all(s->to_replace, s->replace_blocker); 759 error_free(s->replace_blocker); 760 bdrv_unref(s->to_replace); 761 } 762 if (replace_aio_context) { 763 aio_context_release(replace_aio_context); 764 } 765 g_free(s->replaces); 766 bdrv_unref(target_bs); 767 768 /* 769 * Remove the mirror filter driver from the graph. Before this, get rid of 770 * the blockers on the intermediate nodes so that the resulting state is 771 * valid. 772 */ 773 block_job_remove_all_bdrv(bjob); 774 bdrv_replace_node(mirror_top_bs, mirror_top_bs->backing->bs, &error_abort); 775 776 /* We just changed the BDS the job BB refers to (with either or both of the 777 * bdrv_replace_node() calls), so switch the BB back so the cleanup does 778 * the right thing. We don't need any permissions any more now. */ 779 blk_remove_bs(bjob->blk); 780 blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort); 781 blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort); 782 783 bs_opaque->job = NULL; 784 785 bdrv_drained_end(src); 786 bdrv_drained_end(mirror_top_bs); 787 s->in_drain = false; 788 bdrv_unref(mirror_top_bs); 789 bdrv_unref(src); 790 791 return ret; 792 } 793 794 static int mirror_prepare(Job *job) 795 { 796 return mirror_exit_common(job); 797 } 798 799 static void mirror_abort(Job *job) 800 { 801 int ret = mirror_exit_common(job); 802 assert(ret == 0); 803 } 804 805 static void coroutine_fn mirror_throttle(MirrorBlockJob *s) 806 { 807 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 808 809 if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) { 810 s->last_pause_ns = now; 811 job_sleep_ns(&s->common.job, 0); 812 } else { 813 job_pause_point(&s->common.job); 814 } 815 } 816 817 static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s) 818 { 819 int64_t offset; 820 BlockDriverState *bs = s->mirror_top_bs->backing->bs; 821 BlockDriverState *target_bs = blk_bs(s->target); 822 int ret; 823 int64_t count; 824 825 if (s->zero_target) { 826 if (!bdrv_can_write_zeroes_with_unmap(target_bs)) { 827 bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length); 828 return 0; 829 } 830 831 s->initial_zeroing_ongoing = true; 832 for (offset = 0; offset < s->bdev_length; ) { 833 int bytes = MIN(s->bdev_length - offset, 834 QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); 835 836 mirror_throttle(s); 837 838 if (job_is_cancelled(&s->common.job)) { 839 s->initial_zeroing_ongoing = false; 840 return 0; 841 } 842 843 if (s->in_flight >= MAX_IN_FLIGHT) { 844 trace_mirror_yield(s, UINT64_MAX, s->buf_free_count, 845 s->in_flight); 846 mirror_wait_for_free_in_flight_slot(s); 847 continue; 848 } 849 850 mirror_perform(s, offset, bytes, MIRROR_METHOD_ZERO); 851 offset += bytes; 852 } 853 854 mirror_wait_for_all_io(s); 855 s->initial_zeroing_ongoing = false; 856 } 857 858 /* First part, loop on the sectors and initialize the dirty bitmap. */ 859 for (offset = 0; offset < s->bdev_length; ) { 860 /* Just to make sure we are not exceeding int limit. */ 861 int bytes = MIN(s->bdev_length - offset, 862 QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); 863 864 mirror_throttle(s); 865 866 if (job_is_cancelled(&s->common.job)) { 867 return 0; 868 } 869 870 ret = bdrv_is_allocated_above(bs, s->base_overlay, true, offset, bytes, 871 &count); 872 if (ret < 0) { 873 return ret; 874 } 875 876 assert(count); 877 if (ret > 0) { 878 bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count); 879 } 880 offset += count; 881 } 882 return 0; 883 } 884 885 /* Called when going out of the streaming phase to flush the bulk of the 886 * data to the medium, or just before completing. 887 */ 888 static int mirror_flush(MirrorBlockJob *s) 889 { 890 int ret = blk_flush(s->target); 891 if (ret < 0) { 892 if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) { 893 s->ret = ret; 894 } 895 } 896 return ret; 897 } 898 899 static int coroutine_fn mirror_run(Job *job, Error **errp) 900 { 901 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 902 BlockDriverState *bs = s->mirror_top_bs->backing->bs; 903 BlockDriverState *target_bs = blk_bs(s->target); 904 bool need_drain = true; 905 int64_t length; 906 int64_t target_length; 907 BlockDriverInfo bdi; 908 char backing_filename[2]; /* we only need 2 characters because we are only 909 checking for a NULL string */ 910 int ret = 0; 911 912 if (job_is_cancelled(&s->common.job)) { 913 goto immediate_exit; 914 } 915 916 s->bdev_length = bdrv_getlength(bs); 917 if (s->bdev_length < 0) { 918 ret = s->bdev_length; 919 goto immediate_exit; 920 } 921 922 target_length = blk_getlength(s->target); 923 if (target_length < 0) { 924 ret = target_length; 925 goto immediate_exit; 926 } 927 928 /* Active commit must resize the base image if its size differs from the 929 * active layer. */ 930 if (s->base == blk_bs(s->target)) { 931 if (s->bdev_length > target_length) { 932 ret = blk_truncate(s->target, s->bdev_length, false, 933 PREALLOC_MODE_OFF, 0, NULL); 934 if (ret < 0) { 935 goto immediate_exit; 936 } 937 } 938 } else if (s->bdev_length != target_length) { 939 error_setg(errp, "Source and target image have different sizes"); 940 ret = -EINVAL; 941 goto immediate_exit; 942 } 943 944 if (s->bdev_length == 0) { 945 /* Transition to the READY state and wait for complete. */ 946 job_transition_to_ready(&s->common.job); 947 s->synced = true; 948 s->actively_synced = true; 949 while (!job_is_cancelled(&s->common.job) && !s->should_complete) { 950 job_yield(&s->common.job); 951 } 952 s->common.job.cancelled = false; 953 goto immediate_exit; 954 } 955 956 length = DIV_ROUND_UP(s->bdev_length, s->granularity); 957 s->in_flight_bitmap = bitmap_new(length); 958 959 /* If we have no backing file yet in the destination, we cannot let 960 * the destination do COW. Instead, we copy sectors around the 961 * dirty data if needed. We need a bitmap to do that. 962 */ 963 bdrv_get_backing_filename(target_bs, backing_filename, 964 sizeof(backing_filename)); 965 if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) { 966 s->target_cluster_size = bdi.cluster_size; 967 } else { 968 s->target_cluster_size = BDRV_SECTOR_SIZE; 969 } 970 if (backing_filename[0] && !bdrv_backing_chain_next(target_bs) && 971 s->granularity < s->target_cluster_size) { 972 s->buf_size = MAX(s->buf_size, s->target_cluster_size); 973 s->cow_bitmap = bitmap_new(length); 974 } 975 s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov); 976 977 s->buf = qemu_try_blockalign(bs, s->buf_size); 978 if (s->buf == NULL) { 979 ret = -ENOMEM; 980 goto immediate_exit; 981 } 982 983 mirror_free_init(s); 984 985 s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 986 if (!s->is_none_mode) { 987 ret = mirror_dirty_init(s); 988 if (ret < 0 || job_is_cancelled(&s->common.job)) { 989 goto immediate_exit; 990 } 991 } 992 993 assert(!s->dbi); 994 s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap); 995 for (;;) { 996 uint64_t delay_ns = 0; 997 int64_t cnt, delta; 998 bool should_complete; 999 1000 /* Do not start passive operations while there are active 1001 * writes in progress */ 1002 while (s->in_active_write_counter) { 1003 mirror_wait_for_any_operation(s, true); 1004 } 1005 1006 if (s->ret < 0) { 1007 ret = s->ret; 1008 goto immediate_exit; 1009 } 1010 1011 job_pause_point(&s->common.job); 1012 1013 cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1014 /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is 1015 * the number of bytes currently being processed; together those are 1016 * the current remaining operation length */ 1017 job_progress_set_remaining(&s->common.job, s->bytes_in_flight + cnt); 1018 1019 /* Note that even when no rate limit is applied we need to yield 1020 * periodically with no pending I/O so that bdrv_drain_all() returns. 1021 * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is 1022 * an error, or when the source is clean, whichever comes first. */ 1023 delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns; 1024 if (delta < BLOCK_JOB_SLICE_TIME && 1025 s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 1026 if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 || 1027 (cnt == 0 && s->in_flight > 0)) { 1028 trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight); 1029 mirror_wait_for_free_in_flight_slot(s); 1030 continue; 1031 } else if (cnt != 0) { 1032 delay_ns = mirror_iteration(s); 1033 } 1034 } 1035 1036 should_complete = false; 1037 if (s->in_flight == 0 && cnt == 0) { 1038 trace_mirror_before_flush(s); 1039 if (!s->synced) { 1040 if (mirror_flush(s) < 0) { 1041 /* Go check s->ret. */ 1042 continue; 1043 } 1044 /* We're out of the streaming phase. From now on, if the job 1045 * is cancelled we will actually complete all pending I/O and 1046 * report completion. This way, block-job-cancel will leave 1047 * the target in a consistent state. 1048 */ 1049 job_transition_to_ready(&s->common.job); 1050 s->synced = true; 1051 if (s->copy_mode != MIRROR_COPY_MODE_BACKGROUND) { 1052 s->actively_synced = true; 1053 } 1054 } 1055 1056 should_complete = s->should_complete || 1057 job_is_cancelled(&s->common.job); 1058 cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1059 } 1060 1061 if (cnt == 0 && should_complete) { 1062 /* The dirty bitmap is not updated while operations are pending. 1063 * If we're about to exit, wait for pending operations before 1064 * calling bdrv_get_dirty_count(bs), or we may exit while the 1065 * source has dirty data to copy! 1066 * 1067 * Note that I/O can be submitted by the guest while 1068 * mirror_populate runs, so pause it now. Before deciding 1069 * whether to switch to target check one last time if I/O has 1070 * come in the meanwhile, and if not flush the data to disk. 1071 */ 1072 trace_mirror_before_drain(s, cnt); 1073 1074 s->in_drain = true; 1075 bdrv_drained_begin(bs); 1076 cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1077 if (cnt > 0 || mirror_flush(s) < 0) { 1078 bdrv_drained_end(bs); 1079 s->in_drain = false; 1080 continue; 1081 } 1082 1083 /* The two disks are in sync. Exit and report successful 1084 * completion. 1085 */ 1086 assert(QLIST_EMPTY(&bs->tracked_requests)); 1087 s->common.job.cancelled = false; 1088 need_drain = false; 1089 break; 1090 } 1091 1092 ret = 0; 1093 1094 if (s->synced && !should_complete) { 1095 delay_ns = (s->in_flight == 0 && 1096 cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0); 1097 } 1098 trace_mirror_before_sleep(s, cnt, s->synced, delay_ns); 1099 job_sleep_ns(&s->common.job, delay_ns); 1100 if (job_is_cancelled(&s->common.job) && 1101 (!s->synced || s->common.job.force_cancel)) 1102 { 1103 break; 1104 } 1105 s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 1106 } 1107 1108 immediate_exit: 1109 if (s->in_flight > 0) { 1110 /* We get here only if something went wrong. Either the job failed, 1111 * or it was cancelled prematurely so that we do not guarantee that 1112 * the target is a copy of the source. 1113 */ 1114 assert(ret < 0 || ((s->common.job.force_cancel || !s->synced) && 1115 job_is_cancelled(&s->common.job))); 1116 assert(need_drain); 1117 mirror_wait_for_all_io(s); 1118 } 1119 1120 assert(s->in_flight == 0); 1121 qemu_vfree(s->buf); 1122 g_free(s->cow_bitmap); 1123 g_free(s->in_flight_bitmap); 1124 bdrv_dirty_iter_free(s->dbi); 1125 1126 if (need_drain) { 1127 s->in_drain = true; 1128 bdrv_drained_begin(bs); 1129 } 1130 1131 return ret; 1132 } 1133 1134 static void mirror_complete(Job *job, Error **errp) 1135 { 1136 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1137 1138 if (!s->synced) { 1139 error_setg(errp, "The active block job '%s' cannot be completed", 1140 job->id); 1141 return; 1142 } 1143 1144 /* block all operations on to_replace bs */ 1145 if (s->replaces) { 1146 AioContext *replace_aio_context; 1147 1148 s->to_replace = bdrv_find_node(s->replaces); 1149 if (!s->to_replace) { 1150 error_setg(errp, "Node name '%s' not found", s->replaces); 1151 return; 1152 } 1153 1154 replace_aio_context = bdrv_get_aio_context(s->to_replace); 1155 aio_context_acquire(replace_aio_context); 1156 1157 /* TODO Translate this into permission system. Current definition of 1158 * GRAPH_MOD would require to request it for the parents; they might 1159 * not even be BlockDriverStates, however, so a BdrvChild can't address 1160 * them. May need redefinition of GRAPH_MOD. */ 1161 error_setg(&s->replace_blocker, 1162 "block device is in use by block-job-complete"); 1163 bdrv_op_block_all(s->to_replace, s->replace_blocker); 1164 bdrv_ref(s->to_replace); 1165 1166 aio_context_release(replace_aio_context); 1167 } 1168 1169 s->should_complete = true; 1170 1171 /* If the job is paused, it will be re-entered when it is resumed */ 1172 if (!job->paused) { 1173 job_enter(job); 1174 } 1175 } 1176 1177 static void coroutine_fn mirror_pause(Job *job) 1178 { 1179 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1180 1181 mirror_wait_for_all_io(s); 1182 } 1183 1184 static bool mirror_drained_poll(BlockJob *job) 1185 { 1186 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); 1187 1188 /* If the job isn't paused nor cancelled, we can't be sure that it won't 1189 * issue more requests. We make an exception if we've reached this point 1190 * from one of our own drain sections, to avoid a deadlock waiting for 1191 * ourselves. 1192 */ 1193 if (!s->common.job.paused && !s->common.job.cancelled && !s->in_drain) { 1194 return true; 1195 } 1196 1197 return !!s->in_flight; 1198 } 1199 1200 static void mirror_cancel(Job *job, bool force) 1201 { 1202 MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1203 BlockDriverState *target = blk_bs(s->target); 1204 1205 if (force || !job_is_ready(job)) { 1206 bdrv_cancel_in_flight(target); 1207 } 1208 } 1209 1210 static const BlockJobDriver mirror_job_driver = { 1211 .job_driver = { 1212 .instance_size = sizeof(MirrorBlockJob), 1213 .job_type = JOB_TYPE_MIRROR, 1214 .free = block_job_free, 1215 .user_resume = block_job_user_resume, 1216 .run = mirror_run, 1217 .prepare = mirror_prepare, 1218 .abort = mirror_abort, 1219 .pause = mirror_pause, 1220 .complete = mirror_complete, 1221 .cancel = mirror_cancel, 1222 }, 1223 .drained_poll = mirror_drained_poll, 1224 }; 1225 1226 static const BlockJobDriver commit_active_job_driver = { 1227 .job_driver = { 1228 .instance_size = sizeof(MirrorBlockJob), 1229 .job_type = JOB_TYPE_COMMIT, 1230 .free = block_job_free, 1231 .user_resume = block_job_user_resume, 1232 .run = mirror_run, 1233 .prepare = mirror_prepare, 1234 .abort = mirror_abort, 1235 .pause = mirror_pause, 1236 .complete = mirror_complete, 1237 }, 1238 .drained_poll = mirror_drained_poll, 1239 }; 1240 1241 static void coroutine_fn 1242 do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, 1243 uint64_t offset, uint64_t bytes, 1244 QEMUIOVector *qiov, int flags) 1245 { 1246 int ret; 1247 size_t qiov_offset = 0; 1248 int64_t bitmap_offset, bitmap_end; 1249 1250 if (!QEMU_IS_ALIGNED(offset, job->granularity) && 1251 bdrv_dirty_bitmap_get(job->dirty_bitmap, offset)) 1252 { 1253 /* 1254 * Dirty unaligned padding: ignore it. 1255 * 1256 * Reasoning: 1257 * 1. If we copy it, we can't reset corresponding bit in 1258 * dirty_bitmap as there may be some "dirty" bytes still not 1259 * copied. 1260 * 2. It's already dirty, so skipping it we don't diverge mirror 1261 * progress. 1262 * 1263 * Note, that because of this, guest write may have no contribution 1264 * into mirror converge, but that's not bad, as we have background 1265 * process of mirroring. If under some bad circumstances (high guest 1266 * IO load) background process starve, we will not converge anyway, 1267 * even if each write will contribute, as guest is not guaranteed to 1268 * rewrite the whole disk. 1269 */ 1270 qiov_offset = QEMU_ALIGN_UP(offset, job->granularity) - offset; 1271 if (bytes <= qiov_offset) { 1272 /* nothing to do after shrink */ 1273 return; 1274 } 1275 offset += qiov_offset; 1276 bytes -= qiov_offset; 1277 } 1278 1279 if (!QEMU_IS_ALIGNED(offset + bytes, job->granularity) && 1280 bdrv_dirty_bitmap_get(job->dirty_bitmap, offset + bytes - 1)) 1281 { 1282 uint64_t tail = (offset + bytes) % job->granularity; 1283 1284 if (bytes <= tail) { 1285 /* nothing to do after shrink */ 1286 return; 1287 } 1288 bytes -= tail; 1289 } 1290 1291 /* 1292 * Tails are either clean or shrunk, so for bitmap resetting 1293 * we safely align the range down. 1294 */ 1295 bitmap_offset = QEMU_ALIGN_UP(offset, job->granularity); 1296 bitmap_end = QEMU_ALIGN_DOWN(offset + bytes, job->granularity); 1297 if (bitmap_offset < bitmap_end) { 1298 bdrv_reset_dirty_bitmap(job->dirty_bitmap, bitmap_offset, 1299 bitmap_end - bitmap_offset); 1300 } 1301 1302 job_progress_increase_remaining(&job->common.job, bytes); 1303 1304 switch (method) { 1305 case MIRROR_METHOD_COPY: 1306 ret = blk_co_pwritev_part(job->target, offset, bytes, 1307 qiov, qiov_offset, flags); 1308 break; 1309 1310 case MIRROR_METHOD_ZERO: 1311 assert(!qiov); 1312 ret = blk_co_pwrite_zeroes(job->target, offset, bytes, flags); 1313 break; 1314 1315 case MIRROR_METHOD_DISCARD: 1316 assert(!qiov); 1317 ret = blk_co_pdiscard(job->target, offset, bytes); 1318 break; 1319 1320 default: 1321 abort(); 1322 } 1323 1324 if (ret >= 0) { 1325 job_progress_update(&job->common.job, bytes); 1326 } else { 1327 BlockErrorAction action; 1328 1329 /* 1330 * We failed, so we should mark dirty the whole area, aligned up. 1331 * Note that we don't care about shrunk tails if any: they were dirty 1332 * at function start, and they must be still dirty, as we've locked 1333 * the region for in-flight op. 1334 */ 1335 bitmap_offset = QEMU_ALIGN_DOWN(offset, job->granularity); 1336 bitmap_end = QEMU_ALIGN_UP(offset + bytes, job->granularity); 1337 bdrv_set_dirty_bitmap(job->dirty_bitmap, bitmap_offset, 1338 bitmap_end - bitmap_offset); 1339 job->actively_synced = false; 1340 1341 action = mirror_error_action(job, false, -ret); 1342 if (action == BLOCK_ERROR_ACTION_REPORT) { 1343 if (!job->ret) { 1344 job->ret = ret; 1345 } 1346 } 1347 } 1348 } 1349 1350 static MirrorOp *coroutine_fn active_write_prepare(MirrorBlockJob *s, 1351 uint64_t offset, 1352 uint64_t bytes) 1353 { 1354 MirrorOp *op; 1355 uint64_t start_chunk = offset / s->granularity; 1356 uint64_t end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); 1357 1358 op = g_new(MirrorOp, 1); 1359 *op = (MirrorOp){ 1360 .s = s, 1361 .offset = offset, 1362 .bytes = bytes, 1363 .is_active_write = true, 1364 .is_in_flight = true, 1365 .co = qemu_coroutine_self(), 1366 }; 1367 qemu_co_queue_init(&op->waiting_requests); 1368 QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); 1369 1370 s->in_active_write_counter++; 1371 1372 mirror_wait_on_conflicts(op, s, offset, bytes); 1373 1374 bitmap_set(s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); 1375 1376 return op; 1377 } 1378 1379 static void coroutine_fn active_write_settle(MirrorOp *op) 1380 { 1381 uint64_t start_chunk = op->offset / op->s->granularity; 1382 uint64_t end_chunk = DIV_ROUND_UP(op->offset + op->bytes, 1383 op->s->granularity); 1384 1385 if (!--op->s->in_active_write_counter && op->s->actively_synced) { 1386 BdrvChild *source = op->s->mirror_top_bs->backing; 1387 1388 if (QLIST_FIRST(&source->bs->parents) == source && 1389 QLIST_NEXT(source, next_parent) == NULL) 1390 { 1391 /* Assert that we are back in sync once all active write 1392 * operations are settled. 1393 * Note that we can only assert this if the mirror node 1394 * is the source node's only parent. */ 1395 assert(!bdrv_get_dirty_count(op->s->dirty_bitmap)); 1396 } 1397 } 1398 bitmap_clear(op->s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); 1399 QTAILQ_REMOVE(&op->s->ops_in_flight, op, next); 1400 qemu_co_queue_restart_all(&op->waiting_requests); 1401 g_free(op); 1402 } 1403 1404 static int coroutine_fn bdrv_mirror_top_preadv(BlockDriverState *bs, 1405 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) 1406 { 1407 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); 1408 } 1409 1410 static int coroutine_fn bdrv_mirror_top_do_write(BlockDriverState *bs, 1411 MirrorMethod method, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, 1412 int flags) 1413 { 1414 MirrorOp *op = NULL; 1415 MirrorBDSOpaque *s = bs->opaque; 1416 int ret = 0; 1417 bool copy_to_target; 1418 1419 copy_to_target = s->job->ret >= 0 && 1420 s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; 1421 1422 if (copy_to_target) { 1423 op = active_write_prepare(s->job, offset, bytes); 1424 } 1425 1426 switch (method) { 1427 case MIRROR_METHOD_COPY: 1428 ret = bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags); 1429 break; 1430 1431 case MIRROR_METHOD_ZERO: 1432 ret = bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags); 1433 break; 1434 1435 case MIRROR_METHOD_DISCARD: 1436 ret = bdrv_co_pdiscard(bs->backing, offset, bytes); 1437 break; 1438 1439 default: 1440 abort(); 1441 } 1442 1443 if (ret < 0) { 1444 goto out; 1445 } 1446 1447 if (copy_to_target) { 1448 do_sync_target_write(s->job, method, offset, bytes, qiov, flags); 1449 } 1450 1451 out: 1452 if (copy_to_target) { 1453 active_write_settle(op); 1454 } 1455 return ret; 1456 } 1457 1458 static int coroutine_fn bdrv_mirror_top_pwritev(BlockDriverState *bs, 1459 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) 1460 { 1461 MirrorBDSOpaque *s = bs->opaque; 1462 QEMUIOVector bounce_qiov; 1463 void *bounce_buf; 1464 int ret = 0; 1465 bool copy_to_target; 1466 1467 copy_to_target = s->job->ret >= 0 && 1468 s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; 1469 1470 if (copy_to_target) { 1471 /* The guest might concurrently modify the data to write; but 1472 * the data on source and destination must match, so we have 1473 * to use a bounce buffer if we are going to write to the 1474 * target now. */ 1475 bounce_buf = qemu_blockalign(bs, bytes); 1476 iov_to_buf_full(qiov->iov, qiov->niov, 0, bounce_buf, bytes); 1477 1478 qemu_iovec_init(&bounce_qiov, 1); 1479 qemu_iovec_add(&bounce_qiov, bounce_buf, bytes); 1480 qiov = &bounce_qiov; 1481 } 1482 1483 ret = bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, offset, bytes, qiov, 1484 flags); 1485 1486 if (copy_to_target) { 1487 qemu_iovec_destroy(&bounce_qiov); 1488 qemu_vfree(bounce_buf); 1489 } 1490 1491 return ret; 1492 } 1493 1494 static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs) 1495 { 1496 if (bs->backing == NULL) { 1497 /* we can be here after failed bdrv_append in mirror_start_job */ 1498 return 0; 1499 } 1500 return bdrv_co_flush(bs->backing->bs); 1501 } 1502 1503 static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs, 1504 int64_t offset, int bytes, BdrvRequestFlags flags) 1505 { 1506 return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_ZERO, offset, bytes, NULL, 1507 flags); 1508 } 1509 1510 static int coroutine_fn bdrv_mirror_top_pdiscard(BlockDriverState *bs, 1511 int64_t offset, int bytes) 1512 { 1513 return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_DISCARD, offset, bytes, 1514 NULL, 0); 1515 } 1516 1517 static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs) 1518 { 1519 if (bs->backing == NULL) { 1520 /* we can be here after failed bdrv_attach_child in 1521 * bdrv_set_backing_hd */ 1522 return; 1523 } 1524 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), 1525 bs->backing->bs->filename); 1526 } 1527 1528 static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c, 1529 BdrvChildRole role, 1530 BlockReopenQueue *reopen_queue, 1531 uint64_t perm, uint64_t shared, 1532 uint64_t *nperm, uint64_t *nshared) 1533 { 1534 MirrorBDSOpaque *s = bs->opaque; 1535 1536 if (s->stop) { 1537 /* 1538 * If the job is to be stopped, we do not need to forward 1539 * anything to the real image. 1540 */ 1541 *nperm = 0; 1542 *nshared = BLK_PERM_ALL; 1543 return; 1544 } 1545 1546 bdrv_default_perms(bs, c, role, reopen_queue, 1547 perm, shared, nperm, nshared); 1548 1549 if (s->is_commit) { 1550 /* 1551 * For commit jobs, we cannot take CONSISTENT_READ, because 1552 * that permission is unshared for everything above the base 1553 * node (except for filters on the base node). 1554 * We also have to force-share the WRITE permission, or 1555 * otherwise we would block ourselves at the base node (if 1556 * writes are blocked for a node, they are also blocked for 1557 * its backing file). 1558 * (We could also share RESIZE, because it may be needed for 1559 * the target if its size is less than the top node's; but 1560 * bdrv_default_perms_for_cow() automatically shares RESIZE 1561 * for backing nodes if WRITE is shared, so there is no need 1562 * to do it here.) 1563 */ 1564 *nperm &= ~BLK_PERM_CONSISTENT_READ; 1565 *nshared |= BLK_PERM_WRITE; 1566 } 1567 } 1568 1569 /* Dummy node that provides consistent read to its users without requiring it 1570 * from its backing file and that allows writes on the backing file chain. */ 1571 static BlockDriver bdrv_mirror_top = { 1572 .format_name = "mirror_top", 1573 .bdrv_co_preadv = bdrv_mirror_top_preadv, 1574 .bdrv_co_pwritev = bdrv_mirror_top_pwritev, 1575 .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes, 1576 .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard, 1577 .bdrv_co_flush = bdrv_mirror_top_flush, 1578 .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename, 1579 .bdrv_child_perm = bdrv_mirror_top_child_perm, 1580 1581 .is_filter = true, 1582 }; 1583 1584 static BlockJob *mirror_start_job( 1585 const char *job_id, BlockDriverState *bs, 1586 int creation_flags, BlockDriverState *target, 1587 const char *replaces, int64_t speed, 1588 uint32_t granularity, int64_t buf_size, 1589 BlockMirrorBackingMode backing_mode, 1590 bool zero_target, 1591 BlockdevOnError on_source_error, 1592 BlockdevOnError on_target_error, 1593 bool unmap, 1594 BlockCompletionFunc *cb, 1595 void *opaque, 1596 const BlockJobDriver *driver, 1597 bool is_none_mode, BlockDriverState *base, 1598 bool auto_complete, const char *filter_node_name, 1599 bool is_mirror, MirrorCopyMode copy_mode, 1600 Error **errp) 1601 { 1602 MirrorBlockJob *s; 1603 MirrorBDSOpaque *bs_opaque; 1604 BlockDriverState *mirror_top_bs; 1605 bool target_is_backing; 1606 uint64_t target_perms, target_shared_perms; 1607 int ret; 1608 1609 if (granularity == 0) { 1610 granularity = bdrv_get_default_bitmap_granularity(target); 1611 } 1612 1613 assert(is_power_of_2(granularity)); 1614 1615 if (buf_size < 0) { 1616 error_setg(errp, "Invalid parameter 'buf-size'"); 1617 return NULL; 1618 } 1619 1620 if (buf_size == 0) { 1621 buf_size = DEFAULT_MIRROR_BUF_SIZE; 1622 } 1623 1624 if (bdrv_skip_filters(bs) == bdrv_skip_filters(target)) { 1625 error_setg(errp, "Can't mirror node into itself"); 1626 return NULL; 1627 } 1628 1629 target_is_backing = bdrv_chain_contains(bs, target); 1630 1631 /* In the case of active commit, add dummy driver to provide consistent 1632 * reads on the top, while disabling it in the intermediate nodes, and make 1633 * the backing chain writable. */ 1634 mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name, 1635 BDRV_O_RDWR, errp); 1636 if (mirror_top_bs == NULL) { 1637 return NULL; 1638 } 1639 if (!filter_node_name) { 1640 mirror_top_bs->implicit = true; 1641 } 1642 1643 /* So that we can always drop this node */ 1644 mirror_top_bs->never_freeze = true; 1645 1646 mirror_top_bs->total_sectors = bs->total_sectors; 1647 mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; 1648 mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | 1649 BDRV_REQ_NO_FALLBACK; 1650 bs_opaque = g_new0(MirrorBDSOpaque, 1); 1651 mirror_top_bs->opaque = bs_opaque; 1652 1653 bs_opaque->is_commit = target_is_backing; 1654 1655 bdrv_drained_begin(bs); 1656 ret = bdrv_append(mirror_top_bs, bs, errp); 1657 bdrv_drained_end(bs); 1658 1659 if (ret < 0) { 1660 bdrv_unref(mirror_top_bs); 1661 return NULL; 1662 } 1663 1664 /* Make sure that the source is not resized while the job is running */ 1665 s = block_job_create(job_id, driver, NULL, mirror_top_bs, 1666 BLK_PERM_CONSISTENT_READ, 1667 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | 1668 BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed, 1669 creation_flags, cb, opaque, errp); 1670 if (!s) { 1671 goto fail; 1672 } 1673 bs_opaque->job = s; 1674 1675 /* The block job now has a reference to this node */ 1676 bdrv_unref(mirror_top_bs); 1677 1678 s->mirror_top_bs = mirror_top_bs; 1679 1680 /* No resize for the target either; while the mirror is still running, a 1681 * consistent read isn't necessarily possible. We could possibly allow 1682 * writes and graph modifications, though it would likely defeat the 1683 * purpose of a mirror, so leave them blocked for now. 1684 * 1685 * In the case of active commit, things look a bit different, though, 1686 * because the target is an already populated backing file in active use. 1687 * We can allow anything except resize there.*/ 1688 1689 target_perms = BLK_PERM_WRITE; 1690 target_shared_perms = BLK_PERM_WRITE_UNCHANGED; 1691 1692 if (target_is_backing) { 1693 int64_t bs_size, target_size; 1694 bs_size = bdrv_getlength(bs); 1695 if (bs_size < 0) { 1696 error_setg_errno(errp, -bs_size, 1697 "Could not inquire top image size"); 1698 goto fail; 1699 } 1700 1701 target_size = bdrv_getlength(target); 1702 if (target_size < 0) { 1703 error_setg_errno(errp, -target_size, 1704 "Could not inquire base image size"); 1705 goto fail; 1706 } 1707 1708 if (target_size < bs_size) { 1709 target_perms |= BLK_PERM_RESIZE; 1710 } 1711 1712 target_shared_perms |= BLK_PERM_CONSISTENT_READ 1713 | BLK_PERM_WRITE 1714 | BLK_PERM_GRAPH_MOD; 1715 } else if (bdrv_chain_contains(bs, bdrv_skip_filters(target))) { 1716 /* 1717 * We may want to allow this in the future, but it would 1718 * require taking some extra care. 1719 */ 1720 error_setg(errp, "Cannot mirror to a filter on top of a node in the " 1721 "source's backing chain"); 1722 goto fail; 1723 } 1724 1725 if (backing_mode != MIRROR_LEAVE_BACKING_CHAIN) { 1726 target_perms |= BLK_PERM_GRAPH_MOD; 1727 } 1728 1729 s->target = blk_new(s->common.job.aio_context, 1730 target_perms, target_shared_perms); 1731 ret = blk_insert_bs(s->target, target, errp); 1732 if (ret < 0) { 1733 goto fail; 1734 } 1735 if (is_mirror) { 1736 /* XXX: Mirror target could be a NBD server of target QEMU in the case 1737 * of non-shared block migration. To allow migration completion, we 1738 * have to allow "inactivate" of the target BB. When that happens, we 1739 * know the job is drained, and the vcpus are stopped, so no write 1740 * operation will be performed. Block layer already has assertions to 1741 * ensure that. */ 1742 blk_set_force_allow_inactivate(s->target); 1743 } 1744 blk_set_allow_aio_context_change(s->target, true); 1745 blk_set_disable_request_queuing(s->target, true); 1746 1747 s->replaces = g_strdup(replaces); 1748 s->on_source_error = on_source_error; 1749 s->on_target_error = on_target_error; 1750 s->is_none_mode = is_none_mode; 1751 s->backing_mode = backing_mode; 1752 s->zero_target = zero_target; 1753 s->copy_mode = copy_mode; 1754 s->base = base; 1755 s->base_overlay = bdrv_find_overlay(bs, base); 1756 s->granularity = granularity; 1757 s->buf_size = ROUND_UP(buf_size, granularity); 1758 s->unmap = unmap; 1759 if (auto_complete) { 1760 s->should_complete = true; 1761 } 1762 1763 s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); 1764 if (!s->dirty_bitmap) { 1765 goto fail; 1766 } 1767 if (s->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING) { 1768 bdrv_disable_dirty_bitmap(s->dirty_bitmap); 1769 } 1770 1771 ret = block_job_add_bdrv(&s->common, "source", bs, 0, 1772 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE | 1773 BLK_PERM_CONSISTENT_READ, 1774 errp); 1775 if (ret < 0) { 1776 goto fail; 1777 } 1778 1779 /* Required permissions are already taken with blk_new() */ 1780 block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL, 1781 &error_abort); 1782 1783 /* In commit_active_start() all intermediate nodes disappear, so 1784 * any jobs in them must be blocked */ 1785 if (target_is_backing) { 1786 BlockDriverState *iter, *filtered_target; 1787 uint64_t iter_shared_perms; 1788 1789 /* 1790 * The topmost node with 1791 * bdrv_skip_filters(filtered_target) == bdrv_skip_filters(target) 1792 */ 1793 filtered_target = bdrv_cow_bs(bdrv_find_overlay(bs, target)); 1794 1795 assert(bdrv_skip_filters(filtered_target) == 1796 bdrv_skip_filters(target)); 1797 1798 /* 1799 * XXX BLK_PERM_WRITE needs to be allowed so we don't block 1800 * ourselves at s->base (if writes are blocked for a node, they are 1801 * also blocked for its backing file). The other options would be a 1802 * second filter driver above s->base (== target). 1803 */ 1804 iter_shared_perms = BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE; 1805 1806 for (iter = bdrv_filter_or_cow_bs(bs); iter != target; 1807 iter = bdrv_filter_or_cow_bs(iter)) 1808 { 1809 if (iter == filtered_target) { 1810 /* 1811 * From here on, all nodes are filters on the base. 1812 * This allows us to share BLK_PERM_CONSISTENT_READ. 1813 */ 1814 iter_shared_perms |= BLK_PERM_CONSISTENT_READ; 1815 } 1816 1817 ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0, 1818 iter_shared_perms, errp); 1819 if (ret < 0) { 1820 goto fail; 1821 } 1822 } 1823 1824 if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) { 1825 goto fail; 1826 } 1827 } 1828 1829 QTAILQ_INIT(&s->ops_in_flight); 1830 1831 trace_mirror_start(bs, s, opaque); 1832 job_start(&s->common.job); 1833 1834 return &s->common; 1835 1836 fail: 1837 if (s) { 1838 /* Make sure this BDS does not go away until we have completed the graph 1839 * changes below */ 1840 bdrv_ref(mirror_top_bs); 1841 1842 g_free(s->replaces); 1843 blk_unref(s->target); 1844 bs_opaque->job = NULL; 1845 if (s->dirty_bitmap) { 1846 bdrv_release_dirty_bitmap(s->dirty_bitmap); 1847 } 1848 job_early_fail(&s->common.job); 1849 } 1850 1851 bs_opaque->stop = true; 1852 bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, 1853 &error_abort); 1854 bdrv_replace_node(mirror_top_bs, mirror_top_bs->backing->bs, &error_abort); 1855 1856 bdrv_unref(mirror_top_bs); 1857 1858 return NULL; 1859 } 1860 1861 void mirror_start(const char *job_id, BlockDriverState *bs, 1862 BlockDriverState *target, const char *replaces, 1863 int creation_flags, int64_t speed, 1864 uint32_t granularity, int64_t buf_size, 1865 MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, 1866 bool zero_target, 1867 BlockdevOnError on_source_error, 1868 BlockdevOnError on_target_error, 1869 bool unmap, const char *filter_node_name, 1870 MirrorCopyMode copy_mode, Error **errp) 1871 { 1872 bool is_none_mode; 1873 BlockDriverState *base; 1874 1875 if ((mode == MIRROR_SYNC_MODE_INCREMENTAL) || 1876 (mode == MIRROR_SYNC_MODE_BITMAP)) { 1877 error_setg(errp, "Sync mode '%s' not supported", 1878 MirrorSyncMode_str(mode)); 1879 return; 1880 } 1881 is_none_mode = mode == MIRROR_SYNC_MODE_NONE; 1882 base = mode == MIRROR_SYNC_MODE_TOP ? bdrv_backing_chain_next(bs) : NULL; 1883 mirror_start_job(job_id, bs, creation_flags, target, replaces, 1884 speed, granularity, buf_size, backing_mode, zero_target, 1885 on_source_error, on_target_error, unmap, NULL, NULL, 1886 &mirror_job_driver, is_none_mode, base, false, 1887 filter_node_name, true, copy_mode, errp); 1888 } 1889 1890 BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs, 1891 BlockDriverState *base, int creation_flags, 1892 int64_t speed, BlockdevOnError on_error, 1893 const char *filter_node_name, 1894 BlockCompletionFunc *cb, void *opaque, 1895 bool auto_complete, Error **errp) 1896 { 1897 bool base_read_only; 1898 BlockJob *job; 1899 1900 base_read_only = bdrv_is_read_only(base); 1901 1902 if (base_read_only) { 1903 if (bdrv_reopen_set_read_only(base, false, errp) < 0) { 1904 return NULL; 1905 } 1906 } 1907 1908 job = mirror_start_job( 1909 job_id, bs, creation_flags, base, NULL, speed, 0, 0, 1910 MIRROR_LEAVE_BACKING_CHAIN, false, 1911 on_error, on_error, true, cb, opaque, 1912 &commit_active_job_driver, false, base, auto_complete, 1913 filter_node_name, false, MIRROR_COPY_MODE_BACKGROUND, 1914 errp); 1915 if (!job) { 1916 goto error_restore_flags; 1917 } 1918 1919 return job; 1920 1921 error_restore_flags: 1922 /* ignore error and errp for bdrv_reopen, because we want to propagate 1923 * the original error */ 1924 if (base_read_only) { 1925 bdrv_reopen_set_read_only(base, true, NULL); 1926 } 1927 return NULL; 1928 } 1929