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) {
118 len = sizeof(struct vhdr_md5);
121 len = sizeof(struct vhdr_crc64);
124 len = sizeof(struct vhdr_crc32);
127 len = sizeof(struct vhdr_crc16);
130 len = sizeof(struct vhdr_crc7);
133 len = sizeof(struct vhdr_sha256);
136 len = sizeof(struct vhdr_sha512);
139 len = sizeof(struct vhdr_meta);
142 log_err("fio: unknown verify header!\n");
146 return len + sizeof(struct verify_header);
149 static inline unsigned int hdr_size(struct verify_header *hdr)
151 return __hdr_size(hdr->verify_type);
154 static void *hdr_priv(struct verify_header *hdr)
158 return priv + sizeof(struct verify_header);
162 * Return data area 'header_num'
164 static inline void *io_u_verify_off(struct verify_header *hdr,
166 unsigned char header_num)
168 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
171 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
172 struct io_u *io_u, unsigned int header_num)
174 struct vhdr_meta *vh = hdr_priv(hdr);
176 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
177 log_err("meta: verify failed at %llu/%u\n",
178 io_u->offset + header_num * hdr->len,
186 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
187 unsigned int header_num)
189 void *p = io_u_verify_off(hdr, io_u, header_num);
190 struct vhdr_sha512 *vh = hdr_priv(hdr);
192 struct sha512_ctx sha512_ctx = {
196 sha512_init(&sha512_ctx);
197 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
199 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
200 log_err("sha512: verify failed at %llu/%u\n",
201 io_u->offset + header_num * hdr->len,
203 hexdump(vh->sha512, sizeof(vh->sha512));
204 hexdump(sha512_ctx.buf, sizeof(sha512));
211 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
212 unsigned int header_num)
214 void *p = io_u_verify_off(hdr, io_u, header_num);
215 struct vhdr_sha256 *vh = hdr_priv(hdr);
217 struct sha256_ctx sha256_ctx = {
221 sha256_init(&sha256_ctx);
222 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
224 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
225 log_err("sha256: verify failed at %llu/%u\n",
226 io_u->offset + header_num * hdr->len,
228 hexdump(vh->sha256, sizeof(vh->sha256));
229 hexdump(sha256_ctx.buf, sizeof(sha256));
236 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
237 unsigned char header_num)
239 void *p = io_u_verify_off(hdr, io_u, header_num);
240 struct vhdr_crc7 *vh = hdr_priv(hdr);
243 c = crc7(p, hdr->len - hdr_size(hdr));
246 log_err("crc7: verify failed at %llu/%u\n",
247 io_u->offset + header_num * hdr->len,
249 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
256 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
257 unsigned int header_num)
259 void *p = io_u_verify_off(hdr, io_u, header_num);
260 struct vhdr_crc16 *vh = hdr_priv(hdr);
263 c = crc16(p, hdr->len - hdr_size(hdr));
265 if (c != vh->crc16) {
266 log_err("crc16: verify failed at %llu/%u\n",
267 io_u->offset + header_num * hdr->len,
269 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
276 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
277 unsigned int header_num)
279 void *p = io_u_verify_off(hdr, io_u, header_num);
280 struct vhdr_crc64 *vh = hdr_priv(hdr);
281 unsigned long long c;
283 c = crc64(p, hdr->len - hdr_size(hdr));
285 if (c != vh->crc64) {
286 log_err("crc64: verify failed at %llu/%u\n",
287 io_u->offset + header_num * hdr->len,
289 log_err("crc64: wanted %llx, got %llx\n", (unsigned long long) vh->crc64, c);
296 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
297 unsigned int header_num)
299 void *p = io_u_verify_off(hdr, io_u, header_num);
300 struct vhdr_crc32 *vh = hdr_priv(hdr);
303 c = crc32(p, hdr->len - hdr_size(hdr));
305 if (c != vh->crc32) {
306 log_err("crc32: verify failed at %llu/%u\n",
307 io_u->offset + header_num * hdr->len,
309 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
316 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
317 unsigned int header_num)
319 void *p = io_u_verify_off(hdr, io_u, header_num);
320 struct vhdr_md5 *vh = hdr_priv(hdr);
321 uint32_t hash[MD5_HASH_WORDS];
322 struct md5_ctx md5_ctx = {
327 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
329 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
330 log_err("md5: verify failed at %llu/%u\n",
331 io_u->offset + header_num * hdr->len,
333 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
334 hexdump(md5_ctx.hash, sizeof(hash));
341 static unsigned int hweight8(unsigned int w)
343 unsigned int res = w - ((w >> 1) & 0x55);
345 res = (res & 0x33) + ((res >> 2) & 0x33);
346 return (res + (res >> 4)) & 0x0F;
349 int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
350 char* buf, unsigned int len, unsigned int mod)
353 char split_pattern[4];
355 for (i = 0; i < 4; i++) {
356 split_pattern[i] = pattern & 0xff;
360 for (i = 0; i < len; i++) {
361 if (buf[i] != split_pattern[mod]) {
364 bits = hweight8(buf[i] ^ split_pattern[mod]);
365 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
366 buf[i], split_pattern[mod], bits);
367 log_err("fio: bad pattern block offset %u\n", i);
371 if (mod == pattern_size)
378 int verify_io_u(struct thread_data *td, struct io_u *io_u)
380 struct verify_header *hdr;
381 unsigned int hdr_size, hdr_inc, hdr_num = 0;
385 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
388 hdr_inc = io_u->buflen;
389 if (td->o.verify_interval)
390 hdr_inc = td->o.verify_interval;
392 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc, hdr_num++) {
393 hdr_size = __hdr_size(td->o.verify);
394 if (td->o.verify_offset)
395 memswp(p, p + td->o.verify_offset, hdr_size);
398 if (hdr->fio_magic != FIO_HDR_MAGIC) {
399 log_err("Bad verify header %x\n", hdr->fio_magic);
403 if (td->o.verify_pattern_bytes) {
404 ret = verify_io_u_pattern(td->o.verify_pattern,
405 td->o.verify_pattern_bytes,
410 log_err("fio: verify failed at %llu/%u\n",
411 io_u->offset + hdr_num * hdr->len,
416 switch (hdr->verify_type) {
418 ret = verify_io_u_md5(hdr, io_u, hdr_num);
421 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
424 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
427 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
430 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
433 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
436 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
439 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
442 log_err("Bad verify type %u\n", hdr->verify_type);
450 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
451 struct io_u *io_u, unsigned int header_num)
453 struct vhdr_meta *vh = hdr_priv(hdr);
455 vh->thread = td->thread_number;
457 vh->time_sec = io_u->start_time.tv_sec;
458 vh->time_usec = io_u->start_time.tv_usec;
460 vh->numberio = td->io_issues[DDIR_WRITE];
462 vh->offset = io_u->offset + header_num * td->o.verify_interval;
465 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
467 struct vhdr_sha512 *vh = hdr_priv(hdr);
468 struct sha512_ctx sha512_ctx = {
472 sha512_init(&sha512_ctx);
473 sha512_update(&sha512_ctx, p, len);
476 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
478 struct vhdr_sha256 *vh = hdr_priv(hdr);
479 struct sha256_ctx sha256_ctx = {
483 sha256_init(&sha256_ctx);
484 sha256_update(&sha256_ctx, p, len);
487 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
489 struct vhdr_crc7 *vh = hdr_priv(hdr);
491 vh->crc7 = crc7(p, len);
494 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
496 struct vhdr_crc16 *vh = hdr_priv(hdr);
498 vh->crc16 = crc16(p, len);
501 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
503 struct vhdr_crc32 *vh = hdr_priv(hdr);
505 vh->crc32 = crc32(p, len);
508 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
510 struct vhdr_crc64 *vh = hdr_priv(hdr);
512 vh->crc64 = crc64(p, len);
515 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
517 struct vhdr_md5 *vh = hdr_priv(hdr);
518 struct md5_ctx md5_ctx = {
519 .hash = (uint32_t *) vh->md5_digest,
523 md5_update(&md5_ctx, p, len);
527 * fill body of io_u->buf with random data and add a header with the
528 * crc32 or md5 sum of that data.
530 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
532 struct verify_header *hdr;
533 void *p = io_u->buf, *data;
534 unsigned int hdr_inc, data_len, header_num = 0;
536 if (td->o.verify == VERIFY_NULL)
539 fill_pattern(td, p, io_u->buflen);
541 hdr_inc = io_u->buflen;
542 if (td->o.verify_interval)
543 hdr_inc = td->o.verify_interval;
545 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
548 hdr->fio_magic = FIO_HDR_MAGIC;
549 hdr->verify_type = td->o.verify;
551 data_len = hdr_inc - hdr_size(hdr);
553 data = p + hdr_size(hdr);
554 switch (td->o.verify) {
556 fill_md5(hdr, data, data_len);
559 fill_crc64(hdr, data, data_len);
562 fill_crc32(hdr, data, data_len);
565 fill_crc16(hdr, data, data_len);
568 fill_crc7(hdr, data, data_len);
571 fill_sha256(hdr, data, data_len);
574 fill_sha512(hdr, data, data_len);
577 fill_meta(hdr, td, io_u, header_num);
580 log_err("fio: bad verify type: %d\n", td->o.verify);
583 if (td->o.verify_offset)
584 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
589 int get_next_verify(struct thread_data *td, struct io_u *io_u)
591 struct io_piece *ipo = NULL;
594 * this io_u is from a requeue, we already filled the offsets
599 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
600 struct rb_node *n = rb_first(&td->io_hist_tree);
602 ipo = rb_entry(n, struct io_piece, rb_node);
603 rb_erase(n, &td->io_hist_tree);
604 } else if (!list_empty(&td->io_hist_list)) {
605 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
606 list_del(&ipo->list);
610 io_u->offset = ipo->offset;
611 io_u->buflen = ipo->len;
612 io_u->file = ipo->file;
614 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
615 int r = td_io_open_file(td, io_u->file);
622 assert(io_u->file->flags & FIO_FILE_OPEN);
623 io_u->ddir = DDIR_READ;
624 io_u->xfer_buf = io_u->buf;
625 io_u->xfer_buflen = io_u->buflen;