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