1d9d33417SStefan Hajnoczi /*
2d9d33417SStefan Hajnoczi * Block protocol for block driver correctness testing
3d9d33417SStefan Hajnoczi *
4d9d33417SStefan Hajnoczi * Copyright (C) 2010 IBM, Corp.
5d9d33417SStefan Hajnoczi *
6d9d33417SStefan Hajnoczi * This work is licensed under the terms of the GNU GPL, version 2 or later.
7d9d33417SStefan Hajnoczi * See the COPYING file in the top-level directory.
8d9d33417SStefan Hajnoczi */
9d9d33417SStefan Hajnoczi
1080c71a24SPeter Maydell #include "qemu/osdep.h"
11da34e65cSMarkus Armbruster #include "qapi/error.h"
121de7afc9SPaolo Bonzini #include "qemu/sockets.h" /* for EINPROGRESS on Windows */
13e2c1c34fSMarkus Armbruster #include "block/block-io.h"
14737e150eSPaolo Bonzini #include "block/block_int.h"
1574b36b2eSMax Reitz #include "qapi/qmp/qdict.h"
1674b36b2eSMax Reitz #include "qapi/qmp/qstring.h"
17f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
180b8fa32fSMarkus Armbruster #include "qemu/module.h"
19922a01a0SMarkus Armbruster #include "qemu/option.h"
205df022cfSPeter Maydell #include "qemu/memalign.h"
21d9d33417SStefan Hajnoczi
22d9d33417SStefan Hajnoczi typedef struct {
233e586be0SKevin Wolf BdrvChild *test_file;
24d9d33417SStefan Hajnoczi } BDRVBlkverifyState;
25d9d33417SStefan Hajnoczi
2644b67892SKevin Wolf typedef struct BlkverifyRequest {
2744b67892SKevin Wolf Coroutine *co;
2844b67892SKevin Wolf BlockDriverState *bs;
29d9d33417SStefan Hajnoczi
30d9d33417SStefan Hajnoczi /* Request metadata */
31d9d33417SStefan Hajnoczi bool is_write;
3244b67892SKevin Wolf uint64_t offset;
3344b67892SKevin Wolf uint64_t bytes;
3444b67892SKevin Wolf int flags;
35d9d33417SStefan Hajnoczi
36244b26d2SKevin Wolf int GRAPH_RDLOCK_PTR (*request_fn)(
37244b26d2SKevin Wolf BdrvChild *, int64_t, int64_t, QEMUIOVector *, BdrvRequestFlags);
3844b67892SKevin Wolf
3944b67892SKevin Wolf int ret; /* test image result */
4044b67892SKevin Wolf int raw_ret; /* raw image result */
4144b67892SKevin Wolf
42d9d33417SStefan Hajnoczi unsigned int done; /* completion counter */
43d9d33417SStefan Hajnoczi
44d9d33417SStefan Hajnoczi QEMUIOVector *qiov; /* user I/O vector */
4544b67892SKevin Wolf QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */
4644b67892SKevin Wolf } BlkverifyRequest;
47d9d33417SStefan Hajnoczi
blkverify_err(BlkverifyRequest * r,const char * fmt,...)489edc6313SMarc-André Lureau static void G_GNUC_PRINTF(2, 3) blkverify_err(BlkverifyRequest *r,
49a77cffe7SStefan Weil const char *fmt, ...)
50d9d33417SStefan Hajnoczi {
51d9d33417SStefan Hajnoczi va_list ap;
52d9d33417SStefan Hajnoczi
53d9d33417SStefan Hajnoczi va_start(ap, fmt);
5444b67892SKevin Wolf fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ",
5544b67892SKevin Wolf r->is_write ? "write" : "read", r->offset, r->bytes);
56d9d33417SStefan Hajnoczi vfprintf(stderr, fmt, ap);
57d9d33417SStefan Hajnoczi fprintf(stderr, "\n");
58d9d33417SStefan Hajnoczi va_end(ap);
59d9d33417SStefan Hajnoczi exit(1);
60d9d33417SStefan Hajnoczi }
61d9d33417SStefan Hajnoczi
62d9d33417SStefan Hajnoczi /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
blkverify_parse_filename(const char * filename,QDict * options,Error ** errp)6316c79092SKevin Wolf static void blkverify_parse_filename(const char *filename, QDict *options,
6416c79092SKevin Wolf Error **errp)
65d9d33417SStefan Hajnoczi {
6616c79092SKevin Wolf const char *c;
6716c79092SKevin Wolf QString *raw_path;
6816c79092SKevin Wolf
69d9d33417SStefan Hajnoczi
70d9d33417SStefan Hajnoczi /* Parse the blkverify: prefix */
7116c79092SKevin Wolf if (!strstart(filename, "blkverify:", &filename)) {
7222511ad6SMax Reitz /* There was no prefix; therefore, all options have to be already
7322511ad6SMax Reitz present in the QDict (except for the filename) */
7446f5ac20SEric Blake qdict_put_str(options, "x-image", filename);
7516c79092SKevin Wolf return;
76d9d33417SStefan Hajnoczi }
77d9d33417SStefan Hajnoczi
78d9d33417SStefan Hajnoczi /* Parse the raw image filename */
79d9d33417SStefan Hajnoczi c = strchr(filename, ':');
80d9d33417SStefan Hajnoczi if (c == NULL) {
8116c79092SKevin Wolf error_setg(errp, "blkverify requires raw copy and original image path");
8216c79092SKevin Wolf return;
83d9d33417SStefan Hajnoczi }
84d9d33417SStefan Hajnoczi
8516c79092SKevin Wolf /* TODO Implement option pass-through and set raw.filename here */
86ba891d68SMarkus Armbruster raw_path = qstring_from_substr(filename, 0, c - filename);
8716c79092SKevin Wolf qdict_put(options, "x-raw", raw_path);
8816c79092SKevin Wolf
8916c79092SKevin Wolf /* TODO Allow multi-level nesting and set file.filename here */
90d9d33417SStefan Hajnoczi filename = c + 1;
9146f5ac20SEric Blake qdict_put_str(options, "x-image", filename);
9216c79092SKevin Wolf }
9316c79092SKevin Wolf
9416c79092SKevin Wolf static QemuOptsList runtime_opts = {
9516c79092SKevin Wolf .name = "blkverify",
9616c79092SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
9716c79092SKevin Wolf .desc = {
9816c79092SKevin Wolf {
9916c79092SKevin Wolf .name = "x-raw",
10016c79092SKevin Wolf .type = QEMU_OPT_STRING,
10116c79092SKevin Wolf .help = "[internal use only, will be removed]",
10216c79092SKevin Wolf },
10316c79092SKevin Wolf {
10416c79092SKevin Wolf .name = "x-image",
10516c79092SKevin Wolf .type = QEMU_OPT_STRING,
10616c79092SKevin Wolf .help = "[internal use only, will be removed]",
10716c79092SKevin Wolf },
10816c79092SKevin Wolf { /* end of list */ }
10916c79092SKevin Wolf },
11016c79092SKevin Wolf };
11116c79092SKevin Wolf
blkverify_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)112015a1036SMax Reitz static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
113015a1036SMax Reitz Error **errp)
11416c79092SKevin Wolf {
11516c79092SKevin Wolf BDRVBlkverifyState *s = bs->opaque;
11616c79092SKevin Wolf QemuOpts *opts;
11716c79092SKevin Wolf int ret;
11816c79092SKevin Wolf
11987ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
120af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) {
12116c79092SKevin Wolf ret = -EINVAL;
12216c79092SKevin Wolf goto fail;
12316c79092SKevin Wolf }
12416c79092SKevin Wolf
12570b6198aSMax Reitz /* Open the raw file */
12683930780SVladimir Sementsov-Ogievskiy ret = bdrv_open_file_child(qemu_opt_get(opts, "x-raw"), options, "raw",
12783930780SVladimir Sementsov-Ogievskiy bs, errp);
12883930780SVladimir Sementsov-Ogievskiy if (ret < 0) {
12916c79092SKevin Wolf goto fail;
13016c79092SKevin Wolf }
131d9d33417SStefan Hajnoczi
132d9d33417SStefan Hajnoczi /* Open the test file */
1333e586be0SKevin Wolf s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
13436ee58d1SMax Reitz "test", bs, &child_of_bds, BDRV_CHILD_DATA,
135bc520249SVladimir Sementsov-Ogievskiy false, errp);
136bc520249SVladimir Sementsov-Ogievskiy if (!s->test_file) {
1373e586be0SKevin Wolf ret = -EINVAL;
13816c79092SKevin Wolf goto fail;
139d9d33417SStefan Hajnoczi }
140d9d33417SStefan Hajnoczi
141228345bfSMax Reitz bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
142228345bfSMax Reitz bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
143228345bfSMax Reitz
14416c79092SKevin Wolf ret = 0;
14516c79092SKevin Wolf fail:
14631585931SFam Zheng qemu_opts_del(opts);
14716c79092SKevin Wolf return ret;
148d9d33417SStefan Hajnoczi }
149d9d33417SStefan Hajnoczi
blkverify_close(BlockDriverState * bs)150d9d33417SStefan Hajnoczi static void blkverify_close(BlockDriverState *bs)
151d9d33417SStefan Hajnoczi {
152d9d33417SStefan Hajnoczi BDRVBlkverifyState *s = bs->opaque;
153d9d33417SStefan Hajnoczi
1546bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
1553e586be0SKevin Wolf bdrv_unref_child(bs, s->test_file);
156d9d33417SStefan Hajnoczi s->test_file = NULL;
1576bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
158d9d33417SStefan Hajnoczi }
159d9d33417SStefan Hajnoczi
1608ab8140aSKevin Wolf static int64_t coroutine_fn GRAPH_RDLOCK
blkverify_co_getlength(BlockDriverState * bs)1618ab8140aSKevin Wolf blkverify_co_getlength(BlockDriverState *bs)
162d9d33417SStefan Hajnoczi {
163d9d33417SStefan Hajnoczi BDRVBlkverifyState *s = bs->opaque;
164d9d33417SStefan Hajnoczi
165c86422c5SEmanuele Giuseppe Esposito return bdrv_co_getlength(s->test_file->bs);
166d9d33417SStefan Hajnoczi }
167d9d33417SStefan Hajnoczi
blkverify_do_test_req(void * opaque)16844b67892SKevin Wolf static void coroutine_fn blkverify_do_test_req(void *opaque)
169d9d33417SStefan Hajnoczi {
17044b67892SKevin Wolf BlkverifyRequest *r = opaque;
17144b67892SKevin Wolf BDRVBlkverifyState *s = r->bs->opaque;
172d9d33417SStefan Hajnoczi
173244b26d2SKevin Wolf bdrv_graph_co_rdlock();
17444b67892SKevin Wolf r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
17544b67892SKevin Wolf r->flags);
176244b26d2SKevin Wolf bdrv_graph_co_rdunlock();
177244b26d2SKevin Wolf
17844b67892SKevin Wolf r->done++;
17944b67892SKevin Wolf qemu_coroutine_enter_if_inactive(r->co);
180d9d33417SStefan Hajnoczi }
181d9d33417SStefan Hajnoczi
blkverify_do_raw_req(void * opaque)18244b67892SKevin Wolf static void coroutine_fn blkverify_do_raw_req(void *opaque)
183d9d33417SStefan Hajnoczi {
18444b67892SKevin Wolf BlkverifyRequest *r = opaque;
185d9d33417SStefan Hajnoczi
186244b26d2SKevin Wolf bdrv_graph_co_rdlock();
18744b67892SKevin Wolf r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
18844b67892SKevin Wolf r->flags);
189244b26d2SKevin Wolf bdrv_graph_co_rdunlock();
190244b26d2SKevin Wolf
19144b67892SKevin Wolf r->done++;
19244b67892SKevin Wolf qemu_coroutine_enter_if_inactive(r->co);
193d9d33417SStefan Hajnoczi }
194d9d33417SStefan Hajnoczi
195244b26d2SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
blkverify_co_prwv(BlockDriverState * bs,BlkverifyRequest * r,uint64_t offset,uint64_t bytes,QEMUIOVector * qiov,QEMUIOVector * raw_qiov,int flags,bool is_write)19644b67892SKevin Wolf blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
19744b67892SKevin Wolf uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
19844b67892SKevin Wolf int flags, bool is_write)
199d9d33417SStefan Hajnoczi {
20044b67892SKevin Wolf Coroutine *co_a, *co_b;
201d9d33417SStefan Hajnoczi
20244b67892SKevin Wolf *r = (BlkverifyRequest) {
20344b67892SKevin Wolf .co = qemu_coroutine_self(),
20444b67892SKevin Wolf .bs = bs,
20544b67892SKevin Wolf .offset = offset,
20644b67892SKevin Wolf .bytes = bytes,
20744b67892SKevin Wolf .qiov = qiov,
20844b67892SKevin Wolf .raw_qiov = raw_qiov,
20944b67892SKevin Wolf .flags = flags,
21044b67892SKevin Wolf .is_write = is_write,
21144b67892SKevin Wolf .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
21244b67892SKevin Wolf };
213d9d33417SStefan Hajnoczi
21444b67892SKevin Wolf co_a = qemu_coroutine_create(blkverify_do_test_req, r);
21544b67892SKevin Wolf co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
21644b67892SKevin Wolf
21744b67892SKevin Wolf qemu_coroutine_enter(co_a);
21844b67892SKevin Wolf qemu_coroutine_enter(co_b);
21944b67892SKevin Wolf
22044b67892SKevin Wolf while (r->done < 2) {
22144b67892SKevin Wolf qemu_coroutine_yield();
222d9d33417SStefan Hajnoczi }
223d9d33417SStefan Hajnoczi
22444b67892SKevin Wolf if (r->ret != r->raw_ret) {
22544b67892SKevin Wolf blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
226d9d33417SStefan Hajnoczi }
227d9d33417SStefan Hajnoczi
22844b67892SKevin Wolf return r->ret;
229d9d33417SStefan Hajnoczi }
230d9d33417SStefan Hajnoczi
231244b26d2SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
blkverify_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)232f7ef38ddSVladimir Sementsov-Ogievskiy blkverify_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
233f7ef38ddSVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, BdrvRequestFlags flags)
234d9d33417SStefan Hajnoczi {
23544b67892SKevin Wolf BlkverifyRequest r;
23644b67892SKevin Wolf QEMUIOVector raw_qiov;
23744b67892SKevin Wolf void *buf;
23844b67892SKevin Wolf ssize_t cmp_offset;
23944b67892SKevin Wolf int ret;
24044b67892SKevin Wolf
24144b67892SKevin Wolf buf = qemu_blockalign(bs->file->bs, qiov->size);
24244b67892SKevin Wolf qemu_iovec_init(&raw_qiov, qiov->niov);
24344b67892SKevin Wolf qemu_iovec_clone(&raw_qiov, qiov, buf);
24444b67892SKevin Wolf
245e8b65355SStefan Hajnoczi ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov,
246e8b65355SStefan Hajnoczi flags & ~BDRV_REQ_REGISTERED_BUF, false);
24744b67892SKevin Wolf
24844b67892SKevin Wolf cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
24944b67892SKevin Wolf if (cmp_offset != -1) {
25044b67892SKevin Wolf blkverify_err(&r, "contents mismatch at offset %" PRId64,
25144b67892SKevin Wolf offset + cmp_offset);
252d9d33417SStefan Hajnoczi }
253d9d33417SStefan Hajnoczi
25444b67892SKevin Wolf qemu_iovec_destroy(&raw_qiov);
25544b67892SKevin Wolf qemu_vfree(buf);
25644b67892SKevin Wolf
25744b67892SKevin Wolf return ret;
25844b67892SKevin Wolf }
25944b67892SKevin Wolf
260244b26d2SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
blkverify_co_pwritev(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)261e75abedaSVladimir Sementsov-Ogievskiy blkverify_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
262e75abedaSVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, BdrvRequestFlags flags)
263d9d33417SStefan Hajnoczi {
26444b67892SKevin Wolf BlkverifyRequest r;
26544b67892SKevin Wolf return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
266d9d33417SStefan Hajnoczi }
267d9d33417SStefan Hajnoczi
blkverify_co_flush(BlockDriverState * bs)26888095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK blkverify_co_flush(BlockDriverState *bs)
269d9d33417SStefan Hajnoczi {
270d9d33417SStefan Hajnoczi BDRVBlkverifyState *s = bs->opaque;
271d9d33417SStefan Hajnoczi
272d9d33417SStefan Hajnoczi /* Only flush test file, the raw file is not important */
27344b67892SKevin Wolf return bdrv_co_flush(s->test_file->bs);
274d9d33417SStefan Hajnoczi }
275d9d33417SStefan Hajnoczi
276533c6e4eSKevin Wolf static bool GRAPH_RDLOCK
blkverify_recurse_can_replace(BlockDriverState * bs,BlockDriverState * to_replace)277533c6e4eSKevin Wolf blkverify_recurse_can_replace(BlockDriverState *bs,
278998a6b2fSMax Reitz BlockDriverState *to_replace)
279998a6b2fSMax Reitz {
280998a6b2fSMax Reitz BDRVBlkverifyState *s = bs->opaque;
281998a6b2fSMax Reitz
282998a6b2fSMax Reitz /*
283998a6b2fSMax Reitz * blkverify quits the whole qemu process if there is a mismatch
284998a6b2fSMax Reitz * between bs->file->bs and s->test_file->bs. Therefore, we know
285998a6b2fSMax Reitz * know that both must match bs and we can recurse down to either.
286998a6b2fSMax Reitz */
287998a6b2fSMax Reitz return bdrv_recurse_can_replace(bs->file->bs, to_replace) ||
288998a6b2fSMax Reitz bdrv_recurse_can_replace(s->test_file->bs, to_replace);
289998a6b2fSMax Reitz }
290998a6b2fSMax Reitz
blkverify_refresh_filename(BlockDriverState * bs)29179a55866SKevin Wolf static void GRAPH_RDLOCK blkverify_refresh_filename(BlockDriverState *bs)
29274b36b2eSMax Reitz {
29374b36b2eSMax Reitz BDRVBlkverifyState *s = bs->opaque;
29474b36b2eSMax Reitz
2959a4f4c31SKevin Wolf if (bs->file->bs->exact_filename[0]
2969a4f4c31SKevin Wolf && s->test_file->bs->exact_filename[0])
2979a4f4c31SKevin Wolf {
29805cc758aSMax Reitz int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
29974b36b2eSMax Reitz "blkverify:%s:%s",
3009a4f4c31SKevin Wolf bs->file->bs->exact_filename,
3019a4f4c31SKevin Wolf s->test_file->bs->exact_filename);
30205cc758aSMax Reitz if (ret >= sizeof(bs->exact_filename)) {
30305cc758aSMax Reitz /* An overflow makes the filename unusable, so do not report any */
30405cc758aSMax Reitz bs->exact_filename[0] = 0;
30505cc758aSMax Reitz }
30674b36b2eSMax Reitz }
30774b36b2eSMax Reitz }
30874b36b2eSMax Reitz
blkverify_dirname(BlockDriverState * bs,Error ** errp)30927953572SMax Reitz static char *blkverify_dirname(BlockDriverState *bs, Error **errp)
31027953572SMax Reitz {
31127953572SMax Reitz /* In general, there are two BDSs with different dirnames below this one;
31227953572SMax Reitz * so there is no unique dirname we could return (unless both are equal by
31327953572SMax Reitz * chance). Therefore, to be consistent, just always return NULL. */
31427953572SMax Reitz error_setg(errp, "Cannot generate a base directory for blkverify nodes");
31527953572SMax Reitz return NULL;
31627953572SMax Reitz }
31727953572SMax Reitz
318d9d33417SStefan Hajnoczi static BlockDriver bdrv_blkverify = {
319d9d33417SStefan Hajnoczi .format_name = "blkverify",
320d9d33417SStefan Hajnoczi .protocol_name = "blkverify",
321d9d33417SStefan Hajnoczi .instance_size = sizeof(BDRVBlkverifyState),
322d9d33417SStefan Hajnoczi
32316c79092SKevin Wolf .bdrv_parse_filename = blkverify_parse_filename,
324*44b424dcSPaolo Bonzini .bdrv_open = blkverify_open,
325d9d33417SStefan Hajnoczi .bdrv_close = blkverify_close,
32669dca43dSMax Reitz .bdrv_child_perm = bdrv_default_perms,
327c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = blkverify_co_getlength,
32874b36b2eSMax Reitz .bdrv_refresh_filename = blkverify_refresh_filename,
32927953572SMax Reitz .bdrv_dirname = blkverify_dirname,
330d9d33417SStefan Hajnoczi
33144b67892SKevin Wolf .bdrv_co_preadv = blkverify_co_preadv,
33244b67892SKevin Wolf .bdrv_co_pwritev = blkverify_co_pwritev,
33344b67892SKevin Wolf .bdrv_co_flush = blkverify_co_flush,
334f6186f49SBenoît Canet
335b5042a36SBenoît Canet .is_filter = true,
336998a6b2fSMax Reitz .bdrv_recurse_can_replace = blkverify_recurse_can_replace,
337d9d33417SStefan Hajnoczi };
338d9d33417SStefan Hajnoczi
bdrv_blkverify_init(void)339d9d33417SStefan Hajnoczi static void bdrv_blkverify_init(void)
340d9d33417SStefan Hajnoczi {
341d9d33417SStefan Hajnoczi bdrv_register(&bdrv_blkverify);
342d9d33417SStefan Hajnoczi }
343d9d33417SStefan Hajnoczi
344d9d33417SStefan Hajnoczi block_init(bdrv_blkverify_init);
345