[PATCH 2/2] 9p: invalidate readdir buffer on llseek

Pierre Barre <[email protected]> Thu, 16 Apr 2026 07:26:19 +0000
Newsgroups dev.linux.lists.v9fs,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
v9fs_dir_operations{,_dotl} use generic_file_llseek, which updates
f_pos but does not invalidate fid->rdir. After an lseek on a
directory fd, the next getdents64 continues draining stale cached
entries instead of refetching from the server at the new offset.
This causes xfstests generic/637 to fail.

Replace generic_file_llseek with a custom v9fs_dir_llseek that
resets rdir->head and rdir->tail when the position changes,
following the same f_lock pattern used by nfs_llseek_dir.

Also change the readdir refetch condition from == to >= to prevent
an infinite loop if lseek races with an in-progress readdir.

Signed-off-by: Pierre Barre <[email protected]>
---
 fs/9p/vfs_dir.c | 53 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 4 deletions(-)

diff --git a/fs/9p/vfs_dir.c b/fs/9p/vfs_dir.c
index 487c177aae38..b0f8913380bc 100644
--- a/fs/9p/vfs_dir.c
+++ b/fs/9p/vfs_dir.c
@@ -103,7 +103,7 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
 	kvec.iov_len = buflen;
 
 	while (1) {
-		if (rdir->tail == rdir->head) {
+		if (rdir->head >= rdir->tail) {
 			struct iov_iter to;
 			int n;
 
@@ -162,7 +162,7 @@ static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
 		return -ENOMEM;
 
 	while (1) {
-		if (rdir->tail == rdir->head) {
+		if (rdir->head >= rdir->tail) {
 			err = p9_client_readdir(fid, rdir->buf, buflen,
 						ctx->pos);
 			if (err <= 0)
@@ -195,6 +195,51 @@ static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
 }
 
 
+/**
+ * v9fs_dir_llseek - seek on a directory
+ * @filp: file structure
+ * @offset: new position
+ * @whence: SEEK_SET or SEEK_CUR
+ *
+ * Invalidate the readdir buffer when the position changes so the next
+ * getdents call refetches from the server at the new offset.
+ */
+
+static loff_t v9fs_dir_llseek(struct file *filp, loff_t offset, int whence)
+{
+	struct p9_fid *fid = filp->private_data;
+
+	switch (whence) {
+	default:
+		return -EINVAL;
+	case SEEK_SET:
+		if (offset < 0)
+			return -EINVAL;
+		spin_lock(&filp->f_lock);
+		break;
+	case SEEK_CUR:
+		if (offset == 0)
+			return filp->f_pos;
+		spin_lock(&filp->f_lock);
+		offset += filp->f_pos;
+		if (offset < 0) {
+			spin_unlock(&filp->f_lock);
+			return -EINVAL;
+		}
+	}
+	if (offset != filp->f_pos) {
+		struct p9_rdir *rdir = fid->rdir;
+
+		filp->f_pos = offset;
+		if (rdir) {
+			rdir->head = 0;
+			rdir->tail = 0;
+		}
+	}
+	spin_unlock(&filp->f_lock);
+	return offset;
+}
+
 /**
  * v9fs_dir_release - close a directory or a file
  * @inode: inode of the directory or file
@@ -238,7 +283,7 @@ int v9fs_dir_release(struct inode *inode, struct file *filp)
 
 const struct file_operations v9fs_dir_operations = {
 	.read = generic_read_dir,
-	.llseek = generic_file_llseek,
+	.llseek = v9fs_dir_llseek,
 	.iterate_shared = v9fs_dir_readdir,
 	.open = v9fs_file_open,
 	.release = v9fs_dir_release,
@@ -247,7 +292,7 @@ const struct file_operations v9fs_dir_operations = {
 
 const struct file_operations v9fs_dir_operations_dotl = {
 	.read = generic_read_dir,
-	.llseek = generic_file_llseek,
+	.llseek = v9fs_dir_llseek,
 	.iterate_shared = v9fs_dir_readdir_dotl,
 	.open = v9fs_file_open,
 	.release = v9fs_dir_release,
-- 
2.51.0