[PATCH 1/6] kernfs: report times and link count from the node in getattr

From: Shakeel Butt

Date: Wed Nov 18 2026 - 17:02:13 EST


getattr and permission copy a node's attributes into its inode while
holding kernfs_iattr_rwsem for reading, so several of them can write
the same inode at once. That is safe only because every update of
these attributes takes the lock for writing, so they all write the same
values. Commit aa1d058d48f2 ("kernfs: dont take i_lock on inode attr
read") relies on this. Setting up a node's first iattrs from setxattr
does not take the lock, but a refresh that misses them writes no times.

The next patches let a directory's times and link count change without
that lock. A slow refresh could then write an old value after a newer
one, and stat() could return a stale or torn time or link count.

Make getattr report the times and link count from the node instead of
the inode. Permission checks use neither. Nothing changes yet, except
that a stat() racing a node's first setxattr can report the new times.

Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
fs/kernfs/inode.c | 32 ++++++++++++++++++++++++++++++--
1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 53b1a2796e0b..3616161a56cb 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -162,9 +162,22 @@ static inline void set_inode_attr(struct inode *inode,
inode_set_ctime_to_ts(inode, attrs->ia_ctime);
}

+/*
+ * Get the link count of a directory: its subdirectories plus two.
+ * Return false for other nodes and for a directory being removed.
+ */
+static bool kernfs_dir_nlink(struct kernfs_node *kn, unsigned int *nlink)
+{
+ if (kernfs_type(kn) != KERNFS_DIR || (kn->flags & KERNFS_REMOVING))
+ return false;
+ *nlink = kn->dir.subdirs + 2;
+ return true;
+}
+
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs;
+ unsigned int nlink;

inode->i_mode = kn->mode;
attrs = kernfs_iattrs_noalloc(kn);
@@ -175,8 +188,8 @@ static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
*/
set_inode_attr(inode, attrs);

- if (kernfs_type(kn) == KERNFS_DIR && !(kn->flags & KERNFS_REMOVING))
- set_nlink(inode, kn->dir.subdirs + 2);
+ if (kernfs_dir_nlink(kn, &nlink))
+ set_nlink(inode, nlink);
}

int kernfs_iop_getattr(const struct mnt_idmap *idmap,
@@ -186,10 +199,25 @@ int kernfs_iop_getattr(const struct mnt_idmap *idmap,
struct inode *inode = d_inode(path->dentry);
struct kernfs_node *kn = inode->i_private;
struct kernfs_root *root = kernfs_root(kn);
+ struct kernfs_iattrs *attrs;
+ unsigned int nlink;

down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
+
+ /*
+ * Other refreshes may store older times or link counts in the
+ * inode at the same time. Report the values from the node.
+ */
+ attrs = kernfs_iattrs_noalloc(kn);
+ if (attrs) {
+ stat->atime = attrs->ia_atime;
+ stat->mtime = attrs->ia_mtime;
+ stat->ctime = attrs->ia_ctime;
+ }
+ if (kernfs_dir_nlink(kn, &nlink))
+ stat->nlink = nlink;
up_read(&root->kernfs_iattr_rwsem);

return 0;
--
2.53.1-Meta