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