1
2
3
4
5
6
7
8
9
10#include <stdarg.h>
11#include "qemu/sockets.h"
12#include "block/block_int.h"
13#include "qapi/qmp/qdict.h"
14#include "qapi/qmp/qstring.h"
15
16typedef struct {
17 BlockDriverState *test_file;
18} BDRVBlkverifyState;
19
20typedef struct BlkverifyAIOCB BlkverifyAIOCB;
21struct BlkverifyAIOCB {
22 BlockAIOCB common;
23 QEMUBH *bh;
24
25
26 bool is_write;
27 int64_t sector_num;
28 int nb_sectors;
29
30 int ret;
31 unsigned int done;
32
33 QEMUIOVector *qiov;
34 QEMUIOVector raw_qiov;
35 void *buf;
36
37 void (*verify)(BlkverifyAIOCB *acb);
38};
39
40static const AIOCBInfo blkverify_aiocb_info = {
41 .aiocb_size = sizeof(BlkverifyAIOCB),
42};
43
44static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
45 const char *fmt, ...)
46{
47 va_list ap;
48
49 va_start(ap, fmt);
50 fprintf(stderr, "blkverify: %s sector_num=%" PRId64 " nb_sectors=%d ",
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
60static void blkverify_parse_filename(const char *filename, QDict *options,
61 Error **errp)
62{
63 const char *c;
64 QString *raw_path;
65
66
67
68 if (!strstart(filename, "blkverify:", &filename)) {
69
70
71 qdict_put(options, "x-image", qstring_from_str(filename));
72 return;
73 }
74
75
76 c = strchr(filename, ':');
77 if (c == NULL) {
78 error_setg(errp, "blkverify requires raw copy and original image path");
79 return;
80 }
81
82
83 raw_path = qstring_from_substr(filename, 0, c - filename - 1);
84 qdict_put(options, "x-raw", raw_path);
85
86
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 { }
106 },
107};
108
109static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
110 Error **errp)
111{
112 BDRVBlkverifyState *s = bs->opaque;
113 QemuOpts *opts;
114 Error *local_err = NULL;
115 int ret;
116
117 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
118 qemu_opts_absorb_qdict(opts, options, &local_err);
119 if (local_err) {
120 error_propagate(errp, local_err);
121 ret = -EINVAL;
122 goto fail;
123 }
124
125
126 assert(bs->file == NULL);
127 ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-raw"), options,
128 "raw", flags | BDRV_O_PROTOCOL, false, &local_err);
129 if (ret < 0) {
130 error_propagate(errp, local_err);
131 goto fail;
132 }
133
134
135 assert(s->test_file == NULL);
136 ret = bdrv_open_image(&s->test_file, qemu_opt_get(opts, "x-image"), options,
137 "test", flags, false, &local_err);
138 if (ret < 0) {
139 error_propagate(errp, local_err);
140 s->test_file = NULL;
141 goto fail;
142 }
143
144 ret = 0;
145fail:
146 qemu_opts_del(opts);
147 return ret;
148}
149
150static void blkverify_close(BlockDriverState *bs)
151{
152 BDRVBlkverifyState *s = bs->opaque;
153
154 bdrv_unref(s->test_file);
155 s->test_file = NULL;
156}
157
158static int64_t blkverify_getlength(BlockDriverState *bs)
159{
160 BDRVBlkverifyState *s = bs->opaque;
161
162 return bdrv_getlength(s->test_file);
163}
164
165static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
166 int64_t sector_num, QEMUIOVector *qiov,
167 int nb_sectors,
168 BlockCompletionFunc *cb,
169 void *opaque)
170{
171 BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
172
173 acb->bh = NULL;
174 acb->is_write = is_write;
175 acb->sector_num = sector_num;
176 acb->nb_sectors = nb_sectors;
177 acb->ret = -EINPROGRESS;
178 acb->done = 0;
179 acb->qiov = qiov;
180 acb->buf = NULL;
181 acb->verify = NULL;
182 return acb;
183}
184
185static void blkverify_aio_bh(void *opaque)
186{
187 BlkverifyAIOCB *acb = opaque;
188
189 qemu_bh_delete(acb->bh);
190 if (acb->buf) {
191 qemu_iovec_destroy(&acb->raw_qiov);
192 qemu_vfree(acb->buf);
193 }
194 acb->common.cb(acb->common.opaque, acb->ret);
195 qemu_aio_unref(acb);
196}
197
198static void blkverify_aio_cb(void *opaque, int ret)
199{
200 BlkverifyAIOCB *acb = opaque;
201
202 switch (++acb->done) {
203 case 1:
204 acb->ret = ret;
205 break;
206
207 case 2:
208 if (acb->ret != ret) {
209 blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret);
210 }
211
212 if (acb->verify) {
213 acb->verify(acb);
214 }
215
216 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
217 blkverify_aio_bh, acb);
218 qemu_bh_schedule(acb->bh);
219 break;
220 }
221}
222
223static void blkverify_verify_readv(BlkverifyAIOCB *acb)
224{
225 ssize_t offset = qemu_iovec_compare(acb->qiov, &acb->raw_qiov);
226 if (offset != -1) {
227 blkverify_err(acb, "contents mismatch in sector %" PRId64,
228 acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
229 }
230}
231
232static BlockAIOCB *blkverify_aio_readv(BlockDriverState *bs,
233 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
234 BlockCompletionFunc *cb, void *opaque)
235{
236 BDRVBlkverifyState *s = bs->opaque;
237 BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
238 nb_sectors, cb, opaque);
239
240 acb->verify = blkverify_verify_readv;
241 acb->buf = qemu_blockalign(bs->file, qiov->size);
242 qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
243 qemu_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
244
245 bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors,
246 blkverify_aio_cb, acb);
247 bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors,
248 blkverify_aio_cb, acb);
249 return &acb->common;
250}
251
252static BlockAIOCB *blkverify_aio_writev(BlockDriverState *bs,
253 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
254 BlockCompletionFunc *cb, void *opaque)
255{
256 BDRVBlkverifyState *s = bs->opaque;
257 BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
258 nb_sectors, cb, opaque);
259
260 bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors,
261 blkverify_aio_cb, acb);
262 bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors,
263 blkverify_aio_cb, acb);
264 return &acb->common;
265}
266
267static BlockAIOCB *blkverify_aio_flush(BlockDriverState *bs,
268 BlockCompletionFunc *cb,
269 void *opaque)
270{
271 BDRVBlkverifyState *s = bs->opaque;
272
273
274 return bdrv_aio_flush(s->test_file, cb, opaque);
275}
276
277static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs,
278 BlockDriverState *candidate)
279{
280 BDRVBlkverifyState *s = bs->opaque;
281
282 bool perm = bdrv_recurse_is_first_non_filter(bs->file, candidate);
283
284 if (perm) {
285 return true;
286 }
287
288 return bdrv_recurse_is_first_non_filter(s->test_file, candidate);
289}
290
291
292static void blkverify_detach_aio_context(BlockDriverState *bs)
293{
294 BDRVBlkverifyState *s = bs->opaque;
295
296 bdrv_detach_aio_context(s->test_file);
297}
298
299static void blkverify_attach_aio_context(BlockDriverState *bs,
300 AioContext *new_context)
301{
302 BDRVBlkverifyState *s = bs->opaque;
303
304 bdrv_attach_aio_context(s->test_file, new_context);
305}
306
307static void blkverify_refresh_filename(BlockDriverState *bs)
308{
309 BDRVBlkverifyState *s = bs->opaque;
310
311
312 bdrv_refresh_filename(s->test_file);
313
314 if (bs->file->full_open_options && s->test_file->full_open_options) {
315 QDict *opts = qdict_new();
316 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkverify")));
317
318 QINCREF(bs->file->full_open_options);
319 qdict_put_obj(opts, "raw", QOBJECT(bs->file->full_open_options));
320 QINCREF(s->test_file->full_open_options);
321 qdict_put_obj(opts, "test", QOBJECT(s->test_file->full_open_options));
322
323 bs->full_open_options = opts;
324 }
325
326 if (bs->file->exact_filename[0] && s->test_file->exact_filename[0]) {
327 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
328 "blkverify:%s:%s",
329 bs->file->exact_filename, s->test_file->exact_filename);
330 }
331}
332
333static BlockDriver bdrv_blkverify = {
334 .format_name = "blkverify",
335 .protocol_name = "blkverify",
336 .instance_size = sizeof(BDRVBlkverifyState),
337
338 .bdrv_parse_filename = blkverify_parse_filename,
339 .bdrv_file_open = blkverify_open,
340 .bdrv_close = blkverify_close,
341 .bdrv_getlength = blkverify_getlength,
342 .bdrv_refresh_filename = blkverify_refresh_filename,
343
344 .bdrv_aio_readv = blkverify_aio_readv,
345 .bdrv_aio_writev = blkverify_aio_writev,
346 .bdrv_aio_flush = blkverify_aio_flush,
347
348 .bdrv_attach_aio_context = blkverify_attach_aio_context,
349 .bdrv_detach_aio_context = blkverify_detach_aio_context,
350
351 .is_filter = true,
352 .bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
353};
354
355static void bdrv_blkverify_init(void)
356{
357 bdrv_register(&bdrv_blkverify);
358}
359
360block_init(bdrv_blkverify_init);
361