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