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"
13 #include "qapi/qmp/qdict.h"
14 #include "qapi/qmp/qstring.h"
17 BlockDriverState
*test_file
;
20 typedef struct BlkverifyAIOCB BlkverifyAIOCB
;
21 struct BlkverifyAIOCB
{
22 BlockDriverAIOCB common
;
25 /* Request metadata */
30 int ret
; /* first completed request's result */
31 unsigned int done
; /* completion counter */
32 bool *finished
; /* completion signal for cancel */
34 QEMUIOVector
*qiov
; /* user I/O vector */
35 QEMUIOVector raw_qiov
; /* cloned I/O vector for raw file */
36 void *buf
; /* buffer for raw file I/O */
38 void (*verify
)(BlkverifyAIOCB
*acb
);
41 static void blkverify_aio_cancel(BlockDriverAIOCB
*blockacb
)
43 BlkverifyAIOCB
*acb
= (BlkverifyAIOCB
*)blockacb
;
44 AioContext
*aio_context
= bdrv_get_aio_context(blockacb
->bs
);
45 bool finished
= false;
47 /* Wait until request completes, invokes its callback, and frees itself */
48 acb
->finished
= &finished
;
50 aio_poll(aio_context
, true);
54 static const AIOCBInfo blkverify_aiocb_info
= {
55 .aiocb_size
= sizeof(BlkverifyAIOCB
),
56 .cancel
= blkverify_aio_cancel
,
59 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB
*acb
,
65 fprintf(stderr
, "blkverify: %s sector_num=%" PRId64
" nb_sectors=%d ",
66 acb
->is_write
? "write" : "read", acb
->sector_num
,
68 vfprintf(stderr
, fmt
, ap
);
69 fprintf(stderr
, "\n");
74 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
75 static void blkverify_parse_filename(const char *filename
, QDict
*options
,
82 /* Parse the blkverify: prefix */
83 if (!strstart(filename
, "blkverify:", &filename
)) {
84 /* There was no prefix; therefore, all options have to be already
85 present in the QDict (except for the filename) */
86 qdict_put(options
, "x-image", qstring_from_str(filename
));
90 /* Parse the raw image filename */
91 c
= strchr(filename
, ':');
93 error_setg(errp
, "blkverify requires raw copy and original image path");
97 /* TODO Implement option pass-through and set raw.filename here */
98 raw_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
99 qdict_put(options
, "x-raw", raw_path
);
101 /* TODO Allow multi-level nesting and set file.filename here */
103 qdict_put(options
, "x-image", qstring_from_str(filename
));
106 static QemuOptsList runtime_opts
= {
108 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
112 .type
= QEMU_OPT_STRING
,
113 .help
= "[internal use only, will be removed]",
117 .type
= QEMU_OPT_STRING
,
118 .help
= "[internal use only, will be removed]",
120 { /* end of list */ }
124 static int blkverify_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
127 BDRVBlkverifyState
*s
= bs
->opaque
;
129 Error
*local_err
= NULL
;
132 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
133 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
135 error_propagate(errp
, local_err
);
140 /* Open the raw file */
141 assert(bs
->file
== NULL
);
142 ret
= bdrv_open_image(&bs
->file
, qemu_opt_get(opts
, "x-raw"), options
,
143 "raw", flags
| BDRV_O_PROTOCOL
, false, &local_err
);
145 error_propagate(errp
, local_err
);
149 /* Open the test file */
150 assert(s
->test_file
== NULL
);
151 ret
= bdrv_open_image(&s
->test_file
, qemu_opt_get(opts
, "x-image"), options
,
152 "test", flags
, false, &local_err
);
154 error_propagate(errp
, local_err
);
165 static void blkverify_close(BlockDriverState
*bs
)
167 BDRVBlkverifyState
*s
= bs
->opaque
;
169 bdrv_unref(s
->test_file
);
173 static int64_t blkverify_getlength(BlockDriverState
*bs
)
175 BDRVBlkverifyState
*s
= bs
->opaque
;
177 return bdrv_getlength(s
->test_file
);
180 static BlkverifyAIOCB
*blkverify_aio_get(BlockDriverState
*bs
, bool is_write
,
181 int64_t sector_num
, QEMUIOVector
*qiov
,
183 BlockDriverCompletionFunc
*cb
,
186 BlkverifyAIOCB
*acb
= qemu_aio_get(&blkverify_aiocb_info
, bs
, cb
, opaque
);
189 acb
->is_write
= is_write
;
190 acb
->sector_num
= sector_num
;
191 acb
->nb_sectors
= nb_sectors
;
192 acb
->ret
= -EINPROGRESS
;
197 acb
->finished
= NULL
;
201 static void blkverify_aio_bh(void *opaque
)
203 BlkverifyAIOCB
*acb
= opaque
;
205 qemu_bh_delete(acb
->bh
);
207 qemu_iovec_destroy(&acb
->raw_qiov
);
208 qemu_vfree(acb
->buf
);
210 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
212 *acb
->finished
= true;
214 qemu_aio_release(acb
);
217 static void blkverify_aio_cb(void *opaque
, int ret
)
219 BlkverifyAIOCB
*acb
= opaque
;
221 switch (++acb
->done
) {
227 if (acb
->ret
!= ret
) {
228 blkverify_err(acb
, "return value mismatch %d != %d", acb
->ret
, ret
);
235 acb
->bh
= aio_bh_new(bdrv_get_aio_context(acb
->common
.bs
),
236 blkverify_aio_bh
, acb
);
237 qemu_bh_schedule(acb
->bh
);
242 static void blkverify_verify_readv(BlkverifyAIOCB
*acb
)
244 ssize_t offset
= qemu_iovec_compare(acb
->qiov
, &acb
->raw_qiov
);
246 blkverify_err(acb
, "contents mismatch in sector %" PRId64
,
247 acb
->sector_num
+ (int64_t)(offset
/ BDRV_SECTOR_SIZE
));
251 static BlockDriverAIOCB
*blkverify_aio_readv(BlockDriverState
*bs
,
252 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
253 BlockDriverCompletionFunc
*cb
, void *opaque
)
255 BDRVBlkverifyState
*s
= bs
->opaque
;
256 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, false, sector_num
, qiov
,
257 nb_sectors
, cb
, opaque
);
259 acb
->verify
= blkverify_verify_readv
;
260 acb
->buf
= qemu_blockalign(bs
->file
, qiov
->size
);
261 qemu_iovec_init(&acb
->raw_qiov
, acb
->qiov
->niov
);
262 qemu_iovec_clone(&acb
->raw_qiov
, qiov
, acb
->buf
);
264 bdrv_aio_readv(s
->test_file
, sector_num
, qiov
, nb_sectors
,
265 blkverify_aio_cb
, acb
);
266 bdrv_aio_readv(bs
->file
, sector_num
, &acb
->raw_qiov
, nb_sectors
,
267 blkverify_aio_cb
, acb
);
271 static BlockDriverAIOCB
*blkverify_aio_writev(BlockDriverState
*bs
,
272 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
273 BlockDriverCompletionFunc
*cb
, void *opaque
)
275 BDRVBlkverifyState
*s
= bs
->opaque
;
276 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, true, sector_num
, qiov
,
277 nb_sectors
, cb
, opaque
);
279 bdrv_aio_writev(s
->test_file
, sector_num
, qiov
, nb_sectors
,
280 blkverify_aio_cb
, acb
);
281 bdrv_aio_writev(bs
->file
, sector_num
, qiov
, nb_sectors
,
282 blkverify_aio_cb
, acb
);
286 static BlockDriverAIOCB
*blkverify_aio_flush(BlockDriverState
*bs
,
287 BlockDriverCompletionFunc
*cb
,
290 BDRVBlkverifyState
*s
= bs
->opaque
;
292 /* Only flush test file, the raw file is not important */
293 return bdrv_aio_flush(s
->test_file
, cb
, opaque
);
296 static bool blkverify_recurse_is_first_non_filter(BlockDriverState
*bs
,
297 BlockDriverState
*candidate
)
299 BDRVBlkverifyState
*s
= bs
->opaque
;
301 bool perm
= bdrv_recurse_is_first_non_filter(bs
->file
, candidate
);
307 return bdrv_recurse_is_first_non_filter(s
->test_file
, candidate
);
310 /* Propagate AioContext changes to ->test_file */
311 static void blkverify_detach_aio_context(BlockDriverState
*bs
)
313 BDRVBlkverifyState
*s
= bs
->opaque
;
315 bdrv_detach_aio_context(s
->test_file
);
318 static void blkverify_attach_aio_context(BlockDriverState
*bs
,
319 AioContext
*new_context
)
321 BDRVBlkverifyState
*s
= bs
->opaque
;
323 bdrv_attach_aio_context(s
->test_file
, new_context
);
326 static void blkverify_refresh_filename(BlockDriverState
*bs
)
328 BDRVBlkverifyState
*s
= bs
->opaque
;
330 /* bs->file has already been refreshed */
331 bdrv_refresh_filename(s
->test_file
);
333 if (bs
->file
->full_open_options
&& s
->test_file
->full_open_options
) {
334 QDict
*opts
= qdict_new();
335 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("blkverify")));
337 QINCREF(bs
->file
->full_open_options
);
338 qdict_put_obj(opts
, "raw", QOBJECT(bs
->file
->full_open_options
));
339 QINCREF(s
->test_file
->full_open_options
);
340 qdict_put_obj(opts
, "test", QOBJECT(s
->test_file
->full_open_options
));
342 bs
->full_open_options
= opts
;
345 if (bs
->file
->exact_filename
[0] && s
->test_file
->exact_filename
[0]) {
346 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
348 bs
->file
->exact_filename
, s
->test_file
->exact_filename
);
352 static BlockDriver bdrv_blkverify
= {
353 .format_name
= "blkverify",
354 .protocol_name
= "blkverify",
355 .instance_size
= sizeof(BDRVBlkverifyState
),
357 .bdrv_parse_filename
= blkverify_parse_filename
,
358 .bdrv_file_open
= blkverify_open
,
359 .bdrv_close
= blkverify_close
,
360 .bdrv_getlength
= blkverify_getlength
,
361 .bdrv_refresh_filename
= blkverify_refresh_filename
,
363 .bdrv_aio_readv
= blkverify_aio_readv
,
364 .bdrv_aio_writev
= blkverify_aio_writev
,
365 .bdrv_aio_flush
= blkverify_aio_flush
,
367 .bdrv_attach_aio_context
= blkverify_attach_aio_context
,
368 .bdrv_detach_aio_context
= blkverify_detach_aio_context
,
371 .bdrv_recurse_is_first_non_filter
= blkverify_recurse_is_first_non_filter
,
374 static void bdrv_blkverify_init(void)
376 bdrv_register(&bdrv_blkverify
);
379 block_init(bdrv_blkverify_init
);