blob: 721aeb46635701b3f0f1cca8fc2db18d8c2cc4eb [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
386 if (!ret)
Jens Axboebfacf382010-06-18 14:29:52 +0200387 return 0;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200388
Jens Axboebfacf382010-06-18 14:29:52 +0200389 vc->name = "meta";
390 log_verify_failure(hdr, vc);
Jens Axboe92bf48d2011-01-14 21:20:42 +0100391 return ret;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200392}
393
Jens Axboe936216f2010-06-18 13:44:11 +0200394static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200395{
Jens Axboe936216f2010-06-18 13:44:11 +0200396 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200397 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200398 uint8_t sha512[128];
Jens Axboe25dfa842012-02-29 10:01:34 +0100399 struct fio_sha512_ctx sha512_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200400 .buf = sha512,
401 };
402
Jens Axboe936216f2010-06-18 13:44:11 +0200403 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100404
Jens Axboe25dfa842012-02-29 10:01:34 +0100405 fio_sha512_init(&sha512_ctx);
406 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200407
Jens Axboebfacf382010-06-18 14:29:52 +0200408 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
409 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200410
Jens Axboebfacf382010-06-18 14:29:52 +0200411 vc->name = "sha512";
412 vc->good_crc = vh->sha512;
413 vc->bad_crc = sha512_ctx.buf;
414 vc->crc_len = sizeof(vh->sha512);
415 log_verify_failure(hdr, vc);
416 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200417}
418
Jens Axboe936216f2010-06-18 13:44:11 +0200419static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200420{
Jens Axboe936216f2010-06-18 13:44:11 +0200421 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200422 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100423 uint8_t sha256[64];
Jens Axboe25dfa842012-02-29 10:01:34 +0100424 struct fio_sha256_ctx sha256_ctx = {
Jens Axboecd14cc12007-07-30 10:59:33 +0200425 .buf = sha256,
426 };
427
Jens Axboe936216f2010-06-18 13:44:11 +0200428 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100429
Jens Axboe25dfa842012-02-29 10:01:34 +0100430 fio_sha256_init(&sha256_ctx);
431 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200432
Jens Axboebfacf382010-06-18 14:29:52 +0200433 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
434 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200435
Jens Axboebfacf382010-06-18 14:29:52 +0200436 vc->name = "sha256";
437 vc->good_crc = vh->sha256;
438 vc->bad_crc = sha256_ctx.buf;
439 vc->crc_len = sizeof(vh->sha256);
440 log_verify_failure(hdr, vc);
441 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200442}
443
Jens Axboe936216f2010-06-18 13:44:11 +0200444static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200445{
Jens Axboe936216f2010-06-18 13:44:11 +0200446 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200447 struct vhdr_sha1 *vh = hdr_priv(hdr);
448 uint32_t sha1[5];
Jens Axboe25dfa842012-02-29 10:01:34 +0100449 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200450 .H = sha1,
451 };
452
Jens Axboe936216f2010-06-18 13:44:11 +0200453 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200454
Jens Axboe25dfa842012-02-29 10:01:34 +0100455 fio_sha1_init(&sha1_ctx);
456 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboe7c353ce2009-08-09 22:40:33 +0200457
Jens Axboebfacf382010-06-18 14:29:52 +0200458 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
459 return 0;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200460
Jens Axboebfacf382010-06-18 14:29:52 +0200461 vc->name = "sha1";
462 vc->good_crc = vh->sha1;
463 vc->bad_crc = sha1_ctx.H;
464 vc->crc_len = sizeof(vh->sha1);
465 log_verify_failure(hdr, vc);
466 return EILSEQ;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200467}
468
Jens Axboe936216f2010-06-18 13:44:11 +0200469static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200470{
Jens Axboe936216f2010-06-18 13:44:11 +0200471 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200472 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200473 unsigned char c;
474
Jens Axboe936216f2010-06-18 13:44:11 +0200475 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100476
Jens Axboe25dfa842012-02-29 10:01:34 +0100477 c = fio_crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200478
Jens Axboebfacf382010-06-18 14:29:52 +0200479 if (c == vh->crc7)
480 return 0;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200481
Jens Axboebfacf382010-06-18 14:29:52 +0200482 vc->name = "crc7";
483 vc->good_crc = &vh->crc7;
484 vc->bad_crc = &c;
485 vc->crc_len = 1;
486 log_verify_failure(hdr, vc);
487 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200488}
489
Jens Axboe936216f2010-06-18 13:44:11 +0200490static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200491{
Jens Axboe936216f2010-06-18 13:44:11 +0200492 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200493 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200494 unsigned short c;
495
Jens Axboe936216f2010-06-18 13:44:11 +0200496 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100497
Jens Axboe25dfa842012-02-29 10:01:34 +0100498 c = fio_crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200499
Jens Axboebfacf382010-06-18 14:29:52 +0200500 if (c == vh->crc16)
501 return 0;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200502
Jens Axboebfacf382010-06-18 14:29:52 +0200503 vc->name = "crc16";
504 vc->good_crc = &vh->crc16;
505 vc->bad_crc = &c;
506 vc->crc_len = 2;
507 log_verify_failure(hdr, vc);
508 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200509}
510
Jens Axboe936216f2010-06-18 13:44:11 +0200511static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200512{
Jens Axboe936216f2010-06-18 13:44:11 +0200513 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200514 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200515 unsigned long long c;
516
Jens Axboe936216f2010-06-18 13:44:11 +0200517 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100518
Jens Axboe25dfa842012-02-29 10:01:34 +0100519 c = fio_crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200520
Jens Axboebfacf382010-06-18 14:29:52 +0200521 if (c == vh->crc64)
522 return 0;
Jens Axboed77a7af2007-07-27 15:35:06 +0200523
Jens Axboebfacf382010-06-18 14:29:52 +0200524 vc->name = "crc64";
525 vc->good_crc = &vh->crc64;
526 vc->bad_crc = &c;
527 vc->crc_len = 8;
528 log_verify_failure(hdr, vc);
529 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200530}
531
Jens Axboe936216f2010-06-18 13:44:11 +0200532static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200533{
Jens Axboe936216f2010-06-18 13:44:11 +0200534 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200535 struct vhdr_crc32 *vh = hdr_priv(hdr);
536 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200537
Jens Axboe936216f2010-06-18 13:44:11 +0200538 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100539
Jens Axboe25dfa842012-02-29 10:01:34 +0100540 c = fio_crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200541
Jens Axboebfacf382010-06-18 14:29:52 +0200542 if (c == vh->crc32)
543 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200544
Jens Axboebfacf382010-06-18 14:29:52 +0200545 vc->name = "crc32";
546 vc->good_crc = &vh->crc32;
547 vc->bad_crc = &c;
548 vc->crc_len = 4;
549 log_verify_failure(hdr, vc);
550 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200551}
552
Jens Axboe936216f2010-06-18 13:44:11 +0200553static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200554{
Jens Axboe936216f2010-06-18 13:44:11 +0200555 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200556 struct vhdr_crc32 *vh = hdr_priv(hdr);
557 uint32_t c;
558
Jens Axboe936216f2010-06-18 13:44:11 +0200559 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200560
Jens Axboe25dfa842012-02-29 10:01:34 +0100561 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200562
Jens Axboebfacf382010-06-18 14:29:52 +0200563 if (c == vh->crc32)
564 return 0;
Jens Axboebac39e02008-06-11 20:46:19 +0200565
Jens Axboebfacf382010-06-18 14:29:52 +0200566 vc->name = "crc32c";
567 vc->good_crc = &vh->crc32;
568 vc->bad_crc = &c;
569 vc->crc_len = 4;
570 log_verify_failure(hdr, vc);
571 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200572}
573
Jens Axboe936216f2010-06-18 13:44:11 +0200574static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200575{
Jens Axboe936216f2010-06-18 13:44:11 +0200576 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200577 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200578 uint32_t hash[MD5_HASH_WORDS];
Jens Axboe25dfa842012-02-29 10:01:34 +0100579 struct fio_md5_ctx md5_ctx = {
Jens Axboe8c432322007-07-27 13:16:24 +0200580 .hash = hash,
581 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200582
Jens Axboe936216f2010-06-18 13:44:11 +0200583 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100584
Jens Axboe25dfa842012-02-29 10:01:34 +0100585 fio_md5_init(&md5_ctx);
586 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200587
Jens Axboebfacf382010-06-18 14:29:52 +0200588 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
589 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200590
Jens Axboebfacf382010-06-18 14:29:52 +0200591 vc->name = "md5";
592 vc->good_crc = vh->md5_digest;
593 vc->bad_crc = md5_ctx.hash;
594 vc->crc_len = sizeof(hash);
595 log_verify_failure(hdr, vc);
596 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200597}
598
Jens Axboee8462bd2009-07-06 12:59:04 +0200599/*
600 * Push IO verification to a separate thread
601 */
602int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
603{
604 if (io_u->file)
605 put_file_log(td, io_u->file);
606
Jens Axboee8462bd2009-07-06 12:59:04 +0200607 pthread_mutex_lock(&td->io_u_lock);
Steven Langd7ee2a72011-10-26 09:46:50 +0200608
Radha Ramachandran0c412142009-11-03 21:45:31 +0100609 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
610 td->cur_depth--;
611 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
612 }
Jens Axboe2ae0b202013-05-28 14:16:55 +0200613 flist_add_tail(&io_u->verify_list, &td->verify_list);
Jens Axboe2ecc1b52009-11-04 20:58:09 +0100614 io_u->flags |= IO_U_F_FREE_DEF;
Jens Axboee8462bd2009-07-06 12:59:04 +0200615 pthread_mutex_unlock(&td->io_u_lock);
616
617 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200618 return 0;
619}
620
Jens Axboe0d29de82010-09-01 13:54:15 +0200621static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
622{
623 static char zero_buf[1024];
624 unsigned int this_len, len;
625 int ret = 0;
626 void *p;
627
628 if (!td->o.trim_zero)
629 return 0;
630
631 len = io_u->buflen;
632 p = io_u->buf;
633 do {
634 this_len = sizeof(zero_buf);
635 if (this_len > len)
636 this_len = len;
637 if (memcmp(p, zero_buf, this_len)) {
638 ret = EILSEQ;
639 break;
640 }
641 len -= this_len;
642 p += this_len;
643 } while (len);
644
645 if (!ret)
646 return 0;
647
Jens Axboea917a8b2010-09-02 13:23:20 +0200648 log_err("trim: verify failed at file %s offset %llu, length %lu"
649 ", block offset %lu\n",
650 io_u->file->file_name, io_u->offset, io_u->buflen,
Jens Axboe2f681242010-10-21 08:15:59 +0200651 (unsigned long) (p - io_u->buf));
Jens Axboe0d29de82010-09-01 13:54:15 +0200652 return ret;
653}
654
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100655static int verify_header(struct io_u *io_u, struct verify_header *hdr)
Jens Axboef65d1c22012-02-22 20:11:57 +0100656{
657 void *p = hdr;
658 uint32_t crc;
659
Jens Axboeae38c0d2012-02-23 10:31:07 +0100660 if (hdr->magic != FIO_HDR_MAGIC)
661 return 0;
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100662 if (hdr->len > io_u->buflen) {
663 log_err("fio: verify header exceeds buffer length (%u > %lu)\n", hdr->len, io_u->buflen);
664 return 0;
665 }
Jens Axboeae38c0d2012-02-23 10:31:07 +0100666
Jens Axboe25dfa842012-02-29 10:01:34 +0100667 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100668 if (crc == hdr->crc32)
669 return 1;
670
671 log_err("fio: verify header crc %x, calculated %x\n", hdr->crc32, crc);
672 return 0;
673}
674
Jens Axboe36690c92007-03-26 10:23:34 +0200675int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200676{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200677 struct verify_header *hdr;
Jens Axboe2b13e712011-01-19 14:04:16 -0700678 unsigned int header_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200679 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200680 int ret;
681
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200682 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200683 return 0;
Jens Axboe0d29de82010-09-01 13:54:15 +0200684 if (io_u->flags & IO_U_F_TRIMMED) {
685 ret = verify_trimmed_io_u(td, io_u);
686 goto done;
687 }
Jens Axboe36690c92007-03-26 10:23:34 +0200688
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100689 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200690
Jens Axboea12a3b42007-08-09 10:20:54 +0200691 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100692 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
693 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200694 struct vcont vc = {
695 .io_u = io_u,
696 .hdr_num = hdr_num,
Jens Axboe7d9fb452011-01-11 22:16:49 +0100697 .td = td,
Jens Axboebfacf382010-06-18 14:29:52 +0200698 };
Jens Axboef00b2102012-11-30 09:39:02 +0100699 unsigned int verify_type;
Jens Axboe936216f2010-06-18 13:44:11 +0200700
Jens Axboef3e6cb92010-06-18 14:48:43 +0200701 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200702 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200703
Jens Axboe2b13e712011-01-19 14:04:16 -0700704 header_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200705 if (td->o.verify_offset)
Jens Axboe2b13e712011-01-19 14:04:16 -0700706 memswp(p, p + td->o.verify_offset, header_size);
Jens Axboe95646102007-07-28 21:17:50 +0200707 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200708
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100709 if (!verify_header(io_u, hdr)) {
Jens Axboeae38c0d2012-02-23 10:31:07 +0100710 log_err("verify: bad magic header %x, wanted %x at "
711 "file %s offset %llu, length %u\n",
Jens Axboed3a173a2012-02-23 08:23:18 +0100712 hdr->magic, FIO_HDR_MAGIC,
Jens Axboec9b44032010-06-18 14:46:06 +0200713 io_u->file->file_name,
714 io_u->offset + hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200715 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200716 }
717
Jens Axboef00b2102012-11-30 09:39:02 +0100718 if (td->o.verify != VERIFY_NONE)
719 verify_type = td->o.verify;
720 else
721 verify_type = hdr->verify_type;
722
723 switch (verify_type) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200724 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200725 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200726 break;
727 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200728 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200729 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200730 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200731 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200732 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200733 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200734 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200735 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200736 break;
737 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200738 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200739 break;
740 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200741 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200742 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200743 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200744 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200745 break;
746 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200747 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200748 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200749 case VERIFY_META:
Jens Axboe92bf48d2011-01-14 21:20:42 +0100750 ret = verify_io_u_meta(hdr, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200751 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200752 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200753 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200754 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100755 case VERIFY_PATTERN:
756 ret = verify_io_u_pattern(hdr, &vc);
757 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200758 default:
759 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200760 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200761 }
Jens Axboef00b2102012-11-30 09:39:02 +0100762
763 if (ret && verify_type != hdr->verify_type)
764 log_err("fio: verify type mismatch (%u media, %u given)\n",
765 hdr->verify_type, verify_type);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200766 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100767
Jens Axboe0d29de82010-09-01 13:54:15 +0200768done:
Jens Axboef3e6cb92010-06-18 14:48:43 +0200769 if (ret && td->o.verify_fatal)
770 td->terminate = 1;
771
Jens Axboea12a3b42007-08-09 10:20:54 +0200772 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200773}
774
Shawn Lewis7437ee82007-08-02 21:05:58 +0200775static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100776 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200777{
778 struct vhdr_meta *vh = hdr_priv(hdr);
779
780 vh->thread = td->thread_number;
781
782 vh->time_sec = io_u->start_time.tv_sec;
783 vh->time_usec = io_u->start_time.tv_usec;
784
785 vh->numberio = td->io_issues[DDIR_WRITE];
786
787 vh->offset = io_u->offset + header_num * td->o.verify_interval;
788}
789
Jens Axboecd14cc12007-07-30 10:59:33 +0200790static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
791{
Jens Axboe546dfd92007-07-30 12:23:05 +0200792 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100793 struct fio_sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200794 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200795 };
796
Jens Axboe25dfa842012-02-29 10:01:34 +0100797 fio_sha512_init(&sha512_ctx);
798 fio_sha512_update(&sha512_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200799}
800
801static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
802{
Jens Axboe546dfd92007-07-30 12:23:05 +0200803 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100804 struct fio_sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200805 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200806 };
807
Jens Axboe25dfa842012-02-29 10:01:34 +0100808 fio_sha256_init(&sha256_ctx);
809 fio_sha256_update(&sha256_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200810}
811
Jens Axboe7c353ce2009-08-09 22:40:33 +0200812static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
813{
814 struct vhdr_sha1 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100815 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200816 .H = vh->sha1,
817 };
818
Jens Axboe25dfa842012-02-29 10:01:34 +0100819 fio_sha1_init(&sha1_ctx);
820 fio_sha1_update(&sha1_ctx, p, len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200821}
822
Jens Axboe1e154bd2007-07-27 09:52:40 +0200823static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
824{
Jens Axboe546dfd92007-07-30 12:23:05 +0200825 struct vhdr_crc7 *vh = hdr_priv(hdr);
826
Jens Axboe25dfa842012-02-29 10:01:34 +0100827 vh->crc7 = fio_crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200828}
829
Jens Axboe969f7ed2007-07-27 09:07:17 +0200830static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
831{
Jens Axboe546dfd92007-07-30 12:23:05 +0200832 struct vhdr_crc16 *vh = hdr_priv(hdr);
833
Jens Axboe25dfa842012-02-29 10:01:34 +0100834 vh->crc16 = fio_crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200835}
836
Jens Axboee29d1b72006-10-18 15:43:15 +0200837static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
838{
Jens Axboe546dfd92007-07-30 12:23:05 +0200839 struct vhdr_crc32 *vh = hdr_priv(hdr);
840
Jens Axboe25dfa842012-02-29 10:01:34 +0100841 vh->crc32 = fio_crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200842}
843
Jens Axboebac39e02008-06-11 20:46:19 +0200844static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
845{
846 struct vhdr_crc32 *vh = hdr_priv(hdr);
847
Jens Axboe25dfa842012-02-29 10:01:34 +0100848 vh->crc32 = fio_crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200849}
850
Jens Axboed77a7af2007-07-27 15:35:06 +0200851static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
852{
Jens Axboe546dfd92007-07-30 12:23:05 +0200853 struct vhdr_crc64 *vh = hdr_priv(hdr);
854
Jens Axboe25dfa842012-02-29 10:01:34 +0100855 vh->crc64 = fio_crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200856}
857
Jens Axboee29d1b72006-10-18 15:43:15 +0200858static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
859{
Jens Axboe546dfd92007-07-30 12:23:05 +0200860 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100861 struct fio_md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200862 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200863 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200864
Jens Axboe25dfa842012-02-29 10:01:34 +0100865 fio_md5_init(&md5_ctx);
866 fio_md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200867}
868
Jens Axboe7d9fb452011-01-11 22:16:49 +0100869static void populate_hdr(struct thread_data *td, struct io_u *io_u,
870 struct verify_header *hdr, unsigned int header_num,
871 unsigned int header_len)
872{
873 unsigned int data_len;
874 void *data, *p;
875
876 p = (void *) hdr;
877
Jens Axboed3a173a2012-02-23 08:23:18 +0100878 hdr->magic = FIO_HDR_MAGIC;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100879 hdr->verify_type = td->o.verify;
Jens Axboed3a173a2012-02-23 08:23:18 +0100880 hdr->len = header_len;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100881 hdr->rand_seed = io_u->rand_seed;
Jens Axboe25dfa842012-02-29 10:01:34 +0100882 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100883
Jens Axboe7d9fb452011-01-11 22:16:49 +0100884 data_len = header_len - hdr_size(hdr);
885
886 data = p + hdr_size(hdr);
887 switch (td->o.verify) {
888 case VERIFY_MD5:
889 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
890 io_u, hdr->len);
891 fill_md5(hdr, data, data_len);
892 break;
893 case VERIFY_CRC64:
894 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
895 io_u, hdr->len);
896 fill_crc64(hdr, data, data_len);
897 break;
898 case VERIFY_CRC32C:
899 case VERIFY_CRC32C_INTEL:
900 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
901 io_u, hdr->len);
902 fill_crc32c(hdr, data, data_len);
903 break;
904 case VERIFY_CRC32:
905 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
906 io_u, hdr->len);
907 fill_crc32(hdr, data, data_len);
908 break;
909 case VERIFY_CRC16:
910 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
911 io_u, hdr->len);
912 fill_crc16(hdr, data, data_len);
913 break;
914 case VERIFY_CRC7:
915 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
916 io_u, hdr->len);
917 fill_crc7(hdr, data, data_len);
918 break;
919 case VERIFY_SHA256:
920 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
921 io_u, hdr->len);
922 fill_sha256(hdr, data, data_len);
923 break;
924 case VERIFY_SHA512:
925 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
926 io_u, hdr->len);
927 fill_sha512(hdr, data, data_len);
928 break;
929 case VERIFY_META:
930 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
931 io_u, hdr->len);
932 fill_meta(hdr, td, io_u, header_num);
933 break;
934 case VERIFY_SHA1:
935 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
936 io_u, hdr->len);
937 fill_sha1(hdr, data, data_len);
938 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100939 case VERIFY_PATTERN:
940 /* nothing to do here */
941 break;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100942 default:
943 log_err("fio: bad verify type: %d\n", td->o.verify);
944 assert(0);
945 }
946 if (td->o.verify_offset)
947 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
948}
949
Jens Axboee29d1b72006-10-18 15:43:15 +0200950/*
951 * fill body of io_u->buf with random data and add a header with the
Jens Axboec50ca7b2011-01-12 08:31:54 +0100952 * checksum of choice
Jens Axboee29d1b72006-10-18 15:43:15 +0200953 */
954void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
955{
Jens Axboe9cc3d152007-03-26 10:32:30 +0200956 if (td->o.verify == VERIFY_NULL)
957 return;
958
Jens Axboec50ca7b2011-01-12 08:31:54 +0100959 fill_pattern_headers(td, io_u, 0, 0);
Jens Axboee29d1b72006-10-18 15:43:15 +0200960}
961
962int get_next_verify(struct thread_data *td, struct io_u *io_u)
963{
Jens Axboe8de8f042007-03-27 10:36:12 +0200964 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200965
Jens Axboed2d7fa52007-02-19 13:21:35 +0100966 /*
967 * this io_u is from a requeue, we already filled the offsets
968 */
969 if (io_u->file)
970 return 0;
971
Jens Axboe8de8f042007-03-27 10:36:12 +0200972 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
973 struct rb_node *n = rb_first(&td->io_hist_tree);
974
Jens Axboe4b878982007-03-26 09:32:22 +0200975 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200976 rb_erase(n, &td->io_hist_tree);
Jens Axboea917a8b2010-09-02 13:23:20 +0200977 assert(ipo->flags & IP_F_ONRB);
978 ipo->flags &= ~IP_F_ONRB;
Jens Axboe01743ee2008-06-02 12:19:19 +0200979 } else if (!flist_empty(&td->io_hist_list)) {
980 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
981 flist_del(&ipo->list);
Jens Axboea917a8b2010-09-02 13:23:20 +0200982 assert(ipo->flags & IP_F_ONLIST);
983 ipo->flags &= ~IP_F_ONLIST;
Jens Axboe8de8f042007-03-27 10:36:12 +0200984 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200985
Jens Axboe8de8f042007-03-27 10:36:12 +0200986 if (ipo) {
Jens Axboe0d29de82010-09-01 13:54:15 +0200987 td->io_hist_len--;
988
Jens Axboee29d1b72006-10-18 15:43:15 +0200989 io_u->offset = ipo->offset;
990 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100991 io_u->file = ipo->file;
Jens Axboe82af2a72012-03-13 13:45:58 +0100992 io_u->flags |= IO_U_F_VER_LIST;
Jens Axboe97af62c2007-05-22 11:12:13 +0200993
Jens Axboe0d29de82010-09-01 13:54:15 +0200994 if (ipo->flags & IP_F_TRIMMED)
995 io_u->flags |= IO_U_F_TRIMMED;
996
Jens Axboed6aed792009-06-03 08:41:15 +0200997 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200998 int r = td_io_open_file(td, io_u->file);
999
Jens Axboebd6f78b2008-02-01 20:27:52 +01001000 if (r) {
1001 dprint(FD_VERIFY, "failed file %s open\n",
1002 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +02001003 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +01001004 }
Jens Axboe97af62c2007-05-22 11:12:13 +02001005 }
1006
1007 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +02001008 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +02001009 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +01001010 io_u->xfer_buf = io_u->buf;
1011 io_u->xfer_buflen = io_u->buflen;
Jens Axboe0d29de82010-09-01 13:54:15 +02001012
1013 remove_trim_entry(td, ipo);
Jens Axboee29d1b72006-10-18 15:43:15 +02001014 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001015 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +02001016 return 0;
1017 }
1018
Jens Axboebd6f78b2008-02-01 20:27:52 +01001019 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +02001020 return 1;
1021}
Jens Axboee8462bd2009-07-06 12:59:04 +02001022
Jens Axboedc5bfbb2013-04-10 22:23:40 +02001023void fio_verify_init(struct thread_data *td)
1024{
1025 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1026 td->o.verify == VERIFY_CRC32C) {
1027 crc32c_intel_probe();
1028 }
1029}
1030
Jens Axboee8462bd2009-07-06 12:59:04 +02001031static void *verify_async_thread(void *data)
1032{
1033 struct thread_data *td = data;
1034 struct io_u *io_u;
1035 int ret = 0;
1036
1037 if (td->o.verify_cpumask_set &&
1038 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1039 log_err("fio: failed setting verify thread affinity\n");
1040 goto done;
1041 }
1042
1043 do {
Jens Axboee53ab272009-07-06 13:43:46 +02001044 FLIST_HEAD(list);
1045
Jens Axboee8462bd2009-07-06 12:59:04 +02001046 read_barrier();
1047 if (td->verify_thread_exit)
1048 break;
1049
1050 pthread_mutex_lock(&td->io_u_lock);
1051
1052 while (flist_empty(&td->verify_list) &&
1053 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +02001054 ret = pthread_cond_wait(&td->verify_cond,
1055 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +02001056 if (ret) {
1057 pthread_mutex_unlock(&td->io_u_lock);
1058 break;
1059 }
1060 }
1061
Jens Axboee53ab272009-07-06 13:43:46 +02001062 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001063 pthread_mutex_unlock(&td->io_u_lock);
1064
Jens Axboee53ab272009-07-06 13:43:46 +02001065 if (flist_empty(&list))
1066 continue;
1067
1068 while (!flist_empty(&list)) {
Jens Axboe2ae0b202013-05-28 14:16:55 +02001069 io_u = flist_entry(list.next, struct io_u, verify_list);
1070 flist_del(&io_u->verify_list);
Jens Axboee53ab272009-07-06 13:43:46 +02001071
Jens Axboed561f2a2009-07-06 13:51:05 +02001072 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +02001073 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +02001074 if (!ret)
1075 continue;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001076 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
Jens Axboed561f2a2009-07-06 13:51:05 +02001077 update_error_count(td, ret);
1078 td_clear_error(td);
1079 ret = 0;
1080 }
Jens Axboee53ab272009-07-06 13:43:46 +02001081 }
Jens Axboee8462bd2009-07-06 12:59:04 +02001082 } while (!ret);
1083
Jens Axboed561f2a2009-07-06 13:51:05 +02001084 if (ret) {
1085 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +02001086 if (td->o.verify_fatal)
1087 td->terminate = 1;
Jens Axboed561f2a2009-07-06 13:51:05 +02001088 }
1089
Jens Axboee8462bd2009-07-06 12:59:04 +02001090done:
1091 pthread_mutex_lock(&td->io_u_lock);
1092 td->nr_verify_threads--;
1093 pthread_mutex_unlock(&td->io_u_lock);
1094
1095 pthread_cond_signal(&td->free_cond);
1096 return NULL;
1097}
1098
1099int verify_async_init(struct thread_data *td)
1100{
1101 int i, ret;
bart Van Assche304a47c2010-08-01 21:31:26 +02001102 pthread_attr_t attr;
1103
1104 pthread_attr_init(&attr);
1105 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
Jens Axboee8462bd2009-07-06 12:59:04 +02001106
1107 td->verify_thread_exit = 0;
1108
1109 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1110 for (i = 0; i < td->o.verify_async; i++) {
bart Van Assche304a47c2010-08-01 21:31:26 +02001111 ret = pthread_create(&td->verify_threads[i], &attr,
Jens Axboee8462bd2009-07-06 12:59:04 +02001112 verify_async_thread, td);
1113 if (ret) {
1114 log_err("fio: async verify creation failed: %s\n",
1115 strerror(ret));
1116 break;
1117 }
1118 ret = pthread_detach(td->verify_threads[i]);
1119 if (ret) {
1120 log_err("fio: async verify thread detach failed: %s\n",
1121 strerror(ret));
1122 break;
1123 }
1124 td->nr_verify_threads++;
1125 }
1126
bart Van Assche304a47c2010-08-01 21:31:26 +02001127 pthread_attr_destroy(&attr);
1128
Jens Axboee8462bd2009-07-06 12:59:04 +02001129 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +02001130 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +02001131 td->verify_thread_exit = 1;
1132 write_barrier();
1133 pthread_cond_broadcast(&td->verify_cond);
1134 return 1;
1135 }
1136
1137 return 0;
1138}
1139
1140void verify_async_exit(struct thread_data *td)
1141{
1142 td->verify_thread_exit = 1;
1143 write_barrier();
1144 pthread_cond_broadcast(&td->verify_cond);
1145
1146 pthread_mutex_lock(&td->io_u_lock);
1147
1148 while (td->nr_verify_threads)
1149 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1150
1151 pthread_mutex_unlock(&td->io_u_lock);
1152 free(td->verify_threads);
1153 td->verify_threads = NULL;
1154}