block: add bdrv_find_backing_image
[qemu.git] / block / stream.c
1 /*
2 * Image streaming
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
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
14 #include "trace.h"
15 #include "block_int.h"
16
17 enum {
18 /*
19 * Size of data buffer for populating the image file. This should be large
20 * enough to process multiple clusters in a single call, so that populating
21 * contiguous regions of the image is efficient.
22 */
23 STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */
24 };
25
26 #define SLICE_TIME 100000000ULL /* ns */
27
28 typedef struct {
29 int64_t next_slice_time;
30 uint64_t slice_quota;
31 uint64_t dispatched;
32 } RateLimit;
33
34 static int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)
35 {
36 int64_t delay_ns = 0;
37 int64_t now = qemu_get_clock_ns(rt_clock);
38
39 if (limit->next_slice_time < now) {
40 limit->next_slice_time = now + SLICE_TIME;
41 limit->dispatched = 0;
42 }
43 if (limit->dispatched + n > limit->slice_quota) {
44 delay_ns = limit->next_slice_time - now;
45 } else {
46 limit->dispatched += n;
47 }
48 return delay_ns;
49 }
50
51 static void ratelimit_set_speed(RateLimit *limit, uint64_t speed)
52 {
53 limit->slice_quota = speed / (1000000000ULL / SLICE_TIME);
54 }
55
56 typedef struct StreamBlockJob {
57 BlockJob common;
58 RateLimit limit;
59 BlockDriverState *base;
60 } StreamBlockJob;
61
62 static int coroutine_fn stream_populate(BlockDriverState *bs,
63 int64_t sector_num, int nb_sectors,
64 void *buf)
65 {
66 struct iovec iov = {
67 .iov_base = buf,
68 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
69 };
70 QEMUIOVector qiov;
71
72 qemu_iovec_init_external(&qiov, &iov, 1);
73
74 /* Copy-on-read the unallocated clusters */
75 return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
76 }
77
78 static void coroutine_fn stream_run(void *opaque)
79 {
80 StreamBlockJob *s = opaque;
81 BlockDriverState *bs = s->common.bs;
82 int64_t sector_num, end;
83 int ret = 0;
84 int n;
85 void *buf;
86
87 s->common.len = bdrv_getlength(bs);
88 if (s->common.len < 0) {
89 block_job_complete(&s->common, s->common.len);
90 return;
91 }
92
93 end = s->common.len >> BDRV_SECTOR_BITS;
94 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
95
96 /* Turn on copy-on-read for the whole block device so that guest read
97 * requests help us make progress. Only do this when copying the entire
98 * backing chain since the copy-on-read operation does not take base into
99 * account.
100 */
101 if (!base) {
102 bdrv_enable_copy_on_read(bs);
103 }
104
105 for (sector_num = 0; sector_num < end; sector_num += n) {
106 retry:
107 if (block_job_is_cancelled(&s->common)) {
108 break;
109 }
110
111 ret = bdrv_co_is_allocated(bs, sector_num,
112 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
113 trace_stream_one_iteration(s, sector_num, n, ret);
114 if (ret == 0) {
115 if (s->common.speed) {
116 uint64_t delay_ns = ratelimit_calculate_delay(&s->limit, n);
117 if (delay_ns > 0) {
118 co_sleep_ns(rt_clock, delay_ns);
119
120 /* Recheck cancellation and that sectors are unallocated */
121 goto retry;
122 }
123 }
124 ret = stream_populate(bs, sector_num, n, buf);
125 }
126 if (ret < 0) {
127 break;
128 }
129
130 /* Publish progress */
131 s->common.offset += n * BDRV_SECTOR_SIZE;
132
133 /* Note that even when no rate limit is applied we need to yield
134 * with no pending I/O here so that qemu_aio_flush() returns.
135 */
136 co_sleep_ns(rt_clock, 0);
137 }
138
139 if (!base) {
140 bdrv_disable_copy_on_read(bs);
141 }
142
143 if (sector_num == end && ret == 0) {
144 ret = bdrv_change_backing_file(bs, NULL, NULL);
145 }
146
147 qemu_vfree(buf);
148 block_job_complete(&s->common, ret);
149 }
150
151 static int stream_set_speed(BlockJob *job, int64_t value)
152 {
153 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
154
155 if (value < 0) {
156 return -EINVAL;
157 }
158 job->speed = value;
159 ratelimit_set_speed(&s->limit, value / BDRV_SECTOR_SIZE);
160 return 0;
161 }
162
163 static BlockJobType stream_job_type = {
164 .instance_size = sizeof(StreamBlockJob),
165 .job_type = "stream",
166 .set_speed = stream_set_speed,
167 };
168
169 int stream_start(BlockDriverState *bs, BlockDriverState *base,
170 BlockDriverCompletionFunc *cb, void *opaque)
171 {
172 StreamBlockJob *s;
173 Coroutine *co;
174
175 s = block_job_create(&stream_job_type, bs, cb, opaque);
176 if (!s) {
177 return -EBUSY; /* bs must already be in use */
178 }
179
180 s->base = base;
181
182 co = qemu_coroutine_create(stream_run);
183 trace_stream_start(bs, base, s, co, opaque);
184 qemu_coroutine_enter(co, s);
185 return 0;
186 }