[PATCH v3 01/11] vfs: add super_iter_inodes()

From: Huang Shijie

Date: Thu Nov 19 2026 - 22:13:59 EST


Iterating the cached inodes of a superblock is open coded in the
kernel. Add super_iter_inodes() for the callers which can work without
holding the sb->s_inode_list_lock.

For super_iter_inodes():
-- By default, it runs @iter_fn with a referenced inode and with
sb->s_inode_list_lock dropped, so the callback is free to block and to
do whatever it wants with the inode except drop the reference the
iterator holds.

-- Three flags change the default behaviour.

INO_ITER_F_REFERENCED restricts the walk to inodes which still have a
reference.

INO_ITER_F_NOREF does the opposite: only inodes whose reference count
has already reached zero are handed out.

INO_ITER_F_NOIGET tells the iterator do not call __iget for the inode.
Used with INO_ITER_F_NOREF in evict_inodes().

The flags are not orthogonal, so super_iter_inodes() warns if an unknown
bit is set or if INO_ITER_F_REFERENCED is combined with either of the
no-reference flags.

Signed-off-by: Huang Shijie <huangsj@xxxxxxxx>
---
fs/super.c | 87 ++++++++++++++++++++++++++++++++++
include/linux/fs/super_types.h | 29 ++++++++++++
2 files changed, 116 insertions(+)

diff --git a/fs/super.c b/fs/super.c
index c5a0658f7aaf..204c70c7f179 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -171,6 +171,93 @@ static void super_wake(struct super_block *sb, unsigned int flag)
wake_up_var(&sb->s_flags);
}

+static bool super_iter_check(struct inode *inode, int flags,
+ ino_iter_eligible_fn eligible, void *priv)
+{
+ bool ret = false;
+
+ if ((flags & INO_ITER_F_REFERENCED) && !icount_read_once(inode))
+ return false;
+ if ((flags & INO_ITER_F_NOREF) && icount_read_once(inode))
+ return false;
+
+ spin_lock(&inode->i_lock);
+
+ if ((flags & INO_ITER_F_NOREF) && icount_read(inode))
+ goto out_unlock;
+
+ if (inode_state_read(inode) & (I_NEW | I_FREEING | I_WILL_FREE))
+ goto out_unlock;
+
+ if (eligible && !eligible(inode, priv))
+ goto out_unlock;
+
+ if (!(flags & INO_ITER_F_NOIGET))
+ __iget(inode);
+
+ ret = true;
+out_unlock:
+ spin_unlock(&inode->i_lock);
+ return ret;
+}
+
+/**
+ * super_iter_inodes - iterate all the cached inodes on a superblock
+ * @sb: superblock to iterate
+ * @iter_fn: called for each inode, with a reference held
+ * @eligible: optional filter, called before the reference is taken
+ * @priv: caller data, passed to @iter_fn and @eligible
+ * @flags: INO_ITER_F_* control flags
+ *
+ * This function iterates all cached inodes on a superblock that are not in
+ * the process of being initialised or torn down. By default it runs @iter_fn
+ * with a valid, referenced inode, so it is safe for the caller to do anything
+ * it wants with the inode except drop the reference the iterator holds.
+ *
+ * The flag INO_ITER_F_NOIGET changes that contract. It runs @iter_fn without
+ * grabbing the inode with __iget().
+ *
+ * Return INO_ITER_RET_DONE if the iteration runs to the end.
+ * Return other value if the iteration breaks.
+ */
+int super_iter_inodes(struct super_block *sb, ino_iter_fn iter_fn,
+ ino_iter_eligible_fn eligible, void *priv, int flags)
+{
+ struct inode *inode, *old_inode = NULL;
+ int ret;
+
+ WARN_ON_ONCE(flags & ~INO_ITER_F_ALL);
+ WARN_ON_ONCE((flags & INO_ITER_F_REFERENCED) &&
+ (flags & (INO_ITER_F_NOREF | INO_ITER_F_NOIGET)));
+
+ spin_lock(&sb->s_inode_list_lock);
+ list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
+ if (!super_iter_check(inode, flags, eligible, priv))
+ continue;
+
+ spin_unlock(&sb->s_inode_list_lock);
+
+ if (!(flags & INO_ITER_F_NOIGET))
+ iput(old_inode);
+
+ ret = iter_fn(inode, priv);
+
+ old_inode = inode;
+ if (ret != INO_ITER_RET_NEXT)
+ goto out_loop;
+
+ cond_resched();
+ spin_lock(&sb->s_inode_list_lock);
+ }
+ spin_unlock(&sb->s_inode_list_lock);
+ ret = INO_ITER_RET_DONE;
+out_loop:
+ if (!(flags & INO_ITER_F_NOIGET))
+ iput(old_inode);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(super_iter_inodes);
+
/*
* One thing we have to be careful of with a per-sb shrinker is that we don't
* drop the last active reference to the superblock from within the shrinker.
diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h
index 72f4288e16c5..974d3c087952 100644
--- a/include/linux/fs/super_types.h
+++ b/include/linux/fs/super_types.h
@@ -80,6 +80,35 @@ enum freeze_holder {
FREEZE_EXCL = (1U << 3),
};

+/* Inode iteration callback return values */
+#define INO_ITER_RET_DONE 0
+#define INO_ITER_RET_ABORT 1
+#define INO_ITER_RET_NEXT 2
+
+/* Inode iteration control flags */
+/* Only iterate inodes that still have a reference. */
+#define INO_ITER_F_REFERENCED BIT(0)
+/* Only iterate inodes whose reference count has reached zero. */
+#define INO_ITER_F_NOREF BIT(1)
+/* Do not take a reference on the inodes handed to the callback. */
+#define INO_ITER_F_NOIGET BIT(2)
+
+/* REFERENCED and NOREF ask for opposite things, so only one may be set. */
+#define INO_ITER_F_ALL (INO_ITER_F_REFERENCED | INO_ITER_F_NOREF | \
+ INO_ITER_F_NOIGET)
+
+typedef int (*ino_iter_fn)(struct inode *inode, void *priv);
+
+/*
+ * Optional filter, called from inside the inode's i_lock and before
+ * __iget(), so that an inode can be rejected without paying for a
+ * reference.
+ */
+typedef bool (*ino_iter_eligible_fn)(struct inode *inode, void *priv);
+
+int super_iter_inodes(struct super_block *sb, ino_iter_fn iter_fn,
+ ino_iter_eligible_fn eligible, void *priv, int flags);
+
struct super_operations {
struct inode *(*alloc_inode)(struct super_block *sb);
void (*destroy_inode)(struct inode *inode);
--
2.53.0