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