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