1 /* 2 * Replication Block filter 3 * 4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. 5 * Copyright (c) 2016 Intel Corporation 6 * Copyright (c) 2016 FUJITSU LIMITED 7 * 8 * Author: 9 * Wen Congyang <wency@cn.fujitsu.com> 10 * 11 * This work is licensed under the terms of the GNU GPL, version 2 or later. 12 * See the COPYING file in the top-level directory. 13 */ 14 15 #include "qemu/osdep.h" 16 #include "qemu-common.h" 17 #include "block/nbd.h" 18 #include "block/blockjob.h" 19 #include "block/block_int.h" 20 #include "block/block_backup.h" 21 #include "sysemu/block-backend.h" 22 #include "qapi/error.h" 23 #include "replication.h" 24 25 typedef struct BDRVReplicationState { 26 ReplicationMode mode; 27 int replication_state; 28 BdrvChild *active_disk; 29 BdrvChild *hidden_disk; 30 BdrvChild *secondary_disk; 31 char *top_id; 32 ReplicationState *rs; 33 Error *blocker; 34 int orig_hidden_flags; 35 int orig_secondary_flags; 36 int error; 37 } BDRVReplicationState; 38 39 enum { 40 BLOCK_REPLICATION_NONE, /* block replication is not started */ 41 BLOCK_REPLICATION_RUNNING, /* block replication is running */ 42 BLOCK_REPLICATION_FAILOVER, /* failover is running in background */ 43 BLOCK_REPLICATION_FAILOVER_FAILED, /* failover failed */ 44 BLOCK_REPLICATION_DONE, /* block replication is done */ 45 }; 46 47 static void replication_start(ReplicationState *rs, ReplicationMode mode, 48 Error **errp); 49 static void replication_do_checkpoint(ReplicationState *rs, Error **errp); 50 static void replication_get_error(ReplicationState *rs, Error **errp); 51 static void replication_stop(ReplicationState *rs, bool failover, 52 Error **errp); 53 54 #define REPLICATION_MODE "mode" 55 #define REPLICATION_TOP_ID "top-id" 56 static QemuOptsList replication_runtime_opts = { 57 .name = "replication", 58 .head = QTAILQ_HEAD_INITIALIZER(replication_runtime_opts.head), 59 .desc = { 60 { 61 .name = REPLICATION_MODE, 62 .type = QEMU_OPT_STRING, 63 }, 64 { 65 .name = REPLICATION_TOP_ID, 66 .type = QEMU_OPT_STRING, 67 }, 68 { /* end of list */ } 69 }, 70 }; 71 72 static ReplicationOps replication_ops = { 73 .start = replication_start, 74 .checkpoint = replication_do_checkpoint, 75 .get_error = replication_get_error, 76 .stop = replication_stop, 77 }; 78 79 static int replication_open(BlockDriverState *bs, QDict *options, 80 int flags, Error **errp) 81 { 82 int ret; 83 BDRVReplicationState *s = bs->opaque; 84 Error *local_err = NULL; 85 QemuOpts *opts = NULL; 86 const char *mode; 87 const char *top_id; 88 89 ret = -EINVAL; 90 opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort); 91 qemu_opts_absorb_qdict(opts, options, &local_err); 92 if (local_err) { 93 goto fail; 94 } 95 96 mode = qemu_opt_get(opts, REPLICATION_MODE); 97 if (!mode) { 98 error_setg(&local_err, "Missing the option mode"); 99 goto fail; 100 } 101 102 if (!strcmp(mode, "primary")) { 103 s->mode = REPLICATION_MODE_PRIMARY; 104 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID); 105 if (top_id) { 106 error_setg(&local_err, "The primary side does not support option top-id"); 107 goto fail; 108 } 109 } else if (!strcmp(mode, "secondary")) { 110 s->mode = REPLICATION_MODE_SECONDARY; 111 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID); 112 s->top_id = g_strdup(top_id); 113 if (!s->top_id) { 114 error_setg(&local_err, "Missing the option top-id"); 115 goto fail; 116 } 117 } else { 118 error_setg(&local_err, 119 "The option mode's value should be primary or secondary"); 120 goto fail; 121 } 122 123 s->rs = replication_new(bs, &replication_ops); 124 125 ret = 0; 126 127 fail: 128 qemu_opts_del(opts); 129 error_propagate(errp, local_err); 130 131 return ret; 132 } 133 134 static void replication_close(BlockDriverState *bs) 135 { 136 BDRVReplicationState *s = bs->opaque; 137 138 if (s->replication_state == BLOCK_REPLICATION_RUNNING) { 139 replication_stop(s->rs, false, NULL); 140 } 141 142 if (s->mode == REPLICATION_MODE_SECONDARY) { 143 g_free(s->top_id); 144 } 145 146 replication_remove(s->rs); 147 } 148 149 static int64_t replication_getlength(BlockDriverState *bs) 150 { 151 return bdrv_getlength(bs->file->bs); 152 } 153 154 static int replication_get_io_status(BDRVReplicationState *s) 155 { 156 switch (s->replication_state) { 157 case BLOCK_REPLICATION_NONE: 158 return -EIO; 159 case BLOCK_REPLICATION_RUNNING: 160 return 0; 161 case BLOCK_REPLICATION_FAILOVER: 162 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0; 163 case BLOCK_REPLICATION_FAILOVER_FAILED: 164 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1; 165 case BLOCK_REPLICATION_DONE: 166 /* 167 * active commit job completes, and active disk and secondary_disk 168 * is swapped, so we can operate bs->file directly 169 */ 170 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0; 171 default: 172 abort(); 173 } 174 } 175 176 static int replication_return_value(BDRVReplicationState *s, int ret) 177 { 178 if (s->mode == REPLICATION_MODE_SECONDARY) { 179 return ret; 180 } 181 182 if (ret < 0) { 183 s->error = ret; 184 ret = 0; 185 } 186 187 return ret; 188 } 189 190 static coroutine_fn int replication_co_readv(BlockDriverState *bs, 191 int64_t sector_num, 192 int remaining_sectors, 193 QEMUIOVector *qiov) 194 { 195 BDRVReplicationState *s = bs->opaque; 196 BdrvChild *child = s->secondary_disk; 197 BlockJob *job = NULL; 198 CowRequest req; 199 int ret; 200 201 if (s->mode == REPLICATION_MODE_PRIMARY) { 202 /* We only use it to forward primary write requests */ 203 return -EIO; 204 } 205 206 ret = replication_get_io_status(s); 207 if (ret < 0) { 208 return ret; 209 } 210 211 if (child && child->bs) { 212 job = child->bs->job; 213 } 214 215 if (job) { 216 backup_wait_for_overlapping_requests(child->bs->job, sector_num, 217 remaining_sectors); 218 backup_cow_request_begin(&req, child->bs->job, sector_num, 219 remaining_sectors); 220 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, 221 qiov); 222 backup_cow_request_end(&req); 223 goto out; 224 } 225 226 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, qiov); 227 out: 228 return replication_return_value(s, ret); 229 } 230 231 static coroutine_fn int replication_co_writev(BlockDriverState *bs, 232 int64_t sector_num, 233 int remaining_sectors, 234 QEMUIOVector *qiov) 235 { 236 BDRVReplicationState *s = bs->opaque; 237 QEMUIOVector hd_qiov; 238 uint64_t bytes_done = 0; 239 BdrvChild *top = bs->file; 240 BdrvChild *base = s->secondary_disk; 241 BdrvChild *target; 242 int ret, n; 243 244 ret = replication_get_io_status(s); 245 if (ret < 0) { 246 goto out; 247 } 248 249 if (ret == 0) { 250 ret = bdrv_co_writev(top, sector_num, 251 remaining_sectors, qiov); 252 return replication_return_value(s, ret); 253 } 254 255 /* 256 * Failover failed, only write to active disk if the sectors 257 * have already been allocated in active disk/hidden disk. 258 */ 259 qemu_iovec_init(&hd_qiov, qiov->niov); 260 while (remaining_sectors > 0) { 261 ret = bdrv_is_allocated_above(top->bs, base->bs, sector_num, 262 remaining_sectors, &n); 263 if (ret < 0) { 264 goto out1; 265 } 266 267 qemu_iovec_reset(&hd_qiov); 268 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, n * BDRV_SECTOR_SIZE); 269 270 target = ret ? top : base; 271 ret = bdrv_co_writev(target, sector_num, n, &hd_qiov); 272 if (ret < 0) { 273 goto out1; 274 } 275 276 remaining_sectors -= n; 277 sector_num += n; 278 bytes_done += n * BDRV_SECTOR_SIZE; 279 } 280 281 out1: 282 qemu_iovec_destroy(&hd_qiov); 283 out: 284 return ret; 285 } 286 287 static bool replication_recurse_is_first_non_filter(BlockDriverState *bs, 288 BlockDriverState *candidate) 289 { 290 return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate); 291 } 292 293 static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp) 294 { 295 Error *local_err = NULL; 296 int ret; 297 298 if (!s->secondary_disk->bs->job) { 299 error_setg(errp, "Backup job was cancelled unexpectedly"); 300 return; 301 } 302 303 backup_do_checkpoint(s->secondary_disk->bs->job, &local_err); 304 if (local_err) { 305 error_propagate(errp, local_err); 306 return; 307 } 308 309 ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs); 310 if (ret < 0) { 311 error_setg(errp, "Cannot make active disk empty"); 312 return; 313 } 314 315 ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs); 316 if (ret < 0) { 317 error_setg(errp, "Cannot make hidden disk empty"); 318 return; 319 } 320 } 321 322 static void reopen_backing_file(BDRVReplicationState *s, bool writable, 323 Error **errp) 324 { 325 BlockReopenQueue *reopen_queue = NULL; 326 int orig_hidden_flags, orig_secondary_flags; 327 int new_hidden_flags, new_secondary_flags; 328 Error *local_err = NULL; 329 330 if (writable) { 331 orig_hidden_flags = s->orig_hidden_flags = 332 bdrv_get_flags(s->hidden_disk->bs); 333 new_hidden_flags = (orig_hidden_flags | BDRV_O_RDWR) & 334 ~BDRV_O_INACTIVE; 335 orig_secondary_flags = s->orig_secondary_flags = 336 bdrv_get_flags(s->secondary_disk->bs); 337 new_secondary_flags = (orig_secondary_flags | BDRV_O_RDWR) & 338 ~BDRV_O_INACTIVE; 339 } else { 340 orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) & 341 ~BDRV_O_INACTIVE; 342 new_hidden_flags = s->orig_hidden_flags; 343 orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) & 344 ~BDRV_O_INACTIVE; 345 new_secondary_flags = s->orig_secondary_flags; 346 } 347 348 if (orig_hidden_flags != new_hidden_flags) { 349 reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL, 350 new_hidden_flags); 351 } 352 353 if (!(orig_secondary_flags & BDRV_O_RDWR)) { 354 reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs, 355 NULL, new_secondary_flags); 356 } 357 358 if (reopen_queue) { 359 bdrv_reopen_multiple(reopen_queue, &local_err); 360 error_propagate(errp, local_err); 361 } 362 } 363 364 static void backup_job_cleanup(BDRVReplicationState *s) 365 { 366 BlockDriverState *top_bs; 367 368 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL); 369 if (!top_bs) { 370 return; 371 } 372 bdrv_op_unblock_all(top_bs, s->blocker); 373 error_free(s->blocker); 374 reopen_backing_file(s, false, NULL); 375 } 376 377 static void backup_job_completed(void *opaque, int ret) 378 { 379 BDRVReplicationState *s = opaque; 380 381 if (s->replication_state != BLOCK_REPLICATION_FAILOVER) { 382 /* The backup job is cancelled unexpectedly */ 383 s->error = -EIO; 384 } 385 386 backup_job_cleanup(s); 387 } 388 389 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs) 390 { 391 BdrvChild *child; 392 393 /* The bs itself is the top_bs */ 394 if (top_bs == bs) { 395 return true; 396 } 397 398 /* Iterate over top_bs's children */ 399 QLIST_FOREACH(child, &top_bs->children, next) { 400 if (child->bs == bs || check_top_bs(child->bs, bs)) { 401 return true; 402 } 403 } 404 405 return false; 406 } 407 408 static void replication_start(ReplicationState *rs, ReplicationMode mode, 409 Error **errp) 410 { 411 BlockDriverState *bs = rs->opaque; 412 BDRVReplicationState *s; 413 BlockDriverState *top_bs; 414 int64_t active_length, hidden_length, disk_length; 415 AioContext *aio_context; 416 Error *local_err = NULL; 417 418 aio_context = bdrv_get_aio_context(bs); 419 aio_context_acquire(aio_context); 420 s = bs->opaque; 421 422 if (s->replication_state != BLOCK_REPLICATION_NONE) { 423 error_setg(errp, "Block replication is running or done"); 424 aio_context_release(aio_context); 425 return; 426 } 427 428 if (s->mode != mode) { 429 error_setg(errp, "The parameter mode's value is invalid, needs %d," 430 " but got %d", s->mode, mode); 431 aio_context_release(aio_context); 432 return; 433 } 434 435 switch (s->mode) { 436 case REPLICATION_MODE_PRIMARY: 437 break; 438 case REPLICATION_MODE_SECONDARY: 439 s->active_disk = bs->file; 440 if (!s->active_disk || !s->active_disk->bs || 441 !s->active_disk->bs->backing) { 442 error_setg(errp, "Active disk doesn't have backing file"); 443 aio_context_release(aio_context); 444 return; 445 } 446 447 s->hidden_disk = s->active_disk->bs->backing; 448 if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) { 449 error_setg(errp, "Hidden disk doesn't have backing file"); 450 aio_context_release(aio_context); 451 return; 452 } 453 454 s->secondary_disk = s->hidden_disk->bs->backing; 455 if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) { 456 error_setg(errp, "The secondary disk doesn't have block backend"); 457 aio_context_release(aio_context); 458 return; 459 } 460 461 /* verify the length */ 462 active_length = bdrv_getlength(s->active_disk->bs); 463 hidden_length = bdrv_getlength(s->hidden_disk->bs); 464 disk_length = bdrv_getlength(s->secondary_disk->bs); 465 if (active_length < 0 || hidden_length < 0 || disk_length < 0 || 466 active_length != hidden_length || hidden_length != disk_length) { 467 error_setg(errp, "Active disk, hidden disk, secondary disk's length" 468 " are not the same"); 469 aio_context_release(aio_context); 470 return; 471 } 472 473 if (!s->active_disk->bs->drv->bdrv_make_empty || 474 !s->hidden_disk->bs->drv->bdrv_make_empty) { 475 error_setg(errp, 476 "Active disk or hidden disk doesn't support make_empty"); 477 aio_context_release(aio_context); 478 return; 479 } 480 481 /* reopen the backing file in r/w mode */ 482 reopen_backing_file(s, true, &local_err); 483 if (local_err) { 484 error_propagate(errp, local_err); 485 aio_context_release(aio_context); 486 return; 487 } 488 489 /* start backup job now */ 490 error_setg(&s->blocker, 491 "Block device is in use by internal backup job"); 492 493 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL); 494 if (!top_bs || !bdrv_is_root_node(top_bs) || 495 !check_top_bs(top_bs, bs)) { 496 error_setg(errp, "No top_bs or it is invalid"); 497 reopen_backing_file(s, false, NULL); 498 aio_context_release(aio_context); 499 return; 500 } 501 bdrv_op_block_all(top_bs, s->blocker); 502 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker); 503 504 backup_start("replication-backup", s->secondary_disk->bs, 505 s->hidden_disk->bs, 0, MIRROR_SYNC_MODE_NONE, NULL, false, 506 BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT, 507 backup_job_completed, s, NULL, &local_err); 508 if (local_err) { 509 error_propagate(errp, local_err); 510 backup_job_cleanup(s); 511 aio_context_release(aio_context); 512 return; 513 } 514 break; 515 default: 516 aio_context_release(aio_context); 517 abort(); 518 } 519 520 s->replication_state = BLOCK_REPLICATION_RUNNING; 521 522 if (s->mode == REPLICATION_MODE_SECONDARY) { 523 secondary_do_checkpoint(s, errp); 524 } 525 526 s->error = 0; 527 aio_context_release(aio_context); 528 } 529 530 static void replication_do_checkpoint(ReplicationState *rs, Error **errp) 531 { 532 BlockDriverState *bs = rs->opaque; 533 BDRVReplicationState *s; 534 AioContext *aio_context; 535 536 aio_context = bdrv_get_aio_context(bs); 537 aio_context_acquire(aio_context); 538 s = bs->opaque; 539 540 if (s->mode == REPLICATION_MODE_SECONDARY) { 541 secondary_do_checkpoint(s, errp); 542 } 543 aio_context_release(aio_context); 544 } 545 546 static void replication_get_error(ReplicationState *rs, Error **errp) 547 { 548 BlockDriverState *bs = rs->opaque; 549 BDRVReplicationState *s; 550 AioContext *aio_context; 551 552 aio_context = bdrv_get_aio_context(bs); 553 aio_context_acquire(aio_context); 554 s = bs->opaque; 555 556 if (s->replication_state != BLOCK_REPLICATION_RUNNING) { 557 error_setg(errp, "Block replication is not running"); 558 aio_context_release(aio_context); 559 return; 560 } 561 562 if (s->error) { 563 error_setg(errp, "I/O error occurred"); 564 aio_context_release(aio_context); 565 return; 566 } 567 aio_context_release(aio_context); 568 } 569 570 static void replication_done(void *opaque, int ret) 571 { 572 BlockDriverState *bs = opaque; 573 BDRVReplicationState *s = bs->opaque; 574 575 if (ret == 0) { 576 s->replication_state = BLOCK_REPLICATION_DONE; 577 578 /* refresh top bs's filename */ 579 bdrv_refresh_filename(bs); 580 s->active_disk = NULL; 581 s->secondary_disk = NULL; 582 s->hidden_disk = NULL; 583 s->error = 0; 584 } else { 585 s->replication_state = BLOCK_REPLICATION_FAILOVER_FAILED; 586 s->error = -EIO; 587 } 588 } 589 590 static void replication_stop(ReplicationState *rs, bool failover, Error **errp) 591 { 592 BlockDriverState *bs = rs->opaque; 593 BDRVReplicationState *s; 594 AioContext *aio_context; 595 596 aio_context = bdrv_get_aio_context(bs); 597 aio_context_acquire(aio_context); 598 s = bs->opaque; 599 600 if (s->replication_state != BLOCK_REPLICATION_RUNNING) { 601 error_setg(errp, "Block replication is not running"); 602 aio_context_release(aio_context); 603 return; 604 } 605 606 switch (s->mode) { 607 case REPLICATION_MODE_PRIMARY: 608 s->replication_state = BLOCK_REPLICATION_DONE; 609 s->error = 0; 610 break; 611 case REPLICATION_MODE_SECONDARY: 612 /* 613 * This BDS will be closed, and the job should be completed 614 * before the BDS is closed, because we will access hidden 615 * disk, secondary disk in backup_job_completed(). 616 */ 617 if (s->secondary_disk->bs->job) { 618 block_job_cancel_sync(s->secondary_disk->bs->job); 619 } 620 621 if (!failover) { 622 secondary_do_checkpoint(s, errp); 623 s->replication_state = BLOCK_REPLICATION_DONE; 624 aio_context_release(aio_context); 625 return; 626 } 627 628 s->replication_state = BLOCK_REPLICATION_FAILOVER; 629 commit_active_start("replication-commit", s->active_disk->bs, 630 s->secondary_disk->bs, 0, BLOCKDEV_ON_ERROR_REPORT, 631 replication_done, 632 bs, errp, true); 633 break; 634 default: 635 aio_context_release(aio_context); 636 abort(); 637 } 638 aio_context_release(aio_context); 639 } 640 641 BlockDriver bdrv_replication = { 642 .format_name = "replication", 643 .protocol_name = "replication", 644 .instance_size = sizeof(BDRVReplicationState), 645 646 .bdrv_open = replication_open, 647 .bdrv_close = replication_close, 648 649 .bdrv_getlength = replication_getlength, 650 .bdrv_co_readv = replication_co_readv, 651 .bdrv_co_writev = replication_co_writev, 652 653 .is_filter = true, 654 .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter, 655 656 .has_variable_length = true, 657 }; 658 659 static void bdrv_replication_init(void) 660 { 661 bdrv_register(&bdrv_replication); 662 } 663 664 block_init(bdrv_replication_init); 665