MAINTAINERS: Add include/hw/tricore/ to TriCore section
[qemu/kevin.git] / block / replication.c
blob3bd1cf1809d2d6f97bf7ba011f972c2620da0e4f
1 /*
2 * Replication Block filter
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 * Copyright (c) 2016 Intel Corporation
6 * Copyright (c) 2016 FUJITSU LIMITED
8 * Author:
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 {
26 ReplicationMode mode;
27 int replication_state;
28 BdrvChild *active_disk;
29 BdrvChild *hidden_disk;
30 BdrvChild *secondary_disk;
31 char *top_id;
32 ReplicationState *rs;
33 Error *blocker;
34 int orig_hidden_flags;
35 int orig_secondary_flags;
36 int error;
37 } BDRVReplicationState;
39 enum {
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,
48 Error **errp);
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,
52 Error **errp);
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),
59 .desc = {
61 .name = REPLICATION_MODE,
62 .type = QEMU_OPT_STRING,
65 .name = REPLICATION_TOP_ID,
66 .type = QEMU_OPT_STRING,
68 { /* end of list */ }
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)
82 int ret;
83 BDRVReplicationState *s = bs->opaque;
84 Error *local_err = NULL;
85 QemuOpts *opts = NULL;
86 const char *mode;
87 const char *top_id;
89 ret = -EINVAL;
90 opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
91 qemu_opts_absorb_qdict(opts, options, &local_err);
92 if (local_err) {
93 goto fail;
96 mode = qemu_opt_get(opts, REPLICATION_MODE);
97 if (!mode) {
98 error_setg(&local_err, "Missing the option mode");
99 goto fail;
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);
108 if (!s->top_id) {
109 error_setg(&local_err, "Missing the option top-id");
110 goto fail;
112 } else {
113 error_setg(&local_err,
114 "The option mode's value should be primary or secondary");
115 goto fail;
118 s->rs = replication_new(bs, &replication_ops);
120 ret = 0;
122 fail:
123 qemu_opts_del(opts);
124 error_propagate(errp, local_err);
126 return ret;
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) {
138 g_free(s->top_id);
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:
153 return -EIO;
154 case BLOCK_REPLICATION_RUNNING:
155 return 0;
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;
166 default:
167 abort();
171 static int replication_return_value(BDRVReplicationState *s, int ret)
173 if (s->mode == REPLICATION_MODE_SECONDARY) {
174 return ret;
177 if (ret < 0) {
178 s->error = ret;
179 ret = 0;
182 return ret;
185 static coroutine_fn int replication_co_readv(BlockDriverState *bs,
186 int64_t sector_num,
187 int remaining_sectors,
188 QEMUIOVector *qiov)
190 BDRVReplicationState *s = bs->opaque;
191 BdrvChild *child = s->secondary_disk;
192 BlockJob *job = NULL;
193 CowRequest req;
194 int ret;
196 if (s->mode == REPLICATION_MODE_PRIMARY) {
197 /* We only use it to forward primary write requests */
198 return -EIO;
201 ret = replication_get_io_status(s);
202 if (ret < 0) {
203 return ret;
206 if (child && child->bs) {
207 job = child->bs->job;
210 if (job) {
211 backup_wait_for_overlapping_requests(child->bs->job, sector_num,
212 remaining_sectors);
213 backup_cow_request_begin(&req, child->bs->job, sector_num,
214 remaining_sectors);
215 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors,
216 qiov);
217 backup_cow_request_end(&req);
218 goto out;
221 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, qiov);
222 out:
223 return replication_return_value(s, ret);
226 static coroutine_fn int replication_co_writev(BlockDriverState *bs,
227 int64_t sector_num,
228 int remaining_sectors,
229 QEMUIOVector *qiov)
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;
236 BdrvChild *target;
237 int ret, n;
239 ret = replication_get_io_status(s);
240 if (ret < 0) {
241 goto out;
244 if (ret == 0) {
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);
258 if (ret < 0) {
259 goto out1;
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);
267 if (ret < 0) {
268 goto out1;
271 remaining_sectors -= n;
272 sector_num += n;
273 bytes_done += n * BDRV_SECTOR_SIZE;
276 out1:
277 qemu_iovec_destroy(&hd_qiov);
278 out:
279 return ret;
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;
291 int ret;
293 if (!s->secondary_disk->bs->job) {
294 error_setg(errp, "Backup job was cancelled unexpectedly");
295 return;
298 backup_do_checkpoint(s->secondary_disk->bs->job, &local_err);
299 if (local_err) {
300 error_propagate(errp, local_err);
301 return;
304 ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs);
305 if (ret < 0) {
306 error_setg(errp, "Cannot make active disk empty");
307 return;
310 ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs);
311 if (ret < 0) {
312 error_setg(errp, "Cannot make hidden disk empty");
313 return;
317 static void reopen_backing_file(BDRVReplicationState *s, bool writable,
318 Error **errp)
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;
325 if (writable) {
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) &
329 ~BDRV_O_INACTIVE;
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) &
333 ~BDRV_O_INACTIVE;
334 } else {
335 orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) &
336 ~BDRV_O_INACTIVE;
337 new_hidden_flags = s->orig_hidden_flags;
338 orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) &
339 ~BDRV_O_INACTIVE;
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,
345 new_hidden_flags);
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);
353 if (reopen_queue) {
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);
364 if (!top_bs) {
365 return;
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 */
378 s->error = -EIO;
381 backup_job_cleanup(s);
384 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
386 BdrvChild *child;
388 /* The bs itself is the top_bs */
389 if (top_bs == bs) {
390 return true;
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)) {
396 return true;
400 return false;
403 static void replication_start(ReplicationState *rs, ReplicationMode mode,
404 Error **errp)
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);
415 s = bs->opaque;
417 if (s->replication_state != BLOCK_REPLICATION_NONE) {
418 error_setg(errp, "Block replication is running or done");
419 aio_context_release(aio_context);
420 return;
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);
427 return;
430 switch (s->mode) {
431 case REPLICATION_MODE_PRIMARY:
432 break;
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);
439 return;
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);
446 return;
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);
453 return;
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);
465 return;
468 if (!s->active_disk->bs->drv->bdrv_make_empty ||
469 !s->hidden_disk->bs->drv->bdrv_make_empty) {
470 error_setg(errp,
471 "Active disk or hidden disk doesn't support make_empty");
472 aio_context_release(aio_context);
473 return;
476 /* reopen the backing file in r/w mode */
477 reopen_backing_file(s, true, &local_err);
478 if (local_err) {
479 error_propagate(errp, local_err);
480 aio_context_release(aio_context);
481 return;
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);
494 return;
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);
503 if (local_err) {
504 error_propagate(errp, local_err);
505 backup_job_cleanup(s);
506 aio_context_release(aio_context);
507 return;
509 break;
510 default:
511 aio_context_release(aio_context);
512 abort();
515 s->replication_state = BLOCK_REPLICATION_RUNNING;
517 if (s->mode == REPLICATION_MODE_SECONDARY) {
518 secondary_do_checkpoint(s, errp);
521 s->error = 0;
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);
533 s = bs->opaque;
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);
549 s = bs->opaque;
551 if (s->replication_state != BLOCK_REPLICATION_RUNNING) {
552 error_setg(errp, "Block replication is not running");
553 aio_context_release(aio_context);
554 return;
557 if (s->error) {
558 error_setg(errp, "I/O error occurred");
559 aio_context_release(aio_context);
560 return;
562 aio_context_release(aio_context);
565 static void replication_done(void *opaque, int ret)
567 BlockDriverState *bs = opaque;
568 BDRVReplicationState *s = bs->opaque;
570 if (ret == 0) {
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;
578 s->error = 0;
579 } else {
580 s->replication_state = BLOCK_REPLICATION_FAILOVER_FAILED;
581 s->error = -EIO;
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);
593 s = bs->opaque;
595 if (s->replication_state != BLOCK_REPLICATION_RUNNING) {
596 error_setg(errp, "Block replication is not running");
597 aio_context_release(aio_context);
598 return;
601 switch (s->mode) {
602 case REPLICATION_MODE_PRIMARY:
603 s->replication_state = BLOCK_REPLICATION_DONE;
604 s->error = 0;
605 break;
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);
616 if (!failover) {
617 secondary_do_checkpoint(s, errp);
618 s->replication_state = BLOCK_REPLICATION_DONE;
619 aio_context_release(aio_context);
620 return;
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,
626 replication_done,
627 bs, errp, true);
628 break;
629 default:
630 aio_context_release(aio_context);
631 abort();
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,
648 .is_filter = true,
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);