2 * IO verification helpers
10 static void fill_random_bytes(struct thread_data *td,
11 unsigned char *p, unsigned int len)
17 r = os_random_double(&td->verify_state);
20 * lrand48_r seems to be broken and only fill the bottom
21 * 32-bits, even on 64-bit archs with 64-bit longs
34 static void hexdump(void *buffer, int len)
36 unsigned char *p = buffer;
39 for (i = 0; i < len; i++)
40 log_info("%02x", p[i]);
44 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
46 unsigned char *p = (unsigned char *) io_u->buf;
50 c = crc32(p, hdr->len - sizeof(*hdr));
52 if (c != hdr->crc32) {
53 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
54 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
61 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
63 unsigned char *p = (unsigned char *) io_u->buf;
64 struct md5_ctx md5_ctx;
66 memset(&md5_ctx, 0, sizeof(md5_ctx));
68 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
70 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
71 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
72 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
73 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
80 int verify_io_u(struct thread_data *td, struct io_u *io_u)
82 struct verify_header *hdr = (struct verify_header *) io_u->buf;
85 if (td->o.verify == VERIFY_NULL)
88 if (hdr->fio_magic != FIO_HDR_MAGIC) {
89 log_err("Bad verify header %x\n", hdr->fio_magic);
93 if (hdr->verify_type == VERIFY_MD5)
94 ret = verify_io_u_md5(hdr, io_u);
95 else if (hdr->verify_type == VERIFY_CRC32)
96 ret = verify_io_u_crc32(hdr, io_u);
98 log_err("Bad verify type %u\n", hdr->verify_type);
108 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
110 hdr->crc32 = crc32(p, len);
113 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
115 struct md5_ctx md5_ctx;
117 memset(&md5_ctx, 0, sizeof(md5_ctx));
118 md5_update(&md5_ctx, p, len);
119 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
123 * fill body of io_u->buf with random data and add a header with the
124 * crc32 or md5 sum of that data.
126 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
128 unsigned char *p = (unsigned char *) io_u->buf;
129 struct verify_header hdr;
131 if (td->o.verify == VERIFY_NULL)
134 hdr.fio_magic = FIO_HDR_MAGIC;
135 hdr.len = io_u->buflen;
137 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
139 if (td->o.verify == VERIFY_MD5) {
140 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
141 hdr.verify_type = VERIFY_MD5;
142 } else if (td->o.verify == VERIFY_CRC32) {
143 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
144 hdr.verify_type = VERIFY_CRC32;
147 memcpy(io_u->buf, &hdr, sizeof(hdr));
150 int get_next_verify(struct thread_data *td, struct io_u *io_u)
152 struct io_piece *ipo = NULL;
155 * this io_u is from a requeue, we already filled the offsets
160 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
161 struct rb_node *n = rb_first(&td->io_hist_tree);
163 ipo = rb_entry(n, struct io_piece, rb_node);
164 rb_erase(n, &td->io_hist_tree);
165 } else if (!list_empty(&td->io_hist_list)) {
166 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
167 list_del(&ipo->list);
171 io_u->offset = ipo->offset;
172 io_u->buflen = ipo->len;
173 io_u->file = ipo->file;
174 io_u->ddir = DDIR_READ;
175 io_u->xfer_buf = io_u->buf;
176 io_u->xfer_buflen = io_u->buflen;