1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/nfs/fs_context.c
5 * Copyright (C) 1992 Rick Sladkey
6 * Conversion to new mount api Copyright (C) David Howells
10 * Split from fs/nfs/super.c by David Howells <dhowells@redhat.com>
13 #include <linux/module.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>
23 #define NFSDBG_FACILITY NFSDBG_MOUNT
25 #if IS_ENABLED(CONFIG_NFS_V3)
26 #define NFS_DEFAULT_VERSION 3
28 #define NFS_DEFAULT_VERSION 2
31 #define NFS_MAX_CONNECTIONS 16
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
},
103 Opt_lookupcache_none
,
104 Opt_lookupcache_positive
,
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
},
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
),
186 static const struct constant_table nfs_vers_tokens
[] = {
190 { "4.0", Opt_vers_4_0
},
191 { "4.1", Opt_vers_4_1
},
192 { "4.2", Opt_vers_4_2
},
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
},
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
},
245 * Sanity-check a server address provided by the mount command.
247 * Address family must be initialized, and address must not be
248 * the ANY address for that family.
250 static int nfs_verify_server_address(struct sockaddr
*addr
)
252 switch (addr
->sa_family
) {
254 struct sockaddr_in
*sa
= (struct sockaddr_in
*)addr
;
255 return sa
->sin_addr
.s_addr
!= htonl(INADDR_ANY
);
258 struct in6_addr
*sa
= &((struct sockaddr_in6
*)addr
)->sin6_addr
;
259 return !ipv6_addr_any(sa
);
263 dfprintk(MOUNT
, "NFS: Invalid IP address specified\n");
268 * Sanity check the NFS transport protocol.
271 static void nfs_validate_transport_protocol(struct nfs_fs_context
*ctx
)
273 switch (ctx
->nfs_server
.protocol
) {
274 case XPRT_TRANSPORT_UDP
:
275 case XPRT_TRANSPORT_TCP
:
276 case XPRT_TRANSPORT_RDMA
:
279 ctx
->nfs_server
.protocol
= XPRT_TRANSPORT_TCP
;
284 * For text based NFSv2/v3 mounts, the mount protocol transport default
285 * settings should depend upon the specified NFS transport.
287 static void nfs_set_mount_transport_protocol(struct nfs_fs_context
*ctx
)
289 nfs_validate_transport_protocol(ctx
);
291 if (ctx
->mount_server
.protocol
== XPRT_TRANSPORT_UDP
||
292 ctx
->mount_server
.protocol
== XPRT_TRANSPORT_TCP
)
294 switch (ctx
->nfs_server
.protocol
) {
295 case XPRT_TRANSPORT_UDP
:
296 ctx
->mount_server
.protocol
= XPRT_TRANSPORT_UDP
;
298 case XPRT_TRANSPORT_TCP
:
299 case XPRT_TRANSPORT_RDMA
:
300 ctx
->mount_server
.protocol
= XPRT_TRANSPORT_TCP
;
305 * Add 'flavor' to 'auth_info' if not already present.
306 * Returns true if 'flavor' ends up in the list, false otherwise
308 static int nfs_auth_info_add(struct fs_context
*fc
,
309 struct nfs_auth_info
*auth_info
,
310 rpc_authflavor_t flavor
)
313 unsigned int max_flavor_len
= ARRAY_SIZE(auth_info
->flavors
);
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
])
321 if (auth_info
->flavor_len
+ 1 >= max_flavor_len
)
322 return nfs_invalf(fc
, "NFS: too many sec= flavors");
324 auth_info
->flavors
[auth_info
->flavor_len
++] = flavor
;
329 * Parse the value of the 'sec=' option.
331 static int nfs_parse_security_flavors(struct fs_context
*fc
,
332 struct fs_parameter
*param
)
334 struct nfs_fs_context
*ctx
= nfs_fc2context(fc
);
335 rpc_authflavor_t pseudoflavor
;
336 char *string
= param
->string
, *p
;
339 dfprintk(MOUNT
, "NFS: parsing %s=%s option\n", param
->key
, param
->string
);
341 while ((p
= strsep(&string
, ":")) != NULL
) {
344 switch (lookup_constant(nfs_secflavor_tokens
, p
, -1)) {
346 pseudoflavor
= RPC_AUTH_NULL
;
349 pseudoflavor
= RPC_AUTH_UNIX
;
352 pseudoflavor
= RPC_AUTH_GSS_KRB5
;
355 pseudoflavor
= RPC_AUTH_GSS_KRB5I
;
358 pseudoflavor
= RPC_AUTH_GSS_KRB5P
;
361 pseudoflavor
= RPC_AUTH_GSS_LKEY
;
364 pseudoflavor
= RPC_AUTH_GSS_LKEYI
;
367 pseudoflavor
= RPC_AUTH_GSS_LKEYP
;
370 pseudoflavor
= RPC_AUTH_GSS_SPKM
;
373 pseudoflavor
= RPC_AUTH_GSS_SPKMI
;
376 pseudoflavor
= RPC_AUTH_GSS_SPKMP
;
379 return nfs_invalf(fc
, "NFS: sec=%s option not recognized", p
);
382 ret
= nfs_auth_info_add(fc
, &ctx
->auth_info
, pseudoflavor
);
390 static int nfs_parse_version_string(struct fs_context
*fc
,
393 struct nfs_fs_context
*ctx
= nfs_fc2context(fc
);
395 ctx
->flags
&= ~NFS_MOUNT_VER3
;
396 switch (lookup_constant(nfs_vers_tokens
, string
, -1)) {
401 ctx
->flags
|= NFS_MOUNT_VER3
;
405 /* Backward compatibility option. In future,
406 * the mount program should always supply
407 * a NFSv4 minor version number.
413 ctx
->minorversion
= 0;
417 ctx
->minorversion
= 1;
421 ctx
->minorversion
= 2;
424 return nfs_invalf(fc
, "NFS: Unsupported NFS version");
430 * Parse a single mount parameter.
432 static int nfs_fs_context_parse_param(struct fs_context
*fc
,
433 struct fs_parameter
*param
)
435 struct fs_parse_result result
;
436 struct nfs_fs_context
*ctx
= nfs_fc2context(fc
);
437 unsigned short protofamily
, mountfamily
;
441 dfprintk(MOUNT
, "NFS: parsing nfs mount option '%s'\n", param
->key
);
443 opt
= fs_parse(fc
, nfs_fs_parameters
, param
, &result
);
445 return ctx
->sloppy
? 1 : opt
;
450 return nfs_invalf(fc
, "NFS: Multiple sources not supported");
451 fc
->source
= param
->string
;
452 param
->string
= NULL
;
456 * boolean options: foo/nofoo
459 ctx
->flags
|= NFS_MOUNT_SOFT
;
460 ctx
->flags
&= ~NFS_MOUNT_SOFTERR
;
463 ctx
->flags
|= NFS_MOUNT_SOFTERR
| NFS_MOUNT_SOFTREVAL
;
464 ctx
->flags
&= ~NFS_MOUNT_SOFT
;
467 ctx
->flags
&= ~(NFS_MOUNT_SOFT
|
469 NFS_MOUNT_SOFTREVAL
);
473 ctx
->flags
&= ~NFS_MOUNT_SOFTREVAL
;
475 ctx
->flags
&= NFS_MOUNT_SOFTREVAL
;
479 ctx
->flags
&= ~NFS_MOUNT_POSIX
;
481 ctx
->flags
|= NFS_MOUNT_POSIX
;
485 ctx
->flags
|= NFS_MOUNT_NOCTO
;
487 ctx
->flags
&= ~NFS_MOUNT_NOCTO
;
491 ctx
->flags
|= NFS_MOUNT_NOAC
;
493 ctx
->flags
&= ~NFS_MOUNT_NOAC
;
496 if (result
.negated
) {
497 ctx
->flags
|= NFS_MOUNT_NONLM
;
498 ctx
->flags
|= (NFS_MOUNT_LOCAL_FLOCK
| NFS_MOUNT_LOCAL_FCNTL
);
500 ctx
->flags
&= ~NFS_MOUNT_NONLM
;
501 ctx
->flags
&= ~(NFS_MOUNT_LOCAL_FLOCK
| NFS_MOUNT_LOCAL_FCNTL
);
505 ctx
->flags
&= ~NFS_MOUNT_TCP
;
506 ctx
->nfs_server
.protocol
= XPRT_TRANSPORT_UDP
;
509 ctx
->flags
|= NFS_MOUNT_TCP
;
510 ctx
->nfs_server
.protocol
= XPRT_TRANSPORT_TCP
;
513 ctx
->flags
|= NFS_MOUNT_TCP
; /* for side protocols */
514 ctx
->nfs_server
.protocol
= XPRT_TRANSPORT_RDMA
;
515 xprt_load_transport(param
->key
);
519 ctx
->flags
|= NFS_MOUNT_NOACL
;
521 ctx
->flags
&= ~NFS_MOUNT_NOACL
;
525 ctx
->flags
|= NFS_MOUNT_NORDIRPLUS
;
527 ctx
->flags
&= ~NFS_MOUNT_NORDIRPLUS
;
531 ctx
->flags
|= NFS_MOUNT_UNSHARED
;
533 ctx
->flags
&= ~NFS_MOUNT_UNSHARED
;
537 ctx
->flags
|= NFS_MOUNT_NORESVPORT
;
539 ctx
->flags
&= ~NFS_MOUNT_NORESVPORT
;
541 case Opt_fscache_flag
:
543 ctx
->options
&= ~NFS_OPTION_FSCACHE
;
545 ctx
->options
|= NFS_OPTION_FSCACHE
;
546 kfree(ctx
->fscache_uniq
);
547 ctx
->fscache_uniq
= NULL
;
550 ctx
->options
|= NFS_OPTION_FSCACHE
;
551 kfree(ctx
->fscache_uniq
);
552 ctx
->fscache_uniq
= param
->string
;
553 param
->string
= NULL
;
557 ctx
->options
&= ~NFS_OPTION_MIGRATION
;
559 ctx
->options
|= NFS_OPTION_MIGRATION
;
563 * options that take numeric values
566 if (result
.uint_32
> USHRT_MAX
)
568 ctx
->nfs_server
.port
= result
.uint_32
;
571 ctx
->rsize
= result
.uint_32
;
574 ctx
->wsize
= result
.uint_32
;
577 ctx
->bsize
= result
.uint_32
;
580 if (result
.uint_32
< 1 || result
.uint_32
> INT_MAX
)
582 ctx
->timeo
= result
.uint_32
;
585 if (result
.uint_32
> INT_MAX
)
587 ctx
->retrans
= result
.uint_32
;
590 ctx
->acregmin
= result
.uint_32
;
593 ctx
->acregmax
= result
.uint_32
;
596 ctx
->acdirmin
= result
.uint_32
;
599 ctx
->acdirmax
= result
.uint_32
;
602 ctx
->acregmin
= result
.uint_32
;
603 ctx
->acregmax
= result
.uint_32
;
604 ctx
->acdirmin
= result
.uint_32
;
605 ctx
->acdirmax
= result
.uint_32
;
608 ctx
->namlen
= result
.uint_32
;
611 if (result
.uint_32
> USHRT_MAX
)
613 ctx
->mount_server
.port
= result
.uint_32
;
616 if (result
.uint_32
< NFS_MNT_VERSION
||
617 result
.uint_32
> NFS_MNT3_VERSION
)
619 ctx
->mount_server
.version
= result
.uint_32
;
621 case Opt_minorversion
:
622 if (result
.uint_32
> NFS4_MAX_MINOR_VERSION
)
624 ctx
->minorversion
= result
.uint_32
;
628 * options that take text values
631 ret
= nfs_parse_version_string(fc
, param
->key
+ 1);
636 ret
= nfs_parse_version_string(fc
, param
->string
);
641 ret
= nfs_parse_security_flavors(fc
, param
);
647 protofamily
= AF_INET
;
648 switch (lookup_constant(nfs_xprt_protocol_tokens
, param
->string
, -1)) {
650 protofamily
= AF_INET6
;
653 ctx
->flags
&= ~NFS_MOUNT_TCP
;
654 ctx
->nfs_server
.protocol
= XPRT_TRANSPORT_UDP
;
657 protofamily
= AF_INET6
;
660 ctx
->flags
|= NFS_MOUNT_TCP
;
661 ctx
->nfs_server
.protocol
= XPRT_TRANSPORT_TCP
;
664 protofamily
= AF_INET6
;
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
);
673 return nfs_invalf(fc
, "NFS: Unrecognized transport protocol");
676 ctx
->protofamily
= protofamily
;
680 mountfamily
= AF_INET
;
681 switch (lookup_constant(nfs_xprt_protocol_tokens
, param
->string
, -1)) {
683 mountfamily
= AF_INET6
;
686 ctx
->mount_server
.protocol
= XPRT_TRANSPORT_UDP
;
689 mountfamily
= AF_INET6
;
692 ctx
->mount_server
.protocol
= XPRT_TRANSPORT_TCP
;
694 case Opt_xprt_rdma
: /* not used for side protocols */
696 return nfs_invalf(fc
, "NFS: Unrecognized transport protocol");
698 ctx
->mountfamily
= mountfamily
;
702 len
= rpc_pton(fc
->net_ns
, param
->string
, param
->size
,
703 &ctx
->nfs_server
.address
,
704 sizeof(ctx
->nfs_server
._address
));
706 goto out_invalid_address
;
707 ctx
->nfs_server
.addrlen
= len
;
710 kfree(ctx
->client_address
);
711 ctx
->client_address
= param
->string
;
712 param
->string
= NULL
;
715 kfree(ctx
->mount_server
.hostname
);
716 ctx
->mount_server
.hostname
= param
->string
;
717 param
->string
= NULL
;
720 len
= rpc_pton(fc
->net_ns
, param
->string
, param
->size
,
721 &ctx
->mount_server
.address
,
722 sizeof(ctx
->mount_server
._address
));
724 goto out_invalid_address
;
725 ctx
->mount_server
.addrlen
= len
;
728 if (result
.uint_32
< 1 || result
.uint_32
> NFS_MAX_CONNECTIONS
)
730 ctx
->nfs_server
.nconnect
= result
.uint_32
;
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
);
737 case Opt_lookupcache_positive
:
738 ctx
->flags
&= ~NFS_MOUNT_LOOKUP_CACHE_NONE
;
739 ctx
->flags
|= NFS_MOUNT_LOOKUP_CACHE_NONEG
;
741 case Opt_lookupcache_none
:
742 ctx
->flags
|= NFS_MOUNT_LOOKUP_CACHE_NONEG
|NFS_MOUNT_LOOKUP_CACHE_NONE
;
745 goto out_invalid_value
;
749 switch (result
.uint_32
) {
750 case Opt_local_lock_all
:
751 ctx
->flags
|= (NFS_MOUNT_LOCAL_FLOCK
|
752 NFS_MOUNT_LOCAL_FCNTL
);
754 case Opt_local_lock_flock
:
755 ctx
->flags
|= NFS_MOUNT_LOCAL_FLOCK
;
757 case Opt_local_lock_posix
:
758 ctx
->flags
|= NFS_MOUNT_LOCAL_FCNTL
;
760 case Opt_local_lock_none
:
761 ctx
->flags
&= ~(NFS_MOUNT_LOCAL_FLOCK
|
762 NFS_MOUNT_LOCAL_FCNTL
);
765 goto out_invalid_value
;
774 dfprintk(MOUNT
, "NFS: relaxing parsing rules\n");
781 return nfs_invalf(fc
, "NFS: Bad mount option value specified");
783 return nfs_invalf(fc
, "NFS: Bad IP address specified");
785 return nfs_invalf(fc
, "NFS: Value for '%s' out of range", param
->key
);
789 * Split fc->source into "hostname:export_path".
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.
795 * Note: caller frees hostname and export path, even on error.
797 static int nfs_parse_source(struct fs_context
*fc
,
798 size_t maxnamlen
, size_t maxpathlen
)
800 struct nfs_fs_context
*ctx
= nfs_fc2context(fc
);
801 const char *dev_name
= fc
->source
;
805 if (unlikely(!dev_name
|| !*dev_name
)) {
806 dfprintk(MOUNT
, "NFS: device name not specified\n");
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
;
816 len
= end
- dev_name
;
821 end
= strchr(dev_name
, ':');
823 goto out_bad_devname
;
824 len
= end
- dev_name
;
826 /* kill possible hostname list: not supported */
827 comma
= memchr(dev_name
, ',', len
);
829 len
= comma
- dev_name
;
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
)
840 if (len
> maxpathlen
)
842 ctx
->nfs_server
.export_path
= kmemdup_nul(end
, len
, GFP_KERNEL
);
843 if (!ctx
->nfs_server
.export_path
)
846 dfprintk(MOUNT
, "NFS: MNTPATH: '%s'\n", ctx
->nfs_server
.export_path
);
850 return nfs_invalf(fc
, "NFS: device name not in host:path format");
852 nfs_errorf(fc
, "NFS: not enough memory to parse device name");
855 nfs_errorf(fc
, "NFS: server hostname too long");
856 return -ENAMETOOLONG
;
858 nfs_errorf(fc
, "NFS: export pathname too long");
859 return -ENAMETOOLONG
;
862 static inline bool is_remount_fc(struct fs_context
*fc
)
864 return fc
->root
!= NULL
;
868 * Parse monolithic NFS2/NFS3 mount data
869 * - fills in the mount root filehandle
871 * For option strings, user space handles the following behaviors:
873 * + DNS: mapping server host name to IP address ("addr=" option)
875 * + failure mode: how to behave if a mount request can't be handled
876 * immediately ("fg/bg" option)
878 * + retry: how often to retry a mount request ("retry=" option)
880 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
881 * mountproto=tcp after mountproto=udp, and so on
883 static int nfs23_parse_monolithic(struct fs_context
*fc
,
884 struct nfs_mount_data
*data
)
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
;
894 ctx
->version
= NFS_DEFAULT_VERSION
;
895 switch (data
->version
) {
897 data
->namlen
= 0; /* fall through */
899 data
->bsize
= 0; /* fall through */
901 if (data
->flags
& NFS_MOUNT_VER3
)
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
;
909 if (data
->flags
& NFS_MOUNT_SECFLAVOUR
)
913 memset(data
->context
, 0, sizeof(data
->context
));
916 if (data
->flags
& NFS_MOUNT_VER3
) {
917 if (data
->root
.size
> NFS3_FHSIZE
|| data
->root
.size
== 0)
919 mntfh
->size
= data
->root
.size
;
922 mntfh
->size
= NFS2_FHSIZE
;
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
);
933 * Translate to nfs_fs_context, which nfs_fill_super
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;
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
))
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
)
962 ctx
->namlen
= data
->namlen
;
963 ctx
->bsize
= data
->bsize
;
965 if (data
->flags
& NFS_MOUNT_SECFLAVOUR
)
966 ctx
->selected_flavor
= data
->pseudoflavor
;
968 ctx
->selected_flavor
= RPC_AUTH_UNIX
;
970 if (!(data
->flags
& NFS_MOUNT_NONLM
))
971 ctx
->flags
&= ~(NFS_MOUNT_LOCAL_FLOCK
|
972 NFS_MOUNT_LOCAL_FCNTL
);
974 ctx
->flags
|= (NFS_MOUNT_LOCAL_FLOCK
|
975 NFS_MOUNT_LOCAL_FCNTL
);
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.
985 if (data
->context
[0]){
986 #ifdef CONFIG_SECURITY_SELINUX
989 data
->context
[NFS_MAX_CONTEXT_LEN
] = '\0';
990 ret
= vfs_parse_fs_string(fc
, "context",
991 data
->context
, strlen(data
->context
));
1004 ctx
->skip_reconfig_option_check
= true;
1008 return generic_parse_monolithic(fc
, data
);
1011 if (is_remount_fc(fc
)) {
1012 ctx
->skip_reconfig_option_check
= true;
1015 return nfs_invalf(fc
, "NFS: mount program didn't pass any mount data");
1018 return nfs_invalf(fc
, "NFS: nfs_mount_data version does not support v3");
1021 return nfs_invalf(fc
, "NFS: nfs_mount_data version supports only AUTH_SYS");
1024 dfprintk(MOUNT
, "NFS: not enough memory to handle mount options");
1028 return nfs_invalf(fc
, "NFS: mount program didn't pass remote address");
1031 return nfs_invalf(fc
, "NFS: invalid root filehandle");
1034 #if IS_ENABLED(CONFIG_NFS_V4)
1036 * Validate NFSv4 mount options
1038 static int nfs4_parse_monolithic(struct fs_context
*fc
,
1039 struct nfs4_mount_data
*data
)
1041 struct nfs_fs_context
*ctx
= nfs_fc2context(fc
);
1042 struct sockaddr
*sap
= (struct sockaddr
*)&ctx
->nfs_server
.address
;
1050 switch (data
->version
) {
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
))
1059 if (!nfs_verify_server_address(sap
))
1060 goto out_no_address
;
1061 ctx
->nfs_server
.port
= ntohs(((struct sockaddr_in
*)sap
)->sin_port
);
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
)))
1071 ctx
->selected_flavor
= pseudoflavor
;
1073 ctx
->selected_flavor
= RPC_AUTH_UNIX
;
1075 c
= strndup_user(data
->hostname
.data
, NFS4_MAXNAMLEN
);
1078 ctx
->nfs_server
.hostname
= c
;
1080 c
= strndup_user(data
->mnt_path
.data
, NFS4_MAXPATHLEN
);
1083 ctx
->nfs_server
.export_path
= c
;
1084 dfprintk(MOUNT
, "NFS: MNTPATH: '%s'\n", c
);
1086 c
= strndup_user(data
->client_addr
.data
, 16);
1089 ctx
->client_address
= c
;
1092 * Translate to nfs_fs_context, which nfs_fill_super
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
;
1115 ctx
->skip_reconfig_option_check
= true;
1119 return generic_parse_monolithic(fc
, data
);
1122 if (is_remount_fc(fc
)) {
1123 ctx
->skip_reconfig_option_check
= true;
1126 return nfs_invalf(fc
, "NFS4: mount program didn't pass any mount data");
1129 return nfs_invalf(fc
, "NFS4: Invalid number of RPC auth flavours %d",
1130 data
->auth_flavourlen
);
1133 return nfs_invalf(fc
, "NFS4: mount program didn't pass remote address");
1135 out_invalid_transport_udp
:
1136 return nfs_invalf(fc
, "NFSv4: Unsupported transport protocol udp");
1141 * Parse a monolithic block of data from sys_mount().
1143 static int nfs_fs_context_parse_monolithic(struct fs_context
*fc
,
1146 if (fc
->fs_type
== &nfs_fs_type
)
1147 return nfs23_parse_monolithic(fc
, data
);
1149 #if IS_ENABLED(CONFIG_NFS_V4)
1150 if (fc
->fs_type
== &nfs4_fs_type
)
1151 return nfs4_parse_monolithic(fc
, data
);
1154 return nfs_invalf(fc
, "NFS: Unsupported monolithic data version");
1158 * Validate the preparsed information in the config.
1160 static int nfs_fs_context_validate(struct fs_context
*fc
)
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
;
1171 goto out_no_device_name
;
1173 /* Check for sanity first. */
1174 if (ctx
->minorversion
&& ctx
->version
!= 4)
1175 goto out_minorversion_mismatch
;
1177 if (ctx
->options
& NFS_OPTION_MIGRATION
&&
1178 (ctx
->version
!= 4 || ctx
->minorversion
!= 0))
1179 goto out_migration_misuse
;
1181 /* Verify that any proto=/mountproto= options match the address
1182 * families in the addr=/mountaddr= options.
1184 if (ctx
->protofamily
!= AF_UNSPEC
&&
1185 ctx
->protofamily
!= ctx
->nfs_server
.address
.sa_family
)
1186 goto out_proto_mismatch
;
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
;
1193 if (ctx
->mountfamily
!= ctx
->nfs_server
.address
.sa_family
)
1194 goto out_mountproto_mismatch
;
1198 if (!nfs_verify_server_address(sap
))
1199 goto out_no_address
;
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
;
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
);
1216 goto out_v4_not_compiled
;
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
;
1224 if (ctx
->nfs_server
.protocol
== XPRT_TRANSPORT_RDMA
)
1225 port
= NFS_RDMA_PORT
;
1228 nfs_set_port(sap
, &ctx
->nfs_server
.port
, port
);
1230 ret
= nfs_parse_source(fc
, max_namelen
, max_pathlen
);
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
;
1241 ctx
->nfs_mod
= nfs_mod
;
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");
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");
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");
1269 * Create an NFS superblock by the appropriate method.
1271 static int nfs_get_tree(struct fs_context
*fc
)
1273 struct nfs_fs_context
*ctx
= nfs_fc2context(fc
);
1274 int err
= nfs_fs_context_validate(fc
);
1279 return ctx
->nfs_mod
->rpc_ops
->try_get_tree(fc
);
1281 return nfs_get_tree_common(fc
);
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
1290 static int nfs_fs_context_dup(struct fs_context
*fc
, struct fs_context
*src_fc
)
1292 struct nfs_fs_context
*src
= nfs_fc2context(src_fc
), *ctx
;
1294 ctx
= kmemdup(src
, sizeof(struct nfs_fs_context
), GFP_KERNEL
);
1298 ctx
->mntfh
= nfs_alloc_fhandle();
1303 nfs_copy_fh(ctx
->mntfh
, src
->mntfh
);
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
;
1316 static void nfs_fs_context_free(struct fs_context
*fc
)
1318 struct nfs_fs_context
*ctx
= nfs_fc2context(fc
);
1322 nfs_free_server(ctx
->server
);
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
);
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
,
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.
1350 static int nfs_init_fs_context(struct fs_context
*fc
)
1352 struct nfs_fs_context
*ctx
;
1354 ctx
= kzalloc(sizeof(struct nfs_fs_context
), GFP_KERNEL
);
1358 ctx
->mntfh
= nfs_alloc_fhandle();
1359 if (unlikely(!ctx
->mntfh
)) {
1364 ctx
->protofamily
= AF_UNSPEC
;
1365 ctx
->mountfamily
= AF_UNSPEC
;
1366 ctx
->mount_server
.port
= NFS_UNSPEC_PORT
;
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
;
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
;
1388 memcpy(&ctx
->nfs_server
.address
, &nfss
->nfs_client
->cl_addr
,
1389 ctx
->nfs_server
.addrlen
);
1391 if (fc
->net_ns
!= net
) {
1392 put_net(fc
->net_ns
);
1393 fc
->net_ns
= get_net(net
);
1396 ctx
->nfs_mod
= nfss
->nfs_client
->cl_nfs_mod
;
1397 __module_get(ctx
->nfs_mod
->owner
);
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;
1412 fc
->fs_private
= ctx
;
1413 fc
->ops
= &nfs_fs_context_ops
;
1417 struct file_system_type nfs_fs_type
= {
1418 .owner
= THIS_MODULE
,
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
,
1425 MODULE_ALIAS_FS("nfs");
1426 EXPORT_SYMBOL_GPL(nfs_fs_type
);
1428 #if IS_ENABLED(CONFIG_NFS_V4)
1429 struct file_system_type nfs4_fs_type
= {
1430 .owner
= THIS_MODULE
,
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
,
1437 MODULE_ALIAS_FS("nfs4");
1438 MODULE_ALIAS("nfs4");
1439 EXPORT_SYMBOL_GPL(nfs4_fs_type
);
1440 #endif /* CONFIG_NFS_V4 */