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