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