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