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
;
38 BdrvChild
*active_disk
;
40 BdrvChild
*hidden_disk
;
41 BdrvChild
*secondary_disk
;
46 bool orig_hidden_read_only
;
47 bool orig_secondary_read_only
;
49 } BDRVReplicationState
;
51 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
53 static void replication_do_checkpoint(ReplicationState
*rs
, Error
**errp
);
54 static void replication_get_error(ReplicationState
*rs
, Error
**errp
);
55 static void replication_stop(ReplicationState
*rs
, bool failover
,
58 #define REPLICATION_MODE "mode"
59 #define REPLICATION_TOP_ID "top-id"
60 static QemuOptsList replication_runtime_opts
= {
61 .name
= "replication",
62 .head
= QTAILQ_HEAD_INITIALIZER(replication_runtime_opts
.head
),
65 .name
= REPLICATION_MODE
,
66 .type
= QEMU_OPT_STRING
,
69 .name
= REPLICATION_TOP_ID
,
70 .type
= QEMU_OPT_STRING
,
76 static ReplicationOps replication_ops
= {
77 .start
= replication_start
,
78 .checkpoint
= replication_do_checkpoint
,
79 .get_error
= replication_get_error
,
80 .stop
= replication_stop
,
83 static int replication_open(BlockDriverState
*bs
, QDict
*options
,
84 int flags
, Error
**errp
)
87 BDRVReplicationState
*s
= bs
->opaque
;
88 QemuOpts
*opts
= NULL
;
92 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
93 BDRV_CHILD_FILTERED
| BDRV_CHILD_PRIMARY
,
100 opts
= qemu_opts_create(&replication_runtime_opts
, NULL
, 0, &error_abort
);
101 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
105 mode
= qemu_opt_get(opts
, REPLICATION_MODE
);
107 error_setg(errp
, "Missing the option mode");
111 if (!strcmp(mode
, "primary")) {
112 s
->mode
= REPLICATION_MODE_PRIMARY
;
113 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
116 "The primary side does not support option top-id");
119 } else if (!strcmp(mode
, "secondary")) {
120 s
->mode
= REPLICATION_MODE_SECONDARY
;
121 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
122 s
->top_id
= g_strdup(top_id
);
124 error_setg(errp
, "Missing the option top-id");
129 "The option mode's value should be primary or secondary");
133 s
->rs
= replication_new(bs
, &replication_ops
);
142 static void replication_close(BlockDriverState
*bs
)
144 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
);
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 *nperm
= BLK_PERM_CONSISTENT_READ
;
170 if ((bs
->open_flags
& (BDRV_O_INACTIVE
| BDRV_O_RDWR
)) == BDRV_O_RDWR
) {
171 *nperm
|= BLK_PERM_WRITE
;
173 *nshared
= BLK_PERM_CONSISTENT_READ
175 | BLK_PERM_WRITE_UNCHANGED
;
179 static int64_t replication_getlength(BlockDriverState
*bs
)
181 return bdrv_getlength(bs
->file
->bs
);
184 static int replication_get_io_status(BDRVReplicationState
*s
)
187 case BLOCK_REPLICATION_NONE
:
189 case BLOCK_REPLICATION_RUNNING
:
191 case BLOCK_REPLICATION_FAILOVER
:
192 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
193 case BLOCK_REPLICATION_FAILOVER_FAILED
:
194 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 1;
195 case BLOCK_REPLICATION_DONE
:
197 * active commit job completes, and active disk and secondary_disk
198 * is swapped, so we can operate bs->file directly
200 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
206 static int replication_return_value(BDRVReplicationState
*s
, int ret
)
208 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
220 static coroutine_fn
int replication_co_readv(BlockDriverState
*bs
,
222 int remaining_sectors
,
225 BDRVReplicationState
*s
= bs
->opaque
;
228 if (s
->mode
== REPLICATION_MODE_PRIMARY
) {
229 /* We only use it to forward primary write requests */
233 ret
= replication_get_io_status(s
);
238 ret
= bdrv_co_preadv(bs
->file
, sector_num
* BDRV_SECTOR_SIZE
,
239 remaining_sectors
* BDRV_SECTOR_SIZE
, qiov
, 0);
241 return replication_return_value(s
, ret
);
244 static coroutine_fn
int replication_co_writev(BlockDriverState
*bs
,
246 int remaining_sectors
,
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
;
260 ret
= replication_get_io_status(s
);
266 ret
= bdrv_co_pwritev(top
, sector_num
* BDRV_SECTOR_SIZE
,
267 remaining_sectors
* BDRV_SECTOR_SIZE
, qiov
, 0);
268 return replication_return_value(s
, ret
);
272 * Failover failed, only write to active disk if the sectors
273 * have already been allocated in active disk/hidden disk.
275 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
276 while (remaining_sectors
> 0) {
279 ret
= bdrv_is_allocated_above(top
->bs
, base
->bs
, false,
280 sector_num
* BDRV_SECTOR_SIZE
,
281 remaining_sectors
* BDRV_SECTOR_SIZE
,
287 assert(QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
));
288 n
= count
>> BDRV_SECTOR_BITS
;
289 qemu_iovec_reset(&hd_qiov
);
290 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, count
);
292 target
= ret
? top
: base
;
293 ret
= bdrv_co_pwritev(target
, sector_num
* BDRV_SECTOR_SIZE
,
294 n
* BDRV_SECTOR_SIZE
, &hd_qiov
, 0);
299 remaining_sectors
-= n
;
305 qemu_iovec_destroy(&hd_qiov
);
310 static void secondary_do_checkpoint(BDRVReplicationState
*s
, Error
**errp
)
312 Error
*local_err
= NULL
;
315 if (!s
->backup_job
) {
316 error_setg(errp
, "Backup job was cancelled unexpectedly");
320 backup_do_checkpoint(s
->backup_job
, &local_err
);
322 error_propagate(errp
, local_err
);
326 if (!s
->active_disk
->bs
->drv
) {
327 error_setg(errp
, "Active disk %s is ejected",
328 s
->active_disk
->bs
->node_name
);
332 ret
= bdrv_make_empty(s
->active_disk
, errp
);
337 if (!s
->hidden_disk
->bs
->drv
) {
338 error_setg(errp
, "Hidden disk %s is ejected",
339 s
->hidden_disk
->bs
->node_name
);
343 BlockBackend
*blk
= blk_new(qemu_get_current_aio_context(),
344 BLK_PERM_WRITE
, BLK_PERM_ALL
);
345 blk_insert_bs(blk
, s
->hidden_disk
->bs
, &local_err
);
347 error_propagate(errp
, local_err
);
352 ret
= blk_make_empty(blk
, errp
);
359 /* This function is supposed to be called twice:
360 * first with writable = true, then with writable = false.
361 * The first call puts s->hidden_disk and s->secondary_disk in
362 * r/w mode, and the second puts them back in their original state.
364 static void reopen_backing_file(BlockDriverState
*bs
, bool writable
,
367 BDRVReplicationState
*s
= bs
->opaque
;
368 BlockReopenQueue
*reopen_queue
= NULL
;
371 s
->orig_hidden_read_only
= bdrv_is_read_only(s
->hidden_disk
->bs
);
372 s
->orig_secondary_read_only
= bdrv_is_read_only(s
->secondary_disk
->bs
);
375 bdrv_subtree_drained_begin(s
->hidden_disk
->bs
);
376 bdrv_subtree_drained_begin(s
->secondary_disk
->bs
);
378 if (s
->orig_hidden_read_only
) {
379 QDict
*opts
= qdict_new();
380 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !writable
);
381 reopen_queue
= bdrv_reopen_queue(reopen_queue
, s
->hidden_disk
->bs
,
385 if (s
->orig_secondary_read_only
) {
386 QDict
*opts
= qdict_new();
387 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !writable
);
388 reopen_queue
= bdrv_reopen_queue(reopen_queue
, s
->secondary_disk
->bs
,
393 AioContext
*ctx
= bdrv_get_aio_context(bs
);
394 if (ctx
!= qemu_get_aio_context()) {
395 aio_context_release(ctx
);
397 bdrv_reopen_multiple(reopen_queue
, errp
);
398 if (ctx
!= qemu_get_aio_context()) {
399 aio_context_acquire(ctx
);
403 bdrv_subtree_drained_end(s
->hidden_disk
->bs
);
404 bdrv_subtree_drained_end(s
->secondary_disk
->bs
);
407 static void backup_job_cleanup(BlockDriverState
*bs
)
409 BDRVReplicationState
*s
= bs
->opaque
;
410 BlockDriverState
*top_bs
;
412 s
->backup_job
= NULL
;
414 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
418 bdrv_op_unblock_all(top_bs
, s
->blocker
);
419 error_free(s
->blocker
);
420 reopen_backing_file(bs
, false, NULL
);
423 static void backup_job_completed(void *opaque
, int ret
)
425 BlockDriverState
*bs
= opaque
;
426 BDRVReplicationState
*s
= bs
->opaque
;
428 if (s
->stage
!= BLOCK_REPLICATION_FAILOVER
) {
429 /* The backup job is cancelled unexpectedly */
433 backup_job_cleanup(bs
);
436 static bool check_top_bs(BlockDriverState
*top_bs
, BlockDriverState
*bs
)
440 /* The bs itself is the top_bs */
445 /* Iterate over top_bs's children */
446 QLIST_FOREACH(child
, &top_bs
->children
, next
) {
447 if (child
->bs
== bs
|| check_top_bs(child
->bs
, bs
)) {
455 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
458 BlockDriverState
*bs
= rs
->opaque
;
459 BDRVReplicationState
*s
;
460 BlockDriverState
*top_bs
;
461 int64_t active_length
, hidden_length
, disk_length
;
462 AioContext
*aio_context
;
463 Error
*local_err
= NULL
;
464 BackupPerf perf
= { .use_copy_range
= true, .max_workers
= 1 };
466 aio_context
= bdrv_get_aio_context(bs
);
467 aio_context_acquire(aio_context
);
470 if (s
->stage
== BLOCK_REPLICATION_DONE
||
471 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
473 * This case happens when a secondary is promoted to primary.
474 * Ignore the request because the secondary side of replication
475 * doesn't have to do anything anymore.
477 aio_context_release(aio_context
);
481 if (s
->stage
!= BLOCK_REPLICATION_NONE
) {
482 error_setg(errp
, "Block replication is running or done");
483 aio_context_release(aio_context
);
487 if (s
->mode
!= mode
) {
488 error_setg(errp
, "The parameter mode's value is invalid, needs %d,"
489 " but got %d", s
->mode
, mode
);
490 aio_context_release(aio_context
);
495 case REPLICATION_MODE_PRIMARY
:
497 case REPLICATION_MODE_SECONDARY
:
498 s
->active_disk
= bs
->file
;
499 if (!s
->active_disk
|| !s
->active_disk
->bs
||
500 !s
->active_disk
->bs
->backing
) {
501 error_setg(errp
, "Active disk doesn't have backing file");
502 aio_context_release(aio_context
);
506 s
->hidden_disk
= s
->active_disk
->bs
->backing
;
507 if (!s
->hidden_disk
->bs
|| !s
->hidden_disk
->bs
->backing
) {
508 error_setg(errp
, "Hidden disk doesn't have backing file");
509 aio_context_release(aio_context
);
513 s
->secondary_disk
= s
->hidden_disk
->bs
->backing
;
514 if (!s
->secondary_disk
->bs
|| !bdrv_has_blk(s
->secondary_disk
->bs
)) {
515 error_setg(errp
, "The secondary disk doesn't have block backend");
516 aio_context_release(aio_context
);
520 /* verify the length */
521 active_length
= bdrv_getlength(s
->active_disk
->bs
);
522 hidden_length
= bdrv_getlength(s
->hidden_disk
->bs
);
523 disk_length
= bdrv_getlength(s
->secondary_disk
->bs
);
524 if (active_length
< 0 || hidden_length
< 0 || disk_length
< 0 ||
525 active_length
!= hidden_length
|| hidden_length
!= disk_length
) {
526 error_setg(errp
, "Active disk, hidden disk, secondary disk's length"
527 " are not the same");
528 aio_context_release(aio_context
);
532 /* Must be true, or the bdrv_getlength() calls would have failed */
533 assert(s
->active_disk
->bs
->drv
&& s
->hidden_disk
->bs
->drv
);
535 if (!s
->active_disk
->bs
->drv
->bdrv_make_empty
||
536 !s
->hidden_disk
->bs
->drv
->bdrv_make_empty
) {
538 "Active disk or hidden disk doesn't support make_empty");
539 aio_context_release(aio_context
);
543 /* reopen the backing file in r/w mode */
544 reopen_backing_file(bs
, true, &local_err
);
546 error_propagate(errp
, local_err
);
547 aio_context_release(aio_context
);
551 /* start backup job now */
552 error_setg(&s
->blocker
,
553 "Block device is in use by internal backup job");
555 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
556 if (!top_bs
|| !bdrv_is_root_node(top_bs
) ||
557 !check_top_bs(top_bs
, bs
)) {
558 error_setg(errp
, "No top_bs or it is invalid");
559 reopen_backing_file(bs
, false, NULL
);
560 aio_context_release(aio_context
);
563 bdrv_op_block_all(top_bs
, s
->blocker
);
564 bdrv_op_unblock(top_bs
, BLOCK_OP_TYPE_DATAPLANE
, s
->blocker
);
566 s
->backup_job
= backup_job_create(
567 NULL
, s
->secondary_disk
->bs
, s
->hidden_disk
->bs
,
568 0, MIRROR_SYNC_MODE_NONE
, NULL
, 0, false, NULL
,
570 BLOCKDEV_ON_ERROR_REPORT
,
571 BLOCKDEV_ON_ERROR_REPORT
, JOB_INTERNAL
,
572 backup_job_completed
, bs
, NULL
, &local_err
);
574 error_propagate(errp
, local_err
);
575 backup_job_cleanup(bs
);
576 aio_context_release(aio_context
);
579 job_start(&s
->backup_job
->job
);
582 aio_context_release(aio_context
);
586 s
->stage
= BLOCK_REPLICATION_RUNNING
;
588 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
589 secondary_do_checkpoint(s
, errp
);
593 aio_context_release(aio_context
);
596 static void replication_do_checkpoint(ReplicationState
*rs
, Error
**errp
)
598 BlockDriverState
*bs
= rs
->opaque
;
599 BDRVReplicationState
*s
;
600 AioContext
*aio_context
;
602 aio_context
= bdrv_get_aio_context(bs
);
603 aio_context_acquire(aio_context
);
606 if (s
->stage
== BLOCK_REPLICATION_DONE
||
607 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
609 * This case happens when a secondary was promoted to primary.
610 * Ignore the request because the secondary side of replication
611 * doesn't have to do anything anymore.
613 aio_context_release(aio_context
);
617 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
618 secondary_do_checkpoint(s
, errp
);
620 aio_context_release(aio_context
);
623 static void replication_get_error(ReplicationState
*rs
, Error
**errp
)
625 BlockDriverState
*bs
= rs
->opaque
;
626 BDRVReplicationState
*s
;
627 AioContext
*aio_context
;
629 aio_context
= bdrv_get_aio_context(bs
);
630 aio_context_acquire(aio_context
);
633 if (s
->stage
== BLOCK_REPLICATION_NONE
) {
634 error_setg(errp
, "Block replication is not running");
635 aio_context_release(aio_context
);
640 error_setg(errp
, "I/O error occurred");
641 aio_context_release(aio_context
);
644 aio_context_release(aio_context
);
647 static void replication_done(void *opaque
, int ret
)
649 BlockDriverState
*bs
= opaque
;
650 BDRVReplicationState
*s
= bs
->opaque
;
653 s
->stage
= BLOCK_REPLICATION_DONE
;
655 s
->active_disk
= NULL
;
656 s
->secondary_disk
= NULL
;
657 s
->hidden_disk
= NULL
;
660 s
->stage
= BLOCK_REPLICATION_FAILOVER_FAILED
;
665 static void replication_stop(ReplicationState
*rs
, bool failover
, Error
**errp
)
667 BlockDriverState
*bs
= rs
->opaque
;
668 BDRVReplicationState
*s
;
669 AioContext
*aio_context
;
671 aio_context
= bdrv_get_aio_context(bs
);
672 aio_context_acquire(aio_context
);
675 if (s
->stage
== BLOCK_REPLICATION_DONE
||
676 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
678 * This case happens when a secondary was promoted to primary.
679 * Ignore the request because the secondary side of replication
680 * doesn't have to do anything anymore.
682 aio_context_release(aio_context
);
686 if (s
->stage
!= BLOCK_REPLICATION_RUNNING
) {
687 error_setg(errp
, "Block replication is not running");
688 aio_context_release(aio_context
);
693 case REPLICATION_MODE_PRIMARY
:
694 s
->stage
= BLOCK_REPLICATION_DONE
;
697 case REPLICATION_MODE_SECONDARY
:
699 * This BDS will be closed, and the job should be completed
700 * before the BDS is closed, because we will access hidden
701 * disk, secondary disk in backup_job_completed().
704 job_cancel_sync(&s
->backup_job
->job
);
708 secondary_do_checkpoint(s
, errp
);
709 s
->stage
= BLOCK_REPLICATION_DONE
;
710 aio_context_release(aio_context
);
714 s
->stage
= BLOCK_REPLICATION_FAILOVER
;
715 s
->commit_job
= commit_active_start(
716 NULL
, s
->active_disk
->bs
, s
->secondary_disk
->bs
,
717 JOB_INTERNAL
, 0, BLOCKDEV_ON_ERROR_REPORT
,
718 NULL
, replication_done
, bs
, true, errp
);
721 aio_context_release(aio_context
);
724 aio_context_release(aio_context
);
727 static const char *const replication_strong_runtime_opts
[] = {
734 static BlockDriver bdrv_replication
= {
735 .format_name
= "replication",
736 .instance_size
= sizeof(BDRVReplicationState
),
738 .bdrv_open
= replication_open
,
739 .bdrv_close
= replication_close
,
740 .bdrv_child_perm
= replication_child_perm
,
742 .bdrv_getlength
= replication_getlength
,
743 .bdrv_co_readv
= replication_co_readv
,
744 .bdrv_co_writev
= replication_co_writev
,
748 .has_variable_length
= true,
749 .strong_runtime_opts
= replication_strong_runtime_opts
,
752 static void bdrv_replication_init(void)
754 bdrv_register(&bdrv_replication
);
757 block_init(bdrv_replication_init
);