2 * Replication Block filter
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 * Copyright (c) 2016 Intel Corporation
6 * Copyright (c) 2016 FUJITSU LIMITED
9 * Wen Congyang <wency@cn.fujitsu.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qemu/module.h"
17 #include "qemu/option.h"
18 #include "block/nbd.h"
19 #include "block/blockjob.h"
20 #include "block/block_int.h"
21 #include "block/block_backup.h"
22 #include "sysemu/block-backend.h"
23 #include "qapi/error.h"
24 #include "qapi/qmp/qdict.h"
25 #include "block/replication.h"
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 */
35 typedef struct BDRVReplicationState
{
37 ReplicationStage stage
;
39 BdrvChild
*hidden_disk
;
40 BdrvChild
*secondary_disk
;
45 bool orig_hidden_read_only
;
46 bool orig_secondary_read_only
;
48 } BDRVReplicationState
;
50 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
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
,
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
),
64 .name
= REPLICATION_MODE
,
65 .type
= QEMU_OPT_STRING
,
68 .name
= REPLICATION_TOP_ID
,
69 .type
= QEMU_OPT_STRING
,
75 static ReplicationOps replication_ops
= {
76 .start
= replication_start
,
77 .checkpoint
= replication_do_checkpoint
,
78 .get_error
= replication_get_error
,
79 .stop
= replication_stop
,
82 static int replication_open(BlockDriverState
*bs
, QDict
*options
,
83 int flags
, Error
**errp
)
86 BDRVReplicationState
*s
= bs
->opaque
;
87 QemuOpts
*opts
= NULL
;
91 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
92 BDRV_CHILD_FILTERED
| BDRV_CHILD_PRIMARY
,
99 opts
= qemu_opts_create(&replication_runtime_opts
, NULL
, 0, &error_abort
);
100 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
104 mode
= qemu_opt_get(opts
, REPLICATION_MODE
);
106 error_setg(errp
, "Missing the option mode");
110 if (!strcmp(mode
, "primary")) {
111 s
->mode
= REPLICATION_MODE_PRIMARY
;
112 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
115 "The primary side does not support option top-id");
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
);
123 error_setg(errp
, "Missing the option top-id");
128 "The option mode's value should be primary or secondary");
132 s
->rs
= replication_new(bs
, &replication_ops
);
141 static void replication_close(BlockDriverState
*bs
)
143 BDRVReplicationState
*s
= bs
->opaque
;
147 if (s
->stage
== BLOCK_REPLICATION_RUNNING
) {
148 replication_stop(s
->rs
, false, NULL
);
150 if (s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
151 commit_job
= &s
->commit_job
->job
;
152 assert(commit_job
->aio_context
== qemu_get_current_aio_context());
153 job_cancel_sync(commit_job
, false);
156 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
160 replication_remove(s
->rs
);
163 static void replication_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
165 BlockReopenQueue
*reopen_queue
,
166 uint64_t perm
, uint64_t shared
,
167 uint64_t *nperm
, uint64_t *nshared
)
169 if (role
& BDRV_CHILD_PRIMARY
) {
170 *nperm
= BLK_PERM_CONSISTENT_READ
;
175 if ((bs
->open_flags
& (BDRV_O_INACTIVE
| BDRV_O_RDWR
)) == BDRV_O_RDWR
) {
176 *nperm
|= BLK_PERM_WRITE
;
178 *nshared
= BLK_PERM_CONSISTENT_READ
180 | BLK_PERM_WRITE_UNCHANGED
;
184 static int64_t replication_getlength(BlockDriverState
*bs
)
186 return bdrv_getlength(bs
->file
->bs
);
189 static int replication_get_io_status(BDRVReplicationState
*s
)
192 case BLOCK_REPLICATION_NONE
:
194 case BLOCK_REPLICATION_RUNNING
:
196 case BLOCK_REPLICATION_FAILOVER
:
197 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
198 case BLOCK_REPLICATION_FAILOVER_FAILED
:
199 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 1;
200 case BLOCK_REPLICATION_DONE
:
202 * active commit job completes, and active disk and secondary_disk
203 * is swapped, so we can operate bs->file directly
205 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
211 static int replication_return_value(BDRVReplicationState
*s
, int ret
)
213 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
225 static coroutine_fn
int replication_co_readv(BlockDriverState
*bs
,
227 int remaining_sectors
,
230 BDRVReplicationState
*s
= bs
->opaque
;
233 if (s
->mode
== REPLICATION_MODE_PRIMARY
) {
234 /* We only use it to forward primary write requests */
238 ret
= replication_get_io_status(s
);
243 ret
= bdrv_co_preadv(bs
->file
, sector_num
* BDRV_SECTOR_SIZE
,
244 remaining_sectors
* BDRV_SECTOR_SIZE
, qiov
, 0);
246 return replication_return_value(s
, ret
);
249 static coroutine_fn
int replication_co_writev(BlockDriverState
*bs
,
251 int remaining_sectors
,
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
;
265 ret
= replication_get_io_status(s
);
271 ret
= bdrv_co_pwritev(top
, sector_num
* BDRV_SECTOR_SIZE
,
272 remaining_sectors
* BDRV_SECTOR_SIZE
, qiov
, 0);
273 return replication_return_value(s
, ret
);
277 * Failover failed, only write to active disk if the sectors
278 * have already been allocated in active disk/hidden disk.
280 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
281 while (remaining_sectors
> 0) {
284 ret
= bdrv_is_allocated_above(top
->bs
, base
->bs
, false,
285 sector_num
* BDRV_SECTOR_SIZE
,
286 remaining_sectors
* BDRV_SECTOR_SIZE
,
292 assert(QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
));
293 n
= count
>> BDRV_SECTOR_BITS
;
294 qemu_iovec_reset(&hd_qiov
);
295 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, count
);
297 target
= ret
? top
: base
;
298 ret
= bdrv_co_pwritev(target
, sector_num
* BDRV_SECTOR_SIZE
,
299 n
* BDRV_SECTOR_SIZE
, &hd_qiov
, 0);
304 remaining_sectors
-= n
;
310 qemu_iovec_destroy(&hd_qiov
);
315 static void secondary_do_checkpoint(BlockDriverState
*bs
, Error
**errp
)
317 BDRVReplicationState
*s
= bs
->opaque
;
318 BdrvChild
*active_disk
= bs
->file
;
319 Error
*local_err
= NULL
;
322 if (!s
->backup_job
) {
323 error_setg(errp
, "Backup job was cancelled unexpectedly");
327 backup_do_checkpoint(s
->backup_job
, &local_err
);
329 error_propagate(errp
, local_err
);
333 if (!active_disk
->bs
->drv
) {
334 error_setg(errp
, "Active disk %s is ejected",
335 active_disk
->bs
->node_name
);
339 ret
= bdrv_make_empty(active_disk
, errp
);
344 if (!s
->hidden_disk
->bs
->drv
) {
345 error_setg(errp
, "Hidden disk %s is ejected",
346 s
->hidden_disk
->bs
->node_name
);
350 ret
= bdrv_make_empty(s
->hidden_disk
, errp
);
356 /* This function is supposed to be called twice:
357 * first with writable = true, then with writable = false.
358 * The first call puts s->hidden_disk and s->secondary_disk in
359 * r/w mode, and the second puts them back in their original state.
361 static void reopen_backing_file(BlockDriverState
*bs
, bool writable
,
364 BDRVReplicationState
*s
= bs
->opaque
;
365 BdrvChild
*hidden_disk
, *secondary_disk
;
366 BlockReopenQueue
*reopen_queue
= NULL
;
369 * s->hidden_disk and s->secondary_disk may not be set yet, as they will
370 * only be set after the children are writable.
372 hidden_disk
= bs
->file
->bs
->backing
;
373 secondary_disk
= hidden_disk
->bs
->backing
;
376 s
->orig_hidden_read_only
= bdrv_is_read_only(hidden_disk
->bs
);
377 s
->orig_secondary_read_only
= bdrv_is_read_only(secondary_disk
->bs
);
380 bdrv_subtree_drained_begin(hidden_disk
->bs
);
381 bdrv_subtree_drained_begin(secondary_disk
->bs
);
383 if (s
->orig_hidden_read_only
) {
384 QDict
*opts
= qdict_new();
385 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !writable
);
386 reopen_queue
= bdrv_reopen_queue(reopen_queue
, hidden_disk
->bs
,
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
,
398 AioContext
*ctx
= bdrv_get_aio_context(bs
);
399 if (ctx
!= qemu_get_aio_context()) {
400 aio_context_release(ctx
);
402 bdrv_reopen_multiple(reopen_queue
, errp
);
403 if (ctx
!= qemu_get_aio_context()) {
404 aio_context_acquire(ctx
);
408 bdrv_subtree_drained_end(hidden_disk
->bs
);
409 bdrv_subtree_drained_end(secondary_disk
->bs
);
412 static void backup_job_cleanup(BlockDriverState
*bs
)
414 BDRVReplicationState
*s
= bs
->opaque
;
415 BlockDriverState
*top_bs
;
417 s
->backup_job
= NULL
;
419 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
423 bdrv_op_unblock_all(top_bs
, s
->blocker
);
424 error_free(s
->blocker
);
425 reopen_backing_file(bs
, false, NULL
);
428 static void backup_job_completed(void *opaque
, int ret
)
430 BlockDriverState
*bs
= opaque
;
431 BDRVReplicationState
*s
= bs
->opaque
;
433 if (s
->stage
!= BLOCK_REPLICATION_FAILOVER
) {
434 /* The backup job is cancelled unexpectedly */
438 backup_job_cleanup(bs
);
441 static bool check_top_bs(BlockDriverState
*top_bs
, BlockDriverState
*bs
)
445 /* The bs itself is the top_bs */
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
)) {
460 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
463 BlockDriverState
*bs
= rs
->opaque
;
464 BDRVReplicationState
*s
;
465 BlockDriverState
*top_bs
;
466 BdrvChild
*active_disk
, *hidden_disk
, *secondary_disk
;
467 int64_t active_length
, hidden_length
, disk_length
;
468 AioContext
*aio_context
;
469 Error
*local_err
= NULL
;
470 BackupPerf perf
= { .use_copy_range
= true, .max_workers
= 1 };
472 aio_context
= bdrv_get_aio_context(bs
);
473 aio_context_acquire(aio_context
);
476 if (s
->stage
== BLOCK_REPLICATION_DONE
||
477 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
479 * This case happens when a secondary is promoted to primary.
480 * Ignore the request because the secondary side of replication
481 * doesn't have to do anything anymore.
483 aio_context_release(aio_context
);
487 if (s
->stage
!= BLOCK_REPLICATION_NONE
) {
488 error_setg(errp
, "Block replication is running or done");
489 aio_context_release(aio_context
);
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
);
501 case REPLICATION_MODE_PRIMARY
:
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
);
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
);
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
);
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
);
537 /* Must be true, or the bdrv_getlength() calls would have failed */
538 assert(active_disk
->bs
->drv
&& hidden_disk
->bs
->drv
);
540 if (!active_disk
->bs
->drv
->bdrv_make_empty
||
541 !hidden_disk
->bs
->drv
->bdrv_make_empty
) {
543 "Active disk or hidden disk doesn't support make_empty");
544 aio_context_release(aio_context
);
548 /* reopen the backing file in r/w mode */
549 reopen_backing_file(bs
, true, &local_err
);
551 error_propagate(errp
, local_err
);
552 aio_context_release(aio_context
);
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
,
561 error_propagate(errp
, local_err
);
562 aio_context_release(aio_context
);
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
);
571 error_propagate(errp
, local_err
);
572 aio_context_release(aio_context
);
576 /* start backup job now */
577 error_setg(&s
->blocker
,
578 "Block device is in use by internal backup job");
580 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
581 if (!top_bs
|| !bdrv_is_root_node(top_bs
) ||
582 !check_top_bs(top_bs
, bs
)) {
583 error_setg(errp
, "No top_bs or it is invalid");
584 reopen_backing_file(bs
, false, NULL
);
585 aio_context_release(aio_context
);
588 bdrv_op_block_all(top_bs
, s
->blocker
);
589 bdrv_op_unblock(top_bs
, BLOCK_OP_TYPE_DATAPLANE
, s
->blocker
);
591 s
->backup_job
= backup_job_create(
592 NULL
, s
->secondary_disk
->bs
, s
->hidden_disk
->bs
,
593 0, MIRROR_SYNC_MODE_NONE
, NULL
, 0, false, NULL
,
595 BLOCKDEV_ON_ERROR_REPORT
,
596 BLOCKDEV_ON_ERROR_REPORT
, JOB_INTERNAL
,
597 backup_job_completed
, bs
, NULL
, &local_err
);
599 error_propagate(errp
, local_err
);
600 backup_job_cleanup(bs
);
601 aio_context_release(aio_context
);
604 job_start(&s
->backup_job
->job
);
607 aio_context_release(aio_context
);
611 s
->stage
= BLOCK_REPLICATION_RUNNING
;
613 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
614 secondary_do_checkpoint(bs
, errp
);
618 aio_context_release(aio_context
);
621 static void replication_do_checkpoint(ReplicationState
*rs
, Error
**errp
)
623 BlockDriverState
*bs
= rs
->opaque
;
624 BDRVReplicationState
*s
;
625 AioContext
*aio_context
;
627 aio_context
= bdrv_get_aio_context(bs
);
628 aio_context_acquire(aio_context
);
631 if (s
->stage
== BLOCK_REPLICATION_DONE
||
632 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
634 * This case happens when a secondary was promoted to primary.
635 * Ignore the request because the secondary side of replication
636 * doesn't have to do anything anymore.
638 aio_context_release(aio_context
);
642 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
643 secondary_do_checkpoint(bs
, errp
);
645 aio_context_release(aio_context
);
648 static void replication_get_error(ReplicationState
*rs
, Error
**errp
)
650 BlockDriverState
*bs
= rs
->opaque
;
651 BDRVReplicationState
*s
;
652 AioContext
*aio_context
;
654 aio_context
= bdrv_get_aio_context(bs
);
655 aio_context_acquire(aio_context
);
658 if (s
->stage
== BLOCK_REPLICATION_NONE
) {
659 error_setg(errp
, "Block replication is not running");
660 aio_context_release(aio_context
);
665 error_setg(errp
, "I/O error occurred");
666 aio_context_release(aio_context
);
669 aio_context_release(aio_context
);
672 static void replication_done(void *opaque
, int ret
)
674 BlockDriverState
*bs
= opaque
;
675 BDRVReplicationState
*s
= bs
->opaque
;
678 s
->stage
= BLOCK_REPLICATION_DONE
;
680 bdrv_unref_child(bs
, s
->secondary_disk
);
681 s
->secondary_disk
= NULL
;
682 bdrv_unref_child(bs
, s
->hidden_disk
);
683 s
->hidden_disk
= NULL
;
686 s
->stage
= BLOCK_REPLICATION_FAILOVER_FAILED
;
691 static void replication_stop(ReplicationState
*rs
, bool failover
, Error
**errp
)
693 BlockDriverState
*bs
= rs
->opaque
;
694 BDRVReplicationState
*s
;
695 AioContext
*aio_context
;
697 aio_context
= bdrv_get_aio_context(bs
);
698 aio_context_acquire(aio_context
);
701 if (s
->stage
== BLOCK_REPLICATION_DONE
||
702 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
704 * This case happens when a secondary was promoted to primary.
705 * Ignore the request because the secondary side of replication
706 * doesn't have to do anything anymore.
708 aio_context_release(aio_context
);
712 if (s
->stage
!= BLOCK_REPLICATION_RUNNING
) {
713 error_setg(errp
, "Block replication is not running");
714 aio_context_release(aio_context
);
719 case REPLICATION_MODE_PRIMARY
:
720 s
->stage
= BLOCK_REPLICATION_DONE
;
723 case REPLICATION_MODE_SECONDARY
:
725 * This BDS will be closed, and the job should be completed
726 * before the BDS is closed, because we will access hidden
727 * disk, secondary disk in backup_job_completed().
730 aio_context_release(aio_context
);
731 job_cancel_sync(&s
->backup_job
->job
, true);
732 aio_context_acquire(aio_context
);
736 secondary_do_checkpoint(bs
, errp
);
737 s
->stage
= BLOCK_REPLICATION_DONE
;
738 aio_context_release(aio_context
);
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
);
749 aio_context_release(aio_context
);
752 aio_context_release(aio_context
);
755 static const char *const replication_strong_runtime_opts
[] = {
762 static BlockDriver bdrv_replication
= {
763 .format_name
= "replication",
764 .instance_size
= sizeof(BDRVReplicationState
),
766 .bdrv_open
= replication_open
,
767 .bdrv_close
= replication_close
,
768 .bdrv_child_perm
= replication_child_perm
,
770 .bdrv_getlength
= replication_getlength
,
771 .bdrv_co_readv
= replication_co_readv
,
772 .bdrv_co_writev
= replication_co_writev
,
776 .has_variable_length
= true,
777 .strong_runtime_opts
= replication_strong_runtime_opts
,
780 static void bdrv_replication_init(void)
782 bdrv_register(&bdrv_replication
);
785 block_init(bdrv_replication_init
);