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