blob: 9eb532a28e080a66a2483943f09fed16efd9ecfa [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 -070032static void fill_pattern(struct thread_data *td, void *p, unsigned int len,
33 char *pattern, unsigned int pattern_bytes)
Jens Axboe90059d62007-07-30 09:33:12 +020034{
Jens Axboece35b1e2014-01-14 15:35:58 -070035 switch (pattern_bytes) {
Jens Axboe90059d62007-07-30 09:33:12 +020036 case 0:
Jens Axboece35b1e2014-01-14 15:35:58 -070037 assert(0);
Jens Axboe90059d62007-07-30 09:33:12 +020038 break;
39 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010040 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Jens Axboece35b1e2014-01-14 15:35:58 -070041 memset(p, pattern[0], len);
Jens Axboe90059d62007-07-30 09:33:12 +020042 break;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010043 default: {
44 unsigned int i = 0, size = 0;
Jens Axboe90059d62007-07-30 09:33:12 +020045 unsigned char *b = p;
46
Jens Axboebd6f78b2008-02-01 20:27:52 +010047 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
Jens Axboece35b1e2014-01-14 15:35:58 -070048 pattern_bytes, len);
Jens Axboebd6f78b2008-02-01 20:27:52 +010049
Jens Axboe90059d62007-07-30 09:33:12 +020050 while (i < len) {
Jens Axboece35b1e2014-01-14 15:35:58 -070051 size = pattern_bytes;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010052 if (size > (len - i))
53 size = len - i;
Jens Axboece35b1e2014-01-14 15:35:58 -070054 memcpy(b+i, pattern, size);
Radha Ramachandran0e92f872009-10-27 20:14:27 +010055 i += size;
Jens Axboe90059d62007-07-30 09:33:12 +020056 }
57 break;
58 }
59 }
60}
61
Jens Axboece35b1e2014-01-14 15:35:58 -070062void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
63{
64 fill_pattern(td, p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
65}
66
67void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
68 struct io_u *io_u, unsigned long seed, int use_seed)
69{
70 if (!td->o.verify_pattern_bytes) {
71 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
72
73 if (use_seed)
74 __fill_random_buf(p, len, seed);
75 else
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -070076 io_u->rand_seed = fill_random_buf(&td->__verify_state, p, len);
Jens Axboece35b1e2014-01-14 15:35:58 -070077 return;
78 }
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -070079
Jens Axboece35b1e2014-01-14 15:35:58 -070080 if (io_u->buf_filled_len >= len) {
81 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
82 td->o.verify_pattern_bytes, len);
83 return;
84 }
85
86 fill_pattern(td, p, len, td->o.verify_pattern, td->o.verify_pattern_bytes);
87
88 io_u->buf_filled_len = len;
89}
90
Jens Axboecfbcd0d2011-01-14 14:49:20 +010091static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
92{
93 unsigned int hdr_inc;
94
95 hdr_inc = io_u->buflen;
Jens Axboe8a99fdf2012-03-06 19:24:49 +010096 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
Jens Axboecfbcd0d2011-01-14 14:49:20 +010097 hdr_inc = td->o.verify_interval;
98
99 return hdr_inc;
100}
101
Jens Axboec50ca7b2011-01-12 08:31:54 +0100102static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
103 unsigned long seed, int use_seed)
104{
105 unsigned int hdr_inc, header_num;
106 struct verify_header *hdr;
107 void *p = io_u->buf;
108
Jens Axboece35b1e2014-01-14 15:35:58 -0700109 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100110
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100111 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100112 header_num = 0;
113 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
114 hdr = p;
115 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
116 header_num++;
117 }
118}
119
Jens Axboe4764aec2007-08-23 09:03:38 +0200120static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +0200121{
Shawn Lewisdee6de72007-08-02 22:17:52 +0200122 char swap[200];
123
124 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +0200125
Shawn Lewis546a9142007-07-28 21:11:37 +0200126 memcpy(&swap, buf1, len);
127 memcpy(buf1, buf2, len);
128 memcpy(buf2, &swap, len);
129}
130
Jens Axboee29d1b72006-10-18 15:43:15 +0200131static void hexdump(void *buffer, int len)
132{
133 unsigned char *p = buffer;
134 int i;
135
136 for (i = 0; i < len; i++)
Jens Axboebfacf382010-06-18 14:29:52 +0200137 log_err("%02x", p[i]);
138 log_err("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200139}
140
Jens Axboed9f2caf2007-07-28 21:22:03 +0200141/*
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700142 * Prepare for separation of verify_header and checksum header
Jens Axboe87677832007-07-30 12:08:14 +0200143 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200144static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200145{
Bruce Cran03e20d62011-01-02 20:14:54 +0100146 unsigned int len = 0;
Jens Axboe87677832007-07-30 12:08:14 +0200147
Jens Axboe546dfd92007-07-30 12:23:05 +0200148 switch (verify_type) {
149 case VERIFY_NONE:
150 case VERIFY_NULL:
151 len = 0;
152 break;
153 case VERIFY_MD5:
154 len = sizeof(struct vhdr_md5);
155 break;
156 case VERIFY_CRC64:
157 len = sizeof(struct vhdr_crc64);
158 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200159 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200160 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200161 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200162 len = sizeof(struct vhdr_crc32);
163 break;
164 case VERIFY_CRC16:
165 len = sizeof(struct vhdr_crc16);
166 break;
167 case VERIFY_CRC7:
168 len = sizeof(struct vhdr_crc7);
169 break;
170 case VERIFY_SHA256:
171 len = sizeof(struct vhdr_sha256);
172 break;
173 case VERIFY_SHA512:
174 len = sizeof(struct vhdr_sha512);
175 break;
Jens Axboe844ea602014-02-20 13:21:45 -0800176 case VERIFY_XXHASH:
177 len = sizeof(struct vhdr_xxhash);
178 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200179 case VERIFY_META:
180 len = sizeof(struct vhdr_meta);
181 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200182 case VERIFY_SHA1:
183 len = sizeof(struct vhdr_sha1);
184 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100185 case VERIFY_PATTERN:
186 len = 0;
187 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200188 default:
189 log_err("fio: unknown verify header!\n");
190 assert(0);
191 }
192
193 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200194}
195
196static inline unsigned int hdr_size(struct verify_header *hdr)
197{
Jens Axboe546dfd92007-07-30 12:23:05 +0200198 return __hdr_size(hdr->verify_type);
199}
200
201static void *hdr_priv(struct verify_header *hdr)
202{
203 void *priv = hdr;
204
205 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200206}
207
208/*
Jens Axboe936216f2010-06-18 13:44:11 +0200209 * Verify container, pass info to verify handlers and allow them to
210 * pass info back in case of error
211 */
212struct vcont {
213 /*
214 * Input
215 */
216 struct io_u *io_u;
217 unsigned int hdr_num;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100218 struct thread_data *td;
Jens Axboe936216f2010-06-18 13:44:11 +0200219
220 /*
221 * Output, only valid in case of error
222 */
Jens Axboebfacf382010-06-18 14:29:52 +0200223 const char *name;
224 void *good_crc;
225 void *bad_crc;
Jens Axboe936216f2010-06-18 13:44:11 +0200226 unsigned int crc_len;
227};
228
Jens Axboec50ca7b2011-01-12 08:31:54 +0100229static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
230 const char *type, struct fio_file *f)
Jens Axboe7d9fb452011-01-11 22:16:49 +0100231{
Jens Axboe6f260b32011-01-13 18:57:54 +0100232 char *ptr, fname[256];
Jens Axboe7d9fb452011-01-11 22:16:49 +0100233 int ret, fd;
234
Jens Axboe6f260b32011-01-13 18:57:54 +0100235 ptr = strdup(f->file_name);
236 strcpy(fname, basename(ptr));
Jens Axboec50ca7b2011-01-12 08:31:54 +0100237
238 sprintf(fname + strlen(fname), ".%llu.%s", offset, type);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100239
240 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
241 if (fd < 0) {
242 perror("open verify buf file");
243 return;
244 }
245
246 while (len) {
247 ret = write(fd, buf, len);
248 if (!ret)
249 break;
250 else if (ret < 0) {
251 perror("write verify buf file");
252 break;
253 }
254 len -= ret;
255 buf += ret;
256 }
257
258 close(fd);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100259 log_err(" %s data dumped as %s\n", type, fname);
Jens Axboe6f260b32011-01-13 18:57:54 +0100260 free(ptr);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100261}
262
Jens Axboec50ca7b2011-01-12 08:31:54 +0100263/*
264 * Dump the contents of the read block and re-generate the correct data
265 * and dump that too.
266 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100267static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
268{
269 struct thread_data *td = vc->td;
270 struct io_u *io_u = vc->io_u;
271 unsigned long hdr_offset;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100272 struct io_u dummy;
Jens Axboec50ca7b2011-01-12 08:31:54 +0100273 void *buf;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100274
Steven Lang0dce9bc2011-10-25 22:41:05 +0200275 if (!td->o.verify_dump)
276 return;
277
Jens Axboec50ca7b2011-01-12 08:31:54 +0100278 /*
279 * Dump the contents we just read off disk
280 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100281 hdr_offset = vc->hdr_num * hdr->len;
282
Jens Axboec50ca7b2011-01-12 08:31:54 +0100283 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
284 "received", vc->io_u->file);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100285
Jens Axboec50ca7b2011-01-12 08:31:54 +0100286 /*
287 * Allocate a new buf and re-generate the original data
288 */
289 buf = malloc(io_u->buflen);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100290 dummy = *io_u;
Jens Axboec50ca7b2011-01-12 08:31:54 +0100291 dummy.buf = buf;
Jens Axboe4aae38a2011-01-12 08:59:12 +0100292 dummy.rand_seed = hdr->rand_seed;
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100293 dummy.buf_filled_len = 0;
Josef Bacik0d81daf2013-07-08 20:35:01 -0400294 dummy.buflen = io_u->buflen;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100295
Jens Axboec50ca7b2011-01-12 08:31:54 +0100296 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100297
Jens Axboec50ca7b2011-01-12 08:31:54 +0100298 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
299 "expected", vc->io_u->file);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100300 free(buf);
301}
302
Jens Axboebfacf382010-06-18 14:29:52 +0200303static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
304{
305 unsigned long long offset;
306
307 offset = vc->io_u->offset;
308 offset += vc->hdr_num * hdr->len;
Jens Axboe32c17ad2010-06-18 14:32:21 +0200309 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
310 vc->name, vc->io_u->file->file_name, offset, hdr->len);
Jens Axboebfacf382010-06-18 14:29:52 +0200311
312 if (vc->good_crc && vc->bad_crc) {
313 log_err(" Expected CRC: ");
314 hexdump(vc->good_crc, vc->crc_len);
315 log_err(" Received CRC: ");
316 hexdump(vc->bad_crc, vc->crc_len);
317 }
Jens Axboe7d9fb452011-01-11 22:16:49 +0100318
319 dump_verify_buffers(hdr, vc);
Jens Axboebfacf382010-06-18 14:29:52 +0200320}
321
Jens Axboe936216f2010-06-18 13:44:11 +0200322/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200323 * Return data area 'header_num'
324 */
Jens Axboe936216f2010-06-18 13:44:11 +0200325static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200326{
Jens Axboe936216f2010-06-18 13:44:11 +0200327 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200328}
329
Jens Axboe92bf48d2011-01-14 21:20:42 +0100330static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
331{
332 struct thread_data *td = vc->td;
333 struct io_u *io_u = vc->io_u;
334 char *buf, *pattern;
Jens Axboe2b13e712011-01-19 14:04:16 -0700335 unsigned int header_size = __hdr_size(td->o.verify);
Steven Lang9a2a86d2012-02-07 09:42:59 +0100336 unsigned int len, mod, i, size, pattern_size;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100337
338 pattern = td->o.verify_pattern;
Steven Lang9a2a86d2012-02-07 09:42:59 +0100339 pattern_size = td->o.verify_pattern_bytes;
340 if (pattern_size <= 1)
341 pattern_size = MAX_PATTERN_SIZE;
Jens Axboe2b13e712011-01-19 14:04:16 -0700342 buf = (void *) hdr + header_size;
343 len = get_hdr_inc(td, io_u) - header_size;
Steven Lang9a2a86d2012-02-07 09:42:59 +0100344 mod = header_size % pattern_size;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100345
Steven Lang9a2a86d2012-02-07 09:42:59 +0100346 for (i = 0; i < len; i += size) {
347 size = pattern_size - mod;
348 if (size > (len - i))
349 size = len - i;
350 if (memcmp(buf + i, pattern + mod, size))
Jens Axboee4ad68b2012-02-23 08:35:04 +0100351 /* Let the slow compare find the first mismatch byte. */
Steven Lang9a2a86d2012-02-07 09:42:59 +0100352 break;
353 mod = 0;
354 }
355
356 for (; i < len; i++) {
Jens Axboe92bf48d2011-01-14 21:20:42 +0100357 if (buf[i] != pattern[mod]) {
358 unsigned int bits;
359
360 bits = hweight8(buf[i] ^ pattern[mod]);
361 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
362 buf[i], pattern[mod], bits);
363 log_err("fio: bad pattern block offset %u\n", i);
364 dump_verify_buffers(hdr, vc);
365 return EILSEQ;
366 }
367 mod++;
368 if (mod == td->o.verify_pattern_bytes)
369 mod = 0;
370 }
371
372 return 0;
373}
374
375static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
376{
377 struct thread_data *td = vc->td;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200378 struct vhdr_meta *vh = hdr_priv(hdr);
Jens Axboe936216f2010-06-18 13:44:11 +0200379 struct io_u *io_u = vc->io_u;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100380 int ret = EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200381
Jens Axboebd6f78b2008-02-01 20:27:52 +0100382 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
383
Jens Axboebfacf382010-06-18 14:29:52 +0200384 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
Jens Axboe92bf48d2011-01-14 21:20:42 +0100385 ret = 0;
386
387 if (td->o.verify_pattern_bytes)
388 ret |= verify_io_u_pattern(hdr, vc);
389
Juan Casseda0a7bd2013-09-17 14:06:12 -0700390 /*
391 * For read-only workloads, the program cannot be certain of the
392 * last numberio written to a block. Checking of numberio will be done
393 * only for workloads that write data.
Juan Casse62167762013-09-17 14:06:13 -0700394 * For verify_only, numberio will be checked in the last iteration when
395 * the correct state of numberio, that would have been written to each
396 * block in a previous run of fio, has been reached.
Juan Casseda0a7bd2013-09-17 14:06:12 -0700397 */
398 if (td_write(td) || td_rw(td))
Juan Casse62167762013-09-17 14:06:13 -0700399 if (!td->o.verify_only || td->o.loops == 0)
400 if (vh->numberio != io_u->numberio)
401 ret = EILSEQ;
Juan Casseda0a7bd2013-09-17 14:06:12 -0700402
Jens Axboe92bf48d2011-01-14 21:20:42 +0100403 if (!ret)
Jens Axboebfacf382010-06-18 14:29:52 +0200404 return 0;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200405
Jens Axboebfacf382010-06-18 14:29:52 +0200406 vc->name = "meta";
407 log_verify_failure(hdr, vc);
Jens Axboe92bf48d2011-01-14 21:20:42 +0100408 return ret;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200409}
410
Jens Axboe844ea602014-02-20 13:21:45 -0800411static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
412{
413 void *p = io_u_verify_off(hdr, vc);
414 struct vhdr_xxhash *vh = hdr_priv(hdr);
415 uint32_t hash;
416 void *state;
417
418 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
419
420 state = XXH32_init(1);
421 XXH32_update(state, p, hdr->len - hdr_size(hdr));
422 hash = XXH32_digest(state);
423
424 if (vh->hash == hash)
425 return 0;
426
427 vc->name = "xxhash";
428 vc->good_crc = &vh->hash;
429 vc->bad_crc = &hash;
430 vc->crc_len = sizeof(hash);
431 log_verify_failure(hdr, vc);
432 return EILSEQ;
433}
434
Jens Axboe936216f2010-06-18 13:44:11 +0200435static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200436{
Jens Axboe936216f2010-06-18 13:44:11 +0200437 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200438 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200439 uint8_t sha512[128];
Jens Axboe25dfa842012-02-29 10:01:34 +0100440 struct fio_sha512_ctx sha512_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200441 .buf = sha512,
442 };
443
Jens Axboe936216f2010-06-18 13:44:11 +0200444 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100445
Jens Axboe25dfa842012-02-29 10:01:34 +0100446 fio_sha512_init(&sha512_ctx);
447 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200448
Jens Axboebfacf382010-06-18 14:29:52 +0200449 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
450 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200451
Jens Axboebfacf382010-06-18 14:29:52 +0200452 vc->name = "sha512";
453 vc->good_crc = vh->sha512;
454 vc->bad_crc = sha512_ctx.buf;
455 vc->crc_len = sizeof(vh->sha512);
456 log_verify_failure(hdr, vc);
457 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200458}
459
Jens Axboe936216f2010-06-18 13:44:11 +0200460static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200461{
Jens Axboe936216f2010-06-18 13:44:11 +0200462 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200463 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100464 uint8_t sha256[64];
Jens Axboe25dfa842012-02-29 10:01:34 +0100465 struct fio_sha256_ctx sha256_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200466 .buf = sha256,
467 };
468
Jens Axboe936216f2010-06-18 13:44:11 +0200469 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100470
Jens Axboe25dfa842012-02-29 10:01:34 +0100471 fio_sha256_init(&sha256_ctx);
472 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200473
Jens Axboebfacf382010-06-18 14:29:52 +0200474 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
475 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200476
Jens Axboebfacf382010-06-18 14:29:52 +0200477 vc->name = "sha256";
478 vc->good_crc = vh->sha256;
479 vc->bad_crc = sha256_ctx.buf;
480 vc->crc_len = sizeof(vh->sha256);
481 log_verify_failure(hdr, vc);
482 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200483}
484
Jens Axboe936216f2010-06-18 13:44:11 +0200485static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200486{
Jens Axboe936216f2010-06-18 13:44:11 +0200487 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200488 struct vhdr_sha1 *vh = hdr_priv(hdr);
489 uint32_t sha1[5];
Jens Axboe25dfa842012-02-29 10:01:34 +0100490 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200491 .H = sha1,
492 };
493
Jens Axboe936216f2010-06-18 13:44:11 +0200494 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200495
Jens Axboe25dfa842012-02-29 10:01:34 +0100496 fio_sha1_init(&sha1_ctx);
497 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboe7c353ce2009-08-09 22:40:33 +0200498
Jens Axboebfacf382010-06-18 14:29:52 +0200499 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
500 return 0;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200501
Jens Axboebfacf382010-06-18 14:29:52 +0200502 vc->name = "sha1";
503 vc->good_crc = vh->sha1;
504 vc->bad_crc = sha1_ctx.H;
505 vc->crc_len = sizeof(vh->sha1);
506 log_verify_failure(hdr, vc);
507 return EILSEQ;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200508}
509
Jens Axboe936216f2010-06-18 13:44:11 +0200510static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200511{
Jens Axboe936216f2010-06-18 13:44:11 +0200512 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200513 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200514 unsigned char c;
515
Jens Axboe936216f2010-06-18 13:44:11 +0200516 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100517
Jens Axboe25dfa842012-02-29 10:01:34 +0100518 c = fio_crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200519
Jens Axboebfacf382010-06-18 14:29:52 +0200520 if (c == vh->crc7)
521 return 0;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200522
Jens Axboebfacf382010-06-18 14:29:52 +0200523 vc->name = "crc7";
524 vc->good_crc = &vh->crc7;
525 vc->bad_crc = &c;
526 vc->crc_len = 1;
527 log_verify_failure(hdr, vc);
528 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200529}
530
Jens Axboe936216f2010-06-18 13:44:11 +0200531static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200532{
Jens Axboe936216f2010-06-18 13:44:11 +0200533 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200534 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200535 unsigned short c;
536
Jens Axboe936216f2010-06-18 13:44:11 +0200537 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100538
Jens Axboe25dfa842012-02-29 10:01:34 +0100539 c = fio_crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200540
Jens Axboebfacf382010-06-18 14:29:52 +0200541 if (c == vh->crc16)
542 return 0;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200543
Jens Axboebfacf382010-06-18 14:29:52 +0200544 vc->name = "crc16";
545 vc->good_crc = &vh->crc16;
546 vc->bad_crc = &c;
547 vc->crc_len = 2;
548 log_verify_failure(hdr, vc);
549 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200550}
551
Jens Axboe936216f2010-06-18 13:44:11 +0200552static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200553{
Jens Axboe936216f2010-06-18 13:44:11 +0200554 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200555 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200556 unsigned long long c;
557
Jens Axboe936216f2010-06-18 13:44:11 +0200558 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100559
Jens Axboe25dfa842012-02-29 10:01:34 +0100560 c = fio_crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200561
Jens Axboebfacf382010-06-18 14:29:52 +0200562 if (c == vh->crc64)
563 return 0;
Jens Axboed77a7af2007-07-27 15:35:06 +0200564
Jens Axboebfacf382010-06-18 14:29:52 +0200565 vc->name = "crc64";
566 vc->good_crc = &vh->crc64;
567 vc->bad_crc = &c;
568 vc->crc_len = 8;
569 log_verify_failure(hdr, vc);
570 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200571}
572
Jens Axboe936216f2010-06-18 13:44:11 +0200573static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200574{
Jens Axboe936216f2010-06-18 13:44:11 +0200575 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200576 struct vhdr_crc32 *vh = hdr_priv(hdr);
577 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200578
Jens Axboe936216f2010-06-18 13:44:11 +0200579 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100580
Jens Axboe25dfa842012-02-29 10:01:34 +0100581 c = fio_crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200582
Jens Axboebfacf382010-06-18 14:29:52 +0200583 if (c == vh->crc32)
584 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200585
Jens Axboebfacf382010-06-18 14:29:52 +0200586 vc->name = "crc32";
587 vc->good_crc = &vh->crc32;
588 vc->bad_crc = &c;
589 vc->crc_len = 4;
590 log_verify_failure(hdr, vc);
591 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200592}
593
Jens Axboe936216f2010-06-18 13:44:11 +0200594static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200595{
Jens Axboe936216f2010-06-18 13:44:11 +0200596 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200597 struct vhdr_crc32 *vh = hdr_priv(hdr);
598 uint32_t c;
599
Jens Axboe936216f2010-06-18 13:44:11 +0200600 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200601
Jens Axboe25dfa842012-02-29 10:01:34 +0100602 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200603
Jens Axboebfacf382010-06-18 14:29:52 +0200604 if (c == vh->crc32)
605 return 0;
Jens Axboebac39e02008-06-11 20:46:19 +0200606
Jens Axboebfacf382010-06-18 14:29:52 +0200607 vc->name = "crc32c";
608 vc->good_crc = &vh->crc32;
609 vc->bad_crc = &c;
610 vc->crc_len = 4;
611 log_verify_failure(hdr, vc);
612 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200613}
614
Jens Axboe936216f2010-06-18 13:44:11 +0200615static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200616{
Jens Axboe936216f2010-06-18 13:44:11 +0200617 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200618 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200619 uint32_t hash[MD5_HASH_WORDS];
Jens Axboe25dfa842012-02-29 10:01:34 +0100620 struct fio_md5_ctx md5_ctx = {
Jens Axboe8c432322007-07-27 13:16:24 +0200621 .hash = hash,
622 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200623
Jens Axboe936216f2010-06-18 13:44:11 +0200624 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100625
Jens Axboe25dfa842012-02-29 10:01:34 +0100626 fio_md5_init(&md5_ctx);
627 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200628
Jens Axboebfacf382010-06-18 14:29:52 +0200629 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
630 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200631
Jens Axboebfacf382010-06-18 14:29:52 +0200632 vc->name = "md5";
633 vc->good_crc = vh->md5_digest;
634 vc->bad_crc = md5_ctx.hash;
635 vc->crc_len = sizeof(hash);
636 log_verify_failure(hdr, vc);
637 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200638}
639
Jens Axboee8462bd2009-07-06 12:59:04 +0200640/*
641 * Push IO verification to a separate thread
642 */
643int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
644{
645 if (io_u->file)
646 put_file_log(td, io_u->file);
647
Jens Axboee8462bd2009-07-06 12:59:04 +0200648 pthread_mutex_lock(&td->io_u_lock);
Steven Langd7ee2a72011-10-26 09:46:50 +0200649
Radha Ramachandran0c412142009-11-03 21:45:31 +0100650 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
651 td->cur_depth--;
652 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
653 }
Jens Axboe2ae0b202013-05-28 14:16:55 +0200654 flist_add_tail(&io_u->verify_list, &td->verify_list);
Jens Axboe2ecc1b52009-11-04 20:58:09 +0100655 io_u->flags |= IO_U_F_FREE_DEF;
Jens Axboee8462bd2009-07-06 12:59:04 +0200656 pthread_mutex_unlock(&td->io_u_lock);
657
658 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200659 return 0;
660}
661
Jens Axboe0d29de82010-09-01 13:54:15 +0200662static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
663{
664 static char zero_buf[1024];
665 unsigned int this_len, len;
666 int ret = 0;
667 void *p;
668
669 if (!td->o.trim_zero)
670 return 0;
671
672 len = io_u->buflen;
673 p = io_u->buf;
674 do {
675 this_len = sizeof(zero_buf);
676 if (this_len > len)
677 this_len = len;
678 if (memcmp(p, zero_buf, this_len)) {
679 ret = EILSEQ;
680 break;
681 }
682 len -= this_len;
683 p += this_len;
684 } while (len);
685
686 if (!ret)
687 return 0;
688
Jens Axboea917a8b2010-09-02 13:23:20 +0200689 log_err("trim: verify failed at file %s offset %llu, length %lu"
690 ", block offset %lu\n",
691 io_u->file->file_name, io_u->offset, io_u->buflen,
Jens Axboe2f681242010-10-21 08:15:59 +0200692 (unsigned long) (p - io_u->buf));
Jens Axboe0d29de82010-09-01 13:54:15 +0200693 return ret;
694}
695
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100696static int verify_header(struct io_u *io_u, struct verify_header *hdr)
Jens Axboef65d1c22012-02-22 20:11:57 +0100697{
698 void *p = hdr;
699 uint32_t crc;
700
Jens Axboeae38c0d2012-02-23 10:31:07 +0100701 if (hdr->magic != FIO_HDR_MAGIC)
Juan Cassee9e177c2013-09-17 14:06:14 -0700702 return 1;
703 if (hdr->len > io_u->buflen)
704 return 2;
705 if (hdr->rand_seed != io_u->rand_seed)
706 return 3;
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));
Jens Axboef65d1c22012-02-22 20:11:57 +0100709 if (crc == hdr->crc32)
Juan Cassee9e177c2013-09-17 14:06:14 -0700710 return 0;
Jens Axboef65d1c22012-02-22 20:11:57 +0100711 log_err("fio: verify header crc %x, calculated %x\n", hdr->crc32, crc);
Juan Cassee9e177c2013-09-17 14:06:14 -0700712 return 4;
Jens Axboef65d1c22012-02-22 20:11:57 +0100713}
714
Jens Axboe36690c92007-03-26 10:23:34 +0200715int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200716{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200717 struct verify_header *hdr;
Jens Axboe2b13e712011-01-19 14:04:16 -0700718 unsigned int header_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200719 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200720 int ret;
721
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200722 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200723 return 0;
Jens Axboe0d29de82010-09-01 13:54:15 +0200724 if (io_u->flags & IO_U_F_TRIMMED) {
725 ret = verify_trimmed_io_u(td, io_u);
726 goto done;
727 }
Jens Axboe36690c92007-03-26 10:23:34 +0200728
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100729 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200730
Jens Axboea12a3b42007-08-09 10:20:54 +0200731 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100732 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
733 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200734 struct vcont vc = {
735 .io_u = io_u,
736 .hdr_num = hdr_num,
Jens Axboe7d9fb452011-01-11 22:16:49 +0100737 .td = td,
Jens Axboebfacf382010-06-18 14:29:52 +0200738 };
Jens Axboef00b2102012-11-30 09:39:02 +0100739 unsigned int verify_type;
Jens Axboe936216f2010-06-18 13:44:11 +0200740
Jens Axboef3e6cb92010-06-18 14:48:43 +0200741 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200742 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200743
Jens Axboe2b13e712011-01-19 14:04:16 -0700744 header_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200745 if (td->o.verify_offset)
Jens Axboe2b13e712011-01-19 14:04:16 -0700746 memswp(p, p + td->o.verify_offset, header_size);
Jens Axboe95646102007-07-28 21:17:50 +0200747 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200748
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -0700749 /*
750 * Make rand_seed check pass when have verifysort or
751 * verify_backlog.
752 */
753 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
754 io_u->rand_seed = hdr->rand_seed;
755
Juan Cassee9e177c2013-09-17 14:06:14 -0700756 ret = verify_header(io_u, hdr);
757 switch (ret) {
758 case 0:
759 break;
760 case 1:
Jens Axboeae38c0d2012-02-23 10:31:07 +0100761 log_err("verify: bad magic header %x, wanted %x at "
762 "file %s offset %llu, length %u\n",
Jens Axboed3a173a2012-02-23 08:23:18 +0100763 hdr->magic, FIO_HDR_MAGIC,
Jens Axboec9b44032010-06-18 14:46:06 +0200764 io_u->file->file_name,
765 io_u->offset + hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200766 return EILSEQ;
Juan Cassee9e177c2013-09-17 14:06:14 -0700767 break;
768 case 2:
769 log_err("fio: verify header exceeds buffer length (%u "
770 "> %lu)\n", hdr->len, io_u->buflen);
771 return EILSEQ;
772 break;
773 case 3:
774 log_err("verify: bad header rand_seed %"PRIu64
775 ", wanted %"PRIu64" at file %s offset %llu, "
776 "length %u\n",
777 hdr->rand_seed, io_u->rand_seed,
778 io_u->file->file_name,
779 io_u->offset + hdr_num * hdr->len, hdr->len);
780 return EILSEQ;
781 break;
782 case 4:
783 return EILSEQ;
784 break;
785 default:
786 log_err("verify: unknown header error at file %s "
787 "offset %llu, length %u\n",
788 io_u->file->file_name,
789 io_u->offset + hdr_num * hdr->len, hdr->len);
790 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200791 }
792
Jens Axboef00b2102012-11-30 09:39:02 +0100793 if (td->o.verify != VERIFY_NONE)
794 verify_type = td->o.verify;
795 else
796 verify_type = hdr->verify_type;
797
798 switch (verify_type) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200799 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200800 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200801 break;
802 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200803 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200804 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200805 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200806 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200807 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200808 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200809 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200810 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200811 break;
812 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200813 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200814 break;
815 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200816 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200817 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200818 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200819 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200820 break;
821 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200822 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200823 break;
Jens Axboe844ea602014-02-20 13:21:45 -0800824 case VERIFY_XXHASH:
825 ret = verify_io_u_xxhash(hdr, &vc);
826 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200827 case VERIFY_META:
Jens Axboe92bf48d2011-01-14 21:20:42 +0100828 ret = verify_io_u_meta(hdr, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200829 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200830 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200831 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200832 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100833 case VERIFY_PATTERN:
834 ret = verify_io_u_pattern(hdr, &vc);
835 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200836 default:
837 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200838 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200839 }
Jens Axboef00b2102012-11-30 09:39:02 +0100840
841 if (ret && verify_type != hdr->verify_type)
842 log_err("fio: verify type mismatch (%u media, %u given)\n",
843 hdr->verify_type, verify_type);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200844 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100845
Jens Axboe0d29de82010-09-01 13:54:15 +0200846done:
Jens Axboef3e6cb92010-06-18 14:48:43 +0200847 if (ret && td->o.verify_fatal)
848 td->terminate = 1;
849
Jens Axboea12a3b42007-08-09 10:20:54 +0200850 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200851}
852
Shawn Lewis7437ee82007-08-02 21:05:58 +0200853static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100854 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200855{
856 struct vhdr_meta *vh = hdr_priv(hdr);
857
858 vh->thread = td->thread_number;
859
860 vh->time_sec = io_u->start_time.tv_sec;
861 vh->time_usec = io_u->start_time.tv_usec;
862
Juan Casseda0a7bd2013-09-17 14:06:12 -0700863 vh->numberio = io_u->numberio;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200864
865 vh->offset = io_u->offset + header_num * td->o.verify_interval;
866}
867
Jens Axboe844ea602014-02-20 13:21:45 -0800868static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
869{
870 struct vhdr_xxhash *vh = hdr_priv(hdr);
871 void *state;
872
873 state = XXH32_init(1);
874 XXH32_update(state, p, len);
875 vh->hash = XXH32_digest(state);
876}
877
Jens Axboecd14cc12007-07-30 10:59:33 +0200878static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
879{
Jens Axboe546dfd92007-07-30 12:23:05 +0200880 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100881 struct fio_sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200882 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200883 };
884
Jens Axboe25dfa842012-02-29 10:01:34 +0100885 fio_sha512_init(&sha512_ctx);
886 fio_sha512_update(&sha512_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200887}
888
889static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
890{
Jens Axboe546dfd92007-07-30 12:23:05 +0200891 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100892 struct fio_sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200893 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200894 };
895
Jens Axboe25dfa842012-02-29 10:01:34 +0100896 fio_sha256_init(&sha256_ctx);
897 fio_sha256_update(&sha256_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200898}
899
Jens Axboe7c353ce2009-08-09 22:40:33 +0200900static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
901{
902 struct vhdr_sha1 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100903 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200904 .H = vh->sha1,
905 };
906
Jens Axboe25dfa842012-02-29 10:01:34 +0100907 fio_sha1_init(&sha1_ctx);
908 fio_sha1_update(&sha1_ctx, p, len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200909}
910
Jens Axboe1e154bd2007-07-27 09:52:40 +0200911static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
912{
Jens Axboe546dfd92007-07-30 12:23:05 +0200913 struct vhdr_crc7 *vh = hdr_priv(hdr);
914
Jens Axboe25dfa842012-02-29 10:01:34 +0100915 vh->crc7 = fio_crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200916}
917
Jens Axboe969f7ed2007-07-27 09:07:17 +0200918static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
919{
Jens Axboe546dfd92007-07-30 12:23:05 +0200920 struct vhdr_crc16 *vh = hdr_priv(hdr);
921
Jens Axboe25dfa842012-02-29 10:01:34 +0100922 vh->crc16 = fio_crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200923}
924
Jens Axboee29d1b72006-10-18 15:43:15 +0200925static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
926{
Jens Axboe546dfd92007-07-30 12:23:05 +0200927 struct vhdr_crc32 *vh = hdr_priv(hdr);
928
Jens Axboe25dfa842012-02-29 10:01:34 +0100929 vh->crc32 = fio_crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200930}
931
Jens Axboebac39e02008-06-11 20:46:19 +0200932static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
933{
934 struct vhdr_crc32 *vh = hdr_priv(hdr);
935
Jens Axboe25dfa842012-02-29 10:01:34 +0100936 vh->crc32 = fio_crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200937}
938
Jens Axboed77a7af2007-07-27 15:35:06 +0200939static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
940{
Jens Axboe546dfd92007-07-30 12:23:05 +0200941 struct vhdr_crc64 *vh = hdr_priv(hdr);
942
Jens Axboe25dfa842012-02-29 10:01:34 +0100943 vh->crc64 = fio_crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200944}
945
Jens Axboee29d1b72006-10-18 15:43:15 +0200946static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
947{
Jens Axboe546dfd92007-07-30 12:23:05 +0200948 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100949 struct fio_md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200950 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200951 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200952
Jens Axboe25dfa842012-02-29 10:01:34 +0100953 fio_md5_init(&md5_ctx);
954 fio_md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200955}
956
Jens Axboe7d9fb452011-01-11 22:16:49 +0100957static void populate_hdr(struct thread_data *td, struct io_u *io_u,
958 struct verify_header *hdr, unsigned int header_num,
959 unsigned int header_len)
960{
961 unsigned int data_len;
962 void *data, *p;
963
964 p = (void *) hdr;
965
Jens Axboed3a173a2012-02-23 08:23:18 +0100966 hdr->magic = FIO_HDR_MAGIC;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100967 hdr->verify_type = td->o.verify;
Jens Axboed3a173a2012-02-23 08:23:18 +0100968 hdr->len = header_len;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100969 hdr->rand_seed = io_u->rand_seed;
Jens Axboe25dfa842012-02-29 10:01:34 +0100970 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100971
Jens Axboe7d9fb452011-01-11 22:16:49 +0100972 data_len = header_len - hdr_size(hdr);
973
974 data = p + hdr_size(hdr);
975 switch (td->o.verify) {
976 case VERIFY_MD5:
977 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
978 io_u, hdr->len);
979 fill_md5(hdr, data, data_len);
980 break;
981 case VERIFY_CRC64:
982 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
983 io_u, hdr->len);
984 fill_crc64(hdr, data, data_len);
985 break;
986 case VERIFY_CRC32C:
987 case VERIFY_CRC32C_INTEL:
988 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
989 io_u, hdr->len);
990 fill_crc32c(hdr, data, data_len);
991 break;
992 case VERIFY_CRC32:
993 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
994 io_u, hdr->len);
995 fill_crc32(hdr, data, data_len);
996 break;
997 case VERIFY_CRC16:
998 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
999 io_u, hdr->len);
1000 fill_crc16(hdr, data, data_len);
1001 break;
1002 case VERIFY_CRC7:
1003 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1004 io_u, hdr->len);
1005 fill_crc7(hdr, data, data_len);
1006 break;
1007 case VERIFY_SHA256:
1008 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1009 io_u, hdr->len);
1010 fill_sha256(hdr, data, data_len);
1011 break;
1012 case VERIFY_SHA512:
1013 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1014 io_u, hdr->len);
1015 fill_sha512(hdr, data, data_len);
1016 break;
Jens Axboe844ea602014-02-20 13:21:45 -08001017 case VERIFY_XXHASH:
1018 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1019 io_u, hdr->len);
1020 fill_xxhash(hdr, data, data_len);
1021 break;
Jens Axboe7d9fb452011-01-11 22:16:49 +01001022 case VERIFY_META:
1023 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1024 io_u, hdr->len);
1025 fill_meta(hdr, td, io_u, header_num);
1026 break;
1027 case VERIFY_SHA1:
1028 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1029 io_u, hdr->len);
1030 fill_sha1(hdr, data, data_len);
1031 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +01001032 case VERIFY_PATTERN:
1033 /* nothing to do here */
1034 break;
Jens Axboe7d9fb452011-01-11 22:16:49 +01001035 default:
1036 log_err("fio: bad verify type: %d\n", td->o.verify);
1037 assert(0);
1038 }
1039 if (td->o.verify_offset)
1040 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
1041}
1042
Jens Axboee29d1b72006-10-18 15:43:15 +02001043/*
1044 * fill body of io_u->buf with random data and add a header with the
Jens Axboec50ca7b2011-01-12 08:31:54 +01001045 * checksum of choice
Jens Axboee29d1b72006-10-18 15:43:15 +02001046 */
1047void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1048{
Jens Axboe9cc3d152007-03-26 10:32:30 +02001049 if (td->o.verify == VERIFY_NULL)
1050 return;
1051
Juan Casseda0a7bd2013-09-17 14:06:12 -07001052 io_u->numberio = td->io_issues[io_u->ddir];
1053
Jens Axboec50ca7b2011-01-12 08:31:54 +01001054 fill_pattern_headers(td, io_u, 0, 0);
Jens Axboee29d1b72006-10-18 15:43:15 +02001055}
1056
1057int get_next_verify(struct thread_data *td, struct io_u *io_u)
1058{
Jens Axboe8de8f042007-03-27 10:36:12 +02001059 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +02001060
Jens Axboed2d7fa52007-02-19 13:21:35 +01001061 /*
1062 * this io_u is from a requeue, we already filled the offsets
1063 */
1064 if (io_u->file)
1065 return 0;
1066
Jens Axboe8de8f042007-03-27 10:36:12 +02001067 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1068 struct rb_node *n = rb_first(&td->io_hist_tree);
1069
Jens Axboe4b878982007-03-26 09:32:22 +02001070 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboef9401282014-02-06 12:17:37 -07001071
1072 /*
1073 * Ensure that the associated IO has completed
1074 */
1075 read_barrier();
1076 if (ipo->flags & IP_F_IN_FLIGHT)
1077 goto nothing;
1078
Jens Axboe4b878982007-03-26 09:32:22 +02001079 rb_erase(n, &td->io_hist_tree);
Jens Axboea917a8b2010-09-02 13:23:20 +02001080 assert(ipo->flags & IP_F_ONRB);
1081 ipo->flags &= ~IP_F_ONRB;
Jens Axboe01743ee2008-06-02 12:19:19 +02001082 } else if (!flist_empty(&td->io_hist_list)) {
1083 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboef9401282014-02-06 12:17:37 -07001084
1085 /*
1086 * Ensure that the associated IO has completed
1087 */
1088 read_barrier();
1089 if (ipo->flags & IP_F_IN_FLIGHT)
1090 goto nothing;
1091
Jens Axboe01743ee2008-06-02 12:19:19 +02001092 flist_del(&ipo->list);
Jens Axboea917a8b2010-09-02 13:23:20 +02001093 assert(ipo->flags & IP_F_ONLIST);
1094 ipo->flags &= ~IP_F_ONLIST;
Jens Axboe8de8f042007-03-27 10:36:12 +02001095 }
Jens Axboee29d1b72006-10-18 15:43:15 +02001096
Jens Axboe8de8f042007-03-27 10:36:12 +02001097 if (ipo) {
Jens Axboe0d29de82010-09-01 13:54:15 +02001098 td->io_hist_len--;
1099
Jens Axboee29d1b72006-10-18 15:43:15 +02001100 io_u->offset = ipo->offset;
1101 io_u->buflen = ipo->len;
Juan Casseda0a7bd2013-09-17 14:06:12 -07001102 io_u->numberio = ipo->numberio;
Jens Axboe36167d82007-02-18 05:41:31 +01001103 io_u->file = ipo->file;
Jens Axboe82af2a72012-03-13 13:45:58 +01001104 io_u->flags |= IO_U_F_VER_LIST;
Jens Axboe97af62c2007-05-22 11:12:13 +02001105
Jens Axboe0d29de82010-09-01 13:54:15 +02001106 if (ipo->flags & IP_F_TRIMMED)
1107 io_u->flags |= IO_U_F_TRIMMED;
1108
Jens Axboed6aed792009-06-03 08:41:15 +02001109 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +02001110 int r = td_io_open_file(td, io_u->file);
1111
Jens Axboebd6f78b2008-02-01 20:27:52 +01001112 if (r) {
1113 dprint(FD_VERIFY, "failed file %s open\n",
1114 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +02001115 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +01001116 }
Jens Axboe97af62c2007-05-22 11:12:13 +02001117 }
1118
1119 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +02001120 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +02001121 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +01001122 io_u->xfer_buf = io_u->buf;
1123 io_u->xfer_buflen = io_u->buflen;
Jens Axboe0d29de82010-09-01 13:54:15 +02001124
1125 remove_trim_entry(td, ipo);
Jens Axboee29d1b72006-10-18 15:43:15 +02001126 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001127 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -07001128
1129 if (!td->o.verify_pattern_bytes) {
1130 io_u->rand_seed = __rand(&td->__verify_state);
1131 if (sizeof(int) != sizeof(long *))
1132 io_u->rand_seed *= __rand(&td->__verify_state);
1133 }
Jens Axboee29d1b72006-10-18 15:43:15 +02001134 return 0;
1135 }
1136
Jens Axboef9401282014-02-06 12:17:37 -07001137nothing:
Jens Axboebd6f78b2008-02-01 20:27:52 +01001138 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +02001139 return 1;
1140}
Jens Axboee8462bd2009-07-06 12:59:04 +02001141
Jens Axboedc5bfbb2013-04-10 22:23:40 +02001142void fio_verify_init(struct thread_data *td)
1143{
1144 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1145 td->o.verify == VERIFY_CRC32C) {
1146 crc32c_intel_probe();
1147 }
1148}
1149
Jens Axboee8462bd2009-07-06 12:59:04 +02001150static void *verify_async_thread(void *data)
1151{
1152 struct thread_data *td = data;
1153 struct io_u *io_u;
1154 int ret = 0;
1155
1156 if (td->o.verify_cpumask_set &&
1157 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1158 log_err("fio: failed setting verify thread affinity\n");
1159 goto done;
1160 }
1161
1162 do {
Jens Axboee53ab272009-07-06 13:43:46 +02001163 FLIST_HEAD(list);
1164
Jens Axboee8462bd2009-07-06 12:59:04 +02001165 read_barrier();
1166 if (td->verify_thread_exit)
1167 break;
1168
1169 pthread_mutex_lock(&td->io_u_lock);
1170
1171 while (flist_empty(&td->verify_list) &&
1172 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +02001173 ret = pthread_cond_wait(&td->verify_cond,
1174 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +02001175 if (ret) {
1176 pthread_mutex_unlock(&td->io_u_lock);
1177 break;
1178 }
1179 }
1180
Jens Axboee53ab272009-07-06 13:43:46 +02001181 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001182 pthread_mutex_unlock(&td->io_u_lock);
1183
Jens Axboee53ab272009-07-06 13:43:46 +02001184 if (flist_empty(&list))
1185 continue;
1186
1187 while (!flist_empty(&list)) {
Jens Axboe2ae0b202013-05-28 14:16:55 +02001188 io_u = flist_entry(list.next, struct io_u, verify_list);
1189 flist_del(&io_u->verify_list);
Jens Axboee53ab272009-07-06 13:43:46 +02001190
Jens Axboed561f2a2009-07-06 13:51:05 +02001191 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +02001192 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +02001193 if (!ret)
1194 continue;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001195 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
Jens Axboed561f2a2009-07-06 13:51:05 +02001196 update_error_count(td, ret);
1197 td_clear_error(td);
1198 ret = 0;
1199 }
Jens Axboee53ab272009-07-06 13:43:46 +02001200 }
Jens Axboee8462bd2009-07-06 12:59:04 +02001201 } while (!ret);
1202
Jens Axboed561f2a2009-07-06 13:51:05 +02001203 if (ret) {
1204 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +02001205 if (td->o.verify_fatal)
1206 td->terminate = 1;
Jens Axboed561f2a2009-07-06 13:51:05 +02001207 }
1208
Jens Axboee8462bd2009-07-06 12:59:04 +02001209done:
1210 pthread_mutex_lock(&td->io_u_lock);
1211 td->nr_verify_threads--;
1212 pthread_mutex_unlock(&td->io_u_lock);
1213
1214 pthread_cond_signal(&td->free_cond);
1215 return NULL;
1216}
1217
1218int verify_async_init(struct thread_data *td)
1219{
1220 int i, ret;
bart Van Assche304a47c2010-08-01 21:31:26 +02001221 pthread_attr_t attr;
1222
1223 pthread_attr_init(&attr);
1224 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
Jens Axboee8462bd2009-07-06 12:59:04 +02001225
1226 td->verify_thread_exit = 0;
1227
1228 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1229 for (i = 0; i < td->o.verify_async; i++) {
bart Van Assche304a47c2010-08-01 21:31:26 +02001230 ret = pthread_create(&td->verify_threads[i], &attr,
Jens Axboee8462bd2009-07-06 12:59:04 +02001231 verify_async_thread, td);
1232 if (ret) {
1233 log_err("fio: async verify creation failed: %s\n",
1234 strerror(ret));
1235 break;
1236 }
1237 ret = pthread_detach(td->verify_threads[i]);
1238 if (ret) {
1239 log_err("fio: async verify thread detach failed: %s\n",
1240 strerror(ret));
1241 break;
1242 }
1243 td->nr_verify_threads++;
1244 }
1245
bart Van Assche304a47c2010-08-01 21:31:26 +02001246 pthread_attr_destroy(&attr);
1247
Jens Axboee8462bd2009-07-06 12:59:04 +02001248 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +02001249 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +02001250 td->verify_thread_exit = 1;
1251 write_barrier();
1252 pthread_cond_broadcast(&td->verify_cond);
1253 return 1;
1254 }
1255
1256 return 0;
1257}
1258
1259void verify_async_exit(struct thread_data *td)
1260{
1261 td->verify_thread_exit = 1;
1262 write_barrier();
1263 pthread_cond_broadcast(&td->verify_cond);
1264
1265 pthread_mutex_lock(&td->io_u_lock);
1266
1267 while (td->nr_verify_threads)
1268 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1269
1270 pthread_mutex_unlock(&td->io_u_lock);
1271 free(td->verify_threads);
1272 td->verify_threads = NULL;
1273}