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