block/amend: separate amend and create options for qemu-img
[qemu/ar7.git] / block / blkverify.c
blob2f261de24b350a4e4abd6e6998c9b0f884857e44
1 /*
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.
8 */
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */
13 #include "block/block_int.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qstring.h"
16 #include "qemu/cutils.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
20 typedef struct {
21 BdrvChild *test_file;
22 } BDRVBlkverifyState;
24 typedef struct BlkverifyRequest {
25 Coroutine *co;
26 BlockDriverState *bs;
28 /* Request metadata */
29 bool is_write;
30 uint64_t offset;
31 uint64_t bytes;
32 int flags;
34 int (*request_fn)(BdrvChild *, int64_t, unsigned int, QEMUIOVector *,
35 BdrvRequestFlags);
37 int ret; /* test image result */
38 int raw_ret; /* raw image result */
40 unsigned int done; /* completion counter */
42 QEMUIOVector *qiov; /* user I/O vector */
43 QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */
44 } BlkverifyRequest;
46 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyRequest *r,
47 const char *fmt, ...)
49 va_list ap;
51 va_start(ap, fmt);
52 fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ",
53 r->is_write ? "write" : "read", r->offset, r->bytes);
54 vfprintf(stderr, fmt, ap);
55 fprintf(stderr, "\n");
56 va_end(ap);
57 exit(1);
60 /* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
61 static void blkverify_parse_filename(const char *filename, QDict *options,
62 Error **errp)
64 const char *c;
65 QString *raw_path;
68 /* Parse the blkverify: prefix */
69 if (!strstart(filename, "blkverify:", &filename)) {
70 /* There was no prefix; therefore, all options have to be already
71 present in the QDict (except for the filename) */
72 qdict_put_str(options, "x-image", filename);
73 return;
76 /* Parse the raw image filename */
77 c = strchr(filename, ':');
78 if (c == NULL) {
79 error_setg(errp, "blkverify requires raw copy and original image path");
80 return;
83 /* TODO Implement option pass-through and set raw.filename here */
84 raw_path = qstring_from_substr(filename, 0, c - filename);
85 qdict_put(options, "x-raw", raw_path);
87 /* TODO Allow multi-level nesting and set file.filename here */
88 filename = c + 1;
89 qdict_put_str(options, "x-image", filename);
92 static QemuOptsList runtime_opts = {
93 .name = "blkverify",
94 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
95 .desc = {
97 .name = "x-raw",
98 .type = QEMU_OPT_STRING,
99 .help = "[internal use only, will be removed]",
102 .name = "x-image",
103 .type = QEMU_OPT_STRING,
104 .help = "[internal use only, will be removed]",
106 { /* end of list */ }
110 static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
111 Error **errp)
113 BDRVBlkverifyState *s = bs->opaque;
114 QemuOpts *opts;
115 Error *local_err = NULL;
116 int ret;
118 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
119 qemu_opts_absorb_qdict(opts, options, &local_err);
120 if (local_err) {
121 error_propagate(errp, local_err);
122 ret = -EINVAL;
123 goto fail;
126 /* Open the raw file */
127 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw",
128 bs, &child_of_bds,
129 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
130 false, &local_err);
131 if (local_err) {
132 ret = -EINVAL;
133 error_propagate(errp, local_err);
134 goto fail;
137 /* Open the test file */
138 s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
139 "test", bs, &child_of_bds, BDRV_CHILD_DATA,
140 false, &local_err);
141 if (local_err) {
142 ret = -EINVAL;
143 error_propagate(errp, local_err);
144 goto fail;
147 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
148 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
150 ret = 0;
151 fail:
152 qemu_opts_del(opts);
153 return ret;
156 static void blkverify_close(BlockDriverState *bs)
158 BDRVBlkverifyState *s = bs->opaque;
160 bdrv_unref_child(bs, s->test_file);
161 s->test_file = NULL;
164 static int64_t blkverify_getlength(BlockDriverState *bs)
166 BDRVBlkverifyState *s = bs->opaque;
168 return bdrv_getlength(s->test_file->bs);
171 static void coroutine_fn blkverify_do_test_req(void *opaque)
173 BlkverifyRequest *r = opaque;
174 BDRVBlkverifyState *s = r->bs->opaque;
176 r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
177 r->flags);
178 r->done++;
179 qemu_coroutine_enter_if_inactive(r->co);
182 static void coroutine_fn blkverify_do_raw_req(void *opaque)
184 BlkverifyRequest *r = opaque;
186 r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
187 r->flags);
188 r->done++;
189 qemu_coroutine_enter_if_inactive(r->co);
192 static int coroutine_fn
193 blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
194 uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
195 int flags, bool is_write)
197 Coroutine *co_a, *co_b;
199 *r = (BlkverifyRequest) {
200 .co = qemu_coroutine_self(),
201 .bs = bs,
202 .offset = offset,
203 .bytes = bytes,
204 .qiov = qiov,
205 .raw_qiov = raw_qiov,
206 .flags = flags,
207 .is_write = is_write,
208 .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
211 co_a = qemu_coroutine_create(blkverify_do_test_req, r);
212 co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
214 qemu_coroutine_enter(co_a);
215 qemu_coroutine_enter(co_b);
217 while (r->done < 2) {
218 qemu_coroutine_yield();
221 if (r->ret != r->raw_ret) {
222 blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
225 return r->ret;
228 static int coroutine_fn
229 blkverify_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
230 QEMUIOVector *qiov, int flags)
232 BlkverifyRequest r;
233 QEMUIOVector raw_qiov;
234 void *buf;
235 ssize_t cmp_offset;
236 int ret;
238 buf = qemu_blockalign(bs->file->bs, qiov->size);
239 qemu_iovec_init(&raw_qiov, qiov->niov);
240 qemu_iovec_clone(&raw_qiov, qiov, buf);
242 ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, flags,
243 false);
245 cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
246 if (cmp_offset != -1) {
247 blkverify_err(&r, "contents mismatch at offset %" PRId64,
248 offset + cmp_offset);
251 qemu_iovec_destroy(&raw_qiov);
252 qemu_vfree(buf);
254 return ret;
257 static int coroutine_fn
258 blkverify_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
259 QEMUIOVector *qiov, int flags)
261 BlkverifyRequest r;
262 return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
265 static int blkverify_co_flush(BlockDriverState *bs)
267 BDRVBlkverifyState *s = bs->opaque;
269 /* Only flush test file, the raw file is not important */
270 return bdrv_co_flush(s->test_file->bs);
273 static bool blkverify_recurse_can_replace(BlockDriverState *bs,
274 BlockDriverState *to_replace)
276 BDRVBlkverifyState *s = bs->opaque;
279 * blkverify quits the whole qemu process if there is a mismatch
280 * between bs->file->bs and s->test_file->bs. Therefore, we know
281 * know that both must match bs and we can recurse down to either.
283 return bdrv_recurse_can_replace(bs->file->bs, to_replace) ||
284 bdrv_recurse_can_replace(s->test_file->bs, to_replace);
287 static void blkverify_refresh_filename(BlockDriverState *bs)
289 BDRVBlkverifyState *s = bs->opaque;
291 if (bs->file->bs->exact_filename[0]
292 && s->test_file->bs->exact_filename[0])
294 int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
295 "blkverify:%s:%s",
296 bs->file->bs->exact_filename,
297 s->test_file->bs->exact_filename);
298 if (ret >= sizeof(bs->exact_filename)) {
299 /* An overflow makes the filename unusable, so do not report any */
300 bs->exact_filename[0] = 0;
305 static char *blkverify_dirname(BlockDriverState *bs, Error **errp)
307 /* In general, there are two BDSs with different dirnames below this one;
308 * so there is no unique dirname we could return (unless both are equal by
309 * chance). Therefore, to be consistent, just always return NULL. */
310 error_setg(errp, "Cannot generate a base directory for blkverify nodes");
311 return NULL;
314 static BlockDriver bdrv_blkverify = {
315 .format_name = "blkverify",
316 .protocol_name = "blkverify",
317 .instance_size = sizeof(BDRVBlkverifyState),
319 .bdrv_parse_filename = blkverify_parse_filename,
320 .bdrv_file_open = blkverify_open,
321 .bdrv_close = blkverify_close,
322 .bdrv_child_perm = bdrv_default_perms,
323 .bdrv_getlength = blkverify_getlength,
324 .bdrv_refresh_filename = blkverify_refresh_filename,
325 .bdrv_dirname = blkverify_dirname,
327 .bdrv_co_preadv = blkverify_co_preadv,
328 .bdrv_co_pwritev = blkverify_co_pwritev,
329 .bdrv_co_flush = blkverify_co_flush,
331 .is_filter = true,
332 .bdrv_recurse_can_replace = blkverify_recurse_can_replace,
335 static void bdrv_blkverify_init(void)
337 bdrv_register(&bdrv_blkverify);
340 block_init(bdrv_blkverify_init);