Remove .maxval from verify_pattern
[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 10
90059d65 11static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
e29d1b70
JA
12{
13 unsigned int todo;
4c5946c6 14 int r;
e29d1b70
JA
15
16 while (len) {
4c5946c6 17 r = os_random_long(&td->verify_state);
e29d1b70
JA
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
90059d65
JA
34static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
35{
36 switch (td->o.verify_pattern_bytes) {
37 case 0:
38 fill_random_bytes(td, p, len);
39 break;
40 case 1:
41 memset(p, td->o.verify_pattern, len);
42 break;
43 case 2:
44 case 3:
45 case 4: {
46 unsigned int pattern = td->o.verify_pattern;
47 unsigned int i = 0;
48 unsigned char c1, c2, c3, c4;
49 unsigned char *b = p;
50
51 c1 = pattern & 0xff;
52 pattern >>= 8;
53 c2 = pattern & 0xff;
54 pattern >>= 8;
55 c3 = pattern & 0xff;
56 pattern >>= 8;
57 c4 = pattern & 0xff;
58
59 while (i < len) {
60 b[i++] = c1;
61 if (i == len)
62 break;
63 b[i++] = c2;
64 if (td->o.verify_pattern_bytes == 2 || i == len)
65 continue;
66 b[i++] = c3;
67 if (td->o.verify_pattern_bytes == 3 || i == len)
68 continue;
69 b[i++] = c4;
70 }
71 break;
72 }
73 }
74}
75
76static void memswp(void* buf1, void* buf2, unsigned int len)
546a9142
SL
77{
78 struct verify_header swap;
90059d65 79
546a9142
SL
80 memcpy(&swap, buf1, len);
81 memcpy(buf1, buf2, len);
82 memcpy(buf2, &swap, len);
83}
84
e29d1b70
JA
85static void hexdump(void *buffer, int len)
86{
87 unsigned char *p = buffer;
88 int i;
89
90 for (i = 0; i < len; i++)
6d86144d
JA
91 log_info("%02x", p[i]);
92 log_info("\n");
e29d1b70
JA
93}
94
d9f2caf3
JA
95/*
96 * Return data area 'header_num'
97 */
98static inline void *io_u_verify_off(struct verify_header *hdr,
99 struct io_u *io_u,
100 unsigned char header_num)
101{
102 return io_u->buf + sizeof(*hdr) + header_num * hdr->len;
103}
104
3f9f4e26
SL
105static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
106 unsigned char header_num)
1e154bdb 107{
d9f2caf3 108 void *p = io_u_verify_off(hdr, io_u, header_num);
1e154bdb
JA
109 unsigned char c;
110
1e154bdb
JA
111 c = crc7(p, hdr->len - sizeof(*hdr));
112
113 if (c != hdr->crc7) {
3f9f4e26
SL
114 log_err("crc7: verify failed at %llu/%u\n",
115 io_u->offset + header_num * hdr->len,
116 hdr->len);
1e154bdb
JA
117 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
118 return 1;
119 }
120
121 return 0;
122}
123
3f9f4e26
SL
124static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
125 unsigned int header_num)
969f7ed3 126{
d9f2caf3 127 void *p = io_u_verify_off(hdr, io_u, header_num);
969f7ed3
JA
128 unsigned short c;
129
969f7ed3
JA
130 c = crc16(p, hdr->len - sizeof(*hdr));
131
132 if (c != hdr->crc16) {
3f9f4e26
SL
133 log_err("crc16: verify failed at %llu/%u\n",
134 io_u->offset + header_num * hdr->len,
135 hdr->len);
1f24ea44 136 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
969f7ed3
JA
137 return 1;
138 }
139
140 return 0;
141}
142
3f9f4e26
SL
143static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
144 unsigned int header_num)
d77a7af3 145{
d9f2caf3 146 void *p = io_u_verify_off(hdr, io_u, header_num);
d77a7af3
JA
147 unsigned long long c;
148
149 c = crc64(p, hdr->len - sizeof(*hdr));
150
151 if (c != hdr->crc64) {
3f9f4e26
SL
152 log_err("crc64: verify failed at %llu/%u\n",
153 io_u->offset + header_num * hdr->len,
154 hdr->len);
d77a7af3
JA
155 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
156 return 1;
157 }
158
159 return 0;
160}
161
3f9f4e26
SL
162static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
163 unsigned int header_num)
e29d1b70 164{
d9f2caf3 165 void *p = io_u_verify_off(hdr, io_u, header_num);
e29d1b70
JA
166 unsigned long c;
167
e29d1b70
JA
168 c = crc32(p, hdr->len - sizeof(*hdr));
169
170 if (c != hdr->crc32) {
3f9f4e26
SL
171 log_err("crc32: verify failed at %llu/%u\n",
172 io_u->offset + header_num * hdr->len,
173 hdr->len);
e29d1b70
JA
174 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
175 return 1;
176 }
177
178 return 0;
179}
180
3f9f4e26
SL
181static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
182 unsigned int header_num)
e29d1b70 183{
d9f2caf3 184 void *p = io_u_verify_off(hdr, io_u, header_num);
8c432325
JA
185 uint32_t hash[MD5_HASH_WORDS];
186 struct md5_ctx md5_ctx = {
187 .hash = hash,
188 };
e29d1b70 189
fd00954c 190 memset(md5_ctx.hash, 0, sizeof(hdr->md5_digest));
e29d1b70
JA
191 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
192
c9b3fdf3 193 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
3f9f4e26
SL
194 log_err("md5: verify failed at %llu/%u\n",
195 io_u->offset + header_num * hdr->len,
196 hdr->len);
e29d1b70 197 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
8c432325 198 hexdump(md5_ctx.hash, sizeof(hash));
e29d1b70
JA
199 return 1;
200 }
201
202 return 0;
203}
204
36690c9b 205int verify_io_u(struct thread_data *td, struct io_u *io_u)
e29d1b70 206{
3f9f4e26
SL
207 struct verify_header *hdr;
208 unsigned int hdr_inc, hdr_num = 0;
95646108 209 void *p;
e29d1b70
JA
210 int ret;
211
1dcc0498 212 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b
JA
213 return 0;
214
3f9f4e26 215 hdr_inc = io_u->buflen;
a59e170d
JA
216 if (td->o.verify_interval)
217 hdr_inc = td->o.verify_interval;
e29d1b70 218
95646108 219 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
a59e170d
JA
220 if (td->o.verify_offset)
221 memswp(p, p + td->o.verify_offset, sizeof(*hdr));
546a9142 222
95646108 223 hdr = p;
e29d1b70 224
3f9f4e26
SL
225 if (hdr->fio_magic != FIO_HDR_MAGIC) {
226 log_err("Bad verify header %x\n", hdr->fio_magic);
227 return EIO;
228 }
229
230 switch (hdr->verify_type) {
231 case VERIFY_MD5:
232 ret = verify_io_u_md5(hdr, io_u, hdr_num);
233 break;
234 case VERIFY_CRC64:
235 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
236 break;
237 case VERIFY_CRC32:
238 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
239 break;
240 case VERIFY_CRC16:
241 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
242 break;
243 case VERIFY_CRC7:
244 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
245 break;
246 default:
247 log_err("Bad verify type %u\n", hdr->verify_type);
248 ret = 1;
249 }
250 hdr_num++;
251 }
a7dfe862
JA
252
253 return 0;
e29d1b70
JA
254}
255
1e154bdb
JA
256static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
257{
258 hdr->crc7 = crc7(p, len);
259}
260
969f7ed3
JA
261static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
262{
263 hdr->crc16 = crc16(p, len);
264}
265
e29d1b70
JA
266static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
267{
268 hdr->crc32 = crc32(p, len);
269}
270
d77a7af3
JA
271static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
272{
273 hdr->crc64 = crc64(p, len);
274}
275
e29d1b70
JA
276static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
277{
8c432325
JA
278 struct md5_ctx md5_ctx = {
279 .hash = (uint32_t *) hdr->md5_digest,
280 };
e29d1b70 281
fd00954c 282 memset(md5_ctx.hash, 0, sizeof(hdr->md5_digest));
e29d1b70 283 md5_update(&md5_ctx, p, len);
e29d1b70
JA
284}
285
286/*
287 * fill body of io_u->buf with random data and add a header with the
288 * crc32 or md5 sum of that data.
289 */
290void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
291{
baefa9be 292 struct verify_header *hdr;
95646108 293 void *p = io_u->buf, *data;
546a9142 294 unsigned int hdr_inc, data_len;
e29d1b70 295
9cc3d150
JA
296 if (td->o.verify == VERIFY_NULL)
297 return;
298
90059d65 299 fill_pattern(td, p, io_u->buflen);
546a9142
SL
300
301 hdr_inc = io_u->buflen;
a59e170d
JA
302 if (td->o.verify_interval)
303 hdr_inc = td->o.verify_interval;
546a9142 304 data_len = hdr_inc - sizeof(*hdr);
baefa9be 305
95646108
JA
306 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
307 hdr = p;
3f9f4e26
SL
308
309 hdr->fio_magic = FIO_HDR_MAGIC;
310 hdr->verify_type = td->o.verify;
546a9142 311 hdr->len = hdr_inc;
3f9f4e26
SL
312
313 data = p + sizeof(*hdr);
3f9f4e26
SL
314 switch (td->o.verify) {
315 case VERIFY_MD5:
316 fill_md5(hdr, data, data_len);
317 break;
318 case VERIFY_CRC64:
319 fill_crc64(hdr, data, data_len);
320 break;
321 case VERIFY_CRC32:
322 fill_crc32(hdr, data, data_len);
323 break;
324 case VERIFY_CRC16:
325 fill_crc16(hdr, data, data_len);
326 break;
327 case VERIFY_CRC7:
328 fill_crc7(hdr, data, data_len);
329 break;
330 default:
331 log_err("fio: bad verify type: %d\n", td->o.verify);
332 assert(0);
333 }
a59e170d
JA
334 if (td->o.verify_offset)
335 memswp(p, p + td->o.verify_offset, sizeof(*hdr));
e29d1b70 336 }
e29d1b70
JA
337}
338
339int get_next_verify(struct thread_data *td, struct io_u *io_u)
340{
8de8f047 341 struct io_piece *ipo = NULL;
e29d1b70 342
d2d7fa53
JA
343 /*
344 * this io_u is from a requeue, we already filled the offsets
345 */
346 if (io_u->file)
347 return 0;
348
8de8f047
JA
349 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
350 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 351
8de8f047 352 ipo = rb_entry(n, struct io_piece, rb_node);
4b87898e 353 rb_erase(n, &td->io_hist_tree);
8de8f047
JA
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);
357 }
e29d1b70 358
8de8f047 359 if (ipo) {
e29d1b70
JA
360 io_u->offset = ipo->offset;
361 io_u->buflen = ipo->len;
36167d82 362 io_u->file = ipo->file;
97af62ce
JA
363
364 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
365 int r = td_io_open_file(td, io_u->file);
366
367 if (r)
368 return 1;
369 }
370
371 get_file(ipo->file);
372 assert(io_u->file->flags & FIO_FILE_OPEN);
e29d1b70 373 io_u->ddir = DDIR_READ;
36167d82
JA
374 io_u->xfer_buf = io_u->buf;
375 io_u->xfer_buflen = io_u->buflen;
e29d1b70
JA
376 free(ipo);
377 return 0;
378 }
379
380 return 1;
381}