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-io.h"
14 #include "block/block_int.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qapi/qmp/qstring.h"
17 #include "qemu/cutils.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/memalign.h"
26 typedef struct BlkverifyRequest
{
30 /* Request metadata */
36 int (*request_fn
)(BdrvChild
*, int64_t, int64_t, QEMUIOVector
*,
39 int ret
; /* test image result */
40 int raw_ret
; /* raw image result */
42 unsigned int done
; /* completion counter */
44 QEMUIOVector
*qiov
; /* user I/O vector */
45 QEMUIOVector
*raw_qiov
; /* cloned I/O vector for raw file */
48 static void G_GNUC_PRINTF(2, 3) blkverify_err(BlkverifyRequest
*r
,
54 fprintf(stderr
, "blkverify: %s offset=%" PRId64
" bytes=%" PRId64
" ",
55 r
->is_write
? "write" : "read", r
->offset
, r
->bytes
);
56 vfprintf(stderr
, fmt
, ap
);
57 fprintf(stderr
, "\n");
62 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
63 static void blkverify_parse_filename(const char *filename
, QDict
*options
,
70 /* Parse the blkverify: prefix */
71 if (!strstart(filename
, "blkverify:", &filename
)) {
72 /* There was no prefix; therefore, all options have to be already
73 present in the QDict (except for the filename) */
74 qdict_put_str(options
, "x-image", filename
);
78 /* Parse the raw image filename */
79 c
= strchr(filename
, ':');
81 error_setg(errp
, "blkverify requires raw copy and original image path");
85 /* TODO Implement option pass-through and set raw.filename here */
86 raw_path
= qstring_from_substr(filename
, 0, c
- filename
);
87 qdict_put(options
, "x-raw", raw_path
);
89 /* TODO Allow multi-level nesting and set file.filename here */
91 qdict_put_str(options
, "x-image", filename
);
94 static QemuOptsList runtime_opts
= {
96 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
100 .type
= QEMU_OPT_STRING
,
101 .help
= "[internal use only, will be removed]",
105 .type
= QEMU_OPT_STRING
,
106 .help
= "[internal use only, will be removed]",
108 { /* end of list */ }
112 static int blkverify_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
115 BDRVBlkverifyState
*s
= bs
->opaque
;
119 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
120 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
125 /* Open the raw file */
126 ret
= bdrv_open_file_child(qemu_opt_get(opts
, "x-raw"), options
, "raw",
132 /* Open the test file */
133 s
->test_file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
,
134 "test", bs
, &child_of_bds
, BDRV_CHILD_DATA
,
141 bs
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
;
142 bs
->supported_zero_flags
= BDRV_REQ_WRITE_UNCHANGED
;
150 static void blkverify_close(BlockDriverState
*bs
)
152 BDRVBlkverifyState
*s
= bs
->opaque
;
154 bdrv_unref_child(bs
, s
->test_file
);
158 static int64_t coroutine_fn GRAPH_RDLOCK
159 blkverify_co_getlength(BlockDriverState
*bs
)
161 BDRVBlkverifyState
*s
= bs
->opaque
;
163 return bdrv_co_getlength(s
->test_file
->bs
);
166 static void coroutine_fn
blkverify_do_test_req(void *opaque
)
168 BlkverifyRequest
*r
= opaque
;
169 BDRVBlkverifyState
*s
= r
->bs
->opaque
;
171 r
->ret
= r
->request_fn(s
->test_file
, r
->offset
, r
->bytes
, r
->qiov
,
174 qemu_coroutine_enter_if_inactive(r
->co
);
177 static void coroutine_fn
blkverify_do_raw_req(void *opaque
)
179 BlkverifyRequest
*r
= opaque
;
181 r
->raw_ret
= r
->request_fn(r
->bs
->file
, r
->offset
, r
->bytes
, r
->raw_qiov
,
184 qemu_coroutine_enter_if_inactive(r
->co
);
187 static int coroutine_fn
188 blkverify_co_prwv(BlockDriverState
*bs
, BlkverifyRequest
*r
, uint64_t offset
,
189 uint64_t bytes
, QEMUIOVector
*qiov
, QEMUIOVector
*raw_qiov
,
190 int flags
, bool is_write
)
192 Coroutine
*co_a
, *co_b
;
194 *r
= (BlkverifyRequest
) {
195 .co
= qemu_coroutine_self(),
200 .raw_qiov
= raw_qiov
,
202 .is_write
= is_write
,
203 .request_fn
= is_write
? bdrv_co_pwritev
: bdrv_co_preadv
,
206 co_a
= qemu_coroutine_create(blkverify_do_test_req
, r
);
207 co_b
= qemu_coroutine_create(blkverify_do_raw_req
, r
);
209 qemu_coroutine_enter(co_a
);
210 qemu_coroutine_enter(co_b
);
212 while (r
->done
< 2) {
213 qemu_coroutine_yield();
216 if (r
->ret
!= r
->raw_ret
) {
217 blkverify_err(r
, "return value mismatch %d != %d", r
->ret
, r
->raw_ret
);
223 static int coroutine_fn
224 blkverify_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
225 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
228 QEMUIOVector raw_qiov
;
233 buf
= qemu_blockalign(bs
->file
->bs
, qiov
->size
);
234 qemu_iovec_init(&raw_qiov
, qiov
->niov
);
235 qemu_iovec_clone(&raw_qiov
, qiov
, buf
);
237 ret
= blkverify_co_prwv(bs
, &r
, offset
, bytes
, qiov
, &raw_qiov
,
238 flags
& ~BDRV_REQ_REGISTERED_BUF
, false);
240 cmp_offset
= qemu_iovec_compare(qiov
, &raw_qiov
);
241 if (cmp_offset
!= -1) {
242 blkverify_err(&r
, "contents mismatch at offset %" PRId64
,
243 offset
+ cmp_offset
);
246 qemu_iovec_destroy(&raw_qiov
);
252 static int coroutine_fn
253 blkverify_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
254 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
257 return blkverify_co_prwv(bs
, &r
, offset
, bytes
, qiov
, qiov
, flags
, true);
260 static int coroutine_fn GRAPH_RDLOCK
blkverify_co_flush(BlockDriverState
*bs
)
262 BDRVBlkverifyState
*s
= bs
->opaque
;
264 /* Only flush test file, the raw file is not important */
265 return bdrv_co_flush(s
->test_file
->bs
);
268 static bool GRAPH_RDLOCK
269 blkverify_recurse_can_replace(BlockDriverState
*bs
,
270 BlockDriverState
*to_replace
)
272 BDRVBlkverifyState
*s
= bs
->opaque
;
275 * blkverify quits the whole qemu process if there is a mismatch
276 * between bs->file->bs and s->test_file->bs. Therefore, we know
277 * know that both must match bs and we can recurse down to either.
279 return bdrv_recurse_can_replace(bs
->file
->bs
, to_replace
) ||
280 bdrv_recurse_can_replace(s
->test_file
->bs
, to_replace
);
283 static void blkverify_refresh_filename(BlockDriverState
*bs
)
285 BDRVBlkverifyState
*s
= bs
->opaque
;
287 if (bs
->file
->bs
->exact_filename
[0]
288 && s
->test_file
->bs
->exact_filename
[0])
290 int ret
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
292 bs
->file
->bs
->exact_filename
,
293 s
->test_file
->bs
->exact_filename
);
294 if (ret
>= sizeof(bs
->exact_filename
)) {
295 /* An overflow makes the filename unusable, so do not report any */
296 bs
->exact_filename
[0] = 0;
301 static char *blkverify_dirname(BlockDriverState
*bs
, Error
**errp
)
303 /* In general, there are two BDSs with different dirnames below this one;
304 * so there is no unique dirname we could return (unless both are equal by
305 * chance). Therefore, to be consistent, just always return NULL. */
306 error_setg(errp
, "Cannot generate a base directory for blkverify nodes");
310 static BlockDriver bdrv_blkverify
= {
311 .format_name
= "blkverify",
312 .protocol_name
= "blkverify",
313 .instance_size
= sizeof(BDRVBlkverifyState
),
315 .bdrv_parse_filename
= blkverify_parse_filename
,
316 .bdrv_file_open
= blkverify_open
,
317 .bdrv_close
= blkverify_close
,
318 .bdrv_child_perm
= bdrv_default_perms
,
319 .bdrv_co_getlength
= blkverify_co_getlength
,
320 .bdrv_refresh_filename
= blkverify_refresh_filename
,
321 .bdrv_dirname
= blkverify_dirname
,
323 .bdrv_co_preadv
= blkverify_co_preadv
,
324 .bdrv_co_pwritev
= blkverify_co_pwritev
,
325 .bdrv_co_flush
= blkverify_co_flush
,
328 .bdrv_recurse_can_replace
= blkverify_recurse_can_replace
,
331 static void bdrv_blkverify_init(void)
333 bdrv_register(&bdrv_blkverify
);
336 block_init(bdrv_blkverify_init
);