blob: 0bb348ff996995750cc797eca653f5567160e980 [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"
14#include "crc/crc16.h"
15#include "crc/crc7.h"
16#include "crc/sha256.h"
17#include "crc/sha512.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020018
Jens Axboe90059d62007-07-30 09:33:12 +020019static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020020{
21 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020022 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020023
24 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020025 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020026
27 /*
28 * lrand48_r seems to be broken and only fill the bottom
29 * 32-bits, even on 64-bit archs with 64-bit longs
30 */
31 todo = sizeof(r);
32 if (todo > len)
33 todo = len;
34
35 memcpy(p, &r, todo);
36
37 len -= todo;
38 p += todo;
39 }
40}
41
Jens Axboe90059d62007-07-30 09:33:12 +020042static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
43{
44 switch (td->o.verify_pattern_bytes) {
45 case 0:
Jens Axboebd6f78b2008-02-01 20:27:52 +010046 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020047 fill_random_bytes(td, p, len);
48 break;
49 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010050 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020051 memset(p, td->o.verify_pattern, len);
52 break;
53 case 2:
54 case 3:
55 case 4: {
56 unsigned int pattern = td->o.verify_pattern;
57 unsigned int i = 0;
58 unsigned char c1, c2, c3, c4;
59 unsigned char *b = p;
60
Jens Axboebd6f78b2008-02-01 20:27:52 +010061 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
62 td->o.verify_pattern_bytes, len);
63
Jens Axboe90059d62007-07-30 09:33:12 +020064 c1 = pattern & 0xff;
65 pattern >>= 8;
66 c2 = pattern & 0xff;
67 pattern >>= 8;
68 c3 = pattern & 0xff;
69 pattern >>= 8;
70 c4 = pattern & 0xff;
71
72 while (i < len) {
73 b[i++] = c1;
74 if (i == len)
75 break;
76 b[i++] = c2;
77 if (td->o.verify_pattern_bytes == 2 || i == len)
78 continue;
79 b[i++] = c3;
80 if (td->o.verify_pattern_bytes == 3 || i == len)
81 continue;
82 b[i++] = c4;
83 }
84 break;
85 }
86 }
87}
88
Jens Axboe4764aec2007-08-23 09:03:38 +020089static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020090{
Shawn Lewisdee6de72007-08-02 22:17:52 +020091 char swap[200];
92
93 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020094
Shawn Lewis546a9142007-07-28 21:11:37 +020095 memcpy(&swap, buf1, len);
96 memcpy(buf1, buf2, len);
97 memcpy(buf2, &swap, len);
98}
99
Jens Axboee29d1b72006-10-18 15:43:15 +0200100static void hexdump(void *buffer, int len)
101{
102 unsigned char *p = buffer;
103 int i;
104
105 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +0100106 log_info("%02x", p[i]);
107 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200108}
109
Jens Axboed9f2caf2007-07-28 21:22:03 +0200110/*
Jens Axboe87677832007-07-30 12:08:14 +0200111 * Prepare for seperation of verify_header and checksum header
112 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200113static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200114{
Jens Axboe546dfd92007-07-30 12:23:05 +0200115 unsigned int len;
Jens Axboe87677832007-07-30 12:08:14 +0200116
Jens Axboe546dfd92007-07-30 12:23:05 +0200117 switch (verify_type) {
118 case VERIFY_NONE:
119 case VERIFY_NULL:
120 len = 0;
121 break;
122 case VERIFY_MD5:
123 len = sizeof(struct vhdr_md5);
124 break;
125 case VERIFY_CRC64:
126 len = sizeof(struct vhdr_crc64);
127 break;
128 case VERIFY_CRC32:
129 len = sizeof(struct vhdr_crc32);
130 break;
131 case VERIFY_CRC16:
132 len = sizeof(struct vhdr_crc16);
133 break;
134 case VERIFY_CRC7:
135 len = sizeof(struct vhdr_crc7);
136 break;
137 case VERIFY_SHA256:
138 len = sizeof(struct vhdr_sha256);
139 break;
140 case VERIFY_SHA512:
141 len = sizeof(struct vhdr_sha512);
142 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200143 case VERIFY_META:
144 len = sizeof(struct vhdr_meta);
145 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200146 default:
147 log_err("fio: unknown verify header!\n");
148 assert(0);
149 }
150
151 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200152}
153
154static inline unsigned int hdr_size(struct verify_header *hdr)
155{
Jens Axboe546dfd92007-07-30 12:23:05 +0200156 return __hdr_size(hdr->verify_type);
157}
158
159static void *hdr_priv(struct verify_header *hdr)
160{
161 void *priv = hdr;
162
163 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200164}
165
166/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200167 * Return data area 'header_num'
168 */
169static inline void *io_u_verify_off(struct verify_header *hdr,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100170 struct io_u *io_u, unsigned char header_num)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200171{
Jens Axboe87677832007-07-30 12:08:14 +0200172 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200173}
174
Shawn Lewis7437ee82007-08-02 21:05:58 +0200175static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100176 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200177{
178 struct vhdr_meta *vh = hdr_priv(hdr);
179
Jens Axboebd6f78b2008-02-01 20:27:52 +0100180 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
181
Shawn Lewis7437ee82007-08-02 21:05:58 +0200182 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
183 log_err("meta: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100184 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboed16d4e02007-09-06 16:16:44 +0200185 return EIO;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200186 }
187
188 return 0;
189}
190
Jens Axboecd14cc12007-07-30 10:59:33 +0200191static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
192 unsigned int header_num)
193{
194 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200195 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200196 uint8_t sha512[128];
197 struct sha512_ctx sha512_ctx = {
198 .buf = sha512,
199 };
200
Jens Axboebd6f78b2008-02-01 20:27:52 +0100201 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
202
Jens Axboecd14cc12007-07-30 10:59:33 +0200203 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200204 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200205
Jens Axboe546dfd92007-07-30 12:23:05 +0200206 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200207 log_err("sha512: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100208 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200209 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200210 hexdump(sha512_ctx.buf, sizeof(sha512));
Jens Axboed16d4e02007-09-06 16:16:44 +0200211 return EIO;
Jens Axboecd14cc12007-07-30 10:59:33 +0200212 }
213
214 return 0;
215}
216
217static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
218 unsigned int header_num)
219{
220 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200221 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200222 uint8_t sha256[128];
223 struct sha256_ctx sha256_ctx = {
224 .buf = sha256,
225 };
226
Jens Axboebd6f78b2008-02-01 20:27:52 +0100227 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
228
Jens Axboecd14cc12007-07-30 10:59:33 +0200229 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200230 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200231
Jens Axboe546dfd92007-07-30 12:23:05 +0200232 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200233 log_err("sha256: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100234 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200235 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200236 hexdump(sha256_ctx.buf, sizeof(sha256));
Jens Axboed16d4e02007-09-06 16:16:44 +0200237 return EIO;
Jens Axboecd14cc12007-07-30 10:59:33 +0200238 }
239
240 return 0;
241}
242
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200243static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100244 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200245{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200246 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200247 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200248 unsigned char c;
249
Jens Axboebd6f78b2008-02-01 20:27:52 +0100250 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
251
Jens Axboe87677832007-07-30 12:08:14 +0200252 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200253
Jens Axboe546dfd92007-07-30 12:23:05 +0200254 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200255 log_err("crc7: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100256 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200257 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboed16d4e02007-09-06 16:16:44 +0200258 return EIO;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200259 }
260
261 return 0;
262}
263
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200264static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100265 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200266{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200267 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200268 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200269 unsigned short c;
270
Jens Axboebd6f78b2008-02-01 20:27:52 +0100271 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
272
Jens Axboe87677832007-07-30 12:08:14 +0200273 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200274
Jens Axboe546dfd92007-07-30 12:23:05 +0200275 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200276 log_err("crc16: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100277 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200278 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboed16d4e02007-09-06 16:16:44 +0200279 return EIO;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200280 }
281
282 return 0;
283}
284
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200285static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100286 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200287{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200288 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200289 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200290 unsigned long long c;
291
Jens Axboebd6f78b2008-02-01 20:27:52 +0100292 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
293
Jens Axboe87677832007-07-30 12:08:14 +0200294 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200295
Jens Axboe546dfd92007-07-30 12:23:05 +0200296 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200297 log_err("crc64: verify failed at %llu/%u\n",
298 io_u->offset + header_num * hdr->len,
299 hdr->len);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100300 log_err("crc64: wanted %llx, got %llx\n",
301 (unsigned long long) vh->crc64, c);
Jens Axboed16d4e02007-09-06 16:16:44 +0200302 return EIO;
Jens Axboed77a7af2007-07-27 15:35:06 +0200303 }
304
305 return 0;
306}
307
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200308static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100309 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200310{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200311 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200312 struct vhdr_crc32 *vh = hdr_priv(hdr);
313 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200314
Jens Axboebd6f78b2008-02-01 20:27:52 +0100315 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
316
Jens Axboe87677832007-07-30 12:08:14 +0200317 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200318
Jens Axboe546dfd92007-07-30 12:23:05 +0200319 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200320 log_err("crc32: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100321 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200322 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboed16d4e02007-09-06 16:16:44 +0200323 return EIO;
Jens Axboee29d1b72006-10-18 15:43:15 +0200324 }
325
326 return 0;
327}
328
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200329static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100330 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200331{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200332 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200333 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200334 uint32_t hash[MD5_HASH_WORDS];
335 struct md5_ctx md5_ctx = {
336 .hash = hash,
337 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200338
Jens Axboebd6f78b2008-02-01 20:27:52 +0100339 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
340
Jens Axboe61f821f2007-07-30 10:18:06 +0200341 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200342 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200343
Jens Axboe546dfd92007-07-30 12:23:05 +0200344 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200345 log_err("md5: verify failed at %llu/%u\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100346 io_u->offset + header_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200347 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200348 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboed16d4e02007-09-06 16:16:44 +0200349 return EIO;
Jens Axboee29d1b72006-10-18 15:43:15 +0200350 }
351
352 return 0;
353}
354
Jens Axboe3f199b02007-08-09 10:16:31 +0200355static unsigned int hweight8(unsigned int w)
356{
357 unsigned int res = w - ((w >> 1) & 0x55);
358
359 res = (res & 0x33) + ((res >> 2) & 0x33);
360 return (res + (res >> 4)) & 0x0F;
361}
362
Shawn Lewisa944e332007-08-02 22:18:29 +0200363int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100364 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200365{
366 unsigned int i;
367 char split_pattern[4];
368
369 for (i = 0; i < 4; i++) {
370 split_pattern[i] = pattern & 0xff;
371 pattern >>= 8;
372 }
373
374 for (i = 0; i < len; i++) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200375 if (buf[i] != split_pattern[mod]) {
376 unsigned int bits;
377
378 bits = hweight8(buf[i] ^ split_pattern[mod]);
379 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
380 buf[i], split_pattern[mod], bits);
381 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboed16d4e02007-09-06 16:16:44 +0200382 return EIO;
Jens Axboe3f199b02007-08-09 10:16:31 +0200383 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200384 mod++;
385 if (mod == pattern_size)
386 mod = 0;
387 }
388
389 return 0;
390}
391
Jens Axboe36690c92007-03-26 10:23:34 +0200392int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200393{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200394 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200395 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200396 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200397 int ret;
398
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200399 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200400 return 0;
401
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200402 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200403 if (td->o.verify_interval)
404 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200405
Jens Axboea12a3b42007-08-09 10:20:54 +0200406 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100407 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
408 p += hdr_inc, hdr_num++) {
Jens Axboea12a3b42007-08-09 10:20:54 +0200409 if (ret && td->o.verify_fatal) {
410 td->terminate = 1;
411 break;
412 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200413 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200414 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200415 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200416 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200417
Jens Axboe3f199b02007-08-09 10:16:31 +0200418 if (hdr->fio_magic != FIO_HDR_MAGIC) {
419 log_err("Bad verify header %x\n", hdr->fio_magic);
420 return EIO;
421 }
422
Shawn Lewise28218f2008-01-16 11:01:33 +0100423 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100424 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
425 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100426 ret = verify_io_u_pattern(td->o.verify_pattern,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100427 td->o.verify_pattern_bytes,
428 p + hdr_size,
429 hdr_inc - hdr_size,
430 hdr_size % 4);
Shawn Lewise28218f2008-01-16 11:01:33 +0100431 if (ret)
432 log_err("fio: verify failed at %llu/%u\n",
433 io_u->offset + hdr_num * hdr->len,
434 hdr->len);
435 continue;
436 }
437
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200438 switch (hdr->verify_type) {
439 case VERIFY_MD5:
440 ret = verify_io_u_md5(hdr, io_u, hdr_num);
441 break;
442 case VERIFY_CRC64:
443 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
444 break;
445 case VERIFY_CRC32:
446 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
447 break;
448 case VERIFY_CRC16:
449 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
450 break;
451 case VERIFY_CRC7:
452 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
453 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200454 case VERIFY_SHA256:
455 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
456 break;
457 case VERIFY_SHA512:
458 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
459 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200460 case VERIFY_META:
461 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
462 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200463 default:
464 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200465 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200466 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200467 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100468
Jens Axboea12a3b42007-08-09 10:20:54 +0200469 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200470}
471
Shawn Lewis7437ee82007-08-02 21:05:58 +0200472static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100473 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200474{
475 struct vhdr_meta *vh = hdr_priv(hdr);
476
477 vh->thread = td->thread_number;
478
479 vh->time_sec = io_u->start_time.tv_sec;
480 vh->time_usec = io_u->start_time.tv_usec;
481
482 vh->numberio = td->io_issues[DDIR_WRITE];
483
484 vh->offset = io_u->offset + header_num * td->o.verify_interval;
485}
486
Jens Axboecd14cc12007-07-30 10:59:33 +0200487static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
488{
Jens Axboe546dfd92007-07-30 12:23:05 +0200489 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200490 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200491 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200492 };
493
494 sha512_init(&sha512_ctx);
495 sha512_update(&sha512_ctx, p, len);
496}
497
498static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
499{
Jens Axboe546dfd92007-07-30 12:23:05 +0200500 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200501 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200502 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200503 };
504
505 sha256_init(&sha256_ctx);
506 sha256_update(&sha256_ctx, p, len);
507}
508
Jens Axboe1e154bd2007-07-27 09:52:40 +0200509static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
510{
Jens Axboe546dfd92007-07-30 12:23:05 +0200511 struct vhdr_crc7 *vh = hdr_priv(hdr);
512
513 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200514}
515
Jens Axboe969f7ed2007-07-27 09:07:17 +0200516static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
517{
Jens Axboe546dfd92007-07-30 12:23:05 +0200518 struct vhdr_crc16 *vh = hdr_priv(hdr);
519
520 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200521}
522
Jens Axboee29d1b72006-10-18 15:43:15 +0200523static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
524{
Jens Axboe546dfd92007-07-30 12:23:05 +0200525 struct vhdr_crc32 *vh = hdr_priv(hdr);
526
527 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200528}
529
Jens Axboed77a7af2007-07-27 15:35:06 +0200530static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
531{
Jens Axboe546dfd92007-07-30 12:23:05 +0200532 struct vhdr_crc64 *vh = hdr_priv(hdr);
533
534 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200535}
536
Jens Axboee29d1b72006-10-18 15:43:15 +0200537static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
538{
Jens Axboe546dfd92007-07-30 12:23:05 +0200539 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200540 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200541 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200542 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200543
Jens Axboe61f821f2007-07-30 10:18:06 +0200544 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200545 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200546}
547
548/*
549 * fill body of io_u->buf with random data and add a header with the
550 * crc32 or md5 sum of that data.
551 */
552void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
553{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200554 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200555 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200556 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200557
Jens Axboe9cc3d152007-03-26 10:32:30 +0200558 if (td->o.verify == VERIFY_NULL)
559 return;
560
Jens Axboe90059d62007-07-30 09:33:12 +0200561 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200562
Shawn Lewis546a9142007-07-28 21:11:37 +0200563 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200564 if (td->o.verify_interval)
565 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200566
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100567 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200568 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200569
570 hdr->fio_magic = FIO_HDR_MAGIC;
571 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200572 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200573 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200574
Jens Axboe87677832007-07-30 12:08:14 +0200575 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200576 switch (td->o.verify) {
577 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100578 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
579 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200580 fill_md5(hdr, data, data_len);
581 break;
582 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100583 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
584 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200585 fill_crc64(hdr, data, data_len);
586 break;
587 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100588 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
589 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200590 fill_crc32(hdr, data, data_len);
591 break;
592 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100593 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
594 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200595 fill_crc16(hdr, data, data_len);
596 break;
597 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100598 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
599 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200600 fill_crc7(hdr, data, data_len);
601 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200602 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100603 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
604 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200605 fill_sha256(hdr, data, data_len);
606 break;
607 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100608 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
609 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200610 fill_sha512(hdr, data, data_len);
611 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200612 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100613 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
614 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200615 fill_meta(hdr, td, io_u, header_num);
616 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200617 default:
618 log_err("fio: bad verify type: %d\n", td->o.verify);
619 assert(0);
620 }
Jens Axboea59e1702007-07-30 08:53:27 +0200621 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200622 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200623 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200624 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200625}
626
627int get_next_verify(struct thread_data *td, struct io_u *io_u)
628{
Jens Axboe8de8f042007-03-27 10:36:12 +0200629 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200630
Jens Axboed2d7fa52007-02-19 13:21:35 +0100631 /*
632 * this io_u is from a requeue, we already filled the offsets
633 */
634 if (io_u->file)
635 return 0;
636
Jens Axboe8de8f042007-03-27 10:36:12 +0200637 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
638 struct rb_node *n = rb_first(&td->io_hist_tree);
639
Jens Axboe4b878982007-03-26 09:32:22 +0200640 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200641 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200642 } else if (!list_empty(&td->io_hist_list)) {
643 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
644 list_del(&ipo->list);
645 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200646
Jens Axboe8de8f042007-03-27 10:36:12 +0200647 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200648 io_u->offset = ipo->offset;
649 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100650 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200651
652 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
653 int r = td_io_open_file(td, io_u->file);
654
Jens Axboebd6f78b2008-02-01 20:27:52 +0100655 if (r) {
656 dprint(FD_VERIFY, "failed file %s open\n",
657 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200658 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100659 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200660 }
661
662 get_file(ipo->file);
663 assert(io_u->file->flags & FIO_FILE_OPEN);
Jens Axboee29d1b72006-10-18 15:43:15 +0200664 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100665 io_u->xfer_buf = io_u->buf;
666 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200667 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100668 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200669 return 0;
670 }
671
Jens Axboebd6f78b2008-02-01 20:27:52 +0100672 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200673 return 1;
674}