2010-09-21 22:43:03 +08:00
|
|
|
/*
|
|
|
|
* Block protocol for block driver correctness testing
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 IBM, Corp.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-01-19 02:01:42 +08:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
|
2012-12-18 01:19:44 +08:00
|
|
|
#include "block/block_int.h"
|
2014-07-19 02:24:58 +08:00
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/qmp/qstring.h"
|
2016-03-21 01:16:19 +08:00
|
|
|
#include "qemu/cutils.h"
|
2019-05-23 22:35:07 +08:00
|
|
|
#include "qemu/module.h"
|
2018-02-01 19:18:46 +08:00
|
|
|
#include "qemu/option.h"
|
2010-09-21 22:43:03 +08:00
|
|
|
|
|
|
|
typedef struct {
|
2015-06-16 17:13:47 +08:00
|
|
|
BdrvChild *test_file;
|
2010-09-21 22:43:03 +08:00
|
|
|
} BDRVBlkverifyState;
|
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
typedef struct BlkverifyRequest {
|
|
|
|
Coroutine *co;
|
|
|
|
BlockDriverState *bs;
|
2010-09-21 22:43:03 +08:00
|
|
|
|
|
|
|
/* Request metadata */
|
|
|
|
bool is_write;
|
2016-11-05 04:13:45 +08:00
|
|
|
uint64_t offset;
|
|
|
|
uint64_t bytes;
|
|
|
|
int flags;
|
2010-09-21 22:43:03 +08:00
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
int (*request_fn)(BdrvChild *, int64_t, unsigned int, QEMUIOVector *,
|
|
|
|
BdrvRequestFlags);
|
2010-09-21 22:43:03 +08:00
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
int ret; /* test image result */
|
|
|
|
int raw_ret; /* raw image result */
|
2010-09-21 22:43:03 +08:00
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
unsigned int done; /* completion counter */
|
2010-09-21 22:43:03 +08:00
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
QEMUIOVector *qiov; /* user I/O vector */
|
|
|
|
QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */
|
|
|
|
} BlkverifyRequest;
|
2010-09-21 22:43:03 +08:00
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyRequest *r,
|
2010-09-25 03:02:05 +08:00
|
|
|
const char *fmt, ...)
|
2010-09-21 22:43:03 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2016-11-05 04:13:45 +08:00
|
|
|
fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ",
|
|
|
|
r->is_write ? "write" : "read", r->offset, r->bytes);
|
2010-09-21 22:43:03 +08:00
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
|
2013-04-10 20:40:28 +08:00
|
|
|
static void blkverify_parse_filename(const char *filename, QDict *options,
|
|
|
|
Error **errp)
|
2010-09-21 22:43:03 +08:00
|
|
|
{
|
2013-04-10 20:40:28 +08:00
|
|
|
const char *c;
|
|
|
|
QString *raw_path;
|
|
|
|
|
2010-09-21 22:43:03 +08:00
|
|
|
|
|
|
|
/* Parse the blkverify: prefix */
|
2013-04-10 20:40:28 +08:00
|
|
|
if (!strstart(filename, "blkverify:", &filename)) {
|
2013-12-21 02:28:17 +08:00
|
|
|
/* There was no prefix; therefore, all options have to be already
|
|
|
|
present in the QDict (except for the filename) */
|
2017-04-28 05:58:17 +08:00
|
|
|
qdict_put_str(options, "x-image", filename);
|
2013-04-10 20:40:28 +08:00
|
|
|
return;
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the raw image filename */
|
|
|
|
c = strchr(filename, ':');
|
|
|
|
if (c == NULL) {
|
2013-04-10 20:40:28 +08:00
|
|
|
error_setg(errp, "blkverify requires raw copy and original image path");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO Implement option pass-through and set raw.filename here */
|
2018-07-27 14:22:04 +08:00
|
|
|
raw_path = qstring_from_substr(filename, 0, c - filename);
|
2013-04-10 20:40:28 +08:00
|
|
|
qdict_put(options, "x-raw", raw_path);
|
|
|
|
|
|
|
|
/* TODO Allow multi-level nesting and set file.filename here */
|
|
|
|
filename = c + 1;
|
2017-04-28 05:58:17 +08:00
|
|
|
qdict_put_str(options, "x-image", filename);
|
2013-04-10 20:40:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static QemuOptsList runtime_opts = {
|
|
|
|
.name = "blkverify",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = "x-raw",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "[internal use only, will be removed]",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "x-image",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "[internal use only, will be removed]",
|
|
|
|
},
|
|
|
|
{ /* end of list */ }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-09-05 20:22:29 +08:00
|
|
|
static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2013-04-10 20:40:28 +08:00
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
QemuOpts *opts;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2014-01-02 10:49:17 +08:00
|
|
|
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
2013-04-10 20:40:28 +08:00
|
|
|
qemu_opts_absorb_qdict(opts, options, &local_err);
|
2014-01-30 22:07:28 +08:00
|
|
|
if (local_err) {
|
2013-10-10 21:44:04 +08:00
|
|
|
error_propagate(errp, local_err);
|
2013-04-10 20:40:28 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-12-21 02:28:16 +08:00
|
|
|
/* Open the raw file */
|
2015-06-16 20:19:22 +08:00
|
|
|
bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw",
|
2020-05-13 19:05:36 +08:00
|
|
|
bs, &child_of_bds,
|
|
|
|
BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
|
|
|
|
false, &local_err);
|
2015-06-16 20:19:22 +08:00
|
|
|
if (local_err) {
|
|
|
|
ret = -EINVAL;
|
2013-10-10 21:44:04 +08:00
|
|
|
error_propagate(errp, local_err);
|
2013-04-10 20:40:28 +08:00
|
|
|
goto fail;
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the test file */
|
2015-06-16 17:13:47 +08:00
|
|
|
s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
|
2020-05-13 19:05:31 +08:00
|
|
|
"test", bs, &child_of_bds, BDRV_CHILD_DATA,
|
|
|
|
false, &local_err);
|
2015-06-16 17:13:47 +08:00
|
|
|
if (local_err) {
|
|
|
|
ret = -EINVAL;
|
2013-10-10 21:44:04 +08:00
|
|
|
error_propagate(errp, local_err);
|
2013-04-10 20:40:28 +08:00
|
|
|
goto fail;
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2018-04-21 21:29:26 +08:00
|
|
|
bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
|
|
|
|
bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
|
|
|
|
|
2013-04-10 20:40:28 +08:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
2014-08-28 13:56:11 +08:00
|
|
|
qemu_opts_del(opts);
|
2013-04-10 20:40:28 +08:00
|
|
|
return ret;
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void blkverify_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 17:13:47 +08:00
|
|
|
bdrv_unref_child(bs, s->test_file);
|
2010-09-21 22:43:03 +08:00
|
|
|
s->test_file = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t blkverify_getlength(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 17:13:47 +08:00
|
|
|
return bdrv_getlength(s->test_file->bs);
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
static void coroutine_fn blkverify_do_test_req(void *opaque)
|
2010-09-21 22:43:03 +08:00
|
|
|
{
|
2016-11-05 04:13:45 +08:00
|
|
|
BlkverifyRequest *r = opaque;
|
|
|
|
BDRVBlkverifyState *s = r->bs->opaque;
|
|
|
|
|
|
|
|
r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
|
|
|
|
r->flags);
|
|
|
|
r->done++;
|
|
|
|
qemu_coroutine_enter_if_inactive(r->co);
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
static void coroutine_fn blkverify_do_raw_req(void *opaque)
|
2010-09-21 22:43:03 +08:00
|
|
|
{
|
2016-11-05 04:13:45 +08:00
|
|
|
BlkverifyRequest *r = opaque;
|
2010-09-21 22:43:03 +08:00
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
|
|
|
|
r->flags);
|
|
|
|
r->done++;
|
|
|
|
qemu_coroutine_enter_if_inactive(r->co);
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
static int coroutine_fn
|
|
|
|
blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
|
|
|
|
uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
|
|
|
|
int flags, bool is_write)
|
2010-09-21 22:43:03 +08:00
|
|
|
{
|
2016-11-05 04:13:45 +08:00
|
|
|
Coroutine *co_a, *co_b;
|
|
|
|
|
|
|
|
*r = (BlkverifyRequest) {
|
|
|
|
.co = qemu_coroutine_self(),
|
|
|
|
.bs = bs,
|
|
|
|
.offset = offset,
|
|
|
|
.bytes = bytes,
|
|
|
|
.qiov = qiov,
|
|
|
|
.raw_qiov = raw_qiov,
|
|
|
|
.flags = flags,
|
|
|
|
.is_write = is_write,
|
|
|
|
.request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
|
|
|
|
};
|
|
|
|
|
|
|
|
co_a = qemu_coroutine_create(blkverify_do_test_req, r);
|
|
|
|
co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
|
|
|
|
|
|
|
|
qemu_coroutine_enter(co_a);
|
|
|
|
qemu_coroutine_enter(co_b);
|
|
|
|
|
|
|
|
while (r->done < 2) {
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
}
|
2010-09-21 22:43:03 +08:00
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
if (r->ret != r->raw_ret) {
|
|
|
|
blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
2016-11-05 04:13:45 +08:00
|
|
|
|
|
|
|
return r->ret;
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
static int coroutine_fn
|
|
|
|
blkverify_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|
|
|
QEMUIOVector *qiov, int flags)
|
2010-09-21 22:43:03 +08:00
|
|
|
{
|
2016-11-05 04:13:45 +08:00
|
|
|
BlkverifyRequest r;
|
|
|
|
QEMUIOVector raw_qiov;
|
|
|
|
void *buf;
|
|
|
|
ssize_t cmp_offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
buf = qemu_blockalign(bs->file->bs, qiov->size);
|
|
|
|
qemu_iovec_init(&raw_qiov, qiov->niov);
|
|
|
|
qemu_iovec_clone(&raw_qiov, qiov, buf);
|
|
|
|
|
|
|
|
ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, flags,
|
|
|
|
false);
|
|
|
|
|
|
|
|
cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
|
|
|
|
if (cmp_offset != -1) {
|
|
|
|
blkverify_err(&r, "contents mismatch at offset %" PRId64,
|
|
|
|
offset + cmp_offset);
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
qemu_iovec_destroy(&raw_qiov);
|
|
|
|
qemu_vfree(buf);
|
|
|
|
|
|
|
|
return ret;
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
static int coroutine_fn
|
|
|
|
blkverify_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|
|
|
QEMUIOVector *qiov, int flags)
|
2010-09-21 22:43:03 +08:00
|
|
|
{
|
2016-11-05 04:13:45 +08:00
|
|
|
BlkverifyRequest r;
|
|
|
|
return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
static int blkverify_co_flush(BlockDriverState *bs)
|
2010-09-21 22:43:03 +08:00
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
|
|
|
/* Only flush test file, the raw file is not important */
|
2016-11-05 04:13:45 +08:00
|
|
|
return bdrv_co_flush(s->test_file->bs);
|
2010-09-21 22:43:03 +08:00
|
|
|
}
|
|
|
|
|
2020-02-18 18:34:42 +08:00
|
|
|
static bool blkverify_recurse_can_replace(BlockDriverState *bs,
|
|
|
|
BlockDriverState *to_replace)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* blkverify quits the whole qemu process if there is a mismatch
|
|
|
|
* between bs->file->bs and s->test_file->bs. Therefore, we know
|
|
|
|
* know that both must match bs and we can recurse down to either.
|
|
|
|
*/
|
|
|
|
return bdrv_recurse_can_replace(bs->file->bs, to_replace) ||
|
|
|
|
bdrv_recurse_can_replace(s->test_file->bs, to_replace);
|
|
|
|
}
|
|
|
|
|
2019-02-02 03:29:28 +08:00
|
|
|
static void blkverify_refresh_filename(BlockDriverState *bs)
|
2014-07-19 02:24:58 +08:00
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 20:19:22 +08:00
|
|
|
if (bs->file->bs->exact_filename[0]
|
|
|
|
&& s->test_file->bs->exact_filename[0])
|
|
|
|
{
|
2017-06-14 01:20:06 +08:00
|
|
|
int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
|
|
"blkverify:%s:%s",
|
|
|
|
bs->file->bs->exact_filename,
|
|
|
|
s->test_file->bs->exact_filename);
|
|
|
|
if (ret >= sizeof(bs->exact_filename)) {
|
|
|
|
/* An overflow makes the filename unusable, so do not report any */
|
|
|
|
bs->exact_filename[0] = 0;
|
|
|
|
}
|
2014-07-19 02:24:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 03:29:19 +08:00
|
|
|
static char *blkverify_dirname(BlockDriverState *bs, Error **errp)
|
|
|
|
{
|
|
|
|
/* In general, there are two BDSs with different dirnames below this one;
|
|
|
|
* so there is no unique dirname we could return (unless both are equal by
|
|
|
|
* chance). Therefore, to be consistent, just always return NULL. */
|
|
|
|
error_setg(errp, "Cannot generate a base directory for blkverify nodes");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-21 22:43:03 +08:00
|
|
|
static BlockDriver bdrv_blkverify = {
|
2014-05-08 22:34:39 +08:00
|
|
|
.format_name = "blkverify",
|
|
|
|
.protocol_name = "blkverify",
|
|
|
|
.instance_size = sizeof(BDRVBlkverifyState),
|
|
|
|
|
|
|
|
.bdrv_parse_filename = blkverify_parse_filename,
|
|
|
|
.bdrv_file_open = blkverify_open,
|
|
|
|
.bdrv_close = blkverify_close,
|
2020-05-13 19:05:39 +08:00
|
|
|
.bdrv_child_perm = bdrv_default_perms,
|
2014-05-08 22:34:39 +08:00
|
|
|
.bdrv_getlength = blkverify_getlength,
|
2014-07-19 02:24:58 +08:00
|
|
|
.bdrv_refresh_filename = blkverify_refresh_filename,
|
2019-02-02 03:29:19 +08:00
|
|
|
.bdrv_dirname = blkverify_dirname,
|
2013-04-10 20:40:28 +08:00
|
|
|
|
2016-11-05 04:13:45 +08:00
|
|
|
.bdrv_co_preadv = blkverify_co_preadv,
|
|
|
|
.bdrv_co_pwritev = blkverify_co_pwritev,
|
|
|
|
.bdrv_co_flush = blkverify_co_flush,
|
2013-04-10 20:40:28 +08:00
|
|
|
|
2014-05-08 22:34:39 +08:00
|
|
|
.is_filter = true,
|
2020-02-18 18:34:42 +08:00
|
|
|
.bdrv_recurse_can_replace = blkverify_recurse_can_replace,
|
2010-09-21 22:43:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_blkverify_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_blkverify);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_blkverify_init);
|