2 * IO verification helpers
15 #include "lib/hweight.h"
18 #include "crc/crc64.h"
19 #include "crc/crc32.h"
20 #include "crc/crc32c.h"
21 #include "crc/crc16.h"
23 #include "crc/sha256.h"
24 #include "crc/sha512.h"
26 #include "crc/xxhash.h"
28 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
29 struct verify_header *hdr, unsigned int header_num,
30 unsigned int header_len);
31 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
33 static void __fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
36 void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
38 fill_pattern(p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
41 void __fill_buffer(struct thread_options *o, unsigned long seed, void *p,
44 __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
47 unsigned long fill_buffer(struct thread_data *td, void *p, unsigned int len)
49 struct frand_state *fs = &td->verify_state;
50 struct thread_options *o = &td->o;
52 return fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
55 void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
56 struct io_u *io_u, unsigned long seed, int use_seed)
58 struct thread_options *o = &td->o;
60 if (!o->verify_pattern_bytes) {
61 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
64 __fill_buffer(o, seed, p, len);
66 io_u->rand_seed = fill_buffer(td, p, len);
70 if (io_u->buf_filled_len >= len) {
71 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
72 o->verify_pattern_bytes, len);
76 fill_pattern(p, len, o->verify_pattern, o->verify_pattern_bytes);
77 io_u->buf_filled_len = len;
80 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
84 hdr_inc = io_u->buflen;
85 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
86 hdr_inc = td->o.verify_interval;
91 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
92 unsigned long seed, int use_seed)
94 unsigned int hdr_inc, header_num;
95 struct verify_header *hdr;
98 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
100 hdr_inc = get_hdr_inc(td, io_u);
102 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
104 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
109 static void memswp(void *buf1, void *buf2, unsigned int len)
113 assert(len <= sizeof(swap));
115 memcpy(&swap, buf1, len);
116 memcpy(buf1, buf2, len);
117 memcpy(buf2, &swap, len);
120 static void hexdump(void *buffer, int len)
122 unsigned char *p = buffer;
125 for (i = 0; i < len; i++)
126 log_err("%02x", p[i]);
131 * Prepare for separation of verify_header and checksum header
133 static inline unsigned int __hdr_size(int verify_type)
135 unsigned int len = 0;
137 switch (verify_type) {
144 len = sizeof(struct vhdr_md5);
147 len = sizeof(struct vhdr_crc64);
151 case VERIFY_CRC32C_INTEL:
152 len = sizeof(struct vhdr_crc32);
155 len = sizeof(struct vhdr_crc16);
158 len = sizeof(struct vhdr_crc7);
161 len = sizeof(struct vhdr_sha256);
164 len = sizeof(struct vhdr_sha512);
167 len = sizeof(struct vhdr_xxhash);
170 len = sizeof(struct vhdr_meta);
173 len = sizeof(struct vhdr_sha1);
175 case VERIFY_PATTERN_NO_HDR:
178 log_err("fio: unknown verify header!\n");
182 return len + sizeof(struct verify_header);
185 static inline unsigned int hdr_size(struct thread_data *td,
186 struct verify_header *hdr)
188 if (td->o.verify == VERIFY_PATTERN_NO_HDR)
191 return __hdr_size(hdr->verify_type);
194 static void *hdr_priv(struct verify_header *hdr)
198 return priv + sizeof(struct verify_header);
202 * Verify container, pass info to verify handlers and allow them to
203 * pass info back in case of error
210 unsigned int hdr_num;
211 struct thread_data *td;
214 * Output, only valid in case of error
219 unsigned int crc_len;
222 #define DUMP_BUF_SZ 255
223 static int dump_buf_warned;
225 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
226 const char *type, struct fio_file *f)
228 char *ptr, fname[DUMP_BUF_SZ];
229 size_t buf_left = DUMP_BUF_SZ;
232 ptr = strdup(f->file_name);
234 fname[DUMP_BUF_SZ - 1] = '\0';
235 strncpy(fname, basename(ptr), DUMP_BUF_SZ - 1);
237 buf_left -= strlen(fname);
239 if (!dump_buf_warned) {
240 log_err("fio: verify failure dump buffer too small\n");
247 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
249 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
251 perror("open verify buf file");
256 ret = write(fd, buf, len);
260 perror("write verify buf file");
268 log_err(" %s data dumped as %s\n", type, fname);
273 * Dump the contents of the read block and re-generate the correct data
276 static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
278 struct thread_data *td = vc->td;
279 struct io_u *io_u = vc->io_u;
280 unsigned long hdr_offset;
284 if (!td->o.verify_dump)
288 * Dump the contents we just read off disk
290 hdr_offset = vc->hdr_num * hdr->len;
292 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
293 "received", vc->io_u->file);
296 * Allocate a new buf and re-generate the original data
298 buf = malloc(io_u->buflen);
301 dummy.rand_seed = hdr->rand_seed;
302 dummy.buf_filled_len = 0;
303 dummy.buflen = io_u->buflen;
305 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
307 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
308 "expected", vc->io_u->file);
312 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
314 struct thread_data *td = vc->td;
315 struct verify_header shdr;
317 if (td->o.verify == VERIFY_PATTERN_NO_HDR) {
318 __fill_hdr(&shdr, td->o.verify, vc->io_u->buflen, 0);
322 __dump_verify_buffers(hdr, vc);
325 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
327 unsigned long long offset;
329 offset = vc->io_u->offset;
330 offset += vc->hdr_num * hdr->len;
331 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
332 vc->name, vc->io_u->file->file_name, offset, hdr->len);
334 if (vc->good_crc && vc->bad_crc) {
335 log_err(" Expected CRC: ");
336 hexdump(vc->good_crc, vc->crc_len);
337 log_err(" Received CRC: ");
338 hexdump(vc->bad_crc, vc->crc_len);
341 dump_verify_buffers(hdr, vc);
345 * Return data area 'header_num'
347 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
349 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr);
352 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
354 struct thread_data *td = vc->td;
355 struct io_u *io_u = vc->io_u;
357 unsigned int header_size = __hdr_size(td->o.verify);
358 unsigned int len, mod, i, size, pattern_size;
360 pattern = td->o.verify_pattern;
361 pattern_size = td->o.verify_pattern_bytes;
362 if (pattern_size <= 1)
363 pattern_size = MAX_PATTERN_SIZE;
364 buf = (void *) hdr + header_size;
365 len = get_hdr_inc(td, io_u) - header_size;
366 mod = header_size % pattern_size;
368 for (i = 0; i < len; i += size) {
369 size = pattern_size - mod;
370 if (size > (len - i))
372 if (memcmp(buf + i, pattern + mod, size))
373 /* Let the slow compare find the first mismatch byte. */
378 for (; i < len; i++) {
379 if (buf[i] != pattern[mod]) {
382 bits = hweight8(buf[i] ^ pattern[mod]);
383 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
384 buf[i], pattern[mod], bits);
385 log_err("fio: bad pattern block offset %u\n", i);
386 dump_verify_buffers(hdr, vc);
390 if (mod == td->o.verify_pattern_bytes)
397 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
399 struct thread_data *td = vc->td;
400 struct vhdr_meta *vh = hdr_priv(hdr);
401 struct io_u *io_u = vc->io_u;
404 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
406 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
409 if (td->o.verify_pattern_bytes)
410 ret |= verify_io_u_pattern(hdr, vc);
413 * For read-only workloads, the program cannot be certain of the
414 * last numberio written to a block. Checking of numberio will be
415 * done only for workloads that write data. For verify_only,
416 * numberio will be checked in the last iteration when the correct
417 * state of numberio, that would have been written to each block
418 * in a previous run of fio, has been reached.
420 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
422 if (!td->o.verify_only || td->o.loops == 0)
423 if (vh->numberio != io_u->numberio)
430 log_verify_failure(hdr, vc);
434 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
436 void *p = io_u_verify_off(hdr, vc);
437 struct vhdr_xxhash *vh = hdr_priv(hdr);
441 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
443 state = XXH32_init(1);
444 XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr));
445 hash = XXH32_digest(state);
447 if (vh->hash == hash)
451 vc->good_crc = &vh->hash;
453 vc->crc_len = sizeof(hash);
454 log_verify_failure(hdr, vc);
458 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
460 void *p = io_u_verify_off(hdr, vc);
461 struct vhdr_sha512 *vh = hdr_priv(hdr);
463 struct fio_sha512_ctx sha512_ctx = {
467 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
469 fio_sha512_init(&sha512_ctx);
470 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr));
472 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
476 vc->good_crc = vh->sha512;
477 vc->bad_crc = sha512_ctx.buf;
478 vc->crc_len = sizeof(vh->sha512);
479 log_verify_failure(hdr, vc);
483 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
485 void *p = io_u_verify_off(hdr, vc);
486 struct vhdr_sha256 *vh = hdr_priv(hdr);
488 struct fio_sha256_ctx sha256_ctx = {
492 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
494 fio_sha256_init(&sha256_ctx);
495 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr));
496 fio_sha256_final(&sha256_ctx);
498 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
502 vc->good_crc = vh->sha256;
503 vc->bad_crc = sha256_ctx.buf;
504 vc->crc_len = sizeof(vh->sha256);
505 log_verify_failure(hdr, vc);
509 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
511 void *p = io_u_verify_off(hdr, vc);
512 struct vhdr_sha1 *vh = hdr_priv(hdr);
514 struct fio_sha1_ctx sha1_ctx = {
518 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
520 fio_sha1_init(&sha1_ctx);
521 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr));
522 fio_sha1_final(&sha1_ctx);
524 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
528 vc->good_crc = vh->sha1;
529 vc->bad_crc = sha1_ctx.H;
530 vc->crc_len = sizeof(vh->sha1);
531 log_verify_failure(hdr, vc);
535 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
537 void *p = io_u_verify_off(hdr, vc);
538 struct vhdr_crc7 *vh = hdr_priv(hdr);
541 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
543 c = fio_crc7(p, hdr->len - hdr_size(vc->td, hdr));
549 vc->good_crc = &vh->crc7;
552 log_verify_failure(hdr, vc);
556 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
558 void *p = io_u_verify_off(hdr, vc);
559 struct vhdr_crc16 *vh = hdr_priv(hdr);
562 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
564 c = fio_crc16(p, hdr->len - hdr_size(vc->td, hdr));
570 vc->good_crc = &vh->crc16;
573 log_verify_failure(hdr, vc);
577 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
579 void *p = io_u_verify_off(hdr, vc);
580 struct vhdr_crc64 *vh = hdr_priv(hdr);
581 unsigned long long c;
583 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
585 c = fio_crc64(p, hdr->len - hdr_size(vc->td, hdr));
591 vc->good_crc = &vh->crc64;
594 log_verify_failure(hdr, vc);
598 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
600 void *p = io_u_verify_off(hdr, vc);
601 struct vhdr_crc32 *vh = hdr_priv(hdr);
604 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
606 c = fio_crc32(p, hdr->len - hdr_size(vc->td, hdr));
612 vc->good_crc = &vh->crc32;
615 log_verify_failure(hdr, vc);
619 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
621 void *p = io_u_verify_off(hdr, vc);
622 struct vhdr_crc32 *vh = hdr_priv(hdr);
625 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
627 c = fio_crc32c(p, hdr->len - hdr_size(vc->td, hdr));
633 vc->good_crc = &vh->crc32;
636 log_verify_failure(hdr, vc);
640 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
642 void *p = io_u_verify_off(hdr, vc);
643 struct vhdr_md5 *vh = hdr_priv(hdr);
644 uint32_t hash[MD5_HASH_WORDS];
645 struct fio_md5_ctx md5_ctx = {
649 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
651 fio_md5_init(&md5_ctx);
652 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr));
653 fio_md5_final(&md5_ctx);
655 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
659 vc->good_crc = vh->md5_digest;
660 vc->bad_crc = md5_ctx.hash;
661 vc->crc_len = sizeof(hash);
662 log_verify_failure(hdr, vc);
667 * Push IO verification to a separate thread
669 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
671 struct io_u *io_u = *io_u_ptr;
673 pthread_mutex_lock(&td->io_u_lock);
676 put_file_log(td, io_u->file);
678 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
680 io_u_clear(io_u, IO_U_F_IN_CUR_DEPTH);
682 flist_add_tail(&io_u->verify_list, &td->verify_list);
684 pthread_mutex_unlock(&td->io_u_lock);
686 pthread_cond_signal(&td->verify_cond);
690 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
692 static char zero_buf[1024];
693 unsigned int this_len, len;
697 if (!td->o.trim_zero)
703 this_len = sizeof(zero_buf);
706 if (memcmp(p, zero_buf, this_len)) {
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) (p - io_u->buf));
724 static int verify_header(struct io_u *io_u, struct verify_header *hdr,
725 unsigned int hdr_num, unsigned int hdr_len)
730 if (hdr->magic != FIO_HDR_MAGIC) {
731 log_err("verify: bad magic header %x, wanted %x",
732 hdr->magic, FIO_HDR_MAGIC);
735 if (hdr->len != hdr_len) {
736 log_err("verify: bad header length %u, wanted %u",
740 if (hdr->rand_seed != io_u->rand_seed) {
741 log_err("verify: bad header rand_seed %"PRIu64
743 hdr->rand_seed, io_u->rand_seed);
747 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
748 if (crc != hdr->crc32) {
749 log_err("verify: bad header crc %x, calculated %x",
756 log_err(" at file %s offset %llu, length %u\n",
757 io_u->file->file_name,
758 io_u->offset + hdr_num * hdr_len, hdr_len);
762 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
764 struct verify_header *hdr;
765 struct io_u *io_u = *io_u_ptr;
766 unsigned int header_size, hdr_inc, hdr_num = 0;
770 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
773 * If the IO engine is faking IO (like null), then just pretend
774 * we verified everything.
776 if (td->io_ops->flags & FIO_FAKEIO)
779 if (io_u->flags & IO_U_F_TRIMMED) {
780 ret = verify_trimmed_io_u(td, io_u);
784 hdr_inc = get_hdr_inc(td, io_u);
787 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
788 p += hdr_inc, hdr_num++) {
794 unsigned int verify_type;
796 if (ret && td->o.verify_fatal)
799 header_size = __hdr_size(td->o.verify);
800 if (td->o.verify_offset)
801 memswp(p, p + td->o.verify_offset, header_size);
805 * Make rand_seed check pass when have verifysort or
808 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
809 io_u->rand_seed = hdr->rand_seed;
811 if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
812 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
817 if (td->o.verify != VERIFY_NONE)
818 verify_type = td->o.verify;
820 verify_type = hdr->verify_type;
822 switch (verify_type) {
824 ret = verify_io_u_md5(hdr, &vc);
827 ret = verify_io_u_crc64(hdr, &vc);
830 case VERIFY_CRC32C_INTEL:
831 ret = verify_io_u_crc32c(hdr, &vc);
834 ret = verify_io_u_crc32(hdr, &vc);
837 ret = verify_io_u_crc16(hdr, &vc);
840 ret = verify_io_u_crc7(hdr, &vc);
843 ret = verify_io_u_sha256(hdr, &vc);
846 ret = verify_io_u_sha512(hdr, &vc);
849 ret = verify_io_u_xxhash(hdr, &vc);
852 ret = verify_io_u_meta(hdr, &vc);
855 ret = verify_io_u_sha1(hdr, &vc);
858 case VERIFY_PATTERN_NO_HDR:
859 ret = verify_io_u_pattern(hdr, &vc);
862 log_err("Bad verify type %u\n", hdr->verify_type);
866 if (ret && verify_type != hdr->verify_type)
867 log_err("fio: verify type mismatch (%u media, %u given)\n",
868 hdr->verify_type, verify_type);
872 if (ret && td->o.verify_fatal)
873 fio_mark_td_terminate(td);
878 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
879 struct io_u *io_u, unsigned int header_num)
881 struct vhdr_meta *vh = hdr_priv(hdr);
883 vh->thread = td->thread_number;
885 vh->time_sec = io_u->start_time.tv_sec;
886 vh->time_usec = io_u->start_time.tv_usec;
888 vh->numberio = io_u->numberio;
890 vh->offset = io_u->offset + header_num * td->o.verify_interval;
893 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
895 struct vhdr_xxhash *vh = hdr_priv(hdr);
898 state = XXH32_init(1);
899 XXH32_update(state, p, len);
900 vh->hash = XXH32_digest(state);
903 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
905 struct vhdr_sha512 *vh = hdr_priv(hdr);
906 struct fio_sha512_ctx sha512_ctx = {
910 fio_sha512_init(&sha512_ctx);
911 fio_sha512_update(&sha512_ctx, p, len);
914 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
916 struct vhdr_sha256 *vh = hdr_priv(hdr);
917 struct fio_sha256_ctx sha256_ctx = {
921 fio_sha256_init(&sha256_ctx);
922 fio_sha256_update(&sha256_ctx, p, len);
923 fio_sha256_final(&sha256_ctx);
926 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
928 struct vhdr_sha1 *vh = hdr_priv(hdr);
929 struct fio_sha1_ctx sha1_ctx = {
933 fio_sha1_init(&sha1_ctx);
934 fio_sha1_update(&sha1_ctx, p, len);
935 fio_sha1_final(&sha1_ctx);
938 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
940 struct vhdr_crc7 *vh = hdr_priv(hdr);
942 vh->crc7 = fio_crc7(p, len);
945 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
947 struct vhdr_crc16 *vh = hdr_priv(hdr);
949 vh->crc16 = fio_crc16(p, len);
952 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
954 struct vhdr_crc32 *vh = hdr_priv(hdr);
956 vh->crc32 = fio_crc32(p, len);
959 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
961 struct vhdr_crc32 *vh = hdr_priv(hdr);
963 vh->crc32 = fio_crc32c(p, len);
966 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
968 struct vhdr_crc64 *vh = hdr_priv(hdr);
970 vh->crc64 = fio_crc64(p, len);
973 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
975 struct vhdr_md5 *vh = hdr_priv(hdr);
976 struct fio_md5_ctx md5_ctx = {
977 .hash = (uint32_t *) vh->md5_digest,
980 fio_md5_init(&md5_ctx);
981 fio_md5_update(&md5_ctx, p, len);
982 fio_md5_final(&md5_ctx);
985 static void __fill_hdr(struct verify_header *hdr, int verify_type,
986 uint32_t len, uint64_t rand_seed)
990 hdr->magic = FIO_HDR_MAGIC;
991 hdr->verify_type = verify_type;
993 hdr->rand_seed = rand_seed;
994 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
998 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
1001 if (verify_type != VERIFY_PATTERN_NO_HDR)
1002 __fill_hdr(hdr, verify_type, len, rand_seed);
1005 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
1006 struct verify_header *hdr, unsigned int header_num,
1007 unsigned int header_len)
1009 unsigned int data_len;
1014 fill_hdr(hdr, td->o.verify, header_len, io_u->rand_seed);
1016 data_len = header_len - hdr_size(td, hdr);
1018 data = p + hdr_size(td, hdr);
1019 switch (td->o.verify) {
1021 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1023 fill_md5(hdr, data, data_len);
1026 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1028 fill_crc64(hdr, data, data_len);
1031 case VERIFY_CRC32C_INTEL:
1032 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1034 fill_crc32c(hdr, data, data_len);
1037 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1039 fill_crc32(hdr, data, data_len);
1042 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1044 fill_crc16(hdr, data, data_len);
1047 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1049 fill_crc7(hdr, data, data_len);
1052 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1054 fill_sha256(hdr, data, data_len);
1057 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1059 fill_sha512(hdr, data, data_len);
1062 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1064 fill_xxhash(hdr, data, data_len);
1067 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1069 fill_meta(hdr, td, io_u, header_num);
1072 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1074 fill_sha1(hdr, data, data_len);
1076 case VERIFY_PATTERN:
1077 case VERIFY_PATTERN_NO_HDR:
1078 /* nothing to do here */
1081 log_err("fio: bad verify type: %d\n", td->o.verify);
1085 if (td->o.verify_offset && hdr_size(td, hdr))
1086 memswp(p, p + td->o.verify_offset, hdr_size(td, hdr));
1090 * fill body of io_u->buf with random data and add a header with the
1091 * checksum of choice
1093 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1095 if (td->o.verify == VERIFY_NULL)
1098 io_u->numberio = td->io_issues[io_u->ddir];
1100 fill_pattern_headers(td, io_u, 0, 0);
1103 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1105 struct io_piece *ipo = NULL;
1108 * this io_u is from a requeue, we already filled the offsets
1113 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1114 struct rb_node *n = rb_first(&td->io_hist_tree);
1116 ipo = rb_entry(n, struct io_piece, rb_node);
1119 * Ensure that the associated IO has completed
1122 if (ipo->flags & IP_F_IN_FLIGHT)
1125 rb_erase(n, &td->io_hist_tree);
1126 assert(ipo->flags & IP_F_ONRB);
1127 ipo->flags &= ~IP_F_ONRB;
1128 } else if (!flist_empty(&td->io_hist_list)) {
1129 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1132 * Ensure that the associated IO has completed
1135 if (ipo->flags & IP_F_IN_FLIGHT)
1138 flist_del(&ipo->list);
1139 assert(ipo->flags & IP_F_ONLIST);
1140 ipo->flags &= ~IP_F_ONLIST;
1146 io_u->offset = ipo->offset;
1147 io_u->buflen = ipo->len;
1148 io_u->numberio = ipo->numberio;
1149 io_u->file = ipo->file;
1150 io_u_set(io_u, IO_U_F_VER_LIST);
1152 if (ipo->flags & IP_F_TRIMMED)
1153 io_u_set(io_u, IO_U_F_TRIMMED);
1155 if (!fio_file_open(io_u->file)) {
1156 int r = td_io_open_file(td, io_u->file);
1159 dprint(FD_VERIFY, "failed file %s open\n",
1160 io_u->file->file_name);
1165 get_file(ipo->file);
1166 assert(fio_file_open(io_u->file));
1167 io_u->ddir = DDIR_READ;
1168 io_u->xfer_buf = io_u->buf;
1169 io_u->xfer_buflen = io_u->buflen;
1171 remove_trim_entry(td, ipo);
1173 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1175 if (!td->o.verify_pattern_bytes) {
1176 io_u->rand_seed = __rand(&td->verify_state);
1177 if (sizeof(int) != sizeof(long *))
1178 io_u->rand_seed *= __rand(&td->verify_state);
1184 dprint(FD_VERIFY, "get_next_verify: empty\n");
1188 void fio_verify_init(struct thread_data *td)
1190 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1191 td->o.verify == VERIFY_CRC32C) {
1192 crc32c_intel_probe();
1196 static void *verify_async_thread(void *data)
1198 struct thread_data *td = data;
1202 if (fio_option_is_set(&td->o, verify_cpumask) &&
1203 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1204 log_err("fio: failed setting verify thread affinity\n");
1212 if (td->verify_thread_exit)
1215 pthread_mutex_lock(&td->io_u_lock);
1217 while (flist_empty(&td->verify_list) &&
1218 !td->verify_thread_exit) {
1219 ret = pthread_cond_wait(&td->verify_cond,
1222 pthread_mutex_unlock(&td->io_u_lock);
1227 flist_splice_init(&td->verify_list, &list);
1228 pthread_mutex_unlock(&td->io_u_lock);
1230 if (flist_empty(&list))
1233 while (!flist_empty(&list)) {
1234 io_u = flist_first_entry(&list, struct io_u, verify_list);
1235 flist_del_init(&io_u->verify_list);
1237 io_u_set(io_u, IO_U_F_NO_FILE_PUT);
1238 ret = verify_io_u(td, &io_u);
1243 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1244 update_error_count(td, ret);
1252 td_verror(td, ret, "async_verify");
1253 if (td->o.verify_fatal)
1254 fio_mark_td_terminate(td);
1258 pthread_mutex_lock(&td->io_u_lock);
1259 td->nr_verify_threads--;
1260 pthread_mutex_unlock(&td->io_u_lock);
1262 pthread_cond_signal(&td->free_cond);
1266 int verify_async_init(struct thread_data *td)
1269 pthread_attr_t attr;
1271 pthread_attr_init(&attr);
1272 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1274 td->verify_thread_exit = 0;
1276 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1277 for (i = 0; i < td->o.verify_async; i++) {
1278 ret = pthread_create(&td->verify_threads[i], &attr,
1279 verify_async_thread, td);
1281 log_err("fio: async verify creation failed: %s\n",
1285 ret = pthread_detach(td->verify_threads[i]);
1287 log_err("fio: async verify thread detach failed: %s\n",
1291 td->nr_verify_threads++;
1294 pthread_attr_destroy(&attr);
1296 if (i != td->o.verify_async) {
1297 log_err("fio: only %d verify threads started, exiting\n", i);
1298 td->verify_thread_exit = 1;
1300 pthread_cond_broadcast(&td->verify_cond);
1307 void verify_async_exit(struct thread_data *td)
1309 td->verify_thread_exit = 1;
1311 pthread_cond_broadcast(&td->verify_cond);
1313 pthread_mutex_lock(&td->io_u_lock);
1315 while (td->nr_verify_threads)
1316 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1318 pthread_mutex_unlock(&td->io_u_lock);
1319 free(td->verify_threads);
1320 td->verify_threads = NULL;
1323 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1325 struct all_io_list *rep;
1326 struct thread_data *td;
1331 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1334 * Calculate reply space needed. We need one 'io_state' per thread,
1335 * and the size will vary depending on depth.
1339 for_each_td(td, i) {
1340 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1343 td->flags |= TD_F_VSTATE_SAVED;
1344 depth += td->o.iodepth;
1352 *sz += nr * sizeof(struct thread_io_list);
1353 *sz += depth * sizeof(uint64_t);
1356 rep->threads = cpu_to_le64((uint64_t) nr);
1358 next = &rep->state[0];
1359 for_each_td(td, i) {
1360 struct thread_io_list *s = next;
1363 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1366 if (td->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 k = td->last_write_idx - 1;
1375 for (j = 0; j < comps; j++) {
1377 k = td->o.iodepth - 1;
1378 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1384 s->no_comps = cpu_to_le64((uint64_t) comps);
1385 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1386 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1387 s->index = cpu_to_le64((uint64_t) i);
1388 if (td->random_state.use64) {
1389 s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
1390 s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
1391 s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
1392 s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
1393 s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
1394 s->rand.state64.s[5] = 0;
1395 s->rand.use64 = cpu_to_le64((uint64_t)1);
1397 s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
1398 s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
1399 s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
1400 s->rand.state32.s[3] = 0;
1403 s->name[sizeof(s->name) - 1] = '\0';
1404 strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
1405 next = io_list_next(s);
1411 static int open_state_file(const char *name, const char *prefix, int num,
1419 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1423 verify_state_gen_name(out, sizeof(out), name, prefix, num);
1425 fd = open(out, flags, 0644);
1427 perror("fio: open state file");
1434 static int write_thread_list_state(struct thread_io_list *s,
1437 struct verify_state_hdr hdr;
1442 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1446 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1448 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1449 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1450 hdr.crc = cpu_to_le64(crc);
1451 ret = write(fd, &hdr, sizeof(hdr));
1452 if (ret != sizeof(hdr))
1455 ret = write(fd, s, thread_io_list_sz(s));
1456 if (ret != thread_io_list_sz(s)) {
1459 perror("fio: write state file");
1460 log_err("fio: failed to write state file\n");
1469 void __verify_save_state(struct all_io_list *state, const char *prefix)
1471 struct thread_io_list *s = &state->state[0];
1474 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1475 write_thread_list_state(s, prefix);
1476 s = io_list_next(s);
1480 void verify_save_state(void)
1482 struct all_io_list *state;
1485 state = get_all_io_list(IO_LIST_ALL, &sz);
1487 __verify_save_state(state, "local");
1492 void verify_free_state(struct thread_data *td)
1498 static struct thread_io_list *convert_v1_list(struct thread_io_list_v1 *s)
1500 struct thread_io_list *til;
1503 til = malloc(__thread_io_list_sz(s->no_comps));
1504 til->no_comps = s->no_comps;
1505 til->depth = s->depth;
1506 til->numberio = s->numberio;
1507 til->index = s->index;
1508 memcpy(til->name, s->name, sizeof(til->name));
1510 til->rand.use64 = 0;
1511 for (i = 0; i < 4; i++)
1512 til->rand.state32.s[i] = s->rand.s[i];
1514 for (i = 0; i < s->no_comps; i++)
1515 til->offsets[i] = s->offsets[i];
1520 void verify_convert_assign_state(struct thread_data *td, void *p, int version)
1522 struct thread_io_list *til;
1526 struct thread_io_list_v1 *s = p;
1528 s->no_comps = le64_to_cpu(s->no_comps);
1529 s->depth = le64_to_cpu(s->depth);
1530 s->numberio = le64_to_cpu(s->numberio);
1531 for (i = 0; i < 4; i++)
1532 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1533 for (i = 0; i < s->no_comps; i++)
1534 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1536 til = convert_v1_list(s);
1539 struct thread_io_list *s = p;
1541 s->no_comps = le64_to_cpu(s->no_comps);
1542 s->depth = le64_to_cpu(s->depth);
1543 s->numberio = le64_to_cpu(s->numberio);
1544 s->rand.use64 = le64_to_cpu(s->rand.use64);
1546 if (s->rand.use64) {
1547 for (i = 0; i < 6; i++)
1548 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
1550 for (i = 0; i < 4; i++)
1551 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1553 for (i = 0; i < s->no_comps; i++)
1554 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1562 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s,
1567 hdr->version = le64_to_cpu(hdr->version);
1568 hdr->size = le64_to_cpu(hdr->size);
1569 hdr->crc = le64_to_cpu(hdr->crc);
1571 if (hdr->version != VSTATE_HDR_VERSION ||
1572 hdr->version != VSTATE_HDR_VERSION_V1)
1575 crc = fio_crc32c((void *)s, hdr->size);
1576 if (crc != hdr->crc)
1579 *version = hdr->version;
1583 int verify_load_state(struct thread_data *td, const char *prefix)
1585 struct verify_state_hdr hdr;
1591 if (!td->o.verify_state)
1594 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1598 ret = read(fd, &hdr, sizeof(hdr));
1599 if (ret != sizeof(hdr)) {
1601 td_verror(td, errno, "read verify state hdr");
1602 log_err("fio: failed reading verify state header\n");
1606 hdr.version = le64_to_cpu(hdr.version);
1607 hdr.size = le64_to_cpu(hdr.size);
1608 hdr.crc = le64_to_cpu(hdr.crc);
1610 if (hdr.version != VSTATE_HDR_VERSION &&
1611 hdr.version != VSTATE_HDR_VERSION_V1) {
1612 log_err("fio: bad version in verify state header\n");
1616 s = malloc(hdr.size);
1617 ret = read(fd, s, hdr.size);
1618 if (ret != hdr.size) {
1620 td_verror(td, errno, "read verify state");
1621 log_err("fio: failed reading verity state\n");
1625 crc = fio_crc32c(s, hdr.size);
1626 if (crc != hdr.crc) {
1627 log_err("fio: verify state is corrupt\n");
1633 verify_convert_assign_state(td, s, hdr.version);
1643 * Use the loaded verify state to know when to stop doing verification
1645 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1647 struct thread_io_list *s = td->vstate;
1654 * If we're not into the window of issues - depth yet, continue. If
1655 * issue is shorter than depth, do check.
1657 if ((td->io_blocks[DDIR_READ] < s->depth ||
1658 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1659 s->numberio > s->depth)
1663 * We're in the window of having to check if this io was
1664 * completed or not. If the IO was seen as completed, then
1667 for (i = 0; i < s->no_comps; i++)
1668 if (io_u->offset == s->offsets[i])
1672 * Not found, we have to stop