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_long(&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_crc7(struct verify_header *hdr, struct io_u *io_u)
47 unsigned char *p = io_u->buf;
51 c = crc7(p, hdr->len - sizeof(*hdr));
54 log_err("crc7: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
55 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
62 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u)
64 unsigned char *p = io_u->buf;
68 c = crc16(p, hdr->len - sizeof(*hdr));
70 if (c != hdr->crc16) {
71 log_err("crc16: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
72 log_err("crc16: wanted %lx, got %x\n", hdr->crc32, c);
79 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
81 unsigned char *p = io_u->buf;
85 c = crc32(p, hdr->len - sizeof(*hdr));
87 if (c != hdr->crc32) {
88 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
89 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
96 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
98 unsigned char *p = io_u->buf;
99 struct md5_ctx md5_ctx;
101 memset(&md5_ctx, 0, sizeof(md5_ctx));
103 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
105 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
106 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
107 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
108 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
115 int verify_io_u(struct thread_data *td, struct io_u *io_u)
117 struct verify_header *hdr = (struct verify_header *) io_u->buf;
120 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
123 if (hdr->fio_magic != FIO_HDR_MAGIC) {
124 log_err("Bad verify header %x\n", hdr->fio_magic);
128 if (hdr->verify_type == VERIFY_MD5)
129 ret = verify_io_u_md5(hdr, io_u);
130 else if (hdr->verify_type == VERIFY_CRC32)
131 ret = verify_io_u_crc32(hdr, io_u);
132 else if (hdr->verify_type == VERIFY_CRC16)
133 ret = verify_io_u_crc16(hdr, io_u);
134 else if (hdr->verify_type == VERIFY_CRC7)
135 ret = verify_io_u_crc7(hdr, io_u);
137 log_err("Bad verify type %u\n", hdr->verify_type);
147 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
149 hdr->crc7 = crc7(p, len);
152 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
154 hdr->crc16 = crc16(p, len);
157 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
159 hdr->crc32 = crc32(p, len);
162 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
164 struct md5_ctx md5_ctx;
166 memset(&md5_ctx, 0, sizeof(md5_ctx));
167 md5_update(&md5_ctx, p, len);
168 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
172 * fill body of io_u->buf with random data and add a header with the
173 * crc32 or md5 sum of that data.
175 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
177 unsigned char *p = (unsigned char *) io_u->buf;
178 struct verify_header hdr;
180 if (td->o.verify == VERIFY_NULL)
183 hdr.fio_magic = FIO_HDR_MAGIC;
184 hdr.len = io_u->buflen;
186 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
188 if (td->o.verify == VERIFY_MD5) {
189 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
190 hdr.verify_type = VERIFY_MD5;
191 } else if (td->o.verify == VERIFY_CRC32) {
192 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
193 hdr.verify_type = VERIFY_CRC32;
194 } else if (td->o.verify == VERIFY_CRC16) {
195 fill_crc16(&hdr, p, io_u->buflen - sizeof(hdr));
196 hdr.verify_type = VERIFY_CRC16;
197 } else if (td->o.verify == VERIFY_CRC7) {
198 fill_crc7(&hdr, p, io_u->buflen - sizeof(hdr));
199 hdr.verify_type = VERIFY_CRC7;
202 memcpy(io_u->buf, &hdr, sizeof(hdr));
205 int get_next_verify(struct thread_data *td, struct io_u *io_u)
207 struct io_piece *ipo = NULL;
210 * this io_u is from a requeue, we already filled the offsets
215 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
216 struct rb_node *n = rb_first(&td->io_hist_tree);
218 ipo = rb_entry(n, struct io_piece, rb_node);
219 rb_erase(n, &td->io_hist_tree);
220 } else if (!list_empty(&td->io_hist_list)) {
221 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
222 list_del(&ipo->list);
226 io_u->offset = ipo->offset;
227 io_u->buflen = ipo->len;
228 io_u->file = ipo->file;
230 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
231 int r = td_io_open_file(td, io_u->file);
238 assert(io_u->file->flags & FIO_FILE_OPEN);
239 io_u->ddir = DDIR_READ;
240 io_u->xfer_buf = io_u->buf;
241 io_u->xfer_buflen = io_u->buflen;