From: Darrick J. Wong <darrick.wong@xxxxxxxxxx> Add a forgotten check to the AGI verifier, then wire up the scrub infrastructure to check the AGI contents. Signed-off-by: Darrick J. Wong <darrick.wong@xxxxxxxxxx> --- fs/xfs/libxfs/xfs_fs.h | 3 + fs/xfs/scrub/agheader.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/common.c | 10 ++++- fs/xfs/scrub/common.h | 1 fs/xfs/xfs_trace.h | 3 + 5 files changed, 109 insertions(+), 4 deletions(-) diff --git a/fs/xfs/libxfs/xfs_fs.h b/fs/xfs/libxfs/xfs_fs.h index 2fb0de2..f509dcbf 100644 --- a/fs/xfs/libxfs/xfs_fs.h +++ b/fs/xfs/libxfs/xfs_fs.h @@ -496,7 +496,8 @@ struct xfs_scrub_metadata { #define XFS_SCRUB_TYPE_SB 1 /* superblock */ #define XFS_SCRUB_TYPE_AGF 2 /* AG free header */ #define XFS_SCRUB_TYPE_AGFL 3 /* AG free list */ -#define XFS_SCRUB_TYPE_MAX 3 +#define XFS_SCRUB_TYPE_AGI 4 /* AG inode header */ +#define XFS_SCRUB_TYPE_MAX 4 #define XFS_SCRUB_FLAG_REPAIR 0x01 /* i: repair this metadata */ #define XFS_SCRUB_FLAG_CORRUPT 0x02 /* o: needs repair */ diff --git a/fs/xfs/scrub/agheader.c b/fs/xfs/scrub/agheader.c index 4fed3c8..127f06c 100644 --- a/fs/xfs/scrub/agheader.c +++ b/fs/xfs/scrub/agheader.c @@ -421,3 +421,99 @@ xfs_scrub_agfl( } #undef XFS_SCRUB_AGFL_OP_ERROR_GOTO #undef XFS_SCRUB_AGFL_CHECK + +/* AGI */ + +#define XFS_SCRUB_AGI_CHECK(fs_ok) \ + XFS_SCRUB_CHECK(sc, sc->sa.agi_bp, "AGI", fs_ok) +#define XFS_SCRUB_AGI_OP_ERROR_GOTO(error, label) \ + XFS_SCRUB_OP_ERROR_GOTO(sc, sc->sm->sm_agno, \ + XFS_AGI_BLOCK(sc->mp), "AGI", error, label) +/* Scrub the AGI. */ +int +xfs_scrub_agi( + struct xfs_scrub_context *sc) +{ + struct xfs_mount *mp = sc->mp; + struct xfs_agi *agi; + xfs_daddr_t daddr; + xfs_daddr_t eofs; + xfs_agnumber_t agno; + xfs_agblock_t agbno; + xfs_agblock_t eoag; + xfs_agino_t agino; + xfs_agino_t first_agino; + xfs_agino_t last_agino; + int i; + int level; + int error = 0; + + agno = sc->sm->sm_agno; + error = xfs_scrub_load_ag_headers(sc, agno, XFS_SCRUB_TYPE_AGI); + XFS_SCRUB_AGI_OP_ERROR_GOTO(&error, out); + + agi = XFS_BUF_TO_AGI(sc->sa.agi_bp); + eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); + + /* Check the AG length */ + eoag = be32_to_cpu(agi->agi_length); + XFS_SCRUB_AGI_CHECK(eoag == xfs_scrub_ag_blocks(mp, agno)); + + /* Check btree roots and levels */ + agbno = be32_to_cpu(agi->agi_root); + daddr = XFS_AGB_TO_DADDR(mp, agno, agbno); + XFS_SCRUB_AGI_CHECK(agbno > XFS_AGI_BLOCK(mp)); + XFS_SCRUB_AGI_CHECK(agbno < mp->m_sb.sb_agblocks); + XFS_SCRUB_AGI_CHECK(agbno < eoag); + XFS_SCRUB_AGI_CHECK(daddr < eofs); + + level = be32_to_cpu(agi->agi_level); + XFS_SCRUB_AGI_CHECK(level > 0); + XFS_SCRUB_AGI_CHECK(level <= XFS_BTREE_MAXLEVELS); + + if (xfs_sb_version_hasfinobt(&mp->m_sb)) { + agbno = be32_to_cpu(agi->agi_free_root); + daddr = XFS_AGB_TO_DADDR(mp, agno, agbno); + XFS_SCRUB_AGI_CHECK(agbno > XFS_AGI_BLOCK(mp)); + XFS_SCRUB_AGI_CHECK(agbno < mp->m_sb.sb_agblocks); + XFS_SCRUB_AGI_CHECK(agbno < eoag); + XFS_SCRUB_AGI_CHECK(daddr < eofs); + + level = be32_to_cpu(agi->agi_free_level); + XFS_SCRUB_AGI_CHECK(level > 0); + XFS_SCRUB_AGI_CHECK(level <= XFS_BTREE_MAXLEVELS); + } + + /* Check inode counters */ + first_agino = XFS_OFFBNO_TO_AGINO(mp, XFS_AGI_BLOCK(mp) + 1, 0); + last_agino = XFS_OFFBNO_TO_AGINO(mp, eoag + 1, 0) - 1; + agino = be32_to_cpu(agi->agi_count); + XFS_SCRUB_AGI_CHECK(agino <= last_agino - first_agino + 1); + XFS_SCRUB_AGI_CHECK(agino >= be32_to_cpu(agi->agi_freecount)); + + /* Check inode pointers */ + agino = be32_to_cpu(agi->agi_newino); + if (agino != NULLAGINO) { + XFS_SCRUB_AGI_CHECK(agino >= first_agino); + XFS_SCRUB_AGI_CHECK(agino <= last_agino); + } + agino = be32_to_cpu(agi->agi_dirino); + if (agino != NULLAGINO) { + XFS_SCRUB_AGI_CHECK(agino >= first_agino); + XFS_SCRUB_AGI_CHECK(agino <= last_agino); + } + + /* Check unlinked inode buckets */ + for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) { + agino = be32_to_cpu(agi->agi_unlinked[i]); + if (agino == NULLAGINO) + continue; + XFS_SCRUB_AGI_CHECK(agino >= first_agino); + XFS_SCRUB_AGI_CHECK(agino <= last_agino); + } + +out: + return error; +} +#undef XFS_SCRUB_AGI_CHECK +#undef XFS_SCRUB_AGI_OP_ERROR_GOTO diff --git a/fs/xfs/scrub/common.c b/fs/xfs/scrub/common.c index 3bfaa65..e47d795 100644 --- a/fs/xfs/scrub/common.c +++ b/fs/xfs/scrub/common.c @@ -612,12 +612,14 @@ xfs_scrub_load_ag_headers( struct xfs_mount *mp = sc->mp; int error; - ASSERT(type == XFS_SCRUB_TYPE_AGF || type == XFS_SCRUB_TYPE_AGFL); + ASSERT(type == XFS_SCRUB_TYPE_AGF || type == XFS_SCRUB_TYPE_AGFL || + type == XFS_SCRUB_TYPE_AGI); memset(&sc->sa, 0, sizeof(sc->sa)); sc->sa.agno = agno; error = xfs_scrub_load_ag_header(sc, XFS_AGI_DADDR(mp), - &sc->sa.agi_bp, &xfs_agi_buf_ops, false); + &sc->sa.agi_bp, &xfs_agi_buf_ops, + type == XFS_SCRUB_TYPE_AGI); if (error) return error; @@ -655,6 +657,10 @@ static const struct xfs_scrub_meta_fns meta_scrub_fns[] = { .setup = xfs_scrub_setup_ag_header, .scrub = xfs_scrub_agfl, }, + { /* agi */ + .setup = xfs_scrub_setup_ag_header, + .scrub = xfs_scrub_agi, + }, }; /* Dispatch metadata scrubbing. */ diff --git a/fs/xfs/scrub/common.h b/fs/xfs/scrub/common.h index 5514afd..4decfef 100644 --- a/fs/xfs/scrub/common.h +++ b/fs/xfs/scrub/common.h @@ -205,6 +205,7 @@ SETUP_FN(xfs_scrub_setup_ag_header); SCRUB_FN(xfs_scrub_superblock); SCRUB_FN(xfs_scrub_agf); SCRUB_FN(xfs_scrub_agfl); +SCRUB_FN(xfs_scrub_agi); #undef SCRUB_FN #endif /* __XFS_REPAIR_COMMON_H__ */ diff --git a/fs/xfs/xfs_trace.h b/fs/xfs/xfs_trace.h index f3f7cb8..23e0d56 100644 --- a/fs/xfs/xfs_trace.h +++ b/fs/xfs/xfs_trace.h @@ -3333,7 +3333,8 @@ DEFINE_GETFSMAP_EVENT(xfs_getfsmap_mapping); { XFS_SCRUB_TYPE_TEST, "dummy" }, \ { XFS_SCRUB_TYPE_SB, "superblock" }, \ { XFS_SCRUB_TYPE_AGF, "AGF" }, \ - { XFS_SCRUB_TYPE_AGFL, "AGFL" } + { XFS_SCRUB_TYPE_AGFL, "AGFL" }, \ + { XFS_SCRUB_TYPE_AGI, "AGI" } DECLARE_EVENT_CLASS(xfs_scrub_class, TP_PROTO(struct xfs_inode *ip, struct xfs_scrub_metadata *sm, int error), -- To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html