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/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
95 static const struct constant_table nfs_param_enums_local_lock[] = {
96 { "all", Opt_local_lock_all },
97 { "flock", Opt_local_lock_flock },
98 { "posix", Opt_local_lock_posix },
99 { "none", Opt_local_lock_none },
105 Opt_lookupcache_none,
106 Opt_lookupcache_positive,
109 static const struct constant_table nfs_param_enums_lookupcache[] = {
110 { "all", Opt_lookupcache_all },
111 { "none", Opt_lookupcache_none },
112 { "pos", Opt_lookupcache_positive },
113 { "positive", Opt_lookupcache_positive },
123 static const struct constant_table nfs_param_enums_write[] = {
124 { "lazy", Opt_write_lazy },
125 { "eager", Opt_write_eager },
126 { "wait", Opt_write_wait },
130 static const struct fs_parameter_spec nfs_fs_parameters[] = {
131 fsparam_flag_no("ac", Opt_ac),
132 fsparam_u32 ("acdirmax", Opt_acdirmax),
133 fsparam_u32 ("acdirmin", Opt_acdirmin),
134 fsparam_flag_no("acl", Opt_acl),
135 fsparam_u32 ("acregmax", Opt_acregmax),
136 fsparam_u32 ("acregmin", Opt_acregmin),
137 fsparam_u32 ("actimeo", Opt_actimeo),
138 fsparam_string("addr", Opt_addr),
139 fsparam_flag ("bg", Opt_bg),
140 fsparam_u32 ("bsize", Opt_bsize),
141 fsparam_string("clientaddr", Opt_clientaddr),
142 fsparam_flag_no("cto", Opt_cto),
143 fsparam_flag ("fg", Opt_fg),
144 fsparam_flag_no("fsc", Opt_fscache_flag),
145 fsparam_string("fsc", Opt_fscache),
146 fsparam_flag ("hard", Opt_hard),
147 __fsparam(NULL, "intr", Opt_intr,
148 fs_param_neg_with_no|fs_param_deprecated, NULL),
149 fsparam_enum ("local_lock", Opt_local_lock, nfs_param_enums_local_lock),
150 fsparam_flag_no("lock", Opt_lock),
151 fsparam_enum ("lookupcache", Opt_lookupcache, nfs_param_enums_lookupcache),
152 fsparam_flag_no("migration", Opt_migration),
153 fsparam_u32 ("minorversion", Opt_minorversion),
154 fsparam_string("mountaddr", Opt_mountaddr),
155 fsparam_string("mounthost", Opt_mounthost),
156 fsparam_u32 ("mountport", Opt_mountport),
157 fsparam_string("mountproto", Opt_mountproto),
158 fsparam_u32 ("mountvers", Opt_mountvers),
159 fsparam_u32 ("namlen", Opt_namelen),
160 fsparam_u32 ("nconnect", Opt_nconnect),
161 fsparam_string("nfsvers", Opt_vers),
162 fsparam_u32 ("port", Opt_port),
163 fsparam_flag_no("posix", Opt_posix),
164 fsparam_string("proto", Opt_proto),
165 fsparam_flag_no("rdirplus", Opt_rdirplus),
166 fsparam_flag ("rdma", Opt_rdma),
167 fsparam_flag_no("resvport", Opt_resvport),
168 fsparam_u32 ("retrans", Opt_retrans),
169 fsparam_string("retry", Opt_retry),
170 fsparam_u32 ("rsize", Opt_rsize),
171 fsparam_string("sec", Opt_sec),
172 fsparam_flag_no("sharecache", Opt_sharecache),
173 fsparam_flag ("sloppy", Opt_sloppy),
174 fsparam_flag ("soft", Opt_soft),
175 fsparam_flag ("softerr", Opt_softerr),
176 fsparam_flag ("softreval", Opt_softreval),
177 fsparam_string("source", Opt_source),
178 fsparam_flag ("tcp", Opt_tcp),
179 fsparam_u32 ("timeo", Opt_timeo),
180 fsparam_flag ("udp", Opt_udp),
181 fsparam_flag ("v2", Opt_v),
182 fsparam_flag ("v3", Opt_v),
183 fsparam_flag ("v4", Opt_v),
184 fsparam_flag ("v4.0", Opt_v),
185 fsparam_flag ("v4.1", Opt_v),
186 fsparam_flag ("v4.2", Opt_v),
187 fsparam_string("vers", Opt_vers),
188 fsparam_enum ("write", Opt_write, nfs_param_enums_write),
189 fsparam_u32 ("wsize", Opt_wsize),
202 static const struct constant_table nfs_vers_tokens[] = {
206 { "4.0", Opt_vers_4_0 },
207 { "4.1", Opt_vers_4_1 },
208 { "4.2", Opt_vers_4_2 },
222 static const struct constant_table nfs_xprt_protocol_tokens[] = {
223 { "rdma", Opt_xprt_rdma },
224 { "rdma6", Opt_xprt_rdma6 },
225 { "tcp", Opt_xprt_tcp },
226 { "tcp6", Opt_xprt_tcp6 },
227 { "udp", Opt_xprt_udp },
228 { "udp6", Opt_xprt_udp6 },
247 static const struct constant_table nfs_secflavor_tokens[] = {
248 { "krb5", Opt_sec_krb5 },
249 { "krb5i", Opt_sec_krb5i },
250 { "krb5p", Opt_sec_krb5p },
251 { "lkey", Opt_sec_lkey },
252 { "lkeyi", Opt_sec_lkeyi },
253 { "lkeyp", Opt_sec_lkeyp },
254 { "none", Opt_sec_none },
255 { "null", Opt_sec_none },
256 { "spkm3", Opt_sec_spkm },
257 { "spkm3i", Opt_sec_spkmi },
258 { "spkm3p", Opt_sec_spkmp },
259 { "sys", Opt_sec_sys },
264 * Sanity-check a server address provided by the mount command.
266 * Address family must be initialized, and address must not be
267 * the ANY address for that family.
269 static int nfs_verify_server_address(struct sockaddr *addr)
271 switch (addr->sa_family) {
273 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
274 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
277 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
278 return !ipv6_addr_any(sa);
282 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
286 #ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
287 static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
292 static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
294 if (ctx->version == 4)
301 * Sanity check the NFS transport protocol.
303 static int nfs_validate_transport_protocol(struct fs_context *fc,
304 struct nfs_fs_context *ctx)
306 switch (ctx->nfs_server.protocol) {
307 case XPRT_TRANSPORT_UDP:
308 if (nfs_server_transport_udp_invalid(ctx))
309 goto out_invalid_transport_udp;
311 case XPRT_TRANSPORT_TCP:
312 case XPRT_TRANSPORT_RDMA:
315 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
318 out_invalid_transport_udp:
319 return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
323 * For text based NFSv2/v3 mounts, the mount protocol transport default
324 * settings should depend upon the specified NFS transport.
326 static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
328 if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
329 ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
331 switch (ctx->nfs_server.protocol) {
332 case XPRT_TRANSPORT_UDP:
333 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
335 case XPRT_TRANSPORT_TCP:
336 case XPRT_TRANSPORT_RDMA:
337 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
342 * Add 'flavor' to 'auth_info' if not already present.
343 * Returns true if 'flavor' ends up in the list, false otherwise
345 static int nfs_auth_info_add(struct fs_context *fc,
346 struct nfs_auth_info *auth_info,
347 rpc_authflavor_t flavor)
350 unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
352 /* make sure this flavor isn't already in the list */
353 for (i = 0; i < auth_info->flavor_len; i++) {
354 if (flavor == auth_info->flavors[i])
358 if (auth_info->flavor_len + 1 >= max_flavor_len)
359 return nfs_invalf(fc, "NFS: too many sec= flavors");
361 auth_info->flavors[auth_info->flavor_len++] = flavor;
366 * Parse the value of the 'sec=' option.
368 static int nfs_parse_security_flavors(struct fs_context *fc,
369 struct fs_parameter *param)
371 struct nfs_fs_context *ctx = nfs_fc2context(fc);
372 rpc_authflavor_t pseudoflavor;
373 char *string = param->string, *p;
376 dfprintk(MOUNT, "NFS: parsing %s=%s option\n", param->key, param->string);
378 while ((p = strsep(&string, ":")) != NULL) {
381 switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
383 pseudoflavor = RPC_AUTH_NULL;
386 pseudoflavor = RPC_AUTH_UNIX;
389 pseudoflavor = RPC_AUTH_GSS_KRB5;
392 pseudoflavor = RPC_AUTH_GSS_KRB5I;
395 pseudoflavor = RPC_AUTH_GSS_KRB5P;
398 pseudoflavor = RPC_AUTH_GSS_LKEY;
401 pseudoflavor = RPC_AUTH_GSS_LKEYI;
404 pseudoflavor = RPC_AUTH_GSS_LKEYP;
407 pseudoflavor = RPC_AUTH_GSS_SPKM;
410 pseudoflavor = RPC_AUTH_GSS_SPKMI;
413 pseudoflavor = RPC_AUTH_GSS_SPKMP;
416 return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
419 ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
427 static int nfs_parse_version_string(struct fs_context *fc,
430 struct nfs_fs_context *ctx = nfs_fc2context(fc);
432 ctx->flags &= ~NFS_MOUNT_VER3;
433 switch (lookup_constant(nfs_vers_tokens, string, -1)) {
438 ctx->flags |= NFS_MOUNT_VER3;
442 /* Backward compatibility option. In future,
443 * the mount program should always supply
444 * a NFSv4 minor version number.
450 ctx->minorversion = 0;
454 ctx->minorversion = 1;
458 ctx->minorversion = 2;
461 return nfs_invalf(fc, "NFS: Unsupported NFS version");
467 * Parse a single mount parameter.
469 static int nfs_fs_context_parse_param(struct fs_context *fc,
470 struct fs_parameter *param)
472 struct fs_parse_result result;
473 struct nfs_fs_context *ctx = nfs_fc2context(fc);
474 unsigned short protofamily, mountfamily;
478 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", param->key);
480 opt = fs_parse(fc, nfs_fs_parameters, param, &result);
482 return ctx->sloppy ? 1 : opt;
485 ctx->has_sec_mnt_opts = 1;
490 return nfs_invalf(fc, "NFS: Multiple sources not supported");
491 fc->source = param->string;
492 param->string = NULL;
496 * boolean options: foo/nofoo
499 ctx->flags |= NFS_MOUNT_SOFT;
500 ctx->flags &= ~NFS_MOUNT_SOFTERR;
503 ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
504 ctx->flags &= ~NFS_MOUNT_SOFT;
507 ctx->flags &= ~(NFS_MOUNT_SOFT |
509 NFS_MOUNT_SOFTREVAL);
513 ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
515 ctx->flags &= NFS_MOUNT_SOFTREVAL;
519 ctx->flags &= ~NFS_MOUNT_POSIX;
521 ctx->flags |= NFS_MOUNT_POSIX;
525 ctx->flags |= NFS_MOUNT_NOCTO;
527 ctx->flags &= ~NFS_MOUNT_NOCTO;
531 ctx->flags |= NFS_MOUNT_NOAC;
533 ctx->flags &= ~NFS_MOUNT_NOAC;
536 if (result.negated) {
537 ctx->flags |= NFS_MOUNT_NONLM;
538 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
540 ctx->flags &= ~NFS_MOUNT_NONLM;
541 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
545 ctx->flags &= ~NFS_MOUNT_TCP;
546 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
550 ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
551 ret = xprt_find_transport_ident(param->key);
553 goto out_bad_transport;
554 ctx->nfs_server.protocol = ret;
558 ctx->flags |= NFS_MOUNT_NOACL;
560 ctx->flags &= ~NFS_MOUNT_NOACL;
564 ctx->flags |= NFS_MOUNT_NORDIRPLUS;
566 ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
570 ctx->flags |= NFS_MOUNT_UNSHARED;
572 ctx->flags &= ~NFS_MOUNT_UNSHARED;
576 ctx->flags |= NFS_MOUNT_NORESVPORT;
578 ctx->flags &= ~NFS_MOUNT_NORESVPORT;
580 case Opt_fscache_flag:
582 ctx->options &= ~NFS_OPTION_FSCACHE;
584 ctx->options |= NFS_OPTION_FSCACHE;
585 kfree(ctx->fscache_uniq);
586 ctx->fscache_uniq = NULL;
589 ctx->options |= NFS_OPTION_FSCACHE;
590 kfree(ctx->fscache_uniq);
591 ctx->fscache_uniq = param->string;
592 param->string = NULL;
596 ctx->options &= ~NFS_OPTION_MIGRATION;
598 ctx->options |= NFS_OPTION_MIGRATION;
602 * options that take numeric values
605 if (result.uint_32 > USHRT_MAX)
607 ctx->nfs_server.port = result.uint_32;
610 ctx->rsize = result.uint_32;
613 ctx->wsize = result.uint_32;
616 ctx->bsize = result.uint_32;
619 if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
621 ctx->timeo = result.uint_32;
624 if (result.uint_32 > INT_MAX)
626 ctx->retrans = result.uint_32;
629 ctx->acregmin = result.uint_32;
632 ctx->acregmax = result.uint_32;
635 ctx->acdirmin = result.uint_32;
638 ctx->acdirmax = result.uint_32;
641 ctx->acregmin = result.uint_32;
642 ctx->acregmax = result.uint_32;
643 ctx->acdirmin = result.uint_32;
644 ctx->acdirmax = result.uint_32;
647 ctx->namlen = result.uint_32;
650 if (result.uint_32 > USHRT_MAX)
652 ctx->mount_server.port = result.uint_32;
655 if (result.uint_32 < NFS_MNT_VERSION ||
656 result.uint_32 > NFS_MNT3_VERSION)
658 ctx->mount_server.version = result.uint_32;
660 case Opt_minorversion:
661 if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
663 ctx->minorversion = result.uint_32;
667 * options that take text values
670 ret = nfs_parse_version_string(fc, param->key + 1);
675 ret = nfs_parse_version_string(fc, param->string);
680 ret = nfs_parse_security_flavors(fc, param);
686 protofamily = AF_INET;
687 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
689 protofamily = AF_INET6;
692 ctx->flags &= ~NFS_MOUNT_TCP;
693 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
696 protofamily = AF_INET6;
699 ctx->flags |= NFS_MOUNT_TCP;
700 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
703 protofamily = AF_INET6;
706 /* vector side protocols to TCP */
707 ctx->flags |= NFS_MOUNT_TCP;
708 ret = xprt_find_transport_ident(param->string);
710 goto out_bad_transport;
711 ctx->nfs_server.protocol = ret;
714 goto out_bad_transport;
717 ctx->protofamily = protofamily;
721 mountfamily = AF_INET;
722 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
724 mountfamily = AF_INET6;
727 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
730 mountfamily = AF_INET6;
733 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
735 case Opt_xprt_rdma: /* not used for side protocols */
737 goto out_bad_transport;
739 ctx->mountfamily = mountfamily;
743 len = rpc_pton(fc->net_ns, param->string, param->size,
744 &ctx->nfs_server.address,
745 sizeof(ctx->nfs_server._address));
747 goto out_invalid_address;
748 ctx->nfs_server.addrlen = len;
751 kfree(ctx->client_address);
752 ctx->client_address = param->string;
753 param->string = NULL;
756 kfree(ctx->mount_server.hostname);
757 ctx->mount_server.hostname = param->string;
758 param->string = NULL;
761 len = rpc_pton(fc->net_ns, param->string, param->size,
762 &ctx->mount_server.address,
763 sizeof(ctx->mount_server._address));
765 goto out_invalid_address;
766 ctx->mount_server.addrlen = len;
769 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
771 ctx->nfs_server.nconnect = result.uint_32;
773 case Opt_lookupcache:
774 switch (result.uint_32) {
775 case Opt_lookupcache_all:
776 ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
778 case Opt_lookupcache_positive:
779 ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
780 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
782 case Opt_lookupcache_none:
783 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
786 goto out_invalid_value;
790 switch (result.uint_32) {
791 case Opt_local_lock_all:
792 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
793 NFS_MOUNT_LOCAL_FCNTL);
795 case Opt_local_lock_flock:
796 ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
798 case Opt_local_lock_posix:
799 ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
801 case Opt_local_lock_none:
802 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
803 NFS_MOUNT_LOCAL_FCNTL);
806 goto out_invalid_value;
810 switch (result.uint_32) {
813 ~(NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT);
815 case Opt_write_eager:
816 ctx->flags |= NFS_MOUNT_WRITE_EAGER;
817 ctx->flags &= ~NFS_MOUNT_WRITE_WAIT;
821 NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT;
824 goto out_invalid_value;
833 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
840 return nfs_invalf(fc, "NFS: Bad mount option value specified");
842 return nfs_invalf(fc, "NFS: Bad IP address specified");
844 return nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
846 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
850 * Split fc->source into "hostname:export_path".
852 * The leftmost colon demarks the split between the server's hostname
853 * and the export path. If the hostname starts with a left square
854 * bracket, then it may contain colons.
856 * Note: caller frees hostname and export path, even on error.
858 static int nfs_parse_source(struct fs_context *fc,
859 size_t maxnamlen, size_t maxpathlen)
861 struct nfs_fs_context *ctx = nfs_fc2context(fc);
862 const char *dev_name = fc->source;
866 if (unlikely(!dev_name || !*dev_name)) {
867 dfprintk(MOUNT, "NFS: device name not specified\n");
871 /* Is the host name protected with square brakcets? */
872 if (*dev_name == '[') {
873 end = strchr(++dev_name, ']');
874 if (end == NULL || end[1] != ':')
875 goto out_bad_devname;
877 len = end - dev_name;
882 end = strchr(dev_name, ':');
884 goto out_bad_devname;
885 len = end - dev_name;
887 /* kill possible hostname list: not supported */
888 comma = memchr(dev_name, ',', len);
890 len = comma - dev_name;
896 kfree(ctx->nfs_server.hostname);
898 /* N.B. caller will free nfs_server.hostname in all cases */
899 ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
900 if (!ctx->nfs_server.hostname)
903 if (len > maxpathlen)
905 ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
906 if (!ctx->nfs_server.export_path)
909 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", ctx->nfs_server.export_path);
913 return nfs_invalf(fc, "NFS: device name not in host:path format");
915 nfs_errorf(fc, "NFS: not enough memory to parse device name");
918 nfs_errorf(fc, "NFS: server hostname too long");
919 return -ENAMETOOLONG;
921 nfs_errorf(fc, "NFS: export pathname too long");
922 return -ENAMETOOLONG;
925 static inline bool is_remount_fc(struct fs_context *fc)
927 return fc->root != NULL;
931 * Parse monolithic NFS2/NFS3 mount data
932 * - fills in the mount root filehandle
934 * For option strings, user space handles the following behaviors:
936 * + DNS: mapping server host name to IP address ("addr=" option)
938 * + failure mode: how to behave if a mount request can't be handled
939 * immediately ("fg/bg" option)
941 * + retry: how often to retry a mount request ("retry=" option)
943 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
944 * mountproto=tcp after mountproto=udp, and so on
946 static int nfs23_parse_monolithic(struct fs_context *fc,
947 struct nfs_mount_data *data)
949 struct nfs_fs_context *ctx = nfs_fc2context(fc);
950 struct nfs_fh *mntfh = ctx->mntfh;
951 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
952 int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
958 ctx->version = NFS_DEFAULT_VERSION;
959 switch (data->version) {
967 if (data->flags & NFS_MOUNT_VER3)
969 data->root.size = NFS2_FHSIZE;
970 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
971 /* Turn off security negotiation */
972 extra_flags |= NFS_MOUNT_SECFLAVOUR;
975 if (data->flags & NFS_MOUNT_SECFLAVOUR)
979 memset(data->context, 0, sizeof(data->context));
982 if (data->flags & NFS_MOUNT_VER3) {
983 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
985 mntfh->size = data->root.size;
988 mntfh->size = NFS2_FHSIZE;
993 memcpy(mntfh->data, data->root.data, mntfh->size);
994 if (mntfh->size < sizeof(mntfh->data))
995 memset(mntfh->data + mntfh->size, 0,
996 sizeof(mntfh->data) - mntfh->size);
999 * for proto == XPRT_TRANSPORT_UDP, which is what uses
1000 * to_exponential, implying shift: limit the shift value
1001 * to BITS_PER_LONG (majortimeo is unsigned long)
1003 if (!(data->flags & NFS_MOUNT_TCP)) /* this will be UDP */
1004 if (data->retrans >= 64) /* shift value is too large */
1005 goto out_invalid_data;
1008 * Translate to nfs_fs_context, which nfs_fill_super
1011 ctx->flags = data->flags & NFS_MOUNT_FLAGMASK;
1012 ctx->flags |= extra_flags;
1013 ctx->rsize = data->rsize;
1014 ctx->wsize = data->wsize;
1015 ctx->timeo = data->timeo;
1016 ctx->retrans = data->retrans;
1017 ctx->acregmin = data->acregmin;
1018 ctx->acregmax = data->acregmax;
1019 ctx->acdirmin = data->acdirmin;
1020 ctx->acdirmax = data->acdirmax;
1021 ctx->need_mount = false;
1023 memcpy(sap, &data->addr, sizeof(data->addr));
1024 ctx->nfs_server.addrlen = sizeof(data->addr);
1025 ctx->nfs_server.port = ntohs(data->addr.sin_port);
1026 if (sap->sa_family != AF_INET ||
1027 !nfs_verify_server_address(sap))
1028 goto out_no_address;
1030 if (!(data->flags & NFS_MOUNT_TCP))
1031 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1032 /* N.B. caller will free nfs_server.hostname in all cases */
1033 ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1034 if (!ctx->nfs_server.hostname)
1037 ctx->namlen = data->namlen;
1038 ctx->bsize = data->bsize;
1040 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1041 ctx->selected_flavor = data->pseudoflavor;
1043 ctx->selected_flavor = RPC_AUTH_UNIX;
1045 if (!(data->flags & NFS_MOUNT_NONLM))
1046 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1047 NFS_MOUNT_LOCAL_FCNTL);
1049 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1050 NFS_MOUNT_LOCAL_FCNTL);
1053 * The legacy version 6 binary mount data from userspace has a
1054 * field used only to transport selinux information into the
1055 * kernel. To continue to support that functionality we
1056 * have a touch of selinux knowledge here in the NFS code. The
1057 * userspace code converted context=blah to just blah so we are
1058 * converting back to the full string selinux understands.
1060 if (data->context[0]){
1061 #ifdef CONFIG_SECURITY_SELINUX
1064 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1065 ret = vfs_parse_fs_string(fc, "context",
1066 data->context, strlen(data->context));
1079 ret = nfs_validate_transport_protocol(fc, ctx);
1083 ctx->skip_reconfig_option_check = true;
1087 return generic_parse_monolithic(fc, data);
1090 if (is_remount_fc(fc)) {
1091 ctx->skip_reconfig_option_check = true;
1094 return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
1097 return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
1100 return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
1103 dfprintk(MOUNT, "NFS: not enough memory to handle mount options");
1107 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1110 return nfs_invalf(fc, "NFS: invalid root filehandle");
1113 return nfs_invalf(fc, "NFS: invalid binary mount data");
1116 #if IS_ENABLED(CONFIG_NFS_V4)
1117 struct compat_nfs_string {
1122 static inline void compat_nfs_string(struct nfs_string *dst,
1123 struct compat_nfs_string *src)
1125 dst->data = compat_ptr(src->data);
1126 dst->len = src->len;
1129 struct compat_nfs4_mount_data_v1 {
1130 compat_int_t version;
1135 compat_int_t retrans;
1136 compat_int_t acregmin;
1137 compat_int_t acregmax;
1138 compat_int_t acdirmin;
1139 compat_int_t acdirmax;
1140 struct compat_nfs_string client_addr;
1141 struct compat_nfs_string mnt_path;
1142 struct compat_nfs_string hostname;
1143 compat_uint_t host_addrlen;
1144 compat_uptr_t host_addr;
1146 compat_int_t auth_flavourlen;
1147 compat_uptr_t auth_flavours;
1150 static void nfs4_compat_mount_data_conv(struct nfs4_mount_data *data)
1152 struct compat_nfs4_mount_data_v1 *compat =
1153 (struct compat_nfs4_mount_data_v1 *)data;
1155 /* copy the fields backwards */
1156 data->auth_flavours = compat_ptr(compat->auth_flavours);
1157 data->auth_flavourlen = compat->auth_flavourlen;
1158 data->proto = compat->proto;
1159 data->host_addr = compat_ptr(compat->host_addr);
1160 data->host_addrlen = compat->host_addrlen;
1161 compat_nfs_string(&data->hostname, &compat->hostname);
1162 compat_nfs_string(&data->mnt_path, &compat->mnt_path);
1163 compat_nfs_string(&data->client_addr, &compat->client_addr);
1164 data->acdirmax = compat->acdirmax;
1165 data->acdirmin = compat->acdirmin;
1166 data->acregmax = compat->acregmax;
1167 data->acregmin = compat->acregmin;
1168 data->retrans = compat->retrans;
1169 data->timeo = compat->timeo;
1170 data->wsize = compat->wsize;
1171 data->rsize = compat->rsize;
1172 data->flags = compat->flags;
1173 data->version = compat->version;
1177 * Validate NFSv4 mount options
1179 static int nfs4_parse_monolithic(struct fs_context *fc,
1180 struct nfs4_mount_data *data)
1182 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1183 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
1188 if (is_remount_fc(fc))
1190 return nfs_invalf(fc,
1191 "NFS4: mount program didn't pass any mount data");
1196 if (data->version != 1)
1197 return generic_parse_monolithic(fc, data);
1199 if (in_compat_syscall())
1200 nfs4_compat_mount_data_conv(data);
1202 if (data->host_addrlen > sizeof(ctx->nfs_server.address))
1203 goto out_no_address;
1204 if (data->host_addrlen == 0)
1205 goto out_no_address;
1206 ctx->nfs_server.addrlen = data->host_addrlen;
1207 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1209 if (!nfs_verify_server_address(sap))
1210 goto out_no_address;
1211 ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1213 if (data->auth_flavourlen) {
1214 rpc_authflavor_t pseudoflavor;
1216 if (data->auth_flavourlen > 1)
1217 goto out_inval_auth;
1218 if (copy_from_user(&pseudoflavor, data->auth_flavours,
1219 sizeof(pseudoflavor)))
1221 ctx->selected_flavor = pseudoflavor;
1223 ctx->selected_flavor = RPC_AUTH_UNIX;
1226 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1229 ctx->nfs_server.hostname = c;
1231 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1234 ctx->nfs_server.export_path = c;
1235 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1237 c = strndup_user(data->client_addr.data, 16);
1240 ctx->client_address = c;
1243 * Translate to nfs_fs_context, which nfs_fill_super
1247 ctx->flags = data->flags & NFS4_MOUNT_FLAGMASK;
1248 ctx->rsize = data->rsize;
1249 ctx->wsize = data->wsize;
1250 ctx->timeo = data->timeo;
1251 ctx->retrans = data->retrans;
1252 ctx->acregmin = data->acregmin;
1253 ctx->acregmax = data->acregmax;
1254 ctx->acdirmin = data->acdirmin;
1255 ctx->acdirmax = data->acdirmax;
1256 ctx->nfs_server.protocol = data->proto;
1257 ret = nfs_validate_transport_protocol(fc, ctx);
1261 ctx->skip_reconfig_option_check = true;
1265 return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1266 data->auth_flavourlen);
1269 return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
1274 * Parse a monolithic block of data from sys_mount().
1276 static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1279 if (fc->fs_type == &nfs_fs_type)
1280 return nfs23_parse_monolithic(fc, data);
1282 #if IS_ENABLED(CONFIG_NFS_V4)
1283 if (fc->fs_type == &nfs4_fs_type)
1284 return nfs4_parse_monolithic(fc, data);
1287 return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
1291 * Validate the preparsed information in the config.
1293 static int nfs_fs_context_validate(struct fs_context *fc)
1295 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1296 struct nfs_subversion *nfs_mod;
1297 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
1298 int max_namelen = PAGE_SIZE;
1299 int max_pathlen = NFS_MAXPATHLEN;
1304 goto out_no_device_name;
1306 /* Check for sanity first. */
1307 if (ctx->minorversion && ctx->version != 4)
1308 goto out_minorversion_mismatch;
1310 if (ctx->options & NFS_OPTION_MIGRATION &&
1311 (ctx->version != 4 || ctx->minorversion != 0))
1312 goto out_migration_misuse;
1314 /* Verify that any proto=/mountproto= options match the address
1315 * families in the addr=/mountaddr= options.
1317 if (ctx->protofamily != AF_UNSPEC &&
1318 ctx->protofamily != ctx->nfs_server.address.sa_family)
1319 goto out_proto_mismatch;
1321 if (ctx->mountfamily != AF_UNSPEC) {
1322 if (ctx->mount_server.addrlen) {
1323 if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1324 goto out_mountproto_mismatch;
1326 if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1327 goto out_mountproto_mismatch;
1331 if (!nfs_verify_server_address(sap))
1332 goto out_no_address;
1334 ret = nfs_validate_transport_protocol(fc, ctx);
1338 if (ctx->version == 4) {
1339 if (IS_ENABLED(CONFIG_NFS_V4)) {
1340 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1341 port = NFS_RDMA_PORT;
1344 max_namelen = NFS4_MAXNAMLEN;
1345 max_pathlen = NFS4_MAXPATHLEN;
1346 ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1347 NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1348 NFS_MOUNT_LOCAL_FCNTL);
1350 goto out_v4_not_compiled;
1353 nfs_set_mount_transport_protocol(ctx);
1354 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1355 port = NFS_RDMA_PORT;
1358 nfs_set_port(sap, &ctx->nfs_server.port, port);
1360 ret = nfs_parse_source(fc, max_namelen, max_pathlen);
1364 /* Load the NFS protocol module if we haven't done so yet */
1365 if (!ctx->nfs_mod) {
1366 nfs_mod = get_nfs_version(ctx->version);
1367 if (IS_ERR(nfs_mod)) {
1368 ret = PTR_ERR(nfs_mod);
1369 goto out_version_unavailable;
1371 ctx->nfs_mod = nfs_mod;
1374 /* Ensure the filesystem context has the correct fs_type */
1375 if (fc->fs_type != ctx->nfs_mod->nfs_fs) {
1376 module_put(fc->fs_type->owner);
1377 __module_get(ctx->nfs_mod->nfs_fs->owner);
1378 fc->fs_type = ctx->nfs_mod->nfs_fs;
1383 return nfs_invalf(fc, "NFS: Device name not specified");
1384 out_v4_not_compiled:
1385 nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
1386 return -EPROTONOSUPPORT;
1388 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
1389 out_mountproto_mismatch:
1390 return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
1392 return nfs_invalf(fc, "NFS: Server address does not match proto= option");
1393 out_minorversion_mismatch:
1394 return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
1395 ctx->version, ctx->minorversion);
1396 out_migration_misuse:
1397 return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
1398 out_version_unavailable:
1399 nfs_errorf(fc, "NFS: Version unavailable");
1404 * Create an NFS superblock by the appropriate method.
1406 static int nfs_get_tree(struct fs_context *fc)
1408 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1409 int err = nfs_fs_context_validate(fc);
1414 return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
1416 return nfs_get_tree_common(fc);
1420 * Handle duplication of a configuration. The caller copied *src into *sc, but
1421 * it can't deal with resource pointers in the filesystem context, so we have
1422 * to do that. We need to clear pointers, copy data or get extra refs as
1425 static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1427 struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1429 ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1433 ctx->mntfh = nfs_alloc_fhandle();
1438 nfs_copy_fh(ctx->mntfh, src->mntfh);
1440 __module_get(ctx->nfs_mod->owner);
1441 ctx->client_address = NULL;
1442 ctx->mount_server.hostname = NULL;
1443 ctx->nfs_server.export_path = NULL;
1444 ctx->nfs_server.hostname = NULL;
1445 ctx->fscache_uniq = NULL;
1446 ctx->clone_data.fattr = NULL;
1447 fc->fs_private = ctx;
1451 static void nfs_fs_context_free(struct fs_context *fc)
1453 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1457 nfs_free_server(ctx->server);
1459 put_nfs_version(ctx->nfs_mod);
1460 kfree(ctx->client_address);
1461 kfree(ctx->mount_server.hostname);
1462 kfree(ctx->nfs_server.export_path);
1463 kfree(ctx->nfs_server.hostname);
1464 kfree(ctx->fscache_uniq);
1465 nfs_free_fhandle(ctx->mntfh);
1466 nfs_free_fattr(ctx->clone_data.fattr);
1471 static const struct fs_context_operations nfs_fs_context_ops = {
1472 .free = nfs_fs_context_free,
1473 .dup = nfs_fs_context_dup,
1474 .parse_param = nfs_fs_context_parse_param,
1475 .parse_monolithic = nfs_fs_context_parse_monolithic,
1476 .get_tree = nfs_get_tree,
1477 .reconfigure = nfs_reconfigure,
1481 * Prepare superblock configuration. We use the namespaces attached to the
1482 * context. This may be the current process's namespaces, or it may be a
1483 * container's namespaces.
1485 static int nfs_init_fs_context(struct fs_context *fc)
1487 struct nfs_fs_context *ctx;
1489 ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1493 ctx->mntfh = nfs_alloc_fhandle();
1494 if (unlikely(!ctx->mntfh)) {
1499 ctx->protofamily = AF_UNSPEC;
1500 ctx->mountfamily = AF_UNSPEC;
1501 ctx->mount_server.port = NFS_UNSPEC_PORT;
1504 /* reconfigure, start with the current config */
1505 struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1506 struct net *net = nfss->nfs_client->cl_net;
1508 ctx->flags = nfss->flags;
1509 ctx->rsize = nfss->rsize;
1510 ctx->wsize = nfss->wsize;
1511 ctx->retrans = nfss->client->cl_timeout->to_retries;
1512 ctx->selected_flavor = nfss->client->cl_auth->au_flavor;
1513 ctx->acregmin = nfss->acregmin / HZ;
1514 ctx->acregmax = nfss->acregmax / HZ;
1515 ctx->acdirmin = nfss->acdirmin / HZ;
1516 ctx->acdirmax = nfss->acdirmax / HZ;
1517 ctx->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1518 ctx->nfs_server.port = nfss->port;
1519 ctx->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1520 ctx->version = nfss->nfs_client->rpc_ops->version;
1521 ctx->minorversion = nfss->nfs_client->cl_minorversion;
1523 memcpy(&ctx->nfs_server.address, &nfss->nfs_client->cl_addr,
1524 ctx->nfs_server.addrlen);
1526 if (fc->net_ns != net) {
1527 put_net(fc->net_ns);
1528 fc->net_ns = get_net(net);
1531 ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1532 __module_get(ctx->nfs_mod->owner);
1535 ctx->timeo = NFS_UNSPEC_TIMEO;
1536 ctx->retrans = NFS_UNSPEC_RETRANS;
1537 ctx->acregmin = NFS_DEF_ACREGMIN;
1538 ctx->acregmax = NFS_DEF_ACREGMAX;
1539 ctx->acdirmin = NFS_DEF_ACDIRMIN;
1540 ctx->acdirmax = NFS_DEF_ACDIRMAX;
1541 ctx->nfs_server.port = NFS_UNSPEC_PORT;
1542 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1543 ctx->selected_flavor = RPC_AUTH_MAXFLAVOR;
1544 ctx->minorversion = 0;
1545 ctx->need_mount = true;
1547 fc->s_iflags |= SB_I_STABLE_WRITES;
1549 fc->fs_private = ctx;
1550 fc->ops = &nfs_fs_context_ops;
1554 struct file_system_type nfs_fs_type = {
1555 .owner = THIS_MODULE,
1557 .init_fs_context = nfs_init_fs_context,
1558 .parameters = nfs_fs_parameters,
1559 .kill_sb = nfs_kill_super,
1560 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1562 MODULE_ALIAS_FS("nfs");
1563 EXPORT_SYMBOL_GPL(nfs_fs_type);
1565 #if IS_ENABLED(CONFIG_NFS_V4)
1566 struct file_system_type nfs4_fs_type = {
1567 .owner = THIS_MODULE,
1569 .init_fs_context = nfs_init_fs_context,
1570 .parameters = nfs_fs_parameters,
1571 .kill_sb = nfs_kill_super,
1572 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1574 MODULE_ALIAS_FS("nfs4");
1575 MODULE_ALIAS("nfs4");
1576 EXPORT_SYMBOL_GPL(nfs4_fs_type);
1577 #endif /* CONFIG_NFS_V4 */