Clean up verify_io_u()
[fio.git] / verify.c
CommitLineData
e29d1b70
JA
1/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
97af62ce 7#include <assert.h>
e29d1b70
JA
8
9#include "fio.h"
e29d1b70
JA
10
11static void fill_random_bytes(struct thread_data *td,
12 unsigned char *p, unsigned int len)
13{
14 unsigned int todo;
4c5946c6 15 int r;
e29d1b70
JA
16
17 while (len) {
4c5946c6 18 r = os_random_long(&td->verify_state);
e29d1b70
JA
19
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++)
6d86144d
JA
41 log_info("%02x", p[i]);
42 log_info("\n");
e29d1b70
JA
43}
44
1e154bdb
JA
45static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u)
46{
47 unsigned char *p = io_u->buf;
48 unsigned char c;
49
50 p += sizeof(*hdr);
51 c = crc7(p, hdr->len - sizeof(*hdr));
52
53 if (c != hdr->crc7) {
54 log_err("crc7: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
55 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
56 return 1;
57 }
58
59 return 0;
60}
61
969f7ed3
JA
62static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u)
63{
64 unsigned char *p = io_u->buf;
65 unsigned short c;
66
67 p += sizeof(*hdr);
68 c = crc16(p, hdr->len - sizeof(*hdr));
69
70 if (c != hdr->crc16) {
71 log_err("crc16: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
1f24ea44 72 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
969f7ed3
JA
73 return 1;
74 }
75
76 return 0;
77}
78
e29d1b70
JA
79static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
80{
969f7ed3 81 unsigned char *p = io_u->buf;
e29d1b70
JA
82 unsigned long c;
83
84 p += sizeof(*hdr);
85 c = crc32(p, hdr->len - sizeof(*hdr));
86
87 if (c != hdr->crc32) {
a4f4fdd7 88 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
e29d1b70
JA
89 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
90 return 1;
91 }
92
93 return 0;
94}
95
96static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
97{
969f7ed3 98 unsigned char *p = io_u->buf;
e29d1b70
JA
99 struct md5_ctx md5_ctx;
100
101 memset(&md5_ctx, 0, sizeof(md5_ctx));
102 p += sizeof(*hdr);
103 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
104
105 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
a4f4fdd7 106 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
e29d1b70
JA
107 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
108 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
109 return 1;
110 }
111
112 return 0;
113}
114
36690c9b 115int verify_io_u(struct thread_data *td, struct io_u *io_u)
e29d1b70
JA
116{
117 struct verify_header *hdr = (struct verify_header *) io_u->buf;
118 int ret;
119
1dcc0498 120 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
36690c9b
JA
121 return 0;
122
36167d82
JA
123 if (hdr->fio_magic != FIO_HDR_MAGIC) {
124 log_err("Bad verify header %x\n", hdr->fio_magic);
a7dfe862 125 return EIO;
36167d82 126 }
e29d1b70 127
a3ff21e3
JA
128 switch (hdr->verify_type) {
129 case VERIFY_MD5:
e29d1b70 130 ret = verify_io_u_md5(hdr, io_u);
a3ff21e3
JA
131 break;
132 case VERIFY_CRC32:
e29d1b70 133 ret = verify_io_u_crc32(hdr, io_u);
a3ff21e3
JA
134 break;
135 case VERIFY_CRC16:
969f7ed3 136 ret = verify_io_u_crc16(hdr, io_u);
a3ff21e3
JA
137 break;
138 case VERIFY_CRC7:
1e154bdb 139 ret = verify_io_u_crc7(hdr, io_u);
a3ff21e3
JA
140 break;
141 default:
1e97cce9 142 log_err("Bad verify type %u\n", hdr->verify_type);
e29d1b70
JA
143 ret = 1;
144 }
145
a7dfe862
JA
146 if (ret)
147 return EIO;
148
149 return 0;
e29d1b70
JA
150}
151
1e154bdb
JA
152static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
153{
154 hdr->crc7 = crc7(p, len);
155}
156
969f7ed3
JA
157static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
158{
159 hdr->crc16 = crc16(p, len);
160}
161
e29d1b70
JA
162static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
163{
164 hdr->crc32 = crc32(p, len);
165}
166
167static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
168{
169 struct md5_ctx md5_ctx;
170
171 memset(&md5_ctx, 0, sizeof(md5_ctx));
172 md5_update(&md5_ctx, p, len);
173 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
174}
175
176/*
177 * fill body of io_u->buf with random data and add a header with the
178 * crc32 or md5 sum of that data.
179 */
180void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
181{
baefa9be
JA
182 const unsigned int len = io_u->buflen - sizeof(struct verify_header);
183 struct verify_header *hdr;
184 unsigned char *p;
e29d1b70 185
9cc3d150
JA
186 if (td->o.verify == VERIFY_NULL)
187 return;
188
baefa9be
JA
189 hdr = (struct verify_header *) io_u->buf;
190 hdr->fio_magic = FIO_HDR_MAGIC;
191 hdr->len = io_u->buflen;
192 hdr->verify_type = td->o.verify;
193
194 p = io_u->buf + sizeof(*hdr);
195 fill_random_bytes(td, p, len);
196
197 switch (td->o.verify) {
198 case VERIFY_MD5:
199 fill_md5(hdr, p, len);
200 break;
201 case VERIFY_CRC32:
202 fill_crc32(hdr, p, len);
203 break;
204 case VERIFY_CRC16:
205 fill_crc16(hdr, p, len);
206 break;
207 case VERIFY_CRC7:
208 fill_crc7(hdr, p, len);
209 break;
210 default:
211 log_err("fio: bad verify type: %d\n", td->o.verify);
212 assert(0);
e29d1b70 213 }
e29d1b70
JA
214}
215
216int get_next_verify(struct thread_data *td, struct io_u *io_u)
217{
8de8f047 218 struct io_piece *ipo = NULL;
e29d1b70 219
d2d7fa53
JA
220 /*
221 * this io_u is from a requeue, we already filled the offsets
222 */
223 if (io_u->file)
224 return 0;
225
8de8f047
JA
226 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
227 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 228
8de8f047 229 ipo = rb_entry(n, struct io_piece, rb_node);
4b87898e 230 rb_erase(n, &td->io_hist_tree);
8de8f047
JA
231 } else if (!list_empty(&td->io_hist_list)) {
232 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
233 list_del(&ipo->list);
234 }
e29d1b70 235
8de8f047 236 if (ipo) {
e29d1b70
JA
237 io_u->offset = ipo->offset;
238 io_u->buflen = ipo->len;
36167d82 239 io_u->file = ipo->file;
97af62ce
JA
240
241 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
242 int r = td_io_open_file(td, io_u->file);
243
244 if (r)
245 return 1;
246 }
247
248 get_file(ipo->file);
249 assert(io_u->file->flags & FIO_FILE_OPEN);
e29d1b70 250 io_u->ddir = DDIR_READ;
36167d82
JA
251 io_u->xfer_buf = io_u->buf;
252 io_u->xfer_buflen = io_u->buflen;
e29d1b70
JA
253 free(ipo);
254 return 0;
255 }
256
257 return 1;
258}