[PATCH] Allow verification to be done at finer granularity
[fio.git] / verify.c
CommitLineData
e29d1b70
JA
1/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
97af62ce 7#include <assert.h>
e29d1b70
JA
8
9#include "fio.h"
e29d1b70
JA
10
11static void fill_random_bytes(struct thread_data *td,
12 unsigned char *p, unsigned int len)
13{
14 unsigned int todo;
4c5946c6 15 int r;
e29d1b70
JA
16
17 while (len) {
4c5946c6 18 r = os_random_long(&td->verify_state);
e29d1b70
JA
19
20 /*
21 * lrand48_r seems to be broken and only fill the bottom
22 * 32-bits, even on 64-bit archs with 64-bit longs
23 */
24 todo = sizeof(r);
25 if (todo > len)
26 todo = len;
27
28 memcpy(p, &r, todo);
29
30 len -= todo;
31 p += todo;
32 }
33}
34
35static void hexdump(void *buffer, int len)
36{
37 unsigned char *p = buffer;
38 int i;
39
40 for (i = 0; i < len; i++)
6d86144d
JA
41 log_info("%02x", p[i]);
42 log_info("\n");
e29d1b70
JA
43}
44
3f9f4e26
SL
45static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
46 unsigned char header_num)
1e154bdb
JA
47{
48 unsigned char *p = io_u->buf;
49 unsigned char c;
50
3f9f4e26 51 p += header_num * hdr->len + sizeof(*hdr);
1e154bdb
JA
52 c = crc7(p, hdr->len - sizeof(*hdr));
53
54 if (c != hdr->crc7) {
3f9f4e26
SL
55 log_err("crc7: verify failed at %llu/%u\n",
56 io_u->offset + header_num * hdr->len,
57 hdr->len);
1e154bdb
JA
58 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
59 return 1;
60 }
61
62 return 0;
63}
64
3f9f4e26
SL
65static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
66 unsigned int header_num)
969f7ed3
JA
67{
68 unsigned char *p = io_u->buf;
69 unsigned short c;
70
3f9f4e26 71 p += header_num * hdr->len + sizeof(*hdr);
969f7ed3
JA
72 c = crc16(p, hdr->len - sizeof(*hdr));
73
74 if (c != hdr->crc16) {
3f9f4e26
SL
75 log_err("crc16: verify failed at %llu/%u\n",
76 io_u->offset + header_num * hdr->len,
77 hdr->len);
1f24ea44 78 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
969f7ed3
JA
79 return 1;
80 }
81
82 return 0;
83}
84
3f9f4e26
SL
85static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
86 unsigned int header_num)
d77a7af3 87{
3f9f4e26 88 unsigned char *p = io_u->buf;
d77a7af3
JA
89 unsigned long long c;
90
3f9f4e26 91 p += header_num * hdr->len + sizeof(*hdr);
d77a7af3
JA
92 c = crc64(p, hdr->len - sizeof(*hdr));
93
94 if (c != hdr->crc64) {
3f9f4e26
SL
95 log_err("crc64: verify failed at %llu/%u\n",
96 io_u->offset + header_num * hdr->len,
97 hdr->len);
d77a7af3
JA
98 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
99 return 1;
100 }
101
102 return 0;
103}
104
3f9f4e26
SL
105static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
106 unsigned int header_num)
e29d1b70 107{
969f7ed3 108 unsigned char *p = io_u->buf;
e29d1b70
JA
109 unsigned long c;
110
3f9f4e26 111 p += header_num * hdr->len + sizeof(*hdr);
e29d1b70
JA
112 c = crc32(p, hdr->len - sizeof(*hdr));
113
114 if (c != hdr->crc32) {
3f9f4e26
SL
115 log_err("crc32: verify failed at %llu/%u\n",
116 io_u->offset + header_num * hdr->len,
117 hdr->len);
e29d1b70
JA
118 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
119 return 1;
120 }
121
122 return 0;
123}
124
3f9f4e26
SL
125static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
126 unsigned int header_num)
e29d1b70 127{
3f9f4e26 128 unsigned char *p = io_u->buf;
8c432325
JA
129 uint32_t hash[MD5_HASH_WORDS];
130 struct md5_ctx md5_ctx = {
131 .hash = hash,
132 };
e29d1b70 133
3f9f4e26 134 p += header_num * hdr->len + sizeof(*hdr);
e29d1b70
JA
135 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
136
3f9f4e26
SL
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,
140 hdr->len);
e29d1b70 141 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
8c432325 142 hexdump(md5_ctx.hash, sizeof(hash));
e29d1b70
JA
143 return 1;
144 }
145
146 return 0;
147}
148
36690c9b 149int verify_io_u(struct thread_data *td, struct io_u *io_u)
e29d1b70 150{
3f9f4e26
SL
151 unsigned char *p = (unsigned char*) io_u->buf;
152 struct verify_header *hdr;
153 unsigned int hdr_inc, hdr_num = 0;
e29d1b70
JA
154 int ret;
155
1dcc0498 156 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b
JA
157 return 0;
158
3f9f4e26
SL
159 hdr_inc = io_u->buflen;
160 if (td->o.header_interval)
161 hdr_inc = td->o.header_interval;
e29d1b70 162
3f9f4e26
SL
163 for (; p < (unsigned char*) io_u->buf + io_u->buflen; p += hdr_inc) {
164 hdr = (struct verify_header*) p;
e29d1b70 165
3f9f4e26
SL
166 if (hdr->fio_magic != FIO_HDR_MAGIC) {
167 log_err("Bad verify header %x\n", hdr->fio_magic);
168 return EIO;
169 }
170
171 switch (hdr->verify_type) {
172 case VERIFY_MD5:
173 ret = verify_io_u_md5(hdr, io_u, hdr_num);
174 break;
175 case VERIFY_CRC64:
176 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
177 break;
178 case VERIFY_CRC32:
179 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
180 break;
181 case VERIFY_CRC16:
182 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
183 break;
184 case VERIFY_CRC7:
185 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
186 break;
187 default:
188 log_err("Bad verify type %u\n", hdr->verify_type);
189 ret = 1;
190 }
191 hdr_num++;
192 }
a7dfe862
JA
193
194 return 0;
e29d1b70
JA
195}
196
1e154bdb
JA
197static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
198{
199 hdr->crc7 = crc7(p, len);
200}
201
969f7ed3
JA
202static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
203{
204 hdr->crc16 = crc16(p, len);
205}
206
e29d1b70
JA
207static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
208{
209 hdr->crc32 = crc32(p, len);
210}
211
d77a7af3
JA
212static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
213{
214 hdr->crc64 = crc64(p, len);
215}
216
e29d1b70
JA
217static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
218{
8c432325
JA
219 struct md5_ctx md5_ctx = {
220 .hash = (uint32_t *) hdr->md5_digest,
221 };
e29d1b70 222
e29d1b70 223 md5_update(&md5_ctx, p, len);
e29d1b70
JA
224}
225
226/*
227 * fill body of io_u->buf with random data and add a header with the
228 * crc32 or md5 sum of that data.
229 */
230void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
231{
baefa9be
JA
232 const unsigned int len = io_u->buflen - sizeof(struct verify_header);
233 struct verify_header *hdr;
3f9f4e26
SL
234 unsigned char *p = io_u->buf, *data;
235 unsigned int data_len;
e29d1b70 236
9cc3d150
JA
237 if (td->o.verify == VERIFY_NULL)
238 return;
239
baefa9be
JA
240 fill_random_bytes(td, p, len);
241
3f9f4e26
SL
242 for (;p < (unsigned char*) io_u->buf + io_u->buflen; p += hdr->len) {
243 hdr = (struct verify_header*) p;
244
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;
250
251 data = p + sizeof(*hdr);
252 data_len = hdr->len - sizeof(*hdr);
253 switch (td->o.verify) {
254 case VERIFY_MD5:
255 fill_md5(hdr, data, data_len);
256 break;
257 case VERIFY_CRC64:
258 fill_crc64(hdr, data, data_len);
259 break;
260 case VERIFY_CRC32:
261 fill_crc32(hdr, data, data_len);
262 break;
263 case VERIFY_CRC16:
264 fill_crc16(hdr, data, data_len);
265 break;
266 case VERIFY_CRC7:
267 fill_crc7(hdr, data, data_len);
268 break;
269 default:
270 log_err("fio: bad verify type: %d\n", td->o.verify);
271 assert(0);
272 }
e29d1b70 273 }
e29d1b70
JA
274}
275
276int get_next_verify(struct thread_data *td, struct io_u *io_u)
277{
8de8f047 278 struct io_piece *ipo = NULL;
e29d1b70 279
d2d7fa53
JA
280 /*
281 * this io_u is from a requeue, we already filled the offsets
282 */
283 if (io_u->file)
284 return 0;
285
8de8f047
JA
286 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
287 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 288
8de8f047 289 ipo = rb_entry(n, struct io_piece, rb_node);
4b87898e 290 rb_erase(n, &td->io_hist_tree);
8de8f047
JA
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);
294 }
e29d1b70 295
8de8f047 296 if (ipo) {
e29d1b70
JA
297 io_u->offset = ipo->offset;
298 io_u->buflen = ipo->len;
36167d82 299 io_u->file = ipo->file;
97af62ce
JA
300
301 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
302 int r = td_io_open_file(td, io_u->file);
303
304 if (r)
305 return 1;
306 }
307
308 get_file(ipo->file);
309 assert(io_u->file->flags & FIO_FILE_OPEN);
e29d1b70 310 io_u->ddir = DDIR_READ;
36167d82
JA
311 io_u->xfer_buf = io_u->buf;
312 io_u->xfer_buflen = io_u->buflen;
e29d1b70
JA
313 free(ipo);
314 return 0;
315 }
316
317 return 1;
318}