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