xref: /openbmc/linux/fs/smb/client/connect.c (revision 38c8a9a52082579090e34c033d439ed2cd1a462d)
1*38c8a9a5SSteve French // SPDX-License-Identifier: LGPL-2.1
2*38c8a9a5SSteve French /*
3*38c8a9a5SSteve French  *
4*38c8a9a5SSteve French  *   Copyright (C) International Business Machines  Corp., 2002,2011
5*38c8a9a5SSteve French  *   Author(s): Steve French (sfrench@us.ibm.com)
6*38c8a9a5SSteve French  *
7*38c8a9a5SSteve French  */
8*38c8a9a5SSteve French #include <linux/fs.h>
9*38c8a9a5SSteve French #include <linux/net.h>
10*38c8a9a5SSteve French #include <linux/string.h>
11*38c8a9a5SSteve French #include <linux/sched/mm.h>
12*38c8a9a5SSteve French #include <linux/sched/signal.h>
13*38c8a9a5SSteve French #include <linux/list.h>
14*38c8a9a5SSteve French #include <linux/wait.h>
15*38c8a9a5SSteve French #include <linux/slab.h>
16*38c8a9a5SSteve French #include <linux/pagemap.h>
17*38c8a9a5SSteve French #include <linux/ctype.h>
18*38c8a9a5SSteve French #include <linux/utsname.h>
19*38c8a9a5SSteve French #include <linux/mempool.h>
20*38c8a9a5SSteve French #include <linux/delay.h>
21*38c8a9a5SSteve French #include <linux/completion.h>
22*38c8a9a5SSteve French #include <linux/kthread.h>
23*38c8a9a5SSteve French #include <linux/pagevec.h>
24*38c8a9a5SSteve French #include <linux/freezer.h>
25*38c8a9a5SSteve French #include <linux/namei.h>
26*38c8a9a5SSteve French #include <linux/uuid.h>
27*38c8a9a5SSteve French #include <linux/uaccess.h>
28*38c8a9a5SSteve French #include <asm/processor.h>
29*38c8a9a5SSteve French #include <linux/inet.h>
30*38c8a9a5SSteve French #include <linux/module.h>
31*38c8a9a5SSteve French #include <keys/user-type.h>
32*38c8a9a5SSteve French #include <net/ipv6.h>
33*38c8a9a5SSteve French #include <linux/parser.h>
34*38c8a9a5SSteve French #include <linux/bvec.h>
35*38c8a9a5SSteve French #include "cifspdu.h"
36*38c8a9a5SSteve French #include "cifsglob.h"
37*38c8a9a5SSteve French #include "cifsproto.h"
38*38c8a9a5SSteve French #include "cifs_unicode.h"
39*38c8a9a5SSteve French #include "cifs_debug.h"
40*38c8a9a5SSteve French #include "cifs_fs_sb.h"
41*38c8a9a5SSteve French #include "ntlmssp.h"
42*38c8a9a5SSteve French #include "nterr.h"
43*38c8a9a5SSteve French #include "rfc1002pdu.h"
44*38c8a9a5SSteve French #include "fscache.h"
45*38c8a9a5SSteve French #include "smb2proto.h"
46*38c8a9a5SSteve French #include "smbdirect.h"
47*38c8a9a5SSteve French #include "dns_resolve.h"
48*38c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
49*38c8a9a5SSteve French #include "dfs.h"
50*38c8a9a5SSteve French #include "dfs_cache.h"
51*38c8a9a5SSteve French #endif
52*38c8a9a5SSteve French #include "fs_context.h"
53*38c8a9a5SSteve French #include "cifs_swn.h"
54*38c8a9a5SSteve French 
55*38c8a9a5SSteve French extern mempool_t *cifs_req_poolp;
56*38c8a9a5SSteve French extern bool disable_legacy_dialects;
57*38c8a9a5SSteve French 
58*38c8a9a5SSteve French /* FIXME: should these be tunable? */
59*38c8a9a5SSteve French #define TLINK_ERROR_EXPIRE	(1 * HZ)
60*38c8a9a5SSteve French #define TLINK_IDLE_EXPIRE	(600 * HZ)
61*38c8a9a5SSteve French 
62*38c8a9a5SSteve French /* Drop the connection to not overload the server */
63*38c8a9a5SSteve French #define NUM_STATUS_IO_TIMEOUT   5
64*38c8a9a5SSteve French 
65*38c8a9a5SSteve French static int ip_connect(struct TCP_Server_Info *server);
66*38c8a9a5SSteve French static int generic_ip_connect(struct TCP_Server_Info *server);
67*38c8a9a5SSteve French static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
68*38c8a9a5SSteve French static void cifs_prune_tlinks(struct work_struct *work);
69*38c8a9a5SSteve French 
70*38c8a9a5SSteve French /*
71*38c8a9a5SSteve French  * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
72*38c8a9a5SSteve French  * get their ip addresses changed at some point.
73*38c8a9a5SSteve French  *
74*38c8a9a5SSteve French  * This should be called with server->srv_mutex held.
75*38c8a9a5SSteve French  */
76*38c8a9a5SSteve French static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
77*38c8a9a5SSteve French {
78*38c8a9a5SSteve French 	int rc;
79*38c8a9a5SSteve French 	int len;
80*38c8a9a5SSteve French 	char *unc;
81*38c8a9a5SSteve French 	struct sockaddr_storage ss;
82*38c8a9a5SSteve French 
83*38c8a9a5SSteve French 	if (!server->hostname)
84*38c8a9a5SSteve French 		return -EINVAL;
85*38c8a9a5SSteve French 
86*38c8a9a5SSteve French 	/* if server hostname isn't populated, there's nothing to do here */
87*38c8a9a5SSteve French 	if (server->hostname[0] == '\0')
88*38c8a9a5SSteve French 		return 0;
89*38c8a9a5SSteve French 
90*38c8a9a5SSteve French 	len = strlen(server->hostname) + 3;
91*38c8a9a5SSteve French 
92*38c8a9a5SSteve French 	unc = kmalloc(len, GFP_KERNEL);
93*38c8a9a5SSteve French 	if (!unc) {
94*38c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
95*38c8a9a5SSteve French 		return -ENOMEM;
96*38c8a9a5SSteve French 	}
97*38c8a9a5SSteve French 	scnprintf(unc, len, "\\\\%s", server->hostname);
98*38c8a9a5SSteve French 
99*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
100*38c8a9a5SSteve French 	ss = server->dstaddr;
101*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
102*38c8a9a5SSteve French 
103*38c8a9a5SSteve French 	rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
104*38c8a9a5SSteve French 	kfree(unc);
105*38c8a9a5SSteve French 
106*38c8a9a5SSteve French 	if (rc < 0) {
107*38c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
108*38c8a9a5SSteve French 			 __func__, server->hostname, rc);
109*38c8a9a5SSteve French 	} else {
110*38c8a9a5SSteve French 		spin_lock(&server->srv_lock);
111*38c8a9a5SSteve French 		memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr));
112*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
113*38c8a9a5SSteve French 		rc = 0;
114*38c8a9a5SSteve French 	}
115*38c8a9a5SSteve French 
116*38c8a9a5SSteve French 	return rc;
117*38c8a9a5SSteve French }
118*38c8a9a5SSteve French 
119*38c8a9a5SSteve French static void smb2_query_server_interfaces(struct work_struct *work)
120*38c8a9a5SSteve French {
121*38c8a9a5SSteve French 	int rc;
122*38c8a9a5SSteve French 	struct cifs_tcon *tcon = container_of(work,
123*38c8a9a5SSteve French 					struct cifs_tcon,
124*38c8a9a5SSteve French 					query_interfaces.work);
125*38c8a9a5SSteve French 
126*38c8a9a5SSteve French 	/*
127*38c8a9a5SSteve French 	 * query server network interfaces, in case they change
128*38c8a9a5SSteve French 	 */
129*38c8a9a5SSteve French 	rc = SMB3_request_interfaces(0, tcon, false);
130*38c8a9a5SSteve French 	if (rc) {
131*38c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
132*38c8a9a5SSteve French 				__func__, rc);
133*38c8a9a5SSteve French 	}
134*38c8a9a5SSteve French 
135*38c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
136*38c8a9a5SSteve French 			   (SMB_INTERFACE_POLL_INTERVAL * HZ));
137*38c8a9a5SSteve French }
138*38c8a9a5SSteve French 
139*38c8a9a5SSteve French /*
140*38c8a9a5SSteve French  * Update the tcpStatus for the server.
141*38c8a9a5SSteve French  * This is used to signal the cifsd thread to call cifs_reconnect
142*38c8a9a5SSteve French  * ONLY cifsd thread should call cifs_reconnect. For any other
143*38c8a9a5SSteve French  * thread, use this function
144*38c8a9a5SSteve French  *
145*38c8a9a5SSteve French  * @server: the tcp ses for which reconnect is needed
146*38c8a9a5SSteve French  * @all_channels: if this needs to be done for all channels
147*38c8a9a5SSteve French  */
148*38c8a9a5SSteve French void
149*38c8a9a5SSteve French cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
150*38c8a9a5SSteve French 				bool all_channels)
151*38c8a9a5SSteve French {
152*38c8a9a5SSteve French 	struct TCP_Server_Info *pserver;
153*38c8a9a5SSteve French 	struct cifs_ses *ses;
154*38c8a9a5SSteve French 	int i;
155*38c8a9a5SSteve French 
156*38c8a9a5SSteve French 	/* If server is a channel, select the primary channel */
157*38c8a9a5SSteve French 	pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
158*38c8a9a5SSteve French 
159*38c8a9a5SSteve French 	spin_lock(&pserver->srv_lock);
160*38c8a9a5SSteve French 	if (!all_channels) {
161*38c8a9a5SSteve French 		pserver->tcpStatus = CifsNeedReconnect;
162*38c8a9a5SSteve French 		spin_unlock(&pserver->srv_lock);
163*38c8a9a5SSteve French 		return;
164*38c8a9a5SSteve French 	}
165*38c8a9a5SSteve French 	spin_unlock(&pserver->srv_lock);
166*38c8a9a5SSteve French 
167*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
168*38c8a9a5SSteve French 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
169*38c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
170*38c8a9a5SSteve French 		for (i = 0; i < ses->chan_count; i++) {
171*38c8a9a5SSteve French 			spin_lock(&ses->chans[i].server->srv_lock);
172*38c8a9a5SSteve French 			ses->chans[i].server->tcpStatus = CifsNeedReconnect;
173*38c8a9a5SSteve French 			spin_unlock(&ses->chans[i].server->srv_lock);
174*38c8a9a5SSteve French 		}
175*38c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
176*38c8a9a5SSteve French 	}
177*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
178*38c8a9a5SSteve French }
179*38c8a9a5SSteve French 
180*38c8a9a5SSteve French /*
181*38c8a9a5SSteve French  * Mark all sessions and tcons for reconnect.
182*38c8a9a5SSteve French  * IMPORTANT: make sure that this gets called only from
183*38c8a9a5SSteve French  * cifsd thread. For any other thread, use
184*38c8a9a5SSteve French  * cifs_signal_cifsd_for_reconnect
185*38c8a9a5SSteve French  *
186*38c8a9a5SSteve French  * @server: the tcp ses for which reconnect is needed
187*38c8a9a5SSteve French  * @server needs to be previously set to CifsNeedReconnect.
188*38c8a9a5SSteve French  * @mark_smb_session: whether even sessions need to be marked
189*38c8a9a5SSteve French  */
190*38c8a9a5SSteve French void
191*38c8a9a5SSteve French cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
192*38c8a9a5SSteve French 				      bool mark_smb_session)
193*38c8a9a5SSteve French {
194*38c8a9a5SSteve French 	struct TCP_Server_Info *pserver;
195*38c8a9a5SSteve French 	struct cifs_ses *ses, *nses;
196*38c8a9a5SSteve French 	struct cifs_tcon *tcon;
197*38c8a9a5SSteve French 
198*38c8a9a5SSteve French 	/*
199*38c8a9a5SSteve French 	 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
200*38c8a9a5SSteve French 	 * are not used until reconnected.
201*38c8a9a5SSteve French 	 */
202*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
203*38c8a9a5SSteve French 
204*38c8a9a5SSteve French 	/* If server is a channel, select the primary channel */
205*38c8a9a5SSteve French 	pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
206*38c8a9a5SSteve French 
207*38c8a9a5SSteve French 
208*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
209*38c8a9a5SSteve French 	list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) {
210*38c8a9a5SSteve French 		/* check if iface is still active */
211*38c8a9a5SSteve French 		if (!cifs_chan_is_iface_active(ses, server))
212*38c8a9a5SSteve French 			cifs_chan_update_iface(ses, server);
213*38c8a9a5SSteve French 
214*38c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
215*38c8a9a5SSteve French 		if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) {
216*38c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
217*38c8a9a5SSteve French 			continue;
218*38c8a9a5SSteve French 		}
219*38c8a9a5SSteve French 
220*38c8a9a5SSteve French 		if (mark_smb_session)
221*38c8a9a5SSteve French 			CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
222*38c8a9a5SSteve French 		else
223*38c8a9a5SSteve French 			cifs_chan_set_need_reconnect(ses, server);
224*38c8a9a5SSteve French 
225*38c8a9a5SSteve French 		cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
226*38c8a9a5SSteve French 			 __func__, ses->chans_need_reconnect);
227*38c8a9a5SSteve French 
228*38c8a9a5SSteve French 		/* If all channels need reconnect, then tcon needs reconnect */
229*38c8a9a5SSteve French 		if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
230*38c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
231*38c8a9a5SSteve French 			continue;
232*38c8a9a5SSteve French 		}
233*38c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
234*38c8a9a5SSteve French 
235*38c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
236*38c8a9a5SSteve French 		ses->ses_status = SES_NEED_RECON;
237*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
238*38c8a9a5SSteve French 
239*38c8a9a5SSteve French 		list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
240*38c8a9a5SSteve French 			tcon->need_reconnect = true;
241*38c8a9a5SSteve French 			spin_lock(&tcon->tc_lock);
242*38c8a9a5SSteve French 			tcon->status = TID_NEED_RECON;
243*38c8a9a5SSteve French 			spin_unlock(&tcon->tc_lock);
244*38c8a9a5SSteve French 		}
245*38c8a9a5SSteve French 		if (ses->tcon_ipc) {
246*38c8a9a5SSteve French 			ses->tcon_ipc->need_reconnect = true;
247*38c8a9a5SSteve French 			spin_lock(&ses->tcon_ipc->tc_lock);
248*38c8a9a5SSteve French 			ses->tcon_ipc->status = TID_NEED_RECON;
249*38c8a9a5SSteve French 			spin_unlock(&ses->tcon_ipc->tc_lock);
250*38c8a9a5SSteve French 		}
251*38c8a9a5SSteve French 	}
252*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
253*38c8a9a5SSteve French }
254*38c8a9a5SSteve French 
255*38c8a9a5SSteve French static void
256*38c8a9a5SSteve French cifs_abort_connection(struct TCP_Server_Info *server)
257*38c8a9a5SSteve French {
258*38c8a9a5SSteve French 	struct mid_q_entry *mid, *nmid;
259*38c8a9a5SSteve French 	struct list_head retry_list;
260*38c8a9a5SSteve French 
261*38c8a9a5SSteve French 	server->maxBuf = 0;
262*38c8a9a5SSteve French 	server->max_read = 0;
263*38c8a9a5SSteve French 
264*38c8a9a5SSteve French 	/* do not want to be sending data on a socket we are freeing */
265*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
266*38c8a9a5SSteve French 	cifs_server_lock(server);
267*38c8a9a5SSteve French 	if (server->ssocket) {
268*38c8a9a5SSteve French 		cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
269*38c8a9a5SSteve French 			 server->ssocket->flags);
270*38c8a9a5SSteve French 		kernel_sock_shutdown(server->ssocket, SHUT_WR);
271*38c8a9a5SSteve French 		cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
272*38c8a9a5SSteve French 			 server->ssocket->flags);
273*38c8a9a5SSteve French 		sock_release(server->ssocket);
274*38c8a9a5SSteve French 		server->ssocket = NULL;
275*38c8a9a5SSteve French 	}
276*38c8a9a5SSteve French 	server->sequence_number = 0;
277*38c8a9a5SSteve French 	server->session_estab = false;
278*38c8a9a5SSteve French 	kfree_sensitive(server->session_key.response);
279*38c8a9a5SSteve French 	server->session_key.response = NULL;
280*38c8a9a5SSteve French 	server->session_key.len = 0;
281*38c8a9a5SSteve French 	server->lstrp = jiffies;
282*38c8a9a5SSteve French 
283*38c8a9a5SSteve French 	/* mark submitted MIDs for retry and issue callback */
284*38c8a9a5SSteve French 	INIT_LIST_HEAD(&retry_list);
285*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
286*38c8a9a5SSteve French 	spin_lock(&server->mid_lock);
287*38c8a9a5SSteve French 	list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
288*38c8a9a5SSteve French 		kref_get(&mid->refcount);
289*38c8a9a5SSteve French 		if (mid->mid_state == MID_REQUEST_SUBMITTED)
290*38c8a9a5SSteve French 			mid->mid_state = MID_RETRY_NEEDED;
291*38c8a9a5SSteve French 		list_move(&mid->qhead, &retry_list);
292*38c8a9a5SSteve French 		mid->mid_flags |= MID_DELETED;
293*38c8a9a5SSteve French 	}
294*38c8a9a5SSteve French 	spin_unlock(&server->mid_lock);
295*38c8a9a5SSteve French 	cifs_server_unlock(server);
296*38c8a9a5SSteve French 
297*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
298*38c8a9a5SSteve French 	list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
299*38c8a9a5SSteve French 		list_del_init(&mid->qhead);
300*38c8a9a5SSteve French 		mid->callback(mid);
301*38c8a9a5SSteve French 		release_mid(mid);
302*38c8a9a5SSteve French 	}
303*38c8a9a5SSteve French 
304*38c8a9a5SSteve French 	if (cifs_rdma_enabled(server)) {
305*38c8a9a5SSteve French 		cifs_server_lock(server);
306*38c8a9a5SSteve French 		smbd_destroy(server);
307*38c8a9a5SSteve French 		cifs_server_unlock(server);
308*38c8a9a5SSteve French 	}
309*38c8a9a5SSteve French }
310*38c8a9a5SSteve French 
311*38c8a9a5SSteve French static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
312*38c8a9a5SSteve French {
313*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
314*38c8a9a5SSteve French 	server->nr_targets = num_targets;
315*38c8a9a5SSteve French 	if (server->tcpStatus == CifsExiting) {
316*38c8a9a5SSteve French 		/* the demux thread will exit normally next time through the loop */
317*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
318*38c8a9a5SSteve French 		wake_up(&server->response_q);
319*38c8a9a5SSteve French 		return false;
320*38c8a9a5SSteve French 	}
321*38c8a9a5SSteve French 
322*38c8a9a5SSteve French 	cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
323*38c8a9a5SSteve French 	trace_smb3_reconnect(server->CurrentMid, server->conn_id,
324*38c8a9a5SSteve French 			     server->hostname);
325*38c8a9a5SSteve French 	server->tcpStatus = CifsNeedReconnect;
326*38c8a9a5SSteve French 
327*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
328*38c8a9a5SSteve French 	return true;
329*38c8a9a5SSteve French }
330*38c8a9a5SSteve French 
331*38c8a9a5SSteve French /*
332*38c8a9a5SSteve French  * cifs tcp session reconnection
333*38c8a9a5SSteve French  *
334*38c8a9a5SSteve French  * mark tcp session as reconnecting so temporarily locked
335*38c8a9a5SSteve French  * mark all smb sessions as reconnecting for tcp session
336*38c8a9a5SSteve French  * reconnect tcp session
337*38c8a9a5SSteve French  * wake up waiters on reconnection? - (not needed currently)
338*38c8a9a5SSteve French  *
339*38c8a9a5SSteve French  * if mark_smb_session is passed as true, unconditionally mark
340*38c8a9a5SSteve French  * the smb session (and tcon) for reconnect as well. This value
341*38c8a9a5SSteve French  * doesn't really matter for non-multichannel scenario.
342*38c8a9a5SSteve French  *
343*38c8a9a5SSteve French  */
344*38c8a9a5SSteve French static int __cifs_reconnect(struct TCP_Server_Info *server,
345*38c8a9a5SSteve French 			    bool mark_smb_session)
346*38c8a9a5SSteve French {
347*38c8a9a5SSteve French 	int rc = 0;
348*38c8a9a5SSteve French 
349*38c8a9a5SSteve French 	if (!cifs_tcp_ses_needs_reconnect(server, 1))
350*38c8a9a5SSteve French 		return 0;
351*38c8a9a5SSteve French 
352*38c8a9a5SSteve French 	cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
353*38c8a9a5SSteve French 
354*38c8a9a5SSteve French 	cifs_abort_connection(server);
355*38c8a9a5SSteve French 
356*38c8a9a5SSteve French 	do {
357*38c8a9a5SSteve French 		try_to_freeze();
358*38c8a9a5SSteve French 		cifs_server_lock(server);
359*38c8a9a5SSteve French 
360*38c8a9a5SSteve French 		if (!cifs_swn_set_server_dstaddr(server)) {
361*38c8a9a5SSteve French 			/* resolve the hostname again to make sure that IP address is up-to-date */
362*38c8a9a5SSteve French 			rc = reconn_set_ipaddr_from_hostname(server);
363*38c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
364*38c8a9a5SSteve French 		}
365*38c8a9a5SSteve French 
366*38c8a9a5SSteve French 		if (cifs_rdma_enabled(server))
367*38c8a9a5SSteve French 			rc = smbd_reconnect(server);
368*38c8a9a5SSteve French 		else
369*38c8a9a5SSteve French 			rc = generic_ip_connect(server);
370*38c8a9a5SSteve French 		if (rc) {
371*38c8a9a5SSteve French 			cifs_server_unlock(server);
372*38c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
373*38c8a9a5SSteve French 			msleep(3000);
374*38c8a9a5SSteve French 		} else {
375*38c8a9a5SSteve French 			atomic_inc(&tcpSesReconnectCount);
376*38c8a9a5SSteve French 			set_credits(server, 1);
377*38c8a9a5SSteve French 			spin_lock(&server->srv_lock);
378*38c8a9a5SSteve French 			if (server->tcpStatus != CifsExiting)
379*38c8a9a5SSteve French 				server->tcpStatus = CifsNeedNegotiate;
380*38c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
381*38c8a9a5SSteve French 			cifs_swn_reset_server_dstaddr(server);
382*38c8a9a5SSteve French 			cifs_server_unlock(server);
383*38c8a9a5SSteve French 			mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
384*38c8a9a5SSteve French 		}
385*38c8a9a5SSteve French 	} while (server->tcpStatus == CifsNeedReconnect);
386*38c8a9a5SSteve French 
387*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
388*38c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedNegotiate)
389*38c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
390*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
391*38c8a9a5SSteve French 
392*38c8a9a5SSteve French 	wake_up(&server->response_q);
393*38c8a9a5SSteve French 	return rc;
394*38c8a9a5SSteve French }
395*38c8a9a5SSteve French 
396*38c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
397*38c8a9a5SSteve French static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
398*38c8a9a5SSteve French {
399*38c8a9a5SSteve French 	int rc;
400*38c8a9a5SSteve French 	char *hostname;
401*38c8a9a5SSteve French 
402*38c8a9a5SSteve French 	if (!cifs_swn_set_server_dstaddr(server)) {
403*38c8a9a5SSteve French 		if (server->hostname != target) {
404*38c8a9a5SSteve French 			hostname = extract_hostname(target);
405*38c8a9a5SSteve French 			if (!IS_ERR(hostname)) {
406*38c8a9a5SSteve French 				spin_lock(&server->srv_lock);
407*38c8a9a5SSteve French 				kfree(server->hostname);
408*38c8a9a5SSteve French 				server->hostname = hostname;
409*38c8a9a5SSteve French 				spin_unlock(&server->srv_lock);
410*38c8a9a5SSteve French 			} else {
411*38c8a9a5SSteve French 				cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
412*38c8a9a5SSteve French 					 __func__, PTR_ERR(hostname));
413*38c8a9a5SSteve French 				cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
414*38c8a9a5SSteve French 					 server->hostname);
415*38c8a9a5SSteve French 			}
416*38c8a9a5SSteve French 		}
417*38c8a9a5SSteve French 		/* resolve the hostname again to make sure that IP address is up-to-date. */
418*38c8a9a5SSteve French 		rc = reconn_set_ipaddr_from_hostname(server);
419*38c8a9a5SSteve French 		cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
420*38c8a9a5SSteve French 	}
421*38c8a9a5SSteve French 	/* Reconnect the socket */
422*38c8a9a5SSteve French 	if (cifs_rdma_enabled(server))
423*38c8a9a5SSteve French 		rc = smbd_reconnect(server);
424*38c8a9a5SSteve French 	else
425*38c8a9a5SSteve French 		rc = generic_ip_connect(server);
426*38c8a9a5SSteve French 
427*38c8a9a5SSteve French 	return rc;
428*38c8a9a5SSteve French }
429*38c8a9a5SSteve French 
430*38c8a9a5SSteve French static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
431*38c8a9a5SSteve French 				     struct dfs_cache_tgt_iterator **target_hint)
432*38c8a9a5SSteve French {
433*38c8a9a5SSteve French 	int rc;
434*38c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
435*38c8a9a5SSteve French 
436*38c8a9a5SSteve French 	*target_hint = NULL;
437*38c8a9a5SSteve French 
438*38c8a9a5SSteve French 	/* If dfs target list is empty, then reconnect to last server */
439*38c8a9a5SSteve French 	tit = dfs_cache_get_tgt_iterator(tl);
440*38c8a9a5SSteve French 	if (!tit)
441*38c8a9a5SSteve French 		return __reconnect_target_unlocked(server, server->hostname);
442*38c8a9a5SSteve French 
443*38c8a9a5SSteve French 	/* Otherwise, try every dfs target in @tl */
444*38c8a9a5SSteve French 	for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
445*38c8a9a5SSteve French 		rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
446*38c8a9a5SSteve French 		if (!rc) {
447*38c8a9a5SSteve French 			*target_hint = tit;
448*38c8a9a5SSteve French 			break;
449*38c8a9a5SSteve French 		}
450*38c8a9a5SSteve French 	}
451*38c8a9a5SSteve French 	return rc;
452*38c8a9a5SSteve French }
453*38c8a9a5SSteve French 
454*38c8a9a5SSteve French static int reconnect_dfs_server(struct TCP_Server_Info *server)
455*38c8a9a5SSteve French {
456*38c8a9a5SSteve French 	int rc = 0;
457*38c8a9a5SSteve French 	struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
458*38c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *target_hint = NULL;
459*38c8a9a5SSteve French 	int num_targets = 0;
460*38c8a9a5SSteve French 
461*38c8a9a5SSteve French 	/*
462*38c8a9a5SSteve French 	 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
463*38c8a9a5SSteve French 	 *
464*38c8a9a5SSteve French 	 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
465*38c8a9a5SSteve French 	 * targets (server->nr_targets).  It's also possible that the cached referral was cleared
466*38c8a9a5SSteve French 	 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
467*38c8a9a5SSteve French 	 * refreshing the referral, so, in this case, default it to 1.
468*38c8a9a5SSteve French 	 */
469*38c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
470*38c8a9a5SSteve French 	if (!dfs_cache_noreq_find(server->leaf_fullpath + 1, NULL, &tl))
471*38c8a9a5SSteve French 		num_targets = dfs_cache_get_nr_tgts(&tl);
472*38c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
473*38c8a9a5SSteve French 	if (!num_targets)
474*38c8a9a5SSteve French 		num_targets = 1;
475*38c8a9a5SSteve French 
476*38c8a9a5SSteve French 	if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
477*38c8a9a5SSteve French 		return 0;
478*38c8a9a5SSteve French 
479*38c8a9a5SSteve French 	/*
480*38c8a9a5SSteve French 	 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
481*38c8a9a5SSteve French 	 * different server or share during failover.  It could be improved by adding some logic to
482*38c8a9a5SSteve French 	 * only do that in case it connects to a different server or share, though.
483*38c8a9a5SSteve French 	 */
484*38c8a9a5SSteve French 	cifs_mark_tcp_ses_conns_for_reconnect(server, true);
485*38c8a9a5SSteve French 
486*38c8a9a5SSteve French 	cifs_abort_connection(server);
487*38c8a9a5SSteve French 
488*38c8a9a5SSteve French 	do {
489*38c8a9a5SSteve French 		try_to_freeze();
490*38c8a9a5SSteve French 		cifs_server_lock(server);
491*38c8a9a5SSteve French 
492*38c8a9a5SSteve French 		rc = reconnect_target_unlocked(server, &tl, &target_hint);
493*38c8a9a5SSteve French 		if (rc) {
494*38c8a9a5SSteve French 			/* Failed to reconnect socket */
495*38c8a9a5SSteve French 			cifs_server_unlock(server);
496*38c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
497*38c8a9a5SSteve French 			msleep(3000);
498*38c8a9a5SSteve French 			continue;
499*38c8a9a5SSteve French 		}
500*38c8a9a5SSteve French 		/*
501*38c8a9a5SSteve French 		 * Socket was created.  Update tcp session status to CifsNeedNegotiate so that a
502*38c8a9a5SSteve French 		 * process waiting for reconnect will know it needs to re-establish session and tcon
503*38c8a9a5SSteve French 		 * through the reconnected target server.
504*38c8a9a5SSteve French 		 */
505*38c8a9a5SSteve French 		atomic_inc(&tcpSesReconnectCount);
506*38c8a9a5SSteve French 		set_credits(server, 1);
507*38c8a9a5SSteve French 		spin_lock(&server->srv_lock);
508*38c8a9a5SSteve French 		if (server->tcpStatus != CifsExiting)
509*38c8a9a5SSteve French 			server->tcpStatus = CifsNeedNegotiate;
510*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
511*38c8a9a5SSteve French 		cifs_swn_reset_server_dstaddr(server);
512*38c8a9a5SSteve French 		cifs_server_unlock(server);
513*38c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
514*38c8a9a5SSteve French 	} while (server->tcpStatus == CifsNeedReconnect);
515*38c8a9a5SSteve French 
516*38c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
517*38c8a9a5SSteve French 	dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, target_hint);
518*38c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
519*38c8a9a5SSteve French 	dfs_cache_free_tgts(&tl);
520*38c8a9a5SSteve French 
521*38c8a9a5SSteve French 	/* Need to set up echo worker again once connection has been established */
522*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
523*38c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedNegotiate)
524*38c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
525*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
526*38c8a9a5SSteve French 
527*38c8a9a5SSteve French 	wake_up(&server->response_q);
528*38c8a9a5SSteve French 	return rc;
529*38c8a9a5SSteve French }
530*38c8a9a5SSteve French 
531*38c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
532*38c8a9a5SSteve French {
533*38c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
534*38c8a9a5SSteve French 	if (!server->leaf_fullpath) {
535*38c8a9a5SSteve French 		mutex_unlock(&server->refpath_lock);
536*38c8a9a5SSteve French 		return __cifs_reconnect(server, mark_smb_session);
537*38c8a9a5SSteve French 	}
538*38c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
539*38c8a9a5SSteve French 
540*38c8a9a5SSteve French 	return reconnect_dfs_server(server);
541*38c8a9a5SSteve French }
542*38c8a9a5SSteve French #else
543*38c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
544*38c8a9a5SSteve French {
545*38c8a9a5SSteve French 	return __cifs_reconnect(server, mark_smb_session);
546*38c8a9a5SSteve French }
547*38c8a9a5SSteve French #endif
548*38c8a9a5SSteve French 
549*38c8a9a5SSteve French static void
550*38c8a9a5SSteve French cifs_echo_request(struct work_struct *work)
551*38c8a9a5SSteve French {
552*38c8a9a5SSteve French 	int rc;
553*38c8a9a5SSteve French 	struct TCP_Server_Info *server = container_of(work,
554*38c8a9a5SSteve French 					struct TCP_Server_Info, echo.work);
555*38c8a9a5SSteve French 
556*38c8a9a5SSteve French 	/*
557*38c8a9a5SSteve French 	 * We cannot send an echo if it is disabled.
558*38c8a9a5SSteve French 	 * Also, no need to ping if we got a response recently.
559*38c8a9a5SSteve French 	 */
560*38c8a9a5SSteve French 
561*38c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedReconnect ||
562*38c8a9a5SSteve French 	    server->tcpStatus == CifsExiting ||
563*38c8a9a5SSteve French 	    server->tcpStatus == CifsNew ||
564*38c8a9a5SSteve French 	    (server->ops->can_echo && !server->ops->can_echo(server)) ||
565*38c8a9a5SSteve French 	    time_before(jiffies, server->lstrp + server->echo_interval - HZ))
566*38c8a9a5SSteve French 		goto requeue_echo;
567*38c8a9a5SSteve French 
568*38c8a9a5SSteve French 	rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
569*38c8a9a5SSteve French 	cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc);
570*38c8a9a5SSteve French 
571*38c8a9a5SSteve French 	/* Check witness registrations */
572*38c8a9a5SSteve French 	cifs_swn_check();
573*38c8a9a5SSteve French 
574*38c8a9a5SSteve French requeue_echo:
575*38c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
576*38c8a9a5SSteve French }
577*38c8a9a5SSteve French 
578*38c8a9a5SSteve French static bool
579*38c8a9a5SSteve French allocate_buffers(struct TCP_Server_Info *server)
580*38c8a9a5SSteve French {
581*38c8a9a5SSteve French 	if (!server->bigbuf) {
582*38c8a9a5SSteve French 		server->bigbuf = (char *)cifs_buf_get();
583*38c8a9a5SSteve French 		if (!server->bigbuf) {
584*38c8a9a5SSteve French 			cifs_server_dbg(VFS, "No memory for large SMB response\n");
585*38c8a9a5SSteve French 			msleep(3000);
586*38c8a9a5SSteve French 			/* retry will check if exiting */
587*38c8a9a5SSteve French 			return false;
588*38c8a9a5SSteve French 		}
589*38c8a9a5SSteve French 	} else if (server->large_buf) {
590*38c8a9a5SSteve French 		/* we are reusing a dirty large buf, clear its start */
591*38c8a9a5SSteve French 		memset(server->bigbuf, 0, HEADER_SIZE(server));
592*38c8a9a5SSteve French 	}
593*38c8a9a5SSteve French 
594*38c8a9a5SSteve French 	if (!server->smallbuf) {
595*38c8a9a5SSteve French 		server->smallbuf = (char *)cifs_small_buf_get();
596*38c8a9a5SSteve French 		if (!server->smallbuf) {
597*38c8a9a5SSteve French 			cifs_server_dbg(VFS, "No memory for SMB response\n");
598*38c8a9a5SSteve French 			msleep(1000);
599*38c8a9a5SSteve French 			/* retry will check if exiting */
600*38c8a9a5SSteve French 			return false;
601*38c8a9a5SSteve French 		}
602*38c8a9a5SSteve French 		/* beginning of smb buffer is cleared in our buf_get */
603*38c8a9a5SSteve French 	} else {
604*38c8a9a5SSteve French 		/* if existing small buf clear beginning */
605*38c8a9a5SSteve French 		memset(server->smallbuf, 0, HEADER_SIZE(server));
606*38c8a9a5SSteve French 	}
607*38c8a9a5SSteve French 
608*38c8a9a5SSteve French 	return true;
609*38c8a9a5SSteve French }
610*38c8a9a5SSteve French 
611*38c8a9a5SSteve French static bool
612*38c8a9a5SSteve French server_unresponsive(struct TCP_Server_Info *server)
613*38c8a9a5SSteve French {
614*38c8a9a5SSteve French 	/*
615*38c8a9a5SSteve French 	 * We need to wait 3 echo intervals to make sure we handle such
616*38c8a9a5SSteve French 	 * situations right:
617*38c8a9a5SSteve French 	 * 1s  client sends a normal SMB request
618*38c8a9a5SSteve French 	 * 2s  client gets a response
619*38c8a9a5SSteve French 	 * 30s echo workqueue job pops, and decides we got a response recently
620*38c8a9a5SSteve French 	 *     and don't need to send another
621*38c8a9a5SSteve French 	 * ...
622*38c8a9a5SSteve French 	 * 65s kernel_recvmsg times out, and we see that we haven't gotten
623*38c8a9a5SSteve French 	 *     a response in >60s.
624*38c8a9a5SSteve French 	 */
625*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
626*38c8a9a5SSteve French 	if ((server->tcpStatus == CifsGood ||
627*38c8a9a5SSteve French 	    server->tcpStatus == CifsNeedNegotiate) &&
628*38c8a9a5SSteve French 	    (!server->ops->can_echo || server->ops->can_echo(server)) &&
629*38c8a9a5SSteve French 	    time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
630*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
631*38c8a9a5SSteve French 		cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
632*38c8a9a5SSteve French 			 (3 * server->echo_interval) / HZ);
633*38c8a9a5SSteve French 		cifs_reconnect(server, false);
634*38c8a9a5SSteve French 		return true;
635*38c8a9a5SSteve French 	}
636*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
637*38c8a9a5SSteve French 
638*38c8a9a5SSteve French 	return false;
639*38c8a9a5SSteve French }
640*38c8a9a5SSteve French 
641*38c8a9a5SSteve French static inline bool
642*38c8a9a5SSteve French zero_credits(struct TCP_Server_Info *server)
643*38c8a9a5SSteve French {
644*38c8a9a5SSteve French 	int val;
645*38c8a9a5SSteve French 
646*38c8a9a5SSteve French 	spin_lock(&server->req_lock);
647*38c8a9a5SSteve French 	val = server->credits + server->echo_credits + server->oplock_credits;
648*38c8a9a5SSteve French 	if (server->in_flight == 0 && val == 0) {
649*38c8a9a5SSteve French 		spin_unlock(&server->req_lock);
650*38c8a9a5SSteve French 		return true;
651*38c8a9a5SSteve French 	}
652*38c8a9a5SSteve French 	spin_unlock(&server->req_lock);
653*38c8a9a5SSteve French 	return false;
654*38c8a9a5SSteve French }
655*38c8a9a5SSteve French 
656*38c8a9a5SSteve French static int
657*38c8a9a5SSteve French cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
658*38c8a9a5SSteve French {
659*38c8a9a5SSteve French 	int length = 0;
660*38c8a9a5SSteve French 	int total_read;
661*38c8a9a5SSteve French 
662*38c8a9a5SSteve French 	for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
663*38c8a9a5SSteve French 		try_to_freeze();
664*38c8a9a5SSteve French 
665*38c8a9a5SSteve French 		/* reconnect if no credits and no requests in flight */
666*38c8a9a5SSteve French 		if (zero_credits(server)) {
667*38c8a9a5SSteve French 			cifs_reconnect(server, false);
668*38c8a9a5SSteve French 			return -ECONNABORTED;
669*38c8a9a5SSteve French 		}
670*38c8a9a5SSteve French 
671*38c8a9a5SSteve French 		if (server_unresponsive(server))
672*38c8a9a5SSteve French 			return -ECONNABORTED;
673*38c8a9a5SSteve French 		if (cifs_rdma_enabled(server) && server->smbd_conn)
674*38c8a9a5SSteve French 			length = smbd_recv(server->smbd_conn, smb_msg);
675*38c8a9a5SSteve French 		else
676*38c8a9a5SSteve French 			length = sock_recvmsg(server->ssocket, smb_msg, 0);
677*38c8a9a5SSteve French 
678*38c8a9a5SSteve French 		spin_lock(&server->srv_lock);
679*38c8a9a5SSteve French 		if (server->tcpStatus == CifsExiting) {
680*38c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
681*38c8a9a5SSteve French 			return -ESHUTDOWN;
682*38c8a9a5SSteve French 		}
683*38c8a9a5SSteve French 
684*38c8a9a5SSteve French 		if (server->tcpStatus == CifsNeedReconnect) {
685*38c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
686*38c8a9a5SSteve French 			cifs_reconnect(server, false);
687*38c8a9a5SSteve French 			return -ECONNABORTED;
688*38c8a9a5SSteve French 		}
689*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
690*38c8a9a5SSteve French 
691*38c8a9a5SSteve French 		if (length == -ERESTARTSYS ||
692*38c8a9a5SSteve French 		    length == -EAGAIN ||
693*38c8a9a5SSteve French 		    length == -EINTR) {
694*38c8a9a5SSteve French 			/*
695*38c8a9a5SSteve French 			 * Minimum sleep to prevent looping, allowing socket
696*38c8a9a5SSteve French 			 * to clear and app threads to set tcpStatus
697*38c8a9a5SSteve French 			 * CifsNeedReconnect if server hung.
698*38c8a9a5SSteve French 			 */
699*38c8a9a5SSteve French 			usleep_range(1000, 2000);
700*38c8a9a5SSteve French 			length = 0;
701*38c8a9a5SSteve French 			continue;
702*38c8a9a5SSteve French 		}
703*38c8a9a5SSteve French 
704*38c8a9a5SSteve French 		if (length <= 0) {
705*38c8a9a5SSteve French 			cifs_dbg(FYI, "Received no data or error: %d\n", length);
706*38c8a9a5SSteve French 			cifs_reconnect(server, false);
707*38c8a9a5SSteve French 			return -ECONNABORTED;
708*38c8a9a5SSteve French 		}
709*38c8a9a5SSteve French 	}
710*38c8a9a5SSteve French 	return total_read;
711*38c8a9a5SSteve French }
712*38c8a9a5SSteve French 
713*38c8a9a5SSteve French int
714*38c8a9a5SSteve French cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
715*38c8a9a5SSteve French 		      unsigned int to_read)
716*38c8a9a5SSteve French {
717*38c8a9a5SSteve French 	struct msghdr smb_msg = {};
718*38c8a9a5SSteve French 	struct kvec iov = {.iov_base = buf, .iov_len = to_read};
719*38c8a9a5SSteve French 	iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read);
720*38c8a9a5SSteve French 
721*38c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
722*38c8a9a5SSteve French }
723*38c8a9a5SSteve French 
724*38c8a9a5SSteve French ssize_t
725*38c8a9a5SSteve French cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
726*38c8a9a5SSteve French {
727*38c8a9a5SSteve French 	struct msghdr smb_msg = {};
728*38c8a9a5SSteve French 
729*38c8a9a5SSteve French 	/*
730*38c8a9a5SSteve French 	 *  iov_iter_discard already sets smb_msg.type and count and iov_offset
731*38c8a9a5SSteve French 	 *  and cifs_readv_from_socket sets msg_control and msg_controllen
732*38c8a9a5SSteve French 	 *  so little to initialize in struct msghdr
733*38c8a9a5SSteve French 	 */
734*38c8a9a5SSteve French 	iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read);
735*38c8a9a5SSteve French 
736*38c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
737*38c8a9a5SSteve French }
738*38c8a9a5SSteve French 
739*38c8a9a5SSteve French int
740*38c8a9a5SSteve French cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
741*38c8a9a5SSteve French 	unsigned int page_offset, unsigned int to_read)
742*38c8a9a5SSteve French {
743*38c8a9a5SSteve French 	struct msghdr smb_msg = {};
744*38c8a9a5SSteve French 	struct bio_vec bv;
745*38c8a9a5SSteve French 
746*38c8a9a5SSteve French 	bvec_set_page(&bv, page, to_read, page_offset);
747*38c8a9a5SSteve French 	iov_iter_bvec(&smb_msg.msg_iter, ITER_DEST, &bv, 1, to_read);
748*38c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
749*38c8a9a5SSteve French }
750*38c8a9a5SSteve French 
751*38c8a9a5SSteve French int
752*38c8a9a5SSteve French cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter,
753*38c8a9a5SSteve French 			   unsigned int to_read)
754*38c8a9a5SSteve French {
755*38c8a9a5SSteve French 	struct msghdr smb_msg = { .msg_iter = *iter };
756*38c8a9a5SSteve French 	int ret;
757*38c8a9a5SSteve French 
758*38c8a9a5SSteve French 	iov_iter_truncate(&smb_msg.msg_iter, to_read);
759*38c8a9a5SSteve French 	ret = cifs_readv_from_socket(server, &smb_msg);
760*38c8a9a5SSteve French 	if (ret > 0)
761*38c8a9a5SSteve French 		iov_iter_advance(iter, ret);
762*38c8a9a5SSteve French 	return ret;
763*38c8a9a5SSteve French }
764*38c8a9a5SSteve French 
765*38c8a9a5SSteve French static bool
766*38c8a9a5SSteve French is_smb_response(struct TCP_Server_Info *server, unsigned char type)
767*38c8a9a5SSteve French {
768*38c8a9a5SSteve French 	/*
769*38c8a9a5SSteve French 	 * The first byte big endian of the length field,
770*38c8a9a5SSteve French 	 * is actually not part of the length but the type
771*38c8a9a5SSteve French 	 * with the most common, zero, as regular data.
772*38c8a9a5SSteve French 	 */
773*38c8a9a5SSteve French 	switch (type) {
774*38c8a9a5SSteve French 	case RFC1002_SESSION_MESSAGE:
775*38c8a9a5SSteve French 		/* Regular SMB response */
776*38c8a9a5SSteve French 		return true;
777*38c8a9a5SSteve French 	case RFC1002_SESSION_KEEP_ALIVE:
778*38c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 session keep alive\n");
779*38c8a9a5SSteve French 		break;
780*38c8a9a5SSteve French 	case RFC1002_POSITIVE_SESSION_RESPONSE:
781*38c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 positive session response\n");
782*38c8a9a5SSteve French 		break;
783*38c8a9a5SSteve French 	case RFC1002_NEGATIVE_SESSION_RESPONSE:
784*38c8a9a5SSteve French 		/*
785*38c8a9a5SSteve French 		 * We get this from Windows 98 instead of an error on
786*38c8a9a5SSteve French 		 * SMB negprot response.
787*38c8a9a5SSteve French 		 */
788*38c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 negative session response\n");
789*38c8a9a5SSteve French 		/* give server a second to clean up */
790*38c8a9a5SSteve French 		msleep(1000);
791*38c8a9a5SSteve French 		/*
792*38c8a9a5SSteve French 		 * Always try 445 first on reconnect since we get NACK
793*38c8a9a5SSteve French 		 * on some if we ever connected to port 139 (the NACK
794*38c8a9a5SSteve French 		 * is since we do not begin with RFC1001 session
795*38c8a9a5SSteve French 		 * initialize frame).
796*38c8a9a5SSteve French 		 */
797*38c8a9a5SSteve French 		cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
798*38c8a9a5SSteve French 		cifs_reconnect(server, true);
799*38c8a9a5SSteve French 		break;
800*38c8a9a5SSteve French 	default:
801*38c8a9a5SSteve French 		cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
802*38c8a9a5SSteve French 		cifs_reconnect(server, true);
803*38c8a9a5SSteve French 	}
804*38c8a9a5SSteve French 
805*38c8a9a5SSteve French 	return false;
806*38c8a9a5SSteve French }
807*38c8a9a5SSteve French 
808*38c8a9a5SSteve French void
809*38c8a9a5SSteve French dequeue_mid(struct mid_q_entry *mid, bool malformed)
810*38c8a9a5SSteve French {
811*38c8a9a5SSteve French #ifdef CONFIG_CIFS_STATS2
812*38c8a9a5SSteve French 	mid->when_received = jiffies;
813*38c8a9a5SSteve French #endif
814*38c8a9a5SSteve French 	spin_lock(&mid->server->mid_lock);
815*38c8a9a5SSteve French 	if (!malformed)
816*38c8a9a5SSteve French 		mid->mid_state = MID_RESPONSE_RECEIVED;
817*38c8a9a5SSteve French 	else
818*38c8a9a5SSteve French 		mid->mid_state = MID_RESPONSE_MALFORMED;
819*38c8a9a5SSteve French 	/*
820*38c8a9a5SSteve French 	 * Trying to handle/dequeue a mid after the send_recv()
821*38c8a9a5SSteve French 	 * function has finished processing it is a bug.
822*38c8a9a5SSteve French 	 */
823*38c8a9a5SSteve French 	if (mid->mid_flags & MID_DELETED) {
824*38c8a9a5SSteve French 		spin_unlock(&mid->server->mid_lock);
825*38c8a9a5SSteve French 		pr_warn_once("trying to dequeue a deleted mid\n");
826*38c8a9a5SSteve French 	} else {
827*38c8a9a5SSteve French 		list_del_init(&mid->qhead);
828*38c8a9a5SSteve French 		mid->mid_flags |= MID_DELETED;
829*38c8a9a5SSteve French 		spin_unlock(&mid->server->mid_lock);
830*38c8a9a5SSteve French 	}
831*38c8a9a5SSteve French }
832*38c8a9a5SSteve French 
833*38c8a9a5SSteve French static unsigned int
834*38c8a9a5SSteve French smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
835*38c8a9a5SSteve French {
836*38c8a9a5SSteve French 	struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
837*38c8a9a5SSteve French 
838*38c8a9a5SSteve French 	/*
839*38c8a9a5SSteve French 	 * SMB1 does not use credits.
840*38c8a9a5SSteve French 	 */
841*38c8a9a5SSteve French 	if (is_smb1(server))
842*38c8a9a5SSteve French 		return 0;
843*38c8a9a5SSteve French 
844*38c8a9a5SSteve French 	return le16_to_cpu(shdr->CreditRequest);
845*38c8a9a5SSteve French }
846*38c8a9a5SSteve French 
847*38c8a9a5SSteve French static void
848*38c8a9a5SSteve French handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
849*38c8a9a5SSteve French 	   char *buf, int malformed)
850*38c8a9a5SSteve French {
851*38c8a9a5SSteve French 	if (server->ops->check_trans2 &&
852*38c8a9a5SSteve French 	    server->ops->check_trans2(mid, server, buf, malformed))
853*38c8a9a5SSteve French 		return;
854*38c8a9a5SSteve French 	mid->credits_received = smb2_get_credits_from_hdr(buf, server);
855*38c8a9a5SSteve French 	mid->resp_buf = buf;
856*38c8a9a5SSteve French 	mid->large_buf = server->large_buf;
857*38c8a9a5SSteve French 	/* Was previous buf put in mpx struct for multi-rsp? */
858*38c8a9a5SSteve French 	if (!mid->multiRsp) {
859*38c8a9a5SSteve French 		/* smb buffer will be freed by user thread */
860*38c8a9a5SSteve French 		if (server->large_buf)
861*38c8a9a5SSteve French 			server->bigbuf = NULL;
862*38c8a9a5SSteve French 		else
863*38c8a9a5SSteve French 			server->smallbuf = NULL;
864*38c8a9a5SSteve French 	}
865*38c8a9a5SSteve French 	dequeue_mid(mid, malformed);
866*38c8a9a5SSteve French }
867*38c8a9a5SSteve French 
868*38c8a9a5SSteve French int
869*38c8a9a5SSteve French cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
870*38c8a9a5SSteve French {
871*38c8a9a5SSteve French 	bool srv_sign_required = server->sec_mode & server->vals->signing_required;
872*38c8a9a5SSteve French 	bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
873*38c8a9a5SSteve French 	bool mnt_sign_enabled;
874*38c8a9a5SSteve French 
875*38c8a9a5SSteve French 	/*
876*38c8a9a5SSteve French 	 * Is signing required by mnt options? If not then check
877*38c8a9a5SSteve French 	 * global_secflags to see if it is there.
878*38c8a9a5SSteve French 	 */
879*38c8a9a5SSteve French 	if (!mnt_sign_required)
880*38c8a9a5SSteve French 		mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
881*38c8a9a5SSteve French 						CIFSSEC_MUST_SIGN);
882*38c8a9a5SSteve French 
883*38c8a9a5SSteve French 	/*
884*38c8a9a5SSteve French 	 * If signing is required then it's automatically enabled too,
885*38c8a9a5SSteve French 	 * otherwise, check to see if the secflags allow it.
886*38c8a9a5SSteve French 	 */
887*38c8a9a5SSteve French 	mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
888*38c8a9a5SSteve French 				(global_secflags & CIFSSEC_MAY_SIGN);
889*38c8a9a5SSteve French 
890*38c8a9a5SSteve French 	/* If server requires signing, does client allow it? */
891*38c8a9a5SSteve French 	if (srv_sign_required) {
892*38c8a9a5SSteve French 		if (!mnt_sign_enabled) {
893*38c8a9a5SSteve French 			cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n");
894*38c8a9a5SSteve French 			return -EOPNOTSUPP;
895*38c8a9a5SSteve French 		}
896*38c8a9a5SSteve French 		server->sign = true;
897*38c8a9a5SSteve French 	}
898*38c8a9a5SSteve French 
899*38c8a9a5SSteve French 	/* If client requires signing, does server allow it? */
900*38c8a9a5SSteve French 	if (mnt_sign_required) {
901*38c8a9a5SSteve French 		if (!srv_sign_enabled) {
902*38c8a9a5SSteve French 			cifs_dbg(VFS, "Server does not support signing!\n");
903*38c8a9a5SSteve French 			return -EOPNOTSUPP;
904*38c8a9a5SSteve French 		}
905*38c8a9a5SSteve French 		server->sign = true;
906*38c8a9a5SSteve French 	}
907*38c8a9a5SSteve French 
908*38c8a9a5SSteve French 	if (cifs_rdma_enabled(server) && server->sign)
909*38c8a9a5SSteve French 		cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n");
910*38c8a9a5SSteve French 
911*38c8a9a5SSteve French 	return 0;
912*38c8a9a5SSteve French }
913*38c8a9a5SSteve French 
914*38c8a9a5SSteve French 
915*38c8a9a5SSteve French static void clean_demultiplex_info(struct TCP_Server_Info *server)
916*38c8a9a5SSteve French {
917*38c8a9a5SSteve French 	int length;
918*38c8a9a5SSteve French 
919*38c8a9a5SSteve French 	/* take it off the list, if it's not already */
920*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
921*38c8a9a5SSteve French 	list_del_init(&server->tcp_ses_list);
922*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
923*38c8a9a5SSteve French 
924*38c8a9a5SSteve French 	cancel_delayed_work_sync(&server->echo);
925*38c8a9a5SSteve French 
926*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
927*38c8a9a5SSteve French 	server->tcpStatus = CifsExiting;
928*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
929*38c8a9a5SSteve French 	wake_up_all(&server->response_q);
930*38c8a9a5SSteve French 
931*38c8a9a5SSteve French 	/* check if we have blocked requests that need to free */
932*38c8a9a5SSteve French 	spin_lock(&server->req_lock);
933*38c8a9a5SSteve French 	if (server->credits <= 0)
934*38c8a9a5SSteve French 		server->credits = 1;
935*38c8a9a5SSteve French 	spin_unlock(&server->req_lock);
936*38c8a9a5SSteve French 	/*
937*38c8a9a5SSteve French 	 * Although there should not be any requests blocked on this queue it
938*38c8a9a5SSteve French 	 * can not hurt to be paranoid and try to wake up requests that may
939*38c8a9a5SSteve French 	 * haven been blocked when more than 50 at time were on the wire to the
940*38c8a9a5SSteve French 	 * same server - they now will see the session is in exit state and get
941*38c8a9a5SSteve French 	 * out of SendReceive.
942*38c8a9a5SSteve French 	 */
943*38c8a9a5SSteve French 	wake_up_all(&server->request_q);
944*38c8a9a5SSteve French 	/* give those requests time to exit */
945*38c8a9a5SSteve French 	msleep(125);
946*38c8a9a5SSteve French 	if (cifs_rdma_enabled(server))
947*38c8a9a5SSteve French 		smbd_destroy(server);
948*38c8a9a5SSteve French 	if (server->ssocket) {
949*38c8a9a5SSteve French 		sock_release(server->ssocket);
950*38c8a9a5SSteve French 		server->ssocket = NULL;
951*38c8a9a5SSteve French 	}
952*38c8a9a5SSteve French 
953*38c8a9a5SSteve French 	if (!list_empty(&server->pending_mid_q)) {
954*38c8a9a5SSteve French 		struct list_head dispose_list;
955*38c8a9a5SSteve French 		struct mid_q_entry *mid_entry;
956*38c8a9a5SSteve French 		struct list_head *tmp, *tmp2;
957*38c8a9a5SSteve French 
958*38c8a9a5SSteve French 		INIT_LIST_HEAD(&dispose_list);
959*38c8a9a5SSteve French 		spin_lock(&server->mid_lock);
960*38c8a9a5SSteve French 		list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
961*38c8a9a5SSteve French 			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
962*38c8a9a5SSteve French 			cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
963*38c8a9a5SSteve French 			kref_get(&mid_entry->refcount);
964*38c8a9a5SSteve French 			mid_entry->mid_state = MID_SHUTDOWN;
965*38c8a9a5SSteve French 			list_move(&mid_entry->qhead, &dispose_list);
966*38c8a9a5SSteve French 			mid_entry->mid_flags |= MID_DELETED;
967*38c8a9a5SSteve French 		}
968*38c8a9a5SSteve French 		spin_unlock(&server->mid_lock);
969*38c8a9a5SSteve French 
970*38c8a9a5SSteve French 		/* now walk dispose list and issue callbacks */
971*38c8a9a5SSteve French 		list_for_each_safe(tmp, tmp2, &dispose_list) {
972*38c8a9a5SSteve French 			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
973*38c8a9a5SSteve French 			cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
974*38c8a9a5SSteve French 			list_del_init(&mid_entry->qhead);
975*38c8a9a5SSteve French 			mid_entry->callback(mid_entry);
976*38c8a9a5SSteve French 			release_mid(mid_entry);
977*38c8a9a5SSteve French 		}
978*38c8a9a5SSteve French 		/* 1/8th of sec is more than enough time for them to exit */
979*38c8a9a5SSteve French 		msleep(125);
980*38c8a9a5SSteve French 	}
981*38c8a9a5SSteve French 
982*38c8a9a5SSteve French 	if (!list_empty(&server->pending_mid_q)) {
983*38c8a9a5SSteve French 		/*
984*38c8a9a5SSteve French 		 * mpx threads have not exited yet give them at least the smb
985*38c8a9a5SSteve French 		 * send timeout time for long ops.
986*38c8a9a5SSteve French 		 *
987*38c8a9a5SSteve French 		 * Due to delays on oplock break requests, we need to wait at
988*38c8a9a5SSteve French 		 * least 45 seconds before giving up on a request getting a
989*38c8a9a5SSteve French 		 * response and going ahead and killing cifsd.
990*38c8a9a5SSteve French 		 */
991*38c8a9a5SSteve French 		cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
992*38c8a9a5SSteve French 		msleep(46000);
993*38c8a9a5SSteve French 		/*
994*38c8a9a5SSteve French 		 * If threads still have not exited they are probably never
995*38c8a9a5SSteve French 		 * coming home not much else we can do but free the memory.
996*38c8a9a5SSteve French 		 */
997*38c8a9a5SSteve French 	}
998*38c8a9a5SSteve French 
999*38c8a9a5SSteve French 	kfree(server->origin_fullpath);
1000*38c8a9a5SSteve French 	kfree(server->leaf_fullpath);
1001*38c8a9a5SSteve French 	kfree(server);
1002*38c8a9a5SSteve French 
1003*38c8a9a5SSteve French 	length = atomic_dec_return(&tcpSesAllocCount);
1004*38c8a9a5SSteve French 	if (length > 0)
1005*38c8a9a5SSteve French 		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1006*38c8a9a5SSteve French }
1007*38c8a9a5SSteve French 
1008*38c8a9a5SSteve French static int
1009*38c8a9a5SSteve French standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1010*38c8a9a5SSteve French {
1011*38c8a9a5SSteve French 	int length;
1012*38c8a9a5SSteve French 	char *buf = server->smallbuf;
1013*38c8a9a5SSteve French 	unsigned int pdu_length = server->pdu_size;
1014*38c8a9a5SSteve French 
1015*38c8a9a5SSteve French 	/* make sure this will fit in a large buffer */
1016*38c8a9a5SSteve French 	if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
1017*38c8a9a5SSteve French 	    HEADER_PREAMBLE_SIZE(server)) {
1018*38c8a9a5SSteve French 		cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
1019*38c8a9a5SSteve French 		cifs_reconnect(server, true);
1020*38c8a9a5SSteve French 		return -ECONNABORTED;
1021*38c8a9a5SSteve French 	}
1022*38c8a9a5SSteve French 
1023*38c8a9a5SSteve French 	/* switch to large buffer if too big for a small one */
1024*38c8a9a5SSteve French 	if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
1025*38c8a9a5SSteve French 		server->large_buf = true;
1026*38c8a9a5SSteve French 		memcpy(server->bigbuf, buf, server->total_read);
1027*38c8a9a5SSteve French 		buf = server->bigbuf;
1028*38c8a9a5SSteve French 	}
1029*38c8a9a5SSteve French 
1030*38c8a9a5SSteve French 	/* now read the rest */
1031*38c8a9a5SSteve French 	length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
1032*38c8a9a5SSteve French 				       pdu_length - MID_HEADER_SIZE(server));
1033*38c8a9a5SSteve French 
1034*38c8a9a5SSteve French 	if (length < 0)
1035*38c8a9a5SSteve French 		return length;
1036*38c8a9a5SSteve French 	server->total_read += length;
1037*38c8a9a5SSteve French 
1038*38c8a9a5SSteve French 	dump_smb(buf, server->total_read);
1039*38c8a9a5SSteve French 
1040*38c8a9a5SSteve French 	return cifs_handle_standard(server, mid);
1041*38c8a9a5SSteve French }
1042*38c8a9a5SSteve French 
1043*38c8a9a5SSteve French int
1044*38c8a9a5SSteve French cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1045*38c8a9a5SSteve French {
1046*38c8a9a5SSteve French 	char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
1047*38c8a9a5SSteve French 	int rc;
1048*38c8a9a5SSteve French 
1049*38c8a9a5SSteve French 	/*
1050*38c8a9a5SSteve French 	 * We know that we received enough to get to the MID as we
1051*38c8a9a5SSteve French 	 * checked the pdu_length earlier. Now check to see
1052*38c8a9a5SSteve French 	 * if the rest of the header is OK.
1053*38c8a9a5SSteve French 	 *
1054*38c8a9a5SSteve French 	 * 48 bytes is enough to display the header and a little bit
1055*38c8a9a5SSteve French 	 * into the payload for debugging purposes.
1056*38c8a9a5SSteve French 	 */
1057*38c8a9a5SSteve French 	rc = server->ops->check_message(buf, server->total_read, server);
1058*38c8a9a5SSteve French 	if (rc)
1059*38c8a9a5SSteve French 		cifs_dump_mem("Bad SMB: ", buf,
1060*38c8a9a5SSteve French 			min_t(unsigned int, server->total_read, 48));
1061*38c8a9a5SSteve French 
1062*38c8a9a5SSteve French 	if (server->ops->is_session_expired &&
1063*38c8a9a5SSteve French 	    server->ops->is_session_expired(buf)) {
1064*38c8a9a5SSteve French 		cifs_reconnect(server, true);
1065*38c8a9a5SSteve French 		return -1;
1066*38c8a9a5SSteve French 	}
1067*38c8a9a5SSteve French 
1068*38c8a9a5SSteve French 	if (server->ops->is_status_pending &&
1069*38c8a9a5SSteve French 	    server->ops->is_status_pending(buf, server))
1070*38c8a9a5SSteve French 		return -1;
1071*38c8a9a5SSteve French 
1072*38c8a9a5SSteve French 	if (!mid)
1073*38c8a9a5SSteve French 		return rc;
1074*38c8a9a5SSteve French 
1075*38c8a9a5SSteve French 	handle_mid(mid, server, buf, rc);
1076*38c8a9a5SSteve French 	return 0;
1077*38c8a9a5SSteve French }
1078*38c8a9a5SSteve French 
1079*38c8a9a5SSteve French static void
1080*38c8a9a5SSteve French smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
1081*38c8a9a5SSteve French {
1082*38c8a9a5SSteve French 	struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
1083*38c8a9a5SSteve French 	int scredits, in_flight;
1084*38c8a9a5SSteve French 
1085*38c8a9a5SSteve French 	/*
1086*38c8a9a5SSteve French 	 * SMB1 does not use credits.
1087*38c8a9a5SSteve French 	 */
1088*38c8a9a5SSteve French 	if (is_smb1(server))
1089*38c8a9a5SSteve French 		return;
1090*38c8a9a5SSteve French 
1091*38c8a9a5SSteve French 	if (shdr->CreditRequest) {
1092*38c8a9a5SSteve French 		spin_lock(&server->req_lock);
1093*38c8a9a5SSteve French 		server->credits += le16_to_cpu(shdr->CreditRequest);
1094*38c8a9a5SSteve French 		scredits = server->credits;
1095*38c8a9a5SSteve French 		in_flight = server->in_flight;
1096*38c8a9a5SSteve French 		spin_unlock(&server->req_lock);
1097*38c8a9a5SSteve French 		wake_up(&server->request_q);
1098*38c8a9a5SSteve French 
1099*38c8a9a5SSteve French 		trace_smb3_hdr_credits(server->CurrentMid,
1100*38c8a9a5SSteve French 				server->conn_id, server->hostname, scredits,
1101*38c8a9a5SSteve French 				le16_to_cpu(shdr->CreditRequest), in_flight);
1102*38c8a9a5SSteve French 		cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
1103*38c8a9a5SSteve French 				__func__, le16_to_cpu(shdr->CreditRequest),
1104*38c8a9a5SSteve French 				scredits);
1105*38c8a9a5SSteve French 	}
1106*38c8a9a5SSteve French }
1107*38c8a9a5SSteve French 
1108*38c8a9a5SSteve French 
1109*38c8a9a5SSteve French static int
1110*38c8a9a5SSteve French cifs_demultiplex_thread(void *p)
1111*38c8a9a5SSteve French {
1112*38c8a9a5SSteve French 	int i, num_mids, length;
1113*38c8a9a5SSteve French 	struct TCP_Server_Info *server = p;
1114*38c8a9a5SSteve French 	unsigned int pdu_length;
1115*38c8a9a5SSteve French 	unsigned int next_offset;
1116*38c8a9a5SSteve French 	char *buf = NULL;
1117*38c8a9a5SSteve French 	struct task_struct *task_to_wake = NULL;
1118*38c8a9a5SSteve French 	struct mid_q_entry *mids[MAX_COMPOUND];
1119*38c8a9a5SSteve French 	char *bufs[MAX_COMPOUND];
1120*38c8a9a5SSteve French 	unsigned int noreclaim_flag, num_io_timeout = 0;
1121*38c8a9a5SSteve French 
1122*38c8a9a5SSteve French 	noreclaim_flag = memalloc_noreclaim_save();
1123*38c8a9a5SSteve French 	cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
1124*38c8a9a5SSteve French 
1125*38c8a9a5SSteve French 	length = atomic_inc_return(&tcpSesAllocCount);
1126*38c8a9a5SSteve French 	if (length > 1)
1127*38c8a9a5SSteve French 		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1128*38c8a9a5SSteve French 
1129*38c8a9a5SSteve French 	set_freezable();
1130*38c8a9a5SSteve French 	allow_kernel_signal(SIGKILL);
1131*38c8a9a5SSteve French 	while (server->tcpStatus != CifsExiting) {
1132*38c8a9a5SSteve French 		if (try_to_freeze())
1133*38c8a9a5SSteve French 			continue;
1134*38c8a9a5SSteve French 
1135*38c8a9a5SSteve French 		if (!allocate_buffers(server))
1136*38c8a9a5SSteve French 			continue;
1137*38c8a9a5SSteve French 
1138*38c8a9a5SSteve French 		server->large_buf = false;
1139*38c8a9a5SSteve French 		buf = server->smallbuf;
1140*38c8a9a5SSteve French 		pdu_length = 4; /* enough to get RFC1001 header */
1141*38c8a9a5SSteve French 
1142*38c8a9a5SSteve French 		length = cifs_read_from_socket(server, buf, pdu_length);
1143*38c8a9a5SSteve French 		if (length < 0)
1144*38c8a9a5SSteve French 			continue;
1145*38c8a9a5SSteve French 
1146*38c8a9a5SSteve French 		if (is_smb1(server))
1147*38c8a9a5SSteve French 			server->total_read = length;
1148*38c8a9a5SSteve French 		else
1149*38c8a9a5SSteve French 			server->total_read = 0;
1150*38c8a9a5SSteve French 
1151*38c8a9a5SSteve French 		/*
1152*38c8a9a5SSteve French 		 * The right amount was read from socket - 4 bytes,
1153*38c8a9a5SSteve French 		 * so we can now interpret the length field.
1154*38c8a9a5SSteve French 		 */
1155*38c8a9a5SSteve French 		pdu_length = get_rfc1002_length(buf);
1156*38c8a9a5SSteve French 
1157*38c8a9a5SSteve French 		cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
1158*38c8a9a5SSteve French 		if (!is_smb_response(server, buf[0]))
1159*38c8a9a5SSteve French 			continue;
1160*38c8a9a5SSteve French next_pdu:
1161*38c8a9a5SSteve French 		server->pdu_size = pdu_length;
1162*38c8a9a5SSteve French 
1163*38c8a9a5SSteve French 		/* make sure we have enough to get to the MID */
1164*38c8a9a5SSteve French 		if (server->pdu_size < MID_HEADER_SIZE(server)) {
1165*38c8a9a5SSteve French 			cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
1166*38c8a9a5SSteve French 				 server->pdu_size);
1167*38c8a9a5SSteve French 			cifs_reconnect(server, true);
1168*38c8a9a5SSteve French 			continue;
1169*38c8a9a5SSteve French 		}
1170*38c8a9a5SSteve French 
1171*38c8a9a5SSteve French 		/* read down to the MID */
1172*38c8a9a5SSteve French 		length = cifs_read_from_socket(server,
1173*38c8a9a5SSteve French 			     buf + HEADER_PREAMBLE_SIZE(server),
1174*38c8a9a5SSteve French 			     MID_HEADER_SIZE(server));
1175*38c8a9a5SSteve French 		if (length < 0)
1176*38c8a9a5SSteve French 			continue;
1177*38c8a9a5SSteve French 		server->total_read += length;
1178*38c8a9a5SSteve French 
1179*38c8a9a5SSteve French 		if (server->ops->next_header) {
1180*38c8a9a5SSteve French 			next_offset = server->ops->next_header(buf);
1181*38c8a9a5SSteve French 			if (next_offset)
1182*38c8a9a5SSteve French 				server->pdu_size = next_offset;
1183*38c8a9a5SSteve French 		}
1184*38c8a9a5SSteve French 
1185*38c8a9a5SSteve French 		memset(mids, 0, sizeof(mids));
1186*38c8a9a5SSteve French 		memset(bufs, 0, sizeof(bufs));
1187*38c8a9a5SSteve French 		num_mids = 0;
1188*38c8a9a5SSteve French 
1189*38c8a9a5SSteve French 		if (server->ops->is_transform_hdr &&
1190*38c8a9a5SSteve French 		    server->ops->receive_transform &&
1191*38c8a9a5SSteve French 		    server->ops->is_transform_hdr(buf)) {
1192*38c8a9a5SSteve French 			length = server->ops->receive_transform(server,
1193*38c8a9a5SSteve French 								mids,
1194*38c8a9a5SSteve French 								bufs,
1195*38c8a9a5SSteve French 								&num_mids);
1196*38c8a9a5SSteve French 		} else {
1197*38c8a9a5SSteve French 			mids[0] = server->ops->find_mid(server, buf);
1198*38c8a9a5SSteve French 			bufs[0] = buf;
1199*38c8a9a5SSteve French 			num_mids = 1;
1200*38c8a9a5SSteve French 
1201*38c8a9a5SSteve French 			if (!mids[0] || !mids[0]->receive)
1202*38c8a9a5SSteve French 				length = standard_receive3(server, mids[0]);
1203*38c8a9a5SSteve French 			else
1204*38c8a9a5SSteve French 				length = mids[0]->receive(server, mids[0]);
1205*38c8a9a5SSteve French 		}
1206*38c8a9a5SSteve French 
1207*38c8a9a5SSteve French 		if (length < 0) {
1208*38c8a9a5SSteve French 			for (i = 0; i < num_mids; i++)
1209*38c8a9a5SSteve French 				if (mids[i])
1210*38c8a9a5SSteve French 					release_mid(mids[i]);
1211*38c8a9a5SSteve French 			continue;
1212*38c8a9a5SSteve French 		}
1213*38c8a9a5SSteve French 
1214*38c8a9a5SSteve French 		if (server->ops->is_status_io_timeout &&
1215*38c8a9a5SSteve French 		    server->ops->is_status_io_timeout(buf)) {
1216*38c8a9a5SSteve French 			num_io_timeout++;
1217*38c8a9a5SSteve French 			if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1218*38c8a9a5SSteve French 				cifs_reconnect(server, false);
1219*38c8a9a5SSteve French 				num_io_timeout = 0;
1220*38c8a9a5SSteve French 				continue;
1221*38c8a9a5SSteve French 			}
1222*38c8a9a5SSteve French 		}
1223*38c8a9a5SSteve French 
1224*38c8a9a5SSteve French 		server->lstrp = jiffies;
1225*38c8a9a5SSteve French 
1226*38c8a9a5SSteve French 		for (i = 0; i < num_mids; i++) {
1227*38c8a9a5SSteve French 			if (mids[i] != NULL) {
1228*38c8a9a5SSteve French 				mids[i]->resp_buf_size = server->pdu_size;
1229*38c8a9a5SSteve French 
1230*38c8a9a5SSteve French 				if (bufs[i] && server->ops->is_network_name_deleted)
1231*38c8a9a5SSteve French 					server->ops->is_network_name_deleted(bufs[i],
1232*38c8a9a5SSteve French 									server);
1233*38c8a9a5SSteve French 
1234*38c8a9a5SSteve French 				if (!mids[i]->multiRsp || mids[i]->multiEnd)
1235*38c8a9a5SSteve French 					mids[i]->callback(mids[i]);
1236*38c8a9a5SSteve French 
1237*38c8a9a5SSteve French 				release_mid(mids[i]);
1238*38c8a9a5SSteve French 			} else if (server->ops->is_oplock_break &&
1239*38c8a9a5SSteve French 				   server->ops->is_oplock_break(bufs[i],
1240*38c8a9a5SSteve French 								server)) {
1241*38c8a9a5SSteve French 				smb2_add_credits_from_hdr(bufs[i], server);
1242*38c8a9a5SSteve French 				cifs_dbg(FYI, "Received oplock break\n");
1243*38c8a9a5SSteve French 			} else {
1244*38c8a9a5SSteve French 				cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1245*38c8a9a5SSteve French 						atomic_read(&mid_count));
1246*38c8a9a5SSteve French 				cifs_dump_mem("Received Data is: ", bufs[i],
1247*38c8a9a5SSteve French 					      HEADER_SIZE(server));
1248*38c8a9a5SSteve French 				smb2_add_credits_from_hdr(bufs[i], server);
1249*38c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2
1250*38c8a9a5SSteve French 				if (server->ops->dump_detail)
1251*38c8a9a5SSteve French 					server->ops->dump_detail(bufs[i],
1252*38c8a9a5SSteve French 								 server);
1253*38c8a9a5SSteve French 				cifs_dump_mids(server);
1254*38c8a9a5SSteve French #endif /* CIFS_DEBUG2 */
1255*38c8a9a5SSteve French 			}
1256*38c8a9a5SSteve French 		}
1257*38c8a9a5SSteve French 
1258*38c8a9a5SSteve French 		if (pdu_length > server->pdu_size) {
1259*38c8a9a5SSteve French 			if (!allocate_buffers(server))
1260*38c8a9a5SSteve French 				continue;
1261*38c8a9a5SSteve French 			pdu_length -= server->pdu_size;
1262*38c8a9a5SSteve French 			server->total_read = 0;
1263*38c8a9a5SSteve French 			server->large_buf = false;
1264*38c8a9a5SSteve French 			buf = server->smallbuf;
1265*38c8a9a5SSteve French 			goto next_pdu;
1266*38c8a9a5SSteve French 		}
1267*38c8a9a5SSteve French 	} /* end while !EXITING */
1268*38c8a9a5SSteve French 
1269*38c8a9a5SSteve French 	/* buffer usually freed in free_mid - need to free it here on exit */
1270*38c8a9a5SSteve French 	cifs_buf_release(server->bigbuf);
1271*38c8a9a5SSteve French 	if (server->smallbuf) /* no sense logging a debug message if NULL */
1272*38c8a9a5SSteve French 		cifs_small_buf_release(server->smallbuf);
1273*38c8a9a5SSteve French 
1274*38c8a9a5SSteve French 	task_to_wake = xchg(&server->tsk, NULL);
1275*38c8a9a5SSteve French 	clean_demultiplex_info(server);
1276*38c8a9a5SSteve French 
1277*38c8a9a5SSteve French 	/* if server->tsk was NULL then wait for a signal before exiting */
1278*38c8a9a5SSteve French 	if (!task_to_wake) {
1279*38c8a9a5SSteve French 		set_current_state(TASK_INTERRUPTIBLE);
1280*38c8a9a5SSteve French 		while (!signal_pending(current)) {
1281*38c8a9a5SSteve French 			schedule();
1282*38c8a9a5SSteve French 			set_current_state(TASK_INTERRUPTIBLE);
1283*38c8a9a5SSteve French 		}
1284*38c8a9a5SSteve French 		set_current_state(TASK_RUNNING);
1285*38c8a9a5SSteve French 	}
1286*38c8a9a5SSteve French 
1287*38c8a9a5SSteve French 	memalloc_noreclaim_restore(noreclaim_flag);
1288*38c8a9a5SSteve French 	module_put_and_kthread_exit(0);
1289*38c8a9a5SSteve French }
1290*38c8a9a5SSteve French 
1291*38c8a9a5SSteve French /*
1292*38c8a9a5SSteve French  * Returns true if srcaddr isn't specified and rhs isn't specified, or
1293*38c8a9a5SSteve French  * if srcaddr is specified and matches the IP address of the rhs argument
1294*38c8a9a5SSteve French  */
1295*38c8a9a5SSteve French bool
1296*38c8a9a5SSteve French cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
1297*38c8a9a5SSteve French {
1298*38c8a9a5SSteve French 	switch (srcaddr->sa_family) {
1299*38c8a9a5SSteve French 	case AF_UNSPEC:
1300*38c8a9a5SSteve French 		return (rhs->sa_family == AF_UNSPEC);
1301*38c8a9a5SSteve French 	case AF_INET: {
1302*38c8a9a5SSteve French 		struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1303*38c8a9a5SSteve French 		struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1304*38c8a9a5SSteve French 		return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1305*38c8a9a5SSteve French 	}
1306*38c8a9a5SSteve French 	case AF_INET6: {
1307*38c8a9a5SSteve French 		struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1308*38c8a9a5SSteve French 		struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
1309*38c8a9a5SSteve French 		return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr)
1310*38c8a9a5SSteve French 			&& saddr6->sin6_scope_id == vaddr6->sin6_scope_id);
1311*38c8a9a5SSteve French 	}
1312*38c8a9a5SSteve French 	default:
1313*38c8a9a5SSteve French 		WARN_ON(1);
1314*38c8a9a5SSteve French 		return false; /* don't expect to be here */
1315*38c8a9a5SSteve French 	}
1316*38c8a9a5SSteve French }
1317*38c8a9a5SSteve French 
1318*38c8a9a5SSteve French /*
1319*38c8a9a5SSteve French  * If no port is specified in addr structure, we try to match with 445 port
1320*38c8a9a5SSteve French  * and if it fails - with 139 ports. It should be called only if address
1321*38c8a9a5SSteve French  * families of server and addr are equal.
1322*38c8a9a5SSteve French  */
1323*38c8a9a5SSteve French static bool
1324*38c8a9a5SSteve French match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1325*38c8a9a5SSteve French {
1326*38c8a9a5SSteve French 	__be16 port, *sport;
1327*38c8a9a5SSteve French 
1328*38c8a9a5SSteve French 	/* SMBDirect manages its own ports, don't match it here */
1329*38c8a9a5SSteve French 	if (server->rdma)
1330*38c8a9a5SSteve French 		return true;
1331*38c8a9a5SSteve French 
1332*38c8a9a5SSteve French 	switch (addr->sa_family) {
1333*38c8a9a5SSteve French 	case AF_INET:
1334*38c8a9a5SSteve French 		sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1335*38c8a9a5SSteve French 		port = ((struct sockaddr_in *) addr)->sin_port;
1336*38c8a9a5SSteve French 		break;
1337*38c8a9a5SSteve French 	case AF_INET6:
1338*38c8a9a5SSteve French 		sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1339*38c8a9a5SSteve French 		port = ((struct sockaddr_in6 *) addr)->sin6_port;
1340*38c8a9a5SSteve French 		break;
1341*38c8a9a5SSteve French 	default:
1342*38c8a9a5SSteve French 		WARN_ON(1);
1343*38c8a9a5SSteve French 		return false;
1344*38c8a9a5SSteve French 	}
1345*38c8a9a5SSteve French 
1346*38c8a9a5SSteve French 	if (!port) {
1347*38c8a9a5SSteve French 		port = htons(CIFS_PORT);
1348*38c8a9a5SSteve French 		if (port == *sport)
1349*38c8a9a5SSteve French 			return true;
1350*38c8a9a5SSteve French 
1351*38c8a9a5SSteve French 		port = htons(RFC1001_PORT);
1352*38c8a9a5SSteve French 	}
1353*38c8a9a5SSteve French 
1354*38c8a9a5SSteve French 	return port == *sport;
1355*38c8a9a5SSteve French }
1356*38c8a9a5SSteve French 
1357*38c8a9a5SSteve French static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr)
1358*38c8a9a5SSteve French {
1359*38c8a9a5SSteve French 	if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr))
1360*38c8a9a5SSteve French 		return false;
1361*38c8a9a5SSteve French 
1362*38c8a9a5SSteve French 	return true;
1363*38c8a9a5SSteve French }
1364*38c8a9a5SSteve French 
1365*38c8a9a5SSteve French static bool
1366*38c8a9a5SSteve French match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1367*38c8a9a5SSteve French {
1368*38c8a9a5SSteve French 	/*
1369*38c8a9a5SSteve French 	 * The select_sectype function should either return the ctx->sectype
1370*38c8a9a5SSteve French 	 * that was specified, or "Unspecified" if that sectype was not
1371*38c8a9a5SSteve French 	 * compatible with the given NEGOTIATE request.
1372*38c8a9a5SSteve French 	 */
1373*38c8a9a5SSteve French 	if (server->ops->select_sectype(server, ctx->sectype)
1374*38c8a9a5SSteve French 	     == Unspecified)
1375*38c8a9a5SSteve French 		return false;
1376*38c8a9a5SSteve French 
1377*38c8a9a5SSteve French 	/*
1378*38c8a9a5SSteve French 	 * Now check if signing mode is acceptable. No need to check
1379*38c8a9a5SSteve French 	 * global_secflags at this point since if MUST_SIGN is set then
1380*38c8a9a5SSteve French 	 * the server->sign had better be too.
1381*38c8a9a5SSteve French 	 */
1382*38c8a9a5SSteve French 	if (ctx->sign && !server->sign)
1383*38c8a9a5SSteve French 		return false;
1384*38c8a9a5SSteve French 
1385*38c8a9a5SSteve French 	return true;
1386*38c8a9a5SSteve French }
1387*38c8a9a5SSteve French 
1388*38c8a9a5SSteve French /* this function must be called with srv_lock held */
1389*38c8a9a5SSteve French static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1390*38c8a9a5SSteve French {
1391*38c8a9a5SSteve French 	struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
1392*38c8a9a5SSteve French 
1393*38c8a9a5SSteve French 	lockdep_assert_held(&server->srv_lock);
1394*38c8a9a5SSteve French 
1395*38c8a9a5SSteve French 	if (ctx->nosharesock)
1396*38c8a9a5SSteve French 		return 0;
1397*38c8a9a5SSteve French 
1398*38c8a9a5SSteve French 	/* this server does not share socket */
1399*38c8a9a5SSteve French 	if (server->nosharesock)
1400*38c8a9a5SSteve French 		return 0;
1401*38c8a9a5SSteve French 
1402*38c8a9a5SSteve French 	/* If multidialect negotiation see if existing sessions match one */
1403*38c8a9a5SSteve French 	if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
1404*38c8a9a5SSteve French 		if (server->vals->protocol_id < SMB30_PROT_ID)
1405*38c8a9a5SSteve French 			return 0;
1406*38c8a9a5SSteve French 	} else if (strcmp(ctx->vals->version_string,
1407*38c8a9a5SSteve French 		   SMBDEFAULT_VERSION_STRING) == 0) {
1408*38c8a9a5SSteve French 		if (server->vals->protocol_id < SMB21_PROT_ID)
1409*38c8a9a5SSteve French 			return 0;
1410*38c8a9a5SSteve French 	} else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
1411*38c8a9a5SSteve French 		return 0;
1412*38c8a9a5SSteve French 
1413*38c8a9a5SSteve French 	if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1414*38c8a9a5SSteve French 		return 0;
1415*38c8a9a5SSteve French 
1416*38c8a9a5SSteve French 	if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr,
1417*38c8a9a5SSteve French 			       (struct sockaddr *)&server->srcaddr))
1418*38c8a9a5SSteve French 		return 0;
1419*38c8a9a5SSteve French 	/*
1420*38c8a9a5SSteve French 	 * - Match for an DFS tcon (@server->origin_fullpath).
1421*38c8a9a5SSteve French 	 * - Match for an DFS root server connection (@server->leaf_fullpath).
1422*38c8a9a5SSteve French 	 * - If none of the above and @ctx->leaf_fullpath is set, then
1423*38c8a9a5SSteve French 	 *   it is a new DFS connection.
1424*38c8a9a5SSteve French 	 * - If 'nodfs' mount option was passed, then match only connections
1425*38c8a9a5SSteve French 	 *   that have no DFS referrals set
1426*38c8a9a5SSteve French 	 *   (e.g. can't failover to other targets).
1427*38c8a9a5SSteve French 	 */
1428*38c8a9a5SSteve French 	if (!ctx->nodfs) {
1429*38c8a9a5SSteve French 		if (ctx->source && server->origin_fullpath) {
1430*38c8a9a5SSteve French 			if (!dfs_src_pathname_equal(ctx->source,
1431*38c8a9a5SSteve French 						    server->origin_fullpath))
1432*38c8a9a5SSteve French 				return 0;
1433*38c8a9a5SSteve French 		} else if (server->leaf_fullpath) {
1434*38c8a9a5SSteve French 			if (!ctx->leaf_fullpath ||
1435*38c8a9a5SSteve French 			    strcasecmp(server->leaf_fullpath,
1436*38c8a9a5SSteve French 				       ctx->leaf_fullpath))
1437*38c8a9a5SSteve French 				return 0;
1438*38c8a9a5SSteve French 		} else if (ctx->leaf_fullpath) {
1439*38c8a9a5SSteve French 			return 0;
1440*38c8a9a5SSteve French 		}
1441*38c8a9a5SSteve French 	} else if (server->origin_fullpath || server->leaf_fullpath) {
1442*38c8a9a5SSteve French 		return 0;
1443*38c8a9a5SSteve French 	}
1444*38c8a9a5SSteve French 
1445*38c8a9a5SSteve French 	/*
1446*38c8a9a5SSteve French 	 * Match for a regular connection (address/hostname/port) which has no
1447*38c8a9a5SSteve French 	 * DFS referrals set.
1448*38c8a9a5SSteve French 	 */
1449*38c8a9a5SSteve French 	if (!server->origin_fullpath && !server->leaf_fullpath &&
1450*38c8a9a5SSteve French 	    (strcasecmp(server->hostname, ctx->server_hostname) ||
1451*38c8a9a5SSteve French 	     !match_server_address(server, addr) ||
1452*38c8a9a5SSteve French 	     !match_port(server, addr)))
1453*38c8a9a5SSteve French 		return 0;
1454*38c8a9a5SSteve French 
1455*38c8a9a5SSteve French 	if (!match_security(server, ctx))
1456*38c8a9a5SSteve French 		return 0;
1457*38c8a9a5SSteve French 
1458*38c8a9a5SSteve French 	if (server->echo_interval != ctx->echo_interval * HZ)
1459*38c8a9a5SSteve French 		return 0;
1460*38c8a9a5SSteve French 
1461*38c8a9a5SSteve French 	if (server->rdma != ctx->rdma)
1462*38c8a9a5SSteve French 		return 0;
1463*38c8a9a5SSteve French 
1464*38c8a9a5SSteve French 	if (server->ignore_signature != ctx->ignore_signature)
1465*38c8a9a5SSteve French 		return 0;
1466*38c8a9a5SSteve French 
1467*38c8a9a5SSteve French 	if (server->min_offload != ctx->min_offload)
1468*38c8a9a5SSteve French 		return 0;
1469*38c8a9a5SSteve French 
1470*38c8a9a5SSteve French 	return 1;
1471*38c8a9a5SSteve French }
1472*38c8a9a5SSteve French 
1473*38c8a9a5SSteve French struct TCP_Server_Info *
1474*38c8a9a5SSteve French cifs_find_tcp_session(struct smb3_fs_context *ctx)
1475*38c8a9a5SSteve French {
1476*38c8a9a5SSteve French 	struct TCP_Server_Info *server;
1477*38c8a9a5SSteve French 
1478*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
1479*38c8a9a5SSteve French 	list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1480*38c8a9a5SSteve French 		spin_lock(&server->srv_lock);
1481*38c8a9a5SSteve French 		/*
1482*38c8a9a5SSteve French 		 * Skip ses channels since they're only handled in lower layers
1483*38c8a9a5SSteve French 		 * (e.g. cifs_send_recv).
1484*38c8a9a5SSteve French 		 */
1485*38c8a9a5SSteve French 		if (CIFS_SERVER_IS_CHAN(server) || !match_server(server, ctx)) {
1486*38c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
1487*38c8a9a5SSteve French 			continue;
1488*38c8a9a5SSteve French 		}
1489*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
1490*38c8a9a5SSteve French 
1491*38c8a9a5SSteve French 		++server->srv_count;
1492*38c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
1493*38c8a9a5SSteve French 		cifs_dbg(FYI, "Existing tcp session with server found\n");
1494*38c8a9a5SSteve French 		return server;
1495*38c8a9a5SSteve French 	}
1496*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
1497*38c8a9a5SSteve French 	return NULL;
1498*38c8a9a5SSteve French }
1499*38c8a9a5SSteve French 
1500*38c8a9a5SSteve French void
1501*38c8a9a5SSteve French cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
1502*38c8a9a5SSteve French {
1503*38c8a9a5SSteve French 	struct task_struct *task;
1504*38c8a9a5SSteve French 
1505*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
1506*38c8a9a5SSteve French 	if (--server->srv_count > 0) {
1507*38c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
1508*38c8a9a5SSteve French 		return;
1509*38c8a9a5SSteve French 	}
1510*38c8a9a5SSteve French 
1511*38c8a9a5SSteve French 	/* srv_count can never go negative */
1512*38c8a9a5SSteve French 	WARN_ON(server->srv_count < 0);
1513*38c8a9a5SSteve French 
1514*38c8a9a5SSteve French 	put_net(cifs_net_ns(server));
1515*38c8a9a5SSteve French 
1516*38c8a9a5SSteve French 	list_del_init(&server->tcp_ses_list);
1517*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
1518*38c8a9a5SSteve French 
1519*38c8a9a5SSteve French 	/* For secondary channels, we pick up ref-count on the primary server */
1520*38c8a9a5SSteve French 	if (CIFS_SERVER_IS_CHAN(server))
1521*38c8a9a5SSteve French 		cifs_put_tcp_session(server->primary_server, from_reconnect);
1522*38c8a9a5SSteve French 
1523*38c8a9a5SSteve French 	cancel_delayed_work_sync(&server->echo);
1524*38c8a9a5SSteve French 
1525*38c8a9a5SSteve French 	if (from_reconnect)
1526*38c8a9a5SSteve French 		/*
1527*38c8a9a5SSteve French 		 * Avoid deadlock here: reconnect work calls
1528*38c8a9a5SSteve French 		 * cifs_put_tcp_session() at its end. Need to be sure
1529*38c8a9a5SSteve French 		 * that reconnect work does nothing with server pointer after
1530*38c8a9a5SSteve French 		 * that step.
1531*38c8a9a5SSteve French 		 */
1532*38c8a9a5SSteve French 		cancel_delayed_work(&server->reconnect);
1533*38c8a9a5SSteve French 	else
1534*38c8a9a5SSteve French 		cancel_delayed_work_sync(&server->reconnect);
1535*38c8a9a5SSteve French 
1536*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
1537*38c8a9a5SSteve French 	server->tcpStatus = CifsExiting;
1538*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
1539*38c8a9a5SSteve French 
1540*38c8a9a5SSteve French 	cifs_crypto_secmech_release(server);
1541*38c8a9a5SSteve French 
1542*38c8a9a5SSteve French 	kfree_sensitive(server->session_key.response);
1543*38c8a9a5SSteve French 	server->session_key.response = NULL;
1544*38c8a9a5SSteve French 	server->session_key.len = 0;
1545*38c8a9a5SSteve French 	kfree(server->hostname);
1546*38c8a9a5SSteve French 	server->hostname = NULL;
1547*38c8a9a5SSteve French 
1548*38c8a9a5SSteve French 	task = xchg(&server->tsk, NULL);
1549*38c8a9a5SSteve French 	if (task)
1550*38c8a9a5SSteve French 		send_sig(SIGKILL, task, 1);
1551*38c8a9a5SSteve French }
1552*38c8a9a5SSteve French 
1553*38c8a9a5SSteve French struct TCP_Server_Info *
1554*38c8a9a5SSteve French cifs_get_tcp_session(struct smb3_fs_context *ctx,
1555*38c8a9a5SSteve French 		     struct TCP_Server_Info *primary_server)
1556*38c8a9a5SSteve French {
1557*38c8a9a5SSteve French 	struct TCP_Server_Info *tcp_ses = NULL;
1558*38c8a9a5SSteve French 	int rc;
1559*38c8a9a5SSteve French 
1560*38c8a9a5SSteve French 	cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
1561*38c8a9a5SSteve French 
1562*38c8a9a5SSteve French 	/* see if we already have a matching tcp_ses */
1563*38c8a9a5SSteve French 	tcp_ses = cifs_find_tcp_session(ctx);
1564*38c8a9a5SSteve French 	if (tcp_ses)
1565*38c8a9a5SSteve French 		return tcp_ses;
1566*38c8a9a5SSteve French 
1567*38c8a9a5SSteve French 	tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1568*38c8a9a5SSteve French 	if (!tcp_ses) {
1569*38c8a9a5SSteve French 		rc = -ENOMEM;
1570*38c8a9a5SSteve French 		goto out_err;
1571*38c8a9a5SSteve French 	}
1572*38c8a9a5SSteve French 
1573*38c8a9a5SSteve French 	tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1574*38c8a9a5SSteve French 	if (!tcp_ses->hostname) {
1575*38c8a9a5SSteve French 		rc = -ENOMEM;
1576*38c8a9a5SSteve French 		goto out_err;
1577*38c8a9a5SSteve French 	}
1578*38c8a9a5SSteve French 
1579*38c8a9a5SSteve French 	if (ctx->leaf_fullpath) {
1580*38c8a9a5SSteve French 		tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL);
1581*38c8a9a5SSteve French 		if (!tcp_ses->leaf_fullpath) {
1582*38c8a9a5SSteve French 			rc = -ENOMEM;
1583*38c8a9a5SSteve French 			goto out_err;
1584*38c8a9a5SSteve French 		}
1585*38c8a9a5SSteve French 	}
1586*38c8a9a5SSteve French 
1587*38c8a9a5SSteve French 	if (ctx->nosharesock)
1588*38c8a9a5SSteve French 		tcp_ses->nosharesock = true;
1589*38c8a9a5SSteve French 
1590*38c8a9a5SSteve French 	tcp_ses->ops = ctx->ops;
1591*38c8a9a5SSteve French 	tcp_ses->vals = ctx->vals;
1592*38c8a9a5SSteve French 	cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
1593*38c8a9a5SSteve French 
1594*38c8a9a5SSteve French 	tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
1595*38c8a9a5SSteve French 	tcp_ses->noblockcnt = ctx->rootfs;
1596*38c8a9a5SSteve French 	tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1597*38c8a9a5SSteve French 	tcp_ses->noautotune = ctx->noautotune;
1598*38c8a9a5SSteve French 	tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1599*38c8a9a5SSteve French 	tcp_ses->rdma = ctx->rdma;
1600*38c8a9a5SSteve French 	tcp_ses->in_flight = 0;
1601*38c8a9a5SSteve French 	tcp_ses->max_in_flight = 0;
1602*38c8a9a5SSteve French 	tcp_ses->credits = 1;
1603*38c8a9a5SSteve French 	if (primary_server) {
1604*38c8a9a5SSteve French 		spin_lock(&cifs_tcp_ses_lock);
1605*38c8a9a5SSteve French 		++primary_server->srv_count;
1606*38c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
1607*38c8a9a5SSteve French 		tcp_ses->primary_server = primary_server;
1608*38c8a9a5SSteve French 	}
1609*38c8a9a5SSteve French 	init_waitqueue_head(&tcp_ses->response_q);
1610*38c8a9a5SSteve French 	init_waitqueue_head(&tcp_ses->request_q);
1611*38c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1612*38c8a9a5SSteve French 	mutex_init(&tcp_ses->_srv_mutex);
1613*38c8a9a5SSteve French 	memcpy(tcp_ses->workstation_RFC1001_name,
1614*38c8a9a5SSteve French 		ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1615*38c8a9a5SSteve French 	memcpy(tcp_ses->server_RFC1001_name,
1616*38c8a9a5SSteve French 		ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1617*38c8a9a5SSteve French 	tcp_ses->session_estab = false;
1618*38c8a9a5SSteve French 	tcp_ses->sequence_number = 0;
1619*38c8a9a5SSteve French 	tcp_ses->reconnect_instance = 1;
1620*38c8a9a5SSteve French 	tcp_ses->lstrp = jiffies;
1621*38c8a9a5SSteve French 	tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
1622*38c8a9a5SSteve French 	spin_lock_init(&tcp_ses->req_lock);
1623*38c8a9a5SSteve French 	spin_lock_init(&tcp_ses->srv_lock);
1624*38c8a9a5SSteve French 	spin_lock_init(&tcp_ses->mid_lock);
1625*38c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1626*38c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
1627*38c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
1628*38c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1629*38c8a9a5SSteve French 	mutex_init(&tcp_ses->reconnect_mutex);
1630*38c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
1631*38c8a9a5SSteve French 	mutex_init(&tcp_ses->refpath_lock);
1632*38c8a9a5SSteve French #endif
1633*38c8a9a5SSteve French 	memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
1634*38c8a9a5SSteve French 	       sizeof(tcp_ses->srcaddr));
1635*38c8a9a5SSteve French 	memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
1636*38c8a9a5SSteve French 		sizeof(tcp_ses->dstaddr));
1637*38c8a9a5SSteve French 	if (ctx->use_client_guid)
1638*38c8a9a5SSteve French 		memcpy(tcp_ses->client_guid, ctx->client_guid,
1639*38c8a9a5SSteve French 		       SMB2_CLIENT_GUID_SIZE);
1640*38c8a9a5SSteve French 	else
1641*38c8a9a5SSteve French 		generate_random_uuid(tcp_ses->client_guid);
1642*38c8a9a5SSteve French 	/*
1643*38c8a9a5SSteve French 	 * at this point we are the only ones with the pointer
1644*38c8a9a5SSteve French 	 * to the struct since the kernel thread not created yet
1645*38c8a9a5SSteve French 	 * no need to spinlock this init of tcpStatus or srv_count
1646*38c8a9a5SSteve French 	 */
1647*38c8a9a5SSteve French 	tcp_ses->tcpStatus = CifsNew;
1648*38c8a9a5SSteve French 	++tcp_ses->srv_count;
1649*38c8a9a5SSteve French 
1650*38c8a9a5SSteve French 	if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1651*38c8a9a5SSteve French 		ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1652*38c8a9a5SSteve French 		tcp_ses->echo_interval = ctx->echo_interval * HZ;
1653*38c8a9a5SSteve French 	else
1654*38c8a9a5SSteve French 		tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
1655*38c8a9a5SSteve French 	if (tcp_ses->rdma) {
1656*38c8a9a5SSteve French #ifndef CONFIG_CIFS_SMB_DIRECT
1657*38c8a9a5SSteve French 		cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1658*38c8a9a5SSteve French 		rc = -ENOENT;
1659*38c8a9a5SSteve French 		goto out_err_crypto_release;
1660*38c8a9a5SSteve French #endif
1661*38c8a9a5SSteve French 		tcp_ses->smbd_conn = smbd_get_connection(
1662*38c8a9a5SSteve French 			tcp_ses, (struct sockaddr *)&ctx->dstaddr);
1663*38c8a9a5SSteve French 		if (tcp_ses->smbd_conn) {
1664*38c8a9a5SSteve French 			cifs_dbg(VFS, "RDMA transport established\n");
1665*38c8a9a5SSteve French 			rc = 0;
1666*38c8a9a5SSteve French 			goto smbd_connected;
1667*38c8a9a5SSteve French 		} else {
1668*38c8a9a5SSteve French 			rc = -ENOENT;
1669*38c8a9a5SSteve French 			goto out_err_crypto_release;
1670*38c8a9a5SSteve French 		}
1671*38c8a9a5SSteve French 	}
1672*38c8a9a5SSteve French 	rc = ip_connect(tcp_ses);
1673*38c8a9a5SSteve French 	if (rc < 0) {
1674*38c8a9a5SSteve French 		cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
1675*38c8a9a5SSteve French 		goto out_err_crypto_release;
1676*38c8a9a5SSteve French 	}
1677*38c8a9a5SSteve French smbd_connected:
1678*38c8a9a5SSteve French 	/*
1679*38c8a9a5SSteve French 	 * since we're in a cifs function already, we know that
1680*38c8a9a5SSteve French 	 * this will succeed. No need for try_module_get().
1681*38c8a9a5SSteve French 	 */
1682*38c8a9a5SSteve French 	__module_get(THIS_MODULE);
1683*38c8a9a5SSteve French 	tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
1684*38c8a9a5SSteve French 				  tcp_ses, "cifsd");
1685*38c8a9a5SSteve French 	if (IS_ERR(tcp_ses->tsk)) {
1686*38c8a9a5SSteve French 		rc = PTR_ERR(tcp_ses->tsk);
1687*38c8a9a5SSteve French 		cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
1688*38c8a9a5SSteve French 		module_put(THIS_MODULE);
1689*38c8a9a5SSteve French 		goto out_err_crypto_release;
1690*38c8a9a5SSteve French 	}
1691*38c8a9a5SSteve French 	tcp_ses->min_offload = ctx->min_offload;
1692*38c8a9a5SSteve French 	/*
1693*38c8a9a5SSteve French 	 * at this point we are the only ones with the pointer
1694*38c8a9a5SSteve French 	 * to the struct since the kernel thread not created yet
1695*38c8a9a5SSteve French 	 * no need to spinlock this update of tcpStatus
1696*38c8a9a5SSteve French 	 */
1697*38c8a9a5SSteve French 	spin_lock(&tcp_ses->srv_lock);
1698*38c8a9a5SSteve French 	tcp_ses->tcpStatus = CifsNeedNegotiate;
1699*38c8a9a5SSteve French 	spin_unlock(&tcp_ses->srv_lock);
1700*38c8a9a5SSteve French 
1701*38c8a9a5SSteve French 	if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1702*38c8a9a5SSteve French 		tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1703*38c8a9a5SSteve French 	else
1704*38c8a9a5SSteve French 		tcp_ses->max_credits = ctx->max_credits;
1705*38c8a9a5SSteve French 
1706*38c8a9a5SSteve French 	tcp_ses->nr_targets = 1;
1707*38c8a9a5SSteve French 	tcp_ses->ignore_signature = ctx->ignore_signature;
1708*38c8a9a5SSteve French 	/* thread spawned, put it on the list */
1709*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
1710*38c8a9a5SSteve French 	list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
1711*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
1712*38c8a9a5SSteve French 
1713*38c8a9a5SSteve French 	/* queue echo request delayed work */
1714*38c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
1715*38c8a9a5SSteve French 
1716*38c8a9a5SSteve French 	return tcp_ses;
1717*38c8a9a5SSteve French 
1718*38c8a9a5SSteve French out_err_crypto_release:
1719*38c8a9a5SSteve French 	cifs_crypto_secmech_release(tcp_ses);
1720*38c8a9a5SSteve French 
1721*38c8a9a5SSteve French 	put_net(cifs_net_ns(tcp_ses));
1722*38c8a9a5SSteve French 
1723*38c8a9a5SSteve French out_err:
1724*38c8a9a5SSteve French 	if (tcp_ses) {
1725*38c8a9a5SSteve French 		if (CIFS_SERVER_IS_CHAN(tcp_ses))
1726*38c8a9a5SSteve French 			cifs_put_tcp_session(tcp_ses->primary_server, false);
1727*38c8a9a5SSteve French 		kfree(tcp_ses->hostname);
1728*38c8a9a5SSteve French 		kfree(tcp_ses->leaf_fullpath);
1729*38c8a9a5SSteve French 		if (tcp_ses->ssocket)
1730*38c8a9a5SSteve French 			sock_release(tcp_ses->ssocket);
1731*38c8a9a5SSteve French 		kfree(tcp_ses);
1732*38c8a9a5SSteve French 	}
1733*38c8a9a5SSteve French 	return ERR_PTR(rc);
1734*38c8a9a5SSteve French }
1735*38c8a9a5SSteve French 
1736*38c8a9a5SSteve French /* this function must be called with ses_lock and chan_lock held */
1737*38c8a9a5SSteve French static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1738*38c8a9a5SSteve French {
1739*38c8a9a5SSteve French 	if (ctx->sectype != Unspecified &&
1740*38c8a9a5SSteve French 	    ctx->sectype != ses->sectype)
1741*38c8a9a5SSteve French 		return 0;
1742*38c8a9a5SSteve French 
1743*38c8a9a5SSteve French 	/*
1744*38c8a9a5SSteve French 	 * If an existing session is limited to less channels than
1745*38c8a9a5SSteve French 	 * requested, it should not be reused
1746*38c8a9a5SSteve French 	 */
1747*38c8a9a5SSteve French 	if (ses->chan_max < ctx->max_channels)
1748*38c8a9a5SSteve French 		return 0;
1749*38c8a9a5SSteve French 
1750*38c8a9a5SSteve French 	switch (ses->sectype) {
1751*38c8a9a5SSteve French 	case Kerberos:
1752*38c8a9a5SSteve French 		if (!uid_eq(ctx->cred_uid, ses->cred_uid))
1753*38c8a9a5SSteve French 			return 0;
1754*38c8a9a5SSteve French 		break;
1755*38c8a9a5SSteve French 	default:
1756*38c8a9a5SSteve French 		/* NULL username means anonymous session */
1757*38c8a9a5SSteve French 		if (ses->user_name == NULL) {
1758*38c8a9a5SSteve French 			if (!ctx->nullauth)
1759*38c8a9a5SSteve French 				return 0;
1760*38c8a9a5SSteve French 			break;
1761*38c8a9a5SSteve French 		}
1762*38c8a9a5SSteve French 
1763*38c8a9a5SSteve French 		/* anything else takes username/password */
1764*38c8a9a5SSteve French 		if (strncmp(ses->user_name,
1765*38c8a9a5SSteve French 			    ctx->username ? ctx->username : "",
1766*38c8a9a5SSteve French 			    CIFS_MAX_USERNAME_LEN))
1767*38c8a9a5SSteve French 			return 0;
1768*38c8a9a5SSteve French 		if ((ctx->username && strlen(ctx->username) != 0) &&
1769*38c8a9a5SSteve French 		    ses->password != NULL &&
1770*38c8a9a5SSteve French 		    strncmp(ses->password,
1771*38c8a9a5SSteve French 			    ctx->password ? ctx->password : "",
1772*38c8a9a5SSteve French 			    CIFS_MAX_PASSWORD_LEN))
1773*38c8a9a5SSteve French 			return 0;
1774*38c8a9a5SSteve French 	}
1775*38c8a9a5SSteve French 	return 1;
1776*38c8a9a5SSteve French }
1777*38c8a9a5SSteve French 
1778*38c8a9a5SSteve French /**
1779*38c8a9a5SSteve French  * cifs_setup_ipc - helper to setup the IPC tcon for the session
1780*38c8a9a5SSteve French  * @ses: smb session to issue the request on
1781*38c8a9a5SSteve French  * @ctx: the superblock configuration context to use for building the
1782*38c8a9a5SSteve French  *       new tree connection for the IPC (interprocess communication RPC)
1783*38c8a9a5SSteve French  *
1784*38c8a9a5SSteve French  * A new IPC connection is made and stored in the session
1785*38c8a9a5SSteve French  * tcon_ipc. The IPC tcon has the same lifetime as the session.
1786*38c8a9a5SSteve French  */
1787*38c8a9a5SSteve French static int
1788*38c8a9a5SSteve French cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1789*38c8a9a5SSteve French {
1790*38c8a9a5SSteve French 	int rc = 0, xid;
1791*38c8a9a5SSteve French 	struct cifs_tcon *tcon;
1792*38c8a9a5SSteve French 	char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1793*38c8a9a5SSteve French 	bool seal = false;
1794*38c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
1795*38c8a9a5SSteve French 
1796*38c8a9a5SSteve French 	/*
1797*38c8a9a5SSteve French 	 * If the mount request that resulted in the creation of the
1798*38c8a9a5SSteve French 	 * session requires encryption, force IPC to be encrypted too.
1799*38c8a9a5SSteve French 	 */
1800*38c8a9a5SSteve French 	if (ctx->seal) {
1801*38c8a9a5SSteve French 		if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
1802*38c8a9a5SSteve French 			seal = true;
1803*38c8a9a5SSteve French 		else {
1804*38c8a9a5SSteve French 			cifs_server_dbg(VFS,
1805*38c8a9a5SSteve French 				 "IPC: server doesn't support encryption\n");
1806*38c8a9a5SSteve French 			return -EOPNOTSUPP;
1807*38c8a9a5SSteve French 		}
1808*38c8a9a5SSteve French 	}
1809*38c8a9a5SSteve French 
1810*38c8a9a5SSteve French 	tcon = tconInfoAlloc();
1811*38c8a9a5SSteve French 	if (tcon == NULL)
1812*38c8a9a5SSteve French 		return -ENOMEM;
1813*38c8a9a5SSteve French 
1814*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
1815*38c8a9a5SSteve French 	scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
1816*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
1817*38c8a9a5SSteve French 
1818*38c8a9a5SSteve French 	xid = get_xid();
1819*38c8a9a5SSteve French 	tcon->ses = ses;
1820*38c8a9a5SSteve French 	tcon->ipc = true;
1821*38c8a9a5SSteve French 	tcon->seal = seal;
1822*38c8a9a5SSteve French 	rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
1823*38c8a9a5SSteve French 	free_xid(xid);
1824*38c8a9a5SSteve French 
1825*38c8a9a5SSteve French 	if (rc) {
1826*38c8a9a5SSteve French 		cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
1827*38c8a9a5SSteve French 		tconInfoFree(tcon);
1828*38c8a9a5SSteve French 		goto out;
1829*38c8a9a5SSteve French 	}
1830*38c8a9a5SSteve French 
1831*38c8a9a5SSteve French 	cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid);
1832*38c8a9a5SSteve French 
1833*38c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
1834*38c8a9a5SSteve French 	tcon->status = TID_GOOD;
1835*38c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
1836*38c8a9a5SSteve French 	ses->tcon_ipc = tcon;
1837*38c8a9a5SSteve French out:
1838*38c8a9a5SSteve French 	return rc;
1839*38c8a9a5SSteve French }
1840*38c8a9a5SSteve French 
1841*38c8a9a5SSteve French /**
1842*38c8a9a5SSteve French  * cifs_free_ipc - helper to release the session IPC tcon
1843*38c8a9a5SSteve French  * @ses: smb session to unmount the IPC from
1844*38c8a9a5SSteve French  *
1845*38c8a9a5SSteve French  * Needs to be called everytime a session is destroyed.
1846*38c8a9a5SSteve French  *
1847*38c8a9a5SSteve French  * On session close, the IPC is closed and the server must release all tcons of the session.
1848*38c8a9a5SSteve French  * No need to send a tree disconnect here.
1849*38c8a9a5SSteve French  *
1850*38c8a9a5SSteve French  * Besides, it will make the server to not close durable and resilient files on session close, as
1851*38c8a9a5SSteve French  * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
1852*38c8a9a5SSteve French  */
1853*38c8a9a5SSteve French static int
1854*38c8a9a5SSteve French cifs_free_ipc(struct cifs_ses *ses)
1855*38c8a9a5SSteve French {
1856*38c8a9a5SSteve French 	struct cifs_tcon *tcon = ses->tcon_ipc;
1857*38c8a9a5SSteve French 
1858*38c8a9a5SSteve French 	if (tcon == NULL)
1859*38c8a9a5SSteve French 		return 0;
1860*38c8a9a5SSteve French 
1861*38c8a9a5SSteve French 	tconInfoFree(tcon);
1862*38c8a9a5SSteve French 	ses->tcon_ipc = NULL;
1863*38c8a9a5SSteve French 	return 0;
1864*38c8a9a5SSteve French }
1865*38c8a9a5SSteve French 
1866*38c8a9a5SSteve French static struct cifs_ses *
1867*38c8a9a5SSteve French cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1868*38c8a9a5SSteve French {
1869*38c8a9a5SSteve French 	struct cifs_ses *ses, *ret = NULL;
1870*38c8a9a5SSteve French 
1871*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
1872*38c8a9a5SSteve French 	list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1873*38c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
1874*38c8a9a5SSteve French 		if (ses->ses_status == SES_EXITING) {
1875*38c8a9a5SSteve French 			spin_unlock(&ses->ses_lock);
1876*38c8a9a5SSteve French 			continue;
1877*38c8a9a5SSteve French 		}
1878*38c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
1879*38c8a9a5SSteve French 		if (match_session(ses, ctx)) {
1880*38c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
1881*38c8a9a5SSteve French 			spin_unlock(&ses->ses_lock);
1882*38c8a9a5SSteve French 			ret = ses;
1883*38c8a9a5SSteve French 			break;
1884*38c8a9a5SSteve French 		}
1885*38c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
1886*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
1887*38c8a9a5SSteve French 	}
1888*38c8a9a5SSteve French 	if (ret)
1889*38c8a9a5SSteve French 		cifs_smb_ses_inc_refcount(ret);
1890*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
1891*38c8a9a5SSteve French 	return ret;
1892*38c8a9a5SSteve French }
1893*38c8a9a5SSteve French 
1894*38c8a9a5SSteve French void __cifs_put_smb_ses(struct cifs_ses *ses)
1895*38c8a9a5SSteve French {
1896*38c8a9a5SSteve French 	unsigned int rc, xid;
1897*38c8a9a5SSteve French 	unsigned int chan_count;
1898*38c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
1899*38c8a9a5SSteve French 
1900*38c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
1901*38c8a9a5SSteve French 	if (ses->ses_status == SES_EXITING) {
1902*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
1903*38c8a9a5SSteve French 		return;
1904*38c8a9a5SSteve French 	}
1905*38c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
1906*38c8a9a5SSteve French 
1907*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1908*38c8a9a5SSteve French 	cifs_dbg(FYI,
1909*38c8a9a5SSteve French 		 "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->tree_name : "NONE");
1910*38c8a9a5SSteve French 
1911*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
1912*38c8a9a5SSteve French 	if (--ses->ses_count > 0) {
1913*38c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
1914*38c8a9a5SSteve French 		return;
1915*38c8a9a5SSteve French 	}
1916*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
1917*38c8a9a5SSteve French 
1918*38c8a9a5SSteve French 	/* ses_count can never go negative */
1919*38c8a9a5SSteve French 	WARN_ON(ses->ses_count < 0);
1920*38c8a9a5SSteve French 
1921*38c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
1922*38c8a9a5SSteve French 	if (ses->ses_status == SES_GOOD)
1923*38c8a9a5SSteve French 		ses->ses_status = SES_EXITING;
1924*38c8a9a5SSteve French 
1925*38c8a9a5SSteve French 	if (ses->ses_status == SES_EXITING && server->ops->logoff) {
1926*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
1927*38c8a9a5SSteve French 		cifs_free_ipc(ses);
1928*38c8a9a5SSteve French 		xid = get_xid();
1929*38c8a9a5SSteve French 		rc = server->ops->logoff(xid, ses);
1930*38c8a9a5SSteve French 		if (rc)
1931*38c8a9a5SSteve French 			cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
1932*38c8a9a5SSteve French 				__func__, rc);
1933*38c8a9a5SSteve French 		_free_xid(xid);
1934*38c8a9a5SSteve French 	} else {
1935*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
1936*38c8a9a5SSteve French 		cifs_free_ipc(ses);
1937*38c8a9a5SSteve French 	}
1938*38c8a9a5SSteve French 
1939*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
1940*38c8a9a5SSteve French 	list_del_init(&ses->smb_ses_list);
1941*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
1942*38c8a9a5SSteve French 
1943*38c8a9a5SSteve French 	chan_count = ses->chan_count;
1944*38c8a9a5SSteve French 
1945*38c8a9a5SSteve French 	/* close any extra channels */
1946*38c8a9a5SSteve French 	if (chan_count > 1) {
1947*38c8a9a5SSteve French 		int i;
1948*38c8a9a5SSteve French 
1949*38c8a9a5SSteve French 		for (i = 1; i < chan_count; i++) {
1950*38c8a9a5SSteve French 			if (ses->chans[i].iface) {
1951*38c8a9a5SSteve French 				kref_put(&ses->chans[i].iface->refcount, release_iface);
1952*38c8a9a5SSteve French 				ses->chans[i].iface = NULL;
1953*38c8a9a5SSteve French 			}
1954*38c8a9a5SSteve French 			cifs_put_tcp_session(ses->chans[i].server, 0);
1955*38c8a9a5SSteve French 			ses->chans[i].server = NULL;
1956*38c8a9a5SSteve French 		}
1957*38c8a9a5SSteve French 	}
1958*38c8a9a5SSteve French 
1959*38c8a9a5SSteve French 	sesInfoFree(ses);
1960*38c8a9a5SSteve French 	cifs_put_tcp_session(server, 0);
1961*38c8a9a5SSteve French }
1962*38c8a9a5SSteve French 
1963*38c8a9a5SSteve French #ifdef CONFIG_KEYS
1964*38c8a9a5SSteve French 
1965*38c8a9a5SSteve French /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1966*38c8a9a5SSteve French #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
1967*38c8a9a5SSteve French 
1968*38c8a9a5SSteve French /* Populate username and pw fields from keyring if possible */
1969*38c8a9a5SSteve French static int
1970*38c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
1971*38c8a9a5SSteve French {
1972*38c8a9a5SSteve French 	int rc = 0;
1973*38c8a9a5SSteve French 	int is_domain = 0;
1974*38c8a9a5SSteve French 	const char *delim, *payload;
1975*38c8a9a5SSteve French 	char *desc;
1976*38c8a9a5SSteve French 	ssize_t len;
1977*38c8a9a5SSteve French 	struct key *key;
1978*38c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
1979*38c8a9a5SSteve French 	struct sockaddr_in *sa;
1980*38c8a9a5SSteve French 	struct sockaddr_in6 *sa6;
1981*38c8a9a5SSteve French 	const struct user_key_payload *upayload;
1982*38c8a9a5SSteve French 
1983*38c8a9a5SSteve French 	desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1984*38c8a9a5SSteve French 	if (!desc)
1985*38c8a9a5SSteve French 		return -ENOMEM;
1986*38c8a9a5SSteve French 
1987*38c8a9a5SSteve French 	/* try to find an address key first */
1988*38c8a9a5SSteve French 	switch (server->dstaddr.ss_family) {
1989*38c8a9a5SSteve French 	case AF_INET:
1990*38c8a9a5SSteve French 		sa = (struct sockaddr_in *)&server->dstaddr;
1991*38c8a9a5SSteve French 		sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1992*38c8a9a5SSteve French 		break;
1993*38c8a9a5SSteve French 	case AF_INET6:
1994*38c8a9a5SSteve French 		sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1995*38c8a9a5SSteve French 		sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1996*38c8a9a5SSteve French 		break;
1997*38c8a9a5SSteve French 	default:
1998*38c8a9a5SSteve French 		cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1999*38c8a9a5SSteve French 			 server->dstaddr.ss_family);
2000*38c8a9a5SSteve French 		rc = -EINVAL;
2001*38c8a9a5SSteve French 		goto out_err;
2002*38c8a9a5SSteve French 	}
2003*38c8a9a5SSteve French 
2004*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2005*38c8a9a5SSteve French 	key = request_key(&key_type_logon, desc, "");
2006*38c8a9a5SSteve French 	if (IS_ERR(key)) {
2007*38c8a9a5SSteve French 		if (!ses->domainName) {
2008*38c8a9a5SSteve French 			cifs_dbg(FYI, "domainName is NULL\n");
2009*38c8a9a5SSteve French 			rc = PTR_ERR(key);
2010*38c8a9a5SSteve French 			goto out_err;
2011*38c8a9a5SSteve French 		}
2012*38c8a9a5SSteve French 
2013*38c8a9a5SSteve French 		/* didn't work, try to find a domain key */
2014*38c8a9a5SSteve French 		sprintf(desc, "cifs:d:%s", ses->domainName);
2015*38c8a9a5SSteve French 		cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2016*38c8a9a5SSteve French 		key = request_key(&key_type_logon, desc, "");
2017*38c8a9a5SSteve French 		if (IS_ERR(key)) {
2018*38c8a9a5SSteve French 			rc = PTR_ERR(key);
2019*38c8a9a5SSteve French 			goto out_err;
2020*38c8a9a5SSteve French 		}
2021*38c8a9a5SSteve French 		is_domain = 1;
2022*38c8a9a5SSteve French 	}
2023*38c8a9a5SSteve French 
2024*38c8a9a5SSteve French 	down_read(&key->sem);
2025*38c8a9a5SSteve French 	upayload = user_key_payload_locked(key);
2026*38c8a9a5SSteve French 	if (IS_ERR_OR_NULL(upayload)) {
2027*38c8a9a5SSteve French 		rc = upayload ? PTR_ERR(upayload) : -EINVAL;
2028*38c8a9a5SSteve French 		goto out_key_put;
2029*38c8a9a5SSteve French 	}
2030*38c8a9a5SSteve French 
2031*38c8a9a5SSteve French 	/* find first : in payload */
2032*38c8a9a5SSteve French 	payload = upayload->data;
2033*38c8a9a5SSteve French 	delim = strnchr(payload, upayload->datalen, ':');
2034*38c8a9a5SSteve French 	cifs_dbg(FYI, "payload=%s\n", payload);
2035*38c8a9a5SSteve French 	if (!delim) {
2036*38c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2037*38c8a9a5SSteve French 			 upayload->datalen);
2038*38c8a9a5SSteve French 		rc = -EINVAL;
2039*38c8a9a5SSteve French 		goto out_key_put;
2040*38c8a9a5SSteve French 	}
2041*38c8a9a5SSteve French 
2042*38c8a9a5SSteve French 	len = delim - payload;
2043*38c8a9a5SSteve French 	if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
2044*38c8a9a5SSteve French 		cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2045*38c8a9a5SSteve French 			 len);
2046*38c8a9a5SSteve French 		rc = -EINVAL;
2047*38c8a9a5SSteve French 		goto out_key_put;
2048*38c8a9a5SSteve French 	}
2049*38c8a9a5SSteve French 
2050*38c8a9a5SSteve French 	ctx->username = kstrndup(payload, len, GFP_KERNEL);
2051*38c8a9a5SSteve French 	if (!ctx->username) {
2052*38c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2053*38c8a9a5SSteve French 			 len);
2054*38c8a9a5SSteve French 		rc = -ENOMEM;
2055*38c8a9a5SSteve French 		goto out_key_put;
2056*38c8a9a5SSteve French 	}
2057*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
2058*38c8a9a5SSteve French 
2059*38c8a9a5SSteve French 	len = key->datalen - (len + 1);
2060*38c8a9a5SSteve French 	if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2061*38c8a9a5SSteve French 		cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2062*38c8a9a5SSteve French 		rc = -EINVAL;
2063*38c8a9a5SSteve French 		kfree(ctx->username);
2064*38c8a9a5SSteve French 		ctx->username = NULL;
2065*38c8a9a5SSteve French 		goto out_key_put;
2066*38c8a9a5SSteve French 	}
2067*38c8a9a5SSteve French 
2068*38c8a9a5SSteve French 	++delim;
2069*38c8a9a5SSteve French 	ctx->password = kstrndup(delim, len, GFP_KERNEL);
2070*38c8a9a5SSteve French 	if (!ctx->password) {
2071*38c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2072*38c8a9a5SSteve French 			 len);
2073*38c8a9a5SSteve French 		rc = -ENOMEM;
2074*38c8a9a5SSteve French 		kfree(ctx->username);
2075*38c8a9a5SSteve French 		ctx->username = NULL;
2076*38c8a9a5SSteve French 		goto out_key_put;
2077*38c8a9a5SSteve French 	}
2078*38c8a9a5SSteve French 
2079*38c8a9a5SSteve French 	/*
2080*38c8a9a5SSteve French 	 * If we have a domain key then we must set the domainName in the
2081*38c8a9a5SSteve French 	 * for the request.
2082*38c8a9a5SSteve French 	 */
2083*38c8a9a5SSteve French 	if (is_domain && ses->domainName) {
2084*38c8a9a5SSteve French 		ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
2085*38c8a9a5SSteve French 		if (!ctx->domainname) {
2086*38c8a9a5SSteve French 			cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
2087*38c8a9a5SSteve French 				 len);
2088*38c8a9a5SSteve French 			rc = -ENOMEM;
2089*38c8a9a5SSteve French 			kfree(ctx->username);
2090*38c8a9a5SSteve French 			ctx->username = NULL;
2091*38c8a9a5SSteve French 			kfree_sensitive(ctx->password);
2092*38c8a9a5SSteve French 			ctx->password = NULL;
2093*38c8a9a5SSteve French 			goto out_key_put;
2094*38c8a9a5SSteve French 		}
2095*38c8a9a5SSteve French 	}
2096*38c8a9a5SSteve French 
2097*38c8a9a5SSteve French 	strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name));
2098*38c8a9a5SSteve French 
2099*38c8a9a5SSteve French out_key_put:
2100*38c8a9a5SSteve French 	up_read(&key->sem);
2101*38c8a9a5SSteve French 	key_put(key);
2102*38c8a9a5SSteve French out_err:
2103*38c8a9a5SSteve French 	kfree(desc);
2104*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2105*38c8a9a5SSteve French 	return rc;
2106*38c8a9a5SSteve French }
2107*38c8a9a5SSteve French #else /* ! CONFIG_KEYS */
2108*38c8a9a5SSteve French static inline int
2109*38c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
2110*38c8a9a5SSteve French 		   struct cifs_ses *ses __attribute__((unused)))
2111*38c8a9a5SSteve French {
2112*38c8a9a5SSteve French 	return -ENOSYS;
2113*38c8a9a5SSteve French }
2114*38c8a9a5SSteve French #endif /* CONFIG_KEYS */
2115*38c8a9a5SSteve French 
2116*38c8a9a5SSteve French /**
2117*38c8a9a5SSteve French  * cifs_get_smb_ses - get a session matching @ctx data from @server
2118*38c8a9a5SSteve French  * @server: server to setup the session to
2119*38c8a9a5SSteve French  * @ctx: superblock configuration context to use to setup the session
2120*38c8a9a5SSteve French  *
2121*38c8a9a5SSteve French  * This function assumes it is being called from cifs_mount() where we
2122*38c8a9a5SSteve French  * already got a server reference (server refcount +1). See
2123*38c8a9a5SSteve French  * cifs_get_tcon() for refcount explanations.
2124*38c8a9a5SSteve French  */
2125*38c8a9a5SSteve French struct cifs_ses *
2126*38c8a9a5SSteve French cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
2127*38c8a9a5SSteve French {
2128*38c8a9a5SSteve French 	int rc = 0;
2129*38c8a9a5SSteve French 	unsigned int xid;
2130*38c8a9a5SSteve French 	struct cifs_ses *ses;
2131*38c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2132*38c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2133*38c8a9a5SSteve French 
2134*38c8a9a5SSteve French 	xid = get_xid();
2135*38c8a9a5SSteve French 
2136*38c8a9a5SSteve French 	ses = cifs_find_smb_ses(server, ctx);
2137*38c8a9a5SSteve French 	if (ses) {
2138*38c8a9a5SSteve French 		cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2139*38c8a9a5SSteve French 			 ses->ses_status);
2140*38c8a9a5SSteve French 
2141*38c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
2142*38c8a9a5SSteve French 		if (cifs_chan_needs_reconnect(ses, server)) {
2143*38c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
2144*38c8a9a5SSteve French 			cifs_dbg(FYI, "Session needs reconnect\n");
2145*38c8a9a5SSteve French 
2146*38c8a9a5SSteve French 			mutex_lock(&ses->session_mutex);
2147*38c8a9a5SSteve French 			rc = cifs_negotiate_protocol(xid, ses, server);
2148*38c8a9a5SSteve French 			if (rc) {
2149*38c8a9a5SSteve French 				mutex_unlock(&ses->session_mutex);
2150*38c8a9a5SSteve French 				/* problem -- put our ses reference */
2151*38c8a9a5SSteve French 				cifs_put_smb_ses(ses);
2152*38c8a9a5SSteve French 				free_xid(xid);
2153*38c8a9a5SSteve French 				return ERR_PTR(rc);
2154*38c8a9a5SSteve French 			}
2155*38c8a9a5SSteve French 
2156*38c8a9a5SSteve French 			rc = cifs_setup_session(xid, ses, server,
2157*38c8a9a5SSteve French 						ctx->local_nls);
2158*38c8a9a5SSteve French 			if (rc) {
2159*38c8a9a5SSteve French 				mutex_unlock(&ses->session_mutex);
2160*38c8a9a5SSteve French 				/* problem -- put our reference */
2161*38c8a9a5SSteve French 				cifs_put_smb_ses(ses);
2162*38c8a9a5SSteve French 				free_xid(xid);
2163*38c8a9a5SSteve French 				return ERR_PTR(rc);
2164*38c8a9a5SSteve French 			}
2165*38c8a9a5SSteve French 			mutex_unlock(&ses->session_mutex);
2166*38c8a9a5SSteve French 
2167*38c8a9a5SSteve French 			spin_lock(&ses->chan_lock);
2168*38c8a9a5SSteve French 		}
2169*38c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
2170*38c8a9a5SSteve French 
2171*38c8a9a5SSteve French 		/* existing SMB ses has a server reference already */
2172*38c8a9a5SSteve French 		cifs_put_tcp_session(server, 0);
2173*38c8a9a5SSteve French 		free_xid(xid);
2174*38c8a9a5SSteve French 		return ses;
2175*38c8a9a5SSteve French 	}
2176*38c8a9a5SSteve French 
2177*38c8a9a5SSteve French 	rc = -ENOMEM;
2178*38c8a9a5SSteve French 
2179*38c8a9a5SSteve French 	cifs_dbg(FYI, "Existing smb sess not found\n");
2180*38c8a9a5SSteve French 	ses = sesInfoAlloc();
2181*38c8a9a5SSteve French 	if (ses == NULL)
2182*38c8a9a5SSteve French 		goto get_ses_fail;
2183*38c8a9a5SSteve French 
2184*38c8a9a5SSteve French 	/* new SMB session uses our server ref */
2185*38c8a9a5SSteve French 	ses->server = server;
2186*38c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6)
2187*38c8a9a5SSteve French 		sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
2188*38c8a9a5SSteve French 	else
2189*38c8a9a5SSteve French 		sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
2190*38c8a9a5SSteve French 
2191*38c8a9a5SSteve French 	if (ctx->username) {
2192*38c8a9a5SSteve French 		ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
2193*38c8a9a5SSteve French 		if (!ses->user_name)
2194*38c8a9a5SSteve French 			goto get_ses_fail;
2195*38c8a9a5SSteve French 	}
2196*38c8a9a5SSteve French 
2197*38c8a9a5SSteve French 	/* ctx->password freed at unmount */
2198*38c8a9a5SSteve French 	if (ctx->password) {
2199*38c8a9a5SSteve French 		ses->password = kstrdup(ctx->password, GFP_KERNEL);
2200*38c8a9a5SSteve French 		if (!ses->password)
2201*38c8a9a5SSteve French 			goto get_ses_fail;
2202*38c8a9a5SSteve French 	}
2203*38c8a9a5SSteve French 	if (ctx->domainname) {
2204*38c8a9a5SSteve French 		ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
2205*38c8a9a5SSteve French 		if (!ses->domainName)
2206*38c8a9a5SSteve French 			goto get_ses_fail;
2207*38c8a9a5SSteve French 	}
2208*38c8a9a5SSteve French 
2209*38c8a9a5SSteve French 	strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name));
2210*38c8a9a5SSteve French 
2211*38c8a9a5SSteve French 	if (ctx->domainauto)
2212*38c8a9a5SSteve French 		ses->domainAuto = ctx->domainauto;
2213*38c8a9a5SSteve French 	ses->cred_uid = ctx->cred_uid;
2214*38c8a9a5SSteve French 	ses->linux_uid = ctx->linux_uid;
2215*38c8a9a5SSteve French 
2216*38c8a9a5SSteve French 	ses->sectype = ctx->sectype;
2217*38c8a9a5SSteve French 	ses->sign = ctx->sign;
2218*38c8a9a5SSteve French 
2219*38c8a9a5SSteve French 	/* add server as first channel */
2220*38c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
2221*38c8a9a5SSteve French 	ses->chans[0].server = server;
2222*38c8a9a5SSteve French 	ses->chan_count = 1;
2223*38c8a9a5SSteve French 	ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
2224*38c8a9a5SSteve French 	ses->chans_need_reconnect = 1;
2225*38c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
2226*38c8a9a5SSteve French 
2227*38c8a9a5SSteve French 	mutex_lock(&ses->session_mutex);
2228*38c8a9a5SSteve French 	rc = cifs_negotiate_protocol(xid, ses, server);
2229*38c8a9a5SSteve French 	if (!rc)
2230*38c8a9a5SSteve French 		rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
2231*38c8a9a5SSteve French 	mutex_unlock(&ses->session_mutex);
2232*38c8a9a5SSteve French 
2233*38c8a9a5SSteve French 	/* each channel uses a different signing key */
2234*38c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
2235*38c8a9a5SSteve French 	memcpy(ses->chans[0].signkey, ses->smb3signingkey,
2236*38c8a9a5SSteve French 	       sizeof(ses->smb3signingkey));
2237*38c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
2238*38c8a9a5SSteve French 
2239*38c8a9a5SSteve French 	if (rc)
2240*38c8a9a5SSteve French 		goto get_ses_fail;
2241*38c8a9a5SSteve French 
2242*38c8a9a5SSteve French 	/*
2243*38c8a9a5SSteve French 	 * success, put it on the list and add it as first channel
2244*38c8a9a5SSteve French 	 * note: the session becomes active soon after this. So you'll
2245*38c8a9a5SSteve French 	 * need to lock before changing something in the session.
2246*38c8a9a5SSteve French 	 */
2247*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
2248*38c8a9a5SSteve French 	ses->dfs_root_ses = ctx->dfs_root_ses;
2249*38c8a9a5SSteve French 	if (ses->dfs_root_ses)
2250*38c8a9a5SSteve French 		ses->dfs_root_ses->ses_count++;
2251*38c8a9a5SSteve French 	list_add(&ses->smb_ses_list, &server->smb_ses_list);
2252*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
2253*38c8a9a5SSteve French 
2254*38c8a9a5SSteve French 	cifs_setup_ipc(ses, ctx);
2255*38c8a9a5SSteve French 
2256*38c8a9a5SSteve French 	free_xid(xid);
2257*38c8a9a5SSteve French 
2258*38c8a9a5SSteve French 	return ses;
2259*38c8a9a5SSteve French 
2260*38c8a9a5SSteve French get_ses_fail:
2261*38c8a9a5SSteve French 	sesInfoFree(ses);
2262*38c8a9a5SSteve French 	free_xid(xid);
2263*38c8a9a5SSteve French 	return ERR_PTR(rc);
2264*38c8a9a5SSteve French }
2265*38c8a9a5SSteve French 
2266*38c8a9a5SSteve French /* this function must be called with tc_lock held */
2267*38c8a9a5SSteve French static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
2268*38c8a9a5SSteve French {
2269*38c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
2270*38c8a9a5SSteve French 
2271*38c8a9a5SSteve French 	if (tcon->status == TID_EXITING)
2272*38c8a9a5SSteve French 		return 0;
2273*38c8a9a5SSteve French 	/* Skip UNC validation when matching DFS connections or superblocks */
2274*38c8a9a5SSteve French 	if (!server->origin_fullpath && !server->leaf_fullpath &&
2275*38c8a9a5SSteve French 	    strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE))
2276*38c8a9a5SSteve French 		return 0;
2277*38c8a9a5SSteve French 	if (tcon->seal != ctx->seal)
2278*38c8a9a5SSteve French 		return 0;
2279*38c8a9a5SSteve French 	if (tcon->snapshot_time != ctx->snapshot_time)
2280*38c8a9a5SSteve French 		return 0;
2281*38c8a9a5SSteve French 	if (tcon->handle_timeout != ctx->handle_timeout)
2282*38c8a9a5SSteve French 		return 0;
2283*38c8a9a5SSteve French 	if (tcon->no_lease != ctx->no_lease)
2284*38c8a9a5SSteve French 		return 0;
2285*38c8a9a5SSteve French 	if (tcon->nodelete != ctx->nodelete)
2286*38c8a9a5SSteve French 		return 0;
2287*38c8a9a5SSteve French 	return 1;
2288*38c8a9a5SSteve French }
2289*38c8a9a5SSteve French 
2290*38c8a9a5SSteve French static struct cifs_tcon *
2291*38c8a9a5SSteve French cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2292*38c8a9a5SSteve French {
2293*38c8a9a5SSteve French 	struct cifs_tcon *tcon;
2294*38c8a9a5SSteve French 
2295*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
2296*38c8a9a5SSteve French 	list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2297*38c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
2298*38c8a9a5SSteve French 		if (!match_tcon(tcon, ctx)) {
2299*38c8a9a5SSteve French 			spin_unlock(&tcon->tc_lock);
2300*38c8a9a5SSteve French 			continue;
2301*38c8a9a5SSteve French 		}
2302*38c8a9a5SSteve French 		++tcon->tc_count;
2303*38c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
2304*38c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
2305*38c8a9a5SSteve French 		return tcon;
2306*38c8a9a5SSteve French 	}
2307*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
2308*38c8a9a5SSteve French 	return NULL;
2309*38c8a9a5SSteve French }
2310*38c8a9a5SSteve French 
2311*38c8a9a5SSteve French void
2312*38c8a9a5SSteve French cifs_put_tcon(struct cifs_tcon *tcon)
2313*38c8a9a5SSteve French {
2314*38c8a9a5SSteve French 	unsigned int xid;
2315*38c8a9a5SSteve French 	struct cifs_ses *ses;
2316*38c8a9a5SSteve French 
2317*38c8a9a5SSteve French 	/*
2318*38c8a9a5SSteve French 	 * IPC tcon share the lifetime of their session and are
2319*38c8a9a5SSteve French 	 * destroyed in the session put function
2320*38c8a9a5SSteve French 	 */
2321*38c8a9a5SSteve French 	if (tcon == NULL || tcon->ipc)
2322*38c8a9a5SSteve French 		return;
2323*38c8a9a5SSteve French 
2324*38c8a9a5SSteve French 	ses = tcon->ses;
2325*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
2326*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
2327*38c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
2328*38c8a9a5SSteve French 	if (--tcon->tc_count > 0) {
2329*38c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
2330*38c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
2331*38c8a9a5SSteve French 		return;
2332*38c8a9a5SSteve French 	}
2333*38c8a9a5SSteve French 
2334*38c8a9a5SSteve French 	/* tc_count can never go negative */
2335*38c8a9a5SSteve French 	WARN_ON(tcon->tc_count < 0);
2336*38c8a9a5SSteve French 
2337*38c8a9a5SSteve French 	list_del_init(&tcon->tcon_list);
2338*38c8a9a5SSteve French 	tcon->status = TID_EXITING;
2339*38c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
2340*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
2341*38c8a9a5SSteve French 
2342*38c8a9a5SSteve French 	/* cancel polling of interfaces */
2343*38c8a9a5SSteve French 	cancel_delayed_work_sync(&tcon->query_interfaces);
2344*38c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
2345*38c8a9a5SSteve French 	cancel_delayed_work_sync(&tcon->dfs_cache_work);
2346*38c8a9a5SSteve French #endif
2347*38c8a9a5SSteve French 
2348*38c8a9a5SSteve French 	if (tcon->use_witness) {
2349*38c8a9a5SSteve French 		int rc;
2350*38c8a9a5SSteve French 
2351*38c8a9a5SSteve French 		rc = cifs_swn_unregister(tcon);
2352*38c8a9a5SSteve French 		if (rc < 0) {
2353*38c8a9a5SSteve French 			cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2354*38c8a9a5SSteve French 					__func__, rc);
2355*38c8a9a5SSteve French 		}
2356*38c8a9a5SSteve French 	}
2357*38c8a9a5SSteve French 
2358*38c8a9a5SSteve French 	xid = get_xid();
2359*38c8a9a5SSteve French 	if (ses->server->ops->tree_disconnect)
2360*38c8a9a5SSteve French 		ses->server->ops->tree_disconnect(xid, tcon);
2361*38c8a9a5SSteve French 	_free_xid(xid);
2362*38c8a9a5SSteve French 
2363*38c8a9a5SSteve French 	cifs_fscache_release_super_cookie(tcon);
2364*38c8a9a5SSteve French 	tconInfoFree(tcon);
2365*38c8a9a5SSteve French 	cifs_put_smb_ses(ses);
2366*38c8a9a5SSteve French }
2367*38c8a9a5SSteve French 
2368*38c8a9a5SSteve French /**
2369*38c8a9a5SSteve French  * cifs_get_tcon - get a tcon matching @ctx data from @ses
2370*38c8a9a5SSteve French  * @ses: smb session to issue the request on
2371*38c8a9a5SSteve French  * @ctx: the superblock configuration context to use for building the
2372*38c8a9a5SSteve French  *
2373*38c8a9a5SSteve French  * - tcon refcount is the number of mount points using the tcon.
2374*38c8a9a5SSteve French  * - ses refcount is the number of tcon using the session.
2375*38c8a9a5SSteve French  *
2376*38c8a9a5SSteve French  * 1. This function assumes it is being called from cifs_mount() where
2377*38c8a9a5SSteve French  *    we already got a session reference (ses refcount +1).
2378*38c8a9a5SSteve French  *
2379*38c8a9a5SSteve French  * 2. Since we're in the context of adding a mount point, the end
2380*38c8a9a5SSteve French  *    result should be either:
2381*38c8a9a5SSteve French  *
2382*38c8a9a5SSteve French  * a) a new tcon already allocated with refcount=1 (1 mount point) and
2383*38c8a9a5SSteve French  *    its session refcount incremented (1 new tcon). This +1 was
2384*38c8a9a5SSteve French  *    already done in (1).
2385*38c8a9a5SSteve French  *
2386*38c8a9a5SSteve French  * b) an existing tcon with refcount+1 (add a mount point to it) and
2387*38c8a9a5SSteve French  *    identical ses refcount (no new tcon). Because of (1) we need to
2388*38c8a9a5SSteve French  *    decrement the ses refcount.
2389*38c8a9a5SSteve French  */
2390*38c8a9a5SSteve French static struct cifs_tcon *
2391*38c8a9a5SSteve French cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2392*38c8a9a5SSteve French {
2393*38c8a9a5SSteve French 	int rc, xid;
2394*38c8a9a5SSteve French 	struct cifs_tcon *tcon;
2395*38c8a9a5SSteve French 
2396*38c8a9a5SSteve French 	tcon = cifs_find_tcon(ses, ctx);
2397*38c8a9a5SSteve French 	if (tcon) {
2398*38c8a9a5SSteve French 		/*
2399*38c8a9a5SSteve French 		 * tcon has refcount already incremented but we need to
2400*38c8a9a5SSteve French 		 * decrement extra ses reference gotten by caller (case b)
2401*38c8a9a5SSteve French 		 */
2402*38c8a9a5SSteve French 		cifs_dbg(FYI, "Found match on UNC path\n");
2403*38c8a9a5SSteve French 		cifs_put_smb_ses(ses);
2404*38c8a9a5SSteve French 		return tcon;
2405*38c8a9a5SSteve French 	}
2406*38c8a9a5SSteve French 
2407*38c8a9a5SSteve French 	if (!ses->server->ops->tree_connect) {
2408*38c8a9a5SSteve French 		rc = -ENOSYS;
2409*38c8a9a5SSteve French 		goto out_fail;
2410*38c8a9a5SSteve French 	}
2411*38c8a9a5SSteve French 
2412*38c8a9a5SSteve French 	tcon = tconInfoAlloc();
2413*38c8a9a5SSteve French 	if (tcon == NULL) {
2414*38c8a9a5SSteve French 		rc = -ENOMEM;
2415*38c8a9a5SSteve French 		goto out_fail;
2416*38c8a9a5SSteve French 	}
2417*38c8a9a5SSteve French 
2418*38c8a9a5SSteve French 	if (ctx->snapshot_time) {
2419*38c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
2420*38c8a9a5SSteve French 			cifs_dbg(VFS,
2421*38c8a9a5SSteve French 			     "Use SMB2 or later for snapshot mount option\n");
2422*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2423*38c8a9a5SSteve French 			goto out_fail;
2424*38c8a9a5SSteve French 		} else
2425*38c8a9a5SSteve French 			tcon->snapshot_time = ctx->snapshot_time;
2426*38c8a9a5SSteve French 	}
2427*38c8a9a5SSteve French 
2428*38c8a9a5SSteve French 	if (ctx->handle_timeout) {
2429*38c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
2430*38c8a9a5SSteve French 			cifs_dbg(VFS,
2431*38c8a9a5SSteve French 			     "Use SMB2.1 or later for handle timeout option\n");
2432*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2433*38c8a9a5SSteve French 			goto out_fail;
2434*38c8a9a5SSteve French 		} else
2435*38c8a9a5SSteve French 			tcon->handle_timeout = ctx->handle_timeout;
2436*38c8a9a5SSteve French 	}
2437*38c8a9a5SSteve French 
2438*38c8a9a5SSteve French 	tcon->ses = ses;
2439*38c8a9a5SSteve French 	if (ctx->password) {
2440*38c8a9a5SSteve French 		tcon->password = kstrdup(ctx->password, GFP_KERNEL);
2441*38c8a9a5SSteve French 		if (!tcon->password) {
2442*38c8a9a5SSteve French 			rc = -ENOMEM;
2443*38c8a9a5SSteve French 			goto out_fail;
2444*38c8a9a5SSteve French 		}
2445*38c8a9a5SSteve French 	}
2446*38c8a9a5SSteve French 
2447*38c8a9a5SSteve French 	if (ctx->seal) {
2448*38c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
2449*38c8a9a5SSteve French 			cifs_dbg(VFS,
2450*38c8a9a5SSteve French 				 "SMB3 or later required for encryption\n");
2451*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2452*38c8a9a5SSteve French 			goto out_fail;
2453*38c8a9a5SSteve French 		} else if (tcon->ses->server->capabilities &
2454*38c8a9a5SSteve French 					SMB2_GLOBAL_CAP_ENCRYPTION)
2455*38c8a9a5SSteve French 			tcon->seal = true;
2456*38c8a9a5SSteve French 		else {
2457*38c8a9a5SSteve French 			cifs_dbg(VFS, "Encryption is not supported on share\n");
2458*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2459*38c8a9a5SSteve French 			goto out_fail;
2460*38c8a9a5SSteve French 		}
2461*38c8a9a5SSteve French 	}
2462*38c8a9a5SSteve French 
2463*38c8a9a5SSteve French 	if (ctx->linux_ext) {
2464*38c8a9a5SSteve French 		if (ses->server->posix_ext_supported) {
2465*38c8a9a5SSteve French 			tcon->posix_extensions = true;
2466*38c8a9a5SSteve French 			pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
2467*38c8a9a5SSteve French 		} else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
2468*38c8a9a5SSteve French 		    (strcmp(ses->server->vals->version_string,
2469*38c8a9a5SSteve French 		     SMB3ANY_VERSION_STRING) == 0) ||
2470*38c8a9a5SSteve French 		    (strcmp(ses->server->vals->version_string,
2471*38c8a9a5SSteve French 		     SMBDEFAULT_VERSION_STRING) == 0)) {
2472*38c8a9a5SSteve French 			cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
2473*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2474*38c8a9a5SSteve French 			goto out_fail;
2475*38c8a9a5SSteve French 		} else {
2476*38c8a9a5SSteve French 			cifs_dbg(VFS, "Check vers= mount option. SMB3.11 "
2477*38c8a9a5SSteve French 				"disabled but required for POSIX extensions\n");
2478*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2479*38c8a9a5SSteve French 			goto out_fail;
2480*38c8a9a5SSteve French 		}
2481*38c8a9a5SSteve French 	}
2482*38c8a9a5SSteve French 
2483*38c8a9a5SSteve French 	xid = get_xid();
2484*38c8a9a5SSteve French 	rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2485*38c8a9a5SSteve French 					    ctx->local_nls);
2486*38c8a9a5SSteve French 	free_xid(xid);
2487*38c8a9a5SSteve French 	cifs_dbg(FYI, "Tcon rc = %d\n", rc);
2488*38c8a9a5SSteve French 	if (rc)
2489*38c8a9a5SSteve French 		goto out_fail;
2490*38c8a9a5SSteve French 
2491*38c8a9a5SSteve French 	tcon->use_persistent = false;
2492*38c8a9a5SSteve French 	/* check if SMB2 or later, CIFS does not support persistent handles */
2493*38c8a9a5SSteve French 	if (ctx->persistent) {
2494*38c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
2495*38c8a9a5SSteve French 			cifs_dbg(VFS,
2496*38c8a9a5SSteve French 			     "SMB3 or later required for persistent handles\n");
2497*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2498*38c8a9a5SSteve French 			goto out_fail;
2499*38c8a9a5SSteve French 		} else if (ses->server->capabilities &
2500*38c8a9a5SSteve French 			   SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2501*38c8a9a5SSteve French 			tcon->use_persistent = true;
2502*38c8a9a5SSteve French 		else /* persistent handles requested but not supported */ {
2503*38c8a9a5SSteve French 			cifs_dbg(VFS,
2504*38c8a9a5SSteve French 				"Persistent handles not supported on share\n");
2505*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2506*38c8a9a5SSteve French 			goto out_fail;
2507*38c8a9a5SSteve French 		}
2508*38c8a9a5SSteve French 	} else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2509*38c8a9a5SSteve French 	     && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2510*38c8a9a5SSteve French 	     && (ctx->nopersistent == false)) {
2511*38c8a9a5SSteve French 		cifs_dbg(FYI, "enabling persistent handles\n");
2512*38c8a9a5SSteve French 		tcon->use_persistent = true;
2513*38c8a9a5SSteve French 	} else if (ctx->resilient) {
2514*38c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
2515*38c8a9a5SSteve French 			cifs_dbg(VFS,
2516*38c8a9a5SSteve French 			     "SMB2.1 or later required for resilient handles\n");
2517*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2518*38c8a9a5SSteve French 			goto out_fail;
2519*38c8a9a5SSteve French 		}
2520*38c8a9a5SSteve French 		tcon->use_resilient = true;
2521*38c8a9a5SSteve French 	}
2522*38c8a9a5SSteve French 
2523*38c8a9a5SSteve French 	tcon->use_witness = false;
2524*38c8a9a5SSteve French 	if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
2525*38c8a9a5SSteve French 		if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2526*38c8a9a5SSteve French 			if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
2527*38c8a9a5SSteve French 				/*
2528*38c8a9a5SSteve French 				 * Set witness in use flag in first place
2529*38c8a9a5SSteve French 				 * to retry registration in the echo task
2530*38c8a9a5SSteve French 				 */
2531*38c8a9a5SSteve French 				tcon->use_witness = true;
2532*38c8a9a5SSteve French 				/* And try to register immediately */
2533*38c8a9a5SSteve French 				rc = cifs_swn_register(tcon);
2534*38c8a9a5SSteve French 				if (rc < 0) {
2535*38c8a9a5SSteve French 					cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2536*38c8a9a5SSteve French 					goto out_fail;
2537*38c8a9a5SSteve French 				}
2538*38c8a9a5SSteve French 			} else {
2539*38c8a9a5SSteve French 				/* TODO: try to extend for non-cluster uses (eg multichannel) */
2540*38c8a9a5SSteve French 				cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2541*38c8a9a5SSteve French 				rc = -EOPNOTSUPP;
2542*38c8a9a5SSteve French 				goto out_fail;
2543*38c8a9a5SSteve French 			}
2544*38c8a9a5SSteve French 		} else {
2545*38c8a9a5SSteve French 			cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2546*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2547*38c8a9a5SSteve French 			goto out_fail;
2548*38c8a9a5SSteve French 		}
2549*38c8a9a5SSteve French 	}
2550*38c8a9a5SSteve French 
2551*38c8a9a5SSteve French 	/* If the user really knows what they are doing they can override */
2552*38c8a9a5SSteve French 	if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
2553*38c8a9a5SSteve French 		if (ctx->cache_ro)
2554*38c8a9a5SSteve French 			cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
2555*38c8a9a5SSteve French 		else if (ctx->cache_rw)
2556*38c8a9a5SSteve French 			cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2557*38c8a9a5SSteve French 	}
2558*38c8a9a5SSteve French 
2559*38c8a9a5SSteve French 	if (ctx->no_lease) {
2560*38c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
2561*38c8a9a5SSteve French 			cifs_dbg(VFS,
2562*38c8a9a5SSteve French 				"SMB2 or later required for nolease option\n");
2563*38c8a9a5SSteve French 			rc = -EOPNOTSUPP;
2564*38c8a9a5SSteve French 			goto out_fail;
2565*38c8a9a5SSteve French 		} else
2566*38c8a9a5SSteve French 			tcon->no_lease = ctx->no_lease;
2567*38c8a9a5SSteve French 	}
2568*38c8a9a5SSteve French 
2569*38c8a9a5SSteve French 	/*
2570*38c8a9a5SSteve French 	 * We can have only one retry value for a connection to a share so for
2571*38c8a9a5SSteve French 	 * resources mounted more than once to the same server share the last
2572*38c8a9a5SSteve French 	 * value passed in for the retry flag is used.
2573*38c8a9a5SSteve French 	 */
2574*38c8a9a5SSteve French 	tcon->retry = ctx->retry;
2575*38c8a9a5SSteve French 	tcon->nocase = ctx->nocase;
2576*38c8a9a5SSteve French 	tcon->broken_sparse_sup = ctx->no_sparse;
2577*38c8a9a5SSteve French 	if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
2578*38c8a9a5SSteve French 		tcon->nohandlecache = ctx->nohandlecache;
2579*38c8a9a5SSteve French 	else
2580*38c8a9a5SSteve French 		tcon->nohandlecache = true;
2581*38c8a9a5SSteve French 	tcon->nodelete = ctx->nodelete;
2582*38c8a9a5SSteve French 	tcon->local_lease = ctx->local_lease;
2583*38c8a9a5SSteve French 	INIT_LIST_HEAD(&tcon->pending_opens);
2584*38c8a9a5SSteve French 	tcon->status = TID_GOOD;
2585*38c8a9a5SSteve French 
2586*38c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcon->query_interfaces,
2587*38c8a9a5SSteve French 			  smb2_query_server_interfaces);
2588*38c8a9a5SSteve French 	if (ses->server->dialect >= SMB30_PROT_ID &&
2589*38c8a9a5SSteve French 	    (ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
2590*38c8a9a5SSteve French 		/* schedule query interfaces poll */
2591*38c8a9a5SSteve French 		queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
2592*38c8a9a5SSteve French 				   (SMB_INTERFACE_POLL_INTERVAL * HZ));
2593*38c8a9a5SSteve French 	}
2594*38c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
2595*38c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcon->dfs_cache_work, dfs_cache_refresh);
2596*38c8a9a5SSteve French #endif
2597*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
2598*38c8a9a5SSteve French 	list_add(&tcon->tcon_list, &ses->tcon_list);
2599*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
2600*38c8a9a5SSteve French 
2601*38c8a9a5SSteve French 	return tcon;
2602*38c8a9a5SSteve French 
2603*38c8a9a5SSteve French out_fail:
2604*38c8a9a5SSteve French 	tconInfoFree(tcon);
2605*38c8a9a5SSteve French 	return ERR_PTR(rc);
2606*38c8a9a5SSteve French }
2607*38c8a9a5SSteve French 
2608*38c8a9a5SSteve French void
2609*38c8a9a5SSteve French cifs_put_tlink(struct tcon_link *tlink)
2610*38c8a9a5SSteve French {
2611*38c8a9a5SSteve French 	if (!tlink || IS_ERR(tlink))
2612*38c8a9a5SSteve French 		return;
2613*38c8a9a5SSteve French 
2614*38c8a9a5SSteve French 	if (!atomic_dec_and_test(&tlink->tl_count) ||
2615*38c8a9a5SSteve French 	    test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2616*38c8a9a5SSteve French 		tlink->tl_time = jiffies;
2617*38c8a9a5SSteve French 		return;
2618*38c8a9a5SSteve French 	}
2619*38c8a9a5SSteve French 
2620*38c8a9a5SSteve French 	if (!IS_ERR(tlink_tcon(tlink)))
2621*38c8a9a5SSteve French 		cifs_put_tcon(tlink_tcon(tlink));
2622*38c8a9a5SSteve French 	kfree(tlink);
2623*38c8a9a5SSteve French 	return;
2624*38c8a9a5SSteve French }
2625*38c8a9a5SSteve French 
2626*38c8a9a5SSteve French static int
2627*38c8a9a5SSteve French compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2628*38c8a9a5SSteve French {
2629*38c8a9a5SSteve French 	struct cifs_sb_info *old = CIFS_SB(sb);
2630*38c8a9a5SSteve French 	struct cifs_sb_info *new = mnt_data->cifs_sb;
2631*38c8a9a5SSteve French 	unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2632*38c8a9a5SSteve French 	unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
2633*38c8a9a5SSteve French 
2634*38c8a9a5SSteve French 	if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2635*38c8a9a5SSteve French 		return 0;
2636*38c8a9a5SSteve French 
2637*38c8a9a5SSteve French 	if (old->mnt_cifs_serverino_autodisabled)
2638*38c8a9a5SSteve French 		newflags &= ~CIFS_MOUNT_SERVER_INUM;
2639*38c8a9a5SSteve French 
2640*38c8a9a5SSteve French 	if (oldflags != newflags)
2641*38c8a9a5SSteve French 		return 0;
2642*38c8a9a5SSteve French 
2643*38c8a9a5SSteve French 	/*
2644*38c8a9a5SSteve French 	 * We want to share sb only if we don't specify an r/wsize or
2645*38c8a9a5SSteve French 	 * specified r/wsize is greater than or equal to existing one.
2646*38c8a9a5SSteve French 	 */
2647*38c8a9a5SSteve French 	if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
2648*38c8a9a5SSteve French 		return 0;
2649*38c8a9a5SSteve French 
2650*38c8a9a5SSteve French 	if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
2651*38c8a9a5SSteve French 		return 0;
2652*38c8a9a5SSteve French 
2653*38c8a9a5SSteve French 	if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2654*38c8a9a5SSteve French 	    !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
2655*38c8a9a5SSteve French 		return 0;
2656*38c8a9a5SSteve French 
2657*38c8a9a5SSteve French 	if (old->ctx->file_mode != new->ctx->file_mode ||
2658*38c8a9a5SSteve French 	    old->ctx->dir_mode != new->ctx->dir_mode)
2659*38c8a9a5SSteve French 		return 0;
2660*38c8a9a5SSteve French 
2661*38c8a9a5SSteve French 	if (strcmp(old->local_nls->charset, new->local_nls->charset))
2662*38c8a9a5SSteve French 		return 0;
2663*38c8a9a5SSteve French 
2664*38c8a9a5SSteve French 	if (old->ctx->acregmax != new->ctx->acregmax)
2665*38c8a9a5SSteve French 		return 0;
2666*38c8a9a5SSteve French 	if (old->ctx->acdirmax != new->ctx->acdirmax)
2667*38c8a9a5SSteve French 		return 0;
2668*38c8a9a5SSteve French 	if (old->ctx->closetimeo != new->ctx->closetimeo)
2669*38c8a9a5SSteve French 		return 0;
2670*38c8a9a5SSteve French 
2671*38c8a9a5SSteve French 	return 1;
2672*38c8a9a5SSteve French }
2673*38c8a9a5SSteve French 
2674*38c8a9a5SSteve French static int match_prepath(struct super_block *sb,
2675*38c8a9a5SSteve French 			 struct TCP_Server_Info *server,
2676*38c8a9a5SSteve French 			 struct cifs_mnt_data *mnt_data)
2677*38c8a9a5SSteve French {
2678*38c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_data->ctx;
2679*38c8a9a5SSteve French 	struct cifs_sb_info *old = CIFS_SB(sb);
2680*38c8a9a5SSteve French 	struct cifs_sb_info *new = mnt_data->cifs_sb;
2681*38c8a9a5SSteve French 	bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2682*38c8a9a5SSteve French 		old->prepath;
2683*38c8a9a5SSteve French 	bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2684*38c8a9a5SSteve French 		new->prepath;
2685*38c8a9a5SSteve French 
2686*38c8a9a5SSteve French 	if (server->origin_fullpath &&
2687*38c8a9a5SSteve French 	    dfs_src_pathname_equal(server->origin_fullpath, ctx->source))
2688*38c8a9a5SSteve French 		return 1;
2689*38c8a9a5SSteve French 
2690*38c8a9a5SSteve French 	if (old_set && new_set && !strcmp(new->prepath, old->prepath))
2691*38c8a9a5SSteve French 		return 1;
2692*38c8a9a5SSteve French 	else if (!old_set && !new_set)
2693*38c8a9a5SSteve French 		return 1;
2694*38c8a9a5SSteve French 
2695*38c8a9a5SSteve French 	return 0;
2696*38c8a9a5SSteve French }
2697*38c8a9a5SSteve French 
2698*38c8a9a5SSteve French int
2699*38c8a9a5SSteve French cifs_match_super(struct super_block *sb, void *data)
2700*38c8a9a5SSteve French {
2701*38c8a9a5SSteve French 	struct cifs_mnt_data *mnt_data = data;
2702*38c8a9a5SSteve French 	struct smb3_fs_context *ctx;
2703*38c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
2704*38c8a9a5SSteve French 	struct TCP_Server_Info *tcp_srv;
2705*38c8a9a5SSteve French 	struct cifs_ses *ses;
2706*38c8a9a5SSteve French 	struct cifs_tcon *tcon;
2707*38c8a9a5SSteve French 	struct tcon_link *tlink;
2708*38c8a9a5SSteve French 	int rc = 0;
2709*38c8a9a5SSteve French 
2710*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
2711*38c8a9a5SSteve French 	cifs_sb = CIFS_SB(sb);
2712*38c8a9a5SSteve French 
2713*38c8a9a5SSteve French 	/* We do not want to use a superblock that has been shutdown */
2714*38c8a9a5SSteve French 	if (CIFS_MOUNT_SHUTDOWN & cifs_sb->mnt_cifs_flags) {
2715*38c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
2716*38c8a9a5SSteve French 		return 0;
2717*38c8a9a5SSteve French 	}
2718*38c8a9a5SSteve French 
2719*38c8a9a5SSteve French 	tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2720*38c8a9a5SSteve French 	if (tlink == NULL) {
2721*38c8a9a5SSteve French 		/* can not match superblock if tlink were ever null */
2722*38c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
2723*38c8a9a5SSteve French 		return 0;
2724*38c8a9a5SSteve French 	}
2725*38c8a9a5SSteve French 	tcon = tlink_tcon(tlink);
2726*38c8a9a5SSteve French 	ses = tcon->ses;
2727*38c8a9a5SSteve French 	tcp_srv = ses->server;
2728*38c8a9a5SSteve French 
2729*38c8a9a5SSteve French 	ctx = mnt_data->ctx;
2730*38c8a9a5SSteve French 
2731*38c8a9a5SSteve French 	spin_lock(&tcp_srv->srv_lock);
2732*38c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
2733*38c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
2734*38c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
2735*38c8a9a5SSteve French 	if (!match_server(tcp_srv, ctx) ||
2736*38c8a9a5SSteve French 	    !match_session(ses, ctx) ||
2737*38c8a9a5SSteve French 	    !match_tcon(tcon, ctx) ||
2738*38c8a9a5SSteve French 	    !match_prepath(sb, tcp_srv, mnt_data)) {
2739*38c8a9a5SSteve French 		rc = 0;
2740*38c8a9a5SSteve French 		goto out;
2741*38c8a9a5SSteve French 	}
2742*38c8a9a5SSteve French 
2743*38c8a9a5SSteve French 	rc = compare_mount_options(sb, mnt_data);
2744*38c8a9a5SSteve French out:
2745*38c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
2746*38c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
2747*38c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
2748*38c8a9a5SSteve French 	spin_unlock(&tcp_srv->srv_lock);
2749*38c8a9a5SSteve French 
2750*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
2751*38c8a9a5SSteve French 	cifs_put_tlink(tlink);
2752*38c8a9a5SSteve French 	return rc;
2753*38c8a9a5SSteve French }
2754*38c8a9a5SSteve French 
2755*38c8a9a5SSteve French #ifdef CONFIG_DEBUG_LOCK_ALLOC
2756*38c8a9a5SSteve French static struct lock_class_key cifs_key[2];
2757*38c8a9a5SSteve French static struct lock_class_key cifs_slock_key[2];
2758*38c8a9a5SSteve French 
2759*38c8a9a5SSteve French static inline void
2760*38c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock)
2761*38c8a9a5SSteve French {
2762*38c8a9a5SSteve French 	struct sock *sk = sock->sk;
2763*38c8a9a5SSteve French 	BUG_ON(!sock_allow_reclassification(sk));
2764*38c8a9a5SSteve French 	sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2765*38c8a9a5SSteve French 		&cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2766*38c8a9a5SSteve French }
2767*38c8a9a5SSteve French 
2768*38c8a9a5SSteve French static inline void
2769*38c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock)
2770*38c8a9a5SSteve French {
2771*38c8a9a5SSteve French 	struct sock *sk = sock->sk;
2772*38c8a9a5SSteve French 	BUG_ON(!sock_allow_reclassification(sk));
2773*38c8a9a5SSteve French 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2774*38c8a9a5SSteve French 		&cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2775*38c8a9a5SSteve French }
2776*38c8a9a5SSteve French #else
2777*38c8a9a5SSteve French static inline void
2778*38c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock)
2779*38c8a9a5SSteve French {
2780*38c8a9a5SSteve French }
2781*38c8a9a5SSteve French 
2782*38c8a9a5SSteve French static inline void
2783*38c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock)
2784*38c8a9a5SSteve French {
2785*38c8a9a5SSteve French }
2786*38c8a9a5SSteve French #endif
2787*38c8a9a5SSteve French 
2788*38c8a9a5SSteve French /* See RFC1001 section 14 on representation of Netbios names */
2789*38c8a9a5SSteve French static void rfc1002mangle(char *target, char *source, unsigned int length)
2790*38c8a9a5SSteve French {
2791*38c8a9a5SSteve French 	unsigned int i, j;
2792*38c8a9a5SSteve French 
2793*38c8a9a5SSteve French 	for (i = 0, j = 0; i < (length); i++) {
2794*38c8a9a5SSteve French 		/* mask a nibble at a time and encode */
2795*38c8a9a5SSteve French 		target[j] = 'A' + (0x0F & (source[i] >> 4));
2796*38c8a9a5SSteve French 		target[j+1] = 'A' + (0x0F & source[i]);
2797*38c8a9a5SSteve French 		j += 2;
2798*38c8a9a5SSteve French 	}
2799*38c8a9a5SSteve French 
2800*38c8a9a5SSteve French }
2801*38c8a9a5SSteve French 
2802*38c8a9a5SSteve French static int
2803*38c8a9a5SSteve French bind_socket(struct TCP_Server_Info *server)
2804*38c8a9a5SSteve French {
2805*38c8a9a5SSteve French 	int rc = 0;
2806*38c8a9a5SSteve French 	if (server->srcaddr.ss_family != AF_UNSPEC) {
2807*38c8a9a5SSteve French 		/* Bind to the specified local IP address */
2808*38c8a9a5SSteve French 		struct socket *socket = server->ssocket;
2809*38c8a9a5SSteve French 		rc = socket->ops->bind(socket,
2810*38c8a9a5SSteve French 				       (struct sockaddr *) &server->srcaddr,
2811*38c8a9a5SSteve French 				       sizeof(server->srcaddr));
2812*38c8a9a5SSteve French 		if (rc < 0) {
2813*38c8a9a5SSteve French 			struct sockaddr_in *saddr4;
2814*38c8a9a5SSteve French 			struct sockaddr_in6 *saddr6;
2815*38c8a9a5SSteve French 			saddr4 = (struct sockaddr_in *)&server->srcaddr;
2816*38c8a9a5SSteve French 			saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2817*38c8a9a5SSteve French 			if (saddr6->sin6_family == AF_INET6)
2818*38c8a9a5SSteve French 				cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
2819*38c8a9a5SSteve French 					 &saddr6->sin6_addr, rc);
2820*38c8a9a5SSteve French 			else
2821*38c8a9a5SSteve French 				cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
2822*38c8a9a5SSteve French 					 &saddr4->sin_addr.s_addr, rc);
2823*38c8a9a5SSteve French 		}
2824*38c8a9a5SSteve French 	}
2825*38c8a9a5SSteve French 	return rc;
2826*38c8a9a5SSteve French }
2827*38c8a9a5SSteve French 
2828*38c8a9a5SSteve French static int
2829*38c8a9a5SSteve French ip_rfc1001_connect(struct TCP_Server_Info *server)
2830*38c8a9a5SSteve French {
2831*38c8a9a5SSteve French 	int rc = 0;
2832*38c8a9a5SSteve French 	/*
2833*38c8a9a5SSteve French 	 * some servers require RFC1001 sessinit before sending
2834*38c8a9a5SSteve French 	 * negprot - BB check reconnection in case where second
2835*38c8a9a5SSteve French 	 * sessinit is sent but no second negprot
2836*38c8a9a5SSteve French 	 */
2837*38c8a9a5SSteve French 	struct rfc1002_session_packet req = {};
2838*38c8a9a5SSteve French 	struct smb_hdr *smb_buf = (struct smb_hdr *)&req;
2839*38c8a9a5SSteve French 	unsigned int len;
2840*38c8a9a5SSteve French 
2841*38c8a9a5SSteve French 	req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name);
2842*38c8a9a5SSteve French 
2843*38c8a9a5SSteve French 	if (server->server_RFC1001_name[0] != 0)
2844*38c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.called_name,
2845*38c8a9a5SSteve French 			      server->server_RFC1001_name,
2846*38c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
2847*38c8a9a5SSteve French 	else
2848*38c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.called_name,
2849*38c8a9a5SSteve French 			      DEFAULT_CIFS_CALLED_NAME,
2850*38c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
2851*38c8a9a5SSteve French 
2852*38c8a9a5SSteve French 	req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name);
2853*38c8a9a5SSteve French 
2854*38c8a9a5SSteve French 	/* calling name ends in null (byte 16) from old smb convention */
2855*38c8a9a5SSteve French 	if (server->workstation_RFC1001_name[0] != 0)
2856*38c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.calling_name,
2857*38c8a9a5SSteve French 			      server->workstation_RFC1001_name,
2858*38c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
2859*38c8a9a5SSteve French 	else
2860*38c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.calling_name,
2861*38c8a9a5SSteve French 			      "LINUX_CIFS_CLNT",
2862*38c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
2863*38c8a9a5SSteve French 
2864*38c8a9a5SSteve French 	/*
2865*38c8a9a5SSteve French 	 * As per rfc1002, @len must be the number of bytes that follows the
2866*38c8a9a5SSteve French 	 * length field of a rfc1002 session request payload.
2867*38c8a9a5SSteve French 	 */
2868*38c8a9a5SSteve French 	len = sizeof(req) - offsetof(struct rfc1002_session_packet, trailer.session_req);
2869*38c8a9a5SSteve French 
2870*38c8a9a5SSteve French 	smb_buf->smb_buf_length = cpu_to_be32((RFC1002_SESSION_REQUEST << 24) | len);
2871*38c8a9a5SSteve French 	rc = smb_send(server, smb_buf, len);
2872*38c8a9a5SSteve French 	/*
2873*38c8a9a5SSteve French 	 * RFC1001 layer in at least one server requires very short break before
2874*38c8a9a5SSteve French 	 * negprot presumably because not expecting negprot to follow so fast.
2875*38c8a9a5SSteve French 	 * This is a simple solution that works without complicating the code
2876*38c8a9a5SSteve French 	 * and causes no significant slowing down on mount for everyone else
2877*38c8a9a5SSteve French 	 */
2878*38c8a9a5SSteve French 	usleep_range(1000, 2000);
2879*38c8a9a5SSteve French 
2880*38c8a9a5SSteve French 	return rc;
2881*38c8a9a5SSteve French }
2882*38c8a9a5SSteve French 
2883*38c8a9a5SSteve French static int
2884*38c8a9a5SSteve French generic_ip_connect(struct TCP_Server_Info *server)
2885*38c8a9a5SSteve French {
2886*38c8a9a5SSteve French 	int rc = 0;
2887*38c8a9a5SSteve French 	__be16 sport;
2888*38c8a9a5SSteve French 	int slen, sfamily;
2889*38c8a9a5SSteve French 	struct socket *socket = server->ssocket;
2890*38c8a9a5SSteve French 	struct sockaddr *saddr;
2891*38c8a9a5SSteve French 
2892*38c8a9a5SSteve French 	saddr = (struct sockaddr *) &server->dstaddr;
2893*38c8a9a5SSteve French 
2894*38c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6) {
2895*38c8a9a5SSteve French 		struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2896*38c8a9a5SSteve French 
2897*38c8a9a5SSteve French 		sport = ipv6->sin6_port;
2898*38c8a9a5SSteve French 		slen = sizeof(struct sockaddr_in6);
2899*38c8a9a5SSteve French 		sfamily = AF_INET6;
2900*38c8a9a5SSteve French 		cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2901*38c8a9a5SSteve French 				ntohs(sport));
2902*38c8a9a5SSteve French 	} else {
2903*38c8a9a5SSteve French 		struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2904*38c8a9a5SSteve French 
2905*38c8a9a5SSteve French 		sport = ipv4->sin_port;
2906*38c8a9a5SSteve French 		slen = sizeof(struct sockaddr_in);
2907*38c8a9a5SSteve French 		sfamily = AF_INET;
2908*38c8a9a5SSteve French 		cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2909*38c8a9a5SSteve French 				ntohs(sport));
2910*38c8a9a5SSteve French 	}
2911*38c8a9a5SSteve French 
2912*38c8a9a5SSteve French 	if (socket == NULL) {
2913*38c8a9a5SSteve French 		rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2914*38c8a9a5SSteve French 				   IPPROTO_TCP, &socket, 1);
2915*38c8a9a5SSteve French 		if (rc < 0) {
2916*38c8a9a5SSteve French 			cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
2917*38c8a9a5SSteve French 			server->ssocket = NULL;
2918*38c8a9a5SSteve French 			return rc;
2919*38c8a9a5SSteve French 		}
2920*38c8a9a5SSteve French 
2921*38c8a9a5SSteve French 		/* BB other socket options to set KEEPALIVE, NODELAY? */
2922*38c8a9a5SSteve French 		cifs_dbg(FYI, "Socket created\n");
2923*38c8a9a5SSteve French 		server->ssocket = socket;
2924*38c8a9a5SSteve French 		socket->sk->sk_allocation = GFP_NOFS;
2925*38c8a9a5SSteve French 		socket->sk->sk_use_task_frag = false;
2926*38c8a9a5SSteve French 		if (sfamily == AF_INET6)
2927*38c8a9a5SSteve French 			cifs_reclassify_socket6(socket);
2928*38c8a9a5SSteve French 		else
2929*38c8a9a5SSteve French 			cifs_reclassify_socket4(socket);
2930*38c8a9a5SSteve French 	}
2931*38c8a9a5SSteve French 
2932*38c8a9a5SSteve French 	rc = bind_socket(server);
2933*38c8a9a5SSteve French 	if (rc < 0)
2934*38c8a9a5SSteve French 		return rc;
2935*38c8a9a5SSteve French 
2936*38c8a9a5SSteve French 	/*
2937*38c8a9a5SSteve French 	 * Eventually check for other socket options to change from
2938*38c8a9a5SSteve French 	 * the default. sock_setsockopt not used because it expects
2939*38c8a9a5SSteve French 	 * user space buffer
2940*38c8a9a5SSteve French 	 */
2941*38c8a9a5SSteve French 	socket->sk->sk_rcvtimeo = 7 * HZ;
2942*38c8a9a5SSteve French 	socket->sk->sk_sndtimeo = 5 * HZ;
2943*38c8a9a5SSteve French 
2944*38c8a9a5SSteve French 	/* make the bufsizes depend on wsize/rsize and max requests */
2945*38c8a9a5SSteve French 	if (server->noautotune) {
2946*38c8a9a5SSteve French 		if (socket->sk->sk_sndbuf < (200 * 1024))
2947*38c8a9a5SSteve French 			socket->sk->sk_sndbuf = 200 * 1024;
2948*38c8a9a5SSteve French 		if (socket->sk->sk_rcvbuf < (140 * 1024))
2949*38c8a9a5SSteve French 			socket->sk->sk_rcvbuf = 140 * 1024;
2950*38c8a9a5SSteve French 	}
2951*38c8a9a5SSteve French 
2952*38c8a9a5SSteve French 	if (server->tcp_nodelay)
2953*38c8a9a5SSteve French 		tcp_sock_set_nodelay(socket->sk);
2954*38c8a9a5SSteve French 
2955*38c8a9a5SSteve French 	cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
2956*38c8a9a5SSteve French 		 socket->sk->sk_sndbuf,
2957*38c8a9a5SSteve French 		 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2958*38c8a9a5SSteve French 
2959*38c8a9a5SSteve French 	rc = socket->ops->connect(socket, saddr, slen,
2960*38c8a9a5SSteve French 				  server->noblockcnt ? O_NONBLOCK : 0);
2961*38c8a9a5SSteve French 	/*
2962*38c8a9a5SSteve French 	 * When mounting SMB root file systems, we do not want to block in
2963*38c8a9a5SSteve French 	 * connect. Otherwise bail out and then let cifs_reconnect() perform
2964*38c8a9a5SSteve French 	 * reconnect failover - if possible.
2965*38c8a9a5SSteve French 	 */
2966*38c8a9a5SSteve French 	if (server->noblockcnt && rc == -EINPROGRESS)
2967*38c8a9a5SSteve French 		rc = 0;
2968*38c8a9a5SSteve French 	if (rc < 0) {
2969*38c8a9a5SSteve French 		cifs_dbg(FYI, "Error %d connecting to server\n", rc);
2970*38c8a9a5SSteve French 		trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
2971*38c8a9a5SSteve French 		sock_release(socket);
2972*38c8a9a5SSteve French 		server->ssocket = NULL;
2973*38c8a9a5SSteve French 		return rc;
2974*38c8a9a5SSteve French 	}
2975*38c8a9a5SSteve French 	trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
2976*38c8a9a5SSteve French 	if (sport == htons(RFC1001_PORT))
2977*38c8a9a5SSteve French 		rc = ip_rfc1001_connect(server);
2978*38c8a9a5SSteve French 
2979*38c8a9a5SSteve French 	return rc;
2980*38c8a9a5SSteve French }
2981*38c8a9a5SSteve French 
2982*38c8a9a5SSteve French static int
2983*38c8a9a5SSteve French ip_connect(struct TCP_Server_Info *server)
2984*38c8a9a5SSteve French {
2985*38c8a9a5SSteve French 	__be16 *sport;
2986*38c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2987*38c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2988*38c8a9a5SSteve French 
2989*38c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6)
2990*38c8a9a5SSteve French 		sport = &addr6->sin6_port;
2991*38c8a9a5SSteve French 	else
2992*38c8a9a5SSteve French 		sport = &addr->sin_port;
2993*38c8a9a5SSteve French 
2994*38c8a9a5SSteve French 	if (*sport == 0) {
2995*38c8a9a5SSteve French 		int rc;
2996*38c8a9a5SSteve French 
2997*38c8a9a5SSteve French 		/* try with 445 port at first */
2998*38c8a9a5SSteve French 		*sport = htons(CIFS_PORT);
2999*38c8a9a5SSteve French 
3000*38c8a9a5SSteve French 		rc = generic_ip_connect(server);
3001*38c8a9a5SSteve French 		if (rc >= 0)
3002*38c8a9a5SSteve French 			return rc;
3003*38c8a9a5SSteve French 
3004*38c8a9a5SSteve French 		/* if it failed, try with 139 port */
3005*38c8a9a5SSteve French 		*sport = htons(RFC1001_PORT);
3006*38c8a9a5SSteve French 	}
3007*38c8a9a5SSteve French 
3008*38c8a9a5SSteve French 	return generic_ip_connect(server);
3009*38c8a9a5SSteve French }
3010*38c8a9a5SSteve French 
3011*38c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3012*38c8a9a5SSteve French void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
3013*38c8a9a5SSteve French 			  struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3014*38c8a9a5SSteve French {
3015*38c8a9a5SSteve French 	/*
3016*38c8a9a5SSteve French 	 * If we are reconnecting then should we check to see if
3017*38c8a9a5SSteve French 	 * any requested capabilities changed locally e.g. via
3018*38c8a9a5SSteve French 	 * remount but we can not do much about it here
3019*38c8a9a5SSteve French 	 * if they have (even if we could detect it by the following)
3020*38c8a9a5SSteve French 	 * Perhaps we could add a backpointer to array of sb from tcon
3021*38c8a9a5SSteve French 	 * or if we change to make all sb to same share the same
3022*38c8a9a5SSteve French 	 * sb as NFS - then we only have one backpointer to sb.
3023*38c8a9a5SSteve French 	 * What if we wanted to mount the server share twice once with
3024*38c8a9a5SSteve French 	 * and once without posixacls or posix paths?
3025*38c8a9a5SSteve French 	 */
3026*38c8a9a5SSteve French 	__u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3027*38c8a9a5SSteve French 
3028*38c8a9a5SSteve French 	if (ctx && ctx->no_linux_ext) {
3029*38c8a9a5SSteve French 		tcon->fsUnixInfo.Capability = 0;
3030*38c8a9a5SSteve French 		tcon->unix_ext = 0; /* Unix Extensions disabled */
3031*38c8a9a5SSteve French 		cifs_dbg(FYI, "Linux protocol extensions disabled\n");
3032*38c8a9a5SSteve French 		return;
3033*38c8a9a5SSteve French 	} else if (ctx)
3034*38c8a9a5SSteve French 		tcon->unix_ext = 1; /* Unix Extensions supported */
3035*38c8a9a5SSteve French 
3036*38c8a9a5SSteve French 	if (!tcon->unix_ext) {
3037*38c8a9a5SSteve French 		cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
3038*38c8a9a5SSteve French 		return;
3039*38c8a9a5SSteve French 	}
3040*38c8a9a5SSteve French 
3041*38c8a9a5SSteve French 	if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
3042*38c8a9a5SSteve French 		__u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3043*38c8a9a5SSteve French 		cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
3044*38c8a9a5SSteve French 		/*
3045*38c8a9a5SSteve French 		 * check for reconnect case in which we do not
3046*38c8a9a5SSteve French 		 * want to change the mount behavior if we can avoid it
3047*38c8a9a5SSteve French 		 */
3048*38c8a9a5SSteve French 		if (ctx == NULL) {
3049*38c8a9a5SSteve French 			/*
3050*38c8a9a5SSteve French 			 * turn off POSIX ACL and PATHNAMES if not set
3051*38c8a9a5SSteve French 			 * originally at mount time
3052*38c8a9a5SSteve French 			 */
3053*38c8a9a5SSteve French 			if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3054*38c8a9a5SSteve French 				cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3055*38c8a9a5SSteve French 			if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3056*38c8a9a5SSteve French 				if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3057*38c8a9a5SSteve French 					cifs_dbg(VFS, "POSIXPATH support change\n");
3058*38c8a9a5SSteve French 				cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3059*38c8a9a5SSteve French 			} else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3060*38c8a9a5SSteve French 				cifs_dbg(VFS, "possible reconnect error\n");
3061*38c8a9a5SSteve French 				cifs_dbg(VFS, "server disabled POSIX path support\n");
3062*38c8a9a5SSteve French 			}
3063*38c8a9a5SSteve French 		}
3064*38c8a9a5SSteve French 
3065*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3066*38c8a9a5SSteve French 			cifs_dbg(VFS, "per-share encryption not supported yet\n");
3067*38c8a9a5SSteve French 
3068*38c8a9a5SSteve French 		cap &= CIFS_UNIX_CAP_MASK;
3069*38c8a9a5SSteve French 		if (ctx && ctx->no_psx_acl)
3070*38c8a9a5SSteve French 			cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3071*38c8a9a5SSteve French 		else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
3072*38c8a9a5SSteve French 			cifs_dbg(FYI, "negotiated posix acl support\n");
3073*38c8a9a5SSteve French 			if (cifs_sb)
3074*38c8a9a5SSteve French 				cifs_sb->mnt_cifs_flags |=
3075*38c8a9a5SSteve French 					CIFS_MOUNT_POSIXACL;
3076*38c8a9a5SSteve French 		}
3077*38c8a9a5SSteve French 
3078*38c8a9a5SSteve French 		if (ctx && ctx->posix_paths == 0)
3079*38c8a9a5SSteve French 			cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3080*38c8a9a5SSteve French 		else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3081*38c8a9a5SSteve French 			cifs_dbg(FYI, "negotiate posix pathnames\n");
3082*38c8a9a5SSteve French 			if (cifs_sb)
3083*38c8a9a5SSteve French 				cifs_sb->mnt_cifs_flags |=
3084*38c8a9a5SSteve French 					CIFS_MOUNT_POSIX_PATHS;
3085*38c8a9a5SSteve French 		}
3086*38c8a9a5SSteve French 
3087*38c8a9a5SSteve French 		cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
3088*38c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2
3089*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_FCNTL_CAP)
3090*38c8a9a5SSteve French 			cifs_dbg(FYI, "FCNTL cap\n");
3091*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_EXTATTR_CAP)
3092*38c8a9a5SSteve French 			cifs_dbg(FYI, "EXTATTR cap\n");
3093*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3094*38c8a9a5SSteve French 			cifs_dbg(FYI, "POSIX path cap\n");
3095*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_XATTR_CAP)
3096*38c8a9a5SSteve French 			cifs_dbg(FYI, "XATTR cap\n");
3097*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_POSIX_ACL_CAP)
3098*38c8a9a5SSteve French 			cifs_dbg(FYI, "POSIX ACL cap\n");
3099*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_LARGE_READ_CAP)
3100*38c8a9a5SSteve French 			cifs_dbg(FYI, "very large read cap\n");
3101*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3102*38c8a9a5SSteve French 			cifs_dbg(FYI, "very large write cap\n");
3103*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3104*38c8a9a5SSteve French 			cifs_dbg(FYI, "transport encryption cap\n");
3105*38c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3106*38c8a9a5SSteve French 			cifs_dbg(FYI, "mandatory transport encryption cap\n");
3107*38c8a9a5SSteve French #endif /* CIFS_DEBUG2 */
3108*38c8a9a5SSteve French 		if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3109*38c8a9a5SSteve French 			if (ctx == NULL)
3110*38c8a9a5SSteve French 				cifs_dbg(FYI, "resetting capabilities failed\n");
3111*38c8a9a5SSteve French 			else
3112*38c8a9a5SSteve 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");
3113*38c8a9a5SSteve French 
3114*38c8a9a5SSteve French 		}
3115*38c8a9a5SSteve French 	}
3116*38c8a9a5SSteve French }
3117*38c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3118*38c8a9a5SSteve French 
3119*38c8a9a5SSteve French int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
3120*38c8a9a5SSteve French {
3121*38c8a9a5SSteve French 	struct smb3_fs_context *ctx = cifs_sb->ctx;
3122*38c8a9a5SSteve French 
3123*38c8a9a5SSteve French 	INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3124*38c8a9a5SSteve French 
3125*38c8a9a5SSteve French 	spin_lock_init(&cifs_sb->tlink_tree_lock);
3126*38c8a9a5SSteve French 	cifs_sb->tlink_tree = RB_ROOT;
3127*38c8a9a5SSteve French 
3128*38c8a9a5SSteve French 	cifs_dbg(FYI, "file mode: %04ho  dir mode: %04ho\n",
3129*38c8a9a5SSteve French 		 ctx->file_mode, ctx->dir_mode);
3130*38c8a9a5SSteve French 
3131*38c8a9a5SSteve French 	/* this is needed for ASCII cp to Unicode converts */
3132*38c8a9a5SSteve French 	if (ctx->iocharset == NULL) {
3133*38c8a9a5SSteve French 		/* load_nls_default cannot return null */
3134*38c8a9a5SSteve French 		cifs_sb->local_nls = load_nls_default();
3135*38c8a9a5SSteve French 	} else {
3136*38c8a9a5SSteve French 		cifs_sb->local_nls = load_nls(ctx->iocharset);
3137*38c8a9a5SSteve French 		if (cifs_sb->local_nls == NULL) {
3138*38c8a9a5SSteve French 			cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
3139*38c8a9a5SSteve French 				 ctx->iocharset);
3140*38c8a9a5SSteve French 			return -ELIBACC;
3141*38c8a9a5SSteve French 		}
3142*38c8a9a5SSteve French 	}
3143*38c8a9a5SSteve French 	ctx->local_nls = cifs_sb->local_nls;
3144*38c8a9a5SSteve French 
3145*38c8a9a5SSteve French 	smb3_update_mnt_flags(cifs_sb);
3146*38c8a9a5SSteve French 
3147*38c8a9a5SSteve French 	if (ctx->direct_io)
3148*38c8a9a5SSteve French 		cifs_dbg(FYI, "mounting share using direct i/o\n");
3149*38c8a9a5SSteve French 	if (ctx->cache_ro) {
3150*38c8a9a5SSteve French 		cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
3151*38c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
3152*38c8a9a5SSteve French 	} else if (ctx->cache_rw) {
3153*38c8a9a5SSteve French 		cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
3154*38c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
3155*38c8a9a5SSteve French 					    CIFS_MOUNT_RW_CACHE);
3156*38c8a9a5SSteve French 	}
3157*38c8a9a5SSteve French 
3158*38c8a9a5SSteve French 	if ((ctx->cifs_acl) && (ctx->dynperm))
3159*38c8a9a5SSteve French 		cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3160*38c8a9a5SSteve French 
3161*38c8a9a5SSteve French 	if (ctx->prepath) {
3162*38c8a9a5SSteve French 		cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
3163*38c8a9a5SSteve French 		if (cifs_sb->prepath == NULL)
3164*38c8a9a5SSteve French 			return -ENOMEM;
3165*38c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3166*38c8a9a5SSteve French 	}
3167*38c8a9a5SSteve French 
3168*38c8a9a5SSteve French 	return 0;
3169*38c8a9a5SSteve French }
3170*38c8a9a5SSteve French 
3171*38c8a9a5SSteve French /* Release all succeed connections */
3172*38c8a9a5SSteve French void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx)
3173*38c8a9a5SSteve French {
3174*38c8a9a5SSteve French 	int rc = 0;
3175*38c8a9a5SSteve French 
3176*38c8a9a5SSteve French 	if (mnt_ctx->tcon)
3177*38c8a9a5SSteve French 		cifs_put_tcon(mnt_ctx->tcon);
3178*38c8a9a5SSteve French 	else if (mnt_ctx->ses)
3179*38c8a9a5SSteve French 		cifs_put_smb_ses(mnt_ctx->ses);
3180*38c8a9a5SSteve French 	else if (mnt_ctx->server)
3181*38c8a9a5SSteve French 		cifs_put_tcp_session(mnt_ctx->server, 0);
3182*38c8a9a5SSteve French 	mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3183*38c8a9a5SSteve French 	free_xid(mnt_ctx->xid);
3184*38c8a9a5SSteve French }
3185*38c8a9a5SSteve French 
3186*38c8a9a5SSteve French int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx)
3187*38c8a9a5SSteve French {
3188*38c8a9a5SSteve French 	struct TCP_Server_Info *server = NULL;
3189*38c8a9a5SSteve French 	struct smb3_fs_context *ctx;
3190*38c8a9a5SSteve French 	struct cifs_ses *ses = NULL;
3191*38c8a9a5SSteve French 	unsigned int xid;
3192*38c8a9a5SSteve French 	int rc = 0;
3193*38c8a9a5SSteve French 
3194*38c8a9a5SSteve French 	xid = get_xid();
3195*38c8a9a5SSteve French 
3196*38c8a9a5SSteve French 	if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) {
3197*38c8a9a5SSteve French 		rc = -EINVAL;
3198*38c8a9a5SSteve French 		goto out;
3199*38c8a9a5SSteve French 	}
3200*38c8a9a5SSteve French 	ctx = mnt_ctx->fs_ctx;
3201*38c8a9a5SSteve French 
3202*38c8a9a5SSteve French 	/* get a reference to a tcp session */
3203*38c8a9a5SSteve French 	server = cifs_get_tcp_session(ctx, NULL);
3204*38c8a9a5SSteve French 	if (IS_ERR(server)) {
3205*38c8a9a5SSteve French 		rc = PTR_ERR(server);
3206*38c8a9a5SSteve French 		server = NULL;
3207*38c8a9a5SSteve French 		goto out;
3208*38c8a9a5SSteve French 	}
3209*38c8a9a5SSteve French 
3210*38c8a9a5SSteve French 	/* get a reference to a SMB session */
3211*38c8a9a5SSteve French 	ses = cifs_get_smb_ses(server, ctx);
3212*38c8a9a5SSteve French 	if (IS_ERR(ses)) {
3213*38c8a9a5SSteve French 		rc = PTR_ERR(ses);
3214*38c8a9a5SSteve French 		ses = NULL;
3215*38c8a9a5SSteve French 		goto out;
3216*38c8a9a5SSteve French 	}
3217*38c8a9a5SSteve French 
3218*38c8a9a5SSteve French 	if ((ctx->persistent == true) && (!(ses->server->capabilities &
3219*38c8a9a5SSteve French 					    SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
3220*38c8a9a5SSteve French 		cifs_server_dbg(VFS, "persistent handles not supported by server\n");
3221*38c8a9a5SSteve French 		rc = -EOPNOTSUPP;
3222*38c8a9a5SSteve French 	}
3223*38c8a9a5SSteve French 
3224*38c8a9a5SSteve French out:
3225*38c8a9a5SSteve French 	mnt_ctx->xid = xid;
3226*38c8a9a5SSteve French 	mnt_ctx->server = server;
3227*38c8a9a5SSteve French 	mnt_ctx->ses = ses;
3228*38c8a9a5SSteve French 	mnt_ctx->tcon = NULL;
3229*38c8a9a5SSteve French 
3230*38c8a9a5SSteve French 	return rc;
3231*38c8a9a5SSteve French }
3232*38c8a9a5SSteve French 
3233*38c8a9a5SSteve French int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx)
3234*38c8a9a5SSteve French {
3235*38c8a9a5SSteve French 	struct TCP_Server_Info *server;
3236*38c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
3237*38c8a9a5SSteve French 	struct smb3_fs_context *ctx;
3238*38c8a9a5SSteve French 	struct cifs_tcon *tcon = NULL;
3239*38c8a9a5SSteve French 	int rc = 0;
3240*38c8a9a5SSteve French 
3241*38c8a9a5SSteve French 	if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->server || !mnt_ctx->ses || !mnt_ctx->fs_ctx ||
3242*38c8a9a5SSteve French 			 !mnt_ctx->cifs_sb)) {
3243*38c8a9a5SSteve French 		rc = -EINVAL;
3244*38c8a9a5SSteve French 		goto out;
3245*38c8a9a5SSteve French 	}
3246*38c8a9a5SSteve French 	server = mnt_ctx->server;
3247*38c8a9a5SSteve French 	ctx = mnt_ctx->fs_ctx;
3248*38c8a9a5SSteve French 	cifs_sb = mnt_ctx->cifs_sb;
3249*38c8a9a5SSteve French 
3250*38c8a9a5SSteve French 	/* search for existing tcon to this server share */
3251*38c8a9a5SSteve French 	tcon = cifs_get_tcon(mnt_ctx->ses, ctx);
3252*38c8a9a5SSteve French 	if (IS_ERR(tcon)) {
3253*38c8a9a5SSteve French 		rc = PTR_ERR(tcon);
3254*38c8a9a5SSteve French 		tcon = NULL;
3255*38c8a9a5SSteve French 		goto out;
3256*38c8a9a5SSteve French 	}
3257*38c8a9a5SSteve French 
3258*38c8a9a5SSteve French 	/* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3259*38c8a9a5SSteve French 	if (tcon->posix_extensions)
3260*38c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3261*38c8a9a5SSteve French 
3262*38c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3263*38c8a9a5SSteve French 	/* tell server which Unix caps we support */
3264*38c8a9a5SSteve French 	if (cap_unix(tcon->ses)) {
3265*38c8a9a5SSteve French 		/*
3266*38c8a9a5SSteve French 		 * reset of caps checks mount to see if unix extensions disabled
3267*38c8a9a5SSteve French 		 * for just this mount.
3268*38c8a9a5SSteve French 		 */
3269*38c8a9a5SSteve French 		reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx);
3270*38c8a9a5SSteve French 		spin_lock(&tcon->ses->server->srv_lock);
3271*38c8a9a5SSteve French 		if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3272*38c8a9a5SSteve French 		    (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3273*38c8a9a5SSteve French 		     CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3274*38c8a9a5SSteve French 			spin_unlock(&tcon->ses->server->srv_lock);
3275*38c8a9a5SSteve French 			rc = -EACCES;
3276*38c8a9a5SSteve French 			goto out;
3277*38c8a9a5SSteve French 		}
3278*38c8a9a5SSteve French 		spin_unlock(&tcon->ses->server->srv_lock);
3279*38c8a9a5SSteve French 	} else
3280*38c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3281*38c8a9a5SSteve French 		tcon->unix_ext = 0; /* server does not support them */
3282*38c8a9a5SSteve French 
3283*38c8a9a5SSteve French 	/* do not care if a following call succeed - informational */
3284*38c8a9a5SSteve French 	if (!tcon->pipe && server->ops->qfs_tcon) {
3285*38c8a9a5SSteve French 		server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb);
3286*38c8a9a5SSteve French 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
3287*38c8a9a5SSteve French 			if (tcon->fsDevInfo.DeviceCharacteristics &
3288*38c8a9a5SSteve French 			    cpu_to_le32(FILE_READ_ONLY_DEVICE))
3289*38c8a9a5SSteve French 				cifs_dbg(VFS, "mounted to read only share\n");
3290*38c8a9a5SSteve French 			else if ((cifs_sb->mnt_cifs_flags &
3291*38c8a9a5SSteve French 				  CIFS_MOUNT_RW_CACHE) == 0)
3292*38c8a9a5SSteve French 				cifs_dbg(VFS, "read only mount of RW share\n");
3293*38c8a9a5SSteve French 			/* no need to log a RW mount of a typical RW share */
3294*38c8a9a5SSteve French 		}
3295*38c8a9a5SSteve French 	}
3296*38c8a9a5SSteve French 
3297*38c8a9a5SSteve French 	/*
3298*38c8a9a5SSteve French 	 * Clamp the rsize/wsize mount arguments if they are too big for the server
3299*38c8a9a5SSteve French 	 * and set the rsize/wsize to the negotiated values if not passed in by
3300*38c8a9a5SSteve French 	 * the user on mount
3301*38c8a9a5SSteve French 	 */
3302*38c8a9a5SSteve French 	if ((cifs_sb->ctx->wsize == 0) ||
3303*38c8a9a5SSteve French 	    (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
3304*38c8a9a5SSteve French 		cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
3305*38c8a9a5SSteve French 	if ((cifs_sb->ctx->rsize == 0) ||
3306*38c8a9a5SSteve French 	    (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
3307*38c8a9a5SSteve French 		cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
3308*38c8a9a5SSteve French 
3309*38c8a9a5SSteve French 	/*
3310*38c8a9a5SSteve French 	 * The cookie is initialized from volume info returned above.
3311*38c8a9a5SSteve French 	 * Inside cifs_fscache_get_super_cookie it checks
3312*38c8a9a5SSteve French 	 * that we do not get super cookie twice.
3313*38c8a9a5SSteve French 	 */
3314*38c8a9a5SSteve French 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
3315*38c8a9a5SSteve French 		cifs_fscache_get_super_cookie(tcon);
3316*38c8a9a5SSteve French 
3317*38c8a9a5SSteve French out:
3318*38c8a9a5SSteve French 	mnt_ctx->tcon = tcon;
3319*38c8a9a5SSteve French 	return rc;
3320*38c8a9a5SSteve French }
3321*38c8a9a5SSteve French 
3322*38c8a9a5SSteve French static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3323*38c8a9a5SSteve French 			     struct cifs_tcon *tcon)
3324*38c8a9a5SSteve French {
3325*38c8a9a5SSteve French 	struct tcon_link *tlink;
3326*38c8a9a5SSteve French 
3327*38c8a9a5SSteve French 	/* hang the tcon off of the superblock */
3328*38c8a9a5SSteve French 	tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3329*38c8a9a5SSteve French 	if (tlink == NULL)
3330*38c8a9a5SSteve French 		return -ENOMEM;
3331*38c8a9a5SSteve French 
3332*38c8a9a5SSteve French 	tlink->tl_uid = ses->linux_uid;
3333*38c8a9a5SSteve French 	tlink->tl_tcon = tcon;
3334*38c8a9a5SSteve French 	tlink->tl_time = jiffies;
3335*38c8a9a5SSteve French 	set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3336*38c8a9a5SSteve French 	set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3337*38c8a9a5SSteve French 
3338*38c8a9a5SSteve French 	cifs_sb->master_tlink = tlink;
3339*38c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
3340*38c8a9a5SSteve French 	tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3341*38c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
3342*38c8a9a5SSteve French 
3343*38c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3344*38c8a9a5SSteve French 				TLINK_IDLE_EXPIRE);
3345*38c8a9a5SSteve French 	return 0;
3346*38c8a9a5SSteve French }
3347*38c8a9a5SSteve French 
3348*38c8a9a5SSteve French static int
3349*38c8a9a5SSteve French cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3350*38c8a9a5SSteve French 					unsigned int xid,
3351*38c8a9a5SSteve French 					struct cifs_tcon *tcon,
3352*38c8a9a5SSteve French 					struct cifs_sb_info *cifs_sb,
3353*38c8a9a5SSteve French 					char *full_path,
3354*38c8a9a5SSteve French 					int added_treename)
3355*38c8a9a5SSteve French {
3356*38c8a9a5SSteve French 	int rc;
3357*38c8a9a5SSteve French 	char *s;
3358*38c8a9a5SSteve French 	char sep, tmp;
3359*38c8a9a5SSteve French 	int skip = added_treename ? 1 : 0;
3360*38c8a9a5SSteve French 
3361*38c8a9a5SSteve French 	sep = CIFS_DIR_SEP(cifs_sb);
3362*38c8a9a5SSteve French 	s = full_path;
3363*38c8a9a5SSteve French 
3364*38c8a9a5SSteve French 	rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3365*38c8a9a5SSteve French 	while (rc == 0) {
3366*38c8a9a5SSteve French 		/* skip separators */
3367*38c8a9a5SSteve French 		while (*s == sep)
3368*38c8a9a5SSteve French 			s++;
3369*38c8a9a5SSteve French 		if (!*s)
3370*38c8a9a5SSteve French 			break;
3371*38c8a9a5SSteve French 		/* next separator */
3372*38c8a9a5SSteve French 		while (*s && *s != sep)
3373*38c8a9a5SSteve French 			s++;
3374*38c8a9a5SSteve French 		/*
3375*38c8a9a5SSteve French 		 * if the treename is added, we then have to skip the first
3376*38c8a9a5SSteve French 		 * part within the separators
3377*38c8a9a5SSteve French 		 */
3378*38c8a9a5SSteve French 		if (skip) {
3379*38c8a9a5SSteve French 			skip = 0;
3380*38c8a9a5SSteve French 			continue;
3381*38c8a9a5SSteve French 		}
3382*38c8a9a5SSteve French 		/*
3383*38c8a9a5SSteve French 		 * temporarily null-terminate the path at the end of
3384*38c8a9a5SSteve French 		 * the current component
3385*38c8a9a5SSteve French 		 */
3386*38c8a9a5SSteve French 		tmp = *s;
3387*38c8a9a5SSteve French 		*s = 0;
3388*38c8a9a5SSteve French 		rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3389*38c8a9a5SSteve French 						     full_path);
3390*38c8a9a5SSteve French 		*s = tmp;
3391*38c8a9a5SSteve French 	}
3392*38c8a9a5SSteve French 	return rc;
3393*38c8a9a5SSteve French }
3394*38c8a9a5SSteve French 
3395*38c8a9a5SSteve French /*
3396*38c8a9a5SSteve French  * Check if path is remote (i.e. a DFS share).
3397*38c8a9a5SSteve French  *
3398*38c8a9a5SSteve French  * Return -EREMOTE if it is, otherwise 0 or -errno.
3399*38c8a9a5SSteve French  */
3400*38c8a9a5SSteve French int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx)
3401*38c8a9a5SSteve French {
3402*38c8a9a5SSteve French 	int rc;
3403*38c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3404*38c8a9a5SSteve French 	struct TCP_Server_Info *server = mnt_ctx->server;
3405*38c8a9a5SSteve French 	unsigned int xid = mnt_ctx->xid;
3406*38c8a9a5SSteve French 	struct cifs_tcon *tcon = mnt_ctx->tcon;
3407*38c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3408*38c8a9a5SSteve French 	char *full_path;
3409*38c8a9a5SSteve French 
3410*38c8a9a5SSteve French 	if (!server->ops->is_path_accessible)
3411*38c8a9a5SSteve French 		return -EOPNOTSUPP;
3412*38c8a9a5SSteve French 
3413*38c8a9a5SSteve French 	/*
3414*38c8a9a5SSteve French 	 * cifs_build_path_to_root works only when we have a valid tcon
3415*38c8a9a5SSteve French 	 */
3416*38c8a9a5SSteve French 	full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
3417*38c8a9a5SSteve French 					    tcon->Flags & SMB_SHARE_IS_IN_DFS);
3418*38c8a9a5SSteve French 	if (full_path == NULL)
3419*38c8a9a5SSteve French 		return -ENOMEM;
3420*38c8a9a5SSteve French 
3421*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3422*38c8a9a5SSteve French 
3423*38c8a9a5SSteve French 	rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3424*38c8a9a5SSteve French 					     full_path);
3425*38c8a9a5SSteve French 	if (rc != 0 && rc != -EREMOTE)
3426*38c8a9a5SSteve French 		goto out;
3427*38c8a9a5SSteve French 
3428*38c8a9a5SSteve French 	if (rc != -EREMOTE) {
3429*38c8a9a5SSteve French 		rc = cifs_are_all_path_components_accessible(server, xid, tcon,
3430*38c8a9a5SSteve French 			cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
3431*38c8a9a5SSteve French 		if (rc != 0) {
3432*38c8a9a5SSteve French 			cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3433*38c8a9a5SSteve French 			cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3434*38c8a9a5SSteve French 			rc = 0;
3435*38c8a9a5SSteve French 		}
3436*38c8a9a5SSteve French 	}
3437*38c8a9a5SSteve French 
3438*38c8a9a5SSteve French out:
3439*38c8a9a5SSteve French 	kfree(full_path);
3440*38c8a9a5SSteve French 	return rc;
3441*38c8a9a5SSteve French }
3442*38c8a9a5SSteve French 
3443*38c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
3444*38c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3445*38c8a9a5SSteve French {
3446*38c8a9a5SSteve French 	struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3447*38c8a9a5SSteve French 	bool isdfs;
3448*38c8a9a5SSteve French 	int rc;
3449*38c8a9a5SSteve French 
3450*38c8a9a5SSteve French 	INIT_LIST_HEAD(&mnt_ctx.dfs_ses_list);
3451*38c8a9a5SSteve French 
3452*38c8a9a5SSteve French 	rc = dfs_mount_share(&mnt_ctx, &isdfs);
3453*38c8a9a5SSteve French 	if (rc)
3454*38c8a9a5SSteve French 		goto error;
3455*38c8a9a5SSteve French 	if (!isdfs)
3456*38c8a9a5SSteve French 		goto out;
3457*38c8a9a5SSteve French 
3458*38c8a9a5SSteve French 	/*
3459*38c8a9a5SSteve French 	 * After reconnecting to a different server, unique ids won't match anymore, so we disable
3460*38c8a9a5SSteve French 	 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
3461*38c8a9a5SSteve French 	 */
3462*38c8a9a5SSteve French 	cifs_autodisable_serverino(cifs_sb);
3463*38c8a9a5SSteve French 	/*
3464*38c8a9a5SSteve French 	 * Force the use of prefix path to support failover on DFS paths that resolve to targets
3465*38c8a9a5SSteve French 	 * that have different prefix paths.
3466*38c8a9a5SSteve French 	 */
3467*38c8a9a5SSteve French 	cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3468*38c8a9a5SSteve French 	kfree(cifs_sb->prepath);
3469*38c8a9a5SSteve French 	cifs_sb->prepath = ctx->prepath;
3470*38c8a9a5SSteve French 	ctx->prepath = NULL;
3471*38c8a9a5SSteve French 
3472*38c8a9a5SSteve French out:
3473*38c8a9a5SSteve French 	cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
3474*38c8a9a5SSteve French 	rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
3475*38c8a9a5SSteve French 	if (rc)
3476*38c8a9a5SSteve French 		goto error;
3477*38c8a9a5SSteve French 
3478*38c8a9a5SSteve French 	free_xid(mnt_ctx.xid);
3479*38c8a9a5SSteve French 	return rc;
3480*38c8a9a5SSteve French 
3481*38c8a9a5SSteve French error:
3482*38c8a9a5SSteve French 	dfs_put_root_smb_sessions(&mnt_ctx.dfs_ses_list);
3483*38c8a9a5SSteve French 	cifs_mount_put_conns(&mnt_ctx);
3484*38c8a9a5SSteve French 	return rc;
3485*38c8a9a5SSteve French }
3486*38c8a9a5SSteve French #else
3487*38c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3488*38c8a9a5SSteve French {
3489*38c8a9a5SSteve French 	int rc = 0;
3490*38c8a9a5SSteve French 	struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3491*38c8a9a5SSteve French 
3492*38c8a9a5SSteve French 	rc = cifs_mount_get_session(&mnt_ctx);
3493*38c8a9a5SSteve French 	if (rc)
3494*38c8a9a5SSteve French 		goto error;
3495*38c8a9a5SSteve French 
3496*38c8a9a5SSteve French 	rc = cifs_mount_get_tcon(&mnt_ctx);
3497*38c8a9a5SSteve French 	if (rc)
3498*38c8a9a5SSteve French 		goto error;
3499*38c8a9a5SSteve French 
3500*38c8a9a5SSteve French 	rc = cifs_is_path_remote(&mnt_ctx);
3501*38c8a9a5SSteve French 	if (rc == -EREMOTE)
3502*38c8a9a5SSteve French 		rc = -EOPNOTSUPP;
3503*38c8a9a5SSteve French 	if (rc)
3504*38c8a9a5SSteve French 		goto error;
3505*38c8a9a5SSteve French 
3506*38c8a9a5SSteve French 	rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
3507*38c8a9a5SSteve French 	if (rc)
3508*38c8a9a5SSteve French 		goto error;
3509*38c8a9a5SSteve French 
3510*38c8a9a5SSteve French 	free_xid(mnt_ctx.xid);
3511*38c8a9a5SSteve French 	return rc;
3512*38c8a9a5SSteve French 
3513*38c8a9a5SSteve French error:
3514*38c8a9a5SSteve French 	cifs_mount_put_conns(&mnt_ctx);
3515*38c8a9a5SSteve French 	return rc;
3516*38c8a9a5SSteve French }
3517*38c8a9a5SSteve French #endif
3518*38c8a9a5SSteve French 
3519*38c8a9a5SSteve French /*
3520*38c8a9a5SSteve French  * Issue a TREE_CONNECT request.
3521*38c8a9a5SSteve French  */
3522*38c8a9a5SSteve French int
3523*38c8a9a5SSteve French CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
3524*38c8a9a5SSteve French 	 const char *tree, struct cifs_tcon *tcon,
3525*38c8a9a5SSteve French 	 const struct nls_table *nls_codepage)
3526*38c8a9a5SSteve French {
3527*38c8a9a5SSteve French 	struct smb_hdr *smb_buffer;
3528*38c8a9a5SSteve French 	struct smb_hdr *smb_buffer_response;
3529*38c8a9a5SSteve French 	TCONX_REQ *pSMB;
3530*38c8a9a5SSteve French 	TCONX_RSP *pSMBr;
3531*38c8a9a5SSteve French 	unsigned char *bcc_ptr;
3532*38c8a9a5SSteve French 	int rc = 0;
3533*38c8a9a5SSteve French 	int length;
3534*38c8a9a5SSteve French 	__u16 bytes_left, count;
3535*38c8a9a5SSteve French 
3536*38c8a9a5SSteve French 	if (ses == NULL)
3537*38c8a9a5SSteve French 		return -EIO;
3538*38c8a9a5SSteve French 
3539*38c8a9a5SSteve French 	smb_buffer = cifs_buf_get();
3540*38c8a9a5SSteve French 	if (smb_buffer == NULL)
3541*38c8a9a5SSteve French 		return -ENOMEM;
3542*38c8a9a5SSteve French 
3543*38c8a9a5SSteve French 	smb_buffer_response = smb_buffer;
3544*38c8a9a5SSteve French 
3545*38c8a9a5SSteve French 	header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3546*38c8a9a5SSteve French 			NULL /*no tid */ , 4 /*wct */ );
3547*38c8a9a5SSteve French 
3548*38c8a9a5SSteve French 	smb_buffer->Mid = get_next_mid(ses->server);
3549*38c8a9a5SSteve French 	smb_buffer->Uid = ses->Suid;
3550*38c8a9a5SSteve French 	pSMB = (TCONX_REQ *) smb_buffer;
3551*38c8a9a5SSteve French 	pSMBr = (TCONX_RSP *) smb_buffer_response;
3552*38c8a9a5SSteve French 
3553*38c8a9a5SSteve French 	pSMB->AndXCommand = 0xFF;
3554*38c8a9a5SSteve French 	pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
3555*38c8a9a5SSteve French 	bcc_ptr = &pSMB->Password[0];
3556*38c8a9a5SSteve French 
3557*38c8a9a5SSteve French 	pSMB->PasswordLength = cpu_to_le16(1);	/* minimum */
3558*38c8a9a5SSteve French 	*bcc_ptr = 0; /* password is null byte */
3559*38c8a9a5SSteve French 	bcc_ptr++;              /* skip password */
3560*38c8a9a5SSteve French 	/* already aligned so no need to do it below */
3561*38c8a9a5SSteve French 
3562*38c8a9a5SSteve French 	if (ses->server->sign)
3563*38c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3564*38c8a9a5SSteve French 
3565*38c8a9a5SSteve French 	if (ses->capabilities & CAP_STATUS32) {
3566*38c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3567*38c8a9a5SSteve French 	}
3568*38c8a9a5SSteve French 	if (ses->capabilities & CAP_DFS) {
3569*38c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_DFS;
3570*38c8a9a5SSteve French 	}
3571*38c8a9a5SSteve French 	if (ses->capabilities & CAP_UNICODE) {
3572*38c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3573*38c8a9a5SSteve French 		length =
3574*38c8a9a5SSteve French 		    cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
3575*38c8a9a5SSteve French 			6 /* max utf8 char length in bytes */ *
3576*38c8a9a5SSteve French 			(/* server len*/ + 256 /* share len */), nls_codepage);
3577*38c8a9a5SSteve French 		bcc_ptr += 2 * length;	/* convert num 16 bit words to bytes */
3578*38c8a9a5SSteve French 		bcc_ptr += 2;	/* skip trailing null */
3579*38c8a9a5SSteve French 	} else {		/* ASCII */
3580*38c8a9a5SSteve French 		strcpy(bcc_ptr, tree);
3581*38c8a9a5SSteve French 		bcc_ptr += strlen(tree) + 1;
3582*38c8a9a5SSteve French 	}
3583*38c8a9a5SSteve French 	strcpy(bcc_ptr, "?????");
3584*38c8a9a5SSteve French 	bcc_ptr += strlen("?????");
3585*38c8a9a5SSteve French 	bcc_ptr += 1;
3586*38c8a9a5SSteve French 	count = bcc_ptr - &pSMB->Password[0];
3587*38c8a9a5SSteve French 	be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
3588*38c8a9a5SSteve French 	pSMB->ByteCount = cpu_to_le16(count);
3589*38c8a9a5SSteve French 
3590*38c8a9a5SSteve French 	rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3591*38c8a9a5SSteve French 			 0);
3592*38c8a9a5SSteve French 
3593*38c8a9a5SSteve French 	/* above now done in SendReceive */
3594*38c8a9a5SSteve French 	if (rc == 0) {
3595*38c8a9a5SSteve French 		bool is_unicode;
3596*38c8a9a5SSteve French 
3597*38c8a9a5SSteve French 		tcon->tid = smb_buffer_response->Tid;
3598*38c8a9a5SSteve French 		bcc_ptr = pByteArea(smb_buffer_response);
3599*38c8a9a5SSteve French 		bytes_left = get_bcc(smb_buffer_response);
3600*38c8a9a5SSteve French 		length = strnlen(bcc_ptr, bytes_left - 2);
3601*38c8a9a5SSteve French 		if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3602*38c8a9a5SSteve French 			is_unicode = true;
3603*38c8a9a5SSteve French 		else
3604*38c8a9a5SSteve French 			is_unicode = false;
3605*38c8a9a5SSteve French 
3606*38c8a9a5SSteve French 
3607*38c8a9a5SSteve French 		/* skip service field (NB: this field is always ASCII) */
3608*38c8a9a5SSteve French 		if (length == 3) {
3609*38c8a9a5SSteve French 			if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3610*38c8a9a5SSteve French 			    (bcc_ptr[2] == 'C')) {
3611*38c8a9a5SSteve French 				cifs_dbg(FYI, "IPC connection\n");
3612*38c8a9a5SSteve French 				tcon->ipc = true;
3613*38c8a9a5SSteve French 				tcon->pipe = true;
3614*38c8a9a5SSteve French 			}
3615*38c8a9a5SSteve French 		} else if (length == 2) {
3616*38c8a9a5SSteve French 			if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3617*38c8a9a5SSteve French 				/* the most common case */
3618*38c8a9a5SSteve French 				cifs_dbg(FYI, "disk share connection\n");
3619*38c8a9a5SSteve French 			}
3620*38c8a9a5SSteve French 		}
3621*38c8a9a5SSteve French 		bcc_ptr += length + 1;
3622*38c8a9a5SSteve French 		bytes_left -= (length + 1);
3623*38c8a9a5SSteve French 		strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
3624*38c8a9a5SSteve French 
3625*38c8a9a5SSteve French 		/* mostly informational -- no need to fail on error here */
3626*38c8a9a5SSteve French 		kfree(tcon->nativeFileSystem);
3627*38c8a9a5SSteve French 		tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
3628*38c8a9a5SSteve French 						      bytes_left, is_unicode,
3629*38c8a9a5SSteve French 						      nls_codepage);
3630*38c8a9a5SSteve French 
3631*38c8a9a5SSteve French 		cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
3632*38c8a9a5SSteve French 
3633*38c8a9a5SSteve French 		if ((smb_buffer_response->WordCount == 3) ||
3634*38c8a9a5SSteve French 			 (smb_buffer_response->WordCount == 7))
3635*38c8a9a5SSteve French 			/* field is in same location */
3636*38c8a9a5SSteve French 			tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3637*38c8a9a5SSteve French 		else
3638*38c8a9a5SSteve French 			tcon->Flags = 0;
3639*38c8a9a5SSteve French 		cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
3640*38c8a9a5SSteve French 	}
3641*38c8a9a5SSteve French 
3642*38c8a9a5SSteve French 	cifs_buf_release(smb_buffer);
3643*38c8a9a5SSteve French 	return rc;
3644*38c8a9a5SSteve French }
3645*38c8a9a5SSteve French 
3646*38c8a9a5SSteve French static void delayed_free(struct rcu_head *p)
3647*38c8a9a5SSteve French {
3648*38c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3649*38c8a9a5SSteve French 
3650*38c8a9a5SSteve French 	unload_nls(cifs_sb->local_nls);
3651*38c8a9a5SSteve French 	smb3_cleanup_fs_context(cifs_sb->ctx);
3652*38c8a9a5SSteve French 	kfree(cifs_sb);
3653*38c8a9a5SSteve French }
3654*38c8a9a5SSteve French 
3655*38c8a9a5SSteve French void
3656*38c8a9a5SSteve French cifs_umount(struct cifs_sb_info *cifs_sb)
3657*38c8a9a5SSteve French {
3658*38c8a9a5SSteve French 	struct rb_root *root = &cifs_sb->tlink_tree;
3659*38c8a9a5SSteve French 	struct rb_node *node;
3660*38c8a9a5SSteve French 	struct tcon_link *tlink;
3661*38c8a9a5SSteve French 
3662*38c8a9a5SSteve French 	cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3663*38c8a9a5SSteve French 
3664*38c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
3665*38c8a9a5SSteve French 	while ((node = rb_first(root))) {
3666*38c8a9a5SSteve French 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3667*38c8a9a5SSteve French 		cifs_get_tlink(tlink);
3668*38c8a9a5SSteve French 		clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3669*38c8a9a5SSteve French 		rb_erase(node, root);
3670*38c8a9a5SSteve French 
3671*38c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
3672*38c8a9a5SSteve French 		cifs_put_tlink(tlink);
3673*38c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
3674*38c8a9a5SSteve French 	}
3675*38c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
3676*38c8a9a5SSteve French 
3677*38c8a9a5SSteve French 	kfree(cifs_sb->prepath);
3678*38c8a9a5SSteve French 	call_rcu(&cifs_sb->rcu, delayed_free);
3679*38c8a9a5SSteve French }
3680*38c8a9a5SSteve French 
3681*38c8a9a5SSteve French int
3682*38c8a9a5SSteve French cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
3683*38c8a9a5SSteve French 			struct TCP_Server_Info *server)
3684*38c8a9a5SSteve French {
3685*38c8a9a5SSteve French 	int rc = 0;
3686*38c8a9a5SSteve French 
3687*38c8a9a5SSteve French 	if (!server->ops->need_neg || !server->ops->negotiate)
3688*38c8a9a5SSteve French 		return -ENOSYS;
3689*38c8a9a5SSteve French 
3690*38c8a9a5SSteve French 	/* only send once per connect */
3691*38c8a9a5SSteve French 	spin_lock(&server->srv_lock);
3692*38c8a9a5SSteve French 	if (server->tcpStatus != CifsGood &&
3693*38c8a9a5SSteve French 	    server->tcpStatus != CifsNew &&
3694*38c8a9a5SSteve French 	    server->tcpStatus != CifsNeedNegotiate) {
3695*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
3696*38c8a9a5SSteve French 		return -EHOSTDOWN;
3697*38c8a9a5SSteve French 	}
3698*38c8a9a5SSteve French 
3699*38c8a9a5SSteve French 	if (!server->ops->need_neg(server) &&
3700*38c8a9a5SSteve French 	    server->tcpStatus == CifsGood) {
3701*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
3702*38c8a9a5SSteve French 		return 0;
3703*38c8a9a5SSteve French 	}
3704*38c8a9a5SSteve French 
3705*38c8a9a5SSteve French 	server->tcpStatus = CifsInNegotiate;
3706*38c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
3707*38c8a9a5SSteve French 
3708*38c8a9a5SSteve French 	rc = server->ops->negotiate(xid, ses, server);
3709*38c8a9a5SSteve French 	if (rc == 0) {
3710*38c8a9a5SSteve French 		spin_lock(&server->srv_lock);
3711*38c8a9a5SSteve French 		if (server->tcpStatus == CifsInNegotiate)
3712*38c8a9a5SSteve French 			server->tcpStatus = CifsGood;
3713*38c8a9a5SSteve French 		else
3714*38c8a9a5SSteve French 			rc = -EHOSTDOWN;
3715*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
3716*38c8a9a5SSteve French 	} else {
3717*38c8a9a5SSteve French 		spin_lock(&server->srv_lock);
3718*38c8a9a5SSteve French 		if (server->tcpStatus == CifsInNegotiate)
3719*38c8a9a5SSteve French 			server->tcpStatus = CifsNeedNegotiate;
3720*38c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
3721*38c8a9a5SSteve French 	}
3722*38c8a9a5SSteve French 
3723*38c8a9a5SSteve French 	return rc;
3724*38c8a9a5SSteve French }
3725*38c8a9a5SSteve French 
3726*38c8a9a5SSteve French int
3727*38c8a9a5SSteve French cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3728*38c8a9a5SSteve French 		   struct TCP_Server_Info *server,
3729*38c8a9a5SSteve French 		   struct nls_table *nls_info)
3730*38c8a9a5SSteve French {
3731*38c8a9a5SSteve French 	int rc = -ENOSYS;
3732*38c8a9a5SSteve French 	struct TCP_Server_Info *pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
3733*38c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr;
3734*38c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr;
3735*38c8a9a5SSteve French 	bool is_binding = false;
3736*38c8a9a5SSteve French 
3737*38c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
3738*38c8a9a5SSteve French 	cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
3739*38c8a9a5SSteve French 		 __func__, ses->chans_need_reconnect);
3740*38c8a9a5SSteve French 
3741*38c8a9a5SSteve French 	if (ses->ses_status != SES_GOOD &&
3742*38c8a9a5SSteve French 	    ses->ses_status != SES_NEW &&
3743*38c8a9a5SSteve French 	    ses->ses_status != SES_NEED_RECON) {
3744*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
3745*38c8a9a5SSteve French 		return -EHOSTDOWN;
3746*38c8a9a5SSteve French 	}
3747*38c8a9a5SSteve French 
3748*38c8a9a5SSteve French 	/* only send once per connect */
3749*38c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
3750*38c8a9a5SSteve French 	if (CIFS_ALL_CHANS_GOOD(ses)) {
3751*38c8a9a5SSteve French 		if (ses->ses_status == SES_NEED_RECON)
3752*38c8a9a5SSteve French 			ses->ses_status = SES_GOOD;
3753*38c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
3754*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
3755*38c8a9a5SSteve French 		return 0;
3756*38c8a9a5SSteve French 	}
3757*38c8a9a5SSteve French 
3758*38c8a9a5SSteve French 	cifs_chan_set_in_reconnect(ses, server);
3759*38c8a9a5SSteve French 	is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
3760*38c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
3761*38c8a9a5SSteve French 
3762*38c8a9a5SSteve French 	if (!is_binding)
3763*38c8a9a5SSteve French 		ses->ses_status = SES_IN_SETUP;
3764*38c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
3765*38c8a9a5SSteve French 
3766*38c8a9a5SSteve French 	/* update ses ip_addr only for primary chan */
3767*38c8a9a5SSteve French 	if (server == pserver) {
3768*38c8a9a5SSteve French 		if (server->dstaddr.ss_family == AF_INET6)
3769*38c8a9a5SSteve French 			scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr);
3770*38c8a9a5SSteve French 		else
3771*38c8a9a5SSteve French 			scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr);
3772*38c8a9a5SSteve French 	}
3773*38c8a9a5SSteve French 
3774*38c8a9a5SSteve French 	if (!is_binding) {
3775*38c8a9a5SSteve French 		ses->capabilities = server->capabilities;
3776*38c8a9a5SSteve French 		if (!linuxExtEnabled)
3777*38c8a9a5SSteve French 			ses->capabilities &= (~server->vals->cap_unix);
3778*38c8a9a5SSteve French 
3779*38c8a9a5SSteve French 		if (ses->auth_key.response) {
3780*38c8a9a5SSteve French 			cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
3781*38c8a9a5SSteve French 				 ses->auth_key.response);
3782*38c8a9a5SSteve French 			kfree_sensitive(ses->auth_key.response);
3783*38c8a9a5SSteve French 			ses->auth_key.response = NULL;
3784*38c8a9a5SSteve French 			ses->auth_key.len = 0;
3785*38c8a9a5SSteve French 		}
3786*38c8a9a5SSteve French 	}
3787*38c8a9a5SSteve French 
3788*38c8a9a5SSteve French 	cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
3789*38c8a9a5SSteve French 		 server->sec_mode, server->capabilities, server->timeAdj);
3790*38c8a9a5SSteve French 
3791*38c8a9a5SSteve French 	if (server->ops->sess_setup)
3792*38c8a9a5SSteve French 		rc = server->ops->sess_setup(xid, ses, server, nls_info);
3793*38c8a9a5SSteve French 
3794*38c8a9a5SSteve French 	if (rc) {
3795*38c8a9a5SSteve French 		cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
3796*38c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
3797*38c8a9a5SSteve French 		if (ses->ses_status == SES_IN_SETUP)
3798*38c8a9a5SSteve French 			ses->ses_status = SES_NEED_RECON;
3799*38c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
3800*38c8a9a5SSteve French 		cifs_chan_clear_in_reconnect(ses, server);
3801*38c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
3802*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
3803*38c8a9a5SSteve French 	} else {
3804*38c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
3805*38c8a9a5SSteve French 		if (ses->ses_status == SES_IN_SETUP)
3806*38c8a9a5SSteve French 			ses->ses_status = SES_GOOD;
3807*38c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
3808*38c8a9a5SSteve French 		cifs_chan_clear_in_reconnect(ses, server);
3809*38c8a9a5SSteve French 		cifs_chan_clear_need_reconnect(ses, server);
3810*38c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
3811*38c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
3812*38c8a9a5SSteve French 	}
3813*38c8a9a5SSteve French 
3814*38c8a9a5SSteve French 	return rc;
3815*38c8a9a5SSteve French }
3816*38c8a9a5SSteve French 
3817*38c8a9a5SSteve French static int
3818*38c8a9a5SSteve French cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
3819*38c8a9a5SSteve French {
3820*38c8a9a5SSteve French 	ctx->sectype = ses->sectype;
3821*38c8a9a5SSteve French 
3822*38c8a9a5SSteve French 	/* krb5 is special, since we don't need username or pw */
3823*38c8a9a5SSteve French 	if (ctx->sectype == Kerberos)
3824*38c8a9a5SSteve French 		return 0;
3825*38c8a9a5SSteve French 
3826*38c8a9a5SSteve French 	return cifs_set_cifscreds(ctx, ses);
3827*38c8a9a5SSteve French }
3828*38c8a9a5SSteve French 
3829*38c8a9a5SSteve French static struct cifs_tcon *
3830*38c8a9a5SSteve French cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
3831*38c8a9a5SSteve French {
3832*38c8a9a5SSteve French 	int rc;
3833*38c8a9a5SSteve French 	struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3834*38c8a9a5SSteve French 	struct cifs_ses *ses;
3835*38c8a9a5SSteve French 	struct cifs_tcon *tcon = NULL;
3836*38c8a9a5SSteve French 	struct smb3_fs_context *ctx;
3837*38c8a9a5SSteve French 
3838*38c8a9a5SSteve French 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3839*38c8a9a5SSteve French 	if (ctx == NULL)
3840*38c8a9a5SSteve French 		return ERR_PTR(-ENOMEM);
3841*38c8a9a5SSteve French 
3842*38c8a9a5SSteve French 	ctx->local_nls = cifs_sb->local_nls;
3843*38c8a9a5SSteve French 	ctx->linux_uid = fsuid;
3844*38c8a9a5SSteve French 	ctx->cred_uid = fsuid;
3845*38c8a9a5SSteve French 	ctx->UNC = master_tcon->tree_name;
3846*38c8a9a5SSteve French 	ctx->retry = master_tcon->retry;
3847*38c8a9a5SSteve French 	ctx->nocase = master_tcon->nocase;
3848*38c8a9a5SSteve French 	ctx->nohandlecache = master_tcon->nohandlecache;
3849*38c8a9a5SSteve French 	ctx->local_lease = master_tcon->local_lease;
3850*38c8a9a5SSteve French 	ctx->no_lease = master_tcon->no_lease;
3851*38c8a9a5SSteve French 	ctx->resilient = master_tcon->use_resilient;
3852*38c8a9a5SSteve French 	ctx->persistent = master_tcon->use_persistent;
3853*38c8a9a5SSteve French 	ctx->handle_timeout = master_tcon->handle_timeout;
3854*38c8a9a5SSteve French 	ctx->no_linux_ext = !master_tcon->unix_ext;
3855*38c8a9a5SSteve French 	ctx->linux_ext = master_tcon->posix_extensions;
3856*38c8a9a5SSteve French 	ctx->sectype = master_tcon->ses->sectype;
3857*38c8a9a5SSteve French 	ctx->sign = master_tcon->ses->sign;
3858*38c8a9a5SSteve French 	ctx->seal = master_tcon->seal;
3859*38c8a9a5SSteve French 	ctx->witness = master_tcon->use_witness;
3860*38c8a9a5SSteve French 
3861*38c8a9a5SSteve French 	rc = cifs_set_vol_auth(ctx, master_tcon->ses);
3862*38c8a9a5SSteve French 	if (rc) {
3863*38c8a9a5SSteve French 		tcon = ERR_PTR(rc);
3864*38c8a9a5SSteve French 		goto out;
3865*38c8a9a5SSteve French 	}
3866*38c8a9a5SSteve French 
3867*38c8a9a5SSteve French 	/* get a reference for the same TCP session */
3868*38c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
3869*38c8a9a5SSteve French 	++master_tcon->ses->server->srv_count;
3870*38c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
3871*38c8a9a5SSteve French 
3872*38c8a9a5SSteve French 	ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
3873*38c8a9a5SSteve French 	if (IS_ERR(ses)) {
3874*38c8a9a5SSteve French 		tcon = (struct cifs_tcon *)ses;
3875*38c8a9a5SSteve French 		cifs_put_tcp_session(master_tcon->ses->server, 0);
3876*38c8a9a5SSteve French 		goto out;
3877*38c8a9a5SSteve French 	}
3878*38c8a9a5SSteve French 
3879*38c8a9a5SSteve French 	tcon = cifs_get_tcon(ses, ctx);
3880*38c8a9a5SSteve French 	if (IS_ERR(tcon)) {
3881*38c8a9a5SSteve French 		cifs_put_smb_ses(ses);
3882*38c8a9a5SSteve French 		goto out;
3883*38c8a9a5SSteve French 	}
3884*38c8a9a5SSteve French 
3885*38c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3886*38c8a9a5SSteve French 	if (cap_unix(ses))
3887*38c8a9a5SSteve French 		reset_cifs_unix_caps(0, tcon, NULL, ctx);
3888*38c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3889*38c8a9a5SSteve French 
3890*38c8a9a5SSteve French out:
3891*38c8a9a5SSteve French 	kfree(ctx->username);
3892*38c8a9a5SSteve French 	kfree_sensitive(ctx->password);
3893*38c8a9a5SSteve French 	kfree(ctx);
3894*38c8a9a5SSteve French 
3895*38c8a9a5SSteve French 	return tcon;
3896*38c8a9a5SSteve French }
3897*38c8a9a5SSteve French 
3898*38c8a9a5SSteve French struct cifs_tcon *
3899*38c8a9a5SSteve French cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
3900*38c8a9a5SSteve French {
3901*38c8a9a5SSteve French 	return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
3902*38c8a9a5SSteve French }
3903*38c8a9a5SSteve French 
3904*38c8a9a5SSteve French /* find and return a tlink with given uid */
3905*38c8a9a5SSteve French static struct tcon_link *
3906*38c8a9a5SSteve French tlink_rb_search(struct rb_root *root, kuid_t uid)
3907*38c8a9a5SSteve French {
3908*38c8a9a5SSteve French 	struct rb_node *node = root->rb_node;
3909*38c8a9a5SSteve French 	struct tcon_link *tlink;
3910*38c8a9a5SSteve French 
3911*38c8a9a5SSteve French 	while (node) {
3912*38c8a9a5SSteve French 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3913*38c8a9a5SSteve French 
3914*38c8a9a5SSteve French 		if (uid_gt(tlink->tl_uid, uid))
3915*38c8a9a5SSteve French 			node = node->rb_left;
3916*38c8a9a5SSteve French 		else if (uid_lt(tlink->tl_uid, uid))
3917*38c8a9a5SSteve French 			node = node->rb_right;
3918*38c8a9a5SSteve French 		else
3919*38c8a9a5SSteve French 			return tlink;
3920*38c8a9a5SSteve French 	}
3921*38c8a9a5SSteve French 	return NULL;
3922*38c8a9a5SSteve French }
3923*38c8a9a5SSteve French 
3924*38c8a9a5SSteve French /* insert a tcon_link into the tree */
3925*38c8a9a5SSteve French static void
3926*38c8a9a5SSteve French tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
3927*38c8a9a5SSteve French {
3928*38c8a9a5SSteve French 	struct rb_node **new = &(root->rb_node), *parent = NULL;
3929*38c8a9a5SSteve French 	struct tcon_link *tlink;
3930*38c8a9a5SSteve French 
3931*38c8a9a5SSteve French 	while (*new) {
3932*38c8a9a5SSteve French 		tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
3933*38c8a9a5SSteve French 		parent = *new;
3934*38c8a9a5SSteve French 
3935*38c8a9a5SSteve French 		if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
3936*38c8a9a5SSteve French 			new = &((*new)->rb_left);
3937*38c8a9a5SSteve French 		else
3938*38c8a9a5SSteve French 			new = &((*new)->rb_right);
3939*38c8a9a5SSteve French 	}
3940*38c8a9a5SSteve French 
3941*38c8a9a5SSteve French 	rb_link_node(&new_tlink->tl_rbnode, parent, new);
3942*38c8a9a5SSteve French 	rb_insert_color(&new_tlink->tl_rbnode, root);
3943*38c8a9a5SSteve French }
3944*38c8a9a5SSteve French 
3945*38c8a9a5SSteve French /*
3946*38c8a9a5SSteve French  * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
3947*38c8a9a5SSteve French  * current task.
3948*38c8a9a5SSteve French  *
3949*38c8a9a5SSteve French  * If the superblock doesn't refer to a multiuser mount, then just return
3950*38c8a9a5SSteve French  * the master tcon for the mount.
3951*38c8a9a5SSteve French  *
3952*38c8a9a5SSteve French  * First, search the rbtree for an existing tcon for this fsuid. If one
3953*38c8a9a5SSteve French  * exists, then check to see if it's pending construction. If it is then wait
3954*38c8a9a5SSteve French  * for construction to complete. Once it's no longer pending, check to see if
3955*38c8a9a5SSteve French  * it failed and either return an error or retry construction, depending on
3956*38c8a9a5SSteve French  * the timeout.
3957*38c8a9a5SSteve French  *
3958*38c8a9a5SSteve French  * If one doesn't exist then insert a new tcon_link struct into the tree and
3959*38c8a9a5SSteve French  * try to construct a new one.
3960*38c8a9a5SSteve French  */
3961*38c8a9a5SSteve French struct tcon_link *
3962*38c8a9a5SSteve French cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
3963*38c8a9a5SSteve French {
3964*38c8a9a5SSteve French 	int ret;
3965*38c8a9a5SSteve French 	kuid_t fsuid = current_fsuid();
3966*38c8a9a5SSteve French 	struct tcon_link *tlink, *newtlink;
3967*38c8a9a5SSteve French 
3968*38c8a9a5SSteve French 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
3969*38c8a9a5SSteve French 		return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3970*38c8a9a5SSteve French 
3971*38c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
3972*38c8a9a5SSteve French 	tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
3973*38c8a9a5SSteve French 	if (tlink)
3974*38c8a9a5SSteve French 		cifs_get_tlink(tlink);
3975*38c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
3976*38c8a9a5SSteve French 
3977*38c8a9a5SSteve French 	if (tlink == NULL) {
3978*38c8a9a5SSteve French 		newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3979*38c8a9a5SSteve French 		if (newtlink == NULL)
3980*38c8a9a5SSteve French 			return ERR_PTR(-ENOMEM);
3981*38c8a9a5SSteve French 		newtlink->tl_uid = fsuid;
3982*38c8a9a5SSteve French 		newtlink->tl_tcon = ERR_PTR(-EACCES);
3983*38c8a9a5SSteve French 		set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
3984*38c8a9a5SSteve French 		set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
3985*38c8a9a5SSteve French 		cifs_get_tlink(newtlink);
3986*38c8a9a5SSteve French 
3987*38c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
3988*38c8a9a5SSteve French 		/* was one inserted after previous search? */
3989*38c8a9a5SSteve French 		tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
3990*38c8a9a5SSteve French 		if (tlink) {
3991*38c8a9a5SSteve French 			cifs_get_tlink(tlink);
3992*38c8a9a5SSteve French 			spin_unlock(&cifs_sb->tlink_tree_lock);
3993*38c8a9a5SSteve French 			kfree(newtlink);
3994*38c8a9a5SSteve French 			goto wait_for_construction;
3995*38c8a9a5SSteve French 		}
3996*38c8a9a5SSteve French 		tlink = newtlink;
3997*38c8a9a5SSteve French 		tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3998*38c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
3999*38c8a9a5SSteve French 	} else {
4000*38c8a9a5SSteve French wait_for_construction:
4001*38c8a9a5SSteve French 		ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4002*38c8a9a5SSteve French 				  TASK_INTERRUPTIBLE);
4003*38c8a9a5SSteve French 		if (ret) {
4004*38c8a9a5SSteve French 			cifs_put_tlink(tlink);
4005*38c8a9a5SSteve French 			return ERR_PTR(-ERESTARTSYS);
4006*38c8a9a5SSteve French 		}
4007*38c8a9a5SSteve French 
4008*38c8a9a5SSteve French 		/* if it's good, return it */
4009*38c8a9a5SSteve French 		if (!IS_ERR(tlink->tl_tcon))
4010*38c8a9a5SSteve French 			return tlink;
4011*38c8a9a5SSteve French 
4012*38c8a9a5SSteve French 		/* return error if we tried this already recently */
4013*38c8a9a5SSteve French 		if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4014*38c8a9a5SSteve French 			cifs_put_tlink(tlink);
4015*38c8a9a5SSteve French 			return ERR_PTR(-EACCES);
4016*38c8a9a5SSteve French 		}
4017*38c8a9a5SSteve French 
4018*38c8a9a5SSteve French 		if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4019*38c8a9a5SSteve French 			goto wait_for_construction;
4020*38c8a9a5SSteve French 	}
4021*38c8a9a5SSteve French 
4022*38c8a9a5SSteve French 	tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4023*38c8a9a5SSteve French 	clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4024*38c8a9a5SSteve French 	wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4025*38c8a9a5SSteve French 
4026*38c8a9a5SSteve French 	if (IS_ERR(tlink->tl_tcon)) {
4027*38c8a9a5SSteve French 		cifs_put_tlink(tlink);
4028*38c8a9a5SSteve French 		return ERR_PTR(-EACCES);
4029*38c8a9a5SSteve French 	}
4030*38c8a9a5SSteve French 
4031*38c8a9a5SSteve French 	return tlink;
4032*38c8a9a5SSteve French }
4033*38c8a9a5SSteve French 
4034*38c8a9a5SSteve French /*
4035*38c8a9a5SSteve French  * periodic workqueue job that scans tcon_tree for a superblock and closes
4036*38c8a9a5SSteve French  * out tcons.
4037*38c8a9a5SSteve French  */
4038*38c8a9a5SSteve French static void
4039*38c8a9a5SSteve French cifs_prune_tlinks(struct work_struct *work)
4040*38c8a9a5SSteve French {
4041*38c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4042*38c8a9a5SSteve French 						    prune_tlinks.work);
4043*38c8a9a5SSteve French 	struct rb_root *root = &cifs_sb->tlink_tree;
4044*38c8a9a5SSteve French 	struct rb_node *node;
4045*38c8a9a5SSteve French 	struct rb_node *tmp;
4046*38c8a9a5SSteve French 	struct tcon_link *tlink;
4047*38c8a9a5SSteve French 
4048*38c8a9a5SSteve French 	/*
4049*38c8a9a5SSteve French 	 * Because we drop the spinlock in the loop in order to put the tlink
4050*38c8a9a5SSteve French 	 * it's not guarded against removal of links from the tree. The only
4051*38c8a9a5SSteve French 	 * places that remove entries from the tree are this function and
4052*38c8a9a5SSteve French 	 * umounts. Because this function is non-reentrant and is canceled
4053*38c8a9a5SSteve French 	 * before umount can proceed, this is safe.
4054*38c8a9a5SSteve French 	 */
4055*38c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
4056*38c8a9a5SSteve French 	node = rb_first(root);
4057*38c8a9a5SSteve French 	while (node != NULL) {
4058*38c8a9a5SSteve French 		tmp = node;
4059*38c8a9a5SSteve French 		node = rb_next(tmp);
4060*38c8a9a5SSteve French 		tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4061*38c8a9a5SSteve French 
4062*38c8a9a5SSteve French 		if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4063*38c8a9a5SSteve French 		    atomic_read(&tlink->tl_count) != 0 ||
4064*38c8a9a5SSteve French 		    time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4065*38c8a9a5SSteve French 			continue;
4066*38c8a9a5SSteve French 
4067*38c8a9a5SSteve French 		cifs_get_tlink(tlink);
4068*38c8a9a5SSteve French 		clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4069*38c8a9a5SSteve French 		rb_erase(tmp, root);
4070*38c8a9a5SSteve French 
4071*38c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
4072*38c8a9a5SSteve French 		cifs_put_tlink(tlink);
4073*38c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
4074*38c8a9a5SSteve French 	}
4075*38c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
4076*38c8a9a5SSteve French 
4077*38c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4078*38c8a9a5SSteve French 				TLINK_IDLE_EXPIRE);
4079*38c8a9a5SSteve French }
4080*38c8a9a5SSteve French 
4081*38c8a9a5SSteve French #ifndef CONFIG_CIFS_DFS_UPCALL
4082*38c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4083*38c8a9a5SSteve French {
4084*38c8a9a5SSteve French 	int rc;
4085*38c8a9a5SSteve French 	const struct smb_version_operations *ops = tcon->ses->server->ops;
4086*38c8a9a5SSteve French 
4087*38c8a9a5SSteve French 	/* only send once per connect */
4088*38c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
4089*38c8a9a5SSteve French 	if (tcon->status != TID_NEW &&
4090*38c8a9a5SSteve French 	    tcon->status != TID_NEED_TCON) {
4091*38c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
4092*38c8a9a5SSteve French 		return -EHOSTDOWN;
4093*38c8a9a5SSteve French 	}
4094*38c8a9a5SSteve French 
4095*38c8a9a5SSteve French 	if (tcon->status == TID_GOOD) {
4096*38c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
4097*38c8a9a5SSteve French 		return 0;
4098*38c8a9a5SSteve French 	}
4099*38c8a9a5SSteve French 	tcon->status = TID_IN_TCON;
4100*38c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
4101*38c8a9a5SSteve French 
4102*38c8a9a5SSteve French 	rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc);
4103*38c8a9a5SSteve French 	if (rc) {
4104*38c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
4105*38c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
4106*38c8a9a5SSteve French 			tcon->status = TID_NEED_TCON;
4107*38c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
4108*38c8a9a5SSteve French 	} else {
4109*38c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
4110*38c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
4111*38c8a9a5SSteve French 			tcon->status = TID_GOOD;
4112*38c8a9a5SSteve French 		tcon->need_reconnect = false;
4113*38c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
4114*38c8a9a5SSteve French 	}
4115*38c8a9a5SSteve French 
4116*38c8a9a5SSteve French 	return rc;
4117*38c8a9a5SSteve French }
4118*38c8a9a5SSteve French #endif
4119