2 * IO verification helpers
15 #include "crc/crc64.h"
16 #include "crc/crc32.h"
17 #include "crc/crc32c.h"
18 #include "crc/crc16.h"
20 #include "crc/sha256.h"
21 #include "crc/sha512.h"
24 static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
30 r = os_random_long(&td->verify_state);
33 * lrand48_r seems to be broken and only fill the bottom
34 * 32-bits, even on 64-bit archs with 64-bit longs
47 static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
49 switch (td->o.verify_pattern_bytes) {
51 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
52 fill_random_bytes(td, p, len);
55 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
56 memset(p, td->o.verify_pattern[0], len);
59 unsigned int i = 0, size = 0;
62 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
63 td->o.verify_pattern_bytes, len);
66 size = td->o.verify_pattern_bytes;
69 memcpy(b+i, td->o.verify_pattern, size);
77 static void memswp(void *buf1, void *buf2, unsigned int len)
81 assert(len <= sizeof(swap));
83 memcpy(&swap, buf1, len);
84 memcpy(buf1, buf2, len);
85 memcpy(buf2, &swap, len);
88 static void hexdump(void *buffer, int len)
90 unsigned char *p = buffer;
93 for (i = 0; i < len; i++)
94 log_err("%02x", p[i]);
99 * Prepare for seperation of verify_header and checksum header
101 static inline unsigned int __hdr_size(int verify_type)
103 unsigned int len = len;
105 switch (verify_type) {
111 len = sizeof(struct vhdr_md5);
114 len = sizeof(struct vhdr_crc64);
118 case VERIFY_CRC32C_INTEL:
119 len = sizeof(struct vhdr_crc32);
122 len = sizeof(struct vhdr_crc16);
125 len = sizeof(struct vhdr_crc7);
128 len = sizeof(struct vhdr_sha256);
131 len = sizeof(struct vhdr_sha512);
134 len = sizeof(struct vhdr_meta);
137 len = sizeof(struct vhdr_sha1);
140 log_err("fio: unknown verify header!\n");
144 return len + sizeof(struct verify_header);
147 static inline unsigned int hdr_size(struct verify_header *hdr)
149 return __hdr_size(hdr->verify_type);
152 static void *hdr_priv(struct verify_header *hdr)
156 return priv + sizeof(struct verify_header);
160 * Verify container, pass info to verify handlers and allow them to
161 * pass info back in case of error
168 unsigned int hdr_num;
171 * Output, only valid in case of error
176 unsigned int crc_len;
179 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
181 unsigned long long offset;
183 offset = vc->io_u->offset;
184 offset += vc->hdr_num * hdr->len;
185 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
186 vc->name, vc->io_u->file->file_name, offset, hdr->len);
188 if (vc->good_crc && vc->bad_crc) {
189 log_err(" Expected CRC: ");
190 hexdump(vc->good_crc, vc->crc_len);
191 log_err(" Received CRC: ");
192 hexdump(vc->bad_crc, vc->crc_len);
197 * Return data area 'header_num'
199 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
201 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
204 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
207 struct vhdr_meta *vh = hdr_priv(hdr);
208 struct io_u *io_u = vc->io_u;
210 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
212 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
216 log_verify_failure(hdr, vc);
220 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
222 void *p = io_u_verify_off(hdr, vc);
223 struct vhdr_sha512 *vh = hdr_priv(hdr);
225 struct sha512_ctx sha512_ctx = {
229 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
231 sha512_init(&sha512_ctx);
232 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
234 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
238 vc->good_crc = vh->sha512;
239 vc->bad_crc = sha512_ctx.buf;
240 vc->crc_len = sizeof(vh->sha512);
241 log_verify_failure(hdr, vc);
245 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
247 void *p = io_u_verify_off(hdr, vc);
248 struct vhdr_sha256 *vh = hdr_priv(hdr);
250 struct sha256_ctx sha256_ctx = {
254 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
256 sha256_init(&sha256_ctx);
257 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
259 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
263 vc->good_crc = vh->sha256;
264 vc->bad_crc = sha256_ctx.buf;
265 vc->crc_len = sizeof(vh->sha256);
266 log_verify_failure(hdr, vc);
270 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
272 void *p = io_u_verify_off(hdr, vc);
273 struct vhdr_sha1 *vh = hdr_priv(hdr);
275 struct sha1_ctx sha1_ctx = {
279 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
281 sha1_init(&sha1_ctx);
282 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
284 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
288 vc->good_crc = vh->sha1;
289 vc->bad_crc = sha1_ctx.H;
290 vc->crc_len = sizeof(vh->sha1);
291 log_verify_failure(hdr, vc);
295 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
297 void *p = io_u_verify_off(hdr, vc);
298 struct vhdr_crc7 *vh = hdr_priv(hdr);
301 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
303 c = crc7(p, hdr->len - hdr_size(hdr));
309 vc->good_crc = &vh->crc7;
312 log_verify_failure(hdr, vc);
316 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
318 void *p = io_u_verify_off(hdr, vc);
319 struct vhdr_crc16 *vh = hdr_priv(hdr);
322 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
324 c = crc16(p, hdr->len - hdr_size(hdr));
330 vc->good_crc = &vh->crc16;
333 log_verify_failure(hdr, vc);
337 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
339 void *p = io_u_verify_off(hdr, vc);
340 struct vhdr_crc64 *vh = hdr_priv(hdr);
341 unsigned long long c;
343 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
345 c = crc64(p, hdr->len - hdr_size(hdr));
351 vc->good_crc = &vh->crc64;
354 log_verify_failure(hdr, vc);
358 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
360 void *p = io_u_verify_off(hdr, vc);
361 struct vhdr_crc32 *vh = hdr_priv(hdr);
364 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
366 c = crc32(p, hdr->len - hdr_size(hdr));
372 vc->good_crc = &vh->crc32;
375 log_verify_failure(hdr, vc);
379 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
381 void *p = io_u_verify_off(hdr, vc);
382 struct vhdr_crc32 *vh = hdr_priv(hdr);
385 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
387 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
388 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
390 c = crc32c(p, hdr->len - hdr_size(hdr));
396 vc->good_crc = &vh->crc32;
399 log_verify_failure(hdr, vc);
403 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
405 void *p = io_u_verify_off(hdr, vc);
406 struct vhdr_md5 *vh = hdr_priv(hdr);
407 uint32_t hash[MD5_HASH_WORDS];
408 struct md5_ctx md5_ctx = {
412 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
415 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
417 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
421 vc->good_crc = vh->md5_digest;
422 vc->bad_crc = md5_ctx.hash;
423 vc->crc_len = sizeof(hash);
424 log_verify_failure(hdr, vc);
428 static unsigned int hweight8(unsigned int w)
430 unsigned int res = w - ((w >> 1) & 0x55);
432 res = (res & 0x33) + ((res >> 2) & 0x33);
433 return (res + (res >> 4)) & 0x0F;
436 int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
437 char *buf, unsigned int len, unsigned int mod)
441 for (i = 0; i < len; i++) {
442 if (buf[i] != pattern[mod]) {
445 bits = hweight8(buf[i] ^ pattern[mod]);
446 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
447 buf[i], pattern[mod], bits);
448 log_err("fio: bad pattern block offset %u\n", i);
452 if (mod == pattern_size)
460 * Push IO verification to a separate thread
462 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
465 put_file_log(td, io_u->file);
469 pthread_mutex_lock(&td->io_u_lock);
471 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
473 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
475 flist_del(&io_u->list);
476 flist_add_tail(&io_u->list, &td->verify_list);
477 io_u->flags |= IO_U_F_FREE_DEF;
478 pthread_mutex_unlock(&td->io_u_lock);
480 pthread_cond_signal(&td->verify_cond);
484 int verify_io_u(struct thread_data *td, struct io_u *io_u)
486 struct verify_header *hdr;
487 unsigned int hdr_size, hdr_inc, hdr_num = 0;
491 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
494 hdr_inc = io_u->buflen;
495 if (td->o.verify_interval)
496 hdr_inc = td->o.verify_interval;
499 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
500 p += hdr_inc, hdr_num++) {
506 if (ret && td->o.verify_fatal) {
510 hdr_size = __hdr_size(td->o.verify);
511 if (td->o.verify_offset)
512 memswp(p, p + td->o.verify_offset, hdr_size);
515 if (hdr->fio_magic != FIO_HDR_MAGIC) {
516 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
517 hdr->fio_magic, FIO_HDR_MAGIC,
518 io_u->file->file_name,
519 io_u->offset + hdr_num * hdr->len, hdr->len);
523 if (td->o.verify_pattern_bytes) {
524 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
526 ret = verify_io_u_pattern(td->o.verify_pattern,
527 td->o.verify_pattern_bytes,
530 hdr_size % td->o.verify_pattern_bytes);
533 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
534 io_u->file->file_name,
535 io_u->offset + hdr_num * hdr->len,
540 * Also verify the meta data, if applicable
542 if (hdr->verify_type == VERIFY_META)
543 ret |= verify_io_u_meta(hdr, td, &vc);
547 switch (hdr->verify_type) {
549 ret = verify_io_u_md5(hdr, &vc);
552 ret = verify_io_u_crc64(hdr, &vc);
555 case VERIFY_CRC32C_INTEL:
556 ret = verify_io_u_crc32c(hdr, &vc);
559 ret = verify_io_u_crc32(hdr, &vc);
562 ret = verify_io_u_crc16(hdr, &vc);
565 ret = verify_io_u_crc7(hdr, &vc);
568 ret = verify_io_u_sha256(hdr, &vc);
571 ret = verify_io_u_sha512(hdr, &vc);
574 ret = verify_io_u_meta(hdr, td, &vc);
577 ret = verify_io_u_sha1(hdr, &vc);
580 log_err("Bad verify type %u\n", hdr->verify_type);
588 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
589 struct io_u *io_u, unsigned int header_num)
591 struct vhdr_meta *vh = hdr_priv(hdr);
593 vh->thread = td->thread_number;
595 vh->time_sec = io_u->start_time.tv_sec;
596 vh->time_usec = io_u->start_time.tv_usec;
598 vh->numberio = td->io_issues[DDIR_WRITE];
600 vh->offset = io_u->offset + header_num * td->o.verify_interval;
603 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
605 struct vhdr_sha512 *vh = hdr_priv(hdr);
606 struct sha512_ctx sha512_ctx = {
610 sha512_init(&sha512_ctx);
611 sha512_update(&sha512_ctx, p, len);
614 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
616 struct vhdr_sha256 *vh = hdr_priv(hdr);
617 struct sha256_ctx sha256_ctx = {
621 sha256_init(&sha256_ctx);
622 sha256_update(&sha256_ctx, p, len);
625 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
627 struct vhdr_sha1 *vh = hdr_priv(hdr);
628 struct sha1_ctx sha1_ctx = {
632 sha1_init(&sha1_ctx);
633 sha1_update(&sha1_ctx, p, len);
636 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
638 struct vhdr_crc7 *vh = hdr_priv(hdr);
640 vh->crc7 = crc7(p, len);
643 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
645 struct vhdr_crc16 *vh = hdr_priv(hdr);
647 vh->crc16 = crc16(p, len);
650 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
652 struct vhdr_crc32 *vh = hdr_priv(hdr);
654 vh->crc32 = crc32(p, len);
657 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
659 struct vhdr_crc32 *vh = hdr_priv(hdr);
661 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
662 vh->crc32 = crc32c_intel(p, len);
664 vh->crc32 = crc32c(p, len);
667 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
669 struct vhdr_crc64 *vh = hdr_priv(hdr);
671 vh->crc64 = crc64(p, len);
674 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
676 struct vhdr_md5 *vh = hdr_priv(hdr);
677 struct md5_ctx md5_ctx = {
678 .hash = (uint32_t *) vh->md5_digest,
682 md5_update(&md5_ctx, p, len);
686 * fill body of io_u->buf with random data and add a header with the
687 * crc32 or md5 sum of that data.
689 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
691 struct verify_header *hdr;
692 void *p = io_u->buf, *data;
693 unsigned int hdr_inc, data_len, header_num = 0;
695 if (td->o.verify == VERIFY_NULL)
698 fill_pattern(td, p, io_u->buflen);
700 hdr_inc = io_u->buflen;
701 if (td->o.verify_interval)
702 hdr_inc = td->o.verify_interval;
704 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
707 hdr->fio_magic = FIO_HDR_MAGIC;
708 hdr->verify_type = td->o.verify;
710 data_len = hdr_inc - hdr_size(hdr);
712 data = p + hdr_size(hdr);
713 switch (td->o.verify) {
715 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
717 fill_md5(hdr, data, data_len);
720 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
722 fill_crc64(hdr, data, data_len);
725 case VERIFY_CRC32C_INTEL:
726 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
728 fill_crc32c(hdr, data, data_len);
731 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
733 fill_crc32(hdr, data, data_len);
736 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
738 fill_crc16(hdr, data, data_len);
741 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
743 fill_crc7(hdr, data, data_len);
746 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
748 fill_sha256(hdr, data, data_len);
751 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
753 fill_sha512(hdr, data, data_len);
756 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
758 fill_meta(hdr, td, io_u, header_num);
761 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
763 fill_sha1(hdr, data, data_len);
766 log_err("fio: bad verify type: %d\n", td->o.verify);
769 if (td->o.verify_offset)
770 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
775 int get_next_verify(struct thread_data *td, struct io_u *io_u)
777 struct io_piece *ipo = NULL;
780 * this io_u is from a requeue, we already filled the offsets
785 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
786 struct rb_node *n = rb_first(&td->io_hist_tree);
788 ipo = rb_entry(n, struct io_piece, rb_node);
789 rb_erase(n, &td->io_hist_tree);
791 } else if (!flist_empty(&td->io_hist_list)) {
792 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
794 flist_del(&ipo->list);
798 io_u->offset = ipo->offset;
799 io_u->buflen = ipo->len;
800 io_u->file = ipo->file;
802 if (!fio_file_open(io_u->file)) {
803 int r = td_io_open_file(td, io_u->file);
806 dprint(FD_VERIFY, "failed file %s open\n",
807 io_u->file->file_name);
813 assert(fio_file_open(io_u->file));
814 io_u->ddir = DDIR_READ;
815 io_u->xfer_buf = io_u->buf;
816 io_u->xfer_buflen = io_u->buflen;
818 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
822 dprint(FD_VERIFY, "get_next_verify: empty\n");
826 static void *verify_async_thread(void *data)
828 struct thread_data *td = data;
832 if (td->o.verify_cpumask_set &&
833 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
834 log_err("fio: failed setting verify thread affinity\n");
842 if (td->verify_thread_exit)
845 pthread_mutex_lock(&td->io_u_lock);
847 while (flist_empty(&td->verify_list) &&
848 !td->verify_thread_exit) {
849 ret = pthread_cond_wait(&td->verify_cond,
852 pthread_mutex_unlock(&td->io_u_lock);
857 flist_splice_init(&td->verify_list, &list);
858 pthread_mutex_unlock(&td->io_u_lock);
860 if (flist_empty(&list))
863 while (!flist_empty(&list)) {
864 io_u = flist_entry(list.next, struct io_u, list);
865 flist_del_init(&io_u->list);
867 ret = verify_io_u(td, io_u);
871 if (td->o.continue_on_error &&
872 td_non_fatal_error(ret)) {
873 update_error_count(td, ret);
881 td_verror(td, ret, "async_verify");
886 pthread_mutex_lock(&td->io_u_lock);
887 td->nr_verify_threads--;
888 pthread_mutex_unlock(&td->io_u_lock);
890 pthread_cond_signal(&td->free_cond);
894 int verify_async_init(struct thread_data *td)
898 td->verify_thread_exit = 0;
900 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
901 for (i = 0; i < td->o.verify_async; i++) {
902 ret = pthread_create(&td->verify_threads[i], NULL,
903 verify_async_thread, td);
905 log_err("fio: async verify creation failed: %s\n",
909 ret = pthread_detach(td->verify_threads[i]);
911 log_err("fio: async verify thread detach failed: %s\n",
915 td->nr_verify_threads++;
918 if (i != td->o.verify_async) {
919 log_err("fio: only %d verify threads started, exiting\n", i);
920 td->verify_thread_exit = 1;
922 pthread_cond_broadcast(&td->verify_cond);
929 void verify_async_exit(struct thread_data *td)
931 td->verify_thread_exit = 1;
933 pthread_cond_broadcast(&td->verify_cond);
935 pthread_mutex_lock(&td->io_u_lock);
937 while (td->nr_verify_threads)
938 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
940 pthread_mutex_unlock(&td->io_u_lock);
941 free(td->verify_threads);
942 td->verify_threads = NULL;