diff mbox series

cifs: decouple code to handle an existing session

Message ID 20220823142545.9101-1-ematsumiya@suse.de
State New
Headers show
Series cifs: decouple code to handle an existing session | expand

Commit Message

Enzo Matsumiya Aug. 23, 2022, 2:25 p.m. UTC
Create a new get_existing_smb_ses() function to handle existing SMB
sessions. Also simplify the logic from that snippet a little bit.

Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
 fs/cifs/connect.c | 79 ++++++++++++++++++++++++++---------------------
 1 file changed, 44 insertions(+), 35 deletions(-)
diff mbox series

Patch

diff --git a/fs/cifs/connect.c b/fs/cifs/connect.c
index 49162973ca30..2701b3351556 100644
--- a/fs/cifs/connect.c
+++ b/fs/cifs/connect.c
@@ -2151,6 +2151,49 @@  cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
 }
 #endif /* CONFIG_KEYS */
 
+static struct cifs_ses *get_existing_smb_ses(unsigned int xid,
+					     struct TCP_Server_Info *server,
+					     struct cifs_ses *ses,
+					     struct nls_table *cp)
+{
+	bool need_reconnect = false;
+	struct cifs_ses *sesp = NULL;
+	int rc = 0;
+
+	spin_lock(&ses->chan_lock);
+	need_reconnect = cifs_chan_needs_reconnect(ses, server);
+	spin_unlock(&ses->chan_lock);
+
+	if (need_reconnect) {
+		cifs_dbg(FYI, "Session needs reconnect\n");
+
+		mutex_lock(&ses->session_mutex);
+		rc = cifs_negotiate_protocol(xid, ses, server);
+		if (rc)
+			goto out_err;
+
+		rc = cifs_setup_session(xid, ses, server, cp);
+		if (rc)
+			goto out_err;
+		mutex_unlock(&ses->session_mutex);
+		sesp = ses;
+		rc = 0; /* ensure rc = 0 by now */
+	}
+
+	/* existing SMB ses has a server reference already */
+	cifs_put_tcp_session(server, 0);
+out_err:
+	if (rc) {
+		mutex_unlock(&ses->session_mutex);
+		cifs_put_smb_ses(ses);
+		sesp = ERR_PTR(rc);
+	}
+
+	free_xid(xid);
+
+	return sesp;
+}
+
 /**
  * cifs_get_smb_ses - get a session matching @ctx data from @server
  * @server: server to setup the session to
@@ -2175,41 +2218,7 @@  cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
 	if (ses) {
 		cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
 			 ses->ses_status);
-
-		spin_lock(&ses->chan_lock);
-		if (cifs_chan_needs_reconnect(ses, server)) {
-			spin_unlock(&ses->chan_lock);
-			cifs_dbg(FYI, "Session needs reconnect\n");
-
-			mutex_lock(&ses->session_mutex);
-			rc = cifs_negotiate_protocol(xid, ses, server);
-			if (rc) {
-				mutex_unlock(&ses->session_mutex);
-				/* problem -- put our ses reference */
-				cifs_put_smb_ses(ses);
-				free_xid(xid);
-				return ERR_PTR(rc);
-			}
-
-			rc = cifs_setup_session(xid, ses, server,
-						ctx->local_nls);
-			if (rc) {
-				mutex_unlock(&ses->session_mutex);
-				/* problem -- put our reference */
-				cifs_put_smb_ses(ses);
-				free_xid(xid);
-				return ERR_PTR(rc);
-			}
-			mutex_unlock(&ses->session_mutex);
-
-			spin_lock(&ses->chan_lock);
-		}
-		spin_unlock(&ses->chan_lock);
-
-		/* existing SMB ses has a server reference already */
-		cifs_put_tcp_session(server, 0);
-		free_xid(xid);
-		return ses;
+		return get_existing_smb_ses(xid, server, ses, ctx->local_nls);
 	}
 
 	cifs_dbg(FYI, "Existing smb sess not found\n");