Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 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" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 16 | #include "qemu/option.h" |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 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 | |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 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 | |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 33 | typedef struct BDRVReplicationState { |
| 34 | ReplicationMode mode; |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 35 | ReplicationStage stage; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 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 | |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 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 | |
Kevin Wolf | 4e4bf5c | 2016-12-16 18:52:37 +0100 | [diff] [blame] | 89 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, |
| 90 | false, errp); |
| 91 | if (!bs->file) { |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 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; |
Changlong Xie | f4f2539 | 2016-10-12 12:50:08 +0800 | [diff] [blame] | 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 | } |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 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 | |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 144 | if (s->stage == BLOCK_REPLICATION_RUNNING) { |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 145 | replication_stop(s->rs, false, NULL); |
| 146 | } |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 147 | if (s->stage == BLOCK_REPLICATION_FAILOVER) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 148 | job_cancel_sync(&s->active_disk->bs->job->job); |
Paolo Bonzini | 50ab0e0 | 2016-10-27 12:48:49 +0200 | [diff] [blame] | 149 | } |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 150 | |
| 151 | if (s->mode == REPLICATION_MODE_SECONDARY) { |
| 152 | g_free(s->top_id); |
| 153 | } |
| 154 | |
| 155 | replication_remove(s->rs); |
| 156 | } |
| 157 | |
Changlong Xie | 37a9051 | 2017-03-14 19:46:52 +0800 | [diff] [blame] | 158 | static void replication_child_perm(BlockDriverState *bs, BdrvChild *c, |
| 159 | const BdrvChildRole *role, |
Kevin Wolf | e0995dc | 2017-09-14 12:47:11 +0200 | [diff] [blame] | 160 | BlockReopenQueue *reopen_queue, |
Changlong Xie | 37a9051 | 2017-03-14 19:46:52 +0800 | [diff] [blame] | 161 | uint64_t perm, uint64_t shared, |
| 162 | uint64_t *nperm, uint64_t *nshared) |
| 163 | { |
Wang Guang | 611e065 | 2017-10-25 14:51:23 +0800 | [diff] [blame] | 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; |
Changlong Xie | 37a9051 | 2017-03-14 19:46:52 +0800 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 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 | { |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 181 | switch (s->stage) { |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 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) { |
Eric Blake | f6ac207 | 2017-07-07 07:44:54 -0500 | [diff] [blame] | 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); |
Eric Blake | 04a11d8 | 2018-06-28 15:15:23 -0500 | [diff] [blame] | 249 | ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE, |
| 250 | remaining_bytes, qiov, 0); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 251 | backup_cow_request_end(&req); |
| 252 | goto out; |
| 253 | } |
| 254 | |
Eric Blake | 04a11d8 | 2018-06-28 15:15:23 -0500 | [diff] [blame] | 255 | ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE, |
| 256 | remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 257 | out: |
| 258 | return replication_return_value(s, ret); |
| 259 | } |
| 260 | |
| 261 | static coroutine_fn int replication_co_writev(BlockDriverState *bs, |
| 262 | int64_t sector_num, |
| 263 | int remaining_sectors, |
Eric Blake | e18a58b | 2018-04-24 17:01:57 -0500 | [diff] [blame] | 264 | QEMUIOVector *qiov, |
| 265 | int flags) |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 266 | { |
| 267 | BDRVReplicationState *s = bs->opaque; |
| 268 | QEMUIOVector hd_qiov; |
| 269 | uint64_t bytes_done = 0; |
| 270 | BdrvChild *top = bs->file; |
| 271 | BdrvChild *base = s->secondary_disk; |
| 272 | BdrvChild *target; |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 273 | int ret; |
| 274 | int64_t n; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 275 | |
Eric Blake | e18a58b | 2018-04-24 17:01:57 -0500 | [diff] [blame] | 276 | assert(!flags); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 277 | ret = replication_get_io_status(s); |
| 278 | if (ret < 0) { |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | if (ret == 0) { |
Eric Blake | 04a11d8 | 2018-06-28 15:15:23 -0500 | [diff] [blame] | 283 | ret = bdrv_co_pwritev(top, sector_num * BDRV_SECTOR_SIZE, |
| 284 | remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 285 | return replication_return_value(s, ret); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Failover failed, only write to active disk if the sectors |
| 290 | * have already been allocated in active disk/hidden disk. |
| 291 | */ |
| 292 | qemu_iovec_init(&hd_qiov, qiov->niov); |
| 293 | while (remaining_sectors > 0) { |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 294 | int64_t count; |
| 295 | |
| 296 | ret = bdrv_is_allocated_above(top->bs, base->bs, |
| 297 | sector_num * BDRV_SECTOR_SIZE, |
| 298 | remaining_sectors * BDRV_SECTOR_SIZE, |
| 299 | &count); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 300 | if (ret < 0) { |
| 301 | goto out1; |
| 302 | } |
| 303 | |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 304 | assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)); |
| 305 | n = count >> BDRV_SECTOR_BITS; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 306 | qemu_iovec_reset(&hd_qiov); |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 307 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, count); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 308 | |
| 309 | target = ret ? top : base; |
Eric Blake | 04a11d8 | 2018-06-28 15:15:23 -0500 | [diff] [blame] | 310 | ret = bdrv_co_pwritev(target, sector_num * BDRV_SECTOR_SIZE, |
| 311 | n * BDRV_SECTOR_SIZE, &hd_qiov, 0); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 312 | if (ret < 0) { |
| 313 | goto out1; |
| 314 | } |
| 315 | |
| 316 | remaining_sectors -= n; |
| 317 | sector_num += n; |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 318 | bytes_done += count; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | out1: |
| 322 | qemu_iovec_destroy(&hd_qiov); |
| 323 | out: |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | static bool replication_recurse_is_first_non_filter(BlockDriverState *bs, |
| 328 | BlockDriverState *candidate) |
| 329 | { |
| 330 | return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate); |
| 331 | } |
| 332 | |
| 333 | static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp) |
| 334 | { |
| 335 | Error *local_err = NULL; |
| 336 | int ret; |
| 337 | |
| 338 | if (!s->secondary_disk->bs->job) { |
| 339 | error_setg(errp, "Backup job was cancelled unexpectedly"); |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | backup_do_checkpoint(s->secondary_disk->bs->job, &local_err); |
| 344 | if (local_err) { |
| 345 | error_propagate(errp, local_err); |
| 346 | return; |
| 347 | } |
| 348 | |
Max Reitz | d470ad4 | 2017-11-10 21:31:09 +0100 | [diff] [blame] | 349 | if (!s->active_disk->bs->drv) { |
| 350 | error_setg(errp, "Active disk %s is ejected", |
| 351 | s->active_disk->bs->node_name); |
| 352 | return; |
| 353 | } |
| 354 | |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 355 | ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs); |
| 356 | if (ret < 0) { |
| 357 | error_setg(errp, "Cannot make active disk empty"); |
| 358 | return; |
| 359 | } |
| 360 | |
Max Reitz | d470ad4 | 2017-11-10 21:31:09 +0100 | [diff] [blame] | 361 | if (!s->hidden_disk->bs->drv) { |
| 362 | error_setg(errp, "Hidden disk %s is ejected", |
| 363 | s->hidden_disk->bs->node_name); |
| 364 | return; |
| 365 | } |
| 366 | |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 367 | ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs); |
| 368 | if (ret < 0) { |
| 369 | error_setg(errp, "Cannot make hidden disk empty"); |
| 370 | return; |
| 371 | } |
| 372 | } |
| 373 | |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 374 | static void reopen_backing_file(BlockDriverState *bs, bool writable, |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 375 | Error **errp) |
| 376 | { |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 377 | BDRVReplicationState *s = bs->opaque; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 378 | BlockReopenQueue *reopen_queue = NULL; |
| 379 | int orig_hidden_flags, orig_secondary_flags; |
| 380 | int new_hidden_flags, new_secondary_flags; |
| 381 | Error *local_err = NULL; |
| 382 | |
| 383 | if (writable) { |
| 384 | orig_hidden_flags = s->orig_hidden_flags = |
| 385 | bdrv_get_flags(s->hidden_disk->bs); |
| 386 | new_hidden_flags = (orig_hidden_flags | BDRV_O_RDWR) & |
| 387 | ~BDRV_O_INACTIVE; |
| 388 | orig_secondary_flags = s->orig_secondary_flags = |
| 389 | bdrv_get_flags(s->secondary_disk->bs); |
| 390 | new_secondary_flags = (orig_secondary_flags | BDRV_O_RDWR) & |
| 391 | ~BDRV_O_INACTIVE; |
| 392 | } else { |
| 393 | orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) & |
| 394 | ~BDRV_O_INACTIVE; |
| 395 | new_hidden_flags = s->orig_hidden_flags; |
| 396 | orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) & |
| 397 | ~BDRV_O_INACTIVE; |
| 398 | new_secondary_flags = s->orig_secondary_flags; |
| 399 | } |
| 400 | |
Kevin Wolf | 1a63a90 | 2017-12-06 20:24:44 +0100 | [diff] [blame] | 401 | bdrv_subtree_drained_begin(s->hidden_disk->bs); |
| 402 | bdrv_subtree_drained_begin(s->secondary_disk->bs); |
| 403 | |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 404 | if (orig_hidden_flags != new_hidden_flags) { |
| 405 | reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL, |
| 406 | new_hidden_flags); |
| 407 | } |
| 408 | |
| 409 | if (!(orig_secondary_flags & BDRV_O_RDWR)) { |
| 410 | reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs, |
| 411 | NULL, new_secondary_flags); |
| 412 | } |
| 413 | |
| 414 | if (reopen_queue) { |
Paolo Bonzini | 720150f | 2016-10-27 12:49:02 +0200 | [diff] [blame] | 415 | bdrv_reopen_multiple(bdrv_get_aio_context(bs), |
| 416 | reopen_queue, &local_err); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 417 | error_propagate(errp, local_err); |
| 418 | } |
Kevin Wolf | 1a63a90 | 2017-12-06 20:24:44 +0100 | [diff] [blame] | 419 | |
| 420 | bdrv_subtree_drained_end(s->hidden_disk->bs); |
| 421 | bdrv_subtree_drained_end(s->secondary_disk->bs); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 422 | } |
| 423 | |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 424 | static void backup_job_cleanup(BlockDriverState *bs) |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 425 | { |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 426 | BDRVReplicationState *s = bs->opaque; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 427 | BlockDriverState *top_bs; |
| 428 | |
| 429 | top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL); |
| 430 | if (!top_bs) { |
| 431 | return; |
| 432 | } |
| 433 | bdrv_op_unblock_all(top_bs, s->blocker); |
| 434 | error_free(s->blocker); |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 435 | reopen_backing_file(bs, false, NULL); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static void backup_job_completed(void *opaque, int ret) |
| 439 | { |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 440 | BlockDriverState *bs = opaque; |
| 441 | BDRVReplicationState *s = bs->opaque; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 442 | |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 443 | if (s->stage != BLOCK_REPLICATION_FAILOVER) { |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 444 | /* The backup job is cancelled unexpectedly */ |
| 445 | s->error = -EIO; |
| 446 | } |
| 447 | |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 448 | backup_job_cleanup(bs); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static bool check_top_bs(BlockDriverState *top_bs, BlockDriverState *bs) |
| 452 | { |
| 453 | BdrvChild *child; |
| 454 | |
| 455 | /* The bs itself is the top_bs */ |
| 456 | if (top_bs == bs) { |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | /* Iterate over top_bs's children */ |
| 461 | QLIST_FOREACH(child, &top_bs->children, next) { |
| 462 | if (child->bs == bs || check_top_bs(child->bs, bs)) { |
| 463 | return true; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | static void replication_start(ReplicationState *rs, ReplicationMode mode, |
| 471 | Error **errp) |
| 472 | { |
| 473 | BlockDriverState *bs = rs->opaque; |
| 474 | BDRVReplicationState *s; |
| 475 | BlockDriverState *top_bs; |
| 476 | int64_t active_length, hidden_length, disk_length; |
| 477 | AioContext *aio_context; |
| 478 | Error *local_err = NULL; |
John Snow | 111049a | 2016-11-08 01:50:38 -0500 | [diff] [blame] | 479 | BlockJob *job; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 480 | |
| 481 | aio_context = bdrv_get_aio_context(bs); |
| 482 | aio_context_acquire(aio_context); |
| 483 | s = bs->opaque; |
| 484 | |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 485 | if (s->stage != BLOCK_REPLICATION_NONE) { |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 486 | error_setg(errp, "Block replication is running or done"); |
| 487 | aio_context_release(aio_context); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | if (s->mode != mode) { |
| 492 | error_setg(errp, "The parameter mode's value is invalid, needs %d," |
| 493 | " but got %d", s->mode, mode); |
| 494 | aio_context_release(aio_context); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | switch (s->mode) { |
| 499 | case REPLICATION_MODE_PRIMARY: |
| 500 | break; |
| 501 | case REPLICATION_MODE_SECONDARY: |
| 502 | s->active_disk = bs->file; |
| 503 | if (!s->active_disk || !s->active_disk->bs || |
| 504 | !s->active_disk->bs->backing) { |
| 505 | error_setg(errp, "Active disk doesn't have backing file"); |
| 506 | aio_context_release(aio_context); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | s->hidden_disk = s->active_disk->bs->backing; |
| 511 | if (!s->hidden_disk->bs || !s->hidden_disk->bs->backing) { |
| 512 | error_setg(errp, "Hidden disk doesn't have backing file"); |
| 513 | aio_context_release(aio_context); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | s->secondary_disk = s->hidden_disk->bs->backing; |
| 518 | if (!s->secondary_disk->bs || !bdrv_has_blk(s->secondary_disk->bs)) { |
| 519 | error_setg(errp, "The secondary disk doesn't have block backend"); |
| 520 | aio_context_release(aio_context); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | /* verify the length */ |
| 525 | active_length = bdrv_getlength(s->active_disk->bs); |
| 526 | hidden_length = bdrv_getlength(s->hidden_disk->bs); |
| 527 | disk_length = bdrv_getlength(s->secondary_disk->bs); |
| 528 | if (active_length < 0 || hidden_length < 0 || disk_length < 0 || |
| 529 | active_length != hidden_length || hidden_length != disk_length) { |
| 530 | error_setg(errp, "Active disk, hidden disk, secondary disk's length" |
| 531 | " are not the same"); |
| 532 | aio_context_release(aio_context); |
| 533 | return; |
| 534 | } |
| 535 | |
Max Reitz | d470ad4 | 2017-11-10 21:31:09 +0100 | [diff] [blame] | 536 | /* Must be true, or the bdrv_getlength() calls would have failed */ |
| 537 | assert(s->active_disk->bs->drv && s->hidden_disk->bs->drv); |
| 538 | |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 539 | if (!s->active_disk->bs->drv->bdrv_make_empty || |
| 540 | !s->hidden_disk->bs->drv->bdrv_make_empty) { |
| 541 | error_setg(errp, |
| 542 | "Active disk or hidden disk doesn't support make_empty"); |
| 543 | aio_context_release(aio_context); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | /* reopen the backing file in r/w mode */ |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 548 | reopen_backing_file(bs, true, &local_err); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 549 | if (local_err) { |
| 550 | error_propagate(errp, local_err); |
| 551 | aio_context_release(aio_context); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | /* start backup job now */ |
| 556 | error_setg(&s->blocker, |
| 557 | "Block device is in use by internal backup job"); |
| 558 | |
| 559 | top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL); |
| 560 | if (!top_bs || !bdrv_is_root_node(top_bs) || |
| 561 | !check_top_bs(top_bs, bs)) { |
| 562 | error_setg(errp, "No top_bs or it is invalid"); |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 563 | reopen_backing_file(bs, false, NULL); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 564 | aio_context_release(aio_context); |
| 565 | return; |
| 566 | } |
| 567 | bdrv_op_block_all(top_bs, s->blocker); |
| 568 | bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker); |
| 569 | |
John Snow | 111049a | 2016-11-08 01:50:38 -0500 | [diff] [blame] | 570 | job = backup_job_create(NULL, s->secondary_disk->bs, s->hidden_disk->bs, |
| 571 | 0, MIRROR_SYNC_MODE_NONE, NULL, false, |
| 572 | BLOCKDEV_ON_ERROR_REPORT, |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 573 | BLOCKDEV_ON_ERROR_REPORT, JOB_INTERNAL, |
John Snow | 111049a | 2016-11-08 01:50:38 -0500 | [diff] [blame] | 574 | backup_job_completed, bs, NULL, &local_err); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 575 | if (local_err) { |
| 576 | error_propagate(errp, local_err); |
Paolo Bonzini | 8dd9006 | 2016-10-27 12:49:01 +0200 | [diff] [blame] | 577 | backup_job_cleanup(bs); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 578 | aio_context_release(aio_context); |
| 579 | return; |
| 580 | } |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 581 | job_start(&job->job); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 582 | break; |
| 583 | default: |
| 584 | aio_context_release(aio_context); |
| 585 | abort(); |
| 586 | } |
| 587 | |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 588 | s->stage = BLOCK_REPLICATION_RUNNING; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 589 | |
| 590 | if (s->mode == REPLICATION_MODE_SECONDARY) { |
| 591 | secondary_do_checkpoint(s, errp); |
| 592 | } |
| 593 | |
| 594 | s->error = 0; |
| 595 | aio_context_release(aio_context); |
| 596 | } |
| 597 | |
| 598 | static void replication_do_checkpoint(ReplicationState *rs, 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->mode == REPLICATION_MODE_SECONDARY) { |
| 609 | secondary_do_checkpoint(s, errp); |
| 610 | } |
| 611 | aio_context_release(aio_context); |
| 612 | } |
| 613 | |
| 614 | static void replication_get_error(ReplicationState *rs, Error **errp) |
| 615 | { |
| 616 | BlockDriverState *bs = rs->opaque; |
| 617 | BDRVReplicationState *s; |
| 618 | AioContext *aio_context; |
| 619 | |
| 620 | aio_context = bdrv_get_aio_context(bs); |
| 621 | aio_context_acquire(aio_context); |
| 622 | s = bs->opaque; |
| 623 | |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 624 | if (s->stage != BLOCK_REPLICATION_RUNNING) { |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 625 | error_setg(errp, "Block replication is not running"); |
| 626 | aio_context_release(aio_context); |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | if (s->error) { |
| 631 | error_setg(errp, "I/O error occurred"); |
| 632 | aio_context_release(aio_context); |
| 633 | return; |
| 634 | } |
| 635 | aio_context_release(aio_context); |
| 636 | } |
| 637 | |
| 638 | static void replication_done(void *opaque, int ret) |
| 639 | { |
| 640 | BlockDriverState *bs = opaque; |
| 641 | BDRVReplicationState *s = bs->opaque; |
| 642 | |
| 643 | if (ret == 0) { |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 644 | s->stage = BLOCK_REPLICATION_DONE; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 645 | |
| 646 | /* refresh top bs's filename */ |
| 647 | bdrv_refresh_filename(bs); |
| 648 | s->active_disk = NULL; |
| 649 | s->secondary_disk = NULL; |
| 650 | s->hidden_disk = NULL; |
| 651 | s->error = 0; |
| 652 | } else { |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 653 | s->stage = BLOCK_REPLICATION_FAILOVER_FAILED; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 654 | s->error = -EIO; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | static void replication_stop(ReplicationState *rs, bool failover, Error **errp) |
| 659 | { |
| 660 | BlockDriverState *bs = rs->opaque; |
| 661 | BDRVReplicationState *s; |
| 662 | AioContext *aio_context; |
| 663 | |
| 664 | aio_context = bdrv_get_aio_context(bs); |
| 665 | aio_context_acquire(aio_context); |
| 666 | s = bs->opaque; |
| 667 | |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 668 | if (s->stage != BLOCK_REPLICATION_RUNNING) { |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 669 | error_setg(errp, "Block replication is not running"); |
| 670 | aio_context_release(aio_context); |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | switch (s->mode) { |
| 675 | case REPLICATION_MODE_PRIMARY: |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 676 | s->stage = BLOCK_REPLICATION_DONE; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 677 | s->error = 0; |
| 678 | break; |
| 679 | case REPLICATION_MODE_SECONDARY: |
| 680 | /* |
| 681 | * This BDS will be closed, and the job should be completed |
| 682 | * before the BDS is closed, because we will access hidden |
| 683 | * disk, secondary disk in backup_job_completed(). |
| 684 | */ |
| 685 | if (s->secondary_disk->bs->job) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 686 | job_cancel_sync(&s->secondary_disk->bs->job->job); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (!failover) { |
| 690 | secondary_do_checkpoint(s, errp); |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 691 | s->stage = BLOCK_REPLICATION_DONE; |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 692 | aio_context_release(aio_context); |
| 693 | return; |
| 694 | } |
| 695 | |
Fam Zheng | 3c76c60 | 2017-03-17 10:17:39 +0800 | [diff] [blame] | 696 | s->stage = BLOCK_REPLICATION_FAILOVER; |
John Snow | 47970df | 2016-10-27 12:06:57 -0400 | [diff] [blame] | 697 | commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs, |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 698 | JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 699 | NULL, replication_done, bs, true, errp); |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 700 | break; |
| 701 | default: |
| 702 | aio_context_release(aio_context); |
| 703 | abort(); |
| 704 | } |
| 705 | aio_context_release(aio_context); |
| 706 | } |
| 707 | |
| 708 | BlockDriver bdrv_replication = { |
| 709 | .format_name = "replication", |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 710 | .instance_size = sizeof(BDRVReplicationState), |
| 711 | |
| 712 | .bdrv_open = replication_open, |
| 713 | .bdrv_close = replication_close, |
Changlong Xie | 37a9051 | 2017-03-14 19:46:52 +0800 | [diff] [blame] | 714 | .bdrv_child_perm = replication_child_perm, |
Wen Congyang | 29ff789 | 2016-07-27 15:01:50 +0800 | [diff] [blame] | 715 | |
| 716 | .bdrv_getlength = replication_getlength, |
| 717 | .bdrv_co_readv = replication_co_readv, |
| 718 | .bdrv_co_writev = replication_co_writev, |
| 719 | |
| 720 | .is_filter = true, |
| 721 | .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter, |
| 722 | |
| 723 | .has_variable_length = true, |
| 724 | }; |
| 725 | |
| 726 | static void bdrv_replication_init(void) |
| 727 | { |
| 728 | bdrv_register(&bdrv_replication); |
| 729 | } |
| 730 | |
| 731 | block_init(bdrv_replication_init); |