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