crc: add test.h header to test.c
[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>
79402a12 9#include <libgen.h>
e29d1b70
JA
10
11#include "fio.h"
4f5af7b2 12#include "verify.h"
0d29de83 13#include "trim.h"
637ef8d9 14#include "lib/rand.h"
51aa2da8 15#include "lib/hweight.h"
e29d1b70 16
eef6eea1
JA
17#include "crc/md5.h"
18#include "crc/crc64.h"
19#include "crc/crc32.h"
bac39e0e 20#include "crc/crc32c.h"
eef6eea1
JA
21#include "crc/crc16.h"
22#include "crc/crc7.h"
23#include "crc/sha256.h"
24#include "crc/sha512.h"
7c353ceb 25#include "crc/sha1.h"
844ea602 26#include "crc/xxhash.h"
cd14cc10 27
7d9fb455
JA
28static void populate_hdr(struct thread_data *td, struct io_u *io_u,
29 struct verify_header *hdr, unsigned int header_num,
30 unsigned int header_len);
31
ce35b1ec
JA
32static void fill_pattern(struct thread_data *td, void *p, unsigned int len,
33 char *pattern, unsigned int pattern_bytes)
90059d65 34{
ce35b1ec 35 switch (pattern_bytes) {
90059d65 36 case 0:
ce35b1ec 37 assert(0);
90059d65
JA
38 break;
39 case 1:
bd6f78b2 40 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
ce35b1ec 41 memset(p, pattern[0], len);
90059d65 42 break;
0e92f873
RR
43 default: {
44 unsigned int i = 0, size = 0;
90059d65
JA
45 unsigned char *b = p;
46
bd6f78b2 47 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
ce35b1ec 48 pattern_bytes, len);
bd6f78b2 49
90059d65 50 while (i < len) {
ce35b1ec 51 size = pattern_bytes;
0e92f873
RR
52 if (size > (len - i))
53 size = len - i;
ce35b1ec 54 memcpy(b+i, pattern, size);
0e92f873 55 i += size;
90059d65
JA
56 }
57 break;
58 }
59 }
60}
61
ce35b1ec
JA
62void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
63{
64 fill_pattern(td, p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
65}
66
67void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
68 struct io_u *io_u, unsigned long seed, int use_seed)
69{
70 if (!td->o.verify_pattern_bytes) {
71 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
72
73 if (use_seed)
74 __fill_random_buf(p, len, seed);
75 else
c4b6117b 76 io_u->rand_seed = fill_random_buf(&td->__verify_state, p, len);
ce35b1ec
JA
77 return;
78 }
c4b6117b 79
ce35b1ec
JA
80 if (io_u->buf_filled_len >= len) {
81 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
82 td->o.verify_pattern_bytes, len);
83 return;
84 }
85
86 fill_pattern(td, p, len, td->o.verify_pattern, td->o.verify_pattern_bytes);
87
88 io_u->buf_filled_len = len;
89}
90
cfbcd0dc
JA
91static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
92{
93 unsigned int hdr_inc;
94
95 hdr_inc = io_u->buflen;
8a99fdf6 96 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
cfbcd0dc
JA
97 hdr_inc = td->o.verify_interval;
98
99 return hdr_inc;
100}
101
c50ca7bd
JA
102static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
103 unsigned long seed, int use_seed)
104{
105 unsigned int hdr_inc, header_num;
106 struct verify_header *hdr;
107 void *p = io_u->buf;
108
ce35b1ec 109 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
c50ca7bd 110
cfbcd0dc 111 hdr_inc = get_hdr_inc(td, io_u);
c50ca7bd
JA
112 header_num = 0;
113 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
114 hdr = p;
115 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
116 header_num++;
117 }
118}
119
4764aec9 120static void memswp(void *buf1, void *buf2, unsigned int len)
546a9142 121{
dee6de74
SL
122 char swap[200];
123
124 assert(len <= sizeof(swap));
90059d65 125
546a9142
SL
126 memcpy(&swap, buf1, len);
127 memcpy(buf1, buf2, len);
128 memcpy(buf2, &swap, len);
129}
130
e29d1b70
JA
131static void hexdump(void *buffer, int len)
132{
133 unsigned char *p = buffer;
134 int i;
135
136 for (i = 0; i < len; i++)
bfacf389
JA
137 log_err("%02x", p[i]);
138 log_err("\n");
e29d1b70
JA
139}
140
87677832 141/*
de8f6de9 142 * Prepare for separation of verify_header and checksum header
87677832 143 */
546dfd9f 144static inline unsigned int __hdr_size(int verify_type)
87677832 145{
03e20d68 146 unsigned int len = 0;
87677832 147
546dfd9f
JA
148 switch (verify_type) {
149 case VERIFY_NONE:
150 case VERIFY_NULL:
151 len = 0;
152 break;
153 case VERIFY_MD5:
154 len = sizeof(struct vhdr_md5);
155 break;
156 case VERIFY_CRC64:
157 len = sizeof(struct vhdr_crc64);
158 break;
bac39e0e 159 case VERIFY_CRC32C:
546dfd9f 160 case VERIFY_CRC32:
3845591f 161 case VERIFY_CRC32C_INTEL:
546dfd9f
JA
162 len = sizeof(struct vhdr_crc32);
163 break;
164 case VERIFY_CRC16:
165 len = sizeof(struct vhdr_crc16);
166 break;
167 case VERIFY_CRC7:
168 len = sizeof(struct vhdr_crc7);
169 break;
170 case VERIFY_SHA256:
171 len = sizeof(struct vhdr_sha256);
172 break;
173 case VERIFY_SHA512:
174 len = sizeof(struct vhdr_sha512);
175 break;
844ea602
JA
176 case VERIFY_XXHASH:
177 len = sizeof(struct vhdr_xxhash);
178 break;
7437ee87
SL
179 case VERIFY_META:
180 len = sizeof(struct vhdr_meta);
181 break;
7c353ceb
JA
182 case VERIFY_SHA1:
183 len = sizeof(struct vhdr_sha1);
184 break;
92bf48d5
JA
185 case VERIFY_PATTERN:
186 len = 0;
187 break;
546dfd9f
JA
188 default:
189 log_err("fio: unknown verify header!\n");
190 assert(0);
191 }
192
193 return len + sizeof(struct verify_header);
87677832
JA
194}
195
196static inline unsigned int hdr_size(struct verify_header *hdr)
197{
546dfd9f
JA
198 return __hdr_size(hdr->verify_type);
199}
200
201static void *hdr_priv(struct verify_header *hdr)
202{
203 void *priv = hdr;
204
205 return priv + sizeof(struct verify_header);
87677832
JA
206}
207
936216f8
JA
208/*
209 * Verify container, pass info to verify handlers and allow them to
210 * pass info back in case of error
211 */
212struct vcont {
213 /*
214 * Input
215 */
216 struct io_u *io_u;
217 unsigned int hdr_num;
7d9fb455 218 struct thread_data *td;
936216f8
JA
219
220 /*
221 * Output, only valid in case of error
222 */
bfacf389
JA
223 const char *name;
224 void *good_crc;
225 void *bad_crc;
936216f8
JA
226 unsigned int crc_len;
227};
228
c50ca7bd
JA
229static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
230 const char *type, struct fio_file *f)
7d9fb455 231{
6f260b31 232 char *ptr, fname[256];
7d9fb455
JA
233 int ret, fd;
234
6f260b31
JA
235 ptr = strdup(f->file_name);
236 strcpy(fname, basename(ptr));
c50ca7bd
JA
237
238 sprintf(fname + strlen(fname), ".%llu.%s", offset, type);
7d9fb455
JA
239
240 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
241 if (fd < 0) {
242 perror("open verify buf file");
243 return;
244 }
245
246 while (len) {
247 ret = write(fd, buf, len);
248 if (!ret)
249 break;
250 else if (ret < 0) {
251 perror("write verify buf file");
252 break;
253 }
254 len -= ret;
255 buf += ret;
256 }
257
258 close(fd);
c50ca7bd 259 log_err(" %s data dumped as %s\n", type, fname);
6f260b31 260 free(ptr);
7d9fb455
JA
261}
262
c50ca7bd
JA
263/*
264 * Dump the contents of the read block and re-generate the correct data
265 * and dump that too.
266 */
7d9fb455
JA
267static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
268{
269 struct thread_data *td = vc->td;
270 struct io_u *io_u = vc->io_u;
271 unsigned long hdr_offset;
7d9fb455 272 struct io_u dummy;
c50ca7bd 273 void *buf;
7d9fb455 274
0dce9bc9
SL
275 if (!td->o.verify_dump)
276 return;
277
c50ca7bd
JA
278 /*
279 * Dump the contents we just read off disk
280 */
7d9fb455
JA
281 hdr_offset = vc->hdr_num * hdr->len;
282
c50ca7bd
JA
283 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
284 "received", vc->io_u->file);
7d9fb455 285
c50ca7bd
JA
286 /*
287 * Allocate a new buf and re-generate the original data
288 */
289 buf = malloc(io_u->buflen);
7d9fb455 290 dummy = *io_u;
c50ca7bd 291 dummy.buf = buf;
4aae38a6 292 dummy.rand_seed = hdr->rand_seed;
cfbcd0dc 293 dummy.buf_filled_len = 0;
0d81daf9 294 dummy.buflen = io_u->buflen;
7d9fb455 295
c50ca7bd 296 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
7d9fb455 297
c50ca7bd
JA
298 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
299 "expected", vc->io_u->file);
7d9fb455
JA
300 free(buf);
301}
302
bfacf389
JA
303static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
304{
305 unsigned long long offset;
306
307 offset = vc->io_u->offset;
308 offset += vc->hdr_num * hdr->len;
32c17adf
JA
309 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
310 vc->name, vc->io_u->file->file_name, offset, hdr->len);
bfacf389
JA
311
312 if (vc->good_crc && vc->bad_crc) {
313 log_err(" Expected CRC: ");
314 hexdump(vc->good_crc, vc->crc_len);
315 log_err(" Received CRC: ");
316 hexdump(vc->bad_crc, vc->crc_len);
317 }
7d9fb455
JA
318
319 dump_verify_buffers(hdr, vc);
bfacf389
JA
320}
321
d9f2caf3
JA
322/*
323 * Return data area 'header_num'
324 */
936216f8 325static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
d9f2caf3 326{
936216f8 327 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
d9f2caf3
JA
328}
329
92bf48d5
JA
330static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
331{
332 struct thread_data *td = vc->td;
333 struct io_u *io_u = vc->io_u;
334 char *buf, *pattern;
2b13e716 335 unsigned int header_size = __hdr_size(td->o.verify);
9a2a86d0 336 unsigned int len, mod, i, size, pattern_size;
92bf48d5
JA
337
338 pattern = td->o.verify_pattern;
9a2a86d0
SL
339 pattern_size = td->o.verify_pattern_bytes;
340 if (pattern_size <= 1)
341 pattern_size = MAX_PATTERN_SIZE;
2b13e716
JA
342 buf = (void *) hdr + header_size;
343 len = get_hdr_inc(td, io_u) - header_size;
9a2a86d0
SL
344 mod = header_size % pattern_size;
345
346 for (i = 0; i < len; i += size) {
347 size = pattern_size - mod;
348 if (size > (len - i))
349 size = len - i;
350 if (memcmp(buf + i, pattern + mod, size))
e4ad68b1 351 /* Let the slow compare find the first mismatch byte. */
9a2a86d0
SL
352 break;
353 mod = 0;
354 }
92bf48d5 355
9a2a86d0 356 for (; i < len; i++) {
92bf48d5
JA
357 if (buf[i] != pattern[mod]) {
358 unsigned int bits;
359
360 bits = hweight8(buf[i] ^ pattern[mod]);
361 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
362 buf[i], pattern[mod], bits);
363 log_err("fio: bad pattern block offset %u\n", i);
364 dump_verify_buffers(hdr, vc);
365 return EILSEQ;
366 }
367 mod++;
368 if (mod == td->o.verify_pattern_bytes)
369 mod = 0;
370 }
371
372 return 0;
373}
374
375static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
7437ee87 376{
92bf48d5 377 struct thread_data *td = vc->td;
7437ee87 378 struct vhdr_meta *vh = hdr_priv(hdr);
936216f8 379 struct io_u *io_u = vc->io_u;
92bf48d5 380 int ret = EILSEQ;
7437ee87 381
bd6f78b2
JA
382 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
383
bfacf389 384 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
92bf48d5
JA
385 ret = 0;
386
387 if (td->o.verify_pattern_bytes)
388 ret |= verify_io_u_pattern(hdr, vc);
389
da0a7bd2
JC
390 /*
391 * For read-only workloads, the program cannot be certain of the
392 * last numberio written to a block. Checking of numberio will be done
393 * only for workloads that write data.
62167762
JC
394 * For verify_only, numberio will be checked in the last iteration when
395 * the correct state of numberio, that would have been written to each
396 * block in a previous run of fio, has been reached.
da0a7bd2
JC
397 */
398 if (td_write(td) || td_rw(td))
62167762
JC
399 if (!td->o.verify_only || td->o.loops == 0)
400 if (vh->numberio != io_u->numberio)
401 ret = EILSEQ;
da0a7bd2 402
92bf48d5 403 if (!ret)
bfacf389 404 return 0;
7437ee87 405
bfacf389
JA
406 vc->name = "meta";
407 log_verify_failure(hdr, vc);
92bf48d5 408 return ret;
7437ee87
SL
409}
410
844ea602
JA
411static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
412{
413 void *p = io_u_verify_off(hdr, vc);
414 struct vhdr_xxhash *vh = hdr_priv(hdr);
415 uint32_t hash;
416 void *state;
417
418 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
419
420 state = XXH32_init(1);
421 XXH32_update(state, p, hdr->len - hdr_size(hdr));
422 hash = XXH32_digest(state);
423
424 if (vh->hash == hash)
425 return 0;
426
427 vc->name = "xxhash";
428 vc->good_crc = &vh->hash;
429 vc->bad_crc = &hash;
430 vc->crc_len = sizeof(hash);
431 log_verify_failure(hdr, vc);
432 return EILSEQ;
433}
434
936216f8 435static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
cd14cc10 436{
936216f8 437 void *p = io_u_verify_off(hdr, vc);
546dfd9f 438 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10 439 uint8_t sha512[128];
25dfa848 440 struct fio_sha512_ctx sha512_ctx = {
cd14cc10
JA
441 .buf = sha512,
442 };
443
936216f8 444 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 445
25dfa848
JA
446 fio_sha512_init(&sha512_ctx);
447 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
cd14cc10 448
bfacf389
JA
449 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
450 return 0;
cd14cc10 451
bfacf389
JA
452 vc->name = "sha512";
453 vc->good_crc = vh->sha512;
454 vc->bad_crc = sha512_ctx.buf;
455 vc->crc_len = sizeof(vh->sha512);
456 log_verify_failure(hdr, vc);
457 return EILSEQ;
cd14cc10
JA
458}
459
936216f8 460static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
cd14cc10 461{
936216f8 462 void *p = io_u_verify_off(hdr, vc);
546dfd9f 463 struct vhdr_sha256 *vh = hdr_priv(hdr);
bc77f56f 464 uint8_t sha256[64];
25dfa848 465 struct fio_sha256_ctx sha256_ctx = {
cd14cc10
JA
466 .buf = sha256,
467 };
468
936216f8 469 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 470
25dfa848
JA
471 fio_sha256_init(&sha256_ctx);
472 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
cd14cc10 473
bfacf389
JA
474 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
475 return 0;
cd14cc10 476
bfacf389
JA
477 vc->name = "sha256";
478 vc->good_crc = vh->sha256;
479 vc->bad_crc = sha256_ctx.buf;
480 vc->crc_len = sizeof(vh->sha256);
481 log_verify_failure(hdr, vc);
482 return EILSEQ;
cd14cc10
JA
483}
484
936216f8 485static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
7c353ceb 486{
936216f8 487 void *p = io_u_verify_off(hdr, vc);
7c353ceb
JA
488 struct vhdr_sha1 *vh = hdr_priv(hdr);
489 uint32_t sha1[5];
25dfa848 490 struct fio_sha1_ctx sha1_ctx = {
7c353ceb
JA
491 .H = sha1,
492 };
493
936216f8 494 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
7c353ceb 495
25dfa848
JA
496 fio_sha1_init(&sha1_ctx);
497 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
7c353ceb 498
bfacf389
JA
499 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
500 return 0;
7c353ceb 501
bfacf389
JA
502 vc->name = "sha1";
503 vc->good_crc = vh->sha1;
504 vc->bad_crc = sha1_ctx.H;
505 vc->crc_len = sizeof(vh->sha1);
506 log_verify_failure(hdr, vc);
507 return EILSEQ;
7c353ceb
JA
508}
509
936216f8 510static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
1e154bdb 511{
936216f8 512 void *p = io_u_verify_off(hdr, vc);
546dfd9f 513 struct vhdr_crc7 *vh = hdr_priv(hdr);
1e154bdb
JA
514 unsigned char c;
515
936216f8 516 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 517
25dfa848 518 c = fio_crc7(p, hdr->len - hdr_size(hdr));
1e154bdb 519
bfacf389
JA
520 if (c == vh->crc7)
521 return 0;
1e154bdb 522
bfacf389
JA
523 vc->name = "crc7";
524 vc->good_crc = &vh->crc7;
525 vc->bad_crc = &c;
526 vc->crc_len = 1;
527 log_verify_failure(hdr, vc);
528 return EILSEQ;
1e154bdb
JA
529}
530
936216f8 531static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
969f7ed3 532{
936216f8 533 void *p = io_u_verify_off(hdr, vc);
546dfd9f 534 struct vhdr_crc16 *vh = hdr_priv(hdr);
969f7ed3
JA
535 unsigned short c;
536
936216f8 537 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 538
25dfa848 539 c = fio_crc16(p, hdr->len - hdr_size(hdr));
969f7ed3 540
bfacf389
JA
541 if (c == vh->crc16)
542 return 0;
969f7ed3 543
bfacf389
JA
544 vc->name = "crc16";
545 vc->good_crc = &vh->crc16;
546 vc->bad_crc = &c;
547 vc->crc_len = 2;
548 log_verify_failure(hdr, vc);
549 return EILSEQ;
969f7ed3
JA
550}
551
936216f8 552static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
d77a7af3 553{
936216f8 554 void *p = io_u_verify_off(hdr, vc);
546dfd9f 555 struct vhdr_crc64 *vh = hdr_priv(hdr);
d77a7af3
JA
556 unsigned long long c;
557
936216f8 558 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 559
25dfa848 560 c = fio_crc64(p, hdr->len - hdr_size(hdr));
d77a7af3 561
bfacf389
JA
562 if (c == vh->crc64)
563 return 0;
d77a7af3 564
bfacf389
JA
565 vc->name = "crc64";
566 vc->good_crc = &vh->crc64;
567 vc->bad_crc = &c;
568 vc->crc_len = 8;
569 log_verify_failure(hdr, vc);
570 return EILSEQ;
d77a7af3
JA
571}
572
936216f8 573static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
e29d1b70 574{
936216f8 575 void *p = io_u_verify_off(hdr, vc);
546dfd9f
JA
576 struct vhdr_crc32 *vh = hdr_priv(hdr);
577 uint32_t c;
e29d1b70 578
936216f8 579 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 580
25dfa848 581 c = fio_crc32(p, hdr->len - hdr_size(hdr));
e29d1b70 582
bfacf389
JA
583 if (c == vh->crc32)
584 return 0;
e29d1b70 585
bfacf389
JA
586 vc->name = "crc32";
587 vc->good_crc = &vh->crc32;
588 vc->bad_crc = &c;
589 vc->crc_len = 4;
590 log_verify_failure(hdr, vc);
591 return EILSEQ;
e29d1b70
JA
592}
593
936216f8 594static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
bac39e0e 595{
936216f8 596 void *p = io_u_verify_off(hdr, vc);
bac39e0e
JA
597 struct vhdr_crc32 *vh = hdr_priv(hdr);
598 uint32_t c;
599
936216f8 600 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
bac39e0e 601
25dfa848 602 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
bac39e0e 603
bfacf389
JA
604 if (c == vh->crc32)
605 return 0;
bac39e0e 606
bfacf389
JA
607 vc->name = "crc32c";
608 vc->good_crc = &vh->crc32;
609 vc->bad_crc = &c;
610 vc->crc_len = 4;
611 log_verify_failure(hdr, vc);
612 return EILSEQ;
bac39e0e
JA
613}
614
936216f8 615static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
e29d1b70 616{
936216f8 617 void *p = io_u_verify_off(hdr, vc);
546dfd9f 618 struct vhdr_md5 *vh = hdr_priv(hdr);
8c432325 619 uint32_t hash[MD5_HASH_WORDS];
25dfa848 620 struct fio_md5_ctx md5_ctx = {
8c432325
JA
621 .hash = hash,
622 };
e29d1b70 623
936216f8 624 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 625
25dfa848
JA
626 fio_md5_init(&md5_ctx);
627 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
e29d1b70 628
bfacf389
JA
629 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
630 return 0;
e29d1b70 631
bfacf389
JA
632 vc->name = "md5";
633 vc->good_crc = vh->md5_digest;
634 vc->bad_crc = md5_ctx.hash;
635 vc->crc_len = sizeof(hash);
636 log_verify_failure(hdr, vc);
637 return EILSEQ;
e29d1b70
JA
638}
639
e8462bd8
JA
640/*
641 * Push IO verification to a separate thread
642 */
643int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
644{
645 if (io_u->file)
646 put_file_log(td, io_u->file);
647
e8462bd8 648 pthread_mutex_lock(&td->io_u_lock);
d7ee2a7d 649
0c41214f
RR
650 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
651 td->cur_depth--;
652 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
653 }
2ae0b204 654 flist_add_tail(&io_u->verify_list, &td->verify_list);
2ecc1b57 655 io_u->flags |= IO_U_F_FREE_DEF;
e8462bd8
JA
656 pthread_mutex_unlock(&td->io_u_lock);
657
658 pthread_cond_signal(&td->verify_cond);
e8462bd8
JA
659 return 0;
660}
661
0d29de83
JA
662static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
663{
664 static char zero_buf[1024];
665 unsigned int this_len, len;
666 int ret = 0;
667 void *p;
668
669 if (!td->o.trim_zero)
670 return 0;
671
672 len = io_u->buflen;
673 p = io_u->buf;
674 do {
675 this_len = sizeof(zero_buf);
676 if (this_len > len)
677 this_len = len;
678 if (memcmp(p, zero_buf, this_len)) {
679 ret = EILSEQ;
680 break;
681 }
682 len -= this_len;
683 p += this_len;
684 } while (len);
685
686 if (!ret)
687 return 0;
688
a917a8b3
JA
689 log_err("trim: verify failed at file %s offset %llu, length %lu"
690 ", block offset %lu\n",
691 io_u->file->file_name, io_u->offset, io_u->buflen,
2f68124f 692 (unsigned long) (p - io_u->buf));
0d29de83
JA
693 return ret;
694}
695
0ae2c6e1 696static int verify_header(struct io_u *io_u, struct verify_header *hdr)
f65d1c26
JA
697{
698 void *p = hdr;
699 uint32_t crc;
700
ae38c0dc 701 if (hdr->magic != FIO_HDR_MAGIC)
e9e177cc
JC
702 return 1;
703 if (hdr->len > io_u->buflen)
704 return 2;
705 if (hdr->rand_seed != io_u->rand_seed)
706 return 3;
ae38c0dc 707
25dfa848 708 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
f65d1c26 709 if (crc == hdr->crc32)
e9e177cc 710 return 0;
f65d1c26 711 log_err("fio: verify header crc %x, calculated %x\n", hdr->crc32, crc);
e9e177cc 712 return 4;
f65d1c26
JA
713}
714
36690c9b 715int verify_io_u(struct thread_data *td, struct io_u *io_u)
e29d1b70 716{
3f9f4e26 717 struct verify_header *hdr;
2b13e716 718 unsigned int header_size, hdr_inc, hdr_num = 0;
95646108 719 void *p;
e29d1b70
JA
720 int ret;
721
1dcc0498 722 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b 723 return 0;
0d29de83
JA
724 if (io_u->flags & IO_U_F_TRIMMED) {
725 ret = verify_trimmed_io_u(td, io_u);
726 goto done;
727 }
36690c9b 728
cfbcd0dc 729 hdr_inc = get_hdr_inc(td, io_u);
e29d1b70 730
a12a3b4d 731 ret = 0;
5ec10eaa
JA
732 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
733 p += hdr_inc, hdr_num++) {
bfacf389
JA
734 struct vcont vc = {
735 .io_u = io_u,
736 .hdr_num = hdr_num,
7d9fb455 737 .td = td,
bfacf389 738 };
f00b210f 739 unsigned int verify_type;
936216f8 740
f3e6cb95 741 if (ret && td->o.verify_fatal)
a12a3b4d 742 break;
f3e6cb95 743
2b13e716 744 header_size = __hdr_size(td->o.verify);
a59e170d 745 if (td->o.verify_offset)
2b13e716 746 memswp(p, p + td->o.verify_offset, header_size);
95646108 747 hdr = p;
e29d1b70 748
c4b6117b
PV
749 /*
750 * Make rand_seed check pass when have verifysort or
751 * verify_backlog.
752 */
753 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
754 io_u->rand_seed = hdr->rand_seed;
755
e9e177cc
JC
756 ret = verify_header(io_u, hdr);
757 switch (ret) {
758 case 0:
759 break;
760 case 1:
ae38c0dc
JA
761 log_err("verify: bad magic header %x, wanted %x at "
762 "file %s offset %llu, length %u\n",
d3a173a9 763 hdr->magic, FIO_HDR_MAGIC,
c9b44031
JA
764 io_u->file->file_name,
765 io_u->offset + hdr_num * hdr->len, hdr->len);
9fd18969 766 return EILSEQ;
e9e177cc
JC
767 break;
768 case 2:
769 log_err("fio: verify header exceeds buffer length (%u "
770 "> %lu)\n", hdr->len, io_u->buflen);
771 return EILSEQ;
772 break;
773 case 3:
774 log_err("verify: bad header rand_seed %"PRIu64
775 ", wanted %"PRIu64" at file %s offset %llu, "
776 "length %u\n",
777 hdr->rand_seed, io_u->rand_seed,
778 io_u->file->file_name,
779 io_u->offset + hdr_num * hdr->len, hdr->len);
780 return EILSEQ;
781 break;
782 case 4:
783 return EILSEQ;
784 break;
785 default:
786 log_err("verify: unknown header error at file %s "
787 "offset %llu, length %u\n",
788 io_u->file->file_name,
789 io_u->offset + hdr_num * hdr->len, hdr->len);
790 return EILSEQ;
3f199b01
JA
791 }
792
f00b210f
JA
793 if (td->o.verify != VERIFY_NONE)
794 verify_type = td->o.verify;
795 else
796 verify_type = hdr->verify_type;
797
798 switch (verify_type) {
3f9f4e26 799 case VERIFY_MD5:
936216f8 800 ret = verify_io_u_md5(hdr, &vc);
3f9f4e26
SL
801 break;
802 case VERIFY_CRC64:
936216f8 803 ret = verify_io_u_crc64(hdr, &vc);
3f9f4e26 804 break;
bac39e0e 805 case VERIFY_CRC32C:
3845591f 806 case VERIFY_CRC32C_INTEL:
936216f8 807 ret = verify_io_u_crc32c(hdr, &vc);
bac39e0e 808 break;
3f9f4e26 809 case VERIFY_CRC32:
936216f8 810 ret = verify_io_u_crc32(hdr, &vc);
3f9f4e26
SL
811 break;
812 case VERIFY_CRC16:
936216f8 813 ret = verify_io_u_crc16(hdr, &vc);
3f9f4e26
SL
814 break;
815 case VERIFY_CRC7:
936216f8 816 ret = verify_io_u_crc7(hdr, &vc);
3f9f4e26 817 break;
cd14cc10 818 case VERIFY_SHA256:
936216f8 819 ret = verify_io_u_sha256(hdr, &vc);
cd14cc10
JA
820 break;
821 case VERIFY_SHA512:
936216f8 822 ret = verify_io_u_sha512(hdr, &vc);
cd14cc10 823 break;
844ea602
JA
824 case VERIFY_XXHASH:
825 ret = verify_io_u_xxhash(hdr, &vc);
826 break;
7437ee87 827 case VERIFY_META:
92bf48d5 828 ret = verify_io_u_meta(hdr, &vc);
7437ee87 829 break;
7c353ceb 830 case VERIFY_SHA1:
936216f8 831 ret = verify_io_u_sha1(hdr, &vc);
7c353ceb 832 break;
92bf48d5
JA
833 case VERIFY_PATTERN:
834 ret = verify_io_u_pattern(hdr, &vc);
835 break;
3f9f4e26
SL
836 default:
837 log_err("Bad verify type %u\n", hdr->verify_type);
d16d4e09 838 ret = EINVAL;
3f9f4e26 839 }
f00b210f
JA
840
841 if (ret && verify_type != hdr->verify_type)
842 log_err("fio: verify type mismatch (%u media, %u given)\n",
843 hdr->verify_type, verify_type);
3f9f4e26 844 }
a7dfe862 845
0d29de83 846done:
f3e6cb95
JA
847 if (ret && td->o.verify_fatal)
848 td->terminate = 1;
849
a12a3b4d 850 return ret;
e29d1b70
JA
851}
852
7437ee87 853static void fill_meta(struct verify_header *hdr, struct thread_data *td,
5ec10eaa 854 struct io_u *io_u, unsigned int header_num)
7437ee87
SL
855{
856 struct vhdr_meta *vh = hdr_priv(hdr);
857
858 vh->thread = td->thread_number;
859
860 vh->time_sec = io_u->start_time.tv_sec;
861 vh->time_usec = io_u->start_time.tv_usec;
862
da0a7bd2 863 vh->numberio = io_u->numberio;
7437ee87
SL
864
865 vh->offset = io_u->offset + header_num * td->o.verify_interval;
866}
867
844ea602
JA
868static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
869{
870 struct vhdr_xxhash *vh = hdr_priv(hdr);
871 void *state;
872
873 state = XXH32_init(1);
874 XXH32_update(state, p, len);
875 vh->hash = XXH32_digest(state);
876}
877
cd14cc10
JA
878static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
879{
546dfd9f 880 struct vhdr_sha512 *vh = hdr_priv(hdr);
25dfa848 881 struct fio_sha512_ctx sha512_ctx = {
546dfd9f 882 .buf = vh->sha512,
cd14cc10
JA
883 };
884
25dfa848
JA
885 fio_sha512_init(&sha512_ctx);
886 fio_sha512_update(&sha512_ctx, p, len);
cd14cc10
JA
887}
888
889static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
890{
546dfd9f 891 struct vhdr_sha256 *vh = hdr_priv(hdr);
25dfa848 892 struct fio_sha256_ctx sha256_ctx = {
546dfd9f 893 .buf = vh->sha256,
cd14cc10
JA
894 };
895
25dfa848
JA
896 fio_sha256_init(&sha256_ctx);
897 fio_sha256_update(&sha256_ctx, p, len);
cd14cc10
JA
898}
899
7c353ceb
JA
900static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
901{
902 struct vhdr_sha1 *vh = hdr_priv(hdr);
25dfa848 903 struct fio_sha1_ctx sha1_ctx = {
7c353ceb
JA
904 .H = vh->sha1,
905 };
906
25dfa848
JA
907 fio_sha1_init(&sha1_ctx);
908 fio_sha1_update(&sha1_ctx, p, len);
7c353ceb
JA
909}
910
1e154bdb
JA
911static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
912{
546dfd9f
JA
913 struct vhdr_crc7 *vh = hdr_priv(hdr);
914
25dfa848 915 vh->crc7 = fio_crc7(p, len);
1e154bdb
JA
916}
917
969f7ed3
JA
918static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
919{
546dfd9f
JA
920 struct vhdr_crc16 *vh = hdr_priv(hdr);
921
25dfa848 922 vh->crc16 = fio_crc16(p, len);
969f7ed3
JA
923}
924
e29d1b70
JA
925static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
926{
546dfd9f
JA
927 struct vhdr_crc32 *vh = hdr_priv(hdr);
928
25dfa848 929 vh->crc32 = fio_crc32(p, len);
e29d1b70
JA
930}
931
bac39e0e
JA
932static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
933{
934 struct vhdr_crc32 *vh = hdr_priv(hdr);
935
25dfa848 936 vh->crc32 = fio_crc32c(p, len);
bac39e0e
JA
937}
938
d77a7af3
JA
939static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
940{
546dfd9f
JA
941 struct vhdr_crc64 *vh = hdr_priv(hdr);
942
25dfa848 943 vh->crc64 = fio_crc64(p, len);
d77a7af3
JA
944}
945
e29d1b70
JA
946static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
947{
546dfd9f 948 struct vhdr_md5 *vh = hdr_priv(hdr);
25dfa848 949 struct fio_md5_ctx md5_ctx = {
546dfd9f 950 .hash = (uint32_t *) vh->md5_digest,
8c432325 951 };
e29d1b70 952
25dfa848
JA
953 fio_md5_init(&md5_ctx);
954 fio_md5_update(&md5_ctx, p, len);
e29d1b70
JA
955}
956
7d9fb455
JA
957static void populate_hdr(struct thread_data *td, struct io_u *io_u,
958 struct verify_header *hdr, unsigned int header_num,
959 unsigned int header_len)
960{
961 unsigned int data_len;
962 void *data, *p;
963
964 p = (void *) hdr;
965
d3a173a9 966 hdr->magic = FIO_HDR_MAGIC;
7d9fb455 967 hdr->verify_type = td->o.verify;
d3a173a9 968 hdr->len = header_len;
7d9fb455 969 hdr->rand_seed = io_u->rand_seed;
25dfa848 970 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
f65d1c26 971
7d9fb455
JA
972 data_len = header_len - hdr_size(hdr);
973
974 data = p + hdr_size(hdr);
975 switch (td->o.verify) {
976 case VERIFY_MD5:
977 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
978 io_u, hdr->len);
979 fill_md5(hdr, data, data_len);
980 break;
981 case VERIFY_CRC64:
982 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
983 io_u, hdr->len);
984 fill_crc64(hdr, data, data_len);
985 break;
986 case VERIFY_CRC32C:
987 case VERIFY_CRC32C_INTEL:
988 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
989 io_u, hdr->len);
990 fill_crc32c(hdr, data, data_len);
991 break;
992 case VERIFY_CRC32:
993 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
994 io_u, hdr->len);
995 fill_crc32(hdr, data, data_len);
996 break;
997 case VERIFY_CRC16:
998 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
999 io_u, hdr->len);
1000 fill_crc16(hdr, data, data_len);
1001 break;
1002 case VERIFY_CRC7:
1003 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1004 io_u, hdr->len);
1005 fill_crc7(hdr, data, data_len);
1006 break;
1007 case VERIFY_SHA256:
1008 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1009 io_u, hdr->len);
1010 fill_sha256(hdr, data, data_len);
1011 break;
1012 case VERIFY_SHA512:
1013 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1014 io_u, hdr->len);
1015 fill_sha512(hdr, data, data_len);
1016 break;
844ea602
JA
1017 case VERIFY_XXHASH:
1018 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1019 io_u, hdr->len);
1020 fill_xxhash(hdr, data, data_len);
1021 break;
7d9fb455
JA
1022 case VERIFY_META:
1023 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1024 io_u, hdr->len);
1025 fill_meta(hdr, td, io_u, header_num);
1026 break;
1027 case VERIFY_SHA1:
1028 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1029 io_u, hdr->len);
1030 fill_sha1(hdr, data, data_len);
1031 break;
92bf48d5
JA
1032 case VERIFY_PATTERN:
1033 /* nothing to do here */
1034 break;
7d9fb455
JA
1035 default:
1036 log_err("fio: bad verify type: %d\n", td->o.verify);
1037 assert(0);
1038 }
1039 if (td->o.verify_offset)
1040 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
1041}
1042
e29d1b70
JA
1043/*
1044 * fill body of io_u->buf with random data and add a header with the
c50ca7bd 1045 * checksum of choice
e29d1b70
JA
1046 */
1047void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1048{
9cc3d150
JA
1049 if (td->o.verify == VERIFY_NULL)
1050 return;
1051
da0a7bd2
JC
1052 io_u->numberio = td->io_issues[io_u->ddir];
1053
c50ca7bd 1054 fill_pattern_headers(td, io_u, 0, 0);
e29d1b70
JA
1055}
1056
1057int get_next_verify(struct thread_data *td, struct io_u *io_u)
1058{
8de8f047 1059 struct io_piece *ipo = NULL;
e29d1b70 1060
d2d7fa53
JA
1061 /*
1062 * this io_u is from a requeue, we already filled the offsets
1063 */
1064 if (io_u->file)
1065 return 0;
1066
8de8f047
JA
1067 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1068 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 1069
8de8f047 1070 ipo = rb_entry(n, struct io_piece, rb_node);
f9401285
JA
1071
1072 /*
1073 * Ensure that the associated IO has completed
1074 */
1075 read_barrier();
1076 if (ipo->flags & IP_F_IN_FLIGHT)
1077 goto nothing;
1078
4b87898e 1079 rb_erase(n, &td->io_hist_tree);
a917a8b3
JA
1080 assert(ipo->flags & IP_F_ONRB);
1081 ipo->flags &= ~IP_F_ONRB;
01743ee1
JA
1082 } else if (!flist_empty(&td->io_hist_list)) {
1083 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
f9401285
JA
1084
1085 /*
1086 * Ensure that the associated IO has completed
1087 */
1088 read_barrier();
1089 if (ipo->flags & IP_F_IN_FLIGHT)
1090 goto nothing;
1091
01743ee1 1092 flist_del(&ipo->list);
a917a8b3
JA
1093 assert(ipo->flags & IP_F_ONLIST);
1094 ipo->flags &= ~IP_F_ONLIST;
8de8f047 1095 }
e29d1b70 1096
8de8f047 1097 if (ipo) {
0d29de83
JA
1098 td->io_hist_len--;
1099
e29d1b70
JA
1100 io_u->offset = ipo->offset;
1101 io_u->buflen = ipo->len;
da0a7bd2 1102 io_u->numberio = ipo->numberio;
36167d82 1103 io_u->file = ipo->file;
82af2a7c 1104 io_u->flags |= IO_U_F_VER_LIST;
97af62ce 1105
0d29de83
JA
1106 if (ipo->flags & IP_F_TRIMMED)
1107 io_u->flags |= IO_U_F_TRIMMED;
1108
d6aed795 1109 if (!fio_file_open(io_u->file)) {
97af62ce
JA
1110 int r = td_io_open_file(td, io_u->file);
1111
bd6f78b2
JA
1112 if (r) {
1113 dprint(FD_VERIFY, "failed file %s open\n",
1114 io_u->file->file_name);
97af62ce 1115 return 1;
bd6f78b2 1116 }
97af62ce
JA
1117 }
1118
1119 get_file(ipo->file);
d6aed795 1120 assert(fio_file_open(io_u->file));
e29d1b70 1121 io_u->ddir = DDIR_READ;
36167d82
JA
1122 io_u->xfer_buf = io_u->buf;
1123 io_u->xfer_buflen = io_u->buflen;
0d29de83
JA
1124
1125 remove_trim_entry(td, ipo);
e29d1b70 1126 free(ipo);
bd6f78b2 1127 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
c4b6117b
PV
1128
1129 if (!td->o.verify_pattern_bytes) {
1130 io_u->rand_seed = __rand(&td->__verify_state);
1131 if (sizeof(int) != sizeof(long *))
1132 io_u->rand_seed *= __rand(&td->__verify_state);
1133 }
e29d1b70
JA
1134 return 0;
1135 }
1136
f9401285 1137nothing:
bd6f78b2 1138 dprint(FD_VERIFY, "get_next_verify: empty\n");
e29d1b70
JA
1139 return 1;
1140}
e8462bd8 1141
dc5bfbb2
JA
1142void fio_verify_init(struct thread_data *td)
1143{
1144 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1145 td->o.verify == VERIFY_CRC32C) {
1146 crc32c_intel_probe();
1147 }
1148}
1149
e8462bd8
JA
1150static void *verify_async_thread(void *data)
1151{
1152 struct thread_data *td = data;
1153 struct io_u *io_u;
1154 int ret = 0;
1155
1156 if (td->o.verify_cpumask_set &&
1157 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1158 log_err("fio: failed setting verify thread affinity\n");
1159 goto done;
1160 }
1161
1162 do {
e53ab27c
JA
1163 FLIST_HEAD(list);
1164
e8462bd8
JA
1165 read_barrier();
1166 if (td->verify_thread_exit)
1167 break;
1168
1169 pthread_mutex_lock(&td->io_u_lock);
1170
1171 while (flist_empty(&td->verify_list) &&
1172 !td->verify_thread_exit) {
b36e298b
JA
1173 ret = pthread_cond_wait(&td->verify_cond,
1174 &td->io_u_lock);
e8462bd8
JA
1175 if (ret) {
1176 pthread_mutex_unlock(&td->io_u_lock);
1177 break;
1178 }
1179 }
1180
e53ab27c
JA
1181 flist_splice_init(&td->verify_list, &list);
1182 pthread_mutex_unlock(&td->io_u_lock);
1183
1184 if (flist_empty(&list))
e8462bd8 1185 continue;
e8462bd8 1186
e53ab27c 1187 while (!flist_empty(&list)) {
2ae0b204
JA
1188 io_u = flist_entry(list.next, struct io_u, verify_list);
1189 flist_del(&io_u->verify_list);
e8462bd8 1190
d561f2ab 1191 ret = verify_io_u(td, io_u);
e53ab27c 1192 put_io_u(td, io_u);
d561f2ab
JA
1193 if (!ret)
1194 continue;
8b28bd41 1195 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
d561f2ab
JA
1196 update_error_count(td, ret);
1197 td_clear_error(td);
1198 ret = 0;
1199 }
e53ab27c 1200 }
e8462bd8
JA
1201 } while (!ret);
1202
d561f2ab
JA
1203 if (ret) {
1204 td_verror(td, ret, "async_verify");
f3e6cb95
JA
1205 if (td->o.verify_fatal)
1206 td->terminate = 1;
d561f2ab
JA
1207 }
1208
e8462bd8
JA
1209done:
1210 pthread_mutex_lock(&td->io_u_lock);
1211 td->nr_verify_threads--;
1212 pthread_mutex_unlock(&td->io_u_lock);
1213
1214 pthread_cond_signal(&td->free_cond);
1215 return NULL;
1216}
1217
1218int verify_async_init(struct thread_data *td)
1219{
1220 int i, ret;
304a47c7
VA
1221 pthread_attr_t attr;
1222
1223 pthread_attr_init(&attr);
1224 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
e8462bd8
JA
1225
1226 td->verify_thread_exit = 0;
1227
1228 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1229 for (i = 0; i < td->o.verify_async; i++) {
304a47c7 1230 ret = pthread_create(&td->verify_threads[i], &attr,
e8462bd8
JA
1231 verify_async_thread, td);
1232 if (ret) {
1233 log_err("fio: async verify creation failed: %s\n",
1234 strerror(ret));
1235 break;
1236 }
1237 ret = pthread_detach(td->verify_threads[i]);
1238 if (ret) {
1239 log_err("fio: async verify thread detach failed: %s\n",
1240 strerror(ret));
1241 break;
1242 }
1243 td->nr_verify_threads++;
1244 }
1245
304a47c7
VA
1246 pthread_attr_destroy(&attr);
1247
e8462bd8 1248 if (i != td->o.verify_async) {
e40823b1 1249 log_err("fio: only %d verify threads started, exiting\n", i);
e8462bd8
JA
1250 td->verify_thread_exit = 1;
1251 write_barrier();
1252 pthread_cond_broadcast(&td->verify_cond);
1253 return 1;
1254 }
1255
1256 return 0;
1257}
1258
1259void verify_async_exit(struct thread_data *td)
1260{
1261 td->verify_thread_exit = 1;
1262 write_barrier();
1263 pthread_cond_broadcast(&td->verify_cond);
1264
1265 pthread_mutex_lock(&td->io_u_lock);
1266
1267 while (td->nr_verify_threads)
1268 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1269
1270 pthread_mutex_unlock(&td->io_u_lock);
1271 free(td->verify_threads);
1272 td->verify_threads = NULL;
1273}