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 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 16 | #include "trace.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 17 | #include "block/block_int.h" |
| 18 | #include "block/blockjob.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 19 | #include "qapi/error.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 20 | #include "qapi/qmp/qerror.h" |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 21 | #include "qemu/ratelimit.h" |
Max Reitz | 373340b | 2015-10-19 17:53:22 +0200 | [diff] [blame] | 22 | #include "sysemu/block-backend.h" |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 23 | |
| 24 | enum { |
| 25 | /* |
| 26 | * Size of data buffer for populating the image file. This should be large |
| 27 | * enough to process multiple clusters in a single call, so that populating |
| 28 | * contiguous regions of the image is efficient. |
| 29 | */ |
| 30 | COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */ |
| 31 | }; |
| 32 | |
| 33 | #define SLICE_TIME 100000000ULL /* ns */ |
| 34 | |
| 35 | typedef struct CommitBlockJob { |
| 36 | BlockJob common; |
| 37 | RateLimit limit; |
| 38 | BlockDriverState *active; |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 39 | BlockBackend *top; |
| 40 | BlockBackend *base; |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 41 | BlockdevOnError on_error; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 42 | int base_flags; |
| 43 | int orig_overlay_flags; |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 44 | char *backing_file_str; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 45 | } CommitBlockJob; |
| 46 | |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 47 | static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base, |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 48 | int64_t sector_num, int nb_sectors, |
| 49 | void *buf) |
| 50 | { |
| 51 | int ret = 0; |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 52 | QEMUIOVector qiov; |
| 53 | struct iovec iov = { |
| 54 | .iov_base = buf, |
| 55 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
| 56 | }; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 57 | |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 58 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 59 | |
| 60 | ret = blk_co_preadv(bs, sector_num * BDRV_SECTOR_SIZE, |
| 61 | qiov.size, &qiov, 0); |
| 62 | if (ret < 0) { |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 63 | return ret; |
| 64 | } |
| 65 | |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 66 | ret = blk_co_pwritev(base, sector_num * BDRV_SECTOR_SIZE, |
| 67 | qiov.size, &qiov, 0); |
| 68 | if (ret < 0) { |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 75 | typedef struct { |
| 76 | int ret; |
| 77 | } CommitCompleteData; |
| 78 | |
| 79 | static void commit_complete(BlockJob *job, void *opaque) |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 80 | { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 81 | CommitBlockJob *s = container_of(job, CommitBlockJob, common); |
| 82 | CommitCompleteData *data = opaque; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 83 | BlockDriverState *active = s->active; |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 84 | BlockDriverState *top = blk_bs(s->top); |
| 85 | BlockDriverState *base = blk_bs(s->base); |
Alberto Garcia | 4d6f8cb | 2016-08-21 23:36:03 -0400 | [diff] [blame] | 86 | BlockDriverState *overlay_bs = bdrv_find_overlay(active, top); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 87 | int ret = data->ret; |
| 88 | |
| 89 | if (!block_job_is_cancelled(&s->common) && ret == 0) { |
| 90 | /* success */ |
| 91 | ret = bdrv_drop_intermediate(active, top, base, s->backing_file_str); |
| 92 | } |
| 93 | |
| 94 | /* restore base open flags here if appropriate (e.g., change the base back |
| 95 | * to r/o). These reopens do not need to be atomic, since we won't abort |
| 96 | * even on failure here */ |
| 97 | if (s->base_flags != bdrv_get_flags(base)) { |
| 98 | bdrv_reopen(base, s->base_flags, NULL); |
| 99 | } |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 100 | if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) { |
| 101 | bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL); |
| 102 | } |
| 103 | g_free(s->backing_file_str); |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 104 | blk_unref(s->top); |
| 105 | blk_unref(s->base); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 106 | block_job_completed(&s->common, ret); |
| 107 | g_free(data); |
| 108 | } |
| 109 | |
| 110 | static void coroutine_fn commit_run(void *opaque) |
| 111 | { |
| 112 | CommitBlockJob *s = opaque; |
| 113 | CommitCompleteData *data; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 114 | int64_t sector_num, end; |
Sascha Silbe | f14a39c | 2016-06-28 17:28:41 +0200 | [diff] [blame] | 115 | uint64_t delay_ns = 0; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 116 | int ret = 0; |
| 117 | int n = 0; |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 118 | void *buf = NULL; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 119 | int bytes_written = 0; |
| 120 | int64_t base_len; |
| 121 | |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 122 | ret = s->common.len = blk_getlength(s->top); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 123 | |
| 124 | |
| 125 | if (s->common.len < 0) { |
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 | |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 129 | ret = base_len = blk_getlength(s->base); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 130 | if (base_len < 0) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 131 | goto out; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | if (base_len < s->common.len) { |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 135 | ret = blk_truncate(s->base, s->common.len); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 136 | if (ret) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 137 | goto out; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 141 | end = s->common.len >> BDRV_SECTOR_BITS; |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 142 | buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 143 | |
| 144 | for (sector_num = 0; sector_num < end; sector_num += n) { |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 145 | bool copy; |
| 146 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 147 | /* 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] | 148 | * with no pending I/O here so that bdrv_drain_all() returns. |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 149 | */ |
Alex Bligh | 7483d1e | 2013-08-21 16:03:05 +0100 | [diff] [blame] | 150 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 151 | if (block_job_is_cancelled(&s->common)) { |
| 152 | break; |
| 153 | } |
| 154 | /* Copy if allocated above the base */ |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 155 | ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base), |
| 156 | sector_num, |
Paolo Bonzini | 4f57863 | 2013-09-04 19:00:24 +0200 | [diff] [blame] | 157 | COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE, |
| 158 | &n); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 159 | copy = (ret == 1); |
| 160 | trace_commit_one_iteration(s, sector_num, n, ret); |
| 161 | if (copy) { |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 162 | ret = commit_populate(s->top, s->base, sector_num, n, buf); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 163 | bytes_written += n * BDRV_SECTOR_SIZE; |
| 164 | } |
| 165 | if (ret < 0) { |
Kevin Wolf | 1e8fb7f | 2016-06-29 17:38:57 +0200 | [diff] [blame] | 166 | BlockErrorAction action = |
| 167 | block_job_error_action(&s->common, false, s->on_error, -ret); |
| 168 | if (action == BLOCK_ERROR_ACTION_REPORT) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 169 | goto out; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 170 | } else { |
| 171 | n = 0; |
| 172 | continue; |
| 173 | } |
| 174 | } |
| 175 | /* Publish progress */ |
| 176 | s->common.offset += n * BDRV_SECTOR_SIZE; |
Sascha Silbe | f14a39c | 2016-06-28 17:28:41 +0200 | [diff] [blame] | 177 | |
| 178 | if (copy && s->common.speed) { |
| 179 | delay_ns = ratelimit_calculate_delay(&s->limit, n); |
| 180 | } |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | ret = 0; |
| 184 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 185 | out: |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 186 | qemu_vfree(buf); |
| 187 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 188 | data = g_malloc(sizeof(*data)); |
| 189 | data->ret = ret; |
| 190 | block_job_defer_to_main_loop(&s->common, commit_complete, data); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp) |
| 194 | { |
| 195 | CommitBlockJob *s = container_of(job, CommitBlockJob, common); |
| 196 | |
| 197 | if (speed < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 198 | error_setg(errp, QERR_INVALID_PARAMETER, "speed"); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); |
| 202 | } |
| 203 | |
Fam Zheng | 3fc4b10 | 2013-10-08 17:29:38 +0800 | [diff] [blame] | 204 | static const BlockJobDriver commit_job_driver = { |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 205 | .instance_size = sizeof(CommitBlockJob), |
Fam Zheng | 79e14bf | 2013-10-08 17:29:40 +0800 | [diff] [blame] | 206 | .job_type = BLOCK_JOB_TYPE_COMMIT, |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 207 | .set_speed = commit_set_speed, |
| 208 | }; |
| 209 | |
Alberto Garcia | fd62c60 | 2016-07-05 17:29:00 +0300 | [diff] [blame] | 210 | void commit_start(const char *job_id, BlockDriverState *bs, |
| 211 | BlockDriverState *base, BlockDriverState *top, int64_t speed, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 212 | BlockdevOnError on_error, BlockCompletionFunc *cb, |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 213 | void *opaque, const char *backing_file_str, Error **errp) |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 214 | { |
| 215 | CommitBlockJob *s; |
| 216 | BlockReopenQueue *reopen_queue = NULL; |
| 217 | int orig_overlay_flags; |
| 218 | int orig_base_flags; |
Alberto Garcia | 3e4c512 | 2016-10-28 10:08:08 +0300 | [diff] [blame^] | 219 | BlockDriverState *iter; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 220 | BlockDriverState *overlay_bs; |
| 221 | Error *local_err = NULL; |
| 222 | |
Fam Zheng | 18da7f9 | 2013-12-16 14:45:33 +0800 | [diff] [blame] | 223 | assert(top != bs); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 224 | if (top == base) { |
| 225 | error_setg(errp, "Invalid files for merge: top and base are the same"); |
| 226 | return; |
| 227 | } |
| 228 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 229 | overlay_bs = bdrv_find_overlay(bs, top); |
| 230 | |
| 231 | if (overlay_bs == NULL) { |
| 232 | error_setg(errp, "Could not find overlay image for %s:", top->filename); |
| 233 | return; |
| 234 | } |
| 235 | |
Alberto Garcia | fd62c60 | 2016-07-05 17:29:00 +0300 | [diff] [blame] | 236 | s = block_job_create(job_id, &commit_job_driver, bs, speed, |
| 237 | cb, opaque, errp); |
Alberto Garcia | 834fe28 | 2016-05-27 12:53:39 +0200 | [diff] [blame] | 238 | if (!s) { |
| 239 | return; |
| 240 | } |
| 241 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 242 | orig_base_flags = bdrv_get_flags(base); |
| 243 | orig_overlay_flags = bdrv_get_flags(overlay_bs); |
| 244 | |
| 245 | /* convert base & overlay_bs to r/w, if necessary */ |
Alberto Garcia | 3db2bd5 | 2015-10-28 15:43:49 +0200 | [diff] [blame] | 246 | if (!(orig_base_flags & BDRV_O_RDWR)) { |
| 247 | reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL, |
| 248 | orig_base_flags | BDRV_O_RDWR); |
| 249 | } |
Alberto Garcia | 0fe282b | 2016-09-15 17:53:04 +0300 | [diff] [blame] | 250 | if (!(orig_overlay_flags & BDRV_O_RDWR)) { |
| 251 | reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL, |
| 252 | orig_overlay_flags | BDRV_O_RDWR); |
| 253 | } |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 254 | if (reopen_queue) { |
Paolo Bonzini | 720150f | 2016-10-27 12:49:02 +0200 | [diff] [blame] | 255 | bdrv_reopen_multiple(bdrv_get_aio_context(bs), reopen_queue, &local_err); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 256 | if (local_err != NULL) { |
| 257 | error_propagate(errp, local_err); |
Alberto Garcia | 834fe28 | 2016-05-27 12:53:39 +0200 | [diff] [blame] | 258 | block_job_unref(&s->common); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | |
Alberto Garcia | 3e4c512 | 2016-10-28 10:08:08 +0300 | [diff] [blame^] | 264 | /* Block all nodes between top and base, because they will |
| 265 | * disappear from the chain after this operation. */ |
| 266 | assert(bdrv_chain_contains(top, base)); |
| 267 | for (iter = top; iter != backing_bs(base); iter = backing_bs(iter)) { |
| 268 | block_job_add_bdrv(&s->common, iter); |
| 269 | } |
| 270 | /* overlay_bs must be blocked because it needs to be modified to |
| 271 | * update the backing image string, but if it's the root node then |
| 272 | * don't block it again */ |
| 273 | if (bs != overlay_bs) { |
| 274 | block_job_add_bdrv(&s->common, overlay_bs); |
| 275 | } |
| 276 | |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 277 | s->base = blk_new(); |
| 278 | blk_insert_bs(s->base, base); |
| 279 | |
| 280 | s->top = blk_new(); |
| 281 | blk_insert_bs(s->top, top); |
| 282 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 283 | s->active = bs; |
| 284 | |
| 285 | s->base_flags = orig_base_flags; |
| 286 | s->orig_overlay_flags = orig_overlay_flags; |
| 287 | |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 288 | s->backing_file_str = g_strdup(backing_file_str); |
| 289 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 290 | s->on_error = on_error; |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 291 | s->common.co = qemu_coroutine_create(commit_run, s); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 292 | |
| 293 | trace_commit_start(bs, base, top, s, s->common.co, opaque); |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 294 | qemu_coroutine_enter(s->common.co); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 295 | } |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 296 | |
| 297 | |
| 298 | #define COMMIT_BUF_SECTORS 2048 |
| 299 | |
| 300 | /* commit COW file into the raw image */ |
| 301 | int bdrv_commit(BlockDriverState *bs) |
| 302 | { |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 303 | BlockBackend *src, *backing; |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 304 | BlockDriver *drv = bs->drv; |
| 305 | int64_t sector, total_sectors, length, backing_length; |
| 306 | int n, ro, open_flags; |
| 307 | int ret = 0; |
| 308 | uint8_t *buf = NULL; |
| 309 | |
| 310 | if (!drv) |
| 311 | return -ENOMEDIUM; |
| 312 | |
| 313 | if (!bs->backing) { |
| 314 | return -ENOTSUP; |
| 315 | } |
| 316 | |
| 317 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) || |
| 318 | bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) { |
| 319 | return -EBUSY; |
| 320 | } |
| 321 | |
| 322 | ro = bs->backing->bs->read_only; |
| 323 | open_flags = bs->backing->bs->open_flags; |
| 324 | |
| 325 | if (ro) { |
| 326 | if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) { |
| 327 | return -EACCES; |
| 328 | } |
| 329 | } |
| 330 | |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 331 | src = blk_new(); |
| 332 | blk_insert_bs(src, bs); |
| 333 | |
| 334 | backing = blk_new(); |
| 335 | blk_insert_bs(backing, bs->backing->bs); |
| 336 | |
| 337 | length = blk_getlength(src); |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 338 | if (length < 0) { |
| 339 | ret = length; |
| 340 | goto ro_cleanup; |
| 341 | } |
| 342 | |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 343 | backing_length = blk_getlength(backing); |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 344 | if (backing_length < 0) { |
| 345 | ret = backing_length; |
| 346 | goto ro_cleanup; |
| 347 | } |
| 348 | |
| 349 | /* If our top snapshot is larger than the backing file image, |
| 350 | * grow the backing file image if possible. If not possible, |
| 351 | * we must return an error */ |
| 352 | if (length > backing_length) { |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 353 | ret = blk_truncate(backing, length); |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 354 | if (ret < 0) { |
| 355 | goto ro_cleanup; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | total_sectors = length >> BDRV_SECTOR_BITS; |
| 360 | |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 361 | /* blk_try_blockalign() for src will choose an alignment that works for |
| 362 | * backing as well, so no need to compare the alignment manually. */ |
| 363 | buf = blk_try_blockalign(src, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 364 | if (buf == NULL) { |
| 365 | ret = -ENOMEM; |
| 366 | goto ro_cleanup; |
| 367 | } |
| 368 | |
| 369 | for (sector = 0; sector < total_sectors; sector += n) { |
| 370 | ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); |
| 371 | if (ret < 0) { |
| 372 | goto ro_cleanup; |
| 373 | } |
| 374 | if (ret) { |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 375 | ret = blk_pread(src, sector * BDRV_SECTOR_SIZE, buf, |
| 376 | n * BDRV_SECTOR_SIZE); |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 377 | if (ret < 0) { |
| 378 | goto ro_cleanup; |
| 379 | } |
| 380 | |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 381 | ret = blk_pwrite(backing, sector * BDRV_SECTOR_SIZE, buf, |
| 382 | n * BDRV_SECTOR_SIZE, 0); |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 383 | if (ret < 0) { |
| 384 | goto ro_cleanup; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (drv->bdrv_make_empty) { |
| 390 | ret = drv->bdrv_make_empty(bs); |
| 391 | if (ret < 0) { |
| 392 | goto ro_cleanup; |
| 393 | } |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 394 | blk_flush(src); |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Make sure all data we wrote to the backing device is actually |
| 399 | * stable on disk. |
| 400 | */ |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 401 | blk_flush(backing); |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 402 | |
| 403 | ret = 0; |
| 404 | ro_cleanup: |
| 405 | qemu_vfree(buf); |
| 406 | |
Kevin Wolf | f8e2bd5 | 2016-05-30 16:29:47 +0200 | [diff] [blame] | 407 | blk_unref(src); |
| 408 | blk_unref(backing); |
| 409 | |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame] | 410 | if (ro) { |
| 411 | /* ignoring error return here */ |
| 412 | bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL); |
| 413 | } |
| 414 | |
| 415 | return ret; |
| 416 | } |