blob: a0e26c9f97cdfc7f00902c86a740acca553501b1 [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;
136 default:
137 log_err("fio: unknown verify header!\n");
138 assert(0);
139 }
140
141 return len + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200142}
143
144static inline unsigned int hdr_size(struct verify_header *hdr)
145{
Jens Axboe546dfd92007-07-30 12:23:05 +0200146 return __hdr_size(hdr->verify_type);
147}
148
149static void *hdr_priv(struct verify_header *hdr)
150{
151 void *priv = hdr;
152
153 return priv + sizeof(struct verify_header);
Jens Axboe87677832007-07-30 12:08:14 +0200154}
155
156/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200157 * Return data area 'header_num'
158 */
159static inline void *io_u_verify_off(struct verify_header *hdr,
160 struct io_u *io_u,
161 unsigned char header_num)
162{
Jens Axboe87677832007-07-30 12:08:14 +0200163 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200164}
165
Jens Axboecd14cc12007-07-30 10:59:33 +0200166static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
167 unsigned int header_num)
168{
169 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200170 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200171 uint8_t sha512[128];
172 struct sha512_ctx sha512_ctx = {
173 .buf = sha512,
174 };
175
176 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200177 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200178
Jens Axboe546dfd92007-07-30 12:23:05 +0200179 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200180 log_err("sha512: verify failed at %llu/%u\n",
181 io_u->offset + header_num * hdr->len,
182 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200183 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200184 hexdump(sha512_ctx.buf, sizeof(sha512));
185 return 1;
186 }
187
188 return 0;
189}
190
191static int verify_io_u_sha256(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_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200196 uint8_t sha256[128];
197 struct sha256_ctx sha256_ctx = {
198 .buf = sha256,
199 };
200
201 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200202 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200203
Jens Axboe546dfd92007-07-30 12:23:05 +0200204 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200205 log_err("sha256: verify failed at %llu/%u\n",
206 io_u->offset + header_num * hdr->len,
207 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200208 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200209 hexdump(sha256_ctx.buf, sizeof(sha256));
210 return 1;
211 }
212
213 return 0;
214}
215
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200216static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
217 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200218{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200219 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200220 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200221 unsigned char c;
222
Jens Axboe87677832007-07-30 12:08:14 +0200223 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200224
Jens Axboe546dfd92007-07-30 12:23:05 +0200225 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200226 log_err("crc7: verify failed at %llu/%u\n",
227 io_u->offset + header_num * hdr->len,
228 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200229 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200230 return 1;
231 }
232
233 return 0;
234}
235
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200236static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
237 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200238{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200239 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200240 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200241 unsigned short c;
242
Jens Axboe87677832007-07-30 12:08:14 +0200243 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200244
Jens Axboe546dfd92007-07-30 12:23:05 +0200245 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200246 log_err("crc16: verify failed at %llu/%u\n",
247 io_u->offset + header_num * hdr->len,
248 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200249 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200250 return 1;
251 }
252
253 return 0;
254}
255
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200256static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
257 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200258{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200259 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200260 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200261 unsigned long long c;
262
Jens Axboe87677832007-07-30 12:08:14 +0200263 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200264
Jens Axboe546dfd92007-07-30 12:23:05 +0200265 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200266 log_err("crc64: verify failed at %llu/%u\n",
267 io_u->offset + header_num * hdr->len,
268 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200269 log_err("crc64: wanted %llx, got %llx\n", (unsigned long long) vh->crc64, c);
Jens Axboed77a7af2007-07-27 15:35:06 +0200270 return 1;
271 }
272
273 return 0;
274}
275
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200276static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
277 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200278{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200279 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200280 struct vhdr_crc32 *vh = hdr_priv(hdr);
281 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200282
Jens Axboe87677832007-07-30 12:08:14 +0200283 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200284
Jens Axboe546dfd92007-07-30 12:23:05 +0200285 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200286 log_err("crc32: verify failed at %llu/%u\n",
287 io_u->offset + header_num * hdr->len,
288 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200289 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboee29d1b72006-10-18 15:43:15 +0200290 return 1;
291 }
292
293 return 0;
294}
295
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200296static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
297 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200298{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200299 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe546dfd92007-07-30 12:23:05 +0200300 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200301 uint32_t hash[MD5_HASH_WORDS];
302 struct md5_ctx md5_ctx = {
303 .hash = hash,
304 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200305
Jens Axboe61f821f2007-07-30 10:18:06 +0200306 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200307 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200308
Jens Axboe546dfd92007-07-30 12:23:05 +0200309 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200310 log_err("md5: verify failed at %llu/%u\n",
311 io_u->offset + header_num * hdr->len,
312 hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200313 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200314 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboee29d1b72006-10-18 15:43:15 +0200315 return 1;
316 }
317
318 return 0;
319}
320
Jens Axboe36690c92007-03-26 10:23:34 +0200321int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200322{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200323 struct verify_header *hdr;
324 unsigned int hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200325 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200326 int ret;
327
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200328 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200329 return 0;
330
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200331 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200332 if (td->o.verify_interval)
333 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200334
Jens Axboe95646102007-07-28 21:17:50 +0200335 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboea59e1702007-07-30 08:53:27 +0200336 if (td->o.verify_offset)
Jens Axboe546dfd92007-07-30 12:23:05 +0200337 memswp(p, p + td->o.verify_offset, __hdr_size(td->o.verify));
Shawn Lewis546a9142007-07-28 21:11:37 +0200338
Jens Axboe95646102007-07-28 21:17:50 +0200339 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200340
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200341 if (hdr->fio_magic != FIO_HDR_MAGIC) {
342 log_err("Bad verify header %x\n", hdr->fio_magic);
343 return EIO;
344 }
345
346 switch (hdr->verify_type) {
347 case VERIFY_MD5:
348 ret = verify_io_u_md5(hdr, io_u, hdr_num);
349 break;
350 case VERIFY_CRC64:
351 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
352 break;
353 case VERIFY_CRC32:
354 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
355 break;
356 case VERIFY_CRC16:
357 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
358 break;
359 case VERIFY_CRC7:
360 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
361 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200362 case VERIFY_SHA256:
363 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
364 break;
365 case VERIFY_SHA512:
366 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
367 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200368 default:
369 log_err("Bad verify type %u\n", hdr->verify_type);
370 ret = 1;
371 }
372 hdr_num++;
373 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100374
375 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200376}
377
Jens Axboecd14cc12007-07-30 10:59:33 +0200378static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
379{
Jens Axboe546dfd92007-07-30 12:23:05 +0200380 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200381 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200382 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200383 };
384
385 sha512_init(&sha512_ctx);
386 sha512_update(&sha512_ctx, p, len);
387}
388
389static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
390{
Jens Axboe546dfd92007-07-30 12:23:05 +0200391 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200392 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200393 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200394 };
395
396 sha256_init(&sha256_ctx);
397 sha256_update(&sha256_ctx, p, len);
398}
399
Jens Axboe1e154bd2007-07-27 09:52:40 +0200400static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
401{
Jens Axboe546dfd92007-07-30 12:23:05 +0200402 struct vhdr_crc7 *vh = hdr_priv(hdr);
403
404 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200405}
406
Jens Axboe969f7ed2007-07-27 09:07:17 +0200407static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
408{
Jens Axboe546dfd92007-07-30 12:23:05 +0200409 struct vhdr_crc16 *vh = hdr_priv(hdr);
410
411 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200412}
413
Jens Axboee29d1b72006-10-18 15:43:15 +0200414static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
415{
Jens Axboe546dfd92007-07-30 12:23:05 +0200416 struct vhdr_crc32 *vh = hdr_priv(hdr);
417
418 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200419}
420
Jens Axboed77a7af2007-07-27 15:35:06 +0200421static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
422{
Jens Axboe546dfd92007-07-30 12:23:05 +0200423 struct vhdr_crc64 *vh = hdr_priv(hdr);
424
425 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200426}
427
Jens Axboee29d1b72006-10-18 15:43:15 +0200428static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
429{
Jens Axboe546dfd92007-07-30 12:23:05 +0200430 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200431 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200432 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200433 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200434
Jens Axboe61f821f2007-07-30 10:18:06 +0200435 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200436 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200437}
438
439/*
440 * fill body of io_u->buf with random data and add a header with the
441 * crc32 or md5 sum of that data.
442 */
443void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
444{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200445 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200446 void *p = io_u->buf, *data;
Shawn Lewis546a9142007-07-28 21:11:37 +0200447 unsigned int hdr_inc, data_len;
Jens Axboee29d1b72006-10-18 15:43:15 +0200448
Jens Axboe9cc3d152007-03-26 10:32:30 +0200449 if (td->o.verify == VERIFY_NULL)
450 return;
451
Jens Axboe90059d62007-07-30 09:33:12 +0200452 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200453
Shawn Lewis546a9142007-07-28 21:11:37 +0200454 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200455 if (td->o.verify_interval)
456 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200457
Jens Axboe95646102007-07-28 21:17:50 +0200458 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
459 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200460
461 hdr->fio_magic = FIO_HDR_MAGIC;
462 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200463 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200464 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200465
Jens Axboe87677832007-07-30 12:08:14 +0200466 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200467 switch (td->o.verify) {
468 case VERIFY_MD5:
469 fill_md5(hdr, data, data_len);
470 break;
471 case VERIFY_CRC64:
472 fill_crc64(hdr, data, data_len);
473 break;
474 case VERIFY_CRC32:
475 fill_crc32(hdr, data, data_len);
476 break;
477 case VERIFY_CRC16:
478 fill_crc16(hdr, data, data_len);
479 break;
480 case VERIFY_CRC7:
481 fill_crc7(hdr, data, data_len);
482 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200483 case VERIFY_SHA256:
484 fill_sha256(hdr, data, data_len);
485 break;
486 case VERIFY_SHA512:
487 fill_sha512(hdr, data, data_len);
488 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200489 default:
490 log_err("fio: bad verify type: %d\n", td->o.verify);
491 assert(0);
492 }
Jens Axboea59e1702007-07-30 08:53:27 +0200493 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200494 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200495 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200496}
497
498int get_next_verify(struct thread_data *td, struct io_u *io_u)
499{
Jens Axboe8de8f042007-03-27 10:36:12 +0200500 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200501
Jens Axboed2d7fa52007-02-19 13:21:35 +0100502 /*
503 * this io_u is from a requeue, we already filled the offsets
504 */
505 if (io_u->file)
506 return 0;
507
Jens Axboe8de8f042007-03-27 10:36:12 +0200508 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
509 struct rb_node *n = rb_first(&td->io_hist_tree);
510
Jens Axboe4b878982007-03-26 09:32:22 +0200511 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200512 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200513 } else if (!list_empty(&td->io_hist_list)) {
514 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
515 list_del(&ipo->list);
516 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200517
Jens Axboe8de8f042007-03-27 10:36:12 +0200518 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200519 io_u->offset = ipo->offset;
520 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100521 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200522
523 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
524 int r = td_io_open_file(td, io_u->file);
525
526 if (r)
527 return 1;
528 }
529
530 get_file(ipo->file);
531 assert(io_u->file->flags & FIO_FILE_OPEN);
Jens Axboee29d1b72006-10-18 15:43:15 +0200532 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100533 io_u->xfer_buf = io_u->buf;
534 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200535 free(ipo);
536 return 0;
537 }
538
539 return 1;
540}