Merge remote-tracking branch 'mdroth/tags/qga-pull-2017-04-25-v2-tag' into staging
[qemu/kevin.git] / block / replication.c
blobd300c15475fcfd9b5cfef4e9557f678c07939722
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 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->replication_state == BLOCK_REPLICATION_RUNNING) {
145 replication_stop(s->rs, false, NULL);
147 if (s->replication_state == 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->replication_state) {
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 backup_wait_for_overlapping_requests(child->bs->job, sector_num,
238 remaining_sectors);
239 backup_cow_request_begin(&req, child->bs->job, sector_num,
240 remaining_sectors);
241 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors,
242 qiov);
243 backup_cow_request_end(&req);
244 goto out;
247 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, qiov);
248 out:
249 return replication_return_value(s, ret);
252 static coroutine_fn int replication_co_writev(BlockDriverState *bs,
253 int64_t sector_num,
254 int remaining_sectors,
255 QEMUIOVector *qiov)
257 BDRVReplicationState *s = bs->opaque;
258 QEMUIOVector hd_qiov;
259 uint64_t bytes_done = 0;
260 BdrvChild *top = bs->file;
261 BdrvChild *base = s->secondary_disk;
262 BdrvChild *target;
263 int ret, n;
265 ret = replication_get_io_status(s);
266 if (ret < 0) {
267 goto out;
270 if (ret == 0) {
271 ret = bdrv_co_writev(top, sector_num,
272 remaining_sectors, qiov);
273 return replication_return_value(s, ret);
277 * Failover failed, only write to active disk if the sectors
278 * have already been allocated in active disk/hidden disk.
280 qemu_iovec_init(&hd_qiov, qiov->niov);
281 while (remaining_sectors > 0) {
282 ret = bdrv_is_allocated_above(top->bs, base->bs, sector_num,
283 remaining_sectors, &n);
284 if (ret < 0) {
285 goto out1;
288 qemu_iovec_reset(&hd_qiov);
289 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, n * BDRV_SECTOR_SIZE);
291 target = ret ? top : base;
292 ret = bdrv_co_writev(target, sector_num, n, &hd_qiov);
293 if (ret < 0) {
294 goto out1;
297 remaining_sectors -= n;
298 sector_num += n;
299 bytes_done += n * BDRV_SECTOR_SIZE;
302 out1:
303 qemu_iovec_destroy(&hd_qiov);
304 out:
305 return ret;
308 static bool replication_recurse_is_first_non_filter(BlockDriverState *bs,
309 BlockDriverState *candidate)
311 return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
314 static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp)
316 Error *local_err = NULL;
317 int ret;
319 if (!s->secondary_disk->bs->job) {
320 error_setg(errp, "Backup job was cancelled unexpectedly");
321 return;
324 backup_do_checkpoint(s->secondary_disk->bs->job, &local_err);
325 if (local_err) {
326 error_propagate(errp, local_err);
327 return;
330 ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs);
331 if (ret < 0) {
332 error_setg(errp, "Cannot make active disk empty");
333 return;
336 ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs);
337 if (ret < 0) {
338 error_setg(errp, "Cannot make hidden disk empty");
339 return;
343 static void reopen_backing_file(BlockDriverState *bs, bool writable,
344 Error **errp)
346 BDRVReplicationState *s = bs->opaque;
347 BlockReopenQueue *reopen_queue = NULL;
348 int orig_hidden_flags, orig_secondary_flags;
349 int new_hidden_flags, new_secondary_flags;
350 Error *local_err = NULL;
352 if (writable) {
353 orig_hidden_flags = s->orig_hidden_flags =
354 bdrv_get_flags(s->hidden_disk->bs);
355 new_hidden_flags = (orig_hidden_flags | BDRV_O_RDWR) &
356 ~BDRV_O_INACTIVE;
357 orig_secondary_flags = s->orig_secondary_flags =
358 bdrv_get_flags(s->secondary_disk->bs);
359 new_secondary_flags = (orig_secondary_flags | BDRV_O_RDWR) &
360 ~BDRV_O_INACTIVE;
361 } else {
362 orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) &
363 ~BDRV_O_INACTIVE;
364 new_hidden_flags = s->orig_hidden_flags;
365 orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) &
366 ~BDRV_O_INACTIVE;
367 new_secondary_flags = s->orig_secondary_flags;
370 if (orig_hidden_flags != new_hidden_flags) {
371 reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL,
372 new_hidden_flags);
375 if (!(orig_secondary_flags & BDRV_O_RDWR)) {
376 reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs,
377 NULL, new_secondary_flags);
380 if (reopen_queue) {
381 bdrv_reopen_multiple(bdrv_get_aio_context(bs),
382 reopen_queue, &local_err);
383 error_propagate(errp, local_err);
387 static void backup_job_cleanup(BlockDriverState *bs)
389 BDRVReplicationState *s = bs->opaque;
390 BlockDriverState *top_bs;
392 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
393 if (!top_bs) {
394 return;
396 bdrv_op_unblock_all(top_bs, s->blocker);
397 error_free(s->blocker);
398 reopen_backing_file(bs, false, NULL);
401 static void backup_job_completed(void *opaque, int ret)
403 BlockDriverState *bs = opaque;
404 BDRVReplicationState *s = bs->opaque;
406 if (s->replication_state != BLOCK_REPLICATION_FAILOVER) {
407 /* The backup job is cancelled unexpectedly */
408 s->error = -EIO;
411 backup_job_cleanup(bs);
414 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
416 BdrvChild *child;
418 /* The bs itself is the top_bs */
419 if (top_bs == bs) {
420 return true;
423 /* Iterate over top_bs's children */
424 QLIST_FOREACH(child, &top_bs->children, next) {
425 if (child->bs == bs || check_top_bs(child->bs, bs)) {
426 return true;
430 return false;
433 static void replication_start(ReplicationState *rs, ReplicationMode mode,
434 Error **errp)
436 BlockDriverState *bs = rs->opaque;
437 BDRVReplicationState *s;
438 BlockDriverState *top_bs;
439 int64_t active_length, hidden_length, disk_length;
440 AioContext *aio_context;
441 Error *local_err = NULL;
442 BlockJob *job;
444 aio_context = bdrv_get_aio_context(bs);
445 aio_context_acquire(aio_context);
446 s = bs->opaque;
448 if (s->replication_state != BLOCK_REPLICATION_NONE) {
449 error_setg(errp, "Block replication is running or done");
450 aio_context_release(aio_context);
451 return;
454 if (s->mode != mode) {
455 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
456 " but got %d", s->mode, mode);
457 aio_context_release(aio_context);
458 return;
461 switch (s->mode) {
462 case REPLICATION_MODE_PRIMARY:
463 break;
464 case REPLICATION_MODE_SECONDARY:
465 s->active_disk = bs->file;
466 if (!s->active_disk || !s->active_disk->bs ||
467 !s->active_disk->bs->backing) {
468 error_setg(errp, "Active disk doesn't have backing file");
469 aio_context_release(aio_context);
470 return;
473 s->hidden_disk = s->active_disk->bs->backing;
474 if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) {
475 error_setg(errp, "Hidden disk doesn't have backing file");
476 aio_context_release(aio_context);
477 return;
480 s->secondary_disk = s->hidden_disk->bs->backing;
481 if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) {
482 error_setg(errp, "The secondary disk doesn't have block backend");
483 aio_context_release(aio_context);
484 return;
487 /* verify the length */
488 active_length = bdrv_getlength(s->active_disk->bs);
489 hidden_length = bdrv_getlength(s->hidden_disk->bs);
490 disk_length = bdrv_getlength(s->secondary_disk->bs);
491 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
492 active_length != hidden_length || hidden_length != disk_length) {
493 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
494 " are not the same");
495 aio_context_release(aio_context);
496 return;
499 if (!s->active_disk->bs->drv->bdrv_make_empty ||
500 !s->hidden_disk->bs->drv->bdrv_make_empty) {
501 error_setg(errp,
502 "Active disk or hidden disk doesn't support make_empty");
503 aio_context_release(aio_context);
504 return;
507 /* reopen the backing file in r/w mode */
508 reopen_backing_file(bs, true, &local_err);
509 if (local_err) {
510 error_propagate(errp, local_err);
511 aio_context_release(aio_context);
512 return;
515 /* start backup job now */
516 error_setg(&s->blocker,
517 "Block device is in use by internal backup job");
519 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
520 if (!top_bs || !bdrv_is_root_node(top_bs) ||
521 !check_top_bs(top_bs, bs)) {
522 error_setg(errp, "No top_bs or it is invalid");
523 reopen_backing_file(bs, false, NULL);
524 aio_context_release(aio_context);
525 return;
527 bdrv_op_block_all(top_bs, s->blocker);
528 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
530 job = backup_job_create(NULL, s->secondary_disk->bs, s->hidden_disk->bs,
531 0, MIRROR_SYNC_MODE_NONE, NULL, false,
532 BLOCKDEV_ON_ERROR_REPORT,
533 BLOCKDEV_ON_ERROR_REPORT, BLOCK_JOB_INTERNAL,
534 backup_job_completed, bs, NULL, &local_err);
535 if (local_err) {
536 error_propagate(errp, local_err);
537 backup_job_cleanup(bs);
538 aio_context_release(aio_context);
539 return;
541 block_job_start(job);
542 break;
543 default:
544 aio_context_release(aio_context);
545 abort();
548 s->replication_state = BLOCK_REPLICATION_RUNNING;
550 if (s->mode == REPLICATION_MODE_SECONDARY) {
551 secondary_do_checkpoint(s, errp);
554 s->error = 0;
555 aio_context_release(aio_context);
558 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
560 BlockDriverState *bs = rs->opaque;
561 BDRVReplicationState *s;
562 AioContext *aio_context;
564 aio_context = bdrv_get_aio_context(bs);
565 aio_context_acquire(aio_context);
566 s = bs->opaque;
568 if (s->mode == REPLICATION_MODE_SECONDARY) {
569 secondary_do_checkpoint(s, errp);
571 aio_context_release(aio_context);
574 static void replication_get_error(ReplicationState *rs, Error **errp)
576 BlockDriverState *bs = rs->opaque;
577 BDRVReplicationState *s;
578 AioContext *aio_context;
580 aio_context = bdrv_get_aio_context(bs);
581 aio_context_acquire(aio_context);
582 s = bs->opaque;
584 if (s->replication_state != BLOCK_REPLICATION_RUNNING) {
585 error_setg(errp, "Block replication is not running");
586 aio_context_release(aio_context);
587 return;
590 if (s->error) {
591 error_setg(errp, "I/O error occurred");
592 aio_context_release(aio_context);
593 return;
595 aio_context_release(aio_context);
598 static void replication_done(void *opaque, int ret)
600 BlockDriverState *bs = opaque;
601 BDRVReplicationState *s = bs->opaque;
603 if (ret == 0) {
604 s->replication_state = BLOCK_REPLICATION_DONE;
606 /* refresh top bs's filename */
607 bdrv_refresh_filename(bs);
608 s->active_disk = NULL;
609 s->secondary_disk = NULL;
610 s->hidden_disk = NULL;
611 s->error = 0;
612 } else {
613 s->replication_state = BLOCK_REPLICATION_FAILOVER_FAILED;
614 s->error = -EIO;
618 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
620 BlockDriverState *bs = rs->opaque;
621 BDRVReplicationState *s;
622 AioContext *aio_context;
624 aio_context = bdrv_get_aio_context(bs);
625 aio_context_acquire(aio_context);
626 s = bs->opaque;
628 if (s->replication_state != BLOCK_REPLICATION_RUNNING) {
629 error_setg(errp, "Block replication is not running");
630 aio_context_release(aio_context);
631 return;
634 switch (s->mode) {
635 case REPLICATION_MODE_PRIMARY:
636 s->replication_state = BLOCK_REPLICATION_DONE;
637 s->error = 0;
638 break;
639 case REPLICATION_MODE_SECONDARY:
641 * This BDS will be closed, and the job should be completed
642 * before the BDS is closed, because we will access hidden
643 * disk, secondary disk in backup_job_completed().
645 if (s->secondary_disk->bs->job) {
646 block_job_cancel_sync(s->secondary_disk->bs->job);
649 if (!failover) {
650 secondary_do_checkpoint(s, errp);
651 s->replication_state = BLOCK_REPLICATION_DONE;
652 aio_context_release(aio_context);
653 return;
656 s->replication_state = BLOCK_REPLICATION_FAILOVER;
657 commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs,
658 BLOCK_JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
659 NULL, replication_done, bs, true, errp);
660 break;
661 default:
662 aio_context_release(aio_context);
663 abort();
665 aio_context_release(aio_context);
668 BlockDriver bdrv_replication = {
669 .format_name = "replication",
670 .protocol_name = "replication",
671 .instance_size = sizeof(BDRVReplicationState),
673 .bdrv_open = replication_open,
674 .bdrv_close = replication_close,
675 .bdrv_child_perm = replication_child_perm,
677 .bdrv_getlength = replication_getlength,
678 .bdrv_co_readv = replication_co_readv,
679 .bdrv_co_writev = replication_co_writev,
681 .is_filter = true,
682 .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter,
684 .has_variable_length = true,
687 static void bdrv_replication_init(void)
689 bdrv_register(&bdrv_replication);
692 block_init(bdrv_replication_init);