xref: /linux/fs/smb/client/fs_context.c (revision ed1e53796f51c27f743f9f2677e58a47e85c3ff0)
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 "cifsglob.h"
30 #include "cifsproto.h"
31 #include "cifs_unicode.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
34 #include "ntlmssp.h"
35 #include "nterr.h"
36 #include "rfc1002pdu.h"
37 #include "fs_context.h"
38 
39 DEFINE_MUTEX(cifs_mount_mutex);
40 
41 static const match_table_t cifs_smb_version_tokens = {
42 	{ Smb_1, SMB1_VERSION_STRING },
43 	{ Smb_20, SMB20_VERSION_STRING},
44 	{ Smb_21, SMB21_VERSION_STRING },
45 	{ Smb_30, SMB30_VERSION_STRING },
46 	{ Smb_302, SMB302_VERSION_STRING },
47 	{ Smb_302, ALT_SMB302_VERSION_STRING },
48 	{ Smb_311, SMB311_VERSION_STRING },
49 	{ Smb_311, ALT_SMB311_VERSION_STRING },
50 	{ Smb_3any, SMB3ANY_VERSION_STRING },
51 	{ Smb_default, SMBDEFAULT_VERSION_STRING },
52 	{ Smb_version_err, NULL }
53 };
54 
55 static const match_table_t cifs_secflavor_tokens = {
56 	{ Opt_sec_krb5, "krb5" },
57 	{ Opt_sec_krb5i, "krb5i" },
58 	{ Opt_sec_krb5p, "krb5p" },
59 	{ Opt_sec_ntlmsspi, "ntlmsspi" },
60 	{ Opt_sec_ntlmssp, "ntlmssp" },
61 	{ Opt_sec_ntlmv2, "nontlm" },
62 	{ Opt_sec_ntlmv2, "ntlmv2" },
63 	{ Opt_sec_ntlmv2i, "ntlmv2i" },
64 	{ Opt_sec_none, "none" },
65 
66 	{ Opt_sec_err, NULL }
67 };
68 
69 static const match_table_t cifs_upcall_target = {
70 	{ Opt_upcall_target_mount, "mount" },
71 	{ Opt_upcall_target_application, "app" },
72 	{ Opt_upcall_target_err, NULL }
73 };
74 
75 const struct fs_parameter_spec smb3_fs_parameters[] = {
76 	/* Mount options that take no arguments */
77 	fsparam_flag_no("user_xattr", Opt_user_xattr),
78 	fsparam_flag_no("forceuid", Opt_forceuid),
79 	fsparam_flag_no("multichannel", Opt_multichannel),
80 	fsparam_flag_no("forcegid", Opt_forcegid),
81 	fsparam_flag("noblocksend", Opt_noblocksend),
82 	fsparam_flag("noautotune", Opt_noautotune),
83 	fsparam_flag("nolease", Opt_nolease),
84 	fsparam_flag_no("hard", Opt_hard),
85 	fsparam_flag_no("soft", Opt_soft),
86 	fsparam_flag_no("perm", Opt_perm),
87 	fsparam_flag("nodelete", Opt_nodelete),
88 	fsparam_flag_no("mapposix", Opt_mapposix),
89 	fsparam_flag("mapchars", Opt_mapchars),
90 	fsparam_flag("nomapchars", Opt_nomapchars),
91 	fsparam_flag_no("sfu", Opt_sfu),
92 	fsparam_flag("nodfs", Opt_nodfs),
93 	fsparam_flag_no("posixpaths", Opt_posixpaths),
94 	fsparam_flag_no("unix", Opt_unix),
95 	fsparam_flag_no("linux", Opt_unix),
96 	fsparam_flag_no("posix", Opt_unix),
97 	fsparam_flag("nocase", Opt_nocase),
98 	fsparam_flag("ignorecase", Opt_nocase),
99 	fsparam_flag_no("brl", Opt_brl),
100 	fsparam_flag_no("handlecache", Opt_handlecache),
101 	fsparam_flag("forcemandatorylock", Opt_forcemandatorylock),
102 	fsparam_flag("forcemand", Opt_forcemandatorylock),
103 	fsparam_flag("setuidfromacl", Opt_setuidfromacl),
104 	fsparam_flag("idsfromsid", Opt_setuidfromacl),
105 	fsparam_flag_no("setuids", Opt_setuids),
106 	fsparam_flag_no("dynperm", Opt_dynperm),
107 	fsparam_flag_no("intr", Opt_intr),
108 	fsparam_flag_no("strictsync", Opt_strictsync),
109 	fsparam_flag_no("serverino", Opt_serverino),
110 	fsparam_flag("rwpidforward", Opt_rwpidforward),
111 	fsparam_flag("cifsacl", Opt_cifsacl),
112 	fsparam_flag_no("acl", Opt_acl),
113 	fsparam_flag("locallease", Opt_locallease),
114 	fsparam_flag("sign", Opt_sign),
115 	fsparam_flag("ignore_signature", Opt_ignore_signature),
116 	fsparam_flag("signloosely", Opt_ignore_signature),
117 	fsparam_flag("seal", Opt_seal),
118 	fsparam_flag("noac", Opt_noac),
119 	fsparam_flag("fsc", Opt_fsc),
120 	fsparam_flag("mfsymlinks", Opt_mfsymlinks),
121 	fsparam_flag("multiuser", Opt_multiuser),
122 	fsparam_flag("sloppy", Opt_sloppy),
123 	fsparam_flag("nosharesock", Opt_nosharesock),
124 	fsparam_flag_no("persistenthandles", Opt_persistent),
125 	fsparam_flag_no("resilienthandles", Opt_resilient),
126 	fsparam_flag_no("tcpnodelay", Opt_tcp_nodelay),
127 	fsparam_flag("nosparse", Opt_nosparse),
128 	fsparam_flag("domainauto", Opt_domainauto),
129 	fsparam_flag("rdma", Opt_rdma),
130 	fsparam_flag("modesid", Opt_modesid),
131 	fsparam_flag("modefromsid", Opt_modesid),
132 	fsparam_flag("rootfs", Opt_rootfs),
133 	fsparam_flag("compress", Opt_compress),
134 	fsparam_flag("witness", Opt_witness),
135 	fsparam_flag_no("nativesocket", Opt_nativesocket),
136 	fsparam_flag_no("unicode", Opt_unicode),
137 	fsparam_flag_no("nbsessinit", Opt_nbsessinit),
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 /* CONFIG_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 int smb3_handle_conflicting_options(struct fs_context *fc)
714 {
715 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
716 
717 	if (ctx->multichannel_specified) {
718 		if (ctx->multichannel) {
719 			if (!ctx->max_channels_specified) {
720 				ctx->max_channels = 2;
721 			} else if (ctx->max_channels == 1) {
722 				cifs_errorf(fc,
723 					    "max_channels must be greater than 1 when multichannel is enabled\n");
724 				return -EINVAL;
725 			}
726 		} else {
727 			if (!ctx->max_channels_specified) {
728 				ctx->max_channels = 1;
729 			} else if (ctx->max_channels > 1) {
730 				cifs_errorf(fc,
731 					    "max_channels must be equal to 1 when multichannel is disabled\n");
732 				return -EINVAL;
733 			}
734 		}
735 	} else {
736 		if (ctx->max_channels_specified) {
737 			if (ctx->max_channels > 1)
738 				ctx->multichannel = true;
739 			else
740 				ctx->multichannel = false;
741 		} else {
742 			ctx->multichannel = false;
743 			ctx->max_channels = 1;
744 		}
745 	}
746 
747 	//resetting default values as remount doesn't initialize fs_context again
748 	ctx->multichannel_specified = false;
749 	ctx->max_channels_specified = false;
750 
751 	return 0;
752 }
753 
754 static void smb3_fs_context_free(struct fs_context *fc);
755 static int smb3_fs_context_parse_param(struct fs_context *fc,
756 				       struct fs_parameter *param);
757 static int smb3_fs_context_parse_monolithic(struct fs_context *fc,
758 					    void *data);
759 static int smb3_get_tree(struct fs_context *fc);
760 static void smb3_sync_ses_chan_max(struct cifs_ses *ses, unsigned int max_channels);
761 static int smb3_reconfigure(struct fs_context *fc);
762 
763 static const struct fs_context_operations smb3_fs_context_ops = {
764 	.free			= smb3_fs_context_free,
765 	.parse_param		= smb3_fs_context_parse_param,
766 	.parse_monolithic	= smb3_fs_context_parse_monolithic,
767 	.get_tree		= smb3_get_tree,
768 	.reconfigure		= smb3_reconfigure,
769 };
770 
771 /*
772  * Parse a monolithic block of data from sys_mount().
773  * smb3_fs_context_parse_monolithic - Parse key[=val][,key[=val]]* mount data
774  * @ctx: The superblock configuration to fill in.
775  * @data: The data to parse
776  *
777  * Parse a blob of data that's in key[=val][,key[=val]]* form.  This can be
778  * called from the ->monolithic_mount_data() fs_context operation.
779  *
780  * Returns 0 on success or the error returned by the ->parse_option() fs_context
781  * operation on failure.
782  */
783 static int smb3_fs_context_parse_monolithic(struct fs_context *fc,
784 					   void *data)
785 {
786 	char *options = data, *key;
787 	int ret = 0;
788 
789 	if (!options)
790 		return 0;
791 
792 	ret = security_sb_eat_lsm_opts(options, &fc->security);
793 	if (ret)
794 		return ret;
795 
796 	/* BB Need to add support for sep= here TBD */
797 	while ((key = strsep(&options, ",")) != NULL) {
798 		size_t len;
799 		char *value;
800 
801 		if (*key == 0)
802 			break;
803 
804 		/* Check if following character is the deliminator If yes,
805 		 * we have encountered a double deliminator reset the NULL
806 		 * character to the deliminator
807 		 */
808 		while (options && options[0] == ',') {
809 			len = strlen(key);
810 			strcpy(key + len, options);
811 			options = strchr(options, ',');
812 			if (options)
813 				*options++ = 0;
814 		}
815 
816 
817 		value = strchr(key, '=');
818 		if (value) {
819 			if (value == key)
820 				continue;
821 			*value++ = 0;
822 		}
823 
824 		ret = vfs_parse_fs_string(fc, key, value);
825 		if (ret < 0)
826 			break;
827 	}
828 	ret = smb3_handle_conflicting_options(fc);
829 
830 	return ret;
831 }
832 
833 /*
834  * Validate the preparsed information in the config.
835  */
836 static int smb3_fs_context_validate(struct fs_context *fc)
837 {
838 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
839 
840 	if (ctx->rdma && ctx->vals->protocol_id < SMB30_PROT_ID) {
841 		cifs_errorf(fc, "SMB Direct requires Version >=3.0\n");
842 		return -EOPNOTSUPP;
843 	}
844 
845 #ifndef CONFIG_KEYS
846 	/* Muliuser mounts require CONFIG_KEYS support */
847 	if (ctx->multiuser) {
848 		cifs_errorf(fc, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
849 		return -1;
850 	}
851 #endif
852 
853 	if (ctx->got_version == false)
854 		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");
855 
856 
857 	if (!ctx->UNC) {
858 		cifs_errorf(fc, "CIFS mount error: No usable UNC path provided in device string!\n");
859 		return -1;
860 	}
861 
862 	/* make sure UNC has a share name */
863 	if (strlen(ctx->UNC) < 3 || !strchr(ctx->UNC + 3, '\\')) {
864 		cifs_errorf(fc, "Malformed UNC. Unable to find share name.\n");
865 		return -ENOENT;
866 	}
867 
868 	if (!ctx->got_ip) {
869 		int len;
870 		const char *slash;
871 
872 		/* No ip= option specified? Try to get it from UNC */
873 		/* Use the address part of the UNC. */
874 		slash = strchr(&ctx->UNC[2], '\\');
875 		len = slash - &ctx->UNC[2];
876 		if (!cifs_convert_address((struct sockaddr *)&ctx->dstaddr,
877 					  &ctx->UNC[2], len)) {
878 			pr_err("Unable to determine destination address\n");
879 			return -EHOSTUNREACH;
880 		}
881 	}
882 
883 	/* set the port that we got earlier */
884 	cifs_set_port((struct sockaddr *)&ctx->dstaddr, ctx->port);
885 
886 	if (ctx->uid_specified && !ctx->forceuid_specified) {
887 		ctx->override_uid = 1;
888 		pr_notice("enabling forceuid mount option implicitly because uid= option is specified\n");
889 	}
890 
891 	if (ctx->gid_specified && !ctx->forcegid_specified) {
892 		ctx->override_gid = 1;
893 		pr_notice("enabling forcegid mount option implicitly because gid= option is specified\n");
894 	}
895 
896 	if (ctx->override_uid && !ctx->uid_specified) {
897 		ctx->override_uid = 0;
898 		pr_notice("ignoring forceuid mount option specified with no uid= option\n");
899 	}
900 
901 	if (ctx->override_gid && !ctx->gid_specified) {
902 		ctx->override_gid = 0;
903 		pr_notice("ignoring forcegid mount option specified with no gid= option\n");
904 	}
905 
906 	return 0;
907 }
908 
909 static int smb3_get_tree_common(struct fs_context *fc)
910 {
911 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
912 	struct dentry *root;
913 	int rc = 0;
914 
915 	root = cifs_smb3_do_mount(fc->fs_type, 0, ctx);
916 	if (IS_ERR(root))
917 		return PTR_ERR(root);
918 
919 	fc->root = root;
920 
921 	return rc;
922 }
923 
924 /*
925  * Create an SMB3 superblock from the parameters passed.
926  */
927 static int smb3_get_tree(struct fs_context *fc)
928 {
929 	int err = smb3_fs_context_validate(fc);
930 	int ret;
931 
932 	if (err)
933 		return err;
934 	cifs_mount_lock();
935 	ret = smb3_get_tree_common(fc);
936 	cifs_mount_unlock();
937 	return ret;
938 }
939 
940 static void smb3_fs_context_free(struct fs_context *fc)
941 {
942 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
943 
944 	smb3_cleanup_fs_context(ctx);
945 }
946 
947 /*
948  * Compare the old and new proposed context during reconfigure
949  * and check if the changes are compatible.
950  */
951 static int smb3_verify_reconfigure_ctx(struct fs_context *fc,
952 				       struct smb3_fs_context *new_ctx,
953 				       struct smb3_fs_context *old_ctx, bool need_recon)
954 {
955 	if (new_ctx->posix_paths != old_ctx->posix_paths) {
956 		cifs_errorf(fc, "can not change posixpaths during remount\n");
957 		return -EINVAL;
958 	}
959 	if (new_ctx->sectype != old_ctx->sectype) {
960 		cifs_errorf(fc, "can not change sec during remount\n");
961 		return -EINVAL;
962 	}
963 	if (new_ctx->multiuser != old_ctx->multiuser) {
964 		cifs_errorf(fc, "can not change multiuser during remount\n");
965 		return -EINVAL;
966 	}
967 	if (new_ctx->UNC &&
968 	    (!old_ctx->UNC || strcmp(new_ctx->UNC, old_ctx->UNC))) {
969 		cifs_errorf(fc, "can not change UNC during remount\n");
970 		return -EINVAL;
971 	}
972 	if (new_ctx->username &&
973 	    (!old_ctx->username || strcmp(new_ctx->username, old_ctx->username))) {
974 		cifs_errorf(fc, "can not change username during remount\n");
975 		return -EINVAL;
976 	}
977 	if (new_ctx->password &&
978 	    (!old_ctx->password || strcmp(new_ctx->password, old_ctx->password))) {
979 		if (need_recon == false) {
980 			cifs_errorf(fc,
981 				    "can not change password of active session during remount\n");
982 			return -EINVAL;
983 		} else if (old_ctx->sectype == Kerberos) {
984 			cifs_errorf(fc,
985 				    "can not change password for Kerberos via remount\n");
986 			return -EINVAL;
987 		}
988 	}
989 	if (new_ctx->domainname &&
990 	    (!old_ctx->domainname || strcmp(new_ctx->domainname, old_ctx->domainname))) {
991 		cifs_errorf(fc, "can not change domainname during remount\n");
992 		return -EINVAL;
993 	}
994 	if (strcmp(new_ctx->workstation_name, old_ctx->workstation_name)) {
995 		cifs_errorf(fc, "can not change workstation_name during remount\n");
996 		return -EINVAL;
997 	}
998 	if (new_ctx->nodename &&
999 	    (!old_ctx->nodename || strcmp(new_ctx->nodename, old_ctx->nodename))) {
1000 		cifs_errorf(fc, "can not change nodename during remount\n");
1001 		return -EINVAL;
1002 	}
1003 	if (new_ctx->iocharset &&
1004 	    (!old_ctx->iocharset || strcmp(new_ctx->iocharset, old_ctx->iocharset))) {
1005 		cifs_errorf(fc, "can not change iocharset during remount\n");
1006 		return -EINVAL;
1007 	}
1008 	if (new_ctx->unicode != old_ctx->unicode) {
1009 		cifs_errorf(fc, "can not change unicode during remount\n");
1010 		return -EINVAL;
1011 	}
1012 	if (new_ctx->rfc1001_sessinit != old_ctx->rfc1001_sessinit) {
1013 		cifs_errorf(fc, "can not change nbsessinit during remount\n");
1014 		return -EINVAL;
1015 	}
1016 
1017 	return 0;
1018 }
1019 
1020 #define STEAL_STRING(cifs_sb, ctx, field)				\
1021 do {									\
1022 	kfree(ctx->field);						\
1023 	ctx->field = cifs_sb->ctx->field;				\
1024 	cifs_sb->ctx->field = NULL;					\
1025 } while (0)
1026 
1027 #define STEAL_STRING_SENSITIVE(cifs_sb, ctx, field)			\
1028 do {									\
1029 	kfree_sensitive(ctx->field);					\
1030 	ctx->field = cifs_sb->ctx->field;				\
1031 	cifs_sb->ctx->field = NULL;					\
1032 } while (0)
1033 
1034 int smb3_sync_session_ctx_passwords(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
1035 {
1036 	if (ses->password &&
1037 	    cifs_sb->ctx->password &&
1038 	    strcmp(ses->password, cifs_sb->ctx->password)) {
1039 		kfree_sensitive(cifs_sb->ctx->password);
1040 		cifs_sb->ctx->password = kstrdup(ses->password, GFP_KERNEL);
1041 		if (!cifs_sb->ctx->password)
1042 			return -ENOMEM;
1043 	}
1044 	if (ses->password2 &&
1045 	    cifs_sb->ctx->password2 &&
1046 	    strcmp(ses->password2, cifs_sb->ctx->password2)) {
1047 		kfree_sensitive(cifs_sb->ctx->password2);
1048 		cifs_sb->ctx->password2 = kstrdup(ses->password2, GFP_KERNEL);
1049 		if (!cifs_sb->ctx->password2) {
1050 			kfree_sensitive(cifs_sb->ctx->password);
1051 			cifs_sb->ctx->password = NULL;
1052 			return -ENOMEM;
1053 		}
1054 	}
1055 	return 0;
1056 }
1057 
1058 /*
1059  * smb3_sync_ses_chan_max - Synchronize the session's maximum channel count
1060  * @ses: pointer to the old CIFS session structure
1061  * @max_channels: new maximum number of channels to allow
1062  *
1063  * Updates the session's chan_max field to the new value, protecting the update
1064  * with the session's channel lock. This should be called whenever the maximum
1065  * allowed channels for a session changes (e.g., after a remount or reconfigure).
1066  */
1067 static void smb3_sync_ses_chan_max(struct cifs_ses *ses, unsigned int max_channels)
1068 {
1069 	spin_lock(&ses->chan_lock);
1070 	ses->chan_max = max_channels;
1071 	spin_unlock(&ses->chan_lock);
1072 }
1073 
1074 static int smb3_reconfigure(struct fs_context *fc)
1075 {
1076 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
1077 	struct dentry *root = fc->root;
1078 	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
1079 	struct cifs_ses *ses = cifs_sb_master_tcon(cifs_sb)->ses;
1080 	unsigned int rsize = ctx->rsize, wsize = ctx->wsize;
1081 	char *new_password = NULL, *new_password2 = NULL;
1082 	bool need_recon = false;
1083 	int rc;
1084 
1085 	if (ses->expired_pwd)
1086 		need_recon = true;
1087 
1088 	rc = smb3_verify_reconfigure_ctx(fc, ctx, cifs_sb->ctx, need_recon);
1089 	if (rc)
1090 		return rc;
1091 
1092 	/*
1093 	 * We can not change UNC/username/password/domainname/
1094 	 * workstation_name/nodename/iocharset
1095 	 * during reconnect so ignore what we have in the new context and
1096 	 * just use what we already have in cifs_sb->ctx.
1097 	 */
1098 	STEAL_STRING(cifs_sb, ctx, UNC);
1099 	STEAL_STRING(cifs_sb, ctx, source);
1100 	STEAL_STRING(cifs_sb, ctx, username);
1101 
1102 	if (need_recon == false)
1103 		STEAL_STRING_SENSITIVE(cifs_sb, ctx, password);
1104 	else  {
1105 		if (ctx->password) {
1106 			new_password = kstrdup(ctx->password, GFP_KERNEL);
1107 			if (!new_password)
1108 				return -ENOMEM;
1109 		} else
1110 			STEAL_STRING_SENSITIVE(cifs_sb, ctx, password);
1111 	}
1112 
1113 	/*
1114 	 * if a new password2 has been specified, then reset it's value
1115 	 * inside the ses struct
1116 	 */
1117 	if (ctx->password2) {
1118 		new_password2 = kstrdup(ctx->password2, GFP_KERNEL);
1119 		if (!new_password2) {
1120 			kfree_sensitive(new_password);
1121 			return -ENOMEM;
1122 		}
1123 	} else
1124 		STEAL_STRING_SENSITIVE(cifs_sb, ctx, password2);
1125 
1126 	/*
1127 	 * we may update the passwords in the ses struct below. Make sure we do
1128 	 * not race with smb2_reconnect
1129 	 */
1130 	mutex_lock(&ses->session_mutex);
1131 
1132 	/*
1133 	 * smb2_reconnect may swap password and password2 in case session setup
1134 	 * failed. First get ctx passwords in sync with ses passwords. It should
1135 	 * be okay to do this even if this function were to return an error at a
1136 	 * later stage
1137 	 */
1138 	rc = smb3_sync_session_ctx_passwords(cifs_sb, ses);
1139 	if (rc) {
1140 		mutex_unlock(&ses->session_mutex);
1141 		kfree_sensitive(new_password);
1142 		kfree_sensitive(new_password2);
1143 		return rc;
1144 	}
1145 
1146 	/*
1147 	 * now that allocations for passwords are done, commit them
1148 	 */
1149 	if (new_password) {
1150 		kfree_sensitive(ses->password);
1151 		ses->password = new_password;
1152 	}
1153 	if (new_password2) {
1154 		kfree_sensitive(ses->password2);
1155 		ses->password2 = new_password2;
1156 	}
1157 
1158 	/*
1159 	 * If multichannel or max_channels has changed, update the session's channels accordingly.
1160 	 * This may add or remove channels to match the new configuration.
1161 	 */
1162 	if ((ctx->multichannel != cifs_sb->ctx->multichannel) ||
1163 	    (ctx->max_channels != cifs_sb->ctx->max_channels)) {
1164 
1165 		/* Synchronize ses->chan_max with the new mount context */
1166 		smb3_sync_ses_chan_max(ses, ctx->max_channels);
1167 		/* Now update the session's channels to match the new configuration */
1168 		/* Prevent concurrent scaling operations */
1169 		spin_lock(&ses->ses_lock);
1170 		if (ses->flags & CIFS_SES_FLAG_SCALE_CHANNELS) {
1171 			spin_unlock(&ses->ses_lock);
1172 			mutex_unlock(&ses->session_mutex);
1173 			return -EINVAL;
1174 		}
1175 		ses->flags |= CIFS_SES_FLAG_SCALE_CHANNELS;
1176 		spin_unlock(&ses->ses_lock);
1177 
1178 		mutex_unlock(&ses->session_mutex);
1179 
1180 		rc = smb3_update_ses_channels(ses, ses->server,
1181 					       false /* from_reconnect */,
1182 					       false /* disable_mchan */);
1183 
1184 		/* Clear scaling flag after operation */
1185 		spin_lock(&ses->ses_lock);
1186 		ses->flags &= ~CIFS_SES_FLAG_SCALE_CHANNELS;
1187 		spin_unlock(&ses->ses_lock);
1188 	} else {
1189 		mutex_unlock(&ses->session_mutex);
1190 	}
1191 
1192 	STEAL_STRING(cifs_sb, ctx, domainname);
1193 	STEAL_STRING(cifs_sb, ctx, nodename);
1194 	STEAL_STRING(cifs_sb, ctx, iocharset);
1195 
1196 	/* if rsize or wsize not passed in on remount, use previous values */
1197 	ctx->rsize = rsize ? CIFS_ALIGN_RSIZE(fc, rsize) : cifs_sb->ctx->rsize;
1198 	ctx->wsize = wsize ? CIFS_ALIGN_WSIZE(fc, wsize) : cifs_sb->ctx->wsize;
1199 
1200 	smb3_cleanup_fs_context_contents(cifs_sb->ctx);
1201 	rc = smb3_fs_context_dup(cifs_sb->ctx, ctx);
1202 	smb3_update_mnt_flags(cifs_sb);
1203 #ifdef CONFIG_CIFS_DFS_UPCALL
1204 	if (!rc)
1205 		rc = dfs_cache_remount_fs(cifs_sb);
1206 #endif
1207 
1208 	return rc;
1209 }
1210 
1211 static int smb3_fs_context_parse_param(struct fs_context *fc,
1212 				      struct fs_parameter *param)
1213 {
1214 	struct fs_parse_result result;
1215 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
1216 	int i, opt;
1217 	bool is_smb3 = !strcmp(fc->fs_type->name, "smb3");
1218 	bool skip_parsing = false;
1219 	char *hostname;
1220 
1221 	cifs_dbg(FYI, "CIFS: parsing cifs mount option '%s'\n", param->key);
1222 
1223 	/*
1224 	 * fs_parse can not handle string options with an empty value so
1225 	 * we will need special handling of them.
1226 	 */
1227 	if (param->type == fs_value_is_string && param->string[0] == 0) {
1228 		if (!strcmp("pass", param->key) || !strcmp("password", param->key)) {
1229 			skip_parsing = true;
1230 			opt = Opt_pass;
1231 		} else if (!strcmp("user", param->key) || !strcmp("username", param->key)) {
1232 			skip_parsing = true;
1233 			opt = Opt_user;
1234 		} else if (!strcmp("pass2", param->key) || !strcmp("password2", param->key)) {
1235 			skip_parsing = true;
1236 			opt = Opt_pass2;
1237 		}
1238 	}
1239 
1240 	if (!skip_parsing) {
1241 		opt = fs_parse(fc, smb3_fs_parameters, param, &result);
1242 		if (opt < 0)
1243 			return ctx->sloppy ? 1 : opt;
1244 	}
1245 
1246 	switch (opt) {
1247 	case Opt_compress:
1248 		if (!IS_ENABLED(CONFIG_CIFS_COMPRESSION)) {
1249 			cifs_errorf(fc, "CONFIG_CIFS_COMPRESSION kernel config option is unset\n");
1250 			goto cifs_parse_mount_err;
1251 		}
1252 		ctx->compress = true;
1253 		cifs_dbg(VFS, "SMB3 compression support is experimental\n");
1254 		break;
1255 	case Opt_nodfs:
1256 		ctx->nodfs = 1;
1257 		break;
1258 	case Opt_hard:
1259 		if (result.negated) {
1260 			if (ctx->retry == 1)
1261 				cifs_dbg(VFS, "conflicting hard vs. soft mount options\n");
1262 			ctx->retry = 0;
1263 		} else
1264 			ctx->retry = 1;
1265 		break;
1266 	case Opt_soft:
1267 		if (result.negated)
1268 			ctx->retry = 1;
1269 		else {
1270 			if (ctx->retry == 1)
1271 				cifs_dbg(VFS, "conflicting hard vs soft mount options\n");
1272 			ctx->retry = 0;
1273 		}
1274 		break;
1275 	case Opt_mapposix:
1276 		if (result.negated)
1277 			ctx->remap = false;
1278 		else {
1279 			ctx->remap = true;
1280 			ctx->sfu_remap = false; /* disable SFU mapping */
1281 		}
1282 		break;
1283 	case Opt_mapchars:
1284 		if (result.negated)
1285 			ctx->sfu_remap = false;
1286 		else {
1287 			ctx->sfu_remap = true;
1288 			ctx->remap = false; /* disable SFM (mapposix) mapping */
1289 		}
1290 		break;
1291 	case Opt_user_xattr:
1292 		if (result.negated)
1293 			ctx->no_xattr = 1;
1294 		else
1295 			ctx->no_xattr = 0;
1296 		break;
1297 	case Opt_forceuid:
1298 		if (result.negated)
1299 			ctx->override_uid = 0;
1300 		else
1301 			ctx->override_uid = 1;
1302 		ctx->forceuid_specified = true;
1303 		break;
1304 	case Opt_forcegid:
1305 		if (result.negated)
1306 			ctx->override_gid = 0;
1307 		else
1308 			ctx->override_gid = 1;
1309 		ctx->forcegid_specified = true;
1310 		break;
1311 	case Opt_perm:
1312 		if (result.negated)
1313 			ctx->noperm = 1;
1314 		else
1315 			ctx->noperm = 0;
1316 		break;
1317 	case Opt_dynperm:
1318 		if (result.negated)
1319 			ctx->dynperm = 0;
1320 		else
1321 			ctx->dynperm = 1;
1322 		break;
1323 	case Opt_sfu:
1324 		if (result.negated)
1325 			ctx->sfu_emul = 0;
1326 		else
1327 			ctx->sfu_emul = 1;
1328 		break;
1329 	case Opt_noblocksend:
1330 		ctx->noblocksnd = 1;
1331 		break;
1332 	case Opt_noautotune:
1333 		ctx->noautotune = 1;
1334 		break;
1335 	case Opt_nolease:
1336 		ctx->no_lease = 1;
1337 		break;
1338 	case Opt_nosparse:
1339 		ctx->no_sparse = 1;
1340 		break;
1341 	case Opt_nodelete:
1342 		ctx->nodelete = 1;
1343 		break;
1344 	case Opt_multichannel:
1345 		ctx->multichannel_specified = true;
1346 		if (result.negated)
1347 			ctx->multichannel = false;
1348 		else
1349 			ctx->multichannel = true;
1350 		break;
1351 	case Opt_uid:
1352 		ctx->linux_uid = result.uid;
1353 		ctx->uid_specified = true;
1354 		break;
1355 	case Opt_cruid:
1356 		ctx->cred_uid = result.uid;
1357 		ctx->cruid_specified = true;
1358 		break;
1359 	case Opt_backupuid:
1360 		ctx->backupuid = result.uid;
1361 		ctx->backupuid_specified = true;
1362 		break;
1363 	case Opt_backupgid:
1364 		ctx->backupgid = result.gid;
1365 		ctx->backupgid_specified = true;
1366 		break;
1367 	case Opt_gid:
1368 		ctx->linux_gid = result.gid;
1369 		ctx->gid_specified = true;
1370 		break;
1371 	case Opt_port:
1372 		ctx->port = result.uint_32;
1373 		break;
1374 	case Opt_file_mode:
1375 		ctx->file_mode = result.uint_32;
1376 		break;
1377 	case Opt_dirmode:
1378 		ctx->dir_mode = result.uint_32;
1379 		break;
1380 	case Opt_min_enc_offload:
1381 		ctx->min_offload = result.uint_32;
1382 		break;
1383 	case Opt_retrans:
1384 		ctx->retrans = result.uint_32;
1385 		break;
1386 	case Opt_blocksize:
1387 		/*
1388 		 * inode blocksize realistically should never need to be
1389 		 * less than 16K or greater than 16M and default is 1MB.
1390 		 * Note that small inode block sizes (e.g. 64K) can lead
1391 		 * to very poor performance of common tools like cp and scp
1392 		 */
1393 		if ((result.uint_32 < CIFS_MAX_MSGSIZE) ||
1394 		   (result.uint_32 > (4 * SMB3_DEFAULT_IOSIZE))) {
1395 			cifs_errorf(fc, "%s: Invalid blocksize\n",
1396 				__func__);
1397 			goto cifs_parse_mount_err;
1398 		}
1399 		ctx->bsize = CIFS_ALIGN_BSIZE(fc, result.uint_32);
1400 		ctx->got_bsize = true;
1401 		break;
1402 	case Opt_rasize:
1403 		/*
1404 		 * readahead size realistically should never need to be
1405 		 * less than 1M (CIFS_DEFAULT_IOSIZE) or greater than 32M
1406 		 * (perhaps an exception should be considered in the
1407 		 * for the case of a large number of channels
1408 		 * when multichannel is negotiated) since that would lead
1409 		 * to plenty of parallel I/O in flight to the server.
1410 		 * Note that smaller read ahead sizes would
1411 		 * hurt performance of common tools like cp and scp
1412 		 * which often trigger sequential i/o with read ahead
1413 		 */
1414 		if ((result.uint_32 > (8 * SMB3_DEFAULT_IOSIZE)) ||
1415 		    (result.uint_32 < CIFS_DEFAULT_IOSIZE)) {
1416 			cifs_errorf(fc, "%s: Invalid rasize %d vs. %d\n",
1417 				__func__, result.uint_32, SMB3_DEFAULT_IOSIZE);
1418 			goto cifs_parse_mount_err;
1419 		}
1420 		ctx->rasize = result.uint_32;
1421 		break;
1422 	case Opt_rsize:
1423 		ctx->rsize = CIFS_ALIGN_RSIZE(fc, result.uint_32);
1424 		ctx->got_rsize = true;
1425 		ctx->vol_rsize = ctx->rsize;
1426 		break;
1427 	case Opt_wsize:
1428 		ctx->wsize = CIFS_ALIGN_WSIZE(fc, result.uint_32);
1429 		ctx->got_wsize = true;
1430 		ctx->vol_wsize = ctx->wsize;
1431 		break;
1432 	case Opt_acregmax:
1433 		if (result.uint_32 > CIFS_MAX_ACTIMEO / HZ) {
1434 			cifs_errorf(fc, "acregmax too large\n");
1435 			goto cifs_parse_mount_err;
1436 		}
1437 		ctx->acregmax = HZ * result.uint_32;
1438 		break;
1439 	case Opt_acdirmax:
1440 		if (result.uint_32 > CIFS_MAX_ACTIMEO / HZ) {
1441 			cifs_errorf(fc, "acdirmax too large\n");
1442 			goto cifs_parse_mount_err;
1443 		}
1444 		ctx->acdirmax = HZ * result.uint_32;
1445 		break;
1446 	case Opt_actimeo:
1447 		if (result.uint_32 > CIFS_MAX_ACTIMEO / HZ) {
1448 			cifs_errorf(fc, "timeout too large\n");
1449 			goto cifs_parse_mount_err;
1450 		}
1451 		if ((ctx->acdirmax != CIFS_DEF_ACTIMEO) ||
1452 		    (ctx->acregmax != CIFS_DEF_ACTIMEO)) {
1453 			cifs_errorf(fc, "actimeo ignored since acregmax or acdirmax specified\n");
1454 			break;
1455 		}
1456 		ctx->acdirmax = ctx->acregmax = HZ * result.uint_32;
1457 		break;
1458 	case Opt_closetimeo:
1459 		if (result.uint_32 > SMB3_MAX_DCLOSETIMEO / HZ) {
1460 			cifs_errorf(fc, "closetimeo too large\n");
1461 			goto cifs_parse_mount_err;
1462 		}
1463 		ctx->closetimeo = HZ * result.uint_32;
1464 		break;
1465 	case Opt_echo_interval:
1466 		if (result.uint_32 < SMB_ECHO_INTERVAL_MIN ||
1467 		    result.uint_32 > SMB_ECHO_INTERVAL_MAX) {
1468 			cifs_errorf(fc, "echo interval is out of bounds\n");
1469 			goto cifs_parse_mount_err;
1470 		}
1471 		ctx->echo_interval = result.uint_32;
1472 		break;
1473 	case Opt_snapshot:
1474 		ctx->snapshot_time = result.uint_64;
1475 		break;
1476 	case Opt_max_credits:
1477 		if (result.uint_32 < 20 || result.uint_32 > 60000) {
1478 			cifs_errorf(fc, "%s: Invalid max_credits value\n",
1479 				 __func__);
1480 			goto cifs_parse_mount_err;
1481 		}
1482 		ctx->max_credits = result.uint_32;
1483 		break;
1484 	case Opt_max_channels:
1485 		ctx->max_channels_specified = true;
1486 		if (result.uint_32 < 1 || result.uint_32 > CIFS_MAX_CHANNELS) {
1487 			cifs_errorf(fc, "%s: Invalid max_channels value, needs to be 1-%d\n",
1488 				 __func__, CIFS_MAX_CHANNELS);
1489 			goto cifs_parse_mount_err;
1490 		}
1491 		ctx->max_channels = result.uint_32;
1492 		break;
1493 	case Opt_max_cached_dirs:
1494 		if (result.uint_32 < 1) {
1495 			cifs_errorf(fc, "%s: Invalid max_cached_dirs, needs to be 1 or more\n",
1496 				    __func__);
1497 			goto cifs_parse_mount_err;
1498 		}
1499 		ctx->max_cached_dirs = result.uint_32;
1500 		break;
1501 	case Opt_handletimeout:
1502 		ctx->handle_timeout = result.uint_32;
1503 		if (ctx->handle_timeout > SMB3_MAX_HANDLE_TIMEOUT) {
1504 			cifs_errorf(fc, "Invalid handle cache timeout, longer than 16 minutes\n");
1505 			goto cifs_parse_mount_err;
1506 		}
1507 		break;
1508 	case Opt_source:
1509 		kfree(ctx->UNC);
1510 		ctx->UNC = NULL;
1511 		switch (smb3_parse_devname(param->string, ctx)) {
1512 		case 0:
1513 			break;
1514 		case -ENOMEM:
1515 			cifs_errorf(fc, "Unable to allocate memory for devname\n");
1516 			goto cifs_parse_mount_err;
1517 		case -EINVAL:
1518 			cifs_errorf(fc, "Malformed UNC in devname\n");
1519 			goto cifs_parse_mount_err;
1520 		default:
1521 			cifs_errorf(fc, "Unknown error parsing devname\n");
1522 			goto cifs_parse_mount_err;
1523 		}
1524 		kfree(ctx->source);
1525 		ctx->source = smb3_fs_context_fullpath(ctx, '/');
1526 		if (IS_ERR(ctx->source)) {
1527 			ctx->source = NULL;
1528 			cifs_errorf(fc, "OOM when copying UNC string\n");
1529 			goto cifs_parse_mount_err;
1530 		}
1531 		kfree(fc->source);
1532 		fc->source = kstrdup(ctx->source, GFP_KERNEL);
1533 		if (fc->source == NULL) {
1534 			cifs_errorf(fc, "OOM when copying UNC string\n");
1535 			goto cifs_parse_mount_err;
1536 		}
1537 		hostname = extract_hostname(ctx->UNC);
1538 		if (IS_ERR(hostname)) {
1539 			cifs_errorf(fc, "Cannot extract hostname from UNC string\n");
1540 			goto cifs_parse_mount_err;
1541 		}
1542 		/* last byte, type, is 0x20 for servr type */
1543 		memset(ctx->target_rfc1001_name, 0x20, RFC1001_NAME_LEN_WITH_NULL);
1544 		for (i = 0; i < RFC1001_NAME_LEN && hostname[i] != 0; i++)
1545 			ctx->target_rfc1001_name[i] = toupper(hostname[i]);
1546 		kfree(hostname);
1547 		break;
1548 	case Opt_user:
1549 		kfree(ctx->username);
1550 		ctx->username = NULL;
1551 		if (ctx->nullauth)
1552 			break;
1553 		if (strlen(param->string) == 0) {
1554 			/* null user, ie. anonymous authentication */
1555 			ctx->nullauth = 1;
1556 			break;
1557 		}
1558 
1559 		if (strnlen(param->string, CIFS_MAX_USERNAME_LEN) ==
1560 		    CIFS_MAX_USERNAME_LEN) {
1561 			pr_warn("username too long\n");
1562 			goto cifs_parse_mount_err;
1563 		}
1564 		ctx->username = no_free_ptr(param->string);
1565 		break;
1566 	case Opt_pass:
1567 		kfree_sensitive(ctx->password);
1568 		ctx->password = NULL;
1569 		if (strlen(param->string) == 0)
1570 			break;
1571 		ctx->password = no_free_ptr(param->string);
1572 		break;
1573 	case Opt_pass2:
1574 		kfree_sensitive(ctx->password2);
1575 		ctx->password2 = NULL;
1576 		if (strlen(param->string) == 0)
1577 			break;
1578 		ctx->password2 = no_free_ptr(param->string);
1579 		break;
1580 	case Opt_ip:
1581 		if (strlen(param->string) == 0) {
1582 			ctx->got_ip = false;
1583 			break;
1584 		}
1585 		if (!cifs_convert_address((struct sockaddr *)&ctx->dstaddr,
1586 					  param->string,
1587 					  strlen(param->string))) {
1588 			pr_err("bad ip= option (%s)\n", param->string);
1589 			goto cifs_parse_mount_err;
1590 		}
1591 		ctx->got_ip = true;
1592 		break;
1593 	case Opt_domain:
1594 		if (strnlen(param->string, CIFS_MAX_DOMAINNAME_LEN)
1595 				== CIFS_MAX_DOMAINNAME_LEN) {
1596 			pr_warn("domain name too long\n");
1597 			goto cifs_parse_mount_err;
1598 		}
1599 
1600 		kfree(ctx->domainname);
1601 		ctx->domainname = no_free_ptr(param->string);
1602 		cifs_dbg(FYI, "Domain name set\n");
1603 		break;
1604 	case Opt_srcaddr:
1605 		if (!cifs_convert_address(
1606 				(struct sockaddr *)&ctx->srcaddr,
1607 				param->string, strlen(param->string))) {
1608 			pr_warn("Could not parse srcaddr: %s\n",
1609 				param->string);
1610 			goto cifs_parse_mount_err;
1611 		}
1612 		break;
1613 	case Opt_iocharset:
1614 		if (strnlen(param->string, 1024) >= 65) {
1615 			pr_warn("iocharset name too long\n");
1616 			goto cifs_parse_mount_err;
1617 		}
1618 
1619 		if (strncasecmp(param->string, "default", 7) != 0) {
1620 			kfree(ctx->iocharset);
1621 			ctx->iocharset = no_free_ptr(param->string);
1622 		}
1623 		/* if iocharset not set then load_nls_default
1624 		 * is used by caller
1625 		 */
1626 		cifs_dbg(FYI, "iocharset set to %s\n", ctx->iocharset);
1627 		break;
1628 	case Opt_netbiosname:
1629 		memset(ctx->source_rfc1001_name, 0x20,
1630 			RFC1001_NAME_LEN);
1631 		/*
1632 		 * FIXME: are there cases in which a comma can
1633 		 * be valid in workstation netbios name (and
1634 		 * need special handling)?
1635 		 */
1636 		for (i = 0; i < RFC1001_NAME_LEN; i++) {
1637 			/* don't ucase netbiosname for user */
1638 			if (param->string[i] == 0)
1639 				break;
1640 			ctx->source_rfc1001_name[i] = param->string[i];
1641 		}
1642 		/* The string has 16th byte zero still from
1643 		 * set at top of the function
1644 		 */
1645 		if (i == RFC1001_NAME_LEN && param->string[i] != 0)
1646 			pr_warn("netbiosname longer than 15 truncated\n");
1647 		break;
1648 	case Opt_servern:
1649 		/* last byte, type, is 0x20 for servr type */
1650 		memset(ctx->target_rfc1001_name, 0x20,
1651 			RFC1001_NAME_LEN_WITH_NULL);
1652 		/*
1653 		 * BB are there cases in which a comma can be valid in this
1654 		 * workstation netbios name (and need special handling)?
1655 		 */
1656 
1657 		/* user or mount helper must uppercase the netbios name */
1658 		for (i = 0; i < 15; i++) {
1659 			if (param->string[i] == 0)
1660 				break;
1661 			ctx->target_rfc1001_name[i] = param->string[i];
1662 		}
1663 
1664 		/* The string has 16th byte zero still from set at top of function */
1665 		if (i == RFC1001_NAME_LEN && param->string[i] != 0)
1666 			pr_warn("server netbiosname longer than 15 truncated\n");
1667 		break;
1668 	case Opt_nbsessinit:
1669 		ctx->rfc1001_sessinit = !result.negated;
1670 		cifs_dbg(FYI, "rfc1001_sessinit set to %d\n", ctx->rfc1001_sessinit);
1671 		break;
1672 	case Opt_ver:
1673 		/* version of mount userspace tools, not dialect */
1674 		/* If interface changes in mount.cifs bump to new ver */
1675 		if (strncasecmp(param->string, "1", 1) == 0) {
1676 			if (strlen(param->string) > 1) {
1677 				pr_warn("Bad mount helper ver=%s. Did you want SMB1 (CIFS) dialect and mean to type vers=1.0 instead?\n",
1678 					param->string);
1679 				goto cifs_parse_mount_err;
1680 			}
1681 			/* This is the default */
1682 			break;
1683 		}
1684 		/* For all other value, error */
1685 		pr_warn("Invalid mount helper version specified\n");
1686 		goto cifs_parse_mount_err;
1687 	case Opt_vers:
1688 		/* protocol version (dialect) */
1689 		if (cifs_parse_smb_version(fc, param->string, ctx, is_smb3) != 0)
1690 			goto cifs_parse_mount_err;
1691 		ctx->got_version = true;
1692 		break;
1693 	case Opt_sec:
1694 		if (cifs_parse_security_flavors(fc, param->string, ctx) != 0)
1695 			goto cifs_parse_mount_err;
1696 		break;
1697 	case Opt_upcalltarget:
1698 		if (cifs_parse_upcall_target(fc, param->string, ctx) != 0)
1699 			goto cifs_parse_mount_err;
1700 		break;
1701 	case Opt_cache:
1702 		if (cifs_parse_cache_flavor(fc, param->string, ctx) != 0)
1703 			goto cifs_parse_mount_err;
1704 		break;
1705 	case Opt_witness:
1706 #ifndef CONFIG_CIFS_SWN_UPCALL
1707 		cifs_errorf(fc, "Witness support needs CONFIG_CIFS_SWN_UPCALL config option\n");
1708 			goto cifs_parse_mount_err;
1709 #endif
1710 		ctx->witness = true;
1711 		pr_warn_once("Witness protocol support is experimental\n");
1712 		break;
1713 	case Opt_unicode:
1714 		ctx->unicode = !result.negated;
1715 		cifs_dbg(FYI, "unicode set to %d\n", ctx->unicode);
1716 		break;
1717 	case Opt_rootfs:
1718 #ifndef CONFIG_CIFS_ROOT
1719 		cifs_dbg(VFS, "rootfs support requires CONFIG_CIFS_ROOT config option\n");
1720 		goto cifs_parse_mount_err;
1721 #endif
1722 		ctx->rootfs = true;
1723 		break;
1724 	case Opt_posixpaths:
1725 		if (result.negated)
1726 			ctx->posix_paths = 0;
1727 		else
1728 			ctx->posix_paths = 1;
1729 		break;
1730 	case Opt_unix:
1731 		if (result.negated) {
1732 			if (ctx->linux_ext == 1)
1733 				pr_warn_once("conflicting posix mount options specified\n");
1734 			ctx->linux_ext = 0;
1735 			ctx->no_linux_ext = 1;
1736 		} else {
1737 			if (ctx->no_linux_ext == 1)
1738 				pr_warn_once("conflicting posix mount options specified\n");
1739 			ctx->linux_ext = 1;
1740 			ctx->no_linux_ext = 0;
1741 			ctx->nonativesocket = 1; /* POSIX mounts use NFS style reparse points */
1742 		}
1743 		break;
1744 	case Opt_nocase:
1745 		ctx->nocase = 1;
1746 		break;
1747 	case Opt_brl:
1748 		if (result.negated) {
1749 			/*
1750 			 * turn off mandatory locking in mode
1751 			 * if remote locking is turned off since the
1752 			 * local vfs will do advisory
1753 			 */
1754 			if (ctx->file_mode ==
1755 				(S_IALLUGO & ~(S_ISUID | S_IXGRP)))
1756 				ctx->file_mode = S_IALLUGO;
1757 			ctx->nobrl =  1;
1758 		} else
1759 			ctx->nobrl =  0;
1760 		break;
1761 	case Opt_handlecache:
1762 		if (result.negated)
1763 			ctx->nohandlecache = 1;
1764 		else
1765 			ctx->nohandlecache = 0;
1766 		break;
1767 	case Opt_forcemandatorylock:
1768 		ctx->mand_lock = 1;
1769 		break;
1770 	case Opt_setuids:
1771 		ctx->setuids = result.negated;
1772 		break;
1773 	case Opt_intr:
1774 		ctx->intr = !result.negated;
1775 		break;
1776 	case Opt_setuidfromacl:
1777 		ctx->setuidfromacl = 1;
1778 		break;
1779 	case Opt_strictsync:
1780 		ctx->nostrictsync = result.negated;
1781 		break;
1782 	case Opt_serverino:
1783 		ctx->server_ino = !result.negated;
1784 		break;
1785 	case Opt_rwpidforward:
1786 		ctx->rwpidforward = 1;
1787 		break;
1788 	case Opt_modesid:
1789 		ctx->mode_ace = 1;
1790 		break;
1791 	case Opt_cifsacl:
1792 		ctx->cifs_acl = !result.negated;
1793 		break;
1794 	case Opt_acl:
1795 		ctx->no_psx_acl = result.negated;
1796 		break;
1797 	case Opt_locallease:
1798 		ctx->local_lease = 1;
1799 		break;
1800 	case Opt_sign:
1801 		ctx->sign = true;
1802 		break;
1803 	case Opt_ignore_signature:
1804 		ctx->sign = true;
1805 		ctx->ignore_signature = true;
1806 		break;
1807 	case Opt_seal:
1808 		/* we do not do the following in secFlags because seal
1809 		 * is a per tree connection (mount) not a per socket
1810 		 * or per-smb connection option in the protocol
1811 		 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1812 		 */
1813 		ctx->seal = 1;
1814 		break;
1815 	case Opt_noac:
1816 		pr_warn("Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
1817 		break;
1818 	case Opt_fsc:
1819 #ifndef CONFIG_CIFS_FSCACHE
1820 		cifs_errorf(fc, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
1821 		goto cifs_parse_mount_err;
1822 #endif
1823 		ctx->fsc = true;
1824 		break;
1825 	case Opt_mfsymlinks:
1826 		ctx->mfsymlinks = true;
1827 		break;
1828 	case Opt_multiuser:
1829 		ctx->multiuser = true;
1830 		break;
1831 	case Opt_sloppy:
1832 		ctx->sloppy = true;
1833 		break;
1834 	case Opt_nosharesock:
1835 		ctx->nosharesock = true;
1836 		break;
1837 	case Opt_persistent:
1838 		if (result.negated) {
1839 			ctx->nopersistent = true;
1840 			if (ctx->persistent) {
1841 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1842 				goto cifs_parse_mount_err;
1843 			}
1844 		} else {
1845 			ctx->persistent = true;
1846 			if ((ctx->nopersistent) || (ctx->resilient)) {
1847 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1848 				goto cifs_parse_mount_err;
1849 			}
1850 		}
1851 		break;
1852 	case Opt_resilient:
1853 		if (result.negated) {
1854 			ctx->resilient = false; /* already the default */
1855 		} else {
1856 			ctx->resilient = true;
1857 			if (ctx->persistent) {
1858 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1859 				goto cifs_parse_mount_err;
1860 			}
1861 		}
1862 		break;
1863 	case Opt_tcp_nodelay:
1864 		/* tcp nodelay should not usually be needed since we CORK/UNCORK the socket */
1865 		if (result.negated)
1866 			ctx->sockopt_tcp_nodelay = false;
1867 		else
1868 			ctx->sockopt_tcp_nodelay = true;
1869 		break;
1870 	case Opt_domainauto:
1871 		ctx->domainauto = true;
1872 		break;
1873 	case Opt_rdma:
1874 		ctx->rdma = true;
1875 		break;
1876 	case Opt_reparse:
1877 		if (parse_reparse_flavor(fc, param->string, ctx))
1878 			goto cifs_parse_mount_err;
1879 		break;
1880 	case Opt_nativesocket:
1881 		ctx->nonativesocket = result.negated;
1882 		break;
1883 	case Opt_symlink:
1884 		if (parse_symlink_flavor(fc, param->string, ctx))
1885 			goto cifs_parse_mount_err;
1886 		break;
1887 	case Opt_symlinkroot:
1888 		if (param->string[0] != '/') {
1889 			cifs_errorf(fc, "symlinkroot mount options must be absolute path\n");
1890 			goto cifs_parse_mount_err;
1891 		}
1892 		if (strnlen(param->string, PATH_MAX) == PATH_MAX) {
1893 			cifs_errorf(fc, "symlinkroot path too long (max path length: %u)\n",
1894 				    PATH_MAX - 1);
1895 			goto cifs_parse_mount_err;
1896 		}
1897 		kfree(ctx->symlinkroot);
1898 		ctx->symlinkroot = param->string;
1899 		param->string = NULL;
1900 		break;
1901 	}
1902 	/* case Opt_ignore: - is ignored as expected ... */
1903 
1904 	if (ctx->multiuser && ctx->upcall_target == UPTARGET_MOUNT) {
1905 		cifs_errorf(fc, "multiuser mount option not supported with upcalltarget set as 'mount'\n");
1906 		goto cifs_parse_mount_err;
1907 	}
1908 
1909 	return 0;
1910 
1911  cifs_parse_mount_err:
1912 	kfree_sensitive(ctx->password);
1913 	ctx->password = NULL;
1914 	kfree_sensitive(ctx->password2);
1915 	ctx->password2 = NULL;
1916 	kfree(ctx->source);
1917 	ctx->source = NULL;
1918 	kfree(fc->source);
1919 	fc->source = NULL;
1920 	return -EINVAL;
1921 }
1922 
1923 int smb3_init_fs_context(struct fs_context *fc)
1924 {
1925 	struct smb3_fs_context *ctx;
1926 	char *nodename = utsname()->nodename;
1927 	int i;
1928 
1929 	ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
1930 	if (unlikely(!ctx))
1931 		return -ENOMEM;
1932 
1933 	strscpy(ctx->workstation_name, nodename, sizeof(ctx->workstation_name));
1934 
1935 	/*
1936 	 * does not have to be perfect mapping since field is
1937 	 * informational, only used for servers that do not support
1938 	 * port 445 and it can be overridden at mount time
1939 	 */
1940 	memset(ctx->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1941 	for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
1942 		ctx->source_rfc1001_name[i] = toupper(nodename[i]);
1943 	ctx->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
1944 
1945 	/*
1946 	 * null target name indicates to use *SMBSERVR default called name
1947 	 *  if we end up sending RFC1001 session initialize
1948 	 */
1949 	ctx->target_rfc1001_name[0] = 0;
1950 
1951 	ctx->rfc1001_sessinit = -1; /* autodetect based on port number */
1952 
1953 	ctx->cred_uid = current_uid();
1954 	ctx->linux_uid = current_uid();
1955 	ctx->linux_gid = current_gid();
1956 	/* By default 4MB read ahead size, 1MB block size */
1957 	ctx->bsize = CIFS_DEFAULT_IOSIZE; /* can improve cp performance significantly */
1958 	ctx->rasize = 0; /* 0 = use default (ie negotiated rsize) for read ahead pages */
1959 
1960 	/*
1961 	 * default to SFM style remapping of seven reserved characters
1962 	 * unless user overrides it or we negotiate CIFS POSIX where
1963 	 * it is unnecessary.  Can not simultaneously use more than one mapping
1964 	 * since then readdir could list files that open could not open
1965 	 */
1966 	ctx->remap = true;
1967 
1968 	/* default to only allowing write access to owner of the mount */
1969 	ctx->dir_mode = ctx->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1970 
1971 	/* ctx->retry default is 0 (i.e. "soft" limited retry not hard retry) */
1972 	/* default is always to request posix paths. */
1973 	ctx->posix_paths = 1;
1974 	/* default to using server inode numbers where available */
1975 	ctx->server_ino = 1;
1976 
1977 	/* default is to use strict cifs caching semantics */
1978 	ctx->strict_io = true;
1979 
1980 	ctx->acregmax = CIFS_DEF_ACTIMEO;
1981 	ctx->acdirmax = CIFS_DEF_ACTIMEO;
1982 	ctx->closetimeo = SMB3_DEF_DCLOSETIMEO;
1983 	ctx->max_cached_dirs = MAX_CACHED_FIDS;
1984 	/* Most clients set timeout to 0, allows server to use its default */
1985 	ctx->handle_timeout = 0; /* See MS-SMB2 spec section 2.2.14.2.12 */
1986 
1987 	/* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1988 	ctx->ops = &smb30_operations;
1989 	ctx->vals = &smbdefault_values;
1990 
1991 	ctx->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1992 
1993 	/* default to no multichannel (single server connection) */
1994 	ctx->multichannel = false;
1995 	ctx->multichannel_specified = false;
1996 	ctx->max_channels_specified = false;
1997 	ctx->max_channels = 1;
1998 
1999 	ctx->backupuid_specified = false; /* no backup intent for a user */
2000 	ctx->backupgid_specified = false; /* no backup intent for a group */
2001 
2002 	ctx->retrans = 1;
2003 	ctx->reparse_type = CIFS_REPARSE_TYPE_DEFAULT;
2004 	ctx->symlink_type = CIFS_SYMLINK_TYPE_DEFAULT;
2005 	ctx->nonativesocket = 0;
2006 
2007 	ctx->unicode = -1; /* autodetect, but prefer UNICODE mode */
2008 
2009 /*
2010  *	short int override_uid = -1;
2011  *	short int override_gid = -1;
2012  *	char *nodename = strdup(utsname()->nodename);
2013  *	struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
2014  */
2015 
2016 	fc->fs_private = ctx;
2017 	fc->ops = &smb3_fs_context_ops;
2018 	return 0;
2019 }
2020 
2021 void
2022 smb3_cleanup_fs_context_contents(struct smb3_fs_context *ctx)
2023 {
2024 	if (ctx == NULL)
2025 		return;
2026 
2027 	/*
2028 	 * Make sure this stays in sync with smb3_fs_context_dup()
2029 	 */
2030 	kfree(ctx->username);
2031 	ctx->username = NULL;
2032 	kfree_sensitive(ctx->password);
2033 	ctx->password = NULL;
2034 	kfree_sensitive(ctx->password2);
2035 	ctx->password2 = NULL;
2036 	kfree(ctx->server_hostname);
2037 	ctx->server_hostname = NULL;
2038 	kfree(ctx->UNC);
2039 	ctx->UNC = NULL;
2040 	kfree(ctx->source);
2041 	ctx->source = NULL;
2042 	kfree(ctx->domainname);
2043 	ctx->domainname = NULL;
2044 	kfree(ctx->nodename);
2045 	ctx->nodename = NULL;
2046 	kfree(ctx->iocharset);
2047 	ctx->iocharset = NULL;
2048 	kfree(ctx->prepath);
2049 	ctx->prepath = NULL;
2050 	kfree(ctx->leaf_fullpath);
2051 	ctx->leaf_fullpath = NULL;
2052 	kfree(ctx->dns_dom);
2053 	ctx->dns_dom = NULL;
2054 	kfree(ctx->symlinkroot);
2055 	ctx->symlinkroot = NULL;
2056 }
2057 
2058 void
2059 smb3_cleanup_fs_context(struct smb3_fs_context *ctx)
2060 {
2061 	if (!ctx)
2062 		return;
2063 	smb3_cleanup_fs_context_contents(ctx);
2064 	kfree(ctx);
2065 }
2066 
2067 void smb3_update_mnt_flags(struct cifs_sb_info *cifs_sb)
2068 {
2069 	struct smb3_fs_context *ctx = cifs_sb->ctx;
2070 
2071 	if (ctx->nodfs)
2072 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
2073 	else
2074 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_DFS;
2075 
2076 	if (ctx->noperm)
2077 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
2078 	else
2079 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_PERM;
2080 
2081 	if (ctx->setuids)
2082 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
2083 	else
2084 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SET_UID;
2085 
2086 	if (ctx->setuidfromacl)
2087 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
2088 	else
2089 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_UID_FROM_ACL;
2090 
2091 	if (ctx->server_ino)
2092 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
2093 	else
2094 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
2095 
2096 	if (ctx->remap)
2097 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
2098 	else
2099 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MAP_SFM_CHR;
2100 
2101 	if (ctx->sfu_remap)
2102 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
2103 	else
2104 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MAP_SPECIAL_CHR;
2105 
2106 	if (ctx->no_xattr)
2107 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
2108 	else
2109 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_XATTR;
2110 
2111 	if (ctx->sfu_emul)
2112 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
2113 	else
2114 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_UNX_EMUL;
2115 
2116 	if (ctx->nobrl)
2117 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
2118 	else
2119 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_BRL;
2120 
2121 	if (ctx->nohandlecache)
2122 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
2123 	else
2124 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_HANDLE_CACHE;
2125 
2126 	if (ctx->nostrictsync)
2127 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
2128 	else
2129 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NOSSYNC;
2130 
2131 	if (ctx->mand_lock)
2132 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
2133 	else
2134 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NOPOSIXBRL;
2135 
2136 	if (ctx->rwpidforward)
2137 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
2138 	else
2139 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_RWPIDFORWARD;
2140 
2141 	if (ctx->mode_ace)
2142 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MODE_FROM_SID;
2143 	else
2144 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MODE_FROM_SID;
2145 
2146 	if (ctx->cifs_acl)
2147 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
2148 	else
2149 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_ACL;
2150 
2151 	if (ctx->backupuid_specified)
2152 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
2153 	else
2154 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_BACKUPUID;
2155 
2156 	if (ctx->backupgid_specified)
2157 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
2158 	else
2159 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_BACKUPGID;
2160 
2161 	if (ctx->override_uid)
2162 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
2163 	else
2164 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_OVERR_UID;
2165 
2166 	if (ctx->override_gid)
2167 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
2168 	else
2169 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_OVERR_GID;
2170 
2171 	if (ctx->dynperm)
2172 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
2173 	else
2174 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_DYNPERM;
2175 
2176 	if (ctx->fsc)
2177 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
2178 	else
2179 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_FSCACHE;
2180 
2181 	if (ctx->multiuser)
2182 		cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
2183 					    CIFS_MOUNT_NO_PERM);
2184 	else
2185 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MULTIUSER;
2186 
2187 
2188 	if (ctx->strict_io)
2189 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
2190 	else
2191 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_STRICT_IO;
2192 
2193 	if (ctx->direct_io)
2194 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
2195 	else
2196 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_DIRECT_IO;
2197 
2198 	if (ctx->mfsymlinks)
2199 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
2200 	else
2201 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MF_SYMLINKS;
2202 	if (ctx->mfsymlinks) {
2203 		if (ctx->sfu_emul) {
2204 			/*
2205 			 * Our SFU ("Services for Unix") emulation allows now
2206 			 * creating new and reading existing SFU symlinks.
2207 			 * Older Linux kernel versions were not able to neither
2208 			 * read existing nor create new SFU symlinks. But
2209 			 * creating and reading SFU style mknod and FIFOs was
2210 			 * supported for long time. When "mfsymlinks" and
2211 			 * "sfu" are both enabled at the same time, it allows
2212 			 * reading both types of symlinks, but will only create
2213 			 * them with mfsymlinks format. This allows better
2214 			 * Apple compatibility, compatibility with older Linux
2215 			 * kernel clients (probably better for Samba too)
2216 			 * while still recognizing old Windows style symlinks.
2217 			 */
2218 			cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
2219 		}
2220 	}
2221 	cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SHUTDOWN;
2222 
2223 	return;
2224 }
2225