Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 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 | |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 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 | |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 56 | typedef struct StreamBlockJob { |
| 57 | BlockJob common; |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 58 | RateLimit limit; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 59 | BlockDriverState *base; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 60 | char backing_file_id[1024]; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 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 | |
Marcelo Tosatti | 5a67a10 | 2012-03-26 21:22:10 -0300 | [diff] [blame] | 79 | static void close_unused_images(BlockDriverState *top, BlockDriverState *base, |
| 80 | const char *base_id) |
| 81 | { |
| 82 | BlockDriverState *intermediate; |
| 83 | intermediate = top->backing_hd; |
| 84 | |
| 85 | while (intermediate) { |
| 86 | BlockDriverState *unused; |
| 87 | |
| 88 | /* reached base */ |
| 89 | if (intermediate == base) { |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | unused = intermediate; |
| 94 | intermediate = intermediate->backing_hd; |
| 95 | unused->backing_hd = NULL; |
| 96 | bdrv_delete(unused); |
| 97 | } |
| 98 | top->backing_hd = base; |
Marcelo Tosatti | 5a67a10 | 2012-03-26 21:22:10 -0300 | [diff] [blame] | 99 | } |
| 100 | |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 101 | /* |
| 102 | * Given an image chain: [BASE] -> [INTER1] -> [INTER2] -> [TOP] |
| 103 | * |
| 104 | * Return true if the given sector is allocated in top. |
| 105 | * Return false if the given sector is allocated in intermediate images. |
| 106 | * Return true otherwise. |
| 107 | * |
| 108 | * 'pnum' is set to the number of sectors (including and immediately following |
| 109 | * the specified sector) that are known to be in the same |
| 110 | * allocated/unallocated state. |
| 111 | * |
| 112 | */ |
| 113 | static int coroutine_fn is_allocated_base(BlockDriverState *top, |
| 114 | BlockDriverState *base, |
| 115 | int64_t sector_num, |
| 116 | int nb_sectors, int *pnum) |
| 117 | { |
| 118 | BlockDriverState *intermediate; |
| 119 | int ret, n; |
| 120 | |
| 121 | ret = bdrv_co_is_allocated(top, sector_num, nb_sectors, &n); |
| 122 | if (ret) { |
| 123 | *pnum = n; |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Is the unallocated chunk [sector_num, n] also |
| 129 | * unallocated between base and top? |
| 130 | */ |
| 131 | intermediate = top->backing_hd; |
| 132 | |
| 133 | while (intermediate) { |
| 134 | int pnum_inter; |
| 135 | |
| 136 | /* reached base */ |
| 137 | if (intermediate == base) { |
| 138 | *pnum = n; |
| 139 | return 1; |
| 140 | } |
| 141 | ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors, |
| 142 | &pnum_inter); |
| 143 | if (ret < 0) { |
| 144 | return ret; |
| 145 | } else if (ret) { |
| 146 | *pnum = pnum_inter; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * [sector_num, nb_sectors] is unallocated on top but intermediate |
| 152 | * might have |
| 153 | * |
| 154 | * [sector_num+x, nr_sectors] allocated. |
| 155 | */ |
| 156 | if (n > pnum_inter) { |
| 157 | n = pnum_inter; |
| 158 | } |
| 159 | |
| 160 | intermediate = intermediate->backing_hd; |
| 161 | } |
| 162 | |
| 163 | return 1; |
| 164 | } |
| 165 | |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 166 | static void coroutine_fn stream_run(void *opaque) |
| 167 | { |
| 168 | StreamBlockJob *s = opaque; |
| 169 | BlockDriverState *bs = s->common.bs; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 170 | BlockDriverState *base = s->base; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 171 | int64_t sector_num, end; |
| 172 | int ret = 0; |
| 173 | int n; |
| 174 | void *buf; |
| 175 | |
| 176 | s->common.len = bdrv_getlength(bs); |
| 177 | if (s->common.len < 0) { |
| 178 | block_job_complete(&s->common, s->common.len); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | end = s->common.len >> BDRV_SECTOR_BITS; |
| 183 | buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE); |
| 184 | |
| 185 | /* Turn on copy-on-read for the whole block device so that guest read |
| 186 | * requests help us make progress. Only do this when copying the entire |
| 187 | * backing chain since the copy-on-read operation does not take base into |
| 188 | * account. |
| 189 | */ |
| 190 | if (!base) { |
| 191 | bdrv_enable_copy_on_read(bs); |
| 192 | } |
| 193 | |
| 194 | for (sector_num = 0; sector_num < end; sector_num += n) { |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 195 | uint64_t delay_ns = 0; |
| 196 | |
| 197 | wait: |
| 198 | /* Note that even when no rate limit is applied we need to yield |
| 199 | * with no pending I/O here so that qemu_aio_flush() returns. |
| 200 | */ |
| 201 | block_job_sleep_ns(&s->common, rt_clock, delay_ns); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 202 | if (block_job_is_cancelled(&s->common)) { |
| 203 | break; |
| 204 | } |
| 205 | |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 206 | if (base) { |
| 207 | ret = is_allocated_base(bs, base, sector_num, |
| 208 | STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n); |
| 209 | } else { |
| 210 | ret = bdrv_co_is_allocated(bs, sector_num, |
| 211 | STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, |
| 212 | &n); |
| 213 | } |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 214 | trace_stream_one_iteration(s, sector_num, n, ret); |
| 215 | if (ret == 0) { |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 216 | if (s->common.speed) { |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 217 | delay_ns = ratelimit_calculate_delay(&s->limit, n); |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 218 | if (delay_ns > 0) { |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 219 | goto wait; |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 222 | ret = stream_populate(bs, sector_num, n, buf); |
| 223 | } |
| 224 | if (ret < 0) { |
| 225 | break; |
| 226 | } |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 227 | ret = 0; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 228 | |
| 229 | /* Publish progress */ |
| 230 | s->common.offset += n * BDRV_SECTOR_SIZE; |
| 231 | } |
| 232 | |
| 233 | if (!base) { |
| 234 | bdrv_disable_copy_on_read(bs); |
| 235 | } |
| 236 | |
Paolo Bonzini | 3e91465 | 2012-03-30 13:17:11 +0200 | [diff] [blame] | 237 | if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) { |
Paolo Bonzini | f6133de | 2012-05-08 16:51:55 +0200 | [diff] [blame^] | 238 | const char *base_id = NULL, *base_fmt = NULL; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 239 | if (base) { |
| 240 | base_id = s->backing_file_id; |
Paolo Bonzini | f6133de | 2012-05-08 16:51:55 +0200 | [diff] [blame^] | 241 | if (base->drv) { |
| 242 | base_fmt = base->drv->format_name; |
| 243 | } |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 244 | } |
Paolo Bonzini | f6133de | 2012-05-08 16:51:55 +0200 | [diff] [blame^] | 245 | ret = bdrv_change_backing_file(bs, base_id, base_fmt); |
Marcelo Tosatti | 5a67a10 | 2012-03-26 21:22:10 -0300 | [diff] [blame] | 246 | close_unused_images(bs, base, base_id); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | qemu_vfree(buf); |
| 250 | block_job_complete(&s->common, ret); |
| 251 | } |
| 252 | |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 253 | static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp) |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 254 | { |
| 255 | StreamBlockJob *s = container_of(job, StreamBlockJob, common); |
| 256 | |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 257 | if (speed < 0) { |
| 258 | error_set(errp, QERR_INVALID_PARAMETER, "speed"); |
Stefan Hajnoczi | 9e6636c | 2012-04-25 16:51:01 +0100 | [diff] [blame] | 259 | return; |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 260 | } |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 261 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE); |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 264 | static BlockJobType stream_job_type = { |
| 265 | .instance_size = sizeof(StreamBlockJob), |
| 266 | .job_type = "stream", |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 267 | .set_speed = stream_set_speed, |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 270 | void stream_start(BlockDriverState *bs, BlockDriverState *base, |
Stefan Hajnoczi | c83c66c | 2012-04-25 16:51:03 +0100 | [diff] [blame] | 271 | const char *base_id, int64_t speed, |
| 272 | BlockDriverCompletionFunc *cb, |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 273 | void *opaque, Error **errp) |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 274 | { |
| 275 | StreamBlockJob *s; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 276 | |
Stefan Hajnoczi | c83c66c | 2012-04-25 16:51:03 +0100 | [diff] [blame] | 277 | s = block_job_create(&stream_job_type, bs, speed, cb, opaque, errp); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 278 | if (!s) { |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 279 | return; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | s->base = base; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 283 | if (base_id) { |
| 284 | pstrcpy(s->backing_file_id, sizeof(s->backing_file_id), base_id); |
| 285 | } |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 286 | |
Paolo Bonzini | fa4478d | 2012-05-08 16:51:46 +0200 | [diff] [blame] | 287 | s->common.co = qemu_coroutine_create(stream_run); |
| 288 | trace_stream_start(bs, base, s, s->common.co, opaque); |
| 289 | qemu_coroutine_enter(s->common.co, s); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 290 | } |