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