blob: 6de0b884112c0b78aa6fbc7d7c25d7867826e59c [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 Axboee29d1b72006-10-18 15:43:15 +02008
9#include "fio.h"
Jens Axboee29d1b72006-10-18 15:43:15 +020010
Jens Axboeeef6eea2007-07-30 12:27:58 +020011#include "crc/md5.h"
12#include "crc/crc64.h"
13#include "crc/crc32.h"
Jens Axboebac39e02008-06-11 20:46:19 +020014#include "crc/crc32c.h"
Jens Axboeeef6eea2007-07-30 12:27:58 +020015#include "crc/crc16.h"
16#include "crc/crc7.h"
17#include "crc/sha256.h"
18#include "crc/sha512.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020019
Jens Axboe90059d62007-07-30 09:33:12 +020020static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020021{
22 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020023 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020024
25 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020026 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020027
28 /*
29 * lrand48_r seems to be broken and only fill the bottom
30 * 32-bits, even on 64-bit archs with 64-bit longs
31 */
32 todo = sizeof(r);
33 if (todo > len)
34 todo = len;
35
36 memcpy(p, &r, todo);
37
38 len -= todo;
39 p += todo;
40 }
41}
42
Jens Axboe90059d62007-07-30 09:33:12 +020043static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
44{
45 switch (td->o.verify_pattern_bytes) {
46 case 0:
Jens Axboebd6f78b2008-02-01 20:27:52 +010047 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020048 fill_random_bytes(td, p, len);
49 break;
50 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010051 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020052 memset(p, td->o.verify_pattern, len);
53 break;
54 case 2:
55 case 3:
56 case 4: {
57 unsigned int pattern = td->o.verify_pattern;
58 unsigned int i = 0;
59 unsigned char c1, c2, c3, c4;
60 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 c1 = pattern & 0xff;
66 pattern >>= 8;
67 c2 = pattern & 0xff;
68 pattern >>= 8;
69 c3 = pattern & 0xff;
70 pattern >>= 8;
71 c4 = pattern & 0xff;
72
73 while (i < len) {
74 b[i++] = c1;
75 if (i == len)
76 break;
77 b[i++] = c2;
78 if (td->o.verify_pattern_bytes == 2 || i == len)
79 continue;
80 b[i++] = c3;
81 if (td->o.verify_pattern_bytes == 3 || i == len)
82 continue;
83 b[i++] = c4;
84 }
85 break;
86 }
87 }
88}
89
Jens Axboe4764aec2007-08-23 09:03:38 +020090static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020091{
Shawn Lewisdee6de72007-08-02 22:17:52 +020092 char swap[200];
93
94 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020095
Shawn Lewis546a9142007-07-28 21:11:37 +020096 memcpy(&swap, buf1, len);
97 memcpy(buf1, buf2, len);
98 memcpy(buf2, &swap, len);
99}
100
Jens Axboee29d1b72006-10-18 15:43:15 +0200101static void hexdump(void *buffer, int len)
102{
103 unsigned char *p = buffer;
104 int i;
105
106 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +0100107 log_info("%02x", p[i]);
108 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200109}
110
Jens Axboed9f2caf2007-07-28 21:22:03 +0200111/*
Jens Axboe87677832007-07-30 12:08:14 +0200112 * Prepare for seperation of verify_header and checksum header
113 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200114static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200115{
Jens Axboe5921e802008-05-30 15:02:38 +0200116 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +0200117
Jens Axboe546dfd92007-07-30 12:23:05 +0200118 switch (verify_type) {
119 case VERIFY_NONE:
120 case VERIFY_NULL:
121 len = 0;
122 break;
123 case VERIFY_MD5:
124 len = sizeof(struct vhdr_md5);
125 break;
126 case VERIFY_CRC64:
127 len = sizeof(struct vhdr_crc64);
128 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200129 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200130 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200131 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200132 len = sizeof(struct vhdr_crc32);
133 break;
134 case VERIFY_CRC16:
135 len = sizeof(struct vhdr_crc16);
136 break;
137 case VERIFY_CRC7:
138 len = sizeof(struct vhdr_crc7);
139 break;
140 case VERIFY_SHA256:
141 len = sizeof(struct vhdr_sha256);
142 break;
143 case VERIFY_SHA512:
144 len = sizeof(struct vhdr_sha512);
145 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200146 case VERIFY_META:
147 len = sizeof(struct vhdr_meta);
148 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200149 default:
150 log_err("fio: unknown verify header!\n");
151 assert(0);
152 }
153
154 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200155}
156
157static inline unsigned int hdr_size(struct verify_header *hdr)
158{
Jens Axboe546dfd92007-07-30 12:23:05 +0200159 return __hdr_size(hdr->verify_type);
160}
161
162static void *hdr_priv(struct verify_header *hdr)
163{
164 void *priv = hdr;
165
166 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200167}
168
169/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200170 * Return data area 'header_num'
171 */
172static inline void *io_u_verify_off(struct verify_header *hdr,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100173 struct io_u *io_u, unsigned char header_num)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200174{
Jens Axboe87677832007-07-30 12:08:14 +0200175 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200176}
177
Shawn Lewis7437ee82007-08-02 21:05:58 +0200178static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100179 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200180{
181 struct vhdr_meta *vh = hdr_priv(hdr);
182
Jens Axboebd6f78b2008-02-01 20:27:52 +0100183 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
184
Shawn Lewis7437ee82007-08-02 21:05:58 +0200185 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
186 log_err("meta: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100187 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200188 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200189 }
190
191 return 0;
192}
193
Jens Axboecd14cc12007-07-30 10:59:33 +0200194static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
195 unsigned int header_num)
196{
197 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200198 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200199 uint8_t sha512[128];
200 struct sha512_ctx sha512_ctx = {
201 .buf = sha512,
202 };
203
Jens Axboebd6f78b2008-02-01 20:27:52 +0100204 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
205
Jens Axboecd14cc12007-07-30 10:59:33 +0200206 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200207 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200208
Jens Axboe546dfd92007-07-30 12:23:05 +0200209 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200210 log_err("sha512: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100211 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200212 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200213 hexdump(sha512_ctx.buf, sizeof(sha512));
Jens Axboe9fd18962009-05-19 10:35:38 +0200214 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200215 }
216
217 return 0;
218}
219
220static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
221 unsigned int header_num)
222{
223 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200224 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200225 uint8_t sha256[128];
226 struct sha256_ctx sha256_ctx = {
227 .buf = sha256,
228 };
229
Jens Axboebd6f78b2008-02-01 20:27:52 +0100230 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
231
Jens Axboecd14cc12007-07-30 10:59:33 +0200232 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200233 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200234
Jens Axboe546dfd92007-07-30 12:23:05 +0200235 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200236 log_err("sha256: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100237 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200238 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200239 hexdump(sha256_ctx.buf, sizeof(sha256));
Jens Axboe9fd18962009-05-19 10:35:38 +0200240 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200241 }
242
243 return 0;
244}
245
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200246static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100247 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200248{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200249 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200250 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200251 unsigned char c;
252
Jens Axboebd6f78b2008-02-01 20:27:52 +0100253 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
254
Jens Axboe87677832007-07-30 12:08:14 +0200255 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200256
Jens Axboe546dfd92007-07-30 12:23:05 +0200257 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200258 log_err("crc7: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100259 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200260 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200261 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200262 }
263
264 return 0;
265}
266
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200267static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100268 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200269{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200270 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200271 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200272 unsigned short c;
273
Jens Axboebd6f78b2008-02-01 20:27:52 +0100274 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
275
Jens Axboe87677832007-07-30 12:08:14 +0200276 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200277
Jens Axboe546dfd92007-07-30 12:23:05 +0200278 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200279 log_err("crc16: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100280 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200281 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200282 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200283 }
284
285 return 0;
286}
287
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200288static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100289 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200290{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200291 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200292 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200293 unsigned long long c;
294
Jens Axboebd6f78b2008-02-01 20:27:52 +0100295 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
296
Jens Axboe87677832007-07-30 12:08:14 +0200297 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200298
Jens Axboe546dfd92007-07-30 12:23:05 +0200299 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200300 log_err("crc64: verify failed at %llu/%u\n",
301 io_u->offset + header_num * hdr->len,
302 hdr->len);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100303 log_err("crc64: wanted %llx, got %llx\n",
304 (unsigned long long) vh->crc64, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200305 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200306 }
307
308 return 0;
309}
310
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200311static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100312 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200313{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200314 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200315 struct vhdr_crc32 *vh = hdr_priv(hdr);
316 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200317
Jens Axboebd6f78b2008-02-01 20:27:52 +0100318 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
319
Jens Axboe87677832007-07-30 12:08:14 +0200320 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200321
Jens Axboe546dfd92007-07-30 12:23:05 +0200322 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200323 log_err("crc32: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100324 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200325 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200326 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200327 }
328
329 return 0;
330}
331
Jens Axboebac39e02008-06-11 20:46:19 +0200332static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
333 unsigned int header_num)
334{
335 void *p = io_u_verify_off(hdr, io_u, header_num);
336 struct vhdr_crc32 *vh = hdr_priv(hdr);
337 uint32_t c;
338
339 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
340
Jens Axboe38455912008-08-04 15:35:26 +0200341 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
342 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
343 else
344 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200345
346 if (c != vh->crc32) {
347 log_err("crc32c: verify failed at %llu/%u\n",
348 io_u->offset + header_num * hdr->len, hdr->len);
349 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200350 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200351 }
352
353 return 0;
354}
355
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200356static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100357 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200358{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200359 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200360 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200361 uint32_t hash[MD5_HASH_WORDS];
362 struct md5_ctx md5_ctx = {
363 .hash = hash,
364 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200365
Jens Axboebd6f78b2008-02-01 20:27:52 +0100366 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
367
Jens Axboe61f821f2007-07-30 10:18:06 +0200368 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200369 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200370
Jens Axboe546dfd92007-07-30 12:23:05 +0200371 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200372 log_err("md5: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100373 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200374 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200375 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboe9fd18962009-05-19 10:35:38 +0200376 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200377 }
378
379 return 0;
380}
381
Jens Axboe3f199b02007-08-09 10:16:31 +0200382static unsigned int hweight8(unsigned int w)
383{
384 unsigned int res = w - ((w >> 1) & 0x55);
385
386 res = (res & 0x33) + ((res >> 2) & 0x33);
387 return (res + (res >> 4)) & 0x0F;
388}
389
Shawn Lewisa944e332007-08-02 22:18:29 +0200390int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100391 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200392{
393 unsigned int i;
394 char split_pattern[4];
395
396 for (i = 0; i < 4; i++) {
397 split_pattern[i] = pattern & 0xff;
398 pattern >>= 8;
399 }
400
401 for (i = 0; i < len; i++) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200402 if (buf[i] != split_pattern[mod]) {
403 unsigned int bits;
404
405 bits = hweight8(buf[i] ^ split_pattern[mod]);
406 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
407 buf[i], split_pattern[mod], bits);
408 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200409 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200410 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200411 mod++;
412 if (mod == pattern_size)
413 mod = 0;
414 }
415
416 return 0;
417}
418
Jens Axboe36690c92007-03-26 10:23:34 +0200419int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200420{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200421 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200422 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200423 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200424 int ret;
425
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200426 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200427 return 0;
428
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200429 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200430 if (td->o.verify_interval)
431 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200432
Jens Axboea12a3b42007-08-09 10:20:54 +0200433 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100434 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
435 p += hdr_inc, hdr_num++) {
Jens Axboea12a3b42007-08-09 10:20:54 +0200436 if (ret && td->o.verify_fatal) {
437 td->terminate = 1;
438 break;
439 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200440 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200441 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200442 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200443 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200444
Jens Axboe3f199b02007-08-09 10:16:31 +0200445 if (hdr->fio_magic != FIO_HDR_MAGIC) {
446 log_err("Bad verify header %x\n", hdr->fio_magic);
Jens Axboe9fd18962009-05-19 10:35:38 +0200447 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200448 }
449
Shawn Lewise28218f2008-01-16 11:01:33 +0100450 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100451 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
452 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100453 ret = verify_io_u_pattern(td->o.verify_pattern,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100454 td->o.verify_pattern_bytes,
455 p + hdr_size,
456 hdr_inc - hdr_size,
457 hdr_size % 4);
Shawn Lewise28218f2008-01-16 11:01:33 +0100458 if (ret)
459 log_err("fio: verify failed at %llu/%u\n",
460 io_u->offset + hdr_num * hdr->len,
461 hdr->len);
462 continue;
463 }
464
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200465 switch (hdr->verify_type) {
466 case VERIFY_MD5:
467 ret = verify_io_u_md5(hdr, io_u, hdr_num);
468 break;
469 case VERIFY_CRC64:
470 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
471 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200472 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200473 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200474 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
475 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200476 case VERIFY_CRC32:
477 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
478 break;
479 case VERIFY_CRC16:
480 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
481 break;
482 case VERIFY_CRC7:
483 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
484 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200485 case VERIFY_SHA256:
486 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
487 break;
488 case VERIFY_SHA512:
489 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
490 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200491 case VERIFY_META:
492 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
493 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200494 default:
495 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200496 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200497 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200498 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100499
Jens Axboea12a3b42007-08-09 10:20:54 +0200500 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200501}
502
Shawn Lewis7437ee82007-08-02 21:05:58 +0200503static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100504 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200505{
506 struct vhdr_meta *vh = hdr_priv(hdr);
507
508 vh->thread = td->thread_number;
509
510 vh->time_sec = io_u->start_time.tv_sec;
511 vh->time_usec = io_u->start_time.tv_usec;
512
513 vh->numberio = td->io_issues[DDIR_WRITE];
514
515 vh->offset = io_u->offset + header_num * td->o.verify_interval;
516}
517
Jens Axboecd14cc12007-07-30 10:59:33 +0200518static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
519{
Jens Axboe546dfd92007-07-30 12:23:05 +0200520 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200521 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200522 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200523 };
524
525 sha512_init(&sha512_ctx);
526 sha512_update(&sha512_ctx, p, len);
527}
528
529static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
530{
Jens Axboe546dfd92007-07-30 12:23:05 +0200531 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200532 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200533 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200534 };
535
536 sha256_init(&sha256_ctx);
537 sha256_update(&sha256_ctx, p, len);
538}
539
Jens Axboe1e154bd2007-07-27 09:52:40 +0200540static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
541{
Jens Axboe546dfd92007-07-30 12:23:05 +0200542 struct vhdr_crc7 *vh = hdr_priv(hdr);
543
544 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200545}
546
Jens Axboe969f7ed2007-07-27 09:07:17 +0200547static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
548{
Jens Axboe546dfd92007-07-30 12:23:05 +0200549 struct vhdr_crc16 *vh = hdr_priv(hdr);
550
551 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200552}
553
Jens Axboee29d1b72006-10-18 15:43:15 +0200554static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
555{
Jens Axboe546dfd92007-07-30 12:23:05 +0200556 struct vhdr_crc32 *vh = hdr_priv(hdr);
557
558 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200559}
560
Jens Axboebac39e02008-06-11 20:46:19 +0200561static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
562{
563 struct vhdr_crc32 *vh = hdr_priv(hdr);
564
Jens Axboe38455912008-08-04 15:35:26 +0200565 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
566 vh->crc32 = crc32c_intel(p, len);
567 else
568 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200569}
570
Jens Axboed77a7af2007-07-27 15:35:06 +0200571static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
572{
Jens Axboe546dfd92007-07-30 12:23:05 +0200573 struct vhdr_crc64 *vh = hdr_priv(hdr);
574
575 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200576}
577
Jens Axboee29d1b72006-10-18 15:43:15 +0200578static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
579{
Jens Axboe546dfd92007-07-30 12:23:05 +0200580 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200581 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200582 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200583 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200584
Jens Axboe61f821f2007-07-30 10:18:06 +0200585 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200586 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200587}
588
589/*
590 * fill body of io_u->buf with random data and add a header with the
591 * crc32 or md5 sum of that data.
592 */
593void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
594{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200595 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200596 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200597 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200598
Jens Axboe9cc3d152007-03-26 10:32:30 +0200599 if (td->o.verify == VERIFY_NULL)
600 return;
601
Jens Axboe90059d62007-07-30 09:33:12 +0200602 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200603
Shawn Lewis546a9142007-07-28 21:11:37 +0200604 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200605 if (td->o.verify_interval)
606 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200607
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100608 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200609 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200610
611 hdr->fio_magic = FIO_HDR_MAGIC;
612 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200613 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200614 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200615
Jens Axboe87677832007-07-30 12:08:14 +0200616 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200617 switch (td->o.verify) {
618 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100619 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
620 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200621 fill_md5(hdr, data, data_len);
622 break;
623 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100624 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
625 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200626 fill_crc64(hdr, data, data_len);
627 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200628 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200629 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200630 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
631 io_u, hdr->len);
632 fill_crc32c(hdr, data, data_len);
633 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200634 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100635 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
636 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200637 fill_crc32(hdr, data, data_len);
638 break;
639 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100640 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
641 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200642 fill_crc16(hdr, data, data_len);
643 break;
644 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100645 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
646 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200647 fill_crc7(hdr, data, data_len);
648 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200649 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100650 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
651 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200652 fill_sha256(hdr, data, data_len);
653 break;
654 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100655 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
656 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200657 fill_sha512(hdr, data, data_len);
658 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200659 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100660 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
661 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200662 fill_meta(hdr, td, io_u, header_num);
663 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200664 default:
665 log_err("fio: bad verify type: %d\n", td->o.verify);
666 assert(0);
667 }
Jens Axboea59e1702007-07-30 08:53:27 +0200668 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200669 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200670 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200671 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200672}
673
674int get_next_verify(struct thread_data *td, struct io_u *io_u)
675{
Jens Axboe8de8f042007-03-27 10:36:12 +0200676 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200677
Jens Axboed2d7fa52007-02-19 13:21:35 +0100678 /*
679 * this io_u is from a requeue, we already filled the offsets
680 */
681 if (io_u->file)
682 return 0;
683
Jens Axboe8de8f042007-03-27 10:36:12 +0200684 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
685 struct rb_node *n = rb_first(&td->io_hist_tree);
686
Jens Axboe4b878982007-03-26 09:32:22 +0200687 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200688 rb_erase(n, &td->io_hist_tree);
Jens Axboe01743ee2008-06-02 12:19:19 +0200689 } else if (!flist_empty(&td->io_hist_list)) {
690 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
691 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200692 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200693
Jens Axboe8de8f042007-03-27 10:36:12 +0200694 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200695 io_u->offset = ipo->offset;
696 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100697 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200698
Jens Axboed6aed792009-06-03 08:41:15 +0200699 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200700 int r = td_io_open_file(td, io_u->file);
701
Jens Axboebd6f78b2008-02-01 20:27:52 +0100702 if (r) {
703 dprint(FD_VERIFY, "failed file %s open\n",
704 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200705 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100706 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200707 }
708
709 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200710 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200711 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100712 io_u->xfer_buf = io_u->buf;
713 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200714 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100715 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200716 return 0;
717 }
718
Jens Axboebd6f78b2008-02-01 20:27:52 +0100719 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200720 return 1;
721}