summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEnzo Matsumiya <ematsumiya@suse.de>2025-09-24 17:48:02 -0300
committerEnzo Matsumiya <ematsumiya@suse.de>2025-09-26 19:08:08 -0300
commit61fb8c8817bcd2def0b656db85e09985f34925fb (patch)
treedd6e4f04eabc974ea7b05c0517687136e600a6d1
parent5fe39c98a4df391377f5f286f659f8246b9e17e8 (diff)
downloadlinux-61fb8c8817bcd2def0b656db85e09985f34925fb.tar.gz
linux-61fb8c8817bcd2def0b656db85e09985f34925fb.tar.bz2
linux-61fb8c8817bcd2def0b656db85e09985f34925fb.zip
smb: client: wait for concurrent caching of dirents in cifs_readdir()
The file struct passed down to cifs_readdir() is a stack variable, which means it makes no sense to keep/track it across a cached dir lifetime. Instead, use it to track concurrent accesses to the same cached path, and wait for the previous one to finish filling/emitting. Without this patch, virtually every 'ls' will issue a Find request, even when we have both directory and dirents cached and valid. With this patch, the chances of cache hits increases a lot, so on highly concurrent scenarios, the amount of network calls are drastically reduced. Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
-rw-r--r--fs/smb/client/cached_dir.c3
-rw-r--r--fs/smb/client/readdir.c66
2 files changed, 40 insertions, 29 deletions
diff --git a/fs/smb/client/cached_dir.c b/fs/smb/client/cached_dir.c
index 9dd74268b2d8..ad455c067cba 100644
--- a/fs/smb/client/cached_dir.c
+++ b/fs/smb/client/cached_dir.c
@@ -595,6 +595,9 @@ static struct cached_fid *init_cached_dir(const char *path)
/* this is caller/lease ref */
kref_get(&cfid->refcount);
+ /* initial cached dirents position */
+ cfid->dirents.pos = 2;
+
return cfid;
}
diff --git a/fs/smb/client/readdir.c b/fs/smb/client/readdir.c
index 0b2efd680fe6..903919345df1 100644
--- a/fs/smb/client/readdir.c
+++ b/fs/smb/client/readdir.c
@@ -860,22 +860,16 @@ static bool emit_cached_dirents(struct cached_dirents *cde,
return true;
}
-static void update_cached_dirents_count(struct cached_dirents *cde,
- struct file *file)
+static void update_cached_dirents_count(struct cached_dirents *cde)
{
- if (cde->file != file)
- return;
if (cde->is_valid || cde->is_failed)
return;
cde->pos++;
}
-static void finished_cached_dirents_count(struct cached_dirents *cde,
- struct dir_context *ctx, struct file *file)
+static void finished_cached_dirents_count(struct cached_dirents *cde, struct dir_context *ctx)
{
- if (cde->file != file)
- return;
if (cde->is_valid || cde->is_failed)
return;
if (ctx->pos != cde->pos)
@@ -884,16 +878,11 @@ static void finished_cached_dirents_count(struct cached_dirents *cde,
cde->is_valid = 1;
}
-static void add_cached_dirent(struct cached_dirents *cde,
- struct dir_context *ctx,
- const char *name, int namelen,
- struct cifs_fattr *fattr,
- struct file *file)
+static void add_cached_dirent(struct cached_dirents *cde, struct dir_context *ctx,
+ const char *name, int namelen, struct cifs_fattr *fattr)
{
struct cached_dirent *de;
- if (cde->file != file)
- return;
if (cde->is_valid || cde->is_failed)
return;
if (ctx->pos != cde->pos) {
@@ -934,8 +923,7 @@ static bool cifs_dir_emit(struct dir_context *ctx,
if (cfid) {
mutex_lock(&cfid->dirents.de_mutex);
- add_cached_dirent(&cfid->dirents, ctx, name, namelen,
- fattr, file);
+ add_cached_dirent(&cfid->dirents, ctx, name, namelen, fattr);
mutex_unlock(&cfid->dirents.de_mutex);
}
@@ -1076,20 +1064,14 @@ int cifs_readdir(struct file *file, struct dir_context *ctx)
rc = open_cached_dir(xid, tcon, full_path, cifs_sb, &cfid);
cifs_put_tlink(tlink);
- if (rc)
+retry_cached:
+ if (!cfid || !cfid_is_valid(cfid)) {
+ rc = -ENOENT;
goto cache_not_found;
+ }
mutex_lock(&cfid->dirents.de_mutex);
/*
- * If this was reading from the start of the directory
- * we need to initialize scanning and storing the
- * directory content.
- */
- if (ctx->pos == 0 && cfid->dirents.file == NULL) {
- cfid->dirents.file = file;
- cfid->dirents.pos = 2;
- }
- /*
* If we already have the entire directory cached then
* we can just serve the cache.
*/
@@ -1102,10 +1084,32 @@ int cifs_readdir(struct file *file, struct dir_context *ctx)
mutex_unlock(&cfid->dirents.de_mutex);
goto rddir2_exit;
}
+
+ if (cfid->dirents.is_failed) {
+ rc = -ENOENT;
+ mutex_unlock(&cfid->dirents.de_mutex);
+ goto cache_not_found;
+ }
+
+ if (!cfid->dirents.file)
+ cfid->dirents.file = file;
+ else if (cfid->dirents.file != file)
+ rc = -EAGAIN;
mutex_unlock(&cfid->dirents.de_mutex);
+ /* someone else is filling up the dirents for this cfid, wait for them to finish */
+ if (rc == -EAGAIN) {
+ rc = 0;
+ goto retry_cached;
+ }
+
/* keep our cfid ref, but check if still valid after network calls */
cache_not_found:
+ if (rc && cfid) {
+ close_cached_dir(cfid);
+ cfid = NULL;
+ }
+
/*
* Ensure FindFirst doesn't fail before doing filldir() for '.' and
* '..'. Otherwise we won't be able to notify VFS in case of failure.
@@ -1155,7 +1159,7 @@ cache_not_found:
} else {
if (cfid) {
mutex_lock(&cfid->dirents.de_mutex);
- finished_cached_dirents_count(&cfid->dirents, ctx, file);
+ finished_cached_dirents_count(&cfid->dirents, ctx);
mutex_unlock(&cfid->dirents.de_mutex);
}
cifs_dbg(FYI, "Could not find entry\n");
@@ -1196,7 +1200,7 @@ cache_not_found:
ctx->pos++;
if (cfid) {
mutex_lock(&cfid->dirents.de_mutex);
- update_cached_dirents_count(&cfid->dirents, file);
+ update_cached_dirents_count(&cfid->dirents);
mutex_unlock(&cfid->dirents.de_mutex);
}
@@ -1215,8 +1219,12 @@ cache_not_found:
rddir2_exit:
if (cfid) {
+ if (rc || cfid->dirents.is_failed || !cifsFile || cifsFile->srch_inf.endOfSearch)
+ cfid->dirents.file = NULL;
+
if (cifsFile)
cifsFile->invalidHandle = true;
+
close_cached_dir(cfid);
}
free_dentry_path(page);