blob: 46e610d713fd221d0e10b7cc74579f5ffb54fc10 [file] [log] [blame]
Jens Axboee29d1b72006-10-18 15:43:15 +02001/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
7
8#include "fio.h"
Jens Axboee29d1b72006-10-18 15:43:15 +02009
10static void fill_random_bytes(struct thread_data *td,
11 unsigned char *p, unsigned int len)
12{
13 unsigned int todo;
14 double r;
15
16 while (len) {
17 r = os_random_double(&td->verify_state);
18
19 /*
20 * lrand48_r seems to be broken and only fill the bottom
21 * 32-bits, even on 64-bit archs with 64-bit longs
22 */
23 todo = sizeof(r);
24 if (todo > len)
25 todo = len;
26
27 memcpy(p, &r, todo);
28
29 len -= todo;
30 p += todo;
31 }
32}
33
34static void hexdump(void *buffer, int len)
35{
36 unsigned char *p = buffer;
37 int i;
38
39 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +010040 log_info("%02x", p[i]);
41 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020042}
43
44static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
45{
46 unsigned char *p = (unsigned char *) io_u->buf;
47 unsigned long c;
48
49 p += sizeof(*hdr);
50 c = crc32(p, hdr->len - sizeof(*hdr));
51
52 if (c != hdr->crc32) {
Jens Axboea4f4fdd2007-02-14 01:16:39 +010053 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
Jens Axboee29d1b72006-10-18 15:43:15 +020054 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
55 return 1;
56 }
57
58 return 0;
59}
60
61static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
62{
63 unsigned char *p = (unsigned char *) io_u->buf;
64 struct md5_ctx md5_ctx;
65
66 memset(&md5_ctx, 0, sizeof(md5_ctx));
67 p += sizeof(*hdr);
68 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
69
70 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
Jens Axboea4f4fdd2007-02-14 01:16:39 +010071 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
Jens Axboee29d1b72006-10-18 15:43:15 +020072 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
73 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
74 return 1;
75 }
76
77 return 0;
78}
79
Jens Axboe36690c92007-03-26 10:23:34 +020080int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +020081{
82 struct verify_header *hdr = (struct verify_header *) io_u->buf;
83 int ret;
84
Jens Axboe36690c92007-03-26 10:23:34 +020085 if (td->o.verify == VERIFY_NULL)
86 return 0;
87
Jens Axboe36167d82007-02-18 05:41:31 +010088 if (hdr->fio_magic != FIO_HDR_MAGIC) {
89 log_err("Bad verify header %x\n", hdr->fio_magic);
Jens Axboea7dfe862007-03-12 10:05:08 +010090 return EIO;
Jens Axboe36167d82007-02-18 05:41:31 +010091 }
Jens Axboee29d1b72006-10-18 15:43:15 +020092
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);
97 else {
Jens Axboe1e97cce2006-12-05 11:44:16 +010098 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboee29d1b72006-10-18 15:43:15 +020099 ret = 1;
100 }
101
Jens Axboea7dfe862007-03-12 10:05:08 +0100102 if (ret)
103 return EIO;
104
105 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200106}
107
108static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
109{
110 hdr->crc32 = crc32(p, len);
111}
112
113static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
114{
115 struct md5_ctx md5_ctx;
116
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));
120}
121
122/*
123 * fill body of io_u->buf with random data and add a header with the
124 * crc32 or md5 sum of that data.
125 */
126void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
127{
128 unsigned char *p = (unsigned char *) io_u->buf;
129 struct verify_header hdr;
130
Jens Axboe9cc3d152007-03-26 10:32:30 +0200131 if (td->o.verify == VERIFY_NULL)
132 return;
133
Jens Axboee29d1b72006-10-18 15:43:15 +0200134 hdr.fio_magic = FIO_HDR_MAGIC;
135 hdr.len = io_u->buflen;
136 p += sizeof(hdr);
137 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
138
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100139 if (td->o.verify == VERIFY_MD5) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200140 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
141 hdr.verify_type = VERIFY_MD5;
Jens Axboe36690c92007-03-26 10:23:34 +0200142 } else if (td->o.verify == VERIFY_CRC32) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200143 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
144 hdr.verify_type = VERIFY_CRC32;
145 }
146
147 memcpy(io_u->buf, &hdr, sizeof(hdr));
148}
149
150int get_next_verify(struct thread_data *td, struct io_u *io_u)
151{
Jens Axboe8de8f042007-03-27 10:36:12 +0200152 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200153
Jens Axboed2d7fa52007-02-19 13:21:35 +0100154 /*
155 * this io_u is from a requeue, we already filled the offsets
156 */
157 if (io_u->file)
158 return 0;
159
Jens Axboe8de8f042007-03-27 10:36:12 +0200160 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
161 struct rb_node *n = rb_first(&td->io_hist_tree);
162
Jens Axboe4b878982007-03-26 09:32:22 +0200163 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200164 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200165 } 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);
168 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200169
Jens Axboe8de8f042007-03-27 10:36:12 +0200170 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200171 io_u->offset = ipo->offset;
172 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100173 io_u->file = ipo->file;
Jens Axboee29d1b72006-10-18 15:43:15 +0200174 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100175 io_u->xfer_buf = io_u->buf;
176 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200177 free(ipo);
178 return 0;
179 }
180
181 return 1;
182}