blob: 8de447352071893f425d9c95f016ccfb8b490690 [file] [log] [blame]
Jeff Cody747ff602012-09-27 13:29:13 -04001/*
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 Maydell80c71a22016-01-18 18:01:42 +000015#include "qemu/osdep.h"
Jeff Cody747ff602012-09-27 13:29:13 -040016#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010017#include "block/block_int.h"
John Snowc87621e2016-10-27 12:07:00 -040018#include "block/blockjob_int.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010019#include "qapi/error.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010020#include "qapi/qmp/qerror.h"
Jeff Cody747ff602012-09-27 13:29:13 -040021#include "qemu/ratelimit.h"
Max Reitz373340b2015-10-19 17:53:22 +020022#include "sysemu/block-backend.h"
Jeff Cody747ff602012-09-27 13:29:13 -040023
24enum {
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
35typedef struct CommitBlockJob {
36 BlockJob common;
37 RateLimit limit;
38 BlockDriverState *active;
Kevin Wolf8dfba272017-01-16 16:22:34 +010039 BlockDriverState *commit_top_bs;
Kevin Wolf46534562016-04-14 13:09:53 +020040 BlockBackend *top;
41 BlockBackend *base;
Paolo Bonzini92aa5c62012-09-28 17:22:55 +020042 BlockdevOnError on_error;
Jeff Cody747ff602012-09-27 13:29:13 -040043 int base_flags;
44 int orig_overlay_flags;
Jeff Cody54e26902014-06-25 15:40:10 -040045 char *backing_file_str;
Jeff Cody747ff602012-09-27 13:29:13 -040046} CommitBlockJob;
47
Kevin Wolf46534562016-04-14 13:09:53 +020048static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base,
Jeff Cody747ff602012-09-27 13:29:13 -040049 int64_t sector_num, int nb_sectors,
50 void *buf)
51{
52 int ret = 0;
Kevin Wolf46534562016-04-14 13:09:53 +020053 QEMUIOVector qiov;
54 struct iovec iov = {
55 .iov_base = buf,
56 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
57 };
Jeff Cody747ff602012-09-27 13:29:13 -040058
Kevin Wolf46534562016-04-14 13:09:53 +020059 qemu_iovec_init_external(&qiov, &iov, 1);
60
61 ret = blk_co_preadv(bs, sector_num * BDRV_SECTOR_SIZE,
62 qiov.size, &qiov, 0);
63 if (ret < 0) {
Jeff Cody747ff602012-09-27 13:29:13 -040064 return ret;
65 }
66
Kevin Wolf46534562016-04-14 13:09:53 +020067 ret = blk_co_pwritev(base, sector_num * BDRV_SECTOR_SIZE,
68 qiov.size, &qiov, 0);
69 if (ret < 0) {
Jeff Cody747ff602012-09-27 13:29:13 -040070 return ret;
71 }
72
73 return 0;
74}
75
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +010076typedef struct {
77 int ret;
78} CommitCompleteData;
79
80static void commit_complete(BlockJob *job, void *opaque)
Jeff Cody747ff602012-09-27 13:29:13 -040081{
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +010082 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
83 CommitCompleteData *data = opaque;
Jeff Cody747ff602012-09-27 13:29:13 -040084 BlockDriverState *active = s->active;
Kevin Wolf46534562016-04-14 13:09:53 +020085 BlockDriverState *top = blk_bs(s->top);
86 BlockDriverState *base = blk_bs(s->base);
Kevin Wolf8dfba272017-01-16 16:22:34 +010087 BlockDriverState *overlay_bs = bdrv_find_overlay(active, s->commit_top_bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +010088 int ret = data->ret;
Kevin Wolf8dfba272017-01-16 16:22:34 +010089 bool remove_commit_top_bs = false;
90
91 /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
92 * the normal backing chain can be restored. */
93 blk_unref(s->base);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +010094
95 if (!block_job_is_cancelled(&s->common) && ret == 0) {
96 /* success */
Kevin Wolf8dfba272017-01-16 16:22:34 +010097 ret = bdrv_drop_intermediate(active, s->commit_top_bs, base,
98 s->backing_file_str);
99 } else if (overlay_bs) {
100 /* XXX Can (or should) we somehow keep 'consistent read' blocked even
101 * after the failed/cancelled commit job is gone? If we already wrote
102 * something to base, the intermediate images aren't valid any more. */
103 remove_commit_top_bs = true;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100104 }
105
106 /* restore base open flags here if appropriate (e.g., change the base back
107 * to r/o). These reopens do not need to be atomic, since we won't abort
108 * even on failure here */
109 if (s->base_flags != bdrv_get_flags(base)) {
110 bdrv_reopen(base, s->base_flags, NULL);
111 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100112 if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
113 bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
114 }
115 g_free(s->backing_file_str);
Kevin Wolf46534562016-04-14 13:09:53 +0200116 blk_unref(s->top);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100117 block_job_completed(&s->common, ret);
118 g_free(data);
Kevin Wolf8dfba272017-01-16 16:22:34 +0100119
120 /* If bdrv_drop_intermediate() didn't already do that, remove the commit
121 * filter driver from the backing chain. Do this as the final step so that
122 * the 'consistent read' permission can be granted. */
123 if (remove_commit_top_bs) {
124 bdrv_set_backing_hd(overlay_bs, top);
125 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100126}
127
128static void coroutine_fn commit_run(void *opaque)
129{
130 CommitBlockJob *s = opaque;
131 CommitCompleteData *data;
Jeff Cody747ff602012-09-27 13:29:13 -0400132 int64_t sector_num, end;
Sascha Silbef14a39c2016-06-28 17:28:41 +0200133 uint64_t delay_ns = 0;
Jeff Cody747ff602012-09-27 13:29:13 -0400134 int ret = 0;
135 int n = 0;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100136 void *buf = NULL;
Jeff Cody747ff602012-09-27 13:29:13 -0400137 int bytes_written = 0;
138 int64_t base_len;
139
Kevin Wolf46534562016-04-14 13:09:53 +0200140 ret = s->common.len = blk_getlength(s->top);
Jeff Cody747ff602012-09-27 13:29:13 -0400141
142
143 if (s->common.len < 0) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100144 goto out;
Jeff Cody747ff602012-09-27 13:29:13 -0400145 }
146
Kevin Wolf46534562016-04-14 13:09:53 +0200147 ret = base_len = blk_getlength(s->base);
Jeff Cody747ff602012-09-27 13:29:13 -0400148 if (base_len < 0) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100149 goto out;
Jeff Cody747ff602012-09-27 13:29:13 -0400150 }
151
152 if (base_len < s->common.len) {
Kevin Wolf46534562016-04-14 13:09:53 +0200153 ret = blk_truncate(s->base, s->common.len);
Jeff Cody747ff602012-09-27 13:29:13 -0400154 if (ret) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100155 goto out;
Jeff Cody747ff602012-09-27 13:29:13 -0400156 }
157 }
158
Jeff Cody747ff602012-09-27 13:29:13 -0400159 end = s->common.len >> BDRV_SECTOR_BITS;
Kevin Wolf46534562016-04-14 13:09:53 +0200160 buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE);
Jeff Cody747ff602012-09-27 13:29:13 -0400161
162 for (sector_num = 0; sector_num < end; sector_num += n) {
Jeff Cody747ff602012-09-27 13:29:13 -0400163 bool copy;
164
Jeff Cody747ff602012-09-27 13:29:13 -0400165 /* Note that even when no rate limit is applied we need to yield
Kevin Wolfc57b6652012-11-13 16:35:13 +0100166 * with no pending I/O here so that bdrv_drain_all() returns.
Jeff Cody747ff602012-09-27 13:29:13 -0400167 */
Alex Bligh7483d1e2013-08-21 16:03:05 +0100168 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
Jeff Cody747ff602012-09-27 13:29:13 -0400169 if (block_job_is_cancelled(&s->common)) {
170 break;
171 }
172 /* Copy if allocated above the base */
Kevin Wolf46534562016-04-14 13:09:53 +0200173 ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base),
174 sector_num,
Paolo Bonzini4f578632013-09-04 19:00:24 +0200175 COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
176 &n);
Jeff Cody747ff602012-09-27 13:29:13 -0400177 copy = (ret == 1);
178 trace_commit_one_iteration(s, sector_num, n, ret);
179 if (copy) {
Kevin Wolf46534562016-04-14 13:09:53 +0200180 ret = commit_populate(s->top, s->base, sector_num, n, buf);
Jeff Cody747ff602012-09-27 13:29:13 -0400181 bytes_written += n * BDRV_SECTOR_SIZE;
182 }
183 if (ret < 0) {
Kevin Wolf1e8fb7f2016-06-29 17:38:57 +0200184 BlockErrorAction action =
185 block_job_error_action(&s->common, false, s->on_error, -ret);
186 if (action == BLOCK_ERROR_ACTION_REPORT) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100187 goto out;
Jeff Cody747ff602012-09-27 13:29:13 -0400188 } else {
189 n = 0;
190 continue;
191 }
192 }
193 /* Publish progress */
194 s->common.offset += n * BDRV_SECTOR_SIZE;
Sascha Silbef14a39c2016-06-28 17:28:41 +0200195
196 if (copy && s->common.speed) {
197 delay_ns = ratelimit_calculate_delay(&s->limit, n);
198 }
Jeff Cody747ff602012-09-27 13:29:13 -0400199 }
200
201 ret = 0;
202
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100203out:
Jeff Cody747ff602012-09-27 13:29:13 -0400204 qemu_vfree(buf);
205
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100206 data = g_malloc(sizeof(*data));
207 data->ret = ret;
208 block_job_defer_to_main_loop(&s->common, commit_complete, data);
Jeff Cody747ff602012-09-27 13:29:13 -0400209}
210
211static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
212{
213 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
214
215 if (speed < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100216 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
Jeff Cody747ff602012-09-27 13:29:13 -0400217 return;
218 }
219 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
220}
221
Fam Zheng3fc4b102013-10-08 17:29:38 +0800222static const BlockJobDriver commit_job_driver = {
Jeff Cody747ff602012-09-27 13:29:13 -0400223 .instance_size = sizeof(CommitBlockJob),
Fam Zheng79e14bf2013-10-08 17:29:40 +0800224 .job_type = BLOCK_JOB_TYPE_COMMIT,
Jeff Cody747ff602012-09-27 13:29:13 -0400225 .set_speed = commit_set_speed,
John Snowa7815a72016-11-08 01:50:36 -0500226 .start = commit_run,
Jeff Cody747ff602012-09-27 13:29:13 -0400227};
228
Kevin Wolf8dfba272017-01-16 16:22:34 +0100229static int coroutine_fn bdrv_commit_top_preadv(BlockDriverState *bs,
230 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
231{
232 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
233}
234
235static void bdrv_commit_top_close(BlockDriverState *bs)
236{
237}
238
239static void bdrv_commit_top_child_perm(BlockDriverState *bs, BdrvChild *c,
240 const BdrvChildRole *role,
241 uint64_t perm, uint64_t shared,
242 uint64_t *nperm, uint64_t *nshared)
243{
244 *nperm = 0;
245 *nshared = BLK_PERM_ALL;
246}
247
248/* Dummy node that provides consistent read to its users without requiring it
249 * from its backing file and that allows writes on the backing file chain. */
250static BlockDriver bdrv_commit_top = {
251 .format_name = "commit_top",
252 .bdrv_co_preadv = bdrv_commit_top_preadv,
253 .bdrv_close = bdrv_commit_top_close,
254 .bdrv_child_perm = bdrv_commit_top_child_perm,
255};
256
Alberto Garciafd62c602016-07-05 17:29:00 +0300257void commit_start(const char *job_id, BlockDriverState *bs,
258 BlockDriverState *base, BlockDriverState *top, int64_t speed,
John Snow8254b6d2016-10-27 12:06:58 -0400259 BlockdevOnError on_error, const char *backing_file_str,
260 Error **errp)
Jeff Cody747ff602012-09-27 13:29:13 -0400261{
262 CommitBlockJob *s;
263 BlockReopenQueue *reopen_queue = NULL;
264 int orig_overlay_flags;
265 int orig_base_flags;
Alberto Garcia3e4c5122016-10-28 10:08:08 +0300266 BlockDriverState *iter;
Jeff Cody747ff602012-09-27 13:29:13 -0400267 BlockDriverState *overlay_bs;
Kevin Wolf8dfba272017-01-16 16:22:34 +0100268 BlockDriverState *commit_top_bs = NULL;
Jeff Cody747ff602012-09-27 13:29:13 -0400269 Error *local_err = NULL;
Kevin Wolfd7086422017-01-13 19:02:32 +0100270 int ret;
Jeff Cody747ff602012-09-27 13:29:13 -0400271
Fam Zheng18da7f92013-12-16 14:45:33 +0800272 assert(top != bs);
Jeff Cody747ff602012-09-27 13:29:13 -0400273 if (top == base) {
274 error_setg(errp, "Invalid files for merge: top and base are the same");
275 return;
276 }
277
Jeff Cody747ff602012-09-27 13:29:13 -0400278 overlay_bs = bdrv_find_overlay(bs, top);
279
280 if (overlay_bs == NULL) {
281 error_setg(errp, "Could not find overlay image for %s:", top->filename);
282 return;
283 }
284
Kevin Wolfc6cc12b2017-01-16 17:18:09 +0100285 s = block_job_create(job_id, &commit_job_driver, bs, 0, BLK_PERM_ALL,
286 speed, BLOCK_JOB_DEFAULT, NULL, NULL, errp);
Alberto Garcia834fe282016-05-27 12:53:39 +0200287 if (!s) {
288 return;
289 }
290
Jeff Cody747ff602012-09-27 13:29:13 -0400291 orig_base_flags = bdrv_get_flags(base);
292 orig_overlay_flags = bdrv_get_flags(overlay_bs);
293
294 /* convert base & overlay_bs to r/w, if necessary */
Alberto Garcia3db2bd52015-10-28 15:43:49 +0200295 if (!(orig_base_flags & BDRV_O_RDWR)) {
296 reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL,
297 orig_base_flags | BDRV_O_RDWR);
298 }
Alberto Garcia0fe282b2016-09-15 17:53:04 +0300299 if (!(orig_overlay_flags & BDRV_O_RDWR)) {
300 reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL,
301 orig_overlay_flags | BDRV_O_RDWR);
302 }
Jeff Cody747ff602012-09-27 13:29:13 -0400303 if (reopen_queue) {
Paolo Bonzini720150f2016-10-27 12:49:02 +0200304 bdrv_reopen_multiple(bdrv_get_aio_context(bs), reopen_queue, &local_err);
Jeff Cody747ff602012-09-27 13:29:13 -0400305 if (local_err != NULL) {
306 error_propagate(errp, local_err);
Kevin Wolfd7086422017-01-13 19:02:32 +0100307 goto fail;
Jeff Cody747ff602012-09-27 13:29:13 -0400308 }
309 }
310
Kevin Wolf8dfba272017-01-16 16:22:34 +0100311 /* Insert commit_top block node above top, so we can block consistent read
312 * on the backing chain below it */
313 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, NULL, 0, errp);
314 if (commit_top_bs == NULL) {
315 goto fail;
316 }
317
318 bdrv_set_backing_hd(commit_top_bs, top);
319 bdrv_set_backing_hd(overlay_bs, commit_top_bs);
320
321 s->commit_top_bs = commit_top_bs;
322 bdrv_unref(commit_top_bs);
Jeff Cody747ff602012-09-27 13:29:13 -0400323
Alberto Garcia3e4c5122016-10-28 10:08:08 +0300324 /* Block all nodes between top and base, because they will
325 * disappear from the chain after this operation. */
326 assert(bdrv_chain_contains(top, base));
Kevin Wolf8dfba272017-01-16 16:22:34 +0100327 for (iter = top; iter != base; iter = backing_bs(iter)) {
328 /* XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves
329 * at s->base (if writes are blocked for a node, they are also blocked
330 * for its backing file). The other options would be a second filter
331 * driver above s->base. */
332 ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
333 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,
334 errp);
335 if (ret < 0) {
336 goto fail;
337 }
Alberto Garcia3e4c5122016-10-28 10:08:08 +0300338 }
339
Kevin Wolf8dfba272017-01-16 16:22:34 +0100340 ret = block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, errp);
341 if (ret < 0) {
342 goto fail;
343 }
344
345 /* overlay_bs must be blocked because it needs to be modified to
346 * update the backing image string. */
347 ret = block_job_add_bdrv(&s->common, "overlay of top", overlay_bs,
348 BLK_PERM_GRAPH_MOD, BLK_PERM_ALL, errp);
349 if (ret < 0) {
350 goto fail;
351 }
352
353 s->base = blk_new(BLK_PERM_CONSISTENT_READ
354 | BLK_PERM_WRITE
355 | BLK_PERM_RESIZE,
356 BLK_PERM_CONSISTENT_READ
357 | BLK_PERM_GRAPH_MOD
358 | BLK_PERM_WRITE_UNCHANGED);
Kevin Wolfd7086422017-01-13 19:02:32 +0100359 ret = blk_insert_bs(s->base, base, errp);
360 if (ret < 0) {
361 goto fail;
362 }
Kevin Wolf46534562016-04-14 13:09:53 +0200363
Kevin Wolf8dfba272017-01-16 16:22:34 +0100364 /* Required permissions are already taken with block_job_add_bdrv() */
Kevin Wolf6d0eb642017-01-20 17:07:26 +0100365 s->top = blk_new(0, BLK_PERM_ALL);
Kevin Wolf8dfba272017-01-16 16:22:34 +0100366 blk_insert_bs(s->top, top, errp);
Kevin Wolfd7086422017-01-13 19:02:32 +0100367 if (ret < 0) {
368 goto fail;
369 }
Kevin Wolf46534562016-04-14 13:09:53 +0200370
Jeff Cody747ff602012-09-27 13:29:13 -0400371 s->active = bs;
372
373 s->base_flags = orig_base_flags;
374 s->orig_overlay_flags = orig_overlay_flags;
375
Jeff Cody54e26902014-06-25 15:40:10 -0400376 s->backing_file_str = g_strdup(backing_file_str);
377
Jeff Cody747ff602012-09-27 13:29:13 -0400378 s->on_error = on_error;
Jeff Cody747ff602012-09-27 13:29:13 -0400379
John Snow5ccac6f2016-11-08 01:50:37 -0500380 trace_commit_start(bs, base, top, s);
381 block_job_start(&s->common);
Kevin Wolfd7086422017-01-13 19:02:32 +0100382 return;
383
384fail:
385 if (s->base) {
386 blk_unref(s->base);
387 }
388 if (s->top) {
389 blk_unref(s->top);
390 }
Kevin Wolf8dfba272017-01-16 16:22:34 +0100391 if (commit_top_bs) {
392 bdrv_set_backing_hd(overlay_bs, top);
393 }
Kevin Wolfd7086422017-01-13 19:02:32 +0100394 block_job_unref(&s->common);
Jeff Cody747ff602012-09-27 13:29:13 -0400395}
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200396
397
398#define COMMIT_BUF_SECTORS 2048
399
400/* commit COW file into the raw image */
401int bdrv_commit(BlockDriverState *bs)
402{
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200403 BlockBackend *src, *backing;
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200404 BlockDriver *drv = bs->drv;
405 int64_t sector, total_sectors, length, backing_length;
406 int n, ro, open_flags;
407 int ret = 0;
408 uint8_t *buf = NULL;
409
410 if (!drv)
411 return -ENOMEDIUM;
412
413 if (!bs->backing) {
414 return -ENOTSUP;
415 }
416
417 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
418 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
419 return -EBUSY;
420 }
421
422 ro = bs->backing->bs->read_only;
423 open_flags = bs->backing->bs->open_flags;
424
425 if (ro) {
426 if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
427 return -EACCES;
428 }
429 }
430
Kevin Wolf6d0eb642017-01-20 17:07:26 +0100431 /* FIXME Use real permissions */
432 src = blk_new(0, BLK_PERM_ALL);
Kevin Wolf6d0eb642017-01-20 17:07:26 +0100433 backing = blk_new(0, BLK_PERM_ALL);
Kevin Wolfd7086422017-01-13 19:02:32 +0100434
435 ret = blk_insert_bs(src, bs, NULL);
436 if (ret < 0) {
437 goto ro_cleanup;
438 }
439
440 ret = blk_insert_bs(backing, bs->backing->bs, NULL);
441 if (ret < 0) {
442 goto ro_cleanup;
443 }
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200444
445 length = blk_getlength(src);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200446 if (length < 0) {
447 ret = length;
448 goto ro_cleanup;
449 }
450
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200451 backing_length = blk_getlength(backing);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200452 if (backing_length < 0) {
453 ret = backing_length;
454 goto ro_cleanup;
455 }
456
457 /* If our top snapshot is larger than the backing file image,
458 * grow the backing file image if possible. If not possible,
459 * we must return an error */
460 if (length > backing_length) {
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200461 ret = blk_truncate(backing, length);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200462 if (ret < 0) {
463 goto ro_cleanup;
464 }
465 }
466
467 total_sectors = length >> BDRV_SECTOR_BITS;
468
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200469 /* blk_try_blockalign() for src will choose an alignment that works for
470 * backing as well, so no need to compare the alignment manually. */
471 buf = blk_try_blockalign(src, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200472 if (buf == NULL) {
473 ret = -ENOMEM;
474 goto ro_cleanup;
475 }
476
477 for (sector = 0; sector < total_sectors; sector += n) {
478 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
479 if (ret < 0) {
480 goto ro_cleanup;
481 }
482 if (ret) {
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200483 ret = blk_pread(src, sector * BDRV_SECTOR_SIZE, buf,
484 n * BDRV_SECTOR_SIZE);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200485 if (ret < 0) {
486 goto ro_cleanup;
487 }
488
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200489 ret = blk_pwrite(backing, sector * BDRV_SECTOR_SIZE, buf,
490 n * BDRV_SECTOR_SIZE, 0);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200491 if (ret < 0) {
492 goto ro_cleanup;
493 }
494 }
495 }
496
497 if (drv->bdrv_make_empty) {
498 ret = drv->bdrv_make_empty(bs);
499 if (ret < 0) {
500 goto ro_cleanup;
501 }
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200502 blk_flush(src);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200503 }
504
505 /*
506 * Make sure all data we wrote to the backing device is actually
507 * stable on disk.
508 */
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200509 blk_flush(backing);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200510
511 ret = 0;
512ro_cleanup:
513 qemu_vfree(buf);
514
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200515 blk_unref(src);
516 blk_unref(backing);
517
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200518 if (ro) {
519 /* ignoring error return here */
520 bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
521 }
522
523 return ret;
524}