xref: /openbmc/linux/fs/nfs/fs_context.c (revision 3a83e4e6)
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 			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 			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 			fallthrough;
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 			fallthrough;
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;
903 		fallthrough;
904 	case 2:
905 		data->bsize = 0;
906 		fallthrough;
907 	case 3:
908 		if (data->flags & NFS_MOUNT_VER3)
909 			goto out_no_v3;
910 		data->root.size = NFS2_FHSIZE;
911 		memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
912 		/* Turn off security negotiation */
913 		extra_flags |= NFS_MOUNT_SECFLAVOUR;
914 		fallthrough;
915 	case 4:
916 		if (data->flags & NFS_MOUNT_SECFLAVOUR)
917 			goto out_no_sec;
918 		fallthrough;
919 	case 5:
920 		memset(data->context, 0, sizeof(data->context));
921 		fallthrough;
922 	case 6:
923 		if (data->flags & NFS_MOUNT_VER3) {
924 			if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
925 				goto out_invalid_fh;
926 			mntfh->size = data->root.size;
927 			ctx->version = 3;
928 		} else {
929 			mntfh->size = NFS2_FHSIZE;
930 			ctx->version = 2;
931 		}
932 
933 
934 		memcpy(mntfh->data, data->root.data, mntfh->size);
935 		if (mntfh->size < sizeof(mntfh->data))
936 			memset(mntfh->data + mntfh->size, 0,
937 			       sizeof(mntfh->data) - mntfh->size);
938 
939 		/*
940 		 * Translate to nfs_fs_context, which nfs_fill_super
941 		 * can deal with.
942 		 */
943 		ctx->flags	= data->flags & NFS_MOUNT_FLAGMASK;
944 		ctx->flags	|= extra_flags;
945 		ctx->rsize	= data->rsize;
946 		ctx->wsize	= data->wsize;
947 		ctx->timeo	= data->timeo;
948 		ctx->retrans	= data->retrans;
949 		ctx->acregmin	= data->acregmin;
950 		ctx->acregmax	= data->acregmax;
951 		ctx->acdirmin	= data->acdirmin;
952 		ctx->acdirmax	= data->acdirmax;
953 		ctx->need_mount	= false;
954 
955 		memcpy(sap, &data->addr, sizeof(data->addr));
956 		ctx->nfs_server.addrlen = sizeof(data->addr);
957 		ctx->nfs_server.port = ntohs(data->addr.sin_port);
958 		if (sap->sa_family != AF_INET ||
959 		    !nfs_verify_server_address(sap))
960 			goto out_no_address;
961 
962 		if (!(data->flags & NFS_MOUNT_TCP))
963 			ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
964 		/* N.B. caller will free nfs_server.hostname in all cases */
965 		ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
966 		if (!ctx->nfs_server.hostname)
967 			goto out_nomem;
968 
969 		ctx->namlen		= data->namlen;
970 		ctx->bsize		= data->bsize;
971 
972 		if (data->flags & NFS_MOUNT_SECFLAVOUR)
973 			ctx->selected_flavor = data->pseudoflavor;
974 		else
975 			ctx->selected_flavor = RPC_AUTH_UNIX;
976 
977 		if (!(data->flags & NFS_MOUNT_NONLM))
978 			ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
979 					 NFS_MOUNT_LOCAL_FCNTL);
980 		else
981 			ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
982 					NFS_MOUNT_LOCAL_FCNTL);
983 
984 		/*
985 		 * The legacy version 6 binary mount data from userspace has a
986 		 * field used only to transport selinux information into the
987 		 * kernel.  To continue to support that functionality we
988 		 * have a touch of selinux knowledge here in the NFS code. The
989 		 * userspace code converted context=blah to just blah so we are
990 		 * converting back to the full string selinux understands.
991 		 */
992 		if (data->context[0]){
993 #ifdef CONFIG_SECURITY_SELINUX
994 			int ret;
995 
996 			data->context[NFS_MAX_CONTEXT_LEN] = '\0';
997 			ret = vfs_parse_fs_string(fc, "context",
998 						  data->context, strlen(data->context));
999 			if (ret < 0)
1000 				return ret;
1001 #else
1002 			return -EINVAL;
1003 #endif
1004 		}
1005 
1006 		break;
1007 	default:
1008 		goto generic;
1009 	}
1010 
1011 	ctx->skip_reconfig_option_check = true;
1012 	return 0;
1013 
1014 generic:
1015 	return generic_parse_monolithic(fc, data);
1016 
1017 out_no_data:
1018 	if (is_remount_fc(fc)) {
1019 		ctx->skip_reconfig_option_check = true;
1020 		return 0;
1021 	}
1022 	return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
1023 
1024 out_no_v3:
1025 	return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
1026 
1027 out_no_sec:
1028 	return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
1029 
1030 out_nomem:
1031 	dfprintk(MOUNT, "NFS: not enough memory to handle mount options");
1032 	return -ENOMEM;
1033 
1034 out_no_address:
1035 	return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1036 
1037 out_invalid_fh:
1038 	return nfs_invalf(fc, "NFS: invalid root filehandle");
1039 }
1040 
1041 #if IS_ENABLED(CONFIG_NFS_V4)
1042 /*
1043  * Validate NFSv4 mount options
1044  */
1045 static int nfs4_parse_monolithic(struct fs_context *fc,
1046 				 struct nfs4_mount_data *data)
1047 {
1048 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1049 	struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
1050 	char *c;
1051 
1052 	if (data == NULL)
1053 		goto out_no_data;
1054 
1055 	ctx->version = 4;
1056 
1057 	switch (data->version) {
1058 	case 1:
1059 		if (data->host_addrlen > sizeof(ctx->nfs_server.address))
1060 			goto out_no_address;
1061 		if (data->host_addrlen == 0)
1062 			goto out_no_address;
1063 		ctx->nfs_server.addrlen = data->host_addrlen;
1064 		if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1065 			return -EFAULT;
1066 		if (!nfs_verify_server_address(sap))
1067 			goto out_no_address;
1068 		ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1069 
1070 		if (data->auth_flavourlen) {
1071 			rpc_authflavor_t pseudoflavor;
1072 			if (data->auth_flavourlen > 1)
1073 				goto out_inval_auth;
1074 			if (copy_from_user(&pseudoflavor,
1075 					   data->auth_flavours,
1076 					   sizeof(pseudoflavor)))
1077 				return -EFAULT;
1078 			ctx->selected_flavor = pseudoflavor;
1079 		} else
1080 			ctx->selected_flavor = RPC_AUTH_UNIX;
1081 
1082 		c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1083 		if (IS_ERR(c))
1084 			return PTR_ERR(c);
1085 		ctx->nfs_server.hostname = c;
1086 
1087 		c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1088 		if (IS_ERR(c))
1089 			return PTR_ERR(c);
1090 		ctx->nfs_server.export_path = c;
1091 		dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1092 
1093 		c = strndup_user(data->client_addr.data, 16);
1094 		if (IS_ERR(c))
1095 			return PTR_ERR(c);
1096 		ctx->client_address = c;
1097 
1098 		/*
1099 		 * Translate to nfs_fs_context, which nfs_fill_super
1100 		 * can deal with.
1101 		 */
1102 
1103 		ctx->flags	= data->flags & NFS4_MOUNT_FLAGMASK;
1104 		ctx->rsize	= data->rsize;
1105 		ctx->wsize	= data->wsize;
1106 		ctx->timeo	= data->timeo;
1107 		ctx->retrans	= data->retrans;
1108 		ctx->acregmin	= data->acregmin;
1109 		ctx->acregmax	= data->acregmax;
1110 		ctx->acdirmin	= data->acdirmin;
1111 		ctx->acdirmax	= data->acdirmax;
1112 		ctx->nfs_server.protocol = data->proto;
1113 		nfs_validate_transport_protocol(ctx);
1114 		if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1115 			goto out_invalid_transport_udp;
1116 
1117 		break;
1118 	default:
1119 		goto generic;
1120 	}
1121 
1122 	ctx->skip_reconfig_option_check = true;
1123 	return 0;
1124 
1125 generic:
1126 	return generic_parse_monolithic(fc, data);
1127 
1128 out_no_data:
1129 	if (is_remount_fc(fc)) {
1130 		ctx->skip_reconfig_option_check = true;
1131 		return 0;
1132 	}
1133 	return nfs_invalf(fc, "NFS4: mount program didn't pass any mount data");
1134 
1135 out_inval_auth:
1136 	return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1137 		      data->auth_flavourlen);
1138 
1139 out_no_address:
1140 	return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
1141 
1142 out_invalid_transport_udp:
1143 	return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
1144 }
1145 #endif
1146 
1147 /*
1148  * Parse a monolithic block of data from sys_mount().
1149  */
1150 static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1151 					   void *data)
1152 {
1153 	if (fc->fs_type == &nfs_fs_type)
1154 		return nfs23_parse_monolithic(fc, data);
1155 
1156 #if IS_ENABLED(CONFIG_NFS_V4)
1157 	if (fc->fs_type == &nfs4_fs_type)
1158 		return nfs4_parse_monolithic(fc, data);
1159 #endif
1160 
1161 	return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
1162 }
1163 
1164 /*
1165  * Validate the preparsed information in the config.
1166  */
1167 static int nfs_fs_context_validate(struct fs_context *fc)
1168 {
1169 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1170 	struct nfs_subversion *nfs_mod;
1171 	struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
1172 	int max_namelen = PAGE_SIZE;
1173 	int max_pathlen = NFS_MAXPATHLEN;
1174 	int port = 0;
1175 	int ret;
1176 
1177 	if (!fc->source)
1178 		goto out_no_device_name;
1179 
1180 	/* Check for sanity first. */
1181 	if (ctx->minorversion && ctx->version != 4)
1182 		goto out_minorversion_mismatch;
1183 
1184 	if (ctx->options & NFS_OPTION_MIGRATION &&
1185 	    (ctx->version != 4 || ctx->minorversion != 0))
1186 		goto out_migration_misuse;
1187 
1188 	/* Verify that any proto=/mountproto= options match the address
1189 	 * families in the addr=/mountaddr= options.
1190 	 */
1191 	if (ctx->protofamily != AF_UNSPEC &&
1192 	    ctx->protofamily != ctx->nfs_server.address.sa_family)
1193 		goto out_proto_mismatch;
1194 
1195 	if (ctx->mountfamily != AF_UNSPEC) {
1196 		if (ctx->mount_server.addrlen) {
1197 			if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1198 				goto out_mountproto_mismatch;
1199 		} else {
1200 			if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1201 				goto out_mountproto_mismatch;
1202 		}
1203 	}
1204 
1205 	if (!nfs_verify_server_address(sap))
1206 		goto out_no_address;
1207 
1208 	if (ctx->version == 4) {
1209 		if (IS_ENABLED(CONFIG_NFS_V4)) {
1210 			if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1211 				port = NFS_RDMA_PORT;
1212 			else
1213 				port = NFS_PORT;
1214 			max_namelen = NFS4_MAXNAMLEN;
1215 			max_pathlen = NFS4_MAXPATHLEN;
1216 			nfs_validate_transport_protocol(ctx);
1217 			if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1218 				goto out_invalid_transport_udp;
1219 			ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1220 					NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1221 					NFS_MOUNT_LOCAL_FCNTL);
1222 		} else {
1223 			goto out_v4_not_compiled;
1224 		}
1225 	} else {
1226 		nfs_set_mount_transport_protocol(ctx);
1227 #ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
1228 	       if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1229 		       goto out_invalid_transport_udp;
1230 #endif
1231 		if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1232 			port = NFS_RDMA_PORT;
1233 	}
1234 
1235 	nfs_set_port(sap, &ctx->nfs_server.port, port);
1236 
1237 	ret = nfs_parse_source(fc, max_namelen, max_pathlen);
1238 	if (ret < 0)
1239 		return ret;
1240 
1241 	/* Load the NFS protocol module if we haven't done so yet */
1242 	if (!ctx->nfs_mod) {
1243 		nfs_mod = get_nfs_version(ctx->version);
1244 		if (IS_ERR(nfs_mod)) {
1245 			ret = PTR_ERR(nfs_mod);
1246 			goto out_version_unavailable;
1247 		}
1248 		ctx->nfs_mod = nfs_mod;
1249 	}
1250 
1251 	/* Ensure the filesystem context has the correct fs_type */
1252 	if (fc->fs_type != ctx->nfs_mod->nfs_fs) {
1253 		module_put(fc->fs_type->owner);
1254 		__module_get(ctx->nfs_mod->nfs_fs->owner);
1255 		fc->fs_type = ctx->nfs_mod->nfs_fs;
1256 	}
1257 	return 0;
1258 
1259 out_no_device_name:
1260 	return nfs_invalf(fc, "NFS: Device name not specified");
1261 out_v4_not_compiled:
1262 	nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
1263 	return -EPROTONOSUPPORT;
1264 out_invalid_transport_udp:
1265 	return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
1266 out_no_address:
1267 	return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1268 out_mountproto_mismatch:
1269 	return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
1270 out_proto_mismatch:
1271 	return nfs_invalf(fc, "NFS: Server address does not match proto= option");
1272 out_minorversion_mismatch:
1273 	return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
1274 			  ctx->version, ctx->minorversion);
1275 out_migration_misuse:
1276 	return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
1277 out_version_unavailable:
1278 	nfs_errorf(fc, "NFS: Version unavailable");
1279 	return ret;
1280 }
1281 
1282 /*
1283  * Create an NFS superblock by the appropriate method.
1284  */
1285 static int nfs_get_tree(struct fs_context *fc)
1286 {
1287 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1288 	int err = nfs_fs_context_validate(fc);
1289 
1290 	if (err)
1291 		return err;
1292 	if (!ctx->internal)
1293 		return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
1294 	else
1295 		return nfs_get_tree_common(fc);
1296 }
1297 
1298 /*
1299  * Handle duplication of a configuration.  The caller copied *src into *sc, but
1300  * it can't deal with resource pointers in the filesystem context, so we have
1301  * to do that.  We need to clear pointers, copy data or get extra refs as
1302  * appropriate.
1303  */
1304 static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1305 {
1306 	struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1307 
1308 	ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1309 	if (!ctx)
1310 		return -ENOMEM;
1311 
1312 	ctx->mntfh = nfs_alloc_fhandle();
1313 	if (!ctx->mntfh) {
1314 		kfree(ctx);
1315 		return -ENOMEM;
1316 	}
1317 	nfs_copy_fh(ctx->mntfh, src->mntfh);
1318 
1319 	__module_get(ctx->nfs_mod->owner);
1320 	ctx->client_address		= NULL;
1321 	ctx->mount_server.hostname	= NULL;
1322 	ctx->nfs_server.export_path	= NULL;
1323 	ctx->nfs_server.hostname	= NULL;
1324 	ctx->fscache_uniq		= NULL;
1325 	ctx->clone_data.fattr		= NULL;
1326 	fc->fs_private = ctx;
1327 	return 0;
1328 }
1329 
1330 static void nfs_fs_context_free(struct fs_context *fc)
1331 {
1332 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1333 
1334 	if (ctx) {
1335 		if (ctx->server)
1336 			nfs_free_server(ctx->server);
1337 		if (ctx->nfs_mod)
1338 			put_nfs_version(ctx->nfs_mod);
1339 		kfree(ctx->client_address);
1340 		kfree(ctx->mount_server.hostname);
1341 		kfree(ctx->nfs_server.export_path);
1342 		kfree(ctx->nfs_server.hostname);
1343 		kfree(ctx->fscache_uniq);
1344 		nfs_free_fhandle(ctx->mntfh);
1345 		nfs_free_fattr(ctx->clone_data.fattr);
1346 		kfree(ctx);
1347 	}
1348 }
1349 
1350 static const struct fs_context_operations nfs_fs_context_ops = {
1351 	.free			= nfs_fs_context_free,
1352 	.dup			= nfs_fs_context_dup,
1353 	.parse_param		= nfs_fs_context_parse_param,
1354 	.parse_monolithic	= nfs_fs_context_parse_monolithic,
1355 	.get_tree		= nfs_get_tree,
1356 	.reconfigure		= nfs_reconfigure,
1357 };
1358 
1359 /*
1360  * Prepare superblock configuration.  We use the namespaces attached to the
1361  * context.  This may be the current process's namespaces, or it may be a
1362  * container's namespaces.
1363  */
1364 static int nfs_init_fs_context(struct fs_context *fc)
1365 {
1366 	struct nfs_fs_context *ctx;
1367 
1368 	ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1369 	if (unlikely(!ctx))
1370 		return -ENOMEM;
1371 
1372 	ctx->mntfh = nfs_alloc_fhandle();
1373 	if (unlikely(!ctx->mntfh)) {
1374 		kfree(ctx);
1375 		return -ENOMEM;
1376 	}
1377 
1378 	ctx->protofamily	= AF_UNSPEC;
1379 	ctx->mountfamily	= AF_UNSPEC;
1380 	ctx->mount_server.port	= NFS_UNSPEC_PORT;
1381 
1382 	if (fc->root) {
1383 		/* reconfigure, start with the current config */
1384 		struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1385 		struct net *net = nfss->nfs_client->cl_net;
1386 
1387 		ctx->flags		= nfss->flags;
1388 		ctx->rsize		= nfss->rsize;
1389 		ctx->wsize		= nfss->wsize;
1390 		ctx->retrans		= nfss->client->cl_timeout->to_retries;
1391 		ctx->selected_flavor	= nfss->client->cl_auth->au_flavor;
1392 		ctx->acregmin		= nfss->acregmin / HZ;
1393 		ctx->acregmax		= nfss->acregmax / HZ;
1394 		ctx->acdirmin		= nfss->acdirmin / HZ;
1395 		ctx->acdirmax		= nfss->acdirmax / HZ;
1396 		ctx->timeo		= 10U * nfss->client->cl_timeout->to_initval / HZ;
1397 		ctx->nfs_server.port	= nfss->port;
1398 		ctx->nfs_server.addrlen	= nfss->nfs_client->cl_addrlen;
1399 		ctx->version		= nfss->nfs_client->rpc_ops->version;
1400 		ctx->minorversion	= nfss->nfs_client->cl_minorversion;
1401 
1402 		memcpy(&ctx->nfs_server.address, &nfss->nfs_client->cl_addr,
1403 			ctx->nfs_server.addrlen);
1404 
1405 		if (fc->net_ns != net) {
1406 			put_net(fc->net_ns);
1407 			fc->net_ns = get_net(net);
1408 		}
1409 
1410 		ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1411 		__module_get(ctx->nfs_mod->owner);
1412 	} else {
1413 		/* defaults */
1414 		ctx->timeo		= NFS_UNSPEC_TIMEO;
1415 		ctx->retrans		= NFS_UNSPEC_RETRANS;
1416 		ctx->acregmin		= NFS_DEF_ACREGMIN;
1417 		ctx->acregmax		= NFS_DEF_ACREGMAX;
1418 		ctx->acdirmin		= NFS_DEF_ACDIRMIN;
1419 		ctx->acdirmax		= NFS_DEF_ACDIRMAX;
1420 		ctx->nfs_server.port	= NFS_UNSPEC_PORT;
1421 		ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1422 		ctx->selected_flavor	= RPC_AUTH_MAXFLAVOR;
1423 		ctx->minorversion	= 0;
1424 		ctx->need_mount		= true;
1425 	}
1426 	fc->fs_private = ctx;
1427 	fc->ops = &nfs_fs_context_ops;
1428 	return 0;
1429 }
1430 
1431 struct file_system_type nfs_fs_type = {
1432 	.owner			= THIS_MODULE,
1433 	.name			= "nfs",
1434 	.init_fs_context	= nfs_init_fs_context,
1435 	.parameters		= nfs_fs_parameters,
1436 	.kill_sb		= nfs_kill_super,
1437 	.fs_flags		= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1438 };
1439 MODULE_ALIAS_FS("nfs");
1440 EXPORT_SYMBOL_GPL(nfs_fs_type);
1441 
1442 #if IS_ENABLED(CONFIG_NFS_V4)
1443 struct file_system_type nfs4_fs_type = {
1444 	.owner			= THIS_MODULE,
1445 	.name			= "nfs4",
1446 	.init_fs_context	= nfs_init_fs_context,
1447 	.parameters		= nfs_fs_parameters,
1448 	.kill_sb		= nfs_kill_super,
1449 	.fs_flags		= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1450 };
1451 MODULE_ALIAS_FS("nfs4");
1452 MODULE_ALIAS("nfs4");
1453 EXPORT_SYMBOL_GPL(nfs4_fs_type);
1454 #endif /* CONFIG_NFS_V4 */
1455