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