2 * IO verification helpers
16 #include "crc/crc64.h"
17 #include "crc/crc32.h"
18 #include "crc/crc32c.h"
19 #include "crc/crc16.h"
21 #include "crc/sha256.h"
22 #include "crc/sha512.h"
25 void fill_pattern(struct thread_data *td, void *p, unsigned int len, struct io_u *io_u)
27 switch (td->o.verify_pattern_bytes) {
29 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
30 fill_random_buf(p, len);
33 if ((io_u->flags & IO_U_F_FILLED) &&
34 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->flags |= IO_U_F_FILLED;
41 io_u->buf_filled_len = len;
44 unsigned int i = 0, size = 0;
47 if ((io_u->flags & IO_U_F_FILLED) &&
48 io_u->buf_filled_len >= len) {
49 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
50 td->o.verify_pattern_bytes, len);
53 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
54 td->o.verify_pattern_bytes, len);
57 size = td->o.verify_pattern_bytes;
60 memcpy(b+i, td->o.verify_pattern, size);
63 io_u->flags |= IO_U_F_FILLED;
64 io_u->buf_filled_len = len;
70 static void memswp(void *buf1, void *buf2, unsigned int len)
74 assert(len <= sizeof(swap));
76 memcpy(&swap, buf1, len);
77 memcpy(buf1, buf2, len);
78 memcpy(buf2, &swap, len);
81 static void hexdump(void *buffer, int len)
83 unsigned char *p = buffer;
86 for (i = 0; i < len; i++)
87 log_err("%02x", p[i]);
92 * Prepare for seperation of verify_header and checksum header
94 static inline unsigned int __hdr_size(int verify_type)
96 unsigned int len = len;
98 switch (verify_type) {
104 len = sizeof(struct vhdr_md5);
107 len = sizeof(struct vhdr_crc64);
111 case VERIFY_CRC32C_INTEL:
112 len = sizeof(struct vhdr_crc32);
115 len = sizeof(struct vhdr_crc16);
118 len = sizeof(struct vhdr_crc7);
121 len = sizeof(struct vhdr_sha256);
124 len = sizeof(struct vhdr_sha512);
127 len = sizeof(struct vhdr_meta);
130 len = sizeof(struct vhdr_sha1);
133 log_err("fio: unknown verify header!\n");
137 return len + sizeof(struct verify_header);
140 static inline unsigned int hdr_size(struct verify_header *hdr)
142 return __hdr_size(hdr->verify_type);
145 static void *hdr_priv(struct verify_header *hdr)
149 return priv + sizeof(struct verify_header);
153 * Verify container, pass info to verify handlers and allow them to
154 * pass info back in case of error
161 unsigned int hdr_num;
164 * Output, only valid in case of error
169 unsigned int crc_len;
172 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
174 unsigned long long offset;
176 offset = vc->io_u->offset;
177 offset += vc->hdr_num * hdr->len;
178 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
179 vc->name, vc->io_u->file->file_name, offset, hdr->len);
181 if (vc->good_crc && vc->bad_crc) {
182 log_err(" Expected CRC: ");
183 hexdump(vc->good_crc, vc->crc_len);
184 log_err(" Received CRC: ");
185 hexdump(vc->bad_crc, vc->crc_len);
190 * Return data area 'header_num'
192 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
194 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
197 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
200 struct vhdr_meta *vh = hdr_priv(hdr);
201 struct io_u *io_u = vc->io_u;
203 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
205 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
209 log_verify_failure(hdr, vc);
213 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
215 void *p = io_u_verify_off(hdr, vc);
216 struct vhdr_sha512 *vh = hdr_priv(hdr);
218 struct sha512_ctx sha512_ctx = {
222 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
224 sha512_init(&sha512_ctx);
225 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
227 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
231 vc->good_crc = vh->sha512;
232 vc->bad_crc = sha512_ctx.buf;
233 vc->crc_len = sizeof(vh->sha512);
234 log_verify_failure(hdr, vc);
238 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
240 void *p = io_u_verify_off(hdr, vc);
241 struct vhdr_sha256 *vh = hdr_priv(hdr);
243 struct sha256_ctx sha256_ctx = {
247 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
249 sha256_init(&sha256_ctx);
250 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
252 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
256 vc->good_crc = vh->sha256;
257 vc->bad_crc = sha256_ctx.buf;
258 vc->crc_len = sizeof(vh->sha256);
259 log_verify_failure(hdr, vc);
263 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
265 void *p = io_u_verify_off(hdr, vc);
266 struct vhdr_sha1 *vh = hdr_priv(hdr);
268 struct sha1_ctx sha1_ctx = {
272 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
274 sha1_init(&sha1_ctx);
275 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
277 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
281 vc->good_crc = vh->sha1;
282 vc->bad_crc = sha1_ctx.H;
283 vc->crc_len = sizeof(vh->sha1);
284 log_verify_failure(hdr, vc);
288 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
290 void *p = io_u_verify_off(hdr, vc);
291 struct vhdr_crc7 *vh = hdr_priv(hdr);
294 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
296 c = crc7(p, hdr->len - hdr_size(hdr));
302 vc->good_crc = &vh->crc7;
305 log_verify_failure(hdr, vc);
309 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
311 void *p = io_u_verify_off(hdr, vc);
312 struct vhdr_crc16 *vh = hdr_priv(hdr);
315 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
317 c = crc16(p, hdr->len - hdr_size(hdr));
323 vc->good_crc = &vh->crc16;
326 log_verify_failure(hdr, vc);
330 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
332 void *p = io_u_verify_off(hdr, vc);
333 struct vhdr_crc64 *vh = hdr_priv(hdr);
334 unsigned long long c;
336 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
338 c = crc64(p, hdr->len - hdr_size(hdr));
344 vc->good_crc = &vh->crc64;
347 log_verify_failure(hdr, vc);
351 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
353 void *p = io_u_verify_off(hdr, vc);
354 struct vhdr_crc32 *vh = hdr_priv(hdr);
357 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
359 c = crc32(p, hdr->len - hdr_size(hdr));
365 vc->good_crc = &vh->crc32;
368 log_verify_failure(hdr, vc);
372 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
374 void *p = io_u_verify_off(hdr, vc);
375 struct vhdr_crc32 *vh = hdr_priv(hdr);
378 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
380 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
381 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
383 c = crc32c(p, hdr->len - hdr_size(hdr));
389 vc->good_crc = &vh->crc32;
392 log_verify_failure(hdr, vc);
396 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
398 void *p = io_u_verify_off(hdr, vc);
399 struct vhdr_md5 *vh = hdr_priv(hdr);
400 uint32_t hash[MD5_HASH_WORDS];
401 struct md5_ctx md5_ctx = {
405 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
408 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
410 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
414 vc->good_crc = vh->md5_digest;
415 vc->bad_crc = md5_ctx.hash;
416 vc->crc_len = sizeof(hash);
417 log_verify_failure(hdr, vc);
421 static unsigned int hweight8(unsigned int w)
423 unsigned int res = w - ((w >> 1) & 0x55);
425 res = (res & 0x33) + ((res >> 2) & 0x33);
426 return (res + (res >> 4)) & 0x0F;
429 int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
430 char *buf, unsigned int len, unsigned int mod)
434 for (i = 0; i < len; i++) {
435 if (buf[i] != pattern[mod]) {
438 bits = hweight8(buf[i] ^ pattern[mod]);
439 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
440 buf[i], pattern[mod], bits);
441 log_err("fio: bad pattern block offset %u\n", i);
445 if (mod == pattern_size)
453 * Push IO verification to a separate thread
455 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
458 put_file_log(td, io_u->file);
462 pthread_mutex_lock(&td->io_u_lock);
464 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
466 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
468 flist_del(&io_u->list);
469 flist_add_tail(&io_u->list, &td->verify_list);
470 io_u->flags |= IO_U_F_FREE_DEF;
471 pthread_mutex_unlock(&td->io_u_lock);
473 pthread_cond_signal(&td->verify_cond);
477 int verify_io_u(struct thread_data *td, struct io_u *io_u)
479 struct verify_header *hdr;
480 unsigned int hdr_size, hdr_inc, hdr_num = 0;
484 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
487 hdr_inc = io_u->buflen;
488 if (td->o.verify_interval)
489 hdr_inc = td->o.verify_interval;
492 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
493 p += hdr_inc, hdr_num++) {
499 if (ret && td->o.verify_fatal)
502 hdr_size = __hdr_size(td->o.verify);
503 if (td->o.verify_offset)
504 memswp(p, p + td->o.verify_offset, hdr_size);
507 if (hdr->fio_magic != FIO_HDR_MAGIC) {
508 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
509 hdr->fio_magic, FIO_HDR_MAGIC,
510 io_u->file->file_name,
511 io_u->offset + hdr_num * hdr->len, hdr->len);
515 if (td->o.verify_pattern_bytes) {
516 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
518 ret = verify_io_u_pattern(td->o.verify_pattern,
519 td->o.verify_pattern_bytes,
522 hdr_size % td->o.verify_pattern_bytes);
525 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
526 io_u->file->file_name,
527 io_u->offset + hdr_num * hdr->len,
532 * Also verify the meta data, if applicable
534 if (hdr->verify_type == VERIFY_META)
535 ret |= verify_io_u_meta(hdr, td, &vc);
539 switch (hdr->verify_type) {
541 ret = verify_io_u_md5(hdr, &vc);
544 ret = verify_io_u_crc64(hdr, &vc);
547 case VERIFY_CRC32C_INTEL:
548 ret = verify_io_u_crc32c(hdr, &vc);
551 ret = verify_io_u_crc32(hdr, &vc);
554 ret = verify_io_u_crc16(hdr, &vc);
557 ret = verify_io_u_crc7(hdr, &vc);
560 ret = verify_io_u_sha256(hdr, &vc);
563 ret = verify_io_u_sha512(hdr, &vc);
566 ret = verify_io_u_meta(hdr, td, &vc);
569 ret = verify_io_u_sha1(hdr, &vc);
572 log_err("Bad verify type %u\n", hdr->verify_type);
577 if (ret && td->o.verify_fatal)
583 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
584 struct io_u *io_u, unsigned int header_num)
586 struct vhdr_meta *vh = hdr_priv(hdr);
588 vh->thread = td->thread_number;
590 vh->time_sec = io_u->start_time.tv_sec;
591 vh->time_usec = io_u->start_time.tv_usec;
593 vh->numberio = td->io_issues[DDIR_WRITE];
595 vh->offset = io_u->offset + header_num * td->o.verify_interval;
598 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
600 struct vhdr_sha512 *vh = hdr_priv(hdr);
601 struct sha512_ctx sha512_ctx = {
605 sha512_init(&sha512_ctx);
606 sha512_update(&sha512_ctx, p, len);
609 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
611 struct vhdr_sha256 *vh = hdr_priv(hdr);
612 struct sha256_ctx sha256_ctx = {
616 sha256_init(&sha256_ctx);
617 sha256_update(&sha256_ctx, p, len);
620 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
622 struct vhdr_sha1 *vh = hdr_priv(hdr);
623 struct sha1_ctx sha1_ctx = {
627 sha1_init(&sha1_ctx);
628 sha1_update(&sha1_ctx, p, len);
631 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
633 struct vhdr_crc7 *vh = hdr_priv(hdr);
635 vh->crc7 = crc7(p, len);
638 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
640 struct vhdr_crc16 *vh = hdr_priv(hdr);
642 vh->crc16 = crc16(p, len);
645 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
647 struct vhdr_crc32 *vh = hdr_priv(hdr);
649 vh->crc32 = crc32(p, len);
652 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
654 struct vhdr_crc32 *vh = hdr_priv(hdr);
656 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
657 vh->crc32 = crc32c_intel(p, len);
659 vh->crc32 = crc32c(p, len);
662 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
664 struct vhdr_crc64 *vh = hdr_priv(hdr);
666 vh->crc64 = crc64(p, len);
669 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
671 struct vhdr_md5 *vh = hdr_priv(hdr);
672 struct md5_ctx md5_ctx = {
673 .hash = (uint32_t *) vh->md5_digest,
677 md5_update(&md5_ctx, p, len);
681 * fill body of io_u->buf with random data and add a header with the
682 * crc32 or md5 sum of that data.
684 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
686 struct verify_header *hdr;
687 void *p = io_u->buf, *data;
688 unsigned int hdr_inc, data_len, header_num = 0;
690 if (td->o.verify == VERIFY_NULL)
693 fill_pattern(td, p, io_u->buflen, io_u);
695 hdr_inc = io_u->buflen;
696 if (td->o.verify_interval)
697 hdr_inc = td->o.verify_interval;
699 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
702 hdr->fio_magic = FIO_HDR_MAGIC;
703 hdr->verify_type = td->o.verify;
705 data_len = hdr_inc - hdr_size(hdr);
707 data = p + hdr_size(hdr);
708 switch (td->o.verify) {
710 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
712 fill_md5(hdr, data, data_len);
715 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
717 fill_crc64(hdr, data, data_len);
720 case VERIFY_CRC32C_INTEL:
721 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
723 fill_crc32c(hdr, data, data_len);
726 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
728 fill_crc32(hdr, data, data_len);
731 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
733 fill_crc16(hdr, data, data_len);
736 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
738 fill_crc7(hdr, data, data_len);
741 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
743 fill_sha256(hdr, data, data_len);
746 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
748 fill_sha512(hdr, data, data_len);
751 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
753 fill_meta(hdr, td, io_u, header_num);
756 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
758 fill_sha1(hdr, data, data_len);
761 log_err("fio: bad verify type: %d\n", td->o.verify);
764 if (td->o.verify_offset)
765 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
770 int get_next_verify(struct thread_data *td, struct io_u *io_u)
772 struct io_piece *ipo = NULL;
775 * this io_u is from a requeue, we already filled the offsets
780 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
781 struct rb_node *n = rb_first(&td->io_hist_tree);
783 ipo = rb_entry(n, struct io_piece, rb_node);
784 rb_erase(n, &td->io_hist_tree);
786 } else if (!flist_empty(&td->io_hist_list)) {
787 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
789 flist_del(&ipo->list);
793 io_u->offset = ipo->offset;
794 io_u->buflen = ipo->len;
795 io_u->file = ipo->file;
797 if (!fio_file_open(io_u->file)) {
798 int r = td_io_open_file(td, io_u->file);
801 dprint(FD_VERIFY, "failed file %s open\n",
802 io_u->file->file_name);
808 assert(fio_file_open(io_u->file));
809 io_u->ddir = DDIR_READ;
810 io_u->xfer_buf = io_u->buf;
811 io_u->xfer_buflen = io_u->buflen;
813 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
817 dprint(FD_VERIFY, "get_next_verify: empty\n");
821 static void *verify_async_thread(void *data)
823 struct thread_data *td = data;
827 if (td->o.verify_cpumask_set &&
828 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
829 log_err("fio: failed setting verify thread affinity\n");
837 if (td->verify_thread_exit)
840 pthread_mutex_lock(&td->io_u_lock);
842 while (flist_empty(&td->verify_list) &&
843 !td->verify_thread_exit) {
844 ret = pthread_cond_wait(&td->verify_cond,
847 pthread_mutex_unlock(&td->io_u_lock);
852 flist_splice_init(&td->verify_list, &list);
853 pthread_mutex_unlock(&td->io_u_lock);
855 if (flist_empty(&list))
858 while (!flist_empty(&list)) {
859 io_u = flist_entry(list.next, struct io_u, list);
860 flist_del_init(&io_u->list);
862 ret = verify_io_u(td, io_u);
866 if (td->o.continue_on_error &&
867 td_non_fatal_error(ret)) {
868 update_error_count(td, ret);
876 td_verror(td, ret, "async_verify");
877 if (td->o.verify_fatal)
882 pthread_mutex_lock(&td->io_u_lock);
883 td->nr_verify_threads--;
884 pthread_mutex_unlock(&td->io_u_lock);
886 pthread_cond_signal(&td->free_cond);
890 int verify_async_init(struct thread_data *td)
894 td->verify_thread_exit = 0;
896 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
897 for (i = 0; i < td->o.verify_async; i++) {
898 ret = pthread_create(&td->verify_threads[i], NULL,
899 verify_async_thread, td);
901 log_err("fio: async verify creation failed: %s\n",
905 ret = pthread_detach(td->verify_threads[i]);
907 log_err("fio: async verify thread detach failed: %s\n",
911 td->nr_verify_threads++;
914 if (i != td->o.verify_async) {
915 log_err("fio: only %d verify threads started, exiting\n", i);
916 td->verify_thread_exit = 1;
918 pthread_cond_broadcast(&td->verify_cond);
925 void verify_async_exit(struct thread_data *td)
927 td->verify_thread_exit = 1;
929 pthread_cond_broadcast(&td->verify_cond);
931 pthread_mutex_lock(&td->io_u_lock);
933 while (td->nr_verify_threads)
934 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
936 pthread_mutex_unlock(&td->io_u_lock);
937 free(td->verify_threads);
938 td->verify_threads = NULL;