]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/cifs/connect.c
Fix default behaviour for empty domains and add domainauto option
[thirdparty/linux.git] / fs / cifs / connect.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/connect.c
3 *
1080ef75 4 * Copyright (C) International Business Machines Corp., 2002,2011
1da177e4
LT
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
fb8c4b14 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1da177e4
LT
20 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
24#include <linux/list.h>
25#include <linux/wait.h>
5a0e3ad6 26#include <linux/slab.h>
1da177e4
LT
27#include <linux/pagemap.h>
28#include <linux/ctype.h>
29#include <linux/utsname.h>
30#include <linux/mempool.h>
b8643e1b 31#include <linux/delay.h>
f191401f 32#include <linux/completion.h>
aaf737ad 33#include <linux/kthread.h>
0ae0efad 34#include <linux/pagevec.h>
7dfb7103 35#include <linux/freezer.h>
5c2503a8 36#include <linux/namei.h>
1da177e4
LT
37#include <asm/uaccess.h>
38#include <asm/processor.h>
50b64e3b 39#include <linux/inet.h>
143cb494 40#include <linux/module.h>
8a8798a5 41#include <keys/user-type.h>
0e2bedaa 42#include <net/ipv6.h>
8830d7e0
SP
43#include <linux/parser.h>
44
1da177e4
LT
45#include "cifspdu.h"
46#include "cifsglob.h"
47#include "cifsproto.h"
48#include "cifs_unicode.h"
49#include "cifs_debug.h"
50#include "cifs_fs_sb.h"
51#include "ntlmssp.h"
52#include "nterr.h"
53#include "rfc1002pdu.h"
488f1d2d 54#include "fscache.h"
53e0e11e
PS
55#ifdef CONFIG_CIFS_SMB2
56#include "smb2proto.h"
57#endif
1da177e4
LT
58
59#define CIFS_PORT 445
60#define RFC1001_PORT 139
61
1da177e4
LT
62extern mempool_t *cifs_req_poolp;
63
2de970ff 64/* FIXME: should these be tunable? */
9d002df4 65#define TLINK_ERROR_EXPIRE (1 * HZ)
2de970ff 66#define TLINK_IDLE_EXPIRE (600 * HZ)
9d002df4 67
8830d7e0 68enum {
8830d7e0
SP
69 /* Mount options that take no arguments */
70 Opt_user_xattr, Opt_nouser_xattr,
71 Opt_forceuid, Opt_noforceuid,
72bd481f 72 Opt_forcegid, Opt_noforcegid,
8830d7e0
SP
73 Opt_noblocksend, Opt_noautotune,
74 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
2baa2682 75 Opt_mapposix, Opt_nomapposix,
8830d7e0
SP
76 Opt_mapchars, Opt_nomapchars, Opt_sfu,
77 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
78 Opt_noposixpaths, Opt_nounix,
79 Opt_nocase,
80 Opt_brl, Opt_nobrl,
95932655 81 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
8830d7e0
SP
82 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
83 Opt_nohard, Opt_nosoft,
84 Opt_nointr, Opt_intr,
85 Opt_nostrictsync, Opt_strictsync,
86 Opt_serverino, Opt_noserverino,
87 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
88 Opt_acl, Opt_noacl, Opt_locallease,
1b359204 89 Opt_sign, Opt_seal, Opt_noac,
8830d7e0 90 Opt_fsc, Opt_mfsymlinks,
a0b3df5c 91 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
b2a30774 92 Opt_persistent, Opt_nopersistent,
592fafe6 93 Opt_resilient, Opt_noresilient,
39566443 94 Opt_domainauto,
8830d7e0
SP
95
96 /* Mount options which take numeric value */
97 Opt_backupuid, Opt_backupgid, Opt_uid,
98 Opt_cruid, Opt_gid, Opt_file_mode,
99 Opt_dirmode, Opt_port,
100 Opt_rsize, Opt_wsize, Opt_actimeo,
141891f4 101 Opt_echo_interval, Opt_max_credits,
8b217fe7 102 Opt_snapshot,
8830d7e0
SP
103
104 /* Mount options which take string value */
105 Opt_user, Opt_pass, Opt_ip,
73a999fa 106 Opt_domain, Opt_srcaddr, Opt_iocharset,
8830d7e0 107 Opt_netbiosname, Opt_servern,
23db65f5 108 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
8830d7e0
SP
109
110 /* Mount options to be ignored */
111 Opt_ignore,
112
113 /* Options which could be blank */
114 Opt_blank_pass,
4fe9e963
SP
115 Opt_blank_user,
116 Opt_blank_ip,
8830d7e0
SP
117
118 Opt_err
119};
120
121static const match_table_t cifs_mount_option_tokens = {
122
123 { Opt_user_xattr, "user_xattr" },
124 { Opt_nouser_xattr, "nouser_xattr" },
125 { Opt_forceuid, "forceuid" },
126 { Opt_noforceuid, "noforceuid" },
72bd481f
JL
127 { Opt_forcegid, "forcegid" },
128 { Opt_noforcegid, "noforcegid" },
8830d7e0
SP
129 { Opt_noblocksend, "noblocksend" },
130 { Opt_noautotune, "noautotune" },
131 { Opt_hard, "hard" },
132 { Opt_soft, "soft" },
133 { Opt_perm, "perm" },
134 { Opt_noperm, "noperm" },
2baa2682 135 { Opt_mapchars, "mapchars" }, /* SFU style */
8830d7e0 136 { Opt_nomapchars, "nomapchars" },
2baa2682
SF
137 { Opt_mapposix, "mapposix" }, /* SFM style */
138 { Opt_nomapposix, "nomapposix" },
8830d7e0
SP
139 { Opt_sfu, "sfu" },
140 { Opt_nosfu, "nosfu" },
141 { Opt_nodfs, "nodfs" },
142 { Opt_posixpaths, "posixpaths" },
143 { Opt_noposixpaths, "noposixpaths" },
144 { Opt_nounix, "nounix" },
145 { Opt_nounix, "nolinux" },
146 { Opt_nocase, "nocase" },
147 { Opt_nocase, "ignorecase" },
148 { Opt_brl, "brl" },
149 { Opt_nobrl, "nobrl" },
150 { Opt_nobrl, "nolock" },
151 { Opt_forcemandatorylock, "forcemandatorylock" },
5cfdddcf 152 { Opt_forcemandatorylock, "forcemand" },
8830d7e0
SP
153 { Opt_setuids, "setuids" },
154 { Opt_nosetuids, "nosetuids" },
95932655 155 { Opt_setuidfromacl, "idsfromsid" },
8830d7e0
SP
156 { Opt_dynperm, "dynperm" },
157 { Opt_nodynperm, "nodynperm" },
158 { Opt_nohard, "nohard" },
159 { Opt_nosoft, "nosoft" },
160 { Opt_nointr, "nointr" },
161 { Opt_intr, "intr" },
162 { Opt_nostrictsync, "nostrictsync" },
163 { Opt_strictsync, "strictsync" },
164 { Opt_serverino, "serverino" },
165 { Opt_noserverino, "noserverino" },
166 { Opt_rwpidforward, "rwpidforward" },
167 { Opt_cifsacl, "cifsacl" },
168 { Opt_nocifsacl, "nocifsacl" },
169 { Opt_acl, "acl" },
170 { Opt_noacl, "noacl" },
171 { Opt_locallease, "locallease" },
172 { Opt_sign, "sign" },
173 { Opt_seal, "seal" },
8830d7e0
SP
174 { Opt_noac, "noac" },
175 { Opt_fsc, "fsc" },
176 { Opt_mfsymlinks, "mfsymlinks" },
177 { Opt_multiuser, "multiuser" },
d8162558 178 { Opt_sloppy, "sloppy" },
a0b3df5c 179 { Opt_nosharesock, "nosharesock" },
b2a30774
SF
180 { Opt_persistent, "persistenthandles"},
181 { Opt_nopersistent, "nopersistenthandles"},
592fafe6
SF
182 { Opt_resilient, "resilienthandles"},
183 { Opt_noresilient, "noresilienthandles"},
39566443 184 { Opt_domainauto, "domainauto"},
8830d7e0
SP
185
186 { Opt_backupuid, "backupuid=%s" },
187 { Opt_backupgid, "backupgid=%s" },
188 { Opt_uid, "uid=%s" },
189 { Opt_cruid, "cruid=%s" },
190 { Opt_gid, "gid=%s" },
191 { Opt_file_mode, "file_mode=%s" },
192 { Opt_dirmode, "dirmode=%s" },
193 { Opt_dirmode, "dir_mode=%s" },
194 { Opt_port, "port=%s" },
195 { Opt_rsize, "rsize=%s" },
196 { Opt_wsize, "wsize=%s" },
197 { Opt_actimeo, "actimeo=%s" },
adfeb3e0 198 { Opt_echo_interval, "echo_interval=%s" },
141891f4 199 { Opt_max_credits, "max_credits=%s" },
8b217fe7 200 { Opt_snapshot, "snapshot=%s" },
8830d7e0 201
4fe9e963
SP
202 { Opt_blank_user, "user=" },
203 { Opt_blank_user, "username=" },
8830d7e0
SP
204 { Opt_user, "user=%s" },
205 { Opt_user, "username=%s" },
206 { Opt_blank_pass, "pass=" },
3c15b4cf 207 { Opt_blank_pass, "password=" },
8830d7e0
SP
208 { Opt_pass, "pass=%s" },
209 { Opt_pass, "password=%s" },
4fe9e963
SP
210 { Opt_blank_ip, "ip=" },
211 { Opt_blank_ip, "addr=" },
8830d7e0
SP
212 { Opt_ip, "ip=%s" },
213 { Opt_ip, "addr=%s" },
73a999fa
JL
214 { Opt_ignore, "unc=%s" },
215 { Opt_ignore, "target=%s" },
216 { Opt_ignore, "path=%s" },
8830d7e0
SP
217 { Opt_domain, "dom=%s" },
218 { Opt_domain, "domain=%s" },
219 { Opt_domain, "workgroup=%s" },
220 { Opt_srcaddr, "srcaddr=%s" },
73a999fa 221 { Opt_ignore, "prefixpath=%s" },
8830d7e0 222 { Opt_iocharset, "iocharset=%s" },
8830d7e0
SP
223 { Opt_netbiosname, "netbiosname=%s" },
224 { Opt_servern, "servern=%s" },
225 { Opt_ver, "ver=%s" },
23db65f5 226 { Opt_vers, "vers=%s" },
8830d7e0 227 { Opt_sec, "sec=%s" },
15b6a473 228 { Opt_cache, "cache=%s" },
8830d7e0
SP
229
230 { Opt_ignore, "cred" },
231 { Opt_ignore, "credentials" },
a557b976
JL
232 { Opt_ignore, "cred=%s" },
233 { Opt_ignore, "credentials=%s" },
8830d7e0
SP
234 { Opt_ignore, "guest" },
235 { Opt_ignore, "rw" },
236 { Opt_ignore, "ro" },
237 { Opt_ignore, "suid" },
238 { Opt_ignore, "nosuid" },
239 { Opt_ignore, "exec" },
240 { Opt_ignore, "noexec" },
241 { Opt_ignore, "nodev" },
242 { Opt_ignore, "noauto" },
243 { Opt_ignore, "dev" },
244 { Opt_ignore, "mand" },
245 { Opt_ignore, "nomand" },
246 { Opt_ignore, "_netdev" },
247
248 { Opt_err, NULL }
249};
250
251enum {
252 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
253 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
7659624f
JL
254 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
255 Opt_sec_ntlmv2i, Opt_sec_lanman,
8830d7e0
SP
256 Opt_sec_none,
257
258 Opt_sec_err
259};
260
261static const match_table_t cifs_secflavor_tokens = {
262 { Opt_sec_krb5, "krb5" },
263 { Opt_sec_krb5i, "krb5i" },
264 { Opt_sec_krb5p, "krb5p" },
265 { Opt_sec_ntlmsspi, "ntlmsspi" },
266 { Opt_sec_ntlmssp, "ntlmssp" },
267 { Opt_ntlm, "ntlm" },
268 { Opt_sec_ntlmi, "ntlmi" },
7659624f
JL
269 { Opt_sec_ntlmv2, "nontlm" },
270 { Opt_sec_ntlmv2, "ntlmv2" },
8830d7e0 271 { Opt_sec_ntlmv2i, "ntlmv2i" },
8830d7e0
SP
272 { Opt_sec_lanman, "lanman" },
273 { Opt_sec_none, "none" },
274
275 { Opt_sec_err, NULL }
276};
277
15b6a473
JL
278/* cache flavors */
279enum {
280 Opt_cache_loose,
281 Opt_cache_strict,
282 Opt_cache_none,
283 Opt_cache_err
284};
285
286static const match_table_t cifs_cacheflavor_tokens = {
287 { Opt_cache_loose, "loose" },
288 { Opt_cache_strict, "strict" },
289 { Opt_cache_none, "none" },
290 { Opt_cache_err, NULL }
291};
292
23db65f5
JL
293static const match_table_t cifs_smb_version_tokens = {
294 { Smb_1, SMB1_VERSION_STRING },
dd446b16 295 { Smb_20, SMB20_VERSION_STRING},
1080ef75 296 { Smb_21, SMB21_VERSION_STRING },
e4aa25e7 297 { Smb_30, SMB30_VERSION_STRING },
20b6d8b4 298 { Smb_302, SMB302_VERSION_STRING },
5f7fbf73
SF
299#ifdef CONFIG_CIFS_SMB311
300 { Smb_311, SMB311_VERSION_STRING },
aab1893d 301 { Smb_311, ALT_SMB311_VERSION_STRING },
5f7fbf73
SF
302#endif /* SMB311 */
303 { Smb_version_err, NULL }
23db65f5
JL
304};
305
a9f1b85e
PS
306static int ip_connect(struct TCP_Server_Info *server);
307static int generic_ip_connect(struct TCP_Server_Info *server);
b647c35f 308static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
2de970ff 309static void cifs_prune_tlinks(struct work_struct *work);
b9bce2e9
JL
310static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
311 const char *devname);
1da177e4 312
d5c5605c
JL
313/*
314 * cifs tcp session reconnection
315 *
316 * mark tcp session as reconnecting so temporarily locked
317 * mark all smb sessions as reconnecting for tcp session
318 * reconnect tcp session
319 * wake up waiters on reconnection? - (not needed currently)
320 */
28ea5290 321int
1da177e4
LT
322cifs_reconnect(struct TCP_Server_Info *server)
323{
324 int rc = 0;
f1987b44 325 struct list_head *tmp, *tmp2;
96daf2b0
SF
326 struct cifs_ses *ses;
327 struct cifs_tcon *tcon;
fb8c4b14 328 struct mid_q_entry *mid_entry;
3c1105df 329 struct list_head retry_list;
50c2f753 330
1da177e4 331 spin_lock(&GlobalMid_Lock);
469ee614 332 if (server->tcpStatus == CifsExiting) {
fb8c4b14 333 /* the demux thread will exit normally
1da177e4
LT
334 next time through the loop */
335 spin_unlock(&GlobalMid_Lock);
336 return rc;
337 } else
338 server->tcpStatus = CifsNeedReconnect;
339 spin_unlock(&GlobalMid_Lock);
340 server->maxBuf = 0;
aa24d1e9
PS
341#ifdef CONFIG_CIFS_SMB2
342 server->max_read = 0;
343#endif
1da177e4 344
f96637be 345 cifs_dbg(FYI, "Reconnecting tcp session\n");
1da177e4
LT
346
347 /* before reconnecting the tcp session, mark the smb session (uid)
348 and the tid bad so they are not used until reconnected */
f96637be
JP
349 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
350 __func__);
3f9bcca7 351 spin_lock(&cifs_tcp_ses_lock);
14fbf50d 352 list_for_each(tmp, &server->smb_ses_list) {
96daf2b0 353 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
14fbf50d
JL
354 ses->need_reconnect = true;
355 ses->ipc_tid = 0;
f1987b44 356 list_for_each(tmp2, &ses->tcon_list) {
96daf2b0 357 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
3b795210 358 tcon->need_reconnect = true;
1da177e4 359 }
1da177e4 360 }
3f9bcca7 361 spin_unlock(&cifs_tcp_ses_lock);
2b84a36c 362
1da177e4 363 /* do not want to be sending data on a socket we are freeing */
f96637be 364 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
72ca545b 365 mutex_lock(&server->srv_mutex);
fb8c4b14 366 if (server->ssocket) {
f96637be
JP
367 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
368 server->ssocket->state, server->ssocket->flags);
91cf45f0 369 kernel_sock_shutdown(server->ssocket, SHUT_WR);
f96637be
JP
370 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
371 server->ssocket->state, server->ssocket->flags);
1da177e4
LT
372 sock_release(server->ssocket);
373 server->ssocket = NULL;
374 }
5d0d2882
SP
375 server->sequence_number = 0;
376 server->session_estab = false;
21e73393
SP
377 kfree(server->session_key.response);
378 server->session_key.response = NULL;
379 server->session_key.len = 0;
fda35943 380 server->lstrp = jiffies;
1da177e4 381
2b84a36c 382 /* mark submitted MIDs for retry and issue callback */
3c1105df 383 INIT_LIST_HEAD(&retry_list);
f96637be 384 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
1da177e4 385 spin_lock(&GlobalMid_Lock);
2b84a36c
JL
386 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
387 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
7c9421e1
PS
388 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
389 mid_entry->mid_state = MID_RETRY_NEEDED;
3c1105df
JL
390 list_move(&mid_entry->qhead, &retry_list);
391 }
392 spin_unlock(&GlobalMid_Lock);
820962dc 393 mutex_unlock(&server->srv_mutex);
3c1105df 394
f96637be 395 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
3c1105df
JL
396 list_for_each_safe(tmp, tmp2, &retry_list) {
397 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
2b84a36c
JL
398 list_del_init(&mid_entry->qhead);
399 mid_entry->callback(mid_entry);
1da177e4 400 }
1da177e4 401
7fdbaa1b 402 do {
6c3d8909 403 try_to_freeze();
a9f1b85e
PS
404
405 /* we should try only the port we connected to before */
73e216a8 406 mutex_lock(&server->srv_mutex);
a9f1b85e 407 rc = generic_ip_connect(server);
fb8c4b14 408 if (rc) {
f96637be 409 cifs_dbg(FYI, "reconnect error %d\n", rc);
4afe260b 410 mutex_unlock(&server->srv_mutex);
0cb766ae 411 msleep(3000);
1da177e4
LT
412 } else {
413 atomic_inc(&tcpSesReconnectCount);
414 spin_lock(&GlobalMid_Lock);
469ee614 415 if (server->tcpStatus != CifsExiting)
fd88ce93 416 server->tcpStatus = CifsNeedNegotiate;
fb8c4b14 417 spin_unlock(&GlobalMid_Lock);
4afe260b 418 mutex_unlock(&server->srv_mutex);
1da177e4 419 }
7fdbaa1b 420 } while (server->tcpStatus == CifsNeedReconnect);
2b84a36c 421
b8c60012
SP
422 if (server->tcpStatus == CifsNeedNegotiate)
423 mod_delayed_work(cifsiod_wq, &server->echo, 0);
424
1da177e4
LT
425 return rc;
426}
427
c74093b6
JL
428static void
429cifs_echo_request(struct work_struct *work)
430{
431 int rc;
432 struct TCP_Server_Info *server = container_of(work,
433 struct TCP_Server_Info, echo.work);
b8c60012
SP
434 unsigned long echo_interval;
435
436 /*
437 * If we need to renegotiate, set echo interval to zero to
438 * immediately call echo service where we can renegotiate.
439 */
440 if (server->tcpStatus == CifsNeedNegotiate)
441 echo_interval = 0;
442 else
443 echo_interval = server->echo_interval;
c74093b6 444
247ec9b4 445 /*
b8c60012
SP
446 * We cannot send an echo if it is disabled.
447 * Also, no need to ping if we got a response recently.
247ec9b4 448 */
4fcd1813
SF
449
450 if (server->tcpStatus == CifsNeedReconnect ||
b8c60012
SP
451 server->tcpStatus == CifsExiting ||
452 server->tcpStatus == CifsNew ||
f6d76178 453 (server->ops->can_echo && !server->ops->can_echo(server)) ||
adfeb3e0 454 time_before(jiffies, server->lstrp + echo_interval - HZ))
c74093b6
JL
455 goto requeue_echo;
456
f6d76178 457 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
c74093b6 458 if (rc)
f96637be
JP
459 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
460 server->hostname);
c74093b6
JL
461
462requeue_echo:
b8c60012 463 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
c74093b6
JL
464}
465
3d9c2472 466static bool
2a37ef94 467allocate_buffers(struct TCP_Server_Info *server)
3d9c2472 468{
2a37ef94
JL
469 if (!server->bigbuf) {
470 server->bigbuf = (char *)cifs_buf_get();
471 if (!server->bigbuf) {
f96637be 472 cifs_dbg(VFS, "No memory for large SMB response\n");
3d9c2472
PS
473 msleep(3000);
474 /* retry will check if exiting */
475 return false;
476 }
2a37ef94 477 } else if (server->large_buf) {
3d9c2472 478 /* we are reusing a dirty large buf, clear its start */
1887f601 479 memset(server->bigbuf, 0, HEADER_SIZE(server));
3d9c2472
PS
480 }
481
2a37ef94
JL
482 if (!server->smallbuf) {
483 server->smallbuf = (char *)cifs_small_buf_get();
484 if (!server->smallbuf) {
f96637be 485 cifs_dbg(VFS, "No memory for SMB response\n");
3d9c2472
PS
486 msleep(1000);
487 /* retry will check if exiting */
488 return false;
489 }
490 /* beginning of smb buffer is cleared in our buf_get */
491 } else {
492 /* if existing small buf clear beginning */
1887f601 493 memset(server->smallbuf, 0, HEADER_SIZE(server));
3d9c2472
PS
494 }
495
3d9c2472
PS
496 return true;
497}
498
ba749e6d
JL
499static bool
500server_unresponsive(struct TCP_Server_Info *server)
501{
6dae51a5
PS
502 /*
503 * We need to wait 2 echo intervals to make sure we handle such
504 * situations right:
505 * 1s client sends a normal SMB request
506 * 2s client gets a response
507 * 30s echo workqueue job pops, and decides we got a response recently
508 * and don't need to send another
509 * ...
510 * 65s kernel_recvmsg times out, and we see that we haven't gotten
511 * a response in >60s.
512 */
513 if (server->tcpStatus == CifsGood &&
adfeb3e0
SF
514 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
515 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
516 server->hostname, (2 * server->echo_interval) / HZ);
ba749e6d
JL
517 cifs_reconnect(server);
518 wake_up(&server->response_q);
519 return true;
520 }
521
522 return false;
523}
524
71335664
AV
525static int
526cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
e7015fb1 527{
a52c1eb7
JL
528 int length = 0;
529 int total_read;
e7015fb1 530
71335664
AV
531 smb_msg->msg_control = NULL;
532 smb_msg->msg_controllen = 0;
e831e6cf 533
71335664 534 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
95edcff4
JL
535 try_to_freeze();
536
71335664
AV
537 if (server_unresponsive(server))
538 return -ECONNABORTED;
ba749e6d 539
71335664 540 length = sock_recvmsg(server->ssocket, smb_msg, 0);
42c4dfc2 541
71335664
AV
542 if (server->tcpStatus == CifsExiting)
543 return -ESHUTDOWN;
e7015fb1 544
71335664 545 if (server->tcpStatus == CifsNeedReconnect) {
e7015fb1 546 cifs_reconnect(server);
71335664
AV
547 return -ECONNABORTED;
548 }
549
550 if (length == -ERESTARTSYS ||
551 length == -EAGAIN ||
552 length == -EINTR) {
e7015fb1
PS
553 /*
554 * Minimum sleep to prevent looping, allowing socket
555 * to clear and app threads to set tcpStatus
556 * CifsNeedReconnect if server hung.
557 */
558 usleep_range(1000, 2000);
559 length = 0;
a52c1eb7 560 continue;
71335664
AV
561 }
562
563 if (length <= 0) {
09aab880 564 cifs_dbg(FYI, "Received no data or error: %d\n", length);
e7015fb1 565 cifs_reconnect(server);
71335664 566 return -ECONNABORTED;
e7015fb1
PS
567 }
568 }
a52c1eb7 569 return total_read;
e7015fb1 570}
e7015fb1 571
e28bc5b1
JL
572int
573cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
574 unsigned int to_read)
42c4dfc2 575{
71335664
AV
576 struct msghdr smb_msg;
577 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
578 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
42c4dfc2 579
71335664
AV
580 return cifs_readv_from_socket(server, &smb_msg);
581}
42c4dfc2 582
71335664
AV
583int
584cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
585 unsigned int to_read)
586{
587 struct msghdr smb_msg;
588 struct bio_vec bv = {.bv_page = page, .bv_len = to_read};
589 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
590 return cifs_readv_from_socket(server, &smb_msg);
e7015fb1
PS
591}
592
98bac62c 593static bool
fe11e4cc 594is_smb_response(struct TCP_Server_Info *server, unsigned char type)
98bac62c 595{
98bac62c
PS
596 /*
597 * The first byte big endian of the length field,
598 * is actually not part of the length but the type
599 * with the most common, zero, as regular data.
600 */
fe11e4cc
JL
601 switch (type) {
602 case RFC1002_SESSION_MESSAGE:
603 /* Regular SMB response */
604 return true;
605 case RFC1002_SESSION_KEEP_ALIVE:
f96637be 606 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
fe11e4cc
JL
607 break;
608 case RFC1002_POSITIVE_SESSION_RESPONSE:
f96637be 609 cifs_dbg(FYI, "RFC 1002 positive session response\n");
fe11e4cc
JL
610 break;
611 case RFC1002_NEGATIVE_SESSION_RESPONSE:
98bac62c
PS
612 /*
613 * We get this from Windows 98 instead of an error on
614 * SMB negprot response.
615 */
f96637be 616 cifs_dbg(FYI, "RFC 1002 negative session response\n");
98bac62c
PS
617 /* give server a second to clean up */
618 msleep(1000);
619 /*
620 * Always try 445 first on reconnect since we get NACK
621 * on some if we ever connected to port 139 (the NACK
622 * is since we do not begin with RFC1001 session
623 * initialize frame).
624 */
fe11e4cc 625 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
98bac62c
PS
626 cifs_reconnect(server);
627 wake_up(&server->response_q);
fe11e4cc
JL
628 break;
629 default:
f96637be 630 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
98bac62c 631 cifs_reconnect(server);
98bac62c
PS
632 }
633
fe11e4cc 634 return false;
98bac62c
PS
635}
636
e28bc5b1
JL
637void
638dequeue_mid(struct mid_q_entry *mid, bool malformed)
ea1f4502 639{
ad69bae1 640#ifdef CONFIG_CIFS_STATS2
ea1f4502 641 mid->when_received = jiffies;
ad69bae1 642#endif
ea1f4502
JL
643 spin_lock(&GlobalMid_Lock);
644 if (!malformed)
7c9421e1 645 mid->mid_state = MID_RESPONSE_RECEIVED;
ea1f4502 646 else
7c9421e1 647 mid->mid_state = MID_RESPONSE_MALFORMED;
ea1f4502 648 list_del_init(&mid->qhead);
ad69bae1 649 spin_unlock(&GlobalMid_Lock);
ea1f4502 650}
ad69bae1 651
c8054ebd
JL
652static void
653handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
d4e4854f 654 char *buf, int malformed)
ea1f4502 655{
316cf94a
PS
656 if (server->ops->check_trans2 &&
657 server->ops->check_trans2(mid, server, buf, malformed))
c8054ebd 658 return;
ea1f4502 659 mid->resp_buf = buf;
7c9421e1 660 mid->large_buf = server->large_buf;
2a37ef94
JL
661 /* Was previous buf put in mpx struct for multi-rsp? */
662 if (!mid->multiRsp) {
663 /* smb buffer will be freed by user thread */
664 if (server->large_buf)
665 server->bigbuf = NULL;
666 else
667 server->smallbuf = NULL;
668 }
ffc00e27 669 dequeue_mid(mid, malformed);
ad69bae1
PS
670}
671
762dfd10
PS
672static void clean_demultiplex_info(struct TCP_Server_Info *server)
673{
674 int length;
675
676 /* take it off the list, if it's not already */
677 spin_lock(&cifs_tcp_ses_lock);
678 list_del_init(&server->tcp_ses_list);
679 spin_unlock(&cifs_tcp_ses_lock);
680
681 spin_lock(&GlobalMid_Lock);
682 server->tcpStatus = CifsExiting;
683 spin_unlock(&GlobalMid_Lock);
684 wake_up_all(&server->response_q);
685
2d86dbc9 686 /* check if we have blocked requests that need to free */
fc40f9cf 687 spin_lock(&server->req_lock);
2d86dbc9
PS
688 if (server->credits <= 0)
689 server->credits = 1;
fc40f9cf 690 spin_unlock(&server->req_lock);
762dfd10
PS
691 /*
692 * Although there should not be any requests blocked on this queue it
693 * can not hurt to be paranoid and try to wake up requests that may
694 * haven been blocked when more than 50 at time were on the wire to the
695 * same server - they now will see the session is in exit state and get
696 * out of SendReceive.
697 */
698 wake_up_all(&server->request_q);
699 /* give those requests time to exit */
700 msleep(125);
701
702 if (server->ssocket) {
703 sock_release(server->ssocket);
704 server->ssocket = NULL;
705 }
706
707 if (!list_empty(&server->pending_mid_q)) {
708 struct list_head dispose_list;
709 struct mid_q_entry *mid_entry;
710 struct list_head *tmp, *tmp2;
711
712 INIT_LIST_HEAD(&dispose_list);
713 spin_lock(&GlobalMid_Lock);
714 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
715 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
f96637be 716 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
7c9421e1 717 mid_entry->mid_state = MID_SHUTDOWN;
762dfd10
PS
718 list_move(&mid_entry->qhead, &dispose_list);
719 }
720 spin_unlock(&GlobalMid_Lock);
721
722 /* now walk dispose list and issue callbacks */
723 list_for_each_safe(tmp, tmp2, &dispose_list) {
724 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
f96637be 725 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
762dfd10
PS
726 list_del_init(&mid_entry->qhead);
727 mid_entry->callback(mid_entry);
728 }
729 /* 1/8th of sec is more than enough time for them to exit */
730 msleep(125);
731 }
732
733 if (!list_empty(&server->pending_mid_q)) {
734 /*
735 * mpx threads have not exited yet give them at least the smb
736 * send timeout time for long ops.
737 *
738 * Due to delays on oplock break requests, we need to wait at
739 * least 45 seconds before giving up on a request getting a
740 * response and going ahead and killing cifsd.
741 */
f96637be 742 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
762dfd10
PS
743 msleep(46000);
744 /*
745 * If threads still have not exited they are probably never
746 * coming home not much else we can do but free the memory.
747 */
748 }
749
750 kfree(server->hostname);
751 kfree(server);
752
753 length = atomic_dec_return(&tcpSesAllocCount);
754 if (length > 0)
11d83360 755 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
762dfd10
PS
756}
757
e9097ab4
JL
758static int
759standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
760{
761 int length;
762 char *buf = server->smallbuf;
d4e4854f 763 unsigned int pdu_length = get_rfc1002_length(buf);
e9097ab4
JL
764
765 /* make sure this will fit in a large buffer */
1887f601 766 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 4) {
f96637be 767 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
e9097ab4
JL
768 cifs_reconnect(server);
769 wake_up(&server->response_q);
3fabaa27 770 return -ECONNABORTED;
e9097ab4
JL
771 }
772
773 /* switch to large buffer if too big for a small one */
774 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
775 server->large_buf = true;
d4e4854f 776 memcpy(server->bigbuf, buf, server->total_read);
e9097ab4 777 buf = server->bigbuf;
e9097ab4
JL
778 }
779
780 /* now read the rest */
1887f601
PS
781 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
782 pdu_length - HEADER_SIZE(server) + 1 + 4);
e9097ab4
JL
783 if (length < 0)
784 return length;
785 server->total_read += length;
786
d4e4854f 787 dump_smb(buf, server->total_read);
e9097ab4
JL
788
789 /*
790 * We know that we received enough to get to the MID as we
791 * checked the pdu_length earlier. Now check to see
792 * if the rest of the header is OK. We borrow the length
793 * var for the rest of the loop to avoid a new stack var.
794 *
795 * 48 bytes is enough to display the header and a little bit
796 * into the payload for debugging purposes.
797 */
373512ec 798 length = server->ops->check_message(buf, server->total_read, server);
e9097ab4
JL
799 if (length != 0)
800 cifs_dump_mem("Bad SMB: ", buf,
801 min_t(unsigned int, server->total_read, 48));
802
2e44b288
PS
803 if (server->ops->is_status_pending &&
804 server->ops->is_status_pending(buf, server, length))
805 return -1;
806
ff4fa4a2
JL
807 if (!mid)
808 return length;
e9097ab4 809
d4e4854f 810 handle_mid(mid, server, buf, length);
ff4fa4a2 811 return 0;
e9097ab4
JL
812}
813
1da177e4 814static int
7c97c200 815cifs_demultiplex_thread(void *p)
1da177e4
LT
816{
817 int length;
7c97c200 818 struct TCP_Server_Info *server = p;
2a37ef94
JL
819 unsigned int pdu_length;
820 char *buf = NULL;
a5c3e1c7 821 struct task_struct *task_to_wake = NULL;
1da177e4 822 struct mid_q_entry *mid_entry;
1da177e4 823
1da177e4 824 current->flags |= PF_MEMALLOC;
f96637be 825 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
93d0ec85
JL
826
827 length = atomic_inc_return(&tcpSesAllocCount);
828 if (length > 1)
11d83360 829 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1da177e4 830
83144186 831 set_freezable();
469ee614 832 while (server->tcpStatus != CifsExiting) {
ede1327e
SF
833 if (try_to_freeze())
834 continue;
b8643e1b 835
2a37ef94 836 if (!allocate_buffers(server))
3d9c2472 837 continue;
b8643e1b 838
2a37ef94 839 server->large_buf = false;
2a37ef94 840 buf = server->smallbuf;
f01d5e14 841 pdu_length = 4; /* enough to get RFC1001 header */
fda35943 842
e28bc5b1 843 length = cifs_read_from_socket(server, buf, pdu_length);
a52c1eb7 844 if (length < 0)
1da177e4 845 continue;
2a37ef94 846 server->total_read = length;
1da177e4 847
98bac62c
PS
848 /*
849 * The right amount was read from socket - 4 bytes,
850 * so we can now interpret the length field.
851 */
d4e4854f 852 pdu_length = get_rfc1002_length(buf);
70ca734a 853
f96637be 854 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
fe11e4cc 855 if (!is_smb_response(server, buf[0]))
fb8c4b14 856 continue;
e4eb295d 857
89482a56 858 /* make sure we have enough to get to the MID */
1887f601 859 if (pdu_length < HEADER_SIZE(server) - 1 - 4) {
f96637be
JP
860 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
861 pdu_length);
89482a56
JL
862 cifs_reconnect(server);
863 wake_up(&server->response_q);
864 continue;
e4eb295d 865 }
e7015fb1 866
89482a56 867 /* read down to the MID */
e28bc5b1 868 length = cifs_read_from_socket(server, buf + 4,
1887f601 869 HEADER_SIZE(server) - 1 - 4);
89482a56 870 if (length < 0)
e4eb295d 871 continue;
2a37ef94 872 server->total_read += length;
1da177e4 873
8aa26f3e 874 mid_entry = server->ops->find_mid(server, buf);
50c2f753 875
44d22d84
JL
876 if (!mid_entry || !mid_entry->receive)
877 length = standard_receive3(server, mid_entry);
878 else
879 length = mid_entry->receive(server, mid_entry);
71823baf 880
e9097ab4 881 if (length < 0)
fe11e4cc 882 continue;
1da177e4 883
d4e4854f 884 if (server->large_buf)
2a37ef94 885 buf = server->bigbuf;
fda35943 886
fda35943 887 server->lstrp = jiffies;
2b84a36c 888 if (mid_entry != NULL) {
2a37ef94
JL
889 if (!mid_entry->multiRsp || mid_entry->multiEnd)
890 mid_entry->callback(mid_entry);
7f0adb53
PS
891 } else if (!server->ops->is_oplock_break ||
892 !server->ops->is_oplock_break(buf, server)) {
f96637be
JP
893 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
894 atomic_read(&midCount));
1887f601
PS
895 cifs_dump_mem("Received Data is: ", buf,
896 HEADER_SIZE(server));
3979877e 897#ifdef CONFIG_CIFS_DEBUG2
7f0adb53
PS
898 if (server->ops->dump_detail)
899 server->ops->dump_detail(buf);
3979877e
SF
900 cifs_dump_mids(server);
901#endif /* CIFS_DEBUG2 */
50c2f753 902
e4eb295d
SF
903 }
904 } /* end while !EXITING */
905
fd62cb7e 906 /* buffer usually freed in free_mid - need to free it here on exit */
2a37ef94
JL
907 cifs_buf_release(server->bigbuf);
908 if (server->smallbuf) /* no sense logging a debug message if NULL */
909 cifs_small_buf_release(server->smallbuf);
1da177e4 910
a5c3e1c7 911 task_to_wake = xchg(&server->tsk, NULL);
762dfd10 912 clean_demultiplex_info(server);
a5c3e1c7
SF
913
914 /* if server->tsk was NULL then wait for a signal before exiting */
915 if (!task_to_wake) {
916 set_current_state(TASK_INTERRUPTIBLE);
917 while (!signal_pending(current)) {
918 schedule();
919 set_current_state(TASK_INTERRUPTIBLE);
920 }
921 set_current_state(TASK_RUNNING);
922 }
923
0468a2cf 924 module_put_and_exit(0);
1da177e4
LT
925}
926
c359cf3c
JL
927/* extract the host portion of the UNC string */
928static char *
929extract_hostname(const char *unc)
930{
931 const char *src;
932 char *dst, *delim;
933 unsigned int len;
934
935 /* skip double chars at beginning of string */
936 /* BB: check validity of these bytes? */
937 src = unc + 2;
938
939 /* delimiter between hostname and sharename is always '\\' now */
940 delim = strchr(src, '\\');
941 if (!delim)
942 return ERR_PTR(-EINVAL);
943
944 len = delim - src;
945 dst = kmalloc((len + 1), GFP_KERNEL);
946 if (dst == NULL)
947 return ERR_PTR(-ENOMEM);
948
949 memcpy(dst, src, len);
950 dst[len] = '\0';
951
952 return dst;
953}
954
8830d7e0
SP
955static int get_option_ul(substring_t args[], unsigned long *option)
956{
957 int rc;
958 char *string;
959
960 string = match_strdup(args);
961 if (string == NULL)
962 return -ENOMEM;
bfa890a3 963 rc = kstrtoul(string, 0, option);
8830d7e0
SP
964 kfree(string);
965
966 return rc;
967}
968
3da46565
EB
969static int get_option_uid(substring_t args[], kuid_t *result)
970{
971 unsigned long value;
972 kuid_t uid;
973 int rc;
974
975 rc = get_option_ul(args, &value);
976 if (rc)
977 return rc;
978
979 uid = make_kuid(current_user_ns(), value);
980 if (!uid_valid(uid))
981 return -EINVAL;
982
983 *result = uid;
984 return 0;
985}
986
987static int get_option_gid(substring_t args[], kgid_t *result)
988{
989 unsigned long value;
990 kgid_t gid;
991 int rc;
992
993 rc = get_option_ul(args, &value);
994 if (rc)
995 return rc;
996
997 gid = make_kgid(current_user_ns(), value);
998 if (!gid_valid(gid))
999 return -EINVAL;
1000
1001 *result = gid;
1002 return 0;
1003}
8830d7e0
SP
1004
1005static int cifs_parse_security_flavors(char *value,
1006 struct smb_vol *vol)
1007{
1008
1009 substring_t args[MAX_OPT_ARGS];
1010
1e3cc57e
JL
1011 /*
1012 * With mount options, the last one should win. Reset any existing
1013 * settings back to default.
1014 */
1015 vol->sectype = Unspecified;
1016 vol->sign = false;
1017
8830d7e0 1018 switch (match_token(value, cifs_secflavor_tokens, args)) {
3f618223
JL
1019 case Opt_sec_krb5p:
1020 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1021 return 1;
1022 case Opt_sec_krb5i:
1023 vol->sign = true;
1024 /* Fallthrough */
8830d7e0 1025 case Opt_sec_krb5:
1e3cc57e 1026 vol->sectype = Kerberos;
8830d7e0 1027 break;
3f618223 1028 case Opt_sec_ntlmsspi:
1e3cc57e 1029 vol->sign = true;
3f618223 1030 /* Fallthrough */
8830d7e0 1031 case Opt_sec_ntlmssp:
1e3cc57e 1032 vol->sectype = RawNTLMSSP;
8830d7e0 1033 break;
3f618223 1034 case Opt_sec_ntlmi:
1e3cc57e 1035 vol->sign = true;
3f618223 1036 /* Fallthrough */
8830d7e0 1037 case Opt_ntlm:
1e3cc57e 1038 vol->sectype = NTLM;
8830d7e0 1039 break;
3f618223 1040 case Opt_sec_ntlmv2i:
1e3cc57e 1041 vol->sign = true;
3f618223 1042 /* Fallthrough */
7659624f 1043 case Opt_sec_ntlmv2:
1e3cc57e 1044 vol->sectype = NTLMv2;
8830d7e0
SP
1045 break;
1046#ifdef CONFIG_CIFS_WEAK_PW_HASH
1047 case Opt_sec_lanman:
1e3cc57e 1048 vol->sectype = LANMAN;
8830d7e0
SP
1049 break;
1050#endif
1051 case Opt_sec_none:
1052 vol->nullauth = 1;
1053 break;
1054 default:
f96637be 1055 cifs_dbg(VFS, "bad security option: %s\n", value);
8830d7e0
SP
1056 return 1;
1057 }
1058
1059 return 0;
1060}
1061
15b6a473
JL
1062static int
1063cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1064{
1065 substring_t args[MAX_OPT_ARGS];
1066
1067 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1068 case Opt_cache_loose:
1069 vol->direct_io = false;
1070 vol->strict_io = false;
1071 break;
1072 case Opt_cache_strict:
1073 vol->direct_io = false;
1074 vol->strict_io = true;
1075 break;
1076 case Opt_cache_none:
1077 vol->direct_io = true;
1078 vol->strict_io = false;
1079 break;
1080 default:
f96637be 1081 cifs_dbg(VFS, "bad cache= option: %s\n", value);
15b6a473
JL
1082 return 1;
1083 }
1084 return 0;
1085}
1086
23db65f5
JL
1087static int
1088cifs_parse_smb_version(char *value, struct smb_vol *vol)
1089{
1090 substring_t args[MAX_OPT_ARGS];
1091
1092 switch (match_token(value, cifs_smb_version_tokens, args)) {
1093 case Smb_1:
1094 vol->ops = &smb1_operations;
1095 vol->vals = &smb1_values;
1096 break;
1080ef75 1097#ifdef CONFIG_CIFS_SMB2
dd446b16 1098 case Smb_20:
53ef1016 1099 vol->ops = &smb20_operations;
dd446b16
SF
1100 vol->vals = &smb20_values;
1101 break;
1080ef75
SF
1102 case Smb_21:
1103 vol->ops = &smb21_operations;
1104 vol->vals = &smb21_values;
1105 break;
e4aa25e7 1106 case Smb_30:
38107d45 1107 vol->ops = &smb30_operations;
e4aa25e7
SF
1108 vol->vals = &smb30_values;
1109 break;
20b6d8b4
SF
1110 case Smb_302:
1111 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1112 vol->vals = &smb302_values;
1113 break;
5f7fbf73
SF
1114#ifdef CONFIG_CIFS_SMB311
1115 case Smb_311:
aab1893d 1116 vol->ops = &smb311_operations;
5f7fbf73
SF
1117 vol->vals = &smb311_values;
1118 break;
1119#endif /* SMB311 */
1080ef75 1120#endif
23db65f5 1121 default:
f96637be 1122 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
23db65f5
JL
1123 return 1;
1124 }
1125 return 0;
1126}
1127
d387a5c5
JL
1128/*
1129 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1130 * fields with the result. Returns 0 on success and an error otherwise.
1131 */
1132static int
1133cifs_parse_devname(const char *devname, struct smb_vol *vol)
1134{
1135 char *pos;
1136 const char *delims = "/\\";
1137 size_t len;
1138
1139 /* make sure we have a valid UNC double delimiter prefix */
1140 len = strspn(devname, delims);
1141 if (len != 2)
1142 return -EINVAL;
1143
1144 /* find delimiter between host and sharename */
1145 pos = strpbrk(devname + 2, delims);
1146 if (!pos)
1147 return -EINVAL;
1148
1149 /* skip past delimiter */
1150 ++pos;
1151
1152 /* now go until next delimiter or end of string */
1153 len = strcspn(pos, delims);
1154
1155 /* move "pos" up to delimiter or NULL */
1156 pos += len;
1157 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1158 if (!vol->UNC)
1159 return -ENOMEM;
1160
1161 convert_delimiter(vol->UNC, '\\');
1162
11e31647
SP
1163 /* skip any delimiter */
1164 if (*pos == '/' || *pos == '\\')
1165 pos++;
1166
1167 /* If pos is NULL then no prepath */
1168 if (!*pos)
d387a5c5
JL
1169 return 0;
1170
1171 vol->prepath = kstrdup(pos, GFP_KERNEL);
1172 if (!vol->prepath)
1173 return -ENOMEM;
1174
1175 return 0;
1176}
1177
1da177e4 1178static int
b946845a 1179cifs_parse_mount_options(const char *mountdata, const char *devname,
50c2f753 1180 struct smb_vol *vol)
1da177e4 1181{
8830d7e0 1182 char *data, *end;
957df453 1183 char *mountdata_copy = NULL, *options;
1da177e4
LT
1184 unsigned int temp_len, i, j;
1185 char separator[2];
9b9d6b24
JL
1186 short int override_uid = -1;
1187 short int override_gid = -1;
1188 bool uid_specified = false;
1189 bool gid_specified = false;
d8162558
JL
1190 bool sloppy = false;
1191 char *invalid = NULL;
88463999 1192 char *nodename = utsname()->nodename;
8830d7e0
SP
1193 char *string = NULL;
1194 char *tmp_end, *value;
1195 char delim;
b979aaa1
JL
1196 bool got_ip = false;
1197 unsigned short port = 0;
1198 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1da177e4
LT
1199
1200 separator[0] = ',';
50c2f753 1201 separator[1] = 0;
8830d7e0 1202 delim = separator[0];
1da177e4 1203
6ee9542a
JL
1204 /* ensure we always start with zeroed-out smb_vol */
1205 memset(vol, 0, sizeof(*vol));
1206
88463999
JL
1207 /*
1208 * does not have to be perfect mapping since field is
1209 * informational, only used for servers that do not support
1210 * port 445 and it can be overridden at mount time
1211 */
1397f2ee
JL
1212 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1213 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
88463999
JL
1214 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1215
1397f2ee 1216 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
a10faeb2
SF
1217 /* null target name indicates to use *SMBSERVR default called name
1218 if we end up sending RFC1001 session initialize */
1219 vol->target_rfc1001_name[0] = 0;
3e4b3e1f
JL
1220 vol->cred_uid = current_uid();
1221 vol->linux_uid = current_uid();
a001e5b5 1222 vol->linux_gid = current_gid();
f55ed1a8 1223
2baa2682
SF
1224 /*
1225 * default to SFM style remapping of seven reserved characters
1226 * unless user overrides it or we negotiate CIFS POSIX where
1227 * it is unnecessary. Can not simultaneously use more than one mapping
1228 * since then readdir could list files that open could not open
1229 */
1230 vol->remap = true;
1231
f55ed1a8
JL
1232 /* default to only allowing write access to owner of the mount */
1233 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1da177e4
LT
1234
1235 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
ac67055e
JA
1236 /* default is always to request posix paths. */
1237 vol->posix_paths = 1;
a0c9217f
JL
1238 /* default to using server inode numbers where available */
1239 vol->server_ino = 1;
ac67055e 1240
1b359204
JL
1241 /* default is to use strict cifs caching semantics */
1242 vol->strict_io = true;
1243
6d20e840
SJ
1244 vol->actimeo = CIFS_DEF_ACTIMEO;
1245
23db65f5
JL
1246 /* FIXME: add autonegotiation -- for now, SMB1 is default */
1247 vol->ops = &smb1_operations;
1248 vol->vals = &smb1_values;
1249
b782fcc1
RV
1250 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1251
b946845a
SF
1252 if (!mountdata)
1253 goto cifs_parse_mount_err;
1254
1255 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1256 if (!mountdata_copy)
1257 goto cifs_parse_mount_err;
1da177e4 1258
b946845a 1259 options = mountdata_copy;
4906e50b 1260 end = options + strlen(options);
8830d7e0 1261
50c2f753 1262 if (strncmp(options, "sep=", 4) == 0) {
fb8c4b14 1263 if (options[4] != 0) {
1da177e4
LT
1264 separator[0] = options[4];
1265 options += 5;
1266 } else {
f96637be 1267 cifs_dbg(FYI, "Null separator not allowed\n");
1da177e4
LT
1268 }
1269 }
3d3ea8e6
SP
1270 vol->backupuid_specified = false; /* no backup intent for a user */
1271 vol->backupgid_specified = false; /* no backup intent for a group */
50c2f753 1272
37d4f99b
JL
1273 switch (cifs_parse_devname(devname, vol)) {
1274 case 0:
1275 break;
1276 case -ENOMEM:
1277 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1278 goto cifs_parse_mount_err;
1279 case -EINVAL:
1280 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1281 goto cifs_parse_mount_err;
1282 default:
1283 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1284 goto cifs_parse_mount_err;
d387a5c5
JL
1285 }
1286
1da177e4 1287 while ((data = strsep(&options, separator)) != NULL) {
8830d7e0
SP
1288 substring_t args[MAX_OPT_ARGS];
1289 unsigned long option;
1290 int token;
1291
1da177e4
LT
1292 if (!*data)
1293 continue;
1da177e4 1294
8830d7e0
SP
1295 token = match_token(data, cifs_mount_option_tokens, args);
1296
1297 switch (token) {
1298
1299 /* Ingnore the following */
1300 case Opt_ignore:
1301 break;
1302
1303 /* Boolean values */
1304 case Opt_user_xattr:
1da177e4 1305 vol->no_xattr = 0;
8830d7e0
SP
1306 break;
1307 case Opt_nouser_xattr:
1da177e4 1308 vol->no_xattr = 1;
8830d7e0
SP
1309 break;
1310 case Opt_forceuid:
9b9d6b24 1311 override_uid = 1;
8830d7e0
SP
1312 break;
1313 case Opt_noforceuid:
9b9d6b24 1314 override_uid = 0;
8830d7e0 1315 break;
72bd481f
JL
1316 case Opt_forcegid:
1317 override_gid = 1;
1318 break;
1319 case Opt_noforcegid:
1320 override_gid = 0;
1321 break;
8830d7e0 1322 case Opt_noblocksend:
edf1ae40 1323 vol->noblocksnd = 1;
8830d7e0
SP
1324 break;
1325 case Opt_noautotune:
edf1ae40 1326 vol->noautotune = 1;
8830d7e0
SP
1327 break;
1328 case Opt_hard:
1da177e4 1329 vol->retry = 1;
8830d7e0
SP
1330 break;
1331 case Opt_soft:
1da177e4 1332 vol->retry = 0;
8830d7e0
SP
1333 break;
1334 case Opt_perm:
1da177e4 1335 vol->noperm = 0;
8830d7e0
SP
1336 break;
1337 case Opt_noperm:
1da177e4 1338 vol->noperm = 1;
8830d7e0
SP
1339 break;
1340 case Opt_mapchars:
2baa2682
SF
1341 vol->sfu_remap = true;
1342 vol->remap = false; /* disable SFM mapping */
8830d7e0
SP
1343 break;
1344 case Opt_nomapchars:
2baa2682
SF
1345 vol->sfu_remap = false;
1346 break;
1347 case Opt_mapposix:
1348 vol->remap = true;
1349 vol->sfu_remap = false; /* disable SFU mapping */
1350 break;
1351 case Opt_nomapposix:
1352 vol->remap = false;
8830d7e0
SP
1353 break;
1354 case Opt_sfu:
50c2f753 1355 vol->sfu_emul = 1;
8830d7e0
SP
1356 break;
1357 case Opt_nosfu:
50c2f753 1358 vol->sfu_emul = 0;
8830d7e0
SP
1359 break;
1360 case Opt_nodfs:
2c1b8615 1361 vol->nodfs = 1;
8830d7e0
SP
1362 break;
1363 case Opt_posixpaths:
ac67055e 1364 vol->posix_paths = 1;
8830d7e0
SP
1365 break;
1366 case Opt_noposixpaths:
ac67055e 1367 vol->posix_paths = 0;
8830d7e0
SP
1368 break;
1369 case Opt_nounix:
c18c842b 1370 vol->no_linux_ext = 1;
8830d7e0
SP
1371 break;
1372 case Opt_nocase:
50c2f753 1373 vol->nocase = 1;
8830d7e0
SP
1374 break;
1375 case Opt_brl:
c46fa8ac 1376 vol->nobrl = 0;
8830d7e0
SP
1377 break;
1378 case Opt_nobrl:
c46fa8ac 1379 vol->nobrl = 1;
5cfdddcf
PS
1380 /*
1381 * turn off mandatory locking in mode
8830d7e0 1382 * if remote locking is turned off since the
5cfdddcf
PS
1383 * local vfs will do advisory
1384 */
50c2f753
SF
1385 if (vol->file_mode ==
1386 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
d3485d37 1387 vol->file_mode = S_IALLUGO;
8830d7e0
SP
1388 break;
1389 case Opt_forcemandatorylock:
13a6e42a 1390 vol->mand_lock = 1;
8830d7e0
SP
1391 break;
1392 case Opt_setuids:
1da177e4 1393 vol->setuids = 1;
8830d7e0
SP
1394 break;
1395 case Opt_nosetuids:
1da177e4 1396 vol->setuids = 0;
8830d7e0 1397 break;
95932655
SF
1398 case Opt_setuidfromacl:
1399 vol->setuidfromacl = 1;
1400 break;
8830d7e0 1401 case Opt_dynperm:
d0a9c078 1402 vol->dynperm = true;
8830d7e0
SP
1403 break;
1404 case Opt_nodynperm:
d0a9c078 1405 vol->dynperm = false;
8830d7e0
SP
1406 break;
1407 case Opt_nohard:
1da177e4 1408 vol->retry = 0;
8830d7e0
SP
1409 break;
1410 case Opt_nosoft:
1da177e4 1411 vol->retry = 1;
8830d7e0
SP
1412 break;
1413 case Opt_nointr:
1da177e4 1414 vol->intr = 0;
8830d7e0
SP
1415 break;
1416 case Opt_intr:
1da177e4 1417 vol->intr = 1;
8830d7e0
SP
1418 break;
1419 case Opt_nostrictsync:
be652445 1420 vol->nostrictsync = 1;
8830d7e0
SP
1421 break;
1422 case Opt_strictsync:
be652445 1423 vol->nostrictsync = 0;
8830d7e0
SP
1424 break;
1425 case Opt_serverino:
1da177e4 1426 vol->server_ino = 1;
8830d7e0
SP
1427 break;
1428 case Opt_noserverino:
1da177e4 1429 vol->server_ino = 0;
8830d7e0
SP
1430 break;
1431 case Opt_rwpidforward:
d4ffff1f 1432 vol->rwpidforward = 1;
8830d7e0
SP
1433 break;
1434 case Opt_cifsacl:
0a4b92c0 1435 vol->cifs_acl = 1;
8830d7e0
SP
1436 break;
1437 case Opt_nocifsacl:
0a4b92c0 1438 vol->cifs_acl = 0;
8830d7e0
SP
1439 break;
1440 case Opt_acl:
1da177e4 1441 vol->no_psx_acl = 0;
8830d7e0
SP
1442 break;
1443 case Opt_noacl:
1da177e4 1444 vol->no_psx_acl = 1;
8830d7e0
SP
1445 break;
1446 case Opt_locallease:
84210e91 1447 vol->local_lease = 1;
8830d7e0
SP
1448 break;
1449 case Opt_sign:
1e3cc57e 1450 vol->sign = true;
8830d7e0
SP
1451 break;
1452 case Opt_seal:
95b1cb90 1453 /* we do not do the following in secFlags because seal
8830d7e0
SP
1454 * is a per tree connection (mount) not a per socket
1455 * or per-smb connection option in the protocol
1456 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1457 */
95b1cb90 1458 vol->seal = 1;
8830d7e0 1459 break;
8830d7e0 1460 case Opt_noac:
0b456f04 1461 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
8830d7e0
SP
1462 break;
1463 case Opt_fsc:
607a569d 1464#ifndef CONFIG_CIFS_FSCACHE
f96637be 1465 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
b946845a 1466 goto cifs_parse_mount_err;
607a569d 1467#endif
fa1df75d 1468 vol->fsc = true;
8830d7e0
SP
1469 break;
1470 case Opt_mfsymlinks:
736a3320 1471 vol->mfsymlinks = true;
8830d7e0
SP
1472 break;
1473 case Opt_multiuser:
0eb8a132 1474 vol->multiuser = true;
8830d7e0 1475 break;
d8162558
JL
1476 case Opt_sloppy:
1477 sloppy = true;
1478 break;
a0b3df5c
JL
1479 case Opt_nosharesock:
1480 vol->nosharesock = true;
1481 break;
b2a30774
SF
1482 case Opt_nopersistent:
1483 vol->nopersistent = true;
1484 if (vol->persistent) {
1485 cifs_dbg(VFS,
1486 "persistenthandles mount options conflict\n");
1487 goto cifs_parse_mount_err;
1488 }
1489 break;
1490 case Opt_persistent:
1491 vol->persistent = true;
592fafe6 1492 if ((vol->nopersistent) || (vol->resilient)) {
b2a30774
SF
1493 cifs_dbg(VFS,
1494 "persistenthandles mount options conflict\n");
1495 goto cifs_parse_mount_err;
1496 }
1497 break;
592fafe6
SF
1498 case Opt_resilient:
1499 vol->resilient = true;
1500 if (vol->persistent) {
1501 cifs_dbg(VFS,
1502 "persistenthandles mount options conflict\n");
1503 goto cifs_parse_mount_err;
1504 }
1505 break;
1506 case Opt_noresilient:
1507 vol->resilient = false; /* already the default */
1508 break;
39566443
GP
1509 case Opt_domainauto:
1510 vol->domainauto = true;
1511 break;
8830d7e0
SP
1512
1513 /* Numeric Values */
1514 case Opt_backupuid:
3da46565 1515 if (get_option_uid(args, &vol->backupuid)) {
f96637be
JP
1516 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1517 __func__);
3d3ea8e6
SP
1518 goto cifs_parse_mount_err;
1519 }
1520 vol->backupuid_specified = true;
8830d7e0
SP
1521 break;
1522 case Opt_backupgid:
3da46565 1523 if (get_option_gid(args, &vol->backupgid)) {
f96637be
JP
1524 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1525 __func__);
3d3ea8e6
SP
1526 goto cifs_parse_mount_err;
1527 }
1528 vol->backupgid_specified = true;
8830d7e0
SP
1529 break;
1530 case Opt_uid:
3da46565 1531 if (get_option_uid(args, &vol->linux_uid)) {
f96637be
JP
1532 cifs_dbg(VFS, "%s: Invalid uid value\n",
1533 __func__);
8830d7e0
SP
1534 goto cifs_parse_mount_err;
1535 }
8830d7e0
SP
1536 uid_specified = true;
1537 break;
1538 case Opt_cruid:
3da46565 1539 if (get_option_uid(args, &vol->cred_uid)) {
f96637be
JP
1540 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1541 __func__);
8830d7e0
SP
1542 goto cifs_parse_mount_err;
1543 }
8830d7e0
SP
1544 break;
1545 case Opt_gid:
3da46565 1546 if (get_option_gid(args, &vol->linux_gid)) {
f96637be
JP
1547 cifs_dbg(VFS, "%s: Invalid gid value\n",
1548 __func__);
8830d7e0
SP
1549 goto cifs_parse_mount_err;
1550 }
8830d7e0
SP
1551 gid_specified = true;
1552 break;
1553 case Opt_file_mode:
1554 if (get_option_ul(args, &option)) {
f96637be
JP
1555 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1556 __func__);
8830d7e0
SP
1557 goto cifs_parse_mount_err;
1558 }
1559 vol->file_mode = option;
1560 break;
1561 case Opt_dirmode:
1562 if (get_option_ul(args, &option)) {
f96637be
JP
1563 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1564 __func__);
8830d7e0
SP
1565 goto cifs_parse_mount_err;
1566 }
1567 vol->dir_mode = option;
1568 break;
1569 case Opt_port:
b979aaa1
JL
1570 if (get_option_ul(args, &option) ||
1571 option > USHRT_MAX) {
f96637be
JP
1572 cifs_dbg(VFS, "%s: Invalid port value\n",
1573 __func__);
8830d7e0
SP
1574 goto cifs_parse_mount_err;
1575 }
b979aaa1 1576 port = (unsigned short)option;
8830d7e0
SP
1577 break;
1578 case Opt_rsize:
1579 if (get_option_ul(args, &option)) {
f96637be
JP
1580 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1581 __func__);
b946845a 1582 goto cifs_parse_mount_err;
8830d7e0
SP
1583 }
1584 vol->rsize = option;
1585 break;
1586 case Opt_wsize:
1587 if (get_option_ul(args, &option)) {
f96637be
JP
1588 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1589 __func__);
8830d7e0
SP
1590 goto cifs_parse_mount_err;
1591 }
1592 vol->wsize = option;
1593 break;
1594 case Opt_actimeo:
1595 if (get_option_ul(args, &option)) {
f96637be
JP
1596 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1597 __func__);
8830d7e0
SP
1598 goto cifs_parse_mount_err;
1599 }
1600 vol->actimeo = HZ * option;
1601 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
f96637be 1602 cifs_dbg(VFS, "attribute cache timeout too large\n");
8830d7e0
SP
1603 goto cifs_parse_mount_err;
1604 }
1605 break;
adfeb3e0
SF
1606 case Opt_echo_interval:
1607 if (get_option_ul(args, &option)) {
1608 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1609 __func__);
1610 goto cifs_parse_mount_err;
1611 }
1612 vol->echo_interval = option;
1613 break;
8b217fe7
SF
1614 case Opt_snapshot:
1615 if (get_option_ul(args, &option)) {
1616 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1617 __func__);
1618 goto cifs_parse_mount_err;
1619 }
1620 vol->snapshot_time = option;
1621 break;
141891f4
SF
1622 case Opt_max_credits:
1623 if (get_option_ul(args, &option) || (option < 20) ||
1624 (option > 60000)) {
1625 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1626 __func__);
1627 goto cifs_parse_mount_err;
1628 }
1629 vol->max_credits = option;
1630 break;
8830d7e0
SP
1631
1632 /* String Arguments */
1633
4fe9e963
SP
1634 case Opt_blank_user:
1635 /* null user, ie. anonymous authentication */
1636 vol->nullauth = 1;
1637 vol->username = NULL;
1638 break;
8830d7e0
SP
1639 case Opt_user:
1640 string = match_strdup(args);
1641 if (string == NULL)
1642 goto out_nomem;
1643
8c3a2b4c
SL
1644 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1645 CIFS_MAX_USERNAME_LEN) {
0b456f04 1646 pr_warn("CIFS: username too long\n");
8830d7e0
SP
1647 goto cifs_parse_mount_err;
1648 }
2bd50fb3
TK
1649
1650 kfree(vol->username);
8830d7e0 1651 vol->username = kstrdup(string, GFP_KERNEL);
f96637be 1652 if (!vol->username)
8830d7e0 1653 goto cifs_parse_mount_err;
8830d7e0
SP
1654 break;
1655 case Opt_blank_pass:
8830d7e0
SP
1656 /* passwords have to be handled differently
1657 * to allow the character used for deliminator
1658 * to be passed within them
1659 */
1660
c369c9a4
SP
1661 /*
1662 * Check if this is a case where the password
1663 * starts with a delimiter
1664 */
1665 tmp_end = strchr(data, '=');
1666 tmp_end++;
1667 if (!(tmp_end < end && tmp_end[1] == delim)) {
1668 /* No it is not. Set the password to NULL */
d6ccf499 1669 kfree(vol->password);
c369c9a4
SP
1670 vol->password = NULL;
1671 break;
1672 }
1673 /* Yes it is. Drop down to Opt_pass below.*/
1674 case Opt_pass:
8830d7e0
SP
1675 /* Obtain the value string */
1676 value = strchr(data, '=');
10238074 1677 value++;
8830d7e0
SP
1678
1679 /* Set tmp_end to end of the string */
1680 tmp_end = (char *) value + strlen(value);
1681
1682 /* Check if following character is the deliminator
1683 * If yes, we have encountered a double deliminator
1684 * reset the NULL character to the deliminator
1685 */
e73f843a 1686 if (tmp_end < end && tmp_end[1] == delim) {
8830d7e0
SP
1687 tmp_end[0] = delim;
1688
e73f843a
SJ
1689 /* Keep iterating until we get to a single
1690 * deliminator OR the end
1691 */
1692 while ((tmp_end = strchr(tmp_end, delim))
1693 != NULL && (tmp_end[1] == delim)) {
1694 tmp_end = (char *) &tmp_end[2];
1695 }
1696
1697 /* Reset var options to point to next element */
1698 if (tmp_end) {
1699 tmp_end[0] = '\0';
1700 options = (char *) &tmp_end[1];
1701 } else
1702 /* Reached the end of the mount option
1703 * string */
1704 options = end;
8830d7e0
SP
1705 }
1706
d6ccf499 1707 kfree(vol->password);
8830d7e0
SP
1708 /* Now build new password string */
1709 temp_len = strlen(value);
1710 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1711 if (vol->password == NULL) {
0b456f04 1712 pr_warn("CIFS: no memory for password\n");
8830d7e0
SP
1713 goto cifs_parse_mount_err;
1714 }
1715
1716 for (i = 0, j = 0; i < temp_len; i++, j++) {
1717 vol->password[j] = value[i];
1718 if ((value[i] == delim) &&
1719 value[i+1] == delim)
1720 /* skip the second deliminator */
1721 i++;
1722 }
1723 vol->password[j] = '\0';
1724 break;
4fe9e963 1725 case Opt_blank_ip:
b979aaa1
JL
1726 /* FIXME: should this be an error instead? */
1727 got_ip = false;
4fe9e963 1728 break;
8830d7e0
SP
1729 case Opt_ip:
1730 string = match_strdup(args);
1731 if (string == NULL)
1732 goto out_nomem;
1733
b979aaa1
JL
1734 if (!cifs_convert_address(dstaddr, string,
1735 strlen(string))) {
0b456f04 1736 pr_err("CIFS: bad ip= option (%s).\n", string);
8830d7e0
SP
1737 goto cifs_parse_mount_err;
1738 }
b979aaa1 1739 got_ip = true;
8830d7e0 1740 break;
8830d7e0
SP
1741 case Opt_domain:
1742 string = match_strdup(args);
1743 if (string == NULL)
1744 goto out_nomem;
1745
057d6332
CG
1746 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1747 == CIFS_MAX_DOMAINNAME_LEN) {
0b456f04 1748 pr_warn("CIFS: domain name too long\n");
8830d7e0
SP
1749 goto cifs_parse_mount_err;
1750 }
1751
2bd50fb3 1752 kfree(vol->domainname);
8830d7e0
SP
1753 vol->domainname = kstrdup(string, GFP_KERNEL);
1754 if (!vol->domainname) {
0b456f04 1755 pr_warn("CIFS: no memory for domainname\n");
8830d7e0
SP
1756 goto cifs_parse_mount_err;
1757 }
f96637be 1758 cifs_dbg(FYI, "Domain name set\n");
8830d7e0
SP
1759 break;
1760 case Opt_srcaddr:
1761 string = match_strdup(args);
1762 if (string == NULL)
1763 goto out_nomem;
1764
4fe9e963 1765 if (!cifs_convert_address(
8830d7e0
SP
1766 (struct sockaddr *)&vol->srcaddr,
1767 string, strlen(string))) {
0b456f04
AS
1768 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1769 string);
8830d7e0
SP
1770 goto cifs_parse_mount_err;
1771 }
1772 break;
8830d7e0
SP
1773 case Opt_iocharset:
1774 string = match_strdup(args);
1775 if (string == NULL)
1776 goto out_nomem;
1777
4fe9e963 1778 if (strnlen(string, 1024) >= 65) {
0b456f04 1779 pr_warn("CIFS: iocharset name too long.\n");
8830d7e0
SP
1780 goto cifs_parse_mount_err;
1781 }
1782
87e747cd 1783 if (strncasecmp(string, "default", 7) != 0) {
2bd50fb3 1784 kfree(vol->iocharset);
8830d7e0
SP
1785 vol->iocharset = kstrdup(string,
1786 GFP_KERNEL);
1787 if (!vol->iocharset) {
0b456f04 1788 pr_warn("CIFS: no memory for charset\n");
8830d7e0
SP
1789 goto cifs_parse_mount_err;
1790 }
1791 }
1792 /* if iocharset not set then load_nls_default
1793 * is used by caller
1794 */
f96637be 1795 cifs_dbg(FYI, "iocharset set to %s\n", string);
8830d7e0 1796 break;
8830d7e0
SP
1797 case Opt_netbiosname:
1798 string = match_strdup(args);
1799 if (string == NULL)
1800 goto out_nomem;
1801
8830d7e0
SP
1802 memset(vol->source_rfc1001_name, 0x20,
1803 RFC1001_NAME_LEN);
1804 /*
1805 * FIXME: are there cases in which a comma can
1806 * be valid in workstation netbios name (and
1807 * need special handling)?
1808 */
1809 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1810 /* don't ucase netbiosname for user */
1811 if (string[i] == 0)
1812 break;
1813 vol->source_rfc1001_name[i] = string[i];
1814 }
1815 /* The string has 16th byte zero still from
1816 * set at top of the function
1817 */
1818 if (i == RFC1001_NAME_LEN && string[i] != 0)
0b456f04 1819 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
8830d7e0
SP
1820 break;
1821 case Opt_servern:
1822 /* servernetbiosname specified override *SMBSERVER */
1823 string = match_strdup(args);
1824 if (string == NULL)
1825 goto out_nomem;
1826
8830d7e0
SP
1827 /* last byte, type, is 0x20 for servr type */
1828 memset(vol->target_rfc1001_name, 0x20,
1829 RFC1001_NAME_LEN_WITH_NULL);
1830
1831 /* BB are there cases in which a comma can be
1832 valid in this workstation netbios name
1833 (and need special handling)? */
1834
1835 /* user or mount helper must uppercase the
1836 netbios name */
1837 for (i = 0; i < 15; i++) {
1838 if (string[i] == 0)
1839 break;
1840 vol->target_rfc1001_name[i] = string[i];
1841 }
1842 /* The string has 16th byte zero still from
1843 set at top of the function */
1844 if (i == RFC1001_NAME_LEN && string[i] != 0)
0b456f04 1845 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
8830d7e0
SP
1846 break;
1847 case Opt_ver:
1848 string = match_strdup(args);
1849 if (string == NULL)
1850 goto out_nomem;
1851
87e747cd 1852 if (strncasecmp(string, "1", 1) == 0) {
8830d7e0
SP
1853 /* This is the default */
1854 break;
1855 }
1856 /* For all other value, error */
0b456f04 1857 pr_warn("CIFS: Invalid version specified\n");
b946845a 1858 goto cifs_parse_mount_err;
23db65f5
JL
1859 case Opt_vers:
1860 string = match_strdup(args);
1861 if (string == NULL)
1862 goto out_nomem;
1863
1864 if (cifs_parse_smb_version(string, vol) != 0)
1865 goto cifs_parse_mount_err;
1866 break;
8830d7e0
SP
1867 case Opt_sec:
1868 string = match_strdup(args);
1869 if (string == NULL)
1870 goto out_nomem;
1871
8830d7e0
SP
1872 if (cifs_parse_security_flavors(string, vol) != 0)
1873 goto cifs_parse_mount_err;
1874 break;
15b6a473
JL
1875 case Opt_cache:
1876 string = match_strdup(args);
1877 if (string == NULL)
1878 goto out_nomem;
1879
1880 if (cifs_parse_cache_flavor(string, vol) != 0)
1881 goto cifs_parse_mount_err;
1882 break;
8830d7e0 1883 default:
d8162558
JL
1884 /*
1885 * An option we don't recognize. Save it off for later
1886 * if we haven't already found one
1887 */
1888 if (!invalid)
1889 invalid = data;
8830d7e0 1890 break;
1da177e4 1891 }
8830d7e0
SP
1892 /* Free up any allocated string */
1893 kfree(string);
1894 string = NULL;
1da177e4 1895 }
0eb8a132 1896
d8162558 1897 if (!sloppy && invalid) {
0b456f04 1898 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
d8162558
JL
1899 goto cifs_parse_mount_err;
1900 }
1901
8a8798a5
JL
1902#ifndef CONFIG_KEYS
1903 /* Muliuser mounts require CONFIG_KEYS support */
1904 if (vol->multiuser) {
f96637be 1905 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
b946845a 1906 goto cifs_parse_mount_err;
0eb8a132 1907 }
8a8798a5 1908#endif
e5e69abd 1909 if (!vol->UNC) {
37d4f99b 1910 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
e5e69abd
JL
1911 goto cifs_parse_mount_err;
1912 }
0eb8a132 1913
62a1a439
JL
1914 /* make sure UNC has a share name */
1915 if (!strchr(vol->UNC + 3, '\\')) {
f96637be 1916 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
62a1a439
JL
1917 goto cifs_parse_mount_err;
1918 }
1919
b979aaa1
JL
1920 if (!got_ip) {
1921 /* No ip= option specified? Try to get it from UNC */
1922 if (!cifs_convert_address(dstaddr, &vol->UNC[2],
1923 strlen(&vol->UNC[2]))) {
0b456f04 1924 pr_err("Unable to determine destination address.\n");
b979aaa1
JL
1925 goto cifs_parse_mount_err;
1926 }
1927 }
1928
1929 /* set the port that we got earlier */
1930 cifs_set_port(dstaddr, port);
1da177e4 1931
9b9d6b24
JL
1932 if (uid_specified)
1933 vol->override_uid = override_uid;
1934 else if (override_uid == 1)
0b456f04 1935 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
9b9d6b24
JL
1936
1937 if (gid_specified)
1938 vol->override_gid = override_gid;
1939 else if (override_gid == 1)
0b456f04 1940 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
9b9d6b24 1941
b946845a 1942 kfree(mountdata_copy);
1da177e4 1943 return 0;
b946845a 1944
8830d7e0 1945out_nomem:
0b456f04 1946 pr_warn("Could not allocate temporary buffer\n");
b946845a 1947cifs_parse_mount_err:
8830d7e0 1948 kfree(string);
b946845a
SF
1949 kfree(mountdata_copy);
1950 return 1;
1da177e4
LT
1951}
1952
3eb9a889
BG
1953/** Returns true if srcaddr isn't specified and rhs isn't
1954 * specified, or if srcaddr is specified and
1955 * matches the IP address of the rhs argument.
1956 */
1957static bool
1958srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
1959{
1960 switch (srcaddr->sa_family) {
1961 case AF_UNSPEC:
1962 return (rhs->sa_family == AF_UNSPEC);
1963 case AF_INET: {
1964 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1965 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1966 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1967 }
1968 case AF_INET6: {
1969 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
e3e2775c 1970 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
3eb9a889
BG
1971 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1972 }
1973 default:
1974 WARN_ON(1);
1975 return false; /* don't expect to be here */
1976 }
1977}
1978
4b886136
PS
1979/*
1980 * If no port is specified in addr structure, we try to match with 445 port
1981 * and if it fails - with 139 ports. It should be called only if address
1982 * families of server and addr are equal.
1983 */
1984static bool
1985match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1986{
6da97910 1987 __be16 port, *sport;
4b886136
PS
1988
1989 switch (addr->sa_family) {
1990 case AF_INET:
1991 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1992 port = ((struct sockaddr_in *) addr)->sin_port;
1993 break;
1994 case AF_INET6:
1995 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1996 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1997 break;
1998 default:
1999 WARN_ON(1);
2000 return false;
2001 }
2002
2003 if (!port) {
2004 port = htons(CIFS_PORT);
2005 if (port == *sport)
2006 return true;
2007
2008 port = htons(RFC1001_PORT);
2009 }
2010
2011 return port == *sport;
2012}
3eb9a889 2013
4515148e 2014static bool
3eb9a889
BG
2015match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2016 struct sockaddr *srcaddr)
4515148e 2017{
4515148e 2018 switch (addr->sa_family) {
a9f1b85e
PS
2019 case AF_INET: {
2020 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2021 struct sockaddr_in *srv_addr4 =
2022 (struct sockaddr_in *)&server->dstaddr;
2023
2024 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
4515148e 2025 return false;
4515148e 2026 break;
a9f1b85e
PS
2027 }
2028 case AF_INET6: {
2029 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2030 struct sockaddr_in6 *srv_addr6 =
2031 (struct sockaddr_in6 *)&server->dstaddr;
2032
4515148e 2033 if (!ipv6_addr_equal(&addr6->sin6_addr,
a9f1b85e 2034 &srv_addr6->sin6_addr))
4515148e 2035 return false;
a9f1b85e 2036 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
4515148e 2037 return false;
4515148e
JL
2038 break;
2039 }
a9f1b85e
PS
2040 default:
2041 WARN_ON(1);
2042 return false; /* don't expect to be here */
2043 }
4515148e 2044
3eb9a889
BG
2045 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2046 return false;
2047
4515148e
JL
2048 return true;
2049}
2050
daf5b0b6
JL
2051static bool
2052match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2053{
3f618223
JL
2054 /*
2055 * The select_sectype function should either return the vol->sectype
2056 * that was specified, or "Unspecified" if that sectype was not
2057 * compatible with the given NEGOTIATE request.
2058 */
2059 if (select_sectype(server, vol->sectype) == Unspecified)
daf5b0b6 2060 return false;
daf5b0b6 2061
3f618223
JL
2062 /*
2063 * Now check if signing mode is acceptable. No need to check
2064 * global_secflags at this point since if MUST_SIGN is set then
2065 * the server->sign had better be too.
2066 */
38d77c50
JL
2067 if (vol->sign && !server->sign)
2068 return false;
daf5b0b6
JL
2069
2070 return true;
2071}
2072
9fa114f7 2073static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
37bb04e5 2074{
9fa114f7
JL
2075 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2076
a0b3df5c
JL
2077 if (vol->nosharesock)
2078 return 0;
2079
23db65f5
JL
2080 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2081 return 0;
2082
37bb04e5
PS
2083 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2084 return 0;
2085
2086 if (!match_address(server, addr,
2087 (struct sockaddr *)&vol->srcaddr))
2088 return 0;
2089
2090 if (!match_port(server, addr))
2091 return 0;
2092
2093 if (!match_security(server, vol))
2094 return 0;
2095
b782fcc1 2096 if (server->echo_interval != vol->echo_interval * HZ)
adfeb3e0
SF
2097 return 0;
2098
37bb04e5
PS
2099 return 1;
2100}
2101
e7ddee90 2102static struct TCP_Server_Info *
9fa114f7 2103cifs_find_tcp_session(struct smb_vol *vol)
1da177e4 2104{
e7ddee90 2105 struct TCP_Server_Info *server;
e7ddee90 2106
3f9bcca7 2107 spin_lock(&cifs_tcp_ses_lock);
4515148e 2108 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
9fa114f7 2109 if (!match_server(server, vol))
daf5b0b6
JL
2110 continue;
2111
e7ddee90 2112 ++server->srv_count;
3f9bcca7 2113 spin_unlock(&cifs_tcp_ses_lock);
f96637be 2114 cifs_dbg(FYI, "Existing tcp session with server found\n");
e7ddee90 2115 return server;
1da177e4 2116 }
3f9bcca7 2117 spin_unlock(&cifs_tcp_ses_lock);
1da177e4
LT
2118 return NULL;
2119}
1b20d672 2120
53e0e11e
PS
2121void
2122cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
1da177e4 2123{
a5c3e1c7
SF
2124 struct task_struct *task;
2125
3f9bcca7 2126 spin_lock(&cifs_tcp_ses_lock);
e7ddee90 2127 if (--server->srv_count > 0) {
3f9bcca7 2128 spin_unlock(&cifs_tcp_ses_lock);
e7ddee90 2129 return;
1da177e4 2130 }
1b20d672 2131
f1d0c998
RL
2132 put_net(cifs_net_ns(server));
2133
e7ddee90 2134 list_del_init(&server->tcp_ses_list);
3f9bcca7 2135 spin_unlock(&cifs_tcp_ses_lock);
dea570e0 2136
c74093b6
JL
2137 cancel_delayed_work_sync(&server->echo);
2138
53e0e11e
PS
2139#ifdef CONFIG_CIFS_SMB2
2140 if (from_reconnect)
2141 /*
2142 * Avoid deadlock here: reconnect work calls
2143 * cifs_put_tcp_session() at its end. Need to be sure
2144 * that reconnect work does nothing with server pointer after
2145 * that step.
2146 */
2147 cancel_delayed_work(&server->reconnect);
2148 else
2149 cancel_delayed_work_sync(&server->reconnect);
2150#endif
2151
e7ddee90
JL
2152 spin_lock(&GlobalMid_Lock);
2153 server->tcpStatus = CifsExiting;
2154 spin_unlock(&GlobalMid_Lock);
dea570e0 2155
d2b91521 2156 cifs_crypto_shash_release(server);
488f1d2d
SJ
2157 cifs_fscache_release_client_cookie(server);
2158
21e73393
SP
2159 kfree(server->session_key.response);
2160 server->session_key.response = NULL;
2161 server->session_key.len = 0;
a5c3e1c7
SF
2162
2163 task = xchg(&server->tsk, NULL);
2164 if (task)
2165 force_sig(SIGKILL, task);
1da177e4
LT
2166}
2167
63c038c2
JL
2168static struct TCP_Server_Info *
2169cifs_get_tcp_session(struct smb_vol *volume_info)
2170{
2171 struct TCP_Server_Info *tcp_ses = NULL;
63c038c2
JL
2172 int rc;
2173
f96637be 2174 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
63c038c2
JL
2175
2176 /* see if we already have a matching tcp_ses */
9fa114f7 2177 tcp_ses = cifs_find_tcp_session(volume_info);
63c038c2
JL
2178 if (tcp_ses)
2179 return tcp_ses;
2180
2181 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2182 if (!tcp_ses) {
2183 rc = -ENOMEM;
2184 goto out_err;
2185 }
2186
23db65f5
JL
2187 tcp_ses->ops = volume_info->ops;
2188 tcp_ses->vals = volume_info->vals;
f1d0c998 2189 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
63c038c2
JL
2190 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2191 if (IS_ERR(tcp_ses->hostname)) {
2192 rc = PTR_ERR(tcp_ses->hostname);
f7c5445a 2193 goto out_err_crypto_release;
63c038c2
JL
2194 }
2195
2196 tcp_ses->noblocksnd = volume_info->noblocksnd;
2197 tcp_ses->noautotune = volume_info->noautotune;
6a5fa236 2198 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
fc40f9cf 2199 tcp_ses->in_flight = 0;
2d86dbc9 2200 tcp_ses->credits = 1;
63c038c2
JL
2201 init_waitqueue_head(&tcp_ses->response_q);
2202 init_waitqueue_head(&tcp_ses->request_q);
2203 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2204 mutex_init(&tcp_ses->srv_mutex);
2205 memcpy(tcp_ses->workstation_RFC1001_name,
2206 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2207 memcpy(tcp_ses->server_RFC1001_name,
2208 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
5d0d2882 2209 tcp_ses->session_estab = false;
63c038c2 2210 tcp_ses->sequence_number = 0;
fda35943 2211 tcp_ses->lstrp = jiffies;
58fa015f 2212 spin_lock_init(&tcp_ses->req_lock);
63c038c2
JL
2213 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2214 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
c74093b6 2215 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
53e0e11e
PS
2216#ifdef CONFIG_CIFS_SMB2
2217 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2218 mutex_init(&tcp_ses->reconnect_mutex);
2219#endif
9fa114f7
JL
2220 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2221 sizeof(tcp_ses->srcaddr));
2222 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2223 sizeof(tcp_ses->dstaddr));
39552ea8 2224#ifdef CONFIG_CIFS_SMB2
fa70b87c 2225 generate_random_uuid(tcp_ses->client_guid);
39552ea8 2226#endif
63c038c2
JL
2227 /*
2228 * at this point we are the only ones with the pointer
2229 * to the struct since the kernel thread not created yet
2230 * no need to spinlock this init of tcpStatus or srv_count
2231 */
2232 tcp_ses->tcpStatus = CifsNew;
2233 ++tcp_ses->srv_count;
2234
adfeb3e0
SF
2235 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2236 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2237 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2238 else
2239 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
2240
a9f1b85e 2241 rc = ip_connect(tcp_ses);
63c038c2 2242 if (rc < 0) {
f96637be 2243 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
f7c5445a 2244 goto out_err_crypto_release;
63c038c2
JL
2245 }
2246
2247 /*
2248 * since we're in a cifs function already, we know that
2249 * this will succeed. No need for try_module_get().
2250 */
2251 __module_get(THIS_MODULE);
7c97c200 2252 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
63c038c2
JL
2253 tcp_ses, "cifsd");
2254 if (IS_ERR(tcp_ses->tsk)) {
2255 rc = PTR_ERR(tcp_ses->tsk);
f96637be 2256 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
63c038c2 2257 module_put(THIS_MODULE);
f7c5445a 2258 goto out_err_crypto_release;
63c038c2 2259 }
fd88ce93 2260 tcp_ses->tcpStatus = CifsNeedNegotiate;
63c038c2
JL
2261
2262 /* thread spawned, put it on the list */
3f9bcca7 2263 spin_lock(&cifs_tcp_ses_lock);
63c038c2 2264 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
3f9bcca7 2265 spin_unlock(&cifs_tcp_ses_lock);
63c038c2 2266
488f1d2d
SJ
2267 cifs_fscache_get_client_cookie(tcp_ses);
2268
c74093b6 2269 /* queue echo request delayed work */
adfeb3e0 2270 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
c74093b6 2271
63c038c2
JL
2272 return tcp_ses;
2273
f7c5445a 2274out_err_crypto_release:
d2b91521
SP
2275 cifs_crypto_shash_release(tcp_ses);
2276
f1d0c998
RL
2277 put_net(cifs_net_ns(tcp_ses));
2278
63c038c2
JL
2279out_err:
2280 if (tcp_ses) {
8347a5cd
SF
2281 if (!IS_ERR(tcp_ses->hostname))
2282 kfree(tcp_ses->hostname);
63c038c2
JL
2283 if (tcp_ses->ssocket)
2284 sock_release(tcp_ses->ssocket);
2285 kfree(tcp_ses);
2286 }
2287 return ERR_PTR(rc);
2288}
2289
96daf2b0 2290static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
37bb04e5 2291{
3f618223
JL
2292 if (vol->sectype != Unspecified &&
2293 vol->sectype != ses->sectype)
2294 return 0;
2295
2296 switch (ses->sectype) {
37bb04e5 2297 case Kerberos:
64ed39dd 2298 if (!uid_eq(vol->cred_uid, ses->cred_uid))
37bb04e5
PS
2299 return 0;
2300 break;
2301 default:
04febabc
JL
2302 /* NULL username means anonymous session */
2303 if (ses->user_name == NULL) {
2304 if (!vol->nullauth)
2305 return 0;
2306 break;
2307 }
2308
37bb04e5 2309 /* anything else takes username/password */
04febabc
JL
2310 if (strncmp(ses->user_name,
2311 vol->username ? vol->username : "",
8c3a2b4c 2312 CIFS_MAX_USERNAME_LEN))
37bb04e5 2313 return 0;
08b37d51 2314 if ((vol->username && strlen(vol->username) != 0) &&
37bb04e5
PS
2315 ses->password != NULL &&
2316 strncmp(ses->password,
2317 vol->password ? vol->password : "",
8c3a2b4c 2318 CIFS_MAX_PASSWORD_LEN))
37bb04e5
PS
2319 return 0;
2320 }
2321 return 1;
2322}
2323
96daf2b0 2324static struct cifs_ses *
4ff67b72 2325cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
1da177e4 2326{
96daf2b0 2327 struct cifs_ses *ses;
dea570e0 2328
3f9bcca7 2329 spin_lock(&cifs_tcp_ses_lock);
4ff67b72 2330 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
7f48558e
SP
2331 if (ses->status == CifsExiting)
2332 continue;
37bb04e5
PS
2333 if (!match_session(ses, vol))
2334 continue;
14fbf50d 2335 ++ses->ses_count;
3f9bcca7 2336 spin_unlock(&cifs_tcp_ses_lock);
14fbf50d
JL
2337 return ses;
2338 }
3f9bcca7 2339 spin_unlock(&cifs_tcp_ses_lock);
14fbf50d
JL
2340 return NULL;
2341}
dea570e0 2342
14fbf50d 2343static void
96daf2b0 2344cifs_put_smb_ses(struct cifs_ses *ses)
14fbf50d 2345{
7f48558e 2346 unsigned int rc, xid;
14fbf50d 2347 struct TCP_Server_Info *server = ses->server;
dea570e0 2348
f96637be 2349 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
7f48558e 2350
3f9bcca7 2351 spin_lock(&cifs_tcp_ses_lock);
7f48558e
SP
2352 if (ses->status == CifsExiting) {
2353 spin_unlock(&cifs_tcp_ses_lock);
2354 return;
2355 }
14fbf50d 2356 if (--ses->ses_count > 0) {
3f9bcca7 2357 spin_unlock(&cifs_tcp_ses_lock);
14fbf50d
JL
2358 return;
2359 }
7f48558e
SP
2360 if (ses->status == CifsGood)
2361 ses->status = CifsExiting;
3f9bcca7 2362 spin_unlock(&cifs_tcp_ses_lock);
dea570e0 2363
7f48558e 2364 if (ses->status == CifsExiting && server->ops->logoff) {
6d5786a3 2365 xid = get_xid();
7f48558e
SP
2366 rc = server->ops->logoff(xid, ses);
2367 if (rc)
2368 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2369 __func__, rc);
6d5786a3 2370 _free_xid(xid);
14fbf50d 2371 }
7f48558e
SP
2372
2373 spin_lock(&cifs_tcp_ses_lock);
2374 list_del_init(&ses->smb_ses_list);
2375 spin_unlock(&cifs_tcp_ses_lock);
2376
14fbf50d 2377 sesInfoFree(ses);
53e0e11e 2378 cifs_put_tcp_session(server, 0);
14fbf50d 2379}
dea570e0 2380
8a8798a5
JL
2381#ifdef CONFIG_KEYS
2382
057d6332
CG
2383/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2384#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
8a8798a5
JL
2385
2386/* Populate username and pw fields from keyring if possible */
2387static int
2388cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2389{
2390 int rc = 0;
146aa8b1
DH
2391 const char *delim, *payload;
2392 char *desc;
8a8798a5
JL
2393 ssize_t len;
2394 struct key *key;
2395 struct TCP_Server_Info *server = ses->server;
2396 struct sockaddr_in *sa;
2397 struct sockaddr_in6 *sa6;
146aa8b1 2398 const struct user_key_payload *upayload;
8a8798a5
JL
2399
2400 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2401 if (!desc)
2402 return -ENOMEM;
2403
2404 /* try to find an address key first */
2405 switch (server->dstaddr.ss_family) {
2406 case AF_INET:
2407 sa = (struct sockaddr_in *)&server->dstaddr;
2408 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2409 break;
2410 case AF_INET6:
2411 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2412 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2413 break;
2414 default:
f96637be
JP
2415 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2416 server->dstaddr.ss_family);
8a8798a5
JL
2417 rc = -EINVAL;
2418 goto out_err;
2419 }
2420
f96637be 2421 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
8a8798a5
JL
2422 key = request_key(&key_type_logon, desc, "");
2423 if (IS_ERR(key)) {
2424 if (!ses->domainName) {
f96637be 2425 cifs_dbg(FYI, "domainName is NULL\n");
8a8798a5
JL
2426 rc = PTR_ERR(key);
2427 goto out_err;
2428 }
2429
2430 /* didn't work, try to find a domain key */
2431 sprintf(desc, "cifs:d:%s", ses->domainName);
f96637be 2432 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
8a8798a5
JL
2433 key = request_key(&key_type_logon, desc, "");
2434 if (IS_ERR(key)) {
2435 rc = PTR_ERR(key);
2436 goto out_err;
2437 }
2438 }
2439
2440 down_read(&key->sem);
146aa8b1 2441 upayload = user_key_payload(key);
8a8798a5 2442 if (IS_ERR_OR_NULL(upayload)) {
4edc53c1 2443 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
8a8798a5
JL
2444 goto out_key_put;
2445 }
2446
2447 /* find first : in payload */
146aa8b1 2448 payload = upayload->data;
8a8798a5 2449 delim = strnchr(payload, upayload->datalen, ':');
f96637be 2450 cifs_dbg(FYI, "payload=%s\n", payload);
8a8798a5 2451 if (!delim) {
f96637be
JP
2452 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2453 upayload->datalen);
8a8798a5
JL
2454 rc = -EINVAL;
2455 goto out_key_put;
2456 }
2457
2458 len = delim - payload;
8c3a2b4c 2459 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
f96637be
JP
2460 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2461 len);
8a8798a5
JL
2462 rc = -EINVAL;
2463 goto out_key_put;
2464 }
2465
2466 vol->username = kstrndup(payload, len, GFP_KERNEL);
2467 if (!vol->username) {
f96637be
JP
2468 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2469 len);
8a8798a5
JL
2470 rc = -ENOMEM;
2471 goto out_key_put;
2472 }
f96637be 2473 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
8a8798a5
JL
2474
2475 len = key->datalen - (len + 1);
8c3a2b4c 2476 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
f96637be 2477 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
8a8798a5
JL
2478 rc = -EINVAL;
2479 kfree(vol->username);
2480 vol->username = NULL;
2481 goto out_key_put;
2482 }
2483
2484 ++delim;
2485 vol->password = kstrndup(delim, len, GFP_KERNEL);
2486 if (!vol->password) {
f96637be
JP
2487 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2488 len);
8a8798a5
JL
2489 rc = -ENOMEM;
2490 kfree(vol->username);
2491 vol->username = NULL;
2492 goto out_key_put;
2493 }
2494
2495out_key_put:
2496 up_read(&key->sem);
2497 key_put(key);
2498out_err:
2499 kfree(desc);
f96637be 2500 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
8a8798a5
JL
2501 return rc;
2502}
2503#else /* ! CONFIG_KEYS */
2504static inline int
2505cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2506 struct cifs_ses *ses __attribute__((unused)))
2507{
2508 return -ENOSYS;
2509}
2510#endif /* CONFIG_KEYS */
2511
96daf2b0 2512static struct cifs_ses *
36988c76
JL
2513cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2514{
286170aa
PS
2515 int rc = -ENOMEM;
2516 unsigned int xid;
96daf2b0 2517 struct cifs_ses *ses;
a9f1b85e
PS
2518 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2519 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
36988c76 2520
6d5786a3 2521 xid = get_xid();
36988c76 2522
4ff67b72 2523 ses = cifs_find_smb_ses(server, volume_info);
36988c76 2524 if (ses) {
f96637be
JP
2525 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2526 ses->status);
36988c76 2527
36988c76 2528 mutex_lock(&ses->session_mutex);
198b5682
JL
2529 rc = cifs_negotiate_protocol(xid, ses);
2530 if (rc) {
2531 mutex_unlock(&ses->session_mutex);
2532 /* problem -- put our ses reference */
2533 cifs_put_smb_ses(ses);
6d5786a3 2534 free_xid(xid);
198b5682
JL
2535 return ERR_PTR(rc);
2536 }
36988c76 2537 if (ses->need_reconnect) {
f96637be 2538 cifs_dbg(FYI, "Session needs reconnect\n");
36988c76
JL
2539 rc = cifs_setup_session(xid, ses,
2540 volume_info->local_nls);
2541 if (rc) {
2542 mutex_unlock(&ses->session_mutex);
2543 /* problem -- put our reference */
2544 cifs_put_smb_ses(ses);
6d5786a3 2545 free_xid(xid);
36988c76
JL
2546 return ERR_PTR(rc);
2547 }
2548 }
2549 mutex_unlock(&ses->session_mutex);
460cf341
JL
2550
2551 /* existing SMB ses has a server reference already */
53e0e11e 2552 cifs_put_tcp_session(server, 0);
6d5786a3 2553 free_xid(xid);
36988c76
JL
2554 return ses;
2555 }
2556
f96637be 2557 cifs_dbg(FYI, "Existing smb sess not found\n");
36988c76
JL
2558 ses = sesInfoAlloc();
2559 if (ses == NULL)
2560 goto get_ses_fail;
2561
2562 /* new SMB session uses our server ref */
2563 ses->server = server;
a9f1b85e
PS
2564 if (server->dstaddr.ss_family == AF_INET6)
2565 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
36988c76 2566 else
a9f1b85e 2567 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
36988c76 2568
8727c8a8
SF
2569 if (volume_info->username) {
2570 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2571 if (!ses->user_name)
2572 goto get_ses_fail;
2573 }
36988c76
JL
2574
2575 /* volume_info->password freed at unmount */
2576 if (volume_info->password) {
2577 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2578 if (!ses->password)
2579 goto get_ses_fail;
2580 }
2581 if (volume_info->domainname) {
d3686d54
SP
2582 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2583 if (!ses->domainName)
2584 goto get_ses_fail;
36988c76 2585 }
39566443
GP
2586 if (volume_info->domainauto)
2587 ses->domainAuto = volume_info->domainauto;
3e4b3e1f 2588 ses->cred_uid = volume_info->cred_uid;
36988c76 2589 ses->linux_uid = volume_info->linux_uid;
d9b94201 2590
28e11bd8
JL
2591 ses->sectype = volume_info->sectype;
2592 ses->sign = volume_info->sign;
36988c76
JL
2593
2594 mutex_lock(&ses->session_mutex);
198b5682
JL
2595 rc = cifs_negotiate_protocol(xid, ses);
2596 if (!rc)
2597 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
36988c76 2598 mutex_unlock(&ses->session_mutex);
c8e56f1f 2599 if (rc)
36988c76
JL
2600 goto get_ses_fail;
2601
2602 /* success, put it on the list */
3f9bcca7 2603 spin_lock(&cifs_tcp_ses_lock);
36988c76 2604 list_add(&ses->smb_ses_list, &server->smb_ses_list);
3f9bcca7 2605 spin_unlock(&cifs_tcp_ses_lock);
36988c76 2606
6d5786a3 2607 free_xid(xid);
36988c76
JL
2608 return ses;
2609
2610get_ses_fail:
2611 sesInfoFree(ses);
6d5786a3 2612 free_xid(xid);
36988c76
JL
2613 return ERR_PTR(rc);
2614}
2615
96daf2b0 2616static int match_tcon(struct cifs_tcon *tcon, const char *unc)
37bb04e5
PS
2617{
2618 if (tcon->tidStatus == CifsExiting)
2619 return 0;
2620 if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
2621 return 0;
2622 return 1;
2623}
2624
96daf2b0 2625static struct cifs_tcon *
8b217fe7 2626cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
f1987b44
JL
2627{
2628 struct list_head *tmp;
96daf2b0 2629 struct cifs_tcon *tcon;
f1987b44 2630
3f9bcca7 2631 spin_lock(&cifs_tcp_ses_lock);
f1987b44 2632 list_for_each(tmp, &ses->tcon_list) {
96daf2b0 2633 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
8b217fe7
SF
2634 if (!match_tcon(tcon, volume_info->UNC))
2635 continue;
2636
2637#ifdef CONFIG_CIFS_SMB2
2638 if (tcon->snapshot_time != volume_info->snapshot_time)
f1987b44 2639 continue;
8b217fe7
SF
2640#endif /* CONFIG_CIFS_SMB2 */
2641
f1987b44 2642 ++tcon->tc_count;
3f9bcca7 2643 spin_unlock(&cifs_tcp_ses_lock);
dea570e0 2644 return tcon;
1da177e4 2645 }
3f9bcca7 2646 spin_unlock(&cifs_tcp_ses_lock);
1da177e4
LT
2647 return NULL;
2648}
2649
53e0e11e 2650void
96daf2b0 2651cifs_put_tcon(struct cifs_tcon *tcon)
f1987b44 2652{
2e6e02ab 2653 unsigned int xid;
96daf2b0 2654 struct cifs_ses *ses = tcon->ses;
f1987b44 2655
f96637be 2656 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
3f9bcca7 2657 spin_lock(&cifs_tcp_ses_lock);
f1987b44 2658 if (--tcon->tc_count > 0) {
3f9bcca7 2659 spin_unlock(&cifs_tcp_ses_lock);
f1987b44
JL
2660 return;
2661 }
2662
2663 list_del_init(&tcon->tcon_list);
3f9bcca7 2664 spin_unlock(&cifs_tcp_ses_lock);
f1987b44 2665
6d5786a3 2666 xid = get_xid();
2e6e02ab
PS
2667 if (ses->server->ops->tree_disconnect)
2668 ses->server->ops->tree_disconnect(xid, tcon);
6d5786a3 2669 _free_xid(xid);
f1987b44 2670
d03382ce 2671 cifs_fscache_release_super_cookie(tcon);
9f841593 2672 tconInfoFree(tcon);
f1987b44
JL
2673 cifs_put_smb_ses(ses);
2674}
2675
96daf2b0
SF
2676static struct cifs_tcon *
2677cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
d00c28de
JL
2678{
2679 int rc, xid;
96daf2b0 2680 struct cifs_tcon *tcon;
d00c28de 2681
8b217fe7 2682 tcon = cifs_find_tcon(ses, volume_info);
d00c28de 2683 if (tcon) {
f96637be 2684 cifs_dbg(FYI, "Found match on UNC path\n");
d00c28de
JL
2685 /* existing tcon already has a reference */
2686 cifs_put_smb_ses(ses);
2687 if (tcon->seal != volume_info->seal)
f96637be 2688 cifs_dbg(VFS, "transport encryption setting conflicts with existing tid\n");
d00c28de
JL
2689 return tcon;
2690 }
2691
2e6e02ab
PS
2692 if (!ses->server->ops->tree_connect) {
2693 rc = -ENOSYS;
2694 goto out_fail;
2695 }
2696
d00c28de
JL
2697 tcon = tconInfoAlloc();
2698 if (tcon == NULL) {
2699 rc = -ENOMEM;
2700 goto out_fail;
2701 }
2702
8b217fe7
SF
2703 if (volume_info->snapshot_time) {
2704#ifdef CONFIG_CIFS_SMB2
2705 if (ses->server->vals->protocol_id == 0) {
2706 cifs_dbg(VFS,
2707 "Use SMB2 or later for snapshot mount option\n");
2708 rc = -EOPNOTSUPP;
2709 goto out_fail;
2710 } else
2711 tcon->snapshot_time = volume_info->snapshot_time;
2712#else
2713 cifs_dbg(VFS, "Snapshot mount option requires SMB2 support\n");
2714 rc = -EOPNOTSUPP;
2715 goto out_fail;
2716#endif /* CONFIG_CIFS_SMB2 */
2717 }
2718
d00c28de
JL
2719 tcon->ses = ses;
2720 if (volume_info->password) {
2721 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
2722 if (!tcon->password) {
2723 rc = -ENOMEM;
2724 goto out_fail;
2725 }
2726 }
2727
2e6e02ab
PS
2728 /*
2729 * BB Do we need to wrap session_mutex around this TCon call and Unix
2730 * SetFS as we do on SessSetup and reconnect?
2731 */
6d5786a3 2732 xid = get_xid();
2e6e02ab
PS
2733 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
2734 volume_info->local_nls);
6d5786a3 2735 free_xid(xid);
f96637be 2736 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
d00c28de
JL
2737 if (rc)
2738 goto out_fail;
2739
2740 if (volume_info->nodfs) {
2741 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
f96637be 2742 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
d00c28de
JL
2743 }
2744 tcon->seal = volume_info->seal;
b618f001
SF
2745 tcon->use_persistent = false;
2746 /* check if SMB2 or later, CIFS does not support persistent handles */
2747 if (volume_info->persistent) {
2748 if (ses->server->vals->protocol_id == 0) {
2749 cifs_dbg(VFS,
2750 "SMB3 or later required for persistent handles\n");
2751 rc = -EOPNOTSUPP;
2752 goto out_fail;
592fafe6 2753#ifdef CONFIG_CIFS_SMB2
b618f001
SF
2754 } else if (ses->server->capabilities &
2755 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2756 tcon->use_persistent = true;
2757 else /* persistent handles requested but not supported */ {
2758 cifs_dbg(VFS,
2759 "Persistent handles not supported on share\n");
2760 rc = -EOPNOTSUPP;
2761 goto out_fail;
592fafe6 2762#endif /* CONFIG_CIFS_SMB2 */
b618f001 2763 }
592fafe6 2764#ifdef CONFIG_CIFS_SMB2
b618f001
SF
2765 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2766 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2767 && (volume_info->nopersistent == false)) {
2768 cifs_dbg(FYI, "enabling persistent handles\n");
2769 tcon->use_persistent = true;
592fafe6
SF
2770#endif /* CONFIG_CIFS_SMB2 */
2771 } else if (volume_info->resilient) {
2772 if (ses->server->vals->protocol_id == 0) {
2773 cifs_dbg(VFS,
2774 "SMB2.1 or later required for resilient handles\n");
2775 rc = -EOPNOTSUPP;
2776 goto out_fail;
2777 }
2778 tcon->use_resilient = true;
b618f001
SF
2779 }
2780
2e6e02ab
PS
2781 /*
2782 * We can have only one retry value for a connection to a share so for
2783 * resources mounted more than once to the same server share the last
2784 * value passed in for the retry flag is used.
2785 */
d00c28de
JL
2786 tcon->retry = volume_info->retry;
2787 tcon->nocase = volume_info->nocase;
2788 tcon->local_lease = volume_info->local_lease;
233839b1 2789 INIT_LIST_HEAD(&tcon->pending_opens);
d00c28de 2790
3f9bcca7 2791 spin_lock(&cifs_tcp_ses_lock);
d00c28de 2792 list_add(&tcon->tcon_list, &ses->tcon_list);
3f9bcca7 2793 spin_unlock(&cifs_tcp_ses_lock);
d00c28de 2794
d03382ce
SJ
2795 cifs_fscache_get_super_cookie(tcon);
2796
d00c28de
JL
2797 return tcon;
2798
2799out_fail:
2800 tconInfoFree(tcon);
2801 return ERR_PTR(rc);
2802}
2803
9d002df4
JL
2804void
2805cifs_put_tlink(struct tcon_link *tlink)
2806{
2807 if (!tlink || IS_ERR(tlink))
2808 return;
2809
2810 if (!atomic_dec_and_test(&tlink->tl_count) ||
2811 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2812 tlink->tl_time = jiffies;
2813 return;
2814 }
2815
2816 if (!IS_ERR(tlink_tcon(tlink)))
2817 cifs_put_tcon(tlink_tcon(tlink));
2818 kfree(tlink);
2819 return;
2820}
d00c28de 2821
25c7f41e 2822static inline struct tcon_link *
cd51875d
PS
2823cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
2824{
2825 return cifs_sb->master_tlink;
2826}
25c7f41e
PS
2827
2828static int
2829compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2830{
2831 struct cifs_sb_info *old = CIFS_SB(sb);
2832 struct cifs_sb_info *new = mnt_data->cifs_sb;
2833
2834 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2835 return 0;
2836
2837 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
2838 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
2839 return 0;
2840
25c7f41e 2841 /*
5eba8ab3
JL
2842 * We want to share sb only if we don't specify an r/wsize or
2843 * specified r/wsize is greater than or equal to existing one.
25c7f41e
PS
2844 */
2845 if (new->wsize && new->wsize < old->wsize)
2846 return 0;
2847
5eba8ab3
JL
2848 if (new->rsize && new->rsize < old->rsize)
2849 return 0;
2850
1f68233c 2851 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
25c7f41e
PS
2852 return 0;
2853
2854 if (old->mnt_file_mode != new->mnt_file_mode ||
2855 old->mnt_dir_mode != new->mnt_dir_mode)
2856 return 0;
2857
2858 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2859 return 0;
2860
2861 if (old->actimeo != new->actimeo)
2862 return 0;
2863
2864 return 1;
2865}
2866
c1d8b24d
SP
2867static int
2868match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2869{
2870 struct cifs_sb_info *old = CIFS_SB(sb);
2871 struct cifs_sb_info *new = mnt_data->cifs_sb;
2872
2873 if (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) {
2874 if (!(new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH))
2875 return 0;
2876 /* The prepath should be null terminated strings */
2877 if (strcmp(new->prepath, old->prepath))
2878 return 0;
2879
2880 return 1;
2881 }
2882 return 0;
2883}
2884
25c7f41e
PS
2885int
2886cifs_match_super(struct super_block *sb, void *data)
2887{
2888 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2889 struct smb_vol *volume_info;
2890 struct cifs_sb_info *cifs_sb;
2891 struct TCP_Server_Info *tcp_srv;
96daf2b0
SF
2892 struct cifs_ses *ses;
2893 struct cifs_tcon *tcon;
25c7f41e 2894 struct tcon_link *tlink;
25c7f41e
PS
2895 int rc = 0;
2896
25c7f41e
PS
2897 spin_lock(&cifs_tcp_ses_lock);
2898 cifs_sb = CIFS_SB(sb);
2899 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2900 if (IS_ERR(tlink)) {
2901 spin_unlock(&cifs_tcp_ses_lock);
2902 return rc;
2903 }
2904 tcon = tlink_tcon(tlink);
2905 ses = tcon->ses;
2906 tcp_srv = ses->server;
2907
2908 volume_info = mnt_data->vol;
2909
9fa114f7 2910 if (!match_server(tcp_srv, volume_info) ||
25c7f41e 2911 !match_session(ses, volume_info) ||
c1d8b24d
SP
2912 !match_tcon(tcon, volume_info->UNC) ||
2913 !match_prepath(sb, mnt_data)) {
25c7f41e
PS
2914 rc = 0;
2915 goto out;
2916 }
2917
2918 rc = compare_mount_options(sb, mnt_data);
2919out:
25c7f41e 2920 spin_unlock(&cifs_tcp_ses_lock);
f484b5d0 2921 cifs_put_tlink(tlink);
25c7f41e
PS
2922 return rc;
2923}
2924
1da177e4 2925int
b669f33c 2926get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
2e6e02ab
PS
2927 const struct nls_table *nls_codepage, unsigned int *num_referrals,
2928 struct dfs_info3_param **referrals, int remap)
1da177e4
LT
2929{
2930 char *temp_unc;
2931 int rc = 0;
2932
b669f33c 2933 if (!ses->server->ops->tree_connect || !ses->server->ops->get_dfs_refer)
2e6e02ab
PS
2934 return -ENOSYS;
2935
2936 *num_referrals = 0;
2937 *referrals = NULL;
1da177e4 2938
2e6e02ab 2939 if (ses->ipc_tid == 0) {
1da177e4 2940 temp_unc = kmalloc(2 /* for slashes */ +
2e6e02ab
PS
2941 strnlen(ses->serverName, SERVER_NAME_LEN_WITH_NULL * 2)
2942 + 1 + 4 /* slash IPC$ */ + 2, GFP_KERNEL);
1da177e4
LT
2943 if (temp_unc == NULL)
2944 return -ENOMEM;
2945 temp_unc[0] = '\\';
2946 temp_unc[1] = '\\';
2e6e02ab
PS
2947 strcpy(temp_unc + 2, ses->serverName);
2948 strcpy(temp_unc + 2 + strlen(ses->serverName), "\\IPC$");
2949 rc = ses->server->ops->tree_connect(xid, ses, temp_unc, NULL,
2950 nls_codepage);
f96637be 2951 cifs_dbg(FYI, "Tcon rc = %d ipc_tid = %d\n", rc, ses->ipc_tid);
1da177e4
LT
2952 kfree(temp_unc);
2953 }
2954 if (rc == 0)
b669f33c
PS
2955 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
2956 referrals, num_referrals,
2957 nls_codepage, remap);
2e6e02ab
PS
2958 /*
2959 * BB - map targetUNCs to dfs_info3 structures, here or in
b669f33c 2960 * ses->server->ops->get_dfs_refer.
2e6e02ab 2961 */
1da177e4
LT
2962
2963 return rc;
2964}
2965
09e50d55
JL
2966#ifdef CONFIG_DEBUG_LOCK_ALLOC
2967static struct lock_class_key cifs_key[2];
2968static struct lock_class_key cifs_slock_key[2];
2969
2970static inline void
2971cifs_reclassify_socket4(struct socket *sock)
2972{
2973 struct sock *sk = sock->sk;
fafc4e1e 2974 BUG_ON(!sock_allow_reclassification(sk));
09e50d55
JL
2975 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2976 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2977}
2978
2979static inline void
2980cifs_reclassify_socket6(struct socket *sock)
2981{
2982 struct sock *sk = sock->sk;
fafc4e1e 2983 BUG_ON(!sock_allow_reclassification(sk));
09e50d55
JL
2984 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2985 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2986}
2987#else
2988static inline void
2989cifs_reclassify_socket4(struct socket *sock)
2990{
2991}
2992
2993static inline void
2994cifs_reclassify_socket6(struct socket *sock)
2995{
2996}
2997#endif
2998
1da177e4 2999/* See RFC1001 section 14 on representation of Netbios names */
50c2f753 3000static void rfc1002mangle(char *target, char *source, unsigned int length)
1da177e4 3001{
50c2f753 3002 unsigned int i, j;
1da177e4 3003
50c2f753 3004 for (i = 0, j = 0; i < (length); i++) {
1da177e4
LT
3005 /* mask a nibble at a time and encode */
3006 target[j] = 'A' + (0x0F & (source[i] >> 4));
3007 target[j+1] = 'A' + (0x0F & source[i]);
50c2f753 3008 j += 2;
1da177e4
LT
3009 }
3010
3011}
3012
3eb9a889
BG
3013static int
3014bind_socket(struct TCP_Server_Info *server)
3015{
3016 int rc = 0;
3017 if (server->srcaddr.ss_family != AF_UNSPEC) {
3018 /* Bind to the specified local IP address */
3019 struct socket *socket = server->ssocket;
3020 rc = socket->ops->bind(socket,
3021 (struct sockaddr *) &server->srcaddr,
3022 sizeof(server->srcaddr));
3023 if (rc < 0) {
3024 struct sockaddr_in *saddr4;
3025 struct sockaddr_in6 *saddr6;
3026 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3027 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3028 if (saddr6->sin6_family == AF_INET6)
f96637be
JP
3029 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3030 &saddr6->sin6_addr, rc);
3eb9a889 3031 else
f96637be
JP
3032 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3033 &saddr4->sin_addr.s_addr, rc);
3eb9a889
BG
3034 }
3035 }
3036 return rc;
3037}
1da177e4
LT
3038
3039static int
a9f1b85e 3040ip_rfc1001_connect(struct TCP_Server_Info *server)
1da177e4
LT
3041{
3042 int rc = 0;
a9f1b85e
PS
3043 /*
3044 * some servers require RFC1001 sessinit before sending
3045 * negprot - BB check reconnection in case where second
3046 * sessinit is sent but no second negprot
3047 */
3048 struct rfc1002_session_packet *ses_init_buf;
3049 struct smb_hdr *smb_buf;
3050 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3051 GFP_KERNEL);
3052 if (ses_init_buf) {
3053 ses_init_buf->trailer.session_req.called_len = 32;
3054
997152f6 3055 if (server->server_RFC1001_name[0] != 0)
a9f1b85e
PS
3056 rfc1002mangle(ses_init_buf->trailer.
3057 session_req.called_name,
3058 server->server_RFC1001_name,
3059 RFC1001_NAME_LEN_WITH_NULL);
3060 else
3061 rfc1002mangle(ses_init_buf->trailer.
3062 session_req.called_name,
3063 DEFAULT_CIFS_CALLED_NAME,
3064 RFC1001_NAME_LEN_WITH_NULL);
3065
3066 ses_init_buf->trailer.session_req.calling_len = 32;
3067
3068 /*
3069 * calling name ends in null (byte 16) from old smb
3070 * convention.
3071 */
c85c35f8 3072 if (server->workstation_RFC1001_name[0] != 0)
a9f1b85e
PS
3073 rfc1002mangle(ses_init_buf->trailer.
3074 session_req.calling_name,
3075 server->workstation_RFC1001_name,
3076 RFC1001_NAME_LEN_WITH_NULL);
3077 else
3078 rfc1002mangle(ses_init_buf->trailer.
3079 session_req.calling_name,
3080 "LINUX_CIFS_CLNT",
3081 RFC1001_NAME_LEN_WITH_NULL);
3082
3083 ses_init_buf->trailer.session_req.scope1 = 0;
3084 ses_init_buf->trailer.session_req.scope2 = 0;
3085 smb_buf = (struct smb_hdr *)ses_init_buf;
3086
3087 /* sizeof RFC1002_SESSION_REQUEST with no scope */
be8e3b00 3088 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
a9f1b85e
PS
3089 rc = smb_send(server, smb_buf, 0x44);
3090 kfree(ses_init_buf);
3091 /*
3092 * RFC1001 layer in at least one server
3093 * requires very short break before negprot
3094 * presumably because not expecting negprot
3095 * to follow so fast. This is a simple
3096 * solution that works without
3097 * complicating the code and causes no
3098 * significant slowing down on mount
3099 * for everyone else
3100 */
3101 usleep_range(1000, 2000);
3102 }
3103 /*
3104 * else the negprot may still work without this
3105 * even though malloc failed
3106 */
3107
3108 return rc;
3109}
3110
3111static int
3112generic_ip_connect(struct TCP_Server_Info *server)
3113{
3114 int rc = 0;
6da97910 3115 __be16 sport;
a9f1b85e 3116 int slen, sfamily;
bcf4b106 3117 struct socket *socket = server->ssocket;
a9f1b85e
PS
3118 struct sockaddr *saddr;
3119
3120 saddr = (struct sockaddr *) &server->dstaddr;
3121
3122 if (server->dstaddr.ss_family == AF_INET6) {
3123 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3124 slen = sizeof(struct sockaddr_in6);
3125 sfamily = AF_INET6;
3126 } else {
3127 sport = ((struct sockaddr_in *) saddr)->sin_port;
3128 slen = sizeof(struct sockaddr_in);
3129 sfamily = AF_INET;
3130 }
1da177e4 3131
bcf4b106 3132 if (socket == NULL) {
f1d0c998
RL
3133 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3134 IPPROTO_TCP, &socket, 1);
1da177e4 3135 if (rc < 0) {
f96637be 3136 cifs_dbg(VFS, "Error %d creating socket\n", rc);
a9f1b85e 3137 server->ssocket = NULL;
1da177e4 3138 return rc;
1da177e4 3139 }
bcf4b106
JL
3140
3141 /* BB other socket options to set KEEPALIVE, NODELAY? */
f96637be 3142 cifs_dbg(FYI, "Socket created\n");
bcf4b106
JL
3143 server->ssocket = socket;
3144 socket->sk->sk_allocation = GFP_NOFS;
a9f1b85e
PS
3145 if (sfamily == AF_INET6)
3146 cifs_reclassify_socket6(socket);
3147 else
3148 cifs_reclassify_socket4(socket);
1da177e4
LT
3149 }
3150
3eb9a889
BG
3151 rc = bind_socket(server);
3152 if (rc < 0)
3153 return rc;
3154
bcf4b106
JL
3155 /*
3156 * Eventually check for other socket options to change from
a9f1b85e
PS
3157 * the default. sock_setsockopt not used because it expects
3158 * user space buffer
bcf4b106
JL
3159 */
3160 socket->sk->sk_rcvtimeo = 7 * HZ;
da505c38 3161 socket->sk->sk_sndtimeo = 5 * HZ;
edf1ae40 3162
b387eaeb 3163 /* make the bufsizes depend on wsize/rsize and max requests */
bcf4b106
JL
3164 if (server->noautotune) {
3165 if (socket->sk->sk_sndbuf < (200 * 1024))
3166 socket->sk->sk_sndbuf = 200 * 1024;
3167 if (socket->sk->sk_rcvbuf < (140 * 1024))
3168 socket->sk->sk_rcvbuf = 140 * 1024;
edf1ae40 3169 }
1da177e4 3170
6a5fa236 3171 if (server->tcp_nodelay) {
a9f1b85e 3172 int val = 1;
6a5fa236
SF
3173 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3174 (char *)&val, sizeof(val));
3175 if (rc)
f96637be
JP
3176 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3177 rc);
6a5fa236
SF
3178 }
3179
f96637be 3180 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
bcf4b106 3181 socket->sk->sk_sndbuf,
b6b38f70 3182 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
bcf4b106 3183
ee1b3ea9
JL
3184 rc = socket->ops->connect(socket, saddr, slen, 0);
3185 if (rc < 0) {
f96637be 3186 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
ee1b3ea9
JL
3187 sock_release(socket);
3188 server->ssocket = NULL;
3189 return rc;
3190 }
3191
a9f1b85e
PS
3192 if (sport == htons(RFC1001_PORT))
3193 rc = ip_rfc1001_connect(server);
50c2f753 3194
1da177e4
LT
3195 return rc;
3196}
3197
3198static int
a9f1b85e 3199ip_connect(struct TCP_Server_Info *server)
1da177e4 3200{
6da97910 3201 __be16 *sport;
a9f1b85e
PS
3202 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3203 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
1da177e4 3204
a9f1b85e
PS
3205 if (server->dstaddr.ss_family == AF_INET6)
3206 sport = &addr6->sin6_port;
3207 else
3208 sport = &addr->sin_port;
1da177e4 3209
a9f1b85e
PS
3210 if (*sport == 0) {
3211 int rc;
1da177e4 3212
a9f1b85e
PS
3213 /* try with 445 port at first */
3214 *sport = htons(CIFS_PORT);
3eb9a889 3215
a9f1b85e 3216 rc = generic_ip_connect(server);
1da177e4 3217 if (rc >= 0)
a9f1b85e 3218 return rc;
6a5fa236 3219
a9f1b85e
PS
3220 /* if it failed, try with 139 port */
3221 *sport = htons(RFC1001_PORT);
6a5fa236
SF
3222 }
3223
a9f1b85e 3224 return generic_ip_connect(server);
1da177e4
LT
3225}
3226
6d5786a3 3227void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
2c6292ae 3228 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
8af18971
SF
3229{
3230 /* if we are reconnecting then should we check to see if
3231 * any requested capabilities changed locally e.g. via
3232 * remount but we can not do much about it here
3233 * if they have (even if we could detect it by the following)
3234 * Perhaps we could add a backpointer to array of sb from tcon
3235 * or if we change to make all sb to same share the same
3236 * sb as NFS - then we only have one backpointer to sb.
3237 * What if we wanted to mount the server share twice once with
3238 * and once without posixacls or posix paths? */
3239 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
50c2f753 3240
c18c842b
SF
3241 if (vol_info && vol_info->no_linux_ext) {
3242 tcon->fsUnixInfo.Capability = 0;
3243 tcon->unix_ext = 0; /* Unix Extensions disabled */
f96637be 3244 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
c18c842b
SF
3245 return;
3246 } else if (vol_info)
3247 tcon->unix_ext = 1; /* Unix Extensions supported */
3248
3249 if (tcon->unix_ext == 0) {
f96637be 3250 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
c18c842b
SF
3251 return;
3252 }
50c2f753 3253
fb8c4b14 3254 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
8af18971 3255 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
f96637be 3256 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
8af18971
SF
3257 /* check for reconnect case in which we do not
3258 want to change the mount behavior if we can avoid it */
fb8c4b14 3259 if (vol_info == NULL) {
50c2f753 3260 /* turn off POSIX ACL and PATHNAMES if not set
8af18971
SF
3261 originally at mount time */
3262 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3263 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
11b6d645
IM
3264 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3265 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
f96637be 3266 cifs_dbg(VFS, "POSIXPATH support change\n");
8af18971 3267 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
11b6d645 3268 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
f96637be
JP
3269 cifs_dbg(VFS, "possible reconnect error\n");
3270 cifs_dbg(VFS, "server disabled POSIX path support\n");
11b6d645 3271 }
8af18971 3272 }
50c2f753 3273
6848b733 3274 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
f96637be 3275 cifs_dbg(VFS, "per-share encryption not supported yet\n");
6848b733 3276
8af18971 3277 cap &= CIFS_UNIX_CAP_MASK;
75865f8c 3278 if (vol_info && vol_info->no_psx_acl)
8af18971 3279 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
75865f8c 3280 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
f96637be 3281 cifs_dbg(FYI, "negotiated posix acl support\n");
2c6292ae
AV
3282 if (cifs_sb)
3283 cifs_sb->mnt_cifs_flags |=
3284 CIFS_MOUNT_POSIXACL;
8af18971
SF
3285 }
3286
75865f8c 3287 if (vol_info && vol_info->posix_paths == 0)
8af18971 3288 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
75865f8c 3289 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
f96637be 3290 cifs_dbg(FYI, "negotiate posix pathnames\n");
2c6292ae
AV
3291 if (cifs_sb)
3292 cifs_sb->mnt_cifs_flags |=
8af18971
SF
3293 CIFS_MOUNT_POSIX_PATHS;
3294 }
50c2f753 3295
f96637be 3296 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
8af18971 3297#ifdef CONFIG_CIFS_DEBUG2
75865f8c 3298 if (cap & CIFS_UNIX_FCNTL_CAP)
f96637be 3299 cifs_dbg(FYI, "FCNTL cap\n");
75865f8c 3300 if (cap & CIFS_UNIX_EXTATTR_CAP)
f96637be 3301 cifs_dbg(FYI, "EXTATTR cap\n");
75865f8c 3302 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
f96637be 3303 cifs_dbg(FYI, "POSIX path cap\n");
75865f8c 3304 if (cap & CIFS_UNIX_XATTR_CAP)
f96637be 3305 cifs_dbg(FYI, "XATTR cap\n");
75865f8c 3306 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
f96637be 3307 cifs_dbg(FYI, "POSIX ACL cap\n");
75865f8c 3308 if (cap & CIFS_UNIX_LARGE_READ_CAP)
f96637be 3309 cifs_dbg(FYI, "very large read cap\n");
75865f8c 3310 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
f96637be 3311 cifs_dbg(FYI, "very large write cap\n");
6848b733 3312 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
f96637be 3313 cifs_dbg(FYI, "transport encryption cap\n");
6848b733 3314 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
f96637be 3315 cifs_dbg(FYI, "mandatory transport encryption cap\n");
8af18971
SF
3316#endif /* CIFS_DEBUG2 */
3317 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
442aa310 3318 if (vol_info == NULL) {
f96637be 3319 cifs_dbg(FYI, "resetting capabilities failed\n");
442aa310 3320 } else
f96637be 3321 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");
5a44b319 3322
8af18971
SF
3323 }
3324 }
3325}
3326
4214ebf4 3327int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
724d9f1c 3328 struct cifs_sb_info *cifs_sb)
b1c8d2b4 3329{
2de970ff
JL
3330 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3331
2ced6f69
AV
3332 spin_lock_init(&cifs_sb->tlink_tree_lock);
3333 cifs_sb->tlink_tree = RB_ROOT;
3334
25c7f41e 3335 /*
5eba8ab3
JL
3336 * Temporarily set r/wsize for matching superblock. If we end up using
3337 * new sb then client will later negotiate it downward if needed.
25c7f41e 3338 */
5eba8ab3 3339 cifs_sb->rsize = pvolume_info->rsize;
25c7f41e
PS
3340 cifs_sb->wsize = pvolume_info->wsize;
3341
3b795210
SF
3342 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3343 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3344 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3345 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
f96637be
JP
3346 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3347 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
3b795210 3348
6d20e840 3349 cifs_sb->actimeo = pvolume_info->actimeo;
724d9f1c 3350 cifs_sb->local_nls = pvolume_info->local_nls;
6d20e840 3351
3b795210
SF
3352 if (pvolume_info->noperm)
3353 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3354 if (pvolume_info->setuids)
3355 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
95932655
SF
3356 if (pvolume_info->setuidfromacl)
3357 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
3b795210
SF
3358 if (pvolume_info->server_ino)
3359 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3360 if (pvolume_info->remap)
2baa2682
SF
3361 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3362 if (pvolume_info->sfu_remap)
3b795210
SF
3363 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3364 if (pvolume_info->no_xattr)
3365 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3366 if (pvolume_info->sfu_emul)
3367 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3368 if (pvolume_info->nobrl)
3369 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
be652445 3370 if (pvolume_info->nostrictsync)
4717bed6 3371 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
13a6e42a
SF
3372 if (pvolume_info->mand_lock)
3373 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
d4ffff1f
PS
3374 if (pvolume_info->rwpidforward)
3375 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
3b795210
SF
3376 if (pvolume_info->cifs_acl)
3377 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
3c7c87fd 3378 if (pvolume_info->backupuid_specified) {
3d3ea8e6 3379 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
3c7c87fd
SP
3380 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3381 }
3382 if (pvolume_info->backupgid_specified) {
3d3ea8e6 3383 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
3c7c87fd
SP
3384 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3385 }
3b795210
SF
3386 if (pvolume_info->override_uid)
3387 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3388 if (pvolume_info->override_gid)
3389 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3390 if (pvolume_info->dynperm)
3391 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
fa1df75d
SJ
3392 if (pvolume_info->fsc)
3393 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
0eb8a132
JL
3394 if (pvolume_info->multiuser)
3395 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3396 CIFS_MOUNT_NO_PERM);
d39454ff
PS
3397 if (pvolume_info->strict_io)
3398 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
3b795210 3399 if (pvolume_info->direct_io) {
f96637be 3400 cifs_dbg(FYI, "mounting share using direct i/o\n");
3b795210
SF
3401 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3402 }
736a3320
SM
3403 if (pvolume_info->mfsymlinks) {
3404 if (pvolume_info->sfu_emul) {
db8b631d
SF
3405 /*
3406 * Our SFU ("Services for Unix" emulation does not allow
3407 * creating symlinks but does allow reading existing SFU
3408 * symlinks (it does allow both creating and reading SFU
3409 * style mknod and FIFOs though). When "mfsymlinks" and
3410 * "sfu" are both enabled at the same time, it allows
3411 * reading both types of symlinks, but will only create
3412 * them with mfsymlinks format. This allows better
3413 * Apple compatibility (probably better for Samba too)
3414 * while still recognizing old Windows style symlinks.
3415 */
3416 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
736a3320 3417 }
db8b631d 3418 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
736a3320 3419 }
3b795210
SF
3420
3421 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
f96637be 3422 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
4214ebf4
SP
3423
3424 if (pvolume_info->prepath) {
3425 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3426 if (cifs_sb->prepath == NULL)
3427 return -ENOMEM;
3428 }
3429
3430 return 0;
b1c8d2b4
JL
3431}
3432
b9bce2e9
JL
3433static void
3434cleanup_volume_info_contents(struct smb_vol *volume_info)
1bfe73c2 3435{
b946845a 3436 kfree(volume_info->username);
1bfe73c2 3437 kzfree(volume_info->password);
95c75454 3438 kfree(volume_info->UNC);
b946845a
SF
3439 kfree(volume_info->domainname);
3440 kfree(volume_info->iocharset);
1bfe73c2 3441 kfree(volume_info->prepath);
b9bce2e9
JL
3442}
3443
3444void
3445cifs_cleanup_volume_info(struct smb_vol *volume_info)
3446{
3447 if (!volume_info)
3448 return;
3449 cleanup_volume_info_contents(volume_info);
1bfe73c2 3450 kfree(volume_info);
1bfe73c2
IM
3451}
3452
b9bce2e9 3453
2d6d589d 3454#ifdef CONFIG_CIFS_DFS_UPCALL
6d3ea7e4
SF
3455/*
3456 * cifs_build_path_to_root returns full path to root when we do not have an
3457 * exiting connection (tcon)
3458 */
1bfe73c2 3459static char *
b2a0fa15 3460build_unc_path_to_root(const struct smb_vol *vol,
1bfe73c2
IM
3461 const struct cifs_sb_info *cifs_sb)
3462{
b2a0fa15 3463 char *full_path, *pos;
839db3d1 3464 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
b2a0fa15 3465 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
1bfe73c2 3466
b2a0fa15 3467 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
1bfe73c2
IM
3468 if (full_path == NULL)
3469 return ERR_PTR(-ENOMEM);
3470
b2a0fa15
JL
3471 strncpy(full_path, vol->UNC, unc_len);
3472 pos = full_path + unc_len;
3473
3474 if (pplen) {
1fc29bac
JL
3475 *pos = CIFS_DIR_SEP(cifs_sb);
3476 strncpy(pos + 1, vol->prepath, pplen);
b2a0fa15
JL
3477 pos += pplen;
3478 }
3479
3480 *pos = '\0'; /* add trailing null */
f87d39d9 3481 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
f96637be 3482 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
1bfe73c2
IM
3483 return full_path;
3484}
dd613945
SF
3485
3486/*
3487 * Perform a dfs referral query for a share and (optionally) prefix
3488 *
046462ab
SF
3489 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3490 * to a string containing updated options for the submount. Otherwise it
3491 * will be left untouched.
dd613945
SF
3492 *
3493 * Returns the rc from get_dfs_path to the caller, which can be used to
3494 * determine whether there were referrals.
3495 */
3496static int
b669f33c 3497expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
dd613945 3498 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
046462ab 3499 int check_prefix)
dd613945
SF
3500{
3501 int rc;
3502 unsigned int num_referrals = 0;
3503 struct dfs_info3_param *referrals = NULL;
3504 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3505
3506 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3507 if (IS_ERR(full_path))
3508 return PTR_ERR(full_path);
3509
3510 /* For DFS paths, skip the first '\' of the UNC */
3511 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3512
b669f33c 3513 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
2baa2682 3514 &num_referrals, &referrals, cifs_remap(cifs_sb));
dd613945
SF
3515
3516 if (!rc && num_referrals > 0) {
3517 char *fake_devname = NULL;
3518
3519 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3520 full_path + 1, referrals,
3521 &fake_devname);
3522
3523 free_dfs_info_array(referrals, num_referrals);
046462ab 3524
dd613945
SF
3525 if (IS_ERR(mdata)) {
3526 rc = PTR_ERR(mdata);
3527 mdata = NULL;
b9bce2e9
JL
3528 } else {
3529 cleanup_volume_info_contents(volume_info);
b9bce2e9
JL
3530 rc = cifs_setup_volume_info(volume_info, mdata,
3531 fake_devname);
dd613945 3532 }
b9bce2e9
JL
3533 kfree(fake_devname);
3534 kfree(cifs_sb->mountdata);
046462ab 3535 cifs_sb->mountdata = mdata;
dd613945
SF
3536 }
3537 kfree(full_path);
3538 return rc;
3539}
2d6d589d 3540#endif
1bfe73c2 3541
04db79b0
JL
3542static int
3543cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3544 const char *devname)
1da177e4 3545{
724d9f1c 3546 int rc = 0;
1da177e4 3547
04db79b0
JL
3548 if (cifs_parse_mount_options(mount_data, devname, volume_info))
3549 return -EINVAL;
1da177e4 3550
7586b765 3551 if (volume_info->nullauth) {
f96637be 3552 cifs_dbg(FYI, "Anonymous login\n");
04febabc
JL
3553 kfree(volume_info->username);
3554 volume_info->username = NULL;
7586b765 3555 } else if (volume_info->username) {
1da177e4 3556 /* BB fixme parse for domain name here */
f96637be 3557 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
1da177e4 3558 } else {
f96637be 3559 cifs_dbg(VFS, "No username specified\n");
50c2f753
SF
3560 /* In userspace mount helper we can get user name from alternate
3561 locations such as env variables and files on disk */
04db79b0 3562 return -EINVAL;
1da177e4
LT
3563 }
3564
1da177e4 3565 /* this is needed for ASCII cp to Unicode converts */
7586b765 3566 if (volume_info->iocharset == NULL) {
a5fc4ce0
JL
3567 /* load_nls_default cannot return null */
3568 volume_info->local_nls = load_nls_default();
1da177e4 3569 } else {
a5fc4ce0
JL
3570 volume_info->local_nls = load_nls(volume_info->iocharset);
3571 if (volume_info->local_nls == NULL) {
f96637be 3572 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
b6b38f70 3573 volume_info->iocharset);
04db79b0 3574 return -ELIBACC;
1da177e4
LT
3575 }
3576 }
724d9f1c 3577
724d9f1c
PS
3578 return rc;
3579}
3580
04db79b0
JL
3581struct smb_vol *
3582cifs_get_volume_info(char *mount_data, const char *devname)
3583{
3584 int rc;
3585 struct smb_vol *volume_info;
3586
6ee9542a 3587 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
04db79b0
JL
3588 if (!volume_info)
3589 return ERR_PTR(-ENOMEM);
3590
3591 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
3592 if (rc) {
3593 cifs_cleanup_volume_info(volume_info);
3594 volume_info = ERR_PTR(rc);
3595 }
3596
3597 return volume_info;
3598}
3599
a6b5058f
AA
3600static int
3601cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3602 unsigned int xid,
3603 struct cifs_tcon *tcon,
3604 struct cifs_sb_info *cifs_sb,
3605 char *full_path)
3606{
3607 int rc;
3608 char *s;
3609 char sep, tmp;
3610
3611 sep = CIFS_DIR_SEP(cifs_sb);
3612 s = full_path;
3613
3614 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3615 while (rc == 0) {
3616 /* skip separators */
3617 while (*s == sep)
3618 s++;
3619 if (!*s)
3620 break;
3621 /* next separator */
3622 while (*s && *s != sep)
3623 s++;
3624
3625 /*
3626 * temporarily null-terminate the path at the end of
3627 * the current component
3628 */
3629 tmp = *s;
3630 *s = 0;
3631 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3632 full_path);
3633 *s = tmp;
3634 }
3635 return rc;
3636}
3637
724d9f1c 3638int
2c6292ae 3639cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
724d9f1c 3640{
1daaae8f 3641 int rc;
6d5786a3 3642 unsigned int xid;
af4281dc 3643 struct cifs_ses *ses;
96daf2b0 3644 struct cifs_tcon *tcon;
af4281dc 3645 struct TCP_Server_Info *server;
724d9f1c
PS
3646 char *full_path;
3647 struct tcon_link *tlink;
3648#ifdef CONFIG_CIFS_DFS_UPCALL
3649 int referral_walks_count = 0;
20547490 3650#endif
dd854466 3651
b4caecd4 3652 rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs");
dd854466
AV
3653 if (rc)
3654 return rc;
3655
20547490 3656#ifdef CONFIG_CIFS_DFS_UPCALL
724d9f1c
PS
3657try_mount_again:
3658 /* cleanup activities if we're chasing a referral */
3659 if (referral_walks_count) {
3660 if (tcon)
3661 cifs_put_tcon(tcon);
af4281dc
PS
3662 else if (ses)
3663 cifs_put_smb_ses(ses);
724d9f1c 3664
1dfd18d0
SP
3665 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3666
6d5786a3 3667 free_xid(xid);
724d9f1c
PS
3668 }
3669#endif
1daaae8f 3670 rc = 0;
724d9f1c 3671 tcon = NULL;
af4281dc
PS
3672 ses = NULL;
3673 server = NULL;
724d9f1c
PS
3674 full_path = NULL;
3675 tlink = NULL;
3676
6d5786a3 3677 xid = get_xid();
1da177e4 3678
63c038c2 3679 /* get a reference to a tcp session */
af4281dc
PS
3680 server = cifs_get_tcp_session(volume_info);
3681 if (IS_ERR(server)) {
3682 rc = PTR_ERR(server);
dd854466 3683 bdi_destroy(&cifs_sb->bdi);
63c038c2 3684 goto out;
1da177e4 3685 }
141891f4
SF
3686 if ((volume_info->max_credits < 20) ||
3687 (volume_info->max_credits > 60000))
3688 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3689 else
3690 server->max_credits = volume_info->max_credits;
36988c76 3691 /* get a reference to a SMB session */
af4281dc
PS
3692 ses = cifs_get_smb_ses(server, volume_info);
3693 if (IS_ERR(ses)) {
3694 rc = PTR_ERR(ses);
3695 ses = NULL;
36988c76 3696 goto mount_fail_check;
1da177e4 3697 }
50c2f753 3698
592fafe6 3699#ifdef CONFIG_CIFS_SMB2
b618f001
SF
3700 if ((volume_info->persistent == true) && ((ses->server->capabilities &
3701 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
3702 cifs_dbg(VFS, "persistent handles not supported by server\n");
3703 rc = -EOPNOTSUPP;
3704 goto mount_fail_check;
3705 }
592fafe6
SF
3706#endif /* CONFIG_CIFS_SMB2*/
3707
d00c28de 3708 /* search for existing tcon to this server share */
af4281dc 3709 tcon = cifs_get_tcon(ses, volume_info);
d00c28de
JL
3710 if (IS_ERR(tcon)) {
3711 rc = PTR_ERR(tcon);
3712 tcon = NULL;
1bfe73c2 3713 goto remote_path_check;
d00c28de 3714 }
1bfe73c2 3715
d82c2df5 3716 /* tell server which Unix caps we support */
29e20f9c 3717 if (cap_unix(tcon->ses)) {
d82c2df5
SF
3718 /* reset of caps checks mount to see if unix extensions
3719 disabled for just this mount */
2c6292ae 3720 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
6848b733
SF
3721 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3722 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3723 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3724 rc = -EACCES;
3725 goto mount_fail_check;
3726 }
3727 } else
d82c2df5 3728 tcon->unix_ext = 0; /* server does not support them */
c18c842b 3729
af4281dc
PS
3730 /* do not care if a following call succeed - informational */
3731 if (!tcon->ipc && server->ops->qfs_tcon)
3732 server->ops->qfs_tcon(xid, tcon);
6848b733 3733
24985c53
PS
3734 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
3735 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
f7910cbd 3736
66bfaadc 3737 /* tune readahead according to rsize */
09cbfeaf 3738 cifs_sb->bdi.ra_pages = cifs_sb->rsize / PAGE_SIZE;
f7910cbd 3739
1bfe73c2 3740remote_path_check:
c1508ca2
SF
3741#ifdef CONFIG_CIFS_DFS_UPCALL
3742 /*
3743 * Perform an unconditional check for whether there are DFS
3744 * referrals for this path without prefix, to provide support
3745 * for DFS referrals from w2k8 servers which don't seem to respond
3746 * with PATH_NOT_COVERED to requests that include the prefix.
3747 * Chase the referral if found, otherwise continue normally.
3748 */
3749 if (referral_walks_count == 0) {
af4281dc
PS
3750 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
3751 false);
c1508ca2
SF
3752 if (!refrc) {
3753 referral_walks_count++;
3754 goto try_mount_again;
3755 }
3756 }
3757#endif
3758
f87d39d9 3759 /* check if a whole path is not remote */
70945643 3760 if (!rc && tcon) {
68889f26
PS
3761 if (!server->ops->is_path_accessible) {
3762 rc = -ENOSYS;
3763 goto mount_fail_check;
3764 }
6d3ea7e4
SF
3765 /*
3766 * cifs_build_path_to_root works only when we have a valid tcon
3767 */
3768 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon);
e4cce94c
IM
3769 if (full_path == NULL) {
3770 rc = -ENOMEM;
3771 goto mount_fail_check;
3772 }
68889f26
PS
3773 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3774 full_path);
03ceace5 3775 if (rc != 0 && rc != -EREMOTE) {
e4cce94c
IM
3776 kfree(full_path);
3777 goto mount_fail_check;
3778 }
a6b5058f 3779
d171356f
SP
3780 if (rc != -EREMOTE) {
3781 rc = cifs_are_all_path_components_accessible(server,
a6b5058f
AA
3782 xid, tcon, cifs_sb,
3783 full_path);
d171356f
SP
3784 if (rc != 0) {
3785 cifs_dbg(VFS, "cannot query dirs between root and final path, "
3786 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3787 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3788 rc = 0;
3789 }
a6b5058f 3790 }
e4cce94c
IM
3791 kfree(full_path);
3792 }
3793
1bfe73c2
IM
3794 /* get referral if needed */
3795 if (rc == -EREMOTE) {
d036f50f 3796#ifdef CONFIG_CIFS_DFS_UPCALL
5c2503a8
IM
3797 if (referral_walks_count > MAX_NESTED_LINKS) {
3798 /*
3799 * BB: when we implement proper loop detection,
3800 * we will remove this check. But now we need it
3801 * to prevent an indefinite loop if 'DFS tree' is
3802 * misconfigured (i.e. has loops).
3803 */
3804 rc = -ELOOP;
3805 goto mount_fail_check;
3806 }
1bfe73c2 3807
af4281dc 3808 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
7b91e266 3809
dd613945 3810 if (!rc) {
5c2503a8 3811 referral_walks_count++;
1bfe73c2
IM
3812 goto try_mount_again;
3813 }
dd613945 3814 goto mount_fail_check;
d036f50f
SF
3815#else /* No DFS support, return error on mount */
3816 rc = -EOPNOTSUPP;
3817#endif
1bfe73c2
IM
3818 }
3819
9d002df4
JL
3820 if (rc)
3821 goto mount_fail_check;
3822
3823 /* now, hang the tcon off of the superblock */
3824 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
3825 if (tlink == NULL) {
3826 rc = -ENOMEM;
3827 goto mount_fail_check;
3828 }
3829
af4281dc 3830 tlink->tl_uid = ses->linux_uid;
9d002df4
JL
3831 tlink->tl_tcon = tcon;
3832 tlink->tl_time = jiffies;
3833 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3834 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3835
b647c35f 3836 cifs_sb->master_tlink = tlink;
9d002df4 3837 spin_lock(&cifs_sb->tlink_tree_lock);
b647c35f 3838 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
9d002df4 3839 spin_unlock(&cifs_sb->tlink_tree_lock);
413e661c 3840
da472fc8 3841 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
2de970ff
JL
3842 TLINK_IDLE_EXPIRE);
3843
1bfe73c2
IM
3844mount_fail_check:
3845 /* on error free sesinfo and tcon struct if needed */
3846 if (rc) {
1bfe73c2 3847 /* If find_unc succeeded then rc == 0 so we can not end */
25985edc 3848 /* up accidentally freeing someone elses tcon struct */
1bfe73c2
IM
3849 if (tcon)
3850 cifs_put_tcon(tcon);
af4281dc
PS
3851 else if (ses)
3852 cifs_put_smb_ses(ses);
1bfe73c2 3853 else
53e0e11e 3854 cifs_put_tcp_session(server, 0);
dd854466 3855 bdi_destroy(&cifs_sb->bdi);
1bfe73c2
IM
3856 }
3857
70fe7dc0 3858out:
6d5786a3 3859 free_xid(xid);
1da177e4
LT
3860 return rc;
3861}
3862
8d1bca32
JL
3863/*
3864 * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
3865 * pointer may be NULL.
3866 */
1da177e4 3867int
2e6e02ab 3868CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
96daf2b0 3869 const char *tree, struct cifs_tcon *tcon,
1da177e4
LT
3870 const struct nls_table *nls_codepage)
3871{
3872 struct smb_hdr *smb_buffer;
3873 struct smb_hdr *smb_buffer_response;
3874 TCONX_REQ *pSMB;
3875 TCONX_RSP *pSMBr;
3876 unsigned char *bcc_ptr;
3877 int rc = 0;
690c522f
JL
3878 int length;
3879 __u16 bytes_left, count;
1da177e4
LT
3880
3881 if (ses == NULL)
3882 return -EIO;
3883
3884 smb_buffer = cifs_buf_get();
ca43e3be 3885 if (smb_buffer == NULL)
1da177e4 3886 return -ENOMEM;
ca43e3be 3887
1da177e4
LT
3888 smb_buffer_response = smb_buffer;
3889
3890 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3891 NULL /*no tid */ , 4 /*wct */ );
1982c344 3892
88257360 3893 smb_buffer->Mid = get_next_mid(ses->server);
1da177e4
LT
3894 smb_buffer->Uid = ses->Suid;
3895 pSMB = (TCONX_REQ *) smb_buffer;
3896 pSMBr = (TCONX_RSP *) smb_buffer_response;
3897
3898 pSMB->AndXCommand = 0xFF;
3899 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
1da177e4 3900 bcc_ptr = &pSMB->Password[0];
8d1bca32 3901 if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
eeac8047 3902 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
7c7b25bc 3903 *bcc_ptr = 0; /* password is null byte */
eeac8047 3904 bcc_ptr++; /* skip password */
7c7b25bc 3905 /* already aligned so no need to do it below */
eeac8047 3906 } else {
540b2e37 3907 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
eeac8047
SF
3908 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3909 specified as required (when that support is added to
3910 the vfs in the future) as only NTLM or the much
7c7b25bc 3911 weaker LANMAN (which we do not send by default) is accepted
eeac8047
SF
3912 by Samba (not sure whether other servers allow
3913 NTLMv2 password here) */
7c7b25bc 3914#ifdef CONFIG_CIFS_WEAK_PW_HASH
04912d6a 3915 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
3f618223 3916 (ses->sectype == LANMAN))
d3ba50b1 3917 calc_lanman_hash(tcon->password, ses->server->cryptkey,
96daf2b0 3918 ses->server->sec_mode &
4e53a3fb
JL
3919 SECMODE_PW_ENCRYPT ? true : false,
3920 bcc_ptr);
7c7b25bc
SF
3921 else
3922#endif /* CIFS_WEAK_PW_HASH */
ee2c9258 3923 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
9ef5992e 3924 bcc_ptr, nls_codepage);
f3a31a2b
SF
3925 if (rc) {
3926 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
3927 __func__, rc);
3928 cifs_buf_release(smb_buffer);
3929 return rc;
3930 }
eeac8047 3931
540b2e37 3932 bcc_ptr += CIFS_AUTH_RESP_SIZE;
fb8c4b14 3933 if (ses->capabilities & CAP_UNICODE) {
7c7b25bc
SF
3934 /* must align unicode strings */
3935 *bcc_ptr = 0; /* null byte password */
3936 bcc_ptr++;
3937 }
eeac8047 3938 }
1da177e4 3939
38d77c50 3940 if (ses->server->sign)
1da177e4
LT
3941 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3942
3943 if (ses->capabilities & CAP_STATUS32) {
3944 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3945 }
3946 if (ses->capabilities & CAP_DFS) {
3947 smb_buffer->Flags2 |= SMBFLG2_DFS;
3948 }
3949 if (ses->capabilities & CAP_UNICODE) {
3950 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3951 length =
acbbb76a 3952 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
50c2f753 3953 6 /* max utf8 char length in bytes */ *
a878fb22
SF
3954 (/* server len*/ + 256 /* share len */), nls_codepage);
3955 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
1da177e4
LT
3956 bcc_ptr += 2; /* skip trailing null */
3957 } else { /* ASCII */
1da177e4
LT
3958 strcpy(bcc_ptr, tree);
3959 bcc_ptr += strlen(tree) + 1;
3960 }
3961 strcpy(bcc_ptr, "?????");
3962 bcc_ptr += strlen("?????");
3963 bcc_ptr += 1;
3964 count = bcc_ptr - &pSMB->Password[0];
be8e3b00
SF
3965 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
3966 pSMB->hdr.smb_buf_length) + count);
1da177e4
LT
3967 pSMB->ByteCount = cpu_to_le16(count);
3968
133672ef 3969 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
7749981e 3970 0);
1da177e4 3971
1da177e4
LT
3972 /* above now done in SendReceive */
3973 if ((rc == 0) && (tcon != NULL)) {
0e0d2cf3
SF
3974 bool is_unicode;
3975
1da177e4 3976 tcon->tidStatus = CifsGood;
3b795210 3977 tcon->need_reconnect = false;
1da177e4
LT
3978 tcon->tid = smb_buffer_response->Tid;
3979 bcc_ptr = pByteArea(smb_buffer_response);
690c522f 3980 bytes_left = get_bcc(smb_buffer_response);
cc20c031 3981 length = strnlen(bcc_ptr, bytes_left - 2);
0e0d2cf3
SF
3982 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3983 is_unicode = true;
3984 else
3985 is_unicode = false;
3986
cc20c031 3987
50c2f753 3988 /* skip service field (NB: this field is always ASCII) */
7f8ed420
SF
3989 if (length == 3) {
3990 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3991 (bcc_ptr[2] == 'C')) {
f96637be 3992 cifs_dbg(FYI, "IPC connection\n");
7f8ed420
SF
3993 tcon->ipc = 1;
3994 }
3995 } else if (length == 2) {
3996 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3997 /* the most common case */
f96637be 3998 cifs_dbg(FYI, "disk share connection\n");
7f8ed420
SF
3999 }
4000 }
50c2f753 4001 bcc_ptr += length + 1;
cc20c031 4002 bytes_left -= (length + 1);
46b51d08 4003 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
cc20c031
JL
4004
4005 /* mostly informational -- no need to fail on error here */
90a98b2f 4006 kfree(tcon->nativeFileSystem);
acbbb76a 4007 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
0e0d2cf3 4008 bytes_left, is_unicode,
cc20c031
JL
4009 nls_codepage);
4010
f96637be 4011 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
cc20c031 4012
fb8c4b14 4013 if ((smb_buffer_response->WordCount == 3) ||
1a4e15a0
SF
4014 (smb_buffer_response->WordCount == 7))
4015 /* field is in same location */
3979877e
SF
4016 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4017 else
4018 tcon->Flags = 0;
f96637be 4019 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
1da177e4 4020 } else if ((rc == 0) && tcon == NULL) {
50c2f753 4021 /* all we need to save for IPC$ connection */
1da177e4
LT
4022 ses->ipc_tid = smb_buffer_response->Tid;
4023 }
4024
a8a11d39 4025 cifs_buf_release(smb_buffer);
1da177e4
LT
4026 return rc;
4027}
4028
2e32cf5e
AV
4029static void delayed_free(struct rcu_head *p)
4030{
4031 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4032 unload_nls(sbi->local_nls);
4033 kfree(sbi);
4034}
4035
2a9b9951
AV
4036void
4037cifs_umount(struct cifs_sb_info *cifs_sb)
1da177e4 4038{
b647c35f
JL
4039 struct rb_root *root = &cifs_sb->tlink_tree;
4040 struct rb_node *node;
4041 struct tcon_link *tlink;
9d002df4 4042
2de970ff
JL
4043 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4044
b647c35f
JL
4045 spin_lock(&cifs_sb->tlink_tree_lock);
4046 while ((node = rb_first(root))) {
4047 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4048 cifs_get_tlink(tlink);
4049 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4050 rb_erase(node, root);
1da177e4 4051
b647c35f
JL
4052 spin_unlock(&cifs_sb->tlink_tree_lock);
4053 cifs_put_tlink(tlink);
4054 spin_lock(&cifs_sb->tlink_tree_lock);
4055 }
4056 spin_unlock(&cifs_sb->tlink_tree_lock);
50c2f753 4057
dd854466 4058 bdi_destroy(&cifs_sb->bdi);
d757d71b 4059 kfree(cifs_sb->mountdata);
a6b5058f 4060 kfree(cifs_sb->prepath);
2e32cf5e 4061 call_rcu(&cifs_sb->rcu, delayed_free);
50c2f753 4062}
1da177e4 4063
286170aa
PS
4064int
4065cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
1da177e4
LT
4066{
4067 int rc = 0;
198b5682 4068 struct TCP_Server_Info *server = ses->server;
1da177e4 4069
286170aa
PS
4070 if (!server->ops->need_neg || !server->ops->negotiate)
4071 return -ENOSYS;
4072
198b5682 4073 /* only send once per connect */
286170aa 4074 if (!server->ops->need_neg(server))
198b5682
JL
4075 return 0;
4076
45275789 4077 set_credits(server, 1);
286170aa
PS
4078
4079 rc = server->ops->negotiate(xid, ses);
198b5682
JL
4080 if (rc == 0) {
4081 spin_lock(&GlobalMid_Lock);
7fdbaa1b 4082 if (server->tcpStatus == CifsNeedNegotiate)
198b5682
JL
4083 server->tcpStatus = CifsGood;
4084 else
4085 rc = -EHOSTDOWN;
4086 spin_unlock(&GlobalMid_Lock);
198b5682
JL
4087 }
4088
4089 return rc;
4090}
4091
58c45c58
PS
4092int
4093cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4094 struct nls_table *nls_info)
198b5682 4095{
58c45c58 4096 int rc = -ENOSYS;
198b5682 4097 struct TCP_Server_Info *server = ses->server;
26b994fa 4098
198b5682 4099 ses->capabilities = server->capabilities;
26b994fa 4100 if (linuxExtEnabled == 0)
29e20f9c 4101 ses->capabilities &= (~server->vals->cap_unix);
20418acd 4102
f96637be 4103 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
96daf2b0 4104 server->sec_mode, server->capabilities, server->timeAdj);
cb7691b6 4105
58c45c58
PS
4106 if (server->ops->sess_setup)
4107 rc = server->ops->sess_setup(xid, ses, nls_info);
4108
d4e63bd6 4109 if (rc)
f96637be 4110 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
21e73393 4111
1da177e4
LT
4112 return rc;
4113}
4114
8a8798a5
JL
4115static int
4116cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4117{
3f618223
JL
4118 vol->sectype = ses->sectype;
4119
4120 /* krb5 is special, since we don't need username or pw */
4121 if (vol->sectype == Kerberos)
8a8798a5 4122 return 0;
8a8798a5
JL
4123
4124 return cifs_set_cifscreds(vol, ses);
4125}
4126
96daf2b0 4127static struct cifs_tcon *
6d4a0832 4128cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
9d002df4 4129{
8a8798a5 4130 int rc;
96daf2b0
SF
4131 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4132 struct cifs_ses *ses;
4133 struct cifs_tcon *tcon = NULL;
9d002df4 4134 struct smb_vol *vol_info;
9d002df4
JL
4135
4136 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
803ab977
DC
4137 if (vol_info == NULL)
4138 return ERR_PTR(-ENOMEM);
9d002df4 4139
9d002df4
JL
4140 vol_info->local_nls = cifs_sb->local_nls;
4141 vol_info->linux_uid = fsuid;
4142 vol_info->cred_uid = fsuid;
4143 vol_info->UNC = master_tcon->treeName;
4144 vol_info->retry = master_tcon->retry;
4145 vol_info->nocase = master_tcon->nocase;
4146 vol_info->local_lease = master_tcon->local_lease;
4147 vol_info->no_linux_ext = !master_tcon->unix_ext;
28e11bd8
JL
4148 vol_info->sectype = master_tcon->ses->sectype;
4149 vol_info->sign = master_tcon->ses->sign;
9d002df4 4150
8a8798a5
JL
4151 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4152 if (rc) {
4153 tcon = ERR_PTR(rc);
4154 goto out;
4155 }
9d002df4
JL
4156
4157 /* get a reference for the same TCP session */
3f9bcca7 4158 spin_lock(&cifs_tcp_ses_lock);
9d002df4 4159 ++master_tcon->ses->server->srv_count;
3f9bcca7 4160 spin_unlock(&cifs_tcp_ses_lock);
9d002df4
JL
4161
4162 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4163 if (IS_ERR(ses)) {
96daf2b0 4164 tcon = (struct cifs_tcon *)ses;
53e0e11e 4165 cifs_put_tcp_session(master_tcon->ses->server, 0);
9d002df4
JL
4166 goto out;
4167 }
4168
4169 tcon = cifs_get_tcon(ses, vol_info);
4170 if (IS_ERR(tcon)) {
4171 cifs_put_smb_ses(ses);
4172 goto out;
4173 }
4174
29e20f9c 4175 if (cap_unix(ses))
9d002df4
JL
4176 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4177out:
8a8798a5
JL
4178 kfree(vol_info->username);
4179 kfree(vol_info->password);
9d002df4
JL
4180 kfree(vol_info);
4181
4182 return tcon;
4183}
4184
96daf2b0 4185struct cifs_tcon *
9d002df4
JL
4186cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4187{
4188 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4189}
4190
b647c35f
JL
4191/* find and return a tlink with given uid */
4192static struct tcon_link *
6d4a0832 4193tlink_rb_search(struct rb_root *root, kuid_t uid)
b647c35f
JL
4194{
4195 struct rb_node *node = root->rb_node;
4196 struct tcon_link *tlink;
4197
4198 while (node) {
4199 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4200
6d4a0832 4201 if (uid_gt(tlink->tl_uid, uid))
b647c35f 4202 node = node->rb_left;
6d4a0832 4203 else if (uid_lt(tlink->tl_uid, uid))
b647c35f
JL
4204 node = node->rb_right;
4205 else
4206 return tlink;
4207 }
4208 return NULL;
4209}
4210
4211/* insert a tcon_link into the tree */
4212static void
4213tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4214{
4215 struct rb_node **new = &(root->rb_node), *parent = NULL;
4216 struct tcon_link *tlink;
4217
4218 while (*new) {
4219 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4220 parent = *new;
4221
6d4a0832 4222 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
b647c35f
JL
4223 new = &((*new)->rb_left);
4224 else
4225 new = &((*new)->rb_right);
4226 }
4227
4228 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4229 rb_insert_color(&new_tlink->tl_rbnode, root);
4230}
4231
9d002df4
JL
4232/*
4233 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4234 * current task.
4235 *
4236 * If the superblock doesn't refer to a multiuser mount, then just return
4237 * the master tcon for the mount.
4238 *
6ef933a3 4239 * First, search the rbtree for an existing tcon for this fsuid. If one
9d002df4
JL
4240 * exists, then check to see if it's pending construction. If it is then wait
4241 * for construction to complete. Once it's no longer pending, check to see if
4242 * it failed and either return an error or retry construction, depending on
4243 * the timeout.
4244 *
4245 * If one doesn't exist then insert a new tcon_link struct into the tree and
4246 * try to construct a new one.
4247 */
4248struct tcon_link *
4249cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4250{
4251 int ret;
6d4a0832 4252 kuid_t fsuid = current_fsuid();
9d002df4
JL
4253 struct tcon_link *tlink, *newtlink;
4254
4255 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4256 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4257
4258 spin_lock(&cifs_sb->tlink_tree_lock);
b647c35f 4259 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
9d002df4
JL
4260 if (tlink)
4261 cifs_get_tlink(tlink);
4262 spin_unlock(&cifs_sb->tlink_tree_lock);
4263
4264 if (tlink == NULL) {
4265 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4266 if (newtlink == NULL)
4267 return ERR_PTR(-ENOMEM);
b647c35f 4268 newtlink->tl_uid = fsuid;
9d002df4
JL
4269 newtlink->tl_tcon = ERR_PTR(-EACCES);
4270 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4271 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4272 cifs_get_tlink(newtlink);
4273
9d002df4
JL
4274 spin_lock(&cifs_sb->tlink_tree_lock);
4275 /* was one inserted after previous search? */
b647c35f 4276 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
9d002df4
JL
4277 if (tlink) {
4278 cifs_get_tlink(tlink);
4279 spin_unlock(&cifs_sb->tlink_tree_lock);
9d002df4
JL
4280 kfree(newtlink);
4281 goto wait_for_construction;
4282 }
9d002df4 4283 tlink = newtlink;
b647c35f
JL
4284 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4285 spin_unlock(&cifs_sb->tlink_tree_lock);
9d002df4
JL
4286 } else {
4287wait_for_construction:
4288 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
9d002df4
JL
4289 TASK_INTERRUPTIBLE);
4290 if (ret) {
4291 cifs_put_tlink(tlink);
74316201 4292 return ERR_PTR(-ERESTARTSYS);
9d002df4
JL
4293 }
4294
4295 /* if it's good, return it */
4296 if (!IS_ERR(tlink->tl_tcon))
4297 return tlink;
4298
4299 /* return error if we tried this already recently */
4300 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4301 cifs_put_tlink(tlink);
4302 return ERR_PTR(-EACCES);
4303 }
4304
4305 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4306 goto wait_for_construction;
4307 }
4308
4309 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4310 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4311 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4312
4313 if (IS_ERR(tlink->tl_tcon)) {
4314 cifs_put_tlink(tlink);
4315 return ERR_PTR(-EACCES);
4316 }
4317
4318 return tlink;
4319}
2de970ff
JL
4320
4321/*
4322 * periodic workqueue job that scans tcon_tree for a superblock and closes
4323 * out tcons.
4324 */
4325static void
4326cifs_prune_tlinks(struct work_struct *work)
4327{
4328 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4329 prune_tlinks.work);
b647c35f
JL
4330 struct rb_root *root = &cifs_sb->tlink_tree;
4331 struct rb_node *node = rb_first(root);
4332 struct rb_node *tmp;
4333 struct tcon_link *tlink;
2de970ff 4334
b647c35f
JL
4335 /*
4336 * Because we drop the spinlock in the loop in order to put the tlink
4337 * it's not guarded against removal of links from the tree. The only
4338 * places that remove entries from the tree are this function and
4339 * umounts. Because this function is non-reentrant and is canceled
4340 * before umount can proceed, this is safe.
4341 */
4342 spin_lock(&cifs_sb->tlink_tree_lock);
4343 node = rb_first(root);
4344 while (node != NULL) {
4345 tmp = node;
4346 node = rb_next(tmp);
4347 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4348
4349 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4350 atomic_read(&tlink->tl_count) != 0 ||
4351 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4352 continue;
2de970ff 4353
b647c35f
JL
4354 cifs_get_tlink(tlink);
4355 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4356 rb_erase(tmp, root);
4357
4358 spin_unlock(&cifs_sb->tlink_tree_lock);
4359 cifs_put_tlink(tlink);
4360 spin_lock(&cifs_sb->tlink_tree_lock);
4361 }
4362 spin_unlock(&cifs_sb->tlink_tree_lock);
2de970ff 4363
da472fc8 4364 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
2de970ff
JL
4365 TLINK_IDLE_EXPIRE);
4366}