-static void fill_random_bytes(struct thread_data *td,
- unsigned char *p, unsigned int len)
-{
- unsigned int todo;
- double r;
-
- while (len) {
- r = os_random_double(&td->verify_state);
-
- /*
- * lrand48_r seems to be broken and only fill the bottom
- * 32-bits, even on 64-bit archs with 64-bit longs
- */
- todo = sizeof(r);
- if (todo > len)
- todo = len;
-
- memcpy(p, &r, todo);
-
- len -= todo;
- p += todo;
- }
-}
-
-static void hexdump(void *buffer, int len)
-{
- unsigned char *p = buffer;
- int i;
-
- for (i = 0; i < len; i++)
- printf("%02x", p[i]);
- printf("\n");
-}
-
-static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
-{
- unsigned char *p = (unsigned char *) io_u->buf;
- unsigned long c;
-
- p += sizeof(*hdr);
- c = crc32(p, hdr->len - sizeof(*hdr));
-
- if (c != hdr->crc32) {
- fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
- fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
- return 1;
- }
-
- return 0;
-}
-
-static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
-{
- unsigned char *p = (unsigned char *) io_u->buf;
- struct md5_ctx md5_ctx;
-
- memset(&md5_ctx, 0, sizeof(md5_ctx));
- p += sizeof(*hdr);
- md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
-
- if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
- fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
- hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
- hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
- return 1;
- }
-
- return 0;
-}
-
-static int verify_io_u(struct io_u *io_u)
-{
- struct verify_header *hdr = (struct verify_header *) io_u->buf;
- int ret;
-
- if (hdr->fio_magic != FIO_HDR_MAGIC)
- return 1;
-
- if (hdr->verify_type == VERIFY_MD5)
- ret = verify_io_u_md5(hdr, io_u);
- else if (hdr->verify_type == VERIFY_CRC32)
- ret = verify_io_u_crc32(hdr, io_u);
- else {
- fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
- ret = 1;
- }
-
- return ret;
-}
-
-static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
-{
- hdr->crc32 = crc32(p, len);
-}
-
-static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
-{
- struct md5_ctx md5_ctx;
-
- memset(&md5_ctx, 0, sizeof(md5_ctx));
- md5_update(&md5_ctx, p, len);
- memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
-}
-
-static int get_rw_ddir(struct thread_data *td)
-{
- if (td_rw(td)) {
- struct timeval now;
- unsigned long elapsed;
-
- gettimeofday(&now, NULL);
- elapsed = mtime_since_now(&td->rwmix_switch);
-
- /*
- * Check if it's time to seed a new data direction.
- */
- if (elapsed >= td->rwmixcycle) {
- unsigned long v;
- long r;
-
- r = os_random_long(&td->random_state);
- v = 100UL * r / (unsigned long) (RAND_MAX + 1.0);
- if (v < td->rwmixread)
- td->rwmix_ddir = DDIR_READ;
- else
- td->rwmix_ddir = DDIR_WRITE;
- memcpy(&td->rwmix_switch, &now, sizeof(now));
- }
- return td->rwmix_ddir;
- } else if (td_read(td))
- return DDIR_READ;
- else
- return DDIR_WRITE;
-}
-