]> git.proxmox.com Git - mirror_qemu.git/blob - block/blkverify.c
qobject: Replace qobject_incref/QINCREF qobject_decref/QDECREF
[mirror_qemu.git] / block / blkverify.c
1 /*
2 * Block protocol for block driver correctness testing
3 *
4 * Copyright (C) 2010 IBM, Corp.
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */
13 #include "block/block_int.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qstring.h"
16 #include "qemu/cutils.h"
17 #include "qemu/option.h"
18
19 typedef struct {
20 BdrvChild *test_file;
21 } BDRVBlkverifyState;
22
23 typedef struct BlkverifyRequest {
24 Coroutine *co;
25 BlockDriverState *bs;
26
27 /* Request metadata */
28 bool is_write;
29 uint64_t offset;
30 uint64_t bytes;
31 int flags;
32
33 int (*request_fn)(BdrvChild *, int64_t, unsigned int, QEMUIOVector *,
34 BdrvRequestFlags);
35
36 int ret; /* test image result */
37 int raw_ret; /* raw image result */
38
39 unsigned int done; /* completion counter */
40
41 QEMUIOVector *qiov; /* user I/O vector */
42 QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */
43 } BlkverifyRequest;
44
45 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyRequest *r,
46 const char *fmt, ...)
47 {
48 va_list ap;
49
50 va_start(ap, fmt);
51 fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ",
52 r->is_write ? "write" : "read", r->offset, r->bytes);
53 vfprintf(stderr, fmt, ap);
54 fprintf(stderr, "\n");
55 va_end(ap);
56 exit(1);
57 }
58
59 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
60 static void blkverify_parse_filename(const char *filename, QDict *options,
61 Error **errp)
62 {
63 const char *c;
64 QString *raw_path;
65
66
67 /* Parse the blkverify: prefix */
68 if (!strstart(filename, "blkverify:", &filename)) {
69 /* There was no prefix; therefore, all options have to be already
70 present in the QDict (except for the filename) */
71 qdict_put_str(options, "x-image", filename);
72 return;
73 }
74
75 /* Parse the raw image filename */
76 c = strchr(filename, ':');
77 if (c == NULL) {
78 error_setg(errp, "blkverify requires raw copy and original image path");
79 return;
80 }
81
82 /* TODO Implement option pass-through and set raw.filename here */
83 raw_path = qstring_from_substr(filename, 0, c - filename - 1);
84 qdict_put(options, "x-raw", raw_path);
85
86 /* TODO Allow multi-level nesting and set file.filename here */
87 filename = c + 1;
88 qdict_put_str(options, "x-image", filename);
89 }
90
91 static QemuOptsList runtime_opts = {
92 .name = "blkverify",
93 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
94 .desc = {
95 {
96 .name = "x-raw",
97 .type = QEMU_OPT_STRING,
98 .help = "[internal use only, will be removed]",
99 },
100 {
101 .name = "x-image",
102 .type = QEMU_OPT_STRING,
103 .help = "[internal use only, will be removed]",
104 },
105 { /* end of list */ }
106 },
107 };
108
109 static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
110 Error **errp)
111 {
112 BDRVBlkverifyState *s = bs->opaque;
113 QemuOpts *opts;
114 Error *local_err = NULL;
115 int ret;
116
117 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
118 qemu_opts_absorb_qdict(opts, options, &local_err);
119 if (local_err) {
120 error_propagate(errp, local_err);
121 ret = -EINVAL;
122 goto fail;
123 }
124
125 /* Open the raw file */
126 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw",
127 bs, &child_file, false, &local_err);
128 if (local_err) {
129 ret = -EINVAL;
130 error_propagate(errp, local_err);
131 goto fail;
132 }
133
134 /* Open the test file */
135 s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
136 "test", bs, &child_format, false,
137 &local_err);
138 if (local_err) {
139 ret = -EINVAL;
140 error_propagate(errp, local_err);
141 goto fail;
142 }
143
144 ret = 0;
145 fail:
146 qemu_opts_del(opts);
147 return ret;
148 }
149
150 static void blkverify_close(BlockDriverState *bs)
151 {
152 BDRVBlkverifyState *s = bs->opaque;
153
154 bdrv_unref_child(bs, s->test_file);
155 s->test_file = NULL;
156 }
157
158 static int64_t blkverify_getlength(BlockDriverState *bs)
159 {
160 BDRVBlkverifyState *s = bs->opaque;
161
162 return bdrv_getlength(s->test_file->bs);
163 }
164
165 static void coroutine_fn blkverify_do_test_req(void *opaque)
166 {
167 BlkverifyRequest *r = opaque;
168 BDRVBlkverifyState *s = r->bs->opaque;
169
170 r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
171 r->flags);
172 r->done++;
173 qemu_coroutine_enter_if_inactive(r->co);
174 }
175
176 static void coroutine_fn blkverify_do_raw_req(void *opaque)
177 {
178 BlkverifyRequest *r = opaque;
179
180 r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
181 r->flags);
182 r->done++;
183 qemu_coroutine_enter_if_inactive(r->co);
184 }
185
186 static int coroutine_fn
187 blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
188 uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
189 int flags, bool is_write)
190 {
191 Coroutine *co_a, *co_b;
192
193 *r = (BlkverifyRequest) {
194 .co = qemu_coroutine_self(),
195 .bs = bs,
196 .offset = offset,
197 .bytes = bytes,
198 .qiov = qiov,
199 .raw_qiov = raw_qiov,
200 .flags = flags,
201 .is_write = is_write,
202 .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
203 };
204
205 co_a = qemu_coroutine_create(blkverify_do_test_req, r);
206 co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
207
208 qemu_coroutine_enter(co_a);
209 qemu_coroutine_enter(co_b);
210
211 while (r->done < 2) {
212 qemu_coroutine_yield();
213 }
214
215 if (r->ret != r->raw_ret) {
216 blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
217 }
218
219 return r->ret;
220 }
221
222 static int coroutine_fn
223 blkverify_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
224 QEMUIOVector *qiov, int flags)
225 {
226 BlkverifyRequest r;
227 QEMUIOVector raw_qiov;
228 void *buf;
229 ssize_t cmp_offset;
230 int ret;
231
232 buf = qemu_blockalign(bs->file->bs, qiov->size);
233 qemu_iovec_init(&raw_qiov, qiov->niov);
234 qemu_iovec_clone(&raw_qiov, qiov, buf);
235
236 ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, flags,
237 false);
238
239 cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
240 if (cmp_offset != -1) {
241 blkverify_err(&r, "contents mismatch at offset %" PRId64,
242 offset + cmp_offset);
243 }
244
245 qemu_iovec_destroy(&raw_qiov);
246 qemu_vfree(buf);
247
248 return ret;
249 }
250
251 static int coroutine_fn
252 blkverify_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
253 QEMUIOVector *qiov, int flags)
254 {
255 BlkverifyRequest r;
256 return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
257 }
258
259 static int blkverify_co_flush(BlockDriverState *bs)
260 {
261 BDRVBlkverifyState *s = bs->opaque;
262
263 /* Only flush test file, the raw file is not important */
264 return bdrv_co_flush(s->test_file->bs);
265 }
266
267 static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs,
268 BlockDriverState *candidate)
269 {
270 BDRVBlkverifyState *s = bs->opaque;
271
272 bool perm = bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
273
274 if (perm) {
275 return true;
276 }
277
278 return bdrv_recurse_is_first_non_filter(s->test_file->bs, candidate);
279 }
280
281 static void blkverify_refresh_filename(BlockDriverState *bs, QDict *options)
282 {
283 BDRVBlkverifyState *s = bs->opaque;
284
285 /* bs->file->bs has already been refreshed */
286 bdrv_refresh_filename(s->test_file->bs);
287
288 if (bs->file->bs->full_open_options
289 && s->test_file->bs->full_open_options)
290 {
291 QDict *opts = qdict_new();
292 qdict_put_str(opts, "driver", "blkverify");
293
294 qobject_ref(bs->file->bs->full_open_options);
295 qdict_put(opts, "raw", bs->file->bs->full_open_options);
296 qobject_ref(s->test_file->bs->full_open_options);
297 qdict_put(opts, "test", s->test_file->bs->full_open_options);
298
299 bs->full_open_options = opts;
300 }
301
302 if (bs->file->bs->exact_filename[0]
303 && s->test_file->bs->exact_filename[0])
304 {
305 int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
306 "blkverify:%s:%s",
307 bs->file->bs->exact_filename,
308 s->test_file->bs->exact_filename);
309 if (ret >= sizeof(bs->exact_filename)) {
310 /* An overflow makes the filename unusable, so do not report any */
311 bs->exact_filename[0] = 0;
312 }
313 }
314 }
315
316 static BlockDriver bdrv_blkverify = {
317 .format_name = "blkverify",
318 .protocol_name = "blkverify",
319 .instance_size = sizeof(BDRVBlkverifyState),
320
321 .bdrv_parse_filename = blkverify_parse_filename,
322 .bdrv_file_open = blkverify_open,
323 .bdrv_close = blkverify_close,
324 .bdrv_child_perm = bdrv_filter_default_perms,
325 .bdrv_getlength = blkverify_getlength,
326 .bdrv_refresh_filename = blkverify_refresh_filename,
327
328 .bdrv_co_preadv = blkverify_co_preadv,
329 .bdrv_co_pwritev = blkverify_co_pwritev,
330 .bdrv_co_flush = blkverify_co_flush,
331
332 .is_filter = true,
333 .bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
334 };
335
336 static void bdrv_blkverify_init(void)
337 {
338 bdrv_register(&bdrv_blkverify);
339 }
340
341 block_init(bdrv_blkverify_init);