]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: perags need atomic operational state
authorDave Chinner <dchinner@redhat.com>
Tue, 9 May 2023 09:29:49 +0000 (11:29 +0200)
committerCarlos Maiolino <cem@kernel.org>
Wed, 10 May 2023 08:15:42 +0000 (10:15 +0200)
Source kernel commit: 7ac2ff8bb3713c7cb43564c04384af2ee7cc1f8d

We currently don't have any flags or operational state in the
xfs_perag except for the pagf_init and pagi_init flags. And the
agflreset flag. Oh, there's also the pagf_metadata and pagi_inodeok
flags, too.

For controlling per-ag operations, we are going to need some atomic
state flags. Hence add an opstate field similar to what we already
have in the mount and log, and convert all these state flags across
to atomic bit operations.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Allison Henderson <allison.henderson@oracle.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
13 files changed:
include/libxfs.h
libfrog/bitmask.h [new file with mode: 0644]
libxfs/init.c
libxfs/libxfs_priv.h
libxfs/xfs_ag.h
libxfs/xfs_alloc.c
libxfs/xfs_alloc_btree.c
libxfs/xfs_bmap.c
libxfs/xfs_ialloc.c
libxfs/xfs_ialloc_btree.c
libxfs/xfs_refcount_btree.c
libxfs/xfs_rmap_btree.c
repair/rmap.c

index 915bf511313551405ed7e1e2553a9e011f201daa..b28781d19d38b1b450215cd3c553247e19795ce4 100644 (file)
@@ -17,6 +17,7 @@
 #include "bitops.h"
 #include "kmem.h"
 #include "libfrog/radix-tree.h"
+#include "libfrog/bitmask.h"
 #include "atomic.h"
 #include "spinlock.h"
 
diff --git a/libfrog/bitmask.h b/libfrog/bitmask.h
new file mode 100644 (file)
index 0000000..719a6bf
--- /dev/null
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2000-2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ */
+#ifndef __LIBFROG_BITMASK_H_
+#define __LIBFROG_BITMASK_H_
+
+#define BIT_MASK(nr)   (1UL << ((nr) % BITS_PER_LONG))
+#define BIT_WORD(nr)   ((nr) / BITS_PER_LONG)
+
+static inline void set_bit(int nr, volatile unsigned long *addr)
+{
+       unsigned long mask = BIT_MASK(nr);
+       unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+       *p  |= mask;
+}
+
+static inline void clear_bit(int nr, volatile unsigned long *addr)
+{
+       unsigned long mask = BIT_MASK(nr);
+       unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+       *p &= ~mask;
+}
+
+static inline int test_bit(int nr, const volatile unsigned long *addr)
+{
+       unsigned long mask = BIT_MASK(nr);
+       unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+       return *p & mask;
+}
+
+/* Sets and returns original value of the bit */
+static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
+{
+       if (test_bit(nr, addr))
+               return 1;
+       set_bit(nr, addr);
+       return 0;
+}
+
+#endif /* __LIBFROG_BITMASK_H_ */
index ae9636a8c6c19ab64ff558506878464808b0e9ae..fda36ba0f7dc3e6d011eda8947c1b655c0cb1fbe 100644 (file)
@@ -498,6 +498,32 @@ rtmount_init(
        return 0;
 }
 
+static bool
+xfs_set_inode_alloc_perag(
+       struct xfs_perag        *pag,
+       xfs_ino_t               ino,
+       xfs_agnumber_t          max_metadata)
+{
+       if (!xfs_is_inode32(pag->pag_mount)) {
+               set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
+               clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
+               return false;
+       }
+
+       if (ino > XFS_MAXINUMBER_32) {
+               clear_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
+               clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
+               return false;
+       }
+
+       set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
+       if (pag->pag_agno < max_metadata)
+               set_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
+       else
+               clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
+       return true;
+}
+
 /*
  * Set parameters for inode allocation heuristics, taking into account
  * filesystem size and inode32/inode64 mount options; i.e. specifically
@@ -554,9 +580,9 @@ xfs_set_inode_alloc(
         * the allocator to accommodate the request.
         */
        if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
-               xfs_set_inode32(mp);
+               set_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
        else
