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