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> |
e8462bd8 | 8 | #include <pthread.h> |
e29d1b70 JA |
9 | |
10 | #include "fio.h" | |
4f5af7b2 | 11 | #include "verify.h" |
e8462bd8 | 12 | #include "smalloc.h" |
0d29de83 | 13 | #include "trim.h" |
637ef8d9 | 14 | #include "lib/rand.h" |
e29d1b70 | 15 | |
eef6eea1 JA |
16 | #include "crc/md5.h" |
17 | #include "crc/crc64.h" | |
18 | #include "crc/crc32.h" | |
bac39e0e | 19 | #include "crc/crc32c.h" |
eef6eea1 JA |
20 | #include "crc/crc16.h" |
21 | #include "crc/crc7.h" | |
22 | #include "crc/sha256.h" | |
23 | #include "crc/sha512.h" | |
7c353ceb | 24 | #include "crc/sha1.h" |
cd14cc10 | 25 | |
cbe8d756 | 26 | void fill_pattern(struct thread_data *td, void *p, unsigned int len, struct io_u *io_u) |
90059d65 JA |
27 | { |
28 | switch (td->o.verify_pattern_bytes) { | |
29 | case 0: | |
bd6f78b2 | 30 | dprint(FD_VERIFY, "fill random bytes len=%u\n", len); |
637ef8d9 | 31 | fill_random_buf(p, len); |
90059d65 JA |
32 | break; |
33 | case 1: | |
10e8a7b3 | 34 | if (io_u->buf_filled_len >= len) { |
cbe8d756 RR |
35 | dprint(FD_VERIFY, "using already filled verify pattern b=0 len=%u\n", len); |
36 | return; | |
37 | } | |
bd6f78b2 | 38 | dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len); |
0e92f873 | 39 | memset(p, td->o.verify_pattern[0], len); |
cbe8d756 | 40 | io_u->buf_filled_len = len; |
90059d65 | 41 | break; |
0e92f873 RR |
42 | default: { |
43 | unsigned int i = 0, size = 0; | |
90059d65 JA |
44 | unsigned char *b = p; |
45 | ||
10e8a7b3 | 46 | if (io_u->buf_filled_len >= len) { |
cbe8d756 RR |
47 | dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n", |
48 | td->o.verify_pattern_bytes, len); | |
49 | return; | |
50 | } | |
bd6f78b2 JA |
51 | dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n", |
52 | td->o.verify_pattern_bytes, len); | |
53 | ||
90059d65 | 54 | while (i < len) { |
0e92f873 RR |
55 | size = td->o.verify_pattern_bytes; |
56 | if (size > (len - i)) | |
57 | size = len - i; | |
58 | memcpy(b+i, td->o.verify_pattern, size); | |
59 | i += size; | |
90059d65 | 60 | } |
cbe8d756 | 61 | io_u->buf_filled_len = len; |
90059d65 JA |
62 | break; |
63 | } | |
64 | } | |
65 | } | |
66 | ||
4764aec9 | 67 | static void memswp(void *buf1, void *buf2, unsigned int len) |
546a9142 | 68 | { |
dee6de74 SL |
69 | char swap[200]; |
70 | ||
71 | assert(len <= sizeof(swap)); | |
90059d65 | 72 | |
546a9142 SL |
73 | memcpy(&swap, buf1, len); |
74 | memcpy(buf1, buf2, len); | |
75 | memcpy(buf2, &swap, len); | |
76 | } | |
77 | ||
e29d1b70 JA |
78 | static void hexdump(void *buffer, int len) |
79 | { | |
80 | unsigned char *p = buffer; | |
81 | int i; | |
82 | ||
83 | for (i = 0; i < len; i++) | |
bfacf389 JA |
84 | log_err("%02x", p[i]); |
85 | log_err("\n"); | |
e29d1b70 JA |
86 | } |
87 | ||
87677832 JA |
88 | /* |
89 | * Prepare for seperation of verify_header and checksum header | |
90 | */ | |
546dfd9f | 91 | static inline unsigned int __hdr_size(int verify_type) |
87677832 | 92 | { |
5921e80c | 93 | unsigned int len = len; |
87677832 | 94 | |
546dfd9f JA |
95 | switch (verify_type) { |
96 | case VERIFY_NONE: | |
97 | case VERIFY_NULL: | |
98 | len = 0; | |
99 | break; | |
100 | case VERIFY_MD5: | |
101 | len = sizeof(struct vhdr_md5); | |
102 | break; | |
103 | case VERIFY_CRC64: | |
104 | len = sizeof(struct vhdr_crc64); | |
105 | break; | |
bac39e0e | 106 | case VERIFY_CRC32C: |
546dfd9f | 107 | case VERIFY_CRC32: |
3845591f | 108 | case VERIFY_CRC32C_INTEL: |
546dfd9f JA |
109 | len = sizeof(struct vhdr_crc32); |
110 | break; | |
111 | case VERIFY_CRC16: | |
112 | len = sizeof(struct vhdr_crc16); | |
113 | break; | |
114 | case VERIFY_CRC7: | |
115 | len = sizeof(struct vhdr_crc7); | |
116 | break; | |
117 | case VERIFY_SHA256: | |
118 | len = sizeof(struct vhdr_sha256); | |
119 | break; | |
120 | case VERIFY_SHA512: | |
121 | len = sizeof(struct vhdr_sha512); | |
122 | break; | |
7437ee87 SL |
123 | case VERIFY_META: |
124 | len = sizeof(struct vhdr_meta); | |
125 | break; | |
7c353ceb JA |
126 | case VERIFY_SHA1: |
127 | len = sizeof(struct vhdr_sha1); | |
128 | break; | |
546dfd9f JA |
129 | default: |
130 | log_err("fio: unknown verify header!\n"); | |
131 | assert(0); | |
132 | } | |
133 | ||
134 | return len + sizeof(struct verify_header); | |
87677832 JA |
135 | } |
136 | ||
137 | static inline unsigned int hdr_size(struct verify_header *hdr) | |
138 | { | |
546dfd9f JA |
139 | return __hdr_size(hdr->verify_type); |
140 | } | |
141 | ||
142 | static void *hdr_priv(struct verify_header *hdr) | |
143 | { | |
144 | void *priv = hdr; | |
145 | ||
146 | return priv + sizeof(struct verify_header); | |
87677832 JA |
147 | } |
148 | ||
936216f8 JA |
149 | /* |
150 | * Verify container, pass info to verify handlers and allow them to | |
151 | * pass info back in case of error | |
152 | */ | |
153 | struct vcont { | |
154 | /* | |
155 | * Input | |
156 | */ | |
157 | struct io_u *io_u; | |
158 | unsigned int hdr_num; | |
159 | ||
160 | /* | |
161 | * Output, only valid in case of error | |
162 | */ | |
bfacf389 JA |
163 | const char *name; |
164 | void *good_crc; | |
165 | void *bad_crc; | |
936216f8 JA |
166 | unsigned int crc_len; |
167 | }; | |
168 | ||
bfacf389 JA |
169 | static void log_verify_failure(struct verify_header *hdr, struct vcont *vc) |
170 | { | |
171 | unsigned long long offset; | |
172 | ||
173 | offset = vc->io_u->offset; | |
174 | offset += vc->hdr_num * hdr->len; | |
32c17adf JA |
175 | log_err("%.8s: verify failed at file %s offset %llu, length %u\n", |
176 | vc->name, vc->io_u->file->file_name, offset, hdr->len); | |
bfacf389 JA |
177 | |
178 | if (vc->good_crc && vc->bad_crc) { | |
179 | log_err(" Expected CRC: "); | |
180 | hexdump(vc->good_crc, vc->crc_len); | |
181 | log_err(" Received CRC: "); | |
182 | hexdump(vc->bad_crc, vc->crc_len); | |
183 | } | |
184 | } | |
185 | ||
d9f2caf3 JA |
186 | /* |
187 | * Return data area 'header_num' | |
188 | */ | |
936216f8 | 189 | static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc) |
d9f2caf3 | 190 | { |
936216f8 | 191 | return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr); |
d9f2caf3 JA |
192 | } |
193 | ||
7437ee87 | 194 | static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td, |
936216f8 | 195 | struct vcont *vc) |
7437ee87 SL |
196 | { |
197 | struct vhdr_meta *vh = hdr_priv(hdr); | |
936216f8 | 198 | struct io_u *io_u = vc->io_u; |
7437ee87 | 199 | |
bd6f78b2 JA |
200 | dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len); |
201 | ||
bfacf389 JA |
202 | if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval) |
203 | return 0; | |
7437ee87 | 204 | |
bfacf389 JA |
205 | vc->name = "meta"; |
206 | log_verify_failure(hdr, vc); | |
207 | return EILSEQ; | |
7437ee87 SL |
208 | } |
209 | ||
936216f8 | 210 | static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc) |
cd14cc10 | 211 | { |
936216f8 | 212 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 213 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
cd14cc10 JA |
214 | uint8_t sha512[128]; |
215 | struct sha512_ctx sha512_ctx = { | |
216 | .buf = sha512, | |
217 | }; | |
218 | ||
936216f8 | 219 | dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 220 | |
cd14cc10 | 221 | sha512_init(&sha512_ctx); |
87677832 | 222 | sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr)); |
cd14cc10 | 223 | |
bfacf389 JA |
224 | if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) |
225 | return 0; | |
cd14cc10 | 226 | |
bfacf389 JA |
227 | vc->name = "sha512"; |
228 | vc->good_crc = vh->sha512; | |
229 | vc->bad_crc = sha512_ctx.buf; | |
230 | vc->crc_len = sizeof(vh->sha512); | |
231 | log_verify_failure(hdr, vc); | |
232 | return EILSEQ; | |
cd14cc10 JA |
233 | } |
234 | ||
936216f8 | 235 | static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc) |
cd14cc10 | 236 | { |
936216f8 | 237 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 238 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
bc77f56f | 239 | uint8_t sha256[64]; |
cd14cc10 JA |
240 | struct sha256_ctx sha256_ctx = { |
241 | .buf = sha256, | |
242 | }; | |
243 | ||
936216f8 | 244 | dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 245 | |
cd14cc10 | 246 | sha256_init(&sha256_ctx); |
87677832 | 247 | sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr)); |
cd14cc10 | 248 | |
bfacf389 JA |
249 | if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) |
250 | return 0; | |
cd14cc10 | 251 | |
bfacf389 JA |
252 | vc->name = "sha256"; |
253 | vc->good_crc = vh->sha256; | |
254 | vc->bad_crc = sha256_ctx.buf; | |
255 | vc->crc_len = sizeof(vh->sha256); | |
256 | log_verify_failure(hdr, vc); | |
257 | return EILSEQ; | |
cd14cc10 JA |
258 | } |
259 | ||
936216f8 | 260 | static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc) |
7c353ceb | 261 | { |
936216f8 | 262 | void *p = io_u_verify_off(hdr, vc); |
7c353ceb JA |
263 | struct vhdr_sha1 *vh = hdr_priv(hdr); |
264 | uint32_t sha1[5]; | |
265 | struct sha1_ctx sha1_ctx = { | |
266 | .H = sha1, | |
267 | }; | |
268 | ||
936216f8 | 269 | dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
7c353ceb JA |
270 | |
271 | sha1_init(&sha1_ctx); | |
272 | sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr)); | |
273 | ||
bfacf389 JA |
274 | if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) |
275 | return 0; | |
7c353ceb | 276 | |
bfacf389 JA |
277 | vc->name = "sha1"; |
278 | vc->good_crc = vh->sha1; | |
279 | vc->bad_crc = sha1_ctx.H; | |
280 | vc->crc_len = sizeof(vh->sha1); | |
281 | log_verify_failure(hdr, vc); | |
282 | return EILSEQ; | |
7c353ceb JA |
283 | } |
284 | ||
936216f8 | 285 | static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc) |
1e154bdb | 286 | { |
936216f8 | 287 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 288 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
1e154bdb JA |
289 | unsigned char c; |
290 | ||
936216f8 | 291 | dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 292 | |
87677832 | 293 | c = crc7(p, hdr->len - hdr_size(hdr)); |
1e154bdb | 294 | |
bfacf389 JA |
295 | if (c == vh->crc7) |
296 | return 0; | |
1e154bdb | 297 | |
bfacf389 JA |
298 | vc->name = "crc7"; |
299 | vc->good_crc = &vh->crc7; | |
300 | vc->bad_crc = &c; | |
301 | vc->crc_len = 1; | |
302 | log_verify_failure(hdr, vc); | |
303 | return EILSEQ; | |
1e154bdb JA |
304 | } |
305 | ||
936216f8 | 306 | static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc) |
969f7ed3 | 307 | { |
936216f8 | 308 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 309 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
969f7ed3 JA |
310 | unsigned short c; |
311 | ||
936216f8 | 312 | dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 313 | |
87677832 | 314 | c = crc16(p, hdr->len - hdr_size(hdr)); |
969f7ed3 | 315 | |
bfacf389 JA |
316 | if (c == vh->crc16) |
317 | return 0; | |
969f7ed3 | 318 | |
bfacf389 JA |
319 | vc->name = "crc16"; |
320 | vc->good_crc = &vh->crc16; | |
321 | vc->bad_crc = &c; | |
322 | vc->crc_len = 2; | |
323 | log_verify_failure(hdr, vc); | |
324 | return EILSEQ; | |
969f7ed3 JA |
325 | } |
326 | ||
936216f8 | 327 | static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc) |
d77a7af3 | 328 | { |
936216f8 | 329 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 330 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
d77a7af3 JA |
331 | unsigned long long c; |
332 | ||
936216f8 | 333 | dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 334 | |
87677832 | 335 | c = crc64(p, hdr->len - hdr_size(hdr)); |
d77a7af3 | 336 | |
bfacf389 JA |
337 | if (c == vh->crc64) |
338 | return 0; | |
d77a7af3 | 339 | |
bfacf389 JA |
340 | vc->name = "crc64"; |
341 | vc->good_crc = &vh->crc64; | |
342 | vc->bad_crc = &c; | |
343 | vc->crc_len = 8; | |
344 | log_verify_failure(hdr, vc); | |
345 | return EILSEQ; | |
d77a7af3 JA |
346 | } |
347 | ||
936216f8 | 348 | static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc) |
e29d1b70 | 349 | { |
936216f8 | 350 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f JA |
351 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
352 | uint32_t c; | |
e29d1b70 | 353 | |
936216f8 | 354 | dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 355 | |
87677832 | 356 | c = crc32(p, hdr->len - hdr_size(hdr)); |
e29d1b70 | 357 | |
bfacf389 JA |
358 | if (c == vh->crc32) |
359 | return 0; | |
e29d1b70 | 360 | |
bfacf389 JA |
361 | vc->name = "crc32"; |
362 | vc->good_crc = &vh->crc32; | |
363 | vc->bad_crc = &c; | |
364 | vc->crc_len = 4; | |
365 | log_verify_failure(hdr, vc); | |
366 | return EILSEQ; | |
e29d1b70 JA |
367 | } |
368 | ||
936216f8 | 369 | static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc) |
bac39e0e | 370 | { |
936216f8 | 371 | void *p = io_u_verify_off(hdr, vc); |
bac39e0e JA |
372 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
373 | uint32_t c; | |
374 | ||
936216f8 | 375 | dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bac39e0e | 376 | |
3845591f JA |
377 | if (hdr->verify_type == VERIFY_CRC32C_INTEL) |
378 | c = crc32c_intel(p, hdr->len - hdr_size(hdr)); | |
379 | else | |
380 | c = crc32c(p, hdr->len - hdr_size(hdr)); | |
bac39e0e | 381 | |
bfacf389 JA |
382 | if (c == vh->crc32) |
383 | return 0; | |
bac39e0e | 384 | |
bfacf389 JA |
385 | vc->name = "crc32c"; |
386 | vc->good_crc = &vh->crc32; | |
387 | vc->bad_crc = &c; | |
388 | vc->crc_len = 4; | |
389 | log_verify_failure(hdr, vc); | |
390 | return EILSEQ; | |
bac39e0e JA |
391 | } |
392 | ||
936216f8 | 393 | static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc) |
e29d1b70 | 394 | { |
936216f8 | 395 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 396 | struct vhdr_md5 *vh = hdr_priv(hdr); |
8c432325 JA |
397 | uint32_t hash[MD5_HASH_WORDS]; |
398 | struct md5_ctx md5_ctx = { | |
399 | .hash = hash, | |
400 | }; | |
e29d1b70 | 401 | |
936216f8 | 402 | dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 403 | |
61f821f1 | 404 | md5_init(&md5_ctx); |
87677832 | 405 | md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr)); |
e29d1b70 | 406 | |
bfacf389 JA |
407 | if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) |
408 | return 0; | |
e29d1b70 | 409 | |
bfacf389 JA |
410 | vc->name = "md5"; |
411 | vc->good_crc = vh->md5_digest; | |
412 | vc->bad_crc = md5_ctx.hash; | |
413 | vc->crc_len = sizeof(hash); | |
414 | log_verify_failure(hdr, vc); | |
415 | return EILSEQ; | |
e29d1b70 JA |
416 | } |
417 | ||
3f199b01 JA |
418 | static unsigned int hweight8(unsigned int w) |
419 | { | |
420 | unsigned int res = w - ((w >> 1) & 0x55); | |
421 | ||
422 | res = (res & 0x33) + ((res >> 2) & 0x33); | |
423 | return (res + (res >> 4)) & 0x0F; | |
424 | } | |
425 | ||
0e92f873 | 426 | int verify_io_u_pattern(char *pattern, unsigned long pattern_size, |
5ec10eaa | 427 | char *buf, unsigned int len, unsigned int mod) |
a944e335 SL |
428 | { |
429 | unsigned int i; | |
a944e335 SL |
430 | |
431 | for (i = 0; i < len; i++) { | |
0e92f873 | 432 | if (buf[i] != pattern[mod]) { |
3f199b01 JA |
433 | unsigned int bits; |
434 | ||
0e92f873 | 435 | bits = hweight8(buf[i] ^ pattern[mod]); |
3f199b01 | 436 | log_err("fio: got pattern %x, wanted %x. Bad bits %d\n", |
0e92f873 | 437 | buf[i], pattern[mod], bits); |
3f199b01 | 438 | log_err("fio: bad pattern block offset %u\n", i); |
9fd18969 | 439 | return EILSEQ; |
3f199b01 | 440 | } |
a944e335 SL |
441 | mod++; |
442 | if (mod == pattern_size) | |
443 | mod = 0; | |
444 | } | |
445 | ||
446 | return 0; | |
447 | } | |
448 | ||
e8462bd8 JA |
449 | /* |
450 | * Push IO verification to a separate thread | |
451 | */ | |
452 | int verify_io_u_async(struct thread_data *td, struct io_u *io_u) | |
453 | { | |
454 | if (io_u->file) | |
455 | put_file_log(td, io_u->file); | |
456 | ||
457 | io_u->file = NULL; | |
458 | ||
459 | pthread_mutex_lock(&td->io_u_lock); | |
0c41214f RR |
460 | |
461 | if (io_u->flags & IO_U_F_IN_CUR_DEPTH) { | |
462 | td->cur_depth--; | |
463 | io_u->flags &= ~IO_U_F_IN_CUR_DEPTH; | |
464 | } | |
e8462bd8 JA |
465 | flist_del(&io_u->list); |
466 | flist_add_tail(&io_u->list, &td->verify_list); | |
2ecc1b57 | 467 | io_u->flags |= IO_U_F_FREE_DEF; |
e8462bd8 JA |
468 | pthread_mutex_unlock(&td->io_u_lock); |
469 | ||
470 | pthread_cond_signal(&td->verify_cond); | |
e8462bd8 JA |
471 | return 0; |
472 | } | |
473 | ||
0d29de83 JA |
474 | static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u) |
475 | { | |
476 | static char zero_buf[1024]; | |
477 | unsigned int this_len, len; | |
478 | int ret = 0; | |
479 | void *p; | |
480 | ||
481 | if (!td->o.trim_zero) | |
482 | return 0; | |
483 | ||
484 | len = io_u->buflen; | |
485 | p = io_u->buf; | |
486 | do { | |
487 | this_len = sizeof(zero_buf); | |
488 | if (this_len > len) | |
489 | this_len = len; | |
490 | if (memcmp(p, zero_buf, this_len)) { | |
491 | ret = EILSEQ; | |
492 | break; | |
493 | } | |
494 | len -= this_len; | |
495 | p += this_len; | |
496 | } while (len); | |
497 | ||
498 | if (!ret) | |
499 | return 0; | |
500 | ||
501 | log_err("trims: verify failed at file %s offset %llu, length %lu\n", | |
502 | io_u->file->file_name, io_u->offset, io_u->buflen); | |
503 | return ret; | |
504 | } | |
505 | ||
36690c9b | 506 | int verify_io_u(struct thread_data *td, struct io_u *io_u) |
e29d1b70 | 507 | { |
3f9f4e26 | 508 | struct verify_header *hdr; |
a944e335 | 509 | unsigned int hdr_size, hdr_inc, hdr_num = 0; |
95646108 | 510 | void *p; |
e29d1b70 JA |
511 | int ret; |
512 | ||
1dcc0498 | 513 | if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ) |
36690c9b | 514 | return 0; |
0d29de83 JA |
515 | if (io_u->flags & IO_U_F_TRIMMED) { |
516 | ret = verify_trimmed_io_u(td, io_u); | |
517 | goto done; | |
518 | } | |
36690c9b | 519 | |
3f9f4e26 | 520 | hdr_inc = io_u->buflen; |
a59e170d JA |
521 | if (td->o.verify_interval) |
522 | hdr_inc = td->o.verify_interval; | |
e29d1b70 | 523 | |
a12a3b4d | 524 | ret = 0; |
5ec10eaa JA |
525 | for (p = io_u->buf; p < io_u->buf + io_u->buflen; |
526 | p += hdr_inc, hdr_num++) { | |
bfacf389 JA |
527 | struct vcont vc = { |
528 | .io_u = io_u, | |
529 | .hdr_num = hdr_num, | |
530 | }; | |
936216f8 | 531 | |
f3e6cb95 | 532 | if (ret && td->o.verify_fatal) |
a12a3b4d | 533 | break; |
f3e6cb95 | 534 | |
a944e335 | 535 | hdr_size = __hdr_size(td->o.verify); |
a59e170d | 536 | if (td->o.verify_offset) |
a944e335 | 537 | memswp(p, p + td->o.verify_offset, hdr_size); |
95646108 | 538 | hdr = p; |
e29d1b70 | 539 | |
3f199b01 | 540 | if (hdr->fio_magic != FIO_HDR_MAGIC) { |
c9b44031 JA |
541 | log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n", |
542 | hdr->fio_magic, FIO_HDR_MAGIC, | |
543 | io_u->file->file_name, | |
544 | io_u->offset + hdr_num * hdr->len, hdr->len); | |
9fd18969 | 545 | return EILSEQ; |
3f199b01 JA |
546 | } |
547 | ||
e28218f3 | 548 | if (td->o.verify_pattern_bytes) { |
bd6f78b2 JA |
549 | dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n", |
550 | io_u, hdr->len); | |
e28218f3 | 551 | ret = verify_io_u_pattern(td->o.verify_pattern, |
0e92f873 RR |
552 | td->o.verify_pattern_bytes, |
553 | p + hdr_size, | |
554 | hdr_inc - hdr_size, | |
555 | hdr_size % td->o.verify_pattern_bytes); | |
c9b44031 JA |
556 | |
557 | if (ret) { | |
558 | log_err("pattern: verify failed at file %s offset %llu, length %u\n", | |
559 | io_u->file->file_name, | |
560 | io_u->offset + hdr_num * hdr->len, | |
561 | hdr->len); | |
562 | } | |
563 | ||
e92d3d71 RR |
564 | /* |
565 | * Also verify the meta data, if applicable | |
566 | */ | |
567 | if (hdr->verify_type == VERIFY_META) | |
936216f8 | 568 | ret |= verify_io_u_meta(hdr, td, &vc); |
e28218f3 SL |
569 | continue; |
570 | } | |
571 | ||
3f9f4e26 SL |
572 | switch (hdr->verify_type) { |
573 | case VERIFY_MD5: | |
936216f8 | 574 | ret = verify_io_u_md5(hdr, &vc); |
3f9f4e26 SL |
575 | break; |
576 | case VERIFY_CRC64: | |
936216f8 | 577 | ret = verify_io_u_crc64(hdr, &vc); |
3f9f4e26 | 578 | break; |
bac39e0e | 579 | case VERIFY_CRC32C: |
3845591f | 580 | case VERIFY_CRC32C_INTEL: |
936216f8 | 581 | ret = verify_io_u_crc32c(hdr, &vc); |
bac39e0e | 582 | break; |
3f9f4e26 | 583 | case VERIFY_CRC32: |
936216f8 | 584 | ret = verify_io_u_crc32(hdr, &vc); |
3f9f4e26 SL |
585 | break; |
586 | case VERIFY_CRC16: | |
936216f8 | 587 | ret = verify_io_u_crc16(hdr, &vc); |
3f9f4e26 SL |
588 | break; |
589 | case VERIFY_CRC7: | |
936216f8 | 590 | ret = verify_io_u_crc7(hdr, &vc); |
3f9f4e26 | 591 | break; |
cd14cc10 | 592 | case VERIFY_SHA256: |
936216f8 | 593 | ret = verify_io_u_sha256(hdr, &vc); |
cd14cc10 JA |
594 | break; |
595 | case VERIFY_SHA512: | |
936216f8 | 596 | ret = verify_io_u_sha512(hdr, &vc); |
cd14cc10 | 597 | break; |
7437ee87 | 598 | case VERIFY_META: |
936216f8 | 599 | ret = verify_io_u_meta(hdr, td, &vc); |
7437ee87 | 600 | break; |
7c353ceb | 601 | case VERIFY_SHA1: |
936216f8 | 602 | ret = verify_io_u_sha1(hdr, &vc); |
7c353ceb | 603 | break; |
3f9f4e26 SL |
604 | default: |
605 | log_err("Bad verify type %u\n", hdr->verify_type); | |
d16d4e09 | 606 | ret = EINVAL; |
3f9f4e26 | 607 | } |
3f9f4e26 | 608 | } |
a7dfe862 | 609 | |
0d29de83 | 610 | done: |
f3e6cb95 JA |
611 | if (ret && td->o.verify_fatal) |
612 | td->terminate = 1; | |
613 | ||
a12a3b4d | 614 | return ret; |
e29d1b70 JA |
615 | } |
616 | ||
7437ee87 | 617 | static void fill_meta(struct verify_header *hdr, struct thread_data *td, |
5ec10eaa | 618 | struct io_u *io_u, unsigned int header_num) |
7437ee87 SL |
619 | { |
620 | struct vhdr_meta *vh = hdr_priv(hdr); | |
621 | ||
622 | vh->thread = td->thread_number; | |
623 | ||
624 | vh->time_sec = io_u->start_time.tv_sec; | |
625 | vh->time_usec = io_u->start_time.tv_usec; | |
626 | ||
627 | vh->numberio = td->io_issues[DDIR_WRITE]; | |
628 | ||
629 | vh->offset = io_u->offset + header_num * td->o.verify_interval; | |
630 | } | |
631 | ||
cd14cc10 JA |
632 | static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len) |
633 | { | |
546dfd9f | 634 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
cd14cc10 | 635 | struct sha512_ctx sha512_ctx = { |
546dfd9f | 636 | .buf = vh->sha512, |
cd14cc10 JA |
637 | }; |
638 | ||
639 | sha512_init(&sha512_ctx); | |
640 | sha512_update(&sha512_ctx, p, len); | |
641 | } | |
642 | ||
643 | static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len) | |
644 | { | |
546dfd9f | 645 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
cd14cc10 | 646 | struct sha256_ctx sha256_ctx = { |
546dfd9f | 647 | .buf = vh->sha256, |
cd14cc10 JA |
648 | }; |
649 | ||
650 | sha256_init(&sha256_ctx); | |
651 | sha256_update(&sha256_ctx, p, len); | |
652 | } | |
653 | ||
7c353ceb JA |
654 | static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len) |
655 | { | |
656 | struct vhdr_sha1 *vh = hdr_priv(hdr); | |
657 | struct sha1_ctx sha1_ctx = { | |
658 | .H = vh->sha1, | |
659 | }; | |
660 | ||
661 | sha1_init(&sha1_ctx); | |
662 | sha1_update(&sha1_ctx, p, len); | |
663 | } | |
664 | ||
1e154bdb JA |
665 | static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len) |
666 | { | |
546dfd9f JA |
667 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
668 | ||
669 | vh->crc7 = crc7(p, len); | |
1e154bdb JA |
670 | } |
671 | ||
969f7ed3 JA |
672 | static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len) |
673 | { | |
546dfd9f JA |
674 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
675 | ||
676 | vh->crc16 = crc16(p, len); | |
969f7ed3 JA |
677 | } |
678 | ||
e29d1b70 JA |
679 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
680 | { | |
546dfd9f JA |
681 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
682 | ||
683 | vh->crc32 = crc32(p, len); | |
e29d1b70 JA |
684 | } |
685 | ||
bac39e0e JA |
686 | static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len) |
687 | { | |
688 | struct vhdr_crc32 *vh = hdr_priv(hdr); | |
689 | ||
3845591f JA |
690 | if (hdr->verify_type == VERIFY_CRC32C_INTEL) |
691 | vh->crc32 = crc32c_intel(p, len); | |
692 | else | |
693 | vh->crc32 = crc32c(p, len); | |
bac39e0e JA |
694 | } |
695 | ||
d77a7af3 JA |
696 | static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len) |
697 | { | |
546dfd9f JA |
698 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
699 | ||
700 | vh->crc64 = crc64(p, len); | |
d77a7af3 JA |
701 | } |
702 | ||
e29d1b70 JA |
703 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
704 | { | |
546dfd9f | 705 | struct vhdr_md5 *vh = hdr_priv(hdr); |
8c432325 | 706 | struct md5_ctx md5_ctx = { |
546dfd9f | 707 | .hash = (uint32_t *) vh->md5_digest, |
8c432325 | 708 | }; |
e29d1b70 | 709 | |
61f821f1 | 710 | md5_init(&md5_ctx); |
e29d1b70 | 711 | md5_update(&md5_ctx, p, len); |
e29d1b70 JA |
712 | } |
713 | ||
714 | /* | |
715 | * fill body of io_u->buf with random data and add a header with the | |
716 | * crc32 or md5 sum of that data. | |
717 | */ | |
718 | void populate_verify_io_u(struct thread_data *td, struct io_u *io_u) | |
719 | { | |
baefa9be | 720 | struct verify_header *hdr; |
95646108 | 721 | void *p = io_u->buf, *data; |
7437ee87 | 722 | unsigned int hdr_inc, data_len, header_num = 0; |
e29d1b70 | 723 | |
9cc3d150 JA |
724 | if (td->o.verify == VERIFY_NULL) |
725 | return; | |
726 | ||
cbe8d756 | 727 | fill_pattern(td, p, io_u->buflen, io_u); |
546a9142 SL |
728 | |
729 | hdr_inc = io_u->buflen; | |
a59e170d JA |
730 | if (td->o.verify_interval) |
731 | hdr_inc = td->o.verify_interval; | |
baefa9be | 732 | |
5ec10eaa | 733 | for (; p < io_u->buf + io_u->buflen; p += hdr_inc) { |
95646108 | 734 | hdr = p; |
3f9f4e26 SL |
735 | |
736 | hdr->fio_magic = FIO_HDR_MAGIC; | |
737 | hdr->verify_type = td->o.verify; | |
546a9142 | 738 | hdr->len = hdr_inc; |
87677832 | 739 | data_len = hdr_inc - hdr_size(hdr); |
3f9f4e26 | 740 | |
87677832 | 741 | data = p + hdr_size(hdr); |
3f9f4e26 SL |
742 | switch (td->o.verify) { |
743 | case VERIFY_MD5: | |
bd6f78b2 JA |
744 | dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n", |
745 | io_u, hdr->len); | |
3f9f4e26 SL |
746 | fill_md5(hdr, data, data_len); |
747 | break; | |
748 | case VERIFY_CRC64: | |
bd6f78b2 JA |
749 | dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n", |
750 | io_u, hdr->len); | |
3f9f4e26 SL |
751 | fill_crc64(hdr, data, data_len); |
752 | break; | |
bac39e0e | 753 | case VERIFY_CRC32C: |
3845591f | 754 | case VERIFY_CRC32C_INTEL: |
bac39e0e JA |
755 | dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n", |
756 | io_u, hdr->len); | |
757 | fill_crc32c(hdr, data, data_len); | |
758 | break; | |
3f9f4e26 | 759 | case VERIFY_CRC32: |
bd6f78b2 JA |
760 | dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n", |
761 | io_u, hdr->len); | |
3f9f4e26 SL |
762 | fill_crc32(hdr, data, data_len); |
763 | break; | |
764 | case VERIFY_CRC16: | |
bd6f78b2 JA |
765 | dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n", |
766 | io_u, hdr->len); | |
3f9f4e26 SL |
767 | fill_crc16(hdr, data, data_len); |
768 | break; | |
769 | case VERIFY_CRC7: | |
bd6f78b2 JA |
770 | dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n", |
771 | io_u, hdr->len); | |
3f9f4e26 SL |
772 | fill_crc7(hdr, data, data_len); |
773 | break; | |
cd14cc10 | 774 | case VERIFY_SHA256: |
bd6f78b2 JA |
775 | dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n", |
776 | io_u, hdr->len); | |
cd14cc10 JA |
777 | fill_sha256(hdr, data, data_len); |
778 | break; | |
779 | case VERIFY_SHA512: | |
bd6f78b2 JA |
780 | dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n", |
781 | io_u, hdr->len); | |
cd14cc10 JA |
782 | fill_sha512(hdr, data, data_len); |
783 | break; | |
7437ee87 | 784 | case VERIFY_META: |
bd6f78b2 JA |
785 | dprint(FD_VERIFY, "fill meta io_u %p, len %u\n", |
786 | io_u, hdr->len); | |
7437ee87 SL |
787 | fill_meta(hdr, td, io_u, header_num); |
788 | break; | |
7c353ceb JA |
789 | case VERIFY_SHA1: |
790 | dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n", | |
791 | io_u, hdr->len); | |
792 | fill_sha1(hdr, data, data_len); | |
793 | break; | |
3f9f4e26 SL |
794 | default: |
795 | log_err("fio: bad verify type: %d\n", td->o.verify); | |
796 | assert(0); | |
797 | } | |
a59e170d | 798 | if (td->o.verify_offset) |
87677832 | 799 | memswp(p, p + td->o.verify_offset, hdr_size(hdr)); |
7437ee87 | 800 | header_num++; |
e29d1b70 | 801 | } |
e29d1b70 JA |
802 | } |
803 | ||
804 | int get_next_verify(struct thread_data *td, struct io_u *io_u) | |
805 | { | |
8de8f047 | 806 | struct io_piece *ipo = NULL; |
e29d1b70 | 807 | |
d2d7fa53 JA |
808 | /* |
809 | * this io_u is from a requeue, we already filled the offsets | |
810 | */ | |
811 | if (io_u->file) | |
812 | return 0; | |
813 | ||
8de8f047 JA |
814 | if (!RB_EMPTY_ROOT(&td->io_hist_tree)) { |
815 | struct rb_node *n = rb_first(&td->io_hist_tree); | |
e29d1b70 | 816 | |
8de8f047 | 817 | ipo = rb_entry(n, struct io_piece, rb_node); |
4b87898e | 818 | rb_erase(n, &td->io_hist_tree); |
01743ee1 JA |
819 | } else if (!flist_empty(&td->io_hist_list)) { |
820 | ipo = flist_entry(td->io_hist_list.next, struct io_piece, list); | |
821 | flist_del(&ipo->list); | |
8de8f047 | 822 | } |
e29d1b70 | 823 | |
8de8f047 | 824 | if (ipo) { |
0d29de83 JA |
825 | td->io_hist_len--; |
826 | ||
e29d1b70 JA |
827 | io_u->offset = ipo->offset; |
828 | io_u->buflen = ipo->len; | |
36167d82 | 829 | io_u->file = ipo->file; |
97af62ce | 830 | |
0d29de83 JA |
831 | if (ipo->flags & IP_F_TRIMMED) |
832 | io_u->flags |= IO_U_F_TRIMMED; | |
833 | ||
d6aed795 | 834 | if (!fio_file_open(io_u->file)) { |
97af62ce JA |
835 | int r = td_io_open_file(td, io_u->file); |
836 | ||
bd6f78b2 JA |
837 | if (r) { |
838 | dprint(FD_VERIFY, "failed file %s open\n", | |
839 | io_u->file->file_name); | |
97af62ce | 840 | return 1; |
bd6f78b2 | 841 | } |
97af62ce JA |
842 | } |
843 | ||
844 | get_file(ipo->file); | |
d6aed795 | 845 | assert(fio_file_open(io_u->file)); |
e29d1b70 | 846 | io_u->ddir = DDIR_READ; |
36167d82 JA |
847 | io_u->xfer_buf = io_u->buf; |
848 | io_u->xfer_buflen = io_u->buflen; | |
0d29de83 JA |
849 | |
850 | remove_trim_entry(td, ipo); | |
e29d1b70 | 851 | free(ipo); |
bd6f78b2 | 852 | dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u); |
e29d1b70 JA |
853 | return 0; |
854 | } | |
855 | ||
bd6f78b2 | 856 | dprint(FD_VERIFY, "get_next_verify: empty\n"); |
e29d1b70 JA |
857 | return 1; |
858 | } | |
e8462bd8 JA |
859 | |
860 | static void *verify_async_thread(void *data) | |
861 | { | |
862 | struct thread_data *td = data; | |
863 | struct io_u *io_u; | |
864 | int ret = 0; | |
865 | ||
866 | if (td->o.verify_cpumask_set && | |
867 | fio_setaffinity(td->pid, td->o.verify_cpumask)) { | |
868 | log_err("fio: failed setting verify thread affinity\n"); | |
869 | goto done; | |
870 | } | |
871 | ||
872 | do { | |
e53ab27c JA |
873 | FLIST_HEAD(list); |
874 | ||
e8462bd8 JA |
875 | read_barrier(); |
876 | if (td->verify_thread_exit) | |
877 | break; | |
878 | ||
879 | pthread_mutex_lock(&td->io_u_lock); | |
880 | ||
881 | while (flist_empty(&td->verify_list) && | |
882 | !td->verify_thread_exit) { | |
b36e298b JA |
883 | ret = pthread_cond_wait(&td->verify_cond, |
884 | &td->io_u_lock); | |
e8462bd8 JA |
885 | if (ret) { |
886 | pthread_mutex_unlock(&td->io_u_lock); | |
887 | break; | |
888 | } | |
889 | } | |
890 | ||
e53ab27c JA |
891 | flist_splice_init(&td->verify_list, &list); |
892 | pthread_mutex_unlock(&td->io_u_lock); | |
893 | ||
894 | if (flist_empty(&list)) | |
e8462bd8 | 895 | continue; |
e8462bd8 | 896 | |
e53ab27c JA |
897 | while (!flist_empty(&list)) { |
898 | io_u = flist_entry(list.next, struct io_u, list); | |
899 | flist_del_init(&io_u->list); | |
e8462bd8 | 900 | |
d561f2ab | 901 | ret = verify_io_u(td, io_u); |
e53ab27c | 902 | put_io_u(td, io_u); |
d561f2ab JA |
903 | if (!ret) |
904 | continue; | |
905 | if (td->o.continue_on_error && | |
906 | td_non_fatal_error(ret)) { | |
907 | update_error_count(td, ret); | |
908 | td_clear_error(td); | |
909 | ret = 0; | |
910 | } | |
e53ab27c | 911 | } |
e8462bd8 JA |
912 | } while (!ret); |
913 | ||
d561f2ab JA |
914 | if (ret) { |
915 | td_verror(td, ret, "async_verify"); | |
f3e6cb95 JA |
916 | if (td->o.verify_fatal) |
917 | td->terminate = 1; | |
d561f2ab JA |
918 | } |
919 | ||
e8462bd8 JA |
920 | done: |
921 | pthread_mutex_lock(&td->io_u_lock); | |
922 | td->nr_verify_threads--; | |
923 | pthread_mutex_unlock(&td->io_u_lock); | |
924 | ||
925 | pthread_cond_signal(&td->free_cond); | |
926 | return NULL; | |
927 | } | |
928 | ||
929 | int verify_async_init(struct thread_data *td) | |
930 | { | |
931 | int i, ret; | |
304a47c7 VA |
932 | pthread_attr_t attr; |
933 | ||
934 | pthread_attr_init(&attr); | |
935 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN); | |
e8462bd8 JA |
936 | |
937 | td->verify_thread_exit = 0; | |
938 | ||
939 | td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async); | |
940 | for (i = 0; i < td->o.verify_async; i++) { | |
304a47c7 | 941 | ret = pthread_create(&td->verify_threads[i], &attr, |
e8462bd8 JA |
942 | verify_async_thread, td); |
943 | if (ret) { | |
944 | log_err("fio: async verify creation failed: %s\n", | |
945 | strerror(ret)); | |
946 | break; | |
947 | } | |
948 | ret = pthread_detach(td->verify_threads[i]); | |
949 | if (ret) { | |
950 | log_err("fio: async verify thread detach failed: %s\n", | |
951 | strerror(ret)); | |
952 | break; | |
953 | } | |
954 | td->nr_verify_threads++; | |
955 | } | |
956 | ||
304a47c7 VA |
957 | pthread_attr_destroy(&attr); |
958 | ||
e8462bd8 | 959 | if (i != td->o.verify_async) { |
e40823b1 | 960 | log_err("fio: only %d verify threads started, exiting\n", i); |
e8462bd8 JA |
961 | td->verify_thread_exit = 1; |
962 | write_barrier(); | |
963 | pthread_cond_broadcast(&td->verify_cond); | |
964 | return 1; | |
965 | } | |
966 | ||
967 | return 0; | |
968 | } | |
969 | ||
970 | void verify_async_exit(struct thread_data *td) | |
971 | { | |
972 | td->verify_thread_exit = 1; | |
973 | write_barrier(); | |
974 | pthread_cond_broadcast(&td->verify_cond); | |
975 | ||
976 | pthread_mutex_lock(&td->io_u_lock); | |
977 | ||
978 | while (td->nr_verify_threads) | |
979 | pthread_cond_wait(&td->free_cond, &td->io_u_lock); | |
980 | ||
981 | pthread_mutex_unlock(&td->io_u_lock); | |
982 | free(td->verify_threads); | |
983 | td->verify_threads = NULL; | |
984 | } |