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)
86 struct verify_header swap;
88 memcpy(&swap, buf1, len);
89 memcpy(buf1, buf2, len);
90 memcpy(buf2, &swap, len);
93 static void hexdump(void *buffer, int len)
95 unsigned char *p = buffer;
98 for (i = 0; i < len; i++)
99 log_info("%02x", p[i]);
104 * Prepare for seperation of verify_header and checksum header
106 static inline unsigned int __hdr_size(int verify_type)
110 switch (verify_type) {
116 len = sizeof(struct vhdr_md5);
119 len = sizeof(struct vhdr_crc64);
122 len = sizeof(struct vhdr_crc32);
125 len = sizeof(struct vhdr_crc16);
128 len = sizeof(struct vhdr_crc7);
131 len = sizeof(struct vhdr_sha256);
134 len = sizeof(struct vhdr_sha512);
137 log_err("fio: unknown verify header!\n");
141 return len + sizeof(struct verify_header);
144 static inline unsigned int hdr_size(struct verify_header *hdr)
146 return __hdr_size(hdr->verify_type);
149 static void *hdr_priv(struct verify_header *hdr)
153 return priv + sizeof(struct verify_header);
157 * Return data area 'header_num'
159 static inline void *io_u_verify_off(struct verify_header *hdr,
161 unsigned char header_num)
163 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
166 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
167 unsigned int header_num)
169 void *p = io_u_verify_off(hdr, io_u, header_num);
170 struct vhdr_sha512 *vh = hdr_priv(hdr);
172 struct sha512_ctx sha512_ctx = {
176 sha512_init(&sha512_ctx);
177 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
179 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
180 log_err("sha512: verify failed at %llu/%u\n",
181 io_u->offset + header_num * hdr->len,
183 hexdump(vh->sha512, sizeof(vh->sha512));
184 hexdump(sha512_ctx.buf, sizeof(sha512));
191 static int verify_io_u_sha256(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_sha256 *vh = hdr_priv(hdr);
197 struct sha256_ctx sha256_ctx = {
201 sha256_init(&sha256_ctx);
202 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
204 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
205 log_err("sha256: verify failed at %llu/%u\n",
206 io_u->offset + header_num * hdr->len,
208 hexdump(vh->sha256, sizeof(vh->sha256));
209 hexdump(sha256_ctx.buf, sizeof(sha256));
216 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
217 unsigned char header_num)
219 void *p = io_u_verify_off(hdr, io_u, header_num);
220 struct vhdr_crc7 *vh = hdr_priv(hdr);
223 c = crc7(p, hdr->len - hdr_size(hdr));
226 log_err("crc7: verify failed at %llu/%u\n",
227 io_u->offset + header_num * hdr->len,
229 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
236 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
237 unsigned int header_num)
239 void *p = io_u_verify_off(hdr, io_u, header_num);
240 struct vhdr_crc16 *vh = hdr_priv(hdr);
243 c = crc16(p, hdr->len - hdr_size(hdr));
245 if (c != vh->crc16) {
246 log_err("crc16: verify failed at %llu/%u\n",
247 io_u->offset + header_num * hdr->len,
249 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
256 static int verify_io_u_crc64(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_crc64 *vh = hdr_priv(hdr);
261 unsigned long long c;
263 c = crc64(p, hdr->len - hdr_size(hdr));
265 if (c != vh->crc64) {
266 log_err("crc64: verify failed at %llu/%u\n",
267 io_u->offset + header_num * hdr->len,
269 log_err("crc64: wanted %llx, got %llx\n", (unsigned long long) vh->crc64, c);
276 static int verify_io_u_crc32(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_crc32 *vh = hdr_priv(hdr);
283 c = crc32(p, hdr->len - hdr_size(hdr));
285 if (c != vh->crc32) {
286 log_err("crc32: verify failed at %llu/%u\n",
287 io_u->offset + header_num * hdr->len,
289 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
296 static int verify_io_u_md5(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_md5 *vh = hdr_priv(hdr);
301 uint32_t hash[MD5_HASH_WORDS];
302 struct md5_ctx md5_ctx = {
307 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
309 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
310 log_err("md5: verify failed at %llu/%u\n",
311 io_u->offset + header_num * hdr->len,
313 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
314 hexdump(md5_ctx.hash, sizeof(hash));
321 int verify_io_u(struct thread_data *td, struct io_u *io_u)
323 struct verify_header *hdr;
324 unsigned int hdr_inc, hdr_num = 0;
328 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
331 hdr_inc = io_u->buflen;
332 if (td->o.verify_interval)
333 hdr_inc = td->o.verify_interval;
335 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
336 if (td->o.verify_offset)
337 memswp(p, p + td->o.verify_offset, __hdr_size(td->o.verify));
341 if (hdr->fio_magic != FIO_HDR_MAGIC) {
342 log_err("Bad verify header %x\n", hdr->fio_magic);
346 switch (hdr->verify_type) {
348 ret = verify_io_u_md5(hdr, io_u, hdr_num);
351 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
354 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
357 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
360 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
363 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
366 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
369 log_err("Bad verify type %u\n", hdr->verify_type);
378 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
380 struct vhdr_sha512 *vh = hdr_priv(hdr);
381 struct sha512_ctx sha512_ctx = {
385 sha512_init(&sha512_ctx);
386 sha512_update(&sha512_ctx, p, len);
389 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
391 struct vhdr_sha256 *vh = hdr_priv(hdr);
392 struct sha256_ctx sha256_ctx = {
396 sha256_init(&sha256_ctx);
397 sha256_update(&sha256_ctx, p, len);
400 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
402 struct vhdr_crc7 *vh = hdr_priv(hdr);
404 vh->crc7 = crc7(p, len);
407 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
409 struct vhdr_crc16 *vh = hdr_priv(hdr);
411 vh->crc16 = crc16(p, len);
414 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
416 struct vhdr_crc32 *vh = hdr_priv(hdr);
418 vh->crc32 = crc32(p, len);
421 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
423 struct vhdr_crc64 *vh = hdr_priv(hdr);
425 vh->crc64 = crc64(p, len);
428 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
430 struct vhdr_md5 *vh = hdr_priv(hdr);
431 struct md5_ctx md5_ctx = {
432 .hash = (uint32_t *) vh->md5_digest,
436 md5_update(&md5_ctx, p, len);
440 * fill body of io_u->buf with random data and add a header with the
441 * crc32 or md5 sum of that data.
443 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
445 struct verify_header *hdr;
446 void *p = io_u->buf, *data;
447 unsigned int hdr_inc, data_len;
449 if (td->o.verify == VERIFY_NULL)
452 fill_pattern(td, p, io_u->buflen);
454 hdr_inc = io_u->buflen;
455 if (td->o.verify_interval)
456 hdr_inc = td->o.verify_interval;
458 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
461 hdr->fio_magic = FIO_HDR_MAGIC;
462 hdr->verify_type = td->o.verify;
464 data_len = hdr_inc - hdr_size(hdr);
466 data = p + hdr_size(hdr);
467 switch (td->o.verify) {
469 fill_md5(hdr, data, data_len);
472 fill_crc64(hdr, data, data_len);
475 fill_crc32(hdr, data, data_len);
478 fill_crc16(hdr, data, data_len);
481 fill_crc7(hdr, data, data_len);
484 fill_sha256(hdr, data, data_len);
487 fill_sha512(hdr, data, data_len);
490 log_err("fio: bad verify type: %d\n", td->o.verify);
493 if (td->o.verify_offset)
494 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
498 int get_next_verify(struct thread_data *td, struct io_u *io_u)
500 struct io_piece *ipo = NULL;
503 * this io_u is from a requeue, we already filled the offsets
508 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
509 struct rb_node *n = rb_first(&td->io_hist_tree);
511 ipo = rb_entry(n, struct io_piece, rb_node);
512 rb_erase(n, &td->io_hist_tree);
513 } else if (!list_empty(&td->io_hist_list)) {
514 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
515 list_del(&ipo->list);
519 io_u->offset = ipo->offset;
520 io_u->buflen = ipo->len;
521 io_u->file = ipo->file;
523 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
524 int r = td_io_open_file(td, io_u->file);
531 assert(io_u->file->flags & FIO_FILE_OPEN);
532 io_u->ddir = DDIR_READ;
533 io_u->xfer_buf = io_u->buf;
534 io_u->xfer_buflen = io_u->buflen;