blob: 3606ecea77d611e81cd026a3c7fbb7db954d0663 [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
11static void fill_random_bytes(struct thread_data *td,
Jens Axboe95646102007-07-28 21:17:50 +020012 void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020013{
14 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020015 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020016
17 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020018 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020019
20 /*
21 * lrand48_r seems to be broken and only fill the bottom
22 * 32-bits, even on 64-bit archs with 64-bit longs
23 */
24 todo = sizeof(r);
25 if (todo > len)
26 todo = len;
27
28 memcpy(p, &r, todo);
29
30 len -= todo;
31 p += todo;
32 }
33}
34
Shawn Lewis546a9142007-07-28 21:11:37 +020035void memswp(void* buf1, void* buf2, unsigned int len)
36{
37 struct verify_header swap;
38 memcpy(&swap, buf1, len);
39 memcpy(buf1, buf2, len);
40 memcpy(buf2, &swap, len);
41}
42
Jens Axboee29d1b72006-10-18 15:43:15 +020043static void hexdump(void *buffer, int len)
44{
45 unsigned char *p = buffer;
46 int i;
47
48 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +010049 log_info("%02x", p[i]);
50 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020051}
52
Jens Axboed9f2caf2007-07-28 21:22:03 +020053/*
54 * Return data area 'header_num'
55 */
56static inline void *io_u_verify_off(struct verify_header *hdr,
57 struct io_u *io_u,
58 unsigned char header_num)
59{
60 return io_u->buf + sizeof(*hdr) + header_num * hdr->len;
61}
62
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020063static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
64 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +020065{
Jens Axboed9f2caf2007-07-28 21:22:03 +020066 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe1e154bd2007-07-27 09:52:40 +020067 unsigned char c;
68
Jens Axboe1e154bd2007-07-27 09:52:40 +020069 c = crc7(p, hdr->len - sizeof(*hdr));
70
71 if (c != hdr->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020072 log_err("crc7: verify failed at %llu/%u\n",
73 io_u->offset + header_num * hdr->len,
74 hdr->len);
Jens Axboe1e154bd2007-07-27 09:52:40 +020075 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
76 return 1;
77 }
78
79 return 0;
80}
81
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020082static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
83 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +020084{
Jens Axboed9f2caf2007-07-28 21:22:03 +020085 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe969f7ed2007-07-27 09:07:17 +020086 unsigned short c;
87
Jens Axboe969f7ed2007-07-27 09:07:17 +020088 c = crc16(p, hdr->len - sizeof(*hdr));
89
90 if (c != hdr->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020091 log_err("crc16: verify failed at %llu/%u\n",
92 io_u->offset + header_num * hdr->len,
93 hdr->len);
Jens Axboe1f24ea42007-07-27 09:53:14 +020094 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
Jens Axboe969f7ed2007-07-27 09:07:17 +020095 return 1;
96 }
97
98 return 0;
99}
100
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200101static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
102 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200103{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200104 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboed77a7af2007-07-27 15:35:06 +0200105 unsigned long long c;
106
107 c = crc64(p, hdr->len - sizeof(*hdr));
108
109 if (c != hdr->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200110 log_err("crc64: verify failed at %llu/%u\n",
111 io_u->offset + header_num * hdr->len,
112 hdr->len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200113 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
114 return 1;
115 }
116
117 return 0;
118}
119
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200120static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
121 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200122{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200123 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboee29d1b72006-10-18 15:43:15 +0200124 unsigned long c;
125
Jens Axboee29d1b72006-10-18 15:43:15 +0200126 c = crc32(p, hdr->len - sizeof(*hdr));
127
128 if (c != hdr->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200129 log_err("crc32: verify failed at %llu/%u\n",
130 io_u->offset + header_num * hdr->len,
131 hdr->len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200132 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
133 return 1;
134 }
135
136 return 0;
137}
138
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200139static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
140 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200141{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200142 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe8c432322007-07-27 13:16:24 +0200143 uint32_t hash[MD5_HASH_WORDS];
144 struct md5_ctx md5_ctx = {
145 .hash = hash,
146 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200147
Jens Axboee29d1b72006-10-18 15:43:15 +0200148 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
149
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200150 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
151 log_err("md5: verify failed at %llu/%u\n",
152 io_u->offset + header_num * hdr->len,
153 hdr->len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200154 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200155 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboee29d1b72006-10-18 15:43:15 +0200156 return 1;
157 }
158
159 return 0;
160}
161
Jens Axboe36690c92007-03-26 10:23:34 +0200162int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200163{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200164 struct verify_header *hdr;
165 unsigned int hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200166 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200167 int ret;
168
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200169 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200170 return 0;
171
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200172 hdr_inc = io_u->buflen;
173 if (td->o.header_interval)
174 hdr_inc = td->o.header_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200175
Jens Axboe95646102007-07-28 21:17:50 +0200176 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Shawn Lewis546a9142007-07-28 21:11:37 +0200177 if (td->o.header_offset)
Jens Axboe95646102007-07-28 21:17:50 +0200178 memswp(p, p + td->o.header_offset, sizeof(*hdr));
Shawn Lewis546a9142007-07-28 21:11:37 +0200179
Jens Axboe95646102007-07-28 21:17:50 +0200180 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200181
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200182 if (hdr->fio_magic != FIO_HDR_MAGIC) {
183 log_err("Bad verify header %x\n", hdr->fio_magic);
184 return EIO;
185 }
186
187 switch (hdr->verify_type) {
188 case VERIFY_MD5:
189 ret = verify_io_u_md5(hdr, io_u, hdr_num);
190 break;
191 case VERIFY_CRC64:
192 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
193 break;
194 case VERIFY_CRC32:
195 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
196 break;
197 case VERIFY_CRC16:
198 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
199 break;
200 case VERIFY_CRC7:
201 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
202 break;
203 default:
204 log_err("Bad verify type %u\n", hdr->verify_type);
205 ret = 1;
206 }
207 hdr_num++;
208 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100209
210 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200211}
212
Jens Axboe1e154bd2007-07-27 09:52:40 +0200213static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
214{
215 hdr->crc7 = crc7(p, len);
216}
217
Jens Axboe969f7ed2007-07-27 09:07:17 +0200218static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
219{
220 hdr->crc16 = crc16(p, len);
221}
222
Jens Axboee29d1b72006-10-18 15:43:15 +0200223static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
224{
225 hdr->crc32 = crc32(p, len);
226}
227
Jens Axboed77a7af2007-07-27 15:35:06 +0200228static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
229{
230 hdr->crc64 = crc64(p, len);
231}
232
Jens Axboee29d1b72006-10-18 15:43:15 +0200233static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
234{
Jens Axboe8c432322007-07-27 13:16:24 +0200235 struct md5_ctx md5_ctx = {
236 .hash = (uint32_t *) hdr->md5_digest,
237 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200238
Jens Axboee29d1b72006-10-18 15:43:15 +0200239 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200240}
241
242/*
243 * fill body of io_u->buf with random data and add a header with the
244 * crc32 or md5 sum of that data.
245 */
246void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
247{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200248 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200249 void *p = io_u->buf, *data;
Shawn Lewis546a9142007-07-28 21:11:37 +0200250 unsigned int hdr_inc, data_len;
Jens Axboee29d1b72006-10-18 15:43:15 +0200251
Jens Axboe9cc3d152007-03-26 10:32:30 +0200252 if (td->o.verify == VERIFY_NULL)
253 return;
254
Shawn Lewis546a9142007-07-28 21:11:37 +0200255 fill_random_bytes(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200256
Shawn Lewis546a9142007-07-28 21:11:37 +0200257 hdr_inc = io_u->buflen;
258 if (td->o.header_interval)
259 hdr_inc = td->o.header_interval;
260 data_len = hdr_inc - sizeof(*hdr);
261
Jens Axboe95646102007-07-28 21:17:50 +0200262 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
263 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200264
265 hdr->fio_magic = FIO_HDR_MAGIC;
266 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200267 hdr->len = hdr_inc;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200268
269 data = p + sizeof(*hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200270 switch (td->o.verify) {
271 case VERIFY_MD5:
272 fill_md5(hdr, data, data_len);
273 break;
274 case VERIFY_CRC64:
275 fill_crc64(hdr, data, data_len);
276 break;
277 case VERIFY_CRC32:
278 fill_crc32(hdr, data, data_len);
279 break;
280 case VERIFY_CRC16:
281 fill_crc16(hdr, data, data_len);
282 break;
283 case VERIFY_CRC7:
284 fill_crc7(hdr, data, data_len);
285 break;
286 default:
287 log_err("fio: bad verify type: %d\n", td->o.verify);
288 assert(0);
289 }
Shawn Lewis546a9142007-07-28 21:11:37 +0200290 if (td->o.header_offset)
Jens Axboe95646102007-07-28 21:17:50 +0200291 memswp(p, p + td->o.header_offset, sizeof(*hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200292 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200293}
294
295int get_next_verify(struct thread_data *td, struct io_u *io_u)
296{
Jens Axboe8de8f042007-03-27 10:36:12 +0200297 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200298
Jens Axboed2d7fa52007-02-19 13:21:35 +0100299 /*
300 * this io_u is from a requeue, we already filled the offsets
301 */
302 if (io_u->file)
303 return 0;
304
Jens Axboe8de8f042007-03-27 10:36:12 +0200305 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
306 struct rb_node *n = rb_first(&td->io_hist_tree);
307
Jens Axboe4b878982007-03-26 09:32:22 +0200308 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200309 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200310 } else if (!list_empty(&td->io_hist_list)) {
311 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
312 list_del(&ipo->list);
313 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200314
Jens Axboe8de8f042007-03-27 10:36:12 +0200315 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200316 io_u->offset = ipo->offset;
317 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100318 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200319
320 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
321 int r = td_io_open_file(td, io_u->file);
322
323 if (r)
324 return 1;
325 }
326
327 get_file(ipo->file);
328 assert(io_u->file->flags & FIO_FILE_OPEN);
Jens Axboee29d1b72006-10-18 15:43:15 +0200329 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100330 io_u->xfer_buf = io_u->buf;
331 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200332 free(ipo);
333 return 0;
334 }
335
336 return 1;
337}