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); |
Jeff Cody | 6d75911 | 2013-01-15 10:47:24 -0500 | [diff] [blame] | 86 | BlockDriverState *overlay_bs; |
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 | } |
| 100 | overlay_bs = bdrv_find_overlay(active, top); |
| 101 | if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) { |
| 102 | bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL); |
| 103 | } |
| 104 | g_free(s->backing_file_str); |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 105 | blk_unref(s->top); |
| 106 | blk_unref(s->base); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 107 | block_job_completed(&s->common, ret); |
| 108 | g_free(data); |
| 109 | } |
| 110 | |
| 111 | static void coroutine_fn commit_run(void *opaque) |
| 112 | { |
| 113 | CommitBlockJob *s = opaque; |
| 114 | CommitCompleteData *data; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 115 | int64_t sector_num, end; |
| 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) { |
| 145 | uint64_t delay_ns = 0; |
| 146 | bool copy; |
| 147 | |
| 148 | wait: |
| 149 | /* 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] | 150 | * with no pending I/O here so that bdrv_drain_all() returns. |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 151 | */ |
Alex Bligh | 7483d1e | 2013-08-21 16:03:05 +0100 | [diff] [blame] | 152 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 153 | if (block_job_is_cancelled(&s->common)) { |
| 154 | break; |
| 155 | } |
| 156 | /* Copy if allocated above the base */ |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 157 | ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base), |
| 158 | sector_num, |
Paolo Bonzini | 4f57863 | 2013-09-04 19:00:24 +0200 | [diff] [blame] | 159 | COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE, |
| 160 | &n); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 161 | copy = (ret == 1); |
| 162 | trace_commit_one_iteration(s, sector_num, n, ret); |
| 163 | if (copy) { |
| 164 | if (s->common.speed) { |
| 165 | delay_ns = ratelimit_calculate_delay(&s->limit, n); |
| 166 | if (delay_ns > 0) { |
| 167 | goto wait; |
| 168 | } |
| 169 | } |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 170 | ret = commit_populate(s->top, s->base, sector_num, n, buf); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 171 | bytes_written += n * BDRV_SECTOR_SIZE; |
| 172 | } |
| 173 | if (ret < 0) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 174 | if (s->on_error == BLOCKDEV_ON_ERROR_STOP || |
| 175 | s->on_error == BLOCKDEV_ON_ERROR_REPORT|| |
| 176 | (s->on_error == BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC)) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 177 | goto out; |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 178 | } else { |
| 179 | n = 0; |
| 180 | continue; |
| 181 | } |
| 182 | } |
| 183 | /* Publish progress */ |
| 184 | s->common.offset += n * BDRV_SECTOR_SIZE; |
| 185 | } |
| 186 | |
| 187 | ret = 0; |
| 188 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 189 | out: |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 190 | qemu_vfree(buf); |
| 191 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 192 | data = g_malloc(sizeof(*data)); |
| 193 | data->ret = ret; |
| 194 | block_job_defer_to_main_loop(&s->common, commit_complete, data); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp) |
| 198 | { |
| 199 | CommitBlockJob *s = container_of(job, CommitBlockJob, common); |
| 200 | |
| 201 | if (speed < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 202 | error_setg(errp, QERR_INVALID_PARAMETER, "speed"); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); |
| 206 | } |
| 207 | |
Fam Zheng | 3fc4b10 | 2013-10-08 17:29:38 +0800 | [diff] [blame] | 208 | static const BlockJobDriver commit_job_driver = { |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 209 | .instance_size = sizeof(CommitBlockJob), |
Fam Zheng | 79e14bf | 2013-10-08 17:29:40 +0800 | [diff] [blame] | 210 | .job_type = BLOCK_JOB_TYPE_COMMIT, |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 211 | .set_speed = commit_set_speed, |
| 212 | }; |
| 213 | |
| 214 | void commit_start(BlockDriverState *bs, BlockDriverState *base, |
| 215 | BlockDriverState *top, int64_t speed, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 216 | BlockdevOnError on_error, BlockCompletionFunc *cb, |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 217 | void *opaque, const char *backing_file_str, Error **errp) |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 218 | { |
| 219 | CommitBlockJob *s; |
| 220 | BlockReopenQueue *reopen_queue = NULL; |
| 221 | int orig_overlay_flags; |
| 222 | int orig_base_flags; |
| 223 | BlockDriverState *overlay_bs; |
| 224 | Error *local_err = NULL; |
| 225 | |
Fam Zheng | 18da7f9 | 2013-12-16 14:45:33 +0800 | [diff] [blame] | 226 | assert(top != bs); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 227 | if (top == base) { |
| 228 | error_setg(errp, "Invalid files for merge: top and base are the same"); |
| 229 | return; |
| 230 | } |
| 231 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 232 | overlay_bs = bdrv_find_overlay(bs, top); |
| 233 | |
| 234 | if (overlay_bs == NULL) { |
| 235 | error_setg(errp, "Could not find overlay image for %s:", top->filename); |
| 236 | return; |
| 237 | } |
| 238 | |
Alberto Garcia | 834fe28 | 2016-05-27 12:53:39 +0200 | [diff] [blame] | 239 | s = block_job_create(&commit_job_driver, bs, speed, cb, opaque, errp); |
| 240 | if (!s) { |
| 241 | return; |
| 242 | } |
| 243 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 244 | orig_base_flags = bdrv_get_flags(base); |
| 245 | orig_overlay_flags = bdrv_get_flags(overlay_bs); |
| 246 | |
| 247 | /* convert base & overlay_bs to r/w, if necessary */ |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 248 | if (!(orig_overlay_flags & BDRV_O_RDWR)) { |
Kevin Wolf | 4d2cb09 | 2015-04-10 17:50:50 +0200 | [diff] [blame] | 249 | reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL, |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 250 | orig_overlay_flags | BDRV_O_RDWR); |
| 251 | } |
Alberto Garcia | 3db2bd5 | 2015-10-28 15:43:49 +0200 | [diff] [blame] | 252 | if (!(orig_base_flags & BDRV_O_RDWR)) { |
| 253 | reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL, |
| 254 | orig_base_flags | BDRV_O_RDWR); |
| 255 | } |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 256 | if (reopen_queue) { |
| 257 | bdrv_reopen_multiple(reopen_queue, &local_err); |
| 258 | if (local_err != NULL) { |
| 259 | error_propagate(errp, local_err); |
Alberto Garcia | 834fe28 | 2016-05-27 12:53:39 +0200 | [diff] [blame] | 260 | block_job_unref(&s->common); |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
Kevin Wolf | 4653456 | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 266 | s->base = blk_new(); |
| 267 | blk_insert_bs(s->base, base); |
| 268 | |
| 269 | s->top = blk_new(); |
| 270 | blk_insert_bs(s->top, top); |
| 271 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 272 | s->active = bs; |
| 273 | |
| 274 | s->base_flags = orig_base_flags; |
| 275 | s->orig_overlay_flags = orig_overlay_flags; |
| 276 | |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 277 | s->backing_file_str = g_strdup(backing_file_str); |
| 278 | |
Jeff Cody | 747ff60 | 2012-09-27 13:29:13 -0400 | [diff] [blame] | 279 | s->on_error = on_error; |
| 280 | s->common.co = qemu_coroutine_create(commit_run); |
| 281 | |
| 282 | trace_commit_start(bs, base, top, s, s->common.co, opaque); |
| 283 | qemu_coroutine_enter(s->common.co, s); |
| 284 | } |
Kevin Wolf | 83fd6dd | 2016-05-30 15:53:15 +0200 | [diff] [blame^] | 285 | |
| 286 | |
| 287 | #define COMMIT_BUF_SECTORS 2048 |
| 288 | |
| 289 | /* commit COW file into the raw image */ |
| 290 | int bdrv_commit(BlockDriverState *bs) |
| 291 | { |
| 292 | BlockDriver *drv = bs->drv; |
| 293 | int64_t sector, total_sectors, length, backing_length; |
| 294 | int n, ro, open_flags; |
| 295 | int ret = 0; |
| 296 | uint8_t *buf = NULL; |
| 297 | |
| 298 | if (!drv) |
| 299 | return -ENOMEDIUM; |
| 300 | |
| 301 | if (!bs->backing) { |
| 302 | return -ENOTSUP; |
| 303 | } |
| 304 | |
| 305 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) || |
| 306 | bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) { |
| 307 | return -EBUSY; |
| 308 | } |
| 309 | |
| 310 | ro = bs->backing->bs->read_only; |
| 311 | open_flags = bs->backing->bs->open_flags; |
| 312 | |
| 313 | if (ro) { |
| 314 | if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) { |
| 315 | return -EACCES; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | length = bdrv_getlength(bs); |
| 320 | if (length < 0) { |
| 321 | ret = length; |
| 322 | goto ro_cleanup; |
| 323 | } |
| 324 | |
| 325 | backing_length = bdrv_getlength(bs->backing->bs); |
| 326 | if (backing_length < 0) { |
| 327 | ret = backing_length; |
| 328 | goto ro_cleanup; |
| 329 | } |
| 330 | |
| 331 | /* If our top snapshot is larger than the backing file image, |
| 332 | * grow the backing file image if possible. If not possible, |
| 333 | * we must return an error */ |
| 334 | if (length > backing_length) { |
| 335 | ret = bdrv_truncate(bs->backing->bs, length); |
| 336 | if (ret < 0) { |
| 337 | goto ro_cleanup; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | total_sectors = length >> BDRV_SECTOR_BITS; |
| 342 | |
| 343 | /* qemu_try_blockalign() for bs will choose an alignment that works for |
| 344 | * bs->backing->bs as well, so no need to compare the alignment manually. */ |
| 345 | buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
| 346 | if (buf == NULL) { |
| 347 | ret = -ENOMEM; |
| 348 | goto ro_cleanup; |
| 349 | } |
| 350 | |
| 351 | for (sector = 0; sector < total_sectors; sector += n) { |
| 352 | ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); |
| 353 | if (ret < 0) { |
| 354 | goto ro_cleanup; |
| 355 | } |
| 356 | if (ret) { |
| 357 | ret = bdrv_read(bs, sector, buf, n); |
| 358 | if (ret < 0) { |
| 359 | goto ro_cleanup; |
| 360 | } |
| 361 | |
| 362 | ret = bdrv_write(bs->backing->bs, sector, buf, n); |
| 363 | if (ret < 0) { |
| 364 | goto ro_cleanup; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if (drv->bdrv_make_empty) { |
| 370 | ret = drv->bdrv_make_empty(bs); |
| 371 | if (ret < 0) { |
| 372 | goto ro_cleanup; |
| 373 | } |
| 374 | bdrv_flush(bs); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Make sure all data we wrote to the backing device is actually |
| 379 | * stable on disk. |
| 380 | */ |
| 381 | if (bs->backing) { |
| 382 | bdrv_flush(bs->backing->bs); |
| 383 | } |
| 384 | |
| 385 | ret = 0; |
| 386 | ro_cleanup: |
| 387 | qemu_vfree(buf); |
| 388 | |
| 389 | if (ro) { |
| 390 | /* ignoring error return here */ |
| 391 | bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL); |
| 392 | } |
| 393 | |
| 394 | return ret; |
| 395 | } |