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, len);
61 unsigned int pattern = td->o.verify_pattern;
63 unsigned char c1, c2, c3, c4;
66 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
67 td->o.verify_pattern_bytes, len);
82 if (td->o.verify_pattern_bytes == 2 || i == len)
85 if (td->o.verify_pattern_bytes == 3 || i == len)
94 static void memswp(void *buf1, void *buf2, unsigned int len)
98 assert(len <= sizeof(swap));
100 memcpy(&swap, buf1, len);
101 memcpy(buf1, buf2, len);
102 memcpy(buf2, &swap, len);
105 static void hexdump(void *buffer, int len)
107 unsigned char *p = buffer;
110 for (i = 0; i < len; i++)
111 log_info("%02x", p[i]);
116 * Prepare for seperation of verify_header and checksum header
118 static inline unsigned int __hdr_size(int verify_type)
120 unsigned int len = len;
122 switch (verify_type) {
128 len = sizeof(struct vhdr_md5);
131 len = sizeof(struct vhdr_crc64);
135 case VERIFY_CRC32C_INTEL:
136 len = sizeof(struct vhdr_crc32);
139 len = sizeof(struct vhdr_crc16);
142 len = sizeof(struct vhdr_crc7);
145 len = sizeof(struct vhdr_sha256);
148 len = sizeof(struct vhdr_sha512);
151 len = sizeof(struct vhdr_meta);
154 len = sizeof(struct vhdr_sha1);
157 log_err("fio: unknown verify header!\n");
161 return len + sizeof(struct verify_header);
164 static inline unsigned int hdr_size(struct verify_header *hdr)
166 return __hdr_size(hdr->verify_type);
169 static void *hdr_priv(struct verify_header *hdr)
173 return priv + sizeof(struct verify_header);
177 * Return data area 'header_num'
179 static inline void *io_u_verify_off(struct verify_header *hdr,
180 struct io_u *io_u, unsigned char header_num)
182 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
185 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
186 struct io_u *io_u, unsigned int header_num)
188 struct vhdr_meta *vh = hdr_priv(hdr);
190 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
192 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
193 log_err("meta: verify failed at %llu/%u\n",
194 io_u->offset + header_num * hdr->len, hdr->len);
201 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
202 unsigned int header_num)
204 void *p = io_u_verify_off(hdr, io_u, header_num);
205 struct vhdr_sha512 *vh = hdr_priv(hdr);
207 struct sha512_ctx sha512_ctx = {
211 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
213 sha512_init(&sha512_ctx);
214 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
216 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
217 log_err("sha512: verify failed at %llu/%u\n",
218 io_u->offset + header_num * hdr->len, hdr->len);
219 hexdump(vh->sha512, sizeof(vh->sha512));
220 hexdump(sha512_ctx.buf, sizeof(sha512));
227 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
228 unsigned int header_num)
230 void *p = io_u_verify_off(hdr, io_u, header_num);
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", 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 io_u->offset + header_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 io_u *io_u,
254 unsigned int header_num)
256 void *p = io_u_verify_off(hdr, io_u, header_num);
257 struct vhdr_sha1 *vh = hdr_priv(hdr);
259 struct sha1_ctx sha1_ctx = {
263 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", io_u, hdr->len);
265 sha1_init(&sha1_ctx);
266 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
268 if (memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) {
269 log_err("sha1: verify failed at %llu/%u\n",
270 io_u->offset + header_num * hdr->len, hdr->len);
271 hexdump(vh->sha1, sizeof(vh->sha1));
272 hexdump(sha1_ctx.H, sizeof(sha1));
279 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
280 unsigned char header_num)
282 void *p = io_u_verify_off(hdr, io_u, header_num);
283 struct vhdr_crc7 *vh = hdr_priv(hdr);
286 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
288 c = crc7(p, hdr->len - hdr_size(hdr));
291 log_err("crc7: verify failed at %llu/%u\n",
292 io_u->offset + header_num * hdr->len, hdr->len);
293 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
300 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
301 unsigned int header_num)
303 void *p = io_u_verify_off(hdr, io_u, header_num);
304 struct vhdr_crc16 *vh = hdr_priv(hdr);
307 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
309 c = crc16(p, hdr->len - hdr_size(hdr));
311 if (c != vh->crc16) {
312 log_err("crc16: verify failed at %llu/%u\n",
313 io_u->offset + header_num * hdr->len, hdr->len);
314 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
321 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
322 unsigned int header_num)
324 void *p = io_u_verify_off(hdr, io_u, header_num);
325 struct vhdr_crc64 *vh = hdr_priv(hdr);
326 unsigned long long c;
328 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
330 c = crc64(p, hdr->len - hdr_size(hdr));
332 if (c != vh->crc64) {
333 log_err("crc64: verify failed at %llu/%u\n",
334 io_u->offset + header_num * hdr->len,
336 log_err("crc64: wanted %llx, got %llx\n",
337 (unsigned long long) vh->crc64, c);
344 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
345 unsigned int header_num)
347 void *p = io_u_verify_off(hdr, io_u, header_num);
348 struct vhdr_crc32 *vh = hdr_priv(hdr);
351 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
353 c = crc32(p, hdr->len - hdr_size(hdr));
355 if (c != vh->crc32) {
356 log_err("crc32: verify failed at %llu/%u\n",
357 io_u->offset + header_num * hdr->len, hdr->len);
358 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
365 static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
366 unsigned int header_num)
368 void *p = io_u_verify_off(hdr, io_u, header_num);
369 struct vhdr_crc32 *vh = hdr_priv(hdr);
372 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
374 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
375 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
377 c = crc32c(p, hdr->len - hdr_size(hdr));
379 if (c != vh->crc32) {
380 log_err("crc32c: verify failed at %llu/%u\n",
381 io_u->offset + header_num * hdr->len, hdr->len);
382 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
389 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
390 unsigned int header_num)
392 void *p = io_u_verify_off(hdr, io_u, header_num);
393 struct vhdr_md5 *vh = hdr_priv(hdr);
394 uint32_t hash[MD5_HASH_WORDS];
395 struct md5_ctx md5_ctx = {
399 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
402 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
404 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
405 log_err("md5: verify failed at %llu/%u\n",
406 io_u->offset + header_num * hdr->len, hdr->len);
407 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
408 hexdump(md5_ctx.hash, sizeof(hash));
415 static unsigned int hweight8(unsigned int w)
417 unsigned int res = w - ((w >> 1) & 0x55);
419 res = (res & 0x33) + ((res >> 2) & 0x33);
420 return (res + (res >> 4)) & 0x0F;
423 int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
424 char *buf, unsigned int len, unsigned int mod)
427 char split_pattern[4];
429 for (i = 0; i < 4; i++) {
430 split_pattern[i] = pattern & 0xff;
434 for (i = 0; i < len; i++) {
435 if (buf[i] != split_pattern[mod]) {
438 bits = hweight8(buf[i] ^ split_pattern[mod]);
439 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
440 buf[i], split_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);
463 flist_del(&io_u->list);
464 flist_add_tail(&io_u->list, &td->verify_list);
465 pthread_mutex_unlock(&td->io_u_lock);
467 pthread_cond_signal(&td->verify_cond);
468 io_u->flags |= IO_U_F_FREE_DEF;
472 int verify_io_u(struct thread_data *td, struct io_u *io_u)
474 struct verify_header *hdr;
475 unsigned int hdr_size, hdr_inc, hdr_num = 0;
479 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
482 hdr_inc = io_u->buflen;
483 if (td->o.verify_interval)
484 hdr_inc = td->o.verify_interval;
487 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
488 p += hdr_inc, hdr_num++) {
489 if (ret && td->o.verify_fatal) {
493 hdr_size = __hdr_size(td->o.verify);
494 if (td->o.verify_offset)
495 memswp(p, p + td->o.verify_offset, hdr_size);
498 if (hdr->fio_magic != FIO_HDR_MAGIC) {
499 log_err("Bad verify header %x\n", hdr->fio_magic);
503 if (td->o.verify_pattern_bytes) {
504 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
506 ret = verify_io_u_pattern(td->o.verify_pattern,
507 td->o.verify_pattern_bytes,
512 * Also verify the meta data, if applicable
514 if (hdr->verify_type == VERIFY_META)
515 ret |= verify_io_u_meta(hdr, td, io_u, hdr_num);
518 log_err("fio: verify failed at %llu/%u\n",
519 io_u->offset + hdr_num * hdr->len,
524 switch (hdr->verify_type) {
526 ret = verify_io_u_md5(hdr, io_u, hdr_num);
529 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
532 case VERIFY_CRC32C_INTEL:
533 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
536 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
539 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
542 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
545 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
548 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
551 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
554 ret = verify_io_u_sha1(hdr, io_u, hdr_num);
557 log_err("Bad verify type %u\n", hdr->verify_type);
565 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
566 struct io_u *io_u, unsigned int header_num)
568 struct vhdr_meta *vh = hdr_priv(hdr);
570 vh->thread = td->thread_number;
572 vh->time_sec = io_u->start_time.tv_sec;
573 vh->time_usec = io_u->start_time.tv_usec;
575 vh->numberio = td->io_issues[DDIR_WRITE];
577 vh->offset = io_u->offset + header_num * td->o.verify_interval;
580 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
582 struct vhdr_sha512 *vh = hdr_priv(hdr);
583 struct sha512_ctx sha512_ctx = {
587 sha512_init(&sha512_ctx);
588 sha512_update(&sha512_ctx, p, len);
591 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
593 struct vhdr_sha256 *vh = hdr_priv(hdr);
594 struct sha256_ctx sha256_ctx = {
598 sha256_init(&sha256_ctx);
599 sha256_update(&sha256_ctx, p, len);
602 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
604 struct vhdr_sha1 *vh = hdr_priv(hdr);
605 struct sha1_ctx sha1_ctx = {
609 sha1_init(&sha1_ctx);
610 sha1_update(&sha1_ctx, p, len);
613 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
615 struct vhdr_crc7 *vh = hdr_priv(hdr);
617 vh->crc7 = crc7(p, len);
620 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
622 struct vhdr_crc16 *vh = hdr_priv(hdr);
624 vh->crc16 = crc16(p, len);
627 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
629 struct vhdr_crc32 *vh = hdr_priv(hdr);
631 vh->crc32 = crc32(p, len);
634 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
636 struct vhdr_crc32 *vh = hdr_priv(hdr);
638 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
639 vh->crc32 = crc32c_intel(p, len);
641 vh->crc32 = crc32c(p, len);
644 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
646 struct vhdr_crc64 *vh = hdr_priv(hdr);
648 vh->crc64 = crc64(p, len);
651 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
653 struct vhdr_md5 *vh = hdr_priv(hdr);
654 struct md5_ctx md5_ctx = {
655 .hash = (uint32_t *) vh->md5_digest,
659 md5_update(&md5_ctx, p, len);
663 * fill body of io_u->buf with random data and add a header with the
664 * crc32 or md5 sum of that data.
666 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
668 struct verify_header *hdr;
669 void *p = io_u->buf, *data;
670 unsigned int hdr_inc, data_len, header_num = 0;
672 if (td->o.verify == VERIFY_NULL)
675 fill_pattern(td, p, io_u->buflen);
677 hdr_inc = io_u->buflen;
678 if (td->o.verify_interval)
679 hdr_inc = td->o.verify_interval;
681 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
684 hdr->fio_magic = FIO_HDR_MAGIC;
685 hdr->verify_type = td->o.verify;
687 data_len = hdr_inc - hdr_size(hdr);
689 data = p + hdr_size(hdr);
690 switch (td->o.verify) {
692 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
694 fill_md5(hdr, data, data_len);
697 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
699 fill_crc64(hdr, data, data_len);
702 case VERIFY_CRC32C_INTEL:
703 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
705 fill_crc32c(hdr, data, data_len);
708 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
710 fill_crc32(hdr, data, data_len);
713 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
715 fill_crc16(hdr, data, data_len);
718 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
720 fill_crc7(hdr, data, data_len);
723 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
725 fill_sha256(hdr, data, data_len);
728 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
730 fill_sha512(hdr, data, data_len);
733 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
735 fill_meta(hdr, td, io_u, header_num);
738 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
740 fill_sha1(hdr, data, data_len);
743 log_err("fio: bad verify type: %d\n", td->o.verify);
746 if (td->o.verify_offset)
747 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
752 int get_next_verify(struct thread_data *td, struct io_u *io_u)
754 struct io_piece *ipo = NULL;
757 * this io_u is from a requeue, we already filled the offsets
762 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
763 struct rb_node *n = rb_first(&td->io_hist_tree);
765 ipo = rb_entry(n, struct io_piece, rb_node);
766 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);
769 flist_del(&ipo->list);
773 io_u->offset = ipo->offset;
774 io_u->buflen = ipo->len;
775 io_u->file = ipo->file;
777 if (!fio_file_open(io_u->file)) {
778 int r = td_io_open_file(td, io_u->file);
781 dprint(FD_VERIFY, "failed file %s open\n",
782 io_u->file->file_name);
788 assert(fio_file_open(io_u->file));
789 io_u->ddir = DDIR_READ;
790 io_u->xfer_buf = io_u->buf;
791 io_u->xfer_buflen = io_u->buflen;
793 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
797 dprint(FD_VERIFY, "get_next_verify: empty\n");
801 static void *verify_async_thread(void *data)
803 struct thread_data *td = data;
807 if (td->o.verify_cpumask_set &&
808 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
809 log_err("fio: failed setting verify thread affinity\n");
817 if (td->verify_thread_exit)
820 pthread_mutex_lock(&td->io_u_lock);
822 while (flist_empty(&td->verify_list) &&
823 !td->verify_thread_exit) {
824 ret = pthread_cond_wait(&td->verify_cond,
827 pthread_mutex_unlock(&td->io_u_lock);
832 flist_splice_init(&td->verify_list, &list);
833 pthread_mutex_unlock(&td->io_u_lock);
835 if (flist_empty(&list))
838 while (!flist_empty(&list)) {
839 io_u = flist_entry(list.next, struct io_u, list);
840 flist_del_init(&io_u->list);
842 ret = verify_io_u(td, io_u);
846 if (td->o.continue_on_error &&
847 td_non_fatal_error(ret)) {
848 update_error_count(td, ret);
856 td_verror(td, ret, "async_verify");
861 pthread_mutex_lock(&td->io_u_lock);
862 td->nr_verify_threads--;
863 pthread_mutex_unlock(&td->io_u_lock);
865 pthread_cond_signal(&td->free_cond);
869 int verify_async_init(struct thread_data *td)
873 td->verify_thread_exit = 0;
875 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
876 for (i = 0; i < td->o.verify_async; i++) {
877 ret = pthread_create(&td->verify_threads[i], NULL,
878 verify_async_thread, td);
880 log_err("fio: async verify creation failed: %s\n",
884 ret = pthread_detach(td->verify_threads[i]);
886 log_err("fio: async verify thread detach failed: %s\n",
890 td->nr_verify_threads++;
893 if (i != td->o.verify_async) {
894 log_err("fio: only %d verify threads started, exiting\n", i);
895 td->verify_thread_exit = 1;
897 pthread_cond_broadcast(&td->verify_cond);
904 void verify_async_exit(struct thread_data *td)
906 td->verify_thread_exit = 1;
908 pthread_cond_broadcast(&td->verify_cond);
910 pthread_mutex_lock(&td->io_u_lock);
912 while (td->nr_verify_threads)
913 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
915 pthread_mutex_unlock(&td->io_u_lock);
916 free(td->verify_threads);
917 td->verify_threads = NULL;