2 * IO verification helpers
11 static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
17 r = os_random_long(&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 fill_pattern(struct thread_data *td, void *p, unsigned int len)
36 switch (td->o.verify_pattern_bytes) {
38 fill_random_bytes(td, p, len);
41 memset(p, td->o.verify_pattern, len);
46 unsigned int pattern = td->o.verify_pattern;
48 unsigned char c1, c2, c3, c4;
64 if (td->o.verify_pattern_bytes == 2 || i == len)
67 if (td->o.verify_pattern_bytes == 3 || i == len)
76 static void memswp(void* buf1, void* buf2, unsigned int len)
78 struct verify_header swap;
80 memcpy(&swap, buf1, len);
81 memcpy(buf1, buf2, len);
82 memcpy(buf2, &swap, len);
85 static void hexdump(void *buffer, int len)
87 unsigned char *p = buffer;
90 for (i = 0; i < len; i++)
91 log_info("%02x", p[i]);
96 * Return data area 'header_num'
98 static inline void *io_u_verify_off(struct verify_header *hdr,
100 unsigned char header_num)
102 return io_u->buf + sizeof(*hdr) + header_num * hdr->len;
105 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
106 unsigned char header_num)
108 void *p = io_u_verify_off(hdr, io_u, header_num);
111 c = crc7(p, hdr->len - sizeof(*hdr));
113 if (c != hdr->crc7) {
114 log_err("crc7: verify failed at %llu/%u\n",
115 io_u->offset + header_num * hdr->len,
117 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
124 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
125 unsigned int header_num)
127 void *p = io_u_verify_off(hdr, io_u, header_num);
130 c = crc16(p, hdr->len - sizeof(*hdr));
132 if (c != hdr->crc16) {
133 log_err("crc16: verify failed at %llu/%u\n",
134 io_u->offset + header_num * hdr->len,
136 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
143 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
144 unsigned int header_num)
146 void *p = io_u_verify_off(hdr, io_u, header_num);
147 unsigned long long c;
149 c = crc64(p, hdr->len - sizeof(*hdr));
151 if (c != hdr->crc64) {
152 log_err("crc64: verify failed at %llu/%u\n",
153 io_u->offset + header_num * hdr->len,
155 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
162 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
163 unsigned int header_num)
165 void *p = io_u_verify_off(hdr, io_u, header_num);
168 c = crc32(p, hdr->len - sizeof(*hdr));
170 if (c != hdr->crc32) {
171 log_err("crc32: verify failed at %llu/%u\n",
172 io_u->offset + header_num * hdr->len,
174 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
181 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
182 unsigned int header_num)
184 void *p = io_u_verify_off(hdr, io_u, header_num);
185 uint32_t hash[MD5_HASH_WORDS];
186 struct md5_ctx md5_ctx = {
191 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
193 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
194 log_err("md5: verify failed at %llu/%u\n",
195 io_u->offset + header_num * hdr->len,
197 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
198 hexdump(md5_ctx.hash, sizeof(hash));
205 int verify_io_u(struct thread_data *td, struct io_u *io_u)
207 struct verify_header *hdr;
208 unsigned int hdr_inc, hdr_num = 0;
212 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
215 hdr_inc = io_u->buflen;
216 if (td->o.verify_interval)
217 hdr_inc = td->o.verify_interval;
219 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
220 if (td->o.verify_offset)
221 memswp(p, p + td->o.verify_offset, sizeof(*hdr));
225 if (hdr->fio_magic != FIO_HDR_MAGIC) {
226 log_err("Bad verify header %x\n", hdr->fio_magic);
230 switch (hdr->verify_type) {
232 ret = verify_io_u_md5(hdr, io_u, hdr_num);
235 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
238 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
241 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
244 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
247 log_err("Bad verify type %u\n", hdr->verify_type);
256 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
258 hdr->crc7 = crc7(p, len);
261 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
263 hdr->crc16 = crc16(p, len);
266 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
268 hdr->crc32 = crc32(p, len);
271 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
273 hdr->crc64 = crc64(p, len);
276 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
278 struct md5_ctx md5_ctx = {
279 .hash = (uint32_t *) hdr->md5_digest,
283 md5_update(&md5_ctx, p, len);
287 * fill body of io_u->buf with random data and add a header with the
288 * crc32 or md5 sum of that data.
290 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
292 struct verify_header *hdr;
293 void *p = io_u->buf, *data;
294 unsigned int hdr_inc, data_len;
296 if (td->o.verify == VERIFY_NULL)
299 fill_pattern(td, p, io_u->buflen);
301 hdr_inc = io_u->buflen;
302 if (td->o.verify_interval)
303 hdr_inc = td->o.verify_interval;
304 data_len = hdr_inc - sizeof(*hdr);
306 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
309 hdr->fio_magic = FIO_HDR_MAGIC;
310 hdr->verify_type = td->o.verify;
313 data = p + sizeof(*hdr);
314 switch (td->o.verify) {
316 fill_md5(hdr, data, data_len);
319 fill_crc64(hdr, data, data_len);
322 fill_crc32(hdr, data, data_len);
325 fill_crc16(hdr, data, data_len);
328 fill_crc7(hdr, data, data_len);
331 log_err("fio: bad verify type: %d\n", td->o.verify);
334 if (td->o.verify_offset)
335 memswp(p, p + td->o.verify_offset, sizeof(*hdr));
339 int get_next_verify(struct thread_data *td, struct io_u *io_u)
341 struct io_piece *ipo = NULL;
344 * this io_u is from a requeue, we already filled the offsets
349 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
350 struct rb_node *n = rb_first(&td->io_hist_tree);
352 ipo = rb_entry(n, struct io_piece, rb_node);
353 rb_erase(n, &td->io_hist_tree);
354 } else if (!list_empty(&td->io_hist_list)) {
355 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
356 list_del(&ipo->list);
360 io_u->offset = ipo->offset;
361 io_u->buflen = ipo->len;
362 io_u->file = ipo->file;
364 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
365 int r = td_io_open_file(td, io_u->file);
372 assert(io_u->file->flags & FIO_FILE_OPEN);
373 io_u->ddir = DDIR_READ;
374 io_u->xfer_buf = io_u->buf;
375 io_u->xfer_buflen = io_u->buflen;