Move include qemu/option.h from qemu-common.h to actual users
[qemu/armbru.git] / block / replication.c
blobf98ef094b9da59d5c578696ee693a4a45fca2cc3
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/option.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 enum {
26 BLOCK_REPLICATION_NONE, /* block replication is not started */
27 BLOCK_REPLICATION_RUNNING, /* block replication is running */
28 BLOCK_REPLICATION_FAILOVER, /* failover is running in background */
29 BLOCK_REPLICATION_FAILOVER_FAILED, /* failover failed */
30 BLOCK_REPLICATION_DONE, /* block replication is done */
31 } ReplicationStage;
33 typedef struct BDRVReplicationState {
34 ReplicationMode mode;
35 ReplicationStage stage;
36 BdrvChild *active_disk;
37 BdrvChild *hidden_disk;
38 BdrvChild *secondary_disk;
39 char *top_id;
40 ReplicationState *rs;
41 Error *blocker;
42 int orig_hidden_flags;
43 int orig_secondary_flags;
44 int error;
45 } BDRVReplicationState;
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 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
90 false, errp);
91 if (!bs->file) {
92 return -EINVAL;
95 ret = -EINVAL;
96 opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
97 qemu_opts_absorb_qdict(opts, options, &local_err);
98 if (local_err) {
99 goto fail;
102 mode = qemu_opt_get(opts, REPLICATION_MODE);
103 if (!mode) {
104 error_setg(&local_err, "Missing the option mode");
105 goto fail;
108 if (!strcmp(mode, "primary")) {
109 s->mode = REPLICATION_MODE_PRIMARY;
110 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
111 if (top_id) {
112 error_setg(&local_err, "The primary side does not support option top-id");
113 goto fail;
115 } else if (!strcmp(mode, "secondary")) {
116 s->mode = REPLICATION_MODE_SECONDARY;
117 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
118 s->top_id = g_strdup(top_id);
119 if (!s->top_id) {
120 error_setg(&local_err, "Missing the option top-id");
121 goto fail;
123 } else {
124 error_setg(&local_err,
125 "The option mode's value should be primary or secondary");
126 goto fail;
129 s->rs = replication_new(bs, &replication_ops);
131 ret = 0;
133 fail:
134 qemu_opts_del(opts);
135 error_propagate(errp, local_err);
137 return ret;
140 static void replication_close(BlockDriverState *bs)
142 BDRVReplicationState *s = bs->opaque;
144 if (s->stage == BLOCK_REPLICATION_RUNNING) {
145 replication_stop(s->rs, false, NULL);
147 if (s->stage == BLOCK_REPLICATION_FAILOVER) {
148 block_job_cancel_sync(s->active_disk->bs->job);
151 if (s->mode == REPLICATION_MODE_SECONDARY) {
152 g_free(s->top_id);
155 replication_remove(s->rs);
158 static void replication_child_perm(BlockDriverState *bs, BdrvChild *c,
159 const BdrvChildRole *role,
160 BlockReopenQueue *reopen_queue,
161 uint64_t perm, uint64_t shared,
162 uint64_t *nperm, uint64_t *nshared)
164 *nperm = BLK_PERM_CONSISTENT_READ;
165 if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
166 *nperm |= BLK_PERM_WRITE;
168 *nshared = BLK_PERM_CONSISTENT_READ \
169 | BLK_PERM_WRITE \
170 | BLK_PERM_WRITE_UNCHANGED;
171 return;
174 static int64_t replication_getlength(BlockDriverState *bs)
176 return bdrv_getlength(bs->file->bs);
179 static int replication_get_io_status(BDRVReplicationState *s)
181 switch (s->stage) {
182 case BLOCK_REPLICATION_NONE:
183 return -EIO;
184 case BLOCK_REPLICATION_RUNNING:
185 return 0;
186 case BLOCK_REPLICATION_FAILOVER:
187 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
188 case BLOCK_REPLICATION_FAILOVER_FAILED:
189 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
190 case BLOCK_REPLICATION_DONE:
192 * active commit job completes, and active disk and secondary_disk
193 * is swapped, so we can operate bs->file directly
195 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
196 default:
197 abort();
201 static int replication_return_value(BDRVReplicationState *s, int ret)
203 if (s->mode == REPLICATION_MODE_SECONDARY) {
204 return ret;
207 if (ret < 0) {
208 s->error = ret;
209 ret = 0;
212 return ret;
215 static coroutine_fn int replication_co_readv(BlockDriverState *bs,
216 int64_t sector_num,
217 int remaining_sectors,
218 QEMUIOVector *qiov)
220 BDRVReplicationState *s = bs->opaque;
221 BdrvChild *child = s->secondary_disk;
222 BlockJob *job = NULL;
223 CowRequest req;
224 int ret;
226 if (s->mode == REPLICATION_MODE_PRIMARY) {
227 /* We only use it to forward primary write requests */
228 return -EIO;
231 ret = replication_get_io_status(s);
232 if (ret < 0) {
233 return ret;
236 if (child && child->bs) {
237 job = child->bs->job;
240 if (job) {
241 uint64_t remaining_bytes = remaining_sectors * BDRV_SECTOR_SIZE;
243 backup_wait_for_overlapping_requests(child->bs->job,
244 sector_num * BDRV_SECTOR_SIZE,
245 remaining_bytes);
246 backup_cow_request_begin(&req, child->bs->job,
247 sector_num * BDRV_SECTOR_SIZE,
248 remaining_bytes);
249 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors,
250 qiov);
251 backup_cow_request_end(&req);
252 goto out;
255 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, qiov);
256 out:
257 return replication_return_value(s, ret);
260 static coroutine_fn int replication_co_writev(BlockDriverState *bs,
261 int64_t sector_num,
262 int remaining_sectors,
263 QEMUIOVector *qiov)
265 BDRVReplicationState *s = bs->opaque;
266 QEMUIOVector hd_qiov;
267 uint64_t bytes_done = 0;
268 BdrvChild *top = bs->file;
269 BdrvChild *base = s->secondary_disk;
270 BdrvChild *target;
271 int ret;
272 int64_t n;
274 ret = replication_get_io_status(s);
275 if (ret < 0) {
276 goto out;
279 if (ret == 0) {
280 ret = bdrv_co_writev(top, sector_num,
281 remaining_sectors, qiov);
282 return replication_return_value(s, ret);
286 * Failover failed, only write to active disk if the sectors
287 * have already been allocated in active disk/hidden disk.
289 qemu_iovec_init(&hd_qiov, qiov->niov);
290 while (remaining_sectors > 0) {
291 int64_t count;
293 ret = bdrv_is_allocated_above(top->bs, base->bs,
294 sector_num * BDRV_SECTOR_SIZE,
295 remaining_sectors * BDRV_SECTOR_SIZE,
296 &count);
297 if (ret < 0) {
298 goto out1;
301 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
302 n = count >> BDRV_SECTOR_BITS;
303 qemu_iovec_reset(&hd_qiov);
304 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count);
306 target = ret ? top : base;
307 ret = bdrv_co_writev(target, sector_num, n, &hd_qiov);
308 if (ret < 0) {
309 goto out1;
312 remaining_sectors -= n;
313 sector_num += n;
314 bytes_done += count;
317 out1:
318 qemu_iovec_destroy(&hd_qiov);
319 out:
320 return ret;
323 static bool replication_recurse_is_first_non_filter(BlockDriverState *bs,
324 BlockDriverState *candidate)
326 return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
329 static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp)
331 Error *local_err = NULL;
332 int ret;
334 if (!s->secondary_disk->bs->job) {
335 error_setg(errp, "Backup job was cancelled unexpectedly");
336 return;
339 backup_do_checkpoint(s->secondary_disk->bs->job, &local_err);
340 if (local_err) {
341 error_propagate(errp, local_err);
342 return;
345 if (!s->active_disk->bs->drv) {
346 error_setg(errp, "Active disk %s is ejected",
347 s->active_disk->bs->node_name);
348 return;
351 ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs);
352 if (ret < 0) {
353 error_setg(errp, "Cannot make active disk empty");
354 return;
357 if (!s->hidden_disk->bs->drv) {
358 error_setg(errp, "Hidden disk %s is ejected",
359 s->hidden_disk->bs->node_name);
360 return;
363 ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs);
364 if (ret < 0) {
365 error_setg(errp, "Cannot make hidden disk empty");
366 return;
370 static void reopen_backing_file(BlockDriverState *bs, bool writable,
371 Error **errp)
373 BDRVReplicationState *s = bs->opaque;
374 BlockReopenQueue *reopen_queue = NULL;
375 int orig_hidden_flags, orig_secondary_flags;
376 int new_hidden_flags, new_secondary_flags;
377 Error *local_err = NULL;
379 if (writable) {
380 orig_hidden_flags = s->orig_hidden_flags =
381 bdrv_get_flags(s->hidden_disk->bs);
382 new_hidden_flags = (orig_hidden_flags | BDRV_O_RDWR) &
383 ~BDRV_O_INACTIVE;
384 orig_secondary_flags = s->orig_secondary_flags =
385 bdrv_get_flags(s->secondary_disk->bs);
386 new_secondary_flags = (orig_secondary_flags | BDRV_O_RDWR) &
387 ~BDRV_O_INACTIVE;
388 } else {
389 orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) &
390 ~BDRV_O_INACTIVE;
391 new_hidden_flags = s->orig_hidden_flags;
392 orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) &
393 ~BDRV_O_INACTIVE;
394 new_secondary_flags = s->orig_secondary_flags;
397 bdrv_subtree_drained_begin(s->hidden_disk->bs);
398 bdrv_subtree_drained_begin(s->secondary_disk->bs);
400 if (orig_hidden_flags != new_hidden_flags) {
401 reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL,
402 new_hidden_flags);
405 if (!(orig_secondary_flags & BDRV_O_RDWR)) {
406 reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs,
407 NULL, new_secondary_flags);
410 if (reopen_queue) {
411 bdrv_reopen_multiple(bdrv_get_aio_context(bs),
412 reopen_queue, &local_err);
413 error_propagate(errp, local_err);
416 bdrv_subtree_drained_end(s->hidden_disk->bs);
417 bdrv_subtree_drained_end(s->secondary_disk->bs);
420 static void backup_job_cleanup(BlockDriverState *bs)
422 BDRVReplicationState *s = bs->opaque;
423 BlockDriverState *top_bs;
425 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
426 if (!top_bs) {
427 return;
429 bdrv_op_unblock_all(top_bs, s->blocker);
430 error_free(s->blocker);
431 reopen_backing_file(bs, false, NULL);
434 static void backup_job_completed(void *opaque, int ret)
436 BlockDriverState *bs = opaque;
437 BDRVReplicationState *s = bs->opaque;
439 if (s->stage != BLOCK_REPLICATION_FAILOVER) {
440 /* The backup job is cancelled unexpectedly */
441 s->error = -EIO;
444 backup_job_cleanup(bs);
447 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
449 BdrvChild *child;
451 /* The bs itself is the top_bs */
452 if (top_bs == bs) {
453 return true;
456 /* Iterate over top_bs's children */
457 QLIST_FOREACH(child, &top_bs->children, next) {
458 if (child->bs == bs || check_top_bs(child->bs, bs)) {
459 return true;
463 return false;
466 static void replication_start(ReplicationState *rs, ReplicationMode mode,
467 Error **errp)
469 BlockDriverState *bs = rs->opaque;
470 BDRVReplicationState *s;
471 BlockDriverState *top_bs;
472 int64_t active_length, hidden_length, disk_length;
473 AioContext *aio_context;
474 Error *local_err = NULL;
475 BlockJob *job;
477 aio_context = bdrv_get_aio_context(bs);
478 aio_context_acquire(aio_context);
479 s = bs->opaque;
481 if (s->stage != BLOCK_REPLICATION_NONE) {
482 error_setg(errp, "Block replication is running or done");
483 aio_context_release(aio_context);
484 return;
487 if (s->mode != mode) {
488 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
489 " but got %d", s->mode, mode);
490 aio_context_release(aio_context);
491 return;
494 switch (s->mode) {
495 case REPLICATION_MODE_PRIMARY:
496 break;
497 case REPLICATION_MODE_SECONDARY:
498 s->active_disk = bs->file;
499 if (!s->active_disk || !s->active_disk->bs ||
500 !s->active_disk->bs->backing) {
501 error_setg(errp, "Active disk doesn't have backing file");
502 aio_context_release(aio_context);
503 return;
506 s->hidden_disk = s->active_disk->bs->backing;
507 if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) {
508 error_setg(errp, "Hidden disk doesn't have backing file");
509 aio_context_release(aio_context);
510 return;
513 s->secondary_disk = s->hidden_disk->bs->backing;
514 if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) {
515 error_setg(errp, "The secondary disk doesn't have block backend");
516 aio_context_release(aio_context);
517 return;
520 /* verify the length */
521 active_length = bdrv_getlength(s->active_disk->bs);
522 hidden_length = bdrv_getlength(s->hidden_disk->bs);
523 disk_length = bdrv_getlength(s->secondary_disk->bs);
524 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
525 active_length != hidden_length || hidden_length != disk_length) {
526 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
527 " are not the same");
528 aio_context_release(aio_context);
529 return;
532 /* Must be true, or the bdrv_getlength() calls would have failed */
533 assert(s->active_disk->bs->drv && s->hidden_disk->bs->drv);
535 if (!s->active_disk->bs->drv->bdrv_make_empty ||
536 !s->hidden_disk->bs->drv->bdrv_make_empty) {
537 error_setg(errp,
538 "Active disk or hidden disk doesn't support make_empty");
539 aio_context_release(aio_context);
540 return;
543 /* reopen the backing file in r/w mode */
544 reopen_backing_file(bs, true, &local_err);
545 if (local_err) {
546 error_propagate(errp, local_err);
547 aio_context_release(aio_context);
548 return;
551 /* start backup job now */
552 error_setg(&s->blocker,
553 "Block device is in use by internal backup job");
555 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
556 if (!top_bs || !bdrv_is_root_node(top_bs) ||
557 !check_top_bs(top_bs, bs)) {
558 error_setg(errp, "No top_bs or it is invalid");
559 reopen_backing_file(bs, false, NULL);
560 aio_context_release(aio_context);
561 return;
563 bdrv_op_block_all(top_bs, s->blocker);
564 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
566 job = backup_job_create(NULL, s->secondary_disk->bs, s->hidden_disk->bs,
567 0, MIRROR_SYNC_MODE_NONE, NULL, false,
568 BLOCKDEV_ON_ERROR_REPORT,
569 BLOCKDEV_ON_ERROR_REPORT, BLOCK_JOB_INTERNAL,
570 backup_job_completed, bs, NULL, &local_err);
571 if (local_err) {
572 error_propagate(errp, local_err);
573 backup_job_cleanup(bs);
574 aio_context_release(aio_context);
575 return;
577 block_job_start(job);
578 break;
579 default:
580 aio_context_release(aio_context);
581 abort();
584 s->stage = BLOCK_REPLICATION_RUNNING;
586 if (s->mode == REPLICATION_MODE_SECONDARY) {
587 secondary_do_checkpoint(s, errp);
590 s->error = 0;
591 aio_context_release(aio_context);
594 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
596 BlockDriverState *bs = rs->opaque;
597 BDRVReplicationState *s;
598 AioContext *aio_context;
600 aio_context = bdrv_get_aio_context(bs);
601 aio_context_acquire(aio_context);
602 s = bs->opaque;
604 if (s->mode == REPLICATION_MODE_SECONDARY) {
605 secondary_do_checkpoint(s, errp);
607 aio_context_release(aio_context);
610 static void replication_get_error(ReplicationState *rs, Error **errp)
612 BlockDriverState *bs = rs->opaque;
613 BDRVReplicationState *s;
614 AioContext *aio_context;
616 aio_context = bdrv_get_aio_context(bs);
617 aio_context_acquire(aio_context);
618 s = bs->opaque;
620 if (s->stage != BLOCK_REPLICATION_RUNNING) {
621 error_setg(errp, "Block replication is not running");
622 aio_context_release(aio_context);
623 return;
626 if (s->error) {
627 error_setg(errp, "I/O error occurred");
628 aio_context_release(aio_context);
629 return;
631 aio_context_release(aio_context);
634 static void replication_done(void *opaque, int ret)
636 BlockDriverState *bs = opaque;
637 BDRVReplicationState *s = bs->opaque;
639 if (ret == 0) {
640 s->stage = BLOCK_REPLICATION_DONE;
642 /* refresh top bs's filename */
643 bdrv_refresh_filename(bs);
644 s->active_disk = NULL;
645 s->secondary_disk = NULL;
646 s->hidden_disk = NULL;
647 s->error = 0;
648 } else {
649 s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
650 s->error = -EIO;
654 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
656 BlockDriverState *bs = rs->opaque;
657 BDRVReplicationState *s;
658 AioContext *aio_context;
660 aio_context = bdrv_get_aio_context(bs);
661 aio_context_acquire(aio_context);
662 s = bs->opaque;
664 if (s->stage != BLOCK_REPLICATION_RUNNING) {
665 error_setg(errp, "Block replication is not running");
666 aio_context_release(aio_context);
667 return;
670 switch (s->mode) {
671 case REPLICATION_MODE_PRIMARY:
672 s->stage = BLOCK_REPLICATION_DONE;
673 s->error = 0;
674 break;
675 case REPLICATION_MODE_SECONDARY:
677 * This BDS will be closed, and the job should be completed
678 * before the BDS is closed, because we will access hidden
679 * disk, secondary disk in backup_job_completed().
681 if (s->secondary_disk->bs->job) {
682 block_job_cancel_sync(s->secondary_disk->bs->job);
685 if (!failover) {
686 secondary_do_checkpoint(s, errp);
687 s->stage = BLOCK_REPLICATION_DONE;
688 aio_context_release(aio_context);
689 return;
692 s->stage = BLOCK_REPLICATION_FAILOVER;
693 commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs,
694 BLOCK_JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
695 NULL, replication_done, bs, true, errp);
696 break;
697 default:
698 aio_context_release(aio_context);
699 abort();
701 aio_context_release(aio_context);
704 BlockDriver bdrv_replication = {
705 .format_name = "replication",
706 .protocol_name = "replication",
707 .instance_size = sizeof(BDRVReplicationState),
709 .bdrv_open = replication_open,
710 .bdrv_close = replication_close,
711 .bdrv_child_perm = replication_child_perm,
713 .bdrv_getlength = replication_getlength,
714 .bdrv_co_readv = replication_co_readv,
715 .bdrv_co_writev = replication_co_writev,
717 .is_filter = true,
718 .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter,
720 .has_variable_length = true,
723 static void bdrv_replication_init(void)
725 bdrv_register(&bdrv_replication);
728 block_init(bdrv_replication_init);