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