gfio: Improve IOPS textbox labels
[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"
2cac8fcb 16#include "lib/pattern.h"
e29d1b70 17
eef6eea1
JA
18#include "crc/md5.h"
19#include "crc/crc64.h"
20#include "crc/crc32.h"
bac39e0e 21#include "crc/crc32c.h"
eef6eea1
JA
22#include "crc/crc16.h"
23#include "crc/crc7.h"
24#include "crc/sha256.h"
25#include "crc/sha512.h"
7c353ceb 26#include "crc/sha1.h"
844ea602 27#include "crc/xxhash.h"
cd14cc10 28
7d9fb455
JA
29static void populate_hdr(struct thread_data *td, struct io_u *io_u,
30 struct verify_header *hdr, unsigned int header_num,
31 unsigned int header_len);
b638d82f
RP
32static void fill_hdr(struct thread_data *td, struct io_u *io_u,
33 struct verify_header *hdr, unsigned int header_num,
34 unsigned int header_len, uint64_t rand_seed);
35static void __fill_hdr(struct thread_data *td, struct io_u *io_u,
36 struct verify_header *hdr, unsigned int header_num,
37 unsigned int header_len, uint64_t rand_seed);
7d9fb455 38
ce35b1ec
JA
39void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
40{
2cac8fcb 41 (void)cpy_pattern(td->o.buffer_pattern, td->o.buffer_pattern_bytes, p, len);
ce35b1ec
JA
42}
43
a89ba4b1
JA
44static void __fill_buffer(struct thread_options *o, unsigned long seed, void *p,
45 unsigned int len)
7fa085ea
JA
46{
47 __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
48}
49
a89ba4b1
JA
50static unsigned long fill_buffer(struct thread_data *td, void *p,
51 unsigned int len)
7fa085ea
JA
52{
53 struct frand_state *fs = &td->verify_state;
54 struct thread_options *o = &td->o;
55
56 return fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
57}
58
ce35b1ec
JA
59void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
60 struct io_u *io_u, unsigned long seed, int use_seed)
61{
bc769898
JA
62 struct thread_options *o = &td->o;
63
64 if (!o->verify_pattern_bytes) {
ce35b1ec
JA
65 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
66
67 if (use_seed)
7fa085ea
JA
68 __fill_buffer(o, seed, p, len);
69 else
70 io_u->rand_seed = fill_buffer(td, p, len);
ce35b1ec
JA
71 return;
72 }
c4b6117b 73
61b9861d
RP
74 /* Skip if we were here and we do not need to patch pattern
75 * with format */
76 if (!td->o.verify_fmt_sz && io_u->buf_filled_len >= len) {
ce35b1ec 77 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
bc769898 78 o->verify_pattern_bytes, len);
ce35b1ec
JA
79 return;
80 }
81
61b9861d
RP
82 (void)paste_format(td->o.verify_pattern, td->o.verify_pattern_bytes,
83 td->o.verify_fmt, td->o.verify_fmt_sz,
84 p, len, io_u);
ce35b1ec
JA
85 io_u->buf_filled_len = len;
86}
87
cfbcd0dc
JA
88static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
89{
90 unsigned int hdr_inc;
91
92 hdr_inc = io_u->buflen;
8a99fdf6 93 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
cfbcd0dc
JA
94 hdr_inc = td->o.verify_interval;
95
96 return hdr_inc;
97}
98
c50ca7bd
JA
99static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
100 unsigned long seed, int use_seed)
101{
102 unsigned int hdr_inc, header_num;
103 struct verify_header *hdr;
104 void *p = io_u->buf;
105
ce35b1ec 106 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
c50ca7bd 107
cfbcd0dc 108 hdr_inc = get_hdr_inc(td, io_u);
c50ca7bd
JA
109 header_num = 0;
110 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
111 hdr = p;
112 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
113 header_num++;
114 }
115}
116
4764aec9 117static void memswp(void *buf1, void *buf2, unsigned int len)
546a9142 118{
dee6de74
SL
119 char swap[200];
120
121 assert(len <= sizeof(swap));
90059d65 122
546a9142
SL
123 memcpy(&swap, buf1, len);
124 memcpy(buf1, buf2, len);
125 memcpy(buf2, &swap, len);
126}
127
e29d1b70
JA
128static void hexdump(void *buffer, int len)
129{
130 unsigned char *p = buffer;
131 int i;
132
133 for (i = 0; i < len; i++)
bfacf389
JA
134 log_err("%02x", p[i]);
135 log_err("\n");
e29d1b70
JA
136}
137
87677832 138/*
de8f6de9 139 * Prepare for separation of verify_header and checksum header
87677832 140 */
546dfd9f 141static inline unsigned int __hdr_size(int verify_type)
87677832 142{
03e20d68 143 unsigned int len = 0;
87677832 144
546dfd9f
JA
145 switch (verify_type) {
146 case VERIFY_NONE:
b638d82f 147 case VERIFY_HDR_ONLY:
546dfd9f 148 case VERIFY_NULL:
ed3a433d 149 case VERIFY_PATTERN:
546dfd9f
JA
150 len = 0;
151 break;
152 case VERIFY_MD5:
153 len = sizeof(struct vhdr_md5);
154 break;
155 case VERIFY_CRC64:
156 len = sizeof(struct vhdr_crc64);
157 break;
bac39e0e 158 case VERIFY_CRC32C:
546dfd9f 159 case VERIFY_CRC32:
3845591f 160 case VERIFY_CRC32C_INTEL:
546dfd9f
JA
161 len = sizeof(struct vhdr_crc32);
162 break;
163 case VERIFY_CRC16:
164 len = sizeof(struct vhdr_crc16);
165 break;
166 case VERIFY_CRC7:
167 len = sizeof(struct vhdr_crc7);
168 break;
169 case VERIFY_SHA256:
170 len = sizeof(struct vhdr_sha256);
171 break;
172 case VERIFY_SHA512:
173 len = sizeof(struct vhdr_sha512);
174 break;
844ea602
JA
175 case VERIFY_XXHASH:
176 len = sizeof(struct vhdr_xxhash);
177 break;
7c353ceb
JA
178 case VERIFY_SHA1:
179 len = sizeof(struct vhdr_sha1);
180 break;
59245381 181 case VERIFY_PATTERN_NO_HDR:
ed3a433d 182 return 0;
546dfd9f
JA
183 default:
184 log_err("fio: unknown verify header!\n");
185 assert(0);
186 }
187
188 return len + sizeof(struct verify_header);
87677832
JA
189}
190
ed3a433d
JA
191static inline unsigned int hdr_size(struct thread_data *td,
192 struct verify_header *hdr)
87677832 193{
ed3a433d
JA
194 if (td->o.verify == VERIFY_PATTERN_NO_HDR)
195 return 0;
196
546dfd9f
JA
197 return __hdr_size(hdr->verify_type);
198}
199
200static void *hdr_priv(struct verify_header *hdr)
201{
202 void *priv = hdr;
203
204 return priv + sizeof(struct verify_header);
87677832
JA
205}
206
936216f8
JA
207/*
208 * Verify container, pass info to verify handlers and allow them to
209 * pass info back in case of error
210 */
211struct vcont {
212 /*
213 * Input
214 */
215 struct io_u *io_u;
216 unsigned int hdr_num;
7d9fb455 217 struct thread_data *td;
936216f8
JA
218
219 /*
220 * Output, only valid in case of error
221 */
bfacf389
JA
222 const char *name;
223 void *good_crc;
224 void *bad_crc;
936216f8
JA
225 unsigned int crc_len;
226};
227
dacbbb88
JA
228#define DUMP_BUF_SZ 255
229static int dump_buf_warned;
230
c50ca7bd
JA
231static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
232 const char *type, struct fio_file *f)
7d9fb455 233{
dacbbb88
JA
234 char *ptr, fname[DUMP_BUF_SZ];
235 size_t buf_left = DUMP_BUF_SZ;
7d9fb455
JA
236 int ret, fd;
237
6f260b31 238 ptr = strdup(f->file_name);
c50ca7bd 239
d264264a
JA
240 memset(fname, 0, sizeof(fname));
241 if (aux_path)
242 sprintf(fname, "%s%s", aux_path, FIO_OS_PATH_SEPARATOR);
243
244 strncpy(fname + strlen(fname), basename(ptr), buf_left - 1);
dacbbb88
JA
245
246 buf_left -= strlen(fname);
247 if (buf_left <= 0) {
248 if (!dump_buf_warned) {
249 log_err("fio: verify failure dump buffer too small\n");
250 dump_buf_warned = 1;
251 }
252 free(ptr);
253 return;
254 }
255
256 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
7d9fb455
JA
257
258 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
259 if (fd < 0) {
260 perror("open verify buf file");
261 return;
262 }
263
264 while (len) {
265 ret = write(fd, buf, len);
266 if (!ret)
267 break;
268 else if (ret < 0) {
269 perror("write verify buf file");
270 break;
271 }
272 len -= ret;
273 buf += ret;
274 }
275
276 close(fd);
c50ca7bd 277 log_err(" %s data dumped as %s\n", type, fname);
6f260b31 278 free(ptr);
7d9fb455
JA
279}
280
c50ca7bd
JA
281/*
282 * Dump the contents of the read block and re-generate the correct data
283 * and dump that too.
284 */
9ba987cc 285static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
7d9fb455
JA
286{
287 struct thread_data *td = vc->td;
288 struct io_u *io_u = vc->io_u;
289 unsigned long hdr_offset;
7d9fb455 290 struct io_u dummy;
c50ca7bd 291 void *buf;
7d9fb455 292
0dce9bc9
SL
293 if (!td->o.verify_dump)
294 return;
295
c50ca7bd
JA
296 /*
297 * Dump the contents we just read off disk
298 */
7d9fb455
JA
299 hdr_offset = vc->hdr_num * hdr->len;
300
c50ca7bd
JA
301 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
302 "received", vc->io_u->file);
7d9fb455 303
c50ca7bd
JA
304 /*
305 * Allocate a new buf and re-generate the original data
306 */
307 buf = malloc(io_u->buflen);
7d9fb455 308 dummy = *io_u;
c50ca7bd 309 dummy.buf = buf;
4aae38a6 310 dummy.rand_seed = hdr->rand_seed;
cfbcd0dc 311 dummy.buf_filled_len = 0;
0d81daf9 312 dummy.buflen = io_u->buflen;
7d9fb455 313
c50ca7bd 314 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
7d9fb455 315
c50ca7bd
JA
316 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
317 "expected", vc->io_u->file);
7d9fb455
JA
318 free(buf);
319}
320
9ba987cc
JA
321static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
322{
323 struct thread_data *td = vc->td;
324 struct verify_header shdr;
325
326 if (td->o.verify == VERIFY_PATTERN_NO_HDR) {
b638d82f 327 __fill_hdr(td, vc->io_u, &shdr, 0, vc->io_u->buflen, 0);
9ba987cc
JA
328 hdr = &shdr;
329 }
330
331 __dump_verify_buffers(hdr, vc);
332}
333
bfacf389
JA
334static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
335{
336 unsigned long long offset;
337
338 offset = vc->io_u->offset;
339 offset += vc->hdr_num * hdr->len;
32c17adf
JA
340 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
341 vc->name, vc->io_u->file->file_name, offset, hdr->len);
bfacf389
JA
342
343 if (vc->good_crc && vc->bad_crc) {
344 log_err(" Expected CRC: ");
345 hexdump(vc->good_crc, vc->crc_len);
346 log_err(" Received CRC: ");
347 hexdump(vc->bad_crc, vc->crc_len);
348 }
7d9fb455
JA
349
350 dump_verify_buffers(hdr, vc);
bfacf389
JA
351}
352
d9f2caf3
JA
353/*
354 * Return data area 'header_num'
355 */
936216f8 356static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
d9f2caf3 357{
ed3a433d 358 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr);
d9f2caf3
JA
359}
360
92bf48d5
JA
361static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
362{
363 struct thread_data *td = vc->td;
364 struct io_u *io_u = vc->io_u;
365 char *buf, *pattern;
2b13e716 366 unsigned int header_size = __hdr_size(td->o.verify);
25276497
RP
367 unsigned int len, mod, i, pattern_size;
368 int rc;
92bf48d5
JA
369
370 pattern = td->o.verify_pattern;
9a2a86d0 371 pattern_size = td->o.verify_pattern_bytes;
61b9861d
RP
372 assert(pattern_size != 0);
373
374 (void)paste_format_inplace(pattern, pattern_size,
375 td->o.verify_fmt, td->o.verify_fmt_sz, io_u);
376
2b13e716
JA
377 buf = (void *) hdr + header_size;
378 len = get_hdr_inc(td, io_u) - header_size;
61b9861d 379 mod = (get_hdr_inc(td, io_u) * vc->hdr_num + header_size) % pattern_size;
9a2a86d0 380
25276497
RP
381 rc = cmp_pattern(pattern, pattern_size, mod, buf, len);
382 if (!rc)
383 return 0;
92bf48d5 384
25276497
RP
385 /* Slow path, compare each byte */
386 for (i = 0; i < len; i++) {
92bf48d5
JA
387 if (buf[i] != pattern[mod]) {
388 unsigned int bits;
389
390 bits = hweight8(buf[i] ^ pattern[mod]);
25276497
RP
391 log_err("fio: got pattern '%02x', wanted '%02x'. Bad bits %d\n",
392 (unsigned char)buf[i],
393 (unsigned char)pattern[mod],
394 bits);
92bf48d5
JA
395 log_err("fio: bad pattern block offset %u\n", i);
396 dump_verify_buffers(hdr, vc);
397 return EILSEQ;
398 }
399 mod++;
400 if (mod == td->o.verify_pattern_bytes)
401 mod = 0;
402 }
403
25276497
RP
404 /* Unreachable line */
405 assert(0);
406 return EILSEQ;
92bf48d5
JA
407}
408
844ea602
JA
409static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
410{
411 void *p = io_u_verify_off(hdr, vc);
412 struct vhdr_xxhash *vh = hdr_priv(hdr);
413 uint32_t hash;
414 void *state;
415
416 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
417
418 state = XXH32_init(1);
ed3a433d 419 XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr));
844ea602
JA
420 hash = XXH32_digest(state);
421
422 if (vh->hash == hash)
423 return 0;
424
425 vc->name = "xxhash";
426 vc->good_crc = &vh->hash;
427 vc->bad_crc = &hash;
428 vc->crc_len = sizeof(hash);
429 log_verify_failure(hdr, vc);
430 return EILSEQ;
431}
432
936216f8 433static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
cd14cc10 434{
936216f8 435 void *p = io_u_verify_off(hdr, vc);
546dfd9f 436 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10 437 uint8_t sha512[128];
25dfa848 438 struct fio_sha512_ctx sha512_ctx = {
cd14cc10
JA
439 .buf = sha512,
440 };
441
936216f8 442 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 443
25dfa848 444 fio_sha512_init(&sha512_ctx);
ed3a433d 445 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr));
cd14cc10 446
bfacf389
JA
447 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
448 return 0;
cd14cc10 449
bfacf389
JA
450 vc->name = "sha512";
451 vc->good_crc = vh->sha512;
452 vc->bad_crc = sha512_ctx.buf;
453 vc->crc_len = sizeof(vh->sha512);
454 log_verify_failure(hdr, vc);
455 return EILSEQ;
cd14cc10
JA
456}
457
936216f8 458static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
cd14cc10 459{
936216f8 460 void *p = io_u_verify_off(hdr, vc);
546dfd9f 461 struct vhdr_sha256 *vh = hdr_priv(hdr);
bc77f56f 462 uint8_t sha256[64];
25dfa848 463 struct fio_sha256_ctx sha256_ctx = {
cd14cc10
JA
464 .buf = sha256,
465 };
466
936216f8 467 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 468
25dfa848 469 fio_sha256_init(&sha256_ctx);
ed3a433d 470 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr));
017052b6 471 fio_sha256_final(&sha256_ctx);
cd14cc10 472
bfacf389
JA
473 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
474 return 0;
cd14cc10 475
bfacf389
JA
476 vc->name = "sha256";
477 vc->good_crc = vh->sha256;
478 vc->bad_crc = sha256_ctx.buf;
479 vc->crc_len = sizeof(vh->sha256);
480 log_verify_failure(hdr, vc);
481 return EILSEQ;
cd14cc10
JA
482}
483
936216f8 484static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
7c353ceb 485{
936216f8 486 void *p = io_u_verify_off(hdr, vc);
7c353ceb
JA
487 struct vhdr_sha1 *vh = hdr_priv(hdr);
488 uint32_t sha1[5];
25dfa848 489 struct fio_sha1_ctx sha1_ctx = {
7c353ceb
JA
490 .H = sha1,
491 };
492
936216f8 493 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
7c353ceb 494
25dfa848 495 fio_sha1_init(&sha1_ctx);
ed3a433d 496 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr));
017052b6 497 fio_sha1_final(&sha1_ctx);
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
ed3a433d 518 c = fio_crc7(p, hdr->len - hdr_size(vc->td, 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
ed3a433d 539 c = fio_crc16(p, hdr->len - hdr_size(vc->td, 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
ed3a433d 560 c = fio_crc64(p, hdr->len - hdr_size(vc->td, 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
ed3a433d 581 c = fio_crc32(p, hdr->len - hdr_size(vc->td, 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
ed3a433d 602 c = fio_crc32c(p, hdr->len - hdr_size(vc->td, 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 626 fio_md5_init(&md5_ctx);
ed3a433d 627 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr));
017052b6 628 fio_md5_final(&md5_ctx);
e29d1b70 629
bfacf389
JA
630 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
631 return 0;
e29d1b70 632
bfacf389
JA
633 vc->name = "md5";
634 vc->good_crc = vh->md5_digest;
635 vc->bad_crc = md5_ctx.hash;
636 vc->crc_len = sizeof(hash);
637 log_verify_failure(hdr, vc);
638 return EILSEQ;
e29d1b70
JA
639}
640
e8462bd8
JA
641/*
642 * Push IO verification to a separate thread
643 */
f8b0bd10 644int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
e8462bd8 645{
f8b0bd10 646 struct io_u *io_u = *io_u_ptr;
e8462bd8 647
e8462bd8 648 pthread_mutex_lock(&td->io_u_lock);
d7ee2a7d 649
f8b0bd10
JA
650 if (io_u->file)
651 put_file_log(td, io_u->file);
652
0c41214f
RR
653 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
654 td->cur_depth--;
1651e431 655 io_u_clear(td, io_u, IO_U_F_IN_CUR_DEPTH);
0c41214f 656 }
2ae0b204 657 flist_add_tail(&io_u->verify_list, &td->verify_list);
f8b0bd10 658 *io_u_ptr = NULL;
e8462bd8
JA
659 pthread_mutex_unlock(&td->io_u_lock);
660
661 pthread_cond_signal(&td->verify_cond);
e8462bd8
JA
662 return 0;
663}
664
323f58c5
JA
665/*
666 * Thanks Rusty, for spending the time so I don't have to.
667 *
668 * http://rusty.ozlabs.org/?p=560
669 */
670static int mem_is_zero(const void *data, size_t length)
671{
672 const unsigned char *p = data;
673 size_t len;
674
675 /* Check first 16 bytes manually */
676 for (len = 0; len < 16; len++) {
677 if (!length)
678 return 1;
679 if (*p)
680 return 0;
681 p++;
682 length--;
683 }
684
685 /* Now we know that's zero, memcmp with self. */
686 return memcmp(data, p, length) == 0;
687}
688
689static int mem_is_zero_slow(const void *data, size_t length, size_t *offset)
690{
691 const unsigned char *p = data;
692
693 *offset = 0;
694 while (length) {
695 if (*p)
696 break;
697 (*offset)++;
698 length--;
699 p++;
700 }
701
702 return !length;
703}
704
0d29de83
JA
705static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
706{
323f58c5 707 size_t offset;
0d29de83
JA
708
709 if (!td->o.trim_zero)
710 return 0;
711
323f58c5 712 if (mem_is_zero(io_u->buf, io_u->buflen))
0d29de83
JA
713 return 0;
714
323f58c5
JA
715 mem_is_zero_slow(io_u->buf, io_u->buflen, &offset);
716
a917a8b3
JA
717 log_err("trim: verify failed at file %s offset %llu, length %lu"
718 ", block offset %lu\n",
719 io_u->file->file_name, io_u->offset, io_u->buflen,
323f58c5
JA
720 (unsigned long) offset);
721 return EILSEQ;
0d29de83
JA
722}
723
b638d82f
RP
724static int verify_header(struct io_u *io_u, struct thread_data *td,
725 struct verify_header *hdr, unsigned int hdr_num,
726 unsigned int hdr_len)
f65d1c26
JA
727{
728 void *p = hdr;
729 uint32_t crc;
730
5964842c
AG
731 if (hdr->magic != FIO_HDR_MAGIC) {
732 log_err("verify: bad magic header %x, wanted %x",
733 hdr->magic, FIO_HDR_MAGIC);
734 goto err;
735 }
4445856a 736 if (hdr->len != hdr_len) {
5964842c
AG
737 log_err("verify: bad header length %u, wanted %u",
738 hdr->len, hdr_len);
739 goto err;
740 }
741 if (hdr->rand_seed != io_u->rand_seed) {
742 log_err("verify: bad header rand_seed %"PRIu64
743 ", wanted %"PRIu64,
744 hdr->rand_seed, io_u->rand_seed);
745 goto err;
746 }
b638d82f
RP
747 if (hdr->offset != io_u->offset + hdr_num * td->o.verify_interval) {
748 log_err("verify: bad header offset %"PRIu64
749 ", wanted %llu",
750 hdr->offset, io_u->offset);
751 goto err;
752 }
753
754 /*
755 * For read-only workloads, the program cannot be certain of the
756 * last numberio written to a block. Checking of numberio will be
757 * done only for workloads that write data. For verify_only,
758 * numberio will be checked in the last iteration when the correct
759 * state of numberio, that would have been written to each block
760 * in a previous run of fio, has been reached.
761 */
762 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
763 !td->o.time_based)
764 if (!td->o.verify_only || td->o.loops == 0)
765 if (hdr->numberio != io_u->numberio) {
766 log_err("verify: bad header numberio %"PRIu16
767 ", wanted %"PRIu16,
768 hdr->numberio, io_u->numberio);
769 goto err;
770 }
ae38c0dc 771
25dfa848 772 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
5964842c
AG
773 if (crc != hdr->crc32) {
774 log_err("verify: bad header crc %x, calculated %x",
775 hdr->crc32, crc);
776 goto err;
777 }
778 return 0;
779
780err:
781 log_err(" at file %s offset %llu, length %u\n",
782 io_u->file->file_name,
783 io_u->offset + hdr_num * hdr_len, hdr_len);
16758b59
JA
784
785 if (td->o.verify_dump)
786 dump_buf(p, hdr_len, io_u->offset + hdr_num * hdr_len,
787 "hdr_fail", io_u->file);
788
5964842c 789 return EILSEQ;
f65d1c26
JA
790}
791
f8b0bd10 792int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
e29d1b70 793{
3f9f4e26 794 struct verify_header *hdr;
f8b0bd10 795 struct io_u *io_u = *io_u_ptr;
2b13e716 796 unsigned int header_size, hdr_inc, hdr_num = 0;
95646108 797 void *p;
e29d1b70
JA
798 int ret;
799
1dcc0498 800 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b 801 return 0;
5c57c084
JA
802 /*
803 * If the IO engine is faking IO (like null), then just pretend
804 * we verified everything.
805 */
9b87f09b 806 if (td_ioengine_flagged(td, FIO_FAKEIO))
5c57c084
JA
807 return 0;
808
0d29de83
JA
809 if (io_u->flags & IO_U_F_TRIMMED) {
810 ret = verify_trimmed_io_u(td, io_u);
811 goto done;
812 }
36690c9b 813
cfbcd0dc 814 hdr_inc = get_hdr_inc(td, io_u);
e29d1b70 815
a12a3b4d 816 ret = 0;
5ec10eaa
JA
817 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
818 p += hdr_inc, hdr_num++) {
bfacf389
JA
819 struct vcont vc = {
820 .io_u = io_u,
821 .hdr_num = hdr_num,
7d9fb455 822 .td = td,
bfacf389 823 };
f00b210f 824 unsigned int verify_type;
936216f8 825
f3e6cb95 826 if (ret && td->o.verify_fatal)
a12a3b4d 827 break;
f3e6cb95 828
2b13e716 829 header_size = __hdr_size(td->o.verify);
a59e170d 830 if (td->o.verify_offset)
2b13e716 831 memswp(p, p + td->o.verify_offset, header_size);
95646108 832 hdr = p;
e29d1b70 833
c4b6117b
PV
834 /*
835 * Make rand_seed check pass when have verifysort or
836 * verify_backlog.
837 */
838 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
839 io_u->rand_seed = hdr->rand_seed;
840
59245381 841 if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
b638d82f 842 ret = verify_header(io_u, td, hdr, hdr_num, hdr_inc);
59245381
JA
843 if (ret)
844 return ret;
845 }
3f199b01 846
f00b210f
JA
847 if (td->o.verify != VERIFY_NONE)
848 verify_type = td->o.verify;
849 else
850 verify_type = hdr->verify_type;
851
852 switch (verify_type) {
b638d82f
RP
853 case VERIFY_HDR_ONLY:
854 /* Header is always verified, check if pattern is left
855 * for verification. */
856 if (td->o.verify_pattern_bytes)
857 ret = verify_io_u_pattern(hdr, &vc);
858 break;
3f9f4e26 859 case VERIFY_MD5:
936216f8 860 ret = verify_io_u_md5(hdr, &vc);
3f9f4e26
SL
861 break;
862 case VERIFY_CRC64:
936216f8 863 ret = verify_io_u_crc64(hdr, &vc);
3f9f4e26 864 break;
bac39e0e 865 case VERIFY_CRC32C:
3845591f 866 case VERIFY_CRC32C_INTEL:
936216f8 867 ret = verify_io_u_crc32c(hdr, &vc);
bac39e0e 868 break;
3f9f4e26 869 case VERIFY_CRC32:
936216f8 870 ret = verify_io_u_crc32(hdr, &vc);
3f9f4e26
SL
871 break;
872 case VERIFY_CRC16:
936216f8 873 ret = verify_io_u_crc16(hdr, &vc);
3f9f4e26
SL
874 break;
875 case VERIFY_CRC7:
936216f8 876 ret = verify_io_u_crc7(hdr, &vc);
3f9f4e26 877 break;
cd14cc10 878 case VERIFY_SHA256:
936216f8 879 ret = verify_io_u_sha256(hdr, &vc);
cd14cc10
JA
880 break;
881 case VERIFY_SHA512:
936216f8 882 ret = verify_io_u_sha512(hdr, &vc);
cd14cc10 883 break;
844ea602
JA
884 case VERIFY_XXHASH:
885 ret = verify_io_u_xxhash(hdr, &vc);
886 break;
7c353ceb 887 case VERIFY_SHA1:
936216f8 888 ret = verify_io_u_sha1(hdr, &vc);
7c353ceb 889 break;
92bf48d5 890 case VERIFY_PATTERN:
59245381 891 case VERIFY_PATTERN_NO_HDR:
92bf48d5
JA
892 ret = verify_io_u_pattern(hdr, &vc);
893 break;
3f9f4e26
SL
894 default:
895 log_err("Bad verify type %u\n", hdr->verify_type);
d16d4e09 896 ret = EINVAL;
3f9f4e26 897 }
f00b210f
JA
898
899 if (ret && verify_type != hdr->verify_type)
900 log_err("fio: verify type mismatch (%u media, %u given)\n",
901 hdr->verify_type, verify_type);
3f9f4e26 902 }
a7dfe862 903
0d29de83 904done:
f3e6cb95 905 if (ret && td->o.verify_fatal)
ebea2133 906 fio_mark_td_terminate(td);
f3e6cb95 907
a12a3b4d 908 return ret;
e29d1b70
JA
909}
910
844ea602
JA
911static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
912{
913 struct vhdr_xxhash *vh = hdr_priv(hdr);
914 void *state;
915
916 state = XXH32_init(1);
917 XXH32_update(state, p, len);
918 vh->hash = XXH32_digest(state);
919}
920
cd14cc10
JA
921static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
922{
546dfd9f 923 struct vhdr_sha512 *vh = hdr_priv(hdr);
25dfa848 924 struct fio_sha512_ctx sha512_ctx = {
546dfd9f 925 .buf = vh->sha512,
cd14cc10
JA
926 };
927
25dfa848
JA
928 fio_sha512_init(&sha512_ctx);
929 fio_sha512_update(&sha512_ctx, p, len);
cd14cc10
JA
930}
931
932static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
933{
546dfd9f 934 struct vhdr_sha256 *vh = hdr_priv(hdr);
25dfa848 935 struct fio_sha256_ctx sha256_ctx = {
546dfd9f 936 .buf = vh->sha256,
cd14cc10
JA
937 };
938
25dfa848
JA
939 fio_sha256_init(&sha256_ctx);
940 fio_sha256_update(&sha256_ctx, p, len);
017052b6 941 fio_sha256_final(&sha256_ctx);
cd14cc10
JA
942}
943
7c353ceb
JA
944static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
945{
946 struct vhdr_sha1 *vh = hdr_priv(hdr);
25dfa848 947 struct fio_sha1_ctx sha1_ctx = {
7c353ceb
JA
948 .H = vh->sha1,
949 };
950
25dfa848
JA
951 fio_sha1_init(&sha1_ctx);
952 fio_sha1_update(&sha1_ctx, p, len);
017052b6 953 fio_sha1_final(&sha1_ctx);
7c353ceb
JA
954}
955
1e154bdb
JA
956static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
957{
546dfd9f
JA
958 struct vhdr_crc7 *vh = hdr_priv(hdr);
959
25dfa848 960 vh->crc7 = fio_crc7(p, len);
1e154bdb
JA
961}
962
969f7ed3
JA
963static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
964{
546dfd9f
JA
965 struct vhdr_crc16 *vh = hdr_priv(hdr);
966
25dfa848 967 vh->crc16 = fio_crc16(p, len);
969f7ed3
JA
968}
969
e29d1b70
JA
970static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
971{
546dfd9f
JA
972 struct vhdr_crc32 *vh = hdr_priv(hdr);
973
25dfa848 974 vh->crc32 = fio_crc32(p, len);
e29d1b70
JA
975}
976
bac39e0e
JA
977static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
978{
979 struct vhdr_crc32 *vh = hdr_priv(hdr);
980
25dfa848 981 vh->crc32 = fio_crc32c(p, len);
bac39e0e
JA
982}
983
d77a7af3
JA
984static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
985{
546dfd9f
JA
986 struct vhdr_crc64 *vh = hdr_priv(hdr);
987
25dfa848 988 vh->crc64 = fio_crc64(p, len);
d77a7af3
JA
989}
990
e29d1b70
JA
991static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
992{
546dfd9f 993 struct vhdr_md5 *vh = hdr_priv(hdr);
25dfa848 994 struct fio_md5_ctx md5_ctx = {
546dfd9f 995 .hash = (uint32_t *) vh->md5_digest,
8c432325 996 };
e29d1b70 997
25dfa848
JA
998 fio_md5_init(&md5_ctx);
999 fio_md5_update(&md5_ctx, p, len);
017052b6 1000 fio_md5_final(&md5_ctx);
e29d1b70
JA
1001}
1002
b638d82f
RP
1003static void __fill_hdr(struct thread_data *td, struct io_u *io_u,
1004 struct verify_header *hdr, unsigned int header_num,
1005 unsigned int header_len, uint64_t rand_seed)
9ba987cc
JA
1006{
1007 void *p = hdr;
1008
1009 hdr->magic = FIO_HDR_MAGIC;
b638d82f
RP
1010 hdr->verify_type = td->o.verify;
1011 hdr->len = header_len;
9ba987cc 1012 hdr->rand_seed = rand_seed;
b638d82f
RP
1013 hdr->offset = io_u->offset + header_num * td->o.verify_interval;
1014 hdr->time_sec = io_u->start_time.tv_sec;
1015 hdr->time_usec = io_u->start_time.tv_usec;
1016 hdr->thread = td->thread_number;
1017 hdr->numberio = io_u->numberio;
9ba987cc
JA
1018 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
1019}
1020
ed3a433d 1021
b638d82f
RP
1022static void fill_hdr(struct thread_data *td, struct io_u *io_u,
1023 struct verify_header *hdr, unsigned int header_num,
1024 unsigned int header_len, uint64_t rand_seed)
ed3a433d 1025{
b638d82f
RP
1026
1027 if (td->o.verify != VERIFY_PATTERN_NO_HDR)
1028 __fill_hdr(td, io_u, hdr, header_num, header_len, rand_seed);
ed3a433d
JA
1029}
1030
7d9fb455
JA
1031static void populate_hdr(struct thread_data *td, struct io_u *io_u,
1032 struct verify_header *hdr, unsigned int header_num,
1033 unsigned int header_len)
1034{
1035 unsigned int data_len;
1036 void *data, *p;
1037
1038 p = (void *) hdr;
1039
b638d82f 1040 fill_hdr(td, io_u, hdr, header_num, header_len, io_u->rand_seed);
f65d1c26 1041
ed3a433d 1042 data_len = header_len - hdr_size(td, hdr);
7d9fb455 1043
ed3a433d 1044 data = p + hdr_size(td, hdr);
7d9fb455
JA
1045 switch (td->o.verify) {
1046 case VERIFY_MD5:
1047 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1048 io_u, hdr->len);
1049 fill_md5(hdr, data, data_len);
1050 break;
1051 case VERIFY_CRC64:
1052 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1053 io_u, hdr->len);
1054 fill_crc64(hdr, data, data_len);
1055 break;
1056 case VERIFY_CRC32C:
1057 case VERIFY_CRC32C_INTEL:
1058 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1059 io_u, hdr->len);
1060 fill_crc32c(hdr, data, data_len);
1061 break;
1062 case VERIFY_CRC32:
1063 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1064 io_u, hdr->len);
1065 fill_crc32(hdr, data, data_len);
1066 break;
1067 case VERIFY_CRC16:
1068 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1069 io_u, hdr->len);
1070 fill_crc16(hdr, data, data_len);
1071 break;
1072 case VERIFY_CRC7:
1073 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1074 io_u, hdr->len);
1075 fill_crc7(hdr, data, data_len);
1076 break;
1077 case VERIFY_SHA256:
1078 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1079 io_u, hdr->len);
1080 fill_sha256(hdr, data, data_len);
1081 break;
1082 case VERIFY_SHA512:
1083 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1084 io_u, hdr->len);
1085 fill_sha512(hdr, data, data_len);
1086 break;
844ea602
JA
1087 case VERIFY_XXHASH:
1088 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1089 io_u, hdr->len);
1090 fill_xxhash(hdr, data, data_len);
1091 break;
7d9fb455
JA
1092 case VERIFY_SHA1:
1093 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1094 io_u, hdr->len);
1095 fill_sha1(hdr, data, data_len);
1096 break;
b638d82f 1097 case VERIFY_HDR_ONLY:
92bf48d5 1098 case VERIFY_PATTERN:
ed3a433d 1099 case VERIFY_PATTERN_NO_HDR:
92bf48d5
JA
1100 /* nothing to do here */
1101 break;
7d9fb455
JA
1102 default:
1103 log_err("fio: bad verify type: %d\n", td->o.verify);
1104 assert(0);
1105 }
ed3a433d
JA
1106
1107 if (td->o.verify_offset && hdr_size(td, hdr))
1108 memswp(p, p + td->o.verify_offset, hdr_size(td, hdr));
7d9fb455
JA
1109}
1110
e29d1b70
JA
1111/*
1112 * fill body of io_u->buf with random data and add a header with the
c50ca7bd 1113 * checksum of choice
e29d1b70
JA
1114 */
1115void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1116{
9cc3d150
JA
1117 if (td->o.verify == VERIFY_NULL)
1118 return;
1119
da0a7bd2
JC
1120 io_u->numberio = td->io_issues[io_u->ddir];
1121
c50ca7bd 1122 fill_pattern_headers(td, io_u, 0, 0);
e29d1b70
JA
1123}
1124
1125int get_next_verify(struct thread_data *td, struct io_u *io_u)
1126{
8de8f047 1127 struct io_piece *ipo = NULL;
e29d1b70 1128
d2d7fa53
JA
1129 /*
1130 * this io_u is from a requeue, we already filled the offsets
1131 */
1132 if (io_u->file)
1133 return 0;
1134
8de8f047
JA
1135 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1136 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 1137
8de8f047 1138 ipo = rb_entry(n, struct io_piece, rb_node);
f9401285
JA
1139
1140 /*
1141 * Ensure that the associated IO has completed
1142 */
1143 read_barrier();
1144 if (ipo->flags & IP_F_IN_FLIGHT)
1145 goto nothing;
1146
4b87898e 1147 rb_erase(n, &td->io_hist_tree);
a917a8b3
JA
1148 assert(ipo->flags & IP_F_ONRB);
1149 ipo->flags &= ~IP_F_ONRB;
01743ee1 1150 } else if (!flist_empty(&td->io_hist_list)) {
9342d5f8 1151 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
f9401285
JA
1152
1153 /*
1154 * Ensure that the associated IO has completed
1155 */
1156 read_barrier();
1157 if (ipo->flags & IP_F_IN_FLIGHT)
1158 goto nothing;
1159
01743ee1 1160 flist_del(&ipo->list);
a917a8b3
JA
1161 assert(ipo->flags & IP_F_ONLIST);
1162 ipo->flags &= ~IP_F_ONLIST;
8de8f047 1163 }
e29d1b70 1164
8de8f047 1165 if (ipo) {
0d29de83
JA
1166 td->io_hist_len--;
1167
e29d1b70
JA
1168 io_u->offset = ipo->offset;
1169 io_u->buflen = ipo->len;
da0a7bd2 1170 io_u->numberio = ipo->numberio;
36167d82 1171 io_u->file = ipo->file;
1651e431 1172 io_u_set(td, io_u, IO_U_F_VER_LIST);
97af62ce 1173
0d29de83 1174 if (ipo->flags & IP_F_TRIMMED)
1651e431 1175 io_u_set(td, io_u, IO_U_F_TRIMMED);
0d29de83 1176
d6aed795 1177 if (!fio_file_open(io_u->file)) {
97af62ce
JA
1178 int r = td_io_open_file(td, io_u->file);
1179
bd6f78b2
JA
1180 if (r) {
1181 dprint(FD_VERIFY, "failed file %s open\n",
1182 io_u->file->file_name);
97af62ce 1183 return 1;
bd6f78b2 1184 }
97af62ce
JA
1185 }
1186
1187 get_file(ipo->file);
d6aed795 1188 assert(fio_file_open(io_u->file));
e29d1b70 1189 io_u->ddir = DDIR_READ;
36167d82
JA
1190 io_u->xfer_buf = io_u->buf;
1191 io_u->xfer_buflen = io_u->buflen;
0d29de83
JA
1192
1193 remove_trim_entry(td, ipo);
e29d1b70 1194 free(ipo);
bd6f78b2 1195 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
c4b6117b
PV
1196
1197 if (!td->o.verify_pattern_bytes) {
d6b72507 1198 io_u->rand_seed = __rand(&td->verify_state);
c4b6117b 1199 if (sizeof(int) != sizeof(long *))
d6b72507 1200 io_u->rand_seed *= __rand(&td->verify_state);
c4b6117b 1201 }
e29d1b70
JA
1202 return 0;
1203 }
1204
f9401285 1205nothing:
bd6f78b2 1206 dprint(FD_VERIFY, "get_next_verify: empty\n");
e29d1b70
JA
1207 return 1;
1208}
e8462bd8 1209
dc5bfbb2
JA
1210void fio_verify_init(struct thread_data *td)
1211{
1212 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1213 td->o.verify == VERIFY_CRC32C) {
1214 crc32c_intel_probe();
1215 }
1216}
1217
e8462bd8
JA
1218static void *verify_async_thread(void *data)
1219{
1220 struct thread_data *td = data;
1221 struct io_u *io_u;
1222 int ret = 0;
1223
b2a9e649 1224 if (fio_option_is_set(&td->o, verify_cpumask) &&
e8462bd8
JA
1225 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1226 log_err("fio: failed setting verify thread affinity\n");
1227 goto done;
1228 }
1229
1230 do {
e53ab27c
JA
1231 FLIST_HEAD(list);
1232
e8462bd8
JA
1233 read_barrier();
1234 if (td->verify_thread_exit)
1235 break;
1236
1237 pthread_mutex_lock(&td->io_u_lock);
1238
1239 while (flist_empty(&td->verify_list) &&
1240 !td->verify_thread_exit) {
b36e298b
JA
1241 ret = pthread_cond_wait(&td->verify_cond,
1242 &td->io_u_lock);
e8462bd8
JA
1243 if (ret) {
1244 pthread_mutex_unlock(&td->io_u_lock);
1245 break;
1246 }
1247 }
1248
e53ab27c
JA
1249 flist_splice_init(&td->verify_list, &list);
1250 pthread_mutex_unlock(&td->io_u_lock);
1251
1252 if (flist_empty(&list))
e8462bd8 1253 continue;
e8462bd8 1254
e53ab27c 1255 while (!flist_empty(&list)) {
9342d5f8 1256 io_u = flist_first_entry(&list, struct io_u, verify_list);
f8b0bd10
JA
1257 flist_del_init(&io_u->verify_list);
1258
1651e431 1259 io_u_set(td, io_u, IO_U_F_NO_FILE_PUT);
f8b0bd10 1260 ret = verify_io_u(td, &io_u);
e8462bd8 1261
e53ab27c 1262 put_io_u(td, io_u);
d561f2ab
JA
1263 if (!ret)
1264 continue;
8b28bd41 1265 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
d561f2ab
JA
1266 update_error_count(td, ret);
1267 td_clear_error(td);
1268 ret = 0;
1269 }
e53ab27c 1270 }
e8462bd8
JA
1271 } while (!ret);
1272
d561f2ab
JA
1273 if (ret) {
1274 td_verror(td, ret, "async_verify");
f3e6cb95 1275 if (td->o.verify_fatal)
ebea2133 1276 fio_mark_td_terminate(td);
d561f2ab
JA
1277 }
1278
e8462bd8
JA
1279done:
1280 pthread_mutex_lock(&td->io_u_lock);
1281 td->nr_verify_threads--;
1282 pthread_mutex_unlock(&td->io_u_lock);
1283
1284 pthread_cond_signal(&td->free_cond);
1285 return NULL;
1286}
1287
1288int verify_async_init(struct thread_data *td)
1289{
1290 int i, ret;
304a47c7
VA
1291 pthread_attr_t attr;
1292
1293 pthread_attr_init(&attr);
45213f1b 1294 pthread_attr_setstacksize(&attr, 2 * PTHREAD_STACK_MIN);
e8462bd8
JA
1295
1296 td->verify_thread_exit = 0;
1297
1298 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1299 for (i = 0; i < td->o.verify_async; i++) {
304a47c7 1300 ret = pthread_create(&td->verify_threads[i], &attr,
e8462bd8
JA
1301 verify_async_thread, td);
1302 if (ret) {
1303 log_err("fio: async verify creation failed: %s\n",
1304 strerror(ret));
1305 break;
1306 }
1307 ret = pthread_detach(td->verify_threads[i]);
1308 if (ret) {
1309 log_err("fio: async verify thread detach failed: %s\n",
1310 strerror(ret));
1311 break;
1312 }
1313 td->nr_verify_threads++;
1314 }
1315
304a47c7
VA
1316 pthread_attr_destroy(&attr);
1317
e8462bd8 1318 if (i != td->o.verify_async) {
e40823b1 1319 log_err("fio: only %d verify threads started, exiting\n", i);
e8462bd8
JA
1320 td->verify_thread_exit = 1;
1321 write_barrier();
1322 pthread_cond_broadcast(&td->verify_cond);
1323 return 1;
1324 }
1325
1326 return 0;
1327}
1328
1329void verify_async_exit(struct thread_data *td)
1330{
1331 td->verify_thread_exit = 1;
1332 write_barrier();
1333 pthread_cond_broadcast(&td->verify_cond);
1334
1335 pthread_mutex_lock(&td->io_u_lock);
1336
1337 while (td->nr_verify_threads)
1338 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1339
1340 pthread_mutex_unlock(&td->io_u_lock);
1341 free(td->verify_threads);
1342 td->verify_threads = NULL;
1343}
ca09be4b 1344
61b9861d
RP
1345int paste_blockoff(char *buf, unsigned int len, void *priv)
1346{
1347 struct io_u *io = priv;
1348 unsigned long long off;
1349
1350 typecheck(typeof(off), io->offset);
1351 off = cpu_to_le64((uint64_t)io->offset);
1352 len = min(len, (unsigned int)sizeof(off));
1353 memcpy(buf, &off, len);
1354 return 0;
1355}
1356
94a6e1bb
JA
1357static int __fill_file_completions(struct thread_data *td,
1358 struct thread_io_list *s,
1359 struct fio_file *f, unsigned int *index)
1360{
1361 unsigned int comps;
1362 int i, j;
1363
1364 if (!f->last_write_comp)
1365 return 0;
1366
1367 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1368 comps = td->io_blocks[DDIR_WRITE];
1369 else
1370 comps = td->o.iodepth;
1371
1372 j = f->last_write_idx - 1;
1373 for (i = 0; i < comps; i++) {
1374 if (j == -1)
1375 j = td->o.iodepth - 1;
1376 s->comps[*index].fileno = __cpu_to_le64(f->fileno);
1377 s->comps[*index].offset = cpu_to_le64(f->last_write_comp[j]);
1378 (*index)++;
1379 j--;
1380 }
1381
1382 return comps;
1383}
1384
1385static int fill_file_completions(struct thread_data *td,
1386 struct thread_io_list *s, unsigned int *index)
1387{
1388 struct fio_file *f;
1389 unsigned int i;
1390 int comps = 0;
1391
1392 for_each_file(td, f, i)
1393 comps += __fill_file_completions(td, s, f, index);
1394
1395 return comps;
1396}
1397
ca09be4b
JA
1398struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1399{
1400 struct all_io_list *rep;
1401 struct thread_data *td;
1402 size_t depth;
1403 void *next;
1404 int i, nr;
1405
1406 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1407
1408 /*
1409 * Calculate reply space needed. We need one 'io_state' per thread,
1410 * and the size will vary depending on depth.
1411 */
1412 depth = 0;
1413 nr = 0;
1414 for_each_td(td, i) {
1415 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1416 continue;
1417 td->stop_io = 1;
1418 td->flags |= TD_F_VSTATE_SAVED;
94a6e1bb 1419 depth += (td->o.iodepth * td->o.nr_files);
ca09be4b
JA
1420 nr++;
1421 }
1422
1423 if (!nr)
1424 return NULL;
1425
1426 *sz = sizeof(*rep);
1427 *sz += nr * sizeof(struct thread_io_list);
94a6e1bb 1428 *sz += depth * sizeof(struct file_comp);
ca09be4b 1429 rep = malloc(*sz);
c68ce5ea 1430 memset(rep, 0, *sz);
ca09be4b
JA
1431
1432 rep->threads = cpu_to_le64((uint64_t) nr);
1433
1434 next = &rep->state[0];
1435 for_each_td(td, i) {
1436 struct thread_io_list *s = next;
94a6e1bb 1437 unsigned int comps, index = 0;
ca09be4b
JA
1438
1439 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1440 continue;
1441
94a6e1bb 1442 comps = fill_file_completions(td, s, &index);
ca09be4b
JA
1443
1444 s->no_comps = cpu_to_le64((uint64_t) comps);
1445 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
94a6e1bb 1446 s->nofiles = cpu_to_le64((uint64_t) td->o.nr_files);
ca09be4b
JA
1447 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1448 s->index = cpu_to_le64((uint64_t) i);
c3546b53
JA
1449 if (td->random_state.use64) {
1450 s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
1451 s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
1452 s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
1453 s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
1454 s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
1455 s->rand.state64.s[5] = 0;
1456 s->rand.use64 = cpu_to_le64((uint64_t)1);
1457 } else {
1458 s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
1459 s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
1460 s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
1461 s->rand.state32.s[3] = 0;
1462 s->rand.use64 = 0;
1463 }
1dc06a8f
JA
1464 s->name[sizeof(s->name) - 1] = '\0';
1465 strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
ca09be4b
JA
1466 next = io_list_next(s);
1467 }
1468
1469 return rep;
1470}
1471
1472static int open_state_file(const char *name, const char *prefix, int num,
1473 int for_write)
1474{
d117b2bd 1475 char out[PATH_MAX];
ca09be4b
JA
1476 int flags;
1477 int fd;
1478
1479 if (for_write)
1480 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1481 else
1482 flags = O_RDONLY;
1483
e499aedc 1484 verify_state_gen_name(out, sizeof(out), name, prefix, num);
ca09be4b
JA
1485
1486 fd = open(out, flags, 0644);
1487 if (fd == -1) {
1488 perror("fio: open state file");
d117b2bd 1489 log_err("fio: state file: %s (for_write=%d)\n", out, for_write);
ca09be4b
JA
1490 return -1;
1491 }
1492
1493 return fd;
1494}
1495
1496static int write_thread_list_state(struct thread_io_list *s,
1497 const char *prefix)
1498{
1499 struct verify_state_hdr hdr;
1500 uint64_t crc;
1501 ssize_t ret;
1502 int fd;
1503
1504 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1505 if (fd == -1)
1506 return 1;
1507
1508 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1509
1510 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1511 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1512 hdr.crc = cpu_to_le64(crc);
1513 ret = write(fd, &hdr, sizeof(hdr));
1514 if (ret != sizeof(hdr))
1515 goto write_fail;
1516
1517 ret = write(fd, s, thread_io_list_sz(s));
1518 if (ret != thread_io_list_sz(s)) {
1519write_fail:
1520 if (ret < 0)
1521 perror("fio: write state file");
1522 log_err("fio: failed to write state file\n");
1523 ret = 1;
1524 } else
1525 ret = 0;
1526
1527 close(fd);
1528 return ret;
1529}
1530
1531void __verify_save_state(struct all_io_list *state, const char *prefix)
1532{
1533 struct thread_io_list *s = &state->state[0];
1534 unsigned int i;
1535
1536 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1537 write_thread_list_state(s, prefix);
1538 s = io_list_next(s);
1539 }
1540}
1541
d264264a 1542void verify_save_state(int mask)
ca09be4b
JA
1543{
1544 struct all_io_list *state;
1545 size_t sz;
1546
d264264a 1547 state = get_all_io_list(mask, &sz);
ca09be4b 1548 if (state) {
d264264a
JA
1549 char prefix[PATH_MAX];
1550
1551 if (aux_path)
c6d140fb 1552 sprintf(prefix, "%s%slocal", aux_path, FIO_OS_PATH_SEPARATOR);
d264264a 1553 else
c6d140fb 1554 strcpy(prefix, "local");
d264264a
JA
1555
1556 __verify_save_state(state, prefix);
ca09be4b
JA
1557 free(state);
1558 }
1559}
1560
1561void verify_free_state(struct thread_data *td)
1562{
1563 if (td->vstate)
1564 free(td->vstate);
1565}
1566
94a6e1bb 1567void verify_assign_state(struct thread_data *td, void *p)
ca09be4b 1568{
94a6e1bb 1569 struct thread_io_list *s = p;
ca09be4b
JA
1570 int i;
1571
94a6e1bb
JA
1572 s->no_comps = le64_to_cpu(s->no_comps);
1573 s->depth = le32_to_cpu(s->depth);
1574 s->nofiles = le32_to_cpu(s->nofiles);
1575 s->numberio = le64_to_cpu(s->numberio);
1576 s->rand.use64 = le64_to_cpu(s->rand.use64);
c3546b53 1577
94a6e1bb
JA
1578 if (s->rand.use64) {
1579 for (i = 0; i < 6; i++)
1580 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
c3546b53 1581 } else {
94a6e1bb
JA
1582 for (i = 0; i < 4; i++)
1583 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1584 }
c3546b53 1585
94a6e1bb
JA
1586 for (i = 0; i < s->no_comps; i++) {
1587 s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno);
1588 s->comps[i].offset = le64_to_cpu(s->comps[i].offset);
c3546b53 1589 }
ca09be4b 1590
94a6e1bb 1591 td->vstate = p;
ca09be4b
JA
1592}
1593
94a6e1bb 1594int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s)
ca09be4b
JA
1595{
1596 uint64_t crc;
1597
1598 hdr->version = le64_to_cpu(hdr->version);
1599 hdr->size = le64_to_cpu(hdr->size);
1600 hdr->crc = le64_to_cpu(hdr->crc);
1601
94a6e1bb 1602 if (hdr->version != VSTATE_HDR_VERSION)
ca09be4b
JA
1603 return 1;
1604
1605 crc = fio_crc32c((void *)s, hdr->size);
1606 if (crc != hdr->crc)
1607 return 1;
1608
1609 return 0;
1610}
1611
1612int verify_load_state(struct thread_data *td, const char *prefix)
1613{
ca09be4b 1614 struct verify_state_hdr hdr;
c3546b53 1615 void *s = NULL;
ca09be4b
JA
1616 uint64_t crc;
1617 ssize_t ret;
1618 int fd;
1619
1620 if (!td->o.verify_state)
1621 return 0;
1622
1623 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1624 if (fd == -1)
1625 return 1;
1626
1627 ret = read(fd, &hdr, sizeof(hdr));
1628 if (ret != sizeof(hdr)) {
1629 if (ret < 0)
1630 td_verror(td, errno, "read verify state hdr");
1631 log_err("fio: failed reading verify state header\n");
1632 goto err;
1633 }
1634
1635 hdr.version = le64_to_cpu(hdr.version);
1636 hdr.size = le64_to_cpu(hdr.size);
1637 hdr.crc = le64_to_cpu(hdr.crc);
1638
94a6e1bb
JA
1639 if (hdr.version != VSTATE_HDR_VERSION) {
1640 log_err("fio: unsupported (%d) version in verify state header\n",
1641 (unsigned int) hdr.version);
ca09be4b
JA
1642 goto err;
1643 }
1644
1645 s = malloc(hdr.size);
1646 ret = read(fd, s, hdr.size);
1647 if (ret != hdr.size) {
1648 if (ret < 0)
1649 td_verror(td, errno, "read verify state");
1650 log_err("fio: failed reading verity state\n");
1651 goto err;
1652 }
1653
c3546b53 1654 crc = fio_crc32c(s, hdr.size);
ca09be4b
JA
1655 if (crc != hdr.crc) {
1656 log_err("fio: verify state is corrupt\n");
1657 goto err;
1658 }
1659
1660 close(fd);
1661
94a6e1bb 1662 verify_assign_state(td, s);
ca09be4b
JA
1663 return 0;
1664err:
1665 if (s)
1666 free(s);
1667 close(fd);
1668 return 1;
1669}
1670
1671/*
1672 * Use the loaded verify state to know when to stop doing verification
1673 */
1674int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1675{
1676 struct thread_io_list *s = td->vstate;
94a6e1bb 1677 struct fio_file *f = io_u->file;
ca09be4b
JA
1678 int i;
1679
94a6e1bb 1680 if (!s || !f)
ca09be4b
JA
1681 return 0;
1682
1683 /*
def00095
JA
1684 * If we're not into the window of issues - depth yet, continue. If
1685 * issue is shorter than depth, do check.
ca09be4b 1686 */
def00095
JA
1687 if ((td->io_blocks[DDIR_READ] < s->depth ||
1688 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1689 s->numberio > s->depth)
ca09be4b
JA
1690 return 0;
1691
1692 /*
1693 * We're in the window of having to check if this io was
1694 * completed or not. If the IO was seen as completed, then
1695 * lets verify it.
1696 */
94a6e1bb
JA
1697 for (i = 0; i < s->no_comps; i++) {
1698 if (s->comps[i].fileno != f->fileno)
1699 continue;
1700 if (io_u->offset == s->comps[i].offset)
ca09be4b 1701 return 0;
94a6e1bb 1702 }
ca09be4b
JA
1703
1704 /*
1705 * Not found, we have to stop
1706 */
1707 return 1;
1708}