]>
Commit | Line | Data |
---|---|---|
747ff602 JC |
1 | /* |
2 | * Live block commit | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Jeff Cody <[email protected]> | |
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 | |
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; | |
4653456a KW |
39 | BlockBackend *top; |
40 | BlockBackend *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 | ||
4653456a | 47 | static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base, |
747ff602 JC |
48 | int64_t sector_num, int nb_sectors, |
49 | void *buf) | |
50 | { | |
51 | int ret = 0; | |
4653456a KW |
52 | QEMUIOVector qiov; |
53 | struct iovec iov = { | |
54 | .iov_base = buf, | |
55 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, | |
56 | }; | |
747ff602 | 57 | |
4653456a KW |
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) { | |
747ff602 JC |
63 | return ret; |
64 | } | |
65 | ||
4653456a KW |
66 | ret = blk_co_pwritev(base, sector_num * BDRV_SECTOR_SIZE, |
67 | qiov.size, &qiov, 0); | |
68 | if (ret < 0) { | |
747ff602 JC |
69 | return ret; |
70 | } | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
9e85cd5c SH |
75 | typedef struct { |
76 | int ret; | |
77 | } CommitCompleteData; | |
78 | ||
79 | static void commit_complete(BlockJob *job, void *opaque) | |
747ff602 | 80 | { |
9e85cd5c SH |
81 | CommitBlockJob *s = container_of(job, CommitBlockJob, common); |
82 | CommitCompleteData *data = opaque; | |
747ff602 | 83 | BlockDriverState *active = s->active; |
4653456a KW |
84 | BlockDriverState *top = blk_bs(s->top); |
85 | BlockDriverState *base = blk_bs(s->base); | |
6d759117 | 86 | BlockDriverState *overlay_bs; |
9e85cd5c SH |
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); | |
4653456a KW |
105 | blk_unref(s->top); |
106 | blk_unref(s->base); | |
9e85cd5c SH |
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; | |
747ff602 JC |
115 | int64_t sector_num, end; |
116 | int ret = 0; | |
117 | int n = 0; | |
9e85cd5c | 118 | void *buf = NULL; |
747ff602 JC |
119 | int bytes_written = 0; |
120 | int64_t base_len; | |
121 | ||
4653456a | 122 | ret = s->common.len = blk_getlength(s->top); |
747ff602 JC |
123 | |
124 | ||
125 | if (s->common.len < 0) { | |
9e85cd5c | 126 | goto out; |
747ff602 JC |
127 | } |
128 | ||
4653456a | 129 | ret = base_len = blk_getlength(s->base); |
747ff602 | 130 | if (base_len < 0) { |
9e85cd5c | 131 | goto out; |
747ff602 JC |
132 | } |
133 | ||
134 | if (base_len < s->common.len) { | |
4653456a | 135 | ret = blk_truncate(s->base, s->common.len); |
747ff602 | 136 | if (ret) { |
9e85cd5c | 137 | goto out; |
747ff602 JC |
138 | } |
139 | } | |
140 | ||
747ff602 | 141 | end = s->common.len >> BDRV_SECTOR_BITS; |
4653456a | 142 | buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE); |
747ff602 JC |
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 | |
c57b6656 | 150 | * with no pending I/O here so that bdrv_drain_all() returns. |
747ff602 | 151 | */ |
7483d1e5 | 152 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
747ff602 JC |
153 | if (block_job_is_cancelled(&s->common)) { |
154 | break; | |
155 | } | |
156 | /* Copy if allocated above the base */ | |
4653456a KW |
157 | ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base), |
158 | sector_num, | |
4f578637 PB |
159 | COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE, |
160 | &n); | |
747ff602 JC |
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 | } | |
4653456a | 170 | ret = commit_populate(s->top, s->base, sector_num, n, buf); |
747ff602 JC |
171 | bytes_written += n * BDRV_SECTOR_SIZE; |
172 | } | |
173 | if (ret < 0) { | |
92aa5c6d PB |
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)) { | |
9e85cd5c | 177 | goto out; |
747ff602 JC |
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 | ||
9e85cd5c | 189 | out: |
747ff602 JC |
190 | qemu_vfree(buf); |
191 | ||
9e85cd5c SH |
192 | data = g_malloc(sizeof(*data)); |
193 | data->ret = ret; | |
194 | block_job_defer_to_main_loop(&s->common, commit_complete, data); | |
747ff602 JC |
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) { | |
c6bd8c70 | 202 | error_setg(errp, QERR_INVALID_PARAMETER, "speed"); |
747ff602 JC |
203 | return; |
204 | } | |
205 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); | |
206 | } | |
207 | ||
3fc4b10a | 208 | static const BlockJobDriver commit_job_driver = { |
747ff602 | 209 | .instance_size = sizeof(CommitBlockJob), |
79e14bf7 | 210 | .job_type = BLOCK_JOB_TYPE_COMMIT, |
747ff602 JC |
211 | .set_speed = commit_set_speed, |
212 | }; | |
213 | ||
214 | void commit_start(BlockDriverState *bs, BlockDriverState *base, | |
215 | BlockDriverState *top, int64_t speed, | |
097310b5 | 216 | BlockdevOnError on_error, BlockCompletionFunc *cb, |
54e26900 | 217 | void *opaque, const char *backing_file_str, Error **errp) |
747ff602 JC |
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 | ||
18da7f94 | 226 | assert(top != bs); |
747ff602 JC |
227 | if (top == base) { |
228 | error_setg(errp, "Invalid files for merge: top and base are the same"); | |
229 | return; | |
230 | } | |
231 | ||
747ff602 JC |
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 | ||
834fe28d AG |
239 | s = block_job_create(&commit_job_driver, bs, speed, cb, opaque, errp); |
240 | if (!s) { | |
241 | return; | |
242 | } | |
243 | ||
747ff602 JC |
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 */ | |
747ff602 | 248 | if (!(orig_overlay_flags & BDRV_O_RDWR)) { |
4d2cb092 | 249 | reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL, |
747ff602 JC |
250 | orig_overlay_flags | BDRV_O_RDWR); |
251 | } | |
3db2bd55 AG |
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 | } | |
747ff602 JC |
256 | if (reopen_queue) { |
257 | bdrv_reopen_multiple(reopen_queue, &local_err); | |
258 | if (local_err != NULL) { | |
259 | error_propagate(errp, local_err); | |
834fe28d | 260 | block_job_unref(&s->common); |
747ff602 JC |
261 | return; |
262 | } | |
263 | } | |
264 | ||
265 | ||
4653456a KW |
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 | ||
747ff602 JC |
272 | s->active = bs; |
273 | ||
274 | s->base_flags = orig_base_flags; | |
275 | s->orig_overlay_flags = orig_overlay_flags; | |
276 | ||
54e26900 JC |
277 | s->backing_file_str = g_strdup(backing_file_str); |
278 | ||
747ff602 JC |
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 | } | |
83fd6dd3 KW |
285 | |
286 | ||
287 | #define COMMIT_BUF_SECTORS 2048 | |
288 | ||
289 | /* commit COW file into the raw image */ | |
290 | int bdrv_commit(BlockDriverState *bs) | |
291 | { | |
f8e2bd53 | 292 | BlockBackend *src, *backing; |
83fd6dd3 KW |
293 | BlockDriver *drv = bs->drv; |
294 | int64_t sector, total_sectors, length, backing_length; | |
295 | int n, ro, open_flags; | |
296 | int ret = 0; | |
297 | uint8_t *buf = NULL; | |
298 | ||
299 | if (!drv) | |
300 | return -ENOMEDIUM; | |
301 | ||
302 | if (!bs->backing) { | |
303 | return -ENOTSUP; | |
304 | } | |
305 | ||
306 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) || | |
307 | bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) { | |
308 | return -EBUSY; | |
309 | } | |
310 | ||
311 | ro = bs->backing->bs->read_only; | |
312 | open_flags = bs->backing->bs->open_flags; | |
313 | ||
314 | if (ro) { | |
315 | if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) { | |
316 | return -EACCES; | |
317 | } | |
318 | } | |
319 | ||
f8e2bd53 KW |
320 | src = blk_new(); |
321 | blk_insert_bs(src, bs); | |
322 | ||
323 | backing = blk_new(); | |
324 | blk_insert_bs(backing, bs->backing->bs); | |
325 | ||
326 | length = blk_getlength(src); | |
83fd6dd3 KW |
327 | if (length < 0) { |
328 | ret = length; | |
329 | goto ro_cleanup; | |
330 | } | |
331 | ||
f8e2bd53 | 332 | backing_length = blk_getlength(backing); |
83fd6dd3 KW |
333 | if (backing_length < 0) { |
334 | ret = backing_length; | |
335 | goto ro_cleanup; | |
336 | } | |
337 | ||
338 | /* If our top snapshot is larger than the backing file image, | |
339 | * grow the backing file image if possible. If not possible, | |
340 | * we must return an error */ | |
341 | if (length > backing_length) { | |
f8e2bd53 | 342 | ret = blk_truncate(backing, length); |
83fd6dd3 KW |
343 | if (ret < 0) { |
344 | goto ro_cleanup; | |
345 | } | |
346 | } | |
347 | ||
348 | total_sectors = length >> BDRV_SECTOR_BITS; | |
349 | ||
f8e2bd53 KW |
350 | /* blk_try_blockalign() for src will choose an alignment that works for |
351 | * backing as well, so no need to compare the alignment manually. */ | |
352 | buf = blk_try_blockalign(src, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); | |
83fd6dd3 KW |
353 | if (buf == NULL) { |
354 | ret = -ENOMEM; | |
355 | goto ro_cleanup; | |
356 | } | |
357 | ||
358 | for (sector = 0; sector < total_sectors; sector += n) { | |
359 | ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); | |
360 | if (ret < 0) { | |
361 | goto ro_cleanup; | |
362 | } | |
363 | if (ret) { | |
f8e2bd53 KW |
364 | ret = blk_pread(src, sector * BDRV_SECTOR_SIZE, buf, |
365 | n * BDRV_SECTOR_SIZE); | |
83fd6dd3 KW |
366 | if (ret < 0) { |
367 | goto ro_cleanup; | |
368 | } | |
369 | ||
f8e2bd53 KW |
370 | ret = blk_pwrite(backing, sector * BDRV_SECTOR_SIZE, buf, |
371 | n * BDRV_SECTOR_SIZE, 0); | |
83fd6dd3 KW |
372 | if (ret < 0) { |
373 | goto ro_cleanup; | |
374 | } | |
375 | } | |
376 | } | |
377 | ||
378 | if (drv->bdrv_make_empty) { | |
379 | ret = drv->bdrv_make_empty(bs); | |
380 | if (ret < 0) { | |
381 | goto ro_cleanup; | |
382 | } | |
f8e2bd53 | 383 | blk_flush(src); |
83fd6dd3 KW |
384 | } |
385 | ||
386 | /* | |
387 | * Make sure all data we wrote to the backing device is actually | |
388 | * stable on disk. | |
389 | */ | |
f8e2bd53 | 390 | blk_flush(backing); |
83fd6dd3 KW |
391 | |
392 | ret = 0; | |
393 | ro_cleanup: | |
394 | qemu_vfree(buf); | |
395 | ||
f8e2bd53 KW |
396 | blk_unref(src); |
397 | blk_unref(backing); | |
398 | ||
83fd6dd3 KW |
399 | if (ro) { |
400 | /* ignoring error return here */ | |
401 | bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL); | |
402 | } | |
403 | ||
404 | return ret; | |
405 | } |