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