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