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_info("%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 * Return data area 'header_num'
162 static inline void *io_u_verify_off(struct verify_header *hdr,
163 struct io_u *io_u, unsigned char header_num)
165 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
168 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
169 struct io_u *io_u, unsigned int header_num)
171 struct vhdr_meta *vh = hdr_priv(hdr);
173 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
175 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
176 log_err("meta: verify failed at %llu/%u\n",
177 io_u->offset + header_num * hdr->len, hdr->len);
184 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
185 unsigned int header_num)
187 void *p = io_u_verify_off(hdr, io_u, header_num);
188 struct vhdr_sha512 *vh = hdr_priv(hdr);
190 struct sha512_ctx sha512_ctx = {
194 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
196 sha512_init(&sha512_ctx);
197 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
199 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
200 log_err("sha512: verify failed at %llu/%u\n",
201 io_u->offset + header_num * hdr->len, hdr->len);
202 hexdump(vh->sha512, sizeof(vh->sha512));
203 hexdump(sha512_ctx.buf, sizeof(sha512));
210 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
211 unsigned int header_num)
213 void *p = io_u_verify_off(hdr, io_u, header_num);
214 struct vhdr_sha256 *vh = hdr_priv(hdr);
216 struct sha256_ctx sha256_ctx = {
220 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
222 sha256_init(&sha256_ctx);
223 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
225 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
226 log_err("sha256: verify failed at %llu/%u\n",
227 io_u->offset + header_num * hdr->len, hdr->len);
228 hexdump(vh->sha256, sizeof(vh->sha256));
229 hexdump(sha256_ctx.buf, sizeof(sha256));
236 static int verify_io_u_sha1(struct verify_header *hdr, struct io_u *io_u,
237 unsigned int header_num)
239 void *p = io_u_verify_off(hdr, io_u, header_num);
240 struct vhdr_sha1 *vh = hdr_priv(hdr);
242 struct sha1_ctx sha1_ctx = {
246 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", io_u, hdr->len);
248 sha1_init(&sha1_ctx);
249 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
251 if (memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) {
252 log_err("sha1: verify failed at %llu/%u\n",
253 io_u->offset + header_num * hdr->len, hdr->len);
254 hexdump(vh->sha1, sizeof(vh->sha1));
255 hexdump(sha1_ctx.H, sizeof(sha1));
262 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
263 unsigned char header_num)
265 void *p = io_u_verify_off(hdr, io_u, header_num);
266 struct vhdr_crc7 *vh = hdr_priv(hdr);
269 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
271 c = crc7(p, hdr->len - hdr_size(hdr));
274 log_err("crc7: verify failed at %llu/%u\n",
275 io_u->offset + header_num * hdr->len, hdr->len);
276 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
283 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
284 unsigned int header_num)
286 void *p = io_u_verify_off(hdr, io_u, header_num);
287 struct vhdr_crc16 *vh = hdr_priv(hdr);
290 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
292 c = crc16(p, hdr->len - hdr_size(hdr));
294 if (c != vh->crc16) {
295 log_err("crc16: verify failed at %llu/%u\n",
296 io_u->offset + header_num * hdr->len, hdr->len);
297 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
304 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
305 unsigned int header_num)
307 void *p = io_u_verify_off(hdr, io_u, header_num);
308 struct vhdr_crc64 *vh = hdr_priv(hdr);
309 unsigned long long c;
311 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
313 c = crc64(p, hdr->len - hdr_size(hdr));
315 if (c != vh->crc64) {
316 log_err("crc64: verify failed at %llu/%u\n",
317 io_u->offset + header_num * hdr->len,
319 log_err("crc64: wanted %llx, got %llx\n",
320 (unsigned long long) vh->crc64, c);
327 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
328 unsigned int header_num)
330 void *p = io_u_verify_off(hdr, io_u, header_num);
331 struct vhdr_crc32 *vh = hdr_priv(hdr);
334 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
336 c = crc32(p, hdr->len - hdr_size(hdr));
338 if (c != vh->crc32) {
339 log_err("crc32: verify failed at %llu/%u\n",
340 io_u->offset + header_num * hdr->len, hdr->len);
341 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
348 static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
349 unsigned int header_num)
351 void *p = io_u_verify_off(hdr, io_u, header_num);
352 struct vhdr_crc32 *vh = hdr_priv(hdr);
355 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
357 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
358 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
360 c = crc32c(p, hdr->len - hdr_size(hdr));
362 if (c != vh->crc32) {
363 log_err("crc32c: verify failed at %llu/%u\n",
364 io_u->offset + header_num * hdr->len, hdr->len);
365 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
372 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
373 unsigned int header_num)
375 void *p = io_u_verify_off(hdr, io_u, header_num);
376 struct vhdr_md5 *vh = hdr_priv(hdr);
377 uint32_t hash[MD5_HASH_WORDS];
378 struct md5_ctx md5_ctx = {
382 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
385 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
387 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
388 log_err("md5: verify failed at %llu/%u\n",
389 io_u->offset + header_num * hdr->len, hdr->len);
390 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
391 hexdump(md5_ctx.hash, sizeof(hash));
398 static unsigned int hweight8(unsigned int w)
400 unsigned int res = w - ((w >> 1) & 0x55);
402 res = (res & 0x33) + ((res >> 2) & 0x33);
403 return (res + (res >> 4)) & 0x0F;
406 int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
407 char *buf, unsigned int len, unsigned int mod)
411 for (i = 0; i < len; i++) {
412 if (buf[i] != pattern[mod]) {
415 bits = hweight8(buf[i] ^ pattern[mod]);
416 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
417 buf[i], pattern[mod], bits);
418 log_err("fio: bad pattern block offset %u\n", i);
422 if (mod == pattern_size)
430 * Push IO verification to a separate thread
432 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
435 put_file_log(td, io_u->file);
439 pthread_mutex_lock(&td->io_u_lock);
441 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
443 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
445 flist_del(&io_u->list);
446 flist_add_tail(&io_u->list, &td->verify_list);
447 io_u->flags |= IO_U_F_FREE_DEF;
448 pthread_mutex_unlock(&td->io_u_lock);
450 pthread_cond_signal(&td->verify_cond);
454 int verify_io_u(struct thread_data *td, struct io_u *io_u)
456 struct verify_header *hdr;
457 unsigned int hdr_size, hdr_inc, hdr_num = 0;
461 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
464 hdr_inc = io_u->buflen;
465 if (td->o.verify_interval)
466 hdr_inc = td->o.verify_interval;
469 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
470 p += hdr_inc, hdr_num++) {
471 if (ret && td->o.verify_fatal) {
475 hdr_size = __hdr_size(td->o.verify);
476 if (td->o.verify_offset)
477 memswp(p, p + td->o.verify_offset, hdr_size);
480 if (hdr->fio_magic != FIO_HDR_MAGIC) {
481 log_err("Bad verify header %x at %llu\n",
483 io_u->offset + hdr_num * hdr->len);
487 if (td->o.verify_pattern_bytes) {
488 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
490 ret = verify_io_u_pattern(td->o.verify_pattern,
491 td->o.verify_pattern_bytes,
494 hdr_size % td->o.verify_pattern_bytes);
496 * Also verify the meta data, if applicable
498 if (hdr->verify_type == VERIFY_META)
499 ret |= verify_io_u_meta(hdr, td, io_u, hdr_num);
502 log_err("fio: verify failed at %llu/%u\n",
503 io_u->offset + hdr_num * hdr->len,
508 switch (hdr->verify_type) {
510 ret = verify_io_u_md5(hdr, io_u, hdr_num);
513 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
516 case VERIFY_CRC32C_INTEL:
517 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
520 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
523 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
526 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
529 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
532 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
535 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
538 ret = verify_io_u_sha1(hdr, io_u, hdr_num);
541 log_err("Bad verify type %u\n", hdr->verify_type);
549 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
550 struct io_u *io_u, unsigned int header_num)
552 struct vhdr_meta *vh = hdr_priv(hdr);
554 vh->thread = td->thread_number;
556 vh->time_sec = io_u->start_time.tv_sec;
557 vh->time_usec = io_u->start_time.tv_usec;
559 vh->numberio = td->io_issues[DDIR_WRITE];
561 vh->offset = io_u->offset + header_num * td->o.verify_interval;
564 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
566 struct vhdr_sha512 *vh = hdr_priv(hdr);
567 struct sha512_ctx sha512_ctx = {
571 sha512_init(&sha512_ctx);
572 sha512_update(&sha512_ctx, p, len);
575 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
577 struct vhdr_sha256 *vh = hdr_priv(hdr);
578 struct sha256_ctx sha256_ctx = {
582 sha256_init(&sha256_ctx);
583 sha256_update(&sha256_ctx, p, len);
586 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
588 struct vhdr_sha1 *vh = hdr_priv(hdr);
589 struct sha1_ctx sha1_ctx = {
593 sha1_init(&sha1_ctx);
594 sha1_update(&sha1_ctx, p, len);
597 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
599 struct vhdr_crc7 *vh = hdr_priv(hdr);
601 vh->crc7 = crc7(p, len);
604 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
606 struct vhdr_crc16 *vh = hdr_priv(hdr);
608 vh->crc16 = crc16(p, len);
611 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
613 struct vhdr_crc32 *vh = hdr_priv(hdr);
615 vh->crc32 = crc32(p, len);
618 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
620 struct vhdr_crc32 *vh = hdr_priv(hdr);
622 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
623 vh->crc32 = crc32c_intel(p, len);
625 vh->crc32 = crc32c(p, len);
628 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
630 struct vhdr_crc64 *vh = hdr_priv(hdr);
632 vh->crc64 = crc64(p, len);
635 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
637 struct vhdr_md5 *vh = hdr_priv(hdr);
638 struct md5_ctx md5_ctx = {
639 .hash = (uint32_t *) vh->md5_digest,
643 md5_update(&md5_ctx, p, len);
647 * fill body of io_u->buf with random data and add a header with the
648 * crc32 or md5 sum of that data.
650 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
652 struct verify_header *hdr;
653 void *p = io_u->buf, *data;
654 unsigned int hdr_inc, data_len, header_num = 0;
656 if (td->o.verify == VERIFY_NULL)
659 fill_pattern(td, p, io_u->buflen);
661 hdr_inc = io_u->buflen;
662 if (td->o.verify_interval)
663 hdr_inc = td->o.verify_interval;
665 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
668 hdr->fio_magic = FIO_HDR_MAGIC;
669 hdr->verify_type = td->o.verify;
671 data_len = hdr_inc - hdr_size(hdr);
673 data = p + hdr_size(hdr);
674 switch (td->o.verify) {
676 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
678 fill_md5(hdr, data, data_len);
681 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
683 fill_crc64(hdr, data, data_len);
686 case VERIFY_CRC32C_INTEL:
687 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
689 fill_crc32c(hdr, data, data_len);
692 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
694 fill_crc32(hdr, data, data_len);
697 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
699 fill_crc16(hdr, data, data_len);
702 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
704 fill_crc7(hdr, data, data_len);
707 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
709 fill_sha256(hdr, data, data_len);
712 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
714 fill_sha512(hdr, data, data_len);
717 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
719 fill_meta(hdr, td, io_u, header_num);
722 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
724 fill_sha1(hdr, data, data_len);
727 log_err("fio: bad verify type: %d\n", td->o.verify);
730 if (td->o.verify_offset)
731 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
736 int get_next_verify(struct thread_data *td, struct io_u *io_u)
738 struct io_piece *ipo = NULL;
741 * this io_u is from a requeue, we already filled the offsets
746 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
747 struct rb_node *n = rb_first(&td->io_hist_tree);
749 ipo = rb_entry(n, struct io_piece, rb_node);
750 rb_erase(n, &td->io_hist_tree);
751 } else if (!flist_empty(&td->io_hist_list)) {
752 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
753 flist_del(&ipo->list);
757 io_u->offset = ipo->offset;
758 io_u->buflen = ipo->len;
759 io_u->file = ipo->file;
761 if (!fio_file_open(io_u->file)) {
762 int r = td_io_open_file(td, io_u->file);
765 dprint(FD_VERIFY, "failed file %s open\n",
766 io_u->file->file_name);
772 assert(fio_file_open(io_u->file));
773 io_u->ddir = DDIR_READ;
774 io_u->xfer_buf = io_u->buf;
775 io_u->xfer_buflen = io_u->buflen;
777 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
781 dprint(FD_VERIFY, "get_next_verify: empty\n");
785 static void *verify_async_thread(void *data)
787 struct thread_data *td = data;
791 if (td->o.verify_cpumask_set &&
792 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
793 log_err("fio: failed setting verify thread affinity\n");
801 if (td->verify_thread_exit)
804 pthread_mutex_lock(&td->io_u_lock);
806 while (flist_empty(&td->verify_list) &&
807 !td->verify_thread_exit) {
808 ret = pthread_cond_wait(&td->verify_cond,
811 pthread_mutex_unlock(&td->io_u_lock);
816 flist_splice_init(&td->verify_list, &list);
817 pthread_mutex_unlock(&td->io_u_lock);
819 if (flist_empty(&list))
822 while (!flist_empty(&list)) {
823 io_u = flist_entry(list.next, struct io_u, list);
824 flist_del_init(&io_u->list);
826 ret = verify_io_u(td, io_u);
830 if (td->o.continue_on_error &&
831 td_non_fatal_error(ret)) {
832 update_error_count(td, ret);
840 td_verror(td, ret, "async_verify");
845 pthread_mutex_lock(&td->io_u_lock);
846 td->nr_verify_threads--;
847 pthread_mutex_unlock(&td->io_u_lock);
849 pthread_cond_signal(&td->free_cond);
853 int verify_async_init(struct thread_data *td)
857 td->verify_thread_exit = 0;
859 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
860 for (i = 0; i < td->o.verify_async; i++) {
861 ret = pthread_create(&td->verify_threads[i], NULL,
862 verify_async_thread, td);
864 log_err("fio: async verify creation failed: %s\n",
868 ret = pthread_detach(td->verify_threads[i]);
870 log_err("fio: async verify thread detach failed: %s\n",
874 td->nr_verify_threads++;
877 if (i != td->o.verify_async) {
878 log_err("fio: only %d verify threads started, exiting\n", i);
879 td->verify_thread_exit = 1;
881 pthread_cond_broadcast(&td->verify_cond);
888 void verify_async_exit(struct thread_data *td)
890 td->verify_thread_exit = 1;
892 pthread_cond_broadcast(&td->verify_cond);
894 pthread_mutex_lock(&td->io_u_lock);
896 while (td->nr_verify_threads)
897 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
899 pthread_mutex_unlock(&td->io_u_lock);
900 free(td->verify_threads);
901 td->verify_threads = NULL;