1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "trace.h"
15#include "block/block_int.h"
16#include "block/blockjob.h"
17#include "qemu/ratelimit.h"
18
19enum {
20
21
22
23
24
25 STREAM_BUFFER_SIZE = 512 * 1024,
26};
27
28#define SLICE_TIME 100000000ULL
29
30typedef struct StreamBlockJob {
31 BlockJob common;
32 RateLimit limit;
33 BlockDriverState *base;
34 BlockdevOnError on_error;
35 char backing_file_id[1024];
36} StreamBlockJob;
37
38static int coroutine_fn stream_populate(BlockDriverState *bs,
39 int64_t sector_num, int nb_sectors,
40 void *buf)
41{
42 struct iovec iov = {
43 .iov_base = buf,
44 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
45 };
46 QEMUIOVector qiov;
47
48 qemu_iovec_init_external(&qiov, &iov, 1);
49
50
51 return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
52}
53
54static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
55 const char *base_id)
56{
57 BlockDriverState *intermediate;
58 intermediate = top->backing_hd;
59
60
61
62
63 top->backing_hd = base;
64
65 while (intermediate) {
66 BlockDriverState *unused;
67
68
69 if (intermediate == base) {
70 break;
71 }
72
73 unused = intermediate;
74 intermediate = intermediate->backing_hd;
75 unused->backing_hd = NULL;
76 bdrv_unref(unused);
77 }
78
79 bdrv_refresh_limits(top);
80}
81
82static void coroutine_fn stream_run(void *opaque)
83{
84 StreamBlockJob *s = opaque;
85 BlockDriverState *bs = s->common.bs;
86 BlockDriverState *base = s->base;
87 int64_t sector_num, end;
88 int error = 0;
89 int ret = 0;
90 int n = 0;
91 void *buf;
92
93 if (!bs->backing_hd) {
94 block_job_completed(&s->common, 0);
95 return;
96 }
97
98 s->common.len = bdrv_getlength(bs);
99 if (s->common.len < 0) {
100 block_job_completed(&s->common, s->common.len);
101 return;
102 }
103
104 end = s->common.len >> BDRV_SECTOR_BITS;
105 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
106
107
108
109
110
111
112 if (!base) {
113 bdrv_enable_copy_on_read(bs);
114 }
115
116 for (sector_num = 0; sector_num < end; sector_num += n) {
117 uint64_t delay_ns = 0;
118 bool copy;
119
120wait:
121
122
123
124 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
125 if (block_job_is_cancelled(&s->common)) {
126 break;
127 }
128
129 copy = false;
130
131 ret = bdrv_is_allocated(bs, sector_num,
132 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
133 if (ret == 1) {
134
135 } else if (ret >= 0) {
136
137
138 ret = bdrv_is_allocated_above(bs->backing_hd, base,
139 sector_num, n, &n);
140
141
142 if (ret == 0 && n == 0) {
143 n = end - sector_num;
144 }
145
146 copy = (ret == 1);
147 }
148 trace_stream_one_iteration(s, sector_num, n, ret);
149 if (copy) {
150 if (s->common.speed) {
151 delay_ns = ratelimit_calculate_delay(&s->limit, n);
152 if (delay_ns > 0) {
153 goto wait;
154 }
155 }
156 ret = stream_populate(bs, sector_num, n, buf);
157 }
158 if (ret < 0) {
159 BlockErrorAction action =
160 block_job_error_action(&s->common, s->common.bs, s->on_error,
161 true, -ret);
162 if (action == BDRV_ACTION_STOP) {
163 n = 0;
164 continue;
165 }
166 if (error == 0) {
167 error = ret;
168 }
169 if (action == BDRV_ACTION_REPORT) {
170 break;
171 }
172 }
173 ret = 0;
174
175
176 s->common.offset += n * BDRV_SECTOR_SIZE;
177 }
178
179 if (!base) {
180 bdrv_disable_copy_on_read(bs);
181 }
182
183
184 ret = error;
185
186 if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) {
187 const char *base_id = NULL, *base_fmt = NULL;
188 if (base) {
189 base_id = s->backing_file_id;
190 if (base->drv) {
191 base_fmt = base->drv->format_name;
192 }
193 }
194 ret = bdrv_change_backing_file(bs, base_id, base_fmt);
195 close_unused_images(bs, base, base_id);
196 }
197
198 qemu_vfree(buf);
199 block_job_completed(&s->common, ret);
200}
201
202static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
203{
204 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
205
206 if (speed < 0) {
207 error_set(errp, QERR_INVALID_PARAMETER, "speed");
208 return;
209 }
210 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
211}
212
213static const BlockJobDriver stream_job_driver = {
214 .instance_size = sizeof(StreamBlockJob),
215 .job_type = BLOCK_JOB_TYPE_STREAM,
216 .set_speed = stream_set_speed,
217};
218
219void stream_start(BlockDriverState *bs, BlockDriverState *base,
220 const char *base_id, int64_t speed,
221 BlockdevOnError on_error,
222 BlockDriverCompletionFunc *cb,
223 void *opaque, Error **errp)
224{
225 StreamBlockJob *s;
226
227 if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
228 on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
229 !bdrv_iostatus_is_enabled(bs)) {
230 error_set(errp, QERR_INVALID_PARAMETER, "on-error");
231 return;
232 }
233
234 s = block_job_create(&stream_job_driver, bs, speed, cb, opaque, errp);
235 if (!s) {
236 return;
237 }
238
239 s->base = base;
240 if (base_id) {
241 pstrcpy(s->backing_file_id, sizeof(s->backing_file_id), base_id);
242 }
243
244 s->on_error = on_error;
245 s->common.co = qemu_coroutine_create(stream_run);
246 trace_stream_start(bs, base, s, s->common.co, opaque);
247 qemu_coroutine_enter(s->common.co, s);
248}
249