-               xfs_clear_inode32(mp);
+               clear_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
 
        for (index = 0; index < agcount; index++) {
                struct xfs_perag        *pag;
@@ -564,24 +590,8 @@ xfs_set_inode_alloc(
                ino = XFS_AGINO_TO_INO(mp, index, agino);
 
                pag = xfs_perag_get(mp, index);
-
-               if (xfs_is_inode32(mp)) {
-                       if (ino > XFS_MAXINUMBER_32) {
-                               pag->pagi_inodeok = 0;
-                               pag->pagf_metadata = 0;
-                       } else {
-                               pag->pagi_inodeok = 1;
-                               maxagi++;
-                               if (index < max_metadata)
-                                       pag->pagf_metadata = 1;
-                               else
-                                       pag->pagf_metadata = 0;
-                       }
-               } else {
-                       pag->pagi_inodeok = 1;
-                       pag->pagf_metadata = 0;
-               }
-
+               if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
+                       maxagi++;
                xfs_perag_put(pag);
        }
 
index 456c82d70dca11a6011373141d27c049d65f68aa..32a6be124b2f03c698ae9face9029fcdcbba6b5f 100644 (file)
@@ -47,6 +47,7 @@
 #include "bitops.h"
 #include "kmem.h"
 #include "libfrog/radix-tree.h"
+#include "libfrog/bitmask.h"
 #include "atomic.h"
 #include "spinlock.h"
 #include "linux-err.h"
@@ -630,42 +631,6 @@ unsigned int hweight8(unsigned int w);
 unsigned int hweight32(unsigned int w);
 unsigned int hweight64(__u64 w);
 
-#define BIT_MASK(nr)   (1UL << ((nr) % BITS_PER_LONG))
-#define BIT_WORD(nr)   ((nr) / BITS_PER_LONG)
-
-static inline void set_bit(int nr, volatile unsigned long *addr)
-{
-       unsigned long mask = BIT_MASK(nr);
-       unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
-
-       *p  |= mask;
-}
-
-static inline void clear_bit(int nr, volatile unsigned long *addr)
-{
-       unsigned long mask = BIT_MASK(nr);
-       unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
-
-       *p &= ~mask;
-}
-
-static inline int test_bit(int nr, const volatile unsigned long *addr)
-{
-       unsigned long mask = BIT_MASK(nr);
-       unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
-
-       return *p & mask;
-}
-
-/* Sets and returns original value of the bit */
-static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
-{
-       if (test_bit(nr, addr))
-               return 1;
-       set_bit(nr, addr);
-       return 0;
-}
-
 static inline int xfs_buf_hash_init(struct xfs_perag *pag) { return 0; }
 static inline void xfs_buf_hash_destroy(struct xfs_perag *pag) { }
 
index aeb21c8df201eb7f66d5f2191232932ee6eddfb2..187d30d9bb13956b141fc1d27442fe817d978a41 100644 (file)
@@ -35,13 +35,9 @@ struct xfs_perag {
        atomic_t        pag_ref;        /* passive reference count */
        atomic_t        pag_active_ref; /* active reference count */
        wait_queue_head_t pag_active_wq;/* woken active_ref falls to zero */
-       char            pagf_init;      /* this agf's entry is initialized */
-       char            pagi_init;      /* this agi's entry is initialized */
-       char            pagf_metadata;  /* the agf is preferred to be metadata */
-       char            pagi_inodeok;   /* The agi is ok for inodes */
+       unsigned long   pag_opstate;
        uint8_t         pagf_levels[XFS_BTNUM_AGF];
                                        /* # of levels in bno & cnt btree */
-       bool            pagf_agflreset; /* agfl requires reset before use */
        uint32_t        pagf_flcount;   /* count of blocks in freelist */
        xfs_extlen_t    pagf_freeblks;  /* total free blocks */
        xfs_extlen_t    pagf_longest;   /* longest free space */
@@ -108,6 +104,27 @@ struct xfs_perag {
 #endif /* __KERNEL__ */
 };
 
+/*
+ * Per-AG operational state. These are atomic flag bits.
+ */
+#define XFS_AGSTATE_AGF_INIT           0
+#define XFS_AGSTATE_AGI_INIT           1
+#define XFS_AGSTATE_PREFERS_METADATA   2
+#define XFS_AGSTATE_ALLOWS_INODES      3
+#define XFS_AGSTATE_AGFL_NEEDS_RESET   4
+
+#define __XFS_AG_OPSTATE(name, NAME) \
+static inline bool xfs_perag_ ## name (struct xfs_perag *pag) \
+{ \
+       return test_bit(XFS_AGSTATE_ ## NAME, &pag->pag_opstate); \
+}
+
+__XFS_AG_OPSTATE(initialised_agf, AGF_INIT)
+__XFS_AG_OPSTATE(initialised_agi, AGI_INIT)
+__XFS_AG_OPSTATE(prefers_metadata, PREFERS_METADATA)
+__XFS_AG_OPSTATE(allows_inodes, ALLOWS_INODES)
+__XFS_AG_OPSTATE(agfl_needs_reset, AGFL_NEEDS_RESET)
+
 int xfs_initialize_perag(struct xfs_mount *mp, xfs_agnumber_t agcount,
                        xfs_rfsblock_t dcount, xfs_agnumber_t *maxagi);
 int xfs_initialize_perag_data(struct xfs_mount *mp, xfs_agnumber_t agno);
index 82449ba979b807a4715c17214cd3eadee44c21dc..db24632e9aa2f8db2f9e4bad6c7018e34b426556 100644 (file)
@@ -2431,7 +2431,7 @@ xfs_agfl_reset(
        struct xfs_mount        *mp = tp->t_mountp;
        struct xfs_agf          *agf = agbp->b_addr;
 
-       ASSERT(pag->pagf_agflreset);
+       ASSERT(xfs_perag_agfl_needs_reset(pag));
        trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
 
        xfs_warn(mp,
@@ -2446,7 +2446,7 @@ xfs_agfl_reset(
                                    XFS_AGF_FLCOUNT);
 
        pag->pagf_flcount = 0;
-       pag->pagf_agflreset = false;
+       clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
 }
 
 /*
@@ -2601,7 +2601,7 @@ xfs_alloc_fix_freelist(
        /* deferred ops (AGFL block frees) require permanent transactions */
        ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
 
-       if (!pag->pagf_init) {
+       if (!xfs_perag_initialised_agf(pag)) {
                error = xfs_alloc_read_agf(pag, tp, flags, &agbp);
                if (error) {
                        /* Couldn't lock the AGF so skip this AG. */
@@ -2616,7 +2616,8 @@ xfs_alloc_fix_freelist(
         * somewhere else if we are not being asked to try harder at this
         * point
         */
-       if (pag->pagf_metadata && (args->datatype & XFS_ALLOC_USERDATA) &&
+       if (xfs_perag_prefers_metadata(pag) &&
+           (args->datatype & XFS_ALLOC_USERDATA) &&
            (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
                ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
                goto out_agbp_relse;
@@ -2642,7 +2643,7 @@ xfs_alloc_fix_freelist(
        }
 
        /* reset a padding mismatched agfl before final free space check */
-       if (pag->pagf_agflreset)
+       if (xfs_perag_agfl_needs_reset(pag))
                xfs_agfl_reset(tp, agbp, pag);
 
        /* If there isn't enough total space or single-extent, reject it. */
@@ -2799,7 +2800,7 @@ xfs_alloc_get_freelist(
        if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
                agf->agf_flfirst = 0;
 
-       ASSERT(!pag->pagf_agflreset);
+       ASSERT(!xfs_perag_agfl_needs_reset(pag));
        be32_add_cpu(&agf->agf_flcount, -1);
        pag->pagf_flcount--;
 
@@ -2888,7 +2889,7 @@ xfs_alloc_put_freelist(
        if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
                agf->agf_fllast = 0;
 
-       ASSERT(!pag->pagf_agflreset);
+       ASSERT(!xfs_perag_agfl_needs_reset(pag));
        be32_add_cpu(&agf->agf_flcount, 1);
        pag->pagf_flcount++;
 
@@ -3095,7 +3096,7 @@ xfs_alloc_read_agf(
                return error;
 
        agf = agfbp->b_addr;
-       if (!pag->pagf_init) {
+       if (!xfs_perag_initialised_agf(pag)) {
                pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
                pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
                pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
@@ -3107,8 +3108,8 @@ xfs_alloc_read_agf(
                pag->pagf_levels[XFS_BTNUM_RMAPi] =
                        be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
                pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
-               pag->pagf_init = 1;
-               pag->pagf_agflreset = xfs_agfl_needs_reset(pag->pag_mount, agf);
+               if (xfs_agfl_needs_reset(pag->pag_mount, agf))
+                       set_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
 
                /*
                 * Update the in-core allocbt counter. Filter out the rmapbt
@@ -3123,6 +3124,8 @@ xfs_alloc_read_agf(
                if (allocbt_blks > 0)
                        atomic64_add(allocbt_blks,
                                        &pag->pag_mount->m_allocbt_blks);
+
+               set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
        }
 #ifdef DEBUG
        else if (!xfs_is_shutdown(pag->pag_mount)) {
index 0d4a124943490dbb73dfedee916c319d4ae376aa..f4ee585e908a7f3331e6b927bb250834c06a6532 100644 (file)
@@ -313,7 +313,7 @@ xfs_allocbt_verify(
        level = be16_to_cpu(block->bb_level);
        if (bp->b_ops->magic[0] == cpu_to_be32(XFS_ABTC_MAGIC))
                btnum = XFS_BTNUM_CNTi;
-       if (pag && pag->pagf_init) {
+       if (pag && xfs_perag_initialised_agf(pag)) {
                if (level >= pag->pagf_levels[btnum])
                        return __this_address;
        } else if (level >= mp->m_alloc_maxlevels)
index 7ca1ff15b63d92e3a31fc65f23c419c3552a21de..badc15054fddf0729d220af005c078ddc42ae395 100644 (file)
@@ -3140,7 +3140,7 @@ xfs_bmap_longest_free_extent(
        int                     error = 0;
 
        pag = xfs_perag_get(mp, ag);
-       if (!pag->pagf_init) {
+       if (!xfs_perag_initialised_agf(pag)) {
                error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
                                NULL);
                if (error) {
index d17825560ccf0b4ad15858aa87550a534b41e4b9..73add3b381e19304f7e874fc1bc24077fe2a3883 100644 (file)
@@ -993,8 +993,8 @@ xfs_dialloc_ag_inobt(
        int                     i, j;
        int                     searchdistance = 10;
 
-       ASSERT(pag->pagi_init);
-       ASSERT(pag->pagi_inodeok);
+       ASSERT(xfs_perag_initialised_agi(pag));
+       ASSERT(xfs_perag_allows_inodes(pag));
        ASSERT(pag->pagi_freecount > 0);
 
  restart_pagno:
@@ -1587,10 +1587,10 @@ xfs_dialloc_good_ag(
 
        if (!pag)
                return false;
-       if (!pag->pagi_inodeok)
+       if (!xfs_perag_allows_inodes(pag))
                return false;
 
-       if (!pag->pagi_init) {
+       if (!xfs_perag_initialised_agi(pag)) {
                error = xfs_ialloc_read_agi(pag, tp, NULL);
                if (error)
                        return false;
@@ -1601,7 +1601,7 @@ xfs_dialloc_good_ag(
        if (!ok_alloc)
                return false;
 
-       if (!pag->pagf_init) {
+       if (!xfs_perag_initialised_agf(pag)) {
                error = xfs_alloc_read_agf(pag, tp, flags, NULL);
                if (error)
                        return false;
@@ -2598,10 +2598,10 @@ xfs_ialloc_read_agi(
                return error;
 
        agi = agibp->b_addr;
-       if (!pag->pagi_init) {
+       if (!xfs_perag_initialised_agi(pag)) {
                pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
                pag->pagi_count = be32_to_cpu(agi->agi_count);
-               pag->pagi_init = 1;
+               set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
        }
 
        /*
index 1f0840883e2c7cd8e71002ab37c303003b31c7c7..cc4f6d6e1e028a9ab1394de2b7fd5acefb645d53 100644 (file)
@@ -290,8 +290,8 @@ xfs_inobt_verify(
         * Similarly, during log recovery we will have a perag structure
         * attached, but the agi information will not yet have been initialised
         * from the on disk AGI. We don't currently use any of this information,
-        * but beware of the landmine (i.e. need to check pag->pagi_init) if we
-        * ever do.
+        * but beware of the landmine (i.e. need to check
+        * xfs_perag_initialised_agi(pag)) if we ever do.
         */
        if (xfs_has_crc(mp)) {
                fa = xfs_btree_sblock_v5hdr_verify(bp);
index 983d4276d14718e9f8c4d7b3ce3d2145832b6e87..a91026037501fbae99f865758fa822bbae4498d9 100644 (file)
@@ -226,7 +226,7 @@ xfs_refcountbt_verify(
                return fa;
 
        level = be16_to_cpu(block->bb_level);
-       if (pag && pag->pagf_init) {
+       if (pag && xfs_perag_initialised_agf(pag)) {
                if (level >= pag->pagf_refcount_level)
                        return __this_address;
        } else if (level >= mp->m_refc_maxlevels)
index d2d184304c64a69ad0c568860529d2cb6e2173b5..a7e6e652f5c39069d3874ddef02bec2a8d49b290 100644 (file)
@@ -311,7 +311,7 @@ xfs_rmapbt_verify(
                return fa;
 
        level = be16_to_cpu(block->bb_level);
-       if (pag && pag->pagf_init) {
+       if (pag && xfs_perag_initialised_agf(pag)) {
                if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
                        return __this_address;
        } else if (level >= mp->m_rmap_maxlevels)
index 52106fd42f1346a774f901cbeca835bed4029a9a..9013daa2ecd61fdbd7efa6475260e0b9ced38953 100644 (file)
@@ -1008,7 +1008,7 @@ rmaps_verify_btree(
        }
 
        /* Leave the per-ag data "uninitialized" since we rewrite it later */
-       pag->pagf_init = 0;
+       clear_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
 
        bt_cur = libxfs_rmapbt_init_cursor(mp, NULL, agbp, pag);
        if (!bt_cur) {
@@ -1382,7 +1382,7 @@ check_refcounts(
        }
 
        /* Leave the per-ag data "uninitialized" since we rewrite it later */
-       pag->pagf_init = 0;
+       clear_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
 
        bt_cur = libxfs_refcountbt_init_cursor(mp, NULL, agbp, pag);
        if (!bt_cur) {