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 %x, got %x\n", hdr->crc16, c);
79 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u)
81 unsigned char *p = io_u->buf + sizeof(*hdr);
84 c = crc64(p, hdr->len - sizeof(*hdr));
86 if (c != hdr->crc64) {
87 log_err("crc64: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
88 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
95 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
97 unsigned char *p = io_u->buf;
101 c = crc32(p, hdr->len - sizeof(*hdr));
103 if (c != hdr->crc32) {
104 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
105 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
112 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
114 unsigned char *p = io_u->buf + sizeof(*hdr);
115 uint32_t hash[MD5_HASH_WORDS];
116 struct md5_ctx md5_ctx = {
120 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
122 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
123 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
124 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
125 hexdump(md5_ctx.hash, sizeof(hash));
132 int verify_io_u(struct thread_data *td, struct io_u *io_u)
134 struct verify_header *hdr = (struct verify_header *) io_u->buf;
137 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
140 if (hdr->fio_magic != FIO_HDR_MAGIC) {
141 log_err("Bad verify header %x\n", hdr->fio_magic);
145 switch (hdr->verify_type) {
147 ret = verify_io_u_md5(hdr, io_u);
150 ret = verify_io_u_crc64(hdr, io_u);
153 ret = verify_io_u_crc32(hdr, io_u);
156 ret = verify_io_u_crc16(hdr, io_u);
159 ret = verify_io_u_crc7(hdr, io_u);
162 log_err("Bad verify type %u\n", hdr->verify_type);
172 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
174 hdr->crc7 = crc7(p, len);
177 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
179 hdr->crc16 = crc16(p, len);
182 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
184 hdr->crc32 = crc32(p, len);
187 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
189 hdr->crc64 = crc64(p, len);
192 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
194 struct md5_ctx md5_ctx = {
195 .hash = (uint32_t *) hdr->md5_digest,
198 md5_update(&md5_ctx, p, len);
202 * fill body of io_u->buf with random data and add a header with the
203 * crc32 or md5 sum of that data.
205 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
207 const unsigned int len = io_u->buflen - sizeof(struct verify_header);
208 struct verify_header *hdr;
211 if (td->o.verify == VERIFY_NULL)
214 hdr = (struct verify_header *) io_u->buf;
215 hdr->fio_magic = FIO_HDR_MAGIC;
216 hdr->len = io_u->buflen;
217 hdr->verify_type = td->o.verify;
219 p = io_u->buf + sizeof(*hdr);
220 fill_random_bytes(td, p, len);
222 switch (td->o.verify) {
224 fill_md5(hdr, p, len);
227 fill_crc64(hdr, p, len);
230 fill_crc32(hdr, p, len);
233 fill_crc16(hdr, p, len);
236 fill_crc7(hdr, p, len);
239 log_err("fio: bad verify type: %d\n", td->o.verify);
244 int get_next_verify(struct thread_data *td, struct io_u *io_u)
246 struct io_piece *ipo = NULL;
249 * this io_u is from a requeue, we already filled the offsets
254 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
255 struct rb_node *n = rb_first(&td->io_hist_tree);
257 ipo = rb_entry(n, struct io_piece, rb_node);
258 rb_erase(n, &td->io_hist_tree);
259 } else if (!list_empty(&td->io_hist_list)) {
260 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
261 list_del(&ipo->list);
265 io_u->offset = ipo->offset;
266 io_u->buflen = ipo->len;
267 io_u->file = ipo->file;
269 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
270 int r = td_io_open_file(td, io_u->file);
277 assert(io_u->file->flags & FIO_FILE_OPEN);
278 io_u->ddir = DDIR_READ;
279 io_u->xfer_buf = io_u->buf;
280 io_u->xfer_buflen = io_u->buflen;