Linux CIFS filesystem development
 help / color / mirror / Atom feed
From: Ronnie Sahlberg <lsahlber@redhat.com>
To: linux-cifs <linux-cifs@vger.kernel.org>
Cc: Steve French <smfrench@gmail.com>, Ronnie Sahlberg <lsahlber@redhat.com>
Subject: [PATCH 6/6] cifs: Add a laundromat thread that will timeout any directory leases we have
Date: Wed, 31 Aug 2022 12:49:47 +1000	[thread overview]
Message-ID: <20220831024947.3917507-7-lsahlber@redhat.com> (raw)
In-Reply-To: <20220831024947.3917507-1-lsahlber@redhat.com>

after a timeout of 5 seconds. Later we will add instrumentation to tweak this value.

Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
---
 fs/cifs/cached_dir.c | 87 ++++++++++++++++++++++++++++++++++++++------
 fs/cifs/cached_dir.h |  1 +
 2 files changed, 76 insertions(+), 12 deletions(-)

diff --git a/fs/cifs/cached_dir.c b/fs/cifs/cached_dir.c
index 20aba23d8d88..16ab0cb9c53c 100644
--- a/fs/cifs/cached_dir.c
+++ b/fs/cifs/cached_dir.c
@@ -61,17 +61,12 @@ static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids,
 }
 
 static struct dentry *
-path_to_dentry(struct cifs_sb_info *cifs_sb, const char *full_path)
+path_to_dentry(struct cifs_sb_info *cifs_sb, const char *path)
 {
 	struct dentry *dentry;
-	char *path = NULL;
-	char *s, *p;
+	const char *s, *p;
 	char sep;
 
-	path = kstrdup(full_path, GFP_ATOMIC);
-	if (path == NULL)
-		return ERR_PTR(-ENOMEM);
-
 	sep = CIFS_DIR_SEP(cifs_sb);
 	dentry = dget(cifs_sb->root);
 	s = path;
@@ -100,7 +95,7 @@ path_to_dentry(struct cifs_sb_info *cifs_sb, const char *full_path)
 		dput(dentry);
 		dentry = child;
 	} while (!IS_ERR(dentry));
-	kfree(path);
+
 	return dentry;
 }
 
@@ -255,15 +250,14 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
 				(char *)&cfid->file_all_info))
 		cfid->file_all_info_is_valid = true;
 
-	if (!strlen(path)) {
+	if (!path[0])
 		dentry = dget(cifs_sb->root);
-		cfid->dentry = dentry;
-	} else{
+	else {
 		dentry = path_to_dentry(cifs_sb, path);
 		if (IS_ERR(dentry))
 			goto oshr_free;
-		cfid->dentry = dentry;
 	}
+	cfid->dentry = dentry;
 	cfid->tcon = tcon;
 	cfid->time = jiffies;
 	cfid->is_open = true;
@@ -505,6 +499,55 @@ static void free_cached_dir(struct cached_fid *cfid)
 	kfree(cfid);
 }
 
+
+
+static int
+cifs_cfids_laundromat_thread(void *p)
+{
+	struct cached_fids *cfids = p;
+	struct cached_fid *cfid, *q;
+	struct list_head entry;
+
+	while (!kthread_should_stop()) {
+		ssleep(1);
+		INIT_LIST_HEAD(&entry);
+		if (kthread_should_stop())
+			return 0;
+		spin_lock(&cfids->cfid_list_lock);
+		list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
+			if (jiffies > cfid->time + HZ * 5) {
+				list_del(&cfid->entry);
+				list_add(&cfid->entry, &entry);
+				cfids->num_entries--;
+			}
+		}
+		spin_unlock(&cfids->cfid_list_lock);
+
+		list_for_each_entry_safe(cfid, q, &entry, entry) {
+			cfid->on_list = false;
+			list_del(&cfid->entry);
+			/*
+			 * Cancel, and wait for the work to finish in
+			 * case we are racing with it.
+			 */
+			cancel_work_sync(&cfid->lease_break);
+			if (cfid->has_lease) {
+				/*
+				 * We lease has not yet been cancelled from
+				 * the server so we need to drop the reference.
+				 */
+				spin_lock(&cfids->cfid_list_lock);
+				cfid->has_lease = false;
+				spin_unlock(&cfids->cfid_list_lock);
+				kref_put(&cfid->refcount, smb2_close_cached_fid);
+			}
+		}
+	}
+
+	return 0;
+}
+
+
 struct cached_fids *init_cached_dirs(void)
 {
 	struct cached_fids *cfids;
@@ -514,6 +557,20 @@ struct cached_fids *init_cached_dirs(void)
 		return NULL;
 	spin_lock_init(&cfids->cfid_list_lock);
 	INIT_LIST_HEAD(&cfids->entries);
+
+	/*
+	 * since we're in a cifs function already, we know that
+	 * this will succeed. No need for try_module_get().
+	 */
+	__module_get(THIS_MODULE);
+	cfids->laundromat = kthread_run(cifs_cfids_laundromat_thread,
+				  cfids, "cifsd-cfid-laundromat");
+	if (IS_ERR(cfids->laundromat)) {
+		cifs_dbg(VFS, "Failed to start cfids laundromat thread.\n");
+		kfree(cfids);
+		module_put(THIS_MODULE);
+		return NULL;
+	}
 	return cfids;
 }
 
@@ -526,6 +583,12 @@ void free_cached_dirs(struct cached_fids *cfids)
 	struct cached_fid *cfid, *q;
 	struct list_head entry;
 
+	if (cfids->laundromat) {
+		kthread_stop(cfids->laundromat);
+		cfids->laundromat = NULL;
+		module_put(THIS_MODULE);
+	}
+	
        	INIT_LIST_HEAD(&entry);
 	spin_lock(&cfids->cfid_list_lock);
 	list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
diff --git a/fs/cifs/cached_dir.h b/fs/cifs/cached_dir.h
index e536304ca2ce..4ab9b10cd098 100644
--- a/fs/cifs/cached_dir.h
+++ b/fs/cifs/cached_dir.h
@@ -57,6 +57,7 @@ struct cached_fids {
 	spinlock_t cfid_list_lock;
 	int num_entries;
 	struct list_head entries;
+	struct task_struct *laundromat;
 };
 
 extern struct cached_fids *init_cached_dirs(void);
-- 
2.35.3


      parent reply	other threads:[~2022-08-31  2:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-31  2:49 cifs: expand directory caching to handle any directory Ronnie Sahlberg
2022-08-31  2:49 ` [PATCH 1/6] cifs: Make tcon contain a wrapper structure cached_fids instead of cached_fid Ronnie Sahlberg
2022-08-31  2:49 ` [PATCH 2/6] cifs: cifs: handlecache, only track the dentry for the root handle Ronnie Sahlberg
2022-08-31  2:49 ` [PATCH 3/6] cifs: store a pointer to a fid in the cfid structure instead of the struct Ronnie Sahlberg
2022-08-31  2:49 ` [PATCH 4/6] cifs: enable caching of directories for which a lease is held Ronnie Sahlberg
2022-08-31  2:49 ` [PATCH 5/6] cifs: find and use the dentry for cached non-root directories also Ronnie Sahlberg
2022-08-31  2:49 ` Ronnie Sahlberg [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=20220831024947.3917507-7-lsahlber@redhat.com \
    --to=lsahlber@redhat.com \
    --cc=linux-cifs@vger.kernel.org \
    --cc=smfrench@gmail.com \
    /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