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