blob: c1791fc69743c08d40fbdaebdda122545b93037c [file] [log] [blame]
Jens Axboee29d1b72006-10-18 15:43:15 +02001/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
Jens Axboe97af62c2007-05-22 11:12:13 +02007#include <assert.h>
Jens Axboee8462bd2009-07-06 12:59:04 +02008#include <pthread.h>
Jens Axboe79402a12011-01-14 12:41:15 +01009#include <libgen.h>
Jens Axboee29d1b72006-10-18 15:43:15 +020010
11#include "fio.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020012#include "verify.h"
Jens Axboe0d29de82010-09-01 13:54:15 +020013#include "trim.h"
Jens Axboe637ef8d2010-06-21 20:39:38 +020014#include "lib/rand.h"
Jens Axboe51aa2da2013-01-21 10:55:02 -070015#include "lib/hweight.h"
Jens Axboee29d1b72006-10-18 15:43:15 +020016
Jens Axboeeef6eea2007-07-30 12:27:58 +020017#include "crc/md5.h"
18#include "crc/crc64.h"
19#include "crc/crc32.h"
Jens Axboebac39e02008-06-11 20:46:19 +020020#include "crc/crc32c.h"
Jens Axboeeef6eea2007-07-30 12:27:58 +020021#include "crc/crc16.h"
22#include "crc/crc7.h"
23#include "crc/sha256.h"
24#include "crc/sha512.h"
Jens Axboe7c353ce2009-08-09 22:40:33 +020025#include "crc/sha1.h"
Jens Axboe844ea602014-02-20 13:21:45 -080026#include "crc/xxhash.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020027
Jens Axboe7d9fb452011-01-11 22:16:49 +010028static void populate_hdr(struct thread_data *td, struct io_u *io_u,
29 struct verify_header *hdr, unsigned int header_num,
30 unsigned int header_len);
31
Jens Axboece35b1e2014-01-14 15:35:58 -070032void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
33{
Jens Axboed18c0412014-12-03 19:25:13 -070034 fill_pattern(p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
Jens Axboece35b1e2014-01-14 15:35:58 -070035}
36
37void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
38 struct io_u *io_u, unsigned long seed, int use_seed)
39{
40 if (!td->o.verify_pattern_bytes) {
41 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
42
43 if (use_seed)
44 __fill_random_buf(p, len, seed);
45 else
Jens Axboef6787012014-11-05 18:39:23 -070046 io_u->rand_seed = fill_random_buf(&td->verify_state, p, len);
Jens Axboece35b1e2014-01-14 15:35:58 -070047 return;
48 }
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -070049
Jens Axboece35b1e2014-01-14 15:35:58 -070050 if (io_u->buf_filled_len >= len) {
51 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
52 td->o.verify_pattern_bytes, len);
53 return;
54 }
55
Jens Axboed18c0412014-12-03 19:25:13 -070056 fill_pattern(p, len, td->o.verify_pattern, td->o.verify_pattern_bytes);
Jens Axboece35b1e2014-01-14 15:35:58 -070057
58 io_u->buf_filled_len = len;
59}
60
Jens Axboecfbcd0d2011-01-14 14:49:20 +010061static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
62{
63 unsigned int hdr_inc;
64
65 hdr_inc = io_u->buflen;
Jens Axboe8a99fdf2012-03-06 19:24:49 +010066 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
Jens Axboecfbcd0d2011-01-14 14:49:20 +010067 hdr_inc = td->o.verify_interval;
68
69 return hdr_inc;
70}
71
Jens Axboec50ca7b2011-01-12 08:31:54 +010072static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
73 unsigned long seed, int use_seed)
74{
75 unsigned int hdr_inc, header_num;
76 struct verify_header *hdr;
77 void *p = io_u->buf;
78
Jens Axboece35b1e2014-01-14 15:35:58 -070079 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
Jens Axboec50ca7b2011-01-12 08:31:54 +010080
Jens Axboecfbcd0d2011-01-14 14:49:20 +010081 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboec50ca7b2011-01-12 08:31:54 +010082 header_num = 0;
83 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
84 hdr = p;
85 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
86 header_num++;
87 }
88}
89
Jens Axboe4764aec2007-08-23 09:03:38 +020090static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020091{
Shawn Lewisdee6de72007-08-02 22:17:52 +020092 char swap[200];
93
94 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020095
Shawn Lewis546a9142007-07-28 21:11:37 +020096 memcpy(&swap, buf1, len);
97 memcpy(buf1, buf2, len);
98 memcpy(buf2, &swap, len);
99}
100
Jens Axboee29d1b72006-10-18 15:43:15 +0200101static void hexdump(void *buffer, int len)
102{
103 unsigned char *p = buffer;
104 int i;
105
106 for (i = 0; i < len; i++)
Jens Axboebfacf382010-06-18 14:29:52 +0200107 log_err("%02x", p[i]);
108 log_err("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200109}
110
Jens Axboed9f2caf2007-07-28 21:22:03 +0200111/*
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700112 * Prepare for separation of verify_header and checksum header
Jens Axboe87677832007-07-30 12:08:14 +0200113 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200114static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200115{
Bruce Cran03e20d62011-01-02 20:14:54 +0100116 unsigned int len = 0;
Jens Axboe87677832007-07-30 12:08:14 +0200117
Jens Axboe546dfd92007-07-30 12:23:05 +0200118 switch (verify_type) {
119 case VERIFY_NONE:
120 case VERIFY_NULL:
121 len = 0;
122 break;
123 case VERIFY_MD5:
124 len = sizeof(struct vhdr_md5);
125 break;
126 case VERIFY_CRC64:
127 len = sizeof(struct vhdr_crc64);
128 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200129 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200130 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200131 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200132 len = sizeof(struct vhdr_crc32);
133 break;
134 case VERIFY_CRC16:
135 len = sizeof(struct vhdr_crc16);
136 break;
137 case VERIFY_CRC7:
138 len = sizeof(struct vhdr_crc7);
139 break;
140 case VERIFY_SHA256:
141 len = sizeof(struct vhdr_sha256);
142 break;
143 case VERIFY_SHA512:
144 len = sizeof(struct vhdr_sha512);
145 break;
Jens Axboe844ea602014-02-20 13:21:45 -0800146 case VERIFY_XXHASH:
147 len = sizeof(struct vhdr_xxhash);
148 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200149 case VERIFY_META:
150 len = sizeof(struct vhdr_meta);
151 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200152 case VERIFY_SHA1:
153 len = sizeof(struct vhdr_sha1);
154 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100155 case VERIFY_PATTERN:
156 len = 0;
157 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200158 default:
159 log_err("fio: unknown verify header!\n");
160 assert(0);
161 }
162
163 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200164}
165
166static inline unsigned int hdr_size(struct verify_header *hdr)
167{
Jens Axboe546dfd92007-07-30 12:23:05 +0200168 return __hdr_size(hdr->verify_type);
169}
170
171static void *hdr_priv(struct verify_header *hdr)
172{
173 void *priv = hdr;
174
175 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200176}
177
178/*
Jens Axboe936216f2010-06-18 13:44:11 +0200179 * Verify container, pass info to verify handlers and allow them to
180 * pass info back in case of error
181 */
182struct vcont {
183 /*
184 * Input
185 */
186 struct io_u *io_u;
187 unsigned int hdr_num;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100188 struct thread_data *td;
Jens Axboe936216f2010-06-18 13:44:11 +0200189
190 /*
191 * Output, only valid in case of error
192 */
Jens Axboebfacf382010-06-18 14:29:52 +0200193 const char *name;
194 void *good_crc;
195 void *bad_crc;
Jens Axboe936216f2010-06-18 13:44:11 +0200196 unsigned int crc_len;
197};
198
Jens Axboedacbbb82014-04-14 08:43:55 -0600199#define DUMP_BUF_SZ 255
200static int dump_buf_warned;
201
Jens Axboec50ca7b2011-01-12 08:31:54 +0100202static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
203 const char *type, struct fio_file *f)
Jens Axboe7d9fb452011-01-11 22:16:49 +0100204{
Jens Axboedacbbb82014-04-14 08:43:55 -0600205 char *ptr, fname[DUMP_BUF_SZ];
206 size_t buf_left = DUMP_BUF_SZ;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100207 int ret, fd;
208
Jens Axboe6f260b32011-01-13 18:57:54 +0100209 ptr = strdup(f->file_name);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100210
Jens Axboedacbbb82014-04-14 08:43:55 -0600211 fname[DUMP_BUF_SZ - 1] = '\0';
212 strncpy(fname, basename(ptr), DUMP_BUF_SZ - 1);
213
214 buf_left -= strlen(fname);
215 if (buf_left <= 0) {
216 if (!dump_buf_warned) {
217 log_err("fio: verify failure dump buffer too small\n");
218 dump_buf_warned = 1;
219 }
220 free(ptr);
221 return;
222 }
223
224 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100225
226 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
227 if (fd < 0) {
228 perror("open verify buf file");
229 return;
230 }
231
232 while (len) {
233 ret = write(fd, buf, len);
234 if (!ret)
235 break;
236 else if (ret < 0) {
237 perror("write verify buf file");
238 break;
239 }
240 len -= ret;
241 buf += ret;
242 }
243
244 close(fd);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100245 log_err(" %s data dumped as %s\n", type, fname);
Jens Axboe6f260b32011-01-13 18:57:54 +0100246 free(ptr);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100247}
248
Jens Axboec50ca7b2011-01-12 08:31:54 +0100249/*
250 * Dump the contents of the read block and re-generate the correct data
251 * and dump that too.
252 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100253static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
254{
255 struct thread_data *td = vc->td;
256 struct io_u *io_u = vc->io_u;
257 unsigned long hdr_offset;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100258 struct io_u dummy;
Jens Axboec50ca7b2011-01-12 08:31:54 +0100259 void *buf;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100260
Steven Lang0dce9bc2011-10-25 22:41:05 +0200261 if (!td->o.verify_dump)
262 return;
263
Jens Axboec50ca7b2011-01-12 08:31:54 +0100264 /*
265 * Dump the contents we just read off disk
266 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100267 hdr_offset = vc->hdr_num * hdr->len;
268
Jens Axboec50ca7b2011-01-12 08:31:54 +0100269 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
270 "received", vc->io_u->file);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100271
Jens Axboec50ca7b2011-01-12 08:31:54 +0100272 /*
273 * Allocate a new buf and re-generate the original data
274 */
275 buf = malloc(io_u->buflen);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100276 dummy = *io_u;
Jens Axboec50ca7b2011-01-12 08:31:54 +0100277 dummy.buf = buf;
Jens Axboe4aae38a2011-01-12 08:59:12 +0100278 dummy.rand_seed = hdr->rand_seed;
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100279 dummy.buf_filled_len = 0;
Josef Bacik0d81daf2013-07-08 20:35:01 -0400280 dummy.buflen = io_u->buflen;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100281
Jens Axboec50ca7b2011-01-12 08:31:54 +0100282 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100283
Jens Axboec50ca7b2011-01-12 08:31:54 +0100284 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
285 "expected", vc->io_u->file);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100286 free(buf);
287}
288
Jens Axboebfacf382010-06-18 14:29:52 +0200289static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
290{
291 unsigned long long offset;
292
293 offset = vc->io_u->offset;
294 offset += vc->hdr_num * hdr->len;
Jens Axboe32c17ad2010-06-18 14:32:21 +0200295 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
296 vc->name, vc->io_u->file->file_name, offset, hdr->len);
Jens Axboebfacf382010-06-18 14:29:52 +0200297
298 if (vc->good_crc && vc->bad_crc) {
299 log_err(" Expected CRC: ");
300 hexdump(vc->good_crc, vc->crc_len);
301 log_err(" Received CRC: ");
302 hexdump(vc->bad_crc, vc->crc_len);
303 }
Jens Axboe7d9fb452011-01-11 22:16:49 +0100304
305 dump_verify_buffers(hdr, vc);
Jens Axboebfacf382010-06-18 14:29:52 +0200306}
307
Jens Axboe936216f2010-06-18 13:44:11 +0200308/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200309 * Return data area 'header_num'
310 */
Jens Axboe936216f2010-06-18 13:44:11 +0200311static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200312{
Jens Axboe936216f2010-06-18 13:44:11 +0200313 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200314}
315
Jens Axboe92bf48d2011-01-14 21:20:42 +0100316static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
317{
318 struct thread_data *td = vc->td;
319 struct io_u *io_u = vc->io_u;
320 char *buf, *pattern;
Jens Axboe2b13e712011-01-19 14:04:16 -0700321 unsigned int header_size = __hdr_size(td->o.verify);
Steven Lang9a2a86d2012-02-07 09:42:59 +0100322 unsigned int len, mod, i, size, pattern_size;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100323
324 pattern = td->o.verify_pattern;
Steven Lang9a2a86d2012-02-07 09:42:59 +0100325 pattern_size = td->o.verify_pattern_bytes;
326 if (pattern_size <= 1)
327 pattern_size = MAX_PATTERN_SIZE;
Jens Axboe2b13e712011-01-19 14:04:16 -0700328 buf = (void *) hdr + header_size;
329 len = get_hdr_inc(td, io_u) - header_size;
Steven Lang9a2a86d2012-02-07 09:42:59 +0100330 mod = header_size % pattern_size;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100331
Steven Lang9a2a86d2012-02-07 09:42:59 +0100332 for (i = 0; i < len; i += size) {
333 size = pattern_size - mod;
334 if (size > (len - i))
335 size = len - i;
336 if (memcmp(buf + i, pattern + mod, size))
Jens Axboee4ad68b2012-02-23 08:35:04 +0100337 /* Let the slow compare find the first mismatch byte. */
Steven Lang9a2a86d2012-02-07 09:42:59 +0100338 break;
339 mod = 0;
340 }
341
342 for (; i < len; i++) {
Jens Axboe92bf48d2011-01-14 21:20:42 +0100343 if (buf[i] != pattern[mod]) {
344 unsigned int bits;
345
346 bits = hweight8(buf[i] ^ pattern[mod]);
347 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
348 buf[i], pattern[mod], bits);
349 log_err("fio: bad pattern block offset %u\n", i);
350 dump_verify_buffers(hdr, vc);
351 return EILSEQ;
352 }
353 mod++;
354 if (mod == td->o.verify_pattern_bytes)
355 mod = 0;
356 }
357
358 return 0;
359}
360
361static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
362{
363 struct thread_data *td = vc->td;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200364 struct vhdr_meta *vh = hdr_priv(hdr);
Jens Axboe936216f2010-06-18 13:44:11 +0200365 struct io_u *io_u = vc->io_u;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100366 int ret = EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200367
Jens Axboebd6f78b2008-02-01 20:27:52 +0100368 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
369
Jens Axboebfacf382010-06-18 14:29:52 +0200370 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
Jens Axboe92bf48d2011-01-14 21:20:42 +0100371 ret = 0;
372
373 if (td->o.verify_pattern_bytes)
374 ret |= verify_io_u_pattern(hdr, vc);
375
Juan Casseda0a7bd2013-09-17 14:06:12 -0700376 /*
377 * For read-only workloads, the program cannot be certain of the
Jens Axboe984c8692014-07-21 11:31:49 +0200378 * last numberio written to a block. Checking of numberio will be
379 * done only for workloads that write data. For verify_only,
380 * numberio will be checked in the last iteration when the correct
381 * state of numberio, that would have been written to each block
382 * in a previous run of fio, has been reached.
Juan Casseda0a7bd2013-09-17 14:06:12 -0700383 */
Jens Axboe1fc351b2014-08-07 15:27:31 -0600384 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
385 !td->o.time_based)
Juan Casse62167762013-09-17 14:06:13 -0700386 if (!td->o.verify_only || td->o.loops == 0)
387 if (vh->numberio != io_u->numberio)
388 ret = EILSEQ;
Juan Casseda0a7bd2013-09-17 14:06:12 -0700389
Jens Axboe92bf48d2011-01-14 21:20:42 +0100390 if (!ret)
Jens Axboebfacf382010-06-18 14:29:52 +0200391 return 0;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200392
Jens Axboebfacf382010-06-18 14:29:52 +0200393 vc->name = "meta";
394 log_verify_failure(hdr, vc);
Jens Axboe92bf48d2011-01-14 21:20:42 +0100395 return ret;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200396}
397
Jens Axboe844ea602014-02-20 13:21:45 -0800398static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
399{
400 void *p = io_u_verify_off(hdr, vc);
401 struct vhdr_xxhash *vh = hdr_priv(hdr);
402 uint32_t hash;
403 void *state;
404
405 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
406
407 state = XXH32_init(1);
408 XXH32_update(state, p, hdr->len - hdr_size(hdr));
409 hash = XXH32_digest(state);
410
411 if (vh->hash == hash)
412 return 0;
413
414 vc->name = "xxhash";
415 vc->good_crc = &vh->hash;
416 vc->bad_crc = &hash;
417 vc->crc_len = sizeof(hash);
418 log_verify_failure(hdr, vc);
419 return EILSEQ;
420}
421
Jens Axboe936216f2010-06-18 13:44:11 +0200422static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200423{
Jens Axboe936216f2010-06-18 13:44:11 +0200424 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200425 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200426 uint8_t sha512[128];
Jens Axboe25dfa842012-02-29 10:01:34 +0100427 struct fio_sha512_ctx sha512_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200428 .buf = sha512,
429 };
430
Jens Axboe936216f2010-06-18 13:44:11 +0200431 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100432
Jens Axboe25dfa842012-02-29 10:01:34 +0100433 fio_sha512_init(&sha512_ctx);
434 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200435
Jens Axboebfacf382010-06-18 14:29:52 +0200436 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
437 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200438
Jens Axboebfacf382010-06-18 14:29:52 +0200439 vc->name = "sha512";
440 vc->good_crc = vh->sha512;
441 vc->bad_crc = sha512_ctx.buf;
442 vc->crc_len = sizeof(vh->sha512);
443 log_verify_failure(hdr, vc);
444 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200445}
446
Jens Axboe936216f2010-06-18 13:44:11 +0200447static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200448{
Jens Axboe936216f2010-06-18 13:44:11 +0200449 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200450 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100451 uint8_t sha256[64];
Jens Axboe25dfa842012-02-29 10:01:34 +0100452 struct fio_sha256_ctx sha256_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200453 .buf = sha256,
454 };
455
Jens Axboe936216f2010-06-18 13:44:11 +0200456 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100457
Jens Axboe25dfa842012-02-29 10:01:34 +0100458 fio_sha256_init(&sha256_ctx);
459 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200460
Jens Axboebfacf382010-06-18 14:29:52 +0200461 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
462 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200463
Jens Axboebfacf382010-06-18 14:29:52 +0200464 vc->name = "sha256";
465 vc->good_crc = vh->sha256;
466 vc->bad_crc = sha256_ctx.buf;
467 vc->crc_len = sizeof(vh->sha256);
468 log_verify_failure(hdr, vc);
469 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200470}
471
Jens Axboe936216f2010-06-18 13:44:11 +0200472static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200473{
Jens Axboe936216f2010-06-18 13:44:11 +0200474 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200475 struct vhdr_sha1 *vh = hdr_priv(hdr);
476 uint32_t sha1[5];
Jens Axboe25dfa842012-02-29 10:01:34 +0100477 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200478 .H = sha1,
479 };
480
Jens Axboe936216f2010-06-18 13:44:11 +0200481 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200482
Jens Axboe25dfa842012-02-29 10:01:34 +0100483 fio_sha1_init(&sha1_ctx);
484 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboe7c353ce2009-08-09 22:40:33 +0200485
Jens Axboebfacf382010-06-18 14:29:52 +0200486 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
487 return 0;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200488
Jens Axboebfacf382010-06-18 14:29:52 +0200489 vc->name = "sha1";
490 vc->good_crc = vh->sha1;
491 vc->bad_crc = sha1_ctx.H;
492 vc->crc_len = sizeof(vh->sha1);
493 log_verify_failure(hdr, vc);
494 return EILSEQ;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200495}
496
Jens Axboe936216f2010-06-18 13:44:11 +0200497static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200498{
Jens Axboe936216f2010-06-18 13:44:11 +0200499 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200500 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200501 unsigned char c;
502
Jens Axboe936216f2010-06-18 13:44:11 +0200503 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100504
Jens Axboe25dfa842012-02-29 10:01:34 +0100505 c = fio_crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200506
Jens Axboebfacf382010-06-18 14:29:52 +0200507 if (c == vh->crc7)
508 return 0;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200509
Jens Axboebfacf382010-06-18 14:29:52 +0200510 vc->name = "crc7";
511 vc->good_crc = &vh->crc7;
512 vc->bad_crc = &c;
513 vc->crc_len = 1;
514 log_verify_failure(hdr, vc);
515 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200516}
517
Jens Axboe936216f2010-06-18 13:44:11 +0200518static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200519{
Jens Axboe936216f2010-06-18 13:44:11 +0200520 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200521 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200522 unsigned short c;
523
Jens Axboe936216f2010-06-18 13:44:11 +0200524 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100525
Jens Axboe25dfa842012-02-29 10:01:34 +0100526 c = fio_crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200527
Jens Axboebfacf382010-06-18 14:29:52 +0200528 if (c == vh->crc16)
529 return 0;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200530
Jens Axboebfacf382010-06-18 14:29:52 +0200531 vc->name = "crc16";
532 vc->good_crc = &vh->crc16;
533 vc->bad_crc = &c;
534 vc->crc_len = 2;
535 log_verify_failure(hdr, vc);
536 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200537}
538
Jens Axboe936216f2010-06-18 13:44:11 +0200539static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200540{
Jens Axboe936216f2010-06-18 13:44:11 +0200541 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200542 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200543 unsigned long long c;
544
Jens Axboe936216f2010-06-18 13:44:11 +0200545 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100546
Jens Axboe25dfa842012-02-29 10:01:34 +0100547 c = fio_crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200548
Jens Axboebfacf382010-06-18 14:29:52 +0200549 if (c == vh->crc64)
550 return 0;
Jens Axboed77a7af2007-07-27 15:35:06 +0200551
Jens Axboebfacf382010-06-18 14:29:52 +0200552 vc->name = "crc64";
553 vc->good_crc = &vh->crc64;
554 vc->bad_crc = &c;
555 vc->crc_len = 8;
556 log_verify_failure(hdr, vc);
557 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200558}
559
Jens Axboe936216f2010-06-18 13:44:11 +0200560static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200561{
Jens Axboe936216f2010-06-18 13:44:11 +0200562 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200563 struct vhdr_crc32 *vh = hdr_priv(hdr);
564 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200565
Jens Axboe936216f2010-06-18 13:44:11 +0200566 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100567
Jens Axboe25dfa842012-02-29 10:01:34 +0100568 c = fio_crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200569
Jens Axboebfacf382010-06-18 14:29:52 +0200570 if (c == vh->crc32)
571 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200572
Jens Axboebfacf382010-06-18 14:29:52 +0200573 vc->name = "crc32";
574 vc->good_crc = &vh->crc32;
575 vc->bad_crc = &c;
576 vc->crc_len = 4;
577 log_verify_failure(hdr, vc);
578 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200579}
580
Jens Axboe936216f2010-06-18 13:44:11 +0200581static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200582{
Jens Axboe936216f2010-06-18 13:44:11 +0200583 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200584 struct vhdr_crc32 *vh = hdr_priv(hdr);
585 uint32_t c;
586
Jens Axboe936216f2010-06-18 13:44:11 +0200587 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200588
Jens Axboe25dfa842012-02-29 10:01:34 +0100589 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200590
Jens Axboebfacf382010-06-18 14:29:52 +0200591 if (c == vh->crc32)
592 return 0;
Jens Axboebac39e02008-06-11 20:46:19 +0200593
Jens Axboebfacf382010-06-18 14:29:52 +0200594 vc->name = "crc32c";
595 vc->good_crc = &vh->crc32;
596 vc->bad_crc = &c;
597 vc->crc_len = 4;
598 log_verify_failure(hdr, vc);
599 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200600}
601
Jens Axboe936216f2010-06-18 13:44:11 +0200602static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200603{
Jens Axboe936216f2010-06-18 13:44:11 +0200604 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200605 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200606 uint32_t hash[MD5_HASH_WORDS];
Jens Axboe25dfa842012-02-29 10:01:34 +0100607 struct fio_md5_ctx md5_ctx = {
Jens Axboe8c432322007-07-27 13:16:24 +0200608 .hash = hash,
609 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200610
Jens Axboe936216f2010-06-18 13:44:11 +0200611 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100612
Jens Axboe25dfa842012-02-29 10:01:34 +0100613 fio_md5_init(&md5_ctx);
614 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200615
Jens Axboebfacf382010-06-18 14:29:52 +0200616 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
617 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200618
Jens Axboebfacf382010-06-18 14:29:52 +0200619 vc->name = "md5";
620 vc->good_crc = vh->md5_digest;
621 vc->bad_crc = md5_ctx.hash;
622 vc->crc_len = sizeof(hash);
623 log_verify_failure(hdr, vc);
624 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200625}
626
Jens Axboee8462bd2009-07-06 12:59:04 +0200627/*
628 * Push IO verification to a separate thread
629 */
Jens Axboee69fdf72014-07-23 16:11:43 +0200630int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
Jens Axboee8462bd2009-07-06 12:59:04 +0200631{
Jens Axboee69fdf72014-07-23 16:11:43 +0200632 struct io_u *io_u = *io_u_ptr;
Jens Axboee8462bd2009-07-06 12:59:04 +0200633
Jens Axboee8462bd2009-07-06 12:59:04 +0200634 pthread_mutex_lock(&td->io_u_lock);
Steven Langd7ee2a72011-10-26 09:46:50 +0200635
Jens Axboee69fdf72014-07-23 16:11:43 +0200636 if (io_u->file)
637 put_file_log(td, io_u->file);
638
Radha Ramachandran0c412142009-11-03 21:45:31 +0100639 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
640 td->cur_depth--;
641 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
642 }
Jens Axboe2ae0b202013-05-28 14:16:55 +0200643 flist_add_tail(&io_u->verify_list, &td->verify_list);
Jens Axboee69fdf72014-07-23 16:11:43 +0200644 *io_u_ptr = NULL;
Jens Axboee8462bd2009-07-06 12:59:04 +0200645 pthread_mutex_unlock(&td->io_u_lock);
646
647 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200648 return 0;
649}
650
Jens Axboe0d29de82010-09-01 13:54:15 +0200651static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
652{
653 static char zero_buf[1024];
654 unsigned int this_len, len;
655 int ret = 0;
656 void *p;
657
658 if (!td->o.trim_zero)
659 return 0;
660
661 len = io_u->buflen;
662 p = io_u->buf;
663 do {
664 this_len = sizeof(zero_buf);
665 if (this_len > len)
666 this_len = len;
667 if (memcmp(p, zero_buf, this_len)) {
668 ret = EILSEQ;
669 break;
670 }
671 len -= this_len;
672 p += this_len;
673 } while (len);
674
675 if (!ret)
676 return 0;
677
Jens Axboea917a8b2010-09-02 13:23:20 +0200678 log_err("trim: verify failed at file %s offset %llu, length %lu"
679 ", block offset %lu\n",
680 io_u->file->file_name, io_u->offset, io_u->buflen,
Jens Axboe2f681242010-10-21 08:15:59 +0200681 (unsigned long) (p - io_u->buf));
Jens Axboe0d29de82010-09-01 13:54:15 +0200682 return ret;
683}
684
Andreas Gruenbacherbf654152014-06-24 22:13:50 +0200685static int verify_header(struct io_u *io_u, struct verify_header *hdr,
686 unsigned int hdr_num, unsigned int hdr_len)
Jens Axboef65d1c22012-02-22 20:11:57 +0100687{
688 void *p = hdr;
689 uint32_t crc;
690
Andreas Gruenbacherbf654152014-06-24 22:13:50 +0200691 if (hdr->magic != FIO_HDR_MAGIC) {
692 log_err("verify: bad magic header %x, wanted %x",
693 hdr->magic, FIO_HDR_MAGIC);
694 goto err;
695 }
Andreas Gruenbacher45474192014-06-26 19:25:30 +0200696 if (hdr->len != hdr_len) {
Andreas Gruenbacherbf654152014-06-24 22:13:50 +0200697 log_err("verify: bad header length %u, wanted %u",
698 hdr->len, hdr_len);
699 goto err;
700 }
701 if (hdr->rand_seed != io_u->rand_seed) {
702 log_err("verify: bad header rand_seed %"PRIu64
703 ", wanted %"PRIu64,
704 hdr->rand_seed, io_u->rand_seed);
705 goto err;
706 }
Jens Axboeae38c0d2012-02-23 10:31:07 +0100707
Jens Axboe25dfa842012-02-29 10:01:34 +0100708 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
Andreas Gruenbacherbf654152014-06-24 22:13:50 +0200709 if (crc != hdr->crc32) {
710 log_err("verify: bad header crc %x, calculated %x",
711 hdr->crc32, crc);
712 goto err;
713 }
714 return 0;
715
716err:
717 log_err(" at file %s offset %llu, length %u\n",
718 io_u->file->file_name,
719 io_u->offset + hdr_num * hdr_len, hdr_len);
720 return EILSEQ;
Jens Axboef65d1c22012-02-22 20:11:57 +0100721}
722
Jens Axboee69fdf72014-07-23 16:11:43 +0200723int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
Jens Axboee29d1b72006-10-18 15:43:15 +0200724{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200725 struct verify_header *hdr;
Jens Axboee69fdf72014-07-23 16:11:43 +0200726 struct io_u *io_u = *io_u_ptr;
Jens Axboe2b13e712011-01-19 14:04:16 -0700727 unsigned int header_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200728 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200729 int ret;
730
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200731 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200732 return 0;
Jens Axboed9570702014-07-23 16:19:48 +0200733 /*
734 * If the IO engine is faking IO (like null), then just pretend
735 * we verified everything.
736 */
737 if (td->io_ops->flags & FIO_FAKEIO)
738 return 0;
739
Jens Axboe0d29de82010-09-01 13:54:15 +0200740 if (io_u->flags & IO_U_F_TRIMMED) {
741 ret = verify_trimmed_io_u(td, io_u);
742 goto done;
743 }
Jens Axboe36690c92007-03-26 10:23:34 +0200744
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100745 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200746
Jens Axboea12a3b42007-08-09 10:20:54 +0200747 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100748 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
749 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200750 struct vcont vc = {
751 .io_u = io_u,
752 .hdr_num = hdr_num,
Jens Axboe7d9fb452011-01-11 22:16:49 +0100753 .td = td,
Jens Axboebfacf382010-06-18 14:29:52 +0200754 };
Jens Axboef00b2102012-11-30 09:39:02 +0100755 unsigned int verify_type;
Jens Axboe936216f2010-06-18 13:44:11 +0200756
Jens Axboef3e6cb92010-06-18 14:48:43 +0200757 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200758 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200759
Jens Axboe2b13e712011-01-19 14:04:16 -0700760 header_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200761 if (td->o.verify_offset)
Jens Axboe2b13e712011-01-19 14:04:16 -0700762 memswp(p, p + td->o.verify_offset, header_size);
Jens Axboe95646102007-07-28 21:17:50 +0200763 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200764
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -0700765 /*
766 * Make rand_seed check pass when have verifysort or
767 * verify_backlog.
768 */
769 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
770 io_u->rand_seed = hdr->rand_seed;
771
Andreas Gruenbacherbf654152014-06-24 22:13:50 +0200772 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
773 if (ret)
774 return ret;
Jens Axboe3f199b02007-08-09 10:16:31 +0200775
Jens Axboef00b2102012-11-30 09:39:02 +0100776 if (td->o.verify != VERIFY_NONE)
777 verify_type = td->o.verify;
778 else
779 verify_type = hdr->verify_type;
780
781 switch (verify_type) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200782 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200783 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200784 break;
785 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200786 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200787 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200788 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200789 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200790 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200791 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200792 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200793 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200794 break;
795 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200796 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200797 break;
798 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200799 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200800 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200801 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200802 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200803 break;
804 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200805 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200806 break;
Jens Axboe844ea602014-02-20 13:21:45 -0800807 case VERIFY_XXHASH:
808 ret = verify_io_u_xxhash(hdr, &vc);
809 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200810 case VERIFY_META:
Jens Axboe92bf48d2011-01-14 21:20:42 +0100811 ret = verify_io_u_meta(hdr, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200812 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200813 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200814 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200815 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100816 case VERIFY_PATTERN:
817 ret = verify_io_u_pattern(hdr, &vc);
818 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200819 default:
820 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200821 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200822 }
Jens Axboef00b2102012-11-30 09:39:02 +0100823
824 if (ret && verify_type != hdr->verify_type)
825 log_err("fio: verify type mismatch (%u media, %u given)\n",
826 hdr->verify_type, verify_type);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200827 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100828
Jens Axboe0d29de82010-09-01 13:54:15 +0200829done:
Jens Axboef3e6cb92010-06-18 14:48:43 +0200830 if (ret && td->o.verify_fatal)
Jens Axboe3b44e8b2014-07-09 11:24:12 +0200831 fio_mark_td_terminate(td);
Jens Axboef3e6cb92010-06-18 14:48:43 +0200832
Jens Axboea12a3b42007-08-09 10:20:54 +0200833 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200834}
835
Shawn Lewis7437ee82007-08-02 21:05:58 +0200836static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100837 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200838{
839 struct vhdr_meta *vh = hdr_priv(hdr);
840
841 vh->thread = td->thread_number;
842
843 vh->time_sec = io_u->start_time.tv_sec;
844 vh->time_usec = io_u->start_time.tv_usec;
845
Juan Casseda0a7bd2013-09-17 14:06:12 -0700846 vh->numberio = io_u->numberio;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200847
848 vh->offset = io_u->offset + header_num * td->o.verify_interval;
849}
850
Jens Axboe844ea602014-02-20 13:21:45 -0800851static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
852{
853 struct vhdr_xxhash *vh = hdr_priv(hdr);
854 void *state;
855
856 state = XXH32_init(1);
857 XXH32_update(state, p, len);
858 vh->hash = XXH32_digest(state);
859}
860
Jens Axboecd14cc12007-07-30 10:59:33 +0200861static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
862{
Jens Axboe546dfd92007-07-30 12:23:05 +0200863 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100864 struct fio_sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200865 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200866 };
867
Jens Axboe25dfa842012-02-29 10:01:34 +0100868 fio_sha512_init(&sha512_ctx);
869 fio_sha512_update(&sha512_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200870}
871
872static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
873{
Jens Axboe546dfd92007-07-30 12:23:05 +0200874 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100875 struct fio_sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200876 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200877 };
878
Jens Axboe25dfa842012-02-29 10:01:34 +0100879 fio_sha256_init(&sha256_ctx);
880 fio_sha256_update(&sha256_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200881}
882
Jens Axboe7c353ce2009-08-09 22:40:33 +0200883static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
884{
885 struct vhdr_sha1 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100886 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200887 .H = vh->sha1,
888 };
889
Jens Axboe25dfa842012-02-29 10:01:34 +0100890 fio_sha1_init(&sha1_ctx);
891 fio_sha1_update(&sha1_ctx, p, len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200892}
893
Jens Axboe1e154bd2007-07-27 09:52:40 +0200894static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
895{
Jens Axboe546dfd92007-07-30 12:23:05 +0200896 struct vhdr_crc7 *vh = hdr_priv(hdr);
897
Jens Axboe25dfa842012-02-29 10:01:34 +0100898 vh->crc7 = fio_crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200899}
900
Jens Axboe969f7ed2007-07-27 09:07:17 +0200901static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
902{
Jens Axboe546dfd92007-07-30 12:23:05 +0200903 struct vhdr_crc16 *vh = hdr_priv(hdr);
904
Jens Axboe25dfa842012-02-29 10:01:34 +0100905 vh->crc16 = fio_crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200906}
907
Jens Axboee29d1b72006-10-18 15:43:15 +0200908static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
909{
Jens Axboe546dfd92007-07-30 12:23:05 +0200910 struct vhdr_crc32 *vh = hdr_priv(hdr);
911
Jens Axboe25dfa842012-02-29 10:01:34 +0100912 vh->crc32 = fio_crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200913}
914
Jens Axboebac39e02008-06-11 20:46:19 +0200915static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
916{
917 struct vhdr_crc32 *vh = hdr_priv(hdr);
918
Jens Axboe25dfa842012-02-29 10:01:34 +0100919 vh->crc32 = fio_crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200920}
921
Jens Axboed77a7af2007-07-27 15:35:06 +0200922static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
923{
Jens Axboe546dfd92007-07-30 12:23:05 +0200924 struct vhdr_crc64 *vh = hdr_priv(hdr);
925
Jens Axboe25dfa842012-02-29 10:01:34 +0100926 vh->crc64 = fio_crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200927}
928
Jens Axboee29d1b72006-10-18 15:43:15 +0200929static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
930{
Jens Axboe546dfd92007-07-30 12:23:05 +0200931 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100932 struct fio_md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200933 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200934 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200935
Jens Axboe25dfa842012-02-29 10:01:34 +0100936 fio_md5_init(&md5_ctx);
937 fio_md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200938}
939
Jens Axboe7d9fb452011-01-11 22:16:49 +0100940static void populate_hdr(struct thread_data *td, struct io_u *io_u,
941 struct verify_header *hdr, unsigned int header_num,
942 unsigned int header_len)
943{
944 unsigned int data_len;
945 void *data, *p;
946
947 p = (void *) hdr;
948
Jens Axboed3a173a2012-02-23 08:23:18 +0100949 hdr->magic = FIO_HDR_MAGIC;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100950 hdr->verify_type = td->o.verify;
Jens Axboed3a173a2012-02-23 08:23:18 +0100951 hdr->len = header_len;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100952 hdr->rand_seed = io_u->rand_seed;
Jens Axboe25dfa842012-02-29 10:01:34 +0100953 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100954
Jens Axboe7d9fb452011-01-11 22:16:49 +0100955 data_len = header_len - hdr_size(hdr);
956
957 data = p + hdr_size(hdr);
958 switch (td->o.verify) {
959 case VERIFY_MD5:
960 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
961 io_u, hdr->len);
962 fill_md5(hdr, data, data_len);
963 break;
964 case VERIFY_CRC64:
965 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
966 io_u, hdr->len);
967 fill_crc64(hdr, data, data_len);
968 break;
969 case VERIFY_CRC32C:
970 case VERIFY_CRC32C_INTEL:
971 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
972 io_u, hdr->len);
973 fill_crc32c(hdr, data, data_len);
974 break;
975 case VERIFY_CRC32:
976 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
977 io_u, hdr->len);
978 fill_crc32(hdr, data, data_len);
979 break;
980 case VERIFY_CRC16:
981 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
982 io_u, hdr->len);
983 fill_crc16(hdr, data, data_len);
984 break;
985 case VERIFY_CRC7:
986 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
987 io_u, hdr->len);
988 fill_crc7(hdr, data, data_len);
989 break;
990 case VERIFY_SHA256:
991 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
992 io_u, hdr->len);
993 fill_sha256(hdr, data, data_len);
994 break;
995 case VERIFY_SHA512:
996 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
997 io_u, hdr->len);
998 fill_sha512(hdr, data, data_len);
999 break;
Jens Axboe844ea602014-02-20 13:21:45 -08001000 case VERIFY_XXHASH:
1001 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1002 io_u, hdr->len);
1003 fill_xxhash(hdr, data, data_len);
1004 break;
Jens Axboe7d9fb452011-01-11 22:16:49 +01001005 case VERIFY_META:
1006 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1007 io_u, hdr->len);
1008 fill_meta(hdr, td, io_u, header_num);
1009 break;
1010 case VERIFY_SHA1:
1011 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1012 io_u, hdr->len);
1013 fill_sha1(hdr, data, data_len);
1014 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +01001015 case VERIFY_PATTERN:
1016 /* nothing to do here */
1017 break;
Jens Axboe7d9fb452011-01-11 22:16:49 +01001018 default:
1019 log_err("fio: bad verify type: %d\n", td->o.verify);
1020 assert(0);
1021 }
1022 if (td->o.verify_offset)
1023 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
1024}
1025
Jens Axboee29d1b72006-10-18 15:43:15 +02001026/*
1027 * fill body of io_u->buf with random data and add a header with the
Jens Axboec50ca7b2011-01-12 08:31:54 +01001028 * checksum of choice
Jens Axboee29d1b72006-10-18 15:43:15 +02001029 */
1030void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1031{
Jens Axboe9cc3d152007-03-26 10:32:30 +02001032 if (td->o.verify == VERIFY_NULL)
1033 return;
1034
Juan Casseda0a7bd2013-09-17 14:06:12 -07001035 io_u->numberio = td->io_issues[io_u->ddir];
1036
Jens Axboec50ca7b2011-01-12 08:31:54 +01001037 fill_pattern_headers(td, io_u, 0, 0);
Jens Axboee29d1b72006-10-18 15:43:15 +02001038}
1039
1040int get_next_verify(struct thread_data *td, struct io_u *io_u)
1041{
Jens Axboe8de8f042007-03-27 10:36:12 +02001042 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +02001043
Jens Axboed2d7fa52007-02-19 13:21:35 +01001044 /*
1045 * this io_u is from a requeue, we already filled the offsets
1046 */
1047 if (io_u->file)
1048 return 0;
1049
Jens Axboe8de8f042007-03-27 10:36:12 +02001050 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1051 struct rb_node *n = rb_first(&td->io_hist_tree);
1052
Jens Axboe4b878982007-03-26 09:32:22 +02001053 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboef9401282014-02-06 12:17:37 -07001054
1055 /*
1056 * Ensure that the associated IO has completed
1057 */
1058 read_barrier();
1059 if (ipo->flags & IP_F_IN_FLIGHT)
1060 goto nothing;
1061
Jens Axboe4b878982007-03-26 09:32:22 +02001062 rb_erase(n, &td->io_hist_tree);
Jens Axboea917a8b2010-09-02 13:23:20 +02001063 assert(ipo->flags & IP_F_ONRB);
1064 ipo->flags &= ~IP_F_ONRB;
Jens Axboe01743ee2008-06-02 12:19:19 +02001065 } else if (!flist_empty(&td->io_hist_list)) {
Jens Axboe12dbd062014-07-03 21:19:57 -06001066 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
Jens Axboef9401282014-02-06 12:17:37 -07001067
1068 /*
1069 * Ensure that the associated IO has completed
1070 */
1071 read_barrier();
1072 if (ipo->flags & IP_F_IN_FLIGHT)
1073 goto nothing;
1074
Jens Axboe01743ee2008-06-02 12:19:19 +02001075 flist_del(&ipo->list);
Jens Axboea917a8b2010-09-02 13:23:20 +02001076 assert(ipo->flags & IP_F_ONLIST);
1077 ipo->flags &= ~IP_F_ONLIST;
Jens Axboe8de8f042007-03-27 10:36:12 +02001078 }
Jens Axboee29d1b72006-10-18 15:43:15 +02001079
Jens Axboe8de8f042007-03-27 10:36:12 +02001080 if (ipo) {
Jens Axboe0d29de82010-09-01 13:54:15 +02001081 td->io_hist_len--;
1082
Jens Axboee29d1b72006-10-18 15:43:15 +02001083 io_u->offset = ipo->offset;
1084 io_u->buflen = ipo->len;
Juan Casseda0a7bd2013-09-17 14:06:12 -07001085 io_u->numberio = ipo->numberio;
Jens Axboe36167d82007-02-18 05:41:31 +01001086 io_u->file = ipo->file;
Jens Axboe82af2a72012-03-13 13:45:58 +01001087 io_u->flags |= IO_U_F_VER_LIST;
Jens Axboe97af62c2007-05-22 11:12:13 +02001088
Jens Axboe0d29de82010-09-01 13:54:15 +02001089 if (ipo->flags & IP_F_TRIMMED)
1090 io_u->flags |= IO_U_F_TRIMMED;
1091
Jens Axboed6aed792009-06-03 08:41:15 +02001092 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +02001093 int r = td_io_open_file(td, io_u->file);
1094
Jens Axboebd6f78b2008-02-01 20:27:52 +01001095 if (r) {
1096 dprint(FD_VERIFY, "failed file %s open\n",
1097 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +02001098 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +01001099 }
Jens Axboe97af62c2007-05-22 11:12:13 +02001100 }
1101
1102 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +02001103 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +02001104 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +01001105 io_u->xfer_buf = io_u->buf;
1106 io_u->xfer_buflen = io_u->buflen;
Jens Axboe0d29de82010-09-01 13:54:15 +02001107
1108 remove_trim_entry(td, ipo);
Jens Axboee29d1b72006-10-18 15:43:15 +02001109 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001110 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -07001111
1112 if (!td->o.verify_pattern_bytes) {
Jens Axboef6787012014-11-05 18:39:23 -07001113 io_u->rand_seed = __rand(&td->verify_state);
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -07001114 if (sizeof(int) != sizeof(long *))
Jens Axboef6787012014-11-05 18:39:23 -07001115 io_u->rand_seed *= __rand(&td->verify_state);
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -07001116 }
Jens Axboee29d1b72006-10-18 15:43:15 +02001117 return 0;
1118 }
1119
Jens Axboef9401282014-02-06 12:17:37 -07001120nothing:
Jens Axboebd6f78b2008-02-01 20:27:52 +01001121 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +02001122 return 1;
1123}
Jens Axboee8462bd2009-07-06 12:59:04 +02001124
Jens Axboedc5bfbb2013-04-10 22:23:40 +02001125void fio_verify_init(struct thread_data *td)
1126{
1127 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1128 td->o.verify == VERIFY_CRC32C) {
1129 crc32c_intel_probe();
1130 }
1131}
1132
Jens Axboee8462bd2009-07-06 12:59:04 +02001133static void *verify_async_thread(void *data)
1134{
1135 struct thread_data *td = data;
1136 struct io_u *io_u;
1137 int ret = 0;
1138
1139 if (td->o.verify_cpumask_set &&
1140 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1141 log_err("fio: failed setting verify thread affinity\n");
1142 goto done;
1143 }
1144
1145 do {
Jens Axboee53ab272009-07-06 13:43:46 +02001146 FLIST_HEAD(list);
1147
Jens Axboee8462bd2009-07-06 12:59:04 +02001148 read_barrier();
1149 if (td->verify_thread_exit)
1150 break;
1151
1152 pthread_mutex_lock(&td->io_u_lock);
1153
1154 while (flist_empty(&td->verify_list) &&
1155 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +02001156 ret = pthread_cond_wait(&td->verify_cond,
1157 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +02001158 if (ret) {
1159 pthread_mutex_unlock(&td->io_u_lock);
1160 break;
1161 }
1162 }
1163
Jens Axboee53ab272009-07-06 13:43:46 +02001164 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001165 pthread_mutex_unlock(&td->io_u_lock);
1166
Jens Axboee53ab272009-07-06 13:43:46 +02001167 if (flist_empty(&list))
1168 continue;
1169
1170 while (!flist_empty(&list)) {
Jens Axboe12dbd062014-07-03 21:19:57 -06001171 io_u = flist_first_entry(&list, struct io_u, verify_list);
Jens Axboee69fdf72014-07-23 16:11:43 +02001172 flist_del_init(&io_u->verify_list);
Jens Axboee53ab272009-07-06 13:43:46 +02001173
Jens Axboee69fdf72014-07-23 16:11:43 +02001174 io_u->flags |= IO_U_F_NO_FILE_PUT;
1175 ret = verify_io_u(td, &io_u);
1176
Jens Axboee53ab272009-07-06 13:43:46 +02001177 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +02001178 if (!ret)
1179 continue;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001180 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
Jens Axboed561f2a2009-07-06 13:51:05 +02001181 update_error_count(td, ret);
1182 td_clear_error(td);
1183 ret = 0;
1184 }
Jens Axboee53ab272009-07-06 13:43:46 +02001185 }
Jens Axboee8462bd2009-07-06 12:59:04 +02001186 } while (!ret);
1187
Jens Axboed561f2a2009-07-06 13:51:05 +02001188 if (ret) {
1189 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +02001190 if (td->o.verify_fatal)
Jens Axboe3b44e8b2014-07-09 11:24:12 +02001191 fio_mark_td_terminate(td);
Jens Axboed561f2a2009-07-06 13:51:05 +02001192 }
1193
Jens Axboee8462bd2009-07-06 12:59:04 +02001194done:
1195 pthread_mutex_lock(&td->io_u_lock);
1196 td->nr_verify_threads--;
1197 pthread_mutex_unlock(&td->io_u_lock);
1198
1199 pthread_cond_signal(&td->free_cond);
1200 return NULL;
1201}
1202
1203int verify_async_init(struct thread_data *td)
1204{
1205 int i, ret;
bart Van Assche304a47c2010-08-01 21:31:26 +02001206 pthread_attr_t attr;
1207
1208 pthread_attr_init(&attr);
1209 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
Jens Axboee8462bd2009-07-06 12:59:04 +02001210
1211 td->verify_thread_exit = 0;
1212
1213 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1214 for (i = 0; i < td->o.verify_async; i++) {
bart Van Assche304a47c2010-08-01 21:31:26 +02001215 ret = pthread_create(&td->verify_threads[i], &attr,
Jens Axboee8462bd2009-07-06 12:59:04 +02001216 verify_async_thread, td);
1217 if (ret) {
1218 log_err("fio: async verify creation failed: %s\n",
1219 strerror(ret));
1220 break;
1221 }
1222 ret = pthread_detach(td->verify_threads[i]);
1223 if (ret) {
1224 log_err("fio: async verify thread detach failed: %s\n",
1225 strerror(ret));
1226 break;
1227 }
1228 td->nr_verify_threads++;
1229 }
1230
bart Van Assche304a47c2010-08-01 21:31:26 +02001231 pthread_attr_destroy(&attr);
1232
Jens Axboee8462bd2009-07-06 12:59:04 +02001233 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +02001234 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +02001235 td->verify_thread_exit = 1;
1236 write_barrier();
1237 pthread_cond_broadcast(&td->verify_cond);
1238 return 1;
1239 }
1240
1241 return 0;
1242}
1243
1244void verify_async_exit(struct thread_data *td)
1245{
1246 td->verify_thread_exit = 1;
1247 write_barrier();
1248 pthread_cond_broadcast(&td->verify_cond);
1249
1250 pthread_mutex_lock(&td->io_u_lock);
1251
1252 while (td->nr_verify_threads)
1253 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1254
1255 pthread_mutex_unlock(&td->io_u_lock);
1256 free(td->verify_threads);
1257 td->verify_threads = NULL;
1258}
Jens Axboede54cfd2014-11-10 20:34:00 -07001259
1260struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1261{
1262 struct all_io_list *rep;
1263 struct thread_data *td;
1264 size_t depth;
1265 void *next;
1266 int i, nr;
1267
1268 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1269
1270 /*
1271 * Calculate reply space needed. We need one 'io_state' per thread,
1272 * and the size will vary depending on depth.
1273 */
1274 depth = 0;
1275 nr = 0;
1276 for_each_td(td, i) {
1277 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1278 continue;
1279 td->stop_io = 1;
1280 td->flags |= TD_F_VSTATE_SAVED;
1281 depth += td->o.iodepth;
1282 nr++;
1283 }
1284
1285 if (!nr)
1286 return NULL;
1287
1288 *sz = sizeof(*rep);
1289 *sz += nr * sizeof(struct thread_io_list);
1290 *sz += depth * sizeof(uint64_t);
1291 rep = malloc(*sz);
1292
1293 rep->threads = cpu_to_le64((uint64_t) nr);
1294
1295 next = &rep->state[0];
1296 for_each_td(td, i) {
1297 struct thread_io_list *s = next;
1298 unsigned int comps;
1299
1300 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1301 continue;
1302
1303 if (td->last_write_comp) {
1304 int j, k;
1305
1306 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1307 comps = td->io_blocks[DDIR_WRITE];
1308 else
1309 comps = td->o.iodepth;
1310
1311 k = td->last_write_idx - 1;
1312 for (j = 0; j < comps; j++) {
1313 if (k == -1)
1314 k = td->o.iodepth - 1;
1315 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1316 k--;
1317 }
1318 } else
1319 comps = 0;
1320
1321 s->no_comps = cpu_to_le64((uint64_t) comps);
1322 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1323 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1324 s->index = cpu_to_le64((uint64_t) i);
1325 s->rand.s[0] = cpu_to_le32(td->random_state.s1);
1326 s->rand.s[1] = cpu_to_le32(td->random_state.s2);
1327 s->rand.s[2] = cpu_to_le32(td->random_state.s3);
1328 s->rand.s[3] = 0;
1329 strncpy((char *) s->name, td->o.name, sizeof(s->name));
1330 next = io_list_next(s);
1331 }
1332
1333 return rep;
1334}
1335
1336static int open_state_file(const char *name, const char *prefix, int num,
1337 int for_write)
1338{
1339 char out[64];
1340 int flags;
1341 int fd;
1342
1343 if (for_write)
1344 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1345 else
1346 flags = O_RDONLY;
1347
Jens Axboe3ebcfd52014-11-11 20:51:37 -07001348 verify_state_gen_name(out, sizeof(out), name, prefix, num);
Jens Axboede54cfd2014-11-10 20:34:00 -07001349
1350 fd = open(out, flags, 0644);
1351 if (fd == -1) {
1352 perror("fio: open state file");
1353 return -1;
1354 }
1355
1356 return fd;
1357}
1358
1359static int write_thread_list_state(struct thread_io_list *s,
1360 const char *prefix)
1361{
1362 struct verify_state_hdr hdr;
1363 uint64_t crc;
1364 ssize_t ret;
1365 int fd;
1366
1367 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1368 if (fd == -1)
1369 return 1;
1370
1371 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1372
1373 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1374 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1375 hdr.crc = cpu_to_le64(crc);
1376 ret = write(fd, &hdr, sizeof(hdr));
1377 if (ret != sizeof(hdr))
1378 goto write_fail;
1379
1380 ret = write(fd, s, thread_io_list_sz(s));
1381 if (ret != thread_io_list_sz(s)) {
1382write_fail:
1383 if (ret < 0)
1384 perror("fio: write state file");
1385 log_err("fio: failed to write state file\n");
1386 ret = 1;
1387 } else
1388 ret = 0;
1389
1390 close(fd);
1391 return ret;
1392}
1393
1394void __verify_save_state(struct all_io_list *state, const char *prefix)
1395{
1396 struct thread_io_list *s = &state->state[0];
1397 unsigned int i;
1398
1399 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1400 write_thread_list_state(s, prefix);
1401 s = io_list_next(s);
1402 }
1403}
1404
1405void verify_save_state(void)
1406{
1407 struct all_io_list *state;
1408 size_t sz;
1409
1410 state = get_all_io_list(IO_LIST_ALL, &sz);
1411 if (state) {
1412 __verify_save_state(state, "local");
1413 free(state);
1414 }
1415}
1416
1417void verify_free_state(struct thread_data *td)
1418{
1419 if (td->vstate)
1420 free(td->vstate);
1421}
1422
1423void verify_convert_assign_state(struct thread_data *td,
1424 struct thread_io_list *s)
1425{
1426 int i;
1427
1428 s->no_comps = le64_to_cpu(s->no_comps);
1429 s->depth = le64_to_cpu(s->depth);
1430 s->numberio = le64_to_cpu(s->numberio);
1431 for (i = 0; i < 4; i++)
1432 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1433 for (i = 0; i < s->no_comps; i++)
1434 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1435
1436 td->vstate = s;
1437}
1438
1439int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s)
1440{
1441 uint64_t crc;
1442
1443 hdr->version = le64_to_cpu(hdr->version);
1444 hdr->size = le64_to_cpu(hdr->size);
1445 hdr->crc = le64_to_cpu(hdr->crc);
1446
1447 if (hdr->version != VSTATE_HDR_VERSION)
1448 return 1;
1449
1450 crc = fio_crc32c((void *)s, hdr->size);
1451 if (crc != hdr->crc)
1452 return 1;
1453
1454 return 0;
1455}
1456
1457int verify_load_state(struct thread_data *td, const char *prefix)
1458{
1459 struct thread_io_list *s = NULL;
1460 struct verify_state_hdr hdr;
1461 uint64_t crc;
1462 ssize_t ret;
1463 int fd;
1464
1465 if (!td->o.verify_state)
1466 return 0;
1467
1468 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1469 if (fd == -1)
1470 return 1;
1471
1472 ret = read(fd, &hdr, sizeof(hdr));
1473 if (ret != sizeof(hdr)) {
1474 if (ret < 0)
1475 td_verror(td, errno, "read verify state hdr");
1476 log_err("fio: failed reading verify state header\n");
1477 goto err;
1478 }
1479
1480 hdr.version = le64_to_cpu(hdr.version);
1481 hdr.size = le64_to_cpu(hdr.size);
1482 hdr.crc = le64_to_cpu(hdr.crc);
1483
1484 if (hdr.version != VSTATE_HDR_VERSION) {
1485 log_err("fio: bad version in verify state header\n");
1486 goto err;
1487 }
1488
1489 s = malloc(hdr.size);
1490 ret = read(fd, s, hdr.size);
1491 if (ret != hdr.size) {
1492 if (ret < 0)
1493 td_verror(td, errno, "read verify state");
1494 log_err("fio: failed reading verity state\n");
1495 goto err;
1496 }
1497
1498 crc = fio_crc32c((void *)s, hdr.size);
1499 if (crc != hdr.crc) {
1500 log_err("fio: verify state is corrupt\n");
1501 goto err;
1502 }
1503
1504 close(fd);
1505
1506 verify_convert_assign_state(td, s);
1507 return 0;
1508err:
1509 if (s)
1510 free(s);
1511 close(fd);
1512 return 1;
1513}
1514
1515/*
1516 * Use the loaded verify state to know when to stop doing verification
1517 */
1518int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1519{
1520 struct thread_io_list *s = td->vstate;
1521 int i;
1522
1523 if (!s)
1524 return 0;
1525
1526 /*
1527 * If we're not into the window of issues - depth yet, continue
1528 */
1529 if (td->io_blocks[DDIR_READ] < s->depth ||
1530 s->numberio - td->io_blocks[DDIR_READ] > s->depth)
1531 return 0;
1532
1533 /*
1534 * We're in the window of having to check if this io was
1535 * completed or not. If the IO was seen as completed, then
1536 * lets verify it.
1537 */
1538 for (i = 0; i < s->no_comps; i++)
1539 if (io_u->offset == s->offsets[i])
1540 return 0;
1541
1542 /*
1543 * Not found, we have to stop
1544 */
1545 return 1;
1546}