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 | 10 | |
eef6eea1 JA |
11 | #include "crc/md5.h" |
12 | #include "crc/crc64.h" | |
13 | #include "crc/crc32.h" | |
14 | #include "crc/crc16.h" | |
15 | #include "crc/crc7.h" | |
16 | #include "crc/sha256.h" | |
17 | #include "crc/sha512.h" | |
cd14cc10 | 18 | |
90059d65 | 19 | static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len) |
e29d1b70 JA |
20 | { |
21 | unsigned int todo; | |
4c5946c6 | 22 | int r; |
e29d1b70 JA |
23 | |
24 | while (len) { | |
4c5946c6 | 25 | r = os_random_long(&td->verify_state); |
e29d1b70 JA |
26 | |
27 | /* | |
28 | * lrand48_r seems to be broken and only fill the bottom | |
29 | * 32-bits, even on 64-bit archs with 64-bit longs | |
30 | */ | |
31 | todo = sizeof(r); | |
32 | if (todo > len) | |
33 | todo = len; | |
34 | ||
35 | memcpy(p, &r, todo); | |
36 | ||
37 | len -= todo; | |
38 | p += todo; | |
39 | } | |
40 | } | |
41 | ||
90059d65 JA |
42 | static void fill_pattern(struct thread_data *td, void *p, unsigned int len) |
43 | { | |
44 | switch (td->o.verify_pattern_bytes) { | |
45 | case 0: | |
bd6f78b2 | 46 | dprint(FD_VERIFY, "fill random bytes len=%u\n", len); |
90059d65 JA |
47 | fill_random_bytes(td, p, len); |
48 | break; | |
49 | case 1: | |
bd6f78b2 | 50 | dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len); |
90059d65 JA |
51 | memset(p, td->o.verify_pattern, len); |
52 | break; | |
53 | case 2: | |
54 | case 3: | |
55 | case 4: { | |
56 | unsigned int pattern = td->o.verify_pattern; | |
57 | unsigned int i = 0; | |
58 | unsigned char c1, c2, c3, c4; | |
59 | unsigned char *b = p; | |
60 | ||
bd6f78b2 JA |
61 | dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n", |
62 | td->o.verify_pattern_bytes, len); | |
63 | ||
90059d65 JA |
64 | c1 = pattern & 0xff; |
65 | pattern >>= 8; | |
66 | c2 = pattern & 0xff; | |
67 | pattern >>= 8; | |
68 | c3 = pattern & 0xff; | |
69 | pattern >>= 8; | |
70 | c4 = pattern & 0xff; | |
71 | ||
72 | while (i < len) { | |
73 | b[i++] = c1; | |
74 | if (i == len) | |
75 | break; | |
76 | b[i++] = c2; | |
77 | if (td->o.verify_pattern_bytes == 2 || i == len) | |
78 | continue; | |
79 | b[i++] = c3; | |
80 | if (td->o.verify_pattern_bytes == 3 || i == len) | |
81 | continue; | |
82 | b[i++] = c4; | |
83 | } | |
84 | break; | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
4764aec9 | 89 | static void memswp(void *buf1, void *buf2, unsigned int len) |
546a9142 | 90 | { |
dee6de74 SL |
91 | char swap[200]; |
92 | ||
93 | assert(len <= sizeof(swap)); | |
90059d65 | 94 | |
546a9142 SL |
95 | memcpy(&swap, buf1, len); |
96 | memcpy(buf1, buf2, len); | |
97 | memcpy(buf2, &swap, len); | |
98 | } | |
99 | ||
e29d1b70 JA |
100 | static void hexdump(void *buffer, int len) |
101 | { | |
102 | unsigned char *p = buffer; | |
103 | int i; | |
104 | ||
105 | for (i = 0; i < len; i++) | |
6d86144d JA |
106 | log_info("%02x", p[i]); |
107 | log_info("\n"); | |
e29d1b70 JA |
108 | } |
109 | ||
87677832 JA |
110 | /* |
111 | * Prepare for seperation of verify_header and checksum header | |
112 | */ | |
546dfd9f | 113 | static inline unsigned int __hdr_size(int verify_type) |
87677832 | 114 | { |
5921e80c | 115 | unsigned int len = len; |
87677832 | 116 | |
546dfd9f JA |
117 | switch (verify_type) { |
118 | case VERIFY_NONE: | |
119 | case VERIFY_NULL: | |
120 | len = 0; | |
121 | break; | |
122 | case VERIFY_MD5: | |
123 | len = sizeof(struct vhdr_md5); | |
124 | break; | |
125 | case VERIFY_CRC64: | |
126 | len = sizeof(struct vhdr_crc64); | |
127 | break; | |
128 | case VERIFY_CRC32: | |
129 | len = sizeof(struct vhdr_crc32); | |
130 | break; | |
131 | case VERIFY_CRC16: | |
132 | len = sizeof(struct vhdr_crc16); | |
133 | break; | |
134 | case VERIFY_CRC7: | |
135 | len = sizeof(struct vhdr_crc7); | |
136 | break; | |
137 | case VERIFY_SHA256: | |
138 | len = sizeof(struct vhdr_sha256); | |
139 | break; | |
140 | case VERIFY_SHA512: | |
141 | len = sizeof(struct vhdr_sha512); | |
142 | break; | |
7437ee87 SL |
143 | case VERIFY_META: |
144 | len = sizeof(struct vhdr_meta); | |
145 | break; | |
546dfd9f JA |
146 | default: |
147 | log_err("fio: unknown verify header!\n"); | |
148 | assert(0); | |
149 | } | |
150 | ||
151 | return len + sizeof(struct verify_header); | |
87677832 JA |
152 | } |
153 | ||
154 | static inline unsigned int hdr_size(struct verify_header *hdr) | |
155 | { | |
546dfd9f JA |
156 | return __hdr_size(hdr->verify_type); |
157 | } | |
158 | ||
159 | static void *hdr_priv(struct verify_header *hdr) | |
160 | { | |
161 | void *priv = hdr; | |
162 | ||
163 | return priv + sizeof(struct verify_header); | |
87677832 JA |
164 | } |
165 | ||
d9f2caf3 JA |
166 | /* |
167 | * Return data area 'header_num' | |
168 | */ | |
169 | static inline void *io_u_verify_off(struct verify_header *hdr, | |
5ec10eaa | 170 | struct io_u *io_u, unsigned char header_num) |
d9f2caf3 | 171 | { |
87677832 | 172 | return io_u->buf + header_num * hdr->len + hdr_size(hdr); |
d9f2caf3 JA |
173 | } |
174 | ||
7437ee87 | 175 | static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td, |
5ec10eaa | 176 | struct io_u *io_u, unsigned int header_num) |
7437ee87 SL |
177 | { |
178 | struct vhdr_meta *vh = hdr_priv(hdr); | |
179 | ||
bd6f78b2 JA |
180 | dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len); |
181 | ||
7437ee87 SL |
182 | if (vh->offset != io_u->offset + header_num * td->o.verify_interval) { |
183 | log_err("meta: verify failed at %llu/%u\n", | |
5ec10eaa | 184 | io_u->offset + header_num * hdr->len, hdr->len); |
d16d4e09 | 185 | return EIO; |
7437ee87 SL |
186 | } |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
cd14cc10 JA |
191 | static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u, |
192 | unsigned int header_num) | |
193 | { | |
194 | void *p = io_u_verify_off(hdr, io_u, header_num); | |
546dfd9f | 195 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
cd14cc10 JA |
196 | uint8_t sha512[128]; |
197 | struct sha512_ctx sha512_ctx = { | |
198 | .buf = sha512, | |
199 | }; | |
200 | ||
bd6f78b2 JA |
201 | dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len); |
202 | ||
cd14cc10 | 203 | sha512_init(&sha512_ctx); |
87677832 | 204 | sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr)); |
cd14cc10 | 205 | |
546dfd9f | 206 | if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) { |
cd14cc10 | 207 | log_err("sha512: verify failed at %llu/%u\n", |
5ec10eaa | 208 | io_u->offset + header_num * hdr->len, hdr->len); |
546dfd9f | 209 | hexdump(vh->sha512, sizeof(vh->sha512)); |
cd14cc10 | 210 | hexdump(sha512_ctx.buf, sizeof(sha512)); |
d16d4e09 | 211 | return EIO; |
cd14cc10 JA |
212 | } |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u, | |
218 | unsigned int header_num) | |
219 | { | |
220 | void *p = io_u_verify_off(hdr, io_u, header_num); | |
546dfd9f | 221 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
cd14cc10 JA |
222 | uint8_t sha256[128]; |
223 | struct sha256_ctx sha256_ctx = { | |
224 | .buf = sha256, | |
225 | }; | |
226 | ||
bd6f78b2 JA |
227 | dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len); |
228 | ||
cd14cc10 | 229 | sha256_init(&sha256_ctx); |
87677832 | 230 | sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr)); |
cd14cc10 | 231 | |
546dfd9f | 232 | if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) { |
cd14cc10 | 233 | log_err("sha256: verify failed at %llu/%u\n", |
5ec10eaa | 234 | io_u->offset + header_num * hdr->len, hdr->len); |
546dfd9f | 235 | hexdump(vh->sha256, sizeof(vh->sha256)); |
cd14cc10 | 236 | hexdump(sha256_ctx.buf, sizeof(sha256)); |
d16d4e09 | 237 | return EIO; |
cd14cc10 JA |
238 | } |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
3f9f4e26 | 243 | static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u, |
5ec10eaa | 244 | unsigned char header_num) |
1e154bdb | 245 | { |
d9f2caf3 | 246 | void *p = io_u_verify_off(hdr, io_u, header_num); |
546dfd9f | 247 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
1e154bdb JA |
248 | unsigned char c; |
249 | ||
bd6f78b2 JA |
250 | dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len); |
251 | ||
87677832 | 252 | c = crc7(p, hdr->len - hdr_size(hdr)); |
1e154bdb | 253 | |
546dfd9f | 254 | if (c != vh->crc7) { |
3f9f4e26 | 255 | log_err("crc7: verify failed at %llu/%u\n", |
5ec10eaa | 256 | io_u->offset + header_num * hdr->len, hdr->len); |
546dfd9f | 257 | log_err("crc7: wanted %x, got %x\n", vh->crc7, c); |
d16d4e09 | 258 | return EIO; |
1e154bdb JA |
259 | } |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
3f9f4e26 | 264 | static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u, |
5ec10eaa | 265 | unsigned int header_num) |
969f7ed3 | 266 | { |
d9f2caf3 | 267 | void *p = io_u_verify_off(hdr, io_u, header_num); |
546dfd9f | 268 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
969f7ed3 JA |
269 | unsigned short c; |
270 | ||
bd6f78b2 JA |
271 | dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len); |
272 | ||
87677832 | 273 | c = crc16(p, hdr->len - hdr_size(hdr)); |
969f7ed3 | 274 | |
546dfd9f | 275 | if (c != vh->crc16) { |
3f9f4e26 | 276 | log_err("crc16: verify failed at %llu/%u\n", |
5ec10eaa | 277 | io_u->offset + header_num * hdr->len, hdr->len); |
546dfd9f | 278 | log_err("crc16: wanted %x, got %x\n", vh->crc16, c); |
d16d4e09 | 279 | return EIO; |
969f7ed3 JA |
280 | } |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
3f9f4e26 | 285 | static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u, |
5ec10eaa | 286 | unsigned int header_num) |
d77a7af3 | 287 | { |
d9f2caf3 | 288 | void *p = io_u_verify_off(hdr, io_u, header_num); |
546dfd9f | 289 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
d77a7af3 JA |
290 | unsigned long long c; |
291 | ||
bd6f78b2 JA |
292 | dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len); |
293 | ||
87677832 | 294 | c = crc64(p, hdr->len - hdr_size(hdr)); |
d77a7af3 | 295 | |
546dfd9f | 296 | if (c != vh->crc64) { |
3f9f4e26 SL |
297 | log_err("crc64: verify failed at %llu/%u\n", |
298 | io_u->offset + header_num * hdr->len, | |
299 | hdr->len); | |
5ec10eaa JA |
300 | log_err("crc64: wanted %llx, got %llx\n", |
301 | (unsigned long long) vh->crc64, c); | |
d16d4e09 | 302 | return EIO; |
d77a7af3 JA |
303 | } |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
3f9f4e26 | 308 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u, |
5ec10eaa | 309 | unsigned int header_num) |
e29d1b70 | 310 | { |
d9f2caf3 | 311 | void *p = io_u_verify_off(hdr, io_u, header_num); |
546dfd9f JA |
312 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
313 | uint32_t c; | |
e29d1b70 | 314 | |
bd6f78b2 JA |
315 | dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len); |
316 | ||
87677832 | 317 | c = crc32(p, hdr->len - hdr_size(hdr)); |
e29d1b70 | 318 | |
546dfd9f | 319 | if (c != vh->crc32) { |
3f9f4e26 | 320 | log_err("crc32: verify failed at %llu/%u\n", |
5ec10eaa | 321 | io_u->offset + header_num * hdr->len, hdr->len); |
546dfd9f | 322 | log_err("crc32: wanted %x, got %x\n", vh->crc32, c); |
d16d4e09 | 323 | return EIO; |
e29d1b70 JA |
324 | } |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
3f9f4e26 | 329 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u, |
5ec10eaa | 330 | unsigned int header_num) |
e29d1b70 | 331 | { |
d9f2caf3 | 332 | void *p = io_u_verify_off(hdr, io_u, header_num); |
546dfd9f | 333 | struct vhdr_md5 *vh = hdr_priv(hdr); |
8c432325 JA |
334 | uint32_t hash[MD5_HASH_WORDS]; |
335 | struct md5_ctx md5_ctx = { | |
336 | .hash = hash, | |
337 | }; | |
e29d1b70 | 338 | |
bd6f78b2 JA |
339 | dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len); |
340 | ||
61f821f1 | 341 | md5_init(&md5_ctx); |
87677832 | 342 | md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr)); |
e29d1b70 | 343 | |
546dfd9f | 344 | if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) { |
3f9f4e26 | 345 | log_err("md5: verify failed at %llu/%u\n", |
5ec10eaa | 346 | io_u->offset + header_num * hdr->len, hdr->len); |
546dfd9f | 347 | hexdump(vh->md5_digest, sizeof(vh->md5_digest)); |
8c432325 | 348 | hexdump(md5_ctx.hash, sizeof(hash)); |
d16d4e09 | 349 | return EIO; |
e29d1b70 JA |
350 | } |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
3f199b01 JA |
355 | static unsigned int hweight8(unsigned int w) |
356 | { | |
357 | unsigned int res = w - ((w >> 1) & 0x55); | |
358 | ||
359 | res = (res & 0x33) + ((res >> 2) & 0x33); | |
360 | return (res + (res >> 4)) & 0x0F; | |
361 | } | |
362 | ||
a944e335 | 363 | int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size, |
5ec10eaa | 364 | char *buf, unsigned int len, unsigned int mod) |
a944e335 SL |
365 | { |
366 | unsigned int i; | |
367 | char split_pattern[4]; | |
368 | ||
369 | for (i = 0; i < 4; i++) { | |
370 | split_pattern[i] = pattern & 0xff; | |
371 | pattern >>= 8; | |
372 | } | |
373 | ||
374 | for (i = 0; i < len; i++) { | |
3f199b01 JA |
375 | if (buf[i] != split_pattern[mod]) { |
376 | unsigned int bits; | |
377 | ||
378 | bits = hweight8(buf[i] ^ split_pattern[mod]); | |
379 | log_err("fio: got pattern %x, wanted %x. Bad bits %d\n", | |
380 | buf[i], split_pattern[mod], bits); | |
381 | log_err("fio: bad pattern block offset %u\n", i); | |
d16d4e09 | 382 | return EIO; |
3f199b01 | 383 | } |
a944e335 SL |
384 | mod++; |
385 | if (mod == pattern_size) | |
386 | mod = 0; | |
387 | } | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
36690c9b | 392 | int verify_io_u(struct thread_data *td, struct io_u *io_u) |
e29d1b70 | 393 | { |
3f9f4e26 | 394 | struct verify_header *hdr; |
a944e335 | 395 | unsigned int hdr_size, hdr_inc, hdr_num = 0; |
95646108 | 396 | void *p; |
e29d1b70 JA |
397 | int ret; |
398 | ||
1dcc0498 | 399 | if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ) |
36690c9b JA |
400 | return 0; |
401 | ||
3f9f4e26 | 402 | hdr_inc = io_u->buflen; |
a59e170d JA |
403 | if (td->o.verify_interval) |
404 | hdr_inc = td->o.verify_interval; | |
e29d1b70 | 405 | |
a12a3b4d | 406 | ret = 0; |
5ec10eaa JA |
407 | for (p = io_u->buf; p < io_u->buf + io_u->buflen; |
408 | p += hdr_inc, hdr_num++) { | |
a12a3b4d JA |
409 | if (ret && td->o.verify_fatal) { |
410 | td->terminate = 1; | |
411 | break; | |
412 | } | |
a944e335 | 413 | hdr_size = __hdr_size(td->o.verify); |
a59e170d | 414 | if (td->o.verify_offset) |
a944e335 | 415 | memswp(p, p + td->o.verify_offset, hdr_size); |
95646108 | 416 | hdr = p; |
e29d1b70 | 417 | |
3f199b01 JA |
418 | if (hdr->fio_magic != FIO_HDR_MAGIC) { |
419 | log_err("Bad verify header %x\n", hdr->fio_magic); | |
420 | return EIO; | |
421 | } | |
422 | ||
e28218f3 | 423 | if (td->o.verify_pattern_bytes) { |
bd6f78b2 JA |
424 | dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n", |
425 | io_u, hdr->len); | |
e28218f3 | 426 | ret = verify_io_u_pattern(td->o.verify_pattern, |
5ec10eaa JA |
427 | td->o.verify_pattern_bytes, |
428 | p + hdr_size, | |
429 | hdr_inc - hdr_size, | |
430 | hdr_size % 4); | |
e28218f3 SL |
431 | if (ret) |
432 | log_err("fio: verify failed at %llu/%u\n", | |
433 | io_u->offset + hdr_num * hdr->len, | |
434 | hdr->len); | |
435 | continue; | |
436 | } | |
437 | ||
3f9f4e26 SL |
438 | switch (hdr->verify_type) { |
439 | case VERIFY_MD5: | |
440 | ret = verify_io_u_md5(hdr, io_u, hdr_num); | |
441 | break; | |
442 | case VERIFY_CRC64: | |
443 | ret = verify_io_u_crc64(hdr, io_u, hdr_num); | |
444 | break; | |
445 | case VERIFY_CRC32: | |
446 | ret = verify_io_u_crc32(hdr, io_u, hdr_num); | |
447 | break; | |
448 | case VERIFY_CRC16: | |
449 | ret = verify_io_u_crc16(hdr, io_u, hdr_num); | |
450 | break; | |
451 | case VERIFY_CRC7: | |
452 | ret = verify_io_u_crc7(hdr, io_u, hdr_num); | |
453 | break; | |
cd14cc10 JA |
454 | case VERIFY_SHA256: |
455 | ret = verify_io_u_sha256(hdr, io_u, hdr_num); | |
456 | break; | |
457 | case VERIFY_SHA512: | |
458 | ret = verify_io_u_sha512(hdr, io_u, hdr_num); | |
459 | break; | |
7437ee87 SL |
460 | case VERIFY_META: |
461 | ret = verify_io_u_meta(hdr, td, io_u, hdr_num); | |
462 | break; | |
3f9f4e26 SL |
463 | default: |
464 | log_err("Bad verify type %u\n", hdr->verify_type); | |
d16d4e09 | 465 | ret = EINVAL; |
3f9f4e26 | 466 | } |
3f9f4e26 | 467 | } |
a7dfe862 | 468 | |
a12a3b4d | 469 | return ret; |
e29d1b70 JA |
470 | } |
471 | ||
7437ee87 | 472 | static void fill_meta(struct verify_header *hdr, struct thread_data *td, |
5ec10eaa | 473 | struct io_u *io_u, unsigned int header_num) |
7437ee87 SL |
474 | { |
475 | struct vhdr_meta *vh = hdr_priv(hdr); | |
476 | ||
477 | vh->thread = td->thread_number; | |
478 | ||
479 | vh->time_sec = io_u->start_time.tv_sec; | |
480 | vh->time_usec = io_u->start_time.tv_usec; | |
481 | ||
482 | vh->numberio = td->io_issues[DDIR_WRITE]; | |
483 | ||
484 | vh->offset = io_u->offset + header_num * td->o.verify_interval; | |
485 | } | |
486 | ||
cd14cc10 JA |
487 | static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len) |
488 | { | |
546dfd9f | 489 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
cd14cc10 | 490 | struct sha512_ctx sha512_ctx = { |
546dfd9f | 491 | .buf = vh->sha512, |
cd14cc10 JA |
492 | }; |
493 | ||
494 | sha512_init(&sha512_ctx); | |
495 | sha512_update(&sha512_ctx, p, len); | |
496 | } | |
497 | ||
498 | static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len) | |
499 | { | |
546dfd9f | 500 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
cd14cc10 | 501 | struct sha256_ctx sha256_ctx = { |
546dfd9f | 502 | .buf = vh->sha256, |
cd14cc10 JA |
503 | }; |
504 | ||
505 | sha256_init(&sha256_ctx); | |
506 | sha256_update(&sha256_ctx, p, len); | |
507 | } | |
508 | ||
1e154bdb JA |
509 | static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len) |
510 | { | |
546dfd9f JA |
511 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
512 | ||
513 | vh->crc7 = crc7(p, len); | |
1e154bdb JA |
514 | } |
515 | ||
969f7ed3 JA |
516 | static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len) |
517 | { | |
546dfd9f JA |
518 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
519 | ||
520 | vh->crc16 = crc16(p, len); | |
969f7ed3 JA |
521 | } |
522 | ||
e29d1b70 JA |
523 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
524 | { | |
546dfd9f JA |
525 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
526 | ||
527 | vh->crc32 = crc32(p, len); | |
e29d1b70 JA |
528 | } |
529 | ||
d77a7af3 JA |
530 | static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len) |
531 | { | |
546dfd9f JA |
532 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
533 | ||
534 | vh->crc64 = crc64(p, len); | |
d77a7af3 JA |
535 | } |
536 | ||
e29d1b70 JA |
537 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
538 | { | |
546dfd9f | 539 | struct vhdr_md5 *vh = hdr_priv(hdr); |
8c432325 | 540 | struct md5_ctx md5_ctx = { |
546dfd9f | 541 | .hash = (uint32_t *) vh->md5_digest, |
8c432325 | 542 | }; |
e29d1b70 | 543 | |
61f821f1 | 544 | md5_init(&md5_ctx); |
e29d1b70 | 545 | md5_update(&md5_ctx, p, len); |
e29d1b70 JA |
546 | } |
547 | ||
548 | /* | |
549 | * fill body of io_u->buf with random data and add a header with the | |
550 | * crc32 or md5 sum of that data. | |
551 | */ | |
552 | void populate_verify_io_u(struct thread_data *td, struct io_u *io_u) | |
553 | { | |
baefa9be | 554 | struct verify_header *hdr; |
95646108 | 555 | void *p = io_u->buf, *data; |
7437ee87 | 556 | unsigned int hdr_inc, data_len, header_num = 0; |
e29d1b70 | 557 | |
9cc3d150 JA |
558 | if (td->o.verify == VERIFY_NULL) |
559 | return; | |
560 | ||
90059d65 | 561 | fill_pattern(td, p, io_u->buflen); |
546a9142 SL |
562 | |
563 | hdr_inc = io_u->buflen; | |
a59e170d JA |
564 | if (td->o.verify_interval) |
565 | hdr_inc = td->o.verify_interval; | |
baefa9be | 566 | |
5ec10eaa | 567 | for (; p < io_u->buf + io_u->buflen; p += hdr_inc) { |
95646108 | 568 | hdr = p; |
3f9f4e26 SL |
569 | |
570 | hdr->fio_magic = FIO_HDR_MAGIC; | |
571 | hdr->verify_type = td->o.verify; | |
546a9142 | 572 | hdr->len = hdr_inc; |
87677832 | 573 | data_len = hdr_inc - hdr_size(hdr); |
3f9f4e26 | 574 | |
87677832 | 575 | data = p + hdr_size(hdr); |
3f9f4e26 SL |
576 | switch (td->o.verify) { |
577 | case VERIFY_MD5: | |
bd6f78b2 JA |
578 | dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n", |
579 | io_u, hdr->len); | |
3f9f4e26 SL |
580 | fill_md5(hdr, data, data_len); |
581 | break; | |
582 | case VERIFY_CRC64: | |
bd6f78b2 JA |
583 | dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n", |
584 | io_u, hdr->len); | |
3f9f4e26 SL |
585 | fill_crc64(hdr, data, data_len); |
586 | break; | |
587 | case VERIFY_CRC32: | |
bd6f78b2 JA |
588 | dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n", |
589 | io_u, hdr->len); | |
3f9f4e26 SL |
590 | fill_crc32(hdr, data, data_len); |
591 | break; | |
592 | case VERIFY_CRC16: | |
bd6f78b2 JA |
593 | dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n", |
594 | io_u, hdr->len); | |
3f9f4e26 SL |
595 | fill_crc16(hdr, data, data_len); |
596 | break; | |
597 | case VERIFY_CRC7: | |
bd6f78b2 JA |
598 | dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n", |
599 | io_u, hdr->len); | |
3f9f4e26 SL |
600 | fill_crc7(hdr, data, data_len); |
601 | break; | |
cd14cc10 | 602 | case VERIFY_SHA256: |
bd6f78b2 JA |
603 | dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n", |
604 | io_u, hdr->len); | |
cd14cc10 JA |
605 | fill_sha256(hdr, data, data_len); |
606 | break; | |
607 | case VERIFY_SHA512: | |
bd6f78b2 JA |
608 | dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n", |
609 | io_u, hdr->len); | |
cd14cc10 JA |
610 | fill_sha512(hdr, data, data_len); |
611 | break; | |
7437ee87 | 612 | case VERIFY_META: |
bd6f78b2 JA |
613 | dprint(FD_VERIFY, "fill meta io_u %p, len %u\n", |
614 | io_u, hdr->len); | |
7437ee87 SL |
615 | fill_meta(hdr, td, io_u, header_num); |
616 | break; | |
3f9f4e26 SL |
617 | default: |
618 | log_err("fio: bad verify type: %d\n", td->o.verify); | |
619 | assert(0); | |
620 | } | |
a59e170d | 621 | if (td->o.verify_offset) |
87677832 | 622 | memswp(p, p + td->o.verify_offset, hdr_size(hdr)); |
7437ee87 | 623 | header_num++; |
e29d1b70 | 624 | } |
e29d1b70 JA |
625 | } |
626 | ||
627 | int get_next_verify(struct thread_data *td, struct io_u *io_u) | |
628 | { | |
8de8f047 | 629 | struct io_piece *ipo = NULL; |
e29d1b70 | 630 | |
d2d7fa53 JA |
631 | /* |
632 | * this io_u is from a requeue, we already filled the offsets | |
633 | */ | |
634 | if (io_u->file) | |
635 | return 0; | |
636 | ||
8de8f047 JA |
637 | if (!RB_EMPTY_ROOT(&td->io_hist_tree)) { |
638 | struct rb_node *n = rb_first(&td->io_hist_tree); | |
e29d1b70 | 639 | |
8de8f047 | 640 | ipo = rb_entry(n, struct io_piece, rb_node); |
4b87898e | 641 | rb_erase(n, &td->io_hist_tree); |
01743ee1 JA |
642 | } else if (!flist_empty(&td->io_hist_list)) { |
643 | ipo = flist_entry(td->io_hist_list.next, struct io_piece, list); | |
644 | flist_del(&ipo->list); | |
8de8f047 | 645 | } |
e29d1b70 | 646 | |
8de8f047 | 647 | if (ipo) { |
e29d1b70 JA |
648 | io_u->offset = ipo->offset; |
649 | io_u->buflen = ipo->len; | |
36167d82 | 650 | io_u->file = ipo->file; |
97af62ce JA |
651 | |
652 | if ((io_u->file->flags & FIO_FILE_OPEN) == 0) { | |
653 | int r = td_io_open_file(td, io_u->file); | |
654 | ||
bd6f78b2 JA |
655 | if (r) { |
656 | dprint(FD_VERIFY, "failed file %s open\n", | |
657 | io_u->file->file_name); | |
97af62ce | 658 | return 1; |
bd6f78b2 | 659 | } |
97af62ce JA |
660 | } |
661 | ||
662 | get_file(ipo->file); | |
663 | assert(io_u->file->flags & FIO_FILE_OPEN); | |
e29d1b70 | 664 | io_u->ddir = DDIR_READ; |
36167d82 JA |
665 | io_u->xfer_buf = io_u->buf; |
666 | io_u->xfer_buflen = io_u->buflen; | |
e29d1b70 | 667 | free(ipo); |
bd6f78b2 | 668 | dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u); |
e29d1b70 JA |
669 | return 0; |
670 | } | |
671 | ||
bd6f78b2 | 672 | dprint(FD_VERIFY, "get_next_verify: empty\n"); |
e29d1b70 JA |
673 | return 1; |
674 | } |