xref: /openbmc/qemu/block/replication.c (revision cc19f177)
129ff7890SWen Congyang /*
229ff7890SWen Congyang  * Replication Block filter
329ff7890SWen Congyang  *
429ff7890SWen Congyang  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
529ff7890SWen Congyang  * Copyright (c) 2016 Intel Corporation
629ff7890SWen Congyang  * Copyright (c) 2016 FUJITSU LIMITED
729ff7890SWen Congyang  *
829ff7890SWen Congyang  * Author:
929ff7890SWen Congyang  *   Wen Congyang <wency@cn.fujitsu.com>
1029ff7890SWen Congyang  *
1129ff7890SWen Congyang  * This work is licensed under the terms of the GNU GPL, version 2 or later.
1229ff7890SWen Congyang  * See the COPYING file in the top-level directory.
1329ff7890SWen Congyang  */
1429ff7890SWen Congyang 
1529ff7890SWen Congyang #include "qemu/osdep.h"
160b8fa32fSMarkus Armbruster #include "qemu/module.h"
17922a01a0SMarkus Armbruster #include "qemu/option.h"
1829ff7890SWen Congyang #include "block/nbd.h"
1929ff7890SWen Congyang #include "block/blockjob.h"
2029ff7890SWen Congyang #include "block/block_int.h"
2129ff7890SWen Congyang #include "block/block_backup.h"
2229ff7890SWen Congyang #include "sysemu/block-backend.h"
2329ff7890SWen Congyang #include "qapi/error.h"
243c4e9647SAlberto Garcia #include "qapi/qmp/qdict.h"
2529ff7890SWen Congyang #include "replication.h"
2629ff7890SWen Congyang 
273c76c606SFam Zheng typedef enum {
283c76c606SFam Zheng     BLOCK_REPLICATION_NONE,             /* block replication is not started */
293c76c606SFam Zheng     BLOCK_REPLICATION_RUNNING,          /* block replication is running */
303c76c606SFam Zheng     BLOCK_REPLICATION_FAILOVER,         /* failover is running in background */
313c76c606SFam Zheng     BLOCK_REPLICATION_FAILOVER_FAILED,  /* failover failed */
323c76c606SFam Zheng     BLOCK_REPLICATION_DONE,             /* block replication is done */
333c76c606SFam Zheng } ReplicationStage;
343c76c606SFam Zheng 
3529ff7890SWen Congyang typedef struct BDRVReplicationState {
3629ff7890SWen Congyang     ReplicationMode mode;
373c76c606SFam Zheng     ReplicationStage stage;
3829ff7890SWen Congyang     BdrvChild *active_disk;
39*cc19f177SVladimir Sementsov-Ogievskiy     BlockJob *commit_job;
4029ff7890SWen Congyang     BdrvChild *hidden_disk;
4129ff7890SWen Congyang     BdrvChild *secondary_disk;
42*cc19f177SVladimir Sementsov-Ogievskiy     BlockJob *backup_job;
4329ff7890SWen Congyang     char *top_id;
4429ff7890SWen Congyang     ReplicationState *rs;
4529ff7890SWen Congyang     Error *blocker;
463c4e9647SAlberto Garcia     bool orig_hidden_read_only;
473c4e9647SAlberto Garcia     bool orig_secondary_read_only;
4829ff7890SWen Congyang     int error;
4929ff7890SWen Congyang } BDRVReplicationState;
5029ff7890SWen Congyang 
5129ff7890SWen Congyang static void replication_start(ReplicationState *rs, ReplicationMode mode,
5229ff7890SWen Congyang                               Error **errp);
5329ff7890SWen Congyang static void replication_do_checkpoint(ReplicationState *rs, Error **errp);
5429ff7890SWen Congyang static void replication_get_error(ReplicationState *rs, Error **errp);
5529ff7890SWen Congyang static void replication_stop(ReplicationState *rs, bool failover,
5629ff7890SWen Congyang                              Error **errp);
5729ff7890SWen Congyang 
5829ff7890SWen Congyang #define REPLICATION_MODE        "mode"
5929ff7890SWen Congyang #define REPLICATION_TOP_ID      "top-id"
6029ff7890SWen Congyang static QemuOptsList replication_runtime_opts = {
6129ff7890SWen Congyang     .name = "replication",
6229ff7890SWen Congyang     .head = QTAILQ_HEAD_INITIALIZER(replication_runtime_opts.head),
6329ff7890SWen Congyang     .desc = {
6429ff7890SWen Congyang         {
6529ff7890SWen Congyang             .name = REPLICATION_MODE,
6629ff7890SWen Congyang             .type = QEMU_OPT_STRING,
6729ff7890SWen Congyang         },
6829ff7890SWen Congyang         {
6929ff7890SWen Congyang             .name = REPLICATION_TOP_ID,
7029ff7890SWen Congyang             .type = QEMU_OPT_STRING,
7129ff7890SWen Congyang         },
7229ff7890SWen Congyang         { /* end of list */ }
7329ff7890SWen Congyang     },
7429ff7890SWen Congyang };
7529ff7890SWen Congyang 
7629ff7890SWen Congyang static ReplicationOps replication_ops = {
7729ff7890SWen Congyang     .start = replication_start,
7829ff7890SWen Congyang     .checkpoint = replication_do_checkpoint,
7929ff7890SWen Congyang     .get_error = replication_get_error,
8029ff7890SWen Congyang     .stop = replication_stop,
8129ff7890SWen Congyang };
8229ff7890SWen Congyang 
8329ff7890SWen Congyang static int replication_open(BlockDriverState *bs, QDict *options,
8429ff7890SWen Congyang                             int flags, Error **errp)
8529ff7890SWen Congyang {
8629ff7890SWen Congyang     int ret;
8729ff7890SWen Congyang     BDRVReplicationState *s = bs->opaque;
8829ff7890SWen Congyang     Error *local_err = NULL;
8929ff7890SWen Congyang     QemuOpts *opts = NULL;
9029ff7890SWen Congyang     const char *mode;
9129ff7890SWen Congyang     const char *top_id;
9229ff7890SWen Congyang 
934e4bf5c4SKevin Wolf     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
944e4bf5c4SKevin Wolf                                false, errp);
954e4bf5c4SKevin Wolf     if (!bs->file) {
964e4bf5c4SKevin Wolf         return -EINVAL;
974e4bf5c4SKevin Wolf     }
984e4bf5c4SKevin Wolf 
9929ff7890SWen Congyang     ret = -EINVAL;
10029ff7890SWen Congyang     opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
10129ff7890SWen Congyang     qemu_opts_absorb_qdict(opts, options, &local_err);
10229ff7890SWen Congyang     if (local_err) {
10329ff7890SWen Congyang         goto fail;
10429ff7890SWen Congyang     }
10529ff7890SWen Congyang 
10629ff7890SWen Congyang     mode = qemu_opt_get(opts, REPLICATION_MODE);
10729ff7890SWen Congyang     if (!mode) {
10829ff7890SWen Congyang         error_setg(&local_err, "Missing the option mode");
10929ff7890SWen Congyang         goto fail;
11029ff7890SWen Congyang     }
11129ff7890SWen Congyang 
11229ff7890SWen Congyang     if (!strcmp(mode, "primary")) {
11329ff7890SWen Congyang         s->mode = REPLICATION_MODE_PRIMARY;
114f4f2539bSChanglong Xie         top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
115f4f2539bSChanglong Xie         if (top_id) {
116f4f2539bSChanglong Xie             error_setg(&local_err, "The primary side does not support option top-id");
117f4f2539bSChanglong Xie             goto fail;
118f4f2539bSChanglong Xie         }
11929ff7890SWen Congyang     } else if (!strcmp(mode, "secondary")) {
12029ff7890SWen Congyang         s->mode = REPLICATION_MODE_SECONDARY;
12129ff7890SWen Congyang         top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
12229ff7890SWen Congyang         s->top_id = g_strdup(top_id);
12329ff7890SWen Congyang         if (!s->top_id) {
12429ff7890SWen Congyang             error_setg(&local_err, "Missing the option top-id");
12529ff7890SWen Congyang             goto fail;
12629ff7890SWen Congyang         }
12729ff7890SWen Congyang     } else {
12829ff7890SWen Congyang         error_setg(&local_err,
12929ff7890SWen Congyang                    "The option mode's value should be primary or secondary");
13029ff7890SWen Congyang         goto fail;
13129ff7890SWen Congyang     }
13229ff7890SWen Congyang 
13329ff7890SWen Congyang     s->rs = replication_new(bs, &replication_ops);
13429ff7890SWen Congyang 
13529ff7890SWen Congyang     ret = 0;
13629ff7890SWen Congyang 
13729ff7890SWen Congyang fail:
13829ff7890SWen Congyang     qemu_opts_del(opts);
13929ff7890SWen Congyang     error_propagate(errp, local_err);
14029ff7890SWen Congyang 
14129ff7890SWen Congyang     return ret;
14229ff7890SWen Congyang }
14329ff7890SWen Congyang 
14429ff7890SWen Congyang static void replication_close(BlockDriverState *bs)
14529ff7890SWen Congyang {
14629ff7890SWen Congyang     BDRVReplicationState *s = bs->opaque;
14729ff7890SWen Congyang 
1483c76c606SFam Zheng     if (s->stage == BLOCK_REPLICATION_RUNNING) {
14929ff7890SWen Congyang         replication_stop(s->rs, false, NULL);
15029ff7890SWen Congyang     }
1513c76c606SFam Zheng     if (s->stage == BLOCK_REPLICATION_FAILOVER) {
152*cc19f177SVladimir Sementsov-Ogievskiy         job_cancel_sync(&s->commit_job->job);
15350ab0e09SPaolo Bonzini     }
15429ff7890SWen Congyang 
15529ff7890SWen Congyang     if (s->mode == REPLICATION_MODE_SECONDARY) {
15629ff7890SWen Congyang         g_free(s->top_id);
15729ff7890SWen Congyang     }
15829ff7890SWen Congyang 
15929ff7890SWen Congyang     replication_remove(s->rs);
16029ff7890SWen Congyang }
16129ff7890SWen Congyang 
16237a9051cSChanglong Xie static void replication_child_perm(BlockDriverState *bs, BdrvChild *c,
16337a9051cSChanglong Xie                                    const BdrvChildRole *role,
164e0995dc3SKevin Wolf                                    BlockReopenQueue *reopen_queue,
16537a9051cSChanglong Xie                                    uint64_t perm, uint64_t shared,
16637a9051cSChanglong Xie                                    uint64_t *nperm, uint64_t *nshared)
16737a9051cSChanglong Xie {
168611e0653SWang Guang     *nperm = BLK_PERM_CONSISTENT_READ;
169611e0653SWang Guang     if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
170611e0653SWang Guang         *nperm |= BLK_PERM_WRITE;
171611e0653SWang Guang     }
172611e0653SWang Guang     *nshared = BLK_PERM_CONSISTENT_READ \
17337a9051cSChanglong Xie                | BLK_PERM_WRITE \
17437a9051cSChanglong Xie                | BLK_PERM_WRITE_UNCHANGED;
17537a9051cSChanglong Xie     return;
17637a9051cSChanglong Xie }
17737a9051cSChanglong Xie 
17829ff7890SWen Congyang static int64_t replication_getlength(BlockDriverState *bs)
17929ff7890SWen Congyang {
18029ff7890SWen Congyang     return bdrv_getlength(bs->file->bs);
18129ff7890SWen Congyang }
18229ff7890SWen Congyang 
18329ff7890SWen Congyang static int replication_get_io_status(BDRVReplicationState *s)
18429ff7890SWen Congyang {
1853c76c606SFam Zheng     switch (s->stage) {
18629ff7890SWen Congyang     case BLOCK_REPLICATION_NONE:
18729ff7890SWen Congyang         return -EIO;
18829ff7890SWen Congyang     case BLOCK_REPLICATION_RUNNING:
18929ff7890SWen Congyang         return 0;
19029ff7890SWen Congyang     case BLOCK_REPLICATION_FAILOVER:
19129ff7890SWen Congyang         return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
19229ff7890SWen Congyang     case BLOCK_REPLICATION_FAILOVER_FAILED:
19329ff7890SWen Congyang         return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
19429ff7890SWen Congyang     case BLOCK_REPLICATION_DONE:
19529ff7890SWen Congyang         /*
19629ff7890SWen Congyang          * active commit job completes, and active disk and secondary_disk
19729ff7890SWen Congyang          * is swapped, so we can operate bs->file directly
19829ff7890SWen Congyang          */
19929ff7890SWen Congyang         return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
20029ff7890SWen Congyang     default:
20129ff7890SWen Congyang         abort();
20229ff7890SWen Congyang     }
20329ff7890SWen Congyang }
20429ff7890SWen Congyang 
20529ff7890SWen Congyang static int replication_return_value(BDRVReplicationState *s, int ret)
20629ff7890SWen Congyang {
20729ff7890SWen Congyang     if (s->mode == REPLICATION_MODE_SECONDARY) {
20829ff7890SWen Congyang         return ret;
20929ff7890SWen Congyang     }
21029ff7890SWen Congyang 
21129ff7890SWen Congyang     if (ret < 0) {
21229ff7890SWen Congyang         s->error = ret;
21329ff7890SWen Congyang         ret = 0;
21429ff7890SWen Congyang     }
21529ff7890SWen Congyang 
21629ff7890SWen Congyang     return ret;
21729ff7890SWen Congyang }
21829ff7890SWen Congyang 
21929ff7890SWen Congyang static coroutine_fn int replication_co_readv(BlockDriverState *bs,
22029ff7890SWen Congyang                                              int64_t sector_num,
22129ff7890SWen Congyang                                              int remaining_sectors,
22229ff7890SWen Congyang                                              QEMUIOVector *qiov)
22329ff7890SWen Congyang {
22429ff7890SWen Congyang     BDRVReplicationState *s = bs->opaque;
22529ff7890SWen Congyang     int ret;
22629ff7890SWen Congyang 
22729ff7890SWen Congyang     if (s->mode == REPLICATION_MODE_PRIMARY) {
22829ff7890SWen Congyang         /* We only use it to forward primary write requests */
22929ff7890SWen Congyang         return -EIO;
23029ff7890SWen Congyang     }
23129ff7890SWen Congyang 
23229ff7890SWen Congyang     ret = replication_get_io_status(s);
23329ff7890SWen Congyang     if (ret < 0) {
23429ff7890SWen Congyang         return ret;
23529ff7890SWen Congyang     }
23629ff7890SWen Congyang 
23704a11d87SEric Blake     ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
23804a11d87SEric Blake                          remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
239e4f9752cSVladimir Sementsov-Ogievskiy 
24029ff7890SWen Congyang     return replication_return_value(s, ret);
24129ff7890SWen Congyang }
24229ff7890SWen Congyang 
24329ff7890SWen Congyang static coroutine_fn int replication_co_writev(BlockDriverState *bs,
24429ff7890SWen Congyang                                               int64_t sector_num,
24529ff7890SWen Congyang                                               int remaining_sectors,
246e18a58b4SEric Blake                                               QEMUIOVector *qiov,
247e18a58b4SEric Blake                                               int flags)
24829ff7890SWen Congyang {
24929ff7890SWen Congyang     BDRVReplicationState *s = bs->opaque;
25029ff7890SWen Congyang     QEMUIOVector hd_qiov;
25129ff7890SWen Congyang     uint64_t bytes_done = 0;
25229ff7890SWen Congyang     BdrvChild *top = bs->file;
25329ff7890SWen Congyang     BdrvChild *base = s->secondary_disk;
25429ff7890SWen Congyang     BdrvChild *target;
25551b0a488SEric Blake     int ret;
25651b0a488SEric Blake     int64_t n;
25729ff7890SWen Congyang 
258e18a58b4SEric Blake     assert(!flags);
25929ff7890SWen Congyang     ret = replication_get_io_status(s);
26029ff7890SWen Congyang     if (ret < 0) {
26129ff7890SWen Congyang         goto out;
26229ff7890SWen Congyang     }
26329ff7890SWen Congyang 
26429ff7890SWen Congyang     if (ret == 0) {
26504a11d87SEric Blake         ret = bdrv_co_pwritev(top, sector_num * BDRV_SECTOR_SIZE,
26604a11d87SEric Blake                               remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
26729ff7890SWen Congyang         return replication_return_value(s, ret);
26829ff7890SWen Congyang     }
26929ff7890SWen Congyang 
27029ff7890SWen Congyang     /*
27129ff7890SWen Congyang      * Failover failed, only write to active disk if the sectors
27229ff7890SWen Congyang      * have already been allocated in active disk/hidden disk.
27329ff7890SWen Congyang      */
27429ff7890SWen Congyang     qemu_iovec_init(&hd_qiov, qiov->niov);
27529ff7890SWen Congyang     while (remaining_sectors > 0) {
27651b0a488SEric Blake         int64_t count;
27751b0a488SEric Blake 
27851b0a488SEric Blake         ret = bdrv_is_allocated_above(top->bs, base->bs,
27951b0a488SEric Blake                                       sector_num * BDRV_SECTOR_SIZE,
28051b0a488SEric Blake                                       remaining_sectors * BDRV_SECTOR_SIZE,
28151b0a488SEric Blake                                       &count);
28229ff7890SWen Congyang         if (ret < 0) {
28329ff7890SWen Congyang             goto out1;
28429ff7890SWen Congyang         }
28529ff7890SWen Congyang 
28651b0a488SEric Blake         assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
28751b0a488SEric Blake         n = count >> BDRV_SECTOR_BITS;
28829ff7890SWen Congyang         qemu_iovec_reset(&hd_qiov);
28951b0a488SEric Blake         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count);
29029ff7890SWen Congyang 
29129ff7890SWen Congyang         target = ret ? top : base;
29204a11d87SEric Blake         ret = bdrv_co_pwritev(target, sector_num * BDRV_SECTOR_SIZE,
29304a11d87SEric Blake                               n * BDRV_SECTOR_SIZE, &hd_qiov, 0);
29429ff7890SWen Congyang         if (ret < 0) {
29529ff7890SWen Congyang             goto out1;
29629ff7890SWen Congyang         }
29729ff7890SWen Congyang 
29829ff7890SWen Congyang         remaining_sectors -= n;
29929ff7890SWen Congyang         sector_num += n;
30051b0a488SEric Blake         bytes_done += count;
30129ff7890SWen Congyang     }
30229ff7890SWen Congyang 
30329ff7890SWen Congyang out1:
30429ff7890SWen Congyang     qemu_iovec_destroy(&hd_qiov);
30529ff7890SWen Congyang out:
30629ff7890SWen Congyang     return ret;
30729ff7890SWen Congyang }
30829ff7890SWen Congyang 
30929ff7890SWen Congyang static bool replication_recurse_is_first_non_filter(BlockDriverState *bs,
31029ff7890SWen Congyang                                                     BlockDriverState *candidate)
31129ff7890SWen Congyang {
31229ff7890SWen Congyang     return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
31329ff7890SWen Congyang }
31429ff7890SWen Congyang 
31529ff7890SWen Congyang static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp)
31629ff7890SWen Congyang {
31729ff7890SWen Congyang     Error *local_err = NULL;
31829ff7890SWen Congyang     int ret;
31929ff7890SWen Congyang 
320*cc19f177SVladimir Sementsov-Ogievskiy     if (!s->backup_job) {
32129ff7890SWen Congyang         error_setg(errp, "Backup job was cancelled unexpectedly");
32229ff7890SWen Congyang         return;
32329ff7890SWen Congyang     }
32429ff7890SWen Congyang 
325*cc19f177SVladimir Sementsov-Ogievskiy     backup_do_checkpoint(s->backup_job, &local_err);
32629ff7890SWen Congyang     if (local_err) {
32729ff7890SWen Congyang         error_propagate(errp, local_err);
32829ff7890SWen Congyang         return;
32929ff7890SWen Congyang     }
33029ff7890SWen Congyang 
331d470ad42SMax Reitz     if (!s->active_disk->bs->drv) {
332d470ad42SMax Reitz         error_setg(errp, "Active disk %s is ejected",
333d470ad42SMax Reitz                    s->active_disk->bs->node_name);
334d470ad42SMax Reitz         return;
335d470ad42SMax Reitz     }
336d470ad42SMax Reitz 
33729ff7890SWen Congyang     ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs);
33829ff7890SWen Congyang     if (ret < 0) {
33929ff7890SWen Congyang         error_setg(errp, "Cannot make active disk empty");
34029ff7890SWen Congyang         return;
34129ff7890SWen Congyang     }
34229ff7890SWen Congyang 
343d470ad42SMax Reitz     if (!s->hidden_disk->bs->drv) {
344d470ad42SMax Reitz         error_setg(errp, "Hidden disk %s is ejected",
345d470ad42SMax Reitz                    s->hidden_disk->bs->node_name);
346d470ad42SMax Reitz         return;
347d470ad42SMax Reitz     }
348d470ad42SMax Reitz 
34929ff7890SWen Congyang     ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs);
35029ff7890SWen Congyang     if (ret < 0) {
35129ff7890SWen Congyang         error_setg(errp, "Cannot make hidden disk empty");
35229ff7890SWen Congyang         return;
35329ff7890SWen Congyang     }
35429ff7890SWen Congyang }
35529ff7890SWen Congyang 
3563c4e9647SAlberto Garcia /* This function is supposed to be called twice:
3573c4e9647SAlberto Garcia  * first with writable = true, then with writable = false.
3583c4e9647SAlberto Garcia  * The first call puts s->hidden_disk and s->secondary_disk in
3593c4e9647SAlberto Garcia  * r/w mode, and the second puts them back in their original state.
3603c4e9647SAlberto Garcia  */
3618dd9006eSPaolo Bonzini static void reopen_backing_file(BlockDriverState *bs, bool writable,
36229ff7890SWen Congyang                                 Error **errp)
36329ff7890SWen Congyang {
3648dd9006eSPaolo Bonzini     BDRVReplicationState *s = bs->opaque;
36529ff7890SWen Congyang     BlockReopenQueue *reopen_queue = NULL;
36629ff7890SWen Congyang     Error *local_err = NULL;
36729ff7890SWen Congyang 
36829ff7890SWen Congyang     if (writable) {
3693c4e9647SAlberto Garcia         s->orig_hidden_read_only = bdrv_is_read_only(s->hidden_disk->bs);
3703c4e9647SAlberto Garcia         s->orig_secondary_read_only = bdrv_is_read_only(s->secondary_disk->bs);
37129ff7890SWen Congyang     }
37229ff7890SWen Congyang 
3731a63a907SKevin Wolf     bdrv_subtree_drained_begin(s->hidden_disk->bs);
3741a63a907SKevin Wolf     bdrv_subtree_drained_begin(s->secondary_disk->bs);
3751a63a907SKevin Wolf 
3763c4e9647SAlberto Garcia     if (s->orig_hidden_read_only) {
3773c4e9647SAlberto Garcia         QDict *opts = qdict_new();
3783c4e9647SAlberto Garcia         qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
3793c4e9647SAlberto Garcia         reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs,
380077e8e20SAlberto Garcia                                          opts, true);
38129ff7890SWen Congyang     }
38229ff7890SWen Congyang 
3833c4e9647SAlberto Garcia     if (s->orig_secondary_read_only) {
3843c4e9647SAlberto Garcia         QDict *opts = qdict_new();
3853c4e9647SAlberto Garcia         qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
38629ff7890SWen Congyang         reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs,
387077e8e20SAlberto Garcia                                          opts, true);
38829ff7890SWen Congyang     }
38929ff7890SWen Congyang 
39029ff7890SWen Congyang     if (reopen_queue) {
3915019aeceSAlberto Garcia         bdrv_reopen_multiple(reopen_queue, &local_err);
39229ff7890SWen Congyang         error_propagate(errp, local_err);
39329ff7890SWen Congyang     }
3941a63a907SKevin Wolf 
3951a63a907SKevin Wolf     bdrv_subtree_drained_end(s->hidden_disk->bs);
3961a63a907SKevin Wolf     bdrv_subtree_drained_end(s->secondary_disk->bs);
39729ff7890SWen Congyang }
39829ff7890SWen Congyang 
3998dd9006eSPaolo Bonzini static void backup_job_cleanup(BlockDriverState *bs)
40029ff7890SWen Congyang {
4018dd9006eSPaolo Bonzini     BDRVReplicationState *s = bs->opaque;
40229ff7890SWen Congyang     BlockDriverState *top_bs;
40329ff7890SWen Congyang 
40429ff7890SWen Congyang     top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
40529ff7890SWen Congyang     if (!top_bs) {
40629ff7890SWen Congyang         return;
40729ff7890SWen Congyang     }
40829ff7890SWen Congyang     bdrv_op_unblock_all(top_bs, s->blocker);
40929ff7890SWen Congyang     error_free(s->blocker);
4108dd9006eSPaolo Bonzini     reopen_backing_file(bs, false, NULL);
41129ff7890SWen Congyang }
41229ff7890SWen Congyang 
41329ff7890SWen Congyang static void backup_job_completed(void *opaque, int ret)
41429ff7890SWen Congyang {
4158dd9006eSPaolo Bonzini     BlockDriverState *bs = opaque;
4168dd9006eSPaolo Bonzini     BDRVReplicationState *s = bs->opaque;
41729ff7890SWen Congyang 
4183c76c606SFam Zheng     if (s->stage != BLOCK_REPLICATION_FAILOVER) {
41929ff7890SWen Congyang         /* The backup job is cancelled unexpectedly */
42029ff7890SWen Congyang         s->error = -EIO;
42129ff7890SWen Congyang     }
42229ff7890SWen Congyang 
4238dd9006eSPaolo Bonzini     backup_job_cleanup(bs);
42429ff7890SWen Congyang }
42529ff7890SWen Congyang 
42629ff7890SWen Congyang static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
42729ff7890SWen Congyang {
42829ff7890SWen Congyang     BdrvChild *child;
42929ff7890SWen Congyang 
43029ff7890SWen Congyang     /* The bs itself is the top_bs */
43129ff7890SWen Congyang     if (top_bs == bs) {
43229ff7890SWen Congyang         return true;
43329ff7890SWen Congyang     }
43429ff7890SWen Congyang 
43529ff7890SWen Congyang     /* Iterate over top_bs's children */
43629ff7890SWen Congyang     QLIST_FOREACH(child, &top_bs->children, next) {
43729ff7890SWen Congyang         if (child->bs == bs || check_top_bs(child->bs, bs)) {
43829ff7890SWen Congyang             return true;
43929ff7890SWen Congyang         }
44029ff7890SWen Congyang     }
44129ff7890SWen Congyang 
44229ff7890SWen Congyang     return false;
44329ff7890SWen Congyang }
44429ff7890SWen Congyang 
44529ff7890SWen Congyang static void replication_start(ReplicationState *rs, ReplicationMode mode,
44629ff7890SWen Congyang                               Error **errp)
44729ff7890SWen Congyang {
44829ff7890SWen Congyang     BlockDriverState *bs = rs->opaque;
44929ff7890SWen Congyang     BDRVReplicationState *s;
45029ff7890SWen Congyang     BlockDriverState *top_bs;
45129ff7890SWen Congyang     int64_t active_length, hidden_length, disk_length;
45229ff7890SWen Congyang     AioContext *aio_context;
45329ff7890SWen Congyang     Error *local_err = NULL;
45429ff7890SWen Congyang 
45529ff7890SWen Congyang     aio_context = bdrv_get_aio_context(bs);
45629ff7890SWen Congyang     aio_context_acquire(aio_context);
45729ff7890SWen Congyang     s = bs->opaque;
45829ff7890SWen Congyang 
4593c76c606SFam Zheng     if (s->stage != BLOCK_REPLICATION_NONE) {
46029ff7890SWen Congyang         error_setg(errp, "Block replication is running or done");
46129ff7890SWen Congyang         aio_context_release(aio_context);
46229ff7890SWen Congyang         return;
46329ff7890SWen Congyang     }
46429ff7890SWen Congyang 
46529ff7890SWen Congyang     if (s->mode != mode) {
46629ff7890SWen Congyang         error_setg(errp, "The parameter mode's value is invalid, needs %d,"
46729ff7890SWen Congyang                    " but got %d", s->mode, mode);
46829ff7890SWen Congyang         aio_context_release(aio_context);
46929ff7890SWen Congyang         return;
47029ff7890SWen Congyang     }
47129ff7890SWen Congyang 
47229ff7890SWen Congyang     switch (s->mode) {
47329ff7890SWen Congyang     case REPLICATION_MODE_PRIMARY:
47429ff7890SWen Congyang         break;
47529ff7890SWen Congyang     case REPLICATION_MODE_SECONDARY:
47629ff7890SWen Congyang         s->active_disk = bs->file;
47729ff7890SWen Congyang         if (!s->active_disk || !s->active_disk->bs ||
47829ff7890SWen Congyang                                     !s->active_disk->bs->backing) {
47929ff7890SWen Congyang             error_setg(errp, "Active disk doesn't have backing file");
48029ff7890SWen Congyang             aio_context_release(aio_context);
48129ff7890SWen Congyang             return;
48229ff7890SWen Congyang         }
48329ff7890SWen Congyang 
48429ff7890SWen Congyang         s->hidden_disk = s->active_disk->bs->backing;
48529ff7890SWen Congyang         if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) {
48629ff7890SWen Congyang             error_setg(errp, "Hidden disk doesn't have backing file");
48729ff7890SWen Congyang             aio_context_release(aio_context);
48829ff7890SWen Congyang             return;
48929ff7890SWen Congyang         }
49029ff7890SWen Congyang 
49129ff7890SWen Congyang         s->secondary_disk = s->hidden_disk->bs->backing;
49229ff7890SWen Congyang         if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) {
49329ff7890SWen Congyang             error_setg(errp, "The secondary disk doesn't have block backend");
49429ff7890SWen Congyang             aio_context_release(aio_context);
49529ff7890SWen Congyang             return;
49629ff7890SWen Congyang         }
49729ff7890SWen Congyang 
49829ff7890SWen Congyang         /* verify the length */
49929ff7890SWen Congyang         active_length = bdrv_getlength(s->active_disk->bs);
50029ff7890SWen Congyang         hidden_length = bdrv_getlength(s->hidden_disk->bs);
50129ff7890SWen Congyang         disk_length = bdrv_getlength(s->secondary_disk->bs);
50229ff7890SWen Congyang         if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
50329ff7890SWen Congyang             active_length != hidden_length || hidden_length != disk_length) {
50429ff7890SWen Congyang             error_setg(errp, "Active disk, hidden disk, secondary disk's length"
50529ff7890SWen Congyang                        " are not the same");
50629ff7890SWen Congyang             aio_context_release(aio_context);
50729ff7890SWen Congyang             return;
50829ff7890SWen Congyang         }
50929ff7890SWen Congyang 
510d470ad42SMax Reitz         /* Must be true, or the bdrv_getlength() calls would have failed */
511d470ad42SMax Reitz         assert(s->active_disk->bs->drv && s->hidden_disk->bs->drv);
512d470ad42SMax Reitz 
51329ff7890SWen Congyang         if (!s->active_disk->bs->drv->bdrv_make_empty ||
51429ff7890SWen Congyang             !s->hidden_disk->bs->drv->bdrv_make_empty) {
51529ff7890SWen Congyang             error_setg(errp,
51629ff7890SWen Congyang                        "Active disk or hidden disk doesn't support make_empty");
51729ff7890SWen Congyang             aio_context_release(aio_context);
51829ff7890SWen Congyang             return;
51929ff7890SWen Congyang         }
52029ff7890SWen Congyang 
52129ff7890SWen Congyang         /* reopen the backing file in r/w mode */
5228dd9006eSPaolo Bonzini         reopen_backing_file(bs, true, &local_err);
52329ff7890SWen Congyang         if (local_err) {
52429ff7890SWen Congyang             error_propagate(errp, local_err);
52529ff7890SWen Congyang             aio_context_release(aio_context);
52629ff7890SWen Congyang             return;
52729ff7890SWen Congyang         }
52829ff7890SWen Congyang 
52929ff7890SWen Congyang         /* start backup job now */
53029ff7890SWen Congyang         error_setg(&s->blocker,
53129ff7890SWen Congyang                    "Block device is in use by internal backup job");
53229ff7890SWen Congyang 
53329ff7890SWen Congyang         top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
53429ff7890SWen Congyang         if (!top_bs || !bdrv_is_root_node(top_bs) ||
53529ff7890SWen Congyang             !check_top_bs(top_bs, bs)) {
53629ff7890SWen Congyang             error_setg(errp, "No top_bs or it is invalid");
5378dd9006eSPaolo Bonzini             reopen_backing_file(bs, false, NULL);
53829ff7890SWen Congyang             aio_context_release(aio_context);
53929ff7890SWen Congyang             return;
54029ff7890SWen Congyang         }
54129ff7890SWen Congyang         bdrv_op_block_all(top_bs, s->blocker);
54229ff7890SWen Congyang         bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
54329ff7890SWen Congyang 
544*cc19f177SVladimir Sementsov-Ogievskiy         s->backup_job = backup_job_create(
545*cc19f177SVladimir Sementsov-Ogievskiy                                 NULL, s->secondary_disk->bs, s->hidden_disk->bs,
546111049a4SJohn Snow                                 0, MIRROR_SYNC_MODE_NONE, NULL, false,
547111049a4SJohn Snow                                 BLOCKDEV_ON_ERROR_REPORT,
548bb02b65cSKevin Wolf                                 BLOCKDEV_ON_ERROR_REPORT, JOB_INTERNAL,
549111049a4SJohn Snow                                 backup_job_completed, bs, NULL, &local_err);
55029ff7890SWen Congyang         if (local_err) {
55129ff7890SWen Congyang             error_propagate(errp, local_err);
5528dd9006eSPaolo Bonzini             backup_job_cleanup(bs);
55329ff7890SWen Congyang             aio_context_release(aio_context);
55429ff7890SWen Congyang             return;
55529ff7890SWen Congyang         }
556*cc19f177SVladimir Sementsov-Ogievskiy         job_start(&s->backup_job->job);
55729ff7890SWen Congyang         break;
55829ff7890SWen Congyang     default:
55929ff7890SWen Congyang         aio_context_release(aio_context);
56029ff7890SWen Congyang         abort();
56129ff7890SWen Congyang     }
56229ff7890SWen Congyang 
5633c76c606SFam Zheng     s->stage = BLOCK_REPLICATION_RUNNING;
56429ff7890SWen Congyang 
56529ff7890SWen Congyang     if (s->mode == REPLICATION_MODE_SECONDARY) {
56629ff7890SWen Congyang         secondary_do_checkpoint(s, errp);
56729ff7890SWen Congyang     }
56829ff7890SWen Congyang 
56929ff7890SWen Congyang     s->error = 0;
57029ff7890SWen Congyang     aio_context_release(aio_context);
57129ff7890SWen Congyang }
57229ff7890SWen Congyang 
57329ff7890SWen Congyang static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
57429ff7890SWen Congyang {
57529ff7890SWen Congyang     BlockDriverState *bs = rs->opaque;
57629ff7890SWen Congyang     BDRVReplicationState *s;
57729ff7890SWen Congyang     AioContext *aio_context;
57829ff7890SWen Congyang 
57929ff7890SWen Congyang     aio_context = bdrv_get_aio_context(bs);
58029ff7890SWen Congyang     aio_context_acquire(aio_context);
58129ff7890SWen Congyang     s = bs->opaque;
58229ff7890SWen Congyang 
58329ff7890SWen Congyang     if (s->mode == REPLICATION_MODE_SECONDARY) {
58429ff7890SWen Congyang         secondary_do_checkpoint(s, errp);
58529ff7890SWen Congyang     }
58629ff7890SWen Congyang     aio_context_release(aio_context);
58729ff7890SWen Congyang }
58829ff7890SWen Congyang 
58929ff7890SWen Congyang static void replication_get_error(ReplicationState *rs, Error **errp)
59029ff7890SWen Congyang {
59129ff7890SWen Congyang     BlockDriverState *bs = rs->opaque;
59229ff7890SWen Congyang     BDRVReplicationState *s;
59329ff7890SWen Congyang     AioContext *aio_context;
59429ff7890SWen Congyang 
59529ff7890SWen Congyang     aio_context = bdrv_get_aio_context(bs);
59629ff7890SWen Congyang     aio_context_acquire(aio_context);
59729ff7890SWen Congyang     s = bs->opaque;
59829ff7890SWen Congyang 
5993c76c606SFam Zheng     if (s->stage != BLOCK_REPLICATION_RUNNING) {
60029ff7890SWen Congyang         error_setg(errp, "Block replication is not running");
60129ff7890SWen Congyang         aio_context_release(aio_context);
60229ff7890SWen Congyang         return;
60329ff7890SWen Congyang     }
60429ff7890SWen Congyang 
60529ff7890SWen Congyang     if (s->error) {
60629ff7890SWen Congyang         error_setg(errp, "I/O error occurred");
60729ff7890SWen Congyang         aio_context_release(aio_context);
60829ff7890SWen Congyang         return;
60929ff7890SWen Congyang     }
61029ff7890SWen Congyang     aio_context_release(aio_context);
61129ff7890SWen Congyang }
61229ff7890SWen Congyang 
61329ff7890SWen Congyang static void replication_done(void *opaque, int ret)
61429ff7890SWen Congyang {
61529ff7890SWen Congyang     BlockDriverState *bs = opaque;
61629ff7890SWen Congyang     BDRVReplicationState *s = bs->opaque;
61729ff7890SWen Congyang 
61829ff7890SWen Congyang     if (ret == 0) {
6193c76c606SFam Zheng         s->stage = BLOCK_REPLICATION_DONE;
62029ff7890SWen Congyang 
62129ff7890SWen Congyang         s->active_disk = NULL;
62229ff7890SWen Congyang         s->secondary_disk = NULL;
62329ff7890SWen Congyang         s->hidden_disk = NULL;
62429ff7890SWen Congyang         s->error = 0;
62529ff7890SWen Congyang     } else {
6263c76c606SFam Zheng         s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
62729ff7890SWen Congyang         s->error = -EIO;
62829ff7890SWen Congyang     }
62929ff7890SWen Congyang }
63029ff7890SWen Congyang 
63129ff7890SWen Congyang static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
63229ff7890SWen Congyang {
63329ff7890SWen Congyang     BlockDriverState *bs = rs->opaque;
63429ff7890SWen Congyang     BDRVReplicationState *s;
63529ff7890SWen Congyang     AioContext *aio_context;
63629ff7890SWen Congyang 
63729ff7890SWen Congyang     aio_context = bdrv_get_aio_context(bs);
63829ff7890SWen Congyang     aio_context_acquire(aio_context);
63929ff7890SWen Congyang     s = bs->opaque;
64029ff7890SWen Congyang 
6413c76c606SFam Zheng     if (s->stage != BLOCK_REPLICATION_RUNNING) {
64229ff7890SWen Congyang         error_setg(errp, "Block replication is not running");
64329ff7890SWen Congyang         aio_context_release(aio_context);
64429ff7890SWen Congyang         return;
64529ff7890SWen Congyang     }
64629ff7890SWen Congyang 
64729ff7890SWen Congyang     switch (s->mode) {
64829ff7890SWen Congyang     case REPLICATION_MODE_PRIMARY:
6493c76c606SFam Zheng         s->stage = BLOCK_REPLICATION_DONE;
65029ff7890SWen Congyang         s->error = 0;
65129ff7890SWen Congyang         break;
65229ff7890SWen Congyang     case REPLICATION_MODE_SECONDARY:
65329ff7890SWen Congyang         /*
65429ff7890SWen Congyang          * This BDS will be closed, and the job should be completed
65529ff7890SWen Congyang          * before the BDS is closed, because we will access hidden
65629ff7890SWen Congyang          * disk, secondary disk in backup_job_completed().
65729ff7890SWen Congyang          */
658*cc19f177SVladimir Sementsov-Ogievskiy         if (s->backup_job) {
659*cc19f177SVladimir Sementsov-Ogievskiy             job_cancel_sync(&s->backup_job->job);
66029ff7890SWen Congyang         }
66129ff7890SWen Congyang 
66229ff7890SWen Congyang         if (!failover) {
66329ff7890SWen Congyang             secondary_do_checkpoint(s, errp);
6643c76c606SFam Zheng             s->stage = BLOCK_REPLICATION_DONE;
66529ff7890SWen Congyang             aio_context_release(aio_context);
66629ff7890SWen Congyang             return;
66729ff7890SWen Congyang         }
66829ff7890SWen Congyang 
6693c76c606SFam Zheng         s->stage = BLOCK_REPLICATION_FAILOVER;
670*cc19f177SVladimir Sementsov-Ogievskiy         s->commit_job = commit_active_start(
671*cc19f177SVladimir Sementsov-Ogievskiy                             NULL, s->active_disk->bs, s->secondary_disk->bs,
672bb02b65cSKevin Wolf                             JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
67378bbd910SFam Zheng                             NULL, replication_done, bs, true, errp);
67429ff7890SWen Congyang         break;
67529ff7890SWen Congyang     default:
67629ff7890SWen Congyang         aio_context_release(aio_context);
67729ff7890SWen Congyang         abort();
67829ff7890SWen Congyang     }
67929ff7890SWen Congyang     aio_context_release(aio_context);
68029ff7890SWen Congyang }
68129ff7890SWen Congyang 
6822654267cSMax Reitz static const char *const replication_strong_runtime_opts[] = {
6832654267cSMax Reitz     REPLICATION_MODE,
6842654267cSMax Reitz     REPLICATION_TOP_ID,
6852654267cSMax Reitz 
6862654267cSMax Reitz     NULL
6872654267cSMax Reitz };
6882654267cSMax Reitz 
689782b9d06SAlberto Garcia static BlockDriver bdrv_replication = {
69029ff7890SWen Congyang     .format_name                = "replication",
69129ff7890SWen Congyang     .instance_size              = sizeof(BDRVReplicationState),
69229ff7890SWen Congyang 
69329ff7890SWen Congyang     .bdrv_open                  = replication_open,
69429ff7890SWen Congyang     .bdrv_close                 = replication_close,
69537a9051cSChanglong Xie     .bdrv_child_perm            = replication_child_perm,
69629ff7890SWen Congyang 
69729ff7890SWen Congyang     .bdrv_getlength             = replication_getlength,
69829ff7890SWen Congyang     .bdrv_co_readv              = replication_co_readv,
69929ff7890SWen Congyang     .bdrv_co_writev             = replication_co_writev,
70029ff7890SWen Congyang 
70129ff7890SWen Congyang     .is_filter                  = true,
70229ff7890SWen Congyang     .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter,
70329ff7890SWen Congyang 
70429ff7890SWen Congyang     .has_variable_length        = true,
7052654267cSMax Reitz     .strong_runtime_opts        = replication_strong_runtime_opts,
70629ff7890SWen Congyang };
70729ff7890SWen Congyang 
70829ff7890SWen Congyang static void bdrv_replication_init(void)
70929ff7890SWen Congyang {
71029ff7890SWen Congyang     bdrv_register(&bdrv_replication);
71129ff7890SWen Congyang }
71229ff7890SWen Congyang 
71329ff7890SWen Congyang block_init(bdrv_replication_init);
714