]>
Commit | Line | Data |
---|---|---|
4f1043b4 SH |
1 | /* |
2 | * Image streaming | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
10 | * See the COPYING.LIB file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
80c71a24 | 14 | #include "qemu/osdep.h" |
4f1043b4 | 15 | #include "trace.h" |
737e150e | 16 | #include "block/block_int.h" |
c87621ea | 17 | #include "block/blockjob_int.h" |
da34e65c | 18 | #include "qapi/error.h" |
cc7a8ea7 | 19 | #include "qapi/qmp/qerror.h" |
6ef228fc | 20 | #include "qemu/ratelimit.h" |
373340b2 | 21 | #include "sysemu/block-backend.h" |
4f1043b4 SH |
22 | |
23 | enum { | |
24 | /* | |
25 | * Size of data buffer for populating the image file. This should be large | |
26 | * enough to process multiple clusters in a single call, so that populating | |
27 | * contiguous regions of the image is efficient. | |
28 | */ | |
29 | STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */ | |
30 | }; | |
31 | ||
32 | typedef struct StreamBlockJob { | |
33 | BlockJob common; | |
34 | BlockDriverState *base; | |
1d809098 | 35 | BlockdevOnError on_error; |
13d8cc51 | 36 | char *backing_file_str; |
61b49e48 | 37 | int bs_flags; |
4f1043b4 SH |
38 | } StreamBlockJob; |
39 | ||
03e35d82 | 40 | static int coroutine_fn stream_populate(BlockBackend *blk, |
8493211c | 41 | int64_t offset, uint64_t bytes, |
4f1043b4 SH |
42 | void *buf) |
43 | { | |
44 | struct iovec iov = { | |
45 | .iov_base = buf, | |
8493211c | 46 | .iov_len = bytes, |
4f1043b4 SH |
47 | }; |
48 | QEMUIOVector qiov; | |
49 | ||
8493211c | 50 | assert(bytes < SIZE_MAX); |
4f1043b4 SH |
51 | qemu_iovec_init_external(&qiov, &iov, 1); |
52 | ||
53 | /* Copy-on-read the unallocated clusters */ | |
8493211c | 54 | return blk_co_preadv(blk, offset, qiov.size, &qiov, BDRV_REQ_COPY_ON_READ); |
4f1043b4 SH |
55 | } |
56 | ||
eb23654d | 57 | static void stream_exit(Job *job) |
f3e69beb | 58 | { |
1908a559 KW |
59 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); |
60 | BlockJob *bjob = &s->common; | |
1908a559 | 61 | BlockDriverState *bs = blk_bs(bjob->blk); |
f3e69beb | 62 | BlockDriverState *base = s->base; |
12fa4af6 | 63 | Error *local_err = NULL; |
eb23654d | 64 | int ret = job->ret; |
f3e69beb | 65 | |
eb23654d | 66 | if (!job_is_cancelled(job) && bs->backing && ret == 0) { |
f3e69beb SH |
67 | const char *base_id = NULL, *base_fmt = NULL; |
68 | if (base) { | |
69 | base_id = s->backing_file_str; | |
70 | if (base->drv) { | |
71 | base_fmt = base->drv->format_name; | |
72 | } | |
73 | } | |
eb23654d | 74 | ret = bdrv_change_backing_file(bs, base_id, base_fmt); |
12fa4af6 KW |
75 | bdrv_set_backing_hd(bs, base, &local_err); |
76 | if (local_err) { | |
77 | error_report_err(local_err); | |
eb23654d | 78 | ret = -EPERM; |
12fa4af6 KW |
79 | goto out; |
80 | } | |
f3e69beb SH |
81 | } |
82 | ||
12fa4af6 | 83 | out: |
61b49e48 AG |
84 | /* Reopen the image back in read-only mode if necessary */ |
85 | if (s->bs_flags != bdrv_get_flags(bs)) { | |
a170a91f | 86 | /* Give up write permissions before making it read-only */ |
1908a559 | 87 | blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort); |
61b49e48 AG |
88 | bdrv_reopen(bs, s->bs_flags, NULL); |
89 | } | |
90 | ||
f3e69beb | 91 | g_free(s->backing_file_str); |
eb23654d | 92 | job->ret = ret; |
f3e69beb SH |
93 | } |
94 | ||
f67432a2 | 95 | static int coroutine_fn stream_run(Job *job, Error **errp) |
4f1043b4 | 96 | { |
f67432a2 | 97 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); |
03e35d82 KW |
98 | BlockBackend *blk = s->common.blk; |
99 | BlockDriverState *bs = blk_bs(blk); | |
c8c3080f | 100 | BlockDriverState *base = s->base; |
05df8a6a | 101 | int64_t len; |
d535435f | 102 | int64_t offset = 0; |
f14a39cc | 103 | uint64_t delay_ns = 0; |
1d809098 | 104 | int error = 0; |
4f1043b4 | 105 | int ret = 0; |
51b0a488 | 106 | int64_t n = 0; /* bytes */ |
4f1043b4 SH |
107 | void *buf; |
108 | ||
760e0063 | 109 | if (!bs->backing) { |
6578629e | 110 | goto out; |
f4a193e7 HR |
111 | } |
112 | ||
05df8a6a KW |
113 | len = bdrv_getlength(bs); |
114 | if (len < 0) { | |
115 | ret = len; | |
6578629e | 116 | goto out; |
4f1043b4 | 117 | } |
30a5c887 | 118 | job_progress_set_remaining(&s->common.job, len); |
4f1043b4 | 119 | |
4f1043b4 SH |
120 | buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE); |
121 | ||
122 | /* Turn on copy-on-read for the whole block device so that guest read | |
123 | * requests help us make progress. Only do this when copying the entire | |
124 | * backing chain since the copy-on-read operation does not take base into | |
125 | * account. | |
126 | */ | |
127 | if (!base) { | |
128 | bdrv_enable_copy_on_read(bs); | |
129 | } | |
130 | ||
05df8a6a | 131 | for ( ; offset < len; offset += n) { |
f9749f28 | 132 | bool copy; |
4513eafe | 133 | |
4513eafe | 134 | /* Note that even when no rate limit is applied we need to yield |
c57b6656 | 135 | * with no pending I/O here so that bdrv_drain_all() returns. |
4513eafe | 136 | */ |
5d43e86e | 137 | job_sleep_ns(&s->common.job, delay_ns); |
daa7f2f9 | 138 | if (job_is_cancelled(&s->common.job)) { |
4f1043b4 SH |
139 | break; |
140 | } | |
141 | ||
c3e4f43a SW |
142 | copy = false; |
143 | ||
51b0a488 | 144 | ret = bdrv_is_allocated(bs, offset, STREAM_BUFFER_SIZE, &n); |
f9749f28 PB |
145 | if (ret == 1) { |
146 | /* Allocated in the top, no need to copy. */ | |
d663640c | 147 | } else if (ret >= 0) { |
f9749f28 | 148 | /* Copy if allocated in the intermediate images. Limit to the |
d535435f | 149 | * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE). */ |
760e0063 | 150 | ret = bdrv_is_allocated_above(backing_bs(bs), base, |
51b0a488 | 151 | offset, n, &n); |
571cd9dc SH |
152 | |
153 | /* Finish early if end of backing file has been reached */ | |
154 | if (ret == 0 && n == 0) { | |
05df8a6a | 155 | n = len - offset; |
571cd9dc SH |
156 | } |
157 | ||
f9749f28 PB |
158 | copy = (ret == 1); |
159 | } | |
51b0a488 | 160 | trace_stream_one_iteration(s, offset, n, ret); |
c3e4f43a | 161 | if (copy) { |
51b0a488 | 162 | ret = stream_populate(blk, offset, n, buf); |
4f1043b4 SH |
163 | } |
164 | if (ret < 0) { | |
1d809098 | 165 | BlockErrorAction action = |
81e254dc | 166 | block_job_error_action(&s->common, s->on_error, true, -ret); |
a589569f | 167 | if (action == BLOCK_ERROR_ACTION_STOP) { |
1d809098 PB |
168 | n = 0; |
169 | continue; | |
170 | } | |
171 | if (error == 0) { | |
172 | error = ret; | |
173 | } | |
a589569f | 174 | if (action == BLOCK_ERROR_ACTION_REPORT) { |
1d809098 PB |
175 | break; |
176 | } | |
4f1043b4 | 177 | } |
c8c3080f | 178 | ret = 0; |
4f1043b4 SH |
179 | |
180 | /* Publish progress */ | |
30a5c887 | 181 | job_progress_update(&s->common.job, n); |
dee81d51 KW |
182 | if (copy) { |
183 | delay_ns = block_job_ratelimit_get_delay(&s->common, n); | |
2fe4bba1 KW |
184 | } else { |
185 | delay_ns = 0; | |
f14a39cc | 186 | } |
4f1043b4 SH |
187 | } |
188 | ||
189 | if (!base) { | |
190 | bdrv_disable_copy_on_read(bs); | |
191 | } | |
192 | ||
1d809098 PB |
193 | /* Do not remove the backing file if an error was there but ignored. */ |
194 | ret = error; | |
195 | ||
4f1043b4 | 196 | qemu_vfree(buf); |
f3e69beb | 197 | |
6578629e | 198 | out: |
f3e69beb | 199 | /* Modify backing chain and close BDSes in main loop */ |
f67432a2 | 200 | return ret; |
4f1043b4 SH |
201 | } |
202 | ||
3fc4b10a | 203 | static const BlockJobDriver stream_job_driver = { |
33e9e9bd KW |
204 | .job_driver = { |
205 | .instance_size = sizeof(StreamBlockJob), | |
252291ea | 206 | .job_type = JOB_TYPE_STREAM, |
80fa2c75 | 207 | .free = block_job_free, |
f67432a2 | 208 | .run = stream_run, |
eb23654d | 209 | .exit = stream_exit, |
b15de828 | 210 | .user_resume = block_job_user_resume, |
b69f777d | 211 | .drain = block_job_drain, |
33e9e9bd | 212 | }, |
4f1043b4 SH |
213 | }; |
214 | ||
2323322e AG |
215 | void stream_start(const char *job_id, BlockDriverState *bs, |
216 | BlockDriverState *base, const char *backing_file_str, | |
cf6320df JS |
217 | int creation_flags, int64_t speed, |
218 | BlockdevOnError on_error, Error **errp) | |
4f1043b4 SH |
219 | { |
220 | StreamBlockJob *s; | |
61b49e48 AG |
221 | BlockDriverState *iter; |
222 | int orig_bs_flags; | |
4f1043b4 | 223 | |
61b49e48 AG |
224 | /* Make sure that the image is opened in read-write mode */ |
225 | orig_bs_flags = bdrv_get_flags(bs); | |
226 | if (!(orig_bs_flags & BDRV_O_RDWR)) { | |
227 | if (bdrv_reopen(bs, orig_bs_flags | BDRV_O_RDWR, errp) != 0) { | |
61b49e48 AG |
228 | return; |
229 | } | |
230 | } | |
231 | ||
a170a91f KW |
232 | /* Prevent concurrent jobs trying to modify the graph structure here, we |
233 | * already have our own plans. Also don't allow resize as the image size is | |
234 | * queried only at the job start and then cached. */ | |
75859b94 | 235 | s = block_job_create(job_id, &stream_job_driver, NULL, bs, |
a170a91f KW |
236 | BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | |
237 | BLK_PERM_GRAPH_MOD, | |
238 | BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | | |
239 | BLK_PERM_WRITE, | |
cf6320df | 240 | speed, creation_flags, NULL, NULL, errp); |
a170a91f KW |
241 | if (!s) { |
242 | goto fail; | |
243 | } | |
244 | ||
245 | /* Block all intermediate nodes between bs and base, because they will | |
246 | * disappear from the chain after this operation. The streaming job reads | |
247 | * every block only once, assuming that it doesn't change, so block writes | |
248 | * and resizes. */ | |
61b49e48 | 249 | for (iter = backing_bs(bs); iter && iter != base; iter = backing_bs(iter)) { |
76d554e2 | 250 | block_job_add_bdrv(&s->common, "intermediate node", iter, 0, |
a170a91f KW |
251 | BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED, |
252 | &error_abort); | |
61b49e48 AG |
253 | } |
254 | ||
4f1043b4 | 255 | s->base = base; |
13d8cc51 | 256 | s->backing_file_str = g_strdup(backing_file_str); |
61b49e48 | 257 | s->bs_flags = orig_bs_flags; |
4f1043b4 | 258 | |
1d809098 | 259 | s->on_error = on_error; |
5ccac6f1 | 260 | trace_stream_start(bs, base, s); |
da01ff7f | 261 | job_start(&s->common.job); |
a170a91f KW |
262 | return; |
263 | ||
264 | fail: | |
265 | if (orig_bs_flags != bdrv_get_flags(bs)) { | |
525989a5 | 266 | bdrv_reopen(bs, orig_bs_flags, NULL); |
a170a91f | 267 | } |
4f1043b4 | 268 | } |