Bug Summary

File:block/stream.c
Location:line 171, column 9
Description:Value stored to 'ret' is never read

Annotated Source Code

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/block_int.h"
16#include "block/blockjob.h"
17#include "qemu/ratelimit.h"
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
28#define SLICE_TIME100000000ULL 100000000ULL /* ns */
29
30typedef struct StreamBlockJob {
31 BlockJob common;
32 RateLimit limit;
33 BlockDriverState *base;
34 BlockdevOnError on_error;
35 char backing_file_id[1024];
36} 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(1ULL << 9),
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
54static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
55 const char *base_id)
56{
57 BlockDriverState *intermediate;
58 intermediate = top->backing_hd;
59
60 /* Must assign before bdrv_delete() to prevent traversing dangling pointer
61 * while we delete backing image instances.
62 */
63 top->backing_hd = base;
64
65 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;
75 unused->backing_hd = NULL((void*)0);
76 bdrv_unref(unused);
77 }
78}
79
80static void coroutine_fn stream_run(void *opaque)
81{
82 StreamBlockJob *s = opaque;
83 BlockDriverState *bs = s->common.bs;
84 BlockDriverState *base = s->base;
85 int64_t sector_num, end;
86 int error = 0;
87 int ret = 0;
88 int n = 0;
89 void *buf;
90
91 if (!bs->backing_hd) {
92 block_job_completed(&s->common, 0);
93 return;
94 }
95
96 s->common.len = bdrv_getlength(bs);
97 if (s->common.len < 0) {
98 block_job_completed(&s->common, s->common.len);
99 return;
100 }
101
102 end = s->common.len >> BDRV_SECTOR_BITS9;
103 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
104
105 /* Turn on copy-on-read for the whole block device so that guest read
106 * requests help us make progress. Only do this when copying the entire
107 * backing chain since the copy-on-read operation does not take base into
108 * account.
109 */
110 if (!base) {
111 bdrv_enable_copy_on_read(bs);
112 }
113
114 for (sector_num = 0; sector_num < end; sector_num += n) {
115 uint64_t delay_ns = 0;
116 bool_Bool copy;
117
118wait:
119 /* Note that even when no rate limit is applied we need to yield
120 * with no pending I/O here so that bdrv_drain_all() returns.
121 */
122 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
123 if (block_job_is_cancelled(&s->common)) {
124 break;
125 }
126
127 copy = false0;
128
129 ret = bdrv_is_allocated(bs, sector_num,
130 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE(1ULL << 9), &n);
131 if (ret == 1) {
132 /* Allocated in the top, no need to copy. */
133 } else if (ret >= 0) {
134 /* Copy if allocated in the intermediate images. Limit to the
135 * known-unallocated area [sector_num, sector_num+n). */
136 ret = bdrv_is_allocated_above(bs->backing_hd, base,
137 sector_num, n, &n);
138
139 /* Finish early if end of backing file has been reached */
140 if (ret == 0 && n == 0) {
141 n = end - sector_num;
142 }
143
144 copy = (ret == 1);
145 }
146 trace_stream_one_iteration(s, sector_num, n, ret);
147 if (copy) {
148 if (s->common.speed) {
149 delay_ns = ratelimit_calculate_delay(&s->limit, n);
150 if (delay_ns > 0) {
151 goto wait;
152 }
153 }
154 ret = stream_populate(bs, sector_num, n, buf);
155 }
156 if (ret < 0) {
157 BlockErrorAction action =
158 block_job_error_action(&s->common, s->common.bs, s->on_error,
159 true1, -ret);
160 if (action == BDRV_ACTION_STOP) {
161 n = 0;
162 continue;
163 }
164 if (error == 0) {
165 error = ret;
166 }
167 if (action == BDRV_ACTION_REPORT) {
168 break;
169 }
170 }
171 ret = 0;
Value stored to 'ret' is never read
172
173 /* Publish progress */
174 s->common.offset += n * BDRV_SECTOR_SIZE(1ULL << 9);
175 }
176
177 if (!base) {
178 bdrv_disable_copy_on_read(bs);
179 }
180
181 /* Do not remove the backing file if an error was there but ignored. */
182 ret = error;
183
184 if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) {
185 const char *base_id = NULL((void*)0), *base_fmt = NULL((void*)0);
186 if (base) {
187 base_id = s->backing_file_id;
188 if (base->drv) {
189 base_fmt = base->drv->format_name;
190 }
191 }
192 ret = bdrv_change_backing_file(bs, base_id, base_fmt);
193 close_unused_images(bs, base, base_id);
194 }
195
196 qemu_vfree(buf);
197 block_job_completed(&s->common, ret);
198}
199
200static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
201{
202 StreamBlockJob *s = container_of(job, StreamBlockJob, common)({ const typeof(((StreamBlockJob *) 0)->common) *__mptr = (
job); (StreamBlockJob *) ((char *) __mptr - __builtin_offsetof
(StreamBlockJob, common));})
;
203
204 if (speed < 0) {
205 error_set(errp, QERR_INVALID_PARAMETERERROR_CLASS_GENERIC_ERROR, "Invalid parameter '%s'", "speed");
206 return;
207 }
208 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE(1ULL << 9), SLICE_TIME100000000ULL);
209}
210
211static const BlockJobDriver stream_job_driver = {
212 .instance_size = sizeof(StreamBlockJob),
213 .job_type = BLOCK_JOB_TYPE_STREAM,
214 .set_speed = stream_set_speed,
215};
216
217void stream_start(BlockDriverState *bs, BlockDriverState *base,
218 const char *base_id, int64_t speed,
219 BlockdevOnError on_error,
220 BlockDriverCompletionFunc *cb,
221 void *opaque, Error **errp)
222{
223 StreamBlockJob *s;
224
225 if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
226 on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
227 !bdrv_iostatus_is_enabled(bs)) {
228 error_set(errp, QERR_INVALID_PARAMETERERROR_CLASS_GENERIC_ERROR, "Invalid parameter '%s'", "on-error");
229 return;
230 }
231
232 s = block_job_create(&stream_job_driver, bs, speed, cb, opaque, errp);
233 if (!s) {
234 return;
235 }
236
237 s->base = base;
238 if (base_id) {
239 pstrcpy(s->backing_file_id, sizeof(s->backing_file_id), base_id);
240 }
241
242 s->on_error = on_error;
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);
246}