blob: 2ae74b93c28e7e6a2c7c71165a5dc98a76b876ea [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 Axboe4f5af7b2009-06-03 08:45:40 +020010#include "verify.h"
Jens Axboee29d1b72006-10-18 15:43:15 +020011
Jens Axboeeef6eea2007-07-30 12:27:58 +020012#include "crc/md5.h"
13#include "crc/crc64.h"
14#include "crc/crc32.h"
Jens Axboebac39e02008-06-11 20:46:19 +020015#include "crc/crc32c.h"
Jens Axboeeef6eea2007-07-30 12:27:58 +020016#include "crc/crc16.h"
17#include "crc/crc7.h"
18#include "crc/sha256.h"
19#include "crc/sha512.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020020
Jens Axboe90059d62007-07-30 09:33:12 +020021static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020022{
23 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020024 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020025
26 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020027 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020028
29 /*
30 * lrand48_r seems to be broken and only fill the bottom
31 * 32-bits, even on 64-bit archs with 64-bit longs
32 */
33 todo = sizeof(r);
34 if (todo > len)
35 todo = len;
36
37 memcpy(p, &r, todo);
38
39 len -= todo;
40 p += todo;
41 }
42}
43
Jens Axboe90059d62007-07-30 09:33:12 +020044static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
45{
46 switch (td->o.verify_pattern_bytes) {
47 case 0:
Jens Axboebd6f78b2008-02-01 20:27:52 +010048 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020049 fill_random_bytes(td, p, len);
50 break;
51 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010052 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020053 memset(p, td->o.verify_pattern, len);
54 break;
55 case 2:
56 case 3:
57 case 4: {
58 unsigned int pattern = td->o.verify_pattern;
59 unsigned int i = 0;
60 unsigned char c1, c2, c3, c4;
61 unsigned char *b = p;
62
Jens Axboebd6f78b2008-02-01 20:27:52 +010063 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
64 td->o.verify_pattern_bytes, len);
65
Jens Axboe90059d62007-07-30 09:33:12 +020066 c1 = pattern & 0xff;
67 pattern >>= 8;
68 c2 = pattern & 0xff;
69 pattern >>= 8;
70 c3 = pattern & 0xff;
71 pattern >>= 8;
72 c4 = pattern & 0xff;
73
74 while (i < len) {
75 b[i++] = c1;
76 if (i == len)
77 break;
78 b[i++] = c2;
79 if (td->o.verify_pattern_bytes == 2 || i == len)
80 continue;
81 b[i++] = c3;
82 if (td->o.verify_pattern_bytes == 3 || i == len)
83 continue;
84 b[i++] = c4;
85 }
86 break;
87 }
88 }
89}
90
Jens Axboe4764aec2007-08-23 09:03:38 +020091static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020092{
Shawn Lewisdee6de72007-08-02 22:17:52 +020093 char swap[200];
94
95 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020096
Shawn Lewis546a9142007-07-28 21:11:37 +020097 memcpy(&swap, buf1, len);
98 memcpy(buf1, buf2, len);
99 memcpy(buf2, &swap, len);
100}
101
Jens Axboee29d1b72006-10-18 15:43:15 +0200102static void hexdump(void *buffer, int len)
103{
104 unsigned char *p = buffer;
105 int i;
106
107 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +0100108 log_info("%02x", p[i]);
109 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200110}
111
Jens Axboed9f2caf2007-07-28 21:22:03 +0200112/*
Jens Axboe87677832007-07-30 12:08:14 +0200113 * Prepare for seperation of verify_header and checksum header
114 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200115static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200116{
Jens Axboe5921e802008-05-30 15:02:38 +0200117 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +0200118
Jens Axboe546dfd92007-07-30 12:23:05 +0200119 switch (verify_type) {
120 case VERIFY_NONE:
121 case VERIFY_NULL:
122 len = 0;
123 break;
124 case VERIFY_MD5:
125 len = sizeof(struct vhdr_md5);
126 break;
127 case VERIFY_CRC64:
128 len = sizeof(struct vhdr_crc64);
129 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200130 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200131 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200132 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200133 len = sizeof(struct vhdr_crc32);
134 break;
135 case VERIFY_CRC16:
136 len = sizeof(struct vhdr_crc16);
137 break;
138 case VERIFY_CRC7:
139 len = sizeof(struct vhdr_crc7);
140 break;
141 case VERIFY_SHA256:
142 len = sizeof(struct vhdr_sha256);
143 break;
144 case VERIFY_SHA512:
145 len = sizeof(struct vhdr_sha512);
146 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200147 case VERIFY_META:
148 len = sizeof(struct vhdr_meta);
149 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200150 default:
151 log_err("fio: unknown verify header!\n");
152 assert(0);
153 }
154
155 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200156}
157
158static inline unsigned int hdr_size(struct verify_header *hdr)
159{
Jens Axboe546dfd92007-07-30 12:23:05 +0200160 return __hdr_size(hdr->verify_type);
161}
162
163static void *hdr_priv(struct verify_header *hdr)
164{
165 void *priv = hdr;
166
167 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200168}
169
170/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200171 * Return data area 'header_num'
172 */
173static inline void *io_u_verify_off(struct verify_header *hdr,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100174 struct io_u *io_u, unsigned char header_num)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200175{
Jens Axboe87677832007-07-30 12:08:14 +0200176 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200177}
178
Shawn Lewis7437ee82007-08-02 21:05:58 +0200179static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100180 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200181{
182 struct vhdr_meta *vh = hdr_priv(hdr);
183
Jens Axboebd6f78b2008-02-01 20:27:52 +0100184 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
185
Shawn Lewis7437ee82007-08-02 21:05:58 +0200186 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
187 log_err("meta: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100188 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200189 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200190 }
191
192 return 0;
193}
194
Jens Axboecd14cc12007-07-30 10:59:33 +0200195static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
196 unsigned int header_num)
197{
198 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200199 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200200 uint8_t sha512[128];
201 struct sha512_ctx sha512_ctx = {
202 .buf = sha512,
203 };
204
Jens Axboebd6f78b2008-02-01 20:27:52 +0100205 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
206
Jens Axboecd14cc12007-07-30 10:59:33 +0200207 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200208 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200209
Jens Axboe546dfd92007-07-30 12:23:05 +0200210 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200211 log_err("sha512: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100212 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200213 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200214 hexdump(sha512_ctx.buf, sizeof(sha512));
Jens Axboe9fd18962009-05-19 10:35:38 +0200215 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200216 }
217
218 return 0;
219}
220
221static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
222 unsigned int header_num)
223{
224 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200225 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200226 uint8_t sha256[128];
227 struct sha256_ctx sha256_ctx = {
228 .buf = sha256,
229 };
230
Jens Axboebd6f78b2008-02-01 20:27:52 +0100231 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
232
Jens Axboecd14cc12007-07-30 10:59:33 +0200233 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200234 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200235
Jens Axboe546dfd92007-07-30 12:23:05 +0200236 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200237 log_err("sha256: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100238 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200239 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200240 hexdump(sha256_ctx.buf, sizeof(sha256));
Jens Axboe9fd18962009-05-19 10:35:38 +0200241 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200242 }
243
244 return 0;
245}
246
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200247static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100248 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200249{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200250 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200251 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200252 unsigned char c;
253
Jens Axboebd6f78b2008-02-01 20:27:52 +0100254 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
255
Jens Axboe87677832007-07-30 12:08:14 +0200256 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200257
Jens Axboe546dfd92007-07-30 12:23:05 +0200258 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200259 log_err("crc7: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100260 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200261 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200262 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200263 }
264
265 return 0;
266}
267
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200268static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100269 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200270{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200271 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200272 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200273 unsigned short c;
274
Jens Axboebd6f78b2008-02-01 20:27:52 +0100275 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
276
Jens Axboe87677832007-07-30 12:08:14 +0200277 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200278
Jens Axboe546dfd92007-07-30 12:23:05 +0200279 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200280 log_err("crc16: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100281 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200282 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200283 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200284 }
285
286 return 0;
287}
288
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200289static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100290 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200291{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200292 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200293 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200294 unsigned long long c;
295
Jens Axboebd6f78b2008-02-01 20:27:52 +0100296 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
297
Jens Axboe87677832007-07-30 12:08:14 +0200298 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200299
Jens Axboe546dfd92007-07-30 12:23:05 +0200300 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200301 log_err("crc64: verify failed at %llu/%u\n",
302 io_u->offset + header_num * hdr->len,
303 hdr->len);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100304 log_err("crc64: wanted %llx, got %llx\n",
305 (unsigned long long) vh->crc64, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200306 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200307 }
308
309 return 0;
310}
311
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200312static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100313 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200314{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200315 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200316 struct vhdr_crc32 *vh = hdr_priv(hdr);
317 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200318
Jens Axboebd6f78b2008-02-01 20:27:52 +0100319 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
320
Jens Axboe87677832007-07-30 12:08:14 +0200321 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200322
Jens Axboe546dfd92007-07-30 12:23:05 +0200323 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200324 log_err("crc32: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100325 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200326 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200327 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200328 }
329
330 return 0;
331}
332
Jens Axboebac39e02008-06-11 20:46:19 +0200333static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
334 unsigned int header_num)
335{
336 void *p = io_u_verify_off(hdr, io_u, header_num);
337 struct vhdr_crc32 *vh = hdr_priv(hdr);
338 uint32_t c;
339
340 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
341
Jens Axboe38455912008-08-04 15:35:26 +0200342 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
343 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
344 else
345 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200346
347 if (c != vh->crc32) {
348 log_err("crc32c: verify failed at %llu/%u\n",
349 io_u->offset + header_num * hdr->len, hdr->len);
350 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200351 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200352 }
353
354 return 0;
355}
356
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200357static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100358 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200359{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200360 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200361 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200362 uint32_t hash[MD5_HASH_WORDS];
363 struct md5_ctx md5_ctx = {
364 .hash = hash,
365 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200366
Jens Axboebd6f78b2008-02-01 20:27:52 +0100367 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
368
Jens Axboe61f821f2007-07-30 10:18:06 +0200369 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200370 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200371
Jens Axboe546dfd92007-07-30 12:23:05 +0200372 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200373 log_err("md5: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100374 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200375 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200376 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboe9fd18962009-05-19 10:35:38 +0200377 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200378 }
379
380 return 0;
381}
382
Jens Axboe3f199b02007-08-09 10:16:31 +0200383static unsigned int hweight8(unsigned int w)
384{
385 unsigned int res = w - ((w >> 1) & 0x55);
386
387 res = (res & 0x33) + ((res >> 2) & 0x33);
388 return (res + (res >> 4)) & 0x0F;
389}
390
Shawn Lewisa944e332007-08-02 22:18:29 +0200391int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100392 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200393{
394 unsigned int i;
395 char split_pattern[4];
396
397 for (i = 0; i < 4; i++) {
398 split_pattern[i] = pattern & 0xff;
399 pattern >>= 8;
400 }
401
402 for (i = 0; i < len; i++) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200403 if (buf[i] != split_pattern[mod]) {
404 unsigned int bits;
405
406 bits = hweight8(buf[i] ^ split_pattern[mod]);
407 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
408 buf[i], split_pattern[mod], bits);
409 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200410 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200411 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200412 mod++;
413 if (mod == pattern_size)
414 mod = 0;
415 }
416
417 return 0;
418}
419
Jens Axboe36690c92007-03-26 10:23:34 +0200420int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200421{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200422 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200423 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200424 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200425 int ret;
426
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200427 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200428 return 0;
429
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200430 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200431 if (td->o.verify_interval)
432 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200433
Jens Axboea12a3b42007-08-09 10:20:54 +0200434 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100435 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
436 p += hdr_inc, hdr_num++) {
Jens Axboea12a3b42007-08-09 10:20:54 +0200437 if (ret && td->o.verify_fatal) {
438 td->terminate = 1;
439 break;
440 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200441 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200442 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200443 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200444 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200445
Jens Axboe3f199b02007-08-09 10:16:31 +0200446 if (hdr->fio_magic != FIO_HDR_MAGIC) {
447 log_err("Bad verify header %x\n", hdr->fio_magic);
Jens Axboe9fd18962009-05-19 10:35:38 +0200448 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200449 }
450
Shawn Lewise28218f2008-01-16 11:01:33 +0100451 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100452 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
453 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100454 ret = verify_io_u_pattern(td->o.verify_pattern,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100455 td->o.verify_pattern_bytes,
456 p + hdr_size,
457 hdr_inc - hdr_size,
458 hdr_size % 4);
Shawn Lewise28218f2008-01-16 11:01:33 +0100459 if (ret)
460 log_err("fio: verify failed at %llu/%u\n",
461 io_u->offset + hdr_num * hdr->len,
462 hdr->len);
463 continue;
464 }
465
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200466 switch (hdr->verify_type) {
467 case VERIFY_MD5:
468 ret = verify_io_u_md5(hdr, io_u, hdr_num);
469 break;
470 case VERIFY_CRC64:
471 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
472 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200473 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200474 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200475 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
476 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200477 case VERIFY_CRC32:
478 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
479 break;
480 case VERIFY_CRC16:
481 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
482 break;
483 case VERIFY_CRC7:
484 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
485 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200486 case VERIFY_SHA256:
487 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
488 break;
489 case VERIFY_SHA512:
490 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
491 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200492 case VERIFY_META:
493 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
494 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200495 default:
496 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200497 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200498 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200499 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100500
Jens Axboea12a3b42007-08-09 10:20:54 +0200501 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200502}
503
Shawn Lewis7437ee82007-08-02 21:05:58 +0200504static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100505 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200506{
507 struct vhdr_meta *vh = hdr_priv(hdr);
508
509 vh->thread = td->thread_number;
510
511 vh->time_sec = io_u->start_time.tv_sec;
512 vh->time_usec = io_u->start_time.tv_usec;
513
514 vh->numberio = td->io_issues[DDIR_WRITE];
515
516 vh->offset = io_u->offset + header_num * td->o.verify_interval;
517}
518
Jens Axboecd14cc12007-07-30 10:59:33 +0200519static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
520{
Jens Axboe546dfd92007-07-30 12:23:05 +0200521 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200522 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200523 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200524 };
525
526 sha512_init(&sha512_ctx);
527 sha512_update(&sha512_ctx, p, len);
528}
529
530static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
531{
Jens Axboe546dfd92007-07-30 12:23:05 +0200532 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200533 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200534 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200535 };
536
537 sha256_init(&sha256_ctx);
538 sha256_update(&sha256_ctx, p, len);
539}
540
Jens Axboe1e154bd2007-07-27 09:52:40 +0200541static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
542{
Jens Axboe546dfd92007-07-30 12:23:05 +0200543 struct vhdr_crc7 *vh = hdr_priv(hdr);
544
545 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200546}
547
Jens Axboe969f7ed2007-07-27 09:07:17 +0200548static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
549{
Jens Axboe546dfd92007-07-30 12:23:05 +0200550 struct vhdr_crc16 *vh = hdr_priv(hdr);
551
552 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200553}
554
Jens Axboee29d1b72006-10-18 15:43:15 +0200555static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
556{
Jens Axboe546dfd92007-07-30 12:23:05 +0200557 struct vhdr_crc32 *vh = hdr_priv(hdr);
558
559 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200560}
561
Jens Axboebac39e02008-06-11 20:46:19 +0200562static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
563{
564 struct vhdr_crc32 *vh = hdr_priv(hdr);
565
Jens Axboe38455912008-08-04 15:35:26 +0200566 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
567 vh->crc32 = crc32c_intel(p, len);
568 else
569 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200570}
571
Jens Axboed77a7af2007-07-27 15:35:06 +0200572static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
573{
Jens Axboe546dfd92007-07-30 12:23:05 +0200574 struct vhdr_crc64 *vh = hdr_priv(hdr);
575
576 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200577}
578
Jens Axboee29d1b72006-10-18 15:43:15 +0200579static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
580{
Jens Axboe546dfd92007-07-30 12:23:05 +0200581 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200582 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200583 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200584 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200585
Jens Axboe61f821f2007-07-30 10:18:06 +0200586 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200587 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200588}
589
590/*
591 * fill body of io_u->buf with random data and add a header with the
592 * crc32 or md5 sum of that data.
593 */
594void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
595{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200596 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200597 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200598 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200599
Jens Axboe9cc3d152007-03-26 10:32:30 +0200600 if (td->o.verify == VERIFY_NULL)
601 return;
602
Jens Axboe90059d62007-07-30 09:33:12 +0200603 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200604
Shawn Lewis546a9142007-07-28 21:11:37 +0200605 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200606 if (td->o.verify_interval)
607 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200608
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100609 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200610 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200611
612 hdr->fio_magic = FIO_HDR_MAGIC;
613 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200614 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200615 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200616
Jens Axboe87677832007-07-30 12:08:14 +0200617 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200618 switch (td->o.verify) {
619 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100620 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
621 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200622 fill_md5(hdr, data, data_len);
623 break;
624 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100625 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
626 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200627 fill_crc64(hdr, data, data_len);
628 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200629 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200630 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200631 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
632 io_u, hdr->len);
633 fill_crc32c(hdr, data, data_len);
634 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200635 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100636 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
637 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200638 fill_crc32(hdr, data, data_len);
639 break;
640 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100641 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
642 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200643 fill_crc16(hdr, data, data_len);
644 break;
645 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100646 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
647 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200648 fill_crc7(hdr, data, data_len);
649 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200650 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100651 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
652 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200653 fill_sha256(hdr, data, data_len);
654 break;
655 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100656 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
657 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200658 fill_sha512(hdr, data, data_len);
659 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200660 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100661 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
662 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200663 fill_meta(hdr, td, io_u, header_num);
664 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200665 default:
666 log_err("fio: bad verify type: %d\n", td->o.verify);
667 assert(0);
668 }
Jens Axboea59e1702007-07-30 08:53:27 +0200669 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200670 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200671 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200672 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200673}
674
675int get_next_verify(struct thread_data *td, struct io_u *io_u)
676{
Jens Axboe8de8f042007-03-27 10:36:12 +0200677 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200678
Jens Axboed2d7fa52007-02-19 13:21:35 +0100679 /*
680 * this io_u is from a requeue, we already filled the offsets
681 */
682 if (io_u->file)
683 return 0;
684
Jens Axboe8de8f042007-03-27 10:36:12 +0200685 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
686 struct rb_node *n = rb_first(&td->io_hist_tree);
687
Jens Axboe4b878982007-03-26 09:32:22 +0200688 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200689 rb_erase(n, &td->io_hist_tree);
Jens Axboe01743ee2008-06-02 12:19:19 +0200690 } else if (!flist_empty(&td->io_hist_list)) {
691 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
692 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200693 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200694
Jens Axboe8de8f042007-03-27 10:36:12 +0200695 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200696 io_u->offset = ipo->offset;
697 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100698 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200699
Jens Axboed6aed792009-06-03 08:41:15 +0200700 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200701 int r = td_io_open_file(td, io_u->file);
702
Jens Axboebd6f78b2008-02-01 20:27:52 +0100703 if (r) {
704 dprint(FD_VERIFY, "failed file %s open\n",
705 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200706 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100707 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200708 }
709
710 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200711 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200712 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100713 io_u->xfer_buf = io_u->buf;
714 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200715 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100716 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200717 return 0;
718 }
719
Jens Axboebd6f78b2008-02-01 20:27:52 +0100720 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200721 return 1;
722}