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