blob: 371b650a50a327f383672fb41bb332adaa3e7bde [file] [log] [blame]
Jens Axboee29d1b72006-10-18 15:43:15 +02001/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
Jens Axboe97af62c2007-05-22 11:12:13 +02007#include <assert.h>
Jens Axboee8462bd2009-07-06 12:59:04 +02008#include <pthread.h>
Jens Axboe79402a12011-01-14 12:41:15 +01009#include <libgen.h>
Jens Axboee29d1b72006-10-18 15:43:15 +020010
11#include "fio.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020012#include "verify.h"
Jens Axboe0d29de82010-09-01 13:54:15 +020013#include "trim.h"
Jens Axboe637ef8d2010-06-21 20:39:38 +020014#include "lib/rand.h"
Jens Axboe51aa2da2013-01-21 10:55:02 -070015#include "lib/hweight.h"
Jens Axboee29d1b72006-10-18 15:43:15 +020016
Jens Axboeeef6eea2007-07-30 12:27:58 +020017#include "crc/md5.h"
18#include "crc/crc64.h"
19#include "crc/crc32.h"
Jens Axboebac39e02008-06-11 20:46:19 +020020#include "crc/crc32c.h"
Jens Axboeeef6eea2007-07-30 12:27:58 +020021#include "crc/crc16.h"
22#include "crc/crc7.h"
23#include "crc/sha256.h"
24#include "crc/sha512.h"
Jens Axboe7c353ce2009-08-09 22:40:33 +020025#include "crc/sha1.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020026
Jens Axboe7d9fb452011-01-11 22:16:49 +010027static void populate_hdr(struct thread_data *td, struct io_u *io_u,
28 struct verify_header *hdr, unsigned int header_num,
29 unsigned int header_len);
30
Jens Axboece35b1e2014-01-14 15:35:58 -070031static void fill_pattern(struct thread_data *td, void *p, unsigned int len,
32 char *pattern, unsigned int pattern_bytes)
Jens Axboe90059d62007-07-30 09:33:12 +020033{
Jens Axboece35b1e2014-01-14 15:35:58 -070034 switch (pattern_bytes) {
Jens Axboe90059d62007-07-30 09:33:12 +020035 case 0:
Jens Axboece35b1e2014-01-14 15:35:58 -070036 assert(0);
Jens Axboe90059d62007-07-30 09:33:12 +020037 break;
38 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010039 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Jens Axboece35b1e2014-01-14 15:35:58 -070040 memset(p, pattern[0], len);
Jens Axboe90059d62007-07-30 09:33:12 +020041 break;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010042 default: {
43 unsigned int i = 0, size = 0;
Jens Axboe90059d62007-07-30 09:33:12 +020044 unsigned char *b = p;
45
Jens Axboebd6f78b2008-02-01 20:27:52 +010046 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
Jens Axboece35b1e2014-01-14 15:35:58 -070047 pattern_bytes, len);
Jens Axboebd6f78b2008-02-01 20:27:52 +010048
Jens Axboe90059d62007-07-30 09:33:12 +020049 while (i < len) {
Jens Axboece35b1e2014-01-14 15:35:58 -070050 size = pattern_bytes;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010051 if (size > (len - i))
52 size = len - i;
Jens Axboece35b1e2014-01-14 15:35:58 -070053 memcpy(b+i, pattern, size);
Radha Ramachandran0e92f872009-10-27 20:14:27 +010054 i += size;
Jens Axboe90059d62007-07-30 09:33:12 +020055 }
56 break;
57 }
58 }
59}
60
Jens Axboece35b1e2014-01-14 15:35:58 -070061void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
62{
63 fill_pattern(td, p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
64}
65
66void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
67 struct io_u *io_u, unsigned long seed, int use_seed)
68{
69 if (!td->o.verify_pattern_bytes) {
70 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
71
72 if (use_seed)
73 __fill_random_buf(p, len, seed);
74 else
75 io_u->rand_seed = fill_random_buf(&td->buf_state, p, len);
76 return;
77 }
78
79 if (io_u->buf_filled_len >= len) {
80 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
81 td->o.verify_pattern_bytes, len);
82 return;
83 }
84
85 fill_pattern(td, p, len, td->o.verify_pattern, td->o.verify_pattern_bytes);
86
87 io_u->buf_filled_len = len;
88}
89
Jens Axboecfbcd0d2011-01-14 14:49:20 +010090static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
91{
92 unsigned int hdr_inc;
93
94 hdr_inc = io_u->buflen;
Jens Axboe8a99fdf2012-03-06 19:24:49 +010095 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
Jens Axboecfbcd0d2011-01-14 14:49:20 +010096 hdr_inc = td->o.verify_interval;
97
98 return hdr_inc;
99}
100
Jens Axboec50ca7b2011-01-12 08:31:54 +0100101static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
102 unsigned long seed, int use_seed)
103{
104 unsigned int hdr_inc, header_num;
105 struct verify_header *hdr;
106 void *p = io_u->buf;
107
Jens Axboece35b1e2014-01-14 15:35:58 -0700108 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100109
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100110 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100111 header_num = 0;
112 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
113 hdr = p;
114 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
115 header_num++;
116 }
117}
118
Jens Axboe4764aec2007-08-23 09:03:38 +0200119static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +0200120{
Shawn Lewisdee6de72007-08-02 22:17:52 +0200121 char swap[200];
122
123 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +0200124
Shawn Lewis546a9142007-07-28 21:11:37 +0200125 memcpy(&swap, buf1, len);
126 memcpy(buf1, buf2, len);
127 memcpy(buf2, &swap, len);
128}
129
Jens Axboee29d1b72006-10-18 15:43:15 +0200130static void hexdump(void *buffer, int len)
131{
132 unsigned char *p = buffer;
133 int i;
134
135 for (i = 0; i < len; i++)
Jens Axboebfacf382010-06-18 14:29:52 +0200136 log_err("%02x", p[i]);
137 log_err("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200138}
139
Jens Axboed9f2caf2007-07-28 21:22:03 +0200140/*
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700141 * Prepare for separation of verify_header and checksum header
Jens Axboe87677832007-07-30 12:08:14 +0200142 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200143static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200144{
Bruce Cran03e20d62011-01-02 20:14:54 +0100145 unsigned int len = 0;
Jens Axboe87677832007-07-30 12:08:14 +0200146
Jens Axboe546dfd92007-07-30 12:23:05 +0200147 switch (verify_type) {
148 case VERIFY_NONE:
149 case VERIFY_NULL:
150 len = 0;
151 break;
152 case VERIFY_MD5:
153 len = sizeof(struct vhdr_md5);
154 break;
155 case VERIFY_CRC64:
156 len = sizeof(struct vhdr_crc64);
157 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200158 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200159 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200160 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200161 len = sizeof(struct vhdr_crc32);
162 break;
163 case VERIFY_CRC16:
164 len = sizeof(struct vhdr_crc16);
165 break;
166 case VERIFY_CRC7:
167 len = sizeof(struct vhdr_crc7);
168 break;
169 case VERIFY_SHA256:
170 len = sizeof(struct vhdr_sha256);
171 break;
172 case VERIFY_SHA512:
173 len = sizeof(struct vhdr_sha512);
174 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200175 case VERIFY_META:
176 len = sizeof(struct vhdr_meta);
177 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200178 case VERIFY_SHA1:
179 len = sizeof(struct vhdr_sha1);
180 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100181 case VERIFY_PATTERN:
182 len = 0;
183 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200184 default:
185 log_err("fio: unknown verify header!\n");
186 assert(0);
187 }
188
189 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200190}
191
192static inline unsigned int hdr_size(struct verify_header *hdr)
193{
Jens Axboe546dfd92007-07-30 12:23:05 +0200194 return __hdr_size(hdr->verify_type);
195}
196
197static void *hdr_priv(struct verify_header *hdr)
198{
199 void *priv = hdr;
200
201 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200202}
203
204/*
Jens Axboe936216f2010-06-18 13:44:11 +0200205 * Verify container, pass info to verify handlers and allow them to
206 * pass info back in case of error
207 */
208struct vcont {
209 /*
210 * Input
211 */
212 struct io_u *io_u;
213 unsigned int hdr_num;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100214 struct thread_data *td;
Jens Axboe936216f2010-06-18 13:44:11 +0200215
216 /*
217 * Output, only valid in case of error
218 */
Jens Axboebfacf382010-06-18 14:29:52 +0200219 const char *name;
220 void *good_crc;
221 void *bad_crc;
Jens Axboe936216f2010-06-18 13:44:11 +0200222 unsigned int crc_len;
223};
224
Jens Axboec50ca7b2011-01-12 08:31:54 +0100225static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
226 const char *type, struct fio_file *f)
Jens Axboe7d9fb452011-01-11 22:16:49 +0100227{
Jens Axboe6f260b32011-01-13 18:57:54 +0100228 char *ptr, fname[256];
Jens Axboe7d9fb452011-01-11 22:16:49 +0100229 int ret, fd;
230
Jens Axboe6f260b32011-01-13 18:57:54 +0100231 ptr = strdup(f->file_name);
232 strcpy(fname, basename(ptr));
Jens Axboec50ca7b2011-01-12 08:31:54 +0100233
234 sprintf(fname + strlen(fname), ".%llu.%s", offset, type);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100235
236 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
237 if (fd < 0) {
238 perror("open verify buf file");
239 return;
240 }
241
242 while (len) {
243 ret = write(fd, buf, len);
244 if (!ret)
245 break;
246 else if (ret < 0) {
247 perror("write verify buf file");
248 break;
249 }
250 len -= ret;
251 buf += ret;
252 }
253
254 close(fd);
Jens Axboec50ca7b2011-01-12 08:31:54 +0100255 log_err(" %s data dumped as %s\n", type, fname);
Jens Axboe6f260b32011-01-13 18:57:54 +0100256 free(ptr);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100257}
258
Jens Axboec50ca7b2011-01-12 08:31:54 +0100259/*
260 * Dump the contents of the read block and re-generate the correct data
261 * and dump that too.
262 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100263static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
264{
265 struct thread_data *td = vc->td;
266 struct io_u *io_u = vc->io_u;
267 unsigned long hdr_offset;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100268 struct io_u dummy;
Jens Axboec50ca7b2011-01-12 08:31:54 +0100269 void *buf;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100270
Steven Lang0dce9bc2011-10-25 22:41:05 +0200271 if (!td->o.verify_dump)
272 return;
273
Jens Axboec50ca7b2011-01-12 08:31:54 +0100274 /*
275 * Dump the contents we just read off disk
276 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100277 hdr_offset = vc->hdr_num * hdr->len;
278
Jens Axboec50ca7b2011-01-12 08:31:54 +0100279 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
280 "received", vc->io_u->file);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100281
Jens Axboec50ca7b2011-01-12 08:31:54 +0100282 /*
283 * Allocate a new buf and re-generate the original data
284 */
285 buf = malloc(io_u->buflen);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100286 dummy = *io_u;
Jens Axboec50ca7b2011-01-12 08:31:54 +0100287 dummy.buf = buf;
Jens Axboe4aae38a2011-01-12 08:59:12 +0100288 dummy.rand_seed = hdr->rand_seed;
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100289 dummy.buf_filled_len = 0;
Josef Bacik0d81daf2013-07-08 20:35:01 -0400290 dummy.buflen = io_u->buflen;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100291
Jens Axboec50ca7b2011-01-12 08:31:54 +0100292 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100293
Jens Axboec50ca7b2011-01-12 08:31:54 +0100294 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
295 "expected", vc->io_u->file);
Jens Axboe7d9fb452011-01-11 22:16:49 +0100296 free(buf);
297}
298
Jens Axboebfacf382010-06-18 14:29:52 +0200299static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
300{
301 unsigned long long offset;
302
303 offset = vc->io_u->offset;
304 offset += vc->hdr_num * hdr->len;
Jens Axboe32c17ad2010-06-18 14:32:21 +0200305 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
306 vc->name, vc->io_u->file->file_name, offset, hdr->len);
Jens Axboebfacf382010-06-18 14:29:52 +0200307
308 if (vc->good_crc && vc->bad_crc) {
309 log_err(" Expected CRC: ");
310 hexdump(vc->good_crc, vc->crc_len);
311 log_err(" Received CRC: ");
312 hexdump(vc->bad_crc, vc->crc_len);
313 }
Jens Axboe7d9fb452011-01-11 22:16:49 +0100314
315 dump_verify_buffers(hdr, vc);
Jens Axboebfacf382010-06-18 14:29:52 +0200316}
317
Jens Axboe936216f2010-06-18 13:44:11 +0200318/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200319 * Return data area 'header_num'
320 */
Jens Axboe936216f2010-06-18 13:44:11 +0200321static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200322{
Jens Axboe936216f2010-06-18 13:44:11 +0200323 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200324}
325
Jens Axboe92bf48d2011-01-14 21:20:42 +0100326static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
327{
328 struct thread_data *td = vc->td;
329 struct io_u *io_u = vc->io_u;
330 char *buf, *pattern;
Jens Axboe2b13e712011-01-19 14:04:16 -0700331 unsigned int header_size = __hdr_size(td->o.verify);
Steven Lang9a2a86d2012-02-07 09:42:59 +0100332 unsigned int len, mod, i, size, pattern_size;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100333
334 pattern = td->o.verify_pattern;
Steven Lang9a2a86d2012-02-07 09:42:59 +0100335 pattern_size = td->o.verify_pattern_bytes;
336 if (pattern_size <= 1)
337 pattern_size = MAX_PATTERN_SIZE;
Jens Axboe2b13e712011-01-19 14:04:16 -0700338 buf = (void *) hdr + header_size;
339 len = get_hdr_inc(td, io_u) - header_size;
Steven Lang9a2a86d2012-02-07 09:42:59 +0100340 mod = header_size % pattern_size;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100341
Steven Lang9a2a86d2012-02-07 09:42:59 +0100342 for (i = 0; i < len; i += size) {
343 size = pattern_size - mod;
344 if (size > (len - i))
345 size = len - i;
346 if (memcmp(buf + i, pattern + mod, size))
Jens Axboee4ad68b2012-02-23 08:35:04 +0100347 /* Let the slow compare find the first mismatch byte. */
Steven Lang9a2a86d2012-02-07 09:42:59 +0100348 break;
349 mod = 0;
350 }
351
352 for (; i < len; i++) {
Jens Axboe92bf48d2011-01-14 21:20:42 +0100353 if (buf[i] != pattern[mod]) {
354 unsigned int bits;
355
356 bits = hweight8(buf[i] ^ pattern[mod]);
357 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
358 buf[i], pattern[mod], bits);
359 log_err("fio: bad pattern block offset %u\n", i);
360 dump_verify_buffers(hdr, vc);
361 return EILSEQ;
362 }
363 mod++;
364 if (mod == td->o.verify_pattern_bytes)
365 mod = 0;
366 }
367
368 return 0;
369}
370
371static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
372{
373 struct thread_data *td = vc->td;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200374 struct vhdr_meta *vh = hdr_priv(hdr);
Jens Axboe936216f2010-06-18 13:44:11 +0200375 struct io_u *io_u = vc->io_u;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100376 int ret = EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200377
Jens Axboebd6f78b2008-02-01 20:27:52 +0100378 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
379
Jens Axboebfacf382010-06-18 14:29:52 +0200380 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
Jens Axboe92bf48d2011-01-14 21:20:42 +0100381 ret = 0;
382
383 if (td->o.verify_pattern_bytes)
384 ret |= verify_io_u_pattern(hdr, vc);
385
Juan Casseda0a7bd2013-09-17 14:06:12 -0700386 /*
387 * For read-only workloads, the program cannot be certain of the
388 * last numberio written to a block. Checking of numberio will be done
389 * only for workloads that write data.
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)
674 return 0;
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100675 if (hdr->len > io_u->buflen) {
676 log_err("fio: verify header exceeds buffer length (%u > %lu)\n", hdr->len, io_u->buflen);
677 return 0;
678 }
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)
682 return 1;
683
684 log_err("fio: verify header crc %x, calculated %x\n", hdr->crc32, crc);
685 return 0;
686}
687
Jens Axboe36690c92007-03-26 10:23:34 +0200688int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200689{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200690 struct verify_header *hdr;
Jens Axboe2b13e712011-01-19 14:04:16 -0700691 unsigned int header_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200692 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200693 int ret;
694
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200695 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200696 return 0;
Jens Axboe0d29de82010-09-01 13:54:15 +0200697 if (io_u->flags & IO_U_F_TRIMMED) {
698 ret = verify_trimmed_io_u(td, io_u);
699 goto done;
700 }
Jens Axboe36690c92007-03-26 10:23:34 +0200701
Jens Axboecfbcd0d2011-01-14 14:49:20 +0100702 hdr_inc = get_hdr_inc(td, io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200703
Jens Axboea12a3b42007-08-09 10:20:54 +0200704 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100705 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
706 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200707 struct vcont vc = {
708 .io_u = io_u,
709 .hdr_num = hdr_num,
Jens Axboe7d9fb452011-01-11 22:16:49 +0100710 .td = td,
Jens Axboebfacf382010-06-18 14:29:52 +0200711 };
Jens Axboef00b2102012-11-30 09:39:02 +0100712 unsigned int verify_type;
Jens Axboe936216f2010-06-18 13:44:11 +0200713
Jens Axboef3e6cb92010-06-18 14:48:43 +0200714 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200715 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200716
Jens Axboe2b13e712011-01-19 14:04:16 -0700717 header_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200718 if (td->o.verify_offset)
Jens Axboe2b13e712011-01-19 14:04:16 -0700719 memswp(p, p + td->o.verify_offset, header_size);
Jens Axboe95646102007-07-28 21:17:50 +0200720 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200721
Jens Axboe0ae2c6e2012-03-06 17:46:44 +0100722 if (!verify_header(io_u, hdr)) {
Jens Axboeae38c0d2012-02-23 10:31:07 +0100723 log_err("verify: bad magic header %x, wanted %x at "
724 "file %s offset %llu, length %u\n",
Jens Axboed3a173a2012-02-23 08:23:18 +0100725 hdr->magic, FIO_HDR_MAGIC,
Jens Axboec9b44032010-06-18 14:46:06 +0200726 io_u->file->file_name,
727 io_u->offset + hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200728 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200729 }
730
Jens Axboef00b2102012-11-30 09:39:02 +0100731 if (td->o.verify != VERIFY_NONE)
732 verify_type = td->o.verify;
733 else
734 verify_type = hdr->verify_type;
735
736 switch (verify_type) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200737 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200738 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200739 break;
740 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200741 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200742 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200743 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200744 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200745 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200746 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200747 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200748 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200749 break;
750 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200751 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200752 break;
753 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200754 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200755 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200756 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200757 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200758 break;
759 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200760 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200761 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200762 case VERIFY_META:
Jens Axboe92bf48d2011-01-14 21:20:42 +0100763 ret = verify_io_u_meta(hdr, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200764 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200765 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200766 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200767 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100768 case VERIFY_PATTERN:
769 ret = verify_io_u_pattern(hdr, &vc);
770 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200771 default:
772 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200773 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200774 }
Jens Axboef00b2102012-11-30 09:39:02 +0100775
776 if (ret && verify_type != hdr->verify_type)
777 log_err("fio: verify type mismatch (%u media, %u given)\n",
778 hdr->verify_type, verify_type);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200779 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100780
Jens Axboe0d29de82010-09-01 13:54:15 +0200781done:
Jens Axboef3e6cb92010-06-18 14:48:43 +0200782 if (ret && td->o.verify_fatal)
783 td->terminate = 1;
784
Jens Axboea12a3b42007-08-09 10:20:54 +0200785 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200786}
787
Shawn Lewis7437ee82007-08-02 21:05:58 +0200788static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100789 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200790{
791 struct vhdr_meta *vh = hdr_priv(hdr);
792
793 vh->thread = td->thread_number;
794
795 vh->time_sec = io_u->start_time.tv_sec;
796 vh->time_usec = io_u->start_time.tv_usec;
797
Juan Casseda0a7bd2013-09-17 14:06:12 -0700798 vh->numberio = io_u->numberio;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200799
800 vh->offset = io_u->offset + header_num * td->o.verify_interval;
801}
802
Jens Axboecd14cc12007-07-30 10:59:33 +0200803static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
804{
Jens Axboe546dfd92007-07-30 12:23:05 +0200805 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100806 struct fio_sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200807 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200808 };
809
Jens Axboe25dfa842012-02-29 10:01:34 +0100810 fio_sha512_init(&sha512_ctx);
811 fio_sha512_update(&sha512_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200812}
813
814static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
815{
Jens Axboe546dfd92007-07-30 12:23:05 +0200816 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100817 struct fio_sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200818 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200819 };
820
Jens Axboe25dfa842012-02-29 10:01:34 +0100821 fio_sha256_init(&sha256_ctx);
822 fio_sha256_update(&sha256_ctx, p, len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200823}
824
Jens Axboe7c353ce2009-08-09 22:40:33 +0200825static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
826{
827 struct vhdr_sha1 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100828 struct fio_sha1_ctx sha1_ctx = {
Jens Axboe7c353ce2009-08-09 22:40:33 +0200829 .H = vh->sha1,
830 };
831
Jens Axboe25dfa842012-02-29 10:01:34 +0100832 fio_sha1_init(&sha1_ctx);
833 fio_sha1_update(&sha1_ctx, p, len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200834}
835
Jens Axboe1e154bd2007-07-27 09:52:40 +0200836static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
837{
Jens Axboe546dfd92007-07-30 12:23:05 +0200838 struct vhdr_crc7 *vh = hdr_priv(hdr);
839
Jens Axboe25dfa842012-02-29 10:01:34 +0100840 vh->crc7 = fio_crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200841}
842
Jens Axboe969f7ed2007-07-27 09:07:17 +0200843static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
844{
Jens Axboe546dfd92007-07-30 12:23:05 +0200845 struct vhdr_crc16 *vh = hdr_priv(hdr);
846
Jens Axboe25dfa842012-02-29 10:01:34 +0100847 vh->crc16 = fio_crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200848}
849
Jens Axboee29d1b72006-10-18 15:43:15 +0200850static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
851{
Jens Axboe546dfd92007-07-30 12:23:05 +0200852 struct vhdr_crc32 *vh = hdr_priv(hdr);
853
Jens Axboe25dfa842012-02-29 10:01:34 +0100854 vh->crc32 = fio_crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200855}
856
Jens Axboebac39e02008-06-11 20:46:19 +0200857static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
858{
859 struct vhdr_crc32 *vh = hdr_priv(hdr);
860
Jens Axboe25dfa842012-02-29 10:01:34 +0100861 vh->crc32 = fio_crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200862}
863
Jens Axboed77a7af2007-07-27 15:35:06 +0200864static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
865{
Jens Axboe546dfd92007-07-30 12:23:05 +0200866 struct vhdr_crc64 *vh = hdr_priv(hdr);
867
Jens Axboe25dfa842012-02-29 10:01:34 +0100868 vh->crc64 = fio_crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200869}
870
Jens Axboee29d1b72006-10-18 15:43:15 +0200871static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
872{
Jens Axboe546dfd92007-07-30 12:23:05 +0200873 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe25dfa842012-02-29 10:01:34 +0100874 struct fio_md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200875 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200876 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200877
Jens Axboe25dfa842012-02-29 10:01:34 +0100878 fio_md5_init(&md5_ctx);
879 fio_md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200880}
881
Jens Axboe7d9fb452011-01-11 22:16:49 +0100882static void populate_hdr(struct thread_data *td, struct io_u *io_u,
883 struct verify_header *hdr, unsigned int header_num,
884 unsigned int header_len)
885{
886 unsigned int data_len;
887 void *data, *p;
888
889 p = (void *) hdr;
890
Jens Axboed3a173a2012-02-23 08:23:18 +0100891 hdr->magic = FIO_HDR_MAGIC;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100892 hdr->verify_type = td->o.verify;
Jens Axboed3a173a2012-02-23 08:23:18 +0100893 hdr->len = header_len;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100894 hdr->rand_seed = io_u->rand_seed;
Jens Axboe25dfa842012-02-29 10:01:34 +0100895 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
Jens Axboef65d1c22012-02-22 20:11:57 +0100896
Jens Axboe7d9fb452011-01-11 22:16:49 +0100897 data_len = header_len - hdr_size(hdr);
898
899 data = p + hdr_size(hdr);
900 switch (td->o.verify) {
901 case VERIFY_MD5:
902 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
903 io_u, hdr->len);
904 fill_md5(hdr, data, data_len);
905 break;
906 case VERIFY_CRC64:
907 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
908 io_u, hdr->len);
909 fill_crc64(hdr, data, data_len);
910 break;
911 case VERIFY_CRC32C:
912 case VERIFY_CRC32C_INTEL:
913 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
914 io_u, hdr->len);
915 fill_crc32c(hdr, data, data_len);
916 break;
917 case VERIFY_CRC32:
918 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
919 io_u, hdr->len);
920 fill_crc32(hdr, data, data_len);
921 break;
922 case VERIFY_CRC16:
923 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
924 io_u, hdr->len);
925 fill_crc16(hdr, data, data_len);
926 break;
927 case VERIFY_CRC7:
928 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
929 io_u, hdr->len);
930 fill_crc7(hdr, data, data_len);
931 break;
932 case VERIFY_SHA256:
933 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
934 io_u, hdr->len);
935 fill_sha256(hdr, data, data_len);
936 break;
937 case VERIFY_SHA512:
938 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
939 io_u, hdr->len);
940 fill_sha512(hdr, data, data_len);
941 break;
942 case VERIFY_META:
943 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
944 io_u, hdr->len);
945 fill_meta(hdr, td, io_u, header_num);
946 break;
947 case VERIFY_SHA1:
948 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
949 io_u, hdr->len);
950 fill_sha1(hdr, data, data_len);
951 break;
Jens Axboe92bf48d2011-01-14 21:20:42 +0100952 case VERIFY_PATTERN:
953 /* nothing to do here */
954 break;
Jens Axboe7d9fb452011-01-11 22:16:49 +0100955 default:
956 log_err("fio: bad verify type: %d\n", td->o.verify);
957 assert(0);
958 }
959 if (td->o.verify_offset)
960 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
961}
962
Jens Axboee29d1b72006-10-18 15:43:15 +0200963/*
964 * fill body of io_u->buf with random data and add a header with the
Jens Axboec50ca7b2011-01-12 08:31:54 +0100965 * checksum of choice
Jens Axboee29d1b72006-10-18 15:43:15 +0200966 */
967void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
968{
Jens Axboe9cc3d152007-03-26 10:32:30 +0200969 if (td->o.verify == VERIFY_NULL)
970 return;
971
Juan Casseda0a7bd2013-09-17 14:06:12 -0700972 io_u->numberio = td->io_issues[io_u->ddir];
973
Jens Axboec50ca7b2011-01-12 08:31:54 +0100974 fill_pattern_headers(td, io_u, 0, 0);
Jens Axboee29d1b72006-10-18 15:43:15 +0200975}
976
977int get_next_verify(struct thread_data *td, struct io_u *io_u)
978{
Jens Axboe8de8f042007-03-27 10:36:12 +0200979 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200980
Jens Axboed2d7fa52007-02-19 13:21:35 +0100981 /*
982 * this io_u is from a requeue, we already filled the offsets
983 */
984 if (io_u->file)
985 return 0;
986
Jens Axboe8de8f042007-03-27 10:36:12 +0200987 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
988 struct rb_node *n = rb_first(&td->io_hist_tree);
989
Jens Axboe4b878982007-03-26 09:32:22 +0200990 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200991 rb_erase(n, &td->io_hist_tree);
Jens Axboea917a8b2010-09-02 13:23:20 +0200992 assert(ipo->flags & IP_F_ONRB);
993 ipo->flags &= ~IP_F_ONRB;
Jens Axboe01743ee2008-06-02 12:19:19 +0200994 } else if (!flist_empty(&td->io_hist_list)) {
995 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
996 flist_del(&ipo->list);
Jens Axboea917a8b2010-09-02 13:23:20 +0200997 assert(ipo->flags & IP_F_ONLIST);
998 ipo->flags &= ~IP_F_ONLIST;
Jens Axboe8de8f042007-03-27 10:36:12 +0200999 }
Jens Axboee29d1b72006-10-18 15:43:15 +02001000
Jens Axboe8de8f042007-03-27 10:36:12 +02001001 if (ipo) {
Jens Axboe0d29de82010-09-01 13:54:15 +02001002 td->io_hist_len--;
1003
Jens Axboee29d1b72006-10-18 15:43:15 +02001004 io_u->offset = ipo->offset;
1005 io_u->buflen = ipo->len;
Juan Casseda0a7bd2013-09-17 14:06:12 -07001006 io_u->numberio = ipo->numberio;
Jens Axboe36167d82007-02-18 05:41:31 +01001007 io_u->file = ipo->file;
Jens Axboe82af2a72012-03-13 13:45:58 +01001008 io_u->flags |= IO_U_F_VER_LIST;
Jens Axboe97af62c2007-05-22 11:12:13 +02001009
Jens Axboe0d29de82010-09-01 13:54:15 +02001010 if (ipo->flags & IP_F_TRIMMED)
1011 io_u->flags |= IO_U_F_TRIMMED;
1012
Jens Axboed6aed792009-06-03 08:41:15 +02001013 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +02001014 int r = td_io_open_file(td, io_u->file);
1015
Jens Axboebd6f78b2008-02-01 20:27:52 +01001016 if (r) {
1017 dprint(FD_VERIFY, "failed file %s open\n",
1018 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +02001019 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +01001020 }
Jens Axboe97af62c2007-05-22 11:12:13 +02001021 }
1022
1023 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +02001024 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +02001025 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +01001026 io_u->xfer_buf = io_u->buf;
1027 io_u->xfer_buflen = io_u->buflen;
Jens Axboe0d29de82010-09-01 13:54:15 +02001028
1029 remove_trim_entry(td, ipo);
Jens Axboee29d1b72006-10-18 15:43:15 +02001030 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001031 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +02001032 return 0;
1033 }
1034
Jens Axboebd6f78b2008-02-01 20:27:52 +01001035 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +02001036 return 1;
1037}
Jens Axboee8462bd2009-07-06 12:59:04 +02001038
Jens Axboedc5bfbb2013-04-10 22:23:40 +02001039void fio_verify_init(struct thread_data *td)
1040{
1041 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1042 td->o.verify == VERIFY_CRC32C) {
1043 crc32c_intel_probe();
1044 }
1045}
1046
Jens Axboee8462bd2009-07-06 12:59:04 +02001047static void *verify_async_thread(void *data)
1048{
1049 struct thread_data *td = data;
1050 struct io_u *io_u;
1051 int ret = 0;
1052
1053 if (td->o.verify_cpumask_set &&
1054 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1055 log_err("fio: failed setting verify thread affinity\n");
1056 goto done;
1057 }
1058
1059 do {
Jens Axboee53ab272009-07-06 13:43:46 +02001060 FLIST_HEAD(list);
1061
Jens Axboee8462bd2009-07-06 12:59:04 +02001062 read_barrier();
1063 if (td->verify_thread_exit)
1064 break;
1065
1066 pthread_mutex_lock(&td->io_u_lock);
1067
1068 while (flist_empty(&td->verify_list) &&
1069 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +02001070 ret = pthread_cond_wait(&td->verify_cond,
1071 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +02001072 if (ret) {
1073 pthread_mutex_unlock(&td->io_u_lock);
1074 break;
1075 }
1076 }
1077
Jens Axboee53ab272009-07-06 13:43:46 +02001078 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001079 pthread_mutex_unlock(&td->io_u_lock);
1080
Jens Axboee53ab272009-07-06 13:43:46 +02001081 if (flist_empty(&list))
1082 continue;
1083
1084 while (!flist_empty(&list)) {
Jens Axboe2ae0b202013-05-28 14:16:55 +02001085 io_u = flist_entry(list.next, struct io_u, verify_list);
1086 flist_del(&io_u->verify_list);
Jens Axboee53ab272009-07-06 13:43:46 +02001087
Jens Axboed561f2a2009-07-06 13:51:05 +02001088 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +02001089 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +02001090 if (!ret)
1091 continue;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001092 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
Jens Axboed561f2a2009-07-06 13:51:05 +02001093 update_error_count(td, ret);
1094 td_clear_error(td);
1095 ret = 0;
1096 }
Jens Axboee53ab272009-07-06 13:43:46 +02001097 }
Jens Axboee8462bd2009-07-06 12:59:04 +02001098 } while (!ret);
1099
Jens Axboed561f2a2009-07-06 13:51:05 +02001100 if (ret) {
1101 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +02001102 if (td->o.verify_fatal)
1103 td->terminate = 1;
Jens Axboed561f2a2009-07-06 13:51:05 +02001104 }
1105
Jens Axboee8462bd2009-07-06 12:59:04 +02001106done:
1107 pthread_mutex_lock(&td->io_u_lock);
1108 td->nr_verify_threads--;
1109 pthread_mutex_unlock(&td->io_u_lock);
1110
1111 pthread_cond_signal(&td->free_cond);
1112 return NULL;
1113}
1114
1115int verify_async_init(struct thread_data *td)
1116{
1117 int i, ret;
bart Van Assche304a47c2010-08-01 21:31:26 +02001118 pthread_attr_t attr;
1119
1120 pthread_attr_init(&attr);
1121 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
Jens Axboee8462bd2009-07-06 12:59:04 +02001122
1123 td->verify_thread_exit = 0;
1124
1125 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1126 for (i = 0; i < td->o.verify_async; i++) {
bart Van Assche304a47c2010-08-01 21:31:26 +02001127 ret = pthread_create(&td->verify_threads[i], &attr,
Jens Axboee8462bd2009-07-06 12:59:04 +02001128 verify_async_thread, td);
1129 if (ret) {
1130 log_err("fio: async verify creation failed: %s\n",
1131 strerror(ret));
1132 break;
1133 }
1134 ret = pthread_detach(td->verify_threads[i]);
1135 if (ret) {
1136 log_err("fio: async verify thread detach failed: %s\n",
1137 strerror(ret));
1138 break;
1139 }
1140 td->nr_verify_threads++;
1141 }
1142
bart Van Assche304a47c2010-08-01 21:31:26 +02001143 pthread_attr_destroy(&attr);
1144
Jens Axboee8462bd2009-07-06 12:59:04 +02001145 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +02001146 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +02001147 td->verify_thread_exit = 1;
1148 write_barrier();
1149 pthread_cond_broadcast(&td->verify_cond);
1150 return 1;
1151 }
1152
1153 return 0;
1154}
1155
1156void verify_async_exit(struct thread_data *td)
1157{
1158 td->verify_thread_exit = 1;
1159 write_barrier();
1160 pthread_cond_broadcast(&td->verify_cond);
1161
1162 pthread_mutex_lock(&td->io_u_lock);
1163
1164 while (td->nr_verify_threads)
1165 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1166
1167 pthread_mutex_unlock(&td->io_u_lock);
1168 free(td->verify_threads);
1169 td->verify_threads = NULL;
1170}