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