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 char backing_file_id[1024]; 61 } StreamBlockJob; 62 63 static int coroutine_fn stream_populate(BlockDriverState *bs, 64 int64_t sector_num, int nb_sectors, 65 void *buf) 66 { 67 struct iovec iov = { 68 .iov_base = buf, 69 .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 70 }; 71 QEMUIOVector qiov; 72 73 qemu_iovec_init_external(&qiov, &iov, 1); 74 75 /* Copy-on-read the unallocated clusters */ 76 return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov); 77 } 78 79 /* 80 * Given an image chain: [BASE] -> [INTER1] -> [INTER2] -> [TOP] 81 * 82 * Return true if the given sector is allocated in top. 83 * Return false if the given sector is allocated in intermediate images. 84 * Return true otherwise. 85 * 86 * 'pnum' is set to the number of sectors (including and immediately following 87 * the specified sector) that are known to be in the same 88 * allocated/unallocated state. 89 * 90 */ 91 static int coroutine_fn is_allocated_base(BlockDriverState *top, 92 BlockDriverState *base, 93 int64_t sector_num, 94 int nb_sectors, int *pnum) 95 { 96 BlockDriverState *intermediate; 97 int ret, n; 98 99 ret = bdrv_co_is_allocated(top, sector_num, nb_sectors, &n); 100 if (ret) { 101 *pnum = n; 102 return ret; 103 } 104 105 /* 106 * Is the unallocated chunk [sector_num, n] also 107 * unallocated between base and top? 108 */ 109 intermediate = top->backing_hd; 110 111 while (intermediate) { 112 int pnum_inter; 113 114 /* reached base */ 115 if (intermediate == base) { 116 *pnum = n; 117 return 1; 118 } 119 ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors, 120 &pnum_inter); 121 if (ret < 0) { 122 return ret; 123 } else if (ret) { 124 *pnum = pnum_inter; 125 return 0; 126 } 127 128 /* 129 * [sector_num, nb_sectors] is unallocated on top but intermediate 130 * might have 131 * 132 * [sector_num+x, nr_sectors] allocated. 133 */ 134 if (n > pnum_inter) { 135 n = pnum_inter; 136 } 137 138 intermediate = intermediate->backing_hd; 139 } 140 141 return 1; 142 } 143 144 static void coroutine_fn stream_run(void *opaque) 145 { 146 StreamBlockJob *s = opaque; 147 BlockDriverState *bs = s->common.bs; 148 BlockDriverState *base = s->base; 149 int64_t sector_num, end; 150 int ret = 0; 151 int n; 152 void *buf; 153 154 s->common.len = bdrv_getlength(bs); 155 if (s->common.len < 0) { 156 block_job_complete(&s->common, s->common.len); 157 return; 158 } 159 160 end = s->common.len >> BDRV_SECTOR_BITS; 161 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE); 162 163 /* Turn on copy-on-read for the whole block device so that guest read 164 * requests help us make progress. Only do this when copying the entire 165 * backing chain since the copy-on-read operation does not take base into 166 * account. 167 */ 168 if (!base) { 169 bdrv_enable_copy_on_read(bs); 170 } 171 172 for (sector_num = 0; sector_num < end; sector_num += n) { 173 retry: 174 if (block_job_is_cancelled(&s->common)) { 175 break; 176 } 177 178 179 if (base) { 180 ret = is_allocated_base(bs, base, sector_num, 181 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n); 182 } else { 183 ret = bdrv_co_is_allocated(bs, sector_num, 184 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, 185 &n); 186 } 187 trace_stream_one_iteration(s, sector_num, n, ret); 188 if (ret == 0) { 189 if (s->common.speed) { 190 uint64_t delay_ns = ratelimit_calculate_delay(&s->limit, n); 191 if (delay_ns > 0) { 192 co_sleep_ns(rt_clock, delay_ns); 193 194 /* Recheck cancellation and that sectors are unallocated */ 195 goto retry; 196 } 197 } 198 ret = stream_populate(bs, sector_num, n, buf); 199 } 200 if (ret < 0) { 201 break; 202 } 203 ret = 0; 204 205 /* Publish progress */ 206 s->common.offset += n * BDRV_SECTOR_SIZE; 207 208 /* Note that even when no rate limit is applied we need to yield 209 * with no pending I/O here so that qemu_aio_flush() returns. 210 */ 211 co_sleep_ns(rt_clock, 0); 212 } 213 214 if (!base) { 215 bdrv_disable_copy_on_read(bs); 216 } 217 218 if (sector_num == end && ret == 0) { 219 const char *base_id = NULL; 220 if (base) { 221 base_id = s->backing_file_id; 222 } 223 ret = bdrv_change_backing_file(bs, base_id, NULL); 224 } 225 226 qemu_vfree(buf); 227 block_job_complete(&s->common, ret); 228 } 229 230 static int stream_set_speed(BlockJob *job, int64_t value) 231 { 232 StreamBlockJob *s = container_of(job, StreamBlockJob, common); 233 234 if (value < 0) { 235 return -EINVAL; 236 } 237 job->speed = value; 238 ratelimit_set_speed(&s->limit, value / BDRV_SECTOR_SIZE); 239 return 0; 240 } 241 242 static BlockJobType stream_job_type = { 243 .instance_size = sizeof(StreamBlockJob), 244 .job_type = "stream", 245 .set_speed = stream_set_speed, 246 }; 247 248 int stream_start(BlockDriverState *bs, BlockDriverState *base, 249 const char *base_id, BlockDriverCompletionFunc *cb, 250 void *opaque) 251 { 252 StreamBlockJob *s; 253 Coroutine *co; 254 255 s = block_job_create(&stream_job_type, bs, cb, opaque); 256 if (!s) { 257 return -EBUSY; /* bs must already be in use */ 258 } 259 260 s->base = base; 261 if (base_id) { 262 pstrcpy(s->backing_file_id, sizeof(s->backing_file_id), base_id); 263 } 264 265 co = qemu_coroutine_create(stream_run); 266 trace_stream_start(bs, base, s, co, opaque); 267 qemu_coroutine_enter(co, s); 268 return 0; 269 } 270