]>
Commit | Line | Data |
---|---|---|
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" |
d9d33417 SH |
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 | ||
d7331bed | 51 | static const AIOCBInfo blkverify_aiocb_info = { |
d9d33417 SH |
52 | .aiocb_size = sizeof(BlkverifyAIOCB), |
53 | .cancel = blkverify_aio_cancel, | |
54 | }; | |
55 | ||
a77cffe7 SW |
56 | static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb, |
57 | const char *fmt, ...) | |
d9d33417 SH |
58 | { |
59 | va_list ap; | |
60 | ||
61 | va_start(ap, fmt); | |
687db4ed | 62 | fprintf(stderr, "blkverify: %s sector_num=%" PRId64 " nb_sectors=%d ", |
d9d33417 SH |
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 */ | |
16c79092 KW |
72 | static void blkverify_parse_filename(const char *filename, QDict *options, |
73 | Error **errp) | |
d9d33417 | 74 | { |
16c79092 KW |
75 | const char *c; |
76 | QString *raw_path; | |
77 | ||
d9d33417 SH |
78 | |
79 | /* Parse the blkverify: prefix */ | |
16c79092 KW |
80 | if (!strstart(filename, "blkverify:", &filename)) { |
81 | error_setg(errp, "File name string must start with 'blkverify:'"); | |
82 | return; | |
d9d33417 | 83 | } |
d9d33417 SH |
84 | |
85 | /* Parse the raw image filename */ | |
86 | c = strchr(filename, ':'); | |
87 | if (c == NULL) { | |
16c79092 KW |
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 | ||
015a1036 HR |
119 | static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, |
120 | Error **errp) | |
16c79092 KW |
121 | { |
122 | BDRVBlkverifyState *s = bs->opaque; | |
123 | QemuOpts *opts; | |
124 | Error *local_err = NULL; | |
125 | const char *filename, *raw; | |
126 | int ret; | |
127 | ||
128 | opts = qemu_opts_create_nofail(&runtime_opts); | |
129 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
130 | if (error_is_set(&local_err)) { | |
131 | qerror_report_err(local_err); | |
132 | error_free(local_err); | |
133 | ret = -EINVAL; | |
134 | goto fail; | |
135 | } | |
136 | ||
137 | /* Parse the raw image filename */ | |
138 | raw = qemu_opt_get(opts, "x-raw"); | |
139 | if (raw == NULL) { | |
140 | ret = -EINVAL; | |
141 | goto fail; | |
d9d33417 SH |
142 | } |
143 | ||
34b5d2c6 | 144 | ret = bdrv_file_open(&bs->file, raw, NULL, flags, &local_err); |
d9d33417 | 145 | if (ret < 0) { |
34b5d2c6 HR |
146 | qerror_report_err(local_err); |
147 | error_free(local_err); | |
16c79092 | 148 | goto fail; |
d9d33417 | 149 | } |
d9d33417 SH |
150 | |
151 | /* Open the test file */ | |
16c79092 KW |
152 | filename = qemu_opt_get(opts, "x-image"); |
153 | if (filename == NULL) { | |
154 | ret = -EINVAL; | |
155 | goto fail; | |
156 | } | |
157 | ||
d9d33417 | 158 | s->test_file = bdrv_new(""); |
34b5d2c6 | 159 | ret = bdrv_open(s->test_file, filename, NULL, flags, NULL, &local_err); |
d9d33417 | 160 | if (ret < 0) { |
34b5d2c6 HR |
161 | qerror_report_err(local_err); |
162 | error_free(local_err); | |
4f6fd349 | 163 | bdrv_unref(s->test_file); |
d9d33417 | 164 | s->test_file = NULL; |
16c79092 | 165 | goto fail; |
d9d33417 SH |
166 | } |
167 | ||
16c79092 KW |
168 | ret = 0; |
169 | fail: | |
170 | return ret; | |
d9d33417 SH |
171 | } |
172 | ||
173 | static void blkverify_close(BlockDriverState *bs) | |
174 | { | |
175 | BDRVBlkverifyState *s = bs->opaque; | |
176 | ||
4f6fd349 | 177 | bdrv_unref(s->test_file); |
d9d33417 SH |
178 | s->test_file = NULL; |
179 | } | |
180 | ||
d9d33417 SH |
181 | static int64_t blkverify_getlength(BlockDriverState *bs) |
182 | { | |
183 | BDRVBlkverifyState *s = bs->opaque; | |
184 | ||
185 | return bdrv_getlength(s->test_file); | |
186 | } | |
187 | ||
188 | /** | |
189 | * Check that I/O vector contents are identical | |
190 | * | |
191 | * @a: I/O vector | |
192 | * @b: I/O vector | |
193 | * @ret: Offset to first mismatching byte or -1 if match | |
194 | */ | |
195 | static ssize_t blkverify_iovec_compare(QEMUIOVector *a, QEMUIOVector *b) | |
196 | { | |
197 | int i; | |
198 | ssize_t offset = 0; | |
199 | ||
200 | assert(a->niov == b->niov); | |
201 | for (i = 0; i < a->niov; i++) { | |
202 | size_t len = 0; | |
203 | uint8_t *p = (uint8_t *)a->iov[i].iov_base; | |
204 | uint8_t *q = (uint8_t *)b->iov[i].iov_base; | |
205 | ||
206 | assert(a->iov[i].iov_len == b->iov[i].iov_len); | |
207 | while (len < a->iov[i].iov_len && *p++ == *q++) { | |
208 | len++; | |
209 | } | |
210 | ||
211 | offset += len; | |
212 | ||
213 | if (len != a->iov[i].iov_len) { | |
214 | return offset; | |
215 | } | |
216 | } | |
217 | return -1; | |
218 | } | |
219 | ||
220 | typedef struct { | |
221 | int src_index; | |
222 | struct iovec *src_iov; | |
223 | void *dest_base; | |
224 | } IOVectorSortElem; | |
225 | ||
226 | static int sortelem_cmp_src_base(const void *a, const void *b) | |
227 | { | |
228 | const IOVectorSortElem *elem_a = a; | |
229 | const IOVectorSortElem *elem_b = b; | |
230 | ||
231 | /* Don't overflow */ | |
232 | if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) { | |
233 | return -1; | |
234 | } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) { | |
235 | return 1; | |
236 | } else { | |
237 | return 0; | |
238 | } | |
239 | } | |
240 | ||
241 | static int sortelem_cmp_src_index(const void *a, const void *b) | |
242 | { | |
243 | const IOVectorSortElem *elem_a = a; | |
244 | const IOVectorSortElem *elem_b = b; | |
245 | ||
246 | return elem_a->src_index - elem_b->src_index; | |
247 | } | |
248 | ||
249 | /** | |
250 | * Copy contents of I/O vector | |
251 | * | |
252 | * The relative relationships of overlapping iovecs are preserved. This is | |
253 | * necessary to ensure identical semantics in the cloned I/O vector. | |
254 | */ | |
255 | static void blkverify_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, | |
256 | void *buf) | |
257 | { | |
258 | IOVectorSortElem sortelems[src->niov]; | |
259 | void *last_end; | |
260 | int i; | |
261 | ||
262 | /* Sort by source iovecs by base address */ | |
263 | for (i = 0; i < src->niov; i++) { | |
264 | sortelems[i].src_index = i; | |
265 | sortelems[i].src_iov = &src->iov[i]; | |
266 | } | |
267 | qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base); | |
268 | ||
269 | /* Allocate buffer space taking into account overlapping iovecs */ | |
270 | last_end = NULL; | |
271 | for (i = 0; i < src->niov; i++) { | |
272 | struct iovec *cur = sortelems[i].src_iov; | |
273 | ptrdiff_t rewind = 0; | |
274 | ||
275 | /* Detect overlap */ | |
276 | if (last_end && last_end > cur->iov_base) { | |
277 | rewind = last_end - cur->iov_base; | |
278 | } | |
279 | ||
280 | sortelems[i].dest_base = buf - rewind; | |
281 | buf += cur->iov_len - MIN(rewind, cur->iov_len); | |
282 | last_end = MAX(cur->iov_base + cur->iov_len, last_end); | |
283 | } | |
284 | ||
285 | /* Sort by source iovec index and build destination iovec */ | |
286 | qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index); | |
287 | for (i = 0; i < src->niov; i++) { | |
288 | qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len); | |
289 | } | |
290 | } | |
291 | ||
292 | static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write, | |
293 | int64_t sector_num, QEMUIOVector *qiov, | |
294 | int nb_sectors, | |
295 | BlockDriverCompletionFunc *cb, | |
296 | void *opaque) | |
297 | { | |
d7331bed | 298 | BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque); |
d9d33417 SH |
299 | |
300 | acb->bh = NULL; | |
301 | acb->is_write = is_write; | |
302 | acb->sector_num = sector_num; | |
303 | acb->nb_sectors = nb_sectors; | |
304 | acb->ret = -EINPROGRESS; | |
305 | acb->done = 0; | |
306 | acb->qiov = qiov; | |
307 | acb->buf = NULL; | |
308 | acb->verify = NULL; | |
309 | acb->finished = NULL; | |
310 | return acb; | |
311 | } | |
312 | ||
313 | static void blkverify_aio_bh(void *opaque) | |
314 | { | |
315 | BlkverifyAIOCB *acb = opaque; | |
316 | ||
317 | qemu_bh_delete(acb->bh); | |
318 | if (acb->buf) { | |
319 | qemu_iovec_destroy(&acb->raw_qiov); | |
320 | qemu_vfree(acb->buf); | |
321 | } | |
322 | acb->common.cb(acb->common.opaque, acb->ret); | |
323 | if (acb->finished) { | |
324 | *acb->finished = true; | |
325 | } | |
326 | qemu_aio_release(acb); | |
327 | } | |
328 | ||
329 | static void blkverify_aio_cb(void *opaque, int ret) | |
330 | { | |
331 | BlkverifyAIOCB *acb = opaque; | |
332 | ||
333 | switch (++acb->done) { | |
334 | case 1: | |
335 | acb->ret = ret; | |
336 | break; | |
337 | ||
338 | case 2: | |
339 | if (acb->ret != ret) { | |
340 | blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret); | |
341 | } | |
342 | ||
343 | if (acb->verify) { | |
344 | acb->verify(acb); | |
345 | } | |
346 | ||
347 | acb->bh = qemu_bh_new(blkverify_aio_bh, acb); | |
348 | qemu_bh_schedule(acb->bh); | |
349 | break; | |
350 | } | |
351 | } | |
352 | ||
353 | static void blkverify_verify_readv(BlkverifyAIOCB *acb) | |
354 | { | |
355 | ssize_t offset = blkverify_iovec_compare(acb->qiov, &acb->raw_qiov); | |
356 | if (offset != -1) { | |
a3133586 BS |
357 | blkverify_err(acb, "contents mismatch in sector %" PRId64, |
358 | acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE)); | |
d9d33417 SH |
359 | } |
360 | } | |
361 | ||
362 | static BlockDriverAIOCB *blkverify_aio_readv(BlockDriverState *bs, | |
363 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
364 | BlockDriverCompletionFunc *cb, void *opaque) | |
365 | { | |
366 | BDRVBlkverifyState *s = bs->opaque; | |
367 | BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov, | |
368 | nb_sectors, cb, opaque); | |
369 | ||
370 | acb->verify = blkverify_verify_readv; | |
371 | acb->buf = qemu_blockalign(bs->file, qiov->size); | |
372 | qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov); | |
373 | blkverify_iovec_clone(&acb->raw_qiov, qiov, acb->buf); | |
374 | ||
ad54ae80 PB |
375 | bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors, |
376 | blkverify_aio_cb, acb); | |
377 | bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors, | |
378 | blkverify_aio_cb, acb); | |
d9d33417 SH |
379 | return &acb->common; |
380 | } | |
381 | ||
382 | static BlockDriverAIOCB *blkverify_aio_writev(BlockDriverState *bs, | |
383 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
384 | BlockDriverCompletionFunc *cb, void *opaque) | |
385 | { | |
386 | BDRVBlkverifyState *s = bs->opaque; | |
387 | BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov, | |
388 | nb_sectors, cb, opaque); | |
389 | ||
ad54ae80 PB |
390 | bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors, |
391 | blkverify_aio_cb, acb); | |
392 | bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, | |
393 | blkverify_aio_cb, acb); | |
d9d33417 SH |
394 | return &acb->common; |
395 | } | |
396 | ||
397 | static BlockDriverAIOCB *blkverify_aio_flush(BlockDriverState *bs, | |
398 | BlockDriverCompletionFunc *cb, | |
399 | void *opaque) | |
400 | { | |
401 | BDRVBlkverifyState *s = bs->opaque; | |
402 | ||
403 | /* Only flush test file, the raw file is not important */ | |
404 | return bdrv_aio_flush(s->test_file, cb, opaque); | |
405 | } | |
406 | ||
407 | static BlockDriver bdrv_blkverify = { | |
16c79092 KW |
408 | .format_name = "blkverify", |
409 | .protocol_name = "blkverify", | |
410 | .instance_size = sizeof(BDRVBlkverifyState), | |
411 | ||
412 | .bdrv_parse_filename = blkverify_parse_filename, | |
413 | .bdrv_file_open = blkverify_open, | |
414 | .bdrv_close = blkverify_close, | |
415 | .bdrv_getlength = blkverify_getlength, | |
416 | ||
417 | .bdrv_aio_readv = blkverify_aio_readv, | |
418 | .bdrv_aio_writev = blkverify_aio_writev, | |
419 | .bdrv_aio_flush = blkverify_aio_flush, | |
d9d33417 SH |
420 | }; |
421 | ||
422 | static void bdrv_blkverify_init(void) | |
423 | { | |
424 | bdrv_register(&bdrv_blkverify); | |
425 | } | |
426 | ||
427 | block_init(bdrv_blkverify_init); |