]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkverify.c
hw/arm/virt: Add 'compact-highmem' property
[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"
0b8fa32f 17#include "qemu/module.h"
922a01a0 18#include "qemu/option.h"
5df022cf 19#include "qemu/memalign.h"
d9d33417
SH
20
21typedef struct {
3e586be0 22 BdrvChild *test_file;
d9d33417
SH
23} BDRVBlkverifyState;
24
44b67892
KW
25typedef struct BlkverifyRequest {
26 Coroutine *co;
27 BlockDriverState *bs;
d9d33417
SH
28
29 /* Request metadata */
30 bool is_write;
44b67892
KW
31 uint64_t offset;
32 uint64_t bytes;
33 int flags;
d9d33417 34
e9e52efd 35 int (*request_fn)(BdrvChild *, int64_t, int64_t, QEMUIOVector *,
44b67892 36 BdrvRequestFlags);
d9d33417 37
44b67892
KW
38 int ret; /* test image result */
39 int raw_ret; /* raw image result */
d9d33417 40
44b67892 41 unsigned int done; /* completion counter */
d9d33417 42
44b67892
KW
43 QEMUIOVector *qiov; /* user I/O vector */
44 QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */
45} BlkverifyRequest;
d9d33417 46
9edc6313 47static void G_GNUC_PRINTF(2, 3) blkverify_err(BlkverifyRequest *r,
a77cffe7 48 const char *fmt, ...)
d9d33417
SH
49{
50 va_list ap;
51
52 va_start(ap, fmt);
44b67892
KW
53 fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ",
54 r->is_write ? "write" : "read", r->offset, r->bytes);
d9d33417
SH
55 vfprintf(stderr, fmt, ap);
56 fprintf(stderr, "\n");
57 va_end(ap);
58 exit(1);
59}
60
61/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
16c79092
KW
62static void blkverify_parse_filename(const char *filename, QDict *options,
63 Error **errp)
d9d33417 64{
16c79092
KW
65 const char *c;
66 QString *raw_path;
67
d9d33417
SH
68
69 /* Parse the blkverify: prefix */
16c79092 70 if (!strstart(filename, "blkverify:", &filename)) {
22511ad6
HR
71 /* There was no prefix; therefore, all options have to be already
72 present in the QDict (except for the filename) */
46f5ac20 73 qdict_put_str(options, "x-image", filename);
16c79092 74 return;
d9d33417 75 }
d9d33417
SH
76
77 /* Parse the raw image filename */
78 c = strchr(filename, ':');
79 if (c == NULL) {
16c79092
KW
80 error_setg(errp, "blkverify requires raw copy and original image path");
81 return;
82 }
83
84 /* TODO Implement option pass-through and set raw.filename here */
ba891d68 85 raw_path = qstring_from_substr(filename, 0, c - filename);
16c79092
KW
86 qdict_put(options, "x-raw", raw_path);
87
88 /* TODO Allow multi-level nesting and set file.filename here */
89 filename = c + 1;
46f5ac20 90 qdict_put_str(options, "x-image", filename);
16c79092
KW
91}
92
93static QemuOptsList runtime_opts = {
94 .name = "blkverify",
95 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
96 .desc = {
97 {
98 .name = "x-raw",
99 .type = QEMU_OPT_STRING,
100 .help = "[internal use only, will be removed]",
101 },
102 {
103 .name = "x-image",
104 .type = QEMU_OPT_STRING,
105 .help = "[internal use only, will be removed]",
106 },
107 { /* end of list */ }
108 },
109};
110
015a1036
HR
111static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
112 Error **errp)
16c79092
KW
113{
114 BDRVBlkverifyState *s = bs->opaque;
115 QemuOpts *opts;
16c79092
KW
116 int ret;
117
87ea75d5 118 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
af175e85 119 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
16c79092
KW
120 ret = -EINVAL;
121 goto fail;
122 }
123
70b6198a 124 /* Open the raw file */
83930780
VSO
125 ret = bdrv_open_file_child(qemu_opt_get(opts, "x-raw"), options, "raw",
126 bs, errp);
127 if (ret < 0) {
16c79092 128 goto fail;
d9d33417 129 }
d9d33417
SH
130
131 /* Open the test file */
3e586be0 132 s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
36ee58d1 133 "test", bs, &child_of_bds, BDRV_CHILD_DATA,
bc520249
VSO
134 false, errp);
135 if (!s->test_file) {
3e586be0 136 ret = -EINVAL;
16c79092 137 goto fail;
d9d33417
SH
138 }
139
228345bf
HR
140 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
141 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
142
16c79092
KW
143 ret = 0;
144fail:
31585931 145 qemu_opts_del(opts);
16c79092 146 return ret;
d9d33417
SH
147}
148
149static void blkverify_close(BlockDriverState *bs)
150{
151 BDRVBlkverifyState *s = bs->opaque;
152
3e586be0 153 bdrv_unref_child(bs, s->test_file);
d9d33417
SH
154 s->test_file = NULL;
155}
156
d9d33417
SH
157static int64_t blkverify_getlength(BlockDriverState *bs)
158{
159 BDRVBlkverifyState *s = bs->opaque;
160
3e586be0 161 return bdrv_getlength(s->test_file->bs);
d9d33417
SH
162}
163
44b67892 164static void coroutine_fn blkverify_do_test_req(void *opaque)
d9d33417 165{
44b67892
KW
166 BlkverifyRequest *r = opaque;
167 BDRVBlkverifyState *s = r->bs->opaque;
168
169 r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
170 r->flags);
171 r->done++;
172 qemu_coroutine_enter_if_inactive(r->co);
d9d33417
SH
173}
174
44b67892 175static void coroutine_fn blkverify_do_raw_req(void *opaque)
d9d33417 176{
44b67892 177 BlkverifyRequest *r = opaque;
d9d33417 178
44b67892
KW
179 r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
180 r->flags);
181 r->done++;
182 qemu_coroutine_enter_if_inactive(r->co);
d9d33417
SH
183}
184
44b67892
KW
185static int coroutine_fn
186blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
187 uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
188 int flags, bool is_write)
d9d33417 189{
44b67892
KW
190 Coroutine *co_a, *co_b;
191
192 *r = (BlkverifyRequest) {
193 .co = qemu_coroutine_self(),
194 .bs = bs,
195 .offset = offset,
196 .bytes = bytes,
197 .qiov = qiov,
198 .raw_qiov = raw_qiov,
199 .flags = flags,
200 .is_write = is_write,
201 .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
202 };
203
204 co_a = qemu_coroutine_create(blkverify_do_test_req, r);
205 co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
206
207 qemu_coroutine_enter(co_a);
208 qemu_coroutine_enter(co_b);
209
210 while (r->done < 2) {
211 qemu_coroutine_yield();
212 }
d9d33417 213
44b67892
KW
214 if (r->ret != r->raw_ret) {
215 blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
d9d33417 216 }
44b67892
KW
217
218 return r->ret;
d9d33417
SH
219}
220
44b67892 221static int coroutine_fn
f7ef38dd
VSO
222blkverify_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
223 QEMUIOVector *qiov, BdrvRequestFlags flags)
d9d33417 224{
44b67892
KW
225 BlkverifyRequest r;
226 QEMUIOVector raw_qiov;
227 void *buf;
228 ssize_t cmp_offset;
229 int ret;
230
231 buf = qemu_blockalign(bs->file->bs, qiov->size);
232 qemu_iovec_init(&raw_qiov, qiov->niov);
233 qemu_iovec_clone(&raw_qiov, qiov, buf);
234
e8b65355
SH
235 ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov,
236 flags & ~BDRV_REQ_REGISTERED_BUF, false);
44b67892
KW
237
238 cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
239 if (cmp_offset != -1) {
240 blkverify_err(&r, "contents mismatch at offset %" PRId64,
241 offset + cmp_offset);
d9d33417 242 }
d9d33417 243
44b67892
KW
244 qemu_iovec_destroy(&raw_qiov);
245 qemu_vfree(buf);
246
247 return ret;
d9d33417
SH
248}
249
44b67892 250static int coroutine_fn
e75abeda
VSO
251blkverify_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
252 QEMUIOVector *qiov, BdrvRequestFlags flags)
d9d33417 253{
44b67892
KW
254 BlkverifyRequest r;
255 return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
d9d33417
SH
256}
257
617b4b17 258static int coroutine_fn blkverify_co_flush(BlockDriverState *bs)
d9d33417
SH
259{
260 BDRVBlkverifyState *s = bs->opaque;
261
262 /* Only flush test file, the raw file is not important */
44b67892 263 return bdrv_co_flush(s->test_file->bs);
d9d33417
SH
264}
265
998a6b2f
HR
266static bool blkverify_recurse_can_replace(BlockDriverState *bs,
267 BlockDriverState *to_replace)
268{
269 BDRVBlkverifyState *s = bs->opaque;
270
271 /*
272 * blkverify quits the whole qemu process if there is a mismatch
273 * between bs->file->bs and s->test_file->bs. Therefore, we know
274 * know that both must match bs and we can recurse down to either.
275 */
276 return bdrv_recurse_can_replace(bs->file->bs, to_replace) ||
277 bdrv_recurse_can_replace(s->test_file->bs, to_replace);
278}
279
998b3a1e 280static void blkverify_refresh_filename(BlockDriverState *bs)
74b36b2e
HR
281{
282 BDRVBlkverifyState *s = bs->opaque;
283
9a4f4c31
KW
284 if (bs->file->bs->exact_filename[0]
285 && s->test_file->bs->exact_filename[0])
286 {
05cc758a
HR
287 int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
288 "blkverify:%s:%s",
289 bs->file->bs->exact_filename,
290 s->test_file->bs->exact_filename);
291 if (ret >= sizeof(bs->exact_filename)) {
292 /* An overflow makes the filename unusable, so do not report any */
293 bs->exact_filename[0] = 0;
294 }
74b36b2e
HR
295 }
296}
297
27953572
HR
298static char *blkverify_dirname(BlockDriverState *bs, Error **errp)
299{
300 /* In general, there are two BDSs with different dirnames below this one;
301 * so there is no unique dirname we could return (unless both are equal by
302 * chance). Therefore, to be consistent, just always return NULL. */
303 error_setg(errp, "Cannot generate a base directory for blkverify nodes");
304 return NULL;
305}
306
d9d33417 307static BlockDriver bdrv_blkverify = {
9d94020b
SH
308 .format_name = "blkverify",
309 .protocol_name = "blkverify",
310 .instance_size = sizeof(BDRVBlkverifyState),
311
312 .bdrv_parse_filename = blkverify_parse_filename,
313 .bdrv_file_open = blkverify_open,
314 .bdrv_close = blkverify_close,
69dca43d 315 .bdrv_child_perm = bdrv_default_perms,
9d94020b 316 .bdrv_getlength = blkverify_getlength,
74b36b2e 317 .bdrv_refresh_filename = blkverify_refresh_filename,
27953572 318 .bdrv_dirname = blkverify_dirname,
16c79092 319
44b67892
KW
320 .bdrv_co_preadv = blkverify_co_preadv,
321 .bdrv_co_pwritev = blkverify_co_pwritev,
322 .bdrv_co_flush = blkverify_co_flush,
16c79092 323
9d94020b 324 .is_filter = true,
998a6b2f 325 .bdrv_recurse_can_replace = blkverify_recurse_can_replace,
d9d33417
SH
326};
327
328static void bdrv_blkverify_init(void)
329{
330 bdrv_register(&bdrv_blkverify);
331}
332
333block_init(bdrv_blkverify_init);