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