2 * IO verification helpers
15 #include "lib/hweight.h"
16 #include "lib/pattern.h"
19 #include "crc/crc64.h"
20 #include "crc/crc32.h"
21 #include "crc/crc32c.h"
22 #include "crc/crc16.h"
24 #include "crc/sha256.h"
25 #include "crc/sha512.h"
27 #include "crc/xxhash.h"
29 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
30 struct verify_header *hdr, unsigned int header_num,
31 unsigned int header_len);
32 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
34 static void __fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
37 void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
39 (void)cpy_pattern(td->o.buffer_pattern, td->o.buffer_pattern_bytes, p, len);
42 void __fill_buffer(struct thread_options *o, unsigned long seed, void *p,
45 __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
48 unsigned long fill_buffer(struct thread_data *td, void *p, unsigned int len)
50 struct frand_state *fs = &td->verify_state;
51 struct thread_options *o = &td->o;
53 return fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
56 void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
57 struct io_u *io_u, unsigned long seed, int use_seed)
59 struct thread_options *o = &td->o;
61 if (!o->verify_pattern_bytes) {
62 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
65 __fill_buffer(o, seed, p, len);
67 io_u->rand_seed = fill_buffer(td, p, len);
71 if (io_u->buf_filled_len >= len) {
72 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
73 o->verify_pattern_bytes, len);
77 (void)cpy_pattern(td->o.verify_pattern, td->o.verify_pattern_bytes, p, len);
78 io_u->buf_filled_len = len;
81 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
85 hdr_inc = io_u->buflen;
86 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
87 hdr_inc = td->o.verify_interval;
92 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
93 unsigned long seed, int use_seed)
95 unsigned int hdr_inc, header_num;
96 struct verify_header *hdr;
99 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
101 hdr_inc = get_hdr_inc(td, io_u);
103 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
105 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
110 static void memswp(void *buf1, void *buf2, unsigned int len)
114 assert(len <= sizeof(swap));
116 memcpy(&swap, buf1, len);
117 memcpy(buf1, buf2, len);
118 memcpy(buf2, &swap, len);
121 static void hexdump(void *buffer, int len)
123 unsigned char *p = buffer;
126 for (i = 0; i < len; i++)
127 log_err("%02x", p[i]);
132 * Prepare for separation of verify_header and checksum header
134 static inline unsigned int __hdr_size(int verify_type)
136 unsigned int len = 0;
138 switch (verify_type) {
145 len = sizeof(struct vhdr_md5);
148 len = sizeof(struct vhdr_crc64);
152 case VERIFY_CRC32C_INTEL:
153 len = sizeof(struct vhdr_crc32);
156 len = sizeof(struct vhdr_crc16);
159 len = sizeof(struct vhdr_crc7);
162 len = sizeof(struct vhdr_sha256);
165 len = sizeof(struct vhdr_sha512);
168 len = sizeof(struct vhdr_xxhash);
171 len = sizeof(struct vhdr_meta);
174 len = sizeof(struct vhdr_sha1);
176 case VERIFY_PATTERN_NO_HDR:
179 log_err("fio: unknown verify header!\n");
183 return len + sizeof(struct verify_header);
186 static inline unsigned int hdr_size(struct thread_data *td,
187 struct verify_header *hdr)
189 if (td->o.verify == VERIFY_PATTERN_NO_HDR)
192 return __hdr_size(hdr->verify_type);
195 static void *hdr_priv(struct verify_header *hdr)
199 return priv + sizeof(struct verify_header);
203 * Verify container, pass info to verify handlers and allow them to
204 * pass info back in case of error
211 unsigned int hdr_num;
212 struct thread_data *td;
215 * Output, only valid in case of error
220 unsigned int crc_len;
223 #define DUMP_BUF_SZ 255
224 static int dump_buf_warned;
226 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
227 const char *type, struct fio_file *f)
229 char *ptr, fname[DUMP_BUF_SZ];
230 size_t buf_left = DUMP_BUF_SZ;
233 ptr = strdup(f->file_name);
235 memset(fname, 0, sizeof(fname));
237 sprintf(fname, "%s%s", aux_path, FIO_OS_PATH_SEPARATOR);
239 strncpy(fname + strlen(fname), basename(ptr), buf_left - 1);
241 buf_left -= strlen(fname);
243 if (!dump_buf_warned) {
244 log_err("fio: verify failure dump buffer too small\n");
251 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
253 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
255 perror("open verify buf file");
260 ret = write(fd, buf, len);
264 perror("write verify buf file");
272 log_err(" %s data dumped as %s\n", type, fname);
277 * Dump the contents of the read block and re-generate the correct data
280 static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
282 struct thread_data *td = vc->td;
283 struct io_u *io_u = vc->io_u;
284 unsigned long hdr_offset;
288 if (!td->o.verify_dump)
292 * Dump the contents we just read off disk
294 hdr_offset = vc->hdr_num * hdr->len;
296 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
297 "received", vc->io_u->file);
300 * Allocate a new buf and re-generate the original data
302 buf = malloc(io_u->buflen);
305 dummy.rand_seed = hdr->rand_seed;
306 dummy.buf_filled_len = 0;
307 dummy.buflen = io_u->buflen;
309 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
311 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
312 "expected", vc->io_u->file);
316 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
318 struct thread_data *td = vc->td;
319 struct verify_header shdr;
321 if (td->o.verify == VERIFY_PATTERN_NO_HDR) {
322 __fill_hdr(&shdr, td->o.verify, vc->io_u->buflen, 0);
326 __dump_verify_buffers(hdr, vc);
329 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
331 unsigned long long offset;
333 offset = vc->io_u->offset;
334 offset += vc->hdr_num * hdr->len;
335 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
336 vc->name, vc->io_u->file->file_name, offset, hdr->len);
338 if (vc->good_crc && vc->bad_crc) {
339 log_err(" Expected CRC: ");
340 hexdump(vc->good_crc, vc->crc_len);
341 log_err(" Received CRC: ");
342 hexdump(vc->bad_crc, vc->crc_len);
345 dump_verify_buffers(hdr, vc);
349 * Return data area 'header_num'
351 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
353 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr);
356 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
358 struct thread_data *td = vc->td;
359 struct io_u *io_u = vc->io_u;
361 unsigned int header_size = __hdr_size(td->o.verify);
362 unsigned int len, mod, i, pattern_size;
365 pattern = td->o.verify_pattern;
366 pattern_size = td->o.verify_pattern_bytes;
367 if (pattern_size <= 1)
368 pattern_size = MAX_PATTERN_SIZE;
369 buf = (void *) hdr + header_size;
370 len = get_hdr_inc(td, io_u) - header_size;
371 mod = header_size % pattern_size;
373 rc = cmp_pattern(pattern, pattern_size, mod, buf, len);
377 /* Slow path, compare each byte */
378 for (i = 0; i < len; i++) {
379 if (buf[i] != pattern[mod]) {
382 bits = hweight8(buf[i] ^ pattern[mod]);
383 log_err("fio: got pattern '%02x', wanted '%02x'. Bad bits %d\n",
384 (unsigned char)buf[i],
385 (unsigned char)pattern[mod],
387 log_err("fio: bad pattern block offset %u\n", i);
388 dump_verify_buffers(hdr, vc);
392 if (mod == td->o.verify_pattern_bytes)
396 /* Unreachable line */
401 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
403 struct thread_data *td = vc->td;
404 struct vhdr_meta *vh = hdr_priv(hdr);
405 struct io_u *io_u = vc->io_u;
408 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
410 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
413 if (td->o.verify_pattern_bytes)
414 ret |= verify_io_u_pattern(hdr, vc);
417 * For read-only workloads, the program cannot be certain of the
418 * last numberio written to a block. Checking of numberio will be
419 * done only for workloads that write data. For verify_only,
420 * numberio will be checked in the last iteration when the correct
421 * state of numberio, that would have been written to each block
422 * in a previous run of fio, has been reached.
424 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
426 if (!td->o.verify_only || td->o.loops == 0)
427 if (vh->numberio != io_u->numberio)
434 log_verify_failure(hdr, vc);
438 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
440 void *p = io_u_verify_off(hdr, vc);
441 struct vhdr_xxhash *vh = hdr_priv(hdr);
445 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
447 state = XXH32_init(1);
448 XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr));
449 hash = XXH32_digest(state);
451 if (vh->hash == hash)
455 vc->good_crc = &vh->hash;
457 vc->crc_len = sizeof(hash);
458 log_verify_failure(hdr, vc);
462 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
464 void *p = io_u_verify_off(hdr, vc);
465 struct vhdr_sha512 *vh = hdr_priv(hdr);
467 struct fio_sha512_ctx sha512_ctx = {
471 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
473 fio_sha512_init(&sha512_ctx);
474 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr));
476 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
480 vc->good_crc = vh->sha512;
481 vc->bad_crc = sha512_ctx.buf;
482 vc->crc_len = sizeof(vh->sha512);
483 log_verify_failure(hdr, vc);
487 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
489 void *p = io_u_verify_off(hdr, vc);
490 struct vhdr_sha256 *vh = hdr_priv(hdr);
492 struct fio_sha256_ctx sha256_ctx = {
496 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
498 fio_sha256_init(&sha256_ctx);
499 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr));
500 fio_sha256_final(&sha256_ctx);
502 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
506 vc->good_crc = vh->sha256;
507 vc->bad_crc = sha256_ctx.buf;
508 vc->crc_len = sizeof(vh->sha256);
509 log_verify_failure(hdr, vc);
513 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
515 void *p = io_u_verify_off(hdr, vc);
516 struct vhdr_sha1 *vh = hdr_priv(hdr);
518 struct fio_sha1_ctx sha1_ctx = {
522 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
524 fio_sha1_init(&sha1_ctx);
525 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr));
526 fio_sha1_final(&sha1_ctx);
528 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
532 vc->good_crc = vh->sha1;
533 vc->bad_crc = sha1_ctx.H;
534 vc->crc_len = sizeof(vh->sha1);
535 log_verify_failure(hdr, vc);
539 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
541 void *p = io_u_verify_off(hdr, vc);
542 struct vhdr_crc7 *vh = hdr_priv(hdr);
545 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
547 c = fio_crc7(p, hdr->len - hdr_size(vc->td, hdr));
553 vc->good_crc = &vh->crc7;
556 log_verify_failure(hdr, vc);
560 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
562 void *p = io_u_verify_off(hdr, vc);
563 struct vhdr_crc16 *vh = hdr_priv(hdr);
566 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
568 c = fio_crc16(p, hdr->len - hdr_size(vc->td, hdr));
574 vc->good_crc = &vh->crc16;
577 log_verify_failure(hdr, vc);
581 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
583 void *p = io_u_verify_off(hdr, vc);
584 struct vhdr_crc64 *vh = hdr_priv(hdr);
585 unsigned long long c;
587 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
589 c = fio_crc64(p, hdr->len - hdr_size(vc->td, hdr));
595 vc->good_crc = &vh->crc64;
598 log_verify_failure(hdr, vc);
602 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
604 void *p = io_u_verify_off(hdr, vc);
605 struct vhdr_crc32 *vh = hdr_priv(hdr);
608 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
610 c = fio_crc32(p, hdr->len - hdr_size(vc->td, hdr));
616 vc->good_crc = &vh->crc32;
619 log_verify_failure(hdr, vc);
623 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
625 void *p = io_u_verify_off(hdr, vc);
626 struct vhdr_crc32 *vh = hdr_priv(hdr);
629 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
631 c = fio_crc32c(p, hdr->len - hdr_size(vc->td, hdr));
637 vc->good_crc = &vh->crc32;
640 log_verify_failure(hdr, vc);
644 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
646 void *p = io_u_verify_off(hdr, vc);
647 struct vhdr_md5 *vh = hdr_priv(hdr);
648 uint32_t hash[MD5_HASH_WORDS];
649 struct fio_md5_ctx md5_ctx = {
653 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
655 fio_md5_init(&md5_ctx);
656 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr));
657 fio_md5_final(&md5_ctx);
659 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
663 vc->good_crc = vh->md5_digest;
664 vc->bad_crc = md5_ctx.hash;
665 vc->crc_len = sizeof(hash);
666 log_verify_failure(hdr, vc);
671 * Push IO verification to a separate thread
673 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
675 struct io_u *io_u = *io_u_ptr;
677 pthread_mutex_lock(&td->io_u_lock);
680 put_file_log(td, io_u->file);
682 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
684 io_u_clear(io_u, IO_U_F_IN_CUR_DEPTH);
686 flist_add_tail(&io_u->verify_list, &td->verify_list);
688 pthread_mutex_unlock(&td->io_u_lock);
690 pthread_cond_signal(&td->verify_cond);
694 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
696 static char zero_buf[1024];
697 unsigned int this_len, len;
701 if (!td->o.trim_zero)
707 this_len = sizeof(zero_buf);
710 if (memcmp(p, zero_buf, this_len)) {
721 log_err("trim: verify failed at file %s offset %llu, length %lu"
722 ", block offset %lu\n",
723 io_u->file->file_name, io_u->offset, io_u->buflen,
724 (unsigned long) (p - io_u->buf));
728 static int verify_header(struct io_u *io_u, struct verify_header *hdr,
729 unsigned int hdr_num, unsigned int hdr_len)
734 if (hdr->magic != FIO_HDR_MAGIC) {
735 log_err("verify: bad magic header %x, wanted %x",
736 hdr->magic, FIO_HDR_MAGIC);
739 if (hdr->len != hdr_len) {
740 log_err("verify: bad header length %u, wanted %u",
744 if (hdr->rand_seed != io_u->rand_seed) {
745 log_err("verify: bad header rand_seed %"PRIu64
747 hdr->rand_seed, io_u->rand_seed);
751 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
752 if (crc != hdr->crc32) {
753 log_err("verify: bad header crc %x, calculated %x",
760 log_err(" at file %s offset %llu, length %u\n",
761 io_u->file->file_name,
762 io_u->offset + hdr_num * hdr_len, hdr_len);
766 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
768 struct verify_header *hdr;
769 struct io_u *io_u = *io_u_ptr;
770 unsigned int header_size, hdr_inc, hdr_num = 0;
774 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
777 * If the IO engine is faking IO (like null), then just pretend
778 * we verified everything.
780 if (td->io_ops->flags & FIO_FAKEIO)
783 if (io_u->flags & IO_U_F_TRIMMED) {
784 ret = verify_trimmed_io_u(td, io_u);
788 hdr_inc = get_hdr_inc(td, io_u);
791 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
792 p += hdr_inc, hdr_num++) {
798 unsigned int verify_type;
800 if (ret && td->o.verify_fatal)
803 header_size = __hdr_size(td->o.verify);
804 if (td->o.verify_offset)
805 memswp(p, p + td->o.verify_offset, header_size);
809 * Make rand_seed check pass when have verifysort or
812 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
813 io_u->rand_seed = hdr->rand_seed;
815 if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
816 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
821 if (td->o.verify != VERIFY_NONE)
822 verify_type = td->o.verify;
824 verify_type = hdr->verify_type;
826 switch (verify_type) {
828 ret = verify_io_u_md5(hdr, &vc);
831 ret = verify_io_u_crc64(hdr, &vc);
834 case VERIFY_CRC32C_INTEL:
835 ret = verify_io_u_crc32c(hdr, &vc);
838 ret = verify_io_u_crc32(hdr, &vc);
841 ret = verify_io_u_crc16(hdr, &vc);
844 ret = verify_io_u_crc7(hdr, &vc);
847 ret = verify_io_u_sha256(hdr, &vc);
850 ret = verify_io_u_sha512(hdr, &vc);
853 ret = verify_io_u_xxhash(hdr, &vc);
856 ret = verify_io_u_meta(hdr, &vc);
859 ret = verify_io_u_sha1(hdr, &vc);
862 case VERIFY_PATTERN_NO_HDR:
863 ret = verify_io_u_pattern(hdr, &vc);
866 log_err("Bad verify type %u\n", hdr->verify_type);
870 if (ret && verify_type != hdr->verify_type)
871 log_err("fio: verify type mismatch (%u media, %u given)\n",
872 hdr->verify_type, verify_type);
876 if (ret && td->o.verify_fatal)
877 fio_mark_td_terminate(td);
882 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
883 struct io_u *io_u, unsigned int header_num)
885 struct vhdr_meta *vh = hdr_priv(hdr);
887 vh->thread = td->thread_number;
889 vh->time_sec = io_u->start_time.tv_sec;
890 vh->time_usec = io_u->start_time.tv_usec;
892 vh->numberio = io_u->numberio;
894 vh->offset = io_u->offset + header_num * td->o.verify_interval;
897 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
899 struct vhdr_xxhash *vh = hdr_priv(hdr);
902 state = XXH32_init(1);
903 XXH32_update(state, p, len);
904 vh->hash = XXH32_digest(state);
907 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
909 struct vhdr_sha512 *vh = hdr_priv(hdr);
910 struct fio_sha512_ctx sha512_ctx = {
914 fio_sha512_init(&sha512_ctx);
915 fio_sha512_update(&sha512_ctx, p, len);
918 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
920 struct vhdr_sha256 *vh = hdr_priv(hdr);
921 struct fio_sha256_ctx sha256_ctx = {
925 fio_sha256_init(&sha256_ctx);
926 fio_sha256_update(&sha256_ctx, p, len);
927 fio_sha256_final(&sha256_ctx);
930 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
932 struct vhdr_sha1 *vh = hdr_priv(hdr);
933 struct fio_sha1_ctx sha1_ctx = {
937 fio_sha1_init(&sha1_ctx);
938 fio_sha1_update(&sha1_ctx, p, len);
939 fio_sha1_final(&sha1_ctx);
942 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
944 struct vhdr_crc7 *vh = hdr_priv(hdr);
946 vh->crc7 = fio_crc7(p, len);
949 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
951 struct vhdr_crc16 *vh = hdr_priv(hdr);
953 vh->crc16 = fio_crc16(p, len);
956 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
958 struct vhdr_crc32 *vh = hdr_priv(hdr);
960 vh->crc32 = fio_crc32(p, len);
963 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
965 struct vhdr_crc32 *vh = hdr_priv(hdr);
967 vh->crc32 = fio_crc32c(p, len);
970 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
972 struct vhdr_crc64 *vh = hdr_priv(hdr);
974 vh->crc64 = fio_crc64(p, len);
977 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
979 struct vhdr_md5 *vh = hdr_priv(hdr);
980 struct fio_md5_ctx md5_ctx = {
981 .hash = (uint32_t *) vh->md5_digest,
984 fio_md5_init(&md5_ctx);
985 fio_md5_update(&md5_ctx, p, len);
986 fio_md5_final(&md5_ctx);
989 static void __fill_hdr(struct verify_header *hdr, int verify_type,
990 uint32_t len, uint64_t rand_seed)
994 hdr->magic = FIO_HDR_MAGIC;
995 hdr->verify_type = verify_type;
997 hdr->rand_seed = rand_seed;
998 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
1002 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
1005 if (verify_type != VERIFY_PATTERN_NO_HDR)
1006 __fill_hdr(hdr, verify_type, len, rand_seed);
1009 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
1010 struct verify_header *hdr, unsigned int header_num,
1011 unsigned int header_len)
1013 unsigned int data_len;
1018 fill_hdr(hdr, td->o.verify, header_len, io_u->rand_seed);
1020 data_len = header_len - hdr_size(td, hdr);
1022 data = p + hdr_size(td, hdr);
1023 switch (td->o.verify) {
1025 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1027 fill_md5(hdr, data, data_len);
1030 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1032 fill_crc64(hdr, data, data_len);
1035 case VERIFY_CRC32C_INTEL:
1036 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1038 fill_crc32c(hdr, data, data_len);
1041 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1043 fill_crc32(hdr, data, data_len);
1046 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1048 fill_crc16(hdr, data, data_len);
1051 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1053 fill_crc7(hdr, data, data_len);
1056 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1058 fill_sha256(hdr, data, data_len);
1061 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1063 fill_sha512(hdr, data, data_len);
1066 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1068 fill_xxhash(hdr, data, data_len);
1071 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1073 fill_meta(hdr, td, io_u, header_num);
1076 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1078 fill_sha1(hdr, data, data_len);
1080 case VERIFY_PATTERN:
1081 case VERIFY_PATTERN_NO_HDR:
1082 /* nothing to do here */
1085 log_err("fio: bad verify type: %d\n", td->o.verify);
1089 if (td->o.verify_offset && hdr_size(td, hdr))
1090 memswp(p, p + td->o.verify_offset, hdr_size(td, hdr));
1094 * fill body of io_u->buf with random data and add a header with the
1095 * checksum of choice
1097 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1099 if (td->o.verify == VERIFY_NULL)
1102 io_u->numberio = td->io_issues[io_u->ddir];
1104 fill_pattern_headers(td, io_u, 0, 0);
1107 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1109 struct io_piece *ipo = NULL;
1112 * this io_u is from a requeue, we already filled the offsets
1117 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1118 struct rb_node *n = rb_first(&td->io_hist_tree);
1120 ipo = rb_entry(n, struct io_piece, rb_node);
1123 * Ensure that the associated IO has completed
1126 if (ipo->flags & IP_F_IN_FLIGHT)
1129 rb_erase(n, &td->io_hist_tree);
1130 assert(ipo->flags & IP_F_ONRB);
1131 ipo->flags &= ~IP_F_ONRB;
1132 } else if (!flist_empty(&td->io_hist_list)) {
1133 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1136 * Ensure that the associated IO has completed
1139 if (ipo->flags & IP_F_IN_FLIGHT)
1142 flist_del(&ipo->list);
1143 assert(ipo->flags & IP_F_ONLIST);
1144 ipo->flags &= ~IP_F_ONLIST;
1150 io_u->offset = ipo->offset;
1151 io_u->buflen = ipo->len;
1152 io_u->numberio = ipo->numberio;
1153 io_u->file = ipo->file;
1154 io_u_set(io_u, IO_U_F_VER_LIST);
1156 if (ipo->flags & IP_F_TRIMMED)
1157 io_u_set(io_u, IO_U_F_TRIMMED);
1159 if (!fio_file_open(io_u->file)) {
1160 int r = td_io_open_file(td, io_u->file);
1163 dprint(FD_VERIFY, "failed file %s open\n",
1164 io_u->file->file_name);
1169 get_file(ipo->file);
1170 assert(fio_file_open(io_u->file));
1171 io_u->ddir = DDIR_READ;
1172 io_u->xfer_buf = io_u->buf;
1173 io_u->xfer_buflen = io_u->buflen;
1175 remove_trim_entry(td, ipo);
1177 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1179 if (!td->o.verify_pattern_bytes) {
1180 io_u->rand_seed = __rand(&td->verify_state);
1181 if (sizeof(int) != sizeof(long *))
1182 io_u->rand_seed *= __rand(&td->verify_state);
1188 dprint(FD_VERIFY, "get_next_verify: empty\n");
1192 void fio_verify_init(struct thread_data *td)
1194 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1195 td->o.verify == VERIFY_CRC32C) {
1196 crc32c_intel_probe();
1200 static void *verify_async_thread(void *data)
1202 struct thread_data *td = data;
1206 if (fio_option_is_set(&td->o, verify_cpumask) &&
1207 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1208 log_err("fio: failed setting verify thread affinity\n");
1216 if (td->verify_thread_exit)
1219 pthread_mutex_lock(&td->io_u_lock);
1221 while (flist_empty(&td->verify_list) &&
1222 !td->verify_thread_exit) {
1223 ret = pthread_cond_wait(&td->verify_cond,
1226 pthread_mutex_unlock(&td->io_u_lock);
1231 flist_splice_init(&td->verify_list, &list);
1232 pthread_mutex_unlock(&td->io_u_lock);
1234 if (flist_empty(&list))
1237 while (!flist_empty(&list)) {
1238 io_u = flist_first_entry(&list, struct io_u, verify_list);
1239 flist_del_init(&io_u->verify_list);
1241 io_u_set(io_u, IO_U_F_NO_FILE_PUT);
1242 ret = verify_io_u(td, &io_u);
1247 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1248 update_error_count(td, ret);
1256 td_verror(td, ret, "async_verify");
1257 if (td->o.verify_fatal)
1258 fio_mark_td_terminate(td);
1262 pthread_mutex_lock(&td->io_u_lock);
1263 td->nr_verify_threads--;
1264 pthread_mutex_unlock(&td->io_u_lock);
1266 pthread_cond_signal(&td->free_cond);
1270 int verify_async_init(struct thread_data *td)
1273 pthread_attr_t attr;
1275 pthread_attr_init(&attr);
1276 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1278 td->verify_thread_exit = 0;
1280 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1281 for (i = 0; i < td->o.verify_async; i++) {
1282 ret = pthread_create(&td->verify_threads[i], &attr,
1283 verify_async_thread, td);
1285 log_err("fio: async verify creation failed: %s\n",
1289 ret = pthread_detach(td->verify_threads[i]);
1291 log_err("fio: async verify thread detach failed: %s\n",
1295 td->nr_verify_threads++;
1298 pthread_attr_destroy(&attr);
1300 if (i != td->o.verify_async) {
1301 log_err("fio: only %d verify threads started, exiting\n", i);
1302 td->verify_thread_exit = 1;
1304 pthread_cond_broadcast(&td->verify_cond);
1311 void verify_async_exit(struct thread_data *td)
1313 td->verify_thread_exit = 1;
1315 pthread_cond_broadcast(&td->verify_cond);
1317 pthread_mutex_lock(&td->io_u_lock);
1319 while (td->nr_verify_threads)
1320 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1322 pthread_mutex_unlock(&td->io_u_lock);
1323 free(td->verify_threads);
1324 td->verify_threads = NULL;
1327 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1329 struct all_io_list *rep;
1330 struct thread_data *td;
1335 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1338 * Calculate reply space needed. We need one 'io_state' per thread,
1339 * and the size will vary depending on depth.
1343 for_each_td(td, i) {
1344 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1347 td->flags |= TD_F_VSTATE_SAVED;
1348 depth += td->o.iodepth;
1356 *sz += nr * sizeof(struct thread_io_list);
1357 *sz += depth * sizeof(uint64_t);
1360 rep->threads = cpu_to_le64((uint64_t) nr);
1362 next = &rep->state[0];
1363 for_each_td(td, i) {
1364 struct thread_io_list *s = next;
1367 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1370 if (td->last_write_comp) {
1373 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1374 comps = td->io_blocks[DDIR_WRITE];
1376 comps = td->o.iodepth;
1378 k = td->last_write_idx - 1;
1379 for (j = 0; j < comps; j++) {
1381 k = td->o.iodepth - 1;
1382 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1388 s->no_comps = cpu_to_le64((uint64_t) comps);
1389 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1390 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1391 s->index = cpu_to_le64((uint64_t) i);
1392 if (td->random_state.use64) {
1393 s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
1394 s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
1395 s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
1396 s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
1397 s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
1398 s->rand.state64.s[5] = 0;
1399 s->rand.use64 = cpu_to_le64((uint64_t)1);
1401 s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
1402 s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
1403 s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
1404 s->rand.state32.s[3] = 0;
1407 s->name[sizeof(s->name) - 1] = '\0';
1408 strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
1409 next = io_list_next(s);
1415 static int open_state_file(const char *name, const char *prefix, int num,
1423 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1427 verify_state_gen_name(out, sizeof(out), name, prefix, num);
1429 fd = open(out, flags, 0644);
1431 perror("fio: open state file");
1438 static int write_thread_list_state(struct thread_io_list *s,
1441 struct verify_state_hdr hdr;
1446 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1450 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1452 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1453 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1454 hdr.crc = cpu_to_le64(crc);
1455 ret = write(fd, &hdr, sizeof(hdr));
1456 if (ret != sizeof(hdr))
1459 ret = write(fd, s, thread_io_list_sz(s));
1460 if (ret != thread_io_list_sz(s)) {
1463 perror("fio: write state file");
1464 log_err("fio: failed to write state file\n");
1473 void __verify_save_state(struct all_io_list *state, const char *prefix)
1475 struct thread_io_list *s = &state->state[0];
1478 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1479 write_thread_list_state(s, prefix);
1480 s = io_list_next(s);
1484 void verify_save_state(int mask)
1486 struct all_io_list *state;
1489 state = get_all_io_list(mask, &sz);
1491 char prefix[PATH_MAX];
1494 sprintf(prefix, "%s%slocal", aux_path, FIO_OS_PATH_SEPARATOR);
1496 strcpy(prefix, "local");
1498 __verify_save_state(state, prefix);
1503 void verify_free_state(struct thread_data *td)
1509 static struct thread_io_list *convert_v1_list(struct thread_io_list_v1 *s)
1511 struct thread_io_list *til;
1514 til = malloc(__thread_io_list_sz(s->no_comps));
1515 til->no_comps = s->no_comps;
1516 til->depth = s->depth;
1517 til->numberio = s->numberio;
1518 til->index = s->index;
1519 memcpy(til->name, s->name, sizeof(til->name));
1521 til->rand.use64 = 0;
1522 for (i = 0; i < 4; i++)
1523 til->rand.state32.s[i] = s->rand.s[i];
1525 for (i = 0; i < s->no_comps; i++)
1526 til->offsets[i] = s->offsets[i];
1531 void verify_convert_assign_state(struct thread_data *td, void *p, int version)
1533 struct thread_io_list *til;
1537 struct thread_io_list_v1 *s = p;
1539 s->no_comps = le64_to_cpu(s->no_comps);
1540 s->depth = le64_to_cpu(s->depth);
1541 s->numberio = le64_to_cpu(s->numberio);
1542 for (i = 0; i < 4; i++)
1543 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1544 for (i = 0; i < s->no_comps; i++)
1545 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1547 til = convert_v1_list(s);
1550 struct thread_io_list *s = p;
1552 s->no_comps = le64_to_cpu(s->no_comps);
1553 s->depth = le64_to_cpu(s->depth);
1554 s->numberio = le64_to_cpu(s->numberio);
1555 s->rand.use64 = le64_to_cpu(s->rand.use64);
1557 if (s->rand.use64) {
1558 for (i = 0; i < 6; i++)
1559 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
1561 for (i = 0; i < 4; i++)
1562 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1564 for (i = 0; i < s->no_comps; i++)
1565 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1573 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s,
1578 hdr->version = le64_to_cpu(hdr->version);
1579 hdr->size = le64_to_cpu(hdr->size);
1580 hdr->crc = le64_to_cpu(hdr->crc);
1582 if (hdr->version != VSTATE_HDR_VERSION ||
1583 hdr->version != VSTATE_HDR_VERSION_V1)
1586 crc = fio_crc32c((void *)s, hdr->size);
1587 if (crc != hdr->crc)
1590 *version = hdr->version;
1594 int verify_load_state(struct thread_data *td, const char *prefix)
1596 struct verify_state_hdr hdr;
1602 if (!td->o.verify_state)
1605 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1609 ret = read(fd, &hdr, sizeof(hdr));
1610 if (ret != sizeof(hdr)) {
1612 td_verror(td, errno, "read verify state hdr");
1613 log_err("fio: failed reading verify state header\n");
1617 hdr.version = le64_to_cpu(hdr.version);
1618 hdr.size = le64_to_cpu(hdr.size);
1619 hdr.crc = le64_to_cpu(hdr.crc);
1621 if (hdr.version != VSTATE_HDR_VERSION &&
1622 hdr.version != VSTATE_HDR_VERSION_V1) {
1623 log_err("fio: bad version in verify state header\n");
1627 s = malloc(hdr.size);
1628 ret = read(fd, s, hdr.size);
1629 if (ret != hdr.size) {
1631 td_verror(td, errno, "read verify state");
1632 log_err("fio: failed reading verity state\n");
1636 crc = fio_crc32c(s, hdr.size);
1637 if (crc != hdr.crc) {
1638 log_err("fio: verify state is corrupt\n");
1644 verify_convert_assign_state(td, s, hdr.version);
1654 * Use the loaded verify state to know when to stop doing verification
1656 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1658 struct thread_io_list *s = td->vstate;
1665 * If we're not into the window of issues - depth yet, continue. If
1666 * issue is shorter than depth, do check.
1668 if ((td->io_blocks[DDIR_READ] < s->depth ||
1669 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1670 s->numberio > s->depth)
1674 * We're in the window of having to check if this io was
1675 * completed or not. If the IO was seen as completed, then
1678 for (i = 0; i < s->no_comps; i++)
1679 if (io_u->offset == s->offsets[i])
1683 * Not found, we have to stop