]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkverify.c
blkdebug: Implement bdrv_co_preadv/pwritev/flush
[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
22typedef struct BlkverifyAIOCB BlkverifyAIOCB;
23struct BlkverifyAIOCB {
7c84b1b8 24 BlockAIOCB common;
d9d33417
SH
25
26 /* Request metadata */
27 bool is_write;
28 int64_t sector_num;
29 int nb_sectors;
30
31 int ret; /* first completed request's result */
32 unsigned int done; /* completion counter */
d9d33417
SH
33
34 QEMUIOVector *qiov; /* user I/O vector */
35 QEMUIOVector raw_qiov; /* cloned I/O vector for raw file */
36 void *buf; /* buffer for raw file I/O */
37
38 void (*verify)(BlkverifyAIOCB *acb);
39};
40
d7331bed 41static const AIOCBInfo blkverify_aiocb_info = {
d9d33417 42 .aiocb_size = sizeof(BlkverifyAIOCB),
d9d33417
SH
43};
44
a77cffe7
SW
45static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
46 const char *fmt, ...)
d9d33417
SH
47{
48 va_list ap;
49
50 va_start(ap, fmt);
687db4ed 51 fprintf(stderr, "blkverify: %s sector_num=%" PRId64 " nb_sectors=%d ",
d9d33417
SH
52 acb->is_write ? "write" : "read", acb->sector_num,
53 acb->nb_sectors);
54 vfprintf(stderr, fmt, ap);
55 fprintf(stderr, "\n");
56 va_end(ap);
57 exit(1);
58}
59
60/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
16c79092
KW
61static void blkverify_parse_filename(const char *filename, QDict *options,
62 Error **errp)
d9d33417 63{
16c79092
KW
64 const char *c;
65 QString *raw_path;
66
d9d33417
SH
67
68 /* Parse the blkverify: prefix */
16c79092 69 if (!strstart(filename, "blkverify:", &filename)) {
22511ad6
HR
70 /* There was no prefix; therefore, all options have to be already
71 present in the QDict (except for the filename) */
72 qdict_put(options, "x-image", qstring_from_str(filename));
16c79092 73 return;
d9d33417 74 }
d9d33417
SH
75
76 /* Parse the raw image filename */
77 c = strchr(filename, ':');
78 if (c == NULL) {
16c79092
KW
79 error_setg(errp, "blkverify requires raw copy and original image path");
80 return;
81 }
82
83 /* TODO Implement option pass-through and set raw.filename here */
84 raw_path = qstring_from_substr(filename, 0, c - filename - 1);
85 qdict_put(options, "x-raw", raw_path);
86
87 /* TODO Allow multi-level nesting and set file.filename here */
88 filename = c + 1;
89 qdict_put(options, "x-image", qstring_from_str(filename));
90}
91
92static QemuOptsList runtime_opts = {
93 .name = "blkverify",
94 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
95 .desc = {
96 {
97 .name = "x-raw",
98 .type = QEMU_OPT_STRING,
99 .help = "[internal use only, will be removed]",
100 },
101 {
102 .name = "x-image",
103 .type = QEMU_OPT_STRING,
104 .help = "[internal use only, will be removed]",
105 },
106 { /* end of list */ }
107 },
108};
109
015a1036
HR
110static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
111 Error **errp)
16c79092
KW
112{
113 BDRVBlkverifyState *s = bs->opaque;
114 QemuOpts *opts;
115 Error *local_err = NULL;
16c79092
KW
116 int ret;
117
87ea75d5 118 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
16c79092 119 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 120 if (local_err) {
ca288408 121 error_propagate(errp, local_err);
16c79092
KW
122 ret = -EINVAL;
123 goto fail;
124 }
125
70b6198a 126 /* Open the raw file */
9a4f4c31
KW
127 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw",
128 bs, &child_file, false, &local_err);
129 if (local_err) {
130 ret = -EINVAL;
ca288408 131 error_propagate(errp, local_err);
16c79092 132 goto fail;
d9d33417 133 }
d9d33417
SH
134
135 /* Open the test file */
3e586be0
KW
136 s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
137 "test", bs, &child_format, false,
138 &local_err);
139 if (local_err) {
140 ret = -EINVAL;
ca288408 141 error_propagate(errp, local_err);
16c79092 142 goto fail;
d9d33417
SH
143 }
144
16c79092
KW
145 ret = 0;
146fail:
7e39d3a2
KW
147 if (ret < 0) {
148 bdrv_unref_child(bs, bs->file);
149 }
31585931 150 qemu_opts_del(opts);
16c79092 151 return ret;
d9d33417
SH
152}
153
154static void blkverify_close(BlockDriverState *bs)
155{
156 BDRVBlkverifyState *s = bs->opaque;
157
3e586be0 158 bdrv_unref_child(bs, s->test_file);
d9d33417
SH
159 s->test_file = NULL;
160}
161
d9d33417
SH
162static int64_t blkverify_getlength(BlockDriverState *bs)
163{
164 BDRVBlkverifyState *s = bs->opaque;
165
3e586be0 166 return bdrv_getlength(s->test_file->bs);
d9d33417
SH
167}
168
d9d33417
SH
169static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
170 int64_t sector_num, QEMUIOVector *qiov,
171 int nb_sectors,
097310b5 172 BlockCompletionFunc *cb,
d9d33417
SH
173 void *opaque)
174{
d7331bed 175 BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
d9d33417 176
d9d33417
SH
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
d9d33417
SH
192 if (acb->buf) {
193 qemu_iovec_destroy(&acb->raw_qiov);
194 qemu_vfree(acb->buf);
195 }
196 acb->common.cb(acb->common.opaque, acb->ret);
8007429a 197 qemu_aio_unref(acb);
d9d33417
SH
198}
199
200static void blkverify_aio_cb(void *opaque, int ret)
201{
202 BlkverifyAIOCB *acb = opaque;
203
204 switch (++acb->done) {
205 case 1:
206 acb->ret = ret;
207 break;
208
209 case 2:
210 if (acb->ret != ret) {
211 blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret);
212 }
213
214 if (acb->verify) {
215 acb->verify(acb);
216 }
217
fffb6e12
PB
218 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
219 blkverify_aio_bh, acb);
d9d33417
SH
220 break;
221 }
222}
223
224static void blkverify_verify_readv(BlkverifyAIOCB *acb)
225{
f70d7f7e 226 ssize_t offset = qemu_iovec_compare(acb->qiov, &acb->raw_qiov);
d9d33417 227 if (offset != -1) {
a3133586
BS
228 blkverify_err(acb, "contents mismatch in sector %" PRId64,
229 acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
d9d33417
SH
230 }
231}
232
7c84b1b8 233static BlockAIOCB *blkverify_aio_readv(BlockDriverState *bs,
d9d33417 234 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 235 BlockCompletionFunc *cb, void *opaque)
d9d33417
SH
236{
237 BDRVBlkverifyState *s = bs->opaque;
238 BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
239 nb_sectors, cb, opaque);
240
241 acb->verify = blkverify_verify_readv;
9a4f4c31 242 acb->buf = qemu_blockalign(bs->file->bs, qiov->size);
d9d33417 243 qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
f70d7f7e 244 qemu_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
d9d33417 245
ebb7af21 246 bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors,
ad54ae80 247 blkverify_aio_cb, acb);
ebb7af21 248 bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors,
ad54ae80 249 blkverify_aio_cb, acb);
d9d33417
SH
250 return &acb->common;
251}
252
7c84b1b8 253static BlockAIOCB *blkverify_aio_writev(BlockDriverState *bs,
d9d33417 254 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 255 BlockCompletionFunc *cb, void *opaque)
d9d33417
SH
256{
257 BDRVBlkverifyState *s = bs->opaque;
258 BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
259 nb_sectors, cb, opaque);
260
0d1049c7 261 bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors,
ad54ae80 262 blkverify_aio_cb, acb);
0d1049c7 263 bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors,
ad54ae80 264 blkverify_aio_cb, acb);
d9d33417
SH
265 return &acb->common;
266}
267
7c84b1b8 268static BlockAIOCB *blkverify_aio_flush(BlockDriverState *bs,
097310b5 269 BlockCompletionFunc *cb,
7c84b1b8 270 void *opaque)
d9d33417
SH
271{
272 BDRVBlkverifyState *s = bs->opaque;
273
274 /* Only flush test file, the raw file is not important */
3e586be0 275 return bdrv_aio_flush(s->test_file->bs, cb, opaque);
d9d33417
SH
276}
277
b5042a36
BC
278static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs,
279 BlockDriverState *candidate)
280{
281 BDRVBlkverifyState *s = bs->opaque;
282
9a4f4c31 283 bool perm = bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
b5042a36
BC
284
285 if (perm) {
286 return true;
287 }
288
3e586be0 289 return bdrv_recurse_is_first_non_filter(s->test_file->bs, candidate);
b5042a36
BC
290}
291
4cdd01d3 292static void blkverify_refresh_filename(BlockDriverState *bs, QDict *options)
74b36b2e
HR
293{
294 BDRVBlkverifyState *s = bs->opaque;
295
9a4f4c31 296 /* bs->file->bs has already been refreshed */
3e586be0 297 bdrv_refresh_filename(s->test_file->bs);
74b36b2e 298
9a4f4c31
KW
299 if (bs->file->bs->full_open_options
300 && s->test_file->bs->full_open_options)
301 {
74b36b2e
HR
302 QDict *opts = qdict_new();
303 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkverify")));
304
9a4f4c31
KW
305 QINCREF(bs->file->bs->full_open_options);
306 qdict_put_obj(opts, "raw", QOBJECT(bs->file->bs->full_open_options));
3e586be0
KW
307 QINCREF(s->test_file->bs->full_open_options);
308 qdict_put_obj(opts, "test",
309 QOBJECT(s->test_file->bs->full_open_options));
74b36b2e
HR
310
311 bs->full_open_options = opts;
312 }
313
9a4f4c31
KW
314 if (bs->file->bs->exact_filename[0]
315 && s->test_file->bs->exact_filename[0])
316 {
74b36b2e
HR
317 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
318 "blkverify:%s:%s",
9a4f4c31
KW
319 bs->file->bs->exact_filename,
320 s->test_file->bs->exact_filename);
74b36b2e
HR
321 }
322}
323
d9d33417 324static BlockDriver bdrv_blkverify = {
9d94020b
SH
325 .format_name = "blkverify",
326 .protocol_name = "blkverify",
327 .instance_size = sizeof(BDRVBlkverifyState),
328
329 .bdrv_parse_filename = blkverify_parse_filename,
330 .bdrv_file_open = blkverify_open,
331 .bdrv_close = blkverify_close,
332 .bdrv_getlength = blkverify_getlength,
74b36b2e 333 .bdrv_refresh_filename = blkverify_refresh_filename,
16c79092 334
9d94020b
SH
335 .bdrv_aio_readv = blkverify_aio_readv,
336 .bdrv_aio_writev = blkverify_aio_writev,
337 .bdrv_aio_flush = blkverify_aio_flush,
16c79092 338
9d94020b 339 .is_filter = true,
b5042a36 340 .bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
d9d33417
SH
341};
342
343static void bdrv_blkverify_init(void)
344{
345 bdrv_register(&bdrv_blkverify);
346}
347
348block_init(bdrv_blkverify_init);