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