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