blob: 28324820a40cdfbef45a6c3df6b295feacbb289c [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"
Kevin Wolfdcbf37c2017-03-09 11:49:16 +010016#include "qemu/cutils.h"
Jeff Cody747ff602012-09-27 13:29:13 -040017#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010018#include "block/block_int.h"
John Snowc87621e2016-10-27 12:07:00 -040019#include "block/blockjob_int.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010020#include "qapi/error.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010021#include "qapi/qmp/qerror.h"
Jeff Cody747ff602012-09-27 13:29:13 -040022#include "qemu/ratelimit.h"
Max Reitz373340b2015-10-19 17:53:22 +020023#include "sysemu/block-backend.h"
Jeff Cody747ff602012-09-27 13:29:13 -040024
25enum {
26 /*
27 * Size of data buffer for populating the image file. This should be large
28 * enough to process multiple clusters in a single call, so that populating
29 * contiguous regions of the image is efficient.
30 */
31 COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */
32};
33
34#define SLICE_TIME 100000000ULL /* ns */
35
36typedef struct CommitBlockJob {
37 BlockJob common;
38 RateLimit limit;
39 BlockDriverState *active;
Kevin Wolf8dfba272017-01-16 16:22:34 +010040 BlockDriverState *commit_top_bs;
Kevin Wolf46534562016-04-14 13:09:53 +020041 BlockBackend *top;
42 BlockBackend *base;
Paolo Bonzini92aa5c62012-09-28 17:22:55 +020043 BlockdevOnError on_error;
Jeff Cody747ff602012-09-27 13:29:13 -040044 int base_flags;
45 int orig_overlay_flags;
Jeff Cody54e26902014-06-25 15:40:10 -040046 char *backing_file_str;
Jeff Cody747ff602012-09-27 13:29:13 -040047} CommitBlockJob;
48
Kevin Wolf46534562016-04-14 13:09:53 +020049static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base,
Jeff Cody747ff602012-09-27 13:29:13 -040050 int64_t sector_num, int nb_sectors,
51 void *buf)
52{
53 int ret = 0;
Kevin Wolf46534562016-04-14 13:09:53 +020054 QEMUIOVector qiov;
55 struct iovec iov = {
56 .iov_base = buf,
57 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
58 };
Jeff Cody747ff602012-09-27 13:29:13 -040059
Kevin Wolf46534562016-04-14 13:09:53 +020060 qemu_iovec_init_external(&qiov, &iov, 1);
61
62 ret = blk_co_preadv(bs, sector_num * BDRV_SECTOR_SIZE,
63 qiov.size, &qiov, 0);
64 if (ret < 0) {
Jeff Cody747ff602012-09-27 13:29:13 -040065 return ret;
66 }
67
Kevin Wolf46534562016-04-14 13:09:53 +020068 ret = blk_co_pwritev(base, sector_num * BDRV_SECTOR_SIZE,
69 qiov.size, &qiov, 0);
70 if (ret < 0) {
Jeff Cody747ff602012-09-27 13:29:13 -040071 return ret;
72 }
73
74 return 0;
75}
76
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +010077typedef struct {
78 int ret;
79} CommitCompleteData;
80
81static void commit_complete(BlockJob *job, void *opaque)
Jeff Cody747ff602012-09-27 13:29:13 -040082{
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +010083 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
84 CommitCompleteData *data = opaque;
Jeff Cody747ff602012-09-27 13:29:13 -040085 BlockDriverState *active = s->active;
Kevin Wolf46534562016-04-14 13:09:53 +020086 BlockDriverState *top = blk_bs(s->top);
87 BlockDriverState *base = blk_bs(s->base);
Kevin Wolf8dfba272017-01-16 16:22:34 +010088 BlockDriverState *overlay_bs = bdrv_find_overlay(active, s->commit_top_bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +010089 int ret = data->ret;
Kevin Wolf8dfba272017-01-16 16:22:34 +010090 bool remove_commit_top_bs = false;
91
92 /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
93 * the normal backing chain can be restored. */
94 blk_unref(s->base);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +010095
96 if (!block_job_is_cancelled(&s->common) && ret == 0) {
97 /* success */
Kevin Wolf8dfba272017-01-16 16:22:34 +010098 ret = bdrv_drop_intermediate(active, s->commit_top_bs, base,
99 s->backing_file_str);
100 } else if (overlay_bs) {
101 /* XXX Can (or should) we somehow keep 'consistent read' blocked even
102 * after the failed/cancelled commit job is gone? If we already wrote
103 * something to base, the intermediate images aren't valid any more. */
104 remove_commit_top_bs = true;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100105 }
106
107 /* restore base open flags here if appropriate (e.g., change the base back
108 * to r/o). These reopens do not need to be atomic, since we won't abort
109 * even on failure here */
110 if (s->base_flags != bdrv_get_flags(base)) {
111 bdrv_reopen(base, s->base_flags, NULL);
112 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100113 if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
114 bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
115 }
116 g_free(s->backing_file_str);
Kevin Wolf46534562016-04-14 13:09:53 +0200117 blk_unref(s->top);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100118 block_job_completed(&s->common, ret);
119 g_free(data);
Kevin Wolf8dfba272017-01-16 16:22:34 +0100120
121 /* If bdrv_drop_intermediate() didn't already do that, remove the commit
122 * filter driver from the backing chain. Do this as the final step so that
123 * the 'consistent read' permission can be granted. */
124 if (remove_commit_top_bs) {
Kevin Wolf12fa4af2017-02-17 20:42:32 +0100125 bdrv_set_backing_hd(overlay_bs, top, &error_abort);
Kevin Wolf8dfba272017-01-16 16:22:34 +0100126 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100127}
128
129static void coroutine_fn commit_run(void *opaque)
130{
131 CommitBlockJob *s = opaque;
132 CommitCompleteData *data;
Jeff Cody747ff602012-09-27 13:29:13 -0400133 int64_t sector_num, end;
Sascha Silbef14a39c2016-06-28 17:28:41 +0200134 uint64_t delay_ns = 0;
Jeff Cody747ff602012-09-27 13:29:13 -0400135 int ret = 0;
136 int n = 0;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100137 void *buf = NULL;
Jeff Cody747ff602012-09-27 13:29:13 -0400138 int bytes_written = 0;
139 int64_t base_len;
140
Kevin Wolf46534562016-04-14 13:09:53 +0200141 ret = s->common.len = blk_getlength(s->top);
Jeff Cody747ff602012-09-27 13:29:13 -0400142
143
144 if (s->common.len < 0) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100145 goto out;
Jeff Cody747ff602012-09-27 13:29:13 -0400146 }
147
Kevin Wolf46534562016-04-14 13:09:53 +0200148 ret = base_len = blk_getlength(s->base);
Jeff Cody747ff602012-09-27 13:29:13 -0400149 if (base_len < 0) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100150 goto out;
Jeff Cody747ff602012-09-27 13:29:13 -0400151 }
152
153 if (base_len < s->common.len) {
Kevin Wolf46534562016-04-14 13:09:53 +0200154 ret = blk_truncate(s->base, s->common.len);
Jeff Cody747ff602012-09-27 13:29:13 -0400155 if (ret) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100156 goto out;
Jeff Cody747ff602012-09-27 13:29:13 -0400157 }
158 }
159
Jeff Cody747ff602012-09-27 13:29:13 -0400160 end = s->common.len >> BDRV_SECTOR_BITS;
Kevin Wolf46534562016-04-14 13:09:53 +0200161 buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE);
Jeff Cody747ff602012-09-27 13:29:13 -0400162
163 for (sector_num = 0; sector_num < end; sector_num += n) {
Jeff Cody747ff602012-09-27 13:29:13 -0400164 bool copy;
165
Jeff Cody747ff602012-09-27 13:29:13 -0400166 /* Note that even when no rate limit is applied we need to yield
Kevin Wolfc57b6652012-11-13 16:35:13 +0100167 * with no pending I/O here so that bdrv_drain_all() returns.
Jeff Cody747ff602012-09-27 13:29:13 -0400168 */
Alex Bligh7483d1e2013-08-21 16:03:05 +0100169 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
Jeff Cody747ff602012-09-27 13:29:13 -0400170 if (block_job_is_cancelled(&s->common)) {
171 break;
172 }
173 /* Copy if allocated above the base */
Kevin Wolf46534562016-04-14 13:09:53 +0200174 ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base),
175 sector_num,
Paolo Bonzini4f578632013-09-04 19:00:24 +0200176 COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
177 &n);
Jeff Cody747ff602012-09-27 13:29:13 -0400178 copy = (ret == 1);
179 trace_commit_one_iteration(s, sector_num, n, ret);
180 if (copy) {
Kevin Wolf46534562016-04-14 13:09:53 +0200181 ret = commit_populate(s->top, s->base, sector_num, n, buf);
Jeff Cody747ff602012-09-27 13:29:13 -0400182 bytes_written += n * BDRV_SECTOR_SIZE;
183 }
184 if (ret < 0) {
Kevin Wolf1e8fb7f2016-06-29 17:38:57 +0200185 BlockErrorAction action =
186 block_job_error_action(&s->common, false, s->on_error, -ret);
187 if (action == BLOCK_ERROR_ACTION_REPORT) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100188 goto out;
Jeff Cody747ff602012-09-27 13:29:13 -0400189 } else {
190 n = 0;
191 continue;
192 }
193 }
194 /* Publish progress */
195 s->common.offset += n * BDRV_SECTOR_SIZE;
Sascha Silbef14a39c2016-06-28 17:28:41 +0200196
197 if (copy && s->common.speed) {
198 delay_ns = ratelimit_calculate_delay(&s->limit, n);
199 }
Jeff Cody747ff602012-09-27 13:29:13 -0400200 }
201
202 ret = 0;
203
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100204out:
Jeff Cody747ff602012-09-27 13:29:13 -0400205 qemu_vfree(buf);
206
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +0100207 data = g_malloc(sizeof(*data));
208 data->ret = ret;
209 block_job_defer_to_main_loop(&s->common, commit_complete, data);
Jeff Cody747ff602012-09-27 13:29:13 -0400210}
211
212static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
213{
214 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
215
216 if (speed < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100217 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
Jeff Cody747ff602012-09-27 13:29:13 -0400218 return;
219 }
220 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
221}
222
Fam Zheng3fc4b102013-10-08 17:29:38 +0800223static const BlockJobDriver commit_job_driver = {
Jeff Cody747ff602012-09-27 13:29:13 -0400224 .instance_size = sizeof(CommitBlockJob),
Fam Zheng79e14bf2013-10-08 17:29:40 +0800225 .job_type = BLOCK_JOB_TYPE_COMMIT,
Jeff Cody747ff602012-09-27 13:29:13 -0400226 .set_speed = commit_set_speed,
John Snowa7815a72016-11-08 01:50:36 -0500227 .start = commit_run,
Jeff Cody747ff602012-09-27 13:29:13 -0400228};
229
Kevin Wolf8dfba272017-01-16 16:22:34 +0100230static int coroutine_fn bdrv_commit_top_preadv(BlockDriverState *bs,
231 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
232{
233 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
234}
235
Kevin Wolf91965652017-03-08 15:07:12 +0100236static int64_t coroutine_fn bdrv_commit_top_get_block_status(
237 BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum,
238 BlockDriverState **file)
239{
240 *pnum = nb_sectors;
241 *file = bs->backing->bs;
242 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
243 (sector_num << BDRV_SECTOR_BITS);
244}
245
Kevin Wolfdcbf37c2017-03-09 11:49:16 +0100246static void bdrv_commit_top_refresh_filename(BlockDriverState *bs, QDict *opts)
247{
248 bdrv_refresh_filename(bs->backing->bs);
249 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
250 bs->backing->bs->filename);
251}
Kevin Wolf91965652017-03-08 15:07:12 +0100252
Kevin Wolf8dfba272017-01-16 16:22:34 +0100253static void bdrv_commit_top_close(BlockDriverState *bs)
254{
255}
256
257static void bdrv_commit_top_child_perm(BlockDriverState *bs, BdrvChild *c,
258 const BdrvChildRole *role,
259 uint64_t perm, uint64_t shared,
260 uint64_t *nperm, uint64_t *nshared)
261{
262 *nperm = 0;
263 *nshared = BLK_PERM_ALL;
264}
265
266/* Dummy node that provides consistent read to its users without requiring it
267 * from its backing file and that allows writes on the backing file chain. */
268static BlockDriver bdrv_commit_top = {
Kevin Wolf91965652017-03-08 15:07:12 +0100269 .format_name = "commit_top",
270 .bdrv_co_preadv = bdrv_commit_top_preadv,
271 .bdrv_co_get_block_status = bdrv_commit_top_get_block_status,
Kevin Wolfdcbf37c2017-03-09 11:49:16 +0100272 .bdrv_refresh_filename = bdrv_commit_top_refresh_filename,
Kevin Wolf91965652017-03-08 15:07:12 +0100273 .bdrv_close = bdrv_commit_top_close,
274 .bdrv_child_perm = bdrv_commit_top_child_perm,
Kevin Wolf8dfba272017-01-16 16:22:34 +0100275};
276
Alberto Garciafd62c602016-07-05 17:29:00 +0300277void commit_start(const char *job_id, BlockDriverState *bs,
278 BlockDriverState *base, BlockDriverState *top, int64_t speed,
John Snow8254b6d2016-10-27 12:06:58 -0400279 BlockdevOnError on_error, const char *backing_file_str,
Kevin Wolf0db832f2017-02-20 18:10:05 +0100280 const char *filter_node_name, Error **errp)
Jeff Cody747ff602012-09-27 13:29:13 -0400281{
282 CommitBlockJob *s;
283 BlockReopenQueue *reopen_queue = NULL;
284 int orig_overlay_flags;
285 int orig_base_flags;
Alberto Garcia3e4c5122016-10-28 10:08:08 +0300286 BlockDriverState *iter;
Jeff Cody747ff602012-09-27 13:29:13 -0400287 BlockDriverState *overlay_bs;
Kevin Wolf8dfba272017-01-16 16:22:34 +0100288 BlockDriverState *commit_top_bs = NULL;
Jeff Cody747ff602012-09-27 13:29:13 -0400289 Error *local_err = NULL;
Kevin Wolfd7086422017-01-13 19:02:32 +0100290 int ret;
Jeff Cody747ff602012-09-27 13:29:13 -0400291
Fam Zheng18da7f92013-12-16 14:45:33 +0800292 assert(top != bs);
Jeff Cody747ff602012-09-27 13:29:13 -0400293 if (top == base) {
294 error_setg(errp, "Invalid files for merge: top and base are the same");
295 return;
296 }
297
Jeff Cody747ff602012-09-27 13:29:13 -0400298 overlay_bs = bdrv_find_overlay(bs, top);
299
300 if (overlay_bs == NULL) {
301 error_setg(errp, "Could not find overlay image for %s:", top->filename);
302 return;
303 }
304
Kevin Wolfc6cc12b2017-01-16 17:18:09 +0100305 s = block_job_create(job_id, &commit_job_driver, bs, 0, BLK_PERM_ALL,
306 speed, BLOCK_JOB_DEFAULT, NULL, NULL, errp);
Alberto Garcia834fe282016-05-27 12:53:39 +0200307 if (!s) {
308 return;
309 }
310
Jeff Cody747ff602012-09-27 13:29:13 -0400311 orig_base_flags = bdrv_get_flags(base);
312 orig_overlay_flags = bdrv_get_flags(overlay_bs);
313
314 /* convert base & overlay_bs to r/w, if necessary */
Alberto Garcia3db2bd52015-10-28 15:43:49 +0200315 if (!(orig_base_flags & BDRV_O_RDWR)) {
316 reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL,
317 orig_base_flags | BDRV_O_RDWR);
318 }
Alberto Garcia0fe282b2016-09-15 17:53:04 +0300319 if (!(orig_overlay_flags & BDRV_O_RDWR)) {
320 reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL,
321 orig_overlay_flags | BDRV_O_RDWR);
322 }
Jeff Cody747ff602012-09-27 13:29:13 -0400323 if (reopen_queue) {
Paolo Bonzini720150f2016-10-27 12:49:02 +0200324 bdrv_reopen_multiple(bdrv_get_aio_context(bs), reopen_queue, &local_err);
Jeff Cody747ff602012-09-27 13:29:13 -0400325 if (local_err != NULL) {
326 error_propagate(errp, local_err);
Kevin Wolfd7086422017-01-13 19:02:32 +0100327 goto fail;
Jeff Cody747ff602012-09-27 13:29:13 -0400328 }
329 }
330
Kevin Wolf8dfba272017-01-16 16:22:34 +0100331 /* Insert commit_top block node above top, so we can block consistent read
332 * on the backing chain below it */
Kevin Wolf0db832f2017-02-20 18:10:05 +0100333 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, filter_node_name, 0,
334 errp);
Kevin Wolf8dfba272017-01-16 16:22:34 +0100335 if (commit_top_bs == NULL) {
336 goto fail;
337 }
338
Fam Zhengb69f00d2017-03-07 19:07:22 +0800339 bdrv_set_backing_hd(commit_top_bs, top, &local_err);
340 if (local_err) {
341 bdrv_unref(commit_top_bs);
342 commit_top_bs = NULL;
343 error_propagate(errp, local_err);
344 goto fail;
345 }
346 bdrv_set_backing_hd(overlay_bs, commit_top_bs, &local_err);
347 if (local_err) {
348 bdrv_unref(commit_top_bs);
349 commit_top_bs = NULL;
350 error_propagate(errp, local_err);
351 goto fail;
352 }
Kevin Wolf8dfba272017-01-16 16:22:34 +0100353
354 s->commit_top_bs = commit_top_bs;
355 bdrv_unref(commit_top_bs);
Jeff Cody747ff602012-09-27 13:29:13 -0400356
Alberto Garcia3e4c5122016-10-28 10:08:08 +0300357 /* Block all nodes between top and base, because they will
358 * disappear from the chain after this operation. */
359 assert(bdrv_chain_contains(top, base));
Kevin Wolf8dfba272017-01-16 16:22:34 +0100360 for (iter = top; iter != base; iter = backing_bs(iter)) {
361 /* XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves
362 * at s->base (if writes are blocked for a node, they are also blocked
363 * for its backing file). The other options would be a second filter
364 * driver above s->base. */
365 ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
366 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,
367 errp);
368 if (ret < 0) {
369 goto fail;
370 }
Alberto Garcia3e4c5122016-10-28 10:08:08 +0300371 }
372
Kevin Wolf8dfba272017-01-16 16:22:34 +0100373 ret = block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, errp);
374 if (ret < 0) {
375 goto fail;
376 }
377
378 /* overlay_bs must be blocked because it needs to be modified to
379 * update the backing image string. */
380 ret = block_job_add_bdrv(&s->common, "overlay of top", overlay_bs,
381 BLK_PERM_GRAPH_MOD, BLK_PERM_ALL, errp);
382 if (ret < 0) {
383 goto fail;
384 }
385
386 s->base = blk_new(BLK_PERM_CONSISTENT_READ
387 | BLK_PERM_WRITE
388 | BLK_PERM_RESIZE,
389 BLK_PERM_CONSISTENT_READ
390 | BLK_PERM_GRAPH_MOD
391 | BLK_PERM_WRITE_UNCHANGED);
Kevin Wolfd7086422017-01-13 19:02:32 +0100392 ret = blk_insert_bs(s->base, base, errp);
393 if (ret < 0) {
394 goto fail;
395 }
Kevin Wolf46534562016-04-14 13:09:53 +0200396
Kevin Wolf8dfba272017-01-16 16:22:34 +0100397 /* Required permissions are already taken with block_job_add_bdrv() */
Kevin Wolf6d0eb642017-01-20 17:07:26 +0100398 s->top = blk_new(0, BLK_PERM_ALL);
Kevin Wolfb2477672017-03-03 16:54:21 +0100399 ret = blk_insert_bs(s->top, top, errp);
Kevin Wolfd7086422017-01-13 19:02:32 +0100400 if (ret < 0) {
401 goto fail;
402 }
Kevin Wolf46534562016-04-14 13:09:53 +0200403
Jeff Cody747ff602012-09-27 13:29:13 -0400404 s->active = bs;
405
406 s->base_flags = orig_base_flags;
407 s->orig_overlay_flags = orig_overlay_flags;
408
Jeff Cody54e26902014-06-25 15:40:10 -0400409 s->backing_file_str = g_strdup(backing_file_str);
410
Jeff Cody747ff602012-09-27 13:29:13 -0400411 s->on_error = on_error;
Jeff Cody747ff602012-09-27 13:29:13 -0400412
John Snow5ccac6f2016-11-08 01:50:37 -0500413 trace_commit_start(bs, base, top, s);
414 block_job_start(&s->common);
Kevin Wolfd7086422017-01-13 19:02:32 +0100415 return;
416
417fail:
418 if (s->base) {
419 blk_unref(s->base);
420 }
421 if (s->top) {
422 blk_unref(s->top);
423 }
Kevin Wolf8dfba272017-01-16 16:22:34 +0100424 if (commit_top_bs) {
Kevin Wolf12fa4af2017-02-17 20:42:32 +0100425 bdrv_set_backing_hd(overlay_bs, top, &error_abort);
Kevin Wolf8dfba272017-01-16 16:22:34 +0100426 }
Kevin Wolfd7086422017-01-13 19:02:32 +0100427 block_job_unref(&s->common);
Jeff Cody747ff602012-09-27 13:29:13 -0400428}
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200429
430
431#define COMMIT_BUF_SECTORS 2048
432
433/* commit COW file into the raw image */
434int bdrv_commit(BlockDriverState *bs)
435{
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200436 BlockBackend *src, *backing;
Kevin Wolfd3f06752017-01-19 18:16:03 +0100437 BlockDriverState *backing_file_bs = NULL;
438 BlockDriverState *commit_top_bs = NULL;
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200439 BlockDriver *drv = bs->drv;
440 int64_t sector, total_sectors, length, backing_length;
441 int n, ro, open_flags;
442 int ret = 0;
443 uint8_t *buf = NULL;
Kevin Wolfd3f06752017-01-19 18:16:03 +0100444 Error *local_err = NULL;
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200445
446 if (!drv)
447 return -ENOMEDIUM;
448
449 if (!bs->backing) {
450 return -ENOTSUP;
451 }
452
453 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
454 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
455 return -EBUSY;
456 }
457
458 ro = bs->backing->bs->read_only;
459 open_flags = bs->backing->bs->open_flags;
460
461 if (ro) {
462 if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
463 return -EACCES;
464 }
465 }
466
Kevin Wolfd3f06752017-01-19 18:16:03 +0100467 src = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
468 backing = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
Kevin Wolfd7086422017-01-13 19:02:32 +0100469
Kevin Wolfd3f06752017-01-19 18:16:03 +0100470 ret = blk_insert_bs(src, bs, &local_err);
Kevin Wolfd7086422017-01-13 19:02:32 +0100471 if (ret < 0) {
Kevin Wolfd3f06752017-01-19 18:16:03 +0100472 error_report_err(local_err);
Kevin Wolfd7086422017-01-13 19:02:32 +0100473 goto ro_cleanup;
474 }
475
Kevin Wolfd3f06752017-01-19 18:16:03 +0100476 /* Insert commit_top block node above backing, so we can write to it */
477 backing_file_bs = backing_bs(bs);
478
479 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, NULL, BDRV_O_RDWR,
480 &local_err);
481 if (commit_top_bs == NULL) {
482 error_report_err(local_err);
483 goto ro_cleanup;
484 }
485
Kevin Wolf12fa4af2017-02-17 20:42:32 +0100486 bdrv_set_backing_hd(commit_top_bs, backing_file_bs, &error_abort);
487 bdrv_set_backing_hd(bs, commit_top_bs, &error_abort);
Kevin Wolfd3f06752017-01-19 18:16:03 +0100488
489 ret = blk_insert_bs(backing, backing_file_bs, &local_err);
Kevin Wolfd7086422017-01-13 19:02:32 +0100490 if (ret < 0) {
Kevin Wolfd3f06752017-01-19 18:16:03 +0100491 error_report_err(local_err);
Kevin Wolfd7086422017-01-13 19:02:32 +0100492 goto ro_cleanup;
493 }
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200494
495 length = blk_getlength(src);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200496 if (length < 0) {
497 ret = length;
498 goto ro_cleanup;
499 }
500
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200501 backing_length = blk_getlength(backing);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200502 if (backing_length < 0) {
503 ret = backing_length;
504 goto ro_cleanup;
505 }
506
507 /* If our top snapshot is larger than the backing file image,
508 * grow the backing file image if possible. If not possible,
509 * we must return an error */
510 if (length > backing_length) {
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200511 ret = blk_truncate(backing, length);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200512 if (ret < 0) {
513 goto ro_cleanup;
514 }
515 }
516
517 total_sectors = length >> BDRV_SECTOR_BITS;
518
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200519 /* blk_try_blockalign() for src will choose an alignment that works for
520 * backing as well, so no need to compare the alignment manually. */
521 buf = blk_try_blockalign(src, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200522 if (buf == NULL) {
523 ret = -ENOMEM;
524 goto ro_cleanup;
525 }
526
527 for (sector = 0; sector < total_sectors; sector += n) {
528 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
529 if (ret < 0) {
530 goto ro_cleanup;
531 }
532 if (ret) {
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200533 ret = blk_pread(src, sector * BDRV_SECTOR_SIZE, buf,
534 n * BDRV_SECTOR_SIZE);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200535 if (ret < 0) {
536 goto ro_cleanup;
537 }
538
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200539 ret = blk_pwrite(backing, sector * BDRV_SECTOR_SIZE, buf,
540 n * BDRV_SECTOR_SIZE, 0);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200541 if (ret < 0) {
542 goto ro_cleanup;
543 }
544 }
545 }
546
547 if (drv->bdrv_make_empty) {
548 ret = drv->bdrv_make_empty(bs);
549 if (ret < 0) {
550 goto ro_cleanup;
551 }
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200552 blk_flush(src);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200553 }
554
555 /*
556 * Make sure all data we wrote to the backing device is actually
557 * stable on disk.
558 */
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200559 blk_flush(backing);
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200560
561 ret = 0;
562ro_cleanup:
563 qemu_vfree(buf);
564
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200565 blk_unref(backing);
Kevin Wolfd3f06752017-01-19 18:16:03 +0100566 if (backing_file_bs) {
Kevin Wolf12fa4af2017-02-17 20:42:32 +0100567 bdrv_set_backing_hd(bs, backing_file_bs, &error_abort);
Kevin Wolfd3f06752017-01-19 18:16:03 +0100568 }
569 bdrv_unref(commit_top_bs);
570 blk_unref(src);
Kevin Wolff8e2bd52016-05-30 16:29:47 +0200571
Kevin Wolf83fd6dd2016-05-30 15:53:15 +0200572 if (ro) {
573 /* ignoring error return here */
574 bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
575 }
576
577 return ret;
578}