xref: /openbmc/qemu/block/replication.c (revision 3cf71969)
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/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"
26 
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;
34 
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;
49 
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);
56 
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 = {
63         {
64             .name = REPLICATION_MODE,
65             .type = QEMU_OPT_STRING,
66         },
67         {
68             .name = REPLICATION_TOP_ID,
69             .type = QEMU_OPT_STRING,
70         },
71         { /* end of list */ }
72     },
73 };
74 
75 static ReplicationOps replication_ops = {
76     .start = replication_start,
77     .checkpoint = replication_do_checkpoint,
78     .get_error = replication_get_error,
79     .stop = replication_stop,
80 };
81 
82 static int replication_open(BlockDriverState *bs, QDict *options,
83                             int flags, Error **errp)
84 {
85     int ret;
86     BDRVReplicationState *s = bs->opaque;
87     QemuOpts *opts = NULL;
88     const char *mode;
89     const char *top_id;
90 
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;
96     }
97 
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;
102     }
103 
104     mode = qemu_opt_get(opts, REPLICATION_MODE);
105     if (!mode) {
106         error_setg(errp, "Missing the option mode");
107         goto fail;
108     }
109 
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;
117         }
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;
125         }
126     } else {
127         error_setg(errp,
128                    "The option mode's value should be primary or secondary");
129         goto fail;
130     }
131 
132     s->rs = replication_new(bs, &replication_ops);
133 
134     ret = 0;
135 
136 fail:
137     qemu_opts_del(opts);
138     return ret;
139 }
140 
141 static void replication_close(BlockDriverState *bs)
142 {
143     BDRVReplicationState *s = bs->opaque;
144     Job *commit_job;
145     GLOBAL_STATE_CODE();
146 
147     if (s->stage == BLOCK_REPLICATION_RUNNING) {
148         replication_stop(s->rs, false, NULL);
149     }
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);
154     }
155 
156     if (s->mode == REPLICATION_MODE_SECONDARY) {
157         g_free(s->top_id);
158     }
159 
160     replication_remove(s->rs);
161 }
162 
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)
168 {
169     if (role & BDRV_CHILD_PRIMARY) {
170         *nperm = BLK_PERM_CONSISTENT_READ;
171     } else {
172         *nperm = 0;
173     }
174 
175     if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
176         *nperm |= BLK_PERM_WRITE;
177     }
178     *nshared = BLK_PERM_CONSISTENT_READ
179                | BLK_PERM_WRITE
180                | BLK_PERM_WRITE_UNCHANGED;
181     return;
182 }
183 
184 static int64_t replication_getlength(BlockDriverState *bs)
185 {
186     return bdrv_getlength(bs->file->bs);
187 }
188 
189 static int replication_get_io_status(BDRVReplicationState *s)
190 {
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:
201         /*
202          * active commit job completes, and active disk and secondary_disk
203          * is swapped, so we can operate bs->file directly
204          */
205         return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
206     default:
207         abort();
208     }
209 }
210 
211 static int replication_return_value(BDRVReplicationState *s, int ret)
212 {
213     if (s->mode == REPLICATION_MODE_SECONDARY) {
214         return ret;
215     }
216 
217     if (ret < 0) {
218         s->error = ret;
219         ret = 0;
220     }
221 
222     return ret;
223 }
224 
225 static coroutine_fn int replication_co_readv(BlockDriverState *bs,
226                                              int64_t sector_num,
227                                              int remaining_sectors,
228                                              QEMUIOVector *qiov)
229 {
230     BDRVReplicationState *s = bs->opaque;
231     int ret;
232 
233     if (s->mode == REPLICATION_MODE_PRIMARY) {
234         /* We only use it to forward primary write requests */
235         return -EIO;
236     }
237 
238     ret = replication_get_io_status(s);
239     if (ret < 0) {
240         return ret;
241     }
242 
243     ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
244                          remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
245 
246     return replication_return_value(s, ret);
247 }
248 
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)
254 {
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;
263 
264     assert(!flags);
265     ret = replication_get_io_status(s);
266     if (ret < 0) {
267         goto out;
268     }
269 
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);
274     }
275 
276     /*
277      * Failover failed, only write to active disk if the sectors
278      * have already been allocated in active disk/hidden disk.
279      */
280     qemu_iovec_init(&hd_qiov, qiov->niov);
281     while (remaining_sectors > 0) {
282         int64_t count;
283 
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;
290         }
291 
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);
296 
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;
302         }
303 
304         remaining_sectors -= n;
305         sector_num += n;
306         bytes_done += count;
307     }
308 
309 out1:
310     qemu_iovec_destroy(&hd_qiov);
311 out:
312     return ret;
313 }
314 
315 static void secondary_do_checkpoint(BlockDriverState *bs, Error **errp)
316 {
317     BDRVReplicationState *s = bs->opaque;
318     BdrvChild *active_disk = bs->file;
319     Error *local_err = NULL;
320     int ret;
321 
322     if (!s->backup_job) {
323         error_setg(errp, "Backup job was cancelled unexpectedly");
324         return;
325     }
326 
327     backup_do_checkpoint(s->backup_job, &local_err);
328     if (local_err) {
329         error_propagate(errp, local_err);
330         return;
331     }
332 
333     if (!active_disk->bs->drv) {
334         error_setg(errp, "Active disk %s is ejected",
335                    active_disk->bs->node_name);
336         return;
337     }
338 
339     ret = bdrv_make_empty(active_disk, errp);
340     if (ret < 0) {
341         return;
342     }
343 
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;
348     }
349 
350     ret = bdrv_make_empty(s->hidden_disk, errp);
351     if (ret < 0) {
352         return;
353     }
354 }
355 
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.
360  */
361 static void reopen_backing_file(BlockDriverState *bs, bool writable,
362                                 Error **errp)
363 {
364     BDRVReplicationState *s = bs->opaque;
365     BdrvChild *hidden_disk, *secondary_disk;
366     BlockReopenQueue *reopen_queue = NULL;
367 
368     /*
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.
371      */
372     hidden_disk = bs->file->bs->backing;
373     secondary_disk = hidden_disk->bs->backing;
374 
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);
378     }
379 
380     bdrv_subtree_drained_begin(hidden_disk->bs);
381     bdrv_subtree_drained_begin(secondary_disk->bs);
382 
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);
388     }
389 
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);
395     }
396 
397     if (reopen_queue) {
398         AioContext *ctx = bdrv_get_aio_context(bs);
399         if (ctx != qemu_get_aio_context()) {
400             aio_context_release(ctx);
401         }
402         bdrv_reopen_multiple(reopen_queue, errp);
403         if (ctx != qemu_get_aio_context()) {
404             aio_context_acquire(ctx);
405         }
406     }
407 
408     bdrv_subtree_drained_end(hidden_disk->bs);
409     bdrv_subtree_drained_end(secondary_disk->bs);
410 }
411 
412 static void backup_job_cleanup(BlockDriverState *bs)
413 {
414     BDRVReplicationState *s = bs->opaque;
415     BlockDriverState *top_bs;
416 
417     s->backup_job = NULL;
418 
419     top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
420     if (!top_bs) {
421         return;
422     }
423     bdrv_op_unblock_all(top_bs, s->blocker);
424     error_free(s->blocker);
425     reopen_backing_file(bs, false, NULL);
426 }
427 
428 static void backup_job_completed(void *opaque, int ret)
429 {
430     BlockDriverState *bs = opaque;
431     BDRVReplicationState *s = bs->opaque;
432 
433     if (s->stage != BLOCK_REPLICATION_FAILOVER) {
434         /* The backup job is cancelled unexpectedly */
435         s->error = -EIO;
436     }
437 
438     backup_job_cleanup(bs);
439 }
440 
441 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
442 {
443     BdrvChild *child;
444 
445     /* The bs itself is the top_bs */
446     if (top_bs == bs) {
447         return true;
448     }
449 
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;
454         }
455     }
456 
457     return false;
458 }
459 
460 static void replication_start(ReplicationState *rs, ReplicationMode mode,
461                               Error **errp)
462 {
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 };
471 
472     aio_context = bdrv_get_aio_context(bs);
473     aio_context_acquire(aio_context);
474     s = bs->opaque;
475 
476     if (s->stage == BLOCK_REPLICATION_DONE ||
477         s->stage == BLOCK_REPLICATION_FAILOVER) {
478         /*
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.
482          */
483         aio_context_release(aio_context);
484         return;
485     }
486 
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;
491     }
492 
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;
498     }
499 
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;
509         }
510 
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;
516         }
517 
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;
523         }
524 
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;
535         }
536 
537         /* Must be true, or the bdrv_getlength() calls would have failed */
538         assert(active_disk->bs->drv && hidden_disk->bs->drv);
539 
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;
546         }
547 
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;
554         }
555 
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;
564         }
565 
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;
574         }
575 
576         /* start backup job now */
577         error_setg(&s->blocker,
578                    "Block device is in use by internal backup job");
579 
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;
587         }
588         bdrv_op_block_all(top_bs, s->blocker);
589         bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
590 
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;
603         }
604         job_start(&s->backup_job->job);
605         break;
606     default:
607         aio_context_release(aio_context);
608         abort();
609     }
610 
611     s->stage = BLOCK_REPLICATION_RUNNING;
612 
613     if (s->mode == REPLICATION_MODE_SECONDARY) {
614         secondary_do_checkpoint(bs, errp);
615     }
616 
617     s->error = 0;
618     aio_context_release(aio_context);
619 }
620 
621 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
622 {
623     BlockDriverState *bs = rs->opaque;
624     BDRVReplicationState *s;
625     AioContext *aio_context;
626 
627     aio_context = bdrv_get_aio_context(bs);
628     aio_context_acquire(aio_context);
629     s = bs->opaque;
630 
631     if (s->stage == BLOCK_REPLICATION_DONE ||
632         s->stage == BLOCK_REPLICATION_FAILOVER) {
633         /*
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.
637          */
638         aio_context_release(aio_context);
639         return;
640     }
641 
642     if (s->mode == REPLICATION_MODE_SECONDARY) {
643         secondary_do_checkpoint(bs, errp);
644     }
645     aio_context_release(aio_context);
646 }
647 
648 static void replication_get_error(ReplicationState *rs, Error **errp)
649 {
650     BlockDriverState *bs = rs->opaque;
651     BDRVReplicationState *s;
652     AioContext *aio_context;
653 
654     aio_context = bdrv_get_aio_context(bs);
655     aio_context_acquire(aio_context);
656     s = bs->opaque;
657 
658     if (s->stage == BLOCK_REPLICATION_NONE) {
659         error_setg(errp, "Block replication is not running");
660         aio_context_release(aio_context);
661         return;
662     }
663 
664     if (s->error) {
665         error_setg(errp, "I/O error occurred");
666         aio_context_release(aio_context);
667         return;
668     }
669     aio_context_release(aio_context);
670 }
671 
672 static void replication_done(void *opaque, int ret)
673 {
674     BlockDriverState *bs = opaque;
675     BDRVReplicationState *s = bs->opaque;
676 
677     if (ret == 0) {
678         s->stage = BLOCK_REPLICATION_DONE;
679 
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;
688     }
689 }
690 
691 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
692 {
693     BlockDriverState *bs = rs->opaque;
694     BDRVReplicationState *s;
695     AioContext *aio_context;
696 
697     aio_context = bdrv_get_aio_context(bs);
698     aio_context_acquire(aio_context);
699     s = bs->opaque;
700 
701     if (s->stage == BLOCK_REPLICATION_DONE ||
702         s->stage == BLOCK_REPLICATION_FAILOVER) {
703         /*
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.
707          */
708         aio_context_release(aio_context);
709         return;
710     }
711 
712     if (s->stage != BLOCK_REPLICATION_RUNNING) {
713         error_setg(errp, "Block replication is not running");
714         aio_context_release(aio_context);
715         return;
716     }
717 
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:
724         /*
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().
728          */
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);
733         }
734 
735         if (!failover) {
736             secondary_do_checkpoint(bs, errp);
737             s->stage = BLOCK_REPLICATION_DONE;
738             aio_context_release(aio_context);
739             return;
740         }
741 
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();
751     }
752     aio_context_release(aio_context);
753 }
754 
755 static const char *const replication_strong_runtime_opts[] = {
756     REPLICATION_MODE,
757     REPLICATION_TOP_ID,
758 
759     NULL
760 };
761 
762 static BlockDriver bdrv_replication = {
763     .format_name                = "replication",
764     .instance_size              = sizeof(BDRVReplicationState),
765 
766     .bdrv_open                  = replication_open,
767     .bdrv_close                 = replication_close,
768     .bdrv_child_perm            = replication_child_perm,
769 
770     .bdrv_getlength             = replication_getlength,
771     .bdrv_co_readv              = replication_co_readv,
772     .bdrv_co_writev             = replication_co_writev,
773 
774     .is_filter                  = true,
775 
776     .has_variable_length        = true,
777     .strong_runtime_opts        = replication_strong_runtime_opts,
778 };
779 
780 static void bdrv_replication_init(void)
781 {
782     bdrv_register(&bdrv_replication);
783 }
784 
785 block_init(bdrv_replication_init);
786