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