+ return 0;
+}
+
+static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
+ unsigned int header_num)
+{
+ void *p = io_u_verify_off(hdr, io_u, header_num);
+ struct vhdr_crc16 *vh = hdr_priv(hdr);
+ unsigned short c;
+
+ dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
+
+ c = crc16(p, hdr->len - hdr_size(hdr));
+
+ if (c != vh->crc16) {
+ log_err("crc16: verify failed at %llu/%u\n",
+ io_u->offset + header_num * hdr->len, hdr->len);
+ log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
+ return EILSEQ;
+ }
+
+ return 0;
+}
+
+static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
+ unsigned int header_num)
+{
+ void *p = io_u_verify_off(hdr, io_u, header_num);
+ struct vhdr_crc64 *vh = hdr_priv(hdr);
+ unsigned long long c;
+
+ dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
+
+ c = crc64(p, hdr->len - hdr_size(hdr));
+
+ if (c != vh->crc64) {
+ log_err("crc64: verify failed at %llu/%u\n",
+ io_u->offset + header_num * hdr->len,
+ hdr->len);
+ log_err("crc64: wanted %llx, got %llx\n",
+ (unsigned long long) vh->crc64, c);
+ return EILSEQ;
+ }
+
+ return 0;
+}
+
+static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
+ unsigned int header_num)
+{
+ void *p = io_u_verify_off(hdr, io_u, header_num);
+ struct vhdr_crc32 *vh = hdr_priv(hdr);
+ uint32_t c;
+
+ dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
+
+ c = crc32(p, hdr->len - hdr_size(hdr));
+
+ if (c != vh->crc32) {
+ log_err("crc32: verify failed at %llu/%u\n",
+ io_u->offset + header_num * hdr->len, hdr->len);
+ log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
+ return EILSEQ;
+ }
+
+ return 0;
+}
+
+static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
+ unsigned int header_num)
+{
+ void *p = io_u_verify_off(hdr, io_u, header_num);
+ struct vhdr_crc32 *vh = hdr_priv(hdr);
+ uint32_t c;
+
+ dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
+
+ if (hdr->verify_type == VERIFY_CRC32C_INTEL)
+ c = crc32c_intel(p, hdr->len - hdr_size(hdr));
+ else
+ c = crc32c(p, hdr->len - hdr_size(hdr));
+
+ if (c != vh->crc32) {
+ log_err("crc32c: verify failed at %llu/%u\n",
+ io_u->offset + header_num * hdr->len, hdr->len);
+ log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
+ return EILSEQ;
+ }
+
+ return 0;
+}
+
+static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
+ unsigned int header_num)
+{
+ void *p = io_u_verify_off(hdr, io_u, header_num);
+ struct vhdr_md5 *vh = hdr_priv(hdr);
+ uint32_t hash[MD5_HASH_WORDS];
+ struct md5_ctx md5_ctx = {
+ .hash = hash,
+ };
+
+ dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
+
+ md5_init(&md5_ctx);
+ md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
+
+ if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
+ log_err("md5: verify failed at %llu/%u\n",
+ io_u->offset + header_num * hdr->len, hdr->len);
+ hexdump(vh->md5_digest, sizeof(vh->md5_digest));
+ hexdump(md5_ctx.hash, sizeof(hash));
+ return EILSEQ;
+ }
+
+ return 0;
+}
+
+static unsigned int hweight8(unsigned int w)
+{
+ unsigned int res = w - ((w >> 1) & 0x55);
+
+ res = (res & 0x33) + ((res >> 2) & 0x33);
+ return (res + (res >> 4)) & 0x0F;
+}
+
+int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
+ char *buf, unsigned int len, unsigned int mod)
+{
+ unsigned int i;
+
+ for (i = 0; i < len; i++) {
+ if (buf[i] != pattern[mod]) {
+ unsigned int bits;
+
+ bits = hweight8(buf[i] ^ pattern[mod]);
+ log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
+ buf[i], pattern[mod], bits);
+ log_err("fio: bad pattern block offset %u\n", i);
+ return EILSEQ;
+ }
+ mod++;
+ if (mod == pattern_size)
+ mod = 0;
+ }
+
+ return 0;
+}
+
+/*
+ * Push IO verification to a separate thread
+ */
+int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
+{
+ if (io_u->file)
+ put_file_log(td, io_u->file);
+
+ io_u->file = NULL;
+
+ pthread_mutex_lock(&td->io_u_lock);
+
+ if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
+ td->cur_depth--;
+ io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
+ }
+ flist_del(&io_u->list);
+ flist_add_tail(&io_u->list, &td->verify_list);
+ io_u->flags |= IO_U_F_FREE_DEF;
+ pthread_mutex_unlock(&td->io_u_lock);
+
+ pthread_cond_signal(&td->verify_cond);
+ return 0;
+}
+
+int verify_io_u(struct thread_data *td, struct io_u *io_u)
+{
+ struct verify_header *hdr;
+ unsigned int hdr_size, hdr_inc, hdr_num = 0;
+ void *p;
+ int ret;
+
+ if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
+ return 0;
+
+ hdr_inc = io_u->buflen;
+ if (td->o.verify_interval)
+ hdr_inc = td->o.verify_interval;
+
+ ret = 0;
+ for (p = io_u->buf; p < io_u->buf + io_u->buflen;
+ p += hdr_inc, hdr_num++) {
+ if (ret && td->o.verify_fatal) {
+ td->terminate = 1;
+ break;
+ }
+ hdr_size = __hdr_size(td->o.verify);
+ if (td->o.verify_offset)
+ memswp(p, p + td->o.verify_offset, hdr_size);
+ hdr = p;
+
+ if (hdr->fio_magic != FIO_HDR_MAGIC) {
+ log_err("Bad verify header %x at %llu\n",
+ hdr->fio_magic,
+ io_u->offset + hdr_num * hdr->len);
+ return EILSEQ;
+ }
+
+ if (td->o.verify_pattern_bytes) {
+ dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
+ io_u, hdr->len);
+ ret = verify_io_u_pattern(td->o.verify_pattern,
+ td->o.verify_pattern_bytes,
+ p + hdr_size,
+ hdr_inc - hdr_size,
+ hdr_size % td->o.verify_pattern_bytes);
+ /*
+ * Also verify the meta data, if applicable
+ */
+ if (hdr->verify_type == VERIFY_META)
+ ret |= verify_io_u_meta(hdr, td, io_u, hdr_num);
+
+ if (ret)
+ log_err("fio: verify failed at %llu/%u\n",
+ io_u->offset + hdr_num * hdr->len,
+ hdr->len);
+ continue;
+ }
+
+ switch (hdr->verify_type) {
+ case VERIFY_MD5:
+ ret = verify_io_u_md5(hdr, io_u, hdr_num);
+ break;
+ case VERIFY_CRC64:
+ ret = verify_io_u_crc64(hdr, io_u, hdr_num);
+ break;
+ case VERIFY_CRC32C:
+ case VERIFY_CRC32C_INTEL:
+ ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
+ break;
+ case VERIFY_CRC32:
+ ret = verify_io_u_crc32(hdr, io_u, hdr_num);
+ break;
+ case VERIFY_CRC16:
+ ret = verify_io_u_crc16(hdr, io_u, hdr_num);
+ break;
+ case VERIFY_CRC7:
+ ret = verify_io_u_crc7(hdr, io_u, hdr_num);
+ break;
+ case VERIFY_SHA256:
+ ret = verify_io_u_sha256(hdr, io_u, hdr_num);
+ break;
+ case VERIFY_SHA512:
+ ret = verify_io_u_sha512(hdr, io_u, hdr_num);
+ break;
+ case VERIFY_META:
+ ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
+ break;
+ case VERIFY_SHA1:
+ ret = verify_io_u_sha1(hdr, io_u, hdr_num);
+ break;
+ default:
+ log_err("Bad verify type %u\n", hdr->verify_type);
+ ret = EINVAL;
+ }