[PATCH v5 00/10] vfs: add for_each_inode()
From: Huang Shijie
Date: Tue Dec 01 2026 - 01:47:10 EST
Two macros replace the open-coded sb->s_inodes walks in the VFS. The
details stay inside the macro while the loop itself is written by the
caller, so ending the walk early needs nothing special:
for_each_inode(inode, sb) {
/* inode is referenced and unlocked here */
}
for_each_inode(inode, sb, cond) {
/* cond was true for this inode */
}
for_each_inode_locked(inode, sb) {
/* the list lock is held here */
}
The first two forms take a reference on each inode and drop
sb->s_inode_list_lock before the body runs. @cond is expanded at the
call site, so it can use the caller's own variables, and it sees the
candidate in @inode. It is evaluated with both sb->s_inode_list_lock
and inode->i_lock held, and before the reference is paid for, so it may
look at state that is only valid while the inode is locked.
The iterator is a cleanup class, so it goes out of scope as soon as the
loop ends, including through break and return.
for_each_inode_locked() serves the walks that must not take a reference.
It keeps sb->s_inode_list_lock held for the whole walk, so the body has
an existence guarantee only, runs in atomic context, and has to do its
own inode state checks.
Two walks are intentionally left as they are. evict_inodes() only takes
inodes whose reference count has already reached zero, pins each one by
setting I_FREEING, and runs dispose_list() with the list lock dropped.
landlock's hook_sb_delete() needs its own ihold()/iput_not_last()
protocol.
Patch 1 renames the jffs2 local for_each_inode() macro out of the way.
Patch 2 adds for_each_inode().
Patch 3 adds for_each_inode_locked().
Patch 4-6 convert the callers which cannot use referenced inodes
(nr_blockdev_pages(), dquot removal and the
generic_shutdown_super() poisoning loop).
Patch 7-10 convert the remaining callers (sync_bdevs,
drop_pagecache_sb, add_dquot_ref and gfs2_evict_inodes()).
BTW:
This series is based on v7.4-rc4, and applies on top of the last commit
of that base, which is the small patch this series depends on:
v7.4-rc4-588-gaf7b783302f2
commit af7b783302f2 ("fs/drop_caches: skip filesystems without page cache writeback")
That patch was posted as:
https://lore.kernel.org/all/20260923070116.3226846-1-huangsj@xxxxxxxx/
v4-->v5:
-the interface is redone as macros that expand to a for loop, as
Andreas asked for. The caller-owned cursor of v4, along with
sb_inode_iter_next(), is gone.
-there are no flags. The walks that need something other than a
referenced inode get their own macro instead of a flag.
-there is no error channel: a caller that wants to stop simply breaks
out of the loop.
-the precondition is passed as the third argument of for_each_inode()
and expanded in place, so it can use the caller's local variables
without a context argument.
-evict_inodes() and landlock's hook_sb_delete() are left at their
baseline, as agreed.
-the jffs2 local macro is renamed in a separate patch, so that the
generic macro can be introduced without touching that file again.
v4: https://lore.kernel.org/all/20261125091157.1822521-1-huangsj@xxxxxxxx/
v3: https://lore.kernel.org/all/20261120031305.3561045-1-huangsj@xxxxxxxx/
v2: https://lore.kernel.org/all/20261113115702.586594-1-huangsj@xxxxxxxx/
v1: https://lore.kernel.org/all/20261015093854.2082600-1-huangsj@xxxxxxxx/
Huang Shijie (10):
jffs2: rename the local for_each_inode() macro
vfs: add for_each_inode()
vfs: add for_each_inode_locked()
block: convert nr_blockdev_pages() to for_each_inode_locked()
fs/quota: convert remove_dquot_ref() to for_each_inode_locked()
fs: convert generic_shutdown_super() poison loop
block: convert sync_bdevs() to for_each_inode()
fs/drop_caches: convert drop_pagecache_sb() to for_each_inode()
fs/quota: convert add_dquot_ref() to for_each_inode()
gfs2: convert gfs2_evict_inodes() to for_each_inode()
block/bdev.c | 43 ++------------
fs/drop_caches.c | 31 +++--------
fs/gfs2/ops_fstype.c | 26 ++-------
fs/jffs2/build.c | 12 ++--
fs/quota/dquot.c | 42 ++------------
fs/super.c | 4 +-
include/linux/fs.h | 130 +++++++++++++++++++++++++++++++++++++++++++
7 files changed, 162 insertions(+), 126 deletions(-)
--
2.34.1