nfsv3: Make v3 mounts fail with ETIMEDOUTs instead EIO on mountd timeouts
[deliverable/linux.git] / fs / nfs / super.c
CommitLineData
f7b422b1
DH
1/*
2 * linux/fs/nfs/super.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs superblock handling functions
7 *
526719ba 8 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
f7b422b1
DH
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
10 *
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
13 *
14 * Split from inode.c by David Howells <dhowells@redhat.com>
15 *
54ceac45
DH
16 * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
17 * particular server are held in the same superblock
18 * - NFS superblocks can have several effective roots to the dentry tree
19 * - directory type roots are spliced into the tree when a path from one root reaches the root
20 * of another (see nfs_lookup())
f7b422b1
DH
21 */
22
f7b422b1
DH
23#include <linux/module.h>
24#include <linux/init.h>
25
26#include <linux/time.h>
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/string.h>
30#include <linux/stat.h>
31#include <linux/errno.h>
32#include <linux/unistd.h>
33#include <linux/sunrpc/clnt.h>
34#include <linux/sunrpc/stats.h>
35#include <linux/sunrpc/metrics.h>
0896a725 36#include <linux/sunrpc/xprtsock.h>
2cf7ff7a 37#include <linux/sunrpc/xprtrdma.h>
f7b422b1
DH
38#include <linux/nfs_fs.h>
39#include <linux/nfs_mount.h>
40#include <linux/nfs4_mount.h>
41#include <linux/lockd/bind.h>
f7b422b1
DH
42#include <linux/seq_file.h>
43#include <linux/mount.h>
c02d7adf 44#include <linux/namei.h>
f7b422b1
DH
45#include <linux/nfs_idmap.h>
46#include <linux/vfs.h>
47#include <linux/inet.h>
fd00a8ff 48#include <linux/in6.h>
5a0e3ad6 49#include <linux/slab.h>
fd00a8ff 50#include <net/ipv6.h>
d8e7748a 51#include <linux/netdevice.h>
f7b422b1 52#include <linux/nfs_xdr.h>
b5d5dfbd 53#include <linux/magic.h>
bf0fd768 54#include <linux/parser.h>
e50a7a1a 55#include <linux/nsproxy.h>
2446ab60 56#include <linux/rcupdate.h>
f7b422b1 57
f7b422b1
DH
58#include <asm/uaccess.h>
59
60#include "nfs4_fs.h"
61#include "callback.h"
62#include "delegation.h"
63#include "iostat.h"
64#include "internal.h"
08734048 65#include "fscache.h"
ae50c0b5 66#include "pnfs.h"
ab7017a3 67#include "nfs.h"
f7b422b1
DH
68
69#define NFSDBG_FACILITY NFSDBG_VFS
b72e4f42 70#define NFS_TEXT_DATA 1
f7b422b1 71
1c606fb7 72#if IS_ENABLED(CONFIG_NFS_V3)
1e657bd5
PZ
73#define NFS_DEFAULT_VERSION 3
74#else
75#define NFS_DEFAULT_VERSION 2
76#endif
77
bf0fd768
CL
78enum {
79 /* Mount options that take no arguments */
80 Opt_soft, Opt_hard,
bf0fd768
CL
81 Opt_posix, Opt_noposix,
82 Opt_cto, Opt_nocto,
83 Opt_ac, Opt_noac,
84 Opt_lock, Opt_nolock,
2cf7ff7a 85 Opt_udp, Opt_tcp, Opt_rdma,
bf0fd768
CL
86 Opt_acl, Opt_noacl,
87 Opt_rdirplus, Opt_nordirplus,
75180df2 88 Opt_sharecache, Opt_nosharecache,
d740351b 89 Opt_resvport, Opt_noresvport,
b797cac7 90 Opt_fscache, Opt_nofscache,
89652617 91 Opt_migration, Opt_nomigration,
bf0fd768
CL
92
93 /* Mount options that take integer arguments */
94 Opt_port,
95 Opt_rsize, Opt_wsize, Opt_bsize,
96 Opt_timeo, Opt_retrans,
97 Opt_acregmin, Opt_acregmax,
98 Opt_acdirmin, Opt_acdirmax,
99 Opt_actimeo,
100 Opt_namelen,
101 Opt_mountport,
e887cbcf 102 Opt_mountvers,
3fd5be9e 103 Opt_minorversion,
bf0fd768
CL
104
105 /* Mount options that take string arguments */
0d71b058 106 Opt_nfsvers,
33832034 107 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
0ac83779 108 Opt_addr, Opt_mountaddr, Opt_clientaddr,
7973c1f1 109 Opt_lookupcache,
b797cac7 110 Opt_fscache_uniq,
5eebde23 111 Opt_local_lock,
bf0fd768 112
f45663ce
CL
113 /* Special mount options */
114 Opt_userspace, Opt_deprecated, Opt_sloppy,
bf0fd768
CL
115
116 Opt_err
117};
118
a447c093 119static const match_table_t nfs_mount_option_tokens = {
bf0fd768
CL
120 { Opt_userspace, "bg" },
121 { Opt_userspace, "fg" },
ecbb3845
CL
122 { Opt_userspace, "retry=%s" },
123
f45663ce
CL
124 { Opt_sloppy, "sloppy" },
125
bf0fd768
CL
126 { Opt_soft, "soft" },
127 { Opt_hard, "hard" },
d33e4dfe
CL
128 { Opt_deprecated, "intr" },
129 { Opt_deprecated, "nointr" },
bf0fd768
CL
130 { Opt_posix, "posix" },
131 { Opt_noposix, "noposix" },
132 { Opt_cto, "cto" },
133 { Opt_nocto, "nocto" },
134 { Opt_ac, "ac" },
135 { Opt_noac, "noac" },
136 { Opt_lock, "lock" },
137 { Opt_nolock, "nolock" },
bf0fd768
CL
138 { Opt_udp, "udp" },
139 { Opt_tcp, "tcp" },
2cf7ff7a 140 { Opt_rdma, "rdma" },
bf0fd768
CL
141 { Opt_acl, "acl" },
142 { Opt_noacl, "noacl" },
143 { Opt_rdirplus, "rdirplus" },
144 { Opt_nordirplus, "nordirplus" },
75180df2
TM
145 { Opt_sharecache, "sharecache" },
146 { Opt_nosharecache, "nosharecache" },
d740351b
CL
147 { Opt_resvport, "resvport" },
148 { Opt_noresvport, "noresvport" },
b797cac7 149 { Opt_fscache, "fsc" },
b797cac7 150 { Opt_nofscache, "nofsc" },
89652617
CL
151 { Opt_migration, "migration" },
152 { Opt_nomigration, "nomigration" },
bf0fd768 153
a5a16bae
CL
154 { Opt_port, "port=%s" },
155 { Opt_rsize, "rsize=%s" },
156 { Opt_wsize, "wsize=%s" },
157 { Opt_bsize, "bsize=%s" },
158 { Opt_timeo, "timeo=%s" },
159 { Opt_retrans, "retrans=%s" },
160 { Opt_acregmin, "acregmin=%s" },
161 { Opt_acregmax, "acregmax=%s" },
162 { Opt_acdirmin, "acdirmin=%s" },
163 { Opt_acdirmax, "acdirmax=%s" },
164 { Opt_actimeo, "actimeo=%s" },
165 { Opt_namelen, "namlen=%s" },
166 { Opt_mountport, "mountport=%s" },
167 { Opt_mountvers, "mountvers=%s" },
0d71b058
TM
168 { Opt_minorversion, "minorversion=%s" },
169
a5a16bae
CL
170 { Opt_nfsvers, "nfsvers=%s" },
171 { Opt_nfsvers, "vers=%s" },
bf0fd768
CL
172
173 { Opt_sec, "sec=%s" },
174 { Opt_proto, "proto=%s" },
175 { Opt_mountproto, "mountproto=%s" },
176 { Opt_addr, "addr=%s" },
177 { Opt_clientaddr, "clientaddr=%s" },
33832034 178 { Opt_mounthost, "mounthost=%s" },
0ac83779 179 { Opt_mountaddr, "mountaddr=%s" },
bf0fd768 180
7973c1f1 181 { Opt_lookupcache, "lookupcache=%s" },
a6d5ff64 182 { Opt_fscache_uniq, "fsc=%s" },
5eebde23 183 { Opt_local_lock, "local_lock=%s" },
7973c1f1 184
3862279a
TM
185 /* The following needs to be listed after all other options */
186 { Opt_nfsvers, "v%s" },
187
bf0fd768
CL
188 { Opt_err, NULL }
189};
190
191enum {
ee671b01 192 Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
bf0fd768
CL
193
194 Opt_xprt_err
195};
196
a447c093 197static const match_table_t nfs_xprt_protocol_tokens = {
bf0fd768 198 { Opt_xprt_udp, "udp" },
ee671b01 199 { Opt_xprt_udp6, "udp6" },
bf0fd768 200 { Opt_xprt_tcp, "tcp" },
ee671b01 201 { Opt_xprt_tcp6, "tcp6" },
2cf7ff7a 202 { Opt_xprt_rdma, "rdma" },
bf0fd768
CL
203
204 { Opt_xprt_err, NULL }
205};
206
207enum {
208 Opt_sec_none, Opt_sec_sys,
209 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
210 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
211 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
212
213 Opt_sec_err
214};
215
a447c093 216static const match_table_t nfs_secflavor_tokens = {
bf0fd768
CL
217 { Opt_sec_none, "none" },
218 { Opt_sec_none, "null" },
219 { Opt_sec_sys, "sys" },
220
221 { Opt_sec_krb5, "krb5" },
222 { Opt_sec_krb5i, "krb5i" },
223 { Opt_sec_krb5p, "krb5p" },
224
225 { Opt_sec_lkey, "lkey" },
226 { Opt_sec_lkeyi, "lkeyi" },
227 { Opt_sec_lkeyp, "lkeyp" },
228
8d042218
OK
229 { Opt_sec_spkm, "spkm3" },
230 { Opt_sec_spkmi, "spkm3i" },
231 { Opt_sec_spkmp, "spkm3p" },
232
bf0fd768
CL
233 { Opt_sec_err, NULL }
234};
235
7973c1f1
TM
236enum {
237 Opt_lookupcache_all, Opt_lookupcache_positive,
238 Opt_lookupcache_none,
239
240 Opt_lookupcache_err
241};
242
243static match_table_t nfs_lookupcache_tokens = {
244 { Opt_lookupcache_all, "all" },
245 { Opt_lookupcache_positive, "pos" },
246 { Opt_lookupcache_positive, "positive" },
247 { Opt_lookupcache_none, "none" },
248
249 { Opt_lookupcache_err, NULL }
250};
251
5eebde23
SJ
252enum {
253 Opt_local_lock_all, Opt_local_lock_flock, Opt_local_lock_posix,
254 Opt_local_lock_none,
255
256 Opt_local_lock_err
257};
258
259static match_table_t nfs_local_lock_tokens = {
260 { Opt_local_lock_all, "all" },
261 { Opt_local_lock_flock, "flock" },
262 { Opt_local_lock_posix, "posix" },
263 { Opt_local_lock_none, "none" },
264
265 { Opt_local_lock_err, NULL }
266};
267
0d71b058
TM
268enum {
269 Opt_vers_2, Opt_vers_3, Opt_vers_4, Opt_vers_4_0,
270 Opt_vers_4_1,
271
272 Opt_vers_err
273};
274
275static match_table_t nfs_vers_tokens = {
276 { Opt_vers_2, "2" },
277 { Opt_vers_3, "3" },
278 { Opt_vers_4, "4" },
279 { Opt_vers_4_0, "4.0" },
280 { Opt_vers_4_1, "4.1" },
281
282 { Opt_vers_err, NULL }
283};
bf0fd768 284
31f43471
AV
285static struct dentry *nfs_xdev_mount(struct file_system_type *fs_type,
286 int flags, const char *dev_name, void *raw_data);
f7b422b1 287
ab7017a3 288struct file_system_type nfs_fs_type = {
f7b422b1
DH
289 .owner = THIS_MODULE,
290 .name = "nfs",
01194981 291 .mount = nfs_fs_mount,
f7b422b1 292 .kill_sb = nfs_kill_super,
349457cc 293 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
f7b422b1 294};
ddda8e0a 295EXPORT_SYMBOL_GPL(nfs_fs_type);
f7b422b1 296
54ceac45 297struct file_system_type nfs_xdev_fs_type = {
f7b422b1
DH
298 .owner = THIS_MODULE,
299 .name = "nfs",
31f43471 300 .mount = nfs_xdev_mount,
f7b422b1 301 .kill_sb = nfs_kill_super,
349457cc 302 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
f7b422b1
DH
303};
304
6a74490d 305const struct super_operations nfs_sops = {
f7b422b1
DH
306 .alloc_inode = nfs_alloc_inode,
307 .destroy_inode = nfs_destroy_inode,
308 .write_inode = nfs_write_inode,
387c149b 309 .put_super = nfs_put_super,
f7b422b1 310 .statfs = nfs_statfs,
b57922d9 311 .evict_inode = nfs_evict_inode,
f7b422b1
DH
312 .umount_begin = nfs_umount_begin,
313 .show_options = nfs_show_options,
c7f404b4
AV
314 .show_devname = nfs_show_devname,
315 .show_path = nfs_show_path,
f7b422b1 316 .show_stats = nfs_show_stats,
48b605f8 317 .remount_fs = nfs_remount,
f7b422b1 318};
ddda8e0a 319EXPORT_SYMBOL_GPL(nfs_sops);
f7b422b1 320
89d77c8f 321#if IS_ENABLED(CONFIG_NFS_V4)
b72e4f42 322static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *);
d72c727c
BS
323static int nfs4_validate_mount_data(void *options,
324 struct nfs_parsed_mount_data *args, const char *dev_name);
1ae811ee 325
326struct file_system_type nfs4_fs_type = {
327 .owner = THIS_MODULE,
328 .name = "nfs4",
329 .mount = nfs_fs_mount,
330 .kill_sb = nfs_kill_super,
331 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
332};
333EXPORT_SYMBOL_GPL(nfs4_fs_type);
334
335static int __init register_nfs4_fs(void)
336{
337 return register_filesystem(&nfs4_fs_type);
338}
339
340static void unregister_nfs4_fs(void)
341{
342 unregister_filesystem(&nfs4_fs_type);
343}
344#else
345static int __init register_nfs4_fs(void)
346{
347 return 0;
348}
349
350static void unregister_nfs4_fs(void)
351{
352}
f7b422b1
DH
353#endif
354
8e1f936b
RR
355static struct shrinker acl_shrinker = {
356 .shrink = nfs_access_cache_shrinker,
357 .seeks = DEFAULT_SEEKS,
358};
979df72e 359
f7b422b1
DH
360/*
361 * Register the NFS filesystems
362 */
363int __init register_nfs_fs(void)
364{
365 int ret;
366
367 ret = register_filesystem(&nfs_fs_type);
368 if (ret < 0)
369 goto error_0;
370
1ae811ee 371 ret = register_nfs4_fs();
f7b422b1
DH
372 if (ret < 0)
373 goto error_1;
1ae811ee 374
375 ret = nfs_register_sysctl();
376 if (ret < 0)
377 goto error_2;
8e1f936b 378 register_shrinker(&acl_shrinker);
f7b422b1
DH
379 return 0;
380
1ae811ee 381error_2:
382 unregister_nfs4_fs();
f7b422b1
DH
383error_1:
384 unregister_filesystem(&nfs_fs_type);
f7b422b1
DH
385error_0:
386 return ret;
387}
388
389/*
390 * Unregister the NFS filesystems
391 */
392void __exit unregister_nfs_fs(void)
393{
8e1f936b 394 unregister_shrinker(&acl_shrinker);
49af7ee1 395 nfs_unregister_sysctl();
1ae811ee 396 unregister_nfs4_fs();
f7b422b1
DH
397 unregister_filesystem(&nfs_fs_type);
398}
399
1daef0a8 400void nfs_sb_active(struct super_block *sb)
ef818a28 401{
1daef0a8 402 struct nfs_server *server = NFS_SB(sb);
ef818a28 403
1daef0a8
TM
404 if (atomic_inc_return(&server->active) == 1)
405 atomic_inc(&sb->s_active);
ef818a28 406}
89d77c8f 407EXPORT_SYMBOL_GPL(nfs_sb_active);
ef818a28 408
1daef0a8 409void nfs_sb_deactive(struct super_block *sb)
ef818a28
SD
410{
411 struct nfs_server *server = NFS_SB(sb);
1daef0a8
TM
412
413 if (atomic_dec_and_test(&server->active))
414 deactivate_super(sb);
ef818a28 415}
89d77c8f 416EXPORT_SYMBOL_GPL(nfs_sb_deactive);
ef818a28 417
f7b422b1
DH
418/*
419 * Deliver file system statistics to userspace
420 */
fbdefd64 421int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
f7b422b1 422{
0c7d90cf 423 struct nfs_server *server = NFS_SB(dentry->d_sb);
f7b422b1
DH
424 unsigned char blockbits;
425 unsigned long blockres;
0c7d90cf 426 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
ca7e9a0d
TM
427 struct nfs_fsstat res;
428 int error = -ENOMEM;
429
430 res.fattr = nfs_alloc_fattr();
431 if (res.fattr == NULL)
432 goto out_err;
f7b422b1 433
8fa5c000 434 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
fbf3fdd2
MZ
435 if (unlikely(error == -ESTALE)) {
436 struct dentry *pd_dentry;
437
438 pd_dentry = dget_parent(dentry);
439 if (pd_dentry != NULL) {
440 nfs_zap_caches(pd_dentry->d_inode);
441 dput(pd_dentry);
442 }
443 }
ca7e9a0d 444 nfs_free_fattr(res.fattr);
f7b422b1
DH
445 if (error < 0)
446 goto out_err;
ca7e9a0d 447
1a0ba9ae 448 buf->f_type = NFS_SUPER_MAGIC;
f7b422b1
DH
449
450 /*
451 * Current versions of glibc do not correctly handle the
452 * case where f_frsize != f_bsize. Eventually we want to
453 * report the value of wtmult in this field.
454 */
0c7d90cf 455 buf->f_frsize = dentry->d_sb->s_blocksize;
f7b422b1
DH
456
457 /*
458 * On most *nix systems, f_blocks, f_bfree, and f_bavail
459 * are reported in units of f_frsize. Linux hasn't had
460 * an f_frsize field in its statfs struct until recently,
461 * thus historically Linux's sys_statfs reports these
462 * fields in units of f_bsize.
463 */
0c7d90cf
DH
464 buf->f_bsize = dentry->d_sb->s_blocksize;
465 blockbits = dentry->d_sb->s_blocksize_bits;
f7b422b1
DH
466 blockres = (1 << blockbits) - 1;
467 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
468 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
469 buf->f_bavail = (res.abytes + blockres) >> blockbits;
470
471 buf->f_files = res.tfiles;
472 buf->f_ffree = res.afiles;
473
474 buf->f_namelen = server->namelen;
1a0ba9ae 475
f7b422b1
DH
476 return 0;
477
478 out_err:
3110ff80 479 dprintk("%s: statfs error = %d\n", __func__, -error);
1a0ba9ae 480 return error;
f7b422b1 481}
89d77c8f 482EXPORT_SYMBOL_GPL(nfs_statfs);
f7b422b1 483
7d4e2747
DH
484/*
485 * Map the security flavour number to a name
486 */
81039f1f
TM
487static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
488{
7d4e2747 489 static const struct {
81039f1f
TM
490 rpc_authflavor_t flavour;
491 const char *str;
492 } sec_flavours[] = {
493 { RPC_AUTH_NULL, "null" },
494 { RPC_AUTH_UNIX, "sys" },
495 { RPC_AUTH_GSS_KRB5, "krb5" },
496 { RPC_AUTH_GSS_KRB5I, "krb5i" },
497 { RPC_AUTH_GSS_KRB5P, "krb5p" },
498 { RPC_AUTH_GSS_LKEY, "lkey" },
499 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
500 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
501 { RPC_AUTH_GSS_SPKM, "spkm" },
502 { RPC_AUTH_GSS_SPKMI, "spkmi" },
503 { RPC_AUTH_GSS_SPKMP, "spkmp" },
4d81cd16 504 { UINT_MAX, "unknown" }
81039f1f
TM
505 };
506 int i;
507
4d81cd16 508 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
81039f1f
TM
509 if (sec_flavours[i].flavour == flavour)
510 break;
511 }
512 return sec_flavours[i].str;
513}
514
ee671b01
JL
515static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
516 int showdefaults)
517{
518 struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
519
520 seq_printf(m, ",mountproto=");
521 switch (sap->sa_family) {
522 case AF_INET:
523 switch (nfss->mountd_protocol) {
524 case IPPROTO_UDP:
525 seq_printf(m, RPCBIND_NETID_UDP);
526 break;
527 case IPPROTO_TCP:
528 seq_printf(m, RPCBIND_NETID_TCP);
529 break;
530 default:
531 if (showdefaults)
532 seq_printf(m, "auto");
533 }
534 break;
535 case AF_INET6:
536 switch (nfss->mountd_protocol) {
537 case IPPROTO_UDP:
538 seq_printf(m, RPCBIND_NETID_UDP6);
539 break;
540 case IPPROTO_TCP:
541 seq_printf(m, RPCBIND_NETID_TCP6);
542 break;
543 default:
544 if (showdefaults)
545 seq_printf(m, "auto");
546 }
547 break;
548 default:
549 if (showdefaults)
550 seq_printf(m, "auto");
551 }
552}
553
82d101d5
CL
554static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
555 int showdefaults)
556{
557 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
558
d5eff1a3
BS
559 if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
560 return;
561
82d101d5
CL
562 switch (sap->sa_family) {
563 case AF_INET: {
564 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
be859405 565 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
82d101d5
CL
566 break;
567 }
568 case AF_INET6: {
569 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
d250e190 570 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
82d101d5
CL
571 break;
572 }
573 default:
574 if (showdefaults)
575 seq_printf(m, ",mountaddr=unspecified");
576 }
577
578 if (nfss->mountd_version || showdefaults)
579 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
aa699473
SK
580 if ((nfss->mountd_port &&
581 nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
582 showdefaults)
82d101d5
CL
583 seq_printf(m, ",mountport=%u", nfss->mountd_port);
584
ee671b01 585 nfs_show_mountd_netid(m, nfss, showdefaults);
82d101d5
CL
586}
587
89d77c8f 588#if IS_ENABLED(CONFIG_NFS_V4)
0be8189f
TM
589static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
590 int showdefaults)
591{
592 struct nfs_client *clp = nfss->nfs_client;
593
594 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
0be8189f
TM
595}
596#else
597static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
598 int showdefaults)
599{
600}
601#endif
602
7bbceb6f
TM
603static void nfs_show_nfs_version(struct seq_file *m,
604 unsigned int version,
605 unsigned int minorversion)
606{
607 seq_printf(m, ",vers=%u", version);
608 if (version == 4)
609 seq_printf(m, ".%u", minorversion);
610}
611
f7b422b1
DH
612/*
613 * Describe the mount options in force on this server representation
614 */
82d101d5
CL
615static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
616 int showdefaults)
f7b422b1 617{
509de811 618 static const struct proc_nfs_info {
f7b422b1 619 int flag;
509de811
DH
620 const char *str;
621 const char *nostr;
f7b422b1
DH
622 } nfs_info[] = {
623 { NFS_MOUNT_SOFT, ",soft", ",hard" },
82d101d5 624 { NFS_MOUNT_POSIX, ",posix", "" },
f7b422b1
DH
625 { NFS_MOUNT_NOCTO, ",nocto", "" },
626 { NFS_MOUNT_NOAC, ",noac", "" },
627 { NFS_MOUNT_NONLM, ",nolock", "" },
628 { NFS_MOUNT_NOACL, ",noacl", "" },
74dd34e6 629 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
d740351b
CL
630 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
631 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
f7b422b1
DH
632 { 0, NULL, NULL }
633 };
509de811 634 const struct proc_nfs_info *nfs_infop;
8fa5c000 635 struct nfs_client *clp = nfss->nfs_client;
82d101d5 636 u32 version = clp->rpc_ops->version;
7c563cc9 637 int local_flock, local_fcntl;
f7b422b1 638
7bbceb6f 639 nfs_show_nfs_version(m, version, clp->cl_minorversion);
2d767432
CL
640 seq_printf(m, ",rsize=%u", nfss->rsize);
641 seq_printf(m, ",wsize=%u", nfss->wsize);
82d101d5
CL
642 if (nfss->bsize != 0)
643 seq_printf(m, ",bsize=%u", nfss->bsize);
644 seq_printf(m, ",namlen=%u", nfss->namelen);
0e0cab74 645 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
2d767432 646 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
0e0cab74 647 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
2d767432 648 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
0e0cab74 649 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
2d767432 650 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
0e0cab74 651 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
2d767432 652 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
f7b422b1
DH
653 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
654 if (nfss->flags & nfs_infop->flag)
655 seq_puts(m, nfs_infop->str);
656 else
657 seq_puts(m, nfs_infop->nostr);
658 }
2446ab60 659 rcu_read_lock();
56928edd 660 seq_printf(m, ",proto=%s",
ee671b01 661 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
2446ab60 662 rcu_read_unlock();
82d101d5
CL
663 if (version == 4) {
664 if (nfss->port != NFS_PORT)
665 seq_printf(m, ",port=%u", nfss->port);
666 } else
667 if (nfss->port)
668 seq_printf(m, ",port=%u", nfss->port);
669
33170233
TM
670 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
671 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
81039f1f 672 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
82d101d5
CL
673
674 if (version != 4)
675 nfs_show_mountd_options(m, nfss, showdefaults);
0be8189f
TM
676 else
677 nfs_show_nfsv4_options(m, nfss, showdefaults);
82d101d5 678
b797cac7
DH
679 if (nfss->options & NFS_OPTION_FSCACHE)
680 seq_printf(m, ",fsc");
9b00c643 681
89652617
CL
682 if (nfss->options & NFS_OPTION_MIGRATION)
683 seq_printf(m, ",migration");
684
9b00c643
PL
685 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
686 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
687 seq_printf(m, ",lookupcache=none");
688 else
689 seq_printf(m, ",lookupcache=pos");
690 }
7c563cc9
SJ
691
692 local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
693 local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
694
695 if (!local_flock && !local_fcntl)
696 seq_printf(m, ",local_lock=none");
697 else if (local_flock && local_fcntl)
698 seq_printf(m, ",local_lock=all");
699 else if (local_flock)
700 seq_printf(m, ",local_lock=flock");
701 else
702 seq_printf(m, ",local_lock=posix");
f7b422b1
DH
703}
704
705/*
706 * Describe the mount options on this VFS mountpoint
707 */
fbdefd64 708int nfs_show_options(struct seq_file *m, struct dentry *root)
f7b422b1 709{
34c80b1d 710 struct nfs_server *nfss = NFS_SB(root->d_sb);
f7b422b1
DH
711
712 nfs_show_mount_options(m, nfss, 0);
713
2446ab60 714 rcu_read_lock();
5d8515ca
CL
715 seq_printf(m, ",addr=%s",
716 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
717 RPC_DISPLAY_ADDR));
2446ab60 718 rcu_read_unlock();
f7b422b1
DH
719
720 return 0;
721}
89d77c8f 722EXPORT_SYMBOL_GPL(nfs_show_options);
45402c38 723
89d77c8f 724#if IS_ENABLED(CONFIG_NFS_V4)
ae50c0b5 725#ifdef CONFIG_NFS_V4_1
45402c38 726static void show_sessions(struct seq_file *m, struct nfs_server *server)
ae50c0b5
BF
727{
728 if (nfs4_has_session(server->nfs_client))
729 seq_printf(m, ",sessions");
730}
731#else
45402c38
HS
732static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
733#endif
ae50c0b5
BF
734#endif
735
736#ifdef CONFIG_NFS_V4_1
45402c38 737static void show_pnfs(struct seq_file *m, struct nfs_server *server)
ae50c0b5
BF
738{
739 seq_printf(m, ",pnfs=");
740 if (server->pnfs_curr_ld)
741 seq_printf(m, "%s", server->pnfs_curr_ld->name);
742 else
743 seq_printf(m, "not configured");
744}
7e03b7cc
TM
745
746static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
747{
59155546
CL
748 if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
749 struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
7e03b7cc
TM
750 seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
751 "date='%llu,%u'",
752 impl_id->name, impl_id->domain,
753 impl_id->date.seconds, impl_id->date.nseconds);
754 }
755}
45402c38 756#else
89d77c8f 757#if IS_ENABLED(CONFIG_NFS_V4)
7e03b7cc
TM
758static void show_pnfs(struct seq_file *m, struct nfs_server *server)
759{
760}
45402c38 761#endif
7e03b7cc
TM
762static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
763{
764}
45402c38 765#endif
f7b422b1 766
fbdefd64 767int nfs_show_devname(struct seq_file *m, struct dentry *root)
c7f404b4
AV
768{
769 char *page = (char *) __get_free_page(GFP_KERNEL);
770 char *devname, *dummy;
771 int err = 0;
772 if (!page)
773 return -ENOMEM;
d861c630 774 devname = nfs_path(&dummy, root, page, PAGE_SIZE);
c7f404b4
AV
775 if (IS_ERR(devname))
776 err = PTR_ERR(devname);
777 else
778 seq_escape(m, devname, " \t\n\\");
779 free_page((unsigned long)page);
780 return err;
781}
89d77c8f 782EXPORT_SYMBOL_GPL(nfs_show_devname);
c7f404b4 783
fbdefd64 784int nfs_show_path(struct seq_file *m, struct dentry *dentry)
c7f404b4
AV
785{
786 seq_puts(m, "/");
787 return 0;
788}
89d77c8f 789EXPORT_SYMBOL_GPL(nfs_show_path);
c7f404b4 790
f7b422b1
DH
791/*
792 * Present statistical information for this VFS mountpoint
793 */
fbdefd64 794int nfs_show_stats(struct seq_file *m, struct dentry *root)
f7b422b1
DH
795{
796 int i, cpu;
64132379 797 struct nfs_server *nfss = NFS_SB(root->d_sb);
f7b422b1
DH
798 struct rpc_auth *auth = nfss->client->cl_auth;
799 struct nfs_iostats totals = { };
800
801 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
802
803 /*
804 * Display all mount option settings
805 */
806 seq_printf(m, "\n\topts:\t");
64132379
AV
807 seq_puts(m, root->d_sb->s_flags & MS_RDONLY ? "ro" : "rw");
808 seq_puts(m, root->d_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
809 seq_puts(m, root->d_sb->s_flags & MS_NOATIME ? ",noatime" : "");
810 seq_puts(m, root->d_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
f7b422b1
DH
811 nfs_show_mount_options(m, nfss, 1);
812
813 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
814
7e03b7cc 815 show_implementation_id(m, nfss);
7d2ed9ac 816
f7b422b1
DH
817 seq_printf(m, "\n\tcaps:\t");
818 seq_printf(m, "caps=0x%x", nfss->caps);
2d767432
CL
819 seq_printf(m, ",wtmult=%u", nfss->wtmult);
820 seq_printf(m, ",dtsize=%u", nfss->dtsize);
821 seq_printf(m, ",bsize=%u", nfss->bsize);
822 seq_printf(m, ",namlen=%u", nfss->namelen);
f7b422b1 823
89d77c8f 824#if IS_ENABLED(CONFIG_NFS_V4)
40c55319 825 if (nfss->nfs_client->rpc_ops->version == 4) {
f7b422b1
DH
826 seq_printf(m, "\n\tnfsv4:\t");
827 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
828 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
829 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
ae50c0b5
BF
830 show_sessions(m, nfss);
831 show_pnfs(m, nfss);
f7b422b1
DH
832 }
833#endif
834
835 /*
836 * Display security flavor in effect for this mount
837 */
2d767432 838 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
f7b422b1 839 if (auth->au_flavor)
2d767432 840 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
f7b422b1
DH
841
842 /*
843 * Display superblock I/O counters
844 */
845 for_each_possible_cpu(cpu) {
846 struct nfs_iostats *stats;
847
848 preempt_disable();
849 stats = per_cpu_ptr(nfss->io_stats, cpu);
850
851 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
852 totals.events[i] += stats->events[i];
853 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
854 totals.bytes[i] += stats->bytes[i];
6a51091d
DH
855#ifdef CONFIG_NFS_FSCACHE
856 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
857 totals.fscache[i] += stats->fscache[i];
858#endif
f7b422b1
DH
859
860 preempt_enable();
861 }
862
863 seq_printf(m, "\n\tevents:\t");
864 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
865 seq_printf(m, "%lu ", totals.events[i]);
866 seq_printf(m, "\n\tbytes:\t");
867 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
868 seq_printf(m, "%Lu ", totals.bytes[i]);
6a51091d
DH
869#ifdef CONFIG_NFS_FSCACHE
870 if (nfss->options & NFS_OPTION_FSCACHE) {
871 seq_printf(m, "\n\tfsc:\t");
872 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
873 seq_printf(m, "%Lu ", totals.bytes[i]);
874 }
875#endif
f7b422b1
DH
876 seq_printf(m, "\n");
877
878 rpc_print_iostats(m, nfss->client);
879
880 return 0;
881}
89d77c8f 882EXPORT_SYMBOL_GPL(nfs_show_stats);
f7b422b1
DH
883
884/*
885 * Begin unmount by attempting to remove all automounted mountpoints we added
54ceac45 886 * in response to xdev traversals and referrals
f7b422b1 887 */
fbdefd64 888void nfs_umount_begin(struct super_block *sb)
f7b422b1 889{
67e55205 890 struct nfs_server *server;
fc6ae3cf
TM
891 struct rpc_clnt *rpc;
892
67e55205 893 server = NFS_SB(sb);
fc6ae3cf
TM
894 /* -EIO all pending I/O */
895 rpc = server->client_acl;
896 if (!IS_ERR(rpc))
897 rpc_killall_tasks(rpc);
898 rpc = server->client;
899 if (!IS_ERR(rpc))
900 rpc_killall_tasks(rpc);
f7b422b1 901}
89d77c8f 902EXPORT_SYMBOL_GPL(nfs_umount_begin);
f7b422b1 903
db833351 904static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(void)
9423a08a
CL
905{
906 struct nfs_parsed_mount_data *data;
907
908 data = kzalloc(sizeof(*data), GFP_KERNEL);
909 if (data) {
9423a08a
CL
910 data->acregmin = NFS_DEF_ACREGMIN;
911 data->acregmax = NFS_DEF_ACREGMAX;
912 data->acdirmin = NFS_DEF_ACDIRMIN;
913 data->acdirmax = NFS_DEF_ACDIRMAX;
c5811dbd 914 data->mount_server.port = NFS_UNSPEC_PORT;
9423a08a 915 data->nfs_server.port = NFS_UNSPEC_PORT;
c5811dbd 916 data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
9423a08a
CL
917 data->auth_flavors[0] = RPC_AUTH_UNIX;
918 data->auth_flavor_len = 1;
919 data->minorversion = 0;
b72e4f42 920 data->need_mount = true;
9937347a 921 data->net = current->nsproxy->net_ns;
8a0d551a 922 security_init_mnt_opts(&data->lsm_opts);
9423a08a
CL
923 }
924 return data;
925}
926
8a0d551a
JL
927static void nfs_free_parsed_mount_data(struct nfs_parsed_mount_data *data)
928{
929 if (data) {
930 kfree(data->client_address);
931 kfree(data->mount_server.hostname);
932 kfree(data->nfs_server.export_path);
933 kfree(data->nfs_server.hostname);
934 kfree(data->fscache_uniq);
935 security_free_mnt_opts(&data->lsm_opts);
936 kfree(data);
937 }
938}
939
fc50d58f 940/*
cdcd7f9a
CL
941 * Sanity-check a server address provided by the mount command.
942 *
943 * Address family must be initialized, and address must not be
944 * the ANY address for that family.
fc50d58f
CL
945 */
946static int nfs_verify_server_address(struct sockaddr *addr)
947{
948 switch (addr->sa_family) {
949 case AF_INET: {
cdcd7f9a 950 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
e6f1cebf 951 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
cdcd7f9a
CL
952 }
953 case AF_INET6: {
954 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
955 return !ipv6_addr_any(sa);
fc50d58f
CL
956 }
957 }
958
53a0b9c4 959 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
fc50d58f
CL
960 return 0;
961}
962
4cfd74fc
CL
963/*
964 * Select between a default port value and a user-specified port value.
965 * If a zero value is set, then autobind will be used.
966 */
f5855fec 967static void nfs_set_port(struct sockaddr *sap, int *port,
4cfd74fc
CL
968 const unsigned short default_port)
969{
f5855fec
TM
970 if (*port == NFS_UNSPEC_PORT)
971 *port = default_port;
4cfd74fc 972
f5855fec 973 rpc_set_port(sap, *port);
4cfd74fc
CL
974}
975
259875ef
TM
976/*
977 * Sanity check the NFS transport protocol.
978 *
979 */
980static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
981{
982 switch (mnt->nfs_server.protocol) {
983 case XPRT_TRANSPORT_UDP:
984 case XPRT_TRANSPORT_TCP:
985 case XPRT_TRANSPORT_RDMA:
986 break;
987 default:
988 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
989 }
990}
991
992/*
993 * For text based NFSv2/v3 mounts, the mount protocol transport default
994 * settings should depend upon the specified NFS transport.
995 */
996static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
997{
998 nfs_validate_transport_protocol(mnt);
999
1000 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
1001 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
1002 return;
1003 switch (mnt->nfs_server.protocol) {
1004 case XPRT_TRANSPORT_UDP:
1005 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1006 break;
1007 case XPRT_TRANSPORT_TCP:
1008 case XPRT_TRANSPORT_RDMA:
1009 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1010 }
1011}
1012
01060c89
CL
1013/*
1014 * Parse the value of the 'sec=' option.
01060c89
CL
1015 */
1016static int nfs_parse_security_flavors(char *value,
1017 struct nfs_parsed_mount_data *mnt)
1018{
1019 substring_t args[MAX_OPT_ARGS];
1020
1021 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
1022
1023 switch (match_token(value, nfs_secflavor_tokens, args)) {
1024 case Opt_sec_none:
01060c89
CL
1025 mnt->auth_flavors[0] = RPC_AUTH_NULL;
1026 break;
1027 case Opt_sec_sys:
01060c89
CL
1028 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
1029 break;
1030 case Opt_sec_krb5:
01060c89
CL
1031 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
1032 break;
1033 case Opt_sec_krb5i:
01060c89
CL
1034 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
1035 break;
1036 case Opt_sec_krb5p:
01060c89
CL
1037 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
1038 break;
1039 case Opt_sec_lkey:
01060c89
CL
1040 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
1041 break;
1042 case Opt_sec_lkeyi:
01060c89
CL
1043 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
1044 break;
1045 case Opt_sec_lkeyp:
01060c89
CL
1046 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
1047 break;
1048 case Opt_sec_spkm:
01060c89
CL
1049 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
1050 break;
1051 case Opt_sec_spkmi:
01060c89
CL
1052 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
1053 break;
1054 case Opt_sec_spkmp:
01060c89
CL
1055 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
1056 break;
1057 default:
1058 return 0;
1059 }
1060
9b7160c5 1061 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
059f90b3 1062 mnt->auth_flavor_len = 1;
01060c89
CL
1063 return 1;
1064}
1065
0d71b058
TM
1066static int nfs_parse_version_string(char *string,
1067 struct nfs_parsed_mount_data *mnt,
1068 substring_t *args)
1069{
1070 mnt->flags &= ~NFS_MOUNT_VER3;
1071 switch (match_token(string, nfs_vers_tokens, args)) {
1072 case Opt_vers_2:
1073 mnt->version = 2;
1074 break;
1075 case Opt_vers_3:
1076 mnt->flags |= NFS_MOUNT_VER3;
1077 mnt->version = 3;
1078 break;
1079 case Opt_vers_4:
1080 /* Backward compatibility option. In future,
1081 * the mount program should always supply
1082 * a NFSv4 minor version number.
1083 */
1084 mnt->version = 4;
1085 break;
1086 case Opt_vers_4_0:
1087 mnt->version = 4;
1088 mnt->minorversion = 0;
1089 break;
1090 case Opt_vers_4_1:
1091 mnt->version = 4;
1092 mnt->minorversion = 1;
1093 break;
1094 default:
1095 return 0;
1096 }
1097 return 1;
1098}
1099
c5cb09b6
RL
1100static int nfs_get_option_str(substring_t args[], char **option)
1101{
1102 kfree(*option);
1103 *option = match_strdup(args);
1104 return !option;
1105}
1106
1107static int nfs_get_option_ul(substring_t args[], unsigned long *option)
1108{
1109 int rc;
1110 char *string;
1111
1112 string = match_strdup(args);
1113 if (string == NULL)
1114 return -ENOMEM;
7297cb68 1115 rc = kstrtoul(string, 10, option);
c5cb09b6
RL
1116 kfree(string);
1117
1118 return rc;
1119}
1120
bf0fd768
CL
1121/*
1122 * Error-check and convert a string of mount options from user space into
f45663ce
CL
1123 * a data structure. The whole mount string is processed; bad options are
1124 * skipped as they are encountered. If there were no errors, return 1;
1125 * otherwise return 0 (zero).
bf0fd768
CL
1126 */
1127static int nfs_parse_mount_options(char *raw,
1128 struct nfs_parsed_mount_data *mnt)
1129{
f9c3a380 1130 char *p, *string, *secdata;
d23c45fd 1131 int rc, sloppy = 0, invalid_option = 0;
ee671b01
JL
1132 unsigned short protofamily = AF_UNSPEC;
1133 unsigned short mountfamily = AF_UNSPEC;
bf0fd768
CL
1134
1135 if (!raw) {
1136 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
1137 return 1;
1138 }
1139 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
1140
f9c3a380
EP
1141 secdata = alloc_secdata();
1142 if (!secdata)
1143 goto out_nomem;
1144
1145 rc = security_sb_copy_data(raw, secdata);
1146 if (rc)
1147 goto out_security_failure;
1148
1149 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
1150 if (rc)
1151 goto out_security_failure;
1152
1153 free_secdata(secdata);
1154
bf0fd768
CL
1155 while ((p = strsep(&raw, ",")) != NULL) {
1156 substring_t args[MAX_OPT_ARGS];
a5a16bae
CL
1157 unsigned long option;
1158 int token;
bf0fd768
CL
1159
1160 if (!*p)
1161 continue;
1162
1163 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
1164
1165 token = match_token(p, nfs_mount_option_tokens, args);
1166 switch (token) {
f45663ce
CL
1167
1168 /*
1169 * boolean options: foo/nofoo
1170 */
bf0fd768
CL
1171 case Opt_soft:
1172 mnt->flags |= NFS_MOUNT_SOFT;
1173 break;
1174 case Opt_hard:
1175 mnt->flags &= ~NFS_MOUNT_SOFT;
1176 break;
bf0fd768
CL
1177 case Opt_posix:
1178 mnt->flags |= NFS_MOUNT_POSIX;
1179 break;
1180 case Opt_noposix:
1181 mnt->flags &= ~NFS_MOUNT_POSIX;
1182 break;
1183 case Opt_cto:
1184 mnt->flags &= ~NFS_MOUNT_NOCTO;
1185 break;
1186 case Opt_nocto:
1187 mnt->flags |= NFS_MOUNT_NOCTO;
1188 break;
1189 case Opt_ac:
1190 mnt->flags &= ~NFS_MOUNT_NOAC;
1191 break;
1192 case Opt_noac:
1193 mnt->flags |= NFS_MOUNT_NOAC;
1194 break;
1195 case Opt_lock:
1196 mnt->flags &= ~NFS_MOUNT_NONLM;
5eebde23
SJ
1197 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1198 NFS_MOUNT_LOCAL_FCNTL);
bf0fd768
CL
1199 break;
1200 case Opt_nolock:
1201 mnt->flags |= NFS_MOUNT_NONLM;
5eebde23
SJ
1202 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1203 NFS_MOUNT_LOCAL_FCNTL);
bf0fd768 1204 break;
bf0fd768
CL
1205 case Opt_udp:
1206 mnt->flags &= ~NFS_MOUNT_TCP;
0896a725 1207 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
bf0fd768
CL
1208 break;
1209 case Opt_tcp:
1210 mnt->flags |= NFS_MOUNT_TCP;
0896a725 1211 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
bf0fd768 1212 break;
2cf7ff7a
TT
1213 case Opt_rdma:
1214 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1215 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
a67d18f8 1216 xprt_load_transport(p);
2cf7ff7a 1217 break;
bf0fd768
CL
1218 case Opt_acl:
1219 mnt->flags &= ~NFS_MOUNT_NOACL;
1220 break;
1221 case Opt_noacl:
1222 mnt->flags |= NFS_MOUNT_NOACL;
1223 break;
1224 case Opt_rdirplus:
1225 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1226 break;
1227 case Opt_nordirplus:
1228 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1229 break;
75180df2
TM
1230 case Opt_sharecache:
1231 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1232 break;
1233 case Opt_nosharecache:
1234 mnt->flags |= NFS_MOUNT_UNSHARED;
1235 break;
d740351b
CL
1236 case Opt_resvport:
1237 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1238 break;
1239 case Opt_noresvport:
1240 mnt->flags |= NFS_MOUNT_NORESVPORT;
1241 break;
b797cac7
DH
1242 case Opt_fscache:
1243 mnt->options |= NFS_OPTION_FSCACHE;
1244 kfree(mnt->fscache_uniq);
1245 mnt->fscache_uniq = NULL;
1246 break;
1247 case Opt_nofscache:
1248 mnt->options &= ~NFS_OPTION_FSCACHE;
1249 kfree(mnt->fscache_uniq);
1250 mnt->fscache_uniq = NULL;
1251 break;
89652617
CL
1252 case Opt_migration:
1253 mnt->options |= NFS_OPTION_MIGRATION;
1254 break;
1255 case Opt_nomigration:
1256 mnt->options &= NFS_OPTION_MIGRATION;
1257 break;
bf0fd768 1258
f45663ce
CL
1259 /*
1260 * options that take numeric values
1261 */
bf0fd768 1262 case Opt_port:
c5cb09b6
RL
1263 if (nfs_get_option_ul(args, &option) ||
1264 option > USHRT_MAX)
d23c45fd
CL
1265 goto out_invalid_value;
1266 mnt->nfs_server.port = option;
bf0fd768
CL
1267 break;
1268 case Opt_rsize:
c5cb09b6 1269 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1270 goto out_invalid_value;
1271 mnt->rsize = option;
bf0fd768
CL
1272 break;
1273 case Opt_wsize:
c5cb09b6 1274 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1275 goto out_invalid_value;
1276 mnt->wsize = option;
bf0fd768
CL
1277 break;
1278 case Opt_bsize:
c5cb09b6 1279 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1280 goto out_invalid_value;
1281 mnt->bsize = option;
bf0fd768
CL
1282 break;
1283 case Opt_timeo:
c5cb09b6 1284 if (nfs_get_option_ul(args, &option) || option == 0)
d23c45fd
CL
1285 goto out_invalid_value;
1286 mnt->timeo = option;
bf0fd768
CL
1287 break;
1288 case Opt_retrans:
c5cb09b6 1289 if (nfs_get_option_ul(args, &option) || option == 0)
d23c45fd
CL
1290 goto out_invalid_value;
1291 mnt->retrans = option;
bf0fd768
CL
1292 break;
1293 case Opt_acregmin:
c5cb09b6 1294 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1295 goto out_invalid_value;
1296 mnt->acregmin = option;
bf0fd768
CL
1297 break;
1298 case Opt_acregmax:
c5cb09b6 1299 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1300 goto out_invalid_value;
1301 mnt->acregmax = option;
bf0fd768
CL
1302 break;
1303 case Opt_acdirmin:
c5cb09b6 1304 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1305 goto out_invalid_value;
1306 mnt->acdirmin = option;
bf0fd768
CL
1307 break;
1308 case Opt_acdirmax:
c5cb09b6 1309 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1310 goto out_invalid_value;
1311 mnt->acdirmax = option;
bf0fd768
CL
1312 break;
1313 case Opt_actimeo:
c5cb09b6 1314 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1315 goto out_invalid_value;
1316 mnt->acregmin = mnt->acregmax =
1317 mnt->acdirmin = mnt->acdirmax = option;
bf0fd768
CL
1318 break;
1319 case Opt_namelen:
c5cb09b6 1320 if (nfs_get_option_ul(args, &option))
d23c45fd
CL
1321 goto out_invalid_value;
1322 mnt->namlen = option;
bf0fd768
CL
1323 break;
1324 case Opt_mountport:
c5cb09b6
RL
1325 if (nfs_get_option_ul(args, &option) ||
1326 option > USHRT_MAX)
d23c45fd
CL
1327 goto out_invalid_value;
1328 mnt->mount_server.port = option;
bf0fd768 1329 break;
bf0fd768 1330 case Opt_mountvers:
c5cb09b6 1331 if (nfs_get_option_ul(args, &option) ||
f45663ce 1332 option < NFS_MNT_VERSION ||
d23c45fd
CL
1333 option > NFS_MNT3_VERSION)
1334 goto out_invalid_value;
1335 mnt->mount_server.version = option;
bf0fd768 1336 break;
3fd5be9e 1337 case Opt_minorversion:
c5cb09b6 1338 if (nfs_get_option_ul(args, &option))
f3f4f4ed
CL
1339 goto out_invalid_value;
1340 if (option > NFS4_MAX_MINOR_VERSION)
1341 goto out_invalid_value;
1342 mnt->minorversion = option;
3fd5be9e 1343 break;
bf0fd768 1344
f45663ce
CL
1345 /*
1346 * options that take text values
1347 */
0d71b058
TM
1348 case Opt_nfsvers:
1349 string = match_strdup(args);
1350 if (string == NULL)
1351 goto out_nomem;
1352 rc = nfs_parse_version_string(string, mnt, args);
1353 kfree(string);
1354 if (!rc)
1355 goto out_invalid_value;
1356 break;
bf0fd768
CL
1357 case Opt_sec:
1358 string = match_strdup(args);
1359 if (string == NULL)
1360 goto out_nomem;
01060c89 1361 rc = nfs_parse_security_flavors(string, mnt);
bf0fd768 1362 kfree(string);
f45663ce 1363 if (!rc) {
f45663ce
CL
1364 dfprintk(MOUNT, "NFS: unrecognized "
1365 "security flavor\n");
d23c45fd 1366 return 0;
f45663ce 1367 }
bf0fd768
CL
1368 break;
1369 case Opt_proto:
1370 string = match_strdup(args);
1371 if (string == NULL)
1372 goto out_nomem;
1373 token = match_token(string,
1374 nfs_xprt_protocol_tokens, args);
bf0fd768 1375
ee671b01 1376 protofamily = AF_INET;
bf0fd768 1377 switch (token) {
ee671b01
JL
1378 case Opt_xprt_udp6:
1379 protofamily = AF_INET6;
fdb66ff4 1380 case Opt_xprt_udp:
bf0fd768 1381 mnt->flags &= ~NFS_MOUNT_TCP;
0896a725 1382 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
bf0fd768 1383 break;
ee671b01
JL
1384 case Opt_xprt_tcp6:
1385 protofamily = AF_INET6;
fdb66ff4 1386 case Opt_xprt_tcp:
bf0fd768 1387 mnt->flags |= NFS_MOUNT_TCP;
0896a725 1388 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
bf0fd768 1389 break;
2cf7ff7a
TT
1390 case Opt_xprt_rdma:
1391 /* vector side protocols to TCP */
1392 mnt->flags |= NFS_MOUNT_TCP;
1393 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
a67d18f8 1394 xprt_load_transport(string);
2cf7ff7a 1395 break;
bf0fd768 1396 default:
f45663ce
CL
1397 dfprintk(MOUNT, "NFS: unrecognized "
1398 "transport protocol\n");
4223a4a1 1399 kfree(string);
d23c45fd 1400 return 0;
bf0fd768 1401 }
c5cb09b6 1402 kfree(string);
bf0fd768
CL
1403 break;
1404 case Opt_mountproto:
1405 string = match_strdup(args);
1406 if (string == NULL)
1407 goto out_nomem;
1408 token = match_token(string,
1409 nfs_xprt_protocol_tokens, args);
d508afb4 1410 kfree(string);
bf0fd768 1411
ee671b01 1412 mountfamily = AF_INET;
bf0fd768 1413 switch (token) {
ee671b01
JL
1414 case Opt_xprt_udp6:
1415 mountfamily = AF_INET6;
fdb66ff4 1416 case Opt_xprt_udp:
0896a725 1417 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
bf0fd768 1418 break;
ee671b01
JL
1419 case Opt_xprt_tcp6:
1420 mountfamily = AF_INET6;
fdb66ff4 1421 case Opt_xprt_tcp:
0896a725 1422 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
bf0fd768 1423 break;
2cf7ff7a 1424 case Opt_xprt_rdma: /* not used for side protocols */
bf0fd768 1425 default:
f45663ce
CL
1426 dfprintk(MOUNT, "NFS: unrecognized "
1427 "transport protocol\n");
d23c45fd 1428 return 0;
bf0fd768
CL
1429 }
1430 break;
1431 case Opt_addr:
1432 string = match_strdup(args);
1433 if (string == NULL)
1434 goto out_nomem;
53a0b9c4 1435 mnt->nfs_server.addrlen =
b48e1278 1436 rpc_pton(mnt->net, string, strlen(string),
53a0b9c4
CL
1437 (struct sockaddr *)
1438 &mnt->nfs_server.address,
1439 sizeof(mnt->nfs_server.address));
bf0fd768 1440 kfree(string);
53a0b9c4
CL
1441 if (mnt->nfs_server.addrlen == 0)
1442 goto out_invalid_address;
bf0fd768
CL
1443 break;
1444 case Opt_clientaddr:
c5cb09b6 1445 if (nfs_get_option_str(args, &mnt->client_address))
bf0fd768 1446 goto out_nomem;
bf0fd768 1447 break;
33832034 1448 case Opt_mounthost:
c5cb09b6
RL
1449 if (nfs_get_option_str(args,
1450 &mnt->mount_server.hostname))
33832034 1451 goto out_nomem;
33832034 1452 break;
0ac83779 1453 case Opt_mountaddr:
bf0fd768
CL
1454 string = match_strdup(args);
1455 if (string == NULL)
1456 goto out_nomem;
53a0b9c4 1457 mnt->mount_server.addrlen =
b48e1278 1458 rpc_pton(mnt->net, string, strlen(string),
53a0b9c4
CL
1459 (struct sockaddr *)
1460 &mnt->mount_server.address,
1461 sizeof(mnt->mount_server.address));
bf0fd768 1462 kfree(string);
53a0b9c4
CL
1463 if (mnt->mount_server.addrlen == 0)
1464 goto out_invalid_address;
bf0fd768 1465 break;
7973c1f1
TM
1466 case Opt_lookupcache:
1467 string = match_strdup(args);
1468 if (string == NULL)
1469 goto out_nomem;
1470 token = match_token(string,
1471 nfs_lookupcache_tokens, args);
1472 kfree(string);
1473 switch (token) {
1474 case Opt_lookupcache_all:
1475 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1476 break;
1477 case Opt_lookupcache_positive:
1478 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1479 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1480 break;
1481 case Opt_lookupcache_none:
1482 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1483 break;
1484 default:
7973c1f1
TM
1485 dfprintk(MOUNT, "NFS: invalid "
1486 "lookupcache argument\n");
d23c45fd 1487 return 0;
7973c1f1
TM
1488 };
1489 break;
a6d5ff64 1490 case Opt_fscache_uniq:
c5cb09b6 1491 if (nfs_get_option_str(args, &mnt->fscache_uniq))
a6d5ff64 1492 goto out_nomem;
a6d5ff64
CL
1493 mnt->options |= NFS_OPTION_FSCACHE;
1494 break;
5eebde23
SJ
1495 case Opt_local_lock:
1496 string = match_strdup(args);
1497 if (string == NULL)
1498 goto out_nomem;
1499 token = match_token(string, nfs_local_lock_tokens,
1500 args);
1501 kfree(string);
1502 switch (token) {
1503 case Opt_local_lock_all:
1504 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1505 NFS_MOUNT_LOCAL_FCNTL);
1506 break;
1507 case Opt_local_lock_flock:
1508 mnt->flags |= NFS_MOUNT_LOCAL_FLOCK;
1509 break;
1510 case Opt_local_lock_posix:
1511 mnt->flags |= NFS_MOUNT_LOCAL_FCNTL;
1512 break;
1513 case Opt_local_lock_none:
1514 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1515 NFS_MOUNT_LOCAL_FCNTL);
1516 break;
1517 default:
1518 dfprintk(MOUNT, "NFS: invalid "
1519 "local_lock argument\n");
1520 return 0;
1521 };
1522 break;
bf0fd768 1523
f45663ce
CL
1524 /*
1525 * Special options
1526 */
1527 case Opt_sloppy:
1528 sloppy = 1;
1529 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1530 break;
bf0fd768
CL
1531 case Opt_userspace:
1532 case Opt_deprecated:
d33e4dfe
CL
1533 dfprintk(MOUNT, "NFS: ignoring mount option "
1534 "'%s'\n", p);
bf0fd768
CL
1535 break;
1536
1537 default:
d23c45fd 1538 invalid_option = 1;
f45663ce
CL
1539 dfprintk(MOUNT, "NFS: unrecognized mount option "
1540 "'%s'\n", p);
bf0fd768
CL
1541 }
1542 }
1543
d23c45fd
CL
1544 if (!sloppy && invalid_option)
1545 return 0;
1546
571b7554
WAA
1547 if (mnt->minorversion && mnt->version != 4)
1548 goto out_minorversion_mismatch;
1549
89652617
CL
1550 if (mnt->options & NFS_OPTION_MIGRATION &&
1551 mnt->version != 4 && mnt->minorversion != 0)
1552 goto out_migration_misuse;
1553
ee671b01
JL
1554 /*
1555 * verify that any proto=/mountproto= options match the address
1856b225 1556 * families in the addr=/mountaddr= options.
ee671b01
JL
1557 */
1558 if (protofamily != AF_UNSPEC &&
1559 protofamily != mnt->nfs_server.address.ss_family)
1560 goto out_proto_mismatch;
1561
1562 if (mountfamily != AF_UNSPEC) {
1563 if (mnt->mount_server.addrlen) {
1564 if (mountfamily != mnt->mount_server.address.ss_family)
1565 goto out_mountproto_mismatch;
1566 } else {
1567 if (mountfamily != mnt->nfs_server.address.ss_family)
1568 goto out_mountproto_mismatch;
1569 }
1570 }
1571
bf0fd768
CL
1572 return 1;
1573
ee671b01
JL
1574out_mountproto_mismatch:
1575 printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1576 "option\n");
1577 return 0;
1578out_proto_mismatch:
1579 printk(KERN_INFO "NFS: server address does not match proto= option\n");
1580 return 0;
53a0b9c4
CL
1581out_invalid_address:
1582 printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1583 return 0;
d23c45fd 1584out_invalid_value:
53a0b9c4 1585 printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
d23c45fd 1586 return 0;
571b7554
WAA
1587out_minorversion_mismatch:
1588 printk(KERN_INFO "NFS: mount option vers=%u does not support "
1589 "minorversion=%u\n", mnt->version, mnt->minorversion);
1590 return 0;
89652617
CL
1591out_migration_misuse:
1592 printk(KERN_INFO
1593 "NFS: 'migration' not supported for this NFS version\n");
1594 return 0;
bf0fd768
CL
1595out_nomem:
1596 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1597 return 0;
f9c3a380
EP
1598out_security_failure:
1599 free_secdata(secdata);
1600 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1601 return 0;
bf0fd768
CL
1602}
1603
ec88f28d
CL
1604/*
1605 * Match the requested auth flavors with the list returned by
1606 * the server. Returns zero and sets the mount's authentication
1607 * flavor on success; returns -EACCES if server does not support
1608 * the requested flavor.
1609 */
1610static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1611 struct nfs_mount_request *request)
1612{
1613 unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1614
5eecfde6
CL
1615 /*
1616 * Certain releases of Linux's mountd return an empty
1617 * flavor list. To prevent behavioral regression with
1618 * these servers (ie. rejecting mounts that used to
1619 * succeed), revert to pre-2.6.32 behavior (no checking)
1620 * if the returned flavor list is empty.
1621 */
1622 if (server_authlist_len == 0)
1623 return 0;
1624
ec88f28d
CL
1625 /*
1626 * We avoid sophisticated negotiating here, as there are
1627 * plenty of cases where we can get it wrong, providing
1628 * either too little or too much security.
1629 *
1630 * RFC 2623, section 2.7 suggests we SHOULD prefer the
1631 * flavor listed first. However, some servers list
1632 * AUTH_NULL first. Our caller plants AUTH_SYS, the
1633 * preferred default, in args->auth_flavors[0] if user
1634 * didn't specify sec= mount option.
1635 */
1636 for (i = 0; i < args->auth_flavor_len; i++)
1637 for (j = 0; j < server_authlist_len; j++)
1638 if (args->auth_flavors[i] == request->auth_flavs[j]) {
1639 dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1640 request->auth_flavs[j]);
1641 args->auth_flavors[0] = request->auth_flavs[j];
1642 return 0;
1643 }
1644
1645 dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1646 nfs_umount(request);
1647 return -EACCES;
1648}
1649
0076d7b7
CL
1650/*
1651 * Use the remote server's MOUNT service to request the NFS file handle
1652 * corresponding to the provided path.
1653 */
486aa699
BS
1654static int nfs_request_mount(struct nfs_parsed_mount_data *args,
1655 struct nfs_fh *root_fh)
0076d7b7 1656{
ec88f28d
CL
1657 rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1658 unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
c5d120f8
CL
1659 struct nfs_mount_request request = {
1660 .sap = (struct sockaddr *)
1661 &args->mount_server.address,
1662 .dirpath = args->nfs_server.export_path,
1663 .protocol = args->mount_server.protocol,
1664 .fh = root_fh,
50a737f8 1665 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
ec88f28d
CL
1666 .auth_flav_len = &server_authlist_len,
1667 .auth_flavs = server_authlist,
6d59b8d5 1668 .net = args->net,
c5d120f8 1669 };
4c568017 1670 int status;
0076d7b7
CL
1671
1672 if (args->mount_server.version == 0) {
8a6e5deb
TM
1673 switch (args->version) {
1674 default:
1675 args->mount_server.version = NFS_MNT3_VERSION;
1676 break;
1677 case 2:
1678 args->mount_server.version = NFS_MNT_VERSION;
1679 }
0076d7b7 1680 }
c5d120f8 1681 request.version = args->mount_server.version;
0076d7b7 1682
33832034 1683 if (args->mount_server.hostname)
c5d120f8 1684 request.hostname = args->mount_server.hostname;
33832034 1685 else
c5d120f8 1686 request.hostname = args->nfs_server.hostname;
33832034 1687
0076d7b7
CL
1688 /*
1689 * Construct the mount server's address.
1690 */
4c568017 1691 if (args->mount_server.address.ss_family == AF_UNSPEC) {
c5d120f8 1692 memcpy(request.sap, &args->nfs_server.address,
4c568017
CL
1693 args->nfs_server.addrlen);
1694 args->mount_server.addrlen = args->nfs_server.addrlen;
1695 }
c5d120f8 1696 request.salen = args->mount_server.addrlen;
f5855fec 1697 nfs_set_port(request.sap, &args->mount_server.port, 0);
0076d7b7
CL
1698
1699 /*
1700 * Now ask the mount server to map our export path
1701 * to a file handle.
1702 */
c5d120f8 1703 status = nfs_mount(&request);
ec88f28d
CL
1704 if (status != 0) {
1705 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1706 request.hostname, status);
1707 return status;
1708 }
0076d7b7 1709
ec88f28d
CL
1710 /*
1711 * MNTv1 (NFSv2) does not support auth flavor negotiation.
1712 */
1713 if (args->mount_server.version != NFS_MNT3_VERSION)
1714 return 0;
1715 return nfs_walk_authlist(args, &request);
0076d7b7
CL
1716}
1717
ff9099f2
BS
1718struct dentry *nfs_try_mount(int flags, const char *dev_name,
1719 struct nfs_mount_info *mount_info,
1720 struct nfs_subversion *nfs_mod)
486aa699
BS
1721{
1722 int status;
1723 struct nfs_server *server;
1724
b72e4f42 1725 if (mount_info->parsed->need_mount) {
87c7083d 1726 status = nfs_request_mount(mount_info->parsed, mount_info->mntfh);
b72e4f42
BS
1727 if (status)
1728 return ERR_PTR(status);
1729 }
486aa699
BS
1730
1731 /* Get a volume representation */
1179acc6 1732 server = nfs_mod->rpc_ops->create_server(mount_info, nfs_mod);
486aa699
BS
1733 if (IS_ERR(server))
1734 return ERR_CAST(server);
1735
ab7017a3 1736 return nfs_fs_mount_common(server, flags, dev_name, mount_info, nfs_mod);
486aa699 1737}
ddda8e0a 1738EXPORT_SYMBOL_GPL(nfs_try_mount);
486aa699 1739
c12bacec
RL
1740/*
1741 * Split "dev_name" into "hostname:export_path".
1742 *
1743 * The leftmost colon demarks the split between the server's hostname
1744 * and the export path. If the hostname starts with a left square
1745 * bracket, then it may contain colons.
1746 *
1747 * Note: caller frees hostname and export path, even on error.
1748 */
1749static int nfs_parse_devname(const char *dev_name,
1750 char **hostname, size_t maxnamlen,
1751 char **export_path, size_t maxpathlen)
dc045898
CL
1752{
1753 size_t len;
c12bacec 1754 char *end;
dc045898 1755
c12bacec
RL
1756 /* Is the host name protected with square brakcets? */
1757 if (*dev_name == '[') {
1758 end = strchr(++dev_name, ']');
1759 if (end == NULL || end[1] != ':')
dc045898 1760 goto out_bad_devname;
dc045898 1761
c12bacec
RL
1762 len = end - dev_name;
1763 end++;
1764 } else {
1765 char *comma;
dc045898 1766
c12bacec
RL
1767 end = strchr(dev_name, ':');
1768 if (end == NULL)
1769 goto out_bad_devname;
1770 len = end - dev_name;
d1aa0825 1771
c12bacec
RL
1772 /* kill possible hostname list: not supported */
1773 comma = strchr(dev_name, ',');
1774 if (comma != NULL && comma < end)
1775 *comma = 0;
1776 }
d1aa0825 1777
d1aa0825
CL
1778 if (len > maxnamlen)
1779 goto out_hostname;
1780
1781 /* N.B. caller will free nfs_server.hostname in all cases */
c12bacec 1782 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
d1aa0825
CL
1783 if (*hostname == NULL)
1784 goto out_nomem;
c12bacec 1785 len = strlen(++end);
d1aa0825
CL
1786 if (len > maxpathlen)
1787 goto out_path;
1788 *export_path = kstrndup(end, len, GFP_KERNEL);
1789 if (!*export_path)
1790 goto out_nomem;
1791
c12bacec 1792 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
d1aa0825
CL
1793 return 0;
1794
1795out_bad_devname:
1796 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1797 return -EINVAL;
1798
1799out_nomem:
1800 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1801 return -ENOMEM;
1802
1803out_hostname:
1804 dfprintk(MOUNT, "NFS: server hostname too long\n");
1805 return -ENAMETOOLONG;
1806
1807out_path:
1808 dfprintk(MOUNT, "NFS: export pathname too long\n");
1809 return -ENAMETOOLONG;
1810}
1811
f7b422b1 1812/*
54ceac45
DH
1813 * Validate the NFS2/NFS3 mount data
1814 * - fills in the mount root filehandle
136d558c
CL
1815 *
1816 * For option strings, user space handles the following behaviors:
1817 *
1818 * + DNS: mapping server host name to IP address ("addr=" option)
1819 *
1820 * + failure mode: how to behave if a mount request can't be handled
1821 * immediately ("fg/bg" option)
1822 *
1823 * + retry: how often to retry a mount request ("retry=" option)
1824 *
1825 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1826 * mountproto=tcp after mountproto=udp, and so on
f7b422b1 1827 */
d72c727c
BS
1828static int nfs23_validate_mount_data(void *options,
1829 struct nfs_parsed_mount_data *args,
1830 struct nfs_fh *mntfh,
1831 const char *dev_name)
f7b422b1 1832{
2283f8d6 1833 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
4cfd74fc 1834 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
136d558c 1835
5df36e78
CL
1836 if (data == NULL)
1837 goto out_no_data;
f7b422b1 1838
c5afc8da 1839 args->version = NFS_DEFAULT_VERSION;
54ceac45 1840 switch (data->version) {
5df36e78
CL
1841 case 1:
1842 data->namlen = 0;
1843 case 2:
1844 data->bsize = 0;
1845 case 3:
1846 if (data->flags & NFS_MOUNT_VER3)
1847 goto out_no_v3;
1848 data->root.size = NFS2_FHSIZE;
1849 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1850 case 4:
1851 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1852 goto out_no_sec;
1853 case 5:
1854 memset(data->context, 0, sizeof(data->context));
1855 case 6:
b7e24457
TM
1856 if (data->flags & NFS_MOUNT_VER3) {
1857 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1858 goto out_invalid_fh;
5df36e78 1859 mntfh->size = data->root.size;
764302cc
CL
1860 args->version = 3;
1861 } else {
5df36e78 1862 mntfh->size = NFS2_FHSIZE;
764302cc
CL
1863 args->version = 2;
1864 }
5df36e78 1865
5df36e78
CL
1866
1867 memcpy(mntfh->data, data->root.data, mntfh->size);
1868 if (mntfh->size < sizeof(mntfh->data))
1869 memset(mntfh->data + mntfh->size, 0,
1870 sizeof(mntfh->data) - mntfh->size);
6e88e061 1871
2283f8d6
TT
1872 /*
1873 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1874 * can deal with.
1875 */
ff3525a5 1876 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
d5eff1a3 1877 args->flags |= NFS_MOUNT_LEGACY_INTERFACE;
2283f8d6
TT
1878 args->rsize = data->rsize;
1879 args->wsize = data->wsize;
2283f8d6
TT
1880 args->timeo = data->timeo;
1881 args->retrans = data->retrans;
1882 args->acregmin = data->acregmin;
1883 args->acregmax = data->acregmax;
1884 args->acdirmin = data->acdirmin;
1885 args->acdirmax = data->acdirmax;
b72e4f42 1886 args->need_mount = false;
4c568017 1887
4cfd74fc 1888 memcpy(sap, &data->addr, sizeof(data->addr));
4c568017 1889 args->nfs_server.addrlen = sizeof(data->addr);
872ece86 1890 args->nfs_server.port = ntohs(data->addr.sin_port);
4cfd74fc 1891 if (!nfs_verify_server_address(sap))
4c568017
CL
1892 goto out_no_address;
1893
2283f8d6 1894 if (!(data->flags & NFS_MOUNT_TCP))
0896a725 1895 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
2283f8d6
TT
1896 /* N.B. caller will free nfs_server.hostname in all cases */
1897 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1898 args->namlen = data->namlen;
1899 args->bsize = data->bsize;
dd07c947
CL
1900
1901 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1902 args->auth_flavors[0] = data->pseudoflavor;
63649bd7
CG
1903 if (!args->nfs_server.hostname)
1904 goto out_nomem;
f9c3a380 1905
5eebde23
SJ
1906 if (!(data->flags & NFS_MOUNT_NONLM))
1907 args->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1908 NFS_MOUNT_LOCAL_FCNTL);
1909 else
1910 args->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1911 NFS_MOUNT_LOCAL_FCNTL);
f9c3a380
EP
1912 /*
1913 * The legacy version 6 binary mount data from userspace has a
1914 * field used only to transport selinux information into the
1915 * the kernel. To continue to support that functionality we
1916 * have a touch of selinux knowledge here in the NFS code. The
1917 * userspace code converted context=blah to just blah so we are
1918 * converting back to the full string selinux understands.
1919 */
1920 if (data->context[0]){
1921#ifdef CONFIG_SECURITY_SELINUX
1922 int rc;
1923 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1924 if (!opts_str)
1925 return -ENOMEM;
1926 strcpy(opts_str, "context=");
1927 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1928 strcat(opts_str, &data->context[0]);
1929 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1930 kfree(opts_str);
1931 if (rc)
1932 return rc;
1933#else
1934 return -EINVAL;
1935#endif
1936 }
1937
5df36e78 1938 break;
b72e4f42
BS
1939 default:
1940 return NFS_TEXT_DATA;
f7b422b1 1941 }
54ceac45 1942
1c606fb7 1943#if !IS_ENABLED(CONFIG_NFS_V3)
8a6e5deb 1944 if (args->version == 3)
5df36e78
CL
1945 goto out_v3_not_compiled;
1946#endif /* !CONFIG_NFS_V3 */
f7b422b1 1947
5df36e78 1948 return 0;
f7b422b1 1949
5df36e78
CL
1950out_no_data:
1951 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1952 return -EINVAL;
54ceac45 1953
5df36e78
CL
1954out_no_v3:
1955 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1956 data->version);
1957 return -EINVAL;
f7b422b1 1958
5df36e78
CL
1959out_no_sec:
1960 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1961 return -EINVAL;
1962
1c606fb7 1963#if !IS_ENABLED(CONFIG_NFS_V3)
5df36e78
CL
1964out_v3_not_compiled:
1965 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1966 return -EPROTONOSUPPORT;
1967#endif /* !CONFIG_NFS_V3 */
1968
63649bd7
CG
1969out_nomem:
1970 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1971 return -ENOMEM;
1972
5df36e78
CL
1973out_no_address:
1974 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1975 return -EINVAL;
1976
1977out_invalid_fh:
1978 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1979 return -EINVAL;
f7b422b1
DH
1980}
1981
89d77c8f 1982#if IS_ENABLED(CONFIG_NFS_V4)
d72c727c
BS
1983static int nfs_validate_mount_data(struct file_system_type *fs_type,
1984 void *options,
1985 struct nfs_parsed_mount_data *args,
1986 struct nfs_fh *mntfh,
1987 const char *dev_name)
1988{
1989 if (fs_type == &nfs_fs_type)
1990 return nfs23_validate_mount_data(options, args, mntfh, dev_name);
1991 return nfs4_validate_mount_data(options, args, dev_name);
1992}
1993#else
1994static int nfs_validate_mount_data(struct file_system_type *fs_type,
1995 void *options,
1996 struct nfs_parsed_mount_data *args,
1997 struct nfs_fh *mntfh,
1998 const char *dev_name)
1999{
2000 return nfs23_validate_mount_data(options, args, mntfh, dev_name);
2001}
2002#endif
2003
b72e4f42
BS
2004static int nfs_validate_text_mount_data(void *options,
2005 struct nfs_parsed_mount_data *args,
2006 const char *dev_name)
2007{
2008 int port = 0;
2009 int max_namelen = PAGE_SIZE;
2010 int max_pathlen = NFS_MAXPATHLEN;
2011 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2012
2013 if (nfs_parse_mount_options((char *)options, args) == 0)
2014 return -EINVAL;
2015
2016 if (!nfs_verify_server_address(sap))
2017 goto out_no_address;
2018
2019 if (args->version == 4) {
89d77c8f 2020#if IS_ENABLED(CONFIG_NFS_V4)
b72e4f42
BS
2021 port = NFS_PORT;
2022 max_namelen = NFS4_MAXNAMLEN;
2023 max_pathlen = NFS4_MAXPATHLEN;
2024 nfs_validate_transport_protocol(args);
2025 nfs4_validate_mount_flags(args);
2026#else
2027 goto out_v4_not_compiled;
2028#endif /* CONFIG_NFS_V4 */
2029 } else
2030 nfs_set_mount_transport_protocol(args);
2031
2032 nfs_set_port(sap, &args->nfs_server.port, port);
2033
2034 if (args->auth_flavor_len > 1)
2035 goto out_bad_auth;
2036
2037 return nfs_parse_devname(dev_name,
2038 &args->nfs_server.hostname,
2039 max_namelen,
2040 &args->nfs_server.export_path,
2041 max_pathlen);
2042
89d77c8f 2043#if !IS_ENABLED(CONFIG_NFS_V4)
b72e4f42
BS
2044out_v4_not_compiled:
2045 dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
2046 return -EPROTONOSUPPORT;
2047#endif /* !CONFIG_NFS_V4 */
2048
2049out_no_address:
2050 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
2051 return -EINVAL;
2052
2053out_bad_auth:
2054 dfprintk(MOUNT, "NFS: Too many RPC auth flavours specified\n");
2055 return -EINVAL;
2056}
2057
48b605f8
JL
2058static int
2059nfs_compare_remount_data(struct nfs_server *nfss,
2060 struct nfs_parsed_mount_data *data)
2061{
2062 if (data->flags != nfss->flags ||
2063 data->rsize != nfss->rsize ||
2064 data->wsize != nfss->wsize ||
2065 data->retrans != nfss->client->cl_timeout->to_retries ||
2066 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
2067 data->acregmin != nfss->acregmin / HZ ||
2068 data->acregmax != nfss->acregmax / HZ ||
2069 data->acdirmin != nfss->acdirmin / HZ ||
2070 data->acdirmax != nfss->acdirmax / HZ ||
2071 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
bcd2ea17 2072 data->nfs_server.port != nfss->port ||
48b605f8 2073 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
a1be9eee
SR
2074 !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
2075 (struct sockaddr *)&nfss->nfs_client->cl_addr))
48b605f8
JL
2076 return -EINVAL;
2077
2078 return 0;
2079}
2080
fbdefd64 2081int
48b605f8
JL
2082nfs_remount(struct super_block *sb, int *flags, char *raw_data)
2083{
2084 int error;
2085 struct nfs_server *nfss = sb->s_fs_info;
2086 struct nfs_parsed_mount_data *data;
2087 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
2088 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
cd100725 2089 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
48b605f8
JL
2090
2091 /*
2092 * Userspace mount programs that send binary options generally send
2093 * them populated with default values. We have no way to know which
2094 * ones were explicitly specified. Fall back to legacy behavior and
2095 * just return success.
2096 */
31c94469
MZ
2097 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
2098 (nfsvers <= 3 && (!options || (options->version >= 1 &&
2099 options->version <= 6))))
48b605f8
JL
2100 return 0;
2101
2102 data = kzalloc(sizeof(*data), GFP_KERNEL);
2103 if (data == NULL)
2104 return -ENOMEM;
2105
2106 /* fill out struct with values from existing mount */
2107 data->flags = nfss->flags;
2108 data->rsize = nfss->rsize;
2109 data->wsize = nfss->wsize;
2110 data->retrans = nfss->client->cl_timeout->to_retries;
2111 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
2112 data->acregmin = nfss->acregmin / HZ;
2113 data->acregmax = nfss->acregmax / HZ;
2114 data->acdirmin = nfss->acdirmin / HZ;
2115 data->acdirmax = nfss->acdirmax / HZ;
2116 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
bcd2ea17 2117 data->nfs_server.port = nfss->port;
48b605f8
JL
2118 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
2119 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
2120 data->nfs_server.addrlen);
2121
2122 /* overwrite those values with any that were specified */
2123 error = nfs_parse_mount_options((char *)options, data);
2124 if (error < 0)
2125 goto out;
2126
26c4c170
JL
2127 /*
2128 * noac is a special case. It implies -o sync, but that's not
2129 * necessarily reflected in the mtab options. do_remount_sb
2130 * will clear MS_SYNCHRONOUS if -o sync wasn't specified in the
2131 * remount options, so we have to explicitly reset it.
2132 */
2133 if (data->flags & NFS_MOUNT_NOAC)
2134 *flags |= MS_SYNCHRONOUS;
2135
48b605f8
JL
2136 /* compare new mount options with old ones */
2137 error = nfs_compare_remount_data(nfss, data);
2138out:
2139 kfree(data);
2140 return error;
2141}
89d77c8f 2142EXPORT_SYMBOL_GPL(nfs_remount);
48b605f8 2143
f7b422b1 2144/*
54ceac45 2145 * Initialise the common bits of the superblock
f7b422b1 2146 */
fbdefd64 2147inline void nfs_initialise_sb(struct super_block *sb)
f7b422b1 2148{
54ceac45 2149 struct nfs_server *server = NFS_SB(sb);
5006a76c 2150
54ceac45 2151 sb->s_magic = NFS_SUPER_MAGIC;
f7b422b1 2152
54ceac45
DH
2153 /* We probably want something more informative here */
2154 snprintf(sb->s_id, sizeof(sb->s_id),
5a7c9eec 2155 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
5006a76c 2156
54ceac45
DH
2157 if (sb->s_blocksize == 0)
2158 sb->s_blocksize = nfs_block_bits(server->wsize,
2159 &sb->s_blocksize_bits);
f7b422b1 2160
32a88aa1
JA
2161 sb->s_bdi = &server->backing_dev_info;
2162
54ceac45 2163 nfs_super_set_maxbytes(sb, server->maxfilesize);
f7b422b1
DH
2164}
2165
2166/*
54ceac45 2167 * Finish setting up an NFS2/3 superblock
f7b422b1 2168 */
fbdefd64 2169void nfs_fill_super(struct super_block *sb, struct nfs_mount_info *mount_info)
f7b422b1 2170{
c40f8d1d 2171 struct nfs_parsed_mount_data *data = mount_info->parsed;
f7b422b1 2172 struct nfs_server *server = NFS_SB(sb);
5006a76c 2173
54ceac45
DH
2174 sb->s_blocksize_bits = 0;
2175 sb->s_blocksize = 0;
6a74490d
BS
2176 sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
2177 sb->s_op = server->nfs_client->cl_nfs_mod->sops;
2178 if (data && data->bsize)
54ceac45 2179 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
f7b422b1 2180
6a74490d 2181 if (server->nfs_client->rpc_ops->version != 2) {
54ceac45
DH
2182 /* The VFS shouldn't apply the umask to mode bits. We will do
2183 * so ourselves when necessary.
2184 */
2185 sb->s_flags |= MS_POSIXACL;
2186 sb->s_time_gran = 1;
816724e6 2187 }
54ceac45 2188
54ceac45 2189 nfs_initialise_sb(sb);
f7b422b1 2190}
89d77c8f 2191EXPORT_SYMBOL_GPL(nfs_fill_super);
f7b422b1
DH
2192
2193/*
3cadf4b8 2194 * Finish setting up a cloned NFS2/3/4 superblock
f7b422b1 2195 */
fbdefd64 2196void nfs_clone_super(struct super_block *sb, struct nfs_mount_info *mount_info)
f7b422b1 2197{
8c958e0c 2198 const struct super_block *old_sb = mount_info->cloned->sb;
54ceac45
DH
2199 struct nfs_server *server = NFS_SB(sb);
2200
2201 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2202 sb->s_blocksize = old_sb->s_blocksize;
2203 sb->s_maxbytes = old_sb->s_maxbytes;
3cadf4b8
BS
2204 sb->s_xattr = old_sb->s_xattr;
2205 sb->s_op = old_sb->s_op;
2206 sb->s_time_gran = 1;
f7b422b1 2207
3cadf4b8 2208 if (server->nfs_client->rpc_ops->version != 2) {
54ceac45
DH
2209 /* The VFS shouldn't apply the umask to mode bits. We will do
2210 * so ourselves when necessary.
f7b422b1
DH
2211 */
2212 sb->s_flags |= MS_POSIXACL;
f7b422b1
DH
2213 }
2214
54ceac45 2215 nfs_initialise_sb(sb);
f7b422b1
DH
2216}
2217
275a5d24
TM
2218static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2219{
2220 const struct nfs_server *a = s->s_fs_info;
2221 const struct rpc_clnt *clnt_a = a->client;
2222 const struct rpc_clnt *clnt_b = b->client;
2223
2224 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2225 goto Ebusy;
2226 if (a->nfs_client != b->nfs_client)
2227 goto Ebusy;
2228 if (a->flags != b->flags)
2229 goto Ebusy;
2230 if (a->wsize != b->wsize)
2231 goto Ebusy;
2232 if (a->rsize != b->rsize)
2233 goto Ebusy;
2234 if (a->acregmin != b->acregmin)
2235 goto Ebusy;
2236 if (a->acregmax != b->acregmax)
2237 goto Ebusy;
2238 if (a->acdirmin != b->acdirmin)
2239 goto Ebusy;
2240 if (a->acdirmax != b->acdirmax)
2241 goto Ebusy;
2242 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2243 goto Ebusy;
e89a5a43 2244 return 1;
275a5d24 2245Ebusy:
e89a5a43
TM
2246 return 0;
2247}
2248
2249struct nfs_sb_mountdata {
2250 struct nfs_server *server;
2251 int mntflags;
2252};
2253
2254static int nfs_set_super(struct super_block *s, void *data)
2255{
2256 struct nfs_sb_mountdata *sb_mntdata = data;
2257 struct nfs_server *server = sb_mntdata->server;
2258 int ret;
2259
2260 s->s_flags = sb_mntdata->mntflags;
2261 s->s_fs_info = server;
8b244ff2 2262 s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
e89a5a43
TM
2263 ret = set_anon_super(s, server);
2264 if (ret == 0)
2265 server->s_dev = s->s_dev;
2266 return ret;
2267}
2268
fd00a8ff
CL
2269static int nfs_compare_super_address(struct nfs_server *server1,
2270 struct nfs_server *server2)
2271{
2272 struct sockaddr *sap1, *sap2;
2273
2274 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2275 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2276
2277 if (sap1->sa_family != sap2->sa_family)
2278 return 0;
2279
2280 switch (sap1->sa_family) {
2281 case AF_INET: {
2282 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2283 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2284 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2285 return 0;
2286 if (sin1->sin_port != sin2->sin_port)
2287 return 0;
2288 break;
2289 }
2290 case AF_INET6: {
2291 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2292 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2293 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2294 return 0;
2295 if (sin1->sin6_port != sin2->sin6_port)
2296 return 0;
2297 break;
2298 }
2299 default:
2300 return 0;
2301 }
2302
2303 return 1;
2304}
2305
e89a5a43
TM
2306static int nfs_compare_super(struct super_block *sb, void *data)
2307{
2308 struct nfs_sb_mountdata *sb_mntdata = data;
2309 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2310 int mntflags = sb_mntdata->mntflags;
2311
fd00a8ff 2312 if (!nfs_compare_super_address(old, server))
e89a5a43
TM
2313 return 0;
2314 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2315 if (old->flags & NFS_MOUNT_UNSHARED)
2316 return 0;
2317 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2318 return 0;
2319 return nfs_compare_mount_options(sb, server, mntflags);
275a5d24
TM
2320}
2321
39ffb921 2322#ifdef CONFIG_NFS_FSCACHE
2311b943
BS
2323static void nfs_get_cache_cookie(struct super_block *sb,
2324 struct nfs_parsed_mount_data *parsed,
2325 struct nfs_clone_mount *cloned)
2326{
2327 char *uniq = NULL;
2328 int ulen = 0;
2329
2330 if (parsed && parsed->fscache_uniq) {
2331 uniq = parsed->fscache_uniq;
2332 ulen = strlen(parsed->fscache_uniq);
2333 } else if (cloned) {
2334 struct nfs_server *mnt_s = NFS_SB(cloned->sb);
2335 if (mnt_s->fscache_key) {
2336 uniq = mnt_s->fscache_key->key.uniquifier;
2337 ulen = mnt_s->fscache_key->key.uniq_len;
2338 };
2339 }
2340
2341 nfs_fscache_get_super_cookie(sb, uniq, ulen);
2342}
39ffb921
TM
2343#else
2344static void nfs_get_cache_cookie(struct super_block *sb,
2345 struct nfs_parsed_mount_data *parsed,
2346 struct nfs_clone_mount *cloned)
2347{
2348}
2349#endif
2311b943 2350
fa799759
MS
2351static int nfs_bdi_register(struct nfs_server *server)
2352{
2353 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2354}
2355
fbdefd64
BS
2356int nfs_set_sb_security(struct super_block *s, struct dentry *mntroot,
2357 struct nfs_mount_info *mount_info)
3d176e3f
BS
2358{
2359 return security_sb_set_mnt_opts(s, &mount_info->parsed->lsm_opts);
2360}
89d77c8f 2361EXPORT_SYMBOL_GPL(nfs_set_sb_security);
3d176e3f 2362
fbdefd64
BS
2363int nfs_clone_sb_security(struct super_block *s, struct dentry *mntroot,
2364 struct nfs_mount_info *mount_info)
3d176e3f
BS
2365{
2366 /* clone any lsm security options from the parent to the new sb */
2367 security_sb_clone_mnt_opts(mount_info->cloned->sb, s);
2368 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops)
2369 return -ESTALE;
2370 return 0;
2371}
89d77c8f 2372EXPORT_SYMBOL_GPL(nfs_clone_sb_security);
3d176e3f 2373
ab7017a3 2374struct dentry *nfs_fs_mount_common(struct nfs_server *server,
fbdefd64 2375 int flags, const char *dev_name,
ab7017a3
BS
2376 struct nfs_mount_info *mount_info,
2377 struct nfs_subversion *nfs_mod)
f7b422b1 2378{
f7b422b1 2379 struct super_block *s;
01194981 2380 struct dentry *mntroot = ERR_PTR(-ENOMEM);
75180df2 2381 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
e89a5a43
TM
2382 struct nfs_sb_mountdata sb_mntdata = {
2383 .mntflags = flags,
c40f8d1d 2384 .server = server,
e89a5a43 2385 };
01194981 2386 int error;
33852a1f 2387
75180df2
TM
2388 if (server->flags & NFS_MOUNT_UNSHARED)
2389 compare_super = NULL;
2390
fb2088cc
SP
2391 /* -o noac implies -o sync */
2392 if (server->flags & NFS_MOUNT_NOAC)
2393 sb_mntdata.mntflags |= MS_SYNCHRONOUS;
2394
54ceac45 2395 /* Get a superblock - note that we may end up sharing one that already exists */
ab7017a3 2396 s = sget(nfs_mod->nfs_fs, compare_super, nfs_set_super, flags, &sb_mntdata);
816724e6 2397 if (IS_ERR(s)) {
01194981 2398 mntroot = ERR_CAST(s);
54ceac45 2399 goto out_err_nosb;
816724e6
TM
2400 }
2401
54ceac45
DH
2402 if (s->s_fs_info != server) {
2403 nfs_free_server(server);
2404 server = NULL;
fa799759
MS
2405 } else {
2406 error = nfs_bdi_register(server);
01194981
AV
2407 if (error) {
2408 mntroot = ERR_PTR(error);
cfbc0683 2409 goto error_splat_bdi;
01194981 2410 }
54ceac45 2411 }
f7b422b1 2412
54ceac45
DH
2413 if (!s->s_root) {
2414 /* initial superblock/root creation */
c40f8d1d 2415 mount_info->fill_super(s, mount_info);
3d176e3f 2416 nfs_get_cache_cookie(s, mount_info->parsed, mount_info->cloned);
54ceac45 2417 }
f7b422b1 2418
87c7083d 2419 mntroot = nfs_get_root(s, mount_info->mntfh, dev_name);
01194981 2420 if (IS_ERR(mntroot))
54ceac45 2421 goto error_splat_super;
816724e6 2422
3d176e3f 2423 error = mount_info->set_security(s, mntroot, mount_info);
f9c3a380
EP
2424 if (error)
2425 goto error_splat_root;
2426
54ceac45 2427 s->s_flags |= MS_ACTIVE;
0655960f
CL
2428
2429out:
01194981 2430 return mntroot;
816724e6 2431
54ceac45
DH
2432out_err_nosb:
2433 nfs_free_server(server);
0655960f 2434 goto out;
54ceac45 2435
f9c3a380
EP
2436error_splat_root:
2437 dput(mntroot);
01194981 2438 mntroot = ERR_PTR(error);
54ceac45 2439error_splat_super:
cfbc0683
N
2440 if (server && !s->s_root)
2441 bdi_unregister(&server->backing_dev_info);
2442error_splat_bdi:
6f5bbff9 2443 deactivate_locked_super(s);
0655960f 2444 goto out;
f7b422b1 2445}
89d77c8f 2446EXPORT_SYMBOL_GPL(nfs_fs_mount_common);
f7b422b1 2447
fbdefd64 2448struct dentry *nfs_fs_mount(struct file_system_type *fs_type,
c40f8d1d
BS
2449 int flags, const char *dev_name, void *raw_data)
2450{
c40f8d1d
BS
2451 struct nfs_mount_info mount_info = {
2452 .fill_super = nfs_fill_super,
3d176e3f 2453 .set_security = nfs_set_sb_security,
c40f8d1d 2454 };
c40f8d1d 2455 struct dentry *mntroot = ERR_PTR(-ENOMEM);
ab7017a3 2456 struct nfs_subversion *nfs_mod;
c40f8d1d
BS
2457 int error;
2458
46058d46 2459 mount_info.parsed = nfs_alloc_parsed_mount_data();
87c7083d
BS
2460 mount_info.mntfh = nfs_alloc_fhandle();
2461 if (mount_info.parsed == NULL || mount_info.mntfh == NULL)
c40f8d1d
BS
2462 goto out;
2463
2464 /* Validate the mount data */
87c7083d 2465 error = nfs_validate_mount_data(fs_type, raw_data, mount_info.parsed, mount_info.mntfh, dev_name);
b72e4f42 2466 if (error == NFS_TEXT_DATA)
46058d46 2467 error = nfs_validate_text_mount_data(raw_data, mount_info.parsed, dev_name);
c40f8d1d
BS
2468 if (error < 0) {
2469 mntroot = ERR_PTR(error);
2470 goto out;
2471 }
c40f8d1d 2472
ab7017a3
BS
2473 nfs_mod = get_nfs_version(mount_info.parsed->version);
2474 if (IS_ERR(nfs_mod)) {
2475 mntroot = ERR_CAST(nfs_mod);
2476 goto out;
2477 }
2478
ff9099f2 2479 mntroot = nfs_mod->rpc_ops->try_mount(flags, dev_name, &mount_info, nfs_mod);
ab7017a3
BS
2480
2481 put_nfs_version(nfs_mod);
c40f8d1d 2482out:
46058d46 2483 nfs_free_parsed_mount_data(mount_info.parsed);
87c7083d 2484 nfs_free_fhandle(mount_info.mntfh);
c40f8d1d
BS
2485 return mntroot;
2486}
89d77c8f 2487EXPORT_SYMBOL_GPL(nfs_fs_mount);
c40f8d1d 2488
387c149b
TM
2489/*
2490 * Ensure that we unregister the bdi before kill_anon_super
2491 * releases the device name
2492 */
fbdefd64 2493void nfs_put_super(struct super_block *s)
387c149b
TM
2494{
2495 struct nfs_server *server = NFS_SB(s);
2496
2497 bdi_unregister(&server->backing_dev_info);
2498}
89d77c8f 2499EXPORT_SYMBOL_GPL(nfs_put_super);
387c149b 2500
54ceac45
DH
2501/*
2502 * Destroy an NFS2/3 superblock
2503 */
fbdefd64 2504void nfs_kill_super(struct super_block *s)
f7b422b1
DH
2505{
2506 struct nfs_server *server = NFS_SB(s);
2507
2508 kill_anon_super(s);
08734048 2509 nfs_fscache_release_super_cookie(s);
54ceac45 2510 nfs_free_server(server);
f7b422b1 2511}
89d77c8f 2512EXPORT_SYMBOL_GPL(nfs_kill_super);
f7b422b1 2513
54ceac45 2514/*
8c958e0c 2515 * Clone an NFS2/3/4 server record on xdev traversal (FSID-change)
54ceac45 2516 */
3dd4f8ef 2517static struct dentry *
e8f25e6d
BS
2518nfs_xdev_mount(struct file_system_type *fs_type, int flags,
2519 const char *dev_name, void *raw_data)
f7b422b1 2520{
e8f25e6d
BS
2521 struct nfs_clone_mount *data = raw_data;
2522 struct nfs_mount_info mount_info = {
2523 .fill_super = nfs_clone_super,
2524 .set_security = nfs_clone_sb_security,
2525 .cloned = data,
2526 };
54ceac45 2527 struct nfs_server *server;
3d176e3f 2528 struct dentry *mntroot = ERR_PTR(-ENOMEM);
ab7017a3 2529 struct nfs_subversion *nfs_mod = NFS_SB(data->sb)->nfs_client->cl_nfs_mod;
54ceac45 2530 int error;
f7b422b1 2531
8c958e0c 2532 dprintk("--> nfs_xdev_mount_common()\n");
f7b422b1 2533
e8f25e6d 2534 mount_info.mntfh = mount_info.cloned->fh;
87c7083d 2535
54ceac45 2536 /* create a new volume representation */
1179acc6 2537 server = nfs_mod->rpc_ops->clone_server(NFS_SB(data->sb), data->fh, data->fattr, data->authflavor);
54ceac45
DH
2538 if (IS_ERR(server)) {
2539 error = PTR_ERR(server);
3d176e3f 2540 goto out_err;
4c1fe2f7 2541 }
f7b422b1 2542
e8f25e6d 2543 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, nfs_mod);
8c958e0c 2544 dprintk("<-- nfs_xdev_mount_common() = 0\n");
3d176e3f 2545out:
31f43471 2546 return mntroot;
24c8dbbb 2547
3d176e3f 2548out_err:
8c958e0c 2549 dprintk("<-- nfs_xdev_mount_common() = %d [error]\n", error);
3d176e3f 2550 goto out;
f7b422b1
DH
2551}
2552
89d77c8f 2553#if IS_ENABLED(CONFIG_NFS_V4)
54ceac45 2554
01c3f052
TM
2555static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2556{
5eebde23
SJ
2557 args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
2558 NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
01c3f052
TM
2559}
2560
f0768ebd
CL
2561/*
2562 * Validate NFSv4 mount options
2563 */
91ea40b9
TT
2564static int nfs4_validate_mount_data(void *options,
2565 struct nfs_parsed_mount_data *args,
2566 const char *dev_name)
f0768ebd 2567{
4cfd74fc 2568 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
91ea40b9 2569 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
f0768ebd
CL
2570 char *c;
2571
2572 if (data == NULL)
2573 goto out_no_data;
2574
cdf66442
BS
2575 args->version = 4;
2576
f0768ebd
CL
2577 switch (data->version) {
2578 case 1:
4c568017
CL
2579 if (data->host_addrlen > sizeof(args->nfs_server.address))
2580 goto out_no_address;
2581 if (data->host_addrlen == 0)
f0768ebd 2582 goto out_no_address;
4c568017 2583 args->nfs_server.addrlen = data->host_addrlen;
4cfd74fc 2584 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
f0768ebd 2585 return -EFAULT;
4cfd74fc 2586 if (!nfs_verify_server_address(sap))
f0768ebd 2587 goto out_no_address;
872ece86 2588 args->nfs_server.port = ntohs(((struct sockaddr_in *)sap)->sin_port);
f0768ebd 2589
6738b251
CL
2590 if (data->auth_flavourlen) {
2591 if (data->auth_flavourlen > 1)
2592 goto out_inval_auth;
20c71f5e 2593 if (copy_from_user(&args->auth_flavors[0],
91ea40b9 2594 data->auth_flavours,
20c71f5e 2595 sizeof(args->auth_flavors[0])))
f0768ebd 2596 return -EFAULT;
f0768ebd
CL
2597 }
2598
2599 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2600 if (IS_ERR(c))
2601 return PTR_ERR(c);
91ea40b9 2602 args->nfs_server.hostname = c;
f0768ebd
CL
2603
2604 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2605 if (IS_ERR(c))
2606 return PTR_ERR(c);
91ea40b9
TT
2607 args->nfs_server.export_path = c;
2608 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
f0768ebd
CL
2609
2610 c = strndup_user(data->client_addr.data, 16);
2611 if (IS_ERR(c))
2612 return PTR_ERR(c);
91ea40b9
TT
2613 args->client_address = c;
2614
2615 /*
2616 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2617 * can deal with.
2618 */
2619
2620 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2621 args->rsize = data->rsize;
2622 args->wsize = data->wsize;
2623 args->timeo = data->timeo;
2624 args->retrans = data->retrans;
2625 args->acregmin = data->acregmin;
2626 args->acregmax = data->acregmax;
2627 args->acdirmin = data->acdirmin;
2628 args->acdirmax = data->acdirmax;
2629 args->nfs_server.protocol = data->proto;
259875ef 2630 nfs_validate_transport_protocol(args);
f0768ebd
CL
2631
2632 break;
7630c852 2633 default:
b72e4f42 2634 return NFS_TEXT_DATA;
f0768ebd
CL
2635 }
2636
2637 return 0;
2638
2639out_no_data:
2640 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2641 return -EINVAL;
2642
2643out_inval_auth:
2644 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2645 data->auth_flavourlen);
2646 return -EINVAL;
2647
2648out_no_address:
2649 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2650 return -EINVAL;
f0768ebd
CL
2651}
2652
fac1e8e4
BS
2653/*
2654 * NFS v4 module parameters need to stay in the
2655 * NFS client for backwards compatibility
2656 */
2657unsigned int nfs_callback_set_tcpport;
2658unsigned short nfs_callback_tcpport;
2659/* Default cache timeout is 10 minutes */
2660unsigned int nfs_idmap_cache_timeout = 600;
2661/* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
2662bool nfs4_disable_idmapping = true;
2663unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
2664unsigned short send_implementation_id = 1;
6f2ea7f2 2665char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
fac1e8e4 2666
89d77c8f
BS
2667EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
2668EXPORT_SYMBOL_GPL(nfs_callback_tcpport);
2669EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
2670EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
2671EXPORT_SYMBOL_GPL(max_session_slots);
2672EXPORT_SYMBOL_GPL(send_implementation_id);
6f2ea7f2 2673EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
89d77c8f 2674
fac1e8e4
BS
2675#define NFS_CALLBACK_MAXPORTNR (65535U)
2676
2677static int param_set_portnr(const char *val, const struct kernel_param *kp)
2678{
2679 unsigned long num;
2680 int ret;
2681
2682 if (!val)
2683 return -EINVAL;
7297cb68 2684 ret = kstrtoul(val, 0, &num);
fac1e8e4
BS
2685 if (ret == -EINVAL || num > NFS_CALLBACK_MAXPORTNR)
2686 return -EINVAL;
2687 *((unsigned int *)kp->arg) = num;
2688 return 0;
2689}
2690static struct kernel_param_ops param_ops_portnr = {
2691 .set = param_set_portnr,
2692 .get = param_get_uint,
2693};
2694#define param_check_portnr(name, p) __param_check(name, p, unsigned int);
2695
2696module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
2697module_param(nfs_idmap_cache_timeout, int, 0644);
2698module_param(nfs4_disable_idmapping, bool, 0644);
6f2ea7f2
CL
2699module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
2700 NFS4_CLIENT_ID_UNIQ_LEN, 0600);
fac1e8e4
BS
2701MODULE_PARM_DESC(nfs4_disable_idmapping,
2702 "Turn off NFSv4 idmapping when using 'sec=sys'");
2703module_param(max_session_slots, ushort, 0644);
2704MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
2705 "requests the client will negotiate");
2706module_param(send_implementation_id, ushort, 0644);
2707MODULE_PARM_DESC(send_implementation_id,
2708 "Send implementation ID with NFSv4.1 exchange_id");
6f2ea7f2 2709MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
425e776d 2710MODULE_ALIAS("nfs4");
2711
54ceac45 2712#endif /* CONFIG_NFS_V4 */
This page took 0.756882 seconds and 5 git commands to generate.