File Index
Files and directories
Sign in
FILE

fs_context.c

File preview with syntax highlighting.

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