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 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
97 opts
= qemu_opts_create(&replication_runtime_opts
, NULL
, 0, &error_abort
);
98 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
102 mode
= qemu_opt_get(opts
, REPLICATION_MODE
);
104 error_setg(errp
, "Missing the option mode");
108 if (!strcmp(mode
, "primary")) {
109 s
->mode
= REPLICATION_MODE_PRIMARY
;
110 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
113 "The primary side does not support option top-id");
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
);
121 error_setg(errp
, "Missing the option top-id");
126 "The option mode's value should be primary or secondary");
130 s
->rs
= replication_new(bs
, &replication_ops
);
139 static void replication_close(BlockDriverState
*bs
)
141 BDRVReplicationState
*s
= bs
->opaque
;
145 if (s
->stage
== BLOCK_REPLICATION_RUNNING
) {
146 replication_stop(s
->rs
, false, NULL
);
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);
154 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
158 replication_remove(s
->rs
);
161 static void replication_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
163 BlockReopenQueue
*reopen_queue
,
164 uint64_t perm
, uint64_t shared
,
165 uint64_t *nperm
, uint64_t *nshared
)
167 if (role
& BDRV_CHILD_PRIMARY
) {
168 *nperm
= BLK_PERM_CONSISTENT_READ
;
173 if ((bs
->open_flags
& (BDRV_O_INACTIVE
| BDRV_O_RDWR
)) == BDRV_O_RDWR
) {
174 *nperm
|= BLK_PERM_WRITE
;
176 *nshared
= BLK_PERM_CONSISTENT_READ
178 | BLK_PERM_WRITE_UNCHANGED
;
182 static int64_t coroutine_fn
replication_co_getlength(BlockDriverState
*bs
)
184 return bdrv_co_getlength(bs
->file
->bs
);
187 static int replication_get_io_status(BDRVReplicationState
*s
)
190 case BLOCK_REPLICATION_NONE
:
192 case BLOCK_REPLICATION_RUNNING
:
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
:
200 * active commit job completes, and active disk and secondary_disk
201 * is swapped, so we can operate bs->file directly
203 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
209 static int replication_return_value(BDRVReplicationState
*s
, int ret
)
211 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
223 static int coroutine_fn GRAPH_RDLOCK
224 replication_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
225 int remaining_sectors
, QEMUIOVector
*qiov
)
227 BDRVReplicationState
*s
= bs
->opaque
;
230 if (s
->mode
== REPLICATION_MODE_PRIMARY
) {
231 /* We only use it to forward primary write requests */
235 ret
= replication_get_io_status(s
);
240 ret
= bdrv_co_preadv(bs
->file
, sector_num
* BDRV_SECTOR_SIZE
,
241 remaining_sectors
* BDRV_SECTOR_SIZE
, qiov
, 0);
243 return replication_return_value(s
, ret
);
246 static int coroutine_fn GRAPH_RDLOCK
247 replication_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
248 int remaining_sectors
, QEMUIOVector
*qiov
, int flags
)
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
;
259 ret
= replication_get_io_status(s
);
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
);
271 * Failover failed, only write to active disk if the sectors
272 * have already been allocated in active disk/hidden disk.
274 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
275 while (remaining_sectors
> 0) {
278 ret
= bdrv_is_allocated_above(top
->bs
, base
->bs
, false,
279 sector_num
* BDRV_SECTOR_SIZE
,
280 remaining_sectors
* BDRV_SECTOR_SIZE
,
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
);
291 target
= ret
? top
: base
;
292 ret
= bdrv_co_pwritev(target
, sector_num
* BDRV_SECTOR_SIZE
,
293 n
* BDRV_SECTOR_SIZE
, &hd_qiov
, 0);
298 remaining_sectors
-= n
;
304 qemu_iovec_destroy(&hd_qiov
);
309 static void secondary_do_checkpoint(BlockDriverState
*bs
, Error
**errp
)
311 BDRVReplicationState
*s
= bs
->opaque
;
312 BdrvChild
*active_disk
= bs
->file
;
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 (!active_disk
->bs
->drv
) {
328 error_setg(errp
, "Active disk %s is ejected",
329 active_disk
->bs
->node_name
);
333 ret
= bdrv_make_empty(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 ret
= bdrv_make_empty(s
->hidden_disk
, errp
);
350 /* This function is supposed to be called twice:
351 * first with writable = true, then with writable = false.
352 * The first call puts s->hidden_disk and s->secondary_disk in
353 * r/w mode, and the second puts them back in their original state.
355 static void reopen_backing_file(BlockDriverState
*bs
, bool writable
,
358 BDRVReplicationState
*s
= bs
->opaque
;
359 BdrvChild
*hidden_disk
, *secondary_disk
;
360 BlockReopenQueue
*reopen_queue
= NULL
;
363 * s->hidden_disk and s->secondary_disk may not be set yet, as they will
364 * only be set after the children are writable.
366 hidden_disk
= bs
->file
->bs
->backing
;
367 secondary_disk
= hidden_disk
->bs
->backing
;
370 s
->orig_hidden_read_only
= bdrv_is_read_only(hidden_disk
->bs
);
371 s
->orig_secondary_read_only
= bdrv_is_read_only(secondary_disk
->bs
);
374 if (s
->orig_hidden_read_only
) {
375 QDict
*opts
= qdict_new();
376 qdict_put_bool(opts
, BDRV_OPT_READ_ONLY
, !writable
);
377 reopen_queue
= bdrv_reopen_queue(reopen_queue
, hidden_disk
->bs
,
381 if (s
->orig_secondary_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
, secondary_disk
->bs
,
389 AioContext
*ctx
= bdrv_get_aio_context(bs
);
390 if (ctx
!= qemu_get_aio_context()) {
391 aio_context_release(ctx
);
393 bdrv_reopen_multiple(reopen_queue
, errp
);
394 if (ctx
!= qemu_get_aio_context()) {
395 aio_context_acquire(ctx
);
400 static void backup_job_cleanup(BlockDriverState
*bs
)
402 BDRVReplicationState
*s
= bs
->opaque
;
403 BlockDriverState
*top_bs
;
405 s
->backup_job
= NULL
;
407 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
411 bdrv_op_unblock_all(top_bs
, s
->blocker
);
412 error_free(s
->blocker
);
413 reopen_backing_file(bs
, false, NULL
);
416 static void backup_job_completed(void *opaque
, int ret
)
418 BlockDriverState
*bs
= opaque
;
419 BDRVReplicationState
*s
= bs
->opaque
;
421 if (s
->stage
!= BLOCK_REPLICATION_FAILOVER
) {
422 /* The backup job is cancelled unexpectedly */
426 backup_job_cleanup(bs
);
429 static bool check_top_bs(BlockDriverState
*top_bs
, BlockDriverState
*bs
)
433 /* The bs itself is the top_bs */
438 /* Iterate over top_bs's children */
439 QLIST_FOREACH(child
, &top_bs
->children
, next
) {
440 if (child
->bs
== bs
|| check_top_bs(child
->bs
, bs
)) {
448 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
451 BlockDriverState
*bs
= rs
->opaque
;
452 BDRVReplicationState
*s
;
453 BlockDriverState
*top_bs
;
454 BdrvChild
*active_disk
, *hidden_disk
, *secondary_disk
;
455 int64_t active_length
, hidden_length
, disk_length
;
456 AioContext
*aio_context
;
457 Error
*local_err
= NULL
;
458 BackupPerf perf
= { .use_copy_range
= true, .max_workers
= 1 };
460 aio_context
= bdrv_get_aio_context(bs
);
461 aio_context_acquire(aio_context
);
464 if (s
->stage
== BLOCK_REPLICATION_DONE
||
465 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
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.
471 aio_context_release(aio_context
);
475 if (s
->stage
!= BLOCK_REPLICATION_NONE
) {
476 error_setg(errp
, "Block replication is running or done");
477 aio_context_release(aio_context
);
481 if (s
->mode
!= mode
) {
482 error_setg(errp
, "The parameter mode's value is invalid, needs %d,"
483 " but got %d", s
->mode
, mode
);
484 aio_context_release(aio_context
);
489 case REPLICATION_MODE_PRIMARY
:
491 case REPLICATION_MODE_SECONDARY
:
492 active_disk
= bs
->file
;
493 if (!active_disk
|| !active_disk
->bs
|| !active_disk
->bs
->backing
) {
494 error_setg(errp
, "Active disk doesn't have backing file");
495 aio_context_release(aio_context
);
499 hidden_disk
= active_disk
->bs
->backing
;
500 if (!hidden_disk
->bs
|| !hidden_disk
->bs
->backing
) {
501 error_setg(errp
, "Hidden disk doesn't have backing file");
502 aio_context_release(aio_context
);
506 secondary_disk
= hidden_disk
->bs
->backing
;
507 if (!secondary_disk
->bs
|| !bdrv_has_blk(secondary_disk
->bs
)) {
508 error_setg(errp
, "The secondary disk doesn't have block backend");
509 aio_context_release(aio_context
);
513 /* verify the length */
514 active_length
= bdrv_getlength(active_disk
->bs
);
515 hidden_length
= bdrv_getlength(hidden_disk
->bs
);
516 disk_length
= bdrv_getlength(secondary_disk
->bs
);
517 if (active_length
< 0 || hidden_length
< 0 || disk_length
< 0 ||
518 active_length
!= hidden_length
|| hidden_length
!= disk_length
) {
519 error_setg(errp
, "Active disk, hidden disk, secondary disk's length"
520 " are not the same");
521 aio_context_release(aio_context
);
525 /* Must be true, or the bdrv_getlength() calls would have failed */
526 assert(active_disk
->bs
->drv
&& hidden_disk
->bs
->drv
);
528 if (!active_disk
->bs
->drv
->bdrv_make_empty
||
529 !hidden_disk
->bs
->drv
->bdrv_make_empty
) {
531 "Active disk or hidden disk doesn't support make_empty");
532 aio_context_release(aio_context
);
536 /* reopen the backing file in r/w mode */
537 reopen_backing_file(bs
, true, &local_err
);
539 error_propagate(errp
, local_err
);
540 aio_context_release(aio_context
);
544 bdrv_ref(hidden_disk
->bs
);
545 s
->hidden_disk
= bdrv_attach_child(bs
, hidden_disk
->bs
, "hidden disk",
546 &child_of_bds
, BDRV_CHILD_DATA
,
549 error_propagate(errp
, local_err
);
550 aio_context_release(aio_context
);
554 bdrv_ref(secondary_disk
->bs
);
555 s
->secondary_disk
= bdrv_attach_child(bs
, secondary_disk
->bs
,
556 "secondary disk", &child_of_bds
,
557 BDRV_CHILD_DATA
, &local_err
);
559 error_propagate(errp
, local_err
);
560 aio_context_release(aio_context
);
564 /* start backup job now */
565 error_setg(&s
->blocker
,
566 "Block device is in use by internal backup job");
568 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
569 if (!top_bs
|| !bdrv_is_root_node(top_bs
) ||
570 !check_top_bs(top_bs
, bs
)) {
571 error_setg(errp
, "No top_bs or it is invalid");
572 reopen_backing_file(bs
, false, NULL
);
573 aio_context_release(aio_context
);
576 bdrv_op_block_all(top_bs
, s
->blocker
);
577 bdrv_op_unblock(top_bs
, BLOCK_OP_TYPE_DATAPLANE
, s
->blocker
);
579 s
->backup_job
= backup_job_create(
580 NULL
, s
->secondary_disk
->bs
, s
->hidden_disk
->bs
,
581 0, MIRROR_SYNC_MODE_NONE
, NULL
, 0, false, NULL
,
583 BLOCKDEV_ON_ERROR_REPORT
,
584 BLOCKDEV_ON_ERROR_REPORT
, JOB_INTERNAL
,
585 backup_job_completed
, bs
, NULL
, &local_err
);
587 error_propagate(errp
, local_err
);
588 backup_job_cleanup(bs
);
589 aio_context_release(aio_context
);
592 job_start(&s
->backup_job
->job
);
595 aio_context_release(aio_context
);
599 s
->stage
= BLOCK_REPLICATION_RUNNING
;
601 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
602 secondary_do_checkpoint(bs
, errp
);
606 aio_context_release(aio_context
);
609 static void replication_do_checkpoint(ReplicationState
*rs
, Error
**errp
)
611 BlockDriverState
*bs
= rs
->opaque
;
612 BDRVReplicationState
*s
;
613 AioContext
*aio_context
;
615 aio_context
= bdrv_get_aio_context(bs
);
616 aio_context_acquire(aio_context
);
619 if (s
->stage
== BLOCK_REPLICATION_DONE
||
620 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
622 * This case happens when a secondary was promoted to primary.
623 * Ignore the request because the secondary side of replication
624 * doesn't have to do anything anymore.
626 aio_context_release(aio_context
);
630 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
631 secondary_do_checkpoint(bs
, errp
);
633 aio_context_release(aio_context
);
636 static void replication_get_error(ReplicationState
*rs
, Error
**errp
)
638 BlockDriverState
*bs
= rs
->opaque
;
639 BDRVReplicationState
*s
;
640 AioContext
*aio_context
;
642 aio_context
= bdrv_get_aio_context(bs
);
643 aio_context_acquire(aio_context
);
646 if (s
->stage
== BLOCK_REPLICATION_NONE
) {
647 error_setg(errp
, "Block replication is not running");
648 aio_context_release(aio_context
);
653 error_setg(errp
, "I/O error occurred");
654 aio_context_release(aio_context
);
657 aio_context_release(aio_context
);
660 static void replication_done(void *opaque
, int ret
)
662 BlockDriverState
*bs
= opaque
;
663 BDRVReplicationState
*s
= bs
->opaque
;
666 s
->stage
= BLOCK_REPLICATION_DONE
;
668 bdrv_unref_child(bs
, s
->secondary_disk
);
669 s
->secondary_disk
= NULL
;
670 bdrv_unref_child(bs
, s
->hidden_disk
);
671 s
->hidden_disk
= NULL
;
674 s
->stage
= BLOCK_REPLICATION_FAILOVER_FAILED
;
679 static void replication_stop(ReplicationState
*rs
, bool failover
, Error
**errp
)
681 BlockDriverState
*bs
= rs
->opaque
;
682 BDRVReplicationState
*s
;
683 AioContext
*aio_context
;
685 aio_context
= bdrv_get_aio_context(bs
);
686 aio_context_acquire(aio_context
);
689 if (s
->stage
== BLOCK_REPLICATION_DONE
||
690 s
->stage
== BLOCK_REPLICATION_FAILOVER
) {
692 * This case happens when a secondary was promoted to primary.
693 * Ignore the request because the secondary side of replication
694 * doesn't have to do anything anymore.
696 aio_context_release(aio_context
);
700 if (s
->stage
!= BLOCK_REPLICATION_RUNNING
) {
701 error_setg(errp
, "Block replication is not running");
702 aio_context_release(aio_context
);
707 case REPLICATION_MODE_PRIMARY
:
708 s
->stage
= BLOCK_REPLICATION_DONE
;
711 case REPLICATION_MODE_SECONDARY
:
713 * This BDS will be closed, and the job should be completed
714 * before the BDS is closed, because we will access hidden
715 * disk, secondary disk in backup_job_completed().
718 aio_context_release(aio_context
);
719 job_cancel_sync(&s
->backup_job
->job
, true);
720 aio_context_acquire(aio_context
);
724 secondary_do_checkpoint(bs
, errp
);
725 s
->stage
= BLOCK_REPLICATION_DONE
;
726 aio_context_release(aio_context
);
730 s
->stage
= BLOCK_REPLICATION_FAILOVER
;
731 s
->commit_job
= commit_active_start(
732 NULL
, bs
->file
->bs
, s
->secondary_disk
->bs
,
733 JOB_INTERNAL
, 0, BLOCKDEV_ON_ERROR_REPORT
,
734 NULL
, replication_done
, bs
, true, errp
);
737 aio_context_release(aio_context
);
740 aio_context_release(aio_context
);
743 static const char *const replication_strong_runtime_opts
[] = {
750 static BlockDriver bdrv_replication
= {
751 .format_name
= "replication",
752 .instance_size
= sizeof(BDRVReplicationState
),
754 .bdrv_open
= replication_open
,
755 .bdrv_close
= replication_close
,
756 .bdrv_child_perm
= replication_child_perm
,
758 .bdrv_co_getlength
= replication_co_getlength
,
759 .bdrv_co_readv
= replication_co_readv
,
760 .bdrv_co_writev
= replication_co_writev
,
764 .has_variable_length
= true,
765 .strong_runtime_opts
= replication_strong_runtime_opts
,
768 static void bdrv_replication_init(void)
770 bdrv_register(&bdrv_replication
);
773 block_init(bdrv_replication_init
);