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