#include "xfs_trans.h"
 #include "xfs_sb.h"
 #include "xfs_inode.h"
+#include "xfs_alloc.h"
 #include "scrub/xfs_scrub.h"
 #include "scrub/scrub.h"
 #include "scrub/common.h"
        return xfs_scrub_setup_fs(sc, ip);
 }
 
+/* Walk all the blocks in the AGFL. */
+int
+xfs_scrub_walk_agfl(
+       struct xfs_scrub_context        *sc,
+       int                             (*fn)(struct xfs_scrub_context *,
+                                             xfs_agblock_t bno, void *),
+       void                            *priv)
+{
+       struct xfs_agf                  *agf;
+       __be32                          *agfl_bno;
+       struct xfs_mount                *mp = sc->mp;
+       unsigned int                    flfirst;
+       unsigned int                    fllast;
+       int                             i;
+       int                             error;
+
+       agf = XFS_BUF_TO_AGF(sc->sa.agf_bp);
+       agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, sc->sa.agfl_bp);
+       flfirst = be32_to_cpu(agf->agf_flfirst);
+       fllast = be32_to_cpu(agf->agf_fllast);
+
+       /* Nothing to walk in an empty AGFL. */
+       if (agf->agf_flcount == cpu_to_be32(0))
+               return 0;
+
+       /* first to last is a consecutive list. */
+       if (fllast >= flfirst) {
+               for (i = flfirst; i <= fllast; i++) {
+                       error = fn(sc, be32_to_cpu(agfl_bno[i]), priv);
+                       if (error)
+                               return error;
+                       if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
+                               return error;
+               }
+
+               return 0;
+       }
+
+       /* first to the end */
+       for (i = flfirst; i < XFS_AGFL_SIZE(mp); i++) {
+               error = fn(sc, be32_to_cpu(agfl_bno[i]), priv);
+               if (error)
+                       return error;
+               if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
+                       return error;
+       }
+
+       /* the start to last. */
+       for (i = 0; i <= fllast; i++) {
+               error = fn(sc, be32_to_cpu(agfl_bno[i]), priv);
+               if (error)
+                       return error;
+               if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
+                       return error;
+       }
+
+       return 0;
+}
+
 /* Superblock */
 
 /*
 
        return error;
 }
+
+/* AGF */
+
+/* Scrub the AGF. */
+int
+xfs_scrub_agf(
+       struct xfs_scrub_context        *sc)
+{
+       struct xfs_mount                *mp = sc->mp;
+       struct xfs_agf                  *agf;
+       xfs_agnumber_t                  agno;
+       xfs_agblock_t                   agbno;
+       xfs_agblock_t                   eoag;
+       xfs_agblock_t                   agfl_first;
+       xfs_agblock_t                   agfl_last;
+       xfs_agblock_t                   agfl_count;
+       xfs_agblock_t                   fl_count;
+       int                             level;
+       int                             error = 0;
+
+       agno = sc->sa.agno = sc->sm->sm_agno;
+       error = xfs_scrub_ag_read_headers(sc, agno, &sc->sa.agi_bp,
+                       &sc->sa.agf_bp, &sc->sa.agfl_bp);
+       if (!xfs_scrub_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
+               goto out;
+
+       agf = XFS_BUF_TO_AGF(sc->sa.agf_bp);
+
+       /* Check the AG length */
+       eoag = be32_to_cpu(agf->agf_length);
+       if (eoag != xfs_ag_block_count(mp, agno))
+               xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+
+       /* Check the AGF btree roots and levels */
+       agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
+       if (!xfs_verify_agbno(mp, agno, agbno))
+               xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+
+       agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
+       if (!xfs_verify_agbno(mp, agno, agbno))
+               xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+
+       level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
+       if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
+               xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+
+       level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
+       if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
+               xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+
+       if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
+               agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
+               if (!xfs_verify_agbno(mp, agno, agbno))
+                       xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+
+               level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
+               if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
+                       xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+       }
+
+       if (xfs_sb_version_hasreflink(&mp->m_sb)) {
+               agbno = be32_to_cpu(agf->agf_refcount_root);
+               if (!xfs_verify_agbno(mp, agno, agbno))
+                       xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+
+               level = be32_to_cpu(agf->agf_refcount_level);
+               if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
+                       xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+       }
+
+       /* Check the AGFL counters */
+       agfl_first = be32_to_cpu(agf->agf_flfirst);
+       agfl_last = be32_to_cpu(agf->agf_fllast);
+       agfl_count = be32_to_cpu(agf->agf_flcount);
+       if (agfl_last > agfl_first)
+               fl_count = agfl_last - agfl_first + 1;
+       else
+               fl_count = XFS_AGFL_SIZE(mp) - agfl_first + agfl_last + 1;
+       if (agfl_count != 0 && fl_count != agfl_count)
+               xfs_scrub_block_set_corrupt(sc, sc->sa.agf_bp);
+
+out:
+       return error;
+}
+
+/* AGFL */
+
+/* Scrub an AGFL block. */
+STATIC int
+xfs_scrub_agfl_block(
+       struct xfs_scrub_context        *sc,
+       xfs_agblock_t                   agbno,
+       void                            *priv)
+{
+       struct xfs_mount                *mp = sc->mp;
+       xfs_agnumber_t                  agno = sc->sa.agno;
+
+       if (!xfs_verify_agbno(mp, agno, agbno))
+               xfs_scrub_block_set_corrupt(sc, sc->sa.agfl_bp);
+
+       return 0;
+}
+
+/* Scrub the AGFL. */
+int
+xfs_scrub_agfl(
+       struct xfs_scrub_context        *sc)
+{
+       xfs_agnumber_t                  agno;
+       int                             error;
+
+       agno = sc->sa.agno = sc->sm->sm_agno;
+       error = xfs_scrub_ag_read_headers(sc, agno, &sc->sa.agi_bp,
+                       &sc->sa.agf_bp, &sc->sa.agfl_bp);
+       if (!xfs_scrub_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
+               goto out;
+       if (!sc->sa.agf_bp)
+               return -EFSCORRUPTED;
+
+       /* Check the blocks in the AGFL. */
+       return xfs_scrub_walk_agfl(sc, xfs_scrub_agfl_block, NULL);
+out:
+       return error;
+}
 
  * cleaning everything up once we're through.
  */
 
+/* Decide if we want to return an AG header read failure. */
+static inline bool
+want_ag_read_header_failure(
+       struct xfs_scrub_context        *sc,
+       unsigned int                    type)
+{
+       /* Return all AG header read failures when scanning btrees. */
+       if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
+           sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL)
+               return true;
+       /*
+        * If we're scanning a given type of AG header, we only want to
+        * see read failures from that specific header.  We'd like the
+        * other headers to cross-check them, but this isn't required.
+        */
+       if (sc->sm->sm_type == type)
+               return true;
+       return false;
+}
+
 /*
  * Grab all the headers for an AG.
  *
                goto out;
 
        error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
-       if (error)
-               goto out;
-       if (!*agf) {
-               error = -ENOMEM;
+       if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
                goto out;
-       }
 
        error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
-       if (error)
+       if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
                goto out;
 
 out: