Re: [PATCH v2 1/3] vfs: add inode iteration methods
From: Christoph Hellwig
Date: Fri Nov 13 2026 - 08:32:57 EST
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
how the RCU lock is needed when you already hold the spinlock.
I honestly don't get this "iterator. It is trivial, the _unsafe is
wrong naming. What is the point?
> +/**
> + * 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 */
}
which should be easily possibly with this model.
You probably also want to split the core iterator and switching each
user into separate patches.