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 * Prepare for seperation of verify_header and checksum header
106 static inline unsigned int __hdr_size(int fio_unused verify_type)
108 return sizeof(struct verify_header);
112 static inline unsigned int hdr_size(struct verify_header *hdr)
118 * Return data area 'header_num'
120 static inline void *io_u_verify_off(struct verify_header *hdr,
122 unsigned char header_num)
124 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
127 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
128 unsigned int header_num)
130 void *p = io_u_verify_off(hdr, io_u, header_num);
132 struct sha512_ctx sha512_ctx = {
136 sha512_init(&sha512_ctx);
137 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
139 if (memcmp(hdr->sha512, sha512_ctx.buf, sizeof(sha512))) {
140 log_err("sha512: verify failed at %llu/%u\n",
141 io_u->offset + header_num * hdr->len,
143 hexdump(hdr->sha512, sizeof(hdr->sha512));
144 hexdump(sha512_ctx.buf, sizeof(sha512));
151 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
152 unsigned int header_num)
154 void *p = io_u_verify_off(hdr, io_u, header_num);
156 struct sha256_ctx sha256_ctx = {
160 sha256_init(&sha256_ctx);
161 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
163 if (memcmp(hdr->sha256, sha256_ctx.buf, sizeof(sha256))) {
164 log_err("sha256: verify failed at %llu/%u\n",
165 io_u->offset + header_num * hdr->len,
167 hexdump(hdr->sha256, sizeof(hdr->sha256));
168 hexdump(sha256_ctx.buf, sizeof(sha256));
175 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
176 unsigned char header_num)
178 void *p = io_u_verify_off(hdr, io_u, header_num);
181 c = crc7(p, hdr->len - hdr_size(hdr));
183 if (c != hdr->crc7) {
184 log_err("crc7: verify failed at %llu/%u\n",
185 io_u->offset + header_num * hdr->len,
187 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
194 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
195 unsigned int header_num)
197 void *p = io_u_verify_off(hdr, io_u, header_num);
200 c = crc16(p, hdr->len - hdr_size(hdr));
202 if (c != hdr->crc16) {
203 log_err("crc16: verify failed at %llu/%u\n",
204 io_u->offset + header_num * hdr->len,
206 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
213 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
214 unsigned int header_num)
216 void *p = io_u_verify_off(hdr, io_u, header_num);
217 unsigned long long c;
219 c = crc64(p, hdr->len - hdr_size(hdr));
221 if (c != hdr->crc64) {
222 log_err("crc64: verify failed at %llu/%u\n",
223 io_u->offset + header_num * hdr->len,
225 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
232 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
233 unsigned int header_num)
235 void *p = io_u_verify_off(hdr, io_u, header_num);
238 c = crc32(p, hdr->len - hdr_size(hdr));
240 if (c != hdr->crc32) {
241 log_err("crc32: verify failed at %llu/%u\n",
242 io_u->offset + header_num * hdr->len,
244 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
251 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
252 unsigned int header_num)
254 void *p = io_u_verify_off(hdr, io_u, header_num);
255 uint32_t hash[MD5_HASH_WORDS];
256 struct md5_ctx md5_ctx = {
261 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
263 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
264 log_err("md5: verify failed at %llu/%u\n",
265 io_u->offset + header_num * hdr->len,
267 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
268 hexdump(md5_ctx.hash, sizeof(hash));
275 int verify_io_u(struct thread_data *td, struct io_u *io_u)
277 struct verify_header *hdr;
278 unsigned int hdr_inc, hdr_num = 0;
282 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
285 hdr_inc = io_u->buflen;
286 if (td->o.verify_interval)
287 hdr_inc = td->o.verify_interval;
289 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
290 if (td->o.verify_offset)
291 memswp(p, p + td->o.verify_offset, __hdr_size(hdr->verify_type));
295 if (hdr->fio_magic != FIO_HDR_MAGIC) {
296 log_err("Bad verify header %x\n", hdr->fio_magic);
300 switch (hdr->verify_type) {
302 ret = verify_io_u_md5(hdr, io_u, hdr_num);
305 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
308 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
311 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
314 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
317 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
320 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
323 log_err("Bad verify type %u\n", hdr->verify_type);
332 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
334 struct sha512_ctx sha512_ctx = {
338 sha512_init(&sha512_ctx);
339 sha512_update(&sha512_ctx, p, len);
342 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
344 struct sha256_ctx sha256_ctx = {
348 sha256_init(&sha256_ctx);
349 sha256_update(&sha256_ctx, p, len);
352 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
354 hdr->crc7 = crc7(p, len);
357 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
359 hdr->crc16 = crc16(p, len);
362 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
364 hdr->crc32 = crc32(p, len);
367 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
369 hdr->crc64 = crc64(p, len);
372 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
374 struct md5_ctx md5_ctx = {
375 .hash = (uint32_t *) hdr->md5_digest,
379 md5_update(&md5_ctx, p, len);
383 * fill body of io_u->buf with random data and add a header with the
384 * crc32 or md5 sum of that data.
386 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
388 struct verify_header *hdr;
389 void *p = io_u->buf, *data;
390 unsigned int hdr_inc, data_len;
392 if (td->o.verify == VERIFY_NULL)
395 fill_pattern(td, p, io_u->buflen);
397 hdr_inc = io_u->buflen;
398 if (td->o.verify_interval)
399 hdr_inc = td->o.verify_interval;
401 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
404 hdr->fio_magic = FIO_HDR_MAGIC;
405 hdr->verify_type = td->o.verify;
407 data_len = hdr_inc - hdr_size(hdr);
409 data = p + hdr_size(hdr);
410 switch (td->o.verify) {
412 fill_md5(hdr, data, data_len);
415 fill_crc64(hdr, data, data_len);
418 fill_crc32(hdr, data, data_len);
421 fill_crc16(hdr, data, data_len);
424 fill_crc7(hdr, data, data_len);
427 fill_sha256(hdr, data, data_len);
430 fill_sha512(hdr, data, data_len);
433 log_err("fio: bad verify type: %d\n", td->o.verify);
436 if (td->o.verify_offset)
437 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
441 int get_next_verify(struct thread_data *td, struct io_u *io_u)
443 struct io_piece *ipo = NULL;
446 * this io_u is from a requeue, we already filled the offsets
451 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
452 struct rb_node *n = rb_first(&td->io_hist_tree);
454 ipo = rb_entry(n, struct io_piece, rb_node);
455 rb_erase(n, &td->io_hist_tree);
456 } else if (!list_empty(&td->io_hist_list)) {
457 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
458 list_del(&ipo->list);
462 io_u->offset = ipo->offset;
463 io_u->buflen = ipo->len;
464 io_u->file = ipo->file;
466 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
467 int r = td_io_open_file(td, io_u->file);
474 assert(io_u->file->flags & FIO_FILE_OPEN);
475 io_u->ddir = DDIR_READ;
476 io_u->xfer_buf = io_u->buf;
477 io_u->xfer_buflen = io_u->buflen;