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