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 } else if (!strcmp(mode
, "secondary")) {
105 s
->mode
= REPLICATION_MODE_SECONDARY
;
106 top_id
= qemu_opt_get(opts
, REPLICATION_TOP_ID
);
107 s
->top_id
= g_strdup(top_id
);
109 error_setg(&local_err
, "Missing the option top-id");
113 error_setg(&local_err
,
114 "The option mode's value should be primary or secondary");
118 s
->rs
= replication_new(bs
, &replication_ops
);
124 error_propagate(errp
, local_err
);
129 static void replication_close(BlockDriverState
*bs
)
131 BDRVReplicationState
*s
= bs
->opaque
;
133 if (s
->replication_state
== BLOCK_REPLICATION_RUNNING
) {
134 replication_stop(s
->rs
, false, NULL
);
137 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
141 replication_remove(s
->rs
);
144 static int64_t replication_getlength(BlockDriverState
*bs
)
146 return bdrv_getlength(bs
->file
->bs
);
149 static int replication_get_io_status(BDRVReplicationState
*s
)
151 switch (s
->replication_state
) {
152 case BLOCK_REPLICATION_NONE
:
154 case BLOCK_REPLICATION_RUNNING
:
156 case BLOCK_REPLICATION_FAILOVER
:
157 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
158 case BLOCK_REPLICATION_FAILOVER_FAILED
:
159 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 1;
160 case BLOCK_REPLICATION_DONE
:
162 * active commit job completes, and active disk and secondary_disk
163 * is swapped, so we can operate bs->file directly
165 return s
->mode
== REPLICATION_MODE_PRIMARY
? -EIO
: 0;
171 static int replication_return_value(BDRVReplicationState
*s
, int ret
)
173 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
185 static coroutine_fn
int replication_co_readv(BlockDriverState
*bs
,
187 int remaining_sectors
,
190 BDRVReplicationState
*s
= bs
->opaque
;
191 BdrvChild
*child
= s
->secondary_disk
;
192 BlockJob
*job
= NULL
;
196 if (s
->mode
== REPLICATION_MODE_PRIMARY
) {
197 /* We only use it to forward primary write requests */
201 ret
= replication_get_io_status(s
);
206 if (child
&& child
->bs
) {
207 job
= child
->bs
->job
;
211 backup_wait_for_overlapping_requests(child
->bs
->job
, sector_num
,
213 backup_cow_request_begin(&req
, child
->bs
->job
, sector_num
,
215 ret
= bdrv_co_readv(bs
->file
, sector_num
, remaining_sectors
,
217 backup_cow_request_end(&req
);
221 ret
= bdrv_co_readv(bs
->file
, sector_num
, remaining_sectors
, qiov
);
223 return replication_return_value(s
, ret
);
226 static coroutine_fn
int replication_co_writev(BlockDriverState
*bs
,
228 int remaining_sectors
,
231 BDRVReplicationState
*s
= bs
->opaque
;
232 QEMUIOVector hd_qiov
;
233 uint64_t bytes_done
= 0;
234 BdrvChild
*top
= bs
->file
;
235 BdrvChild
*base
= s
->secondary_disk
;
239 ret
= replication_get_io_status(s
);
245 ret
= bdrv_co_writev(top
, sector_num
,
246 remaining_sectors
, qiov
);
247 return replication_return_value(s
, ret
);
251 * Failover failed, only write to active disk if the sectors
252 * have already been allocated in active disk/hidden disk.
254 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
255 while (remaining_sectors
> 0) {
256 ret
= bdrv_is_allocated_above(top
->bs
, base
->bs
, sector_num
,
257 remaining_sectors
, &n
);
262 qemu_iovec_reset(&hd_qiov
);
263 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, n
* BDRV_SECTOR_SIZE
);
265 target
= ret
? top
: base
;
266 ret
= bdrv_co_writev(target
, sector_num
, n
, &hd_qiov
);
271 remaining_sectors
-= n
;
273 bytes_done
+= n
* BDRV_SECTOR_SIZE
;
277 qemu_iovec_destroy(&hd_qiov
);
282 static bool replication_recurse_is_first_non_filter(BlockDriverState
*bs
,
283 BlockDriverState
*candidate
)
285 return bdrv_recurse_is_first_non_filter(bs
->file
->bs
, candidate
);
288 static void secondary_do_checkpoint(BDRVReplicationState
*s
, Error
**errp
)
290 Error
*local_err
= NULL
;
293 if (!s
->secondary_disk
->bs
->job
) {
294 error_setg(errp
, "Backup job was cancelled unexpectedly");
298 backup_do_checkpoint(s
->secondary_disk
->bs
->job
, &local_err
);
300 error_propagate(errp
, local_err
);
304 ret
= s
->active_disk
->bs
->drv
->bdrv_make_empty(s
->active_disk
->bs
);
306 error_setg(errp
, "Cannot make active disk empty");
310 ret
= s
->hidden_disk
->bs
->drv
->bdrv_make_empty(s
->hidden_disk
->bs
);
312 error_setg(errp
, "Cannot make hidden disk empty");
317 static void reopen_backing_file(BDRVReplicationState
*s
, bool writable
,
320 BlockReopenQueue
*reopen_queue
= NULL
;
321 int orig_hidden_flags
, orig_secondary_flags
;
322 int new_hidden_flags
, new_secondary_flags
;
323 Error
*local_err
= NULL
;
326 orig_hidden_flags
= s
->orig_hidden_flags
=
327 bdrv_get_flags(s
->hidden_disk
->bs
);
328 new_hidden_flags
= (orig_hidden_flags
| BDRV_O_RDWR
) &
330 orig_secondary_flags
= s
->orig_secondary_flags
=
331 bdrv_get_flags(s
->secondary_disk
->bs
);
332 new_secondary_flags
= (orig_secondary_flags
| BDRV_O_RDWR
) &
335 orig_hidden_flags
= (s
->orig_hidden_flags
| BDRV_O_RDWR
) &
337 new_hidden_flags
= s
->orig_hidden_flags
;
338 orig_secondary_flags
= (s
->orig_secondary_flags
| BDRV_O_RDWR
) &
340 new_secondary_flags
= s
->orig_secondary_flags
;
343 if (orig_hidden_flags
!= new_hidden_flags
) {
344 reopen_queue
= bdrv_reopen_queue(reopen_queue
, s
->hidden_disk
->bs
, NULL
,
348 if (!(orig_secondary_flags
& BDRV_O_RDWR
)) {
349 reopen_queue
= bdrv_reopen_queue(reopen_queue
, s
->secondary_disk
->bs
,
350 NULL
, new_secondary_flags
);
354 bdrv_reopen_multiple(reopen_queue
, &local_err
);
355 error_propagate(errp
, local_err
);
359 static void backup_job_cleanup(BDRVReplicationState
*s
)
361 BlockDriverState
*top_bs
;
363 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
367 bdrv_op_unblock_all(top_bs
, s
->blocker
);
368 error_free(s
->blocker
);
369 reopen_backing_file(s
, false, NULL
);
372 static void backup_job_completed(void *opaque
, int ret
)
374 BDRVReplicationState
*s
= opaque
;
376 if (s
->replication_state
!= BLOCK_REPLICATION_FAILOVER
) {
377 /* The backup job is cancelled unexpectedly */
381 backup_job_cleanup(s
);
384 static bool check_top_bs(BlockDriverState
*top_bs
, BlockDriverState
*bs
)
388 /* The bs itself is the top_bs */
393 /* Iterate over top_bs's children */
394 QLIST_FOREACH(child
, &top_bs
->children
, next
) {
395 if (child
->bs
== bs
|| check_top_bs(child
->bs
, bs
)) {
403 static void replication_start(ReplicationState
*rs
, ReplicationMode mode
,
406 BlockDriverState
*bs
= rs
->opaque
;
407 BDRVReplicationState
*s
;
408 BlockDriverState
*top_bs
;
409 int64_t active_length
, hidden_length
, disk_length
;
410 AioContext
*aio_context
;
411 Error
*local_err
= NULL
;
413 aio_context
= bdrv_get_aio_context(bs
);
414 aio_context_acquire(aio_context
);
417 if (s
->replication_state
!= BLOCK_REPLICATION_NONE
) {
418 error_setg(errp
, "Block replication is running or done");
419 aio_context_release(aio_context
);
423 if (s
->mode
!= mode
) {
424 error_setg(errp
, "The parameter mode's value is invalid, needs %d,"
425 " but got %d", s
->mode
, mode
);
426 aio_context_release(aio_context
);
431 case REPLICATION_MODE_PRIMARY
:
433 case REPLICATION_MODE_SECONDARY
:
434 s
->active_disk
= bs
->file
;
435 if (!s
->active_disk
|| !s
->active_disk
->bs
||
436 !s
->active_disk
->bs
->backing
) {
437 error_setg(errp
, "Active disk doesn't have backing file");
438 aio_context_release(aio_context
);
442 s
->hidden_disk
= s
->active_disk
->bs
->backing
;
443 if (!s
->hidden_disk
->bs
|| !s
->hidden_disk
->bs
->backing
) {
444 error_setg(errp
, "Hidden disk doesn't have backing file");
445 aio_context_release(aio_context
);
449 s
->secondary_disk
= s
->hidden_disk
->bs
->backing
;
450 if (!s
->secondary_disk
->bs
|| !bdrv_has_blk(s
->secondary_disk
->bs
)) {
451 error_setg(errp
, "The secondary disk doesn't have block backend");
452 aio_context_release(aio_context
);
456 /* verify the length */
457 active_length
= bdrv_getlength(s
->active_disk
->bs
);
458 hidden_length
= bdrv_getlength(s
->hidden_disk
->bs
);
459 disk_length
= bdrv_getlength(s
->secondary_disk
->bs
);
460 if (active_length
< 0 || hidden_length
< 0 || disk_length
< 0 ||
461 active_length
!= hidden_length
|| hidden_length
!= disk_length
) {
462 error_setg(errp
, "Active disk, hidden disk, secondary disk's length"
463 " are not the same");
464 aio_context_release(aio_context
);
468 if (!s
->active_disk
->bs
->drv
->bdrv_make_empty
||
469 !s
->hidden_disk
->bs
->drv
->bdrv_make_empty
) {
471 "Active disk or hidden disk doesn't support make_empty");
472 aio_context_release(aio_context
);
476 /* reopen the backing file in r/w mode */
477 reopen_backing_file(s
, true, &local_err
);
479 error_propagate(errp
, local_err
);
480 aio_context_release(aio_context
);
484 /* start backup job now */
485 error_setg(&s
->blocker
,
486 "Block device is in use by internal backup job");
488 top_bs
= bdrv_lookup_bs(s
->top_id
, s
->top_id
, NULL
);
489 if (!top_bs
|| !bdrv_is_root_node(top_bs
) ||
490 !check_top_bs(top_bs
, bs
)) {
491 error_setg(errp
, "No top_bs or it is invalid");
492 reopen_backing_file(s
, false, NULL
);
493 aio_context_release(aio_context
);
496 bdrv_op_block_all(top_bs
, s
->blocker
);
497 bdrv_op_unblock(top_bs
, BLOCK_OP_TYPE_DATAPLANE
, s
->blocker
);
499 backup_start("replication-backup", s
->secondary_disk
->bs
,
500 s
->hidden_disk
->bs
, 0, MIRROR_SYNC_MODE_NONE
, NULL
, false,
501 BLOCKDEV_ON_ERROR_REPORT
, BLOCKDEV_ON_ERROR_REPORT
,
502 backup_job_completed
, s
, NULL
, &local_err
);
504 error_propagate(errp
, local_err
);
505 backup_job_cleanup(s
);
506 aio_context_release(aio_context
);
511 aio_context_release(aio_context
);
515 s
->replication_state
= BLOCK_REPLICATION_RUNNING
;
517 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
518 secondary_do_checkpoint(s
, errp
);
522 aio_context_release(aio_context
);
525 static void replication_do_checkpoint(ReplicationState
*rs
, Error
**errp
)
527 BlockDriverState
*bs
= rs
->opaque
;
528 BDRVReplicationState
*s
;
529 AioContext
*aio_context
;
531 aio_context
= bdrv_get_aio_context(bs
);
532 aio_context_acquire(aio_context
);
535 if (s
->mode
== REPLICATION_MODE_SECONDARY
) {
536 secondary_do_checkpoint(s
, errp
);
538 aio_context_release(aio_context
);
541 static void replication_get_error(ReplicationState
*rs
, Error
**errp
)
543 BlockDriverState
*bs
= rs
->opaque
;
544 BDRVReplicationState
*s
;
545 AioContext
*aio_context
;
547 aio_context
= bdrv_get_aio_context(bs
);
548 aio_context_acquire(aio_context
);
551 if (s
->replication_state
!= BLOCK_REPLICATION_RUNNING
) {
552 error_setg(errp
, "Block replication is not running");
553 aio_context_release(aio_context
);
558 error_setg(errp
, "I/O error occurred");
559 aio_context_release(aio_context
);
562 aio_context_release(aio_context
);
565 static void replication_done(void *opaque
, int ret
)
567 BlockDriverState
*bs
= opaque
;
568 BDRVReplicationState
*s
= bs
->opaque
;
571 s
->replication_state
= BLOCK_REPLICATION_DONE
;
573 /* refresh top bs's filename */
574 bdrv_refresh_filename(bs
);
575 s
->active_disk
= NULL
;
576 s
->secondary_disk
= NULL
;
577 s
->hidden_disk
= NULL
;
580 s
->replication_state
= BLOCK_REPLICATION_FAILOVER_FAILED
;
585 static void replication_stop(ReplicationState
*rs
, bool failover
, Error
**errp
)
587 BlockDriverState
*bs
= rs
->opaque
;
588 BDRVReplicationState
*s
;
589 AioContext
*aio_context
;
591 aio_context
= bdrv_get_aio_context(bs
);
592 aio_context_acquire(aio_context
);
595 if (s
->replication_state
!= BLOCK_REPLICATION_RUNNING
) {
596 error_setg(errp
, "Block replication is not running");
597 aio_context_release(aio_context
);
602 case REPLICATION_MODE_PRIMARY
:
603 s
->replication_state
= BLOCK_REPLICATION_DONE
;
606 case REPLICATION_MODE_SECONDARY
:
608 * This BDS will be closed, and the job should be completed
609 * before the BDS is closed, because we will access hidden
610 * disk, secondary disk in backup_job_completed().
612 if (s
->secondary_disk
->bs
->job
) {
613 block_job_cancel_sync(s
->secondary_disk
->bs
->job
);
617 secondary_do_checkpoint(s
, errp
);
618 s
->replication_state
= BLOCK_REPLICATION_DONE
;
619 aio_context_release(aio_context
);
623 s
->replication_state
= BLOCK_REPLICATION_FAILOVER
;
624 commit_active_start("replication-commit", s
->active_disk
->bs
,
625 s
->secondary_disk
->bs
, 0, BLOCKDEV_ON_ERROR_REPORT
,
630 aio_context_release(aio_context
);
633 aio_context_release(aio_context
);
636 BlockDriver bdrv_replication
= {
637 .format_name
= "replication",
638 .protocol_name
= "replication",
639 .instance_size
= sizeof(BDRVReplicationState
),
641 .bdrv_open
= replication_open
,
642 .bdrv_close
= replication_close
,
644 .bdrv_getlength
= replication_getlength
,
645 .bdrv_co_readv
= replication_co_readv
,
646 .bdrv_co_writev
= replication_co_writev
,
649 .bdrv_recurse_is_first_non_filter
= replication_recurse_is_first_non_filter
,
651 .has_variable_length
= true,
654 static void bdrv_replication_init(void)
656 bdrv_register(&bdrv_replication
);
659 block_init(bdrv_replication_init
);