Skip to contentSkip to conversation

dfs fixes

PATCH In review Paulo Alcantara 2 participants 10 messages Jun 4, 2021 – Jun 5, 2021

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

cover dfs fixes

Hi Steve,

Follow a series with dfs fixes and improvements.

- correctly handle different charsets when passing around DFS paths by converting them all to a default enconding in cache (utf8).

- keep SMB sessions alive as long as dfs mounts are actives in order to refresh cached entries by using IPC tcons.

- set a mininum of 2 minutes for refreshing cached entries

- fix broken hash of case sensitive DFS paths

- skip unnecessary tree disconnect of IPCs when shutting down SMB sessions (it didn't even work before).

- do not share tcp servers when mounting dfs shares because they may failover to completely different targets (use nosharesock).

Paulo Alcantara (7):
cifs: do not send tree disconnect to ipc shares
cifs: get rid of @noreq param in __dfs_cache_find()
cifs: keep referral server sessions alive
cifs: handle different charsets in dfs cache
cifs: fix path comparison and hash calc
cifs: set a minimum of 2 minutes for refreshing dfs cache
cifs: do not share tcp servers with dfs mounts

 fs/cifs/cifs_fs_sb.h |    7 +-
 fs/cifs/cifsglob.h   |    3 +-
 fs/cifs/connect.c    |  132 +++---
 fs/cifs/dfs_cache.c  | 1004 +++++++++++++++++-------------------------
 fs/cifs/dfs_cache.h  |   45 +-
 5 files changed, 498 insertions(+), 693 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/7 cifs: do not send tree disconnect to ipc shares

On session close, the IPC is closed and the server must release all tcons of the session. It doesn't matter if we send a ipc close or not.

Besides, it will make the server to not close durable and resilient files on session close, as specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.

---
 fs/cifs/connect.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
fs/cifs/connect.c +1 -11
index 495c395f9def..5ac1bd17463d 100644
@@ -1562,24 +1562,14 @@ cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
 static int
 cifs_free_ipc(struct cifs_ses *ses)
 {
-	int rc = 0, xid;
 	struct cifs_tcon *tcon = ses->tcon_ipc;
 
 	if (tcon == NULL)
 		return 0;
 
-	if (ses->server->ops->tree_disconnect) {
-		xid = get_xid();
-		rc = ses->server->ops->tree_disconnect(xid, tcon);
-		free_xid(xid);
-	}
-
-	if (rc)
-		cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
-
 	tconInfoFree(tcon);
 	ses->tcon_ipc = NULL;
-	return rc;
+	return 0;
 }
 
 static struct cifs_ses *
Signature · 1 line
2.31.1
#2 lore raw

PATCH 2/7 cifs: get rid of @noreq param in __dfs_cache_find()

In reply to Paulo Alcantara

@noreq param isn't used anywhere, so just remove it.

---
 fs/cifs/dfs_cache.c | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)
