blob: 10bad7e2bab5f65c8b9168333a5f2567e40d19e1 [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 Axboecd14cc12007-07-30 10:59:33 +020022
Jens Axboe90059d62007-07-30 09:33:12 +020023static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020024{
25 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020026 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020027
28 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020029 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020030
31 /*
32 * lrand48_r seems to be broken and only fill the bottom
33 * 32-bits, even on 64-bit archs with 64-bit longs
34 */
35 todo = sizeof(r);
36 if (todo > len)
37 todo = len;
38
39 memcpy(p, &r, todo);
40
41 len -= todo;
42 p += todo;
43 }
44}
45
Jens Axboe90059d62007-07-30 09:33:12 +020046static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
47{
48 switch (td->o.verify_pattern_bytes) {
49 case 0:
Jens Axboebd6f78b2008-02-01 20:27:52 +010050 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020051 fill_random_bytes(td, p, len);
52 break;
53 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010054 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020055 memset(p, td->o.verify_pattern, len);
56 break;
57 case 2:
58 case 3:
59 case 4: {
60 unsigned int pattern = td->o.verify_pattern;
61 unsigned int i = 0;
62 unsigned char c1, c2, c3, c4;
63 unsigned char *b = p;
64
Jens Axboebd6f78b2008-02-01 20:27:52 +010065 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
66 td->o.verify_pattern_bytes, len);
67
Jens Axboe90059d62007-07-30 09:33:12 +020068 c1 = pattern & 0xff;
69 pattern >>= 8;
70 c2 = pattern & 0xff;
71 pattern >>= 8;
72 c3 = pattern & 0xff;
73 pattern >>= 8;
74 c4 = pattern & 0xff;
75
76 while (i < len) {
77 b[i++] = c1;
78 if (i == len)
79 break;
80 b[i++] = c2;
81 if (td->o.verify_pattern_bytes == 2 || i == len)
82 continue;
83 b[i++] = c3;
84 if (td->o.verify_pattern_bytes == 3 || i == len)
85 continue;
86 b[i++] = c4;
87 }
88 break;
89 }
90 }
91}
92
Jens Axboe4764aec2007-08-23 09:03:38 +020093static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020094{
Shawn Lewisdee6de72007-08-02 22:17:52 +020095 char swap[200];
96
97 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020098
Shawn Lewis546a9142007-07-28 21:11:37 +020099 memcpy(&swap, buf1, len);
100 memcpy(buf1, buf2, len);
101 memcpy(buf2, &swap, len);
102}
103
Jens Axboee29d1b72006-10-18 15:43:15 +0200104static void hexdump(void *buffer, int len)
105{
106 unsigned char *p = buffer;
107 int i;
108
109 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +0100110 log_info("%02x", p[i]);
111 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200112}
113
Jens Axboed9f2caf2007-07-28 21:22:03 +0200114/*
Jens Axboe87677832007-07-30 12:08:14 +0200115 * Prepare for seperation of verify_header and checksum header
116 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200117static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200118{
Jens Axboe5921e802008-05-30 15:02:38 +0200119 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +0200120
Jens Axboe546dfd92007-07-30 12:23:05 +0200121 switch (verify_type) {
122 case VERIFY_NONE:
123 case VERIFY_NULL:
124 len = 0;
125 break;
126 case VERIFY_MD5:
127 len = sizeof(struct vhdr_md5);
128 break;
129 case VERIFY_CRC64:
130 len = sizeof(struct vhdr_crc64);
131 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200132 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200133 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200134 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200135 len = sizeof(struct vhdr_crc32);
136 break;
137 case VERIFY_CRC16:
138 len = sizeof(struct vhdr_crc16);
139 break;
140 case VERIFY_CRC7:
141 len = sizeof(struct vhdr_crc7);
142 break;
143 case VERIFY_SHA256:
144 len = sizeof(struct vhdr_sha256);
145 break;
146 case VERIFY_SHA512:
147 len = sizeof(struct vhdr_sha512);
148 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200149 case VERIFY_META:
150 len = sizeof(struct vhdr_meta);
151 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200152 default:
153 log_err("fio: unknown verify header!\n");
154 assert(0);
155 }
156
157 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200158}
159
160static inline unsigned int hdr_size(struct verify_header *hdr)
161{
Jens Axboe546dfd92007-07-30 12:23:05 +0200162 return __hdr_size(hdr->verify_type);
163}
164
165static void *hdr_priv(struct verify_header *hdr)
166{
167 void *priv = hdr;
168
169 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200170}
171
172/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200173 * Return data area 'header_num'
174 */
175static inline void *io_u_verify_off(struct verify_header *hdr,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100176 struct io_u *io_u, unsigned char header_num)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200177{
Jens Axboe87677832007-07-30 12:08:14 +0200178 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200179}
180
Shawn Lewis7437ee82007-08-02 21:05:58 +0200181static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100182 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200183{
184 struct vhdr_meta *vh = hdr_priv(hdr);
185
Jens Axboebd6f78b2008-02-01 20:27:52 +0100186 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
187
Shawn Lewis7437ee82007-08-02 21:05:58 +0200188 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
189 log_err("meta: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100190 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200191 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200192 }
193
194 return 0;
195}
196
Jens Axboecd14cc12007-07-30 10:59:33 +0200197static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
198 unsigned int header_num)
199{
200 void *p = io_u_verify_off(hdr, io_u, header_num);
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 Axboebd6f78b2008-02-01 20:27:52 +0100207 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
208
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 Axboe546dfd92007-07-30 12:23:05 +0200212 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200213 log_err("sha512: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100214 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200215 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200216 hexdump(sha512_ctx.buf, sizeof(sha512));
Jens Axboe9fd18962009-05-19 10:35:38 +0200217 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200218 }
219
220 return 0;
221}
222
223static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
224 unsigned int header_num)
225{
226 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200227 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200228 uint8_t sha256[128];
229 struct sha256_ctx sha256_ctx = {
230 .buf = sha256,
231 };
232
Jens Axboebd6f78b2008-02-01 20:27:52 +0100233 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
234
Jens Axboecd14cc12007-07-30 10:59:33 +0200235 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200236 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200237
Jens Axboe546dfd92007-07-30 12:23:05 +0200238 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200239 log_err("sha256: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100240 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200241 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200242 hexdump(sha256_ctx.buf, sizeof(sha256));
Jens Axboe9fd18962009-05-19 10:35:38 +0200243 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200244 }
245
246 return 0;
247}
248
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200249static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100250 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200251{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200252 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200253 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200254 unsigned char c;
255
Jens Axboebd6f78b2008-02-01 20:27:52 +0100256 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
257
Jens Axboe87677832007-07-30 12:08:14 +0200258 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200259
Jens Axboe546dfd92007-07-30 12:23:05 +0200260 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200261 log_err("crc7: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100262 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200263 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200264 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200265 }
266
267 return 0;
268}
269
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200270static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100271 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200272{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200273 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200274 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200275 unsigned short c;
276
Jens Axboebd6f78b2008-02-01 20:27:52 +0100277 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
278
Jens Axboe87677832007-07-30 12:08:14 +0200279 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200280
Jens Axboe546dfd92007-07-30 12:23:05 +0200281 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200282 log_err("crc16: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100283 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200284 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200285 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200286 }
287
288 return 0;
289}
290
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200291static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100292 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200293{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200294 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200295 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200296 unsigned long long c;
297
Jens Axboebd6f78b2008-02-01 20:27:52 +0100298 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
299
Jens Axboe87677832007-07-30 12:08:14 +0200300 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200301
Jens Axboe546dfd92007-07-30 12:23:05 +0200302 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200303 log_err("crc64: verify failed at %llu/%u\n",
304 io_u->offset + header_num * hdr->len,
305 hdr->len);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100306 log_err("crc64: wanted %llx, got %llx\n",
307 (unsigned long long) vh->crc64, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200308 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200309 }
310
311 return 0;
312}
313
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200314static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100315 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200316{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200317 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200318 struct vhdr_crc32 *vh = hdr_priv(hdr);
319 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200320
Jens Axboebd6f78b2008-02-01 20:27:52 +0100321 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
322
Jens Axboe87677832007-07-30 12:08:14 +0200323 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200324
Jens Axboe546dfd92007-07-30 12:23:05 +0200325 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200326 log_err("crc32: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100327 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200328 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200329 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200330 }
331
332 return 0;
333}
334
Jens Axboebac39e02008-06-11 20:46:19 +0200335static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
336 unsigned int header_num)
337{
338 void *p = io_u_verify_off(hdr, io_u, header_num);
339 struct vhdr_crc32 *vh = hdr_priv(hdr);
340 uint32_t c;
341
342 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
343
Jens Axboe38455912008-08-04 15:35:26 +0200344 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
345 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
346 else
347 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200348
349 if (c != vh->crc32) {
350 log_err("crc32c: verify failed at %llu/%u\n",
351 io_u->offset + header_num * hdr->len, hdr->len);
352 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200353 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200354 }
355
356 return 0;
357}
358
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200359static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100360 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200361{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200362 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200363 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200364 uint32_t hash[MD5_HASH_WORDS];
365 struct md5_ctx md5_ctx = {
366 .hash = hash,
367 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200368
Jens Axboebd6f78b2008-02-01 20:27:52 +0100369 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
370
Jens Axboe61f821f2007-07-30 10:18:06 +0200371 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200372 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200373
Jens Axboe546dfd92007-07-30 12:23:05 +0200374 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200375 log_err("md5: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100376 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200377 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200378 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboe9fd18962009-05-19 10:35:38 +0200379 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200380 }
381
382 return 0;
383}
384
Jens Axboe3f199b02007-08-09 10:16:31 +0200385static unsigned int hweight8(unsigned int w)
386{
387 unsigned int res = w - ((w >> 1) & 0x55);
388
389 res = (res & 0x33) + ((res >> 2) & 0x33);
390 return (res + (res >> 4)) & 0x0F;
391}
392
Shawn Lewisa944e332007-08-02 22:18:29 +0200393int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100394 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200395{
396 unsigned int i;
397 char split_pattern[4];
398
399 for (i = 0; i < 4; i++) {
400 split_pattern[i] = pattern & 0xff;
401 pattern >>= 8;
402 }
403
404 for (i = 0; i < len; i++) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200405 if (buf[i] != split_pattern[mod]) {
406 unsigned int bits;
407
408 bits = hweight8(buf[i] ^ split_pattern[mod]);
409 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
410 buf[i], split_pattern[mod], bits);
411 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200412 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200413 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200414 mod++;
415 if (mod == pattern_size)
416 mod = 0;
417 }
418
419 return 0;
420}
421
Jens Axboee8462bd2009-07-06 12:59:04 +0200422/*
423 * Push IO verification to a separate thread
424 */
425int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
426{
427 if (io_u->file)
428 put_file_log(td, io_u->file);
429
430 io_u->file = NULL;
431
432 pthread_mutex_lock(&td->io_u_lock);
433 flist_del(&io_u->list);
434 flist_add_tail(&io_u->list, &td->verify_list);
435 pthread_mutex_unlock(&td->io_u_lock);
436
437 pthread_cond_signal(&td->verify_cond);
438 io_u->flags |= IO_U_F_FREE_DEF;
439 return 0;
440}
441
Jens Axboe36690c92007-03-26 10:23:34 +0200442int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200443{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200444 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200445 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200446 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200447 int ret;
448
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200449 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200450 return 0;
451
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200452 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200453 if (td->o.verify_interval)
454 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200455
Jens Axboea12a3b42007-08-09 10:20:54 +0200456 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100457 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
458 p += hdr_inc, hdr_num++) {
Jens Axboea12a3b42007-08-09 10:20:54 +0200459 if (ret && td->o.verify_fatal) {
460 td->terminate = 1;
461 break;
462 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200463 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200464 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200465 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200466 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200467
Jens Axboe3f199b02007-08-09 10:16:31 +0200468 if (hdr->fio_magic != FIO_HDR_MAGIC) {
469 log_err("Bad verify header %x\n", hdr->fio_magic);
Jens Axboe9fd18962009-05-19 10:35:38 +0200470 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200471 }
472
Shawn Lewise28218f2008-01-16 11:01:33 +0100473 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100474 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
475 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100476 ret = verify_io_u_pattern(td->o.verify_pattern,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100477 td->o.verify_pattern_bytes,
478 p + hdr_size,
479 hdr_inc - hdr_size,
480 hdr_size % 4);
Radha Ramachandrane92d3d72009-07-27 12:27:55 +0200481 /*
482 * Also verify the meta data, if applicable
483 */
484 if (hdr->verify_type == VERIFY_META)
485 ret |= verify_io_u_meta(hdr, td, io_u, hdr_num);
486
Shawn Lewise28218f2008-01-16 11:01:33 +0100487 if (ret)
488 log_err("fio: verify failed at %llu/%u\n",
489 io_u->offset + hdr_num * hdr->len,
490 hdr->len);
491 continue;
492 }
493
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200494 switch (hdr->verify_type) {
495 case VERIFY_MD5:
496 ret = verify_io_u_md5(hdr, io_u, hdr_num);
497 break;
498 case VERIFY_CRC64:
499 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
500 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200501 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200502 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200503 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
504 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200505 case VERIFY_CRC32:
506 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
507 break;
508 case VERIFY_CRC16:
509 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
510 break;
511 case VERIFY_CRC7:
512 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
513 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200514 case VERIFY_SHA256:
515 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
516 break;
517 case VERIFY_SHA512:
518 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
519 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200520 case VERIFY_META:
521 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
522 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200523 default:
524 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200525 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200526 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200527 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100528
Jens Axboea12a3b42007-08-09 10:20:54 +0200529 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200530}
531
Shawn Lewis7437ee82007-08-02 21:05:58 +0200532static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100533 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200534{
535 struct vhdr_meta *vh = hdr_priv(hdr);
536
537 vh->thread = td->thread_number;
538
539 vh->time_sec = io_u->start_time.tv_sec;
540 vh->time_usec = io_u->start_time.tv_usec;
541
542 vh->numberio = td->io_issues[DDIR_WRITE];
543
544 vh->offset = io_u->offset + header_num * td->o.verify_interval;
545}
546
Jens Axboecd14cc12007-07-30 10:59:33 +0200547static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
548{
Jens Axboe546dfd92007-07-30 12:23:05 +0200549 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200550 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200551 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200552 };
553
554 sha512_init(&sha512_ctx);
555 sha512_update(&sha512_ctx, p, len);
556}
557
558static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
559{
Jens Axboe546dfd92007-07-30 12:23:05 +0200560 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200561 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200562 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200563 };
564
565 sha256_init(&sha256_ctx);
566 sha256_update(&sha256_ctx, p, len);
567}
568
Jens Axboe1e154bd2007-07-27 09:52:40 +0200569static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
570{
Jens Axboe546dfd92007-07-30 12:23:05 +0200571 struct vhdr_crc7 *vh = hdr_priv(hdr);
572
573 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200574}
575
Jens Axboe969f7ed2007-07-27 09:07:17 +0200576static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
577{
Jens Axboe546dfd92007-07-30 12:23:05 +0200578 struct vhdr_crc16 *vh = hdr_priv(hdr);
579
580 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200581}
582
Jens Axboee29d1b72006-10-18 15:43:15 +0200583static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
584{
Jens Axboe546dfd92007-07-30 12:23:05 +0200585 struct vhdr_crc32 *vh = hdr_priv(hdr);
586
587 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200588}
589
Jens Axboebac39e02008-06-11 20:46:19 +0200590static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
591{
592 struct vhdr_crc32 *vh = hdr_priv(hdr);
593
Jens Axboe38455912008-08-04 15:35:26 +0200594 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
595 vh->crc32 = crc32c_intel(p, len);
596 else
597 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200598}
599
Jens Axboed77a7af2007-07-27 15:35:06 +0200600static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
601{
Jens Axboe546dfd92007-07-30 12:23:05 +0200602 struct vhdr_crc64 *vh = hdr_priv(hdr);
603
604 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200605}
606
Jens Axboee29d1b72006-10-18 15:43:15 +0200607static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
608{
Jens Axboe546dfd92007-07-30 12:23:05 +0200609 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200610 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200611 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200612 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200613
Jens Axboe61f821f2007-07-30 10:18:06 +0200614 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200615 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200616}
617
618/*
619 * fill body of io_u->buf with random data and add a header with the
620 * crc32 or md5 sum of that data.
621 */
622void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
623{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200624 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200625 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200626 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200627
Jens Axboe9cc3d152007-03-26 10:32:30 +0200628 if (td->o.verify == VERIFY_NULL)
629 return;
630
Jens Axboe90059d62007-07-30 09:33:12 +0200631 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200632
Shawn Lewis546a9142007-07-28 21:11:37 +0200633 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200634 if (td->o.verify_interval)
635 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200636
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100637 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200638 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200639
640 hdr->fio_magic = FIO_HDR_MAGIC;
641 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200642 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200643 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200644
Jens Axboe87677832007-07-30 12:08:14 +0200645 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200646 switch (td->o.verify) {
647 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100648 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
649 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200650 fill_md5(hdr, data, data_len);
651 break;
652 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100653 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
654 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200655 fill_crc64(hdr, data, data_len);
656 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200657 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200658 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200659 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
660 io_u, hdr->len);
661 fill_crc32c(hdr, data, data_len);
662 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200663 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100664 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
665 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200666 fill_crc32(hdr, data, data_len);
667 break;
668 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100669 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
670 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200671 fill_crc16(hdr, data, data_len);
672 break;
673 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100674 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
675 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200676 fill_crc7(hdr, data, data_len);
677 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200678 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100679 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
680 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200681 fill_sha256(hdr, data, data_len);
682 break;
683 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100684 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
685 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200686 fill_sha512(hdr, data, data_len);
687 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200688 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100689 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
690 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200691 fill_meta(hdr, td, io_u, header_num);
692 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200693 default:
694 log_err("fio: bad verify type: %d\n", td->o.verify);
695 assert(0);
696 }
Jens Axboea59e1702007-07-30 08:53:27 +0200697 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200698 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200699 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200700 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200701}
702
703int get_next_verify(struct thread_data *td, struct io_u *io_u)
704{
Jens Axboe8de8f042007-03-27 10:36:12 +0200705 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200706
Jens Axboed2d7fa52007-02-19 13:21:35 +0100707 /*
708 * this io_u is from a requeue, we already filled the offsets
709 */
710 if (io_u->file)
711 return 0;
712
Jens Axboe8de8f042007-03-27 10:36:12 +0200713 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
714 struct rb_node *n = rb_first(&td->io_hist_tree);
715
Jens Axboe4b878982007-03-26 09:32:22 +0200716 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200717 rb_erase(n, &td->io_hist_tree);
Jens Axboe01743ee2008-06-02 12:19:19 +0200718 } else if (!flist_empty(&td->io_hist_list)) {
719 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
720 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200721 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200722
Jens Axboe8de8f042007-03-27 10:36:12 +0200723 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200724 io_u->offset = ipo->offset;
725 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100726 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200727
Jens Axboed6aed792009-06-03 08:41:15 +0200728 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200729 int r = td_io_open_file(td, io_u->file);
730
Jens Axboebd6f78b2008-02-01 20:27:52 +0100731 if (r) {
732 dprint(FD_VERIFY, "failed file %s open\n",
733 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200734 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100735 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200736 }
737
738 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200739 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200740 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100741 io_u->xfer_buf = io_u->buf;
742 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200743 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100744 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200745 return 0;
746 }
747
Jens Axboebd6f78b2008-02-01 20:27:52 +0100748 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200749 return 1;
750}
Jens Axboee8462bd2009-07-06 12:59:04 +0200751
752static void *verify_async_thread(void *data)
753{
754 struct thread_data *td = data;
755 struct io_u *io_u;
756 int ret = 0;
757
758 if (td->o.verify_cpumask_set &&
759 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
760 log_err("fio: failed setting verify thread affinity\n");
761 goto done;
762 }
763
764 do {
Jens Axboee53ab272009-07-06 13:43:46 +0200765 FLIST_HEAD(list);
766
Jens Axboee8462bd2009-07-06 12:59:04 +0200767 read_barrier();
768 if (td->verify_thread_exit)
769 break;
770
771 pthread_mutex_lock(&td->io_u_lock);
772
773 while (flist_empty(&td->verify_list) &&
774 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +0200775 ret = pthread_cond_wait(&td->verify_cond,
776 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +0200777 if (ret) {
778 pthread_mutex_unlock(&td->io_u_lock);
779 break;
780 }
781 }
782
Jens Axboee53ab272009-07-06 13:43:46 +0200783 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +0200784 pthread_mutex_unlock(&td->io_u_lock);
785
Jens Axboee53ab272009-07-06 13:43:46 +0200786 if (flist_empty(&list))
787 continue;
788
789 while (!flist_empty(&list)) {
790 io_u = flist_entry(list.next, struct io_u, list);
791 flist_del_init(&io_u->list);
792
Jens Axboed561f2a2009-07-06 13:51:05 +0200793 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +0200794 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +0200795 if (!ret)
796 continue;
797 if (td->o.continue_on_error &&
798 td_non_fatal_error(ret)) {
799 update_error_count(td, ret);
800 td_clear_error(td);
801 ret = 0;
802 }
Jens Axboee53ab272009-07-06 13:43:46 +0200803 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200804 } while (!ret);
805
Jens Axboed561f2a2009-07-06 13:51:05 +0200806 if (ret) {
807 td_verror(td, ret, "async_verify");
808 td->terminate = 1;
809 }
810
Jens Axboee8462bd2009-07-06 12:59:04 +0200811done:
812 pthread_mutex_lock(&td->io_u_lock);
813 td->nr_verify_threads--;
814 pthread_mutex_unlock(&td->io_u_lock);
815
816 pthread_cond_signal(&td->free_cond);
817 return NULL;
818}
819
820int verify_async_init(struct thread_data *td)
821{
822 int i, ret;
823
824 td->verify_thread_exit = 0;
825
826 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
827 for (i = 0; i < td->o.verify_async; i++) {
828 ret = pthread_create(&td->verify_threads[i], NULL,
829 verify_async_thread, td);
830 if (ret) {
831 log_err("fio: async verify creation failed: %s\n",
832 strerror(ret));
833 break;
834 }
835 ret = pthread_detach(td->verify_threads[i]);
836 if (ret) {
837 log_err("fio: async verify thread detach failed: %s\n",
838 strerror(ret));
839 break;
840 }
841 td->nr_verify_threads++;
842 }
843
844 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +0200845 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +0200846 td->verify_thread_exit = 1;
847 write_barrier();
848 pthread_cond_broadcast(&td->verify_cond);
849 return 1;
850 }
851
852 return 0;
853}
854
855void verify_async_exit(struct thread_data *td)
856{
857 td->verify_thread_exit = 1;
858 write_barrier();
859 pthread_cond_broadcast(&td->verify_cond);
860
861 pthread_mutex_lock(&td->io_u_lock);
862
863 while (td->nr_verify_threads)
864 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
865
866 pthread_mutex_unlock(&td->io_u_lock);
867 free(td->verify_threads);
868 td->verify_threads = NULL;
869}