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