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 /* Skip if we were here and we do not need to patch pattern
73 if (!td->o.verify_fmt_sz && io_u->buf_filled_len >= len) {
74 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
75 o->verify_pattern_bytes, len);
79 (void)paste_format(td->o.verify_pattern, td->o.verify_pattern_bytes,
80 td->o.verify_fmt, td->o.verify_fmt_sz,
82 io_u->buf_filled_len = len;
85 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
89 hdr_inc = io_u->buflen;
90 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
91 hdr_inc = td->o.verify_interval;
96 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
97 unsigned long seed, int use_seed)
99 unsigned int hdr_inc, header_num;
100 struct verify_header *hdr;
103 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
105 hdr_inc = get_hdr_inc(td, io_u);
107 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
109 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
114 static void memswp(void *buf1, void *buf2, unsigned int len)
118 assert(len <= sizeof(swap));
120 memcpy(&swap, buf1, len);
121 memcpy(buf1, buf2, len);
122 memcpy(buf2, &swap, len);
125 static void hexdump(void *buffer, int len)
127 unsigned char *p = buffer;
130 for (i = 0; i < len; i++)
131 log_err("%02x", p[i]);
136 * Prepare for separation of verify_header and checksum header
138 static inline unsigned int __hdr_size(int verify_type)
140 unsigned int len = 0;
142 switch (verify_type) {
149 len = sizeof(struct vhdr_md5);
152 len = sizeof(struct vhdr_crc64);
156 case VERIFY_CRC32C_INTEL:
157 len = sizeof(struct vhdr_crc32);
160 len = sizeof(struct vhdr_crc16);
163 len = sizeof(struct vhdr_crc7);
166 len = sizeof(struct vhdr_sha256);
169 len = sizeof(struct vhdr_sha512);
172 len = sizeof(struct vhdr_xxhash);
175 len = sizeof(struct vhdr_meta);
178 len = sizeof(struct vhdr_sha1);
180 case VERIFY_PATTERN_NO_HDR:
183 log_err("fio: unknown verify header!\n");
187 return len + sizeof(struct verify_header);
190 static inline unsigned int hdr_size(struct thread_data *td,
191 struct verify_header *hdr)
193 if (td->o.verify == VERIFY_PATTERN_NO_HDR)
196 return __hdr_size(hdr->verify_type);
199 static void *hdr_priv(struct verify_header *hdr)
203 return priv + sizeof(struct verify_header);
207 * Verify container, pass info to verify handlers and allow them to
208 * pass info back in case of error
215 unsigned int hdr_num;
216 struct thread_data *td;
219 * Output, only valid in case of error
224 unsigned int crc_len;
227 #define DUMP_BUF_SZ 255
228 static int dump_buf_warned;
230 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
231 const char *type, struct fio_file *f)
233 char *ptr, fname[DUMP_BUF_SZ];
234 size_t buf_left = DUMP_BUF_SZ;
237 ptr = strdup(f->file_name);
239 memset(fname, 0, sizeof(fname));
241 sprintf(fname, "%s%s", aux_path, FIO_OS_PATH_SEPARATOR);
243 strncpy(fname + strlen(fname), basename(ptr), buf_left - 1);
245 buf_left -= strlen(fname);
247 if (!dump_buf_warned) {
248 log_err("fio: verify failure dump buffer too small\n");
255 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
257 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
259 perror("open verify buf file");
264 ret = write(fd, buf, len);
268 perror("write verify buf file");
276 log_err(" %s data dumped as %s\n", type, fname);
281 * Dump the contents of the read block and re-generate the correct data
284 static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
286 struct thread_data *td = vc->td;
287 struct io_u *io_u = vc->io_u;
288 unsigned long hdr_offset;
292 if (!td->o.verify_dump)
296 * Dump the contents we just read off disk
298 hdr_offset = vc->hdr_num * hdr->len;
300 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
301 "received", vc->io_u->file);
304 * Allocate a new buf and re-generate the original data
306 buf = malloc(io_u->buflen);
309 dummy.rand_seed = hdr->rand_seed;
310 dummy.buf_filled_len = 0;
311 dummy.buflen = io_u->buflen;
313 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
315 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
316 "expected", vc->io_u->file);
320 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
322 struct thread_data *td = vc->td;
323 struct verify_header shdr;
325 if (td->o.verify == VERIFY_PATTERN_NO_HDR) {
326 __fill_hdr(&shdr, td->o.verify, vc->io_u->buflen, 0);
330 __dump_verify_buffers(hdr, vc);
333 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
335 unsigned long long offset;
337 offset = vc->io_u->offset;
338 offset += vc->hdr_num * hdr->len;
339 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
340 vc->name, vc->io_u->file->file_name, offset, hdr->len);
342 if (vc->good_crc && vc->bad_crc) {
343 log_err(" Expected CRC: ");
344 hexdump(vc->good_crc, vc->crc_len);
345 log_err(" Received CRC: ");
346 hexdump(vc->bad_crc, vc->crc_len);
349 dump_verify_buffers(hdr, vc);
353 * Return data area 'header_num'
355 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
357 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr);
360 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
362 struct thread_data *td = vc->td;
363 struct io_u *io_u = vc->io_u;
365 unsigned int header_size = __hdr_size(td->o.verify);
366 unsigned int len, mod, i, pattern_size;
369 pattern = td->o.verify_pattern;
370 pattern_size = td->o.verify_pattern_bytes;
371 assert(pattern_size != 0);
373 (void)paste_format_inplace(pattern, pattern_size,
374 td->o.verify_fmt, td->o.verify_fmt_sz, io_u);
376 buf = (void *) hdr + header_size;
377 len = get_hdr_inc(td, io_u) - header_size;
378 mod = (get_hdr_inc(td, io_u) * vc->hdr_num + header_size) % pattern_size;
380 rc = cmp_pattern(pattern, pattern_size, mod, buf, len);
384 /* Slow path, compare each byte */
385 for (i = 0; i < len; i++) {
386 if (buf[i] != pattern[mod]) {
389 bits = hweight8(buf[i] ^ pattern[mod]);
390 log_err("fio: got pattern '%02x', wanted '%02x'. Bad bits %d\n",
391 (unsigned char)buf[i],
392 (unsigned char)pattern[mod],
394 log_err("fio: bad pattern block offset %u\n", i);
395 dump_verify_buffers(hdr, vc);
399 if (mod == td->o.verify_pattern_bytes)
403 /* Unreachable line */
408 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
410 struct thread_data *td = vc->td;
411 struct vhdr_meta *vh = hdr_priv(hdr);
412 struct io_u *io_u = vc->io_u;
415 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
417 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
420 if (td->o.verify_pattern_bytes)
421 ret |= verify_io_u_pattern(hdr, vc);
424 * For read-only workloads, the program cannot be certain of the
425 * last numberio written to a block. Checking of numberio will be
426 * done only for workloads that write data. For verify_only,
427 * numberio will be checked in the last iteration when the correct
428 * state of numberio, that would have been written to each block
429 * in a previous run of fio, has been reached.
431 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
433 if (!td->o.verify_only || td->o.loops == 0)
434 if (vh->numberio != io_u->numberio)
441 log_verify_failure(hdr, vc);
445 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
447 void *p = io_u_verify_off(hdr, vc);
448 struct vhdr_xxhash *vh = hdr_priv(hdr);
452 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
454 state = XXH32_init(1);
455 XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr));
456 hash = XXH32_digest(state);
458 if (vh->hash == hash)
462 vc->good_crc = &vh->hash;
464 vc->crc_len = sizeof(hash);
465 log_verify_failure(hdr, vc);
469 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
471 void *p = io_u_verify_off(hdr, vc);
472 struct vhdr_sha512 *vh = hdr_priv(hdr);
474 struct fio_sha512_ctx sha512_ctx = {
478 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
480 fio_sha512_init(&sha512_ctx);
481 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr));
483 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
487 vc->good_crc = vh->sha512;
488 vc->bad_crc = sha512_ctx.buf;
489 vc->crc_len = sizeof(vh->sha512);
490 log_verify_failure(hdr, vc);
494 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
496 void *p = io_u_verify_off(hdr, vc);
497 struct vhdr_sha256 *vh = hdr_priv(hdr);
499 struct fio_sha256_ctx sha256_ctx = {
503 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
505 fio_sha256_init(&sha256_ctx);
506 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr));
507 fio_sha256_final(&sha256_ctx);
509 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
513 vc->good_crc = vh->sha256;
514 vc->bad_crc = sha256_ctx.buf;
515 vc->crc_len = sizeof(vh->sha256);
516 log_verify_failure(hdr, vc);
520 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
522 void *p = io_u_verify_off(hdr, vc);
523 struct vhdr_sha1 *vh = hdr_priv(hdr);
525 struct fio_sha1_ctx sha1_ctx = {
529 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
531 fio_sha1_init(&sha1_ctx);
532 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr));
533 fio_sha1_final(&sha1_ctx);
535 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
539 vc->good_crc = vh->sha1;
540 vc->bad_crc = sha1_ctx.H;
541 vc->crc_len = sizeof(vh->sha1);
542 log_verify_failure(hdr, vc);
546 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
548 void *p = io_u_verify_off(hdr, vc);
549 struct vhdr_crc7 *vh = hdr_priv(hdr);
552 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
554 c = fio_crc7(p, hdr->len - hdr_size(vc->td, hdr));
560 vc->good_crc = &vh->crc7;
563 log_verify_failure(hdr, vc);
567 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
569 void *p = io_u_verify_off(hdr, vc);
570 struct vhdr_crc16 *vh = hdr_priv(hdr);
573 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
575 c = fio_crc16(p, hdr->len - hdr_size(vc->td, hdr));
581 vc->good_crc = &vh->crc16;
584 log_verify_failure(hdr, vc);
588 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
590 void *p = io_u_verify_off(hdr, vc);
591 struct vhdr_crc64 *vh = hdr_priv(hdr);
592 unsigned long long c;
594 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
596 c = fio_crc64(p, hdr->len - hdr_size(vc->td, hdr));
602 vc->good_crc = &vh->crc64;
605 log_verify_failure(hdr, vc);
609 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
611 void *p = io_u_verify_off(hdr, vc);
612 struct vhdr_crc32 *vh = hdr_priv(hdr);
615 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
617 c = fio_crc32(p, hdr->len - hdr_size(vc->td, hdr));
623 vc->good_crc = &vh->crc32;
626 log_verify_failure(hdr, vc);
630 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
632 void *p = io_u_verify_off(hdr, vc);
633 struct vhdr_crc32 *vh = hdr_priv(hdr);
636 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
638 c = fio_crc32c(p, hdr->len - hdr_size(vc->td, hdr));
644 vc->good_crc = &vh->crc32;
647 log_verify_failure(hdr, vc);
651 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
653 void *p = io_u_verify_off(hdr, vc);
654 struct vhdr_md5 *vh = hdr_priv(hdr);
655 uint32_t hash[MD5_HASH_WORDS];
656 struct fio_md5_ctx md5_ctx = {
660 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
662 fio_md5_init(&md5_ctx);
663 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr));
664 fio_md5_final(&md5_ctx);
666 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
670 vc->good_crc = vh->md5_digest;
671 vc->bad_crc = md5_ctx.hash;
672 vc->crc_len = sizeof(hash);
673 log_verify_failure(hdr, vc);
678 * Push IO verification to a separate thread
680 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
682 struct io_u *io_u = *io_u_ptr;
684 pthread_mutex_lock(&td->io_u_lock);
687 put_file_log(td, io_u->file);
689 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
691 io_u_clear(io_u, IO_U_F_IN_CUR_DEPTH);
693 flist_add_tail(&io_u->verify_list, &td->verify_list);
695 pthread_mutex_unlock(&td->io_u_lock);
697 pthread_cond_signal(&td->verify_cond);
701 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
703 static char zero_buf[1024];
704 unsigned int this_len, len;
708 if (!td->o.trim_zero)
714 this_len = sizeof(zero_buf);
717 if (memcmp(p, zero_buf, this_len)) {
728 log_err("trim: verify failed at file %s offset %llu, length %lu"
729 ", block offset %lu\n",
730 io_u->file->file_name, io_u->offset, io_u->buflen,
731 (unsigned long) (p - io_u->buf));
735 static int verify_header(struct io_u *io_u, struct verify_header *hdr,
736 unsigned int hdr_num, unsigned int hdr_len)
741 if (hdr->magic != FIO_HDR_MAGIC) {
742 log_err("verify: bad magic header %x, wanted %x",
743 hdr->magic, FIO_HDR_MAGIC);
746 if (hdr->len != hdr_len) {
747 log_err("verify: bad header length %u, wanted %u",
751 if (hdr->rand_seed != io_u->rand_seed) {
752 log_err("verify: bad header rand_seed %"PRIu64
754 hdr->rand_seed, io_u->rand_seed);
758 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
759 if (crc != hdr->crc32) {
760 log_err("verify: bad header crc %x, calculated %x",
767 log_err(" at file %s offset %llu, length %u\n",
768 io_u->file->file_name,
769 io_u->offset + hdr_num * hdr_len, hdr_len);
773 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
775 struct verify_header *hdr;
776 struct io_u *io_u = *io_u_ptr;
777 unsigned int header_size, hdr_inc, hdr_num = 0;
781 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
784 * If the IO engine is faking IO (like null), then just pretend
785 * we verified everything.
787 if (td->io_ops->flags & FIO_FAKEIO)
790 if (io_u->flags & IO_U_F_TRIMMED) {
791 ret = verify_trimmed_io_u(td, io_u);
795 hdr_inc = get_hdr_inc(td, io_u);
798 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
799 p += hdr_inc, hdr_num++) {
805 unsigned int verify_type;
807 if (ret && td->o.verify_fatal)
810 header_size = __hdr_size(td->o.verify);
811 if (td->o.verify_offset)
812 memswp(p, p + td->o.verify_offset, header_size);
816 * Make rand_seed check pass when have verifysort or
819 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
820 io_u->rand_seed = hdr->rand_seed;
822 if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
823 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
828 if (td->o.verify != VERIFY_NONE)
829 verify_type = td->o.verify;
831 verify_type = hdr->verify_type;
833 switch (verify_type) {
835 ret = verify_io_u_md5(hdr, &vc);
838 ret = verify_io_u_crc64(hdr, &vc);
841 case VERIFY_CRC32C_INTEL:
842 ret = verify_io_u_crc32c(hdr, &vc);
845 ret = verify_io_u_crc32(hdr, &vc);
848 ret = verify_io_u_crc16(hdr, &vc);
851 ret = verify_io_u_crc7(hdr, &vc);
854 ret = verify_io_u_sha256(hdr, &vc);
857 ret = verify_io_u_sha512(hdr, &vc);
860 ret = verify_io_u_xxhash(hdr, &vc);
863 ret = verify_io_u_meta(hdr, &vc);
866 ret = verify_io_u_sha1(hdr, &vc);
869 case VERIFY_PATTERN_NO_HDR:
870 ret = verify_io_u_pattern(hdr, &vc);
873 log_err("Bad verify type %u\n", hdr->verify_type);
877 if (ret && verify_type != hdr->verify_type)
878 log_err("fio: verify type mismatch (%u media, %u given)\n",
879 hdr->verify_type, verify_type);
883 if (ret && td->o.verify_fatal)
884 fio_mark_td_terminate(td);
889 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
890 struct io_u *io_u, unsigned int header_num)
892 struct vhdr_meta *vh = hdr_priv(hdr);
894 vh->thread = td->thread_number;
896 vh->time_sec = io_u->start_time.tv_sec;
897 vh->time_usec = io_u->start_time.tv_usec;
899 vh->numberio = io_u->numberio;
901 vh->offset = io_u->offset + header_num * td->o.verify_interval;
904 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
906 struct vhdr_xxhash *vh = hdr_priv(hdr);
909 state = XXH32_init(1);
910 XXH32_update(state, p, len);
911 vh->hash = XXH32_digest(state);
914 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
916 struct vhdr_sha512 *vh = hdr_priv(hdr);
917 struct fio_sha512_ctx sha512_ctx = {
921 fio_sha512_init(&sha512_ctx);
922 fio_sha512_update(&sha512_ctx, p, len);
925 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
927 struct vhdr_sha256 *vh = hdr_priv(hdr);
928 struct fio_sha256_ctx sha256_ctx = {
932 fio_sha256_init(&sha256_ctx);
933 fio_sha256_update(&sha256_ctx, p, len);
934 fio_sha256_final(&sha256_ctx);
937 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
939 struct vhdr_sha1 *vh = hdr_priv(hdr);
940 struct fio_sha1_ctx sha1_ctx = {
944 fio_sha1_init(&sha1_ctx);
945 fio_sha1_update(&sha1_ctx, p, len);
946 fio_sha1_final(&sha1_ctx);
949 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
951 struct vhdr_crc7 *vh = hdr_priv(hdr);
953 vh->crc7 = fio_crc7(p, len);
956 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
958 struct vhdr_crc16 *vh = hdr_priv(hdr);
960 vh->crc16 = fio_crc16(p, len);
963 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
965 struct vhdr_crc32 *vh = hdr_priv(hdr);
967 vh->crc32 = fio_crc32(p, len);
970 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
972 struct vhdr_crc32 *vh = hdr_priv(hdr);
974 vh->crc32 = fio_crc32c(p, len);
977 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
979 struct vhdr_crc64 *vh = hdr_priv(hdr);
981 vh->crc64 = fio_crc64(p, len);
984 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
986 struct vhdr_md5 *vh = hdr_priv(hdr);
987 struct fio_md5_ctx md5_ctx = {
988 .hash = (uint32_t *) vh->md5_digest,
991 fio_md5_init(&md5_ctx);
992 fio_md5_update(&md5_ctx, p, len);
993 fio_md5_final(&md5_ctx);
996 static void __fill_hdr(struct verify_header *hdr, int verify_type,
997 uint32_t len, uint64_t rand_seed)
1001 hdr->magic = FIO_HDR_MAGIC;
1002 hdr->verify_type = verify_type;
1004 hdr->rand_seed = rand_seed;
1005 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
1009 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
1012 if (verify_type != VERIFY_PATTERN_NO_HDR)
1013 __fill_hdr(hdr, verify_type, len, rand_seed);
1016 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
1017 struct verify_header *hdr, unsigned int header_num,
1018 unsigned int header_len)
1020 unsigned int data_len;
1025 fill_hdr(hdr, td->o.verify, header_len, io_u->rand_seed);
1027 data_len = header_len - hdr_size(td, hdr);
1029 data = p + hdr_size(td, hdr);
1030 switch (td->o.verify) {
1032 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1034 fill_md5(hdr, data, data_len);
1037 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1039 fill_crc64(hdr, data, data_len);
1042 case VERIFY_CRC32C_INTEL:
1043 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1045 fill_crc32c(hdr, data, data_len);
1048 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1050 fill_crc32(hdr, data, data_len);
1053 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1055 fill_crc16(hdr, data, data_len);
1058 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1060 fill_crc7(hdr, data, data_len);
1063 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1065 fill_sha256(hdr, data, data_len);
1068 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1070 fill_sha512(hdr, data, data_len);
1073 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1075 fill_xxhash(hdr, data, data_len);
1078 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1080 fill_meta(hdr, td, io_u, header_num);
1083 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1085 fill_sha1(hdr, data, data_len);
1087 case VERIFY_PATTERN:
1088 case VERIFY_PATTERN_NO_HDR:
1089 /* nothing to do here */
1092 log_err("fio: bad verify type: %d\n", td->o.verify);
1096 if (td->o.verify_offset && hdr_size(td, hdr))
1097 memswp(p, p + td->o.verify_offset, hdr_size(td, hdr));
1101 * fill body of io_u->buf with random data and add a header with the
1102 * checksum of choice
1104 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1106 if (td->o.verify == VERIFY_NULL)
1109 io_u->numberio = td->io_issues[io_u->ddir];
1111 fill_pattern_headers(td, io_u, 0, 0);
1114 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1116 struct io_piece *ipo = NULL;
1119 * this io_u is from a requeue, we already filled the offsets
1124 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1125 struct rb_node *n = rb_first(&td->io_hist_tree);
1127 ipo = rb_entry(n, struct io_piece, rb_node);
1130 * Ensure that the associated IO has completed
1133 if (ipo->flags & IP_F_IN_FLIGHT)
1136 rb_erase(n, &td->io_hist_tree);
1137 assert(ipo->flags & IP_F_ONRB);
1138 ipo->flags &= ~IP_F_ONRB;
1139 } else if (!flist_empty(&td->io_hist_list)) {
1140 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1143 * Ensure that the associated IO has completed
1146 if (ipo->flags & IP_F_IN_FLIGHT)
1149 flist_del(&ipo->list);
1150 assert(ipo->flags & IP_F_ONLIST);
1151 ipo->flags &= ~IP_F_ONLIST;
1157 io_u->offset = ipo->offset;
1158 io_u->buflen = ipo->len;
1159 io_u->numberio = ipo->numberio;
1160 io_u->file = ipo->file;
1161 io_u_set(io_u, IO_U_F_VER_LIST);
1163 if (ipo->flags & IP_F_TRIMMED)
1164 io_u_set(io_u, IO_U_F_TRIMMED);
1166 if (!fio_file_open(io_u->file)) {
1167 int r = td_io_open_file(td, io_u->file);
1170 dprint(FD_VERIFY, "failed file %s open\n",
1171 io_u->file->file_name);
1176 get_file(ipo->file);
1177 assert(fio_file_open(io_u->file));
1178 io_u->ddir = DDIR_READ;
1179 io_u->xfer_buf = io_u->buf;
1180 io_u->xfer_buflen = io_u->buflen;
1182 remove_trim_entry(td, ipo);
1184 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1186 if (!td->o.verify_pattern_bytes) {
1187 io_u->rand_seed = __rand(&td->verify_state);
1188 if (sizeof(int) != sizeof(long *))
1189 io_u->rand_seed *= __rand(&td->verify_state);
1195 dprint(FD_VERIFY, "get_next_verify: empty\n");
1199 void fio_verify_init(struct thread_data *td)
1201 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1202 td->o.verify == VERIFY_CRC32C) {
1203 crc32c_intel_probe();
1207 static void *verify_async_thread(void *data)
1209 struct thread_data *td = data;
1213 if (fio_option_is_set(&td->o, verify_cpumask) &&
1214 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1215 log_err("fio: failed setting verify thread affinity\n");
1223 if (td->verify_thread_exit)
1226 pthread_mutex_lock(&td->io_u_lock);
1228 while (flist_empty(&td->verify_list) &&
1229 !td->verify_thread_exit) {
1230 ret = pthread_cond_wait(&td->verify_cond,
1233 pthread_mutex_unlock(&td->io_u_lock);
1238 flist_splice_init(&td->verify_list, &list);
1239 pthread_mutex_unlock(&td->io_u_lock);
1241 if (flist_empty(&list))
1244 while (!flist_empty(&list)) {
1245 io_u = flist_first_entry(&list, struct io_u, verify_list);
1246 flist_del_init(&io_u->verify_list);
1248 io_u_set(io_u, IO_U_F_NO_FILE_PUT);
1249 ret = verify_io_u(td, &io_u);
1254 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1255 update_error_count(td, ret);
1263 td_verror(td, ret, "async_verify");
1264 if (td->o.verify_fatal)
1265 fio_mark_td_terminate(td);
1269 pthread_mutex_lock(&td->io_u_lock);
1270 td->nr_verify_threads--;
1271 pthread_mutex_unlock(&td->io_u_lock);
1273 pthread_cond_signal(&td->free_cond);
1277 int verify_async_init(struct thread_data *td)
1280 pthread_attr_t attr;
1282 pthread_attr_init(&attr);
1283 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1285 td->verify_thread_exit = 0;
1287 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1288 for (i = 0; i < td->o.verify_async; i++) {
1289 ret = pthread_create(&td->verify_threads[i], &attr,
1290 verify_async_thread, td);
1292 log_err("fio: async verify creation failed: %s\n",
1296 ret = pthread_detach(td->verify_threads[i]);
1298 log_err("fio: async verify thread detach failed: %s\n",
1302 td->nr_verify_threads++;
1305 pthread_attr_destroy(&attr);
1307 if (i != td->o.verify_async) {
1308 log_err("fio: only %d verify threads started, exiting\n", i);
1309 td->verify_thread_exit = 1;
1311 pthread_cond_broadcast(&td->verify_cond);
1318 void verify_async_exit(struct thread_data *td)
1320 td->verify_thread_exit = 1;
1322 pthread_cond_broadcast(&td->verify_cond);
1324 pthread_mutex_lock(&td->io_u_lock);
1326 while (td->nr_verify_threads)
1327 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1329 pthread_mutex_unlock(&td->io_u_lock);
1330 free(td->verify_threads);
1331 td->verify_threads = NULL;
1334 int paste_blockoff(char *buf, unsigned int len, void *priv)
1336 struct io_u *io = priv;
1337 unsigned long long off;
1339 typecheck(typeof(off), io->offset);
1340 off = cpu_to_le64((uint64_t)io->offset);
1341 len = min(len, (unsigned int)sizeof(off));
1342 memcpy(buf, &off, len);
1346 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1348 struct all_io_list *rep;
1349 struct thread_data *td;
1354 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1357 * Calculate reply space needed. We need one 'io_state' per thread,
1358 * and the size will vary depending on depth.
1362 for_each_td(td, i) {
1363 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1366 td->flags |= TD_F_VSTATE_SAVED;
1367 depth += td->o.iodepth;
1375 *sz += nr * sizeof(struct thread_io_list);
1376 *sz += depth * sizeof(uint64_t);
1379 rep->threads = cpu_to_le64((uint64_t) nr);
1381 next = &rep->state[0];
1382 for_each_td(td, i) {
1383 struct thread_io_list *s = next;
1386 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1389 if (td->last_write_comp) {
1392 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1393 comps = td->io_blocks[DDIR_WRITE];
1395 comps = td->o.iodepth;
1397 k = td->last_write_idx - 1;
1398 for (j = 0; j < comps; j++) {
1400 k = td->o.iodepth - 1;
1401 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1407 s->no_comps = cpu_to_le64((uint64_t) comps);
1408 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1409 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1410 s->index = cpu_to_le64((uint64_t) i);
1411 if (td->random_state.use64) {
1412 s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
1413 s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
1414 s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
1415 s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
1416 s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
1417 s->rand.state64.s[5] = 0;
1418 s->rand.use64 = cpu_to_le64((uint64_t)1);
1420 s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
1421 s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
1422 s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
1423 s->rand.state32.s[3] = 0;
1426 s->name[sizeof(s->name) - 1] = '\0';
1427 strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
1428 next = io_list_next(s);
1434 static int open_state_file(const char *name, const char *prefix, int num,
1442 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1446 verify_state_gen_name(out, sizeof(out), name, prefix, num);
1448 fd = open(out, flags, 0644);
1450 perror("fio: open state file");
1457 static int write_thread_list_state(struct thread_io_list *s,
1460 struct verify_state_hdr hdr;
1465 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1469 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1471 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1472 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1473 hdr.crc = cpu_to_le64(crc);
1474 ret = write(fd, &hdr, sizeof(hdr));
1475 if (ret != sizeof(hdr))
1478 ret = write(fd, s, thread_io_list_sz(s));
1479 if (ret != thread_io_list_sz(s)) {
1482 perror("fio: write state file");
1483 log_err("fio: failed to write state file\n");
1492 void __verify_save_state(struct all_io_list *state, const char *prefix)
1494 struct thread_io_list *s = &state->state[0];
1497 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1498 write_thread_list_state(s, prefix);
1499 s = io_list_next(s);
1503 void verify_save_state(int mask)
1505 struct all_io_list *state;
1508 state = get_all_io_list(mask, &sz);
1510 char prefix[PATH_MAX];
1513 sprintf(prefix, "%s%slocal", aux_path, FIO_OS_PATH_SEPARATOR);
1515 strcpy(prefix, "local");
1517 __verify_save_state(state, prefix);
1522 void verify_free_state(struct thread_data *td)
1528 static struct thread_io_list *convert_v1_list(struct thread_io_list_v1 *s)
1530 struct thread_io_list *til;
1533 til = malloc(__thread_io_list_sz(s->no_comps));
1534 til->no_comps = s->no_comps;
1535 til->depth = s->depth;
1536 til->numberio = s->numberio;
1537 til->index = s->index;
1538 memcpy(til->name, s->name, sizeof(til->name));
1540 til->rand.use64 = 0;
1541 for (i = 0; i < 4; i++)
1542 til->rand.state32.s[i] = s->rand.s[i];
1544 for (i = 0; i < s->no_comps; i++)
1545 til->offsets[i] = s->offsets[i];
1550 void verify_convert_assign_state(struct thread_data *td, void *p, int version)
1552 struct thread_io_list *til;
1556 struct thread_io_list_v1 *s = p;
1558 s->no_comps = le64_to_cpu(s->no_comps);
1559 s->depth = le64_to_cpu(s->depth);
1560 s->numberio = le64_to_cpu(s->numberio);
1561 for (i = 0; i < 4; i++)
1562 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1563 for (i = 0; i < s->no_comps; i++)
1564 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1566 til = convert_v1_list(s);
1569 struct thread_io_list *s = p;
1571 s->no_comps = le64_to_cpu(s->no_comps);
1572 s->depth = le64_to_cpu(s->depth);
1573 s->numberio = le64_to_cpu(s->numberio);
1574 s->rand.use64 = le64_to_cpu(s->rand.use64);
1576 if (s->rand.use64) {
1577 for (i = 0; i < 6; i++)
1578 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
1580 for (i = 0; i < 4; i++)
1581 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1583 for (i = 0; i < s->no_comps; i++)
1584 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1592 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s,
1597 hdr->version = le64_to_cpu(hdr->version);
1598 hdr->size = le64_to_cpu(hdr->size);
1599 hdr->crc = le64_to_cpu(hdr->crc);
1601 if (hdr->version != VSTATE_HDR_VERSION ||
1602 hdr->version != VSTATE_HDR_VERSION_V1)
1605 crc = fio_crc32c((void *)s, hdr->size);
1606 if (crc != hdr->crc)
1609 *version = hdr->version;
1613 int verify_load_state(struct thread_data *td, const char *prefix)
1615 struct verify_state_hdr hdr;
1621 if (!td->o.verify_state)
1624 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1628 ret = read(fd, &hdr, sizeof(hdr));
1629 if (ret != sizeof(hdr)) {
1631 td_verror(td, errno, "read verify state hdr");
1632 log_err("fio: failed reading verify state header\n");
1636 hdr.version = le64_to_cpu(hdr.version);
1637 hdr.size = le64_to_cpu(hdr.size);
1638 hdr.crc = le64_to_cpu(hdr.crc);
1640 if (hdr.version != VSTATE_HDR_VERSION &&
1641 hdr.version != VSTATE_HDR_VERSION_V1) {
1642 log_err("fio: bad version in verify state header\n");
1646 s = malloc(hdr.size);
1647 ret = read(fd, s, hdr.size);
1648 if (ret != hdr.size) {
1650 td_verror(td, errno, "read verify state");
1651 log_err("fio: failed reading verity state\n");
1655 crc = fio_crc32c(s, hdr.size);
1656 if (crc != hdr.crc) {
1657 log_err("fio: verify state is corrupt\n");
1663 verify_convert_assign_state(td, s, hdr.version);
1673 * Use the loaded verify state to know when to stop doing verification
1675 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1677 struct thread_io_list *s = td->vstate;
1684 * If we're not into the window of issues - depth yet, continue. If
1685 * issue is shorter than depth, do check.
1687 if ((td->io_blocks[DDIR_READ] < s->depth ||
1688 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1689 s->numberio > s->depth)
1693 * We're in the window of having to check if this io was
1694 * completed or not. If the IO was seen as completed, then
1697 for (i = 0; i < s->no_comps; i++)
1698 if (io_u->offset == s->offsets[i])
1702 * Not found, we have to stop