root / block / blkverify.c @ f67503e5
History | View | Annotate | Download (11.9 kB)
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 |
/* There was no prefix; therefore, all options have to be already
|
82 |
present in the QDict (except for the filename) */
|
83 |
qdict_put(options, "x-image", qstring_from_str(filename));
|
84 |
return;
|
85 |
} |
86 |
|
87 |
/* Parse the raw image filename */
|
88 |
c = strchr(filename, ':');
|
89 |
if (c == NULL) { |
90 |
error_setg(errp, "blkverify requires raw copy and original image path");
|
91 |
return;
|
92 |
} |
93 |
|
94 |
/* TODO Implement option pass-through and set raw.filename here */
|
95 |
raw_path = qstring_from_substr(filename, 0, c - filename - 1); |
96 |
qdict_put(options, "x-raw", raw_path);
|
97 |
|
98 |
/* TODO Allow multi-level nesting and set file.filename here */
|
99 |
filename = c + 1;
|
100 |
qdict_put(options, "x-image", qstring_from_str(filename));
|
101 |
} |
102 |
|
103 |
static QemuOptsList runtime_opts = {
|
104 |
.name = "blkverify",
|
105 |
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
106 |
.desc = { |
107 |
{ |
108 |
.name = "x-raw",
|
109 |
.type = QEMU_OPT_STRING, |
110 |
.help = "[internal use only, will be removed]",
|
111 |
}, |
112 |
{ |
113 |
.name = "x-image",
|
114 |
.type = QEMU_OPT_STRING, |
115 |
.help = "[internal use only, will be removed]",
|
116 |
}, |
117 |
{ /* end of list */ }
|
118 |
}, |
119 |
}; |
120 |
|
121 |
static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, |
122 |
Error **errp) |
123 |
{ |
124 |
BDRVBlkverifyState *s = bs->opaque; |
125 |
QemuOpts *opts; |
126 |
Error *local_err = NULL;
|
127 |
int ret;
|
128 |
|
129 |
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
130 |
qemu_opts_absorb_qdict(opts, options, &local_err); |
131 |
if (local_err) {
|
132 |
error_propagate(errp, local_err); |
133 |
ret = -EINVAL; |
134 |
goto fail;
|
135 |
} |
136 |
|
137 |
/* Open the raw file */
|
138 |
assert(bs->file == NULL);
|
139 |
ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-raw"), options,
|
140 |
"raw", flags, true, false, &local_err); |
141 |
if (ret < 0) { |
142 |
error_propagate(errp, local_err); |
143 |
goto fail;
|
144 |
} |
145 |
|
146 |
/* Open the test file */
|
147 |
assert(s->test_file == NULL);
|
148 |
ret = bdrv_open_image(&s->test_file, qemu_opt_get(opts, "x-image"), options,
|
149 |
"test", flags, false, false, &local_err); |
150 |
if (ret < 0) { |
151 |
error_propagate(errp, local_err); |
152 |
s->test_file = NULL;
|
153 |
goto fail;
|
154 |
} |
155 |
|
156 |
ret = 0;
|
157 |
fail:
|
158 |
return ret;
|
159 |
} |
160 |
|
161 |
static void blkverify_close(BlockDriverState *bs) |
162 |
{ |
163 |
BDRVBlkverifyState *s = bs->opaque; |
164 |
|
165 |
bdrv_unref(s->test_file); |
166 |
s->test_file = NULL;
|
167 |
} |
168 |
|
169 |
static int64_t blkverify_getlength(BlockDriverState *bs)
|
170 |
{ |
171 |
BDRVBlkverifyState *s = bs->opaque; |
172 |
|
173 |
return bdrv_getlength(s->test_file);
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Check that I/O vector contents are identical
|
178 |
*
|
179 |
* @a: I/O vector
|
180 |
* @b: I/O vector
|
181 |
* @ret: Offset to first mismatching byte or -1 if match
|
182 |
*/
|
183 |
static ssize_t blkverify_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
|
184 |
{ |
185 |
int i;
|
186 |
ssize_t offset = 0;
|
187 |
|
188 |
assert(a->niov == b->niov); |
189 |
for (i = 0; i < a->niov; i++) { |
190 |
size_t len = 0;
|
191 |
uint8_t *p = (uint8_t *)a->iov[i].iov_base; |
192 |
uint8_t *q = (uint8_t *)b->iov[i].iov_base; |
193 |
|
194 |
assert(a->iov[i].iov_len == b->iov[i].iov_len); |
195 |
while (len < a->iov[i].iov_len && *p++ == *q++) {
|
196 |
len++; |
197 |
} |
198 |
|
199 |
offset += len; |
200 |
|
201 |
if (len != a->iov[i].iov_len) {
|
202 |
return offset;
|
203 |
} |
204 |
} |
205 |
return -1; |
206 |
} |
207 |
|
208 |
typedef struct { |
209 |
int src_index;
|
210 |
struct iovec *src_iov;
|
211 |
void *dest_base;
|
212 |
} IOVectorSortElem; |
213 |
|
214 |
static int sortelem_cmp_src_base(const void *a, const void *b) |
215 |
{ |
216 |
const IOVectorSortElem *elem_a = a;
|
217 |
const IOVectorSortElem *elem_b = b;
|
218 |
|
219 |
/* Don't overflow */
|
220 |
if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
|
221 |
return -1; |
222 |
} else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) { |
223 |
return 1; |
224 |
} else {
|
225 |
return 0; |
226 |
} |
227 |
} |
228 |
|
229 |
static int sortelem_cmp_src_index(const void *a, const void *b) |
230 |
{ |
231 |
const IOVectorSortElem *elem_a = a;
|
232 |
const IOVectorSortElem *elem_b = b;
|
233 |
|
234 |
return elem_a->src_index - elem_b->src_index;
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* Copy contents of I/O vector
|
239 |
*
|
240 |
* The relative relationships of overlapping iovecs are preserved. This is
|
241 |
* necessary to ensure identical semantics in the cloned I/O vector.
|
242 |
*/
|
243 |
static void blkverify_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, |
244 |
void *buf)
|
245 |
{ |
246 |
IOVectorSortElem sortelems[src->niov]; |
247 |
void *last_end;
|
248 |
int i;
|
249 |
|
250 |
/* Sort by source iovecs by base address */
|
251 |
for (i = 0; i < src->niov; i++) { |
252 |
sortelems[i].src_index = i; |
253 |
sortelems[i].src_iov = &src->iov[i]; |
254 |
} |
255 |
qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base); |
256 |
|
257 |
/* Allocate buffer space taking into account overlapping iovecs */
|
258 |
last_end = NULL;
|
259 |
for (i = 0; i < src->niov; i++) { |
260 |
struct iovec *cur = sortelems[i].src_iov;
|
261 |
ptrdiff_t rewind = 0;
|
262 |
|
263 |
/* Detect overlap */
|
264 |
if (last_end && last_end > cur->iov_base) {
|
265 |
rewind = last_end - cur->iov_base; |
266 |
} |
267 |
|
268 |
sortelems[i].dest_base = buf - rewind; |
269 |
buf += cur->iov_len - MIN(rewind, cur->iov_len); |
270 |
last_end = MAX(cur->iov_base + cur->iov_len, last_end); |
271 |
} |
272 |
|
273 |
/* Sort by source iovec index and build destination iovec */
|
274 |
qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index); |
275 |
for (i = 0; i < src->niov; i++) { |
276 |
qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len); |
277 |
} |
278 |
} |
279 |
|
280 |
static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write, |
281 |
int64_t sector_num, QEMUIOVector *qiov, |
282 |
int nb_sectors,
|
283 |
BlockDriverCompletionFunc *cb, |
284 |
void *opaque)
|
285 |
{ |
286 |
BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque); |
287 |
|
288 |
acb->bh = NULL;
|
289 |
acb->is_write = is_write; |
290 |
acb->sector_num = sector_num; |
291 |
acb->nb_sectors = nb_sectors; |
292 |
acb->ret = -EINPROGRESS; |
293 |
acb->done = 0;
|
294 |
acb->qiov = qiov; |
295 |
acb->buf = NULL;
|
296 |
acb->verify = NULL;
|
297 |
acb->finished = NULL;
|
298 |
return acb;
|
299 |
} |
300 |
|
301 |
static void blkverify_aio_bh(void *opaque) |
302 |
{ |
303 |
BlkverifyAIOCB *acb = opaque; |
304 |
|
305 |
qemu_bh_delete(acb->bh); |
306 |
if (acb->buf) {
|
307 |
qemu_iovec_destroy(&acb->raw_qiov); |
308 |
qemu_vfree(acb->buf); |
309 |
} |
310 |
acb->common.cb(acb->common.opaque, acb->ret); |
311 |
if (acb->finished) {
|
312 |
*acb->finished = true;
|
313 |
} |
314 |
qemu_aio_release(acb); |
315 |
} |
316 |
|
317 |
static void blkverify_aio_cb(void *opaque, int ret) |
318 |
{ |
319 |
BlkverifyAIOCB *acb = opaque; |
320 |
|
321 |
switch (++acb->done) {
|
322 |
case 1: |
323 |
acb->ret = ret; |
324 |
break;
|
325 |
|
326 |
case 2: |
327 |
if (acb->ret != ret) {
|
328 |
blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret);
|
329 |
} |
330 |
|
331 |
if (acb->verify) {
|
332 |
acb->verify(acb); |
333 |
} |
334 |
|
335 |
acb->bh = qemu_bh_new(blkverify_aio_bh, acb); |
336 |
qemu_bh_schedule(acb->bh); |
337 |
break;
|
338 |
} |
339 |
} |
340 |
|
341 |
static void blkverify_verify_readv(BlkverifyAIOCB *acb) |
342 |
{ |
343 |
ssize_t offset = blkverify_iovec_compare(acb->qiov, &acb->raw_qiov); |
344 |
if (offset != -1) { |
345 |
blkverify_err(acb, "contents mismatch in sector %" PRId64,
|
346 |
acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE)); |
347 |
} |
348 |
} |
349 |
|
350 |
static BlockDriverAIOCB *blkverify_aio_readv(BlockDriverState *bs,
|
351 |
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
352 |
BlockDriverCompletionFunc *cb, void *opaque)
|
353 |
{ |
354 |
BDRVBlkverifyState *s = bs->opaque; |
355 |
BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
|
356 |
nb_sectors, cb, opaque); |
357 |
|
358 |
acb->verify = blkverify_verify_readv; |
359 |
acb->buf = qemu_blockalign(bs->file, qiov->size); |
360 |
qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov); |
361 |
blkverify_iovec_clone(&acb->raw_qiov, qiov, acb->buf); |
362 |
|
363 |
bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors, |
364 |
blkverify_aio_cb, acb); |
365 |
bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors, |
366 |
blkverify_aio_cb, acb); |
367 |
return &acb->common;
|
368 |
} |
369 |
|
370 |
static BlockDriverAIOCB *blkverify_aio_writev(BlockDriverState *bs,
|
371 |
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
372 |
BlockDriverCompletionFunc *cb, void *opaque)
|
373 |
{ |
374 |
BDRVBlkverifyState *s = bs->opaque; |
375 |
BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
|
376 |
nb_sectors, cb, opaque); |
377 |
|
378 |
bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors, |
379 |
blkverify_aio_cb, acb); |
380 |
bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, |
381 |
blkverify_aio_cb, acb); |
382 |
return &acb->common;
|
383 |
} |
384 |
|
385 |
static BlockDriverAIOCB *blkverify_aio_flush(BlockDriverState *bs,
|
386 |
BlockDriverCompletionFunc *cb, |
387 |
void *opaque)
|
388 |
{ |
389 |
BDRVBlkverifyState *s = bs->opaque; |
390 |
|
391 |
/* Only flush test file, the raw file is not important */
|
392 |
return bdrv_aio_flush(s->test_file, cb, opaque);
|
393 |
} |
394 |
|
395 |
static BlockDriver bdrv_blkverify = {
|
396 |
.format_name = "blkverify",
|
397 |
.protocol_name = "blkverify",
|
398 |
.instance_size = sizeof(BDRVBlkverifyState),
|
399 |
|
400 |
.bdrv_parse_filename = blkverify_parse_filename, |
401 |
.bdrv_file_open = blkverify_open, |
402 |
.bdrv_close = blkverify_close, |
403 |
.bdrv_getlength = blkverify_getlength, |
404 |
|
405 |
.bdrv_aio_readv = blkverify_aio_readv, |
406 |
.bdrv_aio_writev = blkverify_aio_writev, |
407 |
.bdrv_aio_flush = blkverify_aio_flush, |
408 |
|
409 |
.authorizations = { true, false }, |
410 |
}; |
411 |
|
412 |
static void bdrv_blkverify_init(void) |
413 |
{ |
414 |
bdrv_register(&bdrv_blkverify); |
415 |
} |
416 |
|
417 |
block_init(bdrv_blkverify_init); |