Add 'filesize' option
[fio.git] / verify.c
CommitLineData
e29d1b70
JA
1/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
7
8#include "fio.h"
9#include "os.h"
10
11static void fill_random_bytes(struct thread_data *td,
12 unsigned char *p, unsigned int len)
13{
14 unsigned int todo;
15 double r;
16
17 while (len) {
18 r = os_random_double(&td->verify_state);
19
20 /*
21 * lrand48_r seems to be broken and only fill the bottom
22 * 32-bits, even on 64-bit archs with 64-bit longs
23 */
24 todo = sizeof(r);
25 if (todo > len)
26 todo = len;
27
28 memcpy(p, &r, todo);
29
30 len -= todo;
31 p += todo;
32 }
33}
34
35static void hexdump(void *buffer, int len)
36{
37 unsigned char *p = buffer;
38 int i;
39
40 for (i = 0; i < len; i++)
41 fprintf(f_out, "%02x", p[i]);
42 fprintf(f_out, "\n");
43}
44
45static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
46{
47 unsigned char *p = (unsigned char *) io_u->buf;
48 unsigned long c;
49
50 p += sizeof(*hdr);
51 c = crc32(p, hdr->len - sizeof(*hdr));
52
53 if (c != hdr->crc32) {
a4f4fdd7 54 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
e29d1b70
JA
55 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
56 return 1;
57 }
58
59 return 0;
60}
61
62static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
63{
64 unsigned char *p = (unsigned char *) io_u->buf;
65 struct md5_ctx md5_ctx;
66
67 memset(&md5_ctx, 0, sizeof(md5_ctx));
68 p += sizeof(*hdr);
69 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
70
71 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
a4f4fdd7 72 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
e29d1b70
JA
73 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
74 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
75 return 1;
76 }
77
78 return 0;
79}
80
3af6ef39 81int verify_io_u(struct io_u *io_u)
e29d1b70
JA
82{
83 struct verify_header *hdr = (struct verify_header *) io_u->buf;
84 int ret;
85
36167d82
JA
86 if (hdr->fio_magic != FIO_HDR_MAGIC) {
87 log_err("Bad verify header %x\n", hdr->fio_magic);
a7dfe862 88 return EIO;
36167d82 89 }
e29d1b70
JA
90
91 if (hdr->verify_type == VERIFY_MD5)
92 ret = verify_io_u_md5(hdr, io_u);
93 else if (hdr->verify_type == VERIFY_CRC32)
94 ret = verify_io_u_crc32(hdr, io_u);
95 else {
1e97cce9 96 log_err("Bad verify type %u\n", hdr->verify_type);
e29d1b70
JA
97 ret = 1;
98 }
99
a7dfe862
JA
100 if (ret)
101 return EIO;
102
103 return 0;
e29d1b70
JA
104}
105
106static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
107{
108 hdr->crc32 = crc32(p, len);
109}
110
111static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
112{
113 struct md5_ctx md5_ctx;
114
115 memset(&md5_ctx, 0, sizeof(md5_ctx));
116 md5_update(&md5_ctx, p, len);
117 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
118}
119
120/*
121 * fill body of io_u->buf with random data and add a header with the
122 * crc32 or md5 sum of that data.
123 */
124void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
125{
126 unsigned char *p = (unsigned char *) io_u->buf;
127 struct verify_header hdr;
128
129 hdr.fio_magic = FIO_HDR_MAGIC;
130 hdr.len = io_u->buflen;
131 p += sizeof(hdr);
132 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
133
134 if (td->verify == VERIFY_MD5) {
135 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
136 hdr.verify_type = VERIFY_MD5;
137 } else {
138 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
139 hdr.verify_type = VERIFY_CRC32;
140 }
141
142 memcpy(io_u->buf, &hdr, sizeof(hdr));
143}
144
145int get_next_verify(struct thread_data *td, struct io_u *io_u)
146{
147 struct io_piece *ipo;
148
d2d7fa53
JA
149 /*
150 * this io_u is from a requeue, we already filled the offsets
151 */
152 if (io_u->file)
153 return 0;
154
e29d1b70
JA
155 if (!list_empty(&td->io_hist_list)) {
156 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
157
158 list_del(&ipo->list);
159
160 io_u->offset = ipo->offset;
161 io_u->buflen = ipo->len;
36167d82 162 io_u->file = ipo->file;
e29d1b70 163 io_u->ddir = DDIR_READ;
36167d82
JA
164 io_u->xfer_buf = io_u->buf;
165 io_u->xfer_buflen = io_u->buflen;
e29d1b70
JA
166 free(ipo);
167 return 0;
168 }
169
170 return 1;
171}