Remove holes in verify_header structure
[fio.git] / verify.c
index 91a9077ac272d2ffcc04f5b4e7e00a2eb879944e..696aff762abce69849e254cce47b6accd4fa9dd7 100644 (file)
--- a/verify.c
+++ b/verify.c
@@ -39,38 +39,24 @@ void fill_pattern(struct thread_data *td, void *p, unsigned int len, struct io_u
                        io_u->rand_seed = fill_random_buf(&td->buf_state, p, len);
                break;
        case 1:
-               /*
-                * See below write barrier comment
-                */
-#if 0
-               read_barrier();
                if (io_u->buf_filled_len >= len) {
                        dprint(FD_VERIFY, "using already filled verify pattern b=0 len=%u\n", len);
                        return;
                }
-#endif
                dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
                memset(p, td->o.verify_pattern[0], len);
-               /*
-                * We need to ensure that the pattern stores are seen before
-                * the fill length store, or we could observe headers that
-                * aren't valid to the extent notified by the fill length
-                */
-               write_barrier();
                io_u->buf_filled_len = len;
                break;
        default: {
                unsigned int i = 0, size = 0;
                unsigned char *b = p;
 
-#if 0
-               read_barrier();
                if (io_u->buf_filled_len >= len) {
                        dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
                                        td->o.verify_pattern_bytes, len);
                        return;
                }
-#endif
+
                dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
                                        td->o.verify_pattern_bytes, len);
 
@@ -81,7 +67,6 @@ void fill_pattern(struct thread_data *td, void *p, unsigned int len, struct io_u
                        memcpy(b+i, td->o.verify_pattern, size);
                        i += size;
                }
-               write_barrier();
                io_u->buf_filled_len = len;
                break;
                }
@@ -337,14 +322,27 @@ static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
        struct io_u *io_u = vc->io_u;
        char *buf, *pattern;
        unsigned int header_size = __hdr_size(td->o.verify);
-       unsigned int len, mod, i;
+       unsigned int len, mod, i, size, pattern_size;
 
        pattern = td->o.verify_pattern;
+       pattern_size = td->o.verify_pattern_bytes;
+       if (pattern_size <= 1)
+               pattern_size = MAX_PATTERN_SIZE;
        buf = (void *) hdr + header_size;
        len = get_hdr_inc(td, io_u) - header_size;
-       mod = header_size % td->o.verify_pattern_bytes;
+       mod = header_size % pattern_size;
+
+       for (i = 0; i < len; i += size) {
+               size = pattern_size - mod;
+               if (size > (len - i))
+                       size = len - i;
+               if (memcmp(buf + i, pattern + mod, size))
+                       // Let the slow compare find the first mismatch byte.
+                       break;
+               mod = 0;
+       }
 
-       for (i = 0; i < len; i++) {
+       for (; i < len; i++) {
                if (buf[i] != pattern[mod]) {
                        unsigned int bits;
 
@@ -553,10 +551,7 @@ static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
 
        dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
 
-       if (hdr->verify_type == VERIFY_CRC32C_INTEL)
-               c = crc32c_intel(p, hdr->len - hdr_size(hdr));
-       else
-               c = crc32c(p, hdr->len - hdr_size(hdr));
+       c = crc32c(p, hdr->len - hdr_size(hdr));
 
        if (c == vh->crc32)
                return 0;
@@ -651,6 +646,19 @@ static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
        return ret;
 }
 
+static int verify_hdr_crc(struct verify_header *hdr)
+{
+       void *p = hdr;
+       uint32_t crc;
+
+       crc = crc32c(p, sizeof(*hdr) - sizeof(hdr->crc32));
+       if (crc == hdr->crc32)
+               return 1;
+
+       log_err("fio: verify header crc %x, calculated %x\n", hdr->crc32, crc);
+       return 0;
+}
+
 int verify_io_u(struct thread_data *td, struct io_u *io_u)
 {
        struct verify_header *hdr;
@@ -684,9 +692,9 @@ int verify_io_u(struct thread_data *td, struct io_u *io_u)
                        memswp(p, p + td->o.verify_offset, header_size);
                hdr = p;
 
-               if (hdr->fio_magic != FIO_HDR_MAGIC) {
+               if (hdr->magic != FIO_HDR_MAGIC || !verify_hdr_crc(hdr)) {
                        log_err("verify: bad magic header %x, wanted %x at file %s offset %llu, length %u\n",
-                               hdr->fio_magic, FIO_HDR_MAGIC,
+                               hdr->magic, FIO_HDR_MAGIC,
                                io_u->file->file_name,
                                io_u->offset + hdr_num * hdr->len, hdr->len);
                        return EILSEQ;
@@ -813,10 +821,7 @@ static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
 {
        struct vhdr_crc32 *vh = hdr_priv(hdr);
 
-       if (hdr->verify_type == VERIFY_CRC32C_INTEL)
-               vh->crc32 = crc32c_intel(p, len);
-       else
-               vh->crc32 = crc32c(p, len);
+       vh->crc32 = crc32c(p, len);
 }
 
 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
@@ -846,10 +851,12 @@ static void populate_hdr(struct thread_data *td, struct io_u *io_u,
 
        p = (void *) hdr;
 
-       hdr->fio_magic = FIO_HDR_MAGIC;
-       hdr->len = header_len;
+       hdr->magic = FIO_HDR_MAGIC;
        hdr->verify_type = td->o.verify;
+       hdr->len = header_len;
        hdr->rand_seed = io_u->rand_seed;
+       hdr->crc32 = crc32c(p, sizeof(*hdr) - sizeof(hdr->crc32));
+
        data_len = header_len - hdr_size(hdr);
 
        data = p + hdr_size(hdr);