block: Clean up reopen_backing_file() in block/replication.c
[qemu/ar7.git] / block / replication.c
blobaf9dba86960602372e720f43417959d42524d8d6
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 "qapi/qmp/qdict.h"
24 #include "replication.h"
26 typedef enum {
27 BLOCK_REPLICATION_NONE, /* block replication is not started */
28 BLOCK_REPLICATION_RUNNING, /* block replication is running */
29 BLOCK_REPLICATION_FAILOVER, /* failover is running in background */
30 BLOCK_REPLICATION_FAILOVER_FAILED, /* failover failed */
31 BLOCK_REPLICATION_DONE, /* block replication is done */
32 } ReplicationStage;
34 typedef struct BDRVReplicationState {
35 ReplicationMode mode;
36 ReplicationStage stage;
37 BdrvChild *active_disk;
38 BdrvChild *hidden_disk;
39 BdrvChild *secondary_disk;
40 char *top_id;
41 ReplicationState *rs;
42 Error *blocker;
43 bool orig_hidden_read_only;
44 bool orig_secondary_read_only;
45 int error;
46 } BDRVReplicationState;
48 static void replication_start(ReplicationState *rs, ReplicationMode mode,
49 Error **errp);
50 static void replication_do_checkpoint(ReplicationState *rs, Error **errp);
51 static void replication_get_error(ReplicationState *rs, Error **errp);
52 static void replication_stop(ReplicationState *rs, bool failover,
53 Error **errp);
55 #define REPLICATION_MODE "mode"
56 #define REPLICATION_TOP_ID "top-id"
57 static QemuOptsList replication_runtime_opts = {
58 .name = "replication",
59 .head = QTAILQ_HEAD_INITIALIZER(replication_runtime_opts.head),
60 .desc = {
62 .name = REPLICATION_MODE,
63 .type = QEMU_OPT_STRING,
66 .name = REPLICATION_TOP_ID,
67 .type = QEMU_OPT_STRING,
69 { /* end of list */ }
73 static ReplicationOps replication_ops = {
74 .start = replication_start,
75 .checkpoint = replication_do_checkpoint,
76 .get_error = replication_get_error,
77 .stop = replication_stop,
80 static int replication_open(BlockDriverState *bs, QDict *options,
81 int flags, Error **errp)
83 int ret;
84 BDRVReplicationState *s = bs->opaque;
85 Error *local_err = NULL;
86 QemuOpts *opts = NULL;
87 const char *mode;
88 const char *top_id;
90 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
91 false, errp);
92 if (!bs->file) {
93 return -EINVAL;
96 ret = -EINVAL;
97 opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
98 qemu_opts_absorb_qdict(opts, options, &local_err);
99 if (local_err) {
100 goto fail;
103 mode = qemu_opt_get(opts, REPLICATION_MODE);
104 if (!mode) {
105 error_setg(&local_err, "Missing the option mode");
106 goto fail;
109 if (!strcmp(mode, "primary")) {
110 s->mode = REPLICATION_MODE_PRIMARY;
111 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
112 if (top_id) {
113 error_setg(&local_err, "The primary side does not support option top-id");
114 goto fail;
116 } else if (!strcmp(mode, "secondary")) {
117 s->mode = REPLICATION_MODE_SECONDARY;
118 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
119 s->top_id = g_strdup(top_id);
120 if (!s->top_id) {
121 error_setg(&local_err, "Missing the option top-id");
122 goto fail;
124 } else {
125 error_setg(&local_err,
126 "The option mode's value should be primary or secondary");
127 goto fail;
130 s->rs = replication_new(bs, &replication_ops);
132 ret = 0;
134 fail:
135 qemu_opts_del(opts);
136 error_propagate(errp, local_err);
138 return ret;
141 static void replication_close(BlockDriverState *bs)
143 BDRVReplicationState *s = bs->opaque;
145 if (s->stage == BLOCK_REPLICATION_RUNNING) {
146 replication_stop(s->rs, false, NULL);
148 if (s->stage == BLOCK_REPLICATION_FAILOVER) {
149 job_cancel_sync(&s->active_disk->bs->job->job);
152 if (s->mode == REPLICATION_MODE_SECONDARY) {
153 g_free(s->top_id);
156 replication_remove(s->rs);
159 static void replication_child_perm(BlockDriverState *bs, BdrvChild *c,
160 const BdrvChildRole *role,
161 BlockReopenQueue *reopen_queue,
162 uint64_t perm, uint64_t shared,
163 uint64_t *nperm, uint64_t *nshared)
165 *nperm = BLK_PERM_CONSISTENT_READ;
166 if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
167 *nperm |= BLK_PERM_WRITE;
169 *nshared = BLK_PERM_CONSISTENT_READ \
170 | BLK_PERM_WRITE \
171 | BLK_PERM_WRITE_UNCHANGED;
172 return;
175 static int64_t replication_getlength(BlockDriverState *bs)
177 return bdrv_getlength(bs->file->bs);
180 static int replication_get_io_status(BDRVReplicationState *s)
182 switch (s->stage) {
183 case BLOCK_REPLICATION_NONE:
184 return -EIO;
185 case BLOCK_REPLICATION_RUNNING:
186 return 0;
187 case BLOCK_REPLICATION_FAILOVER:
188 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
189 case BLOCK_REPLICATION_FAILOVER_FAILED:
190 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
191 case BLOCK_REPLICATION_DONE:
193 * active commit job completes, and active disk and secondary_disk
194 * is swapped, so we can operate bs->file directly
196 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
197 default:
198 abort();
202 static int replication_return_value(BDRVReplicationState *s, int ret)
204 if (s->mode == REPLICATION_MODE_SECONDARY) {
205 return ret;
208 if (ret < 0) {
209 s->error = ret;
210 ret = 0;
213 return ret;
216 static coroutine_fn int replication_co_readv(BlockDriverState *bs,
217 int64_t sector_num,
218 int remaining_sectors,
219 QEMUIOVector *qiov)
221 BDRVReplicationState *s = bs->opaque;
222 int ret;
224 if (s->mode == REPLICATION_MODE_PRIMARY) {
225 /* We only use it to forward primary write requests */
226 return -EIO;
229 ret = replication_get_io_status(s);
230 if (ret < 0) {
231 return ret;
234 ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
235 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
237 return replication_return_value(s, ret);
240 static coroutine_fn int replication_co_writev(BlockDriverState *bs,
241 int64_t sector_num,
242 int remaining_sectors,
243 QEMUIOVector *qiov,
244 int flags)
246 BDRVReplicationState *s = bs->opaque;
247 QEMUIOVector hd_qiov;
248 uint64_t bytes_done = 0;
249 BdrvChild *top = bs->file;
250 BdrvChild *base = s->secondary_disk;
251 BdrvChild *target;
252 int ret;
253 int64_t n;
255 assert(!flags);
256 ret = replication_get_io_status(s);
257 if (ret < 0) {
258 goto out;
261 if (ret == 0) {
262 ret = bdrv_co_pwritev(top, sector_num * BDRV_SECTOR_SIZE,
263 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
264 return replication_return_value(s, ret);
268 * Failover failed, only write to active disk if the sectors
269 * have already been allocated in active disk/hidden disk.
271 qemu_iovec_init(&hd_qiov, qiov->niov);
272 while (remaining_sectors > 0) {
273 int64_t count;
275 ret = bdrv_is_allocated_above(top->bs, base->bs,
276 sector_num * BDRV_SECTOR_SIZE,
277 remaining_sectors * BDRV_SECTOR_SIZE,
278 &count);
279 if (ret < 0) {
280 goto out1;
283 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
284 n = count >> BDRV_SECTOR_BITS;
285 qemu_iovec_reset(&hd_qiov);
286 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count);
288 target = ret ? top : base;
289 ret = bdrv_co_pwritev(target, sector_num * BDRV_SECTOR_SIZE,
290 n * BDRV_SECTOR_SIZE, &hd_qiov, 0);
291 if (ret < 0) {
292 goto out1;
295 remaining_sectors -= n;
296 sector_num += n;
297 bytes_done += count;
300 out1:
301 qemu_iovec_destroy(&hd_qiov);
302 out:
303 return ret;
306 static bool replication_recurse_is_first_non_filter(BlockDriverState *bs,
307 BlockDriverState *candidate)
309 return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
312 static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp)
314 Error *local_err = NULL;
315 int ret;
317 if (!s->secondary_disk->bs->job) {
318 error_setg(errp, "Backup job was cancelled unexpectedly");
319 return;
322 backup_do_checkpoint(s->secondary_disk->bs->job, &local_err);
323 if (local_err) {
324 error_propagate(errp, local_err);
325 return;
328 if (!s->active_disk->bs->drv) {
329 error_setg(errp, "Active disk %s is ejected",
330 s->active_disk->bs->node_name);
331 return;
334 ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs);
335 if (ret < 0) {
336 error_setg(errp, "Cannot make active disk empty");
337 return;
340 if (!s->hidden_disk->bs->drv) {
341 error_setg(errp, "Hidden disk %s is ejected",
342 s->hidden_disk->bs->node_name);
343 return;
346 ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs);
347 if (ret < 0) {
348 error_setg(errp, "Cannot make hidden disk empty");
349 return;
353 /* This function is supposed to be called twice:
354 * first with writable = true, then with writable = false.
355 * The first call puts s->hidden_disk and s->secondary_disk in
356 * r/w mode, and the second puts them back in their original state.
358 static void reopen_backing_file(BlockDriverState *bs, bool writable,
359 Error **errp)
361 BDRVReplicationState *s = bs->opaque;
362 BlockReopenQueue *reopen_queue = NULL;
363 Error *local_err = NULL;
365 if (writable) {
366 s->orig_hidden_read_only = bdrv_is_read_only(s->hidden_disk->bs);
367 s->orig_secondary_read_only = bdrv_is_read_only(s->secondary_disk->bs);
370 bdrv_subtree_drained_begin(s->hidden_disk->bs);
371 bdrv_subtree_drained_begin(s->secondary_disk->bs);
373 if (s->orig_hidden_read_only) {
374 int flags = bdrv_get_flags(s->hidden_disk->bs);
375 QDict *opts = qdict_new();
376 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
377 reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs,
378 opts, flags);
381 if (s->orig_secondary_read_only) {
382 int flags = bdrv_get_flags(s->secondary_disk->bs);
383 QDict *opts = qdict_new();
384 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
385 reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs,
386 opts, flags);
389 if (reopen_queue) {
390 bdrv_reopen_multiple(bdrv_get_aio_context(bs),
391 reopen_queue, &local_err);
392 error_propagate(errp, local_err);
395 bdrv_subtree_drained_end(s->hidden_disk->bs);
396 bdrv_subtree_drained_end(s->secondary_disk->bs);
399 static void backup_job_cleanup(BlockDriverState *bs)
401 BDRVReplicationState *s = bs->opaque;
402 BlockDriverState *top_bs;
404 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
405 if (!top_bs) {
406 return;
408 bdrv_op_unblock_all(top_bs, s->blocker);
409 error_free(s->blocker);
410 reopen_backing_file(bs, false, NULL);
413 static void backup_job_completed(void *opaque, int ret)
415 BlockDriverState *bs = opaque;
416 BDRVReplicationState *s = bs->opaque;
418 if (s->stage != BLOCK_REPLICATION_FAILOVER) {
419 /* The backup job is cancelled unexpectedly */
420 s->error = -EIO;
423 backup_job_cleanup(bs);
426 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
428 BdrvChild *child;
430 /* The bs itself is the top_bs */
431 if (top_bs == bs) {
432 return true;
435 /* Iterate over top_bs's children */
436 QLIST_FOREACH(child, &top_bs->children, next) {
437 if (child->bs == bs || check_top_bs(child->bs, bs)) {
438 return true;
442 return false;
445 static void replication_start(ReplicationState *rs, ReplicationMode mode,
446 Error **errp)
448 BlockDriverState *bs = rs->opaque;
449 BDRVReplicationState *s;
450 BlockDriverState *top_bs;
451 int64_t active_length, hidden_length, disk_length;
452 AioContext *aio_context;
453 Error *local_err = NULL;
454 BlockJob *job;
456 aio_context = bdrv_get_aio_context(bs);
457 aio_context_acquire(aio_context);
458 s = bs->opaque;
460 if (s->stage != BLOCK_REPLICATION_NONE) {
461 error_setg(errp, "Block replication is running or done");
462 aio_context_release(aio_context);
463 return;
466 if (s->mode != mode) {
467 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
468 " but got %d", s->mode, mode);
469 aio_context_release(aio_context);
470 return;
473 switch (s->mode) {
474 case REPLICATION_MODE_PRIMARY:
475 break;
476 case REPLICATION_MODE_SECONDARY:
477 s->active_disk = bs->file;
478 if (!s->active_disk || !s->active_disk->bs ||
479 !s->active_disk->bs->backing) {
480 error_setg(errp, "Active disk doesn't have backing file");
481 aio_context_release(aio_context);
482 return;
485 s->hidden_disk = s->active_disk->bs->backing;
486 if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) {
487 error_setg(errp, "Hidden disk doesn't have backing file");
488 aio_context_release(aio_context);
489 return;
492 s->secondary_disk = s->hidden_disk->bs->backing;
493 if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) {
494 error_setg(errp, "The secondary disk doesn't have block backend");
495 aio_context_release(aio_context);
496 return;
499 /* verify the length */
500 active_length = bdrv_getlength(s->active_disk->bs);
501 hidden_length = bdrv_getlength(s->hidden_disk->bs);
502 disk_length = bdrv_getlength(s->secondary_disk->bs);
503 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
504 active_length != hidden_length || hidden_length != disk_length) {
505 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
506 " are not the same");
507 aio_context_release(aio_context);
508 return;
511 /* Must be true, or the bdrv_getlength() calls would have failed */
512 assert(s->active_disk->bs->drv && s->hidden_disk->bs->drv);
514 if (!s->active_disk->bs->drv->bdrv_make_empty ||
515 !s->hidden_disk->bs->drv->bdrv_make_empty) {
516 error_setg(errp,
517 "Active disk or hidden disk doesn't support make_empty");
518 aio_context_release(aio_context);
519 return;
522 /* reopen the backing file in r/w mode */
523 reopen_backing_file(bs, true, &local_err);
524 if (local_err) {
525 error_propagate(errp, local_err);
526 aio_context_release(aio_context);
527 return;
530 /* start backup job now */
531 error_setg(&s->blocker,
532 "Block device is in use by internal backup job");
534 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
535 if (!top_bs || !bdrv_is_root_node(top_bs) ||
536 !check_top_bs(top_bs, bs)) {
537 error_setg(errp, "No top_bs or it is invalid");
538 reopen_backing_file(bs, false, NULL);
539 aio_context_release(aio_context);
540 return;
542 bdrv_op_block_all(top_bs, s->blocker);
543 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
545 job = backup_job_create(NULL, s->secondary_disk->bs, s->hidden_disk->bs,
546 0, MIRROR_SYNC_MODE_NONE, NULL, false,
547 BLOCKDEV_ON_ERROR_REPORT,
548 BLOCKDEV_ON_ERROR_REPORT, JOB_INTERNAL,
549 backup_job_completed, bs, NULL, &local_err);
550 if (local_err) {
551 error_propagate(errp, local_err);
552 backup_job_cleanup(bs);
553 aio_context_release(aio_context);
554 return;
556 job_start(&job->job);
557 break;
558 default:
559 aio_context_release(aio_context);
560 abort();
563 s->stage = BLOCK_REPLICATION_RUNNING;
565 if (s->mode == REPLICATION_MODE_SECONDARY) {
566 secondary_do_checkpoint(s, errp);
569 s->error = 0;
570 aio_context_release(aio_context);
573 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
575 BlockDriverState *bs = rs->opaque;
576 BDRVReplicationState *s;
577 AioContext *aio_context;
579 aio_context = bdrv_get_aio_context(bs);
580 aio_context_acquire(aio_context);
581 s = bs->opaque;
583 if (s->mode == REPLICATION_MODE_SECONDARY) {
584 secondary_do_checkpoint(s, errp);
586 aio_context_release(aio_context);
589 static void replication_get_error(ReplicationState *rs, Error **errp)
591 BlockDriverState *bs = rs->opaque;
592 BDRVReplicationState *s;
593 AioContext *aio_context;
595 aio_context = bdrv_get_aio_context(bs);
596 aio_context_acquire(aio_context);
597 s = bs->opaque;
599 if (s->stage != BLOCK_REPLICATION_RUNNING) {
600 error_setg(errp, "Block replication is not running");
601 aio_context_release(aio_context);
602 return;
605 if (s->error) {
606 error_setg(errp, "I/O error occurred");
607 aio_context_release(aio_context);
608 return;
610 aio_context_release(aio_context);
613 static void replication_done(void *opaque, int ret)
615 BlockDriverState *bs = opaque;
616 BDRVReplicationState *s = bs->opaque;
618 if (ret == 0) {
619 s->stage = BLOCK_REPLICATION_DONE;
621 /* refresh top bs's filename */
622 bdrv_refresh_filename(bs);
623 s->active_disk = NULL;
624 s->secondary_disk = NULL;
625 s->hidden_disk = NULL;
626 s->error = 0;
627 } else {
628 s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
629 s->error = -EIO;
633 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
635 BlockDriverState *bs = rs->opaque;
636 BDRVReplicationState *s;
637 AioContext *aio_context;
639 aio_context = bdrv_get_aio_context(bs);
640 aio_context_acquire(aio_context);
641 s = bs->opaque;
643 if (s->stage != BLOCK_REPLICATION_RUNNING) {
644 error_setg(errp, "Block replication is not running");
645 aio_context_release(aio_context);
646 return;
649 switch (s->mode) {
650 case REPLICATION_MODE_PRIMARY:
651 s->stage = BLOCK_REPLICATION_DONE;
652 s->error = 0;
653 break;
654 case REPLICATION_MODE_SECONDARY:
656 * This BDS will be closed, and the job should be completed
657 * before the BDS is closed, because we will access hidden
658 * disk, secondary disk in backup_job_completed().
660 if (s->secondary_disk->bs->job) {
661 job_cancel_sync(&s->secondary_disk->bs->job->job);
664 if (!failover) {
665 secondary_do_checkpoint(s, errp);
666 s->stage = BLOCK_REPLICATION_DONE;
667 aio_context_release(aio_context);
668 return;
671 s->stage = BLOCK_REPLICATION_FAILOVER;
672 commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs,
673 JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
674 NULL, replication_done, bs, true, errp);
675 break;
676 default:
677 aio_context_release(aio_context);
678 abort();
680 aio_context_release(aio_context);
683 BlockDriver bdrv_replication = {
684 .format_name = "replication",
685 .instance_size = sizeof(BDRVReplicationState),
687 .bdrv_open = replication_open,
688 .bdrv_close = replication_close,
689 .bdrv_child_perm = replication_child_perm,
691 .bdrv_getlength = replication_getlength,
692 .bdrv_co_readv = replication_co_readv,
693 .bdrv_co_writev = replication_co_writev,
695 .is_filter = true,
696 .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter,
698 .has_variable_length = true,
701 static void bdrv_replication_init(void)
703 bdrv_register(&bdrv_replication);
706 block_init(bdrv_replication_init);