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->buf_filled && io_u->buf_filled_len >= len) {
34 dprint(FD_VERIFY, "using already filled verify pattern b=0 len=%u\n", len);
37 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
38 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 && 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);
62 io_u->buf_filled_len = len;
68 static void memswp(void *buf1, void *buf2, unsigned int len)
72 assert(len <= sizeof(swap));
74 memcpy(&swap, buf1, len);
75 memcpy(buf1, buf2, len);
76 memcpy(buf2, &swap, len);
79 static void hexdump(void *buffer, int len)
81 unsigned char *p = buffer;
84 for (i = 0; i < len; i++)
85 log_err("%02x", p[i]);
90 * Prepare for seperation of verify_header and checksum header
92 static inline unsigned int __hdr_size(int verify_type)
94 unsigned int len = len;
96 switch (verify_type) {
102 len = sizeof(struct vhdr_md5);
105 len = sizeof(struct vhdr_crc64);
109 case VERIFY_CRC32C_INTEL:
110 len = sizeof(struct vhdr_crc32);
113 len = sizeof(struct vhdr_crc16);
116 len = sizeof(struct vhdr_crc7);
119 len = sizeof(struct vhdr_sha256);
122 len = sizeof(struct vhdr_sha512);
125 len = sizeof(struct vhdr_meta);
128 len = sizeof(struct vhdr_sha1);
131 log_err("fio: unknown verify header!\n");
135 return len + sizeof(struct verify_header);
138 static inline unsigned int hdr_size(struct verify_header *hdr)
140 return __hdr_size(hdr->verify_type);
143 static void *hdr_priv(struct verify_header *hdr)
147 return priv + sizeof(struct verify_header);
151 * Verify container, pass info to verify handlers and allow them to
152 * pass info back in case of error
159 unsigned int hdr_num;
162 * Output, only valid in case of error
167 unsigned int crc_len;
170 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
172 unsigned long long offset;
174 offset = vc->io_u->offset;
175 offset += vc->hdr_num * hdr->len;
176 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
177 vc->name, vc->io_u->file->file_name, offset, hdr->len);
179 if (vc->good_crc && vc->bad_crc) {
180 log_err(" Expected CRC: ");
181 hexdump(vc->good_crc, vc->crc_len);
182 log_err(" Received CRC: ");
183 hexdump(vc->bad_crc, vc->crc_len);
188 * Return data area 'header_num'
190 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
192 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
195 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
198 struct vhdr_meta *vh = hdr_priv(hdr);
199 struct io_u *io_u = vc->io_u;
201 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
203 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
207 log_verify_failure(hdr, vc);
211 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
213 void *p = io_u_verify_off(hdr, vc);
214 struct vhdr_sha512 *vh = hdr_priv(hdr);
216 struct sha512_ctx sha512_ctx = {
220 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
222 sha512_init(&sha512_ctx);
223 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
225 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
229 vc->good_crc = vh->sha512;
230 vc->bad_crc = sha512_ctx.buf;
231 vc->crc_len = sizeof(vh->sha512);
232 log_verify_failure(hdr, vc);
236 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
238 void *p = io_u_verify_off(hdr, vc);
239 struct vhdr_sha256 *vh = hdr_priv(hdr);
241 struct sha256_ctx sha256_ctx = {
245 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
247 sha256_init(&sha256_ctx);
248 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
250 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
254 vc->good_crc = vh->sha256;
255 vc->bad_crc = sha256_ctx.buf;
256 vc->crc_len = sizeof(vh->sha256);
257 log_verify_failure(hdr, vc);
261 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
263 void *p = io_u_verify_off(hdr, vc);
264 struct vhdr_sha1 *vh = hdr_priv(hdr);
266 struct sha1_ctx sha1_ctx = {
270 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
272 sha1_init(&sha1_ctx);
273 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
275 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
279 vc->good_crc = vh->sha1;
280 vc->bad_crc = sha1_ctx.H;
281 vc->crc_len = sizeof(vh->sha1);
282 log_verify_failure(hdr, vc);
286 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
288 void *p = io_u_verify_off(hdr, vc);
289 struct vhdr_crc7 *vh = hdr_priv(hdr);
292 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
294 c = crc7(p, hdr->len - hdr_size(hdr));
300 vc->good_crc = &vh->crc7;
303 log_verify_failure(hdr, vc);
307 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
309 void *p = io_u_verify_off(hdr, vc);
310 struct vhdr_crc16 *vh = hdr_priv(hdr);
313 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
315 c = crc16(p, hdr->len - hdr_size(hdr));
321 vc->good_crc = &vh->crc16;
324 log_verify_failure(hdr, vc);
328 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
330 void *p = io_u_verify_off(hdr, vc);
331 struct vhdr_crc64 *vh = hdr_priv(hdr);
332 unsigned long long c;
334 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
336 c = crc64(p, hdr->len - hdr_size(hdr));
342 vc->good_crc = &vh->crc64;
345 log_verify_failure(hdr, vc);
349 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
351 void *p = io_u_verify_off(hdr, vc);
352 struct vhdr_crc32 *vh = hdr_priv(hdr);
355 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
357 c = crc32(p, hdr->len - hdr_size(hdr));
363 vc->good_crc = &vh->crc32;
366 log_verify_failure(hdr, vc);
370 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
372 void *p = io_u_verify_off(hdr, vc);
373 struct vhdr_crc32 *vh = hdr_priv(hdr);
376 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
378 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
379 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
381 c = crc32c(p, hdr->len - hdr_size(hdr));
387 vc->good_crc = &vh->crc32;
390 log_verify_failure(hdr, vc);
394 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
396 void *p = io_u_verify_off(hdr, vc);
397 struct vhdr_md5 *vh = hdr_priv(hdr);
398 uint32_t hash[MD5_HASH_WORDS];
399 struct md5_ctx md5_ctx = {
403 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
406 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
408 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
412 vc->good_crc = vh->md5_digest;
413 vc->bad_crc = md5_ctx.hash;
414 vc->crc_len = sizeof(hash);
415 log_verify_failure(hdr, vc);
419 static unsigned int hweight8(unsigned int w)
421 unsigned int res = w - ((w >> 1) & 0x55);
423 res = (res & 0x33) + ((res >> 2) & 0x33);
424 return (res + (res >> 4)) & 0x0F;
427 int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
428 char *buf, unsigned int len, unsigned int mod)
432 for (i = 0; i < len; i++) {
433 if (buf[i] != pattern[mod]) {
436 bits = hweight8(buf[i] ^ pattern[mod]);
437 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
438 buf[i], pattern[mod], bits);
439 log_err("fio: bad pattern block offset %u\n", i);
443 if (mod == pattern_size)
451 * Push IO verification to a separate thread
453 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
456 put_file_log(td, io_u->file);
460 pthread_mutex_lock(&td->io_u_lock);
462 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
464 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
466 flist_del(&io_u->list);
467 flist_add_tail(&io_u->list, &td->verify_list);
468 io_u->flags |= IO_U_F_FREE_DEF;
469 pthread_mutex_unlock(&td->io_u_lock);
471 pthread_cond_signal(&td->verify_cond);
475 int verify_io_u(struct thread_data *td, struct io_u *io_u)
477 struct verify_header *hdr;
478 unsigned int hdr_size, hdr_inc, hdr_num = 0;
482 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
485 hdr_inc = io_u->buflen;
486 if (td->o.verify_interval)
487 hdr_inc = td->o.verify_interval;
490 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
491 p += hdr_inc, hdr_num++) {
497 if (ret && td->o.verify_fatal)
500 hdr_size = __hdr_size(td->o.verify);
501 if (td->o.verify_offset)
502 memswp(p, p + td->o.verify_offset, hdr_size);
505 if (hdr->fio_magic != FIO_HDR_MAGIC) {
506 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
507 hdr->fio_magic, FIO_HDR_MAGIC,
508 io_u->file->file_name,
509 io_u->offset + hdr_num * hdr->len, hdr->len);
513 if (td->o.verify_pattern_bytes) {
514 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
516 ret = verify_io_u_pattern(td->o.verify_pattern,
517 td->o.verify_pattern_bytes,
520 hdr_size % td->o.verify_pattern_bytes);
523 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
524 io_u->file->file_name,
525 io_u->offset + hdr_num * hdr->len,
530 * Also verify the meta data, if applicable
532 if (hdr->verify_type == VERIFY_META)
533 ret |= verify_io_u_meta(hdr, td, &vc);
537 switch (hdr->verify_type) {
539 ret = verify_io_u_md5(hdr, &vc);
542 ret = verify_io_u_crc64(hdr, &vc);
545 case VERIFY_CRC32C_INTEL:
546 ret = verify_io_u_crc32c(hdr, &vc);
549 ret = verify_io_u_crc32(hdr, &vc);
552 ret = verify_io_u_crc16(hdr, &vc);
555 ret = verify_io_u_crc7(hdr, &vc);
558 ret = verify_io_u_sha256(hdr, &vc);
561 ret = verify_io_u_sha512(hdr, &vc);
564 ret = verify_io_u_meta(hdr, td, &vc);
567 ret = verify_io_u_sha1(hdr, &vc);
570 log_err("Bad verify type %u\n", hdr->verify_type);
575 if (ret && td->o.verify_fatal)
581 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
582 struct io_u *io_u, unsigned int header_num)
584 struct vhdr_meta *vh = hdr_priv(hdr);
586 vh->thread = td->thread_number;
588 vh->time_sec = io_u->start_time.tv_sec;
589 vh->time_usec = io_u->start_time.tv_usec;
591 vh->numberio = td->io_issues[DDIR_WRITE];
593 vh->offset = io_u->offset + header_num * td->o.verify_interval;
596 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
598 struct vhdr_sha512 *vh = hdr_priv(hdr);
599 struct sha512_ctx sha512_ctx = {
603 sha512_init(&sha512_ctx);
604 sha512_update(&sha512_ctx, p, len);
607 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
609 struct vhdr_sha256 *vh = hdr_priv(hdr);
610 struct sha256_ctx sha256_ctx = {
614 sha256_init(&sha256_ctx);
615 sha256_update(&sha256_ctx, p, len);
618 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
620 struct vhdr_sha1 *vh = hdr_priv(hdr);
621 struct sha1_ctx sha1_ctx = {
625 sha1_init(&sha1_ctx);
626 sha1_update(&sha1_ctx, p, len);
629 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
631 struct vhdr_crc7 *vh = hdr_priv(hdr);
633 vh->crc7 = crc7(p, len);
636 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
638 struct vhdr_crc16 *vh = hdr_priv(hdr);
640 vh->crc16 = crc16(p, len);
643 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
645 struct vhdr_crc32 *vh = hdr_priv(hdr);
647 vh->crc32 = crc32(p, len);
650 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
652 struct vhdr_crc32 *vh = hdr_priv(hdr);
654 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
655 vh->crc32 = crc32c_intel(p, len);
657 vh->crc32 = crc32c(p, len);
660 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
662 struct vhdr_crc64 *vh = hdr_priv(hdr);
664 vh->crc64 = crc64(p, len);
667 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
669 struct vhdr_md5 *vh = hdr_priv(hdr);
670 struct md5_ctx md5_ctx = {
671 .hash = (uint32_t *) vh->md5_digest,
675 md5_update(&md5_ctx, p, len);
679 * fill body of io_u->buf with random data and add a header with the
680 * crc32 or md5 sum of that data.
682 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
684 struct verify_header *hdr;
685 void *p = io_u->buf, *data;
686 unsigned int hdr_inc, data_len, header_num = 0;
688 if (td->o.verify == VERIFY_NULL)
691 fill_pattern(td, p, io_u->buflen, io_u);
693 hdr_inc = io_u->buflen;
694 if (td->o.verify_interval)
695 hdr_inc = td->o.verify_interval;
697 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
700 hdr->fio_magic = FIO_HDR_MAGIC;
701 hdr->verify_type = td->o.verify;
703 data_len = hdr_inc - hdr_size(hdr);
705 data = p + hdr_size(hdr);
706 switch (td->o.verify) {
708 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
710 fill_md5(hdr, data, data_len);
713 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
715 fill_crc64(hdr, data, data_len);
718 case VERIFY_CRC32C_INTEL:
719 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
721 fill_crc32c(hdr, data, data_len);
724 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
726 fill_crc32(hdr, data, data_len);
729 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
731 fill_crc16(hdr, data, data_len);
734 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
736 fill_crc7(hdr, data, data_len);
739 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
741 fill_sha256(hdr, data, data_len);
744 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
746 fill_sha512(hdr, data, data_len);
749 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
751 fill_meta(hdr, td, io_u, header_num);
754 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
756 fill_sha1(hdr, data, data_len);
759 log_err("fio: bad verify type: %d\n", td->o.verify);
762 if (td->o.verify_offset)
763 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
768 int get_next_verify(struct thread_data *td, struct io_u *io_u)
770 struct io_piece *ipo = NULL;
773 * this io_u is from a requeue, we already filled the offsets
778 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
779 struct rb_node *n = rb_first(&td->io_hist_tree);
781 ipo = rb_entry(n, struct io_piece, rb_node);
782 rb_erase(n, &td->io_hist_tree);
784 } else if (!flist_empty(&td->io_hist_list)) {
785 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
787 flist_del(&ipo->list);
791 io_u->offset = ipo->offset;
792 io_u->buflen = ipo->len;
793 io_u->file = ipo->file;
795 if (!fio_file_open(io_u->file)) {
796 int r = td_io_open_file(td, io_u->file);
799 dprint(FD_VERIFY, "failed file %s open\n",
800 io_u->file->file_name);
806 assert(fio_file_open(io_u->file));
807 io_u->ddir = DDIR_READ;
808 io_u->xfer_buf = io_u->buf;
809 io_u->xfer_buflen = io_u->buflen;
811 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
815 dprint(FD_VERIFY, "get_next_verify: empty\n");
819 static void *verify_async_thread(void *data)
821 struct thread_data *td = data;
825 if (td->o.verify_cpumask_set &&
826 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
827 log_err("fio: failed setting verify thread affinity\n");
835 if (td->verify_thread_exit)
838 pthread_mutex_lock(&td->io_u_lock);
840 while (flist_empty(&td->verify_list) &&
841 !td->verify_thread_exit) {
842 ret = pthread_cond_wait(&td->verify_cond,
845 pthread_mutex_unlock(&td->io_u_lock);
850 flist_splice_init(&td->verify_list, &list);
851 pthread_mutex_unlock(&td->io_u_lock);
853 if (flist_empty(&list))
856 while (!flist_empty(&list)) {
857 io_u = flist_entry(list.next, struct io_u, list);
858 flist_del_init(&io_u->list);
860 ret = verify_io_u(td, io_u);
864 if (td->o.continue_on_error &&
865 td_non_fatal_error(ret)) {
866 update_error_count(td, ret);
874 td_verror(td, ret, "async_verify");
875 if (td->o.verify_fatal)
880 pthread_mutex_lock(&td->io_u_lock);
881 td->nr_verify_threads--;
882 pthread_mutex_unlock(&td->io_u_lock);
884 pthread_cond_signal(&td->free_cond);
888 int verify_async_init(struct thread_data *td)
892 td->verify_thread_exit = 0;
894 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
895 for (i = 0; i < td->o.verify_async; i++) {
896 ret = pthread_create(&td->verify_threads[i], NULL,
897 verify_async_thread, td);
899 log_err("fio: async verify creation failed: %s\n",
903 ret = pthread_detach(td->verify_threads[i]);
905 log_err("fio: async verify thread detach failed: %s\n",
909 td->nr_verify_threads++;
912 if (i != td->o.verify_async) {
913 log_err("fio: only %d verify threads started, exiting\n", i);
914 td->verify_thread_exit = 1;
916 pthread_cond_broadcast(&td->verify_cond);
923 void verify_async_exit(struct thread_data *td)
925 td->verify_thread_exit = 1;
927 pthread_cond_broadcast(&td->verify_cond);
929 pthread_mutex_lock(&td->io_u_lock);
931 while (td->nr_verify_threads)
932 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
934 pthread_mutex_unlock(&td->io_u_lock);
935 free(td->verify_threads);
936 td->verify_threads = NULL;