Commit | Line | Data |
---|---|---|
e29d1b70 JA |
1 | /* |
2 | * IO verification helpers | |
3 | */ | |
4 | #include <unistd.h> | |
5 | #include <fcntl.h> | |
6 | #include <string.h> | |
97af62ce | 7 | #include <assert.h> |
e29d1b70 JA |
8 | |
9 | #include "fio.h" | |
e29d1b70 JA |
10 | |
11 | static void fill_random_bytes(struct thread_data *td, | |
12 | unsigned char *p, unsigned int len) | |
13 | { | |
14 | unsigned int todo; | |
4c5946c6 | 15 | int r; |
e29d1b70 JA |
16 | |
17 | while (len) { | |
4c5946c6 | 18 | r = os_random_long(&td->verify_state); |
e29d1b70 JA |
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 | ||
35 | static void hexdump(void *buffer, int len) | |
36 | { | |
37 | unsigned char *p = buffer; | |
38 | int i; | |
39 | ||
40 | for (i = 0; i < len; i++) | |
6d86144d JA |
41 | log_info("%02x", p[i]); |
42 | log_info("\n"); | |
e29d1b70 JA |
43 | } |
44 | ||
1e154bdb JA |
45 | static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u) |
46 | { | |
47 | unsigned char *p = io_u->buf; | |
48 | unsigned char c; | |
49 | ||
50 | p += sizeof(*hdr); | |
51 | c = crc7(p, hdr->len - sizeof(*hdr)); | |
52 | ||
53 | if (c != hdr->crc7) { | |
54 | log_err("crc7: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen); | |
55 | log_err("crc7: wanted %x, got %x\n", hdr->crc7, c); | |
56 | return 1; | |
57 | } | |
58 | ||
59 | return 0; | |
60 | } | |
61 | ||
969f7ed3 JA |
62 | static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u) |
63 | { | |
64 | unsigned char *p = io_u->buf; | |
65 | unsigned short c; | |
66 | ||
67 | p += sizeof(*hdr); | |
68 | c = crc16(p, hdr->len - sizeof(*hdr)); | |
69 | ||
70 | if (c != hdr->crc16) { | |
71 | log_err("crc16: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen); | |
1f24ea44 | 72 | log_err("crc16: wanted %x, got %x\n", hdr->crc16, c); |
969f7ed3 JA |
73 | return 1; |
74 | } | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
d77a7af3 JA |
79 | static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u) |
80 | { | |
81 | unsigned char *p = io_u->buf + sizeof(*hdr); | |
82 | unsigned long long c; | |
83 | ||
84 | c = crc64(p, hdr->len - sizeof(*hdr)); | |
85 | ||
86 | if (c != hdr->crc64) { | |
87 | log_err("crc64: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen); | |
88 | log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c); | |
89 | return 1; | |
90 | } | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
e29d1b70 JA |
95 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) |
96 | { | |
969f7ed3 | 97 | unsigned char *p = io_u->buf; |
e29d1b70 JA |
98 | unsigned long c; |
99 | ||
100 | p += sizeof(*hdr); | |
101 | c = crc32(p, hdr->len - sizeof(*hdr)); | |
102 | ||
103 | if (c != hdr->crc32) { | |
a4f4fdd7 | 104 | log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen); |
e29d1b70 JA |
105 | log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c); |
106 | return 1; | |
107 | } | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
112 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) | |
113 | { | |
8c432325 JA |
114 | unsigned char *p = io_u->buf + sizeof(*hdr); |
115 | uint32_t hash[MD5_HASH_WORDS]; | |
116 | struct md5_ctx md5_ctx = { | |
117 | .hash = hash, | |
118 | }; | |
e29d1b70 | 119 | |
e29d1b70 JA |
120 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); |
121 | ||
8c432325 | 122 | if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) { |
a4f4fdd7 | 123 | log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen); |
e29d1b70 | 124 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); |
8c432325 | 125 | hexdump(md5_ctx.hash, sizeof(hash)); |
e29d1b70 JA |
126 | return 1; |
127 | } | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
36690c9b | 132 | int verify_io_u(struct thread_data *td, struct io_u *io_u) |
e29d1b70 JA |
133 | { |
134 | struct verify_header *hdr = (struct verify_header *) io_u->buf; | |
135 | int ret; | |
136 | ||
1dcc0498 | 137 | if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ) |
36690c9b JA |
138 | return 0; |
139 | ||
36167d82 JA |
140 | if (hdr->fio_magic != FIO_HDR_MAGIC) { |
141 | log_err("Bad verify header %x\n", hdr->fio_magic); | |
a7dfe862 | 142 | return EIO; |
36167d82 | 143 | } |
e29d1b70 | 144 | |
a3ff21e3 JA |
145 | switch (hdr->verify_type) { |
146 | case VERIFY_MD5: | |
e29d1b70 | 147 | ret = verify_io_u_md5(hdr, io_u); |
a3ff21e3 | 148 | break; |
d77a7af3 JA |
149 | case VERIFY_CRC64: |
150 | ret = verify_io_u_crc64(hdr, io_u); | |
151 | break; | |
a3ff21e3 | 152 | case VERIFY_CRC32: |
e29d1b70 | 153 | ret = verify_io_u_crc32(hdr, io_u); |
a3ff21e3 JA |
154 | break; |
155 | case VERIFY_CRC16: | |
969f7ed3 | 156 | ret = verify_io_u_crc16(hdr, io_u); |
a3ff21e3 JA |
157 | break; |
158 | case VERIFY_CRC7: | |
1e154bdb | 159 | ret = verify_io_u_crc7(hdr, io_u); |
a3ff21e3 JA |
160 | break; |
161 | default: | |
1e97cce9 | 162 | log_err("Bad verify type %u\n", hdr->verify_type); |
e29d1b70 JA |
163 | ret = 1; |
164 | } | |
165 | ||
a7dfe862 JA |
166 | if (ret) |
167 | return EIO; | |
168 | ||
169 | return 0; | |
e29d1b70 JA |
170 | } |
171 | ||
1e154bdb JA |
172 | static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len) |
173 | { | |
174 | hdr->crc7 = crc7(p, len); | |
175 | } | |
176 | ||
969f7ed3 JA |
177 | static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len) |
178 | { | |
179 | hdr->crc16 = crc16(p, len); | |
180 | } | |
181 | ||
e29d1b70 JA |
182 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
183 | { | |
184 | hdr->crc32 = crc32(p, len); | |
185 | } | |
186 | ||
d77a7af3 JA |
187 | static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len) |
188 | { | |
189 | hdr->crc64 = crc64(p, len); | |
190 | } | |
191 | ||
e29d1b70 JA |
192 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
193 | { | |
8c432325 JA |
194 | struct md5_ctx md5_ctx = { |
195 | .hash = (uint32_t *) hdr->md5_digest, | |
196 | }; | |
e29d1b70 | 197 | |
e29d1b70 | 198 | md5_update(&md5_ctx, p, len); |
e29d1b70 JA |
199 | } |
200 | ||
201 | /* | |
202 | * fill body of io_u->buf with random data and add a header with the | |
203 | * crc32 or md5 sum of that data. | |
204 | */ | |
205 | void populate_verify_io_u(struct thread_data *td, struct io_u *io_u) | |
206 | { | |
baefa9be JA |
207 | const unsigned int len = io_u->buflen - sizeof(struct verify_header); |
208 | struct verify_header *hdr; | |
209 | unsigned char *p; | |
e29d1b70 | 210 | |
9cc3d150 JA |
211 | if (td->o.verify == VERIFY_NULL) |
212 | return; | |
213 | ||
baefa9be JA |
214 | hdr = (struct verify_header *) io_u->buf; |
215 | hdr->fio_magic = FIO_HDR_MAGIC; | |
216 | hdr->len = io_u->buflen; | |
217 | hdr->verify_type = td->o.verify; | |
218 | ||
219 | p = io_u->buf + sizeof(*hdr); | |
220 | fill_random_bytes(td, p, len); | |
221 | ||
222 | switch (td->o.verify) { | |
223 | case VERIFY_MD5: | |
224 | fill_md5(hdr, p, len); | |
225 | break; | |
d77a7af3 JA |
226 | case VERIFY_CRC64: |
227 | fill_crc64(hdr, p, len); | |
228 | break; | |
baefa9be JA |
229 | case VERIFY_CRC32: |
230 | fill_crc32(hdr, p, len); | |
231 | break; | |
232 | case VERIFY_CRC16: | |
233 | fill_crc16(hdr, p, len); | |
234 | break; | |
235 | case VERIFY_CRC7: | |
236 | fill_crc7(hdr, p, len); | |
237 | break; | |
238 | default: | |
239 | log_err("fio: bad verify type: %d\n", td->o.verify); | |
240 | assert(0); | |
e29d1b70 | 241 | } |
e29d1b70 JA |
242 | } |
243 | ||
244 | int get_next_verify(struct thread_data *td, struct io_u *io_u) | |
245 | { | |
8de8f047 | 246 | struct io_piece *ipo = NULL; |
e29d1b70 | 247 | |
d2d7fa53 JA |
248 | /* |
249 | * this io_u is from a requeue, we already filled the offsets | |
250 | */ | |
251 | if (io_u->file) | |
252 | return 0; | |
253 | ||
8de8f047 JA |
254 | if (!RB_EMPTY_ROOT(&td->io_hist_tree)) { |
255 | struct rb_node *n = rb_first(&td->io_hist_tree); | |
e29d1b70 | 256 | |
8de8f047 | 257 | ipo = rb_entry(n, struct io_piece, rb_node); |
4b87898e | 258 | rb_erase(n, &td->io_hist_tree); |
8de8f047 JA |
259 | } else if (!list_empty(&td->io_hist_list)) { |
260 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
261 | list_del(&ipo->list); | |
262 | } | |
e29d1b70 | 263 | |
8de8f047 | 264 | if (ipo) { |
e29d1b70 JA |
265 | io_u->offset = ipo->offset; |
266 | io_u->buflen = ipo->len; | |
36167d82 | 267 | io_u->file = ipo->file; |
97af62ce JA |
268 | |
269 | if ((io_u->file->flags & FIO_FILE_OPEN) == 0) { | |
270 | int r = td_io_open_file(td, io_u->file); | |
271 | ||
272 | if (r) | |
273 | return 1; | |
274 | } | |
275 | ||
276 | get_file(ipo->file); | |
277 | assert(io_u->file->flags & FIO_FILE_OPEN); | |
e29d1b70 | 278 | io_u->ddir = DDIR_READ; |
36167d82 JA |
279 | io_u->xfer_buf = io_u->buf; |
280 | io_u->xfer_buflen = io_u->buflen; | |
e29d1b70 JA |
281 | free(ipo); |
282 | return 0; | |
283 | } | |
284 | ||
285 | return 1; | |
286 | } |