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> |
79402a12 | 9 | #include <libgen.h> |
e29d1b70 JA |
10 | |
11 | #include "fio.h" | |
4f5af7b2 | 12 | #include "verify.h" |
0d29de83 | 13 | #include "trim.h" |
637ef8d9 | 14 | #include "lib/rand.h" |
51aa2da8 | 15 | #include "lib/hweight.h" |
2cac8fcb | 16 | #include "lib/pattern.h" |
e29d1b70 | 17 | |
eef6eea1 JA |
18 | #include "crc/md5.h" |
19 | #include "crc/crc64.h" | |
20 | #include "crc/crc32.h" | |
bac39e0e | 21 | #include "crc/crc32c.h" |
eef6eea1 JA |
22 | #include "crc/crc16.h" |
23 | #include "crc/crc7.h" | |
24 | #include "crc/sha256.h" | |
25 | #include "crc/sha512.h" | |
7c353ceb | 26 | #include "crc/sha1.h" |
844ea602 | 27 | #include "crc/xxhash.h" |
cd14cc10 | 28 | |
7d9fb455 JA |
29 | static void populate_hdr(struct thread_data *td, struct io_u *io_u, |
30 | struct verify_header *hdr, unsigned int header_num, | |
31 | unsigned int header_len); | |
b638d82f RP |
32 | static void fill_hdr(struct thread_data *td, struct io_u *io_u, |
33 | struct verify_header *hdr, unsigned int header_num, | |
34 | unsigned int header_len, uint64_t rand_seed); | |
35 | static void __fill_hdr(struct thread_data *td, struct io_u *io_u, | |
36 | struct verify_header *hdr, unsigned int header_num, | |
37 | unsigned int header_len, uint64_t rand_seed); | |
7d9fb455 | 38 | |
ce35b1ec JA |
39 | void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len) |
40 | { | |
2cac8fcb | 41 | (void)cpy_pattern(td->o.buffer_pattern, td->o.buffer_pattern_bytes, p, len); |
ce35b1ec JA |
42 | } |
43 | ||
a89ba4b1 JA |
44 | static void __fill_buffer(struct thread_options *o, unsigned long seed, void *p, |
45 | unsigned int len) | |
7fa085ea JA |
46 | { |
47 | __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes); | |
48 | } | |
49 | ||
a89ba4b1 JA |
50 | static unsigned long fill_buffer(struct thread_data *td, void *p, |
51 | unsigned int len) | |
7fa085ea JA |
52 | { |
53 | struct frand_state *fs = &td->verify_state; | |
54 | struct thread_options *o = &td->o; | |
55 | ||
56 | return fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes); | |
57 | } | |
58 | ||
ce35b1ec JA |
59 | void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len, |
60 | struct io_u *io_u, unsigned long seed, int use_seed) | |
61 | { | |
bc769898 JA |
62 | struct thread_options *o = &td->o; |
63 | ||
64 | if (!o->verify_pattern_bytes) { | |
ce35b1ec JA |
65 | dprint(FD_VERIFY, "fill random bytes len=%u\n", len); |
66 | ||
67 | if (use_seed) | |
7fa085ea JA |
68 | __fill_buffer(o, seed, p, len); |
69 | else | |
70 | io_u->rand_seed = fill_buffer(td, p, len); | |
ce35b1ec JA |
71 | return; |
72 | } | |
c4b6117b | 73 | |
61b9861d RP |
74 | /* Skip if we were here and we do not need to patch pattern |
75 | * with format */ | |
76 | if (!td->o.verify_fmt_sz && io_u->buf_filled_len >= len) { | |
ce35b1ec | 77 | dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n", |
bc769898 | 78 | o->verify_pattern_bytes, len); |
ce35b1ec JA |
79 | return; |
80 | } | |
81 | ||
61b9861d RP |
82 | (void)paste_format(td->o.verify_pattern, td->o.verify_pattern_bytes, |
83 | td->o.verify_fmt, td->o.verify_fmt_sz, | |
84 | p, len, io_u); | |
ce35b1ec JA |
85 | io_u->buf_filled_len = len; |
86 | } | |
87 | ||
cfbcd0dc JA |
88 | static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u) |
89 | { | |
90 | unsigned int hdr_inc; | |
91 | ||
92 | hdr_inc = io_u->buflen; | |
8a99fdf6 | 93 | if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen) |
cfbcd0dc JA |
94 | hdr_inc = td->o.verify_interval; |
95 | ||
96 | return hdr_inc; | |
97 | } | |
98 | ||
c50ca7bd JA |
99 | static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u, |
100 | unsigned long seed, int use_seed) | |
101 | { | |
102 | unsigned int hdr_inc, header_num; | |
103 | struct verify_header *hdr; | |
104 | void *p = io_u->buf; | |
105 | ||
ce35b1ec | 106 | fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed); |
c50ca7bd | 107 | |
cfbcd0dc | 108 | hdr_inc = get_hdr_inc(td, io_u); |
c50ca7bd JA |
109 | header_num = 0; |
110 | for (; p < io_u->buf + io_u->buflen; p += hdr_inc) { | |
111 | hdr = p; | |
112 | populate_hdr(td, io_u, hdr, header_num, hdr_inc); | |
113 | header_num++; | |
114 | } | |
115 | } | |
116 | ||
4764aec9 | 117 | static void memswp(void *buf1, void *buf2, unsigned int len) |
546a9142 | 118 | { |
dee6de74 SL |
119 | char swap[200]; |
120 | ||
121 | assert(len <= sizeof(swap)); | |
90059d65 | 122 | |
546a9142 SL |
123 | memcpy(&swap, buf1, len); |
124 | memcpy(buf1, buf2, len); | |
125 | memcpy(buf2, &swap, len); | |
126 | } | |
127 | ||
e29d1b70 JA |
128 | static void hexdump(void *buffer, int len) |
129 | { | |
130 | unsigned char *p = buffer; | |
131 | int i; | |
132 | ||
133 | for (i = 0; i < len; i++) | |
bfacf389 JA |
134 | log_err("%02x", p[i]); |
135 | log_err("\n"); | |
e29d1b70 JA |
136 | } |
137 | ||
87677832 | 138 | /* |
de8f6de9 | 139 | * Prepare for separation of verify_header and checksum header |
87677832 | 140 | */ |
546dfd9f | 141 | static inline unsigned int __hdr_size(int verify_type) |
87677832 | 142 | { |
03e20d68 | 143 | unsigned int len = 0; |
87677832 | 144 | |
546dfd9f JA |
145 | switch (verify_type) { |
146 | case VERIFY_NONE: | |
b638d82f | 147 | case VERIFY_HDR_ONLY: |
546dfd9f | 148 | case VERIFY_NULL: |
ed3a433d | 149 | case VERIFY_PATTERN: |
546dfd9f JA |
150 | len = 0; |
151 | break; | |
152 | case VERIFY_MD5: | |
153 | len = sizeof(struct vhdr_md5); | |
154 | break; | |
155 | case VERIFY_CRC64: | |
156 | len = sizeof(struct vhdr_crc64); | |
157 | break; | |
bac39e0e | 158 | case VERIFY_CRC32C: |
546dfd9f | 159 | case VERIFY_CRC32: |
3845591f | 160 | case VERIFY_CRC32C_INTEL: |
546dfd9f JA |
161 | len = sizeof(struct vhdr_crc32); |
162 | break; | |
163 | case VERIFY_CRC16: | |
164 | len = sizeof(struct vhdr_crc16); | |
165 | break; | |
166 | case VERIFY_CRC7: | |
167 | len = sizeof(struct vhdr_crc7); | |
168 | break; | |
169 | case VERIFY_SHA256: | |
170 | len = sizeof(struct vhdr_sha256); | |
171 | break; | |
172 | case VERIFY_SHA512: | |
173 | len = sizeof(struct vhdr_sha512); | |
174 | break; | |
844ea602 JA |
175 | case VERIFY_XXHASH: |
176 | len = sizeof(struct vhdr_xxhash); | |
177 | break; | |
7c353ceb JA |
178 | case VERIFY_SHA1: |
179 | len = sizeof(struct vhdr_sha1); | |
180 | break; | |
59245381 | 181 | case VERIFY_PATTERN_NO_HDR: |
ed3a433d | 182 | return 0; |
546dfd9f JA |
183 | default: |
184 | log_err("fio: unknown verify header!\n"); | |
185 | assert(0); | |
186 | } | |
187 | ||
188 | return len + sizeof(struct verify_header); | |
87677832 JA |
189 | } |
190 | ||
ed3a433d JA |
191 | static inline unsigned int hdr_size(struct thread_data *td, |
192 | struct verify_header *hdr) | |
87677832 | 193 | { |
ed3a433d JA |
194 | if (td->o.verify == VERIFY_PATTERN_NO_HDR) |
195 | return 0; | |
196 | ||
546dfd9f JA |
197 | return __hdr_size(hdr->verify_type); |
198 | } | |
199 | ||
200 | static void *hdr_priv(struct verify_header *hdr) | |
201 | { | |
202 | void *priv = hdr; | |
203 | ||
204 | return priv + sizeof(struct verify_header); | |
87677832 JA |
205 | } |
206 | ||
936216f8 JA |
207 | /* |
208 | * Verify container, pass info to verify handlers and allow them to | |
209 | * pass info back in case of error | |
210 | */ | |
211 | struct vcont { | |
212 | /* | |
213 | * Input | |
214 | */ | |
215 | struct io_u *io_u; | |
216 | unsigned int hdr_num; | |
7d9fb455 | 217 | struct thread_data *td; |
936216f8 JA |
218 | |
219 | /* | |
220 | * Output, only valid in case of error | |
221 | */ | |
bfacf389 JA |
222 | const char *name; |
223 | void *good_crc; | |
224 | void *bad_crc; | |
936216f8 JA |
225 | unsigned int crc_len; |
226 | }; | |
227 | ||
dacbbb88 JA |
228 | #define DUMP_BUF_SZ 255 |
229 | static int dump_buf_warned; | |
230 | ||
c50ca7bd JA |
231 | static void dump_buf(char *buf, unsigned int len, unsigned long long offset, |
232 | const char *type, struct fio_file *f) | |
7d9fb455 | 233 | { |
dacbbb88 JA |
234 | char *ptr, fname[DUMP_BUF_SZ]; |
235 | size_t buf_left = DUMP_BUF_SZ; | |
7d9fb455 JA |
236 | int ret, fd; |
237 | ||
6f260b31 | 238 | ptr = strdup(f->file_name); |
c50ca7bd | 239 | |
d264264a JA |
240 | memset(fname, 0, sizeof(fname)); |
241 | if (aux_path) | |
242 | sprintf(fname, "%s%s", aux_path, FIO_OS_PATH_SEPARATOR); | |
243 | ||
244 | strncpy(fname + strlen(fname), basename(ptr), buf_left - 1); | |
dacbbb88 JA |
245 | |
246 | buf_left -= strlen(fname); | |
247 | if (buf_left <= 0) { | |
248 | if (!dump_buf_warned) { | |
249 | log_err("fio: verify failure dump buffer too small\n"); | |
250 | dump_buf_warned = 1; | |
251 | } | |
252 | free(ptr); | |
253 | return; | |
254 | } | |
255 | ||
256 | snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type); | |
7d9fb455 JA |
257 | |
258 | fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644); | |
259 | if (fd < 0) { | |
260 | perror("open verify buf file"); | |
261 | return; | |
262 | } | |
263 | ||
264 | while (len) { | |
265 | ret = write(fd, buf, len); | |
266 | if (!ret) | |
267 | break; | |
268 | else if (ret < 0) { | |
269 | perror("write verify buf file"); | |
270 | break; | |
271 | } | |
272 | len -= ret; | |
273 | buf += ret; | |
274 | } | |
275 | ||
276 | close(fd); | |
c50ca7bd | 277 | log_err(" %s data dumped as %s\n", type, fname); |
6f260b31 | 278 | free(ptr); |
7d9fb455 JA |
279 | } |
280 | ||
c50ca7bd JA |
281 | /* |
282 | * Dump the contents of the read block and re-generate the correct data | |
283 | * and dump that too. | |
284 | */ | |
9ba987cc | 285 | static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc) |
7d9fb455 JA |
286 | { |
287 | struct thread_data *td = vc->td; | |
288 | struct io_u *io_u = vc->io_u; | |
289 | unsigned long hdr_offset; | |
7d9fb455 | 290 | struct io_u dummy; |
c50ca7bd | 291 | void *buf; |
7d9fb455 | 292 | |
0dce9bc9 SL |
293 | if (!td->o.verify_dump) |
294 | return; | |
295 | ||
c50ca7bd JA |
296 | /* |
297 | * Dump the contents we just read off disk | |
298 | */ | |
7d9fb455 JA |
299 | hdr_offset = vc->hdr_num * hdr->len; |
300 | ||
c50ca7bd JA |
301 | dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset, |
302 | "received", vc->io_u->file); | |
7d9fb455 | 303 | |
c50ca7bd JA |
304 | /* |
305 | * Allocate a new buf and re-generate the original data | |
306 | */ | |
307 | buf = malloc(io_u->buflen); | |
7d9fb455 | 308 | dummy = *io_u; |
c50ca7bd | 309 | dummy.buf = buf; |
4aae38a6 | 310 | dummy.rand_seed = hdr->rand_seed; |
cfbcd0dc | 311 | dummy.buf_filled_len = 0; |
0d81daf9 | 312 | dummy.buflen = io_u->buflen; |
7d9fb455 | 313 | |
c50ca7bd | 314 | fill_pattern_headers(td, &dummy, hdr->rand_seed, 1); |
7d9fb455 | 315 | |
c50ca7bd JA |
316 | dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset, |
317 | "expected", vc->io_u->file); | |
7d9fb455 JA |
318 | free(buf); |
319 | } | |
320 | ||
9ba987cc JA |
321 | static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc) |
322 | { | |
323 | struct thread_data *td = vc->td; | |
324 | struct verify_header shdr; | |
325 | ||
326 | if (td->o.verify == VERIFY_PATTERN_NO_HDR) { | |
b638d82f | 327 | __fill_hdr(td, vc->io_u, &shdr, 0, vc->io_u->buflen, 0); |
9ba987cc JA |
328 | hdr = &shdr; |
329 | } | |
330 | ||
331 | __dump_verify_buffers(hdr, vc); | |
332 | } | |
333 | ||
bfacf389 JA |
334 | static void log_verify_failure(struct verify_header *hdr, struct vcont *vc) |
335 | { | |
336 | unsigned long long offset; | |
337 | ||
338 | offset = vc->io_u->offset; | |
339 | offset += vc->hdr_num * hdr->len; | |
32c17adf JA |
340 | log_err("%.8s: verify failed at file %s offset %llu, length %u\n", |
341 | vc->name, vc->io_u->file->file_name, offset, hdr->len); | |
bfacf389 JA |
342 | |
343 | if (vc->good_crc && vc->bad_crc) { | |
344 | log_err(" Expected CRC: "); | |
345 | hexdump(vc->good_crc, vc->crc_len); | |
346 | log_err(" Received CRC: "); | |
347 | hexdump(vc->bad_crc, vc->crc_len); | |
348 | } | |
7d9fb455 JA |
349 | |
350 | dump_verify_buffers(hdr, vc); | |
bfacf389 JA |
351 | } |
352 | ||
d9f2caf3 JA |
353 | /* |
354 | * Return data area 'header_num' | |
355 | */ | |
936216f8 | 356 | static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc) |
d9f2caf3 | 357 | { |
ed3a433d | 358 | return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(vc->td, hdr); |
d9f2caf3 JA |
359 | } |
360 | ||
92bf48d5 JA |
361 | static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc) |
362 | { | |
363 | struct thread_data *td = vc->td; | |
364 | struct io_u *io_u = vc->io_u; | |
365 | char *buf, *pattern; | |
2b13e716 | 366 | unsigned int header_size = __hdr_size(td->o.verify); |
25276497 RP |
367 | unsigned int len, mod, i, pattern_size; |
368 | int rc; | |
92bf48d5 JA |
369 | |
370 | pattern = td->o.verify_pattern; | |
9a2a86d0 | 371 | pattern_size = td->o.verify_pattern_bytes; |
61b9861d RP |
372 | assert(pattern_size != 0); |
373 | ||
374 | (void)paste_format_inplace(pattern, pattern_size, | |
375 | td->o.verify_fmt, td->o.verify_fmt_sz, io_u); | |
376 | ||
2b13e716 JA |
377 | buf = (void *) hdr + header_size; |
378 | len = get_hdr_inc(td, io_u) - header_size; | |
61b9861d | 379 | mod = (get_hdr_inc(td, io_u) * vc->hdr_num + header_size) % pattern_size; |
9a2a86d0 | 380 | |
25276497 RP |
381 | rc = cmp_pattern(pattern, pattern_size, mod, buf, len); |
382 | if (!rc) | |
383 | return 0; | |
92bf48d5 | 384 | |
25276497 RP |
385 | /* Slow path, compare each byte */ |
386 | for (i = 0; i < len; i++) { | |
92bf48d5 JA |
387 | if (buf[i] != pattern[mod]) { |
388 | unsigned int bits; | |
389 | ||
390 | bits = hweight8(buf[i] ^ pattern[mod]); | |
25276497 RP |
391 | log_err("fio: got pattern '%02x', wanted '%02x'. Bad bits %d\n", |
392 | (unsigned char)buf[i], | |
393 | (unsigned char)pattern[mod], | |
394 | bits); | |
92bf48d5 | 395 | log_err("fio: bad pattern block offset %u\n", i); |
7ea0a9ad | 396 | vc->name = "pattern"; |
d0c8ccb0 | 397 | log_verify_failure(hdr, vc); |
92bf48d5 JA |
398 | return EILSEQ; |
399 | } | |
400 | mod++; | |
401 | if (mod == td->o.verify_pattern_bytes) | |
402 | mod = 0; | |
403 | } | |
404 | ||
25276497 RP |
405 | /* Unreachable line */ |
406 | assert(0); | |
407 | return EILSEQ; | |
92bf48d5 JA |
408 | } |
409 | ||
844ea602 JA |
410 | static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc) |
411 | { | |
412 | void *p = io_u_verify_off(hdr, vc); | |
413 | struct vhdr_xxhash *vh = hdr_priv(hdr); | |
414 | uint32_t hash; | |
415 | void *state; | |
416 | ||
417 | dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len); | |
418 | ||
419 | state = XXH32_init(1); | |
ed3a433d | 420 | XXH32_update(state, p, hdr->len - hdr_size(vc->td, hdr)); |
844ea602 JA |
421 | hash = XXH32_digest(state); |
422 | ||
423 | if (vh->hash == hash) | |
424 | return 0; | |
425 | ||
426 | vc->name = "xxhash"; | |
427 | vc->good_crc = &vh->hash; | |
428 | vc->bad_crc = &hash; | |
429 | vc->crc_len = sizeof(hash); | |
430 | log_verify_failure(hdr, vc); | |
431 | return EILSEQ; | |
432 | } | |
433 | ||
936216f8 | 434 | static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc) |
cd14cc10 | 435 | { |
936216f8 | 436 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 437 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
cd14cc10 | 438 | uint8_t sha512[128]; |
25dfa848 | 439 | struct fio_sha512_ctx sha512_ctx = { |
cd14cc10 JA |
440 | .buf = sha512, |
441 | }; | |
442 | ||
936216f8 | 443 | dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 444 | |
25dfa848 | 445 | fio_sha512_init(&sha512_ctx); |
ed3a433d | 446 | fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(vc->td, hdr)); |
cd14cc10 | 447 | |
bfacf389 JA |
448 | if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) |
449 | return 0; | |
cd14cc10 | 450 | |
bfacf389 JA |
451 | vc->name = "sha512"; |
452 | vc->good_crc = vh->sha512; | |
453 | vc->bad_crc = sha512_ctx.buf; | |
454 | vc->crc_len = sizeof(vh->sha512); | |
455 | log_verify_failure(hdr, vc); | |
456 | return EILSEQ; | |
cd14cc10 JA |
457 | } |
458 | ||
936216f8 | 459 | static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc) |
cd14cc10 | 460 | { |
936216f8 | 461 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 462 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
bc77f56f | 463 | uint8_t sha256[64]; |
25dfa848 | 464 | struct fio_sha256_ctx sha256_ctx = { |
cd14cc10 JA |
465 | .buf = sha256, |
466 | }; | |
467 | ||
936216f8 | 468 | dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 469 | |
25dfa848 | 470 | fio_sha256_init(&sha256_ctx); |
ed3a433d | 471 | fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(vc->td, hdr)); |
017052b6 | 472 | fio_sha256_final(&sha256_ctx); |
cd14cc10 | 473 | |
bfacf389 JA |
474 | if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) |
475 | return 0; | |
cd14cc10 | 476 | |
bfacf389 JA |
477 | vc->name = "sha256"; |
478 | vc->good_crc = vh->sha256; | |
479 | vc->bad_crc = sha256_ctx.buf; | |
480 | vc->crc_len = sizeof(vh->sha256); | |
481 | log_verify_failure(hdr, vc); | |
482 | return EILSEQ; | |
cd14cc10 JA |
483 | } |
484 | ||
936216f8 | 485 | static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc) |
7c353ceb | 486 | { |
936216f8 | 487 | void *p = io_u_verify_off(hdr, vc); |
7c353ceb JA |
488 | struct vhdr_sha1 *vh = hdr_priv(hdr); |
489 | uint32_t sha1[5]; | |
25dfa848 | 490 | struct fio_sha1_ctx sha1_ctx = { |
7c353ceb JA |
491 | .H = sha1, |
492 | }; | |
493 | ||
936216f8 | 494 | dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
7c353ceb | 495 | |
25dfa848 | 496 | fio_sha1_init(&sha1_ctx); |
ed3a433d | 497 | fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(vc->td, hdr)); |
017052b6 | 498 | fio_sha1_final(&sha1_ctx); |
7c353ceb | 499 | |
bfacf389 JA |
500 | if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) |
501 | return 0; | |
7c353ceb | 502 | |
bfacf389 JA |
503 | vc->name = "sha1"; |
504 | vc->good_crc = vh->sha1; | |
505 | vc->bad_crc = sha1_ctx.H; | |
506 | vc->crc_len = sizeof(vh->sha1); | |
507 | log_verify_failure(hdr, vc); | |
508 | return EILSEQ; | |
7c353ceb JA |
509 | } |
510 | ||
936216f8 | 511 | static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc) |
1e154bdb | 512 | { |
936216f8 | 513 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 514 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
1e154bdb JA |
515 | unsigned char c; |
516 | ||
936216f8 | 517 | dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 518 | |
ed3a433d | 519 | c = fio_crc7(p, hdr->len - hdr_size(vc->td, hdr)); |
1e154bdb | 520 | |
bfacf389 JA |
521 | if (c == vh->crc7) |
522 | return 0; | |
1e154bdb | 523 | |
bfacf389 JA |
524 | vc->name = "crc7"; |
525 | vc->good_crc = &vh->crc7; | |
526 | vc->bad_crc = &c; | |
527 | vc->crc_len = 1; | |
528 | log_verify_failure(hdr, vc); | |
529 | return EILSEQ; | |
1e154bdb JA |
530 | } |
531 | ||
936216f8 | 532 | static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc) |
969f7ed3 | 533 | { |
936216f8 | 534 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 535 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
969f7ed3 JA |
536 | unsigned short c; |
537 | ||
936216f8 | 538 | dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 539 | |
ed3a433d | 540 | c = fio_crc16(p, hdr->len - hdr_size(vc->td, hdr)); |
969f7ed3 | 541 | |
bfacf389 JA |
542 | if (c == vh->crc16) |
543 | return 0; | |
969f7ed3 | 544 | |
bfacf389 JA |
545 | vc->name = "crc16"; |
546 | vc->good_crc = &vh->crc16; | |
547 | vc->bad_crc = &c; | |
548 | vc->crc_len = 2; | |
549 | log_verify_failure(hdr, vc); | |
550 | return EILSEQ; | |
969f7ed3 JA |
551 | } |
552 | ||
936216f8 | 553 | static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc) |
d77a7af3 | 554 | { |
936216f8 | 555 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 556 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
d77a7af3 JA |
557 | unsigned long long c; |
558 | ||
936216f8 | 559 | dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 560 | |
ed3a433d | 561 | c = fio_crc64(p, hdr->len - hdr_size(vc->td, hdr)); |
d77a7af3 | 562 | |
bfacf389 JA |
563 | if (c == vh->crc64) |
564 | return 0; | |
d77a7af3 | 565 | |
bfacf389 JA |
566 | vc->name = "crc64"; |
567 | vc->good_crc = &vh->crc64; | |
568 | vc->bad_crc = &c; | |
569 | vc->crc_len = 8; | |
570 | log_verify_failure(hdr, vc); | |
571 | return EILSEQ; | |
d77a7af3 JA |
572 | } |
573 | ||
936216f8 | 574 | static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc) |
e29d1b70 | 575 | { |
936216f8 | 576 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f JA |
577 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
578 | uint32_t c; | |
e29d1b70 | 579 | |
936216f8 | 580 | dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 581 | |
ed3a433d | 582 | c = fio_crc32(p, hdr->len - hdr_size(vc->td, hdr)); |
e29d1b70 | 583 | |
bfacf389 JA |
584 | if (c == vh->crc32) |
585 | return 0; | |
e29d1b70 | 586 | |
bfacf389 JA |
587 | vc->name = "crc32"; |
588 | vc->good_crc = &vh->crc32; | |
589 | vc->bad_crc = &c; | |
590 | vc->crc_len = 4; | |
591 | log_verify_failure(hdr, vc); | |
592 | return EILSEQ; | |
e29d1b70 JA |
593 | } |
594 | ||
936216f8 | 595 | static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc) |
bac39e0e | 596 | { |
936216f8 | 597 | void *p = io_u_verify_off(hdr, vc); |
bac39e0e JA |
598 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
599 | uint32_t c; | |
600 | ||
936216f8 | 601 | dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bac39e0e | 602 | |
ed3a433d | 603 | c = fio_crc32c(p, hdr->len - hdr_size(vc->td, hdr)); |
bac39e0e | 604 | |
bfacf389 JA |
605 | if (c == vh->crc32) |
606 | return 0; | |
bac39e0e | 607 | |
bfacf389 JA |
608 | vc->name = "crc32c"; |
609 | vc->good_crc = &vh->crc32; | |
610 | vc->bad_crc = &c; | |
611 | vc->crc_len = 4; | |
612 | log_verify_failure(hdr, vc); | |
613 | return EILSEQ; | |
bac39e0e JA |
614 | } |
615 | ||
936216f8 | 616 | static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc) |
e29d1b70 | 617 | { |
936216f8 | 618 | void *p = io_u_verify_off(hdr, vc); |
546dfd9f | 619 | struct vhdr_md5 *vh = hdr_priv(hdr); |
8c432325 | 620 | uint32_t hash[MD5_HASH_WORDS]; |
25dfa848 | 621 | struct fio_md5_ctx md5_ctx = { |
8c432325 JA |
622 | .hash = hash, |
623 | }; | |
e29d1b70 | 624 | |
936216f8 | 625 | dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len); |
bd6f78b2 | 626 | |
25dfa848 | 627 | fio_md5_init(&md5_ctx); |
ed3a433d | 628 | fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(vc->td, hdr)); |
017052b6 | 629 | fio_md5_final(&md5_ctx); |
e29d1b70 | 630 | |
bfacf389 JA |
631 | if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) |
632 | return 0; | |
e29d1b70 | 633 | |
bfacf389 JA |
634 | vc->name = "md5"; |
635 | vc->good_crc = vh->md5_digest; | |
636 | vc->bad_crc = md5_ctx.hash; | |
637 | vc->crc_len = sizeof(hash); | |
638 | log_verify_failure(hdr, vc); | |
639 | return EILSEQ; | |
e29d1b70 JA |
640 | } |
641 | ||
e8462bd8 JA |
642 | /* |
643 | * Push IO verification to a separate thread | |
644 | */ | |
f8b0bd10 | 645 | int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr) |
e8462bd8 | 646 | { |
f8b0bd10 | 647 | struct io_u *io_u = *io_u_ptr; |
e8462bd8 | 648 | |
e8462bd8 | 649 | pthread_mutex_lock(&td->io_u_lock); |
d7ee2a7d | 650 | |
f8b0bd10 JA |
651 | if (io_u->file) |
652 | put_file_log(td, io_u->file); | |
653 | ||
0c41214f RR |
654 | if (io_u->flags & IO_U_F_IN_CUR_DEPTH) { |
655 | td->cur_depth--; | |
1651e431 | 656 | io_u_clear(td, io_u, IO_U_F_IN_CUR_DEPTH); |
0c41214f | 657 | } |
2ae0b204 | 658 | flist_add_tail(&io_u->verify_list, &td->verify_list); |
f8b0bd10 | 659 | *io_u_ptr = NULL; |
e8462bd8 JA |
660 | pthread_mutex_unlock(&td->io_u_lock); |
661 | ||
662 | pthread_cond_signal(&td->verify_cond); | |
e8462bd8 JA |
663 | return 0; |
664 | } | |
665 | ||
323f58c5 JA |
666 | /* |
667 | * Thanks Rusty, for spending the time so I don't have to. | |
668 | * | |
669 | * http://rusty.ozlabs.org/?p=560 | |
670 | */ | |
671 | static int mem_is_zero(const void *data, size_t length) | |
672 | { | |
673 | const unsigned char *p = data; | |
674 | size_t len; | |
675 | ||
676 | /* Check first 16 bytes manually */ | |
677 | for (len = 0; len < 16; len++) { | |
678 | if (!length) | |
679 | return 1; | |
680 | if (*p) | |
681 | return 0; | |
682 | p++; | |
683 | length--; | |
684 | } | |
685 | ||
686 | /* Now we know that's zero, memcmp with self. */ | |
687 | return memcmp(data, p, length) == 0; | |
688 | } | |
689 | ||
690 | static int mem_is_zero_slow(const void *data, size_t length, size_t *offset) | |
691 | { | |
692 | const unsigned char *p = data; | |
693 | ||
694 | *offset = 0; | |
695 | while (length) { | |
696 | if (*p) | |
697 | break; | |
698 | (*offset)++; | |
699 | length--; | |
700 | p++; | |
701 | } | |
702 | ||
703 | return !length; | |
704 | } | |
705 | ||
0d29de83 JA |
706 | static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u) |
707 | { | |
323f58c5 | 708 | size_t offset; |
0d29de83 JA |
709 | |
710 | if (!td->o.trim_zero) | |
711 | return 0; | |
712 | ||
323f58c5 | 713 | if (mem_is_zero(io_u->buf, io_u->buflen)) |
0d29de83 JA |
714 | return 0; |
715 | ||
323f58c5 JA |
716 | mem_is_zero_slow(io_u->buf, io_u->buflen, &offset); |
717 | ||
a917a8b3 JA |
718 | log_err("trim: verify failed at file %s offset %llu, length %lu" |
719 | ", block offset %lu\n", | |
720 | io_u->file->file_name, io_u->offset, io_u->buflen, | |
323f58c5 JA |
721 | (unsigned long) offset); |
722 | return EILSEQ; | |
0d29de83 JA |
723 | } |
724 | ||
b638d82f RP |
725 | static int verify_header(struct io_u *io_u, struct thread_data *td, |
726 | struct verify_header *hdr, unsigned int hdr_num, | |
727 | unsigned int hdr_len) | |
f65d1c26 JA |
728 | { |
729 | void *p = hdr; | |
730 | uint32_t crc; | |
731 | ||
5964842c AG |
732 | if (hdr->magic != FIO_HDR_MAGIC) { |
733 | log_err("verify: bad magic header %x, wanted %x", | |
734 | hdr->magic, FIO_HDR_MAGIC); | |
735 | goto err; | |
736 | } | |
4445856a | 737 | if (hdr->len != hdr_len) { |
5964842c AG |
738 | log_err("verify: bad header length %u, wanted %u", |
739 | hdr->len, hdr_len); | |
740 | goto err; | |
741 | } | |
742 | if (hdr->rand_seed != io_u->rand_seed) { | |
743 | log_err("verify: bad header rand_seed %"PRIu64 | |
744 | ", wanted %"PRIu64, | |
745 | hdr->rand_seed, io_u->rand_seed); | |
746 | goto err; | |
747 | } | |
b638d82f RP |
748 | if (hdr->offset != io_u->offset + hdr_num * td->o.verify_interval) { |
749 | log_err("verify: bad header offset %"PRIu64 | |
750 | ", wanted %llu", | |
751 | hdr->offset, io_u->offset); | |
752 | goto err; | |
753 | } | |
754 | ||
755 | /* | |
756 | * For read-only workloads, the program cannot be certain of the | |
757 | * last numberio written to a block. Checking of numberio will be | |
758 | * done only for workloads that write data. For verify_only, | |
759 | * numberio will be checked in the last iteration when the correct | |
760 | * state of numberio, that would have been written to each block | |
761 | * in a previous run of fio, has been reached. | |
762 | */ | |
763 | if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) && | |
764 | !td->o.time_based) | |
765 | if (!td->o.verify_only || td->o.loops == 0) | |
766 | if (hdr->numberio != io_u->numberio) { | |
767 | log_err("verify: bad header numberio %"PRIu16 | |
768 | ", wanted %"PRIu16, | |
769 | hdr->numberio, io_u->numberio); | |
770 | goto err; | |
771 | } | |
ae38c0dc | 772 | |
25dfa848 | 773 | crc = fio_crc32c(p, offsetof(struct verify_header, crc32)); |
5964842c AG |
774 | if (crc != hdr->crc32) { |
775 | log_err("verify: bad header crc %x, calculated %x", | |
776 | hdr->crc32, crc); | |
777 | goto err; | |
778 | } | |
779 | return 0; | |
780 | ||
781 | err: | |
782 | log_err(" at file %s offset %llu, length %u\n", | |
783 | io_u->file->file_name, | |
784 | io_u->offset + hdr_num * hdr_len, hdr_len); | |
16758b59 JA |
785 | |
786 | if (td->o.verify_dump) | |
787 | dump_buf(p, hdr_len, io_u->offset + hdr_num * hdr_len, | |
788 | "hdr_fail", io_u->file); | |
789 | ||
5964842c | 790 | return EILSEQ; |
f65d1c26 JA |
791 | } |
792 | ||
f8b0bd10 | 793 | int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr) |
e29d1b70 | 794 | { |
3f9f4e26 | 795 | struct verify_header *hdr; |
f8b0bd10 | 796 | struct io_u *io_u = *io_u_ptr; |
2b13e716 | 797 | unsigned int header_size, hdr_inc, hdr_num = 0; |
95646108 | 798 | void *p; |
e29d1b70 JA |
799 | int ret; |
800 | ||
1dcc0498 | 801 | if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ) |
36690c9b | 802 | return 0; |
5c57c084 JA |
803 | /* |
804 | * If the IO engine is faking IO (like null), then just pretend | |
805 | * we verified everything. | |
806 | */ | |
9b87f09b | 807 | if (td_ioengine_flagged(td, FIO_FAKEIO)) |
5c57c084 JA |
808 | return 0; |
809 | ||
0d29de83 JA |
810 | if (io_u->flags & IO_U_F_TRIMMED) { |
811 | ret = verify_trimmed_io_u(td, io_u); | |
812 | goto done; | |
813 | } | |
36690c9b | 814 | |
cfbcd0dc | 815 | hdr_inc = get_hdr_inc(td, io_u); |
e29d1b70 | 816 | |
a12a3b4d | 817 | ret = 0; |
5ec10eaa JA |
818 | for (p = io_u->buf; p < io_u->buf + io_u->buflen; |
819 | p += hdr_inc, hdr_num++) { | |
bfacf389 JA |
820 | struct vcont vc = { |
821 | .io_u = io_u, | |
822 | .hdr_num = hdr_num, | |
7d9fb455 | 823 | .td = td, |
bfacf389 | 824 | }; |
f00b210f | 825 | unsigned int verify_type; |
936216f8 | 826 | |
f3e6cb95 | 827 | if (ret && td->o.verify_fatal) |
a12a3b4d | 828 | break; |
f3e6cb95 | 829 | |
2b13e716 | 830 | header_size = __hdr_size(td->o.verify); |
a59e170d | 831 | if (td->o.verify_offset) |
2b13e716 | 832 | memswp(p, p + td->o.verify_offset, header_size); |
95646108 | 833 | hdr = p; |
e29d1b70 | 834 | |
c4b6117b PV |
835 | /* |
836 | * Make rand_seed check pass when have verifysort or | |
837 | * verify_backlog. | |
838 | */ | |
839 | if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG)) | |
840 | io_u->rand_seed = hdr->rand_seed; | |
841 | ||
59245381 | 842 | if (td->o.verify != VERIFY_PATTERN_NO_HDR) { |
b638d82f | 843 | ret = verify_header(io_u, td, hdr, hdr_num, hdr_inc); |
59245381 JA |
844 | if (ret) |
845 | return ret; | |
846 | } | |
3f199b01 | 847 | |
f00b210f JA |
848 | if (td->o.verify != VERIFY_NONE) |
849 | verify_type = td->o.verify; | |
850 | else | |
851 | verify_type = hdr->verify_type; | |
852 | ||
853 | switch (verify_type) { | |
b638d82f RP |
854 | case VERIFY_HDR_ONLY: |
855 | /* Header is always verified, check if pattern is left | |
856 | * for verification. */ | |
857 | if (td->o.verify_pattern_bytes) | |
858 | ret = verify_io_u_pattern(hdr, &vc); | |
859 | break; | |
3f9f4e26 | 860 | case VERIFY_MD5: |
936216f8 | 861 | ret = verify_io_u_md5(hdr, &vc); |
3f9f4e26 SL |
862 | break; |
863 | case VERIFY_CRC64: | |
936216f8 | 864 | ret = verify_io_u_crc64(hdr, &vc); |
3f9f4e26 | 865 | break; |
bac39e0e | 866 | case VERIFY_CRC32C: |
3845591f | 867 | case VERIFY_CRC32C_INTEL: |
936216f8 | 868 | ret = verify_io_u_crc32c(hdr, &vc); |
bac39e0e | 869 | break; |
3f9f4e26 | 870 | case VERIFY_CRC32: |
936216f8 | 871 | ret = verify_io_u_crc32(hdr, &vc); |
3f9f4e26 SL |
872 | break; |
873 | case VERIFY_CRC16: | |
936216f8 | 874 | ret = verify_io_u_crc16(hdr, &vc); |
3f9f4e26 SL |
875 | break; |
876 | case VERIFY_CRC7: | |
936216f8 | 877 | ret = verify_io_u_crc7(hdr, &vc); |
3f9f4e26 | 878 | break; |
cd14cc10 | 879 | case VERIFY_SHA256: |
936216f8 | 880 | ret = verify_io_u_sha256(hdr, &vc); |
cd14cc10 JA |
881 | break; |
882 | case VERIFY_SHA512: | |
936216f8 | 883 | ret = verify_io_u_sha512(hdr, &vc); |
cd14cc10 | 884 | break; |
844ea602 JA |
885 | case VERIFY_XXHASH: |
886 | ret = verify_io_u_xxhash(hdr, &vc); | |
887 | break; | |
7c353ceb | 888 | case VERIFY_SHA1: |
936216f8 | 889 | ret = verify_io_u_sha1(hdr, &vc); |
7c353ceb | 890 | break; |
92bf48d5 | 891 | case VERIFY_PATTERN: |
59245381 | 892 | case VERIFY_PATTERN_NO_HDR: |
92bf48d5 JA |
893 | ret = verify_io_u_pattern(hdr, &vc); |
894 | break; | |
3f9f4e26 SL |
895 | default: |
896 | log_err("Bad verify type %u\n", hdr->verify_type); | |
d16d4e09 | 897 | ret = EINVAL; |
3f9f4e26 | 898 | } |
f00b210f JA |
899 | |
900 | if (ret && verify_type != hdr->verify_type) | |
901 | log_err("fio: verify type mismatch (%u media, %u given)\n", | |
902 | hdr->verify_type, verify_type); | |
3f9f4e26 | 903 | } |
a7dfe862 | 904 | |
0d29de83 | 905 | done: |
f3e6cb95 | 906 | if (ret && td->o.verify_fatal) |
ebea2133 | 907 | fio_mark_td_terminate(td); |
f3e6cb95 | 908 | |
a12a3b4d | 909 | return ret; |
e29d1b70 JA |
910 | } |
911 | ||
844ea602 JA |
912 | static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len) |
913 | { | |
914 | struct vhdr_xxhash *vh = hdr_priv(hdr); | |
915 | void *state; | |
916 | ||
917 | state = XXH32_init(1); | |
918 | XXH32_update(state, p, len); | |
919 | vh->hash = XXH32_digest(state); | |
920 | } | |
921 | ||
cd14cc10 JA |
922 | static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len) |
923 | { | |
546dfd9f | 924 | struct vhdr_sha512 *vh = hdr_priv(hdr); |
25dfa848 | 925 | struct fio_sha512_ctx sha512_ctx = { |
546dfd9f | 926 | .buf = vh->sha512, |
cd14cc10 JA |
927 | }; |
928 | ||
25dfa848 JA |
929 | fio_sha512_init(&sha512_ctx); |
930 | fio_sha512_update(&sha512_ctx, p, len); | |
cd14cc10 JA |
931 | } |
932 | ||
933 | static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len) | |
934 | { | |
546dfd9f | 935 | struct vhdr_sha256 *vh = hdr_priv(hdr); |
25dfa848 | 936 | struct fio_sha256_ctx sha256_ctx = { |
546dfd9f | 937 | .buf = vh->sha256, |
cd14cc10 JA |
938 | }; |
939 | ||
25dfa848 JA |
940 | fio_sha256_init(&sha256_ctx); |
941 | fio_sha256_update(&sha256_ctx, p, len); | |
017052b6 | 942 | fio_sha256_final(&sha256_ctx); |
cd14cc10 JA |
943 | } |
944 | ||
7c353ceb JA |
945 | static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len) |
946 | { | |
947 | struct vhdr_sha1 *vh = hdr_priv(hdr); | |
25dfa848 | 948 | struct fio_sha1_ctx sha1_ctx = { |
7c353ceb JA |
949 | .H = vh->sha1, |
950 | }; | |
951 | ||
25dfa848 JA |
952 | fio_sha1_init(&sha1_ctx); |
953 | fio_sha1_update(&sha1_ctx, p, len); | |
017052b6 | 954 | fio_sha1_final(&sha1_ctx); |
7c353ceb JA |
955 | } |
956 | ||
1e154bdb JA |
957 | static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len) |
958 | { | |
546dfd9f JA |
959 | struct vhdr_crc7 *vh = hdr_priv(hdr); |
960 | ||
25dfa848 | 961 | vh->crc7 = fio_crc7(p, len); |
1e154bdb JA |
962 | } |
963 | ||
969f7ed3 JA |
964 | static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len) |
965 | { | |
546dfd9f JA |
966 | struct vhdr_crc16 *vh = hdr_priv(hdr); |
967 | ||
25dfa848 | 968 | vh->crc16 = fio_crc16(p, len); |
969f7ed3 JA |
969 | } |
970 | ||
e29d1b70 JA |
971 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
972 | { | |
546dfd9f JA |
973 | struct vhdr_crc32 *vh = hdr_priv(hdr); |
974 | ||
25dfa848 | 975 | vh->crc32 = fio_crc32(p, len); |
e29d1b70 JA |
976 | } |
977 | ||
bac39e0e JA |
978 | static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len) |
979 | { | |
980 | struct vhdr_crc32 *vh = hdr_priv(hdr); | |
981 | ||
25dfa848 | 982 | vh->crc32 = fio_crc32c(p, len); |
bac39e0e JA |
983 | } |
984 | ||
d77a7af3 JA |
985 | static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len) |
986 | { | |
546dfd9f JA |
987 | struct vhdr_crc64 *vh = hdr_priv(hdr); |
988 | ||
25dfa848 | 989 | vh->crc64 = fio_crc64(p, len); |
d77a7af3 JA |
990 | } |
991 | ||
e29d1b70 JA |
992 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
993 | { | |
546dfd9f | 994 | struct vhdr_md5 *vh = hdr_priv(hdr); |
25dfa848 | 995 | struct fio_md5_ctx md5_ctx = { |
546dfd9f | 996 | .hash = (uint32_t *) vh->md5_digest, |
8c432325 | 997 | }; |
e29d1b70 | 998 | |
25dfa848 JA |
999 | fio_md5_init(&md5_ctx); |
1000 | fio_md5_update(&md5_ctx, p, len); | |
017052b6 | 1001 | fio_md5_final(&md5_ctx); |
e29d1b70 JA |
1002 | } |
1003 | ||
b638d82f RP |
1004 | static void __fill_hdr(struct thread_data *td, struct io_u *io_u, |
1005 | struct verify_header *hdr, unsigned int header_num, | |
1006 | unsigned int header_len, uint64_t rand_seed) | |
9ba987cc JA |
1007 | { |
1008 | void *p = hdr; | |
1009 | ||
1010 | hdr->magic = FIO_HDR_MAGIC; | |
b638d82f RP |
1011 | hdr->verify_type = td->o.verify; |
1012 | hdr->len = header_len; | |
9ba987cc | 1013 | hdr->rand_seed = rand_seed; |
b638d82f RP |
1014 | hdr->offset = io_u->offset + header_num * td->o.verify_interval; |
1015 | hdr->time_sec = io_u->start_time.tv_sec; | |
1016 | hdr->time_usec = io_u->start_time.tv_usec; | |
1017 | hdr->thread = td->thread_number; | |
1018 | hdr->numberio = io_u->numberio; | |
9ba987cc JA |
1019 | hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32)); |
1020 | } | |
1021 | ||
ed3a433d | 1022 | |
b638d82f RP |
1023 | static void fill_hdr(struct thread_data *td, struct io_u *io_u, |
1024 | struct verify_header *hdr, unsigned int header_num, | |
1025 | unsigned int header_len, uint64_t rand_seed) | |
ed3a433d | 1026 | { |
b638d82f RP |
1027 | |
1028 | if (td->o.verify != VERIFY_PATTERN_NO_HDR) | |
1029 | __fill_hdr(td, io_u, hdr, header_num, header_len, rand_seed); | |
ed3a433d JA |
1030 | } |
1031 | ||
7d9fb455 JA |
1032 | static void populate_hdr(struct thread_data *td, struct io_u *io_u, |
1033 | struct verify_header *hdr, unsigned int header_num, | |
1034 | unsigned int header_len) | |
1035 | { | |
1036 | unsigned int data_len; | |
1037 | void *data, *p; | |
1038 | ||
1039 | p = (void *) hdr; | |
1040 | ||
b638d82f | 1041 | fill_hdr(td, io_u, hdr, header_num, header_len, io_u->rand_seed); |
f65d1c26 | 1042 | |
ed3a433d | 1043 | data_len = header_len - hdr_size(td, hdr); |
7d9fb455 | 1044 | |
ed3a433d | 1045 | data = p + hdr_size(td, hdr); |
7d9fb455 JA |
1046 | switch (td->o.verify) { |
1047 | case VERIFY_MD5: | |
1048 | dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n", | |
1049 | io_u, hdr->len); | |
1050 | fill_md5(hdr, data, data_len); | |
1051 | break; | |
1052 | case VERIFY_CRC64: | |
1053 | dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n", | |
1054 | io_u, hdr->len); | |
1055 | fill_crc64(hdr, data, data_len); | |
1056 | break; | |
1057 | case VERIFY_CRC32C: | |
1058 | case VERIFY_CRC32C_INTEL: | |
1059 | dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n", | |
1060 | io_u, hdr->len); | |
1061 | fill_crc32c(hdr, data, data_len); | |
1062 | break; | |
1063 | case VERIFY_CRC32: | |
1064 | dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n", | |
1065 | io_u, hdr->len); | |
1066 | fill_crc32(hdr, data, data_len); | |
1067 | break; | |
1068 | case VERIFY_CRC16: | |
1069 | dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n", | |
1070 | io_u, hdr->len); | |
1071 | fill_crc16(hdr, data, data_len); | |
1072 | break; | |
1073 | case VERIFY_CRC7: | |
1074 | dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n", | |
1075 | io_u, hdr->len); | |
1076 | fill_crc7(hdr, data, data_len); | |
1077 | break; | |
1078 | case VERIFY_SHA256: | |
1079 | dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n", | |
1080 | io_u, hdr->len); | |
1081 | fill_sha256(hdr, data, data_len); | |
1082 | break; | |
1083 | case VERIFY_SHA512: | |
1084 | dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n", | |
1085 | io_u, hdr->len); | |
1086 | fill_sha512(hdr, data, data_len); | |
1087 | break; | |
844ea602 JA |
1088 | case VERIFY_XXHASH: |
1089 | dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n", | |
1090 | io_u, hdr->len); | |
1091 | fill_xxhash(hdr, data, data_len); | |
1092 | break; | |
7d9fb455 JA |
1093 | case VERIFY_SHA1: |
1094 | dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n", | |
1095 | io_u, hdr->len); | |
1096 | fill_sha1(hdr, data, data_len); | |
1097 | break; | |
b638d82f | 1098 | case VERIFY_HDR_ONLY: |
92bf48d5 | 1099 | case VERIFY_PATTERN: |
ed3a433d | 1100 | case VERIFY_PATTERN_NO_HDR: |
92bf48d5 JA |
1101 | /* nothing to do here */ |
1102 | break; | |
7d9fb455 JA |
1103 | default: |
1104 | log_err("fio: bad verify type: %d\n", td->o.verify); | |
1105 | assert(0); | |
1106 | } | |
ed3a433d JA |
1107 | |
1108 | if (td->o.verify_offset && hdr_size(td, hdr)) | |
1109 | memswp(p, p + td->o.verify_offset, hdr_size(td, hdr)); | |
7d9fb455 JA |
1110 | } |
1111 | ||
e29d1b70 JA |
1112 | /* |
1113 | * fill body of io_u->buf with random data and add a header with the | |
c50ca7bd | 1114 | * checksum of choice |
e29d1b70 JA |
1115 | */ |
1116 | void populate_verify_io_u(struct thread_data *td, struct io_u *io_u) | |
1117 | { | |
9cc3d150 JA |
1118 | if (td->o.verify == VERIFY_NULL) |
1119 | return; | |
1120 | ||
da0a7bd2 JC |
1121 | io_u->numberio = td->io_issues[io_u->ddir]; |
1122 | ||
c50ca7bd | 1123 | fill_pattern_headers(td, io_u, 0, 0); |
e29d1b70 JA |
1124 | } |
1125 | ||
1126 | int get_next_verify(struct thread_data *td, struct io_u *io_u) | |
1127 | { | |
8de8f047 | 1128 | struct io_piece *ipo = NULL; |
e29d1b70 | 1129 | |
d2d7fa53 JA |
1130 | /* |
1131 | * this io_u is from a requeue, we already filled the offsets | |
1132 | */ | |
1133 | if (io_u->file) | |
1134 | return 0; | |
1135 | ||
8de8f047 JA |
1136 | if (!RB_EMPTY_ROOT(&td->io_hist_tree)) { |
1137 | struct rb_node *n = rb_first(&td->io_hist_tree); | |
e29d1b70 | 1138 | |
8de8f047 | 1139 | ipo = rb_entry(n, struct io_piece, rb_node); |
f9401285 JA |
1140 | |
1141 | /* | |
1142 | * Ensure that the associated IO has completed | |
1143 | */ | |
1144 | read_barrier(); | |
1145 | if (ipo->flags & IP_F_IN_FLIGHT) | |
1146 | goto nothing; | |
1147 | ||
4b87898e | 1148 | rb_erase(n, &td->io_hist_tree); |
a917a8b3 JA |
1149 | assert(ipo->flags & IP_F_ONRB); |
1150 | ipo->flags &= ~IP_F_ONRB; | |
01743ee1 | 1151 | } else if (!flist_empty(&td->io_hist_list)) { |
9342d5f8 | 1152 | ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list); |
f9401285 JA |
1153 | |
1154 | /* | |
1155 | * Ensure that the associated IO has completed | |
1156 | */ | |
1157 | read_barrier(); | |
1158 | if (ipo->flags & IP_F_IN_FLIGHT) | |
1159 | goto nothing; | |
1160 | ||
01743ee1 | 1161 | flist_del(&ipo->list); |
a917a8b3 JA |
1162 | assert(ipo->flags & IP_F_ONLIST); |
1163 | ipo->flags &= ~IP_F_ONLIST; | |
8de8f047 | 1164 | } |
e29d1b70 | 1165 | |
8de8f047 | 1166 | if (ipo) { |
0d29de83 JA |
1167 | td->io_hist_len--; |
1168 | ||
e29d1b70 JA |
1169 | io_u->offset = ipo->offset; |
1170 | io_u->buflen = ipo->len; | |
da0a7bd2 | 1171 | io_u->numberio = ipo->numberio; |
36167d82 | 1172 | io_u->file = ipo->file; |
1651e431 | 1173 | io_u_set(td, io_u, IO_U_F_VER_LIST); |
97af62ce | 1174 | |
0d29de83 | 1175 | if (ipo->flags & IP_F_TRIMMED) |
1651e431 | 1176 | io_u_set(td, io_u, IO_U_F_TRIMMED); |
0d29de83 | 1177 | |
d6aed795 | 1178 | if (!fio_file_open(io_u->file)) { |
97af62ce JA |
1179 | int r = td_io_open_file(td, io_u->file); |
1180 | ||
bd6f78b2 JA |
1181 | if (r) { |
1182 | dprint(FD_VERIFY, "failed file %s open\n", | |
1183 | io_u->file->file_name); | |
97af62ce | 1184 | return 1; |
bd6f78b2 | 1185 | } |
97af62ce JA |
1186 | } |
1187 | ||
1188 | get_file(ipo->file); | |
d6aed795 | 1189 | assert(fio_file_open(io_u->file)); |
e29d1b70 | 1190 | io_u->ddir = DDIR_READ; |
36167d82 JA |
1191 | io_u->xfer_buf = io_u->buf; |
1192 | io_u->xfer_buflen = io_u->buflen; | |
0d29de83 JA |
1193 | |
1194 | remove_trim_entry(td, ipo); | |
e29d1b70 | 1195 | free(ipo); |
bd6f78b2 | 1196 | dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u); |
c4b6117b PV |
1197 | |
1198 | if (!td->o.verify_pattern_bytes) { | |
d6b72507 | 1199 | io_u->rand_seed = __rand(&td->verify_state); |
c4b6117b | 1200 | if (sizeof(int) != sizeof(long *)) |
d6b72507 | 1201 | io_u->rand_seed *= __rand(&td->verify_state); |
c4b6117b | 1202 | } |
e29d1b70 JA |
1203 | return 0; |
1204 | } | |
1205 | ||
f9401285 | 1206 | nothing: |
bd6f78b2 | 1207 | dprint(FD_VERIFY, "get_next_verify: empty\n"); |
e29d1b70 JA |
1208 | return 1; |
1209 | } | |
e8462bd8 | 1210 | |
dc5bfbb2 JA |
1211 | void fio_verify_init(struct thread_data *td) |
1212 | { | |
1213 | if (td->o.verify == VERIFY_CRC32C_INTEL || | |
214e2d56 | 1214 | td->o.verify == VERIFY_CRC32C_ARM64 || |
dc5bfbb2 | 1215 | td->o.verify == VERIFY_CRC32C) { |
214e2d56 | 1216 | crc32c_arm64_probe(); |
dc5bfbb2 JA |
1217 | crc32c_intel_probe(); |
1218 | } | |
1219 | } | |
1220 | ||
e8462bd8 JA |
1221 | static void *verify_async_thread(void *data) |
1222 | { | |
1223 | struct thread_data *td = data; | |
1224 | struct io_u *io_u; | |
1225 | int ret = 0; | |
1226 | ||
b2a9e649 | 1227 | if (fio_option_is_set(&td->o, verify_cpumask) && |
e8462bd8 JA |
1228 | fio_setaffinity(td->pid, td->o.verify_cpumask)) { |
1229 | log_err("fio: failed setting verify thread affinity\n"); | |
1230 | goto done; | |
1231 | } | |
1232 | ||
1233 | do { | |
e53ab27c JA |
1234 | FLIST_HEAD(list); |
1235 | ||
e8462bd8 JA |
1236 | read_barrier(); |
1237 | if (td->verify_thread_exit) | |
1238 | break; | |
1239 | ||
1240 | pthread_mutex_lock(&td->io_u_lock); | |
1241 | ||
1242 | while (flist_empty(&td->verify_list) && | |
1243 | !td->verify_thread_exit) { | |
b36e298b JA |
1244 | ret = pthread_cond_wait(&td->verify_cond, |
1245 | &td->io_u_lock); | |
e8462bd8 JA |
1246 | if (ret) { |
1247 | pthread_mutex_unlock(&td->io_u_lock); | |
1248 | break; | |
1249 | } | |
1250 | } | |
1251 | ||
e53ab27c JA |
1252 | flist_splice_init(&td->verify_list, &list); |
1253 | pthread_mutex_unlock(&td->io_u_lock); | |
1254 | ||
1255 | if (flist_empty(&list)) | |
e8462bd8 | 1256 | continue; |
e8462bd8 | 1257 | |
e53ab27c | 1258 | while (!flist_empty(&list)) { |
9342d5f8 | 1259 | io_u = flist_first_entry(&list, struct io_u, verify_list); |
f8b0bd10 JA |
1260 | flist_del_init(&io_u->verify_list); |
1261 | ||
1651e431 | 1262 | io_u_set(td, io_u, IO_U_F_NO_FILE_PUT); |
f8b0bd10 | 1263 | ret = verify_io_u(td, &io_u); |
e8462bd8 | 1264 | |
e53ab27c | 1265 | put_io_u(td, io_u); |
d561f2ab JA |
1266 | if (!ret) |
1267 | continue; | |
8b28bd41 | 1268 | if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) { |
d561f2ab JA |
1269 | update_error_count(td, ret); |
1270 | td_clear_error(td); | |
1271 | ret = 0; | |
1272 | } | |
e53ab27c | 1273 | } |
e8462bd8 JA |
1274 | } while (!ret); |
1275 | ||
d561f2ab JA |
1276 | if (ret) { |
1277 | td_verror(td, ret, "async_verify"); | |
f3e6cb95 | 1278 | if (td->o.verify_fatal) |
ebea2133 | 1279 | fio_mark_td_terminate(td); |
d561f2ab JA |
1280 | } |
1281 | ||
e8462bd8 JA |
1282 | done: |
1283 | pthread_mutex_lock(&td->io_u_lock); | |
1284 | td->nr_verify_threads--; | |
1285 | pthread_mutex_unlock(&td->io_u_lock); | |
1286 | ||
1287 | pthread_cond_signal(&td->free_cond); | |
1288 | return NULL; | |
1289 | } | |
1290 | ||
1291 | int verify_async_init(struct thread_data *td) | |
1292 | { | |
1293 | int i, ret; | |
304a47c7 VA |
1294 | pthread_attr_t attr; |
1295 | ||
1296 | pthread_attr_init(&attr); | |
45213f1b | 1297 | pthread_attr_setstacksize(&attr, 2 * PTHREAD_STACK_MIN); |
e8462bd8 JA |
1298 | |
1299 | td->verify_thread_exit = 0; | |
1300 | ||
1301 | td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async); | |
1302 | for (i = 0; i < td->o.verify_async; i++) { | |
304a47c7 | 1303 | ret = pthread_create(&td->verify_threads[i], &attr, |
e8462bd8 JA |
1304 | verify_async_thread, td); |
1305 | if (ret) { | |
1306 | log_err("fio: async verify creation failed: %s\n", | |
1307 | strerror(ret)); | |
1308 | break; | |
1309 | } | |
1310 | ret = pthread_detach(td->verify_threads[i]); | |
1311 | if (ret) { | |
1312 | log_err("fio: async verify thread detach failed: %s\n", | |
1313 | strerror(ret)); | |
1314 | break; | |
1315 | } | |
1316 | td->nr_verify_threads++; | |
1317 | } | |
1318 | ||
304a47c7 VA |
1319 | pthread_attr_destroy(&attr); |
1320 | ||
e8462bd8 | 1321 | if (i != td->o.verify_async) { |
e40823b1 | 1322 | log_err("fio: only %d verify threads started, exiting\n", i); |
e8462bd8 JA |
1323 | td->verify_thread_exit = 1; |
1324 | write_barrier(); | |
1325 | pthread_cond_broadcast(&td->verify_cond); | |
1326 | return 1; | |
1327 | } | |
1328 | ||
1329 | return 0; | |
1330 | } | |
1331 | ||
1332 | void verify_async_exit(struct thread_data *td) | |
1333 | { | |
1334 | td->verify_thread_exit = 1; | |
1335 | write_barrier(); | |
1336 | pthread_cond_broadcast(&td->verify_cond); | |
1337 | ||
1338 | pthread_mutex_lock(&td->io_u_lock); | |
1339 | ||
1340 | while (td->nr_verify_threads) | |
1341 | pthread_cond_wait(&td->free_cond, &td->io_u_lock); | |
1342 | ||
1343 | pthread_mutex_unlock(&td->io_u_lock); | |
1344 | free(td->verify_threads); | |
1345 | td->verify_threads = NULL; | |
1346 | } | |
ca09be4b | 1347 | |
61b9861d RP |
1348 | int paste_blockoff(char *buf, unsigned int len, void *priv) |
1349 | { | |
1350 | struct io_u *io = priv; | |
1351 | unsigned long long off; | |
1352 | ||
1353 | typecheck(typeof(off), io->offset); | |
1354 | off = cpu_to_le64((uint64_t)io->offset); | |
1355 | len = min(len, (unsigned int)sizeof(off)); | |
1356 | memcpy(buf, &off, len); | |
1357 | return 0; | |
1358 | } | |
1359 | ||
94a6e1bb JA |
1360 | static int __fill_file_completions(struct thread_data *td, |
1361 | struct thread_io_list *s, | |
1362 | struct fio_file *f, unsigned int *index) | |
1363 | { | |
1364 | unsigned int comps; | |
1365 | int i, j; | |
1366 | ||
1367 | if (!f->last_write_comp) | |
1368 | return 0; | |
1369 | ||
1370 | if (td->io_blocks[DDIR_WRITE] < td->o.iodepth) | |
1371 | comps = td->io_blocks[DDIR_WRITE]; | |
1372 | else | |
1373 | comps = td->o.iodepth; | |
1374 | ||
1375 | j = f->last_write_idx - 1; | |
1376 | for (i = 0; i < comps; i++) { | |
1377 | if (j == -1) | |
1378 | j = td->o.iodepth - 1; | |
1379 | s->comps[*index].fileno = __cpu_to_le64(f->fileno); | |
1380 | s->comps[*index].offset = cpu_to_le64(f->last_write_comp[j]); | |
1381 | (*index)++; | |
1382 | j--; | |
1383 | } | |
1384 | ||
1385 | return comps; | |
1386 | } | |
1387 | ||
1388 | static int fill_file_completions(struct thread_data *td, | |
1389 | struct thread_io_list *s, unsigned int *index) | |
1390 | { | |
1391 | struct fio_file *f; | |
1392 | unsigned int i; | |
1393 | int comps = 0; | |
1394 | ||
1395 | for_each_file(td, f, i) | |
1396 | comps += __fill_file_completions(td, s, f, index); | |
1397 | ||
1398 | return comps; | |
1399 | } | |
1400 | ||
ca09be4b JA |
1401 | struct all_io_list *get_all_io_list(int save_mask, size_t *sz) |
1402 | { | |
1403 | struct all_io_list *rep; | |
1404 | struct thread_data *td; | |
1405 | size_t depth; | |
1406 | void *next; | |
1407 | int i, nr; | |
1408 | ||
1409 | compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list"); | |
1410 | ||
1411 | /* | |
1412 | * Calculate reply space needed. We need one 'io_state' per thread, | |
1413 | * and the size will vary depending on depth. | |
1414 | */ | |
1415 | depth = 0; | |
1416 | nr = 0; | |
1417 | for_each_td(td, i) { | |
1418 | if (save_mask != IO_LIST_ALL && (i + 1) != save_mask) | |
1419 | continue; | |
1420 | td->stop_io = 1; | |
1421 | td->flags |= TD_F_VSTATE_SAVED; | |
94a6e1bb | 1422 | depth += (td->o.iodepth * td->o.nr_files); |
ca09be4b JA |
1423 | nr++; |
1424 | } | |
1425 | ||
1426 | if (!nr) | |
1427 | return NULL; | |
1428 | ||
1429 | *sz = sizeof(*rep); | |
1430 | *sz += nr * sizeof(struct thread_io_list); | |
94a6e1bb | 1431 | *sz += depth * sizeof(struct file_comp); |
ca09be4b | 1432 | rep = malloc(*sz); |
c68ce5ea | 1433 | memset(rep, 0, *sz); |
ca09be4b JA |
1434 | |
1435 | rep->threads = cpu_to_le64((uint64_t) nr); | |
1436 | ||
1437 | next = &rep->state[0]; | |
1438 | for_each_td(td, i) { | |
1439 | struct thread_io_list *s = next; | |
94a6e1bb | 1440 | unsigned int comps, index = 0; |
ca09be4b JA |
1441 | |
1442 | if (save_mask != IO_LIST_ALL && (i + 1) != save_mask) | |
1443 | continue; | |
1444 | ||
94a6e1bb | 1445 | comps = fill_file_completions(td, s, &index); |
ca09be4b JA |
1446 | |
1447 | s->no_comps = cpu_to_le64((uint64_t) comps); | |
1448 | s->depth = cpu_to_le64((uint64_t) td->o.iodepth); | |
94a6e1bb | 1449 | s->nofiles = cpu_to_le64((uint64_t) td->o.nr_files); |
ca09be4b JA |
1450 | s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]); |
1451 | s->index = cpu_to_le64((uint64_t) i); | |
c3546b53 JA |
1452 | if (td->random_state.use64) { |
1453 | s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1); | |
1454 | s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2); | |
1455 | s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3); | |
1456 | s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4); | |
1457 | s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5); | |
1458 | s->rand.state64.s[5] = 0; | |
1459 | s->rand.use64 = cpu_to_le64((uint64_t)1); | |
1460 | } else { | |
1461 | s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1); | |
1462 | s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2); | |
1463 | s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3); | |
1464 | s->rand.state32.s[3] = 0; | |
1465 | s->rand.use64 = 0; | |
1466 | } | |
1dc06a8f JA |
1467 | s->name[sizeof(s->name) - 1] = '\0'; |
1468 | strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1); | |
ca09be4b JA |
1469 | next = io_list_next(s); |
1470 | } | |
1471 | ||
1472 | return rep; | |
1473 | } | |
1474 | ||
1475 | static int open_state_file(const char *name, const char *prefix, int num, | |
1476 | int for_write) | |
1477 | { | |
d117b2bd | 1478 | char out[PATH_MAX]; |
ca09be4b JA |
1479 | int flags; |
1480 | int fd; | |
1481 | ||
1482 | if (for_write) | |
1483 | flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC; | |
1484 | else | |
1485 | flags = O_RDONLY; | |
1486 | ||
e499aedc | 1487 | verify_state_gen_name(out, sizeof(out), name, prefix, num); |
ca09be4b JA |
1488 | |
1489 | fd = open(out, flags, 0644); | |
1490 | if (fd == -1) { | |
1491 | perror("fio: open state file"); | |
d117b2bd | 1492 | log_err("fio: state file: %s (for_write=%d)\n", out, for_write); |
ca09be4b JA |
1493 | return -1; |
1494 | } | |
1495 | ||
1496 | return fd; | |
1497 | } | |
1498 | ||
1499 | static int write_thread_list_state(struct thread_io_list *s, | |
1500 | const char *prefix) | |
1501 | { | |
1502 | struct verify_state_hdr hdr; | |
1503 | uint64_t crc; | |
1504 | ssize_t ret; | |
1505 | int fd; | |
1506 | ||
1507 | fd = open_state_file((const char *) s->name, prefix, s->index, 1); | |
1508 | if (fd == -1) | |
1509 | return 1; | |
1510 | ||
1511 | crc = fio_crc32c((void *)s, thread_io_list_sz(s)); | |
1512 | ||
1513 | hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION); | |
1514 | hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s)); | |
1515 | hdr.crc = cpu_to_le64(crc); | |
1516 | ret = write(fd, &hdr, sizeof(hdr)); | |
1517 | if (ret != sizeof(hdr)) | |
1518 | goto write_fail; | |
1519 | ||
1520 | ret = write(fd, s, thread_io_list_sz(s)); | |
1521 | if (ret != thread_io_list_sz(s)) { | |
1522 | write_fail: | |
1523 | if (ret < 0) | |
1524 | perror("fio: write state file"); | |
1525 | log_err("fio: failed to write state file\n"); | |
1526 | ret = 1; | |
1527 | } else | |
1528 | ret = 0; | |
1529 | ||
1530 | close(fd); | |
1531 | return ret; | |
1532 | } | |
1533 | ||
1534 | void __verify_save_state(struct all_io_list *state, const char *prefix) | |
1535 | { | |
1536 | struct thread_io_list *s = &state->state[0]; | |
1537 | unsigned int i; | |
1538 | ||
1539 | for (i = 0; i < le64_to_cpu(state->threads); i++) { | |
1540 | write_thread_list_state(s, prefix); | |
1541 | s = io_list_next(s); | |
1542 | } | |
1543 | } | |
1544 | ||
d264264a | 1545 | void verify_save_state(int mask) |
ca09be4b JA |
1546 | { |
1547 | struct all_io_list *state; | |
1548 | size_t sz; | |
1549 | ||
d264264a | 1550 | state = get_all_io_list(mask, &sz); |
ca09be4b | 1551 | if (state) { |
d264264a JA |
1552 | char prefix[PATH_MAX]; |
1553 | ||
1554 | if (aux_path) | |
c6d140fb | 1555 | sprintf(prefix, "%s%slocal", aux_path, FIO_OS_PATH_SEPARATOR); |
d264264a | 1556 | else |
c6d140fb | 1557 | strcpy(prefix, "local"); |
d264264a JA |
1558 | |
1559 | __verify_save_state(state, prefix); | |
ca09be4b JA |
1560 | free(state); |
1561 | } | |
1562 | } | |
1563 | ||
1564 | void verify_free_state(struct thread_data *td) | |
1565 | { | |
1566 | if (td->vstate) | |
1567 | free(td->vstate); | |
1568 | } | |
1569 | ||
94a6e1bb | 1570 | void verify_assign_state(struct thread_data *td, void *p) |
ca09be4b | 1571 | { |
94a6e1bb | 1572 | struct thread_io_list *s = p; |
ca09be4b JA |
1573 | int i; |
1574 | ||
94a6e1bb JA |
1575 | s->no_comps = le64_to_cpu(s->no_comps); |
1576 | s->depth = le32_to_cpu(s->depth); | |
1577 | s->nofiles = le32_to_cpu(s->nofiles); | |
1578 | s->numberio = le64_to_cpu(s->numberio); | |
1579 | s->rand.use64 = le64_to_cpu(s->rand.use64); | |
c3546b53 | 1580 | |
94a6e1bb JA |
1581 | if (s->rand.use64) { |
1582 | for (i = 0; i < 6; i++) | |
1583 | s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]); | |
c3546b53 | 1584 | } else { |
94a6e1bb JA |
1585 | for (i = 0; i < 4; i++) |
1586 | s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]); | |
1587 | } | |
c3546b53 | 1588 | |
94a6e1bb JA |
1589 | for (i = 0; i < s->no_comps; i++) { |
1590 | s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno); | |
1591 | s->comps[i].offset = le64_to_cpu(s->comps[i].offset); | |
c3546b53 | 1592 | } |
ca09be4b | 1593 | |
94a6e1bb | 1594 | td->vstate = p; |
ca09be4b JA |
1595 | } |
1596 | ||
94a6e1bb | 1597 | int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s) |
ca09be4b JA |
1598 | { |
1599 | uint64_t crc; | |
1600 | ||
1601 | hdr->version = le64_to_cpu(hdr->version); | |
1602 | hdr->size = le64_to_cpu(hdr->size); | |
1603 | hdr->crc = le64_to_cpu(hdr->crc); | |
1604 | ||
94a6e1bb | 1605 | if (hdr->version != VSTATE_HDR_VERSION) |
ca09be4b JA |
1606 | return 1; |
1607 | ||
1608 | crc = fio_crc32c((void *)s, hdr->size); | |
1609 | if (crc != hdr->crc) | |
1610 | return 1; | |
1611 | ||
1612 | return 0; | |
1613 | } | |
1614 | ||
1615 | int verify_load_state(struct thread_data *td, const char *prefix) | |
1616 | { | |
ca09be4b | 1617 | struct verify_state_hdr hdr; |
c3546b53 | 1618 | void *s = NULL; |
ca09be4b JA |
1619 | uint64_t crc; |
1620 | ssize_t ret; | |
1621 | int fd; | |
1622 | ||
1623 | if (!td->o.verify_state) | |
1624 | return 0; | |
1625 | ||
1626 | fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0); | |
1627 | if (fd == -1) | |
1628 | return 1; | |
1629 | ||
1630 | ret = read(fd, &hdr, sizeof(hdr)); | |
1631 | if (ret != sizeof(hdr)) { | |
1632 | if (ret < 0) | |
1633 | td_verror(td, errno, "read verify state hdr"); | |
1634 | log_err("fio: failed reading verify state header\n"); | |
1635 | goto err; | |
1636 | } | |
1637 | ||
1638 | hdr.version = le64_to_cpu(hdr.version); | |
1639 | hdr.size = le64_to_cpu(hdr.size); | |
1640 | hdr.crc = le64_to_cpu(hdr.crc); | |
1641 | ||
94a6e1bb JA |
1642 | if (hdr.version != VSTATE_HDR_VERSION) { |
1643 | log_err("fio: unsupported (%d) version in verify state header\n", | |
1644 | (unsigned int) hdr.version); | |
ca09be4b JA |
1645 | goto err; |
1646 | } | |
1647 | ||
1648 | s = malloc(hdr.size); | |
1649 | ret = read(fd, s, hdr.size); | |
1650 | if (ret != hdr.size) { | |
1651 | if (ret < 0) | |
1652 | td_verror(td, errno, "read verify state"); | |
1653 | log_err("fio: failed reading verity state\n"); | |
1654 | goto err; | |
1655 | } | |
1656 | ||
c3546b53 | 1657 | crc = fio_crc32c(s, hdr.size); |
ca09be4b JA |
1658 | if (crc != hdr.crc) { |
1659 | log_err("fio: verify state is corrupt\n"); | |
1660 | goto err; | |
1661 | } | |
1662 | ||
1663 | close(fd); | |
1664 | ||
94a6e1bb | 1665 | verify_assign_state(td, s); |
ca09be4b JA |
1666 | return 0; |
1667 | err: | |
1668 | if (s) | |
1669 | free(s); | |
1670 | close(fd); | |
1671 | return 1; | |
1672 | } | |
1673 | ||
1674 | /* | |
1675 | * Use the loaded verify state to know when to stop doing verification | |
1676 | */ | |
1677 | int verify_state_should_stop(struct thread_data *td, struct io_u *io_u) | |
1678 | { | |
1679 | struct thread_io_list *s = td->vstate; | |
94a6e1bb | 1680 | struct fio_file *f = io_u->file; |
ca09be4b JA |
1681 | int i; |
1682 | ||
94a6e1bb | 1683 | if (!s || !f) |
ca09be4b JA |
1684 | return 0; |
1685 | ||
1686 | /* | |
def00095 JA |
1687 | * If we're not into the window of issues - depth yet, continue. If |
1688 | * issue is shorter than depth, do check. | |
ca09be4b | 1689 | */ |
def00095 JA |
1690 | if ((td->io_blocks[DDIR_READ] < s->depth || |
1691 | s->numberio - td->io_blocks[DDIR_READ] > s->depth) && | |
1692 | s->numberio > s->depth) | |
ca09be4b JA |
1693 | return 0; |
1694 | ||
1695 | /* | |
1696 | * We're in the window of having to check if this io was | |
1697 | * completed or not. If the IO was seen as completed, then | |
1698 | * lets verify it. | |
1699 | */ | |
94a6e1bb JA |
1700 | for (i = 0; i < s->no_comps; i++) { |
1701 | if (s->comps[i].fileno != f->fileno) | |
1702 | continue; | |
1703 | if (io_u->offset == s->comps[i].offset) | |
ca09be4b | 1704 | return 0; |
94a6e1bb | 1705 | } |
ca09be4b JA |
1706 | |
1707 | /* | |
1708 | * Not found, we have to stop | |
1709 | */ | |
1710 | return 1; | |
1711 | } |