Properly initialize md5 hash seed
[fio.git] / verify.c
index a362933d3115e9f340fe351cd8db2bdffdf8a0a8..566c53c1f995c42d51f41f681df880005c6cc078 100644 (file)
--- a/verify.c
+++ b/verify.c
@@ -8,8 +8,7 @@
 
 #include "fio.h"
 
-static void fill_random_bytes(struct thread_data *td,
-                             unsigned char *p, unsigned int len)
+static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
 {
        unsigned int todo;
        int r;
@@ -32,6 +31,57 @@ static void fill_random_bytes(struct thread_data *td,
        }
 }
 
+static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
+{
+       switch (td->o.verify_pattern_bytes) {
+       case 0:
+               fill_random_bytes(td, p, len);
+               break;
+       case 1:
+               memset(p, td->o.verify_pattern, len);
+               break;
+       case 2:
+       case 3:
+       case 4: {
+               unsigned int pattern = td->o.verify_pattern;
+               unsigned int i = 0;
+               unsigned char c1, c2, c3, c4;
+               unsigned char *b = p;
+
+               c1 = pattern & 0xff;
+               pattern >>= 8;
+               c2 = pattern & 0xff;
+               pattern >>= 8;
+               c3 = pattern & 0xff;
+               pattern >>= 8;
+               c4 = pattern & 0xff;
+
+               while (i < len) {
+                       b[i++] = c1;
+                       if (i == len)
+                               break;
+                       b[i++] = c2;
+                       if (td->o.verify_pattern_bytes == 2 || i == len)
+                               continue;
+                       b[i++] = c3;
+                       if (td->o.verify_pattern_bytes == 3 || i == len)
+                               continue;
+                       b[i++] = c4;
+               }
+               break;
+               }
+       }
+}
+
+static void memswp(void* buf1, void* buf2, unsigned int len)
+{
+       struct verify_header swap;
+
+       memcpy(&swap, buf1, len);
+       memcpy(buf1, buf2, len);
+       memcpy(buf2, &swap, len);
+}
+
 static void hexdump(void *buffer, int len)
 {
        unsigned char *p = buffer;
@@ -42,13 +92,22 @@ static void hexdump(void *buffer, int len)
        log_info("\n");
 }
 
+/*
+ * Return data area 'header_num'
+ */
+static inline void *io_u_verify_off(struct verify_header *hdr,
+                                   struct io_u *io_u,
+                                   unsigned char header_num)
+{
+       return io_u->buf + sizeof(*hdr) + header_num * hdr->len;
+}
+
 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
                             unsigned char header_num)
 {
-       unsigned char *p = io_u->buf;
+       void *p = io_u_verify_off(hdr, io_u, header_num);
        unsigned char c;
 
-       p += header_num * hdr->len + sizeof(*hdr);
        c = crc7(p, hdr->len - sizeof(*hdr));
 
        if (c != hdr->crc7) {
@@ -65,10 +124,9 @@ static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
                              unsigned int header_num)
 {
-       unsigned char *p = io_u->buf;
+       void *p = io_u_verify_off(hdr, io_u, header_num);
        unsigned short c;
 
-       p += header_num * hdr->len + sizeof(*hdr);
        c = crc16(p, hdr->len - sizeof(*hdr));
 
        if (c != hdr->crc16) {
@@ -85,10 +143,9 @@ static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
                              unsigned int header_num)
 {
-       unsigned char *p = io_u->buf;
+       void *p = io_u_verify_off(hdr, io_u, header_num);
        unsigned long long c;
 
-       p += header_num * hdr->len + sizeof(*hdr);
        c = crc64(p, hdr->len - sizeof(*hdr));
 
        if (c != hdr->crc64) {
@@ -105,10 +162,9 @@ static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
                             unsigned int header_num)
 {
-       unsigned char *p = io_u->buf;
+       void *p = io_u_verify_off(hdr, io_u, header_num);
        unsigned long c;
 
-       p += header_num * hdr->len + sizeof(*hdr);
        c = crc32(p, hdr->len - sizeof(*hdr));
 
        if (c != hdr->crc32) {
@@ -125,16 +181,16 @@ static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
                           unsigned int header_num)
 {
-       unsigned char *p = io_u->buf;
+       void *p = io_u_verify_off(hdr, io_u, header_num);
        uint32_t hash[MD5_HASH_WORDS];
        struct md5_ctx md5_ctx = {
                .hash = hash,
        };
 
-       p += header_num * hdr->len + sizeof(*hdr);
+       md5_init(&md5_ctx);
        md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
 
-       if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
+       if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
                log_err("md5: verify failed at %llu/%u\n",
                              io_u->offset + header_num * hdr->len,
                              hdr->len);
@@ -148,20 +204,23 @@ static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
 
 int verify_io_u(struct thread_data *td, struct io_u *io_u)
 {
-       unsigned char *p = (unsigned char*) io_u->buf;
        struct verify_header *hdr;
        unsigned int hdr_inc, hdr_num = 0;
+       void *p;
        int ret;
 
        if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
                return 0;
 
        hdr_inc = io_u->buflen;
-       if (td->o.header_interval)
-               hdr_inc = td->o.header_interval;
+       if (td->o.verify_interval)
+               hdr_inc = td->o.verify_interval;
+
+       for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
+               if (td->o.verify_offset)
+                       memswp(p, p + td->o.verify_offset, sizeof(*hdr));
 
-       for (; p < (unsigned char*) io_u->buf + io_u->buflen; p += hdr_inc) {
-               hdr = (struct verify_header*) p;
+               hdr = p;
 
                if (hdr->fio_magic != FIO_HDR_MAGIC) {
                        log_err("Bad verify header %x\n", hdr->fio_magic);
@@ -220,6 +279,7 @@ static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
                .hash = (uint32_t *) hdr->md5_digest,
        };
 
+       md5_init(&md5_ctx);
        md5_update(&md5_ctx, p, len);
 }
 
@@ -229,27 +289,28 @@ static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
  */
 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
 {
-       const unsigned int len = io_u->buflen - sizeof(struct verify_header);
        struct verify_header *hdr;
-       unsigned char *p = io_u->buf, *data;
-       unsigned int data_len;
+       void *p = io_u->buf, *data;
+       unsigned int hdr_inc, data_len;
 
        if (td->o.verify == VERIFY_NULL)
                return;
 
-       fill_random_bytes(td, p, len);
+       fill_pattern(td, p, io_u->buflen);
+
+       hdr_inc = io_u->buflen;
+       if (td->o.verify_interval)
+               hdr_inc = td->o.verify_interval;
+       data_len = hdr_inc - sizeof(*hdr);
 
-       for (;p < (unsigned char*) io_u->buf + io_u->buflen; p += hdr->len) {
-               hdr = (struct verify_header*) p;
+       for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
+               hdr = p;
 
                hdr->fio_magic = FIO_HDR_MAGIC;
                hdr->verify_type = td->o.verify;
-               hdr->len = io_u->buflen;
-               if (td->o.header_interval)
-                       hdr->len = td->o.header_interval;
+               hdr->len = hdr_inc;
 
                data = p + sizeof(*hdr);
-               data_len = hdr->len - sizeof(*hdr);
                switch (td->o.verify) {
                case VERIFY_MD5:
                        fill_md5(hdr, data, data_len);
@@ -270,6 +331,8 @@ void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
                        log_err("fio: bad verify type: %d\n", td->o.verify);
                        assert(0);
                }
+               if (td->o.verify_offset)
+                       memswp(p, p + td->o.verify_offset, sizeof(*hdr));
        }
 }