Don't clear 'refill_buffers' unconditionally for pattern fill
[fio.git] / verify.c
1 /*
2  * IO verification helpers
3  */
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <pthread.h>
9 #include <libgen.h>
10
11 #include "fio.h"
12 #include "verify.h"
13 #include "trim.h"
14 #include "lib/rand.h"
15 #include "lib/hweight.h"
16
17 #include "crc/md5.h"
18 #include "crc/crc64.h"
19 #include "crc/crc32.h"
20 #include "crc/crc32c.h"
21 #include "crc/crc16.h"
22 #include "crc/crc7.h"
23 #include "crc/sha256.h"
24 #include "crc/sha512.h"
25 #include "crc/sha1.h"
26 #include "crc/xxhash.h"
27
28 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
29                          struct verify_header *hdr, unsigned int header_num,
30                          unsigned int header_len);
31
32 void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
33 {
34         fill_pattern(p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
35 }
36
37 void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
38                          struct io_u *io_u, unsigned long seed, int use_seed)
39 {
40         if (!td->o.verify_pattern_bytes) {
41                 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
42
43                 if (use_seed)
44                         __fill_random_buf(p, len, seed);
45                 else
46                         io_u->rand_seed = fill_random_buf(&td->verify_state, p, len);
47                 return;
48         }
49
50         if (io_u->buf_filled_len >= len) {
51                 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
52                         td->o.verify_pattern_bytes, len);
53                 return;
54         }
55
56         fill_pattern(p, len, td->o.verify_pattern, td->o.verify_pattern_bytes);
57
58         io_u->buf_filled_len = len;
59 }
60
61 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
62 {
63         unsigned int hdr_inc;
64
65         hdr_inc = io_u->buflen;
66         if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
67                 hdr_inc = td->o.verify_interval;
68
69         return hdr_inc;
70 }
71
72 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
73                                  unsigned long seed, int use_seed)
74 {
75         unsigned int hdr_inc, header_num;
76         struct verify_header *hdr;
77         void *p = io_u->buf;
78
79         fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
80
81         hdr_inc = get_hdr_inc(td, io_u);
82         header_num = 0;
83         for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
84                 hdr = p;
85                 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
86                 header_num++;
87         }
88 }
89
90 static void memswp(void *buf1, void *buf2, unsigned int len)
91 {
92         char swap[200];
93
94         assert(len <= sizeof(swap));
95
96         memcpy(&swap, buf1, len);
97         memcpy(buf1, buf2, len);
98         memcpy(buf2, &swap, len);
99 }
100
101 static void hexdump(void *buffer, int len)
102 {
103         unsigned char *p = buffer;
104         int i;
105
106         for (i = 0; i < len; i++)
107                 log_err("%02x", p[i]);
108         log_err("\n");
109 }
110
111 /*
112  * Prepare for separation of verify_header and checksum header
113  */
114 static inline unsigned int __hdr_size(int verify_type)
115 {
116         unsigned int len = 0;
117
118         switch (verify_type) {
119         case VERIFY_NONE:
120         case VERIFY_NULL:
121                 len = 0;
122                 break;
123         case VERIFY_MD5:
124                 len = sizeof(struct vhdr_md5);
125                 break;
126         case VERIFY_CRC64:
127                 len = sizeof(struct vhdr_crc64);
128                 break;
129         case VERIFY_CRC32C:
130         case VERIFY_CRC32:
131         case VERIFY_CRC32C_INTEL:
132                 len = sizeof(struct vhdr_crc32);
133                 break;
134         case VERIFY_CRC16:
135                 len = sizeof(struct vhdr_crc16);
136                 break;
137         case VERIFY_CRC7:
138                 len = sizeof(struct vhdr_crc7);
139                 break;
140         case VERIFY_SHA256:
141                 len = sizeof(struct vhdr_sha256);
142                 break;
143         case VERIFY_SHA512:
144                 len = sizeof(struct vhdr_sha512);
145                 break;
146         case VERIFY_XXHASH:
147                 len = sizeof(struct vhdr_xxhash);
148                 break;
149         case VERIFY_META:
150                 len = sizeof(struct vhdr_meta);
151                 break;
152         case VERIFY_SHA1:
153                 len = sizeof(struct vhdr_sha1);
154                 break;
155         case VERIFY_PATTERN:
156                 len = 0;
157                 break;
158         default:
159                 log_err("fio: unknown verify header!\n");
160                 assert(0);
161         }
162
163         return len + sizeof(struct verify_header);
164 }
165
166 static inline unsigned int hdr_size(struct verify_header *hdr)
167 {
168         return __hdr_size(hdr->verify_type);
169 }
170
171 static void *hdr_priv(struct verify_header *hdr)
172 {
173         void *priv = hdr;
174
175         return priv + sizeof(struct verify_header);
176 }
177
178 /*
179  * Verify container, pass info to verify handlers and allow them to
180  * pass info back in case of error
181  */
182 struct vcont {
183         /*
184          * Input
185          */
186         struct io_u *io_u;
187         unsigned int hdr_num;
188         struct thread_data *td;
189
190         /*
191          * Output, only valid in case of error
192          */
193         const char *name;
194         void *good_crc;
195         void *bad_crc;
196         unsigned int crc_len;
197 };
198
199 #define DUMP_BUF_SZ     255
200 static int dump_buf_warned;
201
202 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
203                      const char *type, struct fio_file *f)
204 {
205         char *ptr, fname[DUMP_BUF_SZ];
206         size_t buf_left = DUMP_BUF_SZ;
207         int ret, fd;
208
209         ptr = strdup(f->file_name);
210
211         fname[DUMP_BUF_SZ - 1] = '\0';
212         strncpy(fname, basename(ptr), DUMP_BUF_SZ - 1);
213
214         buf_left -= strlen(fname);
215         if (buf_left <= 0) {
216                 if (!dump_buf_warned) {
217                         log_err("fio: verify failure dump buffer too small\n");
218                         dump_buf_warned = 1;
219                 }
220                 free(ptr);
221                 return;
222         }
223
224         snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
225
226         fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
227         if (fd < 0) {
228                 perror("open verify buf file");
229                 return;
230         }
231
232         while (len) {
233                 ret = write(fd, buf, len);
234                 if (!ret)
235                         break;
236                 else if (ret < 0) {
237                         perror("write verify buf file");
238                         break;
239                 }
240                 len -= ret;
241                 buf += ret;
242         }
243
244         close(fd);
245         log_err("       %s data dumped as %s\n", type, fname);
246         free(ptr);
247 }
248
249 /*
250  * Dump the contents of the read block and re-generate the correct data
251  * and dump that too.
252  */
253 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
254 {
255         struct thread_data *td = vc->td;
256         struct io_u *io_u = vc->io_u;
257         unsigned long hdr_offset;
258         struct io_u dummy;
259         void *buf;
260
261         if (!td->o.verify_dump)
262                 return;
263
264         /*
265          * Dump the contents we just read off disk
266          */
267         hdr_offset = vc->hdr_num * hdr->len;
268
269         dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
270                         "received", vc->io_u->file);
271
272         /*
273          * Allocate a new buf and re-generate the original data
274          */
275         buf = malloc(io_u->buflen);
276         dummy = *io_u;
277         dummy.buf = buf;
278         dummy.rand_seed = hdr->rand_seed;
279         dummy.buf_filled_len = 0;
280         dummy.buflen = io_u->buflen;
281
282         fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
283
284         dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
285                         "expected", vc->io_u->file);
286         free(buf);
287 }
288
289 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
290 {
291         unsigned long long offset;
292
293         offset = vc->io_u->offset;
294         offset += vc->hdr_num * hdr->len;
295         log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
296                         vc->name, vc->io_u->file->file_name, offset, hdr->len);
297
298         if (vc->good_crc && vc->bad_crc) {
299                 log_err("       Expected CRC: ");
300                 hexdump(vc->good_crc, vc->crc_len);
301                 log_err("       Received CRC: ");
302                 hexdump(vc->bad_crc, vc->crc_len);
303         }
304
305         dump_verify_buffers(hdr, vc);
306 }
307
308 /*
309  * Return data area 'header_num'
310  */
311 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
312 {
313         return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
314 }
315
316 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
317 {
318         struct thread_data *td = vc->td;
319         struct io_u *io_u = vc->io_u;
320         char *buf, *pattern;
321         unsigned int header_size = __hdr_size(td->o.verify);
322         unsigned int len, mod, i, size, pattern_size;
323
324         pattern = td->o.verify_pattern;
325         pattern_size = td->o.verify_pattern_bytes;
326         if (pattern_size <= 1)
327                 pattern_size = MAX_PATTERN_SIZE;
328         buf = (void *) hdr + header_size;
329         len = get_hdr_inc(td, io_u) - header_size;
330         mod = header_size % pattern_size;
331
332         for (i = 0; i < len; i += size) {
333                 size = pattern_size - mod;
334                 if (size > (len - i))
335                         size = len - i;
336                 if (memcmp(buf + i, pattern + mod, size))
337                         /* Let the slow compare find the first mismatch byte. */
338                         break;
339                 mod = 0;
340         }
341
342         for (; i < len; i++) {
343                 if (buf[i] != pattern[mod]) {
344                         unsigned int bits;
345
346                         bits = hweight8(buf[i] ^ pattern[mod]);
347                         log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
348                                 buf[i], pattern[mod], bits);
349                         log_err("fio: bad pattern block offset %u\n", i);
350                         dump_verify_buffers(hdr, vc);
351                         return EILSEQ;
352                 }
353                 mod++;
354                 if (mod == td->o.verify_pattern_bytes)
355                         mod = 0;
356         }
357
358         return 0;
359 }
360
361 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
362 {
363         struct thread_data *td = vc->td;
364         struct vhdr_meta *vh = hdr_priv(hdr);
365         struct io_u *io_u = vc->io_u;
366         int ret = EILSEQ;
367
368         dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
369
370         if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
371                 ret = 0;
372
373         if (td->o.verify_pattern_bytes)
374                 ret |= verify_io_u_pattern(hdr, vc);
375
376         /*
377          * For read-only workloads, the program cannot be certain of the
378          * last numberio written to a block. Checking of numberio will be
379          * done only for workloads that write data.  For verify_only,
380          * numberio will be checked in the last iteration when the correct
381          * state of numberio, that would have been written to each block
382          * in a previous run of fio, has been reached.
383          */
384         if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
385             !td->o.time_based)
386                 if (!td->o.verify_only || td->o.loops == 0)
387                         if (vh->numberio != io_u->numberio)
388                                 ret = EILSEQ;
389
390         if (!ret)
391                 return 0;
392
393         vc->name = "meta";
394         log_verify_failure(hdr, vc);
395         return ret;
396 }
397
398 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
399 {
400         void *p = io_u_verify_off(hdr, vc);
401         struct vhdr_xxhash *vh = hdr_priv(hdr);
402         uint32_t hash;
403         void *state;
404
405         dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
406
407         state = XXH32_init(1);
408         XXH32_update(state, p, hdr->len - hdr_size(hdr));
409         hash = XXH32_digest(state);
410
411         if (vh->hash == hash)
412                 return 0;
413
414         vc->name = "xxhash";
415         vc->good_crc = &vh->hash;
416         vc->bad_crc = &hash;
417         vc->crc_len = sizeof(hash);
418         log_verify_failure(hdr, vc);
419         return EILSEQ;
420 }
421
422 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
423 {
424         void *p = io_u_verify_off(hdr, vc);
425         struct vhdr_sha512 *vh = hdr_priv(hdr);
426         uint8_t sha512[128];
427         struct fio_sha512_ctx sha512_ctx = {
428                 .buf = sha512,
429         };
430
431         dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
432
433         fio_sha512_init(&sha512_ctx);
434         fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
435
436         if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
437                 return 0;
438
439         vc->name = "sha512";
440         vc->good_crc = vh->sha512;
441         vc->bad_crc = sha512_ctx.buf;
442         vc->crc_len = sizeof(vh->sha512);
443         log_verify_failure(hdr, vc);
444         return EILSEQ;
445 }
446
447 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
448 {
449         void *p = io_u_verify_off(hdr, vc);
450         struct vhdr_sha256 *vh = hdr_priv(hdr);
451         uint8_t sha256[64];
452         struct fio_sha256_ctx sha256_ctx = {
453                 .buf = sha256,
454         };
455
456         dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
457
458         fio_sha256_init(&sha256_ctx);
459         fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
460
461         if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
462                 return 0;
463
464         vc->name = "sha256";
465         vc->good_crc = vh->sha256;
466         vc->bad_crc = sha256_ctx.buf;
467         vc->crc_len = sizeof(vh->sha256);
468         log_verify_failure(hdr, vc);
469         return EILSEQ;
470 }
471
472 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
473 {
474         void *p = io_u_verify_off(hdr, vc);
475         struct vhdr_sha1 *vh = hdr_priv(hdr);
476         uint32_t sha1[5];
477         struct fio_sha1_ctx sha1_ctx = {
478                 .H = sha1,
479         };
480
481         dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
482
483         fio_sha1_init(&sha1_ctx);
484         fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
485
486         if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
487                 return 0;
488
489         vc->name = "sha1";
490         vc->good_crc = vh->sha1;
491         vc->bad_crc = sha1_ctx.H;
492         vc->crc_len = sizeof(vh->sha1);
493         log_verify_failure(hdr, vc);
494         return EILSEQ;
495 }
496
497 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
498 {
499         void *p = io_u_verify_off(hdr, vc);
500         struct vhdr_crc7 *vh = hdr_priv(hdr);
501         unsigned char c;
502
503         dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
504
505         c = fio_crc7(p, hdr->len - hdr_size(hdr));
506
507         if (c == vh->crc7)
508                 return 0;
509
510         vc->name = "crc7";
511         vc->good_crc = &vh->crc7;
512         vc->bad_crc = &c;
513         vc->crc_len = 1;
514         log_verify_failure(hdr, vc);
515         return EILSEQ;
516 }
517
518 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
519 {
520         void *p = io_u_verify_off(hdr, vc);
521         struct vhdr_crc16 *vh = hdr_priv(hdr);
522         unsigned short c;
523
524         dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
525
526         c = fio_crc16(p, hdr->len - hdr_size(hdr));
527
528         if (c == vh->crc16)
529                 return 0;
530
531         vc->name = "crc16";
532         vc->good_crc = &vh->crc16;
533         vc->bad_crc = &c;
534         vc->crc_len = 2;
535         log_verify_failure(hdr, vc);
536         return EILSEQ;
537 }
538
539 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
540 {
541         void *p = io_u_verify_off(hdr, vc);
542         struct vhdr_crc64 *vh = hdr_priv(hdr);
543         unsigned long long c;
544
545         dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
546
547         c = fio_crc64(p, hdr->len - hdr_size(hdr));
548
549         if (c == vh->crc64)
550                 return 0;
551
552         vc->name = "crc64";
553         vc->good_crc = &vh->crc64;
554         vc->bad_crc = &c;
555         vc->crc_len = 8;
556         log_verify_failure(hdr, vc);
557         return EILSEQ;
558 }
559
560 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
561 {
562         void *p = io_u_verify_off(hdr, vc);
563         struct vhdr_crc32 *vh = hdr_priv(hdr);
564         uint32_t c;
565
566         dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
567
568         c = fio_crc32(p, hdr->len - hdr_size(hdr));
569
570         if (c == vh->crc32)
571                 return 0;
572
573         vc->name = "crc32";
574         vc->good_crc = &vh->crc32;
575         vc->bad_crc = &c;
576         vc->crc_len = 4;
577         log_verify_failure(hdr, vc);
578         return EILSEQ;
579 }
580
581 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
582 {
583         void *p = io_u_verify_off(hdr, vc);
584         struct vhdr_crc32 *vh = hdr_priv(hdr);
585         uint32_t c;
586
587         dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
588
589         c = fio_crc32c(p, hdr->len - hdr_size(hdr));
590
591         if (c == vh->crc32)
592                 return 0;
593
594         vc->name = "crc32c";
595         vc->good_crc = &vh->crc32;
596         vc->bad_crc = &c;
597         vc->crc_len = 4;
598         log_verify_failure(hdr, vc);
599         return EILSEQ;
600 }
601
602 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
603 {
604         void *p = io_u_verify_off(hdr, vc);
605         struct vhdr_md5 *vh = hdr_priv(hdr);
606         uint32_t hash[MD5_HASH_WORDS];
607         struct fio_md5_ctx md5_ctx = {
608                 .hash = hash,
609         };
610
611         dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
612
613         fio_md5_init(&md5_ctx);
614         fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
615
616         if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
617                 return 0;
618
619         vc->name = "md5";
620         vc->good_crc = vh->md5_digest;
621         vc->bad_crc = md5_ctx.hash;
622         vc->crc_len = sizeof(hash);
623         log_verify_failure(hdr, vc);
624         return EILSEQ;
625 }
626
627 /*
628  * Push IO verification to a separate thread
629  */
630 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
631 {
632         struct io_u *io_u = *io_u_ptr;
633
634         pthread_mutex_lock(&td->io_u_lock);
635
636         if (io_u->file)
637                 put_file_log(td, io_u->file);
638
639         if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
640                 td->cur_depth--;
641                 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
642         }
643         flist_add_tail(&io_u->verify_list, &td->verify_list);
644         *io_u_ptr = NULL;
645         pthread_mutex_unlock(&td->io_u_lock);
646
647         pthread_cond_signal(&td->verify_cond);
648         return 0;
649 }
650
651 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
652 {
653         static char zero_buf[1024];
654         unsigned int this_len, len;
655         int ret = 0;
656         void *p;
657
658         if (!td->o.trim_zero)
659                 return 0;
660
661         len = io_u->buflen;
662         p = io_u->buf;
663         do {
664                 this_len = sizeof(zero_buf);
665                 if (this_len > len)
666                         this_len = len;
667                 if (memcmp(p, zero_buf, this_len)) {
668                         ret = EILSEQ;
669                         break;
670                 }
671                 len -= this_len;
672                 p += this_len;
673         } while (len);
674
675         if (!ret)
676                 return 0;
677
678         log_err("trim: verify failed at file %s offset %llu, length %lu"
679                 ", block offset %lu\n",
680                         io_u->file->file_name, io_u->offset, io_u->buflen,
681                         (unsigned long) (p - io_u->buf));
682         return ret;
683 }
684
685 static int verify_header(struct io_u *io_u, struct verify_header *hdr,
686                          unsigned int hdr_num, unsigned int hdr_len)
687 {
688         void *p = hdr;
689         uint32_t crc;
690
691         if (hdr->magic != FIO_HDR_MAGIC) {
692                 log_err("verify: bad magic header %x, wanted %x",
693                         hdr->magic, FIO_HDR_MAGIC);
694                 goto err;
695         }
696         if (hdr->len != hdr_len) {
697                 log_err("verify: bad header length %u, wanted %u",
698                         hdr->len, hdr_len);
699                 goto err;
700         }
701         if (hdr->rand_seed != io_u->rand_seed) {
702                 log_err("verify: bad header rand_seed %"PRIu64
703                         ", wanted %"PRIu64,
704                         hdr->rand_seed, io_u->rand_seed);
705                 goto err;
706         }
707
708         crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
709         if (crc != hdr->crc32) {
710                 log_err("verify: bad header crc %x, calculated %x",
711                         hdr->crc32, crc);
712                 goto err;
713         }
714         return 0;
715
716 err:
717         log_err(" at file %s offset %llu, length %u\n",
718                 io_u->file->file_name,
719                 io_u->offset + hdr_num * hdr_len, hdr_len);
720         return EILSEQ;
721 }
722
723 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
724 {
725         struct verify_header *hdr;
726         struct io_u *io_u = *io_u_ptr;
727         unsigned int header_size, hdr_inc, hdr_num = 0;
728         void *p;
729         int ret;
730
731         if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
732                 return 0;
733         /*
734          * If the IO engine is faking IO (like null), then just pretend
735          * we verified everything.
736          */
737         if (td->io_ops->flags & FIO_FAKEIO)
738                 return 0;
739
740         if (io_u->flags & IO_U_F_TRIMMED) {
741                 ret = verify_trimmed_io_u(td, io_u);
742                 goto done;
743         }
744
745         hdr_inc = get_hdr_inc(td, io_u);
746
747         ret = 0;
748         for (p = io_u->buf; p < io_u->buf + io_u->buflen;
749              p += hdr_inc, hdr_num++) {
750                 struct vcont vc = {
751                         .io_u           = io_u,
752                         .hdr_num        = hdr_num,
753                         .td             = td,
754                 };
755                 unsigned int verify_type;
756
757                 if (ret && td->o.verify_fatal)
758                         break;
759
760                 header_size = __hdr_size(td->o.verify);
761                 if (td->o.verify_offset)
762                         memswp(p, p + td->o.verify_offset, header_size);
763                 hdr = p;
764
765                 /*
766                  * Make rand_seed check pass when have verifysort or
767                  * verify_backlog.
768                  */
769                 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
770                         io_u->rand_seed = hdr->rand_seed;
771
772                 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
773                 if (ret)
774                         return ret;
775
776                 if (td->o.verify != VERIFY_NONE)
777                         verify_type = td->o.verify;
778                 else
779                         verify_type = hdr->verify_type;
780
781                 switch (verify_type) {
782                 case VERIFY_MD5:
783                         ret = verify_io_u_md5(hdr, &vc);
784                         break;
785                 case VERIFY_CRC64:
786                         ret = verify_io_u_crc64(hdr, &vc);
787                         break;
788                 case VERIFY_CRC32C:
789                 case VERIFY_CRC32C_INTEL:
790                         ret = verify_io_u_crc32c(hdr, &vc);
791                         break;
792                 case VERIFY_CRC32:
793                         ret = verify_io_u_crc32(hdr, &vc);
794                         break;
795                 case VERIFY_CRC16:
796                         ret = verify_io_u_crc16(hdr, &vc);
797                         break;
798                 case VERIFY_CRC7:
799                         ret = verify_io_u_crc7(hdr, &vc);
800                         break;
801                 case VERIFY_SHA256:
802                         ret = verify_io_u_sha256(hdr, &vc);
803                         break;
804                 case VERIFY_SHA512:
805                         ret = verify_io_u_sha512(hdr, &vc);
806                         break;
807                 case VERIFY_XXHASH:
808                         ret = verify_io_u_xxhash(hdr, &vc);
809                         break;
810                 case VERIFY_META:
811                         ret = verify_io_u_meta(hdr, &vc);
812                         break;
813                 case VERIFY_SHA1:
814                         ret = verify_io_u_sha1(hdr, &vc);
815                         break;
816                 case VERIFY_PATTERN:
817                         ret = verify_io_u_pattern(hdr, &vc);
818                         break;
819                 default:
820                         log_err("Bad verify type %u\n", hdr->verify_type);
821                         ret = EINVAL;
822                 }
823
824                 if (ret && verify_type != hdr->verify_type)
825                         log_err("fio: verify type mismatch (%u media, %u given)\n",
826                                         hdr->verify_type, verify_type);
827         }
828
829 done:
830         if (ret && td->o.verify_fatal)
831                 fio_mark_td_terminate(td);
832
833         return ret;
834 }
835
836 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
837                       struct io_u *io_u, unsigned int header_num)
838 {
839         struct vhdr_meta *vh = hdr_priv(hdr);
840
841         vh->thread = td->thread_number;
842
843         vh->time_sec = io_u->start_time.tv_sec;
844         vh->time_usec = io_u->start_time.tv_usec;
845
846         vh->numberio = io_u->numberio;
847
848         vh->offset = io_u->offset + header_num * td->o.verify_interval;
849 }
850
851 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
852 {
853         struct vhdr_xxhash *vh = hdr_priv(hdr);
854         void *state;
855
856         state = XXH32_init(1);
857         XXH32_update(state, p, len);
858         vh->hash = XXH32_digest(state);
859 }
860
861 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
862 {
863         struct vhdr_sha512 *vh = hdr_priv(hdr);
864         struct fio_sha512_ctx sha512_ctx = {
865                 .buf = vh->sha512,
866         };
867
868         fio_sha512_init(&sha512_ctx);
869         fio_sha512_update(&sha512_ctx, p, len);
870 }
871
872 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
873 {
874         struct vhdr_sha256 *vh = hdr_priv(hdr);
875         struct fio_sha256_ctx sha256_ctx = {
876                 .buf = vh->sha256,
877         };
878
879         fio_sha256_init(&sha256_ctx);
880         fio_sha256_update(&sha256_ctx, p, len);
881 }
882
883 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
884 {
885         struct vhdr_sha1 *vh = hdr_priv(hdr);
886         struct fio_sha1_ctx sha1_ctx = {
887                 .H = vh->sha1,
888         };
889
890         fio_sha1_init(&sha1_ctx);
891         fio_sha1_update(&sha1_ctx, p, len);
892 }
893
894 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
895 {
896         struct vhdr_crc7 *vh = hdr_priv(hdr);
897
898         vh->crc7 = fio_crc7(p, len);
899 }
900
901 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
902 {
903         struct vhdr_crc16 *vh = hdr_priv(hdr);
904
905         vh->crc16 = fio_crc16(p, len);
906 }
907
908 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
909 {
910         struct vhdr_crc32 *vh = hdr_priv(hdr);
911
912         vh->crc32 = fio_crc32(p, len);
913 }
914
915 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
916 {
917         struct vhdr_crc32 *vh = hdr_priv(hdr);
918
919         vh->crc32 = fio_crc32c(p, len);
920 }
921
922 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
923 {
924         struct vhdr_crc64 *vh = hdr_priv(hdr);
925
926         vh->crc64 = fio_crc64(p, len);
927 }
928
929 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
930 {
931         struct vhdr_md5 *vh = hdr_priv(hdr);
932         struct fio_md5_ctx md5_ctx = {
933                 .hash = (uint32_t *) vh->md5_digest,
934         };
935
936         fio_md5_init(&md5_ctx);
937         fio_md5_update(&md5_ctx, p, len);
938 }
939
940 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
941                          struct verify_header *hdr, unsigned int header_num,
942                          unsigned int header_len)
943 {
944         unsigned int data_len;
945         void *data, *p;
946
947         p = (void *) hdr;
948
949         hdr->magic = FIO_HDR_MAGIC;
950         hdr->verify_type = td->o.verify;
951         hdr->len = header_len;
952         hdr->rand_seed = io_u->rand_seed;
953         hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
954
955         data_len = header_len - hdr_size(hdr);
956
957         data = p + hdr_size(hdr);
958         switch (td->o.verify) {
959         case VERIFY_MD5:
960                 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
961                                                 io_u, hdr->len);
962                 fill_md5(hdr, data, data_len);
963                 break;
964         case VERIFY_CRC64:
965                 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
966                                                 io_u, hdr->len);
967                 fill_crc64(hdr, data, data_len);
968                 break;
969         case VERIFY_CRC32C:
970         case VERIFY_CRC32C_INTEL:
971                 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
972                                                 io_u, hdr->len);
973                 fill_crc32c(hdr, data, data_len);
974                 break;
975         case VERIFY_CRC32:
976                 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
977                                                 io_u, hdr->len);
978                 fill_crc32(hdr, data, data_len);
979                 break;
980         case VERIFY_CRC16:
981                 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
982                                                 io_u, hdr->len);
983                 fill_crc16(hdr, data, data_len);
984                 break;
985         case VERIFY_CRC7:
986                 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
987                                                 io_u, hdr->len);
988                 fill_crc7(hdr, data, data_len);
989                 break;
990         case VERIFY_SHA256:
991                 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
992                                                 io_u, hdr->len);
993                 fill_sha256(hdr, data, data_len);
994                 break;
995         case VERIFY_SHA512:
996                 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
997                                                 io_u, hdr->len);
998                 fill_sha512(hdr, data, data_len);
999                 break;
1000         case VERIFY_XXHASH:
1001                 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1002                                                 io_u, hdr->len);
1003                 fill_xxhash(hdr, data, data_len);
1004                 break;
1005         case VERIFY_META:
1006                 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1007                                                 io_u, hdr->len);
1008                 fill_meta(hdr, td, io_u, header_num);
1009                 break;
1010         case VERIFY_SHA1:
1011                 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1012                                                 io_u, hdr->len);
1013                 fill_sha1(hdr, data, data_len);
1014                 break;
1015         case VERIFY_PATTERN:
1016                 /* nothing to do here */
1017                 break;
1018         default:
1019                 log_err("fio: bad verify type: %d\n", td->o.verify);
1020                 assert(0);
1021         }
1022         if (td->o.verify_offset)
1023                 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
1024 }
1025
1026 /*
1027  * fill body of io_u->buf with random data and add a header with the
1028  * checksum of choice
1029  */
1030 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1031 {
1032         if (td->o.verify == VERIFY_NULL)
1033                 return;
1034
1035         io_u->numberio = td->io_issues[io_u->ddir];
1036
1037         fill_pattern_headers(td, io_u, 0, 0);
1038 }
1039
1040 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1041 {
1042         struct io_piece *ipo = NULL;
1043
1044         /*
1045          * this io_u is from a requeue, we already filled the offsets
1046          */
1047         if (io_u->file)
1048                 return 0;
1049
1050         if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1051                 struct rb_node *n = rb_first(&td->io_hist_tree);
1052
1053                 ipo = rb_entry(n, struct io_piece, rb_node);
1054
1055                 /*
1056                  * Ensure that the associated IO has completed
1057                  */
1058                 read_barrier();
1059                 if (ipo->flags & IP_F_IN_FLIGHT)
1060                         goto nothing;
1061
1062                 rb_erase(n, &td->io_hist_tree);
1063                 assert(ipo->flags & IP_F_ONRB);
1064                 ipo->flags &= ~IP_F_ONRB;
1065         } else if (!flist_empty(&td->io_hist_list)) {
1066                 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1067
1068                 /*
1069                  * Ensure that the associated IO has completed
1070                  */
1071                 read_barrier();
1072                 if (ipo->flags & IP_F_IN_FLIGHT)
1073                         goto nothing;
1074
1075                 flist_del(&ipo->list);
1076                 assert(ipo->flags & IP_F_ONLIST);
1077                 ipo->flags &= ~IP_F_ONLIST;
1078         }
1079
1080         if (ipo) {
1081                 td->io_hist_len--;
1082
1083                 io_u->offset = ipo->offset;
1084                 io_u->buflen = ipo->len;
1085                 io_u->numberio = ipo->numberio;
1086                 io_u->file = ipo->file;
1087                 io_u->flags |= IO_U_F_VER_LIST;
1088
1089                 if (ipo->flags & IP_F_TRIMMED)
1090                         io_u->flags |= IO_U_F_TRIMMED;
1091
1092                 if (!fio_file_open(io_u->file)) {
1093                         int r = td_io_open_file(td, io_u->file);
1094
1095                         if (r) {
1096                                 dprint(FD_VERIFY, "failed file %s open\n",
1097                                                 io_u->file->file_name);
1098                                 return 1;
1099                         }
1100                 }
1101
1102                 get_file(ipo->file);
1103                 assert(fio_file_open(io_u->file));
1104                 io_u->ddir = DDIR_READ;
1105                 io_u->xfer_buf = io_u->buf;
1106                 io_u->xfer_buflen = io_u->buflen;
1107
1108                 remove_trim_entry(td, ipo);
1109                 free(ipo);
1110                 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1111
1112                 if (!td->o.verify_pattern_bytes) {
1113                         io_u->rand_seed = __rand(&td->verify_state);
1114                         if (sizeof(int) != sizeof(long *))
1115                                 io_u->rand_seed *= __rand(&td->verify_state);
1116                 }
1117                 return 0;
1118         }
1119
1120 nothing:
1121         dprint(FD_VERIFY, "get_next_verify: empty\n");
1122         return 1;
1123 }
1124
1125 void fio_verify_init(struct thread_data *td)
1126 {
1127         if (td->o.verify == VERIFY_CRC32C_INTEL ||
1128             td->o.verify == VERIFY_CRC32C) {
1129                 crc32c_intel_probe();
1130         }
1131 }
1132
1133 static void *verify_async_thread(void *data)
1134 {
1135         struct thread_data *td = data;
1136         struct io_u *io_u;
1137         int ret = 0;
1138
1139         if (td->o.verify_cpumask_set &&
1140             fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1141                 log_err("fio: failed setting verify thread affinity\n");
1142                 goto done;
1143         }
1144
1145         do {
1146                 FLIST_HEAD(list);
1147
1148                 read_barrier();
1149                 if (td->verify_thread_exit)
1150                         break;
1151
1152                 pthread_mutex_lock(&td->io_u_lock);
1153
1154                 while (flist_empty(&td->verify_list) &&
1155                        !td->verify_thread_exit) {
1156                         ret = pthread_cond_wait(&td->verify_cond,
1157                                                         &td->io_u_lock);
1158                         if (ret) {
1159                                 pthread_mutex_unlock(&td->io_u_lock);
1160                                 break;
1161                         }
1162                 }
1163
1164                 flist_splice_init(&td->verify_list, &list);
1165                 pthread_mutex_unlock(&td->io_u_lock);
1166
1167                 if (flist_empty(&list))
1168                         continue;
1169
1170                 while (!flist_empty(&list)) {
1171                         io_u = flist_first_entry(&list, struct io_u, verify_list);
1172                         flist_del_init(&io_u->verify_list);
1173
1174                         io_u->flags |= IO_U_F_NO_FILE_PUT;
1175                         ret = verify_io_u(td, &io_u);
1176
1177                         put_io_u(td, io_u);
1178                         if (!ret)
1179                                 continue;
1180                         if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1181                                 update_error_count(td, ret);
1182                                 td_clear_error(td);
1183                                 ret = 0;
1184                         }
1185                 }
1186         } while (!ret);
1187
1188         if (ret) {
1189                 td_verror(td, ret, "async_verify");
1190                 if (td->o.verify_fatal)
1191                         fio_mark_td_terminate(td);
1192         }
1193
1194 done:
1195         pthread_mutex_lock(&td->io_u_lock);
1196         td->nr_verify_threads--;
1197         pthread_mutex_unlock(&td->io_u_lock);
1198
1199         pthread_cond_signal(&td->free_cond);
1200         return NULL;
1201 }
1202
1203 int verify_async_init(struct thread_data *td)
1204 {
1205         int i, ret;
1206         pthread_attr_t attr;
1207
1208         pthread_attr_init(&attr);
1209         pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1210
1211         td->verify_thread_exit = 0;
1212
1213         td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1214         for (i = 0; i < td->o.verify_async; i++) {
1215                 ret = pthread_create(&td->verify_threads[i], &attr,
1216                                         verify_async_thread, td);
1217                 if (ret) {
1218                         log_err("fio: async verify creation failed: %s\n",
1219                                         strerror(ret));
1220                         break;
1221                 }
1222                 ret = pthread_detach(td->verify_threads[i]);
1223                 if (ret) {
1224                         log_err("fio: async verify thread detach failed: %s\n",
1225                                         strerror(ret));
1226                         break;
1227                 }
1228                 td->nr_verify_threads++;
1229         }
1230
1231         pthread_attr_destroy(&attr);
1232
1233         if (i != td->o.verify_async) {
1234                 log_err("fio: only %d verify threads started, exiting\n", i);
1235                 td->verify_thread_exit = 1;
1236                 write_barrier();
1237                 pthread_cond_broadcast(&td->verify_cond);
1238                 return 1;
1239         }
1240
1241         return 0;
1242 }
1243
1244 void verify_async_exit(struct thread_data *td)
1245 {
1246         td->verify_thread_exit = 1;
1247         write_barrier();
1248         pthread_cond_broadcast(&td->verify_cond);
1249
1250         pthread_mutex_lock(&td->io_u_lock);
1251
1252         while (td->nr_verify_threads)
1253                 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1254
1255         pthread_mutex_unlock(&td->io_u_lock);
1256         free(td->verify_threads);
1257         td->verify_threads = NULL;
1258 }
1259
1260 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1261 {
1262         struct all_io_list *rep;
1263         struct thread_data *td;
1264         size_t depth;
1265         void *next;
1266         int i, nr;
1267
1268         compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1269
1270         /*
1271          * Calculate reply space needed. We need one 'io_state' per thread,
1272          * and the size will vary depending on depth.
1273          */
1274         depth = 0;
1275         nr = 0;
1276         for_each_td(td, i) {
1277                 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1278                         continue;
1279                 td->stop_io = 1;
1280                 td->flags |= TD_F_VSTATE_SAVED;
1281                 depth += td->o.iodepth;
1282                 nr++;
1283         }
1284
1285         if (!nr)
1286                 return NULL;
1287
1288         *sz = sizeof(*rep);
1289         *sz += nr * sizeof(struct thread_io_list);
1290         *sz += depth * sizeof(uint64_t);
1291         rep = malloc(*sz);
1292
1293         rep->threads = cpu_to_le64((uint64_t) nr);
1294
1295         next = &rep->state[0];
1296         for_each_td(td, i) {
1297                 struct thread_io_list *s = next;
1298                 unsigned int comps;
1299
1300                 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1301                         continue;
1302
1303                 if (td->last_write_comp) {
1304                         int j, k;
1305
1306                         if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1307                                 comps = td->io_blocks[DDIR_WRITE];
1308                         else
1309                                 comps = td->o.iodepth;
1310
1311                         k = td->last_write_idx - 1;
1312                         for (j = 0; j < comps; j++) {
1313                                 if (k == -1)
1314                                         k = td->o.iodepth - 1;
1315                                 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1316                                 k--;
1317                         }
1318                 } else
1319                         comps = 0;
1320
1321                 s->no_comps = cpu_to_le64((uint64_t) comps);
1322                 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1323                 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1324                 s->index = cpu_to_le64((uint64_t) i);
1325                 s->rand.s[0] = cpu_to_le32(td->random_state.s1);
1326                 s->rand.s[1] = cpu_to_le32(td->random_state.s2);
1327                 s->rand.s[2] = cpu_to_le32(td->random_state.s3);
1328                 s->rand.s[3] = 0;
1329                 strncpy((char *) s->name, td->o.name, sizeof(s->name));
1330                 next = io_list_next(s);
1331         }
1332
1333         return rep;
1334 }
1335
1336 static int open_state_file(const char *name, const char *prefix, int num,
1337                            int for_write)
1338 {
1339         char out[64];
1340         int flags;
1341         int fd;
1342
1343         if (for_write)
1344                 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1345         else
1346                 flags = O_RDONLY;
1347
1348         verify_state_gen_name(out, sizeof(out), name, prefix, num);
1349
1350         fd = open(out, flags, 0644);
1351         if (fd == -1) {
1352                 perror("fio: open state file");
1353                 return -1;
1354         }
1355
1356         return fd;
1357 }
1358
1359 static int write_thread_list_state(struct thread_io_list *s,
1360                                    const char *prefix)
1361 {
1362         struct verify_state_hdr hdr;
1363         uint64_t crc;
1364         ssize_t ret;
1365         int fd;
1366
1367         fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1368         if (fd == -1)
1369                 return 1;
1370
1371         crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1372
1373         hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1374         hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1375         hdr.crc = cpu_to_le64(crc);
1376         ret = write(fd, &hdr, sizeof(hdr));
1377         if (ret != sizeof(hdr))
1378                 goto write_fail;
1379
1380         ret = write(fd, s, thread_io_list_sz(s));
1381         if (ret != thread_io_list_sz(s)) {
1382 write_fail:
1383                 if (ret < 0)
1384                         perror("fio: write state file");
1385                 log_err("fio: failed to write state file\n");
1386                 ret = 1;
1387         } else
1388                 ret = 0;
1389
1390         close(fd);
1391         return ret;
1392 }
1393
1394 void __verify_save_state(struct all_io_list *state, const char *prefix)
1395 {
1396         struct thread_io_list *s = &state->state[0];
1397         unsigned int i;
1398
1399         for (i = 0; i < le64_to_cpu(state->threads); i++) {
1400                 write_thread_list_state(s,  prefix);
1401                 s = io_list_next(s);
1402         }
1403 }
1404
1405 void verify_save_state(void)
1406 {
1407         struct all_io_list *state;
1408         size_t sz;
1409
1410         state = get_all_io_list(IO_LIST_ALL, &sz);
1411         if (state) {
1412                 __verify_save_state(state, "local");
1413                 free(state);
1414         }
1415 }
1416
1417 void verify_free_state(struct thread_data *td)
1418 {
1419         if (td->vstate)
1420                 free(td->vstate);
1421 }
1422
1423 void verify_convert_assign_state(struct thread_data *td,
1424                                  struct thread_io_list *s)
1425 {
1426         int i;
1427
1428         s->no_comps = le64_to_cpu(s->no_comps);
1429         s->depth = le64_to_cpu(s->depth);
1430         s->numberio = le64_to_cpu(s->numberio);
1431         for (i = 0; i < 4; i++)
1432                 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1433         for (i = 0; i < s->no_comps; i++)
1434                 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1435
1436         td->vstate = s;
1437 }
1438
1439 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s)
1440 {
1441         uint64_t crc;
1442
1443         hdr->version = le64_to_cpu(hdr->version);
1444         hdr->size = le64_to_cpu(hdr->size);
1445         hdr->crc = le64_to_cpu(hdr->crc);
1446
1447         if (hdr->version != VSTATE_HDR_VERSION)
1448                 return 1;
1449
1450         crc = fio_crc32c((void *)s, hdr->size);
1451         if (crc != hdr->crc)
1452                 return 1;
1453
1454         return 0;
1455 }
1456
1457 int verify_load_state(struct thread_data *td, const char *prefix)
1458 {
1459         struct thread_io_list *s = NULL;
1460         struct verify_state_hdr hdr;
1461         uint64_t crc;
1462         ssize_t ret;
1463         int fd;
1464
1465         if (!td->o.verify_state)
1466                 return 0;
1467
1468         fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1469         if (fd == -1)
1470                 return 1;
1471
1472         ret = read(fd, &hdr, sizeof(hdr));
1473         if (ret != sizeof(hdr)) {
1474                 if (ret < 0)
1475                         td_verror(td, errno, "read verify state hdr");
1476                 log_err("fio: failed reading verify state header\n");
1477                 goto err;
1478         }
1479
1480         hdr.version = le64_to_cpu(hdr.version);
1481         hdr.size = le64_to_cpu(hdr.size);
1482         hdr.crc = le64_to_cpu(hdr.crc);
1483
1484         if (hdr.version != VSTATE_HDR_VERSION) {
1485                 log_err("fio: bad version in verify state header\n");
1486                 goto err;
1487         }
1488
1489         s = malloc(hdr.size);
1490         ret = read(fd, s, hdr.size);
1491         if (ret != hdr.size) {
1492                 if (ret < 0)
1493                         td_verror(td, errno, "read verify state");
1494                 log_err("fio: failed reading verity state\n");
1495                 goto err;
1496         }
1497
1498         crc = fio_crc32c((void *)s, hdr.size);
1499         if (crc != hdr.crc) {
1500                 log_err("fio: verify state is corrupt\n");
1501                 goto err;
1502         }
1503
1504         close(fd);
1505
1506         verify_convert_assign_state(td, s);
1507         return 0;
1508 err:
1509         if (s)
1510                 free(s);
1511         close(fd);
1512         return 1;
1513 }
1514
1515 /*
1516  * Use the loaded verify state to know when to stop doing verification
1517  */
1518 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1519 {
1520         struct thread_io_list *s = td->vstate;
1521         int i;
1522
1523         if (!s)
1524                 return 0;
1525
1526         /*
1527          * If we're not into the window of issues - depth yet, continue
1528          */
1529         if (td->io_blocks[DDIR_READ] < s->depth ||
1530             s->numberio - td->io_blocks[DDIR_READ] > s->depth)
1531                 return 0;
1532
1533         /*
1534          * We're in the window of having to check if this io was
1535          * completed or not. If the IO was seen as completed, then
1536          * lets verify it.
1537          */
1538         for (i = 0; i < s->no_comps; i++)
1539                 if (io_u->offset == s->offsets[i])
1540                         return 0;
1541
1542         /*
1543          * Not found, we have to stop
1544          */
1545         return 1;
1546 }