blob: 156292f39ecfd04104d11b313e03ec41d34389f7 [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 Axboee8462bd2009-07-06 12:59:04 +02008#include <pthread.h>
Jens Axboee29d1b72006-10-18 15:43:15 +02009
10#include "fio.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020011#include "verify.h"
Jens Axboee8462bd2009-07-06 12:59:04 +020012#include "smalloc.h"
Jens Axboee29d1b72006-10-18 15:43:15 +020013
Jens Axboeeef6eea2007-07-30 12:27:58 +020014#include "crc/md5.h"
15#include "crc/crc64.h"
16#include "crc/crc32.h"
Jens Axboebac39e02008-06-11 20:46:19 +020017#include "crc/crc32c.h"
Jens Axboeeef6eea2007-07-30 12:27:58 +020018#include "crc/crc16.h"
19#include "crc/crc7.h"
20#include "crc/sha256.h"
21#include "crc/sha512.h"
Jens Axboe7c353ce2009-08-09 22:40:33 +020022#include "crc/sha1.h"
Jens Axboecd14cc12007-07-30 10:59:33 +020023
Jens Axboe90059d62007-07-30 09:33:12 +020024static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020025{
26 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020027 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020028
29 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020030 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020031
32 /*
33 * lrand48_r seems to be broken and only fill the bottom
34 * 32-bits, even on 64-bit archs with 64-bit longs
35 */
36 todo = sizeof(r);
37 if (todo > len)
38 todo = len;
39
40 memcpy(p, &r, todo);
41
42 len -= todo;
43 p += todo;
44 }
45}
46
Jens Axboe90059d62007-07-30 09:33:12 +020047static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
48{
49 switch (td->o.verify_pattern_bytes) {
50 case 0:
Jens Axboebd6f78b2008-02-01 20:27:52 +010051 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
Jens Axboe90059d62007-07-30 09:33:12 +020052 fill_random_bytes(td, p, len);
53 break;
54 case 1:
Jens Axboebd6f78b2008-02-01 20:27:52 +010055 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
Radha Ramachandran0e92f872009-10-27 20:14:27 +010056 memset(p, td->o.verify_pattern[0], len);
Jens Axboe90059d62007-07-30 09:33:12 +020057 break;
Radha Ramachandran0e92f872009-10-27 20:14:27 +010058 default: {
59 unsigned int i = 0, size = 0;
Jens Axboe90059d62007-07-30 09:33:12 +020060 unsigned char *b = p;
61
Jens Axboebd6f78b2008-02-01 20:27:52 +010062 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
63 td->o.verify_pattern_bytes, len);
64
Jens Axboe90059d62007-07-30 09:33:12 +020065 while (i < len) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +010066 size = td->o.verify_pattern_bytes;
67 if (size > (len - i))
68 size = len - i;
69 memcpy(b+i, td->o.verify_pattern, size);
70 i += size;
Jens Axboe90059d62007-07-30 09:33:12 +020071 }
72 break;
73 }
74 }
75}
76
Jens Axboe4764aec2007-08-23 09:03:38 +020077static void memswp(void *buf1, void *buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020078{
Shawn Lewisdee6de72007-08-02 22:17:52 +020079 char swap[200];
80
81 assert(len <= sizeof(swap));
Jens Axboe90059d62007-07-30 09:33:12 +020082
Shawn Lewis546a9142007-07-28 21:11:37 +020083 memcpy(&swap, buf1, len);
84 memcpy(buf1, buf2, len);
85 memcpy(buf2, &swap, len);
86}
87
Jens Axboee29d1b72006-10-18 15:43:15 +020088static void hexdump(void *buffer, int len)
89{
90 unsigned char *p = buffer;
91 int i;
92
93 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +010094 log_info("%02x", p[i]);
95 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020096}
97
Jens Axboed9f2caf2007-07-28 21:22:03 +020098/*
Jens Axboe87677832007-07-30 12:08:14 +020099 * Prepare for seperation of verify_header and checksum header
100 */
Jens Axboe546dfd92007-07-30 12:23:05 +0200101static inline unsigned int __hdr_size(int verify_type)
Jens Axboe87677832007-07-30 12:08:14 +0200102{
Jens Axboe5921e802008-05-30 15:02:38 +0200103 unsigned int len = len;
Jens Axboe87677832007-07-30 12:08:14 +0200104
Jens Axboe546dfd92007-07-30 12:23:05 +0200105 switch (verify_type) {
106 case VERIFY_NONE:
107 case VERIFY_NULL:
108 len = 0;
109 break;
110 case VERIFY_MD5:
111 len = sizeof(struct vhdr_md5);
112 break;
113 case VERIFY_CRC64:
114 len = sizeof(struct vhdr_crc64);
115 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200116 case VERIFY_CRC32C:
Jens Axboe546dfd92007-07-30 12:23:05 +0200117 case VERIFY_CRC32:
Jens Axboe38455912008-08-04 15:35:26 +0200118 case VERIFY_CRC32C_INTEL:
Jens Axboe546dfd92007-07-30 12:23:05 +0200119 len = sizeof(struct vhdr_crc32);
120 break;
121 case VERIFY_CRC16:
122 len = sizeof(struct vhdr_crc16);
123 break;
124 case VERIFY_CRC7:
125 len = sizeof(struct vhdr_crc7);
126 break;
127 case VERIFY_SHA256:
128 len = sizeof(struct vhdr_sha256);
129 break;
130 case VERIFY_SHA512:
131 len = sizeof(struct vhdr_sha512);
132 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200133 case VERIFY_META:
134 len = sizeof(struct vhdr_meta);
135 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200136 case VERIFY_SHA1:
137 len = sizeof(struct vhdr_sha1);
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 Axboe936216f2010-06-18 13:44:11 +0200160 * Verify container, pass info to verify handlers and allow them to
161 * pass info back in case of error
162 */
163struct vcont {
164 /*
165 * Input
166 */
167 struct io_u *io_u;
168 unsigned int hdr_num;
169
170 /*
171 * Output, only valid in case of error
172 */
173 unsigned char *good_crc;
174 unsigned char *bad_crc;
175 unsigned int crc_len;
176};
177
178/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200179 * Return data area 'header_num'
180 */
Jens Axboe936216f2010-06-18 13:44:11 +0200181static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
Jens Axboed9f2caf2007-07-28 21:22:03 +0200182{
Jens Axboe936216f2010-06-18 13:44:11 +0200183 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200184}
185
Shawn Lewis7437ee82007-08-02 21:05:58 +0200186static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe936216f2010-06-18 13:44:11 +0200187 struct vcont *vc)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200188{
189 struct vhdr_meta *vh = hdr_priv(hdr);
Jens Axboe936216f2010-06-18 13:44:11 +0200190 struct io_u *io_u = vc->io_u;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200191
Jens Axboebd6f78b2008-02-01 20:27:52 +0100192 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
193
Jens Axboe936216f2010-06-18 13:44:11 +0200194 if (vh->offset != io_u->offset + vc->hdr_num * td->o.verify_interval) {
Shawn Lewis7437ee82007-08-02 21:05:58 +0200195 log_err("meta: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200196 io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200197 return EILSEQ;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200198 }
199
200 return 0;
201}
202
Jens Axboe936216f2010-06-18 13:44:11 +0200203static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200204{
Jens Axboe936216f2010-06-18 13:44:11 +0200205 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200206 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200207 uint8_t sha512[128];
208 struct sha512_ctx sha512_ctx = {
209 .buf = sha512,
210 };
211
Jens Axboe936216f2010-06-18 13:44:11 +0200212 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100213
Jens Axboecd14cc12007-07-30 10:59:33 +0200214 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200215 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200216
Jens Axboe546dfd92007-07-30 12:23:05 +0200217 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200218 log_err("sha512: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200219 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200220 hexdump(vh->sha512, sizeof(vh->sha512));
Jens Axboecd14cc12007-07-30 10:59:33 +0200221 hexdump(sha512_ctx.buf, sizeof(sha512));
Jens Axboe9fd18962009-05-19 10:35:38 +0200222 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200223 }
224
225 return 0;
226}
227
Jens Axboe936216f2010-06-18 13:44:11 +0200228static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
Jens Axboecd14cc12007-07-30 10:59:33 +0200229{
Jens Axboe936216f2010-06-18 13:44:11 +0200230 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200231 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboebc77f562010-02-23 10:36:21 +0100232 uint8_t sha256[64];
Jens Axboecd14cc12007-07-30 10:59:33 +0200233 struct sha256_ctx sha256_ctx = {
234 .buf = sha256,
235 };
236
Jens Axboe936216f2010-06-18 13:44:11 +0200237 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100238
Jens Axboecd14cc12007-07-30 10:59:33 +0200239 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200240 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200241
Jens Axboe546dfd92007-07-30 12:23:05 +0200242 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
Jens Axboecd14cc12007-07-30 10:59:33 +0200243 log_err("sha256: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200244 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200245 hexdump(vh->sha256, sizeof(vh->sha256));
Jens Axboecd14cc12007-07-30 10:59:33 +0200246 hexdump(sha256_ctx.buf, sizeof(sha256));
Jens Axboe9fd18962009-05-19 10:35:38 +0200247 return EILSEQ;
Jens Axboecd14cc12007-07-30 10:59:33 +0200248 }
249
250 return 0;
251}
252
Jens Axboe936216f2010-06-18 13:44:11 +0200253static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
Jens Axboe7c353ce2009-08-09 22:40:33 +0200254{
Jens Axboe936216f2010-06-18 13:44:11 +0200255 void *p = io_u_verify_off(hdr, vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200256 struct vhdr_sha1 *vh = hdr_priv(hdr);
257 uint32_t sha1[5];
258 struct sha1_ctx sha1_ctx = {
259 .H = sha1,
260 };
261
Jens Axboe936216f2010-06-18 13:44:11 +0200262 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200263
264 sha1_init(&sha1_ctx);
265 sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
266
267 if (memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1))) {
268 log_err("sha1: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200269 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200270 hexdump(vh->sha1, sizeof(vh->sha1));
271 hexdump(sha1_ctx.H, sizeof(sha1));
272 return EILSEQ;
273 }
274
275 return 0;
276}
277
Jens Axboe936216f2010-06-18 13:44:11 +0200278static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200279{
Jens Axboe936216f2010-06-18 13:44:11 +0200280 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200281 struct vhdr_crc7 *vh = hdr_priv(hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200282 unsigned char c;
283
Jens Axboe936216f2010-06-18 13:44:11 +0200284 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100285
Jens Axboe87677832007-07-30 12:08:14 +0200286 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200287
Jens Axboe546dfd92007-07-30 12:23:05 +0200288 if (c != vh->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200289 log_err("crc7: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200290 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200291 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200292 return EILSEQ;
Jens Axboe1e154bd2007-07-27 09:52:40 +0200293 }
294
295 return 0;
296}
297
Jens Axboe936216f2010-06-18 13:44:11 +0200298static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200299{
Jens Axboe936216f2010-06-18 13:44:11 +0200300 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200301 struct vhdr_crc16 *vh = hdr_priv(hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200302 unsigned short c;
303
Jens Axboe936216f2010-06-18 13:44:11 +0200304 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100305
Jens Axboe87677832007-07-30 12:08:14 +0200306 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200307
Jens Axboe546dfd92007-07-30 12:23:05 +0200308 if (c != vh->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200309 log_err("crc16: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200310 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200311 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200312 return EILSEQ;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200313 }
314
315 return 0;
316}
317
Jens Axboe936216f2010-06-18 13:44:11 +0200318static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
Jens Axboed77a7af2007-07-27 15:35:06 +0200319{
Jens Axboe936216f2010-06-18 13:44:11 +0200320 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200321 struct vhdr_crc64 *vh = hdr_priv(hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200322 unsigned long long c;
323
Jens Axboe936216f2010-06-18 13:44:11 +0200324 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100325
Jens Axboe87677832007-07-30 12:08:14 +0200326 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200327
Jens Axboe546dfd92007-07-30 12:23:05 +0200328 if (c != vh->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200329 log_err("crc64: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200330 vc->io_u->offset + vc->hdr_num * hdr->len,
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200331 hdr->len);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100332 log_err("crc64: wanted %llx, got %llx\n",
333 (unsigned long long) vh->crc64, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200334 return EILSEQ;
Jens Axboed77a7af2007-07-27 15:35:06 +0200335 }
336
337 return 0;
338}
339
Jens Axboe936216f2010-06-18 13:44:11 +0200340static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200341{
Jens Axboe936216f2010-06-18 13:44:11 +0200342 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200343 struct vhdr_crc32 *vh = hdr_priv(hdr);
344 uint32_t c;
Jens Axboee29d1b72006-10-18 15:43:15 +0200345
Jens Axboe936216f2010-06-18 13:44:11 +0200346 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100347
Jens Axboe87677832007-07-30 12:08:14 +0200348 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200349
Jens Axboe546dfd92007-07-30 12:23:05 +0200350 if (c != vh->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200351 log_err("crc32: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200352 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200353 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200354 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200355 }
356
357 return 0;
358}
359
Jens Axboe936216f2010-06-18 13:44:11 +0200360static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
Jens Axboebac39e02008-06-11 20:46:19 +0200361{
Jens Axboe936216f2010-06-18 13:44:11 +0200362 void *p = io_u_verify_off(hdr, vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200363 struct vhdr_crc32 *vh = hdr_priv(hdr);
364 uint32_t c;
365
Jens Axboe936216f2010-06-18 13:44:11 +0200366 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200367
Jens Axboe38455912008-08-04 15:35:26 +0200368 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
369 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
370 else
371 c = crc32c(p, hdr->len - hdr_size(hdr));
Jens Axboebac39e02008-06-11 20:46:19 +0200372
373 if (c != vh->crc32) {
374 log_err("crc32c: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200375 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboebac39e02008-06-11 20:46:19 +0200376 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
Jens Axboe9fd18962009-05-19 10:35:38 +0200377 return EILSEQ;
Jens Axboebac39e02008-06-11 20:46:19 +0200378 }
379
380 return 0;
381}
382
Jens Axboe936216f2010-06-18 13:44:11 +0200383static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
Jens Axboee29d1b72006-10-18 15:43:15 +0200384{
Jens Axboe936216f2010-06-18 13:44:11 +0200385 void *p = io_u_verify_off(hdr, vc);
Jens Axboe546dfd92007-07-30 12:23:05 +0200386 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200387 uint32_t hash[MD5_HASH_WORDS];
388 struct md5_ctx md5_ctx = {
389 .hash = hash,
390 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200391
Jens Axboe936216f2010-06-18 13:44:11 +0200392 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100393
Jens Axboe61f821f2007-07-30 10:18:06 +0200394 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200395 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200396
Jens Axboe546dfd92007-07-30 12:23:05 +0200397 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200398 log_err("md5: verify failed at %llu/%u\n",
Jens Axboe936216f2010-06-18 13:44:11 +0200399 vc->io_u->offset + vc->hdr_num * hdr->len, hdr->len);
Jens Axboe546dfd92007-07-30 12:23:05 +0200400 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200401 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboe9fd18962009-05-19 10:35:38 +0200402 return EILSEQ;
Jens Axboee29d1b72006-10-18 15:43:15 +0200403 }
404
405 return 0;
406}
407
Jens Axboe3f199b02007-08-09 10:16:31 +0200408static unsigned int hweight8(unsigned int w)
409{
410 unsigned int res = w - ((w >> 1) & 0x55);
411
412 res = (res & 0x33) + ((res >> 2) & 0x33);
413 return (res + (res >> 4)) & 0x0F;
414}
415
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100416int verify_io_u_pattern(char *pattern, unsigned long pattern_size,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100417 char *buf, unsigned int len, unsigned int mod)
Shawn Lewisa944e332007-08-02 22:18:29 +0200418{
419 unsigned int i;
Shawn Lewisa944e332007-08-02 22:18:29 +0200420
421 for (i = 0; i < len; i++) {
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100422 if (buf[i] != pattern[mod]) {
Jens Axboe3f199b02007-08-09 10:16:31 +0200423 unsigned int bits;
424
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100425 bits = hweight8(buf[i] ^ pattern[mod]);
Jens Axboe3f199b02007-08-09 10:16:31 +0200426 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100427 buf[i], pattern[mod], bits);
Jens Axboe3f199b02007-08-09 10:16:31 +0200428 log_err("fio: bad pattern block offset %u\n", i);
Jens Axboe9fd18962009-05-19 10:35:38 +0200429 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200430 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200431 mod++;
432 if (mod == pattern_size)
433 mod = 0;
434 }
435
436 return 0;
437}
438
Jens Axboee8462bd2009-07-06 12:59:04 +0200439/*
440 * Push IO verification to a separate thread
441 */
442int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
443{
444 if (io_u->file)
445 put_file_log(td, io_u->file);
446
447 io_u->file = NULL;
448
449 pthread_mutex_lock(&td->io_u_lock);
Radha Ramachandran0c412142009-11-03 21:45:31 +0100450
451 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
452 td->cur_depth--;
453 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
454 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200455 flist_del(&io_u->list);
456 flist_add_tail(&io_u->list, &td->verify_list);
Jens Axboe2ecc1b52009-11-04 20:58:09 +0100457 io_u->flags |= IO_U_F_FREE_DEF;
Jens Axboee8462bd2009-07-06 12:59:04 +0200458 pthread_mutex_unlock(&td->io_u_lock);
459
460 pthread_cond_signal(&td->verify_cond);
Jens Axboee8462bd2009-07-06 12:59:04 +0200461 return 0;
462}
463
Jens Axboe36690c92007-03-26 10:23:34 +0200464int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200465{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200466 struct verify_header *hdr;
Shawn Lewisa944e332007-08-02 22:18:29 +0200467 unsigned int hdr_size, hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200468 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200469 int ret;
470
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200471 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200472 return 0;
473
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200474 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200475 if (td->o.verify_interval)
476 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200477
Jens Axboea12a3b42007-08-09 10:20:54 +0200478 ret = 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100479 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
480 p += hdr_inc, hdr_num++) {
Jens Axboe936216f2010-06-18 13:44:11 +0200481 struct vcont vc;
482
Jens Axboea12a3b42007-08-09 10:20:54 +0200483 if (ret && td->o.verify_fatal) {
484 td->terminate = 1;
485 break;
486 }
Shawn Lewisa944e332007-08-02 22:18:29 +0200487 hdr_size = __hdr_size(td->o.verify);
Jens Axboea59e1702007-07-30 08:53:27 +0200488 if (td->o.verify_offset)
Shawn Lewisa944e332007-08-02 22:18:29 +0200489 memswp(p, p + td->o.verify_offset, hdr_size);
Jens Axboe95646102007-07-28 21:17:50 +0200490 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200491
Jens Axboe936216f2010-06-18 13:44:11 +0200492 vc.io_u = io_u;
493 vc.hdr_num = hdr_num;
494
Jens Axboe3f199b02007-08-09 10:16:31 +0200495 if (hdr->fio_magic != FIO_HDR_MAGIC) {
Radha Ramachandranb7a3e662009-11-12 08:42:43 +0100496 log_err("Bad verify header %x at %llu\n",
497 hdr->fio_magic,
498 io_u->offset + hdr_num * hdr->len);
Jens Axboe9fd18962009-05-19 10:35:38 +0200499 return EILSEQ;
Jens Axboe3f199b02007-08-09 10:16:31 +0200500 }
501
Shawn Lewise28218f2008-01-16 11:01:33 +0100502 if (td->o.verify_pattern_bytes) {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100503 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
504 io_u, hdr->len);
Shawn Lewise28218f2008-01-16 11:01:33 +0100505 ret = verify_io_u_pattern(td->o.verify_pattern,
Radha Ramachandran0e92f872009-10-27 20:14:27 +0100506 td->o.verify_pattern_bytes,
507 p + hdr_size,
508 hdr_inc - hdr_size,
509 hdr_size % td->o.verify_pattern_bytes);
Radha Ramachandrane92d3d72009-07-27 12:27:55 +0200510 /*
511 * Also verify the meta data, if applicable
512 */
513 if (hdr->verify_type == VERIFY_META)
Jens Axboe936216f2010-06-18 13:44:11 +0200514 ret |= verify_io_u_meta(hdr, td, &vc);
Radha Ramachandrane92d3d72009-07-27 12:27:55 +0200515
Shawn Lewise28218f2008-01-16 11:01:33 +0100516 if (ret)
517 log_err("fio: verify failed at %llu/%u\n",
518 io_u->offset + hdr_num * hdr->len,
519 hdr->len);
520 continue;
521 }
522
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200523 switch (hdr->verify_type) {
524 case VERIFY_MD5:
Jens Axboe936216f2010-06-18 13:44:11 +0200525 ret = verify_io_u_md5(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200526 break;
527 case VERIFY_CRC64:
Jens Axboe936216f2010-06-18 13:44:11 +0200528 ret = verify_io_u_crc64(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200529 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200530 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200531 case VERIFY_CRC32C_INTEL:
Jens Axboe936216f2010-06-18 13:44:11 +0200532 ret = verify_io_u_crc32c(hdr, &vc);
Jens Axboebac39e02008-06-11 20:46:19 +0200533 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200534 case VERIFY_CRC32:
Jens Axboe936216f2010-06-18 13:44:11 +0200535 ret = verify_io_u_crc32(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200536 break;
537 case VERIFY_CRC16:
Jens Axboe936216f2010-06-18 13:44:11 +0200538 ret = verify_io_u_crc16(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200539 break;
540 case VERIFY_CRC7:
Jens Axboe936216f2010-06-18 13:44:11 +0200541 ret = verify_io_u_crc7(hdr, &vc);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200542 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200543 case VERIFY_SHA256:
Jens Axboe936216f2010-06-18 13:44:11 +0200544 ret = verify_io_u_sha256(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200545 break;
546 case VERIFY_SHA512:
Jens Axboe936216f2010-06-18 13:44:11 +0200547 ret = verify_io_u_sha512(hdr, &vc);
Jens Axboecd14cc12007-07-30 10:59:33 +0200548 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200549 case VERIFY_META:
Jens Axboe936216f2010-06-18 13:44:11 +0200550 ret = verify_io_u_meta(hdr, td, &vc);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200551 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200552 case VERIFY_SHA1:
Jens Axboe936216f2010-06-18 13:44:11 +0200553 ret = verify_io_u_sha1(hdr, &vc);
Jens Axboe7c353ce2009-08-09 22:40:33 +0200554 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200555 default:
556 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboed16d4e02007-09-06 16:16:44 +0200557 ret = EINVAL;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200558 }
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200559 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100560
Jens Axboea12a3b42007-08-09 10:20:54 +0200561 return ret;
Jens Axboee29d1b72006-10-18 15:43:15 +0200562}
563
Shawn Lewis7437ee82007-08-02 21:05:58 +0200564static void fill_meta(struct verify_header *hdr, struct thread_data *td,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100565 struct io_u *io_u, unsigned int header_num)
Shawn Lewis7437ee82007-08-02 21:05:58 +0200566{
567 struct vhdr_meta *vh = hdr_priv(hdr);
568
569 vh->thread = td->thread_number;
570
571 vh->time_sec = io_u->start_time.tv_sec;
572 vh->time_usec = io_u->start_time.tv_usec;
573
574 vh->numberio = td->io_issues[DDIR_WRITE];
575
576 vh->offset = io_u->offset + header_num * td->o.verify_interval;
577}
578
Jens Axboecd14cc12007-07-30 10:59:33 +0200579static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
580{
Jens Axboe546dfd92007-07-30 12:23:05 +0200581 struct vhdr_sha512 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200582 struct sha512_ctx sha512_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200583 .buf = vh->sha512,
Jens Axboecd14cc12007-07-30 10:59:33 +0200584 };
585
586 sha512_init(&sha512_ctx);
587 sha512_update(&sha512_ctx, p, len);
588}
589
590static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
591{
Jens Axboe546dfd92007-07-30 12:23:05 +0200592 struct vhdr_sha256 *vh = hdr_priv(hdr);
Jens Axboecd14cc12007-07-30 10:59:33 +0200593 struct sha256_ctx sha256_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200594 .buf = vh->sha256,
Jens Axboecd14cc12007-07-30 10:59:33 +0200595 };
596
597 sha256_init(&sha256_ctx);
598 sha256_update(&sha256_ctx, p, len);
599}
600
Jens Axboe7c353ce2009-08-09 22:40:33 +0200601static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
602{
603 struct vhdr_sha1 *vh = hdr_priv(hdr);
604 struct sha1_ctx sha1_ctx = {
605 .H = vh->sha1,
606 };
607
608 sha1_init(&sha1_ctx);
609 sha1_update(&sha1_ctx, p, len);
610}
611
Jens Axboe1e154bd2007-07-27 09:52:40 +0200612static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
613{
Jens Axboe546dfd92007-07-30 12:23:05 +0200614 struct vhdr_crc7 *vh = hdr_priv(hdr);
615
616 vh->crc7 = crc7(p, len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200617}
618
Jens Axboe969f7ed2007-07-27 09:07:17 +0200619static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
620{
Jens Axboe546dfd92007-07-30 12:23:05 +0200621 struct vhdr_crc16 *vh = hdr_priv(hdr);
622
623 vh->crc16 = crc16(p, len);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200624}
625
Jens Axboee29d1b72006-10-18 15:43:15 +0200626static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
627{
Jens Axboe546dfd92007-07-30 12:23:05 +0200628 struct vhdr_crc32 *vh = hdr_priv(hdr);
629
630 vh->crc32 = crc32(p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200631}
632
Jens Axboebac39e02008-06-11 20:46:19 +0200633static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
634{
635 struct vhdr_crc32 *vh = hdr_priv(hdr);
636
Jens Axboe38455912008-08-04 15:35:26 +0200637 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
638 vh->crc32 = crc32c_intel(p, len);
639 else
640 vh->crc32 = crc32c(p, len);
Jens Axboebac39e02008-06-11 20:46:19 +0200641}
642
Jens Axboed77a7af2007-07-27 15:35:06 +0200643static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
644{
Jens Axboe546dfd92007-07-30 12:23:05 +0200645 struct vhdr_crc64 *vh = hdr_priv(hdr);
646
647 vh->crc64 = crc64(p, len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200648}
649
Jens Axboee29d1b72006-10-18 15:43:15 +0200650static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
651{
Jens Axboe546dfd92007-07-30 12:23:05 +0200652 struct vhdr_md5 *vh = hdr_priv(hdr);
Jens Axboe8c432322007-07-27 13:16:24 +0200653 struct md5_ctx md5_ctx = {
Jens Axboe546dfd92007-07-30 12:23:05 +0200654 .hash = (uint32_t *) vh->md5_digest,
Jens Axboe8c432322007-07-27 13:16:24 +0200655 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200656
Jens Axboe61f821f2007-07-30 10:18:06 +0200657 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200658 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200659}
660
661/*
662 * fill body of io_u->buf with random data and add a header with the
663 * crc32 or md5 sum of that data.
664 */
665void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
666{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200667 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200668 void *p = io_u->buf, *data;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200669 unsigned int hdr_inc, data_len, header_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200670
Jens Axboe9cc3d152007-03-26 10:32:30 +0200671 if (td->o.verify == VERIFY_NULL)
672 return;
673
Jens Axboe90059d62007-07-30 09:33:12 +0200674 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200675
Shawn Lewis546a9142007-07-28 21:11:37 +0200676 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200677 if (td->o.verify_interval)
678 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200679
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100680 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboe95646102007-07-28 21:17:50 +0200681 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200682
683 hdr->fio_magic = FIO_HDR_MAGIC;
684 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200685 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200686 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200687
Jens Axboe87677832007-07-30 12:08:14 +0200688 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200689 switch (td->o.verify) {
690 case VERIFY_MD5:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100691 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
692 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200693 fill_md5(hdr, data, data_len);
694 break;
695 case VERIFY_CRC64:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100696 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
697 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200698 fill_crc64(hdr, data, data_len);
699 break;
Jens Axboebac39e02008-06-11 20:46:19 +0200700 case VERIFY_CRC32C:
Jens Axboe38455912008-08-04 15:35:26 +0200701 case VERIFY_CRC32C_INTEL:
Jens Axboebac39e02008-06-11 20:46:19 +0200702 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
703 io_u, hdr->len);
704 fill_crc32c(hdr, data, data_len);
705 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200706 case VERIFY_CRC32:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100707 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
708 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200709 fill_crc32(hdr, data, data_len);
710 break;
711 case VERIFY_CRC16:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100712 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
713 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200714 fill_crc16(hdr, data, data_len);
715 break;
716 case VERIFY_CRC7:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100717 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
718 io_u, hdr->len);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200719 fill_crc7(hdr, data, data_len);
720 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200721 case VERIFY_SHA256:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100722 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
723 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200724 fill_sha256(hdr, data, data_len);
725 break;
726 case VERIFY_SHA512:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100727 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
728 io_u, hdr->len);
Jens Axboecd14cc12007-07-30 10:59:33 +0200729 fill_sha512(hdr, data, data_len);
730 break;
Shawn Lewis7437ee82007-08-02 21:05:58 +0200731 case VERIFY_META:
Jens Axboebd6f78b2008-02-01 20:27:52 +0100732 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
733 io_u, hdr->len);
Shawn Lewis7437ee82007-08-02 21:05:58 +0200734 fill_meta(hdr, td, io_u, header_num);
735 break;
Jens Axboe7c353ce2009-08-09 22:40:33 +0200736 case VERIFY_SHA1:
737 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
738 io_u, hdr->len);
739 fill_sha1(hdr, data, data_len);
740 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200741 default:
742 log_err("fio: bad verify type: %d\n", td->o.verify);
743 assert(0);
744 }
Jens Axboea59e1702007-07-30 08:53:27 +0200745 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200746 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Shawn Lewis7437ee82007-08-02 21:05:58 +0200747 header_num++;
Jens Axboee29d1b72006-10-18 15:43:15 +0200748 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200749}
750
751int get_next_verify(struct thread_data *td, struct io_u *io_u)
752{
Jens Axboe8de8f042007-03-27 10:36:12 +0200753 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200754
Jens Axboed2d7fa52007-02-19 13:21:35 +0100755 /*
756 * this io_u is from a requeue, we already filled the offsets
757 */
758 if (io_u->file)
759 return 0;
760
Jens Axboe8de8f042007-03-27 10:36:12 +0200761 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
762 struct rb_node *n = rb_first(&td->io_hist_tree);
763
Jens Axboe4b878982007-03-26 09:32:22 +0200764 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200765 rb_erase(n, &td->io_hist_tree);
Jens Axboe9e144182010-06-15 14:25:36 +0200766 td->io_hist_len--;
Jens Axboe01743ee2008-06-02 12:19:19 +0200767 } else if (!flist_empty(&td->io_hist_list)) {
768 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboe9e144182010-06-15 14:25:36 +0200769 td->io_hist_len--;
Jens Axboe01743ee2008-06-02 12:19:19 +0200770 flist_del(&ipo->list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200771 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200772
Jens Axboe8de8f042007-03-27 10:36:12 +0200773 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200774 io_u->offset = ipo->offset;
775 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100776 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200777
Jens Axboed6aed792009-06-03 08:41:15 +0200778 if (!fio_file_open(io_u->file)) {
Jens Axboe97af62c2007-05-22 11:12:13 +0200779 int r = td_io_open_file(td, io_u->file);
780
Jens Axboebd6f78b2008-02-01 20:27:52 +0100781 if (r) {
782 dprint(FD_VERIFY, "failed file %s open\n",
783 io_u->file->file_name);
Jens Axboe97af62c2007-05-22 11:12:13 +0200784 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100785 }
Jens Axboe97af62c2007-05-22 11:12:13 +0200786 }
787
788 get_file(ipo->file);
Jens Axboed6aed792009-06-03 08:41:15 +0200789 assert(fio_file_open(io_u->file));
Jens Axboee29d1b72006-10-18 15:43:15 +0200790 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100791 io_u->xfer_buf = io_u->buf;
792 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200793 free(ipo);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100794 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200795 return 0;
796 }
797
Jens Axboebd6f78b2008-02-01 20:27:52 +0100798 dprint(FD_VERIFY, "get_next_verify: empty\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200799 return 1;
800}
Jens Axboee8462bd2009-07-06 12:59:04 +0200801
802static void *verify_async_thread(void *data)
803{
804 struct thread_data *td = data;
805 struct io_u *io_u;
806 int ret = 0;
807
808 if (td->o.verify_cpumask_set &&
809 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
810 log_err("fio: failed setting verify thread affinity\n");
811 goto done;
812 }
813
814 do {
Jens Axboee53ab272009-07-06 13:43:46 +0200815 FLIST_HEAD(list);
816
Jens Axboee8462bd2009-07-06 12:59:04 +0200817 read_barrier();
818 if (td->verify_thread_exit)
819 break;
820
821 pthread_mutex_lock(&td->io_u_lock);
822
823 while (flist_empty(&td->verify_list) &&
824 !td->verify_thread_exit) {
Jens Axboeb36e2982009-07-06 14:12:52 +0200825 ret = pthread_cond_wait(&td->verify_cond,
826 &td->io_u_lock);
Jens Axboee8462bd2009-07-06 12:59:04 +0200827 if (ret) {
828 pthread_mutex_unlock(&td->io_u_lock);
829 break;
830 }
831 }
832
Jens Axboee53ab272009-07-06 13:43:46 +0200833 flist_splice_init(&td->verify_list, &list);
Jens Axboee8462bd2009-07-06 12:59:04 +0200834 pthread_mutex_unlock(&td->io_u_lock);
835
Jens Axboee53ab272009-07-06 13:43:46 +0200836 if (flist_empty(&list))
837 continue;
838
839 while (!flist_empty(&list)) {
840 io_u = flist_entry(list.next, struct io_u, list);
841 flist_del_init(&io_u->list);
842
Jens Axboed561f2a2009-07-06 13:51:05 +0200843 ret = verify_io_u(td, io_u);
Jens Axboee53ab272009-07-06 13:43:46 +0200844 put_io_u(td, io_u);
Jens Axboed561f2a2009-07-06 13:51:05 +0200845 if (!ret)
846 continue;
847 if (td->o.continue_on_error &&
848 td_non_fatal_error(ret)) {
849 update_error_count(td, ret);
850 td_clear_error(td);
851 ret = 0;
852 }
Jens Axboee53ab272009-07-06 13:43:46 +0200853 }
Jens Axboee8462bd2009-07-06 12:59:04 +0200854 } while (!ret);
855
Jens Axboed561f2a2009-07-06 13:51:05 +0200856 if (ret) {
857 td_verror(td, ret, "async_verify");
858 td->terminate = 1;
859 }
860
Jens Axboee8462bd2009-07-06 12:59:04 +0200861done:
862 pthread_mutex_lock(&td->io_u_lock);
863 td->nr_verify_threads--;
864 pthread_mutex_unlock(&td->io_u_lock);
865
866 pthread_cond_signal(&td->free_cond);
867 return NULL;
868}
869
870int verify_async_init(struct thread_data *td)
871{
872 int i, ret;
873
874 td->verify_thread_exit = 0;
875
876 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
877 for (i = 0; i < td->o.verify_async; i++) {
878 ret = pthread_create(&td->verify_threads[i], NULL,
879 verify_async_thread, td);
880 if (ret) {
881 log_err("fio: async verify creation failed: %s\n",
882 strerror(ret));
883 break;
884 }
885 ret = pthread_detach(td->verify_threads[i]);
886 if (ret) {
887 log_err("fio: async verify thread detach failed: %s\n",
888 strerror(ret));
889 break;
890 }
891 td->nr_verify_threads++;
892 }
893
894 if (i != td->o.verify_async) {
Jens Axboee40823b2009-07-06 14:28:21 +0200895 log_err("fio: only %d verify threads started, exiting\n", i);
Jens Axboee8462bd2009-07-06 12:59:04 +0200896 td->verify_thread_exit = 1;
897 write_barrier();
898 pthread_cond_broadcast(&td->verify_cond);
899 return 1;
900 }
901
902 return 0;
903}
904
905void verify_async_exit(struct thread_data *td)
906{
907 td->verify_thread_exit = 1;
908 write_barrier();
909 pthread_cond_broadcast(&td->verify_cond);
910
911 pthread_mutex_lock(&td->io_u_lock);
912
913 while (td->nr_verify_threads)
914 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
915
916 pthread_mutex_unlock(&td->io_u_lock);
917 free(td->verify_threads);
918 td->verify_threads = NULL;
919}