blob: 93731228f1b68661e0edd3293bc491ccb650774d [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
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -070075 io_u->rand_seed = fill_random_buf(&td->__verify_state, p, len);
Jens Axboece35b1e2014-01-14 15:35:58 -070076 return;
77 }
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -070078
Jens Axboece35b1e2014-01-14 15:35:58 -070079 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.
Juan Casse62167762013-09-17 14:06:13 -0700390 * For verify_only, numberio will be checked in the last iteration when
391 * the correct state of numberio, that would have been written to each
392 * block in a previous run of fio, has been reached.
Juan Casseda0a7bd2013-09-17 14:06:12 -0700393 */
394 if (td_write(td) || td_rw(td))
Juan Casse62167762013-09-17 14:06:13 -0700395 if (!td->o.verify_only || td->o.loops == 0)
396 if (vh->numberio != io_u->numberio)
397 ret = EILSEQ;
Juan Casseda0a7bd2013-09-17 14:06:12 -0700398
Jens Axboe92bf48d2011-01-14 21:20:42 +0100399 if (!ret)
Jens Axboebfacf382010-06-18 14:29:52 +0200400 return 0;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200401
Jens Axboebfacf382010-06-18 14:29:52 +0200402 vc->name = "meta";
403 log_verify_failure(hdr, vc);
Jens Axboe92bf48d2011-01-14 21:20:42 +0100404 return ret;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200405}
406
Jens Axboe936216f2010-06-18 13:44:11 +0200407static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200408{
Jens Axboe936216f2010-06-18 13:44:11 +0200409 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200410 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200411 uint8_t sha512[128];
Jens Axboe25dfa842012-02-29 10:01:34 +0100412 struct fio_sha512_ctx sha512_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200413 .buf = sha512,
414 };
415
Jens Axboe936216f2010-06-18 13:44:11 +0200416 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100417
Jens Axboe25dfa842012-02-29 10:01:34 +0100418 fio_sha512_init(&sha512_ctx);
419 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200420
Jens Axboebfacf382010-06-18 14:29:52 +0200421 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
422 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200423
Jens Axboebfacf382010-06-18 14:29:52 +0200424 vc->name = "sha512";
425 vc->good_crc = vh->sha512;
426 vc->bad_crc = sha512_ctx.buf;
427 vc->crc_len = sizeof(vh->sha512);
428 log_verify_failure(hdr, vc);
429 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200430}
431
Jens Axboe936216f2010-06-18 13:44:11 +0200432static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200433{
Jens Axboe936216f2010-06-18 13:44:11 +0200434 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200435 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100436 uint8_t sha256[64];
Jens Axboe25dfa842012-02-29 10:01:34 +0100437 struct fio_sha256_ctx sha256_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200438 .buf = sha256,
439 };
440
Jens Axboe936216f2010-06-18 13:44:11 +0200441 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100442
Jens Axboe25dfa842012-02-29 10:01:34 +0100443 fio_sha256_init(&sha256_ctx);
444 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200445
Jens Axboebfacf382010-06-18 14:29:52 +0200446 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
447 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200448
Jens Axboebfacf382010-06-18 14:29:52 +0200449 vc->name = "sha256";
450 vc->good_crc = vh->sha256;
451 vc->bad_crc = sha256_ctx.buf;
452 vc->crc_len = sizeof(vh->sha256);
453 log_verify_failure(hdr, vc);
454 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200455}
456
Jens Axboe936216f2010-06-18 13:44:11 +0200457static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200458{
Jens Axboe936216f2010-06-18 13:44:11 +0200459 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200460 struct vhdr_sha1 *vh = hdr_priv(hdr);
461 uint32_t sha1[5];
Jens Axboe25dfa842012-02-29 10:01:34 +0100462 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200463 .H = sha1,
464 };
465
Jens Axboe936216f2010-06-18 13:44:11 +0200466 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200467
Jens Axboe25dfa842012-02-29 10:01:34 +0100468 fio_sha1_init(&sha1_ctx);
469 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboe7c353ce2009-08-09 22:40:33 +0200470
Jens Axboebfacf382010-06-18 14:29:52 +0200471 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
472 return 0;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200473
Jens Axboebfacf382010-06-18 14:29:52 +0200474 vc->name = "sha1";
475 vc->good_crc = vh->sha1;
476 vc->bad_crc = sha1_ctx.H;
477 vc->crc_len = sizeof(vh->sha1);
478 log_verify_failure(hdr, vc);
479 return EILSEQ;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200480}
481
Jens Axboe936216f2010-06-18 13:44:11 +0200482static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200483{
Jens Axboe936216f2010-06-18 13:44:11 +0200484 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200485 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200486 unsigned char c;
487
Jens Axboe936216f2010-06-18 13:44:11 +0200488 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100489
Jens Axboe25dfa842012-02-29 10:01:34 +0100490 c = fio_crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200491
Jens Axboebfacf382010-06-18 14:29:52 +0200492 if (c == vh->crc7)
493 return 0;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200494
Jens Axboebfacf382010-06-18 14:29:52 +0200495 vc->name = "crc7";
496 vc->good_crc = &vh->crc7;
497 vc->bad_crc = &c;
498 vc->crc_len = 1;
499 log_verify_failure(hdr, vc);
500 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200501}
502
Jens Axboe936216f2010-06-18 13:44:11 +0200503static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200504{
Jens Axboe936216f2010-06-18 13:44:11 +0200505 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200506 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200507 unsigned short c;
508
Jens Axboe936216f2010-06-18 13:44:11 +0200509 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100510
Jens Axboe25dfa842012-02-29 10:01:34 +0100511 c = fio_crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200512
Jens Axboebfacf382010-06-18 14:29:52 +0200513 if (c == vh->crc16)
514 return 0;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200515
Jens Axboebfacf382010-06-18 14:29:52 +0200516 vc->name = "crc16";
517 vc->good_crc = &vh->crc16;
518 vc->bad_crc = &c;
519 vc->crc_len = 2;
520 log_verify_failure(hdr, vc);
521 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200522}
523
Jens Axboe936216f2010-06-18 13:44:11 +0200524static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200525{
Jens Axboe936216f2010-06-18 13:44:11 +0200526 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200527 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200528 unsigned long long c;
529
Jens Axboe936216f2010-06-18 13:44:11 +0200530 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100531
Jens Axboe25dfa842012-02-29 10:01:34 +0100532 c = fio_crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200533
Jens Axboebfacf382010-06-18 14:29:52 +0200534 if (c == vh->crc64)
535 return 0;
Jens Axboed77a7af2007-07-27 15:35:06 +0200536
Jens Axboebfacf382010-06-18 14:29:52 +0200537 vc->name = "crc64";
538 vc->good_crc = &vh->crc64;
539 vc->bad_crc = &c;
540 vc->crc_len = 8;
541 log_verify_failure(hdr, vc);
542 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200543}
544
Jens Axboe936216f2010-06-18 13:44:11 +0200545static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200546{
Jens Axboe936216f2010-06-18 13:44:11 +0200547 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200548 struct vhdr_crc32 *vh = hdr_priv(hdr);
549 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200550
Jens Axboe936216f2010-06-18 13:44:11 +0200551 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100552
Jens Axboe25dfa842012-02-29 10:01:34 +0100553 c = fio_crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200554
Jens Axboebfacf382010-06-18 14:29:52 +0200555 if (c == vh->crc32)
556 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200557
Jens Axboebfacf382010-06-18 14:29:52 +0200558 vc->name = "crc32";
559 vc->good_crc = &vh->crc32;
560 vc->bad_crc = &c;
561 vc->crc_len = 4;
562 log_verify_failure(hdr, vc);
563 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200564}
565
Jens Axboe936216f2010-06-18 13:44:11 +0200566static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200567{
Jens Axboe936216f2010-06-18 13:44:11 +0200568 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200569 struct vhdr_crc32 *vh = hdr_priv(hdr);
570 uint32_t c;
571
Jens Axboe936216f2010-06-18 13:44:11 +0200572 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200573
Jens Axboe25dfa842012-02-29 10:01:34 +0100574 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200575
Jens Axboebfacf382010-06-18 14:29:52 +0200576 if (c == vh->crc32)
577 return 0;
Jens Axboebac39e02008-06-11 20:46:19 +0200578
Jens Axboebfacf382010-06-18 14:29:52 +0200579 vc->name = "crc32c";
580 vc->good_crc = &vh->crc32;
581 vc->bad_crc = &c;
582 vc->crc_len = 4;
583 log_verify_failure(hdr, vc);
584 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200585}
586
Jens Axboe936216f2010-06-18 13:44:11 +0200587static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200588{
Jens Axboe936216f2010-06-18 13:44:11 +0200589 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200590 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200591 uint32_t hash[MD5_HASH_WORDS];
Jens Axboe25dfa842012-02-29 10:01:34 +0100592 struct fio_md5_ctx md5_ctx = {
Jens Axboe8c432322007-07-27 13:16:24 +0200593 .hash = hash,
594 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200595
Jens Axboe936216f2010-06-18 13:44:11 +0200596 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100597
Jens Axboe25dfa842012-02-29 10:01:34 +0100598 fio_md5_init(&md5_ctx);
599 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200600
Jens Axboebfacf382010-06-18 14:29:52 +0200601 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
602 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200603
Jens Axboebfacf382010-06-18 14:29:52 +0200604 vc->name = "md5";
605 vc->good_crc = vh->md5_digest;
606 vc->bad_crc = md5_ctx.hash;
607 vc->crc_len = sizeof(hash);
608 log_verify_failure(hdr, vc);
609 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200610}
611
Jens Axboee8462bd2009-07-06 12:59:04 +0200612/*
613 * Push IO verification to a separate thread
614 */
615int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
616{
617 if (io_u->file)
618 put_file_log(td, io_u->file);
619
Jens Axboee8462bd2009-07-06 12:59:04 +0200620 pthread_mutex_lock(&td->io_u_lock);
Steven Langd7ee2a72011-10-26 09:46:50 +0200621
Radha Ramachandran0c412142009-11-03 21:45:31 +0100622 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
623 td->cur_depth--;
624 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
625 }
Jens Axboe2ae0b202013-05-28 14:16:55 +0200626 flist_add_tail(&io_u->verify_list, &td->verify_list);
Jens Axboe2ecc1b52009-11-04 20:58:09 +0100627 io_u->flags |= IO_U_F_FREE_DEF;
Jens Axboee8462bd2009-07-06 12:59:04 +0200628 pthread_mutex_unlock(&td->io_u_lock);
629
630 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200631 return 0;
632}
633
Jens Axboe0d29de82010-09-01 13:54:15 +0200634static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
635{
636 static char zero_buf[1024];
637 unsigned int this_len, len;
638 int ret = 0;
639 void *p;
640
641 if (!td->o.trim_zero)
642 return 0;
643
644 len = io_u->buflen;
645 p = io_u->buf;
646 do {
647 this_len = sizeof(zero_buf);
648 if (this_len > len)
649 this_len = len;
650 if (memcmp(p, zero_buf, this_len)) {
651 ret = EILSEQ;
652 break;
653 }
654 len -= this_len;
655 p += this_len;
656 } while (len);
657
658 if (!ret)
659 return 0;
660
Jens Axboea917a8b2010-09-02 13:23:20 +0200661 log_err("trim: verify failed at file %s offset %llu, length %lu"
662 ", block offset %lu\n",
663 io_u->file->file_name, io_u->offset, io_u->buflen,
Jens Axboe2f681242010-10-21 08:15:59 +0200664 (unsigned long) (p - io_u->buf));
Jens Axboe0d29de82010-09-01 13:54:15 +0200665 return ret;
666}
667
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100668static int verify_header(struct io_u *io_u, struct verify_header *hdr)
Jens Axboef65d1c22012-02-22 20:11:57 +0100669{
670 void *p = hdr;
671 uint32_t crc;
672
Jens Axboeae38c0d2012-02-23 10:31:07 +0100673 if (hdr->magic != FIO_HDR_MAGIC)
Juan Cassee9e177c2013-09-17 14:06:14 -0700674 return 1;
675 if (hdr->len > io_u->buflen)
676 return 2;
677 if (hdr->rand_seed != io_u->rand_seed)
678 return 3;
Jens Axboeae38c0d2012-02-23 10:31:07 +0100679
Jens Axboe25dfa842012-02-29 10:01:34 +0100680 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100681 if (crc == hdr->crc32)
Juan Cassee9e177c2013-09-17 14:06:14 -0700682 return 0;
Jens Axboef65d1c22012-02-22 20:11:57 +0100683 log_err("fio: verify header crc %x, calculated %x\n", hdr->crc32, crc);
Juan Cassee9e177c2013-09-17 14:06:14 -0700684 return 4;
Jens Axboef65d1c22012-02-22 20:11:57 +0100685}
686
Jens Axboe36690c92007-03-26 10:23:34 +0200687int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200688{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200689 struct verify_header *hdr;
Jens Axboe2b13e712011-01-19 14:04:16 -0700690 unsigned int header_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200691 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200692 int ret;
693
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200694 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200695 return 0;
Jens Axboe0d29de82010-09-01 13:54:15 +0200696 if (io_u->flags & IO_U_F_TRIMMED) {
697 ret = verify_trimmed_io_u(td, io_u);
698 goto done;
699 }
Jens Axboe36690c92007-03-26 10:23:34 +0200700
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100701 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200702
Jens Axboea12a3b42007-08-09 10:20:54 +0200703 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100704 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
705 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200706 struct vcont vc = {
707 .io_u = io_u,
708 .hdr_num = hdr_num,
Jens Axboe7d9fb452011-01-11 22:16:49 +0100709 .td = td,
Jens Axboebfacf382010-06-18 14:29:52 +0200710 };
Jens Axboef00b2102012-11-30 09:39:02 +0100711 unsigned int verify_type;
Jens Axboe936216f2010-06-18 13:44:11 +0200712
Jens Axboef3e6cb92010-06-18 14:48:43 +0200713 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200714 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200715
Jens Axboe2b13e712011-01-19 14:04:16 -0700716 header_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200717 if (td->o.verify_offset)
Jens Axboe2b13e712011-01-19 14:04:16 -0700718 memswp(p, p + td->o.verify_offset, header_size);
Jens Axboe95646102007-07-28 21:17:50 +0200719 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200720
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -0700721 /*
722 * Make rand_seed check pass when have verifysort or
723 * verify_backlog.
724 */
725 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
726 io_u->rand_seed = hdr->rand_seed;
727
Juan Cassee9e177c2013-09-17 14:06:14 -0700728 ret = verify_header(io_u, hdr);
729 switch (ret) {
730 case 0:
731 break;
732 case 1:
Jens Axboeae38c0d2012-02-23 10:31:07 +0100733 log_err("verify: bad magic header %x, wanted %x at "
734 "file %s offset %llu, length %u\n",
Jens Axboed3a173a2012-02-23 08:23:18 +0100735 hdr->magic, FIO_HDR_MAGIC,
Jens Axboec9b44032010-06-18 14:46:06 +0200736 io_u->file->file_name,
737 io_u->offset + hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200738 return EILSEQ;
Juan Cassee9e177c2013-09-17 14:06:14 -0700739 break;
740 case 2:
741 log_err("fio: verify header exceeds buffer length (%u "
742 "> %lu)\n", hdr->len, io_u->buflen);
743 return EILSEQ;
744 break;
745 case 3:
746 log_err("verify: bad header rand_seed %"PRIu64
747 ", wanted %"PRIu64" at file %s offset %llu, "
748 "length %u\n",
749 hdr->rand_seed, io_u->rand_seed,
750 io_u->file->file_name,
751 io_u->offset + hdr_num * hdr->len, hdr->len);
752 return EILSEQ;
753 break;
754 case 4:
755 return EILSEQ;
756 break;
757 default:
758 log_err("verify: unknown header error at file %s "
759 "offset %llu, length %u\n",
760 io_u->file->file_name,
761 io_u->offset + hdr_num * hdr->len, hdr->len);
762 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200763 }
764
Jens Axboef00b2102012-11-30 09:39:02 +0100765 if (td->o.verify != VERIFY_NONE)
766 verify_type = td->o.verify;
767 else
768 verify_type = hdr->verify_type;
769
770 switch (verify_type) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200771 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200772 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200773 break;
774 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200775 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200776 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200777 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200778 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200779 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200780 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200781 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200782 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200783 break;
784 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200785 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200786 break;
787 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200788 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200789 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200790 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200791 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200792 break;
793 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200794 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200795 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200796 case VERIFY_META:
Jens Axboe92bf48d2011-01-14 21:20:42 +0100797 ret = verify_io_u_meta(hdr, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200798 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200799 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200800 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200801 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100802 case VERIFY_PATTERN:
803 ret = verify_io_u_pattern(hdr, &vc);
804 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200805 default:
806 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200807 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200808 }
Jens Axboef00b2102012-11-30 09:39:02 +0100809
810 if (ret && verify_type != hdr->verify_type)
811 log_err("fio: verify type mismatch (%u media, %u given)\n",
812 hdr->verify_type, verify_type);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200813 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100814
Jens Axboe0d29de82010-09-01 13:54:15 +0200815done:
Jens Axboef3e6cb92010-06-18 14:48:43 +0200816 if (ret && td->o.verify_fatal)
817 td->terminate = 1;
818
Jens Axboea12a3b42007-08-09 10:20:54 +0200819 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200820}
821
Shawn Lewis7437ee82007-08-02 21:05:58 +0200822static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100823 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200824{
825 struct vhdr_meta *vh = hdr_priv(hdr);
826
827 vh->thread = td->thread_number;
828
829 vh->time_sec = io_u->start_time.tv_sec;
830 vh->time_usec = io_u->start_time.tv_usec;
831
Juan Casseda0a7bd2013-09-17 14:06:12 -0700832 vh->numberio = io_u->numberio;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200833
834 vh->offset = io_u->offset + header_num * td->o.verify_interval;
835}
836
Jens Axboecd14cc12007-07-30 10:59:33 +0200837static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
838{
Jens Axboe546dfd92007-07-30 12:23:05 +0200839 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100840 struct fio_sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200841 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200842 };
843
Jens Axboe25dfa842012-02-29 10:01:34 +0100844 fio_sha512_init(&sha512_ctx);
845 fio_sha512_update(&sha512_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200846}
847
848static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
849{
Jens Axboe546dfd92007-07-30 12:23:05 +0200850 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100851 struct fio_sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200852 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200853 };
854
Jens Axboe25dfa842012-02-29 10:01:34 +0100855 fio_sha256_init(&sha256_ctx);
856 fio_sha256_update(&sha256_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200857}
858
Jens Axboe7c353ce2009-08-09 22:40:33 +0200859static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
860{
861 struct vhdr_sha1 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100862 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200863 .H = vh->sha1,
864 };
865
Jens Axboe25dfa842012-02-29 10:01:34 +0100866 fio_sha1_init(&sha1_ctx);
867 fio_sha1_update(&sha1_ctx, p, len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200868}
869
Jens Axboe1e154bd2007-07-27 09:52:40 +0200870static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
871{
Jens Axboe546dfd92007-07-30 12:23:05 +0200872 struct vhdr_crc7 *vh = hdr_priv(hdr);
873
Jens Axboe25dfa842012-02-29 10:01:34 +0100874 vh->crc7 = fio_crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200875}
876
Jens Axboe969f7ed2007-07-27 09:07:17 +0200877static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
878{
Jens Axboe546dfd92007-07-30 12:23:05 +0200879 struct vhdr_crc16 *vh = hdr_priv(hdr);
880
Jens Axboe25dfa842012-02-29 10:01:34 +0100881 vh->crc16 = fio_crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200882}
883
Jens Axboee29d1b72006-10-18 15:43:15 +0200884static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
885{
Jens Axboe546dfd92007-07-30 12:23:05 +0200886 struct vhdr_crc32 *vh = hdr_priv(hdr);
887
Jens Axboe25dfa842012-02-29 10:01:34 +0100888 vh->crc32 = fio_crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200889}
890
Jens Axboebac39e02008-06-11 20:46:19 +0200891static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
892{
893 struct vhdr_crc32 *vh = hdr_priv(hdr);
894
Jens Axboe25dfa842012-02-29 10:01:34 +0100895 vh->crc32 = fio_crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200896}
897
Jens Axboed77a7af2007-07-27 15:35:06 +0200898static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
899{
Jens Axboe546dfd92007-07-30 12:23:05 +0200900 struct vhdr_crc64 *vh = hdr_priv(hdr);
901
Jens Axboe25dfa842012-02-29 10:01:34 +0100902 vh->crc64 = fio_crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200903}
904
Jens Axboee29d1b72006-10-18 15:43:15 +0200905static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
906{
Jens Axboe546dfd92007-07-30 12:23:05 +0200907 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100908 struct fio_md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200909 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200910 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200911
Jens Axboe25dfa842012-02-29 10:01:34 +0100912 fio_md5_init(&md5_ctx);
913 fio_md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200914}
915
Jens Axboe7d9fb452011-01-11 22:16:49 +0100916static void populate_hdr(struct thread_data *td, struct io_u *io_u,
917 struct verify_header *hdr, unsigned int header_num,
918 unsigned int header_len)
919{
920 unsigned int data_len;
921 void *data, *p;
922
923 p = (void *) hdr;
924
Jens Axboed3a173a2012-02-23 08:23:18 +0100925 hdr->magic = FIO_HDR_MAGIC;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100926 hdr->verify_type = td->o.verify;
Jens Axboed3a173a2012-02-23 08:23:18 +0100927 hdr->len = header_len;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100928 hdr->rand_seed = io_u->rand_seed;
Jens Axboe25dfa842012-02-29 10:01:34 +0100929 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100930
Jens Axboe7d9fb452011-01-11 22:16:49 +0100931 data_len = header_len - hdr_size(hdr);
932
933 data = p + hdr_size(hdr);
934 switch (td->o.verify) {
935 case VERIFY_MD5:
936 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
937 io_u, hdr->len);
938 fill_md5(hdr, data, data_len);
939 break;
940 case VERIFY_CRC64:
941 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
942 io_u, hdr->len);
943 fill_crc64(hdr, data, data_len);
944 break;
945 case VERIFY_CRC32C:
946 case VERIFY_CRC32C_INTEL:
947 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
948 io_u, hdr->len);
949 fill_crc32c(hdr, data, data_len);
950 break;
951 case VERIFY_CRC32:
952 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
953 io_u, hdr->len);
954 fill_crc32(hdr, data, data_len);
955 break;
956 case VERIFY_CRC16:
957 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
958 io_u, hdr->len);
959 fill_crc16(hdr, data, data_len);
960 break;
961 case VERIFY_CRC7:
962 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
963 io_u, hdr->len);
964 fill_crc7(hdr, data, data_len);
965 break;
966 case VERIFY_SHA256:
967 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
968 io_u, hdr->len);
969 fill_sha256(hdr, data, data_len);
970 break;
971 case VERIFY_SHA512:
972 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
973 io_u, hdr->len);
974 fill_sha512(hdr, data, data_len);
975 break;
976 case VERIFY_META:
977 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
978 io_u, hdr->len);
979 fill_meta(hdr, td, io_u, header_num);
980 break;
981 case VERIFY_SHA1:
982 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
983 io_u, hdr->len);
984 fill_sha1(hdr, data, data_len);
985 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100986 case VERIFY_PATTERN:
987 /* nothing to do here */
988 break;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100989 default:
990 log_err("fio: bad verify type: %d\n", td->o.verify);
991 assert(0);
992 }
993 if (td->o.verify_offset)
994 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
995}
996
Jens Axboee29d1b72006-10-18 15:43:15 +0200997/*
998 * fill body of io_u->buf with random data and add a header with the
Jens Axboec50ca7b2011-01-12 08:31:54 +0100999 * checksum of choice
Jens Axboee29d1b72006-10-18 15:43:15 +02001000 */
1001void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1002{
Jens Axboe9cc3d152007-03-26 10:32:30 +02001003 if (td->o.verify == VERIFY_NULL)
1004 return;
1005
Juan Casseda0a7bd2013-09-17 14:06:12 -07001006 io_u->numberio = td->io_issues[io_u->ddir];
1007
Jens Axboec50ca7b2011-01-12 08:31:54 +01001008 fill_pattern_headers(td, io_u, 0, 0);
Jens Axboee29d1b72006-10-18 15:43:15 +02001009}
1010
1011int get_next_verify(struct thread_data *td, struct io_u *io_u)
1012{
Jens Axboe8de8f042007-03-27 10:36:12 +02001013 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +02001014
Jens Axboed2d7fa52007-02-19 13:21:35 +01001015 /*
1016 * this io_u is from a requeue, we already filled the offsets
1017 */
1018 if (io_u->file)
1019 return 0;
1020
Jens Axboe8de8f042007-03-27 10:36:12 +02001021 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1022 struct rb_node *n = rb_first(&td->io_hist_tree);
1023
Jens Axboe4b878982007-03-26 09:32:22 +02001024 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboef9401282014-02-06 12:17:37 -07001025
1026 /*
1027 * Ensure that the associated IO has completed
1028 */
1029 read_barrier();
1030 if (ipo->flags & IP_F_IN_FLIGHT)
1031 goto nothing;
1032
Jens Axboe4b878982007-03-26 09:32:22 +02001033 rb_erase(n, &td->io_hist_tree);
Jens Axboea917a8b2010-09-02 13:23:20 +02001034 assert(ipo->flags & IP_F_ONRB);
1035 ipo->flags &= ~IP_F_ONRB;
Jens Axboe01743ee2008-06-02 12:19:19 +02001036 } else if (!flist_empty(&td->io_hist_list)) {
1037 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboef9401282014-02-06 12:17:37 -07001038
1039 /*
1040 * Ensure that the associated IO has completed
1041 */
1042 read_barrier();
1043 if (ipo->flags & IP_F_IN_FLIGHT)
1044 goto nothing;
1045
Jens Axboe01743ee2008-06-02 12:19:19 +02001046 flist_del(&ipo->list);
Jens Axboea917a8b2010-09-02 13:23:20 +02001047 assert(ipo->flags & IP_F_ONLIST);
1048 ipo->flags &= ~IP_F_ONLIST;
Jens Axboe8de8f042007-03-27 10:36:12 +02001049 }
Jens Axboee29d1b72006-10-18 15:43:15 +02001050
Jens Axboe8de8f042007-03-27 10:36:12 +02001051 if (ipo) {
Jens Axboe0d29de82010-09-01 13:54:15 +02001052 td->io_hist_len--;
1053
Jens Axboee29d1b72006-10-18 15:43:15 +02001054 io_u->offset = ipo->offset;
1055 io_u->buflen = ipo->len;
Juan Casseda0a7bd2013-09-17 14:06:12 -07001056 io_u->numberio = ipo->numberio;
Jens Axboe36167d82007-02-18 05:41:31 +01001057 io_u->file = ipo->file;
Jens Axboe82af2a72012-03-13 13:45:58 +01001058 io_u->flags |= IO_U_F_VER_LIST;
Jens Axboe97af62c2007-05-22 11:12:13 +02001059
Jens Axboe0d29de82010-09-01 13:54:15 +02001060 if (ipo->flags & IP_F_TRIMMED)
1061 io_u->flags |= IO_U_F_TRIMMED;
1062
Jens Axboed6aed792009-06-03 08:41:15 +02001063 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +02001064 int r = td_io_open_file(td, io_u->file);
1065
Jens Axboebd6f78b2008-02-01 20:27:52 +01001066 if (r) {
1067 dprint(FD_VERIFY, "failed file %s open\n",
1068 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +02001069 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +01001070 }
Jens Axboe97af62c2007-05-22 11:12:13 +02001071 }
1072
1073 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +02001074 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +02001075 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +01001076 io_u->xfer_buf = io_u->buf;
1077 io_u->xfer_buflen = io_u->buflen;
Jens Axboe0d29de82010-09-01 13:54:15 +02001078
1079 remove_trim_entry(td, ipo);
Jens Axboee29d1b72006-10-18 15:43:15 +02001080 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001081 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -07001082
1083 if (!td->o.verify_pattern_bytes) {
1084 io_u->rand_seed = __rand(&td->__verify_state);
1085 if (sizeof(int) != sizeof(long *))
1086 io_u->rand_seed *= __rand(&td->__verify_state);
1087 }
Jens Axboee29d1b72006-10-18 15:43:15 +02001088 return 0;
1089 }
1090
Jens Axboef9401282014-02-06 12:17:37 -07001091nothing:
Jens Axboebd6f78b2008-02-01 20:27:52 +01001092 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +02001093 return 1;
1094}
Jens Axboee8462bd2009-07-06 12:59:04 +02001095
Jens Axboedc5bfbb2013-04-10 22:23:40 +02001096void fio_verify_init(struct thread_data *td)
1097{
1098 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1099 td->o.verify == VERIFY_CRC32C) {
1100 crc32c_intel_probe();
1101 }
1102}
1103
Jens Axboee8462bd2009-07-06 12:59:04 +02001104static void *verify_async_thread(void *data)
1105{
1106 struct thread_data *td = data;
1107 struct io_u *io_u;
1108 int ret = 0;
1109
1110 if (td->o.verify_cpumask_set &&
1111 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1112 log_err("fio: failed setting verify thread affinity\n");
1113 goto done;
1114 }
1115
1116 do {
Jens Axboee53ab272009-07-06 13:43:46 +02001117 FLIST_HEAD(list);
1118
Jens Axboee8462bd2009-07-06 12:59:04 +02001119 read_barrier();
1120 if (td->verify_thread_exit)
1121 break;
1122
1123 pthread_mutex_lock(&td->io_u_lock);
1124
1125 while (flist_empty(&td->verify_list) &&
1126 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +02001127 ret = pthread_cond_wait(&td->verify_cond,
1128 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +02001129 if (ret) {
1130 pthread_mutex_unlock(&td->io_u_lock);
1131 break;
1132 }
1133 }
1134
Jens Axboee53ab272009-07-06 13:43:46 +02001135 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001136 pthread_mutex_unlock(&td->io_u_lock);
1137
Jens Axboee53ab272009-07-06 13:43:46 +02001138 if (flist_empty(&list))
1139 continue;
1140
1141 while (!flist_empty(&list)) {
Jens Axboe2ae0b202013-05-28 14:16:55 +02001142 io_u = flist_entry(list.next, struct io_u, verify_list);
1143 flist_del(&io_u->verify_list);
Jens Axboee53ab272009-07-06 13:43:46 +02001144
Jens Axboed561f2a2009-07-06 13:51:05 +02001145 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +02001146 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +02001147 if (!ret)
1148 continue;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001149 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
Jens Axboed561f2a2009-07-06 13:51:05 +02001150 update_error_count(td, ret);
1151 td_clear_error(td);
1152 ret = 0;
1153 }
Jens Axboee53ab272009-07-06 13:43:46 +02001154 }
Jens Axboee8462bd2009-07-06 12:59:04 +02001155 } while (!ret);
1156
Jens Axboed561f2a2009-07-06 13:51:05 +02001157 if (ret) {
1158 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +02001159 if (td->o.verify_fatal)
1160 td->terminate = 1;
Jens Axboed561f2a2009-07-06 13:51:05 +02001161 }
1162
Jens Axboee8462bd2009-07-06 12:59:04 +02001163done:
1164 pthread_mutex_lock(&td->io_u_lock);
1165 td->nr_verify_threads--;
1166 pthread_mutex_unlock(&td->io_u_lock);
1167
1168 pthread_cond_signal(&td->free_cond);
1169 return NULL;
1170}
1171
1172int verify_async_init(struct thread_data *td)
1173{
1174 int i, ret;
bart Van Assche304a47c2010-08-01 21:31:26 +02001175 pthread_attr_t attr;
1176
1177 pthread_attr_init(&attr);
1178 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
Jens Axboee8462bd2009-07-06 12:59:04 +02001179
1180 td->verify_thread_exit = 0;
1181
1182 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1183 for (i = 0; i < td->o.verify_async; i++) {
bart Van Assche304a47c2010-08-01 21:31:26 +02001184 ret = pthread_create(&td->verify_threads[i], &attr,
Jens Axboee8462bd2009-07-06 12:59:04 +02001185 verify_async_thread, td);
1186 if (ret) {
1187 log_err("fio: async verify creation failed: %s\n",
1188 strerror(ret));
1189 break;
1190 }
1191 ret = pthread_detach(td->verify_threads[i]);
1192 if (ret) {
1193 log_err("fio: async verify thread detach failed: %s\n",
1194 strerror(ret));
1195 break;
1196 }
1197 td->nr_verify_threads++;
1198 }
1199
bart Van Assche304a47c2010-08-01 21:31:26 +02001200 pthread_attr_destroy(&attr);
1201
Jens Axboee8462bd2009-07-06 12:59:04 +02001202 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +02001203 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +02001204 td->verify_thread_exit = 1;
1205 write_barrier();
1206 pthread_cond_broadcast(&td->verify_cond);
1207 return 1;
1208 }
1209
1210 return 0;
1211}
1212
1213void verify_async_exit(struct thread_data *td)
1214{
1215 td->verify_thread_exit = 1;
1216 write_barrier();
1217 pthread_cond_broadcast(&td->verify_cond);
1218
1219 pthread_mutex_lock(&td->io_u_lock);
1220
1221 while (td->nr_verify_threads)
1222 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1223
1224 pthread_mutex_unlock(&td->io_u_lock);
1225 free(td->verify_threads);
1226 td->verify_threads = NULL;
1227}