blob: 52d329f5c62552c5b71da6a8abf28340172964ac [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
Peter Maydell80c71a22016-01-18 18:01:42 +000014#include "qemu/osdep.h"
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000015#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010016#include "block/block_int.h"
John Snowc87621e2016-10-27 12:07:00 -040017#include "block/blockjob_int.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010018#include "qapi/error.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010019#include "qapi/qmp/qerror.h"
Paolo Bonzini6ef228f2012-05-09 16:09:46 +020020#include "qemu/ratelimit.h"
Max Reitz373340b2015-10-19 17:53:22 +020021#include "sysemu/block-backend.h"
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000022
23enum {
24 /*
25 * Size of data buffer for populating the image file. This should be large
26 * enough to process multiple clusters in a single call, so that populating
27 * contiguous regions of the image is efficient.
28 */
29 STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */
30};
31
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +000032#define SLICE_TIME 100000000ULL /* ns */
33
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000034typedef struct StreamBlockJob {
35 BlockJob common;
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +000036 RateLimit limit;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000037 BlockDriverState *base;
Paolo Bonzini1d809092012-09-28 17:22:59 +020038 BlockdevOnError on_error;
Jeff Cody13d8cc52014-06-25 15:40:11 -040039 char *backing_file_str;
Alberto Garcia61b49e42016-10-28 10:08:10 +030040 int bs_flags;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000041} StreamBlockJob;
42
Kevin Wolf03e35d82016-04-12 15:15:49 +020043static int coroutine_fn stream_populate(BlockBackend *blk,
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000044 int64_t sector_num, int nb_sectors,
45 void *buf)
46{
47 struct iovec iov = {
48 .iov_base = buf,
49 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
50 };
51 QEMUIOVector qiov;
52
53 qemu_iovec_init_external(&qiov, &iov, 1);
54
55 /* Copy-on-read the unallocated clusters */
Kevin Wolf03e35d82016-04-12 15:15:49 +020056 return blk_co_preadv(blk, sector_num * BDRV_SECTOR_SIZE, qiov.size, &qiov,
57 BDRV_REQ_COPY_ON_READ);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +000058}
59
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +010060typedef struct {
61 int ret;
62 bool reached_end;
63} StreamCompleteData;
64
65static void stream_complete(BlockJob *job, void *opaque)
66{
67 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
68 StreamCompleteData *data = opaque;
Kevin Wolf03e35d82016-04-12 15:15:49 +020069 BlockDriverState *bs = blk_bs(job->blk);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +010070 BlockDriverState *base = s->base;
Kevin Wolf12fa4af2017-02-17 20:42:32 +010071 Error *local_err = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +010072
73 if (!block_job_is_cancelled(&s->common) && data->reached_end &&
74 data->ret == 0) {
75 const char *base_id = NULL, *base_fmt = NULL;
76 if (base) {
77 base_id = s->backing_file_str;
78 if (base->drv) {
79 base_fmt = base->drv->format_name;
80 }
81 }
Kevin Wolf03e35d82016-04-12 15:15:49 +020082 data->ret = bdrv_change_backing_file(bs, base_id, base_fmt);
Kevin Wolf12fa4af2017-02-17 20:42:32 +010083 bdrv_set_backing_hd(bs, base, &local_err);
84 if (local_err) {
85 error_report_err(local_err);
86 data->ret = -EPERM;
87 goto out;
88 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +010089 }
90
Kevin Wolf12fa4af2017-02-17 20:42:32 +010091out:
Alberto Garcia61b49e42016-10-28 10:08:10 +030092 /* Reopen the image back in read-only mode if necessary */
93 if (s->bs_flags != bdrv_get_flags(bs)) {
Kevin Wolfa170a912017-02-09 13:34:18 +010094 /* Give up write permissions before making it read-only */
95 blk_set_perm(job->blk, 0, BLK_PERM_ALL, &error_abort);
Alberto Garcia61b49e42016-10-28 10:08:10 +030096 bdrv_reopen(bs, s->bs_flags, NULL);
97 }
98
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +010099 g_free(s->backing_file_str);
100 block_job_completed(&s->common, data->ret);
101 g_free(data);
102}
103
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000104static void coroutine_fn stream_run(void *opaque)
105{
106 StreamBlockJob *s = opaque;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +0100107 StreamCompleteData *data;
Kevin Wolf03e35d82016-04-12 15:15:49 +0200108 BlockBackend *blk = s->common.blk;
109 BlockDriverState *bs = blk_bs(blk);
Marcelo Tosattic8c30802012-01-18 14:40:53 +0000110 BlockDriverState *base = s->base;
Alberto Garcia65786292016-03-21 15:47:25 +0200111 int64_t sector_num = 0;
112 int64_t end = -1;
Sascha Silbef14a39c2016-06-28 17:28:41 +0200113 uint64_t delay_ns = 0;
Paolo Bonzini1d809092012-09-28 17:22:59 +0200114 int error = 0;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000115 int ret = 0;
Anthony Liguori04120e32012-05-10 09:10:42 -0500116 int n = 0;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000117 void *buf;
118
Kevin Wolf760e0062015-06-17 14:55:21 +0200119 if (!bs->backing) {
Alberto Garcia65786292016-03-21 15:47:25 +0200120 goto out;
Max Reitzf4a193e2013-11-13 20:37:58 +0100121 }
122
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000123 s->common.len = bdrv_getlength(bs);
124 if (s->common.len < 0) {
Alberto Garcia65786292016-03-21 15:47:25 +0200125 ret = s->common.len;
126 goto out;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000127 }
128
129 end = s->common.len >> BDRV_SECTOR_BITS;
130 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
131
132 /* Turn on copy-on-read for the whole block device so that guest read
133 * requests help us make progress. Only do this when copying the entire
134 * backing chain since the copy-on-read operation does not take base into
135 * account.
136 */
137 if (!base) {
138 bdrv_enable_copy_on_read(bs);
139 }
140
141 for (sector_num = 0; sector_num < end; sector_num += n) {
Paolo Bonzinif9749f22012-05-08 16:52:00 +0200142 bool copy;
Paolo Bonzini4513eaf2012-05-08 16:51:45 +0200143
Paolo Bonzini4513eaf2012-05-08 16:51:45 +0200144 /* Note that even when no rate limit is applied we need to yield
Kevin Wolfc57b6652012-11-13 16:35:13 +0100145 * with no pending I/O here so that bdrv_drain_all() returns.
Paolo Bonzini4513eaf2012-05-08 16:51:45 +0200146 */
Alex Bligh7483d1e2013-08-21 16:03:05 +0100147 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000148 if (block_job_is_cancelled(&s->common)) {
149 break;
150 }
151
Stefan Weilc3e4f432013-09-22 08:19:10 +0200152 copy = false;
153
Paolo Bonzinibdad13b2013-09-04 19:00:22 +0200154 ret = bdrv_is_allocated(bs, sector_num,
155 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
Paolo Bonzinif9749f22012-05-08 16:52:00 +0200156 if (ret == 1) {
157 /* Allocated in the top, no need to copy. */
Paolo Bonzinid6636402013-09-04 19:00:25 +0200158 } else if (ret >= 0) {
Paolo Bonzinif9749f22012-05-08 16:52:00 +0200159 /* Copy if allocated in the intermediate images. Limit to the
160 * known-unallocated area [sector_num, sector_num+n). */
Kevin Wolf760e0062015-06-17 14:55:21 +0200161 ret = bdrv_is_allocated_above(backing_bs(bs), base,
Paolo Bonzini4f578632013-09-04 19:00:24 +0200162 sector_num, n, &n);
Stefan Hajnoczi571cd9d2012-08-28 15:26:48 +0100163
164 /* Finish early if end of backing file has been reached */
165 if (ret == 0 && n == 0) {
166 n = end - sector_num;
167 }
168
Paolo Bonzinif9749f22012-05-08 16:52:00 +0200169 copy = (ret == 1);
170 }
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000171 trace_stream_one_iteration(s, sector_num, n, ret);
Stefan Weilc3e4f432013-09-22 08:19:10 +0200172 if (copy) {
Kevin Wolf03e35d82016-04-12 15:15:49 +0200173 ret = stream_populate(blk, sector_num, n, buf);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000174 }
175 if (ret < 0) {
Paolo Bonzini1d809092012-09-28 17:22:59 +0200176 BlockErrorAction action =
Kevin Wolf81e254d2016-04-18 11:36:38 +0200177 block_job_error_action(&s->common, s->on_error, true, -ret);
Wenchao Xiaa5895692014-06-18 08:43:30 +0200178 if (action == BLOCK_ERROR_ACTION_STOP) {
Paolo Bonzini1d809092012-09-28 17:22:59 +0200179 n = 0;
180 continue;
181 }
182 if (error == 0) {
183 error = ret;
184 }
Wenchao Xiaa5895692014-06-18 08:43:30 +0200185 if (action == BLOCK_ERROR_ACTION_REPORT) {
Paolo Bonzini1d809092012-09-28 17:22:59 +0200186 break;
187 }
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000188 }
Marcelo Tosattic8c30802012-01-18 14:40:53 +0000189 ret = 0;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000190
191 /* Publish progress */
192 s->common.offset += n * BDRV_SECTOR_SIZE;
Sascha Silbef14a39c2016-06-28 17:28:41 +0200193 if (copy && s->common.speed) {
194 delay_ns = ratelimit_calculate_delay(&s->limit, n);
195 }
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000196 }
197
198 if (!base) {
199 bdrv_disable_copy_on_read(bs);
200 }
201
Paolo Bonzini1d809092012-09-28 17:22:59 +0200202 /* Do not remove the backing file if an error was there but ignored. */
203 ret = error;
204
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000205 qemu_vfree(buf);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +0100206
Alberto Garcia65786292016-03-21 15:47:25 +0200207out:
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +0100208 /* Modify backing chain and close BDSes in main loop */
209 data = g_malloc(sizeof(*data));
210 data->ret = ret;
211 data->reached_end = sector_num == end;
212 block_job_defer_to_main_loop(&s->common, stream_complete, data);
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000213}
214
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +0100215static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000216{
217 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
218
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +0100219 if (speed < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100220 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
Stefan Hajnoczi9e6636c2012-04-25 16:51:01 +0100221 return;
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000222 }
Paolo Bonzini6ef228f2012-05-09 16:09:46 +0200223 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000224}
225
Fam Zheng3fc4b102013-10-08 17:29:38 +0800226static const BlockJobDriver stream_job_driver = {
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000227 .instance_size = sizeof(StreamBlockJob),
Fam Zheng79e14bf2013-10-08 17:29:40 +0800228 .job_type = BLOCK_JOB_TYPE_STREAM,
Stefan Hajnoczi5094a6c2012-01-18 14:40:45 +0000229 .set_speed = stream_set_speed,
John Snowa7815a72016-11-08 01:50:36 -0500230 .start = stream_run,
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000231};
232
Alberto Garcia23233222016-07-05 17:28:59 +0300233void stream_start(const char *job_id, BlockDriverState *bs,
234 BlockDriverState *base, const char *backing_file_str,
John Snow8254b6d2016-10-27 12:06:58 -0400235 int64_t speed, BlockdevOnError on_error, Error **errp)
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000236{
237 StreamBlockJob *s;
Alberto Garcia61b49e42016-10-28 10:08:10 +0300238 BlockDriverState *iter;
239 int orig_bs_flags;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000240
Alberto Garcia61b49e42016-10-28 10:08:10 +0300241 /* Make sure that the image is opened in read-write mode */
242 orig_bs_flags = bdrv_get_flags(bs);
243 if (!(orig_bs_flags & BDRV_O_RDWR)) {
244 if (bdrv_reopen(bs, orig_bs_flags | BDRV_O_RDWR, errp) != 0) {
Alberto Garcia61b49e42016-10-28 10:08:10 +0300245 return;
246 }
247 }
248
Kevin Wolfa170a912017-02-09 13:34:18 +0100249 /* Prevent concurrent jobs trying to modify the graph structure here, we
250 * already have our own plans. Also don't allow resize as the image size is
251 * queried only at the job start and then cached. */
252 s = block_job_create(job_id, &stream_job_driver, bs,
253 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
254 BLK_PERM_GRAPH_MOD,
255 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
256 BLK_PERM_WRITE,
257 speed, BLOCK_JOB_DEFAULT, NULL, NULL, errp);
258 if (!s) {
259 goto fail;
260 }
261
262 /* Block all intermediate nodes between bs and base, because they will
263 * disappear from the chain after this operation. The streaming job reads
264 * every block only once, assuming that it doesn't change, so block writes
265 * and resizes. */
Alberto Garcia61b49e42016-10-28 10:08:10 +0300266 for (iter = backing_bs(bs); iter && iter != base; iter = backing_bs(iter)) {
Kevin Wolf76d554e2017-01-17 11:56:42 +0100267 block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
Kevin Wolfa170a912017-02-09 13:34:18 +0100268 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED,
269 &error_abort);
Alberto Garcia61b49e42016-10-28 10:08:10 +0300270 }
271
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000272 s->base = base;
Jeff Cody13d8cc52014-06-25 15:40:11 -0400273 s->backing_file_str = g_strdup(backing_file_str);
Alberto Garcia61b49e42016-10-28 10:08:10 +0300274 s->bs_flags = orig_bs_flags;
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000275
Paolo Bonzini1d809092012-09-28 17:22:59 +0200276 s->on_error = on_error;
John Snow5ccac6f2016-11-08 01:50:37 -0500277 trace_stream_start(bs, base, s);
278 block_job_start(&s->common);
Kevin Wolfa170a912017-02-09 13:34:18 +0100279 return;
280
281fail:
282 if (orig_bs_flags != bdrv_get_flags(bs)) {
Alberto Garcia525989a2017-05-15 12:34:24 +0300283 bdrv_reopen(bs, orig_bs_flags, NULL);
Kevin Wolfa170a912017-02-09 13:34:18 +0100284 }
Stefan Hajnoczi4f1043b2012-01-18 14:40:44 +0000285}