Re: [PATCH v2 1/3] vfs: add inode iteration methods

From: Huang Shijie

Date: Mon Nov 16 2026 - 01:46:12 EST


在 Fri, Nov 13, 2026 at 04:23:40AM -0800,Christoph Hellwig 写道:
> On Fri, Nov 13, 2026 at 07:57:00PM +0800, Huang Shijie wrote:
> > +/*
> > + * Unsafe iteration for internal VFS use only:
> > + * @ctx->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_unsafe(struct super_block *sb,
> > + struct inode_iter_context *ctx)
> > +{
> > + struct inode *inode;
> > + int ret = INO_ITER_RET_NEXT;
> > +
> > + rcu_read_lock();
> > + spin_lock(&sb->s_inode_list_lock);
> > + inode = list_prepare_entry(ctx->prev, &sb->s_inodes, i_sb_list);
> > +
> > + list_for_each_entry_continue(inode, &sb->s_inodes, i_sb_list) {
>
> This is a really odd way to write a list iteration. I also don't see
This is for the evict_inodes() which will break and then resume the iteration.

> how the RCU lock is needed when you already hold the spinlock.
okay. I will remove the rcu_read_lock in future.

>
> I honestly don't get this "iterator. It is trivial, the _unsafe is
> wrong naming. What is the point?
What about super_iter_inodes_locked()? ore super_iter_inodes_noref()?

We use this function to hide the fs systems to use the sb->s_inode_list_lock
directly. Then we can add more code to optimize the iteration.

>
> > +/**
> > + * super_iter_inodes - iterate all the cached inodes on a superblock
> > + * @sb: superblock to iterate
> > + * @ctx: the context which has the metadata
> > + *
> > + * This function iterates all cached inodes on a superblock that are not in
> > + * the process of being initialised or torn down. It will run @ctx->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.
> > + *
> > + * 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, struct inode_iter_context *ctx)
>
> This one makes a lot more sense, but the callback pattern is a bit
> annoying as it requires to stuff all information into the context.
> Im much of the core kernel we're slowly switching to an iterator
> model that allows the caller to keep the state. In this case something
> like
>
> struct inode_iter_contex ctx = INODE_ITER_CTX_INIT;
>
> while ((inode = sb_inode_iter()))) {
> /* do stuff */
> }
>
How to manage the sb->s_inode_list_lock in this code?

I have no idea how to hide the sb->s_inode_list_lock in this code.


> which should be easily possibly with this model.
>
> You probably also want to split the core iterator and switching each
> user into separate patches.
no problem. I can split them later.

Thanks
Huang Shijie