/*
  * Perag iteration APIs
- *
- * XXX: for_each_perag_range() usage really needs an iterator to clean up when
- * we terminate at end_agno because we may have taken a reference to the perag
- * beyond end_agno. Right now callers have to be careful to catch and clean that
- * up themselves. This is not necessary for the callers of for_each_perag() and
- * for_each_perag_from() because they terminate at sb_agcount where there are
- * no perag structures in tree beyond end_agno.
  */
 static inline struct xfs_perag *
 xfs_perag_next(
        struct xfs_perag        *pag,
-       xfs_agnumber_t          *agno)
+       xfs_agnumber_t          *agno,
+       xfs_agnumber_t          end_agno)
 {
        struct xfs_mount        *mp = pag->pag_mount;
 
        *agno = pag->pag_agno + 1;
        xfs_perag_put(pag);
+       if (*agno > end_agno)
+               return NULL;
        return xfs_perag_get(mp, *agno);
 }
 
 #define for_each_perag_range(mp, agno, end_agno, pag) \
        for ((pag) = xfs_perag_get((mp), (agno)); \
-               (pag) != NULL && (agno) <= (end_agno); \
-               (pag) = xfs_perag_next((pag), &(agno)))
+               (pag) != NULL; \
+               (pag) = xfs_perag_next((pag), &(agno), (end_agno)))
 
 #define for_each_perag_from(mp, agno, pag) \
        for_each_perag_range((mp), (agno), (mp)->m_sb.sb_agcount - 1, (pag))