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