[PATCH v4 02/11] vfs: add sb_inode_iter_locked()
From: Huang Shijie
Date: Wed Nov 25 2026 - 04:12:39 EST
Add sb_inode_iter_locked(), which keeps sb->s_inode_list_lock held
across the whole walk and takes no reference, so each callback only gets
an existence guarantee. It must do its own inode state checks and must
not block.
It lives in fs/internal.h as a static inline rather than being exported,
so that it stays a VFS internal facility and the small callbacks inline
into their callers. A callback returns a negative value to stop the walk
with that error, and 0 to continue.
Signed-off-by: Huang Shijie <huangsj@xxxxxxxx>
---
fs/internal.h | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/fs/internal.h b/fs/internal.h
index 967c8556c17f..0479094e1330 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -139,6 +139,31 @@ struct super_block *user_get_super(dev_t, bool excl);
void put_super(struct super_block *sb);
extern bool mount_capable(struct fs_context *);
+/*
+ * Iteration for internal VFS use only, with sb->s_inode_list_lock held
+ * across the whole walk and no inode reference taken. @iter_fn runs in
+ * atomic context with only an inode existence guarantee, so it must not
+ * block and must perform its own inode state checks.
+ *
+ * Return a negative value to stop the walk with that error, 0 to continue.
+ */
+static inline int sb_inode_iter_locked(struct super_block *sb,
+ int (*iter_fn)(struct inode *inode, void *priv), void *priv)
+{
+ struct inode *inode;
+ int ret = 0;
+
+ spin_lock(&sb->s_inode_list_lock);
+ list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
+ ret = iter_fn(inode, priv);
+ if (ret)
+ break;
+ }
+ spin_unlock(&sb->s_inode_list_lock);
+
+ return ret;
+}
+
/*
* Prepare superblock for changing its read-only state (i.e., either remount
* read-write superblock read-only or vice versa). After this function returns
--
2.53.0