When verify fails, dump the good/bad blocks to files
[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>
e29d1b70
JA
9
10#include "fio.h"
4f5af7b2 11#include "verify.h"
e8462bd8 12#include "smalloc.h"
0d29de83 13#include "trim.h"
637ef8d9 14#include "lib/rand.h"
e29d1b70 15
eef6eea1
JA
16#include "crc/md5.h"
17#include "crc/crc64.h"
18#include "crc/crc32.h"
bac39e0e 19#include "crc/crc32c.h"
eef6eea1
JA
20#include "crc/crc16.h"
21#include "crc/crc7.h"
22#include "crc/sha256.h"
23#include "crc/sha512.h"
7c353ceb 24#include "crc/sha1.h"
cd14cc10 25
7d9fb455
JA
26static void populate_hdr(struct thread_data *td, struct io_u *io_u,
27 struct verify_header *hdr, unsigned int header_num,
28 unsigned int header_len);
29
30void fill_pattern(struct thread_data *td, void *p, unsigned int len, struct io_u *io_u, unsigned long seed, int use_seed)
90059d65
JA
31{
32 switch (td->o.verify_pattern_bytes) {
33 case 0:
bd6f78b2 34 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
7d9fb455
JA
35 if (use_seed)
36 __fill_random_buf(p, len, seed);
37 else
38 io_u->rand_seed = fill_random_buf(p, len);
90059d65
JA
39 break;
40 case 1:
10e8a7b3 41 if (io_u->buf_filled_len >= len) {
cbe8d756
RR
42 dprint(FD_VERIFY, "using already filled verify pattern b=0 len=%u\n", len);
43 return;
44 }
bd6f78b2 45 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
0e92f873 46 memset(p, td->o.verify_pattern[0], len);
cbe8d756 47 io_u->buf_filled_len = len;
90059d65 48 break;
0e92f873
RR
49 default: {
50 unsigned int i = 0, size = 0;
90059d65
JA
51 unsigned char *b = p;
52
10e8a7b3 53 if (io_u->buf_filled_len >= len) {
cbe8d756
RR
54 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
55 td->o.verify_pattern_bytes, len);
56 return;
57 }
bd6f78b2
JA
58 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
59 td->o.verify_pattern_bytes, len);
60
90059d65 61 while (i < len) {
0e92f873
RR
62 size = td->o.verify_pattern_bytes;
63 if (size > (len - i))
64 size = len - i;
65 memcpy(b+i, td->o.verify_pattern, size);
66 i += size;
90059d65 67 }
cbe8d756 68 io_u->buf_filled_len = len;
90059d65
JA
69 break;
70 }
71 }
72}
73
4764aec9 74static void memswp(void *buf1, void *buf2, unsigned int len)
546a9142 75{
dee6de74
SL
76 char swap[200];
77
78 assert(len <= sizeof(swap));
90059d65 79
546a9142
SL
80 memcpy(&swap, buf1, len);
81 memcpy(buf1, buf2, len);
82 memcpy(buf2, &swap, len);
83}
84
e29d1b70
JA
85static void hexdump(void *buffer, int len)
86{
87 unsigned char *p = buffer;
88 int i;
89
90 for (i = 0; i < len; i++)
bfacf389
JA
91 log_err("%02x", p[i]);
92 log_err("\n");
e29d1b70
JA
93}
94
87677832
JA
95/*
96 * Prepare for seperation of verify_header and checksum header
97 */
546dfd9f 98static inline unsigned int __hdr_size(int verify_type)
87677832 99{
03e20d68 100 unsigned int len = 0;
87677832 101
546dfd9f
JA
102 switch (verify_type) {
103 case VERIFY_NONE:
104 case VERIFY_NULL:
105 len = 0;
106 break;
107 case VERIFY_MD5:
108 len = sizeof(struct vhdr_md5);
109 break;
110 case VERIFY_CRC64:
111 len = sizeof(struct vhdr_crc64);
112 break;
bac39e0e 113 case VERIFY_CRC32C:
546dfd9f 114 case VERIFY_CRC32:
3845591f 115 case VERIFY_CRC32C_INTEL:
546dfd9f
JA
116 len = sizeof(struct vhdr_crc32);
117 break;
118 case VERIFY_CRC16:
119 len = sizeof(struct vhdr_crc16);
120 break;
121 case VERIFY_CRC7:
122 len = sizeof(struct vhdr_crc7);
123 break;
124 case VERIFY_SHA256:
125 len = sizeof(struct vhdr_sha256);
126 break;
127 case VERIFY_SHA512:
128 len = sizeof(struct vhdr_sha512);
129 break;
7437ee87
SL
130 case VERIFY_META:
131 len = sizeof(struct vhdr_meta);
132 break;
7c353ceb
JA
133 case VERIFY_SHA1:
134 len = sizeof(struct vhdr_sha1);
135 break;
546dfd9f
JA
136 default:
137 log_err("fio: unknown verify header!\n");
138 assert(0);
139 }
140
141 return len + sizeof(struct verify_header);
87677832
JA
142}
143
144static inline unsigned int hdr_size(struct verify_header *hdr)
145{
546dfd9f
JA
146 return __hdr_size(hdr->verify_type);
147}
148
149static void *hdr_priv(struct verify_header *hdr)
150{
151 void *priv = hdr;
152
153 return priv + sizeof(struct verify_header);
87677832
JA
154}
155
936216f8
JA
156/*
157 * Verify container, pass info to verify handlers and allow them to
158 * pass info back in case of error
159 */
160struct vcont {
161 /*
162 * Input
163 */
164 struct io_u *io_u;
165 unsigned int hdr_num;
7d9fb455 166 struct thread_data *td;
936216f8
JA
167
168 /*
169 * Output, only valid in case of error
170 */
bfacf389
JA
171 const char *name;
172 void *good_crc;
173 void *bad_crc;
936216f8
JA
174 unsigned int crc_len;
175};
176
7d9fb455
JA
177static void dump_buf(char *buf, unsigned int len, const char *name,
178 unsigned long long offset)
179{
180 char fname[80];
181 int ret, fd;
182
183 sprintf(fname, "%llu.%s", offset, name);
184
185 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
186 if (fd < 0) {
187 perror("open verify buf file");
188 return;
189 }
190
191 while (len) {
192 ret = write(fd, buf, len);
193 if (!ret)
194 break;
195 else if (ret < 0) {
196 perror("write verify buf file");
197 break;
198 }
199 len -= ret;
200 buf += ret;
201 }
202
203 close(fd);
204 log_err(" %s data dumped as %s\n", name, fname);
205}
206
207static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
208{
209 struct thread_data *td = vc->td;
210 struct io_u *io_u = vc->io_u;
211 unsigned long hdr_offset;
212 unsigned int hdr_inc, header_num;
213 struct io_u dummy;
214 void *buf, *p;
215
216 hdr_offset = vc->hdr_num * hdr->len;
217
218 dump_buf(io_u->buf + hdr_offset, hdr->len, "received",
219 io_u->offset + hdr_offset);
220
221 buf = p = malloc(io_u->buflen);
222 dummy = *io_u;
223 fill_pattern(td, p, io_u->buflen, &dummy, hdr->rand_seed, 1);
224
225 hdr_inc = io_u->buflen;
226 if (td->o.verify_interval)
227 hdr_inc = td->o.verify_interval;
228
229 header_num = 0;
230 for (; p < buf + io_u->buflen; p += hdr_inc) {
231 hdr = p;
232 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
233 header_num++;
234 }
235
236 dump_buf(buf + hdr_offset, hdr->len, "expected",
237 io_u->offset + hdr_offset);
238 free(buf);
239}
240
bfacf389
JA
241static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
242{
243 unsigned long long offset;
244
245 offset = vc->io_u->offset;
246 offset += vc->hdr_num * hdr->len;
32c17adf
JA
247 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
248 vc->name, vc->io_u->file->file_name, offset, hdr->len);
bfacf389
JA
249
250 if (vc->good_crc && vc->bad_crc) {
251 log_err(" Expected CRC: ");
252 hexdump(vc->good_crc, vc->crc_len);
253 log_err(" Received CRC: ");
254 hexdump(vc->bad_crc, vc->crc_len);
255 }
7d9fb455
JA
256
257 dump_verify_buffers(hdr, vc);
bfacf389
JA
258}
259
d9f2caf3
JA
260/*
261 * Return data area 'header_num'
262 */
936216f8 263static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
d9f2caf3 264{
936216f8 265 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
d9f2caf3
JA
266}
267
7437ee87 268static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
936216f8 269 struct vcont *vc)
7437ee87
SL
270{
271 struct vhdr_meta *vh = hdr_priv(hdr);
936216f8 272 struct io_u *io_u = vc->io_u;
7437ee87 273
bd6f78b2
JA
274 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
275
bfacf389
JA
276 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
277 return 0;
7437ee87 278
bfacf389
JA
279 vc->name = "meta";
280 log_verify_failure(hdr, vc);
281 return EILSEQ;
7437ee87
SL
282}
283
936216f8 284static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
cd14cc10 285{
936216f8 286 void *p = io_u_verify_off(hdr, vc);
546dfd9f 287 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10
JA
288 uint8_t sha512[128];
289 struct sha512_ctx sha512_ctx = {
290 .buf = sha512,
291 };
292
936216f8 293 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 294
cd14cc10 295 sha512_init(&sha512_ctx);
87677832 296 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
cd14cc10 297
bfacf389
JA
298 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
299 return 0;
cd14cc10 300
bfacf389
JA
301 vc->name = "sha512";
302 vc->good_crc = vh->sha512;
303 vc->bad_crc = sha512_ctx.buf;
304 vc->crc_len = sizeof(vh->sha512);
305 log_verify_failure(hdr, vc);
306 return EILSEQ;
cd14cc10
JA
307}
308
936216f8 309static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
cd14cc10 310{
936216f8 311 void *p = io_u_verify_off(hdr, vc);
546dfd9f 312 struct vhdr_sha256 *vh = hdr_priv(hdr);
bc77f56f 313 uint8_t sha256[64];
cd14cc10
JA
314 struct sha256_ctx sha256_ctx = {
315 .buf = sha256,
316 };
317
936216f8 318 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 319
cd14cc10 320 sha256_init(&sha256_ctx);
87677832 321 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
cd14cc10 322
bfacf389
JA
323 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
324 return 0;
cd14cc10 325
bfacf389
JA
326 vc->name = "sha256";
327 vc->good_crc = vh->sha256;
328 vc->bad_crc = sha256_ctx.buf;
329 vc->crc_len = sizeof(vh->sha256);
330 log_verify_failure(hdr, vc);
331 return EILSEQ;
cd14cc10
JA
332}
333
936216f8 334static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
7c353ceb 335{
936216f8 336 void *p = io_u_verify_off(hdr, vc);
7c353ceb
JA
337 struct vhdr_sha1 *vh = hdr_priv(hdr);
338 uint32_t sha1[5];
339 struct sha1_ctx sha1_ctx = {
340 .H = sha1,
341 };
342
936216f8 343 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
7c353ceb
JA
344
345 sha1_init(&sha1_ctx);
346 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
347
bfacf389
JA
348 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
349 return 0;
7c353ceb 350
bfacf389
JA
351 vc->name = "sha1";
352 vc->good_crc = vh->sha1;
353 vc->bad_crc = sha1_ctx.H;
354 vc->crc_len = sizeof(vh->sha1);
355 log_verify_failure(hdr, vc);
356 return EILSEQ;
7c353ceb
JA
357}
358
936216f8 359static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
1e154bdb 360{
936216f8 361 void *p = io_u_verify_off(hdr, vc);
546dfd9f 362 struct vhdr_crc7 *vh = hdr_priv(hdr);
1e154bdb
JA
363 unsigned char c;
364
936216f8 365 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 366
87677832 367 c = crc7(p, hdr->len - hdr_size(hdr));
1e154bdb 368
bfacf389
JA
369 if (c == vh->crc7)
370 return 0;
1e154bdb 371
bfacf389
JA
372 vc->name = "crc7";
373 vc->good_crc = &vh->crc7;
374 vc->bad_crc = &c;
375 vc->crc_len = 1;
376 log_verify_failure(hdr, vc);
377 return EILSEQ;
1e154bdb
JA
378}
379
936216f8 380static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
969f7ed3 381{
936216f8 382 void *p = io_u_verify_off(hdr, vc);
546dfd9f 383 struct vhdr_crc16 *vh = hdr_priv(hdr);
969f7ed3
JA
384 unsigned short c;
385
936216f8 386 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 387
87677832 388 c = crc16(p, hdr->len - hdr_size(hdr));
969f7ed3 389
bfacf389
JA
390 if (c == vh->crc16)
391 return 0;
969f7ed3 392
bfacf389
JA
393 vc->name = "crc16";
394 vc->good_crc = &vh->crc16;
395 vc->bad_crc = &c;
396 vc->crc_len = 2;
397 log_verify_failure(hdr, vc);
398 return EILSEQ;
969f7ed3
JA
399}
400
936216f8 401static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
d77a7af3 402{
936216f8 403 void *p = io_u_verify_off(hdr, vc);
546dfd9f 404 struct vhdr_crc64 *vh = hdr_priv(hdr);
d77a7af3
JA
405 unsigned long long c;
406
936216f8 407 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 408
87677832 409 c = crc64(p, hdr->len - hdr_size(hdr));
d77a7af3 410
bfacf389
JA
411 if (c == vh->crc64)
412 return 0;
d77a7af3 413
bfacf389
JA
414 vc->name = "crc64";
415 vc->good_crc = &vh->crc64;
416 vc->bad_crc = &c;
417 vc->crc_len = 8;
418 log_verify_failure(hdr, vc);
419 return EILSEQ;
d77a7af3
JA
420}
421
936216f8 422static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
e29d1b70 423{
936216f8 424 void *p = io_u_verify_off(hdr, vc);
546dfd9f
JA
425 struct vhdr_crc32 *vh = hdr_priv(hdr);
426 uint32_t c;
e29d1b70 427
936216f8 428 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 429
87677832 430 c = crc32(p, hdr->len - hdr_size(hdr));
e29d1b70 431
bfacf389
JA
432 if (c == vh->crc32)
433 return 0;
e29d1b70 434
bfacf389
JA
435 vc->name = "crc32";
436 vc->good_crc = &vh->crc32;
437 vc->bad_crc = &c;
438 vc->crc_len = 4;
439 log_verify_failure(hdr, vc);
440 return EILSEQ;
e29d1b70
JA
441}
442
936216f8 443static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
bac39e0e 444{
936216f8 445 void *p = io_u_verify_off(hdr, vc);
bac39e0e
JA
446 struct vhdr_crc32 *vh = hdr_priv(hdr);
447 uint32_t c;
448
936216f8 449 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
bac39e0e 450
3845591f
JA
451 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
452 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
453 else
454 c = crc32c(p, hdr->len - hdr_size(hdr));
bac39e0e 455
bfacf389
JA
456 if (c == vh->crc32)
457 return 0;
bac39e0e 458
bfacf389
JA
459 vc->name = "crc32c";
460 vc->good_crc = &vh->crc32;
461 vc->bad_crc = &c;
462 vc->crc_len = 4;
463 log_verify_failure(hdr, vc);
464 return EILSEQ;
bac39e0e
JA
465}
466
936216f8 467static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
e29d1b70 468{
936216f8 469 void *p = io_u_verify_off(hdr, vc);
546dfd9f 470 struct vhdr_md5 *vh = hdr_priv(hdr);
8c432325
JA
471 uint32_t hash[MD5_HASH_WORDS];
472 struct md5_ctx md5_ctx = {
473 .hash = hash,
474 };
e29d1b70 475
936216f8 476 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
bd6f78b2 477
61f821f1 478 md5_init(&md5_ctx);
87677832 479 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
e29d1b70 480
bfacf389
JA
481 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
482 return 0;
e29d1b70 483
bfacf389
JA
484 vc->name = "md5";
485 vc->good_crc = vh->md5_digest;
486 vc->bad_crc = md5_ctx.hash;
487 vc->crc_len = sizeof(hash);
488 log_verify_failure(hdr, vc);
489 return EILSEQ;
e29d1b70
JA
490}
491
3f199b01
JA
492static unsigned int hweight8(unsigned int w)
493{
494 unsigned int res = w - ((w >> 1) & 0x55);
495
496 res = (res & 0x33) + ((res >> 2) & 0x33);
497 return (res + (res >> 4)) & 0x0F;
498}
499
0e92f873 500int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
5ec10eaa 501 char *buf, unsigned int len, unsigned int mod)
a944e335
SL
502{
503 unsigned int i;
a944e335
SL
504
505 for (i = 0; i < len; i++) {
0e92f873 506 if (buf[i] != pattern[mod]) {
3f199b01
JA
507 unsigned int bits;
508
0e92f873 509 bits = hweight8(buf[i] ^ pattern[mod]);
3f199b01 510 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
0e92f873 511 buf[i], pattern[mod], bits);
3f199b01 512 log_err("fio: bad pattern block offset %u\n", i);
9fd18969 513 return EILSEQ;
3f199b01 514 }
a944e335
SL
515 mod++;
516 if (mod == pattern_size)
517 mod = 0;
518 }
519
520 return 0;
521}
522
e8462bd8
JA
523/*
524 * Push IO verification to a separate thread
525 */
526int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
527{
528 if (io_u->file)
529 put_file_log(td, io_u->file);
530
531 io_u->file = NULL;
532
533 pthread_mutex_lock(&td->io_u_lock);
0c41214f
RR
534
535 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
536 td->cur_depth--;
537 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
538 }
e8462bd8
JA
539 flist_del(&io_u->list);
540 flist_add_tail(&io_u->list, &td->verify_list);
2ecc1b57 541 io_u->flags |= IO_U_F_FREE_DEF;
e8462bd8
JA
542 pthread_mutex_unlock(&td->io_u_lock);
543
544 pthread_cond_signal(&td->verify_cond);
e8462bd8
JA
545 return 0;
546}
547
0d29de83
JA
548static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
549{
550 static char zero_buf[1024];
551 unsigned int this_len, len;
552 int ret = 0;
553 void *p;
554
555 if (!td->o.trim_zero)
556 return 0;
557
558 len = io_u->buflen;
559 p = io_u->buf;
560 do {
561 this_len = sizeof(zero_buf);
562 if (this_len > len)
563 this_len = len;
564 if (memcmp(p, zero_buf, this_len)) {
565 ret = EILSEQ;
566 break;
567 }
568 len -= this_len;
569 p += this_len;
570 } while (len);
571
572 if (!ret)
573 return 0;
574
a917a8b3
JA
575 log_err("trim: verify failed at file %s offset %llu, length %lu"
576 ", block offset %lu\n",
577 io_u->file->file_name, io_u->offset, io_u->buflen,
2f68124f 578 (unsigned long) (p - io_u->buf));
0d29de83
JA
579 return ret;
580}
581
36690c9b 582int verify_io_u(struct thread_data *td, struct io_u *io_u)
e29d1b70 583{
3f9f4e26 584 struct verify_header *hdr;
a944e335 585 unsigned int hdr_size, hdr_inc, hdr_num = 0;
95646108 586 void *p;
e29d1b70
JA
587 int ret;
588
1dcc0498 589 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b 590 return 0;
0d29de83
JA
591 if (io_u->flags & IO_U_F_TRIMMED) {
592 ret = verify_trimmed_io_u(td, io_u);
593 goto done;
594 }
36690c9b 595
3f9f4e26 596 hdr_inc = io_u->buflen;
a59e170d
JA
597 if (td->o.verify_interval)
598 hdr_inc = td->o.verify_interval;
e29d1b70 599
a12a3b4d 600 ret = 0;
5ec10eaa
JA
601 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
602 p += hdr_inc, hdr_num++) {
bfacf389
JA
603 struct vcont vc = {
604 .io_u = io_u,
605 .hdr_num = hdr_num,
7d9fb455 606 .td = td,
bfacf389 607 };
936216f8 608
f3e6cb95 609 if (ret && td->o.verify_fatal)
a12a3b4d 610 break;
f3e6cb95 611
a944e335 612 hdr_size = __hdr_size(td->o.verify);
a59e170d 613 if (td->o.verify_offset)
a944e335 614 memswp(p, p + td->o.verify_offset, hdr_size);
95646108 615 hdr = p;
e29d1b70 616
3f199b01 617 if (hdr->fio_magic != FIO_HDR_MAGIC) {
c9b44031
JA
618 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
619 hdr->fio_magic, FIO_HDR_MAGIC,
620 io_u->file->file_name,
621 io_u->offset + hdr_num * hdr->len, hdr->len);
9fd18969 622 return EILSEQ;
3f199b01
JA
623 }
624
e28218f3 625 if (td->o.verify_pattern_bytes) {
bd6f78b2
JA
626 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
627 io_u, hdr->len);
e28218f3 628 ret = verify_io_u_pattern(td->o.verify_pattern,
0e92f873
RR
629 td->o.verify_pattern_bytes,
630 p + hdr_size,
631 hdr_inc - hdr_size,
632 hdr_size % td->o.verify_pattern_bytes);
c9b44031
JA
633
634 if (ret) {
635 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
636 io_u->file->file_name,
637 io_u->offset + hdr_num * hdr->len,
638 hdr->len);
639 }
640
e92d3d71
RR
641 /*
642 * Also verify the meta data, if applicable
643 */
644 if (hdr->verify_type == VERIFY_META)
936216f8 645 ret |= verify_io_u_meta(hdr, td, &vc);
e28218f3
SL
646 continue;
647 }
648
3f9f4e26
SL
649 switch (hdr->verify_type) {
650 case VERIFY_MD5:
936216f8 651 ret = verify_io_u_md5(hdr, &vc);
3f9f4e26
SL
652 break;
653 case VERIFY_CRC64:
936216f8 654 ret = verify_io_u_crc64(hdr, &vc);
3f9f4e26 655 break;
bac39e0e 656 case VERIFY_CRC32C:
3845591f 657 case VERIFY_CRC32C_INTEL:
936216f8 658 ret = verify_io_u_crc32c(hdr, &vc);
bac39e0e 659 break;
3f9f4e26 660 case VERIFY_CRC32:
936216f8 661 ret = verify_io_u_crc32(hdr, &vc);
3f9f4e26
SL
662 break;
663 case VERIFY_CRC16:
936216f8 664 ret = verify_io_u_crc16(hdr, &vc);
3f9f4e26
SL
665 break;
666 case VERIFY_CRC7:
936216f8 667 ret = verify_io_u_crc7(hdr, &vc);
3f9f4e26 668 break;
cd14cc10 669 case VERIFY_SHA256:
936216f8 670 ret = verify_io_u_sha256(hdr, &vc);
cd14cc10
JA
671 break;
672 case VERIFY_SHA512:
936216f8 673 ret = verify_io_u_sha512(hdr, &vc);
cd14cc10 674 break;
7437ee87 675 case VERIFY_META:
936216f8 676 ret = verify_io_u_meta(hdr, td, &vc);
7437ee87 677 break;
7c353ceb 678 case VERIFY_SHA1:
936216f8 679 ret = verify_io_u_sha1(hdr, &vc);
7c353ceb 680 break;
3f9f4e26
SL
681 default:
682 log_err("Bad verify type %u\n", hdr->verify_type);
d16d4e09 683 ret = EINVAL;
3f9f4e26 684 }
3f9f4e26 685 }
a7dfe862 686
0d29de83 687done:
f3e6cb95
JA
688 if (ret && td->o.verify_fatal)
689 td->terminate = 1;
690
a12a3b4d 691 return ret;
e29d1b70
JA
692}
693
7437ee87 694static void fill_meta(struct verify_header *hdr, struct thread_data *td,
5ec10eaa 695 struct io_u *io_u, unsigned int header_num)
7437ee87
SL
696{
697 struct vhdr_meta *vh = hdr_priv(hdr);
698
699 vh->thread = td->thread_number;
700
701 vh->time_sec = io_u->start_time.tv_sec;
702 vh->time_usec = io_u->start_time.tv_usec;
703
704 vh->numberio = td->io_issues[DDIR_WRITE];
705
706 vh->offset = io_u->offset + header_num * td->o.verify_interval;
707}
708
cd14cc10
JA
709static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
710{
546dfd9f 711 struct vhdr_sha512 *vh = hdr_priv(hdr);
cd14cc10 712 struct sha512_ctx sha512_ctx = {
546dfd9f 713 .buf = vh->sha512,
cd14cc10
JA
714 };
715
716 sha512_init(&sha512_ctx);
717 sha512_update(&sha512_ctx, p, len);
718}
719
720static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
721{
546dfd9f 722 struct vhdr_sha256 *vh = hdr_priv(hdr);
cd14cc10 723 struct sha256_ctx sha256_ctx = {
546dfd9f 724 .buf = vh->sha256,
cd14cc10
JA
725 };
726
727 sha256_init(&sha256_ctx);
728 sha256_update(&sha256_ctx, p, len);
729}
730
7c353ceb
JA
731static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
732{
733 struct vhdr_sha1 *vh = hdr_priv(hdr);
734 struct sha1_ctx sha1_ctx = {
735 .H = vh->sha1,
736 };
737
738 sha1_init(&sha1_ctx);
739 sha1_update(&sha1_ctx, p, len);
740}
741
1e154bdb
JA
742static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
743{
546dfd9f
JA
744 struct vhdr_crc7 *vh = hdr_priv(hdr);
745
746 vh->crc7 = crc7(p, len);
1e154bdb
JA
747}
748
969f7ed3
JA
749static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
750{
546dfd9f
JA
751 struct vhdr_crc16 *vh = hdr_priv(hdr);
752
753 vh->crc16 = crc16(p, len);
969f7ed3
JA
754}
755
e29d1b70
JA
756static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
757{
546dfd9f
JA
758 struct vhdr_crc32 *vh = hdr_priv(hdr);
759
760 vh->crc32 = crc32(p, len);
e29d1b70
JA
761}
762
bac39e0e
JA
763static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
764{
765 struct vhdr_crc32 *vh = hdr_priv(hdr);
766
3845591f
JA
767 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
768 vh->crc32 = crc32c_intel(p, len);
769 else
770 vh->crc32 = crc32c(p, len);
bac39e0e
JA
771}
772
d77a7af3
JA
773static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
774{
546dfd9f
JA
775 struct vhdr_crc64 *vh = hdr_priv(hdr);
776
777 vh->crc64 = crc64(p, len);
d77a7af3
JA
778}
779
e29d1b70
JA
780static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
781{
546dfd9f 782 struct vhdr_md5 *vh = hdr_priv(hdr);
8c432325 783 struct md5_ctx md5_ctx = {
546dfd9f 784 .hash = (uint32_t *) vh->md5_digest,
8c432325 785 };
e29d1b70 786
61f821f1 787 md5_init(&md5_ctx);
e29d1b70 788 md5_update(&md5_ctx, p, len);
e29d1b70
JA
789}
790
7d9fb455
JA
791static void populate_hdr(struct thread_data *td, struct io_u *io_u,
792 struct verify_header *hdr, unsigned int header_num,
793 unsigned int header_len)
794{
795 unsigned int data_len;
796 void *data, *p;
797
798 p = (void *) hdr;
799
800 hdr->fio_magic = FIO_HDR_MAGIC;
801 hdr->len = header_len;
802 hdr->verify_type = td->o.verify;
803 hdr->rand_seed = io_u->rand_seed;
804 data_len = header_len - hdr_size(hdr);
805
806 data = p + hdr_size(hdr);
807 switch (td->o.verify) {
808 case VERIFY_MD5:
809 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
810 io_u, hdr->len);
811 fill_md5(hdr, data, data_len);
812 break;
813 case VERIFY_CRC64:
814 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
815 io_u, hdr->len);
816 fill_crc64(hdr, data, data_len);
817 break;
818 case VERIFY_CRC32C:
819 case VERIFY_CRC32C_INTEL:
820 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
821 io_u, hdr->len);
822 fill_crc32c(hdr, data, data_len);
823 break;
824 case VERIFY_CRC32:
825 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
826 io_u, hdr->len);
827 fill_crc32(hdr, data, data_len);
828 break;
829 case VERIFY_CRC16:
830 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
831 io_u, hdr->len);
832 fill_crc16(hdr, data, data_len);
833 break;
834 case VERIFY_CRC7:
835 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
836 io_u, hdr->len);
837 fill_crc7(hdr, data, data_len);
838 break;
839 case VERIFY_SHA256:
840 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
841 io_u, hdr->len);
842 fill_sha256(hdr, data, data_len);
843 break;
844 case VERIFY_SHA512:
845 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
846 io_u, hdr->len);
847 fill_sha512(hdr, data, data_len);
848 break;
849 case VERIFY_META:
850 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
851 io_u, hdr->len);
852 fill_meta(hdr, td, io_u, header_num);
853 break;
854 case VERIFY_SHA1:
855 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
856 io_u, hdr->len);
857 fill_sha1(hdr, data, data_len);
858 break;
859 default:
860 log_err("fio: bad verify type: %d\n", td->o.verify);
861 assert(0);
862 }
863 if (td->o.verify_offset)
864 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
865}
866
e29d1b70
JA
867/*
868 * fill body of io_u->buf with random data and add a header with the
869 * crc32 or md5 sum of that data.
870 */
871void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
872{
baefa9be 873 struct verify_header *hdr;
7d9fb455
JA
874 unsigned int hdr_inc, header_num = 0;
875 void *p = io_u->buf;
e29d1b70 876
9cc3d150
JA
877 if (td->o.verify == VERIFY_NULL)
878 return;
879
7d9fb455 880 fill_pattern(td, p, io_u->buflen, io_u, 0, 0);
546a9142
SL
881
882 hdr_inc = io_u->buflen;
a59e170d
JA
883 if (td->o.verify_interval)
884 hdr_inc = td->o.verify_interval;
baefa9be 885
5ec10eaa 886 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
95646108 887 hdr = p;
7d9fb455 888 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
7437ee87 889 header_num++;
e29d1b70 890 }
e29d1b70
JA
891}
892
893int get_next_verify(struct thread_data *td, struct io_u *io_u)
894{
8de8f047 895 struct io_piece *ipo = NULL;
e29d1b70 896
d2d7fa53
JA
897 /*
898 * this io_u is from a requeue, we already filled the offsets
899 */
900 if (io_u->file)
901 return 0;
902
8de8f047
JA
903 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
904 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 905
8de8f047 906 ipo = rb_entry(n, struct io_piece, rb_node);
4b87898e 907 rb_erase(n, &td->io_hist_tree);
a917a8b3
JA
908 assert(ipo->flags & IP_F_ONRB);
909 ipo->flags &= ~IP_F_ONRB;
01743ee1
JA
910 } else if (!flist_empty(&td->io_hist_list)) {
911 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
912 flist_del(&ipo->list);
a917a8b3
JA
913 assert(ipo->flags & IP_F_ONLIST);
914 ipo->flags &= ~IP_F_ONLIST;
8de8f047 915 }
e29d1b70 916
8de8f047 917 if (ipo) {
0d29de83
JA
918 td->io_hist_len--;
919
e29d1b70
JA
920 io_u->offset = ipo->offset;
921 io_u->buflen = ipo->len;
36167d82 922 io_u->file = ipo->file;
97af62ce 923
0d29de83
JA
924 if (ipo->flags & IP_F_TRIMMED)
925 io_u->flags |= IO_U_F_TRIMMED;
926
d6aed795 927 if (!fio_file_open(io_u->file)) {
97af62ce
JA
928 int r = td_io_open_file(td, io_u->file);
929
bd6f78b2
JA
930 if (r) {
931 dprint(FD_VERIFY, "failed file %s open\n",
932 io_u->file->file_name);
97af62ce 933 return 1;
bd6f78b2 934 }
97af62ce
JA
935 }
936
937 get_file(ipo->file);
d6aed795 938 assert(fio_file_open(io_u->file));
e29d1b70 939 io_u->ddir = DDIR_READ;
36167d82
JA
940 io_u->xfer_buf = io_u->buf;
941 io_u->xfer_buflen = io_u->buflen;
0d29de83
JA
942
943 remove_trim_entry(td, ipo);
e29d1b70 944 free(ipo);
bd6f78b2 945 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
e29d1b70
JA
946 return 0;
947 }
948
bd6f78b2 949 dprint(FD_VERIFY, "get_next_verify: empty\n");
e29d1b70
JA
950 return 1;
951}
e8462bd8
JA
952
953static void *verify_async_thread(void *data)
954{
955 struct thread_data *td = data;
956 struct io_u *io_u;
957 int ret = 0;
958
959 if (td->o.verify_cpumask_set &&
960 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
961 log_err("fio: failed setting verify thread affinity\n");
962 goto done;
963 }
964
965 do {
e53ab27c
JA
966 FLIST_HEAD(list);
967
e8462bd8
JA
968 read_barrier();
969 if (td->verify_thread_exit)
970 break;
971
972 pthread_mutex_lock(&td->io_u_lock);
973
974 while (flist_empty(&td->verify_list) &&
975 !td->verify_thread_exit) {
b36e298b
JA
976 ret = pthread_cond_wait(&td->verify_cond,
977 &td->io_u_lock);
e8462bd8
JA
978 if (ret) {
979 pthread_mutex_unlock(&td->io_u_lock);
980 break;
981 }
982 }
983
e53ab27c
JA
984 flist_splice_init(&td->verify_list, &list);
985 pthread_mutex_unlock(&td->io_u_lock);
986
987 if (flist_empty(&list))
e8462bd8 988 continue;
e8462bd8 989
e53ab27c
JA
990 while (!flist_empty(&list)) {
991 io_u = flist_entry(list.next, struct io_u, list);
992 flist_del_init(&io_u->list);
e8462bd8 993
d561f2ab 994 ret = verify_io_u(td, io_u);
e53ab27c 995 put_io_u(td, io_u);
d561f2ab
JA
996 if (!ret)
997 continue;
998 if (td->o.continue_on_error &&
999 td_non_fatal_error(ret)) {
1000 update_error_count(td, ret);
1001 td_clear_error(td);
1002 ret = 0;
1003 }
e53ab27c 1004 }
e8462bd8
JA
1005 } while (!ret);
1006
d561f2ab
JA
1007 if (ret) {
1008 td_verror(td, ret, "async_verify");
f3e6cb95
JA
1009 if (td->o.verify_fatal)
1010 td->terminate = 1;
d561f2ab
JA
1011 }
1012
e8462bd8
JA
1013done:
1014 pthread_mutex_lock(&td->io_u_lock);
1015 td->nr_verify_threads--;
1016 pthread_mutex_unlock(&td->io_u_lock);
1017
1018 pthread_cond_signal(&td->free_cond);
1019 return NULL;
1020}
1021
1022int verify_async_init(struct thread_data *td)
1023{
1024 int i, ret;
304a47c7
VA
1025 pthread_attr_t attr;
1026
1027 pthread_attr_init(&attr);
1028 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
e8462bd8
JA
1029
1030 td->verify_thread_exit = 0;
1031
1032 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1033 for (i = 0; i < td->o.verify_async; i++) {
304a47c7 1034 ret = pthread_create(&td->verify_threads[i], &attr,
e8462bd8
JA
1035 verify_async_thread, td);
1036 if (ret) {
1037 log_err("fio: async verify creation failed: %s\n",
1038 strerror(ret));
1039 break;
1040 }
1041 ret = pthread_detach(td->verify_threads[i]);
1042 if (ret) {
1043 log_err("fio: async verify thread detach failed: %s\n",
1044 strerror(ret));
1045 break;
1046 }
1047 td->nr_verify_threads++;
1048 }
1049
304a47c7
VA
1050 pthread_attr_destroy(&attr);
1051
e8462bd8 1052 if (i != td->o.verify_async) {
e40823b1 1053 log_err("fio: only %d verify threads started, exiting\n", i);
e8462bd8
JA
1054 td->verify_thread_exit = 1;
1055 write_barrier();
1056 pthread_cond_broadcast(&td->verify_cond);
1057 return 1;
1058 }
1059
1060 return 0;
1061}
1062
1063void verify_async_exit(struct thread_data *td)
1064{
1065 td->verify_thread_exit = 1;
1066 write_barrier();
1067 pthread_cond_broadcast(&td->verify_cond);
1068
1069 pthread_mutex_lock(&td->io_u_lock);
1070
1071 while (td->nr_verify_threads)
1072 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1073
1074 pthread_mutex_unlock(&td->io_u_lock);
1075 free(td->verify_threads);
1076 td->verify_threads = NULL;
1077}