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