2 * IO verification helpers
12 #include "crc/crc64.h"
13 #include "crc/crc32.h"
14 #include "crc/crc16.h"
16 #include "crc/sha256.h"
17 #include "crc/sha512.h"
19 static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
25 r = os_random_long(&td->verify_state);
28 * lrand48_r seems to be broken and only fill the bottom
29 * 32-bits, even on 64-bit archs with 64-bit longs
42 static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
44 switch (td->o.verify_pattern_bytes) {
46 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
47 fill_random_bytes(td, p, len);
50 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
51 memset(p, td->o.verify_pattern, len);
56 unsigned int pattern = td->o.verify_pattern;
58 unsigned char c1, c2, c3, c4;
61 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
62 td->o.verify_pattern_bytes, len);
77 if (td->o.verify_pattern_bytes == 2 || i == len)
80 if (td->o.verify_pattern_bytes == 3 || i == len)
89 static void memswp(void *buf1, void *buf2, unsigned int len)
93 assert(len <= sizeof(swap));
95 memcpy(&swap, buf1, len);
96 memcpy(buf1, buf2, len);
97 memcpy(buf2, &swap, len);
100 static void hexdump(void *buffer, int len)
102 unsigned char *p = buffer;
105 for (i = 0; i < len; i++)
106 log_info("%02x", p[i]);
111 * Prepare for seperation of verify_header and checksum header
113 static inline unsigned int __hdr_size(int verify_type)
115 unsigned int len = len;
117 switch (verify_type) {
123 len = sizeof(struct vhdr_md5);
126 len = sizeof(struct vhdr_crc64);
129 len = sizeof(struct vhdr_crc32);
132 len = sizeof(struct vhdr_crc16);
135 len = sizeof(struct vhdr_crc7);
138 len = sizeof(struct vhdr_sha256);
141 len = sizeof(struct vhdr_sha512);
144 len = sizeof(struct vhdr_meta);
147 log_err("fio: unknown verify header!\n");
151 return len + sizeof(struct verify_header);
154 static inline unsigned int hdr_size(struct verify_header *hdr)
156 return __hdr_size(hdr->verify_type);
159 static void *hdr_priv(struct verify_header *hdr)
163 return priv + sizeof(struct verify_header);
167 * Return data area 'header_num'
169 static inline void *io_u_verify_off(struct verify_header *hdr,
170 struct io_u *io_u, unsigned char header_num)
172 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
175 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
176 struct io_u *io_u, unsigned int header_num)
178 struct vhdr_meta *vh = hdr_priv(hdr);
180 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
182 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
183 log_err("meta: verify failed at %llu/%u\n",
184 io_u->offset + header_num * hdr->len, hdr->len);
191 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
192 unsigned int header_num)
194 void *p = io_u_verify_off(hdr, io_u, header_num);
195 struct vhdr_sha512 *vh = hdr_priv(hdr);
197 struct sha512_ctx sha512_ctx = {
201 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
203 sha512_init(&sha512_ctx);
204 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
206 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
207 log_err("sha512: verify failed at %llu/%u\n",
208 io_u->offset + header_num * hdr->len, hdr->len);
209 hexdump(vh->sha512, sizeof(vh->sha512));
210 hexdump(sha512_ctx.buf, sizeof(sha512));
217 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
218 unsigned int header_num)
220 void *p = io_u_verify_off(hdr, io_u, header_num);
221 struct vhdr_sha256 *vh = hdr_priv(hdr);
223 struct sha256_ctx sha256_ctx = {
227 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
229 sha256_init(&sha256_ctx);
230 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
232 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
233 log_err("sha256: verify failed at %llu/%u\n",
234 io_u->offset + header_num * hdr->len, hdr->len);
235 hexdump(vh->sha256, sizeof(vh->sha256));
236 hexdump(sha256_ctx.buf, sizeof(sha256));
243 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
244 unsigned char header_num)
246 void *p = io_u_verify_off(hdr, io_u, header_num);
247 struct vhdr_crc7 *vh = hdr_priv(hdr);
250 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
252 c = crc7(p, hdr->len - hdr_size(hdr));
255 log_err("crc7: verify failed at %llu/%u\n",
256 io_u->offset + header_num * hdr->len, hdr->len);
257 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
264 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
265 unsigned int header_num)
267 void *p = io_u_verify_off(hdr, io_u, header_num);
268 struct vhdr_crc16 *vh = hdr_priv(hdr);
271 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
273 c = crc16(p, hdr->len - hdr_size(hdr));
275 if (c != vh->crc16) {
276 log_err("crc16: verify failed at %llu/%u\n",
277 io_u->offset + header_num * hdr->len, hdr->len);
278 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
285 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
286 unsigned int header_num)
288 void *p = io_u_verify_off(hdr, io_u, header_num);
289 struct vhdr_crc64 *vh = hdr_priv(hdr);
290 unsigned long long c;
292 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
294 c = crc64(p, hdr->len - hdr_size(hdr));
296 if (c != vh->crc64) {
297 log_err("crc64: verify failed at %llu/%u\n",
298 io_u->offset + header_num * hdr->len,
300 log_err("crc64: wanted %llx, got %llx\n",
301 (unsigned long long) vh->crc64, c);
308 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
309 unsigned int header_num)
311 void *p = io_u_verify_off(hdr, io_u, header_num);
312 struct vhdr_crc32 *vh = hdr_priv(hdr);
315 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
317 c = crc32(p, hdr->len - hdr_size(hdr));
319 if (c != vh->crc32) {
320 log_err("crc32: verify failed at %llu/%u\n",
321 io_u->offset + header_num * hdr->len, hdr->len);
322 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
329 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
330 unsigned int header_num)
332 void *p = io_u_verify_off(hdr, io_u, header_num);
333 struct vhdr_md5 *vh = hdr_priv(hdr);
334 uint32_t hash[MD5_HASH_WORDS];
335 struct md5_ctx md5_ctx = {
339 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
342 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
344 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
345 log_err("md5: verify failed at %llu/%u\n",
346 io_u->offset + header_num * hdr->len, hdr->len);
347 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
348 hexdump(md5_ctx.hash, sizeof(hash));
355 static unsigned int hweight8(unsigned int w)
357 unsigned int res = w - ((w >> 1) & 0x55);
359 res = (res & 0x33) + ((res >> 2) & 0x33);
360 return (res + (res >> 4)) & 0x0F;
363 int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
364 char *buf, unsigned int len, unsigned int mod)
367 char split_pattern[4];
369 for (i = 0; i < 4; i++) {
370 split_pattern[i] = pattern & 0xff;
374 for (i = 0; i < len; i++) {
375 if (buf[i] != split_pattern[mod]) {
378 bits = hweight8(buf[i] ^ split_pattern[mod]);
379 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
380 buf[i], split_pattern[mod], bits);
381 log_err("fio: bad pattern block offset %u\n", i);
385 if (mod == pattern_size)
392 int verify_io_u(struct thread_data *td, struct io_u *io_u)
394 struct verify_header *hdr;
395 unsigned int hdr_size, hdr_inc, hdr_num = 0;
399 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
402 hdr_inc = io_u->buflen;
403 if (td->o.verify_interval)
404 hdr_inc = td->o.verify_interval;
407 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
408 p += hdr_inc, hdr_num++) {
409 if (ret && td->o.verify_fatal) {
413 hdr_size = __hdr_size(td->o.verify);
414 if (td->o.verify_offset)
415 memswp(p, p + td->o.verify_offset, hdr_size);
418 if (hdr->fio_magic != FIO_HDR_MAGIC) {
419 log_err("Bad verify header %x\n", hdr->fio_magic);
423 if (td->o.verify_pattern_bytes) {
424 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
426 ret = verify_io_u_pattern(td->o.verify_pattern,
427 td->o.verify_pattern_bytes,
432 log_err("fio: verify failed at %llu/%u\n",
433 io_u->offset + hdr_num * hdr->len,
438 switch (hdr->verify_type) {
440 ret = verify_io_u_md5(hdr, io_u, hdr_num);
443 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
446 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
449 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
452 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
455 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
458 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
461 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
464 log_err("Bad verify type %u\n", hdr->verify_type);
472 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
473 struct io_u *io_u, unsigned int header_num)
475 struct vhdr_meta *vh = hdr_priv(hdr);
477 vh->thread = td->thread_number;
479 vh->time_sec = io_u->start_time.tv_sec;
480 vh->time_usec = io_u->start_time.tv_usec;
482 vh->numberio = td->io_issues[DDIR_WRITE];
484 vh->offset = io_u->offset + header_num * td->o.verify_interval;
487 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
489 struct vhdr_sha512 *vh = hdr_priv(hdr);
490 struct sha512_ctx sha512_ctx = {
494 sha512_init(&sha512_ctx);
495 sha512_update(&sha512_ctx, p, len);
498 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
500 struct vhdr_sha256 *vh = hdr_priv(hdr);
501 struct sha256_ctx sha256_ctx = {
505 sha256_init(&sha256_ctx);
506 sha256_update(&sha256_ctx, p, len);
509 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
511 struct vhdr_crc7 *vh = hdr_priv(hdr);
513 vh->crc7 = crc7(p, len);
516 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
518 struct vhdr_crc16 *vh = hdr_priv(hdr);
520 vh->crc16 = crc16(p, len);
523 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
525 struct vhdr_crc32 *vh = hdr_priv(hdr);
527 vh->crc32 = crc32(p, len);
530 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
532 struct vhdr_crc64 *vh = hdr_priv(hdr);
534 vh->crc64 = crc64(p, len);
537 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
539 struct vhdr_md5 *vh = hdr_priv(hdr);
540 struct md5_ctx md5_ctx = {
541 .hash = (uint32_t *) vh->md5_digest,
545 md5_update(&md5_ctx, p, len);
549 * fill body of io_u->buf with random data and add a header with the
550 * crc32 or md5 sum of that data.
552 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
554 struct verify_header *hdr;
555 void *p = io_u->buf, *data;
556 unsigned int hdr_inc, data_len, header_num = 0;
558 if (td->o.verify == VERIFY_NULL)
561 fill_pattern(td, p, io_u->buflen);
563 hdr_inc = io_u->buflen;
564 if (td->o.verify_interval)
565 hdr_inc = td->o.verify_interval;
567 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
570 hdr->fio_magic = FIO_HDR_MAGIC;
571 hdr->verify_type = td->o.verify;
573 data_len = hdr_inc - hdr_size(hdr);
575 data = p + hdr_size(hdr);
576 switch (td->o.verify) {
578 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
580 fill_md5(hdr, data, data_len);
583 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
585 fill_crc64(hdr, data, data_len);
588 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
590 fill_crc32(hdr, data, data_len);
593 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
595 fill_crc16(hdr, data, data_len);
598 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
600 fill_crc7(hdr, data, data_len);
603 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
605 fill_sha256(hdr, data, data_len);
608 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
610 fill_sha512(hdr, data, data_len);
613 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
615 fill_meta(hdr, td, io_u, header_num);
618 log_err("fio: bad verify type: %d\n", td->o.verify);
621 if (td->o.verify_offset)
622 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
627 int get_next_verify(struct thread_data *td, struct io_u *io_u)
629 struct io_piece *ipo = NULL;
632 * this io_u is from a requeue, we already filled the offsets
637 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
638 struct rb_node *n = rb_first(&td->io_hist_tree);
640 ipo = rb_entry(n, struct io_piece, rb_node);
641 rb_erase(n, &td->io_hist_tree);
642 } else if (!list_empty(&td->io_hist_list)) {
643 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
644 list_del(&ipo->list);
648 io_u->offset = ipo->offset;
649 io_u->buflen = ipo->len;
650 io_u->file = ipo->file;
652 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
653 int r = td_io_open_file(td, io_u->file);
656 dprint(FD_VERIFY, "failed file %s open\n",
657 io_u->file->file_name);
663 assert(io_u->file->flags & FIO_FILE_OPEN);
664 io_u->ddir = DDIR_READ;
665 io_u->xfer_buf = io_u->buf;
666 io_u->xfer_buflen = io_u->buflen;
668 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
672 dprint(FD_VERIFY, "get_next_verify: empty\n");