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