2 * IO verification helpers
17 #include "crc/crc64.h"
18 #include "crc/crc32.h"
19 #include "crc/crc32c.h"
20 #include "crc/crc16.h"
22 #include "crc/sha256.h"
23 #include "crc/sha512.h"
26 void fill_pattern(struct thread_data *td, void *p, unsigned int len, struct io_u *io_u)
28 switch (td->o.verify_pattern_bytes) {
30 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
31 fill_random_buf(p, len);
34 if (io_u->buf_filled_len >= len) {
35 dprint(FD_VERIFY, "using already filled verify pattern b=0 len=%u\n", len);
38 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
39 memset(p, td->o.verify_pattern[0], len);
40 io_u->buf_filled_len = len;
43 unsigned int i = 0, size = 0;
46 if (io_u->buf_filled_len >= len) {
47 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
48 td->o.verify_pattern_bytes, len);
51 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
52 td->o.verify_pattern_bytes, len);
55 size = td->o.verify_pattern_bytes;
58 memcpy(b+i, td->o.verify_pattern, size);
61 io_u->buf_filled_len = len;
67 static void memswp(void *buf1, void *buf2, unsigned int len)
71 assert(len <= sizeof(swap));
73 memcpy(&swap, buf1, len);
74 memcpy(buf1, buf2, len);
75 memcpy(buf2, &swap, len);
78 static void hexdump(void *buffer, int len)
80 unsigned char *p = buffer;
83 for (i = 0; i < len; i++)
84 log_err("%02x", p[i]);
89 * Prepare for seperation of verify_header and checksum header
91 static inline unsigned int __hdr_size(int verify_type)
93 unsigned int len = len;
95 switch (verify_type) {
101 len = sizeof(struct vhdr_md5);
104 len = sizeof(struct vhdr_crc64);
108 case VERIFY_CRC32C_INTEL:
109 len = sizeof(struct vhdr_crc32);
112 len = sizeof(struct vhdr_crc16);
115 len = sizeof(struct vhdr_crc7);
118 len = sizeof(struct vhdr_sha256);
121 len = sizeof(struct vhdr_sha512);
124 len = sizeof(struct vhdr_meta);
127 len = sizeof(struct vhdr_sha1);
130 log_err("fio: unknown verify header!\n");
134 return len + sizeof(struct verify_header);
137 static inline unsigned int hdr_size(struct verify_header *hdr)
139 return __hdr_size(hdr->verify_type);
142 static void *hdr_priv(struct verify_header *hdr)
146 return priv + sizeof(struct verify_header);
150 * Verify container, pass info to verify handlers and allow them to
151 * pass info back in case of error
158 unsigned int hdr_num;
161 * Output, only valid in case of error
166 unsigned int crc_len;
169 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
171 unsigned long long offset;
173 offset = vc->io_u->offset;
174 offset += vc->hdr_num * hdr->len;
175 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
176 vc->name, vc->io_u->file->file_name, offset, hdr->len);
178 if (vc->good_crc && vc->bad_crc) {
179 log_err(" Expected CRC: ");
180 hexdump(vc->good_crc, vc->crc_len);
181 log_err(" Received CRC: ");
182 hexdump(vc->bad_crc, vc->crc_len);
187 * Return data area 'header_num'
189 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
191 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
194 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
197 struct vhdr_meta *vh = hdr_priv(hdr);
198 struct io_u *io_u = vc->io_u;
200 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
202 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
206 log_verify_failure(hdr, vc);
210 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
212 void *p = io_u_verify_off(hdr, vc);
213 struct vhdr_sha512 *vh = hdr_priv(hdr);
215 struct sha512_ctx sha512_ctx = {
219 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
221 sha512_init(&sha512_ctx);
222 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
224 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
228 vc->good_crc = vh->sha512;
229 vc->bad_crc = sha512_ctx.buf;
230 vc->crc_len = sizeof(vh->sha512);
231 log_verify_failure(hdr, vc);
235 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
237 void *p = io_u_verify_off(hdr, vc);
238 struct vhdr_sha256 *vh = hdr_priv(hdr);
240 struct sha256_ctx sha256_ctx = {
244 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
246 sha256_init(&sha256_ctx);
247 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
249 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
253 vc->good_crc = vh->sha256;
254 vc->bad_crc = sha256_ctx.buf;
255 vc->crc_len = sizeof(vh->sha256);
256 log_verify_failure(hdr, vc);
260 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
262 void *p = io_u_verify_off(hdr, vc);
263 struct vhdr_sha1 *vh = hdr_priv(hdr);
265 struct sha1_ctx sha1_ctx = {
269 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
271 sha1_init(&sha1_ctx);
272 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
274 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
278 vc->good_crc = vh->sha1;
279 vc->bad_crc = sha1_ctx.H;
280 vc->crc_len = sizeof(vh->sha1);
281 log_verify_failure(hdr, vc);
285 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
287 void *p = io_u_verify_off(hdr, vc);
288 struct vhdr_crc7 *vh = hdr_priv(hdr);
291 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
293 c = crc7(p, hdr->len - hdr_size(hdr));
299 vc->good_crc = &vh->crc7;
302 log_verify_failure(hdr, vc);
306 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
308 void *p = io_u_verify_off(hdr, vc);
309 struct vhdr_crc16 *vh = hdr_priv(hdr);
312 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
314 c = crc16(p, hdr->len - hdr_size(hdr));
320 vc->good_crc = &vh->crc16;
323 log_verify_failure(hdr, vc);
327 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
329 void *p = io_u_verify_off(hdr, vc);
330 struct vhdr_crc64 *vh = hdr_priv(hdr);
331 unsigned long long c;
333 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
335 c = crc64(p, hdr->len - hdr_size(hdr));
341 vc->good_crc = &vh->crc64;
344 log_verify_failure(hdr, vc);
348 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
350 void *p = io_u_verify_off(hdr, vc);
351 struct vhdr_crc32 *vh = hdr_priv(hdr);
354 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
356 c = crc32(p, hdr->len - hdr_size(hdr));
362 vc->good_crc = &vh->crc32;
365 log_verify_failure(hdr, vc);
369 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
371 void *p = io_u_verify_off(hdr, vc);
372 struct vhdr_crc32 *vh = hdr_priv(hdr);
375 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
377 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
378 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
380 c = crc32c(p, hdr->len - hdr_size(hdr));
386 vc->good_crc = &vh->crc32;
389 log_verify_failure(hdr, vc);
393 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
395 void *p = io_u_verify_off(hdr, vc);
396 struct vhdr_md5 *vh = hdr_priv(hdr);
397 uint32_t hash[MD5_HASH_WORDS];
398 struct md5_ctx md5_ctx = {
402 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
405 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
407 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
411 vc->good_crc = vh->md5_digest;
412 vc->bad_crc = md5_ctx.hash;
413 vc->crc_len = sizeof(hash);
414 log_verify_failure(hdr, vc);
418 static unsigned int hweight8(unsigned int w)
420 unsigned int res = w - ((w >> 1) & 0x55);
422 res = (res & 0x33) + ((res >> 2) & 0x33);
423 return (res + (res >> 4)) & 0x0F;
426 int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
427 char *buf, unsigned int len, unsigned int mod)
431 for (i = 0; i < len; i++) {
432 if (buf[i] != pattern[mod]) {
435 bits = hweight8(buf[i] ^ pattern[mod]);
436 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
437 buf[i], pattern[mod], bits);
438 log_err("fio: bad pattern block offset %u\n", i);
442 if (mod == pattern_size)
450 * Push IO verification to a separate thread
452 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
455 put_file_log(td, io_u->file);
459 pthread_mutex_lock(&td->io_u_lock);
461 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
463 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
465 flist_del(&io_u->list);
466 flist_add_tail(&io_u->list, &td->verify_list);
467 io_u->flags |= IO_U_F_FREE_DEF;
468 pthread_mutex_unlock(&td->io_u_lock);
470 pthread_cond_signal(&td->verify_cond);
474 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
476 static char zero_buf[1024];
477 unsigned int this_len, len;
481 if (!td->o.trim_zero)
487 this_len = sizeof(zero_buf);
490 if (memcmp(p, zero_buf, this_len)) {
501 log_err("trim: verify failed at file %s offset %llu, length %lu"
502 ", block offset %lu\n",
503 io_u->file->file_name, io_u->offset, io_u->buflen,
504 (unsigned long) (p - io_u->buf));
508 int verify_io_u(struct thread_data *td, struct io_u *io_u)
510 struct verify_header *hdr;
511 unsigned int hdr_size, hdr_inc, hdr_num = 0;
515 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
517 if (io_u->flags & IO_U_F_TRIMMED) {
518 ret = verify_trimmed_io_u(td, io_u);
522 hdr_inc = io_u->buflen;
523 if (td->o.verify_interval)
524 hdr_inc = td->o.verify_interval;
527 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
528 p += hdr_inc, hdr_num++) {
534 if (ret && td->o.verify_fatal)
537 hdr_size = __hdr_size(td->o.verify);
538 if (td->o.verify_offset)
539 memswp(p, p + td->o.verify_offset, hdr_size);
542 if (hdr->fio_magic != FIO_HDR_MAGIC) {
543 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
544 hdr->fio_magic, FIO_HDR_MAGIC,
545 io_u->file->file_name,
546 io_u->offset + hdr_num * hdr->len, hdr->len);
550 if (td->o.verify_pattern_bytes) {
551 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
553 ret = verify_io_u_pattern(td->o.verify_pattern,
554 td->o.verify_pattern_bytes,
557 hdr_size % td->o.verify_pattern_bytes);
560 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
561 io_u->file->file_name,
562 io_u->offset + hdr_num * hdr->len,
567 * Also verify the meta data, if applicable
569 if (hdr->verify_type == VERIFY_META)
570 ret |= verify_io_u_meta(hdr, td, &vc);
574 switch (hdr->verify_type) {
576 ret = verify_io_u_md5(hdr, &vc);
579 ret = verify_io_u_crc64(hdr, &vc);
582 case VERIFY_CRC32C_INTEL:
583 ret = verify_io_u_crc32c(hdr, &vc);
586 ret = verify_io_u_crc32(hdr, &vc);
589 ret = verify_io_u_crc16(hdr, &vc);
592 ret = verify_io_u_crc7(hdr, &vc);
595 ret = verify_io_u_sha256(hdr, &vc);
598 ret = verify_io_u_sha512(hdr, &vc);
601 ret = verify_io_u_meta(hdr, td, &vc);
604 ret = verify_io_u_sha1(hdr, &vc);
607 log_err("Bad verify type %u\n", hdr->verify_type);
613 if (ret && td->o.verify_fatal)
619 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
620 struct io_u *io_u, unsigned int header_num)
622 struct vhdr_meta *vh = hdr_priv(hdr);
624 vh->thread = td->thread_number;
626 vh->time_sec = io_u->start_time.tv_sec;
627 vh->time_usec = io_u->start_time.tv_usec;
629 vh->numberio = td->io_issues[DDIR_WRITE];
631 vh->offset = io_u->offset + header_num * td->o.verify_interval;
634 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
636 struct vhdr_sha512 *vh = hdr_priv(hdr);
637 struct sha512_ctx sha512_ctx = {
641 sha512_init(&sha512_ctx);
642 sha512_update(&sha512_ctx, p, len);
645 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
647 struct vhdr_sha256 *vh = hdr_priv(hdr);
648 struct sha256_ctx sha256_ctx = {
652 sha256_init(&sha256_ctx);
653 sha256_update(&sha256_ctx, p, len);
656 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
658 struct vhdr_sha1 *vh = hdr_priv(hdr);
659 struct sha1_ctx sha1_ctx = {
663 sha1_init(&sha1_ctx);
664 sha1_update(&sha1_ctx, p, len);
667 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
669 struct vhdr_crc7 *vh = hdr_priv(hdr);
671 vh->crc7 = crc7(p, len);
674 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
676 struct vhdr_crc16 *vh = hdr_priv(hdr);
678 vh->crc16 = crc16(p, len);
681 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
683 struct vhdr_crc32 *vh = hdr_priv(hdr);
685 vh->crc32 = crc32(p, len);
688 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
690 struct vhdr_crc32 *vh = hdr_priv(hdr);
692 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
693 vh->crc32 = crc32c_intel(p, len);
695 vh->crc32 = crc32c(p, len);
698 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
700 struct vhdr_crc64 *vh = hdr_priv(hdr);
702 vh->crc64 = crc64(p, len);
705 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
707 struct vhdr_md5 *vh = hdr_priv(hdr);
708 struct md5_ctx md5_ctx = {
709 .hash = (uint32_t *) vh->md5_digest,
713 md5_update(&md5_ctx, p, len);
717 * fill body of io_u->buf with random data and add a header with the
718 * crc32 or md5 sum of that data.
720 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
722 struct verify_header *hdr;
723 void *p = io_u->buf, *data;
724 unsigned int hdr_inc, data_len, header_num = 0;
726 if (td->o.verify == VERIFY_NULL)
729 fill_pattern(td, p, io_u->buflen, io_u);
731 hdr_inc = io_u->buflen;
732 if (td->o.verify_interval)
733 hdr_inc = td->o.verify_interval;
735 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
738 hdr->fio_magic = FIO_HDR_MAGIC;
739 hdr->verify_type = td->o.verify;
741 data_len = hdr_inc - hdr_size(hdr);
743 data = p + hdr_size(hdr);
744 switch (td->o.verify) {
746 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
748 fill_md5(hdr, data, data_len);
751 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
753 fill_crc64(hdr, data, data_len);
756 case VERIFY_CRC32C_INTEL:
757 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
759 fill_crc32c(hdr, data, data_len);
762 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
764 fill_crc32(hdr, data, data_len);
767 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
769 fill_crc16(hdr, data, data_len);
772 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
774 fill_crc7(hdr, data, data_len);
777 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
779 fill_sha256(hdr, data, data_len);
782 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
784 fill_sha512(hdr, data, data_len);
787 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
789 fill_meta(hdr, td, io_u, header_num);
792 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
794 fill_sha1(hdr, data, data_len);
797 log_err("fio: bad verify type: %d\n", td->o.verify);
800 if (td->o.verify_offset)
801 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
806 int get_next_verify(struct thread_data *td, struct io_u *io_u)
808 struct io_piece *ipo = NULL;
811 * this io_u is from a requeue, we already filled the offsets
816 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
817 struct rb_node *n = rb_first(&td->io_hist_tree);
819 ipo = rb_entry(n, struct io_piece, rb_node);
820 rb_erase(n, &td->io_hist_tree);
821 assert(ipo->flags & IP_F_ONRB);
822 ipo->flags &= ~IP_F_ONRB;
823 } else if (!flist_empty(&td->io_hist_list)) {
824 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
825 flist_del(&ipo->list);
826 assert(ipo->flags & IP_F_ONLIST);
827 ipo->flags &= ~IP_F_ONLIST;
833 io_u->offset = ipo->offset;
834 io_u->buflen = ipo->len;
835 io_u->file = ipo->file;
837 if (ipo->flags & IP_F_TRIMMED)
838 io_u->flags |= IO_U_F_TRIMMED;
840 if (!fio_file_open(io_u->file)) {
841 int r = td_io_open_file(td, io_u->file);
844 dprint(FD_VERIFY, "failed file %s open\n",
845 io_u->file->file_name);
851 assert(fio_file_open(io_u->file));
852 io_u->ddir = DDIR_READ;
853 io_u->xfer_buf = io_u->buf;
854 io_u->xfer_buflen = io_u->buflen;
856 remove_trim_entry(td, ipo);
858 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
862 dprint(FD_VERIFY, "get_next_verify: empty\n");
866 static void *verify_async_thread(void *data)
868 struct thread_data *td = data;
872 if (td->o.verify_cpumask_set &&
873 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
874 log_err("fio: failed setting verify thread affinity\n");
882 if (td->verify_thread_exit)
885 pthread_mutex_lock(&td->io_u_lock);
887 while (flist_empty(&td->verify_list) &&
888 !td->verify_thread_exit) {
889 ret = pthread_cond_wait(&td->verify_cond,
892 pthread_mutex_unlock(&td->io_u_lock);
897 flist_splice_init(&td->verify_list, &list);
898 pthread_mutex_unlock(&td->io_u_lock);
900 if (flist_empty(&list))
903 while (!flist_empty(&list)) {
904 io_u = flist_entry(list.next, struct io_u, list);
905 flist_del_init(&io_u->list);
907 ret = verify_io_u(td, io_u);
911 if (td->o.continue_on_error &&
912 td_non_fatal_error(ret)) {
913 update_error_count(td, ret);
921 td_verror(td, ret, "async_verify");
922 if (td->o.verify_fatal)
927 pthread_mutex_lock(&td->io_u_lock);
928 td->nr_verify_threads--;
929 pthread_mutex_unlock(&td->io_u_lock);
931 pthread_cond_signal(&td->free_cond);
935 int verify_async_init(struct thread_data *td)
940 pthread_attr_init(&attr);
941 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
943 td->verify_thread_exit = 0;
945 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
946 for (i = 0; i < td->o.verify_async; i++) {
947 ret = pthread_create(&td->verify_threads[i], &attr,
948 verify_async_thread, td);
950 log_err("fio: async verify creation failed: %s\n",
954 ret = pthread_detach(td->verify_threads[i]);
956 log_err("fio: async verify thread detach failed: %s\n",
960 td->nr_verify_threads++;
963 pthread_attr_destroy(&attr);
965 if (i != td->o.verify_async) {
966 log_err("fio: only %d verify threads started, exiting\n", i);
967 td->verify_thread_exit = 1;
969 pthread_cond_broadcast(&td->verify_cond);
976 void verify_async_exit(struct thread_data *td)
978 td->verify_thread_exit = 1;
980 pthread_cond_broadcast(&td->verify_cond);
982 pthread_mutex_lock(&td->io_u_lock);
984 while (td->nr_verify_threads)
985 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
987 pthread_mutex_unlock(&td->io_u_lock);
988 free(td->verify_threads);
989 td->verify_threads = NULL;