]> git.proxmox.com Git - mirror_qemu.git/blame - block/replication.c
MAINTAINERS: Change my email address
[mirror_qemu.git] / block / replication.c
CommitLineData
29ff7890
WC
1/*
2 * Replication Block filter
3 *
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 * Copyright (c) 2016 Intel Corporation
6 * Copyright (c) 2016 FUJITSU LIMITED
7 *
8 * Author:
9 * Wen Congyang <wency@cn.fujitsu.com>
10 *
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.
13 */
14
15#include "qemu/osdep.h"
0b8fa32f 16#include "qemu/module.h"
922a01a0 17#include "qemu/option.h"
29ff7890
WC
18#include "block/nbd.h"
19#include "block/blockjob.h"
20#include "block/block_int.h"
21#include "block/block_backup.h"
22#include "sysemu/block-backend.h"
23#include "qapi/error.h"
3c4e9647 24#include "qapi/qmp/qdict.h"
b0262955 25#include "block/replication.h"
29ff7890 26
3c76c606
FZ
27typedef enum {
28 BLOCK_REPLICATION_NONE, /* block replication is not started */
29 BLOCK_REPLICATION_RUNNING, /* block replication is running */
30 BLOCK_REPLICATION_FAILOVER, /* failover is running in background */
31 BLOCK_REPLICATION_FAILOVER_FAILED, /* failover failed */
32 BLOCK_REPLICATION_DONE, /* block replication is done */
33} ReplicationStage;
34
29ff7890
WC
35typedef struct BDRVReplicationState {
36 ReplicationMode mode;
3c76c606 37 ReplicationStage stage;
cc19f177 38 BlockJob *commit_job;
29ff7890
WC
39 BdrvChild *hidden_disk;
40 BdrvChild *secondary_disk;
cc19f177 41 BlockJob *backup_job;
29ff7890
WC
42 char *top_id;
43 ReplicationState *rs;
44 Error *blocker;
3c4e9647
AG
45 bool orig_hidden_read_only;
46 bool orig_secondary_read_only;
29ff7890
WC
47 int error;
48} BDRVReplicationState;
49
29ff7890
WC
50static void replication_start(ReplicationState *rs, ReplicationMode mode,
51 Error **errp);
52static void replication_do_checkpoint(ReplicationState *rs, Error **errp);
53static void replication_get_error(ReplicationState *rs, Error **errp);
54static void replication_stop(ReplicationState *rs, bool failover,
55 Error **errp);
56
57#define REPLICATION_MODE "mode"
58#define REPLICATION_TOP_ID "top-id"
59static QemuOptsList replication_runtime_opts = {
60 .name = "replication",
61 .head = QTAILQ_HEAD_INITIALIZER(replication_runtime_opts.head),
62 .desc = {
63 {
64 .name = REPLICATION_MODE,
65 .type = QEMU_OPT_STRING,
66 },
67 {
68 .name = REPLICATION_TOP_ID,
69 .type = QEMU_OPT_STRING,
70 },
71 { /* end of list */ }
72 },
73};
74
75static ReplicationOps replication_ops = {
76 .start = replication_start,
77 .checkpoint = replication_do_checkpoint,
78 .get_error = replication_get_error,
79 .stop = replication_stop,
80};
81
82static int replication_open(BlockDriverState *bs, QDict *options,
83 int flags, Error **errp)
84{
85 int ret;
86 BDRVReplicationState *s = bs->opaque;
29ff7890
WC
87 QemuOpts *opts = NULL;
88 const char *mode;
89 const char *top_id;
90
b3af2af4
HR
91 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
92 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
4e4bf5c4
KW
93 false, errp);
94 if (!bs->file) {
95 return -EINVAL;
96 }
97
29ff7890
WC
98 ret = -EINVAL;
99 opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
a5f9b9df 100 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
29ff7890
WC
101 goto fail;
102 }
103
104 mode = qemu_opt_get(opts, REPLICATION_MODE);
105 if (!mode) {
dcfe4805 106 error_setg(errp, "Missing the option mode");
29ff7890
WC
107 goto fail;
108 }
109
110 if (!strcmp(mode, "primary")) {
111 s->mode = REPLICATION_MODE_PRIMARY;
f4f2539b
CX
112 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
113 if (top_id) {
dcfe4805
MA
114 error_setg(errp,
115 "The primary side does not support option top-id");
f4f2539b
CX
116 goto fail;
117 }
29ff7890
WC
118 } else if (!strcmp(mode, "secondary")) {
119 s->mode = REPLICATION_MODE_SECONDARY;
120 top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
121 s->top_id = g_strdup(top_id);
122 if (!s->top_id) {
dcfe4805 123 error_setg(errp, "Missing the option top-id");
29ff7890
WC
124 goto fail;
125 }
126 } else {
dcfe4805 127 error_setg(errp,
29ff7890
WC
128 "The option mode's value should be primary or secondary");
129 goto fail;
130 }
131
132 s->rs = replication_new(bs, &replication_ops);
133
134 ret = 0;
135
136fail:
137 qemu_opts_del(opts);
29ff7890
WC
138 return ret;
139}
140
141static void replication_close(BlockDriverState *bs)
142{
143 BDRVReplicationState *s = bs->opaque;
08558e33 144 Job *commit_job;
29ff7890 145
3c76c606 146 if (s->stage == BLOCK_REPLICATION_RUNNING) {
29ff7890
WC
147 replication_stop(s->rs, false, NULL);
148 }
3c76c606 149 if (s->stage == BLOCK_REPLICATION_FAILOVER) {
08558e33
SR
150 commit_job = &s->commit_job->job;
151 assert(commit_job->aio_context == qemu_get_current_aio_context());
4cfb3f05 152 job_cancel_sync(commit_job, false);
50ab0e09 153 }
29ff7890
WC
154
155 if (s->mode == REPLICATION_MODE_SECONDARY) {
156 g_free(s->top_id);
157 }
158
159 replication_remove(s->rs);
160}
161
37a9051c 162static void replication_child_perm(BlockDriverState *bs, BdrvChild *c,
bf8e925e 163 BdrvChildRole role,
e0995dc3 164 BlockReopenQueue *reopen_queue,
37a9051c
CX
165 uint64_t perm, uint64_t shared,
166 uint64_t *nperm, uint64_t *nshared)
167{
3b78420b
LS
168 if (role & BDRV_CHILD_PRIMARY) {
169 *nperm = BLK_PERM_CONSISTENT_READ;
170 } else {
171 *nperm = 0;
172 }
173
611e0653
WG
174 if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
175 *nperm |= BLK_PERM_WRITE;
176 }
78ee6bd0
PMD
177 *nshared = BLK_PERM_CONSISTENT_READ
178 | BLK_PERM_WRITE
611e0653 179 | BLK_PERM_WRITE_UNCHANGED;
37a9051c
CX
180 return;
181}
182
29ff7890
WC
183static int64_t replication_getlength(BlockDriverState *bs)
184{
185 return bdrv_getlength(bs->file->bs);
186}
187
188static int replication_get_io_status(BDRVReplicationState *s)
189{
3c76c606 190 switch (s->stage) {
29ff7890
WC
191 case BLOCK_REPLICATION_NONE:
192 return -EIO;
193 case BLOCK_REPLICATION_RUNNING:
194 return 0;
195 case BLOCK_REPLICATION_FAILOVER:
196 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
197 case BLOCK_REPLICATION_FAILOVER_FAILED:
198 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1;
199 case BLOCK_REPLICATION_DONE:
200 /*
201 * active commit job completes, and active disk and secondary_disk
202 * is swapped, so we can operate bs->file directly
203 */
204 return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0;
205 default:
206 abort();
207 }
208}
209
210static int replication_return_value(BDRVReplicationState *s, int ret)
211{
212 if (s->mode == REPLICATION_MODE_SECONDARY) {
213 return ret;
214 }
215
216 if (ret < 0) {
217 s->error = ret;
218 ret = 0;
219 }
220
221 return ret;
222}
223
224static coroutine_fn int replication_co_readv(BlockDriverState *bs,
225 int64_t sector_num,
226 int remaining_sectors,
227 QEMUIOVector *qiov)
228{
229 BDRVReplicationState *s = bs->opaque;
29ff7890
WC
230 int ret;
231
232 if (s->mode == REPLICATION_MODE_PRIMARY) {
233 /* We only use it to forward primary write requests */
234 return -EIO;
235 }
236
237 ret = replication_get_io_status(s);
238 if (ret < 0) {
239 return ret;
240 }
241
04a11d87
EB
242 ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
243 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
e4f9752c 244
29ff7890
WC
245 return replication_return_value(s, ret);
246}
247
248static coroutine_fn int replication_co_writev(BlockDriverState *bs,
249 int64_t sector_num,
250 int remaining_sectors,
e18a58b4
EB
251 QEMUIOVector *qiov,
252 int flags)
29ff7890
WC
253{
254 BDRVReplicationState *s = bs->opaque;
255 QEMUIOVector hd_qiov;
256 uint64_t bytes_done = 0;
257 BdrvChild *top = bs->file;
258 BdrvChild *base = s->secondary_disk;
259 BdrvChild *target;
51b0a488
EB
260 int ret;
261 int64_t n;
29ff7890 262
e18a58b4 263 assert(!flags);
29ff7890
WC
264 ret = replication_get_io_status(s);
265 if (ret < 0) {
266 goto out;
267 }
268
269 if (ret == 0) {
04a11d87
EB
270 ret = bdrv_co_pwritev(top, sector_num * BDRV_SECTOR_SIZE,
271 remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
29ff7890
WC
272 return replication_return_value(s, ret);
273 }
274
275 /*
276 * Failover failed, only write to active disk if the sectors
277 * have already been allocated in active disk/hidden disk.
278 */
279 qemu_iovec_init(&hd_qiov, qiov->niov);
280 while (remaining_sectors > 0) {
51b0a488
EB
281 int64_t count;
282
170d3bd3 283 ret = bdrv_is_allocated_above(top->bs, base->bs, false,
51b0a488
EB
284 sector_num * BDRV_SECTOR_SIZE,
285 remaining_sectors * BDRV_SECTOR_SIZE,
286 &count);
29ff7890
WC
287 if (ret < 0) {
288 goto out1;
289 }
290
51b0a488
EB
291 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
292 n = count >> BDRV_SECTOR_BITS;
29ff7890 293 qemu_iovec_reset(&hd_qiov);
51b0a488 294 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count);
29ff7890
WC
295
296 target = ret ? top : base;
04a11d87
EB
297 ret = bdrv_co_pwritev(target, sector_num * BDRV_SECTOR_SIZE,
298 n * BDRV_SECTOR_SIZE, &hd_qiov, 0);
29ff7890
WC
299 if (ret < 0) {
300 goto out1;
301 }
302
303 remaining_sectors -= n;
304 sector_num += n;
51b0a488 305 bytes_done += count;
29ff7890
WC
306 }
307
308out1:
309 qemu_iovec_destroy(&hd_qiov);
310out:
311 return ret;
312}
313
1e12ecfd 314static void secondary_do_checkpoint(BlockDriverState *bs, Error **errp)
29ff7890 315{
1e12ecfd
LS
316 BDRVReplicationState *s = bs->opaque;
317 BdrvChild *active_disk = bs->file;
29ff7890
WC
318 Error *local_err = NULL;
319 int ret;
320
cc19f177 321 if (!s->backup_job) {
29ff7890
WC
322 error_setg(errp, "Backup job was cancelled unexpectedly");
323 return;
324 }
325
cc19f177 326 backup_do_checkpoint(s->backup_job, &local_err);
29ff7890
WC
327 if (local_err) {
328 error_propagate(errp, local_err);
329 return;
330 }
331
1e12ecfd 332 if (!active_disk->bs->drv) {
d470ad42 333 error_setg(errp, "Active disk %s is ejected",
1e12ecfd 334 active_disk->bs->node_name);
d470ad42
HR
335 return;
336 }
337
1e12ecfd 338 ret = bdrv_make_empty(active_disk, errp);
29ff7890 339 if (ret < 0) {
29ff7890
WC
340 return;
341 }
342
d470ad42
HR
343 if (!s->hidden_disk->bs->drv) {
344 error_setg(errp, "Hidden disk %s is ejected",
345 s->hidden_disk->bs->node_name);
346 return;
347 }
348
c2cf0eca 349 ret = bdrv_make_empty(s->hidden_disk, errp);
29ff7890 350 if (ret < 0) {
29ff7890
WC
351 return;
352 }
353}
354
3c4e9647
AG
355/* This function is supposed to be called twice:
356 * first with writable = true, then with writable = false.
357 * The first call puts s->hidden_disk and s->secondary_disk in
358 * r/w mode, and the second puts them back in their original state.
359 */
8dd9006e 360static void reopen_backing_file(BlockDriverState *bs, bool writable,
29ff7890
WC
361 Error **errp)
362{
8dd9006e 363 BDRVReplicationState *s = bs->opaque;
a990a42b 364 BdrvChild *hidden_disk, *secondary_disk;
29ff7890 365 BlockReopenQueue *reopen_queue = NULL;
29ff7890 366
a990a42b
LS
367 /*
368 * s->hidden_disk and s->secondary_disk may not be set yet, as they will
369 * only be set after the children are writable.
370 */
371 hidden_disk = bs->file->bs->backing;
372 secondary_disk = hidden_disk->bs->backing;
373
29ff7890 374 if (writable) {
a990a42b
LS
375 s->orig_hidden_read_only = bdrv_is_read_only(hidden_disk->bs);
376 s->orig_secondary_read_only = bdrv_is_read_only(secondary_disk->bs);
29ff7890
WC
377 }
378
a990a42b
LS
379 bdrv_subtree_drained_begin(hidden_disk->bs);
380 bdrv_subtree_drained_begin(secondary_disk->bs);
1a63a907 381
3c4e9647 382 if (s->orig_hidden_read_only) {
3c4e9647
AG
383 QDict *opts = qdict_new();
384 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
a990a42b 385 reopen_queue = bdrv_reopen_queue(reopen_queue, hidden_disk->bs,
077e8e20 386 opts, true);
29ff7890
WC
387 }
388
3c4e9647 389 if (s->orig_secondary_read_only) {
3c4e9647
AG
390 QDict *opts = qdict_new();
391 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
a990a42b 392 reopen_queue = bdrv_reopen_queue(reopen_queue, secondary_disk->bs,
077e8e20 393 opts, true);
29ff7890
WC
394 }
395
396 if (reopen_queue) {
6cf42ca2
KW
397 AioContext *ctx = bdrv_get_aio_context(bs);
398 if (ctx != qemu_get_aio_context()) {
399 aio_context_release(ctx);
400 }
992861fb 401 bdrv_reopen_multiple(reopen_queue, errp);
6cf42ca2
KW
402 if (ctx != qemu_get_aio_context()) {
403 aio_context_acquire(ctx);
404 }
29ff7890 405 }
1a63a907 406
a990a42b
LS
407 bdrv_subtree_drained_end(hidden_disk->bs);
408 bdrv_subtree_drained_end(secondary_disk->bs);
29ff7890
WC
409}
410
8dd9006e 411static void backup_job_cleanup(BlockDriverState *bs)
29ff7890 412{
8dd9006e 413 BDRVReplicationState *s = bs->opaque;
29ff7890
WC
414 BlockDriverState *top_bs;
415
e140f4b7
LS
416 s->backup_job = NULL;
417
29ff7890
WC
418 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
419 if (!top_bs) {
420 return;
421 }
422 bdrv_op_unblock_all(top_bs, s->blocker);
423 error_free(s->blocker);
8dd9006e 424 reopen_backing_file(bs, false, NULL);
29ff7890
WC
425}
426
427static void backup_job_completed(void *opaque, int ret)
428{
8dd9006e
PB
429 BlockDriverState *bs = opaque;
430 BDRVReplicationState *s = bs->opaque;
29ff7890 431
3c76c606 432 if (s->stage != BLOCK_REPLICATION_FAILOVER) {
29ff7890
WC
433 /* The backup job is cancelled unexpectedly */
434 s->error = -EIO;
435 }
436
8dd9006e 437 backup_job_cleanup(bs);
29ff7890
WC
438}
439
440static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs)
441{
442 BdrvChild *child;
443
444 /* The bs itself is the top_bs */
445 if (top_bs == bs) {
446 return true;
447 }
448
449 /* Iterate over top_bs's children */
450 QLIST_FOREACH(child, &top_bs->children, next) {
451 if (child->bs == bs || check_top_bs(child->bs, bs)) {
452 return true;
453 }
454 }
455
456 return false;
457}
458
459static void replication_start(ReplicationState *rs, ReplicationMode mode,
460 Error **errp)
461{
462 BlockDriverState *bs = rs->opaque;
463 BDRVReplicationState *s;
464 BlockDriverState *top_bs;
a990a42b 465 BdrvChild *active_disk, *hidden_disk, *secondary_disk;
29ff7890
WC
466 int64_t active_length, hidden_length, disk_length;
467 AioContext *aio_context;
468 Error *local_err = NULL;
2c59fd83 469 BackupPerf perf = { .use_copy_range = true, .max_workers = 1 };
29ff7890
WC
470
471 aio_context = bdrv_get_aio_context(bs);
472 aio_context_acquire(aio_context);
473 s = bs->opaque;
474
08ddb4eb
LS
475 if (s->stage == BLOCK_REPLICATION_DONE ||
476 s->stage == BLOCK_REPLICATION_FAILOVER) {
477 /*
478 * This case happens when a secondary is promoted to primary.
479 * Ignore the request because the secondary side of replication
480 * doesn't have to do anything anymore.
481 */
482 aio_context_release(aio_context);
483 return;
484 }
485
3c76c606 486 if (s->stage != BLOCK_REPLICATION_NONE) {
29ff7890
WC
487 error_setg(errp, "Block replication is running or done");
488 aio_context_release(aio_context);
489 return;
490 }
491
492 if (s->mode != mode) {
493 error_setg(errp, "The parameter mode's value is invalid, needs %d,"
494 " but got %d", s->mode, mode);
495 aio_context_release(aio_context);
496 return;
497 }
498
499 switch (s->mode) {
500 case REPLICATION_MODE_PRIMARY:
501 break;
502 case REPLICATION_MODE_SECONDARY:
1e12ecfd
LS
503 active_disk = bs->file;
504 if (!active_disk || !active_disk->bs || !active_disk->bs->backing) {
29ff7890
WC
505 error_setg(errp, "Active disk doesn't have backing file");
506 aio_context_release(aio_context);
507 return;
508 }
509
a990a42b
LS
510 hidden_disk = active_disk->bs->backing;
511 if (!hidden_disk->bs || !hidden_disk->bs->backing) {
29ff7890
WC
512 error_setg(errp, "Hidden disk doesn't have backing file");
513 aio_context_release(aio_context);
514 return;
515 }
516
a990a42b
LS
517 secondary_disk = hidden_disk->bs->backing;
518 if (!secondary_disk->bs || !bdrv_has_blk(secondary_disk->bs)) {
29ff7890
WC
519 error_setg(errp, "The secondary disk doesn't have block backend");
520 aio_context_release(aio_context);
521 return;
522 }
523
524 /* verify the length */
1e12ecfd 525 active_length = bdrv_getlength(active_disk->bs);
a990a42b
LS
526 hidden_length = bdrv_getlength(hidden_disk->bs);
527 disk_length = bdrv_getlength(secondary_disk->bs);
29ff7890
WC
528 if (active_length < 0 || hidden_length < 0 || disk_length < 0 ||
529 active_length != hidden_length || hidden_length != disk_length) {
530 error_setg(errp, "Active disk, hidden disk, secondary disk's length"
531 " are not the same");
532 aio_context_release(aio_context);
533 return;
534 }
535
d470ad42 536 /* Must be true, or the bdrv_getlength() calls would have failed */
a990a42b 537 assert(active_disk->bs->drv && hidden_disk->bs->drv);
d470ad42 538
1e12ecfd 539 if (!active_disk->bs->drv->bdrv_make_empty ||
a990a42b 540 !hidden_disk->bs->drv->bdrv_make_empty) {
29ff7890
WC
541 error_setg(errp,
542 "Active disk or hidden disk doesn't support make_empty");
543 aio_context_release(aio_context);
544 return;
545 }
546
547 /* reopen the backing file in r/w mode */
8dd9006e 548 reopen_backing_file(bs, true, &local_err);
29ff7890
WC
549 if (local_err) {
550 error_propagate(errp, local_err);
551 aio_context_release(aio_context);
552 return;
553 }
554
3b78420b
LS
555 bdrv_ref(hidden_disk->bs);
556 s->hidden_disk = bdrv_attach_child(bs, hidden_disk->bs, "hidden disk",
557 &child_of_bds, BDRV_CHILD_DATA,
558 &local_err);
559 if (local_err) {
560 error_propagate(errp, local_err);
561 aio_context_release(aio_context);
562 return;
563 }
564
565 bdrv_ref(secondary_disk->bs);
566 s->secondary_disk = bdrv_attach_child(bs, secondary_disk->bs,
567 "secondary disk", &child_of_bds,
568 BDRV_CHILD_DATA, &local_err);
569 if (local_err) {
570 error_propagate(errp, local_err);
571 aio_context_release(aio_context);
572 return;
573 }
a990a42b 574
29ff7890
WC
575 /* start backup job now */
576 error_setg(&s->blocker,
577 "Block device is in use by internal backup job");
578
579 top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
580 if (!top_bs || !bdrv_is_root_node(top_bs) ||
581 !check_top_bs(top_bs, bs)) {
582 error_setg(errp, "No top_bs or it is invalid");
8dd9006e 583 reopen_backing_file(bs, false, NULL);
29ff7890
WC
584 aio_context_release(aio_context);
585 return;
586 }
587 bdrv_op_block_all(top_bs, s->blocker);
588 bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
589
cc19f177
VSO
590 s->backup_job = backup_job_create(
591 NULL, s->secondary_disk->bs, s->hidden_disk->bs,
00e30f05 592 0, MIRROR_SYNC_MODE_NONE, NULL, 0, false, NULL,
86c6a3b6 593 &perf,
111049a4 594 BLOCKDEV_ON_ERROR_REPORT,
bb02b65c 595 BLOCKDEV_ON_ERROR_REPORT, JOB_INTERNAL,
111049a4 596 backup_job_completed, bs, NULL, &local_err);
29ff7890
WC
597 if (local_err) {
598 error_propagate(errp, local_err);
8dd9006e 599 backup_job_cleanup(bs);
29ff7890
WC
600 aio_context_release(aio_context);
601 return;
602 }
cc19f177 603 job_start(&s->backup_job->job);
29ff7890
WC
604 break;
605 default:
606 aio_context_release(aio_context);
607 abort();
608 }
609
3c76c606 610 s->stage = BLOCK_REPLICATION_RUNNING;
29ff7890
WC
611
612 if (s->mode == REPLICATION_MODE_SECONDARY) {
1e12ecfd 613 secondary_do_checkpoint(bs, errp);
29ff7890
WC
614 }
615
616 s->error = 0;
617 aio_context_release(aio_context);
618}
619
620static void replication_do_checkpoint(ReplicationState *rs, Error **errp)
621{
622 BlockDriverState *bs = rs->opaque;
623 BDRVReplicationState *s;
624 AioContext *aio_context;
625
626 aio_context = bdrv_get_aio_context(bs);
627 aio_context_acquire(aio_context);
628 s = bs->opaque;
629
08ddb4eb
LS
630 if (s->stage == BLOCK_REPLICATION_DONE ||
631 s->stage == BLOCK_REPLICATION_FAILOVER) {
632 /*
633 * This case happens when a secondary was promoted to primary.
634 * Ignore the request because the secondary side of replication
635 * doesn't have to do anything anymore.
636 */
637 aio_context_release(aio_context);
638 return;
639 }
640
29ff7890 641 if (s->mode == REPLICATION_MODE_SECONDARY) {
1e12ecfd 642 secondary_do_checkpoint(bs, errp);
29ff7890
WC
643 }
644 aio_context_release(aio_context);
645}
646
647static void replication_get_error(ReplicationState *rs, Error **errp)
648{
649 BlockDriverState *bs = rs->opaque;
650 BDRVReplicationState *s;
651 AioContext *aio_context;
652
653 aio_context = bdrv_get_aio_context(bs);
654 aio_context_acquire(aio_context);
655 s = bs->opaque;
656
08ddb4eb 657 if (s->stage == BLOCK_REPLICATION_NONE) {
29ff7890
WC
658 error_setg(errp, "Block replication is not running");
659 aio_context_release(aio_context);
660 return;
661 }
662
663 if (s->error) {
664 error_setg(errp, "I/O error occurred");
665 aio_context_release(aio_context);
666 return;
667 }
668 aio_context_release(aio_context);
669}
670
671static void replication_done(void *opaque, int ret)
672{
673 BlockDriverState *bs = opaque;
674 BDRVReplicationState *s = bs->opaque;
675
676 if (ret == 0) {
3c76c606 677 s->stage = BLOCK_REPLICATION_DONE;
29ff7890 678
3b78420b 679 bdrv_unref_child(bs, s->secondary_disk);
29ff7890 680 s->secondary_disk = NULL;
3b78420b 681 bdrv_unref_child(bs, s->hidden_disk);
29ff7890
WC
682 s->hidden_disk = NULL;
683 s->error = 0;
684 } else {
3c76c606 685 s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
29ff7890
WC
686 s->error = -EIO;
687 }
688}
689
690static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
691{
692 BlockDriverState *bs = rs->opaque;
693 BDRVReplicationState *s;
694 AioContext *aio_context;
695
696 aio_context = bdrv_get_aio_context(bs);
697 aio_context_acquire(aio_context);
698 s = bs->opaque;
699
08ddb4eb
LS
700 if (s->stage == BLOCK_REPLICATION_DONE ||
701 s->stage == BLOCK_REPLICATION_FAILOVER) {
702 /*
703 * This case happens when a secondary was promoted to primary.
704 * Ignore the request because the secondary side of replication
705 * doesn't have to do anything anymore.
706 */
707 aio_context_release(aio_context);
708 return;
709 }
710
3c76c606 711 if (s->stage != BLOCK_REPLICATION_RUNNING) {
29ff7890
WC
712 error_setg(errp, "Block replication is not running");
713 aio_context_release(aio_context);
714 return;
715 }
716
717 switch (s->mode) {
718 case REPLICATION_MODE_PRIMARY:
3c76c606 719 s->stage = BLOCK_REPLICATION_DONE;
29ff7890
WC
720 s->error = 0;
721 break;
722 case REPLICATION_MODE_SECONDARY:
723 /*
724 * This BDS will be closed, and the job should be completed
725 * before the BDS is closed, because we will access hidden
726 * disk, secondary disk in backup_job_completed().
727 */
cc19f177 728 if (s->backup_job) {
4cfb3f05 729 job_cancel_sync(&s->backup_job->job, true);
29ff7890
WC
730 }
731
732 if (!failover) {
1e12ecfd 733 secondary_do_checkpoint(bs, errp);
3c76c606 734 s->stage = BLOCK_REPLICATION_DONE;
29ff7890
WC
735 aio_context_release(aio_context);
736 return;
737 }
738
3c76c606 739 s->stage = BLOCK_REPLICATION_FAILOVER;
cc19f177 740 s->commit_job = commit_active_start(
1e12ecfd 741 NULL, bs->file->bs, s->secondary_disk->bs,
bb02b65c 742 JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
78bbd910 743 NULL, replication_done, bs, true, errp);
29ff7890
WC
744 break;
745 default:
746 aio_context_release(aio_context);
747 abort();
748 }
749 aio_context_release(aio_context);
750}
751
2654267c
HR
752static const char *const replication_strong_runtime_opts[] = {
753 REPLICATION_MODE,
754 REPLICATION_TOP_ID,
755
756 NULL
757};
758
782b9d06 759static BlockDriver bdrv_replication = {
29ff7890 760 .format_name = "replication",
29ff7890
WC
761 .instance_size = sizeof(BDRVReplicationState),
762
763 .bdrv_open = replication_open,
764 .bdrv_close = replication_close,
37a9051c 765 .bdrv_child_perm = replication_child_perm,
29ff7890
WC
766
767 .bdrv_getlength = replication_getlength,
768 .bdrv_co_readv = replication_co_readv,
769 .bdrv_co_writev = replication_co_writev,
770
771 .is_filter = true,
29ff7890
WC
772
773 .has_variable_length = true,
2654267c 774 .strong_runtime_opts = replication_strong_runtime_opts,
29ff7890
WC
775};
776
777static void bdrv_replication_init(void)
778{
779 bdrv_register(&bdrv_replication);
780}
781
782block_init(bdrv_replication_init);