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