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