* [PATCH v5 01/10] jffs2: rename the local for_each_inode() macro
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 6:46 ` [PATCH v5 02/10] vfs: add for_each_inode() Huang Shijie
` (8 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
A generic for_each_inode() will be added to fs.h which will
collide with it.
Rename the jffs2 one firstly, so that the generic macro can
be introduced without touching this file again.
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/jffs2/build.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/fs/jffs2/build.c b/fs/jffs2/build.c
index 6ae9d6fefb86..0b21c79b0353 100644
--- a/fs/jffs2/build.c
+++ b/fs/jffs2/build.c
@@ -40,13 +40,13 @@ next_inode(int *i, struct jffs2_inode_cache *ic, struct jffs2_sb_info *c)
if (ic->next)
return ic->next;
(*i)++;
return first_inode_chain(i, c);
}
-#define for_each_inode(i, c, ic) \
+#define for_each_jffs2_inode(i, c, ic) \
for (i = 0, ic = first_inode_chain(&i, (c)); \
ic; \
ic = next_inode(&i, ic, (c)))
static void jffs2_build_inode_pass1(struct jffs2_sb_info *c,
@@ -117,13 +117,13 @@ static int jffs2_build_filesystem(struct jffs2_sb_info *c)
dbg_fsbuild("scanned flash completely\n");
jffs2_dbg_dump_block_lists_nolock(c);
dbg_fsbuild("pass 1 starting\n");
c->flags |= JFFS2_SB_FLAG_BUILDING;
/* Now scan the directory tree, increasing nlink according to every dirent found. */
- for_each_inode(i, c, ic) {
+ for_each_jffs2_inode(i, c, ic) {
if (ic->scan_dents) {
jffs2_build_inode_pass1(c, ic, &dir_hardlinks);
cond_resched();
}
}
@@ -133,13 +133,13 @@ static int jffs2_build_filesystem(struct jffs2_sb_info *c)
they were directories, then decrement the nlink of their
children too, and repeat the scan. As that's going to be
a fairly uncommon occurrence, it's not so evil to do it this
way. Recursion bad. */
dbg_fsbuild("pass 2 starting\n");
- for_each_inode(i, c, ic) {
+ for_each_jffs2_inode(i, c, ic) {
if (ic->pino_nlink)
continue;
jffs2_build_remove_unlinked_inode(c, ic, &dead_fds);
cond_resched();
}
@@ -164,21 +164,21 @@ static int jffs2_build_filesystem(struct jffs2_sb_info *c)
* they are gone now because some of the links were from
* dead directories which still had some old dirents lying
* around and not yet garbage-collected, but which have
* been discarded above. So clear the pino_nlink field
* in each directory, so that the final scan below can
* print appropriate warnings. */
- for_each_inode(i, c, ic) {
+ for_each_jffs2_inode(i, c, ic) {
if (ic->flags & INO_FLAGS_IS_DIR)
ic->pino_nlink = 0;
}
}
dbg_fsbuild("freeing temporary data structures\n");
/* Finally, we can scan again and free the dirent structs */
- for_each_inode(i, c, ic) {
+ for_each_jffs2_inode(i, c, ic) {
while(ic->scan_dents) {
fd = ic->scan_dents;
ic->scan_dents = fd->next;
/* We do use the pino_nlink field to count nlink of
* directories during fs build, so set it to the
* parent ino# now. Now that there's hopefully only
@@ -223,13 +223,13 @@ static int jffs2_build_filesystem(struct jffs2_sb_info *c)
jffs2_rotate_lists(c);
ret = 0;
exit:
if (ret) {
- for_each_inode(i, c, ic) {
+ for_each_jffs2_inode(i, c, ic) {
while(ic->scan_dents) {
fd = ic->scan_dents;
ic->scan_dents = fd->next;
jffs2_free_full_dirent(fd);
}
}
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 02/10] vfs: add for_each_inode()
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
2026-12-01 6:46 ` [PATCH v5 01/10] jffs2: rename the local for_each_inode() macro Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 8:33 ` Christoph Hellwig
2026-12-01 6:46 ` [PATCH v5 03/10] vfs: add for_each_inode_locked() Huang Shijie
` (7 subsequent siblings)
9 siblings, 1 reply; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Walking sb->s_inodes requires taking 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 the inode state checks and the reference count.
Add a caller-owned cursor wrapped in a cleanup class, so that the
iterator goes out of scope as soon as the loop ends:
for_each_inode(inode, sb) {
/* inode is referenced and unlocked here */
}
A precondition is passed as the third argument and evaluated with both
sb->s_inode_list_lock and inode->i_lock held, before a reference is paid
for. It sees the candidate in @inode and may use it, so it can depend on
state that is only valid while the inode is locked:
for_each_inode(inode, sb, cond) {
/* cond was true */
}
The condition is expanded at the call site, so it can use the caller's
own variables without any context argument.
Dropping the previous inode's reference may sleep, so the walk cannot be
used from atomic context.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
include/linux/fs.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5b3a100aba77..383f41c022ec 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3664,12 +3664,84 @@ extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
int advice);
extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
int advice);
int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter);
+/* Caller-owned state for walking sb->s_inodes. */
+struct sb_inode_iter {
+ struct super_block *sb;
+ struct inode *cursor; /* holds a reference */
+ bool done;
+};
+
+static inline void sb_inode_iter_end(struct sb_inode_iter *iter)
+{
+ iput(iter->cursor);
+ iter->cursor = NULL;
+ iter->done = true;
+}
+
+static inline struct sb_inode_iter sb_inode_iter_init(struct super_block *sb)
+{
+ return (struct sb_inode_iter){ .sb = sb };
+}
+
+DEFINE_CLASS(sb_inode_iter, struct sb_inode_iter,
+ sb_inode_iter_end(&_T),
+ sb_inode_iter_init(sb),
+ struct super_block *sb)
+
+#define __sb_inode_iter_step(iter, inode, cond) \
+({ \
+ struct inode *__prev = (iter)->cursor; \
+ struct super_block *__sb = (iter)->sb; \
+ bool __found = false; \
+ \
+ 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) { \
+ spin_lock(&inode->i_lock); \
+ if ((inode_state_read(inode) & \
+ (I_NEW | I_FREEING | I_WILL_FREE)) || \
+ !(cond)) { \
+ spin_unlock(&inode->i_lock); \
+ continue; \
+ } \
+ __iget(inode); \
+ spin_unlock(&inode->i_lock); \
+ __found = true; \
+ break; \
+ } \
+ spin_unlock(&__sb->s_inode_list_lock); \
+ (iter)->cursor = __found ? inode : NULL; \
+ (iter)->done = !__found; \
+ iput(__prev); \
+ (iter)->cursor; \
+})
+
+/*
+ * Walk the inodes of @sb.
+ * -- @inode is referenced and unlocked in the body.
+ * -- @cond, when given, is evaluated with sb->s_inode_list_lock and
+ * inode->i_lock held, and before the reference is taken.
+ * -- Dropping the previous inode's reference may
+ * sleep, so this cannot be used in atomic context.
+ */
+#define for_each_inode(...) \
+ CONCATENATE(__for_each_inode_, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
+
+#define __for_each_inode_2(inode, sb) \
+ for (CLASS(sb_inode_iter, __it)(sb); \
+ !__it.done && __sb_inode_iter_step(&__it, inode, true);)
+
+#define __for_each_inode_3(inode, sb, cond) \
+ for (CLASS(sb_inode_iter, __it)(sb); \
+ !__it.done && __sb_inode_iter_step(&__it, inode, cond);)
+
static inline bool extensible_ioctl_valid(unsigned int cmd_a,
unsigned int cmd_b, size_t min_size)
{
if (_IOC_DIR(cmd_a) != _IOC_DIR(cmd_b))
return false;
if (_IOC_TYPE(cmd_a) != _IOC_TYPE(cmd_b))
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v5 02/10] vfs: add for_each_inode()
2026-12-01 6:46 ` [PATCH v5 02/10] vfs: add for_each_inode() Huang Shijie
@ 2026-12-01 8:33 ` Christoph Hellwig
2026-12-01 9:25 ` Huang Shijie
2026-12-01 11:36 ` Andreas Gruenbacher
0 siblings, 2 replies; 15+ messages in thread
From: Christoph Hellwig @ 2026-12-01 8:33 UTC (permalink / raw)
To: Huang Shijie
Cc: viro, brauner, jack, hch, agruenba, axboe, gnoack, paul, jmorris,
serge, legion, aleksa, libaokun, ebiggers, djwong, sandeen,
zhongyuan, fangbaoshun, yingzhiwei, wujianyong, shijie8,
wangfengyu, linux-fsdevel, linux-kernel, linux-block, gfs2
On Tue, Dec 01, 2026 at 02:46:31PM +0800, Huang Shijie wrote:
> Walking sb->s_inodes requires taking 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 the inode state checks and the reference count.
>
> Add a caller-owned cursor wrapped in a cleanup class, so that the
> iterator goes out of scope as soon as the loop ends:
>
> for_each_inode(inode, sb) {
> /* inode is referenced and unlocked here */
> }
I know you've gotten contrary feedback, but all this CLASS and macro
magic is a complete nightmare. Compared to a simple interface like
writeback_iter() this adds tons of gluecode that makes the code really
hard to understand, even more so when in backtraces after a crash.
All for the tiny "benefit" of allowing a break or goto out of the loop,
which isn't even needed in the usual patterns.
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v5 02/10] vfs: add for_each_inode()
2026-12-01 8:33 ` Christoph Hellwig
@ 2026-12-01 9:25 ` Huang Shijie
2026-12-01 11:36 ` Andreas Gruenbacher
1 sibling, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 9:25 UTC (permalink / raw)
To: Christoph Hellwig
Cc: viro, brauner, jack, agruenba, axboe, gnoack, paul, jmorris,
serge, legion, aleksa, libaokun, ebiggers, djwong, sandeen,
zhongyuan, fangbaoshun, yingzhiwei, wujianyong, shijie8,
wangfengyu, linux-fsdevel, linux-kernel, linux-block, gfs2
在 Tue, Dec 01, 2026 at 12:33:38AM -0800,Christoph Hellwig 写道:
> On Tue, Dec 01, 2026 at 02:46:31PM +0800, Huang Shijie wrote:
> > Walking sb->s_inodes requires taking 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 the inode state checks and the reference count.
> >
> > Add a caller-owned cursor wrapped in a cleanup class, so that the
> > iterator goes out of scope as soon as the loop ends:
> >
> > for_each_inode(inode, sb) {
> > /* inode is referenced and unlocked here */
> > }
>
> I know you've gotten contrary feedback, but all this CLASS and macro
It seems I did the work in the wrong direction..
> magic is a complete nightmare. Compared to a simple interface like
> writeback_iter() this adds tons of gluecode that makes the code really
The v4 code is like the writeback_iter():
v4: https://lore.kernel.org/all/20261125091157.1822521-1-huangsj@hygon.cn/
Could you please comment on the V4 code?
Thanks
Huang Shijie
> hard to understand, even more so when in backtraces after a crash.
>
> All for the tiny "benefit" of allowing a break or goto out of the loop,
> which isn't even needed in the usual patterns.
>
>
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v5 02/10] vfs: add for_each_inode()
2026-12-01 8:33 ` Christoph Hellwig
2026-12-01 9:25 ` Huang Shijie
@ 2026-12-01 11:36 ` Andreas Gruenbacher
1 sibling, 0 replies; 15+ messages in thread
From: Andreas Gruenbacher @ 2026-12-01 11:36 UTC (permalink / raw)
To: Christoph Hellwig
Cc: Huang Shijie, viro, brauner, jack, axboe, gnoack, paul, jmorris,
serge, legion, aleksa, libaokun, ebiggers, djwong, sandeen,
zhongyuan, fangbaoshun, yingzhiwei, wujianyong, shijie8,
wangfengyu, linux-fsdevel, linux-kernel, linux-block, gfs2
On Tue, 1 Dec 2026 at 09:39, Christoph Hellwig <hch@infradead.org> wrote:
> On Tue, Dec 01, 2026 at 02:46:31PM +0800, Huang Shijie wrote:
> > Walking sb->s_inodes requires taking 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 the inode state checks and the reference count.
> >
> > Add a caller-owned cursor wrapped in a cleanup class, so that the
> > iterator goes out of scope as soon as the loop ends:
> >
> > for_each_inode(inode, sb) {
> > /* inode is referenced and unlocked here */
> > }
>
> I know you've gotten contrary feedback, but all this CLASS and macro
> magic is a complete nightmare. Compared to a simple interface like
> writeback_iter() this adds tons of gluecode that makes the code really
> hard to understand, even more so when in backtraces after a crash.
The problem is that in some of those loops, we need a custom
precondition checked while the inode is locked. In the
for_each_inode() implementation from this posting, that precondition
is macro expanded. In a writeback_iter() type approach, that
precondition needs to go into a callback, and that is a really, really
bad idea we should rather avoid. A "simple" writeback_iter() interface
simply doesn't work here.
On top of that, we still have evict_inodes() with a
for_each_inode_locked() type loop with an additional precondition that
is checked before the inode is locked, and hook_sb_delete() with a
for_each_inode() type loop with the same kind of precondition. We
could leave those as they are, or we could complicate the iterator
infrastructure to make it fit those cases as well.
> All for the tiny "benefit" of allowing a break or goto out of the loop,
> which isn't even needed in the usual patterns.
I agree that the cleanup class isn't that important here. It could be
taken out again, but that wouldn't actually improve anything; you
would remain just as unhappy with the rest.
So what's the conclusion: that we don't really want to change things
at all? Or that we want to change things towards a filesystem specific
iterator construct as a first step towards eliminating the inodes
list?
Thanks,
Andreas
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v5 03/10] vfs: add for_each_inode_locked()
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
2026-12-01 6:46 ` [PATCH v5 01/10] jffs2: rename the local for_each_inode() macro Huang Shijie
2026-12-01 6:46 ` [PATCH v5 02/10] vfs: add for_each_inode() Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 8:34 ` Christoph Hellwig
2026-12-01 6:46 ` [PATCH v5 04/10] block: convert nr_blockdev_pages() to for_each_inode_locked() Huang Shijie
` (6 subsequent siblings)
9 siblings, 1 reply; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Some walkers of sb->s_inodes cannot take a reference on the inode they
are looking at.
Give them their own iterator, which keeps sb->s_inode_list_lock held
for the whole walk and takes no reference:
for_each_inode_locked(inode, sb) {
/* inode is only guaranteed to exist */
}
The body runs in atomic context, must not block and must do its own
inode state checks. The lock is dropped after the loop on every exit
path, so the body must not release or reacquire it.
No caller is converted yet, so this is a no-op.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
include/linux/fs.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 383f41c022ec..1f444ad89c17 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3736,12 +3736,70 @@ DEFINE_CLASS(sb_inode_iter, struct sb_inode_iter,
!__it.done && __sb_inode_iter_step(&__it, inode, true);)
#define __for_each_inode_3(inode, sb, cond) \
for (CLASS(sb_inode_iter, __it)(sb); \
!__it.done && __sb_inode_iter_step(&__it, inode, cond);)
+struct sb_inode_iter_locked {
+ struct super_block *sb;
+ struct inode *cursor;
+ bool done;
+};
+
+static inline struct sb_inode_iter_locked
+sb_inode_iter_locked_init(struct super_block *sb)
+{
+ spin_lock(&sb->s_inode_list_lock);
+ return (struct sb_inode_iter_locked){ .sb = sb };
+}
+
+static inline void sb_inode_iter_locked_end(struct sb_inode_iter_locked *it)
+{
+ spin_unlock(&it->sb->s_inode_list_lock);
+}
+
+static inline struct inode *
+sb_inode_iter_locked_next(struct sb_inode_iter_locked *it)
+{
+ struct inode *inode;
+
+ if (it->done)
+ return NULL;
+
+ if (it->cursor)
+ inode = list_next_entry(it->cursor, i_sb_list);
+ else
+ inode = list_first_entry(&it->sb->s_inodes, struct inode,
+ i_sb_list);
+
+ if (list_entry_is_head(inode, &it->sb->s_inodes, i_sb_list)) {
+ it->done = true;
+ return NULL;
+ }
+
+ it->cursor = inode;
+ return inode;
+}
+
+DEFINE_CLASS(sb_inode_iter_locked, struct sb_inode_iter_locked,
+ sb_inode_iter_locked_end(&_T),
+ sb_inode_iter_locked_init(sb),
+ struct super_block *sb)
+
+/*
+ * Walk the inodes of @sb with sb->s_inode_list_lock held.
+ * -- @inode is not referenced: the body only gets an existence
+ * guarantee, so it must do its own inode state checks.
+ * -- The body runs in atomic context and must not block.
+ * -- The lock is released after the loop on every exit path, so the body
+ * must not release or reacquire it.
+ */
+#define for_each_inode_locked(inode, sb) \
+ for (CLASS(sb_inode_iter_locked, __it)(sb); \
+ ((inode) = sb_inode_iter_locked_next(&__it));)
+
static inline bool extensible_ioctl_valid(unsigned int cmd_a,
unsigned int cmd_b, size_t min_size)
{
if (_IOC_DIR(cmd_a) != _IOC_DIR(cmd_b))
return false;
if (_IOC_TYPE(cmd_a) != _IOC_TYPE(cmd_b))
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v5 03/10] vfs: add for_each_inode_locked()
2026-12-01 6:46 ` [PATCH v5 03/10] vfs: add for_each_inode_locked() Huang Shijie
@ 2026-12-01 8:34 ` Christoph Hellwig
0 siblings, 0 replies; 15+ messages in thread
From: Christoph Hellwig @ 2026-12-01 8:34 UTC (permalink / raw)
To: Huang Shijie
Cc: viro, brauner, jack, hch, agruenba, axboe, gnoack, paul, jmorris,
serge, legion, aleksa, libaokun, ebiggers, djwong, sandeen,
zhongyuan, fangbaoshun, yingzhiwei, wujianyong, shijie8,
wangfengyu, linux-fsdevel, linux-kernel, linux-block, gfs2
This actually complicates the callers. What is the point?
And why mix it up with the other change just to make the review
harder because not there is a stream of patches converting to different
iterators.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v5 04/10] block: convert nr_blockdev_pages() to for_each_inode_locked()
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
` (2 preceding siblings ...)
2026-12-01 6:46 ` [PATCH v5 03/10] vfs: add for_each_inode_locked() Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 6:46 ` [PATCH v5 05/10] fs/quota: convert remove_dquot_ref() " Huang Shijie
` (5 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Convert nr_blockdev_pages() to for_each_inode_locked().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
block/bdev.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/block/bdev.c b/block/bdev.c
index f20f91b3176e..ebc54a3e48a6 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -560,17 +560,14 @@ void bdev_drop(struct block_device *bdev)
long nr_blockdev_pages(void)
{
struct inode *inode;
long ret = 0;
- spin_lock(&blockdev_superblock->s_inode_list_lock);
- list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
+ for_each_inode_locked(inode, blockdev_superblock)
ret += inode->i_mapping->nrpages;
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
-
return ret;
}
/**
* bd_may_claim - test whether a block device can be claimed
* @bdev: block device of interest
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 05/10] fs/quota: convert remove_dquot_ref() to for_each_inode_locked()
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
` (3 preceding siblings ...)
2026-12-01 6:46 ` [PATCH v5 04/10] block: convert nr_blockdev_pages() to for_each_inode_locked() Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 6:46 ` [PATCH v5 06/10] fs: convert generic_shutdown_super() poison loop Huang Shijie
` (4 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Convert remove_dquot_ref() to for_each_inode_locked().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/quota/dquot.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c
index a3d503591871..af32ce73cdd0 100644
--- a/fs/quota/dquot.c
+++ b/fs/quota/dquot.c
@@ -1121,14 +1121,13 @@ static void remove_dquot_ref(struct super_block *sb, int type)
{
struct inode *inode;
#ifdef CONFIG_QUOTA_DEBUG
int reserved = 0;
#endif
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
+ for_each_inode_locked(inode, sb) {
/*
* We have to scan also I_NEW inodes because they can already
* have quota pointer initialized. Luckily, we need to touch
* only quota pointers and these have separate locking
* (dq_data_lock).
*/
@@ -1146,13 +1145,12 @@ static void remove_dquot_ref(struct super_block *sb, int type)
rcu_assign_pointer(dquots[type], NULL);
if (dquot)
dqput(dquot);
}
spin_unlock(&dq_data_lock);
}
- spin_unlock(&sb->s_inode_list_lock);
#ifdef CONFIG_QUOTA_DEBUG
if (reserved) {
printk(KERN_WARNING "VFS (%s): Writes happened after quota"
" was disabled thus quota information is probably "
"inconsistent. Please run quotacheck(8).\n", sb->s_id);
}
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 06/10] fs: convert generic_shutdown_super() poison loop
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
` (4 preceding siblings ...)
2026-12-01 6:46 ` [PATCH v5 05/10] fs/quota: convert remove_dquot_ref() " Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 6:46 ` [PATCH v5 07/10] block: convert sync_bdevs() to for_each_inode() Huang Shijie
` (3 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Convert the generic_shutdown_super() poison loop to
for_each_inode_locked().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/super.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/fs/super.c b/fs/super.c
index c5a0658f7aaf..10b4360d9550 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -735,19 +735,17 @@ void generic_shutdown_super(struct super_block *sb)
* Adding a proper bailout path here would be hard, but
* we can at least make it more likely that a later
* iput_final() or such crashes cleanly.
*/
struct inode *inode;
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
+ for_each_inode_locked(inode, sb) {
inode->i_op = VFS_PTR_POISON;
inode->i_sb = VFS_PTR_POISON;
inode->i_mapping = VFS_PTR_POISON;
}
- spin_unlock(&sb->s_inode_list_lock);
}
}
/*
* Broadcast to everyone that grabbed a temporary reference to this
* superblock that it is dying. Every walker of @fs_supers outside
* of sget_fc() will now discard this superblock and treat it as
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 07/10] block: convert sync_bdevs() to for_each_inode()
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
` (5 preceding siblings ...)
2026-12-01 6:46 ` [PATCH v5 06/10] fs: convert generic_shutdown_super() poison loop Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 6:46 ` [PATCH v5 08/10] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
` (2 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Convert sync_bdevs() to for_each_inode().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
block/bdev.c | 38 +++++---------------------------------
1 file changed, 5 insertions(+), 33 deletions(-)
diff --git a/block/bdev.c b/block/bdev.c
index ebc54a3e48a6..c57c1414d75f 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -1332,48 +1332,24 @@ void bdev_mark_dead(struct block_device *bdev, bool surprise)
* gendisk that otherwise looks a lot like a safe device removal.
*/
EXPORT_SYMBOL_GPL(bdev_mark_dead);
void sync_bdevs(bool wait)
{
- struct inode *inode, *old_inode = NULL;
-
- spin_lock(&blockdev_superblock->s_inode_list_lock);
- list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
- struct address_space *mapping = inode->i_mapping;
- struct block_device *bdev;
+ struct inode *inode;
- spin_lock(&inode->i_lock);
- if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW) ||
- mapping->nrpages == 0) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
- /*
- * We hold a reference to 'inode' so it couldn't have been
- * removed from s_inodes list while we dropped the
- * s_inode_list_lock We cannot iput the inode now as we can
- * be holding the last reference and we cannot iput it under
- * s_inode_list_lock. So we keep the reference and iput it
- * later.
- */
- iput(old_inode);
- old_inode = inode;
- bdev = I_BDEV(inode);
+ for_each_inode(inode, blockdev_superblock,
+ inode->i_mapping->nrpages != 0) {
+ struct block_device *bdev = I_BDEV(inode);
/*
* The inode does not keep the gendisk of a partition alive, the
* device does.
*/
- if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) {
- spin_lock(&blockdev_superblock->s_inode_list_lock);
+ if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
continue;
- }
mutex_lock(&bdev->bd_disk->open_mutex);
if (!atomic_read(&bdev->bd_openers)) {
; /* skip */
} else if (wait) {
/*
@@ -1385,17 +1361,13 @@ void sync_bdevs(bool wait)
filemap_fdatawait_keep_errors(inode->i_mapping);
} else {
filemap_fdatawrite(inode->i_mapping);
}
mutex_unlock(&bdev->bd_disk->open_mutex);
put_device(&bdev->bd_device);
-
- spin_lock(&blockdev_superblock->s_inode_list_lock);
}
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
- iput(old_inode);
}
/*
* Handle STATX_{DIOALIGN, WRITE_ATOMIC} for block devices.
*/
void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask)
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 08/10] fs/drop_caches: convert drop_pagecache_sb() to for_each_inode()
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
` (6 preceding siblings ...)
2026-12-01 6:46 ` [PATCH v5 07/10] block: convert sync_bdevs() to for_each_inode() Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 6:46 ` [PATCH v5 09/10] fs/quota: convert add_dquot_ref() " Huang Shijie
2026-12-01 6:46 ` [PATCH v5 10/10] gfs2: convert gfs2_evict_inodes() " Huang Shijie
9 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Convert drop_pagecache_sb() to for_each_inode().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/drop_caches.c | 31 +++++++------------------------
1 file changed, 7 insertions(+), 24 deletions(-)
diff --git a/fs/drop_caches.c b/fs/drop_caches.c
index 28c5fb9c7357..6967813a42ed 100644
--- a/fs/drop_caches.c
+++ b/fs/drop_caches.c
@@ -16,43 +16,26 @@
/* A global variable is a bit ugly, but it keeps the code simple */
static int sysctl_drop_caches;
static void drop_pagecache_sb(struct super_block *sb, void *unused)
{
- struct inode *inode, *toput_inode = NULL;
+ struct inode *inode;
if (sb->s_bdi == &noop_backing_dev_info)
return;
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- spin_lock(&inode->i_lock);
- /*
- * We must skip inodes in unusual state. We may also skip
- * inodes without pages but we deliberately won't in case
- * we need to reschedule to avoid softlockups.
- */
- if ((inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) ||
- (mapping_empty(inode->i_mapping) && !need_resched())) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&sb->s_inode_list_lock);
-
+ /*
+ * We may skip inodes without pages but we deliberately won't in
+ * case we need to reschedule to avoid softlockups.
+ */
+ for_each_inode(inode, sb,
+ !mapping_empty(inode->i_mapping) || need_resched()) {
invalidate_mapping_pages(inode->i_mapping, 0, -1);
- iput(toput_inode);
- toput_inode = inode;
-
cond_resched();
- spin_lock(&sb->s_inode_list_lock);
}
- spin_unlock(&sb->s_inode_list_lock);
- iput(toput_inode);
}
static int drop_caches_sysctl_handler(const struct ctl_table *table, int write,
void *buffer, size_t *length, loff_t *ppos)
{
int ret;
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 09/10] fs/quota: convert add_dquot_ref() to for_each_inode()
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
` (7 preceding siblings ...)
2026-12-01 6:46 ` [PATCH v5 08/10] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
2026-12-01 6:46 ` [PATCH v5 10/10] gfs2: convert gfs2_evict_inodes() " Huang Shijie
9 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Convert add_dquot_ref() to for_each_inode().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/quota/dquot.c | 38 +++++---------------------------------
1 file changed, 5 insertions(+), 33 deletions(-)
diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c
index af32ce73cdd0..f88c0168645d 100644
--- a/fs/quota/dquot.c
+++ b/fs/quota/dquot.c
@@ -1059,57 +1059,29 @@ static int dqinit_needed(struct inode *inode, int type)
return 0;
}
/* This routine is guarded by s_umount semaphore */
static int add_dquot_ref(struct super_block *sb, int type)
{
- struct inode *inode, *old_inode = NULL;
+ struct inode *inode;
#ifdef CONFIG_QUOTA_DEBUG
int reserved = 0;
#endif
int err = 0;
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- spin_lock(&inode->i_lock);
- if ((inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) ||
- !atomic_read(&inode->i_writecount) ||
- !dqinit_needed(inode, type)) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&sb->s_inode_list_lock);
-
+ for_each_inode(inode, sb, atomic_read(&inode->i_writecount) &&
+ dqinit_needed(inode, type)) {
#ifdef CONFIG_QUOTA_DEBUG
if (unlikely(inode_get_rsv_space(inode) > 0))
reserved = 1;
#endif
- iput(old_inode);
err = __dquot_initialize(inode, type);
- if (err) {
- iput(inode);
- goto out;
- }
-
- /*
- * We hold a reference to 'inode' so it couldn't have been
- * removed from s_inodes list while we dropped the
- * s_inode_list_lock. We cannot iput the inode now as we can be
- * holding the last reference and we cannot iput it under
- * s_inode_list_lock. So we keep the reference and iput it
- * later.
- */
- old_inode = inode;
+ if (err)
+ break;
cond_resched();
- spin_lock(&sb->s_inode_list_lock);
}
- spin_unlock(&sb->s_inode_list_lock);
- iput(old_inode);
-out:
#ifdef CONFIG_QUOTA_DEBUG
if (reserved) {
quota_error(sb, "Writes happened before quota was turned on "
"thus quota information is probably inconsistent. "
"Please run quotacheck(8)");
}
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v5 10/10] gfs2: convert gfs2_evict_inodes() to for_each_inode()
2026-12-01 6:46 [PATCH v5 00/10] vfs: add for_each_inode() Huang Shijie
` (8 preceding siblings ...)
2026-12-01 6:46 ` [PATCH v5 09/10] fs/quota: convert add_dquot_ref() " Huang Shijie
@ 2026-12-01 6:46 ` Huang Shijie
9 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-12-01 6:46 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, wangfengyu, linux-fsdevel, linux-kernel, linux-block,
gfs2, Huang Shijie
Convert gfs2_evict_inodes() to for_each_inode().
The original code also handed out an inode which was already being torn
down when need_resched() was set. That is not preserved: taking a
reference on an I_FREEING inode whose count has already reached zero
lets a second iput() run on it once the reference is dropped again.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/gfs2/ops_fstype.c | 26 ++++++--------------------
1 file changed, 6 insertions(+), 20 deletions(-)
diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 6ec47376aecf..22bb50509904 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -1736,37 +1736,23 @@ static int gfs2_meta_init_fs_context(struct fs_context *fc)
* version of evict_inodes() avoids that.
*
* Modeled after drop_pagecache_sb().
*/
static void gfs2_evict_inodes(struct super_block *sb)
{
- struct inode *inode, *toput_inode = NULL;
struct gfs2_sbd *sdp = sb->s_fs_info;
+ struct inode *inode __maybe_unused;
set_bit(SDF_EVICTING, &sdp->sd_flags);
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- spin_lock(&inode->i_lock);
- if ((inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) &&
- !need_resched()) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&sb->s_inode_list_lock);
-
- iput(toput_inode);
- toput_inode = inode;
-
+ /*
+ * Nothing to do with each inode: we only want to bounce it through
+ * iput() so that it is evicted while the list lock is dropped.
+ */
+ for_each_inode(inode, sb)
cond_resched();
- spin_lock(&sb->s_inode_list_lock);
- }
- spin_unlock(&sb->s_inode_list_lock);
- iput(toput_inode);
}
static void gfs2_kill_sb(struct super_block *sb)
{
struct gfs2_sbd *sdp = sb->s_fs_info;
--
2.34.1
^ permalink raw reply [flat|nested] 15+ messages in thread