Commit | Line | Data |
---|---|---|
e29d1b70 JA |
1 | /* |
2 | * IO verification helpers | |
3 | */ | |
4 | #include <unistd.h> | |
5 | #include <fcntl.h> | |
6 | #include <string.h> | |
97af62ce | 7 | #include <assert.h> |
e29d1b70 JA |
8 | |
9 | #include "fio.h" | |
e29d1b70 | 10 | |
cd14cc10 JA |
11 | #include "md5.h" |
12 | #include "crc64.h" | |
13 | #include "crc32.h" | |
14 | #include "crc16.h" | |
15 | #include "crc7.h" | |
16 | #include "sha256.h" | |
17 | #include "sha512.h" | |
18 | ||
90059d65 | 19 | static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len) |
e29d1b70 JA |
20 | { |
21 | unsigned int todo; | |
4c5946c6 | 22 | int r; |
e29d1b70 JA |
23 | |
24 | while (len) { | |
4c5946c6 | 25 | r = os_random_long(&td->verify_state); |
e29d1b70 JA |
26 | |
27 | /* | |
28 | * lrand48_r seems to be broken and only fill the bottom | |
29 | * 32-bits, even on 64-bit archs with 64-bit longs | |
30 | */ | |
31 | todo = sizeof(r); | |
32 | if (todo > len) | |
33 | todo = len; | |
34 | ||
35 | memcpy(p, &r, todo); | |
36 | ||
37 | len -= todo; | |
38 | p += todo; | |
39 | } | |
40 | } | |
41 | ||
90059d65 JA |
42 | static void fill_pattern(struct thread_data *td, void *p, unsigned int len) |
43 | { | |
44 | switch (td->o.verify_pattern_bytes) { | |
45 | case 0: | |
46 | fill_random_bytes(td, p, len); | |
47 | break; | |
48 | case 1: | |
49 | memset(p, td->o.verify_pattern, len); | |
50 | break; | |
51 | case 2: | |
52 | case 3: | |
53 | case 4: { | |
54 | unsigned int pattern = td->o.verify_pattern; | |
55 | unsigned int i = 0; | |
56 | unsigned char c1, c2, c3, c4; | |
57 | unsigned char *b = p; | |
58 | ||
59 | c1 = pattern & 0xff; | |
60 | pattern >>= 8; | |
61 | c2 = pattern & 0xff; | |
62 | pattern >>= 8; | |
63 | c3 = pattern & 0xff; | |
64 | pattern >>= 8; | |
65 | c4 = pattern & 0xff; | |
66 | ||
67 | while (i < len) { | |
68 | b[i++] = c1; | |
69 | if (i == len) | |
70 | break; | |
71 | b[i++] = c2; | |
72 | if (td->o.verify_pattern_bytes == 2 || i == len) | |
73 | continue; | |
74 | b[i++] = c3; | |
75 | if (td->o.verify_pattern_bytes == 3 || i == len) | |
76 | continue; | |
77 | b[i++] = c4; | |
78 | } | |
79 | break; | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | static void memswp(void* buf1, void* buf2, unsigned int len) | |
546a9142 SL |
85 | { |
86 | struct verify_header swap; | |
90059d65 | 87 | |
546a9142 SL |
88 | memcpy(&swap, buf1, len); |
89 | memcpy(buf1, buf2, len); | |
90 | memcpy(buf2, &swap, len); | |
91 | } | |
92 | ||
e29d1b70 JA |
93 | static void hexdump(void *buffer, int len) |
94 | { | |
95 | unsigned char *p = buffer; | |
96 | int i; | |
97 | ||
98 | for (i = 0; i < len; i++) | |
6d86144d JA |
99 | log_info("%02x", p[i]); |
100 | log_info("\n"); | |
e29d1b70 JA |
101 | } |
102 | ||
d9f2caf3 JA |
103 | /* |
104 | * Return data area 'header_num' | |
105 | */ | |
106 | static inline void *io_u_verify_off(struct verify_header *hdr, | |
107 | struct io_u *io_u, | |
108 | unsigned char header_num) | |
109 | { | |
110 | return io_u->buf + sizeof(*hdr) + header_num * hdr->len; | |
111 | } | |
112 | ||
cd14cc10 JA |
113 | static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u, |
114 | unsigned int header_num) | |
115 | { | |
116 | void *p = io_u_verify_off(hdr, io_u, header_num); | |
117 | uint8_t sha512[128]; | |
118 | struct sha512_ctx sha512_ctx = { | |
119 | .buf = sha512, | |
120 | }; | |
121 | ||
122 | sha512_init(&sha512_ctx); | |
123 | sha512_update(&sha512_ctx, p, hdr->len - sizeof(*hdr)); | |
124 | ||
125 | if (memcmp(hdr->sha512, sha512_ctx.buf, sizeof(sha512))) { | |
126 | log_err("sha512: verify failed at %llu/%u\n", | |
127 | io_u->offset + header_num * hdr->len, | |
128 | hdr->len); | |
129 | hexdump(hdr->sha512, sizeof(hdr->sha512)); | |
130 | hexdump(sha512_ctx.buf, sizeof(sha512)); | |
131 | return 1; | |
132 | } | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u, | |
138 | unsigned int header_num) | |
139 | { | |
140 | void *p = io_u_verify_off(hdr, io_u, header_num); | |
141 | uint8_t sha256[128]; | |
142 | struct sha256_ctx sha256_ctx = { | |
143 | .buf = sha256, | |
144 | }; | |
145 | ||
146 | sha256_init(&sha256_ctx); | |
147 | sha256_update(&sha256_ctx, p, hdr->len - sizeof(*hdr)); | |
148 | ||
149 | if (memcmp(hdr->sha256, sha256_ctx.buf, sizeof(sha256))) { | |
150 | log_err("sha256: verify failed at %llu/%u\n", | |
151 | io_u->offset + header_num * hdr->len, | |
152 | hdr->len); | |
153 | hexdump(hdr->sha256, sizeof(hdr->sha256)); | |
154 | hexdump(sha256_ctx.buf, sizeof(sha256)); | |
155 | return 1; | |
156 | } | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
3f9f4e26 SL |
161 | static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u, |
162 | unsigned char header_num) | |
1e154bdb | 163 | { |
d9f2caf3 | 164 | void *p = io_u_verify_off(hdr, io_u, header_num); |
1e154bdb JA |
165 | unsigned char c; |
166 | ||
1e154bdb JA |
167 | c = crc7(p, hdr->len - sizeof(*hdr)); |
168 | ||
169 | if (c != hdr->crc7) { | |
3f9f4e26 SL |
170 | log_err("crc7: verify failed at %llu/%u\n", |
171 | io_u->offset + header_num * hdr->len, | |
172 | hdr->len); | |
1e154bdb JA |
173 | log_err("crc7: wanted %x, got %x\n", hdr->crc7, c); |
174 | return 1; | |
175 | } | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
3f9f4e26 SL |
180 | static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u, |
181 | unsigned int header_num) | |
969f7ed3 | 182 | { |
d9f2caf3 | 183 | void *p = io_u_verify_off(hdr, io_u, header_num); |
969f7ed3 JA |
184 | unsigned short c; |
185 | ||
969f7ed3 JA |
186 | c = crc16(p, hdr->len - sizeof(*hdr)); |
187 | ||
188 | if (c != hdr->crc16) { | |
3f9f4e26 SL |
189 | log_err("crc16: verify failed at %llu/%u\n", |
190 | io_u->offset + header_num * hdr->len, | |
191 | hdr->len); | |
1f24ea44 | 192 | log_err("crc16: wanted %x, got %x\n", hdr->crc16, c); |
969f7ed3 JA |
193 | return 1; |
194 | } | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
3f9f4e26 SL |
199 | static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u, |
200 | unsigned int header_num) | |
d77a7af3 | 201 | { |
d9f2caf3 | 202 | void *p = io_u_verify_off(hdr, io_u, header_num); |
d77a7af3 JA |
203 | unsigned long long c; |
204 | ||
205 | c = crc64(p, hdr->len - sizeof(*hdr)); | |
206 | ||
207 | if (c != hdr->crc64) { | |
3f9f4e26 SL |
208 | log_err("crc64: verify failed at %llu/%u\n", |
209 | io_u->offset + header_num * hdr->len, | |
210 | hdr->len); | |
d77a7af3 JA |
211 | log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c); |
212 | return 1; | |
213 | } | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
3f9f4e26 SL |
218 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u, |
219 | unsigned int header_num) | |
e29d1b70 | 220 | { |
d9f2caf3 | 221 | void *p = io_u_verify_off(hdr, io_u, header_num); |
e29d1b70 JA |
222 | unsigned long c; |
223 | ||
e29d1b70 JA |
224 | c = crc32(p, hdr->len - sizeof(*hdr)); |
225 | ||
226 | if (c != hdr->crc32) { | |
3f9f4e26 SL |
227 | log_err("crc32: verify failed at %llu/%u\n", |
228 | io_u->offset + header_num * hdr->len, | |
229 | hdr->len); | |
e29d1b70 JA |
230 | log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c); |
231 | return 1; | |
232 | } | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
3f9f4e26 SL |
237 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u, |
238 | unsigned int header_num) | |
e29d1b70 | 239 | { |
d9f2caf3 | 240 | void *p = io_u_verify_off(hdr, io_u, header_num); |
8c432325 JA |
241 | uint32_t hash[MD5_HASH_WORDS]; |
242 | struct md5_ctx md5_ctx = { | |
243 | .hash = hash, | |
244 | }; | |
e29d1b70 | 245 | |
61f821f1 | 246 | md5_init(&md5_ctx); |
e29d1b70 JA |
247 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); |
248 | ||
c9b3fdf3 | 249 | if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) { |
3f9f4e26 SL |
250 | log_err("md5: verify failed at %llu/%u\n", |
251 | io_u->offset + header_num * hdr->len, | |
252 | hdr->len); | |
e29d1b70 | 253 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); |
8c432325 | 254 | hexdump(md5_ctx.hash, sizeof(hash)); |
e29d1b70 JA |
255 | return 1; |
256 | } | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
36690c9b | 261 | int verify_io_u(struct thread_data *td, struct io_u *io_u) |
e29d1b70 | 262 | { |
3f9f4e26 SL |
263 | struct verify_header *hdr; |
264 | unsigned int hdr_inc, hdr_num = 0; | |
95646108 | 265 | void *p; |
e29d1b70 JA |
266 | int ret; |
267 | ||
1dcc0498 | 268 | if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ) |
36690c9b JA |
269 | return 0; |
270 | ||
3f9f4e26 | 271 | hdr_inc = io_u->buflen; |
a59e170d JA |
272 | if (td->o.verify_interval) |
273 | hdr_inc = td->o.verify_interval; | |
e29d1b70 | 274 | |
95646108 | 275 | for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) { |
a59e170d JA |
276 | if (td->o.verify_offset) |
277 | memswp(p, p + td->o.verify_offset, sizeof(*hdr)); | |
546a9142 | 278 | |
95646108 | 279 | hdr = p; |
e29d1b70 | 280 | |
3f9f4e26 SL |
281 | if (hdr->fio_magic != FIO_HDR_MAGIC) { |
282 | log_err("Bad verify header %x\n", hdr->fio_magic); | |
283 | return EIO; | |
284 | } | |
285 | ||
286 | switch (hdr->verify_type) { | |
287 | case VERIFY_MD5: | |
288 | ret = verify_io_u_md5(hdr, io_u, hdr_num); | |
289 | break; | |
290 | case VERIFY_CRC64: | |
291 | ret = verify_io_u_crc64(hdr, io_u, hdr_num); | |
292 | break; | |
293 | case VERIFY_CRC32: | |
294 | ret = verify_io_u_crc32(hdr, io_u, hdr_num); | |
295 | break; | |
296 | case VERIFY_CRC16: | |
297 | ret = verify_io_u_crc16(hdr, io_u, hdr_num); | |
298 | break; | |
299 | case VERIFY_CRC7: | |
300 | ret = verify_io_u_crc7(hdr, io_u, hdr_num); | |
301 | break; | |
cd14cc10 JA |
302 | case VERIFY_SHA256: |
303 | ret = verify_io_u_sha256(hdr, io_u, hdr_num); | |
304 | break; | |
305 | case VERIFY_SHA512: | |
306 | ret = verify_io_u_sha512(hdr, io_u, hdr_num); | |
307 | break; | |
3f9f4e26 SL |
308 | default: |
309 | log_err("Bad verify type %u\n", hdr->verify_type); | |
310 | ret = 1; | |
311 | } | |
312 | hdr_num++; | |
313 | } | |
a7dfe862 JA |
314 | |
315 | return 0; | |
e29d1b70 JA |
316 | } |
317 | ||
cd14cc10 JA |
318 | static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len) |
319 | { | |
320 | struct sha512_ctx sha512_ctx = { | |
321 | .buf = hdr->sha512, | |
322 | }; | |
323 | ||
324 | sha512_init(&sha512_ctx); | |
325 | sha512_update(&sha512_ctx, p, len); | |
326 | } | |
327 | ||
328 | static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len) | |
329 | { | |
330 | struct sha256_ctx sha256_ctx = { | |
331 | .buf = hdr->sha256, | |
332 | }; | |
333 | ||
334 | sha256_init(&sha256_ctx); | |
335 | sha256_update(&sha256_ctx, p, len); | |
336 | } | |
337 | ||
1e154bdb JA |
338 | static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len) |
339 | { | |
340 | hdr->crc7 = crc7(p, len); | |
341 | } | |
342 | ||
969f7ed3 JA |
343 | static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len) |
344 | { | |
345 | hdr->crc16 = crc16(p, len); | |
346 | } | |
347 | ||
e29d1b70 JA |
348 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
349 | { | |
350 | hdr->crc32 = crc32(p, len); | |
351 | } | |
352 | ||
d77a7af3 JA |
353 | static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len) |
354 | { | |
355 | hdr->crc64 = crc64(p, len); | |
356 | } | |
357 | ||
e29d1b70 JA |
358 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
359 | { | |
8c432325 JA |
360 | struct md5_ctx md5_ctx = { |
361 | .hash = (uint32_t *) hdr->md5_digest, | |
362 | }; | |
e29d1b70 | 363 | |
61f821f1 | 364 | md5_init(&md5_ctx); |
e29d1b70 | 365 | md5_update(&md5_ctx, p, len); |
e29d1b70 JA |
366 | } |
367 | ||
368 | /* | |
369 | * fill body of io_u->buf with random data and add a header with the | |
370 | * crc32 or md5 sum of that data. | |
371 | */ | |
372 | void populate_verify_io_u(struct thread_data *td, struct io_u *io_u) | |
373 | { | |
baefa9be | 374 | struct verify_header *hdr; |
95646108 | 375 | void *p = io_u->buf, *data; |
546a9142 | 376 | unsigned int hdr_inc, data_len; |
e29d1b70 | 377 | |
9cc3d150 JA |
378 | if (td->o.verify == VERIFY_NULL) |
379 | return; | |
380 | ||
90059d65 | 381 | fill_pattern(td, p, io_u->buflen); |
546a9142 SL |
382 | |
383 | hdr_inc = io_u->buflen; | |
a59e170d JA |
384 | if (td->o.verify_interval) |
385 | hdr_inc = td->o.verify_interval; | |
546a9142 | 386 | data_len = hdr_inc - sizeof(*hdr); |
baefa9be | 387 | |
95646108 JA |
388 | for (;p < io_u->buf + io_u->buflen; p += hdr_inc) { |
389 | hdr = p; | |
3f9f4e26 SL |
390 | |
391 | hdr->fio_magic = FIO_HDR_MAGIC; | |
392 | hdr->verify_type = td->o.verify; | |
546a9142 | 393 | hdr->len = hdr_inc; |
3f9f4e26 SL |
394 | |
395 | data = p + sizeof(*hdr); | |
3f9f4e26 SL |
396 | switch (td->o.verify) { |
397 | case VERIFY_MD5: | |
398 | fill_md5(hdr, data, data_len); | |
399 | break; | |
400 | case VERIFY_CRC64: | |
401 | fill_crc64(hdr, data, data_len); | |
402 | break; | |
403 | case VERIFY_CRC32: | |
404 | fill_crc32(hdr, data, data_len); | |
405 | break; | |
406 | case VERIFY_CRC16: | |
407 | fill_crc16(hdr, data, data_len); | |
408 | break; | |
409 | case VERIFY_CRC7: | |
410 | fill_crc7(hdr, data, data_len); | |
411 | break; | |
cd14cc10 JA |
412 | case VERIFY_SHA256: |
413 | fill_sha256(hdr, data, data_len); | |
414 | break; | |
415 | case VERIFY_SHA512: | |
416 | fill_sha512(hdr, data, data_len); | |
417 | break; | |
3f9f4e26 SL |
418 | default: |
419 | log_err("fio: bad verify type: %d\n", td->o.verify); | |
420 | assert(0); | |
421 | } | |
a59e170d JA |
422 | if (td->o.verify_offset) |
423 | memswp(p, p + td->o.verify_offset, sizeof(*hdr)); | |
e29d1b70 | 424 | } |
e29d1b70 JA |
425 | } |
426 | ||
427 | int get_next_verify(struct thread_data *td, struct io_u *io_u) | |
428 | { | |
8de8f047 | 429 | struct io_piece *ipo = NULL; |
e29d1b70 | 430 | |
d2d7fa53 JA |
431 | /* |
432 | * this io_u is from a requeue, we already filled the offsets | |
433 | */ | |
434 | if (io_u->file) | |
435 | return 0; | |
436 | ||
8de8f047 JA |
437 | if (!RB_EMPTY_ROOT(&td->io_hist_tree)) { |
438 | struct rb_node *n = rb_first(&td->io_hist_tree); | |
e29d1b70 | 439 | |
8de8f047 | 440 | ipo = rb_entry(n, struct io_piece, rb_node); |
4b87898e | 441 | rb_erase(n, &td->io_hist_tree); |
8de8f047 JA |
442 | } else if (!list_empty(&td->io_hist_list)) { |
443 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
444 | list_del(&ipo->list); | |
445 | } | |
e29d1b70 | 446 | |
8de8f047 | 447 | if (ipo) { |
e29d1b70 JA |
448 | io_u->offset = ipo->offset; |
449 | io_u->buflen = ipo->len; | |
36167d82 | 450 | io_u->file = ipo->file; |
97af62ce JA |
451 | |
452 | if ((io_u->file->flags & FIO_FILE_OPEN) == 0) { | |
453 | int r = td_io_open_file(td, io_u->file); | |
454 | ||
455 | if (r) | |
456 | return 1; | |
457 | } | |
458 | ||
459 | get_file(ipo->file); | |
460 | assert(io_u->file->flags & FIO_FILE_OPEN); | |
e29d1b70 | 461 | io_u->ddir = DDIR_READ; |
36167d82 JA |
462 | io_u->xfer_buf = io_u->buf; |
463 | io_u->xfer_buflen = io_u->buflen; | |
e29d1b70 JA |
464 | free(ipo); |
465 | return 0; | |
466 | } | |
467 | ||
468 | return 1; | |
469 | } |