fs/cifs/dfs_cache.c +3 -14
index b1fa30fefe1f..e5ea87869f36 100644
@@ -701,8 +701,7 @@ static int update_cache_entry(const char *path,
  * handle them properly.
  */
 static int __dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
-			    const struct nls_table *nls_codepage, int remap,
-			    const char *path, bool noreq)
+			    const struct nls_table *nls_codepage, int remap, const char *path)
 {
 	int rc;
 	unsigned int hash;
@@ -716,16 +715,6 @@ static int __dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
 	down_read(&htable_rw_lock);
 
 	ce = lookup_cache_entry(path, &hash);
-
-	/*
-	 * If @noreq is set, no requests will be sent to the server. Just return
-	 * the cache entry.
-	 */
-	if (noreq) {
-		up_read(&htable_rw_lock);
-		return PTR_ERR_OR_ZERO(ce);
-	}
-
 	if (!IS_ERR(ce)) {
 		if (!cache_entry_expired(ce)) {
 			dump_ce(ce);
@@ -890,7 +879,7 @@ int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
 	if (rc)
 		return rc;
 
-	rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
+	rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath);
 	if (rc)
 		goto out_free_path;
 
@@ -1002,7 +991,7 @@ int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
 
 	cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
 
-	rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
+	rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath);
 	if (rc)
 		goto out_free_path;
 
Signature · 1 line
2.31.1
#3 lore raw

PATCH 3/7 cifs: keep referral server sessions alive

In reply to Paulo Alcantara

At every mount, keep all sessions alive that were used for chasing the DFS referrals as long as the dfs mounts are active.

Use those sessions in DFS cache to refresh all active tcons as well as cached entries. They will be managed by a list of mount_group structures that will be indexed by a randomly generated uuid at mount time, so we can put all the sessions related to specific dfs mounts and avoid leaking them.

---
 fs/cifs/cifs_fs_sb.h |   2 +
 fs/cifs/connect.c    |  54 ++--
 fs/cifs/dfs_cache.c  | 697 ++++++++++++++++---------------------------
 fs/cifs/dfs_cache.h  |   8 +-
 4 files changed, 285 insertions(+), 476 deletions(-)
fs/cifs/cifs_fs_sb.h +2 -0
index 9c45b3a82ad9..2dfd10c34ace 100644
@@ -77,6 +77,8 @@ struct cifs_sb_info {
 	 * failover properly.
 	 */
 	char *origin_fullpath; /* \\HOST\SHARE\[OPTIONAL PATH] */
+	/* randomly generated 128-bit number for indexing dfs mount groups in referral cache */
+	uuid_t dfs_mount_id;
 	/*
 	 * Indicate whether serverino option was turned off later
 	 * (cifs_autodisable_serverino) in order to match new mounts.
fs/cifs/connect.c +20 -34

144 lines not on this page · raw message

fs/cifs/dfs_cache.c +260 -437

887 lines not on this page · raw message

fs/cifs/dfs_cache.h +3 -5
index 1afc4f590c47..c23b08530a7f 100644
@@ -10,6 +10,7 @@
 
 #include <linux/nls.h>
 #include <linux/list.h>
+#include <linux/uuid.h>
 #include "cifsglob.h"
 
 struct dfs_cache_tgt_list {
@@ -44,13 +45,10 @@ dfs_cache_noreq_update_tgthint(const char *path,
 extern int dfs_cache_get_tgt_referral(const char *path,
 				      const struct dfs_cache_tgt_iterator *it,
 				      struct dfs_info3_param *ref);
-extern int dfs_cache_add_vol(char *mntdata, struct smb3_fs_context *ctx,
-			const char *fullpath);
-extern int dfs_cache_update_vol(const char *fullpath,
-				struct TCP_Server_Info *server);
-extern void dfs_cache_del_vol(const char *fullpath);
 extern int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it,
 				   char **share, char **prefix);
+void dfs_cache_put_refsrv_sessions(const uuid_t *mount_id);
+void dfs_cache_add_refsrv_session(const uuid_t *mount_id, struct cifs_ses *ses);
 
 static inline struct dfs_cache_tgt_iterator *
 dfs_cache_get_next_tgt(struct dfs_cache_tgt_list *tl,
Signature · 1 line
2.31.1
#4 lore raw

PATCH 4/7 cifs: handle different charsets in dfs cache

In reply to Paulo Alcantara

Convert all dfs paths to dfs cache's local codepage (@cache_cp) and avoid mixing them with different charsets.

---
 fs/cifs/cifs_fs_sb.h |   5 +-
 fs/cifs/cifsglob.h   |   3 +-
 fs/cifs/connect.c    |  67 +++++++++++-------
 fs/cifs/dfs_cache.c  | 165 ++++++++++++++++++++-----------------------
 fs/cifs/dfs_cache.h  |  37 +++++-----
 5 files changed, 138 insertions(+), 139 deletions(-)
fs/cifs/cifs_fs_sb.h +2 -3
index 2dfd10c34ace..64990ccd1b9c 100644
@@ -72,9 +72,8 @@ struct cifs_sb_info {
 	char *prepath;
 
 	/*
-	 * Path initially provided by the mount call. We might connect
-	 * to something different via DFS but we want to keep it to do
-	 * failover properly.
+	 * Canonical DFS path initially provided by the mount call. We might connect to something
+	 * different via DFS but we want to keep it to do failover properly.
 	 */
 	char *origin_fullpath; /* \\HOST\SHARE\[OPTIONAL PATH] */
 	/* randomly generated 128-bit number for indexing dfs mount groups in referral cache */
fs/cifs/cifsglob.h +1 -2
index 8488d7024462..d85ef3b41bd9 100644
@@ -1093,8 +1093,7 @@ struct cifs_tcon {
 	struct cached_fid crfid; /* Cached root fid */
 	/* BB add field for back pointer to sb struct(s)? */
 #ifdef CONFIG_CIFS_DFS_UPCALL
-	char *dfs_path;
-	int remap:2;
+	char *dfs_path; /* canonical DFS path */
 	struct list_head ulist; /* cache update list */
 #endif
 };
fs/cifs/connect.c +42 -25

140 lines not on this page · raw message

fs/cifs/dfs_cache.c +77 -88

371 lines not on this page · raw message

fs/cifs/dfs_cache.h +16 -21
index c23b08530a7f..b29d3ae64829 100644
@@ -24,31 +24,26 @@ struct dfs_cache_tgt_iterator {
 	struct list_head it_list;
 };
 
-extern int dfs_cache_init(void);
-extern void dfs_cache_destroy(void);
+int dfs_cache_init(void);
+void dfs_cache_destroy(void);
 extern const struct proc_ops dfscache_proc_ops;
 
-extern int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
-			  const struct nls_table *nls_codepage, int remap,
-			  const char *path, struct dfs_info3_param *ref,
-			  struct dfs_cache_tgt_list *tgt_list);
-extern int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
-				struct dfs_cache_tgt_list *tgt_list);
-extern int dfs_cache_update_tgthint(const unsigned int xid,
-				    struct cifs_ses *ses,
-				    const struct nls_table *nls_codepage,
-				    int remap, const char *path,
-				    const struct dfs_cache_tgt_iterator *it);
-extern int
-dfs_cache_noreq_update_tgthint(const char *path,
-			       const struct dfs_cache_tgt_iterator *it);
-extern int dfs_cache_get_tgt_referral(const char *path,
-				      const struct dfs_cache_tgt_iterator *it,
-				      struct dfs_info3_param *ref);
-extern int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it,
-				   char **share, char **prefix);
+int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nls_table *cp,
+		   int remap, const char *path, struct dfs_info3_param *ref,
+		   struct dfs_cache_tgt_list *tgt_list);
+int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
+			 struct dfs_cache_tgt_list *tgt_list);
+int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
+			     const struct nls_table *cp, int remap, const char *path,
+			     const struct dfs_cache_tgt_iterator *it);
+int dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt_iterator *it);
+int dfs_cache_get_tgt_referral(const char *path, const struct dfs_cache_tgt_iterator *it,
+			       struct dfs_info3_param *ref);
+int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it, char **share,
+			    char **prefix);
 void dfs_cache_put_refsrv_sessions(const uuid_t *mount_id);
 void dfs_cache_add_refsrv_session(const uuid_t *mount_id, struct cifs_ses *ses);
