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.
10 #include "qemu/osdep.h"
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"
20 typedef struct BlkverifyAIOCB BlkverifyAIOCB
;
21 struct BlkverifyAIOCB
{
25 /* Request metadata */
30 int ret
; /* first completed request's result */
31 unsigned int done
; /* completion counter */
33 QEMUIOVector
*qiov
; /* user I/O vector */
34 QEMUIOVector raw_qiov
; /* cloned I/O vector for raw file */
35 void *buf
; /* buffer for raw file I/O */
37 void (*verify
)(BlkverifyAIOCB
*acb
);
40 static const AIOCBInfo blkverify_aiocb_info
= {
41 .aiocb_size
= sizeof(BlkverifyAIOCB
),
44 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB
*acb
,
50 fprintf(stderr
, "blkverify: %s sector_num=%" PRId64
" nb_sectors=%d ",
51 acb
->is_write
? "write" : "read", acb
->sector_num
,
53 vfprintf(stderr
, fmt
, ap
);
54 fprintf(stderr
, "\n");
59 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
60 static void blkverify_parse_filename(const char *filename
, QDict
*options
,
67 /* Parse the blkverify: prefix */
68 if (!strstart(filename
, "blkverify:", &filename
)) {
69 /* There was no prefix; therefore, all options have to be already
70 present in the QDict (except for the filename) */
71 qdict_put(options
, "x-image", qstring_from_str(filename
));
75 /* Parse the raw image filename */
76 c
= strchr(filename
, ':');
78 error_setg(errp
, "blkverify requires raw copy and original image path");
82 /* TODO Implement option pass-through and set raw.filename here */
83 raw_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
84 qdict_put(options
, "x-raw", raw_path
);
86 /* TODO Allow multi-level nesting and set file.filename here */
88 qdict_put(options
, "x-image", qstring_from_str(filename
));
91 static QemuOptsList runtime_opts
= {
93 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
97 .type
= QEMU_OPT_STRING
,
98 .help
= "[internal use only, will be removed]",
102 .type
= QEMU_OPT_STRING
,
103 .help
= "[internal use only, will be removed]",
105 { /* end of list */ }
109 static int blkverify_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
112 BDRVBlkverifyState
*s
= bs
->opaque
;
114 Error
*local_err
= NULL
;
117 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
118 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
120 error_propagate(errp
, local_err
);
125 /* Open the raw file */
126 bs
->file
= bdrv_open_child(qemu_opt_get(opts
, "x-raw"), options
, "raw",
127 bs
, &child_file
, false, &local_err
);
130 error_propagate(errp
, local_err
);
134 /* Open the test file */
135 s
->test_file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
,
136 "test", bs
, &child_format
, false,
140 error_propagate(errp
, local_err
);
147 bdrv_unref_child(bs
, bs
->file
);
153 static void blkverify_close(BlockDriverState
*bs
)
155 BDRVBlkverifyState
*s
= bs
->opaque
;
157 bdrv_unref_child(bs
, s
->test_file
);
161 static int64_t blkverify_getlength(BlockDriverState
*bs
)
163 BDRVBlkverifyState
*s
= bs
->opaque
;
165 return bdrv_getlength(s
->test_file
->bs
);
168 static BlkverifyAIOCB
*blkverify_aio_get(BlockDriverState
*bs
, bool is_write
,
169 int64_t sector_num
, QEMUIOVector
*qiov
,
171 BlockCompletionFunc
*cb
,
174 BlkverifyAIOCB
*acb
= qemu_aio_get(&blkverify_aiocb_info
, bs
, cb
, opaque
);
177 acb
->is_write
= is_write
;
178 acb
->sector_num
= sector_num
;
179 acb
->nb_sectors
= nb_sectors
;
180 acb
->ret
= -EINPROGRESS
;
188 static void blkverify_aio_bh(void *opaque
)
190 BlkverifyAIOCB
*acb
= opaque
;
192 qemu_bh_delete(acb
->bh
);
194 qemu_iovec_destroy(&acb
->raw_qiov
);
195 qemu_vfree(acb
->buf
);
197 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
201 static void blkverify_aio_cb(void *opaque
, int ret
)
203 BlkverifyAIOCB
*acb
= opaque
;
205 switch (++acb
->done
) {
211 if (acb
->ret
!= ret
) {
212 blkverify_err(acb
, "return value mismatch %d != %d", acb
->ret
, ret
);
219 acb
->bh
= aio_bh_new(bdrv_get_aio_context(acb
->common
.bs
),
220 blkverify_aio_bh
, acb
);
221 qemu_bh_schedule(acb
->bh
);
226 static void blkverify_verify_readv(BlkverifyAIOCB
*acb
)
228 ssize_t offset
= qemu_iovec_compare(acb
->qiov
, &acb
->raw_qiov
);
230 blkverify_err(acb
, "contents mismatch in sector %" PRId64
,
231 acb
->sector_num
+ (int64_t)(offset
/ BDRV_SECTOR_SIZE
));
235 static BlockAIOCB
*blkverify_aio_readv(BlockDriverState
*bs
,
236 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
237 BlockCompletionFunc
*cb
, void *opaque
)
239 BDRVBlkverifyState
*s
= bs
->opaque
;
240 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, false, sector_num
, qiov
,
241 nb_sectors
, cb
, opaque
);
243 acb
->verify
= blkverify_verify_readv
;
244 acb
->buf
= qemu_blockalign(bs
->file
->bs
, qiov
->size
);
245 qemu_iovec_init(&acb
->raw_qiov
, acb
->qiov
->niov
);
246 qemu_iovec_clone(&acb
->raw_qiov
, qiov
, acb
->buf
);
248 bdrv_aio_readv(s
->test_file
->bs
, sector_num
, qiov
, nb_sectors
,
249 blkverify_aio_cb
, acb
);
250 bdrv_aio_readv(bs
->file
->bs
, sector_num
, &acb
->raw_qiov
, nb_sectors
,
251 blkverify_aio_cb
, acb
);
255 static BlockAIOCB
*blkverify_aio_writev(BlockDriverState
*bs
,
256 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
257 BlockCompletionFunc
*cb
, void *opaque
)
259 BDRVBlkverifyState
*s
= bs
->opaque
;
260 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, true, sector_num
, qiov
,
261 nb_sectors
, cb
, opaque
);
263 bdrv_aio_writev(s
->test_file
->bs
, sector_num
, qiov
, nb_sectors
,
264 blkverify_aio_cb
, acb
);
265 bdrv_aio_writev(bs
->file
->bs
, sector_num
, qiov
, nb_sectors
,
266 blkverify_aio_cb
, acb
);
270 static BlockAIOCB
*blkverify_aio_flush(BlockDriverState
*bs
,
271 BlockCompletionFunc
*cb
,
274 BDRVBlkverifyState
*s
= bs
->opaque
;
276 /* Only flush test file, the raw file is not important */
277 return bdrv_aio_flush(s
->test_file
->bs
, cb
, opaque
);
280 static bool blkverify_recurse_is_first_non_filter(BlockDriverState
*bs
,
281 BlockDriverState
*candidate
)
283 BDRVBlkverifyState
*s
= bs
->opaque
;
285 bool perm
= bdrv_recurse_is_first_non_filter(bs
->file
->bs
, candidate
);
291 return bdrv_recurse_is_first_non_filter(s
->test_file
->bs
, candidate
);
294 /* Propagate AioContext changes to ->test_file */
295 static void blkverify_detach_aio_context(BlockDriverState
*bs
)
297 BDRVBlkverifyState
*s
= bs
->opaque
;
299 bdrv_detach_aio_context(s
->test_file
->bs
);
302 static void blkverify_attach_aio_context(BlockDriverState
*bs
,
303 AioContext
*new_context
)
305 BDRVBlkverifyState
*s
= bs
->opaque
;
307 bdrv_attach_aio_context(s
->test_file
->bs
, new_context
);
310 static void blkverify_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
312 BDRVBlkverifyState
*s
= bs
->opaque
;
314 /* bs->file->bs has already been refreshed */
315 bdrv_refresh_filename(s
->test_file
->bs
);
317 if (bs
->file
->bs
->full_open_options
318 && s
->test_file
->bs
->full_open_options
)
320 QDict
*opts
= qdict_new();
321 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("blkverify")));
323 QINCREF(bs
->file
->bs
->full_open_options
);
324 qdict_put_obj(opts
, "raw", QOBJECT(bs
->file
->bs
->full_open_options
));
325 QINCREF(s
->test_file
->bs
->full_open_options
);
326 qdict_put_obj(opts
, "test",
327 QOBJECT(s
->test_file
->bs
->full_open_options
));
329 bs
->full_open_options
= opts
;
332 if (bs
->file
->bs
->exact_filename
[0]
333 && s
->test_file
->bs
->exact_filename
[0])
335 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
337 bs
->file
->bs
->exact_filename
,
338 s
->test_file
->bs
->exact_filename
);
342 static BlockDriver bdrv_blkverify
= {
343 .format_name
= "blkverify",
344 .protocol_name
= "blkverify",
345 .instance_size
= sizeof(BDRVBlkverifyState
),
347 .bdrv_parse_filename
= blkverify_parse_filename
,
348 .bdrv_file_open
= blkverify_open
,
349 .bdrv_close
= blkverify_close
,
350 .bdrv_getlength
= blkverify_getlength
,
351 .bdrv_refresh_filename
= blkverify_refresh_filename
,
353 .bdrv_aio_readv
= blkverify_aio_readv
,
354 .bdrv_aio_writev
= blkverify_aio_writev
,
355 .bdrv_aio_flush
= blkverify_aio_flush
,
357 .bdrv_attach_aio_context
= blkverify_attach_aio_context
,
358 .bdrv_detach_aio_context
= blkverify_detach_aio_context
,
361 .bdrv_recurse_is_first_non_filter
= blkverify_recurse_is_first_non_filter
,
364 static void bdrv_blkverify_init(void)
366 bdrv_register(&bdrv_blkverify
);
369 block_init(bdrv_blkverify_init
);