Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Live block commit |
| 3 | * |
| 4 | * Copyright Red Hat, Inc. 2012 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Jeff Cody <jcody@redhat.com> |
| 8 | * Based on stream.c by Stefan Hajnoczi |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 11 | * See the COPYING.LIB file in the top-level directory. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include "trace.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 16 | #include "block/block_int.h" |
| 17 | #include "block/blockjob.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 18 | #include "qapi/qmp/qerror.h" |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 19 | #include "qemu/ratelimit.h" |
| 20 | |
| 21 | enum { |
| 22 | /* |
| 23 | * Size of data buffer for populating the image file. This should be large |
| 24 | * enough to process multiple clusters in a single call, so that populating |
| 25 | * contiguous regions of the image is efficient. |
| 26 | */ |
| 27 | COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */ |
| 28 | }; |
| 29 | |
| 30 | #define SLICE_TIME 100000000ULL /* ns */ |
| 31 | |
| 32 | typedef struct CommitBlockJob { |
| 33 | BlockJob common; |
| 34 | RateLimit limit; |
| 35 | BlockDriverState *active; |
| 36 | BlockDriverState *top; |
| 37 | BlockDriverState *base; |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 38 | BlockdevOnError on_error; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 39 | int base_flags; |
| 40 | int orig_overlay_flags; |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 41 | char *backing_file_str; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 42 | } CommitBlockJob; |
| 43 | |
| 44 | static int coroutine_fn commit_populate(BlockDriverState *bs, |
| 45 | BlockDriverState *base, |
| 46 | int64_t sector_num, int nb_sectors, |
| 47 | void *buf) |
| 48 | { |
| 49 | int ret = 0; |
| 50 | |
| 51 | ret = bdrv_read(bs, sector_num, buf, nb_sectors); |
| 52 | if (ret) { |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | ret = bdrv_write(base, sector_num, buf, nb_sectors); |
| 57 | if (ret) { |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 64 | typedef struct { |
| 65 | int ret; |
| 66 | } CommitCompleteData; |
| 67 | |
| 68 | static void commit_complete(BlockJob *job, void *opaque) |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 69 | { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 70 | CommitBlockJob *s = container_of(job, CommitBlockJob, common); |
| 71 | CommitCompleteData *data = opaque; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 72 | BlockDriverState *active = s->active; |
| 73 | BlockDriverState *top = s->top; |
| 74 | BlockDriverState *base = s->base; |
Jeff Cody | 6d75911 | 2013-01-15 10:47:24 -0500 | [diff] [blame] | 75 | BlockDriverState *overlay_bs; |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 76 | int ret = data->ret; |
| 77 | |
| 78 | if (!block_job_is_cancelled(&s->common) && ret == 0) { |
| 79 | /* success */ |
| 80 | ret = bdrv_drop_intermediate(active, top, base, s->backing_file_str); |
| 81 | } |
| 82 | |
| 83 | /* restore base open flags here if appropriate (e.g., change the base back |
| 84 | * to r/o). These reopens do not need to be atomic, since we won't abort |
| 85 | * even on failure here */ |
| 86 | if (s->base_flags != bdrv_get_flags(base)) { |
| 87 | bdrv_reopen(base, s->base_flags, NULL); |
| 88 | } |
| 89 | overlay_bs = bdrv_find_overlay(active, top); |
| 90 | if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) { |
| 91 | bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL); |
| 92 | } |
| 93 | g_free(s->backing_file_str); |
| 94 | block_job_completed(&s->common, ret); |
| 95 | g_free(data); |
| 96 | } |
| 97 | |
| 98 | static void coroutine_fn commit_run(void *opaque) |
| 99 | { |
| 100 | CommitBlockJob *s = opaque; |
| 101 | CommitCompleteData *data; |
| 102 | BlockDriverState *top = s->top; |
| 103 | BlockDriverState *base = s->base; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 104 | int64_t sector_num, end; |
| 105 | int ret = 0; |
| 106 | int n = 0; |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 107 | void *buf = NULL; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 108 | int bytes_written = 0; |
| 109 | int64_t base_len; |
| 110 | |
| 111 | ret = s->common.len = bdrv_getlength(top); |
| 112 | |
| 113 | |
| 114 | if (s->common.len < 0) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 115 | goto out; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | ret = base_len = bdrv_getlength(base); |
| 119 | if (base_len < 0) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 120 | goto out; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (base_len < s->common.len) { |
| 124 | ret = bdrv_truncate(base, s->common.len); |
| 125 | if (ret) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 126 | goto out; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 130 | end = s->common.len >> BDRV_SECTOR_BITS; |
| 131 | buf = qemu_blockalign(top, COMMIT_BUFFER_SIZE); |
| 132 | |
| 133 | for (sector_num = 0; sector_num < end; sector_num += n) { |
| 134 | uint64_t delay_ns = 0; |
| 135 | bool copy; |
| 136 | |
| 137 | wait: |
| 138 | /* Note that even when no rate limit is applied we need to yield |
Kevin Wolf | c57b665 | 2012-11-13 16:35:13 +0100 | [diff] [blame] | 139 | * with no pending I/O here so that bdrv_drain_all() returns. |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 140 | */ |
Alex Bligh | 7483d1e | 2013-08-21 16:03:05 +0100 | [diff] [blame] | 141 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 142 | if (block_job_is_cancelled(&s->common)) { |
| 143 | break; |
| 144 | } |
| 145 | /* Copy if allocated above the base */ |
Paolo Bonzini | 4f57863 | 2013-09-04 19:00:24 +0200 | [diff] [blame] | 146 | ret = bdrv_is_allocated_above(top, base, sector_num, |
| 147 | COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE, |
| 148 | &n); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 149 | copy = (ret == 1); |
| 150 | trace_commit_one_iteration(s, sector_num, n, ret); |
| 151 | if (copy) { |
| 152 | if (s->common.speed) { |
| 153 | delay_ns = ratelimit_calculate_delay(&s->limit, n); |
| 154 | if (delay_ns > 0) { |
| 155 | goto wait; |
| 156 | } |
| 157 | } |
| 158 | ret = commit_populate(top, base, sector_num, n, buf); |
| 159 | bytes_written += n * BDRV_SECTOR_SIZE; |
| 160 | } |
| 161 | if (ret < 0) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 162 | if (s->on_error == BLOCKDEV_ON_ERROR_STOP || |
| 163 | s->on_error == BLOCKDEV_ON_ERROR_REPORT|| |
| 164 | (s->on_error == BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC)) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 165 | goto out; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 166 | } else { |
| 167 | n = 0; |
| 168 | continue; |
| 169 | } |
| 170 | } |
| 171 | /* Publish progress */ |
| 172 | s->common.offset += n * BDRV_SECTOR_SIZE; |
| 173 | } |
| 174 | |
| 175 | ret = 0; |
| 176 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 177 | out: |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 178 | qemu_vfree(buf); |
| 179 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 180 | data = g_malloc(sizeof(*data)); |
| 181 | data->ret = ret; |
| 182 | block_job_defer_to_main_loop(&s->common, commit_complete, data); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp) |
| 186 | { |
| 187 | CommitBlockJob *s = container_of(job, CommitBlockJob, common); |
| 188 | |
| 189 | if (speed < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 190 | error_setg(errp, QERR_INVALID_PARAMETER, "speed"); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); |
| 194 | } |
| 195 | |
Fam Zheng | 3fc4b10 | 2013-10-08 17:29:38 +0800 | [diff] [blame] | 196 | static const BlockJobDriver commit_job_driver = { |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 197 | .instance_size = sizeof(CommitBlockJob), |
Fam Zheng | 79e14bf | 2013-10-08 17:29:40 +0800 | [diff] [blame] | 198 | .job_type = BLOCK_JOB_TYPE_COMMIT, |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 199 | .set_speed = commit_set_speed, |
| 200 | }; |
| 201 | |
| 202 | void commit_start(BlockDriverState *bs, BlockDriverState *base, |
| 203 | BlockDriverState *top, int64_t speed, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 204 | BlockdevOnError on_error, BlockCompletionFunc *cb, |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 205 | void *opaque, const char *backing_file_str, Error **errp) |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 206 | { |
| 207 | CommitBlockJob *s; |
| 208 | BlockReopenQueue *reopen_queue = NULL; |
| 209 | int orig_overlay_flags; |
| 210 | int orig_base_flags; |
| 211 | BlockDriverState *overlay_bs; |
| 212 | Error *local_err = NULL; |
| 213 | |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 214 | if ((on_error == BLOCKDEV_ON_ERROR_STOP || |
| 215 | on_error == BLOCKDEV_ON_ERROR_ENOSPC) && |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 216 | !bdrv_iostatus_is_enabled(bs)) { |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 217 | error_setg(errp, "Invalid parameter combination"); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | |
Fam Zheng | 18da7f9 | 2013-12-16 14:45:33 +0800 | [diff] [blame] | 221 | assert(top != bs); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 222 | if (top == base) { |
| 223 | error_setg(errp, "Invalid files for merge: top and base are the same"); |
| 224 | return; |
| 225 | } |
| 226 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 227 | overlay_bs = bdrv_find_overlay(bs, top); |
| 228 | |
| 229 | if (overlay_bs == NULL) { |
| 230 | error_setg(errp, "Could not find overlay image for %s:", top->filename); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | orig_base_flags = bdrv_get_flags(base); |
| 235 | orig_overlay_flags = bdrv_get_flags(overlay_bs); |
| 236 | |
| 237 | /* convert base & overlay_bs to r/w, if necessary */ |
| 238 | if (!(orig_base_flags & BDRV_O_RDWR)) { |
| 239 | reopen_queue = bdrv_reopen_queue(reopen_queue, base, |
| 240 | orig_base_flags | BDRV_O_RDWR); |
| 241 | } |
| 242 | if (!(orig_overlay_flags & BDRV_O_RDWR)) { |
| 243 | reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, |
| 244 | orig_overlay_flags | BDRV_O_RDWR); |
| 245 | } |
| 246 | if (reopen_queue) { |
| 247 | bdrv_reopen_multiple(reopen_queue, &local_err); |
| 248 | if (local_err != NULL) { |
| 249 | error_propagate(errp, local_err); |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | |
Fam Zheng | 3fc4b10 | 2013-10-08 17:29:38 +0800 | [diff] [blame] | 255 | s = block_job_create(&commit_job_driver, bs, speed, cb, opaque, errp); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 256 | if (!s) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | s->base = base; |
| 261 | s->top = top; |
| 262 | s->active = bs; |
| 263 | |
| 264 | s->base_flags = orig_base_flags; |
| 265 | s->orig_overlay_flags = orig_overlay_flags; |
| 266 | |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 267 | s->backing_file_str = g_strdup(backing_file_str); |
| 268 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 269 | s->on_error = on_error; |
| 270 | s->common.co = qemu_coroutine_create(commit_run); |
| 271 | |
| 272 | trace_commit_start(bs, base, top, s, s->common.co, opaque); |
| 273 | qemu_coroutine_enter(s->common.co, s); |
| 274 | } |