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 "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 Error
*local_err
= NULL
;
89 QemuOpts
*opts
= NULL
;
93 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
94 BDRV_CHILD_FILTERED
| BDRV_CHILD_PRIMARY
,
101 opts
= qemu_opts_create(&replication_runtime_opts
, NULL
, 0, &error_abort
);
102 if (!qemu_opts_absorb_qdict(opts
, options
, &local_err
)) {
106 mode
= qemu_opt_get(opts
, REPLICATION_MODE
);
108 error_setg(errp
, "Missing the option mode");
112 if (!strcmp(mode
, "primary")) {
113 s
->mode
= REPLICATION_MODE_PRIMARY
;
114 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
117 "The primary side does not support option top-id");
120 } else if (!strcmp(mode
, "secondary")) {
121 s
->mode
= REPLICATION_MODE_SECONDARY
;
122 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
123 s
->top_id
= g_strdup(top_id
);
125 error_setg(errp
, "Missing the option top-id");
130 "The option mode's value should be primary or secondary");
134 s
->rs
= replication_new(bs
, &replication_ops
);
143 static void replication_close(BlockDriverState
*bs
)
145 BDRVReplicationState
*s
= bs
->opaque
;
148 if (s
->stage
== BLOCK_REPLICATION_RUNNING
) {
149 replication_stop(s
->rs
, false, NULL
);
151 if (s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
152 commit_job
= &s
->commit_job
->job
;
153 assert(commit_job
->aio_context
== qemu_get_current_aio_context());
154 job_cancel_sync(commit_job
);
157 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
161 replication_remove(s
->rs
);
164 static void replication_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
166 BlockReopenQueue
*reopen_queue
,
167 uint64_t perm
, uint64_t shared
,
168 uint64_t *nperm
, uint64_t *nshared
)
170 *nperm
= BLK_PERM_CONSISTENT_READ
;
171 if ((bs
->open_flags
& (BDRV_O_INACTIVE
| BDRV_O_RDWR
)) == BDRV_O_RDWR
) {
172 *nperm
|= BLK_PERM_WRITE
;
174 *nshared
= BLK_PERM_CONSISTENT_READ
176 | BLK_PERM_WRITE_UNCHANGED
;
180 static int64_t replication_getlength(BlockDriverState
*bs
)
182 return bdrv_getlength(bs
->file
->bs
);
185 static int replication_get_io_status(BDRVReplicationState
*s
)
188 case BLOCK_REPLICATION_NONE
:
190 case BLOCK_REPLICATION_RUNNING
:
192 case BLOCK_REPLICATION_FAILOVER
:
193 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
194 case BLOCK_REPLICATION_FAILOVER_FAILED
:
195 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 1;
196 case BLOCK_REPLICATION_DONE
:
198 * active commit job completes, and active disk and secondary_disk
199 * is swapped, so we can operate bs->file directly
201 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
207 static int replication_return_value(BDRVReplicationState
*s
, int ret
)
209 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
221 static coroutine_fn
int replication_co_readv(BlockDriverState
*bs
,
223 int remaining_sectors
,
226 BDRVReplicationState
*s
= bs
->opaque
;
229 if (s
->mode
== REPLICATION_MODE_PRIMARY
) {
230 /* We only use it to forward primary write requests */
234 ret
= replication_get_io_status(s
);
239 ret
= bdrv_co_preadv(bs
->file
, sector_num
* BDRV_SECTOR_SIZE
,
240 remaining_sectors
* BDRV_SECTOR_SIZE
, qiov
, 0);
242 return replication_return_value(s
, ret
);
245 static coroutine_fn
int replication_co_writev(BlockDriverState
*bs
,
247 int remaining_sectors
,
251 BDRVReplicationState
*s
= bs
->opaque
;
252 QEMUIOVector hd_qiov
;
253 uint64_t bytes_done
= 0;
254 BdrvChild
*top
= bs
->file
;
255 BdrvChild
*base
= s
->secondary_disk
;
261 ret
= replication_get_io_status(s
);
267 ret
= bdrv_co_pwritev(top
, sector_num
* BDRV_SECTOR_SIZE
,
268 remaining_sectors
* BDRV_SECTOR_SIZE
, qiov
, 0);
269 return replication_return_value(s
, ret
);
273 * Failover failed, only write to active disk if the sectors
274 * have already been allocated in active disk/hidden disk.
276 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
277 while (remaining_sectors
> 0) {
280 ret
= bdrv_is_allocated_above(top
->bs
, base
->bs
, false,
281 sector_num
* BDRV_SECTOR_SIZE
,
282 remaining_sectors
* BDRV_SECTOR_SIZE
,
288 assert(QEMU_IS_ALIGNED(count
, BDRV_SECTOR_SIZE
));
289 n
= count
>> BDRV_SECTOR_BITS
;
290 qemu_iovec_reset(&hd_qiov
);
291 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, count
);
293 target
= ret
? top
: base
;
294 ret
= bdrv_co_pwritev(target
, sector_num
* BDRV_SECTOR_SIZE
,
295 n
* BDRV_SECTOR_SIZE
, &hd_qiov
, 0);
300 remaining_sectors
-= n
;
306 qemu_iovec_destroy(&hd_qiov
);
311 static void secondary_do_checkpoint(BDRVReplicationState
*s
, Error
**errp
)
313 Error
*local_err
= NULL
;
316 if (!s
->backup_job
) {
317 error_setg(errp
, "Backup job was cancelled unexpectedly");
321 backup_do_checkpoint(s
->backup_job
, &local_err
);
323 error_propagate(errp
, local_err
);
327 if (!s
->active_disk
->bs
->drv
) {
328 error_setg(errp
, "Active disk %s is ejected",
329 s
->active_disk
->bs
->node_name
);
333 ret
= bdrv_make_empty(s
->active_disk
, errp
);
338 if (!s
->hidden_disk
->bs
->drv
) {
339 error_setg(errp
, "Hidden disk %s is ejected",
340 s
->hidden_disk
->bs
->node_name
);
344 BlockBackend
*blk
= blk_new(qemu_get_current_aio_context(),
345 BLK_PERM_WRITE
, BLK_PERM_ALL
);
346 blk_insert_bs(blk
, s
->hidden_disk
->bs
, &local_err
);
348 error_propagate(errp
, local_err
);
353 ret
= blk_make_empty(blk
, errp
);
360 /* This function is supposed to be called twice:
361 * first with writable = true, then with writable = false.
362 * The first call puts s->hidden_disk and s->secondary_disk in
363 * r/w mode, and the second puts them back in their original state.
365 static void reopen_backing_file(BlockDriverState
*bs
, bool writable
,
368 BDRVReplicationState
*s
= bs
->opaque
;
369 BlockReopenQueue
*reopen_queue
= NULL
;
370 Error
*local_err
= NULL
;
373 s
->orig_hidden_read_only
= bdrv_is_read_only(s
->hidden_disk
->bs
);
374 s
->orig_secondary_read_only
= bdrv_is_read_only(s
->secondary_disk
->bs
);
377 bdrv_subtree_drained_begin(s
->hidden_disk
->bs
);
378 bdrv_subtree_drained_begin(s
->secondary_disk
->bs
);
380 if (s
->orig_hidden_read_only
) {
381 QDict
*opts
= qdict_new();
382 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !writable
);
383 reopen_queue
= bdrv_reopen_queue(reopen_queue
, s
->hidden_disk
->bs
,
387 if (s
->orig_secondary_read_only
) {
388 QDict
*opts
= qdict_new();
389 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !writable
);
390 reopen_queue
= bdrv_reopen_queue(reopen_queue
, s
->secondary_disk
->bs
,
395 bdrv_reopen_multiple(reopen_queue
, &local_err
);
396 error_propagate(errp
, local_err
);
399 bdrv_subtree_drained_end(s
->hidden_disk
->bs
);
400 bdrv_subtree_drained_end(s
->secondary_disk
->bs
);
403 static void backup_job_cleanup(BlockDriverState
*bs
)
405 BDRVReplicationState
*s
= bs
->opaque
;
406 BlockDriverState
*top_bs
;
408 s
->backup_job
= NULL
;
410 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
414 bdrv_op_unblock_all(top_bs
, s
->blocker
);
415 error_free(s
->blocker
);
416 reopen_backing_file(bs
, false, NULL
);
419 static void backup_job_completed(void *opaque
, int ret
)
421 BlockDriverState
*bs
= opaque
;
422 BDRVReplicationState
*s
= bs
->opaque
;
424 if (s
->stage
!= BLOCK_REPLICATION_FAILOVER
) {
425 /* The backup job is cancelled unexpectedly */
429 backup_job_cleanup(bs
);
432 static bool check_top_bs(BlockDriverState
*top_bs
, BlockDriverState
*bs
)
436 /* The bs itself is the top_bs */
441 /* Iterate over top_bs's children */
442 QLIST_FOREACH(child
, &top_bs
->children
, next
) {
443 if (child
->bs
== bs
|| check_top_bs(child
->bs
, bs
)) {
451 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
454 BlockDriverState
*bs
= rs
->opaque
;
455 BDRVReplicationState
*s
;
456 BlockDriverState
*top_bs
;
457 int64_t active_length
, hidden_length
, disk_length
;
458 AioContext
*aio_context
;
459 Error
*local_err
= NULL
;
461 aio_context
= bdrv_get_aio_context(bs
);
462 aio_context_acquire(aio_context
);
465 if (s
->stage
== BLOCK_REPLICATION_DONE
||
466 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
468 * This case happens when a secondary is promoted to primary.
469 * Ignore the request because the secondary side of replication
470 * doesn't have to do anything anymore.
472 aio_context_release(aio_context
);
476 if (s
->stage
!= BLOCK_REPLICATION_NONE
) {
477 error_setg(errp
, "Block replication is running or done");
478 aio_context_release(aio_context
);
482 if (s
->mode
!= mode
) {
483 error_setg(errp
, "The parameter mode's value is invalid, needs %d,"
484 " but got %d", s
->mode
, mode
);
485 aio_context_release(aio_context
);
490 case REPLICATION_MODE_PRIMARY
:
492 case REPLICATION_MODE_SECONDARY
:
493 s
->active_disk
= bs
->file
;
494 if (!s
->active_disk
|| !s
->active_disk
->bs
||
495 !s
->active_disk
->bs
->backing
) {
496 error_setg(errp
, "Active disk doesn't have backing file");
497 aio_context_release(aio_context
);
501 s
->hidden_disk
= s
->active_disk
->bs
->backing
;
502 if (!s
->hidden_disk
->bs
|| !s
->hidden_disk
->bs
->backing
) {
503 error_setg(errp
, "Hidden disk doesn't have backing file");
504 aio_context_release(aio_context
);
508 s
->secondary_disk
= s
->hidden_disk
->bs
->backing
;
509 if (!s
->secondary_disk
->bs
|| !bdrv_has_blk(s
->secondary_disk
->bs
)) {
510 error_setg(errp
, "The secondary disk doesn't have block backend");
511 aio_context_release(aio_context
);
515 /* verify the length */
516 active_length
= bdrv_getlength(s
->active_disk
->bs
);
517 hidden_length
= bdrv_getlength(s
->hidden_disk
->bs
);
518 disk_length
= bdrv_getlength(s
->secondary_disk
->bs
);
519 if (active_length
< 0 || hidden_length
< 0 || disk_length
< 0 ||
520 active_length
!= hidden_length
|| hidden_length
!= disk_length
) {
521 error_setg(errp
, "Active disk, hidden disk, secondary disk's length"
522 " are not the same");
523 aio_context_release(aio_context
);
527 /* Must be true, or the bdrv_getlength() calls would have failed */
528 assert(s
->active_disk
->bs
->drv
&& s
->hidden_disk
->bs
->drv
);
530 if (!s
->active_disk
->bs
->drv
->bdrv_make_empty
||
531 !s
->hidden_disk
->bs
->drv
->bdrv_make_empty
) {
533 "Active disk or hidden disk doesn't support make_empty");
534 aio_context_release(aio_context
);
538 /* reopen the backing file in r/w mode */
539 reopen_backing_file(bs
, true, &local_err
);
541 error_propagate(errp
, local_err
);
542 aio_context_release(aio_context
);
546 /* start backup job now */
547 error_setg(&s
->blocker
,
548 "Block device is in use by internal backup job");
550 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
551 if (!top_bs
|| !bdrv_is_root_node(top_bs
) ||
552 !check_top_bs(top_bs
, bs
)) {
553 error_setg(errp
, "No top_bs or it is invalid");
554 reopen_backing_file(bs
, false, NULL
);
555 aio_context_release(aio_context
);
558 bdrv_op_block_all(top_bs
, s
->blocker
);
559 bdrv_op_unblock(top_bs
, BLOCK_OP_TYPE_DATAPLANE
, s
->blocker
);
561 s
->backup_job
= backup_job_create(
562 NULL
, s
->secondary_disk
->bs
, s
->hidden_disk
->bs
,
563 0, MIRROR_SYNC_MODE_NONE
, NULL
, 0, false, NULL
,
564 BLOCKDEV_ON_ERROR_REPORT
,
565 BLOCKDEV_ON_ERROR_REPORT
, JOB_INTERNAL
,
566 backup_job_completed
, bs
, NULL
, &local_err
);
568 error_propagate(errp
, local_err
);
569 backup_job_cleanup(bs
);
570 aio_context_release(aio_context
);
573 job_start(&s
->backup_job
->job
);
576 aio_context_release(aio_context
);
580 s
->stage
= BLOCK_REPLICATION_RUNNING
;
582 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
583 secondary_do_checkpoint(s
, errp
);
587 aio_context_release(aio_context
);
590 static void replication_do_checkpoint(ReplicationState
*rs
, Error
**errp
)
592 BlockDriverState
*bs
= rs
->opaque
;
593 BDRVReplicationState
*s
;
594 AioContext
*aio_context
;
596 aio_context
= bdrv_get_aio_context(bs
);
597 aio_context_acquire(aio_context
);
600 if (s
->stage
== BLOCK_REPLICATION_DONE
||
601 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
603 * This case happens when a secondary was promoted to primary.
604 * Ignore the request because the secondary side of replication
605 * doesn't have to do anything anymore.
607 aio_context_release(aio_context
);
611 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
612 secondary_do_checkpoint(s
, errp
);
614 aio_context_release(aio_context
);
617 static void replication_get_error(ReplicationState
*rs
, Error
**errp
)
619 BlockDriverState
*bs
= rs
->opaque
;
620 BDRVReplicationState
*s
;
621 AioContext
*aio_context
;
623 aio_context
= bdrv_get_aio_context(bs
);
624 aio_context_acquire(aio_context
);
627 if (s
->stage
== BLOCK_REPLICATION_NONE
) {
628 error_setg(errp
, "Block replication is not running");
629 aio_context_release(aio_context
);
634 error_setg(errp
, "I/O error occurred");
635 aio_context_release(aio_context
);
638 aio_context_release(aio_context
);
641 static void replication_done(void *opaque
, int ret
)
643 BlockDriverState
*bs
= opaque
;
644 BDRVReplicationState
*s
= bs
->opaque
;
647 s
->stage
= BLOCK_REPLICATION_DONE
;
649 s
->active_disk
= NULL
;
650 s
->secondary_disk
= NULL
;
651 s
->hidden_disk
= NULL
;
654 s
->stage
= BLOCK_REPLICATION_FAILOVER_FAILED
;
659 static void replication_stop(ReplicationState
*rs
, bool failover
, Error
**errp
)
661 BlockDriverState
*bs
= rs
->opaque
;
662 BDRVReplicationState
*s
;
663 AioContext
*aio_context
;
665 aio_context
= bdrv_get_aio_context(bs
);
666 aio_context_acquire(aio_context
);
669 if (s
->stage
== BLOCK_REPLICATION_DONE
||
670 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
672 * This case happens when a secondary was promoted to primary.
673 * Ignore the request because the secondary side of replication
674 * doesn't have to do anything anymore.
676 aio_context_release(aio_context
);
680 if (s
->stage
!= BLOCK_REPLICATION_RUNNING
) {
681 error_setg(errp
, "Block replication is not running");
682 aio_context_release(aio_context
);
687 case REPLICATION_MODE_PRIMARY
:
688 s
->stage
= BLOCK_REPLICATION_DONE
;
691 case REPLICATION_MODE_SECONDARY
:
693 * This BDS will be closed, and the job should be completed
694 * before the BDS is closed, because we will access hidden
695 * disk, secondary disk in backup_job_completed().
698 job_cancel_sync(&s
->backup_job
->job
);
702 secondary_do_checkpoint(s
, errp
);
703 s
->stage
= BLOCK_REPLICATION_DONE
;
704 aio_context_release(aio_context
);
708 s
->stage
= BLOCK_REPLICATION_FAILOVER
;
709 s
->commit_job
= commit_active_start(
710 NULL
, s
->active_disk
->bs
, s
->secondary_disk
->bs
,
711 JOB_INTERNAL
, 0, BLOCKDEV_ON_ERROR_REPORT
,
712 NULL
, replication_done
, bs
, true, errp
);
715 aio_context_release(aio_context
);
718 aio_context_release(aio_context
);
721 static const char *const replication_strong_runtime_opts
[] = {
728 static BlockDriver bdrv_replication
= {
729 .format_name
= "replication",
730 .instance_size
= sizeof(BDRVReplicationState
),
732 .bdrv_open
= replication_open
,
733 .bdrv_close
= replication_close
,
734 .bdrv_child_perm
= replication_child_perm
,
736 .bdrv_getlength
= replication_getlength
,
737 .bdrv_co_readv
= replication_co_readv
,
738 .bdrv_co_writev
= replication_co_writev
,
742 .has_variable_length
= true,
743 .strong_runtime_opts
= replication_strong_runtime_opts
,
746 static void bdrv_replication_init(void)
748 bdrv_register(&bdrv_replication
);
751 block_init(bdrv_replication_init
);