[PATCH v3 02/11] vfs: add super_iter_inodes_locked()

Huang Shijie huangsj at hygon.cn
Fri Nov 20 03:12:56 UTC 2026


The super_iter_inodes_locked() keeps sb->s_inode_list_lock held
for the whole walk and takes no inode references at all.
Its callback therefore only has an existence guarantee:
  it must not block and must do its own inode state checks and
  its own reference handling.

The iterator is internal to the VFS, being declared in fs/internal.h.

Signed-off-by: Huang Shijie <huangsj at hygon.cn>
---
 fs/internal.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/fs/internal.h b/fs/internal.h
index 967c8556c17f..27fe7948296c 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -139,6 +139,37 @@ 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 INO_ITER_RET_ABORT if the iteration stops early.
+ * Return INO_ITER_RET_DONE if the iteration runs to the end.
+ */
+static inline int super_iter_inodes_locked(struct super_block *sb,
+					   ino_iter_fn iter_fn, void *priv)
+{
+	struct inode *inode;
+	int ret = INO_ITER_RET_NEXT;
+
+	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 == INO_ITER_RET_ABORT)
+			break;
+	}
+	spin_unlock(&sb->s_inode_list_lock);
+
+	/* Mark the end here */
+	if (ret == INO_ITER_RET_NEXT)
+		ret = INO_ITER_RET_DONE;
+
+	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





More information about the Linux-security-module-archive mailing list