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