2 * Block protocol for block driver correctness testing
4 * Copyright (C) 2010 IBM, Corp.
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.
11 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */
12 #include "block/block_int.h"
15 BlockDriverState
*test_file
;
18 typedef struct BlkverifyAIOCB BlkverifyAIOCB
;
19 struct BlkverifyAIOCB
{
20 BlockDriverAIOCB common
;
23 /* Request metadata */
28 int ret
; /* first completed request's result */
29 unsigned int done
; /* completion counter */
30 bool *finished
; /* completion signal for cancel */
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 */
36 void (*verify
)(BlkverifyAIOCB
*acb
);
39 static void blkverify_aio_cancel(BlockDriverAIOCB
*blockacb
)
41 BlkverifyAIOCB
*acb
= (BlkverifyAIOCB
*)blockacb
;
42 bool finished
= false;
44 /* Wait until request completes, invokes its callback, and frees itself */
45 acb
->finished
= &finished
;
51 static const AIOCBInfo blkverify_aiocb_info
= {
52 .aiocb_size
= sizeof(BlkverifyAIOCB
),
53 .cancel
= blkverify_aio_cancel
,
56 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB
*acb
,
62 fprintf(stderr
, "blkverify: %s sector_num=%" PRId64
" nb_sectors=%d ",
63 acb
->is_write
? "write" : "read", acb
->sector_num
,
65 vfprintf(stderr
, fmt
, ap
);
66 fprintf(stderr
, "\n");
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
,
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
));
87 /* Parse the raw image filename */
88 c
= strchr(filename
, ':');
90 error_setg(errp
, "blkverify requires raw copy and original image path");
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
);
98 /* TODO Allow multi-level nesting and set file.filename here */
100 qdict_put(options
, "x-image", qstring_from_str(filename
));
103 static QemuOptsList runtime_opts
= {
105 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
109 .type
= QEMU_OPT_STRING
,
110 .help
= "[internal use only, will be removed]",
114 .type
= QEMU_OPT_STRING
,
115 .help
= "[internal use only, will be removed]",
117 { /* end of list */ }
121 static int blkverify_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
124 BDRVBlkverifyState
*s
= bs
->opaque
;
126 Error
*local_err
= NULL
;
129 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
130 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
131 if (error_is_set(&local_err
)) {
132 error_propagate(errp
, local_err
);
137 /* Open the raw file */
138 ret
= bdrv_open_image(&bs
->file
, qemu_opt_get(opts
, "x-raw"), options
,
139 "raw", flags
, true, false, &local_err
);
141 error_propagate(errp
, local_err
);
145 /* Open the test file */
146 ret
= bdrv_open_image(&s
->test_file
, qemu_opt_get(opts
, "x-image"), options
,
147 "test", flags
, false, false, &local_err
);
149 error_propagate(errp
, local_err
);
159 static void blkverify_close(BlockDriverState
*bs
)
161 BDRVBlkverifyState
*s
= bs
->opaque
;
163 bdrv_unref(s
->test_file
);
167 static int64_t blkverify_getlength(BlockDriverState
*bs
)
169 BDRVBlkverifyState
*s
= bs
->opaque
;
171 return bdrv_getlength(s
->test_file
);
175 * Check that I/O vector contents are identical
179 * @ret: Offset to first mismatching byte or -1 if match
181 static ssize_t
blkverify_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
186 assert(a
->niov
== b
->niov
);
187 for (i
= 0; i
< a
->niov
; i
++) {
189 uint8_t *p
= (uint8_t *)a
->iov
[i
].iov_base
;
190 uint8_t *q
= (uint8_t *)b
->iov
[i
].iov_base
;
192 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
193 while (len
< a
->iov
[i
].iov_len
&& *p
++ == *q
++) {
199 if (len
!= a
->iov
[i
].iov_len
) {
208 struct iovec
*src_iov
;
212 static int sortelem_cmp_src_base(const void *a
, const void *b
)
214 const IOVectorSortElem
*elem_a
= a
;
215 const IOVectorSortElem
*elem_b
= b
;
218 if (elem_a
->src_iov
->iov_base
< elem_b
->src_iov
->iov_base
) {
220 } else if (elem_a
->src_iov
->iov_base
> elem_b
->src_iov
->iov_base
) {
227 static int sortelem_cmp_src_index(const void *a
, const void *b
)
229 const IOVectorSortElem
*elem_a
= a
;
230 const IOVectorSortElem
*elem_b
= b
;
232 return elem_a
->src_index
- elem_b
->src_index
;
236 * Copy contents of I/O vector
238 * The relative relationships of overlapping iovecs are preserved. This is
239 * necessary to ensure identical semantics in the cloned I/O vector.
241 static void blkverify_iovec_clone(QEMUIOVector
*dest
, const QEMUIOVector
*src
,
244 IOVectorSortElem sortelems
[src
->niov
];
248 /* Sort by source iovecs by base address */
249 for (i
= 0; i
< src
->niov
; i
++) {
250 sortelems
[i
].src_index
= i
;
251 sortelems
[i
].src_iov
= &src
->iov
[i
];
253 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_base
);
255 /* Allocate buffer space taking into account overlapping iovecs */
257 for (i
= 0; i
< src
->niov
; i
++) {
258 struct iovec
*cur
= sortelems
[i
].src_iov
;
259 ptrdiff_t rewind
= 0;
262 if (last_end
&& last_end
> cur
->iov_base
) {
263 rewind
= last_end
- cur
->iov_base
;
266 sortelems
[i
].dest_base
= buf
- rewind
;
267 buf
+= cur
->iov_len
- MIN(rewind
, cur
->iov_len
);
268 last_end
= MAX(cur
->iov_base
+ cur
->iov_len
, last_end
);
271 /* Sort by source iovec index and build destination iovec */
272 qsort(sortelems
, src
->niov
, sizeof(sortelems
[0]), sortelem_cmp_src_index
);
273 for (i
= 0; i
< src
->niov
; i
++) {
274 qemu_iovec_add(dest
, sortelems
[i
].dest_base
, src
->iov
[i
].iov_len
);
278 static BlkverifyAIOCB
*blkverify_aio_get(BlockDriverState
*bs
, bool is_write
,
279 int64_t sector_num
, QEMUIOVector
*qiov
,
281 BlockDriverCompletionFunc
*cb
,
284 BlkverifyAIOCB
*acb
= qemu_aio_get(&blkverify_aiocb_info
, bs
, cb
, opaque
);
287 acb
->is_write
= is_write
;
288 acb
->sector_num
= sector_num
;
289 acb
->nb_sectors
= nb_sectors
;
290 acb
->ret
= -EINPROGRESS
;
295 acb
->finished
= NULL
;
299 static void blkverify_aio_bh(void *opaque
)
301 BlkverifyAIOCB
*acb
= opaque
;
303 qemu_bh_delete(acb
->bh
);
305 qemu_iovec_destroy(&acb
->raw_qiov
);
306 qemu_vfree(acb
->buf
);
308 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
310 *acb
->finished
= true;
312 qemu_aio_release(acb
);
315 static void blkverify_aio_cb(void *opaque
, int ret
)
317 BlkverifyAIOCB
*acb
= opaque
;
319 switch (++acb
->done
) {
325 if (acb
->ret
!= ret
) {
326 blkverify_err(acb
, "return value mismatch %d != %d", acb
->ret
, ret
);
333 acb
->bh
= qemu_bh_new(blkverify_aio_bh
, acb
);
334 qemu_bh_schedule(acb
->bh
);
339 static void blkverify_verify_readv(BlkverifyAIOCB
*acb
)
341 ssize_t offset
= blkverify_iovec_compare(acb
->qiov
, &acb
->raw_qiov
);
343 blkverify_err(acb
, "contents mismatch in sector %" PRId64
,
344 acb
->sector_num
+ (int64_t)(offset
/ BDRV_SECTOR_SIZE
));
348 static BlockDriverAIOCB
*blkverify_aio_readv(BlockDriverState
*bs
,
349 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
350 BlockDriverCompletionFunc
*cb
, void *opaque
)
352 BDRVBlkverifyState
*s
= bs
->opaque
;
353 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, false, sector_num
, qiov
,
354 nb_sectors
, cb
, opaque
);
356 acb
->verify
= blkverify_verify_readv
;
357 acb
->buf
= qemu_blockalign(bs
->file
, qiov
->size
);
358 qemu_iovec_init(&acb
->raw_qiov
, acb
->qiov
->niov
);
359 blkverify_iovec_clone(&acb
->raw_qiov
, qiov
, acb
->buf
);
361 bdrv_aio_readv(s
->test_file
, sector_num
, qiov
, nb_sectors
,
362 blkverify_aio_cb
, acb
);
363 bdrv_aio_readv(bs
->file
, sector_num
, &acb
->raw_qiov
, nb_sectors
,
364 blkverify_aio_cb
, acb
);
368 static BlockDriverAIOCB
*blkverify_aio_writev(BlockDriverState
*bs
,
369 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
370 BlockDriverCompletionFunc
*cb
, void *opaque
)
372 BDRVBlkverifyState
*s
= bs
->opaque
;
373 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, true, sector_num
, qiov
,
374 nb_sectors
, cb
, opaque
);
376 bdrv_aio_writev(s
->test_file
, sector_num
, qiov
, nb_sectors
,
377 blkverify_aio_cb
, acb
);
378 bdrv_aio_writev(bs
->file
, sector_num
, qiov
, nb_sectors
,
379 blkverify_aio_cb
, acb
);
383 static BlockDriverAIOCB
*blkverify_aio_flush(BlockDriverState
*bs
,
384 BlockDriverCompletionFunc
*cb
,
387 BDRVBlkverifyState
*s
= bs
->opaque
;
389 /* Only flush test file, the raw file is not important */
390 return bdrv_aio_flush(s
->test_file
, cb
, opaque
);
393 static BlockDriver bdrv_blkverify
= {
394 .format_name
= "blkverify",
395 .protocol_name
= "blkverify",
396 .instance_size
= sizeof(BDRVBlkverifyState
),
398 .bdrv_parse_filename
= blkverify_parse_filename
,
399 .bdrv_file_open
= blkverify_open
,
400 .bdrv_close
= blkverify_close
,
401 .bdrv_getlength
= blkverify_getlength
,
403 .bdrv_aio_readv
= blkverify_aio_readv
,
404 .bdrv_aio_writev
= blkverify_aio_writev
,
405 .bdrv_aio_flush
= blkverify_aio_flush
,
407 .authorizations
= { true, false },
410 static void bdrv_blkverify_init(void)
412 bdrv_register(&bdrv_blkverify
);
415 block_init(bdrv_blkverify_init
);