blob: 193e171871928265dad467d6570800dc788cd669 [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);
Jens Axboe90059d62007-07-30 09:33:12 +020056 memset(p, td->o.verify_pattern, len);
57 break;
58 case 2:
59 case 3:
60 case 4: {
61 unsigned int pattern = td->o.verify_pattern;
62 unsigned int i = 0;
63 unsigned char c1, c2, c3, c4;
64 unsigned char *b = p;
65
Jens Axboebd6f78b2008-02-01 20:27:52 +010066 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
67 td->o.verify_pattern_bytes, len);
68
Jens Axboe90059d62007-07-30 09:33:12 +020069 c1 = pattern & 0xff;
70 pattern >>= 8;
71 c2 = pattern & 0xff;
72 pattern >>= 8;
73 c3 = pattern & 0xff;
74 pattern >>= 8;
75 c4 = pattern & 0xff;
76
77 while (i < len) {
78 b[i++] = c1;
79 if (i == len)
80 break;
81 b[i++] = c2;
82 if (td->o.verify_pattern_bytes == 2 || i == len)
83 continue;
84 b[i++] = c3;
85 if (td->o.verify_pattern_bytes == 3 || i == len)
86 continue;
87 b[i++] = c4;
88 }
89 break;
90 }
91 }
92}
93
Jens Axboe4764aec2007-08-23 09:03:38 +020094static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020095{
Shawn Lewisdee6de72007-08-02 22:17:52 +020096 char swap[200];
97
98 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020099
Shawn Lewis546a9142007-07-28 21:11:37 +0200100 memcpy(&swap, buf1, len);
101 memcpy(buf1, buf2, len);
102 memcpy(buf2, &swap, len);
103}
104
Jens Axboee29d1b72006-10-18 15:43:15 +0200105static void hexdump(void *buffer, int len)
106{
107 unsigned char *p = buffer;
108 int i;
109
110 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +0100111 log_info("%02x", p[i]);
112 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200113}
114
Jens Axboed9f2caf2007-07-28 21:22:03 +0200115/*
Jens Axboe87677832007-07-30 12:08:14 +0200116 * Prepare for seperation of verify_header and checksum header
117 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200118static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200119{
Jens Axboe5921e802008-05-30 15:02:38 +0200120 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +0200121
Jens Axboe546dfd92007-07-30 12:23:05 +0200122 switch (verify_type) {
123 case VERIFY_NONE:
124 case VERIFY_NULL:
125 len = 0;
126 break;
127 case VERIFY_MD5:
128 len = sizeof(struct vhdr_md5);
129 break;
130 case VERIFY_CRC64:
131 len = sizeof(struct vhdr_crc64);
132 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200133 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200134 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200135 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200136 len = sizeof(struct vhdr_crc32);
137 break;
138 case VERIFY_CRC16:
139 len = sizeof(struct vhdr_crc16);
140 break;
141 case VERIFY_CRC7:
142 len = sizeof(struct vhdr_crc7);
143 break;
144 case VERIFY_SHA256:
145 len = sizeof(struct vhdr_sha256);
146 break;
147 case VERIFY_SHA512:
148 len = sizeof(struct vhdr_sha512);
149 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200150 case VERIFY_META:
151 len = sizeof(struct vhdr_meta);
152 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200153 case VERIFY_SHA1:
154 len = sizeof(struct vhdr_sha1);
155 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200156 default:
157 log_err("fio: unknown verify header!\n");
158 assert(0);
159 }
160
161 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200162}
163
164static inline unsigned int hdr_size(struct verify_header *hdr)
165{
Jens Axboe546dfd92007-07-30 12:23:05 +0200166 return __hdr_size(hdr->verify_type);
167}
168
169static void *hdr_priv(struct verify_header *hdr)
170{
171 void *priv = hdr;
172
173 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200174}
175
176/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200177 * Return data area 'header_num'
178 */
179static inline void *io_u_verify_off(struct verify_header *hdr,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100180 struct io_u *io_u, unsigned char header_num)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200181{
Jens Axboe87677832007-07-30 12:08:14 +0200182 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200183}
184
Shawn Lewis7437ee82007-08-02 21:05:58 +0200185static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100186 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200187{
188 struct vhdr_meta *vh = hdr_priv(hdr);
189
Jens Axboebd6f78b2008-02-01 20:27:52 +0100190 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
191
Shawn Lewis7437ee82007-08-02 21:05:58 +0200192 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
193 log_err("meta: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100194 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200195 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200196 }
197
198 return 0;
199}
200
Jens Axboecd14cc12007-07-30 10:59:33 +0200201static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
202 unsigned int header_num)
203{
204 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200205 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200206 uint8_t sha512[128];
207 struct sha512_ctx sha512_ctx = {
208 .buf = sha512,
209 };
210
Jens Axboebd6f78b2008-02-01 20:27:52 +0100211 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
212
Jens Axboecd14cc12007-07-30 10:59:33 +0200213 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200214 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200215
Jens Axboe546dfd92007-07-30 12:23:05 +0200216 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200217 log_err("sha512: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100218 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200219 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200220 hexdump(sha512_ctx.buf, sizeof(sha512));
Jens Axboe9fd18962009-05-19 10:35:38 +0200221 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200222 }
223
224 return 0;
225}
226
227static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
228 unsigned int header_num)
229{
230 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200231 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200232 uint8_t sha256[128];
233 struct sha256_ctx sha256_ctx = {
234 .buf = sha256,
235 };
236
Jens Axboebd6f78b2008-02-01 20:27:52 +0100237 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
238
Jens Axboecd14cc12007-07-30 10:59:33 +0200239 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200240 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200241
Jens Axboe546dfd92007-07-30 12:23:05 +0200242 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200243 log_err("sha256: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100244 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200245 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200246 hexdump(sha256_ctx.buf, sizeof(sha256));
Jens Axboe9fd18962009-05-19 10:35:38 +0200247 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200248 }
249
250 return 0;
251}
252
Jens Axboe7c353ce2009-08-09 22:40:33 +0200253static int verify_io_u_sha1(struct verify_header *hdr, struct io_u *io_u,
254 unsigned int header_num)
255{
256 void *p = io_u_verify_off(hdr, io_u, header_num);
257 struct vhdr_sha1 *vh = hdr_priv(hdr);
258 uint32_t sha1[5];
259 struct sha1_ctx sha1_ctx = {
260 .H = sha1,
261 };
262
263 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", io_u, hdr->len);
264
265 sha1_init(&sha1_ctx);
266 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
267
268 if (memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) {
269 log_err("sha1: verify failed at %llu/%u\n",
270 io_u->offset + header_num * hdr->len, hdr->len);
271 hexdump(vh->sha1, sizeof(vh->sha1));
272 hexdump(sha1_ctx.H, sizeof(sha1));
273 return EILSEQ;
274 }
275
276 return 0;
277}
278
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200279static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100280 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200281{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200282 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200283 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200284 unsigned char c;
285
Jens Axboebd6f78b2008-02-01 20:27:52 +0100286 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
287
Jens Axboe87677832007-07-30 12:08:14 +0200288 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200289
Jens Axboe546dfd92007-07-30 12:23:05 +0200290 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200291 log_err("crc7: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100292 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200293 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200294 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200295 }
296
297 return 0;
298}
299
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200300static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100301 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200302{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200303 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200304 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200305 unsigned short c;
306
Jens Axboebd6f78b2008-02-01 20:27:52 +0100307 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
308
Jens Axboe87677832007-07-30 12:08:14 +0200309 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200310
Jens Axboe546dfd92007-07-30 12:23:05 +0200311 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200312 log_err("crc16: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100313 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200314 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200315 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200316 }
317
318 return 0;
319}
320
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200321static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100322 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200323{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200324 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200325 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200326 unsigned long long c;
327
Jens Axboebd6f78b2008-02-01 20:27:52 +0100328 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
329
Jens Axboe87677832007-07-30 12:08:14 +0200330 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200331
Jens Axboe546dfd92007-07-30 12:23:05 +0200332 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200333 log_err("crc64: verify failed at %llu/%u\n",
334 io_u->offset + header_num * hdr->len,
335 hdr->len);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100336 log_err("crc64: wanted %llx, got %llx\n",
337 (unsigned long long) vh->crc64, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200338 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200339 }
340
341 return 0;
342}
343
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200344static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100345 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200346{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200347 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200348 struct vhdr_crc32 *vh = hdr_priv(hdr);
349 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200350
Jens Axboebd6f78b2008-02-01 20:27:52 +0100351 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
352
Jens Axboe87677832007-07-30 12:08:14 +0200353 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200354
Jens Axboe546dfd92007-07-30 12:23:05 +0200355 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200356 log_err("crc32: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100357 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200358 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200359 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200360 }
361
362 return 0;
363}
364
Jens Axboebac39e02008-06-11 20:46:19 +0200365static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
366 unsigned int header_num)
367{
368 void *p = io_u_verify_off(hdr, io_u, header_num);
369 struct vhdr_crc32 *vh = hdr_priv(hdr);
370 uint32_t c;
371
372 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
373
Jens Axboe38455912008-08-04 15:35:26 +0200374 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
375 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
376 else
377 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200378
379 if (c != vh->crc32) {
380 log_err("crc32c: verify failed at %llu/%u\n",
381 io_u->offset + header_num * hdr->len, hdr->len);
382 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200383 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200384 }
385
386 return 0;
387}
388
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200389static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100390 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200391{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200392 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200393 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200394 uint32_t hash[MD5_HASH_WORDS];
395 struct md5_ctx md5_ctx = {
396 .hash = hash,
397 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200398
Jens Axboebd6f78b2008-02-01 20:27:52 +0100399 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
400
Jens Axboe61f821f2007-07-30 10:18:06 +0200401 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200402 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200403
Jens Axboe546dfd92007-07-30 12:23:05 +0200404 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200405 log_err("md5: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100406 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200407 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200408 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboe9fd18962009-05-19 10:35:38 +0200409 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200410 }
411
412 return 0;
413}
414
Jens Axboe3f199b02007-08-09 10:16:31 +0200415static unsigned int hweight8(unsigned int w)
416{
417 unsigned int res = w - ((w >> 1) & 0x55);
418
419 res = (res & 0x33) + ((res >> 2) & 0x33);
420 return (res + (res >> 4)) & 0x0F;
421}
422
Shawn Lewisa944e332007-08-02 22:18:29 +0200423int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100424 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200425{
426 unsigned int i;
427 char split_pattern[4];
428
429 for (i = 0; i < 4; i++) {
430 split_pattern[i] = pattern & 0xff;
431 pattern >>= 8;
432 }
433
434 for (i = 0; i < len; i++) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200435 if (buf[i] != split_pattern[mod]) {
436 unsigned int bits;
437
438 bits = hweight8(buf[i] ^ split_pattern[mod]);
439 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
440 buf[i], split_pattern[mod], bits);
441 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200442 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200443 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200444 mod++;
445 if (mod == pattern_size)
446 mod = 0;
447 }
448
449 return 0;
450}
451
Jens Axboee8462bd2009-07-06 12:59:04 +0200452/*
453 * Push IO verification to a separate thread
454 */
455int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
456{
457 if (io_u->file)
458 put_file_log(td, io_u->file);
459
460 io_u->file = NULL;
461
462 pthread_mutex_lock(&td->io_u_lock);
463 flist_del(&io_u->list);
464 flist_add_tail(&io_u->list, &td->verify_list);
465 pthread_mutex_unlock(&td->io_u_lock);
466
467 pthread_cond_signal(&td->verify_cond);
468 io_u->flags |= IO_U_F_FREE_DEF;
469 return 0;
470}
471
Jens Axboe36690c92007-03-26 10:23:34 +0200472int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200473{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200474 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200475 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200476 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200477 int ret;
478
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200479 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200480 return 0;
481
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200482 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200483 if (td->o.verify_interval)
484 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200485
Jens Axboea12a3b42007-08-09 10:20:54 +0200486 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100487 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
488 p += hdr_inc, hdr_num++) {
Jens Axboea12a3b42007-08-09 10:20:54 +0200489 if (ret && td->o.verify_fatal) {
490 td->terminate = 1;
491 break;
492 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200493 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200494 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200495 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200496 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200497
Jens Axboe3f199b02007-08-09 10:16:31 +0200498 if (hdr->fio_magic != FIO_HDR_MAGIC) {
499 log_err("Bad verify header %x\n", hdr->fio_magic);
Jens Axboe9fd18962009-05-19 10:35:38 +0200500 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200501 }
502
Shawn Lewise28218f2008-01-16 11:01:33 +0100503 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100504 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
505 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100506 ret = verify_io_u_pattern(td->o.verify_pattern,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100507 td->o.verify_pattern_bytes,
508 p + hdr_size,
509 hdr_inc - hdr_size,
510 hdr_size % 4);
Radha Ramachandrane92d3d72009-07-27 12:27:55 +0200511 /*
512 * Also verify the meta data, if applicable
513 */
514 if (hdr->verify_type == VERIFY_META)
515 ret |= verify_io_u_meta(hdr, td, io_u, hdr_num);
516
Shawn Lewise28218f2008-01-16 11:01:33 +0100517 if (ret)
518 log_err("fio: verify failed at %llu/%u\n",
519 io_u->offset + hdr_num * hdr->len,
520 hdr->len);
521 continue;
522 }
523
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200524 switch (hdr->verify_type) {
525 case VERIFY_MD5:
526 ret = verify_io_u_md5(hdr, io_u, hdr_num);
527 break;
528 case VERIFY_CRC64:
529 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
530 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 Axboebac39e02008-06-11 20:46:19 +0200533 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
534 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200535 case VERIFY_CRC32:
536 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
537 break;
538 case VERIFY_CRC16:
539 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
540 break;
541 case VERIFY_CRC7:
542 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
543 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200544 case VERIFY_SHA256:
545 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
546 break;
547 case VERIFY_SHA512:
548 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
549 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200550 case VERIFY_META:
551 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
552 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200553 case VERIFY_SHA1:
554 ret = verify_io_u_sha1(hdr, io_u, hdr_num);
555 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 Axboea12a3b42007-08-09 10:20:54 +0200562 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200563}
564
Shawn Lewis7437ee82007-08-02 21:05:58 +0200565static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100566 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200567{
568 struct vhdr_meta *vh = hdr_priv(hdr);
569
570 vh->thread = td->thread_number;
571
572 vh->time_sec = io_u->start_time.tv_sec;
573 vh->time_usec = io_u->start_time.tv_usec;
574
575 vh->numberio = td->io_issues[DDIR_WRITE];
576
577 vh->offset = io_u->offset + header_num * td->o.verify_interval;
578}
579
Jens Axboecd14cc12007-07-30 10:59:33 +0200580static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
581{
Jens Axboe546dfd92007-07-30 12:23:05 +0200582 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200583 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200584 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200585 };
586
587 sha512_init(&sha512_ctx);
588 sha512_update(&sha512_ctx, p, len);
589}
590
591static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
592{
Jens Axboe546dfd92007-07-30 12:23:05 +0200593 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200594 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200595 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200596 };
597
598 sha256_init(&sha256_ctx);
599 sha256_update(&sha256_ctx, p, len);
600}
601
Jens Axboe7c353ce2009-08-09 22:40:33 +0200602static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
603{
604 struct vhdr_sha1 *vh = hdr_priv(hdr);
605 struct sha1_ctx sha1_ctx = {
606 .H = vh->sha1,
607 };
608
609 sha1_init(&sha1_ctx);
610 sha1_update(&sha1_ctx, p, len);
611}
612
Jens Axboe1e154bd2007-07-27 09:52:40 +0200613static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
614{
Jens Axboe546dfd92007-07-30 12:23:05 +0200615 struct vhdr_crc7 *vh = hdr_priv(hdr);
616
617 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200618}
619
Jens Axboe969f7ed2007-07-27 09:07:17 +0200620static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
621{
Jens Axboe546dfd92007-07-30 12:23:05 +0200622 struct vhdr_crc16 *vh = hdr_priv(hdr);
623
624 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200625}
626
Jens Axboee29d1b72006-10-18 15:43:15 +0200627static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
628{
Jens Axboe546dfd92007-07-30 12:23:05 +0200629 struct vhdr_crc32 *vh = hdr_priv(hdr);
630
631 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200632}
633
Jens Axboebac39e02008-06-11 20:46:19 +0200634static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
635{
636 struct vhdr_crc32 *vh = hdr_priv(hdr);
637
Jens Axboe38455912008-08-04 15:35:26 +0200638 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
639 vh->crc32 = crc32c_intel(p, len);
640 else
641 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200642}
643
Jens Axboed77a7af2007-07-27 15:35:06 +0200644static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
645{
Jens Axboe546dfd92007-07-30 12:23:05 +0200646 struct vhdr_crc64 *vh = hdr_priv(hdr);
647
648 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200649}
650
Jens Axboee29d1b72006-10-18 15:43:15 +0200651static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
652{
Jens Axboe546dfd92007-07-30 12:23:05 +0200653 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200654 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200655 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200656 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200657
Jens Axboe61f821f2007-07-30 10:18:06 +0200658 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200659 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200660}
661
662/*
663 * fill body of io_u->buf with random data and add a header with the
664 * crc32 or md5 sum of that data.
665 */
666void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
667{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200668 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200669 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200670 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200671
Jens Axboe9cc3d152007-03-26 10:32:30 +0200672 if (td->o.verify == VERIFY_NULL)
673 return;
674
Jens Axboe90059d62007-07-30 09:33:12 +0200675 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200676
Shawn Lewis546a9142007-07-28 21:11:37 +0200677 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200678 if (td->o.verify_interval)
679 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200680
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100681 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200682 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200683
684 hdr->fio_magic = FIO_HDR_MAGIC;
685 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200686 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200687 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200688
Jens Axboe87677832007-07-30 12:08:14 +0200689 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200690 switch (td->o.verify) {
691 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100692 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
693 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200694 fill_md5(hdr, data, data_len);
695 break;
696 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100697 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
698 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200699 fill_crc64(hdr, data, data_len);
700 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200701 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200702 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200703 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
704 io_u, hdr->len);
705 fill_crc32c(hdr, data, data_len);
706 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200707 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100708 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
709 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200710 fill_crc32(hdr, data, data_len);
711 break;
712 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100713 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
714 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200715 fill_crc16(hdr, data, data_len);
716 break;
717 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100718 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
719 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200720 fill_crc7(hdr, data, data_len);
721 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200722 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100723 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
724 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200725 fill_sha256(hdr, data, data_len);
726 break;
727 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100728 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
729 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200730 fill_sha512(hdr, data, data_len);
731 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200732 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100733 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
734 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200735 fill_meta(hdr, td, io_u, header_num);
736 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200737 case VERIFY_SHA1:
738 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
739 io_u, hdr->len);
740 fill_sha1(hdr, data, data_len);
741 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200742 default:
743 log_err("fio: bad verify type: %d\n", td->o.verify);
744 assert(0);
745 }
Jens Axboea59e1702007-07-30 08:53:27 +0200746 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200747 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200748 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200749 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200750}
751
752int get_next_verify(struct thread_data *td, struct io_u *io_u)
753{
Jens Axboe8de8f042007-03-27 10:36:12 +0200754 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200755
Jens Axboed2d7fa52007-02-19 13:21:35 +0100756 /*
757 * this io_u is from a requeue, we already filled the offsets
758 */
759 if (io_u->file)
760 return 0;
761
Jens Axboe8de8f042007-03-27 10:36:12 +0200762 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
763 struct rb_node *n = rb_first(&td->io_hist_tree);
764
Jens Axboe4b878982007-03-26 09:32:22 +0200765 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200766 rb_erase(n, &td->io_hist_tree);
Jens Axboe01743ee2008-06-02 12:19:19 +0200767 } else if (!flist_empty(&td->io_hist_list)) {
768 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
769 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200770 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200771
Jens Axboe8de8f042007-03-27 10:36:12 +0200772 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200773 io_u->offset = ipo->offset;
774 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100775 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200776
Jens Axboed6aed792009-06-03 08:41:15 +0200777 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200778 int r = td_io_open_file(td, io_u->file);
779
Jens Axboebd6f78b2008-02-01 20:27:52 +0100780 if (r) {
781 dprint(FD_VERIFY, "failed file %s open\n",
782 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200783 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100784 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200785 }
786
787 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200788 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200789 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100790 io_u->xfer_buf = io_u->buf;
791 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200792 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100793 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200794 return 0;
795 }
796
Jens Axboebd6f78b2008-02-01 20:27:52 +0100797 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200798 return 1;
799}
Jens Axboee8462bd2009-07-06 12:59:04 +0200800
801static void *verify_async_thread(void *data)
802{
803 struct thread_data *td = data;
804 struct io_u *io_u;
805 int ret = 0;
806
807 if (td->o.verify_cpumask_set &&
808 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
809 log_err("fio: failed setting verify thread affinity\n");
810 goto done;
811 }
812
813 do {
Jens Axboee53ab272009-07-06 13:43:46 +0200814 FLIST_HEAD(list);
815
Jens Axboee8462bd2009-07-06 12:59:04 +0200816 read_barrier();
817 if (td->verify_thread_exit)
818 break;
819
820 pthread_mutex_lock(&td->io_u_lock);
821
822 while (flist_empty(&td->verify_list) &&
823 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +0200824 ret = pthread_cond_wait(&td->verify_cond,
825 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +0200826 if (ret) {
827 pthread_mutex_unlock(&td->io_u_lock);
828 break;
829 }
830 }
831
Jens Axboee53ab272009-07-06 13:43:46 +0200832 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +0200833 pthread_mutex_unlock(&td->io_u_lock);
834
Jens Axboee53ab272009-07-06 13:43:46 +0200835 if (flist_empty(&list))
836 continue;
837
838 while (!flist_empty(&list)) {
839 io_u = flist_entry(list.next, struct io_u, list);
840 flist_del_init(&io_u->list);
841
Jens Axboed561f2a2009-07-06 13:51:05 +0200842 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +0200843 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +0200844 if (!ret)
845 continue;
846 if (td->o.continue_on_error &&
847 td_non_fatal_error(ret)) {
848 update_error_count(td, ret);
849 td_clear_error(td);
850 ret = 0;
851 }
Jens Axboee53ab272009-07-06 13:43:46 +0200852 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200853 } while (!ret);
854
Jens Axboed561f2a2009-07-06 13:51:05 +0200855 if (ret) {
856 td_verror(td, ret, "async_verify");
857 td->terminate = 1;
858 }
859
Jens Axboee8462bd2009-07-06 12:59:04 +0200860done:
861 pthread_mutex_lock(&td->io_u_lock);
862 td->nr_verify_threads--;
863 pthread_mutex_unlock(&td->io_u_lock);
864
865 pthread_cond_signal(&td->free_cond);
866 return NULL;
867}
868
869int verify_async_init(struct thread_data *td)
870{
871 int i, ret;
872
873 td->verify_thread_exit = 0;
874
875 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
876 for (i = 0; i < td->o.verify_async; i++) {
877 ret = pthread_create(&td->verify_threads[i], NULL,
878 verify_async_thread, td);
879 if (ret) {
880 log_err("fio: async verify creation failed: %s\n",
881 strerror(ret));
882 break;
883 }
884 ret = pthread_detach(td->verify_threads[i]);
885 if (ret) {
886 log_err("fio: async verify thread detach failed: %s\n",
887 strerror(ret));
888 break;
889 }
890 td->nr_verify_threads++;
891 }
892
893 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +0200894 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +0200895 td->verify_thread_exit = 1;
896 write_barrier();
897 pthread_cond_broadcast(&td->verify_cond);
898 return 1;
899 }
900
901 return 0;
902}
903
904void verify_async_exit(struct thread_data *td)
905{
906 td->verify_thread_exit = 1;
907 write_barrier();
908 pthread_cond_broadcast(&td->verify_cond);
909
910 pthread_mutex_lock(&td->io_u_lock);
911
912 while (td->nr_verify_threads)
913 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
914
915 pthread_mutex_unlock(&td->io_u_lock);
916 free(td->verify_threads);
917 td->verify_threads = NULL;
918}