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