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 "qapi/error.h"
12 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */
13 #include "block/block_int.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qstring.h"
16 #include "qemu/cutils.h"
22 typedef struct BlkverifyAIOCB BlkverifyAIOCB
;
23 struct BlkverifyAIOCB
{
27 /* Request metadata */
32 int ret
; /* first completed request's result */
33 unsigned int done
; /* completion counter */
35 QEMUIOVector
*qiov
; /* user I/O vector */
36 QEMUIOVector raw_qiov
; /* cloned I/O vector for raw file */
37 void *buf
; /* buffer for raw file I/O */
39 void (*verify
)(BlkverifyAIOCB
*acb
);
42 static const AIOCBInfo blkverify_aiocb_info
= {
43 .aiocb_size
= sizeof(BlkverifyAIOCB
),
46 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB
*acb
,
52 fprintf(stderr
, "blkverify: %s sector_num=%" PRId64
" nb_sectors=%d ",
53 acb
->is_write
? "write" : "read", acb
->sector_num
,
55 vfprintf(stderr
, fmt
, ap
);
56 fprintf(stderr
, "\n");
61 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
62 static void blkverify_parse_filename(const char *filename
, QDict
*options
,
69 /* Parse the blkverify: prefix */
70 if (!strstart(filename
, "blkverify:", &filename
)) {
71 /* There was no prefix; therefore, all options have to be already
72 present in the QDict (except for the filename) */
73 qdict_put(options
, "x-image", qstring_from_str(filename
));
77 /* Parse the raw image filename */
78 c
= strchr(filename
, ':');
80 error_setg(errp
, "blkverify requires raw copy and original image path");
84 /* TODO Implement option pass-through and set raw.filename here */
85 raw_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
86 qdict_put(options
, "x-raw", raw_path
);
88 /* TODO Allow multi-level nesting and set file.filename here */
90 qdict_put(options
, "x-image", qstring_from_str(filename
));
93 static QemuOptsList runtime_opts
= {
95 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
99 .type
= QEMU_OPT_STRING
,
100 .help
= "[internal use only, will be removed]",
104 .type
= QEMU_OPT_STRING
,
105 .help
= "[internal use only, will be removed]",
107 { /* end of list */ }
111 static int blkverify_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
114 BDRVBlkverifyState
*s
= bs
->opaque
;
116 Error
*local_err
= NULL
;
119 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
120 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
122 error_propagate(errp
, local_err
);
127 /* Open the raw file */
128 bs
->file
= bdrv_open_child(qemu_opt_get(opts
, "x-raw"), options
, "raw",
129 bs
, &child_file
, false, &local_err
);
132 error_propagate(errp
, local_err
);
136 /* Open the test file */
137 s
->test_file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
,
138 "test", bs
, &child_format
, false,
142 error_propagate(errp
, local_err
);
149 bdrv_unref_child(bs
, bs
->file
);
155 static void blkverify_close(BlockDriverState
*bs
)
157 BDRVBlkverifyState
*s
= bs
->opaque
;
159 bdrv_unref_child(bs
, s
->test_file
);
163 static int64_t blkverify_getlength(BlockDriverState
*bs
)
165 BDRVBlkverifyState
*s
= bs
->opaque
;
167 return bdrv_getlength(s
->test_file
->bs
);
170 static BlkverifyAIOCB
*blkverify_aio_get(BlockDriverState
*bs
, bool is_write
,
171 int64_t sector_num
, QEMUIOVector
*qiov
,
173 BlockCompletionFunc
*cb
,
176 BlkverifyAIOCB
*acb
= qemu_aio_get(&blkverify_aiocb_info
, bs
, cb
, opaque
);
179 acb
->is_write
= is_write
;
180 acb
->sector_num
= sector_num
;
181 acb
->nb_sectors
= nb_sectors
;
182 acb
->ret
= -EINPROGRESS
;
190 static void blkverify_aio_bh(void *opaque
)
192 BlkverifyAIOCB
*acb
= opaque
;
194 qemu_bh_delete(acb
->bh
);
196 qemu_iovec_destroy(&acb
->raw_qiov
);
197 qemu_vfree(acb
->buf
);
199 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
203 static void blkverify_aio_cb(void *opaque
, int ret
)
205 BlkverifyAIOCB
*acb
= opaque
;
207 switch (++acb
->done
) {
213 if (acb
->ret
!= ret
) {
214 blkverify_err(acb
, "return value mismatch %d != %d", acb
->ret
, ret
);
221 acb
->bh
= aio_bh_new(bdrv_get_aio_context(acb
->common
.bs
),
222 blkverify_aio_bh
, acb
);
223 qemu_bh_schedule(acb
->bh
);
228 static void blkverify_verify_readv(BlkverifyAIOCB
*acb
)
230 ssize_t offset
= qemu_iovec_compare(acb
->qiov
, &acb
->raw_qiov
);
232 blkverify_err(acb
, "contents mismatch in sector %" PRId64
,
233 acb
->sector_num
+ (int64_t)(offset
/ BDRV_SECTOR_SIZE
));
237 static BlockAIOCB
*blkverify_aio_readv(BlockDriverState
*bs
,
238 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
239 BlockCompletionFunc
*cb
, void *opaque
)
241 BDRVBlkverifyState
*s
= bs
->opaque
;
242 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, false, sector_num
, qiov
,
243 nb_sectors
, cb
, opaque
);
245 acb
->verify
= blkverify_verify_readv
;
246 acb
->buf
= qemu_blockalign(bs
->file
->bs
, qiov
->size
);
247 qemu_iovec_init(&acb
->raw_qiov
, acb
->qiov
->niov
);
248 qemu_iovec_clone(&acb
->raw_qiov
, qiov
, acb
->buf
);
250 bdrv_aio_readv(s
->test_file
->bs
, sector_num
, qiov
, nb_sectors
,
251 blkverify_aio_cb
, acb
);
252 bdrv_aio_readv(bs
->file
->bs
, sector_num
, &acb
->raw_qiov
, nb_sectors
,
253 blkverify_aio_cb
, acb
);
257 static BlockAIOCB
*blkverify_aio_writev(BlockDriverState
*bs
,
258 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
259 BlockCompletionFunc
*cb
, void *opaque
)
261 BDRVBlkverifyState
*s
= bs
->opaque
;
262 BlkverifyAIOCB
*acb
= blkverify_aio_get(bs
, true, sector_num
, qiov
,
263 nb_sectors
, cb
, opaque
);
265 bdrv_aio_writev(s
->test_file
->bs
, sector_num
, qiov
, nb_sectors
,
266 blkverify_aio_cb
, acb
);
267 bdrv_aio_writev(bs
->file
->bs
, sector_num
, qiov
, nb_sectors
,
268 blkverify_aio_cb
, acb
);
272 static BlockAIOCB
*blkverify_aio_flush(BlockDriverState
*bs
,
273 BlockCompletionFunc
*cb
,
276 BDRVBlkverifyState
*s
= bs
->opaque
;
278 /* Only flush test file, the raw file is not important */
279 return bdrv_aio_flush(s
->test_file
->bs
, cb
, opaque
);
282 static bool blkverify_recurse_is_first_non_filter(BlockDriverState
*bs
,
283 BlockDriverState
*candidate
)
285 BDRVBlkverifyState
*s
= bs
->opaque
;
287 bool perm
= bdrv_recurse_is_first_non_filter(bs
->file
->bs
, candidate
);
293 return bdrv_recurse_is_first_non_filter(s
->test_file
->bs
, candidate
);
296 static void blkverify_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
298 BDRVBlkverifyState
*s
= bs
->opaque
;
300 /* bs->file->bs has already been refreshed */
301 bdrv_refresh_filename(s
->test_file
->bs
);
303 if (bs
->file
->bs
->full_open_options
304 && s
->test_file
->bs
->full_open_options
)
306 QDict
*opts
= qdict_new();
307 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("blkverify")));
309 QINCREF(bs
->file
->bs
->full_open_options
);
310 qdict_put_obj(opts
, "raw", QOBJECT(bs
->file
->bs
->full_open_options
));
311 QINCREF(s
->test_file
->bs
->full_open_options
);
312 qdict_put_obj(opts
, "test",
313 QOBJECT(s
->test_file
->bs
->full_open_options
));
315 bs
->full_open_options
= opts
;
318 if (bs
->file
->bs
->exact_filename
[0]
319 && s
->test_file
->bs
->exact_filename
[0])
321 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
323 bs
->file
->bs
->exact_filename
,
324 s
->test_file
->bs
->exact_filename
);
328 static BlockDriver bdrv_blkverify
= {
329 .format_name
= "blkverify",
330 .protocol_name
= "blkverify",
331 .instance_size
= sizeof(BDRVBlkverifyState
),
333 .bdrv_parse_filename
= blkverify_parse_filename
,
334 .bdrv_file_open
= blkverify_open
,
335 .bdrv_close
= blkverify_close
,
336 .bdrv_getlength
= blkverify_getlength
,
337 .bdrv_refresh_filename
= blkverify_refresh_filename
,
339 .bdrv_aio_readv
= blkverify_aio_readv
,
340 .bdrv_aio_writev
= blkverify_aio_writev
,
341 .bdrv_aio_flush
= blkverify_aio_flush
,
344 .bdrv_recurse_is_first_non_filter
= blkverify_recurse_is_first_non_filter
,
347 static void bdrv_blkverify_init(void)
349 bdrv_register(&bdrv_blkverify
);
352 block_init(bdrv_blkverify_init
);