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