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" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 15 | #include "block/block_int.h" |
| 16 | #include "block/blockjob.h" |
Paolo Bonzini | 6ef228f | 2012-05-09 16:09:46 +0200 | [diff] [blame] | 17 | #include "qemu/ratelimit.h" |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 18 | |
| 19 | enum { |
| 20 | /* |
| 21 | * Size of data buffer for populating the image file. This should be large |
| 22 | * enough to process multiple clusters in a single call, so that populating |
| 23 | * contiguous regions of the image is efficient. |
| 24 | */ |
| 25 | STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */ |
| 26 | }; |
| 27 | |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 28 | #define SLICE_TIME 100000000ULL /* ns */ |
| 29 | |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 30 | typedef struct StreamBlockJob { |
| 31 | BlockJob common; |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 32 | RateLimit limit; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 33 | BlockDriverState *base; |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 34 | BlockdevOnError on_error; |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 35 | char *backing_file_str; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 36 | } StreamBlockJob; |
| 37 | |
| 38 | static 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 | /* Copy-on-read the unallocated clusters */ |
| 51 | return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov); |
| 52 | } |
| 53 | |
Marcelo Tosatti | 5a67a10 | 2012-03-26 21:22:10 -0300 | [diff] [blame] | 54 | static void close_unused_images(BlockDriverState *top, BlockDriverState *base, |
| 55 | const char *base_id) |
| 56 | { |
| 57 | BlockDriverState *intermediate; |
| 58 | intermediate = top->backing_hd; |
| 59 | |
Stefan Hajnoczi | 88266f5 | 2013-04-11 15:41:13 +0200 | [diff] [blame] | 60 | /* Must assign before bdrv_delete() to prevent traversing dangling pointer |
| 61 | * while we delete backing image instances. |
| 62 | */ |
Fam Zheng | 920beae | 2014-05-23 21:29:46 +0800 | [diff] [blame] | 63 | bdrv_set_backing_hd(top, base); |
Stefan Hajnoczi | 88266f5 | 2013-04-11 15:41:13 +0200 | [diff] [blame] | 64 | |
Marcelo Tosatti | 5a67a10 | 2012-03-26 21:22:10 -0300 | [diff] [blame] | 65 | while (intermediate) { |
| 66 | BlockDriverState *unused; |
| 67 | |
| 68 | /* reached base */ |
| 69 | if (intermediate == base) { |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | unused = intermediate; |
| 74 | intermediate = intermediate->backing_hd; |
Fam Zheng | 920beae | 2014-05-23 21:29:46 +0800 | [diff] [blame] | 75 | bdrv_set_backing_hd(unused, NULL); |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 76 | bdrv_unref(unused); |
Marcelo Tosatti | 5a67a10 | 2012-03-26 21:22:10 -0300 | [diff] [blame] | 77 | } |
Kevin Wolf | 355ef4a | 2013-12-11 20:14:09 +0100 | [diff] [blame] | 78 | |
Kevin Wolf | 3baca89 | 2014-07-16 17:48:16 +0200 | [diff] [blame] | 79 | bdrv_refresh_limits(top, NULL); |
Marcelo Tosatti | 5a67a10 | 2012-03-26 21:22:10 -0300 | [diff] [blame] | 80 | } |
| 81 | |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 82 | static void coroutine_fn stream_run(void *opaque) |
| 83 | { |
| 84 | StreamBlockJob *s = opaque; |
| 85 | BlockDriverState *bs = s->common.bs; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 86 | BlockDriverState *base = s->base; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 87 | int64_t sector_num, end; |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 88 | int error = 0; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 89 | int ret = 0; |
Anthony Liguori | 04120e3 | 2012-05-10 09:10:42 -0500 | [diff] [blame] | 90 | int n = 0; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 91 | void *buf; |
| 92 | |
Max Reitz | f4a193e | 2013-11-13 20:37:58 +0100 | [diff] [blame] | 93 | if (!bs->backing_hd) { |
| 94 | block_job_completed(&s->common, 0); |
| 95 | return; |
| 96 | } |
| 97 | |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 98 | s->common.len = bdrv_getlength(bs); |
| 99 | if (s->common.len < 0) { |
Paolo Bonzini | 65f4632 | 2012-10-18 16:49:20 +0200 | [diff] [blame] | 100 | block_job_completed(&s->common, s->common.len); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | |
| 104 | end = s->common.len >> BDRV_SECTOR_BITS; |
| 105 | buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE); |
| 106 | |
| 107 | /* Turn on copy-on-read for the whole block device so that guest read |
| 108 | * requests help us make progress. Only do this when copying the entire |
| 109 | * backing chain since the copy-on-read operation does not take base into |
| 110 | * account. |
| 111 | */ |
| 112 | if (!base) { |
| 113 | bdrv_enable_copy_on_read(bs); |
| 114 | } |
| 115 | |
| 116 | for (sector_num = 0; sector_num < end; sector_num += n) { |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 117 | uint64_t delay_ns = 0; |
Paolo Bonzini | f9749f2 | 2012-05-08 16:52:00 +0200 | [diff] [blame] | 118 | bool copy; |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 119 | |
| 120 | wait: |
| 121 | /* Note that even when no rate limit is applied we need to yield |
Kevin Wolf | c57b665 | 2012-11-13 16:35:13 +0100 | [diff] [blame] | 122 | * with no pending I/O here so that bdrv_drain_all() returns. |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 123 | */ |
Alex Bligh | 7483d1e | 2013-08-21 16:03:05 +0100 | [diff] [blame] | 124 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 125 | if (block_job_is_cancelled(&s->common)) { |
| 126 | break; |
| 127 | } |
| 128 | |
Stefan Weil | c3e4f43 | 2013-09-22 08:19:10 +0200 | [diff] [blame] | 129 | copy = false; |
| 130 | |
Paolo Bonzini | bdad13b | 2013-09-04 19:00:22 +0200 | [diff] [blame] | 131 | ret = bdrv_is_allocated(bs, sector_num, |
| 132 | STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n); |
Paolo Bonzini | f9749f2 | 2012-05-08 16:52:00 +0200 | [diff] [blame] | 133 | if (ret == 1) { |
| 134 | /* Allocated in the top, no need to copy. */ |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 135 | } else if (ret >= 0) { |
Paolo Bonzini | f9749f2 | 2012-05-08 16:52:00 +0200 | [diff] [blame] | 136 | /* Copy if allocated in the intermediate images. Limit to the |
| 137 | * known-unallocated area [sector_num, sector_num+n). */ |
Paolo Bonzini | 4f57863 | 2013-09-04 19:00:24 +0200 | [diff] [blame] | 138 | ret = bdrv_is_allocated_above(bs->backing_hd, base, |
| 139 | sector_num, n, &n); |
Stefan Hajnoczi | 571cd9d | 2012-08-28 15:26:48 +0100 | [diff] [blame] | 140 | |
| 141 | /* Finish early if end of backing file has been reached */ |
| 142 | if (ret == 0 && n == 0) { |
| 143 | n = end - sector_num; |
| 144 | } |
| 145 | |
Paolo Bonzini | f9749f2 | 2012-05-08 16:52:00 +0200 | [diff] [blame] | 146 | copy = (ret == 1); |
| 147 | } |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 148 | trace_stream_one_iteration(s, sector_num, n, ret); |
Stefan Weil | c3e4f43 | 2013-09-22 08:19:10 +0200 | [diff] [blame] | 149 | if (copy) { |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 150 | if (s->common.speed) { |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 151 | delay_ns = ratelimit_calculate_delay(&s->limit, n); |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 152 | if (delay_ns > 0) { |
Paolo Bonzini | 4513eaf | 2012-05-08 16:51:45 +0200 | [diff] [blame] | 153 | goto wait; |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 156 | ret = stream_populate(bs, sector_num, n, buf); |
| 157 | } |
| 158 | if (ret < 0) { |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 159 | BlockErrorAction action = |
| 160 | block_job_error_action(&s->common, s->common.bs, s->on_error, |
| 161 | true, -ret); |
Wenchao Xia | a589569 | 2014-06-18 08:43:30 +0200 | [diff] [blame] | 162 | if (action == BLOCK_ERROR_ACTION_STOP) { |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 163 | n = 0; |
| 164 | continue; |
| 165 | } |
| 166 | if (error == 0) { |
| 167 | error = ret; |
| 168 | } |
Wenchao Xia | a589569 | 2014-06-18 08:43:30 +0200 | [diff] [blame] | 169 | if (action == BLOCK_ERROR_ACTION_REPORT) { |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 170 | break; |
| 171 | } |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 172 | } |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 173 | ret = 0; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 174 | |
| 175 | /* Publish progress */ |
| 176 | s->common.offset += n * BDRV_SECTOR_SIZE; |
| 177 | } |
| 178 | |
| 179 | if (!base) { |
| 180 | bdrv_disable_copy_on_read(bs); |
| 181 | } |
| 182 | |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 183 | /* Do not remove the backing file if an error was there but ignored. */ |
| 184 | ret = error; |
| 185 | |
Paolo Bonzini | 3e91465 | 2012-03-30 13:17:11 +0200 | [diff] [blame] | 186 | if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) { |
Paolo Bonzini | f6133de | 2012-05-08 16:51:55 +0200 | [diff] [blame] | 187 | const char *base_id = NULL, *base_fmt = NULL; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 188 | if (base) { |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 189 | base_id = s->backing_file_str; |
Paolo Bonzini | f6133de | 2012-05-08 16:51:55 +0200 | [diff] [blame] | 190 | if (base->drv) { |
| 191 | base_fmt = base->drv->format_name; |
| 192 | } |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 193 | } |
Paolo Bonzini | f6133de | 2012-05-08 16:51:55 +0200 | [diff] [blame] | 194 | ret = bdrv_change_backing_file(bs, base_id, base_fmt); |
Marcelo Tosatti | 5a67a10 | 2012-03-26 21:22:10 -0300 | [diff] [blame] | 195 | close_unused_images(bs, base, base_id); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | qemu_vfree(buf); |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 199 | g_free(s->backing_file_str); |
Paolo Bonzini | 65f4632 | 2012-10-18 16:49:20 +0200 | [diff] [blame] | 200 | block_job_completed(&s->common, ret); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 203 | static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp) |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 204 | { |
| 205 | StreamBlockJob *s = container_of(job, StreamBlockJob, common); |
| 206 | |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 207 | if (speed < 0) { |
| 208 | error_set(errp, QERR_INVALID_PARAMETER, "speed"); |
Stefan Hajnoczi | 9e6636c | 2012-04-25 16:51:01 +0100 | [diff] [blame] | 209 | return; |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 210 | } |
Paolo Bonzini | 6ef228f | 2012-05-09 16:09:46 +0200 | [diff] [blame] | 211 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Fam Zheng | 3fc4b10 | 2013-10-08 17:29:38 +0800 | [diff] [blame] | 214 | static const BlockJobDriver stream_job_driver = { |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 215 | .instance_size = sizeof(StreamBlockJob), |
Fam Zheng | 79e14bf | 2013-10-08 17:29:40 +0800 | [diff] [blame] | 216 | .job_type = BLOCK_JOB_TYPE_STREAM, |
Stefan Hajnoczi | 5094a6c | 2012-01-18 14:40:45 +0000 | [diff] [blame] | 217 | .set_speed = stream_set_speed, |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 220 | void stream_start(BlockDriverState *bs, BlockDriverState *base, |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 221 | const char *backing_file_str, int64_t speed, |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 222 | BlockdevOnError on_error, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 223 | BlockCompletionFunc *cb, |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 224 | void *opaque, Error **errp) |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 225 | { |
| 226 | StreamBlockJob *s; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 227 | |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 228 | if ((on_error == BLOCKDEV_ON_ERROR_STOP || |
| 229 | on_error == BLOCKDEV_ON_ERROR_ENOSPC) && |
| 230 | !bdrv_iostatus_is_enabled(bs)) { |
| 231 | error_set(errp, QERR_INVALID_PARAMETER, "on-error"); |
| 232 | return; |
| 233 | } |
| 234 | |
Fam Zheng | 3fc4b10 | 2013-10-08 17:29:38 +0800 | [diff] [blame] | 235 | s = block_job_create(&stream_job_driver, bs, speed, cb, opaque, errp); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 236 | if (!s) { |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 237 | return; |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | s->base = base; |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 241 | s->backing_file_str = g_strdup(backing_file_str); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 242 | |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 243 | s->on_error = on_error; |
Paolo Bonzini | fa4478d | 2012-05-08 16:51:46 +0200 | [diff] [blame] | 244 | s->common.co = qemu_coroutine_create(stream_run); |
| 245 | trace_stream_start(bs, base, s, s->common.co, opaque); |
| 246 | qemu_coroutine_enter(s->common.co, s); |
Stefan Hajnoczi | 4f1043b | 2012-01-18 14:40:44 +0000 | [diff] [blame] | 247 | } |