Improve verify pattern failure logging
[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
eef6eea1
JA
11#include "crc/md5.h"
12#include "crc/crc64.h"
13#include "crc/crc32.h"
14#include "crc/crc16.h"
15#include "crc/crc7.h"
16#include "crc/sha256.h"
17#include "crc/sha512.h"
cd14cc10 18
90059d65 19static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
e29d1b70
JA
20{
21 unsigned int todo;
4c5946c6 22 int r;
e29d1b70
JA
23
24 while (len) {
4c5946c6 25 r = os_random_long(&td->verify_state);
e29d1b70
JA
26
27 /*
28 * lrand48_r seems to be broken and only fill the bottom
29 * 32-bits, even on 64-bit archs with 64-bit longs
30 */
31 todo = sizeof(r);
32 if (todo > len)
33 todo = len;
34
35 memcpy(p, &r, todo);
36
37 len -= todo;
38 p += todo;
39 }
40}
41
90059d65
JA
42static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
43{
44 switch (td->o.verify_pattern_bytes) {
45 case 0:
46 fill_random_bytes(td, p, len);
47 break;
48 case 1:
49 memset(p, td->o.verify_pattern, len);
50 break;
51 case 2:
52 case 3:
53 case 4: {
54 unsigned int pattern = td->o.verify_pattern;
55 unsigned int i = 0;
56 unsigned char c1, c2, c3, c4;
57 unsigned char *b = p;
58
59 c1 = pattern & 0xff;
60 pattern >>= 8;
61 c2 = pattern & 0xff;
62 pattern >>= 8;
63 c3 = pattern & 0xff;
64 pattern >>= 8;
65 c4 = pattern & 0xff;
66
67 while (i < len) {
68 b[i++] = c1;
69 if (i == len)
70 break;
71 b[i++] = c2;
72 if (td->o.verify_pattern_bytes == 2 || i == len)
73 continue;
74 b[i++] = c3;
75 if (td->o.verify_pattern_bytes == 3 || i == len)
76 continue;
77 b[i++] = c4;
78 }
79 break;
80 }
81 }
82}
83
84static void memswp(void* buf1, void* buf2, unsigned int len)
546a9142 85{
dee6de74
SL
86 char swap[200];
87
88 assert(len <= sizeof(swap));
90059d65 89
546a9142
SL
90 memcpy(&swap, buf1, len);
91 memcpy(buf1, buf2, len);
92 memcpy(buf2, &swap, len);
93}
94
e29d1b70
JA
95static void hexdump(void *buffer, int len)
96{
97 unsigned char *p = buffer;
98 int i;
99
100 for (i = 0; i < len; i++)
6d86144d
JA
101 log_info("%02x", p[i]);
102 log_info("\n");
e29d1b70
JA
103}
104
87677832
JA
105/*
106 * Prepare for seperation of verify_header and checksum header
107 */
546dfd9f 108static inline unsigned int __hdr_size(int verify_type)
87677832 109{
546dfd9f 110 unsigned int len;
87677832 111
546dfd9f
JA
112 switch (verify_type) {
113 case VERIFY_NONE:
114 case VERIFY_NULL:
115 len = 0;
116 break;
117 case VERIFY_MD5:
118 len = sizeof(struct vhdr_md5);
119 break;
120 case VERIFY_CRC64:
121 len = sizeof(struct vhdr_crc64);
122 break;
123 case VERIFY_CRC32:
124 len = sizeof(struct vhdr_crc32);
125 break;
126 case VERIFY_CRC16:
127 len = sizeof(struct vhdr_crc16);
128 break;
129 case VERIFY_CRC7:
130 len = sizeof(struct vhdr_crc7);
131 break;
132 case VERIFY_SHA256:
133 len = sizeof(struct vhdr_sha256);
134 break;
135 case VERIFY_SHA512:
136 len = sizeof(struct vhdr_sha512);
137 break;
7437ee87
SL
138 case VERIFY_META:
139 len = sizeof(struct vhdr_meta);
140 break;
546dfd9f
JA
141 default:
142 log_err("fio: unknown verify header!\n");
143 assert(0);
144 }
145
146 return len + sizeof(struct verify_header);
87677832
JA
147}
148
149static inline unsigned int hdr_size(struct verify_header *hdr)
150{
546dfd9f
JA
151 return __hdr_size(hdr->verify_type);
152}
153
154static void *hdr_priv(struct verify_header *hdr)
155{
156 void *priv = hdr;
157
158 return priv + sizeof(struct verify_header);
87677832
JA
159}
160
d9f2caf3
JA
161/*
162 * Return data area 'header_num'
163 */
164static inline void *io_u_verify_off(struct verify_header *hdr,
165 struct io_u *io_u,
166 unsigned char header_num)
167{
87677832 168 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
d9f2caf3
JA
169}
170
7437ee87
SL
171static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
172 struct io_u *io_u, unsigned int header_num)
173{
174 struct vhdr_meta *vh = hdr_priv(hdr);
175
176 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
177 log_err("meta: verify failed at %llu/%u\n",
178 io_u->offset + header_num * hdr->len,
179 hdr->len);
180 return 1;
181 }
182
183 return 0;
184}
185
cd14cc10
JA
186static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
187 unsigned int header_num)
188{
189 void *p = io_u_verify_off(hdr, io_u, header_num);
546dfd9f 190 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10
JA
191 uint8_t sha512[128];
192 struct sha512_ctx sha512_ctx = {
193 .buf = sha512,
194 };
195
196 sha512_init(&sha512_ctx);
87677832 197 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
cd14cc10 198
546dfd9f 199 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
cd14cc10
JA
200 log_err("sha512: verify failed at %llu/%u\n",
201 io_u->offset + header_num * hdr->len,
202 hdr->len);
546dfd9f 203 hexdump(vh->sha512, sizeof(vh->sha512));
cd14cc10
JA
204 hexdump(sha512_ctx.buf, sizeof(sha512));
205 return 1;
206 }
207
208 return 0;
209}
210
211static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
212 unsigned int header_num)
213{
214 void *p = io_u_verify_off(hdr, io_u, header_num);
546dfd9f 215 struct vhdr_sha256 *vh = hdr_priv(hdr);
cd14cc10
JA
216 uint8_t sha256[128];
217 struct sha256_ctx sha256_ctx = {
218 .buf = sha256,
219 };
220
221 sha256_init(&sha256_ctx);
87677832 222 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
cd14cc10 223
546dfd9f 224 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
cd14cc10
JA
225 log_err("sha256: verify failed at %llu/%u\n",
226 io_u->offset + header_num * hdr->len,
227 hdr->len);
546dfd9f 228 hexdump(vh->sha256, sizeof(vh->sha256));
cd14cc10
JA
229 hexdump(sha256_ctx.buf, sizeof(sha256));
230 return 1;
231 }
232
233 return 0;
234}
235
3f9f4e26
SL
236static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
237 unsigned char header_num)
1e154bdb 238{
d9f2caf3 239 void *p = io_u_verify_off(hdr, io_u, header_num);
546dfd9f 240 struct vhdr_crc7 *vh = hdr_priv(hdr);
1e154bdb
JA
241 unsigned char c;
242
87677832 243 c = crc7(p, hdr->len - hdr_size(hdr));
1e154bdb 244
546dfd9f 245 if (c != vh->crc7) {
3f9f4e26
SL
246 log_err("crc7: verify failed at %llu/%u\n",
247 io_u->offset + header_num * hdr->len,
248 hdr->len);
546dfd9f 249 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
1e154bdb
JA
250 return 1;
251 }
252
253 return 0;
254}
255
3f9f4e26
SL
256static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
257 unsigned int header_num)
969f7ed3 258{
d9f2caf3 259 void *p = io_u_verify_off(hdr, io_u, header_num);
546dfd9f 260 struct vhdr_crc16 *vh = hdr_priv(hdr);
969f7ed3
JA
261 unsigned short c;
262
87677832 263 c = crc16(p, hdr->len - hdr_size(hdr));
969f7ed3 264
546dfd9f 265 if (c != vh->crc16) {
3f9f4e26
SL
266 log_err("crc16: verify failed at %llu/%u\n",
267 io_u->offset + header_num * hdr->len,
268 hdr->len);
546dfd9f 269 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
969f7ed3
JA
270 return 1;
271 }
272
273 return 0;
274}
275
3f9f4e26
SL
276static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
277 unsigned int header_num)
d77a7af3 278{
d9f2caf3 279 void *p = io_u_verify_off(hdr, io_u, header_num);
546dfd9f 280 struct vhdr_crc64 *vh = hdr_priv(hdr);
d77a7af3
JA
281 unsigned long long c;
282
87677832 283 c = crc64(p, hdr->len - hdr_size(hdr));
d77a7af3 284
546dfd9f 285 if (c != vh->crc64) {
3f9f4e26
SL
286 log_err("crc64: verify failed at %llu/%u\n",
287 io_u->offset + header_num * hdr->len,
288 hdr->len);
546dfd9f 289 log_err("crc64: wanted %llx, got %llx\n", (unsigned long long) vh->crc64, c);
d77a7af3
JA
290 return 1;
291 }
292
293 return 0;
294}
295
3f9f4e26
SL
296static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
297 unsigned int header_num)
e29d1b70 298{
d9f2caf3 299 void *p = io_u_verify_off(hdr, io_u, header_num);
546dfd9f
JA
300 struct vhdr_crc32 *vh = hdr_priv(hdr);
301 uint32_t c;
e29d1b70 302
87677832 303 c = crc32(p, hdr->len - hdr_size(hdr));
e29d1b70 304
546dfd9f 305 if (c != vh->crc32) {
3f9f4e26
SL
306 log_err("crc32: verify failed at %llu/%u\n",
307 io_u->offset + header_num * hdr->len,
308 hdr->len);
546dfd9f 309 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
e29d1b70
JA
310 return 1;
311 }
312
313 return 0;
314}
315
3f9f4e26
SL
316static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
317 unsigned int header_num)
e29d1b70 318{
d9f2caf3 319 void *p = io_u_verify_off(hdr, io_u, header_num);
546dfd9f 320 struct vhdr_md5 *vh = hdr_priv(hdr);
8c432325
JA
321 uint32_t hash[MD5_HASH_WORDS];
322 struct md5_ctx md5_ctx = {
323 .hash = hash,
324 };
e29d1b70 325
61f821f1 326 md5_init(&md5_ctx);
87677832 327 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
e29d1b70 328
546dfd9f 329 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
3f9f4e26
SL
330 log_err("md5: verify failed at %llu/%u\n",
331 io_u->offset + header_num * hdr->len,
332 hdr->len);
546dfd9f 333 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
8c432325 334 hexdump(md5_ctx.hash, sizeof(hash));
e29d1b70
JA
335 return 1;
336 }
337
338 return 0;
339}
340
3f199b01
JA
341static unsigned int hweight8(unsigned int w)
342{
343 unsigned int res = w - ((w >> 1) & 0x55);
344
345 res = (res & 0x33) + ((res >> 2) & 0x33);
346 return (res + (res >> 4)) & 0x0F;
347}
348
a944e335
SL
349int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
350 char* buf, unsigned int len, unsigned int mod)
351{
352 unsigned int i;
353 char split_pattern[4];
354
355 for (i = 0; i < 4; i++) {
356 split_pattern[i] = pattern & 0xff;
357 pattern >>= 8;
358 }
359
360 for (i = 0; i < len; i++) {
3f199b01
JA
361 if (buf[i] != split_pattern[mod]) {
362 unsigned int bits;
363
364 bits = hweight8(buf[i] ^ split_pattern[mod]);
365 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
366 buf[i], split_pattern[mod], bits);
367 log_err("fio: bad pattern block offset %u\n", i);
a944e335 368 return 1;
3f199b01 369 }
a944e335
SL
370 mod++;
371 if (mod == pattern_size)
372 mod = 0;
373 }
374
375 return 0;
376}
377
36690c9b 378int verify_io_u(struct thread_data *td, struct io_u *io_u)
e29d1b70 379{
3f9f4e26 380 struct verify_header *hdr;
a944e335 381 unsigned int hdr_size, hdr_inc, hdr_num = 0;
95646108 382 void *p;
e29d1b70
JA
383 int ret;
384
1dcc0498 385 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b
JA
386 return 0;
387
3f9f4e26 388 hdr_inc = io_u->buflen;
a59e170d
JA
389 if (td->o.verify_interval)
390 hdr_inc = td->o.verify_interval;
e29d1b70 391
3f199b01 392 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc, hdr_num++) {
a944e335 393 hdr_size = __hdr_size(td->o.verify);
a59e170d 394 if (td->o.verify_offset)
a944e335 395 memswp(p, p + td->o.verify_offset, hdr_size);
95646108 396 hdr = p;
e29d1b70 397
3f199b01
JA
398 if (hdr->fio_magic != FIO_HDR_MAGIC) {
399 log_err("Bad verify header %x\n", hdr->fio_magic);
400 return EIO;
401 }
402
a944e335
SL
403 if (td->o.verify_pattern_bytes) {
404 ret = verify_io_u_pattern(td->o.verify_pattern,
405 td->o.verify_pattern_bytes,
406 p + hdr_size,
407 hdr_inc - hdr_size,
408 hdr_size % 4);
3f199b01
JA
409 if (ret)
410 log_err("fio: verify failed at %llu/%u\n",
a944e335 411 io_u->offset + hdr_num * hdr->len,
3f199b01
JA
412 hdr->len);
413 continue;
3f9f4e26
SL
414 }
415
416 switch (hdr->verify_type) {
417 case VERIFY_MD5:
418 ret = verify_io_u_md5(hdr, io_u, hdr_num);
419 break;
420 case VERIFY_CRC64:
421 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
422 break;
423 case VERIFY_CRC32:
424 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
425 break;
426 case VERIFY_CRC16:
427 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
428 break;
429 case VERIFY_CRC7:
430 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
431 break;
cd14cc10
JA
432 case VERIFY_SHA256:
433 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
434 break;
435 case VERIFY_SHA512:
436 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
437 break;
7437ee87
SL
438 case VERIFY_META:
439 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
440 break;
3f9f4e26
SL
441 default:
442 log_err("Bad verify type %u\n", hdr->verify_type);
443 ret = 1;
444 }
3f9f4e26 445 }
a7dfe862
JA
446
447 return 0;
e29d1b70
JA
448}
449
7437ee87
SL
450static void fill_meta(struct verify_header *hdr, struct thread_data *td,
451 struct io_u *io_u, unsigned int header_num)
452{
453 struct vhdr_meta *vh = hdr_priv(hdr);
454
455 vh->thread = td->thread_number;
456
457 vh->time_sec = io_u->start_time.tv_sec;
458 vh->time_usec = io_u->start_time.tv_usec;
459
460 vh->numberio = td->io_issues[DDIR_WRITE];
461
462 vh->offset = io_u->offset + header_num * td->o.verify_interval;
463}
464
cd14cc10
JA
465static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
466{
546dfd9f 467 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10 468 struct sha512_ctx sha512_ctx = {
546dfd9f 469 .buf = vh->sha512,
cd14cc10
JA
470 };
471
472 sha512_init(&sha512_ctx);
473 sha512_update(&sha512_ctx, p, len);
474}
475
476static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
477{
546dfd9f 478 struct vhdr_sha256 *vh = hdr_priv(hdr);
cd14cc10 479 struct sha256_ctx sha256_ctx = {
546dfd9f 480 .buf = vh->sha256,
cd14cc10
JA
481 };
482
483 sha256_init(&sha256_ctx);
484 sha256_update(&sha256_ctx, p, len);
485}
486
1e154bdb
JA
487static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
488{
546dfd9f
JA
489 struct vhdr_crc7 *vh = hdr_priv(hdr);
490
491 vh->crc7 = crc7(p, len);
1e154bdb
JA
492}
493
969f7ed3
JA
494static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
495{
546dfd9f
JA
496 struct vhdr_crc16 *vh = hdr_priv(hdr);
497
498 vh->crc16 = crc16(p, len);
969f7ed3
JA
499}
500
e29d1b70
JA
501static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
502{
546dfd9f
JA
503 struct vhdr_crc32 *vh = hdr_priv(hdr);
504
505 vh->crc32 = crc32(p, len);
e29d1b70
JA
506}
507
d77a7af3
JA
508static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
509{
546dfd9f
JA
510 struct vhdr_crc64 *vh = hdr_priv(hdr);
511
512 vh->crc64 = crc64(p, len);
d77a7af3
JA
513}
514
e29d1b70
JA
515static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
516{
546dfd9f 517 struct vhdr_md5 *vh = hdr_priv(hdr);
8c432325 518 struct md5_ctx md5_ctx = {
546dfd9f 519 .hash = (uint32_t *) vh->md5_digest,
8c432325 520 };
e29d1b70 521
61f821f1 522 md5_init(&md5_ctx);
e29d1b70 523 md5_update(&md5_ctx, p, len);
e29d1b70
JA
524}
525
526/*
527 * fill body of io_u->buf with random data and add a header with the
528 * crc32 or md5 sum of that data.
529 */
530void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
531{
baefa9be 532 struct verify_header *hdr;
95646108 533 void *p = io_u->buf, *data;
7437ee87 534 unsigned int hdr_inc, data_len, header_num = 0;
e29d1b70 535
9cc3d150
JA
536 if (td->o.verify == VERIFY_NULL)
537 return;
538
90059d65 539 fill_pattern(td, p, io_u->buflen);
546a9142
SL
540
541 hdr_inc = io_u->buflen;
a59e170d
JA
542 if (td->o.verify_interval)
543 hdr_inc = td->o.verify_interval;
baefa9be 544
95646108
JA
545 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
546 hdr = p;
3f9f4e26
SL
547
548 hdr->fio_magic = FIO_HDR_MAGIC;
549 hdr->verify_type = td->o.verify;
546a9142 550 hdr->len = hdr_inc;
87677832 551 data_len = hdr_inc - hdr_size(hdr);
3f9f4e26 552
87677832 553 data = p + hdr_size(hdr);
3f9f4e26
SL
554 switch (td->o.verify) {
555 case VERIFY_MD5:
556 fill_md5(hdr, data, data_len);
557 break;
558 case VERIFY_CRC64:
559 fill_crc64(hdr, data, data_len);
560 break;
561 case VERIFY_CRC32:
562 fill_crc32(hdr, data, data_len);
563 break;
564 case VERIFY_CRC16:
565 fill_crc16(hdr, data, data_len);
566 break;
567 case VERIFY_CRC7:
568 fill_crc7(hdr, data, data_len);
569 break;
cd14cc10
JA
570 case VERIFY_SHA256:
571 fill_sha256(hdr, data, data_len);
572 break;
573 case VERIFY_SHA512:
574 fill_sha512(hdr, data, data_len);
575 break;
7437ee87
SL
576 case VERIFY_META:
577 fill_meta(hdr, td, io_u, header_num);
578 break;
3f9f4e26
SL
579 default:
580 log_err("fio: bad verify type: %d\n", td->o.verify);
581 assert(0);
582 }
a59e170d 583 if (td->o.verify_offset)
87677832 584 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
7437ee87 585 header_num++;
e29d1b70 586 }
e29d1b70
JA
587}
588
589int get_next_verify(struct thread_data *td, struct io_u *io_u)
590{
8de8f047 591 struct io_piece *ipo = NULL;
e29d1b70 592
d2d7fa53
JA
593 /*
594 * this io_u is from a requeue, we already filled the offsets
595 */
596 if (io_u->file)
597 return 0;
598
8de8f047
JA
599 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
600 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 601
8de8f047 602 ipo = rb_entry(n, struct io_piece, rb_node);
4b87898e 603 rb_erase(n, &td->io_hist_tree);
8de8f047
JA
604 } else if (!list_empty(&td->io_hist_list)) {
605 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
606 list_del(&ipo->list);
607 }
e29d1b70 608
8de8f047 609 if (ipo) {
e29d1b70
JA
610 io_u->offset = ipo->offset;
611 io_u->buflen = ipo->len;
36167d82 612 io_u->file = ipo->file;
97af62ce
JA
613
614 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
615 int r = td_io_open_file(td, io_u->file);
616
617 if (r)
618 return 1;
619 }
620
621 get_file(ipo->file);
622 assert(io_u->file->flags & FIO_FILE_OPEN);
e29d1b70 623 io_u->ddir = DDIR_READ;
36167d82
JA
624 io_u->xfer_buf = io_u->buf;
625 io_u->xfer_buflen = io_u->buflen;
e29d1b70
JA
626 free(ipo);
627 return 0;
628 }
629
630 return 1;
631}