Re: [PATCH v5 02/10] vfs: add for_each_inode()

From: Andreas Gruenbacher

Date: Tue Dec 01 2026 - 06:37:33 EST


On Tue, 1 Dec 2026 at 09:39, Christoph Hellwig <hch@xxxxxxxxxxxxx> 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