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