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
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>
24 #define NFSDBG_FACILITY NFSDBG_MOUNT
26 #if IS_ENABLED(CONFIG_NFS_V3)
27 #define NFS_DEFAULT_VERSION 3
29 #define NFS_DEFAULT_VERSION 2
32 #define NFS_MAX_CONNECTIONS 16
98 static const struct constant_table nfs_param_enums_local_lock[] = {
99 { "all", Opt_local_lock_all },
100 { "flock", Opt_local_lock_flock },
101 { "posix", Opt_local_lock_posix },
102 { "none", Opt_local_lock_none },
108 Opt_lookupcache_none,
109 Opt_lookupcache_positive,
112 static const struct constant_table nfs_param_enums_lookupcache[] = {
113 { "all", Opt_lookupcache_all },
114 { "none", Opt_lookupcache_none },
115 { "pos", Opt_lookupcache_positive },
116 { "positive", Opt_lookupcache_positive },
126 static const struct constant_table nfs_param_enums_write[] = {
127 { "lazy", Opt_write_lazy },
128 { "eager", Opt_write_eager },
129 { "wait", Opt_write_wait },
133 static const struct fs_parameter_spec nfs_fs_parameters[] = {
134 fsparam_flag_no("ac", Opt_ac),
135 fsparam_u32 ("acdirmax", Opt_acdirmax),
136 fsparam_u32 ("acdirmin", Opt_acdirmin),
137 fsparam_flag_no("acl", Opt_acl),
138 fsparam_u32 ("acregmax", Opt_acregmax),
139 fsparam_u32 ("acregmin", Opt_acregmin),
140 fsparam_u32 ("actimeo", Opt_actimeo),
141 fsparam_string("addr", Opt_addr),
142 fsparam_flag ("bg", Opt_bg),
143 fsparam_u32 ("bsize", Opt_bsize),
144 fsparam_string("clientaddr", Opt_clientaddr),
145 fsparam_flag_no("cto", Opt_cto),
146 fsparam_flag ("fg", Opt_fg),
147 fsparam_flag_no("fsc", Opt_fscache_flag),
148 fsparam_string("fsc", Opt_fscache),
149 fsparam_flag ("hard", Opt_hard),
150 __fsparam(NULL, "intr", Opt_intr,
151 fs_param_neg_with_no|fs_param_deprecated, NULL),
152 fsparam_enum ("local_lock", Opt_local_lock, nfs_param_enums_local_lock),
153 fsparam_flag_no("lock", Opt_lock),
154 fsparam_enum ("lookupcache", Opt_lookupcache, nfs_param_enums_lookupcache),
155 fsparam_flag_no("migration", Opt_migration),
156 fsparam_u32 ("minorversion", Opt_minorversion),
157 fsparam_string("mountaddr", Opt_mountaddr),
158 fsparam_string("mounthost", Opt_mounthost),
159 fsparam_u32 ("mountport", Opt_mountport),
160 fsparam_string("mountproto", Opt_mountproto),
161 fsparam_u32 ("mountvers", Opt_mountvers),
162 fsparam_u32 ("namlen", Opt_namelen),
163 fsparam_u32 ("nconnect", Opt_nconnect),
164 fsparam_u32 ("max_connect", Opt_max_connect),
165 fsparam_string("nfsvers", Opt_vers),
166 fsparam_u32 ("port", Opt_port),
167 fsparam_flag_no("posix", Opt_posix),
168 fsparam_string("proto", Opt_proto),
169 fsparam_flag_no("rdirplus", Opt_rdirplus),
170 fsparam_flag ("rdma", Opt_rdma),
171 fsparam_flag_no("resvport", Opt_resvport),
172 fsparam_u32 ("retrans", Opt_retrans),
173 fsparam_string("retry", Opt_retry),
174 fsparam_u32 ("rsize", Opt_rsize),
175 fsparam_string("sec", Opt_sec),
176 fsparam_flag_no("sharecache", Opt_sharecache),
177 fsparam_flag ("sloppy", Opt_sloppy),
178 fsparam_flag ("soft", Opt_soft),
179 fsparam_flag ("softerr", Opt_softerr),
180 fsparam_flag ("softreval", Opt_softreval),
181 fsparam_string("source", Opt_source),
182 fsparam_flag ("tcp", Opt_tcp),
183 fsparam_u32 ("timeo", Opt_timeo),
184 fsparam_flag_no("trunkdiscovery", Opt_trunkdiscovery),
185 fsparam_flag ("udp", Opt_udp),
186 fsparam_flag ("v2", Opt_v),
187 fsparam_flag ("v3", Opt_v),
188 fsparam_flag ("v4", Opt_v),
189 fsparam_flag ("v4.0", Opt_v),
190 fsparam_flag ("v4.1", Opt_v),
191 fsparam_flag ("v4.2", Opt_v),
192 fsparam_string("vers", Opt_vers),
193 fsparam_enum ("write", Opt_write, nfs_param_enums_write),
194 fsparam_u32 ("wsize", Opt_wsize),
207 static const struct constant_table nfs_vers_tokens[] = {
211 { "4.0", Opt_vers_4_0 },
212 { "4.1", Opt_vers_4_1 },
213 { "4.2", Opt_vers_4_2 },
227 static const struct constant_table nfs_xprt_protocol_tokens[] = {
228 { "rdma", Opt_xprt_rdma },
229 { "rdma6", Opt_xprt_rdma6 },
230 { "tcp", Opt_xprt_tcp },
231 { "tcp6", Opt_xprt_tcp6 },
232 { "udp", Opt_xprt_udp },
233 { "udp6", Opt_xprt_udp6 },
252 static const struct constant_table nfs_secflavor_tokens[] = {
253 { "krb5", Opt_sec_krb5 },
254 { "krb5i", Opt_sec_krb5i },
255 { "krb5p", Opt_sec_krb5p },
256 { "lkey", Opt_sec_lkey },
257 { "lkeyi", Opt_sec_lkeyi },
258 { "lkeyp", Opt_sec_lkeyp },
259 { "none", Opt_sec_none },
260 { "null", Opt_sec_none },
261 { "spkm3", Opt_sec_spkm },
262 { "spkm3i", Opt_sec_spkmi },
263 { "spkm3p", Opt_sec_spkmp },
264 { "sys", Opt_sec_sys },
269 * Sanity-check a server address provided by the mount command.
271 * Address family must be initialized, and address must not be
272 * the ANY address for that family.
274 static int nfs_verify_server_address(struct sockaddr *addr)
276 switch (addr->sa_family) {
278 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
279 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
282 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
283 return !ipv6_addr_any(sa);
287 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
291 #ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
292 static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
297 static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
299 if (ctx->version == 4)
306 * Sanity check the NFS transport protocol.
308 static int nfs_validate_transport_protocol(struct fs_context *fc,
309 struct nfs_fs_context *ctx)
311 switch (ctx->nfs_server.protocol) {
312 case XPRT_TRANSPORT_UDP:
313 if (nfs_server_transport_udp_invalid(ctx))
314 goto out_invalid_transport_udp;
316 case XPRT_TRANSPORT_TCP:
317 case XPRT_TRANSPORT_RDMA:
320 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
323 out_invalid_transport_udp:
324 return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
328 * For text based NFSv2/v3 mounts, the mount protocol transport default
329 * settings should depend upon the specified NFS transport.
331 static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
333 if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
334 ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
336 switch (ctx->nfs_server.protocol) {
337 case XPRT_TRANSPORT_UDP:
338 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
340 case XPRT_TRANSPORT_TCP:
341 case XPRT_TRANSPORT_RDMA:
342 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
347 * Add 'flavor' to 'auth_info' if not already present.
348 * Returns true if 'flavor' ends up in the list, false otherwise
350 static int nfs_auth_info_add(struct fs_context *fc,
351 struct nfs_auth_info *auth_info,
352 rpc_authflavor_t flavor)
355 unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
357 /* make sure this flavor isn't already in the list */
358 for (i = 0; i < auth_info->flavor_len; i++) {
359 if (flavor == auth_info->flavors[i])
363 if (auth_info->flavor_len + 1 >= max_flavor_len)
364 return nfs_invalf(fc, "NFS: too many sec= flavors");
366 auth_info->flavors[auth_info->flavor_len++] = flavor;
371 * Parse the value of the 'sec=' option.
373 static int nfs_parse_security_flavors(struct fs_context *fc,
374 struct fs_parameter *param)
376 struct nfs_fs_context *ctx = nfs_fc2context(fc);
377 rpc_authflavor_t pseudoflavor;
378 char *string = param->string, *p;
381 dfprintk(MOUNT, "NFS: parsing %s=%s option\n", param->key, param->string);
383 while ((p = strsep(&string, ":")) != NULL) {
386 switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
388 pseudoflavor = RPC_AUTH_NULL;
391 pseudoflavor = RPC_AUTH_UNIX;
394 pseudoflavor = RPC_AUTH_GSS_KRB5;
397 pseudoflavor = RPC_AUTH_GSS_KRB5I;
400 pseudoflavor = RPC_AUTH_GSS_KRB5P;
403 pseudoflavor = RPC_AUTH_GSS_LKEY;
406 pseudoflavor = RPC_AUTH_GSS_LKEYI;
409 pseudoflavor = RPC_AUTH_GSS_LKEYP;
412 pseudoflavor = RPC_AUTH_GSS_SPKM;
415 pseudoflavor = RPC_AUTH_GSS_SPKMI;
418 pseudoflavor = RPC_AUTH_GSS_SPKMP;
421 return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
424 ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
432 static int nfs_parse_version_string(struct fs_context *fc,
435 struct nfs_fs_context *ctx = nfs_fc2context(fc);
437 ctx->flags &= ~NFS_MOUNT_VER3;
438 switch (lookup_constant(nfs_vers_tokens, string, -1)) {
443 ctx->flags |= NFS_MOUNT_VER3;
447 /* Backward compatibility option. In future,
448 * the mount program should always supply
449 * a NFSv4 minor version number.
455 ctx->minorversion = 0;
459 ctx->minorversion = 1;
463 ctx->minorversion = 2;
466 return nfs_invalf(fc, "NFS: Unsupported NFS version");
472 * Parse a single mount parameter.
474 static int nfs_fs_context_parse_param(struct fs_context *fc,
475 struct fs_parameter *param)
477 struct fs_parse_result result;
478 struct nfs_fs_context *ctx = nfs_fc2context(fc);
479 unsigned short protofamily, mountfamily;
483 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", param->key);
485 opt = fs_parse(fc, nfs_fs_parameters, param, &result);
487 return ctx->sloppy ? 1 : opt;
490 ctx->has_sec_mnt_opts = 1;
495 return nfs_invalf(fc, "NFS: Multiple sources not supported");
496 fc->source = param->string;
497 param->string = NULL;
501 * boolean options: foo/nofoo
504 ctx->flags |= NFS_MOUNT_SOFT;
505 ctx->flags &= ~NFS_MOUNT_SOFTERR;
508 ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
509 ctx->flags &= ~NFS_MOUNT_SOFT;
512 ctx->flags &= ~(NFS_MOUNT_SOFT |
514 NFS_MOUNT_SOFTREVAL);
518 ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
520 ctx->flags &= NFS_MOUNT_SOFTREVAL;
524 ctx->flags &= ~NFS_MOUNT_POSIX;
526 ctx->flags |= NFS_MOUNT_POSIX;
530 ctx->flags |= NFS_MOUNT_NOCTO;
532 ctx->flags &= ~NFS_MOUNT_NOCTO;
534 case Opt_trunkdiscovery:
536 ctx->flags &= ~NFS_MOUNT_TRUNK_DISCOVERY;
538 ctx->flags |= NFS_MOUNT_TRUNK_DISCOVERY;
542 ctx->flags |= NFS_MOUNT_NOAC;
544 ctx->flags &= ~NFS_MOUNT_NOAC;
547 if (result.negated) {
548 ctx->flags |= NFS_MOUNT_NONLM;
549 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
551 ctx->flags &= ~NFS_MOUNT_NONLM;
552 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
556 ctx->flags &= ~NFS_MOUNT_TCP;
557 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
561 ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
562 ret = xprt_find_transport_ident(param->key);
564 goto out_bad_transport;
565 ctx->nfs_server.protocol = ret;
569 ctx->flags |= NFS_MOUNT_NOACL;
571 ctx->flags &= ~NFS_MOUNT_NOACL;
575 ctx->flags |= NFS_MOUNT_NORDIRPLUS;
577 ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
581 ctx->flags |= NFS_MOUNT_UNSHARED;
583 ctx->flags &= ~NFS_MOUNT_UNSHARED;
587 ctx->flags |= NFS_MOUNT_NORESVPORT;
589 ctx->flags &= ~NFS_MOUNT_NORESVPORT;
591 case Opt_fscache_flag:
593 ctx->options &= ~NFS_OPTION_FSCACHE;
595 ctx->options |= NFS_OPTION_FSCACHE;
596 kfree(ctx->fscache_uniq);
597 ctx->fscache_uniq = NULL;
600 ctx->options |= NFS_OPTION_FSCACHE;
601 kfree(ctx->fscache_uniq);
602 ctx->fscache_uniq = param->string;
603 param->string = NULL;
607 ctx->options &= ~NFS_OPTION_MIGRATION;
609 ctx->options |= NFS_OPTION_MIGRATION;
613 * options that take numeric values
616 if (result.uint_32 > USHRT_MAX)
618 ctx->nfs_server.port = result.uint_32;
621 ctx->rsize = result.uint_32;
624 ctx->wsize = result.uint_32;
627 ctx->bsize = result.uint_32;
630 if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
632 ctx->timeo = result.uint_32;
635 if (result.uint_32 > INT_MAX)
637 ctx->retrans = result.uint_32;
640 ctx->acregmin = result.uint_32;
643 ctx->acregmax = result.uint_32;
646 ctx->acdirmin = result.uint_32;
649 ctx->acdirmax = result.uint_32;
652 ctx->acregmin = result.uint_32;
653 ctx->acregmax = result.uint_32;
654 ctx->acdirmin = result.uint_32;
655 ctx->acdirmax = result.uint_32;
658 ctx->namlen = result.uint_32;
661 if (result.uint_32 > USHRT_MAX)
663 ctx->mount_server.port = result.uint_32;
666 if (result.uint_32 < NFS_MNT_VERSION ||
667 result.uint_32 > NFS_MNT3_VERSION)
669 ctx->mount_server.version = result.uint_32;
671 case Opt_minorversion:
672 if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
674 ctx->minorversion = result.uint_32;
678 * options that take text values
681 ret = nfs_parse_version_string(fc, param->key + 1);
686 ret = nfs_parse_version_string(fc, param->string);
691 ret = nfs_parse_security_flavors(fc, param);
697 protofamily = AF_INET;
698 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
700 protofamily = AF_INET6;
703 ctx->flags &= ~NFS_MOUNT_TCP;
704 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
707 protofamily = AF_INET6;
710 ctx->flags |= NFS_MOUNT_TCP;
711 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
714 protofamily = AF_INET6;
717 /* vector side protocols to TCP */
718 ctx->flags |= NFS_MOUNT_TCP;
719 ret = xprt_find_transport_ident(param->string);
721 goto out_bad_transport;
722 ctx->nfs_server.protocol = ret;
725 goto out_bad_transport;
728 ctx->protofamily = protofamily;
732 mountfamily = AF_INET;
733 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
735 mountfamily = AF_INET6;
738 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
741 mountfamily = AF_INET6;
744 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
746 case Opt_xprt_rdma: /* not used for side protocols */
748 goto out_bad_transport;
750 ctx->mountfamily = mountfamily;
754 len = rpc_pton(fc->net_ns, param->string, param->size,
755 &ctx->nfs_server.address,
756 sizeof(ctx->nfs_server._address));
758 goto out_invalid_address;
759 ctx->nfs_server.addrlen = len;
762 kfree(ctx->client_address);
763 ctx->client_address = param->string;
764 param->string = NULL;
767 kfree(ctx->mount_server.hostname);
768 ctx->mount_server.hostname = param->string;
769 param->string = NULL;
772 len = rpc_pton(fc->net_ns, param->string, param->size,
773 &ctx->mount_server.address,
774 sizeof(ctx->mount_server._address));
776 goto out_invalid_address;
777 ctx->mount_server.addrlen = len;
780 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
782 ctx->nfs_server.nconnect = result.uint_32;
784 case Opt_max_connect:
785 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_TRANSPORTS)
787 ctx->nfs_server.max_connect = result.uint_32;
789 case Opt_lookupcache:
790 switch (result.uint_32) {
791 case Opt_lookupcache_all:
792 ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
794 case Opt_lookupcache_positive:
795 ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
796 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
798 case Opt_lookupcache_none:
799 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
802 goto out_invalid_value;
806 switch (result.uint_32) {
807 case Opt_local_lock_all:
808 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
809 NFS_MOUNT_LOCAL_FCNTL);
811 case Opt_local_lock_flock:
812 ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
814 case Opt_local_lock_posix:
815 ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
817 case Opt_local_lock_none:
818 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
819 NFS_MOUNT_LOCAL_FCNTL);
822 goto out_invalid_value;
826 switch (result.uint_32) {
829 ~(NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT);
831 case Opt_write_eager:
832 ctx->flags |= NFS_MOUNT_WRITE_EAGER;
833 ctx->flags &= ~NFS_MOUNT_WRITE_WAIT;
837 NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT;
840 goto out_invalid_value;
849 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
856 return nfs_invalf(fc, "NFS: Bad mount option value specified");
858 return nfs_invalf(fc, "NFS: Bad IP address specified");
860 return nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
862 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
866 * Split fc->source into "hostname:export_path".
868 * The leftmost colon demarks the split between the server's hostname
869 * and the export path. If the hostname starts with a left square
870 * bracket, then it may contain colons.
872 * Note: caller frees hostname and export path, even on error.
874 static int nfs_parse_source(struct fs_context *fc,
875 size_t maxnamlen, size_t maxpathlen)
877 struct nfs_fs_context *ctx = nfs_fc2context(fc);
878 const char *dev_name = fc->source;
882 if (unlikely(!dev_name || !*dev_name)) {
883 dfprintk(MOUNT, "NFS: device name not specified\n");
887 /* Is the host name protected with square brakcets? */
888 if (*dev_name == '[') {
889 end = strchr(++dev_name, ']');
890 if (end == NULL || end[1] != ':')
891 goto out_bad_devname;
893 len = end - dev_name;
898 end = strchr(dev_name, ':');
900 goto out_bad_devname;
901 len = end - dev_name;
903 /* kill possible hostname list: not supported */
904 comma = memchr(dev_name, ',', len);
906 len = comma - dev_name;
912 kfree(ctx->nfs_server.hostname);
914 /* N.B. caller will free nfs_server.hostname in all cases */
915 ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
916 if (!ctx->nfs_server.hostname)
919 if (len > maxpathlen)
921 ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
922 if (!ctx->nfs_server.export_path)
925 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", ctx->nfs_server.export_path);
929 return nfs_invalf(fc, "NFS: device name not in host:path format");
931 nfs_errorf(fc, "NFS: not enough memory to parse device name");
934 nfs_errorf(fc, "NFS: server hostname too long");
935 return -ENAMETOOLONG;
937 nfs_errorf(fc, "NFS: export pathname too long");
938 return -ENAMETOOLONG;
941 static inline bool is_remount_fc(struct fs_context *fc)
943 return fc->root != NULL;
947 * Parse monolithic NFS2/NFS3 mount data
948 * - fills in the mount root filehandle
950 * For option strings, user space handles the following behaviors:
952 * + DNS: mapping server host name to IP address ("addr=" option)
954 * + failure mode: how to behave if a mount request can't be handled
955 * immediately ("fg/bg" option)
957 * + retry: how often to retry a mount request ("retry=" option)
959 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
960 * mountproto=tcp after mountproto=udp, and so on
962 static int nfs23_parse_monolithic(struct fs_context *fc,
963 struct nfs_mount_data *data)
965 struct nfs_fs_context *ctx = nfs_fc2context(fc);
966 struct nfs_fh *mntfh = ctx->mntfh;
967 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
968 int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
974 ctx->version = NFS_DEFAULT_VERSION;
975 switch (data->version) {
983 if (data->flags & NFS_MOUNT_VER3)
985 data->root.size = NFS2_FHSIZE;
986 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
987 /* Turn off security negotiation */
988 extra_flags |= NFS_MOUNT_SECFLAVOUR;
991 if (data->flags & NFS_MOUNT_SECFLAVOUR)
995 memset(data->context, 0, sizeof(data->context));
998 if (data->flags & NFS_MOUNT_VER3) {
999 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1000 goto out_invalid_fh;
1001 mntfh->size = data->root.size;
1004 mntfh->size = NFS2_FHSIZE;
1009 memcpy(mntfh->data, data->root.data, mntfh->size);
1010 if (mntfh->size < sizeof(mntfh->data))
1011 memset(mntfh->data + mntfh->size, 0,
1012 sizeof(mntfh->data) - mntfh->size);
1015 * for proto == XPRT_TRANSPORT_UDP, which is what uses
1016 * to_exponential, implying shift: limit the shift value
1017 * to BITS_PER_LONG (majortimeo is unsigned long)
1019 if (!(data->flags & NFS_MOUNT_TCP)) /* this will be UDP */
1020 if (data->retrans >= 64) /* shift value is too large */
1021 goto out_invalid_data;
1024 * Translate to nfs_fs_context, which nfs_fill_super
1027 ctx->flags = data->flags & NFS_MOUNT_FLAGMASK;
1028 ctx->flags |= extra_flags;
1029 ctx->rsize = data->rsize;
1030 ctx->wsize = data->wsize;
1031 ctx->timeo = data->timeo;
1032 ctx->retrans = data->retrans;
1033 ctx->acregmin = data->acregmin;
1034 ctx->acregmax = data->acregmax;
1035 ctx->acdirmin = data->acdirmin;
1036 ctx->acdirmax = data->acdirmax;
1037 ctx->need_mount = false;
1039 memcpy(sap, &data->addr, sizeof(data->addr));
1040 ctx->nfs_server.addrlen = sizeof(data->addr);
1041 ctx->nfs_server.port = ntohs(data->addr.sin_port);
1042 if (sap->sa_family != AF_INET ||
1043 !nfs_verify_server_address(sap))
1044 goto out_no_address;
1046 if (!(data->flags & NFS_MOUNT_TCP))
1047 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1048 /* N.B. caller will free nfs_server.hostname in all cases */
1049 ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1050 if (!ctx->nfs_server.hostname)
1053 ctx->namlen = data->namlen;
1054 ctx->bsize = data->bsize;
1056 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1057 ctx->selected_flavor = data->pseudoflavor;
1059 ctx->selected_flavor = RPC_AUTH_UNIX;
1061 if (!(data->flags & NFS_MOUNT_NONLM))
1062 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1063 NFS_MOUNT_LOCAL_FCNTL);
1065 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1066 NFS_MOUNT_LOCAL_FCNTL);
1069 * The legacy version 6 binary mount data from userspace has a
1070 * field used only to transport selinux information into the
1071 * kernel. To continue to support that functionality we
1072 * have a touch of selinux knowledge here in the NFS code. The
1073 * userspace code converted context=blah to just blah so we are
1074 * converting back to the full string selinux understands.
1076 if (data->context[0]){
1077 #ifdef CONFIG_SECURITY_SELINUX
1080 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1081 ret = vfs_parse_fs_string(fc, "context",
1082 data->context, strlen(data->context));
1095 ret = nfs_validate_transport_protocol(fc, ctx);
1099 ctx->skip_reconfig_option_check = true;
1103 return generic_parse_monolithic(fc, data);
1106 if (is_remount_fc(fc)) {
1107 ctx->skip_reconfig_option_check = true;
1110 return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
1113 return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
1116 return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
1119 dfprintk(MOUNT, "NFS: not enough memory to handle mount options");
1123 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1126 return nfs_invalf(fc, "NFS: invalid root filehandle");
1129 return nfs_invalf(fc, "NFS: invalid binary mount data");
1132 #if IS_ENABLED(CONFIG_NFS_V4)
1133 struct compat_nfs_string {
1138 static inline void compat_nfs_string(struct nfs_string *dst,
1139 struct compat_nfs_string *src)
1141 dst->data = compat_ptr(src->data);
1142 dst->len = src->len;
1145 struct compat_nfs4_mount_data_v1 {
1146 compat_int_t version;
1151 compat_int_t retrans;
1152 compat_int_t acregmin;
1153 compat_int_t acregmax;
1154 compat_int_t acdirmin;
1155 compat_int_t acdirmax;
1156 struct compat_nfs_string client_addr;
1157 struct compat_nfs_string mnt_path;
1158 struct compat_nfs_string hostname;
1159 compat_uint_t host_addrlen;
1160 compat_uptr_t host_addr;
1162 compat_int_t auth_flavourlen;
1163 compat_uptr_t auth_flavours;
1166 static void nfs4_compat_mount_data_conv(struct nfs4_mount_data *data)
1168 struct compat_nfs4_mount_data_v1 *compat =
1169 (struct compat_nfs4_mount_data_v1 *)data;
1171 /* copy the fields backwards */
1172 data->auth_flavours = compat_ptr(compat->auth_flavours);
1173 data->auth_flavourlen = compat->auth_flavourlen;
1174 data->proto = compat->proto;
1175 data->host_addr = compat_ptr(compat->host_addr);
1176 data->host_addrlen = compat->host_addrlen;
1177 compat_nfs_string(&data->hostname, &compat->hostname);
1178 compat_nfs_string(&data->mnt_path, &compat->mnt_path);
1179 compat_nfs_string(&data->client_addr, &compat->client_addr);
1180 data->acdirmax = compat->acdirmax;
1181 data->acdirmin = compat->acdirmin;
1182 data->acregmax = compat->acregmax;
1183 data->acregmin = compat->acregmin;
1184 data->retrans = compat->retrans;
1185 data->timeo = compat->timeo;
1186 data->wsize = compat->wsize;
1187 data->rsize = compat->rsize;
1188 data->flags = compat->flags;
1189 data->version = compat->version;
1193 * Validate NFSv4 mount options
1195 static int nfs4_parse_monolithic(struct fs_context *fc,
1196 struct nfs4_mount_data *data)
1198 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1199 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
1204 if (is_remount_fc(fc))
1206 return nfs_invalf(fc,
1207 "NFS4: mount program didn't pass any mount data");
1212 if (data->version != 1)
1213 return generic_parse_monolithic(fc, data);
1215 if (in_compat_syscall())
1216 nfs4_compat_mount_data_conv(data);
1218 if (data->host_addrlen > sizeof(ctx->nfs_server.address))
1219 goto out_no_address;
1220 if (data->host_addrlen == 0)
1221 goto out_no_address;
1222 ctx->nfs_server.addrlen = data->host_addrlen;
1223 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1225 if (!nfs_verify_server_address(sap))
1226 goto out_no_address;
1227 ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1229 if (data->auth_flavourlen) {
1230 rpc_authflavor_t pseudoflavor;
1232 if (data->auth_flavourlen > 1)
1233 goto out_inval_auth;
1234 if (copy_from_user(&pseudoflavor, data->auth_flavours,
1235 sizeof(pseudoflavor)))
1237 ctx->selected_flavor = pseudoflavor;
1239 ctx->selected_flavor = RPC_AUTH_UNIX;
1242 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1245 ctx->nfs_server.hostname = c;
1247 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1250 ctx->nfs_server.export_path = c;
1251 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1253 c = strndup_user(data->client_addr.data, 16);
1256 ctx->client_address = c;
1259 * Translate to nfs_fs_context, which nfs_fill_super
1263 ctx->flags = data->flags & NFS4_MOUNT_FLAGMASK;
1264 ctx->rsize = data->rsize;
1265 ctx->wsize = data->wsize;
1266 ctx->timeo = data->timeo;
1267 ctx->retrans = data->retrans;
1268 ctx->acregmin = data->acregmin;
1269 ctx->acregmax = data->acregmax;
1270 ctx->acdirmin = data->acdirmin;
1271 ctx->acdirmax = data->acdirmax;
1272 ctx->nfs_server.protocol = data->proto;
1273 ret = nfs_validate_transport_protocol(fc, ctx);
1277 ctx->skip_reconfig_option_check = true;
1281 return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1282 data->auth_flavourlen);
1285 return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
1290 * Parse a monolithic block of data from sys_mount().
1292 static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1295 if (fc->fs_type == &nfs_fs_type)
1296 return nfs23_parse_monolithic(fc, data);
1298 #if IS_ENABLED(CONFIG_NFS_V4)
1299 if (fc->fs_type == &nfs4_fs_type)
1300 return nfs4_parse_monolithic(fc, data);
1303 return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
1307 * Validate the preparsed information in the config.
1309 static int nfs_fs_context_validate(struct fs_context *fc)
1311 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1312 struct nfs_subversion *nfs_mod;
1313 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
1314 int max_namelen = PAGE_SIZE;
1315 int max_pathlen = NFS_MAXPATHLEN;
1320 goto out_no_device_name;
1322 /* Check for sanity first. */
1323 if (ctx->minorversion && ctx->version != 4)
1324 goto out_minorversion_mismatch;
1326 if (ctx->options & NFS_OPTION_MIGRATION &&
1327 (ctx->version != 4 || ctx->minorversion != 0))
1328 goto out_migration_misuse;
1330 /* Verify that any proto=/mountproto= options match the address
1331 * families in the addr=/mountaddr= options.
1333 if (ctx->protofamily != AF_UNSPEC &&
1334 ctx->protofamily != ctx->nfs_server.address.sa_family)
1335 goto out_proto_mismatch;
1337 if (ctx->mountfamily != AF_UNSPEC) {
1338 if (ctx->mount_server.addrlen) {
1339 if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1340 goto out_mountproto_mismatch;
1342 if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1343 goto out_mountproto_mismatch;
1347 if (!nfs_verify_server_address(sap))
1348 goto out_no_address;
1350 ret = nfs_validate_transport_protocol(fc, ctx);
1354 if (ctx->version == 4) {
1355 if (IS_ENABLED(CONFIG_NFS_V4)) {
1356 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1357 port = NFS_RDMA_PORT;
1360 max_namelen = NFS4_MAXNAMLEN;
1361 max_pathlen = NFS4_MAXPATHLEN;
1362 ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1363 NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1364 NFS_MOUNT_LOCAL_FCNTL);
1366 goto out_v4_not_compiled;
1369 nfs_set_mount_transport_protocol(ctx);
1370 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1371 port = NFS_RDMA_PORT;
1374 nfs_set_port(sap, &ctx->nfs_server.port, port);
1376 ret = nfs_parse_source(fc, max_namelen, max_pathlen);
1380 /* Load the NFS protocol module if we haven't done so yet */
1381 if (!ctx->nfs_mod) {
1382 nfs_mod = get_nfs_version(ctx->version);
1383 if (IS_ERR(nfs_mod)) {
1384 ret = PTR_ERR(nfs_mod);
1385 goto out_version_unavailable;
1387 ctx->nfs_mod = nfs_mod;
1390 /* Ensure the filesystem context has the correct fs_type */
1391 if (fc->fs_type != ctx->nfs_mod->nfs_fs) {
1392 module_put(fc->fs_type->owner);
1393 __module_get(ctx->nfs_mod->nfs_fs->owner);
1394 fc->fs_type = ctx->nfs_mod->nfs_fs;
1399 return nfs_invalf(fc, "NFS: Device name not specified");
1400 out_v4_not_compiled:
1401 nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
1402 return -EPROTONOSUPPORT;
1404 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1405 out_mountproto_mismatch:
1406 return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
1408 return nfs_invalf(fc, "NFS: Server address does not match proto= option");
1409 out_minorversion_mismatch:
1410 return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
1411 ctx->version, ctx->minorversion);
1412 out_migration_misuse:
1413 return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
1414 out_version_unavailable:
1415 nfs_errorf(fc, "NFS: Version unavailable");
1420 * Create an NFS superblock by the appropriate method.
1422 static int nfs_get_tree(struct fs_context *fc)
1424 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1425 int err = nfs_fs_context_validate(fc);
1430 return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
1432 return nfs_get_tree_common(fc);
1436 * Handle duplication of a configuration. The caller copied *src into *sc, but
1437 * it can't deal with resource pointers in the filesystem context, so we have
1438 * to do that. We need to clear pointers, copy data or get extra refs as
1441 static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1443 struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1445 ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1449 ctx->mntfh = nfs_alloc_fhandle();
1454 nfs_copy_fh(ctx->mntfh, src->mntfh);
1456 __module_get(ctx->nfs_mod->owner);
1457 ctx->client_address = NULL;
1458 ctx->mount_server.hostname = NULL;
1459 ctx->nfs_server.export_path = NULL;
1460 ctx->nfs_server.hostname = NULL;
1461 ctx->fscache_uniq = NULL;
1462 ctx->clone_data.fattr = NULL;
1463 fc->fs_private = ctx;
1467 static void nfs_fs_context_free(struct fs_context *fc)
1469 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1473 nfs_free_server(ctx->server);
1475 put_nfs_version(ctx->nfs_mod);
1476 kfree(ctx->client_address);
1477 kfree(ctx->mount_server.hostname);
1478 kfree(ctx->nfs_server.export_path);
1479 kfree(ctx->nfs_server.hostname);
1480 kfree(ctx->fscache_uniq);
1481 nfs_free_fhandle(ctx->mntfh);
1482 nfs_free_fattr(ctx->clone_data.fattr);
1487 static const struct fs_context_operations nfs_fs_context_ops = {
1488 .free = nfs_fs_context_free,
1489 .dup = nfs_fs_context_dup,
1490 .parse_param = nfs_fs_context_parse_param,
1491 .parse_monolithic = nfs_fs_context_parse_monolithic,
1492 .get_tree = nfs_get_tree,
1493 .reconfigure = nfs_reconfigure,
1497 * Prepare superblock configuration. We use the namespaces attached to the
1498 * context. This may be the current process's namespaces, or it may be a
1499 * container's namespaces.
1501 static int nfs_init_fs_context(struct fs_context *fc)
1503 struct nfs_fs_context *ctx;
1505 ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1509 ctx->mntfh = nfs_alloc_fhandle();
1510 if (unlikely(!ctx->mntfh)) {
1515 ctx->protofamily = AF_UNSPEC;
1516 ctx->mountfamily = AF_UNSPEC;
1517 ctx->mount_server.port = NFS_UNSPEC_PORT;
1520 /* reconfigure, start with the current config */
1521 struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1522 struct net *net = nfss->nfs_client->cl_net;
1524 ctx->flags = nfss->flags;
1525 ctx->rsize = nfss->rsize;
1526 ctx->wsize = nfss->wsize;
1527 ctx->retrans = nfss->client->cl_timeout->to_retries;
1528 ctx->selected_flavor = nfss->client->cl_auth->au_flavor;
1529 ctx->acregmin = nfss->acregmin / HZ;
1530 ctx->acregmax = nfss->acregmax / HZ;
1531 ctx->acdirmin = nfss->acdirmin / HZ;
1532 ctx->acdirmax = nfss->acdirmax / HZ;
1533 ctx->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1534 ctx->nfs_server.port = nfss->port;
1535 ctx->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1536 ctx->version = nfss->nfs_client->rpc_ops->version;
1537 ctx->minorversion = nfss->nfs_client->cl_minorversion;
1539 memcpy(&ctx->nfs_server.address, &nfss->nfs_client->cl_addr,
1540 ctx->nfs_server.addrlen);
1542 if (fc->net_ns != net) {
1543 put_net(fc->net_ns);
1544 fc->net_ns = get_net(net);
1547 ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1548 __module_get(ctx->nfs_mod->owner);
1551 ctx->timeo = NFS_UNSPEC_TIMEO;
1552 ctx->retrans = NFS_UNSPEC_RETRANS;
1553 ctx->acregmin = NFS_DEF_ACREGMIN;
1554 ctx->acregmax = NFS_DEF_ACREGMAX;
1555 ctx->acdirmin = NFS_DEF_ACDIRMIN;
1556 ctx->acdirmax = NFS_DEF_ACDIRMAX;
1557 ctx->nfs_server.port = NFS_UNSPEC_PORT;
1558 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1559 ctx->selected_flavor = RPC_AUTH_MAXFLAVOR;
1560 ctx->minorversion = 0;
1561 ctx->need_mount = true;
1563 fc->s_iflags |= SB_I_STABLE_WRITES;
1565 fc->fs_private = ctx;
1566 fc->ops = &nfs_fs_context_ops;
1570 struct file_system_type nfs_fs_type = {
1571 .owner = THIS_MODULE,
1573 .init_fs_context = nfs_init_fs_context,
1574 .parameters = nfs_fs_parameters,
1575 .kill_sb = nfs_kill_super,
1576 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1578 MODULE_ALIAS_FS("nfs");
1579 EXPORT_SYMBOL_GPL(nfs_fs_type);
1581 #if IS_ENABLED(CONFIG_NFS_V4)
1582 struct file_system_type nfs4_fs_type = {
1583 .owner = THIS_MODULE,
1585 .init_fs_context = nfs_init_fs_context,
1586 .parameters = nfs_fs_parameters,
1587 .kill_sb = nfs_kill_super,
1588 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1590 MODULE_ALIAS_FS("nfs4");
1591 MODULE_ALIAS("nfs4");
1592 EXPORT_SYMBOL_GPL(nfs4_fs_type);
1593 #endif /* CONFIG_NFS_V4 */