]>
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" | |
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" | |
24 | ||
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; | |
38 | ||
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 */ | |
45 | }; | |
46 | ||
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); | |
53 | ||
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 = { | |
60 | { | |
61 | .name = REPLICATION_MODE, | |
62 | .type = QEMU_OPT_STRING, | |
63 | }, | |
64 | { | |
65 | .name = REPLICATION_TOP_ID, | |
66 | .type = QEMU_OPT_STRING, | |
67 | }, | |
68 | { /* end of list */ } | |
69 | }, | |
70 | }; | |
71 | ||
72 | static ReplicationOps replication_ops = { | |
73 | .start = replication_start, | |
74 | .checkpoint = replication_do_checkpoint, | |
75 | .get_error = replication_get_error, | |
76 | .stop = replication_stop, | |
77 | }; | |
78 | ||
79 | static int replication_open(BlockDriverState *bs, QDict *options, | |
80 | int flags, Error **errp) | |
81 | { | |
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; | |
88 | ||
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; | |
94 | } | |
95 | ||
96 | mode = qemu_opt_get(opts, REPLICATION_MODE); | |
97 | if (!mode) { | |
98 | error_setg(&local_err, "Missing the option mode"); | |
99 | goto fail; | |
100 | } | |
101 | ||
102 | if (!strcmp(mode, "primary")) { | |
103 | s->mode = REPLICATION_MODE_PRIMARY; | |
f4f2539b CX |
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; | |
108 | } | |
29ff7890 WC |
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; | |
116 | } | |
117 | } else { | |
118 | error_setg(&local_err, | |
119 | "The option mode's value should be primary or secondary"); | |
120 | goto fail; | |
121 | } | |
122 | ||
123 | s->rs = replication_new(bs, &replication_ops); | |
124 | ||
125 | ret = 0; | |
126 | ||
127 | fail: | |
128 | qemu_opts_del(opts); | |
129 | error_propagate(errp, local_err); | |
130 | ||
131 | return ret; | |
132 | } | |
133 | ||
134 | static void replication_close(BlockDriverState *bs) | |
135 | { | |
136 | BDRVReplicationState *s = bs->opaque; | |
137 | ||
138 | if (s->replication_state == BLOCK_REPLICATION_RUNNING) { | |
139 | replication_stop(s->rs, false, NULL); | |
140 | } | |
50ab0e09 PB |
141 | if (s->replication_state == BLOCK_REPLICATION_FAILOVER) { |
142 | block_job_cancel_sync(s->active_disk->bs->job); | |
143 | } | |
29ff7890 WC |
144 | |
145 | if (s->mode == REPLICATION_MODE_SECONDARY) { | |
146 | g_free(s->top_id); | |
147 | } | |
148 | ||
149 | replication_remove(s->rs); | |
150 | } | |
151 | ||
152 | static int64_t replication_getlength(BlockDriverState *bs) | |
153 | { | |
154 | return bdrv_getlength(bs->file->bs); | |
155 | } | |
156 | ||
157 | static int replication_get_io_status(BDRVReplicationState *s) | |
158 | { | |
159 | switch (s->replication_state) { | |
160 | case BLOCK_REPLICATION_NONE: | |
161 | return -EIO; | |
162 | case BLOCK_REPLICATION_RUNNING: | |
163 | return 0; | |
164 | case BLOCK_REPLICATION_FAILOVER: | |
165 | return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0; | |
166 | case BLOCK_REPLICATION_FAILOVER_FAILED: | |
167 | return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 1; | |
168 | case BLOCK_REPLICATION_DONE: | |
169 | /* | |
170 | * active commit job completes, and active disk and secondary_disk | |
171 | * is swapped, so we can operate bs->file directly | |
172 | */ | |
173 | return s->mode == REPLICATION_MODE_PRIMARY ? -EIO : 0; | |
174 | default: | |
175 | abort(); | |
176 | } | |
177 | } | |
178 | ||
179 | static int replication_return_value(BDRVReplicationState *s, int ret) | |
180 | { | |
181 | if (s->mode == REPLICATION_MODE_SECONDARY) { | |
182 | return ret; | |
183 | } | |
184 | ||
185 | if (ret < 0) { | |
186 | s->error = ret; | |
187 | ret = 0; | |
188 | } | |
189 | ||
190 | return ret; | |
191 | } | |
192 | ||
193 | static coroutine_fn int replication_co_readv(BlockDriverState *bs, | |
194 | int64_t sector_num, | |
195 | int remaining_sectors, | |
196 | QEMUIOVector *qiov) | |
197 | { | |
198 | BDRVReplicationState *s = bs->opaque; | |
199 | BdrvChild *child = s->secondary_disk; | |
200 | BlockJob *job = NULL; | |
201 | CowRequest req; | |
202 | int ret; | |
203 | ||
204 | if (s->mode == REPLICATION_MODE_PRIMARY) { | |
205 | /* We only use it to forward primary write requests */ | |
206 | return -EIO; | |
207 | } | |
208 | ||
209 | ret = replication_get_io_status(s); | |
210 | if (ret < 0) { | |
211 | return ret; | |
212 | } | |
213 | ||
214 | if (child && child->bs) { | |
215 | job = child->bs->job; | |
216 | } | |
217 | ||
218 | if (job) { | |
219 | backup_wait_for_overlapping_requests(child->bs->job, sector_num, | |
220 | remaining_sectors); | |
221 | backup_cow_request_begin(&req, child->bs->job, sector_num, | |
222 | remaining_sectors); | |
223 | ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, | |
224 | qiov); | |
225 | backup_cow_request_end(&req); | |
226 | goto out; | |
227 | } | |
228 | ||
229 | ret = bdrv_co_readv(bs->file, sector_num, remaining_sectors, qiov); | |
230 | out: | |
231 | return replication_return_value(s, ret); | |
232 | } | |
233 | ||
234 | static coroutine_fn int replication_co_writev(BlockDriverState *bs, | |
235 | int64_t sector_num, | |
236 | int remaining_sectors, | |
237 | QEMUIOVector *qiov) | |
238 | { | |
239 | BDRVReplicationState *s = bs->opaque; | |
240 | QEMUIOVector hd_qiov; | |
241 | uint64_t bytes_done = 0; | |
242 | BdrvChild *top = bs->file; | |
243 | BdrvChild *base = s->secondary_disk; | |
244 | BdrvChild *target; | |
245 | int ret, n; | |
246 | ||
247 | ret = replication_get_io_status(s); | |
248 | if (ret < 0) { | |
249 | goto out; | |
250 | } | |
251 | ||
252 | if (ret == 0) { | |
253 | ret = bdrv_co_writev(top, sector_num, | |
254 | remaining_sectors, qiov); | |
255 | return replication_return_value(s, ret); | |
256 | } | |
257 | ||
258 | /* | |
259 | * Failover failed, only write to active disk if the sectors | |
260 | * have already been allocated in active disk/hidden disk. | |
261 | */ | |
262 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
263 | while (remaining_sectors > 0) { | |
264 | ret = bdrv_is_allocated_above(top->bs, base->bs, sector_num, | |
265 | remaining_sectors, &n); | |
266 | if (ret < 0) { | |
267 | goto out1; | |
268 | } | |
269 | ||
270 | qemu_iovec_reset(&hd_qiov); | |
271 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, n * BDRV_SECTOR_SIZE); | |
272 | ||
273 | target = ret ? top : base; | |
274 | ret = bdrv_co_writev(target, sector_num, n, &hd_qiov); | |
275 | if (ret < 0) { | |
276 | goto out1; | |
277 | } | |
278 | ||
279 | remaining_sectors -= n; | |
280 | sector_num += n; | |
281 | bytes_done += n * BDRV_SECTOR_SIZE; | |
282 | } | |
283 | ||
284 | out1: | |
285 | qemu_iovec_destroy(&hd_qiov); | |
286 | out: | |
287 | return ret; | |
288 | } | |
289 | ||
290 | static bool replication_recurse_is_first_non_filter(BlockDriverState *bs, | |
291 | BlockDriverState *candidate) | |
292 | { | |
293 | return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate); | |
294 | } | |
295 | ||
296 | static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp) | |
297 | { | |
298 | Error *local_err = NULL; | |
299 | int ret; | |
300 | ||
301 | if (!s->secondary_disk->bs->job) { | |
302 | error_setg(errp, "Backup job was cancelled unexpectedly"); | |
303 | return; | |
304 | } | |
305 | ||
306 | backup_do_checkpoint(s->secondary_disk->bs->job, &local_err); | |
307 | if (local_err) { | |
308 | error_propagate(errp, local_err); | |
309 | return; | |
310 | } | |
311 | ||
312 | ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs); | |
313 | if (ret < 0) { | |
314 | error_setg(errp, "Cannot make active disk empty"); | |
315 | return; | |
316 | } | |
317 | ||
318 | ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs); | |
319 | if (ret < 0) { | |
320 | error_setg(errp, "Cannot make hidden disk empty"); | |
321 | return; | |
322 | } | |
323 | } | |
324 | ||
8dd9006e | 325 | static void reopen_backing_file(BlockDriverState *bs, bool writable, |
29ff7890 WC |
326 | Error **errp) |
327 | { | |
8dd9006e | 328 | BDRVReplicationState *s = bs->opaque; |
29ff7890 WC |
329 | BlockReopenQueue *reopen_queue = NULL; |
330 | int orig_hidden_flags, orig_secondary_flags; | |
331 | int new_hidden_flags, new_secondary_flags; | |
332 | Error *local_err = NULL; | |
333 | ||
334 | if (writable) { | |
335 | orig_hidden_flags = s->orig_hidden_flags = | |
336 | bdrv_get_flags(s->hidden_disk->bs); | |
337 | new_hidden_flags = (orig_hidden_flags | BDRV_O_RDWR) & | |
338 | ~BDRV_O_INACTIVE; | |
339 | orig_secondary_flags = s->orig_secondary_flags = | |
340 | bdrv_get_flags(s->secondary_disk->bs); | |
341 | new_secondary_flags = (orig_secondary_flags | BDRV_O_RDWR) & | |
342 | ~BDRV_O_INACTIVE; | |
343 | } else { | |
344 | orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) & | |
345 | ~BDRV_O_INACTIVE; | |
346 | new_hidden_flags = s->orig_hidden_flags; | |
347 | orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) & | |
348 | ~BDRV_O_INACTIVE; | |
349 | new_secondary_flags = s->orig_secondary_flags; | |
350 | } | |
351 | ||
352 | if (orig_hidden_flags != new_hidden_flags) { | |
353 | reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL, | |
354 | new_hidden_flags); | |
355 | } | |
356 | ||
357 | if (!(orig_secondary_flags & BDRV_O_RDWR)) { | |
358 | reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs, | |
359 | NULL, new_secondary_flags); | |
360 | } | |
361 | ||
362 | if (reopen_queue) { | |
720150f3 PB |
363 | bdrv_reopen_multiple(bdrv_get_aio_context(bs), |
364 | reopen_queue, &local_err); | |
29ff7890 WC |
365 | error_propagate(errp, local_err); |
366 | } | |
367 | } | |
368 | ||
8dd9006e | 369 | static void backup_job_cleanup(BlockDriverState *bs) |
29ff7890 | 370 | { |
8dd9006e | 371 | BDRVReplicationState *s = bs->opaque; |
29ff7890 WC |
372 | BlockDriverState *top_bs; |
373 | ||
374 | top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL); | |
375 | if (!top_bs) { | |
376 | return; | |
377 | } | |
378 | bdrv_op_unblock_all(top_bs, s->blocker); | |
379 | error_free(s->blocker); | |
8dd9006e | 380 | reopen_backing_file(bs, false, NULL); |
29ff7890 WC |
381 | } |
382 | ||
383 | static void backup_job_completed(void *opaque, int ret) | |
384 | { | |
8dd9006e PB |
385 | BlockDriverState *bs = opaque; |
386 | BDRVReplicationState *s = bs->opaque; | |
29ff7890 WC |
387 | |
388 | if (s->replication_state != BLOCK_REPLICATION_FAILOVER) { | |
389 | /* The backup job is cancelled unexpectedly */ | |
390 | s->error = -EIO; | |
391 | } | |
392 | ||
8dd9006e | 393 | backup_job_cleanup(bs); |
29ff7890 WC |
394 | } |
395 | ||
396 | static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs) | |
397 | { | |
398 | BdrvChild *child; | |
399 | ||
400 | /* The bs itself is the top_bs */ | |
401 | if (top_bs == bs) { | |
402 | return true; | |
403 | } | |
404 | ||
405 | /* Iterate over top_bs's children */ | |
406 | QLIST_FOREACH(child, &top_bs->children, next) { | |
407 | if (child->bs == bs || check_top_bs(child->bs, bs)) { | |
408 | return true; | |
409 | } | |
410 | } | |
411 | ||
412 | return false; | |
413 | } | |
414 | ||
415 | static void replication_start(ReplicationState *rs, ReplicationMode mode, | |
416 | Error **errp) | |
417 | { | |
418 | BlockDriverState *bs = rs->opaque; | |
419 | BDRVReplicationState *s; | |
420 | BlockDriverState *top_bs; | |
421 | int64_t active_length, hidden_length, disk_length; | |
422 | AioContext *aio_context; | |
423 | Error *local_err = NULL; | |
424 | ||
425 | aio_context = bdrv_get_aio_context(bs); | |
426 | aio_context_acquire(aio_context); | |
427 | s = bs->opaque; | |
428 | ||
429 | if (s->replication_state != BLOCK_REPLICATION_NONE) { | |
430 | error_setg(errp, "Block replication is running or done"); | |
431 | aio_context_release(aio_context); | |
432 | return; | |
433 | } | |
434 | ||
435 | if (s->mode != mode) { | |
436 | error_setg(errp, "The parameter mode's value is invalid, needs %d," | |
437 | " but got %d", s->mode, mode); | |
438 | aio_context_release(aio_context); | |
439 | return; | |
440 | } | |
441 | ||
442 | switch (s->mode) { | |
443 | case REPLICATION_MODE_PRIMARY: | |
444 | break; | |
445 | case REPLICATION_MODE_SECONDARY: | |
446 | s->active_disk = bs->file; | |
447 | if (!s->active_disk || !s->active_disk->bs || | |
448 | !s->active_disk->bs->backing) { | |
449 | error_setg(errp, "Active disk doesn't have backing file"); | |
450 | aio_context_release(aio_context); | |
451 | return; | |
452 | } | |
453 | ||
454 | s->hidden_disk = s->active_disk->bs->backing; | |
455 | if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) { | |
456 | error_setg(errp, "Hidden disk doesn't have backing file"); | |
457 | aio_context_release(aio_context); | |
458 | return; | |
459 | } | |
460 | ||
461 | s->secondary_disk = s->hidden_disk->bs->backing; | |
462 | if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) { | |
463 | error_setg(errp, "The secondary disk doesn't have block backend"); | |
464 | aio_context_release(aio_context); | |
465 | return; | |
466 | } | |
467 | ||
468 | /* verify the length */ | |
469 | active_length = bdrv_getlength(s->active_disk->bs); | |
470 | hidden_length = bdrv_getlength(s->hidden_disk->bs); | |
471 | disk_length = bdrv_getlength(s->secondary_disk->bs); | |
472 | if (active_length < 0 || hidden_length < 0 || disk_length < 0 || | |
473 | active_length != hidden_length || hidden_length != disk_length) { | |
474 | error_setg(errp, "Active disk, hidden disk, secondary disk's length" | |
475 | " are not the same"); | |
476 | aio_context_release(aio_context); | |
477 | return; | |
478 | } | |
479 | ||
480 | if (!s->active_disk->bs->drv->bdrv_make_empty || | |
481 | !s->hidden_disk->bs->drv->bdrv_make_empty) { | |
482 | error_setg(errp, | |
483 | "Active disk or hidden disk doesn't support make_empty"); | |
484 | aio_context_release(aio_context); | |
485 | return; | |
486 | } | |
487 | ||
488 | /* reopen the backing file in r/w mode */ | |
8dd9006e | 489 | reopen_backing_file(bs, true, &local_err); |
29ff7890 WC |
490 | if (local_err) { |
491 | error_propagate(errp, local_err); | |
492 | aio_context_release(aio_context); | |
493 | return; | |
494 | } | |
495 | ||
496 | /* start backup job now */ | |
497 | error_setg(&s->blocker, | |
498 | "Block device is in use by internal backup job"); | |
499 | ||
500 | top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL); | |
501 | if (!top_bs || !bdrv_is_root_node(top_bs) || | |
502 | !check_top_bs(top_bs, bs)) { | |
503 | error_setg(errp, "No top_bs or it is invalid"); | |
8dd9006e | 504 | reopen_backing_file(bs, false, NULL); |
29ff7890 WC |
505 | aio_context_release(aio_context); |
506 | return; | |
507 | } | |
508 | bdrv_op_block_all(top_bs, s->blocker); | |
509 | bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker); | |
510 | ||
47970dfb JS |
511 | backup_start(NULL, s->secondary_disk->bs, s->hidden_disk->bs, 0, |
512 | MIRROR_SYNC_MODE_NONE, NULL, false, | |
29ff7890 | 513 | BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT, |
47970dfb JS |
514 | BLOCK_JOB_INTERNAL, backup_job_completed, bs, |
515 | NULL, &local_err); | |
29ff7890 WC |
516 | if (local_err) { |
517 | error_propagate(errp, local_err); | |
8dd9006e | 518 | backup_job_cleanup(bs); |
29ff7890 WC |
519 | aio_context_release(aio_context); |
520 | return; | |
521 | } | |
522 | break; | |
523 | default: | |
524 | aio_context_release(aio_context); | |
525 | abort(); | |
526 | } | |
527 | ||
528 | s->replication_state = BLOCK_REPLICATION_RUNNING; | |
529 | ||
530 | if (s->mode == REPLICATION_MODE_SECONDARY) { | |
531 | secondary_do_checkpoint(s, errp); | |
532 | } | |
533 | ||
534 | s->error = 0; | |
535 | aio_context_release(aio_context); | |
536 | } | |
537 | ||
538 | static void replication_do_checkpoint(ReplicationState *rs, Error **errp) | |
539 | { | |
540 | BlockDriverState *bs = rs->opaque; | |
541 | BDRVReplicationState *s; | |
542 | AioContext *aio_context; | |
543 | ||
544 | aio_context = bdrv_get_aio_context(bs); | |
545 | aio_context_acquire(aio_context); | |
546 | s = bs->opaque; | |
547 | ||
548 | if (s->mode == REPLICATION_MODE_SECONDARY) { | |
549 | secondary_do_checkpoint(s, errp); | |
550 | } | |
551 | aio_context_release(aio_context); | |
552 | } | |
553 | ||
554 | static void replication_get_error(ReplicationState *rs, Error **errp) | |
555 | { | |
556 | BlockDriverState *bs = rs->opaque; | |
557 | BDRVReplicationState *s; | |
558 | AioContext *aio_context; | |
559 | ||
560 | aio_context = bdrv_get_aio_context(bs); | |
561 | aio_context_acquire(aio_context); | |
562 | s = bs->opaque; | |
563 | ||
564 | if (s->replication_state != BLOCK_REPLICATION_RUNNING) { | |
565 | error_setg(errp, "Block replication is not running"); | |
566 | aio_context_release(aio_context); | |
567 | return; | |
568 | } | |
569 | ||
570 | if (s->error) { | |
571 | error_setg(errp, "I/O error occurred"); | |
572 | aio_context_release(aio_context); | |
573 | return; | |
574 | } | |
575 | aio_context_release(aio_context); | |
576 | } | |
577 | ||
578 | static void replication_done(void *opaque, int ret) | |
579 | { | |
580 | BlockDriverState *bs = opaque; | |
581 | BDRVReplicationState *s = bs->opaque; | |
582 | ||
583 | if (ret == 0) { | |
584 | s->replication_state = BLOCK_REPLICATION_DONE; | |
585 | ||
586 | /* refresh top bs's filename */ | |
587 | bdrv_refresh_filename(bs); | |
588 | s->active_disk = NULL; | |
589 | s->secondary_disk = NULL; | |
590 | s->hidden_disk = NULL; | |
591 | s->error = 0; | |
592 | } else { | |
593 | s->replication_state = BLOCK_REPLICATION_FAILOVER_FAILED; | |
594 | s->error = -EIO; | |
595 | } | |
596 | } | |
597 | ||
598 | static void replication_stop(ReplicationState *rs, bool failover, Error **errp) | |
599 | { | |
600 | BlockDriverState *bs = rs->opaque; | |
601 | BDRVReplicationState *s; | |
602 | AioContext *aio_context; | |
603 | ||
604 | aio_context = bdrv_get_aio_context(bs); | |
605 | aio_context_acquire(aio_context); | |
606 | s = bs->opaque; | |
607 | ||
608 | if (s->replication_state != BLOCK_REPLICATION_RUNNING) { | |
609 | error_setg(errp, "Block replication is not running"); | |
610 | aio_context_release(aio_context); | |
611 | return; | |
612 | } | |
613 | ||
614 | switch (s->mode) { | |
615 | case REPLICATION_MODE_PRIMARY: | |
616 | s->replication_state = BLOCK_REPLICATION_DONE; | |
617 | s->error = 0; | |
618 | break; | |
619 | case REPLICATION_MODE_SECONDARY: | |
620 | /* | |
621 | * This BDS will be closed, and the job should be completed | |
622 | * before the BDS is closed, because we will access hidden | |
623 | * disk, secondary disk in backup_job_completed(). | |
624 | */ | |
625 | if (s->secondary_disk->bs->job) { | |
626 | block_job_cancel_sync(s->secondary_disk->bs->job); | |
627 | } | |
628 | ||
629 | if (!failover) { | |
630 | secondary_do_checkpoint(s, errp); | |
631 | s->replication_state = BLOCK_REPLICATION_DONE; | |
632 | aio_context_release(aio_context); | |
633 | return; | |
634 | } | |
635 | ||
636 | s->replication_state = BLOCK_REPLICATION_FAILOVER; | |
47970dfb JS |
637 | commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs, |
638 | BLOCK_JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT, | |
639 | replication_done, bs, errp, true); | |
29ff7890 WC |
640 | break; |
641 | default: | |
642 | aio_context_release(aio_context); | |
643 | abort(); | |
644 | } | |
645 | aio_context_release(aio_context); | |
646 | } | |
647 | ||
648 | BlockDriver bdrv_replication = { | |
649 | .format_name = "replication", | |
650 | .protocol_name = "replication", | |
651 | .instance_size = sizeof(BDRVReplicationState), | |
652 | ||
653 | .bdrv_open = replication_open, | |
654 | .bdrv_close = replication_close, | |
655 | ||
656 | .bdrv_getlength = replication_getlength, | |
657 | .bdrv_co_readv = replication_co_readv, | |
658 | .bdrv_co_writev = replication_co_writev, | |
659 | ||
660 | .is_filter = true, | |
661 | .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter, | |
662 | ||
663 | .has_variable_length = true, | |
664 | }; | |
665 | ||
666 | static void bdrv_replication_init(void) | |
667 | { | |
668 | bdrv_register(&bdrv_replication); | |
669 | } | |
670 | ||
671 | block_init(bdrv_replication_init); |