vnc: use DIV_ROUND_UP
[qemu/ar7.git] / block / replication.c
blobbf4462c8e739f3eca63523e7d9dde31b849f79ee
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 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 uint64_t perm, uint64_t shared,
161 uint64_t *nperm, uint64_t *nshared)
163 *nperm = *nshared = BLK_PERM_CONSISTENT_READ \
164 | BLK_PERM_WRITE \
165 | BLK_PERM_WRITE_UNCHANGED;
167 return;
170 static int64_t replication_getlength(BlockDriverState *bs)
172 return bdrv_getlength(bs->file->bs);
175 static int replication_get_io_status(BDRVReplicationState *s)
177 switch (s->stage) {
178 case BLOCK_REPLICATION_NONE:
179 return -EIO;
180 case BLOCK_REPLICATION_RUNNING:
181 return 0;
182 case BLOCK_REPLICATION_FAILOVER:
183 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
184 case BLOCK_REPLICATION_FAILOVER_FAILED:
185 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
186 case BLOCK_REPLICATION_DONE:
188 * active commit job completes, and active disk and secondary_disk
189 * is swapped, so we can operate bs->file directly
191 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
192 default:
193 abort();
197 static int replication_return_value(BDRVReplicationState *s, int ret)
199 if (s->mode == REPLICATION_MODE_SECONDARY) {
200 return ret;
203 if (ret < 0) {
204 s->error = ret;
205 ret = 0;
208 return ret;
211 static coroutine_fn int replication_co_readv(BlockDriverState *bs,
212 int64_t sector_num,
213 int remaining_sectors,
214 QEMUIOVector *qiov)
216 BDRVReplicationState *s = bs->opaque;
217 BdrvChild *child = s->secondary_disk;
218 BlockJob *job = NULL;
219 CowRequest req;
220 int ret;
222 if (s->mode == REPLICATION_MODE_PRIMARY) {
223 /* We only use it to forward primary write requests */
224 return -EIO;
227 ret = replication_get_io_status(s);
228 if (ret < 0) {
229 return ret;
232 if (child && child->bs) {
233 job = child->bs->job;
236 if (job) {
237 uint64_t remaining_bytes = remaining_sectors * BDRV_SECTOR_SIZE;
239 backup_wait_for_overlapping_requests(child->bs->job,
240 sector_num * BDRV_SECTOR_SIZE,
241 remaining_bytes);
242 backup_cow_request_begin(&req, child->bs->job,
243 sector_num * BDRV_SECTOR_SIZE,
244 remaining_bytes);
245 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors,
246 qiov);
247 backup_cow_request_end(&req);
248 goto out;
251 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, qiov);
252 out:
253 return replication_return_value(s, ret);
256 static coroutine_fn int replication_co_writev(BlockDriverState *bs,
257 int64_t sector_num,
258 int remaining_sectors,
259 QEMUIOVector *qiov)
261 BDRVReplicationState *s = bs->opaque;
262 QEMUIOVector hd_qiov;
263 uint64_t bytes_done = 0;
264 BdrvChild *top = bs->file;
265 BdrvChild *base = s->secondary_disk;
266 BdrvChild *target;
267 int ret;
268 int64_t n;
270 ret = replication_get_io_status(s);
271 if (ret < 0) {
272 goto out;
275 if (ret == 0) {
276 ret = bdrv_co_writev(top, sector_num,
277 remaining_sectors, qiov);
278 return replication_return_value(s, ret);
282 * Failover failed, only write to active disk if the sectors
283 * have already been allocated in active disk/hidden disk.
285 qemu_iovec_init(&hd_qiov, qiov->niov);
286 while (remaining_sectors > 0) {
287 int64_t count;
289 ret = bdrv_is_allocated_above(top->bs, base->bs,
290 sector_num * BDRV_SECTOR_SIZE,
291 remaining_sectors * BDRV_SECTOR_SIZE,
292 &count);
293 if (ret < 0) {
294 goto out1;
297 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
298 n = count >> BDRV_SECTOR_BITS;
299 qemu_iovec_reset(&hd_qiov);
300 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count);
302 target = ret ? top : base;
303 ret = bdrv_co_writev(target, sector_num, n, &hd_qiov);
304 if (ret < 0) {
305 goto out1;
308 remaining_sectors -= n;
309 sector_num += n;
310 bytes_done += count;
313 out1:
314 qemu_iovec_destroy(&hd_qiov);
315 out:
316 return ret;
319 static bool replication_recurse_is_first_non_filter(BlockDriverState *bs,
320 BlockDriverState *candidate)
322 return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
325 static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp)
327 Error *local_err = NULL;
328 int ret;
330 if (!s->secondary_disk->bs->job) {
331 error_setg(errp, "Backup job was cancelled unexpectedly");
332 return;
335 backup_do_checkpoint(s->secondary_disk->bs->job, &local_err);
336 if (local_err) {
337 error_propagate(errp, local_err);
338 return;
341 ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs);
342 if (ret < 0) {
343 error_setg(errp, "Cannot make active disk empty");
344 return;
347 ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs);
348 if (ret < 0) {
349 error_setg(errp, "Cannot make hidden disk empty");
350 return;
354 static void reopen_backing_file(BlockDriverState *bs, bool writable,
355 Error **errp)
357 BDRVReplicationState *s = bs->opaque;
358 BlockReopenQueue *reopen_queue = NULL;
359 int orig_hidden_flags, orig_secondary_flags;
360 int new_hidden_flags, new_secondary_flags;
361 Error *local_err = NULL;
363 if (writable) {
364 orig_hidden_flags = s->orig_hidden_flags =
365 bdrv_get_flags(s->hidden_disk->bs);
366 new_hidden_flags = (orig_hidden_flags | BDRV_O_RDWR) &
367 ~BDRV_O_INACTIVE;
368 orig_secondary_flags = s->orig_secondary_flags =
369 bdrv_get_flags(s->secondary_disk->bs);
370 new_secondary_flags = (orig_secondary_flags | BDRV_O_RDWR) &
371 ~BDRV_O_INACTIVE;
372 } else {
373 orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) &
374 ~BDRV_O_INACTIVE;
375 new_hidden_flags = s->orig_hidden_flags;
376 orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) &
377 ~BDRV_O_INACTIVE;
378 new_secondary_flags = s->orig_secondary_flags;
381 if (orig_hidden_flags != new_hidden_flags) {
382 reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL,
383 new_hidden_flags);
386 if (!(orig_secondary_flags & BDRV_O_RDWR)) {
387 reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs,
388 NULL, new_secondary_flags);
391 if (reopen_queue) {
392 bdrv_reopen_multiple(bdrv_get_aio_context(bs),
393 reopen_queue, &local_err);
394 error_propagate(errp, local_err);
398 static void backup_job_cleanup(BlockDriverState *bs)
400 BDRVReplicationState *s = bs->opaque;
401 BlockDriverState *top_bs;
403 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
404 if (!top_bs) {
405 return;
407 bdrv_op_unblock_all(top_bs, s->blocker);
408 error_free(s->blocker);
409 reopen_backing_file(bs, false, NULL);
412 static void backup_job_completed(void *opaque, int ret)
414 BlockDriverState *bs = opaque;
415 BDRVReplicationState *s = bs->opaque;
417 if (s->stage != BLOCK_REPLICATION_FAILOVER) {
418 /* The backup job is cancelled unexpectedly */
419 s->error = -EIO;
422 backup_job_cleanup(bs);
425 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
427 BdrvChild *child;
429 /* The bs itself is the top_bs */
430 if (top_bs == bs) {
431 return true;
434 /* Iterate over top_bs's children */
435 QLIST_FOREACH(child, &top_bs->children, next) {
436 if (child->bs == bs || check_top_bs(child->bs, bs)) {
437 return true;
441 return false;
444 static void replication_start(ReplicationState *rs, ReplicationMode mode,
445 Error **errp)
447 BlockDriverState *bs = rs->opaque;
448 BDRVReplicationState *s;
449 BlockDriverState *top_bs;
450 int64_t active_length, hidden_length, disk_length;
451 AioContext *aio_context;
452 Error *local_err = NULL;
453 BlockJob *job;
455 aio_context = bdrv_get_aio_context(bs);
456 aio_context_acquire(aio_context);
457 s = bs->opaque;
459 if (s->stage != BLOCK_REPLICATION_NONE) {
460 error_setg(errp, "Block replication is running or done");
461 aio_context_release(aio_context);
462 return;
465 if (s->mode != mode) {
466 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
467 " but got %d", s->mode, mode);
468 aio_context_release(aio_context);
469 return;
472 switch (s->mode) {
473 case REPLICATION_MODE_PRIMARY:
474 break;
475 case REPLICATION_MODE_SECONDARY:
476 s->active_disk = bs->file;
477 if (!s->active_disk || !s->active_disk->bs ||
478 !s->active_disk->bs->backing) {
479 error_setg(errp, "Active disk doesn't have backing file");
480 aio_context_release(aio_context);
481 return;
484 s->hidden_disk = s->active_disk->bs->backing;
485 if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) {
486 error_setg(errp, "Hidden disk doesn't have backing file");
487 aio_context_release(aio_context);
488 return;
491 s->secondary_disk = s->hidden_disk->bs->backing;
492 if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) {
493 error_setg(errp, "The secondary disk doesn't have block backend");
494 aio_context_release(aio_context);
495 return;
498 /* verify the length */
499 active_length = bdrv_getlength(s->active_disk->bs);
500 hidden_length = bdrv_getlength(s->hidden_disk->bs);
501 disk_length = bdrv_getlength(s->secondary_disk->bs);
502 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
503 active_length != hidden_length || hidden_length != disk_length) {
504 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
505 " are not the same");
506 aio_context_release(aio_context);
507 return;
510 if (!s->active_disk->bs->drv->bdrv_make_empty ||
511 !s->hidden_disk->bs->drv->bdrv_make_empty) {
512 error_setg(errp,
513 "Active disk or hidden disk doesn't support make_empty");
514 aio_context_release(aio_context);
515 return;
518 /* reopen the backing file in r/w mode */
519 reopen_backing_file(bs, true, &local_err);
520 if (local_err) {
521 error_propagate(errp, local_err);
522 aio_context_release(aio_context);
523 return;
526 /* start backup job now */
527 error_setg(&s->blocker,
528 "Block device is in use by internal backup job");
530 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
531 if (!top_bs || !bdrv_is_root_node(top_bs) ||
532 !check_top_bs(top_bs, bs)) {
533 error_setg(errp, "No top_bs or it is invalid");
534 reopen_backing_file(bs, false, NULL);
535 aio_context_release(aio_context);
536 return;
538 bdrv_op_block_all(top_bs, s->blocker);
539 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
541 job = backup_job_create(NULL, s->secondary_disk->bs, s->hidden_disk->bs,
542 0, MIRROR_SYNC_MODE_NONE, NULL, false,
543 BLOCKDEV_ON_ERROR_REPORT,
544 BLOCKDEV_ON_ERROR_REPORT, BLOCK_JOB_INTERNAL,
545 backup_job_completed, bs, NULL, &local_err);
546 if (local_err) {
547 error_propagate(errp, local_err);
548 backup_job_cleanup(bs);
549 aio_context_release(aio_context);
550 return;
552 block_job_start(job);
553 break;
554 default:
555 aio_context_release(aio_context);
556 abort();
559 s->stage = BLOCK_REPLICATION_RUNNING;
561 if (s->mode == REPLICATION_MODE_SECONDARY) {
562 secondary_do_checkpoint(s, errp);
565 s->error = 0;
566 aio_context_release(aio_context);
569 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
571 BlockDriverState *bs = rs->opaque;
572 BDRVReplicationState *s;
573 AioContext *aio_context;
575 aio_context = bdrv_get_aio_context(bs);
576 aio_context_acquire(aio_context);
577 s = bs->opaque;
579 if (s->mode == REPLICATION_MODE_SECONDARY) {
580 secondary_do_checkpoint(s, errp);
582 aio_context_release(aio_context);
585 static void replication_get_error(ReplicationState *rs, 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->stage != BLOCK_REPLICATION_RUNNING) {
596 error_setg(errp, "Block replication is not running");
597 aio_context_release(aio_context);
598 return;
601 if (s->error) {
602 error_setg(errp, "I/O error occurred");
603 aio_context_release(aio_context);
604 return;
606 aio_context_release(aio_context);
609 static void replication_done(void *opaque, int ret)
611 BlockDriverState *bs = opaque;
612 BDRVReplicationState *s = bs->opaque;
614 if (ret == 0) {
615 s->stage = BLOCK_REPLICATION_DONE;
617 /* refresh top bs's filename */
618 bdrv_refresh_filename(bs);
619 s->active_disk = NULL;
620 s->secondary_disk = NULL;
621 s->hidden_disk = NULL;
622 s->error = 0;
623 } else {
624 s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
625 s->error = -EIO;
629 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
631 BlockDriverState *bs = rs->opaque;
632 BDRVReplicationState *s;
633 AioContext *aio_context;
635 aio_context = bdrv_get_aio_context(bs);
636 aio_context_acquire(aio_context);
637 s = bs->opaque;
639 if (s->stage != BLOCK_REPLICATION_RUNNING) {
640 error_setg(errp, "Block replication is not running");
641 aio_context_release(aio_context);
642 return;
645 switch (s->mode) {
646 case REPLICATION_MODE_PRIMARY:
647 s->stage = BLOCK_REPLICATION_DONE;
648 s->error = 0;
649 break;
650 case REPLICATION_MODE_SECONDARY:
652 * This BDS will be closed, and the job should be completed
653 * before the BDS is closed, because we will access hidden
654 * disk, secondary disk in backup_job_completed().
656 if (s->secondary_disk->bs->job) {
657 block_job_cancel_sync(s->secondary_disk->bs->job);
660 if (!failover) {
661 secondary_do_checkpoint(s, errp);
662 s->stage = BLOCK_REPLICATION_DONE;
663 aio_context_release(aio_context);
664 return;
667 s->stage = BLOCK_REPLICATION_FAILOVER;
668 commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs,
669 BLOCK_JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
670 NULL, replication_done, bs, true, errp);
671 break;
672 default:
673 aio_context_release(aio_context);
674 abort();
676 aio_context_release(aio_context);
679 BlockDriver bdrv_replication = {
680 .format_name = "replication",
681 .protocol_name = "replication",
682 .instance_size = sizeof(BDRVReplicationState),
684 .bdrv_open = replication_open,
685 .bdrv_close = replication_close,
686 .bdrv_child_perm = replication_child_perm,
688 .bdrv_getlength = replication_getlength,
689 .bdrv_co_readv = replication_co_readv,
690 .bdrv_co_writev = replication_co_writev,
692 .is_filter = true,
693 .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter,
695 .has_variable_length = true,
698 static void bdrv_replication_init(void)
700 bdrv_register(&bdrv_replication);
703 block_init(bdrv_replication_init);