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