2010-09-21 18:43:03 +04: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block_int.h"
|
2014-07-18 22:24:58 +04:00
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/qmp/qstring.h"
|
2010-09-21 18:43:03 +04:00
|
|
|
|
|
|
|
typedef struct {
|
2015-06-16 12:13:47 +03:00
|
|
|
BdrvChild *test_file;
|
2010-09-21 18:43:03 +04:00
|
|
|
} BDRVBlkverifyState;
|
|
|
|
|
|
|
|
typedef struct BlkverifyAIOCB BlkverifyAIOCB;
|
|
|
|
struct BlkverifyAIOCB {
|
2014-10-07 15:59:14 +04:00
|
|
|
BlockAIOCB common;
|
2010-09-21 18:43:03 +04:00
|
|
|
QEMUBH *bh;
|
|
|
|
|
|
|
|
/* Request metadata */
|
|
|
|
bool is_write;
|
|
|
|
int64_t sector_num;
|
|
|
|
int nb_sectors;
|
|
|
|
|
|
|
|
int ret; /* first completed request's result */
|
|
|
|
unsigned int done; /* completion counter */
|
|
|
|
|
|
|
|
QEMUIOVector *qiov; /* user I/O vector */
|
|
|
|
QEMUIOVector raw_qiov; /* cloned I/O vector for raw file */
|
|
|
|
void *buf; /* buffer for raw file I/O */
|
|
|
|
|
|
|
|
void (*verify)(BlkverifyAIOCB *acb);
|
|
|
|
};
|
|
|
|
|
2012-10-31 19:34:37 +04:00
|
|
|
static const AIOCBInfo blkverify_aiocb_info = {
|
2010-09-21 18:43:03 +04:00
|
|
|
.aiocb_size = sizeof(BlkverifyAIOCB),
|
|
|
|
};
|
|
|
|
|
2010-09-24 23:02:05 +04:00
|
|
|
static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
|
|
|
|
const char *fmt, ...)
|
2010-09-21 18:43:03 +04:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2010-09-22 23:46:33 +04:00
|
|
|
fprintf(stderr, "blkverify: %s sector_num=%" PRId64 " nb_sectors=%d ",
|
2010-09-21 18:43:03 +04:00
|
|
|
acb->is_write ? "write" : "read", acb->sector_num,
|
|
|
|
acb->nb_sectors);
|
|
|
|
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 16:40:28 +04:00
|
|
|
static void blkverify_parse_filename(const char *filename, QDict *options,
|
|
|
|
Error **errp)
|
2010-09-21 18:43:03 +04:00
|
|
|
{
|
2013-04-10 16:40:28 +04:00
|
|
|
const char *c;
|
|
|
|
QString *raw_path;
|
|
|
|
|
2010-09-21 18:43:03 +04:00
|
|
|
|
|
|
|
/* Parse the blkverify: prefix */
|
2013-04-10 16:40:28 +04:00
|
|
|
if (!strstart(filename, "blkverify:", &filename)) {
|
2013-12-20 22:28:17 +04:00
|
|
|
/* There was no prefix; therefore, all options have to be already
|
|
|
|
present in the QDict (except for the filename) */
|
|
|
|
qdict_put(options, "x-image", qstring_from_str(filename));
|
2013-04-10 16:40:28 +04:00
|
|
|
return;
|
2010-09-21 18:43:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the raw image filename */
|
|
|
|
c = strchr(filename, ':');
|
|
|
|
if (c == NULL) {
|
2013-04-10 16:40:28 +04:00
|
|
|
error_setg(errp, "blkverify requires raw copy and original image path");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO Implement option pass-through and set raw.filename here */
|
|
|
|
raw_path = qstring_from_substr(filename, 0, c - filename - 1);
|
|
|
|
qdict_put(options, "x-raw", raw_path);
|
|
|
|
|
|
|
|
/* TODO Allow multi-level nesting and set file.filename here */
|
|
|
|
filename = c + 1;
|
|
|
|
qdict_put(options, "x-image", qstring_from_str(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:22:29 +04:00
|
|
|
static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2013-04-10 16:40:28 +04:00
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
QemuOpts *opts;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2014-01-02 06:49:17 +04:00
|
|
|
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
2013-04-10 16:40:28 +04:00
|
|
|
qemu_opts_absorb_qdict(opts, options, &local_err);
|
2014-01-30 18:07:28 +04:00
|
|
|
if (local_err) {
|
2013-10-10 17:44:04 +04:00
|
|
|
error_propagate(errp, local_err);
|
2013-04-10 16:40:28 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-12-20 22:28:16 +04:00
|
|
|
/* Open the raw file */
|
2015-06-16 15:19:22 +03:00
|
|
|
bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw",
|
|
|
|
bs, &child_file, false, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
ret = -EINVAL;
|
2013-10-10 17:44:04 +04:00
|
|
|
error_propagate(errp, local_err);
|
2013-04-10 16:40:28 +04:00
|
|
|
goto fail;
|
2010-09-21 18:43:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the test file */
|
2015-06-16 12:13:47 +03:00
|
|
|
s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
|
|
|
|
"test", bs, &child_format, false,
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
ret = -EINVAL;
|
2013-10-10 17:44:04 +04:00
|
|
|
error_propagate(errp, local_err);
|
2013-04-10 16:40:28 +04:00
|
|
|
goto fail;
|
2010-09-21 18:43:03 +04:00
|
|
|
}
|
|
|
|
|
2013-04-10 16:40:28 +04:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
2015-10-13 15:15:53 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
bdrv_unref_child(bs, bs->file);
|
|
|
|
}
|
2014-08-28 09:56:11 +04:00
|
|
|
qemu_opts_del(opts);
|
2013-04-10 16:40:28 +04:00
|
|
|
return ret;
|
2010-09-21 18:43:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void blkverify_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 12:13:47 +03:00
|
|
|
bdrv_unref_child(bs, s->test_file);
|
2010-09-21 18:43:03 +04:00
|
|
|
s->test_file = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t blkverify_getlength(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 12:13:47 +03:00
|
|
|
return bdrv_getlength(s->test_file->bs);
|
2010-09-21 18:43:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
|
|
|
|
int64_t sector_num, QEMUIOVector *qiov,
|
|
|
|
int nb_sectors,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb,
|
2010-09-21 18:43:03 +04:00
|
|
|
void *opaque)
|
|
|
|
{
|
2012-10-31 19:34:37 +04:00
|
|
|
BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
|
2010-09-21 18:43:03 +04:00
|
|
|
|
|
|
|
acb->bh = NULL;
|
|
|
|
acb->is_write = is_write;
|
|
|
|
acb->sector_num = sector_num;
|
|
|
|
acb->nb_sectors = nb_sectors;
|
|
|
|
acb->ret = -EINPROGRESS;
|
|
|
|
acb->done = 0;
|
|
|
|
acb->qiov = qiov;
|
|
|
|
acb->buf = NULL;
|
|
|
|
acb->verify = NULL;
|
|
|
|
return acb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blkverify_aio_bh(void *opaque)
|
|
|
|
{
|
|
|
|
BlkverifyAIOCB *acb = opaque;
|
|
|
|
|
|
|
|
qemu_bh_delete(acb->bh);
|
|
|
|
if (acb->buf) {
|
|
|
|
qemu_iovec_destroy(&acb->raw_qiov);
|
|
|
|
qemu_vfree(acb->buf);
|
|
|
|
}
|
|
|
|
acb->common.cb(acb->common.opaque, acb->ret);
|
2014-09-11 09:41:28 +04:00
|
|
|
qemu_aio_unref(acb);
|
2010-09-21 18:43:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void blkverify_aio_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
BlkverifyAIOCB *acb = opaque;
|
|
|
|
|
|
|
|
switch (++acb->done) {
|
|
|
|
case 1:
|
|
|
|
acb->ret = ret;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if (acb->ret != ret) {
|
|
|
|
blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acb->verify) {
|
|
|
|
acb->verify(acb);
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:34:39 +04:00
|
|
|
acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
|
|
|
|
blkverify_aio_bh, acb);
|
2010-09-21 18:43:03 +04:00
|
|
|
qemu_bh_schedule(acb->bh);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blkverify_verify_readv(BlkverifyAIOCB *acb)
|
|
|
|
{
|
2014-02-22 01:21:13 +04:00
|
|
|
ssize_t offset = qemu_iovec_compare(acb->qiov, &acb->raw_qiov);
|
2010-09-21 18:43:03 +04:00
|
|
|
if (offset != -1) {
|
2010-10-30 20:46:27 +04:00
|
|
|
blkverify_err(acb, "contents mismatch in sector %" PRId64,
|
|
|
|
acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
|
2010-09-21 18:43:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:14 +04:00
|
|
|
static BlockAIOCB *blkverify_aio_readv(BlockDriverState *bs,
|
2010-09-21 18:43:03 +04:00
|
|
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb, void *opaque)
|
2010-09-21 18:43:03 +04:00
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
|
|
|
|
nb_sectors, cb, opaque);
|
|
|
|
|
|
|
|
acb->verify = blkverify_verify_readv;
|
2015-06-16 15:19:22 +03:00
|
|
|
acb->buf = qemu_blockalign(bs->file->bs, qiov->size);
|
2010-09-21 18:43:03 +04:00
|
|
|
qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
|
2014-02-22 01:21:13 +04:00
|
|
|
qemu_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
|
2010-09-21 18:43:03 +04:00
|
|
|
|
2015-06-16 12:13:47 +03:00
|
|
|
bdrv_aio_readv(s->test_file->bs, sector_num, qiov, nb_sectors,
|
2011-11-30 12:12:30 +04:00
|
|
|
blkverify_aio_cb, acb);
|
2015-06-16 15:19:22 +03:00
|
|
|
bdrv_aio_readv(bs->file->bs, sector_num, &acb->raw_qiov, nb_sectors,
|
2011-11-30 12:12:30 +04:00
|
|
|
blkverify_aio_cb, acb);
|
2010-09-21 18:43:03 +04:00
|
|
|
return &acb->common;
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:14 +04:00
|
|
|
static BlockAIOCB *blkverify_aio_writev(BlockDriverState *bs,
|
2010-09-21 18:43:03 +04:00
|
|
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb, void *opaque)
|
2010-09-21 18:43:03 +04:00
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
|
|
|
|
nb_sectors, cb, opaque);
|
|
|
|
|
2015-06-16 12:13:47 +03:00
|
|
|
bdrv_aio_writev(s->test_file->bs, sector_num, qiov, nb_sectors,
|
2011-11-30 12:12:30 +04:00
|
|
|
blkverify_aio_cb, acb);
|
2015-06-16 15:19:22 +03:00
|
|
|
bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors,
|
2011-11-30 12:12:30 +04:00
|
|
|
blkverify_aio_cb, acb);
|
2010-09-21 18:43:03 +04:00
|
|
|
return &acb->common;
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:14 +04:00
|
|
|
static BlockAIOCB *blkverify_aio_flush(BlockDriverState *bs,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb,
|
2014-10-07 15:59:14 +04:00
|
|
|
void *opaque)
|
2010-09-21 18:43:03 +04:00
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
|
|
|
/* Only flush test file, the raw file is not important */
|
2015-06-16 12:13:47 +03:00
|
|
|
return bdrv_aio_flush(s->test_file->bs, cb, opaque);
|
2010-09-21 18:43:03 +04:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:11:34 +04:00
|
|
|
static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs,
|
|
|
|
BlockDriverState *candidate)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 15:19:22 +03:00
|
|
|
bool perm = bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
|
2014-03-03 22:11:34 +04:00
|
|
|
|
|
|
|
if (perm) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-16 12:13:47 +03:00
|
|
|
return bdrv_recurse_is_first_non_filter(s->test_file->bs, candidate);
|
2014-03-03 22:11:34 +04:00
|
|
|
}
|
|
|
|
|
2014-05-08 18:34:39 +04:00
|
|
|
/* Propagate AioContext changes to ->test_file */
|
|
|
|
static void blkverify_detach_aio_context(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 12:13:47 +03:00
|
|
|
bdrv_detach_aio_context(s->test_file->bs);
|
2014-05-08 18:34:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void blkverify_attach_aio_context(BlockDriverState *bs,
|
|
|
|
AioContext *new_context)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 12:13:47 +03:00
|
|
|
bdrv_attach_aio_context(s->test_file->bs, new_context);
|
2014-05-08 18:34:39 +04:00
|
|
|
}
|
|
|
|
|
2014-07-18 22:24:58 +04:00
|
|
|
static void blkverify_refresh_filename(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVBlkverifyState *s = bs->opaque;
|
|
|
|
|
2015-06-16 15:19:22 +03:00
|
|
|
/* bs->file->bs has already been refreshed */
|
2015-06-16 12:13:47 +03:00
|
|
|
bdrv_refresh_filename(s->test_file->bs);
|
2014-07-18 22:24:58 +04:00
|
|
|
|
2015-06-16 15:19:22 +03:00
|
|
|
if (bs->file->bs->full_open_options
|
|
|
|
&& s->test_file->bs->full_open_options)
|
|
|
|
{
|
2014-07-18 22:24:58 +04:00
|
|
|
QDict *opts = qdict_new();
|
|
|
|
qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkverify")));
|
|
|
|
|
2015-06-16 15:19:22 +03:00
|
|
|
QINCREF(bs->file->bs->full_open_options);
|
|
|
|
qdict_put_obj(opts, "raw", QOBJECT(bs->file->bs->full_open_options));
|
2015-06-16 12:13:47 +03:00
|
|
|
QINCREF(s->test_file->bs->full_open_options);
|
|
|
|
qdict_put_obj(opts, "test",
|
|
|
|
QOBJECT(s->test_file->bs->full_open_options));
|
2014-07-18 22:24:58 +04:00
|
|
|
|
|
|
|
bs->full_open_options = opts;
|
|
|
|
}
|
|
|
|
|
2015-06-16 15:19:22 +03:00
|
|
|
if (bs->file->bs->exact_filename[0]
|
|
|
|
&& s->test_file->bs->exact_filename[0])
|
|
|
|
{
|
2014-07-18 22:24:58 +04:00
|
|
|
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
|
|
"blkverify:%s:%s",
|
2015-06-16 15:19:22 +03:00
|
|
|
bs->file->bs->exact_filename,
|
|
|
|
s->test_file->bs->exact_filename);
|
2014-07-18 22:24:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-21 18:43:03 +04:00
|
|
|
static BlockDriver bdrv_blkverify = {
|
2014-05-08 18:34:39 +04: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,
|
|
|
|
.bdrv_getlength = blkverify_getlength,
|
2014-07-18 22:24:58 +04:00
|
|
|
.bdrv_refresh_filename = blkverify_refresh_filename,
|
2013-04-10 16:40:28 +04:00
|
|
|
|
2014-05-08 18:34:39 +04:00
|
|
|
.bdrv_aio_readv = blkverify_aio_readv,
|
|
|
|
.bdrv_aio_writev = blkverify_aio_writev,
|
|
|
|
.bdrv_aio_flush = blkverify_aio_flush,
|
2013-04-10 16:40:28 +04:00
|
|
|
|
2014-05-08 18:34:39 +04:00
|
|
|
.bdrv_attach_aio_context = blkverify_attach_aio_context,
|
|
|
|
.bdrv_detach_aio_context = blkverify_detach_aio_context,
|
2013-10-02 16:33:48 +04:00
|
|
|
|
2014-05-08 18:34:39 +04:00
|
|
|
.is_filter = true,
|
2014-03-03 22:11:34 +04:00
|
|
|
.bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
|
2010-09-21 18:43:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_blkverify_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_blkverify);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_blkverify_init);
|