blob: faa5684d9dafa369551c41c4faf045b2fa4ad469 [file] [log] [blame]
Jens Axboee29d1b72006-10-18 15:43:15 +02001/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
Jens Axboe97af62c2007-05-22 11:12:13 +02007#include <assert.h>
Jens Axboee8462bd2009-07-06 12:59:04 +02008#include <pthread.h>
Jens Axboee29d1b72006-10-18 15:43:15 +02009
10#include "fio.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020011#include "verify.h"
Jens Axboee8462bd2009-07-06 12:59:04 +020012#include "smalloc.h"
Jens Axboee29d1b72006-10-18 15:43:15 +020013
Jens Axboeeef6eea2007-07-30 12:27:58 +020014#include "crc/md5.h"
15#include "crc/crc64.h"
16#include "crc/crc32.h"
Jens Axboebac39e02008-06-11 20:46:19 +020017#include "crc/crc32c.h"
Jens Axboeeef6eea2007-07-30 12:27:58 +020018#include "crc/crc16.h"
19#include "crc/crc7.h"
20#include "crc/sha256.h"
21#include "crc/sha512.h"
Jens Axboe7c353ce2009-08-09 22:40:33 +020022#include "crc/sha1.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020023
Jens Axboe90059d62007-07-30 09:33:12 +020024static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020025{
26 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020027 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020028
29 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020030 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020031
32 /*
33 * lrand48_r seems to be broken and only fill the bottom
34 * 32-bits, even on 64-bit archs with 64-bit longs
35 */
36 todo = sizeof(r);
37 if (todo > len)
38 todo = len;
39
40 memcpy(p, &r, todo);
41
42 len -= todo;
43 p += todo;
44 }
45}
46
Jens Axboe90059d62007-07-30 09:33:12 +020047static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
48{
49 switch (td->o.verify_pattern_bytes) {
50 case 0:
Jens Axboebd6f78b2008-02-01 20:27:52 +010051 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020052 fill_random_bytes(td, p, len);
53 break;
54 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010055 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Radha Ramachandran0e92f872009-10-27 20:14:27 +010056 memset(p, td->o.verify_pattern[0], len);
Jens Axboe90059d62007-07-30 09:33:12 +020057 break;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010058 default: {
59 unsigned int i = 0, size = 0;
Jens Axboe90059d62007-07-30 09:33:12 +020060 unsigned char *b = p;
61
Jens Axboebd6f78b2008-02-01 20:27:52 +010062 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
63 td->o.verify_pattern_bytes, len);
64
Jens Axboe90059d62007-07-30 09:33:12 +020065 while (i < len) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +010066 size = td->o.verify_pattern_bytes;
67 if (size > (len - i))
68 size = len - i;
69 memcpy(b+i, td->o.verify_pattern, size);
70 i += size;
Jens Axboe90059d62007-07-30 09:33:12 +020071 }
72 break;
73 }
74 }
75}
76
Jens Axboe4764aec2007-08-23 09:03:38 +020077static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020078{
Shawn Lewisdee6de72007-08-02 22:17:52 +020079 char swap[200];
80
81 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020082
Shawn Lewis546a9142007-07-28 21:11:37 +020083 memcpy(&swap, buf1, len);
84 memcpy(buf1, buf2, len);
85 memcpy(buf2, &swap, len);
86}
87
Jens Axboee29d1b72006-10-18 15:43:15 +020088static void hexdump(void *buffer, int len)
89{
90 unsigned char *p = buffer;
91 int i;
92
93 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +010094 log_info("%02x", p[i]);
95 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020096}
97
Jens Axboed9f2caf2007-07-28 21:22:03 +020098/*
Jens Axboe87677832007-07-30 12:08:14 +020099 * Prepare for seperation of verify_header and checksum header
100 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200101static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200102{
Jens Axboe5921e802008-05-30 15:02:38 +0200103 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +0200104
Jens Axboe546dfd92007-07-30 12:23:05 +0200105 switch (verify_type) {
106 case VERIFY_NONE:
107 case VERIFY_NULL:
108 len = 0;
109 break;
110 case VERIFY_MD5:
111 len = sizeof(struct vhdr_md5);
112 break;
113 case VERIFY_CRC64:
114 len = sizeof(struct vhdr_crc64);
115 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200116 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200117 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200118 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200119 len = sizeof(struct vhdr_crc32);
120 break;
121 case VERIFY_CRC16:
122 len = sizeof(struct vhdr_crc16);
123 break;
124 case VERIFY_CRC7:
125 len = sizeof(struct vhdr_crc7);
126 break;
127 case VERIFY_SHA256:
128 len = sizeof(struct vhdr_sha256);
129 break;
130 case VERIFY_SHA512:
131 len = sizeof(struct vhdr_sha512);
132 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200133 case VERIFY_META:
134 len = sizeof(struct vhdr_meta);
135 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200136 case VERIFY_SHA1:
137 len = sizeof(struct vhdr_sha1);
138 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200139 default:
140 log_err("fio: unknown verify header!\n");
141 assert(0);
142 }
143
144 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200145}
146
147static inline unsigned int hdr_size(struct verify_header *hdr)
148{
Jens Axboe546dfd92007-07-30 12:23:05 +0200149 return __hdr_size(hdr->verify_type);
150}
151
152static void *hdr_priv(struct verify_header *hdr)
153{
154 void *priv = hdr;
155
156 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200157}
158
159/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200160 * Return data area 'header_num'
161 */
162static inline void *io_u_verify_off(struct verify_header *hdr,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100163 struct io_u *io_u, unsigned char header_num)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200164{
Jens Axboe87677832007-07-30 12:08:14 +0200165 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200166}
167
Shawn Lewis7437ee82007-08-02 21:05:58 +0200168static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100169 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200170{
171 struct vhdr_meta *vh = hdr_priv(hdr);
172
Jens Axboebd6f78b2008-02-01 20:27:52 +0100173 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
174
Shawn Lewis7437ee82007-08-02 21:05:58 +0200175 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
176 log_err("meta: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100177 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200178 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200179 }
180
181 return 0;
182}
183
Jens Axboecd14cc12007-07-30 10:59:33 +0200184static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
185 unsigned int header_num)
186{
187 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200188 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200189 uint8_t sha512[128];
190 struct sha512_ctx sha512_ctx = {
191 .buf = sha512,
192 };
193
Jens Axboebd6f78b2008-02-01 20:27:52 +0100194 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
195
Jens Axboecd14cc12007-07-30 10:59:33 +0200196 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200197 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200198
Jens Axboe546dfd92007-07-30 12:23:05 +0200199 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200200 log_err("sha512: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100201 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200202 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200203 hexdump(sha512_ctx.buf, sizeof(sha512));
Jens Axboe9fd18962009-05-19 10:35:38 +0200204 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200205 }
206
207 return 0;
208}
209
210static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
211 unsigned int header_num)
212{
213 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200214 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200215 uint8_t sha256[128];
216 struct sha256_ctx sha256_ctx = {
217 .buf = sha256,
218 };
219
Jens Axboebd6f78b2008-02-01 20:27:52 +0100220 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
221
Jens Axboecd14cc12007-07-30 10:59:33 +0200222 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200223 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200224
Jens Axboe546dfd92007-07-30 12:23:05 +0200225 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200226 log_err("sha256: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100227 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200228 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200229 hexdump(sha256_ctx.buf, sizeof(sha256));
Jens Axboe9fd18962009-05-19 10:35:38 +0200230 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200231 }
232
233 return 0;
234}
235
Jens Axboe7c353ce2009-08-09 22:40:33 +0200236static int verify_io_u_sha1(struct verify_header *hdr, struct io_u *io_u,
237 unsigned int header_num)
238{
239 void *p = io_u_verify_off(hdr, io_u, header_num);
240 struct vhdr_sha1 *vh = hdr_priv(hdr);
241 uint32_t sha1[5];
242 struct sha1_ctx sha1_ctx = {
243 .H = sha1,
244 };
245
246 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", io_u, hdr->len);
247
248 sha1_init(&sha1_ctx);
249 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
250
251 if (memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) {
252 log_err("sha1: verify failed at %llu/%u\n",
253 io_u->offset + header_num * hdr->len, hdr->len);
254 hexdump(vh->sha1, sizeof(vh->sha1));
255 hexdump(sha1_ctx.H, sizeof(sha1));
256 return EILSEQ;
257 }
258
259 return 0;
260}
261
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200262static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100263 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200264{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200265 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200266 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200267 unsigned char c;
268
Jens Axboebd6f78b2008-02-01 20:27:52 +0100269 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
270
Jens Axboe87677832007-07-30 12:08:14 +0200271 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200272
Jens Axboe546dfd92007-07-30 12:23:05 +0200273 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200274 log_err("crc7: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100275 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200276 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200277 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200278 }
279
280 return 0;
281}
282
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200283static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100284 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200285{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200286 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200287 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200288 unsigned short c;
289
Jens Axboebd6f78b2008-02-01 20:27:52 +0100290 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
291
Jens Axboe87677832007-07-30 12:08:14 +0200292 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200293
Jens Axboe546dfd92007-07-30 12:23:05 +0200294 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200295 log_err("crc16: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100296 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200297 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200298 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200299 }
300
301 return 0;
302}
303
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200304static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100305 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200306{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200307 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200308 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200309 unsigned long long c;
310
Jens Axboebd6f78b2008-02-01 20:27:52 +0100311 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
312
Jens Axboe87677832007-07-30 12:08:14 +0200313 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200314
Jens Axboe546dfd92007-07-30 12:23:05 +0200315 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200316 log_err("crc64: verify failed at %llu/%u\n",
317 io_u->offset + header_num * hdr->len,
318 hdr->len);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100319 log_err("crc64: wanted %llx, got %llx\n",
320 (unsigned long long) vh->crc64, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200321 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200322 }
323
324 return 0;
325}
326
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200327static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100328 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200329{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200330 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200331 struct vhdr_crc32 *vh = hdr_priv(hdr);
332 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200333
Jens Axboebd6f78b2008-02-01 20:27:52 +0100334 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
335
Jens Axboe87677832007-07-30 12:08:14 +0200336 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200337
Jens Axboe546dfd92007-07-30 12:23:05 +0200338 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200339 log_err("crc32: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100340 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200341 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200342 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200343 }
344
345 return 0;
346}
347
Jens Axboebac39e02008-06-11 20:46:19 +0200348static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
349 unsigned int header_num)
350{
351 void *p = io_u_verify_off(hdr, io_u, header_num);
352 struct vhdr_crc32 *vh = hdr_priv(hdr);
353 uint32_t c;
354
355 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
356
Jens Axboe38455912008-08-04 15:35:26 +0200357 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
358 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
359 else
360 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200361
362 if (c != vh->crc32) {
363 log_err("crc32c: verify failed at %llu/%u\n",
364 io_u->offset + header_num * hdr->len, hdr->len);
365 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200366 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200367 }
368
369 return 0;
370}
371
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200372static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100373 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200374{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200375 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200376 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200377 uint32_t hash[MD5_HASH_WORDS];
378 struct md5_ctx md5_ctx = {
379 .hash = hash,
380 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200381
Jens Axboebd6f78b2008-02-01 20:27:52 +0100382 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
383
Jens Axboe61f821f2007-07-30 10:18:06 +0200384 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200385 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200386
Jens Axboe546dfd92007-07-30 12:23:05 +0200387 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200388 log_err("md5: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100389 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200390 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200391 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboe9fd18962009-05-19 10:35:38 +0200392 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200393 }
394
395 return 0;
396}
397
Jens Axboe3f199b02007-08-09 10:16:31 +0200398static unsigned int hweight8(unsigned int w)
399{
400 unsigned int res = w - ((w >> 1) & 0x55);
401
402 res = (res & 0x33) + ((res >> 2) & 0x33);
403 return (res + (res >> 4)) & 0x0F;
404}
405
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100406int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100407 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200408{
409 unsigned int i;
Shawn Lewisa944e332007-08-02 22:18:29 +0200410
411 for (i = 0; i < len; i++) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100412 if (buf[i] != pattern[mod]) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200413 unsigned int bits;
414
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100415 bits = hweight8(buf[i] ^ pattern[mod]);
Jens Axboe3f199b02007-08-09 10:16:31 +0200416 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100417 buf[i], pattern[mod], bits);
Jens Axboe3f199b02007-08-09 10:16:31 +0200418 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200419 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200420 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200421 mod++;
422 if (mod == pattern_size)
423 mod = 0;
424 }
425
426 return 0;
427}
428
Jens Axboee8462bd2009-07-06 12:59:04 +0200429/*
430 * Push IO verification to a separate thread
431 */
432int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
433{
434 if (io_u->file)
435 put_file_log(td, io_u->file);
436
437 io_u->file = NULL;
438
439 pthread_mutex_lock(&td->io_u_lock);
440 flist_del(&io_u->list);
441 flist_add_tail(&io_u->list, &td->verify_list);
442 pthread_mutex_unlock(&td->io_u_lock);
443
444 pthread_cond_signal(&td->verify_cond);
445 io_u->flags |= IO_U_F_FREE_DEF;
446 return 0;
447}
448
Jens Axboe36690c92007-03-26 10:23:34 +0200449int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200450{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200451 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200452 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200453 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200454 int ret;
455
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200456 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200457 return 0;
458
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200459 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200460 if (td->o.verify_interval)
461 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200462
Jens Axboea12a3b42007-08-09 10:20:54 +0200463 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100464 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
465 p += hdr_inc, hdr_num++) {
Jens Axboea12a3b42007-08-09 10:20:54 +0200466 if (ret && td->o.verify_fatal) {
467 td->terminate = 1;
468 break;
469 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200470 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200471 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200472 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200473 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200474
Jens Axboe3f199b02007-08-09 10:16:31 +0200475 if (hdr->fio_magic != FIO_HDR_MAGIC) {
476 log_err("Bad verify header %x\n", hdr->fio_magic);
Jens Axboe9fd18962009-05-19 10:35:38 +0200477 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200478 }
479
Shawn Lewise28218f2008-01-16 11:01:33 +0100480 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100481 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
482 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100483 ret = verify_io_u_pattern(td->o.verify_pattern,
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100484 td->o.verify_pattern_bytes,
485 p + hdr_size,
486 hdr_inc - hdr_size,
487 hdr_size % td->o.verify_pattern_bytes);
Radha Ramachandrane92d3d72009-07-27 12:27:55 +0200488 /*
489 * Also verify the meta data, if applicable
490 */
491 if (hdr->verify_type == VERIFY_META)
492 ret |= verify_io_u_meta(hdr, td, io_u, hdr_num);
493
Shawn Lewise28218f2008-01-16 11:01:33 +0100494 if (ret)
495 log_err("fio: verify failed at %llu/%u\n",
496 io_u->offset + hdr_num * hdr->len,
497 hdr->len);
498 continue;
499 }
500
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200501 switch (hdr->verify_type) {
502 case VERIFY_MD5:
503 ret = verify_io_u_md5(hdr, io_u, hdr_num);
504 break;
505 case VERIFY_CRC64:
506 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
507 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200508 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200509 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200510 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
511 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200512 case VERIFY_CRC32:
513 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
514 break;
515 case VERIFY_CRC16:
516 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
517 break;
518 case VERIFY_CRC7:
519 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
520 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200521 case VERIFY_SHA256:
522 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
523 break;
524 case VERIFY_SHA512:
525 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
526 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200527 case VERIFY_META:
528 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
529 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200530 case VERIFY_SHA1:
531 ret = verify_io_u_sha1(hdr, io_u, hdr_num);
532 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200533 default:
534 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200535 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200536 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200537 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100538
Jens Axboea12a3b42007-08-09 10:20:54 +0200539 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200540}
541
Shawn Lewis7437ee82007-08-02 21:05:58 +0200542static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100543 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200544{
545 struct vhdr_meta *vh = hdr_priv(hdr);
546
547 vh->thread = td->thread_number;
548
549 vh->time_sec = io_u->start_time.tv_sec;
550 vh->time_usec = io_u->start_time.tv_usec;
551
552 vh->numberio = td->io_issues[DDIR_WRITE];
553
554 vh->offset = io_u->offset + header_num * td->o.verify_interval;
555}
556
Jens Axboecd14cc12007-07-30 10:59:33 +0200557static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
558{
Jens Axboe546dfd92007-07-30 12:23:05 +0200559 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200560 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200561 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200562 };
563
564 sha512_init(&sha512_ctx);
565 sha512_update(&sha512_ctx, p, len);
566}
567
568static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
569{
Jens Axboe546dfd92007-07-30 12:23:05 +0200570 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200571 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200572 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200573 };
574
575 sha256_init(&sha256_ctx);
576 sha256_update(&sha256_ctx, p, len);
577}
578
Jens Axboe7c353ce2009-08-09 22:40:33 +0200579static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
580{
581 struct vhdr_sha1 *vh = hdr_priv(hdr);
582 struct sha1_ctx sha1_ctx = {
583 .H = vh->sha1,
584 };
585
586 sha1_init(&sha1_ctx);
587 sha1_update(&sha1_ctx, p, len);
588}
589
Jens Axboe1e154bd2007-07-27 09:52:40 +0200590static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
591{
Jens Axboe546dfd92007-07-30 12:23:05 +0200592 struct vhdr_crc7 *vh = hdr_priv(hdr);
593
594 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200595}
596
Jens Axboe969f7ed2007-07-27 09:07:17 +0200597static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
598{
Jens Axboe546dfd92007-07-30 12:23:05 +0200599 struct vhdr_crc16 *vh = hdr_priv(hdr);
600
601 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200602}
603
Jens Axboee29d1b72006-10-18 15:43:15 +0200604static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
605{
Jens Axboe546dfd92007-07-30 12:23:05 +0200606 struct vhdr_crc32 *vh = hdr_priv(hdr);
607
608 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200609}
610
Jens Axboebac39e02008-06-11 20:46:19 +0200611static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
612{
613 struct vhdr_crc32 *vh = hdr_priv(hdr);
614
Jens Axboe38455912008-08-04 15:35:26 +0200615 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
616 vh->crc32 = crc32c_intel(p, len);
617 else
618 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200619}
620
Jens Axboed77a7af2007-07-27 15:35:06 +0200621static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
622{
Jens Axboe546dfd92007-07-30 12:23:05 +0200623 struct vhdr_crc64 *vh = hdr_priv(hdr);
624
625 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200626}
627
Jens Axboee29d1b72006-10-18 15:43:15 +0200628static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
629{
Jens Axboe546dfd92007-07-30 12:23:05 +0200630 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200631 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200632 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200633 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200634
Jens Axboe61f821f2007-07-30 10:18:06 +0200635 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200636 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200637}
638
639/*
640 * fill body of io_u->buf with random data and add a header with the
641 * crc32 or md5 sum of that data.
642 */
643void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
644{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200645 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200646 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200647 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200648
Jens Axboe9cc3d152007-03-26 10:32:30 +0200649 if (td->o.verify == VERIFY_NULL)
650 return;
651
Jens Axboe90059d62007-07-30 09:33:12 +0200652 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200653
Shawn Lewis546a9142007-07-28 21:11:37 +0200654 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200655 if (td->o.verify_interval)
656 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200657
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100658 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200659 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200660
661 hdr->fio_magic = FIO_HDR_MAGIC;
662 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200663 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200664 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200665
Jens Axboe87677832007-07-30 12:08:14 +0200666 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200667 switch (td->o.verify) {
668 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100669 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
670 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200671 fill_md5(hdr, data, data_len);
672 break;
673 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100674 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
675 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200676 fill_crc64(hdr, data, data_len);
677 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200678 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200679 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200680 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
681 io_u, hdr->len);
682 fill_crc32c(hdr, data, data_len);
683 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200684 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100685 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
686 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200687 fill_crc32(hdr, data, data_len);
688 break;
689 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100690 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
691 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200692 fill_crc16(hdr, data, data_len);
693 break;
694 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100695 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
696 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200697 fill_crc7(hdr, data, data_len);
698 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200699 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100700 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
701 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200702 fill_sha256(hdr, data, data_len);
703 break;
704 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100705 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
706 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200707 fill_sha512(hdr, data, data_len);
708 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200709 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100710 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
711 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200712 fill_meta(hdr, td, io_u, header_num);
713 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200714 case VERIFY_SHA1:
715 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
716 io_u, hdr->len);
717 fill_sha1(hdr, data, data_len);
718 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200719 default:
720 log_err("fio: bad verify type: %d\n", td->o.verify);
721 assert(0);
722 }
Jens Axboea59e1702007-07-30 08:53:27 +0200723 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200724 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200725 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200726 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200727}
728
729int get_next_verify(struct thread_data *td, struct io_u *io_u)
730{
Jens Axboe8de8f042007-03-27 10:36:12 +0200731 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200732
Jens Axboed2d7fa52007-02-19 13:21:35 +0100733 /*
734 * this io_u is from a requeue, we already filled the offsets
735 */
736 if (io_u->file)
737 return 0;
738
Jens Axboe8de8f042007-03-27 10:36:12 +0200739 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
740 struct rb_node *n = rb_first(&td->io_hist_tree);
741
Jens Axboe4b878982007-03-26 09:32:22 +0200742 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200743 rb_erase(n, &td->io_hist_tree);
Jens Axboe01743ee2008-06-02 12:19:19 +0200744 } else if (!flist_empty(&td->io_hist_list)) {
745 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
746 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200747 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200748
Jens Axboe8de8f042007-03-27 10:36:12 +0200749 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200750 io_u->offset = ipo->offset;
751 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100752 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200753
Jens Axboed6aed792009-06-03 08:41:15 +0200754 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200755 int r = td_io_open_file(td, io_u->file);
756
Jens Axboebd6f78b2008-02-01 20:27:52 +0100757 if (r) {
758 dprint(FD_VERIFY, "failed file %s open\n",
759 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200760 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100761 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200762 }
763
764 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200765 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200766 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100767 io_u->xfer_buf = io_u->buf;
768 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200769 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100770 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200771 return 0;
772 }
773
Jens Axboebd6f78b2008-02-01 20:27:52 +0100774 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200775 return 1;
776}
Jens Axboee8462bd2009-07-06 12:59:04 +0200777
778static void *verify_async_thread(void *data)
779{
780 struct thread_data *td = data;
781 struct io_u *io_u;
782 int ret = 0;
783
784 if (td->o.verify_cpumask_set &&
785 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
786 log_err("fio: failed setting verify thread affinity\n");
787 goto done;
788 }
789
790 do {
Jens Axboee53ab272009-07-06 13:43:46 +0200791 FLIST_HEAD(list);
792
Jens Axboee8462bd2009-07-06 12:59:04 +0200793 read_barrier();
794 if (td->verify_thread_exit)
795 break;
796
797 pthread_mutex_lock(&td->io_u_lock);
798
799 while (flist_empty(&td->verify_list) &&
800 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +0200801 ret = pthread_cond_wait(&td->verify_cond,
802 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +0200803 if (ret) {
804 pthread_mutex_unlock(&td->io_u_lock);
805 break;
806 }
807 }
808
Jens Axboee53ab272009-07-06 13:43:46 +0200809 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +0200810 pthread_mutex_unlock(&td->io_u_lock);
811
Jens Axboee53ab272009-07-06 13:43:46 +0200812 if (flist_empty(&list))
813 continue;
814
815 while (!flist_empty(&list)) {
816 io_u = flist_entry(list.next, struct io_u, list);
817 flist_del_init(&io_u->list);
818
Jens Axboed561f2a2009-07-06 13:51:05 +0200819 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +0200820 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +0200821 if (!ret)
822 continue;
823 if (td->o.continue_on_error &&
824 td_non_fatal_error(ret)) {
825 update_error_count(td, ret);
826 td_clear_error(td);
827 ret = 0;
828 }
Jens Axboee53ab272009-07-06 13:43:46 +0200829 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200830 } while (!ret);
831
Jens Axboed561f2a2009-07-06 13:51:05 +0200832 if (ret) {
833 td_verror(td, ret, "async_verify");
834 td->terminate = 1;
835 }
836
Jens Axboee8462bd2009-07-06 12:59:04 +0200837done:
838 pthread_mutex_lock(&td->io_u_lock);
839 td->nr_verify_threads--;
840 pthread_mutex_unlock(&td->io_u_lock);
841
842 pthread_cond_signal(&td->free_cond);
843 return NULL;
844}
845
846int verify_async_init(struct thread_data *td)
847{
848 int i, ret;
849
850 td->verify_thread_exit = 0;
851
852 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
853 for (i = 0; i < td->o.verify_async; i++) {
854 ret = pthread_create(&td->verify_threads[i], NULL,
855 verify_async_thread, td);
856 if (ret) {
857 log_err("fio: async verify creation failed: %s\n",
858 strerror(ret));
859 break;
860 }
861 ret = pthread_detach(td->verify_threads[i]);
862 if (ret) {
863 log_err("fio: async verify thread detach failed: %s\n",
864 strerror(ret));
865 break;
866 }
867 td->nr_verify_threads++;
868 }
869
870 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +0200871 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +0200872 td->verify_thread_exit = 1;
873 write_barrier();
874 pthread_cond_broadcast(&td->verify_cond);
875 return 1;
876 }
877
878 return 0;
879}
880
881void verify_async_exit(struct thread_data *td)
882{
883 td->verify_thread_exit = 1;
884 write_barrier();
885 pthread_cond_broadcast(&td->verify_cond);
886
887 pthread_mutex_lock(&td->io_u_lock);
888
889 while (td->nr_verify_threads)
890 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
891
892 pthread_mutex_unlock(&td->io_u_lock);
893 free(td->verify_threads);
894 td->verify_threads = NULL;
895}