]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkverify.c
Revert "audio: fix pc speaker init"
[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"
922a01a0 17#include "qemu/option.h"
d9d33417
SH
18
19typedef struct {
3e586be0 20 BdrvChild *test_file;
d9d33417
SH
21} BDRVBlkverifyState;
22
44b67892
KW
23typedef struct BlkverifyRequest {
24 Coroutine *co;
25 BlockDriverState *bs;
d9d33417
SH
26
27 /* Request metadata */
28 bool is_write;
44b67892
KW
29 uint64_t offset;
30 uint64_t bytes;
31 int flags;
d9d33417 32
44b67892
KW
33 int (*request_fn)(BdrvChild *, int64_t, unsigned int, QEMUIOVector *,
34 BdrvRequestFlags);
d9d33417 35
44b67892
KW
36 int ret; /* test image result */
37 int raw_ret; /* raw image result */
d9d33417 38
44b67892 39 unsigned int done; /* completion counter */
d9d33417 40
44b67892
KW
41 QEMUIOVector *qiov; /* user I/O vector */
42 QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */
43} BlkverifyRequest;
d9d33417 44
44b67892 45static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyRequest *r,
a77cffe7 46 const char *fmt, ...)
d9d33417
SH
47{
48 va_list ap;
49
50 va_start(ap, fmt);
44b67892
KW
51 fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ",
52 r->is_write ? "write" : "read", r->offset, r->bytes);
d9d33417
SH
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 */
16c79092
KW
60static void blkverify_parse_filename(const char *filename, QDict *options,
61 Error **errp)
d9d33417 62{
16c79092
KW
63 const char *c;
64 QString *raw_path;
65
d9d33417
SH
66
67 /* Parse the blkverify: prefix */
16c79092 68 if (!strstart(filename, "blkverify:", &filename)) {
22511ad6
HR
69 /* There was no prefix; therefore, all options have to be already
70 present in the QDict (except for the filename) */
46f5ac20 71 qdict_put_str(options, "x-image", filename);
16c79092 72 return;
d9d33417 73 }
d9d33417
SH
74
75 /* Parse the raw image filename */
76 c = strchr(filename, ':');
77 if (c == NULL) {
16c79092
KW
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 */
ba891d68 83 raw_path = qstring_from_substr(filename, 0, c - filename);
16c79092
KW
84 qdict_put(options, "x-raw", raw_path);
85
86 /* TODO Allow multi-level nesting and set file.filename here */
87 filename = c + 1;
46f5ac20 88 qdict_put_str(options, "x-image", filename);
16c79092
KW
89}
90
91static 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
015a1036
HR
109static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
110 Error **errp)
16c79092
KW
111{
112 BDRVBlkverifyState *s = bs->opaque;
113 QemuOpts *opts;
114 Error *local_err = NULL;
16c79092
KW
115 int ret;
116
87ea75d5 117 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
16c79092 118 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 119 if (local_err) {
ca288408 120 error_propagate(errp, local_err);
16c79092
KW
121 ret = -EINVAL;
122 goto fail;
123 }
124
70b6198a 125 /* Open the raw file */
9a4f4c31
KW
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;
ca288408 130 error_propagate(errp, local_err);
16c79092 131 goto fail;
d9d33417 132 }
d9d33417
SH
133
134 /* Open the test file */
3e586be0
KW
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;
ca288408 140 error_propagate(errp, local_err);
16c79092 141 goto fail;
d9d33417
SH
142 }
143
228345bf
HR
144 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
145 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
146
16c79092
KW
147 ret = 0;
148fail:
31585931 149 qemu_opts_del(opts);
16c79092 150 return ret;
d9d33417
SH
151}
152
153static void blkverify_close(BlockDriverState *bs)
154{
155 BDRVBlkverifyState *s = bs->opaque;
156
3e586be0 157 bdrv_unref_child(bs, s->test_file);
d9d33417
SH
158 s->test_file = NULL;
159}
160
d9d33417
SH
161static int64_t blkverify_getlength(BlockDriverState *bs)
162{
163 BDRVBlkverifyState *s = bs->opaque;
164
3e586be0 165 return bdrv_getlength(s->test_file->bs);
d9d33417
SH
166}
167
44b67892 168static void coroutine_fn blkverify_do_test_req(void *opaque)
d9d33417 169{
44b67892
KW
170 BlkverifyRequest *r = opaque;
171 BDRVBlkverifyState *s = r->bs->opaque;
172
173 r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
174 r->flags);
175 r->done++;
176 qemu_coroutine_enter_if_inactive(r->co);
d9d33417
SH
177}
178
44b67892 179static void coroutine_fn blkverify_do_raw_req(void *opaque)
d9d33417 180{
44b67892 181 BlkverifyRequest *r = opaque;
d9d33417 182
44b67892
KW
183 r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
184 r->flags);
185 r->done++;
186 qemu_coroutine_enter_if_inactive(r->co);
d9d33417
SH
187}
188
44b67892
KW
189static int coroutine_fn
190blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
191 uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
192 int flags, bool is_write)
d9d33417 193{
44b67892
KW
194 Coroutine *co_a, *co_b;
195
196 *r = (BlkverifyRequest) {
197 .co = qemu_coroutine_self(),
198 .bs = bs,
199 .offset = offset,
200 .bytes = bytes,
201 .qiov = qiov,
202 .raw_qiov = raw_qiov,
203 .flags = flags,
204 .is_write = is_write,
205 .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
206 };
207
208 co_a = qemu_coroutine_create(blkverify_do_test_req, r);
209 co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
210
211 qemu_coroutine_enter(co_a);
212 qemu_coroutine_enter(co_b);
213
214 while (r->done < 2) {
215 qemu_coroutine_yield();
216 }
d9d33417 217
44b67892
KW
218 if (r->ret != r->raw_ret) {
219 blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
d9d33417 220 }
44b67892
KW
221
222 return r->ret;
d9d33417
SH
223}
224
44b67892
KW
225static int coroutine_fn
226blkverify_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
227 QEMUIOVector *qiov, int flags)
d9d33417 228{
44b67892
KW
229 BlkverifyRequest r;
230 QEMUIOVector raw_qiov;
231 void *buf;
232 ssize_t cmp_offset;
233 int ret;
234
235 buf = qemu_blockalign(bs->file->bs, qiov->size);
236 qemu_iovec_init(&raw_qiov, qiov->niov);
237 qemu_iovec_clone(&raw_qiov, qiov, buf);
238
239 ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, flags,
240 false);
241
242 cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
243 if (cmp_offset != -1) {
244 blkverify_err(&r, "contents mismatch at offset %" PRId64,
245 offset + cmp_offset);
d9d33417 246 }
d9d33417 247
44b67892
KW
248 qemu_iovec_destroy(&raw_qiov);
249 qemu_vfree(buf);
250
251 return ret;
d9d33417
SH
252}
253
44b67892
KW
254static int coroutine_fn
255blkverify_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
256 QEMUIOVector *qiov, int flags)
d9d33417 257{
44b67892
KW
258 BlkverifyRequest r;
259 return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
d9d33417
SH
260}
261
44b67892 262static int blkverify_co_flush(BlockDriverState *bs)
d9d33417
SH
263{
264 BDRVBlkverifyState *s = bs->opaque;
265
266 /* Only flush test file, the raw file is not important */
44b67892 267 return bdrv_co_flush(s->test_file->bs);
d9d33417
SH
268}
269
b5042a36
BC
270static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs,
271 BlockDriverState *candidate)
272{
273 BDRVBlkverifyState *s = bs->opaque;
274
9a4f4c31 275 bool perm = bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
b5042a36
BC
276
277 if (perm) {
278 return true;
279 }
280
3e586be0 281 return bdrv_recurse_is_first_non_filter(s->test_file->bs, candidate);
b5042a36
BC
282}
283
998b3a1e 284static void blkverify_refresh_filename(BlockDriverState *bs)
74b36b2e
HR
285{
286 BDRVBlkverifyState *s = bs->opaque;
287
9a4f4c31
KW
288 if (bs->file->bs->exact_filename[0]
289 && s->test_file->bs->exact_filename[0])
290 {
05cc758a
HR
291 int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
292 "blkverify:%s:%s",
293 bs->file->bs->exact_filename,
294 s->test_file->bs->exact_filename);
295 if (ret >= sizeof(bs->exact_filename)) {
296 /* An overflow makes the filename unusable, so do not report any */
297 bs->exact_filename[0] = 0;
298 }
74b36b2e
HR
299 }
300}
301
27953572
HR
302static char *blkverify_dirname(BlockDriverState *bs, Error **errp)
303{
304 /* In general, there are two BDSs with different dirnames below this one;
305 * so there is no unique dirname we could return (unless both are equal by
306 * chance). Therefore, to be consistent, just always return NULL. */
307 error_setg(errp, "Cannot generate a base directory for blkverify nodes");
308 return NULL;
309}
310
d9d33417 311static BlockDriver bdrv_blkverify = {
9d94020b
SH
312 .format_name = "blkverify",
313 .protocol_name = "blkverify",
314 .instance_size = sizeof(BDRVBlkverifyState),
315
316 .bdrv_parse_filename = blkverify_parse_filename,
317 .bdrv_file_open = blkverify_open,
318 .bdrv_close = blkverify_close,
d7010dfb 319 .bdrv_child_perm = bdrv_filter_default_perms,
9d94020b 320 .bdrv_getlength = blkverify_getlength,
74b36b2e 321 .bdrv_refresh_filename = blkverify_refresh_filename,
27953572 322 .bdrv_dirname = blkverify_dirname,
16c79092 323
44b67892
KW
324 .bdrv_co_preadv = blkverify_co_preadv,
325 .bdrv_co_pwritev = blkverify_co_pwritev,
326 .bdrv_co_flush = blkverify_co_flush,
16c79092 327
9d94020b 328 .is_filter = true,
b5042a36 329 .bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
d9d33417
SH
330};
331
332static void bdrv_blkverify_init(void)
333{
334 bdrv_register(&bdrv_blkverify);
335}
336
337block_init(bdrv_blkverify_init);