]> git.proxmox.com Git - qemu.git/blame - block/stream.c
block: introduce block job error
[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
14#include "trace.h"
15#include "block_int.h"
2f0c9fe6 16#include "blockjob.h"
6ef228fc 17#include "qemu/ratelimit.h"
4f1043b4
SH
18
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
5094a6c0
SH
28#define SLICE_TIME 100000000ULL /* ns */
29
4f1043b4
SH
30typedef struct StreamBlockJob {
31 BlockJob common;
5094a6c0 32 RateLimit limit;
4f1043b4 33 BlockDriverState *base;
c8c3080f 34 char backing_file_id[1024];
4f1043b4
SH
35} StreamBlockJob;
36
37static int coroutine_fn stream_populate(BlockDriverState *bs,
38 int64_t sector_num, int nb_sectors,
39 void *buf)
40{
41 struct iovec iov = {
42 .iov_base = buf,
43 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
44 };
45 QEMUIOVector qiov;
46
47 qemu_iovec_init_external(&qiov, &iov, 1);
48
49 /* Copy-on-read the unallocated clusters */
50 return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
51}
52
5a67a104
MT
53static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
54 const char *base_id)
55{
56 BlockDriverState *intermediate;
57 intermediate = top->backing_hd;
58
59 while (intermediate) {
60 BlockDriverState *unused;
61
62 /* reached base */
63 if (intermediate == base) {
64 break;
65 }
66
67 unused = intermediate;
68 intermediate = intermediate->backing_hd;
69 unused->backing_hd = NULL;
70 bdrv_delete(unused);
71 }
72 top->backing_hd = base;
5a67a104
MT
73}
74
4f1043b4
SH
75static void coroutine_fn stream_run(void *opaque)
76{
77 StreamBlockJob *s = opaque;
78 BlockDriverState *bs = s->common.bs;
c8c3080f 79 BlockDriverState *base = s->base;
4f1043b4
SH
80 int64_t sector_num, end;
81 int ret = 0;
04120e3b 82 int n = 0;
4f1043b4
SH
83 void *buf;
84
85 s->common.len = bdrv_getlength(bs);
86 if (s->common.len < 0) {
87 block_job_complete(&s->common, s->common.len);
88 return;
89 }
90
91 end = s->common.len >> BDRV_SECTOR_BITS;
92 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
93
94 /* Turn on copy-on-read for the whole block device so that guest read
95 * requests help us make progress. Only do this when copying the entire
96 * backing chain since the copy-on-read operation does not take base into
97 * account.
98 */
99 if (!base) {
100 bdrv_enable_copy_on_read(bs);
101 }
102
103 for (sector_num = 0; sector_num < end; sector_num += n) {
4513eafe 104 uint64_t delay_ns = 0;
f9749f28 105 bool copy;
4513eafe
PB
106
107wait:
108 /* Note that even when no rate limit is applied we need to yield
109 * with no pending I/O here so that qemu_aio_flush() returns.
110 */
111 block_job_sleep_ns(&s->common, rt_clock, delay_ns);
4f1043b4
SH
112 if (block_job_is_cancelled(&s->common)) {
113 break;
114 }
115
f9749f28
PB
116 ret = bdrv_co_is_allocated(bs, sector_num,
117 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
118 if (ret == 1) {
119 /* Allocated in the top, no need to copy. */
120 copy = false;
121 } else {
122 /* Copy if allocated in the intermediate images. Limit to the
123 * known-unallocated area [sector_num, sector_num+n). */
188a7bbf
PB
124 ret = bdrv_co_is_allocated_above(bs->backing_hd, base,
125 sector_num, n, &n);
571cd9dc
SH
126
127 /* Finish early if end of backing file has been reached */
128 if (ret == 0 && n == 0) {
129 n = end - sector_num;
130 }
131
f9749f28
PB
132 copy = (ret == 1);
133 }
4f1043b4 134 trace_stream_one_iteration(s, sector_num, n, ret);
f9749f28 135 if (ret >= 0 && copy) {
5094a6c0 136 if (s->common.speed) {
4513eafe 137 delay_ns = ratelimit_calculate_delay(&s->limit, n);
5094a6c0 138 if (delay_ns > 0) {
4513eafe 139 goto wait;
5094a6c0
SH
140 }
141 }
4f1043b4
SH
142 ret = stream_populate(bs, sector_num, n, buf);
143 }
144 if (ret < 0) {
145 break;
146 }
c8c3080f 147 ret = 0;
4f1043b4
SH
148
149 /* Publish progress */
150 s->common.offset += n * BDRV_SECTOR_SIZE;
151 }
152
153 if (!base) {
154 bdrv_disable_copy_on_read(bs);
155 }
156
3e914655 157 if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) {
f6133def 158 const char *base_id = NULL, *base_fmt = NULL;
c8c3080f
MT
159 if (base) {
160 base_id = s->backing_file_id;
f6133def
PB
161 if (base->drv) {
162 base_fmt = base->drv->format_name;
163 }
c8c3080f 164 }
f6133def 165 ret = bdrv_change_backing_file(bs, base_id, base_fmt);
5a67a104 166 close_unused_images(bs, base, base_id);
4f1043b4
SH
167 }
168
169 qemu_vfree(buf);
170 block_job_complete(&s->common, ret);
171}
172
882ec7ce 173static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
5094a6c0
SH
174{
175 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
176
882ec7ce
SH
177 if (speed < 0) {
178 error_set(errp, QERR_INVALID_PARAMETER, "speed");
9e6636c7 179 return;
5094a6c0 180 }
6ef228fc 181 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
5094a6c0
SH
182}
183
4f1043b4
SH
184static BlockJobType stream_job_type = {
185 .instance_size = sizeof(StreamBlockJob),
186 .job_type = "stream",
5094a6c0 187 .set_speed = stream_set_speed,
4f1043b4
SH
188};
189
fd7f8c65 190void stream_start(BlockDriverState *bs, BlockDriverState *base,
c83c66c3
SH
191 const char *base_id, int64_t speed,
192 BlockDriverCompletionFunc *cb,
fd7f8c65 193 void *opaque, Error **errp)
4f1043b4
SH
194{
195 StreamBlockJob *s;
4f1043b4 196
c83c66c3 197 s = block_job_create(&stream_job_type, bs, speed, cb, opaque, errp);
4f1043b4 198 if (!s) {
fd7f8c65 199 return;
4f1043b4
SH
200 }
201
202 s->base = base;
c8c3080f
MT
203 if (base_id) {
204 pstrcpy(s->backing_file_id, sizeof(s->backing_file_id), base_id);
205 }
4f1043b4 206
fa4478d5
PB
207 s->common.co = qemu_coroutine_create(stream_run);
208 trace_stream_start(bs, base, s, s->common.co, opaque);
209 qemu_coroutine_enter(s->common.co, s);
4f1043b4 210}