blob: ec45d8335ed80745101ea95f24e9c90145622a01 [file] [log] [blame]
Stefan Hajnoczid9d33412010-09-21 15:43:03 +01001/*
2 * Block protocol for block driver correctness testing
3 *
4 * Copyright (C) 2010 IBM, Corp.
5 *
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.
8 */
9
Peter Maydell80c71a22016-01-18 18:01:42 +000010#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010011#include "qapi/error.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010012#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
Markus Armbrustere2c1c342022-12-21 14:35:49 +010013#include "block/block-io.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010014#include "block/block_int.h"
Max Reitz74b36b22014-07-18 20:24:58 +020015#include "qapi/qmp/qdict.h"
16#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020018#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010019#include "qemu/option.h"
Peter Maydell5df022c2022-02-26 18:07:23 +000020#include "qemu/memalign.h"
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010021
22typedef struct {
Kevin Wolf3e586be2015-06-16 11:13:47 +020023 BdrvChild *test_file;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010024} BDRVBlkverifyState;
25
Kevin Wolf44b67892016-11-04 21:13:45 +010026typedef struct BlkverifyRequest {
27 Coroutine *co;
28 BlockDriverState *bs;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010029
30 /* Request metadata */
31 bool is_write;
Kevin Wolf44b67892016-11-04 21:13:45 +010032 uint64_t offset;
33 uint64_t bytes;
34 int flags;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010035
Kevin Wolf244b26d2023-10-27 17:53:27 +020036 int GRAPH_RDLOCK_PTR (*request_fn)(
37 BdrvChild *, int64_t, int64_t, QEMUIOVector *, BdrvRequestFlags);
Kevin Wolf44b67892016-11-04 21:13:45 +010038
39 int ret; /* test image result */
40 int raw_ret; /* raw image result */
41
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010042 unsigned int done; /* completion counter */
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010043
44 QEMUIOVector *qiov; /* user I/O vector */
Kevin Wolf44b67892016-11-04 21:13:45 +010045 QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */
46} BlkverifyRequest;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010047
Marc-André Lureau9edc6312022-02-20 20:39:25 +040048static void G_GNUC_PRINTF(2, 3) blkverify_err(BlkverifyRequest *r,
Stefan Weila77cffe2010-09-24 21:02:05 +020049 const char *fmt, ...)
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010050{
51 va_list ap;
52
53 va_start(ap, fmt);
Kevin Wolf44b67892016-11-04 21:13:45 +010054 fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ",
55 r->is_write ? "write" : "read", r->offset, r->bytes);
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010056 vfprintf(stderr, fmt, ap);
57 fprintf(stderr, "\n");
58 va_end(ap);
59 exit(1);
60}
61
62/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
Kevin Wolf16c79092013-04-10 14:40:28 +020063static void blkverify_parse_filename(const char *filename, QDict *options,
64 Error **errp)
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010065{
Kevin Wolf16c79092013-04-10 14:40:28 +020066 const char *c;
67 QString *raw_path;
68
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010069
70 /* Parse the blkverify: prefix */
Kevin Wolf16c79092013-04-10 14:40:28 +020071 if (!strstart(filename, "blkverify:", &filename)) {
Max Reitz22511ad2013-12-20 19:28:17 +010072 /* There was no prefix; therefore, all options have to be already
73 present in the QDict (except for the filename) */
Eric Blake46f5ac22017-04-27 16:58:17 -050074 qdict_put_str(options, "x-image", filename);
Kevin Wolf16c79092013-04-10 14:40:28 +020075 return;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010076 }
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010077
78 /* Parse the raw image filename */
79 c = strchr(filename, ':');
80 if (c == NULL) {
Kevin Wolf16c79092013-04-10 14:40:28 +020081 error_setg(errp, "blkverify requires raw copy and original image path");
82 return;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010083 }
84
Kevin Wolf16c79092013-04-10 14:40:28 +020085 /* TODO Implement option pass-through and set raw.filename here */
Markus Armbrusterba891d62018-07-27 08:22:04 +020086 raw_path = qstring_from_substr(filename, 0, c - filename);
Kevin Wolf16c79092013-04-10 14:40:28 +020087 qdict_put(options, "x-raw", raw_path);
88
89 /* TODO Allow multi-level nesting and set file.filename here */
Stefan Hajnoczid9d33412010-09-21 15:43:03 +010090 filename = c + 1;
Eric Blake46f5ac22017-04-27 16:58:17 -050091 qdict_put_str(options, "x-image", filename);
Kevin Wolf16c79092013-04-10 14:40:28 +020092}
93
94static QemuOptsList runtime_opts = {
95 .name = "blkverify",
96 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
97 .desc = {
98 {
99 .name = "x-raw",
100 .type = QEMU_OPT_STRING,
101 .help = "[internal use only, will be removed]",
102 },
103 {
104 .name = "x-image",
105 .type = QEMU_OPT_STRING,
106 .help = "[internal use only, will be removed]",
107 },
108 { /* end of list */ }
109 },
110};
111
Max Reitz015a1032013-09-05 14:22:29 +0200112static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
113 Error **errp)
Kevin Wolf16c79092013-04-10 14:40:28 +0200114{
115 BDRVBlkverifyState *s = bs->opaque;
116 QemuOpts *opts;
Kevin Wolf16c79092013-04-10 14:40:28 +0200117 int ret;
118
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800119 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Markus Armbrusteraf175e82020-07-07 18:06:03 +0200120 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
Kevin Wolf16c79092013-04-10 14:40:28 +0200121 ret = -EINVAL;
122 goto fail;
123 }
124
Max Reitz70b61982013-12-20 19:28:16 +0100125 /* Open the raw file */
Vladimir Sementsov-Ogievskiy83930782022-07-26 23:11:21 +0300126 ret = bdrv_open_file_child(qemu_opt_get(opts, "x-raw"), options, "raw",
127 bs, errp);
128 if (ret < 0) {
Kevin Wolf16c79092013-04-10 14:40:28 +0200129 goto fail;
130 }
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100131
132 /* Open the test file */
Kevin Wolf3e586be2015-06-16 11:13:47 +0200133 s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
Max Reitz36ee58d2020-05-13 13:05:31 +0200134 "test", bs, &child_of_bds, BDRV_CHILD_DATA,
Vladimir Sementsov-Ogievskiybc520242021-02-02 15:49:45 +0300135 false, errp);
136 if (!s->test_file) {
Kevin Wolf3e586be2015-06-16 11:13:47 +0200137 ret = -EINVAL;
Kevin Wolf16c79092013-04-10 14:40:28 +0200138 goto fail;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100139 }
140
Max Reitz228345b2018-04-21 15:29:26 +0200141 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
142 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
143
Kevin Wolf16c79092013-04-10 14:40:28 +0200144 ret = 0;
145fail:
Fam Zheng31585932014-08-28 13:56:11 +0800146 qemu_opts_del(opts);
Kevin Wolf16c79092013-04-10 14:40:28 +0200147 return ret;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100148}
149
150static void blkverify_close(BlockDriverState *bs)
151{
152 BDRVBlkverifyState *s = bs->opaque;
153
Stefan Hajnoczi6bc30f12023-12-05 13:20:02 -0500154 bdrv_graph_wrlock();
Kevin Wolf3e586be2015-06-16 11:13:47 +0200155 bdrv_unref_child(bs, s->test_file);
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100156 s->test_file = NULL;
Stefan Hajnoczi6bc30f12023-12-05 13:20:02 -0500157 bdrv_graph_wrunlock();
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100158}
159
Kevin Wolf8ab81402023-02-03 16:22:02 +0100160static int64_t coroutine_fn GRAPH_RDLOCK
161blkverify_co_getlength(BlockDriverState *bs)
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100162{
163 BDRVBlkverifyState *s = bs->opaque;
164
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +0100165 return bdrv_co_getlength(s->test_file->bs);
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100166}
167
Kevin Wolf44b67892016-11-04 21:13:45 +0100168static void coroutine_fn blkverify_do_test_req(void *opaque)
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100169{
Kevin Wolf44b67892016-11-04 21:13:45 +0100170 BlkverifyRequest *r = opaque;
171 BDRVBlkverifyState *s = r->bs->opaque;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100172
Kevin Wolf244b26d2023-10-27 17:53:27 +0200173 bdrv_graph_co_rdlock();
Kevin Wolf44b67892016-11-04 21:13:45 +0100174 r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
175 r->flags);
Kevin Wolf244b26d2023-10-27 17:53:27 +0200176 bdrv_graph_co_rdunlock();
177
Kevin Wolf44b67892016-11-04 21:13:45 +0100178 r->done++;
179 qemu_coroutine_enter_if_inactive(r->co);
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100180}
181
Kevin Wolf44b67892016-11-04 21:13:45 +0100182static void coroutine_fn blkverify_do_raw_req(void *opaque)
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100183{
Kevin Wolf44b67892016-11-04 21:13:45 +0100184 BlkverifyRequest *r = opaque;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100185
Kevin Wolf244b26d2023-10-27 17:53:27 +0200186 bdrv_graph_co_rdlock();
Kevin Wolf44b67892016-11-04 21:13:45 +0100187 r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
188 r->flags);
Kevin Wolf244b26d2023-10-27 17:53:27 +0200189 bdrv_graph_co_rdunlock();
190
Kevin Wolf44b67892016-11-04 21:13:45 +0100191 r->done++;
192 qemu_coroutine_enter_if_inactive(r->co);
193}
194
Kevin Wolf244b26d2023-10-27 17:53:27 +0200195static int coroutine_fn GRAPH_RDLOCK
Kevin Wolf44b67892016-11-04 21:13:45 +0100196blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
197 uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
198 int flags, bool is_write)
199{
200 Coroutine *co_a, *co_b;
201
202 *r = (BlkverifyRequest) {
203 .co = qemu_coroutine_self(),
204 .bs = bs,
205 .offset = offset,
206 .bytes = bytes,
207 .qiov = qiov,
208 .raw_qiov = raw_qiov,
209 .flags = flags,
210 .is_write = is_write,
211 .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
212 };
213
214 co_a = qemu_coroutine_create(blkverify_do_test_req, r);
215 co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
216
217 qemu_coroutine_enter(co_a);
218 qemu_coroutine_enter(co_b);
219
220 while (r->done < 2) {
221 qemu_coroutine_yield();
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100222 }
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100223
Kevin Wolf44b67892016-11-04 21:13:45 +0100224 if (r->ret != r->raw_ret) {
225 blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100226 }
Kevin Wolf44b67892016-11-04 21:13:45 +0100227
228 return r->ret;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100229}
230
Kevin Wolf244b26d2023-10-27 17:53:27 +0200231static int coroutine_fn GRAPH_RDLOCK
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300232blkverify_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
233 QEMUIOVector *qiov, BdrvRequestFlags flags)
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100234{
Kevin Wolf44b67892016-11-04 21:13:45 +0100235 BlkverifyRequest r;
236 QEMUIOVector raw_qiov;
237 void *buf;
238 ssize_t cmp_offset;
239 int ret;
240
241 buf = qemu_blockalign(bs->file->bs, qiov->size);
242 qemu_iovec_init(&raw_qiov, qiov->niov);
243 qemu_iovec_clone(&raw_qiov, qiov, buf);
244
Stefan Hajnoczie8b65352022-10-13 14:59:01 -0400245 ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov,
246 flags & ~BDRV_REQ_REGISTERED_BUF, false);
Kevin Wolf44b67892016-11-04 21:13:45 +0100247
248 cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
249 if (cmp_offset != -1) {
250 blkverify_err(&r, "contents mismatch at offset %" PRId64,
251 offset + cmp_offset);
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100252 }
Kevin Wolf44b67892016-11-04 21:13:45 +0100253
254 qemu_iovec_destroy(&raw_qiov);
255 qemu_vfree(buf);
256
257 return ret;
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100258}
259
Kevin Wolf244b26d2023-10-27 17:53:27 +0200260static int coroutine_fn GRAPH_RDLOCK
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +0300261blkverify_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
262 QEMUIOVector *qiov, BdrvRequestFlags flags)
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100263{
Kevin Wolf44b67892016-11-04 21:13:45 +0100264 BlkverifyRequest r;
265 return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100266}
267
Emanuele Giuseppe Esposito88095342023-02-03 16:21:46 +0100268static int coroutine_fn GRAPH_RDLOCK blkverify_co_flush(BlockDriverState *bs)
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100269{
270 BDRVBlkverifyState *s = bs->opaque;
271
272 /* Only flush test file, the raw file is not important */
Kevin Wolf44b67892016-11-04 21:13:45 +0100273 return bdrv_co_flush(s->test_file->bs);
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100274}
275
Kevin Wolf533c6e42023-05-04 13:57:49 +0200276static bool GRAPH_RDLOCK
277blkverify_recurse_can_replace(BlockDriverState *bs,
278 BlockDriverState *to_replace)
Max Reitz998a6b22020-02-18 11:34:42 +0100279{
280 BDRVBlkverifyState *s = bs->opaque;
281
282 /*
283 * blkverify quits the whole qemu process if there is a mismatch
284 * between bs->file->bs and s->test_file->bs. Therefore, we know
285 * know that both must match bs and we can recurse down to either.
286 */
287 return bdrv_recurse_can_replace(bs->file->bs, to_replace) ||
288 bdrv_recurse_can_replace(s->test_file->bs, to_replace);
289}
290
Kevin Wolf79a55862023-10-27 17:53:29 +0200291static void GRAPH_RDLOCK blkverify_refresh_filename(BlockDriverState *bs)
Max Reitz74b36b22014-07-18 20:24:58 +0200292{
293 BDRVBlkverifyState *s = bs->opaque;
294
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200295 if (bs->file->bs->exact_filename[0]
296 && s->test_file->bs->exact_filename[0])
297 {
Max Reitz05cc7582017-06-13 19:20:06 +0200298 int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
299 "blkverify:%s:%s",
300 bs->file->bs->exact_filename,
301 s->test_file->bs->exact_filename);
302 if (ret >= sizeof(bs->exact_filename)) {
303 /* An overflow makes the filename unusable, so do not report any */
304 bs->exact_filename[0] = 0;
305 }
Max Reitz74b36b22014-07-18 20:24:58 +0200306 }
307}
308
Max Reitz27953572019-02-01 20:29:19 +0100309static char *blkverify_dirname(BlockDriverState *bs, Error **errp)
310{
311 /* In general, there are two BDSs with different dirnames below this one;
312 * so there is no unique dirname we could return (unless both are equal by
313 * chance). Therefore, to be consistent, just always return NULL. */
314 error_setg(errp, "Cannot generate a base directory for blkverify nodes");
315 return NULL;
316}
317
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100318static BlockDriver bdrv_blkverify = {
Stefan Hajnoczi9d940202014-05-08 16:34:39 +0200319 .format_name = "blkverify",
320 .protocol_name = "blkverify",
321 .instance_size = sizeof(BDRVBlkverifyState),
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100322
Stefan Hajnoczi9d940202014-05-08 16:34:39 +0200323 .bdrv_parse_filename = blkverify_parse_filename,
324 .bdrv_file_open = blkverify_open,
325 .bdrv_close = blkverify_close,
Max Reitz69dca432020-05-13 13:05:39 +0200326 .bdrv_child_perm = bdrv_default_perms,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +0100327 .bdrv_co_getlength = blkverify_co_getlength,
Max Reitz74b36b22014-07-18 20:24:58 +0200328 .bdrv_refresh_filename = blkverify_refresh_filename,
Max Reitz27953572019-02-01 20:29:19 +0100329 .bdrv_dirname = blkverify_dirname,
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100330
Kevin Wolf44b67892016-11-04 21:13:45 +0100331 .bdrv_co_preadv = blkverify_co_preadv,
332 .bdrv_co_pwritev = blkverify_co_pwritev,
333 .bdrv_co_flush = blkverify_co_flush,
Benoît Canetf6186f42013-10-02 14:33:48 +0200334
Stefan Hajnoczi9d940202014-05-08 16:34:39 +0200335 .is_filter = true,
Max Reitz998a6b22020-02-18 11:34:42 +0100336 .bdrv_recurse_can_replace = blkverify_recurse_can_replace,
Stefan Hajnoczid9d33412010-09-21 15:43:03 +0100337};
338
339static void bdrv_blkverify_init(void)
340{
341 bdrv_register(&bdrv_blkverify);
342}
343
344block_init(bdrv_blkverify_init);