blob: 2227fc2e6c7aa83c6dcfbb31382f9a32271b0118 [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
15#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010016#include "block/block_int.h"
17#include "block/blockjob.h"
Jeff Cody747ff602012-09-27 13:29:13 -040018#include "qemu/ratelimit.h"
19
20enum {
21 /*
22 * Size of data buffer for populating the image file. This should be large
23 * enough to process multiple clusters in a single call, so that populating
24 * contiguous regions of the image is efficient.
25 */
26 COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */
27};
28
29#define SLICE_TIME 100000000ULL /* ns */
30
31typedef struct CommitBlockJob {
32 BlockJob common;
33 RateLimit limit;
34 BlockDriverState *active;
35 BlockDriverState *top;
36 BlockDriverState *base;
Paolo Bonzini92aa5c62012-09-28 17:22:55 +020037 BlockdevOnError on_error;
Jeff Cody747ff602012-09-27 13:29:13 -040038 int base_flags;
39 int orig_overlay_flags;
40} CommitBlockJob;
41
42static int coroutine_fn commit_populate(BlockDriverState *bs,
43 BlockDriverState *base,
44 int64_t sector_num, int nb_sectors,
45 void *buf)
46{
47 int ret = 0;
48
49 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
50 if (ret) {
51 return ret;
52 }
53
54 ret = bdrv_write(base, sector_num, buf, nb_sectors);
55 if (ret) {
56 return ret;
57 }
58
59 return 0;
60}
61
62static void coroutine_fn commit_run(void *opaque)
63{
64 CommitBlockJob *s = opaque;
65 BlockDriverState *active = s->active;
66 BlockDriverState *top = s->top;
67 BlockDriverState *base = s->base;
Jeff Cody6d759112013-01-15 10:47:24 -050068 BlockDriverState *overlay_bs;
Jeff Cody747ff602012-09-27 13:29:13 -040069 int64_t sector_num, end;
70 int ret = 0;
71 int n = 0;
72 void *buf;
73 int bytes_written = 0;
74 int64_t base_len;
75
76 ret = s->common.len = bdrv_getlength(top);
77
78
79 if (s->common.len < 0) {
80 goto exit_restore_reopen;
81 }
82
83 ret = base_len = bdrv_getlength(base);
84 if (base_len < 0) {
85 goto exit_restore_reopen;
86 }
87
88 if (base_len < s->common.len) {
89 ret = bdrv_truncate(base, s->common.len);
90 if (ret) {
91 goto exit_restore_reopen;
92 }
93 }
94
Jeff Cody747ff602012-09-27 13:29:13 -040095 end = s->common.len >> BDRV_SECTOR_BITS;
96 buf = qemu_blockalign(top, COMMIT_BUFFER_SIZE);
97
98 for (sector_num = 0; sector_num < end; sector_num += n) {
99 uint64_t delay_ns = 0;
100 bool copy;
101
102wait:
103 /* Note that even when no rate limit is applied we need to yield
Kevin Wolfc57b6652012-11-13 16:35:13 +0100104 * with no pending I/O here so that bdrv_drain_all() returns.
Jeff Cody747ff602012-09-27 13:29:13 -0400105 */
106 block_job_sleep_ns(&s->common, rt_clock, delay_ns);
107 if (block_job_is_cancelled(&s->common)) {
108 break;
109 }
110 /* Copy if allocated above the base */
111 ret = bdrv_co_is_allocated_above(top, base, sector_num,
112 COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
113 &n);
114 copy = (ret == 1);
115 trace_commit_one_iteration(s, sector_num, n, ret);
116 if (copy) {
117 if (s->common.speed) {
118 delay_ns = ratelimit_calculate_delay(&s->limit, n);
119 if (delay_ns > 0) {
120 goto wait;
121 }
122 }
123 ret = commit_populate(top, base, sector_num, n, buf);
124 bytes_written += n * BDRV_SECTOR_SIZE;
125 }
126 if (ret < 0) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200127 if (s->on_error == BLOCKDEV_ON_ERROR_STOP ||
128 s->on_error == BLOCKDEV_ON_ERROR_REPORT||
129 (s->on_error == BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC)) {
Jeff Cody747ff602012-09-27 13:29:13 -0400130 goto exit_free_buf;
131 } else {
132 n = 0;
133 continue;
134 }
135 }
136 /* Publish progress */
137 s->common.offset += n * BDRV_SECTOR_SIZE;
138 }
139
140 ret = 0;
141
142 if (!block_job_is_cancelled(&s->common) && sector_num == end) {
143 /* success */
144 ret = bdrv_drop_intermediate(active, top, base);
145 }
146
147exit_free_buf:
148 qemu_vfree(buf);
149
150exit_restore_reopen:
151 /* restore base open flags here if appropriate (e.g., change the base back
152 * to r/o). These reopens do not need to be atomic, since we won't abort
153 * even on failure here */
154 if (s->base_flags != bdrv_get_flags(base)) {
155 bdrv_reopen(base, s->base_flags, NULL);
156 }
Jeff Cody6d759112013-01-15 10:47:24 -0500157 overlay_bs = bdrv_find_overlay(active, top);
158 if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
Jeff Cody747ff602012-09-27 13:29:13 -0400159 bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
160 }
161
Paolo Bonzini65f46322012-10-18 16:49:20 +0200162 block_job_completed(&s->common, ret);
Jeff Cody747ff602012-09-27 13:29:13 -0400163}
164
165static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
166{
167 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
168
169 if (speed < 0) {
170 error_set(errp, QERR_INVALID_PARAMETER, "speed");
171 return;
172 }
173 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
174}
175
Kevin Wolff59fee82013-06-25 15:13:43 +0200176static const BlockJobType commit_job_type = {
Jeff Cody747ff602012-09-27 13:29:13 -0400177 .instance_size = sizeof(CommitBlockJob),
178 .job_type = "commit",
179 .set_speed = commit_set_speed,
180};
181
182void commit_start(BlockDriverState *bs, BlockDriverState *base,
183 BlockDriverState *top, int64_t speed,
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200184 BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
Jeff Cody747ff602012-09-27 13:29:13 -0400185 void *opaque, Error **errp)
186{
187 CommitBlockJob *s;
188 BlockReopenQueue *reopen_queue = NULL;
189 int orig_overlay_flags;
190 int orig_base_flags;
191 BlockDriverState *overlay_bs;
192 Error *local_err = NULL;
193
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200194 if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
195 on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
Jeff Cody747ff602012-09-27 13:29:13 -0400196 !bdrv_iostatus_is_enabled(bs)) {
197 error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
198 return;
199 }
200
201 /* Once we support top == active layer, remove this check */
202 if (top == bs) {
203 error_setg(errp,
204 "Top image as the active layer is currently unsupported");
205 return;
206 }
207
208 if (top == base) {
209 error_setg(errp, "Invalid files for merge: top and base are the same");
210 return;
211 }
212
Jeff Cody747ff602012-09-27 13:29:13 -0400213 overlay_bs = bdrv_find_overlay(bs, top);
214
215 if (overlay_bs == NULL) {
216 error_setg(errp, "Could not find overlay image for %s:", top->filename);
217 return;
218 }
219
220 orig_base_flags = bdrv_get_flags(base);
221 orig_overlay_flags = bdrv_get_flags(overlay_bs);
222
223 /* convert base & overlay_bs to r/w, if necessary */
224 if (!(orig_base_flags & BDRV_O_RDWR)) {
225 reopen_queue = bdrv_reopen_queue(reopen_queue, base,
226 orig_base_flags | BDRV_O_RDWR);
227 }
228 if (!(orig_overlay_flags & BDRV_O_RDWR)) {
229 reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs,
230 orig_overlay_flags | BDRV_O_RDWR);
231 }
232 if (reopen_queue) {
233 bdrv_reopen_multiple(reopen_queue, &local_err);
234 if (local_err != NULL) {
235 error_propagate(errp, local_err);
236 return;
237 }
238 }
239
240
241 s = block_job_create(&commit_job_type, bs, speed, cb, opaque, errp);
242 if (!s) {
243 return;
244 }
245
246 s->base = base;
247 s->top = top;
248 s->active = bs;
249
250 s->base_flags = orig_base_flags;
251 s->orig_overlay_flags = orig_overlay_flags;
252
253 s->on_error = on_error;
254 s->common.co = qemu_coroutine_create(commit_run);
255
256 trace_commit_start(bs, base, top, s, s->common.co, opaque);
257 qemu_coroutine_enter(s->common.co, s);
258}