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