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