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