vdpa: Delete duplicated vdpa_feature_bits entry
[qemu.git] / block / replication.c
blobc67f931f376d7edb8e3847ef2d4b1ee084c0dc8d
1 /*
2 * Replication Block filter
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 * Copyright (c) 2016 Intel Corporation
6 * Copyright (c) 2016 FUJITSU LIMITED
8 * Author:
9 * Wen Congyang <wency@cn.fujitsu.com>
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.
15 #include "qemu/osdep.h"
16 #include "qemu/module.h"
17 #include "qemu/option.h"
18 #include "block/nbd.h"
19 #include "block/blockjob.h"
20 #include "block/block_int.h"
21 #include "block/block_backup.h"
22 #include "sysemu/block-backend.h"
23 #include "qapi/error.h"
24 #include "qapi/qmp/qdict.h"
25 #include "block/replication.h"
27 typedef enum {
28 BLOCK_REPLICATION_NONE, /* block replication is not started */
29 BLOCK_REPLICATION_RUNNING, /* block replication is running */
30 BLOCK_REPLICATION_FAILOVER, /* failover is running in background */
31 BLOCK_REPLICATION_FAILOVER_FAILED, /* failover failed */
32 BLOCK_REPLICATION_DONE, /* block replication is done */
33 } ReplicationStage;
35 typedef struct BDRVReplicationState {
36 ReplicationMode mode;
37 ReplicationStage stage;
38 BlockJob *commit_job;
39 BdrvChild *hidden_disk;
40 BdrvChild *secondary_disk;
41 BlockJob *backup_job;
42 char *top_id;
43 ReplicationState *rs;
44 Error *blocker;
45 bool orig_hidden_read_only;
46 bool orig_secondary_read_only;
47 int error;
48 } BDRVReplicationState;
50 static void replication_start(ReplicationState *rs, ReplicationMode mode,
51 Error **errp);
52 static void replication_do_checkpoint(ReplicationState *rs, Error **errp);
53 static void replication_get_error(ReplicationState *rs, Error **errp);
54 static void replication_stop(ReplicationState *rs, bool failover,
55 Error **errp);
57 #define REPLICATION_MODE "mode"
58 #define REPLICATION_TOP_ID "top-id"
59 static QemuOptsList replication_runtime_opts = {
60 .name = "replication",
61 .head = QTAILQ_HEAD_INITIALIZER(replication_runtime_opts.head),
62 .desc = {
64 .name = REPLICATION_MODE,
65 .type = QEMU_OPT_STRING,
68 .name = REPLICATION_TOP_ID,
69 .type = QEMU_OPT_STRING,
71 { /* end of list */ }
75 static ReplicationOps replication_ops = {
76 .start = replication_start,
77 .checkpoint = replication_do_checkpoint,
78 .get_error = replication_get_error,
79 .stop = replication_stop,
82 static int replication_open(BlockDriverState *bs, QDict *options,
83 int flags, Error **errp)
85 int ret;
86 BDRVReplicationState *s = bs->opaque;
87 QemuOpts *opts = NULL;
88 const char *mode;
89 const char *top_id;
91 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
92 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
93 false, errp);
94 if (!bs->file) {
95 return -EINVAL;
98 ret = -EINVAL;
99 opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
100 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
101 goto fail;
104 mode = qemu_opt_get(opts, REPLICATION_MODE);
105 if (!mode) {
106 error_setg(errp, "Missing the option mode");
107 goto fail;
110 if (!strcmp(mode, "primary")) {
111 s->mode = REPLICATION_MODE_PRIMARY;
112 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
113 if (top_id) {
114 error_setg(errp,
115 "The primary side does not support option top-id");
116 goto fail;
118 } else if (!strcmp(mode, "secondary")) {
119 s->mode = REPLICATION_MODE_SECONDARY;
120 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
121 s->top_id = g_strdup(top_id);
122 if (!s->top_id) {
123 error_setg(errp, "Missing the option top-id");
124 goto fail;
126 } else {
127 error_setg(errp,
128 "The option mode's value should be primary or secondary");
129 goto fail;
132 s->rs = replication_new(bs, &replication_ops);
134 ret = 0;
136 fail:
137 qemu_opts_del(opts);
138 return ret;
141 static void replication_close(BlockDriverState *bs)
143 BDRVReplicationState *s = bs->opaque;
144 Job *commit_job;
145 GLOBAL_STATE_CODE();
147 if (s->stage == BLOCK_REPLICATION_RUNNING) {
148 replication_stop(s->rs, false, NULL);
150 if (s->stage == BLOCK_REPLICATION_FAILOVER) {
151 commit_job = &s->commit_job->job;
152 assert(commit_job->aio_context == qemu_get_current_aio_context());
153 job_cancel_sync(commit_job, false);
156 if (s->mode == REPLICATION_MODE_SECONDARY) {
157 g_free(s->top_id);
160 replication_remove(s->rs);
163 static void replication_child_perm(BlockDriverState *bs, BdrvChild *c,
164 BdrvChildRole role,
165 BlockReopenQueue *reopen_queue,
166 uint64_t perm, uint64_t shared,
167 uint64_t *nperm, uint64_t *nshared)
169 if (role & BDRV_CHILD_PRIMARY) {
170 *nperm = BLK_PERM_CONSISTENT_READ;
171 } else {
172 *nperm = 0;
175 if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
176 *nperm |= BLK_PERM_WRITE;
178 *nshared = BLK_PERM_CONSISTENT_READ
179 | BLK_PERM_WRITE
180 | BLK_PERM_WRITE_UNCHANGED;
181 return;
184 static int64_t replication_getlength(BlockDriverState *bs)
186 return bdrv_getlength(bs->file->bs);
189 static int replication_get_io_status(BDRVReplicationState *s)
191 switch (s->stage) {
192 case BLOCK_REPLICATION_NONE:
193 return -EIO;
194 case BLOCK_REPLICATION_RUNNING:
195 return 0;
196 case BLOCK_REPLICATION_FAILOVER:
197 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
198 case BLOCK_REPLICATION_FAILOVER_FAILED:
199 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
200 case BLOCK_REPLICATION_DONE:
202 * active commit job completes, and active disk and secondary_disk
203 * is swapped, so we can operate bs->file directly
205 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
206 default:
207 abort();
211 static int replication_return_value(BDRVReplicationState *s, int ret)
213 if (s->mode == REPLICATION_MODE_SECONDARY) {
214 return ret;
217 if (ret < 0) {
218 s->error = ret;
219 ret = 0;
222 return ret;
225 static coroutine_fn int replication_co_readv(BlockDriverState *bs,
226 int64_t sector_num,
227 int remaining_sectors,
228 QEMUIOVector *qiov)
230 BDRVReplicationState *s = bs->opaque;
231 int ret;
233 if (s->mode == REPLICATION_MODE_PRIMARY) {
234 /* We only use it to forward primary write requests */
235 return -EIO;
238 ret = replication_get_io_status(s);
239 if (ret < 0) {
240 return ret;
243 ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
244 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
246 return replication_return_value(s, ret);
249 static coroutine_fn int replication_co_writev(BlockDriverState *bs,
250 int64_t sector_num,
251 int remaining_sectors,
252 QEMUIOVector *qiov,
253 int flags)
255 BDRVReplicationState *s = bs->opaque;
256 QEMUIOVector hd_qiov;
257 uint64_t bytes_done = 0;
258 BdrvChild *top = bs->file;
259 BdrvChild *base = s->secondary_disk;
260 BdrvChild *target;
261 int ret;
262 int64_t n;
264 assert(!flags);
265 ret = replication_get_io_status(s);
266 if (ret < 0) {
267 goto out;
270 if (ret == 0) {
271 ret = bdrv_co_pwritev(top, sector_num * BDRV_SECTOR_SIZE,
272 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
273 return replication_return_value(s, ret);
277 * Failover failed, only write to active disk if the sectors
278 * have already been allocated in active disk/hidden disk.
280 qemu_iovec_init(&hd_qiov, qiov->niov);
281 while (remaining_sectors > 0) {
282 int64_t count;
284 ret = bdrv_is_allocated_above(top->bs, base->bs, false,
285 sector_num * BDRV_SECTOR_SIZE,
286 remaining_sectors * BDRV_SECTOR_SIZE,
287 &count);
288 if (ret < 0) {
289 goto out1;
292 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
293 n = count >> BDRV_SECTOR_BITS;
294 qemu_iovec_reset(&hd_qiov);
295 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count);
297 target = ret ? top : base;
298 ret = bdrv_co_pwritev(target, sector_num * BDRV_SECTOR_SIZE,
299 n * BDRV_SECTOR_SIZE, &hd_qiov, 0);
300 if (ret < 0) {
301 goto out1;
304 remaining_sectors -= n;
305 sector_num += n;
306 bytes_done += count;
309 out1:
310 qemu_iovec_destroy(&hd_qiov);
311 out:
312 return ret;
315 static void secondary_do_checkpoint(BlockDriverState *bs, Error **errp)
317 BDRVReplicationState *s = bs->opaque;
318 BdrvChild *active_disk = bs->file;
319 Error *local_err = NULL;
320 int ret;
322 if (!s->backup_job) {
323 error_setg(errp, "Backup job was cancelled unexpectedly");
324 return;
327 backup_do_checkpoint(s->backup_job, &local_err);
328 if (local_err) {
329 error_propagate(errp, local_err);
330 return;
333 if (!active_disk->bs->drv) {
334 error_setg(errp, "Active disk %s is ejected",
335 active_disk->bs->node_name);
336 return;
339 ret = bdrv_make_empty(active_disk, errp);
340 if (ret < 0) {
341 return;
344 if (!s->hidden_disk->bs->drv) {
345 error_setg(errp, "Hidden disk %s is ejected",
346 s->hidden_disk->bs->node_name);
347 return;
350 ret = bdrv_make_empty(s->hidden_disk, errp);
351 if (ret < 0) {
352 return;
356 /* This function is supposed to be called twice:
357 * first with writable = true, then with writable = false.
358 * The first call puts s->hidden_disk and s->secondary_disk in
359 * r/w mode, and the second puts them back in their original state.
361 static void reopen_backing_file(BlockDriverState *bs, bool writable,
362 Error **errp)
364 BDRVReplicationState *s = bs->opaque;
365 BdrvChild *hidden_disk, *secondary_disk;
366 BlockReopenQueue *reopen_queue = NULL;
369 * s->hidden_disk and s->secondary_disk may not be set yet, as they will
370 * only be set after the children are writable.
372 hidden_disk = bs->file->bs->backing;
373 secondary_disk = hidden_disk->bs->backing;
375 if (writable) {
376 s->orig_hidden_read_only = bdrv_is_read_only(hidden_disk->bs);
377 s->orig_secondary_read_only = bdrv_is_read_only(secondary_disk->bs);
380 bdrv_subtree_drained_begin(hidden_disk->bs);
381 bdrv_subtree_drained_begin(secondary_disk->bs);
383 if (s->orig_hidden_read_only) {
384 QDict *opts = qdict_new();
385 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
386 reopen_queue = bdrv_reopen_queue(reopen_queue, hidden_disk->bs,
387 opts, true);
390 if (s->orig_secondary_read_only) {
391 QDict *opts = qdict_new();
392 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
393 reopen_queue = bdrv_reopen_queue(reopen_queue, secondary_disk->bs,
394 opts, true);
397 if (reopen_queue) {
398 AioContext *ctx = bdrv_get_aio_context(bs);
399 if (ctx != qemu_get_aio_context()) {
400 aio_context_release(ctx);
402 bdrv_reopen_multiple(reopen_queue, errp);
403 if (ctx != qemu_get_aio_context()) {
404 aio_context_acquire(ctx);
408 bdrv_subtree_drained_end(hidden_disk->bs);
409 bdrv_subtree_drained_end(secondary_disk->bs);
412 static void backup_job_cleanup(BlockDriverState *bs)
414 BDRVReplicationState *s = bs->opaque;
415 BlockDriverState *top_bs;
417 s->backup_job = NULL;
419 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
420 if (!top_bs) {
421 return;
423 bdrv_op_unblock_all(top_bs, s->blocker);
424 error_free(s->blocker);
425 reopen_backing_file(bs, false, NULL);
428 static void backup_job_completed(void *opaque, int ret)
430 BlockDriverState *bs = opaque;
431 BDRVReplicationState *s = bs->opaque;
433 if (s->stage != BLOCK_REPLICATION_FAILOVER) {
434 /* The backup job is cancelled unexpectedly */
435 s->error = -EIO;
438 backup_job_cleanup(bs);
441 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
443 BdrvChild *child;
445 /* The bs itself is the top_bs */
446 if (top_bs == bs) {
447 return true;
450 /* Iterate over top_bs's children */
451 QLIST_FOREACH(child, &top_bs->children, next) {
452 if (child->bs == bs || check_top_bs(child->bs, bs)) {
453 return true;
457 return false;
460 static void replication_start(ReplicationState *rs, ReplicationMode mode,
461 Error **errp)
463 BlockDriverState *bs = rs->opaque;
464 BDRVReplicationState *s;
465 BlockDriverState *top_bs;
466 BdrvChild *active_disk, *hidden_disk, *secondary_disk;
467 int64_t active_length, hidden_length, disk_length;
468 AioContext *aio_context;
469 Error *local_err = NULL;
470 BackupPerf perf = { .use_copy_range = true, .max_workers = 1 };
472 aio_context = bdrv_get_aio_context(bs);
473 aio_context_acquire(aio_context);
474 s = bs->opaque;
476 if (s->stage == BLOCK_REPLICATION_DONE ||
477 s->stage == BLOCK_REPLICATION_FAILOVER) {
479 * This case happens when a secondary is promoted to primary.
480 * Ignore the request because the secondary side of replication
481 * doesn't have to do anything anymore.
483 aio_context_release(aio_context);
484 return;
487 if (s->stage != BLOCK_REPLICATION_NONE) {
488 error_setg(errp, "Block replication is running or done");
489 aio_context_release(aio_context);
490 return;
493 if (s->mode != mode) {
494 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
495 " but got %d", s->mode, mode);
496 aio_context_release(aio_context);
497 return;
500 switch (s->mode) {
501 case REPLICATION_MODE_PRIMARY:
502 break;
503 case REPLICATION_MODE_SECONDARY:
504 active_disk = bs->file;
505 if (!active_disk || !active_disk->bs || !active_disk->bs->backing) {
506 error_setg(errp, "Active disk doesn't have backing file");
507 aio_context_release(aio_context);
508 return;
511 hidden_disk = active_disk->bs->backing;
512 if (!hidden_disk->bs || !hidden_disk->bs->backing) {
513 error_setg(errp, "Hidden disk doesn't have backing file");
514 aio_context_release(aio_context);
515 return;
518 secondary_disk = hidden_disk->bs->backing;
519 if (!secondary_disk->bs || !bdrv_has_blk(secondary_disk->bs)) {
520 error_setg(errp, "The secondary disk doesn't have block backend");
521 aio_context_release(aio_context);
522 return;
525 /* verify the length */
526 active_length = bdrv_getlength(active_disk->bs);
527 hidden_length = bdrv_getlength(hidden_disk->bs);
528 disk_length = bdrv_getlength(secondary_disk->bs);
529 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
530 active_length != hidden_length || hidden_length != disk_length) {
531 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
532 " are not the same");
533 aio_context_release(aio_context);
534 return;
537 /* Must be true, or the bdrv_getlength() calls would have failed */
538 assert(active_disk->bs->drv && hidden_disk->bs->drv);
540 if (!active_disk->bs->drv->bdrv_make_empty ||
541 !hidden_disk->bs->drv->bdrv_make_empty) {
542 error_setg(errp,
543 "Active disk or hidden disk doesn't support make_empty");
544 aio_context_release(aio_context);
545 return;
548 /* reopen the backing file in r/w mode */
549 reopen_backing_file(bs, true, &local_err);
550 if (local_err) {
551 error_propagate(errp, local_err);
552 aio_context_release(aio_context);
553 return;
556 bdrv_ref(hidden_disk->bs);
557 s->hidden_disk = bdrv_attach_child(bs, hidden_disk->bs, "hidden disk",
558 &child_of_bds, BDRV_CHILD_DATA,
559 &local_err);
560 if (local_err) {
561 error_propagate(errp, local_err);
562 aio_context_release(aio_context);
563 return;
566 bdrv_ref(secondary_disk->bs);
567 s->secondary_disk = bdrv_attach_child(bs, secondary_disk->bs,
568 "secondary disk", &child_of_bds,
569 BDRV_CHILD_DATA, &local_err);
570 if (local_err) {
571 error_propagate(errp, local_err);
572 aio_context_release(aio_context);
573 return;
576 /* start backup job now */
577 error_setg(&s->blocker,
578 "Block device is in use by internal backup job");
580 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
581 if (!top_bs || !bdrv_is_root_node(top_bs) ||
582 !check_top_bs(top_bs, bs)) {
583 error_setg(errp, "No top_bs or it is invalid");
584 reopen_backing_file(bs, false, NULL);
585 aio_context_release(aio_context);
586 return;
588 bdrv_op_block_all(top_bs, s->blocker);
589 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
591 s->backup_job = backup_job_create(
592 NULL, s->secondary_disk->bs, s->hidden_disk->bs,
593 0, MIRROR_SYNC_MODE_NONE, NULL, 0, false, NULL,
594 &perf,
595 BLOCKDEV_ON_ERROR_REPORT,
596 BLOCKDEV_ON_ERROR_REPORT, JOB_INTERNAL,
597 backup_job_completed, bs, NULL, &local_err);
598 if (local_err) {
599 error_propagate(errp, local_err);
600 backup_job_cleanup(bs);
601 aio_context_release(aio_context);
602 return;
604 job_start(&s->backup_job->job);
605 break;
606 default:
607 aio_context_release(aio_context);
608 abort();
611 s->stage = BLOCK_REPLICATION_RUNNING;
613 if (s->mode == REPLICATION_MODE_SECONDARY) {
614 secondary_do_checkpoint(bs, errp);
617 s->error = 0;
618 aio_context_release(aio_context);
621 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
623 BlockDriverState *bs = rs->opaque;
624 BDRVReplicationState *s;
625 AioContext *aio_context;
627 aio_context = bdrv_get_aio_context(bs);
628 aio_context_acquire(aio_context);
629 s = bs->opaque;
631 if (s->stage == BLOCK_REPLICATION_DONE ||
632 s->stage == BLOCK_REPLICATION_FAILOVER) {
634 * This case happens when a secondary was promoted to primary.
635 * Ignore the request because the secondary side of replication
636 * doesn't have to do anything anymore.
638 aio_context_release(aio_context);
639 return;
642 if (s->mode == REPLICATION_MODE_SECONDARY) {
643 secondary_do_checkpoint(bs, errp);
645 aio_context_release(aio_context);
648 static void replication_get_error(ReplicationState *rs, Error **errp)
650 BlockDriverState *bs = rs->opaque;
651 BDRVReplicationState *s;
652 AioContext *aio_context;
654 aio_context = bdrv_get_aio_context(bs);
655 aio_context_acquire(aio_context);
656 s = bs->opaque;
658 if (s->stage == BLOCK_REPLICATION_NONE) {
659 error_setg(errp, "Block replication is not running");
660 aio_context_release(aio_context);
661 return;
664 if (s->error) {
665 error_setg(errp, "I/O error occurred");
666 aio_context_release(aio_context);
667 return;
669 aio_context_release(aio_context);
672 static void replication_done(void *opaque, int ret)
674 BlockDriverState *bs = opaque;
675 BDRVReplicationState *s = bs->opaque;
677 if (ret == 0) {
678 s->stage = BLOCK_REPLICATION_DONE;
680 bdrv_unref_child(bs, s->secondary_disk);
681 s->secondary_disk = NULL;
682 bdrv_unref_child(bs, s->hidden_disk);
683 s->hidden_disk = NULL;
684 s->error = 0;
685 } else {
686 s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
687 s->error = -EIO;
691 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
693 BlockDriverState *bs = rs->opaque;
694 BDRVReplicationState *s;
695 AioContext *aio_context;
697 aio_context = bdrv_get_aio_context(bs);
698 aio_context_acquire(aio_context);
699 s = bs->opaque;
701 if (s->stage == BLOCK_REPLICATION_DONE ||
702 s->stage == BLOCK_REPLICATION_FAILOVER) {
704 * This case happens when a secondary was promoted to primary.
705 * Ignore the request because the secondary side of replication
706 * doesn't have to do anything anymore.
708 aio_context_release(aio_context);
709 return;
712 if (s->stage != BLOCK_REPLICATION_RUNNING) {
713 error_setg(errp, "Block replication is not running");
714 aio_context_release(aio_context);
715 return;
718 switch (s->mode) {
719 case REPLICATION_MODE_PRIMARY:
720 s->stage = BLOCK_REPLICATION_DONE;
721 s->error = 0;
722 break;
723 case REPLICATION_MODE_SECONDARY:
725 * This BDS will be closed, and the job should be completed
726 * before the BDS is closed, because we will access hidden
727 * disk, secondary disk in backup_job_completed().
729 if (s->backup_job) {
730 aio_context_release(aio_context);
731 job_cancel_sync(&s->backup_job->job, true);
732 aio_context_acquire(aio_context);
735 if (!failover) {
736 secondary_do_checkpoint(bs, errp);
737 s->stage = BLOCK_REPLICATION_DONE;
738 aio_context_release(aio_context);
739 return;
742 s->stage = BLOCK_REPLICATION_FAILOVER;
743 s->commit_job = commit_active_start(
744 NULL, bs->file->bs, s->secondary_disk->bs,
745 JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
746 NULL, replication_done, bs, true, errp);
747 break;
748 default:
749 aio_context_release(aio_context);
750 abort();
752 aio_context_release(aio_context);
755 static const char *const replication_strong_runtime_opts[] = {
756 REPLICATION_MODE,
757 REPLICATION_TOP_ID,
759 NULL
762 static BlockDriver bdrv_replication = {
763 .format_name = "replication",
764 .instance_size = sizeof(BDRVReplicationState),
766 .bdrv_open = replication_open,
767 .bdrv_close = replication_close,
768 .bdrv_child_perm = replication_child_perm,
770 .bdrv_getlength = replication_getlength,
771 .bdrv_co_readv = replication_co_readv,
772 .bdrv_co_writev = replication_co_writev,
774 .is_filter = true,
776 .has_variable_length = true,
777 .strong_runtime_opts = replication_strong_runtime_opts,
780 static void bdrv_replication_init(void)
782 bdrv_register(&bdrv_replication);
785 block_init(bdrv_replication_init);