blob: 39f980a0ee48abfe30acca57490b6e3180905914 [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
Al Viro2710c957a2019-09-06 22:12:08 -040086enum {
87 Opt_local_lock_all,
88 Opt_local_lock_flock,
89 Opt_local_lock_none,
90 Opt_local_lock_posix,
91};
92
Al Viro5eede622019-12-16 13:33:32 -050093static const struct constant_table nfs_param_enums_local_lock[] = {
Al Viro2710c957a2019-09-06 22:12:08 -040094 { "all", Opt_local_lock_all },
95 { "flock", Opt_local_lock_flock },
96 { "none", Opt_local_lock_none },
97 {}
98};
99
100enum {
101 Opt_lookupcache_all,
102 Opt_lookupcache_none,
103 Opt_lookupcache_positive,
104};
105
Al Viro5eede622019-12-16 13:33:32 -0500106static const struct constant_table nfs_param_enums_lookupcache[] = {
Al Viro2710c957a2019-09-06 22:12:08 -0400107 { "all", Opt_lookupcache_all },
108 { "none", Opt_lookupcache_none },
109 { "pos", Opt_lookupcache_positive },
110 { "positive", Opt_lookupcache_positive },
111 {}
112};
113
Al Virod7167b12019-09-07 07:23:15 -0400114static const struct fs_parameter_spec nfs_fs_parameters[] = {
Scott Mayhewe38bb232019-12-10 07:31:12 -0500115 fsparam_flag_no("ac", Opt_ac),
116 fsparam_u32 ("acdirmax", Opt_acdirmax),
117 fsparam_u32 ("acdirmin", Opt_acdirmin),
118 fsparam_flag_no("acl", Opt_acl),
119 fsparam_u32 ("acregmax", Opt_acregmax),
120 fsparam_u32 ("acregmin", Opt_acregmin),
121 fsparam_u32 ("actimeo", Opt_actimeo),
122 fsparam_string("addr", Opt_addr),
123 fsparam_flag ("bg", Opt_bg),
124 fsparam_u32 ("bsize", Opt_bsize),
125 fsparam_string("clientaddr", Opt_clientaddr),
126 fsparam_flag_no("cto", Opt_cto),
127 fsparam_flag ("fg", Opt_fg),
128 __fsparam(fs_param_is_string, "fsc", Opt_fscache,
Al Viro2710c957a2019-09-06 22:12:08 -0400129 fs_param_neg_with_no|fs_param_v_optional, NULL),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500130 fsparam_flag ("hard", Opt_hard),
131 __fsparam(fs_param_is_flag, "intr", Opt_intr,
Al Viro2710c957a2019-09-06 22:12:08 -0400132 fs_param_neg_with_no|fs_param_deprecated, NULL),
133 fsparam_enum ("local_lock", Opt_local_lock, nfs_param_enums_local_lock),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500134 fsparam_flag_no("lock", Opt_lock),
Al Viro2710c957a2019-09-06 22:12:08 -0400135 fsparam_enum ("lookupcache", Opt_lookupcache, nfs_param_enums_lookupcache),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500136 fsparam_flag_no("migration", Opt_migration),
137 fsparam_u32 ("minorversion", Opt_minorversion),
138 fsparam_string("mountaddr", Opt_mountaddr),
139 fsparam_string("mounthost", Opt_mounthost),
140 fsparam_u32 ("mountport", Opt_mountport),
141 fsparam_string("mountproto", Opt_mountproto),
142 fsparam_u32 ("mountvers", Opt_mountvers),
143 fsparam_u32 ("namlen", Opt_namelen),
144 fsparam_u32 ("nconnect", Opt_nconnect),
145 fsparam_string("nfsvers", Opt_vers),
146 fsparam_u32 ("port", Opt_port),
147 fsparam_flag_no("posix", Opt_posix),
148 fsparam_string("proto", Opt_proto),
149 fsparam_flag_no("rdirplus", Opt_rdirplus),
150 fsparam_flag ("rdma", Opt_rdma),
151 fsparam_flag_no("resvport", Opt_resvport),
152 fsparam_u32 ("retrans", Opt_retrans),
153 fsparam_string("retry", Opt_retry),
154 fsparam_u32 ("rsize", Opt_rsize),
155 fsparam_string("sec", Opt_sec),
156 fsparam_flag_no("sharecache", Opt_sharecache),
157 fsparam_flag ("sloppy", Opt_sloppy),
158 fsparam_flag ("soft", Opt_soft),
159 fsparam_flag ("softerr", Opt_softerr),
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500160 fsparam_flag ("softreval", Opt_softreval),
Scott Mayhewe38bb232019-12-10 07:31:12 -0500161 fsparam_string("source", Opt_source),
162 fsparam_flag ("tcp", Opt_tcp),
163 fsparam_u32 ("timeo", Opt_timeo),
164 fsparam_flag ("udp", Opt_udp),
165 fsparam_flag ("v2", Opt_v),
166 fsparam_flag ("v3", Opt_v),
167 fsparam_flag ("v4", Opt_v),
168 fsparam_flag ("v4.0", Opt_v),
169 fsparam_flag ("v4.1", Opt_v),
170 fsparam_flag ("v4.2", Opt_v),
171 fsparam_string("vers", Opt_vers),
172 fsparam_u32 ("wsize", Opt_wsize),
173 {}
David Howells9954bf92019-12-10 07:31:04 -0500174};
175
Scott Mayhewe38bb232019-12-10 07:31:12 -0500176enum {
177 Opt_vers_2,
178 Opt_vers_3,
179 Opt_vers_4,
180 Opt_vers_4_0,
181 Opt_vers_4_1,
182 Opt_vers_4_2,
183};
184
185static const struct constant_table nfs_vers_tokens[] = {
186 { "2", Opt_vers_2 },
187 { "3", Opt_vers_3 },
188 { "4", Opt_vers_4 },
189 { "4.0", Opt_vers_4_0 },
190 { "4.1", Opt_vers_4_1 },
191 { "4.2", Opt_vers_4_2 },
192};
193
194enum {
195 Opt_xprt_rdma,
196 Opt_xprt_rdma6,
197 Opt_xprt_tcp,
198 Opt_xprt_tcp6,
199 Opt_xprt_udp,
200 Opt_xprt_udp6,
201 nr__Opt_xprt
202};
203
204static const struct constant_table nfs_xprt_protocol_tokens[nr__Opt_xprt] = {
205 { "rdma", Opt_xprt_rdma },
206 { "rdma6", Opt_xprt_rdma6 },
207 { "tcp", Opt_xprt_tcp },
208 { "tcp6", Opt_xprt_tcp6 },
209 { "udp", Opt_xprt_udp },
210 { "udp6", Opt_xprt_udp6 },
211};
212
213enum {
214 Opt_sec_krb5,
215 Opt_sec_krb5i,
216 Opt_sec_krb5p,
217 Opt_sec_lkey,
218 Opt_sec_lkeyi,
219 Opt_sec_lkeyp,
220 Opt_sec_none,
221 Opt_sec_spkm,
222 Opt_sec_spkmi,
223 Opt_sec_spkmp,
224 Opt_sec_sys,
225 nr__Opt_sec
226};
227
228static const struct constant_table nfs_secflavor_tokens[] = {
229 { "krb5", Opt_sec_krb5 },
230 { "krb5i", Opt_sec_krb5i },
231 { "krb5p", Opt_sec_krb5p },
232 { "lkey", Opt_sec_lkey },
233 { "lkeyi", Opt_sec_lkeyi },
234 { "lkeyp", Opt_sec_lkeyp },
235 { "none", Opt_sec_none },
236 { "null", Opt_sec_none },
237 { "spkm3", Opt_sec_spkm },
238 { "spkm3i", Opt_sec_spkmi },
239 { "spkm3p", Opt_sec_spkmp },
240 { "sys", Opt_sec_sys },
David Howells9954bf92019-12-10 07:31:04 -0500241};
242
David Howells9954bf92019-12-10 07:31:04 -0500243/*
244 * Sanity-check a server address provided by the mount command.
245 *
246 * Address family must be initialized, and address must not be
247 * the ANY address for that family.
248 */
249static int nfs_verify_server_address(struct sockaddr *addr)
250{
251 switch (addr->sa_family) {
252 case AF_INET: {
253 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
254 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
255 }
256 case AF_INET6: {
257 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
258 return !ipv6_addr_any(sa);
259 }
260 }
261
262 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
263 return 0;
264}
265
266/*
267 * Sanity check the NFS transport protocol.
268 *
269 */
David Howells5eb005c2019-12-10 07:31:06 -0500270static void nfs_validate_transport_protocol(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500271{
David Howells5eb005c2019-12-10 07:31:06 -0500272 switch (ctx->nfs_server.protocol) {
David Howells9954bf92019-12-10 07:31:04 -0500273 case XPRT_TRANSPORT_UDP:
274 case XPRT_TRANSPORT_TCP:
275 case XPRT_TRANSPORT_RDMA:
276 break;
277 default:
David Howells5eb005c2019-12-10 07:31:06 -0500278 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500279 }
280}
281
282/*
283 * For text based NFSv2/v3 mounts, the mount protocol transport default
284 * settings should depend upon the specified NFS transport.
285 */
David Howells5eb005c2019-12-10 07:31:06 -0500286static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
David Howells9954bf92019-12-10 07:31:04 -0500287{
David Howells5eb005c2019-12-10 07:31:06 -0500288 nfs_validate_transport_protocol(ctx);
David Howells9954bf92019-12-10 07:31:04 -0500289
David Howells5eb005c2019-12-10 07:31:06 -0500290 if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
291 ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
David Howells9954bf92019-12-10 07:31:04 -0500292 return;
David Howells5eb005c2019-12-10 07:31:06 -0500293 switch (ctx->nfs_server.protocol) {
David Howells9954bf92019-12-10 07:31:04 -0500294 case XPRT_TRANSPORT_UDP:
David Howells5eb005c2019-12-10 07:31:06 -0500295 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500296 break;
297 case XPRT_TRANSPORT_TCP:
298 case XPRT_TRANSPORT_RDMA:
David Howells5eb005c2019-12-10 07:31:06 -0500299 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500300 }
301}
302
303/*
304 * Add 'flavor' to 'auth_info' if not already present.
305 * Returns true if 'flavor' ends up in the list, false otherwise
306 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500307static int nfs_auth_info_add(struct fs_context *fc,
David Howellse5581002019-12-10 07:31:10 -0500308 struct nfs_auth_info *auth_info,
309 rpc_authflavor_t flavor)
David Howells9954bf92019-12-10 07:31:04 -0500310{
311 unsigned int i;
312 unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
313
314 /* make sure this flavor isn't already in the list */
315 for (i = 0; i < auth_info->flavor_len; i++) {
316 if (flavor == auth_info->flavors[i])
David Howellse5581002019-12-10 07:31:10 -0500317 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500318 }
319
Scott Mayhewce8866f02019-12-10 07:31:15 -0500320 if (auth_info->flavor_len + 1 >= max_flavor_len)
321 return nfs_invalf(fc, "NFS: too many sec= flavors");
David Howells9954bf92019-12-10 07:31:04 -0500322
323 auth_info->flavors[auth_info->flavor_len++] = flavor;
David Howellse5581002019-12-10 07:31:10 -0500324 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500325}
326
327/*
328 * Parse the value of the 'sec=' option.
329 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500330static int nfs_parse_security_flavors(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500331 struct fs_parameter *param)
David Howells9954bf92019-12-10 07:31:04 -0500332{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500333 struct nfs_fs_context *ctx = nfs_fc2context(fc);
David Howells9954bf92019-12-10 07:31:04 -0500334 rpc_authflavor_t pseudoflavor;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500335 char *string = param->string, *p;
David Howellse5581002019-12-10 07:31:10 -0500336 int ret;
David Howells9954bf92019-12-10 07:31:04 -0500337
Scott Mayhewe38bb232019-12-10 07:31:12 -0500338 dfprintk(MOUNT, "NFS: parsing %s=%s option\n", param->key, param->string);
David Howells9954bf92019-12-10 07:31:04 -0500339
Scott Mayhewe38bb232019-12-10 07:31:12 -0500340 while ((p = strsep(&string, ":")) != NULL) {
341 if (!*p)
342 continue;
343 switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
David Howells9954bf92019-12-10 07:31:04 -0500344 case Opt_sec_none:
345 pseudoflavor = RPC_AUTH_NULL;
346 break;
347 case Opt_sec_sys:
348 pseudoflavor = RPC_AUTH_UNIX;
349 break;
350 case Opt_sec_krb5:
351 pseudoflavor = RPC_AUTH_GSS_KRB5;
352 break;
353 case Opt_sec_krb5i:
354 pseudoflavor = RPC_AUTH_GSS_KRB5I;
355 break;
356 case Opt_sec_krb5p:
357 pseudoflavor = RPC_AUTH_GSS_KRB5P;
358 break;
359 case Opt_sec_lkey:
360 pseudoflavor = RPC_AUTH_GSS_LKEY;
361 break;
362 case Opt_sec_lkeyi:
363 pseudoflavor = RPC_AUTH_GSS_LKEYI;
364 break;
365 case Opt_sec_lkeyp:
366 pseudoflavor = RPC_AUTH_GSS_LKEYP;
367 break;
368 case Opt_sec_spkm:
369 pseudoflavor = RPC_AUTH_GSS_SPKM;
370 break;
371 case Opt_sec_spkmi:
372 pseudoflavor = RPC_AUTH_GSS_SPKMI;
373 break;
374 case Opt_sec_spkmp:
375 pseudoflavor = RPC_AUTH_GSS_SPKMP;
376 break;
377 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500378 return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
David Howells9954bf92019-12-10 07:31:04 -0500379 }
380
Scott Mayhew62a55d02019-12-10 07:31:14 -0500381 ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
David Howellse5581002019-12-10 07:31:10 -0500382 if (ret < 0)
383 return ret;
David Howells9954bf92019-12-10 07:31:04 -0500384 }
385
David Howellse5581002019-12-10 07:31:10 -0500386 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500387}
388
Scott Mayhew62a55d02019-12-10 07:31:14 -0500389static int nfs_parse_version_string(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500390 const char *string)
David Howells9954bf92019-12-10 07:31:04 -0500391{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500392 struct nfs_fs_context *ctx = nfs_fc2context(fc);
393
David Howells5eb005c2019-12-10 07:31:06 -0500394 ctx->flags &= ~NFS_MOUNT_VER3;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500395 switch (lookup_constant(nfs_vers_tokens, string, -1)) {
David Howells9954bf92019-12-10 07:31:04 -0500396 case Opt_vers_2:
David Howells5eb005c2019-12-10 07:31:06 -0500397 ctx->version = 2;
David Howells9954bf92019-12-10 07:31:04 -0500398 break;
399 case Opt_vers_3:
David Howells5eb005c2019-12-10 07:31:06 -0500400 ctx->flags |= NFS_MOUNT_VER3;
401 ctx->version = 3;
David Howells9954bf92019-12-10 07:31:04 -0500402 break;
403 case Opt_vers_4:
404 /* Backward compatibility option. In future,
405 * the mount program should always supply
406 * a NFSv4 minor version number.
407 */
David Howells5eb005c2019-12-10 07:31:06 -0500408 ctx->version = 4;
David Howells9954bf92019-12-10 07:31:04 -0500409 break;
410 case Opt_vers_4_0:
David Howells5eb005c2019-12-10 07:31:06 -0500411 ctx->version = 4;
412 ctx->minorversion = 0;
David Howells9954bf92019-12-10 07:31:04 -0500413 break;
414 case Opt_vers_4_1:
David Howells5eb005c2019-12-10 07:31:06 -0500415 ctx->version = 4;
416 ctx->minorversion = 1;
David Howells9954bf92019-12-10 07:31:04 -0500417 break;
418 case Opt_vers_4_2:
David Howells5eb005c2019-12-10 07:31:06 -0500419 ctx->version = 4;
420 ctx->minorversion = 2;
David Howells9954bf92019-12-10 07:31:04 -0500421 break;
422 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500423 return nfs_invalf(fc, "NFS: Unsupported NFS version");
David Howells9954bf92019-12-10 07:31:04 -0500424 }
David Howellse5581002019-12-10 07:31:10 -0500425 return 0;
David Howells9954bf92019-12-10 07:31:04 -0500426}
427
David Howells9954bf92019-12-10 07:31:04 -0500428/*
Scott Mayhewe38bb232019-12-10 07:31:12 -0500429 * Parse a single mount parameter.
David Howells9954bf92019-12-10 07:31:04 -0500430 */
David Howellsf2aedb72019-12-10 07:31:13 -0500431static int nfs_fs_context_parse_param(struct fs_context *fc,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500432 struct fs_parameter *param)
David Howells9954bf92019-12-10 07:31:04 -0500433{
Scott Mayhewe38bb232019-12-10 07:31:12 -0500434 struct fs_parse_result result;
David Howellsf2aedb72019-12-10 07:31:13 -0500435 struct nfs_fs_context *ctx = nfs_fc2context(fc);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500436 unsigned short protofamily, mountfamily;
437 unsigned int len;
438 int ret, opt;
David Howells9954bf92019-12-10 07:31:04 -0500439
Scott Mayhewe38bb232019-12-10 07:31:12 -0500440 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", param->key);
David Howells9954bf92019-12-10 07:31:04 -0500441
Al Virod7167b12019-09-07 07:23:15 -0400442 opt = fs_parse(fc, nfs_fs_parameters, param, &result);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500443 if (opt < 0)
444 return ctx->sloppy ? 1 : opt;
445
446 switch (opt) {
David Howellsf2aedb72019-12-10 07:31:13 -0500447 case Opt_source:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500448 if (fc->source)
449 return nfs_invalf(fc, "NFS: Multiple sources not supported");
David Howellsf2aedb72019-12-10 07:31:13 -0500450 fc->source = param->string;
451 param->string = NULL;
452 break;
453
David Howells9954bf92019-12-10 07:31:04 -0500454 /*
455 * boolean options: foo/nofoo
456 */
David Howellscbd071b2019-12-10 07:31:08 -0500457 case Opt_soft:
458 ctx->flags |= NFS_MOUNT_SOFT;
459 ctx->flags &= ~NFS_MOUNT_SOFTERR;
460 break;
461 case Opt_softerr:
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500462 ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
David Howellscbd071b2019-12-10 07:31:08 -0500463 ctx->flags &= ~NFS_MOUNT_SOFT;
464 break;
465 case Opt_hard:
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500466 ctx->flags &= ~(NFS_MOUNT_SOFT |
467 NFS_MOUNT_SOFTERR |
468 NFS_MOUNT_SOFTREVAL);
469 break;
470 case Opt_softreval:
471 if (result.negated)
472 ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
473 else
474 ctx->flags &= NFS_MOUNT_SOFTREVAL;
David Howellscbd071b2019-12-10 07:31:08 -0500475 break;
476 case Opt_posix:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500477 if (result.negated)
478 ctx->flags &= ~NFS_MOUNT_POSIX;
479 else
480 ctx->flags |= NFS_MOUNT_POSIX;
David Howellscbd071b2019-12-10 07:31:08 -0500481 break;
482 case Opt_cto:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500483 if (result.negated)
484 ctx->flags |= NFS_MOUNT_NOCTO;
485 else
486 ctx->flags &= ~NFS_MOUNT_NOCTO;
David Howellscbd071b2019-12-10 07:31:08 -0500487 break;
488 case Opt_ac:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500489 if (result.negated)
490 ctx->flags |= NFS_MOUNT_NOAC;
491 else
492 ctx->flags &= ~NFS_MOUNT_NOAC;
David Howellscbd071b2019-12-10 07:31:08 -0500493 break;
494 case Opt_lock:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500495 if (result.negated) {
496 ctx->flags |= NFS_MOUNT_NONLM;
497 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
498 } else {
499 ctx->flags &= ~NFS_MOUNT_NONLM;
500 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
501 }
David Howellscbd071b2019-12-10 07:31:08 -0500502 break;
503 case Opt_udp:
504 ctx->flags &= ~NFS_MOUNT_TCP;
505 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
506 break;
507 case Opt_tcp:
508 ctx->flags |= NFS_MOUNT_TCP;
509 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
510 break;
511 case Opt_rdma:
512 ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
513 ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500514 xprt_load_transport(param->key);
David Howellscbd071b2019-12-10 07:31:08 -0500515 break;
516 case Opt_acl:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500517 if (result.negated)
518 ctx->flags |= NFS_MOUNT_NOACL;
519 else
520 ctx->flags &= ~NFS_MOUNT_NOACL;
David Howellscbd071b2019-12-10 07:31:08 -0500521 break;
522 case Opt_rdirplus:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500523 if (result.negated)
524 ctx->flags |= NFS_MOUNT_NORDIRPLUS;
525 else
526 ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
David Howellscbd071b2019-12-10 07:31:08 -0500527 break;
528 case Opt_sharecache:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500529 if (result.negated)
530 ctx->flags |= NFS_MOUNT_UNSHARED;
531 else
532 ctx->flags &= ~NFS_MOUNT_UNSHARED;
David Howellscbd071b2019-12-10 07:31:08 -0500533 break;
534 case Opt_resvport:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500535 if (result.negated)
536 ctx->flags |= NFS_MOUNT_NORESVPORT;
537 else
538 ctx->flags &= ~NFS_MOUNT_NORESVPORT;
David Howellscbd071b2019-12-10 07:31:08 -0500539 break;
540 case Opt_fscache:
David Howellscbd071b2019-12-10 07:31:08 -0500541 kfree(ctx->fscache_uniq);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500542 ctx->fscache_uniq = param->string;
543 param->string = NULL;
544 if (result.negated)
545 ctx->options &= ~NFS_OPTION_FSCACHE;
546 else
547 ctx->options |= NFS_OPTION_FSCACHE;
David Howellscbd071b2019-12-10 07:31:08 -0500548 break;
549 case Opt_migration:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500550 if (result.negated)
551 ctx->options &= ~NFS_OPTION_MIGRATION;
552 else
553 ctx->options |= NFS_OPTION_MIGRATION;
David Howellscbd071b2019-12-10 07:31:08 -0500554 break;
David Howells9954bf92019-12-10 07:31:04 -0500555
556 /*
557 * options that take numeric values
558 */
David Howellscbd071b2019-12-10 07:31:08 -0500559 case Opt_port:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500560 if (result.uint_32 > USHRT_MAX)
561 goto out_of_bounds;
562 ctx->nfs_server.port = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500563 break;
564 case Opt_rsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500565 ctx->rsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500566 break;
567 case Opt_wsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500568 ctx->wsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500569 break;
570 case Opt_bsize:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500571 ctx->bsize = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500572 break;
573 case Opt_timeo:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500574 if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
575 goto out_of_bounds;
576 ctx->timeo = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500577 break;
578 case Opt_retrans:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500579 if (result.uint_32 > INT_MAX)
580 goto out_of_bounds;
581 ctx->retrans = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500582 break;
583 case Opt_acregmin:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500584 ctx->acregmin = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500585 break;
586 case Opt_acregmax:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500587 ctx->acregmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500588 break;
589 case Opt_acdirmin:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500590 ctx->acdirmin = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500591 break;
592 case Opt_acdirmax:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500593 ctx->acdirmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500594 break;
595 case Opt_actimeo:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500596 ctx->acregmin = result.uint_32;
597 ctx->acregmax = result.uint_32;
598 ctx->acdirmin = result.uint_32;
599 ctx->acdirmax = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500600 break;
601 case Opt_namelen:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500602 ctx->namlen = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500603 break;
604 case Opt_mountport:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500605 if (result.uint_32 > USHRT_MAX)
606 goto out_of_bounds;
607 ctx->mount_server.port = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500608 break;
609 case Opt_mountvers:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500610 if (result.uint_32 < NFS_MNT_VERSION ||
611 result.uint_32 > NFS_MNT3_VERSION)
612 goto out_of_bounds;
613 ctx->mount_server.version = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500614 break;
615 case Opt_minorversion:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500616 if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
617 goto out_of_bounds;
618 ctx->minorversion = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500619 break;
David Howells9954bf92019-12-10 07:31:04 -0500620
621 /*
622 * options that take text values
623 */
Scott Mayhewe38bb232019-12-10 07:31:12 -0500624 case Opt_v:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500625 ret = nfs_parse_version_string(fc, param->key + 1);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500626 if (ret < 0)
627 return ret;
628 break;
629 case Opt_vers:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500630 ret = nfs_parse_version_string(fc, param->string);
David Howellse5581002019-12-10 07:31:10 -0500631 if (ret < 0)
632 return ret;
David Howellscbd071b2019-12-10 07:31:08 -0500633 break;
634 case Opt_sec:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500635 ret = nfs_parse_security_flavors(fc, param);
David Howellse5581002019-12-10 07:31:10 -0500636 if (ret < 0)
637 return ret;
David Howellscbd071b2019-12-10 07:31:08 -0500638 break;
David Howells9954bf92019-12-10 07:31:04 -0500639
Scott Mayhewe38bb232019-12-10 07:31:12 -0500640 case Opt_proto:
641 protofamily = AF_INET;
642 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
David Howellscbd071b2019-12-10 07:31:08 -0500643 case Opt_xprt_udp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500644 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500645 /* fall through */
646 case Opt_xprt_udp:
647 ctx->flags &= ~NFS_MOUNT_TCP;
648 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500649 break;
David Howellscbd071b2019-12-10 07:31:08 -0500650 case Opt_xprt_tcp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500651 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500652 /* fall through */
653 case Opt_xprt_tcp:
654 ctx->flags |= NFS_MOUNT_TCP;
655 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
656 break;
657 case Opt_xprt_rdma6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500658 protofamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500659 /* fall through */
660 case Opt_xprt_rdma:
661 /* vector side protocols to TCP */
662 ctx->flags |= NFS_MOUNT_TCP;
663 ctx->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500664 xprt_load_transport(param->string);
David Howellscbd071b2019-12-10 07:31:08 -0500665 break;
666 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500667 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
David Howellscbd071b2019-12-10 07:31:08 -0500668 }
David Howells9954bf92019-12-10 07:31:04 -0500669
Scott Mayhewe38bb232019-12-10 07:31:12 -0500670 ctx->protofamily = protofamily;
671 break;
672
673 case Opt_mountproto:
674 mountfamily = AF_INET;
675 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
David Howellscbd071b2019-12-10 07:31:08 -0500676 case Opt_xprt_udp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500677 mountfamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500678 /* fall through */
679 case Opt_xprt_udp:
680 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500681 break;
David Howellscbd071b2019-12-10 07:31:08 -0500682 case Opt_xprt_tcp6:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500683 mountfamily = AF_INET6;
David Howellscbd071b2019-12-10 07:31:08 -0500684 /* fall through */
685 case Opt_xprt_tcp:
686 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
David Howells9954bf92019-12-10 07:31:04 -0500687 break;
David Howellscbd071b2019-12-10 07:31:08 -0500688 case Opt_xprt_rdma: /* not used for side protocols */
689 default:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500690 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
David Howellscbd071b2019-12-10 07:31:08 -0500691 }
Scott Mayhewe38bb232019-12-10 07:31:12 -0500692 ctx->mountfamily = mountfamily;
David Howellscbd071b2019-12-10 07:31:08 -0500693 break;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500694
David Howellscbd071b2019-12-10 07:31:08 -0500695 case Opt_addr:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500696 len = rpc_pton(fc->net_ns, param->string, param->size,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500697 &ctx->nfs_server.address,
698 sizeof(ctx->nfs_server._address));
699 if (len == 0)
David Howellscbd071b2019-12-10 07:31:08 -0500700 goto out_invalid_address;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500701 ctx->nfs_server.addrlen = len;
David Howellscbd071b2019-12-10 07:31:08 -0500702 break;
703 case Opt_clientaddr:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500704 kfree(ctx->client_address);
705 ctx->client_address = param->string;
706 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500707 break;
708 case Opt_mounthost:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500709 kfree(ctx->mount_server.hostname);
710 ctx->mount_server.hostname = param->string;
711 param->string = NULL;
David Howellscbd071b2019-12-10 07:31:08 -0500712 break;
713 case Opt_mountaddr:
Scott Mayhew62a55d02019-12-10 07:31:14 -0500714 len = rpc_pton(fc->net_ns, param->string, param->size,
Scott Mayhewe38bb232019-12-10 07:31:12 -0500715 &ctx->mount_server.address,
716 sizeof(ctx->mount_server._address));
717 if (len == 0)
David Howellscbd071b2019-12-10 07:31:08 -0500718 goto out_invalid_address;
Scott Mayhewe38bb232019-12-10 07:31:12 -0500719 ctx->mount_server.addrlen = len;
David Howellscbd071b2019-12-10 07:31:08 -0500720 break;
721 case Opt_nconnect:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500722 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
723 goto out_of_bounds;
724 ctx->nfs_server.nconnect = result.uint_32;
David Howellscbd071b2019-12-10 07:31:08 -0500725 break;
726 case Opt_lookupcache:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500727 switch (result.uint_32) {
David Howellscbd071b2019-12-10 07:31:08 -0500728 case Opt_lookupcache_all:
729 ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
David Howells9954bf92019-12-10 07:31:04 -0500730 break;
David Howellscbd071b2019-12-10 07:31:08 -0500731 case Opt_lookupcache_positive:
732 ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
733 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
David Howells9954bf92019-12-10 07:31:04 -0500734 break;
David Howellscbd071b2019-12-10 07:31:08 -0500735 case Opt_lookupcache_none:
736 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
David Howells9954bf92019-12-10 07:31:04 -0500737 break;
David Howellscbd071b2019-12-10 07:31:08 -0500738 default:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500739 goto out_invalid_value;
David Howellscbd071b2019-12-10 07:31:08 -0500740 }
741 break;
David Howellscbd071b2019-12-10 07:31:08 -0500742 case Opt_local_lock:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500743 switch (result.uint_32) {
David Howellscbd071b2019-12-10 07:31:08 -0500744 case Opt_local_lock_all:
745 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
746 NFS_MOUNT_LOCAL_FCNTL);
David Howells9954bf92019-12-10 07:31:04 -0500747 break;
David Howellscbd071b2019-12-10 07:31:08 -0500748 case Opt_local_lock_flock:
749 ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
David Howells9954bf92019-12-10 07:31:04 -0500750 break;
David Howellscbd071b2019-12-10 07:31:08 -0500751 case Opt_local_lock_posix:
752 ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
David Howells9954bf92019-12-10 07:31:04 -0500753 break;
David Howellscbd071b2019-12-10 07:31:08 -0500754 case Opt_local_lock_none:
755 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
756 NFS_MOUNT_LOCAL_FCNTL);
David Howells9954bf92019-12-10 07:31:04 -0500757 break;
David Howellscbd071b2019-12-10 07:31:08 -0500758 default:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500759 goto out_invalid_value;
David Howellscbd071b2019-12-10 07:31:08 -0500760 }
761 break;
David Howells9954bf92019-12-10 07:31:04 -0500762
763 /*
764 * Special options
765 */
David Howellscbd071b2019-12-10 07:31:08 -0500766 case Opt_sloppy:
Scott Mayhewe38bb232019-12-10 07:31:12 -0500767 ctx->sloppy = true;
David Howellscbd071b2019-12-10 07:31:08 -0500768 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
769 break;
David Howells9954bf92019-12-10 07:31:04 -0500770 }
771
David Howellsf8ee01e2019-12-10 07:31:07 -0500772 return 0;
773
David Howellsf8ee01e2019-12-10 07:31:07 -0500774out_invalid_value:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500775 return nfs_invalf(fc, "NFS: Bad mount option value specified");
Scott Mayhewe38bb232019-12-10 07:31:12 -0500776out_invalid_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500777 return nfs_invalf(fc, "NFS: Bad IP address specified");
Scott Mayhewe38bb232019-12-10 07:31:12 -0500778out_of_bounds:
David Howells3a214092020-01-17 15:55:09 +0000779 return nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
Scott Mayhewe38bb232019-12-10 07:31:12 -0500780}
781
David Howells9954bf92019-12-10 07:31:04 -0500782/*
Scott Mayhew62a55d02019-12-10 07:31:14 -0500783 * Split fc->source into "hostname:export_path".
David Howells9954bf92019-12-10 07:31:04 -0500784 *
785 * The leftmost colon demarks the split between the server's hostname
786 * and the export path. If the hostname starts with a left square
787 * bracket, then it may contain colons.
788 *
789 * Note: caller frees hostname and export path, even on error.
790 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500791static int nfs_parse_source(struct fs_context *fc,
792 size_t maxnamlen, size_t maxpathlen)
David Howells9954bf92019-12-10 07:31:04 -0500793{
Scott Mayhew62a55d02019-12-10 07:31:14 -0500794 struct nfs_fs_context *ctx = nfs_fc2context(fc);
795 const char *dev_name = fc->source;
David Howells9954bf92019-12-10 07:31:04 -0500796 size_t len;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500797 const char *end;
David Howells9954bf92019-12-10 07:31:04 -0500798
799 if (unlikely(!dev_name || !*dev_name)) {
800 dfprintk(MOUNT, "NFS: device name not specified\n");
801 return -EINVAL;
802 }
803
804 /* Is the host name protected with square brakcets? */
805 if (*dev_name == '[') {
806 end = strchr(++dev_name, ']');
807 if (end == NULL || end[1] != ':')
808 goto out_bad_devname;
809
810 len = end - dev_name;
811 end++;
812 } else {
Scott Mayhew62a55d02019-12-10 07:31:14 -0500813 const char *comma;
David Howells9954bf92019-12-10 07:31:04 -0500814
815 end = strchr(dev_name, ':');
816 if (end == NULL)
817 goto out_bad_devname;
818 len = end - dev_name;
819
820 /* kill possible hostname list: not supported */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500821 comma = memchr(dev_name, ',', len);
822 if (comma)
David Howells9954bf92019-12-10 07:31:04 -0500823 len = comma - dev_name;
824 }
825
826 if (len > maxnamlen)
827 goto out_hostname;
828
829 /* N.B. caller will free nfs_server.hostname in all cases */
David Howellse5581002019-12-10 07:31:10 -0500830 ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
831 if (!ctx->nfs_server.hostname)
David Howells9954bf92019-12-10 07:31:04 -0500832 goto out_nomem;
833 len = strlen(++end);
834 if (len > maxpathlen)
835 goto out_path;
David Howellse5581002019-12-10 07:31:10 -0500836 ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
837 if (!ctx->nfs_server.export_path)
David Howells9954bf92019-12-10 07:31:04 -0500838 goto out_nomem;
839
David Howellse5581002019-12-10 07:31:10 -0500840 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", ctx->nfs_server.export_path);
David Howells9954bf92019-12-10 07:31:04 -0500841 return 0;
842
843out_bad_devname:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500844 return nfs_invalf(fc, "NFS: device name not in host:path format");
David Howells9954bf92019-12-10 07:31:04 -0500845out_nomem:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500846 nfs_errorf(fc, "NFS: not enough memory to parse device name");
David Howells9954bf92019-12-10 07:31:04 -0500847 return -ENOMEM;
David Howells9954bf92019-12-10 07:31:04 -0500848out_hostname:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500849 nfs_errorf(fc, "NFS: server hostname too long");
David Howells9954bf92019-12-10 07:31:04 -0500850 return -ENAMETOOLONG;
David Howells9954bf92019-12-10 07:31:04 -0500851out_path:
Scott Mayhewce8866f02019-12-10 07:31:15 -0500852 nfs_errorf(fc, "NFS: export pathname too long");
David Howells9954bf92019-12-10 07:31:04 -0500853 return -ENAMETOOLONG;
854}
855
David Howellsf2aedb72019-12-10 07:31:13 -0500856static inline bool is_remount_fc(struct fs_context *fc)
857{
858 return fc->root != NULL;
859}
860
David Howells9954bf92019-12-10 07:31:04 -0500861/*
David Howellse5581002019-12-10 07:31:10 -0500862 * Parse monolithic NFS2/NFS3 mount data
David Howells9954bf92019-12-10 07:31:04 -0500863 * - fills in the mount root filehandle
864 *
865 * For option strings, user space handles the following behaviors:
866 *
867 * + DNS: mapping server host name to IP address ("addr=" option)
868 *
869 * + failure mode: how to behave if a mount request can't be handled
870 * immediately ("fg/bg" option)
871 *
872 * + retry: how often to retry a mount request ("retry=" option)
873 *
874 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
875 * mountproto=tcp after mountproto=udp, and so on
876 */
David Howellsf2aedb72019-12-10 07:31:13 -0500877static int nfs23_parse_monolithic(struct fs_context *fc,
878 struct nfs_mount_data *data)
David Howells9954bf92019-12-10 07:31:04 -0500879{
David Howellsf2aedb72019-12-10 07:31:13 -0500880 struct nfs_fs_context *ctx = nfs_fc2context(fc);
Scott Mayhew62a55d02019-12-10 07:31:14 -0500881 struct nfs_fh *mntfh = ctx->mntfh;
David Howells5eb005c2019-12-10 07:31:06 -0500882 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -0500883 int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
884
885 if (data == NULL)
886 goto out_no_data;
887
David Howells5eb005c2019-12-10 07:31:06 -0500888 ctx->version = NFS_DEFAULT_VERSION;
David Howells9954bf92019-12-10 07:31:04 -0500889 switch (data->version) {
890 case 1:
891 data->namlen = 0; /* fall through */
892 case 2:
893 data->bsize = 0; /* fall through */
894 case 3:
895 if (data->flags & NFS_MOUNT_VER3)
896 goto out_no_v3;
897 data->root.size = NFS2_FHSIZE;
898 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
899 /* Turn off security negotiation */
900 extra_flags |= NFS_MOUNT_SECFLAVOUR;
901 /* fall through */
902 case 4:
903 if (data->flags & NFS_MOUNT_SECFLAVOUR)
904 goto out_no_sec;
905 /* fall through */
906 case 5:
907 memset(data->context, 0, sizeof(data->context));
908 /* fall through */
909 case 6:
910 if (data->flags & NFS_MOUNT_VER3) {
911 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
912 goto out_invalid_fh;
913 mntfh->size = data->root.size;
David Howells5eb005c2019-12-10 07:31:06 -0500914 ctx->version = 3;
David Howells9954bf92019-12-10 07:31:04 -0500915 } else {
916 mntfh->size = NFS2_FHSIZE;
David Howells5eb005c2019-12-10 07:31:06 -0500917 ctx->version = 2;
David Howells9954bf92019-12-10 07:31:04 -0500918 }
919
920
921 memcpy(mntfh->data, data->root.data, mntfh->size);
922 if (mntfh->size < sizeof(mntfh->data))
923 memset(mntfh->data + mntfh->size, 0,
924 sizeof(mntfh->data) - mntfh->size);
925
926 /*
David Howells5eb005c2019-12-10 07:31:06 -0500927 * Translate to nfs_fs_context, which nfs_fill_super
David Howells9954bf92019-12-10 07:31:04 -0500928 * can deal with.
929 */
David Howells5eb005c2019-12-10 07:31:06 -0500930 ctx->flags = data->flags & NFS_MOUNT_FLAGMASK;
931 ctx->flags |= extra_flags;
932 ctx->rsize = data->rsize;
933 ctx->wsize = data->wsize;
934 ctx->timeo = data->timeo;
935 ctx->retrans = data->retrans;
936 ctx->acregmin = data->acregmin;
937 ctx->acregmax = data->acregmax;
938 ctx->acdirmin = data->acdirmin;
939 ctx->acdirmax = data->acdirmax;
940 ctx->need_mount = false;
David Howells9954bf92019-12-10 07:31:04 -0500941
942 memcpy(sap, &data->addr, sizeof(data->addr));
David Howells5eb005c2019-12-10 07:31:06 -0500943 ctx->nfs_server.addrlen = sizeof(data->addr);
944 ctx->nfs_server.port = ntohs(data->addr.sin_port);
David Howells9954bf92019-12-10 07:31:04 -0500945 if (sap->sa_family != AF_INET ||
946 !nfs_verify_server_address(sap))
947 goto out_no_address;
948
949 if (!(data->flags & NFS_MOUNT_TCP))
David Howells5eb005c2019-12-10 07:31:06 -0500950 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
David Howells9954bf92019-12-10 07:31:04 -0500951 /* N.B. caller will free nfs_server.hostname in all cases */
David Howells5eb005c2019-12-10 07:31:06 -0500952 ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
David Howellsf2aedb72019-12-10 07:31:13 -0500953 if (!ctx->nfs_server.hostname)
954 goto out_nomem;
955
David Howells5eb005c2019-12-10 07:31:06 -0500956 ctx->namlen = data->namlen;
957 ctx->bsize = data->bsize;
David Howells9954bf92019-12-10 07:31:04 -0500958
959 if (data->flags & NFS_MOUNT_SECFLAVOUR)
David Howells5eb005c2019-12-10 07:31:06 -0500960 ctx->selected_flavor = data->pseudoflavor;
David Howells9954bf92019-12-10 07:31:04 -0500961 else
David Howells5eb005c2019-12-10 07:31:06 -0500962 ctx->selected_flavor = RPC_AUTH_UNIX;
David Howells9954bf92019-12-10 07:31:04 -0500963
964 if (!(data->flags & NFS_MOUNT_NONLM))
David Howells5eb005c2019-12-10 07:31:06 -0500965 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
David Howells9954bf92019-12-10 07:31:04 -0500966 NFS_MOUNT_LOCAL_FCNTL);
967 else
David Howells5eb005c2019-12-10 07:31:06 -0500968 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
David Howells9954bf92019-12-10 07:31:04 -0500969 NFS_MOUNT_LOCAL_FCNTL);
Scott Mayhew62a55d02019-12-10 07:31:14 -0500970
David Howells9954bf92019-12-10 07:31:04 -0500971 /*
972 * The legacy version 6 binary mount data from userspace has a
973 * field used only to transport selinux information into the
974 * the kernel. To continue to support that functionality we
975 * have a touch of selinux knowledge here in the NFS code. The
976 * userspace code converted context=blah to just blah so we are
977 * converting back to the full string selinux understands.
978 */
979 if (data->context[0]){
980#ifdef CONFIG_SECURITY_SELINUX
David Howellsf2aedb72019-12-10 07:31:13 -0500981 int ret;
982
David Howells9954bf92019-12-10 07:31:04 -0500983 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
David Howellsf2aedb72019-12-10 07:31:13 -0500984 ret = vfs_parse_fs_string(fc, "context",
985 data->context, strlen(data->context));
986 if (ret < 0)
987 return ret;
David Howells9954bf92019-12-10 07:31:04 -0500988#else
989 return -EINVAL;
990#endif
991 }
992
993 break;
994 default:
David Howellsf2aedb72019-12-10 07:31:13 -0500995 goto generic;
David Howells9954bf92019-12-10 07:31:04 -0500996 }
997
David Howellsf2aedb72019-12-10 07:31:13 -0500998 ctx->skip_reconfig_option_check = true;
David Howells9954bf92019-12-10 07:31:04 -0500999 return 0;
1000
David Howellsf2aedb72019-12-10 07:31:13 -05001001generic:
1002 return generic_parse_monolithic(fc, data);
1003
David Howells9954bf92019-12-10 07:31:04 -05001004out_no_data:
David Howellsf2aedb72019-12-10 07:31:13 -05001005 if (is_remount_fc(fc)) {
1006 ctx->skip_reconfig_option_check = true;
1007 return 0;
1008 }
Scott Mayhewce8866f02019-12-10 07:31:15 -05001009 return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
David Howells9954bf92019-12-10 07:31:04 -05001010
1011out_no_v3:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001012 return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
David Howells9954bf92019-12-10 07:31:04 -05001013
1014out_no_sec:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001015 return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
David Howells9954bf92019-12-10 07:31:04 -05001016
1017out_nomem:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001018 dfprintk(MOUNT, "NFS: not enough memory to handle mount options");
David Howells9954bf92019-12-10 07:31:04 -05001019 return -ENOMEM;
1020
1021out_no_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001022 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
David Howells9954bf92019-12-10 07:31:04 -05001023
1024out_invalid_fh:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001025 return nfs_invalf(fc, "NFS: invalid root filehandle");
David Howells9954bf92019-12-10 07:31:04 -05001026}
1027
1028#if IS_ENABLED(CONFIG_NFS_V4)
David Howells9954bf92019-12-10 07:31:04 -05001029/*
1030 * Validate NFSv4 mount options
1031 */
David Howellsf2aedb72019-12-10 07:31:13 -05001032static int nfs4_parse_monolithic(struct fs_context *fc,
1033 struct nfs4_mount_data *data)
David Howells9954bf92019-12-10 07:31:04 -05001034{
David Howellsf2aedb72019-12-10 07:31:13 -05001035 struct nfs_fs_context *ctx = nfs_fc2context(fc);
David Howells5eb005c2019-12-10 07:31:06 -05001036 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001037 char *c;
1038
1039 if (data == NULL)
1040 goto out_no_data;
1041
David Howells5eb005c2019-12-10 07:31:06 -05001042 ctx->version = 4;
David Howells9954bf92019-12-10 07:31:04 -05001043
1044 switch (data->version) {
1045 case 1:
David Howells5eb005c2019-12-10 07:31:06 -05001046 if (data->host_addrlen > sizeof(ctx->nfs_server.address))
David Howells9954bf92019-12-10 07:31:04 -05001047 goto out_no_address;
1048 if (data->host_addrlen == 0)
1049 goto out_no_address;
David Howells5eb005c2019-12-10 07:31:06 -05001050 ctx->nfs_server.addrlen = data->host_addrlen;
David Howells9954bf92019-12-10 07:31:04 -05001051 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1052 return -EFAULT;
1053 if (!nfs_verify_server_address(sap))
1054 goto out_no_address;
David Howells5eb005c2019-12-10 07:31:06 -05001055 ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
David Howells9954bf92019-12-10 07:31:04 -05001056
1057 if (data->auth_flavourlen) {
1058 rpc_authflavor_t pseudoflavor;
1059 if (data->auth_flavourlen > 1)
1060 goto out_inval_auth;
1061 if (copy_from_user(&pseudoflavor,
1062 data->auth_flavours,
1063 sizeof(pseudoflavor)))
1064 return -EFAULT;
David Howells5eb005c2019-12-10 07:31:06 -05001065 ctx->selected_flavor = pseudoflavor;
David Howells9954bf92019-12-10 07:31:04 -05001066 } else
David Howells5eb005c2019-12-10 07:31:06 -05001067 ctx->selected_flavor = RPC_AUTH_UNIX;
David Howells9954bf92019-12-10 07:31:04 -05001068
1069 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1070 if (IS_ERR(c))
1071 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001072 ctx->nfs_server.hostname = c;
David Howells9954bf92019-12-10 07:31:04 -05001073
1074 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1075 if (IS_ERR(c))
1076 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001077 ctx->nfs_server.export_path = c;
David Howells9954bf92019-12-10 07:31:04 -05001078 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1079
1080 c = strndup_user(data->client_addr.data, 16);
1081 if (IS_ERR(c))
1082 return PTR_ERR(c);
David Howells5eb005c2019-12-10 07:31:06 -05001083 ctx->client_address = c;
David Howells9954bf92019-12-10 07:31:04 -05001084
1085 /*
David Howellsf2aedb72019-12-10 07:31:13 -05001086 * Translate to nfs_fs_context, which nfs_fill_super
David Howells9954bf92019-12-10 07:31:04 -05001087 * can deal with.
1088 */
1089
David Howells5eb005c2019-12-10 07:31:06 -05001090 ctx->flags = data->flags & NFS4_MOUNT_FLAGMASK;
1091 ctx->rsize = data->rsize;
1092 ctx->wsize = data->wsize;
1093 ctx->timeo = data->timeo;
1094 ctx->retrans = data->retrans;
1095 ctx->acregmin = data->acregmin;
1096 ctx->acregmax = data->acregmax;
1097 ctx->acdirmin = data->acdirmin;
1098 ctx->acdirmax = data->acdirmax;
1099 ctx->nfs_server.protocol = data->proto;
1100 nfs_validate_transport_protocol(ctx);
1101 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
David Howells9954bf92019-12-10 07:31:04 -05001102 goto out_invalid_transport_udp;
1103
1104 break;
1105 default:
David Howellsf2aedb72019-12-10 07:31:13 -05001106 goto generic;
David Howells9954bf92019-12-10 07:31:04 -05001107 }
1108
David Howellsf2aedb72019-12-10 07:31:13 -05001109 ctx->skip_reconfig_option_check = true;
David Howells9954bf92019-12-10 07:31:04 -05001110 return 0;
1111
David Howellsf2aedb72019-12-10 07:31:13 -05001112generic:
1113 return generic_parse_monolithic(fc, data);
1114
David Howells9954bf92019-12-10 07:31:04 -05001115out_no_data:
David Howellsf2aedb72019-12-10 07:31:13 -05001116 if (is_remount_fc(fc)) {
1117 ctx->skip_reconfig_option_check = true;
1118 return 0;
1119 }
Scott Mayhewce8866f02019-12-10 07:31:15 -05001120 return nfs_invalf(fc, "NFS4: mount program didn't pass any mount data");
David Howells9954bf92019-12-10 07:31:04 -05001121
1122out_inval_auth:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001123 return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1124 data->auth_flavourlen);
David Howells9954bf92019-12-10 07:31:04 -05001125
1126out_no_address:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001127 return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
David Howells9954bf92019-12-10 07:31:04 -05001128
1129out_invalid_transport_udp:
Scott Mayhewce8866f02019-12-10 07:31:15 -05001130 return nfs_invalf(fc, "NFSv4: Unsupported transport protocol udp");
David Howells9954bf92019-12-10 07:31:04 -05001131}
David Howells9954bf92019-12-10 07:31:04 -05001132#endif
1133
David Howellsf2aedb72019-12-10 07:31:13 -05001134/*
1135 * Parse a monolithic block of data from sys_mount().
1136 */
1137static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1138 void *data)
David Howells9954bf92019-12-10 07:31:04 -05001139{
David Howellsf2aedb72019-12-10 07:31:13 -05001140 if (fc->fs_type == &nfs_fs_type)
1141 return nfs23_parse_monolithic(fc, data);
1142
1143#if IS_ENABLED(CONFIG_NFS_V4)
1144 if (fc->fs_type == &nfs4_fs_type)
1145 return nfs4_parse_monolithic(fc, data);
1146#endif
1147
Scott Mayhewce8866f02019-12-10 07:31:15 -05001148 return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
David Howellsf2aedb72019-12-10 07:31:13 -05001149}
1150
1151/*
1152 * Validate the preparsed information in the config.
1153 */
1154static int nfs_fs_context_validate(struct fs_context *fc)
1155{
1156 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1157 struct nfs_subversion *nfs_mod;
1158 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
David Howells9954bf92019-12-10 07:31:04 -05001159 int max_namelen = PAGE_SIZE;
1160 int max_pathlen = NFS_MAXPATHLEN;
David Howellsf2aedb72019-12-10 07:31:13 -05001161 int port = 0;
1162 int ret;
David Howells9954bf92019-12-10 07:31:04 -05001163
David Howellsf2aedb72019-12-10 07:31:13 -05001164 if (!fc->source)
1165 goto out_no_device_name;
1166
1167 /* Check for sanity first. */
1168 if (ctx->minorversion && ctx->version != 4)
1169 goto out_minorversion_mismatch;
1170
1171 if (ctx->options & NFS_OPTION_MIGRATION &&
1172 (ctx->version != 4 || ctx->minorversion != 0))
1173 goto out_migration_misuse;
1174
1175 /* Verify that any proto=/mountproto= options match the address
1176 * families in the addr=/mountaddr= options.
1177 */
1178 if (ctx->protofamily != AF_UNSPEC &&
1179 ctx->protofamily != ctx->nfs_server.address.sa_family)
1180 goto out_proto_mismatch;
1181
1182 if (ctx->mountfamily != AF_UNSPEC) {
1183 if (ctx->mount_server.addrlen) {
1184 if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1185 goto out_mountproto_mismatch;
1186 } else {
1187 if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1188 goto out_mountproto_mismatch;
1189 }
1190 }
David Howells9954bf92019-12-10 07:31:04 -05001191
1192 if (!nfs_verify_server_address(sap))
1193 goto out_no_address;
1194
David Howells5eb005c2019-12-10 07:31:06 -05001195 if (ctx->version == 4) {
Scott Mayhew62a55d02019-12-10 07:31:14 -05001196 if (IS_ENABLED(CONFIG_NFS_V4)) {
1197 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1198 port = NFS_RDMA_PORT;
1199 else
1200 port = NFS_PORT;
1201 max_namelen = NFS4_MAXNAMLEN;
1202 max_pathlen = NFS4_MAXPATHLEN;
1203 nfs_validate_transport_protocol(ctx);
1204 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1205 goto out_invalid_transport_udp;
1206 ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1207 NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1208 NFS_MOUNT_LOCAL_FCNTL);
1209 } else {
1210 goto out_v4_not_compiled;
1211 }
David Howells9954bf92019-12-10 07:31:04 -05001212 } else {
David Howells5eb005c2019-12-10 07:31:06 -05001213 nfs_set_mount_transport_protocol(ctx);
Olga Kornievskaiab24ee6c2019-12-16 16:34:02 -05001214#ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
1215 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_UDP)
1216 goto out_invalid_transport_udp;
1217#endif
David Howells5eb005c2019-12-10 07:31:06 -05001218 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,
Al Virod7167b12019-09-07 07:23:15 -04001415 .parameters = nfs_fs_parameters,
David Howellsf2aedb72019-12-10 07:31:13 -05001416 .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,
Al Virod7167b12019-09-07 07:23:15 -04001427 .parameters = nfs_fs_parameters,
David Howellsf2aedb72019-12-10 07:31:13 -05001428 .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 */