xref: /openbmc/linux/fs/smb/client/connect.c (revision 91f4480c41f56f7c723323cf7f581f1d95d9ffbc)
138c8a9a5SSteve French // SPDX-License-Identifier: LGPL-2.1
238c8a9a5SSteve French /*
338c8a9a5SSteve French  *
438c8a9a5SSteve French  *   Copyright (C) International Business Machines  Corp., 2002,2011
538c8a9a5SSteve French  *   Author(s): Steve French (sfrench@us.ibm.com)
638c8a9a5SSteve French  *
738c8a9a5SSteve French  */
838c8a9a5SSteve French #include <linux/fs.h>
938c8a9a5SSteve French #include <linux/net.h>
1038c8a9a5SSteve French #include <linux/string.h>
1138c8a9a5SSteve French #include <linux/sched/mm.h>
1238c8a9a5SSteve French #include <linux/sched/signal.h>
1338c8a9a5SSteve French #include <linux/list.h>
1438c8a9a5SSteve French #include <linux/wait.h>
1538c8a9a5SSteve French #include <linux/slab.h>
1638c8a9a5SSteve French #include <linux/pagemap.h>
1738c8a9a5SSteve French #include <linux/ctype.h>
1838c8a9a5SSteve French #include <linux/utsname.h>
1938c8a9a5SSteve French #include <linux/mempool.h>
2038c8a9a5SSteve French #include <linux/delay.h>
2138c8a9a5SSteve French #include <linux/completion.h>
2238c8a9a5SSteve French #include <linux/kthread.h>
2338c8a9a5SSteve French #include <linux/pagevec.h>
2438c8a9a5SSteve French #include <linux/freezer.h>
2538c8a9a5SSteve French #include <linux/namei.h>
2638c8a9a5SSteve French #include <linux/uuid.h>
2738c8a9a5SSteve French #include <linux/uaccess.h>
2838c8a9a5SSteve French #include <asm/processor.h>
2938c8a9a5SSteve French #include <linux/inet.h>
3038c8a9a5SSteve French #include <linux/module.h>
3138c8a9a5SSteve French #include <keys/user-type.h>
3238c8a9a5SSteve French #include <net/ipv6.h>
3338c8a9a5SSteve French #include <linux/parser.h>
3438c8a9a5SSteve French #include <linux/bvec.h>
3538c8a9a5SSteve French #include "cifspdu.h"
3638c8a9a5SSteve French #include "cifsglob.h"
3738c8a9a5SSteve French #include "cifsproto.h"
3838c8a9a5SSteve French #include "cifs_unicode.h"
3938c8a9a5SSteve French #include "cifs_debug.h"
4038c8a9a5SSteve French #include "cifs_fs_sb.h"
4138c8a9a5SSteve French #include "ntlmssp.h"
4238c8a9a5SSteve French #include "nterr.h"
4338c8a9a5SSteve French #include "rfc1002pdu.h"
4438c8a9a5SSteve French #include "fscache.h"
4538c8a9a5SSteve French #include "smb2proto.h"
4638c8a9a5SSteve French #include "smbdirect.h"
4738c8a9a5SSteve French #include "dns_resolve.h"
4838c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
4938c8a9a5SSteve French #include "dfs.h"
5038c8a9a5SSteve French #include "dfs_cache.h"
5138c8a9a5SSteve French #endif
5238c8a9a5SSteve French #include "fs_context.h"
5338c8a9a5SSteve French #include "cifs_swn.h"
5438c8a9a5SSteve French 
5538c8a9a5SSteve French extern mempool_t *cifs_req_poolp;
5638c8a9a5SSteve French extern bool disable_legacy_dialects;
5738c8a9a5SSteve French 
5838c8a9a5SSteve French /* FIXME: should these be tunable? */
5938c8a9a5SSteve French #define TLINK_ERROR_EXPIRE	(1 * HZ)
6038c8a9a5SSteve French #define TLINK_IDLE_EXPIRE	(600 * HZ)
6138c8a9a5SSteve French 
6238c8a9a5SSteve French /* Drop the connection to not overload the server */
6338c8a9a5SSteve French #define NUM_STATUS_IO_TIMEOUT   5
6438c8a9a5SSteve French 
6538c8a9a5SSteve French static int ip_connect(struct TCP_Server_Info *server);
6638c8a9a5SSteve French static int generic_ip_connect(struct TCP_Server_Info *server);
6738c8a9a5SSteve French static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
6838c8a9a5SSteve French static void cifs_prune_tlinks(struct work_struct *work);
6938c8a9a5SSteve French 
7038c8a9a5SSteve French /*
7138c8a9a5SSteve French  * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
7238c8a9a5SSteve French  * get their ip addresses changed at some point.
7338c8a9a5SSteve French  *
7438c8a9a5SSteve French  * This should be called with server->srv_mutex held.
7538c8a9a5SSteve French  */
7638c8a9a5SSteve French static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
7738c8a9a5SSteve French {
7838c8a9a5SSteve French 	int rc;
7938c8a9a5SSteve French 	int len;
8038c8a9a5SSteve French 	char *unc;
8138c8a9a5SSteve French 	struct sockaddr_storage ss;
8238c8a9a5SSteve French 
8338c8a9a5SSteve French 	if (!server->hostname)
8438c8a9a5SSteve French 		return -EINVAL;
8538c8a9a5SSteve French 
8638c8a9a5SSteve French 	/* if server hostname isn't populated, there's nothing to do here */
8738c8a9a5SSteve French 	if (server->hostname[0] == '\0')
8838c8a9a5SSteve French 		return 0;
8938c8a9a5SSteve French 
9038c8a9a5SSteve French 	len = strlen(server->hostname) + 3;
9138c8a9a5SSteve French 
9238c8a9a5SSteve French 	unc = kmalloc(len, GFP_KERNEL);
9338c8a9a5SSteve French 	if (!unc) {
9438c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
9538c8a9a5SSteve French 		return -ENOMEM;
9638c8a9a5SSteve French 	}
9738c8a9a5SSteve French 	scnprintf(unc, len, "\\\\%s", server->hostname);
9838c8a9a5SSteve French 
9938c8a9a5SSteve French 	spin_lock(&server->srv_lock);
10038c8a9a5SSteve French 	ss = server->dstaddr;
10138c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
10238c8a9a5SSteve French 
10338c8a9a5SSteve French 	rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
10438c8a9a5SSteve French 	kfree(unc);
10538c8a9a5SSteve French 
10638c8a9a5SSteve French 	if (rc < 0) {
10738c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
10838c8a9a5SSteve French 			 __func__, server->hostname, rc);
10938c8a9a5SSteve French 	} else {
11038c8a9a5SSteve French 		spin_lock(&server->srv_lock);
11138c8a9a5SSteve French 		memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr));
11238c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
11338c8a9a5SSteve French 		rc = 0;
11438c8a9a5SSteve French 	}
11538c8a9a5SSteve French 
11638c8a9a5SSteve French 	return rc;
11738c8a9a5SSteve French }
11838c8a9a5SSteve French 
11938c8a9a5SSteve French static void smb2_query_server_interfaces(struct work_struct *work)
12038c8a9a5SSteve French {
12138c8a9a5SSteve French 	int rc;
12238c8a9a5SSteve French 	struct cifs_tcon *tcon = container_of(work,
12338c8a9a5SSteve French 					struct cifs_tcon,
12438c8a9a5SSteve French 					query_interfaces.work);
12538c8a9a5SSteve French 
12638c8a9a5SSteve French 	/*
12738c8a9a5SSteve French 	 * query server network interfaces, in case they change
12838c8a9a5SSteve French 	 */
12938c8a9a5SSteve French 	rc = SMB3_request_interfaces(0, tcon, false);
13038c8a9a5SSteve French 	if (rc) {
13138c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
13238c8a9a5SSteve French 				__func__, rc);
13338c8a9a5SSteve French 	}
13438c8a9a5SSteve French 
13538c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
13638c8a9a5SSteve French 			   (SMB_INTERFACE_POLL_INTERVAL * HZ));
13738c8a9a5SSteve French }
13838c8a9a5SSteve French 
13938c8a9a5SSteve French /*
14038c8a9a5SSteve French  * Update the tcpStatus for the server.
14138c8a9a5SSteve French  * This is used to signal the cifsd thread to call cifs_reconnect
14238c8a9a5SSteve French  * ONLY cifsd thread should call cifs_reconnect. For any other
14338c8a9a5SSteve French  * thread, use this function
14438c8a9a5SSteve French  *
14538c8a9a5SSteve French  * @server: the tcp ses for which reconnect is needed
14638c8a9a5SSteve French  * @all_channels: if this needs to be done for all channels
14738c8a9a5SSteve French  */
14838c8a9a5SSteve French void
14938c8a9a5SSteve French cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
15038c8a9a5SSteve French 				bool all_channels)
15138c8a9a5SSteve French {
15238c8a9a5SSteve French 	struct TCP_Server_Info *pserver;
15338c8a9a5SSteve French 	struct cifs_ses *ses;
15438c8a9a5SSteve French 	int i;
15538c8a9a5SSteve French 
15638c8a9a5SSteve French 	/* If server is a channel, select the primary channel */
15738c8a9a5SSteve French 	pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
15838c8a9a5SSteve French 
15938c8a9a5SSteve French 	spin_lock(&pserver->srv_lock);
16038c8a9a5SSteve French 	if (!all_channels) {
16138c8a9a5SSteve French 		pserver->tcpStatus = CifsNeedReconnect;
16238c8a9a5SSteve French 		spin_unlock(&pserver->srv_lock);
16338c8a9a5SSteve French 		return;
16438c8a9a5SSteve French 	}
16538c8a9a5SSteve French 	spin_unlock(&pserver->srv_lock);
16638c8a9a5SSteve French 
16738c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
16838c8a9a5SSteve French 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
16938c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
17038c8a9a5SSteve French 		for (i = 0; i < ses->chan_count; i++) {
17138c8a9a5SSteve French 			spin_lock(&ses->chans[i].server->srv_lock);
17238c8a9a5SSteve French 			ses->chans[i].server->tcpStatus = CifsNeedReconnect;
17338c8a9a5SSteve French 			spin_unlock(&ses->chans[i].server->srv_lock);
17438c8a9a5SSteve French 		}
17538c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
17638c8a9a5SSteve French 	}
17738c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
17838c8a9a5SSteve French }
17938c8a9a5SSteve French 
18038c8a9a5SSteve French /*
18138c8a9a5SSteve French  * Mark all sessions and tcons for reconnect.
18238c8a9a5SSteve French  * IMPORTANT: make sure that this gets called only from
18338c8a9a5SSteve French  * cifsd thread. For any other thread, use
18438c8a9a5SSteve French  * cifs_signal_cifsd_for_reconnect
18538c8a9a5SSteve French  *
18638c8a9a5SSteve French  * @server: the tcp ses for which reconnect is needed
18738c8a9a5SSteve French  * @server needs to be previously set to CifsNeedReconnect.
18838c8a9a5SSteve French  * @mark_smb_session: whether even sessions need to be marked
18938c8a9a5SSteve French  */
19038c8a9a5SSteve French void
19138c8a9a5SSteve French cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
19238c8a9a5SSteve French 				      bool mark_smb_session)
19338c8a9a5SSteve French {
19438c8a9a5SSteve French 	struct TCP_Server_Info *pserver;
19538c8a9a5SSteve French 	struct cifs_ses *ses, *nses;
19638c8a9a5SSteve French 	struct cifs_tcon *tcon;
19738c8a9a5SSteve French 
19838c8a9a5SSteve French 	/*
19938c8a9a5SSteve French 	 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
20038c8a9a5SSteve French 	 * are not used until reconnected.
20138c8a9a5SSteve French 	 */
20238c8a9a5SSteve French 	cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
20338c8a9a5SSteve French 
20438c8a9a5SSteve French 	/* If server is a channel, select the primary channel */
20538c8a9a5SSteve French 	pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
20638c8a9a5SSteve French 
20738c8a9a5SSteve French 
20838c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
20938c8a9a5SSteve French 	list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) {
21038c8a9a5SSteve French 		/* check if iface is still active */
21138c8a9a5SSteve French 		if (!cifs_chan_is_iface_active(ses, server))
21238c8a9a5SSteve French 			cifs_chan_update_iface(ses, server);
21338c8a9a5SSteve French 
21438c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
21538c8a9a5SSteve French 		if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) {
21638c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
21738c8a9a5SSteve French 			continue;
21838c8a9a5SSteve French 		}
21938c8a9a5SSteve French 
22038c8a9a5SSteve French 		if (mark_smb_session)
22138c8a9a5SSteve French 			CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
22238c8a9a5SSteve French 		else
22338c8a9a5SSteve French 			cifs_chan_set_need_reconnect(ses, server);
22438c8a9a5SSteve French 
22538c8a9a5SSteve French 		cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
22638c8a9a5SSteve French 			 __func__, ses->chans_need_reconnect);
22738c8a9a5SSteve French 
22838c8a9a5SSteve French 		/* If all channels need reconnect, then tcon needs reconnect */
22938c8a9a5SSteve French 		if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
23038c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
23138c8a9a5SSteve French 			continue;
23238c8a9a5SSteve French 		}
23338c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
23438c8a9a5SSteve French 
23538c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
23638c8a9a5SSteve French 		ses->ses_status = SES_NEED_RECON;
23738c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
23838c8a9a5SSteve French 
23938c8a9a5SSteve French 		list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
24038c8a9a5SSteve French 			tcon->need_reconnect = true;
24138c8a9a5SSteve French 			spin_lock(&tcon->tc_lock);
24238c8a9a5SSteve French 			tcon->status = TID_NEED_RECON;
24338c8a9a5SSteve French 			spin_unlock(&tcon->tc_lock);
24438c8a9a5SSteve French 		}
24538c8a9a5SSteve French 		if (ses->tcon_ipc) {
24638c8a9a5SSteve French 			ses->tcon_ipc->need_reconnect = true;
24738c8a9a5SSteve French 			spin_lock(&ses->tcon_ipc->tc_lock);
24838c8a9a5SSteve French 			ses->tcon_ipc->status = TID_NEED_RECON;
24938c8a9a5SSteve French 			spin_unlock(&ses->tcon_ipc->tc_lock);
25038c8a9a5SSteve French 		}
25138c8a9a5SSteve French 	}
25238c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
25338c8a9a5SSteve French }
25438c8a9a5SSteve French 
25538c8a9a5SSteve French static void
25638c8a9a5SSteve French cifs_abort_connection(struct TCP_Server_Info *server)
25738c8a9a5SSteve French {
25838c8a9a5SSteve French 	struct mid_q_entry *mid, *nmid;
25938c8a9a5SSteve French 	struct list_head retry_list;
26038c8a9a5SSteve French 
26138c8a9a5SSteve French 	server->maxBuf = 0;
26238c8a9a5SSteve French 	server->max_read = 0;
26338c8a9a5SSteve French 
26438c8a9a5SSteve French 	/* do not want to be sending data on a socket we are freeing */
26538c8a9a5SSteve French 	cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
26638c8a9a5SSteve French 	cifs_server_lock(server);
26738c8a9a5SSteve French 	if (server->ssocket) {
26838c8a9a5SSteve French 		cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
26938c8a9a5SSteve French 			 server->ssocket->flags);
27038c8a9a5SSteve French 		kernel_sock_shutdown(server->ssocket, SHUT_WR);
27138c8a9a5SSteve French 		cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
27238c8a9a5SSteve French 			 server->ssocket->flags);
27338c8a9a5SSteve French 		sock_release(server->ssocket);
27438c8a9a5SSteve French 		server->ssocket = NULL;
27538c8a9a5SSteve French 	}
27638c8a9a5SSteve French 	server->sequence_number = 0;
27738c8a9a5SSteve French 	server->session_estab = false;
27838c8a9a5SSteve French 	kfree_sensitive(server->session_key.response);
27938c8a9a5SSteve French 	server->session_key.response = NULL;
28038c8a9a5SSteve French 	server->session_key.len = 0;
28138c8a9a5SSteve French 	server->lstrp = jiffies;
28238c8a9a5SSteve French 
28338c8a9a5SSteve French 	/* mark submitted MIDs for retry and issue callback */
28438c8a9a5SSteve French 	INIT_LIST_HEAD(&retry_list);
28538c8a9a5SSteve French 	cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
28638c8a9a5SSteve French 	spin_lock(&server->mid_lock);
28738c8a9a5SSteve French 	list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
28838c8a9a5SSteve French 		kref_get(&mid->refcount);
28938c8a9a5SSteve French 		if (mid->mid_state == MID_REQUEST_SUBMITTED)
29038c8a9a5SSteve French 			mid->mid_state = MID_RETRY_NEEDED;
29138c8a9a5SSteve French 		list_move(&mid->qhead, &retry_list);
29238c8a9a5SSteve French 		mid->mid_flags |= MID_DELETED;
29338c8a9a5SSteve French 	}
29438c8a9a5SSteve French 	spin_unlock(&server->mid_lock);
29538c8a9a5SSteve French 	cifs_server_unlock(server);
29638c8a9a5SSteve French 
29738c8a9a5SSteve French 	cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
29838c8a9a5SSteve French 	list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
29938c8a9a5SSteve French 		list_del_init(&mid->qhead);
30038c8a9a5SSteve French 		mid->callback(mid);
30138c8a9a5SSteve French 		release_mid(mid);
30238c8a9a5SSteve French 	}
30338c8a9a5SSteve French 
30438c8a9a5SSteve French 	if (cifs_rdma_enabled(server)) {
30538c8a9a5SSteve French 		cifs_server_lock(server);
30638c8a9a5SSteve French 		smbd_destroy(server);
30738c8a9a5SSteve French 		cifs_server_unlock(server);
30838c8a9a5SSteve French 	}
30938c8a9a5SSteve French }
31038c8a9a5SSteve French 
31138c8a9a5SSteve French static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
31238c8a9a5SSteve French {
31338c8a9a5SSteve French 	spin_lock(&server->srv_lock);
31438c8a9a5SSteve French 	server->nr_targets = num_targets;
31538c8a9a5SSteve French 	if (server->tcpStatus == CifsExiting) {
31638c8a9a5SSteve French 		/* the demux thread will exit normally next time through the loop */
31738c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
31838c8a9a5SSteve French 		wake_up(&server->response_q);
31938c8a9a5SSteve French 		return false;
32038c8a9a5SSteve French 	}
32138c8a9a5SSteve French 
32238c8a9a5SSteve French 	cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
32338c8a9a5SSteve French 	trace_smb3_reconnect(server->CurrentMid, server->conn_id,
32438c8a9a5SSteve French 			     server->hostname);
32538c8a9a5SSteve French 	server->tcpStatus = CifsNeedReconnect;
32638c8a9a5SSteve French 
32738c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
32838c8a9a5SSteve French 	return true;
32938c8a9a5SSteve French }
33038c8a9a5SSteve French 
33138c8a9a5SSteve French /*
33238c8a9a5SSteve French  * cifs tcp session reconnection
33338c8a9a5SSteve French  *
33438c8a9a5SSteve French  * mark tcp session as reconnecting so temporarily locked
33538c8a9a5SSteve French  * mark all smb sessions as reconnecting for tcp session
33638c8a9a5SSteve French  * reconnect tcp session
33738c8a9a5SSteve French  * wake up waiters on reconnection? - (not needed currently)
33838c8a9a5SSteve French  *
33938c8a9a5SSteve French  * if mark_smb_session is passed as true, unconditionally mark
34038c8a9a5SSteve French  * the smb session (and tcon) for reconnect as well. This value
34138c8a9a5SSteve French  * doesn't really matter for non-multichannel scenario.
34238c8a9a5SSteve French  *
34338c8a9a5SSteve French  */
34438c8a9a5SSteve French static int __cifs_reconnect(struct TCP_Server_Info *server,
34538c8a9a5SSteve French 			    bool mark_smb_session)
34638c8a9a5SSteve French {
34738c8a9a5SSteve French 	int rc = 0;
34838c8a9a5SSteve French 
34938c8a9a5SSteve French 	if (!cifs_tcp_ses_needs_reconnect(server, 1))
35038c8a9a5SSteve French 		return 0;
35138c8a9a5SSteve French 
35238c8a9a5SSteve French 	cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
35338c8a9a5SSteve French 
35438c8a9a5SSteve French 	cifs_abort_connection(server);
35538c8a9a5SSteve French 
35638c8a9a5SSteve French 	do {
35738c8a9a5SSteve French 		try_to_freeze();
35838c8a9a5SSteve French 		cifs_server_lock(server);
35938c8a9a5SSteve French 
36038c8a9a5SSteve French 		if (!cifs_swn_set_server_dstaddr(server)) {
36138c8a9a5SSteve French 			/* resolve the hostname again to make sure that IP address is up-to-date */
36238c8a9a5SSteve French 			rc = reconn_set_ipaddr_from_hostname(server);
36338c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
36438c8a9a5SSteve French 		}
36538c8a9a5SSteve French 
36638c8a9a5SSteve French 		if (cifs_rdma_enabled(server))
36738c8a9a5SSteve French 			rc = smbd_reconnect(server);
36838c8a9a5SSteve French 		else
36938c8a9a5SSteve French 			rc = generic_ip_connect(server);
37038c8a9a5SSteve French 		if (rc) {
37138c8a9a5SSteve French 			cifs_server_unlock(server);
37238c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
37338c8a9a5SSteve French 			msleep(3000);
37438c8a9a5SSteve French 		} else {
37538c8a9a5SSteve French 			atomic_inc(&tcpSesReconnectCount);
37638c8a9a5SSteve French 			set_credits(server, 1);
37738c8a9a5SSteve French 			spin_lock(&server->srv_lock);
37838c8a9a5SSteve French 			if (server->tcpStatus != CifsExiting)
37938c8a9a5SSteve French 				server->tcpStatus = CifsNeedNegotiate;
38038c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
38138c8a9a5SSteve French 			cifs_swn_reset_server_dstaddr(server);
38238c8a9a5SSteve French 			cifs_server_unlock(server);
38338c8a9a5SSteve French 			mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
38438c8a9a5SSteve French 		}
38538c8a9a5SSteve French 	} while (server->tcpStatus == CifsNeedReconnect);
38638c8a9a5SSteve French 
38738c8a9a5SSteve French 	spin_lock(&server->srv_lock);
38838c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedNegotiate)
38938c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
39038c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
39138c8a9a5SSteve French 
39238c8a9a5SSteve French 	wake_up(&server->response_q);
39338c8a9a5SSteve French 	return rc;
39438c8a9a5SSteve French }
39538c8a9a5SSteve French 
39638c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
39738c8a9a5SSteve French static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
39838c8a9a5SSteve French {
39938c8a9a5SSteve French 	int rc;
40038c8a9a5SSteve French 	char *hostname;
40138c8a9a5SSteve French 
40238c8a9a5SSteve French 	if (!cifs_swn_set_server_dstaddr(server)) {
40338c8a9a5SSteve French 		if (server->hostname != target) {
40438c8a9a5SSteve French 			hostname = extract_hostname(target);
40538c8a9a5SSteve French 			if (!IS_ERR(hostname)) {
40638c8a9a5SSteve French 				spin_lock(&server->srv_lock);
40738c8a9a5SSteve French 				kfree(server->hostname);
40838c8a9a5SSteve French 				server->hostname = hostname;
40938c8a9a5SSteve French 				spin_unlock(&server->srv_lock);
41038c8a9a5SSteve French 			} else {
41138c8a9a5SSteve French 				cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
41238c8a9a5SSteve French 					 __func__, PTR_ERR(hostname));
41338c8a9a5SSteve French 				cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
41438c8a9a5SSteve French 					 server->hostname);
41538c8a9a5SSteve French 			}
41638c8a9a5SSteve French 		}
41738c8a9a5SSteve French 		/* resolve the hostname again to make sure that IP address is up-to-date. */
41838c8a9a5SSteve French 		rc = reconn_set_ipaddr_from_hostname(server);
41938c8a9a5SSteve French 		cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
42038c8a9a5SSteve French 	}
42138c8a9a5SSteve French 	/* Reconnect the socket */
42238c8a9a5SSteve French 	if (cifs_rdma_enabled(server))
42338c8a9a5SSteve French 		rc = smbd_reconnect(server);
42438c8a9a5SSteve French 	else
42538c8a9a5SSteve French 		rc = generic_ip_connect(server);
42638c8a9a5SSteve French 
42738c8a9a5SSteve French 	return rc;
42838c8a9a5SSteve French }
42938c8a9a5SSteve French 
43038c8a9a5SSteve French static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
43138c8a9a5SSteve French 				     struct dfs_cache_tgt_iterator **target_hint)
43238c8a9a5SSteve French {
43338c8a9a5SSteve French 	int rc;
43438c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
43538c8a9a5SSteve French 
43638c8a9a5SSteve French 	*target_hint = NULL;
43738c8a9a5SSteve French 
43838c8a9a5SSteve French 	/* If dfs target list is empty, then reconnect to last server */
43938c8a9a5SSteve French 	tit = dfs_cache_get_tgt_iterator(tl);
44038c8a9a5SSteve French 	if (!tit)
44138c8a9a5SSteve French 		return __reconnect_target_unlocked(server, server->hostname);
44238c8a9a5SSteve French 
44338c8a9a5SSteve French 	/* Otherwise, try every dfs target in @tl */
44438c8a9a5SSteve French 	for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
44538c8a9a5SSteve French 		rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
44638c8a9a5SSteve French 		if (!rc) {
44738c8a9a5SSteve French 			*target_hint = tit;
44838c8a9a5SSteve French 			break;
44938c8a9a5SSteve French 		}
45038c8a9a5SSteve French 	}
45138c8a9a5SSteve French 	return rc;
45238c8a9a5SSteve French }
45338c8a9a5SSteve French 
45438c8a9a5SSteve French static int reconnect_dfs_server(struct TCP_Server_Info *server)
45538c8a9a5SSteve French {
45638c8a9a5SSteve French 	int rc = 0;
45738c8a9a5SSteve French 	struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
45838c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *target_hint = NULL;
45938c8a9a5SSteve French 	int num_targets = 0;
46038c8a9a5SSteve French 
46138c8a9a5SSteve French 	/*
46238c8a9a5SSteve French 	 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
46338c8a9a5SSteve French 	 *
46438c8a9a5SSteve French 	 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
46538c8a9a5SSteve French 	 * targets (server->nr_targets).  It's also possible that the cached referral was cleared
46638c8a9a5SSteve French 	 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
46738c8a9a5SSteve French 	 * refreshing the referral, so, in this case, default it to 1.
46838c8a9a5SSteve French 	 */
46938c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
47038c8a9a5SSteve French 	if (!dfs_cache_noreq_find(server->leaf_fullpath + 1, NULL, &tl))
47138c8a9a5SSteve French 		num_targets = dfs_cache_get_nr_tgts(&tl);
47238c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
47338c8a9a5SSteve French 	if (!num_targets)
47438c8a9a5SSteve French 		num_targets = 1;
47538c8a9a5SSteve French 
47638c8a9a5SSteve French 	if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
47738c8a9a5SSteve French 		return 0;
47838c8a9a5SSteve French 
47938c8a9a5SSteve French 	/*
48038c8a9a5SSteve French 	 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
48138c8a9a5SSteve French 	 * different server or share during failover.  It could be improved by adding some logic to
48238c8a9a5SSteve French 	 * only do that in case it connects to a different server or share, though.
48338c8a9a5SSteve French 	 */
48438c8a9a5SSteve French 	cifs_mark_tcp_ses_conns_for_reconnect(server, true);
48538c8a9a5SSteve French 
48638c8a9a5SSteve French 	cifs_abort_connection(server);
48738c8a9a5SSteve French 
48838c8a9a5SSteve French 	do {
48938c8a9a5SSteve French 		try_to_freeze();
49038c8a9a5SSteve French 		cifs_server_lock(server);
49138c8a9a5SSteve French 
49238c8a9a5SSteve French 		rc = reconnect_target_unlocked(server, &tl, &target_hint);
49338c8a9a5SSteve French 		if (rc) {
49438c8a9a5SSteve French 			/* Failed to reconnect socket */
49538c8a9a5SSteve French 			cifs_server_unlock(server);
49638c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
49738c8a9a5SSteve French 			msleep(3000);
49838c8a9a5SSteve French 			continue;
49938c8a9a5SSteve French 		}
50038c8a9a5SSteve French 		/*
50138c8a9a5SSteve French 		 * Socket was created.  Update tcp session status to CifsNeedNegotiate so that a
50238c8a9a5SSteve French 		 * process waiting for reconnect will know it needs to re-establish session and tcon
50338c8a9a5SSteve French 		 * through the reconnected target server.
50438c8a9a5SSteve French 		 */
50538c8a9a5SSteve French 		atomic_inc(&tcpSesReconnectCount);
50638c8a9a5SSteve French 		set_credits(server, 1);
50738c8a9a5SSteve French 		spin_lock(&server->srv_lock);
50838c8a9a5SSteve French 		if (server->tcpStatus != CifsExiting)
50938c8a9a5SSteve French 			server->tcpStatus = CifsNeedNegotiate;
51038c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
51138c8a9a5SSteve French 		cifs_swn_reset_server_dstaddr(server);
51238c8a9a5SSteve French 		cifs_server_unlock(server);
51338c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
51438c8a9a5SSteve French 	} while (server->tcpStatus == CifsNeedReconnect);
51538c8a9a5SSteve French 
51638c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
51738c8a9a5SSteve French 	dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, target_hint);
51838c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
51938c8a9a5SSteve French 	dfs_cache_free_tgts(&tl);
52038c8a9a5SSteve French 
52138c8a9a5SSteve French 	/* Need to set up echo worker again once connection has been established */
52238c8a9a5SSteve French 	spin_lock(&server->srv_lock);
52338c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedNegotiate)
52438c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
52538c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
52638c8a9a5SSteve French 
52738c8a9a5SSteve French 	wake_up(&server->response_q);
52838c8a9a5SSteve French 	return rc;
52938c8a9a5SSteve French }
53038c8a9a5SSteve French 
53138c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
53238c8a9a5SSteve French {
53338c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
53438c8a9a5SSteve French 	if (!server->leaf_fullpath) {
53538c8a9a5SSteve French 		mutex_unlock(&server->refpath_lock);
53638c8a9a5SSteve French 		return __cifs_reconnect(server, mark_smb_session);
53738c8a9a5SSteve French 	}
53838c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
53938c8a9a5SSteve French 
54038c8a9a5SSteve French 	return reconnect_dfs_server(server);
54138c8a9a5SSteve French }
54238c8a9a5SSteve French #else
54338c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
54438c8a9a5SSteve French {
54538c8a9a5SSteve French 	return __cifs_reconnect(server, mark_smb_session);
54638c8a9a5SSteve French }
54738c8a9a5SSteve French #endif
54838c8a9a5SSteve French 
54938c8a9a5SSteve French static void
55038c8a9a5SSteve French cifs_echo_request(struct work_struct *work)
55138c8a9a5SSteve French {
55238c8a9a5SSteve French 	int rc;
55338c8a9a5SSteve French 	struct TCP_Server_Info *server = container_of(work,
55438c8a9a5SSteve French 					struct TCP_Server_Info, echo.work);
55538c8a9a5SSteve French 
55638c8a9a5SSteve French 	/*
55738c8a9a5SSteve French 	 * We cannot send an echo if it is disabled.
55838c8a9a5SSteve French 	 * Also, no need to ping if we got a response recently.
55938c8a9a5SSteve French 	 */
56038c8a9a5SSteve French 
56138c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedReconnect ||
56238c8a9a5SSteve French 	    server->tcpStatus == CifsExiting ||
56338c8a9a5SSteve French 	    server->tcpStatus == CifsNew ||
56438c8a9a5SSteve French 	    (server->ops->can_echo && !server->ops->can_echo(server)) ||
56538c8a9a5SSteve French 	    time_before(jiffies, server->lstrp + server->echo_interval - HZ))
56638c8a9a5SSteve French 		goto requeue_echo;
56738c8a9a5SSteve French 
56838c8a9a5SSteve French 	rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
56938c8a9a5SSteve French 	cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc);
57038c8a9a5SSteve French 
57138c8a9a5SSteve French 	/* Check witness registrations */
57238c8a9a5SSteve French 	cifs_swn_check();
57338c8a9a5SSteve French 
57438c8a9a5SSteve French requeue_echo:
57538c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
57638c8a9a5SSteve French }
57738c8a9a5SSteve French 
57838c8a9a5SSteve French static bool
57938c8a9a5SSteve French allocate_buffers(struct TCP_Server_Info *server)
58038c8a9a5SSteve French {
58138c8a9a5SSteve French 	if (!server->bigbuf) {
58238c8a9a5SSteve French 		server->bigbuf = (char *)cifs_buf_get();
58338c8a9a5SSteve French 		if (!server->bigbuf) {
58438c8a9a5SSteve French 			cifs_server_dbg(VFS, "No memory for large SMB response\n");
58538c8a9a5SSteve French 			msleep(3000);
58638c8a9a5SSteve French 			/* retry will check if exiting */
58738c8a9a5SSteve French 			return false;
58838c8a9a5SSteve French 		}
58938c8a9a5SSteve French 	} else if (server->large_buf) {
59038c8a9a5SSteve French 		/* we are reusing a dirty large buf, clear its start */
59138c8a9a5SSteve French 		memset(server->bigbuf, 0, HEADER_SIZE(server));
59238c8a9a5SSteve French 	}
59338c8a9a5SSteve French 
59438c8a9a5SSteve French 	if (!server->smallbuf) {
59538c8a9a5SSteve French 		server->smallbuf = (char *)cifs_small_buf_get();
59638c8a9a5SSteve French 		if (!server->smallbuf) {
59738c8a9a5SSteve French 			cifs_server_dbg(VFS, "No memory for SMB response\n");
59838c8a9a5SSteve French 			msleep(1000);
59938c8a9a5SSteve French 			/* retry will check if exiting */
60038c8a9a5SSteve French 			return false;
60138c8a9a5SSteve French 		}
60238c8a9a5SSteve French 		/* beginning of smb buffer is cleared in our buf_get */
60338c8a9a5SSteve French 	} else {
60438c8a9a5SSteve French 		/* if existing small buf clear beginning */
60538c8a9a5SSteve French 		memset(server->smallbuf, 0, HEADER_SIZE(server));
60638c8a9a5SSteve French 	}
60738c8a9a5SSteve French 
60838c8a9a5SSteve French 	return true;
60938c8a9a5SSteve French }
61038c8a9a5SSteve French 
61138c8a9a5SSteve French static bool
61238c8a9a5SSteve French server_unresponsive(struct TCP_Server_Info *server)
61338c8a9a5SSteve French {
61438c8a9a5SSteve French 	/*
61538c8a9a5SSteve French 	 * We need to wait 3 echo intervals to make sure we handle such
61638c8a9a5SSteve French 	 * situations right:
61738c8a9a5SSteve French 	 * 1s  client sends a normal SMB request
61838c8a9a5SSteve French 	 * 2s  client gets a response
61938c8a9a5SSteve French 	 * 30s echo workqueue job pops, and decides we got a response recently
62038c8a9a5SSteve French 	 *     and don't need to send another
62138c8a9a5SSteve French 	 * ...
62238c8a9a5SSteve French 	 * 65s kernel_recvmsg times out, and we see that we haven't gotten
62338c8a9a5SSteve French 	 *     a response in >60s.
62438c8a9a5SSteve French 	 */
62538c8a9a5SSteve French 	spin_lock(&server->srv_lock);
62638c8a9a5SSteve French 	if ((server->tcpStatus == CifsGood ||
62738c8a9a5SSteve French 	    server->tcpStatus == CifsNeedNegotiate) &&
62838c8a9a5SSteve French 	    (!server->ops->can_echo || server->ops->can_echo(server)) &&
62938c8a9a5SSteve French 	    time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
63038c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
63138c8a9a5SSteve French 		cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
63238c8a9a5SSteve French 			 (3 * server->echo_interval) / HZ);
63338c8a9a5SSteve French 		cifs_reconnect(server, false);
63438c8a9a5SSteve French 		return true;
63538c8a9a5SSteve French 	}
63638c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
63738c8a9a5SSteve French 
63838c8a9a5SSteve French 	return false;
63938c8a9a5SSteve French }
64038c8a9a5SSteve French 
64138c8a9a5SSteve French static inline bool
64238c8a9a5SSteve French zero_credits(struct TCP_Server_Info *server)
64338c8a9a5SSteve French {
64438c8a9a5SSteve French 	int val;
64538c8a9a5SSteve French 
64638c8a9a5SSteve French 	spin_lock(&server->req_lock);
64738c8a9a5SSteve French 	val = server->credits + server->echo_credits + server->oplock_credits;
64838c8a9a5SSteve French 	if (server->in_flight == 0 && val == 0) {
64938c8a9a5SSteve French 		spin_unlock(&server->req_lock);
65038c8a9a5SSteve French 		return true;
65138c8a9a5SSteve French 	}
65238c8a9a5SSteve French 	spin_unlock(&server->req_lock);
65338c8a9a5SSteve French 	return false;
65438c8a9a5SSteve French }
65538c8a9a5SSteve French 
65638c8a9a5SSteve French static int
65738c8a9a5SSteve French cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
65838c8a9a5SSteve French {
65938c8a9a5SSteve French 	int length = 0;
66038c8a9a5SSteve French 	int total_read;
66138c8a9a5SSteve French 
66238c8a9a5SSteve French 	for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
66338c8a9a5SSteve French 		try_to_freeze();
66438c8a9a5SSteve French 
66538c8a9a5SSteve French 		/* reconnect if no credits and no requests in flight */
66638c8a9a5SSteve French 		if (zero_credits(server)) {
66738c8a9a5SSteve French 			cifs_reconnect(server, false);
66838c8a9a5SSteve French 			return -ECONNABORTED;
66938c8a9a5SSteve French 		}
67038c8a9a5SSteve French 
67138c8a9a5SSteve French 		if (server_unresponsive(server))
67238c8a9a5SSteve French 			return -ECONNABORTED;
67338c8a9a5SSteve French 		if (cifs_rdma_enabled(server) && server->smbd_conn)
67438c8a9a5SSteve French 			length = smbd_recv(server->smbd_conn, smb_msg);
67538c8a9a5SSteve French 		else
67638c8a9a5SSteve French 			length = sock_recvmsg(server->ssocket, smb_msg, 0);
67738c8a9a5SSteve French 
67838c8a9a5SSteve French 		spin_lock(&server->srv_lock);
67938c8a9a5SSteve French 		if (server->tcpStatus == CifsExiting) {
68038c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
68138c8a9a5SSteve French 			return -ESHUTDOWN;
68238c8a9a5SSteve French 		}
68338c8a9a5SSteve French 
68438c8a9a5SSteve French 		if (server->tcpStatus == CifsNeedReconnect) {
68538c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
68638c8a9a5SSteve French 			cifs_reconnect(server, false);
68738c8a9a5SSteve French 			return -ECONNABORTED;
68838c8a9a5SSteve French 		}
68938c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
69038c8a9a5SSteve French 
69138c8a9a5SSteve French 		if (length == -ERESTARTSYS ||
69238c8a9a5SSteve French 		    length == -EAGAIN ||
69338c8a9a5SSteve French 		    length == -EINTR) {
69438c8a9a5SSteve French 			/*
69538c8a9a5SSteve French 			 * Minimum sleep to prevent looping, allowing socket
69638c8a9a5SSteve French 			 * to clear and app threads to set tcpStatus
69738c8a9a5SSteve French 			 * CifsNeedReconnect if server hung.
69838c8a9a5SSteve French 			 */
69938c8a9a5SSteve French 			usleep_range(1000, 2000);
70038c8a9a5SSteve French 			length = 0;
70138c8a9a5SSteve French 			continue;
70238c8a9a5SSteve French 		}
70338c8a9a5SSteve French 
70438c8a9a5SSteve French 		if (length <= 0) {
70538c8a9a5SSteve French 			cifs_dbg(FYI, "Received no data or error: %d\n", length);
70638c8a9a5SSteve French 			cifs_reconnect(server, false);
70738c8a9a5SSteve French 			return -ECONNABORTED;
70838c8a9a5SSteve French 		}
70938c8a9a5SSteve French 	}
71038c8a9a5SSteve French 	return total_read;
71138c8a9a5SSteve French }
71238c8a9a5SSteve French 
71338c8a9a5SSteve French int
71438c8a9a5SSteve French cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
71538c8a9a5SSteve French 		      unsigned int to_read)
71638c8a9a5SSteve French {
71738c8a9a5SSteve French 	struct msghdr smb_msg = {};
71838c8a9a5SSteve French 	struct kvec iov = {.iov_base = buf, .iov_len = to_read};
71938c8a9a5SSteve French 	iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read);
72038c8a9a5SSteve French 
72138c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
72238c8a9a5SSteve French }
72338c8a9a5SSteve French 
72438c8a9a5SSteve French ssize_t
72538c8a9a5SSteve French cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
72638c8a9a5SSteve French {
72738c8a9a5SSteve French 	struct msghdr smb_msg = {};
72838c8a9a5SSteve French 
72938c8a9a5SSteve French 	/*
73038c8a9a5SSteve French 	 *  iov_iter_discard already sets smb_msg.type and count and iov_offset
73138c8a9a5SSteve French 	 *  and cifs_readv_from_socket sets msg_control and msg_controllen
73238c8a9a5SSteve French 	 *  so little to initialize in struct msghdr
73338c8a9a5SSteve French 	 */
73438c8a9a5SSteve French 	iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read);
73538c8a9a5SSteve French 
73638c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
73738c8a9a5SSteve French }
73838c8a9a5SSteve French 
73938c8a9a5SSteve French int
74038c8a9a5SSteve French cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
74138c8a9a5SSteve French 	unsigned int page_offset, unsigned int to_read)
74238c8a9a5SSteve French {
74338c8a9a5SSteve French 	struct msghdr smb_msg = {};
74438c8a9a5SSteve French 	struct bio_vec bv;
74538c8a9a5SSteve French 
74638c8a9a5SSteve French 	bvec_set_page(&bv, page, to_read, page_offset);
74738c8a9a5SSteve French 	iov_iter_bvec(&smb_msg.msg_iter, ITER_DEST, &bv, 1, to_read);
74838c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
74938c8a9a5SSteve French }
75038c8a9a5SSteve French 
75138c8a9a5SSteve French int
75238c8a9a5SSteve French cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter,
75338c8a9a5SSteve French 			   unsigned int to_read)
75438c8a9a5SSteve French {
75538c8a9a5SSteve French 	struct msghdr smb_msg = { .msg_iter = *iter };
75638c8a9a5SSteve French 	int ret;
75738c8a9a5SSteve French 
75838c8a9a5SSteve French 	iov_iter_truncate(&smb_msg.msg_iter, to_read);
75938c8a9a5SSteve French 	ret = cifs_readv_from_socket(server, &smb_msg);
76038c8a9a5SSteve French 	if (ret > 0)
76138c8a9a5SSteve French 		iov_iter_advance(iter, ret);
76238c8a9a5SSteve French 	return ret;
76338c8a9a5SSteve French }
76438c8a9a5SSteve French 
76538c8a9a5SSteve French static bool
76638c8a9a5SSteve French is_smb_response(struct TCP_Server_Info *server, unsigned char type)
76738c8a9a5SSteve French {
76838c8a9a5SSteve French 	/*
76938c8a9a5SSteve French 	 * The first byte big endian of the length field,
77038c8a9a5SSteve French 	 * is actually not part of the length but the type
77138c8a9a5SSteve French 	 * with the most common, zero, as regular data.
77238c8a9a5SSteve French 	 */
77338c8a9a5SSteve French 	switch (type) {
77438c8a9a5SSteve French 	case RFC1002_SESSION_MESSAGE:
77538c8a9a5SSteve French 		/* Regular SMB response */
77638c8a9a5SSteve French 		return true;
77738c8a9a5SSteve French 	case RFC1002_SESSION_KEEP_ALIVE:
77838c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 session keep alive\n");
77938c8a9a5SSteve French 		break;
78038c8a9a5SSteve French 	case RFC1002_POSITIVE_SESSION_RESPONSE:
78138c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 positive session response\n");
78238c8a9a5SSteve French 		break;
78338c8a9a5SSteve French 	case RFC1002_NEGATIVE_SESSION_RESPONSE:
78438c8a9a5SSteve French 		/*
78538c8a9a5SSteve French 		 * We get this from Windows 98 instead of an error on
78638c8a9a5SSteve French 		 * SMB negprot response.
78738c8a9a5SSteve French 		 */
78838c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 negative session response\n");
78938c8a9a5SSteve French 		/* give server a second to clean up */
79038c8a9a5SSteve French 		msleep(1000);
79138c8a9a5SSteve French 		/*
79238c8a9a5SSteve French 		 * Always try 445 first on reconnect since we get NACK
79338c8a9a5SSteve French 		 * on some if we ever connected to port 139 (the NACK
79438c8a9a5SSteve French 		 * is since we do not begin with RFC1001 session
79538c8a9a5SSteve French 		 * initialize frame).
79638c8a9a5SSteve French 		 */
79738c8a9a5SSteve French 		cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
79838c8a9a5SSteve French 		cifs_reconnect(server, true);
79938c8a9a5SSteve French 		break;
80038c8a9a5SSteve French 	default:
80138c8a9a5SSteve French 		cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
80238c8a9a5SSteve French 		cifs_reconnect(server, true);
80338c8a9a5SSteve French 	}
80438c8a9a5SSteve French 
80538c8a9a5SSteve French 	return false;
80638c8a9a5SSteve French }
80738c8a9a5SSteve French 
80838c8a9a5SSteve French void
80938c8a9a5SSteve French dequeue_mid(struct mid_q_entry *mid, bool malformed)
81038c8a9a5SSteve French {
81138c8a9a5SSteve French #ifdef CONFIG_CIFS_STATS2
81238c8a9a5SSteve French 	mid->when_received = jiffies;
81338c8a9a5SSteve French #endif
81438c8a9a5SSteve French 	spin_lock(&mid->server->mid_lock);
81538c8a9a5SSteve French 	if (!malformed)
81638c8a9a5SSteve French 		mid->mid_state = MID_RESPONSE_RECEIVED;
81738c8a9a5SSteve French 	else
81838c8a9a5SSteve French 		mid->mid_state = MID_RESPONSE_MALFORMED;
81938c8a9a5SSteve French 	/*
82038c8a9a5SSteve French 	 * Trying to handle/dequeue a mid after the send_recv()
82138c8a9a5SSteve French 	 * function has finished processing it is a bug.
82238c8a9a5SSteve French 	 */
82338c8a9a5SSteve French 	if (mid->mid_flags & MID_DELETED) {
82438c8a9a5SSteve French 		spin_unlock(&mid->server->mid_lock);
82538c8a9a5SSteve French 		pr_warn_once("trying to dequeue a deleted mid\n");
82638c8a9a5SSteve French 	} else {
82738c8a9a5SSteve French 		list_del_init(&mid->qhead);
82838c8a9a5SSteve French 		mid->mid_flags |= MID_DELETED;
82938c8a9a5SSteve French 		spin_unlock(&mid->server->mid_lock);
83038c8a9a5SSteve French 	}
83138c8a9a5SSteve French }
83238c8a9a5SSteve French 
83338c8a9a5SSteve French static unsigned int
83438c8a9a5SSteve French smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
83538c8a9a5SSteve French {
83638c8a9a5SSteve French 	struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
83738c8a9a5SSteve French 
83838c8a9a5SSteve French 	/*
83938c8a9a5SSteve French 	 * SMB1 does not use credits.
84038c8a9a5SSteve French 	 */
84138c8a9a5SSteve French 	if (is_smb1(server))
84238c8a9a5SSteve French 		return 0;
84338c8a9a5SSteve French 
84438c8a9a5SSteve French 	return le16_to_cpu(shdr->CreditRequest);
84538c8a9a5SSteve French }
84638c8a9a5SSteve French 
84738c8a9a5SSteve French static void
84838c8a9a5SSteve French handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
84938c8a9a5SSteve French 	   char *buf, int malformed)
85038c8a9a5SSteve French {
85138c8a9a5SSteve French 	if (server->ops->check_trans2 &&
85238c8a9a5SSteve French 	    server->ops->check_trans2(mid, server, buf, malformed))
85338c8a9a5SSteve French 		return;
85438c8a9a5SSteve French 	mid->credits_received = smb2_get_credits_from_hdr(buf, server);
85538c8a9a5SSteve French 	mid->resp_buf = buf;
85638c8a9a5SSteve French 	mid->large_buf = server->large_buf;
85738c8a9a5SSteve French 	/* Was previous buf put in mpx struct for multi-rsp? */
85838c8a9a5SSteve French 	if (!mid->multiRsp) {
85938c8a9a5SSteve French 		/* smb buffer will be freed by user thread */
86038c8a9a5SSteve French 		if (server->large_buf)
86138c8a9a5SSteve French 			server->bigbuf = NULL;
86238c8a9a5SSteve French 		else
86338c8a9a5SSteve French 			server->smallbuf = NULL;
86438c8a9a5SSteve French 	}
86538c8a9a5SSteve French 	dequeue_mid(mid, malformed);
86638c8a9a5SSteve French }
86738c8a9a5SSteve French 
86838c8a9a5SSteve French int
86938c8a9a5SSteve French cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
87038c8a9a5SSteve French {
87138c8a9a5SSteve French 	bool srv_sign_required = server->sec_mode & server->vals->signing_required;
87238c8a9a5SSteve French 	bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
87338c8a9a5SSteve French 	bool mnt_sign_enabled;
87438c8a9a5SSteve French 
87538c8a9a5SSteve French 	/*
87638c8a9a5SSteve French 	 * Is signing required by mnt options? If not then check
87738c8a9a5SSteve French 	 * global_secflags to see if it is there.
87838c8a9a5SSteve French 	 */
87938c8a9a5SSteve French 	if (!mnt_sign_required)
88038c8a9a5SSteve French 		mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
88138c8a9a5SSteve French 						CIFSSEC_MUST_SIGN);
88238c8a9a5SSteve French 
88338c8a9a5SSteve French 	/*
88438c8a9a5SSteve French 	 * If signing is required then it's automatically enabled too,
88538c8a9a5SSteve French 	 * otherwise, check to see if the secflags allow it.
88638c8a9a5SSteve French 	 */
88738c8a9a5SSteve French 	mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
88838c8a9a5SSteve French 				(global_secflags & CIFSSEC_MAY_SIGN);
88938c8a9a5SSteve French 
89038c8a9a5SSteve French 	/* If server requires signing, does client allow it? */
89138c8a9a5SSteve French 	if (srv_sign_required) {
89238c8a9a5SSteve French 		if (!mnt_sign_enabled) {
89338c8a9a5SSteve French 			cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n");
89438c8a9a5SSteve French 			return -EOPNOTSUPP;
89538c8a9a5SSteve French 		}
89638c8a9a5SSteve French 		server->sign = true;
89738c8a9a5SSteve French 	}
89838c8a9a5SSteve French 
89938c8a9a5SSteve French 	/* If client requires signing, does server allow it? */
90038c8a9a5SSteve French 	if (mnt_sign_required) {
90138c8a9a5SSteve French 		if (!srv_sign_enabled) {
90238c8a9a5SSteve French 			cifs_dbg(VFS, "Server does not support signing!\n");
90338c8a9a5SSteve French 			return -EOPNOTSUPP;
90438c8a9a5SSteve French 		}
90538c8a9a5SSteve French 		server->sign = true;
90638c8a9a5SSteve French 	}
90738c8a9a5SSteve French 
90838c8a9a5SSteve French 	if (cifs_rdma_enabled(server) && server->sign)
90938c8a9a5SSteve French 		cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n");
91038c8a9a5SSteve French 
91138c8a9a5SSteve French 	return 0;
91238c8a9a5SSteve French }
91338c8a9a5SSteve French 
91438c8a9a5SSteve French 
91538c8a9a5SSteve French static void clean_demultiplex_info(struct TCP_Server_Info *server)
91638c8a9a5SSteve French {
91738c8a9a5SSteve French 	int length;
91838c8a9a5SSteve French 
91938c8a9a5SSteve French 	/* take it off the list, if it's not already */
92038c8a9a5SSteve French 	spin_lock(&server->srv_lock);
92138c8a9a5SSteve French 	list_del_init(&server->tcp_ses_list);
92238c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
92338c8a9a5SSteve French 
92438c8a9a5SSteve French 	cancel_delayed_work_sync(&server->echo);
92538c8a9a5SSteve French 
92638c8a9a5SSteve French 	spin_lock(&server->srv_lock);
92738c8a9a5SSteve French 	server->tcpStatus = CifsExiting;
92838c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
92938c8a9a5SSteve French 	wake_up_all(&server->response_q);
93038c8a9a5SSteve French 
93138c8a9a5SSteve French 	/* check if we have blocked requests that need to free */
93238c8a9a5SSteve French 	spin_lock(&server->req_lock);
93338c8a9a5SSteve French 	if (server->credits <= 0)
93438c8a9a5SSteve French 		server->credits = 1;
93538c8a9a5SSteve French 	spin_unlock(&server->req_lock);
93638c8a9a5SSteve French 	/*
93738c8a9a5SSteve French 	 * Although there should not be any requests blocked on this queue it
93838c8a9a5SSteve French 	 * can not hurt to be paranoid and try to wake up requests that may
93938c8a9a5SSteve French 	 * haven been blocked when more than 50 at time were on the wire to the
94038c8a9a5SSteve French 	 * same server - they now will see the session is in exit state and get
94138c8a9a5SSteve French 	 * out of SendReceive.
94238c8a9a5SSteve French 	 */
94338c8a9a5SSteve French 	wake_up_all(&server->request_q);
94438c8a9a5SSteve French 	/* give those requests time to exit */
94538c8a9a5SSteve French 	msleep(125);
94638c8a9a5SSteve French 	if (cifs_rdma_enabled(server))
94738c8a9a5SSteve French 		smbd_destroy(server);
94838c8a9a5SSteve French 	if (server->ssocket) {
94938c8a9a5SSteve French 		sock_release(server->ssocket);
95038c8a9a5SSteve French 		server->ssocket = NULL;
95138c8a9a5SSteve French 	}
95238c8a9a5SSteve French 
95338c8a9a5SSteve French 	if (!list_empty(&server->pending_mid_q)) {
95438c8a9a5SSteve French 		struct list_head dispose_list;
95538c8a9a5SSteve French 		struct mid_q_entry *mid_entry;
95638c8a9a5SSteve French 		struct list_head *tmp, *tmp2;
95738c8a9a5SSteve French 
95838c8a9a5SSteve French 		INIT_LIST_HEAD(&dispose_list);
95938c8a9a5SSteve French 		spin_lock(&server->mid_lock);
96038c8a9a5SSteve French 		list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
96138c8a9a5SSteve French 			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
96238c8a9a5SSteve French 			cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
96338c8a9a5SSteve French 			kref_get(&mid_entry->refcount);
96438c8a9a5SSteve French 			mid_entry->mid_state = MID_SHUTDOWN;
96538c8a9a5SSteve French 			list_move(&mid_entry->qhead, &dispose_list);
96638c8a9a5SSteve French 			mid_entry->mid_flags |= MID_DELETED;
96738c8a9a5SSteve French 		}
96838c8a9a5SSteve French 		spin_unlock(&server->mid_lock);
96938c8a9a5SSteve French 
97038c8a9a5SSteve French 		/* now walk dispose list and issue callbacks */
97138c8a9a5SSteve French 		list_for_each_safe(tmp, tmp2, &dispose_list) {
97238c8a9a5SSteve French 			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
97338c8a9a5SSteve French 			cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
97438c8a9a5SSteve French 			list_del_init(&mid_entry->qhead);
97538c8a9a5SSteve French 			mid_entry->callback(mid_entry);
97638c8a9a5SSteve French 			release_mid(mid_entry);
97738c8a9a5SSteve French 		}
97838c8a9a5SSteve French 		/* 1/8th of sec is more than enough time for them to exit */
97938c8a9a5SSteve French 		msleep(125);
98038c8a9a5SSteve French 	}
98138c8a9a5SSteve French 
98238c8a9a5SSteve French 	if (!list_empty(&server->pending_mid_q)) {
98338c8a9a5SSteve French 		/*
98438c8a9a5SSteve French 		 * mpx threads have not exited yet give them at least the smb
98538c8a9a5SSteve French 		 * send timeout time for long ops.
98638c8a9a5SSteve French 		 *
98738c8a9a5SSteve French 		 * Due to delays on oplock break requests, we need to wait at
98838c8a9a5SSteve French 		 * least 45 seconds before giving up on a request getting a
98938c8a9a5SSteve French 		 * response and going ahead and killing cifsd.
99038c8a9a5SSteve French 		 */
99138c8a9a5SSteve French 		cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
99238c8a9a5SSteve French 		msleep(46000);
99338c8a9a5SSteve French 		/*
99438c8a9a5SSteve French 		 * If threads still have not exited they are probably never
99538c8a9a5SSteve French 		 * coming home not much else we can do but free the memory.
99638c8a9a5SSteve French 		 */
99738c8a9a5SSteve French 	}
99838c8a9a5SSteve French 
99938c8a9a5SSteve French 	kfree(server->origin_fullpath);
100038c8a9a5SSteve French 	kfree(server->leaf_fullpath);
100138c8a9a5SSteve French 	kfree(server);
100238c8a9a5SSteve French 
100338c8a9a5SSteve French 	length = atomic_dec_return(&tcpSesAllocCount);
100438c8a9a5SSteve French 	if (length > 0)
100538c8a9a5SSteve French 		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
100638c8a9a5SSteve French }
100738c8a9a5SSteve French 
100838c8a9a5SSteve French static int
100938c8a9a5SSteve French standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
101038c8a9a5SSteve French {
101138c8a9a5SSteve French 	int length;
101238c8a9a5SSteve French 	char *buf = server->smallbuf;
101338c8a9a5SSteve French 	unsigned int pdu_length = server->pdu_size;
101438c8a9a5SSteve French 
101538c8a9a5SSteve French 	/* make sure this will fit in a large buffer */
101638c8a9a5SSteve French 	if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
101738c8a9a5SSteve French 	    HEADER_PREAMBLE_SIZE(server)) {
101838c8a9a5SSteve French 		cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
101938c8a9a5SSteve French 		cifs_reconnect(server, true);
102038c8a9a5SSteve French 		return -ECONNABORTED;
102138c8a9a5SSteve French 	}
102238c8a9a5SSteve French 
102338c8a9a5SSteve French 	/* switch to large buffer if too big for a small one */
102438c8a9a5SSteve French 	if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
102538c8a9a5SSteve French 		server->large_buf = true;
102638c8a9a5SSteve French 		memcpy(server->bigbuf, buf, server->total_read);
102738c8a9a5SSteve French 		buf = server->bigbuf;
102838c8a9a5SSteve French 	}
102938c8a9a5SSteve French 
103038c8a9a5SSteve French 	/* now read the rest */
103138c8a9a5SSteve French 	length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
103238c8a9a5SSteve French 				       pdu_length - MID_HEADER_SIZE(server));
103338c8a9a5SSteve French 
103438c8a9a5SSteve French 	if (length < 0)
103538c8a9a5SSteve French 		return length;
103638c8a9a5SSteve French 	server->total_read += length;
103738c8a9a5SSteve French 
103838c8a9a5SSteve French 	dump_smb(buf, server->total_read);
103938c8a9a5SSteve French 
104038c8a9a5SSteve French 	return cifs_handle_standard(server, mid);
104138c8a9a5SSteve French }
104238c8a9a5SSteve French 
104338c8a9a5SSteve French int
104438c8a9a5SSteve French cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
104538c8a9a5SSteve French {
104638c8a9a5SSteve French 	char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
104738c8a9a5SSteve French 	int rc;
104838c8a9a5SSteve French 
104938c8a9a5SSteve French 	/*
105038c8a9a5SSteve French 	 * We know that we received enough to get to the MID as we
105138c8a9a5SSteve French 	 * checked the pdu_length earlier. Now check to see
105238c8a9a5SSteve French 	 * if the rest of the header is OK.
105338c8a9a5SSteve French 	 *
105438c8a9a5SSteve French 	 * 48 bytes is enough to display the header and a little bit
105538c8a9a5SSteve French 	 * into the payload for debugging purposes.
105638c8a9a5SSteve French 	 */
105738c8a9a5SSteve French 	rc = server->ops->check_message(buf, server->total_read, server);
105838c8a9a5SSteve French 	if (rc)
105938c8a9a5SSteve French 		cifs_dump_mem("Bad SMB: ", buf,
106038c8a9a5SSteve French 			min_t(unsigned int, server->total_read, 48));
106138c8a9a5SSteve French 
106238c8a9a5SSteve French 	if (server->ops->is_session_expired &&
106338c8a9a5SSteve French 	    server->ops->is_session_expired(buf)) {
106438c8a9a5SSteve French 		cifs_reconnect(server, true);
106538c8a9a5SSteve French 		return -1;
106638c8a9a5SSteve French 	}
106738c8a9a5SSteve French 
106838c8a9a5SSteve French 	if (server->ops->is_status_pending &&
106938c8a9a5SSteve French 	    server->ops->is_status_pending(buf, server))
107038c8a9a5SSteve French 		return -1;
107138c8a9a5SSteve French 
107238c8a9a5SSteve French 	if (!mid)
107338c8a9a5SSteve French 		return rc;
107438c8a9a5SSteve French 
107538c8a9a5SSteve French 	handle_mid(mid, server, buf, rc);
107638c8a9a5SSteve French 	return 0;
107738c8a9a5SSteve French }
107838c8a9a5SSteve French 
107938c8a9a5SSteve French static void
108038c8a9a5SSteve French smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
108138c8a9a5SSteve French {
108238c8a9a5SSteve French 	struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
108338c8a9a5SSteve French 	int scredits, in_flight;
108438c8a9a5SSteve French 
108538c8a9a5SSteve French 	/*
108638c8a9a5SSteve French 	 * SMB1 does not use credits.
108738c8a9a5SSteve French 	 */
108838c8a9a5SSteve French 	if (is_smb1(server))
108938c8a9a5SSteve French 		return;
109038c8a9a5SSteve French 
109138c8a9a5SSteve French 	if (shdr->CreditRequest) {
109238c8a9a5SSteve French 		spin_lock(&server->req_lock);
109338c8a9a5SSteve French 		server->credits += le16_to_cpu(shdr->CreditRequest);
109438c8a9a5SSteve French 		scredits = server->credits;
109538c8a9a5SSteve French 		in_flight = server->in_flight;
109638c8a9a5SSteve French 		spin_unlock(&server->req_lock);
109738c8a9a5SSteve French 		wake_up(&server->request_q);
109838c8a9a5SSteve French 
109938c8a9a5SSteve French 		trace_smb3_hdr_credits(server->CurrentMid,
110038c8a9a5SSteve French 				server->conn_id, server->hostname, scredits,
110138c8a9a5SSteve French 				le16_to_cpu(shdr->CreditRequest), in_flight);
110238c8a9a5SSteve French 		cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
110338c8a9a5SSteve French 				__func__, le16_to_cpu(shdr->CreditRequest),
110438c8a9a5SSteve French 				scredits);
110538c8a9a5SSteve French 	}
110638c8a9a5SSteve French }
110738c8a9a5SSteve French 
110838c8a9a5SSteve French 
110938c8a9a5SSteve French static int
111038c8a9a5SSteve French cifs_demultiplex_thread(void *p)
111138c8a9a5SSteve French {
111238c8a9a5SSteve French 	int i, num_mids, length;
111338c8a9a5SSteve French 	struct TCP_Server_Info *server = p;
111438c8a9a5SSteve French 	unsigned int pdu_length;
111538c8a9a5SSteve French 	unsigned int next_offset;
111638c8a9a5SSteve French 	char *buf = NULL;
111738c8a9a5SSteve French 	struct task_struct *task_to_wake = NULL;
111838c8a9a5SSteve French 	struct mid_q_entry *mids[MAX_COMPOUND];
111938c8a9a5SSteve French 	char *bufs[MAX_COMPOUND];
112038c8a9a5SSteve French 	unsigned int noreclaim_flag, num_io_timeout = 0;
112138c8a9a5SSteve French 
112238c8a9a5SSteve French 	noreclaim_flag = memalloc_noreclaim_save();
112338c8a9a5SSteve French 	cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
112438c8a9a5SSteve French 
112538c8a9a5SSteve French 	length = atomic_inc_return(&tcpSesAllocCount);
112638c8a9a5SSteve French 	if (length > 1)
112738c8a9a5SSteve French 		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
112838c8a9a5SSteve French 
112938c8a9a5SSteve French 	set_freezable();
113038c8a9a5SSteve French 	allow_kernel_signal(SIGKILL);
113138c8a9a5SSteve French 	while (server->tcpStatus != CifsExiting) {
113238c8a9a5SSteve French 		if (try_to_freeze())
113338c8a9a5SSteve French 			continue;
113438c8a9a5SSteve French 
113538c8a9a5SSteve French 		if (!allocate_buffers(server))
113638c8a9a5SSteve French 			continue;
113738c8a9a5SSteve French 
113838c8a9a5SSteve French 		server->large_buf = false;
113938c8a9a5SSteve French 		buf = server->smallbuf;
114038c8a9a5SSteve French 		pdu_length = 4; /* enough to get RFC1001 header */
114138c8a9a5SSteve French 
114238c8a9a5SSteve French 		length = cifs_read_from_socket(server, buf, pdu_length);
114338c8a9a5SSteve French 		if (length < 0)
114438c8a9a5SSteve French 			continue;
114538c8a9a5SSteve French 
114638c8a9a5SSteve French 		if (is_smb1(server))
114738c8a9a5SSteve French 			server->total_read = length;
114838c8a9a5SSteve French 		else
114938c8a9a5SSteve French 			server->total_read = 0;
115038c8a9a5SSteve French 
115138c8a9a5SSteve French 		/*
115238c8a9a5SSteve French 		 * The right amount was read from socket - 4 bytes,
115338c8a9a5SSteve French 		 * so we can now interpret the length field.
115438c8a9a5SSteve French 		 */
115538c8a9a5SSteve French 		pdu_length = get_rfc1002_length(buf);
115638c8a9a5SSteve French 
115738c8a9a5SSteve French 		cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
115838c8a9a5SSteve French 		if (!is_smb_response(server, buf[0]))
115938c8a9a5SSteve French 			continue;
116038c8a9a5SSteve French next_pdu:
116138c8a9a5SSteve French 		server->pdu_size = pdu_length;
116238c8a9a5SSteve French 
116338c8a9a5SSteve French 		/* make sure we have enough to get to the MID */
116438c8a9a5SSteve French 		if (server->pdu_size < MID_HEADER_SIZE(server)) {
116538c8a9a5SSteve French 			cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
116638c8a9a5SSteve French 				 server->pdu_size);
116738c8a9a5SSteve French 			cifs_reconnect(server, true);
116838c8a9a5SSteve French 			continue;
116938c8a9a5SSteve French 		}
117038c8a9a5SSteve French 
117138c8a9a5SSteve French 		/* read down to the MID */
117238c8a9a5SSteve French 		length = cifs_read_from_socket(server,
117338c8a9a5SSteve French 			     buf + HEADER_PREAMBLE_SIZE(server),
117438c8a9a5SSteve French 			     MID_HEADER_SIZE(server));
117538c8a9a5SSteve French 		if (length < 0)
117638c8a9a5SSteve French 			continue;
117738c8a9a5SSteve French 		server->total_read += length;
117838c8a9a5SSteve French 
117938c8a9a5SSteve French 		if (server->ops->next_header) {
118038c8a9a5SSteve French 			next_offset = server->ops->next_header(buf);
118138c8a9a5SSteve French 			if (next_offset)
118238c8a9a5SSteve French 				server->pdu_size = next_offset;
118338c8a9a5SSteve French 		}
118438c8a9a5SSteve French 
118538c8a9a5SSteve French 		memset(mids, 0, sizeof(mids));
118638c8a9a5SSteve French 		memset(bufs, 0, sizeof(bufs));
118738c8a9a5SSteve French 		num_mids = 0;
118838c8a9a5SSteve French 
118938c8a9a5SSteve French 		if (server->ops->is_transform_hdr &&
119038c8a9a5SSteve French 		    server->ops->receive_transform &&
119138c8a9a5SSteve French 		    server->ops->is_transform_hdr(buf)) {
119238c8a9a5SSteve French 			length = server->ops->receive_transform(server,
119338c8a9a5SSteve French 								mids,
119438c8a9a5SSteve French 								bufs,
119538c8a9a5SSteve French 								&num_mids);
119638c8a9a5SSteve French 		} else {
119738c8a9a5SSteve French 			mids[0] = server->ops->find_mid(server, buf);
119838c8a9a5SSteve French 			bufs[0] = buf;
119938c8a9a5SSteve French 			num_mids = 1;
120038c8a9a5SSteve French 
120138c8a9a5SSteve French 			if (!mids[0] || !mids[0]->receive)
120238c8a9a5SSteve French 				length = standard_receive3(server, mids[0]);
120338c8a9a5SSteve French 			else
120438c8a9a5SSteve French 				length = mids[0]->receive(server, mids[0]);
120538c8a9a5SSteve French 		}
120638c8a9a5SSteve French 
120738c8a9a5SSteve French 		if (length < 0) {
120838c8a9a5SSteve French 			for (i = 0; i < num_mids; i++)
120938c8a9a5SSteve French 				if (mids[i])
121038c8a9a5SSteve French 					release_mid(mids[i]);
121138c8a9a5SSteve French 			continue;
121238c8a9a5SSteve French 		}
121338c8a9a5SSteve French 
121438c8a9a5SSteve French 		if (server->ops->is_status_io_timeout &&
121538c8a9a5SSteve French 		    server->ops->is_status_io_timeout(buf)) {
121638c8a9a5SSteve French 			num_io_timeout++;
121738c8a9a5SSteve French 			if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
121838c8a9a5SSteve French 				cifs_reconnect(server, false);
121938c8a9a5SSteve French 				num_io_timeout = 0;
122038c8a9a5SSteve French 				continue;
122138c8a9a5SSteve French 			}
122238c8a9a5SSteve French 		}
122338c8a9a5SSteve French 
122438c8a9a5SSteve French 		server->lstrp = jiffies;
122538c8a9a5SSteve French 
122638c8a9a5SSteve French 		for (i = 0; i < num_mids; i++) {
122738c8a9a5SSteve French 			if (mids[i] != NULL) {
122838c8a9a5SSteve French 				mids[i]->resp_buf_size = server->pdu_size;
122938c8a9a5SSteve French 
123038c8a9a5SSteve French 				if (bufs[i] && server->ops->is_network_name_deleted)
123138c8a9a5SSteve French 					server->ops->is_network_name_deleted(bufs[i],
123238c8a9a5SSteve French 									server);
123338c8a9a5SSteve French 
123438c8a9a5SSteve French 				if (!mids[i]->multiRsp || mids[i]->multiEnd)
123538c8a9a5SSteve French 					mids[i]->callback(mids[i]);
123638c8a9a5SSteve French 
123738c8a9a5SSteve French 				release_mid(mids[i]);
123838c8a9a5SSteve French 			} else if (server->ops->is_oplock_break &&
123938c8a9a5SSteve French 				   server->ops->is_oplock_break(bufs[i],
124038c8a9a5SSteve French 								server)) {
124138c8a9a5SSteve French 				smb2_add_credits_from_hdr(bufs[i], server);
124238c8a9a5SSteve French 				cifs_dbg(FYI, "Received oplock break\n");
124338c8a9a5SSteve French 			} else {
124438c8a9a5SSteve French 				cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
124538c8a9a5SSteve French 						atomic_read(&mid_count));
124638c8a9a5SSteve French 				cifs_dump_mem("Received Data is: ", bufs[i],
124738c8a9a5SSteve French 					      HEADER_SIZE(server));
124838c8a9a5SSteve French 				smb2_add_credits_from_hdr(bufs[i], server);
124938c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2
125038c8a9a5SSteve French 				if (server->ops->dump_detail)
125138c8a9a5SSteve French 					server->ops->dump_detail(bufs[i],
125238c8a9a5SSteve French 								 server);
125338c8a9a5SSteve French 				cifs_dump_mids(server);
125438c8a9a5SSteve French #endif /* CIFS_DEBUG2 */
125538c8a9a5SSteve French 			}
125638c8a9a5SSteve French 		}
125738c8a9a5SSteve French 
125838c8a9a5SSteve French 		if (pdu_length > server->pdu_size) {
125938c8a9a5SSteve French 			if (!allocate_buffers(server))
126038c8a9a5SSteve French 				continue;
126138c8a9a5SSteve French 			pdu_length -= server->pdu_size;
126238c8a9a5SSteve French 			server->total_read = 0;
126338c8a9a5SSteve French 			server->large_buf = false;
126438c8a9a5SSteve French 			buf = server->smallbuf;
126538c8a9a5SSteve French 			goto next_pdu;
126638c8a9a5SSteve French 		}
126738c8a9a5SSteve French 	} /* end while !EXITING */
126838c8a9a5SSteve French 
126938c8a9a5SSteve French 	/* buffer usually freed in free_mid - need to free it here on exit */
127038c8a9a5SSteve French 	cifs_buf_release(server->bigbuf);
127138c8a9a5SSteve French 	if (server->smallbuf) /* no sense logging a debug message if NULL */
127238c8a9a5SSteve French 		cifs_small_buf_release(server->smallbuf);
127338c8a9a5SSteve French 
127438c8a9a5SSteve French 	task_to_wake = xchg(&server->tsk, NULL);
127538c8a9a5SSteve French 	clean_demultiplex_info(server);
127638c8a9a5SSteve French 
127738c8a9a5SSteve French 	/* if server->tsk was NULL then wait for a signal before exiting */
127838c8a9a5SSteve French 	if (!task_to_wake) {
127938c8a9a5SSteve French 		set_current_state(TASK_INTERRUPTIBLE);
128038c8a9a5SSteve French 		while (!signal_pending(current)) {
128138c8a9a5SSteve French 			schedule();
128238c8a9a5SSteve French 			set_current_state(TASK_INTERRUPTIBLE);
128338c8a9a5SSteve French 		}
128438c8a9a5SSteve French 		set_current_state(TASK_RUNNING);
128538c8a9a5SSteve French 	}
128638c8a9a5SSteve French 
128738c8a9a5SSteve French 	memalloc_noreclaim_restore(noreclaim_flag);
128838c8a9a5SSteve French 	module_put_and_kthread_exit(0);
128938c8a9a5SSteve French }
129038c8a9a5SSteve French 
129138c8a9a5SSteve French /*
129238c8a9a5SSteve French  * Returns true if srcaddr isn't specified and rhs isn't specified, or
129338c8a9a5SSteve French  * if srcaddr is specified and matches the IP address of the rhs argument
129438c8a9a5SSteve French  */
129538c8a9a5SSteve French bool
129638c8a9a5SSteve French cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
129738c8a9a5SSteve French {
129838c8a9a5SSteve French 	switch (srcaddr->sa_family) {
129938c8a9a5SSteve French 	case AF_UNSPEC:
130038c8a9a5SSteve French 		return (rhs->sa_family == AF_UNSPEC);
130138c8a9a5SSteve French 	case AF_INET: {
130238c8a9a5SSteve French 		struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
130338c8a9a5SSteve French 		struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
130438c8a9a5SSteve French 		return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
130538c8a9a5SSteve French 	}
130638c8a9a5SSteve French 	case AF_INET6: {
130738c8a9a5SSteve French 		struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
130838c8a9a5SSteve French 		struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
130938c8a9a5SSteve French 		return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr)
131038c8a9a5SSteve French 			&& saddr6->sin6_scope_id == vaddr6->sin6_scope_id);
131138c8a9a5SSteve French 	}
131238c8a9a5SSteve French 	default:
131338c8a9a5SSteve French 		WARN_ON(1);
131438c8a9a5SSteve French 		return false; /* don't expect to be here */
131538c8a9a5SSteve French 	}
131638c8a9a5SSteve French }
131738c8a9a5SSteve French 
131838c8a9a5SSteve French /*
131938c8a9a5SSteve French  * If no port is specified in addr structure, we try to match with 445 port
132038c8a9a5SSteve French  * and if it fails - with 139 ports. It should be called only if address
132138c8a9a5SSteve French  * families of server and addr are equal.
132238c8a9a5SSteve French  */
132338c8a9a5SSteve French static bool
132438c8a9a5SSteve French match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
132538c8a9a5SSteve French {
132638c8a9a5SSteve French 	__be16 port, *sport;
132738c8a9a5SSteve French 
132838c8a9a5SSteve French 	/* SMBDirect manages its own ports, don't match it here */
132938c8a9a5SSteve French 	if (server->rdma)
133038c8a9a5SSteve French 		return true;
133138c8a9a5SSteve French 
133238c8a9a5SSteve French 	switch (addr->sa_family) {
133338c8a9a5SSteve French 	case AF_INET:
133438c8a9a5SSteve French 		sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
133538c8a9a5SSteve French 		port = ((struct sockaddr_in *) addr)->sin_port;
133638c8a9a5SSteve French 		break;
133738c8a9a5SSteve French 	case AF_INET6:
133838c8a9a5SSteve French 		sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
133938c8a9a5SSteve French 		port = ((struct sockaddr_in6 *) addr)->sin6_port;
134038c8a9a5SSteve French 		break;
134138c8a9a5SSteve French 	default:
134238c8a9a5SSteve French 		WARN_ON(1);
134338c8a9a5SSteve French 		return false;
134438c8a9a5SSteve French 	}
134538c8a9a5SSteve French 
134638c8a9a5SSteve French 	if (!port) {
134738c8a9a5SSteve French 		port = htons(CIFS_PORT);
134838c8a9a5SSteve French 		if (port == *sport)
134938c8a9a5SSteve French 			return true;
135038c8a9a5SSteve French 
135138c8a9a5SSteve French 		port = htons(RFC1001_PORT);
135238c8a9a5SSteve French 	}
135338c8a9a5SSteve French 
135438c8a9a5SSteve French 	return port == *sport;
135538c8a9a5SSteve French }
135638c8a9a5SSteve French 
135738c8a9a5SSteve French static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr)
135838c8a9a5SSteve French {
135938c8a9a5SSteve French 	if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr))
136038c8a9a5SSteve French 		return false;
136138c8a9a5SSteve French 
136238c8a9a5SSteve French 	return true;
136338c8a9a5SSteve French }
136438c8a9a5SSteve French 
136538c8a9a5SSteve French static bool
136638c8a9a5SSteve French match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
136738c8a9a5SSteve French {
136838c8a9a5SSteve French 	/*
136938c8a9a5SSteve French 	 * The select_sectype function should either return the ctx->sectype
137038c8a9a5SSteve French 	 * that was specified, or "Unspecified" if that sectype was not
137138c8a9a5SSteve French 	 * compatible with the given NEGOTIATE request.
137238c8a9a5SSteve French 	 */
137338c8a9a5SSteve French 	if (server->ops->select_sectype(server, ctx->sectype)
137438c8a9a5SSteve French 	     == Unspecified)
137538c8a9a5SSteve French 		return false;
137638c8a9a5SSteve French 
137738c8a9a5SSteve French 	/*
137838c8a9a5SSteve French 	 * Now check if signing mode is acceptable. No need to check
137938c8a9a5SSteve French 	 * global_secflags at this point since if MUST_SIGN is set then
138038c8a9a5SSteve French 	 * the server->sign had better be too.
138138c8a9a5SSteve French 	 */
138238c8a9a5SSteve French 	if (ctx->sign && !server->sign)
138338c8a9a5SSteve French 		return false;
138438c8a9a5SSteve French 
138538c8a9a5SSteve French 	return true;
138638c8a9a5SSteve French }
138738c8a9a5SSteve French 
138838c8a9a5SSteve French /* this function must be called with srv_lock held */
138938c8a9a5SSteve French static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
139038c8a9a5SSteve French {
139138c8a9a5SSteve French 	struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
139238c8a9a5SSteve French 
139338c8a9a5SSteve French 	lockdep_assert_held(&server->srv_lock);
139438c8a9a5SSteve French 
139538c8a9a5SSteve French 	if (ctx->nosharesock)
139638c8a9a5SSteve French 		return 0;
139738c8a9a5SSteve French 
139838c8a9a5SSteve French 	/* this server does not share socket */
139938c8a9a5SSteve French 	if (server->nosharesock)
140038c8a9a5SSteve French 		return 0;
140138c8a9a5SSteve French 
140238c8a9a5SSteve French 	/* If multidialect negotiation see if existing sessions match one */
140338c8a9a5SSteve French 	if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
140438c8a9a5SSteve French 		if (server->vals->protocol_id < SMB30_PROT_ID)
140538c8a9a5SSteve French 			return 0;
140638c8a9a5SSteve French 	} else if (strcmp(ctx->vals->version_string,
140738c8a9a5SSteve French 		   SMBDEFAULT_VERSION_STRING) == 0) {
140838c8a9a5SSteve French 		if (server->vals->protocol_id < SMB21_PROT_ID)
140938c8a9a5SSteve French 			return 0;
141038c8a9a5SSteve French 	} else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
141138c8a9a5SSteve French 		return 0;
141238c8a9a5SSteve French 
141338c8a9a5SSteve French 	if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
141438c8a9a5SSteve French 		return 0;
141538c8a9a5SSteve French 
141638c8a9a5SSteve French 	if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr,
141738c8a9a5SSteve French 			       (struct sockaddr *)&server->srcaddr))
141838c8a9a5SSteve French 		return 0;
141938c8a9a5SSteve French 	/*
142038c8a9a5SSteve French 	 * - Match for an DFS tcon (@server->origin_fullpath).
142138c8a9a5SSteve French 	 * - Match for an DFS root server connection (@server->leaf_fullpath).
142238c8a9a5SSteve French 	 * - If none of the above and @ctx->leaf_fullpath is set, then
142338c8a9a5SSteve French 	 *   it is a new DFS connection.
142438c8a9a5SSteve French 	 * - If 'nodfs' mount option was passed, then match only connections
142538c8a9a5SSteve French 	 *   that have no DFS referrals set
142638c8a9a5SSteve French 	 *   (e.g. can't failover to other targets).
142738c8a9a5SSteve French 	 */
142838c8a9a5SSteve French 	if (!ctx->nodfs) {
142938c8a9a5SSteve French 		if (ctx->source && server->origin_fullpath) {
143038c8a9a5SSteve French 			if (!dfs_src_pathname_equal(ctx->source,
143138c8a9a5SSteve French 						    server->origin_fullpath))
143238c8a9a5SSteve French 				return 0;
143338c8a9a5SSteve French 		} else if (server->leaf_fullpath) {
143438c8a9a5SSteve French 			if (!ctx->leaf_fullpath ||
143538c8a9a5SSteve French 			    strcasecmp(server->leaf_fullpath,
143638c8a9a5SSteve French 				       ctx->leaf_fullpath))
143738c8a9a5SSteve French 				return 0;
143838c8a9a5SSteve French 		} else if (ctx->leaf_fullpath) {
143938c8a9a5SSteve French 			return 0;
144038c8a9a5SSteve French 		}
144138c8a9a5SSteve French 	} else if (server->origin_fullpath || server->leaf_fullpath) {
144238c8a9a5SSteve French 		return 0;
144338c8a9a5SSteve French 	}
144438c8a9a5SSteve French 
144538c8a9a5SSteve French 	/*
144638c8a9a5SSteve French 	 * Match for a regular connection (address/hostname/port) which has no
144738c8a9a5SSteve French 	 * DFS referrals set.
144838c8a9a5SSteve French 	 */
144938c8a9a5SSteve French 	if (!server->origin_fullpath && !server->leaf_fullpath &&
145038c8a9a5SSteve French 	    (strcasecmp(server->hostname, ctx->server_hostname) ||
145138c8a9a5SSteve French 	     !match_server_address(server, addr) ||
145238c8a9a5SSteve French 	     !match_port(server, addr)))
145338c8a9a5SSteve French 		return 0;
145438c8a9a5SSteve French 
145538c8a9a5SSteve French 	if (!match_security(server, ctx))
145638c8a9a5SSteve French 		return 0;
145738c8a9a5SSteve French 
145838c8a9a5SSteve French 	if (server->echo_interval != ctx->echo_interval * HZ)
145938c8a9a5SSteve French 		return 0;
146038c8a9a5SSteve French 
146138c8a9a5SSteve French 	if (server->rdma != ctx->rdma)
146238c8a9a5SSteve French 		return 0;
146338c8a9a5SSteve French 
146438c8a9a5SSteve French 	if (server->ignore_signature != ctx->ignore_signature)
146538c8a9a5SSteve French 		return 0;
146638c8a9a5SSteve French 
146738c8a9a5SSteve French 	if (server->min_offload != ctx->min_offload)
146838c8a9a5SSteve French 		return 0;
146938c8a9a5SSteve French 
147038c8a9a5SSteve French 	return 1;
147138c8a9a5SSteve French }
147238c8a9a5SSteve French 
147338c8a9a5SSteve French struct TCP_Server_Info *
147438c8a9a5SSteve French cifs_find_tcp_session(struct smb3_fs_context *ctx)
147538c8a9a5SSteve French {
147638c8a9a5SSteve French 	struct TCP_Server_Info *server;
147738c8a9a5SSteve French 
147838c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
147938c8a9a5SSteve French 	list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
148038c8a9a5SSteve French 		spin_lock(&server->srv_lock);
148138c8a9a5SSteve French 		/*
148238c8a9a5SSteve French 		 * Skip ses channels since they're only handled in lower layers
148338c8a9a5SSteve French 		 * (e.g. cifs_send_recv).
148438c8a9a5SSteve French 		 */
148538c8a9a5SSteve French 		if (CIFS_SERVER_IS_CHAN(server) || !match_server(server, ctx)) {
148638c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
148738c8a9a5SSteve French 			continue;
148838c8a9a5SSteve French 		}
148938c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
149038c8a9a5SSteve French 
149138c8a9a5SSteve French 		++server->srv_count;
149238c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
149338c8a9a5SSteve French 		cifs_dbg(FYI, "Existing tcp session with server found\n");
149438c8a9a5SSteve French 		return server;
149538c8a9a5SSteve French 	}
149638c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
149738c8a9a5SSteve French 	return NULL;
149838c8a9a5SSteve French }
149938c8a9a5SSteve French 
150038c8a9a5SSteve French void
150138c8a9a5SSteve French cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
150238c8a9a5SSteve French {
150338c8a9a5SSteve French 	struct task_struct *task;
150438c8a9a5SSteve French 
150538c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
150638c8a9a5SSteve French 	if (--server->srv_count > 0) {
150738c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
150838c8a9a5SSteve French 		return;
150938c8a9a5SSteve French 	}
151038c8a9a5SSteve French 
151138c8a9a5SSteve French 	/* srv_count can never go negative */
151238c8a9a5SSteve French 	WARN_ON(server->srv_count < 0);
151338c8a9a5SSteve French 
151438c8a9a5SSteve French 	put_net(cifs_net_ns(server));
151538c8a9a5SSteve French 
151638c8a9a5SSteve French 	list_del_init(&server->tcp_ses_list);
151738c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
151838c8a9a5SSteve French 
151938c8a9a5SSteve French 	/* For secondary channels, we pick up ref-count on the primary server */
152038c8a9a5SSteve French 	if (CIFS_SERVER_IS_CHAN(server))
152138c8a9a5SSteve French 		cifs_put_tcp_session(server->primary_server, from_reconnect);
152238c8a9a5SSteve French 
152338c8a9a5SSteve French 	cancel_delayed_work_sync(&server->echo);
152438c8a9a5SSteve French 
152538c8a9a5SSteve French 	if (from_reconnect)
152638c8a9a5SSteve French 		/*
152738c8a9a5SSteve French 		 * Avoid deadlock here: reconnect work calls
152838c8a9a5SSteve French 		 * cifs_put_tcp_session() at its end. Need to be sure
152938c8a9a5SSteve French 		 * that reconnect work does nothing with server pointer after
153038c8a9a5SSteve French 		 * that step.
153138c8a9a5SSteve French 		 */
153238c8a9a5SSteve French 		cancel_delayed_work(&server->reconnect);
153338c8a9a5SSteve French 	else
153438c8a9a5SSteve French 		cancel_delayed_work_sync(&server->reconnect);
153538c8a9a5SSteve French 
153638c8a9a5SSteve French 	spin_lock(&server->srv_lock);
153738c8a9a5SSteve French 	server->tcpStatus = CifsExiting;
153838c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
153938c8a9a5SSteve French 
154038c8a9a5SSteve French 	cifs_crypto_secmech_release(server);
154138c8a9a5SSteve French 
154238c8a9a5SSteve French 	kfree_sensitive(server->session_key.response);
154338c8a9a5SSteve French 	server->session_key.response = NULL;
154438c8a9a5SSteve French 	server->session_key.len = 0;
154538c8a9a5SSteve French 	kfree(server->hostname);
154638c8a9a5SSteve French 	server->hostname = NULL;
154738c8a9a5SSteve French 
154838c8a9a5SSteve French 	task = xchg(&server->tsk, NULL);
154938c8a9a5SSteve French 	if (task)
155038c8a9a5SSteve French 		send_sig(SIGKILL, task, 1);
155138c8a9a5SSteve French }
155238c8a9a5SSteve French 
155338c8a9a5SSteve French struct TCP_Server_Info *
155438c8a9a5SSteve French cifs_get_tcp_session(struct smb3_fs_context *ctx,
155538c8a9a5SSteve French 		     struct TCP_Server_Info *primary_server)
155638c8a9a5SSteve French {
155738c8a9a5SSteve French 	struct TCP_Server_Info *tcp_ses = NULL;
155838c8a9a5SSteve French 	int rc;
155938c8a9a5SSteve French 
156038c8a9a5SSteve French 	cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
156138c8a9a5SSteve French 
156238c8a9a5SSteve French 	/* see if we already have a matching tcp_ses */
156338c8a9a5SSteve French 	tcp_ses = cifs_find_tcp_session(ctx);
156438c8a9a5SSteve French 	if (tcp_ses)
156538c8a9a5SSteve French 		return tcp_ses;
156638c8a9a5SSteve French 
156738c8a9a5SSteve French 	tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
156838c8a9a5SSteve French 	if (!tcp_ses) {
156938c8a9a5SSteve French 		rc = -ENOMEM;
157038c8a9a5SSteve French 		goto out_err;
157138c8a9a5SSteve French 	}
157238c8a9a5SSteve French 
157338c8a9a5SSteve French 	tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
157438c8a9a5SSteve French 	if (!tcp_ses->hostname) {
157538c8a9a5SSteve French 		rc = -ENOMEM;
157638c8a9a5SSteve French 		goto out_err;
157738c8a9a5SSteve French 	}
157838c8a9a5SSteve French 
157938c8a9a5SSteve French 	if (ctx->leaf_fullpath) {
158038c8a9a5SSteve French 		tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL);
158138c8a9a5SSteve French 		if (!tcp_ses->leaf_fullpath) {
158238c8a9a5SSteve French 			rc = -ENOMEM;
158338c8a9a5SSteve French 			goto out_err;
158438c8a9a5SSteve French 		}
158538c8a9a5SSteve French 	}
158638c8a9a5SSteve French 
158738c8a9a5SSteve French 	if (ctx->nosharesock)
158838c8a9a5SSteve French 		tcp_ses->nosharesock = true;
158938c8a9a5SSteve French 
159038c8a9a5SSteve French 	tcp_ses->ops = ctx->ops;
159138c8a9a5SSteve French 	tcp_ses->vals = ctx->vals;
159238c8a9a5SSteve French 	cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
159338c8a9a5SSteve French 
159438c8a9a5SSteve French 	tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
159538c8a9a5SSteve French 	tcp_ses->noblockcnt = ctx->rootfs;
159638c8a9a5SSteve French 	tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
159738c8a9a5SSteve French 	tcp_ses->noautotune = ctx->noautotune;
159838c8a9a5SSteve French 	tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
159938c8a9a5SSteve French 	tcp_ses->rdma = ctx->rdma;
160038c8a9a5SSteve French 	tcp_ses->in_flight = 0;
160138c8a9a5SSteve French 	tcp_ses->max_in_flight = 0;
160238c8a9a5SSteve French 	tcp_ses->credits = 1;
160338c8a9a5SSteve French 	if (primary_server) {
160438c8a9a5SSteve French 		spin_lock(&cifs_tcp_ses_lock);
160538c8a9a5SSteve French 		++primary_server->srv_count;
160638c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
160738c8a9a5SSteve French 		tcp_ses->primary_server = primary_server;
160838c8a9a5SSteve French 	}
160938c8a9a5SSteve French 	init_waitqueue_head(&tcp_ses->response_q);
161038c8a9a5SSteve French 	init_waitqueue_head(&tcp_ses->request_q);
161138c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
161238c8a9a5SSteve French 	mutex_init(&tcp_ses->_srv_mutex);
161338c8a9a5SSteve French 	memcpy(tcp_ses->workstation_RFC1001_name,
161438c8a9a5SSteve French 		ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
161538c8a9a5SSteve French 	memcpy(tcp_ses->server_RFC1001_name,
161638c8a9a5SSteve French 		ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
161738c8a9a5SSteve French 	tcp_ses->session_estab = false;
161838c8a9a5SSteve French 	tcp_ses->sequence_number = 0;
161938c8a9a5SSteve French 	tcp_ses->reconnect_instance = 1;
162038c8a9a5SSteve French 	tcp_ses->lstrp = jiffies;
162138c8a9a5SSteve French 	tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
162238c8a9a5SSteve French 	spin_lock_init(&tcp_ses->req_lock);
162338c8a9a5SSteve French 	spin_lock_init(&tcp_ses->srv_lock);
162438c8a9a5SSteve French 	spin_lock_init(&tcp_ses->mid_lock);
162538c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
162638c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
162738c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
162838c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
162938c8a9a5SSteve French 	mutex_init(&tcp_ses->reconnect_mutex);
163038c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
163138c8a9a5SSteve French 	mutex_init(&tcp_ses->refpath_lock);
163238c8a9a5SSteve French #endif
163338c8a9a5SSteve French 	memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
163438c8a9a5SSteve French 	       sizeof(tcp_ses->srcaddr));
163538c8a9a5SSteve French 	memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
163638c8a9a5SSteve French 		sizeof(tcp_ses->dstaddr));
163738c8a9a5SSteve French 	if (ctx->use_client_guid)
163838c8a9a5SSteve French 		memcpy(tcp_ses->client_guid, ctx->client_guid,
163938c8a9a5SSteve French 		       SMB2_CLIENT_GUID_SIZE);
164038c8a9a5SSteve French 	else
164138c8a9a5SSteve French 		generate_random_uuid(tcp_ses->client_guid);
164238c8a9a5SSteve French 	/*
164338c8a9a5SSteve French 	 * at this point we are the only ones with the pointer
164438c8a9a5SSteve French 	 * to the struct since the kernel thread not created yet
164538c8a9a5SSteve French 	 * no need to spinlock this init of tcpStatus or srv_count
164638c8a9a5SSteve French 	 */
164738c8a9a5SSteve French 	tcp_ses->tcpStatus = CifsNew;
164838c8a9a5SSteve French 	++tcp_ses->srv_count;
164938c8a9a5SSteve French 
165038c8a9a5SSteve French 	if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
165138c8a9a5SSteve French 		ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
165238c8a9a5SSteve French 		tcp_ses->echo_interval = ctx->echo_interval * HZ;
165338c8a9a5SSteve French 	else
165438c8a9a5SSteve French 		tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
165538c8a9a5SSteve French 	if (tcp_ses->rdma) {
165638c8a9a5SSteve French #ifndef CONFIG_CIFS_SMB_DIRECT
165738c8a9a5SSteve French 		cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
165838c8a9a5SSteve French 		rc = -ENOENT;
165938c8a9a5SSteve French 		goto out_err_crypto_release;
166038c8a9a5SSteve French #endif
166138c8a9a5SSteve French 		tcp_ses->smbd_conn = smbd_get_connection(
166238c8a9a5SSteve French 			tcp_ses, (struct sockaddr *)&ctx->dstaddr);
166338c8a9a5SSteve French 		if (tcp_ses->smbd_conn) {
166438c8a9a5SSteve French 			cifs_dbg(VFS, "RDMA transport established\n");
166538c8a9a5SSteve French 			rc = 0;
166638c8a9a5SSteve French 			goto smbd_connected;
166738c8a9a5SSteve French 		} else {
166838c8a9a5SSteve French 			rc = -ENOENT;
166938c8a9a5SSteve French 			goto out_err_crypto_release;
167038c8a9a5SSteve French 		}
167138c8a9a5SSteve French 	}
167238c8a9a5SSteve French 	rc = ip_connect(tcp_ses);
167338c8a9a5SSteve French 	if (rc < 0) {
167438c8a9a5SSteve French 		cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
167538c8a9a5SSteve French 		goto out_err_crypto_release;
167638c8a9a5SSteve French 	}
167738c8a9a5SSteve French smbd_connected:
167838c8a9a5SSteve French 	/*
167938c8a9a5SSteve French 	 * since we're in a cifs function already, we know that
168038c8a9a5SSteve French 	 * this will succeed. No need for try_module_get().
168138c8a9a5SSteve French 	 */
168238c8a9a5SSteve French 	__module_get(THIS_MODULE);
168338c8a9a5SSteve French 	tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
168438c8a9a5SSteve French 				  tcp_ses, "cifsd");
168538c8a9a5SSteve French 	if (IS_ERR(tcp_ses->tsk)) {
168638c8a9a5SSteve French 		rc = PTR_ERR(tcp_ses->tsk);
168738c8a9a5SSteve French 		cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
168838c8a9a5SSteve French 		module_put(THIS_MODULE);
168938c8a9a5SSteve French 		goto out_err_crypto_release;
169038c8a9a5SSteve French 	}
169138c8a9a5SSteve French 	tcp_ses->min_offload = ctx->min_offload;
169238c8a9a5SSteve French 	/*
169338c8a9a5SSteve French 	 * at this point we are the only ones with the pointer
169438c8a9a5SSteve French 	 * to the struct since the kernel thread not created yet
169538c8a9a5SSteve French 	 * no need to spinlock this update of tcpStatus
169638c8a9a5SSteve French 	 */
169738c8a9a5SSteve French 	spin_lock(&tcp_ses->srv_lock);
169838c8a9a5SSteve French 	tcp_ses->tcpStatus = CifsNeedNegotiate;
169938c8a9a5SSteve French 	spin_unlock(&tcp_ses->srv_lock);
170038c8a9a5SSteve French 
170138c8a9a5SSteve French 	if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
170238c8a9a5SSteve French 		tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
170338c8a9a5SSteve French 	else
170438c8a9a5SSteve French 		tcp_ses->max_credits = ctx->max_credits;
170538c8a9a5SSteve French 
170638c8a9a5SSteve French 	tcp_ses->nr_targets = 1;
170738c8a9a5SSteve French 	tcp_ses->ignore_signature = ctx->ignore_signature;
170838c8a9a5SSteve French 	/* thread spawned, put it on the list */
170938c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
171038c8a9a5SSteve French 	list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
171138c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
171238c8a9a5SSteve French 
171338c8a9a5SSteve French 	/* queue echo request delayed work */
171438c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
171538c8a9a5SSteve French 
171638c8a9a5SSteve French 	return tcp_ses;
171738c8a9a5SSteve French 
171838c8a9a5SSteve French out_err_crypto_release:
171938c8a9a5SSteve French 	cifs_crypto_secmech_release(tcp_ses);
172038c8a9a5SSteve French 
172138c8a9a5SSteve French 	put_net(cifs_net_ns(tcp_ses));
172238c8a9a5SSteve French 
172338c8a9a5SSteve French out_err:
172438c8a9a5SSteve French 	if (tcp_ses) {
172538c8a9a5SSteve French 		if (CIFS_SERVER_IS_CHAN(tcp_ses))
172638c8a9a5SSteve French 			cifs_put_tcp_session(tcp_ses->primary_server, false);
172738c8a9a5SSteve French 		kfree(tcp_ses->hostname);
172838c8a9a5SSteve French 		kfree(tcp_ses->leaf_fullpath);
172938c8a9a5SSteve French 		if (tcp_ses->ssocket)
173038c8a9a5SSteve French 			sock_release(tcp_ses->ssocket);
173138c8a9a5SSteve French 		kfree(tcp_ses);
173238c8a9a5SSteve French 	}
173338c8a9a5SSteve French 	return ERR_PTR(rc);
173438c8a9a5SSteve French }
173538c8a9a5SSteve French 
173638c8a9a5SSteve French /* this function must be called with ses_lock and chan_lock held */
173738c8a9a5SSteve French static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
173838c8a9a5SSteve French {
173938c8a9a5SSteve French 	if (ctx->sectype != Unspecified &&
174038c8a9a5SSteve French 	    ctx->sectype != ses->sectype)
174138c8a9a5SSteve French 		return 0;
174238c8a9a5SSteve French 
174338c8a9a5SSteve French 	/*
174438c8a9a5SSteve French 	 * If an existing session is limited to less channels than
174538c8a9a5SSteve French 	 * requested, it should not be reused
174638c8a9a5SSteve French 	 */
174738c8a9a5SSteve French 	if (ses->chan_max < ctx->max_channels)
174838c8a9a5SSteve French 		return 0;
174938c8a9a5SSteve French 
175038c8a9a5SSteve French 	switch (ses->sectype) {
175138c8a9a5SSteve French 	case Kerberos:
175238c8a9a5SSteve French 		if (!uid_eq(ctx->cred_uid, ses->cred_uid))
175338c8a9a5SSteve French 			return 0;
175438c8a9a5SSteve French 		break;
175538c8a9a5SSteve French 	default:
175638c8a9a5SSteve French 		/* NULL username means anonymous session */
175738c8a9a5SSteve French 		if (ses->user_name == NULL) {
175838c8a9a5SSteve French 			if (!ctx->nullauth)
175938c8a9a5SSteve French 				return 0;
176038c8a9a5SSteve French 			break;
176138c8a9a5SSteve French 		}
176238c8a9a5SSteve French 
176338c8a9a5SSteve French 		/* anything else takes username/password */
176438c8a9a5SSteve French 		if (strncmp(ses->user_name,
176538c8a9a5SSteve French 			    ctx->username ? ctx->username : "",
176638c8a9a5SSteve French 			    CIFS_MAX_USERNAME_LEN))
176738c8a9a5SSteve French 			return 0;
176838c8a9a5SSteve French 		if ((ctx->username && strlen(ctx->username) != 0) &&
176938c8a9a5SSteve French 		    ses->password != NULL &&
177038c8a9a5SSteve French 		    strncmp(ses->password,
177138c8a9a5SSteve French 			    ctx->password ? ctx->password : "",
177238c8a9a5SSteve French 			    CIFS_MAX_PASSWORD_LEN))
177338c8a9a5SSteve French 			return 0;
177438c8a9a5SSteve French 	}
177538c8a9a5SSteve French 	return 1;
177638c8a9a5SSteve French }
177738c8a9a5SSteve French 
177838c8a9a5SSteve French /**
177938c8a9a5SSteve French  * cifs_setup_ipc - helper to setup the IPC tcon for the session
178038c8a9a5SSteve French  * @ses: smb session to issue the request on
178138c8a9a5SSteve French  * @ctx: the superblock configuration context to use for building the
178238c8a9a5SSteve French  *       new tree connection for the IPC (interprocess communication RPC)
178338c8a9a5SSteve French  *
178438c8a9a5SSteve French  * A new IPC connection is made and stored in the session
178538c8a9a5SSteve French  * tcon_ipc. The IPC tcon has the same lifetime as the session.
178638c8a9a5SSteve French  */
178738c8a9a5SSteve French static int
178838c8a9a5SSteve French cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
178938c8a9a5SSteve French {
179038c8a9a5SSteve French 	int rc = 0, xid;
179138c8a9a5SSteve French 	struct cifs_tcon *tcon;
179238c8a9a5SSteve French 	char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
179338c8a9a5SSteve French 	bool seal = false;
179438c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
179538c8a9a5SSteve French 
179638c8a9a5SSteve French 	/*
179738c8a9a5SSteve French 	 * If the mount request that resulted in the creation of the
179838c8a9a5SSteve French 	 * session requires encryption, force IPC to be encrypted too.
179938c8a9a5SSteve French 	 */
180038c8a9a5SSteve French 	if (ctx->seal) {
180138c8a9a5SSteve French 		if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
180238c8a9a5SSteve French 			seal = true;
180338c8a9a5SSteve French 		else {
180438c8a9a5SSteve French 			cifs_server_dbg(VFS,
180538c8a9a5SSteve French 				 "IPC: server doesn't support encryption\n");
180638c8a9a5SSteve French 			return -EOPNOTSUPP;
180738c8a9a5SSteve French 		}
180838c8a9a5SSteve French 	}
180938c8a9a5SSteve French 
181038c8a9a5SSteve French 	tcon = tconInfoAlloc();
181138c8a9a5SSteve French 	if (tcon == NULL)
181238c8a9a5SSteve French 		return -ENOMEM;
181338c8a9a5SSteve French 
181438c8a9a5SSteve French 	spin_lock(&server->srv_lock);
181538c8a9a5SSteve French 	scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
181638c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
181738c8a9a5SSteve French 
181838c8a9a5SSteve French 	xid = get_xid();
181938c8a9a5SSteve French 	tcon->ses = ses;
182038c8a9a5SSteve French 	tcon->ipc = true;
182138c8a9a5SSteve French 	tcon->seal = seal;
182238c8a9a5SSteve French 	rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
182338c8a9a5SSteve French 	free_xid(xid);
182438c8a9a5SSteve French 
182538c8a9a5SSteve French 	if (rc) {
182638c8a9a5SSteve French 		cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
182738c8a9a5SSteve French 		tconInfoFree(tcon);
182838c8a9a5SSteve French 		goto out;
182938c8a9a5SSteve French 	}
183038c8a9a5SSteve French 
183138c8a9a5SSteve French 	cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid);
183238c8a9a5SSteve French 
183338c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
183438c8a9a5SSteve French 	tcon->status = TID_GOOD;
183538c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
183638c8a9a5SSteve French 	ses->tcon_ipc = tcon;
183738c8a9a5SSteve French out:
183838c8a9a5SSteve French 	return rc;
183938c8a9a5SSteve French }
184038c8a9a5SSteve French 
184138c8a9a5SSteve French /**
184238c8a9a5SSteve French  * cifs_free_ipc - helper to release the session IPC tcon
184338c8a9a5SSteve French  * @ses: smb session to unmount the IPC from
184438c8a9a5SSteve French  *
184538c8a9a5SSteve French  * Needs to be called everytime a session is destroyed.
184638c8a9a5SSteve French  *
184738c8a9a5SSteve French  * On session close, the IPC is closed and the server must release all tcons of the session.
184838c8a9a5SSteve French  * No need to send a tree disconnect here.
184938c8a9a5SSteve French  *
185038c8a9a5SSteve French  * Besides, it will make the server to not close durable and resilient files on session close, as
185138c8a9a5SSteve French  * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
185238c8a9a5SSteve French  */
185338c8a9a5SSteve French static int
185438c8a9a5SSteve French cifs_free_ipc(struct cifs_ses *ses)
185538c8a9a5SSteve French {
185638c8a9a5SSteve French 	struct cifs_tcon *tcon = ses->tcon_ipc;
185738c8a9a5SSteve French 
185838c8a9a5SSteve French 	if (tcon == NULL)
185938c8a9a5SSteve French 		return 0;
186038c8a9a5SSteve French 
186138c8a9a5SSteve French 	tconInfoFree(tcon);
186238c8a9a5SSteve French 	ses->tcon_ipc = NULL;
186338c8a9a5SSteve French 	return 0;
186438c8a9a5SSteve French }
186538c8a9a5SSteve French 
186638c8a9a5SSteve French static struct cifs_ses *
186738c8a9a5SSteve French cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
186838c8a9a5SSteve French {
186938c8a9a5SSteve French 	struct cifs_ses *ses, *ret = NULL;
187038c8a9a5SSteve French 
187138c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
187238c8a9a5SSteve French 	list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
187338c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
187438c8a9a5SSteve French 		if (ses->ses_status == SES_EXITING) {
187538c8a9a5SSteve French 			spin_unlock(&ses->ses_lock);
187638c8a9a5SSteve French 			continue;
187738c8a9a5SSteve French 		}
187838c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
187938c8a9a5SSteve French 		if (match_session(ses, ctx)) {
188038c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
188138c8a9a5SSteve French 			spin_unlock(&ses->ses_lock);
188238c8a9a5SSteve French 			ret = ses;
188338c8a9a5SSteve French 			break;
188438c8a9a5SSteve French 		}
188538c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
188638c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
188738c8a9a5SSteve French 	}
188838c8a9a5SSteve French 	if (ret)
188938c8a9a5SSteve French 		cifs_smb_ses_inc_refcount(ret);
189038c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
189138c8a9a5SSteve French 	return ret;
189238c8a9a5SSteve French }
189338c8a9a5SSteve French 
189438c8a9a5SSteve French void __cifs_put_smb_ses(struct cifs_ses *ses)
189538c8a9a5SSteve French {
189638c8a9a5SSteve French 	unsigned int rc, xid;
189738c8a9a5SSteve French 	unsigned int chan_count;
189838c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
189938c8a9a5SSteve French 
190038c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
190138c8a9a5SSteve French 	if (ses->ses_status == SES_EXITING) {
190238c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
190338c8a9a5SSteve French 		return;
190438c8a9a5SSteve French 	}
190538c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
190638c8a9a5SSteve French 
190738c8a9a5SSteve French 	cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
190838c8a9a5SSteve French 	cifs_dbg(FYI,
190938c8a9a5SSteve French 		 "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->tree_name : "NONE");
191038c8a9a5SSteve French 
191138c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
191238c8a9a5SSteve French 	if (--ses->ses_count > 0) {
191338c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
191438c8a9a5SSteve French 		return;
191538c8a9a5SSteve French 	}
191638c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
191738c8a9a5SSteve French 
191838c8a9a5SSteve French 	/* ses_count can never go negative */
191938c8a9a5SSteve French 	WARN_ON(ses->ses_count < 0);
192038c8a9a5SSteve French 
192138c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
192238c8a9a5SSteve French 	if (ses->ses_status == SES_GOOD)
192338c8a9a5SSteve French 		ses->ses_status = SES_EXITING;
192438c8a9a5SSteve French 
192538c8a9a5SSteve French 	if (ses->ses_status == SES_EXITING && server->ops->logoff) {
192638c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
192738c8a9a5SSteve French 		cifs_free_ipc(ses);
192838c8a9a5SSteve French 		xid = get_xid();
192938c8a9a5SSteve French 		rc = server->ops->logoff(xid, ses);
193038c8a9a5SSteve French 		if (rc)
193138c8a9a5SSteve French 			cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
193238c8a9a5SSteve French 				__func__, rc);
193338c8a9a5SSteve French 		_free_xid(xid);
193438c8a9a5SSteve French 	} else {
193538c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
193638c8a9a5SSteve French 		cifs_free_ipc(ses);
193738c8a9a5SSteve French 	}
193838c8a9a5SSteve French 
193938c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
194038c8a9a5SSteve French 	list_del_init(&ses->smb_ses_list);
194138c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
194238c8a9a5SSteve French 
194338c8a9a5SSteve French 	chan_count = ses->chan_count;
194438c8a9a5SSteve French 
194538c8a9a5SSteve French 	/* close any extra channels */
194638c8a9a5SSteve French 	if (chan_count > 1) {
194738c8a9a5SSteve French 		int i;
194838c8a9a5SSteve French 
194938c8a9a5SSteve French 		for (i = 1; i < chan_count; i++) {
195038c8a9a5SSteve French 			if (ses->chans[i].iface) {
195138c8a9a5SSteve French 				kref_put(&ses->chans[i].iface->refcount, release_iface);
195238c8a9a5SSteve French 				ses->chans[i].iface = NULL;
195338c8a9a5SSteve French 			}
195438c8a9a5SSteve French 			cifs_put_tcp_session(ses->chans[i].server, 0);
195538c8a9a5SSteve French 			ses->chans[i].server = NULL;
195638c8a9a5SSteve French 		}
195738c8a9a5SSteve French 	}
195838c8a9a5SSteve French 
195938c8a9a5SSteve French 	sesInfoFree(ses);
196038c8a9a5SSteve French 	cifs_put_tcp_session(server, 0);
196138c8a9a5SSteve French }
196238c8a9a5SSteve French 
196338c8a9a5SSteve French #ifdef CONFIG_KEYS
196438c8a9a5SSteve French 
196538c8a9a5SSteve French /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
196638c8a9a5SSteve French #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
196738c8a9a5SSteve French 
196838c8a9a5SSteve French /* Populate username and pw fields from keyring if possible */
196938c8a9a5SSteve French static int
197038c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
197138c8a9a5SSteve French {
197238c8a9a5SSteve French 	int rc = 0;
197338c8a9a5SSteve French 	int is_domain = 0;
197438c8a9a5SSteve French 	const char *delim, *payload;
197538c8a9a5SSteve French 	char *desc;
197638c8a9a5SSteve French 	ssize_t len;
197738c8a9a5SSteve French 	struct key *key;
197838c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
197938c8a9a5SSteve French 	struct sockaddr_in *sa;
198038c8a9a5SSteve French 	struct sockaddr_in6 *sa6;
198138c8a9a5SSteve French 	const struct user_key_payload *upayload;
198238c8a9a5SSteve French 
198338c8a9a5SSteve French 	desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
198438c8a9a5SSteve French 	if (!desc)
198538c8a9a5SSteve French 		return -ENOMEM;
198638c8a9a5SSteve French 
198738c8a9a5SSteve French 	/* try to find an address key first */
198838c8a9a5SSteve French 	switch (server->dstaddr.ss_family) {
198938c8a9a5SSteve French 	case AF_INET:
199038c8a9a5SSteve French 		sa = (struct sockaddr_in *)&server->dstaddr;
199138c8a9a5SSteve French 		sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
199238c8a9a5SSteve French 		break;
199338c8a9a5SSteve French 	case AF_INET6:
199438c8a9a5SSteve French 		sa6 = (struct sockaddr_in6 *)&server->dstaddr;
199538c8a9a5SSteve French 		sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
199638c8a9a5SSteve French 		break;
199738c8a9a5SSteve French 	default:
199838c8a9a5SSteve French 		cifs_dbg(FYI, "Bad ss_family (%hu)\n",
199938c8a9a5SSteve French 			 server->dstaddr.ss_family);
200038c8a9a5SSteve French 		rc = -EINVAL;
200138c8a9a5SSteve French 		goto out_err;
200238c8a9a5SSteve French 	}
200338c8a9a5SSteve French 
200438c8a9a5SSteve French 	cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
200538c8a9a5SSteve French 	key = request_key(&key_type_logon, desc, "");
200638c8a9a5SSteve French 	if (IS_ERR(key)) {
200738c8a9a5SSteve French 		if (!ses->domainName) {
200838c8a9a5SSteve French 			cifs_dbg(FYI, "domainName is NULL\n");
200938c8a9a5SSteve French 			rc = PTR_ERR(key);
201038c8a9a5SSteve French 			goto out_err;
201138c8a9a5SSteve French 		}
201238c8a9a5SSteve French 
201338c8a9a5SSteve French 		/* didn't work, try to find a domain key */
201438c8a9a5SSteve French 		sprintf(desc, "cifs:d:%s", ses->domainName);
201538c8a9a5SSteve French 		cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
201638c8a9a5SSteve French 		key = request_key(&key_type_logon, desc, "");
201738c8a9a5SSteve French 		if (IS_ERR(key)) {
201838c8a9a5SSteve French 			rc = PTR_ERR(key);
201938c8a9a5SSteve French 			goto out_err;
202038c8a9a5SSteve French 		}
202138c8a9a5SSteve French 		is_domain = 1;
202238c8a9a5SSteve French 	}
202338c8a9a5SSteve French 
202438c8a9a5SSteve French 	down_read(&key->sem);
202538c8a9a5SSteve French 	upayload = user_key_payload_locked(key);
202638c8a9a5SSteve French 	if (IS_ERR_OR_NULL(upayload)) {
202738c8a9a5SSteve French 		rc = upayload ? PTR_ERR(upayload) : -EINVAL;
202838c8a9a5SSteve French 		goto out_key_put;
202938c8a9a5SSteve French 	}
203038c8a9a5SSteve French 
203138c8a9a5SSteve French 	/* find first : in payload */
203238c8a9a5SSteve French 	payload = upayload->data;
203338c8a9a5SSteve French 	delim = strnchr(payload, upayload->datalen, ':');
203438c8a9a5SSteve French 	cifs_dbg(FYI, "payload=%s\n", payload);
203538c8a9a5SSteve French 	if (!delim) {
203638c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
203738c8a9a5SSteve French 			 upayload->datalen);
203838c8a9a5SSteve French 		rc = -EINVAL;
203938c8a9a5SSteve French 		goto out_key_put;
204038c8a9a5SSteve French 	}
204138c8a9a5SSteve French 
204238c8a9a5SSteve French 	len = delim - payload;
204338c8a9a5SSteve French 	if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
204438c8a9a5SSteve French 		cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
204538c8a9a5SSteve French 			 len);
204638c8a9a5SSteve French 		rc = -EINVAL;
204738c8a9a5SSteve French 		goto out_key_put;
204838c8a9a5SSteve French 	}
204938c8a9a5SSteve French 
205038c8a9a5SSteve French 	ctx->username = kstrndup(payload, len, GFP_KERNEL);
205138c8a9a5SSteve French 	if (!ctx->username) {
205238c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
205338c8a9a5SSteve French 			 len);
205438c8a9a5SSteve French 		rc = -ENOMEM;
205538c8a9a5SSteve French 		goto out_key_put;
205638c8a9a5SSteve French 	}
205738c8a9a5SSteve French 	cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
205838c8a9a5SSteve French 
205938c8a9a5SSteve French 	len = key->datalen - (len + 1);
206038c8a9a5SSteve French 	if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
206138c8a9a5SSteve French 		cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
206238c8a9a5SSteve French 		rc = -EINVAL;
206338c8a9a5SSteve French 		kfree(ctx->username);
206438c8a9a5SSteve French 		ctx->username = NULL;
206538c8a9a5SSteve French 		goto out_key_put;
206638c8a9a5SSteve French 	}
206738c8a9a5SSteve French 
206838c8a9a5SSteve French 	++delim;
206938c8a9a5SSteve French 	ctx->password = kstrndup(delim, len, GFP_KERNEL);
207038c8a9a5SSteve French 	if (!ctx->password) {
207138c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
207238c8a9a5SSteve French 			 len);
207338c8a9a5SSteve French 		rc = -ENOMEM;
207438c8a9a5SSteve French 		kfree(ctx->username);
207538c8a9a5SSteve French 		ctx->username = NULL;
207638c8a9a5SSteve French 		goto out_key_put;
207738c8a9a5SSteve French 	}
207838c8a9a5SSteve French 
207938c8a9a5SSteve French 	/*
208038c8a9a5SSteve French 	 * If we have a domain key then we must set the domainName in the
208138c8a9a5SSteve French 	 * for the request.
208238c8a9a5SSteve French 	 */
208338c8a9a5SSteve French 	if (is_domain && ses->domainName) {
208438c8a9a5SSteve French 		ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
208538c8a9a5SSteve French 		if (!ctx->domainname) {
208638c8a9a5SSteve French 			cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
208738c8a9a5SSteve French 				 len);
208838c8a9a5SSteve French 			rc = -ENOMEM;
208938c8a9a5SSteve French 			kfree(ctx->username);
209038c8a9a5SSteve French 			ctx->username = NULL;
209138c8a9a5SSteve French 			kfree_sensitive(ctx->password);
209238c8a9a5SSteve French 			ctx->password = NULL;
209338c8a9a5SSteve French 			goto out_key_put;
209438c8a9a5SSteve French 		}
209538c8a9a5SSteve French 	}
209638c8a9a5SSteve French 
209738c8a9a5SSteve French 	strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name));
209838c8a9a5SSteve French 
209938c8a9a5SSteve French out_key_put:
210038c8a9a5SSteve French 	up_read(&key->sem);
210138c8a9a5SSteve French 	key_put(key);
210238c8a9a5SSteve French out_err:
210338c8a9a5SSteve French 	kfree(desc);
210438c8a9a5SSteve French 	cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
210538c8a9a5SSteve French 	return rc;
210638c8a9a5SSteve French }
210738c8a9a5SSteve French #else /* ! CONFIG_KEYS */
210838c8a9a5SSteve French static inline int
210938c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
211038c8a9a5SSteve French 		   struct cifs_ses *ses __attribute__((unused)))
211138c8a9a5SSteve French {
211238c8a9a5SSteve French 	return -ENOSYS;
211338c8a9a5SSteve French }
211438c8a9a5SSteve French #endif /* CONFIG_KEYS */
211538c8a9a5SSteve French 
211638c8a9a5SSteve French /**
211738c8a9a5SSteve French  * cifs_get_smb_ses - get a session matching @ctx data from @server
211838c8a9a5SSteve French  * @server: server to setup the session to
211938c8a9a5SSteve French  * @ctx: superblock configuration context to use to setup the session
212038c8a9a5SSteve French  *
212138c8a9a5SSteve French  * This function assumes it is being called from cifs_mount() where we
212238c8a9a5SSteve French  * already got a server reference (server refcount +1). See
212338c8a9a5SSteve French  * cifs_get_tcon() for refcount explanations.
212438c8a9a5SSteve French  */
212538c8a9a5SSteve French struct cifs_ses *
212638c8a9a5SSteve French cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
212738c8a9a5SSteve French {
212838c8a9a5SSteve French 	int rc = 0;
212938c8a9a5SSteve French 	unsigned int xid;
213038c8a9a5SSteve French 	struct cifs_ses *ses;
213138c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
213238c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
213338c8a9a5SSteve French 
213438c8a9a5SSteve French 	xid = get_xid();
213538c8a9a5SSteve French 
213638c8a9a5SSteve French 	ses = cifs_find_smb_ses(server, ctx);
213738c8a9a5SSteve French 	if (ses) {
213838c8a9a5SSteve French 		cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
213938c8a9a5SSteve French 			 ses->ses_status);
214038c8a9a5SSteve French 
214138c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
214238c8a9a5SSteve French 		if (cifs_chan_needs_reconnect(ses, server)) {
214338c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
214438c8a9a5SSteve French 			cifs_dbg(FYI, "Session needs reconnect\n");
214538c8a9a5SSteve French 
214638c8a9a5SSteve French 			mutex_lock(&ses->session_mutex);
214738c8a9a5SSteve French 			rc = cifs_negotiate_protocol(xid, ses, server);
214838c8a9a5SSteve French 			if (rc) {
214938c8a9a5SSteve French 				mutex_unlock(&ses->session_mutex);
215038c8a9a5SSteve French 				/* problem -- put our ses reference */
215138c8a9a5SSteve French 				cifs_put_smb_ses(ses);
215238c8a9a5SSteve French 				free_xid(xid);
215338c8a9a5SSteve French 				return ERR_PTR(rc);
215438c8a9a5SSteve French 			}
215538c8a9a5SSteve French 
215638c8a9a5SSteve French 			rc = cifs_setup_session(xid, ses, server,
215738c8a9a5SSteve French 						ctx->local_nls);
215838c8a9a5SSteve French 			if (rc) {
215938c8a9a5SSteve French 				mutex_unlock(&ses->session_mutex);
216038c8a9a5SSteve French 				/* problem -- put our reference */
216138c8a9a5SSteve French 				cifs_put_smb_ses(ses);
216238c8a9a5SSteve French 				free_xid(xid);
216338c8a9a5SSteve French 				return ERR_PTR(rc);
216438c8a9a5SSteve French 			}
216538c8a9a5SSteve French 			mutex_unlock(&ses->session_mutex);
216638c8a9a5SSteve French 
216738c8a9a5SSteve French 			spin_lock(&ses->chan_lock);
216838c8a9a5SSteve French 		}
216938c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
217038c8a9a5SSteve French 
217138c8a9a5SSteve French 		/* existing SMB ses has a server reference already */
217238c8a9a5SSteve French 		cifs_put_tcp_session(server, 0);
217338c8a9a5SSteve French 		free_xid(xid);
217438c8a9a5SSteve French 		return ses;
217538c8a9a5SSteve French 	}
217638c8a9a5SSteve French 
217738c8a9a5SSteve French 	rc = -ENOMEM;
217838c8a9a5SSteve French 
217938c8a9a5SSteve French 	cifs_dbg(FYI, "Existing smb sess not found\n");
218038c8a9a5SSteve French 	ses = sesInfoAlloc();
218138c8a9a5SSteve French 	if (ses == NULL)
218238c8a9a5SSteve French 		goto get_ses_fail;
218338c8a9a5SSteve French 
218438c8a9a5SSteve French 	/* new SMB session uses our server ref */
218538c8a9a5SSteve French 	ses->server = server;
218638c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6)
218738c8a9a5SSteve French 		sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
218838c8a9a5SSteve French 	else
218938c8a9a5SSteve French 		sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
219038c8a9a5SSteve French 
219138c8a9a5SSteve French 	if (ctx->username) {
219238c8a9a5SSteve French 		ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
219338c8a9a5SSteve French 		if (!ses->user_name)
219438c8a9a5SSteve French 			goto get_ses_fail;
219538c8a9a5SSteve French 	}
219638c8a9a5SSteve French 
219738c8a9a5SSteve French 	/* ctx->password freed at unmount */
219838c8a9a5SSteve French 	if (ctx->password) {
219938c8a9a5SSteve French 		ses->password = kstrdup(ctx->password, GFP_KERNEL);
220038c8a9a5SSteve French 		if (!ses->password)
220138c8a9a5SSteve French 			goto get_ses_fail;
220238c8a9a5SSteve French 	}
220338c8a9a5SSteve French 	if (ctx->domainname) {
220438c8a9a5SSteve French 		ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
220538c8a9a5SSteve French 		if (!ses->domainName)
220638c8a9a5SSteve French 			goto get_ses_fail;
220738c8a9a5SSteve French 	}
220838c8a9a5SSteve French 
220938c8a9a5SSteve French 	strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name));
221038c8a9a5SSteve French 
221138c8a9a5SSteve French 	if (ctx->domainauto)
221238c8a9a5SSteve French 		ses->domainAuto = ctx->domainauto;
221338c8a9a5SSteve French 	ses->cred_uid = ctx->cred_uid;
221438c8a9a5SSteve French 	ses->linux_uid = ctx->linux_uid;
221538c8a9a5SSteve French 
221638c8a9a5SSteve French 	ses->sectype = ctx->sectype;
221738c8a9a5SSteve French 	ses->sign = ctx->sign;
221838c8a9a5SSteve French 
221938c8a9a5SSteve French 	/* add server as first channel */
222038c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
222138c8a9a5SSteve French 	ses->chans[0].server = server;
222238c8a9a5SSteve French 	ses->chan_count = 1;
222338c8a9a5SSteve French 	ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
222438c8a9a5SSteve French 	ses->chans_need_reconnect = 1;
222538c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
222638c8a9a5SSteve French 
222738c8a9a5SSteve French 	mutex_lock(&ses->session_mutex);
222838c8a9a5SSteve French 	rc = cifs_negotiate_protocol(xid, ses, server);
222938c8a9a5SSteve French 	if (!rc)
223038c8a9a5SSteve French 		rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
223138c8a9a5SSteve French 	mutex_unlock(&ses->session_mutex);
223238c8a9a5SSteve French 
223338c8a9a5SSteve French 	/* each channel uses a different signing key */
223438c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
223538c8a9a5SSteve French 	memcpy(ses->chans[0].signkey, ses->smb3signingkey,
223638c8a9a5SSteve French 	       sizeof(ses->smb3signingkey));
223738c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
223838c8a9a5SSteve French 
223938c8a9a5SSteve French 	if (rc)
224038c8a9a5SSteve French 		goto get_ses_fail;
224138c8a9a5SSteve French 
224238c8a9a5SSteve French 	/*
224338c8a9a5SSteve French 	 * success, put it on the list and add it as first channel
224438c8a9a5SSteve French 	 * note: the session becomes active soon after this. So you'll
224538c8a9a5SSteve French 	 * need to lock before changing something in the session.
224638c8a9a5SSteve French 	 */
224738c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
224838c8a9a5SSteve French 	ses->dfs_root_ses = ctx->dfs_root_ses;
224938c8a9a5SSteve French 	if (ses->dfs_root_ses)
225038c8a9a5SSteve French 		ses->dfs_root_ses->ses_count++;
225138c8a9a5SSteve French 	list_add(&ses->smb_ses_list, &server->smb_ses_list);
225238c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
225338c8a9a5SSteve French 
225438c8a9a5SSteve French 	cifs_setup_ipc(ses, ctx);
225538c8a9a5SSteve French 
225638c8a9a5SSteve French 	free_xid(xid);
225738c8a9a5SSteve French 
225838c8a9a5SSteve French 	return ses;
225938c8a9a5SSteve French 
226038c8a9a5SSteve French get_ses_fail:
226138c8a9a5SSteve French 	sesInfoFree(ses);
226238c8a9a5SSteve French 	free_xid(xid);
226338c8a9a5SSteve French 	return ERR_PTR(rc);
226438c8a9a5SSteve French }
226538c8a9a5SSteve French 
226638c8a9a5SSteve French /* this function must be called with tc_lock held */
226738c8a9a5SSteve French static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
226838c8a9a5SSteve French {
226938c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
227038c8a9a5SSteve French 
227138c8a9a5SSteve French 	if (tcon->status == TID_EXITING)
227238c8a9a5SSteve French 		return 0;
227338c8a9a5SSteve French 	/* Skip UNC validation when matching DFS connections or superblocks */
227438c8a9a5SSteve French 	if (!server->origin_fullpath && !server->leaf_fullpath &&
227538c8a9a5SSteve French 	    strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE))
227638c8a9a5SSteve French 		return 0;
227738c8a9a5SSteve French 	if (tcon->seal != ctx->seal)
227838c8a9a5SSteve French 		return 0;
227938c8a9a5SSteve French 	if (tcon->snapshot_time != ctx->snapshot_time)
228038c8a9a5SSteve French 		return 0;
228138c8a9a5SSteve French 	if (tcon->handle_timeout != ctx->handle_timeout)
228238c8a9a5SSteve French 		return 0;
228338c8a9a5SSteve French 	if (tcon->no_lease != ctx->no_lease)
228438c8a9a5SSteve French 		return 0;
228538c8a9a5SSteve French 	if (tcon->nodelete != ctx->nodelete)
228638c8a9a5SSteve French 		return 0;
228738c8a9a5SSteve French 	return 1;
228838c8a9a5SSteve French }
228938c8a9a5SSteve French 
229038c8a9a5SSteve French static struct cifs_tcon *
229138c8a9a5SSteve French cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
229238c8a9a5SSteve French {
229338c8a9a5SSteve French 	struct cifs_tcon *tcon;
229438c8a9a5SSteve French 
229538c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
229638c8a9a5SSteve French 	list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
229738c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
229838c8a9a5SSteve French 		if (!match_tcon(tcon, ctx)) {
229938c8a9a5SSteve French 			spin_unlock(&tcon->tc_lock);
230038c8a9a5SSteve French 			continue;
230138c8a9a5SSteve French 		}
230238c8a9a5SSteve French 		++tcon->tc_count;
230338c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
230438c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
230538c8a9a5SSteve French 		return tcon;
230638c8a9a5SSteve French 	}
230738c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
230838c8a9a5SSteve French 	return NULL;
230938c8a9a5SSteve French }
231038c8a9a5SSteve French 
231138c8a9a5SSteve French void
231238c8a9a5SSteve French cifs_put_tcon(struct cifs_tcon *tcon)
231338c8a9a5SSteve French {
231438c8a9a5SSteve French 	unsigned int xid;
231538c8a9a5SSteve French 	struct cifs_ses *ses;
231638c8a9a5SSteve French 
231738c8a9a5SSteve French 	/*
231838c8a9a5SSteve French 	 * IPC tcon share the lifetime of their session and are
231938c8a9a5SSteve French 	 * destroyed in the session put function
232038c8a9a5SSteve French 	 */
232138c8a9a5SSteve French 	if (tcon == NULL || tcon->ipc)
232238c8a9a5SSteve French 		return;
232338c8a9a5SSteve French 
232438c8a9a5SSteve French 	ses = tcon->ses;
232538c8a9a5SSteve French 	cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
232638c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
232738c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
232838c8a9a5SSteve French 	if (--tcon->tc_count > 0) {
232938c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
233038c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
233138c8a9a5SSteve French 		return;
233238c8a9a5SSteve French 	}
233338c8a9a5SSteve French 
233438c8a9a5SSteve French 	/* tc_count can never go negative */
233538c8a9a5SSteve French 	WARN_ON(tcon->tc_count < 0);
233638c8a9a5SSteve French 
233738c8a9a5SSteve French 	list_del_init(&tcon->tcon_list);
233838c8a9a5SSteve French 	tcon->status = TID_EXITING;
233938c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
234038c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
234138c8a9a5SSteve French 
234238c8a9a5SSteve French 	/* cancel polling of interfaces */
234338c8a9a5SSteve French 	cancel_delayed_work_sync(&tcon->query_interfaces);
234438c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
234538c8a9a5SSteve French 	cancel_delayed_work_sync(&tcon->dfs_cache_work);
234638c8a9a5SSteve French #endif
234738c8a9a5SSteve French 
234838c8a9a5SSteve French 	if (tcon->use_witness) {
234938c8a9a5SSteve French 		int rc;
235038c8a9a5SSteve French 
235138c8a9a5SSteve French 		rc = cifs_swn_unregister(tcon);
235238c8a9a5SSteve French 		if (rc < 0) {
235338c8a9a5SSteve French 			cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
235438c8a9a5SSteve French 					__func__, rc);
235538c8a9a5SSteve French 		}
235638c8a9a5SSteve French 	}
235738c8a9a5SSteve French 
235838c8a9a5SSteve French 	xid = get_xid();
235938c8a9a5SSteve French 	if (ses->server->ops->tree_disconnect)
236038c8a9a5SSteve French 		ses->server->ops->tree_disconnect(xid, tcon);
236138c8a9a5SSteve French 	_free_xid(xid);
236238c8a9a5SSteve French 
236338c8a9a5SSteve French 	cifs_fscache_release_super_cookie(tcon);
236438c8a9a5SSteve French 	tconInfoFree(tcon);
236538c8a9a5SSteve French 	cifs_put_smb_ses(ses);
236638c8a9a5SSteve French }
236738c8a9a5SSteve French 
236838c8a9a5SSteve French /**
236938c8a9a5SSteve French  * cifs_get_tcon - get a tcon matching @ctx data from @ses
237038c8a9a5SSteve French  * @ses: smb session to issue the request on
237138c8a9a5SSteve French  * @ctx: the superblock configuration context to use for building the
237238c8a9a5SSteve French  *
237338c8a9a5SSteve French  * - tcon refcount is the number of mount points using the tcon.
237438c8a9a5SSteve French  * - ses refcount is the number of tcon using the session.
237538c8a9a5SSteve French  *
237638c8a9a5SSteve French  * 1. This function assumes it is being called from cifs_mount() where
237738c8a9a5SSteve French  *    we already got a session reference (ses refcount +1).
237838c8a9a5SSteve French  *
237938c8a9a5SSteve French  * 2. Since we're in the context of adding a mount point, the end
238038c8a9a5SSteve French  *    result should be either:
238138c8a9a5SSteve French  *
238238c8a9a5SSteve French  * a) a new tcon already allocated with refcount=1 (1 mount point) and
238338c8a9a5SSteve French  *    its session refcount incremented (1 new tcon). This +1 was
238438c8a9a5SSteve French  *    already done in (1).
238538c8a9a5SSteve French  *
238638c8a9a5SSteve French  * b) an existing tcon with refcount+1 (add a mount point to it) and
238738c8a9a5SSteve French  *    identical ses refcount (no new tcon). Because of (1) we need to
238838c8a9a5SSteve French  *    decrement the ses refcount.
238938c8a9a5SSteve French  */
239038c8a9a5SSteve French static struct cifs_tcon *
239138c8a9a5SSteve French cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
239238c8a9a5SSteve French {
239338c8a9a5SSteve French 	int rc, xid;
239438c8a9a5SSteve French 	struct cifs_tcon *tcon;
239538c8a9a5SSteve French 
239638c8a9a5SSteve French 	tcon = cifs_find_tcon(ses, ctx);
239738c8a9a5SSteve French 	if (tcon) {
239838c8a9a5SSteve French 		/*
239938c8a9a5SSteve French 		 * tcon has refcount already incremented but we need to
240038c8a9a5SSteve French 		 * decrement extra ses reference gotten by caller (case b)
240138c8a9a5SSteve French 		 */
240238c8a9a5SSteve French 		cifs_dbg(FYI, "Found match on UNC path\n");
240338c8a9a5SSteve French 		cifs_put_smb_ses(ses);
240438c8a9a5SSteve French 		return tcon;
240538c8a9a5SSteve French 	}
240638c8a9a5SSteve French 
240738c8a9a5SSteve French 	if (!ses->server->ops->tree_connect) {
240838c8a9a5SSteve French 		rc = -ENOSYS;
240938c8a9a5SSteve French 		goto out_fail;
241038c8a9a5SSteve French 	}
241138c8a9a5SSteve French 
241238c8a9a5SSteve French 	tcon = tconInfoAlloc();
241338c8a9a5SSteve French 	if (tcon == NULL) {
241438c8a9a5SSteve French 		rc = -ENOMEM;
241538c8a9a5SSteve French 		goto out_fail;
241638c8a9a5SSteve French 	}
241738c8a9a5SSteve French 
241838c8a9a5SSteve French 	if (ctx->snapshot_time) {
241938c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
242038c8a9a5SSteve French 			cifs_dbg(VFS,
242138c8a9a5SSteve French 			     "Use SMB2 or later for snapshot mount option\n");
242238c8a9a5SSteve French 			rc = -EOPNOTSUPP;
242338c8a9a5SSteve French 			goto out_fail;
242438c8a9a5SSteve French 		} else
242538c8a9a5SSteve French 			tcon->snapshot_time = ctx->snapshot_time;
242638c8a9a5SSteve French 	}
242738c8a9a5SSteve French 
242838c8a9a5SSteve French 	if (ctx->handle_timeout) {
242938c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
243038c8a9a5SSteve French 			cifs_dbg(VFS,
243138c8a9a5SSteve French 			     "Use SMB2.1 or later for handle timeout option\n");
243238c8a9a5SSteve French 			rc = -EOPNOTSUPP;
243338c8a9a5SSteve French 			goto out_fail;
243438c8a9a5SSteve French 		} else
243538c8a9a5SSteve French 			tcon->handle_timeout = ctx->handle_timeout;
243638c8a9a5SSteve French 	}
243738c8a9a5SSteve French 
243838c8a9a5SSteve French 	tcon->ses = ses;
243938c8a9a5SSteve French 	if (ctx->password) {
244038c8a9a5SSteve French 		tcon->password = kstrdup(ctx->password, GFP_KERNEL);
244138c8a9a5SSteve French 		if (!tcon->password) {
244238c8a9a5SSteve French 			rc = -ENOMEM;
244338c8a9a5SSteve French 			goto out_fail;
244438c8a9a5SSteve French 		}
244538c8a9a5SSteve French 	}
244638c8a9a5SSteve French 
244738c8a9a5SSteve French 	if (ctx->seal) {
244838c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
244938c8a9a5SSteve French 			cifs_dbg(VFS,
245038c8a9a5SSteve French 				 "SMB3 or later required for encryption\n");
245138c8a9a5SSteve French 			rc = -EOPNOTSUPP;
245238c8a9a5SSteve French 			goto out_fail;
245338c8a9a5SSteve French 		} else if (tcon->ses->server->capabilities &
245438c8a9a5SSteve French 					SMB2_GLOBAL_CAP_ENCRYPTION)
245538c8a9a5SSteve French 			tcon->seal = true;
245638c8a9a5SSteve French 		else {
245738c8a9a5SSteve French 			cifs_dbg(VFS, "Encryption is not supported on share\n");
245838c8a9a5SSteve French 			rc = -EOPNOTSUPP;
245938c8a9a5SSteve French 			goto out_fail;
246038c8a9a5SSteve French 		}
246138c8a9a5SSteve French 	}
246238c8a9a5SSteve French 
246338c8a9a5SSteve French 	if (ctx->linux_ext) {
246438c8a9a5SSteve French 		if (ses->server->posix_ext_supported) {
246538c8a9a5SSteve French 			tcon->posix_extensions = true;
246638c8a9a5SSteve French 			pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
246738c8a9a5SSteve French 		} else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
246838c8a9a5SSteve French 		    (strcmp(ses->server->vals->version_string,
246938c8a9a5SSteve French 		     SMB3ANY_VERSION_STRING) == 0) ||
247038c8a9a5SSteve French 		    (strcmp(ses->server->vals->version_string,
247138c8a9a5SSteve French 		     SMBDEFAULT_VERSION_STRING) == 0)) {
247238c8a9a5SSteve French 			cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
247338c8a9a5SSteve French 			rc = -EOPNOTSUPP;
247438c8a9a5SSteve French 			goto out_fail;
247538c8a9a5SSteve French 		} else {
247638c8a9a5SSteve French 			cifs_dbg(VFS, "Check vers= mount option. SMB3.11 "
247738c8a9a5SSteve French 				"disabled but required for POSIX extensions\n");
247838c8a9a5SSteve French 			rc = -EOPNOTSUPP;
247938c8a9a5SSteve French 			goto out_fail;
248038c8a9a5SSteve French 		}
248138c8a9a5SSteve French 	}
248238c8a9a5SSteve French 
248338c8a9a5SSteve French 	xid = get_xid();
248438c8a9a5SSteve French 	rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
248538c8a9a5SSteve French 					    ctx->local_nls);
248638c8a9a5SSteve French 	free_xid(xid);
248738c8a9a5SSteve French 	cifs_dbg(FYI, "Tcon rc = %d\n", rc);
248838c8a9a5SSteve French 	if (rc)
248938c8a9a5SSteve French 		goto out_fail;
249038c8a9a5SSteve French 
249138c8a9a5SSteve French 	tcon->use_persistent = false;
249238c8a9a5SSteve French 	/* check if SMB2 or later, CIFS does not support persistent handles */
249338c8a9a5SSteve French 	if (ctx->persistent) {
249438c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
249538c8a9a5SSteve French 			cifs_dbg(VFS,
249638c8a9a5SSteve French 			     "SMB3 or later required for persistent handles\n");
249738c8a9a5SSteve French 			rc = -EOPNOTSUPP;
249838c8a9a5SSteve French 			goto out_fail;
249938c8a9a5SSteve French 		} else if (ses->server->capabilities &
250038c8a9a5SSteve French 			   SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
250138c8a9a5SSteve French 			tcon->use_persistent = true;
250238c8a9a5SSteve French 		else /* persistent handles requested but not supported */ {
250338c8a9a5SSteve French 			cifs_dbg(VFS,
250438c8a9a5SSteve French 				"Persistent handles not supported on share\n");
250538c8a9a5SSteve French 			rc = -EOPNOTSUPP;
250638c8a9a5SSteve French 			goto out_fail;
250738c8a9a5SSteve French 		}
250838c8a9a5SSteve French 	} else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
250938c8a9a5SSteve French 	     && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
251038c8a9a5SSteve French 	     && (ctx->nopersistent == false)) {
251138c8a9a5SSteve French 		cifs_dbg(FYI, "enabling persistent handles\n");
251238c8a9a5SSteve French 		tcon->use_persistent = true;
251338c8a9a5SSteve French 	} else if (ctx->resilient) {
251438c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
251538c8a9a5SSteve French 			cifs_dbg(VFS,
251638c8a9a5SSteve French 			     "SMB2.1 or later required for resilient handles\n");
251738c8a9a5SSteve French 			rc = -EOPNOTSUPP;
251838c8a9a5SSteve French 			goto out_fail;
251938c8a9a5SSteve French 		}
252038c8a9a5SSteve French 		tcon->use_resilient = true;
252138c8a9a5SSteve French 	}
252238c8a9a5SSteve French 
252338c8a9a5SSteve French 	tcon->use_witness = false;
252438c8a9a5SSteve French 	if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
252538c8a9a5SSteve French 		if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
252638c8a9a5SSteve French 			if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
252738c8a9a5SSteve French 				/*
252838c8a9a5SSteve French 				 * Set witness in use flag in first place
252938c8a9a5SSteve French 				 * to retry registration in the echo task
253038c8a9a5SSteve French 				 */
253138c8a9a5SSteve French 				tcon->use_witness = true;
253238c8a9a5SSteve French 				/* And try to register immediately */
253338c8a9a5SSteve French 				rc = cifs_swn_register(tcon);
253438c8a9a5SSteve French 				if (rc < 0) {
253538c8a9a5SSteve French 					cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
253638c8a9a5SSteve French 					goto out_fail;
253738c8a9a5SSteve French 				}
253838c8a9a5SSteve French 			} else {
253938c8a9a5SSteve French 				/* TODO: try to extend for non-cluster uses (eg multichannel) */
254038c8a9a5SSteve French 				cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
254138c8a9a5SSteve French 				rc = -EOPNOTSUPP;
254238c8a9a5SSteve French 				goto out_fail;
254338c8a9a5SSteve French 			}
254438c8a9a5SSteve French 		} else {
254538c8a9a5SSteve French 			cifs_dbg(VFS, "SMB3 or later required for witness option\n");
254638c8a9a5SSteve French 			rc = -EOPNOTSUPP;
254738c8a9a5SSteve French 			goto out_fail;
254838c8a9a5SSteve French 		}
254938c8a9a5SSteve French 	}
255038c8a9a5SSteve French 
255138c8a9a5SSteve French 	/* If the user really knows what they are doing they can override */
255238c8a9a5SSteve French 	if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
255338c8a9a5SSteve French 		if (ctx->cache_ro)
255438c8a9a5SSteve French 			cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
255538c8a9a5SSteve French 		else if (ctx->cache_rw)
255638c8a9a5SSteve French 			cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
255738c8a9a5SSteve French 	}
255838c8a9a5SSteve French 
255938c8a9a5SSteve French 	if (ctx->no_lease) {
256038c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
256138c8a9a5SSteve French 			cifs_dbg(VFS,
256238c8a9a5SSteve French 				"SMB2 or later required for nolease option\n");
256338c8a9a5SSteve French 			rc = -EOPNOTSUPP;
256438c8a9a5SSteve French 			goto out_fail;
256538c8a9a5SSteve French 		} else
256638c8a9a5SSteve French 			tcon->no_lease = ctx->no_lease;
256738c8a9a5SSteve French 	}
256838c8a9a5SSteve French 
256938c8a9a5SSteve French 	/*
257038c8a9a5SSteve French 	 * We can have only one retry value for a connection to a share so for
257138c8a9a5SSteve French 	 * resources mounted more than once to the same server share the last
257238c8a9a5SSteve French 	 * value passed in for the retry flag is used.
257338c8a9a5SSteve French 	 */
257438c8a9a5SSteve French 	tcon->retry = ctx->retry;
257538c8a9a5SSteve French 	tcon->nocase = ctx->nocase;
257638c8a9a5SSteve French 	tcon->broken_sparse_sup = ctx->no_sparse;
257738c8a9a5SSteve French 	if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
257838c8a9a5SSteve French 		tcon->nohandlecache = ctx->nohandlecache;
257938c8a9a5SSteve French 	else
258038c8a9a5SSteve French 		tcon->nohandlecache = true;
258138c8a9a5SSteve French 	tcon->nodelete = ctx->nodelete;
258238c8a9a5SSteve French 	tcon->local_lease = ctx->local_lease;
258338c8a9a5SSteve French 	INIT_LIST_HEAD(&tcon->pending_opens);
258438c8a9a5SSteve French 	tcon->status = TID_GOOD;
258538c8a9a5SSteve French 
258638c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcon->query_interfaces,
258738c8a9a5SSteve French 			  smb2_query_server_interfaces);
258838c8a9a5SSteve French 	if (ses->server->dialect >= SMB30_PROT_ID &&
258938c8a9a5SSteve French 	    (ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
259038c8a9a5SSteve French 		/* schedule query interfaces poll */
259138c8a9a5SSteve French 		queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
259238c8a9a5SSteve French 				   (SMB_INTERFACE_POLL_INTERVAL * HZ));
259338c8a9a5SSteve French 	}
259438c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
259538c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcon->dfs_cache_work, dfs_cache_refresh);
259638c8a9a5SSteve French #endif
259738c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
259838c8a9a5SSteve French 	list_add(&tcon->tcon_list, &ses->tcon_list);
259938c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
260038c8a9a5SSteve French 
260138c8a9a5SSteve French 	return tcon;
260238c8a9a5SSteve French 
260338c8a9a5SSteve French out_fail:
260438c8a9a5SSteve French 	tconInfoFree(tcon);
260538c8a9a5SSteve French 	return ERR_PTR(rc);
260638c8a9a5SSteve French }
260738c8a9a5SSteve French 
260838c8a9a5SSteve French void
260938c8a9a5SSteve French cifs_put_tlink(struct tcon_link *tlink)
261038c8a9a5SSteve French {
261138c8a9a5SSteve French 	if (!tlink || IS_ERR(tlink))
261238c8a9a5SSteve French 		return;
261338c8a9a5SSteve French 
261438c8a9a5SSteve French 	if (!atomic_dec_and_test(&tlink->tl_count) ||
261538c8a9a5SSteve French 	    test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
261638c8a9a5SSteve French 		tlink->tl_time = jiffies;
261738c8a9a5SSteve French 		return;
261838c8a9a5SSteve French 	}
261938c8a9a5SSteve French 
262038c8a9a5SSteve French 	if (!IS_ERR(tlink_tcon(tlink)))
262138c8a9a5SSteve French 		cifs_put_tcon(tlink_tcon(tlink));
262238c8a9a5SSteve French 	kfree(tlink);
262338c8a9a5SSteve French 	return;
262438c8a9a5SSteve French }
262538c8a9a5SSteve French 
262638c8a9a5SSteve French static int
262738c8a9a5SSteve French compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
262838c8a9a5SSteve French {
262938c8a9a5SSteve French 	struct cifs_sb_info *old = CIFS_SB(sb);
263038c8a9a5SSteve French 	struct cifs_sb_info *new = mnt_data->cifs_sb;
263138c8a9a5SSteve French 	unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
263238c8a9a5SSteve French 	unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
263338c8a9a5SSteve French 
263438c8a9a5SSteve French 	if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
263538c8a9a5SSteve French 		return 0;
263638c8a9a5SSteve French 
263738c8a9a5SSteve French 	if (old->mnt_cifs_serverino_autodisabled)
263838c8a9a5SSteve French 		newflags &= ~CIFS_MOUNT_SERVER_INUM;
263938c8a9a5SSteve French 
264038c8a9a5SSteve French 	if (oldflags != newflags)
264138c8a9a5SSteve French 		return 0;
264238c8a9a5SSteve French 
264338c8a9a5SSteve French 	/*
264438c8a9a5SSteve French 	 * We want to share sb only if we don't specify an r/wsize or
264538c8a9a5SSteve French 	 * specified r/wsize is greater than or equal to existing one.
264638c8a9a5SSteve French 	 */
264738c8a9a5SSteve French 	if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
264838c8a9a5SSteve French 		return 0;
264938c8a9a5SSteve French 
265038c8a9a5SSteve French 	if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
265138c8a9a5SSteve French 		return 0;
265238c8a9a5SSteve French 
265338c8a9a5SSteve French 	if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
265438c8a9a5SSteve French 	    !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
265538c8a9a5SSteve French 		return 0;
265638c8a9a5SSteve French 
265738c8a9a5SSteve French 	if (old->ctx->file_mode != new->ctx->file_mode ||
265838c8a9a5SSteve French 	    old->ctx->dir_mode != new->ctx->dir_mode)
265938c8a9a5SSteve French 		return 0;
266038c8a9a5SSteve French 
266138c8a9a5SSteve French 	if (strcmp(old->local_nls->charset, new->local_nls->charset))
266238c8a9a5SSteve French 		return 0;
266338c8a9a5SSteve French 
266438c8a9a5SSteve French 	if (old->ctx->acregmax != new->ctx->acregmax)
266538c8a9a5SSteve French 		return 0;
266638c8a9a5SSteve French 	if (old->ctx->acdirmax != new->ctx->acdirmax)
266738c8a9a5SSteve French 		return 0;
266838c8a9a5SSteve French 	if (old->ctx->closetimeo != new->ctx->closetimeo)
266938c8a9a5SSteve French 		return 0;
267038c8a9a5SSteve French 
267138c8a9a5SSteve French 	return 1;
267238c8a9a5SSteve French }
267338c8a9a5SSteve French 
267438c8a9a5SSteve French static int match_prepath(struct super_block *sb,
267538c8a9a5SSteve French 			 struct TCP_Server_Info *server,
267638c8a9a5SSteve French 			 struct cifs_mnt_data *mnt_data)
267738c8a9a5SSteve French {
267838c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_data->ctx;
267938c8a9a5SSteve French 	struct cifs_sb_info *old = CIFS_SB(sb);
268038c8a9a5SSteve French 	struct cifs_sb_info *new = mnt_data->cifs_sb;
268138c8a9a5SSteve French 	bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
268238c8a9a5SSteve French 		old->prepath;
268338c8a9a5SSteve French 	bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
268438c8a9a5SSteve French 		new->prepath;
268538c8a9a5SSteve French 
268638c8a9a5SSteve French 	if (server->origin_fullpath &&
268738c8a9a5SSteve French 	    dfs_src_pathname_equal(server->origin_fullpath, ctx->source))
268838c8a9a5SSteve French 		return 1;
268938c8a9a5SSteve French 
269038c8a9a5SSteve French 	if (old_set && new_set && !strcmp(new->prepath, old->prepath))
269138c8a9a5SSteve French 		return 1;
269238c8a9a5SSteve French 	else if (!old_set && !new_set)
269338c8a9a5SSteve French 		return 1;
269438c8a9a5SSteve French 
269538c8a9a5SSteve French 	return 0;
269638c8a9a5SSteve French }
269738c8a9a5SSteve French 
269838c8a9a5SSteve French int
269938c8a9a5SSteve French cifs_match_super(struct super_block *sb, void *data)
270038c8a9a5SSteve French {
270138c8a9a5SSteve French 	struct cifs_mnt_data *mnt_data = data;
270238c8a9a5SSteve French 	struct smb3_fs_context *ctx;
270338c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
270438c8a9a5SSteve French 	struct TCP_Server_Info *tcp_srv;
270538c8a9a5SSteve French 	struct cifs_ses *ses;
270638c8a9a5SSteve French 	struct cifs_tcon *tcon;
270738c8a9a5SSteve French 	struct tcon_link *tlink;
270838c8a9a5SSteve French 	int rc = 0;
270938c8a9a5SSteve French 
271038c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
271138c8a9a5SSteve French 	cifs_sb = CIFS_SB(sb);
271238c8a9a5SSteve French 
271338c8a9a5SSteve French 	/* We do not want to use a superblock that has been shutdown */
271438c8a9a5SSteve French 	if (CIFS_MOUNT_SHUTDOWN & cifs_sb->mnt_cifs_flags) {
271538c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
271638c8a9a5SSteve French 		return 0;
271738c8a9a5SSteve French 	}
271838c8a9a5SSteve French 
271938c8a9a5SSteve French 	tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
272038c8a9a5SSteve French 	if (tlink == NULL) {
272138c8a9a5SSteve French 		/* can not match superblock if tlink were ever null */
272238c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
272338c8a9a5SSteve French 		return 0;
272438c8a9a5SSteve French 	}
272538c8a9a5SSteve French 	tcon = tlink_tcon(tlink);
272638c8a9a5SSteve French 	ses = tcon->ses;
272738c8a9a5SSteve French 	tcp_srv = ses->server;
272838c8a9a5SSteve French 
272938c8a9a5SSteve French 	ctx = mnt_data->ctx;
273038c8a9a5SSteve French 
273138c8a9a5SSteve French 	spin_lock(&tcp_srv->srv_lock);
273238c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
273338c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
273438c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
273538c8a9a5SSteve French 	if (!match_server(tcp_srv, ctx) ||
273638c8a9a5SSteve French 	    !match_session(ses, ctx) ||
273738c8a9a5SSteve French 	    !match_tcon(tcon, ctx) ||
273838c8a9a5SSteve French 	    !match_prepath(sb, tcp_srv, mnt_data)) {
273938c8a9a5SSteve French 		rc = 0;
274038c8a9a5SSteve French 		goto out;
274138c8a9a5SSteve French 	}
274238c8a9a5SSteve French 
274338c8a9a5SSteve French 	rc = compare_mount_options(sb, mnt_data);
274438c8a9a5SSteve French out:
274538c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
274638c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
274738c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
274838c8a9a5SSteve French 	spin_unlock(&tcp_srv->srv_lock);
274938c8a9a5SSteve French 
275038c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
275138c8a9a5SSteve French 	cifs_put_tlink(tlink);
275238c8a9a5SSteve French 	return rc;
275338c8a9a5SSteve French }
275438c8a9a5SSteve French 
275538c8a9a5SSteve French #ifdef CONFIG_DEBUG_LOCK_ALLOC
275638c8a9a5SSteve French static struct lock_class_key cifs_key[2];
275738c8a9a5SSteve French static struct lock_class_key cifs_slock_key[2];
275838c8a9a5SSteve French 
275938c8a9a5SSteve French static inline void
276038c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock)
276138c8a9a5SSteve French {
276238c8a9a5SSteve French 	struct sock *sk = sock->sk;
276338c8a9a5SSteve French 	BUG_ON(!sock_allow_reclassification(sk));
276438c8a9a5SSteve French 	sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
276538c8a9a5SSteve French 		&cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
276638c8a9a5SSteve French }
276738c8a9a5SSteve French 
276838c8a9a5SSteve French static inline void
276938c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock)
277038c8a9a5SSteve French {
277138c8a9a5SSteve French 	struct sock *sk = sock->sk;
277238c8a9a5SSteve French 	BUG_ON(!sock_allow_reclassification(sk));
277338c8a9a5SSteve French 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
277438c8a9a5SSteve French 		&cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
277538c8a9a5SSteve French }
277638c8a9a5SSteve French #else
277738c8a9a5SSteve French static inline void
277838c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock)
277938c8a9a5SSteve French {
278038c8a9a5SSteve French }
278138c8a9a5SSteve French 
278238c8a9a5SSteve French static inline void
278338c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock)
278438c8a9a5SSteve French {
278538c8a9a5SSteve French }
278638c8a9a5SSteve French #endif
278738c8a9a5SSteve French 
278838c8a9a5SSteve French /* See RFC1001 section 14 on representation of Netbios names */
278938c8a9a5SSteve French static void rfc1002mangle(char *target, char *source, unsigned int length)
279038c8a9a5SSteve French {
279138c8a9a5SSteve French 	unsigned int i, j;
279238c8a9a5SSteve French 
279338c8a9a5SSteve French 	for (i = 0, j = 0; i < (length); i++) {
279438c8a9a5SSteve French 		/* mask a nibble at a time and encode */
279538c8a9a5SSteve French 		target[j] = 'A' + (0x0F & (source[i] >> 4));
279638c8a9a5SSteve French 		target[j+1] = 'A' + (0x0F & source[i]);
279738c8a9a5SSteve French 		j += 2;
279838c8a9a5SSteve French 	}
279938c8a9a5SSteve French 
280038c8a9a5SSteve French }
280138c8a9a5SSteve French 
280238c8a9a5SSteve French static int
280338c8a9a5SSteve French bind_socket(struct TCP_Server_Info *server)
280438c8a9a5SSteve French {
280538c8a9a5SSteve French 	int rc = 0;
280638c8a9a5SSteve French 	if (server->srcaddr.ss_family != AF_UNSPEC) {
280738c8a9a5SSteve French 		/* Bind to the specified local IP address */
280838c8a9a5SSteve French 		struct socket *socket = server->ssocket;
280938c8a9a5SSteve French 		rc = socket->ops->bind(socket,
281038c8a9a5SSteve French 				       (struct sockaddr *) &server->srcaddr,
281138c8a9a5SSteve French 				       sizeof(server->srcaddr));
281238c8a9a5SSteve French 		if (rc < 0) {
281338c8a9a5SSteve French 			struct sockaddr_in *saddr4;
281438c8a9a5SSteve French 			struct sockaddr_in6 *saddr6;
281538c8a9a5SSteve French 			saddr4 = (struct sockaddr_in *)&server->srcaddr;
281638c8a9a5SSteve French 			saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
281738c8a9a5SSteve French 			if (saddr6->sin6_family == AF_INET6)
281838c8a9a5SSteve French 				cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
281938c8a9a5SSteve French 					 &saddr6->sin6_addr, rc);
282038c8a9a5SSteve French 			else
282138c8a9a5SSteve French 				cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
282238c8a9a5SSteve French 					 &saddr4->sin_addr.s_addr, rc);
282338c8a9a5SSteve French 		}
282438c8a9a5SSteve French 	}
282538c8a9a5SSteve French 	return rc;
282638c8a9a5SSteve French }
282738c8a9a5SSteve French 
282838c8a9a5SSteve French static int
282938c8a9a5SSteve French ip_rfc1001_connect(struct TCP_Server_Info *server)
283038c8a9a5SSteve French {
283138c8a9a5SSteve French 	int rc = 0;
283238c8a9a5SSteve French 	/*
283338c8a9a5SSteve French 	 * some servers require RFC1001 sessinit before sending
283438c8a9a5SSteve French 	 * negprot - BB check reconnection in case where second
283538c8a9a5SSteve French 	 * sessinit is sent but no second negprot
283638c8a9a5SSteve French 	 */
283738c8a9a5SSteve French 	struct rfc1002_session_packet req = {};
283838c8a9a5SSteve French 	struct smb_hdr *smb_buf = (struct smb_hdr *)&req;
283938c8a9a5SSteve French 	unsigned int len;
284038c8a9a5SSteve French 
284138c8a9a5SSteve French 	req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name);
284238c8a9a5SSteve French 
284338c8a9a5SSteve French 	if (server->server_RFC1001_name[0] != 0)
284438c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.called_name,
284538c8a9a5SSteve French 			      server->server_RFC1001_name,
284638c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
284738c8a9a5SSteve French 	else
284838c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.called_name,
284938c8a9a5SSteve French 			      DEFAULT_CIFS_CALLED_NAME,
285038c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
285138c8a9a5SSteve French 
285238c8a9a5SSteve French 	req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name);
285338c8a9a5SSteve French 
285438c8a9a5SSteve French 	/* calling name ends in null (byte 16) from old smb convention */
285538c8a9a5SSteve French 	if (server->workstation_RFC1001_name[0] != 0)
285638c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.calling_name,
285738c8a9a5SSteve French 			      server->workstation_RFC1001_name,
285838c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
285938c8a9a5SSteve French 	else
286038c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.calling_name,
286138c8a9a5SSteve French 			      "LINUX_CIFS_CLNT",
286238c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
286338c8a9a5SSteve French 
286438c8a9a5SSteve French 	/*
286538c8a9a5SSteve French 	 * As per rfc1002, @len must be the number of bytes that follows the
286638c8a9a5SSteve French 	 * length field of a rfc1002 session request payload.
286738c8a9a5SSteve French 	 */
286838c8a9a5SSteve French 	len = sizeof(req) - offsetof(struct rfc1002_session_packet, trailer.session_req);
286938c8a9a5SSteve French 
287038c8a9a5SSteve French 	smb_buf->smb_buf_length = cpu_to_be32((RFC1002_SESSION_REQUEST << 24) | len);
287138c8a9a5SSteve French 	rc = smb_send(server, smb_buf, len);
287238c8a9a5SSteve French 	/*
287338c8a9a5SSteve French 	 * RFC1001 layer in at least one server requires very short break before
287438c8a9a5SSteve French 	 * negprot presumably because not expecting negprot to follow so fast.
287538c8a9a5SSteve French 	 * This is a simple solution that works without complicating the code
287638c8a9a5SSteve French 	 * and causes no significant slowing down on mount for everyone else
287738c8a9a5SSteve French 	 */
287838c8a9a5SSteve French 	usleep_range(1000, 2000);
287938c8a9a5SSteve French 
288038c8a9a5SSteve French 	return rc;
288138c8a9a5SSteve French }
288238c8a9a5SSteve French 
288338c8a9a5SSteve French static int
288438c8a9a5SSteve French generic_ip_connect(struct TCP_Server_Info *server)
288538c8a9a5SSteve French {
288638c8a9a5SSteve French 	int rc = 0;
288738c8a9a5SSteve French 	__be16 sport;
288838c8a9a5SSteve French 	int slen, sfamily;
288938c8a9a5SSteve French 	struct socket *socket = server->ssocket;
289038c8a9a5SSteve French 	struct sockaddr *saddr;
289138c8a9a5SSteve French 
289238c8a9a5SSteve French 	saddr = (struct sockaddr *) &server->dstaddr;
289338c8a9a5SSteve French 
289438c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6) {
289538c8a9a5SSteve French 		struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
289638c8a9a5SSteve French 
289738c8a9a5SSteve French 		sport = ipv6->sin6_port;
289838c8a9a5SSteve French 		slen = sizeof(struct sockaddr_in6);
289938c8a9a5SSteve French 		sfamily = AF_INET6;
290038c8a9a5SSteve French 		cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
290138c8a9a5SSteve French 				ntohs(sport));
290238c8a9a5SSteve French 	} else {
290338c8a9a5SSteve French 		struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
290438c8a9a5SSteve French 
290538c8a9a5SSteve French 		sport = ipv4->sin_port;
290638c8a9a5SSteve French 		slen = sizeof(struct sockaddr_in);
290738c8a9a5SSteve French 		sfamily = AF_INET;
290838c8a9a5SSteve French 		cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
290938c8a9a5SSteve French 				ntohs(sport));
291038c8a9a5SSteve French 	}
291138c8a9a5SSteve French 
291238c8a9a5SSteve French 	if (socket == NULL) {
291338c8a9a5SSteve French 		rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
291438c8a9a5SSteve French 				   IPPROTO_TCP, &socket, 1);
291538c8a9a5SSteve French 		if (rc < 0) {
291638c8a9a5SSteve French 			cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
291738c8a9a5SSteve French 			server->ssocket = NULL;
291838c8a9a5SSteve French 			return rc;
291938c8a9a5SSteve French 		}
292038c8a9a5SSteve French 
292138c8a9a5SSteve French 		/* BB other socket options to set KEEPALIVE, NODELAY? */
292238c8a9a5SSteve French 		cifs_dbg(FYI, "Socket created\n");
292338c8a9a5SSteve French 		server->ssocket = socket;
292438c8a9a5SSteve French 		socket->sk->sk_allocation = GFP_NOFS;
292538c8a9a5SSteve French 		socket->sk->sk_use_task_frag = false;
292638c8a9a5SSteve French 		if (sfamily == AF_INET6)
292738c8a9a5SSteve French 			cifs_reclassify_socket6(socket);
292838c8a9a5SSteve French 		else
292938c8a9a5SSteve French 			cifs_reclassify_socket4(socket);
293038c8a9a5SSteve French 	}
293138c8a9a5SSteve French 
293238c8a9a5SSteve French 	rc = bind_socket(server);
293338c8a9a5SSteve French 	if (rc < 0)
293438c8a9a5SSteve French 		return rc;
293538c8a9a5SSteve French 
293638c8a9a5SSteve French 	/*
293738c8a9a5SSteve French 	 * Eventually check for other socket options to change from
293838c8a9a5SSteve French 	 * the default. sock_setsockopt not used because it expects
293938c8a9a5SSteve French 	 * user space buffer
294038c8a9a5SSteve French 	 */
294138c8a9a5SSteve French 	socket->sk->sk_rcvtimeo = 7 * HZ;
294238c8a9a5SSteve French 	socket->sk->sk_sndtimeo = 5 * HZ;
294338c8a9a5SSteve French 
294438c8a9a5SSteve French 	/* make the bufsizes depend on wsize/rsize and max requests */
294538c8a9a5SSteve French 	if (server->noautotune) {
294638c8a9a5SSteve French 		if (socket->sk->sk_sndbuf < (200 * 1024))
294738c8a9a5SSteve French 			socket->sk->sk_sndbuf = 200 * 1024;
294838c8a9a5SSteve French 		if (socket->sk->sk_rcvbuf < (140 * 1024))
294938c8a9a5SSteve French 			socket->sk->sk_rcvbuf = 140 * 1024;
295038c8a9a5SSteve French 	}
295138c8a9a5SSteve French 
295238c8a9a5SSteve French 	if (server->tcp_nodelay)
295338c8a9a5SSteve French 		tcp_sock_set_nodelay(socket->sk);
295438c8a9a5SSteve French 
295538c8a9a5SSteve French 	cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
295638c8a9a5SSteve French 		 socket->sk->sk_sndbuf,
295738c8a9a5SSteve French 		 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
295838c8a9a5SSteve French 
295938c8a9a5SSteve French 	rc = socket->ops->connect(socket, saddr, slen,
296038c8a9a5SSteve French 				  server->noblockcnt ? O_NONBLOCK : 0);
296138c8a9a5SSteve French 	/*
296238c8a9a5SSteve French 	 * When mounting SMB root file systems, we do not want to block in
296338c8a9a5SSteve French 	 * connect. Otherwise bail out and then let cifs_reconnect() perform
296438c8a9a5SSteve French 	 * reconnect failover - if possible.
296538c8a9a5SSteve French 	 */
296638c8a9a5SSteve French 	if (server->noblockcnt && rc == -EINPROGRESS)
296738c8a9a5SSteve French 		rc = 0;
296838c8a9a5SSteve French 	if (rc < 0) {
296938c8a9a5SSteve French 		cifs_dbg(FYI, "Error %d connecting to server\n", rc);
297038c8a9a5SSteve French 		trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
297138c8a9a5SSteve French 		sock_release(socket);
297238c8a9a5SSteve French 		server->ssocket = NULL;
297338c8a9a5SSteve French 		return rc;
297438c8a9a5SSteve French 	}
297538c8a9a5SSteve French 	trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
297638c8a9a5SSteve French 	if (sport == htons(RFC1001_PORT))
297738c8a9a5SSteve French 		rc = ip_rfc1001_connect(server);
297838c8a9a5SSteve French 
297938c8a9a5SSteve French 	return rc;
298038c8a9a5SSteve French }
298138c8a9a5SSteve French 
298238c8a9a5SSteve French static int
298338c8a9a5SSteve French ip_connect(struct TCP_Server_Info *server)
298438c8a9a5SSteve French {
298538c8a9a5SSteve French 	__be16 *sport;
298638c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
298738c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
298838c8a9a5SSteve French 
298938c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6)
299038c8a9a5SSteve French 		sport = &addr6->sin6_port;
299138c8a9a5SSteve French 	else
299238c8a9a5SSteve French 		sport = &addr->sin_port;
299338c8a9a5SSteve French 
299438c8a9a5SSteve French 	if (*sport == 0) {
299538c8a9a5SSteve French 		int rc;
299638c8a9a5SSteve French 
299738c8a9a5SSteve French 		/* try with 445 port at first */
299838c8a9a5SSteve French 		*sport = htons(CIFS_PORT);
299938c8a9a5SSteve French 
300038c8a9a5SSteve French 		rc = generic_ip_connect(server);
300138c8a9a5SSteve French 		if (rc >= 0)
300238c8a9a5SSteve French 			return rc;
300338c8a9a5SSteve French 
300438c8a9a5SSteve French 		/* if it failed, try with 139 port */
300538c8a9a5SSteve French 		*sport = htons(RFC1001_PORT);
300638c8a9a5SSteve French 	}
300738c8a9a5SSteve French 
300838c8a9a5SSteve French 	return generic_ip_connect(server);
300938c8a9a5SSteve French }
301038c8a9a5SSteve French 
301138c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
301238c8a9a5SSteve French void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
301338c8a9a5SSteve French 			  struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
301438c8a9a5SSteve French {
301538c8a9a5SSteve French 	/*
301638c8a9a5SSteve French 	 * If we are reconnecting then should we check to see if
301738c8a9a5SSteve French 	 * any requested capabilities changed locally e.g. via
301838c8a9a5SSteve French 	 * remount but we can not do much about it here
301938c8a9a5SSteve French 	 * if they have (even if we could detect it by the following)
302038c8a9a5SSteve French 	 * Perhaps we could add a backpointer to array of sb from tcon
302138c8a9a5SSteve French 	 * or if we change to make all sb to same share the same
302238c8a9a5SSteve French 	 * sb as NFS - then we only have one backpointer to sb.
302338c8a9a5SSteve French 	 * What if we wanted to mount the server share twice once with
302438c8a9a5SSteve French 	 * and once without posixacls or posix paths?
302538c8a9a5SSteve French 	 */
302638c8a9a5SSteve French 	__u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
302738c8a9a5SSteve French 
302838c8a9a5SSteve French 	if (ctx && ctx->no_linux_ext) {
302938c8a9a5SSteve French 		tcon->fsUnixInfo.Capability = 0;
303038c8a9a5SSteve French 		tcon->unix_ext = 0; /* Unix Extensions disabled */
303138c8a9a5SSteve French 		cifs_dbg(FYI, "Linux protocol extensions disabled\n");
303238c8a9a5SSteve French 		return;
303338c8a9a5SSteve French 	} else if (ctx)
303438c8a9a5SSteve French 		tcon->unix_ext = 1; /* Unix Extensions supported */
303538c8a9a5SSteve French 
303638c8a9a5SSteve French 	if (!tcon->unix_ext) {
303738c8a9a5SSteve French 		cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
303838c8a9a5SSteve French 		return;
303938c8a9a5SSteve French 	}
304038c8a9a5SSteve French 
304138c8a9a5SSteve French 	if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
304238c8a9a5SSteve French 		__u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
304338c8a9a5SSteve French 		cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
304438c8a9a5SSteve French 		/*
304538c8a9a5SSteve French 		 * check for reconnect case in which we do not
304638c8a9a5SSteve French 		 * want to change the mount behavior if we can avoid it
304738c8a9a5SSteve French 		 */
304838c8a9a5SSteve French 		if (ctx == NULL) {
304938c8a9a5SSteve French 			/*
305038c8a9a5SSteve French 			 * turn off POSIX ACL and PATHNAMES if not set
305138c8a9a5SSteve French 			 * originally at mount time
305238c8a9a5SSteve French 			 */
305338c8a9a5SSteve French 			if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
305438c8a9a5SSteve French 				cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
305538c8a9a5SSteve French 			if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
305638c8a9a5SSteve French 				if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
305738c8a9a5SSteve French 					cifs_dbg(VFS, "POSIXPATH support change\n");
305838c8a9a5SSteve French 				cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
305938c8a9a5SSteve French 			} else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
306038c8a9a5SSteve French 				cifs_dbg(VFS, "possible reconnect error\n");
306138c8a9a5SSteve French 				cifs_dbg(VFS, "server disabled POSIX path support\n");
306238c8a9a5SSteve French 			}
306338c8a9a5SSteve French 		}
306438c8a9a5SSteve French 
306538c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
306638c8a9a5SSteve French 			cifs_dbg(VFS, "per-share encryption not supported yet\n");
306738c8a9a5SSteve French 
306838c8a9a5SSteve French 		cap &= CIFS_UNIX_CAP_MASK;
306938c8a9a5SSteve French 		if (ctx && ctx->no_psx_acl)
307038c8a9a5SSteve French 			cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
307138c8a9a5SSteve French 		else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
307238c8a9a5SSteve French 			cifs_dbg(FYI, "negotiated posix acl support\n");
307338c8a9a5SSteve French 			if (cifs_sb)
307438c8a9a5SSteve French 				cifs_sb->mnt_cifs_flags |=
307538c8a9a5SSteve French 					CIFS_MOUNT_POSIXACL;
307638c8a9a5SSteve French 		}
307738c8a9a5SSteve French 
307838c8a9a5SSteve French 		if (ctx && ctx->posix_paths == 0)
307938c8a9a5SSteve French 			cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
308038c8a9a5SSteve French 		else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
308138c8a9a5SSteve French 			cifs_dbg(FYI, "negotiate posix pathnames\n");
308238c8a9a5SSteve French 			if (cifs_sb)
308338c8a9a5SSteve French 				cifs_sb->mnt_cifs_flags |=
308438c8a9a5SSteve French 					CIFS_MOUNT_POSIX_PATHS;
308538c8a9a5SSteve French 		}
308638c8a9a5SSteve French 
308738c8a9a5SSteve French 		cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
308838c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2
308938c8a9a5SSteve French 		if (cap & CIFS_UNIX_FCNTL_CAP)
309038c8a9a5SSteve French 			cifs_dbg(FYI, "FCNTL cap\n");
309138c8a9a5SSteve French 		if (cap & CIFS_UNIX_EXTATTR_CAP)
309238c8a9a5SSteve French 			cifs_dbg(FYI, "EXTATTR cap\n");
309338c8a9a5SSteve French 		if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
309438c8a9a5SSteve French 			cifs_dbg(FYI, "POSIX path cap\n");
309538c8a9a5SSteve French 		if (cap & CIFS_UNIX_XATTR_CAP)
309638c8a9a5SSteve French 			cifs_dbg(FYI, "XATTR cap\n");
309738c8a9a5SSteve French 		if (cap & CIFS_UNIX_POSIX_ACL_CAP)
309838c8a9a5SSteve French 			cifs_dbg(FYI, "POSIX ACL cap\n");
309938c8a9a5SSteve French 		if (cap & CIFS_UNIX_LARGE_READ_CAP)
310038c8a9a5SSteve French 			cifs_dbg(FYI, "very large read cap\n");
310138c8a9a5SSteve French 		if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
310238c8a9a5SSteve French 			cifs_dbg(FYI, "very large write cap\n");
310338c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
310438c8a9a5SSteve French 			cifs_dbg(FYI, "transport encryption cap\n");
310538c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
310638c8a9a5SSteve French 			cifs_dbg(FYI, "mandatory transport encryption cap\n");
310738c8a9a5SSteve French #endif /* CIFS_DEBUG2 */
310838c8a9a5SSteve French 		if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
310938c8a9a5SSteve French 			if (ctx == NULL)
311038c8a9a5SSteve French 				cifs_dbg(FYI, "resetting capabilities failed\n");
311138c8a9a5SSteve French 			else
311238c8a9a5SSteve French 				cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n");
311338c8a9a5SSteve French 
311438c8a9a5SSteve French 		}
311538c8a9a5SSteve French 	}
311638c8a9a5SSteve French }
311738c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
311838c8a9a5SSteve French 
311938c8a9a5SSteve French int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
312038c8a9a5SSteve French {
312138c8a9a5SSteve French 	struct smb3_fs_context *ctx = cifs_sb->ctx;
312238c8a9a5SSteve French 
312338c8a9a5SSteve French 	INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
312438c8a9a5SSteve French 
312538c8a9a5SSteve French 	spin_lock_init(&cifs_sb->tlink_tree_lock);
312638c8a9a5SSteve French 	cifs_sb->tlink_tree = RB_ROOT;
312738c8a9a5SSteve French 
312838c8a9a5SSteve French 	cifs_dbg(FYI, "file mode: %04ho  dir mode: %04ho\n",
312938c8a9a5SSteve French 		 ctx->file_mode, ctx->dir_mode);
313038c8a9a5SSteve French 
313138c8a9a5SSteve French 	/* this is needed for ASCII cp to Unicode converts */
313238c8a9a5SSteve French 	if (ctx->iocharset == NULL) {
313338c8a9a5SSteve French 		/* load_nls_default cannot return null */
313438c8a9a5SSteve French 		cifs_sb->local_nls = load_nls_default();
313538c8a9a5SSteve French 	} else {
313638c8a9a5SSteve French 		cifs_sb->local_nls = load_nls(ctx->iocharset);
313738c8a9a5SSteve French 		if (cifs_sb->local_nls == NULL) {
313838c8a9a5SSteve French 			cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
313938c8a9a5SSteve French 				 ctx->iocharset);
314038c8a9a5SSteve French 			return -ELIBACC;
314138c8a9a5SSteve French 		}
314238c8a9a5SSteve French 	}
314338c8a9a5SSteve French 	ctx->local_nls = cifs_sb->local_nls;
314438c8a9a5SSteve French 
314538c8a9a5SSteve French 	smb3_update_mnt_flags(cifs_sb);
314638c8a9a5SSteve French 
314738c8a9a5SSteve French 	if (ctx->direct_io)
314838c8a9a5SSteve French 		cifs_dbg(FYI, "mounting share using direct i/o\n");
314938c8a9a5SSteve French 	if (ctx->cache_ro) {
315038c8a9a5SSteve French 		cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
315138c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
315238c8a9a5SSteve French 	} else if (ctx->cache_rw) {
315338c8a9a5SSteve French 		cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
315438c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
315538c8a9a5SSteve French 					    CIFS_MOUNT_RW_CACHE);
315638c8a9a5SSteve French 	}
315738c8a9a5SSteve French 
315838c8a9a5SSteve French 	if ((ctx->cifs_acl) && (ctx->dynperm))
315938c8a9a5SSteve French 		cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
316038c8a9a5SSteve French 
316138c8a9a5SSteve French 	if (ctx->prepath) {
316238c8a9a5SSteve French 		cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
316338c8a9a5SSteve French 		if (cifs_sb->prepath == NULL)
316438c8a9a5SSteve French 			return -ENOMEM;
316538c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
316638c8a9a5SSteve French 	}
316738c8a9a5SSteve French 
316838c8a9a5SSteve French 	return 0;
316938c8a9a5SSteve French }
317038c8a9a5SSteve French 
317138c8a9a5SSteve French /* Release all succeed connections */
317238c8a9a5SSteve French void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx)
317338c8a9a5SSteve French {
317438c8a9a5SSteve French 	int rc = 0;
317538c8a9a5SSteve French 
317638c8a9a5SSteve French 	if (mnt_ctx->tcon)
317738c8a9a5SSteve French 		cifs_put_tcon(mnt_ctx->tcon);
317838c8a9a5SSteve French 	else if (mnt_ctx->ses)
317938c8a9a5SSteve French 		cifs_put_smb_ses(mnt_ctx->ses);
318038c8a9a5SSteve French 	else if (mnt_ctx->server)
318138c8a9a5SSteve French 		cifs_put_tcp_session(mnt_ctx->server, 0);
318238c8a9a5SSteve French 	mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
318338c8a9a5SSteve French 	free_xid(mnt_ctx->xid);
318438c8a9a5SSteve French }
318538c8a9a5SSteve French 
318638c8a9a5SSteve French int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx)
318738c8a9a5SSteve French {
318838c8a9a5SSteve French 	struct TCP_Server_Info *server = NULL;
318938c8a9a5SSteve French 	struct smb3_fs_context *ctx;
319038c8a9a5SSteve French 	struct cifs_ses *ses = NULL;
319138c8a9a5SSteve French 	unsigned int xid;
319238c8a9a5SSteve French 	int rc = 0;
319338c8a9a5SSteve French 
319438c8a9a5SSteve French 	xid = get_xid();
319538c8a9a5SSteve French 
319638c8a9a5SSteve French 	if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) {
319738c8a9a5SSteve French 		rc = -EINVAL;
319838c8a9a5SSteve French 		goto out;
319938c8a9a5SSteve French 	}
320038c8a9a5SSteve French 	ctx = mnt_ctx->fs_ctx;
320138c8a9a5SSteve French 
320238c8a9a5SSteve French 	/* get a reference to a tcp session */
320338c8a9a5SSteve French 	server = cifs_get_tcp_session(ctx, NULL);
320438c8a9a5SSteve French 	if (IS_ERR(server)) {
320538c8a9a5SSteve French 		rc = PTR_ERR(server);
320638c8a9a5SSteve French 		server = NULL;
320738c8a9a5SSteve French 		goto out;
320838c8a9a5SSteve French 	}
320938c8a9a5SSteve French 
321038c8a9a5SSteve French 	/* get a reference to a SMB session */
321138c8a9a5SSteve French 	ses = cifs_get_smb_ses(server, ctx);
321238c8a9a5SSteve French 	if (IS_ERR(ses)) {
321338c8a9a5SSteve French 		rc = PTR_ERR(ses);
321438c8a9a5SSteve French 		ses = NULL;
321538c8a9a5SSteve French 		goto out;
321638c8a9a5SSteve French 	}
321738c8a9a5SSteve French 
321838c8a9a5SSteve French 	if ((ctx->persistent == true) && (!(ses->server->capabilities &
321938c8a9a5SSteve French 					    SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
322038c8a9a5SSteve French 		cifs_server_dbg(VFS, "persistent handles not supported by server\n");
322138c8a9a5SSteve French 		rc = -EOPNOTSUPP;
322238c8a9a5SSteve French 	}
322338c8a9a5SSteve French 
322438c8a9a5SSteve French out:
322538c8a9a5SSteve French 	mnt_ctx->xid = xid;
322638c8a9a5SSteve French 	mnt_ctx->server = server;
322738c8a9a5SSteve French 	mnt_ctx->ses = ses;
322838c8a9a5SSteve French 	mnt_ctx->tcon = NULL;
322938c8a9a5SSteve French 
323038c8a9a5SSteve French 	return rc;
323138c8a9a5SSteve French }
323238c8a9a5SSteve French 
323338c8a9a5SSteve French int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx)
323438c8a9a5SSteve French {
323538c8a9a5SSteve French 	struct TCP_Server_Info *server;
323638c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
323738c8a9a5SSteve French 	struct smb3_fs_context *ctx;
323838c8a9a5SSteve French 	struct cifs_tcon *tcon = NULL;
323938c8a9a5SSteve French 	int rc = 0;
324038c8a9a5SSteve French 
324138c8a9a5SSteve French 	if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->server || !mnt_ctx->ses || !mnt_ctx->fs_ctx ||
324238c8a9a5SSteve French 			 !mnt_ctx->cifs_sb)) {
324338c8a9a5SSteve French 		rc = -EINVAL;
324438c8a9a5SSteve French 		goto out;
324538c8a9a5SSteve French 	}
324638c8a9a5SSteve French 	server = mnt_ctx->server;
324738c8a9a5SSteve French 	ctx = mnt_ctx->fs_ctx;
324838c8a9a5SSteve French 	cifs_sb = mnt_ctx->cifs_sb;
324938c8a9a5SSteve French 
325038c8a9a5SSteve French 	/* search for existing tcon to this server share */
325138c8a9a5SSteve French 	tcon = cifs_get_tcon(mnt_ctx->ses, ctx);
325238c8a9a5SSteve French 	if (IS_ERR(tcon)) {
325338c8a9a5SSteve French 		rc = PTR_ERR(tcon);
325438c8a9a5SSteve French 		tcon = NULL;
325538c8a9a5SSteve French 		goto out;
325638c8a9a5SSteve French 	}
325738c8a9a5SSteve French 
325838c8a9a5SSteve French 	/* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
325938c8a9a5SSteve French 	if (tcon->posix_extensions)
326038c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
326138c8a9a5SSteve French 
326238c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
326338c8a9a5SSteve French 	/* tell server which Unix caps we support */
326438c8a9a5SSteve French 	if (cap_unix(tcon->ses)) {
326538c8a9a5SSteve French 		/*
326638c8a9a5SSteve French 		 * reset of caps checks mount to see if unix extensions disabled
326738c8a9a5SSteve French 		 * for just this mount.
326838c8a9a5SSteve French 		 */
326938c8a9a5SSteve French 		reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx);
327038c8a9a5SSteve French 		spin_lock(&tcon->ses->server->srv_lock);
327138c8a9a5SSteve French 		if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
327238c8a9a5SSteve French 		    (le64_to_cpu(tcon->fsUnixInfo.Capability) &
327338c8a9a5SSteve French 		     CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
327438c8a9a5SSteve French 			spin_unlock(&tcon->ses->server->srv_lock);
327538c8a9a5SSteve French 			rc = -EACCES;
327638c8a9a5SSteve French 			goto out;
327738c8a9a5SSteve French 		}
327838c8a9a5SSteve French 		spin_unlock(&tcon->ses->server->srv_lock);
327938c8a9a5SSteve French 	} else
328038c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
328138c8a9a5SSteve French 		tcon->unix_ext = 0; /* server does not support them */
328238c8a9a5SSteve French 
328338c8a9a5SSteve French 	/* do not care if a following call succeed - informational */
328438c8a9a5SSteve French 	if (!tcon->pipe && server->ops->qfs_tcon) {
328538c8a9a5SSteve French 		server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb);
328638c8a9a5SSteve French 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
328738c8a9a5SSteve French 			if (tcon->fsDevInfo.DeviceCharacteristics &
328838c8a9a5SSteve French 			    cpu_to_le32(FILE_READ_ONLY_DEVICE))
328938c8a9a5SSteve French 				cifs_dbg(VFS, "mounted to read only share\n");
329038c8a9a5SSteve French 			else if ((cifs_sb->mnt_cifs_flags &
329138c8a9a5SSteve French 				  CIFS_MOUNT_RW_CACHE) == 0)
329238c8a9a5SSteve French 				cifs_dbg(VFS, "read only mount of RW share\n");
329338c8a9a5SSteve French 			/* no need to log a RW mount of a typical RW share */
329438c8a9a5SSteve French 		}
329538c8a9a5SSteve French 	}
329638c8a9a5SSteve French 
329738c8a9a5SSteve French 	/*
329838c8a9a5SSteve French 	 * Clamp the rsize/wsize mount arguments if they are too big for the server
329938c8a9a5SSteve French 	 * and set the rsize/wsize to the negotiated values if not passed in by
330038c8a9a5SSteve French 	 * the user on mount
330138c8a9a5SSteve French 	 */
330238c8a9a5SSteve French 	if ((cifs_sb->ctx->wsize == 0) ||
330338c8a9a5SSteve French 	    (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
330438c8a9a5SSteve French 		cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
330538c8a9a5SSteve French 	if ((cifs_sb->ctx->rsize == 0) ||
330638c8a9a5SSteve French 	    (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
330738c8a9a5SSteve French 		cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
330838c8a9a5SSteve French 
330938c8a9a5SSteve French 	/*
331038c8a9a5SSteve French 	 * The cookie is initialized from volume info returned above.
331138c8a9a5SSteve French 	 * Inside cifs_fscache_get_super_cookie it checks
331238c8a9a5SSteve French 	 * that we do not get super cookie twice.
331338c8a9a5SSteve French 	 */
331438c8a9a5SSteve French 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
331538c8a9a5SSteve French 		cifs_fscache_get_super_cookie(tcon);
331638c8a9a5SSteve French 
331738c8a9a5SSteve French out:
331838c8a9a5SSteve French 	mnt_ctx->tcon = tcon;
331938c8a9a5SSteve French 	return rc;
332038c8a9a5SSteve French }
332138c8a9a5SSteve French 
332238c8a9a5SSteve French static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
332338c8a9a5SSteve French 			     struct cifs_tcon *tcon)
332438c8a9a5SSteve French {
332538c8a9a5SSteve French 	struct tcon_link *tlink;
332638c8a9a5SSteve French 
332738c8a9a5SSteve French 	/* hang the tcon off of the superblock */
332838c8a9a5SSteve French 	tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
332938c8a9a5SSteve French 	if (tlink == NULL)
333038c8a9a5SSteve French 		return -ENOMEM;
333138c8a9a5SSteve French 
333238c8a9a5SSteve French 	tlink->tl_uid = ses->linux_uid;
333338c8a9a5SSteve French 	tlink->tl_tcon = tcon;
333438c8a9a5SSteve French 	tlink->tl_time = jiffies;
333538c8a9a5SSteve French 	set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
333638c8a9a5SSteve French 	set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
333738c8a9a5SSteve French 
333838c8a9a5SSteve French 	cifs_sb->master_tlink = tlink;
333938c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
334038c8a9a5SSteve French 	tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
334138c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
334238c8a9a5SSteve French 
334338c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
334438c8a9a5SSteve French 				TLINK_IDLE_EXPIRE);
334538c8a9a5SSteve French 	return 0;
334638c8a9a5SSteve French }
334738c8a9a5SSteve French 
334838c8a9a5SSteve French static int
334938c8a9a5SSteve French cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
335038c8a9a5SSteve French 					unsigned int xid,
335138c8a9a5SSteve French 					struct cifs_tcon *tcon,
335238c8a9a5SSteve French 					struct cifs_sb_info *cifs_sb,
335338c8a9a5SSteve French 					char *full_path,
335438c8a9a5SSteve French 					int added_treename)
335538c8a9a5SSteve French {
335638c8a9a5SSteve French 	int rc;
335738c8a9a5SSteve French 	char *s;
335838c8a9a5SSteve French 	char sep, tmp;
335938c8a9a5SSteve French 	int skip = added_treename ? 1 : 0;
336038c8a9a5SSteve French 
336138c8a9a5SSteve French 	sep = CIFS_DIR_SEP(cifs_sb);
336238c8a9a5SSteve French 	s = full_path;
336338c8a9a5SSteve French 
336438c8a9a5SSteve French 	rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
336538c8a9a5SSteve French 	while (rc == 0) {
336638c8a9a5SSteve French 		/* skip separators */
336738c8a9a5SSteve French 		while (*s == sep)
336838c8a9a5SSteve French 			s++;
336938c8a9a5SSteve French 		if (!*s)
337038c8a9a5SSteve French 			break;
337138c8a9a5SSteve French 		/* next separator */
337238c8a9a5SSteve French 		while (*s && *s != sep)
337338c8a9a5SSteve French 			s++;
337438c8a9a5SSteve French 		/*
337538c8a9a5SSteve French 		 * if the treename is added, we then have to skip the first
337638c8a9a5SSteve French 		 * part within the separators
337738c8a9a5SSteve French 		 */
337838c8a9a5SSteve French 		if (skip) {
337938c8a9a5SSteve French 			skip = 0;
338038c8a9a5SSteve French 			continue;
338138c8a9a5SSteve French 		}
338238c8a9a5SSteve French 		/*
338338c8a9a5SSteve French 		 * temporarily null-terminate the path at the end of
338438c8a9a5SSteve French 		 * the current component
338538c8a9a5SSteve French 		 */
338638c8a9a5SSteve French 		tmp = *s;
338738c8a9a5SSteve French 		*s = 0;
338838c8a9a5SSteve French 		rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
338938c8a9a5SSteve French 						     full_path);
339038c8a9a5SSteve French 		*s = tmp;
339138c8a9a5SSteve French 	}
339238c8a9a5SSteve French 	return rc;
339338c8a9a5SSteve French }
339438c8a9a5SSteve French 
339538c8a9a5SSteve French /*
339638c8a9a5SSteve French  * Check if path is remote (i.e. a DFS share).
339738c8a9a5SSteve French  *
339838c8a9a5SSteve French  * Return -EREMOTE if it is, otherwise 0 or -errno.
339938c8a9a5SSteve French  */
340038c8a9a5SSteve French int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx)
340138c8a9a5SSteve French {
340238c8a9a5SSteve French 	int rc;
340338c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
340438c8a9a5SSteve French 	struct TCP_Server_Info *server = mnt_ctx->server;
340538c8a9a5SSteve French 	unsigned int xid = mnt_ctx->xid;
340638c8a9a5SSteve French 	struct cifs_tcon *tcon = mnt_ctx->tcon;
340738c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
340838c8a9a5SSteve French 	char *full_path;
340938c8a9a5SSteve French 
341038c8a9a5SSteve French 	if (!server->ops->is_path_accessible)
341138c8a9a5SSteve French 		return -EOPNOTSUPP;
341238c8a9a5SSteve French 
341338c8a9a5SSteve French 	/*
341438c8a9a5SSteve French 	 * cifs_build_path_to_root works only when we have a valid tcon
341538c8a9a5SSteve French 	 */
341638c8a9a5SSteve French 	full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
341738c8a9a5SSteve French 					    tcon->Flags & SMB_SHARE_IS_IN_DFS);
341838c8a9a5SSteve French 	if (full_path == NULL)
341938c8a9a5SSteve French 		return -ENOMEM;
342038c8a9a5SSteve French 
342138c8a9a5SSteve French 	cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
342238c8a9a5SSteve French 
342338c8a9a5SSteve French 	rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
342438c8a9a5SSteve French 					     full_path);
342538c8a9a5SSteve French 	if (rc != 0 && rc != -EREMOTE)
342638c8a9a5SSteve French 		goto out;
342738c8a9a5SSteve French 
342838c8a9a5SSteve French 	if (rc != -EREMOTE) {
342938c8a9a5SSteve French 		rc = cifs_are_all_path_components_accessible(server, xid, tcon,
343038c8a9a5SSteve French 			cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
343138c8a9a5SSteve French 		if (rc != 0) {
343238c8a9a5SSteve French 			cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
343338c8a9a5SSteve French 			cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
343438c8a9a5SSteve French 			rc = 0;
343538c8a9a5SSteve French 		}
343638c8a9a5SSteve French 	}
343738c8a9a5SSteve French 
343838c8a9a5SSteve French out:
343938c8a9a5SSteve French 	kfree(full_path);
344038c8a9a5SSteve French 	return rc;
344138c8a9a5SSteve French }
344238c8a9a5SSteve French 
344338c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
344438c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
344538c8a9a5SSteve French {
344638c8a9a5SSteve French 	struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
344738c8a9a5SSteve French 	bool isdfs;
344838c8a9a5SSteve French 	int rc;
344938c8a9a5SSteve French 
345038c8a9a5SSteve French 	INIT_LIST_HEAD(&mnt_ctx.dfs_ses_list);
345138c8a9a5SSteve French 
345238c8a9a5SSteve French 	rc = dfs_mount_share(&mnt_ctx, &isdfs);
345338c8a9a5SSteve French 	if (rc)
345438c8a9a5SSteve French 		goto error;
345538c8a9a5SSteve French 	if (!isdfs)
345638c8a9a5SSteve French 		goto out;
345738c8a9a5SSteve French 
345838c8a9a5SSteve French 	/*
345938c8a9a5SSteve French 	 * After reconnecting to a different server, unique ids won't match anymore, so we disable
346038c8a9a5SSteve French 	 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
346138c8a9a5SSteve French 	 */
346238c8a9a5SSteve French 	cifs_autodisable_serverino(cifs_sb);
346338c8a9a5SSteve French 	/*
346438c8a9a5SSteve French 	 * Force the use of prefix path to support failover on DFS paths that resolve to targets
346538c8a9a5SSteve French 	 * that have different prefix paths.
346638c8a9a5SSteve French 	 */
346738c8a9a5SSteve French 	cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
346838c8a9a5SSteve French 	kfree(cifs_sb->prepath);
346938c8a9a5SSteve French 	cifs_sb->prepath = ctx->prepath;
347038c8a9a5SSteve French 	ctx->prepath = NULL;
347138c8a9a5SSteve French 
347238c8a9a5SSteve French out:
347338c8a9a5SSteve French 	cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
347438c8a9a5SSteve French 	rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
347538c8a9a5SSteve French 	if (rc)
347638c8a9a5SSteve French 		goto error;
347738c8a9a5SSteve French 
347838c8a9a5SSteve French 	free_xid(mnt_ctx.xid);
347938c8a9a5SSteve French 	return rc;
348038c8a9a5SSteve French 
348138c8a9a5SSteve French error:
348238c8a9a5SSteve French 	dfs_put_root_smb_sessions(&mnt_ctx.dfs_ses_list);
348338c8a9a5SSteve French 	cifs_mount_put_conns(&mnt_ctx);
348438c8a9a5SSteve French 	return rc;
348538c8a9a5SSteve French }
348638c8a9a5SSteve French #else
348738c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
348838c8a9a5SSteve French {
348938c8a9a5SSteve French 	int rc = 0;
349038c8a9a5SSteve French 	struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
349138c8a9a5SSteve French 
349238c8a9a5SSteve French 	rc = cifs_mount_get_session(&mnt_ctx);
349338c8a9a5SSteve French 	if (rc)
349438c8a9a5SSteve French 		goto error;
349538c8a9a5SSteve French 
349638c8a9a5SSteve French 	rc = cifs_mount_get_tcon(&mnt_ctx);
349738c8a9a5SSteve French 	if (rc)
349838c8a9a5SSteve French 		goto error;
349938c8a9a5SSteve French 
350038c8a9a5SSteve French 	rc = cifs_is_path_remote(&mnt_ctx);
350138c8a9a5SSteve French 	if (rc == -EREMOTE)
350238c8a9a5SSteve French 		rc = -EOPNOTSUPP;
350338c8a9a5SSteve French 	if (rc)
350438c8a9a5SSteve French 		goto error;
350538c8a9a5SSteve French 
350638c8a9a5SSteve French 	rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
350738c8a9a5SSteve French 	if (rc)
350838c8a9a5SSteve French 		goto error;
350938c8a9a5SSteve French 
351038c8a9a5SSteve French 	free_xid(mnt_ctx.xid);
351138c8a9a5SSteve French 	return rc;
351238c8a9a5SSteve French 
351338c8a9a5SSteve French error:
351438c8a9a5SSteve French 	cifs_mount_put_conns(&mnt_ctx);
351538c8a9a5SSteve French 	return rc;
351638c8a9a5SSteve French }
351738c8a9a5SSteve French #endif
351838c8a9a5SSteve French 
351938c8a9a5SSteve French /*
352038c8a9a5SSteve French  * Issue a TREE_CONNECT request.
352138c8a9a5SSteve French  */
352238c8a9a5SSteve French int
352338c8a9a5SSteve French CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
352438c8a9a5SSteve French 	 const char *tree, struct cifs_tcon *tcon,
352538c8a9a5SSteve French 	 const struct nls_table *nls_codepage)
352638c8a9a5SSteve French {
352738c8a9a5SSteve French 	struct smb_hdr *smb_buffer;
352838c8a9a5SSteve French 	struct smb_hdr *smb_buffer_response;
352938c8a9a5SSteve French 	TCONX_REQ *pSMB;
353038c8a9a5SSteve French 	TCONX_RSP *pSMBr;
353138c8a9a5SSteve French 	unsigned char *bcc_ptr;
353238c8a9a5SSteve French 	int rc = 0;
353338c8a9a5SSteve French 	int length;
353438c8a9a5SSteve French 	__u16 bytes_left, count;
353538c8a9a5SSteve French 
353638c8a9a5SSteve French 	if (ses == NULL)
353738c8a9a5SSteve French 		return -EIO;
353838c8a9a5SSteve French 
353938c8a9a5SSteve French 	smb_buffer = cifs_buf_get();
354038c8a9a5SSteve French 	if (smb_buffer == NULL)
354138c8a9a5SSteve French 		return -ENOMEM;
354238c8a9a5SSteve French 
354338c8a9a5SSteve French 	smb_buffer_response = smb_buffer;
354438c8a9a5SSteve French 
354538c8a9a5SSteve French 	header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
354638c8a9a5SSteve French 			NULL /*no tid */ , 4 /*wct */ );
354738c8a9a5SSteve French 
354838c8a9a5SSteve French 	smb_buffer->Mid = get_next_mid(ses->server);
354938c8a9a5SSteve French 	smb_buffer->Uid = ses->Suid;
355038c8a9a5SSteve French 	pSMB = (TCONX_REQ *) smb_buffer;
355138c8a9a5SSteve French 	pSMBr = (TCONX_RSP *) smb_buffer_response;
355238c8a9a5SSteve French 
355338c8a9a5SSteve French 	pSMB->AndXCommand = 0xFF;
355438c8a9a5SSteve French 	pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
355538c8a9a5SSteve French 	bcc_ptr = &pSMB->Password[0];
355638c8a9a5SSteve French 
355738c8a9a5SSteve French 	pSMB->PasswordLength = cpu_to_le16(1);	/* minimum */
355838c8a9a5SSteve French 	*bcc_ptr = 0; /* password is null byte */
355938c8a9a5SSteve French 	bcc_ptr++;              /* skip password */
356038c8a9a5SSteve French 	/* already aligned so no need to do it below */
356138c8a9a5SSteve French 
356238c8a9a5SSteve French 	if (ses->server->sign)
356338c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
356438c8a9a5SSteve French 
356538c8a9a5SSteve French 	if (ses->capabilities & CAP_STATUS32) {
356638c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
356738c8a9a5SSteve French 	}
356838c8a9a5SSteve French 	if (ses->capabilities & CAP_DFS) {
356938c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_DFS;
357038c8a9a5SSteve French 	}
357138c8a9a5SSteve French 	if (ses->capabilities & CAP_UNICODE) {
357238c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_UNICODE;
357338c8a9a5SSteve French 		length =
357438c8a9a5SSteve French 		    cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
357538c8a9a5SSteve French 			6 /* max utf8 char length in bytes */ *
357638c8a9a5SSteve French 			(/* server len*/ + 256 /* share len */), nls_codepage);
357738c8a9a5SSteve French 		bcc_ptr += 2 * length;	/* convert num 16 bit words to bytes */
357838c8a9a5SSteve French 		bcc_ptr += 2;	/* skip trailing null */
357938c8a9a5SSteve French 	} else {		/* ASCII */
358038c8a9a5SSteve French 		strcpy(bcc_ptr, tree);
358138c8a9a5SSteve French 		bcc_ptr += strlen(tree) + 1;
358238c8a9a5SSteve French 	}
358338c8a9a5SSteve French 	strcpy(bcc_ptr, "?????");
358438c8a9a5SSteve French 	bcc_ptr += strlen("?????");
358538c8a9a5SSteve French 	bcc_ptr += 1;
358638c8a9a5SSteve French 	count = bcc_ptr - &pSMB->Password[0];
358738c8a9a5SSteve French 	be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
358838c8a9a5SSteve French 	pSMB->ByteCount = cpu_to_le16(count);
358938c8a9a5SSteve French 
359038c8a9a5SSteve French 	rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
359138c8a9a5SSteve French 			 0);
359238c8a9a5SSteve French 
359338c8a9a5SSteve French 	/* above now done in SendReceive */
359438c8a9a5SSteve French 	if (rc == 0) {
359538c8a9a5SSteve French 		bool is_unicode;
359638c8a9a5SSteve French 
359738c8a9a5SSteve French 		tcon->tid = smb_buffer_response->Tid;
359838c8a9a5SSteve French 		bcc_ptr = pByteArea(smb_buffer_response);
359938c8a9a5SSteve French 		bytes_left = get_bcc(smb_buffer_response);
360038c8a9a5SSteve French 		length = strnlen(bcc_ptr, bytes_left - 2);
360138c8a9a5SSteve French 		if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
360238c8a9a5SSteve French 			is_unicode = true;
360338c8a9a5SSteve French 		else
360438c8a9a5SSteve French 			is_unicode = false;
360538c8a9a5SSteve French 
360638c8a9a5SSteve French 
360738c8a9a5SSteve French 		/* skip service field (NB: this field is always ASCII) */
360838c8a9a5SSteve French 		if (length == 3) {
360938c8a9a5SSteve French 			if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
361038c8a9a5SSteve French 			    (bcc_ptr[2] == 'C')) {
361138c8a9a5SSteve French 				cifs_dbg(FYI, "IPC connection\n");
361238c8a9a5SSteve French 				tcon->ipc = true;
361338c8a9a5SSteve French 				tcon->pipe = true;
361438c8a9a5SSteve French 			}
361538c8a9a5SSteve French 		} else if (length == 2) {
361638c8a9a5SSteve French 			if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
361738c8a9a5SSteve French 				/* the most common case */
361838c8a9a5SSteve French 				cifs_dbg(FYI, "disk share connection\n");
361938c8a9a5SSteve French 			}
362038c8a9a5SSteve French 		}
362138c8a9a5SSteve French 		bcc_ptr += length + 1;
362238c8a9a5SSteve French 		bytes_left -= (length + 1);
362338c8a9a5SSteve French 		strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
362438c8a9a5SSteve French 
362538c8a9a5SSteve French 		/* mostly informational -- no need to fail on error here */
362638c8a9a5SSteve French 		kfree(tcon->nativeFileSystem);
362738c8a9a5SSteve French 		tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
362838c8a9a5SSteve French 						      bytes_left, is_unicode,
362938c8a9a5SSteve French 						      nls_codepage);
363038c8a9a5SSteve French 
363138c8a9a5SSteve French 		cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
363238c8a9a5SSteve French 
363338c8a9a5SSteve French 		if ((smb_buffer_response->WordCount == 3) ||
363438c8a9a5SSteve French 			 (smb_buffer_response->WordCount == 7))
363538c8a9a5SSteve French 			/* field is in same location */
363638c8a9a5SSteve French 			tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
363738c8a9a5SSteve French 		else
363838c8a9a5SSteve French 			tcon->Flags = 0;
363938c8a9a5SSteve French 		cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
364038c8a9a5SSteve French 	}
364138c8a9a5SSteve French 
364238c8a9a5SSteve French 	cifs_buf_release(smb_buffer);
364338c8a9a5SSteve French 	return rc;
364438c8a9a5SSteve French }
364538c8a9a5SSteve French 
364638c8a9a5SSteve French static void delayed_free(struct rcu_head *p)
364738c8a9a5SSteve French {
364838c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
364938c8a9a5SSteve French 
365038c8a9a5SSteve French 	unload_nls(cifs_sb->local_nls);
365138c8a9a5SSteve French 	smb3_cleanup_fs_context(cifs_sb->ctx);
365238c8a9a5SSteve French 	kfree(cifs_sb);
365338c8a9a5SSteve French }
365438c8a9a5SSteve French 
365538c8a9a5SSteve French void
365638c8a9a5SSteve French cifs_umount(struct cifs_sb_info *cifs_sb)
365738c8a9a5SSteve French {
365838c8a9a5SSteve French 	struct rb_root *root = &cifs_sb->tlink_tree;
365938c8a9a5SSteve French 	struct rb_node *node;
366038c8a9a5SSteve French 	struct tcon_link *tlink;
366138c8a9a5SSteve French 
366238c8a9a5SSteve French 	cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
366338c8a9a5SSteve French 
366438c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
366538c8a9a5SSteve French 	while ((node = rb_first(root))) {
366638c8a9a5SSteve French 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
366738c8a9a5SSteve French 		cifs_get_tlink(tlink);
366838c8a9a5SSteve French 		clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
366938c8a9a5SSteve French 		rb_erase(node, root);
367038c8a9a5SSteve French 
367138c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
367238c8a9a5SSteve French 		cifs_put_tlink(tlink);
367338c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
367438c8a9a5SSteve French 	}
367538c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
367638c8a9a5SSteve French 
367738c8a9a5SSteve French 	kfree(cifs_sb->prepath);
367838c8a9a5SSteve French 	call_rcu(&cifs_sb->rcu, delayed_free);
367938c8a9a5SSteve French }
368038c8a9a5SSteve French 
368138c8a9a5SSteve French int
368238c8a9a5SSteve French cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
368338c8a9a5SSteve French 			struct TCP_Server_Info *server)
368438c8a9a5SSteve French {
368538c8a9a5SSteve French 	int rc = 0;
368638c8a9a5SSteve French 
368738c8a9a5SSteve French 	if (!server->ops->need_neg || !server->ops->negotiate)
368838c8a9a5SSteve French 		return -ENOSYS;
368938c8a9a5SSteve French 
369038c8a9a5SSteve French 	/* only send once per connect */
369138c8a9a5SSteve French 	spin_lock(&server->srv_lock);
369238c8a9a5SSteve French 	if (server->tcpStatus != CifsGood &&
369338c8a9a5SSteve French 	    server->tcpStatus != CifsNew &&
369438c8a9a5SSteve French 	    server->tcpStatus != CifsNeedNegotiate) {
369538c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
369638c8a9a5SSteve French 		return -EHOSTDOWN;
369738c8a9a5SSteve French 	}
369838c8a9a5SSteve French 
369938c8a9a5SSteve French 	if (!server->ops->need_neg(server) &&
370038c8a9a5SSteve French 	    server->tcpStatus == CifsGood) {
370138c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
370238c8a9a5SSteve French 		return 0;
370338c8a9a5SSteve French 	}
370438c8a9a5SSteve French 
370538c8a9a5SSteve French 	server->tcpStatus = CifsInNegotiate;
370638c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
370738c8a9a5SSteve French 
370838c8a9a5SSteve French 	rc = server->ops->negotiate(xid, ses, server);
370938c8a9a5SSteve French 	if (rc == 0) {
371038c8a9a5SSteve French 		spin_lock(&server->srv_lock);
371138c8a9a5SSteve French 		if (server->tcpStatus == CifsInNegotiate)
371238c8a9a5SSteve French 			server->tcpStatus = CifsGood;
371338c8a9a5SSteve French 		else
371438c8a9a5SSteve French 			rc = -EHOSTDOWN;
371538c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
371638c8a9a5SSteve French 	} else {
371738c8a9a5SSteve French 		spin_lock(&server->srv_lock);
371838c8a9a5SSteve French 		if (server->tcpStatus == CifsInNegotiate)
371938c8a9a5SSteve French 			server->tcpStatus = CifsNeedNegotiate;
372038c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
372138c8a9a5SSteve French 	}
372238c8a9a5SSteve French 
372338c8a9a5SSteve French 	return rc;
372438c8a9a5SSteve French }
372538c8a9a5SSteve French 
372638c8a9a5SSteve French int
372738c8a9a5SSteve French cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
372838c8a9a5SSteve French 		   struct TCP_Server_Info *server,
372938c8a9a5SSteve French 		   struct nls_table *nls_info)
373038c8a9a5SSteve French {
373138c8a9a5SSteve French 	int rc = -ENOSYS;
373238c8a9a5SSteve French 	struct TCP_Server_Info *pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
373338c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr;
373438c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr;
373538c8a9a5SSteve French 	bool is_binding = false;
373638c8a9a5SSteve French 
373738c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
373838c8a9a5SSteve French 	cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
373938c8a9a5SSteve French 		 __func__, ses->chans_need_reconnect);
374038c8a9a5SSteve French 
374138c8a9a5SSteve French 	if (ses->ses_status != SES_GOOD &&
374238c8a9a5SSteve French 	    ses->ses_status != SES_NEW &&
374338c8a9a5SSteve French 	    ses->ses_status != SES_NEED_RECON) {
374438c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
374538c8a9a5SSteve French 		return -EHOSTDOWN;
374638c8a9a5SSteve French 	}
374738c8a9a5SSteve French 
374838c8a9a5SSteve French 	/* only send once per connect */
374938c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
375038c8a9a5SSteve French 	if (CIFS_ALL_CHANS_GOOD(ses)) {
375138c8a9a5SSteve French 		if (ses->ses_status == SES_NEED_RECON)
375238c8a9a5SSteve French 			ses->ses_status = SES_GOOD;
375338c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
375438c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
375538c8a9a5SSteve French 		return 0;
375638c8a9a5SSteve French 	}
375738c8a9a5SSteve French 
375838c8a9a5SSteve French 	cifs_chan_set_in_reconnect(ses, server);
375938c8a9a5SSteve French 	is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
376038c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
376138c8a9a5SSteve French 
376238c8a9a5SSteve French 	if (!is_binding)
376338c8a9a5SSteve French 		ses->ses_status = SES_IN_SETUP;
376438c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
376538c8a9a5SSteve French 
376638c8a9a5SSteve French 	/* update ses ip_addr only for primary chan */
376738c8a9a5SSteve French 	if (server == pserver) {
376838c8a9a5SSteve French 		if (server->dstaddr.ss_family == AF_INET6)
376938c8a9a5SSteve French 			scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr);
377038c8a9a5SSteve French 		else
377138c8a9a5SSteve French 			scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr);
377238c8a9a5SSteve French 	}
377338c8a9a5SSteve French 
377438c8a9a5SSteve French 	if (!is_binding) {
377538c8a9a5SSteve French 		ses->capabilities = server->capabilities;
377638c8a9a5SSteve French 		if (!linuxExtEnabled)
377738c8a9a5SSteve French 			ses->capabilities &= (~server->vals->cap_unix);
377838c8a9a5SSteve French 
377938c8a9a5SSteve French 		if (ses->auth_key.response) {
378038c8a9a5SSteve French 			cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
378138c8a9a5SSteve French 				 ses->auth_key.response);
378238c8a9a5SSteve French 			kfree_sensitive(ses->auth_key.response);
378338c8a9a5SSteve French 			ses->auth_key.response = NULL;
378438c8a9a5SSteve French 			ses->auth_key.len = 0;
378538c8a9a5SSteve French 		}
378638c8a9a5SSteve French 	}
378738c8a9a5SSteve French 
378838c8a9a5SSteve French 	cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
378938c8a9a5SSteve French 		 server->sec_mode, server->capabilities, server->timeAdj);
379038c8a9a5SSteve French 
379138c8a9a5SSteve French 	if (server->ops->sess_setup)
379238c8a9a5SSteve French 		rc = server->ops->sess_setup(xid, ses, server, nls_info);
379338c8a9a5SSteve French 
379438c8a9a5SSteve French 	if (rc) {
379538c8a9a5SSteve French 		cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
379638c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
379738c8a9a5SSteve French 		if (ses->ses_status == SES_IN_SETUP)
379838c8a9a5SSteve French 			ses->ses_status = SES_NEED_RECON;
379938c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
380038c8a9a5SSteve French 		cifs_chan_clear_in_reconnect(ses, server);
380138c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
380238c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
380338c8a9a5SSteve French 	} else {
380438c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
380538c8a9a5SSteve French 		if (ses->ses_status == SES_IN_SETUP)
380638c8a9a5SSteve French 			ses->ses_status = SES_GOOD;
380738c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
380838c8a9a5SSteve French 		cifs_chan_clear_in_reconnect(ses, server);
380938c8a9a5SSteve French 		cifs_chan_clear_need_reconnect(ses, server);
381038c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
381138c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
381238c8a9a5SSteve French 	}
381338c8a9a5SSteve French 
381438c8a9a5SSteve French 	return rc;
381538c8a9a5SSteve French }
381638c8a9a5SSteve French 
381738c8a9a5SSteve French static int
381838c8a9a5SSteve French cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
381938c8a9a5SSteve French {
382038c8a9a5SSteve French 	ctx->sectype = ses->sectype;
382138c8a9a5SSteve French 
382238c8a9a5SSteve French 	/* krb5 is special, since we don't need username or pw */
382338c8a9a5SSteve French 	if (ctx->sectype == Kerberos)
382438c8a9a5SSteve French 		return 0;
382538c8a9a5SSteve French 
382638c8a9a5SSteve French 	return cifs_set_cifscreds(ctx, ses);
382738c8a9a5SSteve French }
382838c8a9a5SSteve French 
382938c8a9a5SSteve French static struct cifs_tcon *
383038c8a9a5SSteve French cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
383138c8a9a5SSteve French {
383238c8a9a5SSteve French 	int rc;
383338c8a9a5SSteve French 	struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
383438c8a9a5SSteve French 	struct cifs_ses *ses;
383538c8a9a5SSteve French 	struct cifs_tcon *tcon = NULL;
383638c8a9a5SSteve French 	struct smb3_fs_context *ctx;
383738c8a9a5SSteve French 
383838c8a9a5SSteve French 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
383938c8a9a5SSteve French 	if (ctx == NULL)
384038c8a9a5SSteve French 		return ERR_PTR(-ENOMEM);
384138c8a9a5SSteve French 
384238c8a9a5SSteve French 	ctx->local_nls = cifs_sb->local_nls;
384338c8a9a5SSteve French 	ctx->linux_uid = fsuid;
384438c8a9a5SSteve French 	ctx->cred_uid = fsuid;
384538c8a9a5SSteve French 	ctx->UNC = master_tcon->tree_name;
384638c8a9a5SSteve French 	ctx->retry = master_tcon->retry;
384738c8a9a5SSteve French 	ctx->nocase = master_tcon->nocase;
384838c8a9a5SSteve French 	ctx->nohandlecache = master_tcon->nohandlecache;
384938c8a9a5SSteve French 	ctx->local_lease = master_tcon->local_lease;
385038c8a9a5SSteve French 	ctx->no_lease = master_tcon->no_lease;
385138c8a9a5SSteve French 	ctx->resilient = master_tcon->use_resilient;
385238c8a9a5SSteve French 	ctx->persistent = master_tcon->use_persistent;
385338c8a9a5SSteve French 	ctx->handle_timeout = master_tcon->handle_timeout;
385438c8a9a5SSteve French 	ctx->no_linux_ext = !master_tcon->unix_ext;
385538c8a9a5SSteve French 	ctx->linux_ext = master_tcon->posix_extensions;
385638c8a9a5SSteve French 	ctx->sectype = master_tcon->ses->sectype;
385738c8a9a5SSteve French 	ctx->sign = master_tcon->ses->sign;
385838c8a9a5SSteve French 	ctx->seal = master_tcon->seal;
385938c8a9a5SSteve French 	ctx->witness = master_tcon->use_witness;
386038c8a9a5SSteve French 
386138c8a9a5SSteve French 	rc = cifs_set_vol_auth(ctx, master_tcon->ses);
386238c8a9a5SSteve French 	if (rc) {
386338c8a9a5SSteve French 		tcon = ERR_PTR(rc);
386438c8a9a5SSteve French 		goto out;
386538c8a9a5SSteve French 	}
386638c8a9a5SSteve French 
386738c8a9a5SSteve French 	/* get a reference for the same TCP session */
386838c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
386938c8a9a5SSteve French 	++master_tcon->ses->server->srv_count;
387038c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
387138c8a9a5SSteve French 
387238c8a9a5SSteve French 	ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
387338c8a9a5SSteve French 	if (IS_ERR(ses)) {
387438c8a9a5SSteve French 		tcon = (struct cifs_tcon *)ses;
387538c8a9a5SSteve French 		cifs_put_tcp_session(master_tcon->ses->server, 0);
387638c8a9a5SSteve French 		goto out;
387738c8a9a5SSteve French 	}
387838c8a9a5SSteve French 
387938c8a9a5SSteve French 	tcon = cifs_get_tcon(ses, ctx);
388038c8a9a5SSteve French 	if (IS_ERR(tcon)) {
388138c8a9a5SSteve French 		cifs_put_smb_ses(ses);
388238c8a9a5SSteve French 		goto out;
388338c8a9a5SSteve French 	}
388438c8a9a5SSteve French 
388538c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
388638c8a9a5SSteve French 	if (cap_unix(ses))
388738c8a9a5SSteve French 		reset_cifs_unix_caps(0, tcon, NULL, ctx);
388838c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
388938c8a9a5SSteve French 
389038c8a9a5SSteve French out:
389138c8a9a5SSteve French 	kfree(ctx->username);
389238c8a9a5SSteve French 	kfree_sensitive(ctx->password);
389338c8a9a5SSteve French 	kfree(ctx);
389438c8a9a5SSteve French 
389538c8a9a5SSteve French 	return tcon;
389638c8a9a5SSteve French }
389738c8a9a5SSteve French 
389838c8a9a5SSteve French struct cifs_tcon *
389938c8a9a5SSteve French cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
390038c8a9a5SSteve French {
390138c8a9a5SSteve French 	return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
390238c8a9a5SSteve French }
390338c8a9a5SSteve French 
390438c8a9a5SSteve French /* find and return a tlink with given uid */
390538c8a9a5SSteve French static struct tcon_link *
390638c8a9a5SSteve French tlink_rb_search(struct rb_root *root, kuid_t uid)
390738c8a9a5SSteve French {
390838c8a9a5SSteve French 	struct rb_node *node = root->rb_node;
390938c8a9a5SSteve French 	struct tcon_link *tlink;
391038c8a9a5SSteve French 
391138c8a9a5SSteve French 	while (node) {
391238c8a9a5SSteve French 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
391338c8a9a5SSteve French 
391438c8a9a5SSteve French 		if (uid_gt(tlink->tl_uid, uid))
391538c8a9a5SSteve French 			node = node->rb_left;
391638c8a9a5SSteve French 		else if (uid_lt(tlink->tl_uid, uid))
391738c8a9a5SSteve French 			node = node->rb_right;
391838c8a9a5SSteve French 		else
391938c8a9a5SSteve French 			return tlink;
392038c8a9a5SSteve French 	}
392138c8a9a5SSteve French 	return NULL;
392238c8a9a5SSteve French }
392338c8a9a5SSteve French 
392438c8a9a5SSteve French /* insert a tcon_link into the tree */
392538c8a9a5SSteve French static void
392638c8a9a5SSteve French tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
392738c8a9a5SSteve French {
392838c8a9a5SSteve French 	struct rb_node **new = &(root->rb_node), *parent = NULL;
392938c8a9a5SSteve French 	struct tcon_link *tlink;
393038c8a9a5SSteve French 
393138c8a9a5SSteve French 	while (*new) {
393238c8a9a5SSteve French 		tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
393338c8a9a5SSteve French 		parent = *new;
393438c8a9a5SSteve French 
393538c8a9a5SSteve French 		if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
393638c8a9a5SSteve French 			new = &((*new)->rb_left);
393738c8a9a5SSteve French 		else
393838c8a9a5SSteve French 			new = &((*new)->rb_right);
393938c8a9a5SSteve French 	}
394038c8a9a5SSteve French 
394138c8a9a5SSteve French 	rb_link_node(&new_tlink->tl_rbnode, parent, new);
394238c8a9a5SSteve French 	rb_insert_color(&new_tlink->tl_rbnode, root);
394338c8a9a5SSteve French }
394438c8a9a5SSteve French 
394538c8a9a5SSteve French /*
394638c8a9a5SSteve French  * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
394738c8a9a5SSteve French  * current task.
394838c8a9a5SSteve French  *
394938c8a9a5SSteve French  * If the superblock doesn't refer to a multiuser mount, then just return
395038c8a9a5SSteve French  * the master tcon for the mount.
395138c8a9a5SSteve French  *
395238c8a9a5SSteve French  * First, search the rbtree for an existing tcon for this fsuid. If one
395338c8a9a5SSteve French  * exists, then check to see if it's pending construction. If it is then wait
395438c8a9a5SSteve French  * for construction to complete. Once it's no longer pending, check to see if
395538c8a9a5SSteve French  * it failed and either return an error or retry construction, depending on
395638c8a9a5SSteve French  * the timeout.
395738c8a9a5SSteve French  *
395838c8a9a5SSteve French  * If one doesn't exist then insert a new tcon_link struct into the tree and
395938c8a9a5SSteve French  * try to construct a new one.
396038c8a9a5SSteve French  */
396138c8a9a5SSteve French struct tcon_link *
396238c8a9a5SSteve French cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
396338c8a9a5SSteve French {
396438c8a9a5SSteve French 	int ret;
396538c8a9a5SSteve French 	kuid_t fsuid = current_fsuid();
396638c8a9a5SSteve French 	struct tcon_link *tlink, *newtlink;
396738c8a9a5SSteve French 
396838c8a9a5SSteve French 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
396938c8a9a5SSteve French 		return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
397038c8a9a5SSteve French 
397138c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
397238c8a9a5SSteve French 	tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
397338c8a9a5SSteve French 	if (tlink)
397438c8a9a5SSteve French 		cifs_get_tlink(tlink);
397538c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
397638c8a9a5SSteve French 
397738c8a9a5SSteve French 	if (tlink == NULL) {
397838c8a9a5SSteve French 		newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
397938c8a9a5SSteve French 		if (newtlink == NULL)
398038c8a9a5SSteve French 			return ERR_PTR(-ENOMEM);
398138c8a9a5SSteve French 		newtlink->tl_uid = fsuid;
398238c8a9a5SSteve French 		newtlink->tl_tcon = ERR_PTR(-EACCES);
398338c8a9a5SSteve French 		set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
398438c8a9a5SSteve French 		set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
398538c8a9a5SSteve French 		cifs_get_tlink(newtlink);
398638c8a9a5SSteve French 
398738c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
398838c8a9a5SSteve French 		/* was one inserted after previous search? */
398938c8a9a5SSteve French 		tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
399038c8a9a5SSteve French 		if (tlink) {
399138c8a9a5SSteve French 			cifs_get_tlink(tlink);
399238c8a9a5SSteve French 			spin_unlock(&cifs_sb->tlink_tree_lock);
399338c8a9a5SSteve French 			kfree(newtlink);
399438c8a9a5SSteve French 			goto wait_for_construction;
399538c8a9a5SSteve French 		}
399638c8a9a5SSteve French 		tlink = newtlink;
399738c8a9a5SSteve French 		tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
399838c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
399938c8a9a5SSteve French 	} else {
400038c8a9a5SSteve French wait_for_construction:
400138c8a9a5SSteve French 		ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
400238c8a9a5SSteve French 				  TASK_INTERRUPTIBLE);
400338c8a9a5SSteve French 		if (ret) {
400438c8a9a5SSteve French 			cifs_put_tlink(tlink);
400538c8a9a5SSteve French 			return ERR_PTR(-ERESTARTSYS);
400638c8a9a5SSteve French 		}
400738c8a9a5SSteve French 
400838c8a9a5SSteve French 		/* if it's good, return it */
400938c8a9a5SSteve French 		if (!IS_ERR(tlink->tl_tcon))
401038c8a9a5SSteve French 			return tlink;
401138c8a9a5SSteve French 
401238c8a9a5SSteve French 		/* return error if we tried this already recently */
401338c8a9a5SSteve French 		if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
401438c8a9a5SSteve French 			cifs_put_tlink(tlink);
401538c8a9a5SSteve French 			return ERR_PTR(-EACCES);
401638c8a9a5SSteve French 		}
401738c8a9a5SSteve French 
401838c8a9a5SSteve French 		if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
401938c8a9a5SSteve French 			goto wait_for_construction;
402038c8a9a5SSteve French 	}
402138c8a9a5SSteve French 
402238c8a9a5SSteve French 	tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
402338c8a9a5SSteve French 	clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
402438c8a9a5SSteve French 	wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
402538c8a9a5SSteve French 
402638c8a9a5SSteve French 	if (IS_ERR(tlink->tl_tcon)) {
402738c8a9a5SSteve French 		cifs_put_tlink(tlink);
402838c8a9a5SSteve French 		return ERR_PTR(-EACCES);
402938c8a9a5SSteve French 	}
403038c8a9a5SSteve French 
403138c8a9a5SSteve French 	return tlink;
403238c8a9a5SSteve French }
403338c8a9a5SSteve French 
403438c8a9a5SSteve French /*
403538c8a9a5SSteve French  * periodic workqueue job that scans tcon_tree for a superblock and closes
403638c8a9a5SSteve French  * out tcons.
403738c8a9a5SSteve French  */
403838c8a9a5SSteve French static void
403938c8a9a5SSteve French cifs_prune_tlinks(struct work_struct *work)
404038c8a9a5SSteve French {
404138c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
404238c8a9a5SSteve French 						    prune_tlinks.work);
404338c8a9a5SSteve French 	struct rb_root *root = &cifs_sb->tlink_tree;
404438c8a9a5SSteve French 	struct rb_node *node;
404538c8a9a5SSteve French 	struct rb_node *tmp;
404638c8a9a5SSteve French 	struct tcon_link *tlink;
404738c8a9a5SSteve French 
404838c8a9a5SSteve French 	/*
404938c8a9a5SSteve French 	 * Because we drop the spinlock in the loop in order to put the tlink
405038c8a9a5SSteve French 	 * it's not guarded against removal of links from the tree. The only
405138c8a9a5SSteve French 	 * places that remove entries from the tree are this function and
405238c8a9a5SSteve French 	 * umounts. Because this function is non-reentrant and is canceled
405338c8a9a5SSteve French 	 * before umount can proceed, this is safe.
405438c8a9a5SSteve French 	 */
405538c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
405638c8a9a5SSteve French 	node = rb_first(root);
405738c8a9a5SSteve French 	while (node != NULL) {
405838c8a9a5SSteve French 		tmp = node;
405938c8a9a5SSteve French 		node = rb_next(tmp);
406038c8a9a5SSteve French 		tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
406138c8a9a5SSteve French 
406238c8a9a5SSteve French 		if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
406338c8a9a5SSteve French 		    atomic_read(&tlink->tl_count) != 0 ||
406438c8a9a5SSteve French 		    time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
406538c8a9a5SSteve French 			continue;
406638c8a9a5SSteve French 
406738c8a9a5SSteve French 		cifs_get_tlink(tlink);
406838c8a9a5SSteve French 		clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
406938c8a9a5SSteve French 		rb_erase(tmp, root);
407038c8a9a5SSteve French 
407138c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
407238c8a9a5SSteve French 		cifs_put_tlink(tlink);
407338c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
407438c8a9a5SSteve French 	}
407538c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
407638c8a9a5SSteve French 
407738c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
407838c8a9a5SSteve French 				TLINK_IDLE_EXPIRE);
407938c8a9a5SSteve French }
408038c8a9a5SSteve French 
408138c8a9a5SSteve French #ifndef CONFIG_CIFS_DFS_UPCALL
408238c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
408338c8a9a5SSteve French {
408438c8a9a5SSteve French 	int rc;
408538c8a9a5SSteve French 	const struct smb_version_operations *ops = tcon->ses->server->ops;
408638c8a9a5SSteve French 
408738c8a9a5SSteve French 	/* only send once per connect */
408838c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
4089*91f4480cSShyam Prasad N 	if (tcon->status == TID_GOOD) {
4090*91f4480cSShyam Prasad N 		spin_unlock(&tcon->tc_lock);
4091*91f4480cSShyam Prasad N 		return 0;
4092*91f4480cSShyam Prasad N 	}
4093*91f4480cSShyam Prasad N 
409438c8a9a5SSteve French 	if (tcon->status != TID_NEW &&
409538c8a9a5SSteve French 	    tcon->status != TID_NEED_TCON) {
409638c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
409738c8a9a5SSteve French 		return -EHOSTDOWN;
409838c8a9a5SSteve French 	}
409938c8a9a5SSteve French 
410038c8a9a5SSteve French 	tcon->status = TID_IN_TCON;
410138c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
410238c8a9a5SSteve French 
410338c8a9a5SSteve French 	rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc);
410438c8a9a5SSteve French 	if (rc) {
410538c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
410638c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
410738c8a9a5SSteve French 			tcon->status = TID_NEED_TCON;
410838c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
410938c8a9a5SSteve French 	} else {
411038c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
411138c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
411238c8a9a5SSteve French 			tcon->status = TID_GOOD;
411338c8a9a5SSteve French 		tcon->need_reconnect = false;
411438c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
411538c8a9a5SSteve French 	}
411638c8a9a5SSteve French 
411738c8a9a5SSteve French 	return rc;
411838c8a9a5SSteve French }
411938c8a9a5SSteve French #endif
4120