Skip to contentSkip to conversation

cache dirent names for the cached directory while we hold a lease

PATCH In review Ronnie Sahlberg 4 participants 8 messages May 4, 2022 – May 10, 2022

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 cifs: cache dirent names for cached directories

Cache the names of entries for cached-directories while we have a lease held. This allows us to avoid expensive calls to the server when re-scanning a cached directory.

---
 fs/cifs/cifsglob.h |  84 ++++++++++++++--------
 fs/cifs/misc.c     |   2 +
 fs/cifs/readdir.c  | 173 ++++++++++++++++++++++++++++++++++++++++++---
 fs/cifs/smb2ops.c  |  18 ++++-
 4 files changed, 236 insertions(+), 41 deletions(-)
fs/cifs/cifsglob.h +53 -31
index 8de977c359b1..4ec6a3df17fa 100644
@@ -1009,6 +1009,58 @@ cap_unix(struct cifs_ses *ses)
 	return ses->server->vals->cap_unix & ses->capabilities;
 }
 
+/*
+ * common struct for holding inode info when searching for or updating an
+ * inode with new info
+ */
+
+#define CIFS_FATTR_DFS_REFERRAL		0x1
+#define CIFS_FATTR_DELETE_PENDING	0x2
+#define CIFS_FATTR_NEED_REVAL		0x4
+#define CIFS_FATTR_INO_COLLISION	0x8
+#define CIFS_FATTR_UNKNOWN_NLINK	0x10
+#define CIFS_FATTR_FAKE_ROOT_INO	0x20
+
+struct cifs_fattr {
+	u32		cf_flags;
+	u32		cf_cifsattrs;
+	u64		cf_uniqueid;
+	u64		cf_eof;
+	u64		cf_bytes;
+	u64		cf_createtime;
+	kuid_t		cf_uid;
+	kgid_t		cf_gid;
+	umode_t		cf_mode;
+	dev_t		cf_rdev;
+	unsigned int	cf_nlink;
+	unsigned int	cf_dtype;
+	struct timespec64 cf_atime;
+	struct timespec64 cf_mtime;
+	struct timespec64 cf_ctime;
+	u32             cf_cifstag;
+};
+
+struct cached_dirent {
+	struct list_head entry;
+	char *name;
+	int namelen;
+	loff_t pos;
+
+	struct cifs_fattr fattr;
+};
+
+struct cached_dirents {
+	bool is_valid:1;
+	bool is_failed:1;
+	struct dir_context *ctx; /*
+				  * Only used to make sure we only take entries
+				  * from a single context. Never dereferenced.
+				  */
+	struct mutex de_mutex;
+	int pos;		 /* Expected ctx->pos */
+	struct list_head entries;
+};
+
 struct cached_fid {
 	bool is_valid:1;	/* Do we have a useable root fid */
 	bool file_all_info_is_valid:1;
@@ -1021,6 +1073,7 @@ struct cached_fid {
 	struct dentry *dentry;
 	struct work_struct lease_break;
 	struct smb2_file_all_info file_all_info;
+	struct cached_dirents dirents;
 };
 
 /*
@@ -1641,37 +1694,6 @@ struct file_list {
 	struct cifsFileInfo *cfile;
 };
 
-/*
- * common struct for holding inode info when searching for or updating an
- * inode with new info
- */
-
-#define CIFS_FATTR_DFS_REFERRAL		0x1
-#define CIFS_FATTR_DELETE_PENDING	0x2
-#define CIFS_FATTR_NEED_REVAL		0x4
-#define CIFS_FATTR_INO_COLLISION	0x8
-#define CIFS_FATTR_UNKNOWN_NLINK	0x10
-#define CIFS_FATTR_FAKE_ROOT_INO	0x20
-
-struct cifs_fattr {
-	u32		cf_flags;
-	u32		cf_cifsattrs;
-	u64		cf_uniqueid;
-	u64		cf_eof;
-	u64		cf_bytes;
-	u64		cf_createtime;
-	kuid_t		cf_uid;
-	kgid_t		cf_gid;
-	umode_t		cf_mode;
-	dev_t		cf_rdev;
-	unsigned int	cf_nlink;
-	unsigned int	cf_dtype;
-	struct timespec64 cf_atime;
-	struct timespec64 cf_mtime;
-	struct timespec64 cf_ctime;
-	u32             cf_cifstag;
-};
-
 static inline void free_dfs_info_param(struct dfs_info3_param *param)
 {
 	if (param) {
fs/cifs/misc.c +2 -0
index afaf59c22193..7fef08add3bc 100644
@@ -114,6 +114,8 @@ tconInfoAlloc(void)
 		kfree(ret_buf);
 		return NULL;
 	}
+	INIT_LIST_HEAD(&ret_buf->crfid.dirents.entries);
+	mutex_init(&ret_buf->crfid.dirents.de_mutex);
 
 	atomic_inc(&tconInfoAllocCount);
 	ret_buf->status = TID_NEW;
fs/cifs/readdir.c +164 -9

248 lines not on this page · raw message

fs/cifs/smb2ops.c +17 -1
index d6aaeff4a30a..d92c5d43cbd5 100644
@@ -699,6 +699,7 @@ smb2_close_cached_fid(struct kref *ref)
 {
 	struct cached_fid *cfid = container_of(ref, struct cached_fid,
 					       refcount);
+	struct cached_dirent *dirent, *q;
 
 	if (cfid->is_valid) {
 		cifs_dbg(FYI, "clear cached root file handle\n");
@@ -718,6 +719,20 @@ smb2_close_cached_fid(struct kref *ref)
 		dput(cfid->dentry);
 		cfid->dentry = NULL;
 	}
+	/*
+	 * Delete all cached dirent names
+	 */
+	mutex_lock(&cfid->dirents.de_mutex);
+	list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) {
+		list_del(&dirent->entry);
+		kfree(dirent->name);
+		kfree(dirent);
+	}
+	cfid->dirents.is_valid = 0;
+	cfid->dirents.is_failed = 0;
+	cfid->dirents.ctx = NULL;
+	cfid->dirents.pos = 0;
+	mutex_unlock(&cfid->dirents.de_mutex);
 }
 
 void close_cached_dir(struct cached_fid *cfid)
@@ -776,7 +791,8 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
 	struct cifs_fid *pfid;
 	struct dentry *dentry;
 
-	if (tcon->nohandlecache)
+	if (tcon == NULL || tcon->nohandlecache ||
+	    is_smb1_server(tcon->ses->server))
 		return -ENOTSUPP;
 
 	if (cifs_sb->root == NULL)
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH] cifs: cache dirent names for cached directories

