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