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 static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
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 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
34 memset(p, td->o.verify_pattern[0], len);
37 unsigned int i = 0, size = 0;
40 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
41 td->o.verify_pattern_bytes, len);
44 size = td->o.verify_pattern_bytes;
47 memcpy(b+i, td->o.verify_pattern, size);
55 static void memswp(void *buf1, void *buf2, unsigned int len)
59 assert(len <= sizeof(swap));
61 memcpy(&swap, buf1, len);
62 memcpy(buf1, buf2, len);
63 memcpy(buf2, &swap, len);
66 static void hexdump(void *buffer, int len)
68 unsigned char *p = buffer;
71 for (i = 0; i < len; i++)
72 log_err("%02x", p[i]);
77 * Prepare for seperation of verify_header and checksum header
79 static inline unsigned int __hdr_size(int verify_type)
81 unsigned int len = len;
83 switch (verify_type) {
89 len = sizeof(struct vhdr_md5);
92 len = sizeof(struct vhdr_crc64);
96 case VERIFY_CRC32C_INTEL:
97 len = sizeof(struct vhdr_crc32);
100 len = sizeof(struct vhdr_crc16);
103 len = sizeof(struct vhdr_crc7);
106 len = sizeof(struct vhdr_sha256);
109 len = sizeof(struct vhdr_sha512);
112 len = sizeof(struct vhdr_meta);
115 len = sizeof(struct vhdr_sha1);
118 log_err("fio: unknown verify header!\n");
122 return len + sizeof(struct verify_header);
125 static inline unsigned int hdr_size(struct verify_header *hdr)
127 return __hdr_size(hdr->verify_type);
130 static void *hdr_priv(struct verify_header *hdr)
134 return priv + sizeof(struct verify_header);
138 * Verify container, pass info to verify handlers and allow them to
139 * pass info back in case of error
146 unsigned int hdr_num;
149 * Output, only valid in case of error
154 unsigned int crc_len;
157 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
159 unsigned long long offset;
161 offset = vc->io_u->offset;
162 offset += vc->hdr_num * hdr->len;
163 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
164 vc->name, vc->io_u->file->file_name, offset, hdr->len);
166 if (vc->good_crc && vc->bad_crc) {
167 log_err(" Expected CRC: ");
168 hexdump(vc->good_crc, vc->crc_len);
169 log_err(" Received CRC: ");
170 hexdump(vc->bad_crc, vc->crc_len);
175 * Return data area 'header_num'
177 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
179 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
182 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
185 struct vhdr_meta *vh = hdr_priv(hdr);
186 struct io_u *io_u = vc->io_u;
188 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
190 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
194 log_verify_failure(hdr, vc);
198 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
200 void *p = io_u_verify_off(hdr, vc);
201 struct vhdr_sha512 *vh = hdr_priv(hdr);
203 struct sha512_ctx sha512_ctx = {
207 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
209 sha512_init(&sha512_ctx);
210 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
212 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
216 vc->good_crc = vh->sha512;
217 vc->bad_crc = sha512_ctx.buf;
218 vc->crc_len = sizeof(vh->sha512);
219 log_verify_failure(hdr, vc);
223 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
225 void *p = io_u_verify_off(hdr, vc);
226 struct vhdr_sha256 *vh = hdr_priv(hdr);
228 struct sha256_ctx sha256_ctx = {
232 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
234 sha256_init(&sha256_ctx);
235 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
237 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
241 vc->good_crc = vh->sha256;
242 vc->bad_crc = sha256_ctx.buf;
243 vc->crc_len = sizeof(vh->sha256);
244 log_verify_failure(hdr, vc);
248 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
250 void *p = io_u_verify_off(hdr, vc);
251 struct vhdr_sha1 *vh = hdr_priv(hdr);
253 struct sha1_ctx sha1_ctx = {
257 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
259 sha1_init(&sha1_ctx);
260 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
262 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
266 vc->good_crc = vh->sha1;
267 vc->bad_crc = sha1_ctx.H;
268 vc->crc_len = sizeof(vh->sha1);
269 log_verify_failure(hdr, vc);
273 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
275 void *p = io_u_verify_off(hdr, vc);
276 struct vhdr_crc7 *vh = hdr_priv(hdr);
279 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
281 c = crc7(p, hdr->len - hdr_size(hdr));
287 vc->good_crc = &vh->crc7;
290 log_verify_failure(hdr, vc);
294 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
296 void *p = io_u_verify_off(hdr, vc);
297 struct vhdr_crc16 *vh = hdr_priv(hdr);
300 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
302 c = crc16(p, hdr->len - hdr_size(hdr));
308 vc->good_crc = &vh->crc16;
311 log_verify_failure(hdr, vc);
315 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
317 void *p = io_u_verify_off(hdr, vc);
318 struct vhdr_crc64 *vh = hdr_priv(hdr);
319 unsigned long long c;
321 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
323 c = crc64(p, hdr->len - hdr_size(hdr));
329 vc->good_crc = &vh->crc64;
332 log_verify_failure(hdr, vc);
336 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
338 void *p = io_u_verify_off(hdr, vc);
339 struct vhdr_crc32 *vh = hdr_priv(hdr);
342 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
344 c = crc32(p, hdr->len - hdr_size(hdr));
350 vc->good_crc = &vh->crc32;
353 log_verify_failure(hdr, vc);
357 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
359 void *p = io_u_verify_off(hdr, vc);
360 struct vhdr_crc32 *vh = hdr_priv(hdr);
363 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
365 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
366 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
368 c = crc32c(p, hdr->len - hdr_size(hdr));
374 vc->good_crc = &vh->crc32;
377 log_verify_failure(hdr, vc);
381 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
383 void *p = io_u_verify_off(hdr, vc);
384 struct vhdr_md5 *vh = hdr_priv(hdr);
385 uint32_t hash[MD5_HASH_WORDS];
386 struct md5_ctx md5_ctx = {
390 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
393 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
395 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
399 vc->good_crc = vh->md5_digest;
400 vc->bad_crc = md5_ctx.hash;
401 vc->crc_len = sizeof(hash);
402 log_verify_failure(hdr, vc);
406 static unsigned int hweight8(unsigned int w)
408 unsigned int res = w - ((w >> 1) & 0x55);
410 res = (res & 0x33) + ((res >> 2) & 0x33);
411 return (res + (res >> 4)) & 0x0F;
414 int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
415 char *buf, unsigned int len, unsigned int mod)
419 for (i = 0; i < len; i++) {
420 if (buf[i] != pattern[mod]) {
423 bits = hweight8(buf[i] ^ pattern[mod]);
424 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
425 buf[i], pattern[mod], bits);
426 log_err("fio: bad pattern block offset %u\n", i);
430 if (mod == pattern_size)
438 * Push IO verification to a separate thread
440 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
443 put_file_log(td, io_u->file);
447 pthread_mutex_lock(&td->io_u_lock);
449 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
451 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
453 flist_del(&io_u->list);
454 flist_add_tail(&io_u->list, &td->verify_list);
455 io_u->flags |= IO_U_F_FREE_DEF;
456 pthread_mutex_unlock(&td->io_u_lock);
458 pthread_cond_signal(&td->verify_cond);
462 int verify_io_u(struct thread_data *td, struct io_u *io_u)
464 struct verify_header *hdr;
465 unsigned int hdr_size, hdr_inc, hdr_num = 0;
469 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
472 hdr_inc = io_u->buflen;
473 if (td->o.verify_interval)
474 hdr_inc = td->o.verify_interval;
477 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
478 p += hdr_inc, hdr_num++) {
484 if (ret && td->o.verify_fatal)
487 hdr_size = __hdr_size(td->o.verify);
488 if (td->o.verify_offset)
489 memswp(p, p + td->o.verify_offset, hdr_size);
492 if (hdr->fio_magic != FIO_HDR_MAGIC) {
493 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
494 hdr->fio_magic, FIO_HDR_MAGIC,
495 io_u->file->file_name,
496 io_u->offset + hdr_num * hdr->len, hdr->len);
500 if (td->o.verify_pattern_bytes) {
501 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
503 ret = verify_io_u_pattern(td->o.verify_pattern,
504 td->o.verify_pattern_bytes,
507 hdr_size % td->o.verify_pattern_bytes);
510 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
511 io_u->file->file_name,
512 io_u->offset + hdr_num * hdr->len,
517 * Also verify the meta data, if applicable
519 if (hdr->verify_type == VERIFY_META)
520 ret |= verify_io_u_meta(hdr, td, &vc);
524 switch (hdr->verify_type) {
526 ret = verify_io_u_md5(hdr, &vc);
529 ret = verify_io_u_crc64(hdr, &vc);
532 case VERIFY_CRC32C_INTEL:
533 ret = verify_io_u_crc32c(hdr, &vc);
536 ret = verify_io_u_crc32(hdr, &vc);
539 ret = verify_io_u_crc16(hdr, &vc);
542 ret = verify_io_u_crc7(hdr, &vc);
545 ret = verify_io_u_sha256(hdr, &vc);
548 ret = verify_io_u_sha512(hdr, &vc);
551 ret = verify_io_u_meta(hdr, td, &vc);
554 ret = verify_io_u_sha1(hdr, &vc);
557 log_err("Bad verify type %u\n", hdr->verify_type);
562 if (ret && td->o.verify_fatal)
568 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
569 struct io_u *io_u, unsigned int header_num)
571 struct vhdr_meta *vh = hdr_priv(hdr);
573 vh->thread = td->thread_number;
575 vh->time_sec = io_u->start_time.tv_sec;
576 vh->time_usec = io_u->start_time.tv_usec;
578 vh->numberio = td->io_issues[DDIR_WRITE];
580 vh->offset = io_u->offset + header_num * td->o.verify_interval;
583 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
585 struct vhdr_sha512 *vh = hdr_priv(hdr);
586 struct sha512_ctx sha512_ctx = {
590 sha512_init(&sha512_ctx);
591 sha512_update(&sha512_ctx, p, len);
594 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
596 struct vhdr_sha256 *vh = hdr_priv(hdr);
597 struct sha256_ctx sha256_ctx = {
601 sha256_init(&sha256_ctx);
602 sha256_update(&sha256_ctx, p, len);
605 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
607 struct vhdr_sha1 *vh = hdr_priv(hdr);
608 struct sha1_ctx sha1_ctx = {
612 sha1_init(&sha1_ctx);
613 sha1_update(&sha1_ctx, p, len);
616 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
618 struct vhdr_crc7 *vh = hdr_priv(hdr);
620 vh->crc7 = crc7(p, len);
623 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
625 struct vhdr_crc16 *vh = hdr_priv(hdr);
627 vh->crc16 = crc16(p, len);
630 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
632 struct vhdr_crc32 *vh = hdr_priv(hdr);
634 vh->crc32 = crc32(p, len);
637 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
639 struct vhdr_crc32 *vh = hdr_priv(hdr);
641 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
642 vh->crc32 = crc32c_intel(p, len);
644 vh->crc32 = crc32c(p, len);
647 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
649 struct vhdr_crc64 *vh = hdr_priv(hdr);
651 vh->crc64 = crc64(p, len);
654 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
656 struct vhdr_md5 *vh = hdr_priv(hdr);
657 struct md5_ctx md5_ctx = {
658 .hash = (uint32_t *) vh->md5_digest,
662 md5_update(&md5_ctx, p, len);
666 * fill body of io_u->buf with random data and add a header with the
667 * crc32 or md5 sum of that data.
669 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
671 struct verify_header *hdr;
672 void *p = io_u->buf, *data;
673 unsigned int hdr_inc, data_len, header_num = 0;
675 if (td->o.verify == VERIFY_NULL)
678 fill_pattern(td, p, io_u->buflen);
680 hdr_inc = io_u->buflen;
681 if (td->o.verify_interval)
682 hdr_inc = td->o.verify_interval;
684 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
687 hdr->fio_magic = FIO_HDR_MAGIC;
688 hdr->verify_type = td->o.verify;
690 data_len = hdr_inc - hdr_size(hdr);
692 data = p + hdr_size(hdr);
693 switch (td->o.verify) {
695 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
697 fill_md5(hdr, data, data_len);
700 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
702 fill_crc64(hdr, data, data_len);
705 case VERIFY_CRC32C_INTEL:
706 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
708 fill_crc32c(hdr, data, data_len);
711 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
713 fill_crc32(hdr, data, data_len);
716 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
718 fill_crc16(hdr, data, data_len);
721 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
723 fill_crc7(hdr, data, data_len);
726 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
728 fill_sha256(hdr, data, data_len);
731 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
733 fill_sha512(hdr, data, data_len);
736 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
738 fill_meta(hdr, td, io_u, header_num);
741 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
743 fill_sha1(hdr, data, data_len);
746 log_err("fio: bad verify type: %d\n", td->o.verify);
749 if (td->o.verify_offset)
750 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
755 int get_next_verify(struct thread_data *td, struct io_u *io_u)
757 struct io_piece *ipo = NULL;
760 * this io_u is from a requeue, we already filled the offsets
765 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
766 struct rb_node *n = rb_first(&td->io_hist_tree);
768 ipo = rb_entry(n, struct io_piece, rb_node);
769 rb_erase(n, &td->io_hist_tree);
771 } else if (!flist_empty(&td->io_hist_list)) {
772 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
774 flist_del(&ipo->list);
778 io_u->offset = ipo->offset;
779 io_u->buflen = ipo->len;
780 io_u->file = ipo->file;
782 if (!fio_file_open(io_u->file)) {
783 int r = td_io_open_file(td, io_u->file);
786 dprint(FD_VERIFY, "failed file %s open\n",
787 io_u->file->file_name);
793 assert(fio_file_open(io_u->file));
794 io_u->ddir = DDIR_READ;
795 io_u->xfer_buf = io_u->buf;
796 io_u->xfer_buflen = io_u->buflen;
798 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
802 dprint(FD_VERIFY, "get_next_verify: empty\n");
806 static void *verify_async_thread(void *data)
808 struct thread_data *td = data;
812 if (td->o.verify_cpumask_set &&
813 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
814 log_err("fio: failed setting verify thread affinity\n");
822 if (td->verify_thread_exit)
825 pthread_mutex_lock(&td->io_u_lock);
827 while (flist_empty(&td->verify_list) &&
828 !td->verify_thread_exit) {
829 ret = pthread_cond_wait(&td->verify_cond,
832 pthread_mutex_unlock(&td->io_u_lock);
837 flist_splice_init(&td->verify_list, &list);
838 pthread_mutex_unlock(&td->io_u_lock);
840 if (flist_empty(&list))
843 while (!flist_empty(&list)) {
844 io_u = flist_entry(list.next, struct io_u, list);
845 flist_del_init(&io_u->list);
847 ret = verify_io_u(td, io_u);
851 if (td->o.continue_on_error &&
852 td_non_fatal_error(ret)) {
853 update_error_count(td, ret);
861 td_verror(td, ret, "async_verify");
862 if (td->o.verify_fatal)
867 pthread_mutex_lock(&td->io_u_lock);
868 td->nr_verify_threads--;
869 pthread_mutex_unlock(&td->io_u_lock);
871 pthread_cond_signal(&td->free_cond);
875 int verify_async_init(struct thread_data *td)
879 td->verify_thread_exit = 0;
881 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
882 for (i = 0; i < td->o.verify_async; i++) {
883 ret = pthread_create(&td->verify_threads[i], NULL,
884 verify_async_thread, td);
886 log_err("fio: async verify creation failed: %s\n",
890 ret = pthread_detach(td->verify_threads[i]);
892 log_err("fio: async verify thread detach failed: %s\n",
896 td->nr_verify_threads++;
899 if (i != td->o.verify_async) {
900 log_err("fio: only %d verify threads started, exiting\n", i);
901 td->verify_thread_exit = 1;
903 pthread_cond_broadcast(&td->verify_cond);
910 void verify_async_exit(struct thread_data *td)
912 td->verify_thread_exit = 1;
914 pthread_cond_broadcast(&td->verify_cond);
916 pthread_mutex_lock(&td->io_u_lock);
918 while (td->nr_verify_threads)
919 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
921 pthread_mutex_unlock(&td->io_u_lock);
922 free(td->verify_threads);
923 td->verify_threads = NULL;