Add clue for ENOMEM hugepage allocation
[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;
15 double r;
16
17 while (len) {
18 r = os_random_double(&td->verify_state);
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
45static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
46{
47 unsigned char *p = (unsigned char *) io_u->buf;
48 unsigned long c;
49
50 p += sizeof(*hdr);
51 c = crc32(p, hdr->len - sizeof(*hdr));
52
53 if (c != hdr->crc32) {
a4f4fdd7 54 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
e29d1b70
JA
55 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
56 return 1;
57 }
58
59 return 0;
60}
61
62static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
63{
64 unsigned char *p = (unsigned char *) io_u->buf;
65 struct md5_ctx md5_ctx;
66
67 memset(&md5_ctx, 0, sizeof(md5_ctx));
68 p += sizeof(*hdr);
69 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
70
71 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
a4f4fdd7 72 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
e29d1b70
JA
73 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
74 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
75 return 1;
76 }
77
78 return 0;
79}
80
36690c9b 81int verify_io_u(struct thread_data *td, struct io_u *io_u)
e29d1b70
JA
82{
83 struct verify_header *hdr = (struct verify_header *) io_u->buf;
84 int ret;
85
36690c9b
JA
86 if (td->o.verify == VERIFY_NULL)
87 return 0;
88
36167d82
JA
89 if (hdr->fio_magic != FIO_HDR_MAGIC) {
90 log_err("Bad verify header %x\n", hdr->fio_magic);
a7dfe862 91 return EIO;
36167d82 92 }
e29d1b70
JA
93
94 if (hdr->verify_type == VERIFY_MD5)
95 ret = verify_io_u_md5(hdr, io_u);
96 else if (hdr->verify_type == VERIFY_CRC32)
97 ret = verify_io_u_crc32(hdr, io_u);
98 else {
1e97cce9 99 log_err("Bad verify type %u\n", hdr->verify_type);
e29d1b70
JA
100 ret = 1;
101 }
102
a7dfe862
JA
103 if (ret)
104 return EIO;
105
106 return 0;
e29d1b70
JA
107}
108
109static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
110{
111 hdr->crc32 = crc32(p, len);
112}
113
114static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
115{
116 struct md5_ctx md5_ctx;
117
118 memset(&md5_ctx, 0, sizeof(md5_ctx));
119 md5_update(&md5_ctx, p, len);
120 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
121}
122
123/*
124 * fill body of io_u->buf with random data and add a header with the
125 * crc32 or md5 sum of that data.
126 */
127void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
128{
129 unsigned char *p = (unsigned char *) io_u->buf;
130 struct verify_header hdr;
131
9cc3d150
JA
132 if (td->o.verify == VERIFY_NULL)
133 return;
134
e29d1b70
JA
135 hdr.fio_magic = FIO_HDR_MAGIC;
136 hdr.len = io_u->buflen;
137 p += sizeof(hdr);
138 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
139
2dc1bbeb 140 if (td->o.verify == VERIFY_MD5) {
e29d1b70
JA
141 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
142 hdr.verify_type = VERIFY_MD5;
36690c9b 143 } else if (td->o.verify == VERIFY_CRC32) {
e29d1b70
JA
144 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
145 hdr.verify_type = VERIFY_CRC32;
146 }
147
148 memcpy(io_u->buf, &hdr, sizeof(hdr));
149}
150
151int get_next_verify(struct thread_data *td, struct io_u *io_u)
152{
8de8f047 153 struct io_piece *ipo = NULL;
e29d1b70 154
d2d7fa53
JA
155 /*
156 * this io_u is from a requeue, we already filled the offsets
157 */
158 if (io_u->file)
159 return 0;
160
8de8f047
JA
161 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
162 struct rb_node *n = rb_first(&td->io_hist_tree);
e29d1b70 163
8de8f047 164 ipo = rb_entry(n, struct io_piece, rb_node);
4b87898e 165 rb_erase(n, &td->io_hist_tree);
8de8f047
JA
166 } else if (!list_empty(&td->io_hist_list)) {
167 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
168 list_del(&ipo->list);
169 }
e29d1b70 170
8de8f047 171 if (ipo) {
e29d1b70
JA
172 io_u->offset = ipo->offset;
173 io_u->buflen = ipo->len;
36167d82 174 io_u->file = ipo->file;
97af62ce
JA
175
176 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
177 int r = td_io_open_file(td, io_u->file);
178
179 if (r)
180 return 1;
181 }
182
183 get_file(ipo->file);
184 assert(io_u->file->flags & FIO_FILE_OPEN);
e29d1b70 185 io_u->ddir = DDIR_READ;
36167d82
JA
186 io_u->xfer_buf = io_u->buf;
187 io_u->xfer_buflen = io_u->buflen;
e29d1b70
JA
188 free(ipo);
189 return 0;
190 }
191
192 return 1;
193}