+char *dfs_cache_canonical_path(const char *path, const struct nls_table *cp, int remap);
 
 static inline struct dfs_cache_tgt_iterator *
 dfs_cache_get_next_tgt(struct dfs_cache_tgt_list *tl,
Signature · 1 line
2.31.1
#5 lore raw

PATCH 5/7 cifs: fix path comparison and hash calc

In reply to Paulo Alcantara

Fix cache lookup and hash calculations when handling paths with different cases.

---
 fs/cifs/dfs_cache.c | 168 ++++++++++++++++++++++++--------------------
 1 file changed, 93 insertions(+), 75 deletions(-)
fs/cifs/dfs_cache.c +93 -75
index c52fec3c4092..f7c4874ddc17 100644
@@ -424,12 +424,24 @@ int dfs_cache_init(void)
 	return rc;
 }
 
-static inline unsigned int cache_entry_hash(const void *data, int size)
+static int cache_entry_hash(const void *data, int size, unsigned int *hash)
 {
-	unsigned int h;
+	int i, clen;
+	const unsigned char *s = data;
+	wchar_t c;
+	unsigned int h = 0;
 
-	h = jhash(data, size, 0);
-	return h & (CACHE_HTABLE_SIZE - 1);
+	for (i = 0; i < size; i += clen) {
+		clen = cache_cp->char2uni(&s[i], size - i, &c);
+		if (unlikely(clen < 0)) {
+			cifs_dbg(VFS, "%s: can't convert char\n", __func__);
+			return clen;
+		}
+		c = cifs_toupper(c);
+		h = jhash(&c, sizeof(c), h);
+	}
+	*hash = h % CACHE_HTABLE_SIZE;
+	return 0;
 }
 
 /* Check whether second path component of @path is SYSVOL or NETLOGON */
@@ -522,9 +534,7 @@ static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
 }
 
 /* Allocate a new cache entry */
