blob: 7957bd4110d88bea8aecc242c6460f739871ef99 [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 Axboee29d1b72006-10-18 15:43:15 +02009
10#include "fio.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020011#include "verify.h"
Jens Axboee8462bd2009-07-06 12:59:04 +020012#include "smalloc.h"
Jens Axboe637ef8d2010-06-21 20:39:38 +020013#include "lib/rand.h"
Jens Axboee29d1b72006-10-18 15:43:15 +020014
Jens Axboeeef6eea2007-07-30 12:27:58 +020015#include "crc/md5.h"
16#include "crc/crc64.h"
17#include "crc/crc32.h"
Jens Axboebac39e02008-06-11 20:46:19 +020018#include "crc/crc32c.h"
Jens Axboeeef6eea2007-07-30 12:27:58 +020019#include "crc/crc16.h"
20#include "crc/crc7.h"
21#include "crc/sha256.h"
22#include "crc/sha512.h"
Jens Axboe7c353ce2009-08-09 22:40:33 +020023#include "crc/sha1.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020024
Radha Ramachandrancbe8d752010-07-14 08:36:07 +020025void fill_pattern(struct thread_data *td, void *p, unsigned int len, struct io_u *io_u)
Jens Axboe90059d62007-07-30 09:33:12 +020026{
27 switch (td->o.verify_pattern_bytes) {
28 case 0:
Jens Axboebd6f78b2008-02-01 20:27:52 +010029 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
Jens Axboe637ef8d2010-06-21 20:39:38 +020030 fill_random_buf(p, len);
Jens Axboe90059d62007-07-30 09:33:12 +020031 break;
32 case 1:
Radha Ramachandran10e8a7b2010-07-14 17:39:05 -060033 if (io_u->buf_filled_len >= len) {
Radha Ramachandrancbe8d752010-07-14 08:36:07 +020034 dprint(FD_VERIFY, "using already filled verify pattern b=0 len=%u\n", len);
35 return;
36 }
Jens Axboebd6f78b2008-02-01 20:27:52 +010037 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Radha Ramachandran0e92f872009-10-27 20:14:27 +010038 memset(p, td->o.verify_pattern[0], len);
Radha Ramachandrancbe8d752010-07-14 08:36:07 +020039 io_u->buf_filled_len = len;
Jens Axboe90059d62007-07-30 09:33:12 +020040 break;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010041 default: {
42 unsigned int i = 0, size = 0;
Jens Axboe90059d62007-07-30 09:33:12 +020043 unsigned char *b = p;
44
Radha Ramachandran10e8a7b2010-07-14 17:39:05 -060045 if (io_u->buf_filled_len >= len) {
Radha Ramachandrancbe8d752010-07-14 08:36:07 +020046 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
47 td->o.verify_pattern_bytes, len);
48 return;
49 }
Jens Axboebd6f78b2008-02-01 20:27:52 +010050 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
51 td->o.verify_pattern_bytes, len);
52
Jens Axboe90059d62007-07-30 09:33:12 +020053 while (i < len) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +010054 size = td->o.verify_pattern_bytes;
55 if (size > (len - i))
56 size = len - i;
57 memcpy(b+i, td->o.verify_pattern, size);
58 i += size;
Jens Axboe90059d62007-07-30 09:33:12 +020059 }
Radha Ramachandrancbe8d752010-07-14 08:36:07 +020060 io_u->buf_filled_len = len;
Jens Axboe90059d62007-07-30 09:33:12 +020061 break;
62 }
63 }
64}
65
Jens Axboe4764aec2007-08-23 09:03:38 +020066static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020067{
Shawn Lewisdee6de72007-08-02 22:17:52 +020068 char swap[200];
69
70 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020071
Shawn Lewis546a9142007-07-28 21:11:37 +020072 memcpy(&swap, buf1, len);
73 memcpy(buf1, buf2, len);
74 memcpy(buf2, &swap, len);
75}
76
Jens Axboee29d1b72006-10-18 15:43:15 +020077static void hexdump(void *buffer, int len)
78{
79 unsigned char *p = buffer;
80 int i;
81
82 for (i = 0; i < len; i++)
Jens Axboebfacf382010-06-18 14:29:52 +020083 log_err("%02x", p[i]);
84 log_err("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020085}
86
Jens Axboed9f2caf2007-07-28 21:22:03 +020087/*
Jens Axboe87677832007-07-30 12:08:14 +020088 * Prepare for seperation of verify_header and checksum header
89 */
Jens Axboe546dfd92007-07-30 12:23:05 +020090static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +020091{
Jens Axboe5921e802008-05-30 15:02:38 +020092 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +020093
Jens Axboe546dfd92007-07-30 12:23:05 +020094 switch (verify_type) {
95 case VERIFY_NONE:
96 case VERIFY_NULL:
97 len = 0;
98 break;
99 case VERIFY_MD5:
100 len = sizeof(struct vhdr_md5);
101 break;
102 case VERIFY_CRC64:
103 len = sizeof(struct vhdr_crc64);
104 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200105 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200106 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200107 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200108 len = sizeof(struct vhdr_crc32);
109 break;
110 case VERIFY_CRC16:
111 len = sizeof(struct vhdr_crc16);
112 break;
113 case VERIFY_CRC7:
114 len = sizeof(struct vhdr_crc7);
115 break;
116 case VERIFY_SHA256:
117 len = sizeof(struct vhdr_sha256);
118 break;
119 case VERIFY_SHA512:
120 len = sizeof(struct vhdr_sha512);
121 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200122 case VERIFY_META:
123 len = sizeof(struct vhdr_meta);
124 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200125 case VERIFY_SHA1:
126 len = sizeof(struct vhdr_sha1);
127 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200128 default:
129 log_err("fio: unknown verify header!\n");
130 assert(0);
131 }
132
133 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200134}
135
136static inline unsigned int hdr_size(struct verify_header *hdr)
137{
Jens Axboe546dfd92007-07-30 12:23:05 +0200138 return __hdr_size(hdr->verify_type);
139}
140
141static void *hdr_priv(struct verify_header *hdr)
142{
143 void *priv = hdr;
144
145 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200146}
147
148/*
Jens Axboe936216f2010-06-18 13:44:11 +0200149 * Verify container, pass info to verify handlers and allow them to
150 * pass info back in case of error
151 */
152struct vcont {
153 /*
154 * Input
155 */
156 struct io_u *io_u;
157 unsigned int hdr_num;
158
159 /*
160 * Output, only valid in case of error
161 */
Jens Axboebfacf382010-06-18 14:29:52 +0200162 const char *name;
163 void *good_crc;
164 void *bad_crc;
Jens Axboe936216f2010-06-18 13:44:11 +0200165 unsigned int crc_len;
166};
167
Jens Axboebfacf382010-06-18 14:29:52 +0200168static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
169{
170 unsigned long long offset;
171
172 offset = vc->io_u->offset;
173 offset += vc->hdr_num * hdr->len;
Jens Axboe32c17ad2010-06-18 14:32:21 +0200174 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
175 vc->name, vc->io_u->file->file_name, offset, hdr->len);
Jens Axboebfacf382010-06-18 14:29:52 +0200176
177 if (vc->good_crc && vc->bad_crc) {
178 log_err(" Expected CRC: ");
179 hexdump(vc->good_crc, vc->crc_len);
180 log_err(" Received CRC: ");
181 hexdump(vc->bad_crc, vc->crc_len);
182 }
183}
184
Jens Axboe936216f2010-06-18 13:44:11 +0200185/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200186 * Return data area 'header_num'
187 */
Jens Axboe936216f2010-06-18 13:44:11 +0200188static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200189{
Jens Axboe936216f2010-06-18 13:44:11 +0200190 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200191}
192
Shawn Lewis7437ee82007-08-02 21:05:58 +0200193static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe936216f2010-06-18 13:44:11 +0200194 struct vcont *vc)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200195{
196 struct vhdr_meta *vh = hdr_priv(hdr);
Jens Axboe936216f2010-06-18 13:44:11 +0200197 struct io_u *io_u = vc->io_u;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200198
Jens Axboebd6f78b2008-02-01 20:27:52 +0100199 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
200
Jens Axboebfacf382010-06-18 14:29:52 +0200201 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
202 return 0;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200203
Jens Axboebfacf382010-06-18 14:29:52 +0200204 vc->name = "meta";
205 log_verify_failure(hdr, vc);
206 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200207}
208
Jens Axboe936216f2010-06-18 13:44:11 +0200209static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200210{
Jens Axboe936216f2010-06-18 13:44:11 +0200211 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200212 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200213 uint8_t sha512[128];
214 struct sha512_ctx sha512_ctx = {
215 .buf = sha512,
216 };
217
Jens Axboe936216f2010-06-18 13:44:11 +0200218 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100219
Jens Axboecd14cc12007-07-30 10:59:33 +0200220 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200221 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200222
Jens Axboebfacf382010-06-18 14:29:52 +0200223 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
224 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200225
Jens Axboebfacf382010-06-18 14:29:52 +0200226 vc->name = "sha512";
227 vc->good_crc = vh->sha512;
228 vc->bad_crc = sha512_ctx.buf;
229 vc->crc_len = sizeof(vh->sha512);
230 log_verify_failure(hdr, vc);
231 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200232}
233
Jens Axboe936216f2010-06-18 13:44:11 +0200234static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200235{
Jens Axboe936216f2010-06-18 13:44:11 +0200236 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200237 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100238 uint8_t sha256[64];
Jens Axboecd14cc12007-07-30 10:59:33 +0200239 struct sha256_ctx sha256_ctx = {
240 .buf = sha256,
241 };
242
Jens Axboe936216f2010-06-18 13:44:11 +0200243 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100244
Jens Axboecd14cc12007-07-30 10:59:33 +0200245 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200246 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200247
Jens Axboebfacf382010-06-18 14:29:52 +0200248 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
249 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200250
Jens Axboebfacf382010-06-18 14:29:52 +0200251 vc->name = "sha256";
252 vc->good_crc = vh->sha256;
253 vc->bad_crc = sha256_ctx.buf;
254 vc->crc_len = sizeof(vh->sha256);
255 log_verify_failure(hdr, vc);
256 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200257}
258
Jens Axboe936216f2010-06-18 13:44:11 +0200259static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200260{
Jens Axboe936216f2010-06-18 13:44:11 +0200261 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200262 struct vhdr_sha1 *vh = hdr_priv(hdr);
263 uint32_t sha1[5];
264 struct sha1_ctx sha1_ctx = {
265 .H = sha1,
266 };
267
Jens Axboe936216f2010-06-18 13:44:11 +0200268 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200269
270 sha1_init(&sha1_ctx);
271 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
272
Jens Axboebfacf382010-06-18 14:29:52 +0200273 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
274 return 0;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200275
Jens Axboebfacf382010-06-18 14:29:52 +0200276 vc->name = "sha1";
277 vc->good_crc = vh->sha1;
278 vc->bad_crc = sha1_ctx.H;
279 vc->crc_len = sizeof(vh->sha1);
280 log_verify_failure(hdr, vc);
281 return EILSEQ;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200282}
283
Jens Axboe936216f2010-06-18 13:44:11 +0200284static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200285{
Jens Axboe936216f2010-06-18 13:44:11 +0200286 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200287 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200288 unsigned char c;
289
Jens Axboe936216f2010-06-18 13:44:11 +0200290 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100291
Jens Axboe87677832007-07-30 12:08:14 +0200292 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200293
Jens Axboebfacf382010-06-18 14:29:52 +0200294 if (c == vh->crc7)
295 return 0;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200296
Jens Axboebfacf382010-06-18 14:29:52 +0200297 vc->name = "crc7";
298 vc->good_crc = &vh->crc7;
299 vc->bad_crc = &c;
300 vc->crc_len = 1;
301 log_verify_failure(hdr, vc);
302 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200303}
304
Jens Axboe936216f2010-06-18 13:44:11 +0200305static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200306{
Jens Axboe936216f2010-06-18 13:44:11 +0200307 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200308 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200309 unsigned short c;
310
Jens Axboe936216f2010-06-18 13:44:11 +0200311 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100312
Jens Axboe87677832007-07-30 12:08:14 +0200313 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200314
Jens Axboebfacf382010-06-18 14:29:52 +0200315 if (c == vh->crc16)
316 return 0;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200317
Jens Axboebfacf382010-06-18 14:29:52 +0200318 vc->name = "crc16";
319 vc->good_crc = &vh->crc16;
320 vc->bad_crc = &c;
321 vc->crc_len = 2;
322 log_verify_failure(hdr, vc);
323 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200324}
325
Jens Axboe936216f2010-06-18 13:44:11 +0200326static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200327{
Jens Axboe936216f2010-06-18 13:44:11 +0200328 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200329 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200330 unsigned long long c;
331
Jens Axboe936216f2010-06-18 13:44:11 +0200332 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100333
Jens Axboe87677832007-07-30 12:08:14 +0200334 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200335
Jens Axboebfacf382010-06-18 14:29:52 +0200336 if (c == vh->crc64)
337 return 0;
Jens Axboed77a7af2007-07-27 15:35:06 +0200338
Jens Axboebfacf382010-06-18 14:29:52 +0200339 vc->name = "crc64";
340 vc->good_crc = &vh->crc64;
341 vc->bad_crc = &c;
342 vc->crc_len = 8;
343 log_verify_failure(hdr, vc);
344 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200345}
346
Jens Axboe936216f2010-06-18 13:44:11 +0200347static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200348{
Jens Axboe936216f2010-06-18 13:44:11 +0200349 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200350 struct vhdr_crc32 *vh = hdr_priv(hdr);
351 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200352
Jens Axboe936216f2010-06-18 13:44:11 +0200353 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100354
Jens Axboe87677832007-07-30 12:08:14 +0200355 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200356
Jens Axboebfacf382010-06-18 14:29:52 +0200357 if (c == vh->crc32)
358 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200359
Jens Axboebfacf382010-06-18 14:29:52 +0200360 vc->name = "crc32";
361 vc->good_crc = &vh->crc32;
362 vc->bad_crc = &c;
363 vc->crc_len = 4;
364 log_verify_failure(hdr, vc);
365 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200366}
367
Jens Axboe936216f2010-06-18 13:44:11 +0200368static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200369{
Jens Axboe936216f2010-06-18 13:44:11 +0200370 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200371 struct vhdr_crc32 *vh = hdr_priv(hdr);
372 uint32_t c;
373
Jens Axboe936216f2010-06-18 13:44:11 +0200374 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200375
Jens Axboe38455912008-08-04 15:35:26 +0200376 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
377 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
378 else
379 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200380
Jens Axboebfacf382010-06-18 14:29:52 +0200381 if (c == vh->crc32)
382 return 0;
Jens Axboebac39e02008-06-11 20:46:19 +0200383
Jens Axboebfacf382010-06-18 14:29:52 +0200384 vc->name = "crc32c";
385 vc->good_crc = &vh->crc32;
386 vc->bad_crc = &c;
387 vc->crc_len = 4;
388 log_verify_failure(hdr, vc);
389 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200390}
391
Jens Axboe936216f2010-06-18 13:44:11 +0200392static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200393{
Jens Axboe936216f2010-06-18 13:44:11 +0200394 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200395 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200396 uint32_t hash[MD5_HASH_WORDS];
397 struct md5_ctx md5_ctx = {
398 .hash = hash,
399 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200400
Jens Axboe936216f2010-06-18 13:44:11 +0200401 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100402
Jens Axboe61f821f2007-07-30 10:18:06 +0200403 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200404 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200405
Jens Axboebfacf382010-06-18 14:29:52 +0200406 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
407 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200408
Jens Axboebfacf382010-06-18 14:29:52 +0200409 vc->name = "md5";
410 vc->good_crc = vh->md5_digest;
411 vc->bad_crc = md5_ctx.hash;
412 vc->crc_len = sizeof(hash);
413 log_verify_failure(hdr, vc);
414 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200415}
416
Jens Axboe3f199b02007-08-09 10:16:31 +0200417static unsigned int hweight8(unsigned int w)
418{
419 unsigned int res = w - ((w >> 1) & 0x55);
420
421 res = (res & 0x33) + ((res >> 2) & 0x33);
422 return (res + (res >> 4)) & 0x0F;
423}
424
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100425int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100426 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200427{
428 unsigned int i;
Shawn Lewisa944e332007-08-02 22:18:29 +0200429
430 for (i = 0; i < len; i++) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100431 if (buf[i] != pattern[mod]) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200432 unsigned int bits;
433
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100434 bits = hweight8(buf[i] ^ pattern[mod]);
Jens Axboe3f199b02007-08-09 10:16:31 +0200435 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100436 buf[i], pattern[mod], bits);
Jens Axboe3f199b02007-08-09 10:16:31 +0200437 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200438 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200439 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200440 mod++;
441 if (mod == pattern_size)
442 mod = 0;
443 }
444
445 return 0;
446}
447
Jens Axboee8462bd2009-07-06 12:59:04 +0200448/*
449 * Push IO verification to a separate thread
450 */
451int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
452{
453 if (io_u->file)
454 put_file_log(td, io_u->file);
455
456 io_u->file = NULL;
457
458 pthread_mutex_lock(&td->io_u_lock);
Radha Ramachandran0c412142009-11-03 21:45:31 +0100459
460 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
461 td->cur_depth--;
462 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
463 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200464 flist_del(&io_u->list);
465 flist_add_tail(&io_u->list, &td->verify_list);
Jens Axboe2ecc1b52009-11-04 20:58:09 +0100466 io_u->flags |= IO_U_F_FREE_DEF;
Jens Axboee8462bd2009-07-06 12:59:04 +0200467 pthread_mutex_unlock(&td->io_u_lock);
468
469 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200470 return 0;
471}
472
Jens Axboe36690c92007-03-26 10:23:34 +0200473int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200474{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200475 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200476 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200477 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200478 int ret;
479
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200480 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200481 return 0;
482
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200483 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200484 if (td->o.verify_interval)
485 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200486
Jens Axboea12a3b42007-08-09 10:20:54 +0200487 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100488 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
489 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200490 struct vcont vc = {
491 .io_u = io_u,
492 .hdr_num = hdr_num,
493 };
Jens Axboe936216f2010-06-18 13:44:11 +0200494
Jens Axboef3e6cb92010-06-18 14:48:43 +0200495 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200496 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200497
Shawn Lewisa944e332007-08-02 22:18:29 +0200498 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200499 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200500 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200501 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200502
Jens Axboe3f199b02007-08-09 10:16:31 +0200503 if (hdr->fio_magic != FIO_HDR_MAGIC) {
Jens Axboec9b44032010-06-18 14:46:06 +0200504 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
505 hdr->fio_magic, FIO_HDR_MAGIC,
506 io_u->file->file_name,
507 io_u->offset + hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200508 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200509 }
510
Shawn Lewise28218f2008-01-16 11:01:33 +0100511 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100512 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
513 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100514 ret = verify_io_u_pattern(td->o.verify_pattern,
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100515 td->o.verify_pattern_bytes,
516 p + hdr_size,
517 hdr_inc - hdr_size,
518 hdr_size % td->o.verify_pattern_bytes);
Jens Axboec9b44032010-06-18 14:46:06 +0200519
520 if (ret) {
521 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
522 io_u->file->file_name,
523 io_u->offset + hdr_num * hdr->len,
524 hdr->len);
525 }
526
Radha Ramachandrane92d3d72009-07-27 12:27:55 +0200527 /*
528 * Also verify the meta data, if applicable
529 */
530 if (hdr->verify_type == VERIFY_META)
Jens Axboe936216f2010-06-18 13:44:11 +0200531 ret |= verify_io_u_meta(hdr, td, &vc);
Shawn Lewise28218f2008-01-16 11:01:33 +0100532 continue;
533 }
534
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200535 switch (hdr->verify_type) {
536 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200537 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200538 break;
539 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200540 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200541 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200542 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200543 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200544 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200545 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200546 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200547 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200548 break;
549 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200550 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200551 break;
552 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200553 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200554 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200555 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200556 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200557 break;
558 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200559 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200560 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200561 case VERIFY_META:
Jens Axboe936216f2010-06-18 13:44:11 +0200562 ret = verify_io_u_meta(hdr, td, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200563 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200564 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200565 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200566 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200567 default:
568 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200569 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200570 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200571 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100572
Jens Axboef3e6cb92010-06-18 14:48:43 +0200573 if (ret && td->o.verify_fatal)
574 td->terminate = 1;
575
Jens Axboea12a3b42007-08-09 10:20:54 +0200576 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200577}
578
Shawn Lewis7437ee82007-08-02 21:05:58 +0200579static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100580 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200581{
582 struct vhdr_meta *vh = hdr_priv(hdr);
583
584 vh->thread = td->thread_number;
585
586 vh->time_sec = io_u->start_time.tv_sec;
587 vh->time_usec = io_u->start_time.tv_usec;
588
589 vh->numberio = td->io_issues[DDIR_WRITE];
590
591 vh->offset = io_u->offset + header_num * td->o.verify_interval;
592}
593
Jens Axboecd14cc12007-07-30 10:59:33 +0200594static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
595{
Jens Axboe546dfd92007-07-30 12:23:05 +0200596 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200597 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200598 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200599 };
600
601 sha512_init(&sha512_ctx);
602 sha512_update(&sha512_ctx, p, len);
603}
604
605static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
606{
Jens Axboe546dfd92007-07-30 12:23:05 +0200607 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200608 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200609 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200610 };
611
612 sha256_init(&sha256_ctx);
613 sha256_update(&sha256_ctx, p, len);
614}
615
Jens Axboe7c353ce2009-08-09 22:40:33 +0200616static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
617{
618 struct vhdr_sha1 *vh = hdr_priv(hdr);
619 struct sha1_ctx sha1_ctx = {
620 .H = vh->sha1,
621 };
622
623 sha1_init(&sha1_ctx);
624 sha1_update(&sha1_ctx, p, len);
625}
626
Jens Axboe1e154bd2007-07-27 09:52:40 +0200627static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
628{
Jens Axboe546dfd92007-07-30 12:23:05 +0200629 struct vhdr_crc7 *vh = hdr_priv(hdr);
630
631 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200632}
633
Jens Axboe969f7ed2007-07-27 09:07:17 +0200634static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
635{
Jens Axboe546dfd92007-07-30 12:23:05 +0200636 struct vhdr_crc16 *vh = hdr_priv(hdr);
637
638 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200639}
640
Jens Axboee29d1b72006-10-18 15:43:15 +0200641static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
642{
Jens Axboe546dfd92007-07-30 12:23:05 +0200643 struct vhdr_crc32 *vh = hdr_priv(hdr);
644
645 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200646}
647
Jens Axboebac39e02008-06-11 20:46:19 +0200648static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
649{
650 struct vhdr_crc32 *vh = hdr_priv(hdr);
651
Jens Axboe38455912008-08-04 15:35:26 +0200652 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
653 vh->crc32 = crc32c_intel(p, len);
654 else
655 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200656}
657
Jens Axboed77a7af2007-07-27 15:35:06 +0200658static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
659{
Jens Axboe546dfd92007-07-30 12:23:05 +0200660 struct vhdr_crc64 *vh = hdr_priv(hdr);
661
662 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200663}
664
Jens Axboee29d1b72006-10-18 15:43:15 +0200665static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
666{
Jens Axboe546dfd92007-07-30 12:23:05 +0200667 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200668 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200669 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200670 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200671
Jens Axboe61f821f2007-07-30 10:18:06 +0200672 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200673 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200674}
675
676/*
677 * fill body of io_u->buf with random data and add a header with the
678 * crc32 or md5 sum of that data.
679 */
680void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
681{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200682 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200683 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200684 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200685
Jens Axboe9cc3d152007-03-26 10:32:30 +0200686 if (td->o.verify == VERIFY_NULL)
687 return;
688
Radha Ramachandrancbe8d752010-07-14 08:36:07 +0200689 fill_pattern(td, p, io_u->buflen, io_u);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200690
Shawn Lewis546a9142007-07-28 21:11:37 +0200691 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200692 if (td->o.verify_interval)
693 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200694
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100695 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200696 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200697
698 hdr->fio_magic = FIO_HDR_MAGIC;
699 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200700 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200701 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200702
Jens Axboe87677832007-07-30 12:08:14 +0200703 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200704 switch (td->o.verify) {
705 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100706 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
707 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200708 fill_md5(hdr, data, data_len);
709 break;
710 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100711 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
712 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200713 fill_crc64(hdr, data, data_len);
714 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200715 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200716 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200717 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
718 io_u, hdr->len);
719 fill_crc32c(hdr, data, data_len);
720 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200721 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100722 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
723 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200724 fill_crc32(hdr, data, data_len);
725 break;
726 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100727 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
728 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200729 fill_crc16(hdr, data, data_len);
730 break;
731 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100732 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
733 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200734 fill_crc7(hdr, data, data_len);
735 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200736 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100737 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
738 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200739 fill_sha256(hdr, data, data_len);
740 break;
741 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100742 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
743 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200744 fill_sha512(hdr, data, data_len);
745 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200746 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100747 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
748 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200749 fill_meta(hdr, td, io_u, header_num);
750 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200751 case VERIFY_SHA1:
752 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
753 io_u, hdr->len);
754 fill_sha1(hdr, data, data_len);
755 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200756 default:
757 log_err("fio: bad verify type: %d\n", td->o.verify);
758 assert(0);
759 }
Jens Axboea59e1702007-07-30 08:53:27 +0200760 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200761 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200762 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200763 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200764}
765
766int get_next_verify(struct thread_data *td, struct io_u *io_u)
767{
Jens Axboe8de8f042007-03-27 10:36:12 +0200768 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200769
Jens Axboed2d7fa52007-02-19 13:21:35 +0100770 /*
771 * this io_u is from a requeue, we already filled the offsets
772 */
773 if (io_u->file)
774 return 0;
775
Jens Axboe8de8f042007-03-27 10:36:12 +0200776 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
777 struct rb_node *n = rb_first(&td->io_hist_tree);
778
Jens Axboe4b878982007-03-26 09:32:22 +0200779 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200780 rb_erase(n, &td->io_hist_tree);
Jens Axboe9e144182010-06-15 14:25:36 +0200781 td->io_hist_len--;
Jens Axboe01743ee2008-06-02 12:19:19 +0200782 } else if (!flist_empty(&td->io_hist_list)) {
783 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboe9e144182010-06-15 14:25:36 +0200784 td->io_hist_len--;
Jens Axboe01743ee2008-06-02 12:19:19 +0200785 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200786 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200787
Jens Axboe8de8f042007-03-27 10:36:12 +0200788 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200789 io_u->offset = ipo->offset;
790 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100791 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200792
Jens Axboed6aed792009-06-03 08:41:15 +0200793 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200794 int r = td_io_open_file(td, io_u->file);
795
Jens Axboebd6f78b2008-02-01 20:27:52 +0100796 if (r) {
797 dprint(FD_VERIFY, "failed file %s open\n",
798 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200799 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100800 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200801 }
802
803 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200804 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200805 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100806 io_u->xfer_buf = io_u->buf;
807 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200808 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100809 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200810 return 0;
811 }
812
Jens Axboebd6f78b2008-02-01 20:27:52 +0100813 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200814 return 1;
815}
Jens Axboee8462bd2009-07-06 12:59:04 +0200816
817static void *verify_async_thread(void *data)
818{
819 struct thread_data *td = data;
820 struct io_u *io_u;
821 int ret = 0;
822
823 if (td->o.verify_cpumask_set &&
824 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
825 log_err("fio: failed setting verify thread affinity\n");
826 goto done;
827 }
828
829 do {
Jens Axboee53ab272009-07-06 13:43:46 +0200830 FLIST_HEAD(list);
831
Jens Axboee8462bd2009-07-06 12:59:04 +0200832 read_barrier();
833 if (td->verify_thread_exit)
834 break;
835
836 pthread_mutex_lock(&td->io_u_lock);
837
838 while (flist_empty(&td->verify_list) &&
839 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +0200840 ret = pthread_cond_wait(&td->verify_cond,
841 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +0200842 if (ret) {
843 pthread_mutex_unlock(&td->io_u_lock);
844 break;
845 }
846 }
847
Jens Axboee53ab272009-07-06 13:43:46 +0200848 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +0200849 pthread_mutex_unlock(&td->io_u_lock);
850
Jens Axboee53ab272009-07-06 13:43:46 +0200851 if (flist_empty(&list))
852 continue;
853
854 while (!flist_empty(&list)) {
855 io_u = flist_entry(list.next, struct io_u, list);
856 flist_del_init(&io_u->list);
857
Jens Axboed561f2a2009-07-06 13:51:05 +0200858 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +0200859 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +0200860 if (!ret)
861 continue;
862 if (td->o.continue_on_error &&
863 td_non_fatal_error(ret)) {
864 update_error_count(td, ret);
865 td_clear_error(td);
866 ret = 0;
867 }
Jens Axboee53ab272009-07-06 13:43:46 +0200868 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200869 } while (!ret);
870
Jens Axboed561f2a2009-07-06 13:51:05 +0200871 if (ret) {
872 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +0200873 if (td->o.verify_fatal)
874 td->terminate = 1;
Jens Axboed561f2a2009-07-06 13:51:05 +0200875 }
876
Jens Axboee8462bd2009-07-06 12:59:04 +0200877done:
878 pthread_mutex_lock(&td->io_u_lock);
879 td->nr_verify_threads--;
880 pthread_mutex_unlock(&td->io_u_lock);
881
882 pthread_cond_signal(&td->free_cond);
883 return NULL;
884}
885
886int verify_async_init(struct thread_data *td)
887{
888 int i, ret;
bart Van Assche304a47c2010-08-01 21:31:26 +0200889 pthread_attr_t attr;
890
891 pthread_attr_init(&attr);
892 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
Jens Axboee8462bd2009-07-06 12:59:04 +0200893
894 td->verify_thread_exit = 0;
895
896 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
897 for (i = 0; i < td->o.verify_async; i++) {
bart Van Assche304a47c2010-08-01 21:31:26 +0200898 ret = pthread_create(&td->verify_threads[i], &attr,
Jens Axboee8462bd2009-07-06 12:59:04 +0200899 verify_async_thread, td);
900 if (ret) {
901 log_err("fio: async verify creation failed: %s\n",
902 strerror(ret));
903 break;
904 }
905 ret = pthread_detach(td->verify_threads[i]);
906 if (ret) {
907 log_err("fio: async verify thread detach failed: %s\n",
908 strerror(ret));
909 break;
910 }
911 td->nr_verify_threads++;
912 }
913
bart Van Assche304a47c2010-08-01 21:31:26 +0200914 pthread_attr_destroy(&attr);
915
Jens Axboee8462bd2009-07-06 12:59:04 +0200916 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +0200917 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +0200918 td->verify_thread_exit = 1;
919 write_barrier();
920 pthread_cond_broadcast(&td->verify_cond);
921 return 1;
922 }
923
924 return 0;
925}
926
927void verify_async_exit(struct thread_data *td)
928{
929 td->verify_thread_exit = 1;
930 write_barrier();
931 pthread_cond_broadcast(&td->verify_cond);
932
933 pthread_mutex_lock(&td->io_u_lock);
934
935 while (td->nr_verify_threads)
936 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
937
938 pthread_mutex_unlock(&td->io_u_lock);
939 free(td->verify_threads);
940 td->verify_threads = NULL;
941}