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 thread_data *td, struct io_u *io_u,
33 struct verify_header *hdr, unsigned int header_num,
34 unsigned int header_len, uint64_t rand_seed);
35 static void __fill_hdr(struct thread_data *td, struct io_u *io_u,
36 struct verify_header *hdr, unsigned int header_num,
37 unsigned int header_len, uint64_t rand_seed);
39 void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
41 (void)cpy_pattern(td->o.buffer_pattern, td->o.buffer_pattern_bytes, p, len);
44 static void __fill_buffer(struct thread_options *o, unsigned long seed, void *p,
47 __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
50 static unsigned long fill_buffer(struct thread_data *td, void *p,
53 struct frand_state *fs = &td->verify_state;
54 struct thread_options *o = &td->o;
56 return fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
59 void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
60 struct io_u *io_u, unsigned long seed, int use_seed)
62 struct thread_options *o = &td->o;
64 if (!o->verify_pattern_bytes) {
65 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
68 __fill_buffer(o, seed, p, len);
70 io_u->rand_seed = fill_buffer(td, p, len);
74 /* Skip if we were here and we do not need to patch pattern
76 if (!td->o.verify_fmt_sz && io_u->buf_filled_len >= len) {
77 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
78 o->verify_pattern_bytes, len);
82 (void)paste_format(td->o.verify_pattern, td->o.verify_pattern_bytes,
83 td->o.verify_fmt, td->o.verify_fmt_sz,
85 io_u->buf_filled_len = len;
88 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
92 hdr_inc = io_u->buflen;
93 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
94 hdr_inc = td->o.verify_interval;
99 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
100 unsigned long seed, int use_seed)
102 unsigned int hdr_inc, header_num;
103 struct verify_header *hdr;
106 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
108 hdr_inc = get_hdr_inc(td, io_u);
110 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
112 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
117 static void memswp(void *buf1, void *buf2, unsigned int len)
121 assert(len <= sizeof(swap));
123 memcpy(&swap, buf1, len);
124 memcpy(buf1, buf2, len);
125 memcpy(buf2, &swap, len);
128 static void hexdump(void *buffer, int len)
130 unsigned char *p = buffer;
133 for (i = 0; i < len; i++)
134 log_err("%02x", p[i]);
139 * Prepare for separation of verify_header and checksum header
141 static inline unsigned int __hdr_size(int verify_type)
143 unsigned int len = 0;
145 switch (verify_type) {
147 case VERIFY_HDR_ONLY:
153 len = sizeof(struct vhdr_md5);
156 len = sizeof(struct vhdr_crc64);
160 case VERIFY_CRC32C_INTEL:
161 len = sizeof(struct vhdr_crc32);
164 len = sizeof(struct vhdr_crc16);
167 len = sizeof(struct vhdr_crc7);
170 len = sizeof(struct vhdr_sha256);
173 len = sizeof(struct vhdr_sha512);
176 len = sizeof(struct vhdr_xxhash);
179 len = sizeof(struct vhdr_sha1);
181 case VERIFY_PATTERN_NO_HDR:
184 log_err("fio: unknown verify header!\n");
188 return len + sizeof(struct verify_header);
191 static inline unsigned int hdr_size(struct thread_data *td,
192 struct verify_header *hdr)
194 if (td->o.verify == VERIFY_PATTERN_NO_HDR)
197 return __hdr_size(hdr->verify_type);
200 static void *hdr_priv(struct verify_header *hdr)
204 return priv + sizeof(struct verify_header);
208 * Verify container, pass info to verify handlers and allow them to
209 * pass info back in case of error
216 unsigned int hdr_num;
217 struct thread_data *td;
220 * Output, only valid in case of error
225 unsigned int crc_len;
228 #define DUMP_BUF_SZ 255
229 static int dump_buf_warned;
231 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
232 const char *type, struct fio_file *f)
234 char *ptr, fname[DUMP_BUF_SZ];
235 size_t buf_left = DUMP_BUF_SZ;
238 ptr = strdup(f->file_name);
240 memset(fname, 0, sizeof(fname));
242 sprintf(fname, "%s%s", aux_path, FIO_OS_PATH_SEPARATOR);
244 strncpy(fname + strlen(fname), basename(ptr), buf_left - 1);
246 buf_left -= strlen(fname);
248 if (!dump_buf_warned) {
249 log_err("fio: verify failure dump buffer too small\n");
256 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
258 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
260 perror("open verify buf file");
265 ret = write(fd, buf, len);
269 perror("write verify buf file");
277 log_err(" %s data dumped as %s\n", type, fname);
282 * Dump the contents of the read block and re-generate the correct data
285 static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
287 struct thread_data *td = vc->td;
288 struct io_u *io_u = vc->io_u;
289 unsigned long hdr_offset;
293 if (!td->o.verify_dump)
297 * Dump the contents we just read off disk
299 hdr_offset = vc->hdr_num * hdr->len;
301 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
302 "received", vc->io_u->file);
305 * Allocate a new buf and re-generate the original data
307 buf = malloc(io_u->buflen);
310 dummy.rand_seed = hdr->rand_seed;
311 dummy.buf_filled_len = 0;
312 dummy.buflen = io_u->buflen;
314 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
316 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
317 "expected", vc->io_u->file);
321 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
323 struct thread_data *td = vc->td;
324 struct verify_header shdr;
326 if (td->o.verify == VERIFY_PATTERN_NO_HDR) {
327 __fill_hdr(td, vc->io_u, &shdr, 0, vc->io_u->buflen, 0);
331 __dump_verify_buffers(hdr, vc);
334 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
336 unsigned long long offset;
338 offset = vc->io_u->offset;
339 offset += vc->hdr_num * hdr->len;
340 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
341 vc->name, vc->io_u->file->file_name, offset, hdr->len);
343 if (vc->good_crc && vc->bad_crc) {
344 log_err(" Expected CRC: ");
345 hexdump(vc->good_crc, vc->crc_len);
346 log_err(" Received CRC: ");
347 hexdump(vc->bad_crc, vc->crc_len);
350 dump_verify_buffers(hdr, vc);
354 * Return data area 'header_num'
356 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
358 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr);
361 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
363 struct thread_data *td = vc->td;
364 struct io_u *io_u = vc->io_u;
366 unsigned int header_size = __hdr_size(td->o.verify);
367 unsigned int len, mod, i, pattern_size;
370 pattern = td->o.verify_pattern;
371 pattern_size = td->o.verify_pattern_bytes;
372 assert(pattern_size != 0);
374 (void)paste_format_inplace(pattern, pattern_size,
375 td->o.verify_fmt, td->o.verify_fmt_sz, io_u);
377 buf = (void *) hdr + header_size;
378 len = get_hdr_inc(td, io_u) - header_size;
379 mod = (get_hdr_inc(td, io_u) * vc->hdr_num + header_size) % pattern_size;
381 rc = cmp_pattern(pattern, pattern_size, mod, buf, len);
385 /* Slow path, compare each byte */
386 for (i = 0; i < len; i++) {
387 if (buf[i] != pattern[mod]) {
390 bits = hweight8(buf[i] ^ pattern[mod]);
391 log_err("fio: got pattern '%02x', wanted '%02x'. Bad bits %d\n",
392 (unsigned char)buf[i],
393 (unsigned char)pattern[mod],
395 log_err("fio: bad pattern block offset %u\n", i);
396 log_verify_failure(hdr, vc);
400 if (mod == td->o.verify_pattern_bytes)
404 /* Unreachable line */
409 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
411 void *p = io_u_verify_off(hdr, vc);
412 struct vhdr_xxhash *vh = hdr_priv(hdr);
416 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
418 state = XXH32_init(1);
419 XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr));
420 hash = XXH32_digest(state);
422 if (vh->hash == hash)
426 vc->good_crc = &vh->hash;
428 vc->crc_len = sizeof(hash);
429 log_verify_failure(hdr, vc);
433 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
435 void *p = io_u_verify_off(hdr, vc);
436 struct vhdr_sha512 *vh = hdr_priv(hdr);
438 struct fio_sha512_ctx sha512_ctx = {
442 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
444 fio_sha512_init(&sha512_ctx);
445 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr));
447 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
451 vc->good_crc = vh->sha512;
452 vc->bad_crc = sha512_ctx.buf;
453 vc->crc_len = sizeof(vh->sha512);
454 log_verify_failure(hdr, vc);
458 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
460 void *p = io_u_verify_off(hdr, vc);
461 struct vhdr_sha256 *vh = hdr_priv(hdr);
463 struct fio_sha256_ctx sha256_ctx = {
467 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
469 fio_sha256_init(&sha256_ctx);
470 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr));
471 fio_sha256_final(&sha256_ctx);
473 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
477 vc->good_crc = vh->sha256;
478 vc->bad_crc = sha256_ctx.buf;
479 vc->crc_len = sizeof(vh->sha256);
480 log_verify_failure(hdr, vc);
484 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
486 void *p = io_u_verify_off(hdr, vc);
487 struct vhdr_sha1 *vh = hdr_priv(hdr);
489 struct fio_sha1_ctx sha1_ctx = {
493 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
495 fio_sha1_init(&sha1_ctx);
496 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr));
497 fio_sha1_final(&sha1_ctx);
499 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(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);
510 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
512 void *p = io_u_verify_off(hdr, vc);
513 struct vhdr_crc7 *vh = hdr_priv(hdr);
516 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
518 c = fio_crc7(p, hdr->len - hdr_size(vc->td, hdr));
524 vc->good_crc = &vh->crc7;
527 log_verify_failure(hdr, vc);
531 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
533 void *p = io_u_verify_off(hdr, vc);
534 struct vhdr_crc16 *vh = hdr_priv(hdr);
537 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
539 c = fio_crc16(p, hdr->len - hdr_size(vc->td, hdr));
545 vc->good_crc = &vh->crc16;
548 log_verify_failure(hdr, vc);
552 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
554 void *p = io_u_verify_off(hdr, vc);
555 struct vhdr_crc64 *vh = hdr_priv(hdr);
556 unsigned long long c;
558 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
560 c = fio_crc64(p, hdr->len - hdr_size(vc->td, hdr));
566 vc->good_crc = &vh->crc64;
569 log_verify_failure(hdr, vc);
573 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
575 void *p = io_u_verify_off(hdr, vc);
576 struct vhdr_crc32 *vh = hdr_priv(hdr);
579 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
581 c = fio_crc32(p, hdr->len - hdr_size(vc->td, hdr));
587 vc->good_crc = &vh->crc32;
590 log_verify_failure(hdr, vc);
594 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
596 void *p = io_u_verify_off(hdr, vc);
597 struct vhdr_crc32 *vh = hdr_priv(hdr);
600 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
602 c = fio_crc32c(p, hdr->len - hdr_size(vc->td, hdr));
608 vc->good_crc = &vh->crc32;
611 log_verify_failure(hdr, vc);
615 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
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 = {
624 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
626 fio_md5_init(&md5_ctx);
627 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr));
628 fio_md5_final(&md5_ctx);
630 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
634 vc->good_crc = vh->md5_digest;
635 vc->bad_crc = md5_ctx.hash;
636 vc->crc_len = sizeof(hash);
637 log_verify_failure(hdr, vc);
642 * Push IO verification to a separate thread
644 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
646 struct io_u *io_u = *io_u_ptr;
648 pthread_mutex_lock(&td->io_u_lock);
651 put_file_log(td, io_u->file);
653 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
655 io_u_clear(td, io_u, IO_U_F_IN_CUR_DEPTH);
657 flist_add_tail(&io_u->verify_list, &td->verify_list);
659 pthread_mutex_unlock(&td->io_u_lock);
661 pthread_cond_signal(&td->verify_cond);
666 * Thanks Rusty, for spending the time so I don't have to.
668 * http://rusty.ozlabs.org/?p=560
670 static int mem_is_zero(const void *data, size_t length)
672 const unsigned char *p = data;
675 /* Check first 16 bytes manually */
676 for (len = 0; len < 16; len++) {
685 /* Now we know that's zero, memcmp with self. */
686 return memcmp(data, p, length) == 0;
689 static int mem_is_zero_slow(const void *data, size_t length, size_t *offset)
691 const unsigned char *p = data;
705 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
709 if (!td->o.trim_zero)
712 if (mem_is_zero(io_u->buf, io_u->buflen))
715 mem_is_zero_slow(io_u->buf, io_u->buflen, &offset);
717 log_err("trim: verify failed at file %s offset %llu, length %lu"
718 ", block offset %lu\n",
719 io_u->file->file_name, io_u->offset, io_u->buflen,
720 (unsigned long) offset);
724 static int verify_header(struct io_u *io_u, struct thread_data *td,
725 struct verify_header *hdr, unsigned int hdr_num,
726 unsigned int hdr_len)
731 if (hdr->magic != FIO_HDR_MAGIC) {
732 log_err("verify: bad magic header %x, wanted %x",
733 hdr->magic, FIO_HDR_MAGIC);
736 if (hdr->len != hdr_len) {
737 log_err("verify: bad header length %u, wanted %u",
741 if (hdr->rand_seed != io_u->rand_seed) {
742 log_err("verify: bad header rand_seed %"PRIu64
744 hdr->rand_seed, io_u->rand_seed);
747 if (hdr->offset != io_u->offset + hdr_num * td->o.verify_interval) {
748 log_err("verify: bad header offset %"PRIu64
750 hdr->offset, io_u->offset);
755 * For read-only workloads, the program cannot be certain of the
756 * last numberio written to a block. Checking of numberio will be
757 * done only for workloads that write data. For verify_only,
758 * numberio will be checked in the last iteration when the correct
759 * state of numberio, that would have been written to each block
760 * in a previous run of fio, has been reached.
762 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
764 if (!td->o.verify_only || td->o.loops == 0)
765 if (hdr->numberio != io_u->numberio) {
766 log_err("verify: bad header numberio %"PRIu16
768 hdr->numberio, io_u->numberio);
772 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
773 if (crc != hdr->crc32) {
774 log_err("verify: bad header crc %x, calculated %x",
781 log_err(" at file %s offset %llu, length %u\n",
782 io_u->file->file_name,
783 io_u->offset + hdr_num * hdr_len, hdr_len);
785 if (td->o.verify_dump)
786 dump_buf(p, hdr_len, io_u->offset + hdr_num * hdr_len,
787 "hdr_fail", io_u->file);
792 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
794 struct verify_header *hdr;
795 struct io_u *io_u = *io_u_ptr;
796 unsigned int header_size, hdr_inc, hdr_num = 0;
800 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
803 * If the IO engine is faking IO (like null), then just pretend
804 * we verified everything.
806 if (td_ioengine_flagged(td, FIO_FAKEIO))
809 if (io_u->flags & IO_U_F_TRIMMED) {
810 ret = verify_trimmed_io_u(td, io_u);
814 hdr_inc = get_hdr_inc(td, io_u);
817 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
818 p += hdr_inc, hdr_num++) {
824 unsigned int verify_type;
826 if (ret && td->o.verify_fatal)
829 header_size = __hdr_size(td->o.verify);
830 if (td->o.verify_offset)
831 memswp(p, p + td->o.verify_offset, header_size);
835 * Make rand_seed check pass when have verifysort or
838 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
839 io_u->rand_seed = hdr->rand_seed;
841 if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
842 ret = verify_header(io_u, td, hdr, hdr_num, hdr_inc);
847 if (td->o.verify != VERIFY_NONE)
848 verify_type = td->o.verify;
850 verify_type = hdr->verify_type;
852 switch (verify_type) {
853 case VERIFY_HDR_ONLY:
854 /* Header is always verified, check if pattern is left
855 * for verification. */
856 if (td->o.verify_pattern_bytes)
857 ret = verify_io_u_pattern(hdr, &vc);
860 ret = verify_io_u_md5(hdr, &vc);
863 ret = verify_io_u_crc64(hdr, &vc);
866 case VERIFY_CRC32C_INTEL:
867 ret = verify_io_u_crc32c(hdr, &vc);
870 ret = verify_io_u_crc32(hdr, &vc);
873 ret = verify_io_u_crc16(hdr, &vc);
876 ret = verify_io_u_crc7(hdr, &vc);
879 ret = verify_io_u_sha256(hdr, &vc);
882 ret = verify_io_u_sha512(hdr, &vc);
885 ret = verify_io_u_xxhash(hdr, &vc);
888 ret = verify_io_u_sha1(hdr, &vc);
891 case VERIFY_PATTERN_NO_HDR:
892 ret = verify_io_u_pattern(hdr, &vc);
895 log_err("Bad verify type %u\n", hdr->verify_type);
899 if (ret && verify_type != hdr->verify_type)
900 log_err("fio: verify type mismatch (%u media, %u given)\n",
901 hdr->verify_type, verify_type);
905 if (ret && td->o.verify_fatal)
906 fio_mark_td_terminate(td);
911 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
913 struct vhdr_xxhash *vh = hdr_priv(hdr);
916 state = XXH32_init(1);
917 XXH32_update(state, p, len);
918 vh->hash = XXH32_digest(state);
921 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
923 struct vhdr_sha512 *vh = hdr_priv(hdr);
924 struct fio_sha512_ctx sha512_ctx = {
928 fio_sha512_init(&sha512_ctx);
929 fio_sha512_update(&sha512_ctx, p, len);
932 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
934 struct vhdr_sha256 *vh = hdr_priv(hdr);
935 struct fio_sha256_ctx sha256_ctx = {
939 fio_sha256_init(&sha256_ctx);
940 fio_sha256_update(&sha256_ctx, p, len);
941 fio_sha256_final(&sha256_ctx);
944 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
946 struct vhdr_sha1 *vh = hdr_priv(hdr);
947 struct fio_sha1_ctx sha1_ctx = {
951 fio_sha1_init(&sha1_ctx);
952 fio_sha1_update(&sha1_ctx, p, len);
953 fio_sha1_final(&sha1_ctx);
956 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
958 struct vhdr_crc7 *vh = hdr_priv(hdr);
960 vh->crc7 = fio_crc7(p, len);
963 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
965 struct vhdr_crc16 *vh = hdr_priv(hdr);
967 vh->crc16 = fio_crc16(p, len);
970 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
972 struct vhdr_crc32 *vh = hdr_priv(hdr);
974 vh->crc32 = fio_crc32(p, len);
977 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
979 struct vhdr_crc32 *vh = hdr_priv(hdr);
981 vh->crc32 = fio_crc32c(p, len);
984 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
986 struct vhdr_crc64 *vh = hdr_priv(hdr);
988 vh->crc64 = fio_crc64(p, len);
991 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
993 struct vhdr_md5 *vh = hdr_priv(hdr);
994 struct fio_md5_ctx md5_ctx = {
995 .hash = (uint32_t *) vh->md5_digest,
998 fio_md5_init(&md5_ctx);
999 fio_md5_update(&md5_ctx, p, len);
1000 fio_md5_final(&md5_ctx);
1003 static void __fill_hdr(struct thread_data *td, struct io_u *io_u,
1004 struct verify_header *hdr, unsigned int header_num,
1005 unsigned int header_len, uint64_t rand_seed)
1009 hdr->magic = FIO_HDR_MAGIC;
1010 hdr->verify_type = td->o.verify;
1011 hdr->len = header_len;
1012 hdr->rand_seed = rand_seed;
1013 hdr->offset = io_u->offset + header_num * td->o.verify_interval;
1014 hdr->time_sec = io_u->start_time.tv_sec;
1015 hdr->time_usec = io_u->start_time.tv_usec;
1016 hdr->thread = td->thread_number;
1017 hdr->numberio = io_u->numberio;
1018 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
1022 static void fill_hdr(struct thread_data *td, struct io_u *io_u,
1023 struct verify_header *hdr, unsigned int header_num,
1024 unsigned int header_len, uint64_t rand_seed)
1027 if (td->o.verify != VERIFY_PATTERN_NO_HDR)
1028 __fill_hdr(td, io_u, hdr, header_num, header_len, rand_seed);
1031 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
1032 struct verify_header *hdr, unsigned int header_num,
1033 unsigned int header_len)
1035 unsigned int data_len;
1040 fill_hdr(td, io_u, hdr, header_num, header_len, io_u->rand_seed);
1042 data_len = header_len - hdr_size(td, hdr);
1044 data = p + hdr_size(td, hdr);
1045 switch (td->o.verify) {
1047 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1049 fill_md5(hdr, data, data_len);
1052 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1054 fill_crc64(hdr, data, data_len);
1057 case VERIFY_CRC32C_INTEL:
1058 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1060 fill_crc32c(hdr, data, data_len);
1063 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1065 fill_crc32(hdr, data, data_len);
1068 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1070 fill_crc16(hdr, data, data_len);
1073 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1075 fill_crc7(hdr, data, data_len);
1078 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1080 fill_sha256(hdr, data, data_len);
1083 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1085 fill_sha512(hdr, data, data_len);
1088 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1090 fill_xxhash(hdr, data, data_len);
1093 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1095 fill_sha1(hdr, data, data_len);
1097 case VERIFY_HDR_ONLY:
1098 case VERIFY_PATTERN:
1099 case VERIFY_PATTERN_NO_HDR:
1100 /* nothing to do here */
1103 log_err("fio: bad verify type: %d\n", td->o.verify);
1107 if (td->o.verify_offset && hdr_size(td, hdr))
1108 memswp(p, p + td->o.verify_offset, hdr_size(td, hdr));
1112 * fill body of io_u->buf with random data and add a header with the
1113 * checksum of choice
1115 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1117 if (td->o.verify == VERIFY_NULL)
1120 io_u->numberio = td->io_issues[io_u->ddir];
1122 fill_pattern_headers(td, io_u, 0, 0);
1125 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1127 struct io_piece *ipo = NULL;
1130 * this io_u is from a requeue, we already filled the offsets
1135 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1136 struct rb_node *n = rb_first(&td->io_hist_tree);
1138 ipo = rb_entry(n, struct io_piece, rb_node);
1141 * Ensure that the associated IO has completed
1144 if (ipo->flags & IP_F_IN_FLIGHT)
1147 rb_erase(n, &td->io_hist_tree);
1148 assert(ipo->flags & IP_F_ONRB);
1149 ipo->flags &= ~IP_F_ONRB;
1150 } else if (!flist_empty(&td->io_hist_list)) {
1151 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1154 * Ensure that the associated IO has completed
1157 if (ipo->flags & IP_F_IN_FLIGHT)
1160 flist_del(&ipo->list);
1161 assert(ipo->flags & IP_F_ONLIST);
1162 ipo->flags &= ~IP_F_ONLIST;
1168 io_u->offset = ipo->offset;
1169 io_u->buflen = ipo->len;
1170 io_u->numberio = ipo->numberio;
1171 io_u->file = ipo->file;
1172 io_u_set(td, io_u, IO_U_F_VER_LIST);
1174 if (ipo->flags & IP_F_TRIMMED)
1175 io_u_set(td, io_u, IO_U_F_TRIMMED);
1177 if (!fio_file_open(io_u->file)) {
1178 int r = td_io_open_file(td, io_u->file);
1181 dprint(FD_VERIFY, "failed file %s open\n",
1182 io_u->file->file_name);
1187 get_file(ipo->file);
1188 assert(fio_file_open(io_u->file));
1189 io_u->ddir = DDIR_READ;
1190 io_u->xfer_buf = io_u->buf;
1191 io_u->xfer_buflen = io_u->buflen;
1193 remove_trim_entry(td, ipo);
1195 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1197 if (!td->o.verify_pattern_bytes) {
1198 io_u->rand_seed = __rand(&td->verify_state);
1199 if (sizeof(int) != sizeof(long *))
1200 io_u->rand_seed *= __rand(&td->verify_state);
1206 dprint(FD_VERIFY, "get_next_verify: empty\n");
1210 void fio_verify_init(struct thread_data *td)
1212 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1213 td->o.verify == VERIFY_CRC32C_ARM64 ||
1214 td->o.verify == VERIFY_CRC32C) {
1215 crc32c_arm64_probe();
1216 crc32c_intel_probe();
1220 static void *verify_async_thread(void *data)
1222 struct thread_data *td = data;
1226 if (fio_option_is_set(&td->o, verify_cpumask) &&
1227 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1228 log_err("fio: failed setting verify thread affinity\n");
1236 if (td->verify_thread_exit)
1239 pthread_mutex_lock(&td->io_u_lock);
1241 while (flist_empty(&td->verify_list) &&
1242 !td->verify_thread_exit) {
1243 ret = pthread_cond_wait(&td->verify_cond,
1246 pthread_mutex_unlock(&td->io_u_lock);
1251 flist_splice_init(&td->verify_list, &list);
1252 pthread_mutex_unlock(&td->io_u_lock);
1254 if (flist_empty(&list))
1257 while (!flist_empty(&list)) {
1258 io_u = flist_first_entry(&list, struct io_u, verify_list);
1259 flist_del_init(&io_u->verify_list);
1261 io_u_set(td, io_u, IO_U_F_NO_FILE_PUT);
1262 ret = verify_io_u(td, &io_u);
1267 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1268 update_error_count(td, ret);
1276 td_verror(td, ret, "async_verify");
1277 if (td->o.verify_fatal)
1278 fio_mark_td_terminate(td);
1282 pthread_mutex_lock(&td->io_u_lock);
1283 td->nr_verify_threads--;
1284 pthread_mutex_unlock(&td->io_u_lock);
1286 pthread_cond_signal(&td->free_cond);
1290 int verify_async_init(struct thread_data *td)
1293 pthread_attr_t attr;
1295 pthread_attr_init(&attr);
1296 pthread_attr_setstacksize(&attr, 2 * PTHREAD_STACK_MIN);
1298 td->verify_thread_exit = 0;
1300 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1301 for (i = 0; i < td->o.verify_async; i++) {
1302 ret = pthread_create(&td->verify_threads[i], &attr,
1303 verify_async_thread, td);
1305 log_err("fio: async verify creation failed: %s\n",
1309 ret = pthread_detach(td->verify_threads[i]);
1311 log_err("fio: async verify thread detach failed: %s\n",
1315 td->nr_verify_threads++;
1318 pthread_attr_destroy(&attr);
1320 if (i != td->o.verify_async) {
1321 log_err("fio: only %d verify threads started, exiting\n", i);
1322 td->verify_thread_exit = 1;
1324 pthread_cond_broadcast(&td->verify_cond);
1331 void verify_async_exit(struct thread_data *td)
1333 td->verify_thread_exit = 1;
1335 pthread_cond_broadcast(&td->verify_cond);
1337 pthread_mutex_lock(&td->io_u_lock);
1339 while (td->nr_verify_threads)
1340 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1342 pthread_mutex_unlock(&td->io_u_lock);
1343 free(td->verify_threads);
1344 td->verify_threads = NULL;
1347 int paste_blockoff(char *buf, unsigned int len, void *priv)
1349 struct io_u *io = priv;
1350 unsigned long long off;
1352 typecheck(typeof(off), io->offset);
1353 off = cpu_to_le64((uint64_t)io->offset);
1354 len = min(len, (unsigned int)sizeof(off));
1355 memcpy(buf, &off, len);
1359 static int __fill_file_completions(struct thread_data *td,
1360 struct thread_io_list *s,
1361 struct fio_file *f, unsigned int *index)
1366 if (!f->last_write_comp)
1369 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1370 comps = td->io_blocks[DDIR_WRITE];
1372 comps = td->o.iodepth;
1374 j = f->last_write_idx - 1;
1375 for (i = 0; i < comps; i++) {
1377 j = td->o.iodepth - 1;
1378 s->comps[*index].fileno = __cpu_to_le64(f->fileno);
1379 s->comps[*index].offset = cpu_to_le64(f->last_write_comp[j]);
1387 static int fill_file_completions(struct thread_data *td,
1388 struct thread_io_list *s, unsigned int *index)
1394 for_each_file(td, f, i)
1395 comps += __fill_file_completions(td, s, f, index);
1400 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1402 struct all_io_list *rep;
1403 struct thread_data *td;
1408 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1411 * Calculate reply space needed. We need one 'io_state' per thread,
1412 * and the size will vary depending on depth.
1416 for_each_td(td, i) {
1417 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1420 td->flags |= TD_F_VSTATE_SAVED;
1421 depth += (td->o.iodepth * td->o.nr_files);
1429 *sz += nr * sizeof(struct thread_io_list);
1430 *sz += depth * sizeof(struct file_comp);
1432 memset(rep, 0, *sz);
1434 rep->threads = cpu_to_le64((uint64_t) nr);
1436 next = &rep->state[0];
1437 for_each_td(td, i) {
1438 struct thread_io_list *s = next;
1439 unsigned int comps, index = 0;
1441 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1444 comps = fill_file_completions(td, s, &index);
1446 s->no_comps = cpu_to_le64((uint64_t) comps);
1447 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1448 s->nofiles = cpu_to_le64((uint64_t) td->o.nr_files);
1449 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1450 s->index = cpu_to_le64((uint64_t) i);
1451 if (td->random_state.use64) {
1452 s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
1453 s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
1454 s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
1455 s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
1456 s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
1457 s->rand.state64.s[5] = 0;
1458 s->rand.use64 = cpu_to_le64((uint64_t)1);
1460 s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
1461 s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
1462 s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
1463 s->rand.state32.s[3] = 0;
1466 s->name[sizeof(s->name) - 1] = '\0';
1467 strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
1468 next = io_list_next(s);
1474 static int open_state_file(const char *name, const char *prefix, int num,
1482 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1486 verify_state_gen_name(out, sizeof(out), name, prefix, num);
1488 fd = open(out, flags, 0644);
1490 perror("fio: open state file");
1491 log_err("fio: state file: %s (for_write=%d)\n", out, for_write);
1498 static int write_thread_list_state(struct thread_io_list *s,
1501 struct verify_state_hdr hdr;
1506 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1510 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1512 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1513 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1514 hdr.crc = cpu_to_le64(crc);
1515 ret = write(fd, &hdr, sizeof(hdr));
1516 if (ret != sizeof(hdr))
1519 ret = write(fd, s, thread_io_list_sz(s));
1520 if (ret != thread_io_list_sz(s)) {
1523 perror("fio: write state file");
1524 log_err("fio: failed to write state file\n");
1533 void __verify_save_state(struct all_io_list *state, const char *prefix)
1535 struct thread_io_list *s = &state->state[0];
1538 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1539 write_thread_list_state(s, prefix);
1540 s = io_list_next(s);
1544 void verify_save_state(int mask)
1546 struct all_io_list *state;
1549 state = get_all_io_list(mask, &sz);
1551 char prefix[PATH_MAX];
1554 sprintf(prefix, "%s%slocal", aux_path, FIO_OS_PATH_SEPARATOR);
1556 strcpy(prefix, "local");
1558 __verify_save_state(state, prefix);
1563 void verify_free_state(struct thread_data *td)
1569 void verify_assign_state(struct thread_data *td, void *p)
1571 struct thread_io_list *s = p;
1574 s->no_comps = le64_to_cpu(s->no_comps);
1575 s->depth = le32_to_cpu(s->depth);
1576 s->nofiles = le32_to_cpu(s->nofiles);
1577 s->numberio = le64_to_cpu(s->numberio);
1578 s->rand.use64 = le64_to_cpu(s->rand.use64);
1580 if (s->rand.use64) {
1581 for (i = 0; i < 6; i++)
1582 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
1584 for (i = 0; i < 4; i++)
1585 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1588 for (i = 0; i < s->no_comps; i++) {
1589 s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno);
1590 s->comps[i].offset = le64_to_cpu(s->comps[i].offset);
1596 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s)
1600 hdr->version = le64_to_cpu(hdr->version);
1601 hdr->size = le64_to_cpu(hdr->size);
1602 hdr->crc = le64_to_cpu(hdr->crc);
1604 if (hdr->version != VSTATE_HDR_VERSION)
1607 crc = fio_crc32c((void *)s, hdr->size);
1608 if (crc != hdr->crc)
1614 int verify_load_state(struct thread_data *td, const char *prefix)
1616 struct verify_state_hdr hdr;
1622 if (!td->o.verify_state)
1625 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1629 ret = read(fd, &hdr, sizeof(hdr));
1630 if (ret != sizeof(hdr)) {
1632 td_verror(td, errno, "read verify state hdr");
1633 log_err("fio: failed reading verify state header\n");
1637 hdr.version = le64_to_cpu(hdr.version);
1638 hdr.size = le64_to_cpu(hdr.size);
1639 hdr.crc = le64_to_cpu(hdr.crc);
1641 if (hdr.version != VSTATE_HDR_VERSION) {
1642 log_err("fio: unsupported (%d) version in verify state header\n",
1643 (unsigned int) hdr.version);
1647 s = malloc(hdr.size);
1648 ret = read(fd, s, hdr.size);
1649 if (ret != hdr.size) {
1651 td_verror(td, errno, "read verify state");
1652 log_err("fio: failed reading verity state\n");
1656 crc = fio_crc32c(s, hdr.size);
1657 if (crc != hdr.crc) {
1658 log_err("fio: verify state is corrupt\n");
1664 verify_assign_state(td, s);
1674 * Use the loaded verify state to know when to stop doing verification
1676 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1678 struct thread_io_list *s = td->vstate;
1679 struct fio_file *f = io_u->file;
1686 * If we're not into the window of issues - depth yet, continue. If
1687 * issue is shorter than depth, do check.
1689 if ((td->io_blocks[DDIR_READ] < s->depth ||
1690 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1691 s->numberio > s->depth)
1695 * We're in the window of having to check if this io was
1696 * completed or not. If the IO was seen as completed, then
1699 for (i = 0; i < s->no_comps; i++) {
1700 if (s->comps[i].fileno != f->fileno)
1702 if (io_u->offset == s->comps[i].offset)
1707 * Not found, we have to stop