]> git.proxmox.com Git - mirror_qemu.git/blame - block/stream.c
block: User BdrvChild callback for device name
[mirror_qemu.git] / block / stream.c
CommitLineData
4f1043b4
SH
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
80c71a24 14#include "qemu/osdep.h"
4f1043b4 15#include "trace.h"
737e150e
PB
16#include "block/block_int.h"
17#include "block/blockjob.h"
da34e65c 18#include "qapi/error.h"
cc7a8ea7 19#include "qapi/qmp/qerror.h"
6ef228fc 20#include "qemu/ratelimit.h"
373340b2 21#include "sysemu/block-backend.h"
4f1043b4
SH
22
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
5094a6c0
SH
32#define SLICE_TIME 100000000ULL /* ns */
33
4f1043b4
SH
34typedef struct StreamBlockJob {
35 BlockJob common;
5094a6c0 36 RateLimit limit;
4f1043b4 37 BlockDriverState *base;
1d809098 38 BlockdevOnError on_error;
13d8cc51 39 char *backing_file_str;
4f1043b4
SH
40} StreamBlockJob;
41
42static int coroutine_fn stream_populate(BlockDriverState *bs,
43 int64_t sector_num, int nb_sectors,
44 void *buf)
45{
46 struct iovec iov = {
47 .iov_base = buf,
48 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
49 };
50 QEMUIOVector qiov;
51
52 qemu_iovec_init_external(&qiov, &iov, 1);
53
54 /* Copy-on-read the unallocated clusters */
55 return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
56}
57
f3e69beb
SH
58typedef struct {
59 int ret;
60 bool reached_end;
61} StreamCompleteData;
62
63static void stream_complete(BlockJob *job, void *opaque)
64{
65 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
66 StreamCompleteData *data = opaque;
67 BlockDriverState *base = s->base;
68
69 if (!block_job_is_cancelled(&s->common) && data->reached_end &&
70 data->ret == 0) {
71 const char *base_id = NULL, *base_fmt = NULL;
72 if (base) {
73 base_id = s->backing_file_str;
74 if (base->drv) {
75 base_fmt = base->drv->format_name;
76 }
77 }
78 data->ret = bdrv_change_backing_file(job->bs, base_id, base_fmt);
5db15a57 79 bdrv_set_backing_hd(job->bs, base);
f3e69beb
SH
80 }
81
82 g_free(s->backing_file_str);
83 block_job_completed(&s->common, data->ret);
84 g_free(data);
85}
86
4f1043b4
SH
87static void coroutine_fn stream_run(void *opaque)
88{
89 StreamBlockJob *s = opaque;
f3e69beb 90 StreamCompleteData *data;
4f1043b4 91 BlockDriverState *bs = s->common.bs;
c8c3080f 92 BlockDriverState *base = s->base;
6578629e
AG
93 int64_t sector_num = 0;
94 int64_t end = -1;
1d809098 95 int error = 0;
4f1043b4 96 int ret = 0;
04120e3b 97 int n = 0;
4f1043b4
SH
98 void *buf;
99
760e0063 100 if (!bs->backing) {
6578629e 101 goto out;
f4a193e7
HR
102 }
103
4f1043b4
SH
104 s->common.len = bdrv_getlength(bs);
105 if (s->common.len < 0) {
6578629e
AG
106 ret = s->common.len;
107 goto out;
4f1043b4
SH
108 }
109
110 end = s->common.len >> BDRV_SECTOR_BITS;
111 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
112
113 /* Turn on copy-on-read for the whole block device so that guest read
114 * requests help us make progress. Only do this when copying the entire
115 * backing chain since the copy-on-read operation does not take base into
116 * account.
117 */
118 if (!base) {
119 bdrv_enable_copy_on_read(bs);
120 }
121
122 for (sector_num = 0; sector_num < end; sector_num += n) {
4513eafe 123 uint64_t delay_ns = 0;
f9749f28 124 bool copy;
4513eafe
PB
125
126wait:
127 /* Note that even when no rate limit is applied we need to yield
c57b6656 128 * with no pending I/O here so that bdrv_drain_all() returns.
4513eafe 129 */
7483d1e5 130 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
4f1043b4
SH
131 if (block_job_is_cancelled(&s->common)) {
132 break;
133 }
134
c3e4f43a
SW
135 copy = false;
136
bdad13b9
PB
137 ret = bdrv_is_allocated(bs, sector_num,
138 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
f9749f28
PB
139 if (ret == 1) {
140 /* Allocated in the top, no need to copy. */
d663640c 141 } else if (ret >= 0) {
f9749f28
PB
142 /* Copy if allocated in the intermediate images. Limit to the
143 * known-unallocated area [sector_num, sector_num+n). */
760e0063 144 ret = bdrv_is_allocated_above(backing_bs(bs), base,
4f578637 145 sector_num, n, &n);
571cd9dc
SH
146
147 /* Finish early if end of backing file has been reached */
148 if (ret == 0 && n == 0) {
149 n = end - sector_num;
150 }
151
f9749f28
PB
152 copy = (ret == 1);
153 }
4f1043b4 154 trace_stream_one_iteration(s, sector_num, n, ret);
c3e4f43a 155 if (copy) {
5094a6c0 156 if (s->common.speed) {
4513eafe 157 delay_ns = ratelimit_calculate_delay(&s->limit, n);
5094a6c0 158 if (delay_ns > 0) {
4513eafe 159 goto wait;
5094a6c0
SH
160 }
161 }
4f1043b4
SH
162 ret = stream_populate(bs, sector_num, n, buf);
163 }
164 if (ret < 0) {
1d809098
PB
165 BlockErrorAction action =
166 block_job_error_action(&s->common, s->common.bs, s->on_error,
167 true, -ret);
a589569f 168 if (action == BLOCK_ERROR_ACTION_STOP) {
1d809098
PB
169 n = 0;
170 continue;
171 }
172 if (error == 0) {
173 error = ret;
174 }
a589569f 175 if (action == BLOCK_ERROR_ACTION_REPORT) {
1d809098
PB
176 break;
177 }
4f1043b4 178 }
c8c3080f 179 ret = 0;
4f1043b4
SH
180
181 /* Publish progress */
182 s->common.offset += n * BDRV_SECTOR_SIZE;
183 }
184
185 if (!base) {
186 bdrv_disable_copy_on_read(bs);
187 }
188
1d809098
PB
189 /* Do not remove the backing file if an error was there but ignored. */
190 ret = error;
191
4f1043b4 192 qemu_vfree(buf);
f3e69beb 193
6578629e 194out:
f3e69beb
SH
195 /* Modify backing chain and close BDSes in main loop */
196 data = g_malloc(sizeof(*data));
197 data->ret = ret;
198 data->reached_end = sector_num == end;
199 block_job_defer_to_main_loop(&s->common, stream_complete, data);
4f1043b4
SH
200}
201
882ec7ce 202static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
5094a6c0
SH
203{
204 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
205
882ec7ce 206 if (speed < 0) {
c6bd8c70 207 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
9e6636c7 208 return;
5094a6c0 209 }
6ef228fc 210 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
5094a6c0
SH
211}
212
3fc4b10a 213static const BlockJobDriver stream_job_driver = {
4f1043b4 214 .instance_size = sizeof(StreamBlockJob),
79e14bf7 215 .job_type = BLOCK_JOB_TYPE_STREAM,
5094a6c0 216 .set_speed = stream_set_speed,
4f1043b4
SH
217};
218
fd7f8c65 219void stream_start(BlockDriverState *bs, BlockDriverState *base,
13d8cc51 220 const char *backing_file_str, int64_t speed,
1d809098 221 BlockdevOnError on_error,
097310b5 222 BlockCompletionFunc *cb,
fd7f8c65 223 void *opaque, Error **errp)
4f1043b4
SH
224{
225 StreamBlockJob *s;
4f1043b4 226
1d809098
PB
227 if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
228 on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
373340b2 229 (!bs->blk || !blk_iostatus_is_enabled(bs->blk))) {
c6bd8c70 230 error_setg(errp, QERR_INVALID_PARAMETER, "on-error");
1d809098
PB
231 return;
232 }
233
3fc4b10a 234 s = block_job_create(&stream_job_driver, bs, speed, cb, opaque, errp);
4f1043b4 235 if (!s) {
fd7f8c65 236 return;
4f1043b4
SH
237 }
238
239 s->base = base;
13d8cc51 240 s->backing_file_str = g_strdup(backing_file_str);
4f1043b4 241
1d809098 242 s->on_error = on_error;
fa4478d5
PB
243 s->common.co = qemu_coroutine_create(stream_run);
244 trace_stream_start(bs, base, s, s->common.co, opaque);
245 qemu_coroutine_enter(s->common.co, s);
4f1043b4 246}