mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pierre Barre <pierre@barre.sh>
To: ericvh@kernel.org, lucho@ionkov.net, asmadeus@codewreck.org
Cc: linux_oss@crudebyte.com, v9fs@lists.linux.dev,
	linux-kernel@vger.kernel.org, Pierre Barre <pierre@barre.sh>
Subject: [PATCH 2/2] 9p: invalidate readdir buffer on llseek
Date: Thu, 16 Apr 2026 07:26:19 +0000	[thread overview]
Message-ID: <20260416072619.14241-2-pierre@barre.sh> (raw)
In-Reply-To: <20260416072619.14241-1-pierre@barre.sh>

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 <pierre@barre.sh>
---
 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


      reply	other threads:[~2026-04-16  7:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-16  7:26 [PATCH 1/2] 9p: use kvzalloc for readdir buffer Pierre Barre
2026-04-16  7:26 ` Pierre Barre [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260416072619.14241-2-pierre@barre.sh \
    --to=pierre@barre.sh \
    --cc=asmadeus@codewreck.org \
    --cc=ericvh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux_oss@crudebyte.com \
    --cc=lucho@ionkov.net \
    --cc=v9fs@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®