]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkverify.c
qemu-iotests: Reduce racy output in 028
[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
10#include <stdarg.h>
1de7afc9 11#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
737e150e 12#include "block/block_int.h"
74b36b2e
HR
13#include "qapi/qmp/qdict.h"
14#include "qapi/qmp/qstring.h"
d9d33417
SH
15
16typedef struct {
3e586be0 17 BdrvChild *test_file;
d9d33417
SH
18} BDRVBlkverifyState;
19
20typedef struct BlkverifyAIOCB BlkverifyAIOCB;
21struct BlkverifyAIOCB {
7c84b1b8 22 BlockAIOCB common;
d9d33417
SH
23 QEMUBH *bh;
24
25 /* Request metadata */
26 bool is_write;
27 int64_t sector_num;
28 int nb_sectors;
29
30 int ret; /* first completed request's result */
31 unsigned int done; /* completion counter */
d9d33417
SH
32
33 QEMUIOVector *qiov; /* user I/O vector */
34 QEMUIOVector raw_qiov; /* cloned I/O vector for raw file */
35 void *buf; /* buffer for raw file I/O */
36
37 void (*verify)(BlkverifyAIOCB *acb);
38};
39
d7331bed 40static const AIOCBInfo blkverify_aiocb_info = {
d9d33417 41 .aiocb_size = sizeof(BlkverifyAIOCB),
d9d33417
SH
42};
43
a77cffe7
SW
44static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
45 const char *fmt, ...)
d9d33417
SH
46{
47 va_list ap;
48
49 va_start(ap, fmt);
687db4ed 50 fprintf(stderr, "blkverify: %s sector_num=%" PRId64 " nb_sectors=%d ",
d9d33417
SH
51 acb->is_write ? "write" : "read", acb->sector_num,
52 acb->nb_sectors);
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) */
71 qdict_put(options, "x-image", qstring_from_str(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 */
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(options, "x-image", qstring_from_str(filename));
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
16c79092
KW
144 ret = 0;
145fail:
7e39d3a2
KW
146 if (ret < 0) {
147 bdrv_unref_child(bs, bs->file);
148 }
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
d9d33417
SH
168static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
169 int64_t sector_num, QEMUIOVector *qiov,
170 int nb_sectors,
097310b5 171 BlockCompletionFunc *cb,
d9d33417
SH
172 void *opaque)
173{
d7331bed 174 BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
d9d33417
SH
175
176 acb->bh = NULL;
177 acb->is_write = is_write;
178 acb->sector_num = sector_num;
179 acb->nb_sectors = nb_sectors;
180 acb->ret = -EINPROGRESS;
181 acb->done = 0;
182 acb->qiov = qiov;
183 acb->buf = NULL;
184 acb->verify = NULL;
d9d33417
SH
185 return acb;
186}
187
188static void blkverify_aio_bh(void *opaque)
189{
190 BlkverifyAIOCB *acb = opaque;
191
192 qemu_bh_delete(acb->bh);
193 if (acb->buf) {
194 qemu_iovec_destroy(&acb->raw_qiov);
195 qemu_vfree(acb->buf);
196 }
197 acb->common.cb(acb->common.opaque, acb->ret);
8007429a 198 qemu_aio_unref(acb);
d9d33417
SH
199}
200
201static void blkverify_aio_cb(void *opaque, int ret)
202{
203 BlkverifyAIOCB *acb = opaque;
204
205 switch (++acb->done) {
206 case 1:
207 acb->ret = ret;
208 break;
209
210 case 2:
211 if (acb->ret != ret) {
212 blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret);
213 }
214
215 if (acb->verify) {
216 acb->verify(acb);
217 }
218
9d94020b
SH
219 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
220 blkverify_aio_bh, acb);
d9d33417
SH
221 qemu_bh_schedule(acb->bh);
222 break;
223 }
224}
225
226static void blkverify_verify_readv(BlkverifyAIOCB *acb)
227{
f70d7f7e 228 ssize_t offset = qemu_iovec_compare(acb->qiov, &acb->raw_qiov);
d9d33417 229 if (offset != -1) {
a3133586
BS
230 blkverify_err(acb, "contents mismatch in sector %" PRId64,
231 acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
d9d33417
SH
232 }
233}
234
7c84b1b8 235static BlockAIOCB *blkverify_aio_readv(BlockDriverState *bs,
d9d33417 236 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 237 BlockCompletionFunc *cb, void *opaque)
d9d33417
SH
238{
239 BDRVBlkverifyState *s = bs->opaque;
240 BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
241 nb_sectors, cb, opaque);
242
243 acb->verify = blkverify_verify_readv;
9a4f4c31 244 acb->buf = qemu_blockalign(bs->file->bs, qiov->size);
d9d33417 245 qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
f70d7f7e 246 qemu_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
d9d33417 247
3e586be0 248 bdrv_aio_readv(s->test_file->bs, sector_num, qiov, nb_sectors,
ad54ae80 249 blkverify_aio_cb, acb);
9a4f4c31 250 bdrv_aio_readv(bs->file->bs, sector_num, &acb->raw_qiov, nb_sectors,
ad54ae80 251 blkverify_aio_cb, acb);
d9d33417
SH
252 return &acb->common;
253}
254
7c84b1b8 255static BlockAIOCB *blkverify_aio_writev(BlockDriverState *bs,
d9d33417 256 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 257 BlockCompletionFunc *cb, void *opaque)
d9d33417
SH
258{
259 BDRVBlkverifyState *s = bs->opaque;
260 BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
261 nb_sectors, cb, opaque);
262
3e586be0 263 bdrv_aio_writev(s->test_file->bs, sector_num, qiov, nb_sectors,
ad54ae80 264 blkverify_aio_cb, acb);
9a4f4c31 265 bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors,
ad54ae80 266 blkverify_aio_cb, acb);
d9d33417
SH
267 return &acb->common;
268}
269
7c84b1b8 270static BlockAIOCB *blkverify_aio_flush(BlockDriverState *bs,
097310b5 271 BlockCompletionFunc *cb,
7c84b1b8 272 void *opaque)
d9d33417
SH
273{
274 BDRVBlkverifyState *s = bs->opaque;
275
276 /* Only flush test file, the raw file is not important */
3e586be0 277 return bdrv_aio_flush(s->test_file->bs, cb, opaque);
d9d33417
SH
278}
279
b5042a36
BC
280static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs,
281 BlockDriverState *candidate)
282{
283 BDRVBlkverifyState *s = bs->opaque;
284
9a4f4c31 285 bool perm = bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
b5042a36
BC
286
287 if (perm) {
288 return true;
289 }
290
3e586be0 291 return bdrv_recurse_is_first_non_filter(s->test_file->bs, candidate);
b5042a36
BC
292}
293
9d94020b
SH
294/* Propagate AioContext changes to ->test_file */
295static void blkverify_detach_aio_context(BlockDriverState *bs)
296{
297 BDRVBlkverifyState *s = bs->opaque;
298
3e586be0 299 bdrv_detach_aio_context(s->test_file->bs);
9d94020b
SH
300}
301
302static void blkverify_attach_aio_context(BlockDriverState *bs,
303 AioContext *new_context)
304{
305 BDRVBlkverifyState *s = bs->opaque;
306
3e586be0 307 bdrv_attach_aio_context(s->test_file->bs, new_context);
9d94020b
SH
308}
309
4cdd01d3 310static void blkverify_refresh_filename(BlockDriverState *bs, QDict *options)
74b36b2e
HR
311{
312 BDRVBlkverifyState *s = bs->opaque;
313
9a4f4c31 314 /* bs->file->bs has already been refreshed */
3e586be0 315 bdrv_refresh_filename(s->test_file->bs);
74b36b2e 316
9a4f4c31
KW
317 if (bs->file->bs->full_open_options
318 && s->test_file->bs->full_open_options)
319 {
74b36b2e
HR
320 QDict *opts = qdict_new();
321 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkverify")));
322
9a4f4c31
KW
323 QINCREF(bs->file->bs->full_open_options);
324 qdict_put_obj(opts, "raw", QOBJECT(bs->file->bs->full_open_options));
3e586be0
KW
325 QINCREF(s->test_file->bs->full_open_options);
326 qdict_put_obj(opts, "test",
327 QOBJECT(s->test_file->bs->full_open_options));
74b36b2e
HR
328
329 bs->full_open_options = opts;
330 }
331
9a4f4c31
KW
332 if (bs->file->bs->exact_filename[0]
333 && s->test_file->bs->exact_filename[0])
334 {
74b36b2e
HR
335 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
336 "blkverify:%s:%s",
9a4f4c31
KW
337 bs->file->bs->exact_filename,
338 s->test_file->bs->exact_filename);
74b36b2e
HR
339 }
340}
341
d9d33417 342static BlockDriver bdrv_blkverify = {
9d94020b
SH
343 .format_name = "blkverify",
344 .protocol_name = "blkverify",
345 .instance_size = sizeof(BDRVBlkverifyState),
346
347 .bdrv_parse_filename = blkverify_parse_filename,
348 .bdrv_file_open = blkverify_open,
349 .bdrv_close = blkverify_close,
350 .bdrv_getlength = blkverify_getlength,
74b36b2e 351 .bdrv_refresh_filename = blkverify_refresh_filename,
16c79092 352
9d94020b
SH
353 .bdrv_aio_readv = blkverify_aio_readv,
354 .bdrv_aio_writev = blkverify_aio_writev,
355 .bdrv_aio_flush = blkverify_aio_flush,
16c79092 356
9d94020b
SH
357 .bdrv_attach_aio_context = blkverify_attach_aio_context,
358 .bdrv_detach_aio_context = blkverify_detach_aio_context,
f6186f49 359
9d94020b 360 .is_filter = true,
b5042a36 361 .bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
d9d33417
SH
362};
363
364static void bdrv_blkverify_init(void)
365{
366 bdrv_register(&bdrv_blkverify);
367}
368
369block_init(bdrv_blkverify_init);