Skip to contentSkip to conversation

cifs: cache the directory content for shroot

PATCH In review Ronnie Sahlberg 2 participants 2 messages Oct 5, 2020 – Oct 7, 2020

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

PATCH 3/3 cifs: cache the directory content for shroot

Add caching of the directory content for the shroot. Populate the cache during the initial scan in readdir() and then try to serve out of the cache for subsequent scans.

---
 fs/cifs/cifsglob.h |  22 +++++++
 fs/cifs/misc.c     |   2 +
 fs/cifs/readdir.c  | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 fs/cifs/smb2ops.c  |  14 +++++
 4 files changed, 207 insertions(+), 8 deletions(-)
fs/cifs/cifsglob.h +22 -0
index b565d83ba89e..487e6de80e47 100644
@@ -1073,6 +1073,27 @@ cap_unix(struct cifs_ses *ses)
 	return ses->server->vals->cap_unix & ses->capabilities;
 }
 
+struct cached_dirent {
+	struct list_head entry;
+	char *name;
+	int namelen;
+	loff_t pos;
+	u64 ino;
+	unsigned type;
+};
+
+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;
@@ -1083,6 +1104,7 @@ struct cached_fid {
 	struct cifs_tcon *tcon;
 	struct work_struct lease_break;
 	struct smb2_file_all_info file_all_info;
+	struct cached_dirents dirents;
 };
 
 /*
fs/cifs/misc.c +2 -0
index 1c14cf01dbef..a106bd3cc20b 100644
@@ -124,6 +124,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->tidStatus = CifsNew;
fs/cifs/readdir.c +169 -8

249 lines not on this page · raw message

fs/cifs/smb2ops.c +14 -0
index fd6c136066df..280464e21a5f 100644
@@ -605,6 +605,8 @@ smb2_close_cached_fid(struct kref *ref)
 {
 	struct cached_fid *cfid = container_of(ref, struct cached_fid,
 					       refcount);
+	struct list_head *pos, *q;
+	struct cached_dirent *dirent;
 
 	if (cfid->is_valid) {
 		cifs_dbg(FYI, "clear cached root file handle\n");
@@ -613,6 +615,18 @@ smb2_close_cached_fid(struct kref *ref)
 		cfid->is_valid = false;
 		cfid->file_all_info_is_valid = false;
 		cfid->has_lease = false;
+		mutex_lock(&cfid->dirents.de_mutex);
+		list_for_each_safe(pos, q, &cfid->dirents.entries) {
+			dirent = list_entry(pos, struct cached_dirent, entry);
+			list_del(pos);
+			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);
 	}
 }
 
Signature · 1 line
2.13.6
#1 lore raw

Hi Ronnie,

I'm assuming this is the latest V4:

Can you document which functions require a lock to be held when calling?

Would it work properly if in any of the patched functions we have this scenario:

TASK A DEMULTIPLEX THREAD
------ ------------------
open_shroot()
... oplock break on shroot
...close/reopen shroot, fid and ptr gets updated...
...
do stuff with dirents (with old fid/ptr?)
...
close_shroot()

More comments below:

Quoted 11 lines · Ronnie Sahlberg
fs/cifs/readdir.c +11 -0
+
+static bool cifs_dir_emit(struct dir_context *ctx,
+			  const char *name, int namelen,
+			  u64 ino, unsigned type,
+			  struct cached_fid *cfid)
+{
+	bool rc;
+
+	rc = dir_emit(ctx, name, namelen, ino, type);
+	if (!rc)
+		return rc;

fs/cifs/readdir.c find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos, · line 912

Quoted 10 lines
fs/cifs/readdir.c +10 -0
+
+	if (cfid) {
+		mutex_lock(&cfid->dirents.de_mutex);
+		add_cached_dirent(&cfid->dirents, ctx, name, namelen, ino,
+				  type);
+		mutex_unlock(&cfid->dirents.de_mutex);
+	}
+
+	return rc;
+}

Should return cfid->dirents.is_failed?

Quoted 17 lines
fs/cifs/readdir.c +3 -2
-
 int cifs_readdir(struct file *file, struct dir_context *ctx)
 {
 	int rc = 0;
 	unsigned int xid;
 	int i;
-	struct cifs_tcon *tcon;
+	struct cifs_tcon *tcon, *mtcon;
 	struct cifsFileInfo *cifsFile = NULL;
 	char *current_entry;
 	int num_to_fill = 0;
@@ -920,15 +1021,59 @@ int cifs_readdir(struct file *file, struct dir_context *ctx)
 	char *end_of_smb;
 	unsigned int max_len;
 	char *full_path = NULL;
+	struct cached_fid *cfid = NULL;
+	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);

fs/cifs/readdir.c int cifs_readdir(struct file *file, struct dir_context *ctx) · line 1034

Quoted 10 lines
fs/cifs/readdir.c +1 -1
 
 	xid = get_xid();
-
 	full_path = build_path_from_dentry(file_dentry(file));
 	if (full_path == NULL) {
 		rc = -ENOMEM;
 		goto rddir2_exit;
 	}
 
+	mtcon = cifs_sb_master_tcon(cifs_sb);

Why using the master tcon? The rest of the code is using the user tcon.

Quoted 26 lines
fs/cifs/readdir.c +26 -0
+	if (!is_smb1_server(mtcon->ses->server) && !strcmp(full_path, "")) {
+		rc = open_shroot(xid, mtcon, cifs_sb, &cfid);
+		if (rc)
+			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.ctx == NULL) {
+			cfid->dirents.ctx = ctx;
+			cfid->dirents.pos = 2;
+		}
+		/*
+		 * If we already have the entire directory cached then
+		 * we can just serve the cache.
+		 */
+		if (cfid->dirents.is_valid) {
+			if (!dir_emit_dots(file, ctx)){
+				mutex_unlock(&cfid->dirents.de_mutex);
+				goto rddir2_exit;
+			}
+			emit_cached_dirents(&cfid->dirents, ctx);
+			mutex_unlock(&cfid->dirents.de_mutex);

fs/cifs/readdir.c int cifs_readdir(struct file *file, struct dir_context *ctx) · line 1070

Quoted 10 lines
fs/cifs/readdir.c +10 -0
+			goto rddir2_exit;
+		}
+		mutex_unlock(&cfid->dirents.de_mutex);
+	}
+ cache_not_found:
+
+	/* Drop the cache while calling initiate_cifs_search and
+	 * find_cifs_entry in case there will be reconnects during
+	 * query_directory.
+	 */

comment style

Cheers,

Signature · 4 lines
Aurélien Aptel / SUSE Labs Samba Team
GPG: 1839 CB5F 9F5B FB9B AA97  8C99 03C8 A49B 521B D5D3
SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg, DE
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah HRB 247165 (AG München)