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