]> Git Repo - qemu.git/blob - block/blkverify.c
block: use int64_t instead of uint64_t in driver read handlers
[qemu.git] / block / blkverify.c
1 /*
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
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"
19
20 typedef struct {
21     BdrvChild *test_file;
22 } BDRVBlkverifyState;
23
24 typedef struct BlkverifyRequest {
25     Coroutine *co;
26     BlockDriverState *bs;
27
28     /* Request metadata */
29     bool is_write;
30     uint64_t offset;
31     uint64_t bytes;
32     int flags;
33
34     int (*request_fn)(BdrvChild *, int64_t, int64_t, QEMUIOVector *,
35                       BdrvRequestFlags);
36
37     int ret;                    /* test image result */
38     int raw_ret;                /* raw image result */
39
40     unsigned int done;          /* completion counter */
41
42     QEMUIOVector *qiov;         /* user I/O vector */
43     QEMUIOVector *raw_qiov;     /* cloned I/O vector for raw file */
44 } BlkverifyRequest;
45
46 static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyRequest *r,
47                                              const char *fmt, ...)
48 {
49     va_list ap;
50
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);
58 }
59
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)
63 {
64     const char *c;
65     QString *raw_path;
66
67
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;
74     }
75
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;
81     }
82
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);
86
87     /* TODO Allow multi-level nesting and set file.filename here */
88     filename = c + 1;
89     qdict_put_str(options, "x-image", filename);
90 }
91
92 static QemuOptsList runtime_opts = {
93     .name = "blkverify",
94     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
95     .desc = {
96         {
97             .name = "x-raw",
98             .type = QEMU_OPT_STRING,
99             .help = "[internal use only, will be removed]",
100         },
101         {
102             .name = "x-image",
103             .type = QEMU_OPT_STRING,
104             .help = "[internal use only, will be removed]",
105         },
106         { /* end of list */ }
107     },
108 };
109
110 static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
111                           Error **errp)
112 {
113     BDRVBlkverifyState *s = bs->opaque;
114     QemuOpts *opts;
115     int ret;
116
117     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
118     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
119         ret = -EINVAL;
120         goto fail;
121     }
122
123     /* Open the raw file */
124     bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw",
125                                bs, &child_of_bds,
126                                BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
127                                false, errp);
128     if (!bs->file) {
129         ret = -EINVAL;
130         goto fail;
131     }
132
133     /* Open the test file */
134     s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
135                                    "test", bs, &child_of_bds, BDRV_CHILD_DATA,
136                                    false, errp);
137     if (!s->test_file) {
138         ret = -EINVAL;
139         goto fail;
140     }
141
142     bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
143     bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
144
145     ret = 0;
146 fail:
147     qemu_opts_del(opts);
148     return ret;
149 }
150
151 static void blkverify_close(BlockDriverState *bs)
152 {
153     BDRVBlkverifyState *s = bs->opaque;
154
155     bdrv_unref_child(bs, s->test_file);
156     s->test_file = NULL;
157 }
158
159 static int64_t blkverify_getlength(BlockDriverState *bs)
160 {
161     BDRVBlkverifyState *s = bs->opaque;
162
163     return bdrv_getlength(s->test_file->bs);
164 }
165
166 static void coroutine_fn blkverify_do_test_req(void *opaque)
167 {
168     BlkverifyRequest *r = opaque;
169     BDRVBlkverifyState *s = r->bs->opaque;
170
171     r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
172                            r->flags);
173     r->done++;
174     qemu_coroutine_enter_if_inactive(r->co);
175 }
176
177 static void coroutine_fn blkverify_do_raw_req(void *opaque)
178 {
179     BlkverifyRequest *r = opaque;
180
181     r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
182                                r->flags);
183     r->done++;
184     qemu_coroutine_enter_if_inactive(r->co);
185 }
186
187 static int coroutine_fn
188 blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
189                   uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
190                   int flags, bool is_write)
191 {
192     Coroutine *co_a, *co_b;
193
194     *r = (BlkverifyRequest) {
195         .co         = qemu_coroutine_self(),
196         .bs         = bs,
197         .offset     = offset,
198         .bytes      = bytes,
199         .qiov       = qiov,
200         .raw_qiov   = raw_qiov,
201         .flags      = flags,
202         .is_write   = is_write,
203         .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
204     };
205
206     co_a = qemu_coroutine_create(blkverify_do_test_req, r);
207     co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
208
209     qemu_coroutine_enter(co_a);
210     qemu_coroutine_enter(co_b);
211
212     while (r->done < 2) {
213         qemu_coroutine_yield();
214     }
215
216     if (r->ret != r->raw_ret) {
217         blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
218     }
219
220     return r->ret;
221 }
222
223 static int coroutine_fn
224 blkverify_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
225                     QEMUIOVector *qiov, BdrvRequestFlags flags)
226 {
227     BlkverifyRequest r;
228     QEMUIOVector raw_qiov;
229     void *buf;
230     ssize_t cmp_offset;
231     int ret;
232
233     buf = qemu_blockalign(bs->file->bs, qiov->size);
234     qemu_iovec_init(&raw_qiov, qiov->niov);
235     qemu_iovec_clone(&raw_qiov, qiov, buf);
236
237     ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, flags,
238                             false);
239
240     cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
241     if (cmp_offset != -1) {
242         blkverify_err(&r, "contents mismatch at offset %" PRId64,
243                       offset + cmp_offset);
244     }
245
246     qemu_iovec_destroy(&raw_qiov);
247     qemu_vfree(buf);
248
249     return ret;
250 }
251
252 static int coroutine_fn
253 blkverify_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
254                      QEMUIOVector *qiov, int flags)
255 {
256     BlkverifyRequest r;
257     return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
258 }
259
260 static int blkverify_co_flush(BlockDriverState *bs)
261 {
262     BDRVBlkverifyState *s = bs->opaque;
263
264     /* Only flush test file, the raw file is not important */
265     return bdrv_co_flush(s->test_file->bs);
266 }
267
268 static bool blkverify_recurse_can_replace(BlockDriverState *bs,
269                                           BlockDriverState *to_replace)
270 {
271     BDRVBlkverifyState *s = bs->opaque;
272
273     /*
274      * blkverify quits the whole qemu process if there is a mismatch
275      * between bs->file->bs and s->test_file->bs.  Therefore, we know
276      * know that both must match bs and we can recurse down to either.
277      */
278     return bdrv_recurse_can_replace(bs->file->bs, to_replace) ||
279            bdrv_recurse_can_replace(s->test_file->bs, to_replace);
280 }
281
282 static void blkverify_refresh_filename(BlockDriverState *bs)
283 {
284     BDRVBlkverifyState *s = bs->opaque;
285
286     if (bs->file->bs->exact_filename[0]
287         && s->test_file->bs->exact_filename[0])
288     {
289         int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
290                            "blkverify:%s:%s",
291                            bs->file->bs->exact_filename,
292                            s->test_file->bs->exact_filename);
293         if (ret >= sizeof(bs->exact_filename)) {
294             /* An overflow makes the filename unusable, so do not report any */
295             bs->exact_filename[0] = 0;
296         }
297     }
298 }
299
300 static char *blkverify_dirname(BlockDriverState *bs, Error **errp)
301 {
302     /* In general, there are two BDSs with different dirnames below this one;
303      * so there is no unique dirname we could return (unless both are equal by
304      * chance). Therefore, to be consistent, just always return NULL. */
305     error_setg(errp, "Cannot generate a base directory for blkverify nodes");
306     return NULL;
307 }
308
309 static BlockDriver bdrv_blkverify = {
310     .format_name                      = "blkverify",
311     .protocol_name                    = "blkverify",
312     .instance_size                    = sizeof(BDRVBlkverifyState),
313
314     .bdrv_parse_filename              = blkverify_parse_filename,
315     .bdrv_file_open                   = blkverify_open,
316     .bdrv_close                       = blkverify_close,
317     .bdrv_child_perm                  = bdrv_default_perms,
318     .bdrv_getlength                   = blkverify_getlength,
319     .bdrv_refresh_filename            = blkverify_refresh_filename,
320     .bdrv_dirname                     = blkverify_dirname,
321
322     .bdrv_co_preadv                   = blkverify_co_preadv,
323     .bdrv_co_pwritev                  = blkverify_co_pwritev,
324     .bdrv_co_flush                    = blkverify_co_flush,
325
326     .is_filter                        = true,
327     .bdrv_recurse_can_replace         = blkverify_recurse_can_replace,
328 };
329
330 static void bdrv_blkverify_init(void)
331 {
332     bdrv_register(&bdrv_blkverify);
333 }
334
335 block_init(bdrv_blkverify_init);
This page took 0.038332 seconds and 4 git commands to generate.