2 * IO verification helpers
12 #include "crc/crc64.h"
13 #include "crc/crc32.h"
14 #include "crc/crc32c.h"
15 #include "crc/crc16.h"
17 #include "crc/sha256.h"
18 #include "crc/sha512.h"
20 static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
26 r = os_random_long(&td->verify_state);
29 * lrand48_r seems to be broken and only fill the bottom
30 * 32-bits, even on 64-bit archs with 64-bit longs
43 static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
45 switch (td->o.verify_pattern_bytes) {
47 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
48 fill_random_bytes(td, p, len);
51 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
52 memset(p, td->o.verify_pattern, len);
57 unsigned int pattern = td->o.verify_pattern;
59 unsigned char c1, c2, c3, c4;
62 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
63 td->o.verify_pattern_bytes, len);
78 if (td->o.verify_pattern_bytes == 2 || i == len)
81 if (td->o.verify_pattern_bytes == 3 || i == len)
90 static void memswp(void *buf1, void *buf2, unsigned int len)
94 assert(len <= sizeof(swap));
96 memcpy(&swap, buf1, len);
97 memcpy(buf1, buf2, len);
98 memcpy(buf2, &swap, len);
101 static void hexdump(void *buffer, int len)
103 unsigned char *p = buffer;
106 for (i = 0; i < len; i++)
107 log_info("%02x", p[i]);
112 * Prepare for seperation of verify_header and checksum header
114 static inline unsigned int __hdr_size(int verify_type)
116 unsigned int len = len;
118 switch (verify_type) {
124 len = sizeof(struct vhdr_md5);
127 len = sizeof(struct vhdr_crc64);
131 len = sizeof(struct vhdr_crc32);
134 len = sizeof(struct vhdr_crc16);
137 len = sizeof(struct vhdr_crc7);
140 len = sizeof(struct vhdr_sha256);
143 len = sizeof(struct vhdr_sha512);
146 len = sizeof(struct vhdr_meta);
149 log_err("fio: unknown verify header!\n");
153 return len + sizeof(struct verify_header);
156 static inline unsigned int hdr_size(struct verify_header *hdr)
158 return __hdr_size(hdr->verify_type);
161 static void *hdr_priv(struct verify_header *hdr)
165 return priv + sizeof(struct verify_header);
169 * Return data area 'header_num'
171 static inline void *io_u_verify_off(struct verify_header *hdr,
172 struct io_u *io_u, unsigned char header_num)
174 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
177 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
178 struct io_u *io_u, unsigned int header_num)
180 struct vhdr_meta *vh = hdr_priv(hdr);
182 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
184 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
185 log_err("meta: verify failed at %llu/%u\n",
186 io_u->offset + header_num * hdr->len, hdr->len);
193 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
194 unsigned int header_num)
196 void *p = io_u_verify_off(hdr, io_u, header_num);
197 struct vhdr_sha512 *vh = hdr_priv(hdr);
199 struct sha512_ctx sha512_ctx = {
203 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
205 sha512_init(&sha512_ctx);
206 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
208 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
209 log_err("sha512: verify failed at %llu/%u\n",
210 io_u->offset + header_num * hdr->len, hdr->len);
211 hexdump(vh->sha512, sizeof(vh->sha512));
212 hexdump(sha512_ctx.buf, sizeof(sha512));
219 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
220 unsigned int header_num)
222 void *p = io_u_verify_off(hdr, io_u, header_num);
223 struct vhdr_sha256 *vh = hdr_priv(hdr);
225 struct sha256_ctx sha256_ctx = {
229 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
231 sha256_init(&sha256_ctx);
232 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
234 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
235 log_err("sha256: verify failed at %llu/%u\n",
236 io_u->offset + header_num * hdr->len, hdr->len);
237 hexdump(vh->sha256, sizeof(vh->sha256));
238 hexdump(sha256_ctx.buf, sizeof(sha256));
245 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
246 unsigned char header_num)
248 void *p = io_u_verify_off(hdr, io_u, header_num);
249 struct vhdr_crc7 *vh = hdr_priv(hdr);
252 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
254 c = crc7(p, hdr->len - hdr_size(hdr));
257 log_err("crc7: verify failed at %llu/%u\n",
258 io_u->offset + header_num * hdr->len, hdr->len);
259 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
266 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
267 unsigned int header_num)
269 void *p = io_u_verify_off(hdr, io_u, header_num);
270 struct vhdr_crc16 *vh = hdr_priv(hdr);
273 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
275 c = crc16(p, hdr->len - hdr_size(hdr));
277 if (c != vh->crc16) {
278 log_err("crc16: verify failed at %llu/%u\n",
279 io_u->offset + header_num * hdr->len, hdr->len);
280 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
287 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
288 unsigned int header_num)
290 void *p = io_u_verify_off(hdr, io_u, header_num);
291 struct vhdr_crc64 *vh = hdr_priv(hdr);
292 unsigned long long c;
294 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
296 c = crc64(p, hdr->len - hdr_size(hdr));
298 if (c != vh->crc64) {
299 log_err("crc64: verify failed at %llu/%u\n",
300 io_u->offset + header_num * hdr->len,
302 log_err("crc64: wanted %llx, got %llx\n",
303 (unsigned long long) vh->crc64, c);
310 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
311 unsigned int header_num)
313 void *p = io_u_verify_off(hdr, io_u, header_num);
314 struct vhdr_crc32 *vh = hdr_priv(hdr);
317 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
319 c = crc32(p, hdr->len - hdr_size(hdr));
321 if (c != vh->crc32) {
322 log_err("crc32: verify failed at %llu/%u\n",
323 io_u->offset + header_num * hdr->len, hdr->len);
324 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
331 static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
332 unsigned int header_num)
334 void *p = io_u_verify_off(hdr, io_u, header_num);
335 struct vhdr_crc32 *vh = hdr_priv(hdr);
338 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
340 c = crc32c(p, hdr->len - hdr_size(hdr));
342 if (c != vh->crc32) {
343 log_err("crc32c: verify failed at %llu/%u\n",
344 io_u->offset + header_num * hdr->len, hdr->len);
345 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
352 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
353 unsigned int header_num)
355 void *p = io_u_verify_off(hdr, io_u, header_num);
356 struct vhdr_md5 *vh = hdr_priv(hdr);
357 uint32_t hash[MD5_HASH_WORDS];
358 struct md5_ctx md5_ctx = {
362 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
365 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
367 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
368 log_err("md5: verify failed at %llu/%u\n",
369 io_u->offset + header_num * hdr->len, hdr->len);
370 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
371 hexdump(md5_ctx.hash, sizeof(hash));
378 static unsigned int hweight8(unsigned int w)
380 unsigned int res = w - ((w >> 1) & 0x55);
382 res = (res & 0x33) + ((res >> 2) & 0x33);
383 return (res + (res >> 4)) & 0x0F;
386 int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
387 char *buf, unsigned int len, unsigned int mod)
390 char split_pattern[4];
392 for (i = 0; i < 4; i++) {
393 split_pattern[i] = pattern & 0xff;
397 for (i = 0; i < len; i++) {
398 if (buf[i] != split_pattern[mod]) {
401 bits = hweight8(buf[i] ^ split_pattern[mod]);
402 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
403 buf[i], split_pattern[mod], bits);
404 log_err("fio: bad pattern block offset %u\n", i);
408 if (mod == pattern_size)
415 int verify_io_u(struct thread_data *td, struct io_u *io_u)
417 struct verify_header *hdr;
418 unsigned int hdr_size, hdr_inc, hdr_num = 0;
422 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
425 hdr_inc = io_u->buflen;
426 if (td->o.verify_interval)
427 hdr_inc = td->o.verify_interval;
430 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
431 p += hdr_inc, hdr_num++) {
432 if (ret && td->o.verify_fatal) {
436 hdr_size = __hdr_size(td->o.verify);
437 if (td->o.verify_offset)
438 memswp(p, p + td->o.verify_offset, hdr_size);
441 if (hdr->fio_magic != FIO_HDR_MAGIC) {
442 log_err("Bad verify header %x\n", hdr->fio_magic);
446 if (td->o.verify_pattern_bytes) {
447 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
449 ret = verify_io_u_pattern(td->o.verify_pattern,
450 td->o.verify_pattern_bytes,
455 log_err("fio: verify failed at %llu/%u\n",
456 io_u->offset + hdr_num * hdr->len,
461 switch (hdr->verify_type) {
463 ret = verify_io_u_md5(hdr, io_u, hdr_num);
466 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
469 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
472 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
475 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
478 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
481 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
484 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
487 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
490 log_err("Bad verify type %u\n", hdr->verify_type);
498 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
499 struct io_u *io_u, unsigned int header_num)
501 struct vhdr_meta *vh = hdr_priv(hdr);
503 vh->thread = td->thread_number;
505 vh->time_sec = io_u->start_time.tv_sec;
506 vh->time_usec = io_u->start_time.tv_usec;
508 vh->numberio = td->io_issues[DDIR_WRITE];
510 vh->offset = io_u->offset + header_num * td->o.verify_interval;
513 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
515 struct vhdr_sha512 *vh = hdr_priv(hdr);
516 struct sha512_ctx sha512_ctx = {
520 sha512_init(&sha512_ctx);
521 sha512_update(&sha512_ctx, p, len);
524 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
526 struct vhdr_sha256 *vh = hdr_priv(hdr);
527 struct sha256_ctx sha256_ctx = {
531 sha256_init(&sha256_ctx);
532 sha256_update(&sha256_ctx, p, len);
535 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
537 struct vhdr_crc7 *vh = hdr_priv(hdr);
539 vh->crc7 = crc7(p, len);
542 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
544 struct vhdr_crc16 *vh = hdr_priv(hdr);
546 vh->crc16 = crc16(p, len);
549 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
551 struct vhdr_crc32 *vh = hdr_priv(hdr);
553 vh->crc32 = crc32(p, len);
556 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
558 struct vhdr_crc32 *vh = hdr_priv(hdr);
560 vh->crc32 = crc32c(p, len);
563 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
565 struct vhdr_crc64 *vh = hdr_priv(hdr);
567 vh->crc64 = crc64(p, len);
570 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
572 struct vhdr_md5 *vh = hdr_priv(hdr);
573 struct md5_ctx md5_ctx = {
574 .hash = (uint32_t *) vh->md5_digest,
578 md5_update(&md5_ctx, p, len);
582 * fill body of io_u->buf with random data and add a header with the
583 * crc32 or md5 sum of that data.
585 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
587 struct verify_header *hdr;
588 void *p = io_u->buf, *data;
589 unsigned int hdr_inc, data_len, header_num = 0;
591 if (td->o.verify == VERIFY_NULL)
594 fill_pattern(td, p, io_u->buflen);
596 hdr_inc = io_u->buflen;
597 if (td->o.verify_interval)
598 hdr_inc = td->o.verify_interval;
600 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
603 hdr->fio_magic = FIO_HDR_MAGIC;
604 hdr->verify_type = td->o.verify;
606 data_len = hdr_inc - hdr_size(hdr);
608 data = p + hdr_size(hdr);
609 switch (td->o.verify) {
611 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
613 fill_md5(hdr, data, data_len);
616 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
618 fill_crc64(hdr, data, data_len);
621 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
623 fill_crc32c(hdr, data, data_len);
626 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
628 fill_crc32(hdr, data, data_len);
631 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
633 fill_crc16(hdr, data, data_len);
636 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
638 fill_crc7(hdr, data, data_len);
641 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
643 fill_sha256(hdr, data, data_len);
646 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
648 fill_sha512(hdr, data, data_len);
651 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
653 fill_meta(hdr, td, io_u, header_num);
656 log_err("fio: bad verify type: %d\n", td->o.verify);
659 if (td->o.verify_offset)
660 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
665 int get_next_verify(struct thread_data *td, struct io_u *io_u)
667 struct io_piece *ipo = NULL;
670 * this io_u is from a requeue, we already filled the offsets
675 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
676 struct rb_node *n = rb_first(&td->io_hist_tree);
678 ipo = rb_entry(n, struct io_piece, rb_node);
679 rb_erase(n, &td->io_hist_tree);
680 } else if (!flist_empty(&td->io_hist_list)) {
681 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
682 flist_del(&ipo->list);
686 io_u->offset = ipo->offset;
687 io_u->buflen = ipo->len;
688 io_u->file = ipo->file;
690 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
691 int r = td_io_open_file(td, io_u->file);
694 dprint(FD_VERIFY, "failed file %s open\n",
695 io_u->file->file_name);
701 assert(io_u->file->flags & FIO_FILE_OPEN);
702 io_u->ddir = DDIR_READ;
703 io_u->xfer_buf = io_u->buf;
704 io_u->xfer_buflen = io_u->buflen;
706 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
710 dprint(FD_VERIFY, "get_next_verify: empty\n");