2 * IO verification helpers
19 static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
25 r = os_random_long(&td->verify_state);
28 * lrand48_r seems to be broken and only fill the bottom
29 * 32-bits, even on 64-bit archs with 64-bit longs
42 static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
44 switch (td->o.verify_pattern_bytes) {
46 fill_random_bytes(td, p, len);
49 memset(p, td->o.verify_pattern, len);
54 unsigned int pattern = td->o.verify_pattern;
56 unsigned char c1, c2, c3, c4;
72 if (td->o.verify_pattern_bytes == 2 || i == len)
75 if (td->o.verify_pattern_bytes == 3 || i == len)
84 static void memswp(void* buf1, void* buf2, unsigned int len)
86 struct verify_header swap;
88 memcpy(&swap, buf1, len);
89 memcpy(buf1, buf2, len);
90 memcpy(buf2, &swap, len);
93 static void hexdump(void *buffer, int len)
95 unsigned char *p = buffer;
98 for (i = 0; i < len; i++)
99 log_info("%02x", p[i]);
104 * Return data area 'header_num'
106 static inline void *io_u_verify_off(struct verify_header *hdr,
108 unsigned char header_num)
110 return io_u->buf + sizeof(*hdr) + header_num * hdr->len;
113 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
114 unsigned int header_num)
116 void *p = io_u_verify_off(hdr, io_u, header_num);
118 struct sha512_ctx sha512_ctx = {
122 sha512_init(&sha512_ctx);
123 sha512_update(&sha512_ctx, p, hdr->len - sizeof(*hdr));
125 if (memcmp(hdr->sha512, sha512_ctx.buf, sizeof(sha512))) {
126 log_err("sha512: verify failed at %llu/%u\n",
127 io_u->offset + header_num * hdr->len,
129 hexdump(hdr->sha512, sizeof(hdr->sha512));
130 hexdump(sha512_ctx.buf, sizeof(sha512));
137 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
138 unsigned int header_num)
140 void *p = io_u_verify_off(hdr, io_u, header_num);
142 struct sha256_ctx sha256_ctx = {
146 sha256_init(&sha256_ctx);
147 sha256_update(&sha256_ctx, p, hdr->len - sizeof(*hdr));
149 if (memcmp(hdr->sha256, sha256_ctx.buf, sizeof(sha256))) {
150 log_err("sha256: verify failed at %llu/%u\n",
151 io_u->offset + header_num * hdr->len,
153 hexdump(hdr->sha256, sizeof(hdr->sha256));
154 hexdump(sha256_ctx.buf, sizeof(sha256));
161 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
162 unsigned char header_num)
164 void *p = io_u_verify_off(hdr, io_u, header_num);
167 c = crc7(p, hdr->len - sizeof(*hdr));
169 if (c != hdr->crc7) {
170 log_err("crc7: verify failed at %llu/%u\n",
171 io_u->offset + header_num * hdr->len,
173 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
180 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
181 unsigned int header_num)
183 void *p = io_u_verify_off(hdr, io_u, header_num);
186 c = crc16(p, hdr->len - sizeof(*hdr));
188 if (c != hdr->crc16) {
189 log_err("crc16: verify failed at %llu/%u\n",
190 io_u->offset + header_num * hdr->len,
192 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
199 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
200 unsigned int header_num)
202 void *p = io_u_verify_off(hdr, io_u, header_num);
203 unsigned long long c;
205 c = crc64(p, hdr->len - sizeof(*hdr));
207 if (c != hdr->crc64) {
208 log_err("crc64: verify failed at %llu/%u\n",
209 io_u->offset + header_num * hdr->len,
211 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
218 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
219 unsigned int header_num)
221 void *p = io_u_verify_off(hdr, io_u, header_num);
224 c = crc32(p, hdr->len - sizeof(*hdr));
226 if (c != hdr->crc32) {
227 log_err("crc32: verify failed at %llu/%u\n",
228 io_u->offset + header_num * hdr->len,
230 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
237 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
238 unsigned int header_num)
240 void *p = io_u_verify_off(hdr, io_u, header_num);
241 uint32_t hash[MD5_HASH_WORDS];
242 struct md5_ctx md5_ctx = {
247 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
249 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
250 log_err("md5: verify failed at %llu/%u\n",
251 io_u->offset + header_num * hdr->len,
253 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
254 hexdump(md5_ctx.hash, sizeof(hash));
261 int verify_io_u(struct thread_data *td, struct io_u *io_u)
263 struct verify_header *hdr;
264 unsigned int hdr_inc, hdr_num = 0;
268 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
271 hdr_inc = io_u->buflen;
272 if (td->o.verify_interval)
273 hdr_inc = td->o.verify_interval;
275 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
276 if (td->o.verify_offset)
277 memswp(p, p + td->o.verify_offset, sizeof(*hdr));
281 if (hdr->fio_magic != FIO_HDR_MAGIC) {
282 log_err("Bad verify header %x\n", hdr->fio_magic);
286 switch (hdr->verify_type) {
288 ret = verify_io_u_md5(hdr, io_u, hdr_num);
291 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
294 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
297 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
300 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
303 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
306 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
309 log_err("Bad verify type %u\n", hdr->verify_type);
318 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
320 struct sha512_ctx sha512_ctx = {
324 sha512_init(&sha512_ctx);
325 sha512_update(&sha512_ctx, p, len);
328 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
330 struct sha256_ctx sha256_ctx = {
334 sha256_init(&sha256_ctx);
335 sha256_update(&sha256_ctx, p, len);
338 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
340 hdr->crc7 = crc7(p, len);
343 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
345 hdr->crc16 = crc16(p, len);
348 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
350 hdr->crc32 = crc32(p, len);
353 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
355 hdr->crc64 = crc64(p, len);
358 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
360 struct md5_ctx md5_ctx = {
361 .hash = (uint32_t *) hdr->md5_digest,
365 md5_update(&md5_ctx, p, len);
369 * fill body of io_u->buf with random data and add a header with the
370 * crc32 or md5 sum of that data.
372 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
374 struct verify_header *hdr;
375 void *p = io_u->buf, *data;
376 unsigned int hdr_inc, data_len;
378 if (td->o.verify == VERIFY_NULL)
381 fill_pattern(td, p, io_u->buflen);
383 hdr_inc = io_u->buflen;
384 if (td->o.verify_interval)
385 hdr_inc = td->o.verify_interval;
386 data_len = hdr_inc - sizeof(*hdr);
388 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
391 hdr->fio_magic = FIO_HDR_MAGIC;
392 hdr->verify_type = td->o.verify;
395 data = p + sizeof(*hdr);
396 switch (td->o.verify) {
398 fill_md5(hdr, data, data_len);
401 fill_crc64(hdr, data, data_len);
404 fill_crc32(hdr, data, data_len);
407 fill_crc16(hdr, data, data_len);
410 fill_crc7(hdr, data, data_len);
413 fill_sha256(hdr, data, data_len);
416 fill_sha512(hdr, data, data_len);
419 log_err("fio: bad verify type: %d\n", td->o.verify);
422 if (td->o.verify_offset)
423 memswp(p, p + td->o.verify_offset, sizeof(*hdr));
427 int get_next_verify(struct thread_data *td, struct io_u *io_u)
429 struct io_piece *ipo = NULL;
432 * this io_u is from a requeue, we already filled the offsets
437 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
438 struct rb_node *n = rb_first(&td->io_hist_tree);
440 ipo = rb_entry(n, struct io_piece, rb_node);
441 rb_erase(n, &td->io_hist_tree);
442 } else if (!list_empty(&td->io_hist_list)) {
443 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
444 list_del(&ipo->list);
448 io_u->offset = ipo->offset;
449 io_u->buflen = ipo->len;
450 io_u->file = ipo->file;
452 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
453 int r = td_io_open_file(td, io_u->file);
460 assert(io_u->file->flags & FIO_FILE_OPEN);
461 io_u->ddir = DDIR_READ;
462 io_u->xfer_buf = io_u->buf;
463 io_u->xfer_buflen = io_u->buflen;