Add FIO_PREFERRED_CLOCK_SOURCE to allow selection of clock source on a per-platform...
[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 "smalloc.h"
14 #include "trim.h"
15 #include "lib/rand.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
27 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
28                          struct verify_header *hdr, unsigned int header_num,
29                          unsigned int header_len);
30
31 void fill_pattern(struct thread_data *td, void *p, unsigned int len, struct io_u *io_u, unsigned long seed, int use_seed)
32 {
33         switch (td->o.verify_pattern_bytes) {
34         case 0:
35                 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
36                 if (use_seed)
37                         __fill_random_buf(p, len, seed);
38                 else
39                         io_u->rand_seed = fill_random_buf(&td->buf_state, p, len);
40                 break;
41         case 1:
42                 if (io_u->buf_filled_len >= len) {
43                         dprint(FD_VERIFY, "using already filled verify pattern b=0 len=%u\n", len);
44                         return;
45                 }
46                 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
47                 memset(p, td->o.verify_pattern[0], len);
48                 io_u->buf_filled_len = len;
49                 break;
50         default: {
51                 unsigned int i = 0, size = 0;
52                 unsigned char *b = p;
53
54                 if (io_u->buf_filled_len >= len) {
55                         dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
56                                         td->o.verify_pattern_bytes, len);
57                         return;
58                 }
59
60                 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
61                                         td->o.verify_pattern_bytes, len);
62
63                 while (i < len) {
64                         size = td->o.verify_pattern_bytes;
65                         if (size > (len - i))
66                                 size = len - i;
67                         memcpy(b+i, td->o.verify_pattern, size);
68                         i += size;
69                 }
70                 io_u->buf_filled_len = len;
71                 break;
72                 }
73         }
74 }
75
76 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
77 {
78         unsigned int hdr_inc;
79
80         hdr_inc = io_u->buflen;
81         if (td->o.verify_interval)
82                 hdr_inc = td->o.verify_interval;
83
84         return hdr_inc;
85 }
86
87 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
88                                  unsigned long seed, int use_seed)
89 {
90         unsigned int hdr_inc, header_num;
91         struct verify_header *hdr;
92         void *p = io_u->buf;
93
94         fill_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
95
96         hdr_inc = get_hdr_inc(td, io_u);
97         header_num = 0;
98         for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
99                 hdr = p;
100                 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
101                 header_num++;
102         }
103 }
104
105 static void memswp(void *buf1, void *buf2, unsigned int len)
106 {
107         char swap[200];
108
109         assert(len <= sizeof(swap));
110
111         memcpy(&swap, buf1, len);
112         memcpy(buf1, buf2, len);
113         memcpy(buf2, &swap, len);
114 }
115
116 static void hexdump(void *buffer, int len)
117 {
118         unsigned char *p = buffer;
119         int i;
120
121         for (i = 0; i < len; i++)
122                 log_err("%02x", p[i]);
123         log_err("\n");
124 }
125
126 /*
127  * Prepare for seperation of verify_header and checksum header
128  */
129 static inline unsigned int __hdr_size(int verify_type)
130 {
131         unsigned int len = 0;
132
133         switch (verify_type) {
134         case VERIFY_NONE:
135         case VERIFY_NULL:
136                 len = 0;
137                 break;
138         case VERIFY_MD5:
139                 len = sizeof(struct vhdr_md5);
140                 break;
141         case VERIFY_CRC64:
142                 len = sizeof(struct vhdr_crc64);
143                 break;
144         case VERIFY_CRC32C:
145         case VERIFY_CRC32:
146         case VERIFY_CRC32C_INTEL:
147                 len = sizeof(struct vhdr_crc32);
148                 break;
149         case VERIFY_CRC16:
150                 len = sizeof(struct vhdr_crc16);
151                 break;
152         case VERIFY_CRC7:
153                 len = sizeof(struct vhdr_crc7);
154                 break;
155         case VERIFY_SHA256:
156                 len = sizeof(struct vhdr_sha256);
157                 break;
158         case VERIFY_SHA512:
159                 len = sizeof(struct vhdr_sha512);
160                 break;
161         case VERIFY_META:
162                 len = sizeof(struct vhdr_meta);
163                 break;
164         case VERIFY_SHA1:
165                 len = sizeof(struct vhdr_sha1);
166                 break;
167         case VERIFY_PATTERN:
168                 len = 0;
169                 break;
170         default:
171                 log_err("fio: unknown verify header!\n");
172                 assert(0);
173         }
174
175         return len + sizeof(struct verify_header);
176 }
177
178 static inline unsigned int hdr_size(struct verify_header *hdr)
179 {
180         return __hdr_size(hdr->verify_type);
181 }
182
183 static void *hdr_priv(struct verify_header *hdr)
184 {
185         void *priv = hdr;
186
187         return priv + sizeof(struct verify_header);
188 }
189
190 /*
191  * Verify container, pass info to verify handlers and allow them to
192  * pass info back in case of error
193  */
194 struct vcont {
195         /*
196          * Input
197          */
198         struct io_u *io_u;
199         unsigned int hdr_num;
200         struct thread_data *td;
201
202         /*
203          * Output, only valid in case of error
204          */
205         const char *name;
206         void *good_crc;
207         void *bad_crc;
208         unsigned int crc_len;
209 };
210
211 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
212                      const char *type, struct fio_file *f)
213 {
214         char *ptr, fname[256];
215         int ret, fd;
216
217         ptr = strdup(f->file_name);
218         strcpy(fname, basename(ptr));
219
220         sprintf(fname + strlen(fname), ".%llu.%s", offset, type);
221
222         fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
223         if (fd < 0) {
224                 perror("open verify buf file");
225                 return;
226         }
227
228         while (len) {
229                 ret = write(fd, buf, len);
230                 if (!ret)
231                         break;
232                 else if (ret < 0) {
233                         perror("write verify buf file");
234                         break;
235                 }
236                 len -= ret;
237                 buf += ret;
238         }
239
240         close(fd);
241         log_err("       %s data dumped as %s\n", type, fname);
242         free(ptr);
243 }
244
245 /*
246  * Dump the contents of the read block and re-generate the correct data
247  * and dump that too.
248  */
249 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
250 {
251         struct thread_data *td = vc->td;
252         struct io_u *io_u = vc->io_u;
253         unsigned long hdr_offset;
254         struct io_u dummy;
255         void *buf;
256
257         if (!td->o.verify_dump)
258                 return;
259
260         /*
261          * Dump the contents we just read off disk
262          */
263         hdr_offset = vc->hdr_num * hdr->len;
264
265         dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
266                         "received", vc->io_u->file);
267
268         /*
269          * Allocate a new buf and re-generate the original data
270          */
271         buf = malloc(io_u->buflen);
272         dummy = *io_u;
273         dummy.buf = buf;
274         dummy.rand_seed = hdr->rand_seed;
275         dummy.buf_filled_len = 0;
276
277         fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
278
279         dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
280                         "expected", vc->io_u->file);
281         free(buf);
282 }
283
284 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
285 {
286         unsigned long long offset;
287
288         offset = vc->io_u->offset;
289         offset += vc->hdr_num * hdr->len;
290         log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
291                         vc->name, vc->io_u->file->file_name, offset, hdr->len);
292
293         if (vc->good_crc && vc->bad_crc) {
294                 log_err("       Expected CRC: ");
295                 hexdump(vc->good_crc, vc->crc_len);
296                 log_err("       Received CRC: ");
297                 hexdump(vc->bad_crc, vc->crc_len);
298         }
299
300         dump_verify_buffers(hdr, vc);
301 }
302
303 /*
304  * Return data area 'header_num'
305  */
306 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
307 {
308         return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
309 }
310
311 static unsigned int hweight8(unsigned int w)
312 {
313         unsigned int res = w - ((w >> 1) & 0x55);
314
315         res = (res & 0x33) + ((res >> 2) & 0x33);
316         return (res + (res >> 4)) & 0x0F;
317 }
318
319 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
320 {
321         struct thread_data *td = vc->td;
322         struct io_u *io_u = vc->io_u;
323         char *buf, *pattern;
324         unsigned int header_size = __hdr_size(td->o.verify);
325         unsigned int len, mod, i, size, pattern_size;
326
327         pattern = td->o.verify_pattern;
328         pattern_size = td->o.verify_pattern_bytes;
329         if (pattern_size <= 1)
330                 pattern_size = MAX_PATTERN_SIZE;
331         buf = (void *) hdr + header_size;
332         len = get_hdr_inc(td, io_u) - header_size;
333         mod = header_size % pattern_size;
334
335         for (i = 0; i < len; i += size) {
336                 size = pattern_size - mod;
337                 if (size > (len - i))
338                         size = len - i;
339                 if (memcmp(buf + i, pattern + mod, size))
340                         // Let the slow compare find the first mismatch byte.
341                         break;
342                 mod = 0;
343         }
344
345         for (; i < len; i++) {
346                 if (buf[i] != pattern[mod]) {
347                         unsigned int bits;
348
349                         bits = hweight8(buf[i] ^ pattern[mod]);
350                         log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
351                                 buf[i], pattern[mod], bits);
352                         log_err("fio: bad pattern block offset %u\n", i);
353                         dump_verify_buffers(hdr, vc);
354                         return EILSEQ;
355                 }
356                 mod++;
357                 if (mod == td->o.verify_pattern_bytes)
358                         mod = 0;
359         }
360
361         return 0;
362 }
363
364 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
365 {
366         struct thread_data *td = vc->td;
367         struct vhdr_meta *vh = hdr_priv(hdr);
368         struct io_u *io_u = vc->io_u;
369         int ret = EILSEQ;
370
371         dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
372
373         if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
374                 ret = 0;
375
376         if (td->o.verify_pattern_bytes)
377                 ret |= verify_io_u_pattern(hdr, vc);
378
379         if (!ret)
380                 return 0;
381
382         vc->name = "meta";
383         log_verify_failure(hdr, vc);
384         return ret;
385 }
386
387 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
388 {
389         void *p = io_u_verify_off(hdr, vc);
390         struct vhdr_sha512 *vh = hdr_priv(hdr);
391         uint8_t sha512[128];
392         struct sha512_ctx sha512_ctx = {
393                 .buf = sha512,
394         };
395
396         dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
397
398         sha512_init(&sha512_ctx);
399         sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
400
401         if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
402                 return 0;
403
404         vc->name = "sha512";
405         vc->good_crc = vh->sha512;
406         vc->bad_crc = sha512_ctx.buf;
407         vc->crc_len = sizeof(vh->sha512);
408         log_verify_failure(hdr, vc);
409         return EILSEQ;
410 }
411
412 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
413 {
414         void *p = io_u_verify_off(hdr, vc);
415         struct vhdr_sha256 *vh = hdr_priv(hdr);
416         uint8_t sha256[64];
417         struct sha256_ctx sha256_ctx = {
418                 .buf = sha256,
419         };
420
421         dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
422
423         sha256_init(&sha256_ctx);
424         sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
425
426         if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
427                 return 0;
428
429         vc->name = "sha256";
430         vc->good_crc = vh->sha256;
431         vc->bad_crc = sha256_ctx.buf;
432         vc->crc_len = sizeof(vh->sha256);
433         log_verify_failure(hdr, vc);
434         return EILSEQ;
435 }
436
437 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
438 {
439         void *p = io_u_verify_off(hdr, vc);
440         struct vhdr_sha1 *vh = hdr_priv(hdr);
441         uint32_t sha1[5];
442         struct sha1_ctx sha1_ctx = {
443                 .H = sha1,
444         };
445
446         dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
447
448         sha1_init(&sha1_ctx);
449         sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
450
451         if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
452                 return 0;
453
454         vc->name = "sha1";
455         vc->good_crc = vh->sha1;
456         vc->bad_crc = sha1_ctx.H;
457         vc->crc_len = sizeof(vh->sha1);
458         log_verify_failure(hdr, vc);
459         return EILSEQ;
460 }
461
462 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
463 {
464         void *p = io_u_verify_off(hdr, vc);
465         struct vhdr_crc7 *vh = hdr_priv(hdr);
466         unsigned char c;
467
468         dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
469
470         c = crc7(p, hdr->len - hdr_size(hdr));
471
472         if (c == vh->crc7)
473                 return 0;
474
475         vc->name = "crc7";
476         vc->good_crc = &vh->crc7;
477         vc->bad_crc = &c;
478         vc->crc_len = 1;
479         log_verify_failure(hdr, vc);
480         return EILSEQ;
481 }
482
483 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
484 {
485         void *p = io_u_verify_off(hdr, vc);
486         struct vhdr_crc16 *vh = hdr_priv(hdr);
487         unsigned short c;
488
489         dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
490
491         c = crc16(p, hdr->len - hdr_size(hdr));
492
493         if (c == vh->crc16)
494                 return 0;
495
496         vc->name = "crc16";
497         vc->good_crc = &vh->crc16;
498         vc->bad_crc = &c;
499         vc->crc_len = 2;
500         log_verify_failure(hdr, vc);
501         return EILSEQ;
502 }
503
504 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
505 {
506         void *p = io_u_verify_off(hdr, vc);
507         struct vhdr_crc64 *vh = hdr_priv(hdr);
508         unsigned long long c;
509
510         dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
511
512         c = crc64(p, hdr->len - hdr_size(hdr));
513
514         if (c == vh->crc64)
515                 return 0;
516
517         vc->name = "crc64";
518         vc->good_crc = &vh->crc64;
519         vc->bad_crc = &c;
520         vc->crc_len = 8;
521         log_verify_failure(hdr, vc);
522         return EILSEQ;
523 }
524
525 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
526 {
527         void *p = io_u_verify_off(hdr, vc);
528         struct vhdr_crc32 *vh = hdr_priv(hdr);
529         uint32_t c;
530
531         dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
532
533         c = crc32(p, hdr->len - hdr_size(hdr));
534
535         if (c == vh->crc32)
536                 return 0;
537
538         vc->name = "crc32";
539         vc->good_crc = &vh->crc32;
540         vc->bad_crc = &c;
541         vc->crc_len = 4;
542         log_verify_failure(hdr, vc);
543         return EILSEQ;
544 }
545
546 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
547 {
548         void *p = io_u_verify_off(hdr, vc);
549         struct vhdr_crc32 *vh = hdr_priv(hdr);
550         uint32_t c;
551
552         dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
553
554         if (hdr->verify_type == VERIFY_CRC32C_INTEL)
555                 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
556         else
557                 c = crc32c(p, hdr->len - hdr_size(hdr));
558
559         if (c == vh->crc32)
560                 return 0;
561
562         vc->name = "crc32c";
563         vc->good_crc = &vh->crc32;
564         vc->bad_crc = &c;
565         vc->crc_len = 4;
566         log_verify_failure(hdr, vc);
567         return EILSEQ;
568 }
569
570 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
571 {
572         void *p = io_u_verify_off(hdr, vc);
573         struct vhdr_md5 *vh = hdr_priv(hdr);
574         uint32_t hash[MD5_HASH_WORDS];
575         struct md5_ctx md5_ctx = {
576                 .hash = hash,
577         };
578
579         dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
580
581         md5_init(&md5_ctx);
582         md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
583
584         if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
585                 return 0;
586
587         vc->name = "md5";
588         vc->good_crc = vh->md5_digest;
589         vc->bad_crc = md5_ctx.hash;
590         vc->crc_len = sizeof(hash);
591         log_verify_failure(hdr, vc);
592         return EILSEQ;
593 }
594
595 /*
596  * Push IO verification to a separate thread
597  */
598 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
599 {
600         if (io_u->file)
601                 put_file_log(td, io_u->file);
602
603         pthread_mutex_lock(&td->io_u_lock);
604
605         if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
606                 td->cur_depth--;
607                 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
608         }
609         flist_del(&io_u->list);
610         flist_add_tail(&io_u->list, &td->verify_list);
611         io_u->flags |= IO_U_F_FREE_DEF;
612         pthread_mutex_unlock(&td->io_u_lock);
613
614         pthread_cond_signal(&td->verify_cond);
615         return 0;
616 }
617
618 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
619 {
620         static char zero_buf[1024];
621         unsigned int this_len, len;
622         int ret = 0;
623         void *p;
624
625         if (!td->o.trim_zero)
626                 return 0;
627
628         len = io_u->buflen;
629         p = io_u->buf;
630         do {
631                 this_len = sizeof(zero_buf);
632                 if (this_len > len)
633                         this_len = len;
634                 if (memcmp(p, zero_buf, this_len)) {
635                         ret = EILSEQ;
636                         break;
637                 }
638                 len -= this_len;
639                 p += this_len;
640         } while (len);
641
642         if (!ret)
643                 return 0;
644
645         log_err("trim: verify failed at file %s offset %llu, length %lu"
646                 ", block offset %lu\n",
647                         io_u->file->file_name, io_u->offset, io_u->buflen,
648                         (unsigned long) (p - io_u->buf));
649         return ret;
650 }
651
652 int verify_io_u(struct thread_data *td, struct io_u *io_u)
653 {
654         struct verify_header *hdr;
655         unsigned int header_size, hdr_inc, hdr_num = 0;
656         void *p;
657         int ret;
658
659         if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
660                 return 0;
661         if (io_u->flags & IO_U_F_TRIMMED) {
662                 ret = verify_trimmed_io_u(td, io_u);
663                 goto done;
664         }
665
666         hdr_inc = get_hdr_inc(td, io_u);
667
668         ret = 0;
669         for (p = io_u->buf; p < io_u->buf + io_u->buflen;
670              p += hdr_inc, hdr_num++) {
671                 struct vcont vc = {
672                         .io_u           = io_u,
673                         .hdr_num        = hdr_num,
674                         .td             = td,
675                 };
676
677                 if (ret && td->o.verify_fatal)
678                         break;
679
680                 header_size = __hdr_size(td->o.verify);
681                 if (td->o.verify_offset)
682                         memswp(p, p + td->o.verify_offset, header_size);
683                 hdr = p;
684
685                 if (hdr->fio_magic != FIO_HDR_MAGIC) {
686                         log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
687                                 hdr->fio_magic, FIO_HDR_MAGIC,
688                                 io_u->file->file_name,
689                                 io_u->offset + hdr_num * hdr->len, hdr->len);
690                         return EILSEQ;
691                 }
692
693                 switch (hdr->verify_type) {
694                 case VERIFY_MD5:
695                         ret = verify_io_u_md5(hdr, &vc);
696                         break;
697                 case VERIFY_CRC64:
698                         ret = verify_io_u_crc64(hdr, &vc);
699                         break;
700                 case VERIFY_CRC32C:
701                 case VERIFY_CRC32C_INTEL:
702                         ret = verify_io_u_crc32c(hdr, &vc);
703                         break;
704                 case VERIFY_CRC32:
705                         ret = verify_io_u_crc32(hdr, &vc);
706                         break;
707                 case VERIFY_CRC16:
708                         ret = verify_io_u_crc16(hdr, &vc);
709                         break;
710                 case VERIFY_CRC7:
711                         ret = verify_io_u_crc7(hdr, &vc);
712                         break;
713                 case VERIFY_SHA256:
714                         ret = verify_io_u_sha256(hdr, &vc);
715                         break;
716                 case VERIFY_SHA512:
717                         ret = verify_io_u_sha512(hdr, &vc);
718                         break;
719                 case VERIFY_META:
720                         ret = verify_io_u_meta(hdr, &vc);
721                         break;
722                 case VERIFY_SHA1:
723                         ret = verify_io_u_sha1(hdr, &vc);
724                         break;
725                 case VERIFY_PATTERN:
726                         ret = verify_io_u_pattern(hdr, &vc);
727                         break;
728                 default:
729                         log_err("Bad verify type %u\n", hdr->verify_type);
730                         ret = EINVAL;
731                 }
732         }
733
734 done:
735         if (ret && td->o.verify_fatal)
736                 td->terminate = 1;
737
738         return ret;
739 }
740
741 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
742                       struct io_u *io_u, unsigned int header_num)
743 {
744         struct vhdr_meta *vh = hdr_priv(hdr);
745
746         vh->thread = td->thread_number;
747
748         vh->time_sec = io_u->start_time.tv_sec;
749         vh->time_usec = io_u->start_time.tv_usec;
750
751         vh->numberio = td->io_issues[DDIR_WRITE];
752
753         vh->offset = io_u->offset + header_num * td->o.verify_interval;
754 }
755
756 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
757 {
758         struct vhdr_sha512 *vh = hdr_priv(hdr);
759         struct sha512_ctx sha512_ctx = {
760                 .buf = vh->sha512,
761         };
762
763         sha512_init(&sha512_ctx);
764         sha512_update(&sha512_ctx, p, len);
765 }
766
767 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
768 {
769         struct vhdr_sha256 *vh = hdr_priv(hdr);
770         struct sha256_ctx sha256_ctx = {
771                 .buf = vh->sha256,
772         };
773
774         sha256_init(&sha256_ctx);
775         sha256_update(&sha256_ctx, p, len);
776 }
777
778 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
779 {
780         struct vhdr_sha1 *vh = hdr_priv(hdr);
781         struct sha1_ctx sha1_ctx = {
782                 .H = vh->sha1,
783         };
784
785         sha1_init(&sha1_ctx);
786         sha1_update(&sha1_ctx, p, len);
787 }
788
789 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
790 {
791         struct vhdr_crc7 *vh = hdr_priv(hdr);
792
793         vh->crc7 = crc7(p, len);
794 }
795
796 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
797 {
798         struct vhdr_crc16 *vh = hdr_priv(hdr);
799
800         vh->crc16 = crc16(p, len);
801 }
802
803 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
804 {
805         struct vhdr_crc32 *vh = hdr_priv(hdr);
806
807         vh->crc32 = crc32(p, len);
808 }
809
810 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
811 {
812         struct vhdr_crc32 *vh = hdr_priv(hdr);
813
814         if (hdr->verify_type == VERIFY_CRC32C_INTEL)
815                 vh->crc32 = crc32c_intel(p, len);
816         else
817                 vh->crc32 = crc32c(p, len);
818 }
819
820 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
821 {
822         struct vhdr_crc64 *vh = hdr_priv(hdr);
823
824         vh->crc64 = crc64(p, len);
825 }
826
827 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
828 {
829         struct vhdr_md5 *vh = hdr_priv(hdr);
830         struct md5_ctx md5_ctx = {
831                 .hash = (uint32_t *) vh->md5_digest,
832         };
833
834         md5_init(&md5_ctx);
835         md5_update(&md5_ctx, p, len);
836 }
837
838 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
839                          struct verify_header *hdr, unsigned int header_num,
840                          unsigned int header_len)
841 {
842         unsigned int data_len;
843         void *data, *p;
844
845         p = (void *) hdr;
846
847         hdr->fio_magic = FIO_HDR_MAGIC;
848         hdr->len = header_len;
849         hdr->verify_type = td->o.verify;
850         hdr->rand_seed = io_u->rand_seed;
851         data_len = header_len - hdr_size(hdr);
852
853         data = p + hdr_size(hdr);
854         switch (td->o.verify) {
855         case VERIFY_MD5:
856                 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
857                                                 io_u, hdr->len);
858                 fill_md5(hdr, data, data_len);
859                 break;
860         case VERIFY_CRC64:
861                 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
862                                                 io_u, hdr->len);
863                 fill_crc64(hdr, data, data_len);
864                 break;
865         case VERIFY_CRC32C:
866         case VERIFY_CRC32C_INTEL:
867                 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
868                                                 io_u, hdr->len);
869                 fill_crc32c(hdr, data, data_len);
870                 break;
871         case VERIFY_CRC32:
872                 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
873                                                 io_u, hdr->len);
874                 fill_crc32(hdr, data, data_len);
875                 break;
876         case VERIFY_CRC16:
877                 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
878                                                 io_u, hdr->len);
879                 fill_crc16(hdr, data, data_len);
880                 break;
881         case VERIFY_CRC7:
882                 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
883                                                 io_u, hdr->len);
884                 fill_crc7(hdr, data, data_len);
885                 break;
886         case VERIFY_SHA256:
887                 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
888                                                 io_u, hdr->len);
889                 fill_sha256(hdr, data, data_len);
890                 break;
891         case VERIFY_SHA512:
892                 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
893                                                 io_u, hdr->len);
894                 fill_sha512(hdr, data, data_len);
895                 break;
896         case VERIFY_META:
897                 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
898                                                 io_u, hdr->len);
899                 fill_meta(hdr, td, io_u, header_num);
900                 break;
901         case VERIFY_SHA1:
902                 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
903                                                 io_u, hdr->len);
904                 fill_sha1(hdr, data, data_len);
905                 break;
906         case VERIFY_PATTERN:
907                 /* nothing to do here */
908                 break;
909         default:
910                 log_err("fio: bad verify type: %d\n", td->o.verify);
911                 assert(0);
912         }
913         if (td->o.verify_offset)
914                 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
915 }
916
917 /*
918  * fill body of io_u->buf with random data and add a header with the
919  * checksum of choice
920  */
921 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
922 {
923         if (td->o.verify == VERIFY_NULL)
924                 return;
925
926         fill_pattern_headers(td, io_u, 0, 0);
927 }
928
929 int get_next_verify(struct thread_data *td, struct io_u *io_u)
930 {
931         struct io_piece *ipo = NULL;
932
933         /*
934          * this io_u is from a requeue, we already filled the offsets
935          */
936         if (io_u->file)
937                 return 0;
938
939         if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
940                 struct rb_node *n = rb_first(&td->io_hist_tree);
941
942                 ipo = rb_entry(n, struct io_piece, rb_node);
943                 rb_erase(n, &td->io_hist_tree);
944                 assert(ipo->flags & IP_F_ONRB);
945                 ipo->flags &= ~IP_F_ONRB;
946         } else if (!flist_empty(&td->io_hist_list)) {
947                 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
948                 flist_del(&ipo->list);
949                 assert(ipo->flags & IP_F_ONLIST);
950                 ipo->flags &= ~IP_F_ONLIST;
951         }
952
953         if (ipo) {
954                 td->io_hist_len--;
955
956                 io_u->offset = ipo->offset;
957                 io_u->buflen = ipo->len;
958                 io_u->file = ipo->file;
959
960                 if (ipo->flags & IP_F_TRIMMED)
961                         io_u->flags |= IO_U_F_TRIMMED;
962
963                 if (!fio_file_open(io_u->file)) {
964                         int r = td_io_open_file(td, io_u->file);
965
966                         if (r) {
967                                 dprint(FD_VERIFY, "failed file %s open\n",
968                                                 io_u->file->file_name);
969                                 return 1;
970                         }
971                 }
972
973                 get_file(ipo->file);
974                 assert(fio_file_open(io_u->file));
975                 io_u->ddir = DDIR_READ;
976                 io_u->xfer_buf = io_u->buf;
977                 io_u->xfer_buflen = io_u->buflen;
978
979                 remove_trim_entry(td, ipo);
980                 free(ipo);
981                 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
982                 return 0;
983         }
984
985         dprint(FD_VERIFY, "get_next_verify: empty\n");
986         return 1;
987 }
988
989 static void *verify_async_thread(void *data)
990 {
991         struct thread_data *td = data;
992         struct io_u *io_u;
993         int ret = 0;
994
995         if (td->o.verify_cpumask_set &&
996             fio_setaffinity(td->pid, td->o.verify_cpumask)) {
997                 log_err("fio: failed setting verify thread affinity\n");
998                 goto done;
999         }
1000
1001         do {
1002                 FLIST_HEAD(list);
1003
1004                 read_barrier();
1005                 if (td->verify_thread_exit)
1006                         break;
1007
1008                 pthread_mutex_lock(&td->io_u_lock);
1009
1010                 while (flist_empty(&td->verify_list) &&
1011                        !td->verify_thread_exit) {
1012                         ret = pthread_cond_wait(&td->verify_cond,
1013                                                         &td->io_u_lock);
1014                         if (ret) {
1015                                 pthread_mutex_unlock(&td->io_u_lock);
1016                                 break;
1017                         }
1018                 }
1019
1020                 flist_splice_init(&td->verify_list, &list);
1021                 pthread_mutex_unlock(&td->io_u_lock);
1022
1023                 if (flist_empty(&list))
1024                         continue;
1025
1026                 while (!flist_empty(&list)) {
1027                         io_u = flist_entry(list.next, struct io_u, list);
1028                         flist_del_init(&io_u->list);
1029
1030                         ret = verify_io_u(td, io_u);
1031                         put_io_u(td, io_u);
1032                         if (!ret)
1033                                 continue;
1034                         if (td->o.continue_on_error & ERROR_TYPE_VERIFY &&
1035                             td_non_fatal_error(ret)) {
1036                                 update_error_count(td, ret);
1037                                 td_clear_error(td);
1038                                 ret = 0;
1039                         }
1040                 }
1041         } while (!ret);
1042
1043         if (ret) {
1044                 td_verror(td, ret, "async_verify");
1045                 if (td->o.verify_fatal)
1046                         td->terminate = 1;
1047         }
1048
1049 done:
1050         pthread_mutex_lock(&td->io_u_lock);
1051         td->nr_verify_threads--;
1052         pthread_mutex_unlock(&td->io_u_lock);
1053
1054         pthread_cond_signal(&td->free_cond);
1055         return NULL;
1056 }
1057
1058 int verify_async_init(struct thread_data *td)
1059 {
1060         int i, ret;
1061         pthread_attr_t attr;
1062
1063         pthread_attr_init(&attr);
1064         pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1065
1066         td->verify_thread_exit = 0;
1067
1068         td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1069         for (i = 0; i < td->o.verify_async; i++) {
1070                 ret = pthread_create(&td->verify_threads[i], &attr,
1071                                         verify_async_thread, td);
1072                 if (ret) {
1073                         log_err("fio: async verify creation failed: %s\n",
1074                                         strerror(ret));
1075                         break;
1076                 }
1077                 ret = pthread_detach(td->verify_threads[i]);
1078                 if (ret) {
1079                         log_err("fio: async verify thread detach failed: %s\n",
1080                                         strerror(ret));
1081                         break;
1082                 }
1083                 td->nr_verify_threads++;
1084         }
1085
1086         pthread_attr_destroy(&attr);
1087
1088         if (i != td->o.verify_async) {
1089                 log_err("fio: only %d verify threads started, exiting\n", i);
1090                 td->verify_thread_exit = 1;
1091                 write_barrier();
1092                 pthread_cond_broadcast(&td->verify_cond);
1093                 return 1;
1094         }
1095
1096         return 0;
1097 }
1098
1099 void verify_async_exit(struct thread_data *td)
1100 {
1101         td->verify_thread_exit = 1;
1102         write_barrier();
1103         pthread_cond_broadcast(&td->verify_cond);
1104
1105         pthread_mutex_lock(&td->io_u_lock);
1106
1107         while (td->nr_verify_threads)
1108                 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1109
1110         pthread_mutex_unlock(&td->io_u_lock);
1111         free(td->verify_threads);
1112         td->verify_threads = NULL;
1113 }