4 * Copyright Red Hat, Inc. 2012
8 * Based on stream.c by Stefan Hajnoczi
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.
16 #include "block/block_int.h"
17 #include "block/blockjob.h"
18 #include "qemu/ratelimit.h"
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.
26 COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */
29 #define SLICE_TIME 100000000ULL /* ns */
31 typedef struct CommitBlockJob {
34 BlockDriverState *active;
35 BlockDriverState *top;
36 BlockDriverState *base;
37 BlockdevOnError on_error;
39 int orig_overlay_flags;
40 char *backing_file_str;
43 static int coroutine_fn commit_populate(BlockDriverState *bs,
44 BlockDriverState *base,
45 int64_t sector_num, int nb_sectors,
50 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
55 ret = bdrv_write(base, sector_num, buf, nb_sectors);
67 static void commit_complete(BlockJob *job, void *opaque)
69 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
70 CommitCompleteData *data = opaque;
71 BlockDriverState *active = s->active;
72 BlockDriverState *top = s->top;
73 BlockDriverState *base = s->base;
74 BlockDriverState *overlay_bs;
77 if (!block_job_is_cancelled(&s->common) && ret == 0) {
79 ret = bdrv_drop_intermediate(active, top, base, s->backing_file_str);
82 /* restore base open flags here if appropriate (e.g., change the base back
83 * to r/o). These reopens do not need to be atomic, since we won't abort
84 * even on failure here */
85 if (s->base_flags != bdrv_get_flags(base)) {
86 bdrv_reopen(base, s->base_flags, NULL);
88 overlay_bs = bdrv_find_overlay(active, top);
89 if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
90 bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
92 g_free(s->backing_file_str);
93 block_job_completed(&s->common, ret);
97 static void coroutine_fn commit_run(void *opaque)
99 CommitBlockJob *s = opaque;
100 CommitCompleteData *data;
101 BlockDriverState *top = s->top;
102 BlockDriverState *base = s->base;
103 int64_t sector_num, end;
107 int bytes_written = 0;
110 ret = s->common.len = bdrv_getlength(top);
113 if (s->common.len < 0) {
117 ret = base_len = bdrv_getlength(base);
122 if (base_len < s->common.len) {
123 ret = bdrv_truncate(base, s->common.len);
129 end = s->common.len >> BDRV_SECTOR_BITS;
130 buf = qemu_blockalign(top, COMMIT_BUFFER_SIZE);
132 for (sector_num = 0; sector_num < end; sector_num += n) {
133 uint64_t delay_ns = 0;
137 /* Note that even when no rate limit is applied we need to yield
138 * with no pending I/O here so that bdrv_drain_all() returns.
140 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
141 if (block_job_is_cancelled(&s->common)) {
144 /* Copy if allocated above the base */
145 ret = bdrv_is_allocated_above(top, base, sector_num,
146 COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
149 trace_commit_one_iteration(s, sector_num, n, ret);
151 if (s->common.speed) {
152 delay_ns = ratelimit_calculate_delay(&s->limit, n);
157 ret = commit_populate(top, base, sector_num, n, buf);
158 bytes_written += n * BDRV_SECTOR_SIZE;
161 if (s->on_error == BLOCKDEV_ON_ERROR_STOP ||
162 s->on_error == BLOCKDEV_ON_ERROR_REPORT||
163 (s->on_error == BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC)) {
170 /* Publish progress */
171 s->common.offset += n * BDRV_SECTOR_SIZE;
179 data = g_malloc(sizeof(*data));
181 block_job_defer_to_main_loop(&s->common, commit_complete, data);
184 static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
186 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
189 error_set(errp, QERR_INVALID_PARAMETER, "speed");
192 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
195 static const BlockJobDriver commit_job_driver = {
196 .instance_size = sizeof(CommitBlockJob),
197 .job_type = BLOCK_JOB_TYPE_COMMIT,
198 .set_speed = commit_set_speed,
201 void commit_start(BlockDriverState *bs, BlockDriverState *base,
202 BlockDriverState *top, int64_t speed,
203 BlockdevOnError on_error, BlockCompletionFunc *cb,
204 void *opaque, const char *backing_file_str, Error **errp)
207 BlockReopenQueue *reopen_queue = NULL;
208 int orig_overlay_flags;
210 BlockDriverState *overlay_bs;
211 Error *local_err = NULL;
213 if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
214 on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
215 !bdrv_iostatus_is_enabled(bs)) {
216 error_setg(errp, "Invalid parameter combination");
222 error_setg(errp, "Invalid files for merge: top and base are the same");
226 overlay_bs = bdrv_find_overlay(bs, top);
228 if (overlay_bs == NULL) {
229 error_setg(errp, "Could not find overlay image for %s:", top->filename);
233 orig_base_flags = bdrv_get_flags(base);
234 orig_overlay_flags = bdrv_get_flags(overlay_bs);
236 /* convert base & overlay_bs to r/w, if necessary */
237 if (!(orig_base_flags & BDRV_O_RDWR)) {
238 reopen_queue = bdrv_reopen_queue(reopen_queue, base,
239 orig_base_flags | BDRV_O_RDWR);
241 if (!(orig_overlay_flags & BDRV_O_RDWR)) {
242 reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs,
243 orig_overlay_flags | BDRV_O_RDWR);
246 bdrv_reopen_multiple(reopen_queue, &local_err);
247 if (local_err != NULL) {
248 error_propagate(errp, local_err);
254 s = block_job_create(&commit_job_driver, bs, speed, cb, opaque, errp);
263 s->base_flags = orig_base_flags;
264 s->orig_overlay_flags = orig_overlay_flags;
266 s->backing_file_str = g_strdup(backing_file_str);
268 s->on_error = on_error;
269 s->common.co = qemu_coroutine_create(commit_run);
271 trace_commit_start(bs, base, top, s, s->common.co, opaque);
272 qemu_coroutine_enter(s->common.co, s);