Merge branch 'fix-randtrimwrite' of https://github.com/minwooim/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{
434 return td->o.verify_async &&
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 }
4445856a 925 if (hdr->len != hdr_len) {
5964842c
AG
926 log_err("verify: bad header length %u, wanted %u",
927 hdr->len, hdr_len);
928 goto err;
929 }
703d04e7 930 if (td->o.verify_header_seed && (hdr->rand_seed != io_u->rand_seed)) {
5964842c
AG
931 log_err("verify: bad header rand_seed %"PRIu64
932 ", wanted %"PRIu64,
933 hdr->rand_seed, io_u->rand_seed);
934 goto err;
935 }
4fff54cc 936 if (hdr->offset != io_u->verify_offset + hdr_num * td->o.verify_interval) {
b638d82f
RP
937 log_err("verify: bad header offset %"PRIu64
938 ", wanted %llu",
4fff54cc 939 hdr->offset, io_u->verify_offset);
b638d82f
RP
940 goto err;
941 }
942
943 /*
944 * For read-only workloads, the program cannot be certain of the
945 * last numberio written to a block. Checking of numberio will be
2dd80ee4
JG
946 * done only for workloads that write data. For verify_only or
947 * any mode de-selecting verify_write_sequence, numberio check is
948 * skipped.
b638d82f 949 */
dcf9844e 950 if (td_write(td) && (td_min_bs(td) == td_max_bs(td)) &&
b638d82f 951 !td->o.time_based)
2dd80ee4 952 if (td->o.verify_write_sequence)
b638d82f
RP
953 if (hdr->numberio != io_u->numberio) {
954 log_err("verify: bad header numberio %"PRIu16
955 ", wanted %"PRIu16,
956 hdr->numberio, io_u->numberio);
957 goto err;
958 }
ae38c0dc 959
25dfa848 960 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
5964842c
AG
961 if (crc != hdr->crc32) {
962 log_err("verify: bad header crc %x, calculated %x",
963 hdr->crc32, crc);
964 goto err;
965 }
966 return 0;
967
968err:
46ff70e8
FC
969 log_err(" at file %s offset %llu, length %u"
970 " (requested block: offset=%llu, length=%llu)\n",
5964842c 971 io_u->file->file_name,
4fff54cc
JA
972 io_u->verify_offset + hdr_num * hdr_len, hdr_len,
973 io_u->verify_offset, io_u->buflen);
16758b59
JA
974
975 if (td->o.verify_dump)
4fff54cc 976 dump_buf(p, hdr_len, io_u->verify_offset + hdr_num * hdr_len,
16758b59
JA
977 "hdr_fail", io_u->file);
978
5964842c 979 return EILSEQ;
f65d1c26
JA
980}
981
f8b0bd10 982int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
e29d1b70 983{
3f9f4e26 984 struct verify_header *hdr;
f8b0bd10 985 struct io_u *io_u = *io_u_ptr;
2b13e716 986 unsigned int header_size, hdr_inc, hdr_num = 0;
95646108 987 void *p;
e29d1b70
JA
988 int ret;
989
1dcc0498 990 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b 991 return 0;
5c57c084
JA
992 /*
993 * If the IO engine is faking IO (like null), then just pretend
994 * we verified everything.
995 */
9b87f09b 996 if (td_ioengine_flagged(td, FIO_FAKEIO))
5c57c084
JA
997 return 0;
998
6170d92a
MI
999 /*
1000 * If data has already been verified from the device, we can skip
1001 * the actual verification phase here.
1002 */
1003 if (io_u->flags & IO_U_F_VER_IN_DEV)
1004 return 0;
1005
0d29de83
JA
1006 if (io_u->flags & IO_U_F_TRIMMED) {
1007 ret = verify_trimmed_io_u(td, io_u);
1008 goto done;
1009 }
36690c9b 1010
cfbcd0dc 1011 hdr_inc = get_hdr_inc(td, io_u);
e29d1b70 1012
a12a3b4d 1013 ret = 0;
5ec10eaa
JA
1014 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
1015 p += hdr_inc, hdr_num++) {
bfacf389
JA
1016 struct vcont vc = {
1017 .io_u = io_u,
1018 .hdr_num = hdr_num,
7d9fb455 1019 .td = td,
bfacf389 1020 };
f00b210f 1021 unsigned int verify_type;
936216f8 1022
f3e6cb95 1023 if (ret && td->o.verify_fatal)
a12a3b4d 1024 break;
f3e6cb95 1025
2b13e716 1026 header_size = __hdr_size(td->o.verify);
a59e170d 1027 if (td->o.verify_offset)
2b13e716 1028 memswp(p, p + td->o.verify_offset, header_size);
95646108 1029 hdr = p;
e29d1b70 1030
59245381 1031 if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
b638d82f 1032 ret = verify_header(io_u, td, hdr, hdr_num, hdr_inc);
59245381
JA
1033 if (ret)
1034 return ret;
1035 }
3f199b01 1036
f00b210f
JA
1037 if (td->o.verify != VERIFY_NONE)
1038 verify_type = td->o.verify;
1039 else
1040 verify_type = hdr->verify_type;
1041
1042 switch (verify_type) {
b638d82f
RP
1043 case VERIFY_HDR_ONLY:
1044 /* Header is always verified, check if pattern is left
1045 * for verification. */
1046 if (td->o.verify_pattern_bytes)
1047 ret = verify_io_u_pattern(hdr, &vc);
1048 break;
3f9f4e26 1049 case VERIFY_MD5:
936216f8 1050 ret = verify_io_u_md5(hdr, &vc);
3f9f4e26
SL
1051 break;
1052 case VERIFY_CRC64:
936216f8 1053 ret = verify_io_u_crc64(hdr, &vc);
3f9f4e26 1054 break;
bac39e0e 1055 case VERIFY_CRC32C:
3845591f 1056 case VERIFY_CRC32C_INTEL:
936216f8 1057 ret = verify_io_u_crc32c(hdr, &vc);
bac39e0e 1058 break;
3f9f4e26 1059 case VERIFY_CRC32:
936216f8 1060 ret = verify_io_u_crc32(hdr, &vc);
3f9f4e26
SL
1061 break;
1062 case VERIFY_CRC16:
936216f8 1063 ret = verify_io_u_crc16(hdr, &vc);
3f9f4e26
SL
1064 break;
1065 case VERIFY_CRC7:
936216f8 1066 ret = verify_io_u_crc7(hdr, &vc);
3f9f4e26 1067 break;
cd14cc10 1068 case VERIFY_SHA256:
936216f8 1069 ret = verify_io_u_sha256(hdr, &vc);
cd14cc10
JA
1070 break;
1071 case VERIFY_SHA512:
936216f8 1072 ret = verify_io_u_sha512(hdr, &vc);
cd14cc10 1073 break;
ae3a5acc
JA
1074 case VERIFY_SHA3_224:
1075 ret = verify_io_u_sha3_224(hdr, &vc);
1076 break;
1077 case VERIFY_SHA3_256:
1078 ret = verify_io_u_sha3_256(hdr, &vc);
1079 break;
1080 case VERIFY_SHA3_384:
1081 ret = verify_io_u_sha3_384(hdr, &vc);
1082 break;
1083 case VERIFY_SHA3_512:
1084 ret = verify_io_u_sha3_512(hdr, &vc);
1085 break;
844ea602
JA
1086 case VERIFY_XXHASH:
1087 ret = verify_io_u_xxhash(hdr, &vc);
1088 break;
7c353ceb 1089 case VERIFY_SHA1:
936216f8 1090 ret = verify_io_u_sha1(hdr, &vc);
7c353ceb 1091 break;
92bf48d5 1092 case VERIFY_PATTERN:
59245381 1093 case VERIFY_PATTERN_NO_HDR:
92bf48d5
JA
1094 ret = verify_io_u_pattern(hdr, &vc);
1095 break;
3f9f4e26
SL
1096 default:
1097 log_err("Bad verify type %u\n", hdr->verify_type);
d16d4e09 1098 ret = EINVAL;
3f9f4e26 1099 }
f00b210f 1100
76a8799b 1101 if (ret && verify_type != hdr->verify_type && verify_type != VERIFY_PATTERN_NO_HDR)
f00b210f
JA
1102 log_err("fio: verify type mismatch (%u media, %u given)\n",
1103 hdr->verify_type, verify_type);
3f9f4e26 1104 }
a7dfe862 1105
0d29de83 1106done:
f3e6cb95 1107 if (ret && td->o.verify_fatal)
ebea2133 1108 fio_mark_td_terminate(td);
f3e6cb95 1109
a12a3b4d 1110 return ret;
e29d1b70
JA
1111}
1112
844ea602
JA
1113static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
1114{
1115 struct vhdr_xxhash *vh = hdr_priv(hdr);
1116 void *state;
1117
1118 state = XXH32_init(1);
1119 XXH32_update(state, p, len);
1120 vh->hash = XXH32_digest(state);
1121}
1122
ae3a5acc
JA
1123static void fill_sha3(struct fio_sha3_ctx *sha3_ctx, void *p, unsigned int len)
1124{
1125 fio_sha3_update(sha3_ctx, p, len);
1126 fio_sha3_final(sha3_ctx);
1127}
1128
1129static void fill_sha3_224(struct verify_header *hdr, void *p, unsigned int len)
1130{
1131 struct vhdr_sha3_224 *vh = hdr_priv(hdr);
1132 struct fio_sha3_ctx sha3_ctx = {
1133 .sha = vh->sha,
1134 };
1135
1136 fio_sha3_224_init(&sha3_ctx);
1137 fill_sha3(&sha3_ctx, p, len);
1138}
1139
1140static void fill_sha3_256(struct verify_header *hdr, void *p, unsigned int len)
1141{
1142 struct vhdr_sha3_256 *vh = hdr_priv(hdr);
1143 struct fio_sha3_ctx sha3_ctx = {
1144 .sha = vh->sha,
1145 };
1146
1147 fio_sha3_256_init(&sha3_ctx);
1148 fill_sha3(&sha3_ctx, p, len);
1149}
1150
1151static void fill_sha3_384(struct verify_header *hdr, void *p, unsigned int len)
1152{
1153 struct vhdr_sha3_384 *vh = hdr_priv(hdr);
1154 struct fio_sha3_ctx sha3_ctx = {
1155 .sha = vh->sha,
1156 };
1157
1158 fio_sha3_384_init(&sha3_ctx);
1159 fill_sha3(&sha3_ctx, p, len);
1160}
1161
1162static void fill_sha3_512(struct verify_header *hdr, void *p, unsigned int len)
1163{
1164 struct vhdr_sha3_512 *vh = hdr_priv(hdr);
1165 struct fio_sha3_ctx sha3_ctx = {
1166 .sha = vh->sha,
1167 };
1168
1169 fio_sha3_512_init(&sha3_ctx);
1170 fill_sha3(&sha3_ctx, p, len);
1171}
1172
cd14cc10
JA
1173static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
1174{
546dfd9f 1175 struct vhdr_sha512 *vh = hdr_priv(hdr);
25dfa848 1176 struct fio_sha512_ctx sha512_ctx = {
546dfd9f 1177 .buf = vh->sha512,
cd14cc10
JA
1178 };
1179
25dfa848
JA
1180 fio_sha512_init(&sha512_ctx);
1181 fio_sha512_update(&sha512_ctx, p, len);
3ee53198 1182 fio_sha512_final(&sha512_ctx);
cd14cc10
JA
1183}
1184
1185static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
1186{
546dfd9f 1187 struct vhdr_sha256 *vh = hdr_priv(hdr);
25dfa848 1188 struct fio_sha256_ctx sha256_ctx = {
546dfd9f 1189 .buf = vh->sha256,
cd14cc10
JA
1190 };
1191
25dfa848
JA
1192 fio_sha256_init(&sha256_ctx);
1193 fio_sha256_update(&sha256_ctx, p, len);
017052b6 1194 fio_sha256_final(&sha256_ctx);
cd14cc10
JA
1195}
1196
7c353ceb
JA
1197static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
1198{
1199 struct vhdr_sha1 *vh = hdr_priv(hdr);
25dfa848 1200 struct fio_sha1_ctx sha1_ctx = {
7c353ceb
JA
1201 .H = vh->sha1,
1202 };
1203
25dfa848
JA
1204 fio_sha1_init(&sha1_ctx);
1205 fio_sha1_update(&sha1_ctx, p, len);
017052b6 1206 fio_sha1_final(&sha1_ctx);
7c353ceb
JA
1207}
1208
1e154bdb
JA
1209static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
1210{
546dfd9f
JA
1211 struct vhdr_crc7 *vh = hdr_priv(hdr);
1212
25dfa848 1213 vh->crc7 = fio_crc7(p, len);
1e154bdb
JA
1214}
1215
969f7ed3
JA
1216static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
1217{
546dfd9f
JA
1218 struct vhdr_crc16 *vh = hdr_priv(hdr);
1219
25dfa848 1220 vh->crc16 = fio_crc16(p, len);
969f7ed3
JA
1221}
1222
e29d1b70
JA
1223static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
1224{
546dfd9f
JA
1225 struct vhdr_crc32 *vh = hdr_priv(hdr);
1226
25dfa848 1227 vh->crc32 = fio_crc32(p, len);
e29d1b70
JA
1228}
1229
bac39e0e
JA
1230static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
1231{
1232 struct vhdr_crc32 *vh = hdr_priv(hdr);
1233
25dfa848 1234 vh->crc32 = fio_crc32c(p, len);
bac39e0e
JA
1235}
1236
d77a7af3
JA
1237static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
1238{
546dfd9f
JA
1239 struct vhdr_crc64 *vh = hdr_priv(hdr);
1240
25dfa848 1241 vh->crc64 = fio_crc64(p, len);
d77a7af3
JA
1242}
1243
e29d1b70
JA
1244static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
1245{
546dfd9f 1246 struct vhdr_md5 *vh = hdr_priv(hdr);
25dfa848 1247 struct fio_md5_ctx md5_ctx = {
546dfd9f 1248 .hash = (uint32_t *) vh->md5_digest,
8c432325 1249 };
e29d1b70 1250
25dfa848
JA
1251 fio_md5_init(&md5_ctx);
1252 fio_md5_update(&md5_ctx, p, len);
017052b6 1253 fio_md5_final(&md5_ctx);
e29d1b70
JA
1254}
1255
b638d82f
RP
1256static void __fill_hdr(struct thread_data *td, struct io_u *io_u,
1257 struct verify_header *hdr, unsigned int header_num,
1258 unsigned int header_len, uint64_t rand_seed)
9ba987cc
JA
1259{
1260 void *p = hdr;
1261
1262 hdr->magic = FIO_HDR_MAGIC;
b638d82f
RP
1263 hdr->verify_type = td->o.verify;
1264 hdr->len = header_len;
9ba987cc 1265 hdr->rand_seed = rand_seed;
4fff54cc 1266 hdr->offset = io_u->verify_offset + header_num * td->o.verify_interval;
b638d82f 1267 hdr->time_sec = io_u->start_time.tv_sec;
faa9b2b2 1268 hdr->time_nsec = io_u->start_time.tv_nsec;
b638d82f
RP
1269 hdr->thread = td->thread_number;
1270 hdr->numberio = io_u->numberio;
9ba987cc
JA
1271 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
1272}
1273
ed3a433d 1274
b638d82f
RP
1275static void fill_hdr(struct thread_data *td, struct io_u *io_u,
1276 struct verify_header *hdr, unsigned int header_num,
1277 unsigned int header_len, uint64_t rand_seed)
ed3a433d 1278{
b638d82f
RP
1279 if (td->o.verify != VERIFY_PATTERN_NO_HDR)
1280 __fill_hdr(td, io_u, hdr, header_num, header_len, rand_seed);
ed3a433d
JA
1281}
1282
7d9fb455
JA
1283static void populate_hdr(struct thread_data *td, struct io_u *io_u,
1284 struct verify_header *hdr, unsigned int header_num,
1285 unsigned int header_len)
1286{
1287 unsigned int data_len;
d637b8b8
TK
1288 void *data;
1289 char *p;
7d9fb455 1290
d637b8b8 1291 p = (char *) hdr;
7d9fb455 1292
b638d82f 1293 fill_hdr(td, io_u, hdr, header_num, header_len, io_u->rand_seed);
f65d1c26 1294
3982ec03
JA
1295 if (header_len <= hdr_size(td, hdr)) {
1296 td_verror(td, EINVAL, "Blocksize too small");
1297 return;
1298 }
ed3a433d 1299 data_len = header_len - hdr_size(td, hdr);
7d9fb455 1300
ed3a433d 1301 data = p + hdr_size(td, hdr);
7d9fb455
JA
1302 switch (td->o.verify) {
1303 case VERIFY_MD5:
1304 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1305 io_u, hdr->len);
1306 fill_md5(hdr, data, data_len);
1307 break;
1308 case VERIFY_CRC64:
1309 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1310 io_u, hdr->len);
1311 fill_crc64(hdr, data, data_len);
1312 break;
1313 case VERIFY_CRC32C:
1314 case VERIFY_CRC32C_INTEL:
1315 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1316 io_u, hdr->len);
1317 fill_crc32c(hdr, data, data_len);
1318 break;
1319 case VERIFY_CRC32:
1320 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1321 io_u, hdr->len);
1322 fill_crc32(hdr, data, data_len);
1323 break;
1324 case VERIFY_CRC16:
1325 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1326 io_u, hdr->len);
1327 fill_crc16(hdr, data, data_len);
1328 break;
1329 case VERIFY_CRC7:
1330 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1331 io_u, hdr->len);
1332 fill_crc7(hdr, data, data_len);
1333 break;
1334 case VERIFY_SHA256:
1335 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1336 io_u, hdr->len);
1337 fill_sha256(hdr, data, data_len);
1338 break;
1339 case VERIFY_SHA512:
1340 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1341 io_u, hdr->len);
1342 fill_sha512(hdr, data, data_len);
1343 break;
ae3a5acc
JA
1344 case VERIFY_SHA3_224:
1345 dprint(FD_VERIFY, "fill sha3-224 io_u %p, len %u\n",
1346 io_u, hdr->len);
1347 fill_sha3_224(hdr, data, data_len);
1348 break;
1349 case VERIFY_SHA3_256:
1350 dprint(FD_VERIFY, "fill sha3-256 io_u %p, len %u\n",
1351 io_u, hdr->len);
1352 fill_sha3_256(hdr, data, data_len);
1353 break;
1354 case VERIFY_SHA3_384:
1355 dprint(FD_VERIFY, "fill sha3-384 io_u %p, len %u\n",
1356 io_u, hdr->len);
1357 fill_sha3_384(hdr, data, data_len);
1358 break;
1359 case VERIFY_SHA3_512:
1360 dprint(FD_VERIFY, "fill sha3-512 io_u %p, len %u\n",
1361 io_u, hdr->len);
1362 fill_sha3_512(hdr, data, data_len);
1363 break;
844ea602
JA
1364 case VERIFY_XXHASH:
1365 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1366 io_u, hdr->len);
1367 fill_xxhash(hdr, data, data_len);
1368 break;
7d9fb455
JA
1369 case VERIFY_SHA1:
1370 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1371 io_u, hdr->len);
1372 fill_sha1(hdr, data, data_len);
1373 break;
b638d82f 1374 case VERIFY_HDR_ONLY:
92bf48d5 1375 case VERIFY_PATTERN:
ed3a433d 1376 case VERIFY_PATTERN_NO_HDR:
92bf48d5
JA
1377 /* nothing to do here */
1378 break;
7d9fb455
JA
1379 default:
1380 log_err("fio: bad verify type: %d\n", td->o.verify);
1381 assert(0);
1382 }
ed3a433d
JA
1383
1384 if (td->o.verify_offset && hdr_size(td, hdr))
1385 memswp(p, p + td->o.verify_offset, hdr_size(td, hdr));
7d9fb455
JA
1386}
1387
e29d1b70
JA
1388/*
1389 * fill body of io_u->buf with random data and add a header with the
c50ca7bd 1390 * checksum of choice
e29d1b70
JA
1391 */
1392void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1393{
9cc3d150
JA
1394 if (td->o.verify == VERIFY_NULL)
1395 return;
1396
c50ca7bd 1397 fill_pattern_headers(td, io_u, 0, 0);
e29d1b70
JA
1398}
1399
1400int get_next_verify(struct thread_data *td, struct io_u *io_u)
1401{
8de8f047 1402 struct io_piece *ipo = NULL;
e29d1b70 1403
d2d7fa53
JA
1404 /*
1405 * this io_u is from a requeue, we already filled the offsets
1406 */
1407 if (io_u->file)
1408 return 0;
1409
8de8f047 1410 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
e96c0125 1411 struct fio_rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 1412
8de8f047 1413 ipo = rb_entry(n, struct io_piece, rb_node);
f9401285
JA
1414
1415 /*
1416 * Ensure that the associated IO has completed
1417 */
0337173e 1418 if (atomic_load_acquire(&ipo->flags) & IP_F_IN_FLIGHT)
f9401285
JA
1419 goto nothing;
1420
4b87898e 1421 rb_erase(n, &td->io_hist_tree);
a917a8b3
JA
1422 assert(ipo->flags & IP_F_ONRB);
1423 ipo->flags &= ~IP_F_ONRB;
01743ee1 1424 } else if (!flist_empty(&td->io_hist_list)) {
9342d5f8 1425 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
f9401285
JA
1426
1427 /*
1428 * Ensure that the associated IO has completed
1429 */
0337173e 1430 if (atomic_load_acquire(&ipo->flags) & IP_F_IN_FLIGHT)
f9401285
JA
1431 goto nothing;
1432
01743ee1 1433 flist_del(&ipo->list);
a917a8b3
JA
1434 assert(ipo->flags & IP_F_ONLIST);
1435 ipo->flags &= ~IP_F_ONLIST;
8de8f047 1436 }
e29d1b70 1437
8de8f047 1438 if (ipo) {
0d29de83
JA
1439 td->io_hist_len--;
1440
e29d1b70 1441 io_u->offset = ipo->offset;
4fff54cc 1442 io_u->verify_offset = ipo->offset;
e29d1b70 1443 io_u->buflen = ipo->len;
da0a7bd2 1444 io_u->numberio = ipo->numberio;
36167d82 1445 io_u->file = ipo->file;
1651e431 1446 io_u_set(td, io_u, IO_U_F_VER_LIST);
97af62ce 1447
0d29de83 1448 if (ipo->flags & IP_F_TRIMMED)
1651e431 1449 io_u_set(td, io_u, IO_U_F_TRIMMED);
0d29de83 1450
d6aed795 1451 if (!fio_file_open(io_u->file)) {
97af62ce
JA
1452 int r = td_io_open_file(td, io_u->file);
1453
bd6f78b2
JA
1454 if (r) {
1455 dprint(FD_VERIFY, "failed file %s open\n",
1456 io_u->file->file_name);
97af62ce 1457 return 1;
bd6f78b2 1458 }
97af62ce
JA
1459 }
1460
1461 get_file(ipo->file);
d6aed795 1462 assert(fio_file_open(io_u->file));
e29d1b70 1463 io_u->ddir = DDIR_READ;
36167d82
JA
1464 io_u->xfer_buf = io_u->buf;
1465 io_u->xfer_buflen = io_u->buflen;
0d29de83
JA
1466
1467 remove_trim_entry(td, ipo);
e29d1b70 1468 free(ipo);
bd6f78b2 1469 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
c4b6117b
PV
1470
1471 if (!td->o.verify_pattern_bytes) {
d6b72507 1472 io_u->rand_seed = __rand(&td->verify_state);
c4b6117b 1473 if (sizeof(int) != sizeof(long *))
d6b72507 1474 io_u->rand_seed *= __rand(&td->verify_state);
c4b6117b 1475 }
e29d1b70
JA
1476 return 0;
1477 }
1478
f9401285 1479nothing:
bd6f78b2 1480 dprint(FD_VERIFY, "get_next_verify: empty\n");
e29d1b70
JA
1481 return 1;
1482}
e8462bd8 1483
dc5bfbb2
JA
1484void fio_verify_init(struct thread_data *td)
1485{
1486 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1487 td->o.verify == VERIFY_CRC32C) {
214e2d56 1488 crc32c_arm64_probe();
dc5bfbb2
JA
1489 crc32c_intel_probe();
1490 }
1491}
1492
e8462bd8
JA
1493static void *verify_async_thread(void *data)
1494{
1495 struct thread_data *td = data;
1496 struct io_u *io_u;
1497 int ret = 0;
1498
b2a9e649 1499 if (fio_option_is_set(&td->o, verify_cpumask) &&
e8462bd8
JA
1500 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1501 log_err("fio: failed setting verify thread affinity\n");
1502 goto done;
1503 }
1504
1505 do {
e53ab27c
JA
1506 FLIST_HEAD(list);
1507
e8462bd8
JA
1508 read_barrier();
1509 if (td->verify_thread_exit)
1510 break;
1511
1512 pthread_mutex_lock(&td->io_u_lock);
1513
1514 while (flist_empty(&td->verify_list) &&
1515 !td->verify_thread_exit) {
b36e298b
JA
1516 ret = pthread_cond_wait(&td->verify_cond,
1517 &td->io_u_lock);
e8462bd8 1518 if (ret) {
e8462bd8
JA
1519 break;
1520 }
1521 }
1522
e53ab27c
JA
1523 flist_splice_init(&td->verify_list, &list);
1524 pthread_mutex_unlock(&td->io_u_lock);
1525
1526 if (flist_empty(&list))
e8462bd8 1527 continue;
e8462bd8 1528
e53ab27c 1529 while (!flist_empty(&list)) {
9342d5f8 1530 io_u = flist_first_entry(&list, struct io_u, verify_list);
f8b0bd10
JA
1531 flist_del_init(&io_u->verify_list);
1532
1651e431 1533 io_u_set(td, io_u, IO_U_F_NO_FILE_PUT);
f8b0bd10 1534 ret = verify_io_u(td, &io_u);
e8462bd8 1535
e53ab27c 1536 put_io_u(td, io_u);
d561f2ab
JA
1537 if (!ret)
1538 continue;
8b28bd41 1539 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
d561f2ab
JA
1540 update_error_count(td, ret);
1541 td_clear_error(td);
1542 ret = 0;
1543 }
e53ab27c 1544 }
e8462bd8
JA
1545 } while (!ret);
1546
d561f2ab
JA
1547 if (ret) {
1548 td_verror(td, ret, "async_verify");
f3e6cb95 1549 if (td->o.verify_fatal)
ebea2133 1550 fio_mark_td_terminate(td);
d561f2ab
JA
1551 }
1552
e8462bd8
JA
1553done:
1554 pthread_mutex_lock(&td->io_u_lock);
1555 td->nr_verify_threads--;
564de8d1 1556 pthread_cond_signal(&td->free_cond);
e8462bd8
JA
1557 pthread_mutex_unlock(&td->io_u_lock);
1558
e8462bd8
JA
1559 return NULL;
1560}
1561
1562int verify_async_init(struct thread_data *td)
1563{
1564 int i, ret;
304a47c7
VA
1565 pthread_attr_t attr;
1566
1567 pthread_attr_init(&attr);
45213f1b 1568 pthread_attr_setstacksize(&attr, 2 * PTHREAD_STACK_MIN);
e8462bd8
JA
1569
1570 td->verify_thread_exit = 0;
1571
1572 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1573 for (i = 0; i < td->o.verify_async; i++) {
304a47c7 1574 ret = pthread_create(&td->verify_threads[i], &attr,
e8462bd8
JA
1575 verify_async_thread, td);
1576 if (ret) {
1577 log_err("fio: async verify creation failed: %s\n",
1578 strerror(ret));
1579 break;
1580 }
1581 ret = pthread_detach(td->verify_threads[i]);
1582 if (ret) {
1583 log_err("fio: async verify thread detach failed: %s\n",
1584 strerror(ret));
1585 break;
1586 }
1587 td->nr_verify_threads++;
1588 }
1589
304a47c7
VA
1590 pthread_attr_destroy(&attr);
1591
e8462bd8 1592 if (i != td->o.verify_async) {
e40823b1 1593 log_err("fio: only %d verify threads started, exiting\n", i);
564de8d1
BVA
1594
1595 pthread_mutex_lock(&td->io_u_lock);
e8462bd8 1596 td->verify_thread_exit = 1;
e8462bd8 1597 pthread_cond_broadcast(&td->verify_cond);
564de8d1
BVA
1598 pthread_mutex_unlock(&td->io_u_lock);
1599
e8462bd8
JA
1600 return 1;
1601 }
1602
1603 return 0;
1604}
1605
1606void verify_async_exit(struct thread_data *td)
1607{
564de8d1 1608 pthread_mutex_lock(&td->io_u_lock);
e8462bd8 1609 td->verify_thread_exit = 1;
e8462bd8
JA
1610 pthread_cond_broadcast(&td->verify_cond);
1611
e8462bd8
JA
1612 while (td->nr_verify_threads)
1613 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1614
1615 pthread_mutex_unlock(&td->io_u_lock);
1616 free(td->verify_threads);
1617 td->verify_threads = NULL;
1618}
ca09be4b 1619
61b9861d
RP
1620int paste_blockoff(char *buf, unsigned int len, void *priv)
1621{
1622 struct io_u *io = priv;
1623 unsigned long long off;
1624
3376ecf4 1625 typecheck(__typeof__(off), io->offset);
61b9861d
RP
1626 off = cpu_to_le64((uint64_t)io->offset);
1627 len = min(len, (unsigned int)sizeof(off));
1628 memcpy(buf, &off, len);
1629 return 0;
1630}
1631
94a6e1bb
JA
1632static int __fill_file_completions(struct thread_data *td,
1633 struct thread_io_list *s,
1634 struct fio_file *f, unsigned int *index)
1635{
1636 unsigned int comps;
1637 int i, j;
1638
1639 if (!f->last_write_comp)
1640 return 0;
1641
9abce429 1642 if (td->io_blocks[DDIR_WRITE] < td->last_write_comp_depth)
94a6e1bb
JA
1643 comps = td->io_blocks[DDIR_WRITE];
1644 else
9abce429 1645 comps = td->last_write_comp_depth;
94a6e1bb
JA
1646
1647 j = f->last_write_idx - 1;
1648 for (i = 0; i < comps; i++) {
1649 if (j == -1)
9abce429 1650 j = td->last_write_comp_depth - 1;
94a6e1bb
JA
1651 s->comps[*index].fileno = __cpu_to_le64(f->fileno);
1652 s->comps[*index].offset = cpu_to_le64(f->last_write_comp[j]);
1653 (*index)++;
1654 j--;
1655 }
1656
1657 return comps;
1658}
1659
1660static int fill_file_completions(struct thread_data *td,
1661 struct thread_io_list *s, unsigned int *index)
1662{
1663 struct fio_file *f;
1664 unsigned int i;
1665 int comps = 0;
1666
1667 for_each_file(td, f, i)
1668 comps += __fill_file_completions(td, s, f, index);
1669
1670 return comps;
1671}
1672
ca09be4b
JA
1673struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1674{
1675 struct all_io_list *rep;
ca09be4b
JA
1676 size_t depth;
1677 void *next;
da8f124f 1678 int nr;
ca09be4b
JA
1679
1680 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1681
1682 /*
1683 * Calculate reply space needed. We need one 'io_state' per thread,
1684 * and the size will vary depending on depth.
1685 */
1686 depth = 0;
1687 nr = 0;
da8f124f
H
1688 for_each_td(td) {
1689 if (save_mask != IO_LIST_ALL && (__td_index + 1) != save_mask)
ca09be4b
JA
1690 continue;
1691 td->stop_io = 1;
1692 td->flags |= TD_F_VSTATE_SAVED;
9abce429 1693 depth += (td->last_write_comp_depth * td->o.nr_files);
ca09be4b 1694 nr++;
da8f124f 1695 } end_for_each();
ca09be4b
JA
1696
1697 if (!nr)
1698 return NULL;
1699
1700 *sz = sizeof(*rep);
1701 *sz += nr * sizeof(struct thread_io_list);
94a6e1bb 1702 *sz += depth * sizeof(struct file_comp);
223decdd 1703 rep = calloc(1, *sz);
ca09be4b
JA
1704
1705 rep->threads = cpu_to_le64((uint64_t) nr);
1706
1707 next = &rep->state[0];
da8f124f 1708 for_each_td(td) {
ca09be4b 1709 struct thread_io_list *s = next;
94a6e1bb 1710 unsigned int comps, index = 0;
ca09be4b 1711
da8f124f 1712 if (save_mask != IO_LIST_ALL && (__td_index + 1) != save_mask)
ca09be4b
JA
1713 continue;
1714
94a6e1bb 1715 comps = fill_file_completions(td, s, &index);
ca09be4b
JA
1716
1717 s->no_comps = cpu_to_le64((uint64_t) comps);
38d01cc5 1718 s->depth = cpu_to_le32((uint32_t) td->o.iodepth);
9abce429 1719 s->max_no_comps_per_file = cpu_to_le32((uint32_t) td->last_write_comp_depth);
38d01cc5 1720 s->nofiles = cpu_to_le32((uint32_t) td->o.nr_files);
ca09be4b 1721 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
da8f124f 1722 s->index = cpu_to_le64((uint64_t) __td_index);
c3546b53
JA
1723 if (td->random_state.use64) {
1724 s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
1725 s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
1726 s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
1727 s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
1728 s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
1729 s->rand.state64.s[5] = 0;
1730 s->rand.use64 = cpu_to_le64((uint64_t)1);
1731 } else {
1732 s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
1733 s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
1734 s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
1735 s->rand.state32.s[3] = 0;
1736 s->rand.use64 = 0;
1737 }
36833fb0 1738 snprintf((char *) s->name, sizeof(s->name), "%s", td->o.name);
ca09be4b 1739 next = io_list_next(s);
da8f124f 1740 } end_for_each();
ca09be4b
JA
1741
1742 return rep;
1743}
1744
1745static int open_state_file(const char *name, const char *prefix, int num,
1746 int for_write)
1747{
d117b2bd 1748 char out[PATH_MAX];
ca09be4b
JA
1749 int flags;
1750 int fd;
1751
1752 if (for_write)
1753 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1754 else
1755 flags = O_RDONLY;
1756
e2c5f17e
VF
1757#ifdef _WIN32
1758 flags |= O_BINARY;
1759#endif
1760
e499aedc 1761 verify_state_gen_name(out, sizeof(out), name, prefix, num);
ca09be4b
JA
1762
1763 fd = open(out, flags, 0644);
1764 if (fd == -1) {
1765 perror("fio: open state file");
d117b2bd 1766 log_err("fio: state file: %s (for_write=%d)\n", out, for_write);
ca09be4b
JA
1767 return -1;
1768 }
1769
1770 return fd;
1771}
1772
1773static int write_thread_list_state(struct thread_io_list *s,
1774 const char *prefix)
1775{
1776 struct verify_state_hdr hdr;
1777 uint64_t crc;
1778 ssize_t ret;
1779 int fd;
1780
1781 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1782 if (fd == -1)
1783 return 1;
1784
1785 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1786
1787 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1788 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1789 hdr.crc = cpu_to_le64(crc);
1790 ret = write(fd, &hdr, sizeof(hdr));
1791 if (ret != sizeof(hdr))
1792 goto write_fail;
1793
1794 ret = write(fd, s, thread_io_list_sz(s));
1795 if (ret != thread_io_list_sz(s)) {
1796write_fail:
1797 if (ret < 0)
1798 perror("fio: write state file");
1799 log_err("fio: failed to write state file\n");
1800 ret = 1;
1801 } else
1802 ret = 0;
1803
1804 close(fd);
1805 return ret;
1806}
1807
1808void __verify_save_state(struct all_io_list *state, const char *prefix)
1809{
1810 struct thread_io_list *s = &state->state[0];
1811 unsigned int i;
1812
1813 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1814 write_thread_list_state(s, prefix);
1815 s = io_list_next(s);
1816 }
1817}
1818
d264264a 1819void verify_save_state(int mask)
ca09be4b
JA
1820{
1821 struct all_io_list *state;
1822 size_t sz;
1823
d264264a 1824 state = get_all_io_list(mask, &sz);
ca09be4b 1825 if (state) {
d264264a
JA
1826 char prefix[PATH_MAX];
1827
1828 if (aux_path)
53a7af85 1829 sprintf(prefix, "%s%clocal", aux_path, FIO_OS_PATH_SEPARATOR);
d264264a 1830 else
c6d140fb 1831 strcpy(prefix, "local");
d264264a
JA
1832
1833 __verify_save_state(state, prefix);
ca09be4b
JA
1834 free(state);
1835 }
1836}
1837
1838void verify_free_state(struct thread_data *td)
1839{
1840 if (td->vstate)
1841 free(td->vstate);
1842}
1843
94a6e1bb 1844void verify_assign_state(struct thread_data *td, void *p)
ca09be4b 1845{
94a6e1bb 1846 struct thread_io_list *s = p;
ca09be4b
JA
1847 int i;
1848
94a6e1bb
JA
1849 s->no_comps = le64_to_cpu(s->no_comps);
1850 s->depth = le32_to_cpu(s->depth);
9abce429 1851 s->max_no_comps_per_file = le32_to_cpu(s->max_no_comps_per_file);
94a6e1bb
JA
1852 s->nofiles = le32_to_cpu(s->nofiles);
1853 s->numberio = le64_to_cpu(s->numberio);
1854 s->rand.use64 = le64_to_cpu(s->rand.use64);
c3546b53 1855
94a6e1bb
JA
1856 if (s->rand.use64) {
1857 for (i = 0; i < 6; i++)
1858 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
c3546b53 1859 } else {
94a6e1bb
JA
1860 for (i = 0; i < 4; i++)
1861 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1862 }
c3546b53 1863
94a6e1bb
JA
1864 for (i = 0; i < s->no_comps; i++) {
1865 s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno);
1866 s->comps[i].offset = le64_to_cpu(s->comps[i].offset);
c3546b53 1867 }
ca09be4b 1868
94a6e1bb 1869 td->vstate = p;
ca09be4b
JA
1870}
1871
94a6e1bb 1872int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s)
ca09be4b
JA
1873{
1874 uint64_t crc;
1875
1876 hdr->version = le64_to_cpu(hdr->version);
1877 hdr->size = le64_to_cpu(hdr->size);
1878 hdr->crc = le64_to_cpu(hdr->crc);
1879
94a6e1bb 1880 if (hdr->version != VSTATE_HDR_VERSION)
ca09be4b
JA
1881 return 1;
1882
1883 crc = fio_crc32c((void *)s, hdr->size);
1884 if (crc != hdr->crc)
1885 return 1;
1886
1887 return 0;
1888}
1889
1890int verify_load_state(struct thread_data *td, const char *prefix)
1891{
ca09be4b 1892 struct verify_state_hdr hdr;
c3546b53 1893 void *s = NULL;
ca09be4b
JA
1894 uint64_t crc;
1895 ssize_t ret;
1896 int fd;
1897
1898 if (!td->o.verify_state)
1899 return 0;
1900
1901 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1902 if (fd == -1)
1903 return 1;
1904
1905 ret = read(fd, &hdr, sizeof(hdr));
1906 if (ret != sizeof(hdr)) {
1907 if (ret < 0)
1908 td_verror(td, errno, "read verify state hdr");
1909 log_err("fio: failed reading verify state header\n");
1910 goto err;
1911 }
1912
1913 hdr.version = le64_to_cpu(hdr.version);
1914 hdr.size = le64_to_cpu(hdr.size);
1915 hdr.crc = le64_to_cpu(hdr.crc);
1916
94a6e1bb
JA
1917 if (hdr.version != VSTATE_HDR_VERSION) {
1918 log_err("fio: unsupported (%d) version in verify state header\n",
1919 (unsigned int) hdr.version);
ca09be4b
JA
1920 goto err;
1921 }
1922
1923 s = malloc(hdr.size);
1924 ret = read(fd, s, hdr.size);
1925 if (ret != hdr.size) {
1926 if (ret < 0)
1927 td_verror(td, errno, "read verify state");
1928 log_err("fio: failed reading verity state\n");
1929 goto err;
1930 }
1931
c3546b53 1932 crc = fio_crc32c(s, hdr.size);
ca09be4b
JA
1933 if (crc != hdr.crc) {
1934 log_err("fio: verify state is corrupt\n");
1935 goto err;
1936 }
1937
1938 close(fd);
1939
94a6e1bb 1940 verify_assign_state(td, s);
ca09be4b
JA
1941 return 0;
1942err:
1943 if (s)
1944 free(s);
1945 close(fd);
1946 return 1;
1947}
1948
1949/*
1950 * Use the loaded verify state to know when to stop doing verification
1951 */
1952int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1953{
1954 struct thread_io_list *s = td->vstate;
94a6e1bb 1955 struct fio_file *f = io_u->file;
ca09be4b
JA
1956 int i;
1957
94a6e1bb 1958 if (!s || !f)
ca09be4b
JA
1959 return 0;
1960
1961 /*
def00095
JA
1962 * If we're not into the window of issues - depth yet, continue. If
1963 * issue is shorter than depth, do check.
ca09be4b 1964 */
def00095
JA
1965 if ((td->io_blocks[DDIR_READ] < s->depth ||
1966 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1967 s->numberio > s->depth)
ca09be4b
JA
1968 return 0;
1969
1970 /*
1971 * We're in the window of having to check if this io was
1972 * completed or not. If the IO was seen as completed, then
1973 * lets verify it.
1974 */
94a6e1bb
JA
1975 for (i = 0; i < s->no_comps; i++) {
1976 if (s->comps[i].fileno != f->fileno)
1977 continue;
4fff54cc 1978 if (io_u->verify_offset == s->comps[i].offset)
ca09be4b 1979 return 0;
94a6e1bb 1980 }
ca09be4b
JA
1981
1982 /*
1983 * Not found, we have to stop
1984 */
ceb624e8
SK
1985 log_info("Stop verify because offset %llu in %s is not recorded in verify state\n",
1986 io_u->verify_offset, f->file_name);
ca09be4b
JA
1987 return 1;
1988}