blob: 6b54b70ff672ffd3c75f7e0b3b72d35592c1e91c [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 Axboee29d1b72006-10-18 15:43:15 +020013
Jens Axboeeef6eea2007-07-30 12:27:58 +020014#include "crc/md5.h"
15#include "crc/crc64.h"
16#include "crc/crc32.h"
Jens Axboebac39e02008-06-11 20:46:19 +020017#include "crc/crc32c.h"
Jens Axboeeef6eea2007-07-30 12:27:58 +020018#include "crc/crc16.h"
19#include "crc/crc7.h"
20#include "crc/sha256.h"
21#include "crc/sha512.h"
Jens Axboe7c353ce2009-08-09 22:40:33 +020022#include "crc/sha1.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020023
Jens Axboe90059d62007-07-30 09:33:12 +020024static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020025{
26 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020027 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020028
29 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020030 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020031
32 /*
33 * lrand48_r seems to be broken and only fill the bottom
34 * 32-bits, even on 64-bit archs with 64-bit longs
35 */
36 todo = sizeof(r);
37 if (todo > len)
38 todo = len;
39
40 memcpy(p, &r, todo);
41
42 len -= todo;
43 p += todo;
44 }
45}
46
Jens Axboe90059d62007-07-30 09:33:12 +020047static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
48{
49 switch (td->o.verify_pattern_bytes) {
50 case 0:
Jens Axboebd6f78b2008-02-01 20:27:52 +010051 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020052 fill_random_bytes(td, p, len);
53 break;
54 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010055 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Radha Ramachandran0e92f872009-10-27 20:14:27 +010056 memset(p, td->o.verify_pattern[0], len);
Jens Axboe90059d62007-07-30 09:33:12 +020057 break;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010058 default: {
59 unsigned int i = 0, size = 0;
Jens Axboe90059d62007-07-30 09:33:12 +020060 unsigned char *b = p;
61
Jens Axboebd6f78b2008-02-01 20:27:52 +010062 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
63 td->o.verify_pattern_bytes, len);
64
Jens Axboe90059d62007-07-30 09:33:12 +020065 while (i < len) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +010066 size = td->o.verify_pattern_bytes;
67 if (size > (len - i))
68 size = len - i;
69 memcpy(b+i, td->o.verify_pattern, size);
70 i += size;
Jens Axboe90059d62007-07-30 09:33:12 +020071 }
72 break;
73 }
74 }
75}
76
Jens Axboe4764aec2007-08-23 09:03:38 +020077static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020078{
Shawn Lewisdee6de72007-08-02 22:17:52 +020079 char swap[200];
80
81 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020082
Shawn Lewis546a9142007-07-28 21:11:37 +020083 memcpy(&swap, buf1, len);
84 memcpy(buf1, buf2, len);
85 memcpy(buf2, &swap, len);
86}
87
Jens Axboee29d1b72006-10-18 15:43:15 +020088static void hexdump(void *buffer, int len)
89{
90 unsigned char *p = buffer;
91 int i;
92
93 for (i = 0; i < len; i++)
Jens Axboebfacf382010-06-18 14:29:52 +020094 log_err("%02x", p[i]);
95 log_err("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020096}
97
Jens Axboed9f2caf2007-07-28 21:22:03 +020098/*
Jens Axboe87677832007-07-30 12:08:14 +020099 * Prepare for seperation of verify_header and checksum header
100 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200101static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200102{
Jens Axboe5921e802008-05-30 15:02:38 +0200103 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +0200104
Jens Axboe546dfd92007-07-30 12:23:05 +0200105 switch (verify_type) {
106 case VERIFY_NONE:
107 case VERIFY_NULL:
108 len = 0;
109 break;
110 case VERIFY_MD5:
111 len = sizeof(struct vhdr_md5);
112 break;
113 case VERIFY_CRC64:
114 len = sizeof(struct vhdr_crc64);
115 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200116 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200117 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200118 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200119 len = sizeof(struct vhdr_crc32);
120 break;
121 case VERIFY_CRC16:
122 len = sizeof(struct vhdr_crc16);
123 break;
124 case VERIFY_CRC7:
125 len = sizeof(struct vhdr_crc7);
126 break;
127 case VERIFY_SHA256:
128 len = sizeof(struct vhdr_sha256);
129 break;
130 case VERIFY_SHA512:
131 len = sizeof(struct vhdr_sha512);
132 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200133 case VERIFY_META:
134 len = sizeof(struct vhdr_meta);
135 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200136 case VERIFY_SHA1:
137 len = sizeof(struct vhdr_sha1);
138 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200139 default:
140 log_err("fio: unknown verify header!\n");
141 assert(0);
142 }
143
144 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200145}
146
147static inline unsigned int hdr_size(struct verify_header *hdr)
148{
Jens Axboe546dfd92007-07-30 12:23:05 +0200149 return __hdr_size(hdr->verify_type);
150}
151
152static void *hdr_priv(struct verify_header *hdr)
153{
154 void *priv = hdr;
155
156 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200157}
158
159/*
Jens Axboe936216f2010-06-18 13:44:11 +0200160 * Verify container, pass info to verify handlers and allow them to
161 * pass info back in case of error
162 */
163struct vcont {
164 /*
165 * Input
166 */
167 struct io_u *io_u;
168 unsigned int hdr_num;
169
170 /*
171 * Output, only valid in case of error
172 */
Jens Axboebfacf382010-06-18 14:29:52 +0200173 const char *name;
174 void *good_crc;
175 void *bad_crc;
Jens Axboe936216f2010-06-18 13:44:11 +0200176 unsigned int crc_len;
177};
178
Jens Axboebfacf382010-06-18 14:29:52 +0200179static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
180{
181 unsigned long long offset;
182
183 offset = vc->io_u->offset;
184 offset += vc->hdr_num * hdr->len;
Jens Axboe32c17ad2010-06-18 14:32:21 +0200185 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
186 vc->name, vc->io_u->file->file_name, offset, hdr->len);
Jens Axboebfacf382010-06-18 14:29:52 +0200187
188 if (vc->good_crc && vc->bad_crc) {
189 log_err(" Expected CRC: ");
190 hexdump(vc->good_crc, vc->crc_len);
191 log_err(" Received CRC: ");
192 hexdump(vc->bad_crc, vc->crc_len);
193 }
194}
195
Jens Axboe936216f2010-06-18 13:44:11 +0200196/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200197 * Return data area 'header_num'
198 */
Jens Axboe936216f2010-06-18 13:44:11 +0200199static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200200{
Jens Axboe936216f2010-06-18 13:44:11 +0200201 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200202}
203
Shawn Lewis7437ee82007-08-02 21:05:58 +0200204static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe936216f2010-06-18 13:44:11 +0200205 struct vcont *vc)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200206{
207 struct vhdr_meta *vh = hdr_priv(hdr);
Jens Axboe936216f2010-06-18 13:44:11 +0200208 struct io_u *io_u = vc->io_u;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200209
Jens Axboebd6f78b2008-02-01 20:27:52 +0100210 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
211
Jens Axboebfacf382010-06-18 14:29:52 +0200212 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
213 return 0;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200214
Jens Axboebfacf382010-06-18 14:29:52 +0200215 vc->name = "meta";
216 log_verify_failure(hdr, vc);
217 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200218}
219
Jens Axboe936216f2010-06-18 13:44:11 +0200220static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200221{
Jens Axboe936216f2010-06-18 13:44:11 +0200222 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200223 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200224 uint8_t sha512[128];
225 struct sha512_ctx sha512_ctx = {
226 .buf = sha512,
227 };
228
Jens Axboe936216f2010-06-18 13:44:11 +0200229 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100230
Jens Axboecd14cc12007-07-30 10:59:33 +0200231 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200232 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200233
Jens Axboebfacf382010-06-18 14:29:52 +0200234 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
235 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200236
Jens Axboebfacf382010-06-18 14:29:52 +0200237 vc->name = "sha512";
238 vc->good_crc = vh->sha512;
239 vc->bad_crc = sha512_ctx.buf;
240 vc->crc_len = sizeof(vh->sha512);
241 log_verify_failure(hdr, vc);
242 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200243}
244
Jens Axboe936216f2010-06-18 13:44:11 +0200245static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200246{
Jens Axboe936216f2010-06-18 13:44:11 +0200247 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200248 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100249 uint8_t sha256[64];
Jens Axboecd14cc12007-07-30 10:59:33 +0200250 struct sha256_ctx sha256_ctx = {
251 .buf = sha256,
252 };
253
Jens Axboe936216f2010-06-18 13:44:11 +0200254 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100255
Jens Axboecd14cc12007-07-30 10:59:33 +0200256 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200257 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200258
Jens Axboebfacf382010-06-18 14:29:52 +0200259 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
260 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200261
Jens Axboebfacf382010-06-18 14:29:52 +0200262 vc->name = "sha256";
263 vc->good_crc = vh->sha256;
264 vc->bad_crc = sha256_ctx.buf;
265 vc->crc_len = sizeof(vh->sha256);
266 log_verify_failure(hdr, vc);
267 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200268}
269
Jens Axboe936216f2010-06-18 13:44:11 +0200270static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200271{
Jens Axboe936216f2010-06-18 13:44:11 +0200272 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200273 struct vhdr_sha1 *vh = hdr_priv(hdr);
274 uint32_t sha1[5];
275 struct sha1_ctx sha1_ctx = {
276 .H = sha1,
277 };
278
Jens Axboe936216f2010-06-18 13:44:11 +0200279 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200280
281 sha1_init(&sha1_ctx);
282 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
283
Jens Axboebfacf382010-06-18 14:29:52 +0200284 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
285 return 0;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200286
Jens Axboebfacf382010-06-18 14:29:52 +0200287 vc->name = "sha1";
288 vc->good_crc = vh->sha1;
289 vc->bad_crc = sha1_ctx.H;
290 vc->crc_len = sizeof(vh->sha1);
291 log_verify_failure(hdr, vc);
292 return EILSEQ;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200293}
294
Jens Axboe936216f2010-06-18 13:44:11 +0200295static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200296{
Jens Axboe936216f2010-06-18 13:44:11 +0200297 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200298 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200299 unsigned char c;
300
Jens Axboe936216f2010-06-18 13:44:11 +0200301 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100302
Jens Axboe87677832007-07-30 12:08:14 +0200303 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200304
Jens Axboebfacf382010-06-18 14:29:52 +0200305 if (c == vh->crc7)
306 return 0;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200307
Jens Axboebfacf382010-06-18 14:29:52 +0200308 vc->name = "crc7";
309 vc->good_crc = &vh->crc7;
310 vc->bad_crc = &c;
311 vc->crc_len = 1;
312 log_verify_failure(hdr, vc);
313 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200314}
315
Jens Axboe936216f2010-06-18 13:44:11 +0200316static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200317{
Jens Axboe936216f2010-06-18 13:44:11 +0200318 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200319 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200320 unsigned short c;
321
Jens Axboe936216f2010-06-18 13:44:11 +0200322 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100323
Jens Axboe87677832007-07-30 12:08:14 +0200324 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200325
Jens Axboebfacf382010-06-18 14:29:52 +0200326 if (c == vh->crc16)
327 return 0;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200328
Jens Axboebfacf382010-06-18 14:29:52 +0200329 vc->name = "crc16";
330 vc->good_crc = &vh->crc16;
331 vc->bad_crc = &c;
332 vc->crc_len = 2;
333 log_verify_failure(hdr, vc);
334 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200335}
336
Jens Axboe936216f2010-06-18 13:44:11 +0200337static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200338{
Jens Axboe936216f2010-06-18 13:44:11 +0200339 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200340 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200341 unsigned long long c;
342
Jens Axboe936216f2010-06-18 13:44:11 +0200343 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100344
Jens Axboe87677832007-07-30 12:08:14 +0200345 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200346
Jens Axboebfacf382010-06-18 14:29:52 +0200347 if (c == vh->crc64)
348 return 0;
Jens Axboed77a7af2007-07-27 15:35:06 +0200349
Jens Axboebfacf382010-06-18 14:29:52 +0200350 vc->name = "crc64";
351 vc->good_crc = &vh->crc64;
352 vc->bad_crc = &c;
353 vc->crc_len = 8;
354 log_verify_failure(hdr, vc);
355 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200356}
357
Jens Axboe936216f2010-06-18 13:44:11 +0200358static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200359{
Jens Axboe936216f2010-06-18 13:44:11 +0200360 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200361 struct vhdr_crc32 *vh = hdr_priv(hdr);
362 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200363
Jens Axboe936216f2010-06-18 13:44:11 +0200364 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100365
Jens Axboe87677832007-07-30 12:08:14 +0200366 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200367
Jens Axboebfacf382010-06-18 14:29:52 +0200368 if (c == vh->crc32)
369 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200370
Jens Axboebfacf382010-06-18 14:29:52 +0200371 vc->name = "crc32";
372 vc->good_crc = &vh->crc32;
373 vc->bad_crc = &c;
374 vc->crc_len = 4;
375 log_verify_failure(hdr, vc);
376 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200377}
378
Jens Axboe936216f2010-06-18 13:44:11 +0200379static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200380{
Jens Axboe936216f2010-06-18 13:44:11 +0200381 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200382 struct vhdr_crc32 *vh = hdr_priv(hdr);
383 uint32_t c;
384
Jens Axboe936216f2010-06-18 13:44:11 +0200385 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200386
Jens Axboe38455912008-08-04 15:35:26 +0200387 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
388 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
389 else
390 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200391
Jens Axboebfacf382010-06-18 14:29:52 +0200392 if (c == vh->crc32)
393 return 0;
Jens Axboebac39e02008-06-11 20:46:19 +0200394
Jens Axboebfacf382010-06-18 14:29:52 +0200395 vc->name = "crc32c";
396 vc->good_crc = &vh->crc32;
397 vc->bad_crc = &c;
398 vc->crc_len = 4;
399 log_verify_failure(hdr, vc);
400 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200401}
402
Jens Axboe936216f2010-06-18 13:44:11 +0200403static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200404{
Jens Axboe936216f2010-06-18 13:44:11 +0200405 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200406 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200407 uint32_t hash[MD5_HASH_WORDS];
408 struct md5_ctx md5_ctx = {
409 .hash = hash,
410 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200411
Jens Axboe936216f2010-06-18 13:44:11 +0200412 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100413
Jens Axboe61f821f2007-07-30 10:18:06 +0200414 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200415 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200416
Jens Axboebfacf382010-06-18 14:29:52 +0200417 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
418 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200419
Jens Axboebfacf382010-06-18 14:29:52 +0200420 vc->name = "md5";
421 vc->good_crc = vh->md5_digest;
422 vc->bad_crc = md5_ctx.hash;
423 vc->crc_len = sizeof(hash);
424 log_verify_failure(hdr, vc);
425 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200426}
427
Jens Axboe3f199b02007-08-09 10:16:31 +0200428static unsigned int hweight8(unsigned int w)
429{
430 unsigned int res = w - ((w >> 1) & 0x55);
431
432 res = (res & 0x33) + ((res >> 2) & 0x33);
433 return (res + (res >> 4)) & 0x0F;
434}
435
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100436int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100437 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200438{
439 unsigned int i;
Shawn Lewisa944e332007-08-02 22:18:29 +0200440
441 for (i = 0; i < len; i++) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100442 if (buf[i] != pattern[mod]) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200443 unsigned int bits;
444
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100445 bits = hweight8(buf[i] ^ pattern[mod]);
Jens Axboe3f199b02007-08-09 10:16:31 +0200446 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100447 buf[i], pattern[mod], bits);
Jens Axboe3f199b02007-08-09 10:16:31 +0200448 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200449 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200450 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200451 mod++;
452 if (mod == pattern_size)
453 mod = 0;
454 }
455
456 return 0;
457}
458
Jens Axboee8462bd2009-07-06 12:59:04 +0200459/*
460 * Push IO verification to a separate thread
461 */
462int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
463{
464 if (io_u->file)
465 put_file_log(td, io_u->file);
466
467 io_u->file = NULL;
468
469 pthread_mutex_lock(&td->io_u_lock);
Radha Ramachandran0c412142009-11-03 21:45:31 +0100470
471 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
472 td->cur_depth--;
473 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
474 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200475 flist_del(&io_u->list);
476 flist_add_tail(&io_u->list, &td->verify_list);
Jens Axboe2ecc1b52009-11-04 20:58:09 +0100477 io_u->flags |= IO_U_F_FREE_DEF;
Jens Axboee8462bd2009-07-06 12:59:04 +0200478 pthread_mutex_unlock(&td->io_u_lock);
479
480 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200481 return 0;
482}
483
Jens Axboe36690c92007-03-26 10:23:34 +0200484int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200485{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200486 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200487 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200488 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200489 int ret;
490
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200491 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200492 return 0;
493
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200494 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200495 if (td->o.verify_interval)
496 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200497
Jens Axboea12a3b42007-08-09 10:20:54 +0200498 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100499 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
500 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200501 struct vcont vc = {
502 .io_u = io_u,
503 .hdr_num = hdr_num,
504 };
Jens Axboe936216f2010-06-18 13:44:11 +0200505
Jens Axboef3e6cb92010-06-18 14:48:43 +0200506 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200507 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200508
Shawn Lewisa944e332007-08-02 22:18:29 +0200509 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200510 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200511 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200512 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200513
Jens Axboe3f199b02007-08-09 10:16:31 +0200514 if (hdr->fio_magic != FIO_HDR_MAGIC) {
Jens Axboec9b44032010-06-18 14:46:06 +0200515 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
516 hdr->fio_magic, FIO_HDR_MAGIC,
517 io_u->file->file_name,
518 io_u->offset + hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200519 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200520 }
521
Shawn Lewise28218f2008-01-16 11:01:33 +0100522 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100523 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
524 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100525 ret = verify_io_u_pattern(td->o.verify_pattern,
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100526 td->o.verify_pattern_bytes,
527 p + hdr_size,
528 hdr_inc - hdr_size,
529 hdr_size % td->o.verify_pattern_bytes);
Jens Axboec9b44032010-06-18 14:46:06 +0200530
531 if (ret) {
532 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
533 io_u->file->file_name,
534 io_u->offset + hdr_num * hdr->len,
535 hdr->len);
536 }
537
Radha Ramachandrane92d3d72009-07-27 12:27:55 +0200538 /*
539 * Also verify the meta data, if applicable
540 */
541 if (hdr->verify_type == VERIFY_META)
Jens Axboe936216f2010-06-18 13:44:11 +0200542 ret |= verify_io_u_meta(hdr, td, &vc);
Shawn Lewise28218f2008-01-16 11:01:33 +0100543 continue;
544 }
545
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200546 switch (hdr->verify_type) {
547 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200548 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200549 break;
550 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200551 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200552 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200553 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200554 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200555 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200556 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200557 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200558 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200559 break;
560 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200561 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200562 break;
563 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200564 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200565 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200566 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200567 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200568 break;
569 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200570 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200571 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200572 case VERIFY_META:
Jens Axboe936216f2010-06-18 13:44:11 +0200573 ret = verify_io_u_meta(hdr, td, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200574 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200575 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200576 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200577 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200578 default:
579 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200580 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200581 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200582 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100583
Jens Axboef3e6cb92010-06-18 14:48:43 +0200584 if (ret && td->o.verify_fatal)
585 td->terminate = 1;
586
Jens Axboea12a3b42007-08-09 10:20:54 +0200587 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200588}
589
Shawn Lewis7437ee82007-08-02 21:05:58 +0200590static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100591 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200592{
593 struct vhdr_meta *vh = hdr_priv(hdr);
594
595 vh->thread = td->thread_number;
596
597 vh->time_sec = io_u->start_time.tv_sec;
598 vh->time_usec = io_u->start_time.tv_usec;
599
600 vh->numberio = td->io_issues[DDIR_WRITE];
601
602 vh->offset = io_u->offset + header_num * td->o.verify_interval;
603}
604
Jens Axboecd14cc12007-07-30 10:59:33 +0200605static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
606{
Jens Axboe546dfd92007-07-30 12:23:05 +0200607 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200608 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200609 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200610 };
611
612 sha512_init(&sha512_ctx);
613 sha512_update(&sha512_ctx, p, len);
614}
615
616static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
617{
Jens Axboe546dfd92007-07-30 12:23:05 +0200618 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200619 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200620 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200621 };
622
623 sha256_init(&sha256_ctx);
624 sha256_update(&sha256_ctx, p, len);
625}
626
Jens Axboe7c353ce2009-08-09 22:40:33 +0200627static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
628{
629 struct vhdr_sha1 *vh = hdr_priv(hdr);
630 struct sha1_ctx sha1_ctx = {
631 .H = vh->sha1,
632 };
633
634 sha1_init(&sha1_ctx);
635 sha1_update(&sha1_ctx, p, len);
636}
637
Jens Axboe1e154bd2007-07-27 09:52:40 +0200638static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
639{
Jens Axboe546dfd92007-07-30 12:23:05 +0200640 struct vhdr_crc7 *vh = hdr_priv(hdr);
641
642 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200643}
644
Jens Axboe969f7ed2007-07-27 09:07:17 +0200645static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
646{
Jens Axboe546dfd92007-07-30 12:23:05 +0200647 struct vhdr_crc16 *vh = hdr_priv(hdr);
648
649 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200650}
651
Jens Axboee29d1b72006-10-18 15:43:15 +0200652static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
653{
Jens Axboe546dfd92007-07-30 12:23:05 +0200654 struct vhdr_crc32 *vh = hdr_priv(hdr);
655
656 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200657}
658
Jens Axboebac39e02008-06-11 20:46:19 +0200659static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
660{
661 struct vhdr_crc32 *vh = hdr_priv(hdr);
662
Jens Axboe38455912008-08-04 15:35:26 +0200663 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
664 vh->crc32 = crc32c_intel(p, len);
665 else
666 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200667}
668
Jens Axboed77a7af2007-07-27 15:35:06 +0200669static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
670{
Jens Axboe546dfd92007-07-30 12:23:05 +0200671 struct vhdr_crc64 *vh = hdr_priv(hdr);
672
673 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200674}
675
Jens Axboee29d1b72006-10-18 15:43:15 +0200676static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
677{
Jens Axboe546dfd92007-07-30 12:23:05 +0200678 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200679 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200680 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200681 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200682
Jens Axboe61f821f2007-07-30 10:18:06 +0200683 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200684 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200685}
686
687/*
688 * fill body of io_u->buf with random data and add a header with the
689 * crc32 or md5 sum of that data.
690 */
691void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
692{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200693 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200694 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200695 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200696
Jens Axboe9cc3d152007-03-26 10:32:30 +0200697 if (td->o.verify == VERIFY_NULL)
698 return;
699
Jens Axboe90059d62007-07-30 09:33:12 +0200700 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200701
Shawn Lewis546a9142007-07-28 21:11:37 +0200702 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200703 if (td->o.verify_interval)
704 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200705
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100706 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200707 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200708
709 hdr->fio_magic = FIO_HDR_MAGIC;
710 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200711 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200712 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200713
Jens Axboe87677832007-07-30 12:08:14 +0200714 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200715 switch (td->o.verify) {
716 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100717 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
718 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200719 fill_md5(hdr, data, data_len);
720 break;
721 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100722 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
723 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200724 fill_crc64(hdr, data, data_len);
725 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200726 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200727 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200728 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
729 io_u, hdr->len);
730 fill_crc32c(hdr, data, data_len);
731 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200732 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100733 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
734 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200735 fill_crc32(hdr, data, data_len);
736 break;
737 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100738 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
739 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200740 fill_crc16(hdr, data, data_len);
741 break;
742 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100743 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
744 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200745 fill_crc7(hdr, data, data_len);
746 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200747 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100748 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
749 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200750 fill_sha256(hdr, data, data_len);
751 break;
752 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100753 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
754 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200755 fill_sha512(hdr, data, data_len);
756 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200757 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100758 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
759 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200760 fill_meta(hdr, td, io_u, header_num);
761 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200762 case VERIFY_SHA1:
763 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
764 io_u, hdr->len);
765 fill_sha1(hdr, data, data_len);
766 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200767 default:
768 log_err("fio: bad verify type: %d\n", td->o.verify);
769 assert(0);
770 }
Jens Axboea59e1702007-07-30 08:53:27 +0200771 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200772 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200773 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200774 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200775}
776
777int get_next_verify(struct thread_data *td, struct io_u *io_u)
778{
Jens Axboe8de8f042007-03-27 10:36:12 +0200779 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200780
Jens Axboed2d7fa52007-02-19 13:21:35 +0100781 /*
782 * this io_u is from a requeue, we already filled the offsets
783 */
784 if (io_u->file)
785 return 0;
786
Jens Axboe8de8f042007-03-27 10:36:12 +0200787 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
788 struct rb_node *n = rb_first(&td->io_hist_tree);
789
Jens Axboe4b878982007-03-26 09:32:22 +0200790 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200791 rb_erase(n, &td->io_hist_tree);
Jens Axboe9e144182010-06-15 14:25:36 +0200792 td->io_hist_len--;
Jens Axboe01743ee2008-06-02 12:19:19 +0200793 } else if (!flist_empty(&td->io_hist_list)) {
794 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboe9e144182010-06-15 14:25:36 +0200795 td->io_hist_len--;
Jens Axboe01743ee2008-06-02 12:19:19 +0200796 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200797 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200798
Jens Axboe8de8f042007-03-27 10:36:12 +0200799 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200800 io_u->offset = ipo->offset;
801 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100802 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200803
Jens Axboed6aed792009-06-03 08:41:15 +0200804 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200805 int r = td_io_open_file(td, io_u->file);
806
Jens Axboebd6f78b2008-02-01 20:27:52 +0100807 if (r) {
808 dprint(FD_VERIFY, "failed file %s open\n",
809 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200810 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100811 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200812 }
813
814 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200815 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200816 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100817 io_u->xfer_buf = io_u->buf;
818 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200819 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100820 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200821 return 0;
822 }
823
Jens Axboebd6f78b2008-02-01 20:27:52 +0100824 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200825 return 1;
826}
Jens Axboee8462bd2009-07-06 12:59:04 +0200827
828static void *verify_async_thread(void *data)
829{
830 struct thread_data *td = data;
831 struct io_u *io_u;
832 int ret = 0;
833
834 if (td->o.verify_cpumask_set &&
835 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
836 log_err("fio: failed setting verify thread affinity\n");
837 goto done;
838 }
839
840 do {
Jens Axboee53ab272009-07-06 13:43:46 +0200841 FLIST_HEAD(list);
842
Jens Axboee8462bd2009-07-06 12:59:04 +0200843 read_barrier();
844 if (td->verify_thread_exit)
845 break;
846
847 pthread_mutex_lock(&td->io_u_lock);
848
849 while (flist_empty(&td->verify_list) &&
850 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +0200851 ret = pthread_cond_wait(&td->verify_cond,
852 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +0200853 if (ret) {
854 pthread_mutex_unlock(&td->io_u_lock);
855 break;
856 }
857 }
858
Jens Axboee53ab272009-07-06 13:43:46 +0200859 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +0200860 pthread_mutex_unlock(&td->io_u_lock);
861
Jens Axboee53ab272009-07-06 13:43:46 +0200862 if (flist_empty(&list))
863 continue;
864
865 while (!flist_empty(&list)) {
866 io_u = flist_entry(list.next, struct io_u, list);
867 flist_del_init(&io_u->list);
868
Jens Axboed561f2a2009-07-06 13:51:05 +0200869 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +0200870 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +0200871 if (!ret)
872 continue;
873 if (td->o.continue_on_error &&
874 td_non_fatal_error(ret)) {
875 update_error_count(td, ret);
876 td_clear_error(td);
877 ret = 0;
878 }
Jens Axboee53ab272009-07-06 13:43:46 +0200879 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200880 } while (!ret);
881
Jens Axboed561f2a2009-07-06 13:51:05 +0200882 if (ret) {
883 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +0200884 if (td->o.verify_fatal)
885 td->terminate = 1;
Jens Axboed561f2a2009-07-06 13:51:05 +0200886 }
887
Jens Axboee8462bd2009-07-06 12:59:04 +0200888done:
889 pthread_mutex_lock(&td->io_u_lock);
890 td->nr_verify_threads--;
891 pthread_mutex_unlock(&td->io_u_lock);
892
893 pthread_cond_signal(&td->free_cond);
894 return NULL;
895}
896
897int verify_async_init(struct thread_data *td)
898{
899 int i, ret;
900
901 td->verify_thread_exit = 0;
902
903 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
904 for (i = 0; i < td->o.verify_async; i++) {
905 ret = pthread_create(&td->verify_threads[i], NULL,
906 verify_async_thread, td);
907 if (ret) {
908 log_err("fio: async verify creation failed: %s\n",
909 strerror(ret));
910 break;
911 }
912 ret = pthread_detach(td->verify_threads[i]);
913 if (ret) {
914 log_err("fio: async verify thread detach failed: %s\n",
915 strerror(ret));
916 break;
917 }
918 td->nr_verify_threads++;
919 }
920
921 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +0200922 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +0200923 td->verify_thread_exit = 1;
924 write_barrier();
925 pthread_cond_broadcast(&td->verify_cond);
926 return 1;
927 }
928
929 return 0;
930}
931
932void verify_async_exit(struct thread_data *td)
933{
934 td->verify_thread_exit = 1;
935 write_barrier();
936 pthread_cond_broadcast(&td->verify_cond);
937
938 pthread_mutex_lock(&td->io_u_lock);
939
940 while (td->nr_verify_threads)
941 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
942
943 pthread_mutex_unlock(&td->io_u_lock);
944 free(td->verify_threads);
945 td->verify_threads = NULL;
946}