]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/nfs/fs_context.c
NFSv4: Handle case where the lookup of a directory fails
[mirror_ubuntu-jammy-kernel.git] / fs / nfs / fs_context.c
CommitLineData
9954bf92
DH
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/fs/nfs/fs_context.c
4 *
5 * Copyright (C) 1992 Rick Sladkey
f2aedb71 6 * Conversion to new mount api Copyright (C) David Howells
9954bf92
DH
7 *
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>
e38bb238
SM
15#include <linux/fs_context.h>
16#include <linux/fs_parser.h>
9954bf92
DH
17#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
e38bb238
SM
33enum nfs_param {
34 Opt_ac,
35 Opt_acdirmax,
36 Opt_acdirmin,
37 Opt_acl,
38 Opt_acregmax,
39 Opt_acregmin,
9954bf92 40 Opt_actimeo,
e38bb238
SM
41 Opt_addr,
42 Opt_bg,
43 Opt_bsize,
44 Opt_clientaddr,
45 Opt_cto,
46 Opt_fg,
47 Opt_fscache,
48ce73b1 48 Opt_fscache_flag,
e38bb238
SM
49 Opt_hard,
50 Opt_intr,
51 Opt_local_lock,
52 Opt_lock,
53 Opt_lookupcache,
54 Opt_migration,
55 Opt_minorversion,
56 Opt_mountaddr,
57 Opt_mounthost,
9954bf92 58 Opt_mountport,
e38bb238 59 Opt_mountproto,
9954bf92 60 Opt_mountvers,
e38bb238 61 Opt_namelen,
9954bf92 62 Opt_nconnect,
7e134205 63 Opt_max_connect,
e38bb238
SM
64 Opt_port,
65 Opt_posix,
66 Opt_proto,
67 Opt_rdirplus,
68 Opt_rdma,
69 Opt_resvport,
70 Opt_retrans,
71 Opt_retry,
72 Opt_rsize,
73 Opt_sec,
74 Opt_sharecache,
75 Opt_sloppy,
76 Opt_soft,
77 Opt_softerr,
c74dfe97 78 Opt_softreval,
e38bb238
SM
79 Opt_source,
80 Opt_tcp,
81 Opt_timeo,
82 Opt_udp,
83 Opt_v,
84 Opt_vers,
85 Opt_wsize,
a0492339 86 Opt_write,
9954bf92
DH
87};
88
2710c957
AV
89enum {
90 Opt_local_lock_all,
91 Opt_local_lock_flock,
92 Opt_local_lock_none,
93 Opt_local_lock_posix,
94};
95
5eede625 96static const struct constant_table nfs_param_enums_local_lock[] = {
2710c957
AV
97 { "all", Opt_local_lock_all },
98 { "flock", Opt_local_lock_flock },
a2d24bcb 99 { "posix", Opt_local_lock_posix },
2710c957
AV
100 { "none", Opt_local_lock_none },
101 {}
102};
103
104enum {
105 Opt_lookupcache_all,
106 Opt_lookupcache_none,
107 Opt_lookupcache_positive,
108};
109
5eede625 110static const struct constant_table nfs_param_enums_lookupcache[] = {
2710c957
AV
111 { "all", Opt_lookupcache_all },
112 { "none", Opt_lookupcache_none },
113 { "pos", Opt_lookupcache_positive },
114 { "positive", Opt_lookupcache_positive },
115 {}
116};
117
a0492339
TM
118enum {
119 Opt_write_lazy,
120 Opt_write_eager,
121 Opt_write_wait,
122};
123
124static const struct constant_table nfs_param_enums_write[] = {
125 { "lazy", Opt_write_lazy },
126 { "eager", Opt_write_eager },
127 { "wait", Opt_write_wait },
128 {}
129};
130
d7167b14 131static const struct fs_parameter_spec nfs_fs_parameters[] = {
e38bb238
SM
132 fsparam_flag_no("ac", Opt_ac),
133 fsparam_u32 ("acdirmax", Opt_acdirmax),
134 fsparam_u32 ("acdirmin", Opt_acdirmin),
135 fsparam_flag_no("acl", Opt_acl),
136 fsparam_u32 ("acregmax", Opt_acregmax),
137 fsparam_u32 ("acregmin", Opt_acregmin),
138 fsparam_u32 ("actimeo", Opt_actimeo),
139 fsparam_string("addr", Opt_addr),
140 fsparam_flag ("bg", Opt_bg),
141 fsparam_u32 ("bsize", Opt_bsize),
142 fsparam_string("clientaddr", Opt_clientaddr),
143 fsparam_flag_no("cto", Opt_cto),
144 fsparam_flag ("fg", Opt_fg),
48ce73b1
AV
145 fsparam_flag_no("fsc", Opt_fscache_flag),
146 fsparam_string("fsc", Opt_fscache),
e38bb238 147 fsparam_flag ("hard", Opt_hard),
328de528 148 __fsparam(NULL, "intr", Opt_intr,
2710c957
AV
149 fs_param_neg_with_no|fs_param_deprecated, NULL),
150 fsparam_enum ("local_lock", Opt_local_lock, nfs_param_enums_local_lock),
e38bb238 151 fsparam_flag_no("lock", Opt_lock),
2710c957 152 fsparam_enum ("lookupcache", Opt_lookupcache, nfs_param_enums_lookupcache),
e38bb238
SM
153 fsparam_flag_no("migration", Opt_migration),
154 fsparam_u32 ("minorversion", Opt_minorversion),
155 fsparam_string("mountaddr", Opt_mountaddr),
156 fsparam_string("mounthost", Opt_mounthost),
157 fsparam_u32 ("mountport", Opt_mountport),
158 fsparam_string("mountproto", Opt_mountproto),
159 fsparam_u32 ("mountvers", Opt_mountvers),
160 fsparam_u32 ("namlen", Opt_namelen),
161 fsparam_u32 ("nconnect", Opt_nconnect),
7e134205 162 fsparam_u32 ("max_connect", Opt_max_connect),
e38bb238
SM
163 fsparam_string("nfsvers", Opt_vers),
164 fsparam_u32 ("port", Opt_port),
165 fsparam_flag_no("posix", Opt_posix),
166 fsparam_string("proto", Opt_proto),
167 fsparam_flag_no("rdirplus", Opt_rdirplus),
168 fsparam_flag ("rdma", Opt_rdma),
169 fsparam_flag_no("resvport", Opt_resvport),
170 fsparam_u32 ("retrans", Opt_retrans),
171 fsparam_string("retry", Opt_retry),
172 fsparam_u32 ("rsize", Opt_rsize),
173 fsparam_string("sec", Opt_sec),
174 fsparam_flag_no("sharecache", Opt_sharecache),
175 fsparam_flag ("sloppy", Opt_sloppy),
176 fsparam_flag ("soft", Opt_soft),
177 fsparam_flag ("softerr", Opt_softerr),
c74dfe97 178 fsparam_flag ("softreval", Opt_softreval),
e38bb238
SM
179 fsparam_string("source", Opt_source),
180 fsparam_flag ("tcp", Opt_tcp),
181 fsparam_u32 ("timeo", Opt_timeo),
182 fsparam_flag ("udp", Opt_udp),
183 fsparam_flag ("v2", Opt_v),
184 fsparam_flag ("v3", Opt_v),
185 fsparam_flag ("v4", Opt_v),
186 fsparam_flag ("v4.0", Opt_v),
187 fsparam_flag ("v4.1", Opt_v),
188 fsparam_flag ("v4.2", Opt_v),
189 fsparam_string("vers", Opt_vers),
a0492339 190 fsparam_enum ("write", Opt_write, nfs_param_enums_write),
e38bb238
SM
191 fsparam_u32 ("wsize", Opt_wsize),
192 {}
9954bf92
DH
193};
194
9954bf92 195enum {
e38bb238
SM
196 Opt_vers_2,
197 Opt_vers_3,
198 Opt_vers_4,
199 Opt_vers_4_0,
200 Opt_vers_4_1,
201 Opt_vers_4_2,
9954bf92
DH
202};
203
e38bb238
SM
204static const struct constant_table nfs_vers_tokens[] = {
205 { "2", Opt_vers_2 },
206 { "3", Opt_vers_3 },
207 { "4", Opt_vers_4 },
208 { "4.0", Opt_vers_4_0 },
209 { "4.1", Opt_vers_4_1 },
210 { "4.2", Opt_vers_4_2 },
529af905 211 {}
9954bf92
DH
212};
213
214enum {
e38bb238
SM
215 Opt_xprt_rdma,
216 Opt_xprt_rdma6,
217 Opt_xprt_tcp,
218 Opt_xprt_tcp6,
219 Opt_xprt_udp,
220 Opt_xprt_udp6,
221 nr__Opt_xprt
9954bf92
DH
222};
223
529af905 224static const struct constant_table nfs_xprt_protocol_tokens[] = {
e38bb238
SM
225 { "rdma", Opt_xprt_rdma },
226 { "rdma6", Opt_xprt_rdma6 },
227 { "tcp", Opt_xprt_tcp },
228 { "tcp6", Opt_xprt_tcp6 },
229 { "udp", Opt_xprt_udp },
230 { "udp6", Opt_xprt_udp6 },
529af905 231 {}
9954bf92
DH
232};
233
234enum {
e38bb238
SM
235 Opt_sec_krb5,
236 Opt_sec_krb5i,
237 Opt_sec_krb5p,
238 Opt_sec_lkey,
239 Opt_sec_lkeyi,
240 Opt_sec_lkeyp,
241 Opt_sec_none,
242 Opt_sec_spkm,
243 Opt_sec_spkmi,
244 Opt_sec_spkmp,
245 Opt_sec_sys,
246 nr__Opt_sec
9954bf92
DH
247};
248
e38bb238
SM
249static const struct constant_table nfs_secflavor_tokens[] = {
250 { "krb5", Opt_sec_krb5 },
251 { "krb5i", Opt_sec_krb5i },
252 { "krb5p", Opt_sec_krb5p },
253 { "lkey", Opt_sec_lkey },
254 { "lkeyi", Opt_sec_lkeyi },
255 { "lkeyp", Opt_sec_lkeyp },
256 { "none", Opt_sec_none },
257 { "null", Opt_sec_none },
258 { "spkm3", Opt_sec_spkm },
259 { "spkm3i", Opt_sec_spkmi },
260 { "spkm3p", Opt_sec_spkmp },
261 { "sys", Opt_sec_sys },
529af905 262 {}
9954bf92
DH
263};
264
9954bf92
DH
265/*
266 * Sanity-check a server address provided by the mount command.
267 *
268 * Address family must be initialized, and address must not be
269 * the ANY address for that family.
270 */
271static int nfs_verify_server_address(struct sockaddr *addr)
272{
273 switch (addr->sa_family) {
274 case AF_INET: {
275 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
276 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
277 }
278 case AF_INET6: {
279 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
280 return !ipv6_addr_any(sa);
281 }
282 }
283
284 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
285 return 0;
286}
287
90ff57bf
TM
288#ifdef CONFIG_NFS_DISABLE_UDP_SUPPORT
289static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
290{
291 return true;
292}
293#else
294static bool nfs_server_transport_udp_invalid(const struct nfs_fs_context *ctx)
295{
296 if (ctx->version == 4)
297 return true;
298 return false;
299}
300#endif
301
9954bf92
DH
302/*
303 * Sanity check the NFS transport protocol.
9954bf92 304 */
90ff57bf
TM
305static int nfs_validate_transport_protocol(struct fs_context *fc,
306 struct nfs_fs_context *ctx)
9954bf92 307{
5eb005ca 308 switch (ctx->nfs_server.protocol) {
9954bf92 309 case XPRT_TRANSPORT_UDP:
90ff57bf
TM
310 if (nfs_server_transport_udp_invalid(ctx))
311 goto out_invalid_transport_udp;
312 break;
9954bf92
DH
313 case XPRT_TRANSPORT_TCP:
314 case XPRT_TRANSPORT_RDMA:
315 break;
316 default:
5eb005ca 317 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
9954bf92 318 }
90ff57bf
TM
319 return 0;
320out_invalid_transport_udp:
321 return nfs_invalf(fc, "NFS: Unsupported transport protocol udp");
9954bf92
DH
322}
323
324/*
325 * For text based NFSv2/v3 mounts, the mount protocol transport default
326 * settings should depend upon the specified NFS transport.
327 */
5eb005ca 328static void nfs_set_mount_transport_protocol(struct nfs_fs_context *ctx)
9954bf92 329{
5eb005ca
DH
330 if (ctx->mount_server.protocol == XPRT_TRANSPORT_UDP ||
331 ctx->mount_server.protocol == XPRT_TRANSPORT_TCP)
9954bf92 332 return;
5eb005ca 333 switch (ctx->nfs_server.protocol) {
9954bf92 334 case XPRT_TRANSPORT_UDP:
5eb005ca 335 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
9954bf92
DH
336 break;
337 case XPRT_TRANSPORT_TCP:
338 case XPRT_TRANSPORT_RDMA:
5eb005ca 339 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
9954bf92
DH
340 }
341}
342
343/*
344 * Add 'flavor' to 'auth_info' if not already present.
345 * Returns true if 'flavor' ends up in the list, false otherwise
346 */
62a55d08 347static int nfs_auth_info_add(struct fs_context *fc,
e558100f
DH
348 struct nfs_auth_info *auth_info,
349 rpc_authflavor_t flavor)
9954bf92
DH
350{
351 unsigned int i;
352 unsigned int max_flavor_len = ARRAY_SIZE(auth_info->flavors);
353
354 /* make sure this flavor isn't already in the list */
355 for (i = 0; i < auth_info->flavor_len; i++) {
356 if (flavor == auth_info->flavors[i])
e558100f 357 return 0;
9954bf92
DH
358 }
359
ce8866f0
SM
360 if (auth_info->flavor_len + 1 >= max_flavor_len)
361 return nfs_invalf(fc, "NFS: too many sec= flavors");
9954bf92
DH
362
363 auth_info->flavors[auth_info->flavor_len++] = flavor;
e558100f 364 return 0;
9954bf92
DH
365}
366
367/*
368 * Parse the value of the 'sec=' option.
369 */
62a55d08 370static int nfs_parse_security_flavors(struct fs_context *fc,
e38bb238 371 struct fs_parameter *param)
9954bf92 372{
62a55d08 373 struct nfs_fs_context *ctx = nfs_fc2context(fc);
9954bf92 374 rpc_authflavor_t pseudoflavor;
e38bb238 375 char *string = param->string, *p;
e558100f 376 int ret;
9954bf92 377
e38bb238 378 dfprintk(MOUNT, "NFS: parsing %s=%s option\n", param->key, param->string);
9954bf92 379
e38bb238
SM
380 while ((p = strsep(&string, ":")) != NULL) {
381 if (!*p)
382 continue;
383 switch (lookup_constant(nfs_secflavor_tokens, p, -1)) {
9954bf92
DH
384 case Opt_sec_none:
385 pseudoflavor = RPC_AUTH_NULL;
386 break;
387 case Opt_sec_sys:
388 pseudoflavor = RPC_AUTH_UNIX;
389 break;
390 case Opt_sec_krb5:
391 pseudoflavor = RPC_AUTH_GSS_KRB5;
392 break;
393 case Opt_sec_krb5i:
394 pseudoflavor = RPC_AUTH_GSS_KRB5I;
395 break;
396 case Opt_sec_krb5p:
397 pseudoflavor = RPC_AUTH_GSS_KRB5P;
398 break;
399 case Opt_sec_lkey:
400 pseudoflavor = RPC_AUTH_GSS_LKEY;
401 break;
402 case Opt_sec_lkeyi:
403 pseudoflavor = RPC_AUTH_GSS_LKEYI;
404 break;
405 case Opt_sec_lkeyp:
406 pseudoflavor = RPC_AUTH_GSS_LKEYP;
407 break;
408 case Opt_sec_spkm:
409 pseudoflavor = RPC_AUTH_GSS_SPKM;
410 break;
411 case Opt_sec_spkmi:
412 pseudoflavor = RPC_AUTH_GSS_SPKMI;
413 break;
414 case Opt_sec_spkmp:
415 pseudoflavor = RPC_AUTH_GSS_SPKMP;
416 break;
417 default:
ce8866f0 418 return nfs_invalf(fc, "NFS: sec=%s option not recognized", p);
9954bf92
DH
419 }
420
62a55d08 421 ret = nfs_auth_info_add(fc, &ctx->auth_info, pseudoflavor);
e558100f
DH
422 if (ret < 0)
423 return ret;
9954bf92
DH
424 }
425
e558100f 426 return 0;
9954bf92
DH
427}
428
62a55d08 429static int nfs_parse_version_string(struct fs_context *fc,
e38bb238 430 const char *string)
9954bf92 431{
62a55d08
SM
432 struct nfs_fs_context *ctx = nfs_fc2context(fc);
433
5eb005ca 434 ctx->flags &= ~NFS_MOUNT_VER3;
e38bb238 435 switch (lookup_constant(nfs_vers_tokens, string, -1)) {
9954bf92 436 case Opt_vers_2:
5eb005ca 437 ctx->version = 2;
9954bf92
DH
438 break;
439 case Opt_vers_3:
5eb005ca
DH
440 ctx->flags |= NFS_MOUNT_VER3;
441 ctx->version = 3;
9954bf92
DH
442 break;
443 case Opt_vers_4:
444 /* Backward compatibility option. In future,
445 * the mount program should always supply
446 * a NFSv4 minor version number.
447 */
5eb005ca 448 ctx->version = 4;
9954bf92
DH
449 break;
450 case Opt_vers_4_0:
5eb005ca
DH
451 ctx->version = 4;
452 ctx->minorversion = 0;
9954bf92
DH
453 break;
454 case Opt_vers_4_1:
5eb005ca
DH
455 ctx->version = 4;
456 ctx->minorversion = 1;
9954bf92
DH
457 break;
458 case Opt_vers_4_2:
5eb005ca
DH
459 ctx->version = 4;
460 ctx->minorversion = 2;
9954bf92
DH
461 break;
462 default:
ce8866f0 463 return nfs_invalf(fc, "NFS: Unsupported NFS version");
9954bf92 464 }
e558100f 465 return 0;
9954bf92
DH
466}
467
9954bf92 468/*
e38bb238 469 * Parse a single mount parameter.
9954bf92 470 */
f2aedb71 471static int nfs_fs_context_parse_param(struct fs_context *fc,
e38bb238 472 struct fs_parameter *param)
9954bf92 473{
e38bb238 474 struct fs_parse_result result;
f2aedb71 475 struct nfs_fs_context *ctx = nfs_fc2context(fc);
e38bb238
SM
476 unsigned short protofamily, mountfamily;
477 unsigned int len;
478 int ret, opt;
9954bf92 479
e38bb238 480 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", param->key);
9954bf92 481
d7167b14 482 opt = fs_parse(fc, nfs_fs_parameters, param, &result);
e38bb238
SM
483 if (opt < 0)
484 return ctx->sloppy ? 1 : opt;
485
ec1ade6a
OK
486 if (fc->security)
487 ctx->has_sec_mnt_opts = 1;
488
e38bb238 489 switch (opt) {
f2aedb71 490 case Opt_source:
ce8866f0
SM
491 if (fc->source)
492 return nfs_invalf(fc, "NFS: Multiple sources not supported");
f2aedb71
DH
493 fc->source = param->string;
494 param->string = NULL;
495 break;
496
9954bf92
DH
497 /*
498 * boolean options: foo/nofoo
499 */
cbd071b5
DH
500 case Opt_soft:
501 ctx->flags |= NFS_MOUNT_SOFT;
502 ctx->flags &= ~NFS_MOUNT_SOFTERR;
503 break;
504 case Opt_softerr:
c74dfe97 505 ctx->flags |= NFS_MOUNT_SOFTERR | NFS_MOUNT_SOFTREVAL;
cbd071b5
DH
506 ctx->flags &= ~NFS_MOUNT_SOFT;
507 break;
508 case Opt_hard:
c74dfe97
TM
509 ctx->flags &= ~(NFS_MOUNT_SOFT |
510 NFS_MOUNT_SOFTERR |
511 NFS_MOUNT_SOFTREVAL);
512 break;
513 case Opt_softreval:
514 if (result.negated)
515 ctx->flags &= ~NFS_MOUNT_SOFTREVAL;
516 else
517 ctx->flags &= NFS_MOUNT_SOFTREVAL;
cbd071b5
DH
518 break;
519 case Opt_posix:
e38bb238
SM
520 if (result.negated)
521 ctx->flags &= ~NFS_MOUNT_POSIX;
522 else
523 ctx->flags |= NFS_MOUNT_POSIX;
cbd071b5
DH
524 break;
525 case Opt_cto:
e38bb238
SM
526 if (result.negated)
527 ctx->flags |= NFS_MOUNT_NOCTO;
528 else
529 ctx->flags &= ~NFS_MOUNT_NOCTO;
cbd071b5
DH
530 break;
531 case Opt_ac:
e38bb238
SM
532 if (result.negated)
533 ctx->flags |= NFS_MOUNT_NOAC;
534 else
535 ctx->flags &= ~NFS_MOUNT_NOAC;
cbd071b5
DH
536 break;
537 case Opt_lock:
e38bb238
SM
538 if (result.negated) {
539 ctx->flags |= NFS_MOUNT_NONLM;
540 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
541 } else {
542 ctx->flags &= ~NFS_MOUNT_NONLM;
543 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK | NFS_MOUNT_LOCAL_FCNTL);
544 }
cbd071b5
DH
545 break;
546 case Opt_udp:
547 ctx->flags &= ~NFS_MOUNT_TCP;
548 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
549 break;
550 case Opt_tcp:
cbd071b5
DH
551 case Opt_rdma:
552 ctx->flags |= NFS_MOUNT_TCP; /* for side protocols */
1c3695d0
TM
553 ret = xprt_find_transport_ident(param->key);
554 if (ret < 0)
555 goto out_bad_transport;
556 ctx->nfs_server.protocol = ret;
cbd071b5
DH
557 break;
558 case Opt_acl:
e38bb238
SM
559 if (result.negated)
560 ctx->flags |= NFS_MOUNT_NOACL;
561 else
562 ctx->flags &= ~NFS_MOUNT_NOACL;
cbd071b5
DH
563 break;
564 case Opt_rdirplus:
e38bb238
SM
565 if (result.negated)
566 ctx->flags |= NFS_MOUNT_NORDIRPLUS;
567 else
568 ctx->flags &= ~NFS_MOUNT_NORDIRPLUS;
cbd071b5
DH
569 break;
570 case Opt_sharecache:
e38bb238
SM
571 if (result.negated)
572 ctx->flags |= NFS_MOUNT_UNSHARED;
573 else
574 ctx->flags &= ~NFS_MOUNT_UNSHARED;
cbd071b5
DH
575 break;
576 case Opt_resvport:
e38bb238
SM
577 if (result.negated)
578 ctx->flags |= NFS_MOUNT_NORESVPORT;
579 else
580 ctx->flags &= ~NFS_MOUNT_NORESVPORT;
cbd071b5 581 break;
48ce73b1 582 case Opt_fscache_flag:
e38bb238
SM
583 if (result.negated)
584 ctx->options &= ~NFS_OPTION_FSCACHE;
585 else
586 ctx->options |= NFS_OPTION_FSCACHE;
48ce73b1
AV
587 kfree(ctx->fscache_uniq);
588 ctx->fscache_uniq = NULL;
589 break;
590 case Opt_fscache:
591 ctx->options |= NFS_OPTION_FSCACHE;
592 kfree(ctx->fscache_uniq);
593 ctx->fscache_uniq = param->string;
594 param->string = NULL;
cbd071b5
DH
595 break;
596 case Opt_migration:
e38bb238
SM
597 if (result.negated)
598 ctx->options &= ~NFS_OPTION_MIGRATION;
599 else
600 ctx->options |= NFS_OPTION_MIGRATION;
cbd071b5 601 break;
9954bf92
DH
602
603 /*
604 * options that take numeric values
605 */
cbd071b5 606 case Opt_port:
e38bb238
SM
607 if (result.uint_32 > USHRT_MAX)
608 goto out_of_bounds;
609 ctx->nfs_server.port = result.uint_32;
cbd071b5
DH
610 break;
611 case Opt_rsize:
e38bb238 612 ctx->rsize = result.uint_32;
cbd071b5
DH
613 break;
614 case Opt_wsize:
e38bb238 615 ctx->wsize = result.uint_32;
cbd071b5
DH
616 break;
617 case Opt_bsize:
e38bb238 618 ctx->bsize = result.uint_32;
cbd071b5
DH
619 break;
620 case Opt_timeo:
e38bb238
SM
621 if (result.uint_32 < 1 || result.uint_32 > INT_MAX)
622 goto out_of_bounds;
623 ctx->timeo = result.uint_32;
cbd071b5
DH
624 break;
625 case Opt_retrans:
e38bb238
SM
626 if (result.uint_32 > INT_MAX)
627 goto out_of_bounds;
628 ctx->retrans = result.uint_32;
cbd071b5
DH
629 break;
630 case Opt_acregmin:
e38bb238 631 ctx->acregmin = result.uint_32;
cbd071b5
DH
632 break;
633 case Opt_acregmax:
e38bb238 634 ctx->acregmax = result.uint_32;
cbd071b5
DH
635 break;
636 case Opt_acdirmin:
e38bb238 637 ctx->acdirmin = result.uint_32;
cbd071b5
DH
638 break;
639 case Opt_acdirmax:
e38bb238 640 ctx->acdirmax = result.uint_32;
cbd071b5
DH
641 break;
642 case Opt_actimeo:
e38bb238
SM
643 ctx->acregmin = result.uint_32;
644 ctx->acregmax = result.uint_32;
645 ctx->acdirmin = result.uint_32;
646 ctx->acdirmax = result.uint_32;
cbd071b5
DH
647 break;
648 case Opt_namelen:
e38bb238 649 ctx->namlen = result.uint_32;
cbd071b5
DH
650 break;
651 case Opt_mountport:
e38bb238
SM
652 if (result.uint_32 > USHRT_MAX)
653 goto out_of_bounds;
654 ctx->mount_server.port = result.uint_32;
cbd071b5
DH
655 break;
656 case Opt_mountvers:
e38bb238
SM
657 if (result.uint_32 < NFS_MNT_VERSION ||
658 result.uint_32 > NFS_MNT3_VERSION)
659 goto out_of_bounds;
660 ctx->mount_server.version = result.uint_32;
cbd071b5
DH
661 break;
662 case Opt_minorversion:
e38bb238
SM
663 if (result.uint_32 > NFS4_MAX_MINOR_VERSION)
664 goto out_of_bounds;
665 ctx->minorversion = result.uint_32;
cbd071b5 666 break;
9954bf92
DH
667
668 /*
669 * options that take text values
670 */
e38bb238 671 case Opt_v:
62a55d08 672 ret = nfs_parse_version_string(fc, param->key + 1);
e38bb238
SM
673 if (ret < 0)
674 return ret;
675 break;
676 case Opt_vers:
62a55d08 677 ret = nfs_parse_version_string(fc, param->string);
e558100f
DH
678 if (ret < 0)
679 return ret;
cbd071b5
DH
680 break;
681 case Opt_sec:
62a55d08 682 ret = nfs_parse_security_flavors(fc, param);
e558100f
DH
683 if (ret < 0)
684 return ret;
cbd071b5 685 break;
cbd071b5 686
e38bb238
SM
687 case Opt_proto:
688 protofamily = AF_INET;
689 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
cbd071b5 690 case Opt_xprt_udp6:
e38bb238 691 protofamily = AF_INET6;
df561f66 692 fallthrough;
cbd071b5
DH
693 case Opt_xprt_udp:
694 ctx->flags &= ~NFS_MOUNT_TCP;
695 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
9954bf92 696 break;
cbd071b5 697 case Opt_xprt_tcp6:
e38bb238 698 protofamily = AF_INET6;
df561f66 699 fallthrough;
cbd071b5
DH
700 case Opt_xprt_tcp:
701 ctx->flags |= NFS_MOUNT_TCP;
702 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
9954bf92 703 break;
cbd071b5 704 case Opt_xprt_rdma6:
e38bb238 705 protofamily = AF_INET6;
df561f66 706 fallthrough;
cbd071b5
DH
707 case Opt_xprt_rdma:
708 /* vector side protocols to TCP */
709 ctx->flags |= NFS_MOUNT_TCP;
1c3695d0
TM
710 ret = xprt_find_transport_ident(param->string);
711 if (ret < 0)
712 goto out_bad_transport;
713 ctx->nfs_server.protocol = ret;
9954bf92 714 break;
cbd071b5 715 default:
1c3695d0 716 goto out_bad_transport;
cbd071b5 717 }
e38bb238
SM
718
719 ctx->protofamily = protofamily;
cbd071b5 720 break;
e38bb238 721
cbd071b5 722 case Opt_mountproto:
e38bb238
SM
723 mountfamily = AF_INET;
724 switch (lookup_constant(nfs_xprt_protocol_tokens, param->string, -1)) {
cbd071b5 725 case Opt_xprt_udp6:
e38bb238 726 mountfamily = AF_INET6;
df561f66 727 fallthrough;
cbd071b5
DH
728 case Opt_xprt_udp:
729 ctx->mount_server.protocol = XPRT_TRANSPORT_UDP;
730 break;
731 case Opt_xprt_tcp6:
e38bb238 732 mountfamily = AF_INET6;
df561f66 733 fallthrough;
cbd071b5
DH
734 case Opt_xprt_tcp:
735 ctx->mount_server.protocol = XPRT_TRANSPORT_TCP;
736 break;
737 case Opt_xprt_rdma: /* not used for side protocols */
738 default:
1c3695d0 739 goto out_bad_transport;
cbd071b5 740 }
e38bb238 741 ctx->mountfamily = mountfamily;
cbd071b5 742 break;
e38bb238 743
cbd071b5 744 case Opt_addr:
62a55d08 745 len = rpc_pton(fc->net_ns, param->string, param->size,
e38bb238
SM
746 &ctx->nfs_server.address,
747 sizeof(ctx->nfs_server._address));
748 if (len == 0)
cbd071b5 749 goto out_invalid_address;
e38bb238 750 ctx->nfs_server.addrlen = len;
cbd071b5
DH
751 break;
752 case Opt_clientaddr:
e38bb238
SM
753 kfree(ctx->client_address);
754 ctx->client_address = param->string;
755 param->string = NULL;
cbd071b5
DH
756 break;
757 case Opt_mounthost:
e38bb238
SM
758 kfree(ctx->mount_server.hostname);
759 ctx->mount_server.hostname = param->string;
760 param->string = NULL;
cbd071b5
DH
761 break;
762 case Opt_mountaddr:
62a55d08 763 len = rpc_pton(fc->net_ns, param->string, param->size,
e38bb238
SM
764 &ctx->mount_server.address,
765 sizeof(ctx->mount_server._address));
766 if (len == 0)
cbd071b5 767 goto out_invalid_address;
e38bb238 768 ctx->mount_server.addrlen = len;
cbd071b5
DH
769 break;
770 case Opt_nconnect:
e38bb238
SM
771 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_CONNECTIONS)
772 goto out_of_bounds;
773 ctx->nfs_server.nconnect = result.uint_32;
cbd071b5 774 break;
7e134205
OK
775 case Opt_max_connect:
776 if (result.uint_32 < 1 || result.uint_32 > NFS_MAX_TRANSPORTS)
777 goto out_of_bounds;
778 ctx->nfs_server.max_connect = result.uint_32;
779 break;
cbd071b5 780 case Opt_lookupcache:
e38bb238 781 switch (result.uint_32) {
cbd071b5
DH
782 case Opt_lookupcache_all:
783 ctx->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
9954bf92 784 break;
cbd071b5
DH
785 case Opt_lookupcache_positive:
786 ctx->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
787 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
9954bf92 788 break;
cbd071b5
DH
789 case Opt_lookupcache_none:
790 ctx->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
9954bf92 791 break;
cbd071b5 792 default:
e38bb238 793 goto out_invalid_value;
cbd071b5
DH
794 }
795 break;
cbd071b5 796 case Opt_local_lock:
e38bb238 797 switch (result.uint_32) {
cbd071b5
DH
798 case Opt_local_lock_all:
799 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK |
800 NFS_MOUNT_LOCAL_FCNTL);
9954bf92 801 break;
cbd071b5
DH
802 case Opt_local_lock_flock:
803 ctx->flags |= NFS_MOUNT_LOCAL_FLOCK;
9954bf92 804 break;
cbd071b5
DH
805 case Opt_local_lock_posix:
806 ctx->flags |= NFS_MOUNT_LOCAL_FCNTL;
9954bf92 807 break;
cbd071b5
DH
808 case Opt_local_lock_none:
809 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
810 NFS_MOUNT_LOCAL_FCNTL);
9954bf92 811 break;
cbd071b5 812 default:
e38bb238 813 goto out_invalid_value;
cbd071b5
DH
814 }
815 break;
a0492339
TM
816 case Opt_write:
817 switch (result.uint_32) {
818 case Opt_write_lazy:
819 ctx->flags &=
820 ~(NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT);
821 break;
822 case Opt_write_eager:
823 ctx->flags |= NFS_MOUNT_WRITE_EAGER;
824 ctx->flags &= ~NFS_MOUNT_WRITE_WAIT;
825 break;
826 case Opt_write_wait:
827 ctx->flags |=
828 NFS_MOUNT_WRITE_EAGER | NFS_MOUNT_WRITE_WAIT;
829 break;
830 default:
831 goto out_invalid_value;
832 }
833 break;
9954bf92
DH
834
835 /*
836 * Special options
837 */
cbd071b5 838 case Opt_sloppy:
e38bb238 839 ctx->sloppy = true;
cbd071b5
DH
840 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
841 break;
9954bf92
DH
842 }
843
f8ee01e3
DH
844 return 0;
845
f8ee01e3 846out_invalid_value:
ce8866f0 847 return nfs_invalf(fc, "NFS: Bad mount option value specified");
e38bb238 848out_invalid_address:
ce8866f0 849 return nfs_invalf(fc, "NFS: Bad IP address specified");
e38bb238 850out_of_bounds:
3a21409a 851 return nfs_invalf(fc, "NFS: Value for '%s' out of range", param->key);
1c3695d0
TM
852out_bad_transport:
853 return nfs_invalf(fc, "NFS: Unrecognized transport protocol");
e38bb238
SM
854}
855
9954bf92 856/*
62a55d08 857 * Split fc->source into "hostname:export_path".
9954bf92
DH
858 *
859 * The leftmost colon demarks the split between the server's hostname
860 * and the export path. If the hostname starts with a left square
861 * bracket, then it may contain colons.
862 *
863 * Note: caller frees hostname and export path, even on error.
864 */
62a55d08
SM
865static int nfs_parse_source(struct fs_context *fc,
866 size_t maxnamlen, size_t maxpathlen)
9954bf92 867{
62a55d08
SM
868 struct nfs_fs_context *ctx = nfs_fc2context(fc);
869 const char *dev_name = fc->source;
9954bf92 870 size_t len;
62a55d08 871 const char *end;
9954bf92
DH
872
873 if (unlikely(!dev_name || !*dev_name)) {
874 dfprintk(MOUNT, "NFS: device name not specified\n");
875 return -EINVAL;
876 }
877
878 /* Is the host name protected with square brakcets? */
879 if (*dev_name == '[') {
880 end = strchr(++dev_name, ']');
881 if (end == NULL || end[1] != ':')
882 goto out_bad_devname;
883
884 len = end - dev_name;
885 end++;
886 } else {
62a55d08 887 const char *comma;
9954bf92
DH
888
889 end = strchr(dev_name, ':');
890 if (end == NULL)
891 goto out_bad_devname;
892 len = end - dev_name;
893
894 /* kill possible hostname list: not supported */
62a55d08
SM
895 comma = memchr(dev_name, ',', len);
896 if (comma)
9954bf92
DH
897 len = comma - dev_name;
898 }
899
900 if (len > maxnamlen)
901 goto out_hostname;
902
75a9b917
SM
903 kfree(ctx->nfs_server.hostname);
904
9954bf92 905 /* N.B. caller will free nfs_server.hostname in all cases */
e558100f
DH
906 ctx->nfs_server.hostname = kmemdup_nul(dev_name, len, GFP_KERNEL);
907 if (!ctx->nfs_server.hostname)
9954bf92
DH
908 goto out_nomem;
909 len = strlen(++end);
910 if (len > maxpathlen)
911 goto out_path;
e558100f
DH
912 ctx->nfs_server.export_path = kmemdup_nul(end, len, GFP_KERNEL);
913 if (!ctx->nfs_server.export_path)
9954bf92
DH
914 goto out_nomem;
915
e558100f 916 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", ctx->nfs_server.export_path);
9954bf92
DH
917 return 0;
918
919out_bad_devname:
ce8866f0 920 return nfs_invalf(fc, "NFS: device name not in host:path format");
9954bf92 921out_nomem:
ce8866f0 922 nfs_errorf(fc, "NFS: not enough memory to parse device name");
9954bf92 923 return -ENOMEM;
9954bf92 924out_hostname:
ce8866f0 925 nfs_errorf(fc, "NFS: server hostname too long");
9954bf92 926 return -ENAMETOOLONG;
9954bf92 927out_path:
ce8866f0 928 nfs_errorf(fc, "NFS: export pathname too long");
9954bf92
DH
929 return -ENAMETOOLONG;
930}
931
f2aedb71
DH
932static inline bool is_remount_fc(struct fs_context *fc)
933{
934 return fc->root != NULL;
935}
936
9954bf92 937/*
e558100f 938 * Parse monolithic NFS2/NFS3 mount data
9954bf92
DH
939 * - fills in the mount root filehandle
940 *
941 * For option strings, user space handles the following behaviors:
942 *
943 * + DNS: mapping server host name to IP address ("addr=" option)
944 *
945 * + failure mode: how to behave if a mount request can't be handled
946 * immediately ("fg/bg" option)
947 *
948 * + retry: how often to retry a mount request ("retry=" option)
949 *
950 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
951 * mountproto=tcp after mountproto=udp, and so on
952 */
f2aedb71
DH
953static int nfs23_parse_monolithic(struct fs_context *fc,
954 struct nfs_mount_data *data)
9954bf92 955{
f2aedb71 956 struct nfs_fs_context *ctx = nfs_fc2context(fc);
62a55d08 957 struct nfs_fh *mntfh = ctx->mntfh;
5eb005ca 958 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
9954bf92 959 int extra_flags = NFS_MOUNT_LEGACY_INTERFACE;
90ff57bf 960 int ret;
9954bf92
DH
961
962 if (data == NULL)
963 goto out_no_data;
964
5eb005ca 965 ctx->version = NFS_DEFAULT_VERSION;
9954bf92
DH
966 switch (data->version) {
967 case 1:
df561f66
GS
968 data->namlen = 0;
969 fallthrough;
9954bf92 970 case 2:
df561f66
GS
971 data->bsize = 0;
972 fallthrough;
9954bf92
DH
973 case 3:
974 if (data->flags & NFS_MOUNT_VER3)
975 goto out_no_v3;
976 data->root.size = NFS2_FHSIZE;
977 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
978 /* Turn off security negotiation */
979 extra_flags |= NFS_MOUNT_SECFLAVOUR;
df561f66 980 fallthrough;
9954bf92
DH
981 case 4:
982 if (data->flags & NFS_MOUNT_SECFLAVOUR)
983 goto out_no_sec;
df561f66 984 fallthrough;
9954bf92
DH
985 case 5:
986 memset(data->context, 0, sizeof(data->context));
df561f66 987 fallthrough;
9954bf92
DH
988 case 6:
989 if (data->flags & NFS_MOUNT_VER3) {
990 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
991 goto out_invalid_fh;
992 mntfh->size = data->root.size;
5eb005ca 993 ctx->version = 3;
9954bf92
DH
994 } else {
995 mntfh->size = NFS2_FHSIZE;
5eb005ca 996 ctx->version = 2;
9954bf92
DH
997 }
998
999
1000 memcpy(mntfh->data, data->root.data, mntfh->size);
1001 if (mntfh->size < sizeof(mntfh->data))
1002 memset(mntfh->data + mntfh->size, 0,
1003 sizeof(mntfh->data) - mntfh->size);
1004
c09f11ef
RD
1005 /*
1006 * for proto == XPRT_TRANSPORT_UDP, which is what uses
1007 * to_exponential, implying shift: limit the shift value
1008 * to BITS_PER_LONG (majortimeo is unsigned long)
1009 */
1010 if (!(data->flags & NFS_MOUNT_TCP)) /* this will be UDP */
1011 if (data->retrans >= 64) /* shift value is too large */
1012 goto out_invalid_data;
1013
9954bf92 1014 /*
5eb005ca 1015 * Translate to nfs_fs_context, which nfs_fill_super
9954bf92
DH
1016 * can deal with.
1017 */
5eb005ca
DH
1018 ctx->flags = data->flags & NFS_MOUNT_FLAGMASK;
1019 ctx->flags |= extra_flags;
1020 ctx->rsize = data->rsize;
1021 ctx->wsize = data->wsize;
1022 ctx->timeo = data->timeo;
1023 ctx->retrans = data->retrans;
1024 ctx->acregmin = data->acregmin;
1025 ctx->acregmax = data->acregmax;
1026 ctx->acdirmin = data->acdirmin;
1027 ctx->acdirmax = data->acdirmax;
1028 ctx->need_mount = false;
9954bf92
DH
1029
1030 memcpy(sap, &data->addr, sizeof(data->addr));
5eb005ca
DH
1031 ctx->nfs_server.addrlen = sizeof(data->addr);
1032 ctx->nfs_server.port = ntohs(data->addr.sin_port);
9954bf92
DH
1033 if (sap->sa_family != AF_INET ||
1034 !nfs_verify_server_address(sap))
1035 goto out_no_address;
1036
1037 if (!(data->flags & NFS_MOUNT_TCP))
5eb005ca 1038 ctx->nfs_server.protocol = XPRT_TRANSPORT_UDP;
9954bf92 1039 /* N.B. caller will free nfs_server.hostname in all cases */
5eb005ca 1040 ctx->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
f2aedb71
DH
1041 if (!ctx->nfs_server.hostname)
1042 goto out_nomem;
1043
5eb005ca
DH
1044 ctx->namlen = data->namlen;
1045 ctx->bsize = data->bsize;
9954bf92
DH
1046
1047 if (data->flags & NFS_MOUNT_SECFLAVOUR)
5eb005ca 1048 ctx->selected_flavor = data->pseudoflavor;
9954bf92 1049 else
5eb005ca 1050 ctx->selected_flavor = RPC_AUTH_UNIX;
9954bf92
DH
1051
1052 if (!(data->flags & NFS_MOUNT_NONLM))
5eb005ca 1053 ctx->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
9954bf92
DH
1054 NFS_MOUNT_LOCAL_FCNTL);
1055 else
5eb005ca 1056 ctx->flags |= (NFS_MOUNT_LOCAL_FLOCK|
9954bf92 1057 NFS_MOUNT_LOCAL_FCNTL);
62a55d08 1058
9954bf92
DH
1059 /*
1060 * The legacy version 6 binary mount data from userspace has a
1061 * field used only to transport selinux information into the
a5032910 1062 * kernel. To continue to support that functionality we
9954bf92
DH
1063 * have a touch of selinux knowledge here in the NFS code. The
1064 * userspace code converted context=blah to just blah so we are
1065 * converting back to the full string selinux understands.
1066 */
1067 if (data->context[0]){
1068#ifdef CONFIG_SECURITY_SELINUX
f2aedb71
DH
1069 int ret;
1070
9954bf92 1071 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
f2aedb71
DH
1072 ret = vfs_parse_fs_string(fc, "context",
1073 data->context, strlen(data->context));
1074 if (ret < 0)
1075 return ret;
9954bf92
DH
1076#else
1077 return -EINVAL;
1078#endif
1079 }
1080
1081 break;
1082 default:
f2aedb71 1083 goto generic;
9954bf92
DH
1084 }
1085
90ff57bf
TM
1086 ret = nfs_validate_transport_protocol(fc, ctx);
1087 if (ret)
1088 return ret;
1089
f2aedb71 1090 ctx->skip_reconfig_option_check = true;
9954bf92
DH
1091 return 0;
1092
f2aedb71
DH
1093generic:
1094 return generic_parse_monolithic(fc, data);
1095
9954bf92 1096out_no_data:
f2aedb71
DH
1097 if (is_remount_fc(fc)) {
1098 ctx->skip_reconfig_option_check = true;
1099 return 0;
1100 }
ce8866f0 1101 return nfs_invalf(fc, "NFS: mount program didn't pass any mount data");
9954bf92
DH
1102
1103out_no_v3:
ce8866f0 1104 return nfs_invalf(fc, "NFS: nfs_mount_data version does not support v3");
9954bf92
DH
1105
1106out_no_sec:
ce8866f0 1107 return nfs_invalf(fc, "NFS: nfs_mount_data version supports only AUTH_SYS");
9954bf92
DH
1108
1109out_nomem:
ce8866f0 1110 dfprintk(MOUNT, "NFS: not enough memory to handle mount options");
9954bf92
DH
1111 return -ENOMEM;
1112
1113out_no_address:
ce8866f0 1114 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
9954bf92
DH
1115
1116out_invalid_fh:
ce8866f0 1117 return nfs_invalf(fc, "NFS: invalid root filehandle");
c09f11ef
RD
1118
1119out_invalid_data:
1120 return nfs_invalf(fc, "NFS: invalid binary mount data");
9954bf92
DH
1121}
1122
1123#if IS_ENABLED(CONFIG_NFS_V4)
67e306c6
CH
1124struct compat_nfs_string {
1125 compat_uint_t len;
1126 compat_uptr_t data;
1127};
1128
1129static inline void compat_nfs_string(struct nfs_string *dst,
1130 struct compat_nfs_string *src)
1131{
1132 dst->data = compat_ptr(src->data);
1133 dst->len = src->len;
1134}
1135
1136struct compat_nfs4_mount_data_v1 {
1137 compat_int_t version;
1138 compat_int_t flags;
1139 compat_int_t rsize;
1140 compat_int_t wsize;
1141 compat_int_t timeo;
1142 compat_int_t retrans;
1143 compat_int_t acregmin;
1144 compat_int_t acregmax;
1145 compat_int_t acdirmin;
1146 compat_int_t acdirmax;
1147 struct compat_nfs_string client_addr;
1148 struct compat_nfs_string mnt_path;
1149 struct compat_nfs_string hostname;
1150 compat_uint_t host_addrlen;
1151 compat_uptr_t host_addr;
1152 compat_int_t proto;
1153 compat_int_t auth_flavourlen;
1154 compat_uptr_t auth_flavours;
1155};
1156
1157static void nfs4_compat_mount_data_conv(struct nfs4_mount_data *data)
1158{
1159 struct compat_nfs4_mount_data_v1 *compat =
1160 (struct compat_nfs4_mount_data_v1 *)data;
1161
1162 /* copy the fields backwards */
1163 data->auth_flavours = compat_ptr(compat->auth_flavours);
1164 data->auth_flavourlen = compat->auth_flavourlen;
1165 data->proto = compat->proto;
1166 data->host_addr = compat_ptr(compat->host_addr);
1167 data->host_addrlen = compat->host_addrlen;
1168 compat_nfs_string(&data->hostname, &compat->hostname);
1169 compat_nfs_string(&data->mnt_path, &compat->mnt_path);
1170 compat_nfs_string(&data->client_addr, &compat->client_addr);
1171 data->acdirmax = compat->acdirmax;
1172 data->acdirmin = compat->acdirmin;
1173 data->acregmax = compat->acregmax;
1174 data->acregmin = compat->acregmin;
1175 data->retrans = compat->retrans;
1176 data->timeo = compat->timeo;
1177 data->wsize = compat->wsize;
1178 data->rsize = compat->rsize;
1179 data->flags = compat->flags;
1180 data->version = compat->version;
1181}
1182
9954bf92
DH
1183/*
1184 * Validate NFSv4 mount options
1185 */
f2aedb71
DH
1186static int nfs4_parse_monolithic(struct fs_context *fc,
1187 struct nfs4_mount_data *data)
9954bf92 1188{
f2aedb71 1189 struct nfs_fs_context *ctx = nfs_fc2context(fc);
5eb005ca 1190 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
90ff57bf 1191 int ret;
9954bf92
DH
1192 char *c;
1193
a1c7dc5d
CH
1194 if (!data) {
1195 if (is_remount_fc(fc))
1196 goto done;
1197 return nfs_invalf(fc,
1198 "NFS4: mount program didn't pass any mount data");
1199 }
9954bf92 1200
5eb005ca 1201 ctx->version = 4;
9954bf92 1202
a1c7dc5d
CH
1203 if (data->version != 1)
1204 return generic_parse_monolithic(fc, data);
1205
67e306c6
CH
1206 if (in_compat_syscall())
1207 nfs4_compat_mount_data_conv(data);
1208
a1c7dc5d
CH
1209 if (data->host_addrlen > sizeof(ctx->nfs_server.address))
1210 goto out_no_address;
1211 if (data->host_addrlen == 0)
1212 goto out_no_address;
1213 ctx->nfs_server.addrlen = data->host_addrlen;
1214 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
1215 return -EFAULT;
1216 if (!nfs_verify_server_address(sap))
1217 goto out_no_address;
1218 ctx->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1219
1220 if (data->auth_flavourlen) {
1221 rpc_authflavor_t pseudoflavor;
1222
1223 if (data->auth_flavourlen > 1)
1224 goto out_inval_auth;
1225 if (copy_from_user(&pseudoflavor, data->auth_flavours,
1226 sizeof(pseudoflavor)))
9954bf92 1227 return -EFAULT;
a1c7dc5d
CH
1228 ctx->selected_flavor = pseudoflavor;
1229 } else {
1230 ctx->selected_flavor = RPC_AUTH_UNIX;
9954bf92
DH
1231 }
1232
a1c7dc5d
CH
1233 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1234 if (IS_ERR(c))
1235 return PTR_ERR(c);
1236 ctx->nfs_server.hostname = c;
1237
1238 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1239 if (IS_ERR(c))
1240 return PTR_ERR(c);
1241 ctx->nfs_server.export_path = c;
1242 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1243
1244 c = strndup_user(data->client_addr.data, 16);
1245 if (IS_ERR(c))
1246 return PTR_ERR(c);
1247 ctx->client_address = c;
1248
1249 /*
1250 * Translate to nfs_fs_context, which nfs_fill_super
1251 * can deal with.
1252 */
1253
1254 ctx->flags = data->flags & NFS4_MOUNT_FLAGMASK;
1255 ctx->rsize = data->rsize;
1256 ctx->wsize = data->wsize;
1257 ctx->timeo = data->timeo;
1258 ctx->retrans = data->retrans;
1259 ctx->acregmin = data->acregmin;
1260 ctx->acregmax = data->acregmax;
1261 ctx->acdirmin = data->acdirmin;
1262 ctx->acdirmax = data->acdirmax;
1263 ctx->nfs_server.protocol = data->proto;
90ff57bf
TM
1264 ret = nfs_validate_transport_protocol(fc, ctx);
1265 if (ret)
1266 return ret;
a1c7dc5d 1267done:
f2aedb71 1268 ctx->skip_reconfig_option_check = true;
9954bf92
DH
1269 return 0;
1270
9954bf92 1271out_inval_auth:
ce8866f0
SM
1272 return nfs_invalf(fc, "NFS4: Invalid number of RPC auth flavours %d",
1273 data->auth_flavourlen);
9954bf92
DH
1274
1275out_no_address:
ce8866f0 1276 return nfs_invalf(fc, "NFS4: mount program didn't pass remote address");
9954bf92 1277}
f2aedb71 1278#endif
9954bf92 1279
f2aedb71
DH
1280/*
1281 * Parse a monolithic block of data from sys_mount().
1282 */
1283static int nfs_fs_context_parse_monolithic(struct fs_context *fc,
1284 void *data)
9954bf92 1285{
f2aedb71
DH
1286 if (fc->fs_type == &nfs_fs_type)
1287 return nfs23_parse_monolithic(fc, data);
1288
1289#if IS_ENABLED(CONFIG_NFS_V4)
1290 if (fc->fs_type == &nfs4_fs_type)
1291 return nfs4_parse_monolithic(fc, data);
9954bf92
DH
1292#endif
1293
ce8866f0 1294 return nfs_invalf(fc, "NFS: Unsupported monolithic data version");
f2aedb71
DH
1295}
1296
1297/*
1298 * Validate the preparsed information in the config.
1299 */
1300static int nfs_fs_context_validate(struct fs_context *fc)
9954bf92 1301{
f2aedb71
DH
1302 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1303 struct nfs_subversion *nfs_mod;
1304 struct sockaddr *sap = (struct sockaddr *)&ctx->nfs_server.address;
9954bf92
DH
1305 int max_namelen = PAGE_SIZE;
1306 int max_pathlen = NFS_MAXPATHLEN;
f2aedb71
DH
1307 int port = 0;
1308 int ret;
9954bf92 1309
f2aedb71
DH
1310 if (!fc->source)
1311 goto out_no_device_name;
1312
1313 /* Check for sanity first. */
1314 if (ctx->minorversion && ctx->version != 4)
1315 goto out_minorversion_mismatch;
1316
1317 if (ctx->options & NFS_OPTION_MIGRATION &&
1318 (ctx->version != 4 || ctx->minorversion != 0))
1319 goto out_migration_misuse;
1320
1321 /* Verify that any proto=/mountproto= options match the address
1322 * families in the addr=/mountaddr= options.
1323 */
1324 if (ctx->protofamily != AF_UNSPEC &&
1325 ctx->protofamily != ctx->nfs_server.address.sa_family)
1326 goto out_proto_mismatch;
1327
1328 if (ctx->mountfamily != AF_UNSPEC) {
1329 if (ctx->mount_server.addrlen) {
1330 if (ctx->mountfamily != ctx->mount_server.address.sa_family)
1331 goto out_mountproto_mismatch;
1332 } else {
1333 if (ctx->mountfamily != ctx->nfs_server.address.sa_family)
1334 goto out_mountproto_mismatch;
1335 }
1336 }
9954bf92
DH
1337
1338 if (!nfs_verify_server_address(sap))
1339 goto out_no_address;
1340
90ff57bf
TM
1341 ret = nfs_validate_transport_protocol(fc, ctx);
1342 if (ret)
1343 return ret;
1344
5eb005ca 1345 if (ctx->version == 4) {
62a55d08
SM
1346 if (IS_ENABLED(CONFIG_NFS_V4)) {
1347 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
1348 port = NFS_RDMA_PORT;
1349 else
1350 port = NFS_PORT;
1351 max_namelen = NFS4_MAXNAMLEN;
1352 max_pathlen = NFS4_MAXPATHLEN;
62a55d08
SM
1353 ctx->flags &= ~(NFS_MOUNT_NONLM | NFS_MOUNT_NOACL |
1354 NFS_MOUNT_VER3 | NFS_MOUNT_LOCAL_FLOCK |
1355 NFS_MOUNT_LOCAL_FCNTL);
1356 } else {
1357 goto out_v4_not_compiled;
1358 }
9954bf92 1359 } else {
5eb005ca
DH
1360 nfs_set_mount_transport_protocol(ctx);
1361 if (ctx->nfs_server.protocol == XPRT_TRANSPORT_RDMA)
9954bf92
DH
1362 port = NFS_RDMA_PORT;
1363 }
1364
5eb005ca 1365 nfs_set_port(sap, &ctx->nfs_server.port, port);
9954bf92 1366
62a55d08 1367 ret = nfs_parse_source(fc, max_namelen, max_pathlen);
f2aedb71
DH
1368 if (ret < 0)
1369 return ret;
1370
1371 /* Load the NFS protocol module if we haven't done so yet */
62a55d08 1372 if (!ctx->nfs_mod) {
f2aedb71
DH
1373 nfs_mod = get_nfs_version(ctx->version);
1374 if (IS_ERR(nfs_mod)) {
1375 ret = PTR_ERR(nfs_mod);
1376 goto out_version_unavailable;
1377 }
62a55d08 1378 ctx->nfs_mod = nfs_mod;
f2aedb71 1379 }
1cef2184
SM
1380
1381 /* Ensure the filesystem context has the correct fs_type */
1382 if (fc->fs_type != ctx->nfs_mod->nfs_fs) {
1383 module_put(fc->fs_type->owner);
1384 __module_get(ctx->nfs_mod->nfs_fs->owner);
1385 fc->fs_type = ctx->nfs_mod->nfs_fs;
1386 }
f2aedb71 1387 return 0;
9954bf92 1388
f2aedb71 1389out_no_device_name:
ce8866f0 1390 return nfs_invalf(fc, "NFS: Device name not specified");
9954bf92 1391out_v4_not_compiled:
ce8866f0 1392 nfs_errorf(fc, "NFS: NFSv4 is not compiled into kernel");
9954bf92 1393 return -EPROTONOSUPPORT;
9954bf92 1394out_no_address:
ce8866f0 1395 return nfs_invalf(fc, "NFS: mount program didn't pass remote address");
f2aedb71 1396out_mountproto_mismatch:
ce8866f0 1397 return nfs_invalf(fc, "NFS: Mount server address does not match mountproto= option");
f2aedb71 1398out_proto_mismatch:
ce8866f0 1399 return nfs_invalf(fc, "NFS: Server address does not match proto= option");
f2aedb71 1400out_minorversion_mismatch:
ce8866f0 1401 return nfs_invalf(fc, "NFS: Mount option vers=%u does not support minorversion=%u",
f2aedb71 1402 ctx->version, ctx->minorversion);
f2aedb71 1403out_migration_misuse:
ce8866f0 1404 return nfs_invalf(fc, "NFS: 'Migration' not supported for this NFS version");
f2aedb71 1405out_version_unavailable:
ce8866f0 1406 nfs_errorf(fc, "NFS: Version unavailable");
f2aedb71
DH
1407 return ret;
1408}
1409
1410/*
1411 * Create an NFS superblock by the appropriate method.
1412 */
1413static int nfs_get_tree(struct fs_context *fc)
1414{
1415 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1416 int err = nfs_fs_context_validate(fc);
1417
1418 if (err)
1419 return err;
1420 if (!ctx->internal)
62a55d08 1421 return ctx->nfs_mod->rpc_ops->try_get_tree(fc);
f2aedb71
DH
1422 else
1423 return nfs_get_tree_common(fc);
9954bf92 1424}
f2aedb71
DH
1425
1426/*
1427 * Handle duplication of a configuration. The caller copied *src into *sc, but
1428 * it can't deal with resource pointers in the filesystem context, so we have
1429 * to do that. We need to clear pointers, copy data or get extra refs as
1430 * appropriate.
1431 */
1432static int nfs_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1433{
1434 struct nfs_fs_context *src = nfs_fc2context(src_fc), *ctx;
1435
1436 ctx = kmemdup(src, sizeof(struct nfs_fs_context), GFP_KERNEL);
1437 if (!ctx)
1438 return -ENOMEM;
1439
62a55d08
SM
1440 ctx->mntfh = nfs_alloc_fhandle();
1441 if (!ctx->mntfh) {
f2aedb71
DH
1442 kfree(ctx);
1443 return -ENOMEM;
1444 }
62a55d08 1445 nfs_copy_fh(ctx->mntfh, src->mntfh);
f2aedb71 1446
62a55d08 1447 __module_get(ctx->nfs_mod->owner);
f2aedb71
DH
1448 ctx->client_address = NULL;
1449 ctx->mount_server.hostname = NULL;
1450 ctx->nfs_server.export_path = NULL;
1451 ctx->nfs_server.hostname = NULL;
1452 ctx->fscache_uniq = NULL;
f2aedb71
DH
1453 ctx->clone_data.fattr = NULL;
1454 fc->fs_private = ctx;
1455 return 0;
1456}
1457
1458static void nfs_fs_context_free(struct fs_context *fc)
1459{
1460 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1461
1462 if (ctx) {
62a55d08
SM
1463 if (ctx->server)
1464 nfs_free_server(ctx->server);
1465 if (ctx->nfs_mod)
1466 put_nfs_version(ctx->nfs_mod);
f2aedb71
DH
1467 kfree(ctx->client_address);
1468 kfree(ctx->mount_server.hostname);
1469 kfree(ctx->nfs_server.export_path);
1470 kfree(ctx->nfs_server.hostname);
1471 kfree(ctx->fscache_uniq);
62a55d08 1472 nfs_free_fhandle(ctx->mntfh);
f2aedb71
DH
1473 nfs_free_fattr(ctx->clone_data.fattr);
1474 kfree(ctx);
1475 }
1476}
1477
1478static const struct fs_context_operations nfs_fs_context_ops = {
1479 .free = nfs_fs_context_free,
1480 .dup = nfs_fs_context_dup,
1481 .parse_param = nfs_fs_context_parse_param,
1482 .parse_monolithic = nfs_fs_context_parse_monolithic,
1483 .get_tree = nfs_get_tree,
1484 .reconfigure = nfs_reconfigure,
1485};
1486
1487/*
1488 * Prepare superblock configuration. We use the namespaces attached to the
1489 * context. This may be the current process's namespaces, or it may be a
1490 * container's namespaces.
1491 */
1492static int nfs_init_fs_context(struct fs_context *fc)
1493{
1494 struct nfs_fs_context *ctx;
1495
1496 ctx = kzalloc(sizeof(struct nfs_fs_context), GFP_KERNEL);
1497 if (unlikely(!ctx))
1498 return -ENOMEM;
1499
62a55d08
SM
1500 ctx->mntfh = nfs_alloc_fhandle();
1501 if (unlikely(!ctx->mntfh)) {
f2aedb71
DH
1502 kfree(ctx);
1503 return -ENOMEM;
1504 }
1505
1506 ctx->protofamily = AF_UNSPEC;
1507 ctx->mountfamily = AF_UNSPEC;
1508 ctx->mount_server.port = NFS_UNSPEC_PORT;
1509
1510 if (fc->root) {
1511 /* reconfigure, start with the current config */
1512 struct nfs_server *nfss = fc->root->d_sb->s_fs_info;
1513 struct net *net = nfss->nfs_client->cl_net;
1514
1515 ctx->flags = nfss->flags;
1516 ctx->rsize = nfss->rsize;
1517 ctx->wsize = nfss->wsize;
1518 ctx->retrans = nfss->client->cl_timeout->to_retries;
1519 ctx->selected_flavor = nfss->client->cl_auth->au_flavor;
1520 ctx->acregmin = nfss->acregmin / HZ;
1521 ctx->acregmax = nfss->acregmax / HZ;
1522 ctx->acdirmin = nfss->acdirmin / HZ;
1523 ctx->acdirmax = nfss->acdirmax / HZ;
1524 ctx->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1525 ctx->nfs_server.port = nfss->port;
1526 ctx->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1527 ctx->version = nfss->nfs_client->rpc_ops->version;
1528 ctx->minorversion = nfss->nfs_client->cl_minorversion;
1529
1530 memcpy(&ctx->nfs_server.address, &nfss->nfs_client->cl_addr,
1531 ctx->nfs_server.addrlen);
1532
1533 if (fc->net_ns != net) {
1534 put_net(fc->net_ns);
1535 fc->net_ns = get_net(net);
1536 }
1537
62a55d08
SM
1538 ctx->nfs_mod = nfss->nfs_client->cl_nfs_mod;
1539 __module_get(ctx->nfs_mod->owner);
f2aedb71
DH
1540 } else {
1541 /* defaults */
1542 ctx->timeo = NFS_UNSPEC_TIMEO;
1543 ctx->retrans = NFS_UNSPEC_RETRANS;
1544 ctx->acregmin = NFS_DEF_ACREGMIN;
1545 ctx->acregmax = NFS_DEF_ACREGMAX;
1546 ctx->acdirmin = NFS_DEF_ACDIRMIN;
1547 ctx->acdirmax = NFS_DEF_ACDIRMAX;
1548 ctx->nfs_server.port = NFS_UNSPEC_PORT;
1549 ctx->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1550 ctx->selected_flavor = RPC_AUTH_MAXFLAVOR;
1551 ctx->minorversion = 0;
1552 ctx->need_mount = true;
6c17260c
TM
1553
1554 fc->s_iflags |= SB_I_STABLE_WRITES;
f2aedb71 1555 }
f2aedb71
DH
1556 fc->fs_private = ctx;
1557 fc->ops = &nfs_fs_context_ops;
1558 return 0;
1559}
1560
1561struct file_system_type nfs_fs_type = {
1562 .owner = THIS_MODULE,
1563 .name = "nfs",
1564 .init_fs_context = nfs_init_fs_context,
d7167b14 1565 .parameters = nfs_fs_parameters,
f2aedb71
DH
1566 .kill_sb = nfs_kill_super,
1567 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1568};
1569MODULE_ALIAS_FS("nfs");
1570EXPORT_SYMBOL_GPL(nfs_fs_type);
1571
1572#if IS_ENABLED(CONFIG_NFS_V4)
1573struct file_system_type nfs4_fs_type = {
1574 .owner = THIS_MODULE,
1575 .name = "nfs4",
1576 .init_fs_context = nfs_init_fs_context,
d7167b14 1577 .parameters = nfs_fs_parameters,
f2aedb71
DH
1578 .kill_sb = nfs_kill_super,
1579 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
1580};
1581MODULE_ALIAS_FS("nfs4");
1582MODULE_ALIAS("nfs4");
1583EXPORT_SYMBOL_GPL(nfs4_fs_type);
1584#endif /* CONFIG_NFS_V4 */