blob: d98dfcf4c52b8bd5a43411bb2cf8397c6fd0952d [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 Axboefd009542007-07-29 20:59:01 +0200148 memset(md5_ctx.hash, 0, sizeof(hdr->md5_digest));
Jens Axboee29d1b72006-10-18 15:43:15 +0200149 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
150
Jens Axboec9b3fdf2007-07-29 08:49:16 +0200151 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200152 log_err("md5: verify failed at %llu/%u\n",
153 io_u->offset + header_num * hdr->len,
154 hdr->len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200155 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200156 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboee29d1b72006-10-18 15:43:15 +0200157 return 1;
158 }
159
160 return 0;
161}
162
Jens Axboe36690c92007-03-26 10:23:34 +0200163int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200164{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200165 struct verify_header *hdr;
166 unsigned int hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200167 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200168 int ret;
169
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200170 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200171 return 0;
172
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200173 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200174 if (td->o.verify_interval)
175 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200176
Jens Axboe95646102007-07-28 21:17:50 +0200177 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboea59e1702007-07-30 08:53:27 +0200178 if (td->o.verify_offset)
179 memswp(p, p + td->o.verify_offset, sizeof(*hdr));
Shawn Lewis546a9142007-07-28 21:11:37 +0200180
Jens Axboe95646102007-07-28 21:17:50 +0200181 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200182
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200183 if (hdr->fio_magic != FIO_HDR_MAGIC) {
184 log_err("Bad verify header %x\n", hdr->fio_magic);
185 return EIO;
186 }
187
188 switch (hdr->verify_type) {
189 case VERIFY_MD5:
190 ret = verify_io_u_md5(hdr, io_u, hdr_num);
191 break;
192 case VERIFY_CRC64:
193 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
194 break;
195 case VERIFY_CRC32:
196 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
197 break;
198 case VERIFY_CRC16:
199 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
200 break;
201 case VERIFY_CRC7:
202 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
203 break;
204 default:
205 log_err("Bad verify type %u\n", hdr->verify_type);
206 ret = 1;
207 }
208 hdr_num++;
209 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100210
211 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200212}
213
Jens Axboe1e154bd2007-07-27 09:52:40 +0200214static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
215{
216 hdr->crc7 = crc7(p, len);
217}
218
Jens Axboe969f7ed2007-07-27 09:07:17 +0200219static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
220{
221 hdr->crc16 = crc16(p, len);
222}
223
Jens Axboee29d1b72006-10-18 15:43:15 +0200224static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
225{
226 hdr->crc32 = crc32(p, len);
227}
228
Jens Axboed77a7af2007-07-27 15:35:06 +0200229static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
230{
231 hdr->crc64 = crc64(p, len);
232}
233
Jens Axboee29d1b72006-10-18 15:43:15 +0200234static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
235{
Jens Axboe8c432322007-07-27 13:16:24 +0200236 struct md5_ctx md5_ctx = {
237 .hash = (uint32_t *) hdr->md5_digest,
238 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200239
Jens Axboefd009542007-07-29 20:59:01 +0200240 memset(md5_ctx.hash, 0, sizeof(hdr->md5_digest));
Jens Axboee29d1b72006-10-18 15:43:15 +0200241 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200242}
243
244/*
245 * fill body of io_u->buf with random data and add a header with the
246 * crc32 or md5 sum of that data.
247 */
248void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
249{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200250 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200251 void *p = io_u->buf, *data;
Shawn Lewis546a9142007-07-28 21:11:37 +0200252 unsigned int hdr_inc, data_len;
Jens Axboee29d1b72006-10-18 15:43:15 +0200253
Jens Axboe9cc3d152007-03-26 10:32:30 +0200254 if (td->o.verify == VERIFY_NULL)
255 return;
256
Shawn Lewis546a9142007-07-28 21:11:37 +0200257 fill_random_bytes(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200258
Shawn Lewis546a9142007-07-28 21:11:37 +0200259 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200260 if (td->o.verify_interval)
261 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200262 data_len = hdr_inc - sizeof(*hdr);
263
Jens Axboe95646102007-07-28 21:17:50 +0200264 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
265 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200266
267 hdr->fio_magic = FIO_HDR_MAGIC;
268 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200269 hdr->len = hdr_inc;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200270
271 data = p + sizeof(*hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200272 switch (td->o.verify) {
273 case VERIFY_MD5:
274 fill_md5(hdr, data, data_len);
275 break;
276 case VERIFY_CRC64:
277 fill_crc64(hdr, data, data_len);
278 break;
279 case VERIFY_CRC32:
280 fill_crc32(hdr, data, data_len);
281 break;
282 case VERIFY_CRC16:
283 fill_crc16(hdr, data, data_len);
284 break;
285 case VERIFY_CRC7:
286 fill_crc7(hdr, data, data_len);
287 break;
288 default:
289 log_err("fio: bad verify type: %d\n", td->o.verify);
290 assert(0);
291 }
Jens Axboea59e1702007-07-30 08:53:27 +0200292 if (td->o.verify_offset)
293 memswp(p, p + td->o.verify_offset, sizeof(*hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200294 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200295}
296
297int get_next_verify(struct thread_data *td, struct io_u *io_u)
298{
Jens Axboe8de8f042007-03-27 10:36:12 +0200299 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200300
Jens Axboed2d7fa52007-02-19 13:21:35 +0100301 /*
302 * this io_u is from a requeue, we already filled the offsets
303 */
304 if (io_u->file)
305 return 0;
306
Jens Axboe8de8f042007-03-27 10:36:12 +0200307 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
308 struct rb_node *n = rb_first(&td->io_hist_tree);
309
Jens Axboe4b878982007-03-26 09:32:22 +0200310 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200311 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200312 } else if (!list_empty(&td->io_hist_list)) {
313 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
314 list_del(&ipo->list);
315 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200316
Jens Axboe8de8f042007-03-27 10:36:12 +0200317 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200318 io_u->offset = ipo->offset;
319 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100320 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200321
322 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
323 int r = td_io_open_file(td, io_u->file);
324
325 if (r)
326 return 1;
327 }
328
329 get_file(ipo->file);
330 assert(io_u->file->flags & FIO_FILE_OPEN);
Jens Axboee29d1b72006-10-18 15:43:15 +0200331 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100332 io_u->xfer_buf = io_u->buf;
333 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200334 free(ipo);
335 return 0;
336 }
337
338 return 1;
339}