-static struct cache_entry *alloc_cache_entry(const char *path,
-					     const struct dfs_info3_param *refs,
-					     int numrefs)
+static struct cache_entry *alloc_cache_entry(struct dfs_info3_param *refs, int numrefs)
 {
 	struct cache_entry *ce;
 	int rc;
@@ -533,11 +543,9 @@ static struct cache_entry *alloc_cache_entry(const char *path,
 	if (!ce)
 		return ERR_PTR(-ENOMEM);
 
-	ce->path = kstrdup(path, GFP_KERNEL);
-	if (!ce->path) {
-		kmem_cache_free(cache_slab, ce);
-		return ERR_PTR(-ENOMEM);
-	}
+	ce->path = refs[0].path_name;
+	refs[0].path_name = NULL;
+
 	INIT_HLIST_NODE(&ce->hlist);
 	INIT_LIST_HEAD(&ce->tlist);
 
@@ -579,12 +587,18 @@ static void remove_oldest_entry_locked(void)
 }
 
 /* Add a new DFS cache entry */
-static int add_cache_entry_locked(const char *path, unsigned int hash,
-				  struct dfs_info3_param *refs, int numrefs)
+static int add_cache_entry_locked(struct dfs_info3_param *refs, int numrefs)
 {
+	int rc;
 	struct cache_entry *ce;
+	unsigned int hash;
 
-	ce = alloc_cache_entry(path, refs, numrefs);
+	convert_delimiter(refs[0].path_name, '\\');
+	rc = cache_entry_hash(refs[0].path_name, strlen(refs[0].path_name), &hash);
+	if (rc)
+		return rc;
+
+	ce = alloc_cache_entry(refs, numrefs);
 	if (IS_ERR(ce))
 		return PTR_ERR(ce);
 
@@ -604,57 +618,69 @@ static int add_cache_entry_locked(const char *path, unsigned int hash,
 	return 0;
 }
 
-static struct cache_entry *__lookup_cache_entry(const char *path)
+/* Check if two DFS paths are equal.  @s1 and @s2 are expected to be in @cache_cp's charset */
+static bool dfs_path_equal(const char *s1, int len1, const char *s2, int len2)
+{
+	int i, l1, l2;
+	wchar_t c1, c2;
+
+	if (len1 != len2)
+		return false;
+
+	for (i = 0; i < len1; i += l1) {
+		l1 = cache_cp->char2uni(&s1[i], len1 - i, &c1);
+		l2 = cache_cp->char2uni(&s2[i], len2 - i, &c2);
+		if (unlikely(l1 < 0 && l2 < 0)) {
+			if (s1[i] != s2[i])
+				return false;
+			l1 = 1;
+			continue;
+		}
+		if (l1 != l2)
+			return false;
+		if (cifs_toupper(c1) != cifs_toupper(c2))
+			return false;
+	}
+	return true;
+}
+
+static struct cache_entry *__lookup_cache_entry(const char *path, unsigned int hash, int len)
 {
 	struct cache_entry *ce;
-	unsigned int h;
-	bool found = false;
 
-	h = cache_entry_hash(path, strlen(path));
-
-	hlist_for_each_entry(ce, &cache_htable[h], hlist) {
-		if (!strcasecmp(path, ce->path)) {
-			found = true;
+	hlist_for_each_entry(ce, &cache_htable[hash], hlist) {
+		if (dfs_path_equal(ce->path, strlen(ce->path), path, len)) {
 			dump_ce(ce);
-			break;
+			return ce;
 		}
 	}
-
-	if (!found)
-		ce = ERR_PTR(-ENOENT);
-	return ce;
+	return ERR_PTR(-EEXIST);
 }
 
 /*
- * Find a DFS cache entry in hash table and optionally check prefix path against
- * @path.
- * Use whole path components in the match.
- * Must be called with htable_rw_lock held.
+ * Find a DFS cache entry in hash table and optionally check prefix path against normalized @path.
  *
- * Return ERR_PTR(-ENOENT) if the entry is not found.
+ * Use whole path components in the match.  Must be called with htable_rw_lock held.
+ *
+ * Return ERR_PTR(-EEXIST) if the entry is not found.
  */
-static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *hash)
+static struct cache_entry *lookup_cache_entry(const char *path)
 {
-	struct cache_entry *ce = ERR_PTR(-ENOENT);
-	unsigned int h;
+	struct cache_entry *ce;
 	int cnt = 0;
-	char *npath;
-	char *s, *e;
-	char sep;
+	const char *s = path, *e;
+	char sep = *s;
+	unsigned int hash;
+	int rc;
 
-	npath = kstrdup(path, GFP_KERNEL);
-	if (!npath)
-		return ERR_PTR(-ENOMEM);
-
-	s = npath;
-	sep = *npath;
 	while ((s = strchr(s, sep)) && ++cnt < 3)
 		s++;
 
 	if (cnt < 3) {
-		h = cache_entry_hash(path, strlen(path));
-		ce = __lookup_cache_entry(path);
-		goto out;
+		rc = cache_entry_hash(path, strlen(path), &hash);
+		if (rc)
+			return ERR_PTR(rc);
+		return __lookup_cache_entry(path, hash, strlen(path));
 	}
 	/*
 	 * Handle paths that have more than two path components and are a complete prefix of the DFS
@@ -662,36 +688,29 @@ static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *ha
 	 *
 	 * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
 	 */
-	h = cache_entry_hash(npath, strlen(npath));
-	e = npath + strlen(npath) - 1;
+	e = path + strlen(path) - 1;
 	while (e > s) {
-		char tmp;
+		int len;
 
 		/* skip separators */
 		while (e > s && *e == sep)
 			e--;
 		if (e == s)
-			goto out;
-
-		tmp = *(e+1);
-		*(e+1) = 0;
-
-		ce = __lookup_cache_entry(npath);
-		if (!IS_ERR(ce)) {
-			h = cache_entry_hash(npath, strlen(npath));
 			break;
-		}
 
-		*(e+1) = tmp;
+		len = e + 1 - path;
+		rc = cache_entry_hash(path, len, &hash);
+		if (rc)
+			return ERR_PTR(rc);
+		ce = __lookup_cache_entry(path, hash, len);
+		if (!IS_ERR(ce))
+			return ce;
+
 		/* backward until separator */
 		while (e > s && *e != sep)
 			e--;
 	}
-out:
-	if (hash)
-		*hash = h;
-	kfree(npath);
-	return ce;
+	return ERR_PTR(-EEXIST);
 }
 
 /**
@@ -717,7 +736,7 @@ static int update_cache_entry_locked(const char *path, const struct dfs_info3_pa
 	struct cache_entry *ce;
 	char *s, *th = NULL;
 
-	ce = lookup_cache_entry(path, NULL);
+	ce = lookup_cache_entry(path);
 	if (IS_ERR(ce))
 		return PTR_ERR(ce);
 
@@ -767,7 +786,6 @@ static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses, const
 static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, const char *path)
 {
 	int rc;
-	unsigned int hash;
 	struct cache_entry *ce;
 	struct dfs_info3_param *refs = NULL;
 	int numrefs = 0;
@@ -777,7 +795,7 @@ static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, cons
 
 	down_write(&htable_rw_lock);
 
-	ce = lookup_cache_entry(path, &hash);
+	ce = lookup_cache_entry(path);
 	if (!IS_ERR(ce)) {
 		if (!cache_entry_expired(ce)) {
 			dump_ce(ce);
@@ -808,7 +826,7 @@ static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, cons
 		remove_oldest_entry_locked();
 	}
 
-	rc = add_cache_entry_locked(path, hash, refs, numrefs);
+	rc = add_cache_entry_locked(refs, numrefs);
 	if (!rc)
 		atomic_inc(&cache_count);
 
@@ -940,7 +958,7 @@ int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nl
 
 	down_read(&htable_rw_lock);
 
-	ce = lookup_cache_entry(npath, NULL);
+	ce = lookup_cache_entry(npath);
 	if (IS_ERR(ce)) {
 		up_read(&htable_rw_lock);
 		rc = PTR_ERR(ce);
@@ -987,7 +1005,7 @@ int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
 
 	down_read(&htable_rw_lock);
 
-	ce = lookup_cache_entry(path, NULL);
+	ce = lookup_cache_entry(path);
 	if (IS_ERR(ce)) {
 		rc = PTR_ERR(ce);
 		goto out_unlock;
@@ -1044,7 +1062,7 @@ int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
 
 	down_write(&htable_rw_lock);
 
-	ce = lookup_cache_entry(npath, NULL);
+	ce = lookup_cache_entry(npath);
 	if (IS_ERR(ce)) {
 		rc = PTR_ERR(ce);
 		goto out_unlock;
@@ -1098,7 +1116,7 @@ int dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt_
 
 	down_write(&htable_rw_lock);
 
-	ce = lookup_cache_entry(path, NULL);
+	ce = lookup_cache_entry(path);
 	if (IS_ERR(ce)) {
 		rc = PTR_ERR(ce);
 		goto out_unlock;
@@ -1147,7 +1165,7 @@ int dfs_cache_get_tgt_referral(const char *path, const struct dfs_cache_tgt_iter
 
 	down_read(&htable_rw_lock);
 
-	ce = lookup_cache_entry(path, NULL);
+	ce = lookup_cache_entry(path);
 	if (IS_ERR(ce)) {
 		rc = PTR_ERR(ce);
 		goto out_unlock;
Signature · 1 line
2.31.1
#6 lore raw

Re: [PATCH 5/7] cifs: fix path comparison and hash calc

applied the other 6 patches to cifs-2.6.git for-next but this one wouldn't apply to for-next so I left it out temporarily.

Quoted 324 lines · Paulo Alcantara

Fix cache lookup and hash calculations when handling paths with different cases.

---
 fs/cifs/dfs_cache.c | 168 ++++++++++++++++++++++++--------------------
 1 file changed, 93 insertions(+), 75 deletions(-)
fs/cifs/dfs_cache.c +93 -76
index c52fec3c4092..f7c4874ddc17 100644
@@ -424,12 +424,24 @@ int dfs_cache_init(void)
        return rc;
 }
 
-static inline unsigned int cache_entry_hash(const void *data, int size)
+static int cache_entry_hash(const void *data, int size, unsigned int *hash)
 {
-       unsigned int h;
+       int i, clen;
+       const unsigned char *s = data;
+       wchar_t c;
+       unsigned int h = 0;
 
-       h = jhash(data, size, 0);
-       return h & (CACHE_HTABLE_SIZE - 1);
+       for (i = 0; i < size; i += clen) {
+               clen = cache_cp->char2uni(&s[i], size - i, &c);
+               if (unlikely(clen < 0)) {
+                       cifs_dbg(VFS, "%s: can't convert char\n", __func__);
+                       return clen;
+               }
+               c = cifs_toupper(c);
+               h = jhash(&c, sizeof(c), h);
+       }
+       *hash = h % CACHE_HTABLE_SIZE;
+       return 0;
 }
 
 /* Check whether second path component of @path is SYSVOL or NETLOGON */
@@ -522,9 +534,7 @@ static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
 }
 
 /* Allocate a new cache entry */
-static struct cache_entry *alloc_cache_entry(const char *path,
-                                            const struct dfs_info3_param *refs,
-                                            int numrefs)
+static struct cache_entry *alloc_cache_entry(struct dfs_info3_param *refs, int numrefs)
 {
        struct cache_entry *ce;
        int rc;
@@ -533,11 +543,9 @@ static struct cache_entry *alloc_cache_entry(const char *path,
        if (!ce)
                return ERR_PTR(-ENOMEM);
 
-       ce->path = kstrdup(path, GFP_KERNEL);
-       if (!ce->path) {
-               kmem_cache_free(cache_slab, ce);
-               return ERR_PTR(-ENOMEM);
-       }
+       ce->path = refs[0].path_name;
+       refs[0].path_name = NULL;
+
        INIT_HLIST_NODE(&ce->hlist);
        INIT_LIST_HEAD(&ce->tlist);
 
@@ -579,12 +587,18 @@ static void remove_oldest_entry_locked(void)
 }
 
 /* Add a new DFS cache entry */
-static int add_cache_entry_locked(const char *path, unsigned int hash,
-                                 struct dfs_info3_param *refs, int numrefs)
+static int add_cache_entry_locked(struct dfs_info3_param *refs, int numrefs)
 {
+       int rc;
        struct cache_entry *ce;
+       unsigned int hash;
 
-       ce = alloc_cache_entry(path, refs, numrefs);
+       convert_delimiter(refs[0].path_name, '\\');
+       rc = cache_entry_hash(refs[0].path_name, strlen(refs[0].path_name), &hash);
+       if (rc)
+               return rc;
+
+       ce = alloc_cache_entry(refs, numrefs);
        if (IS_ERR(ce))
                return PTR_ERR(ce);
 
@@ -604,57 +618,69 @@ static int add_cache_entry_locked(const char *path, unsigned int hash,
        return 0;
 }
 
-static struct cache_entry *__lookup_cache_entry(const char *path)
+/* Check if two DFS paths are equal.  @s1 and @s2 are expected to be in @cache_cp's charset */
+static bool dfs_path_equal(const char *s1, int len1, const char *s2, int len2)
+{
+       int i, l1, l2;
+       wchar_t c1, c2;
+
+       if (len1 != len2)
+               return false;
+
+       for (i = 0; i < len1; i += l1) {
+               l1 = cache_cp->char2uni(&s1[i], len1 - i, &c1);
+               l2 = cache_cp->char2uni(&s2[i], len2 - i, &c2);
+               if (unlikely(l1 < 0 && l2 < 0)) {
+                       if (s1[i] != s2[i])
+                               return false;
+                       l1 = 1;
+                       continue;
+               }
+               if (l1 != l2)
+                       return false;
+               if (cifs_toupper(c1) != cifs_toupper(c2))
+                       return false;
+       }
+       return true;
+}
+
+static struct cache_entry *__lookup_cache_entry(const char *path, unsigned int hash, int len)
 {
        struct cache_entry *ce;
-       unsigned int h;
-       bool found = false;
 
-       h = cache_entry_hash(path, strlen(path));
-
-       hlist_for_each_entry(ce, &cache_htable[h], hlist) {
-               if (!strcasecmp(path, ce->path)) {
-                       found = true;
+       hlist_for_each_entry(ce, &cache_htable[hash], hlist) {
+               if (dfs_path_equal(ce->path, strlen(ce->path), path, len)) {
                        dump_ce(ce);
-                       break;
+                       return ce;
                }
        }
-
-       if (!found)
-               ce = ERR_PTR(-ENOENT);
-       return ce;
+       return ERR_PTR(-EEXIST);
 }
 
 /*
- * Find a DFS cache entry in hash table and optionally check prefix path against
- * @path.
- * Use whole path components in the match.
- * Must be called with htable_rw_lock held.
+ * Find a DFS cache entry in hash table and optionally check prefix path against normalized @path.
  *
- * Return ERR_PTR(-ENOENT) if the entry is not found.
+ * Use whole path components in the match.  Must be called with htable_rw_lock held.
+ *
+ * Return ERR_PTR(-EEXIST) if the entry is not found.
  */
-static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *hash)
+static struct cache_entry *lookup_cache_entry(const char *path)
 {
-       struct cache_entry *ce = ERR_PTR(-ENOENT);
-       unsigned int h;
+       struct cache_entry *ce;
        int cnt = 0;
-       char *npath;
-       char *s, *e;
-       char sep;
+       const char *s = path, *e;
+       char sep = *s;
+       unsigned int hash;
+       int rc;
 
-       npath = kstrdup(path, GFP_KERNEL);
-       if (!npath)
-               return ERR_PTR(-ENOMEM);
-
-       s = npath;
-       sep = *npath;
        while ((s = strchr(s, sep)) && ++cnt < 3)
                s++;
 
        if (cnt < 3) {
-               h = cache_entry_hash(path, strlen(path));
-               ce = __lookup_cache_entry(path);
-               goto out;
+               rc = cache_entry_hash(path, strlen(path), &hash);
+               if (rc)
+                       return ERR_PTR(rc);
+               return __lookup_cache_entry(path, hash, strlen(path));
        }
        /*
         * Handle paths that have more than two path components and are a complete prefix of the DFS
@@ -662,36 +688,29 @@ static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *ha
         *
         * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
         */
-       h = cache_entry_hash(npath, strlen(npath));
-       e = npath + strlen(npath) - 1;
+       e = path + strlen(path) - 1;
        while (e > s) {
-               char tmp;
+               int len;
 
                /* skip separators */
                while (e > s && *e == sep)
                        e--;
                if (e == s)
-                       goto out;
-
-               tmp = *(e+1);
-               *(e+1) = 0;
-
-               ce = __lookup_cache_entry(npath);
-               if (!IS_ERR(ce)) {
-                       h = cache_entry_hash(npath, strlen(npath));
                        break;
-               }
 
-               *(e+1) = tmp;
+               len = e + 1 - path;
+               rc = cache_entry_hash(path, len, &hash);
+               if (rc)
+                       return ERR_PTR(rc);
+               ce = __lookup_cache_entry(path, hash, len);
+               if (!IS_ERR(ce))
+                       return ce;
+
                /* backward until separator */
                while (e > s && *e != sep)
                        e--;
        }
-out:
-       if (hash)
-               *hash = h;
-       kfree(npath);
-       return ce;
+       return ERR_PTR(-EEXIST);
 }
 
 /**
@@ -717,7 +736,7 @@ static int update_cache_entry_locked(const char *path, const struct dfs_info3_pa
        struct cache_entry *ce;
        char *s, *th = NULL;
 
-       ce = lookup_cache_entry(path, NULL);
+       ce = lookup_cache_entry(path);
        if (IS_ERR(ce))
                return PTR_ERR(ce);
 
@@ -767,7 +786,6 @@ static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses, const
 static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, const char *path)
 {
        int rc;
-       unsigned int hash;
        struct cache_entry *ce;
        struct dfs_info3_param *refs = NULL;
        int numrefs = 0;
@@ -777,7 +795,7 @@ static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, cons
 
        down_write(&htable_rw_lock);
 
-       ce = lookup_cache_entry(path, &hash);
+       ce = lookup_cache_entry(path);
        if (!IS_ERR(ce)) {
                if (!cache_entry_expired(ce)) {
                        dump_ce(ce);
@@ -808,7 +826,7 @@ static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, cons
                remove_oldest_entry_locked();
        }
 
-       rc = add_cache_entry_locked(path, hash, refs, numrefs);
+       rc = add_cache_entry_locked(refs, numrefs);
        if (!rc)
                atomic_inc(&cache_count);
 
@@ -940,7 +958,7 @@ int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nl
 
        down_read(&htable_rw_lock);
 
-       ce = lookup_cache_entry(npath, NULL);
+       ce = lookup_cache_entry(npath);
        if (IS_ERR(ce)) {
                up_read(&htable_rw_lock);
                rc = PTR_ERR(ce);
@@ -987,7 +1005,7 @@ int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
 
        down_read(&htable_rw_lock);
 
-       ce = lookup_cache_entry(path, NULL);
+       ce = lookup_cache_entry(path);
        if (IS_ERR(ce)) {
                rc = PTR_ERR(ce);
                goto out_unlock;
@@ -1044,7 +1062,7 @@ int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
 
        down_write(&htable_rw_lock);
 
-       ce = lookup_cache_entry(npath, NULL);
+       ce = lookup_cache_entry(npath);
        if (IS_ERR(ce)) {
                rc = PTR_ERR(ce);
                goto out_unlock;
@@ -1098,7 +1116,7 @@ int dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt_
 
        down_write(&htable_rw_lock);
 
-       ce = lookup_cache_entry(path, NULL);
+       ce = lookup_cache_entry(path);
        if (IS_ERR(ce)) {
                rc = PTR_ERR(ce);
                goto out_unlock;
@@ -1147,7 +1165,7 @@ int dfs_cache_get_tgt_referral(const char *path, const struct dfs_cache_tgt_iter
 
        down_read(&htable_rw_lock);
 
-       ce = lookup_cache_entry(path, NULL);
+       ce = lookup_cache_entry(path);
        if (IS_ERR(ce)) {
                rc = PTR_ERR(ce);
                goto out_unlock;
--

2.31.1

Signature · 3 lines
Thanks,

Steve
#7 lore raw

PATCH 6/7 cifs: set a minimum of 2 minutes for refreshing dfs cache

In reply to Paulo Alcantara

We don't want to refresh the dfs cache in very short intervals, so setting a minimum interval of 2 minutes is OK.

If it needs to be refreshed immediately, one could have the cache cleared with

	$ echo 0 > /proc/fs/cifs/dfscache

and then remounting the dfs share.

---
 fs/cifs/dfs_cache.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/cifs/dfs_cache.c +2 -1
index f7c4874ddc17..b4df779e4248 100644
@@ -24,6 +24,7 @@
 
 #define CACHE_HTABLE_SIZE 32
 #define CACHE_MAX_ENTRIES 64
+#define CACHE_MIN_TTL 120 /* 2 minutes */
 
 #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
 				    DFSREF_STORAGE_SERVER))
@@ -503,7 +504,7 @@ static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
 {
 	int i;
 
-	ce->ttl = refs[0].ttl;
+	ce->ttl = max_t(int, refs[0].ttl, CACHE_MIN_TTL);
 	ce->etime = get_expire_time(ce->ttl);
 	ce->srvtype = refs[0].server_type;
 	ce->hdr_flags = refs[0].flags;
Signature · 1 line
2.31.1
#8 lore raw

PATCH 7/7 cifs: do not share tcp servers with dfs mounts

In reply to Paulo Alcantara

It isn't enough to have unshared tcons because multiple DFS mounts can connect to same target server and failover to different servers, so we can't use a single tcp server for such cases.

For the simplest solution, use nosharesock option to achieve that.

---
 fs/cifs/connect.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/cifs/connect.c +3 -4
index cece0c2249c3..05f5c84a63a4 100644
@@ -1938,10 +1938,7 @@ cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
 	spin_lock(&cifs_tcp_ses_lock);
 	list_for_each(tmp, &ses->tcon_list) {
 		tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
-#ifdef CONFIG_CIFS_DFS_UPCALL
-		if (tcon->dfs_path)
-			continue;
-#endif
+
 		if (!match_tcon(tcon, ctx))
 			continue;
 		++tcon->tc_count;
@@ -3406,6 +3403,8 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
 			goto error;
 	}
 
+	ctx->nosharesock = true;
+
 	/* Get path of DFS root */
 	ref_path = build_unc_path_to_root(ctx, cifs_sb, false);
 	if (IS_ERR(ref_path)) {
Signature · 1 line
2.31.1
#9 lore raw
In reply to Paulo Alcantara

Tentatively merged into for-next (pending testing and more review) six of the 7 patches in the series - patch 5 wouldn't apply.

Quoted 40 lines · Paulo Alcantara

Hi Steve,

Follow a series with dfs fixes and improvements.

- correctly handle different charsets when passing around DFS paths by converting them all to a default enconding in cache (utf8).

- keep SMB sessions alive as long as dfs mounts are actives in order to refresh cached entries by using IPC tcons.

- set a mininum of 2 minutes for refreshing cached entries

- fix broken hash of case sensitive DFS paths

- skip unnecessary tree disconnect of IPCs when shutting down SMB sessions (it didn't even work before).

- do not share tcp servers when mounting dfs shares because they may failover to completely different targets (use nosharesock).

Paulo Alcantara (7):
cifs: do not send tree disconnect to ipc shares
cifs: get rid of @noreq param in __dfs_cache_find()
cifs: keep referral server sessions alive
cifs: handle different charsets in dfs cache
cifs: fix path comparison and hash calc
cifs: set a minimum of 2 minutes for refreshing dfs cache
cifs: do not share tcp servers with dfs mounts

 fs/cifs/cifs_fs_sb.h |    7 +-
 fs/cifs/cifsglob.h   |    3 +-
 fs/cifs/connect.c    |  132 +++---
 fs/cifs/dfs_cache.c  | 1004 +++++++++++++++++-------------------------
 fs/cifs/dfs_cache.h  |   45 +-
 5 files changed, 498 insertions(+), 693 deletions(-)

--
2.31.1

Signature · 3 lines
Thanks,

Steve