blob: cca070c8279f609456cb24e2c33a41dcfb292785 [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,
12 unsigned char *p, unsigned int len)
13{
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
35static void hexdump(void *buffer, int len)
36{
37 unsigned char *p = buffer;
38 int i;
39
40 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +010041 log_info("%02x", p[i]);
42 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020043}
44
Jens Axboe969f7ed2007-07-27 09:07:17 +020045static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u)
46{
47 unsigned char *p = io_u->buf;
48 unsigned short c;
49
50 p += sizeof(*hdr);
51 c = crc16(p, hdr->len - sizeof(*hdr));
52
53 if (c != hdr->crc16) {
54 log_err("crc16: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
55 log_err("crc16: wanted %lx, got %x\n", hdr->crc32, c);
56 return 1;
57 }
58
59 return 0;
60}
61
Jens Axboee29d1b72006-10-18 15:43:15 +020062static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
63{
Jens Axboe969f7ed2007-07-27 09:07:17 +020064 unsigned char *p = io_u->buf;
Jens Axboee29d1b72006-10-18 15:43:15 +020065 unsigned long c;
66
67 p += sizeof(*hdr);
68 c = crc32(p, hdr->len - sizeof(*hdr));
69
70 if (c != hdr->crc32) {
Jens Axboea4f4fdd2007-02-14 01:16:39 +010071 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
Jens Axboee29d1b72006-10-18 15:43:15 +020072 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
73 return 1;
74 }
75
76 return 0;
77}
78
79static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
80{
Jens Axboe969f7ed2007-07-27 09:07:17 +020081 unsigned char *p = io_u->buf;
Jens Axboee29d1b72006-10-18 15:43:15 +020082 struct md5_ctx md5_ctx;
83
84 memset(&md5_ctx, 0, sizeof(md5_ctx));
85 p += sizeof(*hdr);
86 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
87
88 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
Jens Axboea4f4fdd2007-02-14 01:16:39 +010089 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
Jens Axboee29d1b72006-10-18 15:43:15 +020090 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
91 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
92 return 1;
93 }
94
95 return 0;
96}
97
Jens Axboe36690c92007-03-26 10:23:34 +020098int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +020099{
100 struct verify_header *hdr = (struct verify_header *) io_u->buf;
101 int ret;
102
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200103 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200104 return 0;
105
Jens Axboe36167d82007-02-18 05:41:31 +0100106 if (hdr->fio_magic != FIO_HDR_MAGIC) {
107 log_err("Bad verify header %x\n", hdr->fio_magic);
Jens Axboea7dfe862007-03-12 10:05:08 +0100108 return EIO;
Jens Axboe36167d82007-02-18 05:41:31 +0100109 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200110
111 if (hdr->verify_type == VERIFY_MD5)
112 ret = verify_io_u_md5(hdr, io_u);
113 else if (hdr->verify_type == VERIFY_CRC32)
114 ret = verify_io_u_crc32(hdr, io_u);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200115 else if (hdr->verify_type == VERIFY_CRC16)
116 ret = verify_io_u_crc16(hdr, io_u);
Jens Axboee29d1b72006-10-18 15:43:15 +0200117 else {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100118 log_err("Bad verify type %u\n", hdr->verify_type);
Jens Axboee29d1b72006-10-18 15:43:15 +0200119 ret = 1;
120 }
121
Jens Axboea7dfe862007-03-12 10:05:08 +0100122 if (ret)
123 return EIO;
124
125 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200126}
127
Jens Axboe969f7ed2007-07-27 09:07:17 +0200128static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
129{
130 hdr->crc16 = crc16(p, len);
131}
132
Jens Axboee29d1b72006-10-18 15:43:15 +0200133static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
134{
135 hdr->crc32 = crc32(p, len);
136}
137
138static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
139{
140 struct md5_ctx md5_ctx;
141
142 memset(&md5_ctx, 0, sizeof(md5_ctx));
143 md5_update(&md5_ctx, p, len);
144 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
145}
146
147/*
148 * fill body of io_u->buf with random data and add a header with the
149 * crc32 or md5 sum of that data.
150 */
151void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
152{
153 unsigned char *p = (unsigned char *) io_u->buf;
154 struct verify_header hdr;
155
Jens Axboe9cc3d152007-03-26 10:32:30 +0200156 if (td->o.verify == VERIFY_NULL)
157 return;
158
Jens Axboee29d1b72006-10-18 15:43:15 +0200159 hdr.fio_magic = FIO_HDR_MAGIC;
160 hdr.len = io_u->buflen;
161 p += sizeof(hdr);
162 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
163
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100164 if (td->o.verify == VERIFY_MD5) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200165 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
166 hdr.verify_type = VERIFY_MD5;
Jens Axboe36690c92007-03-26 10:23:34 +0200167 } else if (td->o.verify == VERIFY_CRC32) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200168 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
169 hdr.verify_type = VERIFY_CRC32;
Jens Axboe969f7ed2007-07-27 09:07:17 +0200170 } else if (td->o.verify == VERIFY_CRC16) {
171 fill_crc16(&hdr, p, io_u->buflen - sizeof(hdr));
172 hdr.verify_type = VERIFY_CRC16;
Jens Axboee29d1b72006-10-18 15:43:15 +0200173 }
174
175 memcpy(io_u->buf, &hdr, sizeof(hdr));
176}
177
178int get_next_verify(struct thread_data *td, struct io_u *io_u)
179{
Jens Axboe8de8f042007-03-27 10:36:12 +0200180 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200181
Jens Axboed2d7fa52007-02-19 13:21:35 +0100182 /*
183 * this io_u is from a requeue, we already filled the offsets
184 */
185 if (io_u->file)
186 return 0;
187
Jens Axboe8de8f042007-03-27 10:36:12 +0200188 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
189 struct rb_node *n = rb_first(&td->io_hist_tree);
190
Jens Axboe4b878982007-03-26 09:32:22 +0200191 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200192 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200193 } else if (!list_empty(&td->io_hist_list)) {
194 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
195 list_del(&ipo->list);
196 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200197
Jens Axboe8de8f042007-03-27 10:36:12 +0200198 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200199 io_u->offset = ipo->offset;
200 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100201 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200202
203 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
204 int r = td_io_open_file(td, io_u->file);
205
206 if (r)
207 return 1;
208 }
209
210 get_file(ipo->file);
211 assert(io_u->file->flags & FIO_FILE_OPEN);
Jens Axboee29d1b72006-10-18 15:43:15 +0200212 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100213 io_u->xfer_buf = io_u->buf;
214 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200215 free(ipo);
216 return 0;
217 }
218
219 return 1;
220}