[PATCH v4 01/11] vfs: add sb_inode_iter_next()

From: Huang Shijie

Date: Wed Nov 25 2026 - 04:12:51 EST


Walking sb->s_inodes requires taking sb->s_inode_list_lock, grabbing a
reference on the next inode, dropping the lock and then working on the
inode. That sequence is open coded in several places, each with its own
subtleties around rescheduling and iput().

Add struct sb_inode_iter, which the caller declares and owns, plus
sb_inode_iter_next() to step through the list and sb_inode_iter_end() to
release the last reference.

As the cursor inode is referenced or pinned by other method, it cannot be
unlinked from sb->s_inodes, so the walk resumes from it after the caller
has dropped the lock.

Callers that need to filter inodes can provide iter->eligible,
which is called with inode->i_lock held, and callers that need to
stop early can set iter->status.

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

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

+static bool sb_inode_iter_check(struct sb_inode_iter *iter, struct inode *inode)
+{
+ bool ret = false;
+
+ if ((iter->flags & INO_ITER_F_REFERENCED) && !icount_read_once(inode))
+ return false;
+
+ if ((iter->flags & INO_ITER_F_NOREF) && icount_read_once(inode))
+ return false;
+
+ spin_lock(&inode->i_lock);
+
+ if ((iter->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 (iter->eligible && !iter->eligible(inode, iter->priv))
+ goto out_unlock;
+
+ if (!(iter->flags & INO_ITER_F_NOIGET))
+ __iget(inode);
+
+ ret = true;
+out_unlock:
+ spin_unlock(&inode->i_lock);
+ return ret;
+}
+
+static inline bool sb_inode_iter_flags_valid(int flags)
+{
+ switch (flags) {
+ case 0:
+ case INO_ITER_F_REFERENCED:
+ case INO_ITER_F_NOREF | INO_ITER_F_NOIGET:
+ /* only used by evict_inodes() */
+ return true;
+ default:
+ return false;
+ }
+}
+
+/**
+ * sb_inode_iter_next - return the next inode of @iter->sb
+ * @iter: iterator state
+ *
+ * The returned inode stays on sb->s_inodes for the next round
+ * without holding sb->s_inode_list_lock:
+ * -- By default, this inode has an increased reference count.
+ * -- If INO_ITER_F_NOIGET is set, this inode will not change
+ * its reference count.
+ *
+ * Return the next inode, or NULL once the walk is over or @iter->status is
+ * negative.
+ */
+struct inode *sb_inode_iter_next(struct sb_inode_iter *iter)
+{
+ struct super_block *sb = iter->sb;
+ struct inode *prev = iter->inode, *inode;
+
+ if (!sb_inode_iter_flags_valid(iter->flags)) {
+ VFS_WARN_ON_ONCE(1);
+ iter->status = -EINVAL;
+ return NULL;
+ }
+
+ if (iter->status || iter->done)
+ return NULL;
+
+ spin_lock(&sb->s_inode_list_lock);
+ VFS_WARN_ON_ONCE(prev && list_empty(&prev->i_sb_list));
+ inode = list_prepare_entry(prev, &sb->s_inodes, i_sb_list);
+ list_for_each_entry_continue(inode, &sb->s_inodes, i_sb_list) {
+ if (!sb_inode_iter_check(iter, inode))
+ continue;
+
+ spin_unlock(&sb->s_inode_list_lock);
+ iter->inode = inode;
+ if (!(iter->flags & INO_ITER_F_NOIGET))
+ iput(prev);
+ return inode;
+ }
+ spin_unlock(&sb->s_inode_list_lock);
+
+ iter->inode = NULL;
+ iter->done = true;
+ if (!(iter->flags & INO_ITER_F_NOIGET))
+ iput(prev);
+ return NULL;
+}
+EXPORT_SYMBOL_GPL(sb_inode_iter_next);
+
+void sb_inode_iter_end(struct sb_inode_iter *iter)
+{
+ if (!(iter->flags & INO_ITER_F_NOIGET))
+ iput(iter->inode);
+ iter->inode = NULL;
+ iter->done = true;
+}
+EXPORT_SYMBOL_GPL(sb_inode_iter_end);
+
/*
* 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..b844e5259360 100644
--- a/include/linux/fs/super_types.h
+++ b/include/linux/fs/super_types.h
@@ -80,6 +80,46 @@ enum freeze_holder {
FREEZE_EXCL = (1U << 3),
};

+/* Caller-owned state for walking sb->s_inodes. */
+struct sb_inode_iter {
+ struct super_block *sb;
+ struct inode *inode;
+ bool (*eligible)(struct inode *inode, void *priv);
+ void *priv;
+ bool done;
+ int status;
+ int flags;
+};
+
+/* Only hand out inodes that still have a reference. */
+#define INO_ITER_F_REFERENCED BIT(0)
+/* Only hand out inodes whose reference count has reached zero. */
+#define INO_ITER_F_NOREF BIT(1)
+/* Do not take a reference on the inodes handed out. */
+#define INO_ITER_F_NOIGET BIT(2)
+
+struct inode *sb_inode_iter_next(struct sb_inode_iter *iter);
+void sb_inode_iter_end(struct sb_inode_iter *iter);
+
+static inline struct sb_inode_iter
+sb_inode_iter_init(struct super_block *sb,
+ bool (*eligible)(struct inode *inode, void *priv),
+ void *priv, int flags)
+{
+ return (struct sb_inode_iter){
+ .sb = sb,
+ .eligible = eligible,
+ .priv = priv,
+ .flags = flags,
+ };
+}
+
+DEFINE_CLASS(sb_inode_iter, struct sb_inode_iter,
+ sb_inode_iter_end(&_T),
+ sb_inode_iter_init(sb, eligible, priv, flags),
+ struct super_block *sb, bool (*eligible)(struct inode *, void *),
+ void *priv, int flags)
+
struct super_operations {
struct inode *(*alloc_inode)(struct super_block *sb);
void (*destroy_inode)(struct inode *inode);
--
2.53.0