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 fill_random_bytes(td, p, len);
49 memset(p, td->o.verify_pattern, len);
54 unsigned int pattern = td->o.verify_pattern;
56 unsigned char c1, c2, c3, c4;
72 if (td->o.verify_pattern_bytes == 2 || i == len)
75 if (td->o.verify_pattern_bytes == 3 || i == len)
84 static void memswp(void *buf1, void *buf2, unsigned int len)
88 assert(len <= sizeof(swap));
90 memcpy(&swap, buf1, len);
91 memcpy(buf1, buf2, len);
92 memcpy(buf2, &swap, len);
95 static void hexdump(void *buffer, int len)
97 unsigned char *p = buffer;
100 for (i = 0; i < len; i++)
101 log_info("%02x", p[i]);
106 * Prepare for seperation of verify_header and checksum header
108 static inline unsigned int __hdr_size(int verify_type)
112 switch (verify_type) {
119 len = sizeof(struct vhdr_md5);
122 len = sizeof(struct vhdr_crc64);
125 len = sizeof(struct vhdr_crc32);
128 len = sizeof(struct vhdr_crc16);
131 len = sizeof(struct vhdr_crc7);
134 len = sizeof(struct vhdr_sha256);
137 len = sizeof(struct vhdr_sha512);
140 len = sizeof(struct vhdr_meta);
143 log_err("fio: unknown verify header!\n");
147 return len + sizeof(struct verify_header);
150 static inline unsigned int hdr_size(struct verify_header *hdr)
152 return __hdr_size(hdr->verify_type);
155 static void *hdr_priv(struct verify_header *hdr)
159 return priv + sizeof(struct verify_header);
163 * Return data area 'header_num'
165 static inline void *io_u_verify_off(struct verify_header *hdr,
167 unsigned char header_num)
169 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
172 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
173 struct io_u *io_u, unsigned int header_num)
175 struct vhdr_meta *vh = hdr_priv(hdr);
177 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
178 log_err("meta: verify failed at %llu/%u\n",
179 io_u->offset + header_num * hdr->len,
187 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
188 unsigned int header_num)
190 void *p = io_u_verify_off(hdr, io_u, header_num);
191 struct vhdr_sha512 *vh = hdr_priv(hdr);
193 struct sha512_ctx sha512_ctx = {
197 sha512_init(&sha512_ctx);
198 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
200 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
201 log_err("sha512: verify failed at %llu/%u\n",
202 io_u->offset + header_num * hdr->len,
204 hexdump(vh->sha512, sizeof(vh->sha512));
205 hexdump(sha512_ctx.buf, sizeof(sha512));
212 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
213 unsigned int header_num)
215 void *p = io_u_verify_off(hdr, io_u, header_num);
216 struct vhdr_sha256 *vh = hdr_priv(hdr);
218 struct sha256_ctx sha256_ctx = {
222 sha256_init(&sha256_ctx);
223 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
225 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
226 log_err("sha256: verify failed at %llu/%u\n",
227 io_u->offset + header_num * hdr->len,
229 hexdump(vh->sha256, sizeof(vh->sha256));
230 hexdump(sha256_ctx.buf, sizeof(sha256));
237 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
238 unsigned char header_num)
240 void *p = io_u_verify_off(hdr, io_u, header_num);
241 struct vhdr_crc7 *vh = hdr_priv(hdr);
244 c = crc7(p, hdr->len - hdr_size(hdr));
247 log_err("crc7: verify failed at %llu/%u\n",
248 io_u->offset + header_num * hdr->len,
250 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
257 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
258 unsigned int header_num)
260 void *p = io_u_verify_off(hdr, io_u, header_num);
261 struct vhdr_crc16 *vh = hdr_priv(hdr);
264 c = crc16(p, hdr->len - hdr_size(hdr));
266 if (c != vh->crc16) {
267 log_err("crc16: verify failed at %llu/%u\n",
268 io_u->offset + header_num * hdr->len,
270 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
277 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
278 unsigned int header_num)
280 void *p = io_u_verify_off(hdr, io_u, header_num);
281 struct vhdr_crc64 *vh = hdr_priv(hdr);
282 unsigned long long c;
284 c = crc64(p, hdr->len - hdr_size(hdr));
286 if (c != vh->crc64) {
287 log_err("crc64: verify failed at %llu/%u\n",
288 io_u->offset + header_num * hdr->len,
290 log_err("crc64: wanted %llx, got %llx\n", (unsigned long long) vh->crc64, c);
297 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
298 unsigned int header_num)
300 void *p = io_u_verify_off(hdr, io_u, header_num);
301 struct vhdr_crc32 *vh = hdr_priv(hdr);
304 c = crc32(p, hdr->len - hdr_size(hdr));
306 if (c != vh->crc32) {
307 log_err("crc32: verify failed at %llu/%u\n",
308 io_u->offset + header_num * hdr->len,
310 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
317 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
318 unsigned int header_num)
320 void *p = io_u_verify_off(hdr, io_u, header_num);
321 struct vhdr_md5 *vh = hdr_priv(hdr);
322 uint32_t hash[MD5_HASH_WORDS];
323 struct md5_ctx md5_ctx = {
328 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
330 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
331 log_err("md5: verify failed at %llu/%u\n",
332 io_u->offset + header_num * hdr->len,
334 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
335 hexdump(md5_ctx.hash, sizeof(hash));
342 static unsigned int hweight8(unsigned int w)
344 unsigned int res = w - ((w >> 1) & 0x55);
346 res = (res & 0x33) + ((res >> 2) & 0x33);
347 return (res + (res >> 4)) & 0x0F;
350 int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
351 char *buf, unsigned int len, unsigned int mod)
354 char split_pattern[4];
356 for (i = 0; i < 4; i++) {
357 split_pattern[i] = pattern & 0xff;
361 for (i = 0; i < len; i++) {
362 if (buf[i] != split_pattern[mod]) {
365 bits = hweight8(buf[i] ^ split_pattern[mod]);
366 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
367 buf[i], split_pattern[mod], bits);
368 log_err("fio: bad pattern block offset %u\n", i);
372 if (mod == pattern_size)
379 int verify_io_u(struct thread_data *td, struct io_u *io_u)
381 struct verify_header *hdr;
382 unsigned int hdr_size, hdr_inc, hdr_num = 0;
386 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
389 hdr_inc = io_u->buflen;
390 if (td->o.verify_interval)
391 hdr_inc = td->o.verify_interval;
394 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc, hdr_num++) {
395 if (ret && td->o.verify_fatal) {
399 hdr_size = __hdr_size(td->o.verify);
400 if (td->o.verify_offset)
401 memswp(p, p + td->o.verify_offset, hdr_size);
404 if (hdr->fio_magic != FIO_HDR_MAGIC) {
405 log_err("Bad verify header %x\n", hdr->fio_magic);
409 switch (hdr->verify_type) {
411 ret = verify_io_u_md5(hdr, io_u, hdr_num);
414 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
417 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
420 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
423 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
426 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
429 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
432 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
435 ret = verify_io_u_pattern(td->o.verify_pattern,
436 td->o.verify_pattern_bytes,
441 log_err("fio: verify failed at %llu/%u\n",
442 io_u->offset + hdr_num * hdr->len,
446 log_err("Bad verify type %u\n", hdr->verify_type);
454 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
455 struct io_u *io_u, unsigned int header_num)
457 struct vhdr_meta *vh = hdr_priv(hdr);
459 vh->thread = td->thread_number;
461 vh->time_sec = io_u->start_time.tv_sec;
462 vh->time_usec = io_u->start_time.tv_usec;
464 vh->numberio = td->io_issues[DDIR_WRITE];
466 vh->offset = io_u->offset + header_num * td->o.verify_interval;
469 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
471 struct vhdr_sha512 *vh = hdr_priv(hdr);
472 struct sha512_ctx sha512_ctx = {
476 sha512_init(&sha512_ctx);
477 sha512_update(&sha512_ctx, p, len);
480 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
482 struct vhdr_sha256 *vh = hdr_priv(hdr);
483 struct sha256_ctx sha256_ctx = {
487 sha256_init(&sha256_ctx);
488 sha256_update(&sha256_ctx, p, len);
491 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
493 struct vhdr_crc7 *vh = hdr_priv(hdr);
495 vh->crc7 = crc7(p, len);
498 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
500 struct vhdr_crc16 *vh = hdr_priv(hdr);
502 vh->crc16 = crc16(p, len);
505 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
507 struct vhdr_crc32 *vh = hdr_priv(hdr);
509 vh->crc32 = crc32(p, len);
512 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
514 struct vhdr_crc64 *vh = hdr_priv(hdr);
516 vh->crc64 = crc64(p, len);
519 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
521 struct vhdr_md5 *vh = hdr_priv(hdr);
522 struct md5_ctx md5_ctx = {
523 .hash = (uint32_t *) vh->md5_digest,
527 md5_update(&md5_ctx, p, len);
531 * fill body of io_u->buf with random data and add a header with the
532 * crc32 or md5 sum of that data.
534 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
536 struct verify_header *hdr;
537 void *p = io_u->buf, *data;
538 unsigned int hdr_inc, data_len, header_num = 0;
540 if (td->o.verify == VERIFY_NULL)
543 fill_pattern(td, p, io_u->buflen);
545 hdr_inc = io_u->buflen;
546 if (td->o.verify_interval)
547 hdr_inc = td->o.verify_interval;
549 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
552 hdr->fio_magic = FIO_HDR_MAGIC;
553 hdr->verify_type = td->o.verify;
555 data_len = hdr_inc - hdr_size(hdr);
557 data = p + hdr_size(hdr);
558 switch (td->o.verify) {
560 fill_md5(hdr, data, data_len);
563 fill_crc64(hdr, data, data_len);
566 fill_crc32(hdr, data, data_len);
569 fill_crc16(hdr, data, data_len);
572 fill_crc7(hdr, data, data_len);
575 fill_sha256(hdr, data, data_len);
578 fill_sha512(hdr, data, data_len);
581 fill_meta(hdr, td, io_u, header_num);
586 log_err("fio: bad verify type: %d\n", td->o.verify);
589 if (td->o.verify_offset)
590 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
595 int get_next_verify(struct thread_data *td, struct io_u *io_u)
597 struct io_piece *ipo = NULL;
600 * this io_u is from a requeue, we already filled the offsets
605 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
606 struct rb_node *n = rb_first(&td->io_hist_tree);
608 ipo = rb_entry(n, struct io_piece, rb_node);
609 rb_erase(n, &td->io_hist_tree);
610 } else if (!list_empty(&td->io_hist_list)) {
611 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
612 list_del(&ipo->list);
616 io_u->offset = ipo->offset;
617 io_u->buflen = ipo->len;
618 io_u->file = ipo->file;
620 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
621 int r = td_io_open_file(td, io_u->file);
628 assert(io_u->file->flags & FIO_FILE_OPEN);
629 io_u->ddir = DDIR_READ;
630 io_u->xfer_buf = io_u->buf;
631 io_u->xfer_buflen = io_u->buflen;