blob: 0247dcb7b316217ec06b56d1fb93c9c5125dc72a [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,
48 Opt_hard,
49 Opt_intr,
David Howells9954bf92019-12-10 07:31:04 -050050 Opt_local_lock,
Scott Mayhewe38bb232019-12-10 07:31:12 -050051 Opt_lock,
52 Opt_lookupcache,
53 Opt_migration,
54 Opt_minorversion,
55 Opt_mountaddr,
56 Opt_mounthost,
57 Opt_mountport,
58 Opt_mountproto,
59 Opt_mountvers,
60 Opt_namelen,
61 Opt_nconnect,
62 Opt_port,
63 Opt_posix,
64 Opt_proto,
65 Opt_rdirplus,
66 Opt_rdma,
67 Opt_resvport,
68 Opt_retrans,
69 Opt_retry,
70 Opt_rsize,
71 Opt_sec,
72 Opt_sharecache,
73 Opt_sloppy,
74 Opt_soft,
75 Opt_softerr,
Trond Myklebustc74dfe92020-01-06 15:39:37 -050076 Opt_softreval,
Scott Mayhewe38bb232019-12-10 07:31:12 -050077 Opt_source,
78 Opt_tcp,
79 Opt_timeo,
80 Opt_udp,
81 Opt_v,
82 Opt_vers,
83 Opt_wsize,
David Howells9954bf92019-12-10 07:31:04 -050084};
85
Scott Mayhewe38bb232019-12-10 07:31:12 -050086static const struct fs_parameter_spec nfs_param_specs[] = {
87 fsparam_flag_no("ac", Opt_ac),
88 fsparam_u32 ("acdirmax", Opt_acdirmax),
89 fsparam_u32 ("acdirmin", Opt_acdirmin),
90 fsparam_flag_no("acl", Opt_acl),
91 fsparam_u32 ("acregmax", Opt_acregmax),
92 fsparam_u32 ("acregmin", Opt_acregmin),
93 fsparam_u32 ("actimeo", Opt_actimeo),
94 fsparam_string("addr", Opt_addr),
95 fsparam_flag ("bg", Opt_bg),
96 fsparam_u32 ("bsize", Opt_bsize),
97 fsparam_string("clientaddr", Opt_clientaddr),
98 fsparam_flag_no("cto", Opt_cto),
99 fsparam_flag ("fg", Opt_fg),
100 __fsparam(fs_param_is_string, "fsc", Opt_fscache,
101 fs_param_neg_with_no|fs_param_v_optional),
102 fsparam_flag ("hard", Opt_hard),
103 __fsparam(fs_param_is_flag, "intr", Opt_intr,
104 fs_param_neg_with_no|fs_param_deprecated),
105 fsparam_enum ("local_lock", Opt_local_lock),
106 fsparam_flag_no("lock", Opt_lock),
107 fsparam_enum ("lookupcache", Opt_lookupcache),
108 fsparam_flag_no("migration", Opt_migration),
109 fsparam_u32 ("minorversion", Opt_minorversion),
110 fsparam_string("mountaddr", Opt_mountaddr),
111 fsparam_string("mounthost", Opt_mounthost),
112 fsparam_u32 ("mountport", Opt_mountport),
113 fsparam_string("mountproto", Opt_mountproto),
114 fsparam_u32 ("mountvers", Opt_mountvers),
115 fsparam_u32 ("namlen", Opt_namelen),
116 fsparam_u32 ("nconnect", Opt_nconnect),
117 fsparam_string("nfsvers", Opt_vers),
118 fsparam_u32 ("port", Opt_port),
119 fsparam_flag_no("posix", Opt_posix),
120 fsparam_string("proto", Opt_proto),
121 fsparam_flag_no("rdirplus", Opt_rdirplus),
122 fsparam_flag ("rdma", Opt_rdma),
123 fsparam_flag_no("resvport", Opt_resvport),
124 fsparam_u32 ("retrans", Opt_retrans),
125 fsparam_string("retry", Opt_retry),
126 fsparam_u32 ("rsize", Opt_rsize),
127 fsparam_string("sec", Opt_sec),
128 fsparam_flag_no("sharecache", Opt_sharecache),
129 fsparam_flag ("sloppy", Opt_sloppy),
130 fsparam_flag ("soft", Opt_soft),
131 fsparam_flag ("softerr", Opt_softerr),
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500132 fsparam_flag ("softreval", Opt_softreval),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500133 fsparam_string("source", Opt_source),
134 fsparam_flag ("tcp", Opt_tcp),
135 fsparam_u32 ("timeo", Opt_timeo),
136 fsparam_flag ("udp", Opt_udp),
137 fsparam_flag ("v2", Opt_v),
138 fsparam_flag ("v3", Opt_v),
139 fsparam_flag ("v4", Opt_v),
140 fsparam_flag ("v4.0", Opt_v),
141 fsparam_flag ("v4.1", Opt_v),
142 fsparam_flag ("v4.2", Opt_v),
143 fsparam_string("vers", Opt_vers),
144 fsparam_u32 ("wsize", Opt_wsize),
145 {}
David Howells9954bf92019-12-10 07:31:04 -0500146};
147
148enum {
Scott Mayhewe38bb232019-12-10 07:31:12 -0500149 Opt_local_lock_all,
150 Opt_local_lock_flock,
David Howells9954bf92019-12-10 07:31:04 -0500151 Opt_local_lock_none,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500152 Opt_local_lock_posix,
David Howells9954bf92019-12-10 07:31:04 -0500153};
154
155enum {
Scott Mayhewe38bb232019-12-10 07:31:12 -0500156 Opt_lookupcache_all,
157 Opt_lookupcache_none,
158 Opt_lookupcache_positive,
David Howells9954bf92019-12-10 07:31:04 -0500159};
160
Scott Mayhewe38bb232019-12-10 07:31:12 -0500161static const struct fs_parameter_enum nfs_param_enums[] = {
162 { Opt_local_lock, "all", Opt_local_lock_all },
163 { Opt_local_lock, "flock", Opt_local_lock_flock },
164 { Opt_local_lock, "none", Opt_local_lock_none },
165 { Opt_local_lock, "posix", Opt_local_lock_posix },
166 { Opt_lookupcache, "all", Opt_lookupcache_all },
167 { Opt_lookupcache, "none", Opt_lookupcache_none },
168 { Opt_lookupcache, "pos", Opt_lookupcache_positive },
169 { Opt_lookupcache, "positive", Opt_lookupcache_positive },
170 {}
171};
David Howells9954bf92019-12-10 07:31:04 -0500172
Scott Mayhewe38bb232019-12-10 07:31:12 -0500173static const struct fs_parameter_description nfs_fs_parameters = {
174 .name = "nfs",
175 .specs = nfs_param_specs,
176 .enums = nfs_param_enums,
177};
178
179enum {
180 Opt_vers_2,
181 Opt_vers_3,
182 Opt_vers_4,
183 Opt_vers_4_0,
184 Opt_vers_4_1,
185 Opt_vers_4_2,
186};
187
188static const struct constant_table nfs_vers_tokens[] = {
189 { "2", Opt_vers_2 },
190 { "3", Opt_vers_3 },
191 { "4", Opt_vers_4 },
192 { "4.0", Opt_vers_4_0 },
193 { "4.1", Opt_vers_4_1 },
194 { "4.2", Opt_vers_4_2 },
195};
196
197enum {
198 Opt_xprt_rdma,
199 Opt_xprt_rdma6,
200 Opt_xprt_tcp,
201 Opt_xprt_tcp6,
202 Opt_xprt_udp,
203 Opt_xprt_udp6,
204 nr__Opt_xprt
205};
206
207static const struct constant_table nfs_xprt_protocol_tokens[nr__Opt_xprt] = {
208 { "rdma", Opt_xprt_rdma },
209 { "rdma6", Opt_xprt_rdma6 },
210 { "tcp", Opt_xprt_tcp },
211 { "tcp6", Opt_xprt_tcp6 },
212 { "udp", Opt_xprt_udp },
213 { "udp6", Opt_xprt_udp6 },
214};
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 },
David Howells9954bf92019-12-10 07:31:04 -0500244};
245
David Howells9954bf92019-12-10 07:31:04 -0500246/*
247 * Sanity-check a server address provided by the mount command.
248 *
249 * Address family must be initialized, and address must not be
250 * the ANY address for that family.
251 */
252static int nfs_verify_server_address(struct sockaddr *addr)
253{
254 switch (addr->sa_family) {
255 case AF_INET: {
256 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
257 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
258 }
259 case AF_INET6: {
260 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
261 return !ipv6_addr_any(sa);
262 }
263 }
264
265 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
266 return 0;
267}
268
269/*
270 * Sanity check the NFS transport protocol.
271 *
272 */
David Howells5eb005c2019-12-10 07:31:06 -0500273static void nfs_validate_transport_protocol(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500274{
David Howells5eb005c2019-12-10 07:31:06 -0500275 switch (ctx->nfs_server.protocol) {
David Howells9954bf92019-12-10 07:31:04 -0500276 case XPRT_TRANSPORT_UDP:
277 case XPRT_TRANSPORT_TCP:
278 case XPRT_TRANSPORT_RDMA:
279 break;
280 default:
David Howells5eb005c2019-12-10 07:31:06 -0500281 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500282 }
283}
284
285/*
286 * For text based NFSv2/v3 mounts, the mount protocol transport default
287 * settings should depend upon the specified NFS transport.
288 */
David Howells5eb005c2019-12-10 07:31:06 -0500289static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500290{
David Howells5eb005c2019-12-10 07:31:06 -0500291 nfs_validate_transport_protocol(ctx);
David Howells9954bf92019-12-10 07:31:04 -0500292
David Howells5eb005c2019-12-10 07:31:06 -0500293 if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
294 ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
David Howells9954bf92019-12-10 07:31:04 -0500295 return;
David Howells5eb005c2019-12-10 07:31:06 -0500296 switch (ctx->nfs_server.protocol) {
David Howells9954bf92019-12-10 07:31:04 -0500297 case XPRT_TRANSPORT_UDP:
David Howells5eb005c2019-12-10 07:31:06 -0500298 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500299 break;
300 case XPRT_TRANSPORT_TCP:
301 case XPRT_TRANSPORT_RDMA:
David Howells5eb005c2019-12-10 07:31:06 -0500302 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500303 }
304}
305
306/*
307 * Add 'flavor' to 'auth_info' if not already present.
308 * Returns true if 'flavor' ends up in the list, false otherwise
309 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500310static int nfs_auth_info_add(struct fs_context *fc,
David Howellse5581002019-12-10 07:31:10 -0500311 struct nfs_auth_info *auth_info,
312 rpc_authflavor_t flavor)
David Howells9954bf92019-12-10 07:31:04 -0500313{
314 unsigned int i;
315 unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
316
317 /* make sure this flavor isn't already in the list */
318 for (i = 0; i < auth_info->flavor_len; i++) {
319 if (flavor == auth_info->flavors[i])
David Howellse5581002019-12-10 07:31:10 -0500320 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500321 }
322
Scott Mayhewce8866f02019-12-10 07:31:15 -0500323 if (auth_info->flavor_len + 1 >= max_flavor_len)
324 return nfs_invalf(fc, "NFS: too many sec= flavors");
David Howells9954bf92019-12-10 07:31:04 -0500325
326 auth_info->flavors[auth_info->flavor_len++] = flavor;
David Howellse5581002019-12-10 07:31:10 -0500327 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500328}
329
330/*
331 * Parse the value of the 'sec=' option.
332 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500333static int nfs_parse_security_flavors(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500334 struct fs_parameter *param)
David Howells9954bf92019-12-10 07:31:04 -0500335{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500336 struct nfs_fs_context *ctx = nfs_fc2context(fc);
David Howells9954bf92019-12-10 07:31:04 -0500337 rpc_authflavor_t pseudoflavor;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500338 char *string = param->string, *p;
David Howellse5581002019-12-10 07:31:10 -0500339 int ret;
David Howells9954bf92019-12-10 07:31:04 -0500340
Scott Mayhewe38bb232019-12-10 07:31:12 -0500341 dfprintk(MOUNT, "NFS: parsing %s=%s option\n", param->key, param->string);
David Howells9954bf92019-12-10 07:31:04 -0500342
Scott Mayhewe38bb232019-12-10 07:31:12 -0500343 while ((p = strsep(&string, ":")) != NULL) {
344 if (!*p)
345 continue;
346 switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
David Howells9954bf92019-12-10 07:31:04 -0500347 case Opt_sec_none:
348 pseudoflavor = RPC_AUTH_NULL;
349 break;
350 case Opt_sec_sys:
351 pseudoflavor = RPC_AUTH_UNIX;
352 break;
353 case Opt_sec_krb5:
354 pseudoflavor = RPC_AUTH_GSS_KRB5;
355 break;
356 case Opt_sec_krb5i:
357 pseudoflavor = RPC_AUTH_GSS_KRB5I;
358 break;
359 case Opt_sec_krb5p:
360 pseudoflavor = RPC_AUTH_GSS_KRB5P;
361 break;
362 case Opt_sec_lkey:
363 pseudoflavor = RPC_AUTH_GSS_LKEY;
364 break;
365 case Opt_sec_lkeyi:
366 pseudoflavor = RPC_AUTH_GSS_LKEYI;
367 break;
368 case Opt_sec_lkeyp:
369 pseudoflavor = RPC_AUTH_GSS_LKEYP;
370 break;
371 case Opt_sec_spkm:
372 pseudoflavor = RPC_AUTH_GSS_SPKM;
373 break;
374 case Opt_sec_spkmi:
375 pseudoflavor = RPC_AUTH_GSS_SPKMI;
376 break;
377 case Opt_sec_spkmp:
378 pseudoflavor = RPC_AUTH_GSS_SPKMP;
379 break;
380 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500381 return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
David Howells9954bf92019-12-10 07:31:04 -0500382 }
383
Scott Mayhew62a55d02019-12-10 07:31:14 -0500384 ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
David Howellse5581002019-12-10 07:31:10 -0500385 if (ret < 0)
386 return ret;
David Howells9954bf92019-12-10 07:31:04 -0500387 }
388
David Howellse5581002019-12-10 07:31:10 -0500389 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500390}
391
Scott Mayhew62a55d02019-12-10 07:31:14 -0500392static int nfs_parse_version_string(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500393 const char *string)
David Howells9954bf92019-12-10 07:31:04 -0500394{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500395 struct nfs_fs_context *ctx = nfs_fc2context(fc);
396
David Howells5eb005c2019-12-10 07:31:06 -0500397 ctx->flags &= ~NFS_MOUNT_VER3;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500398 switch (lookup_constant(nfs_vers_tokens, string, -1)) {
David Howells9954bf92019-12-10 07:31:04 -0500399 case Opt_vers_2:
David Howells5eb005c2019-12-10 07:31:06 -0500400 ctx->version = 2;
David Howells9954bf92019-12-10 07:31:04 -0500401 break;
402 case Opt_vers_3:
David Howells5eb005c2019-12-10 07:31:06 -0500403 ctx->flags |= NFS_MOUNT_VER3;
404 ctx->version = 3;
David Howells9954bf92019-12-10 07:31:04 -0500405 break;
406 case Opt_vers_4:
407 /* Backward compatibility option. In future,
408 * the mount program should always supply
409 * a NFSv4 minor version number.
410 */
David Howells5eb005c2019-12-10 07:31:06 -0500411 ctx->version = 4;
David Howells9954bf92019-12-10 07:31:04 -0500412 break;
413 case Opt_vers_4_0:
David Howells5eb005c2019-12-10 07:31:06 -0500414 ctx->version = 4;
415 ctx->minorversion = 0;
David Howells9954bf92019-12-10 07:31:04 -0500416 break;
417 case Opt_vers_4_1:
David Howells5eb005c2019-12-10 07:31:06 -0500418 ctx->version = 4;
419 ctx->minorversion = 1;
David Howells9954bf92019-12-10 07:31:04 -0500420 break;
421 case Opt_vers_4_2:
David Howells5eb005c2019-12-10 07:31:06 -0500422 ctx->version = 4;
423 ctx->minorversion = 2;
David Howells9954bf92019-12-10 07:31:04 -0500424 break;
425 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500426 return nfs_invalf(fc, "NFS: Unsupported NFS version");
David Howells9954bf92019-12-10 07:31:04 -0500427 }
David Howellse5581002019-12-10 07:31:10 -0500428 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500429}
430
David Howells9954bf92019-12-10 07:31:04 -0500431/*
Scott Mayhewe38bb232019-12-10 07:31:12 -0500432 * Parse a single mount parameter.
David Howells9954bf92019-12-10 07:31:04 -0500433 */
David Howellsf2aedb72019-12-10 07:31:13 -0500434static int nfs_fs_context_parse_param(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500435 struct fs_parameter *param)
David Howells9954bf92019-12-10 07:31:04 -0500436{
Scott Mayhewe38bb232019-12-10 07:31:12 -0500437 struct fs_parse_result result;
David Howellsf2aedb72019-12-10 07:31:13 -0500438 struct nfs_fs_context *ctx = nfs_fc2context(fc);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500439 unsigned short protofamily, mountfamily;
440 unsigned int len;
441 int ret, opt;
David Howells9954bf92019-12-10 07:31:04 -0500442
Scott Mayhewe38bb232019-12-10 07:31:12 -0500443 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", param->key);
David Howells9954bf92019-12-10 07:31:04 -0500444
David Howellsf2aedb72019-12-10 07:31:13 -0500445 opt = fs_parse(fc, &nfs_fs_parameters, param, &result);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500446 if (opt < 0)
447 return ctx->sloppy ? 1 : opt;
448
449 switch (opt) {
David Howellsf2aedb72019-12-10 07:31:13 -0500450 case Opt_source:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500451 if (fc->source)
452 return nfs_invalf(fc, "NFS: Multiple sources not supported");
David Howellsf2aedb72019-12-10 07:31:13 -0500453 fc->source = param->string;
454 param->string = NULL;
455 break;
456
David Howells9954bf92019-12-10 07:31:04 -0500457 /*
458 * boolean options: foo/nofoo
459 */
David Howellscbd071b2019-12-10 07:31:08 -0500460 case Opt_soft:
461 ctx->flags |= NFS_MOUNT_SOFT;
462 ctx->flags &= ~NFS_MOUNT_SOFTERR;
463 break;
464 case Opt_softerr:
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500465 ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
David Howellscbd071b2019-12-10 07:31:08 -0500466 ctx->flags &= ~NFS_MOUNT_SOFT;
467 break;
468 case Opt_hard:
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500469 ctx->flags &= ~(NFS_MOUNT_SOFT |
470 NFS_MOUNT_SOFTERR |
471 NFS_MOUNT_SOFTREVAL);
472 break;
473 case Opt_softreval:
474 if (result.negated)
475 ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
476 else
477 ctx->flags &= NFS_MOUNT_SOFTREVAL;
David Howellscbd071b2019-12-10 07:31:08 -0500478 break;
479 case Opt_posix:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500480 if (result.negated)
481 ctx->flags &= ~NFS_MOUNT_POSIX;
482 else
483 ctx->flags |= NFS_MOUNT_POSIX;
David Howellscbd071b2019-12-10 07:31:08 -0500484 break;
485 case Opt_cto:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500486 if (result.negated)
487 ctx->flags |= NFS_MOUNT_NOCTO;
488 else
489 ctx->flags &= ~NFS_MOUNT_NOCTO;
David Howellscbd071b2019-12-10 07:31:08 -0500490 break;
491 case Opt_ac:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500492 if (result.negated)
493 ctx->flags |= NFS_MOUNT_NOAC;
494 else
495 ctx->flags &= ~NFS_MOUNT_NOAC;
David Howellscbd071b2019-12-10 07:31:08 -0500496 break;
497 case Opt_lock:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500498 if (result.negated) {
499 ctx->flags |= NFS_MOUNT_NONLM;
500 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
501 } else {
502 ctx->flags &= ~NFS_MOUNT_NONLM;
503 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
504 }
David Howellscbd071b2019-12-10 07:31:08 -0500505 break;
506 case Opt_udp:
507 ctx->flags &= ~NFS_MOUNT_TCP;
508 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
509 break;
510 case Opt_tcp:
511 ctx->flags |= NFS_MOUNT_TCP;
512 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
513 break;
514 case Opt_rdma:
515 ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
516 ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500517 xprt_load_transport(param->key);
David Howellscbd071b2019-12-10 07:31:08 -0500518 break;
519 case Opt_acl:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500520 if (result.negated)
521 ctx->flags |= NFS_MOUNT_NOACL;
522 else
523 ctx->flags &= ~NFS_MOUNT_NOACL;
David Howellscbd071b2019-12-10 07:31:08 -0500524 break;
525 case Opt_rdirplus:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500526 if (result.negated)
527 ctx->flags |= NFS_MOUNT_NORDIRPLUS;
528 else
529 ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
David Howellscbd071b2019-12-10 07:31:08 -0500530 break;
531 case Opt_sharecache:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500532 if (result.negated)
533 ctx->flags |= NFS_MOUNT_UNSHARED;
534 else
535 ctx->flags &= ~NFS_MOUNT_UNSHARED;
David Howellscbd071b2019-12-10 07:31:08 -0500536 break;
537 case Opt_resvport:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500538 if (result.negated)
539 ctx->flags |= NFS_MOUNT_NORESVPORT;
540 else
541 ctx->flags &= ~NFS_MOUNT_NORESVPORT;
David Howellscbd071b2019-12-10 07:31:08 -0500542 break;
543 case Opt_fscache:
David Howellscbd071b2019-12-10 07:31:08 -0500544 kfree(ctx->fscache_uniq);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500545 ctx->fscache_uniq = param->string;
546 param->string = NULL;
547 if (result.negated)
548 ctx->options &= ~NFS_OPTION_FSCACHE;
549 else
550 ctx->options |= NFS_OPTION_FSCACHE;
David Howellscbd071b2019-12-10 07:31:08 -0500551 break;
552 case Opt_migration:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500553 if (result.negated)
554 ctx->options &= ~NFS_OPTION_MIGRATION;
555 else
556 ctx->options |= NFS_OPTION_MIGRATION;
David Howellscbd071b2019-12-10 07:31:08 -0500557 break;
David Howells9954bf92019-12-10 07:31:04 -0500558
559 /*
560 * options that take numeric values
561 */
David Howellscbd071b2019-12-10 07:31:08 -0500562 case Opt_port:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500563 if (result.uint_32 > USHRT_MAX)
564 goto out_of_bounds;
565 ctx->nfs_server.port = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500566 break;
567 case Opt_rsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500568 ctx->rsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500569 break;
570 case Opt_wsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500571 ctx->wsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500572 break;
573 case Opt_bsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500574 ctx->bsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500575 break;
576 case Opt_timeo:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500577 if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
578 goto out_of_bounds;
579 ctx->timeo = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500580 break;
581 case Opt_retrans:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500582 if (result.uint_32 > INT_MAX)
583 goto out_of_bounds;
584 ctx->retrans = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500585 break;
586 case Opt_acregmin:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500587 ctx->acregmin = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500588 break;
589 case Opt_acregmax:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500590 ctx->acregmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500591 break;
592 case Opt_acdirmin:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500593 ctx->acdirmin = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500594 break;
595 case Opt_acdirmax:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500596 ctx->acdirmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500597 break;
598 case Opt_actimeo:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500599 ctx->acregmin = result.uint_32;
600 ctx->acregmax = result.uint_32;
601 ctx->acdirmin = result.uint_32;
602 ctx->acdirmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500603 break;
604 case Opt_namelen:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500605 ctx->namlen = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500606 break;
607 case Opt_mountport:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500608 if (result.uint_32 > USHRT_MAX)
609 goto out_of_bounds;
610 ctx->mount_server.port = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500611 break;
612 case Opt_mountvers:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500613 if (result.uint_32 < NFS_MNT_VERSION ||
614 result.uint_32 > NFS_MNT3_VERSION)
615 goto out_of_bounds;
616 ctx->mount_server.version = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500617 break;
618 case Opt_minorversion:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500619 if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
620 goto out_of_bounds;
621 ctx->minorversion = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500622 break;
David Howells9954bf92019-12-10 07:31:04 -0500623
624 /*
625 * options that take text values
626 */
Scott Mayhewe38bb232019-12-10 07:31:12 -0500627 case Opt_v:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500628 ret = nfs_parse_version_string(fc, param->key + 1);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500629 if (ret < 0)
630 return ret;
631 break;
632 case Opt_vers:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500633 ret = nfs_parse_version_string(fc, param->string);
David Howellse5581002019-12-10 07:31:10 -0500634 if (ret < 0)
635 return ret;
David Howellscbd071b2019-12-10 07:31:08 -0500636 break;
637 case Opt_sec:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500638 ret = nfs_parse_security_flavors(fc, param);
David Howellse5581002019-12-10 07:31:10 -0500639 if (ret < 0)
640 return ret;
David Howellscbd071b2019-12-10 07:31:08 -0500641 break;
David Howells9954bf92019-12-10 07:31:04 -0500642
Scott Mayhewe38bb232019-12-10 07:31:12 -0500643 case Opt_proto:
644 protofamily = AF_INET;
645 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
David Howellscbd071b2019-12-10 07:31:08 -0500646 case Opt_xprt_udp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500647 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500648 /* fall through */
649 case Opt_xprt_udp:
650 ctx->flags &= ~NFS_MOUNT_TCP;
651 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500652 break;
David Howellscbd071b2019-12-10 07:31:08 -0500653 case Opt_xprt_tcp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500654 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500655 /* fall through */
656 case Opt_xprt_tcp:
657 ctx->flags |= NFS_MOUNT_TCP;
658 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
659 break;
660 case Opt_xprt_rdma6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500661 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500662 /* fall through */
663 case Opt_xprt_rdma:
664 /* vector side protocols to TCP */
665 ctx->flags |= NFS_MOUNT_TCP;
666 ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500667 xprt_load_transport(param->string);
David Howellscbd071b2019-12-10 07:31:08 -0500668 break;
669 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500670 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
David Howellscbd071b2019-12-10 07:31:08 -0500671 }
David Howells9954bf92019-12-10 07:31:04 -0500672
Scott Mayhewe38bb232019-12-10 07:31:12 -0500673 ctx->protofamily = protofamily;
674 break;
675
676 case Opt_mountproto:
677 mountfamily = AF_INET;
678 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
David Howellscbd071b2019-12-10 07:31:08 -0500679 case Opt_xprt_udp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500680 mountfamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500681 /* fall through */
682 case Opt_xprt_udp:
683 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500684 break;
David Howellscbd071b2019-12-10 07:31:08 -0500685 case Opt_xprt_tcp6:
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_tcp:
689 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500690 break;
David Howellscbd071b2019-12-10 07:31:08 -0500691 case Opt_xprt_rdma: /* not used for side protocols */
692 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500693 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
David Howellscbd071b2019-12-10 07:31:08 -0500694 }
Scott Mayhewe38bb232019-12-10 07:31:12 -0500695 ctx->mountfamily = mountfamily;
David Howellscbd071b2019-12-10 07:31:08 -0500696 break;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500697
David Howellscbd071b2019-12-10 07:31:08 -0500698 case Opt_addr:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500699 len = rpc_pton(fc->net_ns, param->string, param->size,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500700 &ctx->nfs_server.address,
701 sizeof(ctx->nfs_server._address));
702 if (len == 0)
David Howellscbd071b2019-12-10 07:31:08 -0500703 goto out_invalid_address;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500704 ctx->nfs_server.addrlen = len;
David Howellscbd071b2019-12-10 07:31:08 -0500705 break;
706 case Opt_clientaddr:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500707 kfree(ctx->client_address);
708 ctx->client_address = param->string;
709 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500710 break;
711 case Opt_mounthost:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500712 kfree(ctx->mount_server.hostname);
713 ctx->mount_server.hostname = param->string;
714 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500715 break;
716 case Opt_mountaddr:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500717 len = rpc_pton(fc->net_ns, param->string, param->size,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500718 &ctx->mount_server.address,
719 sizeof(ctx->mount_server._address));
720 if (len == 0)
David Howellscbd071b2019-12-10 07:31:08 -0500721 goto out_invalid_address;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500722 ctx->mount_server.addrlen = len;
David Howellscbd071b2019-12-10 07:31:08 -0500723 break;
724 case Opt_nconnect:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500725 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
726 goto out_of_bounds;
727 ctx->nfs_server.nconnect = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500728 break;
729 case Opt_lookupcache:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500730 switch (result.uint_32) {
David Howellscbd071b2019-12-10 07:31:08 -0500731 case Opt_lookupcache_all:
732 ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
David Howells9954bf92019-12-10 07:31:04 -0500733 break;
David Howellscbd071b2019-12-10 07:31:08 -0500734 case Opt_lookupcache_positive:
735 ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
736 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
David Howells9954bf92019-12-10 07:31:04 -0500737 break;
David Howellscbd071b2019-12-10 07:31:08 -0500738 case Opt_lookupcache_none:
739 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
David Howells9954bf92019-12-10 07:31:04 -0500740 break;
David Howellscbd071b2019-12-10 07:31:08 -0500741 default:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500742 goto out_invalid_value;
David Howellscbd071b2019-12-10 07:31:08 -0500743 }
744 break;
David Howellscbd071b2019-12-10 07:31:08 -0500745 case Opt_local_lock:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500746 switch (result.uint_32) {
David Howellscbd071b2019-12-10 07:31:08 -0500747 case Opt_local_lock_all:
748 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
749 NFS_MOUNT_LOCAL_FCNTL);
David Howells9954bf92019-12-10 07:31:04 -0500750 break;
David Howellscbd071b2019-12-10 07:31:08 -0500751 case Opt_local_lock_flock:
752 ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
David Howells9954bf92019-12-10 07:31:04 -0500753 break;
David Howellscbd071b2019-12-10 07:31:08 -0500754 case Opt_local_lock_posix:
755 ctx->flags |= 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_none:
758 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
759 NFS_MOUNT_LOCAL_FCNTL);
David Howells9954bf92019-12-10 07:31:04 -0500760 break;
David Howellscbd071b2019-12-10 07:31:08 -0500761 default:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500762 goto out_invalid_value;
David Howellscbd071b2019-12-10 07:31:08 -0500763 }
764 break;
David Howells9954bf92019-12-10 07:31:04 -0500765
766 /*
767 * Special options
768 */
David Howellscbd071b2019-12-10 07:31:08 -0500769 case Opt_sloppy:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500770 ctx->sloppy = true;
David Howellscbd071b2019-12-10 07:31:08 -0500771 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
772 break;
David Howells9954bf92019-12-10 07:31:04 -0500773 }
774
David Howellsf8ee01e2019-12-10 07:31:07 -0500775 return 0;
776
David Howellsf8ee01e2019-12-10 07:31:07 -0500777out_invalid_value:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500778 return nfs_invalf(fc, "NFS: Bad mount option value specified");
Scott Mayhewe38bb232019-12-10 07:31:12 -0500779out_invalid_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500780 return nfs_invalf(fc, "NFS: Bad IP address specified");
Scott Mayhewe38bb232019-12-10 07:31:12 -0500781out_of_bounds:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500782 nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500783 return -ERANGE;
784}
785
David Howells9954bf92019-12-10 07:31:04 -0500786/*
Scott Mayhew62a55d02019-12-10 07:31:14 -0500787 * Split fc->source into "hostname:export_path".
David Howells9954bf92019-12-10 07:31:04 -0500788 *
789 * The leftmost colon demarks the split between the server's hostname
790 * and the export path. If the hostname starts with a left square
791 * bracket, then it may contain colons.
792 *
793 * Note: caller frees hostname and export path, even on error.
794 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500795static int nfs_parse_source(struct fs_context *fc,
796 size_t maxnamlen, size_t maxpathlen)
David Howells9954bf92019-12-10 07:31:04 -0500797{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500798 struct nfs_fs_context *ctx = nfs_fc2context(fc);
799 const char *dev_name = fc->source;
David Howells9954bf92019-12-10 07:31:04 -0500800 size_t len;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500801 const char *end;
David Howells9954bf92019-12-10 07:31:04 -0500802
803 if (unlikely(!dev_name || !*dev_name)) {
804 dfprintk(MOUNT, "NFS: device name not specified\n");
805 return -EINVAL;
806 }
807
808 /* Is the host name protected with square brakcets? */
809 if (*dev_name == '[') {
810 end = strchr(++dev_name, ']');
811 if (end == NULL || end[1] != ':')
812 goto out_bad_devname;
813
814 len = end - dev_name;
815 end++;
816 } else {
Scott Mayhew62a55d02019-12-10 07:31:14 -0500817 const char *comma;
David Howells9954bf92019-12-10 07:31:04 -0500818
819 end = strchr(dev_name, ':');
820 if (end == NULL)
821 goto out_bad_devname;
822 len = end - dev_name;
823
824 /* kill possible hostname list: not supported */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500825 comma = memchr(dev_name, ',', len);
826 if (comma)
David Howells9954bf92019-12-10 07:31:04 -0500827 len = comma - dev_name;
828 }
829
830 if (len > maxnamlen)
831 goto out_hostname;
832
833 /* N.B. caller will free nfs_server.hostname in all cases */
David Howellse5581002019-12-10 07:31:10 -0500834 ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
835 if (!ctx->nfs_server.hostname)
David Howells9954bf92019-12-10 07:31:04 -0500836 goto out_nomem;
837 len = strlen(++end);
838 if (len > maxpathlen)
839 goto out_path;
David Howellse5581002019-12-10 07:31:10 -0500840 ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
841 if (!ctx->nfs_server.export_path)
David Howells9954bf92019-12-10 07:31:04 -0500842 goto out_nomem;
843
David Howellse5581002019-12-10 07:31:10 -0500844 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", ctx->nfs_server.export_path);
David Howells9954bf92019-12-10 07:31:04 -0500845 return 0;
846
847out_bad_devname:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500848 return nfs_invalf(fc, "NFS: device name not in host:path format");
David Howells9954bf92019-12-10 07:31:04 -0500849out_nomem:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500850 nfs_errorf(fc, "NFS: not enough memory to parse device name");
David Howells9954bf92019-12-10 07:31:04 -0500851 return -ENOMEM;
David Howells9954bf92019-12-10 07:31:04 -0500852out_hostname:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500853 nfs_errorf(fc, "NFS: server hostname too long");
David Howells9954bf92019-12-10 07:31:04 -0500854 return -ENAMETOOLONG;
David Howells9954bf92019-12-10 07:31:04 -0500855out_path:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500856 nfs_errorf(fc, "NFS: export pathname too long");
David Howells9954bf92019-12-10 07:31:04 -0500857 return -ENAMETOOLONG;
858}
859
David Howellsf2aedb72019-12-10 07:31:13 -0500860static inline bool is_remount_fc(struct fs_context *fc)
861{
862 return fc->root != NULL;
863}
864
David Howells9954bf92019-12-10 07:31:04 -0500865/*
David Howellse5581002019-12-10 07:31:10 -0500866 * Parse monolithic NFS2/NFS3 mount data
David Howells9954bf92019-12-10 07:31:04 -0500867 * - fills in the mount root filehandle
868 *
869 * For option strings, user space handles the following behaviors:
870 *
871 * + DNS: mapping server host name to IP address ("addr=" option)
872 *
873 * + failure mode: how to behave if a mount request can't be handled
874 * immediately ("fg/bg" option)
875 *
876 * + retry: how often to retry a mount request ("retry=" option)
877 *
878 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
879 * mountproto=tcp after mountproto=udp, and so on
880 */
David Howellsf2aedb72019-12-10 07:31:13 -0500881static int nfs23_parse_monolithic(struct fs_context *fc,
882 struct nfs_mount_data *data)
David Howells9954bf92019-12-10 07:31:04 -0500883{
David Howellsf2aedb72019-12-10 07:31:13 -0500884 struct nfs_fs_context *ctx = nfs_fc2context(fc);
Scott Mayhew62a55d02019-12-10 07:31:14 -0500885 struct nfs_fh *mntfh = ctx->mntfh;
David Howells5eb005c2019-12-10 07:31:06 -0500886 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -0500887 int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
888
889 if (data == NULL)
890 goto out_no_data;
891
David Howells5eb005c2019-12-10 07:31:06 -0500892 ctx->version = NFS_DEFAULT_VERSION;
David Howells9954bf92019-12-10 07:31:04 -0500893 switch (data->version) {
894 case 1:
895 data->namlen = 0; /* fall through */
896 case 2:
897 data->bsize = 0; /* fall through */
898 case 3:
899 if (data->flags & NFS_MOUNT_VER3)
900 goto out_no_v3;
901 data->root.size = NFS2_FHSIZE;
902 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
903 /* Turn off security negotiation */
904 extra_flags |= NFS_MOUNT_SECFLAVOUR;
905 /* fall through */
906 case 4:
907 if (data->flags & NFS_MOUNT_SECFLAVOUR)
908 goto out_no_sec;
909 /* fall through */
910 case 5:
911 memset(data->context, 0, sizeof(data->context));
912 /* fall through */
913 case 6:
914 if (data->flags & NFS_MOUNT_VER3) {
915 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
916 goto out_invalid_fh;
917 mntfh->size = data->root.size;
David Howells5eb005c2019-12-10 07:31:06 -0500918 ctx->version = 3;
David Howells9954bf92019-12-10 07:31:04 -0500919 } else {
920 mntfh->size = NFS2_FHSIZE;
David Howells5eb005c2019-12-10 07:31:06 -0500921 ctx->version = 2;
David Howells9954bf92019-12-10 07:31:04 -0500922 }
923
924
925 memcpy(mntfh->data, data->root.data, mntfh->size);
926 if (mntfh->size < sizeof(mntfh->data))
927 memset(mntfh->data + mntfh->size, 0,
928 sizeof(mntfh->data) - mntfh->size);
929
930 /*
David Howells5eb005c2019-12-10 07:31:06 -0500931 * Translate to nfs_fs_context, which nfs_fill_super
David Howells9954bf92019-12-10 07:31:04 -0500932 * can deal with.
933 */
David Howells5eb005c2019-12-10 07:31:06 -0500934 ctx->flags = data->flags & NFS_MOUNT_FLAGMASK;
935 ctx->flags |= extra_flags;
936 ctx->rsize = data->rsize;
937 ctx->wsize = data->wsize;
938 ctx->timeo = data->timeo;
939 ctx->retrans = data->retrans;
940 ctx->acregmin = data->acregmin;
941 ctx->acregmax = data->acregmax;
942 ctx->acdirmin = data->acdirmin;
943 ctx->acdirmax = data->acdirmax;
944 ctx->need_mount = false;
David Howells9954bf92019-12-10 07:31:04 -0500945
946 memcpy(sap, &data->addr, sizeof(data->addr));
David Howells5eb005c2019-12-10 07:31:06 -0500947 ctx->nfs_server.addrlen = sizeof(data->addr);
948 ctx->nfs_server.port = ntohs(data->addr.sin_port);
David Howells9954bf92019-12-10 07:31:04 -0500949 if (sap->sa_family != AF_INET ||
950 !nfs_verify_server_address(sap))
951 goto out_no_address;
952
953 if (!(data->flags & NFS_MOUNT_TCP))
David Howells5eb005c2019-12-10 07:31:06 -0500954 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500955 /* N.B. caller will free nfs_server.hostname in all cases */
David Howells5eb005c2019-12-10 07:31:06 -0500956 ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
David Howellsf2aedb72019-12-10 07:31:13 -0500957 if (!ctx->nfs_server.hostname)
958 goto out_nomem;
959
David Howells5eb005c2019-12-10 07:31:06 -0500960 ctx->namlen = data->namlen;
961 ctx->bsize = data->bsize;
David Howells9954bf92019-12-10 07:31:04 -0500962
963 if (data->flags & NFS_MOUNT_SECFLAVOUR)
David Howells5eb005c2019-12-10 07:31:06 -0500964 ctx->selected_flavor = data->pseudoflavor;
David Howells9954bf92019-12-10 07:31:04 -0500965 else
David Howells5eb005c2019-12-10 07:31:06 -0500966 ctx->selected_flavor = RPC_AUTH_UNIX;
David Howells9954bf92019-12-10 07:31:04 -0500967
968 if (!(data->flags & NFS_MOUNT_NONLM))
David Howells5eb005c2019-12-10 07:31:06 -0500969 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
David Howells9954bf92019-12-10 07:31:04 -0500970 NFS_MOUNT_LOCAL_FCNTL);
971 else
David Howells5eb005c2019-12-10 07:31:06 -0500972 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
David Howells9954bf92019-12-10 07:31:04 -0500973 NFS_MOUNT_LOCAL_FCNTL);
Scott Mayhew62a55d02019-12-10 07:31:14 -0500974
David Howells9954bf92019-12-10 07:31:04 -0500975 /*
976 * The legacy version 6 binary mount data from userspace has a
977 * field used only to transport selinux information into the
978 * the kernel. To continue to support that functionality we
979 * have a touch of selinux knowledge here in the NFS code. The
980 * userspace code converted context=blah to just blah so we are
981 * converting back to the full string selinux understands.
982 */
983 if (data->context[0]){
984#ifdef CONFIG_SECURITY_SELINUX
David Howellsf2aedb72019-12-10 07:31:13 -0500985 int ret;
986
David Howells9954bf92019-12-10 07:31:04 -0500987 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
David Howellsf2aedb72019-12-10 07:31:13 -0500988 ret = vfs_parse_fs_string(fc, "context",
989 data->context, strlen(data->context));
990 if (ret < 0)
991 return ret;
David Howells9954bf92019-12-10 07:31:04 -0500992#else
993 return -EINVAL;
994#endif
995 }
996
997 break;
998 default:
David Howellsf2aedb72019-12-10 07:31:13 -0500999 goto generic;
David Howells9954bf92019-12-10 07:31:04 -05001000 }
1001
David Howellsf2aedb72019-12-10 07:31:13 -05001002 ctx->skip_reconfig_option_check = true;
David Howells9954bf92019-12-10 07:31:04 -05001003 return 0;
1004
David Howellsf2aedb72019-12-10 07:31:13 -05001005generic:
1006 return generic_parse_monolithic(fc, data);
1007
David Howells9954bf92019-12-10 07:31:04 -05001008out_no_data:
David Howellsf2aedb72019-12-10 07:31:13 -05001009 if (is_remount_fc(fc)) {
1010 ctx->skip_reconfig_option_check = true;
1011 return 0;
1012 }
Scott Mayhewce8866f02019-12-10 07:31:15 -05001013 return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
David Howells9954bf92019-12-10 07:31:04 -05001014
1015out_no_v3:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001016 return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
David Howells9954bf92019-12-10 07:31:04 -05001017
1018out_no_sec:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001019 return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
David Howells9954bf92019-12-10 07:31:04 -05001020
1021out_nomem:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001022 dfprintk(MOUNT, "NFS: not enough memory to handle mount options");
David Howells9954bf92019-12-10 07:31:04 -05001023 return -ENOMEM;
1024
1025out_no_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001026 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
David Howells9954bf92019-12-10 07:31:04 -05001027
1028out_invalid_fh:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001029 return nfs_invalf(fc, "NFS: invalid root filehandle");
David Howells9954bf92019-12-10 07:31:04 -05001030}
1031
1032#if IS_ENABLED(CONFIG_NFS_V4)
David Howells9954bf92019-12-10 07:31:04 -05001033/*
1034 * Validate NFSv4 mount options
1035 */
David Howellsf2aedb72019-12-10 07:31:13 -05001036static int nfs4_parse_monolithic(struct fs_context *fc,
1037 struct nfs4_mount_data *data)
David Howells9954bf92019-12-10 07:31:04 -05001038{
David Howellsf2aedb72019-12-10 07:31:13 -05001039 struct nfs_fs_context *ctx = nfs_fc2context(fc);
David Howells5eb005c2019-12-10 07:31:06 -05001040 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001041 char *c;
1042
1043 if (data == NULL)
1044 goto out_no_data;
1045
David Howells5eb005c2019-12-10 07:31:06 -05001046 ctx->version = 4;
David Howells9954bf92019-12-10 07:31:04 -05001047
1048 switch (data->version) {
1049 case 1:
David Howells5eb005c2019-12-10 07:31:06 -05001050 if (data->host_addrlen > sizeof(ctx->nfs_server.address))
David Howells9954bf92019-12-10 07:31:04 -05001051 goto out_no_address;
1052 if (data->host_addrlen == 0)
1053 goto out_no_address;
David Howells5eb005c2019-12-10 07:31:06 -05001054 ctx->nfs_server.addrlen = data->host_addrlen;
David Howells9954bf92019-12-10 07:31:04 -05001055 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1056 return -EFAULT;
1057 if (!nfs_verify_server_address(sap))
1058 goto out_no_address;
David Howells5eb005c2019-12-10 07:31:06 -05001059 ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
David Howells9954bf92019-12-10 07:31:04 -05001060
1061 if (data->auth_flavourlen) {
1062 rpc_authflavor_t pseudoflavor;
1063 if (data->auth_flavourlen > 1)
1064 goto out_inval_auth;
1065 if (copy_from_user(&pseudoflavor,
1066 data->auth_flavours,
1067 sizeof(pseudoflavor)))
1068 return -EFAULT;
David Howells5eb005c2019-12-10 07:31:06 -05001069 ctx->selected_flavor = pseudoflavor;
David Howells9954bf92019-12-10 07:31:04 -05001070 } else
David Howells5eb005c2019-12-10 07:31:06 -05001071 ctx->selected_flavor = RPC_AUTH_UNIX;
David Howells9954bf92019-12-10 07:31:04 -05001072
1073 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1074 if (IS_ERR(c))
1075 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001076 ctx->nfs_server.hostname = c;
David Howells9954bf92019-12-10 07:31:04 -05001077
1078 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1079 if (IS_ERR(c))
1080 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001081 ctx->nfs_server.export_path = c;
David Howells9954bf92019-12-10 07:31:04 -05001082 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1083
1084 c = strndup_user(data->client_addr.data, 16);
1085 if (IS_ERR(c))
1086 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001087 ctx->client_address = c;
David Howells9954bf92019-12-10 07:31:04 -05001088
1089 /*
David Howellsf2aedb72019-12-10 07:31:13 -05001090 * Translate to nfs_fs_context, which nfs_fill_super
David Howells9954bf92019-12-10 07:31:04 -05001091 * can deal with.
1092 */
1093
David Howells5eb005c2019-12-10 07:31:06 -05001094 ctx->flags = data->flags & NFS4_MOUNT_FLAGMASK;
1095 ctx->rsize = data->rsize;
1096 ctx->wsize = data->wsize;
1097 ctx->timeo = data->timeo;
1098 ctx->retrans = data->retrans;
1099 ctx->acregmin = data->acregmin;
1100 ctx->acregmax = data->acregmax;
1101 ctx->acdirmin = data->acdirmin;
1102 ctx->acdirmax = data->acdirmax;
1103 ctx->nfs_server.protocol = data->proto;
1104 nfs_validate_transport_protocol(ctx);
1105 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
David Howells9954bf92019-12-10 07:31:04 -05001106 goto out_invalid_transport_udp;
1107
1108 break;
1109 default:
David Howellsf2aedb72019-12-10 07:31:13 -05001110 goto generic;
David Howells9954bf92019-12-10 07:31:04 -05001111 }
1112
David Howellsf2aedb72019-12-10 07:31:13 -05001113 ctx->skip_reconfig_option_check = true;
David Howells9954bf92019-12-10 07:31:04 -05001114 return 0;
1115
David Howellsf2aedb72019-12-10 07:31:13 -05001116generic:
1117 return generic_parse_monolithic(fc, data);
1118
David Howells9954bf92019-12-10 07:31:04 -05001119out_no_data:
David Howellsf2aedb72019-12-10 07:31:13 -05001120 if (is_remount_fc(fc)) {
1121 ctx->skip_reconfig_option_check = true;
1122 return 0;
1123 }
Scott Mayhewce8866f02019-12-10 07:31:15 -05001124 return nfs_invalf(fc, "NFS4: mount program didn't pass any mount data");
David Howells9954bf92019-12-10 07:31:04 -05001125
1126out_inval_auth:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001127 return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1128 data->auth_flavourlen);
David Howells9954bf92019-12-10 07:31:04 -05001129
1130out_no_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001131 return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
David Howells9954bf92019-12-10 07:31:04 -05001132
1133out_invalid_transport_udp:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001134 return nfs_invalf(fc, "NFSv4: Unsupported transport protocol udp");
David Howells9954bf92019-12-10 07:31:04 -05001135}
David Howells9954bf92019-12-10 07:31:04 -05001136#endif
1137
David Howellsf2aedb72019-12-10 07:31:13 -05001138/*
1139 * Parse a monolithic block of data from sys_mount().
1140 */
1141static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1142 void *data)
David Howells9954bf92019-12-10 07:31:04 -05001143{
David Howellsf2aedb72019-12-10 07:31:13 -05001144 if (fc->fs_type == &nfs_fs_type)
1145 return nfs23_parse_monolithic(fc, data);
1146
1147#if IS_ENABLED(CONFIG_NFS_V4)
1148 if (fc->fs_type == &nfs4_fs_type)
1149 return nfs4_parse_monolithic(fc, data);
1150#endif
1151
Scott Mayhewce8866f02019-12-10 07:31:15 -05001152 return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
David Howellsf2aedb72019-12-10 07:31:13 -05001153}
1154
1155/*
1156 * Validate the preparsed information in the config.
1157 */
1158static int nfs_fs_context_validate(struct fs_context *fc)
1159{
1160 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1161 struct nfs_subversion *nfs_mod;
1162 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001163 int max_namelen = PAGE_SIZE;
1164 int max_pathlen = NFS_MAXPATHLEN;
David Howellsf2aedb72019-12-10 07:31:13 -05001165 int port = 0;
1166 int ret;
David Howells9954bf92019-12-10 07:31:04 -05001167
David Howellsf2aedb72019-12-10 07:31:13 -05001168 if (!fc->source)
1169 goto out_no_device_name;
1170
1171 /* Check for sanity first. */
1172 if (ctx->minorversion && ctx->version != 4)
1173 goto out_minorversion_mismatch;
1174
1175 if (ctx->options & NFS_OPTION_MIGRATION &&
1176 (ctx->version != 4 || ctx->minorversion != 0))
1177 goto out_migration_misuse;
1178
1179 /* Verify that any proto=/mountproto= options match the address
1180 * families in the addr=/mountaddr= options.
1181 */
1182 if (ctx->protofamily != AF_UNSPEC &&
1183 ctx->protofamily != ctx->nfs_server.address.sa_family)
1184 goto out_proto_mismatch;
1185
1186 if (ctx->mountfamily != AF_UNSPEC) {
1187 if (ctx->mount_server.addrlen) {
1188 if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1189 goto out_mountproto_mismatch;
1190 } else {
1191 if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1192 goto out_mountproto_mismatch;
1193 }
1194 }
David Howells9954bf92019-12-10 07:31:04 -05001195
1196 if (!nfs_verify_server_address(sap))
1197 goto out_no_address;
1198
David Howells5eb005c2019-12-10 07:31:06 -05001199 if (ctx->version == 4) {
Scott Mayhew62a55d02019-12-10 07:31:14 -05001200 if (IS_ENABLED(CONFIG_NFS_V4)) {
1201 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1202 port = NFS_RDMA_PORT;
1203 else
1204 port = NFS_PORT;
1205 max_namelen = NFS4_MAXNAMLEN;
1206 max_pathlen = NFS4_MAXPATHLEN;
1207 nfs_validate_transport_protocol(ctx);
1208 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1209 goto out_invalid_transport_udp;
1210 ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1211 NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1212 NFS_MOUNT_LOCAL_FCNTL);
1213 } else {
1214 goto out_v4_not_compiled;
1215 }
David Howells9954bf92019-12-10 07:31:04 -05001216 } else {
David Howells5eb005c2019-12-10 07:31:06 -05001217 nfs_set_mount_transport_protocol(ctx);
1218 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
David Howells9954bf92019-12-10 07:31:04 -05001219 port = NFS_RDMA_PORT;
1220 }
1221
David Howells5eb005c2019-12-10 07:31:06 -05001222 nfs_set_port(sap, &ctx->nfs_server.port, port);
David Howells9954bf92019-12-10 07:31:04 -05001223
Scott Mayhew62a55d02019-12-10 07:31:14 -05001224 ret = nfs_parse_source(fc, max_namelen, max_pathlen);
David Howellsf2aedb72019-12-10 07:31:13 -05001225 if (ret < 0)
1226 return ret;
David Howells9954bf92019-12-10 07:31:04 -05001227
David Howellsf2aedb72019-12-10 07:31:13 -05001228 /* Load the NFS protocol module if we haven't done so yet */
Scott Mayhew62a55d02019-12-10 07:31:14 -05001229 if (!ctx->nfs_mod) {
David Howellsf2aedb72019-12-10 07:31:13 -05001230 nfs_mod = get_nfs_version(ctx->version);
1231 if (IS_ERR(nfs_mod)) {
1232 ret = PTR_ERR(nfs_mod);
1233 goto out_version_unavailable;
1234 }
Scott Mayhew62a55d02019-12-10 07:31:14 -05001235 ctx->nfs_mod = nfs_mod;
David Howellsf2aedb72019-12-10 07:31:13 -05001236 }
1237 return 0;
1238
1239out_no_device_name:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001240 return nfs_invalf(fc, "NFS: Device name not specified");
David Howells9954bf92019-12-10 07:31:04 -05001241out_v4_not_compiled:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001242 nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
David Howells9954bf92019-12-10 07:31:04 -05001243 return -EPROTONOSUPPORT;
David Howells9954bf92019-12-10 07:31:04 -05001244out_invalid_transport_udp:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001245 return nfs_invalf(fc, "NFSv4: Unsupported transport protocol udp");
David Howells9954bf92019-12-10 07:31:04 -05001246out_no_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001247 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
David Howellsf2aedb72019-12-10 07:31:13 -05001248out_mountproto_mismatch:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001249 return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
David Howellsf2aedb72019-12-10 07:31:13 -05001250out_proto_mismatch:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001251 return nfs_invalf(fc, "NFS: Server address does not match proto= option");
David Howellsf2aedb72019-12-10 07:31:13 -05001252out_minorversion_mismatch:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001253 return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
David Howellsf2aedb72019-12-10 07:31:13 -05001254 ctx->version, ctx->minorversion);
David Howellsf2aedb72019-12-10 07:31:13 -05001255out_migration_misuse:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001256 return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
David Howellsf2aedb72019-12-10 07:31:13 -05001257out_version_unavailable:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001258 nfs_errorf(fc, "NFS: Version unavailable");
David Howellsf2aedb72019-12-10 07:31:13 -05001259 return ret;
David Howells9954bf92019-12-10 07:31:04 -05001260}
David Howellsf2aedb72019-12-10 07:31:13 -05001261
1262/*
1263 * Create an NFS superblock by the appropriate method.
1264 */
1265static int nfs_get_tree(struct fs_context *fc)
1266{
1267 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1268 int err = nfs_fs_context_validate(fc);
1269
1270 if (err)
1271 return err;
1272 if (!ctx->internal)
Scott Mayhew62a55d02019-12-10 07:31:14 -05001273 return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
David Howellsf2aedb72019-12-10 07:31:13 -05001274 else
1275 return nfs_get_tree_common(fc);
1276}
1277
1278/*
1279 * Handle duplication of a configuration. The caller copied *src into *sc, but
1280 * it can't deal with resource pointers in the filesystem context, so we have
1281 * to do that. We need to clear pointers, copy data or get extra refs as
1282 * appropriate.
1283 */
1284static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1285{
1286 struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1287
1288 ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1289 if (!ctx)
1290 return -ENOMEM;
1291
Scott Mayhew62a55d02019-12-10 07:31:14 -05001292 ctx->mntfh = nfs_alloc_fhandle();
1293 if (!ctx->mntfh) {
David Howellsf2aedb72019-12-10 07:31:13 -05001294 kfree(ctx);
1295 return -ENOMEM;
1296 }
Scott Mayhew62a55d02019-12-10 07:31:14 -05001297 nfs_copy_fh(ctx->mntfh, src->mntfh);
David Howellsf2aedb72019-12-10 07:31:13 -05001298
Scott Mayhew62a55d02019-12-10 07:31:14 -05001299 __module_get(ctx->nfs_mod->owner);
David Howellsf2aedb72019-12-10 07:31:13 -05001300 ctx->client_address = NULL;
1301 ctx->mount_server.hostname = NULL;
1302 ctx->nfs_server.export_path = NULL;
1303 ctx->nfs_server.hostname = NULL;
1304 ctx->fscache_uniq = NULL;
David Howellsf2aedb72019-12-10 07:31:13 -05001305 ctx->clone_data.fattr = NULL;
1306 fc->fs_private = ctx;
1307 return 0;
1308}
1309
1310static void nfs_fs_context_free(struct fs_context *fc)
1311{
1312 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1313
1314 if (ctx) {
Scott Mayhew62a55d02019-12-10 07:31:14 -05001315 if (ctx->server)
1316 nfs_free_server(ctx->server);
1317 if (ctx->nfs_mod)
1318 put_nfs_version(ctx->nfs_mod);
David Howellsf2aedb72019-12-10 07:31:13 -05001319 kfree(ctx->client_address);
1320 kfree(ctx->mount_server.hostname);
1321 kfree(ctx->nfs_server.export_path);
1322 kfree(ctx->nfs_server.hostname);
1323 kfree(ctx->fscache_uniq);
Scott Mayhew62a55d02019-12-10 07:31:14 -05001324 nfs_free_fhandle(ctx->mntfh);
David Howellsf2aedb72019-12-10 07:31:13 -05001325 nfs_free_fattr(ctx->clone_data.fattr);
1326 kfree(ctx);
1327 }
1328}
1329
1330static const struct fs_context_operations nfs_fs_context_ops = {
1331 .free = nfs_fs_context_free,
1332 .dup = nfs_fs_context_dup,
1333 .parse_param = nfs_fs_context_parse_param,
1334 .parse_monolithic = nfs_fs_context_parse_monolithic,
1335 .get_tree = nfs_get_tree,
1336 .reconfigure = nfs_reconfigure,
1337};
1338
1339/*
1340 * Prepare superblock configuration. We use the namespaces attached to the
1341 * context. This may be the current process's namespaces, or it may be a
1342 * container's namespaces.
1343 */
1344static int nfs_init_fs_context(struct fs_context *fc)
1345{
1346 struct nfs_fs_context *ctx;
1347
1348 ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1349 if (unlikely(!ctx))
1350 return -ENOMEM;
1351
Scott Mayhew62a55d02019-12-10 07:31:14 -05001352 ctx->mntfh = nfs_alloc_fhandle();
1353 if (unlikely(!ctx->mntfh)) {
David Howellsf2aedb72019-12-10 07:31:13 -05001354 kfree(ctx);
1355 return -ENOMEM;
1356 }
1357
1358 ctx->protofamily = AF_UNSPEC;
1359 ctx->mountfamily = AF_UNSPEC;
1360 ctx->mount_server.port = NFS_UNSPEC_PORT;
1361
1362 if (fc->root) {
1363 /* reconfigure, start with the current config */
1364 struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1365 struct net *net = nfss->nfs_client->cl_net;
1366
1367 ctx->flags = nfss->flags;
1368 ctx->rsize = nfss->rsize;
1369 ctx->wsize = nfss->wsize;
1370 ctx->retrans = nfss->client->cl_timeout->to_retries;
1371 ctx->selected_flavor = nfss->client->cl_auth->au_flavor;
1372 ctx->acregmin = nfss->acregmin / HZ;
1373 ctx->acregmax = nfss->acregmax / HZ;
1374 ctx->acdirmin = nfss->acdirmin / HZ;
1375 ctx->acdirmax = nfss->acdirmax / HZ;
1376 ctx->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1377 ctx->nfs_server.port = nfss->port;
1378 ctx->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1379 ctx->version = nfss->nfs_client->rpc_ops->version;
1380 ctx->minorversion = nfss->nfs_client->cl_minorversion;
1381
1382 memcpy(&ctx->nfs_server.address, &nfss->nfs_client->cl_addr,
1383 ctx->nfs_server.addrlen);
1384
1385 if (fc->net_ns != net) {
1386 put_net(fc->net_ns);
1387 fc->net_ns = get_net(net);
1388 }
1389
Scott Mayhew62a55d02019-12-10 07:31:14 -05001390 ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1391 __module_get(ctx->nfs_mod->owner);
David Howellsf2aedb72019-12-10 07:31:13 -05001392 } else {
1393 /* defaults */
1394 ctx->timeo = NFS_UNSPEC_TIMEO;
1395 ctx->retrans = NFS_UNSPEC_RETRANS;
1396 ctx->acregmin = NFS_DEF_ACREGMIN;
1397 ctx->acregmax = NFS_DEF_ACREGMAX;
1398 ctx->acdirmin = NFS_DEF_ACDIRMIN;
1399 ctx->acdirmax = NFS_DEF_ACDIRMAX;
1400 ctx->nfs_server.port = NFS_UNSPEC_PORT;
1401 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1402 ctx->selected_flavor = RPC_AUTH_MAXFLAVOR;
1403 ctx->minorversion = 0;
1404 ctx->need_mount = true;
1405 }
David Howellsf2aedb72019-12-10 07:31:13 -05001406 fc->fs_private = ctx;
1407 fc->ops = &nfs_fs_context_ops;
1408 return 0;
1409}
1410
1411struct file_system_type nfs_fs_type = {
1412 .owner = THIS_MODULE,
1413 .name = "nfs",
1414 .init_fs_context = nfs_init_fs_context,
1415 .parameters = &nfs_fs_parameters,
1416 .kill_sb = nfs_kill_super,
1417 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1418};
1419MODULE_ALIAS_FS("nfs");
1420EXPORT_SYMBOL_GPL(nfs_fs_type);
1421
1422#if IS_ENABLED(CONFIG_NFS_V4)
1423struct file_system_type nfs4_fs_type = {
1424 .owner = THIS_MODULE,
1425 .name = "nfs4",
1426 .init_fs_context = nfs_init_fs_context,
1427 .parameters = &nfs_fs_parameters,
1428 .kill_sb = nfs_kill_super,
1429 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1430};
1431MODULE_ALIAS_FS("nfs4");
1432MODULE_ALIAS("nfs4");
1433EXPORT_SYMBOL_GPL(nfs4_fs_type);
1434#endif /* CONFIG_NFS_V4 */