xref: /openbmc/qemu/block/blkverify.c (revision 181103cd)
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>
11 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */
12 #include "block/block_int.h"
13 
14 typedef struct {
15     BlockDriverState *test_file;
16 } BDRVBlkverifyState;
17 
18 typedef struct BlkverifyAIOCB BlkverifyAIOCB;
19 struct BlkverifyAIOCB {
20     BlockDriverAIOCB common;
21     QEMUBH *bh;
22 
23     /* Request metadata */
24     bool is_write;
25     int64_t sector_num;
26     int nb_sectors;
27 
28     int ret;                    /* first completed request's result */
29     unsigned int done;          /* completion counter */
30     bool *finished;             /* completion signal for cancel */
31 
32     QEMUIOVector *qiov;         /* user I/O vector */
33     QEMUIOVector raw_qiov;      /* cloned I/O vector for raw file */
34     void *buf;                  /* buffer for raw file I/O */
35 
36     void (*verify)(BlkverifyAIOCB *acb);
37 };
38 
39 static void blkverify_aio_cancel(BlockDriverAIOCB *blockacb)
40 {
41     BlkverifyAIOCB *acb = (BlkverifyAIOCB *)blockacb;
42     bool finished = false;
43 
44     /* Wait until request completes, invokes its callback, and frees itself */
45     acb->finished = &finished;
46     while (!finished) {
47         qemu_aio_wait();
48     }
49 }
50 
51 static const AIOCBInfo blkverify_aiocb_info = {
52     .aiocb_size         = sizeof(BlkverifyAIOCB),
53     .cancel             = blkverify_aio_cancel,
54 };
55 
56 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
57                                              const char *fmt, ...)
58 {
59     va_list ap;
60 
61     va_start(ap, fmt);
62     fprintf(stderr, "blkverify: %s sector_num=%" PRId64 " nb_sectors=%d ",
63             acb->is_write ? "write" : "read", acb->sector_num,
64             acb->nb_sectors);
65     vfprintf(stderr, fmt, ap);
66     fprintf(stderr, "\n");
67     va_end(ap);
68     exit(1);
69 }
70 
71 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
72 static void blkverify_parse_filename(const char *filename, QDict *options,
73                                      Error **errp)
74 {
75     const char *c;
76     QString *raw_path;
77 
78 
79     /* Parse the blkverify: prefix */
80     if (!strstart(filename, "blkverify:", &filename)) {
81         error_setg(errp, "File name string must start with 'blkverify:'");
82         return;
83     }
84 
85     /* Parse the raw image filename */
86     c = strchr(filename, ':');
87     if (c == NULL) {
88         error_setg(errp, "blkverify requires raw copy and original image path");
89         return;
90     }
91 
92     /* TODO Implement option pass-through and set raw.filename here */
93     raw_path = qstring_from_substr(filename, 0, c - filename - 1);
94     qdict_put(options, "x-raw", raw_path);
95 
96     /* TODO Allow multi-level nesting and set file.filename here */
97     filename = c + 1;
98     qdict_put(options, "x-image", qstring_from_str(filename));
99 }
100 
101 static QemuOptsList runtime_opts = {
102     .name = "blkverify",
103     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
104     .desc = {
105         {
106             .name = "x-raw",
107             .type = QEMU_OPT_STRING,
108             .help = "[internal use only, will be removed]",
109         },
110         {
111             .name = "x-image",
112             .type = QEMU_OPT_STRING,
113             .help = "[internal use only, will be removed]",
114         },
115         { /* end of list */ }
116     },
117 };
118 
119 static int blkverify_open(BlockDriverState *bs, QDict *options, int flags)
120 {
121     BDRVBlkverifyState *s = bs->opaque;
122     QemuOpts *opts;
123     Error *local_err = NULL;
124     const char *filename, *raw;
125     int ret;
126 
127     opts = qemu_opts_create_nofail(&runtime_opts);
128     qemu_opts_absorb_qdict(opts, options, &local_err);
129     if (error_is_set(&local_err)) {
130         qerror_report_err(local_err);
131         error_free(local_err);
132         ret = -EINVAL;
133         goto fail;
134     }
135 
136     /* Parse the raw image filename */
137     raw = qemu_opt_get(opts, "x-raw");
138     if (raw == NULL) {
139         ret = -EINVAL;
140         goto fail;
141     }
142 
143     ret = bdrv_file_open(&bs->file, raw, NULL, flags);
144     if (ret < 0) {
145         goto fail;
146     }
147 
148     /* Open the test file */
149     filename = qemu_opt_get(opts, "x-image");
150     if (filename == NULL) {
151         ret = -EINVAL;
152         goto fail;
153     }
154 
155     s->test_file = bdrv_new("");
156     ret = bdrv_open(s->test_file, filename, NULL, flags, NULL);
157     if (ret < 0) {
158         bdrv_delete(s->test_file);
159         s->test_file = NULL;
160         goto fail;
161     }
162 
163     ret = 0;
164 fail:
165     return ret;
166 }
167 
168 static void blkverify_close(BlockDriverState *bs)
169 {
170     BDRVBlkverifyState *s = bs->opaque;
171 
172     bdrv_delete(s->test_file);
173     s->test_file = NULL;
174 }
175 
176 static int64_t blkverify_getlength(BlockDriverState *bs)
177 {
178     BDRVBlkverifyState *s = bs->opaque;
179 
180     return bdrv_getlength(s->test_file);
181 }
182 
183 /**
184  * Check that I/O vector contents are identical
185  *
186  * @a:          I/O vector
187  * @b:          I/O vector
188  * @ret:        Offset to first mismatching byte or -1 if match
189  */
190 static ssize_t blkverify_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
191 {
192     int i;
193     ssize_t offset = 0;
194 
195     assert(a->niov == b->niov);
196     for (i = 0; i < a->niov; i++) {
197         size_t len = 0;
198         uint8_t *p = (uint8_t *)a->iov[i].iov_base;
199         uint8_t *q = (uint8_t *)b->iov[i].iov_base;
200 
201         assert(a->iov[i].iov_len == b->iov[i].iov_len);
202         while (len < a->iov[i].iov_len && *p++ == *q++) {
203             len++;
204         }
205 
206         offset += len;
207 
208         if (len != a->iov[i].iov_len) {
209             return offset;
210         }
211     }
212     return -1;
213 }
214 
215 typedef struct {
216     int src_index;
217     struct iovec *src_iov;
218     void *dest_base;
219 } IOVectorSortElem;
220 
221 static int sortelem_cmp_src_base(const void *a, const void *b)
222 {
223     const IOVectorSortElem *elem_a = a;
224     const IOVectorSortElem *elem_b = b;
225 
226     /* Don't overflow */
227     if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
228         return -1;
229     } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
230         return 1;
231     } else {
232         return 0;
233     }
234 }
235 
236 static int sortelem_cmp_src_index(const void *a, const void *b)
237 {
238     const IOVectorSortElem *elem_a = a;
239     const IOVectorSortElem *elem_b = b;
240 
241     return elem_a->src_index - elem_b->src_index;
242 }
243 
244 /**
245  * Copy contents of I/O vector
246  *
247  * The relative relationships of overlapping iovecs are preserved.  This is
248  * necessary to ensure identical semantics in the cloned I/O vector.
249  */
250 static void blkverify_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src,
251                                   void *buf)
252 {
253     IOVectorSortElem sortelems[src->niov];
254     void *last_end;
255     int i;
256 
257     /* Sort by source iovecs by base address */
258     for (i = 0; i < src->niov; i++) {
259         sortelems[i].src_index = i;
260         sortelems[i].src_iov = &src->iov[i];
261     }
262     qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
263 
264     /* Allocate buffer space taking into account overlapping iovecs */
265     last_end = NULL;
266     for (i = 0; i < src->niov; i++) {
267         struct iovec *cur = sortelems[i].src_iov;
268         ptrdiff_t rewind = 0;
269 
270         /* Detect overlap */
271         if (last_end && last_end > cur->iov_base) {
272             rewind = last_end - cur->iov_base;
273         }
274 
275         sortelems[i].dest_base = buf - rewind;
276         buf += cur->iov_len - MIN(rewind, cur->iov_len);
277         last_end = MAX(cur->iov_base + cur->iov_len, last_end);
278     }
279 
280     /* Sort by source iovec index and build destination iovec */
281     qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
282     for (i = 0; i < src->niov; i++) {
283         qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
284     }
285 }
286 
287 static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
288                                          int64_t sector_num, QEMUIOVector *qiov,
289                                          int nb_sectors,
290                                          BlockDriverCompletionFunc *cb,
291                                          void *opaque)
292 {
293     BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
294 
295     acb->bh = NULL;
296     acb->is_write = is_write;
297     acb->sector_num = sector_num;
298     acb->nb_sectors = nb_sectors;
299     acb->ret = -EINPROGRESS;
300     acb->done = 0;
301     acb->qiov = qiov;
302     acb->buf = NULL;
303     acb->verify = NULL;
304     acb->finished = NULL;
305     return acb;
306 }
307 
308 static void blkverify_aio_bh(void *opaque)
309 {
310     BlkverifyAIOCB *acb = opaque;
311 
312     qemu_bh_delete(acb->bh);
313     if (acb->buf) {
314         qemu_iovec_destroy(&acb->raw_qiov);
315         qemu_vfree(acb->buf);
316     }
317     acb->common.cb(acb->common.opaque, acb->ret);
318     if (acb->finished) {
319         *acb->finished = true;
320     }
321     qemu_aio_release(acb);
322 }
323 
324 static void blkverify_aio_cb(void *opaque, int ret)
325 {
326     BlkverifyAIOCB *acb = opaque;
327 
328     switch (++acb->done) {
329     case 1:
330         acb->ret = ret;
331         break;
332 
333     case 2:
334         if (acb->ret != ret) {
335             blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret);
336         }
337 
338         if (acb->verify) {
339             acb->verify(acb);
340         }
341 
342         acb->bh = qemu_bh_new(blkverify_aio_bh, acb);
343         qemu_bh_schedule(acb->bh);
344         break;
345     }
346 }
347 
348 static void blkverify_verify_readv(BlkverifyAIOCB *acb)
349 {
350     ssize_t offset = blkverify_iovec_compare(acb->qiov, &acb->raw_qiov);
351     if (offset != -1) {
352         blkverify_err(acb, "contents mismatch in sector %" PRId64,
353                       acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
354     }
355 }
356 
357 static BlockDriverAIOCB *blkverify_aio_readv(BlockDriverState *bs,
358         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
359         BlockDriverCompletionFunc *cb, void *opaque)
360 {
361     BDRVBlkverifyState *s = bs->opaque;
362     BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
363                                             nb_sectors, cb, opaque);
364 
365     acb->verify = blkverify_verify_readv;
366     acb->buf = qemu_blockalign(bs->file, qiov->size);
367     qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
368     blkverify_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
369 
370     bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors,
371                    blkverify_aio_cb, acb);
372     bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors,
373                    blkverify_aio_cb, acb);
374     return &acb->common;
375 }
376 
377 static BlockDriverAIOCB *blkverify_aio_writev(BlockDriverState *bs,
378         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
379         BlockDriverCompletionFunc *cb, void *opaque)
380 {
381     BDRVBlkverifyState *s = bs->opaque;
382     BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
383                                             nb_sectors, cb, opaque);
384 
385     bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors,
386                     blkverify_aio_cb, acb);
387     bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors,
388                     blkverify_aio_cb, acb);
389     return &acb->common;
390 }
391 
392 static BlockDriverAIOCB *blkverify_aio_flush(BlockDriverState *bs,
393                                              BlockDriverCompletionFunc *cb,
394                                              void *opaque)
395 {
396     BDRVBlkverifyState *s = bs->opaque;
397 
398     /* Only flush test file, the raw file is not important */
399     return bdrv_aio_flush(s->test_file, cb, opaque);
400 }
401 
402 static BlockDriver bdrv_blkverify = {
403     .format_name            = "blkverify",
404     .protocol_name          = "blkverify",
405     .instance_size          = sizeof(BDRVBlkverifyState),
406 
407     .bdrv_parse_filename    = blkverify_parse_filename,
408     .bdrv_file_open         = blkverify_open,
409     .bdrv_close             = blkverify_close,
410     .bdrv_getlength         = blkverify_getlength,
411 
412     .bdrv_aio_readv         = blkverify_aio_readv,
413     .bdrv_aio_writev        = blkverify_aio_writev,
414     .bdrv_aio_flush         = blkverify_aio_flush,
415 };
416 
417 static void bdrv_blkverify_init(void)
418 {
419     bdrv_register(&bdrv_blkverify);
420 }
421 
422 block_init(bdrv_blkverify_init);
423