]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
xfs: switch perag iteration from the for_each macros to a while based iterator
authorChristoph Hellwig <hch@lst.de>
Mon, 25 Nov 2024 21:14:14 +0000 (13:14 -0800)
committerDarrick J. Wong <djwong@kernel.org>
Thu, 28 Nov 2024 02:33:09 +0000 (18:33 -0800)
Source kernel commit: 86437e6abbd2ef040f42ef190264819db6118415

The current for_each_perag* macros are a bit annoying in that they
require the caller to both provide an object and an index iterator, and
also somewhat obsfucate the underlying control flow mechanism.

Switch to open coded while loops using new xfs_perag_next{,_from,_range}
helpers that return the next pag structure to iterate on based on the
previous one or NULL for the loop start.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
db/fsmap.c
db/info.c
db/iunlink.c
libxfs/xfs_ag.h
libxfs/xfs_sb.c
libxfs/xfs_types.c
repair/bmap_repair.c
repair/phase2.c
repair/phase5.c

index 923d7568b9d9770147c769db4af3e6930ade22e4..a9259c4632185b74f05a53bb2f235f78709e5ec2 100644 (file)
@@ -46,7 +46,7 @@ fsmap(
        struct xfs_rmap_irec    high = {0};
        struct xfs_btree_cur    *bt_cur;
        struct xfs_buf          *agbp;
-       struct xfs_perag        *pag;
+       struct xfs_perag        *pag = NULL;
        int                     error;
 
        eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
@@ -63,7 +63,7 @@ fsmap(
        end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);
 
        info.nr = 0;
-       for_each_perag_range(mp, start_ag, end_ag, pag) {
+       while ((pag = xfs_perag_next_range(mp, pag, start_ag, end_ag))) {
                if (pag_agno(pag) == end_ag)
                        high.rm_startblock = XFS_FSB_TO_AGBNO(mp, end_fsb);
 
index 6a8765ec761a4907a97ffd919190f665de22d101..9a86d247839f846c051d05a29c5ec394463abe32 100644 (file)
--- a/db/info.c
+++ b/db/info.c
@@ -104,8 +104,7 @@ agresv_f(
        int                     argc,
        char                    **argv)
 {
-       struct xfs_perag        *pag;
-       xfs_agnumber_t          agno;
+       struct xfs_perag        *pag = NULL;
        int                     i;
 
        if (argc > 1) {
@@ -134,7 +133,7 @@ agresv_f(
                return 0;
        }
 
-       for_each_perag(mp, agno, pag)
+       while ((pag = xfs_perag_next(mp, pag)))
                print_agresv_info(pag);
 
        return 0;
index c9977a859e28423c113833a0d7c3564619859df1..c73f818242b9837a2017599e81907f3739500017 100644 (file)
@@ -144,7 +144,7 @@ dump_iunlinked_f(
        int                     argc,
        char                    **argv)
 {
-       struct xfs_perag        *pag;
+       struct xfs_perag        *pag = NULL;
        xfs_agnumber_t          agno = NULLAGNUMBER;
        unsigned int            bucket = -1U;
        bool                    quiet = false;
@@ -189,7 +189,7 @@ dump_iunlinked_f(
                return 0;
        }
 
-       for_each_perag(mp, agno, pag)
+       while ((pag = xfs_perag_next(mp, pag)))
                dump_unlinked(pag, bucket, quiet, verbose);
 
        return 0;
index 69b934ad2c4aad6ee5a63ab9b913d6ce0f945bf9..80969682dc4746ef886ddd0eace4d2df797e114d 100644 (file)
@@ -208,6 +208,34 @@ xfs_perag_rele(
        xfs_group_rele(pag_group(pag));
 }
 
+static inline struct xfs_perag *
+xfs_perag_next_range(
+       struct xfs_mount        *mp,
+       struct xfs_perag        *pag,
+       xfs_agnumber_t          start_agno,
+       xfs_agnumber_t          end_agno)
+{
+       return to_perag(xfs_group_next_range(mp, pag ? pag_group(pag) : NULL,
+                       start_agno, end_agno, XG_TYPE_AG));
+}
+
+static inline struct xfs_perag *
+xfs_perag_next_from(
+       struct xfs_mount        *mp,
+       struct xfs_perag        *pag,
+       xfs_agnumber_t          start_agno)
+{
+       return xfs_perag_next_range(mp, pag, start_agno, mp->m_sb.sb_agcount - 1);
+}
+
+static inline struct xfs_perag *
+xfs_perag_next(
+       struct xfs_mount        *mp,
+       struct xfs_perag        *pag)
+{
+       return xfs_perag_next_from(mp, pag, 0);
+}
+
 /*
  * Per-ag geometry infomation and validation
  */
@@ -273,40 +301,6 @@ xfs_ag_contains_log(struct xfs_mount *mp, xfs_agnumber_t agno)
               agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
 }
 
-/*
- * Perag iteration APIs
- */
-static inline struct xfs_perag *
-xfs_perag_next(
-       struct xfs_perag        *pag,
-       xfs_agnumber_t          *agno,
-       xfs_agnumber_t          end_agno)
-{
-       struct xfs_mount        *mp = pag_mount(pag);
-
-       *agno = pag_agno(pag) + 1;
-       xfs_perag_rele(pag);
-       while (*agno <= end_agno) {
-               pag = xfs_perag_grab(mp, *agno);
-               if (pag)
-                       return pag;
-               (*agno)++;
-       }
-       return NULL;
-}
-
-#define for_each_perag_range(mp, agno, end_agno, pag) \
-       for ((pag) = xfs_perag_grab((mp), (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))
-
-#define for_each_perag(mp, agno, pag) \
-       (agno) = 0; \
-       for_each_perag_from((mp), (agno), (pag))
-
 static inline struct xfs_perag *
 xfs_perag_next_wrap(
        struct xfs_perag        *pag,
index f534ae5d4c4db2f3758337d84181b57d8df4825b..d32f789037389f0589b1b30511dce5079e146e83 100644 (file)
@@ -1120,14 +1120,13 @@ int
 xfs_update_secondary_sbs(
        struct xfs_mount        *mp)
 {
-       struct xfs_perag        *pag;
-       xfs_agnumber_t          agno = 1;
+       struct xfs_perag        *pag = NULL;
        int                     saved_error = 0;
        int                     error = 0;
        LIST_HEAD               (buffer_list);
 
        /* update secondary superblocks. */
-       for_each_perag_from(mp, agno, pag) {
+       while ((pag = xfs_perag_next_from(mp, pag, 1))) {
                struct xfs_buf          *bp;
 
                error = xfs_buf_get(mp->m_ddev_targp,
@@ -1157,7 +1156,7 @@ xfs_update_secondary_sbs(
                xfs_buf_relse(bp);
 
                /* don't hold too many buffers at once */
-               if (agno % 16)
+               if (pag_agno(pag) % 16)
                        continue;
 
                error = xfs_buf_delwri_submit(&buffer_list);
@@ -1171,12 +1170,8 @@ xfs_update_secondary_sbs(
                }
        }
        error = xfs_buf_delwri_submit(&buffer_list);
-       if (error) {
-               xfs_warn(mp,
-               "write error %d updating a secondary superblock near ag %d",
-                       error, agno);
-       }
-
+       if (error)
+               xfs_warn(mp, "error %d writing secondary superblocks", error);
        return saved_error ? saved_error : error;
 }
 
index 74ab1965a8f49c9b4c5f71ff2263b272cb47ea02..0d1b86ae59d93e3d6122e3282c7d18356fcf536f 100644 (file)
@@ -170,13 +170,12 @@ xfs_icount_range(
        unsigned long long      *max)
 {
        unsigned long long      nr_inos = 0;
-       struct xfs_perag        *pag;
-       xfs_agnumber_t          agno;
+       struct xfs_perag        *pag = NULL;
 
        /* root, rtbitmap, rtsum all live in the first chunk */
        *min = XFS_INODES_PER_CHUNK;
 
-       for_each_perag(mp, agno, pag)
+       while ((pag = xfs_perag_next(mp, pag)))
                nr_inos += pag->agino_max - pag->agino_min + 1;
        *max = nr_inos;
 }
index 7ccbb96fa8dc5ec28403164756f45b3d90607ba5..3a214c85a1de5ff89b5d13bd41403018cf41a05c 100644 (file)
@@ -218,12 +218,11 @@ STATIC int
 xrep_bmap_find_mappings(
        struct xrep_bmap        *rb)
 {
-       struct xfs_perag        *pag;
-       xfs_agnumber_t          agno;
+       struct xfs_perag        *pag = NULL;
        int                     error;
 
        /* Iterate the rmaps for extents. */
-       for_each_perag(rb->sc->mp, agno, pag) {
+       while ((pag = xfs_perag_next(rb->sc->mp, pag))) {
                error = xrep_bmap_scan_ag(rb, pag);
                if (error) {
                        libxfs_perag_put(pag);
index 42a2861dcc37147fff1f5811b0e198c7be0e5647..17966bb54db09d639a1ea841a2db42ae0acee232 100644 (file)
@@ -274,12 +274,11 @@ check_fs_free_space(
        const struct check_state        *old,
        struct xfs_sb                   *new_sb)
 {
-       struct xfs_perag                *pag;
-       xfs_agnumber_t                  agno;
+       struct xfs_perag                *pag = NULL;
        int                             error;
 
        /* Make sure we have enough space for per-AG reservations. */
-       for_each_perag(mp, agno, pag) {
+       while ((pag = xfs_perag_next(mp, pag))) {
                struct xfs_trans        *tp;
                struct xfs_agf          *agf;
                struct xfs_buf          *agi_bp, *agf_bp;
@@ -365,7 +364,7 @@ check_fs_free_space(
         * uninitialized so that we don't trip over stale cached counters
         * after the upgrade/
         */
-       for_each_perag(mp, agno, pag) {
+       while ((pag = xfs_perag_next(mp, pag))) {
                libxfs_ag_resv_free(pag);
                clear_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
                clear_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
index fdbd9f56998fb471369f22c77415648556ddd489..86b1f681a72bb8c7b8f87c46ce44ffa8026c5a5d 100644 (file)
@@ -632,7 +632,7 @@ void
 phase5(xfs_mount_t *mp)
 {
        struct bitmap           *lost_blocks = NULL;
-       struct xfs_perag        *pag;
+       struct xfs_perag        *pag = NULL;
        xfs_agnumber_t          agno;
        int                     error;
 
@@ -679,7 +679,7 @@ phase5(xfs_mount_t *mp)
        if (error)
                do_error(_("cannot alloc lost block bitmap\n"));
 
-       for_each_perag(mp, agno, pag)
+       while ((pag = xfs_perag_next(mp, pag)))
                phase5_func(mp, pag, lost_blocks);
 
        print_final_rpt();