mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Huang Shijie <huangsj@hygon.cn>
To: <viro@zeniv.linux.org.uk>, <brauner@kernel.org>, <jack@suse.cz>,
	<hch@infradead.org>, <agruenba@redhat.com>, <mic@digikod.net>,
	<axboe@kernel.dk>
Cc: <gnoack@google.com>, <paul@paul-moore.com>, <jmorris@namei.org>,
	<serge@hallyn.com>, <legion@kernel.org>, <aleksa@amutable.com>,
	<libaokun@linux.alibaba.com>, <ebiggers@kernel.org>,
	<djwong@kernel.org>, <sandeen@redhat.com>, <zhongyuan@hygon.cn>,
	<fangbaoshun@hygon.cn>, <yingzhiwei@hygon.cn>,
	<wujianyong@hygon.cn>, <shijie8@gmail.com>,
	<linux-fsdevel@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-block@vger.kernel.org>, <gfs2@lists.linux.dev>,
	<linux-security-module@vger.kernel.org>,
	Huang Shijie <huangsj@hygon.cn>
Subject: [PATCH v3 01/11] vfs: add super_iter_inodes()
Date: Fri, 20 Nov 2026 11:12:55 +0800	[thread overview]
Message-ID: <20261120031305.3561045-2-huangsj@hygon.cn> (raw)
In-Reply-To: <20261120031305.3561045-1-huangsj@hygon.cn>

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@hygon.cn>
---
 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



  reply	other threads:[~2026-11-20  3:13 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
2026-11-20  3:12 ` Huang Shijie [this message]
2026-11-20 14:56   ` [PATCH v3 01/11] vfs: add super_iter_inodes() Andreas Gruenbacher
2026-11-20  3:12 ` [PATCH v3 02/11] vfs: add super_iter_inodes_locked() Huang Shijie
2026-11-20  3:12 ` [PATCH v3 03/11] fs/quota: convert remove_dquot_ref() to super_iter_inodes_locked() Huang Shijie
2026-11-20  3:12 ` [PATCH v3 04/11] block: convert nr_blockdev_pages() " Huang Shijie
2026-11-20  3:12 ` [PATCH v3 05/11] fs: convert generic_shutdown_super() poison loop Huang Shijie
2026-11-20  3:13 ` [PATCH v3 06/11] fs/inode: convert evict_inodes() to super_iter_inodes() Huang Shijie
2026-11-20  3:13 ` [PATCH v3 07/11] block: convert sync_bdevs() " Huang Shijie
2026-11-20  3:13 ` [PATCH v3 08/11] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
2026-11-20  3:13 ` [PATCH v3 09/11] fs/quota: convert add_dquot_ref() " Huang Shijie
2026-11-20  3:13 ` [PATCH v3 10/11] landlock: convert hook_sb_delete() " Huang Shijie
2026-11-20  3:13 ` [PATCH v3 11/11] gfs2: convert gfs2_evict_inodes() " Huang Shijie
2026-11-20 14:59 ` [PATCH v3 0/11] vfs: add inode iteration methods Andreas Gruenbacher
2026-11-23  2:31   ` Huang Shijie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120031305.3561045-2-huangsj@hygon.cn \
    --to=huangsj@hygon.cn \
    --cc=agruenba@redhat.com \
    --cc=aleksa@amutable.com \
    --cc=axboe@kernel.dk \
    --cc=brauner@kernel.org \
    --cc=djwong@kernel.org \
    --cc=ebiggers@kernel.org \
    --cc=fangbaoshun@hygon.cn \
    --cc=gfs2@lists.linux.dev \
    --cc=gnoack@google.com \
    --cc=hch@infradead.org \
    --cc=jack@suse.cz \
    --cc=jmorris@namei.org \
    --cc=legion@kernel.org \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    --cc=paul@paul-moore.com \
    --cc=sandeen@redhat.com \
    --cc=serge@hallyn.com \
    --cc=shijie8@gmail.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wujianyong@hygon.cn \
    --cc=yingzhiwei@hygon.cn \
    --cc=zhongyuan@hygon.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®