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