2 * IO verification helpers
15 #include "lib/hweight.h"
18 #include "crc/crc64.h"
19 #include "crc/crc32.h"
20 #include "crc/crc32c.h"
21 #include "crc/crc16.h"
23 #include "crc/sha256.h"
24 #include "crc/sha512.h"
26 #include "crc/xxhash.h"
28 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
29 struct verify_header *hdr, unsigned int header_num,
30 unsigned int header_len);
31 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
33 static void __fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
36 void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
38 fill_pattern(p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
41 void __fill_buffer(struct thread_options *o, unsigned long seed, void *p,
44 __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
47 unsigned long fill_buffer(struct thread_data *td, void *p, unsigned int len)
49 struct frand_state *fs = &td->verify_state;
50 struct thread_options *o = &td->o;
52 return fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
55 void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
56 struct io_u *io_u, unsigned long seed, int use_seed)
58 struct thread_options *o = &td->o;
60 if (!o->verify_pattern_bytes) {
61 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
64 __fill_buffer(o, seed, p, len);
66 io_u->rand_seed = fill_buffer(td, p, len);
70 if (io_u->buf_filled_len >= len) {
71 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
72 o->verify_pattern_bytes, len);
76 fill_pattern(p, len, o->verify_pattern, o->verify_pattern_bytes);
77 io_u->buf_filled_len = len;
80 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
84 hdr_inc = io_u->buflen;
85 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
86 hdr_inc = td->o.verify_interval;
91 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
92 unsigned long seed, int use_seed)
94 unsigned int hdr_inc, header_num;
95 struct verify_header *hdr;
98 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
100 hdr_inc = get_hdr_inc(td, io_u);
102 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
104 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
109 static void memswp(void *buf1, void *buf2, unsigned int len)
113 assert(len <= sizeof(swap));
115 memcpy(&swap, buf1, len);
116 memcpy(buf1, buf2, len);
117 memcpy(buf2, &swap, len);
120 static void hexdump(void *buffer, int len)
122 unsigned char *p = buffer;
125 for (i = 0; i < len; i++)
126 log_err("%02x", p[i]);
131 * Prepare for separation of verify_header and checksum header
133 static inline unsigned int __hdr_size(int verify_type)
135 unsigned int len = 0;
137 switch (verify_type) {
144 len = sizeof(struct vhdr_md5);
147 len = sizeof(struct vhdr_crc64);
151 case VERIFY_CRC32C_INTEL:
152 len = sizeof(struct vhdr_crc32);
155 len = sizeof(struct vhdr_crc16);
158 len = sizeof(struct vhdr_crc7);
161 len = sizeof(struct vhdr_sha256);
164 len = sizeof(struct vhdr_sha512);
167 len = sizeof(struct vhdr_xxhash);
170 len = sizeof(struct vhdr_meta);
173 len = sizeof(struct vhdr_sha1);
175 case VERIFY_PATTERN_NO_HDR:
178 log_err("fio: unknown verify header!\n");
182 return len + sizeof(struct verify_header);
185 static inline unsigned int hdr_size(struct thread_data *td,
186 struct verify_header *hdr)
188 if (td->o.verify == VERIFY_PATTERN_NO_HDR)
191 return __hdr_size(hdr->verify_type);
194 static void *hdr_priv(struct verify_header *hdr)
198 return priv + sizeof(struct verify_header);
202 * Verify container, pass info to verify handlers and allow them to
203 * pass info back in case of error
210 unsigned int hdr_num;
211 struct thread_data *td;
214 * Output, only valid in case of error
219 unsigned int crc_len;
222 #define DUMP_BUF_SZ 255
223 static int dump_buf_warned;
225 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
226 const char *type, struct fio_file *f)
228 char *ptr, fname[DUMP_BUF_SZ];
229 size_t buf_left = DUMP_BUF_SZ;
232 ptr = strdup(f->file_name);
234 memset(fname, 0, sizeof(fname));
236 sprintf(fname, "%s%s", aux_path, FIO_OS_PATH_SEPARATOR);
238 strncpy(fname + strlen(fname), basename(ptr), buf_left - 1);
240 buf_left -= strlen(fname);
242 if (!dump_buf_warned) {
243 log_err("fio: verify failure dump buffer too small\n");
250 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
252 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
254 perror("open verify buf file");
259 ret = write(fd, buf, len);
263 perror("write verify buf file");
271 log_err(" %s data dumped as %s\n", type, fname);
276 * Dump the contents of the read block and re-generate the correct data
279 static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
281 struct thread_data *td = vc->td;
282 struct io_u *io_u = vc->io_u;
283 unsigned long hdr_offset;
287 if (!td->o.verify_dump)
291 * Dump the contents we just read off disk
293 hdr_offset = vc->hdr_num * hdr->len;
295 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
296 "received", vc->io_u->file);
299 * Allocate a new buf and re-generate the original data
301 buf = malloc(io_u->buflen);
304 dummy.rand_seed = hdr->rand_seed;
305 dummy.buf_filled_len = 0;
306 dummy.buflen = io_u->buflen;
308 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
310 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
311 "expected", vc->io_u->file);
315 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
317 struct thread_data *td = vc->td;
318 struct verify_header shdr;
320 if (td->o.verify == VERIFY_PATTERN_NO_HDR) {
321 __fill_hdr(&shdr, td->o.verify, vc->io_u->buflen, 0);
325 __dump_verify_buffers(hdr, vc);
328 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
330 unsigned long long offset;
332 offset = vc->io_u->offset;
333 offset += vc->hdr_num * hdr->len;
334 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
335 vc->name, vc->io_u->file->file_name, offset, hdr->len);
337 if (vc->good_crc && vc->bad_crc) {
338 log_err(" Expected CRC: ");
339 hexdump(vc->good_crc, vc->crc_len);
340 log_err(" Received CRC: ");
341 hexdump(vc->bad_crc, vc->crc_len);
344 dump_verify_buffers(hdr, vc);
348 * Return data area 'header_num'
350 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
352 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr);
355 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
357 struct thread_data *td = vc->td;
358 struct io_u *io_u = vc->io_u;
360 unsigned int header_size = __hdr_size(td->o.verify);
361 unsigned int len, mod, i, size, pattern_size;
363 pattern = td->o.verify_pattern;
364 pattern_size = td->o.verify_pattern_bytes;
365 if (pattern_size <= 1)
366 pattern_size = MAX_PATTERN_SIZE;
367 buf = (void *) hdr + header_size;
368 len = get_hdr_inc(td, io_u) - header_size;
369 mod = header_size % pattern_size;
371 for (i = 0; i < len; i += size) {
372 size = pattern_size - mod;
373 if (size > (len - i))
375 if (memcmp(buf + i, pattern + mod, size))
376 /* Let the slow compare find the first mismatch byte. */
381 for (; i < len; i++) {
382 if (buf[i] != pattern[mod]) {
385 bits = hweight8(buf[i] ^ pattern[mod]);
386 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
387 buf[i], pattern[mod], bits);
388 log_err("fio: bad pattern block offset %u\n", i);
389 dump_verify_buffers(hdr, vc);
393 if (mod == td->o.verify_pattern_bytes)
400 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
402 struct thread_data *td = vc->td;
403 struct vhdr_meta *vh = hdr_priv(hdr);
404 struct io_u *io_u = vc->io_u;
407 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
409 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
412 if (td->o.verify_pattern_bytes)
413 ret |= verify_io_u_pattern(hdr, vc);
416 * For read-only workloads, the program cannot be certain of the
417 * last numberio written to a block. Checking of numberio will be
418 * done only for workloads that write data. For verify_only,
419 * numberio will be checked in the last iteration when the correct
420 * state of numberio, that would have been written to each block
421 * in a previous run of fio, has been reached.
423 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
425 if (!td->o.verify_only || td->o.loops == 0)
426 if (vh->numberio != io_u->numberio)
433 log_verify_failure(hdr, vc);
437 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
439 void *p = io_u_verify_off(hdr, vc);
440 struct vhdr_xxhash *vh = hdr_priv(hdr);
444 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
446 state = XXH32_init(1);
447 XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr));
448 hash = XXH32_digest(state);
450 if (vh->hash == hash)
454 vc->good_crc = &vh->hash;
456 vc->crc_len = sizeof(hash);
457 log_verify_failure(hdr, vc);
461 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
463 void *p = io_u_verify_off(hdr, vc);
464 struct vhdr_sha512 *vh = hdr_priv(hdr);
466 struct fio_sha512_ctx sha512_ctx = {
470 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
472 fio_sha512_init(&sha512_ctx);
473 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr));
475 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
479 vc->good_crc = vh->sha512;
480 vc->bad_crc = sha512_ctx.buf;
481 vc->crc_len = sizeof(vh->sha512);
482 log_verify_failure(hdr, vc);
486 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
488 void *p = io_u_verify_off(hdr, vc);
489 struct vhdr_sha256 *vh = hdr_priv(hdr);
491 struct fio_sha256_ctx sha256_ctx = {
495 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
497 fio_sha256_init(&sha256_ctx);
498 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr));
499 fio_sha256_final(&sha256_ctx);
501 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
505 vc->good_crc = vh->sha256;
506 vc->bad_crc = sha256_ctx.buf;
507 vc->crc_len = sizeof(vh->sha256);
508 log_verify_failure(hdr, vc);
512 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
514 void *p = io_u_verify_off(hdr, vc);
515 struct vhdr_sha1 *vh = hdr_priv(hdr);
517 struct fio_sha1_ctx sha1_ctx = {
521 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
523 fio_sha1_init(&sha1_ctx);
524 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr));
525 fio_sha1_final(&sha1_ctx);
527 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
531 vc->good_crc = vh->sha1;
532 vc->bad_crc = sha1_ctx.H;
533 vc->crc_len = sizeof(vh->sha1);
534 log_verify_failure(hdr, vc);
538 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
540 void *p = io_u_verify_off(hdr, vc);
541 struct vhdr_crc7 *vh = hdr_priv(hdr);
544 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
546 c = fio_crc7(p, hdr->len - hdr_size(vc->td, hdr));
552 vc->good_crc = &vh->crc7;
555 log_verify_failure(hdr, vc);
559 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
561 void *p = io_u_verify_off(hdr, vc);
562 struct vhdr_crc16 *vh = hdr_priv(hdr);
565 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
567 c = fio_crc16(p, hdr->len - hdr_size(vc->td, hdr));
573 vc->good_crc = &vh->crc16;
576 log_verify_failure(hdr, vc);
580 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
582 void *p = io_u_verify_off(hdr, vc);
583 struct vhdr_crc64 *vh = hdr_priv(hdr);
584 unsigned long long c;
586 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
588 c = fio_crc64(p, hdr->len - hdr_size(vc->td, hdr));
594 vc->good_crc = &vh->crc64;
597 log_verify_failure(hdr, vc);
601 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
603 void *p = io_u_verify_off(hdr, vc);
604 struct vhdr_crc32 *vh = hdr_priv(hdr);
607 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
609 c = fio_crc32(p, hdr->len - hdr_size(vc->td, hdr));
615 vc->good_crc = &vh->crc32;
618 log_verify_failure(hdr, vc);
622 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
624 void *p = io_u_verify_off(hdr, vc);
625 struct vhdr_crc32 *vh = hdr_priv(hdr);
628 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
630 c = fio_crc32c(p, hdr->len - hdr_size(vc->td, hdr));
636 vc->good_crc = &vh->crc32;
639 log_verify_failure(hdr, vc);
643 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
645 void *p = io_u_verify_off(hdr, vc);
646 struct vhdr_md5 *vh = hdr_priv(hdr);
647 uint32_t hash[MD5_HASH_WORDS];
648 struct fio_md5_ctx md5_ctx = {
652 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
654 fio_md5_init(&md5_ctx);
655 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr));
656 fio_md5_final(&md5_ctx);
658 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
662 vc->good_crc = vh->md5_digest;
663 vc->bad_crc = md5_ctx.hash;
664 vc->crc_len = sizeof(hash);
665 log_verify_failure(hdr, vc);
670 * Push IO verification to a separate thread
672 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
674 struct io_u *io_u = *io_u_ptr;
676 pthread_mutex_lock(&td->io_u_lock);
679 put_file_log(td, io_u->file);
681 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
683 io_u_clear(io_u, IO_U_F_IN_CUR_DEPTH);
685 flist_add_tail(&io_u->verify_list, &td->verify_list);
687 pthread_mutex_unlock(&td->io_u_lock);
689 pthread_cond_signal(&td->verify_cond);
693 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
695 static char zero_buf[1024];
696 unsigned int this_len, len;
700 if (!td->o.trim_zero)
706 this_len = sizeof(zero_buf);
709 if (memcmp(p, zero_buf, this_len)) {
720 log_err("trim: verify failed at file %s offset %llu, length %lu"
721 ", block offset %lu\n",
722 io_u->file->file_name, io_u->offset, io_u->buflen,
723 (unsigned long) (p - io_u->buf));
727 static int verify_header(struct io_u *io_u, struct verify_header *hdr,
728 unsigned int hdr_num, unsigned int hdr_len)
733 if (hdr->magic != FIO_HDR_MAGIC) {
734 log_err("verify: bad magic header %x, wanted %x",
735 hdr->magic, FIO_HDR_MAGIC);
738 if (hdr->len != hdr_len) {
739 log_err("verify: bad header length %u, wanted %u",
743 if (hdr->rand_seed != io_u->rand_seed) {
744 log_err("verify: bad header rand_seed %"PRIu64
746 hdr->rand_seed, io_u->rand_seed);
750 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
751 if (crc != hdr->crc32) {
752 log_err("verify: bad header crc %x, calculated %x",
759 log_err(" at file %s offset %llu, length %u\n",
760 io_u->file->file_name,
761 io_u->offset + hdr_num * hdr_len, hdr_len);
765 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
767 struct verify_header *hdr;
768 struct io_u *io_u = *io_u_ptr;
769 unsigned int header_size, hdr_inc, hdr_num = 0;
773 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
776 * If the IO engine is faking IO (like null), then just pretend
777 * we verified everything.
779 if (td->io_ops->flags & FIO_FAKEIO)
782 if (io_u->flags & IO_U_F_TRIMMED) {
783 ret = verify_trimmed_io_u(td, io_u);
787 hdr_inc = get_hdr_inc(td, io_u);
790 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
791 p += hdr_inc, hdr_num++) {
797 unsigned int verify_type;
799 if (ret && td->o.verify_fatal)
802 header_size = __hdr_size(td->o.verify);
803 if (td->o.verify_offset)
804 memswp(p, p + td->o.verify_offset, header_size);
808 * Make rand_seed check pass when have verifysort or
811 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
812 io_u->rand_seed = hdr->rand_seed;
814 if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
815 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
820 if (td->o.verify != VERIFY_NONE)
821 verify_type = td->o.verify;
823 verify_type = hdr->verify_type;
825 switch (verify_type) {
827 ret = verify_io_u_md5(hdr, &vc);
830 ret = verify_io_u_crc64(hdr, &vc);
833 case VERIFY_CRC32C_INTEL:
834 ret = verify_io_u_crc32c(hdr, &vc);
837 ret = verify_io_u_crc32(hdr, &vc);
840 ret = verify_io_u_crc16(hdr, &vc);
843 ret = verify_io_u_crc7(hdr, &vc);
846 ret = verify_io_u_sha256(hdr, &vc);
849 ret = verify_io_u_sha512(hdr, &vc);
852 ret = verify_io_u_xxhash(hdr, &vc);
855 ret = verify_io_u_meta(hdr, &vc);
858 ret = verify_io_u_sha1(hdr, &vc);
861 case VERIFY_PATTERN_NO_HDR:
862 ret = verify_io_u_pattern(hdr, &vc);
865 log_err("Bad verify type %u\n", hdr->verify_type);
869 if (ret && verify_type != hdr->verify_type)
870 log_err("fio: verify type mismatch (%u media, %u given)\n",
871 hdr->verify_type, verify_type);
875 if (ret && td->o.verify_fatal)
876 fio_mark_td_terminate(td);
881 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
882 struct io_u *io_u, unsigned int header_num)
884 struct vhdr_meta *vh = hdr_priv(hdr);
886 vh->thread = td->thread_number;
888 vh->time_sec = io_u->start_time.tv_sec;
889 vh->time_usec = io_u->start_time.tv_usec;
891 vh->numberio = io_u->numberio;
893 vh->offset = io_u->offset + header_num * td->o.verify_interval;
896 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
898 struct vhdr_xxhash *vh = hdr_priv(hdr);
901 state = XXH32_init(1);
902 XXH32_update(state, p, len);
903 vh->hash = XXH32_digest(state);
906 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
908 struct vhdr_sha512 *vh = hdr_priv(hdr);
909 struct fio_sha512_ctx sha512_ctx = {
913 fio_sha512_init(&sha512_ctx);
914 fio_sha512_update(&sha512_ctx, p, len);
917 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
919 struct vhdr_sha256 *vh = hdr_priv(hdr);
920 struct fio_sha256_ctx sha256_ctx = {
924 fio_sha256_init(&sha256_ctx);
925 fio_sha256_update(&sha256_ctx, p, len);
926 fio_sha256_final(&sha256_ctx);
929 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
931 struct vhdr_sha1 *vh = hdr_priv(hdr);
932 struct fio_sha1_ctx sha1_ctx = {
936 fio_sha1_init(&sha1_ctx);
937 fio_sha1_update(&sha1_ctx, p, len);
938 fio_sha1_final(&sha1_ctx);
941 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
943 struct vhdr_crc7 *vh = hdr_priv(hdr);
945 vh->crc7 = fio_crc7(p, len);
948 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
950 struct vhdr_crc16 *vh = hdr_priv(hdr);
952 vh->crc16 = fio_crc16(p, len);
955 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
957 struct vhdr_crc32 *vh = hdr_priv(hdr);
959 vh->crc32 = fio_crc32(p, len);
962 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
964 struct vhdr_crc32 *vh = hdr_priv(hdr);
966 vh->crc32 = fio_crc32c(p, len);
969 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
971 struct vhdr_crc64 *vh = hdr_priv(hdr);
973 vh->crc64 = fio_crc64(p, len);
976 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
978 struct vhdr_md5 *vh = hdr_priv(hdr);
979 struct fio_md5_ctx md5_ctx = {
980 .hash = (uint32_t *) vh->md5_digest,
983 fio_md5_init(&md5_ctx);
984 fio_md5_update(&md5_ctx, p, len);
985 fio_md5_final(&md5_ctx);
988 static void __fill_hdr(struct verify_header *hdr, int verify_type,
989 uint32_t len, uint64_t rand_seed)
993 hdr->magic = FIO_HDR_MAGIC;
994 hdr->verify_type = verify_type;
996 hdr->rand_seed = rand_seed;
997 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
1001 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
1004 if (verify_type != VERIFY_PATTERN_NO_HDR)
1005 __fill_hdr(hdr, verify_type, len, rand_seed);
1008 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
1009 struct verify_header *hdr, unsigned int header_num,
1010 unsigned int header_len)
1012 unsigned int data_len;
1017 fill_hdr(hdr, td->o.verify, header_len, io_u->rand_seed);
1019 data_len = header_len - hdr_size(td, hdr);
1021 data = p + hdr_size(td, hdr);
1022 switch (td->o.verify) {
1024 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1026 fill_md5(hdr, data, data_len);
1029 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1031 fill_crc64(hdr, data, data_len);
1034 case VERIFY_CRC32C_INTEL:
1035 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1037 fill_crc32c(hdr, data, data_len);
1040 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1042 fill_crc32(hdr, data, data_len);
1045 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1047 fill_crc16(hdr, data, data_len);
1050 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1052 fill_crc7(hdr, data, data_len);
1055 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1057 fill_sha256(hdr, data, data_len);
1060 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1062 fill_sha512(hdr, data, data_len);
1065 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1067 fill_xxhash(hdr, data, data_len);
1070 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1072 fill_meta(hdr, td, io_u, header_num);
1075 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1077 fill_sha1(hdr, data, data_len);
1079 case VERIFY_PATTERN:
1080 case VERIFY_PATTERN_NO_HDR:
1081 /* nothing to do here */
1084 log_err("fio: bad verify type: %d\n", td->o.verify);
1088 if (td->o.verify_offset && hdr_size(td, hdr))
1089 memswp(p, p + td->o.verify_offset, hdr_size(td, hdr));
1093 * fill body of io_u->buf with random data and add a header with the
1094 * checksum of choice
1096 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1098 if (td->o.verify == VERIFY_NULL)
1101 io_u->numberio = td->io_issues[io_u->ddir];
1103 fill_pattern_headers(td, io_u, 0, 0);
1106 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1108 struct io_piece *ipo = NULL;
1111 * this io_u is from a requeue, we already filled the offsets
1116 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1117 struct rb_node *n = rb_first(&td->io_hist_tree);
1119 ipo = rb_entry(n, struct io_piece, rb_node);
1122 * Ensure that the associated IO has completed
1125 if (ipo->flags & IP_F_IN_FLIGHT)
1128 rb_erase(n, &td->io_hist_tree);
1129 assert(ipo->flags & IP_F_ONRB);
1130 ipo->flags &= ~IP_F_ONRB;
1131 } else if (!flist_empty(&td->io_hist_list)) {
1132 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1135 * Ensure that the associated IO has completed
1138 if (ipo->flags & IP_F_IN_FLIGHT)
1141 flist_del(&ipo->list);
1142 assert(ipo->flags & IP_F_ONLIST);
1143 ipo->flags &= ~IP_F_ONLIST;
1149 io_u->offset = ipo->offset;
1150 io_u->buflen = ipo->len;
1151 io_u->numberio = ipo->numberio;
1152 io_u->file = ipo->file;
1153 io_u_set(io_u, IO_U_F_VER_LIST);
1155 if (ipo->flags & IP_F_TRIMMED)
1156 io_u_set(io_u, IO_U_F_TRIMMED);
1158 if (!fio_file_open(io_u->file)) {
1159 int r = td_io_open_file(td, io_u->file);
1162 dprint(FD_VERIFY, "failed file %s open\n",
1163 io_u->file->file_name);
1168 get_file(ipo->file);
1169 assert(fio_file_open(io_u->file));
1170 io_u->ddir = DDIR_READ;
1171 io_u->xfer_buf = io_u->buf;
1172 io_u->xfer_buflen = io_u->buflen;
1174 remove_trim_entry(td, ipo);
1176 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1178 if (!td->o.verify_pattern_bytes) {
1179 io_u->rand_seed = __rand(&td->verify_state);
1180 if (sizeof(int) != sizeof(long *))
1181 io_u->rand_seed *= __rand(&td->verify_state);
1187 dprint(FD_VERIFY, "get_next_verify: empty\n");
1191 void fio_verify_init(struct thread_data *td)
1193 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1194 td->o.verify == VERIFY_CRC32C) {
1195 crc32c_intel_probe();
1199 static void *verify_async_thread(void *data)
1201 struct thread_data *td = data;
1205 if (fio_option_is_set(&td->o, verify_cpumask) &&
1206 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1207 log_err("fio: failed setting verify thread affinity\n");
1215 if (td->verify_thread_exit)
1218 pthread_mutex_lock(&td->io_u_lock);
1220 while (flist_empty(&td->verify_list) &&
1221 !td->verify_thread_exit) {
1222 ret = pthread_cond_wait(&td->verify_cond,
1225 pthread_mutex_unlock(&td->io_u_lock);
1230 flist_splice_init(&td->verify_list, &list);
1231 pthread_mutex_unlock(&td->io_u_lock);
1233 if (flist_empty(&list))
1236 while (!flist_empty(&list)) {
1237 io_u = flist_first_entry(&list, struct io_u, verify_list);
1238 flist_del_init(&io_u->verify_list);
1240 io_u_set(io_u, IO_U_F_NO_FILE_PUT);
1241 ret = verify_io_u(td, &io_u);
1246 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1247 update_error_count(td, ret);
1255 td_verror(td, ret, "async_verify");
1256 if (td->o.verify_fatal)
1257 fio_mark_td_terminate(td);
1261 pthread_mutex_lock(&td->io_u_lock);
1262 td->nr_verify_threads--;
1263 pthread_mutex_unlock(&td->io_u_lock);
1265 pthread_cond_signal(&td->free_cond);
1269 int verify_async_init(struct thread_data *td)
1272 pthread_attr_t attr;
1274 pthread_attr_init(&attr);
1275 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1277 td->verify_thread_exit = 0;
1279 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1280 for (i = 0; i < td->o.verify_async; i++) {
1281 ret = pthread_create(&td->verify_threads[i], &attr,
1282 verify_async_thread, td);
1284 log_err("fio: async verify creation failed: %s\n",
1288 ret = pthread_detach(td->verify_threads[i]);
1290 log_err("fio: async verify thread detach failed: %s\n",
1294 td->nr_verify_threads++;
1297 pthread_attr_destroy(&attr);
1299 if (i != td->o.verify_async) {
1300 log_err("fio: only %d verify threads started, exiting\n", i);
1301 td->verify_thread_exit = 1;
1303 pthread_cond_broadcast(&td->verify_cond);
1310 void verify_async_exit(struct thread_data *td)
1312 td->verify_thread_exit = 1;
1314 pthread_cond_broadcast(&td->verify_cond);
1316 pthread_mutex_lock(&td->io_u_lock);
1318 while (td->nr_verify_threads)
1319 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1321 pthread_mutex_unlock(&td->io_u_lock);
1322 free(td->verify_threads);
1323 td->verify_threads = NULL;
1326 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1328 struct all_io_list *rep;
1329 struct thread_data *td;
1334 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1337 * Calculate reply space needed. We need one 'io_state' per thread,
1338 * and the size will vary depending on depth.
1342 for_each_td(td, i) {
1343 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1346 td->flags |= TD_F_VSTATE_SAVED;
1347 depth += td->o.iodepth;
1355 *sz += nr * sizeof(struct thread_io_list);
1356 *sz += depth * sizeof(uint64_t);
1359 rep->threads = cpu_to_le64((uint64_t) nr);
1361 next = &rep->state[0];
1362 for_each_td(td, i) {
1363 struct thread_io_list *s = next;
1366 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1369 if (td->last_write_comp) {
1372 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1373 comps = td->io_blocks[DDIR_WRITE];
1375 comps = td->o.iodepth;
1377 k = td->last_write_idx - 1;
1378 for (j = 0; j < comps; j++) {
1380 k = td->o.iodepth - 1;
1381 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1387 s->no_comps = cpu_to_le64((uint64_t) comps);
1388 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1389 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1390 s->index = cpu_to_le64((uint64_t) i);
1391 if (td->random_state.use64) {
1392 s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
1393 s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
1394 s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
1395 s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
1396 s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
1397 s->rand.state64.s[5] = 0;
1398 s->rand.use64 = cpu_to_le64((uint64_t)1);
1400 s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
1401 s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
1402 s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
1403 s->rand.state32.s[3] = 0;
1406 s->name[sizeof(s->name) - 1] = '\0';
1407 strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
1408 next = io_list_next(s);
1414 static int open_state_file(const char *name, const char *prefix, int num,
1422 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1426 verify_state_gen_name(out, sizeof(out), name, prefix, num);
1428 fd = open(out, flags, 0644);
1430 perror("fio: open state file");
1437 static int write_thread_list_state(struct thread_io_list *s,
1440 struct verify_state_hdr hdr;
1445 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1449 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1451 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1452 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1453 hdr.crc = cpu_to_le64(crc);
1454 ret = write(fd, &hdr, sizeof(hdr));
1455 if (ret != sizeof(hdr))
1458 ret = write(fd, s, thread_io_list_sz(s));
1459 if (ret != thread_io_list_sz(s)) {
1462 perror("fio: write state file");
1463 log_err("fio: failed to write state file\n");
1472 void __verify_save_state(struct all_io_list *state, const char *prefix)
1474 struct thread_io_list *s = &state->state[0];
1477 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1478 write_thread_list_state(s, prefix);
1479 s = io_list_next(s);
1483 void verify_save_state(int mask)
1485 struct all_io_list *state;
1488 state = get_all_io_list(mask, &sz);
1490 char prefix[PATH_MAX];
1493 sprintf(aux_path, "%s%slocal", aux_path, FIO_OS_PATH_SEPARATOR);
1495 strcpy(aux_path, "local");
1497 __verify_save_state(state, prefix);
1502 void verify_free_state(struct thread_data *td)
1508 static struct thread_io_list *convert_v1_list(struct thread_io_list_v1 *s)
1510 struct thread_io_list *til;
1513 til = malloc(__thread_io_list_sz(s->no_comps));
1514 til->no_comps = s->no_comps;
1515 til->depth = s->depth;
1516 til->numberio = s->numberio;
1517 til->index = s->index;
1518 memcpy(til->name, s->name, sizeof(til->name));
1520 til->rand.use64 = 0;
1521 for (i = 0; i < 4; i++)
1522 til->rand.state32.s[i] = s->rand.s[i];
1524 for (i = 0; i < s->no_comps; i++)
1525 til->offsets[i] = s->offsets[i];
1530 void verify_convert_assign_state(struct thread_data *td, void *p, int version)
1532 struct thread_io_list *til;
1536 struct thread_io_list_v1 *s = p;
1538 s->no_comps = le64_to_cpu(s->no_comps);
1539 s->depth = le64_to_cpu(s->depth);
1540 s->numberio = le64_to_cpu(s->numberio);
1541 for (i = 0; i < 4; i++)
1542 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1543 for (i = 0; i < s->no_comps; i++)
1544 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1546 til = convert_v1_list(s);
1549 struct thread_io_list *s = p;
1551 s->no_comps = le64_to_cpu(s->no_comps);
1552 s->depth = le64_to_cpu(s->depth);
1553 s->numberio = le64_to_cpu(s->numberio);
1554 s->rand.use64 = le64_to_cpu(s->rand.use64);
1556 if (s->rand.use64) {
1557 for (i = 0; i < 6; i++)
1558 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
1560 for (i = 0; i < 4; i++)
1561 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1563 for (i = 0; i < s->no_comps; i++)
1564 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1572 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s,
1577 hdr->version = le64_to_cpu(hdr->version);
1578 hdr->size = le64_to_cpu(hdr->size);
1579 hdr->crc = le64_to_cpu(hdr->crc);
1581 if (hdr->version != VSTATE_HDR_VERSION ||
1582 hdr->version != VSTATE_HDR_VERSION_V1)
1585 crc = fio_crc32c((void *)s, hdr->size);
1586 if (crc != hdr->crc)
1589 *version = hdr->version;
1593 int verify_load_state(struct thread_data *td, const char *prefix)
1595 struct verify_state_hdr hdr;
1601 if (!td->o.verify_state)
1604 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1608 ret = read(fd, &hdr, sizeof(hdr));
1609 if (ret != sizeof(hdr)) {
1611 td_verror(td, errno, "read verify state hdr");
1612 log_err("fio: failed reading verify state header\n");
1616 hdr.version = le64_to_cpu(hdr.version);
1617 hdr.size = le64_to_cpu(hdr.size);
1618 hdr.crc = le64_to_cpu(hdr.crc);
1620 if (hdr.version != VSTATE_HDR_VERSION &&
1621 hdr.version != VSTATE_HDR_VERSION_V1) {
1622 log_err("fio: bad version in verify state header\n");
1626 s = malloc(hdr.size);
1627 ret = read(fd, s, hdr.size);
1628 if (ret != hdr.size) {
1630 td_verror(td, errno, "read verify state");
1631 log_err("fio: failed reading verity state\n");
1635 crc = fio_crc32c(s, hdr.size);
1636 if (crc != hdr.crc) {
1637 log_err("fio: verify state is corrupt\n");
1643 verify_convert_assign_state(td, s, hdr.version);
1653 * Use the loaded verify state to know when to stop doing verification
1655 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1657 struct thread_io_list *s = td->vstate;
1664 * If we're not into the window of issues - depth yet, continue. If
1665 * issue is shorter than depth, do check.
1667 if ((td->io_blocks[DDIR_READ] < s->depth ||
1668 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1669 s->numberio > s->depth)
1673 * We're in the window of having to check if this io was
1674 * completed or not. If the IO was seen as completed, then
1677 for (i = 0; i < s->no_comps; i++)
1678 if (io_u->offset == s->offsets[i])
1682 * Not found, we have to stop