When reading in buffers from disk, set a new BH_Verify buffer head flag.  In
gfs2_metatype_check, skip the check if BH_Verify is cleared and clear the flag
when checking.  That way, we'll only check the metatype once when reading
buffers from disk, and not when the buffers were already in the page cache.

While touching this code, convert two 'be32_to_cpu(magic) == GFS2_MAGIC' checks
into 'magic == cpu_to_be32(GFS2_MAGIC)'.

Signed-off-by: Andreas Gruenbacher <agrue...@redhat.com>
---
 fs/gfs2/incore.h  |  5 ++++-
 fs/gfs2/meta_io.c | 10 ++++++++--
 fs/gfs2/util.c    | 31 ++++++++++++++++++++++++-------
 fs/gfs2/util.h    | 24 ++++++------------------
 4 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/fs/gfs2/incore.h b/fs/gfs2/incore.h
index d39c26b950121..40644e1207969 100644
--- a/fs/gfs2/incore.h
+++ b/fs/gfs2/incore.h
@@ -126,13 +126,16 @@ struct gfs2_rgrpd {
 
 enum gfs2_state_bits {
        BH_Pinned = BH_PrivateStart,
-       BH_Escaped = BH_PrivateStart + 1,
+       BH_Escaped,
+       BH_Verify
 };
 
 BUFFER_FNS(Pinned, pinned)
 TAS_BUFFER_FNS(Pinned, pinned)
 BUFFER_FNS(Escaped, escaped)
 TAS_BUFFER_FNS(Escaped, escaped)
+BUFFER_FNS(Verify, verify)
+TAS_BUFFER_FNS(Verify, verify)
 
 struct gfs2_bufdata {
        struct buffer_head *bd_bh;
diff --git a/fs/gfs2/meta_io.c b/fs/gfs2/meta_io.c
index be9c0bf697fe6..a111e12a5e1b6 100644
--- a/fs/gfs2/meta_io.c
+++ b/fs/gfs2/meta_io.c
@@ -268,6 +268,7 @@ int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int 
flags,
        } else {
                bh->b_end_io = end_buffer_read_sync;
                get_bh(bh);
+               set_buffer_verify(bh);
                bhs[num++] = bh;
        }
 
@@ -280,6 +281,7 @@ int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int 
flags,
                        brelse(bh);
                } else {
                        bh->b_end_io = end_buffer_read_sync;
+                       set_buffer_verify(bh);
                        bhs[num++] = bh;
                }
        }
@@ -452,8 +454,10 @@ struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, 
u64 dblock, u32 extlen)
 
        if (buffer_uptodate(first_bh))
                goto out;
-       if (!buffer_locked(first_bh))
+       if (!buffer_locked(first_bh)) {
+               set_buffer_verify(first_bh);
                ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &first_bh);
+       }
 
        dblock++;
        extlen--;
@@ -461,10 +465,12 @@ struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, 
u64 dblock, u32 extlen)
        while (extlen) {
                bh = gfs2_getbuf(gl, dblock, CREATE);
 
-               if (!buffer_uptodate(bh) && !buffer_locked(bh))
+               if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
+                       set_buffer_verify(bh);
                        ll_rw_block(REQ_OP_READ,
                                    REQ_RAHEAD | REQ_META | REQ_PRIO,
                                    1, &bh);
+               }
                brelse(bh);
                dblock++;
                extlen--;
diff --git a/fs/gfs2/util.c b/fs/gfs2/util.c
index b4c72bb799052..2fb4e83bd3efa 100644
--- a/fs/gfs2/util.c
+++ b/fs/gfs2/util.c
@@ -202,20 +202,37 @@ int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct 
buffer_head *bh,
 }
 
 /**
- * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw
+ * gfs2_metatype_check_ii - Check the metadata type of a block
+ *
+ * Report and withdraw on inconsistencies.
+ *
  * Returns: -1 if this call withdrew the machine,
  *          -2 if it was already withdrawn
  */
 
 int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
-                          u16 type, u16 t, void *caller)
+                          u16 type, void *caller)
 {
+       struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
        int me;
-       me = gfs2_lm_withdraw(sdp,
-                             "fatal: invalid metadata block at %pS\n"
-                             "  bh = %llu (type: exp=%u, found=%u)\n",
-                             caller,
-                             (unsigned long long)bh->b_blocknr, type, t);
+
+       clear_buffer_verify(bh);
+
+       if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
+               me = gfs2_lm_withdraw(sdp,
+                                     "fatal: invalid metadata block at %pS\n"
+                                     "  bh = %llu (magic number)\n",
+                                     caller,
+                                     (unsigned long long)bh->b_blocknr);
+       } else if (unlikely(be32_to_cpu(mh->mh_type) != type)) {
+               me = gfs2_lm_withdraw(sdp,
+                                     "fatal: invalid metadata block at %pS\n"
+                                     "  bh = %llu (type: exp=%u, found=%u)\n",
+                                     caller,
+                                     (unsigned long long)bh->b_blocknr, type,
+                                     be32_to_cpu(mh->mh_type));
+       } else
+               return 0;
        return (me) ? -1 : -2;
 }
 
diff --git a/fs/gfs2/util.h b/fs/gfs2/util.h
index 453d814c1efa3..0432b0aedefc2 100644
--- a/fs/gfs2/util.h
+++ b/fs/gfs2/util.h
@@ -55,15 +55,11 @@ int gfs2_consist(struct gfs2_sbd *sdp);
 int gfs2_consist_inode(struct gfs2_inode *ip);
 int gfs2_consist_rgrpd(struct gfs2_rgrpd *rgd);
 
-int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
-                      const char *type, void *caller);
-
 static inline int gfs2_meta_check(struct gfs2_sbd *sdp,
                                    struct buffer_head *bh)
 {
        struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
-       u32 magic = be32_to_cpu(mh->mh_magic);
-       if (unlikely(magic != GFS2_MAGIC)) {
+       if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
                fs_err(sdp, "Magic number missing at %llu\n",
                       (unsigned long long)bh->b_blocknr);
                return -EIO;
@@ -72,22 +68,14 @@ static inline int gfs2_meta_check(struct gfs2_sbd *sdp,
 }
 
 int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
-                          u16 type, u16 t,
-                          void *caller);
+                          u16 type, void *caller);
 
 static inline int gfs2_metatype_check(struct gfs2_sbd *sdp,
-                                       struct buffer_head *bh,
-                                       u16 type)
+                                     struct buffer_head *bh, u16 type)
 {
-       struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
-       u32 magic = be32_to_cpu(mh->mh_magic);
-       u16 t = be32_to_cpu(mh->mh_type);
-       if (unlikely(magic != GFS2_MAGIC))
-               return gfs2_meta_check_ii(sdp, bh, "magic number", (void 
*)_RET_IP_);
-        if (unlikely(t != type))
-               return gfs2_metatype_check_ii(sdp, bh, type, t,
-                                             (void *)_RET_IP_);
-       return 0;
+       if (!buffer_verify(bh))
+               return 0;
+       return gfs2_metatype_check_ii(sdp, bh, type, (void *)_RET_IP_);
 }
 
 static inline void gfs2_metatype_set(struct buffer_head *bh, u16 type,
-- 
2.20.1

Reply via email to