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