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