blob: 265bd5563210bcb1db2cadd69c127e8f4c79869f [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
Jens Axboe90059d62007-07-30 09:33:12 +020025static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
26{
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:
Jens Axboebd6f78b2008-02-01 20:27:52 +010033 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Radha Ramachandran0e92f872009-10-27 20:14:27 +010034 memset(p, td->o.verify_pattern[0], len);
Jens Axboe90059d62007-07-30 09:33:12 +020035 break;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010036 default: {
37 unsigned int i = 0, size = 0;
Jens Axboe90059d62007-07-30 09:33:12 +020038 unsigned char *b = p;
39
Jens Axboebd6f78b2008-02-01 20:27:52 +010040 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
41 td->o.verify_pattern_bytes, len);
42
Jens Axboe90059d62007-07-30 09:33:12 +020043 while (i < len) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +010044 size = td->o.verify_pattern_bytes;
45 if (size > (len - i))
46 size = len - i;
47 memcpy(b+i, td->o.verify_pattern, size);
48 i += size;
Jens Axboe90059d62007-07-30 09:33:12 +020049 }
50 break;
51 }
52 }
53}
54
Jens Axboe4764aec2007-08-23 09:03:38 +020055static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020056{
Shawn Lewisdee6de72007-08-02 22:17:52 +020057 char swap[200];
58
59 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020060
Shawn Lewis546a9142007-07-28 21:11:37 +020061 memcpy(&swap, buf1, len);
62 memcpy(buf1, buf2, len);
63 memcpy(buf2, &swap, len);
64}
65
Jens Axboee29d1b72006-10-18 15:43:15 +020066static void hexdump(void *buffer, int len)
67{
68 unsigned char *p = buffer;
69 int i;
70
71 for (i = 0; i < len; i++)
Jens Axboebfacf382010-06-18 14:29:52 +020072 log_err("%02x", p[i]);
73 log_err("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020074}
75
Jens Axboed9f2caf2007-07-28 21:22:03 +020076/*
Jens Axboe87677832007-07-30 12:08:14 +020077 * Prepare for seperation of verify_header and checksum header
78 */
Jens Axboe546dfd92007-07-30 12:23:05 +020079static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +020080{
Jens Axboe5921e802008-05-30 15:02:38 +020081 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +020082
Jens Axboe546dfd92007-07-30 12:23:05 +020083 switch (verify_type) {
84 case VERIFY_NONE:
85 case VERIFY_NULL:
86 len = 0;
87 break;
88 case VERIFY_MD5:
89 len = sizeof(struct vhdr_md5);
90 break;
91 case VERIFY_CRC64:
92 len = sizeof(struct vhdr_crc64);
93 break;
Jens Axboebac39e02008-06-11 20:46:19 +020094 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +020095 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +020096 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +020097 len = sizeof(struct vhdr_crc32);
98 break;
99 case VERIFY_CRC16:
100 len = sizeof(struct vhdr_crc16);
101 break;
102 case VERIFY_CRC7:
103 len = sizeof(struct vhdr_crc7);
104 break;
105 case VERIFY_SHA256:
106 len = sizeof(struct vhdr_sha256);
107 break;
108 case VERIFY_SHA512:
109 len = sizeof(struct vhdr_sha512);
110 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200111 case VERIFY_META:
112 len = sizeof(struct vhdr_meta);
113 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200114 case VERIFY_SHA1:
115 len = sizeof(struct vhdr_sha1);
116 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200117 default:
118 log_err("fio: unknown verify header!\n");
119 assert(0);
120 }
121
122 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200123}
124
125static inline unsigned int hdr_size(struct verify_header *hdr)
126{
Jens Axboe546dfd92007-07-30 12:23:05 +0200127 return __hdr_size(hdr->verify_type);
128}
129
130static void *hdr_priv(struct verify_header *hdr)
131{
132 void *priv = hdr;
133
134 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200135}
136
137/*
Jens Axboe936216f2010-06-18 13:44:11 +0200138 * Verify container, pass info to verify handlers and allow them to
139 * pass info back in case of error
140 */
141struct vcont {
142 /*
143 * Input
144 */
145 struct io_u *io_u;
146 unsigned int hdr_num;
147
148 /*
149 * Output, only valid in case of error
150 */
Jens Axboebfacf382010-06-18 14:29:52 +0200151 const char *name;
152 void *good_crc;
153 void *bad_crc;
Jens Axboe936216f2010-06-18 13:44:11 +0200154 unsigned int crc_len;
155};
156
Jens Axboebfacf382010-06-18 14:29:52 +0200157static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
158{
159 unsigned long long offset;
160
161 offset = vc->io_u->offset;
162 offset += vc->hdr_num * hdr->len;
Jens Axboe32c17ad2010-06-18 14:32:21 +0200163 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
164 vc->name, vc->io_u->file->file_name, offset, hdr->len);
Jens Axboebfacf382010-06-18 14:29:52 +0200165
166 if (vc->good_crc && vc->bad_crc) {
167 log_err(" Expected CRC: ");
168 hexdump(vc->good_crc, vc->crc_len);
169 log_err(" Received CRC: ");
170 hexdump(vc->bad_crc, vc->crc_len);
171 }
172}
173
Jens Axboe936216f2010-06-18 13:44:11 +0200174/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200175 * Return data area 'header_num'
176 */
Jens Axboe936216f2010-06-18 13:44:11 +0200177static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200178{
Jens Axboe936216f2010-06-18 13:44:11 +0200179 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200180}
181
Shawn Lewis7437ee82007-08-02 21:05:58 +0200182static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe936216f2010-06-18 13:44:11 +0200183 struct vcont *vc)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200184{
185 struct vhdr_meta *vh = hdr_priv(hdr);
Jens Axboe936216f2010-06-18 13:44:11 +0200186 struct io_u *io_u = vc->io_u;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200187
Jens Axboebd6f78b2008-02-01 20:27:52 +0100188 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
189
Jens Axboebfacf382010-06-18 14:29:52 +0200190 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
191 return 0;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200192
Jens Axboebfacf382010-06-18 14:29:52 +0200193 vc->name = "meta";
194 log_verify_failure(hdr, vc);
195 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200196}
197
Jens Axboe936216f2010-06-18 13:44:11 +0200198static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200199{
Jens Axboe936216f2010-06-18 13:44:11 +0200200 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200201 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200202 uint8_t sha512[128];
203 struct sha512_ctx sha512_ctx = {
204 .buf = sha512,
205 };
206
Jens Axboe936216f2010-06-18 13:44:11 +0200207 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100208
Jens Axboecd14cc12007-07-30 10:59:33 +0200209 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200210 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200211
Jens Axboebfacf382010-06-18 14:29:52 +0200212 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
213 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200214
Jens Axboebfacf382010-06-18 14:29:52 +0200215 vc->name = "sha512";
216 vc->good_crc = vh->sha512;
217 vc->bad_crc = sha512_ctx.buf;
218 vc->crc_len = sizeof(vh->sha512);
219 log_verify_failure(hdr, vc);
220 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200221}
222
Jens Axboe936216f2010-06-18 13:44:11 +0200223static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200224{
Jens Axboe936216f2010-06-18 13:44:11 +0200225 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200226 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100227 uint8_t sha256[64];
Jens Axboecd14cc12007-07-30 10:59:33 +0200228 struct sha256_ctx sha256_ctx = {
229 .buf = sha256,
230 };
231
Jens Axboe936216f2010-06-18 13:44:11 +0200232 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100233
Jens Axboecd14cc12007-07-30 10:59:33 +0200234 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200235 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200236
Jens Axboebfacf382010-06-18 14:29:52 +0200237 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
238 return 0;
Jens Axboecd14cc12007-07-30 10:59:33 +0200239
Jens Axboebfacf382010-06-18 14:29:52 +0200240 vc->name = "sha256";
241 vc->good_crc = vh->sha256;
242 vc->bad_crc = sha256_ctx.buf;
243 vc->crc_len = sizeof(vh->sha256);
244 log_verify_failure(hdr, vc);
245 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200246}
247
Jens Axboe936216f2010-06-18 13:44:11 +0200248static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200249{
Jens Axboe936216f2010-06-18 13:44:11 +0200250 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200251 struct vhdr_sha1 *vh = hdr_priv(hdr);
252 uint32_t sha1[5];
253 struct sha1_ctx sha1_ctx = {
254 .H = sha1,
255 };
256
Jens Axboe936216f2010-06-18 13:44:11 +0200257 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200258
259 sha1_init(&sha1_ctx);
260 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
261
Jens Axboebfacf382010-06-18 14:29:52 +0200262 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
263 return 0;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200264
Jens Axboebfacf382010-06-18 14:29:52 +0200265 vc->name = "sha1";
266 vc->good_crc = vh->sha1;
267 vc->bad_crc = sha1_ctx.H;
268 vc->crc_len = sizeof(vh->sha1);
269 log_verify_failure(hdr, vc);
270 return EILSEQ;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200271}
272
Jens Axboe936216f2010-06-18 13:44:11 +0200273static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200274{
Jens Axboe936216f2010-06-18 13:44:11 +0200275 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200276 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200277 unsigned char c;
278
Jens Axboe936216f2010-06-18 13:44:11 +0200279 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100280
Jens Axboe87677832007-07-30 12:08:14 +0200281 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200282
Jens Axboebfacf382010-06-18 14:29:52 +0200283 if (c == vh->crc7)
284 return 0;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200285
Jens Axboebfacf382010-06-18 14:29:52 +0200286 vc->name = "crc7";
287 vc->good_crc = &vh->crc7;
288 vc->bad_crc = &c;
289 vc->crc_len = 1;
290 log_verify_failure(hdr, vc);
291 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200292}
293
Jens Axboe936216f2010-06-18 13:44:11 +0200294static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200295{
Jens Axboe936216f2010-06-18 13:44:11 +0200296 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200297 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200298 unsigned short c;
299
Jens Axboe936216f2010-06-18 13:44:11 +0200300 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100301
Jens Axboe87677832007-07-30 12:08:14 +0200302 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200303
Jens Axboebfacf382010-06-18 14:29:52 +0200304 if (c == vh->crc16)
305 return 0;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200306
Jens Axboebfacf382010-06-18 14:29:52 +0200307 vc->name = "crc16";
308 vc->good_crc = &vh->crc16;
309 vc->bad_crc = &c;
310 vc->crc_len = 2;
311 log_verify_failure(hdr, vc);
312 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200313}
314
Jens Axboe936216f2010-06-18 13:44:11 +0200315static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200316{
Jens Axboe936216f2010-06-18 13:44:11 +0200317 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200318 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200319 unsigned long long c;
320
Jens Axboe936216f2010-06-18 13:44:11 +0200321 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100322
Jens Axboe87677832007-07-30 12:08:14 +0200323 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200324
Jens Axboebfacf382010-06-18 14:29:52 +0200325 if (c == vh->crc64)
326 return 0;
Jens Axboed77a7af2007-07-27 15:35:06 +0200327
Jens Axboebfacf382010-06-18 14:29:52 +0200328 vc->name = "crc64";
329 vc->good_crc = &vh->crc64;
330 vc->bad_crc = &c;
331 vc->crc_len = 8;
332 log_verify_failure(hdr, vc);
333 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200334}
335
Jens Axboe936216f2010-06-18 13:44:11 +0200336static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200337{
Jens Axboe936216f2010-06-18 13:44:11 +0200338 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200339 struct vhdr_crc32 *vh = hdr_priv(hdr);
340 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200341
Jens Axboe936216f2010-06-18 13:44:11 +0200342 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100343
Jens Axboe87677832007-07-30 12:08:14 +0200344 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200345
Jens Axboebfacf382010-06-18 14:29:52 +0200346 if (c == vh->crc32)
347 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200348
Jens Axboebfacf382010-06-18 14:29:52 +0200349 vc->name = "crc32";
350 vc->good_crc = &vh->crc32;
351 vc->bad_crc = &c;
352 vc->crc_len = 4;
353 log_verify_failure(hdr, vc);
354 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200355}
356
Jens Axboe936216f2010-06-18 13:44:11 +0200357static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200358{
Jens Axboe936216f2010-06-18 13:44:11 +0200359 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200360 struct vhdr_crc32 *vh = hdr_priv(hdr);
361 uint32_t c;
362
Jens Axboe936216f2010-06-18 13:44:11 +0200363 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200364
Jens Axboe38455912008-08-04 15:35:26 +0200365 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
366 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
367 else
368 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200369
Jens Axboebfacf382010-06-18 14:29:52 +0200370 if (c == vh->crc32)
371 return 0;
Jens Axboebac39e02008-06-11 20:46:19 +0200372
Jens Axboebfacf382010-06-18 14:29:52 +0200373 vc->name = "crc32c";
374 vc->good_crc = &vh->crc32;
375 vc->bad_crc = &c;
376 vc->crc_len = 4;
377 log_verify_failure(hdr, vc);
378 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200379}
380
Jens Axboe936216f2010-06-18 13:44:11 +0200381static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200382{
Jens Axboe936216f2010-06-18 13:44:11 +0200383 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200384 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200385 uint32_t hash[MD5_HASH_WORDS];
386 struct md5_ctx md5_ctx = {
387 .hash = hash,
388 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200389
Jens Axboe936216f2010-06-18 13:44:11 +0200390 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100391
Jens Axboe61f821f2007-07-30 10:18:06 +0200392 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200393 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200394
Jens Axboebfacf382010-06-18 14:29:52 +0200395 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
396 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200397
Jens Axboebfacf382010-06-18 14:29:52 +0200398 vc->name = "md5";
399 vc->good_crc = vh->md5_digest;
400 vc->bad_crc = md5_ctx.hash;
401 vc->crc_len = sizeof(hash);
402 log_verify_failure(hdr, vc);
403 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200404}
405
Jens Axboe3f199b02007-08-09 10:16:31 +0200406static unsigned int hweight8(unsigned int w)
407{
408 unsigned int res = w - ((w >> 1) & 0x55);
409
410 res = (res & 0x33) + ((res >> 2) & 0x33);
411 return (res + (res >> 4)) & 0x0F;
412}
413
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100414int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100415 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200416{
417 unsigned int i;
Shawn Lewisa944e332007-08-02 22:18:29 +0200418
419 for (i = 0; i < len; i++) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100420 if (buf[i] != pattern[mod]) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200421 unsigned int bits;
422
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100423 bits = hweight8(buf[i] ^ pattern[mod]);
Jens Axboe3f199b02007-08-09 10:16:31 +0200424 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100425 buf[i], pattern[mod], bits);
Jens Axboe3f199b02007-08-09 10:16:31 +0200426 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200427 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200428 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200429 mod++;
430 if (mod == pattern_size)
431 mod = 0;
432 }
433
434 return 0;
435}
436
Jens Axboee8462bd2009-07-06 12:59:04 +0200437/*
438 * Push IO verification to a separate thread
439 */
440int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
441{
442 if (io_u->file)
443 put_file_log(td, io_u->file);
444
445 io_u->file = NULL;
446
447 pthread_mutex_lock(&td->io_u_lock);
Radha Ramachandran0c412142009-11-03 21:45:31 +0100448
449 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
450 td->cur_depth--;
451 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
452 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200453 flist_del(&io_u->list);
454 flist_add_tail(&io_u->list, &td->verify_list);
Jens Axboe2ecc1b52009-11-04 20:58:09 +0100455 io_u->flags |= IO_U_F_FREE_DEF;
Jens Axboee8462bd2009-07-06 12:59:04 +0200456 pthread_mutex_unlock(&td->io_u_lock);
457
458 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200459 return 0;
460}
461
Jens Axboe36690c92007-03-26 10:23:34 +0200462int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200463{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200464 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200465 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200466 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200467 int ret;
468
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200469 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200470 return 0;
471
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200472 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200473 if (td->o.verify_interval)
474 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200475
Jens Axboea12a3b42007-08-09 10:20:54 +0200476 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100477 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
478 p += hdr_inc, hdr_num++) {
Jens Axboebfacf382010-06-18 14:29:52 +0200479 struct vcont vc = {
480 .io_u = io_u,
481 .hdr_num = hdr_num,
482 };
Jens Axboe936216f2010-06-18 13:44:11 +0200483
Jens Axboef3e6cb92010-06-18 14:48:43 +0200484 if (ret && td->o.verify_fatal)
Jens Axboea12a3b42007-08-09 10:20:54 +0200485 break;
Jens Axboef3e6cb92010-06-18 14:48:43 +0200486
Shawn Lewisa944e332007-08-02 22:18:29 +0200487 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200488 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200489 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200490 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200491
Jens Axboe3f199b02007-08-09 10:16:31 +0200492 if (hdr->fio_magic != FIO_HDR_MAGIC) {
Jens Axboec9b44032010-06-18 14:46:06 +0200493 log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
494 hdr->fio_magic, FIO_HDR_MAGIC,
495 io_u->file->file_name,
496 io_u->offset + hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200497 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200498 }
499
Shawn Lewise28218f2008-01-16 11:01:33 +0100500 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100501 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
502 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100503 ret = verify_io_u_pattern(td->o.verify_pattern,
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100504 td->o.verify_pattern_bytes,
505 p + hdr_size,
506 hdr_inc - hdr_size,
507 hdr_size % td->o.verify_pattern_bytes);
Jens Axboec9b44032010-06-18 14:46:06 +0200508
509 if (ret) {
510 log_err("pattern: verify failed at file %s offset %llu, length %u\n",
511 io_u->file->file_name,
512 io_u->offset + hdr_num * hdr->len,
513 hdr->len);
514 }
515
Radha Ramachandrane92d3d72009-07-27 12:27:55 +0200516 /*
517 * Also verify the meta data, if applicable
518 */
519 if (hdr->verify_type == VERIFY_META)
Jens Axboe936216f2010-06-18 13:44:11 +0200520 ret |= verify_io_u_meta(hdr, td, &vc);
Shawn Lewise28218f2008-01-16 11:01:33 +0100521 continue;
522 }
523
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200524 switch (hdr->verify_type) {
525 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200526 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200527 break;
528 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200529 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200530 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200531 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200532 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200533 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200534 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200535 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200536 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200537 break;
538 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200539 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200540 break;
541 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200542 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200543 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200544 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200545 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200546 break;
547 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200548 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200549 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200550 case VERIFY_META:
Jens Axboe936216f2010-06-18 13:44:11 +0200551 ret = verify_io_u_meta(hdr, td, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200552 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200553 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200554 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200555 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200556 default:
557 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200558 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200559 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200560 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100561
Jens Axboef3e6cb92010-06-18 14:48:43 +0200562 if (ret && td->o.verify_fatal)
563 td->terminate = 1;
564
Jens Axboea12a3b42007-08-09 10:20:54 +0200565 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200566}
567
Shawn Lewis7437ee82007-08-02 21:05:58 +0200568static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100569 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200570{
571 struct vhdr_meta *vh = hdr_priv(hdr);
572
573 vh->thread = td->thread_number;
574
575 vh->time_sec = io_u->start_time.tv_sec;
576 vh->time_usec = io_u->start_time.tv_usec;
577
578 vh->numberio = td->io_issues[DDIR_WRITE];
579
580 vh->offset = io_u->offset + header_num * td->o.verify_interval;
581}
582
Jens Axboecd14cc12007-07-30 10:59:33 +0200583static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
584{
Jens Axboe546dfd92007-07-30 12:23:05 +0200585 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200586 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200587 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200588 };
589
590 sha512_init(&sha512_ctx);
591 sha512_update(&sha512_ctx, p, len);
592}
593
594static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
595{
Jens Axboe546dfd92007-07-30 12:23:05 +0200596 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200597 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200598 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200599 };
600
601 sha256_init(&sha256_ctx);
602 sha256_update(&sha256_ctx, p, len);
603}
604
Jens Axboe7c353ce2009-08-09 22:40:33 +0200605static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
606{
607 struct vhdr_sha1 *vh = hdr_priv(hdr);
608 struct sha1_ctx sha1_ctx = {
609 .H = vh->sha1,
610 };
611
612 sha1_init(&sha1_ctx);
613 sha1_update(&sha1_ctx, p, len);
614}
615
Jens Axboe1e154bd2007-07-27 09:52:40 +0200616static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
617{
Jens Axboe546dfd92007-07-30 12:23:05 +0200618 struct vhdr_crc7 *vh = hdr_priv(hdr);
619
620 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200621}
622
Jens Axboe969f7ed2007-07-27 09:07:17 +0200623static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
624{
Jens Axboe546dfd92007-07-30 12:23:05 +0200625 struct vhdr_crc16 *vh = hdr_priv(hdr);
626
627 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200628}
629
Jens Axboee29d1b72006-10-18 15:43:15 +0200630static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
631{
Jens Axboe546dfd92007-07-30 12:23:05 +0200632 struct vhdr_crc32 *vh = hdr_priv(hdr);
633
634 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200635}
636
Jens Axboebac39e02008-06-11 20:46:19 +0200637static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
638{
639 struct vhdr_crc32 *vh = hdr_priv(hdr);
640
Jens Axboe38455912008-08-04 15:35:26 +0200641 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
642 vh->crc32 = crc32c_intel(p, len);
643 else
644 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200645}
646
Jens Axboed77a7af2007-07-27 15:35:06 +0200647static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
648{
Jens Axboe546dfd92007-07-30 12:23:05 +0200649 struct vhdr_crc64 *vh = hdr_priv(hdr);
650
651 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200652}
653
Jens Axboee29d1b72006-10-18 15:43:15 +0200654static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
655{
Jens Axboe546dfd92007-07-30 12:23:05 +0200656 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200657 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200658 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200659 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200660
Jens Axboe61f821f2007-07-30 10:18:06 +0200661 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200662 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200663}
664
665/*
666 * fill body of io_u->buf with random data and add a header with the
667 * crc32 or md5 sum of that data.
668 */
669void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
670{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200671 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200672 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200673 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200674
Jens Axboe9cc3d152007-03-26 10:32:30 +0200675 if (td->o.verify == VERIFY_NULL)
676 return;
677
Jens Axboe90059d62007-07-30 09:33:12 +0200678 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200679
Shawn Lewis546a9142007-07-28 21:11:37 +0200680 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200681 if (td->o.verify_interval)
682 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200683
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100684 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200685 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200686
687 hdr->fio_magic = FIO_HDR_MAGIC;
688 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200689 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200690 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200691
Jens Axboe87677832007-07-30 12:08:14 +0200692 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200693 switch (td->o.verify) {
694 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100695 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
696 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200697 fill_md5(hdr, data, data_len);
698 break;
699 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100700 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
701 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200702 fill_crc64(hdr, data, data_len);
703 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200704 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200705 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200706 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
707 io_u, hdr->len);
708 fill_crc32c(hdr, data, data_len);
709 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200710 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100711 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
712 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200713 fill_crc32(hdr, data, data_len);
714 break;
715 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100716 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
717 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200718 fill_crc16(hdr, data, data_len);
719 break;
720 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100721 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
722 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200723 fill_crc7(hdr, data, data_len);
724 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200725 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100726 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
727 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200728 fill_sha256(hdr, data, data_len);
729 break;
730 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100731 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
732 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200733 fill_sha512(hdr, data, data_len);
734 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200735 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100736 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
737 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200738 fill_meta(hdr, td, io_u, header_num);
739 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200740 case VERIFY_SHA1:
741 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
742 io_u, hdr->len);
743 fill_sha1(hdr, data, data_len);
744 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200745 default:
746 log_err("fio: bad verify type: %d\n", td->o.verify);
747 assert(0);
748 }
Jens Axboea59e1702007-07-30 08:53:27 +0200749 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200750 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200751 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200752 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200753}
754
755int get_next_verify(struct thread_data *td, struct io_u *io_u)
756{
Jens Axboe8de8f042007-03-27 10:36:12 +0200757 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200758
Jens Axboed2d7fa52007-02-19 13:21:35 +0100759 /*
760 * this io_u is from a requeue, we already filled the offsets
761 */
762 if (io_u->file)
763 return 0;
764
Jens Axboe8de8f042007-03-27 10:36:12 +0200765 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
766 struct rb_node *n = rb_first(&td->io_hist_tree);
767
Jens Axboe4b878982007-03-26 09:32:22 +0200768 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200769 rb_erase(n, &td->io_hist_tree);
Jens Axboe9e144182010-06-15 14:25:36 +0200770 td->io_hist_len--;
Jens Axboe01743ee2008-06-02 12:19:19 +0200771 } else if (!flist_empty(&td->io_hist_list)) {
772 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboe9e144182010-06-15 14:25:36 +0200773 td->io_hist_len--;
Jens Axboe01743ee2008-06-02 12:19:19 +0200774 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200775 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200776
Jens Axboe8de8f042007-03-27 10:36:12 +0200777 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200778 io_u->offset = ipo->offset;
779 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100780 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200781
Jens Axboed6aed792009-06-03 08:41:15 +0200782 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200783 int r = td_io_open_file(td, io_u->file);
784
Jens Axboebd6f78b2008-02-01 20:27:52 +0100785 if (r) {
786 dprint(FD_VERIFY, "failed file %s open\n",
787 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200788 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100789 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200790 }
791
792 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200793 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200794 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100795 io_u->xfer_buf = io_u->buf;
796 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200797 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100798 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200799 return 0;
800 }
801
Jens Axboebd6f78b2008-02-01 20:27:52 +0100802 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200803 return 1;
804}
Jens Axboee8462bd2009-07-06 12:59:04 +0200805
806static void *verify_async_thread(void *data)
807{
808 struct thread_data *td = data;
809 struct io_u *io_u;
810 int ret = 0;
811
812 if (td->o.verify_cpumask_set &&
813 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
814 log_err("fio: failed setting verify thread affinity\n");
815 goto done;
816 }
817
818 do {
Jens Axboee53ab272009-07-06 13:43:46 +0200819 FLIST_HEAD(list);
820
Jens Axboee8462bd2009-07-06 12:59:04 +0200821 read_barrier();
822 if (td->verify_thread_exit)
823 break;
824
825 pthread_mutex_lock(&td->io_u_lock);
826
827 while (flist_empty(&td->verify_list) &&
828 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +0200829 ret = pthread_cond_wait(&td->verify_cond,
830 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +0200831 if (ret) {
832 pthread_mutex_unlock(&td->io_u_lock);
833 break;
834 }
835 }
836
Jens Axboee53ab272009-07-06 13:43:46 +0200837 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +0200838 pthread_mutex_unlock(&td->io_u_lock);
839
Jens Axboee53ab272009-07-06 13:43:46 +0200840 if (flist_empty(&list))
841 continue;
842
843 while (!flist_empty(&list)) {
844 io_u = flist_entry(list.next, struct io_u, list);
845 flist_del_init(&io_u->list);
846
Jens Axboed561f2a2009-07-06 13:51:05 +0200847 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +0200848 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +0200849 if (!ret)
850 continue;
851 if (td->o.continue_on_error &&
852 td_non_fatal_error(ret)) {
853 update_error_count(td, ret);
854 td_clear_error(td);
855 ret = 0;
856 }
Jens Axboee53ab272009-07-06 13:43:46 +0200857 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200858 } while (!ret);
859
Jens Axboed561f2a2009-07-06 13:51:05 +0200860 if (ret) {
861 td_verror(td, ret, "async_verify");
Jens Axboef3e6cb92010-06-18 14:48:43 +0200862 if (td->o.verify_fatal)
863 td->terminate = 1;
Jens Axboed561f2a2009-07-06 13:51:05 +0200864 }
865
Jens Axboee8462bd2009-07-06 12:59:04 +0200866done:
867 pthread_mutex_lock(&td->io_u_lock);
868 td->nr_verify_threads--;
869 pthread_mutex_unlock(&td->io_u_lock);
870
871 pthread_cond_signal(&td->free_cond);
872 return NULL;
873}
874
875int verify_async_init(struct thread_data *td)
876{
877 int i, ret;
878
879 td->verify_thread_exit = 0;
880
881 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
882 for (i = 0; i < td->o.verify_async; i++) {
883 ret = pthread_create(&td->verify_threads[i], NULL,
884 verify_async_thread, td);
885 if (ret) {
886 log_err("fio: async verify creation failed: %s\n",
887 strerror(ret));
888 break;
889 }
890 ret = pthread_detach(td->verify_threads[i]);
891 if (ret) {
892 log_err("fio: async verify thread detach failed: %s\n",
893 strerror(ret));
894 break;
895 }
896 td->nr_verify_threads++;
897 }
898
899 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +0200900 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +0200901 td->verify_thread_exit = 1;
902 write_barrier();
903 pthread_cond_broadcast(&td->verify_cond);
904 return 1;
905 }
906
907 return 0;
908}
909
910void verify_async_exit(struct thread_data *td)
911{
912 td->verify_thread_exit = 1;
913 write_barrier();
914 pthread_cond_broadcast(&td->verify_cond);
915
916 pthread_mutex_lock(&td->io_u_lock);
917
918 while (td->nr_verify_threads)
919 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
920
921 pthread_mutex_unlock(&td->io_u_lock);
922 free(td->verify_threads);
923 td->verify_threads = NULL;
924}