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