configure: stop enabling fdatasync on OSX
[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
9781c080 42static void __fill_buffer(struct thread_options *o, uint64_t seed, void *p,
a89ba4b1 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
9781c080
MHT
48static uint64_t 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 57void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
9781c080 58 struct io_u *io_u, uint64_t seed, int use_seed)
ce35b1ec 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 102static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
9781c080 103 uint64_t seed, int use_seed)
c50ca7bd
JA
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 243#define DUMP_BUF_SZ 255
dacbbb88 244
c50ca7bd
JA
245static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
246 const char *type, struct fio_file *f)
7d9fb455 247{
9db753ec
BVA
248 char *ptr, *fname;
249 char sep[2] = { FIO_OS_PATH_SEPARATOR, 0 };
7d9fb455
JA
250 int ret, fd;
251
6f260b31 252 ptr = strdup(f->file_name);
c50ca7bd 253
9db753ec
BVA
254 if (asprintf(&fname, "%s%s%s.%llu.%s", aux_path ? : "",
255 aux_path ? sep : "", basename(ptr), offset, type) < 0) {
7fc08e99 256 if (!fio_did_warn(FIO_WARN_VERIFY_BUF))
9db753ec
BVA
257 log_err("fio: not enough memory for dump buffer filename\n");
258 goto free_ptr;
dacbbb88
JA
259 }
260
7d9fb455
JA
261 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
262 if (fd < 0) {
263 perror("open verify buf file");
9db753ec 264 goto free_fname;
7d9fb455
JA
265 }
266
267 while (len) {
268 ret = write(fd, buf, len);
269 if (!ret)
270 break;
271 else if (ret < 0) {
272 perror("write verify buf file");
273 break;
274 }
275 len -= ret;
276 buf += ret;
277 }
278
279 close(fd);
c50ca7bd 280 log_err(" %s data dumped as %s\n", type, fname);
9db753ec
BVA
281
282free_fname:
283 free(fname);
284
285free_ptr:
6f260b31 286 free(ptr);
7d9fb455
JA
287}
288
c50ca7bd
JA
289/*
290 * Dump the contents of the read block and re-generate the correct data
291 * and dump that too.
292 */
9ba987cc 293static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
7d9fb455
JA
294{
295 struct thread_data *td = vc->td;
296 struct io_u *io_u = vc->io_u;
297 unsigned long hdr_offset;
7d9fb455 298 struct io_u dummy;
c50ca7bd 299 void *buf;
7d9fb455 300
0dce9bc9
SL
301 if (!td->o.verify_dump)
302 return;
303
c50ca7bd
JA
304 /*
305 * Dump the contents we just read off disk
306 */
7d9fb455
JA
307 hdr_offset = vc->hdr_num * hdr->len;
308
c50ca7bd
JA
309 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
310 "received", vc->io_u->file);
7d9fb455 311
c50ca7bd
JA
312 /*
313 * Allocate a new buf and re-generate the original data
314 */
315 buf = malloc(io_u->buflen);
7d9fb455 316 dummy = *io_u;
c50ca7bd 317 dummy.buf = buf;
4aae38a6 318 dummy.rand_seed = hdr->rand_seed;
cfbcd0dc 319 dummy.buf_filled_len = 0;
0d81daf9 320 dummy.buflen = io_u->buflen;
7d9fb455 321
c50ca7bd 322 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
7d9fb455 323
c50ca7bd
JA
324 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
325 "expected", vc->io_u->file);
7d9fb455
JA
326 free(buf);
327}
328
9ba987cc
JA
329static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
330{
331 struct thread_data *td = vc->td;
332 struct verify_header shdr;
333
334 if (td->o.verify == VERIFY_PATTERN_NO_HDR) {
b638d82f 335 __fill_hdr(td, vc->io_u, &shdr, 0, vc->io_u->buflen, 0);
9ba987cc
JA
336 hdr = &shdr;
337 }
338
339 __dump_verify_buffers(hdr, vc);
340}
341
bfacf389
JA
342static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
343{
344 unsigned long long offset;
345
346 offset = vc->io_u->offset;
347 offset += vc->hdr_num * hdr->len;
46ff70e8
FC
348 log_err("%.8s: verify failed at file %s offset %llu, length %u"
349 " (requested block: offset=%llu, length=%llu)\n",
350 vc->name, vc->io_u->file->file_name, offset, hdr->len,
351 vc->io_u->offset, vc->io_u->buflen);
bfacf389
JA
352
353 if (vc->good_crc && vc->bad_crc) {
354 log_err(" Expected CRC: ");
355 hexdump(vc->good_crc, vc->crc_len);
356 log_err(" Received CRC: ");
357 hexdump(vc->bad_crc, vc->crc_len);
358 }
7d9fb455
JA
359
360 dump_verify_buffers(hdr, vc);
bfacf389
JA
361}
362
d9f2caf3
JA
363/*
364 * Return data area 'header_num'
365 */
936216f8 366static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
d9f2caf3 367{
ed3a433d 368 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr);
d9f2caf3
JA
369}
370
92bf48d5
JA
371static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
372{
373 struct thread_data *td = vc->td;
374 struct io_u *io_u = vc->io_u;
375 char *buf, *pattern;
2b13e716 376 unsigned int header_size = __hdr_size(td->o.verify);
25276497
RP
377 unsigned int len, mod, i, pattern_size;
378 int rc;
92bf48d5
JA
379
380 pattern = td->o.verify_pattern;
9a2a86d0 381 pattern_size = td->o.verify_pattern_bytes;
61b9861d
RP
382 assert(pattern_size != 0);
383
384 (void)paste_format_inplace(pattern, pattern_size,
385 td->o.verify_fmt, td->o.verify_fmt_sz, io_u);
386
d637b8b8 387 buf = (char *) hdr + header_size;
2b13e716 388 len = get_hdr_inc(td, io_u) - header_size;
61b9861d 389 mod = (get_hdr_inc(td, io_u) * vc->hdr_num + header_size) % pattern_size;
9a2a86d0 390
25276497
RP
391 rc = cmp_pattern(pattern, pattern_size, mod, buf, len);
392 if (!rc)
393 return 0;
92bf48d5 394
25276497
RP
395 /* Slow path, compare each byte */
396 for (i = 0; i < len; i++) {
92bf48d5
JA
397 if (buf[i] != pattern[mod]) {
398 unsigned int bits;
399
400 bits = hweight8(buf[i] ^ pattern[mod]);
25276497
RP
401 log_err("fio: got pattern '%02x', wanted '%02x'. Bad bits %d\n",
402 (unsigned char)buf[i],
403 (unsigned char)pattern[mod],
404 bits);
92bf48d5 405 log_err("fio: bad pattern block offset %u\n", i);
7ea0a9ad 406 vc->name = "pattern";
d0c8ccb0 407 log_verify_failure(hdr, vc);
92bf48d5
JA
408 return EILSEQ;
409 }
410 mod++;
411 if (mod == td->o.verify_pattern_bytes)
412 mod = 0;
413 }
414
25276497
RP
415 /* Unreachable line */
416 assert(0);
417 return EILSEQ;
92bf48d5
JA
418}
419
844ea602
JA
420static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
421{
422 void *p = io_u_verify_off(hdr, vc);
423 struct vhdr_xxhash *vh = hdr_priv(hdr);
424 uint32_t hash;
425 void *state;
426
427 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
428
429 state = XXH32_init(1);
ed3a433d 430 XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr));
844ea602
JA
431 hash = XXH32_digest(state);
432
433 if (vh->hash == hash)
434 return 0;
435
436 vc->name = "xxhash";
437 vc->good_crc = &vh->hash;
438 vc->bad_crc = &hash;
439 vc->crc_len = sizeof(hash);
440 log_verify_failure(hdr, vc);
441 return EILSEQ;
442}
443
ae3a5acc
JA
444static int verify_io_u_sha3(struct verify_header *hdr, struct vcont *vc,
445 struct fio_sha3_ctx *sha3_ctx, uint8_t *sha,
446 unsigned int sha_size, const char *name)
447{
448 void *p = io_u_verify_off(hdr, vc);
449
450 dprint(FD_VERIFY, "%s verify io_u %p, len %u\n", name, vc->io_u, hdr->len);
451
452 fio_sha3_update(sha3_ctx, p, hdr->len - hdr_size(vc->td, hdr));
453 fio_sha3_final(sha3_ctx);
454
455 if (!memcmp(sha, sha3_ctx->sha, sha_size))
456 return 0;
457
458 vc->name = name;
459 vc->good_crc = sha;
460 vc->bad_crc = sha3_ctx->sha;
461 vc->crc_len = sha_size;
462 log_verify_failure(hdr, vc);
463 return EILSEQ;
464}
465
466static int verify_io_u_sha3_224(struct verify_header *hdr, struct vcont *vc)
467{
468 struct vhdr_sha3_224 *vh = hdr_priv(hdr);
469 uint8_t sha[SHA3_224_DIGEST_SIZE];
470 struct fio_sha3_ctx sha3_ctx = {
471 .sha = sha,
472 };
473
474 fio_sha3_224_init(&sha3_ctx);
475
476 return verify_io_u_sha3(hdr, vc, &sha3_ctx, vh->sha,
477 SHA3_224_DIGEST_SIZE, "sha3-224");
478}
479
480static int verify_io_u_sha3_256(struct verify_header *hdr, struct vcont *vc)
481{
482 struct vhdr_sha3_256 *vh = hdr_priv(hdr);
483 uint8_t sha[SHA3_256_DIGEST_SIZE];
484 struct fio_sha3_ctx sha3_ctx = {
485 .sha = sha,
486 };
487
488 fio_sha3_256_init(&sha3_ctx);
489
490 return verify_io_u_sha3(hdr, vc, &sha3_ctx, vh->sha,
491 SHA3_256_DIGEST_SIZE, "sha3-256");
492}
493
494static int verify_io_u_sha3_384(struct verify_header *hdr, struct vcont *vc)
495{
496 struct vhdr_sha3_384 *vh = hdr_priv(hdr);
497 uint8_t sha[SHA3_384_DIGEST_SIZE];
498 struct fio_sha3_ctx sha3_ctx = {
499 .sha = sha,
500 };
501
502 fio_sha3_384_init(&sha3_ctx);
503
504 return verify_io_u_sha3(hdr, vc, &sha3_ctx, vh->sha,
505 SHA3_384_DIGEST_SIZE, "sha3-384");
506}
507
508static int verify_io_u_sha3_512(struct verify_header *hdr, struct vcont *vc)
509{
510 struct vhdr_sha3_512 *vh = hdr_priv(hdr);
511 uint8_t sha[SHA3_512_DIGEST_SIZE];
512 struct fio_sha3_ctx sha3_ctx = {
513 .sha = sha,
514 };
515
516 fio_sha3_512_init(&sha3_ctx);
517
518 return verify_io_u_sha3(hdr, vc, &sha3_ctx, vh->sha,
519 SHA3_512_DIGEST_SIZE, "sha3-512");
520}
521
936216f8 522static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
cd14cc10 523{
936216f8 524 void *p = io_u_verify_off(hdr, vc);
546dfd9f 525 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10 526 uint8_t sha512[128];
25dfa848 527 struct fio_sha512_ctx sha512_ctx = {
cd14cc10
JA
528 .buf = sha512,
529 };
530
936216f8 531 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 532
25dfa848 533 fio_sha512_init(&sha512_ctx);
ed3a433d 534 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr));
cd14cc10 535
bfacf389
JA
536 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
537 return 0;
cd14cc10 538
bfacf389
JA
539 vc->name = "sha512";
540 vc->good_crc = vh->sha512;
541 vc->bad_crc = sha512_ctx.buf;
542 vc->crc_len = sizeof(vh->sha512);
543 log_verify_failure(hdr, vc);
544 return EILSEQ;
cd14cc10
JA
545}
546
936216f8 547static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
cd14cc10 548{
936216f8 549 void *p = io_u_verify_off(hdr, vc);
546dfd9f 550 struct vhdr_sha256 *vh = hdr_priv(hdr);
bc77f56f 551 uint8_t sha256[64];
25dfa848 552 struct fio_sha256_ctx sha256_ctx = {
cd14cc10
JA
553 .buf = sha256,
554 };
555
936216f8 556 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 557
25dfa848 558 fio_sha256_init(&sha256_ctx);
ed3a433d 559 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr));
017052b6 560 fio_sha256_final(&sha256_ctx);
cd14cc10 561
bfacf389
JA
562 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
563 return 0;
cd14cc10 564
bfacf389
JA
565 vc->name = "sha256";
566 vc->good_crc = vh->sha256;
567 vc->bad_crc = sha256_ctx.buf;
568 vc->crc_len = sizeof(vh->sha256);
569 log_verify_failure(hdr, vc);
570 return EILSEQ;
cd14cc10
JA
571}
572
936216f8 573static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
7c353ceb 574{
936216f8 575 void *p = io_u_verify_off(hdr, vc);
7c353ceb
JA
576 struct vhdr_sha1 *vh = hdr_priv(hdr);
577 uint32_t sha1[5];
25dfa848 578 struct fio_sha1_ctx sha1_ctx = {
7c353ceb
JA
579 .H = sha1,
580 };
581
936216f8 582 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
7c353ceb 583
25dfa848 584 fio_sha1_init(&sha1_ctx);
ed3a433d 585 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr));
017052b6 586 fio_sha1_final(&sha1_ctx);
7c353ceb 587
bfacf389
JA
588 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
589 return 0;
7c353ceb 590
bfacf389
JA
591 vc->name = "sha1";
592 vc->good_crc = vh->sha1;
593 vc->bad_crc = sha1_ctx.H;
594 vc->crc_len = sizeof(vh->sha1);
595 log_verify_failure(hdr, vc);
596 return EILSEQ;
7c353ceb
JA
597}
598
936216f8 599static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
1e154bdb 600{
936216f8 601 void *p = io_u_verify_off(hdr, vc);
546dfd9f 602 struct vhdr_crc7 *vh = hdr_priv(hdr);
1e154bdb
JA
603 unsigned char c;
604
936216f8 605 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 606
ed3a433d 607 c = fio_crc7(p, hdr->len - hdr_size(vc->td, hdr));
1e154bdb 608
bfacf389
JA
609 if (c == vh->crc7)
610 return 0;
1e154bdb 611
bfacf389
JA
612 vc->name = "crc7";
613 vc->good_crc = &vh->crc7;
614 vc->bad_crc = &c;
615 vc->crc_len = 1;
616 log_verify_failure(hdr, vc);
617 return EILSEQ;
1e154bdb
JA
618}
619
936216f8 620static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
969f7ed3 621{
936216f8 622 void *p = io_u_verify_off(hdr, vc);
546dfd9f 623 struct vhdr_crc16 *vh = hdr_priv(hdr);
969f7ed3
JA
624 unsigned short c;
625
936216f8 626 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 627
ed3a433d 628 c = fio_crc16(p, hdr->len - hdr_size(vc->td, hdr));
969f7ed3 629
bfacf389
JA
630 if (c == vh->crc16)
631 return 0;
969f7ed3 632
bfacf389
JA
633 vc->name = "crc16";
634 vc->good_crc = &vh->crc16;
635 vc->bad_crc = &c;
636 vc->crc_len = 2;
637 log_verify_failure(hdr, vc);
638 return EILSEQ;
969f7ed3
JA
639}
640
936216f8 641static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
d77a7af3 642{
936216f8 643 void *p = io_u_verify_off(hdr, vc);
546dfd9f 644 struct vhdr_crc64 *vh = hdr_priv(hdr);
d77a7af3
JA
645 unsigned long long c;
646
936216f8 647 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 648
ed3a433d 649 c = fio_crc64(p, hdr->len - hdr_size(vc->td, hdr));
d77a7af3 650
bfacf389
JA
651 if (c == vh->crc64)
652 return 0;
d77a7af3 653
bfacf389
JA
654 vc->name = "crc64";
655 vc->good_crc = &vh->crc64;
656 vc->bad_crc = &c;
657 vc->crc_len = 8;
658 log_verify_failure(hdr, vc);
659 return EILSEQ;
d77a7af3
JA
660}
661
936216f8 662static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
e29d1b70 663{
936216f8 664 void *p = io_u_verify_off(hdr, vc);
546dfd9f
JA
665 struct vhdr_crc32 *vh = hdr_priv(hdr);
666 uint32_t c;
e29d1b70 667
936216f8 668 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 669
ed3a433d 670 c = fio_crc32(p, hdr->len - hdr_size(vc->td, hdr));
e29d1b70 671
bfacf389
JA
672 if (c == vh->crc32)
673 return 0;
e29d1b70 674
bfacf389
JA
675 vc->name = "crc32";
676 vc->good_crc = &vh->crc32;
677 vc->bad_crc = &c;
678 vc->crc_len = 4;
679 log_verify_failure(hdr, vc);
680 return EILSEQ;
e29d1b70
JA
681}
682
936216f8 683static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
bac39e0e 684{
936216f8 685 void *p = io_u_verify_off(hdr, vc);
bac39e0e
JA
686 struct vhdr_crc32 *vh = hdr_priv(hdr);
687 uint32_t c;
688
936216f8 689 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
bac39e0e 690
ed3a433d 691 c = fio_crc32c(p, hdr->len - hdr_size(vc->td, hdr));
bac39e0e 692
bfacf389
JA
693 if (c == vh->crc32)
694 return 0;
bac39e0e 695
bfacf389
JA
696 vc->name = "crc32c";
697 vc->good_crc = &vh->crc32;
698 vc->bad_crc = &c;
699 vc->crc_len = 4;
700 log_verify_failure(hdr, vc);
701 return EILSEQ;
bac39e0e
JA
702}
703
936216f8 704static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
e29d1b70 705{
936216f8 706 void *p = io_u_verify_off(hdr, vc);
546dfd9f 707 struct vhdr_md5 *vh = hdr_priv(hdr);
8c432325 708 uint32_t hash[MD5_HASH_WORDS];
25dfa848 709 struct fio_md5_ctx md5_ctx = {
8c432325
JA
710 .hash = hash,
711 };
e29d1b70 712
936216f8 713 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 714
25dfa848 715 fio_md5_init(&md5_ctx);
ed3a433d 716 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr));
017052b6 717 fio_md5_final(&md5_ctx);
e29d1b70 718
bfacf389
JA
719 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
720 return 0;
e29d1b70 721
bfacf389
JA
722 vc->name = "md5";
723 vc->good_crc = vh->md5_digest;
724 vc->bad_crc = md5_ctx.hash;
725 vc->crc_len = sizeof(hash);
726 log_verify_failure(hdr, vc);
727 return EILSEQ;
e29d1b70
JA
728}
729
e8462bd8
JA
730/*
731 * Push IO verification to a separate thread
732 */
f8b0bd10 733int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
e8462bd8 734{
f8b0bd10 735 struct io_u *io_u = *io_u_ptr;
e8462bd8 736
e8462bd8 737 pthread_mutex_lock(&td->io_u_lock);
d7ee2a7d 738
f8b0bd10
JA
739 if (io_u->file)
740 put_file_log(td, io_u->file);
741
0c41214f
RR
742 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
743 td->cur_depth--;
1651e431 744 io_u_clear(td, io_u, IO_U_F_IN_CUR_DEPTH);
0c41214f 745 }
2ae0b204 746 flist_add_tail(&io_u->verify_list, &td->verify_list);
f8b0bd10 747 *io_u_ptr = NULL;
e8462bd8
JA
748
749 pthread_cond_signal(&td->verify_cond);
b93a8eb0 750 pthread_mutex_unlock(&td->io_u_lock);
e8462bd8
JA
751 return 0;
752}
753
323f58c5
JA
754/*
755 * Thanks Rusty, for spending the time so I don't have to.
756 *
757 * http://rusty.ozlabs.org/?p=560
758 */
759static int mem_is_zero(const void *data, size_t length)
760{
761 const unsigned char *p = data;
762 size_t len;
763
764 /* Check first 16 bytes manually */
765 for (len = 0; len < 16; len++) {
766 if (!length)
767 return 1;
768 if (*p)
769 return 0;
770 p++;
771 length--;
772 }
773
774 /* Now we know that's zero, memcmp with self. */
775 return memcmp(data, p, length) == 0;
776}
777
778static int mem_is_zero_slow(const void *data, size_t length, size_t *offset)
779{
780 const unsigned char *p = data;
781
782 *offset = 0;
783 while (length) {
784 if (*p)
785 break;
786 (*offset)++;
787 length--;
788 p++;
789 }
790
791 return !length;
792}
793
0d29de83
JA
794static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
795{
323f58c5 796 size_t offset;
0d29de83
JA
797
798 if (!td->o.trim_zero)
799 return 0;
800
323f58c5 801 if (mem_is_zero(io_u->buf, io_u->buflen))
0d29de83
JA
802 return 0;
803
323f58c5
JA
804 mem_is_zero_slow(io_u->buf, io_u->buflen, &offset);
805
5fff9543 806 log_err("trim: verify failed at file %s offset %llu, length %llu"
a917a8b3
JA
807 ", block offset %lu\n",
808 io_u->file->file_name, io_u->offset, io_u->buflen,
323f58c5
JA
809 (unsigned long) offset);
810 return EILSEQ;
0d29de83
JA
811}
812
b638d82f
RP
813static int verify_header(struct io_u *io_u, struct thread_data *td,
814 struct verify_header *hdr, unsigned int hdr_num,
815 unsigned int hdr_len)
f65d1c26
JA
816{
817 void *p = hdr;
818 uint32_t crc;
819
5964842c
AG
820 if (hdr->magic != FIO_HDR_MAGIC) {
821 log_err("verify: bad magic header %x, wanted %x",
822 hdr->magic, FIO_HDR_MAGIC);
823 goto err;
824 }
4445856a 825 if (hdr->len != hdr_len) {
5964842c
AG
826 log_err("verify: bad header length %u, wanted %u",
827 hdr->len, hdr_len);
828 goto err;
829 }
830 if (hdr->rand_seed != io_u->rand_seed) {
831 log_err("verify: bad header rand_seed %"PRIu64
832 ", wanted %"PRIu64,
833 hdr->rand_seed, io_u->rand_seed);
834 goto err;
835 }
b638d82f
RP
836 if (hdr->offset != io_u->offset + hdr_num * td->o.verify_interval) {
837 log_err("verify: bad header offset %"PRIu64
838 ", wanted %llu",
839 hdr->offset, io_u->offset);
840 goto err;
841 }
842
843 /*
844 * For read-only workloads, the program cannot be certain of the
845 * last numberio written to a block. Checking of numberio will be
846 * done only for workloads that write data. For verify_only,
847 * numberio will be checked in the last iteration when the correct
848 * state of numberio, that would have been written to each block
849 * in a previous run of fio, has been reached.
850 */
dcf9844e 851 if (td_write(td) && (td_min_bs(td) == td_max_bs(td)) &&
b638d82f
RP
852 !td->o.time_based)
853 if (!td->o.verify_only || td->o.loops == 0)
854 if (hdr->numberio != io_u->numberio) {
855 log_err("verify: bad header numberio %"PRIu16
856 ", wanted %"PRIu16,
857 hdr->numberio, io_u->numberio);
858 goto err;
859 }
ae38c0dc 860
25dfa848 861 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
5964842c
AG
862 if (crc != hdr->crc32) {
863 log_err("verify: bad header crc %x, calculated %x",
864 hdr->crc32, crc);
865 goto err;
866 }
867 return 0;
868
869err:
46ff70e8
FC
870 log_err(" at file %s offset %llu, length %u"
871 " (requested block: offset=%llu, length=%llu)\n",
5964842c 872 io_u->file->file_name,
46ff70e8
FC
873 io_u->offset + hdr_num * hdr_len, hdr_len,
874 io_u->offset, io_u->buflen);
16758b59
JA
875
876 if (td->o.verify_dump)
877 dump_buf(p, hdr_len, io_u->offset + hdr_num * hdr_len,
878 "hdr_fail", io_u->file);
879
5964842c 880 return EILSEQ;
f65d1c26
JA
881}
882
f8b0bd10 883int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
e29d1b70 884{
3f9f4e26 885 struct verify_header *hdr;
f8b0bd10 886 struct io_u *io_u = *io_u_ptr;
2b13e716 887 unsigned int header_size, hdr_inc, hdr_num = 0;
95646108 888 void *p;
e29d1b70
JA
889 int ret;
890
1dcc0498 891 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b 892 return 0;
5c57c084
JA
893 /*
894 * If the IO engine is faking IO (like null), then just pretend
895 * we verified everything.
896 */
9b87f09b 897 if (td_ioengine_flagged(td, FIO_FAKEIO))
5c57c084
JA
898 return 0;
899
0d29de83
JA
900 if (io_u->flags & IO_U_F_TRIMMED) {
901 ret = verify_trimmed_io_u(td, io_u);
902 goto done;
903 }
36690c9b 904
cfbcd0dc 905 hdr_inc = get_hdr_inc(td, io_u);
e29d1b70 906
a12a3b4d 907 ret = 0;
5ec10eaa
JA
908 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
909 p += hdr_inc, hdr_num++) {
bfacf389
JA
910 struct vcont vc = {
911 .io_u = io_u,
912 .hdr_num = hdr_num,
7d9fb455 913 .td = td,
bfacf389 914 };
f00b210f 915 unsigned int verify_type;
936216f8 916
f3e6cb95 917 if (ret && td->o.verify_fatal)
a12a3b4d 918 break;
f3e6cb95 919
2b13e716 920 header_size = __hdr_size(td->o.verify);
a59e170d 921 if (td->o.verify_offset)
2b13e716 922 memswp(p, p + td->o.verify_offset, header_size);
95646108 923 hdr = p;
e29d1b70 924
c4b6117b 925 /*
f31feaa2 926 * Make rand_seed check pass when have verify_backlog.
c4b6117b 927 */
f31feaa2 928 if (!td_rw(td) || (td->flags & TD_F_VER_BACKLOG))
c4b6117b
PV
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 1006done:
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
1013static 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
1023static 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
1029static 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
1040static 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
1051static 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
1062static 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
1073static 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
1084static 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
1096static 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
1108static 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
1115static 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
1122static 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
1129static 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
1136static 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
1143static 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
1155static 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
1174static 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 if (td->o.verify != VERIFY_PATTERN_NO_HDR)
1179 __fill_hdr(td, io_u, hdr, header_num, header_len, rand_seed);
ed3a433d
JA
1180}
1181
7d9fb455
JA
1182static void populate_hdr(struct thread_data *td, struct io_u *io_u,
1183 struct verify_header *hdr, unsigned int header_num,
1184 unsigned int header_len)
1185{
1186 unsigned int data_len;
d637b8b8
TK
1187 void *data;
1188 char *p;
7d9fb455 1189
d637b8b8 1190 p = (char *) hdr;
7d9fb455 1191
b638d82f 1192 fill_hdr(td, io_u, hdr, header_num, header_len, io_u->rand_seed);
f65d1c26 1193
3982ec03
JA
1194 if (header_len <= hdr_size(td, hdr)) {
1195 td_verror(td, EINVAL, "Blocksize too small");
1196 return;
1197 }
ed3a433d 1198 data_len = header_len - hdr_size(td, hdr);
7d9fb455 1199
ed3a433d 1200 data = p + hdr_size(td, hdr);
7d9fb455
JA
1201 switch (td->o.verify) {
1202 case VERIFY_MD5:
1203 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1204 io_u, hdr->len);
1205 fill_md5(hdr, data, data_len);
1206 break;
1207 case VERIFY_CRC64:
1208 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1209 io_u, hdr->len);
1210 fill_crc64(hdr, data, data_len);
1211 break;
1212 case VERIFY_CRC32C:
1213 case VERIFY_CRC32C_INTEL:
1214 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1215 io_u, hdr->len);
1216 fill_crc32c(hdr, data, data_len);
1217 break;
1218 case VERIFY_CRC32:
1219 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1220 io_u, hdr->len);
1221 fill_crc32(hdr, data, data_len);
1222 break;
1223 case VERIFY_CRC16:
1224 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1225 io_u, hdr->len);
1226 fill_crc16(hdr, data, data_len);
1227 break;
1228 case VERIFY_CRC7:
1229 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1230 io_u, hdr->len);
1231 fill_crc7(hdr, data, data_len);
1232 break;
1233 case VERIFY_SHA256:
1234 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1235 io_u, hdr->len);
1236 fill_sha256(hdr, data, data_len);
1237 break;
1238 case VERIFY_SHA512:
1239 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1240 io_u, hdr->len);
1241 fill_sha512(hdr, data, data_len);
1242 break;
ae3a5acc
JA
1243 case VERIFY_SHA3_224:
1244 dprint(FD_VERIFY, "fill sha3-224 io_u %p, len %u\n",
1245 io_u, hdr->len);
1246 fill_sha3_224(hdr, data, data_len);
1247 break;
1248 case VERIFY_SHA3_256:
1249 dprint(FD_VERIFY, "fill sha3-256 io_u %p, len %u\n",
1250 io_u, hdr->len);
1251 fill_sha3_256(hdr, data, data_len);
1252 break;
1253 case VERIFY_SHA3_384:
1254 dprint(FD_VERIFY, "fill sha3-384 io_u %p, len %u\n",
1255 io_u, hdr->len);
1256 fill_sha3_384(hdr, data, data_len);
1257 break;
1258 case VERIFY_SHA3_512:
1259 dprint(FD_VERIFY, "fill sha3-512 io_u %p, len %u\n",
1260 io_u, hdr->len);
1261 fill_sha3_512(hdr, data, data_len);
1262 break;
844ea602
JA
1263 case VERIFY_XXHASH:
1264 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1265 io_u, hdr->len);
1266 fill_xxhash(hdr, data, data_len);
1267 break;
7d9fb455
JA
1268 case VERIFY_SHA1:
1269 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1270 io_u, hdr->len);
1271 fill_sha1(hdr, data, data_len);
1272 break;
b638d82f 1273 case VERIFY_HDR_ONLY:
92bf48d5 1274 case VERIFY_PATTERN:
ed3a433d 1275 case VERIFY_PATTERN_NO_HDR:
92bf48d5
JA
1276 /* nothing to do here */
1277 break;
7d9fb455
JA
1278 default:
1279 log_err("fio: bad verify type: %d\n", td->o.verify);
1280 assert(0);
1281 }
ed3a433d
JA
1282
1283 if (td->o.verify_offset && hdr_size(td, hdr))
1284 memswp(p, p + td->o.verify_offset, hdr_size(td, hdr));
7d9fb455
JA
1285}
1286
e29d1b70
JA
1287/*
1288 * fill body of io_u->buf with random data and add a header with the
c50ca7bd 1289 * checksum of choice
e29d1b70
JA
1290 */
1291void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1292{
9cc3d150
JA
1293 if (td->o.verify == VERIFY_NULL)
1294 return;
1295
da0a7bd2
JC
1296 io_u->numberio = td->io_issues[io_u->ddir];
1297
c50ca7bd 1298 fill_pattern_headers(td, io_u, 0, 0);
e29d1b70
JA
1299}
1300
1301int get_next_verify(struct thread_data *td, struct io_u *io_u)
1302{
8de8f047 1303 struct io_piece *ipo = NULL;
e29d1b70 1304
d2d7fa53
JA
1305 /*
1306 * this io_u is from a requeue, we already filled the offsets
1307 */
1308 if (io_u->file)
1309 return 0;
1310
8de8f047 1311 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
e96c0125 1312 struct fio_rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 1313
8de8f047 1314 ipo = rb_entry(n, struct io_piece, rb_node);
f9401285
JA
1315
1316 /*
1317 * Ensure that the associated IO has completed
1318 */
1319 read_barrier();
1320 if (ipo->flags & IP_F_IN_FLIGHT)
1321 goto nothing;
1322
4b87898e 1323 rb_erase(n, &td->io_hist_tree);
a917a8b3
JA
1324 assert(ipo->flags & IP_F_ONRB);
1325 ipo->flags &= ~IP_F_ONRB;
01743ee1 1326 } else if (!flist_empty(&td->io_hist_list)) {
9342d5f8 1327 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
f9401285
JA
1328
1329 /*
1330 * Ensure that the associated IO has completed
1331 */
1332 read_barrier();
1333 if (ipo->flags & IP_F_IN_FLIGHT)
1334 goto nothing;
1335
01743ee1 1336 flist_del(&ipo->list);
a917a8b3
JA
1337 assert(ipo->flags & IP_F_ONLIST);
1338 ipo->flags &= ~IP_F_ONLIST;
8de8f047 1339 }
e29d1b70 1340
8de8f047 1341 if (ipo) {
0d29de83
JA
1342 td->io_hist_len--;
1343
e29d1b70
JA
1344 io_u->offset = ipo->offset;
1345 io_u->buflen = ipo->len;
da0a7bd2 1346 io_u->numberio = ipo->numberio;
36167d82 1347 io_u->file = ipo->file;
1651e431 1348 io_u_set(td, io_u, IO_U_F_VER_LIST);
97af62ce 1349
0d29de83 1350 if (ipo->flags & IP_F_TRIMMED)
1651e431 1351 io_u_set(td, io_u, IO_U_F_TRIMMED);
0d29de83 1352
d6aed795 1353 if (!fio_file_open(io_u->file)) {
97af62ce
JA
1354 int r = td_io_open_file(td, io_u->file);
1355
bd6f78b2
JA
1356 if (r) {
1357 dprint(FD_VERIFY, "failed file %s open\n",
1358 io_u->file->file_name);
97af62ce 1359 return 1;
bd6f78b2 1360 }
97af62ce
JA
1361 }
1362
1363 get_file(ipo->file);
d6aed795 1364 assert(fio_file_open(io_u->file));
e29d1b70 1365 io_u->ddir = DDIR_READ;
36167d82
JA
1366 io_u->xfer_buf = io_u->buf;
1367 io_u->xfer_buflen = io_u->buflen;
0d29de83
JA
1368
1369 remove_trim_entry(td, ipo);
e29d1b70 1370 free(ipo);
bd6f78b2 1371 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
c4b6117b
PV
1372
1373 if (!td->o.verify_pattern_bytes) {
d6b72507 1374 io_u->rand_seed = __rand(&td->verify_state);
c4b6117b 1375 if (sizeof(int) != sizeof(long *))
d6b72507 1376 io_u->rand_seed *= __rand(&td->verify_state);
c4b6117b 1377 }
e29d1b70
JA
1378 return 0;
1379 }
1380
f9401285 1381nothing:
bd6f78b2 1382 dprint(FD_VERIFY, "get_next_verify: empty\n");
e29d1b70
JA
1383 return 1;
1384}
e8462bd8 1385
dc5bfbb2
JA
1386void fio_verify_init(struct thread_data *td)
1387{
1388 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1389 td->o.verify == VERIFY_CRC32C) {
214e2d56 1390 crc32c_arm64_probe();
dc5bfbb2
JA
1391 crc32c_intel_probe();
1392 }
1393}
1394
e8462bd8
JA
1395static void *verify_async_thread(void *data)
1396{
1397 struct thread_data *td = data;
1398 struct io_u *io_u;
1399 int ret = 0;
1400
b2a9e649 1401 if (fio_option_is_set(&td->o, verify_cpumask) &&
e8462bd8
JA
1402 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1403 log_err("fio: failed setting verify thread affinity\n");
1404 goto done;
1405 }
1406
1407 do {
e53ab27c
JA
1408 FLIST_HEAD(list);
1409
e8462bd8
JA
1410 read_barrier();
1411 if (td->verify_thread_exit)
1412 break;
1413
1414 pthread_mutex_lock(&td->io_u_lock);
1415
1416 while (flist_empty(&td->verify_list) &&
1417 !td->verify_thread_exit) {
b36e298b
JA
1418 ret = pthread_cond_wait(&td->verify_cond,
1419 &td->io_u_lock);
e8462bd8
JA
1420 if (ret) {
1421 pthread_mutex_unlock(&td->io_u_lock);
1422 break;
1423 }
1424 }
1425
e53ab27c
JA
1426 flist_splice_init(&td->verify_list, &list);
1427 pthread_mutex_unlock(&td->io_u_lock);
1428
1429 if (flist_empty(&list))
e8462bd8 1430 continue;
e8462bd8 1431
e53ab27c 1432 while (!flist_empty(&list)) {
9342d5f8 1433 io_u = flist_first_entry(&list, struct io_u, verify_list);
f8b0bd10
JA
1434 flist_del_init(&io_u->verify_list);
1435
1651e431 1436 io_u_set(td, io_u, IO_U_F_NO_FILE_PUT);
f8b0bd10 1437 ret = verify_io_u(td, &io_u);
e8462bd8 1438
e53ab27c 1439 put_io_u(td, io_u);
d561f2ab
JA
1440 if (!ret)
1441 continue;
8b28bd41 1442 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
d561f2ab
JA
1443 update_error_count(td, ret);
1444 td_clear_error(td);
1445 ret = 0;
1446 }
e53ab27c 1447 }
e8462bd8
JA
1448 } while (!ret);
1449
d561f2ab
JA
1450 if (ret) {
1451 td_verror(td, ret, "async_verify");
f3e6cb95 1452 if (td->o.verify_fatal)
ebea2133 1453 fio_mark_td_terminate(td);
d561f2ab
JA
1454 }
1455
e8462bd8
JA
1456done:
1457 pthread_mutex_lock(&td->io_u_lock);
1458 td->nr_verify_threads--;
564de8d1 1459 pthread_cond_signal(&td->free_cond);
e8462bd8
JA
1460 pthread_mutex_unlock(&td->io_u_lock);
1461
e8462bd8
JA
1462 return NULL;
1463}
1464
1465int verify_async_init(struct thread_data *td)
1466{
1467 int i, ret;
304a47c7
VA
1468 pthread_attr_t attr;
1469
1470 pthread_attr_init(&attr);
45213f1b 1471 pthread_attr_setstacksize(&attr, 2 * PTHREAD_STACK_MIN);
e8462bd8
JA
1472
1473 td->verify_thread_exit = 0;
1474
1475 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1476 for (i = 0; i < td->o.verify_async; i++) {
304a47c7 1477 ret = pthread_create(&td->verify_threads[i], &attr,
e8462bd8
JA
1478 verify_async_thread, td);
1479 if (ret) {
1480 log_err("fio: async verify creation failed: %s\n",
1481 strerror(ret));
1482 break;
1483 }
1484 ret = pthread_detach(td->verify_threads[i]);
1485 if (ret) {
1486 log_err("fio: async verify thread detach failed: %s\n",
1487 strerror(ret));
1488 break;
1489 }
1490 td->nr_verify_threads++;
1491 }
1492
304a47c7
VA
1493 pthread_attr_destroy(&attr);
1494
e8462bd8 1495 if (i != td->o.verify_async) {
e40823b1 1496 log_err("fio: only %d verify threads started, exiting\n", i);
564de8d1
BVA
1497
1498 pthread_mutex_lock(&td->io_u_lock);
e8462bd8 1499 td->verify_thread_exit = 1;
e8462bd8 1500 pthread_cond_broadcast(&td->verify_cond);
564de8d1
BVA
1501 pthread_mutex_unlock(&td->io_u_lock);
1502
e8462bd8
JA
1503 return 1;
1504 }
1505
1506 return 0;
1507}
1508
1509void verify_async_exit(struct thread_data *td)
1510{
564de8d1 1511 pthread_mutex_lock(&td->io_u_lock);
e8462bd8 1512 td->verify_thread_exit = 1;
e8462bd8
JA
1513 pthread_cond_broadcast(&td->verify_cond);
1514
e8462bd8
JA
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
3376ecf4 1528 typecheck(__typeof__(off), io->offset);
61b9861d
RP
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 }
36833fb0 1642 snprintf((char *) s->name, sizeof(s->name), "%s", td->o.name);
ca09be4b
JA
1643 next = io_list_next(s);
1644 }
1645
1646 return rep;
1647}
1648
1649static int open_state_file(const char *name, const char *prefix, int num,
1650 int for_write)
1651{
d117b2bd 1652 char out[PATH_MAX];
ca09be4b
JA
1653 int flags;
1654 int fd;
1655
1656 if (for_write)
1657 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1658 else
1659 flags = O_RDONLY;
1660
e499aedc 1661 verify_state_gen_name(out, sizeof(out), name, prefix, num);
ca09be4b
JA
1662
1663 fd = open(out, flags, 0644);
1664 if (fd == -1) {
1665 perror("fio: open state file");
d117b2bd 1666 log_err("fio: state file: %s (for_write=%d)\n", out, for_write);
ca09be4b
JA
1667 return -1;
1668 }
1669
1670 return fd;
1671}
1672
1673static int write_thread_list_state(struct thread_io_list *s,
1674 const char *prefix)
1675{
1676 struct verify_state_hdr hdr;
1677 uint64_t crc;
1678 ssize_t ret;
1679 int fd;
1680
1681 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1682 if (fd == -1)
1683 return 1;
1684
1685 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1686
1687 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1688 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1689 hdr.crc = cpu_to_le64(crc);
1690 ret = write(fd, &hdr, sizeof(hdr));
1691 if (ret != sizeof(hdr))
1692 goto write_fail;
1693
1694 ret = write(fd, s, thread_io_list_sz(s));
1695 if (ret != thread_io_list_sz(s)) {
1696write_fail:
1697 if (ret < 0)
1698 perror("fio: write state file");
1699 log_err("fio: failed to write state file\n");
1700 ret = 1;
1701 } else
1702 ret = 0;
1703
1704 close(fd);
1705 return ret;
1706}
1707
1708void __verify_save_state(struct all_io_list *state, const char *prefix)
1709{
1710 struct thread_io_list *s = &state->state[0];
1711 unsigned int i;
1712
1713 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1714 write_thread_list_state(s, prefix);
1715 s = io_list_next(s);
1716 }
1717}
1718
d264264a 1719void verify_save_state(int mask)
ca09be4b
JA
1720{
1721 struct all_io_list *state;
1722 size_t sz;
1723
d264264a 1724 state = get_all_io_list(mask, &sz);
ca09be4b 1725 if (state) {
d264264a
JA
1726 char prefix[PATH_MAX];
1727
1728 if (aux_path)
53a7af85 1729 sprintf(prefix, "%s%clocal", aux_path, FIO_OS_PATH_SEPARATOR);
d264264a 1730 else
c6d140fb 1731 strcpy(prefix, "local");
d264264a
JA
1732
1733 __verify_save_state(state, prefix);
ca09be4b
JA
1734 free(state);
1735 }
1736}
1737
1738void verify_free_state(struct thread_data *td)
1739{
1740 if (td->vstate)
1741 free(td->vstate);
1742}
1743
94a6e1bb 1744void verify_assign_state(struct thread_data *td, void *p)
ca09be4b 1745{
94a6e1bb 1746 struct thread_io_list *s = p;
ca09be4b
JA
1747 int i;
1748
94a6e1bb
JA
1749 s->no_comps = le64_to_cpu(s->no_comps);
1750 s->depth = le32_to_cpu(s->depth);
1751 s->nofiles = le32_to_cpu(s->nofiles);
1752 s->numberio = le64_to_cpu(s->numberio);
1753 s->rand.use64 = le64_to_cpu(s->rand.use64);
c3546b53 1754
94a6e1bb
JA
1755 if (s->rand.use64) {
1756 for (i = 0; i < 6; i++)
1757 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
c3546b53 1758 } else {
94a6e1bb
JA
1759 for (i = 0; i < 4; i++)
1760 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1761 }
c3546b53 1762
94a6e1bb
JA
1763 for (i = 0; i < s->no_comps; i++) {
1764 s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno);
1765 s->comps[i].offset = le64_to_cpu(s->comps[i].offset);
c3546b53 1766 }
ca09be4b 1767
94a6e1bb 1768 td->vstate = p;
ca09be4b
JA
1769}
1770
94a6e1bb 1771int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s)
ca09be4b
JA
1772{
1773 uint64_t crc;
1774
1775 hdr->version = le64_to_cpu(hdr->version);
1776 hdr->size = le64_to_cpu(hdr->size);
1777 hdr->crc = le64_to_cpu(hdr->crc);
1778
94a6e1bb 1779 if (hdr->version != VSTATE_HDR_VERSION)
ca09be4b
JA
1780 return 1;
1781
1782 crc = fio_crc32c((void *)s, hdr->size);
1783 if (crc != hdr->crc)
1784 return 1;
1785
1786 return 0;
1787}
1788
1789int verify_load_state(struct thread_data *td, const char *prefix)
1790{
ca09be4b 1791 struct verify_state_hdr hdr;
c3546b53 1792 void *s = NULL;
ca09be4b
JA
1793 uint64_t crc;
1794 ssize_t ret;
1795 int fd;
1796
1797 if (!td->o.verify_state)
1798 return 0;
1799
1800 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1801 if (fd == -1)
1802 return 1;
1803
1804 ret = read(fd, &hdr, sizeof(hdr));
1805 if (ret != sizeof(hdr)) {
1806 if (ret < 0)
1807 td_verror(td, errno, "read verify state hdr");
1808 log_err("fio: failed reading verify state header\n");
1809 goto err;
1810 }
1811
1812 hdr.version = le64_to_cpu(hdr.version);
1813 hdr.size = le64_to_cpu(hdr.size);
1814 hdr.crc = le64_to_cpu(hdr.crc);
1815
94a6e1bb
JA
1816 if (hdr.version != VSTATE_HDR_VERSION) {
1817 log_err("fio: unsupported (%d) version in verify state header\n",
1818 (unsigned int) hdr.version);
ca09be4b
JA
1819 goto err;
1820 }
1821
1822 s = malloc(hdr.size);
1823 ret = read(fd, s, hdr.size);
1824 if (ret != hdr.size) {
1825 if (ret < 0)
1826 td_verror(td, errno, "read verify state");
1827 log_err("fio: failed reading verity state\n");
1828 goto err;
1829 }
1830
c3546b53 1831 crc = fio_crc32c(s, hdr.size);
ca09be4b
JA
1832 if (crc != hdr.crc) {
1833 log_err("fio: verify state is corrupt\n");
1834 goto err;
1835 }
1836
1837 close(fd);
1838
94a6e1bb 1839 verify_assign_state(td, s);
ca09be4b
JA
1840 return 0;
1841err:
1842 if (s)
1843 free(s);
1844 close(fd);
1845 return 1;
1846}
1847
1848/*
1849 * Use the loaded verify state to know when to stop doing verification
1850 */
1851int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1852{
1853 struct thread_io_list *s = td->vstate;
94a6e1bb 1854 struct fio_file *f = io_u->file;
ca09be4b
JA
1855 int i;
1856
94a6e1bb 1857 if (!s || !f)
ca09be4b
JA
1858 return 0;
1859
1860 /*
def00095
JA
1861 * If we're not into the window of issues - depth yet, continue. If
1862 * issue is shorter than depth, do check.
ca09be4b 1863 */
def00095
JA
1864 if ((td->io_blocks[DDIR_READ] < s->depth ||
1865 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1866 s->numberio > s->depth)
ca09be4b
JA
1867 return 0;
1868
1869 /*
1870 * We're in the window of having to check if this io was
1871 * completed or not. If the IO was seen as completed, then
1872 * lets verify it.
1873 */
94a6e1bb
JA
1874 for (i = 0; i < s->no_comps; i++) {
1875 if (s->comps[i].fileno != f->fileno)
1876 continue;
1877 if (io_u->offset == s->comps[i].offset)
ca09be4b 1878 return 0;
94a6e1bb 1879 }
ca09be4b
JA
1880
1881 /*
1882 * Not found, we have to stop
1883 */
1884 return 1;
1885}