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