+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(unsigned long pattern, unsigned long pattern_size,
+ char *buf, unsigned int len, unsigned int mod)
+{
+ unsigned int i;
+ char split_pattern[4];
+
+ for (i = 0; i < 4; i++) {
+ split_pattern[i] = pattern & 0xff;
+ pattern >>= 8;
+ }
+
+ for (i = 0; i < len; i++) {
+ if (buf[i] != split_pattern[mod]) {
+ unsigned int bits;
+
+ bits = hweight8(buf[i] ^ split_pattern[mod]);
+ log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
+ buf[i], split_pattern[mod], bits);
+ log_err("fio: bad pattern block offset %u\n", i);
+ return EILSEQ;
+ }
+ mod++;
+ if (mod == pattern_size)
+ mod = 0;
+ }
+
+ 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\n", hdr->fio_magic);
+ 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 % 4);
+ 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;
+ default:
+ log_err("Bad verify type %u\n", hdr->verify_type);
+ ret = EINVAL;
+ }
+ }
+
+ return ret;
+}
+
+static void fill_meta(struct verify_header *hdr, struct thread_data *td,
+ struct io_u *io_u, unsigned int header_num)
+{
+ struct vhdr_meta *vh = hdr_priv(hdr);
+
+ vh->thread = td->thread_number;
+
+ vh->time_sec = io_u->start_time.tv_sec;
+ vh->time_usec = io_u->start_time.tv_usec;
+
+ vh->numberio = td->io_issues[DDIR_WRITE];
+
+ vh->offset = io_u->offset + header_num * td->o.verify_interval;
+}
+
+static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
+{
+ struct vhdr_sha512 *vh = hdr_priv(hdr);
+ struct sha512_ctx sha512_ctx = {
+ .buf = vh->sha512,
+ };
+
+ sha512_init(&sha512_ctx);
+ sha512_update(&sha512_ctx, p, len);
+}
+
+static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
+{
+ struct vhdr_sha256 *vh = hdr_priv(hdr);
+ struct sha256_ctx sha256_ctx = {
+ .buf = vh->sha256,
+ };
+
+ sha256_init(&sha256_ctx);
+ sha256_update(&sha256_ctx, p, len);
+}
+
+static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
+{
+ struct vhdr_crc7 *vh = hdr_priv(hdr);
+
+ vh->crc7 = crc7(p, len);
+}
+
+static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
+{
+ struct vhdr_crc16 *vh = hdr_priv(hdr);
+
+ vh->crc16 = crc16(p, len);
+}
+