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 "system/block-backend.h"
23 #include "qapi/error.h"
24 #include "qobject/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
replication_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)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 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
92 if (ret < 0) {
93 return ret;
94 }
95
96 ret = -EINVAL;
97 opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
98 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
99 goto fail;
100 }
101
102 mode = qemu_opt_get(opts, REPLICATION_MODE);
103 if (!mode) {
104 error_setg(errp, "Missing the option mode");
105 goto fail;
106 }
107
108 if (!strcmp(mode, "primary")) {
109 s->mode = REPLICATION_MODE_PRIMARY;
110 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
111 if (top_id) {
112 error_setg(errp,
113 "The primary side does not support option top-id");
114 goto fail;
115 }
116 } else if (!strcmp(mode, "secondary")) {
117 s->mode = REPLICATION_MODE_SECONDARY;
118 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
119 s->top_id = g_strdup(top_id);
120 if (!s->top_id) {
121 error_setg(errp, "Missing the option top-id");
122 goto fail;
123 }
124 } else {
125 error_setg(errp,
126 "The option mode's value should be primary or secondary");
127 goto fail;
128 }
129
130 s->rs = replication_new(bs, &replication_ops);
131
132 ret = 0;
133
134 fail:
135 qemu_opts_del(opts);
136 return ret;
137 }
138
replication_close(BlockDriverState * bs)139 static void replication_close(BlockDriverState *bs)
140 {
141 BDRVReplicationState *s = bs->opaque;
142 Job *commit_job;
143 GLOBAL_STATE_CODE();
144
145 if (s->stage == BLOCK_REPLICATION_RUNNING) {
146 replication_stop(s->rs, false, NULL);
147 }
148 if (s->stage == BLOCK_REPLICATION_FAILOVER) {
149 commit_job = &s->commit_job->job;
150 assert(commit_job->aio_context == qemu_get_current_aio_context());
151 job_cancel_sync(commit_job, false);
152 }
153
154 if (s->mode == REPLICATION_MODE_SECONDARY) {
155 g_free(s->top_id);
156 }
157
158 replication_remove(s->rs);
159 }
160
replication_child_perm(BlockDriverState * bs,BdrvChild * c,BdrvChildRole role,BlockReopenQueue * reopen_queue,uint64_t perm,uint64_t shared,uint64_t * nperm,uint64_t * nshared)161 static void replication_child_perm(BlockDriverState *bs, BdrvChild *c,
162 BdrvChildRole role,
163 BlockReopenQueue *reopen_queue,
164 uint64_t perm, uint64_t shared,
165 uint64_t *nperm, uint64_t *nshared)
166 {
167 if (role & BDRV_CHILD_PRIMARY) {
168 *nperm = BLK_PERM_CONSISTENT_READ;
169 } else {
170 *nperm = 0;
171 }
172
173 if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
174 *nperm |= BLK_PERM_WRITE;
175 }
176 *nshared = BLK_PERM_CONSISTENT_READ
177 | BLK_PERM_WRITE
178 | BLK_PERM_WRITE_UNCHANGED;
179 }
180
181 static int64_t coroutine_fn GRAPH_RDLOCK
replication_co_getlength(BlockDriverState * bs)182 replication_co_getlength(BlockDriverState *bs)
183 {
184 return bdrv_co_getlength(bs->file->bs);
185 }
186
replication_get_io_status(BDRVReplicationState * s)187 static int replication_get_io_status(BDRVReplicationState *s)
188 {
189 switch (s->stage) {
190 case BLOCK_REPLICATION_NONE:
191 return -EIO;
192 case BLOCK_REPLICATION_RUNNING:
193 return 0;
194 case BLOCK_REPLICATION_FAILOVER:
195 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
196 case BLOCK_REPLICATION_FAILOVER_FAILED:
197 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
198 case BLOCK_REPLICATION_DONE:
199 /*
200 * active commit job completes, and active disk and secondary_disk
201 * is swapped, so we can operate bs->file directly
202 */
203 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
204 default:
205 abort();
206 }
207 }
208
replication_return_value(BDRVReplicationState * s,int ret)209 static int replication_return_value(BDRVReplicationState *s, int ret)
210 {
211 if (s->mode == REPLICATION_MODE_SECONDARY) {
212 return ret;
213 }
214
215 if (ret < 0) {
216 s->error = ret;
217 ret = 0;
218 }
219
220 return ret;
221 }
222
223 static int coroutine_fn GRAPH_RDLOCK
replication_co_readv(BlockDriverState * bs,int64_t sector_num,int remaining_sectors,QEMUIOVector * qiov)224 replication_co_readv(BlockDriverState *bs, int64_t sector_num,
225 int remaining_sectors, QEMUIOVector *qiov)
226 {
227 BDRVReplicationState *s = bs->opaque;
228 int ret;
229
230 if (s->mode == REPLICATION_MODE_PRIMARY) {
231 /* We only use it to forward primary write requests */
232 return -EIO;
233 }
234
235 ret = replication_get_io_status(s);
236 if (ret < 0) {
237 return ret;
238 }
239
240 ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
241 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
242
243 return replication_return_value(s, ret);
244 }
245
246 static int coroutine_fn GRAPH_RDLOCK
replication_co_writev(BlockDriverState * bs,int64_t sector_num,int remaining_sectors,QEMUIOVector * qiov,int flags)247 replication_co_writev(BlockDriverState *bs, int64_t sector_num,
248 int remaining_sectors, QEMUIOVector *qiov, int flags)
249 {
250 BDRVReplicationState *s = bs->opaque;
251 QEMUIOVector hd_qiov;
252 uint64_t bytes_done = 0;
253 BdrvChild *top = bs->file;
254 BdrvChild *base = s->secondary_disk;
255 BdrvChild *target;
256 int ret;
257 int64_t n;
258
259 ret = replication_get_io_status(s);
260 if (ret < 0) {
261 goto out;
262 }
263
264 if (ret == 0) {
265 ret = bdrv_co_pwritev(top, sector_num * BDRV_SECTOR_SIZE,
266 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
267 return replication_return_value(s, ret);
268 }
269
270 /*
271 * Failover failed, only write to active disk if the sectors
272 * have already been allocated in active disk/hidden disk.
273 */
274 qemu_iovec_init(&hd_qiov, qiov->niov);
275 while (remaining_sectors > 0) {
276 int64_t count;
277
278 ret = bdrv_co_is_allocated_above(top->bs, base->bs, false,
279 sector_num * BDRV_SECTOR_SIZE,
280 remaining_sectors * BDRV_SECTOR_SIZE,
281 &count);
282 if (ret < 0) {
283 goto out1;
284 }
285
286 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
287 n = count >> BDRV_SECTOR_BITS;
288 qemu_iovec_reset(&hd_qiov);
289 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count);
290
291 target = ret ? top : base;
292 ret = bdrv_co_pwritev(target, sector_num * BDRV_SECTOR_SIZE,
293 n * BDRV_SECTOR_SIZE, &hd_qiov, 0);
294 if (ret < 0) {
295 goto out1;
296 }
297
298 remaining_sectors -= n;
299 sector_num += n;
300 bytes_done += count;
301 }
302
303 out1:
304 qemu_iovec_destroy(&hd_qiov);
305 out:
306 return ret;
307 }
308
309 static void GRAPH_UNLOCKED
secondary_do_checkpoint(BlockDriverState * bs,Error ** errp)310 secondary_do_checkpoint(BlockDriverState *bs, Error **errp)
311 {
312 BDRVReplicationState *s = bs->opaque;
313 BdrvChild *active_disk;
314 Error *local_err = NULL;
315 int ret;
316
317 GRAPH_RDLOCK_GUARD_MAINLOOP();
318
319 if (!s->backup_job) {
320 error_setg(errp, "Backup job was cancelled unexpectedly");
321 return;
322 }
323
324 backup_do_checkpoint(s->backup_job, &local_err);
325 if (local_err) {
326 error_propagate(errp, local_err);
327 return;
328 }
329
330 active_disk = bs->file;
331 if (!active_disk->bs->drv) {
332 error_setg(errp, "Active disk %s is ejected",
333 active_disk->bs->node_name);
334 return;
335 }
336
337 ret = bdrv_make_empty(active_disk, errp);
338 if (ret < 0) {
339 return;
340 }
341
342 if (!s->hidden_disk->bs->drv) {
343 error_setg(errp, "Hidden disk %s is ejected",
344 s->hidden_disk->bs->node_name);
345 return;
346 }
347
348 ret = bdrv_make_empty(s->hidden_disk, errp);
349 if (ret < 0) {
350 return;
351 }
352 }
353
354 /* This function is supposed to be called twice:
355 * first with writable = true, then with writable = false.
356 * The first call puts s->hidden_disk and s->secondary_disk in
357 * r/w mode, and the second puts them back in their original state.
358 */
reopen_backing_file(BlockDriverState * bs,bool writable,Error ** errp)359 static void reopen_backing_file(BlockDriverState *bs, bool writable,
360 Error **errp)
361 {
362 BDRVReplicationState *s = bs->opaque;
363 BdrvChild *hidden_disk, *secondary_disk;
364 BlockReopenQueue *reopen_queue = NULL;
365
366 GLOBAL_STATE_CODE();
367 GRAPH_RDLOCK_GUARD_MAINLOOP();
368
369 /*
370 * s->hidden_disk and s->secondary_disk may not be set yet, as they will
371 * only be set after the children are writable.
372 */
373 hidden_disk = bs->file->bs->backing;
374 secondary_disk = hidden_disk->bs->backing;
375
376 if (writable) {
377 s->orig_hidden_read_only = bdrv_is_read_only(hidden_disk->bs);
378 s->orig_secondary_read_only = bdrv_is_read_only(secondary_disk->bs);
379 }
380
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, hidden_disk->bs,
385 opts, true);
386 }
387
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, secondary_disk->bs,
392 opts, true);
393 }
394
395 if (reopen_queue) {
396 bdrv_reopen_multiple(reopen_queue, errp);
397 }
398 }
399
backup_job_cleanup(BlockDriverState * bs)400 static void backup_job_cleanup(BlockDriverState *bs)
401 {
402 BDRVReplicationState *s = bs->opaque;
403 BlockDriverState *top_bs;
404
405 s->backup_job = NULL;
406
407 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
408 if (!top_bs) {
409 return;
410 }
411 bdrv_op_unblock_all(top_bs, s->blocker);
412 error_free(s->blocker);
413 reopen_backing_file(bs, false, NULL);
414 }
415
backup_job_completed(void * opaque,int ret)416 static void backup_job_completed(void *opaque, int ret)
417 {
418 BlockDriverState *bs = opaque;
419 BDRVReplicationState *s = bs->opaque;
420
421 if (s->stage != BLOCK_REPLICATION_FAILOVER) {
422 /* The backup job is cancelled unexpectedly */
423 s->error = -EIO;
424 }
425
426 backup_job_cleanup(bs);
427 }
428
429 static bool GRAPH_RDLOCK
check_top_bs(BlockDriverState * top_bs,BlockDriverState * bs)430 check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
431 {
432 BdrvChild *child;
433
434 /* The bs itself is the top_bs */
435 if (top_bs == bs) {
436 return true;
437 }
438
439 /* Iterate over top_bs's children */
440 QLIST_FOREACH(child, &top_bs->children, next) {
441 if (child->bs == bs || check_top_bs(child->bs, bs)) {
442 return true;
443 }
444 }
445
446 return false;
447 }
448
replication_start(ReplicationState * rs,ReplicationMode mode,Error ** errp)449 static void replication_start(ReplicationState *rs, ReplicationMode mode,
450 Error **errp)
451 {
452 BlockDriverState *bs = rs->opaque;
453 BDRVReplicationState *s;
454 BlockDriverState *top_bs;
455 BdrvChild *active_disk, *hidden_disk, *secondary_disk;
456 int64_t active_length, hidden_length, disk_length;
457 Error *local_err = NULL;
458 BackupPerf perf = { .use_copy_range = true, .max_workers = 1 };
459
460 GLOBAL_STATE_CODE();
461
462 s = bs->opaque;
463
464 if (s->stage == BLOCK_REPLICATION_DONE ||
465 s->stage == BLOCK_REPLICATION_FAILOVER) {
466 /*
467 * This case happens when a secondary is promoted to primary.
468 * Ignore the request because the secondary side of replication
469 * doesn't have to do anything anymore.
470 */
471 return;
472 }
473
474 if (s->stage != BLOCK_REPLICATION_NONE) {
475 error_setg(errp, "Block replication is running or done");
476 return;
477 }
478
479 if (s->mode != mode) {
480 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
481 " but got %d", s->mode, mode);
482 return;
483 }
484
485 switch (s->mode) {
486 case REPLICATION_MODE_PRIMARY:
487 break;
488 case REPLICATION_MODE_SECONDARY:
489 bdrv_graph_rdlock_main_loop();
490 active_disk = bs->file;
491 if (!active_disk || !active_disk->bs || !active_disk->bs->backing) {
492 error_setg(errp, "Active disk doesn't have backing file");
493 bdrv_graph_rdunlock_main_loop();
494 return;
495 }
496
497 hidden_disk = active_disk->bs->backing;
498 if (!hidden_disk->bs || !hidden_disk->bs->backing) {
499 error_setg(errp, "Hidden disk doesn't have backing file");
500 bdrv_graph_rdunlock_main_loop();
501 return;
502 }
503
504 secondary_disk = hidden_disk->bs->backing;
505 if (!secondary_disk->bs || !bdrv_has_blk(secondary_disk->bs)) {
506 error_setg(errp, "The secondary disk doesn't have block backend");
507 bdrv_graph_rdunlock_main_loop();
508 return;
509 }
510 bdrv_graph_rdunlock_main_loop();
511
512 /* verify the length */
513 active_length = bdrv_getlength(active_disk->bs);
514 hidden_length = bdrv_getlength(hidden_disk->bs);
515 disk_length = bdrv_getlength(secondary_disk->bs);
516 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
517 active_length != hidden_length || hidden_length != disk_length) {
518 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
519 " are not the same");
520 return;
521 }
522
523 /* Must be true, or the bdrv_getlength() calls would have failed */
524 assert(active_disk->bs->drv && hidden_disk->bs->drv);
525
526 bdrv_graph_rdlock_main_loop();
527 if (!active_disk->bs->drv->bdrv_make_empty ||
528 !hidden_disk->bs->drv->bdrv_make_empty) {
529 error_setg(errp,
530 "Active disk or hidden disk doesn't support make_empty");
531 bdrv_graph_rdunlock_main_loop();
532 return;
533 }
534 bdrv_graph_rdunlock_main_loop();
535
536 /* reopen the backing file in r/w mode */
537 reopen_backing_file(bs, true, &local_err);
538 if (local_err) {
539 error_propagate(errp, local_err);
540 return;
541 }
542
543 bdrv_drain_all_begin();
544 bdrv_graph_wrlock();
545
546 bdrv_ref(hidden_disk->bs);
547 s->hidden_disk = bdrv_attach_child(bs, hidden_disk->bs, "hidden disk",
548 &child_of_bds, BDRV_CHILD_DATA,
549 &local_err);
550 if (local_err) {
551 error_propagate(errp, local_err);
552 bdrv_graph_wrunlock();
553 bdrv_drain_all_end();
554 return;
555 }
556
557 bdrv_ref(secondary_disk->bs);
558 s->secondary_disk = bdrv_attach_child(bs, secondary_disk->bs,
559 "secondary disk", &child_of_bds,
560 BDRV_CHILD_DATA, &local_err);
561 if (local_err) {
562 error_propagate(errp, local_err);
563 bdrv_graph_wrunlock();
564 bdrv_drain_all_end();
565 return;
566 }
567
568 /* start backup job now */
569 error_setg(&s->blocker,
570 "Block device is in use by internal backup job");
571
572 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
573 if (!top_bs || !bdrv_is_root_node(top_bs) ||
574 !check_top_bs(top_bs, bs)) {
575 error_setg(errp, "No top_bs or it is invalid");
576 bdrv_graph_wrunlock();
577 bdrv_drain_all_end();
578 reopen_backing_file(bs, false, NULL);
579 return;
580 }
581 bdrv_op_block_all(top_bs, s->blocker);
582
583 bdrv_graph_wrunlock();
584 bdrv_drain_all_end();
585
586 s->backup_job = backup_job_create(
587 NULL, s->secondary_disk->bs, s->hidden_disk->bs,
588 0, MIRROR_SYNC_MODE_NONE, NULL, 0, false, false,
589 NULL, &perf,
590 BLOCKDEV_ON_ERROR_REPORT,
591 BLOCKDEV_ON_ERROR_REPORT,
592 ON_CBW_ERROR_BREAK_GUEST_WRITE,
593 JOB_INTERNAL,
594 backup_job_completed, bs, NULL, &local_err);
595 if (local_err) {
596 error_propagate(errp, local_err);
597 backup_job_cleanup(bs);
598 return;
599 }
600 job_start(&s->backup_job->job);
601 break;
602 default:
603 abort();
604 }
605
606 s->stage = BLOCK_REPLICATION_RUNNING;
607
608 if (s->mode == REPLICATION_MODE_SECONDARY) {
609 secondary_do_checkpoint(bs, errp);
610 }
611
612 s->error = 0;
613 }
614
replication_do_checkpoint(ReplicationState * rs,Error ** errp)615 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
616 {
617 BlockDriverState *bs = rs->opaque;
618 BDRVReplicationState *s = bs->opaque;
619
620 if (s->stage == BLOCK_REPLICATION_DONE ||
621 s->stage == BLOCK_REPLICATION_FAILOVER) {
622 /*
623 * This case happens when a secondary was promoted to primary.
624 * Ignore the request because the secondary side of replication
625 * doesn't have to do anything anymore.
626 */
627 return;
628 }
629
630 if (s->mode == REPLICATION_MODE_SECONDARY) {
631 secondary_do_checkpoint(bs, errp);
632 }
633 }
634
replication_get_error(ReplicationState * rs,Error ** errp)635 static void replication_get_error(ReplicationState *rs, Error **errp)
636 {
637 BlockDriverState *bs = rs->opaque;
638 BDRVReplicationState *s = bs->opaque;
639
640 if (s->stage == BLOCK_REPLICATION_NONE) {
641 error_setg(errp, "Block replication is not running");
642 return;
643 }
644
645 if (s->error) {
646 error_setg(errp, "I/O error occurred");
647 return;
648 }
649 }
650
replication_done(void * opaque,int ret)651 static void replication_done(void *opaque, int ret)
652 {
653 BlockDriverState *bs = opaque;
654 BDRVReplicationState *s = bs->opaque;
655
656 if (ret == 0) {
657 s->stage = BLOCK_REPLICATION_DONE;
658
659 bdrv_drain_all_begin();
660 bdrv_graph_wrlock();
661 bdrv_unref_child(bs, s->secondary_disk);
662 s->secondary_disk = NULL;
663 bdrv_unref_child(bs, s->hidden_disk);
664 s->hidden_disk = NULL;
665 bdrv_graph_wrunlock();
666 bdrv_drain_all_end();
667
668 s->error = 0;
669 } else {
670 s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
671 s->error = -EIO;
672 }
673 }
674
replication_stop(ReplicationState * rs,bool failover,Error ** errp)675 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
676 {
677 BlockDriverState *bs = rs->opaque;
678 BDRVReplicationState *s = bs->opaque;
679
680 if (s->stage == BLOCK_REPLICATION_DONE ||
681 s->stage == BLOCK_REPLICATION_FAILOVER) {
682 /*
683 * This case happens when a secondary was promoted to primary.
684 * Ignore the request because the secondary side of replication
685 * doesn't have to do anything anymore.
686 */
687 return;
688 }
689
690 if (s->stage != BLOCK_REPLICATION_RUNNING) {
691 error_setg(errp, "Block replication is not running");
692 return;
693 }
694
695 switch (s->mode) {
696 case REPLICATION_MODE_PRIMARY:
697 s->stage = BLOCK_REPLICATION_DONE;
698 s->error = 0;
699 break;
700 case REPLICATION_MODE_SECONDARY:
701 /*
702 * This BDS will be closed, and the job should be completed
703 * before the BDS is closed, because we will access hidden
704 * disk, secondary disk in backup_job_completed().
705 */
706 if (s->backup_job) {
707 job_cancel_sync(&s->backup_job->job, true);
708 }
709
710 if (!failover) {
711 secondary_do_checkpoint(bs, errp);
712 s->stage = BLOCK_REPLICATION_DONE;
713 return;
714 }
715
716 bdrv_graph_rdlock_main_loop();
717 s->stage = BLOCK_REPLICATION_FAILOVER;
718 s->commit_job = commit_active_start(
719 NULL, bs->file->bs, s->secondary_disk->bs,
720 JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
721 NULL, replication_done, bs, true, errp);
722 bdrv_graph_rdunlock_main_loop();
723 break;
724 default:
725 abort();
726 }
727 }
728
729 static const char *const replication_strong_runtime_opts[] = {
730 REPLICATION_MODE,
731 REPLICATION_TOP_ID,
732
733 NULL
734 };
735
736 static BlockDriver bdrv_replication = {
737 .format_name = "replication",
738 .instance_size = sizeof(BDRVReplicationState),
739
740 .bdrv_open = replication_open,
741 .bdrv_close = replication_close,
742 .bdrv_child_perm = replication_child_perm,
743
744 .bdrv_co_getlength = replication_co_getlength,
745 .bdrv_co_readv = replication_co_readv,
746 .bdrv_co_writev = replication_co_writev,
747
748 .is_filter = true,
749
750 .strong_runtime_opts = replication_strong_runtime_opts,
751 };
752
bdrv_replication_init(void)753 static void bdrv_replication_init(void)
754 {
755 bdrv_register(&bdrv_replication);
756 }
757
758 block_init(bdrv_replication_init);
759