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 * 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
173 unsigned char *good_crc;
174 unsigned char *bad_crc;
175 unsigned int crc_len;
179 * Return data area 'header_num'
181 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
183 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
186 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
189 struct vhdr_meta *vh = hdr_priv(hdr);
190 struct io_u *io_u = vc->io_u;
192 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
194 if (vh->offset != io_u->offset + vc->hdr_num * td->o.verify_interval) {
195 log_err("meta: verify failed at %llu/%u\n",
196 io_u->offset + vc->hdr_num * hdr->len, hdr->len);
203 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
205 void *p = io_u_verify_off(hdr, vc);
206 struct vhdr_sha512 *vh = hdr_priv(hdr);
208 struct sha512_ctx sha512_ctx = {
212 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
214 sha512_init(&sha512_ctx);
215 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
217 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
218 log_err("sha512: verify failed at %llu/%u\n",
219 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
220 hexdump(vh->sha512, sizeof(vh->sha512));
221 hexdump(sha512_ctx.buf, sizeof(sha512));
228 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
230 void *p = io_u_verify_off(hdr, vc);
231 struct vhdr_sha256 *vh = hdr_priv(hdr);
233 struct sha256_ctx sha256_ctx = {
237 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
239 sha256_init(&sha256_ctx);
240 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
242 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
243 log_err("sha256: verify failed at %llu/%u\n",
244 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
245 hexdump(vh->sha256, sizeof(vh->sha256));
246 hexdump(sha256_ctx.buf, sizeof(sha256));
253 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
255 void *p = io_u_verify_off(hdr, vc);
256 struct vhdr_sha1 *vh = hdr_priv(hdr);
258 struct sha1_ctx sha1_ctx = {
262 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
264 sha1_init(&sha1_ctx);
265 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
267 if (memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) {
268 log_err("sha1: verify failed at %llu/%u\n",
269 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
270 hexdump(vh->sha1, sizeof(vh->sha1));
271 hexdump(sha1_ctx.H, sizeof(sha1));
278 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
280 void *p = io_u_verify_off(hdr, vc);
281 struct vhdr_crc7 *vh = hdr_priv(hdr);
284 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
286 c = crc7(p, hdr->len - hdr_size(hdr));
289 log_err("crc7: verify failed at %llu/%u\n",
290 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
291 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
298 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
300 void *p = io_u_verify_off(hdr, vc);
301 struct vhdr_crc16 *vh = hdr_priv(hdr);
304 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
306 c = crc16(p, hdr->len - hdr_size(hdr));
308 if (c != vh->crc16) {
309 log_err("crc16: verify failed at %llu/%u\n",
310 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
311 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
318 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
320 void *p = io_u_verify_off(hdr, vc);
321 struct vhdr_crc64 *vh = hdr_priv(hdr);
322 unsigned long long c;
324 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
326 c = crc64(p, hdr->len - hdr_size(hdr));
328 if (c != vh->crc64) {
329 log_err("crc64: verify failed at %llu/%u\n",
330 vc->io_u->offset + vc->hdr_num * hdr->len,
332 log_err("crc64: wanted %llx, got %llx\n",
333 (unsigned long long) vh->crc64, c);
340 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
342 void *p = io_u_verify_off(hdr, vc);
343 struct vhdr_crc32 *vh = hdr_priv(hdr);
346 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
348 c = crc32(p, hdr->len - hdr_size(hdr));
350 if (c != vh->crc32) {
351 log_err("crc32: verify failed at %llu/%u\n",
352 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
353 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
360 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
362 void *p = io_u_verify_off(hdr, vc);
363 struct vhdr_crc32 *vh = hdr_priv(hdr);
366 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
368 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
369 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
371 c = crc32c(p, hdr->len - hdr_size(hdr));
373 if (c != vh->crc32) {
374 log_err("crc32c: verify failed at %llu/%u\n",
375 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
376 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
383 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
385 void *p = io_u_verify_off(hdr, vc);
386 struct vhdr_md5 *vh = hdr_priv(hdr);
387 uint32_t hash[MD5_HASH_WORDS];
388 struct md5_ctx md5_ctx = {
392 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
395 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
397 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
398 log_err("md5: verify failed at %llu/%u\n",
399 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
400 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
401 hexdump(md5_ctx.hash, sizeof(hash));
408 static unsigned int hweight8(unsigned int w)
410 unsigned int res = w - ((w >> 1) & 0x55);
412 res = (res & 0x33) + ((res >> 2) & 0x33);
413 return (res + (res >> 4)) & 0x0F;
416 int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
417 char *buf, unsigned int len, unsigned int mod)
421 for (i = 0; i < len; i++) {
422 if (buf[i] != pattern[mod]) {
425 bits = hweight8(buf[i] ^ pattern[mod]);
426 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
427 buf[i], pattern[mod], bits);
428 log_err("fio: bad pattern block offset %u\n", i);
432 if (mod == pattern_size)
440 * Push IO verification to a separate thread
442 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
445 put_file_log(td, io_u->file);
449 pthread_mutex_lock(&td->io_u_lock);
451 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
453 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
455 flist_del(&io_u->list);
456 flist_add_tail(&io_u->list, &td->verify_list);
457 io_u->flags |= IO_U_F_FREE_DEF;
458 pthread_mutex_unlock(&td->io_u_lock);
460 pthread_cond_signal(&td->verify_cond);
464 int verify_io_u(struct thread_data *td, struct io_u *io_u)
466 struct verify_header *hdr;
467 unsigned int hdr_size, hdr_inc, hdr_num = 0;
471 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
474 hdr_inc = io_u->buflen;
475 if (td->o.verify_interval)
476 hdr_inc = td->o.verify_interval;
479 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
480 p += hdr_inc, hdr_num++) {
483 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);
493 vc.hdr_num = hdr_num;
495 if (hdr->fio_magic != FIO_HDR_MAGIC) {
496 log_err("Bad verify header %x at %llu\n",
498 io_u->offset + hdr_num * hdr->len);
502 if (td->o.verify_pattern_bytes) {
503 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
505 ret = verify_io_u_pattern(td->o.verify_pattern,
506 td->o.verify_pattern_bytes,
509 hdr_size % td->o.verify_pattern_bytes);
511 * Also verify the meta data, if applicable
513 if (hdr->verify_type == VERIFY_META)
514 ret |= verify_io_u_meta(hdr, td, &vc);
517 log_err("fio: verify failed at %llu/%u\n",
518 io_u->offset + hdr_num * hdr->len,
523 switch (hdr->verify_type) {
525 ret = verify_io_u_md5(hdr, &vc);
528 ret = verify_io_u_crc64(hdr, &vc);
531 case VERIFY_CRC32C_INTEL:
532 ret = verify_io_u_crc32c(hdr, &vc);
535 ret = verify_io_u_crc32(hdr, &vc);
538 ret = verify_io_u_crc16(hdr, &vc);
541 ret = verify_io_u_crc7(hdr, &vc);
544 ret = verify_io_u_sha256(hdr, &vc);
547 ret = verify_io_u_sha512(hdr, &vc);
550 ret = verify_io_u_meta(hdr, td, &vc);
553 ret = verify_io_u_sha1(hdr, &vc);
556 log_err("Bad verify type %u\n", hdr->verify_type);
564 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
565 struct io_u *io_u, unsigned int header_num)
567 struct vhdr_meta *vh = hdr_priv(hdr);
569 vh->thread = td->thread_number;
571 vh->time_sec = io_u->start_time.tv_sec;
572 vh->time_usec = io_u->start_time.tv_usec;
574 vh->numberio = td->io_issues[DDIR_WRITE];
576 vh->offset = io_u->offset + header_num * td->o.verify_interval;
579 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
581 struct vhdr_sha512 *vh = hdr_priv(hdr);
582 struct sha512_ctx sha512_ctx = {
586 sha512_init(&sha512_ctx);
587 sha512_update(&sha512_ctx, p, len);
590 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
592 struct vhdr_sha256 *vh = hdr_priv(hdr);
593 struct sha256_ctx sha256_ctx = {
597 sha256_init(&sha256_ctx);
598 sha256_update(&sha256_ctx, p, len);
601 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
603 struct vhdr_sha1 *vh = hdr_priv(hdr);
604 struct sha1_ctx sha1_ctx = {
608 sha1_init(&sha1_ctx);
609 sha1_update(&sha1_ctx, p, len);
612 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
614 struct vhdr_crc7 *vh = hdr_priv(hdr);
616 vh->crc7 = crc7(p, len);
619 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
621 struct vhdr_crc16 *vh = hdr_priv(hdr);
623 vh->crc16 = crc16(p, len);
626 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
628 struct vhdr_crc32 *vh = hdr_priv(hdr);
630 vh->crc32 = crc32(p, len);
633 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
635 struct vhdr_crc32 *vh = hdr_priv(hdr);
637 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
638 vh->crc32 = crc32c_intel(p, len);
640 vh->crc32 = crc32c(p, len);
643 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
645 struct vhdr_crc64 *vh = hdr_priv(hdr);
647 vh->crc64 = crc64(p, len);
650 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
652 struct vhdr_md5 *vh = hdr_priv(hdr);
653 struct md5_ctx md5_ctx = {
654 .hash = (uint32_t *) vh->md5_digest,
658 md5_update(&md5_ctx, p, len);
662 * fill body of io_u->buf with random data and add a header with the
663 * crc32 or md5 sum of that data.
665 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
667 struct verify_header *hdr;
668 void *p = io_u->buf, *data;
669 unsigned int hdr_inc, data_len, header_num = 0;
671 if (td->o.verify == VERIFY_NULL)
674 fill_pattern(td, p, io_u->buflen);
676 hdr_inc = io_u->buflen;
677 if (td->o.verify_interval)
678 hdr_inc = td->o.verify_interval;
680 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
683 hdr->fio_magic = FIO_HDR_MAGIC;
684 hdr->verify_type = td->o.verify;
686 data_len = hdr_inc - hdr_size(hdr);
688 data = p + hdr_size(hdr);
689 switch (td->o.verify) {
691 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
693 fill_md5(hdr, data, data_len);
696 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
698 fill_crc64(hdr, data, data_len);
701 case VERIFY_CRC32C_INTEL:
702 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
704 fill_crc32c(hdr, data, data_len);
707 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
709 fill_crc32(hdr, data, data_len);
712 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
714 fill_crc16(hdr, data, data_len);
717 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
719 fill_crc7(hdr, data, data_len);
722 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
724 fill_sha256(hdr, data, data_len);
727 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
729 fill_sha512(hdr, data, data_len);
732 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
734 fill_meta(hdr, td, io_u, header_num);
737 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
739 fill_sha1(hdr, data, data_len);
742 log_err("fio: bad verify type: %d\n", td->o.verify);
745 if (td->o.verify_offset)
746 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
751 int get_next_verify(struct thread_data *td, struct io_u *io_u)
753 struct io_piece *ipo = NULL;
756 * this io_u is from a requeue, we already filled the offsets
761 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
762 struct rb_node *n = rb_first(&td->io_hist_tree);
764 ipo = rb_entry(n, struct io_piece, rb_node);
765 rb_erase(n, &td->io_hist_tree);
767 } else if (!flist_empty(&td->io_hist_list)) {
768 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
770 flist_del(&ipo->list);
774 io_u->offset = ipo->offset;
775 io_u->buflen = ipo->len;
776 io_u->file = ipo->file;
778 if (!fio_file_open(io_u->file)) {
779 int r = td_io_open_file(td, io_u->file);
782 dprint(FD_VERIFY, "failed file %s open\n",
783 io_u->file->file_name);
789 assert(fio_file_open(io_u->file));
790 io_u->ddir = DDIR_READ;
791 io_u->xfer_buf = io_u->buf;
792 io_u->xfer_buflen = io_u->buflen;
794 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
798 dprint(FD_VERIFY, "get_next_verify: empty\n");
802 static void *verify_async_thread(void *data)
804 struct thread_data *td = data;
808 if (td->o.verify_cpumask_set &&
809 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
810 log_err("fio: failed setting verify thread affinity\n");
818 if (td->verify_thread_exit)
821 pthread_mutex_lock(&td->io_u_lock);
823 while (flist_empty(&td->verify_list) &&
824 !td->verify_thread_exit) {
825 ret = pthread_cond_wait(&td->verify_cond,
828 pthread_mutex_unlock(&td->io_u_lock);
833 flist_splice_init(&td->verify_list, &list);
834 pthread_mutex_unlock(&td->io_u_lock);
836 if (flist_empty(&list))
839 while (!flist_empty(&list)) {
840 io_u = flist_entry(list.next, struct io_u, list);
841 flist_del_init(&io_u->list);
843 ret = verify_io_u(td, io_u);
847 if (td->o.continue_on_error &&
848 td_non_fatal_error(ret)) {
849 update_error_count(td, ret);
857 td_verror(td, ret, "async_verify");
862 pthread_mutex_lock(&td->io_u_lock);
863 td->nr_verify_threads--;
864 pthread_mutex_unlock(&td->io_u_lock);
866 pthread_cond_signal(&td->free_cond);
870 int verify_async_init(struct thread_data *td)
874 td->verify_thread_exit = 0;
876 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
877 for (i = 0; i < td->o.verify_async; i++) {
878 ret = pthread_create(&td->verify_threads[i], NULL,
879 verify_async_thread, td);
881 log_err("fio: async verify creation failed: %s\n",
885 ret = pthread_detach(td->verify_threads[i]);
887 log_err("fio: async verify thread detach failed: %s\n",
891 td->nr_verify_threads++;
894 if (i != td->o.verify_async) {
895 log_err("fio: only %d verify threads started, exiting\n", i);
896 td->verify_thread_exit = 1;
898 pthread_cond_broadcast(&td->verify_cond);
905 void verify_async_exit(struct thread_data *td)
907 td->verify_thread_exit = 1;
909 pthread_cond_broadcast(&td->verify_cond);
911 pthread_mutex_lock(&td->io_u_lock);
913 while (td->nr_verify_threads)
914 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
916 pthread_mutex_unlock(&td->io_u_lock);
917 free(td->verify_threads);
918 td->verify_threads = NULL;