blob: 66949da0e82779891350964bc45e87872f9a8b78 [file] [log] [blame]
David Howells9954bf92019-12-10 07:31:04 -05001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/fs/nfs/fs_context.c
4 *
5 * Copyright (C) 1992 Rick Sladkey
David Howellsf2aedb72019-12-10 07:31:13 -05006 * Conversion to new mount api Copyright (C) David Howells
David Howells9954bf92019-12-10 07:31:04 -05007 *
8 * NFS mount handling.
9 *
10 * Split from fs/nfs/super.c by David Howells <dhowells@redhat.com>
11 */
12
13#include <linux/module.h>
14#include <linux/fs.h>
Scott Mayhewe38bb232019-12-10 07:31:12 -050015#include <linux/fs_context.h>
16#include <linux/fs_parser.h>
David Howells9954bf92019-12-10 07:31:04 -050017#include <linux/nfs_fs.h>
18#include <linux/nfs_mount.h>
19#include <linux/nfs4_mount.h>
20#include "nfs.h"
21#include "internal.h"
22
23#define NFSDBG_FACILITY NFSDBG_MOUNT
24
25#if IS_ENABLED(CONFIG_NFS_V3)
26#define NFS_DEFAULT_VERSION 3
27#else
28#define NFS_DEFAULT_VERSION 2
29#endif
30
31#define NFS_MAX_CONNECTIONS 16
32
Scott Mayhewe38bb232019-12-10 07:31:12 -050033enum nfs_param {
34 Opt_ac,
35 Opt_acdirmax,
36 Opt_acdirmin,
37 Opt_acl,
38 Opt_acregmax,
39 Opt_acregmin,
David Howells9954bf92019-12-10 07:31:04 -050040 Opt_actimeo,
Scott Mayhewe38bb232019-12-10 07:31:12 -050041 Opt_addr,
42 Opt_bg,
43 Opt_bsize,
44 Opt_clientaddr,
45 Opt_cto,
46 Opt_fg,
47 Opt_fscache,
Al Viro48ce73b2019-12-17 20:03:59 -050048 Opt_fscache_flag,
Scott Mayhewe38bb232019-12-10 07:31:12 -050049 Opt_hard,
50 Opt_intr,
David Howells9954bf92019-12-10 07:31:04 -050051 Opt_local_lock,
Scott Mayhewe38bb232019-12-10 07:31:12 -050052 Opt_lock,
53 Opt_lookupcache,
54 Opt_migration,
55 Opt_minorversion,
56 Opt_mountaddr,
57 Opt_mounthost,
58 Opt_mountport,
59 Opt_mountproto,
60 Opt_mountvers,
61 Opt_namelen,
62 Opt_nconnect,
63 Opt_port,
64 Opt_posix,
65 Opt_proto,
66 Opt_rdirplus,
67 Opt_rdma,
68 Opt_resvport,
69 Opt_retrans,
70 Opt_retry,
71 Opt_rsize,
72 Opt_sec,
73 Opt_sharecache,
74 Opt_sloppy,
75 Opt_soft,
76 Opt_softerr,
Trond Myklebustc74dfe92020-01-06 15:39:37 -050077 Opt_softreval,
Scott Mayhewe38bb232019-12-10 07:31:12 -050078 Opt_source,
79 Opt_tcp,
80 Opt_timeo,
81 Opt_udp,
82 Opt_v,
83 Opt_vers,
84 Opt_wsize,
David Howells9954bf92019-12-10 07:31:04 -050085};
86
Al Viro2710c957a2019-09-06 22:12:08 -040087enum {
88 Opt_local_lock_all,
89 Opt_local_lock_flock,
90 Opt_local_lock_none,
91 Opt_local_lock_posix,
92};
93
Al Viro5eede622019-12-16 13:33:32 -050094static const struct constant_table nfs_param_enums_local_lock[] = {
Al Viro2710c957a2019-09-06 22:12:08 -040095 { "all", Opt_local_lock_all },
96 { "flock", Opt_local_lock_flock },
97 { "none", Opt_local_lock_none },
98 {}
99};
100
101enum {
102 Opt_lookupcache_all,
103 Opt_lookupcache_none,
104 Opt_lookupcache_positive,
105};
106
Al Viro5eede622019-12-16 13:33:32 -0500107static const struct constant_table nfs_param_enums_lookupcache[] = {
Al Viro2710c957a2019-09-06 22:12:08 -0400108 { "all", Opt_lookupcache_all },
109 { "none", Opt_lookupcache_none },
110 { "pos", Opt_lookupcache_positive },
111 { "positive", Opt_lookupcache_positive },
112 {}
113};
114
Al Virod7167b12019-09-07 07:23:15 -0400115static const struct fs_parameter_spec nfs_fs_parameters[] = {
Scott Mayhewe38bb232019-12-10 07:31:12 -0500116 fsparam_flag_no("ac", Opt_ac),
117 fsparam_u32 ("acdirmax", Opt_acdirmax),
118 fsparam_u32 ("acdirmin", Opt_acdirmin),
119 fsparam_flag_no("acl", Opt_acl),
120 fsparam_u32 ("acregmax", Opt_acregmax),
121 fsparam_u32 ("acregmin", Opt_acregmin),
122 fsparam_u32 ("actimeo", Opt_actimeo),
123 fsparam_string("addr", Opt_addr),
124 fsparam_flag ("bg", Opt_bg),
125 fsparam_u32 ("bsize", Opt_bsize),
126 fsparam_string("clientaddr", Opt_clientaddr),
127 fsparam_flag_no("cto", Opt_cto),
128 fsparam_flag ("fg", Opt_fg),
Al Viro48ce73b2019-12-17 20:03:59 -0500129 fsparam_flag_no("fsc", Opt_fscache_flag),
130 fsparam_string("fsc", Opt_fscache),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500131 fsparam_flag ("hard", Opt_hard),
Al Viro328de522019-12-18 00:02:31 -0500132 __fsparam(NULL, "intr", Opt_intr,
Al Viro2710c957a2019-09-06 22:12:08 -0400133 fs_param_neg_with_no|fs_param_deprecated, NULL),
134 fsparam_enum ("local_lock", Opt_local_lock, nfs_param_enums_local_lock),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500135 fsparam_flag_no("lock", Opt_lock),
Al Viro2710c957a2019-09-06 22:12:08 -0400136 fsparam_enum ("lookupcache", Opt_lookupcache, nfs_param_enums_lookupcache),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500137 fsparam_flag_no("migration", Opt_migration),
138 fsparam_u32 ("minorversion", Opt_minorversion),
139 fsparam_string("mountaddr", Opt_mountaddr),
140 fsparam_string("mounthost", Opt_mounthost),
141 fsparam_u32 ("mountport", Opt_mountport),
142 fsparam_string("mountproto", Opt_mountproto),
143 fsparam_u32 ("mountvers", Opt_mountvers),
144 fsparam_u32 ("namlen", Opt_namelen),
145 fsparam_u32 ("nconnect", Opt_nconnect),
146 fsparam_string("nfsvers", Opt_vers),
147 fsparam_u32 ("port", Opt_port),
148 fsparam_flag_no("posix", Opt_posix),
149 fsparam_string("proto", Opt_proto),
150 fsparam_flag_no("rdirplus", Opt_rdirplus),
151 fsparam_flag ("rdma", Opt_rdma),
152 fsparam_flag_no("resvport", Opt_resvport),
153 fsparam_u32 ("retrans", Opt_retrans),
154 fsparam_string("retry", Opt_retry),
155 fsparam_u32 ("rsize", Opt_rsize),
156 fsparam_string("sec", Opt_sec),
157 fsparam_flag_no("sharecache", Opt_sharecache),
158 fsparam_flag ("sloppy", Opt_sloppy),
159 fsparam_flag ("soft", Opt_soft),
160 fsparam_flag ("softerr", Opt_softerr),
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500161 fsparam_flag ("softreval", Opt_softreval),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500162 fsparam_string("source", Opt_source),
163 fsparam_flag ("tcp", Opt_tcp),
164 fsparam_u32 ("timeo", Opt_timeo),
165 fsparam_flag ("udp", Opt_udp),
166 fsparam_flag ("v2", Opt_v),
167 fsparam_flag ("v3", Opt_v),
168 fsparam_flag ("v4", Opt_v),
169 fsparam_flag ("v4.0", Opt_v),
170 fsparam_flag ("v4.1", Opt_v),
171 fsparam_flag ("v4.2", Opt_v),
172 fsparam_string("vers", Opt_vers),
173 fsparam_u32 ("wsize", Opt_wsize),
174 {}
David Howells9954bf92019-12-10 07:31:04 -0500175};
176
Scott Mayhewe38bb232019-12-10 07:31:12 -0500177enum {
178 Opt_vers_2,
179 Opt_vers_3,
180 Opt_vers_4,
181 Opt_vers_4_0,
182 Opt_vers_4_1,
183 Opt_vers_4_2,
184};
185
186static const struct constant_table nfs_vers_tokens[] = {
187 { "2", Opt_vers_2 },
188 { "3", Opt_vers_3 },
189 { "4", Opt_vers_4 },
190 { "4.0", Opt_vers_4_0 },
191 { "4.1", Opt_vers_4_1 },
192 { "4.2", Opt_vers_4_2 },
Scott Mayhew529af902020-04-02 17:20:44 -0400193 {}
Scott Mayhewe38bb232019-12-10 07:31:12 -0500194};
195
196enum {
197 Opt_xprt_rdma,
198 Opt_xprt_rdma6,
199 Opt_xprt_tcp,
200 Opt_xprt_tcp6,
201 Opt_xprt_udp,
202 Opt_xprt_udp6,
203 nr__Opt_xprt
204};
205
Scott Mayhew529af902020-04-02 17:20:44 -0400206static const struct constant_table nfs_xprt_protocol_tokens[] = {
Scott Mayhewe38bb232019-12-10 07:31:12 -0500207 { "rdma", Opt_xprt_rdma },
208 { "rdma6", Opt_xprt_rdma6 },
209 { "tcp", Opt_xprt_tcp },
210 { "tcp6", Opt_xprt_tcp6 },
211 { "udp", Opt_xprt_udp },
212 { "udp6", Opt_xprt_udp6 },
Scott Mayhew529af902020-04-02 17:20:44 -0400213 {}
Scott Mayhewe38bb232019-12-10 07:31:12 -0500214};
215
216enum {
217 Opt_sec_krb5,
218 Opt_sec_krb5i,
219 Opt_sec_krb5p,
220 Opt_sec_lkey,
221 Opt_sec_lkeyi,
222 Opt_sec_lkeyp,
223 Opt_sec_none,
224 Opt_sec_spkm,
225 Opt_sec_spkmi,
226 Opt_sec_spkmp,
227 Opt_sec_sys,
228 nr__Opt_sec
229};
230
231static const struct constant_table nfs_secflavor_tokens[] = {
232 { "krb5", Opt_sec_krb5 },
233 { "krb5i", Opt_sec_krb5i },
234 { "krb5p", Opt_sec_krb5p },
235 { "lkey", Opt_sec_lkey },
236 { "lkeyi", Opt_sec_lkeyi },
237 { "lkeyp", Opt_sec_lkeyp },
238 { "none", Opt_sec_none },
239 { "null", Opt_sec_none },
240 { "spkm3", Opt_sec_spkm },
241 { "spkm3i", Opt_sec_spkmi },
242 { "spkm3p", Opt_sec_spkmp },
243 { "sys", Opt_sec_sys },
Scott Mayhew529af902020-04-02 17:20:44 -0400244 {}
David Howells9954bf92019-12-10 07:31:04 -0500245};
246
David Howells9954bf92019-12-10 07:31:04 -0500247/*
248 * Sanity-check a server address provided by the mount command.
249 *
250 * Address family must be initialized, and address must not be
251 * the ANY address for that family.
252 */
253static int nfs_verify_server_address(struct sockaddr *addr)
254{
255 switch (addr->sa_family) {
256 case AF_INET: {
257 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
258 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
259 }
260 case AF_INET6: {
261 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
262 return !ipv6_addr_any(sa);
263 }
264 }
265
266 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
267 return 0;
268}
269
270/*
271 * Sanity check the NFS transport protocol.
272 *
273 */
David Howells5eb005c2019-12-10 07:31:06 -0500274static void nfs_validate_transport_protocol(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500275{
David Howells5eb005c2019-12-10 07:31:06 -0500276 switch (ctx->nfs_server.protocol) {
David Howells9954bf92019-12-10 07:31:04 -0500277 case XPRT_TRANSPORT_UDP:
278 case XPRT_TRANSPORT_TCP:
279 case XPRT_TRANSPORT_RDMA:
280 break;
281 default:
David Howells5eb005c2019-12-10 07:31:06 -0500282 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500283 }
284}
285
286/*
287 * For text based NFSv2/v3 mounts, the mount protocol transport default
288 * settings should depend upon the specified NFS transport.
289 */
David Howells5eb005c2019-12-10 07:31:06 -0500290static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500291{
David Howells5eb005c2019-12-10 07:31:06 -0500292 nfs_validate_transport_protocol(ctx);
David Howells9954bf92019-12-10 07:31:04 -0500293
David Howells5eb005c2019-12-10 07:31:06 -0500294 if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
295 ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
David Howells9954bf92019-12-10 07:31:04 -0500296 return;
David Howells5eb005c2019-12-10 07:31:06 -0500297 switch (ctx->nfs_server.protocol) {
David Howells9954bf92019-12-10 07:31:04 -0500298 case XPRT_TRANSPORT_UDP:
David Howells5eb005c2019-12-10 07:31:06 -0500299 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500300 break;
301 case XPRT_TRANSPORT_TCP:
302 case XPRT_TRANSPORT_RDMA:
David Howells5eb005c2019-12-10 07:31:06 -0500303 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500304 }
305}
306
307/*
308 * Add 'flavor' to 'auth_info' if not already present.
309 * Returns true if 'flavor' ends up in the list, false otherwise
310 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500311static int nfs_auth_info_add(struct fs_context *fc,
David Howellse5581002019-12-10 07:31:10 -0500312 struct nfs_auth_info *auth_info,
313 rpc_authflavor_t flavor)
David Howells9954bf92019-12-10 07:31:04 -0500314{
315 unsigned int i;
316 unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
317
318 /* make sure this flavor isn't already in the list */
319 for (i = 0; i < auth_info->flavor_len; i++) {
320 if (flavor == auth_info->flavors[i])
David Howellse5581002019-12-10 07:31:10 -0500321 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500322 }
323
Scott Mayhewce8866f02019-12-10 07:31:15 -0500324 if (auth_info->flavor_len + 1 >= max_flavor_len)
325 return nfs_invalf(fc, "NFS: too many sec= flavors");
David Howells9954bf92019-12-10 07:31:04 -0500326
327 auth_info->flavors[auth_info->flavor_len++] = flavor;
David Howellse5581002019-12-10 07:31:10 -0500328 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500329}
330
331/*
332 * Parse the value of the 'sec=' option.
333 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500334static int nfs_parse_security_flavors(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500335 struct fs_parameter *param)
David Howells9954bf92019-12-10 07:31:04 -0500336{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500337 struct nfs_fs_context *ctx = nfs_fc2context(fc);
David Howells9954bf92019-12-10 07:31:04 -0500338 rpc_authflavor_t pseudoflavor;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500339 char *string = param->string, *p;
David Howellse5581002019-12-10 07:31:10 -0500340 int ret;
David Howells9954bf92019-12-10 07:31:04 -0500341
Scott Mayhewe38bb232019-12-10 07:31:12 -0500342 dfprintk(MOUNT, "NFS: parsing %s=%s option\n", param->key, param->string);
David Howells9954bf92019-12-10 07:31:04 -0500343
Scott Mayhewe38bb232019-12-10 07:31:12 -0500344 while ((p = strsep(&string, ":")) != NULL) {
345 if (!*p)
346 continue;
347 switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
David Howells9954bf92019-12-10 07:31:04 -0500348 case Opt_sec_none:
349 pseudoflavor = RPC_AUTH_NULL;
350 break;
351 case Opt_sec_sys:
352 pseudoflavor = RPC_AUTH_UNIX;
353 break;
354 case Opt_sec_krb5:
355 pseudoflavor = RPC_AUTH_GSS_KRB5;
356 break;
357 case Opt_sec_krb5i:
358 pseudoflavor = RPC_AUTH_GSS_KRB5I;
359 break;
360 case Opt_sec_krb5p:
361 pseudoflavor = RPC_AUTH_GSS_KRB5P;
362 break;
363 case Opt_sec_lkey:
364 pseudoflavor = RPC_AUTH_GSS_LKEY;
365 break;
366 case Opt_sec_lkeyi:
367 pseudoflavor = RPC_AUTH_GSS_LKEYI;
368 break;
369 case Opt_sec_lkeyp:
370 pseudoflavor = RPC_AUTH_GSS_LKEYP;
371 break;
372 case Opt_sec_spkm:
373 pseudoflavor = RPC_AUTH_GSS_SPKM;
374 break;
375 case Opt_sec_spkmi:
376 pseudoflavor = RPC_AUTH_GSS_SPKMI;
377 break;
378 case Opt_sec_spkmp:
379 pseudoflavor = RPC_AUTH_GSS_SPKMP;
380 break;
381 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500382 return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
David Howells9954bf92019-12-10 07:31:04 -0500383 }
384
Scott Mayhew62a55d02019-12-10 07:31:14 -0500385 ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
David Howellse5581002019-12-10 07:31:10 -0500386 if (ret < 0)
387 return ret;
David Howells9954bf92019-12-10 07:31:04 -0500388 }
389
David Howellse5581002019-12-10 07:31:10 -0500390 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500391}
392
Scott Mayhew62a55d02019-12-10 07:31:14 -0500393static int nfs_parse_version_string(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500394 const char *string)
David Howells9954bf92019-12-10 07:31:04 -0500395{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500396 struct nfs_fs_context *ctx = nfs_fc2context(fc);
397
David Howells5eb005c2019-12-10 07:31:06 -0500398 ctx->flags &= ~NFS_MOUNT_VER3;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500399 switch (lookup_constant(nfs_vers_tokens, string, -1)) {
David Howells9954bf92019-12-10 07:31:04 -0500400 case Opt_vers_2:
David Howells5eb005c2019-12-10 07:31:06 -0500401 ctx->version = 2;
David Howells9954bf92019-12-10 07:31:04 -0500402 break;
403 case Opt_vers_3:
David Howells5eb005c2019-12-10 07:31:06 -0500404 ctx->flags |= NFS_MOUNT_VER3;
405 ctx->version = 3;
David Howells9954bf92019-12-10 07:31:04 -0500406 break;
407 case Opt_vers_4:
408 /* Backward compatibility option. In future,
409 * the mount program should always supply
410 * a NFSv4 minor version number.
411 */
David Howells5eb005c2019-12-10 07:31:06 -0500412 ctx->version = 4;
David Howells9954bf92019-12-10 07:31:04 -0500413 break;
414 case Opt_vers_4_0:
David Howells5eb005c2019-12-10 07:31:06 -0500415 ctx->version = 4;
416 ctx->minorversion = 0;
David Howells9954bf92019-12-10 07:31:04 -0500417 break;
418 case Opt_vers_4_1:
David Howells5eb005c2019-12-10 07:31:06 -0500419 ctx->version = 4;
420 ctx->minorversion = 1;
David Howells9954bf92019-12-10 07:31:04 -0500421 break;
422 case Opt_vers_4_2:
David Howells5eb005c2019-12-10 07:31:06 -0500423 ctx->version = 4;
424 ctx->minorversion = 2;
David Howells9954bf92019-12-10 07:31:04 -0500425 break;
426 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500427 return nfs_invalf(fc, "NFS: Unsupported NFS version");
David Howells9954bf92019-12-10 07:31:04 -0500428 }
David Howellse5581002019-12-10 07:31:10 -0500429 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500430}
431
David Howells9954bf92019-12-10 07:31:04 -0500432/*
Scott Mayhewe38bb232019-12-10 07:31:12 -0500433 * Parse a single mount parameter.
David Howells9954bf92019-12-10 07:31:04 -0500434 */
David Howellsf2aedb72019-12-10 07:31:13 -0500435static int nfs_fs_context_parse_param(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500436 struct fs_parameter *param)
David Howells9954bf92019-12-10 07:31:04 -0500437{
Scott Mayhewe38bb232019-12-10 07:31:12 -0500438 struct fs_parse_result result;
David Howellsf2aedb72019-12-10 07:31:13 -0500439 struct nfs_fs_context *ctx = nfs_fc2context(fc);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500440 unsigned short protofamily, mountfamily;
441 unsigned int len;
442 int ret, opt;
David Howells9954bf92019-12-10 07:31:04 -0500443
Scott Mayhewe38bb232019-12-10 07:31:12 -0500444 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", param->key);
David Howells9954bf92019-12-10 07:31:04 -0500445
Al Virod7167b12019-09-07 07:23:15 -0400446 opt = fs_parse(fc, nfs_fs_parameters, param, &result);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500447 if (opt < 0)
448 return ctx->sloppy ? 1 : opt;
449
450 switch (opt) {
David Howellsf2aedb72019-12-10 07:31:13 -0500451 case Opt_source:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500452 if (fc->source)
453 return nfs_invalf(fc, "NFS: Multiple sources not supported");
David Howellsf2aedb72019-12-10 07:31:13 -0500454 fc->source = param->string;
455 param->string = NULL;
456 break;
457
David Howells9954bf92019-12-10 07:31:04 -0500458 /*
459 * boolean options: foo/nofoo
460 */
David Howellscbd071b2019-12-10 07:31:08 -0500461 case Opt_soft:
462 ctx->flags |= NFS_MOUNT_SOFT;
463 ctx->flags &= ~NFS_MOUNT_SOFTERR;
464 break;
465 case Opt_softerr:
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500466 ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
David Howellscbd071b2019-12-10 07:31:08 -0500467 ctx->flags &= ~NFS_MOUNT_SOFT;
468 break;
469 case Opt_hard:
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500470 ctx->flags &= ~(NFS_MOUNT_SOFT |
471 NFS_MOUNT_SOFTERR |
472 NFS_MOUNT_SOFTREVAL);
473 break;
474 case Opt_softreval:
475 if (result.negated)
476 ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
477 else
478 ctx->flags &= NFS_MOUNT_SOFTREVAL;
David Howellscbd071b2019-12-10 07:31:08 -0500479 break;
480 case Opt_posix:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500481 if (result.negated)
482 ctx->flags &= ~NFS_MOUNT_POSIX;
483 else
484 ctx->flags |= NFS_MOUNT_POSIX;
David Howellscbd071b2019-12-10 07:31:08 -0500485 break;
486 case Opt_cto:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500487 if (result.negated)
488 ctx->flags |= NFS_MOUNT_NOCTO;
489 else
490 ctx->flags &= ~NFS_MOUNT_NOCTO;
David Howellscbd071b2019-12-10 07:31:08 -0500491 break;
492 case Opt_ac:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500493 if (result.negated)
494 ctx->flags |= NFS_MOUNT_NOAC;
495 else
496 ctx->flags &= ~NFS_MOUNT_NOAC;
David Howellscbd071b2019-12-10 07:31:08 -0500497 break;
498 case Opt_lock:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500499 if (result.negated) {
500 ctx->flags |= NFS_MOUNT_NONLM;
501 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
502 } else {
503 ctx->flags &= ~NFS_MOUNT_NONLM;
504 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
505 }
David Howellscbd071b2019-12-10 07:31:08 -0500506 break;
507 case Opt_udp:
508 ctx->flags &= ~NFS_MOUNT_TCP;
509 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
510 break;
511 case Opt_tcp:
512 ctx->flags |= NFS_MOUNT_TCP;
513 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
514 break;
515 case Opt_rdma:
516 ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
517 ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500518 xprt_load_transport(param->key);
David Howellscbd071b2019-12-10 07:31:08 -0500519 break;
520 case Opt_acl:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500521 if (result.negated)
522 ctx->flags |= NFS_MOUNT_NOACL;
523 else
524 ctx->flags &= ~NFS_MOUNT_NOACL;
David Howellscbd071b2019-12-10 07:31:08 -0500525 break;
526 case Opt_rdirplus:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500527 if (result.negated)
528 ctx->flags |= NFS_MOUNT_NORDIRPLUS;
529 else
530 ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
David Howellscbd071b2019-12-10 07:31:08 -0500531 break;
532 case Opt_sharecache:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500533 if (result.negated)
534 ctx->flags |= NFS_MOUNT_UNSHARED;
535 else
536 ctx->flags &= ~NFS_MOUNT_UNSHARED;
David Howellscbd071b2019-12-10 07:31:08 -0500537 break;
538 case Opt_resvport:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500539 if (result.negated)
540 ctx->flags |= NFS_MOUNT_NORESVPORT;
541 else
542 ctx->flags &= ~NFS_MOUNT_NORESVPORT;
David Howellscbd071b2019-12-10 07:31:08 -0500543 break;
Al Viro48ce73b2019-12-17 20:03:59 -0500544 case Opt_fscache_flag:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500545 if (result.negated)
546 ctx->options &= ~NFS_OPTION_FSCACHE;
547 else
548 ctx->options |= NFS_OPTION_FSCACHE;
Al Viro48ce73b2019-12-17 20:03:59 -0500549 kfree(ctx->fscache_uniq);
550 ctx->fscache_uniq = NULL;
551 break;
552 case Opt_fscache:
553 ctx->options |= NFS_OPTION_FSCACHE;
554 kfree(ctx->fscache_uniq);
555 ctx->fscache_uniq = param->string;
556 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500557 break;
558 case Opt_migration:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500559 if (result.negated)
560 ctx->options &= ~NFS_OPTION_MIGRATION;
561 else
562 ctx->options |= NFS_OPTION_MIGRATION;
David Howellscbd071b2019-12-10 07:31:08 -0500563 break;
David Howells9954bf92019-12-10 07:31:04 -0500564
565 /*
566 * options that take numeric values
567 */
David Howellscbd071b2019-12-10 07:31:08 -0500568 case Opt_port:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500569 if (result.uint_32 > USHRT_MAX)
570 goto out_of_bounds;
571 ctx->nfs_server.port = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500572 break;
573 case Opt_rsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500574 ctx->rsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500575 break;
576 case Opt_wsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500577 ctx->wsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500578 break;
579 case Opt_bsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500580 ctx->bsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500581 break;
582 case Opt_timeo:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500583 if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
584 goto out_of_bounds;
585 ctx->timeo = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500586 break;
587 case Opt_retrans:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500588 if (result.uint_32 > INT_MAX)
589 goto out_of_bounds;
590 ctx->retrans = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500591 break;
592 case Opt_acregmin:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500593 ctx->acregmin = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500594 break;
595 case Opt_acregmax:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500596 ctx->acregmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500597 break;
598 case Opt_acdirmin:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500599 ctx->acdirmin = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500600 break;
601 case Opt_acdirmax:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500602 ctx->acdirmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500603 break;
604 case Opt_actimeo:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500605 ctx->acregmin = result.uint_32;
606 ctx->acregmax = result.uint_32;
607 ctx->acdirmin = result.uint_32;
608 ctx->acdirmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500609 break;
610 case Opt_namelen:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500611 ctx->namlen = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500612 break;
613 case Opt_mountport:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500614 if (result.uint_32 > USHRT_MAX)
615 goto out_of_bounds;
616 ctx->mount_server.port = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500617 break;
618 case Opt_mountvers:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500619 if (result.uint_32 < NFS_MNT_VERSION ||
620 result.uint_32 > NFS_MNT3_VERSION)
621 goto out_of_bounds;
622 ctx->mount_server.version = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500623 break;
624 case Opt_minorversion:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500625 if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
626 goto out_of_bounds;
627 ctx->minorversion = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500628 break;
David Howells9954bf92019-12-10 07:31:04 -0500629
630 /*
631 * options that take text values
632 */
Scott Mayhewe38bb232019-12-10 07:31:12 -0500633 case Opt_v:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500634 ret = nfs_parse_version_string(fc, param->key + 1);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500635 if (ret < 0)
636 return ret;
637 break;
638 case Opt_vers:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500639 ret = nfs_parse_version_string(fc, param->string);
David Howellse5581002019-12-10 07:31:10 -0500640 if (ret < 0)
641 return ret;
David Howellscbd071b2019-12-10 07:31:08 -0500642 break;
643 case Opt_sec:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500644 ret = nfs_parse_security_flavors(fc, param);
David Howellse5581002019-12-10 07:31:10 -0500645 if (ret < 0)
646 return ret;
David Howellscbd071b2019-12-10 07:31:08 -0500647 break;
David Howells9954bf92019-12-10 07:31:04 -0500648
Scott Mayhewe38bb232019-12-10 07:31:12 -0500649 case Opt_proto:
650 protofamily = AF_INET;
651 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
David Howellscbd071b2019-12-10 07:31:08 -0500652 case Opt_xprt_udp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500653 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500654 /* fall through */
655 case Opt_xprt_udp:
656 ctx->flags &= ~NFS_MOUNT_TCP;
657 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500658 break;
David Howellscbd071b2019-12-10 07:31:08 -0500659 case Opt_xprt_tcp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500660 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500661 /* fall through */
662 case Opt_xprt_tcp:
663 ctx->flags |= NFS_MOUNT_TCP;
664 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
665 break;
666 case Opt_xprt_rdma6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500667 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500668 /* fall through */
669 case Opt_xprt_rdma:
670 /* vector side protocols to TCP */
671 ctx->flags |= NFS_MOUNT_TCP;
672 ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500673 xprt_load_transport(param->string);
David Howellscbd071b2019-12-10 07:31:08 -0500674 break;
675 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500676 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
David Howellscbd071b2019-12-10 07:31:08 -0500677 }
David Howells9954bf92019-12-10 07:31:04 -0500678
Scott Mayhewe38bb232019-12-10 07:31:12 -0500679 ctx->protofamily = protofamily;
680 break;
681
682 case Opt_mountproto:
683 mountfamily = AF_INET;
684 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
David Howellscbd071b2019-12-10 07:31:08 -0500685 case Opt_xprt_udp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500686 mountfamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500687 /* fall through */
688 case Opt_xprt_udp:
689 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500690 break;
David Howellscbd071b2019-12-10 07:31:08 -0500691 case Opt_xprt_tcp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500692 mountfamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500693 /* fall through */
694 case Opt_xprt_tcp:
695 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500696 break;
David Howellscbd071b2019-12-10 07:31:08 -0500697 case Opt_xprt_rdma: /* not used for side protocols */
698 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500699 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
David Howellscbd071b2019-12-10 07:31:08 -0500700 }
Scott Mayhewe38bb232019-12-10 07:31:12 -0500701 ctx->mountfamily = mountfamily;
David Howellscbd071b2019-12-10 07:31:08 -0500702 break;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500703
David Howellscbd071b2019-12-10 07:31:08 -0500704 case Opt_addr:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500705 len = rpc_pton(fc->net_ns, param->string, param->size,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500706 &ctx->nfs_server.address,
707 sizeof(ctx->nfs_server._address));
708 if (len == 0)
David Howellscbd071b2019-12-10 07:31:08 -0500709 goto out_invalid_address;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500710 ctx->nfs_server.addrlen = len;
David Howellscbd071b2019-12-10 07:31:08 -0500711 break;
712 case Opt_clientaddr:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500713 kfree(ctx->client_address);
714 ctx->client_address = param->string;
715 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500716 break;
717 case Opt_mounthost:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500718 kfree(ctx->mount_server.hostname);
719 ctx->mount_server.hostname = param->string;
720 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500721 break;
722 case Opt_mountaddr:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500723 len = rpc_pton(fc->net_ns, param->string, param->size,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500724 &ctx->mount_server.address,
725 sizeof(ctx->mount_server._address));
726 if (len == 0)
David Howellscbd071b2019-12-10 07:31:08 -0500727 goto out_invalid_address;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500728 ctx->mount_server.addrlen = len;
David Howellscbd071b2019-12-10 07:31:08 -0500729 break;
730 case Opt_nconnect:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500731 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
732 goto out_of_bounds;
733 ctx->nfs_server.nconnect = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500734 break;
735 case Opt_lookupcache:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500736 switch (result.uint_32) {
David Howellscbd071b2019-12-10 07:31:08 -0500737 case Opt_lookupcache_all:
738 ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
David Howells9954bf92019-12-10 07:31:04 -0500739 break;
David Howellscbd071b2019-12-10 07:31:08 -0500740 case Opt_lookupcache_positive:
741 ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
742 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
David Howells9954bf92019-12-10 07:31:04 -0500743 break;
David Howellscbd071b2019-12-10 07:31:08 -0500744 case Opt_lookupcache_none:
745 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
David Howells9954bf92019-12-10 07:31:04 -0500746 break;
David Howellscbd071b2019-12-10 07:31:08 -0500747 default:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500748 goto out_invalid_value;
David Howellscbd071b2019-12-10 07:31:08 -0500749 }
750 break;
David Howellscbd071b2019-12-10 07:31:08 -0500751 case Opt_local_lock:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500752 switch (result.uint_32) {
David Howellscbd071b2019-12-10 07:31:08 -0500753 case Opt_local_lock_all:
754 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
755 NFS_MOUNT_LOCAL_FCNTL);
David Howells9954bf92019-12-10 07:31:04 -0500756 break;
David Howellscbd071b2019-12-10 07:31:08 -0500757 case Opt_local_lock_flock:
758 ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
David Howells9954bf92019-12-10 07:31:04 -0500759 break;
David Howellscbd071b2019-12-10 07:31:08 -0500760 case Opt_local_lock_posix:
761 ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
David Howells9954bf92019-12-10 07:31:04 -0500762 break;
David Howellscbd071b2019-12-10 07:31:08 -0500763 case Opt_local_lock_none:
764 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
765 NFS_MOUNT_LOCAL_FCNTL);
David Howells9954bf92019-12-10 07:31:04 -0500766 break;
David Howellscbd071b2019-12-10 07:31:08 -0500767 default:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500768 goto out_invalid_value;
David Howellscbd071b2019-12-10 07:31:08 -0500769 }
770 break;
David Howells9954bf92019-12-10 07:31:04 -0500771
772 /*
773 * Special options
774 */
David Howellscbd071b2019-12-10 07:31:08 -0500775 case Opt_sloppy:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500776 ctx->sloppy = true;
David Howellscbd071b2019-12-10 07:31:08 -0500777 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
778 break;
David Howells9954bf92019-12-10 07:31:04 -0500779 }
780
David Howellsf8ee01e2019-12-10 07:31:07 -0500781 return 0;
782
David Howellsf8ee01e2019-12-10 07:31:07 -0500783out_invalid_value:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500784 return nfs_invalf(fc, "NFS: Bad mount option value specified");
Scott Mayhewe38bb232019-12-10 07:31:12 -0500785out_invalid_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500786 return nfs_invalf(fc, "NFS: Bad IP address specified");
Scott Mayhewe38bb232019-12-10 07:31:12 -0500787out_of_bounds:
David Howells3a214092020-01-17 15:55:09 +0000788 return nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500789}
790
David Howells9954bf92019-12-10 07:31:04 -0500791/*
Scott Mayhew62a55d02019-12-10 07:31:14 -0500792 * Split fc->source into "hostname:export_path".
David Howells9954bf92019-12-10 07:31:04 -0500793 *
794 * The leftmost colon demarks the split between the server's hostname
795 * and the export path. If the hostname starts with a left square
796 * bracket, then it may contain colons.
797 *
798 * Note: caller frees hostname and export path, even on error.
799 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500800static int nfs_parse_source(struct fs_context *fc,
801 size_t maxnamlen, size_t maxpathlen)
David Howells9954bf92019-12-10 07:31:04 -0500802{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500803 struct nfs_fs_context *ctx = nfs_fc2context(fc);
804 const char *dev_name = fc->source;
David Howells9954bf92019-12-10 07:31:04 -0500805 size_t len;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500806 const char *end;
David Howells9954bf92019-12-10 07:31:04 -0500807
808 if (unlikely(!dev_name || !*dev_name)) {
809 dfprintk(MOUNT, "NFS: device name not specified\n");
810 return -EINVAL;
811 }
812
813 /* Is the host name protected with square brakcets? */
814 if (*dev_name == '[') {
815 end = strchr(++dev_name, ']');
816 if (end == NULL || end[1] != ':')
817 goto out_bad_devname;
818
819 len = end - dev_name;
820 end++;
821 } else {
Scott Mayhew62a55d02019-12-10 07:31:14 -0500822 const char *comma;
David Howells9954bf92019-12-10 07:31:04 -0500823
824 end = strchr(dev_name, ':');
825 if (end == NULL)
826 goto out_bad_devname;
827 len = end - dev_name;
828
829 /* kill possible hostname list: not supported */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500830 comma = memchr(dev_name, ',', len);
831 if (comma)
David Howells9954bf92019-12-10 07:31:04 -0500832 len = comma - dev_name;
833 }
834
835 if (len > maxnamlen)
836 goto out_hostname;
837
Scott Mayhew75a9b912020-02-25 11:05:22 -0500838 kfree(ctx->nfs_server.hostname);
839
David Howells9954bf92019-12-10 07:31:04 -0500840 /* N.B. caller will free nfs_server.hostname in all cases */
David Howellse5581002019-12-10 07:31:10 -0500841 ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
842 if (!ctx->nfs_server.hostname)
David Howells9954bf92019-12-10 07:31:04 -0500843 goto out_nomem;
844 len = strlen(++end);
845 if (len > maxpathlen)
846 goto out_path;
David Howellse5581002019-12-10 07:31:10 -0500847 ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
848 if (!ctx->nfs_server.export_path)
David Howells9954bf92019-12-10 07:31:04 -0500849 goto out_nomem;
850
David Howellse5581002019-12-10 07:31:10 -0500851 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", ctx->nfs_server.export_path);
David Howells9954bf92019-12-10 07:31:04 -0500852 return 0;
853
854out_bad_devname:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500855 return nfs_invalf(fc, "NFS: device name not in host:path format");
David Howells9954bf92019-12-10 07:31:04 -0500856out_nomem:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500857 nfs_errorf(fc, "NFS: not enough memory to parse device name");
David Howells9954bf92019-12-10 07:31:04 -0500858 return -ENOMEM;
David Howells9954bf92019-12-10 07:31:04 -0500859out_hostname:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500860 nfs_errorf(fc, "NFS: server hostname too long");
David Howells9954bf92019-12-10 07:31:04 -0500861 return -ENAMETOOLONG;
David Howells9954bf92019-12-10 07:31:04 -0500862out_path:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500863 nfs_errorf(fc, "NFS: export pathname too long");
David Howells9954bf92019-12-10 07:31:04 -0500864 return -ENAMETOOLONG;
865}
866
David Howellsf2aedb72019-12-10 07:31:13 -0500867static inline bool is_remount_fc(struct fs_context *fc)
868{
869 return fc->root != NULL;
870}
871
David Howells9954bf92019-12-10 07:31:04 -0500872/*
David Howellse5581002019-12-10 07:31:10 -0500873 * Parse monolithic NFS2/NFS3 mount data
David Howells9954bf92019-12-10 07:31:04 -0500874 * - fills in the mount root filehandle
875 *
876 * For option strings, user space handles the following behaviors:
877 *
878 * + DNS: mapping server host name to IP address ("addr=" option)
879 *
880 * + failure mode: how to behave if a mount request can't be handled
881 * immediately ("fg/bg" option)
882 *
883 * + retry: how often to retry a mount request ("retry=" option)
884 *
885 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
886 * mountproto=tcp after mountproto=udp, and so on
887 */
David Howellsf2aedb72019-12-10 07:31:13 -0500888static int nfs23_parse_monolithic(struct fs_context *fc,
889 struct nfs_mount_data *data)
David Howells9954bf92019-12-10 07:31:04 -0500890{
David Howellsf2aedb72019-12-10 07:31:13 -0500891 struct nfs_fs_context *ctx = nfs_fc2context(fc);
Scott Mayhew62a55d02019-12-10 07:31:14 -0500892 struct nfs_fh *mntfh = ctx->mntfh;
David Howells5eb005c2019-12-10 07:31:06 -0500893 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -0500894 int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
895
896 if (data == NULL)
897 goto out_no_data;
898
David Howells5eb005c2019-12-10 07:31:06 -0500899 ctx->version = NFS_DEFAULT_VERSION;
David Howells9954bf92019-12-10 07:31:04 -0500900 switch (data->version) {
901 case 1:
902 data->namlen = 0; /* fall through */
903 case 2:
904 data->bsize = 0; /* fall through */
905 case 3:
906 if (data->flags & NFS_MOUNT_VER3)
907 goto out_no_v3;
908 data->root.size = NFS2_FHSIZE;
909 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
910 /* Turn off security negotiation */
911 extra_flags |= NFS_MOUNT_SECFLAVOUR;
912 /* fall through */
913 case 4:
914 if (data->flags & NFS_MOUNT_SECFLAVOUR)
915 goto out_no_sec;
916 /* fall through */
917 case 5:
918 memset(data->context, 0, sizeof(data->context));
919 /* fall through */
920 case 6:
921 if (data->flags & NFS_MOUNT_VER3) {
922 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
923 goto out_invalid_fh;
924 mntfh->size = data->root.size;
David Howells5eb005c2019-12-10 07:31:06 -0500925 ctx->version = 3;
David Howells9954bf92019-12-10 07:31:04 -0500926 } else {
927 mntfh->size = NFS2_FHSIZE;
David Howells5eb005c2019-12-10 07:31:06 -0500928 ctx->version = 2;
David Howells9954bf92019-12-10 07:31:04 -0500929 }
930
931
932 memcpy(mntfh->data, data->root.data, mntfh->size);
933 if (mntfh->size < sizeof(mntfh->data))
934 memset(mntfh->data + mntfh->size, 0,
935 sizeof(mntfh->data) - mntfh->size);
936
937 /*
David Howells5eb005c2019-12-10 07:31:06 -0500938 * Translate to nfs_fs_context, which nfs_fill_super
David Howells9954bf92019-12-10 07:31:04 -0500939 * can deal with.
940 */
David Howells5eb005c2019-12-10 07:31:06 -0500941 ctx->flags = data->flags & NFS_MOUNT_FLAGMASK;
942 ctx->flags |= extra_flags;
943 ctx->rsize = data->rsize;
944 ctx->wsize = data->wsize;
945 ctx->timeo = data->timeo;
946 ctx->retrans = data->retrans;
947 ctx->acregmin = data->acregmin;
948 ctx->acregmax = data->acregmax;
949 ctx->acdirmin = data->acdirmin;
950 ctx->acdirmax = data->acdirmax;
951 ctx->need_mount = false;
David Howells9954bf92019-12-10 07:31:04 -0500952
953 memcpy(sap, &data->addr, sizeof(data->addr));
David Howells5eb005c2019-12-10 07:31:06 -0500954 ctx->nfs_server.addrlen = sizeof(data->addr);
955 ctx->nfs_server.port = ntohs(data->addr.sin_port);
David Howells9954bf92019-12-10 07:31:04 -0500956 if (sap->sa_family != AF_INET ||
957 !nfs_verify_server_address(sap))
958 goto out_no_address;
959
960 if (!(data->flags & NFS_MOUNT_TCP))
David Howells5eb005c2019-12-10 07:31:06 -0500961 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500962 /* N.B. caller will free nfs_server.hostname in all cases */
David Howells5eb005c2019-12-10 07:31:06 -0500963 ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
David Howellsf2aedb72019-12-10 07:31:13 -0500964 if (!ctx->nfs_server.hostname)
965 goto out_nomem;
966
David Howells5eb005c2019-12-10 07:31:06 -0500967 ctx->namlen = data->namlen;
968 ctx->bsize = data->bsize;
David Howells9954bf92019-12-10 07:31:04 -0500969
970 if (data->flags & NFS_MOUNT_SECFLAVOUR)
David Howells5eb005c2019-12-10 07:31:06 -0500971 ctx->selected_flavor = data->pseudoflavor;
David Howells9954bf92019-12-10 07:31:04 -0500972 else
David Howells5eb005c2019-12-10 07:31:06 -0500973 ctx->selected_flavor = RPC_AUTH_UNIX;
David Howells9954bf92019-12-10 07:31:04 -0500974
975 if (!(data->flags & NFS_MOUNT_NONLM))
David Howells5eb005c2019-12-10 07:31:06 -0500976 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
David Howells9954bf92019-12-10 07:31:04 -0500977 NFS_MOUNT_LOCAL_FCNTL);
978 else
David Howells5eb005c2019-12-10 07:31:06 -0500979 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
David Howells9954bf92019-12-10 07:31:04 -0500980 NFS_MOUNT_LOCAL_FCNTL);
Scott Mayhew62a55d02019-12-10 07:31:14 -0500981
David Howells9954bf92019-12-10 07:31:04 -0500982 /*
983 * The legacy version 6 binary mount data from userspace has a
984 * field used only to transport selinux information into the
Randy Dunlapa5032912020-08-10 19:18:35 -0700985 * kernel. To continue to support that functionality we
David Howells9954bf92019-12-10 07:31:04 -0500986 * have a touch of selinux knowledge here in the NFS code. The
987 * userspace code converted context=blah to just blah so we are
988 * converting back to the full string selinux understands.
989 */
990 if (data->context[0]){
991#ifdef CONFIG_SECURITY_SELINUX
David Howellsf2aedb72019-12-10 07:31:13 -0500992 int ret;
993
David Howells9954bf92019-12-10 07:31:04 -0500994 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
David Howellsf2aedb72019-12-10 07:31:13 -0500995 ret = vfs_parse_fs_string(fc, "context",
996 data->context, strlen(data->context));
997 if (ret < 0)
998 return ret;
David Howells9954bf92019-12-10 07:31:04 -0500999#else
1000 return -EINVAL;
1001#endif
1002 }
1003
1004 break;
1005 default:
David Howellsf2aedb72019-12-10 07:31:13 -05001006 goto generic;
David Howells9954bf92019-12-10 07:31:04 -05001007 }
1008
David Howellsf2aedb72019-12-10 07:31:13 -05001009 ctx->skip_reconfig_option_check = true;
David Howells9954bf92019-12-10 07:31:04 -05001010 return 0;
1011
David Howellsf2aedb72019-12-10 07:31:13 -05001012generic:
1013 return generic_parse_monolithic(fc, data);
1014
David Howells9954bf92019-12-10 07:31:04 -05001015out_no_data:
David Howellsf2aedb72019-12-10 07:31:13 -05001016 if (is_remount_fc(fc)) {
1017 ctx->skip_reconfig_option_check = true;
1018 return 0;
1019 }
Scott Mayhewce8866f02019-12-10 07:31:15 -05001020 return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
David Howells9954bf92019-12-10 07:31:04 -05001021
1022out_no_v3:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001023 return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
David Howells9954bf92019-12-10 07:31:04 -05001024
1025out_no_sec:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001026 return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
David Howells9954bf92019-12-10 07:31:04 -05001027
1028out_nomem:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001029 dfprintk(MOUNT, "NFS: not enough memory to handle mount options");
David Howells9954bf92019-12-10 07:31:04 -05001030 return -ENOMEM;
1031
1032out_no_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001033 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
David Howells9954bf92019-12-10 07:31:04 -05001034
1035out_invalid_fh:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001036 return nfs_invalf(fc, "NFS: invalid root filehandle");
David Howells9954bf92019-12-10 07:31:04 -05001037}
1038
1039#if IS_ENABLED(CONFIG_NFS_V4)
David Howells9954bf92019-12-10 07:31:04 -05001040/*
1041 * Validate NFSv4 mount options
1042 */
David Howellsf2aedb72019-12-10 07:31:13 -05001043static int nfs4_parse_monolithic(struct fs_context *fc,
1044 struct nfs4_mount_data *data)
David Howells9954bf92019-12-10 07:31:04 -05001045{
David Howellsf2aedb72019-12-10 07:31:13 -05001046 struct nfs_fs_context *ctx = nfs_fc2context(fc);
David Howells5eb005c2019-12-10 07:31:06 -05001047 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001048 char *c;
1049
1050 if (data == NULL)
1051 goto out_no_data;
1052
David Howells5eb005c2019-12-10 07:31:06 -05001053 ctx->version = 4;
David Howells9954bf92019-12-10 07:31:04 -05001054
1055 switch (data->version) {
1056 case 1:
David Howells5eb005c2019-12-10 07:31:06 -05001057 if (data->host_addrlen > sizeof(ctx->nfs_server.address))
David Howells9954bf92019-12-10 07:31:04 -05001058 goto out_no_address;
1059 if (data->host_addrlen == 0)
1060 goto out_no_address;
David Howells5eb005c2019-12-10 07:31:06 -05001061 ctx->nfs_server.addrlen = data->host_addrlen;
David Howells9954bf92019-12-10 07:31:04 -05001062 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1063 return -EFAULT;
1064 if (!nfs_verify_server_address(sap))
1065 goto out_no_address;
David Howells5eb005c2019-12-10 07:31:06 -05001066 ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
David Howells9954bf92019-12-10 07:31:04 -05001067
1068 if (data->auth_flavourlen) {
1069 rpc_authflavor_t pseudoflavor;
1070 if (data->auth_flavourlen > 1)
1071 goto out_inval_auth;
1072 if (copy_from_user(&pseudoflavor,
1073 data->auth_flavours,
1074 sizeof(pseudoflavor)))
1075 return -EFAULT;
David Howells5eb005c2019-12-10 07:31:06 -05001076 ctx->selected_flavor = pseudoflavor;
David Howells9954bf92019-12-10 07:31:04 -05001077 } else
David Howells5eb005c2019-12-10 07:31:06 -05001078 ctx->selected_flavor = RPC_AUTH_UNIX;
David Howells9954bf92019-12-10 07:31:04 -05001079
1080 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1081 if (IS_ERR(c))
1082 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001083 ctx->nfs_server.hostname = c;
David Howells9954bf92019-12-10 07:31:04 -05001084
1085 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1086 if (IS_ERR(c))
1087 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001088 ctx->nfs_server.export_path = c;
David Howells9954bf92019-12-10 07:31:04 -05001089 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1090
1091 c = strndup_user(data->client_addr.data, 16);
1092 if (IS_ERR(c))
1093 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001094 ctx->client_address = c;
David Howells9954bf92019-12-10 07:31:04 -05001095
1096 /*
David Howellsf2aedb72019-12-10 07:31:13 -05001097 * Translate to nfs_fs_context, which nfs_fill_super
David Howells9954bf92019-12-10 07:31:04 -05001098 * can deal with.
1099 */
1100
David Howells5eb005c2019-12-10 07:31:06 -05001101 ctx->flags = data->flags & NFS4_MOUNT_FLAGMASK;
1102 ctx->rsize = data->rsize;
1103 ctx->wsize = data->wsize;
1104 ctx->timeo = data->timeo;
1105 ctx->retrans = data->retrans;
1106 ctx->acregmin = data->acregmin;
1107 ctx->acregmax = data->acregmax;
1108 ctx->acdirmin = data->acdirmin;
1109 ctx->acdirmax = data->acdirmax;
1110 ctx->nfs_server.protocol = data->proto;
1111 nfs_validate_transport_protocol(ctx);
1112 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
David Howells9954bf92019-12-10 07:31:04 -05001113 goto out_invalid_transport_udp;
1114
1115 break;
1116 default:
David Howellsf2aedb72019-12-10 07:31:13 -05001117 goto generic;
David Howells9954bf92019-12-10 07:31:04 -05001118 }
1119
David Howellsf2aedb72019-12-10 07:31:13 -05001120 ctx->skip_reconfig_option_check = true;
David Howells9954bf92019-12-10 07:31:04 -05001121 return 0;
1122
David Howellsf2aedb72019-12-10 07:31:13 -05001123generic:
1124 return generic_parse_monolithic(fc, data);
1125
David Howells9954bf92019-12-10 07:31:04 -05001126out_no_data:
David Howellsf2aedb72019-12-10 07:31:13 -05001127 if (is_remount_fc(fc)) {
1128 ctx->skip_reconfig_option_check = true;
1129 return 0;
1130 }
Scott Mayhewce8866f02019-12-10 07:31:15 -05001131 return nfs_invalf(fc, "NFS4: mount program didn't pass any mount data");
David Howells9954bf92019-12-10 07:31:04 -05001132
1133out_inval_auth:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001134 return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1135 data->auth_flavourlen);
David Howells9954bf92019-12-10 07:31:04 -05001136
1137out_no_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001138 return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
David Howells9954bf92019-12-10 07:31:04 -05001139
1140out_invalid_transport_udp:
Petr Vorelaa3367c2020-03-24 21:08:49 +01001141 return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
David Howells9954bf92019-12-10 07:31:04 -05001142}
David Howells9954bf92019-12-10 07:31:04 -05001143#endif
1144
David Howellsf2aedb72019-12-10 07:31:13 -05001145/*
1146 * Parse a monolithic block of data from sys_mount().
1147 */
1148static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1149 void *data)
David Howells9954bf92019-12-10 07:31:04 -05001150{
David Howellsf2aedb72019-12-10 07:31:13 -05001151 if (fc->fs_type == &nfs_fs_type)
1152 return nfs23_parse_monolithic(fc, data);
1153
1154#if IS_ENABLED(CONFIG_NFS_V4)
1155 if (fc->fs_type == &nfs4_fs_type)
1156 return nfs4_parse_monolithic(fc, data);
1157#endif
1158
Scott Mayhewce8866f02019-12-10 07:31:15 -05001159 return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
David Howellsf2aedb72019-12-10 07:31:13 -05001160}
1161
1162/*
1163 * Validate the preparsed information in the config.
1164 */
1165static int nfs_fs_context_validate(struct fs_context *fc)
1166{
1167 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1168 struct nfs_subversion *nfs_mod;
1169 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001170 int max_namelen = PAGE_SIZE;
1171 int max_pathlen = NFS_MAXPATHLEN;
David Howellsf2aedb72019-12-10 07:31:13 -05001172 int port = 0;
1173 int ret;
David Howells9954bf92019-12-10 07:31:04 -05001174
David Howellsf2aedb72019-12-10 07:31:13 -05001175 if (!fc->source)
1176 goto out_no_device_name;
1177
1178 /* Check for sanity first. */
1179 if (ctx->minorversion && ctx->version != 4)
1180 goto out_minorversion_mismatch;
1181
1182 if (ctx->options & NFS_OPTION_MIGRATION &&
1183 (ctx->version != 4 || ctx->minorversion != 0))
1184 goto out_migration_misuse;
1185
1186 /* Verify that any proto=/mountproto= options match the address
1187 * families in the addr=/mountaddr= options.
1188 */
1189 if (ctx->protofamily != AF_UNSPEC &&
1190 ctx->protofamily != ctx->nfs_server.address.sa_family)
1191 goto out_proto_mismatch;
1192
1193 if (ctx->mountfamily != AF_UNSPEC) {
1194 if (ctx->mount_server.addrlen) {
1195 if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1196 goto out_mountproto_mismatch;
1197 } else {
1198 if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1199 goto out_mountproto_mismatch;
1200 }
1201 }
David Howells9954bf92019-12-10 07:31:04 -05001202
1203 if (!nfs_verify_server_address(sap))
1204 goto out_no_address;
1205
David Howells5eb005c2019-12-10 07:31:06 -05001206 if (ctx->version == 4) {
Scott Mayhew62a55d02019-12-10 07:31:14 -05001207 if (IS_ENABLED(CONFIG_NFS_V4)) {
1208 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1209 port = NFS_RDMA_PORT;
1210 else
1211 port = NFS_PORT;
1212 max_namelen = NFS4_MAXNAMLEN;
1213 max_pathlen = NFS4_MAXPATHLEN;
1214 nfs_validate_transport_protocol(ctx);
1215 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1216 goto out_invalid_transport_udp;
1217 ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1218 NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1219 NFS_MOUNT_LOCAL_FCNTL);
1220 } else {
1221 goto out_v4_not_compiled;
1222 }
David Howells9954bf92019-12-10 07:31:04 -05001223 } else {
David Howells5eb005c2019-12-10 07:31:06 -05001224 nfs_set_mount_transport_protocol(ctx);
Olga Kornievskaiab24ee6c2019-12-16 16:34:02 -05001225#ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
1226 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1227 goto out_invalid_transport_udp;
1228#endif
David Howells5eb005c2019-12-10 07:31:06 -05001229 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
David Howells9954bf92019-12-10 07:31:04 -05001230 port = NFS_RDMA_PORT;
1231 }
1232
David Howells5eb005c2019-12-10 07:31:06 -05001233 nfs_set_port(sap, &ctx->nfs_server.port, port);
David Howells9954bf92019-12-10 07:31:04 -05001234
Scott Mayhew62a55d02019-12-10 07:31:14 -05001235 ret = nfs_parse_source(fc, max_namelen, max_pathlen);
David Howellsf2aedb72019-12-10 07:31:13 -05001236 if (ret < 0)
1237 return ret;
David Howells9954bf92019-12-10 07:31:04 -05001238
David Howellsf2aedb72019-12-10 07:31:13 -05001239 /* Load the NFS protocol module if we haven't done so yet */
Scott Mayhew62a55d02019-12-10 07:31:14 -05001240 if (!ctx->nfs_mod) {
David Howellsf2aedb72019-12-10 07:31:13 -05001241 nfs_mod = get_nfs_version(ctx->version);
1242 if (IS_ERR(nfs_mod)) {
1243 ret = PTR_ERR(nfs_mod);
1244 goto out_version_unavailable;
1245 }
Scott Mayhew62a55d02019-12-10 07:31:14 -05001246 ctx->nfs_mod = nfs_mod;
David Howellsf2aedb72019-12-10 07:31:13 -05001247 }
Scott Mayhew1cef2182020-02-21 15:21:38 -05001248
1249 /* Ensure the filesystem context has the correct fs_type */
1250 if (fc->fs_type != ctx->nfs_mod->nfs_fs) {
1251 module_put(fc->fs_type->owner);
1252 __module_get(ctx->nfs_mod->nfs_fs->owner);
1253 fc->fs_type = ctx->nfs_mod->nfs_fs;
1254 }
David Howellsf2aedb72019-12-10 07:31:13 -05001255 return 0;
1256
1257out_no_device_name:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001258 return nfs_invalf(fc, "NFS: Device name not specified");
David Howells9954bf92019-12-10 07:31:04 -05001259out_v4_not_compiled:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001260 nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
David Howells9954bf92019-12-10 07:31:04 -05001261 return -EPROTONOSUPPORT;
David Howells9954bf92019-12-10 07:31:04 -05001262out_invalid_transport_udp:
Petr Vorelaa3367c2020-03-24 21:08:49 +01001263 return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
David Howells9954bf92019-12-10 07:31:04 -05001264out_no_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001265 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
David Howellsf2aedb72019-12-10 07:31:13 -05001266out_mountproto_mismatch:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001267 return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
David Howellsf2aedb72019-12-10 07:31:13 -05001268out_proto_mismatch:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001269 return nfs_invalf(fc, "NFS: Server address does not match proto= option");
David Howellsf2aedb72019-12-10 07:31:13 -05001270out_minorversion_mismatch:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001271 return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
David Howellsf2aedb72019-12-10 07:31:13 -05001272 ctx->version, ctx->minorversion);
David Howellsf2aedb72019-12-10 07:31:13 -05001273out_migration_misuse:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001274 return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
David Howellsf2aedb72019-12-10 07:31:13 -05001275out_version_unavailable:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001276 nfs_errorf(fc, "NFS: Version unavailable");
David Howellsf2aedb72019-12-10 07:31:13 -05001277 return ret;
David Howells9954bf92019-12-10 07:31:04 -05001278}
David Howellsf2aedb72019-12-10 07:31:13 -05001279
1280/*
1281 * Create an NFS superblock by the appropriate method.
1282 */
1283static int nfs_get_tree(struct fs_context *fc)
1284{
1285 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1286 int err = nfs_fs_context_validate(fc);
1287
1288 if (err)
1289 return err;
1290 if (!ctx->internal)
Scott Mayhew62a55d02019-12-10 07:31:14 -05001291 return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
David Howellsf2aedb72019-12-10 07:31:13 -05001292 else
1293 return nfs_get_tree_common(fc);
1294}
1295
1296/*
1297 * Handle duplication of a configuration. The caller copied *src into *sc, but
1298 * it can't deal with resource pointers in the filesystem context, so we have
1299 * to do that. We need to clear pointers, copy data or get extra refs as
1300 * appropriate.
1301 */
1302static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1303{
1304 struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1305
1306 ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1307 if (!ctx)
1308 return -ENOMEM;
1309
Scott Mayhew62a55d02019-12-10 07:31:14 -05001310 ctx->mntfh = nfs_alloc_fhandle();
1311 if (!ctx->mntfh) {
David Howellsf2aedb72019-12-10 07:31:13 -05001312 kfree(ctx);
1313 return -ENOMEM;
1314 }
Scott Mayhew62a55d02019-12-10 07:31:14 -05001315 nfs_copy_fh(ctx->mntfh, src->mntfh);
David Howellsf2aedb72019-12-10 07:31:13 -05001316
Scott Mayhew62a55d02019-12-10 07:31:14 -05001317 __module_get(ctx->nfs_mod->owner);
David Howellsf2aedb72019-12-10 07:31:13 -05001318 ctx->client_address = NULL;
1319 ctx->mount_server.hostname = NULL;
1320 ctx->nfs_server.export_path = NULL;
1321 ctx->nfs_server.hostname = NULL;
1322 ctx->fscache_uniq = NULL;
David Howellsf2aedb72019-12-10 07:31:13 -05001323 ctx->clone_data.fattr = NULL;
1324 fc->fs_private = ctx;
1325 return 0;
1326}
1327
1328static void nfs_fs_context_free(struct fs_context *fc)
1329{
1330 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1331
1332 if (ctx) {
Scott Mayhew62a55d02019-12-10 07:31:14 -05001333 if (ctx->server)
1334 nfs_free_server(ctx->server);
1335 if (ctx->nfs_mod)
1336 put_nfs_version(ctx->nfs_mod);
David Howellsf2aedb72019-12-10 07:31:13 -05001337 kfree(ctx->client_address);
1338 kfree(ctx->mount_server.hostname);
1339 kfree(ctx->nfs_server.export_path);
1340 kfree(ctx->nfs_server.hostname);
1341 kfree(ctx->fscache_uniq);
Scott Mayhew62a55d02019-12-10 07:31:14 -05001342 nfs_free_fhandle(ctx->mntfh);
David Howellsf2aedb72019-12-10 07:31:13 -05001343 nfs_free_fattr(ctx->clone_data.fattr);
1344 kfree(ctx);
1345 }
1346}
1347
1348static const struct fs_context_operations nfs_fs_context_ops = {
1349 .free = nfs_fs_context_free,
1350 .dup = nfs_fs_context_dup,
1351 .parse_param = nfs_fs_context_parse_param,
1352 .parse_monolithic = nfs_fs_context_parse_monolithic,
1353 .get_tree = nfs_get_tree,
1354 .reconfigure = nfs_reconfigure,
1355};
1356
1357/*
1358 * Prepare superblock configuration. We use the namespaces attached to the
1359 * context. This may be the current process's namespaces, or it may be a
1360 * container's namespaces.
1361 */
1362static int nfs_init_fs_context(struct fs_context *fc)
1363{
1364 struct nfs_fs_context *ctx;
1365
1366 ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1367 if (unlikely(!ctx))
1368 return -ENOMEM;
1369
Scott Mayhew62a55d02019-12-10 07:31:14 -05001370 ctx->mntfh = nfs_alloc_fhandle();
1371 if (unlikely(!ctx->mntfh)) {
David Howellsf2aedb72019-12-10 07:31:13 -05001372 kfree(ctx);
1373 return -ENOMEM;
1374 }
1375
1376 ctx->protofamily = AF_UNSPEC;
1377 ctx->mountfamily = AF_UNSPEC;
1378 ctx->mount_server.port = NFS_UNSPEC_PORT;
1379
1380 if (fc->root) {
1381 /* reconfigure, start with the current config */
1382 struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1383 struct net *net = nfss->nfs_client->cl_net;
1384
1385 ctx->flags = nfss->flags;
1386 ctx->rsize = nfss->rsize;
1387 ctx->wsize = nfss->wsize;
1388 ctx->retrans = nfss->client->cl_timeout->to_retries;
1389 ctx->selected_flavor = nfss->client->cl_auth->au_flavor;
1390 ctx->acregmin = nfss->acregmin / HZ;
1391 ctx->acregmax = nfss->acregmax / HZ;
1392 ctx->acdirmin = nfss->acdirmin / HZ;
1393 ctx->acdirmax = nfss->acdirmax / HZ;
1394 ctx->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1395 ctx->nfs_server.port = nfss->port;
1396 ctx->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1397 ctx->version = nfss->nfs_client->rpc_ops->version;
1398 ctx->minorversion = nfss->nfs_client->cl_minorversion;
1399
1400 memcpy(&ctx->nfs_server.address, &nfss->nfs_client->cl_addr,
1401 ctx->nfs_server.addrlen);
1402
1403 if (fc->net_ns != net) {
1404 put_net(fc->net_ns);
1405 fc->net_ns = get_net(net);
1406 }
1407
Scott Mayhew62a55d02019-12-10 07:31:14 -05001408 ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1409 __module_get(ctx->nfs_mod->owner);
David Howellsf2aedb72019-12-10 07:31:13 -05001410 } else {
1411 /* defaults */
1412 ctx->timeo = NFS_UNSPEC_TIMEO;
1413 ctx->retrans = NFS_UNSPEC_RETRANS;
1414 ctx->acregmin = NFS_DEF_ACREGMIN;
1415 ctx->acregmax = NFS_DEF_ACREGMAX;
1416 ctx->acdirmin = NFS_DEF_ACDIRMIN;
1417 ctx->acdirmax = NFS_DEF_ACDIRMAX;
1418 ctx->nfs_server.port = NFS_UNSPEC_PORT;
1419 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1420 ctx->selected_flavor = RPC_AUTH_MAXFLAVOR;
1421 ctx->minorversion = 0;
1422 ctx->need_mount = true;
1423 }
David Howellsf2aedb72019-12-10 07:31:13 -05001424 fc->fs_private = ctx;
1425 fc->ops = &nfs_fs_context_ops;
1426 return 0;
1427}
1428
1429struct file_system_type nfs_fs_type = {
1430 .owner = THIS_MODULE,
1431 .name = "nfs",
1432 .init_fs_context = nfs_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04001433 .parameters = nfs_fs_parameters,
David Howellsf2aedb72019-12-10 07:31:13 -05001434 .kill_sb = nfs_kill_super,
1435 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1436};
1437MODULE_ALIAS_FS("nfs");
1438EXPORT_SYMBOL_GPL(nfs_fs_type);
1439
1440#if IS_ENABLED(CONFIG_NFS_V4)
1441struct file_system_type nfs4_fs_type = {
1442 .owner = THIS_MODULE,
1443 .name = "nfs4",
1444 .init_fs_context = nfs_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04001445 .parameters = nfs_fs_parameters,
David Howellsf2aedb72019-12-10 07:31:13 -05001446 .kill_sb = nfs_kill_super,
1447 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1448};
1449MODULE_ALIAS_FS("nfs4");
1450MODULE_ALIAS("nfs4");
1451EXPORT_SYMBOL_GPL(nfs4_fs_type);
1452#endif /* CONFIG_NFS_V4 */