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 AioContext
*aio_context
= bdrv_get_aio_context(blockacb
->bs
);
43 bool finished
= false;
45 /* Wait until request completes, invokes its callback, and frees itself */
46 acb
->finished
= &finished
;
48 aio_poll(aio_context
, true);
52 static const AIOCBInfo blkverify_aiocb_info
= {
53 .aiocb_size
= sizeof(BlkverifyAIOCB
),
54 .cancel
= blkverify_aio_cancel
,
57 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB
*acb
,
63 fprintf(stderr
, "blkverify: %s sector_num=%" PRId64
" nb_sectors=%d ",
64 acb
->is_write
? "write" : "read", acb
->sector_num
,
66 vfprintf(stderr
, fmt
, ap
);
67 fprintf(stderr
, "\n");
72 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
73 static void blkverify_parse_filename(const char *filename
, QDict
*options
,
80 /* Parse the blkverify: prefix */
81 if (!strstart(filename
, "blkverify:", &filename
)) {
82 /* There was no prefix; therefore, all options have to be already
83 present in the QDict (except for the filename) */
84 qdict_put(options
, "x-image", qstring_from_str(filename
));
88 /* Parse the raw image filename */
89 c
= strchr(filename
, ':');
91 error_setg(errp
, "blkverify requires raw copy and original image path");
95 /* TODO Implement option pass-through and set raw.filename here */
96 raw_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
97 qdict_put(options
, "x-raw", raw_path
);
99 /* TODO Allow multi-level nesting and set file.filename here */
101 qdict_put(options
, "x-image", qstring_from_str(filename
));
104 static QemuOptsList runtime_opts
= {
106 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
110 .type
= QEMU_OPT_STRING
,
111 .help
= "[internal use only, will be removed]",
115 .type
= QEMU_OPT_STRING
,
116 .help
= "[internal use only, will be removed]",
118 { /* end of list */ }
122 static int blkverify_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
125 BDRVBlkverifyState
*s
= bs
->opaque
;
127 Error
*local_err
= NULL
;
130 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
131 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
133 error_propagate(errp
, local_err
);
138 /* Open the raw file */
139 assert(bs
->file
== NULL
);
140 ret
= bdrv_open_image(&bs
->file
, qemu_opt_get(opts
, "x-raw"), options
,
141 "raw", flags
| BDRV_O_PROTOCOL
, false, &local_err
);
143 error_propagate(errp
, local_err
);
147 /* Open the test file */
148 assert(s
->test_file
== NULL
);
149 ret
= bdrv_open_image(&s
->test_file
, qemu_opt_get(opts
, "x-image"), options
,
150 "test", flags
, false, &local_err
);
152 error_propagate(errp
, local_err
);
162 static void blkverify_close(BlockDriverState
*bs
)
164 BDRVBlkverifyState
*s
= bs
->opaque
;
166 bdrv_unref(s
->test_file
);
170 static int64_t blkverify_getlength(BlockDriverState
*bs
)
172 BDRVBlkverifyState
*s
= bs
->opaque
;
174 return bdrv_getlength(s
->test_file
);
177 static BlkverifyAIOCB
*blkverify_aio_get(BlockDriverState
*bs
, bool is_write
,
178 int64_t sector_num
, QEMUIOVector
*qiov
,
180 BlockDriverCompletionFunc
*cb
,
183 BlkverifyAIOCB
*acb
= qemu_aio_get(&blkverify_aiocb_info
, bs
, cb
, opaque
);
186 acb
->is_write
= is_write
;
187 acb
->sector_num
= sector_num
;
188 acb
->nb_sectors
= nb_sectors
;
189 acb
->ret
= -EINPROGRESS
;
194 acb
->finished
= NULL
;
198 static void blkverify_aio_bh(void *opaque
)
200 BlkverifyAIOCB
*acb
= opaque
;
202 qemu_bh_delete(acb
->bh
);
204 qemu_iovec_destroy(&acb
->raw_qiov
);
205 qemu_vfree(acb
->buf
);
207 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
209 *acb
->finished
= true;
211 qemu_aio_release(acb
);
214 static void blkverify_aio_cb(void *opaque
, int ret
)
216 BlkverifyAIOCB
*acb
= opaque
;
218 switch (++acb
->done
) {
224 if (acb
->ret
!= ret
) {
225 blkverify_err(acb
, "return value mismatch %d != %d", acb
->ret
, ret
);
232 acb
->bh
= aio_bh_new(bdrv_get_aio_context(acb
->common
.bs
),
233 blkverify_aio_bh
, acb
);
234 qemu_bh_schedule(acb
->bh
);
239 static void blkverify_verify_readv(BlkverifyAIOCB
*acb
)
241 ssize_t offset
= qemu_iovec_compare(acb
->qiov
, &acb
->raw_qiov
);
243 blkverify_err(acb
, "contents mismatch in sector %" PRId64
,
244 acb
->sector_num
+ (int64_t)(offset
/ BDRV_SECTOR_SIZE
));
248 static BlockDriverAIOCB
*blkverify_aio_readv(BlockDriverState
*bs
,
249 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
250 BlockDriverCompletionFunc
*cb
, void *opaque
)
252 BDRVBlkverifyState
*s
= bs
->opaque
;
253 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, false, sector_num
, qiov
,
254 nb_sectors
, cb
, opaque
);
256 acb
->verify
= blkverify_verify_readv
;
257 acb
->buf
= qemu_blockalign(bs
->file
, qiov
->size
);
258 qemu_iovec_init(&acb
->raw_qiov
, acb
->qiov
->niov
);
259 qemu_iovec_clone(&acb
->raw_qiov
, qiov
, acb
->buf
);
261 bdrv_aio_readv(s
->test_file
, sector_num
, qiov
, nb_sectors
,
262 blkverify_aio_cb
, acb
);
263 bdrv_aio_readv(bs
->file
, sector_num
, &acb
->raw_qiov
, nb_sectors
,
264 blkverify_aio_cb
, acb
);
268 static BlockDriverAIOCB
*blkverify_aio_writev(BlockDriverState
*bs
,
269 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
270 BlockDriverCompletionFunc
*cb
, void *opaque
)
272 BDRVBlkverifyState
*s
= bs
->opaque
;
273 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, true, sector_num
, qiov
,
274 nb_sectors
, cb
, opaque
);
276 bdrv_aio_writev(s
->test_file
, sector_num
, qiov
, nb_sectors
,
277 blkverify_aio_cb
, acb
);
278 bdrv_aio_writev(bs
->file
, sector_num
, qiov
, nb_sectors
,
279 blkverify_aio_cb
, acb
);
283 static BlockDriverAIOCB
*blkverify_aio_flush(BlockDriverState
*bs
,
284 BlockDriverCompletionFunc
*cb
,
287 BDRVBlkverifyState
*s
= bs
->opaque
;
289 /* Only flush test file, the raw file is not important */
290 return bdrv_aio_flush(s
->test_file
, cb
, opaque
);
293 static bool blkverify_recurse_is_first_non_filter(BlockDriverState
*bs
,
294 BlockDriverState
*candidate
)
296 BDRVBlkverifyState
*s
= bs
->opaque
;
298 bool perm
= bdrv_recurse_is_first_non_filter(bs
->file
, candidate
);
304 return bdrv_recurse_is_first_non_filter(s
->test_file
, candidate
);
307 /* Propagate AioContext changes to ->test_file */
308 static void blkverify_detach_aio_context(BlockDriverState
*bs
)
310 BDRVBlkverifyState
*s
= bs
->opaque
;
312 bdrv_detach_aio_context(s
->test_file
);
315 static void blkverify_attach_aio_context(BlockDriverState
*bs
,
316 AioContext
*new_context
)
318 BDRVBlkverifyState
*s
= bs
->opaque
;
320 bdrv_attach_aio_context(s
->test_file
, new_context
);
323 static BlockDriver bdrv_blkverify
= {
324 .format_name
= "blkverify",
325 .protocol_name
= "blkverify",
326 .instance_size
= sizeof(BDRVBlkverifyState
),
328 .bdrv_parse_filename
= blkverify_parse_filename
,
329 .bdrv_file_open
= blkverify_open
,
330 .bdrv_close
= blkverify_close
,
331 .bdrv_getlength
= blkverify_getlength
,
333 .bdrv_aio_readv
= blkverify_aio_readv
,
334 .bdrv_aio_writev
= blkverify_aio_writev
,
335 .bdrv_aio_flush
= blkverify_aio_flush
,
337 .bdrv_attach_aio_context
= blkverify_attach_aio_context
,
338 .bdrv_detach_aio_context
= blkverify_detach_aio_context
,
341 .bdrv_recurse_is_first_non_filter
= blkverify_recurse_is_first_non_filter
,
344 static void bdrv_blkverify_init(void)
346 bdrv_register(&bdrv_blkverify
);
349 block_init(bdrv_blkverify_init
);