First step in unified crc error 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>
e8462bd8 8#include <pthread.h>
e29d1b70
JA
9
10#include "fio.h"
4f5af7b2 11#include "verify.h"
e8462bd8 12#include "smalloc.h"
e29d1b70 13
eef6eea1
JA
14#include "crc/md5.h"
15#include "crc/crc64.h"
16#include "crc/crc32.h"
bac39e0e 17#include "crc/crc32c.h"
eef6eea1
JA
18#include "crc/crc16.h"
19#include "crc/crc7.h"
20#include "crc/sha256.h"
21#include "crc/sha512.h"
7c353ceb 22#include "crc/sha1.h"
cd14cc10 23
90059d65 24static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
e29d1b70
JA
25{
26 unsigned int todo;
4c5946c6 27 int r;
e29d1b70
JA
28
29 while (len) {
4c5946c6 30 r = os_random_long(&td->verify_state);
e29d1b70
JA
31
32 /*
33 * lrand48_r seems to be broken and only fill the bottom
34 * 32-bits, even on 64-bit archs with 64-bit longs
35 */
36 todo = sizeof(r);
37 if (todo > len)
38 todo = len;
39
40 memcpy(p, &r, todo);
41
42 len -= todo;
43 p += todo;
44 }
45}
46
90059d65
JA
47static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
48{
49 switch (td->o.verify_pattern_bytes) {
50 case 0:
bd6f78b2 51 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
90059d65
JA
52 fill_random_bytes(td, p, len);
53 break;
54 case 1:
bd6f78b2 55 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
0e92f873 56 memset(p, td->o.verify_pattern[0], len);
90059d65 57 break;
0e92f873
RR
58 default: {
59 unsigned int i = 0, size = 0;
90059d65
JA
60 unsigned char *b = p;
61
bd6f78b2
JA
62 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
63 td->o.verify_pattern_bytes, len);
64
90059d65 65 while (i < len) {
0e92f873
RR
66 size = td->o.verify_pattern_bytes;
67 if (size > (len - i))
68 size = len - i;
69 memcpy(b+i, td->o.verify_pattern, size);
70 i += size;
90059d65
JA
71 }
72 break;
73 }
74 }
75}
76
4764aec9 77static void memswp(void *buf1, void *buf2, unsigned int len)
546a9142 78{
dee6de74
SL
79 char swap[200];
80
81 assert(len <= sizeof(swap));
90059d65 82
546a9142
SL
83 memcpy(&swap, buf1, len);
84 memcpy(buf1, buf2, len);
85 memcpy(buf2, &swap, len);
86}
87
e29d1b70
JA
88static void hexdump(void *buffer, int len)
89{
90 unsigned char *p = buffer;
91 int i;
92
93 for (i = 0; i < len; i++)
6d86144d
JA
94 log_info("%02x", p[i]);
95 log_info("\n");
e29d1b70
JA
96}
97
87677832
JA
98/*
99 * Prepare for seperation of verify_header and checksum header
100 */
546dfd9f 101static inline unsigned int __hdr_size(int verify_type)
87677832 102{
5921e80c 103 unsigned int len = len;
87677832 104
546dfd9f
JA
105 switch (verify_type) {
106 case VERIFY_NONE:
107 case VERIFY_NULL:
108 len = 0;
109 break;
110 case VERIFY_MD5:
111 len = sizeof(struct vhdr_md5);
112 break;
113 case VERIFY_CRC64:
114 len = sizeof(struct vhdr_crc64);
115 break;
bac39e0e 116 case VERIFY_CRC32C:
546dfd9f 117 case VERIFY_CRC32:
3845591f 118 case VERIFY_CRC32C_INTEL:
546dfd9f
JA
119 len = sizeof(struct vhdr_crc32);
120 break;
121 case VERIFY_CRC16:
122 len = sizeof(struct vhdr_crc16);
123 break;
124 case VERIFY_CRC7:
125 len = sizeof(struct vhdr_crc7);
126 break;
127 case VERIFY_SHA256:
128 len = sizeof(struct vhdr_sha256);
129 break;
130 case VERIFY_SHA512:
131 len = sizeof(struct vhdr_sha512);
132 break;
7437ee87
SL
133 case VERIFY_META:
134 len = sizeof(struct vhdr_meta);
135 break;
7c353ceb
JA
136 case VERIFY_SHA1:
137 len = sizeof(struct vhdr_sha1);
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
936216f8
JA
159/*
160 * Verify container, pass info to verify handlers and allow them to
161 * pass info back in case of error
162 */
163struct vcont {
164 /*
165 * Input
166 */
167 struct io_u *io_u;
168 unsigned int hdr_num;
169
170 /*
171 * Output, only valid in case of error
172 */
173 unsigned char *good_crc;
174 unsigned char *bad_crc;
175 unsigned int crc_len;
176};
177
d9f2caf3
JA
178/*
179 * Return data area 'header_num'
180 */
936216f8 181static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
d9f2caf3 182{
936216f8 183 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
d9f2caf3
JA
184}
185
7437ee87 186static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
936216f8 187 struct vcont *vc)
7437ee87
SL
188{
189 struct vhdr_meta *vh = hdr_priv(hdr);
936216f8 190 struct io_u *io_u = vc->io_u;
7437ee87 191
bd6f78b2
JA
192 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
193
936216f8 194 if (vh->offset != io_u->offset + vc->hdr_num * td->o.verify_interval) {
7437ee87 195 log_err("meta: verify failed at %llu/%u\n",
936216f8 196 io_u->offset + vc->hdr_num * hdr->len, hdr->len);
9fd18969 197 return EILSEQ;
7437ee87
SL
198 }
199
200 return 0;
201}
202
936216f8 203static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
cd14cc10 204{
936216f8 205 void *p = io_u_verify_off(hdr, vc);
546dfd9f 206 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10
JA
207 uint8_t sha512[128];
208 struct sha512_ctx sha512_ctx = {
209 .buf = sha512,
210 };
211
936216f8 212 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 213
cd14cc10 214 sha512_init(&sha512_ctx);
87677832 215 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
cd14cc10 216
546dfd9f 217 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
cd14cc10 218 log_err("sha512: verify failed at %llu/%u\n",
936216f8 219 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
546dfd9f 220 hexdump(vh->sha512, sizeof(vh->sha512));
cd14cc10 221 hexdump(sha512_ctx.buf, sizeof(sha512));
9fd18969 222 return EILSEQ;
cd14cc10
JA
223 }
224
225 return 0;
226}
227
936216f8 228static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
cd14cc10 229{
936216f8 230 void *p = io_u_verify_off(hdr, vc);
546dfd9f 231 struct vhdr_sha256 *vh = hdr_priv(hdr);
bc77f56f 232 uint8_t sha256[64];
cd14cc10
JA
233 struct sha256_ctx sha256_ctx = {
234 .buf = sha256,
235 };
236
936216f8 237 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 238
cd14cc10 239 sha256_init(&sha256_ctx);
87677832 240 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
cd14cc10 241
546dfd9f 242 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
cd14cc10 243 log_err("sha256: verify failed at %llu/%u\n",
936216f8 244 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
546dfd9f 245 hexdump(vh->sha256, sizeof(vh->sha256));
cd14cc10 246 hexdump(sha256_ctx.buf, sizeof(sha256));
9fd18969 247 return EILSEQ;
cd14cc10
JA
248 }
249
250 return 0;
251}
252
936216f8 253static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
7c353ceb 254{
936216f8 255 void *p = io_u_verify_off(hdr, vc);
7c353ceb
JA
256 struct vhdr_sha1 *vh = hdr_priv(hdr);
257 uint32_t sha1[5];
258 struct sha1_ctx sha1_ctx = {
259 .H = sha1,
260 };
261
936216f8 262 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
7c353ceb
JA
263
264 sha1_init(&sha1_ctx);
265 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
266
267 if (memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) {
268 log_err("sha1: verify failed at %llu/%u\n",
936216f8 269 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
7c353ceb
JA
270 hexdump(vh->sha1, sizeof(vh->sha1));
271 hexdump(sha1_ctx.H, sizeof(sha1));
272 return EILSEQ;
273 }
274
275 return 0;
276}
277
936216f8 278static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
1e154bdb 279{
936216f8 280 void *p = io_u_verify_off(hdr, vc);
546dfd9f 281 struct vhdr_crc7 *vh = hdr_priv(hdr);
1e154bdb
JA
282 unsigned char c;
283
936216f8 284 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 285
87677832 286 c = crc7(p, hdr->len - hdr_size(hdr));
1e154bdb 287
546dfd9f 288 if (c != vh->crc7) {
3f9f4e26 289 log_err("crc7: verify failed at %llu/%u\n",
936216f8 290 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
546dfd9f 291 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
9fd18969 292 return EILSEQ;
1e154bdb
JA
293 }
294
295 return 0;
296}
297
936216f8 298static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
969f7ed3 299{
936216f8 300 void *p = io_u_verify_off(hdr, vc);
546dfd9f 301 struct vhdr_crc16 *vh = hdr_priv(hdr);
969f7ed3
JA
302 unsigned short c;
303
936216f8 304 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 305
87677832 306 c = crc16(p, hdr->len - hdr_size(hdr));
969f7ed3 307
546dfd9f 308 if (c != vh->crc16) {
3f9f4e26 309 log_err("crc16: verify failed at %llu/%u\n",
936216f8 310 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
546dfd9f 311 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
9fd18969 312 return EILSEQ;
969f7ed3
JA
313 }
314
315 return 0;
316}
317
936216f8 318static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
d77a7af3 319{
936216f8 320 void *p = io_u_verify_off(hdr, vc);
546dfd9f 321 struct vhdr_crc64 *vh = hdr_priv(hdr);
d77a7af3
JA
322 unsigned long long c;
323
936216f8 324 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 325
87677832 326 c = crc64(p, hdr->len - hdr_size(hdr));
d77a7af3 327
546dfd9f 328 if (c != vh->crc64) {
3f9f4e26 329 log_err("crc64: verify failed at %llu/%u\n",
936216f8 330 vc->io_u->offset + vc->hdr_num * hdr->len,
3f9f4e26 331 hdr->len);
5ec10eaa
JA
332 log_err("crc64: wanted %llx, got %llx\n",
333 (unsigned long long) vh->crc64, c);
9fd18969 334 return EILSEQ;
d77a7af3
JA
335 }
336
337 return 0;
338}
339
936216f8 340static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
e29d1b70 341{
936216f8 342 void *p = io_u_verify_off(hdr, vc);
546dfd9f
JA
343 struct vhdr_crc32 *vh = hdr_priv(hdr);
344 uint32_t c;
e29d1b70 345
936216f8 346 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 347
87677832 348 c = crc32(p, hdr->len - hdr_size(hdr));
e29d1b70 349
546dfd9f 350 if (c != vh->crc32) {
3f9f4e26 351 log_err("crc32: verify failed at %llu/%u\n",
936216f8 352 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
546dfd9f 353 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
9fd18969 354 return EILSEQ;
e29d1b70
JA
355 }
356
357 return 0;
358}
359
936216f8 360static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
bac39e0e 361{
936216f8 362 void *p = io_u_verify_off(hdr, vc);
bac39e0e
JA
363 struct vhdr_crc32 *vh = hdr_priv(hdr);
364 uint32_t c;
365
936216f8 366 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
bac39e0e 367
3845591f
JA
368 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
369 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
370 else
371 c = crc32c(p, hdr->len - hdr_size(hdr));
bac39e0e
JA
372
373 if (c != vh->crc32) {
374 log_err("crc32c: verify failed at %llu/%u\n",
936216f8 375 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
bac39e0e 376 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
9fd18969 377 return EILSEQ;
bac39e0e
JA
378 }
379
380 return 0;
381}
382
936216f8 383static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
e29d1b70 384{
936216f8 385 void *p = io_u_verify_off(hdr, vc);
546dfd9f 386 struct vhdr_md5 *vh = hdr_priv(hdr);
8c432325
JA
387 uint32_t hash[MD5_HASH_WORDS];
388 struct md5_ctx md5_ctx = {
389 .hash = hash,
390 };
e29d1b70 391
936216f8 392 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 393
61f821f1 394 md5_init(&md5_ctx);
87677832 395 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
e29d1b70 396
546dfd9f 397 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
3f9f4e26 398 log_err("md5: verify failed at %llu/%u\n",
936216f8 399 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
546dfd9f 400 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
8c432325 401 hexdump(md5_ctx.hash, sizeof(hash));
9fd18969 402 return EILSEQ;
e29d1b70
JA
403 }
404
405 return 0;
406}
407
3f199b01
JA
408static unsigned int hweight8(unsigned int w)
409{
410 unsigned int res = w - ((w >> 1) & 0x55);
411
412 res = (res & 0x33) + ((res >> 2) & 0x33);
413 return (res + (res >> 4)) & 0x0F;
414}
415
0e92f873 416int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
5ec10eaa 417 char *buf, unsigned int len, unsigned int mod)
a944e335
SL
418{
419 unsigned int i;
a944e335
SL
420
421 for (i = 0; i < len; i++) {
0e92f873 422 if (buf[i] != pattern[mod]) {
3f199b01
JA
423 unsigned int bits;
424
0e92f873 425 bits = hweight8(buf[i] ^ pattern[mod]);
3f199b01 426 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
0e92f873 427 buf[i], pattern[mod], bits);
3f199b01 428 log_err("fio: bad pattern block offset %u\n", i);
9fd18969 429 return EILSEQ;
3f199b01 430 }
a944e335
SL
431 mod++;
432 if (mod == pattern_size)
433 mod = 0;
434 }
435
436 return 0;
437}
438
e8462bd8
JA
439/*
440 * Push IO verification to a separate thread
441 */
442int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
443{
444 if (io_u->file)
445 put_file_log(td, io_u->file);
446
447 io_u->file = NULL;
448
449 pthread_mutex_lock(&td->io_u_lock);
0c41214f
RR
450
451 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
452 td->cur_depth--;
453 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
454 }
e8462bd8
JA
455 flist_del(&io_u->list);
456 flist_add_tail(&io_u->list, &td->verify_list);
2ecc1b57 457 io_u->flags |= IO_U_F_FREE_DEF;
e8462bd8
JA
458 pthread_mutex_unlock(&td->io_u_lock);
459
460 pthread_cond_signal(&td->verify_cond);
e8462bd8
JA
461 return 0;
462}
463
36690c9b 464int verify_io_u(struct thread_data *td, struct io_u *io_u)
e29d1b70 465{
3f9f4e26 466 struct verify_header *hdr;
a944e335 467 unsigned int hdr_size, hdr_inc, hdr_num = 0;
95646108 468 void *p;
e29d1b70
JA
469 int ret;
470
1dcc0498 471 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b
JA
472 return 0;
473
3f9f4e26 474 hdr_inc = io_u->buflen;
a59e170d
JA
475 if (td->o.verify_interval)
476 hdr_inc = td->o.verify_interval;
e29d1b70 477
a12a3b4d 478 ret = 0;
5ec10eaa
JA
479 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
480 p += hdr_inc, hdr_num++) {
936216f8
JA
481 struct vcont vc;
482
a12a3b4d
JA
483 if (ret && td->o.verify_fatal) {
484 td->terminate = 1;
485 break;
486 }
a944e335 487 hdr_size = __hdr_size(td->o.verify);
a59e170d 488 if (td->o.verify_offset)
a944e335 489 memswp(p, p + td->o.verify_offset, hdr_size);
95646108 490 hdr = p;
e29d1b70 491
936216f8
JA
492 vc.io_u = io_u;
493 vc.hdr_num = hdr_num;
494
3f199b01 495 if (hdr->fio_magic != FIO_HDR_MAGIC) {
b7a3e661
RR
496 log_err("Bad verify header %x at %llu\n",
497 hdr->fio_magic,
498 io_u->offset + hdr_num * hdr->len);
9fd18969 499 return EILSEQ;
3f199b01
JA
500 }
501
e28218f3 502 if (td->o.verify_pattern_bytes) {
bd6f78b2
JA
503 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
504 io_u, hdr->len);
e28218f3 505 ret = verify_io_u_pattern(td->o.verify_pattern,
0e92f873
RR
506 td->o.verify_pattern_bytes,
507 p + hdr_size,
508 hdr_inc - hdr_size,
509 hdr_size % td->o.verify_pattern_bytes);
e92d3d71
RR
510 /*
511 * Also verify the meta data, if applicable
512 */
513 if (hdr->verify_type == VERIFY_META)
936216f8 514 ret |= verify_io_u_meta(hdr, td, &vc);
e92d3d71 515
e28218f3
SL
516 if (ret)
517 log_err("fio: verify failed at %llu/%u\n",
518 io_u->offset + hdr_num * hdr->len,
519 hdr->len);
520 continue;
521 }
522
3f9f4e26
SL
523 switch (hdr->verify_type) {
524 case VERIFY_MD5:
936216f8 525 ret = verify_io_u_md5(hdr, &vc);
3f9f4e26
SL
526 break;
527 case VERIFY_CRC64:
936216f8 528 ret = verify_io_u_crc64(hdr, &vc);
3f9f4e26 529 break;
bac39e0e 530 case VERIFY_CRC32C:
3845591f 531 case VERIFY_CRC32C_INTEL:
936216f8 532 ret = verify_io_u_crc32c(hdr, &vc);
bac39e0e 533 break;
3f9f4e26 534 case VERIFY_CRC32:
936216f8 535 ret = verify_io_u_crc32(hdr, &vc);
3f9f4e26
SL
536 break;
537 case VERIFY_CRC16:
936216f8 538 ret = verify_io_u_crc16(hdr, &vc);
3f9f4e26
SL
539 break;
540 case VERIFY_CRC7:
936216f8 541 ret = verify_io_u_crc7(hdr, &vc);
3f9f4e26 542 break;
cd14cc10 543 case VERIFY_SHA256:
936216f8 544 ret = verify_io_u_sha256(hdr, &vc);
cd14cc10
JA
545 break;
546 case VERIFY_SHA512:
936216f8 547 ret = verify_io_u_sha512(hdr, &vc);
cd14cc10 548 break;
7437ee87 549 case VERIFY_META:
936216f8 550 ret = verify_io_u_meta(hdr, td, &vc);
7437ee87 551 break;
7c353ceb 552 case VERIFY_SHA1:
936216f8 553 ret = verify_io_u_sha1(hdr, &vc);
7c353ceb 554 break;
3f9f4e26
SL
555 default:
556 log_err("Bad verify type %u\n", hdr->verify_type);
d16d4e09 557 ret = EINVAL;
3f9f4e26 558 }
3f9f4e26 559 }
a7dfe862 560
a12a3b4d 561 return ret;
e29d1b70
JA
562}
563
7437ee87 564static void fill_meta(struct verify_header *hdr, struct thread_data *td,
5ec10eaa 565 struct io_u *io_u, unsigned int header_num)
7437ee87
SL
566{
567 struct vhdr_meta *vh = hdr_priv(hdr);
568
569 vh->thread = td->thread_number;
570
571 vh->time_sec = io_u->start_time.tv_sec;
572 vh->time_usec = io_u->start_time.tv_usec;
573
574 vh->numberio = td->io_issues[DDIR_WRITE];
575
576 vh->offset = io_u->offset + header_num * td->o.verify_interval;
577}
578
cd14cc10
JA
579static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
580{
546dfd9f 581 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10 582 struct sha512_ctx sha512_ctx = {
546dfd9f 583 .buf = vh->sha512,
cd14cc10
JA
584 };
585
586 sha512_init(&sha512_ctx);
587 sha512_update(&sha512_ctx, p, len);
588}
589
590static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
591{
546dfd9f 592 struct vhdr_sha256 *vh = hdr_priv(hdr);
cd14cc10 593 struct sha256_ctx sha256_ctx = {
546dfd9f 594 .buf = vh->sha256,
cd14cc10
JA
595 };
596
597 sha256_init(&sha256_ctx);
598 sha256_update(&sha256_ctx, p, len);
599}
600
7c353ceb
JA
601static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
602{
603 struct vhdr_sha1 *vh = hdr_priv(hdr);
604 struct sha1_ctx sha1_ctx = {
605 .H = vh->sha1,
606 };
607
608 sha1_init(&sha1_ctx);
609 sha1_update(&sha1_ctx, p, len);
610}
611
1e154bdb
JA
612static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
613{
546dfd9f
JA
614 struct vhdr_crc7 *vh = hdr_priv(hdr);
615
616 vh->crc7 = crc7(p, len);
1e154bdb
JA
617}
618
969f7ed3
JA
619static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
620{
546dfd9f
JA
621 struct vhdr_crc16 *vh = hdr_priv(hdr);
622
623 vh->crc16 = crc16(p, len);
969f7ed3
JA
624}
625
e29d1b70
JA
626static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
627{
546dfd9f
JA
628 struct vhdr_crc32 *vh = hdr_priv(hdr);
629
630 vh->crc32 = crc32(p, len);
e29d1b70
JA
631}
632
bac39e0e
JA
633static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
634{
635 struct vhdr_crc32 *vh = hdr_priv(hdr);
636
3845591f
JA
637 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
638 vh->crc32 = crc32c_intel(p, len);
639 else
640 vh->crc32 = crc32c(p, len);
bac39e0e
JA
641}
642
d77a7af3
JA
643static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
644{
546dfd9f
JA
645 struct vhdr_crc64 *vh = hdr_priv(hdr);
646
647 vh->crc64 = crc64(p, len);
d77a7af3
JA
648}
649
e29d1b70
JA
650static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
651{
546dfd9f 652 struct vhdr_md5 *vh = hdr_priv(hdr);
8c432325 653 struct md5_ctx md5_ctx = {
546dfd9f 654 .hash = (uint32_t *) vh->md5_digest,
8c432325 655 };
e29d1b70 656
61f821f1 657 md5_init(&md5_ctx);
e29d1b70 658 md5_update(&md5_ctx, p, len);
e29d1b70
JA
659}
660
661/*
662 * fill body of io_u->buf with random data and add a header with the
663 * crc32 or md5 sum of that data.
664 */
665void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
666{
baefa9be 667 struct verify_header *hdr;
95646108 668 void *p = io_u->buf, *data;
7437ee87 669 unsigned int hdr_inc, data_len, header_num = 0;
e29d1b70 670
9cc3d150
JA
671 if (td->o.verify == VERIFY_NULL)
672 return;
673
90059d65 674 fill_pattern(td, p, io_u->buflen);
546a9142
SL
675
676 hdr_inc = io_u->buflen;
a59e170d
JA
677 if (td->o.verify_interval)
678 hdr_inc = td->o.verify_interval;
baefa9be 679
5ec10eaa 680 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
95646108 681 hdr = p;
3f9f4e26
SL
682
683 hdr->fio_magic = FIO_HDR_MAGIC;
684 hdr->verify_type = td->o.verify;
546a9142 685 hdr->len = hdr_inc;
87677832 686 data_len = hdr_inc - hdr_size(hdr);
3f9f4e26 687
87677832 688 data = p + hdr_size(hdr);
3f9f4e26
SL
689 switch (td->o.verify) {
690 case VERIFY_MD5:
bd6f78b2
JA
691 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
692 io_u, hdr->len);
3f9f4e26
SL
693 fill_md5(hdr, data, data_len);
694 break;
695 case VERIFY_CRC64:
bd6f78b2
JA
696 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
697 io_u, hdr->len);
3f9f4e26
SL
698 fill_crc64(hdr, data, data_len);
699 break;
bac39e0e 700 case VERIFY_CRC32C:
3845591f 701 case VERIFY_CRC32C_INTEL:
bac39e0e
JA
702 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
703 io_u, hdr->len);
704 fill_crc32c(hdr, data, data_len);
705 break;
3f9f4e26 706 case VERIFY_CRC32:
bd6f78b2
JA
707 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
708 io_u, hdr->len);
3f9f4e26
SL
709 fill_crc32(hdr, data, data_len);
710 break;
711 case VERIFY_CRC16:
bd6f78b2
JA
712 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
713 io_u, hdr->len);
3f9f4e26
SL
714 fill_crc16(hdr, data, data_len);
715 break;
716 case VERIFY_CRC7:
bd6f78b2
JA
717 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
718 io_u, hdr->len);
3f9f4e26
SL
719 fill_crc7(hdr, data, data_len);
720 break;
cd14cc10 721 case VERIFY_SHA256:
bd6f78b2
JA
722 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
723 io_u, hdr->len);
cd14cc10
JA
724 fill_sha256(hdr, data, data_len);
725 break;
726 case VERIFY_SHA512:
bd6f78b2
JA
727 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
728 io_u, hdr->len);
cd14cc10
JA
729 fill_sha512(hdr, data, data_len);
730 break;
7437ee87 731 case VERIFY_META:
bd6f78b2
JA
732 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
733 io_u, hdr->len);
7437ee87
SL
734 fill_meta(hdr, td, io_u, header_num);
735 break;
7c353ceb
JA
736 case VERIFY_SHA1:
737 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
738 io_u, hdr->len);
739 fill_sha1(hdr, data, data_len);
740 break;
3f9f4e26
SL
741 default:
742 log_err("fio: bad verify type: %d\n", td->o.verify);
743 assert(0);
744 }
a59e170d 745 if (td->o.verify_offset)
87677832 746 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
7437ee87 747 header_num++;
e29d1b70 748 }
e29d1b70
JA
749}
750
751int get_next_verify(struct thread_data *td, struct io_u *io_u)
752{
8de8f047 753 struct io_piece *ipo = NULL;
e29d1b70 754
d2d7fa53
JA
755 /*
756 * this io_u is from a requeue, we already filled the offsets
757 */
758 if (io_u->file)
759 return 0;
760
8de8f047
JA
761 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
762 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 763
8de8f047 764 ipo = rb_entry(n, struct io_piece, rb_node);
4b87898e 765 rb_erase(n, &td->io_hist_tree);
9e144189 766 td->io_hist_len--;
01743ee1
JA
767 } else if (!flist_empty(&td->io_hist_list)) {
768 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
9e144189 769 td->io_hist_len--;
01743ee1 770 flist_del(&ipo->list);
8de8f047 771 }
e29d1b70 772
8de8f047 773 if (ipo) {
e29d1b70
JA
774 io_u->offset = ipo->offset;
775 io_u->buflen = ipo->len;
36167d82 776 io_u->file = ipo->file;
97af62ce 777
d6aed795 778 if (!fio_file_open(io_u->file)) {
97af62ce
JA
779 int r = td_io_open_file(td, io_u->file);
780
bd6f78b2
JA
781 if (r) {
782 dprint(FD_VERIFY, "failed file %s open\n",
783 io_u->file->file_name);
97af62ce 784 return 1;
bd6f78b2 785 }
97af62ce
JA
786 }
787
788 get_file(ipo->file);
d6aed795 789 assert(fio_file_open(io_u->file));
e29d1b70 790 io_u->ddir = DDIR_READ;
36167d82
JA
791 io_u->xfer_buf = io_u->buf;
792 io_u->xfer_buflen = io_u->buflen;
e29d1b70 793 free(ipo);
bd6f78b2 794 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
e29d1b70
JA
795 return 0;
796 }
797
bd6f78b2 798 dprint(FD_VERIFY, "get_next_verify: empty\n");
e29d1b70
JA
799 return 1;
800}
e8462bd8
JA
801
802static void *verify_async_thread(void *data)
803{
804 struct thread_data *td = data;
805 struct io_u *io_u;
806 int ret = 0;
807
808 if (td->o.verify_cpumask_set &&
809 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
810 log_err("fio: failed setting verify thread affinity\n");
811 goto done;
812 }
813
814 do {
e53ab27c
JA
815 FLIST_HEAD(list);
816
e8462bd8
JA
817 read_barrier();
818 if (td->verify_thread_exit)
819 break;
820
821 pthread_mutex_lock(&td->io_u_lock);
822
823 while (flist_empty(&td->verify_list) &&
824 !td->verify_thread_exit) {
b36e298b
JA
825 ret = pthread_cond_wait(&td->verify_cond,
826 &td->io_u_lock);
e8462bd8
JA
827 if (ret) {
828 pthread_mutex_unlock(&td->io_u_lock);
829 break;
830 }
831 }
832
e53ab27c
JA
833 flist_splice_init(&td->verify_list, &list);
834 pthread_mutex_unlock(&td->io_u_lock);
835
836 if (flist_empty(&list))
e8462bd8 837 continue;
e8462bd8 838
e53ab27c
JA
839 while (!flist_empty(&list)) {
840 io_u = flist_entry(list.next, struct io_u, list);
841 flist_del_init(&io_u->list);
e8462bd8 842
d561f2ab 843 ret = verify_io_u(td, io_u);
e53ab27c 844 put_io_u(td, io_u);
d561f2ab
JA
845 if (!ret)
846 continue;
847 if (td->o.continue_on_error &&
848 td_non_fatal_error(ret)) {
849 update_error_count(td, ret);
850 td_clear_error(td);
851 ret = 0;
852 }
e53ab27c 853 }
e8462bd8
JA
854 } while (!ret);
855
d561f2ab
JA
856 if (ret) {
857 td_verror(td, ret, "async_verify");
858 td->terminate = 1;
859 }
860
e8462bd8
JA
861done:
862 pthread_mutex_lock(&td->io_u_lock);
863 td->nr_verify_threads--;
864 pthread_mutex_unlock(&td->io_u_lock);
865
866 pthread_cond_signal(&td->free_cond);
867 return NULL;
868}
869
870int verify_async_init(struct thread_data *td)
871{
872 int i, ret;
873
874 td->verify_thread_exit = 0;
875
876 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
877 for (i = 0; i < td->o.verify_async; i++) {
878 ret = pthread_create(&td->verify_threads[i], NULL,
879 verify_async_thread, td);
880 if (ret) {
881 log_err("fio: async verify creation failed: %s\n",
882 strerror(ret));
883 break;
884 }
885 ret = pthread_detach(td->verify_threads[i]);
886 if (ret) {
887 log_err("fio: async verify thread detach failed: %s\n",
888 strerror(ret));
889 break;
890 }
891 td->nr_verify_threads++;
892 }
893
894 if (i != td->o.verify_async) {
e40823b1 895 log_err("fio: only %d verify threads started, exiting\n", i);
e8462bd8
JA
896 td->verify_thread_exit = 1;
897 write_barrier();
898 pthread_cond_broadcast(&td->verify_cond);
899 return 1;
900 }
901
902 return 0;
903}
904
905void verify_async_exit(struct thread_data *td)
906{
907 td->verify_thread_exit = 1;
908 write_barrier();
909 pthread_cond_broadcast(&td->verify_cond);
910
911 pthread_mutex_lock(&td->io_u_lock);
912
913 while (td->nr_verify_threads)
914 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
915
916 pthread_mutex_unlock(&td->io_u_lock);
917 free(td->verify_threads);
918 td->verify_threads = NULL;
919}