xref: /openbmc/qemu/block/commit.c (revision dd9fe29c)
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 
15 #include "trace.h"
16 #include "block/block_int.h"
17 #include "block/blockjob.h"
18 #include "qemu/ratelimit.h"
19 
20 enum {
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 
31 typedef struct CommitBlockJob {
32     BlockJob common;
33     RateLimit limit;
34     BlockDriverState *active;
35     BlockDriverState *top;
36     BlockDriverState *base;
37     BlockdevOnError on_error;
38     int base_flags;
39     int orig_overlay_flags;
40     char *backing_file_str;
41 } CommitBlockJob;
42 
43 static int coroutine_fn commit_populate(BlockDriverState *bs,
44                                         BlockDriverState *base,
45                                         int64_t sector_num, int nb_sectors,
46                                         void *buf)
47 {
48     int ret = 0;
49 
50     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
51     if (ret) {
52         return ret;
53     }
54 
55     ret = bdrv_write(base, sector_num, buf, nb_sectors);
56     if (ret) {
57         return ret;
58     }
59 
60     return 0;
61 }
62 
63 typedef struct {
64     int ret;
65 } CommitCompleteData;
66 
67 static void commit_complete(BlockJob *job, void *opaque)
68 {
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;
75     int ret = data->ret;
76 
77     if (!block_job_is_cancelled(&s->common) && ret == 0) {
78         /* success */
79         ret = bdrv_drop_intermediate(active, top, base, s->backing_file_str);
80     }
81 
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);
87     }
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);
91     }
92     g_free(s->backing_file_str);
93     block_job_completed(&s->common, ret);
94     g_free(data);
95 }
96 
97 static void coroutine_fn commit_run(void *opaque)
98 {
99     CommitBlockJob *s = opaque;
100     CommitCompleteData *data;
101     BlockDriverState *top = s->top;
102     BlockDriverState *base = s->base;
103     int64_t sector_num, end;
104     int ret = 0;
105     int n = 0;
106     void *buf = NULL;
107     int bytes_written = 0;
108     int64_t base_len;
109 
110     ret = s->common.len = bdrv_getlength(top);
111 
112 
113     if (s->common.len < 0) {
114         goto out;
115     }
116 
117     ret = base_len = bdrv_getlength(base);
118     if (base_len < 0) {
119         goto out;
120     }
121 
122     if (base_len < s->common.len) {
123         ret = bdrv_truncate(base, s->common.len);
124         if (ret) {
125             goto out;
126         }
127     }
128 
129     end = s->common.len >> BDRV_SECTOR_BITS;
130     buf = qemu_blockalign(top, COMMIT_BUFFER_SIZE);
131 
132     for (sector_num = 0; sector_num < end; sector_num += n) {
133         uint64_t delay_ns = 0;
134         bool copy;
135 
136 wait:
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.
139          */
140         block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
141         if (block_job_is_cancelled(&s->common)) {
142             break;
143         }
144         /* Copy if allocated above the base */
145         ret = bdrv_is_allocated_above(top, base, sector_num,
146                                       COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
147                                       &n);
148         copy = (ret == 1);
149         trace_commit_one_iteration(s, sector_num, n, ret);
150         if (copy) {
151             if (s->common.speed) {
152                 delay_ns = ratelimit_calculate_delay(&s->limit, n);
153                 if (delay_ns > 0) {
154                     goto wait;
155                 }
156             }
157             ret = commit_populate(top, base, sector_num, n, buf);
158             bytes_written += n * BDRV_SECTOR_SIZE;
159         }
160         if (ret < 0) {
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)) {
164                 goto out;
165             } else {
166                 n = 0;
167                 continue;
168             }
169         }
170         /* Publish progress */
171         s->common.offset += n * BDRV_SECTOR_SIZE;
172     }
173 
174     ret = 0;
175 
176 out:
177     qemu_vfree(buf);
178 
179     data = g_malloc(sizeof(*data));
180     data->ret = ret;
181     block_job_defer_to_main_loop(&s->common, commit_complete, data);
182 }
183 
184 static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
185 {
186     CommitBlockJob *s = container_of(job, CommitBlockJob, common);
187 
188     if (speed < 0) {
189         error_set(errp, QERR_INVALID_PARAMETER, "speed");
190         return;
191     }
192     ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
193 }
194 
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,
199 };
200 
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)
205 {
206     CommitBlockJob *s;
207     BlockReopenQueue *reopen_queue = NULL;
208     int orig_overlay_flags;
209     int orig_base_flags;
210     BlockDriverState *overlay_bs;
211     Error *local_err = NULL;
212 
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");
217         return;
218     }
219 
220     assert(top != bs);
221     if (top == base) {
222         error_setg(errp, "Invalid files for merge: top and base are the same");
223         return;
224     }
225 
226     overlay_bs = bdrv_find_overlay(bs, top);
227 
228     if (overlay_bs == NULL) {
229         error_setg(errp, "Could not find overlay image for %s:", top->filename);
230         return;
231     }
232 
233     orig_base_flags    = bdrv_get_flags(base);
234     orig_overlay_flags = bdrv_get_flags(overlay_bs);
235 
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);
240     }
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);
244     }
245     if (reopen_queue) {
246         bdrv_reopen_multiple(reopen_queue, &local_err);
247         if (local_err != NULL) {
248             error_propagate(errp, local_err);
249             return;
250         }
251     }
252 
253 
254     s = block_job_create(&commit_job_driver, bs, speed, cb, opaque, errp);
255     if (!s) {
256         return;
257     }
258 
259     s->base   = base;
260     s->top    = top;
261     s->active = bs;
262 
263     s->base_flags          = orig_base_flags;
264     s->orig_overlay_flags  = orig_overlay_flags;
265 
266     s->backing_file_str = g_strdup(backing_file_str);
267 
268     s->on_error = on_error;
269     s->common.co = qemu_coroutine_create(commit_run);
270 
271     trace_commit_start(bs, base, top, s, s->common.co, opaque);
272     qemu_coroutine_enter(s->common.co, s);
273 }
274