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> |
e8462bd8 | 8 | #include <pthread.h> |
79402a12 | 9 | #include <libgen.h> |
e29d1b70 JA |
10 | |
11 | #include "fio.h" | |
4f5af7b2 | 12 | #include "verify.h" |
0d29de83 | 13 | #include "trim.h" |
637ef8d9 | 14 | #include "lib/rand.h" |
51aa2da8 | 15 | #include "lib/hweight.h" |
2cac8fcb | 16 | #include "lib/pattern.h" |
e29d1b70 | 17 | |
eef6eea1 JA |
18 | #include "crc/md5.h" |
19 | #include "crc/crc64.h" | |
20 | #include "crc/crc32.h" | |
bac39e0e | 21 | #include "crc/crc32c.h" |
eef6eea1 JA |
22 | #include "crc/crc16.h" |
23 | #include "crc/crc7.h" | |
24 | #include "crc/sha256.h" | |
25 | #include "crc/sha512.h" | |
7c353ceb | 26 | #include "crc/sha1.h" |
844ea602 | 27 | #include "crc/xxhash.h" |
ae3a5acc | 28 | #include "crc/sha3.h" |
cd14cc10 | 29 | |
7d9fb455 JA |
30 | static void populate_hdr(struct thread_data *td, struct io_u *io_u, |
31 | struct verify_header *hdr, unsigned int header_num, | |
32 | unsigned int header_len); | |
b638d82f RP |
33 | static void __fill_hdr(struct thread_data *td, struct io_u *io_u, |
34 | struct verify_header *hdr, unsigned int header_num, | |
35 | unsigned int header_len, uint64_t rand_seed); | |
7d9fb455 | 36 | |
ce35b1ec JA |
37 | void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len) |
38 | { | |
2cac8fcb | 39 | (void)cpy_pattern(td->o.buffer_pattern, td->o.buffer_pattern_bytes, p, len); |
ce35b1ec JA |
40 | } |
41 | ||
a89ba4b1 JA |
42 | static void __fill_buffer(struct thread_options *o, unsigned long seed, void *p, |
43 | unsigned int len) | |
7fa085ea JA |
44 | { |
45 | __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes); | |
46 | } | |
47 | ||
a89ba4b1 JA |
48 | static unsigned long fill_buffer(struct thread_data *td, void *p, |
49 | unsigned int len) | |
7fa085ea JA |
50 | { |
51 | struct frand_state *fs = &td->verify_state; | |
52 | struct thread_options *o = &td->o; | |
53 | ||
54 | return fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes); | |
55 | } | |
56 | ||
ce35b1ec JA |
57 | void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len, |
58 | struct io_u *io_u, unsigned long seed, int use_seed) | |
59 | { | |
bc769898 JA |
60 | struct thread_options *o = &td->o; |
61 | ||
62 | if (!o->verify_pattern_bytes) { | |
ce35b1ec JA |
63 | dprint(FD_VERIFY, "fill random bytes len=%u\n", len); |
64 | ||
65 | if (use_seed) | |
7fa085ea JA |
66 | __fill_buffer(o, seed, p, len); |
67 | else | |
68 | io_u->rand_seed = fill_buffer(td, p, len); | |
ce35b1ec JA |
69 | return; |
70 | } | |
c4b6117b | 71 | |
61b9861d RP |
72 | /* Skip if we were here and we do not need to patch pattern |
73 | * with format */ | |
74 | if (!td->o.verify_fmt_sz && io_u->buf_filled_len >= len) { | |
ce35b1ec | 75 | dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n", |
bc769898 | 76 | o->verify_pattern_bytes, len); |
ce35b1ec JA |
77 | return; |
78 | } | |
79 | ||
61b9861d RP |
80 | (void)paste_format(td->o.verify_pattern, td->o.verify_pattern_bytes, |
81 | td->o.verify_fmt, td->o.verify_fmt_sz, | |
82 | p, len, io_u); | |
ce35b1ec JA |
83 | io_u->buf_filled_len = len; |
84 | } | |
85 | ||
cfbcd0dc JA |
86 | static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u) |
87 | { | |
88 | unsigned int hdr_inc; | |
89 | ||
90 | hdr_inc = io_u->buflen; | |
8a99fdf6 | 91 | if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen) |
cfbcd0dc JA |
92 | hdr_inc = td->o.verify_interval; |
93 | ||
94 | return hdr_inc; | |
95 | } | |
96 | ||
c50ca7bd JA |
97 | static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u, |
98 | unsigned long seed, int use_seed) | |
99 | { | |
100 | unsigned int hdr_inc, header_num; | |
101 | struct verify_header *hdr; | |
102 | void *p = io_u->buf; | |
103 | ||
ce35b1ec | 104 | fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed); |
c50ca7bd | 105 | |
cfbcd0dc | 106 | hdr_inc = get_hdr_inc(td, io_u); |
c50ca7bd JA |
107 | header_num = 0; |
108 | for (; p < io_u->buf + io_u->buflen; p += hdr_inc) { | |
109 | hdr = p; | |
110 | populate_hdr(td, io_u, hdr, header_num, hdr_inc); | |
111 | header_num++; | |
112 | } | |
113 | } | |
114 | ||
4764aec9 | 115 | static void memswp(void *buf1, void *buf2, unsigned int len) |
546a9142 | 116 | { |
dee6de74 SL |
117 | char swap[200]; |
118 | ||
119 | assert(len <= sizeof(swap)); | |
90059d65 | 120 | |
546a9142 SL |
121 | memcpy(&swap, buf1, len); |
122 | memcpy(buf1, buf2, len); | |
123 | memcpy(buf2, &swap, len); | |
124 | } | |
125 | ||
e29d1b70 JA |
126 | static void hexdump(void *buffer, int len) |
127 | { | |
128 | unsigned char *p = buffer; | |
129 | int i; | |
130 | ||
131 | for (i = 0; i < len; i++) | |
bfacf389 JA |
132 | log_err("%02x", p[i]); |
133 | log_err("\n"); | |
e29d1b70 JA |
134 | } |
135 | ||
87677832 | 136 | /* |
de8f6de9 | 137 | * Prepare for separation of verify_header and checksum header |
87677832 | 138 | */ |
546dfd9f | 139 | static inline unsigned int __hdr_size(int verify_type) |
87677832 | 140 | { |
03e20d68 | 141 | unsigned int len = 0; |
87677832 | 142 | |
546dfd9f JA |
143 | switch (verify_type) { |
144 | case VERIFY_NONE: | |
b638d82f | 145 | case VERIFY_HDR_ONLY: |
546dfd9f | 146 | case VERIFY_NULL: |
ed3a433d | 147 | case VERIFY_PATTERN: |
546dfd9f JA |
148 | len = 0; |
149 | break; | |
150 | case VERIFY_MD5: | |
151 | len = sizeof(struct vhdr_md5); | |
152 | break; | |
153 | case VERIFY_CRC64: | |
154 | len = sizeof(struct vhdr_crc64); | |
155 | break; | |
bac39e0e | 156 | case VERIFY_CRC32C: |
546dfd9f | 157 | case VERIFY_CRC32: |
3845591f | 158 | case VERIFY_CRC32C_INTEL: |
546dfd9f JA |
159 | len = sizeof(struct vhdr_crc32); |
160 | break; | |
161 | case VERIFY_CRC16: | |
162 | len = sizeof(struct vhdr_crc16); | |
163 | break; | |
164 | case VERIFY_CRC7: | |
165 | len = sizeof(struct vhdr_crc7); | |
166 | break; | |
167 | case VERIFY_SHA256: | |
168 | len = sizeof(struct vhdr_sha256); | |
169 | break; | |
170 | case VERIFY_SHA512: | |
171 | len = sizeof(struct vhdr_sha512); | |
172 | break; | |
ae3a5acc JA |
173 | case VERIFY_SHA3_224: |
174 | len = sizeof(struct vhdr_sha3_224); | |
175 | break; | |
176 | case VERIFY_SHA3_256: | |
177 | len = sizeof(struct vhdr_sha3_256); | |
178 | break; | |
179 | case VERIFY_SHA3_384: | |
180 | len = sizeof(struct vhdr_sha3_384); | |
181 | break; | |
182 | case VERIFY_SHA3_512: | |
183 | len = sizeof(struct vhdr_sha3_512); | |
184 | break; | |
844ea602 JA |
185 | case VERIFY_XXHASH: |
186 | len = sizeof(struct vhdr_xxhash); | |
187 | break; | |
7c353ceb JA |
188 | case VERIFY_SHA1: |
189 | len = sizeof(struct vhdr_sha1); | |
190 | break; | |
59245381 | 191 | case VERIFY_PATTERN_NO_HDR: |
ed3a433d | 192 | return 0; |
546dfd9f JA |
193 | default: |
194 | log_err("fio: unknown verify header!\n"); | |
195 | assert(0); | |
196 | } | |
197 | ||
198 | return len + sizeof(struct verify_header); | |
87677832 JA |
199 | } |
200 | ||
ed3a433d JA |
201 | static inline unsigned int hdr_size(struct thread_data *td, |
202 | struct verify_header *hdr) | |
87677832 | 203 | { |
ed3a433d JA |
204 | if (td->o.verify == VERIFY_PATTERN_NO_HDR) |
205 | return 0; | |
206 | ||
546dfd9f JA |
207 | return __hdr_size(hdr->verify_type); |
208 | } | |
209 | ||
210 | static void *hdr_priv(struct verify_header *hdr) | |
211 | { | |
212 | void *priv = hdr; | |
213 | ||
214 | return priv + sizeof(struct verify_header); | |
87677832 JA |
215 | } |
216 | ||
936216f8 JA |
217 | /* |
218 | * Verify container, pass info to verify handlers and allow them to | |
219 | * pass info back in case of error | |
220 | */ | |
221 | struct vcont { | |
222 | /* | |
223 | * Input | |
224 | */ | |
225 | struct io_u *io_u; | |
226 | unsigned int hdr_num; | |
7d9fb455 | 227 | struct thread_data *td; |
936216f8 JA |
228 | |
229 | /* | |
230 | * Output, only valid in case of error | |
231 | */ | |
bfacf389 JA |
232 | const char *name; |
233 | void *good_crc; | |
234 | void *bad_crc; | |
936216f8 JA |
235 | unsigned int crc_len; |
236 | }; | |
237 | ||
dacbbb88 JA |
238 | #define DUMP_BUF_SZ 255 |
239 | static int dump_buf_warned; | |
240 | ||
c50ca7bd JA |
241 | static void dump_buf(char *buf, unsigned int len, unsigned long long offset, |
242 | const char *type, struct fio_file *f) | |
7d9fb455 | 243 | { |
dacbbb88 JA |
244 | char *ptr, fname[DUMP_BUF_SZ]; |
245 | size_t buf_left = DUMP_BUF_SZ; | |
7d9fb455 JA |
246 | int ret, fd; |
247 | ||
6f260b31 | 248 | ptr = strdup(f->file_name); |
c50ca7bd | 249 | |
d264264a JA |
250 | memset(fname, 0, sizeof(fname)); |
251 | if (aux_path) | |
53a7af85 | 252 | sprintf(fname, "%s%c", aux_path, FIO_OS_PATH_SEPARATOR); |
d264264a JA |
253 | |
254 | strncpy(fname + strlen(fname), basename(ptr), buf_left - 1); | |
dacbbb88 JA |
255 | |
256 | buf_left -= strlen(fname); | |
257 | if (buf_left <= 0) { | |
258 | if (!dump_buf_warned) { | |
259 | log_err("fio: verify failure dump buffer too small\n"); | |
260 | dump_buf_warned = 1; | |
261 | } | |
262 | free(ptr); | |
263 | return; | |
264 | } | |
265 | ||
266 | snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type); | |
7d9fb455 JA |
267 | |
268 | fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644); | |
269 | if (fd < 0) { | |
270 | perror("open verify buf file"); | |
5bb51e60 | 271 | free(ptr); |
7d9fb455 JA |
272 | return; |
273 | } | |
274 | ||
275 | while (len) { | |
276 | ret = write(fd, buf, len); | |
277 | if (!ret) | |
278 | break; | |
279 | else if (ret < 0) { | |
280 | perror("write verify buf file"); | |
281 | break; | |
282 | } | |
283 | len -= ret; | |
284 | buf += ret; | |
285 | } | |
286 | ||
287 | close(fd); | |
c50ca7bd | 288 | log_err(" %s data dumped as %s\n", type, fname); |
6f260b31 | 289 | free(ptr); |
7d9fb455 JA |
290 | } |
291 | ||
c50ca7bd JA |
292 | /* |
293 | * Dump the contents of the read block and re-generate the correct data | |
294 | * and dump that too. | |
295 | */ | |
9ba987cc | 296 | static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc) |
7d9fb455 JA |
297 | { |
298 | struct thread_data *td = vc->td; | |
299 | struct io_u *io_u = vc->io_u; | |
300 | unsigned long hdr_offset; | |
7d9fb455 | 301 | struct io_u dummy; |
c50ca7bd | 302 | void *buf; |
7d9fb455 | 303 | |
0dce9bc9 SL |
304 | if (!td->o.verify_dump) |
305 | return; | |
306 | ||
c50ca7bd JA |
307 | /* |
308 | * Dump the contents we just read off disk | |
309 | */ | |
7d9fb455 JA |
310 | hdr_offset = vc->hdr_num * hdr->len; |
311 | ||
c50ca7bd JA |
312 | dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset, |
313 | "received", vc->io_u->file); | |
7d9fb455 | 314 | |
c50ca7bd JA |
315 | /* |
316 | * Allocate a new buf and re-generate the original data | |
317 | */ | |
318 | buf = malloc(io_u->buflen); | |
7d9fb455 | 319 | dummy = *io_u; |
c50ca7bd | 320 | dummy.buf = buf; |
4aae38a6 | 321 | dummy.rand_seed = hdr->rand_seed; |
cfbcd0dc | 322 | dummy.buf_filled_len = 0; |
0d81daf9 | 323 | dummy.buflen = io_u->buflen; |
7d9fb455 | 324 | |
c50ca7bd | 325 | fill_pattern_headers(td, &dummy, hdr->rand_seed, 1); |
7d9fb455 | 326 | |
c50ca7bd JA |
327 | dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset, |
328 | "expected", vc->io_u->file); | |
7d9fb455 JA |
329 | free(buf); |
330 | } | |
331 | ||
9ba987cc JA |
332 | static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc) |
333 | { | |
334 | struct thread_data *td = vc->td; | |
335 | struct verify_header shdr; | |
336 | ||
337 | if (td->o.verify == VERIFY_PATTERN_NO_HDR) { | |
b638d82f | 338 | __fill_hdr(td, vc->io_u, &shdr, 0, vc->io_u->buflen, 0); |
9ba987cc JA |
339 | hdr = &shdr; |
340 | } | |
341 | ||
342 | __dump_verify_buffers(hdr, vc); | |
343 | } | |
344 | ||
bfacf389 JA |
345 | static void log_verify_failure(struct verify_header *hdr, struct vcont *vc) |
346 | { | |
347 | unsigned long long offset; | |
348 | ||
349 | offset = vc->io_u->offset; | |
350 | offset += vc->hdr_num * hdr->len; | |
32c17adf JA |
351 | log_err("%.8s: verify failed at file %s offset %llu, length %u\n", |
352 | vc->name, vc->io_u->file->file_name, offset, hdr->len); | |
bfacf389 JA |
353 | |
354 | if (vc->good_crc && vc->bad_crc) { | |
355 | log_err(" Expected CRC: "); | |
356 | hexdump(vc->good_crc, vc->crc_len); | |
357 | log_err(" Received CRC: "); | |
358 | hexdump(vc->bad_crc, vc->crc_len); | |
359 | } | |
7d9fb455 JA |
360 | |
361 | dump_verify_buffers(hdr, vc); | |
bfacf389 JA |
362 | } |
363 | ||
d9f2caf3 JA |
364 | /* |
365 | * Return data area 'header_num' | |
366 | */ | |
936216f8 | 367 | static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc) |
d9f2caf3 | 368 | { |
ed3a433d | 369 | return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr); |
d9f2caf3 JA |
370 | } |
371 | ||
92bf48d5 JA |
372 | static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc) |
373 | { | |
374 | struct thread_data *td = vc->td; | |
375 | struct io_u *io_u = vc->io_u; | |
376 | char *buf, *pattern; | |
2b13e716 | 377 | unsigned int header_size = __hdr_size(td->o.verify); |
25276497 RP |
378 | unsigned int len, mod, i, pattern_size; |
379 | int rc; | |
92bf48d5 JA |
380 | |
381 | pattern = td->o.verify_pattern; | |
9a2a86d0 | 382 | pattern_size = td->o.verify_pattern_bytes; |
61b9861d RP |
383 | assert(pattern_size != 0); |
384 | ||
385 | (void)paste_format_inplace(pattern, pattern_size, | |
386 | td->o.verify_fmt, td->o.verify_fmt_sz, io_u); | |
387 | ||
d637b8b8 | 388 | buf = (char *) hdr + header_size; |
2b13e716 | 389 | len = get_hdr_inc(td, io_u) - header_size; |
61b9861d | 390 | mod = (get_hdr_inc(td, io_u) * vc->hdr_num + header_size) % pattern_size; |
9a2a86d0 | 391 | |
25276497 RP |
392 | rc = cmp_pattern(pattern, pattern_size, mod, buf, len); |
393 | if (!rc) | |
394 | return 0; | |
92bf48d5 | 395 | |
25276497 RP |
396 | /* Slow path, compare each byte */ |
397 | for (i = 0; i < len; i++) { | |
92bf48d5 JA |
398 | if (buf[i] != pattern[mod]) { |
399 | unsigned int bits; | |
400 | ||
401 | bits = hweight8(buf[i] ^ pattern[mod]); | |
25276497 RP |
402 | log_err("fio: got pattern '%02x', wanted '%02x'. Bad bits %d\n", |
403 | (unsigned char)buf[i], | |
404 | (unsigned char)pattern[mod], | |
405 | bits); | |
92bf48d5 | 406 | log_err("fio: bad pattern block offset %u\n", i); |
7ea0a9ad | 407 | vc->name = "pattern"; |
d0c8ccb0 | 408 | log_verify_failure(hdr, vc); |
92bf48d5 JA |
409 | return EILSEQ; |
410 | } | |
411 | mod++; | |
412 | if (mod == td->o.verify_pattern_bytes) | |
413 | mod = 0; | |
414 | } | |
415 | ||
25276497 RP |
416 | /* Unreachable line */ |
417 | assert(0); | |
418 | return EILSEQ; | |
92bf48d5 JA |
419 | } |
420 | ||
844ea602 JA |
421 | static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc) |
422 | { | |
423 | void *p = io_u_verify_off(hdr, vc); | |
424 | struct vhdr_xxhash *vh = hdr_priv(hdr); | |
425 | uint32_t hash; | |
426 | void *state; | |
427 | ||
428 | dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len); | |
429 | ||
430 | state = XXH32_init(1); | |
ed3a433d | 431 | XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr)); |
844ea602 JA |
432 | hash = XXH32_digest(state); |
433 | ||
434 | if (vh->hash == hash) | |
435 | return 0; | |
436 | ||
437 | vc->name = "xxhash"; | |
438 | vc->good_crc = &vh->hash; | |
439 | vc->bad_crc = &hash; | |
440 | vc->crc_len = sizeof(hash); | |
441 | log_verify_failure(hdr, vc); | |
442 | return EILSEQ; | |
443 | } | |
444 | ||
ae3a5acc JA |
445 | static int verify_io_u_sha3(struct verify_header *hdr, struct vcont *vc, |
446 | struct fio_sha3_ctx *sha3_ctx, uint8_t *sha, | |
447 | unsigned int sha_size, const char *name) | |
448 | { | |
449 | void *p = io_u_verify_off(hdr, vc); | |
450 | ||
451 | dprint(FD_VERIFY, "%s verify io_u %p, len %u\n", name, vc->io_u, hdr->len); | |
452 | ||
453 | fio_sha3_update(sha3_ctx, p, hdr->len - hdr_size(vc->td, hdr)); | |
454 | fio_sha3_final(sha3_ctx); | |
455 | ||
456 | if (!memcmp(sha, sha3_ctx->sha, sha_size)) | |
457 | return 0; | |
458 | ||
459 | vc->name = name; | |
460 | vc->good_crc = sha; | |
461 | vc->bad_crc = sha3_ctx->sha; | |
462 | vc->crc_len = sha_size; | |
463 | log_verify_failure(hdr, vc); | |
464 | return EILSEQ; | |
465 | } | |
466 | ||
467 | static int verify_io_u_sha3_224(struct verify_header *hdr, struct vcont *vc) | |
468 | { | |
469 | struct vhdr_sha3_224 *vh = hdr_priv(hdr); | |
470 | uint8_t sha[SHA3_224_DIGEST_SIZE]; | |
471 | struct fio_sha3_ctx sha3_ctx = { | |
472 | .sha = sha, | |
473 | }; | |
474 | ||
475 | fio_sha3_224_init(&sha3_ctx); | |
476 | ||
477 | return verify_io_u_sha3(hdr, vc, &sha3_ctx, vh->sha, | |
478 | SHA3_224_DIGEST_SIZE, "sha3-224"); | |
479 | } | |
480 | ||
481 | static int verify_io_u_sha3_256(struct verify_header *hdr, struct vcont *vc) | |
482 | { | |
483 | struct vhdr_sha3_256 *vh = hdr_priv(hdr); | |
484 | uint8_t sha[SHA3_256_DIGEST_SIZE]; | |
485 | struct fio_sha3_ctx sha3_ctx = { | |
486 | .sha = sha, | |
487 | }; | |
488 | ||
489 | fio_sha3_256_init(&sha3_ctx); | |
490 | ||
491 | return verify_io_u_sha3(hdr, vc, &sha3_ctx, vh->sha, | |
492 | SHA3_256_DIGEST_SIZE, "sha3-256"); | |
493 | } | |
494 | ||
495 | static int verify_io_u_sha3_384(struct verify_header *hdr, struct vcont *vc) | |
496 | { | |
497 | struct vhdr_sha3_384 *vh = hdr_priv(hdr); | |
498 | uint8_t sha[SHA3_384_DIGEST_SIZE]; | |
499 | struct fio_sha3_ctx sha3_ctx = { | |
500 | .sha = sha, | |
501 | }; | |
502 | ||
503 | fio_sha3_384_init(&sha3_ctx); | |
504 | ||
505 | return verify_io_u_sha3(hdr, vc, &sha3_ctx, vh->sha, | |
506 | SHA3_384_DIGEST_SIZE, "sha3-384"); | |
507 | } | |
508 | ||
509 | static int verify_io_u_sha3_512(struct verify_header *hdr, struct vcont *vc) | |
510 | { | |
511 | struct vhdr_sha3_512 *vh = hdr_priv(hdr); | |
512 | uint8_t sha[SHA3_512_DIGEST_SIZE]; | |
513 | struct fio_sha3_ctx sha3_ctx = { | |
514 | .sha = sha, | |
515 | }; | |
516 | ||
517 | fio_sha3_512_init(&sha3_ctx); | |
518 | ||
519 | return verify_io_u_sha3(hdr, vc, &sha3_ctx, vh->sha, | |
520 | SHA3_512_DIGEST_SIZE, "sha3-512"); | |
521 | } | |
522 | ||
936216f8 | 523 | static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc) |
cd14cc10 | 524 | { |
936216f8 | 525 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 526 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
cd14cc10 | 527 | uint8_t sha512[128]; |
25dfa848 | 528 | struct fio_sha512_ctx sha512_ctx = { |
cd14cc10 JA |
529 | .buf = sha512, |
530 | }; | |
531 | ||
936216f8 | 532 | dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 533 | |
25dfa848 | 534 | fio_sha512_init(&sha512_ctx); |
ed3a433d | 535 | fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr)); |
cd14cc10 | 536 | |
bfacf389 JA |
537 | if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) |
538 | return 0; | |
cd14cc10 | 539 | |
bfacf389 JA |
540 | vc->name = "sha512"; |
541 | vc->good_crc = vh->sha512; | |
542 | vc->bad_crc = sha512_ctx.buf; | |
543 | vc->crc_len = sizeof(vh->sha512); | |
544 | log_verify_failure(hdr, vc); | |
545 | return EILSEQ; | |
cd14cc10 JA |
546 | } |
547 | ||
936216f8 | 548 | static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc) |
cd14cc10 | 549 | { |
936216f8 | 550 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 551 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
bc77f56f | 552 | uint8_t sha256[64]; |
25dfa848 | 553 | struct fio_sha256_ctx sha256_ctx = { |
cd14cc10 JA |
554 | .buf = sha256, |
555 | }; | |
556 | ||
936216f8 | 557 | dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 558 | |
25dfa848 | 559 | fio_sha256_init(&sha256_ctx); |
ed3a433d | 560 | fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr)); |
017052b6 | 561 | fio_sha256_final(&sha256_ctx); |
cd14cc10 | 562 | |
bfacf389 JA |
563 | if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) |
564 | return 0; | |
cd14cc10 | 565 | |
bfacf389 JA |
566 | vc->name = "sha256"; |
567 | vc->good_crc = vh->sha256; | |
568 | vc->bad_crc = sha256_ctx.buf; | |
569 | vc->crc_len = sizeof(vh->sha256); | |
570 | log_verify_failure(hdr, vc); | |
571 | return EILSEQ; | |
cd14cc10 JA |
572 | } |
573 | ||
936216f8 | 574 | static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc) |
7c353ceb | 575 | { |
936216f8 | 576 | void *p = io_u_verify_off(hdr, vc); |
7c353ceb JA |
577 | struct vhdr_sha1 *vh = hdr_priv(hdr); |
578 | uint32_t sha1[5]; | |
25dfa848 | 579 | struct fio_sha1_ctx sha1_ctx = { |
7c353ceb JA |
580 | .H = sha1, |
581 | }; | |
582 | ||
936216f8 | 583 | dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
7c353ceb | 584 | |
25dfa848 | 585 | fio_sha1_init(&sha1_ctx); |
ed3a433d | 586 | fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr)); |
017052b6 | 587 | fio_sha1_final(&sha1_ctx); |
7c353ceb | 588 | |
bfacf389 JA |
589 | if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) |
590 | return 0; | |
7c353ceb | 591 | |
bfacf389 JA |
592 | vc->name = "sha1"; |
593 | vc->good_crc = vh->sha1; | |
594 | vc->bad_crc = sha1_ctx.H; | |
595 | vc->crc_len = sizeof(vh->sha1); | |
596 | log_verify_failure(hdr, vc); | |
597 | return EILSEQ; | |
7c353ceb JA |
598 | } |
599 | ||
936216f8 | 600 | static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc) |
1e154bdb | 601 | { |
936216f8 | 602 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 603 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
1e154bdb JA |
604 | unsigned char c; |
605 | ||
936216f8 | 606 | dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 607 | |
ed3a433d | 608 | c = fio_crc7(p, hdr->len - hdr_size(vc->td, hdr)); |
1e154bdb | 609 | |
bfacf389 JA |
610 | if (c == vh->crc7) |
611 | return 0; | |
1e154bdb | 612 | |
bfacf389 JA |
613 | vc->name = "crc7"; |
614 | vc->good_crc = &vh->crc7; | |
615 | vc->bad_crc = &c; | |
616 | vc->crc_len = 1; | |
617 | log_verify_failure(hdr, vc); | |
618 | return EILSEQ; | |
1e154bdb JA |
619 | } |
620 | ||
936216f8 | 621 | static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc) |
969f7ed3 | 622 | { |
936216f8 | 623 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 624 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
969f7ed3 JA |
625 | unsigned short c; |
626 | ||
936216f8 | 627 | dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 628 | |
ed3a433d | 629 | c = fio_crc16(p, hdr->len - hdr_size(vc->td, hdr)); |
969f7ed3 | 630 | |
bfacf389 JA |
631 | if (c == vh->crc16) |
632 | return 0; | |
969f7ed3 | 633 | |
bfacf389 JA |
634 | vc->name = "crc16"; |
635 | vc->good_crc = &vh->crc16; | |
636 | vc->bad_crc = &c; | |
637 | vc->crc_len = 2; | |
638 | log_verify_failure(hdr, vc); | |
639 | return EILSEQ; | |
969f7ed3 JA |
640 | } |
641 | ||
936216f8 | 642 | static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc) |
d77a7af3 | 643 | { |
936216f8 | 644 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 645 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
d77a7af3 JA |
646 | unsigned long long c; |
647 | ||
936216f8 | 648 | dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 649 | |
ed3a433d | 650 | c = fio_crc64(p, hdr->len - hdr_size(vc->td, hdr)); |
d77a7af3 | 651 | |
bfacf389 JA |
652 | if (c == vh->crc64) |
653 | return 0; | |
d77a7af3 | 654 | |
bfacf389 JA |
655 | vc->name = "crc64"; |
656 | vc->good_crc = &vh->crc64; | |
657 | vc->bad_crc = &c; | |
658 | vc->crc_len = 8; | |
659 | log_verify_failure(hdr, vc); | |
660 | return EILSEQ; | |
d77a7af3 JA |
661 | } |
662 | ||
936216f8 | 663 | static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc) |
e29d1b70 | 664 | { |
936216f8 | 665 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f JA |
666 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
667 | uint32_t c; | |
e29d1b70 | 668 | |
936216f8 | 669 | dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 670 | |
ed3a433d | 671 | c = fio_crc32(p, hdr->len - hdr_size(vc->td, hdr)); |
e29d1b70 | 672 | |
bfacf389 JA |
673 | if (c == vh->crc32) |
674 | return 0; | |
e29d1b70 | 675 | |
bfacf389 JA |
676 | vc->name = "crc32"; |
677 | vc->good_crc = &vh->crc32; | |
678 | vc->bad_crc = &c; | |
679 | vc->crc_len = 4; | |
680 | log_verify_failure(hdr, vc); | |
681 | return EILSEQ; | |
e29d1b70 JA |
682 | } |
683 | ||
936216f8 | 684 | static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc) |
bac39e0e | 685 | { |
936216f8 | 686 | void *p = io_u_verify_off(hdr, vc); |
bac39e0e JA |
687 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
688 | uint32_t c; | |
689 | ||
936216f8 | 690 | dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bac39e0e | 691 | |
ed3a433d | 692 | c = fio_crc32c(p, hdr->len - hdr_size(vc->td, hdr)); |
bac39e0e | 693 | |
bfacf389 JA |
694 | if (c == vh->crc32) |
695 | return 0; | |
bac39e0e | 696 | |
bfacf389 JA |
697 | vc->name = "crc32c"; |
698 | vc->good_crc = &vh->crc32; | |
699 | vc->bad_crc = &c; | |
700 | vc->crc_len = 4; | |
701 | log_verify_failure(hdr, vc); | |
702 | return EILSEQ; | |
bac39e0e JA |
703 | } |
704 | ||
936216f8 | 705 | static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc) |
e29d1b70 | 706 | { |
936216f8 | 707 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 708 | struct vhdr_md5 *vh = hdr_priv(hdr); |
8c432325 | 709 | uint32_t hash[MD5_HASH_WORDS]; |
25dfa848 | 710 | struct fio_md5_ctx md5_ctx = { |
8c432325 JA |
711 | .hash = hash, |
712 | }; | |
e29d1b70 | 713 | |
936216f8 | 714 | dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 715 | |
25dfa848 | 716 | fio_md5_init(&md5_ctx); |
ed3a433d | 717 | fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr)); |
017052b6 | 718 | fio_md5_final(&md5_ctx); |
e29d1b70 | 719 | |
bfacf389 JA |
720 | if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) |
721 | return 0; | |
e29d1b70 | 722 | |
bfacf389 JA |
723 | vc->name = "md5"; |
724 | vc->good_crc = vh->md5_digest; | |
725 | vc->bad_crc = md5_ctx.hash; | |
726 | vc->crc_len = sizeof(hash); | |
727 | log_verify_failure(hdr, vc); | |
728 | return EILSEQ; | |
e29d1b70 JA |
729 | } |
730 | ||
e8462bd8 JA |
731 | /* |
732 | * Push IO verification to a separate thread | |
733 | */ | |
f8b0bd10 | 734 | int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr) |
e8462bd8 | 735 | { |
f8b0bd10 | 736 | struct io_u *io_u = *io_u_ptr; |
e8462bd8 | 737 | |
e8462bd8 | 738 | pthread_mutex_lock(&td->io_u_lock); |
d7ee2a7d | 739 | |
f8b0bd10 JA |
740 | if (io_u->file) |
741 | put_file_log(td, io_u->file); | |
742 | ||
0c41214f RR |
743 | if (io_u->flags & IO_U_F_IN_CUR_DEPTH) { |
744 | td->cur_depth--; | |
1651e431 | 745 | io_u_clear(td, io_u, IO_U_F_IN_CUR_DEPTH); |
0c41214f | 746 | } |
2ae0b204 | 747 | flist_add_tail(&io_u->verify_list, &td->verify_list); |
f8b0bd10 | 748 | *io_u_ptr = NULL; |
e8462bd8 JA |
749 | pthread_mutex_unlock(&td->io_u_lock); |
750 | ||
751 | pthread_cond_signal(&td->verify_cond); | |
e8462bd8 JA |
752 | return 0; |
753 | } | |
754 | ||
323f58c5 JA |
755 | /* |
756 | * Thanks Rusty, for spending the time so I don't have to. | |
757 | * | |
758 | * http://rusty.ozlabs.org/?p=560 | |
759 | */ | |
760 | static int mem_is_zero(const void *data, size_t length) | |
761 | { | |
762 | const unsigned char *p = data; | |
763 | size_t len; | |
764 | ||
765 | /* Check first 16 bytes manually */ | |
766 | for (len = 0; len < 16; len++) { | |
767 | if (!length) | |
768 | return 1; | |
769 | if (*p) | |
770 | return 0; | |
771 | p++; | |
772 | length--; | |
773 | } | |
774 | ||
775 | /* Now we know that's zero, memcmp with self. */ | |
776 | return memcmp(data, p, length) == 0; | |
777 | } | |
778 | ||
779 | static int mem_is_zero_slow(const void *data, size_t length, size_t *offset) | |
780 | { | |
781 | const unsigned char *p = data; | |
782 | ||
783 | *offset = 0; | |
784 | while (length) { | |
785 | if (*p) | |
786 | break; | |
787 | (*offset)++; | |
788 | length--; | |
789 | p++; | |
790 | } | |
791 | ||
792 | return !length; | |
793 | } | |
794 | ||
0d29de83 JA |
795 | static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u) |
796 | { | |
323f58c5 | 797 | size_t offset; |
0d29de83 JA |
798 | |
799 | if (!td->o.trim_zero) | |
800 | return 0; | |
801 | ||
323f58c5 | 802 | if (mem_is_zero(io_u->buf, io_u->buflen)) |
0d29de83 JA |
803 | return 0; |
804 | ||
323f58c5 JA |
805 | mem_is_zero_slow(io_u->buf, io_u->buflen, &offset); |
806 | ||
a917a8b3 JA |
807 | log_err("trim: verify failed at file %s offset %llu, length %lu" |
808 | ", block offset %lu\n", | |
809 | io_u->file->file_name, io_u->offset, io_u->buflen, | |
323f58c5 JA |
810 | (unsigned long) offset); |
811 | return EILSEQ; | |
0d29de83 JA |
812 | } |
813 | ||
b638d82f RP |
814 | static int verify_header(struct io_u *io_u, struct thread_data *td, |
815 | struct verify_header *hdr, unsigned int hdr_num, | |
816 | unsigned int hdr_len) | |
f65d1c26 JA |
817 | { |
818 | void *p = hdr; | |
819 | uint32_t crc; | |
820 | ||
5964842c AG |
821 | if (hdr->magic != FIO_HDR_MAGIC) { |
822 | log_err("verify: bad magic header %x, wanted %x", | |
823 | hdr->magic, FIO_HDR_MAGIC); | |
824 | goto err; | |
825 | } | |
4445856a | 826 | if (hdr->len != hdr_len) { |
5964842c AG |
827 | log_err("verify: bad header length %u, wanted %u", |
828 | hdr->len, hdr_len); | |
829 | goto err; | |
830 | } | |
831 | if (hdr->rand_seed != io_u->rand_seed) { | |
832 | log_err("verify: bad header rand_seed %"PRIu64 | |
833 | ", wanted %"PRIu64, | |
834 | hdr->rand_seed, io_u->rand_seed); | |
835 | goto err; | |
836 | } | |
b638d82f RP |
837 | if (hdr->offset != io_u->offset + hdr_num * td->o.verify_interval) { |
838 | log_err("verify: bad header offset %"PRIu64 | |
839 | ", wanted %llu", | |
840 | hdr->offset, io_u->offset); | |
841 | goto err; | |
842 | } | |
843 | ||
844 | /* | |
845 | * For read-only workloads, the program cannot be certain of the | |
846 | * last numberio written to a block. Checking of numberio will be | |
847 | * done only for workloads that write data. For verify_only, | |
848 | * numberio will be checked in the last iteration when the correct | |
849 | * state of numberio, that would have been written to each block | |
850 | * in a previous run of fio, has been reached. | |
851 | */ | |
dcf9844e | 852 | if (td_write(td) && (td_min_bs(td) == td_max_bs(td)) && |
b638d82f RP |
853 | !td->o.time_based) |
854 | if (!td->o.verify_only || td->o.loops == 0) | |
855 | if (hdr->numberio != io_u->numberio) { | |
856 | log_err("verify: bad header numberio %"PRIu16 | |
857 | ", wanted %"PRIu16, | |
858 | hdr->numberio, io_u->numberio); | |
859 | goto err; | |
860 | } | |
ae38c0dc | 861 | |
25dfa848 | 862 | crc = fio_crc32c(p, offsetof(struct verify_header, crc32)); |
5964842c AG |
863 | if (crc != hdr->crc32) { |
864 | log_err("verify: bad header crc %x, calculated %x", | |
865 | hdr->crc32, crc); | |
866 | goto err; | |
867 | } | |
868 | return 0; | |
869 | ||
870 | err: | |
871 | log_err(" at file %s offset %llu, length %u\n", | |
872 | io_u->file->file_name, | |
873 | io_u->offset + hdr_num * hdr_len, hdr_len); | |
16758b59 JA |
874 | |
875 | if (td->o.verify_dump) | |
876 | dump_buf(p, hdr_len, io_u->offset + hdr_num * hdr_len, | |
877 | "hdr_fail", io_u->file); | |
878 | ||
5964842c | 879 | return EILSEQ; |
f65d1c26 JA |
880 | } |
881 | ||
f8b0bd10 | 882 | int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr) |
e29d1b70 | 883 | { |
3f9f4e26 | 884 | struct verify_header *hdr; |
f8b0bd10 | 885 | struct io_u *io_u = *io_u_ptr; |
2b13e716 | 886 | unsigned int header_size, hdr_inc, hdr_num = 0; |
95646108 | 887 | void *p; |
e29d1b70 JA |
888 | int ret; |
889 | ||
1dcc0498 | 890 | if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ) |
36690c9b | 891 | return 0; |
5c57c084 JA |
892 | /* |
893 | * If the IO engine is faking IO (like null), then just pretend | |
894 | * we verified everything. | |
895 | */ | |
9b87f09b | 896 | if (td_ioengine_flagged(td, FIO_FAKEIO)) |
5c57c084 JA |
897 | return 0; |
898 | ||
0d29de83 JA |
899 | if (io_u->flags & IO_U_F_TRIMMED) { |
900 | ret = verify_trimmed_io_u(td, io_u); | |
901 | goto done; | |
902 | } | |
36690c9b | 903 | |
cfbcd0dc | 904 | hdr_inc = get_hdr_inc(td, io_u); |
e29d1b70 | 905 | |
a12a3b4d | 906 | ret = 0; |
5ec10eaa JA |
907 | for (p = io_u->buf; p < io_u->buf + io_u->buflen; |
908 | p += hdr_inc, hdr_num++) { | |
bfacf389 JA |
909 | struct vcont vc = { |
910 | .io_u = io_u, | |
911 | .hdr_num = hdr_num, | |
7d9fb455 | 912 | .td = td, |
bfacf389 | 913 | }; |
f00b210f | 914 | unsigned int verify_type; |
936216f8 | 915 | |
f3e6cb95 | 916 | if (ret && td->o.verify_fatal) |
a12a3b4d | 917 | break; |
f3e6cb95 | 918 | |
2b13e716 | 919 | header_size = __hdr_size(td->o.verify); |
a59e170d | 920 | if (td->o.verify_offset) |
2b13e716 | 921 | memswp(p, p + td->o.verify_offset, header_size); |
95646108 | 922 | hdr = p; |
e29d1b70 | 923 | |
c4b6117b PV |
924 | /* |
925 | * Make rand_seed check pass when have verifysort or | |
926 | * verify_backlog. | |
927 | */ | |
928 | if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG)) | |
929 | io_u->rand_seed = hdr->rand_seed; | |
930 | ||
59245381 | 931 | if (td->o.verify != VERIFY_PATTERN_NO_HDR) { |
b638d82f | 932 | ret = verify_header(io_u, td, hdr, hdr_num, hdr_inc); |
59245381 JA |
933 | if (ret) |
934 | return ret; | |
935 | } | |
3f199b01 | 936 | |
f00b210f JA |
937 | if (td->o.verify != VERIFY_NONE) |
938 | verify_type = td->o.verify; | |
939 | else | |
940 | verify_type = hdr->verify_type; | |
941 | ||
942 | switch (verify_type) { | |
b638d82f RP |
943 | case VERIFY_HDR_ONLY: |
944 | /* Header is always verified, check if pattern is left | |
945 | * for verification. */ | |
946 | if (td->o.verify_pattern_bytes) | |
947 | ret = verify_io_u_pattern(hdr, &vc); | |
948 | break; | |
3f9f4e26 | 949 | case VERIFY_MD5: |
936216f8 | 950 | ret = verify_io_u_md5(hdr, &vc); |
3f9f4e26 SL |
951 | break; |
952 | case VERIFY_CRC64: | |
936216f8 | 953 | ret = verify_io_u_crc64(hdr, &vc); |
3f9f4e26 | 954 | break; |
bac39e0e | 955 | case VERIFY_CRC32C: |
3845591f | 956 | case VERIFY_CRC32C_INTEL: |
936216f8 | 957 | ret = verify_io_u_crc32c(hdr, &vc); |
bac39e0e | 958 | break; |
3f9f4e26 | 959 | case VERIFY_CRC32: |
936216f8 | 960 | ret = verify_io_u_crc32(hdr, &vc); |
3f9f4e26 SL |
961 | break; |
962 | case VERIFY_CRC16: | |
936216f8 | 963 | ret = verify_io_u_crc16(hdr, &vc); |
3f9f4e26 SL |
964 | break; |
965 | case VERIFY_CRC7: | |
936216f8 | 966 | ret = verify_io_u_crc7(hdr, &vc); |
3f9f4e26 | 967 | break; |
cd14cc10 | 968 | case VERIFY_SHA256: |
936216f8 | 969 | ret = verify_io_u_sha256(hdr, &vc); |
cd14cc10 JA |
970 | break; |
971 | case VERIFY_SHA512: | |
936216f8 | 972 | ret = verify_io_u_sha512(hdr, &vc); |
cd14cc10 | 973 | break; |
ae3a5acc JA |
974 | case VERIFY_SHA3_224: |
975 | ret = verify_io_u_sha3_224(hdr, &vc); | |
976 | break; | |
977 | case VERIFY_SHA3_256: | |
978 | ret = verify_io_u_sha3_256(hdr, &vc); | |
979 | break; | |
980 | case VERIFY_SHA3_384: | |
981 | ret = verify_io_u_sha3_384(hdr, &vc); | |
982 | break; | |
983 | case VERIFY_SHA3_512: | |
984 | ret = verify_io_u_sha3_512(hdr, &vc); | |
985 | break; | |
844ea602 JA |
986 | case VERIFY_XXHASH: |
987 | ret = verify_io_u_xxhash(hdr, &vc); | |
988 | break; | |
7c353ceb | 989 | case VERIFY_SHA1: |
936216f8 | 990 | ret = verify_io_u_sha1(hdr, &vc); |
7c353ceb | 991 | break; |
92bf48d5 | 992 | case VERIFY_PATTERN: |
59245381 | 993 | case VERIFY_PATTERN_NO_HDR: |
92bf48d5 JA |
994 | ret = verify_io_u_pattern(hdr, &vc); |
995 | break; | |
3f9f4e26 SL |
996 | default: |
997 | log_err("Bad verify type %u\n", hdr->verify_type); | |
d16d4e09 | 998 | ret = EINVAL; |
3f9f4e26 | 999 | } |
f00b210f JA |
1000 | |
1001 | if (ret && verify_type != hdr->verify_type) | |
1002 | log_err("fio: verify type mismatch (%u media, %u given)\n", | |
1003 | hdr->verify_type, verify_type); | |
3f9f4e26 | 1004 | } |
a7dfe862 | 1005 | |
0d29de83 | 1006 | done: |
f3e6cb95 | 1007 | if (ret && td->o.verify_fatal) |
ebea2133 | 1008 | fio_mark_td_terminate(td); |
f3e6cb95 | 1009 | |
a12a3b4d | 1010 | return ret; |
e29d1b70 JA |
1011 | } |
1012 | ||
844ea602 JA |
1013 | static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len) |
1014 | { | |
1015 | struct vhdr_xxhash *vh = hdr_priv(hdr); | |
1016 | void *state; | |
1017 | ||
1018 | state = XXH32_init(1); | |
1019 | XXH32_update(state, p, len); | |
1020 | vh->hash = XXH32_digest(state); | |
1021 | } | |
1022 | ||
ae3a5acc JA |
1023 | static void fill_sha3(struct fio_sha3_ctx *sha3_ctx, void *p, unsigned int len) |
1024 | { | |
1025 | fio_sha3_update(sha3_ctx, p, len); | |
1026 | fio_sha3_final(sha3_ctx); | |
1027 | } | |
1028 | ||
1029 | static void fill_sha3_224(struct verify_header *hdr, void *p, unsigned int len) | |
1030 | { | |
1031 | struct vhdr_sha3_224 *vh = hdr_priv(hdr); | |
1032 | struct fio_sha3_ctx sha3_ctx = { | |
1033 | .sha = vh->sha, | |
1034 | }; | |
1035 | ||
1036 | fio_sha3_224_init(&sha3_ctx); | |
1037 | fill_sha3(&sha3_ctx, p, len); | |
1038 | } | |
1039 | ||
1040 | static void fill_sha3_256(struct verify_header *hdr, void *p, unsigned int len) | |
1041 | { | |
1042 | struct vhdr_sha3_256 *vh = hdr_priv(hdr); | |
1043 | struct fio_sha3_ctx sha3_ctx = { | |
1044 | .sha = vh->sha, | |
1045 | }; | |
1046 | ||
1047 | fio_sha3_256_init(&sha3_ctx); | |
1048 | fill_sha3(&sha3_ctx, p, len); | |
1049 | } | |
1050 | ||
1051 | static void fill_sha3_384(struct verify_header *hdr, void *p, unsigned int len) | |
1052 | { | |
1053 | struct vhdr_sha3_384 *vh = hdr_priv(hdr); | |
1054 | struct fio_sha3_ctx sha3_ctx = { | |
1055 | .sha = vh->sha, | |
1056 | }; | |
1057 | ||
1058 | fio_sha3_384_init(&sha3_ctx); | |
1059 | fill_sha3(&sha3_ctx, p, len); | |
1060 | } | |
1061 | ||
1062 | static void fill_sha3_512(struct verify_header *hdr, void *p, unsigned int len) | |
1063 | { | |
1064 | struct vhdr_sha3_512 *vh = hdr_priv(hdr); | |
1065 | struct fio_sha3_ctx sha3_ctx = { | |
1066 | .sha = vh->sha, | |
1067 | }; | |
1068 | ||
1069 | fio_sha3_512_init(&sha3_ctx); | |
1070 | fill_sha3(&sha3_ctx, p, len); | |
1071 | } | |
1072 | ||
cd14cc10 JA |
1073 | static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len) |
1074 | { | |
546dfd9f | 1075 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
25dfa848 | 1076 | struct fio_sha512_ctx sha512_ctx = { |
546dfd9f | 1077 | .buf = vh->sha512, |
cd14cc10 JA |
1078 | }; |
1079 | ||
25dfa848 JA |
1080 | fio_sha512_init(&sha512_ctx); |
1081 | fio_sha512_update(&sha512_ctx, p, len); | |
cd14cc10 JA |
1082 | } |
1083 | ||
1084 | static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len) | |
1085 | { | |
546dfd9f | 1086 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
25dfa848 | 1087 | struct fio_sha256_ctx sha256_ctx = { |
546dfd9f | 1088 | .buf = vh->sha256, |
cd14cc10 JA |
1089 | }; |
1090 | ||
25dfa848 JA |
1091 | fio_sha256_init(&sha256_ctx); |
1092 | fio_sha256_update(&sha256_ctx, p, len); | |
017052b6 | 1093 | fio_sha256_final(&sha256_ctx); |
cd14cc10 JA |
1094 | } |
1095 | ||
7c353ceb JA |
1096 | static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len) |
1097 | { | |
1098 | struct vhdr_sha1 *vh = hdr_priv(hdr); | |
25dfa848 | 1099 | struct fio_sha1_ctx sha1_ctx = { |
7c353ceb JA |
1100 | .H = vh->sha1, |
1101 | }; | |
1102 | ||
25dfa848 JA |
1103 | fio_sha1_init(&sha1_ctx); |
1104 | fio_sha1_update(&sha1_ctx, p, len); | |
017052b6 | 1105 | fio_sha1_final(&sha1_ctx); |
7c353ceb JA |
1106 | } |
1107 | ||
1e154bdb JA |
1108 | static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len) |
1109 | { | |
546dfd9f JA |
1110 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
1111 | ||
25dfa848 | 1112 | vh->crc7 = fio_crc7(p, len); |
1e154bdb JA |
1113 | } |
1114 | ||
969f7ed3 JA |
1115 | static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len) |
1116 | { | |
546dfd9f JA |
1117 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
1118 | ||
25dfa848 | 1119 | vh->crc16 = fio_crc16(p, len); |
969f7ed3 JA |
1120 | } |
1121 | ||
e29d1b70 JA |
1122 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
1123 | { | |
546dfd9f JA |
1124 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
1125 | ||
25dfa848 | 1126 | vh->crc32 = fio_crc32(p, len); |
e29d1b70 JA |
1127 | } |
1128 | ||
bac39e0e JA |
1129 | static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len) |
1130 | { | |
1131 | struct vhdr_crc32 *vh = hdr_priv(hdr); | |
1132 | ||
25dfa848 | 1133 | vh->crc32 = fio_crc32c(p, len); |
bac39e0e JA |
1134 | } |
1135 | ||
d77a7af3 JA |
1136 | static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len) |
1137 | { | |
546dfd9f JA |
1138 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
1139 | ||
25dfa848 | 1140 | vh->crc64 = fio_crc64(p, len); |
d77a7af3 JA |
1141 | } |
1142 | ||
e29d1b70 JA |
1143 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
1144 | { | |
546dfd9f | 1145 | struct vhdr_md5 *vh = hdr_priv(hdr); |
25dfa848 | 1146 | struct fio_md5_ctx md5_ctx = { |
546dfd9f | 1147 | .hash = (uint32_t *) vh->md5_digest, |
8c432325 | 1148 | }; |
e29d1b70 | 1149 | |
25dfa848 JA |
1150 | fio_md5_init(&md5_ctx); |
1151 | fio_md5_update(&md5_ctx, p, len); | |
017052b6 | 1152 | fio_md5_final(&md5_ctx); |
e29d1b70 JA |
1153 | } |
1154 | ||
b638d82f RP |
1155 | static void __fill_hdr(struct thread_data *td, struct io_u *io_u, |
1156 | struct verify_header *hdr, unsigned int header_num, | |
1157 | unsigned int header_len, uint64_t rand_seed) | |
9ba987cc JA |
1158 | { |
1159 | void *p = hdr; | |
1160 | ||
1161 | hdr->magic = FIO_HDR_MAGIC; | |
b638d82f RP |
1162 | hdr->verify_type = td->o.verify; |
1163 | hdr->len = header_len; | |
9ba987cc | 1164 | hdr->rand_seed = rand_seed; |
b638d82f RP |
1165 | hdr->offset = io_u->offset + header_num * td->o.verify_interval; |
1166 | hdr->time_sec = io_u->start_time.tv_sec; | |
faa9b2b2 | 1167 | hdr->time_nsec = io_u->start_time.tv_nsec; |
b638d82f RP |
1168 | hdr->thread = td->thread_number; |
1169 | hdr->numberio = io_u->numberio; | |
9ba987cc JA |
1170 | hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32)); |
1171 | } | |
1172 | ||
ed3a433d | 1173 | |
b638d82f RP |
1174 | static void fill_hdr(struct thread_data *td, struct io_u *io_u, |
1175 | struct verify_header *hdr, unsigned int header_num, | |
1176 | unsigned int header_len, uint64_t rand_seed) | |
ed3a433d | 1177 | { |
b638d82f RP |
1178 | |
1179 | if (td->o.verify != VERIFY_PATTERN_NO_HDR) | |
1180 | __fill_hdr(td, io_u, hdr, header_num, header_len, rand_seed); | |
ed3a433d JA |
1181 | } |
1182 | ||
7d9fb455 JA |
1183 | static void populate_hdr(struct thread_data *td, struct io_u *io_u, |
1184 | struct verify_header *hdr, unsigned int header_num, | |
1185 | unsigned int header_len) | |
1186 | { | |
1187 | unsigned int data_len; | |
d637b8b8 TK |
1188 | void *data; |
1189 | char *p; | |
7d9fb455 | 1190 | |
d637b8b8 | 1191 | p = (char *) hdr; |
7d9fb455 | 1192 | |
b638d82f | 1193 | fill_hdr(td, io_u, hdr, header_num, header_len, io_u->rand_seed); |
f65d1c26 | 1194 | |
ed3a433d | 1195 | data_len = header_len - hdr_size(td, hdr); |
7d9fb455 | 1196 | |
ed3a433d | 1197 | data = p + hdr_size(td, hdr); |
7d9fb455 JA |
1198 | switch (td->o.verify) { |
1199 | case VERIFY_MD5: | |
1200 | dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n", | |
1201 | io_u, hdr->len); | |
1202 | fill_md5(hdr, data, data_len); | |
1203 | break; | |
1204 | case VERIFY_CRC64: | |
1205 | dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n", | |
1206 | io_u, hdr->len); | |
1207 | fill_crc64(hdr, data, data_len); | |
1208 | break; | |
1209 | case VERIFY_CRC32C: | |
1210 | case VERIFY_CRC32C_INTEL: | |
1211 | dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n", | |
1212 | io_u, hdr->len); | |
1213 | fill_crc32c(hdr, data, data_len); | |
1214 | break; | |
1215 | case VERIFY_CRC32: | |
1216 | dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n", | |
1217 | io_u, hdr->len); | |
1218 | fill_crc32(hdr, data, data_len); | |
1219 | break; | |
1220 | case VERIFY_CRC16: | |
1221 | dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n", | |
1222 | io_u, hdr->len); | |
1223 | fill_crc16(hdr, data, data_len); | |
1224 | break; | |
1225 | case VERIFY_CRC7: | |
1226 | dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n", | |
1227 | io_u, hdr->len); | |
1228 | fill_crc7(hdr, data, data_len); | |
1229 | break; | |
1230 | case VERIFY_SHA256: | |
1231 | dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n", | |
1232 | io_u, hdr->len); | |
1233 | fill_sha256(hdr, data, data_len); | |
1234 | break; | |
1235 | case VERIFY_SHA512: | |
1236 | dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n", | |
1237 | io_u, hdr->len); | |
1238 | fill_sha512(hdr, data, data_len); | |
1239 | break; | |
ae3a5acc JA |
1240 | case VERIFY_SHA3_224: |
1241 | dprint(FD_VERIFY, "fill sha3-224 io_u %p, len %u\n", | |
1242 | io_u, hdr->len); | |
1243 | fill_sha3_224(hdr, data, data_len); | |
1244 | break; | |
1245 | case VERIFY_SHA3_256: | |
1246 | dprint(FD_VERIFY, "fill sha3-256 io_u %p, len %u\n", | |
1247 | io_u, hdr->len); | |
1248 | fill_sha3_256(hdr, data, data_len); | |
1249 | break; | |
1250 | case VERIFY_SHA3_384: | |
1251 | dprint(FD_VERIFY, "fill sha3-384 io_u %p, len %u\n", | |
1252 | io_u, hdr->len); | |
1253 | fill_sha3_384(hdr, data, data_len); | |
1254 | break; | |
1255 | case VERIFY_SHA3_512: | |
1256 | dprint(FD_VERIFY, "fill sha3-512 io_u %p, len %u\n", | |
1257 | io_u, hdr->len); | |
1258 | fill_sha3_512(hdr, data, data_len); | |
1259 | break; | |
844ea602 JA |
1260 | case VERIFY_XXHASH: |
1261 | dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n", | |
1262 | io_u, hdr->len); | |
1263 | fill_xxhash(hdr, data, data_len); | |
1264 | break; | |
7d9fb455 JA |
1265 | case VERIFY_SHA1: |
1266 | dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n", | |
1267 | io_u, hdr->len); | |
1268 | fill_sha1(hdr, data, data_len); | |
1269 | break; | |
b638d82f | 1270 | case VERIFY_HDR_ONLY: |
92bf48d5 | 1271 | case VERIFY_PATTERN: |
ed3a433d | 1272 | case VERIFY_PATTERN_NO_HDR: |
92bf48d5 JA |
1273 | /* nothing to do here */ |
1274 | break; | |
7d9fb455 JA |
1275 | default: |
1276 | log_err("fio: bad verify type: %d\n", td->o.verify); | |
1277 | assert(0); | |
1278 | } | |
ed3a433d JA |
1279 | |
1280 | if (td->o.verify_offset && hdr_size(td, hdr)) | |
1281 | memswp(p, p + td->o.verify_offset, hdr_size(td, hdr)); | |
7d9fb455 JA |
1282 | } |
1283 | ||
e29d1b70 JA |
1284 | /* |
1285 | * fill body of io_u->buf with random data and add a header with the | |
c50ca7bd | 1286 | * checksum of choice |
e29d1b70 JA |
1287 | */ |
1288 | void populate_verify_io_u(struct thread_data *td, struct io_u *io_u) | |
1289 | { | |
9cc3d150 JA |
1290 | if (td->o.verify == VERIFY_NULL) |
1291 | return; | |
1292 | ||
da0a7bd2 JC |
1293 | io_u->numberio = td->io_issues[io_u->ddir]; |
1294 | ||
c50ca7bd | 1295 | fill_pattern_headers(td, io_u, 0, 0); |
e29d1b70 JA |
1296 | } |
1297 | ||
1298 | int get_next_verify(struct thread_data *td, struct io_u *io_u) | |
1299 | { | |
8de8f047 | 1300 | struct io_piece *ipo = NULL; |
e29d1b70 | 1301 | |
d2d7fa53 JA |
1302 | /* |
1303 | * this io_u is from a requeue, we already filled the offsets | |
1304 | */ | |
1305 | if (io_u->file) | |
1306 | return 0; | |
1307 | ||
8de8f047 JA |
1308 | if (!RB_EMPTY_ROOT(&td->io_hist_tree)) { |
1309 | struct rb_node *n = rb_first(&td->io_hist_tree); | |
e29d1b70 | 1310 | |
8de8f047 | 1311 | ipo = rb_entry(n, struct io_piece, rb_node); |
f9401285 JA |
1312 | |
1313 | /* | |
1314 | * Ensure that the associated IO has completed | |
1315 | */ | |
1316 | read_barrier(); | |
1317 | if (ipo->flags & IP_F_IN_FLIGHT) | |
1318 | goto nothing; | |
1319 | ||
4b87898e | 1320 | rb_erase(n, &td->io_hist_tree); |
a917a8b3 JA |
1321 | assert(ipo->flags & IP_F_ONRB); |
1322 | ipo->flags &= ~IP_F_ONRB; | |
01743ee1 | 1323 | } else if (!flist_empty(&td->io_hist_list)) { |
9342d5f8 | 1324 | ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list); |
f9401285 JA |
1325 | |
1326 | /* | |
1327 | * Ensure that the associated IO has completed | |
1328 | */ | |
1329 | read_barrier(); | |
1330 | if (ipo->flags & IP_F_IN_FLIGHT) | |
1331 | goto nothing; | |
1332 | ||
01743ee1 | 1333 | flist_del(&ipo->list); |
a917a8b3 JA |
1334 | assert(ipo->flags & IP_F_ONLIST); |
1335 | ipo->flags &= ~IP_F_ONLIST; | |
8de8f047 | 1336 | } |
e29d1b70 | 1337 | |
8de8f047 | 1338 | if (ipo) { |
0d29de83 JA |
1339 | td->io_hist_len--; |
1340 | ||
e29d1b70 JA |
1341 | io_u->offset = ipo->offset; |
1342 | io_u->buflen = ipo->len; | |
da0a7bd2 | 1343 | io_u->numberio = ipo->numberio; |
36167d82 | 1344 | io_u->file = ipo->file; |
1651e431 | 1345 | io_u_set(td, io_u, IO_U_F_VER_LIST); |
97af62ce | 1346 | |
0d29de83 | 1347 | if (ipo->flags & IP_F_TRIMMED) |
1651e431 | 1348 | io_u_set(td, io_u, IO_U_F_TRIMMED); |
0d29de83 | 1349 | |
d6aed795 | 1350 | if (!fio_file_open(io_u->file)) { |
97af62ce JA |
1351 | int r = td_io_open_file(td, io_u->file); |
1352 | ||
bd6f78b2 JA |
1353 | if (r) { |
1354 | dprint(FD_VERIFY, "failed file %s open\n", | |
1355 | io_u->file->file_name); | |
97af62ce | 1356 | return 1; |
bd6f78b2 | 1357 | } |
97af62ce JA |
1358 | } |
1359 | ||
1360 | get_file(ipo->file); | |
d6aed795 | 1361 | assert(fio_file_open(io_u->file)); |
e29d1b70 | 1362 | io_u->ddir = DDIR_READ; |
36167d82 JA |
1363 | io_u->xfer_buf = io_u->buf; |
1364 | io_u->xfer_buflen = io_u->buflen; | |
0d29de83 JA |
1365 | |
1366 | remove_trim_entry(td, ipo); | |
e29d1b70 | 1367 | free(ipo); |
bd6f78b2 | 1368 | dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u); |
c4b6117b PV |
1369 | |
1370 | if (!td->o.verify_pattern_bytes) { | |
d6b72507 | 1371 | io_u->rand_seed = __rand(&td->verify_state); |
c4b6117b | 1372 | if (sizeof(int) != sizeof(long *)) |
d6b72507 | 1373 | io_u->rand_seed *= __rand(&td->verify_state); |
c4b6117b | 1374 | } |
e29d1b70 JA |
1375 | return 0; |
1376 | } | |
1377 | ||
f9401285 | 1378 | nothing: |
bd6f78b2 | 1379 | dprint(FD_VERIFY, "get_next_verify: empty\n"); |
e29d1b70 JA |
1380 | return 1; |
1381 | } | |
e8462bd8 | 1382 | |
dc5bfbb2 JA |
1383 | void fio_verify_init(struct thread_data *td) |
1384 | { | |
1385 | if (td->o.verify == VERIFY_CRC32C_INTEL || | |
1386 | td->o.verify == VERIFY_CRC32C) { | |
214e2d56 | 1387 | crc32c_arm64_probe(); |
dc5bfbb2 JA |
1388 | crc32c_intel_probe(); |
1389 | } | |
1390 | } | |
1391 | ||
e8462bd8 JA |
1392 | static void *verify_async_thread(void *data) |
1393 | { | |
1394 | struct thread_data *td = data; | |
1395 | struct io_u *io_u; | |
1396 | int ret = 0; | |
1397 | ||
b2a9e649 | 1398 | if (fio_option_is_set(&td->o, verify_cpumask) && |
e8462bd8 JA |
1399 | fio_setaffinity(td->pid, td->o.verify_cpumask)) { |
1400 | log_err("fio: failed setting verify thread affinity\n"); | |
1401 | goto done; | |
1402 | } | |
1403 | ||
1404 | do { | |
e53ab27c JA |
1405 | FLIST_HEAD(list); |
1406 | ||
e8462bd8 JA |
1407 | read_barrier(); |
1408 | if (td->verify_thread_exit) | |
1409 | break; | |
1410 | ||
1411 | pthread_mutex_lock(&td->io_u_lock); | |
1412 | ||
1413 | while (flist_empty(&td->verify_list) && | |
1414 | !td->verify_thread_exit) { | |
b36e298b JA |
1415 | ret = pthread_cond_wait(&td->verify_cond, |
1416 | &td->io_u_lock); | |
e8462bd8 JA |
1417 | if (ret) { |
1418 | pthread_mutex_unlock(&td->io_u_lock); | |
1419 | break; | |
1420 | } | |
1421 | } | |
1422 | ||
e53ab27c JA |
1423 | flist_splice_init(&td->verify_list, &list); |
1424 | pthread_mutex_unlock(&td->io_u_lock); | |
1425 | ||
1426 | if (flist_empty(&list)) | |
e8462bd8 | 1427 | continue; |
e8462bd8 | 1428 | |
e53ab27c | 1429 | while (!flist_empty(&list)) { |
9342d5f8 | 1430 | io_u = flist_first_entry(&list, struct io_u, verify_list); |
f8b0bd10 JA |
1431 | flist_del_init(&io_u->verify_list); |
1432 | ||
1651e431 | 1433 | io_u_set(td, io_u, IO_U_F_NO_FILE_PUT); |
f8b0bd10 | 1434 | ret = verify_io_u(td, &io_u); |
e8462bd8 | 1435 | |
e53ab27c | 1436 | put_io_u(td, io_u); |
d561f2ab JA |
1437 | if (!ret) |
1438 | continue; | |
8b28bd41 | 1439 | if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) { |
d561f2ab JA |
1440 | update_error_count(td, ret); |
1441 | td_clear_error(td); | |
1442 | ret = 0; | |
1443 | } | |
e53ab27c | 1444 | } |
e8462bd8 JA |
1445 | } while (!ret); |
1446 | ||
d561f2ab JA |
1447 | if (ret) { |
1448 | td_verror(td, ret, "async_verify"); | |
f3e6cb95 | 1449 | if (td->o.verify_fatal) |
ebea2133 | 1450 | fio_mark_td_terminate(td); |
d561f2ab JA |
1451 | } |
1452 | ||
e8462bd8 JA |
1453 | done: |
1454 | pthread_mutex_lock(&td->io_u_lock); | |
1455 | td->nr_verify_threads--; | |
1456 | pthread_mutex_unlock(&td->io_u_lock); | |
1457 | ||
1458 | pthread_cond_signal(&td->free_cond); | |
1459 | return NULL; | |
1460 | } | |
1461 | ||
1462 | int verify_async_init(struct thread_data *td) | |
1463 | { | |
1464 | int i, ret; | |
304a47c7 VA |
1465 | pthread_attr_t attr; |
1466 | ||
1467 | pthread_attr_init(&attr); | |
45213f1b | 1468 | pthread_attr_setstacksize(&attr, 2 * PTHREAD_STACK_MIN); |
e8462bd8 JA |
1469 | |
1470 | td->verify_thread_exit = 0; | |
1471 | ||
1472 | td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async); | |
1473 | for (i = 0; i < td->o.verify_async; i++) { | |
304a47c7 | 1474 | ret = pthread_create(&td->verify_threads[i], &attr, |
e8462bd8 JA |
1475 | verify_async_thread, td); |
1476 | if (ret) { | |
1477 | log_err("fio: async verify creation failed: %s\n", | |
1478 | strerror(ret)); | |
1479 | break; | |
1480 | } | |
1481 | ret = pthread_detach(td->verify_threads[i]); | |
1482 | if (ret) { | |
1483 | log_err("fio: async verify thread detach failed: %s\n", | |
1484 | strerror(ret)); | |
1485 | break; | |
1486 | } | |
1487 | td->nr_verify_threads++; | |
1488 | } | |
1489 | ||
304a47c7 VA |
1490 | pthread_attr_destroy(&attr); |
1491 | ||
e8462bd8 | 1492 | if (i != td->o.verify_async) { |
e40823b1 | 1493 | log_err("fio: only %d verify threads started, exiting\n", i); |
e8462bd8 JA |
1494 | td->verify_thread_exit = 1; |
1495 | write_barrier(); | |
1496 | pthread_cond_broadcast(&td->verify_cond); | |
1497 | return 1; | |
1498 | } | |
1499 | ||
1500 | return 0; | |
1501 | } | |
1502 | ||
1503 | void verify_async_exit(struct thread_data *td) | |
1504 | { | |
1505 | td->verify_thread_exit = 1; | |
1506 | write_barrier(); | |
1507 | pthread_cond_broadcast(&td->verify_cond); | |
1508 | ||
1509 | pthread_mutex_lock(&td->io_u_lock); | |
1510 | ||
1511 | while (td->nr_verify_threads) | |
1512 | pthread_cond_wait(&td->free_cond, &td->io_u_lock); | |
1513 | ||
1514 | pthread_mutex_unlock(&td->io_u_lock); | |
1515 | free(td->verify_threads); | |
1516 | td->verify_threads = NULL; | |
1517 | } | |
ca09be4b | 1518 | |
61b9861d RP |
1519 | int paste_blockoff(char *buf, unsigned int len, void *priv) |
1520 | { | |
1521 | struct io_u *io = priv; | |
1522 | unsigned long long off; | |
1523 | ||
1524 | typecheck(typeof(off), io->offset); | |
1525 | off = cpu_to_le64((uint64_t)io->offset); | |
1526 | len = min(len, (unsigned int)sizeof(off)); | |
1527 | memcpy(buf, &off, len); | |
1528 | return 0; | |
1529 | } | |
1530 | ||
94a6e1bb JA |
1531 | static int __fill_file_completions(struct thread_data *td, |
1532 | struct thread_io_list *s, | |
1533 | struct fio_file *f, unsigned int *index) | |
1534 | { | |
1535 | unsigned int comps; | |
1536 | int i, j; | |
1537 | ||
1538 | if (!f->last_write_comp) | |
1539 | return 0; | |
1540 | ||
1541 | if (td->io_blocks[DDIR_WRITE] < td->o.iodepth) | |
1542 | comps = td->io_blocks[DDIR_WRITE]; | |
1543 | else | |
1544 | comps = td->o.iodepth; | |
1545 | ||
1546 | j = f->last_write_idx - 1; | |
1547 | for (i = 0; i < comps; i++) { | |
1548 | if (j == -1) | |
1549 | j = td->o.iodepth - 1; | |
1550 | s->comps[*index].fileno = __cpu_to_le64(f->fileno); | |
1551 | s->comps[*index].offset = cpu_to_le64(f->last_write_comp[j]); | |
1552 | (*index)++; | |
1553 | j--; | |
1554 | } | |
1555 | ||
1556 | return comps; | |
1557 | } | |
1558 | ||
1559 | static int fill_file_completions(struct thread_data *td, | |
1560 | struct thread_io_list *s, unsigned int *index) | |
1561 | { | |
1562 | struct fio_file *f; | |
1563 | unsigned int i; | |
1564 | int comps = 0; | |
1565 | ||
1566 | for_each_file(td, f, i) | |
1567 | comps += __fill_file_completions(td, s, f, index); | |
1568 | ||
1569 | return comps; | |
1570 | } | |
1571 | ||
ca09be4b JA |
1572 | struct all_io_list *get_all_io_list(int save_mask, size_t *sz) |
1573 | { | |
1574 | struct all_io_list *rep; | |
1575 | struct thread_data *td; | |
1576 | size_t depth; | |
1577 | void *next; | |
1578 | int i, nr; | |
1579 | ||
1580 | compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list"); | |
1581 | ||
1582 | /* | |
1583 | * Calculate reply space needed. We need one 'io_state' per thread, | |
1584 | * and the size will vary depending on depth. | |
1585 | */ | |
1586 | depth = 0; | |
1587 | nr = 0; | |
1588 | for_each_td(td, i) { | |
1589 | if (save_mask != IO_LIST_ALL && (i + 1) != save_mask) | |
1590 | continue; | |
1591 | td->stop_io = 1; | |
1592 | td->flags |= TD_F_VSTATE_SAVED; | |
94a6e1bb | 1593 | depth += (td->o.iodepth * td->o.nr_files); |
ca09be4b JA |
1594 | nr++; |
1595 | } | |
1596 | ||
1597 | if (!nr) | |
1598 | return NULL; | |
1599 | ||
1600 | *sz = sizeof(*rep); | |
1601 | *sz += nr * sizeof(struct thread_io_list); | |
94a6e1bb | 1602 | *sz += depth * sizeof(struct file_comp); |
ca09be4b | 1603 | rep = malloc(*sz); |
c68ce5ea | 1604 | memset(rep, 0, *sz); |
ca09be4b JA |
1605 | |
1606 | rep->threads = cpu_to_le64((uint64_t) nr); | |
1607 | ||
1608 | next = &rep->state[0]; | |
1609 | for_each_td(td, i) { | |
1610 | struct thread_io_list *s = next; | |
94a6e1bb | 1611 | unsigned int comps, index = 0; |
ca09be4b JA |
1612 | |
1613 | if (save_mask != IO_LIST_ALL && (i + 1) != save_mask) | |
1614 | continue; | |
1615 | ||
94a6e1bb | 1616 | comps = fill_file_completions(td, s, &index); |
ca09be4b JA |
1617 | |
1618 | s->no_comps = cpu_to_le64((uint64_t) comps); | |
1619 | s->depth = cpu_to_le64((uint64_t) td->o.iodepth); | |
94a6e1bb | 1620 | s->nofiles = cpu_to_le64((uint64_t) td->o.nr_files); |
ca09be4b JA |
1621 | s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]); |
1622 | s->index = cpu_to_le64((uint64_t) i); | |
c3546b53 JA |
1623 | if (td->random_state.use64) { |
1624 | s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1); | |
1625 | s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2); | |
1626 | s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3); | |
1627 | s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4); | |
1628 | s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5); | |
1629 | s->rand.state64.s[5] = 0; | |
1630 | s->rand.use64 = cpu_to_le64((uint64_t)1); | |
1631 | } else { | |
1632 | s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1); | |
1633 | s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2); | |
1634 | s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3); | |
1635 | s->rand.state32.s[3] = 0; | |
1636 | s->rand.use64 = 0; | |
1637 | } | |
1dc06a8f JA |
1638 | s->name[sizeof(s->name) - 1] = '\0'; |
1639 | strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1); | |
ca09be4b JA |
1640 | next = io_list_next(s); |
1641 | } | |
1642 | ||
1643 | return rep; | |
1644 | } | |
1645 | ||
1646 | static int open_state_file(const char *name, const char *prefix, int num, | |
1647 | int for_write) | |
1648 | { | |
d117b2bd | 1649 | char out[PATH_MAX]; |
ca09be4b JA |
1650 | int flags; |
1651 | int fd; | |
1652 | ||
1653 | if (for_write) | |
1654 | flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC; | |
1655 | else | |
1656 | flags = O_RDONLY; | |
1657 | ||
e499aedc | 1658 | verify_state_gen_name(out, sizeof(out), name, prefix, num); |
ca09be4b JA |
1659 | |
1660 | fd = open(out, flags, 0644); | |
1661 | if (fd == -1) { | |
1662 | perror("fio: open state file"); | |
d117b2bd | 1663 | log_err("fio: state file: %s (for_write=%d)\n", out, for_write); |
ca09be4b JA |
1664 | return -1; |
1665 | } | |
1666 | ||
1667 | return fd; | |
1668 | } | |
1669 | ||
1670 | static int write_thread_list_state(struct thread_io_list *s, | |
1671 | const char *prefix) | |
1672 | { | |
1673 | struct verify_state_hdr hdr; | |
1674 | uint64_t crc; | |
1675 | ssize_t ret; | |
1676 | int fd; | |
1677 | ||
1678 | fd = open_state_file((const char *) s->name, prefix, s->index, 1); | |
1679 | if (fd == -1) | |
1680 | return 1; | |
1681 | ||
1682 | crc = fio_crc32c((void *)s, thread_io_list_sz(s)); | |
1683 | ||
1684 | hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION); | |
1685 | hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s)); | |
1686 | hdr.crc = cpu_to_le64(crc); | |
1687 | ret = write(fd, &hdr, sizeof(hdr)); | |
1688 | if (ret != sizeof(hdr)) | |
1689 | goto write_fail; | |
1690 | ||
1691 | ret = write(fd, s, thread_io_list_sz(s)); | |
1692 | if (ret != thread_io_list_sz(s)) { | |
1693 | write_fail: | |
1694 | if (ret < 0) | |
1695 | perror("fio: write state file"); | |
1696 | log_err("fio: failed to write state file\n"); | |
1697 | ret = 1; | |
1698 | } else | |
1699 | ret = 0; | |
1700 | ||
1701 | close(fd); | |
1702 | return ret; | |
1703 | } | |
1704 | ||
1705 | void __verify_save_state(struct all_io_list *state, const char *prefix) | |
1706 | { | |
1707 | struct thread_io_list *s = &state->state[0]; | |
1708 | unsigned int i; | |
1709 | ||
1710 | for (i = 0; i < le64_to_cpu(state->threads); i++) { | |
1711 | write_thread_list_state(s, prefix); | |
1712 | s = io_list_next(s); | |
1713 | } | |
1714 | } | |
1715 | ||
d264264a | 1716 | void verify_save_state(int mask) |
ca09be4b JA |
1717 | { |
1718 | struct all_io_list *state; | |
1719 | size_t sz; | |
1720 | ||
d264264a | 1721 | state = get_all_io_list(mask, &sz); |
ca09be4b | 1722 | if (state) { |
d264264a JA |
1723 | char prefix[PATH_MAX]; |
1724 | ||
1725 | if (aux_path) | |
53a7af85 | 1726 | sprintf(prefix, "%s%clocal", aux_path, FIO_OS_PATH_SEPARATOR); |
d264264a | 1727 | else |
c6d140fb | 1728 | strcpy(prefix, "local"); |
d264264a JA |
1729 | |
1730 | __verify_save_state(state, prefix); | |
ca09be4b JA |
1731 | free(state); |
1732 | } | |
1733 | } | |
1734 | ||
1735 | void verify_free_state(struct thread_data *td) | |
1736 | { | |
1737 | if (td->vstate) | |
1738 | free(td->vstate); | |
1739 | } | |
1740 | ||
94a6e1bb | 1741 | void verify_assign_state(struct thread_data *td, void *p) |
ca09be4b | 1742 | { |
94a6e1bb | 1743 | struct thread_io_list *s = p; |
ca09be4b JA |
1744 | int i; |
1745 | ||
94a6e1bb JA |
1746 | s->no_comps = le64_to_cpu(s->no_comps); |
1747 | s->depth = le32_to_cpu(s->depth); | |
1748 | s->nofiles = le32_to_cpu(s->nofiles); | |
1749 | s->numberio = le64_to_cpu(s->numberio); | |
1750 | s->rand.use64 = le64_to_cpu(s->rand.use64); | |
c3546b53 | 1751 | |
94a6e1bb JA |
1752 | if (s->rand.use64) { |
1753 | for (i = 0; i < 6; i++) | |
1754 | s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]); | |
c3546b53 | 1755 | } else { |
94a6e1bb JA |
1756 | for (i = 0; i < 4; i++) |
1757 | s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]); | |
1758 | } | |
c3546b53 | 1759 | |
94a6e1bb JA |
1760 | for (i = 0; i < s->no_comps; i++) { |
1761 | s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno); | |
1762 | s->comps[i].offset = le64_to_cpu(s->comps[i].offset); | |
c3546b53 | 1763 | } |
ca09be4b | 1764 | |
94a6e1bb | 1765 | td->vstate = p; |
ca09be4b JA |
1766 | } |
1767 | ||
94a6e1bb | 1768 | int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s) |
ca09be4b JA |
1769 | { |
1770 | uint64_t crc; | |
1771 | ||
1772 | hdr->version = le64_to_cpu(hdr->version); | |
1773 | hdr->size = le64_to_cpu(hdr->size); | |
1774 | hdr->crc = le64_to_cpu(hdr->crc); | |
1775 | ||
94a6e1bb | 1776 | if (hdr->version != VSTATE_HDR_VERSION) |
ca09be4b JA |
1777 | return 1; |
1778 | ||
1779 | crc = fio_crc32c((void *)s, hdr->size); | |
1780 | if (crc != hdr->crc) | |
1781 | return 1; | |
1782 | ||
1783 | return 0; | |
1784 | } | |
1785 | ||
1786 | int verify_load_state(struct thread_data *td, const char *prefix) | |
1787 | { | |
ca09be4b | 1788 | struct verify_state_hdr hdr; |
c3546b53 | 1789 | void *s = NULL; |
ca09be4b JA |
1790 | uint64_t crc; |
1791 | ssize_t ret; | |
1792 | int fd; | |
1793 | ||
1794 | if (!td->o.verify_state) | |
1795 | return 0; | |
1796 | ||
1797 | fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0); | |
1798 | if (fd == -1) | |
1799 | return 1; | |
1800 | ||
1801 | ret = read(fd, &hdr, sizeof(hdr)); | |
1802 | if (ret != sizeof(hdr)) { | |
1803 | if (ret < 0) | |
1804 | td_verror(td, errno, "read verify state hdr"); | |
1805 | log_err("fio: failed reading verify state header\n"); | |
1806 | goto err; | |
1807 | } | |
1808 | ||
1809 | hdr.version = le64_to_cpu(hdr.version); | |
1810 | hdr.size = le64_to_cpu(hdr.size); | |
1811 | hdr.crc = le64_to_cpu(hdr.crc); | |
1812 | ||
94a6e1bb JA |
1813 | if (hdr.version != VSTATE_HDR_VERSION) { |
1814 | log_err("fio: unsupported (%d) version in verify state header\n", | |
1815 | (unsigned int) hdr.version); | |
ca09be4b JA |
1816 | goto err; |
1817 | } | |
1818 | ||
1819 | s = malloc(hdr.size); | |
1820 | ret = read(fd, s, hdr.size); | |
1821 | if (ret != hdr.size) { | |
1822 | if (ret < 0) | |
1823 | td_verror(td, errno, "read verify state"); | |
1824 | log_err("fio: failed reading verity state\n"); | |
1825 | goto err; | |
1826 | } | |
1827 | ||
c3546b53 | 1828 | crc = fio_crc32c(s, hdr.size); |
ca09be4b JA |
1829 | if (crc != hdr.crc) { |
1830 | log_err("fio: verify state is corrupt\n"); | |
1831 | goto err; | |
1832 | } | |
1833 | ||
1834 | close(fd); | |
1835 | ||
94a6e1bb | 1836 | verify_assign_state(td, s); |
ca09be4b JA |
1837 | return 0; |
1838 | err: | |
1839 | if (s) | |
1840 | free(s); | |
1841 | close(fd); | |
1842 | return 1; | |
1843 | } | |
1844 | ||
1845 | /* | |
1846 | * Use the loaded verify state to know when to stop doing verification | |
1847 | */ | |
1848 | int verify_state_should_stop(struct thread_data *td, struct io_u *io_u) | |
1849 | { | |
1850 | struct thread_io_list *s = td->vstate; | |
94a6e1bb | 1851 | struct fio_file *f = io_u->file; |
ca09be4b JA |
1852 | int i; |
1853 | ||
94a6e1bb | 1854 | if (!s || !f) |
ca09be4b JA |
1855 | return 0; |
1856 | ||
1857 | /* | |
def00095 JA |
1858 | * If we're not into the window of issues - depth yet, continue. If |
1859 | * issue is shorter than depth, do check. | |
ca09be4b | 1860 | */ |
def00095 JA |
1861 | if ((td->io_blocks[DDIR_READ] < s->depth || |
1862 | s->numberio - td->io_blocks[DDIR_READ] > s->depth) && | |
1863 | s->numberio > s->depth) | |
ca09be4b JA |
1864 | return 0; |
1865 | ||
1866 | /* | |
1867 | * We're in the window of having to check if this io was | |
1868 | * completed or not. If the IO was seen as completed, then | |
1869 | * lets verify it. | |
1870 | */ | |
94a6e1bb JA |
1871 | for (i = 0; i < s->no_comps; i++) { |
1872 | if (s->comps[i].fileno != f->fileno) | |
1873 | continue; | |
1874 | if (io_u->offset == s->comps[i].offset) | |
ca09be4b | 1875 | return 0; |
94a6e1bb | 1876 | } |
ca09be4b JA |
1877 | |
1878 | /* | |
1879 | * Not found, we have to stop | |
1880 | */ | |
1881 | return 1; | |
1882 | } |