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-common.h"
17 #include "block/nbd.h"
18 #include "block/blockjob.h"
19 #include "block/block_int.h"
20 #include "block/block_backup.h"
21 #include "sysemu/block-backend.h"
22 #include "qapi/error.h"
23 #include "replication.h"
25 typedef struct BDRVReplicationState
{
27 int replication_state
;
28 BdrvChild
*active_disk
;
29 BdrvChild
*hidden_disk
;
30 BdrvChild
*secondary_disk
;
34 int orig_hidden_flags
;
35 int orig_secondary_flags
;
37 } BDRVReplicationState
;
40 BLOCK_REPLICATION_NONE
, /* block replication is not started */
41 BLOCK_REPLICATION_RUNNING
, /* block replication is running */
42 BLOCK_REPLICATION_FAILOVER
, /* failover is running in background */
43 BLOCK_REPLICATION_FAILOVER_FAILED
, /* failover failed */
44 BLOCK_REPLICATION_DONE
, /* block replication is done */
47 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
49 static void replication_do_checkpoint(ReplicationState
*rs
, Error
**errp
);
50 static void replication_get_error(ReplicationState
*rs
, Error
**errp
);
51 static void replication_stop(ReplicationState
*rs
, bool failover
,
54 #define REPLICATION_MODE "mode"
55 #define REPLICATION_TOP_ID "top-id"
56 static QemuOptsList replication_runtime_opts
= {
57 .name
= "replication",
58 .head
= QTAILQ_HEAD_INITIALIZER(replication_runtime_opts
.head
),
61 .name
= REPLICATION_MODE
,
62 .type
= QEMU_OPT_STRING
,
65 .name
= REPLICATION_TOP_ID
,
66 .type
= QEMU_OPT_STRING
,
72 static ReplicationOps replication_ops
= {
73 .start
= replication_start
,
74 .checkpoint
= replication_do_checkpoint
,
75 .get_error
= replication_get_error
,
76 .stop
= replication_stop
,
79 static int replication_open(BlockDriverState
*bs
, QDict
*options
,
80 int flags
, Error
**errp
)
83 BDRVReplicationState
*s
= bs
->opaque
;
84 Error
*local_err
= NULL
;
85 QemuOpts
*opts
= NULL
;
90 opts
= qemu_opts_create(&replication_runtime_opts
, NULL
, 0, &error_abort
);
91 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
96 mode
= qemu_opt_get(opts
, REPLICATION_MODE
);
98 error_setg(&local_err
, "Missing the option mode");
102 if (!strcmp(mode
, "primary")) {
103 s
->mode
= REPLICATION_MODE_PRIMARY
;
104 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
106 error_setg(&local_err
, "The primary side does not support option top-id");
109 } else if (!strcmp(mode
, "secondary")) {
110 s
->mode
= REPLICATION_MODE_SECONDARY
;
111 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
112 s
->top_id
= g_strdup(top_id
);
114 error_setg(&local_err
, "Missing the option top-id");
118 error_setg(&local_err
,
119 "The option mode's value should be primary or secondary");
123 s
->rs
= replication_new(bs
, &replication_ops
);
129 error_propagate(errp
, local_err
);
134 static void replication_close(BlockDriverState
*bs
)
136 BDRVReplicationState
*s
= bs
->opaque
;
138 if (s
->replication_state
== BLOCK_REPLICATION_RUNNING
) {
139 replication_stop(s
->rs
, false, NULL
);
142 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
146 replication_remove(s
->rs
);
149 static int64_t replication_getlength(BlockDriverState
*bs
)
151 return bdrv_getlength(bs
->file
->bs
);
154 static int replication_get_io_status(BDRVReplicationState
*s
)
156 switch (s
->replication_state
) {
157 case BLOCK_REPLICATION_NONE
:
159 case BLOCK_REPLICATION_RUNNING
:
161 case BLOCK_REPLICATION_FAILOVER
:
162 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
163 case BLOCK_REPLICATION_FAILOVER_FAILED
:
164 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 1;
165 case BLOCK_REPLICATION_DONE
:
167 * active commit job completes, and active disk and secondary_disk
168 * is swapped, so we can operate bs->file directly
170 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
176 static int replication_return_value(BDRVReplicationState
*s
, int ret
)
178 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
190 static coroutine_fn
int replication_co_readv(BlockDriverState
*bs
,
192 int remaining_sectors
,
195 BDRVReplicationState
*s
= bs
->opaque
;
196 BdrvChild
*child
= s
->secondary_disk
;
197 BlockJob
*job
= NULL
;
201 if (s
->mode
== REPLICATION_MODE_PRIMARY
) {
202 /* We only use it to forward primary write requests */
206 ret
= replication_get_io_status(s
);
211 if (child
&& child
->bs
) {
212 job
= child
->bs
->job
;
216 backup_wait_for_overlapping_requests(child
->bs
->job
, sector_num
,
218 backup_cow_request_begin(&req
, child
->bs
->job
, sector_num
,
220 ret
= bdrv_co_readv(bs
->file
, sector_num
, remaining_sectors
,
222 backup_cow_request_end(&req
);
226 ret
= bdrv_co_readv(bs
->file
, sector_num
, remaining_sectors
, qiov
);
228 return replication_return_value(s
, ret
);
231 static coroutine_fn
int replication_co_writev(BlockDriverState
*bs
,
233 int remaining_sectors
,
236 BDRVReplicationState
*s
= bs
->opaque
;
237 QEMUIOVector hd_qiov
;
238 uint64_t bytes_done
= 0;
239 BdrvChild
*top
= bs
->file
;
240 BdrvChild
*base
= s
->secondary_disk
;
244 ret
= replication_get_io_status(s
);
250 ret
= bdrv_co_writev(top
, sector_num
,
251 remaining_sectors
, qiov
);
252 return replication_return_value(s
, ret
);
256 * Failover failed, only write to active disk if the sectors
257 * have already been allocated in active disk/hidden disk.
259 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
260 while (remaining_sectors
> 0) {
261 ret
= bdrv_is_allocated_above(top
->bs
, base
->bs
, sector_num
,
262 remaining_sectors
, &n
);
267 qemu_iovec_reset(&hd_qiov
);
268 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, n
* BDRV_SECTOR_SIZE
);
270 target
= ret
? top
: base
;
271 ret
= bdrv_co_writev(target
, sector_num
, n
, &hd_qiov
);
276 remaining_sectors
-= n
;
278 bytes_done
+= n
* BDRV_SECTOR_SIZE
;
282 qemu_iovec_destroy(&hd_qiov
);
287 static bool replication_recurse_is_first_non_filter(BlockDriverState
*bs
,
288 BlockDriverState
*candidate
)
290 return bdrv_recurse_is_first_non_filter(bs
->file
->bs
, candidate
);
293 static void secondary_do_checkpoint(BDRVReplicationState
*s
, Error
**errp
)
295 Error
*local_err
= NULL
;
298 if (!s
->secondary_disk
->bs
->job
) {
299 error_setg(errp
, "Backup job was cancelled unexpectedly");
303 backup_do_checkpoint(s
->secondary_disk
->bs
->job
, &local_err
);
305 error_propagate(errp
, local_err
);
309 ret
= s
->active_disk
->bs
->drv
->bdrv_make_empty(s
->active_disk
->bs
);
311 error_setg(errp
, "Cannot make active disk empty");
315 ret
= s
->hidden_disk
->bs
->drv
->bdrv_make_empty(s
->hidden_disk
->bs
);
317 error_setg(errp
, "Cannot make hidden disk empty");
322 static void reopen_backing_file(BDRVReplicationState
*s
, bool writable
,
325 BlockReopenQueue
*reopen_queue
= NULL
;
326 int orig_hidden_flags
, orig_secondary_flags
;
327 int new_hidden_flags
, new_secondary_flags
;
328 Error
*local_err
= NULL
;
331 orig_hidden_flags
= s
->orig_hidden_flags
=
332 bdrv_get_flags(s
->hidden_disk
->bs
);
333 new_hidden_flags
= (orig_hidden_flags
| BDRV_O_RDWR
) &
335 orig_secondary_flags
= s
->orig_secondary_flags
=
336 bdrv_get_flags(s
->secondary_disk
->bs
);
337 new_secondary_flags
= (orig_secondary_flags
| BDRV_O_RDWR
) &
340 orig_hidden_flags
= (s
->orig_hidden_flags
| BDRV_O_RDWR
) &
342 new_hidden_flags
= s
->orig_hidden_flags
;
343 orig_secondary_flags
= (s
->orig_secondary_flags
| BDRV_O_RDWR
) &
345 new_secondary_flags
= s
->orig_secondary_flags
;
348 if (orig_hidden_flags
!= new_hidden_flags
) {
349 reopen_queue
= bdrv_reopen_queue(reopen_queue
, s
->hidden_disk
->bs
, NULL
,
353 if (!(orig_secondary_flags
& BDRV_O_RDWR
)) {
354 reopen_queue
= bdrv_reopen_queue(reopen_queue
, s
->secondary_disk
->bs
,
355 NULL
, new_secondary_flags
);
359 bdrv_reopen_multiple(reopen_queue
, &local_err
);
360 error_propagate(errp
, local_err
);
364 static void backup_job_cleanup(BDRVReplicationState
*s
)
366 BlockDriverState
*top_bs
;
368 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
372 bdrv_op_unblock_all(top_bs
, s
->blocker
);
373 error_free(s
->blocker
);
374 reopen_backing_file(s
, false, NULL
);
377 static void backup_job_completed(void *opaque
, int ret
)
379 BDRVReplicationState
*s
= opaque
;
381 if (s
->replication_state
!= BLOCK_REPLICATION_FAILOVER
) {
382 /* The backup job is cancelled unexpectedly */
386 backup_job_cleanup(s
);
389 static bool check_top_bs(BlockDriverState
*top_bs
, BlockDriverState
*bs
)
393 /* The bs itself is the top_bs */
398 /* Iterate over top_bs's children */
399 QLIST_FOREACH(child
, &top_bs
->children
, next
) {
400 if (child
->bs
== bs
|| check_top_bs(child
->bs
, bs
)) {
408 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
411 BlockDriverState
*bs
= rs
->opaque
;
412 BDRVReplicationState
*s
;
413 BlockDriverState
*top_bs
;
414 int64_t active_length
, hidden_length
, disk_length
;
415 AioContext
*aio_context
;
416 Error
*local_err
= NULL
;
418 aio_context
= bdrv_get_aio_context(bs
);
419 aio_context_acquire(aio_context
);
422 if (s
->replication_state
!= BLOCK_REPLICATION_NONE
) {
423 error_setg(errp
, "Block replication is running or done");
424 aio_context_release(aio_context
);
428 if (s
->mode
!= mode
) {
429 error_setg(errp
, "The parameter mode's value is invalid, needs %d,"
430 " but got %d", s
->mode
, mode
);
431 aio_context_release(aio_context
);
436 case REPLICATION_MODE_PRIMARY
:
438 case REPLICATION_MODE_SECONDARY
:
439 s
->active_disk
= bs
->file
;
440 if (!s
->active_disk
|| !s
->active_disk
->bs
||
441 !s
->active_disk
->bs
->backing
) {
442 error_setg(errp
, "Active disk doesn't have backing file");
443 aio_context_release(aio_context
);
447 s
->hidden_disk
= s
->active_disk
->bs
->backing
;
448 if (!s
->hidden_disk
->bs
|| !s
->hidden_disk
->bs
->backing
) {
449 error_setg(errp
, "Hidden disk doesn't have backing file");
450 aio_context_release(aio_context
);
454 s
->secondary_disk
= s
->hidden_disk
->bs
->backing
;
455 if (!s
->secondary_disk
->bs
|| !bdrv_has_blk(s
->secondary_disk
->bs
)) {
456 error_setg(errp
, "The secondary disk doesn't have block backend");
457 aio_context_release(aio_context
);
461 /* verify the length */
462 active_length
= bdrv_getlength(s
->active_disk
->bs
);
463 hidden_length
= bdrv_getlength(s
->hidden_disk
->bs
);
464 disk_length
= bdrv_getlength(s
->secondary_disk
->bs
);
465 if (active_length
< 0 || hidden_length
< 0 || disk_length
< 0 ||
466 active_length
!= hidden_length
|| hidden_length
!= disk_length
) {
467 error_setg(errp
, "Active disk, hidden disk, secondary disk's length"
468 " are not the same");
469 aio_context_release(aio_context
);
473 if (!s
->active_disk
->bs
->drv
->bdrv_make_empty
||
474 !s
->hidden_disk
->bs
->drv
->bdrv_make_empty
) {
476 "Active disk or hidden disk doesn't support make_empty");
477 aio_context_release(aio_context
);
481 /* reopen the backing file in r/w mode */
482 reopen_backing_file(s
, true, &local_err
);
484 error_propagate(errp
, local_err
);
485 aio_context_release(aio_context
);
489 /* start backup job now */
490 error_setg(&s
->blocker
,
491 "Block device is in use by internal backup job");
493 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
494 if (!top_bs
|| !bdrv_is_root_node(top_bs
) ||
495 !check_top_bs(top_bs
, bs
)) {
496 error_setg(errp
, "No top_bs or it is invalid");
497 reopen_backing_file(s
, false, NULL
);
498 aio_context_release(aio_context
);
501 bdrv_op_block_all(top_bs
, s
->blocker
);
502 bdrv_op_unblock(top_bs
, BLOCK_OP_TYPE_DATAPLANE
, s
->blocker
);
504 backup_start("replication-backup", s
->secondary_disk
->bs
,
505 s
->hidden_disk
->bs
, 0, MIRROR_SYNC_MODE_NONE
, NULL
, false,
506 BLOCKDEV_ON_ERROR_REPORT
, BLOCKDEV_ON_ERROR_REPORT
,
507 backup_job_completed
, s
, NULL
, &local_err
);
509 error_propagate(errp
, local_err
);
510 backup_job_cleanup(s
);
511 aio_context_release(aio_context
);
516 aio_context_release(aio_context
);
520 s
->replication_state
= BLOCK_REPLICATION_RUNNING
;
522 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
523 secondary_do_checkpoint(s
, errp
);
527 aio_context_release(aio_context
);
530 static void replication_do_checkpoint(ReplicationState
*rs
, Error
**errp
)
532 BlockDriverState
*bs
= rs
->opaque
;
533 BDRVReplicationState
*s
;
534 AioContext
*aio_context
;
536 aio_context
= bdrv_get_aio_context(bs
);
537 aio_context_acquire(aio_context
);
540 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
541 secondary_do_checkpoint(s
, errp
);
543 aio_context_release(aio_context
);
546 static void replication_get_error(ReplicationState
*rs
, Error
**errp
)
548 BlockDriverState
*bs
= rs
->opaque
;
549 BDRVReplicationState
*s
;
550 AioContext
*aio_context
;
552 aio_context
= bdrv_get_aio_context(bs
);
553 aio_context_acquire(aio_context
);
556 if (s
->replication_state
!= BLOCK_REPLICATION_RUNNING
) {
557 error_setg(errp
, "Block replication is not running");
558 aio_context_release(aio_context
);
563 error_setg(errp
, "I/O error occurred");
564 aio_context_release(aio_context
);
567 aio_context_release(aio_context
);
570 static void replication_done(void *opaque
, int ret
)
572 BlockDriverState
*bs
= opaque
;
573 BDRVReplicationState
*s
= bs
->opaque
;
576 s
->replication_state
= BLOCK_REPLICATION_DONE
;
578 /* refresh top bs's filename */
579 bdrv_refresh_filename(bs
);
580 s
->active_disk
= NULL
;
581 s
->secondary_disk
= NULL
;
582 s
->hidden_disk
= NULL
;
585 s
->replication_state
= BLOCK_REPLICATION_FAILOVER_FAILED
;
590 static void replication_stop(ReplicationState
*rs
, bool failover
, 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
->replication_state
!= BLOCK_REPLICATION_RUNNING
) {
601 error_setg(errp
, "Block replication is not running");
602 aio_context_release(aio_context
);
607 case REPLICATION_MODE_PRIMARY
:
608 s
->replication_state
= BLOCK_REPLICATION_DONE
;
611 case REPLICATION_MODE_SECONDARY
:
613 * This BDS will be closed, and the job should be completed
614 * before the BDS is closed, because we will access hidden
615 * disk, secondary disk in backup_job_completed().
617 if (s
->secondary_disk
->bs
->job
) {
618 block_job_cancel_sync(s
->secondary_disk
->bs
->job
);
622 secondary_do_checkpoint(s
, errp
);
623 s
->replication_state
= BLOCK_REPLICATION_DONE
;
624 aio_context_release(aio_context
);
628 s
->replication_state
= BLOCK_REPLICATION_FAILOVER
;
629 commit_active_start("replication-commit", s
->active_disk
->bs
,
630 s
->secondary_disk
->bs
, 0, BLOCKDEV_ON_ERROR_REPORT
,
635 aio_context_release(aio_context
);
638 aio_context_release(aio_context
);
641 BlockDriver bdrv_replication
= {
642 .format_name
= "replication",
643 .protocol_name
= "replication",
644 .instance_size
= sizeof(BDRVReplicationState
),
646 .bdrv_open
= replication_open
,
647 .bdrv_close
= replication_close
,
649 .bdrv_getlength
= replication_getlength
,
650 .bdrv_co_readv
= replication_co_readv
,
651 .bdrv_co_writev
= replication_co_writev
,
654 .bdrv_recurse_is_first_non_filter
= replication_recurse_is_first_non_filter
,
656 .has_variable_length
= true,
659 static void bdrv_replication_init(void)
661 bdrv_register(&bdrv_replication
);
664 block_init(bdrv_replication_init
);