xref: /openbmc/linux/fs/nfs/fs_context.c (revision 97a532c3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/fs/nfs/fs_context.c
4  *
5  * Copyright (C) 1992 Rick Sladkey
6  * Conversion to new mount api Copyright (C) David Howells
7  *
8  * NFS mount handling.
9  *
10  * Split from fs/nfs/super.c by David Howells <dhowells@redhat.com>
11  */
12 
13 #include <linux/compat.h>
14 #include <linux/module.h>
15 #include <linux/fs.h>
16 #include <linux/fs_context.h>
17 #include <linux/fs_parser.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_mount.h>
20 #include <linux/nfs4_mount.h>
21 
22 #include <net/handshake.h>
23 
24 #include "nfs.h"
25 #include "internal.h"
26 
27 #include "nfstrace.h"
28 
29 #define NFSDBG_FACILITY		NFSDBG_MOUNT
30 
31 #if IS_ENABLED(CONFIG_NFS_V3)
32 #define NFS_DEFAULT_VERSION 3
33 #else
34 #define NFS_DEFAULT_VERSION 2
35 #endif
36 
37 #define NFS_MAX_CONNECTIONS 16
38 
39 enum nfs_param {
40 	Opt_ac,
41 	Opt_acdirmax,
42 	Opt_acdirmin,
43 	Opt_acl,
44 	Opt_acregmax,
45 	Opt_acregmin,
46 	Opt_actimeo,
47 	Opt_addr,
48 	Opt_bg,
49 	Opt_bsize,
50 	Opt_clientaddr,
51 	Opt_cto,
52 	Opt_fg,
53 	Opt_fscache,
54 	Opt_fscache_flag,
55 	Opt_hard,
56 	Opt_intr,
57 	Opt_local_lock,
58 	Opt_lock,
59 	Opt_lookupcache,
60 	Opt_migration,
61 	Opt_minorversion,
62 	Opt_mountaddr,
63 	Opt_mounthost,
64 	Opt_mountport,
65 	Opt_mountproto,
66 	Opt_mountvers,
67 	Opt_namelen,
68 	Opt_nconnect,
69 	Opt_max_connect,
70 	Opt_port,
71 	Opt_posix,
72 	Opt_proto,
73 	Opt_rdirplus,
74 	Opt_rdma,
75 	Opt_resvport,
76 	Opt_retrans,
77 	Opt_retry,
78 	Opt_rsize,
79 	Opt_sec,
80 	Opt_sharecache,
81 	Opt_sloppy,
82 	Opt_soft,
83 	Opt_softerr,
84 	Opt_softreval,
85 	Opt_source,
86 	Opt_tcp,
87 	Opt_timeo,
88 	Opt_trunkdiscovery,
89 	Opt_udp,
90 	Opt_v,
91 	Opt_vers,
92 	Opt_wsize,
93 	Opt_write,
94 	Opt_xprtsec,
95 };
96 
97 enum {
98 	Opt_local_lock_all,
99 	Opt_local_lock_flock,
100 	Opt_local_lock_none,
101 	Opt_local_lock_posix,
102 };
103 
104 static const struct constant_table nfs_param_enums_local_lock[] = {
105 	{ "all",		Opt_local_lock_all },
106 	{ "flock",	Opt_local_lock_flock },
107 	{ "posix",	Opt_local_lock_posix },
108 	{ "none",		Opt_local_lock_none },
109 	{}
110 };
111 
112 enum {
113 	Opt_lookupcache_all,
114 	Opt_lookupcache_none,
115 	Opt_lookupcache_positive,
116 };
117 
118 static const struct constant_table nfs_param_enums_lookupcache[] = {
119 	{ "all",		Opt_lookupcache_all },
120 	{ "none",		Opt_lookupcache_none },
121 	{ "pos",		Opt_lookupcache_positive },
122 	{ "positive",		Opt_lookupcache_positive },
123 	{}
124 };
125 
126 enum {
127 	Opt_write_lazy,
128 	Opt_write_eager,
129 	Opt_write_wait,
130 };
131 
132 static const struct constant_table nfs_param_enums_write[] = {
133 	{ "lazy",		Opt_write_lazy },
134 	{ "eager",		Opt_write_eager },
135 	{ "wait",		Opt_write_wait },
136 	{}
137 };
138 
139 static const struct fs_parameter_spec nfs_fs_parameters[] = {
140 	fsparam_flag_no("ac",		Opt_ac),
141 	fsparam_u32   ("acdirmax",	Opt_acdirmax),
142 	fsparam_u32   ("acdirmin",	Opt_acdirmin),
143 	fsparam_flag_no("acl",		Opt_acl),
144 	fsparam_u32   ("acregmax",	Opt_acregmax),
145 	fsparam_u32   ("acregmin",	Opt_acregmin),
146 	fsparam_u32   ("actimeo",	Opt_actimeo),
147 	fsparam_string("addr",		Opt_addr),
148 	fsparam_flag  ("bg",		Opt_bg),
149 	fsparam_u32   ("bsize",		Opt_bsize),
150 	fsparam_string("clientaddr",	Opt_clientaddr),
151 	fsparam_flag_no("cto",		Opt_cto),
152 	fsparam_flag  ("fg",		Opt_fg),
153 	fsparam_flag_no("fsc",		Opt_fscache_flag),
154 	fsparam_string("fsc",		Opt_fscache),
155 	fsparam_flag  ("hard",		Opt_hard),
156 	__fsparam(NULL, "intr",		Opt_intr,
157 		  fs_param_neg_with_no|fs_param_deprecated, NULL),
158 	fsparam_enum  ("local_lock",	Opt_local_lock, nfs_param_enums_local_lock),
159 	fsparam_flag_no("lock",		Opt_lock),
160 	fsparam_enum  ("lookupcache",	Opt_lookupcache, nfs_param_enums_lookupcache),
161 	fsparam_flag_no("migration",	Opt_migration),
162 	fsparam_u32   ("minorversion",	Opt_minorversion),
163 	fsparam_string("mountaddr",	Opt_mountaddr),
164 	fsparam_string("mounthost",	Opt_mounthost),
165 	fsparam_u32   ("mountport",	Opt_mountport),
166 	fsparam_string("mountproto",	Opt_mountproto),
167 	fsparam_u32   ("mountvers",	Opt_mountvers),
168 	fsparam_u32   ("namlen",	Opt_namelen),
169 	fsparam_u32   ("nconnect",	Opt_nconnect),
170 	fsparam_u32   ("max_connect",	Opt_max_connect),
171 	fsparam_string("nfsvers",	Opt_vers),
172 	fsparam_u32   ("port",		Opt_port),
173 	fsparam_flag_no("posix",	Opt_posix),
174 	fsparam_string("proto",		Opt_proto),
175 	fsparam_flag_no("rdirplus",	Opt_rdirplus),
176 	fsparam_flag  ("rdma",		Opt_rdma),
177 	fsparam_flag_no("resvport",	Opt_resvport),
178 	fsparam_u32   ("retrans",	Opt_retrans),
179 	fsparam_string("retry",		Opt_retry),
180 	fsparam_u32   ("rsize",		Opt_rsize),
181 	fsparam_string("sec",		Opt_sec),
182 	fsparam_flag_no("sharecache",	Opt_sharecache),
183 	fsparam_flag  ("sloppy",	Opt_sloppy),
184 	fsparam_flag  ("soft",		Opt_soft),
185 	fsparam_flag  ("softerr",	Opt_softerr),
186 	fsparam_flag  ("softreval",	Opt_softreval),
187 	fsparam_string("source",	Opt_source),
188 	fsparam_flag  ("tcp",		Opt_tcp),
189 	fsparam_u32   ("timeo",		Opt_timeo),
190 	fsparam_flag_no("trunkdiscovery", Opt_trunkdiscovery),
191 	fsparam_flag  ("udp",		Opt_udp),
192 	fsparam_flag  ("v2",		Opt_v),
193 	fsparam_flag  ("v3",		Opt_v),
194 	fsparam_flag  ("v4",		Opt_v),
195 	fsparam_flag  ("v4.0",		Opt_v),
196 	fsparam_flag  ("v4.1",		Opt_v),
197 	fsparam_flag  ("v4.2",		Opt_v),
198 	fsparam_string("vers",		Opt_vers),
199 	fsparam_enum  ("write",		Opt_write, nfs_param_enums_write),
200 	fsparam_u32   ("wsize",		Opt_wsize),
201 	fsparam_string("xprtsec",	Opt_xprtsec),
202 	{}
203 };
204 
205 enum {
206 	Opt_vers_2,
207 	Opt_vers_3,
208 	Opt_vers_4,
209 	Opt_vers_4_0,
210 	Opt_vers_4_1,
211 	Opt_vers_4_2,
212 };
213 
214 static const struct constant_table nfs_vers_tokens[] = {
215 	{ "2",		Opt_vers_2 },
216 	{ "3",		Opt_vers_3 },
217 	{ "4",		Opt_vers_4 },
218 	{ "4.0",	Opt_vers_4_0 },
219 	{ "4.1",	Opt_vers_4_1 },
220 	{ "4.2",	Opt_vers_4_2 },
221 	{}
222 };
223 
224 enum {
225 	Opt_xprt_rdma,
226 	Opt_xprt_rdma6,
227 	Opt_xprt_tcp,
228 	Opt_xprt_tcp6,
229 	Opt_xprt_udp,
230 	Opt_xprt_udp6,
231 	nr__Opt_xprt
232 };
233 
234 static const struct constant_table nfs_xprt_protocol_tokens[] = {
235 	{ "rdma",	Opt_xprt_rdma },
236 	{ "rdma6",	Opt_xprt_rdma6 },
237 	{ "tcp",	Opt_xprt_tcp },
238 	{ "tcp6",	Opt_xprt_tcp6 },
239 	{ "udp",	Opt_xprt_udp },
240 	{ "udp6",	Opt_xprt_udp6 },
241 	{}
242 };
243 
244 enum {
245 	Opt_sec_krb5,
246 	Opt_sec_krb5i,
247 	Opt_sec_krb5p,
248 	Opt_sec_lkey,
249 	Opt_sec_lkeyi,
250 	Opt_sec_lkeyp,
251 	Opt_sec_none,
252 	Opt_sec_spkm,
253 	Opt_sec_spkmi,
254 	Opt_sec_spkmp,
255 	Opt_sec_sys,
256 	nr__Opt_sec
257 };
258 
259 static const struct constant_table nfs_secflavor_tokens[] = {
260 	{ "krb5",	Opt_sec_krb5 },
261 	{ "krb5i",	Opt_sec_krb5i },
262 	{ "krb5p",	Opt_sec_krb5p },
263 	{ "lkey",	Opt_sec_lkey },
264 	{ "lkeyi",	Opt_sec_lkeyi },
265 	{ "lkeyp",	Opt_sec_lkeyp },
266 	{ "none",	Opt_sec_none },
267 	{ "null",	Opt_sec_none },
268 	{ "spkm3",	Opt_sec_spkm },
269 	{ "spkm3i",	Opt_sec_spkmi },
270 	{ "spkm3p",	Opt_sec_spkmp },
271 	{ "sys",	Opt_sec_sys },
272 	{}
273 };
274 
275 enum {
276 	Opt_xprtsec_none,
277 	Opt_xprtsec_tls,
278 	Opt_xprtsec_mtls,
279 	nr__Opt_xprtsec
280 };
281 
282 static const struct constant_table nfs_xprtsec_policies[] = {
283 	{ "none",	Opt_xprtsec_none },
284 	{ "tls",	Opt_xprtsec_tls },
285 	{ "mtls",	Opt_xprtsec_mtls },
286 	{}
287 };
288 
289 /*
290  * Sanity-check a server address provided by the mount command.
291  *
292  * Address family must be initialized, and address must not be
293  * the ANY address for that family.
294  */
295 static int nfs_verify_server_address(struct sockaddr_storage *addr)
296 {
297 	switch (addr->ss_family) {
298 	case AF_INET: {
299 		struct sockaddr_in *sa = (struct sockaddr_in *)addr;
300 		return sa->sin_addr.s_addr != htonl(INADDR_ANY);
301 	}
302 	case AF_INET6: {
303 		struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
304 		return !ipv6_addr_any(sa);
305 	}
306 	}
307 
308 	return 0;
309 }
310 
311 #ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
312 static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
313 {
314 	return true;
315 }
316 #else
317 static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
318 {
319 	if (ctx->version == 4)
320 		return true;
321 	return false;
322 }
323 #endif
324 
325 /*
326  * Sanity check the NFS transport protocol.
327  */
328 static int nfs_validate_transport_protocol(struct fs_context *fc,
329 					   struct nfs_fs_context *ctx)
330 {
331 	switch (ctx->nfs_server.protocol) {
332 	case XPRT_TRANSPORT_UDP:
333 		if (nfs_server_transport_udp_invalid(ctx))
334 			goto out_invalid_transport_udp;
335 		break;
336 	case XPRT_TRANSPORT_TCP:
337 	case XPRT_TRANSPORT_RDMA:
338 		break;
339 	default:
340 		ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
341 	}
342 
343 	if (ctx->xprtsec.policy != RPC_XPRTSEC_NONE)
344 		switch (ctx->nfs_server.protocol) {
345 		case XPRT_TRANSPORT_TCP:
346 			ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP_TLS;
347 			break;
348 		default:
349 			goto out_invalid_xprtsec_policy;
350 	}
351 
352 	return 0;
353 out_invalid_transport_udp:
354 	return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
355 out_invalid_xprtsec_policy:
356 	return nfs_invalf(fc, "NFS: Transport does not support xprtsec");
357 }
358 
359 /*
360  * For text based NFSv2/v3 mounts, the mount protocol transport default
361  * settings should depend upon the specified NFS transport.
362  */
363 static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
364 {
365 	if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
366 	    ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
367 			return;
368 	switch (ctx->nfs_server.protocol) {
369 	case XPRT_TRANSPORT_UDP:
370 		ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
371 		break;
372 	case XPRT_TRANSPORT_TCP:
373 	case XPRT_TRANSPORT_RDMA:
374 		ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
375 	}
376 }
377 
378 /*
379  * Add 'flavor' to 'auth_info' if not already present.
380  * Returns true if 'flavor' ends up in the list, false otherwise
381  */
382 static int nfs_auth_info_add(struct fs_context *fc,
383 			     struct nfs_auth_info *auth_info,
384 			     rpc_authflavor_t flavor)
385 {
386 	unsigned int i;
387 	unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
388 
389 	/* make sure this flavor isn't already in the list */
390 	for (i = 0; i < auth_info->flavor_len; i++) {
391 		if (flavor == auth_info->flavors[i])
392 			return 0;
393 	}
394 
395 	if (auth_info->flavor_len + 1 >= max_flavor_len)
396 		return nfs_invalf(fc, "NFS: too many sec= flavors");
397 
398 	auth_info->flavors[auth_info->flavor_len++] = flavor;
399 	return 0;
400 }
401 
402 /*
403  * Parse the value of the 'sec=' option.
404  */
405 static int nfs_parse_security_flavors(struct fs_context *fc,
406 				      struct fs_parameter *param)
407 {
408 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
409 	rpc_authflavor_t pseudoflavor;
410 	char *string = param->string, *p;
411 	int ret;
412 
413 	trace_nfs_mount_assign(param->key, string);
414 
415 	while ((p = strsep(&string, ":")) != NULL) {
416 		if (!*p)
417 			continue;
418 		switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
419 		case Opt_sec_none:
420 			pseudoflavor = RPC_AUTH_NULL;
421 			break;
422 		case Opt_sec_sys:
423 			pseudoflavor = RPC_AUTH_UNIX;
424 			break;
425 		case Opt_sec_krb5:
426 			pseudoflavor = RPC_AUTH_GSS_KRB5;
427 			break;
428 		case Opt_sec_krb5i:
429 			pseudoflavor = RPC_AUTH_GSS_KRB5I;
430 			break;
431 		case Opt_sec_krb5p:
432 			pseudoflavor = RPC_AUTH_GSS_KRB5P;
433 			break;
434 		case Opt_sec_lkey:
435 			pseudoflavor = RPC_AUTH_GSS_LKEY;
436 			break;
437 		case Opt_sec_lkeyi:
438 			pseudoflavor = RPC_AUTH_GSS_LKEYI;
439 			break;
440 		case Opt_sec_lkeyp:
441 			pseudoflavor = RPC_AUTH_GSS_LKEYP;
442 			break;
443 		case Opt_sec_spkm:
444 			pseudoflavor = RPC_AUTH_GSS_SPKM;
445 			break;
446 		case Opt_sec_spkmi:
447 			pseudoflavor = RPC_AUTH_GSS_SPKMI;
448 			break;
449 		case Opt_sec_spkmp:
450 			pseudoflavor = RPC_AUTH_GSS_SPKMP;
451 			break;
452 		default:
453 			return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
454 		}
455 
456 		ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
457 		if (ret < 0)
458 			return ret;
459 	}
460 
461 	return 0;
462 }
463 
464 static int nfs_parse_xprtsec_policy(struct fs_context *fc,
465 				    struct fs_parameter *param)
466 {
467 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
468 
469 	trace_nfs_mount_assign(param->key, param->string);
470 
471 	switch (lookup_constant(nfs_xprtsec_policies, param->string, -1)) {
472 	case Opt_xprtsec_none:
473 		ctx->xprtsec.policy = RPC_XPRTSEC_NONE;
474 		break;
475 	case Opt_xprtsec_tls:
476 		ctx->xprtsec.policy = RPC_XPRTSEC_TLS_ANON;
477 		break;
478 	case Opt_xprtsec_mtls:
479 		ctx->xprtsec.policy = RPC_XPRTSEC_TLS_X509;
480 		break;
481 	default:
482 		return nfs_invalf(fc, "NFS: Unrecognized transport security policy");
483 	}
484 	return 0;
485 }
486 
487 static int nfs_parse_version_string(struct fs_context *fc,
488 				    const char *string)
489 {
490 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
491 
492 	ctx->flags &= ~NFS_MOUNT_VER3;
493 	switch (lookup_constant(nfs_vers_tokens, string, -1)) {
494 	case Opt_vers_2:
495 		ctx->version = 2;
496 		break;
497 	case Opt_vers_3:
498 		ctx->flags |= NFS_MOUNT_VER3;
499 		ctx->version = 3;
500 		break;
501 	case Opt_vers_4:
502 		/* Backward compatibility option. In future,
503 		 * the mount program should always supply
504 		 * a NFSv4 minor version number.
505 		 */
506 		ctx->version = 4;
507 		break;
508 	case Opt_vers_4_0:
509 		ctx->version = 4;
510 		ctx->minorversion = 0;
511 		break;
512 	case Opt_vers_4_1:
513 		ctx->version = 4;
514 		ctx->minorversion = 1;
515 		break;
516 	case Opt_vers_4_2:
517 		ctx->version = 4;
518 		ctx->minorversion = 2;
519 		break;
520 	default:
521 		return nfs_invalf(fc, "NFS: Unsupported NFS version");
522 	}
523 	return 0;
524 }
525 
526 /*
527  * Parse a single mount parameter.
528  */
529 static int nfs_fs_context_parse_param(struct fs_context *fc,
530 				      struct fs_parameter *param)
531 {
532 	struct fs_parse_result result;
533 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
534 	unsigned short protofamily, mountfamily;
535 	unsigned int len;
536 	int ret, opt;
537 
538 	trace_nfs_mount_option(param);
539 
540 	opt = fs_parse(fc, nfs_fs_parameters, param, &result);
541 	if (opt < 0)
542 		return (opt == -ENOPARAM && ctx->sloppy) ? 1 : opt;
543 
544 	if (fc->security)
545 		ctx->has_sec_mnt_opts = 1;
546 
547 	switch (opt) {
548 	case Opt_source:
549 		if (fc->source)
550 			return nfs_invalf(fc, "NFS: Multiple sources not supported");
551 		fc->source = param->string;
552 		param->string = NULL;
553 		break;
554 
555 		/*
556 		 * boolean options:  foo/nofoo
557 		 */
558 	case Opt_soft:
559 		ctx->flags |= NFS_MOUNT_SOFT;
560 		ctx->flags &= ~NFS_MOUNT_SOFTERR;
561 		break;
562 	case Opt_softerr:
563 		ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
564 		ctx->flags &= ~NFS_MOUNT_SOFT;
565 		break;
566 	case Opt_hard:
567 		ctx->flags &= ~(NFS_MOUNT_SOFT |
568 				NFS_MOUNT_SOFTERR |
569 				NFS_MOUNT_SOFTREVAL);
570 		break;
571 	case Opt_softreval:
572 		if (result.negated)
573 			ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
574 		else
575 			ctx->flags |= NFS_MOUNT_SOFTREVAL;
576 		break;
577 	case Opt_posix:
578 		if (result.negated)
579 			ctx->flags &= ~NFS_MOUNT_POSIX;
580 		else
581 			ctx->flags |= NFS_MOUNT_POSIX;
582 		break;
583 	case Opt_cto:
584 		if (result.negated)
585 			ctx->flags |= NFS_MOUNT_NOCTO;
586 		else
587 			ctx->flags &= ~NFS_MOUNT_NOCTO;
588 		break;
589 	case Opt_trunkdiscovery:
590 		if (result.negated)
591 			ctx->flags &= ~NFS_MOUNT_TRUNK_DISCOVERY;
592 		else
593 			ctx->flags |= NFS_MOUNT_TRUNK_DISCOVERY;
594 		break;
595 	case Opt_ac:
596 		if (result.negated)
597 			ctx->flags |= NFS_MOUNT_NOAC;
598 		else
599 			ctx->flags &= ~NFS_MOUNT_NOAC;
600 		break;
601 	case Opt_lock:
602 		if (result.negated) {
603 			ctx->flags |= NFS_MOUNT_NONLM;
604 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
605 		} else {
606 			ctx->flags &= ~NFS_MOUNT_NONLM;
607 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
608 		}
609 		break;
610 	case Opt_udp:
611 		ctx->flags &= ~NFS_MOUNT_TCP;
612 		ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
613 		break;
614 	case Opt_tcp:
615 	case Opt_rdma:
616 		ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
617 		ret = xprt_find_transport_ident(param->key);
618 		if (ret < 0)
619 			goto out_bad_transport;
620 		ctx->nfs_server.protocol = ret;
621 		break;
622 	case Opt_acl:
623 		if (result.negated)
624 			ctx->flags |= NFS_MOUNT_NOACL;
625 		else
626 			ctx->flags &= ~NFS_MOUNT_NOACL;
627 		break;
628 	case Opt_rdirplus:
629 		if (result.negated)
630 			ctx->flags |= NFS_MOUNT_NORDIRPLUS;
631 		else
632 			ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
633 		break;
634 	case Opt_sharecache:
635 		if (result.negated)
636 			ctx->flags |= NFS_MOUNT_UNSHARED;
637 		else
638 			ctx->flags &= ~NFS_MOUNT_UNSHARED;
639 		break;
640 	case Opt_resvport:
641 		if (result.negated)
642 			ctx->flags |= NFS_MOUNT_NORESVPORT;
643 		else
644 			ctx->flags &= ~NFS_MOUNT_NORESVPORT;
645 		break;
646 	case Opt_fscache_flag:
647 		if (result.negated)
648 			ctx->options &= ~NFS_OPTION_FSCACHE;
649 		else
650 			ctx->options |= NFS_OPTION_FSCACHE;
651 		kfree(ctx->fscache_uniq);
652 		ctx->fscache_uniq = NULL;
653 		break;
654 	case Opt_fscache:
655 		ctx->options |= NFS_OPTION_FSCACHE;
656 		kfree(ctx->fscache_uniq);
657 		ctx->fscache_uniq = param->string;
658 		param->string = NULL;
659 		break;
660 	case Opt_migration:
661 		if (result.negated)
662 			ctx->options &= ~NFS_OPTION_MIGRATION;
663 		else
664 			ctx->options |= NFS_OPTION_MIGRATION;
665 		break;
666 
667 		/*
668 		 * options that take numeric values
669 		 */
670 	case Opt_port:
671 		if (result.uint_32 > USHRT_MAX)
672 			goto out_of_bounds;
673 		ctx->nfs_server.port = result.uint_32;
674 		break;
675 	case Opt_rsize:
676 		ctx->rsize = result.uint_32;
677 		break;
678 	case Opt_wsize:
679 		ctx->wsize = result.uint_32;
680 		break;
681 	case Opt_bsize:
682 		ctx->bsize = result.uint_32;
683 		break;
684 	case Opt_timeo:
685 		if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
686 			goto out_of_bounds;
687 		ctx->timeo = result.uint_32;
688 		break;
689 	case Opt_retrans:
690 		if (result.uint_32 > INT_MAX)
691 			goto out_of_bounds;
692 		ctx->retrans = result.uint_32;
693 		break;
694 	case Opt_acregmin:
695 		ctx->acregmin = result.uint_32;
696 		break;
697 	case Opt_acregmax:
698 		ctx->acregmax = result.uint_32;
699 		break;
700 	case Opt_acdirmin:
701 		ctx->acdirmin = result.uint_32;
702 		break;
703 	case Opt_acdirmax:
704 		ctx->acdirmax = result.uint_32;
705 		break;
706 	case Opt_actimeo:
707 		ctx->acregmin = result.uint_32;
708 		ctx->acregmax = result.uint_32;
709 		ctx->acdirmin = result.uint_32;
710 		ctx->acdirmax = result.uint_32;
711 		break;
712 	case Opt_namelen:
713 		ctx->namlen = result.uint_32;
714 		break;
715 	case Opt_mountport:
716 		if (result.uint_32 > USHRT_MAX)
717 			goto out_of_bounds;
718 		ctx->mount_server.port = result.uint_32;
719 		break;
720 	case Opt_mountvers:
721 		if (result.uint_32 < NFS_MNT_VERSION ||
722 		    result.uint_32 > NFS_MNT3_VERSION)
723 			goto out_of_bounds;
724 		ctx->mount_server.version = result.uint_32;
725 		break;
726 	case Opt_minorversion:
727 		if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
728 			goto out_of_bounds;
729 		ctx->minorversion = result.uint_32;
730 		break;
731 
732 		/*
733 		 * options that take text values
734 		 */
735 	case Opt_v:
736 		ret = nfs_parse_version_string(fc, param->key + 1);
737 		if (ret < 0)
738 			return ret;
739 		break;
740 	case Opt_vers:
741 		if (!param->string)
742 			goto out_invalid_value;
743 		trace_nfs_mount_assign(param->key, param->string);
744 		ret = nfs_parse_version_string(fc, param->string);
745 		if (ret < 0)
746 			return ret;
747 		break;
748 	case Opt_sec:
749 		ret = nfs_parse_security_flavors(fc, param);
750 		if (ret < 0)
751 			return ret;
752 		break;
753 	case Opt_xprtsec:
754 		ret = nfs_parse_xprtsec_policy(fc, param);
755 		if (ret < 0)
756 			return ret;
757 		break;
758 
759 	case Opt_proto:
760 		if (!param->string)
761 			goto out_invalid_value;
762 		trace_nfs_mount_assign(param->key, param->string);
763 		protofamily = AF_INET;
764 		switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
765 		case Opt_xprt_udp6:
766 			protofamily = AF_INET6;
767 			fallthrough;
768 		case Opt_xprt_udp:
769 			ctx->flags &= ~NFS_MOUNT_TCP;
770 			ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
771 			break;
772 		case Opt_xprt_tcp6:
773 			protofamily = AF_INET6;
774 			fallthrough;
775 		case Opt_xprt_tcp:
776 			ctx->flags |= NFS_MOUNT_TCP;
777 			ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
778 			break;
779 		case Opt_xprt_rdma6:
780 			protofamily = AF_INET6;
781 			fallthrough;
782 		case Opt_xprt_rdma:
783 			/* vector side protocols to TCP */
784 			ctx->flags |= NFS_MOUNT_TCP;
785 			ret = xprt_find_transport_ident(param->string);
786 			if (ret < 0)
787 				goto out_bad_transport;
788 			ctx->nfs_server.protocol = ret;
789 			break;
790 		default:
791 			goto out_bad_transport;
792 		}
793 
794 		ctx->protofamily = protofamily;
795 		break;
796 
797 	case Opt_mountproto:
798 		if (!param->string)
799 			goto out_invalid_value;
800 		trace_nfs_mount_assign(param->key, param->string);
801 		mountfamily = AF_INET;
802 		switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
803 		case Opt_xprt_udp6:
804 			mountfamily = AF_INET6;
805 			fallthrough;
806 		case Opt_xprt_udp:
807 			ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
808 			break;
809 		case Opt_xprt_tcp6:
810 			mountfamily = AF_INET6;
811 			fallthrough;
812 		case Opt_xprt_tcp:
813 			ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
814 			break;
815 		case Opt_xprt_rdma: /* not used for side protocols */
816 		default:
817 			goto out_bad_transport;
818 		}
819 		ctx->mountfamily = mountfamily;
820 		break;
821 
822 	case Opt_addr:
823 		trace_nfs_mount_assign(param->key, param->string);
824 		len = rpc_pton(fc->net_ns, param->string, param->size,
825 			       &ctx->nfs_server.address,
826 			       sizeof(ctx->nfs_server._address));
827 		if (len == 0)
828 			goto out_invalid_address;
829 		ctx->nfs_server.addrlen = len;
830 		break;
831 	case Opt_clientaddr:
832 		trace_nfs_mount_assign(param->key, param->string);
833 		kfree(ctx->client_address);
834 		ctx->client_address = param->string;
835 		param->string = NULL;
836 		break;
837 	case Opt_mounthost:
838 		trace_nfs_mount_assign(param->key, param->string);
839 		kfree(ctx->mount_server.hostname);
840 		ctx->mount_server.hostname = param->string;
841 		param->string = NULL;
842 		break;
843 	case Opt_mountaddr:
844 		trace_nfs_mount_assign(param->key, param->string);
845 		len = rpc_pton(fc->net_ns, param->string, param->size,
846 			       &ctx->mount_server.address,
847 			       sizeof(ctx->mount_server._address));
848 		if (len == 0)
849 			goto out_invalid_address;
850 		ctx->mount_server.addrlen = len;
851 		break;
852 	case Opt_nconnect:
853 		trace_nfs_mount_assign(param->key, param->string);
854 		if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
855 			goto out_of_bounds;
856 		ctx->nfs_server.nconnect = result.uint_32;
857 		break;
858 	case Opt_max_connect:
859 		trace_nfs_mount_assign(param->key, param->string);
860 		if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_TRANSPORTS)
861 			goto out_of_bounds;
862 		ctx->nfs_server.max_connect = result.uint_32;
863 		break;
864 	case Opt_lookupcache:
865 		trace_nfs_mount_assign(param->key, param->string);
866 		switch (result.uint_32) {
867 		case Opt_lookupcache_all:
868 			ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
869 			break;
870 		case Opt_lookupcache_positive:
871 			ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
872 			ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
873 			break;
874 		case Opt_lookupcache_none:
875 			ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
876 			break;
877 		default:
878 			goto out_invalid_value;
879 		}
880 		break;
881 	case Opt_local_lock:
882 		trace_nfs_mount_assign(param->key, param->string);
883 		switch (result.uint_32) {
884 		case Opt_local_lock_all:
885 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
886 				       NFS_MOUNT_LOCAL_FCNTL);
887 			break;
888 		case Opt_local_lock_flock:
889 			ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
890 			break;
891 		case Opt_local_lock_posix:
892 			ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
893 			break;
894 		case Opt_local_lock_none:
895 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
896 					NFS_MOUNT_LOCAL_FCNTL);
897 			break;
898 		default:
899 			goto out_invalid_value;
900 		}
901 		break;
902 	case Opt_write:
903 		trace_nfs_mount_assign(param->key, param->string);
904 		switch (result.uint_32) {
905 		case Opt_write_lazy:
906 			ctx->flags &=
907 				~(NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT);
908 			break;
909 		case Opt_write_eager:
910 			ctx->flags |= NFS_MOUNT_WRITE_EAGER;
911 			ctx->flags &= ~NFS_MOUNT_WRITE_WAIT;
912 			break;
913 		case Opt_write_wait:
914 			ctx->flags |=
915 				NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT;
916 			break;
917 		default:
918 			goto out_invalid_value;
919 		}
920 		break;
921 
922 		/*
923 		 * Special options
924 		 */
925 	case Opt_sloppy:
926 		ctx->sloppy = true;
927 		break;
928 	}
929 
930 	return 0;
931 
932 out_invalid_value:
933 	return nfs_invalf(fc, "NFS: Bad mount option value specified");
934 out_invalid_address:
935 	return nfs_invalf(fc, "NFS: Bad IP address specified");
936 out_of_bounds:
937 	return nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
938 out_bad_transport:
939 	return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
940 }
941 
942 /*
943  * Split fc->source into "hostname:export_path".
944  *
945  * The leftmost colon demarks the split between the server's hostname
946  * and the export path.  If the hostname starts with a left square
947  * bracket, then it may contain colons.
948  *
949  * Note: caller frees hostname and export path, even on error.
950  */
951 static int nfs_parse_source(struct fs_context *fc,
952 			    size_t maxnamlen, size_t maxpathlen)
953 {
954 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
955 	const char *dev_name = fc->source;
956 	size_t len;
957 	const char *end;
958 
959 	if (unlikely(!dev_name || !*dev_name))
960 		return -EINVAL;
961 
962 	/* Is the host name protected with square brakcets? */
963 	if (*dev_name == '[') {
964 		end = strchr(++dev_name, ']');
965 		if (end == NULL || end[1] != ':')
966 			goto out_bad_devname;
967 
968 		len = end - dev_name;
969 		end++;
970 	} else {
971 		const char *comma;
972 
973 		end = strchr(dev_name, ':');
974 		if (end == NULL)
975 			goto out_bad_devname;
976 		len = end - dev_name;
977 
978 		/* kill possible hostname list: not supported */
979 		comma = memchr(dev_name, ',', len);
980 		if (comma)
981 			len = comma - dev_name;
982 	}
983 
984 	if (len > maxnamlen)
985 		goto out_hostname;
986 
987 	kfree(ctx->nfs_server.hostname);
988 
989 	/* N.B. caller will free nfs_server.hostname in all cases */
990 	ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
991 	if (!ctx->nfs_server.hostname)
992 		goto out_nomem;
993 	len = strlen(++end);
994 	if (len > maxpathlen)
995 		goto out_path;
996 	ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
997 	if (!ctx->nfs_server.export_path)
998 		goto out_nomem;
999 
1000 	trace_nfs_mount_path(ctx->nfs_server.export_path);
1001 	return 0;
1002 
1003 out_bad_devname:
1004 	return nfs_invalf(fc, "NFS: device name not in host:path format");
1005 out_nomem:
1006 	nfs_errorf(fc, "NFS: not enough memory to parse device name");
1007 	return -ENOMEM;
1008 out_hostname:
1009 	nfs_errorf(fc, "NFS: server hostname too long");
1010 	return -ENAMETOOLONG;
1011 out_path:
1012 	nfs_errorf(fc, "NFS: export pathname too long");
1013 	return -ENAMETOOLONG;
1014 }
1015 
1016 static inline bool is_remount_fc(struct fs_context *fc)
1017 {
1018 	return fc->root != NULL;
1019 }
1020 
1021 /*
1022  * Parse monolithic NFS2/NFS3 mount data
1023  * - fills in the mount root filehandle
1024  *
1025  * For option strings, user space handles the following behaviors:
1026  *
1027  * + DNS: mapping server host name to IP address ("addr=" option)
1028  *
1029  * + failure mode: how to behave if a mount request can't be handled
1030  *   immediately ("fg/bg" option)
1031  *
1032  * + retry: how often to retry a mount request ("retry=" option)
1033  *
1034  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1035  *   mountproto=tcp after mountproto=udp, and so on
1036  */
1037 static int nfs23_parse_monolithic(struct fs_context *fc,
1038 				  struct nfs_mount_data *data)
1039 {
1040 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1041 	struct nfs_fh *mntfh = ctx->mntfh;
1042 	struct sockaddr_storage *sap = &ctx->nfs_server._address;
1043 	int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
1044 	int ret;
1045 
1046 	if (data == NULL)
1047 		goto out_no_data;
1048 
1049 	ctx->version = NFS_DEFAULT_VERSION;
1050 	switch (data->version) {
1051 	case 1:
1052 		data->namlen = 0;
1053 		fallthrough;
1054 	case 2:
1055 		data->bsize = 0;
1056 		fallthrough;
1057 	case 3:
1058 		if (data->flags & NFS_MOUNT_VER3)
1059 			goto out_no_v3;
1060 		data->root.size = NFS2_FHSIZE;
1061 		memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1062 		/* Turn off security negotiation */
1063 		extra_flags |= NFS_MOUNT_SECFLAVOUR;
1064 		fallthrough;
1065 	case 4:
1066 		if (data->flags & NFS_MOUNT_SECFLAVOUR)
1067 			goto out_no_sec;
1068 		fallthrough;
1069 	case 5:
1070 		memset(data->context, 0, sizeof(data->context));
1071 		fallthrough;
1072 	case 6:
1073 		if (data->flags & NFS_MOUNT_VER3) {
1074 			if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1075 				goto out_invalid_fh;
1076 			mntfh->size = data->root.size;
1077 			ctx->version = 3;
1078 		} else {
1079 			mntfh->size = NFS2_FHSIZE;
1080 			ctx->version = 2;
1081 		}
1082 
1083 
1084 		memcpy(mntfh->data, data->root.data, mntfh->size);
1085 		if (mntfh->size < sizeof(mntfh->data))
1086 			memset(mntfh->data + mntfh->size, 0,
1087 			       sizeof(mntfh->data) - mntfh->size);
1088 
1089 		/*
1090 		 * for proto == XPRT_TRANSPORT_UDP, which is what uses
1091 		 * to_exponential, implying shift: limit the shift value
1092 		 * to BITS_PER_LONG (majortimeo is unsigned long)
1093 		 */
1094 		if (!(data->flags & NFS_MOUNT_TCP)) /* this will be UDP */
1095 			if (data->retrans >= 64) /* shift value is too large */
1096 				goto out_invalid_data;
1097 
1098 		/*
1099 		 * Translate to nfs_fs_context, which nfs_fill_super
1100 		 * can deal with.
1101 		 */
1102 		ctx->flags	= data->flags & NFS_MOUNT_FLAGMASK;
1103 		ctx->flags	|= extra_flags;
1104 		ctx->rsize	= data->rsize;
1105 		ctx->wsize	= data->wsize;
1106 		ctx->timeo	= data->timeo;
1107 		ctx->retrans	= data->retrans;
1108 		ctx->acregmin	= data->acregmin;
1109 		ctx->acregmax	= data->acregmax;
1110 		ctx->acdirmin	= data->acdirmin;
1111 		ctx->acdirmax	= data->acdirmax;
1112 		ctx->need_mount	= false;
1113 
1114 		if (!is_remount_fc(fc)) {
1115 			memcpy(sap, &data->addr, sizeof(data->addr));
1116 			ctx->nfs_server.addrlen = sizeof(data->addr);
1117 			ctx->nfs_server.port = ntohs(data->addr.sin_port);
1118 		}
1119 
1120 		if (sap->ss_family != AF_INET ||
1121 		    !nfs_verify_server_address(sap))
1122 			goto out_no_address;
1123 
1124 		if (!(data->flags & NFS_MOUNT_TCP))
1125 			ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1126 		/* N.B. caller will free nfs_server.hostname in all cases */
1127 		ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1128 		if (!ctx->nfs_server.hostname)
1129 			goto out_nomem;
1130 
1131 		ctx->namlen		= data->namlen;
1132 		ctx->bsize		= data->bsize;
1133 
1134 		if (data->flags & NFS_MOUNT_SECFLAVOUR)
1135 			ctx->selected_flavor = data->pseudoflavor;
1136 		else
1137 			ctx->selected_flavor = RPC_AUTH_UNIX;
1138 
1139 		if (!(data->flags & NFS_MOUNT_NONLM))
1140 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1141 					 NFS_MOUNT_LOCAL_FCNTL);
1142 		else
1143 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1144 					NFS_MOUNT_LOCAL_FCNTL);
1145 
1146 		/*
1147 		 * The legacy version 6 binary mount data from userspace has a
1148 		 * field used only to transport selinux information into the
1149 		 * kernel.  To continue to support that functionality we
1150 		 * have a touch of selinux knowledge here in the NFS code. The
1151 		 * userspace code converted context=blah to just blah so we are
1152 		 * converting back to the full string selinux understands.
1153 		 */
1154 		if (data->context[0]){
1155 #ifdef CONFIG_SECURITY_SELINUX
1156 			int ret;
1157 
1158 			data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1159 			ret = vfs_parse_fs_string(fc, "context",
1160 						  data->context, strlen(data->context));
1161 			if (ret < 0)
1162 				return ret;
1163 #else
1164 			return -EINVAL;
1165 #endif
1166 		}
1167 
1168 		break;
1169 	default:
1170 		goto generic;
1171 	}
1172 
1173 	ret = nfs_validate_transport_protocol(fc, ctx);
1174 	if (ret)
1175 		return ret;
1176 
1177 	ctx->skip_reconfig_option_check = true;
1178 	return 0;
1179 
1180 generic:
1181 	return generic_parse_monolithic(fc, data);
1182 
1183 out_no_data:
1184 	if (is_remount_fc(fc)) {
1185 		ctx->skip_reconfig_option_check = true;
1186 		return 0;
1187 	}
1188 	return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
1189 
1190 out_no_v3:
1191 	return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
1192 
1193 out_no_sec:
1194 	return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
1195 
1196 out_nomem:
1197 	return -ENOMEM;
1198 
1199 out_no_address:
1200 	return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1201 
1202 out_invalid_fh:
1203 	return nfs_invalf(fc, "NFS: invalid root filehandle");
1204 
1205 out_invalid_data:
1206 	return nfs_invalf(fc, "NFS: invalid binary mount data");
1207 }
1208 
1209 #if IS_ENABLED(CONFIG_NFS_V4)
1210 struct compat_nfs_string {
1211 	compat_uint_t len;
1212 	compat_uptr_t data;
1213 };
1214 
1215 static inline void compat_nfs_string(struct nfs_string *dst,
1216 				     struct compat_nfs_string *src)
1217 {
1218 	dst->data = compat_ptr(src->data);
1219 	dst->len = src->len;
1220 }
1221 
1222 struct compat_nfs4_mount_data_v1 {
1223 	compat_int_t version;
1224 	compat_int_t flags;
1225 	compat_int_t rsize;
1226 	compat_int_t wsize;
1227 	compat_int_t timeo;
1228 	compat_int_t retrans;
1229 	compat_int_t acregmin;
1230 	compat_int_t acregmax;
1231 	compat_int_t acdirmin;
1232 	compat_int_t acdirmax;
1233 	struct compat_nfs_string client_addr;
1234 	struct compat_nfs_string mnt_path;
1235 	struct compat_nfs_string hostname;
1236 	compat_uint_t host_addrlen;
1237 	compat_uptr_t host_addr;
1238 	compat_int_t proto;
1239 	compat_int_t auth_flavourlen;
1240 	compat_uptr_t auth_flavours;
1241 };
1242 
1243 static void nfs4_compat_mount_data_conv(struct nfs4_mount_data *data)
1244 {
1245 	struct compat_nfs4_mount_data_v1 *compat =
1246 			(struct compat_nfs4_mount_data_v1 *)data;
1247 
1248 	/* copy the fields backwards */
1249 	data->auth_flavours = compat_ptr(compat->auth_flavours);
1250 	data->auth_flavourlen = compat->auth_flavourlen;
1251 	data->proto = compat->proto;
1252 	data->host_addr = compat_ptr(compat->host_addr);
1253 	data->host_addrlen = compat->host_addrlen;
1254 	compat_nfs_string(&data->hostname, &compat->hostname);
1255 	compat_nfs_string(&data->mnt_path, &compat->mnt_path);
1256 	compat_nfs_string(&data->client_addr, &compat->client_addr);
1257 	data->acdirmax = compat->acdirmax;
1258 	data->acdirmin = compat->acdirmin;
1259 	data->acregmax = compat->acregmax;
1260 	data->acregmin = compat->acregmin;
1261 	data->retrans = compat->retrans;
1262 	data->timeo = compat->timeo;
1263 	data->wsize = compat->wsize;
1264 	data->rsize = compat->rsize;
1265 	data->flags = compat->flags;
1266 	data->version = compat->version;
1267 }
1268 
1269 /*
1270  * Validate NFSv4 mount options
1271  */
1272 static int nfs4_parse_monolithic(struct fs_context *fc,
1273 				 struct nfs4_mount_data *data)
1274 {
1275 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1276 	struct sockaddr_storage *sap = &ctx->nfs_server._address;
1277 	int ret;
1278 	char *c;
1279 
1280 	if (!data) {
1281 		if (is_remount_fc(fc))
1282 			goto done;
1283 		return nfs_invalf(fc,
1284 			"NFS4: mount program didn't pass any mount data");
1285 	}
1286 
1287 	ctx->version = 4;
1288 
1289 	if (data->version != 1)
1290 		return generic_parse_monolithic(fc, data);
1291 
1292 	if (in_compat_syscall())
1293 		nfs4_compat_mount_data_conv(data);
1294 
1295 	if (data->host_addrlen > sizeof(ctx->nfs_server.address))
1296 		goto out_no_address;
1297 	if (data->host_addrlen == 0)
1298 		goto out_no_address;
1299 	ctx->nfs_server.addrlen = data->host_addrlen;
1300 	if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1301 		return -EFAULT;
1302 	if (!nfs_verify_server_address(sap))
1303 		goto out_no_address;
1304 	ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1305 
1306 	if (data->auth_flavourlen) {
1307 		rpc_authflavor_t pseudoflavor;
1308 
1309 		if (data->auth_flavourlen > 1)
1310 			goto out_inval_auth;
1311 		if (copy_from_user(&pseudoflavor, data->auth_flavours,
1312 				   sizeof(pseudoflavor)))
1313 			return -EFAULT;
1314 		ctx->selected_flavor = pseudoflavor;
1315 	} else {
1316 		ctx->selected_flavor = RPC_AUTH_UNIX;
1317 	}
1318 
1319 	c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1320 	if (IS_ERR(c))
1321 		return PTR_ERR(c);
1322 	ctx->nfs_server.hostname = c;
1323 
1324 	c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1325 	if (IS_ERR(c))
1326 		return PTR_ERR(c);
1327 	ctx->nfs_server.export_path = c;
1328 	trace_nfs_mount_path(c);
1329 
1330 	c = strndup_user(data->client_addr.data, 16);
1331 	if (IS_ERR(c))
1332 		return PTR_ERR(c);
1333 	ctx->client_address = c;
1334 
1335 	/*
1336 	 * Translate to nfs_fs_context, which nfs_fill_super
1337 	 * can deal with.
1338 	 */
1339 
1340 	ctx->flags	= data->flags & NFS4_MOUNT_FLAGMASK;
1341 	ctx->rsize	= data->rsize;
1342 	ctx->wsize	= data->wsize;
1343 	ctx->timeo	= data->timeo;
1344 	ctx->retrans	= data->retrans;
1345 	ctx->acregmin	= data->acregmin;
1346 	ctx->acregmax	= data->acregmax;
1347 	ctx->acdirmin	= data->acdirmin;
1348 	ctx->acdirmax	= data->acdirmax;
1349 	ctx->nfs_server.protocol = data->proto;
1350 	ret = nfs_validate_transport_protocol(fc, ctx);
1351 	if (ret)
1352 		return ret;
1353 done:
1354 	ctx->skip_reconfig_option_check = true;
1355 	return 0;
1356 
1357 out_inval_auth:
1358 	return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1359 		      data->auth_flavourlen);
1360 
1361 out_no_address:
1362 	return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
1363 }
1364 #endif
1365 
1366 /*
1367  * Parse a monolithic block of data from sys_mount().
1368  */
1369 static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1370 					   void *data)
1371 {
1372 	if (fc->fs_type == &nfs_fs_type)
1373 		return nfs23_parse_monolithic(fc, data);
1374 
1375 #if IS_ENABLED(CONFIG_NFS_V4)
1376 	if (fc->fs_type == &nfs4_fs_type)
1377 		return nfs4_parse_monolithic(fc, data);
1378 #endif
1379 
1380 	return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
1381 }
1382 
1383 /*
1384  * Validate the preparsed information in the config.
1385  */
1386 static int nfs_fs_context_validate(struct fs_context *fc)
1387 {
1388 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1389 	struct nfs_subversion *nfs_mod;
1390 	struct sockaddr_storage *sap = &ctx->nfs_server._address;
1391 	int max_namelen = PAGE_SIZE;
1392 	int max_pathlen = NFS_MAXPATHLEN;
1393 	int port = 0;
1394 	int ret;
1395 
1396 	if (!fc->source)
1397 		goto out_no_device_name;
1398 
1399 	/* Check for sanity first. */
1400 	if (ctx->minorversion && ctx->version != 4)
1401 		goto out_minorversion_mismatch;
1402 
1403 	if (ctx->options & NFS_OPTION_MIGRATION &&
1404 	    (ctx->version != 4 || ctx->minorversion != 0))
1405 		goto out_migration_misuse;
1406 
1407 	/* Verify that any proto=/mountproto= options match the address
1408 	 * families in the addr=/mountaddr= options.
1409 	 */
1410 	if (ctx->protofamily != AF_UNSPEC &&
1411 	    ctx->protofamily != ctx->nfs_server.address.sa_family)
1412 		goto out_proto_mismatch;
1413 
1414 	if (ctx->mountfamily != AF_UNSPEC) {
1415 		if (ctx->mount_server.addrlen) {
1416 			if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1417 				goto out_mountproto_mismatch;
1418 		} else {
1419 			if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1420 				goto out_mountproto_mismatch;
1421 		}
1422 	}
1423 
1424 	if (!nfs_verify_server_address(sap))
1425 		goto out_no_address;
1426 
1427 	ret = nfs_validate_transport_protocol(fc, ctx);
1428 	if (ret)
1429 		return ret;
1430 
1431 	if (ctx->version == 4) {
1432 		if (IS_ENABLED(CONFIG_NFS_V4)) {
1433 			if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1434 				port = NFS_RDMA_PORT;
1435 			else
1436 				port = NFS_PORT;
1437 			max_namelen = NFS4_MAXNAMLEN;
1438 			max_pathlen = NFS4_MAXPATHLEN;
1439 			ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1440 					NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1441 					NFS_MOUNT_LOCAL_FCNTL);
1442 		} else {
1443 			goto out_v4_not_compiled;
1444 		}
1445 	} else {
1446 		nfs_set_mount_transport_protocol(ctx);
1447 		if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1448 			port = NFS_RDMA_PORT;
1449 	}
1450 
1451 	nfs_set_port(sap, &ctx->nfs_server.port, port);
1452 
1453 	ret = nfs_parse_source(fc, max_namelen, max_pathlen);
1454 	if (ret < 0)
1455 		return ret;
1456 
1457 	/* Load the NFS protocol module if we haven't done so yet */
1458 	if (!ctx->nfs_mod) {
1459 		nfs_mod = get_nfs_version(ctx->version);
1460 		if (IS_ERR(nfs_mod)) {
1461 			ret = PTR_ERR(nfs_mod);
1462 			goto out_version_unavailable;
1463 		}
1464 		ctx->nfs_mod = nfs_mod;
1465 	}
1466 
1467 	/* Ensure the filesystem context has the correct fs_type */
1468 	if (fc->fs_type != ctx->nfs_mod->nfs_fs) {
1469 		module_put(fc->fs_type->owner);
1470 		__module_get(ctx->nfs_mod->nfs_fs->owner);
1471 		fc->fs_type = ctx->nfs_mod->nfs_fs;
1472 	}
1473 	return 0;
1474 
1475 out_no_device_name:
1476 	return nfs_invalf(fc, "NFS: Device name not specified");
1477 out_v4_not_compiled:
1478 	nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
1479 	return -EPROTONOSUPPORT;
1480 out_no_address:
1481 	return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1482 out_mountproto_mismatch:
1483 	return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
1484 out_proto_mismatch:
1485 	return nfs_invalf(fc, "NFS: Server address does not match proto= option");
1486 out_minorversion_mismatch:
1487 	return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
1488 			  ctx->version, ctx->minorversion);
1489 out_migration_misuse:
1490 	return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
1491 out_version_unavailable:
1492 	nfs_errorf(fc, "NFS: Version unavailable");
1493 	return ret;
1494 }
1495 
1496 /*
1497  * Create an NFS superblock by the appropriate method.
1498  */
1499 static int nfs_get_tree(struct fs_context *fc)
1500 {
1501 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1502 	int err = nfs_fs_context_validate(fc);
1503 
1504 	if (err)
1505 		return err;
1506 	if (!ctx->internal)
1507 		return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
1508 	else
1509 		return nfs_get_tree_common(fc);
1510 }
1511 
1512 /*
1513  * Handle duplication of a configuration.  The caller copied *src into *sc, but
1514  * it can't deal with resource pointers in the filesystem context, so we have
1515  * to do that.  We need to clear pointers, copy data or get extra refs as
1516  * appropriate.
1517  */
1518 static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1519 {
1520 	struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1521 
1522 	ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1523 	if (!ctx)
1524 		return -ENOMEM;
1525 
1526 	ctx->mntfh = nfs_alloc_fhandle();
1527 	if (!ctx->mntfh) {
1528 		kfree(ctx);
1529 		return -ENOMEM;
1530 	}
1531 	nfs_copy_fh(ctx->mntfh, src->mntfh);
1532 
1533 	__module_get(ctx->nfs_mod->owner);
1534 	ctx->client_address		= NULL;
1535 	ctx->mount_server.hostname	= NULL;
1536 	ctx->nfs_server.export_path	= NULL;
1537 	ctx->nfs_server.hostname	= NULL;
1538 	ctx->fscache_uniq		= NULL;
1539 	ctx->clone_data.fattr		= NULL;
1540 	fc->fs_private = ctx;
1541 	return 0;
1542 }
1543 
1544 static void nfs_fs_context_free(struct fs_context *fc)
1545 {
1546 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1547 
1548 	if (ctx) {
1549 		if (ctx->server)
1550 			nfs_free_server(ctx->server);
1551 		if (ctx->nfs_mod)
1552 			put_nfs_version(ctx->nfs_mod);
1553 		kfree(ctx->client_address);
1554 		kfree(ctx->mount_server.hostname);
1555 		kfree(ctx->nfs_server.export_path);
1556 		kfree(ctx->nfs_server.hostname);
1557 		kfree(ctx->fscache_uniq);
1558 		nfs_free_fhandle(ctx->mntfh);
1559 		nfs_free_fattr(ctx->clone_data.fattr);
1560 		kfree(ctx);
1561 	}
1562 }
1563 
1564 static const struct fs_context_operations nfs_fs_context_ops = {
1565 	.free			= nfs_fs_context_free,
1566 	.dup			= nfs_fs_context_dup,
1567 	.parse_param		= nfs_fs_context_parse_param,
1568 	.parse_monolithic	= nfs_fs_context_parse_monolithic,
1569 	.get_tree		= nfs_get_tree,
1570 	.reconfigure		= nfs_reconfigure,
1571 };
1572 
1573 /*
1574  * Prepare superblock configuration.  We use the namespaces attached to the
1575  * context.  This may be the current process's namespaces, or it may be a
1576  * container's namespaces.
1577  */
1578 static int nfs_init_fs_context(struct fs_context *fc)
1579 {
1580 	struct nfs_fs_context *ctx;
1581 
1582 	ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1583 	if (unlikely(!ctx))
1584 		return -ENOMEM;
1585 
1586 	ctx->mntfh = nfs_alloc_fhandle();
1587 	if (unlikely(!ctx->mntfh)) {
1588 		kfree(ctx);
1589 		return -ENOMEM;
1590 	}
1591 
1592 	ctx->protofamily	= AF_UNSPEC;
1593 	ctx->mountfamily	= AF_UNSPEC;
1594 	ctx->mount_server.port	= NFS_UNSPEC_PORT;
1595 
1596 	if (fc->root) {
1597 		/* reconfigure, start with the current config */
1598 		struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1599 		struct net *net = nfss->nfs_client->cl_net;
1600 
1601 		ctx->flags		= nfss->flags;
1602 		ctx->rsize		= nfss->rsize;
1603 		ctx->wsize		= nfss->wsize;
1604 		ctx->retrans		= nfss->client->cl_timeout->to_retries;
1605 		ctx->selected_flavor	= nfss->client->cl_auth->au_flavor;
1606 		ctx->acregmin		= nfss->acregmin / HZ;
1607 		ctx->acregmax		= nfss->acregmax / HZ;
1608 		ctx->acdirmin		= nfss->acdirmin / HZ;
1609 		ctx->acdirmax		= nfss->acdirmax / HZ;
1610 		ctx->timeo		= 10U * nfss->client->cl_timeout->to_initval / HZ;
1611 		ctx->nfs_server.port	= nfss->port;
1612 		ctx->nfs_server.addrlen	= nfss->nfs_client->cl_addrlen;
1613 		ctx->version		= nfss->nfs_client->rpc_ops->version;
1614 		ctx->minorversion	= nfss->nfs_client->cl_minorversion;
1615 
1616 		memcpy(&ctx->nfs_server._address, &nfss->nfs_client->cl_addr,
1617 			ctx->nfs_server.addrlen);
1618 
1619 		if (fc->net_ns != net) {
1620 			put_net(fc->net_ns);
1621 			fc->net_ns = get_net(net);
1622 		}
1623 
1624 		ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1625 		__module_get(ctx->nfs_mod->owner);
1626 	} else {
1627 		/* defaults */
1628 		ctx->timeo		= NFS_UNSPEC_TIMEO;
1629 		ctx->retrans		= NFS_UNSPEC_RETRANS;
1630 		ctx->acregmin		= NFS_DEF_ACREGMIN;
1631 		ctx->acregmax		= NFS_DEF_ACREGMAX;
1632 		ctx->acdirmin		= NFS_DEF_ACDIRMIN;
1633 		ctx->acdirmax		= NFS_DEF_ACDIRMAX;
1634 		ctx->nfs_server.port	= NFS_UNSPEC_PORT;
1635 		ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1636 		ctx->selected_flavor	= RPC_AUTH_MAXFLAVOR;
1637 		ctx->minorversion	= 0;
1638 		ctx->need_mount		= true;
1639 		ctx->xprtsec.policy	= RPC_XPRTSEC_NONE;
1640 		ctx->xprtsec.cert_serial	= TLS_NO_CERT;
1641 		ctx->xprtsec.privkey_serial	= TLS_NO_PRIVKEY;
1642 
1643 		fc->s_iflags		|= SB_I_STABLE_WRITES;
1644 	}
1645 	fc->fs_private = ctx;
1646 	fc->ops = &nfs_fs_context_ops;
1647 	return 0;
1648 }
1649 
1650 struct file_system_type nfs_fs_type = {
1651 	.owner			= THIS_MODULE,
1652 	.name			= "nfs",
1653 	.init_fs_context	= nfs_init_fs_context,
1654 	.parameters		= nfs_fs_parameters,
1655 	.kill_sb		= nfs_kill_super,
1656 	.fs_flags		= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1657 };
1658 MODULE_ALIAS_FS("nfs");
1659 EXPORT_SYMBOL_GPL(nfs_fs_type);
1660 
1661 #if IS_ENABLED(CONFIG_NFS_V4)
1662 struct file_system_type nfs4_fs_type = {
1663 	.owner			= THIS_MODULE,
1664 	.name			= "nfs4",
1665 	.init_fs_context	= nfs_init_fs_context,
1666 	.parameters		= nfs_fs_parameters,
1667 	.kill_sb		= nfs_kill_super,
1668 	.fs_flags		= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1669 };
1670 MODULE_ALIAS_FS("nfs4");
1671 MODULE_ALIAS("nfs4");
1672 EXPORT_SYMBOL_GPL(nfs4_fs_type);
1673 #endif /* CONFIG_NFS_V4 */
1674