]>
Commit | Line | Data |
---|---|---|
d9d33417 SH |
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 | ||
80c71a24 | 10 | #include "qemu/osdep.h" |
da34e65c | 11 | #include "qapi/error.h" |
1de7afc9 | 12 | #include "qemu/sockets.h" /* for EINPROGRESS on Windows */ |
e2c1c34f | 13 | #include "block/block-io.h" |
737e150e | 14 | #include "block/block_int.h" |
74b36b2e HR |
15 | #include "qapi/qmp/qdict.h" |
16 | #include "qapi/qmp/qstring.h" | |
f348b6d1 | 17 | #include "qemu/cutils.h" |
0b8fa32f | 18 | #include "qemu/module.h" |
922a01a0 | 19 | #include "qemu/option.h" |
5df022cf | 20 | #include "qemu/memalign.h" |
d9d33417 SH |
21 | |
22 | typedef struct { | |
3e586be0 | 23 | BdrvChild *test_file; |
d9d33417 SH |
24 | } BDRVBlkverifyState; |
25 | ||
44b67892 KW |
26 | typedef struct BlkverifyRequest { |
27 | Coroutine *co; | |
28 | BlockDriverState *bs; | |
d9d33417 SH |
29 | |
30 | /* Request metadata */ | |
31 | bool is_write; | |
44b67892 KW |
32 | uint64_t offset; |
33 | uint64_t bytes; | |
34 | int flags; | |
d9d33417 | 35 | |
244b26d2 KW |
36 | int GRAPH_RDLOCK_PTR (*request_fn)( |
37 | BdrvChild *, int64_t, int64_t, QEMUIOVector *, BdrvRequestFlags); | |
d9d33417 | 38 | |
44b67892 KW |
39 | int ret; /* test image result */ |
40 | int raw_ret; /* raw image result */ | |
d9d33417 | 41 | |
44b67892 | 42 | unsigned int done; /* completion counter */ |
d9d33417 | 43 | |
44b67892 KW |
44 | QEMUIOVector *qiov; /* user I/O vector */ |
45 | QEMUIOVector *raw_qiov; /* cloned I/O vector for raw file */ | |
46 | } BlkverifyRequest; | |
d9d33417 | 47 | |
9edc6313 | 48 | static void G_GNUC_PRINTF(2, 3) blkverify_err(BlkverifyRequest *r, |
a77cffe7 | 49 | const char *fmt, ...) |
d9d33417 SH |
50 | { |
51 | va_list ap; | |
52 | ||
53 | va_start(ap, fmt); | |
44b67892 KW |
54 | fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ", |
55 | r->is_write ? "write" : "read", r->offset, r->bytes); | |
d9d33417 SH |
56 | 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 */ | |
16c79092 KW |
63 | static void blkverify_parse_filename(const char *filename, QDict *options, |
64 | Error **errp) | |
d9d33417 | 65 | { |
16c79092 KW |
66 | const char *c; |
67 | QString *raw_path; | |
68 | ||
d9d33417 SH |
69 | |
70 | /* Parse the blkverify: prefix */ | |
16c79092 | 71 | if (!strstart(filename, "blkverify:", &filename)) { |
22511ad6 HR |
72 | /* There was no prefix; therefore, all options have to be already |
73 | present in the QDict (except for the filename) */ | |
46f5ac20 | 74 | qdict_put_str(options, "x-image", filename); |
16c79092 | 75 | return; |
d9d33417 | 76 | } |
d9d33417 SH |
77 | |
78 | /* Parse the raw image filename */ | |
79 | c = strchr(filename, ':'); | |
80 | if (c == NULL) { | |
16c79092 KW |
81 | error_setg(errp, "blkverify requires raw copy and original image path"); |
82 | return; | |
83 | } | |
84 | ||
85 | /* TODO Implement option pass-through and set raw.filename here */ | |
ba891d68 | 86 | raw_path = qstring_from_substr(filename, 0, c - filename); |
16c79092 KW |
87 | qdict_put(options, "x-raw", raw_path); |
88 | ||
89 | /* TODO Allow multi-level nesting and set file.filename here */ | |
90 | filename = c + 1; | |
46f5ac20 | 91 | qdict_put_str(options, "x-image", filename); |
16c79092 KW |
92 | } |
93 | ||
94 | static 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 | ||
015a1036 HR |
112 | static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, |
113 | Error **errp) | |
16c79092 KW |
114 | { |
115 | BDRVBlkverifyState *s = bs->opaque; | |
116 | QemuOpts *opts; | |
16c79092 KW |
117 | int ret; |
118 | ||
87ea75d5 | 119 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
af175e85 | 120 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
16c79092 KW |
121 | ret = -EINVAL; |
122 | goto fail; | |
123 | } | |
124 | ||
70b6198a | 125 | /* Open the raw file */ |
83930780 VSO |
126 | ret = bdrv_open_file_child(qemu_opt_get(opts, "x-raw"), options, "raw", |
127 | bs, errp); | |
128 | if (ret < 0) { | |
16c79092 | 129 | goto fail; |
d9d33417 | 130 | } |
d9d33417 SH |
131 | |
132 | /* Open the test file */ | |
3e586be0 | 133 | s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, |
36ee58d1 | 134 | "test", bs, &child_of_bds, BDRV_CHILD_DATA, |
bc520249 VSO |
135 | false, errp); |
136 | if (!s->test_file) { | |
3e586be0 | 137 | ret = -EINVAL; |
16c79092 | 138 | goto fail; |
d9d33417 SH |
139 | } |
140 | ||
228345bf HR |
141 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; |
142 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED; | |
143 | ||
16c79092 KW |
144 | ret = 0; |
145 | fail: | |
31585931 | 146 | qemu_opts_del(opts); |
16c79092 | 147 | return ret; |
d9d33417 SH |
148 | } |
149 | ||
150 | static void blkverify_close(BlockDriverState *bs) | |
151 | { | |
152 | BDRVBlkverifyState *s = bs->opaque; | |
153 | ||
6bc30f19 | 154 | bdrv_graph_wrlock(); |
3e586be0 | 155 | bdrv_unref_child(bs, s->test_file); |
d9d33417 | 156 | s->test_file = NULL; |
6bc30f19 | 157 | bdrv_graph_wrunlock(); |
d9d33417 SH |
158 | } |
159 | ||
8ab8140a KW |
160 | static int64_t coroutine_fn GRAPH_RDLOCK |
161 | blkverify_co_getlength(BlockDriverState *bs) | |
d9d33417 SH |
162 | { |
163 | BDRVBlkverifyState *s = bs->opaque; | |
164 | ||
c86422c5 | 165 | return bdrv_co_getlength(s->test_file->bs); |
d9d33417 SH |
166 | } |
167 | ||
44b67892 | 168 | static void coroutine_fn blkverify_do_test_req(void *opaque) |
d9d33417 | 169 | { |
44b67892 KW |
170 | BlkverifyRequest *r = opaque; |
171 | BDRVBlkverifyState *s = r->bs->opaque; | |
172 | ||
244b26d2 | 173 | bdrv_graph_co_rdlock(); |
44b67892 KW |
174 | r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov, |
175 | r->flags); | |
244b26d2 KW |
176 | bdrv_graph_co_rdunlock(); |
177 | ||
44b67892 KW |
178 | r->done++; |
179 | qemu_coroutine_enter_if_inactive(r->co); | |
d9d33417 SH |
180 | } |
181 | ||
44b67892 | 182 | static void coroutine_fn blkverify_do_raw_req(void *opaque) |
d9d33417 | 183 | { |
44b67892 | 184 | BlkverifyRequest *r = opaque; |
d9d33417 | 185 | |
244b26d2 | 186 | bdrv_graph_co_rdlock(); |
44b67892 KW |
187 | r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov, |
188 | r->flags); | |
244b26d2 KW |
189 | bdrv_graph_co_rdunlock(); |
190 | ||
44b67892 KW |
191 | r->done++; |
192 | qemu_coroutine_enter_if_inactive(r->co); | |
d9d33417 SH |
193 | } |
194 | ||
244b26d2 | 195 | static int coroutine_fn GRAPH_RDLOCK |
44b67892 KW |
196 | blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset, |
197 | uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov, | |
198 | int flags, bool is_write) | |
d9d33417 | 199 | { |
44b67892 KW |
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(); | |
222 | } | |
d9d33417 | 223 | |
44b67892 KW |
224 | if (r->ret != r->raw_ret) { |
225 | blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret); | |
d9d33417 | 226 | } |
44b67892 KW |
227 | |
228 | return r->ret; | |
d9d33417 SH |
229 | } |
230 | ||
244b26d2 | 231 | static int coroutine_fn GRAPH_RDLOCK |
f7ef38dd VSO |
232 | blkverify_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, |
233 | QEMUIOVector *qiov, BdrvRequestFlags flags) | |
d9d33417 | 234 | { |
44b67892 KW |
235 | 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 | ||
e8b65355 SH |
245 | ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, |
246 | flags & ~BDRV_REQ_REGISTERED_BUF, false); | |
44b67892 KW |
247 | |
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); | |
d9d33417 | 252 | } |
d9d33417 | 253 | |
44b67892 KW |
254 | qemu_iovec_destroy(&raw_qiov); |
255 | qemu_vfree(buf); | |
256 | ||
257 | return ret; | |
d9d33417 SH |
258 | } |
259 | ||
244b26d2 | 260 | static int coroutine_fn GRAPH_RDLOCK |
e75abeda VSO |
261 | blkverify_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, |
262 | QEMUIOVector *qiov, BdrvRequestFlags flags) | |
d9d33417 | 263 | { |
44b67892 KW |
264 | BlkverifyRequest r; |
265 | return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true); | |
d9d33417 SH |
266 | } |
267 | ||
88095349 | 268 | static int coroutine_fn GRAPH_RDLOCK blkverify_co_flush(BlockDriverState *bs) |
d9d33417 SH |
269 | { |
270 | BDRVBlkverifyState *s = bs->opaque; | |
271 | ||
272 | /* Only flush test file, the raw file is not important */ | |
44b67892 | 273 | return bdrv_co_flush(s->test_file->bs); |
d9d33417 SH |
274 | } |
275 | ||
533c6e4e KW |
276 | static bool GRAPH_RDLOCK |
277 | blkverify_recurse_can_replace(BlockDriverState *bs, | |
278 | BlockDriverState *to_replace) | |
998a6b2f HR |
279 | { |
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 | ||
79a55866 | 291 | static void GRAPH_RDLOCK blkverify_refresh_filename(BlockDriverState *bs) |
74b36b2e HR |
292 | { |
293 | BDRVBlkverifyState *s = bs->opaque; | |
294 | ||
9a4f4c31 KW |
295 | if (bs->file->bs->exact_filename[0] |
296 | && s->test_file->bs->exact_filename[0]) | |
297 | { | |
05cc758a HR |
298 | 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 | } | |
74b36b2e HR |
306 | } |
307 | } | |
308 | ||
27953572 HR |
309 | static 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 | ||
d9d33417 | 318 | static BlockDriver bdrv_blkverify = { |
9d94020b SH |
319 | .format_name = "blkverify", |
320 | .protocol_name = "blkverify", | |
321 | .instance_size = sizeof(BDRVBlkverifyState), | |
322 | ||
323 | .bdrv_parse_filename = blkverify_parse_filename, | |
324 | .bdrv_file_open = blkverify_open, | |
325 | .bdrv_close = blkverify_close, | |
69dca43d | 326 | .bdrv_child_perm = bdrv_default_perms, |
c86422c5 | 327 | .bdrv_co_getlength = blkverify_co_getlength, |
74b36b2e | 328 | .bdrv_refresh_filename = blkverify_refresh_filename, |
27953572 | 329 | .bdrv_dirname = blkverify_dirname, |
16c79092 | 330 | |
44b67892 KW |
331 | .bdrv_co_preadv = blkverify_co_preadv, |
332 | .bdrv_co_pwritev = blkverify_co_pwritev, | |
333 | .bdrv_co_flush = blkverify_co_flush, | |
16c79092 | 334 | |
9d94020b | 335 | .is_filter = true, |
998a6b2f | 336 | .bdrv_recurse_can_replace = blkverify_recurse_can_replace, |
d9d33417 SH |
337 | }; |
338 | ||
339 | static void bdrv_blkverify_init(void) | |
340 | { | |
341 | bdrv_register(&bdrv_blkverify); | |
342 | } | |
343 | ||
344 | block_init(bdrv_blkverify_init); |