block: Rename BdrvChildRole to BdrvChildClass
[qemu/ar7.git] / block / replication.c
blobaf428c5b66ea17407713917010a390554d5cd7ed
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 "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 BdrvChild *active_disk;
39 BlockJob *commit_job;
40 BdrvChild *hidden_disk;
41 BdrvChild *secondary_disk;
42 BlockJob *backup_job;
43 char *top_id;
44 ReplicationState *rs;
45 Error *blocker;
46 bool orig_hidden_read_only;
47 bool orig_secondary_read_only;
48 int error;
49 } BDRVReplicationState;
51 static void replication_start(ReplicationState *rs, ReplicationMode mode,
52 Error **errp);
53 static void replication_do_checkpoint(ReplicationState *rs, Error **errp);
54 static void replication_get_error(ReplicationState *rs, Error **errp);
55 static void replication_stop(ReplicationState *rs, bool failover,
56 Error **errp);
58 #define REPLICATION_MODE "mode"
59 #define REPLICATION_TOP_ID "top-id"
60 static QemuOptsList replication_runtime_opts = {
61 .name = "replication",
62 .head = QTAILQ_HEAD_INITIALIZER(replication_runtime_opts.head),
63 .desc = {
65 .name = REPLICATION_MODE,
66 .type = QEMU_OPT_STRING,
69 .name = REPLICATION_TOP_ID,
70 .type = QEMU_OPT_STRING,
72 { /* end of list */ }
76 static ReplicationOps replication_ops = {
77 .start = replication_start,
78 .checkpoint = replication_do_checkpoint,
79 .get_error = replication_get_error,
80 .stop = replication_stop,
83 static int replication_open(BlockDriverState *bs, QDict *options,
84 int flags, Error **errp)
86 int ret;
87 BDRVReplicationState *s = bs->opaque;
88 Error *local_err = NULL;
89 QemuOpts *opts = NULL;
90 const char *mode;
91 const char *top_id;
93 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
94 false, errp);
95 if (!bs->file) {
96 return -EINVAL;
99 ret = -EINVAL;
100 opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
101 qemu_opts_absorb_qdict(opts, options, &local_err);
102 if (local_err) {
103 goto fail;
106 mode = qemu_opt_get(opts, REPLICATION_MODE);
107 if (!mode) {
108 error_setg(&local_err, "Missing the option mode");
109 goto fail;
112 if (!strcmp(mode, "primary")) {
113 s->mode = REPLICATION_MODE_PRIMARY;
114 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
115 if (top_id) {
116 error_setg(&local_err, "The primary side does not support option top-id");
117 goto fail;
119 } else if (!strcmp(mode, "secondary")) {
120 s->mode = REPLICATION_MODE_SECONDARY;
121 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
122 s->top_id = g_strdup(top_id);
123 if (!s->top_id) {
124 error_setg(&local_err, "Missing the option top-id");
125 goto fail;
127 } else {
128 error_setg(&local_err,
129 "The option mode's value should be primary or secondary");
130 goto fail;
133 s->rs = replication_new(bs, &replication_ops);
135 ret = 0;
137 fail:
138 qemu_opts_del(opts);
139 error_propagate(errp, local_err);
141 return ret;
144 static void replication_close(BlockDriverState *bs)
146 BDRVReplicationState *s = bs->opaque;
147 Job *commit_job;
149 if (s->stage == BLOCK_REPLICATION_RUNNING) {
150 replication_stop(s->rs, false, NULL);
152 if (s->stage == BLOCK_REPLICATION_FAILOVER) {
153 commit_job = &s->commit_job->job;
154 assert(commit_job->aio_context == qemu_get_current_aio_context());
155 job_cancel_sync(commit_job);
158 if (s->mode == REPLICATION_MODE_SECONDARY) {
159 g_free(s->top_id);
162 replication_remove(s->rs);
165 static void replication_child_perm(BlockDriverState *bs, BdrvChild *c,
166 const BdrvChildClass *child_class,
167 BlockReopenQueue *reopen_queue,
168 uint64_t perm, uint64_t shared,
169 uint64_t *nperm, uint64_t *nshared)
171 *nperm = BLK_PERM_CONSISTENT_READ;
172 if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
173 *nperm |= BLK_PERM_WRITE;
175 *nshared = BLK_PERM_CONSISTENT_READ
176 | BLK_PERM_WRITE
177 | BLK_PERM_WRITE_UNCHANGED;
178 return;
181 static int64_t replication_getlength(BlockDriverState *bs)
183 return bdrv_getlength(bs->file->bs);
186 static int replication_get_io_status(BDRVReplicationState *s)
188 switch (s->stage) {
189 case BLOCK_REPLICATION_NONE:
190 return -EIO;
191 case BLOCK_REPLICATION_RUNNING:
192 return 0;
193 case BLOCK_REPLICATION_FAILOVER:
194 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
195 case BLOCK_REPLICATION_FAILOVER_FAILED:
196 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
197 case BLOCK_REPLICATION_DONE:
199 * active commit job completes, and active disk and secondary_disk
200 * is swapped, so we can operate bs->file directly
202 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
203 default:
204 abort();
208 static int replication_return_value(BDRVReplicationState *s, int ret)
210 if (s->mode == REPLICATION_MODE_SECONDARY) {
211 return ret;
214 if (ret < 0) {
215 s->error = ret;
216 ret = 0;
219 return ret;
222 static coroutine_fn int replication_co_readv(BlockDriverState *bs,
223 int64_t sector_num,
224 int remaining_sectors,
225 QEMUIOVector *qiov)
227 BDRVReplicationState *s = bs->opaque;
228 int ret;
230 if (s->mode == REPLICATION_MODE_PRIMARY) {
231 /* We only use it to forward primary write requests */
232 return -EIO;
235 ret = replication_get_io_status(s);
236 if (ret < 0) {
237 return ret;
240 ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
241 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
243 return replication_return_value(s, ret);
246 static coroutine_fn int replication_co_writev(BlockDriverState *bs,
247 int64_t sector_num,
248 int remaining_sectors,
249 QEMUIOVector *qiov,
250 int flags)
252 BDRVReplicationState *s = bs->opaque;
253 QEMUIOVector hd_qiov;
254 uint64_t bytes_done = 0;
255 BdrvChild *top = bs->file;
256 BdrvChild *base = s->secondary_disk;
257 BdrvChild *target;
258 int ret;
259 int64_t n;
261 assert(!flags);
262 ret = replication_get_io_status(s);
263 if (ret < 0) {
264 goto out;
267 if (ret == 0) {
268 ret = bdrv_co_pwritev(top, sector_num * BDRV_SECTOR_SIZE,
269 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
270 return replication_return_value(s, ret);
274 * Failover failed, only write to active disk if the sectors
275 * have already been allocated in active disk/hidden disk.
277 qemu_iovec_init(&hd_qiov, qiov->niov);
278 while (remaining_sectors > 0) {
279 int64_t count;
281 ret = bdrv_is_allocated_above(top->bs, base->bs, false,
282 sector_num * BDRV_SECTOR_SIZE,
283 remaining_sectors * BDRV_SECTOR_SIZE,
284 &count);
285 if (ret < 0) {
286 goto out1;
289 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
290 n = count >> BDRV_SECTOR_BITS;
291 qemu_iovec_reset(&hd_qiov);
292 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count);
294 target = ret ? top : base;
295 ret = bdrv_co_pwritev(target, sector_num * BDRV_SECTOR_SIZE,
296 n * BDRV_SECTOR_SIZE, &hd_qiov, 0);
297 if (ret < 0) {
298 goto out1;
301 remaining_sectors -= n;
302 sector_num += n;
303 bytes_done += count;
306 out1:
307 qemu_iovec_destroy(&hd_qiov);
308 out:
309 return ret;
312 static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp)
314 Error *local_err = NULL;
315 int ret;
317 if (!s->backup_job) {
318 error_setg(errp, "Backup job was cancelled unexpectedly");
319 return;
322 backup_do_checkpoint(s->backup_job, &local_err);
323 if (local_err) {
324 error_propagate(errp, local_err);
325 return;
328 if (!s->active_disk->bs->drv) {
329 error_setg(errp, "Active disk %s is ejected",
330 s->active_disk->bs->node_name);
331 return;
334 ret = bdrv_make_empty(s->active_disk, errp);
335 if (ret < 0) {
336 return;
339 if (!s->hidden_disk->bs->drv) {
340 error_setg(errp, "Hidden disk %s is ejected",
341 s->hidden_disk->bs->node_name);
342 return;
345 BlockBackend *blk = blk_new(qemu_get_current_aio_context(),
346 BLK_PERM_WRITE, BLK_PERM_ALL);
347 blk_insert_bs(blk, s->hidden_disk->bs, &local_err);
348 if (local_err) {
349 error_propagate(errp, local_err);
350 blk_unref(blk);
351 return;
354 ret = blk_make_empty(blk, errp);
355 blk_unref(blk);
356 if (ret < 0) {
357 return;
361 /* This function is supposed to be called twice:
362 * first with writable = true, then with writable = false.
363 * The first call puts s->hidden_disk and s->secondary_disk in
364 * r/w mode, and the second puts them back in their original state.
366 static void reopen_backing_file(BlockDriverState *bs, bool writable,
367 Error **errp)
369 BDRVReplicationState *s = bs->opaque;
370 BlockReopenQueue *reopen_queue = NULL;
371 Error *local_err = NULL;
373 if (writable) {
374 s->orig_hidden_read_only = bdrv_is_read_only(s->hidden_disk->bs);
375 s->orig_secondary_read_only = bdrv_is_read_only(s->secondary_disk->bs);
378 bdrv_subtree_drained_begin(s->hidden_disk->bs);
379 bdrv_subtree_drained_begin(s->secondary_disk->bs);
381 if (s->orig_hidden_read_only) {
382 QDict *opts = qdict_new();
383 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
384 reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs,
385 opts, true);
388 if (s->orig_secondary_read_only) {
389 QDict *opts = qdict_new();
390 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
391 reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs,
392 opts, true);
395 if (reopen_queue) {
396 bdrv_reopen_multiple(reopen_queue, &local_err);
397 error_propagate(errp, local_err);
400 bdrv_subtree_drained_end(s->hidden_disk->bs);
401 bdrv_subtree_drained_end(s->secondary_disk->bs);
404 static void backup_job_cleanup(BlockDriverState *bs)
406 BDRVReplicationState *s = bs->opaque;
407 BlockDriverState *top_bs;
409 s->backup_job = NULL;
411 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
412 if (!top_bs) {
413 return;
415 bdrv_op_unblock_all(top_bs, s->blocker);
416 error_free(s->blocker);
417 reopen_backing_file(bs, false, NULL);
420 static void backup_job_completed(void *opaque, int ret)
422 BlockDriverState *bs = opaque;
423 BDRVReplicationState *s = bs->opaque;
425 if (s->stage != BLOCK_REPLICATION_FAILOVER) {
426 /* The backup job is cancelled unexpectedly */
427 s->error = -EIO;
430 backup_job_cleanup(bs);
433 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
435 BdrvChild *child;
437 /* The bs itself is the top_bs */
438 if (top_bs == bs) {
439 return true;
442 /* Iterate over top_bs's children */
443 QLIST_FOREACH(child, &top_bs->children, next) {
444 if (child->bs == bs || check_top_bs(child->bs, bs)) {
445 return true;
449 return false;
452 static void replication_start(ReplicationState *rs, ReplicationMode mode,
453 Error **errp)
455 BlockDriverState *bs = rs->opaque;
456 BDRVReplicationState *s;
457 BlockDriverState *top_bs;
458 int64_t active_length, hidden_length, disk_length;
459 AioContext *aio_context;
460 Error *local_err = NULL;
462 aio_context = bdrv_get_aio_context(bs);
463 aio_context_acquire(aio_context);
464 s = bs->opaque;
466 if (s->stage == BLOCK_REPLICATION_DONE ||
467 s->stage == BLOCK_REPLICATION_FAILOVER) {
469 * This case happens when a secondary is promoted to primary.
470 * Ignore the request because the secondary side of replication
471 * doesn't have to do anything anymore.
473 aio_context_release(aio_context);
474 return;
477 if (s->stage != BLOCK_REPLICATION_NONE) {
478 error_setg(errp, "Block replication is running or done");
479 aio_context_release(aio_context);
480 return;
483 if (s->mode != mode) {
484 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
485 " but got %d", s->mode, mode);
486 aio_context_release(aio_context);
487 return;
490 switch (s->mode) {
491 case REPLICATION_MODE_PRIMARY:
492 break;
493 case REPLICATION_MODE_SECONDARY:
494 s->active_disk = bs->file;
495 if (!s->active_disk || !s->active_disk->bs ||
496 !s->active_disk->bs->backing) {
497 error_setg(errp, "Active disk doesn't have backing file");
498 aio_context_release(aio_context);
499 return;
502 s->hidden_disk = s->active_disk->bs->backing;
503 if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) {
504 error_setg(errp, "Hidden disk doesn't have backing file");
505 aio_context_release(aio_context);
506 return;
509 s->secondary_disk = s->hidden_disk->bs->backing;
510 if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) {
511 error_setg(errp, "The secondary disk doesn't have block backend");
512 aio_context_release(aio_context);
513 return;
516 /* verify the length */
517 active_length = bdrv_getlength(s->active_disk->bs);
518 hidden_length = bdrv_getlength(s->hidden_disk->bs);
519 disk_length = bdrv_getlength(s->secondary_disk->bs);
520 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
521 active_length != hidden_length || hidden_length != disk_length) {
522 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
523 " are not the same");
524 aio_context_release(aio_context);
525 return;
528 /* Must be true, or the bdrv_getlength() calls would have failed */
529 assert(s->active_disk->bs->drv && s->hidden_disk->bs->drv);
531 if (!s->active_disk->bs->drv->bdrv_make_empty ||
532 !s->hidden_disk->bs->drv->bdrv_make_empty) {
533 error_setg(errp,
534 "Active disk or hidden disk doesn't support make_empty");
535 aio_context_release(aio_context);
536 return;
539 /* reopen the backing file in r/w mode */
540 reopen_backing_file(bs, true, &local_err);
541 if (local_err) {
542 error_propagate(errp, local_err);
543 aio_context_release(aio_context);
544 return;
547 /* start backup job now */
548 error_setg(&s->blocker,
549 "Block device is in use by internal backup job");
551 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
552 if (!top_bs || !bdrv_is_root_node(top_bs) ||
553 !check_top_bs(top_bs, bs)) {
554 error_setg(errp, "No top_bs or it is invalid");
555 reopen_backing_file(bs, false, NULL);
556 aio_context_release(aio_context);
557 return;
559 bdrv_op_block_all(top_bs, s->blocker);
560 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
562 s->backup_job = backup_job_create(
563 NULL, s->secondary_disk->bs, s->hidden_disk->bs,
564 0, MIRROR_SYNC_MODE_NONE, NULL, 0, false, NULL,
565 BLOCKDEV_ON_ERROR_REPORT,
566 BLOCKDEV_ON_ERROR_REPORT, JOB_INTERNAL,
567 backup_job_completed, bs, NULL, &local_err);
568 if (local_err) {
569 error_propagate(errp, local_err);
570 backup_job_cleanup(bs);
571 aio_context_release(aio_context);
572 return;
574 job_start(&s->backup_job->job);
575 break;
576 default:
577 aio_context_release(aio_context);
578 abort();
581 s->stage = BLOCK_REPLICATION_RUNNING;
583 if (s->mode == REPLICATION_MODE_SECONDARY) {
584 secondary_do_checkpoint(s, errp);
587 s->error = 0;
588 aio_context_release(aio_context);
591 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
593 BlockDriverState *bs = rs->opaque;
594 BDRVReplicationState *s;
595 AioContext *aio_context;
597 aio_context = bdrv_get_aio_context(bs);
598 aio_context_acquire(aio_context);
599 s = bs->opaque;
601 if (s->stage == BLOCK_REPLICATION_DONE ||
602 s->stage == BLOCK_REPLICATION_FAILOVER) {
604 * This case happens when a secondary was promoted to primary.
605 * Ignore the request because the secondary side of replication
606 * doesn't have to do anything anymore.
608 aio_context_release(aio_context);
609 return;
612 if (s->mode == REPLICATION_MODE_SECONDARY) {
613 secondary_do_checkpoint(s, errp);
615 aio_context_release(aio_context);
618 static void replication_get_error(ReplicationState *rs, Error **errp)
620 BlockDriverState *bs = rs->opaque;
621 BDRVReplicationState *s;
622 AioContext *aio_context;
624 aio_context = bdrv_get_aio_context(bs);
625 aio_context_acquire(aio_context);
626 s = bs->opaque;
628 if (s->stage == BLOCK_REPLICATION_NONE) {
629 error_setg(errp, "Block replication is not running");
630 aio_context_release(aio_context);
631 return;
634 if (s->error) {
635 error_setg(errp, "I/O error occurred");
636 aio_context_release(aio_context);
637 return;
639 aio_context_release(aio_context);
642 static void replication_done(void *opaque, int ret)
644 BlockDriverState *bs = opaque;
645 BDRVReplicationState *s = bs->opaque;
647 if (ret == 0) {
648 s->stage = BLOCK_REPLICATION_DONE;
650 s->active_disk = NULL;
651 s->secondary_disk = NULL;
652 s->hidden_disk = NULL;
653 s->error = 0;
654 } else {
655 s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
656 s->error = -EIO;
660 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
662 BlockDriverState *bs = rs->opaque;
663 BDRVReplicationState *s;
664 AioContext *aio_context;
666 aio_context = bdrv_get_aio_context(bs);
667 aio_context_acquire(aio_context);
668 s = bs->opaque;
670 if (s->stage == BLOCK_REPLICATION_DONE ||
671 s->stage == BLOCK_REPLICATION_FAILOVER) {
673 * This case happens when a secondary was promoted to primary.
674 * Ignore the request because the secondary side of replication
675 * doesn't have to do anything anymore.
677 aio_context_release(aio_context);
678 return;
681 if (s->stage != BLOCK_REPLICATION_RUNNING) {
682 error_setg(errp, "Block replication is not running");
683 aio_context_release(aio_context);
684 return;
687 switch (s->mode) {
688 case REPLICATION_MODE_PRIMARY:
689 s->stage = BLOCK_REPLICATION_DONE;
690 s->error = 0;
691 break;
692 case REPLICATION_MODE_SECONDARY:
694 * This BDS will be closed, and the job should be completed
695 * before the BDS is closed, because we will access hidden
696 * disk, secondary disk in backup_job_completed().
698 if (s->backup_job) {
699 job_cancel_sync(&s->backup_job->job);
702 if (!failover) {
703 secondary_do_checkpoint(s, errp);
704 s->stage = BLOCK_REPLICATION_DONE;
705 aio_context_release(aio_context);
706 return;
709 s->stage = BLOCK_REPLICATION_FAILOVER;
710 s->commit_job = commit_active_start(
711 NULL, s->active_disk->bs, s->secondary_disk->bs,
712 JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
713 NULL, replication_done, bs, true, errp);
714 break;
715 default:
716 aio_context_release(aio_context);
717 abort();
719 aio_context_release(aio_context);
722 static const char *const replication_strong_runtime_opts[] = {
723 REPLICATION_MODE,
724 REPLICATION_TOP_ID,
726 NULL
729 static BlockDriver bdrv_replication = {
730 .format_name = "replication",
731 .instance_size = sizeof(BDRVReplicationState),
733 .bdrv_open = replication_open,
734 .bdrv_close = replication_close,
735 .bdrv_child_perm = replication_child_perm,
737 .bdrv_getlength = replication_getlength,
738 .bdrv_co_readv = replication_co_readv,
739 .bdrv_co_writev = replication_co_writev,
741 .is_filter = true,
743 .has_variable_length = true,
744 .strong_runtime_opts = replication_strong_runtime_opts,
747 static void bdrv_replication_init(void)
749 bdrv_register(&bdrv_replication);
752 block_init(bdrv_replication_init);