blob: d9a68d656e490ca2d9a3e0b6a76777895e08f045 [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:
46 fill_random_bytes(td, p, len);
47 break;
48 case 1:
49 memset(p, td->o.verify_pattern, len);
50 break;
51 case 2:
52 case 3:
53 case 4: {
54 unsigned int pattern = td->o.verify_pattern;
55 unsigned int i = 0;
56 unsigned char c1, c2, c3, c4;
57 unsigned char *b = p;
58
59 c1 = pattern & 0xff;
60 pattern >>= 8;
61 c2 = pattern & 0xff;
62 pattern >>= 8;
63 c3 = pattern & 0xff;
64 pattern >>= 8;
65 c4 = pattern & 0xff;
66
67 while (i < len) {
68 b[i++] = c1;
69 if (i == len)
70 break;
71 b[i++] = c2;
72 if (td->o.verify_pattern_bytes == 2 || i == len)
73 continue;
74 b[i++] = c3;
75 if (td->o.verify_pattern_bytes == 3 || i == len)
76 continue;
77 b[i++] = c4;
78 }
79 break;
80 }
81 }
82}
83
84static void memswp(void* buf1, void* buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020085{
86 struct verify_header swap;
Jens Axboe90059d62007-07-30 09:33:12 +020087
Shawn Lewis546a9142007-07-28 21:11:37 +020088 memcpy(&swap, buf1, len);
89 memcpy(buf1, buf2, len);
90 memcpy(buf2, &swap, len);
91}
92
Jens Axboee29d1b72006-10-18 15:43:15 +020093static void hexdump(void *buffer, int len)
94{
95 unsigned char *p = buffer;
96 int i;
97
98 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +010099 log_info("%02x", p[i]);
100 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200101}
102
Jens Axboed9f2caf2007-07-28 21:22:03 +0200103/*
Jens Axboe87677832007-07-30 12:08:14 +0200104 * Prepare for seperation of verify_header and checksum header
105 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200106static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200107{
Jens Axboe546dfd92007-07-30 12:23:05 +0200108 unsigned int len;
Jens Axboe87677832007-07-30 12:08:14 +0200109
Jens Axboe546dfd92007-07-30 12:23:05 +0200110 switch (verify_type) {
111 case VERIFY_NONE:
112 case VERIFY_NULL:
113 len = 0;
114 break;
115 case VERIFY_MD5:
116 len = sizeof(struct vhdr_md5);
117 break;
118 case VERIFY_CRC64:
119 len = sizeof(struct vhdr_crc64);
120 break;
121 case VERIFY_CRC32:
122 len = sizeof(struct vhdr_crc32);
123 break;
124 case VERIFY_CRC16:
125 len = sizeof(struct vhdr_crc16);
126 break;
127 case VERIFY_CRC7:
128 len = sizeof(struct vhdr_crc7);
129 break;
130 case VERIFY_SHA256:
131 len = sizeof(struct vhdr_sha256);
132 break;
133 case VERIFY_SHA512:
134 len = sizeof(struct vhdr_sha512);
135 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200136 case VERIFY_META:
137 len = sizeof(struct vhdr_meta);
138 break;
Jens Axboe546dfd92007-07-30 12:23:05 +0200139 default:
140 log_err("fio: unknown verify header!\n");
141 assert(0);
142 }
143
144 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200145}
146
147static inline unsigned int hdr_size(struct verify_header *hdr)
148{
Jens Axboe546dfd92007-07-30 12:23:05 +0200149 return __hdr_size(hdr->verify_type);
150}
151
152static void *hdr_priv(struct verify_header *hdr)
153{
154 void *priv = hdr;
155
156 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200157}
158
159/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200160 * Return data area 'header_num'
161 */
162static inline void *io_u_verify_off(struct verify_header *hdr,
163 struct io_u *io_u,
164 unsigned char header_num)
165{
Jens Axboe87677832007-07-30 12:08:14 +0200166 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200167}
168
Shawn Lewis7437ee82007-08-02 21:05:58 +0200169static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
170 struct io_u *io_u, unsigned int header_num)
171{
172 struct vhdr_meta *vh = hdr_priv(hdr);
173
174 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
175 log_err("meta: verify failed at %llu/%u\n",
176 io_u->offset + header_num * hdr->len,
177 hdr->len);
178 return 1;
179 }
180
181 return 0;
182}
183
Jens Axboecd14cc12007-07-30 10:59:33 +0200184static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
185 unsigned int header_num)
186{
187 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200188 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200189 uint8_t sha512[128];
190 struct sha512_ctx sha512_ctx = {
191 .buf = sha512,
192 };
193
194 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200195 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200196
Jens Axboe546dfd92007-07-30 12:23:05 +0200197 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200198 log_err("sha512: verify failed at %llu/%u\n",
199 io_u->offset + header_num * hdr->len,
200 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200201 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200202 hexdump(sha512_ctx.buf, sizeof(sha512));
203 return 1;
204 }
205
206 return 0;
207}
208
209static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
210 unsigned int header_num)
211{
212 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200213 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200214 uint8_t sha256[128];
215 struct sha256_ctx sha256_ctx = {
216 .buf = sha256,
217 };
218
219 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200220 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200221
Jens Axboe546dfd92007-07-30 12:23:05 +0200222 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200223 log_err("sha256: verify failed at %llu/%u\n",
224 io_u->offset + header_num * hdr->len,
225 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200226 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200227 hexdump(sha256_ctx.buf, sizeof(sha256));
228 return 1;
229 }
230
231 return 0;
232}
233
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200234static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
235 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200236{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200237 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200238 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200239 unsigned char c;
240
Jens Axboe87677832007-07-30 12:08:14 +0200241 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200242
Jens Axboe546dfd92007-07-30 12:23:05 +0200243 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200244 log_err("crc7: verify failed at %llu/%u\n",
245 io_u->offset + header_num * hdr->len,
246 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200247 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200248 return 1;
249 }
250
251 return 0;
252}
253
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200254static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
255 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200256{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200257 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200258 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200259 unsigned short c;
260
Jens Axboe87677832007-07-30 12:08:14 +0200261 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200262
Jens Axboe546dfd92007-07-30 12:23:05 +0200263 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200264 log_err("crc16: verify failed at %llu/%u\n",
265 io_u->offset + header_num * hdr->len,
266 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200267 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200268 return 1;
269 }
270
271 return 0;
272}
273
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200274static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
275 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200276{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200277 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200278 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200279 unsigned long long c;
280
Jens Axboe87677832007-07-30 12:08:14 +0200281 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200282
Jens Axboe546dfd92007-07-30 12:23:05 +0200283 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200284 log_err("crc64: verify failed at %llu/%u\n",
285 io_u->offset + header_num * hdr->len,
286 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200287 log_err("crc64: wanted %llx, got %llx\n", (unsigned long long) vh->crc64, c);
Jens Axboed77a7af2007-07-27 15:35:06 +0200288 return 1;
289 }
290
291 return 0;
292}
293
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200294static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
295 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200296{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200297 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200298 struct vhdr_crc32 *vh = hdr_priv(hdr);
299 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200300
Jens Axboe87677832007-07-30 12:08:14 +0200301 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200302
Jens Axboe546dfd92007-07-30 12:23:05 +0200303 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200304 log_err("crc32: verify failed at %llu/%u\n",
305 io_u->offset + header_num * hdr->len,
306 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200307 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboee29d1b72006-10-18 15:43:15 +0200308 return 1;
309 }
310
311 return 0;
312}
313
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200314static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
315 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200316{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200317 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200318 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200319 uint32_t hash[MD5_HASH_WORDS];
320 struct md5_ctx md5_ctx = {
321 .hash = hash,
322 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200323
Jens Axboe61f821f2007-07-30 10:18:06 +0200324 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200325 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200326
Jens Axboe546dfd92007-07-30 12:23:05 +0200327 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200328 log_err("md5: verify failed at %llu/%u\n",
329 io_u->offset + header_num * hdr->len,
330 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200331 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200332 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboee29d1b72006-10-18 15:43:15 +0200333 return 1;
334 }
335
336 return 0;
337}
338
Jens Axboe36690c92007-03-26 10:23:34 +0200339int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200340{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200341 struct verify_header *hdr;
342 unsigned int hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200343 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200344 int ret;
345
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200346 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200347 return 0;
348
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200349 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200350 if (td->o.verify_interval)
351 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200352
Jens Axboe95646102007-07-28 21:17:50 +0200353 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboea59e1702007-07-30 08:53:27 +0200354 if (td->o.verify_offset)
Jens Axboe546dfd92007-07-30 12:23:05 +0200355 memswp(p, p + td->o.verify_offset, __hdr_size(td->o.verify));
Shawn Lewis546a9142007-07-28 21:11:37 +0200356
Jens Axboe95646102007-07-28 21:17:50 +0200357 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200358
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200359 if (hdr->fio_magic != FIO_HDR_MAGIC) {
360 log_err("Bad verify header %x\n", hdr->fio_magic);
361 return EIO;
362 }
363
364 switch (hdr->verify_type) {
365 case VERIFY_MD5:
366 ret = verify_io_u_md5(hdr, io_u, hdr_num);
367 break;
368 case VERIFY_CRC64:
369 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
370 break;
371 case VERIFY_CRC32:
372 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
373 break;
374 case VERIFY_CRC16:
375 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
376 break;
377 case VERIFY_CRC7:
378 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
379 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200380 case VERIFY_SHA256:
381 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
382 break;
383 case VERIFY_SHA512:
384 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
385 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200386 case VERIFY_META:
387 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
388 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200389 default:
390 log_err("Bad verify type %u\n", hdr->verify_type);
391 ret = 1;
392 }
393 hdr_num++;
394 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100395
396 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200397}
398
Shawn Lewis7437ee82007-08-02 21:05:58 +0200399static void fill_meta(struct verify_header *hdr, struct thread_data *td,
400 struct io_u *io_u, unsigned int header_num)
401{
402 struct vhdr_meta *vh = hdr_priv(hdr);
403
404 vh->thread = td->thread_number;
405
406 vh->time_sec = io_u->start_time.tv_sec;
407 vh->time_usec = io_u->start_time.tv_usec;
408
409 vh->numberio = td->io_issues[DDIR_WRITE];
410
411 vh->offset = io_u->offset + header_num * td->o.verify_interval;
412}
413
Jens Axboecd14cc12007-07-30 10:59:33 +0200414static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
415{
Jens Axboe546dfd92007-07-30 12:23:05 +0200416 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200417 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200418 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200419 };
420
421 sha512_init(&sha512_ctx);
422 sha512_update(&sha512_ctx, p, len);
423}
424
425static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
426{
Jens Axboe546dfd92007-07-30 12:23:05 +0200427 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200428 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200429 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200430 };
431
432 sha256_init(&sha256_ctx);
433 sha256_update(&sha256_ctx, p, len);
434}
435
Jens Axboe1e154bd2007-07-27 09:52:40 +0200436static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
437{
Jens Axboe546dfd92007-07-30 12:23:05 +0200438 struct vhdr_crc7 *vh = hdr_priv(hdr);
439
440 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200441}
442
Jens Axboe969f7ed2007-07-27 09:07:17 +0200443static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
444{
Jens Axboe546dfd92007-07-30 12:23:05 +0200445 struct vhdr_crc16 *vh = hdr_priv(hdr);
446
447 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200448}
449
Jens Axboee29d1b72006-10-18 15:43:15 +0200450static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
451{
Jens Axboe546dfd92007-07-30 12:23:05 +0200452 struct vhdr_crc32 *vh = hdr_priv(hdr);
453
454 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200455}
456
Jens Axboed77a7af2007-07-27 15:35:06 +0200457static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
458{
Jens Axboe546dfd92007-07-30 12:23:05 +0200459 struct vhdr_crc64 *vh = hdr_priv(hdr);
460
461 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200462}
463
Jens Axboee29d1b72006-10-18 15:43:15 +0200464static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
465{
Jens Axboe546dfd92007-07-30 12:23:05 +0200466 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200467 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200468 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200469 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200470
Jens Axboe61f821f2007-07-30 10:18:06 +0200471 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200472 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200473}
474
475/*
476 * fill body of io_u->buf with random data and add a header with the
477 * crc32 or md5 sum of that data.
478 */
479void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
480{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200481 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200482 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200483 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200484
Jens Axboe9cc3d152007-03-26 10:32:30 +0200485 if (td->o.verify == VERIFY_NULL)
486 return;
487
Jens Axboe90059d62007-07-30 09:33:12 +0200488 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200489
Shawn Lewis546a9142007-07-28 21:11:37 +0200490 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200491 if (td->o.verify_interval)
492 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200493
Jens Axboe95646102007-07-28 21:17:50 +0200494 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
495 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200496
497 hdr->fio_magic = FIO_HDR_MAGIC;
498 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200499 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200500 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200501
Jens Axboe87677832007-07-30 12:08:14 +0200502 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200503 switch (td->o.verify) {
504 case VERIFY_MD5:
505 fill_md5(hdr, data, data_len);
506 break;
507 case VERIFY_CRC64:
508 fill_crc64(hdr, data, data_len);
509 break;
510 case VERIFY_CRC32:
511 fill_crc32(hdr, data, data_len);
512 break;
513 case VERIFY_CRC16:
514 fill_crc16(hdr, data, data_len);
515 break;
516 case VERIFY_CRC7:
517 fill_crc7(hdr, data, data_len);
518 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200519 case VERIFY_SHA256:
520 fill_sha256(hdr, data, data_len);
521 break;
522 case VERIFY_SHA512:
523 fill_sha512(hdr, data, data_len);
524 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200525 case VERIFY_META:
526 fill_meta(hdr, td, io_u, header_num);
527 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200528 default:
529 log_err("fio: bad verify type: %d\n", td->o.verify);
530 assert(0);
531 }
Jens Axboea59e1702007-07-30 08:53:27 +0200532 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200533 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200534 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200535 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200536}
537
538int get_next_verify(struct thread_data *td, struct io_u *io_u)
539{
Jens Axboe8de8f042007-03-27 10:36:12 +0200540 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200541
Jens Axboed2d7fa52007-02-19 13:21:35 +0100542 /*
543 * this io_u is from a requeue, we already filled the offsets
544 */
545 if (io_u->file)
546 return 0;
547
Jens Axboe8de8f042007-03-27 10:36:12 +0200548 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
549 struct rb_node *n = rb_first(&td->io_hist_tree);
550
Jens Axboe4b878982007-03-26 09:32:22 +0200551 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200552 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200553 } else if (!list_empty(&td->io_hist_list)) {
554 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
555 list_del(&ipo->list);
556 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200557
Jens Axboe8de8f042007-03-27 10:36:12 +0200558 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200559 io_u->offset = ipo->offset;
560 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100561 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200562
563 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
564 int r = td_io_open_file(td, io_u->file);
565
566 if (r)
567 return 1;
568 }
569
570 get_file(ipo->file);
571 assert(io_u->file->flags & FIO_FILE_OPEN);
Jens Axboee29d1b72006-10-18 15:43:15 +0200572 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100573 io_u->xfer_buf = io_u->buf;
574 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200575 free(ipo);
576 return 0;
577 }
578
579 return 1;
580}