Quoted 11 lines · Ronnie Sahlberg

Cache the names of entries for cached-directories while we have a lease held. This allows us to avoid expensive calls to the server when re-scanning a cached directory.

---
 fs/cifs/cifsglob.h |  84 ++++++++++++++--------
 fs/cifs/misc.c     |   2 +
 fs/cifs/readdir.c  | 173 ++++++++++++++++++++++++++++++++++++++++++---
 fs/cifs/smb2ops.c  |  18 ++++-
 4 files changed, 236 insertions(+), 41 deletions(-)
Reviewed-by on v1 1/1
#3 lore raw

Re: [PATCH] cifs: cache dirent names for cached directories

In reply to Ronnie Sahlberg

fs/cifs/cifsglob.h cap_unix(struct cifs_ses *ses) · line 1054

Quoted 3 lines · Ronnie Sahlberg
fs/cifs/cifsglob.h +3 -0
+struct cached_dirents {
+	bool is_valid:1;
+	bool is_failed:1;

Does it make sense to have both? Do you expect a situation where is_valid && is_failed happens? From the patch, I don't see such case and the code could be adjusted to use !is_valid where appropriate. But let me know if I missed something.

This is just a cosmetic nitpick, but other than that,

Cheers,

Enzo

#4 lore raw

Re: [PATCH] cifs: cache dirent names for cached directories

Quoted 10 lines · Enzo Matsumiya

fs/cifs/cifsglob.h cap_unix(struct cifs_ses *ses) · line 1054

Quoted 3 lines · Ronnie Sahlberg
fs/cifs/cifsglob.h +3 -0
+struct cached_dirents {
+      bool is_valid:1;
+      bool is_failed:1;

Does it make sense to have both? Do you expect a situation where is_valid && is_failed happens? From the patch, I don't see such case and the code could be adjusted to use !is_valid where appropriate. But let me know if I missed something.

The reason to have both is to handle cases where we fail populating the cache partway through readdir.

The idea is that is_valid is set once we have fully populated the cache successfully
and is_failed is set on failure during the population of the cache and once it is set there is no point in even trying to add new entries to the cache.

Quoted 9 lines

This is just a cosmetic nitpick, but other than that,

Cheers,

Enzo

#5 lore raw

Re: [PATCH] cifs: cache dirent names for cached directories

Quoted 13 lines · ronnie sahlberg
Quoted 10 lines · Enzo Matsumiya

fs/cifs/cifsglob.h cap_unix(struct cifs_ses *ses) · line 1054

Quoted 3 lines · Ronnie Sahlberg
fs/cifs/cifsglob.h +3 -0
+struct cached_dirents {
+      bool is_valid:1;
+      bool is_failed:1;

Does it make sense to have both? Do you expect a situation where is_valid && is_failed happens? From the patch, I don't see such case and the code could be adjusted to use !is_valid where appropriate. But let me know if I missed something.

Quoted 8 lines

The reason to have both is to handle cases where we fail populating the cache partway through readdir.

The idea is that is_valid is set once we have fully populated the cache successfully
and is_failed is set on failure during the population of the cache and once it is set there is no point in even trying to add new entries to the cache.

What I mean is that we have three states and thus need two booleans:
1, successfully scanned the whole directory and cached all entries 2, in the process of scanning the directory and no failure yet 3, in the process of scanning the direcotry but we have already encountered a failure

Quoted 10 lines
Quoted 10 lines

This is just a cosmetic nitpick, but other than that,

Cheers,

Enzo

#6 lore raw

Re: [PATCH] cifs: cache dirent names for cached directories

In reply to Ronnie Sahlberg

Just another nitpick

On 05/04, Ronnie Sahlberg wrote:
<snip>

fs/cifs/smb2ops.c int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, · line 798

Quoted 10 lines
fs/cifs/smb2ops.c +2 -1
@@ -776,7 +791,8 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
 	struct cifs_fid *pfid;
 	struct dentry *dentry;
 
-	if (tcon->nohandlecache)
+	if (tcon == NULL || tcon->nohandlecache ||
+	    is_smb1_server(tcon->ses->server))
 		return -ENOTSUPP;
 
 	if (cifs_sb->root == NULL)

This last hunk looks unrelated to the original topic. Could it be sent as a separate patch please? This helps tracking when doing backports/bisects.

Cheers,

Enzo

#7 lore raw

Re: [PATCH] cifs: cache dirent names for cached directories

Quoted 14 lines · Enzo Matsumiya

Just another nitpick

On 05/04, Ronnie Sahlberg wrote:
<snip>

Quoted 9 lines
fs/cifs/smb2ops.c +2 -1
@@ -776,7 +791,8 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
      struct cifs_fid *pfid;
      struct dentry *dentry;
 
-      if (tcon->nohandlecache)
+      if (tcon == NULL || tcon->nohandlecache ||
+          is_smb1_server(tcon->ses->server))
              return -ENOTSUPP;
 
Quoted 5 lines

fs/cifs/smb2ops.c int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, · line 798

Quoted 1 line
fs/cifs/smb2ops.c +0 -0
      if (cifs_sb->root == NULL)

This last hunk looks unrelated to the original topic. Could it be sent as a separate patch please? This helps tracking when doing backports/bisects.

Thanks. I will do that.
Steve, I will rework the patch and split it up a little. I will also do some small minor changes so that it will be very easy to expand readdir() to not just do this for hardcoded root of share but so that we can do this for any dir.
So please leave it out from the for-next for now, or at least do not include it in any push requests until I have re-worked it.

In testing, in my latest iteration, doing the second 'ls /mnt' results in zero network traffic as long as the lease is kept.
Even IF we cache the directory like this, we still do a few commands to the server for entries that are softlinks/reparse-points but that might be possible to address too.

Quoted 6 lines

Cheers,

Enzo