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,
46 unsigned char header_num)
48 unsigned char *p = io_u->buf;
51 p += header_num * hdr->len + sizeof(*hdr);
52 c = crc7(p, hdr->len - sizeof(*hdr));
55 log_err("crc7: verify failed at %llu/%u\n",
56 io_u->offset + header_num * hdr->len,
58 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
65 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
66 unsigned int header_num)
68 unsigned char *p = io_u->buf;
71 p += header_num * hdr->len + sizeof(*hdr);
72 c = crc16(p, hdr->len - sizeof(*hdr));
74 if (c != hdr->crc16) {
75 log_err("crc16: verify failed at %llu/%u\n",
76 io_u->offset + header_num * hdr->len,
78 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
85 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
86 unsigned int header_num)
88 unsigned char *p = io_u->buf;
91 p += header_num * hdr->len + sizeof(*hdr);
92 c = crc64(p, hdr->len - sizeof(*hdr));
94 if (c != hdr->crc64) {
95 log_err("crc64: verify failed at %llu/%u\n",
96 io_u->offset + header_num * hdr->len,
98 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
105 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
106 unsigned int header_num)
108 unsigned char *p = io_u->buf;
111 p += header_num * hdr->len + sizeof(*hdr);
112 c = crc32(p, hdr->len - sizeof(*hdr));
114 if (c != hdr->crc32) {
115 log_err("crc32: verify failed at %llu/%u\n",
116 io_u->offset + header_num * hdr->len,
118 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
125 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
126 unsigned int header_num)
128 unsigned char *p = io_u->buf;
129 uint32_t hash[MD5_HASH_WORDS];
130 struct md5_ctx md5_ctx = {
134 p += header_num * hdr->len + sizeof(*hdr);
135 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
137 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
138 log_err("md5: verify failed at %llu/%u\n",
139 io_u->offset + header_num * hdr->len,
141 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
142 hexdump(md5_ctx.hash, sizeof(hash));
149 int verify_io_u(struct thread_data *td, struct io_u *io_u)
151 unsigned char *p = (unsigned char*) io_u->buf;
152 struct verify_header *hdr;
153 unsigned int hdr_inc, hdr_num = 0;
156 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
159 hdr_inc = io_u->buflen;
160 if (td->o.header_interval)
161 hdr_inc = td->o.header_interval;
163 for (; p < (unsigned char*) io_u->buf + io_u->buflen; p += hdr_inc) {
164 hdr = (struct verify_header*) p;
166 if (hdr->fio_magic != FIO_HDR_MAGIC) {
167 log_err("Bad verify header %x\n", hdr->fio_magic);
171 switch (hdr->verify_type) {
173 ret = verify_io_u_md5(hdr, io_u, hdr_num);
176 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
179 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
182 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
185 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
188 log_err("Bad verify type %u\n", hdr->verify_type);
197 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
199 hdr->crc7 = crc7(p, len);
202 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
204 hdr->crc16 = crc16(p, len);
207 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
209 hdr->crc32 = crc32(p, len);
212 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
214 hdr->crc64 = crc64(p, len);
217 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
219 struct md5_ctx md5_ctx = {
220 .hash = (uint32_t *) hdr->md5_digest,
223 md5_update(&md5_ctx, p, len);
227 * fill body of io_u->buf with random data and add a header with the
228 * crc32 or md5 sum of that data.
230 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
232 const unsigned int len = io_u->buflen - sizeof(struct verify_header);
233 struct verify_header *hdr;
234 unsigned char *p = io_u->buf, *data;
235 unsigned int data_len;
237 if (td->o.verify == VERIFY_NULL)
240 fill_random_bytes(td, p, len);
242 for (;p < (unsigned char*) io_u->buf + io_u->buflen; p += hdr->len) {
243 hdr = (struct verify_header*) p;
245 hdr->fio_magic = FIO_HDR_MAGIC;
246 hdr->verify_type = td->o.verify;
247 hdr->len = io_u->buflen;
248 if (td->o.header_interval)
249 hdr->len = td->o.header_interval;
251 data = p + sizeof(*hdr);
252 data_len = hdr->len - sizeof(*hdr);
253 switch (td->o.verify) {
255 fill_md5(hdr, data, data_len);
258 fill_crc64(hdr, data, data_len);
261 fill_crc32(hdr, data, data_len);
264 fill_crc16(hdr, data, data_len);
267 fill_crc7(hdr, data, data_len);
270 log_err("fio: bad verify type: %d\n", td->o.verify);
276 int get_next_verify(struct thread_data *td, struct io_u *io_u)
278 struct io_piece *ipo = NULL;
281 * this io_u is from a requeue, we already filled the offsets
286 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
287 struct rb_node *n = rb_first(&td->io_hist_tree);
289 ipo = rb_entry(n, struct io_piece, rb_node);
290 rb_erase(n, &td->io_hist_tree);
291 } else if (!list_empty(&td->io_hist_list)) {
292 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
293 list_del(&ipo->list);
297 io_u->offset = ipo->offset;
298 io_u->buflen = ipo->len;
299 io_u->file = ipo->file;
301 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
302 int r = td_io_open_file(td, io_u->file);
309 assert(io_u->file->flags & FIO_FILE_OPEN);
310 io_u->ddir = DDIR_READ;
311 io_u->xfer_buf = io_u->buf;
312 io_u->xfer_buflen = io_u->buflen;