blob: f592c55c509e6e811417bf73b654b9dfae8adce5 [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 Axboecd14cc12007-07-30 10:59:33 +020026
Jens Axboe7d9fb452011-01-11 22:16:49 +010027static void populate_hdr(struct thread_data *td, struct io_u *io_u,
28 struct verify_header *hdr, unsigned int header_num,
29 unsigned int header_len);
30
Jens Axboece35b1e2014-01-14 15:35:58 -070031static void fill_pattern(struct thread_data *td, void *p, unsigned int len,
32 char *pattern, unsigned int pattern_bytes)
Jens Axboe90059d62007-07-30 09:33:12 +020033{
Jens Axboece35b1e2014-01-14 15:35:58 -070034 switch (pattern_bytes) {
Jens Axboe90059d62007-07-30 09:33:12 +020035 case 0:
Jens Axboece35b1e2014-01-14 15:35:58 -070036 assert(0);
Jens Axboe90059d62007-07-30 09:33:12 +020037 break;
38 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010039 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Jens Axboece35b1e2014-01-14 15:35:58 -070040 memset(p, pattern[0], len);
Jens Axboe90059d62007-07-30 09:33:12 +020041 break;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010042 default: {
43 unsigned int i = 0, size = 0;
Jens Axboe90059d62007-07-30 09:33:12 +020044 unsigned char *b = p;
45
Jens Axboebd6f78b2008-02-01 20:27:52 +010046 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
Jens Axboece35b1e2014-01-14 15:35:58 -070047 pattern_bytes, len);
Jens Axboebd6f78b2008-02-01 20:27:52 +010048
Jens Axboe90059d62007-07-30 09:33:12 +020049 while (i < len) {
Jens Axboece35b1e2014-01-14 15:35:58 -070050 size = pattern_bytes;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010051 if (size > (len - i))
52 size = len - i;
Jens Axboece35b1e2014-01-14 15:35:58 -070053 memcpy(b+i, pattern, size);
Radha Ramachandran0e92f872009-10-27 20:14:27 +010054 i += size;
Jens Axboe90059d62007-07-30 09:33:12 +020055 }
56 break;
57 }
58 }
59}
60
Jens Axboece35b1e2014-01-14 15:35:58 -070061void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
62{
63 fill_pattern(td, p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
64}
65
66void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
67 struct io_u *io_u, unsigned long seed, int use_seed)
68{
69 if (!td->o.verify_pattern_bytes) {
70 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
71
72 if (use_seed)
73 __fill_random_buf(p, len, seed);
74 else
75 io_u->rand_seed = fill_random_buf(&td->buf_state, p, len);
76 return;
77 }
78
79 if (io_u->buf_filled_len >= len) {
80 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
81 td->o.verify_pattern_bytes, len);
82 return;
83 }
84
85 fill_pattern(td, p, len, td->o.verify_pattern, td->o.verify_pattern_bytes);
86
87 io_u->buf_filled_len = len;
88}
89
Jens Axboecfbcd0d2011-01-14 14:49:20 +010090static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
91{
92 unsigned int hdr_inc;
93
94 hdr_inc = io_u->buflen;
Jens Axboe8a99fdf2012-03-06 19:24:49 +010095 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
Jens Axboecfbcd0d2011-01-14 14:49:20 +010096 hdr_inc = td->o.verify_interval;
97
98 return hdr_inc;
99}
100
Jens Axboec50ca7b2011-01-12 08:31:54 +0100101static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
102 unsigned long seed, int use_seed)
103{
104 unsigned int hdr_inc, header_num;
105 struct verify_header *hdr;
106 void *p = io_u->buf;
107
Jens Axboece35b1e2014-01-14 15:35:58 -0700108 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100109
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100110 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100111 header_num = 0;
112 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
113 hdr = p;
114 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
115 header_num++;
116 }
117}
118
Jens Axboe4764aec2007-08-23 09:03:38 +0200119static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +0200120{
Shawn Lewisdee6de72007-08-02 22:17:52 +0200121 char swap[200];
122
123 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +0200124
Shawn Lewis546a9142007-07-28 21:11:37 +0200125 memcpy(&swap, buf1, len);
126 memcpy(buf1, buf2, len);
127 memcpy(buf2, &swap, len);
128}
129
Jens Axboee29d1b72006-10-18 15:43:15 +0200130static void hexdump(void *buffer, int len)
131{
132 unsigned char *p = buffer;
133 int i;
134
135 for (i = 0; i < len; i++)
Jens Axboebfacf382010-06-18 14:29:52 +0200136 log_err("%02x", p[i]);
137 log_err("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200138}
139
Jens Axboed9f2caf2007-07-28 21:22:03 +0200140/*
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700141 * Prepare for separation of verify_header and checksum header
Jens Axboe87677832007-07-30 12:08:14 +0200142 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200143static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200144{
Bruce Cran03e20d62011-01-02 20:14:54 +0100145 unsigned int len = 0;
Jens Axboe87677832007-07-30 12:08:14 +0200146
Jens Axboe546dfd92007-07-30 12:23:05 +0200147 switch (verify_type) {
148 case VERIFY_NONE:
149 case VERIFY_NULL:
150 len = 0;
151 break;
152 case VERIFY_MD5:
153 len = sizeof(struct vhdr_md5);
154 break;
155 case VERIFY_CRC64:
156 len = sizeof(struct vhdr_crc64);
157 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200158 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200159 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200160 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200161 len = sizeof(struct vhdr_crc32);
162 break;
163 case VERIFY_CRC16:
164 len = sizeof(struct vhdr_crc16);
165 break;
166 case VERIFY_CRC7:
167 len = sizeof(struct vhdr_crc7);
168 break;
169 case VERIFY_SHA256:
170 len = sizeof(struct vhdr_sha256);
171 break;
172 case VERIFY_SHA512:
173 len = sizeof(struct vhdr_sha512);
174 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200175 case VERIFY_META:
176 len = sizeof(struct vhdr_meta);
177 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200178 case VERIFY_SHA1:
179 len = sizeof(struct vhdr_sha1);
180 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100181 case VERIFY_PATTERN:
182 len = 0;
183 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200184 default:
185 log_err("fio: unknown verify header!\n");
186 assert(0);
187 }
188
189 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200190}
191
192static inline unsigned int hdr_size(struct verify_header *hdr)
193{
Jens Axboe546dfd92007-07-30 12:23:05 +0200194 return __hdr_size(hdr->verify_type);
195}
196
197static void *hdr_priv(struct verify_header *hdr)
198{
199 void *priv = hdr;
200
201 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200202}
203
204/*
Jens Axboe936216f2010-06-18 13:44:11 +0200205 * Verify container, pass info to verify handlers and allow them to
206 * pass info back in case of error
207 */
208struct vcont {
209 /*
210 * Input
211 */
212 struct io_u *io_u;
213 unsigned int hdr_num;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100214 struct thread_data *td;
Jens Axboe936216f2010-06-18 13:44:11 +0200215
216 /*
217 * Output, only valid in case of error
218 */
Jens Axboebfacf382010-06-18 14:29:52 +0200219 const char *name;
220 void *good_crc;
221 void *bad_crc;
Jens Axboe936216f2010-06-18 13:44:11 +0200222 unsigned int crc_len;
223};
224
Jens Axboec50ca7b2011-01-12 08:31:54 +0100225static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
226 const char *type, struct fio_file *f)
Jens Axboe7d9fb452011-01-11 22:16:49 +0100227{
Jens Axboe6f260b32011-01-13 18:57:54 +0100228 char *ptr, fname[256];
Jens Axboe7d9fb452011-01-11 22:16:49 +0100229 int ret, fd;
230
Jens Axboe6f260b32011-01-13 18:57:54 +0100231 ptr = strdup(f->file_name);
232 strcpy(fname, basename(ptr));
Jens Axboec50ca7b2011-01-12 08:31:54 +0100233
234 sprintf(fname + strlen(fname), ".%llu.%s", offset, type);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100235
236 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
237 if (fd < 0) {
238 perror("open verify buf file");
239 return;
240 }
241
242 while (len) {
243 ret = write(fd, buf, len);
244 if (!ret)
245 break;
246 else if (ret < 0) {
247 perror("write verify buf file");
248 break;
249 }
250 len -= ret;
251 buf += ret;
252 }
253
254 close(fd);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100255 log_err(" %s data dumped as %s\n", type, fname);
Jens Axboe6f260b32011-01-13 18:57:54 +0100256 free(ptr);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100257}
258
Jens Axboec50ca7b2011-01-12 08:31:54 +0100259/*
260 * Dump the contents of the read block and re-generate the correct data
261 * and dump that too.
262 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100263static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
264{
265 struct thread_data *td = vc->td;
266 struct io_u *io_u = vc->io_u;
267 unsigned long hdr_offset;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100268 struct io_u dummy;
Jens Axboec50ca7b2011-01-12 08:31:54 +0100269 void *buf;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100270
Steven Lang0dce9bc2011-10-25 22:41:05 +0200271 if (!td->o.verify_dump)
272 return;
273
Jens Axboec50ca7b2011-01-12 08:31:54 +0100274 /*
275 * Dump the contents we just read off disk
276 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100277 hdr_offset = vc->hdr_num * hdr->len;
278
Jens Axboec50ca7b2011-01-12 08:31:54 +0100279 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
280 "received", vc->io_u->file);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100281
Jens Axboec50ca7b2011-01-12 08:31:54 +0100282 /*
283 * Allocate a new buf and re-generate the original data
284 */
285 buf = malloc(io_u->buflen);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100286 dummy = *io_u;
Jens Axboec50ca7b2011-01-12 08:31:54 +0100287 dummy.buf = buf;
Jens Axboe4aae38a2011-01-12 08:59:12 +0100288 dummy.rand_seed = hdr->rand_seed;
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100289 dummy.buf_filled_len = 0;
Josef Bacik0d81daf2013-07-08 20:35:01 -0400290 dummy.buflen = io_u->buflen;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100291
Jens Axboec50ca7b2011-01-12 08:31:54 +0100292 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100293
Jens Axboec50ca7b2011-01-12 08:31:54 +0100294 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
295 "expected", vc->io_u->file);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100296 free(buf);
297}
298
Jens Axboebfacf382010-06-18 14:29:52 +0200299static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
300{
301 unsigned long long offset;
302
303 offset = vc->io_u->offset;
304 offset += vc->hdr_num * hdr->len;
Jens Axboe32c17ad2010-06-18 14:32:21 +0200305 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
306 vc->name, vc->io_u->file->file_name, offset, hdr->len);
Jens Axboebfacf382010-06-18 14:29:52 +0200307
308 if (vc->good_crc && vc->bad_crc) {
309 log_err(" Expected CRC: ");
310 hexdump(vc->good_crc, vc->crc_len);
311 log_err(" Received CRC: ");
312 hexdump(vc->bad_crc, vc->crc_len);
313 }
Jens Axboe7d9fb452011-01-11 22:16:49 +0100314
315 dump_verify_buffers(hdr, vc);
Jens Axboebfacf382010-06-18 14:29:52 +0200316}
317
Jens Axboe936216f2010-06-18 13:44:11 +0200318/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200319 * Return data area 'header_num'
320 */
Jens Axboe936216f2010-06-18 13:44:11 +0200321static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200322{
Jens Axboe936216f2010-06-18 13:44:11 +0200323 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200324}
325
Jens Axboe92bf48d2011-01-14 21:20:42 +0100326static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
327{
328 struct thread_data *td = vc->td;
329 struct io_u *io_u = vc->io_u;
330 char *buf, *pattern;
Jens Axboe2b13e712011-01-19 14:04:16 -0700331 unsigned int header_size = __hdr_size(td->o.verify);
Steven Lang9a2a86d2012-02-07 09:42:59 +0100332 unsigned int len, mod, i, size, pattern_size;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100333
334 pattern = td->o.verify_pattern;
Steven Lang9a2a86d2012-02-07 09:42:59 +0100335 pattern_size = td->o.verify_pattern_bytes;
336 if (pattern_size <= 1)
337 pattern_size = MAX_PATTERN_SIZE;
Jens Axboe2b13e712011-01-19 14:04:16 -0700338 buf = (void *) hdr + header_size;
339 len = get_hdr_inc(td, io_u) - header_size;
Steven Lang9a2a86d2012-02-07 09:42:59 +0100340 mod = header_size % pattern_size;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100341
Steven Lang9a2a86d2012-02-07 09:42:59 +0100342 for (i = 0; i < len; i += size) {
343 size = pattern_size - mod;
344 if (size > (len - i))
345 size = len - i;
346 if (memcmp(buf + i, pattern + mod, size))
Jens Axboee4ad68b2012-02-23 08:35:04 +0100347 /* Let the slow compare find the first mismatch byte. */
Steven Lang9a2a86d2012-02-07 09:42:59 +0100348 break;
349 mod = 0;
350 }
351
352 for (; i < len; i++) {
Jens Axboe92bf48d2011-01-14 21:20:42 +0100353 if (buf[i] != pattern[mod]) {
354 unsigned int bits;
355
356 bits = hweight8(buf[i] ^ pattern[mod]);
357 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
358 buf[i], pattern[mod], bits);
359 log_err("fio: bad pattern block offset %u\n", i);
360 dump_verify_buffers(hdr, vc);
361 return EILSEQ;
362 }
363 mod++;
364 if (mod == td->o.verify_pattern_bytes)
365 mod = 0;
366 }
367
368 return 0;
369}
370
371static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
372{
373 struct thread_data *td = vc->td;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200374 struct vhdr_meta *vh = hdr_priv(hdr);
Jens Axboe936216f2010-06-18 13:44:11 +0200375 struct io_u *io_u = vc->io_u;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100376 int ret = EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200377
Jens Axboebd6f78b2008-02-01 20:27:52 +0100378 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
379
Jens Axboebfacf382010-06-18 14:29:52 +0200380 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
Jens Axboe92bf48d2011-01-14 21:20:42 +0100381 ret = 0;
382
383 if (td->o.verify_pattern_bytes)
384 ret |= verify_io_u_pattern(hdr, vc);
385
Juan Casseda0a7bd2013-09-17 14:06:12 -0700386 /*
387 * For read-only workloads, the program cannot be certain of the
388 * last numberio written to a block. Checking of numberio will be done
389 * only for workloads that write data.
390 */
391 if (td_write(td) || td_rw(td))
392 if (vh->numberio != io_u->numberio)
393 ret = EILSEQ;
394
Jens Axboe92bf48d2011-01-14 21:20:42 +0100395 if (!ret)
Jens Axboebfacf382010-06-18 14:29:52 +0200396 return 0;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200397
Jens Axboebfacf382010-06-18 14:29:52 +0200398 vc->name = "meta";
399 log_verify_failure(hdr, vc);
Jens Axboe92bf48d2011-01-14 21:20:42 +0100400 return ret;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200401}
402
Jens Axboe936216f2010-06-18 13:44:11 +0200403static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200404{
Jens Axboe936216f2010-06-18 13:44:11 +0200405 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200406 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200407 uint8_t sha512[128];
Jens Axboe25dfa842012-02-29 10:01:34 +0100408 struct fio_sha512_ctx sha512_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200409 .buf = sha512,
410 };
411
Jens Axboe936216f2010-06-18 13:44:11 +0200412 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100413
Jens Axboe25dfa842012-02-29 10:01:34 +0100414 fio_sha512_init(&sha512_ctx);
415 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200416
Jens Axboebfacf382010-06-18 14:29:52 +0200417 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
418 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200419
Jens Axboebfacf382010-06-18 14:29:52 +0200420 vc->name = "sha512";
421 vc->good_crc = vh->sha512;
422 vc->bad_crc = sha512_ctx.buf;
423 vc->crc_len = sizeof(vh->sha512);
424 log_verify_failure(hdr, vc);
425 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200426}
427
Jens Axboe936216f2010-06-18 13:44:11 +0200428static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200429{
Jens Axboe936216f2010-06-18 13:44:11 +0200430 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200431 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100432 uint8_t sha256[64];
Jens Axboe25dfa842012-02-29 10:01:34 +0100433 struct fio_sha256_ctx sha256_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200434 .buf = sha256,
435 };
436
Jens Axboe936216f2010-06-18 13:44:11 +0200437 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100438
Jens Axboe25dfa842012-02-29 10:01:34 +0100439 fio_sha256_init(&sha256_ctx);
440 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200441
Jens Axboebfacf382010-06-18 14:29:52 +0200442 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
443 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200444
Jens Axboebfacf382010-06-18 14:29:52 +0200445 vc->name = "sha256";
446 vc->good_crc = vh->sha256;
447 vc->bad_crc = sha256_ctx.buf;
448 vc->crc_len = sizeof(vh->sha256);
449 log_verify_failure(hdr, vc);
450 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200451}
452
Jens Axboe936216f2010-06-18 13:44:11 +0200453static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200454{
Jens Axboe936216f2010-06-18 13:44:11 +0200455 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200456 struct vhdr_sha1 *vh = hdr_priv(hdr);
457 uint32_t sha1[5];
Jens Axboe25dfa842012-02-29 10:01:34 +0100458 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200459 .H = sha1,
460 };
461
Jens Axboe936216f2010-06-18 13:44:11 +0200462 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200463
Jens Axboe25dfa842012-02-29 10:01:34 +0100464 fio_sha1_init(&sha1_ctx);
465 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboe7c353ce2009-08-09 22:40:33 +0200466
Jens Axboebfacf382010-06-18 14:29:52 +0200467 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
468 return 0;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200469
Jens Axboebfacf382010-06-18 14:29:52 +0200470 vc->name = "sha1";
471 vc->good_crc = vh->sha1;
472 vc->bad_crc = sha1_ctx.H;
473 vc->crc_len = sizeof(vh->sha1);
474 log_verify_failure(hdr, vc);
475 return EILSEQ;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200476}
477
Jens Axboe936216f2010-06-18 13:44:11 +0200478static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200479{
Jens Axboe936216f2010-06-18 13:44:11 +0200480 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200481 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200482 unsigned char c;
483
Jens Axboe936216f2010-06-18 13:44:11 +0200484 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100485
Jens Axboe25dfa842012-02-29 10:01:34 +0100486 c = fio_crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200487
Jens Axboebfacf382010-06-18 14:29:52 +0200488 if (c == vh->crc7)
489 return 0;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200490
Jens Axboebfacf382010-06-18 14:29:52 +0200491 vc->name = "crc7";
492 vc->good_crc = &vh->crc7;
493 vc->bad_crc = &c;
494 vc->crc_len = 1;
495 log_verify_failure(hdr, vc);
496 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200497}
498
Jens Axboe936216f2010-06-18 13:44:11 +0200499static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200500{
Jens Axboe936216f2010-06-18 13:44:11 +0200501 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200502 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200503 unsigned short c;
504
Jens Axboe936216f2010-06-18 13:44:11 +0200505 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100506
Jens Axboe25dfa842012-02-29 10:01:34 +0100507 c = fio_crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200508
Jens Axboebfacf382010-06-18 14:29:52 +0200509 if (c == vh->crc16)
510 return 0;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200511
Jens Axboebfacf382010-06-18 14:29:52 +0200512 vc->name = "crc16";
513 vc->good_crc = &vh->crc16;
514 vc->bad_crc = &c;
515 vc->crc_len = 2;
516 log_verify_failure(hdr, vc);
517 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200518}
519
Jens Axboe936216f2010-06-18 13:44:11 +0200520static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200521{
Jens Axboe936216f2010-06-18 13:44:11 +0200522 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200523 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200524 unsigned long long c;
525
Jens Axboe936216f2010-06-18 13:44:11 +0200526 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100527
Jens Axboe25dfa842012-02-29 10:01:34 +0100528 c = fio_crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200529
Jens Axboebfacf382010-06-18 14:29:52 +0200530 if (c == vh->crc64)
531 return 0;
Jens Axboed77a7af2007-07-27 15:35:06 +0200532
Jens Axboebfacf382010-06-18 14:29:52 +0200533 vc->name = "crc64";
534 vc->good_crc = &vh->crc64;
535 vc->bad_crc = &c;
536 vc->crc_len = 8;
537 log_verify_failure(hdr, vc);
538 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200539}
540
Jens Axboe936216f2010-06-18 13:44:11 +0200541static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200542{
Jens Axboe936216f2010-06-18 13:44:11 +0200543 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200544 struct vhdr_crc32 *vh = hdr_priv(hdr);
545 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200546
Jens Axboe936216f2010-06-18 13:44:11 +0200547 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100548
Jens Axboe25dfa842012-02-29 10:01:34 +0100549 c = fio_crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200550
Jens Axboebfacf382010-06-18 14:29:52 +0200551 if (c == vh->crc32)
552 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200553
Jens Axboebfacf382010-06-18 14:29:52 +0200554 vc->name = "crc32";
555 vc->good_crc = &vh->crc32;
556 vc->bad_crc = &c;
557 vc->crc_len = 4;
558 log_verify_failure(hdr, vc);
559 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200560}
561
Jens Axboe936216f2010-06-18 13:44:11 +0200562static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200563{
Jens Axboe936216f2010-06-18 13:44:11 +0200564 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200565 struct vhdr_crc32 *vh = hdr_priv(hdr);
566 uint32_t c;
567
Jens Axboe936216f2010-06-18 13:44:11 +0200568 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200569
Jens Axboe25dfa842012-02-29 10:01:34 +0100570 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200571
Jens Axboebfacf382010-06-18 14:29:52 +0200572 if (c == vh->crc32)
573 return 0;
Jens Axboebac39e02008-06-11 20:46:19 +0200574
Jens Axboebfacf382010-06-18 14:29:52 +0200575 vc->name = "crc32c";
576 vc->good_crc = &vh->crc32;
577 vc->bad_crc = &c;
578 vc->crc_len = 4;
579 log_verify_failure(hdr, vc);
580 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200581}
582
Jens Axboe936216f2010-06-18 13:44:11 +0200583static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200584{
Jens Axboe936216f2010-06-18 13:44:11 +0200585 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200586 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200587 uint32_t hash[MD5_HASH_WORDS];
Jens Axboe25dfa842012-02-29 10:01:34 +0100588 struct fio_md5_ctx md5_ctx = {
Jens Axboe8c432322007-07-27 13:16:24 +0200589 .hash = hash,
590 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200591
Jens Axboe936216f2010-06-18 13:44:11 +0200592 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100593
Jens Axboe25dfa842012-02-29 10:01:34 +0100594 fio_md5_init(&md5_ctx);
595 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200596
Jens Axboebfacf382010-06-18 14:29:52 +0200597 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
598 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200599
Jens Axboebfacf382010-06-18 14:29:52 +0200600 vc->name = "md5";
601 vc->good_crc = vh->md5_digest;
602 vc->bad_crc = md5_ctx.hash;
603 vc->crc_len = sizeof(hash);
604 log_verify_failure(hdr, vc);
605 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200606}
607
Jens Axboee8462bd2009-07-06 12:59:04 +0200608/*
609 * Push IO verification to a separate thread
610 */
611int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
612{
613 if (io_u->file)
614 put_file_log(td, io_u->file);
615
Jens Axboee8462bd2009-07-06 12:59:04 +0200616 pthread_mutex_lock(&td->io_u_lock);
Steven Langd7ee2a72011-10-26 09:46:50 +0200617
Radha Ramachandran0c412142009-11-03 21:45:31 +0100618 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
619 td->cur_depth--;
620 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
621 }
Jens Axboe2ae0b202013-05-28 14:16:55 +0200622 flist_add_tail(&io_u->verify_list, &td->verify_list);
Jens Axboe2ecc1b52009-11-04 20:58:09 +0100623 io_u->flags |= IO_U_F_FREE_DEF;
Jens Axboee8462bd2009-07-06 12:59:04 +0200624 pthread_mutex_unlock(&td->io_u_lock);
625
626 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200627 return 0;
628}
629
Jens Axboe0d29de82010-09-01 13:54:15 +0200630static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
631{
632 static char zero_buf[1024];
633 unsigned int this_len, len;
634 int ret = 0;
635 void *p;
636
637 if (!td->o.trim_zero)
638 return 0;
639
640 len = io_u->buflen;
641 p = io_u->buf;
642 do {
643 this_len = sizeof(zero_buf);
644 if (this_len > len)
645 this_len = len;
646 if (memcmp(p, zero_buf, this_len)) {
647 ret = EILSEQ;
648 break;
649 }
650 len -= this_len;
651 p += this_len;
652 } while (len);
653
654 if (!ret)
655 return 0;
656
Jens Axboea917a8b2010-09-02 13:23:20 +0200657 log_err("trim: verify failed at file %s offset %llu, length %lu"
658 ", block offset %lu\n",
659 io_u->file->file_name, io_u->offset, io_u->buflen,
Jens Axboe2f681242010-10-21 08:15:59 +0200660 (unsigned long) (p - io_u->buf));
Jens Axboe0d29de82010-09-01 13:54:15 +0200661 return ret;
662}
663
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100664static int verify_header(struct io_u *io_u, struct verify_header *hdr)
Jens Axboef65d1c22012-02-22 20:11:57 +0100665{
666 void *p = hdr;
667 uint32_t crc;
668
Jens Axboeae38c0d2012-02-23 10:31:07 +0100669 if (hdr->magic != FIO_HDR_MAGIC)
670 return 0;
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100671 if (hdr->len > io_u->buflen) {
672 log_err("fio: verify header exceeds buffer length (%u > %lu)\n", hdr->len, io_u->buflen);
673 return 0;
674 }
Jens Axboeae38c0d2012-02-23 10:31:07 +0100675
Jens Axboe25dfa842012-02-29 10:01:34 +0100676 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100677 if (crc == hdr->crc32)
678 return 1;
679
680 log_err("fio: verify header crc %x, calculated %x\n", hdr->crc32, crc);
681 return 0;
682}
683
Jens Axboe36690c92007-03-26 10:23:34 +0200684int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200685{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200686 struct verify_header *hdr;
Jens Axboe2b13e712011-01-19 14:04:16 -0700687 unsigned int header_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200688 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200689 int ret;
690
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200691 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200692 return 0;
Jens Axboe0d29de82010-09-01 13:54:15 +0200693 if (io_u->flags & IO_U_F_TRIMMED) {
694 ret = verify_trimmed_io_u(td, io_u);
695 goto done;
696 }
Jens Axboe36690c92007-03-26 10:23:34 +0200697
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100698 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200699
Jens Axboea12a3b42007-08-09 10:20:54 +0200700 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100701 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
702 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200703 struct vcont vc = {
704 .io_u = io_u,
705 .hdr_num = hdr_num,
Jens Axboe7d9fb452011-01-11 22:16:49 +0100706 .td = td,
Jens Axboebfacf382010-06-18 14:29:52 +0200707 };
Jens Axboef00b2102012-11-30 09:39:02 +0100708 unsigned int verify_type;
Jens Axboe936216f2010-06-18 13:44:11 +0200709
Jens Axboef3e6cb92010-06-18 14:48:43 +0200710 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200711 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200712
Jens Axboe2b13e712011-01-19 14:04:16 -0700713 header_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200714 if (td->o.verify_offset)
Jens Axboe2b13e712011-01-19 14:04:16 -0700715 memswp(p, p + td->o.verify_offset, header_size);
Jens Axboe95646102007-07-28 21:17:50 +0200716 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200717
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100718 if (!verify_header(io_u, hdr)) {
Jens Axboeae38c0d2012-02-23 10:31:07 +0100719 log_err("verify: bad magic header %x, wanted %x at "
720 "file %s offset %llu, length %u\n",
Jens Axboed3a173a2012-02-23 08:23:18 +0100721 hdr->magic, FIO_HDR_MAGIC,
Jens Axboec9b44032010-06-18 14:46:06 +0200722 io_u->file->file_name,
723 io_u->offset + hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200724 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200725 }
726
Jens Axboef00b2102012-11-30 09:39:02 +0100727 if (td->o.verify != VERIFY_NONE)
728 verify_type = td->o.verify;
729 else
730 verify_type = hdr->verify_type;
731
732 switch (verify_type) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200733 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200734 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200735 break;
736 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200737 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200738 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200739 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200740 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200741 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200742 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200743 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200744 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200745 break;
746 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200747 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200748 break;
749 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200750 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200751 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200752 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200753 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200754 break;
755 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200756 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200757 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200758 case VERIFY_META:
Jens Axboe92bf48d2011-01-14 21:20:42 +0100759 ret = verify_io_u_meta(hdr, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200760 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200761 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200762 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200763 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100764 case VERIFY_PATTERN:
765 ret = verify_io_u_pattern(hdr, &vc);
766 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200767 default:
768 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200769 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200770 }
Jens Axboef00b2102012-11-30 09:39:02 +0100771
772 if (ret && verify_type != hdr->verify_type)
773 log_err("fio: verify type mismatch (%u media, %u given)\n",
774 hdr->verify_type, verify_type);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200775 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100776
Jens Axboe0d29de82010-09-01 13:54:15 +0200777done:
Jens Axboef3e6cb92010-06-18 14:48:43 +0200778 if (ret && td->o.verify_fatal)
779 td->terminate = 1;
780
Jens Axboea12a3b42007-08-09 10:20:54 +0200781 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200782}
783
Shawn Lewis7437ee82007-08-02 21:05:58 +0200784static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100785 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200786{
787 struct vhdr_meta *vh = hdr_priv(hdr);
788
789 vh->thread = td->thread_number;
790
791 vh->time_sec = io_u->start_time.tv_sec;
792 vh->time_usec = io_u->start_time.tv_usec;
793
Juan Casseda0a7bd2013-09-17 14:06:12 -0700794 vh->numberio = io_u->numberio;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200795
796 vh->offset = io_u->offset + header_num * td->o.verify_interval;
797}
798
Jens Axboecd14cc12007-07-30 10:59:33 +0200799static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
800{
Jens Axboe546dfd92007-07-30 12:23:05 +0200801 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100802 struct fio_sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200803 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200804 };
805
Jens Axboe25dfa842012-02-29 10:01:34 +0100806 fio_sha512_init(&sha512_ctx);
807 fio_sha512_update(&sha512_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200808}
809
810static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
811{
Jens Axboe546dfd92007-07-30 12:23:05 +0200812 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100813 struct fio_sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200814 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200815 };
816
Jens Axboe25dfa842012-02-29 10:01:34 +0100817 fio_sha256_init(&sha256_ctx);
818 fio_sha256_update(&sha256_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200819}
820
Jens Axboe7c353ce2009-08-09 22:40:33 +0200821static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
822{
823 struct vhdr_sha1 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100824 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200825 .H = vh->sha1,
826 };
827
Jens Axboe25dfa842012-02-29 10:01:34 +0100828 fio_sha1_init(&sha1_ctx);
829 fio_sha1_update(&sha1_ctx, p, len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200830}
831
Jens Axboe1e154bd2007-07-27 09:52:40 +0200832static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
833{
Jens Axboe546dfd92007-07-30 12:23:05 +0200834 struct vhdr_crc7 *vh = hdr_priv(hdr);
835
Jens Axboe25dfa842012-02-29 10:01:34 +0100836 vh->crc7 = fio_crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200837}
838
Jens Axboe969f7ed2007-07-27 09:07:17 +0200839static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
840{
Jens Axboe546dfd92007-07-30 12:23:05 +0200841 struct vhdr_crc16 *vh = hdr_priv(hdr);
842
Jens Axboe25dfa842012-02-29 10:01:34 +0100843 vh->crc16 = fio_crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200844}
845
Jens Axboee29d1b72006-10-18 15:43:15 +0200846static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
847{
Jens Axboe546dfd92007-07-30 12:23:05 +0200848 struct vhdr_crc32 *vh = hdr_priv(hdr);
849
Jens Axboe25dfa842012-02-29 10:01:34 +0100850 vh->crc32 = fio_crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200851}
852
Jens Axboebac39e02008-06-11 20:46:19 +0200853static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
854{
855 struct vhdr_crc32 *vh = hdr_priv(hdr);
856
Jens Axboe25dfa842012-02-29 10:01:34 +0100857 vh->crc32 = fio_crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200858}
859
Jens Axboed77a7af2007-07-27 15:35:06 +0200860static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
861{
Jens Axboe546dfd92007-07-30 12:23:05 +0200862 struct vhdr_crc64 *vh = hdr_priv(hdr);
863
Jens Axboe25dfa842012-02-29 10:01:34 +0100864 vh->crc64 = fio_crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200865}
866
Jens Axboee29d1b72006-10-18 15:43:15 +0200867static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
868{
Jens Axboe546dfd92007-07-30 12:23:05 +0200869 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100870 struct fio_md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200871 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200872 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200873
Jens Axboe25dfa842012-02-29 10:01:34 +0100874 fio_md5_init(&md5_ctx);
875 fio_md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200876}
877
Jens Axboe7d9fb452011-01-11 22:16:49 +0100878static void populate_hdr(struct thread_data *td, struct io_u *io_u,
879 struct verify_header *hdr, unsigned int header_num,
880 unsigned int header_len)
881{
882 unsigned int data_len;
883 void *data, *p;
884
885 p = (void *) hdr;
886
Jens Axboed3a173a2012-02-23 08:23:18 +0100887 hdr->magic = FIO_HDR_MAGIC;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100888 hdr->verify_type = td->o.verify;
Jens Axboed3a173a2012-02-23 08:23:18 +0100889 hdr->len = header_len;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100890 hdr->rand_seed = io_u->rand_seed;
Jens Axboe25dfa842012-02-29 10:01:34 +0100891 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100892
Jens Axboe7d9fb452011-01-11 22:16:49 +0100893 data_len = header_len - hdr_size(hdr);
894
895 data = p + hdr_size(hdr);
896 switch (td->o.verify) {
897 case VERIFY_MD5:
898 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
899 io_u, hdr->len);
900 fill_md5(hdr, data, data_len);
901 break;
902 case VERIFY_CRC64:
903 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
904 io_u, hdr->len);
905 fill_crc64(hdr, data, data_len);
906 break;
907 case VERIFY_CRC32C:
908 case VERIFY_CRC32C_INTEL:
909 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
910 io_u, hdr->len);
911 fill_crc32c(hdr, data, data_len);
912 break;
913 case VERIFY_CRC32:
914 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
915 io_u, hdr->len);
916 fill_crc32(hdr, data, data_len);
917 break;
918 case VERIFY_CRC16:
919 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
920 io_u, hdr->len);
921 fill_crc16(hdr, data, data_len);
922 break;
923 case VERIFY_CRC7:
924 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
925 io_u, hdr->len);
926 fill_crc7(hdr, data, data_len);
927 break;
928 case VERIFY_SHA256:
929 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
930 io_u, hdr->len);
931 fill_sha256(hdr, data, data_len);
932 break;
933 case VERIFY_SHA512:
934 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
935 io_u, hdr->len);
936 fill_sha512(hdr, data, data_len);
937 break;
938 case VERIFY_META:
939 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
940 io_u, hdr->len);
941 fill_meta(hdr, td, io_u, header_num);
942 break;
943 case VERIFY_SHA1:
944 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
945 io_u, hdr->len);
946 fill_sha1(hdr, data, data_len);
947 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100948 case VERIFY_PATTERN:
949 /* nothing to do here */
950 break;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100951 default:
952 log_err("fio: bad verify type: %d\n", td->o.verify);
953 assert(0);
954 }
955 if (td->o.verify_offset)
956 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
957}
958
Jens Axboee29d1b72006-10-18 15:43:15 +0200959/*
960 * fill body of io_u->buf with random data and add a header with the
Jens Axboec50ca7b2011-01-12 08:31:54 +0100961 * checksum of choice
Jens Axboee29d1b72006-10-18 15:43:15 +0200962 */
963void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
964{
Jens Axboe9cc3d152007-03-26 10:32:30 +0200965 if (td->o.verify == VERIFY_NULL)
966 return;
967
Juan Casseda0a7bd2013-09-17 14:06:12 -0700968 io_u->numberio = td->io_issues[io_u->ddir];
969
Jens Axboec50ca7b2011-01-12 08:31:54 +0100970 fill_pattern_headers(td, io_u, 0, 0);
Jens Axboee29d1b72006-10-18 15:43:15 +0200971}
972
973int get_next_verify(struct thread_data *td, struct io_u *io_u)
974{
Jens Axboe8de8f042007-03-27 10:36:12 +0200975 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200976
Jens Axboed2d7fa52007-02-19 13:21:35 +0100977 /*
978 * this io_u is from a requeue, we already filled the offsets
979 */
980 if (io_u->file)
981 return 0;
982
Jens Axboe8de8f042007-03-27 10:36:12 +0200983 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
984 struct rb_node *n = rb_first(&td->io_hist_tree);
985
Jens Axboe4b878982007-03-26 09:32:22 +0200986 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200987 rb_erase(n, &td->io_hist_tree);
Jens Axboea917a8b2010-09-02 13:23:20 +0200988 assert(ipo->flags & IP_F_ONRB);
989 ipo->flags &= ~IP_F_ONRB;
Jens Axboe01743ee2008-06-02 12:19:19 +0200990 } else if (!flist_empty(&td->io_hist_list)) {
991 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
992 flist_del(&ipo->list);
Jens Axboea917a8b2010-09-02 13:23:20 +0200993 assert(ipo->flags & IP_F_ONLIST);
994 ipo->flags &= ~IP_F_ONLIST;
Jens Axboe8de8f042007-03-27 10:36:12 +0200995 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200996
Jens Axboe8de8f042007-03-27 10:36:12 +0200997 if (ipo) {
Jens Axboe0d29de82010-09-01 13:54:15 +0200998 td->io_hist_len--;
999
Jens Axboee29d1b72006-10-18 15:43:15 +02001000 io_u->offset = ipo->offset;
1001 io_u->buflen = ipo->len;
Juan Casseda0a7bd2013-09-17 14:06:12 -07001002 io_u->numberio = ipo->numberio;
Jens Axboe36167d82007-02-18 05:41:31 +01001003 io_u->file = ipo->file;
Jens Axboe82af2a72012-03-13 13:45:58 +01001004 io_u->flags |= IO_U_F_VER_LIST;
Jens Axboe97af62c2007-05-22 11:12:13 +02001005
Jens Axboe0d29de82010-09-01 13:54:15 +02001006 if (ipo->flags & IP_F_TRIMMED)
1007 io_u->flags |= IO_U_F_TRIMMED;
1008
Jens Axboed6aed792009-06-03 08:41:15 +02001009 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +02001010 int r = td_io_open_file(td, io_u->file);
1011
Jens Axboebd6f78b2008-02-01 20:27:52 +01001012 if (r) {
1013 dprint(FD_VERIFY, "failed file %s open\n",
1014 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +02001015 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +01001016 }
Jens Axboe97af62c2007-05-22 11:12:13 +02001017 }
1018
1019 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +02001020 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +02001021 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +01001022 io_u->xfer_buf = io_u->buf;
1023 io_u->xfer_buflen = io_u->buflen;
Jens Axboe0d29de82010-09-01 13:54:15 +02001024
1025 remove_trim_entry(td, ipo);
Jens Axboee29d1b72006-10-18 15:43:15 +02001026 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001027 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +02001028 return 0;
1029 }
1030
Jens Axboebd6f78b2008-02-01 20:27:52 +01001031 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +02001032 return 1;
1033}
Jens Axboee8462bd2009-07-06 12:59:04 +02001034
Jens Axboedc5bfbb2013-04-10 22:23:40 +02001035void fio_verify_init(struct thread_data *td)
1036{
1037 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1038 td->o.verify == VERIFY_CRC32C) {
1039 crc32c_intel_probe();
1040 }
1041}
1042
Jens Axboee8462bd2009-07-06 12:59:04 +02001043static void *verify_async_thread(void *data)
1044{
1045 struct thread_data *td = data;
1046 struct io_u *io_u;
1047 int ret = 0;
1048
1049 if (td->o.verify_cpumask_set &&
1050 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1051 log_err("fio: failed setting verify thread affinity\n");
1052 goto done;
1053 }
1054
1055 do {
Jens Axboee53ab272009-07-06 13:43:46 +02001056 FLIST_HEAD(list);
1057
Jens Axboee8462bd2009-07-06 12:59:04 +02001058 read_barrier();
1059 if (td->verify_thread_exit)
1060 break;
1061
1062 pthread_mutex_lock(&td->io_u_lock);
1063
1064 while (flist_empty(&td->verify_list) &&
1065 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +02001066 ret = pthread_cond_wait(&td->verify_cond,
1067 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +02001068 if (ret) {
1069 pthread_mutex_unlock(&td->io_u_lock);
1070 break;
1071 }
1072 }
1073
Jens Axboee53ab272009-07-06 13:43:46 +02001074 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001075 pthread_mutex_unlock(&td->io_u_lock);
1076
Jens Axboee53ab272009-07-06 13:43:46 +02001077 if (flist_empty(&list))
1078 continue;
1079
1080 while (!flist_empty(&list)) {
Jens Axboe2ae0b202013-05-28 14:16:55 +02001081 io_u = flist_entry(list.next, struct io_u, verify_list);
1082 flist_del(&io_u->verify_list);
Jens Axboee53ab272009-07-06 13:43:46 +02001083
Jens Axboed561f2a2009-07-06 13:51:05 +02001084 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +02001085 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +02001086 if (!ret)
1087 continue;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001088 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
Jens Axboed561f2a2009-07-06 13:51:05 +02001089 update_error_count(td, ret);
1090 td_clear_error(td);
1091 ret = 0;
1092 }
Jens Axboee53ab272009-07-06 13:43:46 +02001093 }
Jens Axboee8462bd2009-07-06 12:59:04 +02001094 } while (!ret);
1095
Jens Axboed561f2a2009-07-06 13:51:05 +02001096 if (ret) {
1097 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +02001098 if (td->o.verify_fatal)
1099 td->terminate = 1;
Jens Axboed561f2a2009-07-06 13:51:05 +02001100 }
1101
Jens Axboee8462bd2009-07-06 12:59:04 +02001102done:
1103 pthread_mutex_lock(&td->io_u_lock);
1104 td->nr_verify_threads--;
1105 pthread_mutex_unlock(&td->io_u_lock);
1106
1107 pthread_cond_signal(&td->free_cond);
1108 return NULL;
1109}
1110
1111int verify_async_init(struct thread_data *td)
1112{
1113 int i, ret;
bart Van Assche304a47c2010-08-01 21:31:26 +02001114 pthread_attr_t attr;
1115
1116 pthread_attr_init(&attr);
1117 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
Jens Axboee8462bd2009-07-06 12:59:04 +02001118
1119 td->verify_thread_exit = 0;
1120
1121 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1122 for (i = 0; i < td->o.verify_async; i++) {
bart Van Assche304a47c2010-08-01 21:31:26 +02001123 ret = pthread_create(&td->verify_threads[i], &attr,
Jens Axboee8462bd2009-07-06 12:59:04 +02001124 verify_async_thread, td);
1125 if (ret) {
1126 log_err("fio: async verify creation failed: %s\n",
1127 strerror(ret));
1128 break;
1129 }
1130 ret = pthread_detach(td->verify_threads[i]);
1131 if (ret) {
1132 log_err("fio: async verify thread detach failed: %s\n",
1133 strerror(ret));
1134 break;
1135 }
1136 td->nr_verify_threads++;
1137 }
1138
bart Van Assche304a47c2010-08-01 21:31:26 +02001139 pthread_attr_destroy(&attr);
1140
Jens Axboee8462bd2009-07-06 12:59:04 +02001141 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +02001142 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +02001143 td->verify_thread_exit = 1;
1144 write_barrier();
1145 pthread_cond_broadcast(&td->verify_cond);
1146 return 1;
1147 }
1148
1149 return 0;
1150}
1151
1152void verify_async_exit(struct thread_data *td)
1153{
1154 td->verify_thread_exit = 1;
1155 write_barrier();
1156 pthread_cond_broadcast(&td->verify_cond);
1157
1158 pthread_mutex_lock(&td->io_u_lock);
1159
1160 while (td->nr_verify_threads)
1161 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1162
1163 pthread_mutex_unlock(&td->io_u_lock);
1164 free(td->verify_threads);
1165 td->verify_threads = NULL;
1166}