hw/i2c: QOM'ify exynos4210_i2c.c
[qemu/cris-port.git] / block / commit.c
blob8a00e1146c749bf0401345382fb4d3a2b867558c
1 /*
2 * Live block commit
4 * Copyright Red Hat, Inc. 2012
6 * Authors:
7 * Jeff Cody <jcody@redhat.com>
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.
15 #include "qemu/osdep.h"
16 #include "trace.h"
17 #include "block/block_int.h"
18 #include "block/blockjob.h"
19 #include "qapi/error.h"
20 #include "qapi/qmp/qerror.h"
21 #include "qemu/ratelimit.h"
22 #include "sysemu/block-backend.h"
24 enum {
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.
30 COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */
33 #define SLICE_TIME 100000000ULL /* ns */
35 typedef struct CommitBlockJob {
36 BlockJob common;
37 RateLimit limit;
38 BlockDriverState *active;
39 BlockBackend *top;
40 BlockBackend *base;
41 BlockdevOnError on_error;
42 int base_flags;
43 int orig_overlay_flags;
44 char *backing_file_str;
45 } CommitBlockJob;
47 static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base,
48 int64_t sector_num, int nb_sectors,
49 void *buf)
51 int ret = 0;
52 QEMUIOVector qiov;
53 struct iovec iov = {
54 .iov_base = buf,
55 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
58 qemu_iovec_init_external(&qiov, &iov, 1);
60 ret = blk_co_preadv(bs, sector_num * BDRV_SECTOR_SIZE,
61 qiov.size, &qiov, 0);
62 if (ret < 0) {
63 return ret;
66 ret = blk_co_pwritev(base, sector_num * BDRV_SECTOR_SIZE,
67 qiov.size, &qiov, 0);
68 if (ret < 0) {
69 return ret;
72 return 0;
75 typedef struct {
76 int ret;
77 } CommitCompleteData;
79 static void commit_complete(BlockJob *job, void *opaque)
81 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
82 CommitCompleteData *data = opaque;
83 BlockDriverState *active = s->active;
84 BlockDriverState *top = blk_bs(s->top);
85 BlockDriverState *base = blk_bs(s->base);
86 BlockDriverState *overlay_bs;
87 int ret = data->ret;
89 if (!block_job_is_cancelled(&s->common) && ret == 0) {
90 /* success */
91 ret = bdrv_drop_intermediate(active, top, base, s->backing_file_str);
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);
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);
104 g_free(s->backing_file_str);
105 blk_unref(s->top);
106 blk_unref(s->base);
107 block_job_completed(&s->common, ret);
108 g_free(data);
111 static void coroutine_fn commit_run(void *opaque)
113 CommitBlockJob *s = opaque;
114 CommitCompleteData *data;
115 int64_t sector_num, end;
116 int ret = 0;
117 int n = 0;
118 void *buf = NULL;
119 int bytes_written = 0;
120 int64_t base_len;
122 ret = s->common.len = blk_getlength(s->top);
125 if (s->common.len < 0) {
126 goto out;
129 ret = base_len = blk_getlength(s->base);
130 if (base_len < 0) {
131 goto out;
134 if (base_len < s->common.len) {
135 ret = blk_truncate(s->base, s->common.len);
136 if (ret) {
137 goto out;
141 end = s->common.len >> BDRV_SECTOR_BITS;
142 buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE);
144 for (sector_num = 0; sector_num < end; sector_num += n) {
145 uint64_t delay_ns = 0;
146 bool copy;
148 wait:
149 /* Note that even when no rate limit is applied we need to yield
150 * with no pending I/O here so that bdrv_drain_all() returns.
152 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
153 if (block_job_is_cancelled(&s->common)) {
154 break;
156 /* Copy if allocated above the base */
157 ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base),
158 sector_num,
159 COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
160 &n);
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;
170 ret = commit_populate(s->top, s->base, sector_num, n, buf);
171 bytes_written += n * BDRV_SECTOR_SIZE;
173 if (ret < 0) {
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)) {
177 goto out;
178 } else {
179 n = 0;
180 continue;
183 /* Publish progress */
184 s->common.offset += n * BDRV_SECTOR_SIZE;
187 ret = 0;
189 out:
190 qemu_vfree(buf);
192 data = g_malloc(sizeof(*data));
193 data->ret = ret;
194 block_job_defer_to_main_loop(&s->common, commit_complete, data);
197 static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
199 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
201 if (speed < 0) {
202 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
203 return;
205 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
208 static const BlockJobDriver commit_job_driver = {
209 .instance_size = sizeof(CommitBlockJob),
210 .job_type = BLOCK_JOB_TYPE_COMMIT,
211 .set_speed = commit_set_speed,
214 void commit_start(BlockDriverState *bs, BlockDriverState *base,
215 BlockDriverState *top, int64_t speed,
216 BlockdevOnError on_error, BlockCompletionFunc *cb,
217 void *opaque, const char *backing_file_str, Error **errp)
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;
226 assert(top != bs);
227 if (top == base) {
228 error_setg(errp, "Invalid files for merge: top and base are the same");
229 return;
232 overlay_bs = bdrv_find_overlay(bs, top);
234 if (overlay_bs == NULL) {
235 error_setg(errp, "Could not find overlay image for %s:", top->filename);
236 return;
239 orig_base_flags = bdrv_get_flags(base);
240 orig_overlay_flags = bdrv_get_flags(overlay_bs);
242 /* convert base & overlay_bs to r/w, if necessary */
243 if (!(orig_overlay_flags & BDRV_O_RDWR)) {
244 reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL,
245 orig_overlay_flags | BDRV_O_RDWR);
247 if (!(orig_base_flags & BDRV_O_RDWR)) {
248 reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL,
249 orig_base_flags | BDRV_O_RDWR);
251 if (reopen_queue) {
252 bdrv_reopen_multiple(reopen_queue, &local_err);
253 if (local_err != NULL) {
254 error_propagate(errp, local_err);
255 return;
260 s = block_job_create(&commit_job_driver, bs, speed, cb, opaque, errp);
261 if (!s) {
262 return;
265 s->base = blk_new();
266 blk_insert_bs(s->base, base);
268 s->top = blk_new();
269 blk_insert_bs(s->top, top);
271 s->active = bs;
273 s->base_flags = orig_base_flags;
274 s->orig_overlay_flags = orig_overlay_flags;
276 s->backing_file_str = g_strdup(backing_file_str);
278 s->on_error = on_error;
279 s->common.co = qemu_coroutine_create(commit_run);
281 trace_commit_start(bs, base, top, s, s->common.co, opaque);
282 qemu_coroutine_enter(s->common.co, s);