]> git.proxmox.com Git - mirror_qemu.git/blame - block/commit.c
include/qemu/osdep.h: Don't include qapi/error.h
[mirror_qemu.git] / block / commit.c
CommitLineData
747ff602
JC
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
80c71a24 15#include "qemu/osdep.h"
747ff602 16#include "trace.h"
737e150e
PB
17#include "block/block_int.h"
18#include "block/blockjob.h"
da34e65c 19#include "qapi/error.h"
cc7a8ea7 20#include "qapi/qmp/qerror.h"
747ff602 21#include "qemu/ratelimit.h"
373340b2 22#include "sysemu/block-backend.h"
747ff602
JC
23
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;
39 BlockDriverState *top;
40 BlockDriverState *base;
92aa5c6d 41 BlockdevOnError on_error;
747ff602
JC
42 int base_flags;
43 int orig_overlay_flags;
54e26900 44 char *backing_file_str;
747ff602
JC
45} CommitBlockJob;
46
47static int coroutine_fn commit_populate(BlockDriverState *bs,
48 BlockDriverState *base,
49 int64_t sector_num, int nb_sectors,
50 void *buf)
51{
52 int ret = 0;
53
54 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
55 if (ret) {
56 return ret;
57 }
58
59 ret = bdrv_write(base, sector_num, buf, nb_sectors);
60 if (ret) {
61 return ret;
62 }
63
64 return 0;
65}
66
9e85cd5c
SH
67typedef struct {
68 int ret;
69} CommitCompleteData;
70
71static void commit_complete(BlockJob *job, void *opaque)
747ff602 72{
9e85cd5c
SH
73 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
74 CommitCompleteData *data = opaque;
747ff602
JC
75 BlockDriverState *active = s->active;
76 BlockDriverState *top = s->top;
77 BlockDriverState *base = s->base;
6d759117 78 BlockDriverState *overlay_bs;
9e85cd5c
SH
79 int ret = data->ret;
80
81 if (!block_job_is_cancelled(&s->common) && ret == 0) {
82 /* success */
83 ret = bdrv_drop_intermediate(active, top, base, s->backing_file_str);
84 }
85
86 /* restore base open flags here if appropriate (e.g., change the base back
87 * to r/o). These reopens do not need to be atomic, since we won't abort
88 * even on failure here */
89 if (s->base_flags != bdrv_get_flags(base)) {
90 bdrv_reopen(base, s->base_flags, NULL);
91 }
92 overlay_bs = bdrv_find_overlay(active, top);
93 if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
94 bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
95 }
96 g_free(s->backing_file_str);
97 block_job_completed(&s->common, ret);
98 g_free(data);
99}
100
101static void coroutine_fn commit_run(void *opaque)
102{
103 CommitBlockJob *s = opaque;
104 CommitCompleteData *data;
105 BlockDriverState *top = s->top;
106 BlockDriverState *base = s->base;
747ff602
JC
107 int64_t sector_num, end;
108 int ret = 0;
109 int n = 0;
9e85cd5c 110 void *buf = NULL;
747ff602
JC
111 int bytes_written = 0;
112 int64_t base_len;
113
114 ret = s->common.len = bdrv_getlength(top);
115
116
117 if (s->common.len < 0) {
9e85cd5c 118 goto out;
747ff602
JC
119 }
120
121 ret = base_len = bdrv_getlength(base);
122 if (base_len < 0) {
9e85cd5c 123 goto out;
747ff602
JC
124 }
125
126 if (base_len < s->common.len) {
127 ret = bdrv_truncate(base, s->common.len);
128 if (ret) {
9e85cd5c 129 goto out;
747ff602
JC
130 }
131 }
132
747ff602
JC
133 end = s->common.len >> BDRV_SECTOR_BITS;
134 buf = qemu_blockalign(top, COMMIT_BUFFER_SIZE);
135
136 for (sector_num = 0; sector_num < end; sector_num += n) {
137 uint64_t delay_ns = 0;
138 bool copy;
139
140wait:
141 /* Note that even when no rate limit is applied we need to yield
c57b6656 142 * with no pending I/O here so that bdrv_drain_all() returns.
747ff602 143 */
7483d1e5 144 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
747ff602
JC
145 if (block_job_is_cancelled(&s->common)) {
146 break;
147 }
148 /* Copy if allocated above the base */
4f578637
PB
149 ret = bdrv_is_allocated_above(top, base, sector_num,
150 COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
151 &n);
747ff602
JC
152 copy = (ret == 1);
153 trace_commit_one_iteration(s, sector_num, n, ret);
154 if (copy) {
155 if (s->common.speed) {
156 delay_ns = ratelimit_calculate_delay(&s->limit, n);
157 if (delay_ns > 0) {
158 goto wait;
159 }
160 }
161 ret = commit_populate(top, base, sector_num, n, buf);
162 bytes_written += n * BDRV_SECTOR_SIZE;
163 }
164 if (ret < 0) {
92aa5c6d
PB
165 if (s->on_error == BLOCKDEV_ON_ERROR_STOP ||
166 s->on_error == BLOCKDEV_ON_ERROR_REPORT||
167 (s->on_error == BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC)) {
9e85cd5c 168 goto out;
747ff602
JC
169 } else {
170 n = 0;
171 continue;
172 }
173 }
174 /* Publish progress */
175 s->common.offset += n * BDRV_SECTOR_SIZE;
176 }
177
178 ret = 0;
179
9e85cd5c 180out:
747ff602
JC
181 qemu_vfree(buf);
182
9e85cd5c
SH
183 data = g_malloc(sizeof(*data));
184 data->ret = ret;
185 block_job_defer_to_main_loop(&s->common, commit_complete, data);
747ff602
JC
186}
187
188static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
189{
190 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
191
192 if (speed < 0) {
c6bd8c70 193 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
747ff602
JC
194 return;
195 }
196 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
197}
198
3fc4b10a 199static const BlockJobDriver commit_job_driver = {
747ff602 200 .instance_size = sizeof(CommitBlockJob),
79e14bf7 201 .job_type = BLOCK_JOB_TYPE_COMMIT,
747ff602
JC
202 .set_speed = commit_set_speed,
203};
204
205void commit_start(BlockDriverState *bs, BlockDriverState *base,
206 BlockDriverState *top, int64_t speed,
097310b5 207 BlockdevOnError on_error, BlockCompletionFunc *cb,
54e26900 208 void *opaque, const char *backing_file_str, Error **errp)
747ff602
JC
209{
210 CommitBlockJob *s;
211 BlockReopenQueue *reopen_queue = NULL;
212 int orig_overlay_flags;
213 int orig_base_flags;
214 BlockDriverState *overlay_bs;
215 Error *local_err = NULL;
216
92aa5c6d
PB
217 if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
218 on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
373340b2 219 (!bs->blk || !blk_iostatus_is_enabled(bs->blk))) {
f231b88d 220 error_setg(errp, "Invalid parameter combination");
747ff602
JC
221 return;
222 }
223
18da7f94 224 assert(top != bs);
747ff602
JC
225 if (top == base) {
226 error_setg(errp, "Invalid files for merge: top and base are the same");
227 return;
228 }
229
747ff602
JC
230 overlay_bs = bdrv_find_overlay(bs, top);
231
232 if (overlay_bs == NULL) {
233 error_setg(errp, "Could not find overlay image for %s:", top->filename);
234 return;
235 }
236
237 orig_base_flags = bdrv_get_flags(base);
238 orig_overlay_flags = bdrv_get_flags(overlay_bs);
239
240 /* convert base & overlay_bs to r/w, if necessary */
747ff602 241 if (!(orig_overlay_flags & BDRV_O_RDWR)) {
4d2cb092 242 reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL,
747ff602
JC
243 orig_overlay_flags | BDRV_O_RDWR);
244 }
3db2bd55
AG
245 if (!(orig_base_flags & BDRV_O_RDWR)) {
246 reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL,
247 orig_base_flags | BDRV_O_RDWR);
248 }
747ff602
JC
249 if (reopen_queue) {
250 bdrv_reopen_multiple(reopen_queue, &local_err);
251 if (local_err != NULL) {
252 error_propagate(errp, local_err);
253 return;
254 }
255 }
256
257
3fc4b10a 258 s = block_job_create(&commit_job_driver, bs, speed, cb, opaque, errp);
747ff602
JC
259 if (!s) {
260 return;
261 }
262
263 s->base = base;
264 s->top = top;
265 s->active = bs;
266
267 s->base_flags = orig_base_flags;
268 s->orig_overlay_flags = orig_overlay_flags;
269
54e26900
JC
270 s->backing_file_str = g_strdup(backing_file_str);
271
747ff602
JC
272 s->on_error = on_error;
273 s->common.co = qemu_coroutine_create(commit_run);
274
275 trace_commit_start(bs, base, top, s, s->common.co, opaque);
276 qemu_coroutine_enter(s->common.co, s);
277}