xref: /linux/fs/smb/client/fs_context.c (revision 6b72cd9ef062702390fc96c469beea1729a5dffe)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2020, Microsoft Corporation.
4  *
5  *   Author(s): Steve French <stfrench@microsoft.com>
6  *              David Howells <dhowells@redhat.com>
7  */
8 
9 /*
10 #include <linux/module.h>
11 #include <linux/nsproxy.h>
12 #include <linux/slab.h>
13 #include <linux/magic.h>
14 #include <linux/security.h>
15 #include <net/net_namespace.h>
16 #ifdef CONFIG_CIFS_DFS_UPCALL
17 #include "dfs_cache.h"
18 #endif
19 */
20 
21 #include <linux/ctype.h>
22 #include <linux/fs_context.h>
23 #include <linux/fs_parser.h>
24 #include <linux/fs.h>
25 #include <linux/mount.h>
26 #include <linux/parser.h>
27 #include <linux/utsname.h>
28 #include "cifsfs.h"
29 #include "cifspdu.h"
30 #include "cifsglob.h"
31 #include "cifsproto.h"
32 #include "cifs_unicode.h"
33 #include "cifs_debug.h"
34 #include "cifs_fs_sb.h"
35 #include "ntlmssp.h"
36 #include "nterr.h"
37 #include "rfc1002pdu.h"
38 #include "fs_context.h"
39 
40 DEFINE_MUTEX(cifs_mount_mutex);
41 
42 static const match_table_t cifs_smb_version_tokens = {
43 	{ Smb_1, SMB1_VERSION_STRING },
44 	{ Smb_20, SMB20_VERSION_STRING},
45 	{ Smb_21, SMB21_VERSION_STRING },
46 	{ Smb_30, SMB30_VERSION_STRING },
47 	{ Smb_302, SMB302_VERSION_STRING },
48 	{ Smb_302, ALT_SMB302_VERSION_STRING },
49 	{ Smb_311, SMB311_VERSION_STRING },
50 	{ Smb_311, ALT_SMB311_VERSION_STRING },
51 	{ Smb_3any, SMB3ANY_VERSION_STRING },
52 	{ Smb_default, SMBDEFAULT_VERSION_STRING },
53 	{ Smb_version_err, NULL }
54 };
55 
56 static const match_table_t cifs_secflavor_tokens = {
57 	{ Opt_sec_krb5, "krb5" },
58 	{ Opt_sec_krb5i, "krb5i" },
59 	{ Opt_sec_krb5p, "krb5p" },
60 	{ Opt_sec_ntlmsspi, "ntlmsspi" },
61 	{ Opt_sec_ntlmssp, "ntlmssp" },
62 	{ Opt_sec_ntlmv2, "nontlm" },
63 	{ Opt_sec_ntlmv2, "ntlmv2" },
64 	{ Opt_sec_ntlmv2i, "ntlmv2i" },
65 	{ Opt_sec_none, "none" },
66 
67 	{ Opt_sec_err, NULL }
68 };
69 
70 static const match_table_t cifs_upcall_target = {
71 	{ Opt_upcall_target_mount, "mount" },
72 	{ Opt_upcall_target_application, "app" },
73 	{ Opt_upcall_target_err, NULL }
74 };
75 
76 const struct fs_parameter_spec smb3_fs_parameters[] = {
77 	/* Mount options that take no arguments */
78 	fsparam_flag_no("user_xattr", Opt_user_xattr),
79 	fsparam_flag_no("forceuid", Opt_forceuid),
80 	fsparam_flag_no("multichannel", Opt_multichannel),
81 	fsparam_flag_no("forcegid", Opt_forcegid),
82 	fsparam_flag("noblocksend", Opt_noblocksend),
83 	fsparam_flag("noautotune", Opt_noautotune),
84 	fsparam_flag("nolease", Opt_nolease),
85 	fsparam_flag_no("hard", Opt_hard),
86 	fsparam_flag_no("soft", Opt_soft),
87 	fsparam_flag_no("perm", Opt_perm),
88 	fsparam_flag("nodelete", Opt_nodelete),
89 	fsparam_flag_no("mapposix", Opt_mapposix),
90 	fsparam_flag("mapchars", Opt_mapchars),
91 	fsparam_flag("nomapchars", Opt_nomapchars),
92 	fsparam_flag_no("sfu", Opt_sfu),
93 	fsparam_flag("nodfs", Opt_nodfs),
94 	fsparam_flag_no("posixpaths", Opt_posixpaths),
95 	fsparam_flag_no("unix", Opt_unix),
96 	fsparam_flag_no("linux", Opt_unix),
97 	fsparam_flag_no("posix", Opt_unix),
98 	fsparam_flag("nocase", Opt_nocase),
99 	fsparam_flag("ignorecase", Opt_nocase),
100 	fsparam_flag_no("brl", Opt_brl),
101 	fsparam_flag_no("handlecache", Opt_handlecache),
102 	fsparam_flag("forcemandatorylock", Opt_forcemandatorylock),
103 	fsparam_flag("forcemand", Opt_forcemandatorylock),
104 	fsparam_flag("setuidfromacl", Opt_setuidfromacl),
105 	fsparam_flag("idsfromsid", Opt_setuidfromacl),
106 	fsparam_flag_no("setuids", Opt_setuids),
107 	fsparam_flag_no("dynperm", Opt_dynperm),
108 	fsparam_flag_no("intr", Opt_intr),
109 	fsparam_flag_no("strictsync", Opt_strictsync),
110 	fsparam_flag_no("serverino", Opt_serverino),
111 	fsparam_flag("rwpidforward", Opt_rwpidforward),
112 	fsparam_flag("cifsacl", Opt_cifsacl),
113 	fsparam_flag_no("acl", Opt_acl),
114 	fsparam_flag("locallease", Opt_locallease),
115 	fsparam_flag("sign", Opt_sign),
116 	fsparam_flag("ignore_signature", Opt_ignore_signature),
117 	fsparam_flag("signloosely", Opt_ignore_signature),
118 	fsparam_flag("seal", Opt_seal),
119 	fsparam_flag("noac", Opt_noac),
120 	fsparam_flag("fsc", Opt_fsc),
121 	fsparam_flag("mfsymlinks", Opt_mfsymlinks),
122 	fsparam_flag("multiuser", Opt_multiuser),
123 	fsparam_flag("sloppy", Opt_sloppy),
124 	fsparam_flag("nosharesock", Opt_nosharesock),
125 	fsparam_flag_no("persistenthandles", Opt_persistent),
126 	fsparam_flag_no("resilienthandles", Opt_resilient),
127 	fsparam_flag_no("tcpnodelay", Opt_tcp_nodelay),
128 	fsparam_flag("nosparse", Opt_nosparse),
129 	fsparam_flag("domainauto", Opt_domainauto),
130 	fsparam_flag("rdma", Opt_rdma),
131 	fsparam_flag("modesid", Opt_modesid),
132 	fsparam_flag("modefromsid", Opt_modesid),
133 	fsparam_flag("rootfs", Opt_rootfs),
134 	fsparam_flag("compress", Opt_compress),
135 	fsparam_flag("witness", Opt_witness),
136 	fsparam_flag_no("nativesocket", Opt_nativesocket),
137 
138 	/* Mount options which take uid or gid */
139 	fsparam_uid("backupuid", Opt_backupuid),
140 	fsparam_gid("backupgid", Opt_backupgid),
141 	fsparam_uid("uid", Opt_uid),
142 	fsparam_uid("cruid", Opt_cruid),
143 	fsparam_gid("gid", Opt_gid),
144 
145 	/* Mount options which take numeric value */
146 	fsparam_u32("file_mode", Opt_file_mode),
147 	fsparam_u32("dirmode", Opt_dirmode),
148 	fsparam_u32("dir_mode", Opt_dirmode),
149 	fsparam_u32("port", Opt_port),
150 	fsparam_u32("min_enc_offload", Opt_min_enc_offload),
151 	fsparam_u32("retrans", Opt_retrans),
152 	fsparam_u32("esize", Opt_min_enc_offload),
153 	fsparam_u32("bsize", Opt_blocksize),
154 	fsparam_u32("rasize", Opt_rasize),
155 	fsparam_u32("rsize", Opt_rsize),
156 	fsparam_u32("wsize", Opt_wsize),
157 	fsparam_u32("actimeo", Opt_actimeo),
158 	fsparam_u32("acdirmax", Opt_acdirmax),
159 	fsparam_u32("acregmax", Opt_acregmax),
160 	fsparam_u32("closetimeo", Opt_closetimeo),
161 	fsparam_u32("echo_interval", Opt_echo_interval),
162 	fsparam_u32("max_credits", Opt_max_credits),
163 	fsparam_u32("max_cached_dirs", Opt_max_cached_dirs),
164 	fsparam_u32("handletimeout", Opt_handletimeout),
165 	fsparam_u64("snapshot", Opt_snapshot),
166 	fsparam_u32("max_channels", Opt_max_channels),
167 
168 	/* Mount options which take string value */
169 	fsparam_string("source", Opt_source),
170 	fsparam_string("user", Opt_user),
171 	fsparam_string("username", Opt_user),
172 	fsparam_string("pass", Opt_pass),
173 	fsparam_string("password", Opt_pass),
174 	fsparam_string("password2", Opt_pass2),
175 	fsparam_string("ip", Opt_ip),
176 	fsparam_string("addr", Opt_ip),
177 	fsparam_string("domain", Opt_domain),
178 	fsparam_string("dom", Opt_domain),
179 	fsparam_string("srcaddr", Opt_srcaddr),
180 	fsparam_string("iocharset", Opt_iocharset),
181 	fsparam_string("netbiosname", Opt_netbiosname),
182 	fsparam_string("servern", Opt_servern),
183 	fsparam_string("ver", Opt_ver),
184 	fsparam_string("vers", Opt_vers),
185 	fsparam_string("sec", Opt_sec),
186 	fsparam_string("cache", Opt_cache),
187 	fsparam_string("reparse", Opt_reparse),
188 	fsparam_string("upcall_target", Opt_upcalltarget),
189 	fsparam_string("symlink", Opt_symlink),
190 	fsparam_string("symlinkroot", Opt_symlinkroot),
191 
192 	/* Arguments that should be ignored */
193 	fsparam_flag("guest", Opt_ignore),
194 	fsparam_flag("noatime", Opt_ignore),
195 	fsparam_flag("relatime", Opt_ignore),
196 	fsparam_flag("_netdev", Opt_ignore),
197 	fsparam_flag_no("suid", Opt_ignore),
198 	fsparam_flag_no("exec", Opt_ignore),
199 	fsparam_flag_no("dev", Opt_ignore),
200 	fsparam_flag_no("mand", Opt_ignore),
201 	fsparam_flag_no("auto", Opt_ignore),
202 	fsparam_string("cred", Opt_ignore),
203 	fsparam_string("credentials", Opt_ignore),
204 	/*
205 	 * UNC and prefixpath is now extracted from Opt_source
206 	 * in the new mount API so we can just ignore them going forward.
207 	 */
208 	fsparam_string("unc", Opt_ignore),
209 	fsparam_string("prefixpath", Opt_ignore),
210 	{}
211 };
212 
213 static int
214 cifs_parse_security_flavors(struct fs_context *fc, char *value, struct smb3_fs_context *ctx)
215 {
216 
217 	substring_t args[MAX_OPT_ARGS];
218 
219 	/*
220 	 * With mount options, the last one should win. Reset any existing
221 	 * settings back to default.
222 	 */
223 	ctx->sectype = Unspecified;
224 	ctx->sign = false;
225 
226 	switch (match_token(value, cifs_secflavor_tokens, args)) {
227 	case Opt_sec_krb5p:
228 		cifs_errorf(fc, "sec=krb5p is not supported. Use sec=krb5,seal instead\n");
229 		return 1;
230 	case Opt_sec_krb5i:
231 		ctx->sign = true;
232 		fallthrough;
233 	case Opt_sec_krb5:
234 		ctx->sectype = Kerberos;
235 		break;
236 	case Opt_sec_ntlmsspi:
237 		ctx->sign = true;
238 		fallthrough;
239 	case Opt_sec_ntlmssp:
240 		ctx->sectype = RawNTLMSSP;
241 		break;
242 	case Opt_sec_ntlmv2i:
243 		ctx->sign = true;
244 		fallthrough;
245 	case Opt_sec_ntlmv2:
246 		ctx->sectype = NTLMv2;
247 		break;
248 	case Opt_sec_none:
249 		ctx->nullauth = 1;
250 		kfree(ctx->username);
251 		ctx->username = NULL;
252 		break;
253 	default:
254 		cifs_errorf(fc, "bad security option: %s\n", value);
255 		return 1;
256 	}
257 
258 	return 0;
259 }
260 
261 static int
262 cifs_parse_upcall_target(struct fs_context *fc, char *value, struct smb3_fs_context *ctx)
263 {
264 	substring_t args[MAX_OPT_ARGS];
265 
266 	ctx->upcall_target = UPTARGET_UNSPECIFIED;
267 
268 	switch (match_token(value, cifs_upcall_target, args)) {
269 	case Opt_upcall_target_mount:
270 		ctx->upcall_target = UPTARGET_MOUNT;
271 		break;
272 	case Opt_upcall_target_application:
273 		ctx->upcall_target = UPTARGET_APP;
274 		break;
275 
276 	default:
277 		cifs_errorf(fc, "bad upcall target: %s\n", value);
278 		return 1;
279 	}
280 
281 	return 0;
282 }
283 
284 static const match_table_t cifs_cacheflavor_tokens = {
285 	{ Opt_cache_loose, "loose" },
286 	{ Opt_cache_strict, "strict" },
287 	{ Opt_cache_none, "none" },
288 	{ Opt_cache_ro, "ro" },
289 	{ Opt_cache_rw, "singleclient" },
290 	{ Opt_cache_err, NULL }
291 };
292 
293 static int
294 cifs_parse_cache_flavor(struct fs_context *fc, char *value, struct smb3_fs_context *ctx)
295 {
296 	substring_t args[MAX_OPT_ARGS];
297 
298 	switch (match_token(value, cifs_cacheflavor_tokens, args)) {
299 	case Opt_cache_loose:
300 		ctx->direct_io = false;
301 		ctx->strict_io = false;
302 		ctx->cache_ro = false;
303 		ctx->cache_rw = false;
304 		break;
305 	case Opt_cache_strict:
306 		ctx->direct_io = false;
307 		ctx->strict_io = true;
308 		ctx->cache_ro = false;
309 		ctx->cache_rw = false;
310 		break;
311 	case Opt_cache_none:
312 		ctx->direct_io = true;
313 		ctx->strict_io = false;
314 		ctx->cache_ro = false;
315 		ctx->cache_rw = false;
316 		break;
317 	case Opt_cache_ro:
318 		ctx->direct_io = false;
319 		ctx->strict_io = false;
320 		ctx->cache_ro = true;
321 		ctx->cache_rw = false;
322 		break;
323 	case Opt_cache_rw:
324 		ctx->direct_io = false;
325 		ctx->strict_io = false;
326 		ctx->cache_ro = false;
327 		ctx->cache_rw = true;
328 		break;
329 	default:
330 		cifs_errorf(fc, "bad cache= option: %s\n", value);
331 		return 1;
332 	}
333 	return 0;
334 }
335 
336 static const match_table_t reparse_flavor_tokens = {
337 	{ Opt_reparse_default,	"default" },
338 	{ Opt_reparse_none,	"none" },
339 	{ Opt_reparse_nfs,	"nfs" },
340 	{ Opt_reparse_wsl,	"wsl" },
341 	{ Opt_reparse_err,	NULL },
342 };
343 
344 static int parse_reparse_flavor(struct fs_context *fc, char *value,
345 				struct smb3_fs_context *ctx)
346 {
347 	substring_t args[MAX_OPT_ARGS];
348 
349 	switch (match_token(value, reparse_flavor_tokens, args)) {
350 	case Opt_reparse_default:
351 		ctx->reparse_type = CIFS_REPARSE_TYPE_DEFAULT;
352 		break;
353 	case Opt_reparse_none:
354 		ctx->reparse_type = CIFS_REPARSE_TYPE_NONE;
355 		break;
356 	case Opt_reparse_nfs:
357 		ctx->reparse_type = CIFS_REPARSE_TYPE_NFS;
358 		break;
359 	case Opt_reparse_wsl:
360 		ctx->reparse_type = CIFS_REPARSE_TYPE_WSL;
361 		break;
362 	default:
363 		cifs_errorf(fc, "bad reparse= option: %s\n", value);
364 		return 1;
365 	}
366 	return 0;
367 }
368 
369 static const match_table_t symlink_flavor_tokens = {
370 	{ Opt_symlink_default,		"default" },
371 	{ Opt_symlink_none,		"none" },
372 	{ Opt_symlink_native,		"native" },
373 	{ Opt_symlink_unix,		"unix" },
374 	{ Opt_symlink_mfsymlinks,	"mfsymlinks" },
375 	{ Opt_symlink_sfu,		"sfu" },
376 	{ Opt_symlink_nfs,		"nfs" },
377 	{ Opt_symlink_wsl,		"wsl" },
378 	{ Opt_symlink_err,		NULL },
379 };
380 
381 static int parse_symlink_flavor(struct fs_context *fc, char *value,
382 				struct smb3_fs_context *ctx)
383 {
384 	substring_t args[MAX_OPT_ARGS];
385 
386 	switch (match_token(value, symlink_flavor_tokens, args)) {
387 	case Opt_symlink_default:
388 		ctx->symlink_type = CIFS_SYMLINK_TYPE_DEFAULT;
389 		break;
390 	case Opt_symlink_none:
391 		ctx->symlink_type = CIFS_SYMLINK_TYPE_NONE;
392 		break;
393 	case Opt_symlink_native:
394 		ctx->symlink_type = CIFS_SYMLINK_TYPE_NATIVE;
395 		break;
396 	case Opt_symlink_unix:
397 		ctx->symlink_type = CIFS_SYMLINK_TYPE_UNIX;
398 		break;
399 	case Opt_symlink_mfsymlinks:
400 		ctx->symlink_type = CIFS_SYMLINK_TYPE_MFSYMLINKS;
401 		break;
402 	case Opt_symlink_sfu:
403 		ctx->symlink_type = CIFS_SYMLINK_TYPE_SFU;
404 		break;
405 	case Opt_symlink_nfs:
406 		ctx->symlink_type = CIFS_SYMLINK_TYPE_NFS;
407 		break;
408 	case Opt_symlink_wsl:
409 		ctx->symlink_type = CIFS_SYMLINK_TYPE_WSL;
410 		break;
411 	default:
412 		cifs_errorf(fc, "bad symlink= option: %s\n", value);
413 		return 1;
414 	}
415 	return 0;
416 }
417 
418 #define DUP_CTX_STR(field)						\
419 do {									\
420 	if (ctx->field) {						\
421 		new_ctx->field = kstrdup(ctx->field, GFP_ATOMIC);	\
422 		if (new_ctx->field == NULL) {				\
423 			smb3_cleanup_fs_context_contents(new_ctx);	\
424 			return -ENOMEM;					\
425 		}							\
426 	}								\
427 } while (0)
428 
429 int
430 smb3_fs_context_dup(struct smb3_fs_context *new_ctx, struct smb3_fs_context *ctx)
431 {
432 	memcpy(new_ctx, ctx, sizeof(*ctx));
433 	new_ctx->prepath = NULL;
434 	new_ctx->nodename = NULL;
435 	new_ctx->username = NULL;
436 	new_ctx->password = NULL;
437 	new_ctx->password2 = NULL;
438 	new_ctx->server_hostname = NULL;
439 	new_ctx->domainname = NULL;
440 	new_ctx->UNC = NULL;
441 	new_ctx->source = NULL;
442 	new_ctx->iocharset = NULL;
443 	new_ctx->leaf_fullpath = NULL;
444 	new_ctx->dns_dom = NULL;
445 	new_ctx->symlinkroot = NULL;
446 	/*
447 	 * Make sure to stay in sync with smb3_cleanup_fs_context_contents()
448 	 */
449 	DUP_CTX_STR(prepath);
450 	DUP_CTX_STR(username);
451 	DUP_CTX_STR(password);
452 	DUP_CTX_STR(password2);
453 	DUP_CTX_STR(server_hostname);
454 	DUP_CTX_STR(UNC);
455 	DUP_CTX_STR(source);
456 	DUP_CTX_STR(domainname);
457 	DUP_CTX_STR(nodename);
458 	DUP_CTX_STR(iocharset);
459 	DUP_CTX_STR(leaf_fullpath);
460 	DUP_CTX_STR(dns_dom);
461 	DUP_CTX_STR(symlinkroot);
462 
463 	return 0;
464 }
465 
466 static int
467 cifs_parse_smb_version(struct fs_context *fc, char *value, struct smb3_fs_context *ctx, bool is_smb3)
468 {
469 	substring_t args[MAX_OPT_ARGS];
470 
471 	switch (match_token(value, cifs_smb_version_tokens, args)) {
472 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
473 	case Smb_1:
474 		if (disable_legacy_dialects) {
475 			cifs_errorf(fc, "mount with legacy dialect disabled\n");
476 			return 1;
477 		}
478 		if (is_smb3) {
479 			cifs_errorf(fc, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
480 			return 1;
481 		}
482 		cifs_errorf(fc, "Use of the less secure dialect vers=1.0 is not recommended unless required for access to very old servers\n");
483 		ctx->ops = &smb1_operations;
484 		ctx->vals = &smb1_values;
485 		break;
486 	case Smb_20:
487 		if (disable_legacy_dialects) {
488 			cifs_errorf(fc, "mount with legacy dialect disabled\n");
489 			return 1;
490 		}
491 		if (is_smb3) {
492 			cifs_errorf(fc, "vers=2.0 not permitted when mounting with smb3\n");
493 			return 1;
494 		}
495 		ctx->ops = &smb20_operations;
496 		ctx->vals = &smb20_values;
497 		break;
498 #else
499 	case Smb_1:
500 		cifs_errorf(fc, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
501 		return 1;
502 	case Smb_20:
503 		cifs_errorf(fc, "vers=2.0 mount not permitted when legacy dialects disabled\n");
504 		return 1;
505 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
506 	case Smb_21:
507 		ctx->ops = &smb21_operations;
508 		ctx->vals = &smb21_values;
509 		break;
510 	case Smb_30:
511 		ctx->ops = &smb30_operations;
512 		ctx->vals = &smb30_values;
513 		break;
514 	case Smb_302:
515 		ctx->ops = &smb30_operations; /* currently identical with 3.0 */
516 		ctx->vals = &smb302_values;
517 		break;
518 	case Smb_311:
519 		ctx->ops = &smb311_operations;
520 		ctx->vals = &smb311_values;
521 		break;
522 	case Smb_3any:
523 		ctx->ops = &smb30_operations; /* currently identical with 3.0 */
524 		ctx->vals = &smb3any_values;
525 		break;
526 	case Smb_default:
527 		ctx->ops = &smb30_operations;
528 		ctx->vals = &smbdefault_values;
529 		break;
530 	default:
531 		cifs_errorf(fc, "Unknown vers= option specified: %s\n", value);
532 		return 1;
533 	}
534 	return 0;
535 }
536 
537 int smb3_parse_opt(const char *options, const char *key, char **val)
538 {
539 	int rc = -ENOENT;
540 	char *opts, *orig, *p;
541 
542 	orig = opts = kstrdup(options, GFP_KERNEL);
543 	if (!opts)
544 		return -ENOMEM;
545 
546 	while ((p = strsep(&opts, ","))) {
547 		char *nval;
548 
549 		if (!*p)
550 			continue;
551 		if (strncasecmp(p, key, strlen(key)))
552 			continue;
553 		nval = strchr(p, '=');
554 		if (nval) {
555 			if (nval == p)
556 				continue;
557 			*nval++ = 0;
558 			*val = kstrdup(nval, GFP_KERNEL);
559 			rc = !*val ? -ENOMEM : 0;
560 			goto out;
561 		}
562 	}
563 out:
564 	kfree(orig);
565 	return rc;
566 }
567 
568 /*
569  * Remove duplicate path delimiters. Windows is supposed to do that
570  * but there are some bugs that prevent rename from working if there are
571  * multiple delimiters.
572  *
573  * Return a sanitized duplicate of @path or NULL for empty prefix paths.
574  * Otherwise, return ERR_PTR.
575  *
576  * @gfp indicates the GFP_* flags for kstrdup.
577  * The caller is responsible for freeing the original.
578  */
579 #define IS_DELIM(c) ((c) == '/' || (c) == '\\')
580 char *cifs_sanitize_prepath(char *prepath, gfp_t gfp)
581 {
582 	char *cursor1 = prepath, *cursor2 = prepath;
583 	char *s;
584 
585 	/* skip all prepended delimiters */
586 	while (IS_DELIM(*cursor1))
587 		cursor1++;
588 
589 	/* copy the first letter */
590 	*cursor2 = *cursor1;
591 
592 	/* copy the remainder... */
593 	while (*(cursor1++)) {
594 		/* ... skipping all duplicated delimiters */
595 		if (IS_DELIM(*cursor1) && IS_DELIM(*cursor2))
596 			continue;
597 		*(++cursor2) = *cursor1;
598 	}
599 
600 	/* if the last character is a delimiter, skip it */
601 	if (IS_DELIM(*(cursor2 - 1)))
602 		cursor2--;
603 
604 	*cursor2 = '\0';
605 	if (!*prepath)
606 		return NULL;
607 	s = kstrdup(prepath, gfp);
608 	if (!s)
609 		return ERR_PTR(-ENOMEM);
610 	return s;
611 }
612 
613 /*
614  * Return full path based on the values of @ctx->{UNC,prepath}.
615  *
616  * It is assumed that both values were already parsed by smb3_parse_devname().
617  */
618 char *smb3_fs_context_fullpath(const struct smb3_fs_context *ctx, char dirsep)
619 {
620 	size_t ulen, plen;
621 	char *s;
622 
623 	ulen = strlen(ctx->UNC);
624 	plen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
625 
626 	s = kmalloc(ulen + plen + 1, GFP_KERNEL);
627 	if (!s)
628 		return ERR_PTR(-ENOMEM);
629 	memcpy(s, ctx->UNC, ulen);
630 	if (plen) {
631 		s[ulen] = dirsep;
632 		memcpy(s + ulen + 1, ctx->prepath, plen);
633 	}
634 	s[ulen + plen] = '\0';
635 	convert_delimiter(s, dirsep);
636 	return s;
637 }
638 
639 /*
640  * Parse a devname into substrings and populate the ctx->UNC and ctx->prepath
641  * fields with the result. Returns 0 on success and an error otherwise
642  * (e.g. ENOMEM or EINVAL)
643  */
644 int
645 smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
646 {
647 	char *pos;
648 	const char *delims = "/\\";
649 	size_t len;
650 	int rc;
651 
652 	if (unlikely(!devname || !*devname)) {
653 		cifs_dbg(VFS, "Device name not specified\n");
654 		return -EINVAL;
655 	}
656 
657 	/* make sure we have a valid UNC double delimiter prefix */
658 	len = strspn(devname, delims);
659 	if (len != 2)
660 		return -EINVAL;
661 
662 	/* find delimiter between host and sharename */
663 	pos = strpbrk(devname + 2, delims);
664 	if (!pos)
665 		return -EINVAL;
666 
667 	/* record the server hostname */
668 	kfree(ctx->server_hostname);
669 	ctx->server_hostname = kstrndup(devname + 2, pos - devname - 2, GFP_KERNEL);
670 	if (!ctx->server_hostname)
671 		return -ENOMEM;
672 
673 	/* skip past delimiter */
674 	++pos;
675 
676 	/* now go until next delimiter or end of string */
677 	len = strcspn(pos, delims);
678 	if (!len)
679 		return -EINVAL;
680 
681 	/* move "pos" up to delimiter or NULL */
682 	pos += len;
683 	kfree(ctx->UNC);
684 	ctx->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
685 	if (!ctx->UNC)
686 		return -ENOMEM;
687 
688 	convert_delimiter(ctx->UNC, '\\');
689 
690 	/* skip any delimiter */
691 	if (*pos == '/' || *pos == '\\')
692 		pos++;
693 
694 	kfree(ctx->prepath);
695 	ctx->prepath = NULL;
696 
697 	/* If pos is NULL then no prepath */
698 	if (!*pos)
699 		return 0;
700 
701 	ctx->prepath = cifs_sanitize_prepath(pos, GFP_KERNEL);
702 	if (IS_ERR(ctx->prepath)) {
703 		rc = PTR_ERR(ctx->prepath);
704 		ctx->prepath = NULL;
705 		return rc;
706 	}
707 
708 	return 0;
709 }
710 
711 static void smb3_fs_context_free(struct fs_context *fc);
712 static int smb3_fs_context_parse_param(struct fs_context *fc,
713 				       struct fs_parameter *param);
714 static int smb3_fs_context_parse_monolithic(struct fs_context *fc,
715 					    void *data);
716 static int smb3_get_tree(struct fs_context *fc);
717 static int smb3_reconfigure(struct fs_context *fc);
718 
719 static const struct fs_context_operations smb3_fs_context_ops = {
720 	.free			= smb3_fs_context_free,
721 	.parse_param		= smb3_fs_context_parse_param,
722 	.parse_monolithic	= smb3_fs_context_parse_monolithic,
723 	.get_tree		= smb3_get_tree,
724 	.reconfigure		= smb3_reconfigure,
725 };
726 
727 /*
728  * Parse a monolithic block of data from sys_mount().
729  * smb3_fs_context_parse_monolithic - Parse key[=val][,key[=val]]* mount data
730  * @ctx: The superblock configuration to fill in.
731  * @data: The data to parse
732  *
733  * Parse a blob of data that's in key[=val][,key[=val]]* form.  This can be
734  * called from the ->monolithic_mount_data() fs_context operation.
735  *
736  * Returns 0 on success or the error returned by the ->parse_option() fs_context
737  * operation on failure.
738  */
739 static int smb3_fs_context_parse_monolithic(struct fs_context *fc,
740 					   void *data)
741 {
742 	char *options = data, *key;
743 	int ret = 0;
744 
745 	if (!options)
746 		return 0;
747 
748 	ret = security_sb_eat_lsm_opts(options, &fc->security);
749 	if (ret)
750 		return ret;
751 
752 	/* BB Need to add support for sep= here TBD */
753 	while ((key = strsep(&options, ",")) != NULL) {
754 		size_t len;
755 		char *value;
756 
757 		if (*key == 0)
758 			break;
759 
760 		/* Check if following character is the deliminator If yes,
761 		 * we have encountered a double deliminator reset the NULL
762 		 * character to the deliminator
763 		 */
764 		while (options && options[0] == ',') {
765 			len = strlen(key);
766 			strcpy(key + len, options);
767 			options = strchr(options, ',');
768 			if (options)
769 				*options++ = 0;
770 		}
771 
772 
773 		len = 0;
774 		value = strchr(key, '=');
775 		if (value) {
776 			if (value == key)
777 				continue;
778 			*value++ = 0;
779 			len = strlen(value);
780 		}
781 
782 		ret = vfs_parse_fs_string(fc, key, value, len);
783 		if (ret < 0)
784 			break;
785 	}
786 
787 	return ret;
788 }
789 
790 /*
791  * Validate the preparsed information in the config.
792  */
793 static int smb3_fs_context_validate(struct fs_context *fc)
794 {
795 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
796 
797 	if (ctx->rdma && ctx->vals->protocol_id < SMB30_PROT_ID) {
798 		cifs_errorf(fc, "SMB Direct requires Version >=3.0\n");
799 		return -EOPNOTSUPP;
800 	}
801 
802 #ifndef CONFIG_KEYS
803 	/* Muliuser mounts require CONFIG_KEYS support */
804 	if (ctx->multiuser) {
805 		cifs_errorf(fc, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
806 		return -1;
807 	}
808 #endif
809 
810 	if (ctx->got_version == false)
811 		pr_warn_once("No dialect specified on mount. Default has changed to a more secure dialect, SMB2.1 or later (e.g. SMB3.1.1), from CIFS (SMB1). To use the less secure SMB1 dialect to access old servers which do not support SMB3.1.1 (or even SMB3 or SMB2.1) specify vers=1.0 on mount.\n");
812 
813 
814 	if (!ctx->UNC) {
815 		cifs_errorf(fc, "CIFS mount error: No usable UNC path provided in device string!\n");
816 		return -1;
817 	}
818 
819 	/* make sure UNC has a share name */
820 	if (strlen(ctx->UNC) < 3 || !strchr(ctx->UNC + 3, '\\')) {
821 		cifs_errorf(fc, "Malformed UNC. Unable to find share name.\n");
822 		return -ENOENT;
823 	}
824 
825 	if (!ctx->got_ip) {
826 		int len;
827 		const char *slash;
828 
829 		/* No ip= option specified? Try to get it from UNC */
830 		/* Use the address part of the UNC. */
831 		slash = strchr(&ctx->UNC[2], '\\');
832 		len = slash - &ctx->UNC[2];
833 		if (!cifs_convert_address((struct sockaddr *)&ctx->dstaddr,
834 					  &ctx->UNC[2], len)) {
835 			pr_err("Unable to determine destination address\n");
836 			return -EHOSTUNREACH;
837 		}
838 	}
839 
840 	/* set the port that we got earlier */
841 	cifs_set_port((struct sockaddr *)&ctx->dstaddr, ctx->port);
842 
843 	if (ctx->uid_specified && !ctx->forceuid_specified) {
844 		ctx->override_uid = 1;
845 		pr_notice("enabling forceuid mount option implicitly because uid= option is specified\n");
846 	}
847 
848 	if (ctx->gid_specified && !ctx->forcegid_specified) {
849 		ctx->override_gid = 1;
850 		pr_notice("enabling forcegid mount option implicitly because gid= option is specified\n");
851 	}
852 
853 	if (ctx->override_uid && !ctx->uid_specified) {
854 		ctx->override_uid = 0;
855 		pr_notice("ignoring forceuid mount option specified with no uid= option\n");
856 	}
857 
858 	if (ctx->override_gid && !ctx->gid_specified) {
859 		ctx->override_gid = 0;
860 		pr_notice("ignoring forcegid mount option specified with no gid= option\n");
861 	}
862 
863 	return 0;
864 }
865 
866 static int smb3_get_tree_common(struct fs_context *fc)
867 {
868 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
869 	struct dentry *root;
870 	int rc = 0;
871 
872 	root = cifs_smb3_do_mount(fc->fs_type, 0, ctx);
873 	if (IS_ERR(root))
874 		return PTR_ERR(root);
875 
876 	fc->root = root;
877 
878 	return rc;
879 }
880 
881 /*
882  * Create an SMB3 superblock from the parameters passed.
883  */
884 static int smb3_get_tree(struct fs_context *fc)
885 {
886 	int err = smb3_fs_context_validate(fc);
887 	int ret;
888 
889 	if (err)
890 		return err;
891 	cifs_mount_lock();
892 	ret = smb3_get_tree_common(fc);
893 	cifs_mount_unlock();
894 	return ret;
895 }
896 
897 static void smb3_fs_context_free(struct fs_context *fc)
898 {
899 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
900 
901 	smb3_cleanup_fs_context(ctx);
902 }
903 
904 /*
905  * Compare the old and new proposed context during reconfigure
906  * and check if the changes are compatible.
907  */
908 static int smb3_verify_reconfigure_ctx(struct fs_context *fc,
909 				       struct smb3_fs_context *new_ctx,
910 				       struct smb3_fs_context *old_ctx, bool need_recon)
911 {
912 	if (new_ctx->posix_paths != old_ctx->posix_paths) {
913 		cifs_errorf(fc, "can not change posixpaths during remount\n");
914 		return -EINVAL;
915 	}
916 	if (new_ctx->sectype != old_ctx->sectype) {
917 		cifs_errorf(fc, "can not change sec during remount\n");
918 		return -EINVAL;
919 	}
920 	if (new_ctx->multiuser != old_ctx->multiuser) {
921 		cifs_errorf(fc, "can not change multiuser during remount\n");
922 		return -EINVAL;
923 	}
924 	if (new_ctx->UNC &&
925 	    (!old_ctx->UNC || strcmp(new_ctx->UNC, old_ctx->UNC))) {
926 		cifs_errorf(fc, "can not change UNC during remount\n");
927 		return -EINVAL;
928 	}
929 	if (new_ctx->username &&
930 	    (!old_ctx->username || strcmp(new_ctx->username, old_ctx->username))) {
931 		cifs_errorf(fc, "can not change username during remount\n");
932 		return -EINVAL;
933 	}
934 	if (new_ctx->password &&
935 	    (!old_ctx->password || strcmp(new_ctx->password, old_ctx->password))) {
936 		if (need_recon == false) {
937 			cifs_errorf(fc,
938 				    "can not change password of active session during remount\n");
939 			return -EINVAL;
940 		} else if (old_ctx->sectype == Kerberos) {
941 			cifs_errorf(fc,
942 				    "can not change password for Kerberos via remount\n");
943 			return -EINVAL;
944 		}
945 	}
946 	if (new_ctx->domainname &&
947 	    (!old_ctx->domainname || strcmp(new_ctx->domainname, old_ctx->domainname))) {
948 		cifs_errorf(fc, "can not change domainname during remount\n");
949 		return -EINVAL;
950 	}
951 	if (strcmp(new_ctx->workstation_name, old_ctx->workstation_name)) {
952 		cifs_errorf(fc, "can not change workstation_name during remount\n");
953 		return -EINVAL;
954 	}
955 	if (new_ctx->nodename &&
956 	    (!old_ctx->nodename || strcmp(new_ctx->nodename, old_ctx->nodename))) {
957 		cifs_errorf(fc, "can not change nodename during remount\n");
958 		return -EINVAL;
959 	}
960 	if (new_ctx->iocharset &&
961 	    (!old_ctx->iocharset || strcmp(new_ctx->iocharset, old_ctx->iocharset))) {
962 		cifs_errorf(fc, "can not change iocharset during remount\n");
963 		return -EINVAL;
964 	}
965 
966 	return 0;
967 }
968 
969 #define STEAL_STRING(cifs_sb, ctx, field)				\
970 do {									\
971 	kfree(ctx->field);						\
972 	ctx->field = cifs_sb->ctx->field;				\
973 	cifs_sb->ctx->field = NULL;					\
974 } while (0)
975 
976 #define STEAL_STRING_SENSITIVE(cifs_sb, ctx, field)			\
977 do {									\
978 	kfree_sensitive(ctx->field);					\
979 	ctx->field = cifs_sb->ctx->field;				\
980 	cifs_sb->ctx->field = NULL;					\
981 } while (0)
982 
983 int smb3_sync_session_ctx_passwords(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
984 {
985 	if (ses->password &&
986 	    cifs_sb->ctx->password &&
987 	    strcmp(ses->password, cifs_sb->ctx->password)) {
988 		kfree_sensitive(cifs_sb->ctx->password);
989 		cifs_sb->ctx->password = kstrdup(ses->password, GFP_KERNEL);
990 		if (!cifs_sb->ctx->password)
991 			return -ENOMEM;
992 	}
993 	if (ses->password2 &&
994 	    cifs_sb->ctx->password2 &&
995 	    strcmp(ses->password2, cifs_sb->ctx->password2)) {
996 		kfree_sensitive(cifs_sb->ctx->password2);
997 		cifs_sb->ctx->password2 = kstrdup(ses->password2, GFP_KERNEL);
998 		if (!cifs_sb->ctx->password2) {
999 			kfree_sensitive(cifs_sb->ctx->password);
1000 			cifs_sb->ctx->password = NULL;
1001 			return -ENOMEM;
1002 		}
1003 	}
1004 	return 0;
1005 }
1006 
1007 static int smb3_reconfigure(struct fs_context *fc)
1008 {
1009 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
1010 	struct dentry *root = fc->root;
1011 	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
1012 	struct cifs_ses *ses = cifs_sb_master_tcon(cifs_sb)->ses;
1013 	char *new_password = NULL, *new_password2 = NULL;
1014 	bool need_recon = false;
1015 	int rc;
1016 
1017 	if (ses->expired_pwd)
1018 		need_recon = true;
1019 
1020 	rc = smb3_verify_reconfigure_ctx(fc, ctx, cifs_sb->ctx, need_recon);
1021 	if (rc)
1022 		return rc;
1023 
1024 	/*
1025 	 * We can not change UNC/username/password/domainname/
1026 	 * workstation_name/nodename/iocharset
1027 	 * during reconnect so ignore what we have in the new context and
1028 	 * just use what we already have in cifs_sb->ctx.
1029 	 */
1030 	STEAL_STRING(cifs_sb, ctx, UNC);
1031 	STEAL_STRING(cifs_sb, ctx, source);
1032 	STEAL_STRING(cifs_sb, ctx, username);
1033 
1034 	if (need_recon == false)
1035 		STEAL_STRING_SENSITIVE(cifs_sb, ctx, password);
1036 	else  {
1037 		if (ctx->password) {
1038 			new_password = kstrdup(ctx->password, GFP_KERNEL);
1039 			if (!new_password)
1040 				return -ENOMEM;
1041 		} else
1042 			STEAL_STRING_SENSITIVE(cifs_sb, ctx, password);
1043 	}
1044 
1045 	/*
1046 	 * if a new password2 has been specified, then reset it's value
1047 	 * inside the ses struct
1048 	 */
1049 	if (ctx->password2) {
1050 		new_password2 = kstrdup(ctx->password2, GFP_KERNEL);
1051 		if (!new_password2) {
1052 			kfree_sensitive(new_password);
1053 			return -ENOMEM;
1054 		}
1055 	} else
1056 		STEAL_STRING_SENSITIVE(cifs_sb, ctx, password2);
1057 
1058 	/*
1059 	 * we may update the passwords in the ses struct below. Make sure we do
1060 	 * not race with smb2_reconnect
1061 	 */
1062 	mutex_lock(&ses->session_mutex);
1063 
1064 	/*
1065 	 * smb2_reconnect may swap password and password2 in case session setup
1066 	 * failed. First get ctx passwords in sync with ses passwords. It should
1067 	 * be okay to do this even if this function were to return an error at a
1068 	 * later stage
1069 	 */
1070 	rc = smb3_sync_session_ctx_passwords(cifs_sb, ses);
1071 	if (rc) {
1072 		mutex_unlock(&ses->session_mutex);
1073 		return rc;
1074 	}
1075 
1076 	/*
1077 	 * now that allocations for passwords are done, commit them
1078 	 */
1079 	if (new_password) {
1080 		kfree_sensitive(ses->password);
1081 		ses->password = new_password;
1082 	}
1083 	if (new_password2) {
1084 		kfree_sensitive(ses->password2);
1085 		ses->password2 = new_password2;
1086 	}
1087 
1088 	mutex_unlock(&ses->session_mutex);
1089 
1090 	STEAL_STRING(cifs_sb, ctx, domainname);
1091 	STEAL_STRING(cifs_sb, ctx, nodename);
1092 	STEAL_STRING(cifs_sb, ctx, iocharset);
1093 
1094 	/* if rsize or wsize not passed in on remount, use previous values */
1095 	if (ctx->rsize == 0)
1096 		ctx->rsize = cifs_sb->ctx->rsize;
1097 	if (ctx->wsize == 0)
1098 		ctx->wsize = cifs_sb->ctx->wsize;
1099 
1100 
1101 	smb3_cleanup_fs_context_contents(cifs_sb->ctx);
1102 	rc = smb3_fs_context_dup(cifs_sb->ctx, ctx);
1103 	smb3_update_mnt_flags(cifs_sb);
1104 #ifdef CONFIG_CIFS_DFS_UPCALL
1105 	if (!rc)
1106 		rc = dfs_cache_remount_fs(cifs_sb);
1107 #endif
1108 
1109 	return rc;
1110 }
1111 
1112 static int smb3_fs_context_parse_param(struct fs_context *fc,
1113 				      struct fs_parameter *param)
1114 {
1115 	struct fs_parse_result result;
1116 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
1117 	int i, opt;
1118 	bool is_smb3 = !strcmp(fc->fs_type->name, "smb3");
1119 	bool skip_parsing = false;
1120 
1121 	cifs_dbg(FYI, "CIFS: parsing cifs mount option '%s'\n", param->key);
1122 
1123 	/*
1124 	 * fs_parse can not handle string options with an empty value so
1125 	 * we will need special handling of them.
1126 	 */
1127 	if (param->type == fs_value_is_string && param->string[0] == 0) {
1128 		if (!strcmp("pass", param->key) || !strcmp("password", param->key)) {
1129 			skip_parsing = true;
1130 			opt = Opt_pass;
1131 		} else if (!strcmp("user", param->key) || !strcmp("username", param->key)) {
1132 			skip_parsing = true;
1133 			opt = Opt_user;
1134 		}
1135 	}
1136 
1137 	if (!skip_parsing) {
1138 		opt = fs_parse(fc, smb3_fs_parameters, param, &result);
1139 		if (opt < 0)
1140 			return ctx->sloppy ? 1 : opt;
1141 	}
1142 
1143 	switch (opt) {
1144 	case Opt_compress:
1145 		if (!IS_ENABLED(CONFIG_CIFS_COMPRESSION)) {
1146 			cifs_errorf(fc, "CONFIG_CIFS_COMPRESSION kernel config option is unset\n");
1147 			goto cifs_parse_mount_err;
1148 		}
1149 		ctx->compress = true;
1150 		cifs_dbg(VFS, "SMB3 compression support is experimental\n");
1151 		break;
1152 	case Opt_nodfs:
1153 		ctx->nodfs = 1;
1154 		break;
1155 	case Opt_hard:
1156 		if (result.negated) {
1157 			if (ctx->retry == 1)
1158 				cifs_dbg(VFS, "conflicting hard vs. soft mount options\n");
1159 			ctx->retry = 0;
1160 		} else
1161 			ctx->retry = 1;
1162 		break;
1163 	case Opt_soft:
1164 		if (result.negated)
1165 			ctx->retry = 1;
1166 		else {
1167 			if (ctx->retry == 1)
1168 				cifs_dbg(VFS, "conflicting hard vs soft mount options\n");
1169 			ctx->retry = 0;
1170 		}
1171 		break;
1172 	case Opt_mapposix:
1173 		if (result.negated)
1174 			ctx->remap = false;
1175 		else {
1176 			ctx->remap = true;
1177 			ctx->sfu_remap = false; /* disable SFU mapping */
1178 		}
1179 		break;
1180 	case Opt_mapchars:
1181 		if (result.negated)
1182 			ctx->sfu_remap = false;
1183 		else {
1184 			ctx->sfu_remap = true;
1185 			ctx->remap = false; /* disable SFM (mapposix) mapping */
1186 		}
1187 		break;
1188 	case Opt_user_xattr:
1189 		if (result.negated)
1190 			ctx->no_xattr = 1;
1191 		else
1192 			ctx->no_xattr = 0;
1193 		break;
1194 	case Opt_forceuid:
1195 		if (result.negated)
1196 			ctx->override_uid = 0;
1197 		else
1198 			ctx->override_uid = 1;
1199 		ctx->forceuid_specified = true;
1200 		break;
1201 	case Opt_forcegid:
1202 		if (result.negated)
1203 			ctx->override_gid = 0;
1204 		else
1205 			ctx->override_gid = 1;
1206 		ctx->forcegid_specified = true;
1207 		break;
1208 	case Opt_perm:
1209 		if (result.negated)
1210 			ctx->noperm = 1;
1211 		else
1212 			ctx->noperm = 0;
1213 		break;
1214 	case Opt_dynperm:
1215 		if (result.negated)
1216 			ctx->dynperm = 0;
1217 		else
1218 			ctx->dynperm = 1;
1219 		break;
1220 	case Opt_sfu:
1221 		if (result.negated)
1222 			ctx->sfu_emul = 0;
1223 		else
1224 			ctx->sfu_emul = 1;
1225 		break;
1226 	case Opt_noblocksend:
1227 		ctx->noblocksnd = 1;
1228 		break;
1229 	case Opt_noautotune:
1230 		ctx->noautotune = 1;
1231 		break;
1232 	case Opt_nolease:
1233 		ctx->no_lease = 1;
1234 		break;
1235 	case Opt_nosparse:
1236 		ctx->no_sparse = 1;
1237 		break;
1238 	case Opt_nodelete:
1239 		ctx->nodelete = 1;
1240 		break;
1241 	case Opt_multichannel:
1242 		if (result.negated) {
1243 			ctx->multichannel = false;
1244 			ctx->max_channels = 1;
1245 		} else {
1246 			ctx->multichannel = true;
1247 			/* if number of channels not specified, default to 2 */
1248 			if (ctx->max_channels < 2)
1249 				ctx->max_channels = 2;
1250 		}
1251 		break;
1252 	case Opt_uid:
1253 		ctx->linux_uid = result.uid;
1254 		ctx->uid_specified = true;
1255 		break;
1256 	case Opt_cruid:
1257 		ctx->cred_uid = result.uid;
1258 		ctx->cruid_specified = true;
1259 		break;
1260 	case Opt_backupuid:
1261 		ctx->backupuid = result.uid;
1262 		ctx->backupuid_specified = true;
1263 		break;
1264 	case Opt_backupgid:
1265 		ctx->backupgid = result.gid;
1266 		ctx->backupgid_specified = true;
1267 		break;
1268 	case Opt_gid:
1269 		ctx->linux_gid = result.gid;
1270 		ctx->gid_specified = true;
1271 		break;
1272 	case Opt_port:
1273 		ctx->port = result.uint_32;
1274 		break;
1275 	case Opt_file_mode:
1276 		ctx->file_mode = result.uint_32;
1277 		break;
1278 	case Opt_dirmode:
1279 		ctx->dir_mode = result.uint_32;
1280 		break;
1281 	case Opt_min_enc_offload:
1282 		ctx->min_offload = result.uint_32;
1283 		break;
1284 	case Opt_retrans:
1285 		ctx->retrans = result.uint_32;
1286 		break;
1287 	case Opt_blocksize:
1288 		/*
1289 		 * inode blocksize realistically should never need to be
1290 		 * less than 16K or greater than 16M and default is 1MB.
1291 		 * Note that small inode block sizes (e.g. 64K) can lead
1292 		 * to very poor performance of common tools like cp and scp
1293 		 */
1294 		if ((result.uint_32 < CIFS_MAX_MSGSIZE) ||
1295 		   (result.uint_32 > (4 * SMB3_DEFAULT_IOSIZE))) {
1296 			cifs_errorf(fc, "%s: Invalid blocksize\n",
1297 				__func__);
1298 			goto cifs_parse_mount_err;
1299 		}
1300 		ctx->bsize = result.uint_32;
1301 		ctx->got_bsize = true;
1302 		break;
1303 	case Opt_rasize:
1304 		/*
1305 		 * readahead size realistically should never need to be
1306 		 * less than 1M (CIFS_DEFAULT_IOSIZE) or greater than 32M
1307 		 * (perhaps an exception should be considered in the
1308 		 * for the case of a large number of channels
1309 		 * when multichannel is negotiated) since that would lead
1310 		 * to plenty of parallel I/O in flight to the server.
1311 		 * Note that smaller read ahead sizes would
1312 		 * hurt performance of common tools like cp and scp
1313 		 * which often trigger sequential i/o with read ahead
1314 		 */
1315 		if ((result.uint_32 > (8 * SMB3_DEFAULT_IOSIZE)) ||
1316 		    (result.uint_32 < CIFS_DEFAULT_IOSIZE)) {
1317 			cifs_errorf(fc, "%s: Invalid rasize %d vs. %d\n",
1318 				__func__, result.uint_32, SMB3_DEFAULT_IOSIZE);
1319 			goto cifs_parse_mount_err;
1320 		}
1321 		ctx->rasize = result.uint_32;
1322 		break;
1323 	case Opt_rsize:
1324 		ctx->rsize = result.uint_32;
1325 		ctx->got_rsize = true;
1326 		break;
1327 	case Opt_wsize:
1328 		ctx->wsize = result.uint_32;
1329 		ctx->got_wsize = true;
1330 		if (ctx->wsize % PAGE_SIZE != 0) {
1331 			ctx->wsize = round_down(ctx->wsize, PAGE_SIZE);
1332 			if (ctx->wsize == 0) {
1333 				ctx->wsize = PAGE_SIZE;
1334 				cifs_dbg(VFS, "wsize too small, reset to minimum %ld\n", PAGE_SIZE);
1335 			} else {
1336 				cifs_dbg(VFS,
1337 					 "wsize rounded down to %d to multiple of PAGE_SIZE %ld\n",
1338 					 ctx->wsize, PAGE_SIZE);
1339 			}
1340 		}
1341 		break;
1342 	case Opt_acregmax:
1343 		ctx->acregmax = HZ * result.uint_32;
1344 		if (ctx->acregmax > CIFS_MAX_ACTIMEO) {
1345 			cifs_errorf(fc, "acregmax too large\n");
1346 			goto cifs_parse_mount_err;
1347 		}
1348 		break;
1349 	case Opt_acdirmax:
1350 		ctx->acdirmax = HZ * result.uint_32;
1351 		if (ctx->acdirmax > CIFS_MAX_ACTIMEO) {
1352 			cifs_errorf(fc, "acdirmax too large\n");
1353 			goto cifs_parse_mount_err;
1354 		}
1355 		break;
1356 	case Opt_actimeo:
1357 		if (HZ * result.uint_32 > CIFS_MAX_ACTIMEO) {
1358 			cifs_errorf(fc, "timeout too large\n");
1359 			goto cifs_parse_mount_err;
1360 		}
1361 		if ((ctx->acdirmax != CIFS_DEF_ACTIMEO) ||
1362 		    (ctx->acregmax != CIFS_DEF_ACTIMEO)) {
1363 			cifs_errorf(fc, "actimeo ignored since acregmax or acdirmax specified\n");
1364 			break;
1365 		}
1366 		ctx->acdirmax = ctx->acregmax = HZ * result.uint_32;
1367 		break;
1368 	case Opt_closetimeo:
1369 		ctx->closetimeo = HZ * result.uint_32;
1370 		if (ctx->closetimeo > SMB3_MAX_DCLOSETIMEO) {
1371 			cifs_errorf(fc, "closetimeo too large\n");
1372 			goto cifs_parse_mount_err;
1373 		}
1374 		break;
1375 	case Opt_echo_interval:
1376 		ctx->echo_interval = result.uint_32;
1377 		break;
1378 	case Opt_snapshot:
1379 		ctx->snapshot_time = result.uint_64;
1380 		break;
1381 	case Opt_max_credits:
1382 		if (result.uint_32 < 20 || result.uint_32 > 60000) {
1383 			cifs_errorf(fc, "%s: Invalid max_credits value\n",
1384 				 __func__);
1385 			goto cifs_parse_mount_err;
1386 		}
1387 		ctx->max_credits = result.uint_32;
1388 		break;
1389 	case Opt_max_channels:
1390 		if (result.uint_32 < 1 || result.uint_32 > CIFS_MAX_CHANNELS) {
1391 			cifs_errorf(fc, "%s: Invalid max_channels value, needs to be 1-%d\n",
1392 				 __func__, CIFS_MAX_CHANNELS);
1393 			goto cifs_parse_mount_err;
1394 		}
1395 		ctx->max_channels = result.uint_32;
1396 		/* If more than one channel requested ... they want multichan */
1397 		if (result.uint_32 > 1)
1398 			ctx->multichannel = true;
1399 		break;
1400 	case Opt_max_cached_dirs:
1401 		if (result.uint_32 < 1) {
1402 			cifs_errorf(fc, "%s: Invalid max_cached_dirs, needs to be 1 or more\n",
1403 				    __func__);
1404 			goto cifs_parse_mount_err;
1405 		}
1406 		ctx->max_cached_dirs = result.uint_32;
1407 		break;
1408 	case Opt_handletimeout:
1409 		ctx->handle_timeout = result.uint_32;
1410 		if (ctx->handle_timeout > SMB3_MAX_HANDLE_TIMEOUT) {
1411 			cifs_errorf(fc, "Invalid handle cache timeout, longer than 16 minutes\n");
1412 			goto cifs_parse_mount_err;
1413 		}
1414 		break;
1415 	case Opt_source:
1416 		kfree(ctx->UNC);
1417 		ctx->UNC = NULL;
1418 		switch (smb3_parse_devname(param->string, ctx)) {
1419 		case 0:
1420 			break;
1421 		case -ENOMEM:
1422 			cifs_errorf(fc, "Unable to allocate memory for devname\n");
1423 			goto cifs_parse_mount_err;
1424 		case -EINVAL:
1425 			cifs_errorf(fc, "Malformed UNC in devname\n");
1426 			goto cifs_parse_mount_err;
1427 		default:
1428 			cifs_errorf(fc, "Unknown error parsing devname\n");
1429 			goto cifs_parse_mount_err;
1430 		}
1431 		ctx->source = smb3_fs_context_fullpath(ctx, '/');
1432 		if (IS_ERR(ctx->source)) {
1433 			ctx->source = NULL;
1434 			cifs_errorf(fc, "OOM when copying UNC string\n");
1435 			goto cifs_parse_mount_err;
1436 		}
1437 		fc->source = kstrdup(ctx->source, GFP_KERNEL);
1438 		if (fc->source == NULL) {
1439 			cifs_errorf(fc, "OOM when copying UNC string\n");
1440 			goto cifs_parse_mount_err;
1441 		}
1442 		break;
1443 	case Opt_user:
1444 		kfree(ctx->username);
1445 		ctx->username = NULL;
1446 		if (ctx->nullauth)
1447 			break;
1448 		if (strlen(param->string) == 0) {
1449 			/* null user, ie. anonymous authentication */
1450 			ctx->nullauth = 1;
1451 			break;
1452 		}
1453 
1454 		if (strnlen(param->string, CIFS_MAX_USERNAME_LEN) >
1455 		    CIFS_MAX_USERNAME_LEN) {
1456 			pr_warn("username too long\n");
1457 			goto cifs_parse_mount_err;
1458 		}
1459 		ctx->username = kstrdup(param->string, GFP_KERNEL);
1460 		if (ctx->username == NULL) {
1461 			cifs_errorf(fc, "OOM when copying username string\n");
1462 			goto cifs_parse_mount_err;
1463 		}
1464 		break;
1465 	case Opt_pass:
1466 		kfree_sensitive(ctx->password);
1467 		ctx->password = NULL;
1468 		if (strlen(param->string) == 0)
1469 			break;
1470 
1471 		ctx->password = kstrdup(param->string, GFP_KERNEL);
1472 		if (ctx->password == NULL) {
1473 			cifs_errorf(fc, "OOM when copying password string\n");
1474 			goto cifs_parse_mount_err;
1475 		}
1476 		break;
1477 	case Opt_pass2:
1478 		kfree_sensitive(ctx->password2);
1479 		ctx->password2 = NULL;
1480 		if (strlen(param->string) == 0)
1481 			break;
1482 
1483 		ctx->password2 = kstrdup(param->string, GFP_KERNEL);
1484 		if (ctx->password2 == NULL) {
1485 			cifs_errorf(fc, "OOM when copying password2 string\n");
1486 			goto cifs_parse_mount_err;
1487 		}
1488 		break;
1489 	case Opt_ip:
1490 		if (strlen(param->string) == 0) {
1491 			ctx->got_ip = false;
1492 			break;
1493 		}
1494 		if (!cifs_convert_address((struct sockaddr *)&ctx->dstaddr,
1495 					  param->string,
1496 					  strlen(param->string))) {
1497 			pr_err("bad ip= option (%s)\n", param->string);
1498 			goto cifs_parse_mount_err;
1499 		}
1500 		ctx->got_ip = true;
1501 		break;
1502 	case Opt_domain:
1503 		if (strnlen(param->string, CIFS_MAX_DOMAINNAME_LEN)
1504 				== CIFS_MAX_DOMAINNAME_LEN) {
1505 			pr_warn("domain name too long\n");
1506 			goto cifs_parse_mount_err;
1507 		}
1508 
1509 		kfree(ctx->domainname);
1510 		ctx->domainname = kstrdup(param->string, GFP_KERNEL);
1511 		if (ctx->domainname == NULL) {
1512 			cifs_errorf(fc, "OOM when copying domainname string\n");
1513 			goto cifs_parse_mount_err;
1514 		}
1515 		cifs_dbg(FYI, "Domain name set\n");
1516 		break;
1517 	case Opt_srcaddr:
1518 		if (!cifs_convert_address(
1519 				(struct sockaddr *)&ctx->srcaddr,
1520 				param->string, strlen(param->string))) {
1521 			pr_warn("Could not parse srcaddr: %s\n",
1522 				param->string);
1523 			goto cifs_parse_mount_err;
1524 		}
1525 		break;
1526 	case Opt_iocharset:
1527 		if (strnlen(param->string, 1024) >= 65) {
1528 			pr_warn("iocharset name too long\n");
1529 			goto cifs_parse_mount_err;
1530 		}
1531 
1532 		if (strncasecmp(param->string, "default", 7) != 0) {
1533 			kfree(ctx->iocharset);
1534 			ctx->iocharset = kstrdup(param->string, GFP_KERNEL);
1535 			if (ctx->iocharset == NULL) {
1536 				cifs_errorf(fc, "OOM when copying iocharset string\n");
1537 				goto cifs_parse_mount_err;
1538 			}
1539 		}
1540 		/* if iocharset not set then load_nls_default
1541 		 * is used by caller
1542 		 */
1543 		cifs_dbg(FYI, "iocharset set to %s\n", ctx->iocharset);
1544 		break;
1545 	case Opt_netbiosname:
1546 		memset(ctx->source_rfc1001_name, 0x20,
1547 			RFC1001_NAME_LEN);
1548 		/*
1549 		 * FIXME: are there cases in which a comma can
1550 		 * be valid in workstation netbios name (and
1551 		 * need special handling)?
1552 		 */
1553 		for (i = 0; i < RFC1001_NAME_LEN; i++) {
1554 			/* don't ucase netbiosname for user */
1555 			if (param->string[i] == 0)
1556 				break;
1557 			ctx->source_rfc1001_name[i] = param->string[i];
1558 		}
1559 		/* The string has 16th byte zero still from
1560 		 * set at top of the function
1561 		 */
1562 		if (i == RFC1001_NAME_LEN && param->string[i] != 0)
1563 			pr_warn("netbiosname longer than 15 truncated\n");
1564 		break;
1565 	case Opt_servern:
1566 		/* last byte, type, is 0x20 for servr type */
1567 		memset(ctx->target_rfc1001_name, 0x20,
1568 			RFC1001_NAME_LEN_WITH_NULL);
1569 		/*
1570 		 * BB are there cases in which a comma can be valid in this
1571 		 * workstation netbios name (and need special handling)?
1572 		 */
1573 
1574 		/* user or mount helper must uppercase the netbios name */
1575 		for (i = 0; i < 15; i++) {
1576 			if (param->string[i] == 0)
1577 				break;
1578 			ctx->target_rfc1001_name[i] = param->string[i];
1579 		}
1580 
1581 		/* The string has 16th byte zero still from set at top of function */
1582 		if (i == RFC1001_NAME_LEN && param->string[i] != 0)
1583 			pr_warn("server netbiosname longer than 15 truncated\n");
1584 		break;
1585 	case Opt_ver:
1586 		/* version of mount userspace tools, not dialect */
1587 		/* If interface changes in mount.cifs bump to new ver */
1588 		if (strncasecmp(param->string, "1", 1) == 0) {
1589 			if (strlen(param->string) > 1) {
1590 				pr_warn("Bad mount helper ver=%s. Did you want SMB1 (CIFS) dialect and mean to type vers=1.0 instead?\n",
1591 					param->string);
1592 				goto cifs_parse_mount_err;
1593 			}
1594 			/* This is the default */
1595 			break;
1596 		}
1597 		/* For all other value, error */
1598 		pr_warn("Invalid mount helper version specified\n");
1599 		goto cifs_parse_mount_err;
1600 	case Opt_vers:
1601 		/* protocol version (dialect) */
1602 		if (cifs_parse_smb_version(fc, param->string, ctx, is_smb3) != 0)
1603 			goto cifs_parse_mount_err;
1604 		ctx->got_version = true;
1605 		break;
1606 	case Opt_sec:
1607 		if (cifs_parse_security_flavors(fc, param->string, ctx) != 0)
1608 			goto cifs_parse_mount_err;
1609 		break;
1610 	case Opt_upcalltarget:
1611 		if (cifs_parse_upcall_target(fc, param->string, ctx) != 0)
1612 			goto cifs_parse_mount_err;
1613 		break;
1614 	case Opt_cache:
1615 		if (cifs_parse_cache_flavor(fc, param->string, ctx) != 0)
1616 			goto cifs_parse_mount_err;
1617 		break;
1618 	case Opt_witness:
1619 #ifndef CONFIG_CIFS_SWN_UPCALL
1620 		cifs_errorf(fc, "Witness support needs CONFIG_CIFS_SWN_UPCALL config option\n");
1621 			goto cifs_parse_mount_err;
1622 #endif
1623 		ctx->witness = true;
1624 		pr_warn_once("Witness protocol support is experimental\n");
1625 		break;
1626 	case Opt_rootfs:
1627 #ifndef CONFIG_CIFS_ROOT
1628 		cifs_dbg(VFS, "rootfs support requires CONFIG_CIFS_ROOT config option\n");
1629 		goto cifs_parse_mount_err;
1630 #endif
1631 		ctx->rootfs = true;
1632 		break;
1633 	case Opt_posixpaths:
1634 		if (result.negated)
1635 			ctx->posix_paths = 0;
1636 		else
1637 			ctx->posix_paths = 1;
1638 		break;
1639 	case Opt_unix:
1640 		if (result.negated) {
1641 			if (ctx->linux_ext == 1)
1642 				pr_warn_once("conflicting posix mount options specified\n");
1643 			ctx->linux_ext = 0;
1644 			ctx->no_linux_ext = 1;
1645 		} else {
1646 			if (ctx->no_linux_ext == 1)
1647 				pr_warn_once("conflicting posix mount options specified\n");
1648 			ctx->linux_ext = 1;
1649 			ctx->no_linux_ext = 0;
1650 		}
1651 		break;
1652 	case Opt_nocase:
1653 		ctx->nocase = 1;
1654 		break;
1655 	case Opt_brl:
1656 		if (result.negated) {
1657 			/*
1658 			 * turn off mandatory locking in mode
1659 			 * if remote locking is turned off since the
1660 			 * local vfs will do advisory
1661 			 */
1662 			if (ctx->file_mode ==
1663 				(S_IALLUGO & ~(S_ISUID | S_IXGRP)))
1664 				ctx->file_mode = S_IALLUGO;
1665 			ctx->nobrl =  1;
1666 		} else
1667 			ctx->nobrl =  0;
1668 		break;
1669 	case Opt_handlecache:
1670 		if (result.negated)
1671 			ctx->nohandlecache = 1;
1672 		else
1673 			ctx->nohandlecache = 0;
1674 		break;
1675 	case Opt_forcemandatorylock:
1676 		ctx->mand_lock = 1;
1677 		break;
1678 	case Opt_setuids:
1679 		ctx->setuids = result.negated;
1680 		break;
1681 	case Opt_intr:
1682 		ctx->intr = !result.negated;
1683 		break;
1684 	case Opt_setuidfromacl:
1685 		ctx->setuidfromacl = 1;
1686 		break;
1687 	case Opt_strictsync:
1688 		ctx->nostrictsync = result.negated;
1689 		break;
1690 	case Opt_serverino:
1691 		ctx->server_ino = !result.negated;
1692 		break;
1693 	case Opt_rwpidforward:
1694 		ctx->rwpidforward = 1;
1695 		break;
1696 	case Opt_modesid:
1697 		ctx->mode_ace = 1;
1698 		break;
1699 	case Opt_cifsacl:
1700 		ctx->cifs_acl = !result.negated;
1701 		break;
1702 	case Opt_acl:
1703 		ctx->no_psx_acl = result.negated;
1704 		break;
1705 	case Opt_locallease:
1706 		ctx->local_lease = 1;
1707 		break;
1708 	case Opt_sign:
1709 		ctx->sign = true;
1710 		break;
1711 	case Opt_ignore_signature:
1712 		ctx->sign = true;
1713 		ctx->ignore_signature = true;
1714 		break;
1715 	case Opt_seal:
1716 		/* we do not do the following in secFlags because seal
1717 		 * is a per tree connection (mount) not a per socket
1718 		 * or per-smb connection option in the protocol
1719 		 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1720 		 */
1721 		ctx->seal = 1;
1722 		break;
1723 	case Opt_noac:
1724 		pr_warn("Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
1725 		break;
1726 	case Opt_fsc:
1727 #ifndef CONFIG_CIFS_FSCACHE
1728 		cifs_errorf(fc, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
1729 		goto cifs_parse_mount_err;
1730 #endif
1731 		ctx->fsc = true;
1732 		break;
1733 	case Opt_mfsymlinks:
1734 		ctx->mfsymlinks = true;
1735 		break;
1736 	case Opt_multiuser:
1737 		ctx->multiuser = true;
1738 		break;
1739 	case Opt_sloppy:
1740 		ctx->sloppy = true;
1741 		break;
1742 	case Opt_nosharesock:
1743 		ctx->nosharesock = true;
1744 		break;
1745 	case Opt_persistent:
1746 		if (result.negated) {
1747 			ctx->nopersistent = true;
1748 			if (ctx->persistent) {
1749 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1750 				goto cifs_parse_mount_err;
1751 			}
1752 		} else {
1753 			ctx->persistent = true;
1754 			if ((ctx->nopersistent) || (ctx->resilient)) {
1755 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1756 				goto cifs_parse_mount_err;
1757 			}
1758 		}
1759 		break;
1760 	case Opt_resilient:
1761 		if (result.negated) {
1762 			ctx->resilient = false; /* already the default */
1763 		} else {
1764 			ctx->resilient = true;
1765 			if (ctx->persistent) {
1766 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1767 				goto cifs_parse_mount_err;
1768 			}
1769 		}
1770 		break;
1771 	case Opt_tcp_nodelay:
1772 		/* tcp nodelay should not usually be needed since we CORK/UNCORK the socket */
1773 		if (result.negated)
1774 			ctx->sockopt_tcp_nodelay = false;
1775 		else
1776 			ctx->sockopt_tcp_nodelay = true;
1777 		break;
1778 	case Opt_domainauto:
1779 		ctx->domainauto = true;
1780 		break;
1781 	case Opt_rdma:
1782 		ctx->rdma = true;
1783 		break;
1784 	case Opt_reparse:
1785 		if (parse_reparse_flavor(fc, param->string, ctx))
1786 			goto cifs_parse_mount_err;
1787 		break;
1788 	case Opt_nativesocket:
1789 		ctx->nonativesocket = result.negated;
1790 		break;
1791 	case Opt_symlink:
1792 		if (parse_symlink_flavor(fc, param->string, ctx))
1793 			goto cifs_parse_mount_err;
1794 		break;
1795 	case Opt_symlinkroot:
1796 		if (param->string[0] != '/') {
1797 			cifs_errorf(fc, "symlinkroot mount options must be absolute path\n");
1798 			goto cifs_parse_mount_err;
1799 		}
1800 		kfree(ctx->symlinkroot);
1801 		ctx->symlinkroot = kstrdup(param->string, GFP_KERNEL);
1802 		if (!ctx->symlinkroot)
1803 			goto cifs_parse_mount_err;
1804 		break;
1805 	}
1806 	/* case Opt_ignore: - is ignored as expected ... */
1807 
1808 	if (ctx->multiuser && ctx->upcall_target == UPTARGET_MOUNT) {
1809 		cifs_errorf(fc, "multiuser mount option not supported with upcalltarget set as 'mount'\n");
1810 		goto cifs_parse_mount_err;
1811 	}
1812 
1813 	/*
1814 	 * By default resolve all native absolute symlinks relative to "/mnt/".
1815 	 * Same default has drvfs driver running in WSL for resolving SMB shares.
1816 	 */
1817 	if (!ctx->symlinkroot)
1818 		ctx->symlinkroot = kstrdup("/mnt/", GFP_KERNEL);
1819 
1820 	return 0;
1821 
1822  cifs_parse_mount_err:
1823 	kfree_sensitive(ctx->password);
1824 	ctx->password = NULL;
1825 	kfree_sensitive(ctx->password2);
1826 	ctx->password2 = NULL;
1827 	return -EINVAL;
1828 }
1829 
1830 enum cifs_symlink_type get_cifs_symlink_type(struct cifs_sb_info *cifs_sb)
1831 {
1832 	if (cifs_sb->ctx->symlink_type == CIFS_SYMLINK_TYPE_DEFAULT) {
1833 		if (cifs_sb->ctx->mfsymlinks)
1834 			return CIFS_SYMLINK_TYPE_MFSYMLINKS;
1835 		else if (cifs_sb->ctx->sfu_emul)
1836 			return CIFS_SYMLINK_TYPE_SFU;
1837 		else if (cifs_sb->ctx->linux_ext && !cifs_sb->ctx->no_linux_ext)
1838 			return CIFS_SYMLINK_TYPE_UNIX;
1839 		else if (cifs_sb->ctx->reparse_type != CIFS_REPARSE_TYPE_NONE)
1840 			return CIFS_SYMLINK_TYPE_NATIVE;
1841 		else
1842 			return CIFS_SYMLINK_TYPE_NONE;
1843 	} else {
1844 		return cifs_sb->ctx->symlink_type;
1845 	}
1846 }
1847 
1848 int smb3_init_fs_context(struct fs_context *fc)
1849 {
1850 	struct smb3_fs_context *ctx;
1851 	char *nodename = utsname()->nodename;
1852 	int i;
1853 
1854 	ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
1855 	if (unlikely(!ctx))
1856 		return -ENOMEM;
1857 
1858 	strscpy(ctx->workstation_name, nodename, sizeof(ctx->workstation_name));
1859 
1860 	/*
1861 	 * does not have to be perfect mapping since field is
1862 	 * informational, only used for servers that do not support
1863 	 * port 445 and it can be overridden at mount time
1864 	 */
1865 	memset(ctx->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1866 	for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
1867 		ctx->source_rfc1001_name[i] = toupper(nodename[i]);
1868 
1869 	ctx->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
1870 	/*
1871 	 * null target name indicates to use *SMBSERVR default called name
1872 	 *  if we end up sending RFC1001 session initialize
1873 	 */
1874 	ctx->target_rfc1001_name[0] = 0;
1875 	ctx->cred_uid = current_uid();
1876 	ctx->linux_uid = current_uid();
1877 	ctx->linux_gid = current_gid();
1878 	/* By default 4MB read ahead size, 1MB block size */
1879 	ctx->bsize = CIFS_DEFAULT_IOSIZE; /* can improve cp performance significantly */
1880 	ctx->rasize = 0; /* 0 = use default (ie negotiated rsize) for read ahead pages */
1881 
1882 	/*
1883 	 * default to SFM style remapping of seven reserved characters
1884 	 * unless user overrides it or we negotiate CIFS POSIX where
1885 	 * it is unnecessary.  Can not simultaneously use more than one mapping
1886 	 * since then readdir could list files that open could not open
1887 	 */
1888 	ctx->remap = true;
1889 
1890 	/* default to only allowing write access to owner of the mount */
1891 	ctx->dir_mode = ctx->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1892 
1893 	/* ctx->retry default is 0 (i.e. "soft" limited retry not hard retry) */
1894 	/* default is always to request posix paths. */
1895 	ctx->posix_paths = 1;
1896 	/* default to using server inode numbers where available */
1897 	ctx->server_ino = 1;
1898 
1899 	/* default is to use strict cifs caching semantics */
1900 	ctx->strict_io = true;
1901 
1902 	ctx->acregmax = CIFS_DEF_ACTIMEO;
1903 	ctx->acdirmax = CIFS_DEF_ACTIMEO;
1904 	ctx->closetimeo = SMB3_DEF_DCLOSETIMEO;
1905 	ctx->max_cached_dirs = MAX_CACHED_FIDS;
1906 	/* Most clients set timeout to 0, allows server to use its default */
1907 	ctx->handle_timeout = 0; /* See MS-SMB2 spec section 2.2.14.2.12 */
1908 
1909 	/* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1910 	ctx->ops = &smb30_operations;
1911 	ctx->vals = &smbdefault_values;
1912 
1913 	ctx->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1914 
1915 	/* default to no multichannel (single server connection) */
1916 	ctx->multichannel = false;
1917 	ctx->max_channels = 1;
1918 
1919 	ctx->backupuid_specified = false; /* no backup intent for a user */
1920 	ctx->backupgid_specified = false; /* no backup intent for a group */
1921 
1922 	ctx->retrans = 1;
1923 	ctx->reparse_type = CIFS_REPARSE_TYPE_DEFAULT;
1924 	ctx->symlink_type = CIFS_SYMLINK_TYPE_DEFAULT;
1925 	ctx->nonativesocket = 0;
1926 
1927 /*
1928  *	short int override_uid = -1;
1929  *	short int override_gid = -1;
1930  *	char *nodename = strdup(utsname()->nodename);
1931  *	struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1932  */
1933 
1934 	fc->fs_private = ctx;
1935 	fc->ops = &smb3_fs_context_ops;
1936 	return 0;
1937 }
1938 
1939 void
1940 smb3_cleanup_fs_context_contents(struct smb3_fs_context *ctx)
1941 {
1942 	if (ctx == NULL)
1943 		return;
1944 
1945 	/*
1946 	 * Make sure this stays in sync with smb3_fs_context_dup()
1947 	 */
1948 	kfree(ctx->username);
1949 	ctx->username = NULL;
1950 	kfree_sensitive(ctx->password);
1951 	ctx->password = NULL;
1952 	kfree_sensitive(ctx->password2);
1953 	ctx->password2 = NULL;
1954 	kfree(ctx->server_hostname);
1955 	ctx->server_hostname = NULL;
1956 	kfree(ctx->UNC);
1957 	ctx->UNC = NULL;
1958 	kfree(ctx->source);
1959 	ctx->source = NULL;
1960 	kfree(ctx->domainname);
1961 	ctx->domainname = NULL;
1962 	kfree(ctx->nodename);
1963 	ctx->nodename = NULL;
1964 	kfree(ctx->iocharset);
1965 	ctx->iocharset = NULL;
1966 	kfree(ctx->prepath);
1967 	ctx->prepath = NULL;
1968 	kfree(ctx->leaf_fullpath);
1969 	ctx->leaf_fullpath = NULL;
1970 	kfree(ctx->dns_dom);
1971 	ctx->dns_dom = NULL;
1972 	kfree(ctx->symlinkroot);
1973 	ctx->symlinkroot = NULL;
1974 }
1975 
1976 void
1977 smb3_cleanup_fs_context(struct smb3_fs_context *ctx)
1978 {
1979 	if (!ctx)
1980 		return;
1981 	smb3_cleanup_fs_context_contents(ctx);
1982 	kfree(ctx);
1983 }
1984 
1985 void smb3_update_mnt_flags(struct cifs_sb_info *cifs_sb)
1986 {
1987 	struct smb3_fs_context *ctx = cifs_sb->ctx;
1988 
1989 	if (ctx->nodfs)
1990 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
1991 	else
1992 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_DFS;
1993 
1994 	if (ctx->noperm)
1995 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
1996 	else
1997 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_PERM;
1998 
1999 	if (ctx->setuids)
2000 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
2001 	else
2002 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SET_UID;
2003 
2004 	if (ctx->setuidfromacl)
2005 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
2006 	else
2007 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_UID_FROM_ACL;
2008 
2009 	if (ctx->server_ino)
2010 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
2011 	else
2012 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
2013 
2014 	if (ctx->remap)
2015 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
2016 	else
2017 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MAP_SFM_CHR;
2018 
2019 	if (ctx->sfu_remap)
2020 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
2021 	else
2022 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MAP_SPECIAL_CHR;
2023 
2024 	if (ctx->no_xattr)
2025 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
2026 	else
2027 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_XATTR;
2028 
2029 	if (ctx->sfu_emul)
2030 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
2031 	else
2032 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_UNX_EMUL;
2033 
2034 	if (ctx->nobrl)
2035 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
2036 	else
2037 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_BRL;
2038 
2039 	if (ctx->nohandlecache)
2040 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
2041 	else
2042 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_HANDLE_CACHE;
2043 
2044 	if (ctx->nostrictsync)
2045 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
2046 	else
2047 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NOSSYNC;
2048 
2049 	if (ctx->mand_lock)
2050 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
2051 	else
2052 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NOPOSIXBRL;
2053 
2054 	if (ctx->rwpidforward)
2055 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
2056 	else
2057 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_RWPIDFORWARD;
2058 
2059 	if (ctx->mode_ace)
2060 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MODE_FROM_SID;
2061 	else
2062 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MODE_FROM_SID;
2063 
2064 	if (ctx->cifs_acl)
2065 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
2066 	else
2067 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_ACL;
2068 
2069 	if (ctx->backupuid_specified)
2070 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
2071 	else
2072 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_BACKUPUID;
2073 
2074 	if (ctx->backupgid_specified)
2075 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
2076 	else
2077 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_BACKUPGID;
2078 
2079 	if (ctx->override_uid)
2080 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
2081 	else
2082 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_OVERR_UID;
2083 
2084 	if (ctx->override_gid)
2085 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
2086 	else
2087 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_OVERR_GID;
2088 
2089 	if (ctx->dynperm)
2090 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
2091 	else
2092 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_DYNPERM;
2093 
2094 	if (ctx->fsc)
2095 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
2096 	else
2097 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_FSCACHE;
2098 
2099 	if (ctx->multiuser)
2100 		cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
2101 					    CIFS_MOUNT_NO_PERM);
2102 	else
2103 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MULTIUSER;
2104 
2105 
2106 	if (ctx->strict_io)
2107 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
2108 	else
2109 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_STRICT_IO;
2110 
2111 	if (ctx->direct_io)
2112 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
2113 	else
2114 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_DIRECT_IO;
2115 
2116 	if (ctx->mfsymlinks)
2117 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
2118 	else
2119 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MF_SYMLINKS;
2120 	if (ctx->mfsymlinks) {
2121 		if (ctx->sfu_emul) {
2122 			/*
2123 			 * Our SFU ("Services for Unix") emulation allows now
2124 			 * creating new and reading existing SFU symlinks.
2125 			 * Older Linux kernel versions were not able to neither
2126 			 * read existing nor create new SFU symlinks. But
2127 			 * creating and reading SFU style mknod and FIFOs was
2128 			 * supported for long time. When "mfsymlinks" and
2129 			 * "sfu" are both enabled at the same time, it allows
2130 			 * reading both types of symlinks, but will only create
2131 			 * them with mfsymlinks format. This allows better
2132 			 * Apple compatibility, compatibility with older Linux
2133 			 * kernel clients (probably better for Samba too)
2134 			 * while still recognizing old Windows style symlinks.
2135 			 */
2136 			cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
2137 		}
2138 	}
2139 	cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SHUTDOWN;
2140 
2141 	return;
2142 }
2143