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/compat.h>
14 #include <linux/module.h>
16 #include <linux/fs_context.h>
17 #include <linux/fs_parser.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_mount.h>
20 #include <linux/nfs4_mount.h>
26 #define NFSDBG_FACILITY NFSDBG_MOUNT
28 #if IS_ENABLED(CONFIG_NFS_V3)
29 #define NFS_DEFAULT_VERSION 3
31 #define NFS_DEFAULT_VERSION 2
34 #define NFS_MAX_CONNECTIONS 16
100 static const struct constant_table nfs_param_enums_local_lock[] = {
101 { "all", Opt_local_lock_all },
102 { "flock", Opt_local_lock_flock },
103 { "posix", Opt_local_lock_posix },
104 { "none", Opt_local_lock_none },
110 Opt_lookupcache_none,
111 Opt_lookupcache_positive,
114 static const struct constant_table nfs_param_enums_lookupcache[] = {
115 { "all", Opt_lookupcache_all },
116 { "none", Opt_lookupcache_none },
117 { "pos", Opt_lookupcache_positive },
118 { "positive", Opt_lookupcache_positive },
128 static const struct constant_table nfs_param_enums_write[] = {
129 { "lazy", Opt_write_lazy },
130 { "eager", Opt_write_eager },
131 { "wait", Opt_write_wait },
135 static const struct fs_parameter_spec nfs_fs_parameters[] = {
136 fsparam_flag_no("ac", Opt_ac),
137 fsparam_u32 ("acdirmax", Opt_acdirmax),
138 fsparam_u32 ("acdirmin", Opt_acdirmin),
139 fsparam_flag_no("acl", Opt_acl),
140 fsparam_u32 ("acregmax", Opt_acregmax),
141 fsparam_u32 ("acregmin", Opt_acregmin),
142 fsparam_u32 ("actimeo", Opt_actimeo),
143 fsparam_string("addr", Opt_addr),
144 fsparam_flag ("bg", Opt_bg),
145 fsparam_u32 ("bsize", Opt_bsize),
146 fsparam_string("clientaddr", Opt_clientaddr),
147 fsparam_flag_no("cto", Opt_cto),
148 fsparam_flag ("fg", Opt_fg),
149 fsparam_flag_no("fsc", Opt_fscache_flag),
150 fsparam_string("fsc", Opt_fscache),
151 fsparam_flag ("hard", Opt_hard),
152 __fsparam(NULL, "intr", Opt_intr,
153 fs_param_neg_with_no|fs_param_deprecated, NULL),
154 fsparam_enum ("local_lock", Opt_local_lock, nfs_param_enums_local_lock),
155 fsparam_flag_no("lock", Opt_lock),
156 fsparam_enum ("lookupcache", Opt_lookupcache, nfs_param_enums_lookupcache),
157 fsparam_flag_no("migration", Opt_migration),
158 fsparam_u32 ("minorversion", Opt_minorversion),
159 fsparam_string("mountaddr", Opt_mountaddr),
160 fsparam_string("mounthost", Opt_mounthost),
161 fsparam_u32 ("mountport", Opt_mountport),
162 fsparam_string("mountproto", Opt_mountproto),
163 fsparam_u32 ("mountvers", Opt_mountvers),
164 fsparam_u32 ("namlen", Opt_namelen),
165 fsparam_u32 ("nconnect", Opt_nconnect),
166 fsparam_u32 ("max_connect", Opt_max_connect),
167 fsparam_string("nfsvers", Opt_vers),
168 fsparam_u32 ("port", Opt_port),
169 fsparam_flag_no("posix", Opt_posix),
170 fsparam_string("proto", Opt_proto),
171 fsparam_flag_no("rdirplus", Opt_rdirplus),
172 fsparam_flag ("rdma", Opt_rdma),
173 fsparam_flag_no("resvport", Opt_resvport),
174 fsparam_u32 ("retrans", Opt_retrans),
175 fsparam_string("retry", Opt_retry),
176 fsparam_u32 ("rsize", Opt_rsize),
177 fsparam_string("sec", Opt_sec),
178 fsparam_flag_no("sharecache", Opt_sharecache),
179 fsparam_flag ("sloppy", Opt_sloppy),
180 fsparam_flag ("soft", Opt_soft),
181 fsparam_flag ("softerr", Opt_softerr),
182 fsparam_flag ("softreval", Opt_softreval),
183 fsparam_string("source", Opt_source),
184 fsparam_flag ("tcp", Opt_tcp),
185 fsparam_u32 ("timeo", Opt_timeo),
186 fsparam_flag_no("trunkdiscovery", Opt_trunkdiscovery),
187 fsparam_flag ("udp", Opt_udp),
188 fsparam_flag ("v2", Opt_v),
189 fsparam_flag ("v3", Opt_v),
190 fsparam_flag ("v4", Opt_v),
191 fsparam_flag ("v4.0", Opt_v),
192 fsparam_flag ("v4.1", Opt_v),
193 fsparam_flag ("v4.2", Opt_v),
194 fsparam_string("vers", Opt_vers),
195 fsparam_enum ("write", Opt_write, nfs_param_enums_write),
196 fsparam_u32 ("wsize", Opt_wsize),
209 static const struct constant_table nfs_vers_tokens[] = {
213 { "4.0", Opt_vers_4_0 },
214 { "4.1", Opt_vers_4_1 },
215 { "4.2", Opt_vers_4_2 },
229 static const struct constant_table nfs_xprt_protocol_tokens[] = {
230 { "rdma", Opt_xprt_rdma },
231 { "rdma6", Opt_xprt_rdma6 },
232 { "tcp", Opt_xprt_tcp },
233 { "tcp6", Opt_xprt_tcp6 },
234 { "udp", Opt_xprt_udp },
235 { "udp6", Opt_xprt_udp6 },
254 static const struct constant_table nfs_secflavor_tokens[] = {
255 { "krb5", Opt_sec_krb5 },
256 { "krb5i", Opt_sec_krb5i },
257 { "krb5p", Opt_sec_krb5p },
258 { "lkey", Opt_sec_lkey },
259 { "lkeyi", Opt_sec_lkeyi },
260 { "lkeyp", Opt_sec_lkeyp },
261 { "none", Opt_sec_none },
262 { "null", Opt_sec_none },
263 { "spkm3", Opt_sec_spkm },
264 { "spkm3i", Opt_sec_spkmi },
265 { "spkm3p", Opt_sec_spkmp },
266 { "sys", Opt_sec_sys },
271 * Sanity-check a server address provided by the mount command.
273 * Address family must be initialized, and address must not be
274 * the ANY address for that family.
276 static int nfs_verify_server_address(struct sockaddr_storage *addr)
278 switch (addr->ss_family) {
280 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
281 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
284 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
285 return !ipv6_addr_any(sa);
292 #ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
293 static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
298 static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
300 if (ctx->version == 4)
307 * Sanity check the NFS transport protocol.
309 static int nfs_validate_transport_protocol(struct fs_context *fc,
310 struct nfs_fs_context *ctx)
312 switch (ctx->nfs_server.protocol) {
313 case XPRT_TRANSPORT_UDP:
314 if (nfs_server_transport_udp_invalid(ctx))
315 goto out_invalid_transport_udp;
317 case XPRT_TRANSPORT_TCP:
318 case XPRT_TRANSPORT_RDMA:
321 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
324 out_invalid_transport_udp:
325 return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
329 * For text based NFSv2/v3 mounts, the mount protocol transport default
330 * settings should depend upon the specified NFS transport.
332 static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
334 if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
335 ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
337 switch (ctx->nfs_server.protocol) {
338 case XPRT_TRANSPORT_UDP:
339 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
341 case XPRT_TRANSPORT_TCP:
342 case XPRT_TRANSPORT_RDMA:
343 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
348 * Add 'flavor' to 'auth_info' if not already present.
349 * Returns true if 'flavor' ends up in the list, false otherwise
351 static int nfs_auth_info_add(struct fs_context *fc,
352 struct nfs_auth_info *auth_info,
353 rpc_authflavor_t flavor)
356 unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
358 /* make sure this flavor isn't already in the list */
359 for (i = 0; i < auth_info->flavor_len; i++) {
360 if (flavor == auth_info->flavors[i])
364 if (auth_info->flavor_len + 1 >= max_flavor_len)
365 return nfs_invalf(fc, "NFS: too many sec= flavors");
367 auth_info->flavors[auth_info->flavor_len++] = flavor;
372 * Parse the value of the 'sec=' option.
374 static int nfs_parse_security_flavors(struct fs_context *fc,
375 struct fs_parameter *param)
377 struct nfs_fs_context *ctx = nfs_fc2context(fc);
378 rpc_authflavor_t pseudoflavor;
379 char *string = param->string, *p;
382 trace_nfs_mount_assign(param->key, string);
384 while ((p = strsep(&string, ":")) != NULL) {
387 switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
389 pseudoflavor = RPC_AUTH_NULL;
392 pseudoflavor = RPC_AUTH_UNIX;
395 pseudoflavor = RPC_AUTH_GSS_KRB5;
398 pseudoflavor = RPC_AUTH_GSS_KRB5I;
401 pseudoflavor = RPC_AUTH_GSS_KRB5P;
404 pseudoflavor = RPC_AUTH_GSS_LKEY;
407 pseudoflavor = RPC_AUTH_GSS_LKEYI;
410 pseudoflavor = RPC_AUTH_GSS_LKEYP;
413 pseudoflavor = RPC_AUTH_GSS_SPKM;
416 pseudoflavor = RPC_AUTH_GSS_SPKMI;
419 pseudoflavor = RPC_AUTH_GSS_SPKMP;
422 return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
425 ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
433 static int nfs_parse_version_string(struct fs_context *fc,
436 struct nfs_fs_context *ctx = nfs_fc2context(fc);
438 ctx->flags &= ~NFS_MOUNT_VER3;
439 switch (lookup_constant(nfs_vers_tokens, string, -1)) {
444 ctx->flags |= NFS_MOUNT_VER3;
448 /* Backward compatibility option. In future,
449 * the mount program should always supply
450 * a NFSv4 minor version number.
456 ctx->minorversion = 0;
460 ctx->minorversion = 1;
464 ctx->minorversion = 2;
467 return nfs_invalf(fc, "NFS: Unsupported NFS version");
473 * Parse a single mount parameter.
475 static int nfs_fs_context_parse_param(struct fs_context *fc,
476 struct fs_parameter *param)
478 struct fs_parse_result result;
479 struct nfs_fs_context *ctx = nfs_fc2context(fc);
480 unsigned short protofamily, mountfamily;
484 trace_nfs_mount_option(param);
486 opt = fs_parse(fc, nfs_fs_parameters, param, &result);
488 return (opt == -ENOPARAM && ctx->sloppy) ? 1 : opt;
491 ctx->has_sec_mnt_opts = 1;
496 return nfs_invalf(fc, "NFS: Multiple sources not supported");
497 fc->source = param->string;
498 param->string = NULL;
502 * boolean options: foo/nofoo
505 ctx->flags |= NFS_MOUNT_SOFT;
506 ctx->flags &= ~NFS_MOUNT_SOFTERR;
509 ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
510 ctx->flags &= ~NFS_MOUNT_SOFT;
513 ctx->flags &= ~(NFS_MOUNT_SOFT |
515 NFS_MOUNT_SOFTREVAL);
519 ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
521 ctx->flags |= NFS_MOUNT_SOFTREVAL;
525 ctx->flags &= ~NFS_MOUNT_POSIX;
527 ctx->flags |= NFS_MOUNT_POSIX;
531 ctx->flags |= NFS_MOUNT_NOCTO;
533 ctx->flags &= ~NFS_MOUNT_NOCTO;
535 case Opt_trunkdiscovery:
537 ctx->flags &= ~NFS_MOUNT_TRUNK_DISCOVERY;
539 ctx->flags |= NFS_MOUNT_TRUNK_DISCOVERY;
543 ctx->flags |= NFS_MOUNT_NOAC;
545 ctx->flags &= ~NFS_MOUNT_NOAC;
548 if (result.negated) {
549 ctx->flags |= NFS_MOUNT_NONLM;
550 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
552 ctx->flags &= ~NFS_MOUNT_NONLM;
553 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
557 ctx->flags &= ~NFS_MOUNT_TCP;
558 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
562 ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
563 ret = xprt_find_transport_ident(param->key);
565 goto out_bad_transport;
566 ctx->nfs_server.protocol = ret;
570 ctx->flags |= NFS_MOUNT_NOACL;
572 ctx->flags &= ~NFS_MOUNT_NOACL;
576 ctx->flags |= NFS_MOUNT_NORDIRPLUS;
578 ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
582 ctx->flags |= NFS_MOUNT_UNSHARED;
584 ctx->flags &= ~NFS_MOUNT_UNSHARED;
588 ctx->flags |= NFS_MOUNT_NORESVPORT;
590 ctx->flags &= ~NFS_MOUNT_NORESVPORT;
592 case Opt_fscache_flag:
594 ctx->options &= ~NFS_OPTION_FSCACHE;
596 ctx->options |= NFS_OPTION_FSCACHE;
597 kfree(ctx->fscache_uniq);
598 ctx->fscache_uniq = NULL;
601 ctx->options |= NFS_OPTION_FSCACHE;
602 kfree(ctx->fscache_uniq);
603 ctx->fscache_uniq = param->string;
604 param->string = NULL;
608 ctx->options &= ~NFS_OPTION_MIGRATION;
610 ctx->options |= NFS_OPTION_MIGRATION;
614 * options that take numeric values
617 if (result.uint_32 > USHRT_MAX)
619 ctx->nfs_server.port = result.uint_32;
622 ctx->rsize = result.uint_32;
625 ctx->wsize = result.uint_32;
628 ctx->bsize = result.uint_32;
631 if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
633 ctx->timeo = result.uint_32;
636 if (result.uint_32 > INT_MAX)
638 ctx->retrans = result.uint_32;
641 ctx->acregmin = result.uint_32;
644 ctx->acregmax = result.uint_32;
647 ctx->acdirmin = result.uint_32;
650 ctx->acdirmax = result.uint_32;
653 ctx->acregmin = result.uint_32;
654 ctx->acregmax = result.uint_32;
655 ctx->acdirmin = result.uint_32;
656 ctx->acdirmax = result.uint_32;
659 ctx->namlen = result.uint_32;
662 if (result.uint_32 > USHRT_MAX)
664 ctx->mount_server.port = result.uint_32;
667 if (result.uint_32 < NFS_MNT_VERSION ||
668 result.uint_32 > NFS_MNT3_VERSION)
670 ctx->mount_server.version = result.uint_32;
672 case Opt_minorversion:
673 if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
675 ctx->minorversion = result.uint_32;
679 * options that take text values
682 ret = nfs_parse_version_string(fc, param->key + 1);
688 goto out_invalid_value;
689 trace_nfs_mount_assign(param->key, param->string);
690 ret = nfs_parse_version_string(fc, param->string);
695 ret = nfs_parse_security_flavors(fc, param);
702 goto out_invalid_value;
703 trace_nfs_mount_assign(param->key, param->string);
704 protofamily = AF_INET;
705 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
707 protofamily = AF_INET6;
710 ctx->flags &= ~NFS_MOUNT_TCP;
711 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
714 protofamily = AF_INET6;
717 ctx->flags |= NFS_MOUNT_TCP;
718 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
721 protofamily = AF_INET6;
724 /* vector side protocols to TCP */
725 ctx->flags |= NFS_MOUNT_TCP;
726 ret = xprt_find_transport_ident(param->string);
728 goto out_bad_transport;
729 ctx->nfs_server.protocol = ret;
732 goto out_bad_transport;
735 ctx->protofamily = protofamily;
740 goto out_invalid_value;
741 trace_nfs_mount_assign(param->key, param->string);
742 mountfamily = AF_INET;
743 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
745 mountfamily = AF_INET6;
748 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
751 mountfamily = AF_INET6;
754 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
756 case Opt_xprt_rdma: /* not used for side protocols */
758 goto out_bad_transport;
760 ctx->mountfamily = mountfamily;
764 trace_nfs_mount_assign(param->key, param->string);
765 len = rpc_pton(fc->net_ns, param->string, param->size,
766 &ctx->nfs_server.address,
767 sizeof(ctx->nfs_server._address));
769 goto out_invalid_address;
770 ctx->nfs_server.addrlen = len;
773 trace_nfs_mount_assign(param->key, param->string);
774 kfree(ctx->client_address);
775 ctx->client_address = param->string;
776 param->string = NULL;
779 trace_nfs_mount_assign(param->key, param->string);
780 kfree(ctx->mount_server.hostname);
781 ctx->mount_server.hostname = param->string;
782 param->string = NULL;
785 trace_nfs_mount_assign(param->key, param->string);
786 len = rpc_pton(fc->net_ns, param->string, param->size,
787 &ctx->mount_server.address,
788 sizeof(ctx->mount_server._address));
790 goto out_invalid_address;
791 ctx->mount_server.addrlen = len;
794 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
796 ctx->nfs_server.nconnect = result.uint_32;
798 case Opt_max_connect:
799 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_TRANSPORTS)
801 ctx->nfs_server.max_connect = result.uint_32;
803 case Opt_lookupcache:
804 switch (result.uint_32) {
805 case Opt_lookupcache_all:
806 ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
808 case Opt_lookupcache_positive:
809 ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
810 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
812 case Opt_lookupcache_none:
813 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
816 goto out_invalid_value;
820 switch (result.uint_32) {
821 case Opt_local_lock_all:
822 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
823 NFS_MOUNT_LOCAL_FCNTL);
825 case Opt_local_lock_flock:
826 ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
828 case Opt_local_lock_posix:
829 ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
831 case Opt_local_lock_none:
832 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
833 NFS_MOUNT_LOCAL_FCNTL);
836 goto out_invalid_value;
840 switch (result.uint_32) {
843 ~(NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT);
845 case Opt_write_eager:
846 ctx->flags |= NFS_MOUNT_WRITE_EAGER;
847 ctx->flags &= ~NFS_MOUNT_WRITE_WAIT;
851 NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT;
854 goto out_invalid_value;
869 return nfs_invalf(fc, "NFS: Bad mount option value specified");
871 return nfs_invalf(fc, "NFS: Bad IP address specified");
873 return nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
875 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
879 * Split fc->source into "hostname:export_path".
881 * The leftmost colon demarks the split between the server's hostname
882 * and the export path. If the hostname starts with a left square
883 * bracket, then it may contain colons.
885 * Note: caller frees hostname and export path, even on error.
887 static int nfs_parse_source(struct fs_context *fc,
888 size_t maxnamlen, size_t maxpathlen)
890 struct nfs_fs_context *ctx = nfs_fc2context(fc);
891 const char *dev_name = fc->source;
895 if (unlikely(!dev_name || !*dev_name))
898 /* Is the host name protected with square brakcets? */
899 if (*dev_name == '[') {
900 end = strchr(++dev_name, ']');
901 if (end == NULL || end[1] != ':')
902 goto out_bad_devname;
904 len = end - dev_name;
909 end = strchr(dev_name, ':');
911 goto out_bad_devname;
912 len = end - dev_name;
914 /* kill possible hostname list: not supported */
915 comma = memchr(dev_name, ',', len);
917 len = comma - dev_name;
923 kfree(ctx->nfs_server.hostname);
925 /* N.B. caller will free nfs_server.hostname in all cases */
926 ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
927 if (!ctx->nfs_server.hostname)
930 if (len > maxpathlen)
932 ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
933 if (!ctx->nfs_server.export_path)
936 trace_nfs_mount_path(ctx->nfs_server.export_path);
940 return nfs_invalf(fc, "NFS: device name not in host:path format");
942 nfs_errorf(fc, "NFS: not enough memory to parse device name");
945 nfs_errorf(fc, "NFS: server hostname too long");
946 return -ENAMETOOLONG;
948 nfs_errorf(fc, "NFS: export pathname too long");
949 return -ENAMETOOLONG;
952 static inline bool is_remount_fc(struct fs_context *fc)
954 return fc->root != NULL;
958 * Parse monolithic NFS2/NFS3 mount data
959 * - fills in the mount root filehandle
961 * For option strings, user space handles the following behaviors:
963 * + DNS: mapping server host name to IP address ("addr=" option)
965 * + failure mode: how to behave if a mount request can't be handled
966 * immediately ("fg/bg" option)
968 * + retry: how often to retry a mount request ("retry=" option)
970 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
971 * mountproto=tcp after mountproto=udp, and so on
973 static int nfs23_parse_monolithic(struct fs_context *fc,
974 struct nfs_mount_data *data)
976 struct nfs_fs_context *ctx = nfs_fc2context(fc);
977 struct nfs_fh *mntfh = ctx->mntfh;
978 struct sockaddr_storage *sap = &ctx->nfs_server._address;
979 int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
985 ctx->version = NFS_DEFAULT_VERSION;
986 switch (data->version) {
994 if (data->flags & NFS_MOUNT_VER3)
996 data->root.size = NFS2_FHSIZE;
997 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
998 /* Turn off security negotiation */
999 extra_flags |= NFS_MOUNT_SECFLAVOUR;
1002 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1006 memset(data->context, 0, sizeof(data->context));
1009 if (data->flags & NFS_MOUNT_VER3) {
1010 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1011 goto out_invalid_fh;
1012 mntfh->size = data->root.size;
1015 mntfh->size = NFS2_FHSIZE;
1020 memcpy(mntfh->data, data->root.data, mntfh->size);
1021 if (mntfh->size < sizeof(mntfh->data))
1022 memset(mntfh->data + mntfh->size, 0,
1023 sizeof(mntfh->data) - mntfh->size);
1026 * for proto == XPRT_TRANSPORT_UDP, which is what uses
1027 * to_exponential, implying shift: limit the shift value
1028 * to BITS_PER_LONG (majortimeo is unsigned long)
1030 if (!(data->flags & NFS_MOUNT_TCP)) /* this will be UDP */
1031 if (data->retrans >= 64) /* shift value is too large */
1032 goto out_invalid_data;
1035 * Translate to nfs_fs_context, which nfs_fill_super
1038 ctx->flags = data->flags & NFS_MOUNT_FLAGMASK;
1039 ctx->flags |= extra_flags;
1040 ctx->rsize = data->rsize;
1041 ctx->wsize = data->wsize;
1042 ctx->timeo = data->timeo;
1043 ctx->retrans = data->retrans;
1044 ctx->acregmin = data->acregmin;
1045 ctx->acregmax = data->acregmax;
1046 ctx->acdirmin = data->acdirmin;
1047 ctx->acdirmax = data->acdirmax;
1048 ctx->need_mount = false;
1050 memcpy(sap, &data->addr, sizeof(data->addr));
1051 ctx->nfs_server.addrlen = sizeof(data->addr);
1052 ctx->nfs_server.port = ntohs(data->addr.sin_port);
1053 if (sap->ss_family != AF_INET ||
1054 !nfs_verify_server_address(sap))
1055 goto out_no_address;
1057 if (!(data->flags & NFS_MOUNT_TCP))
1058 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1059 /* N.B. caller will free nfs_server.hostname in all cases */
1060 ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1061 if (!ctx->nfs_server.hostname)
1064 ctx->namlen = data->namlen;
1065 ctx->bsize = data->bsize;
1067 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1068 ctx->selected_flavor = data->pseudoflavor;
1070 ctx->selected_flavor = RPC_AUTH_UNIX;
1072 if (!(data->flags & NFS_MOUNT_NONLM))
1073 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1074 NFS_MOUNT_LOCAL_FCNTL);
1076 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1077 NFS_MOUNT_LOCAL_FCNTL);
1080 * The legacy version 6 binary mount data from userspace has a
1081 * field used only to transport selinux information into the
1082 * kernel. To continue to support that functionality we
1083 * have a touch of selinux knowledge here in the NFS code. The
1084 * userspace code converted context=blah to just blah so we are
1085 * converting back to the full string selinux understands.
1087 if (data->context[0]){
1088 #ifdef CONFIG_SECURITY_SELINUX
1091 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1092 ret = vfs_parse_fs_string(fc, "context",
1093 data->context, strlen(data->context));
1106 ret = nfs_validate_transport_protocol(fc, ctx);
1110 ctx->skip_reconfig_option_check = true;
1114 return generic_parse_monolithic(fc, data);
1117 if (is_remount_fc(fc)) {
1118 ctx->skip_reconfig_option_check = true;
1121 return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
1124 return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
1127 return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
1133 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1136 return nfs_invalf(fc, "NFS: invalid root filehandle");
1139 return nfs_invalf(fc, "NFS: invalid binary mount data");
1142 #if IS_ENABLED(CONFIG_NFS_V4)
1143 struct compat_nfs_string {
1148 static inline void compat_nfs_string(struct nfs_string *dst,
1149 struct compat_nfs_string *src)
1151 dst->data = compat_ptr(src->data);
1152 dst->len = src->len;
1155 struct compat_nfs4_mount_data_v1 {
1156 compat_int_t version;
1161 compat_int_t retrans;
1162 compat_int_t acregmin;
1163 compat_int_t acregmax;
1164 compat_int_t acdirmin;
1165 compat_int_t acdirmax;
1166 struct compat_nfs_string client_addr;
1167 struct compat_nfs_string mnt_path;
1168 struct compat_nfs_string hostname;
1169 compat_uint_t host_addrlen;
1170 compat_uptr_t host_addr;
1172 compat_int_t auth_flavourlen;
1173 compat_uptr_t auth_flavours;
1176 static void nfs4_compat_mount_data_conv(struct nfs4_mount_data *data)
1178 struct compat_nfs4_mount_data_v1 *compat =
1179 (struct compat_nfs4_mount_data_v1 *)data;
1181 /* copy the fields backwards */
1182 data->auth_flavours = compat_ptr(compat->auth_flavours);
1183 data->auth_flavourlen = compat->auth_flavourlen;
1184 data->proto = compat->proto;
1185 data->host_addr = compat_ptr(compat->host_addr);
1186 data->host_addrlen = compat->host_addrlen;
1187 compat_nfs_string(&data->hostname, &compat->hostname);
1188 compat_nfs_string(&data->mnt_path, &compat->mnt_path);
1189 compat_nfs_string(&data->client_addr, &compat->client_addr);
1190 data->acdirmax = compat->acdirmax;
1191 data->acdirmin = compat->acdirmin;
1192 data->acregmax = compat->acregmax;
1193 data->acregmin = compat->acregmin;
1194 data->retrans = compat->retrans;
1195 data->timeo = compat->timeo;
1196 data->wsize = compat->wsize;
1197 data->rsize = compat->rsize;
1198 data->flags = compat->flags;
1199 data->version = compat->version;
1203 * Validate NFSv4 mount options
1205 static int nfs4_parse_monolithic(struct fs_context *fc,
1206 struct nfs4_mount_data *data)
1208 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1209 struct sockaddr_storage *sap = &ctx->nfs_server._address;
1214 if (is_remount_fc(fc))
1216 return nfs_invalf(fc,
1217 "NFS4: mount program didn't pass any mount data");
1222 if (data->version != 1)
1223 return generic_parse_monolithic(fc, data);
1225 if (in_compat_syscall())
1226 nfs4_compat_mount_data_conv(data);
1228 if (data->host_addrlen > sizeof(ctx->nfs_server.address))
1229 goto out_no_address;
1230 if (data->host_addrlen == 0)
1231 goto out_no_address;
1232 ctx->nfs_server.addrlen = data->host_addrlen;
1233 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1235 if (!nfs_verify_server_address(sap))
1236 goto out_no_address;
1237 ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1239 if (data->auth_flavourlen) {
1240 rpc_authflavor_t pseudoflavor;
1242 if (data->auth_flavourlen > 1)
1243 goto out_inval_auth;
1244 if (copy_from_user(&pseudoflavor, data->auth_flavours,
1245 sizeof(pseudoflavor)))
1247 ctx->selected_flavor = pseudoflavor;
1249 ctx->selected_flavor = RPC_AUTH_UNIX;
1252 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1255 ctx->nfs_server.hostname = c;
1257 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1260 ctx->nfs_server.export_path = c;
1261 trace_nfs_mount_path(c);
1263 c = strndup_user(data->client_addr.data, 16);
1266 ctx->client_address = c;
1269 * Translate to nfs_fs_context, which nfs_fill_super
1273 ctx->flags = data->flags & NFS4_MOUNT_FLAGMASK;
1274 ctx->rsize = data->rsize;
1275 ctx->wsize = data->wsize;
1276 ctx->timeo = data->timeo;
1277 ctx->retrans = data->retrans;
1278 ctx->acregmin = data->acregmin;
1279 ctx->acregmax = data->acregmax;
1280 ctx->acdirmin = data->acdirmin;
1281 ctx->acdirmax = data->acdirmax;
1282 ctx->nfs_server.protocol = data->proto;
1283 ret = nfs_validate_transport_protocol(fc, ctx);
1287 ctx->skip_reconfig_option_check = true;
1291 return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1292 data->auth_flavourlen);
1295 return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
1300 * Parse a monolithic block of data from sys_mount().
1302 static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1305 if (fc->fs_type == &nfs_fs_type)
1306 return nfs23_parse_monolithic(fc, data);
1308 #if IS_ENABLED(CONFIG_NFS_V4)
1309 if (fc->fs_type == &nfs4_fs_type)
1310 return nfs4_parse_monolithic(fc, data);
1313 return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
1317 * Validate the preparsed information in the config.
1319 static int nfs_fs_context_validate(struct fs_context *fc)
1321 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1322 struct nfs_subversion *nfs_mod;
1323 struct sockaddr_storage *sap = &ctx->nfs_server._address;
1324 int max_namelen = PAGE_SIZE;
1325 int max_pathlen = NFS_MAXPATHLEN;
1330 goto out_no_device_name;
1332 /* Check for sanity first. */
1333 if (ctx->minorversion && ctx->version != 4)
1334 goto out_minorversion_mismatch;
1336 if (ctx->options & NFS_OPTION_MIGRATION &&
1337 (ctx->version != 4 || ctx->minorversion != 0))
1338 goto out_migration_misuse;
1340 /* Verify that any proto=/mountproto= options match the address
1341 * families in the addr=/mountaddr= options.
1343 if (ctx->protofamily != AF_UNSPEC &&
1344 ctx->protofamily != ctx->nfs_server.address.sa_family)
1345 goto out_proto_mismatch;
1347 if (ctx->mountfamily != AF_UNSPEC) {
1348 if (ctx->mount_server.addrlen) {
1349 if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1350 goto out_mountproto_mismatch;
1352 if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1353 goto out_mountproto_mismatch;
1357 if (!nfs_verify_server_address(sap))
1358 goto out_no_address;
1360 ret = nfs_validate_transport_protocol(fc, ctx);
1364 if (ctx->version == 4) {
1365 if (IS_ENABLED(CONFIG_NFS_V4)) {
1366 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1367 port = NFS_RDMA_PORT;
1370 max_namelen = NFS4_MAXNAMLEN;
1371 max_pathlen = NFS4_MAXPATHLEN;
1372 ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1373 NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1374 NFS_MOUNT_LOCAL_FCNTL);
1376 goto out_v4_not_compiled;
1379 nfs_set_mount_transport_protocol(ctx);
1380 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1381 port = NFS_RDMA_PORT;
1384 nfs_set_port(sap, &ctx->nfs_server.port, port);
1386 ret = nfs_parse_source(fc, max_namelen, max_pathlen);
1390 /* Load the NFS protocol module if we haven't done so yet */
1391 if (!ctx->nfs_mod) {
1392 nfs_mod = get_nfs_version(ctx->version);
1393 if (IS_ERR(nfs_mod)) {
1394 ret = PTR_ERR(nfs_mod);
1395 goto out_version_unavailable;
1397 ctx->nfs_mod = nfs_mod;
1400 /* Ensure the filesystem context has the correct fs_type */
1401 if (fc->fs_type != ctx->nfs_mod->nfs_fs) {
1402 module_put(fc->fs_type->owner);
1403 __module_get(ctx->nfs_mod->nfs_fs->owner);
1404 fc->fs_type = ctx->nfs_mod->nfs_fs;
1409 return nfs_invalf(fc, "NFS: Device name not specified");
1410 out_v4_not_compiled:
1411 nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
1412 return -EPROTONOSUPPORT;
1414 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1415 out_mountproto_mismatch:
1416 return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
1418 return nfs_invalf(fc, "NFS: Server address does not match proto= option");
1419 out_minorversion_mismatch:
1420 return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
1421 ctx->version, ctx->minorversion);
1422 out_migration_misuse:
1423 return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
1424 out_version_unavailable:
1425 nfs_errorf(fc, "NFS: Version unavailable");
1430 * Create an NFS superblock by the appropriate method.
1432 static int nfs_get_tree(struct fs_context *fc)
1434 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1435 int err = nfs_fs_context_validate(fc);
1440 return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
1442 return nfs_get_tree_common(fc);
1446 * Handle duplication of a configuration. The caller copied *src into *sc, but
1447 * it can't deal with resource pointers in the filesystem context, so we have
1448 * to do that. We need to clear pointers, copy data or get extra refs as
1451 static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1453 struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1455 ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1459 ctx->mntfh = nfs_alloc_fhandle();
1464 nfs_copy_fh(ctx->mntfh, src->mntfh);
1466 __module_get(ctx->nfs_mod->owner);
1467 ctx->client_address = NULL;
1468 ctx->mount_server.hostname = NULL;
1469 ctx->nfs_server.export_path = NULL;
1470 ctx->nfs_server.hostname = NULL;
1471 ctx->fscache_uniq = NULL;
1472 ctx->clone_data.fattr = NULL;
1473 fc->fs_private = ctx;
1477 static void nfs_fs_context_free(struct fs_context *fc)
1479 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1483 nfs_free_server(ctx->server);
1485 put_nfs_version(ctx->nfs_mod);
1486 kfree(ctx->client_address);
1487 kfree(ctx->mount_server.hostname);
1488 kfree(ctx->nfs_server.export_path);
1489 kfree(ctx->nfs_server.hostname);
1490 kfree(ctx->fscache_uniq);
1491 nfs_free_fhandle(ctx->mntfh);
1492 nfs_free_fattr(ctx->clone_data.fattr);
1497 static const struct fs_context_operations nfs_fs_context_ops = {
1498 .free = nfs_fs_context_free,
1499 .dup = nfs_fs_context_dup,
1500 .parse_param = nfs_fs_context_parse_param,
1501 .parse_monolithic = nfs_fs_context_parse_monolithic,
1502 .get_tree = nfs_get_tree,
1503 .reconfigure = nfs_reconfigure,
1507 * Prepare superblock configuration. We use the namespaces attached to the
1508 * context. This may be the current process's namespaces, or it may be a
1509 * container's namespaces.
1511 static int nfs_init_fs_context(struct fs_context *fc)
1513 struct nfs_fs_context *ctx;
1515 ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1519 ctx->mntfh = nfs_alloc_fhandle();
1520 if (unlikely(!ctx->mntfh)) {
1525 ctx->protofamily = AF_UNSPEC;
1526 ctx->mountfamily = AF_UNSPEC;
1527 ctx->mount_server.port = NFS_UNSPEC_PORT;
1530 /* reconfigure, start with the current config */
1531 struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1532 struct net *net = nfss->nfs_client->cl_net;
1534 ctx->flags = nfss->flags;
1535 ctx->rsize = nfss->rsize;
1536 ctx->wsize = nfss->wsize;
1537 ctx->retrans = nfss->client->cl_timeout->to_retries;
1538 ctx->selected_flavor = nfss->client->cl_auth->au_flavor;
1539 ctx->acregmin = nfss->acregmin / HZ;
1540 ctx->acregmax = nfss->acregmax / HZ;
1541 ctx->acdirmin = nfss->acdirmin / HZ;
1542 ctx->acdirmax = nfss->acdirmax / HZ;
1543 ctx->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1544 ctx->nfs_server.port = nfss->port;
1545 ctx->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1546 ctx->version = nfss->nfs_client->rpc_ops->version;
1547 ctx->minorversion = nfss->nfs_client->cl_minorversion;
1549 memcpy(&ctx->nfs_server._address, &nfss->nfs_client->cl_addr,
1550 ctx->nfs_server.addrlen);
1552 if (fc->net_ns != net) {
1553 put_net(fc->net_ns);
1554 fc->net_ns = get_net(net);
1557 ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1558 __module_get(ctx->nfs_mod->owner);
1561 ctx->timeo = NFS_UNSPEC_TIMEO;
1562 ctx->retrans = NFS_UNSPEC_RETRANS;
1563 ctx->acregmin = NFS_DEF_ACREGMIN;
1564 ctx->acregmax = NFS_DEF_ACREGMAX;
1565 ctx->acdirmin = NFS_DEF_ACDIRMIN;
1566 ctx->acdirmax = NFS_DEF_ACDIRMAX;
1567 ctx->nfs_server.port = NFS_UNSPEC_PORT;
1568 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1569 ctx->selected_flavor = RPC_AUTH_MAXFLAVOR;
1570 ctx->minorversion = 0;
1571 ctx->need_mount = true;
1573 fc->s_iflags |= SB_I_STABLE_WRITES;
1575 fc->fs_private = ctx;
1576 fc->ops = &nfs_fs_context_ops;
1580 struct file_system_type nfs_fs_type = {
1581 .owner = THIS_MODULE,
1583 .init_fs_context = nfs_init_fs_context,
1584 .parameters = nfs_fs_parameters,
1585 .kill_sb = nfs_kill_super,
1586 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1588 MODULE_ALIAS_FS("nfs");
1589 EXPORT_SYMBOL_GPL(nfs_fs_type);
1591 #if IS_ENABLED(CONFIG_NFS_V4)
1592 struct file_system_type nfs4_fs_type = {
1593 .owner = THIS_MODULE,
1595 .init_fs_context = nfs_init_fs_context,
1596 .parameters = nfs_fs_parameters,
1597 .kill_sb = nfs_kill_super,
1598 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1600 MODULE_ALIAS_FS("nfs4");
1601 MODULE_ALIAS("nfs4");
1602 EXPORT_SYMBOL_GPL(nfs4_fs_type);
1603 #endif /* CONFIG_NFS_V4 */