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