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