linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] libfs: getdents() should return 0 after reaching EOD
@ 2023-11-18 19:33 Chuck Lever
  2023-11-18 22:11 ` Chuck Lever
  0 siblings, 1 reply; 6+ messages in thread
From: Chuck Lever @ 2023-11-18 19:33 UTC (permalink / raw)
  To: akpm, brauner, hughd, jlayton, viro
  Cc: Tavian Barnes, Chuck Lever, linux-fsdevel, linux-mm

From: Chuck Lever <chuck.lever@oracle.com>

The new directory offset helpers don't conform with the convention
of getdents() returning no more entries once a directory file
descriptor has reached the current end-of-directory.

To address this, copy the logic from dcache_readdir() to mark the
open directory file descriptor once EOD has been reached. Rewinding
resets the mark.

Reported-by: Tavian Barnes <tavianator@tavianator.com>
Closes: https://lore.kernel.org/linux-fsdevel/20231113180616.2831430-1-tavianator@tavianator.com/
Fixes: 6faddda69f62 ("libfs: Add directory operations for stable offsets")
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
 fs/libfs.c |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

This patch passes Tavian's reproducer. fstests over NFS shows no
regression. However, generic/676 fails when running directly against
a tmpfs mount:

     QA output created by 676
    -All tests passed
    +Unexpected EOF while reading dir.

I will look into that.

Changes since v2:
- Go back to marking EOD in file->private_data (with a comment)

Changes since RFC:
- Keep file->private_data stable while directory descriptor remains open


diff --git a/fs/libfs.c b/fs/libfs.c
index e9440d55073c..851e29fdd7e7 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -428,7 +428,7 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
 			  inode->i_ino, fs_umode_to_dtype(inode->i_mode));
 }
 
-static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
+static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
 {
 	struct offset_ctx *so_ctx = inode->i_op->get_offset_ctx(inode);
 	XA_STATE(xas, &so_ctx->xa, ctx->pos);
@@ -437,7 +437,7 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
 	while (true) {
 		dentry = offset_find_next(&xas);
 		if (!dentry)
-			break;
+			return ERR_PTR(-ENOENT);
 
 		if (!offset_dir_emit(ctx, dentry)) {
 			dput(dentry);
@@ -447,6 +447,7 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
 		dput(dentry);
 		ctx->pos = xas.xa_index + 1;
 	}
+	return NULL;
 }
 
 /**
@@ -479,7 +480,12 @@ static int offset_readdir(struct file *file, struct dir_context *ctx)
 	if (!dir_emit_dots(file, ctx))
 		return 0;
 
-	offset_iterate_dir(d_inode(dir), ctx);
+	/* In this case, ->private_data is protected by f_pos_lock */
+	if (ctx->pos == 2)
+		file->private_data = NULL;
+	else if (file->private_data == ERR_PTR(-ENOENT))
+		return 0;
+	file->private_data = offset_iterate_dir(d_inode(dir), ctx);
 	return 0;
 }
 



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-11-19 21:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-18 19:33 [PATCH v3] libfs: getdents() should return 0 after reaching EOD Chuck Lever
2023-11-18 22:11 ` Chuck Lever
2023-11-18 23:36   ` Al Viro
2023-11-19 19:18     ` Chuck Lever III
2023-11-19 20:22       ` Chuck Lever III
2023-11-19 21:14         ` Al Viro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).