[SRU][Focal][PATCH 1/3] (upstream) NFS: Clear the file access cache upon login

Chengen Du chengen.du at canonical.com
Wed Jan 18 14:57:06 UTC 2023


From: Trond Myklebust <trond.myklebust at hammerspace.com>

BugLink: https://bugs.launchpad.net/bugs/2003053

POSIX typically only refreshes the user's supplementary group
information upon login. Since NFS servers may often refresh their
concept of the user supplementary group membership at their own cadence,
it is possible for the NFS client's access cache to become stale due to
the user's group membership changing on the server after the user has
already logged in on the client.
While it is reasonable to expect that such group membership changes are
rare, and that we do not want to optimise the cache to accommodate them,
it is also not unreasonable for the user to expect that if they log out
and log back in again, that the staleness would clear up.

Reviewed-by: Benjamin Coddington <bcodding at redhat.com>
Tested-by: Benjamin Coddington <bcodding at redhat.com>
Signed-off-by: Trond Myklebust <trond.myklebust at hammerspace.com>
(backported from commit 0eb43812c0270ee3d005ff32f91f7d0a6c4943af)
Signed-off-by: Chengen Du <chengen.du at canonical.com>
---
 fs/nfs/dir.c           | 23 +++++++++++++++++++++++
 include/linux/nfs_fs.h |  1 +
 2 files changed, 24 insertions(+)

diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
index 28ceee102d0b..f35b705d2351 100644
--- a/fs/nfs/dir.c
+++ b/fs/nfs/dir.c
@@ -2350,9 +2350,28 @@ static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, co
 	return NULL;
 }
 
+static u64 nfs_access_login_time(const struct task_struct *task,
+				 const struct cred *cred)
+{
+	const struct task_struct *parent;
+	u64 ret;
+
+	rcu_read_lock();
+	for (;;) {
+		parent = rcu_dereference(task->real_parent);
+		if (parent == task || cred_fscmp(parent->cred, cred) != 0)
+			break;
+		task = parent;
+	}
+	ret = task->start_time;
+	rcu_read_unlock();
+	return ret;
+}
+
 static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
 {
 	struct nfs_inode *nfsi = NFS_I(inode);
+	u64 login_time = nfs_access_login_time(current, cred);
 	struct nfs_access_entry *cache;
 	bool retry = true;
 	int err;
@@ -2380,6 +2399,9 @@ static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, s
 		spin_lock(&inode->i_lock);
 		retry = false;
 	}
+	err = -ENOENT;
+	if ((s64)(login_time - cache->timestamp) > 0)
+		goto out;
 	res->cred = cache->cred;
 	res->mask = cache->mask;
 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
@@ -2445,6 +2467,7 @@ static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *
 		else
 			goto found;
 	}
+	set->timestamp = ktime_get_ns();
 	rb_link_node(&set->rb_node, parent, p);
 	rb_insert_color(&set->rb_node, root_node);
 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
diff --git a/include/linux/nfs_fs.h b/include/linux/nfs_fs.h
index 7c59af824e4e..9a2d56d4b6dd 100644
--- a/include/linux/nfs_fs.h
+++ b/include/linux/nfs_fs.h
@@ -52,6 +52,7 @@ struct nfs_access_entry {
 	struct rb_node		rb_node;
 	struct list_head	lru;
 	const struct cred *	cred;
+	u64			timestamp;
 	__u32			mask;
 	struct rcu_head		rcu_head;
 };
-- 
2.25.1




More information about the kernel-team mailing list