spapr_events: add support for dedicated hotplug event source
[qemu/ar7.git] / block / replication.c
blob8bbfc8f870978070fe8f4d43f304199f616fe0a8
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 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
105 if (top_id) {
106 error_setg(&local_err, "The primary side does not support option top-id");
107 goto fail;
109 } else if (!strcmp(mode, "secondary")) {
110 s->mode = REPLICATION_MODE_SECONDARY;
111 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
112 s->top_id = g_strdup(top_id);
113 if (!s->top_id) {
114 error_setg(&local_err, "Missing the option top-id");
115 goto fail;
117 } else {
118 error_setg(&local_err,
119 "The option mode's value should be primary or secondary");
120 goto fail;
123 s->rs = replication_new(bs, &replication_ops);
125 ret = 0;
127 fail:
128 qemu_opts_del(opts);
129 error_propagate(errp, local_err);
131 return ret;
134 static void replication_close(BlockDriverState *bs)
136 BDRVReplicationState *s = bs->opaque;
138 if (s->replication_state == BLOCK_REPLICATION_RUNNING) {
139 replication_stop(s->rs, false, NULL);
142 if (s->mode == REPLICATION_MODE_SECONDARY) {
143 g_free(s->top_id);
146 replication_remove(s->rs);
149 static int64_t replication_getlength(BlockDriverState *bs)
151 return bdrv_getlength(bs->file->bs);
154 static int replication_get_io_status(BDRVReplicationState *s)
156 switch (s->replication_state) {
157 case BLOCK_REPLICATION_NONE:
158 return -EIO;
159 case BLOCK_REPLICATION_RUNNING:
160 return 0;
161 case BLOCK_REPLICATION_FAILOVER:
162 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
163 case BLOCK_REPLICATION_FAILOVER_FAILED:
164 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
165 case BLOCK_REPLICATION_DONE:
167 * active commit job completes, and active disk and secondary_disk
168 * is swapped, so we can operate bs->file directly
170 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
171 default:
172 abort();
176 static int replication_return_value(BDRVReplicationState *s, int ret)
178 if (s->mode == REPLICATION_MODE_SECONDARY) {
179 return ret;
182 if (ret < 0) {
183 s->error = ret;
184 ret = 0;
187 return ret;
190 static coroutine_fn int replication_co_readv(BlockDriverState *bs,
191 int64_t sector_num,
192 int remaining_sectors,
193 QEMUIOVector *qiov)
195 BDRVReplicationState *s = bs->opaque;
196 BdrvChild *child = s->secondary_disk;
197 BlockJob *job = NULL;
198 CowRequest req;
199 int ret;
201 if (s->mode == REPLICATION_MODE_PRIMARY) {
202 /* We only use it to forward primary write requests */
203 return -EIO;
206 ret = replication_get_io_status(s);
207 if (ret < 0) {
208 return ret;
211 if (child && child->bs) {
212 job = child->bs->job;
215 if (job) {
216 backup_wait_for_overlapping_requests(child->bs->job, sector_num,
217 remaining_sectors);
218 backup_cow_request_begin(&req, child->bs->job, sector_num,
219 remaining_sectors);
220 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors,
221 qiov);
222 backup_cow_request_end(&req);
223 goto out;
226 ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, qiov);
227 out:
228 return replication_return_value(s, ret);
231 static coroutine_fn int replication_co_writev(BlockDriverState *bs,
232 int64_t sector_num,
233 int remaining_sectors,
234 QEMUIOVector *qiov)
236 BDRVReplicationState *s = bs->opaque;
237 QEMUIOVector hd_qiov;
238 uint64_t bytes_done = 0;
239 BdrvChild *top = bs->file;
240 BdrvChild *base = s->secondary_disk;
241 BdrvChild *target;
242 int ret, n;
244 ret = replication_get_io_status(s);
245 if (ret < 0) {
246 goto out;
249 if (ret == 0) {
250 ret = bdrv_co_writev(top, sector_num,
251 remaining_sectors, qiov);
252 return replication_return_value(s, ret);
256 * Failover failed, only write to active disk if the sectors
257 * have already been allocated in active disk/hidden disk.
259 qemu_iovec_init(&hd_qiov, qiov->niov);
260 while (remaining_sectors > 0) {
261 ret = bdrv_is_allocated_above(top->bs, base->bs, sector_num,
262 remaining_sectors, &n);
263 if (ret < 0) {
264 goto out1;
267 qemu_iovec_reset(&hd_qiov);
268 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, n * BDRV_SECTOR_SIZE);
270 target = ret ? top : base;
271 ret = bdrv_co_writev(target, sector_num, n, &hd_qiov);
272 if (ret < 0) {
273 goto out1;
276 remaining_sectors -= n;
277 sector_num += n;
278 bytes_done += n * BDRV_SECTOR_SIZE;
281 out1:
282 qemu_iovec_destroy(&hd_qiov);
283 out:
284 return ret;
287 static bool replication_recurse_is_first_non_filter(BlockDriverState *bs,
288 BlockDriverState *candidate)
290 return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
293 static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp)
295 Error *local_err = NULL;
296 int ret;
298 if (!s->secondary_disk->bs->job) {
299 error_setg(errp, "Backup job was cancelled unexpectedly");
300 return;
303 backup_do_checkpoint(s->secondary_disk->bs->job, &local_err);
304 if (local_err) {
305 error_propagate(errp, local_err);
306 return;
309 ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs);
310 if (ret < 0) {
311 error_setg(errp, "Cannot make active disk empty");
312 return;
315 ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs);
316 if (ret < 0) {
317 error_setg(errp, "Cannot make hidden disk empty");
318 return;
322 static void reopen_backing_file(BDRVReplicationState *s, bool writable,
323 Error **errp)
325 BlockReopenQueue *reopen_queue = NULL;
326 int orig_hidden_flags, orig_secondary_flags;
327 int new_hidden_flags, new_secondary_flags;
328 Error *local_err = NULL;
330 if (writable) {
331 orig_hidden_flags = s->orig_hidden_flags =
332 bdrv_get_flags(s->hidden_disk->bs);
333 new_hidden_flags = (orig_hidden_flags | BDRV_O_RDWR) &
334 ~BDRV_O_INACTIVE;
335 orig_secondary_flags = s->orig_secondary_flags =
336 bdrv_get_flags(s->secondary_disk->bs);
337 new_secondary_flags = (orig_secondary_flags | BDRV_O_RDWR) &
338 ~BDRV_O_INACTIVE;
339 } else {
340 orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) &
341 ~BDRV_O_INACTIVE;
342 new_hidden_flags = s->orig_hidden_flags;
343 orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) &
344 ~BDRV_O_INACTIVE;
345 new_secondary_flags = s->orig_secondary_flags;
348 if (orig_hidden_flags != new_hidden_flags) {
349 reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL,
350 new_hidden_flags);
353 if (!(orig_secondary_flags & BDRV_O_RDWR)) {
354 reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs,
355 NULL, new_secondary_flags);
358 if (reopen_queue) {
359 bdrv_reopen_multiple(reopen_queue, &local_err);
360 error_propagate(errp, local_err);
364 static void backup_job_cleanup(BDRVReplicationState *s)
366 BlockDriverState *top_bs;
368 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
369 if (!top_bs) {
370 return;
372 bdrv_op_unblock_all(top_bs, s->blocker);
373 error_free(s->blocker);
374 reopen_backing_file(s, false, NULL);
377 static void backup_job_completed(void *opaque, int ret)
379 BDRVReplicationState *s = opaque;
381 if (s->replication_state != BLOCK_REPLICATION_FAILOVER) {
382 /* The backup job is cancelled unexpectedly */
383 s->error = -EIO;
386 backup_job_cleanup(s);
389 static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
391 BdrvChild *child;
393 /* The bs itself is the top_bs */
394 if (top_bs == bs) {
395 return true;
398 /* Iterate over top_bs's children */
399 QLIST_FOREACH(child, &top_bs->children, next) {
400 if (child->bs == bs || check_top_bs(child->bs, bs)) {
401 return true;
405 return false;
408 static void replication_start(ReplicationState *rs, ReplicationMode mode,
409 Error **errp)
411 BlockDriverState *bs = rs->opaque;
412 BDRVReplicationState *s;
413 BlockDriverState *top_bs;
414 int64_t active_length, hidden_length, disk_length;
415 AioContext *aio_context;
416 Error *local_err = NULL;
418 aio_context = bdrv_get_aio_context(bs);
419 aio_context_acquire(aio_context);
420 s = bs->opaque;
422 if (s->replication_state != BLOCK_REPLICATION_NONE) {
423 error_setg(errp, "Block replication is running or done");
424 aio_context_release(aio_context);
425 return;
428 if (s->mode != mode) {
429 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
430 " but got %d", s->mode, mode);
431 aio_context_release(aio_context);
432 return;
435 switch (s->mode) {
436 case REPLICATION_MODE_PRIMARY:
437 break;
438 case REPLICATION_MODE_SECONDARY:
439 s->active_disk = bs->file;
440 if (!s->active_disk || !s->active_disk->bs ||
441 !s->active_disk->bs->backing) {
442 error_setg(errp, "Active disk doesn't have backing file");
443 aio_context_release(aio_context);
444 return;
447 s->hidden_disk = s->active_disk->bs->backing;
448 if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) {
449 error_setg(errp, "Hidden disk doesn't have backing file");
450 aio_context_release(aio_context);
451 return;
454 s->secondary_disk = s->hidden_disk->bs->backing;
455 if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) {
456 error_setg(errp, "The secondary disk doesn't have block backend");
457 aio_context_release(aio_context);
458 return;
461 /* verify the length */
462 active_length = bdrv_getlength(s->active_disk->bs);
463 hidden_length = bdrv_getlength(s->hidden_disk->bs);
464 disk_length = bdrv_getlength(s->secondary_disk->bs);
465 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
466 active_length != hidden_length || hidden_length != disk_length) {
467 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
468 " are not the same");
469 aio_context_release(aio_context);
470 return;
473 if (!s->active_disk->bs->drv->bdrv_make_empty ||
474 !s->hidden_disk->bs->drv->bdrv_make_empty) {
475 error_setg(errp,
476 "Active disk or hidden disk doesn't support make_empty");
477 aio_context_release(aio_context);
478 return;
481 /* reopen the backing file in r/w mode */
482 reopen_backing_file(s, true, &local_err);
483 if (local_err) {
484 error_propagate(errp, local_err);
485 aio_context_release(aio_context);
486 return;
489 /* start backup job now */
490 error_setg(&s->blocker,
491 "Block device is in use by internal backup job");
493 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
494 if (!top_bs || !bdrv_is_root_node(top_bs) ||
495 !check_top_bs(top_bs, bs)) {
496 error_setg(errp, "No top_bs or it is invalid");
497 reopen_backing_file(s, false, NULL);
498 aio_context_release(aio_context);
499 return;
501 bdrv_op_block_all(top_bs, s->blocker);
502 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
504 backup_start("replication-backup", s->secondary_disk->bs,
505 s->hidden_disk->bs, 0, MIRROR_SYNC_MODE_NONE, NULL, false,
506 BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
507 backup_job_completed, s, NULL, &local_err);
508 if (local_err) {
509 error_propagate(errp, local_err);
510 backup_job_cleanup(s);
511 aio_context_release(aio_context);
512 return;
514 break;
515 default:
516 aio_context_release(aio_context);
517 abort();
520 s->replication_state = BLOCK_REPLICATION_RUNNING;
522 if (s->mode == REPLICATION_MODE_SECONDARY) {
523 secondary_do_checkpoint(s, errp);
526 s->error = 0;
527 aio_context_release(aio_context);
530 static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
532 BlockDriverState *bs = rs->opaque;
533 BDRVReplicationState *s;
534 AioContext *aio_context;
536 aio_context = bdrv_get_aio_context(bs);
537 aio_context_acquire(aio_context);
538 s = bs->opaque;
540 if (s->mode == REPLICATION_MODE_SECONDARY) {
541 secondary_do_checkpoint(s, errp);
543 aio_context_release(aio_context);
546 static void replication_get_error(ReplicationState *rs, Error **errp)
548 BlockDriverState *bs = rs->opaque;
549 BDRVReplicationState *s;
550 AioContext *aio_context;
552 aio_context = bdrv_get_aio_context(bs);
553 aio_context_acquire(aio_context);
554 s = bs->opaque;
556 if (s->replication_state != BLOCK_REPLICATION_RUNNING) {
557 error_setg(errp, "Block replication is not running");
558 aio_context_release(aio_context);
559 return;
562 if (s->error) {
563 error_setg(errp, "I/O error occurred");
564 aio_context_release(aio_context);
565 return;
567 aio_context_release(aio_context);
570 static void replication_done(void *opaque, int ret)
572 BlockDriverState *bs = opaque;
573 BDRVReplicationState *s = bs->opaque;
575 if (ret == 0) {
576 s->replication_state = BLOCK_REPLICATION_DONE;
578 /* refresh top bs's filename */
579 bdrv_refresh_filename(bs);
580 s->active_disk = NULL;
581 s->secondary_disk = NULL;
582 s->hidden_disk = NULL;
583 s->error = 0;
584 } else {
585 s->replication_state = BLOCK_REPLICATION_FAILOVER_FAILED;
586 s->error = -EIO;
590 static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
592 BlockDriverState *bs = rs->opaque;
593 BDRVReplicationState *s;
594 AioContext *aio_context;
596 aio_context = bdrv_get_aio_context(bs);
597 aio_context_acquire(aio_context);
598 s = bs->opaque;
600 if (s->replication_state != BLOCK_REPLICATION_RUNNING) {
601 error_setg(errp, "Block replication is not running");
602 aio_context_release(aio_context);
603 return;
606 switch (s->mode) {
607 case REPLICATION_MODE_PRIMARY:
608 s->replication_state = BLOCK_REPLICATION_DONE;
609 s->error = 0;
610 break;
611 case REPLICATION_MODE_SECONDARY:
613 * This BDS will be closed, and the job should be completed
614 * before the BDS is closed, because we will access hidden
615 * disk, secondary disk in backup_job_completed().
617 if (s->secondary_disk->bs->job) {
618 block_job_cancel_sync(s->secondary_disk->bs->job);
621 if (!failover) {
622 secondary_do_checkpoint(s, errp);
623 s->replication_state = BLOCK_REPLICATION_DONE;
624 aio_context_release(aio_context);
625 return;
628 s->replication_state = BLOCK_REPLICATION_FAILOVER;
629 commit_active_start("replication-commit", s->active_disk->bs,
630 s->secondary_disk->bs, 0, BLOCKDEV_ON_ERROR_REPORT,
631 replication_done,
632 bs, errp, true);
633 break;
634 default:
635 aio_context_release(aio_context);
636 abort();
638 aio_context_release(aio_context);
641 BlockDriver bdrv_replication = {
642 .format_name = "replication",
643 .protocol_name = "replication",
644 .instance_size = sizeof(BDRVReplicationState),
646 .bdrv_open = replication_open,
647 .bdrv_close = replication_close,
649 .bdrv_getlength = replication_getlength,
650 .bdrv_co_readv = replication_co_readv,
651 .bdrv_co_writev = replication_co_writev,
653 .is_filter = true,
654 .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter,
656 .has_variable_length = true,
659 static void bdrv_replication_init(void)
661 bdrv_register(&bdrv_replication);
664 block_init(bdrv_replication_init);