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