blob: a1dc8da484df3306d5cbeb7e2701efb6432e02b3 [file] [log] [blame]
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +00001/*
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 Bonzini737e1502012-12-17 18:19:44 +010015#include "block/block_int.h"
16#include "block/blockjob.h"
Paolo Bonzini6ef228f2012-05-09 16:09:46 +020017#include "qemu/ratelimit.h"
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000018
19enum {
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 Hajnoczi5094a6c2012-01-18 14:40:45 +000028#define SLICE_TIME 100000000ULL /* ns */
29
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000030typedef struct StreamBlockJob {
31 BlockJob common;
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +000032 RateLimit limit;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000033 BlockDriverState *base;
Paolo Bonzini1d809092012-09-28 17:22:59 +020034 BlockdevOnError on_error;
Jeff Cody13d8cc52014-06-25 15:40:11 -040035 char *backing_file_str;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000036} 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 /* Copy-on-read the unallocated clusters */
51 return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
52}
53
Marcelo Tosatti5a67a102012-03-26 21:22:10 -030054static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
55 const char *base_id)
56{
57 BlockDriverState *intermediate;
58 intermediate = top->backing_hd;
59
Stefan Hajnoczi88266f52013-04-11 15:41:13 +020060 /* Must assign before bdrv_delete() to prevent traversing dangling pointer
61 * while we delete backing image instances.
62 */
Fam Zheng920beae2014-05-23 21:29:46 +080063 bdrv_set_backing_hd(top, base);
Stefan Hajnoczi88266f52013-04-11 15:41:13 +020064
Marcelo Tosatti5a67a102012-03-26 21:22:10 -030065 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 Zheng920beae2014-05-23 21:29:46 +080075 bdrv_set_backing_hd(unused, NULL);
Fam Zheng4f6fd342013-08-23 09:14:47 +080076 bdrv_unref(unused);
Marcelo Tosatti5a67a102012-03-26 21:22:10 -030077 }
Kevin Wolf355ef4a2013-12-11 20:14:09 +010078
Kevin Wolf3baca892014-07-16 17:48:16 +020079 bdrv_refresh_limits(top, NULL);
Marcelo Tosatti5a67a102012-03-26 21:22:10 -030080}
81
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000082static void coroutine_fn stream_run(void *opaque)
83{
84 StreamBlockJob *s = opaque;
85 BlockDriverState *bs = s->common.bs;
Marcelo Tosattic8c30802012-01-18 14:40:53 +000086 BlockDriverState *base = s->base;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000087 int64_t sector_num, end;
Paolo Bonzini1d809092012-09-28 17:22:59 +020088 int error = 0;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000089 int ret = 0;
Anthony Liguori04120e32012-05-10 09:10:42 -050090 int n = 0;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000091 void *buf;
92
Max Reitzf4a193e2013-11-13 20:37:58 +010093 if (!bs->backing_hd) {
94 block_job_completed(&s->common, 0);
95 return;
96 }
97
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000098 s->common.len = bdrv_getlength(bs);
99 if (s->common.len < 0) {
Paolo Bonzini65f46322012-10-18 16:49:20 +0200100 block_job_completed(&s->common, s->common.len);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000101 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 Bonzini4513eaf2012-05-08 16:51:45 +0200117 uint64_t delay_ns = 0;
Paolo Bonzinif9749f22012-05-08 16:52:00 +0200118 bool copy;
Paolo Bonzini4513eaf2012-05-08 16:51:45 +0200119
120wait:
121 /* Note that even when no rate limit is applied we need to yield
Kevin Wolfc57b6652012-11-13 16:35:13 +0100122 * with no pending I/O here so that bdrv_drain_all() returns.
Paolo Bonzini4513eaf2012-05-08 16:51:45 +0200123 */
Alex Bligh7483d1e2013-08-21 16:03:05 +0100124 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000125 if (block_job_is_cancelled(&s->common)) {
126 break;
127 }
128
Stefan Weilc3e4f432013-09-22 08:19:10 +0200129 copy = false;
130
Paolo Bonzinibdad13b2013-09-04 19:00:22 +0200131 ret = bdrv_is_allocated(bs, sector_num,
132 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
Paolo Bonzinif9749f22012-05-08 16:52:00 +0200133 if (ret == 1) {
134 /* Allocated in the top, no need to copy. */
Paolo Bonzinid6636402013-09-04 19:00:25 +0200135 } else if (ret >= 0) {
Paolo Bonzinif9749f22012-05-08 16:52:00 +0200136 /* Copy if allocated in the intermediate images. Limit to the
137 * known-unallocated area [sector_num, sector_num+n). */
Paolo Bonzini4f578632013-09-04 19:00:24 +0200138 ret = bdrv_is_allocated_above(bs->backing_hd, base,
139 sector_num, n, &n);
Stefan Hajnoczi571cd9d2012-08-28 15:26:48 +0100140
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 Bonzinif9749f22012-05-08 16:52:00 +0200146 copy = (ret == 1);
147 }
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000148 trace_stream_one_iteration(s, sector_num, n, ret);
Stefan Weilc3e4f432013-09-22 08:19:10 +0200149 if (copy) {
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000150 if (s->common.speed) {
Paolo Bonzini4513eaf2012-05-08 16:51:45 +0200151 delay_ns = ratelimit_calculate_delay(&s->limit, n);
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000152 if (delay_ns > 0) {
Paolo Bonzini4513eaf2012-05-08 16:51:45 +0200153 goto wait;
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000154 }
155 }
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000156 ret = stream_populate(bs, sector_num, n, buf);
157 }
158 if (ret < 0) {
Paolo Bonzini1d809092012-09-28 17:22:59 +0200159 BlockErrorAction action =
160 block_job_error_action(&s->common, s->common.bs, s->on_error,
161 true, -ret);
Wenchao Xiaa5895692014-06-18 08:43:30 +0200162 if (action == BLOCK_ERROR_ACTION_STOP) {
Paolo Bonzini1d809092012-09-28 17:22:59 +0200163 n = 0;
164 continue;
165 }
166 if (error == 0) {
167 error = ret;
168 }
Wenchao Xiaa5895692014-06-18 08:43:30 +0200169 if (action == BLOCK_ERROR_ACTION_REPORT) {
Paolo Bonzini1d809092012-09-28 17:22:59 +0200170 break;
171 }
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000172 }
Marcelo Tosattic8c30802012-01-18 14:40:53 +0000173 ret = 0;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000174
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 Bonzini1d809092012-09-28 17:22:59 +0200183 /* Do not remove the backing file if an error was there but ignored. */
184 ret = error;
185
Paolo Bonzini3e914652012-03-30 13:17:11 +0200186 if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) {
Paolo Bonzinif6133de2012-05-08 16:51:55 +0200187 const char *base_id = NULL, *base_fmt = NULL;
Marcelo Tosattic8c30802012-01-18 14:40:53 +0000188 if (base) {
Jeff Cody13d8cc52014-06-25 15:40:11 -0400189 base_id = s->backing_file_str;
Paolo Bonzinif6133de2012-05-08 16:51:55 +0200190 if (base->drv) {
191 base_fmt = base->drv->format_name;
192 }
Marcelo Tosattic8c30802012-01-18 14:40:53 +0000193 }
Paolo Bonzinif6133de2012-05-08 16:51:55 +0200194 ret = bdrv_change_backing_file(bs, base_id, base_fmt);
Marcelo Tosatti5a67a102012-03-26 21:22:10 -0300195 close_unused_images(bs, base, base_id);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000196 }
197
198 qemu_vfree(buf);
Jeff Cody13d8cc52014-06-25 15:40:11 -0400199 g_free(s->backing_file_str);
Paolo Bonzini65f46322012-10-18 16:49:20 +0200200 block_job_completed(&s->common, ret);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000201}
202
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +0100203static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000204{
205 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
206
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +0100207 if (speed < 0) {
208 error_set(errp, QERR_INVALID_PARAMETER, "speed");
Stefan Hajnoczi9e6636c2012-04-25 16:51:01 +0100209 return;
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000210 }
Paolo Bonzini6ef228f2012-05-09 16:09:46 +0200211 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000212}
213
Fam Zheng3fc4b102013-10-08 17:29:38 +0800214static const BlockJobDriver stream_job_driver = {
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000215 .instance_size = sizeof(StreamBlockJob),
Fam Zheng79e14bf2013-10-08 17:29:40 +0800216 .job_type = BLOCK_JOB_TYPE_STREAM,
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000217 .set_speed = stream_set_speed,
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000218};
219
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +0100220void stream_start(BlockDriverState *bs, BlockDriverState *base,
Jeff Cody13d8cc52014-06-25 15:40:11 -0400221 const char *backing_file_str, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +0200222 BlockdevOnError on_error,
Markus Armbruster097310b2014-10-07 13:59:15 +0200223 BlockCompletionFunc *cb,
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +0100224 void *opaque, Error **errp)
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000225{
226 StreamBlockJob *s;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000227
Paolo Bonzini1d809092012-09-28 17:22:59 +0200228 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 Zheng3fc4b102013-10-08 17:29:38 +0800235 s = block_job_create(&stream_job_driver, bs, speed, cb, opaque, errp);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000236 if (!s) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +0100237 return;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000238 }
239
240 s->base = base;
Jeff Cody13d8cc52014-06-25 15:40:11 -0400241 s->backing_file_str = g_strdup(backing_file_str);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000242
Paolo Bonzini1d809092012-09-28 17:22:59 +0200243 s->on_error = on_error;
Paolo Bonzinifa4478d2012-05-08 16:51:46 +0200244 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 Hajnoczi4f1043b2012-01-18 14:40:44 +0000247}