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)
509 hdr_size = __hdr_size(td->o.verify);
510 if (td->o.verify_offset)
511 memswp(p, p + td->o.verify_offset, hdr_size);
514 if (hdr->fio_magic != FIO_HDR_MAGIC) {
515 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
516 hdr->fio_magic, FIO_HDR_MAGIC,
517 io_u->file->file_name,
518 io_u->offset + hdr_num * hdr->len, hdr->len);
522 if (td->o.verify_pattern_bytes) {
523 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
525 ret = verify_io_u_pattern(td->o.verify_pattern,
526 td->o.verify_pattern_bytes,
529 hdr_size % td->o.verify_pattern_bytes);
532 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
533 io_u->file->file_name,
534 io_u->offset + hdr_num * hdr->len,
539 * Also verify the meta data, if applicable
541 if (hdr->verify_type == VERIFY_META)
542 ret |= verify_io_u_meta(hdr, td, &vc);
546 switch (hdr->verify_type) {
548 ret = verify_io_u_md5(hdr, &vc);
551 ret = verify_io_u_crc64(hdr, &vc);
554 case VERIFY_CRC32C_INTEL:
555 ret = verify_io_u_crc32c(hdr, &vc);
558 ret = verify_io_u_crc32(hdr, &vc);
561 ret = verify_io_u_crc16(hdr, &vc);
564 ret = verify_io_u_crc7(hdr, &vc);
567 ret = verify_io_u_sha256(hdr, &vc);
570 ret = verify_io_u_sha512(hdr, &vc);
573 ret = verify_io_u_meta(hdr, td, &vc);
576 ret = verify_io_u_sha1(hdr, &vc);
579 log_err("Bad verify type %u\n", hdr->verify_type);
584 if (ret && td->o.verify_fatal)
590 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
591 struct io_u *io_u, unsigned int header_num)
593 struct vhdr_meta *vh = hdr_priv(hdr);
595 vh->thread = td->thread_number;
597 vh->time_sec = io_u->start_time.tv_sec;
598 vh->time_usec = io_u->start_time.tv_usec;
600 vh->numberio = td->io_issues[DDIR_WRITE];
602 vh->offset = io_u->offset + header_num * td->o.verify_interval;
605 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
607 struct vhdr_sha512 *vh = hdr_priv(hdr);
608 struct sha512_ctx sha512_ctx = {
612 sha512_init(&sha512_ctx);
613 sha512_update(&sha512_ctx, p, len);
616 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
618 struct vhdr_sha256 *vh = hdr_priv(hdr);
619 struct sha256_ctx sha256_ctx = {
623 sha256_init(&sha256_ctx);
624 sha256_update(&sha256_ctx, p, len);
627 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
629 struct vhdr_sha1 *vh = hdr_priv(hdr);
630 struct sha1_ctx sha1_ctx = {
634 sha1_init(&sha1_ctx);
635 sha1_update(&sha1_ctx, p, len);
638 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
640 struct vhdr_crc7 *vh = hdr_priv(hdr);
642 vh->crc7 = crc7(p, len);
645 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
647 struct vhdr_crc16 *vh = hdr_priv(hdr);
649 vh->crc16 = crc16(p, len);
652 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
654 struct vhdr_crc32 *vh = hdr_priv(hdr);
656 vh->crc32 = crc32(p, len);
659 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
661 struct vhdr_crc32 *vh = hdr_priv(hdr);
663 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
664 vh->crc32 = crc32c_intel(p, len);
666 vh->crc32 = crc32c(p, len);
669 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
671 struct vhdr_crc64 *vh = hdr_priv(hdr);
673 vh->crc64 = crc64(p, len);
676 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
678 struct vhdr_md5 *vh = hdr_priv(hdr);
679 struct md5_ctx md5_ctx = {
680 .hash = (uint32_t *) vh->md5_digest,
684 md5_update(&md5_ctx, p, len);
688 * fill body of io_u->buf with random data and add a header with the
689 * crc32 or md5 sum of that data.
691 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
693 struct verify_header *hdr;
694 void *p = io_u->buf, *data;
695 unsigned int hdr_inc, data_len, header_num = 0;
697 if (td->o.verify == VERIFY_NULL)
700 fill_pattern(td, p, io_u->buflen);
702 hdr_inc = io_u->buflen;
703 if (td->o.verify_interval)
704 hdr_inc = td->o.verify_interval;
706 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
709 hdr->fio_magic = FIO_HDR_MAGIC;
710 hdr->verify_type = td->o.verify;
712 data_len = hdr_inc - hdr_size(hdr);
714 data = p + hdr_size(hdr);
715 switch (td->o.verify) {
717 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
719 fill_md5(hdr, data, data_len);
722 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
724 fill_crc64(hdr, data, data_len);
727 case VERIFY_CRC32C_INTEL:
728 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
730 fill_crc32c(hdr, data, data_len);
733 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
735 fill_crc32(hdr, data, data_len);
738 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
740 fill_crc16(hdr, data, data_len);
743 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
745 fill_crc7(hdr, data, data_len);
748 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
750 fill_sha256(hdr, data, data_len);
753 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
755 fill_sha512(hdr, data, data_len);
758 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
760 fill_meta(hdr, td, io_u, header_num);
763 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
765 fill_sha1(hdr, data, data_len);
768 log_err("fio: bad verify type: %d\n", td->o.verify);
771 if (td->o.verify_offset)
772 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
777 int get_next_verify(struct thread_data *td, struct io_u *io_u)
779 struct io_piece *ipo = NULL;
782 * this io_u is from a requeue, we already filled the offsets
787 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
788 struct rb_node *n = rb_first(&td->io_hist_tree);
790 ipo = rb_entry(n, struct io_piece, rb_node);
791 rb_erase(n, &td->io_hist_tree);
793 } else if (!flist_empty(&td->io_hist_list)) {
794 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
796 flist_del(&ipo->list);
800 io_u->offset = ipo->offset;
801 io_u->buflen = ipo->len;
802 io_u->file = ipo->file;
804 if (!fio_file_open(io_u->file)) {
805 int r = td_io_open_file(td, io_u->file);
808 dprint(FD_VERIFY, "failed file %s open\n",
809 io_u->file->file_name);
815 assert(fio_file_open(io_u->file));
816 io_u->ddir = DDIR_READ;
817 io_u->xfer_buf = io_u->buf;
818 io_u->xfer_buflen = io_u->buflen;
820 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
824 dprint(FD_VERIFY, "get_next_verify: empty\n");
828 static void *verify_async_thread(void *data)
830 struct thread_data *td = data;
834 if (td->o.verify_cpumask_set &&
835 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
836 log_err("fio: failed setting verify thread affinity\n");
844 if (td->verify_thread_exit)
847 pthread_mutex_lock(&td->io_u_lock);
849 while (flist_empty(&td->verify_list) &&
850 !td->verify_thread_exit) {
851 ret = pthread_cond_wait(&td->verify_cond,
854 pthread_mutex_unlock(&td->io_u_lock);
859 flist_splice_init(&td->verify_list, &list);
860 pthread_mutex_unlock(&td->io_u_lock);
862 if (flist_empty(&list))
865 while (!flist_empty(&list)) {
866 io_u = flist_entry(list.next, struct io_u, list);
867 flist_del_init(&io_u->list);
869 ret = verify_io_u(td, io_u);
873 if (td->o.continue_on_error &&
874 td_non_fatal_error(ret)) {
875 update_error_count(td, ret);
883 td_verror(td, ret, "async_verify");
884 if (td->o.verify_fatal)
889 pthread_mutex_lock(&td->io_u_lock);
890 td->nr_verify_threads--;
891 pthread_mutex_unlock(&td->io_u_lock);
893 pthread_cond_signal(&td->free_cond);
897 int verify_async_init(struct thread_data *td)
901 td->verify_thread_exit = 0;
903 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
904 for (i = 0; i < td->o.verify_async; i++) {
905 ret = pthread_create(&td->verify_threads[i], NULL,
906 verify_async_thread, td);
908 log_err("fio: async verify creation failed: %s\n",
912 ret = pthread_detach(td->verify_threads[i]);
914 log_err("fio: async verify thread detach failed: %s\n",
918 td->nr_verify_threads++;
921 if (i != td->o.verify_async) {
922 log_err("fio: only %d verify threads started, exiting\n", i);
923 td->verify_thread_exit = 1;
925 pthread_cond_broadcast(&td->verify_cond);
932 void verify_async_exit(struct thread_data *td)
934 td->verify_thread_exit = 1;
936 pthread_cond_broadcast(&td->verify_cond);
938 pthread_mutex_lock(&td->io_u_lock);
940 while (td->nr_verify_threads)
941 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
943 pthread_mutex_unlock(&td->io_u_lock);
944 free(td->verify_threads);
945 td->verify_threads = NULL;