2 * IO verification helpers
11 static void fill_random_bytes(struct thread_data *td,
12 unsigned char *p, unsigned int len)
18 r = os_random_double(&td->verify_state);
21 * lrand48_r seems to be broken and only fill the bottom
22 * 32-bits, even on 64-bit archs with 64-bit longs
35 static void hexdump(void *buffer, int len)
37 unsigned char *p = buffer;
40 for (i = 0; i < len; i++)
41 log_info("%02x", p[i]);
45 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
47 unsigned char *p = (unsigned char *) io_u->buf;
51 c = crc32(p, hdr->len - sizeof(*hdr));
53 if (c != hdr->crc32) {
54 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
55 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
62 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
64 unsigned char *p = (unsigned char *) io_u->buf;
65 struct md5_ctx md5_ctx;
67 memset(&md5_ctx, 0, sizeof(md5_ctx));
69 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
71 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
72 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
73 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
74 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
81 int verify_io_u(struct io_u *io_u)
83 struct verify_header *hdr = (struct verify_header *) io_u->buf;
86 if (hdr->fio_magic != FIO_HDR_MAGIC) {
87 log_err("Bad verify header %x\n", hdr->fio_magic);
91 if (hdr->verify_type == VERIFY_MD5)
92 ret = verify_io_u_md5(hdr, io_u);
93 else if (hdr->verify_type == VERIFY_CRC32)
94 ret = verify_io_u_crc32(hdr, io_u);
96 log_err("Bad verify type %u\n", hdr->verify_type);
106 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
108 hdr->crc32 = crc32(p, len);
111 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
113 struct md5_ctx md5_ctx;
115 memset(&md5_ctx, 0, sizeof(md5_ctx));
116 md5_update(&md5_ctx, p, len);
117 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
121 * fill body of io_u->buf with random data and add a header with the
122 * crc32 or md5 sum of that data.
124 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
126 unsigned char *p = (unsigned char *) io_u->buf;
127 struct verify_header hdr;
129 hdr.fio_magic = FIO_HDR_MAGIC;
130 hdr.len = io_u->buflen;
132 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
134 if (td->o.verify == VERIFY_MD5) {
135 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
136 hdr.verify_type = VERIFY_MD5;
138 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
139 hdr.verify_type = VERIFY_CRC32;
142 memcpy(io_u->buf, &hdr, sizeof(hdr));
145 int get_next_verify(struct thread_data *td, struct io_u *io_u)
147 struct io_piece *ipo;
150 * this io_u is from a requeue, we already filled the offsets
155 if (!list_empty(&td->io_hist_list)) {
156 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
158 list_del(&ipo->list);
160 io_u->offset = ipo->offset;
161 io_u->buflen = ipo->len;
162 io_u->file = ipo->file;
163 io_u->ddir = DDIR_READ;
164 io_u->xfer_buf = io_u->buf;
165 io_u->xfer_buflen = io_u->buflen;