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