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