Regression: fix mounting NFS when NFSv3 support is not compiled
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / super.c
blob9587506d493c320b36249de69495359f51d721c0
1 /*
2 * linux/fs/nfs/super.c
4 * Copyright (C) 1992 Rick Sladkey
6 * nfs superblock handling functions
8 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
14 * Split from inode.c by David Howells <dhowells@redhat.com>
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())
23 #include <linux/module.h>
24 #include <linux/init.h>
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>
36 #include <linux/sunrpc/xprtsock.h>
37 #include <linux/sunrpc/xprtrdma.h>
38 #include <linux/nfs_fs.h>
39 #include <linux/nfs_mount.h>
40 #include <linux/nfs4_mount.h>
41 #include <linux/lockd/bind.h>
42 #include <linux/smp_lock.h>
43 #include <linux/seq_file.h>
44 #include <linux/mount.h>
45 #include <linux/mnt_namespace.h>
46 #include <linux/namei.h>
47 #include <linux/nfs_idmap.h>
48 #include <linux/vfs.h>
49 #include <linux/inet.h>
50 #include <linux/in6.h>
51 #include <linux/slab.h>
52 #include <net/ipv6.h>
53 #include <linux/netdevice.h>
54 #include <linux/nfs_xdr.h>
55 #include <linux/magic.h>
56 #include <linux/parser.h>
58 #include <asm/system.h>
59 #include <asm/uaccess.h>
61 #include "nfs4_fs.h"
62 #include "callback.h"
63 #include "delegation.h"
64 #include "iostat.h"
65 #include "internal.h"
66 #include "fscache.h"
68 #define NFSDBG_FACILITY NFSDBG_VFS
70 #ifdef CONFIG_NFS_V3
71 #define NFS_DEFAULT_VERSION 3
72 #else
73 #define NFS_DEFAULT_VERSION 2
74 #endif
76 enum {
77 /* Mount options that take no arguments */
78 Opt_soft, Opt_hard,
79 Opt_posix, Opt_noposix,
80 Opt_cto, Opt_nocto,
81 Opt_ac, Opt_noac,
82 Opt_lock, Opt_nolock,
83 Opt_v2, Opt_v3, Opt_v4,
84 Opt_udp, Opt_tcp, Opt_rdma,
85 Opt_acl, Opt_noacl,
86 Opt_rdirplus, Opt_nordirplus,
87 Opt_sharecache, Opt_nosharecache,
88 Opt_resvport, Opt_noresvport,
89 Opt_fscache, Opt_nofscache,
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,
100 Opt_mountvers,
101 Opt_nfsvers,
102 Opt_minorversion,
104 /* Mount options that take string arguments */
105 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
106 Opt_addr, Opt_mountaddr, Opt_clientaddr,
107 Opt_lookupcache,
108 Opt_fscache_uniq,
109 Opt_local_lock,
111 /* Special mount options */
112 Opt_userspace, Opt_deprecated, Opt_sloppy,
114 Opt_err
117 static const match_table_t nfs_mount_option_tokens = {
118 { Opt_userspace, "bg" },
119 { Opt_userspace, "fg" },
120 { Opt_userspace, "retry=%s" },
122 { Opt_sloppy, "sloppy" },
124 { Opt_soft, "soft" },
125 { Opt_hard, "hard" },
126 { Opt_deprecated, "intr" },
127 { Opt_deprecated, "nointr" },
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" },
136 { Opt_v2, "v2" },
137 { Opt_v3, "v3" },
138 { Opt_v4, "v4" },
139 { Opt_udp, "udp" },
140 { Opt_tcp, "tcp" },
141 { Opt_rdma, "rdma" },
142 { Opt_acl, "acl" },
143 { Opt_noacl, "noacl" },
144 { Opt_rdirplus, "rdirplus" },
145 { Opt_nordirplus, "nordirplus" },
146 { Opt_sharecache, "sharecache" },
147 { Opt_nosharecache, "nosharecache" },
148 { Opt_resvport, "resvport" },
149 { Opt_noresvport, "noresvport" },
150 { Opt_fscache, "fsc" },
151 { Opt_nofscache, "nofsc" },
153 { Opt_port, "port=%s" },
154 { Opt_rsize, "rsize=%s" },
155 { Opt_wsize, "wsize=%s" },
156 { Opt_bsize, "bsize=%s" },
157 { Opt_timeo, "timeo=%s" },
158 { Opt_retrans, "retrans=%s" },
159 { Opt_acregmin, "acregmin=%s" },
160 { Opt_acregmax, "acregmax=%s" },
161 { Opt_acdirmin, "acdirmin=%s" },
162 { Opt_acdirmax, "acdirmax=%s" },
163 { Opt_actimeo, "actimeo=%s" },
164 { Opt_namelen, "namlen=%s" },
165 { Opt_mountport, "mountport=%s" },
166 { Opt_mountvers, "mountvers=%s" },
167 { Opt_nfsvers, "nfsvers=%s" },
168 { Opt_nfsvers, "vers=%s" },
169 { Opt_minorversion, "minorversion=%s" },
171 { Opt_sec, "sec=%s" },
172 { Opt_proto, "proto=%s" },
173 { Opt_mountproto, "mountproto=%s" },
174 { Opt_addr, "addr=%s" },
175 { Opt_clientaddr, "clientaddr=%s" },
176 { Opt_mounthost, "mounthost=%s" },
177 { Opt_mountaddr, "mountaddr=%s" },
179 { Opt_lookupcache, "lookupcache=%s" },
180 { Opt_fscache_uniq, "fsc=%s" },
181 { Opt_local_lock, "local_lock=%s" },
183 { Opt_err, NULL }
186 enum {
187 Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
189 Opt_xprt_err
192 static const match_table_t nfs_xprt_protocol_tokens = {
193 { Opt_xprt_udp, "udp" },
194 { Opt_xprt_udp6, "udp6" },
195 { Opt_xprt_tcp, "tcp" },
196 { Opt_xprt_tcp6, "tcp6" },
197 { Opt_xprt_rdma, "rdma" },
199 { Opt_xprt_err, NULL }
202 enum {
203 Opt_sec_none, Opt_sec_sys,
204 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
205 Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
206 Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
208 Opt_sec_err
211 static const match_table_t nfs_secflavor_tokens = {
212 { Opt_sec_none, "none" },
213 { Opt_sec_none, "null" },
214 { Opt_sec_sys, "sys" },
216 { Opt_sec_krb5, "krb5" },
217 { Opt_sec_krb5i, "krb5i" },
218 { Opt_sec_krb5p, "krb5p" },
220 { Opt_sec_lkey, "lkey" },
221 { Opt_sec_lkeyi, "lkeyi" },
222 { Opt_sec_lkeyp, "lkeyp" },
224 { Opt_sec_spkm, "spkm3" },
225 { Opt_sec_spkmi, "spkm3i" },
226 { Opt_sec_spkmp, "spkm3p" },
228 { Opt_sec_err, NULL }
231 enum {
232 Opt_lookupcache_all, Opt_lookupcache_positive,
233 Opt_lookupcache_none,
235 Opt_lookupcache_err
238 static match_table_t nfs_lookupcache_tokens = {
239 { Opt_lookupcache_all, "all" },
240 { Opt_lookupcache_positive, "pos" },
241 { Opt_lookupcache_positive, "positive" },
242 { Opt_lookupcache_none, "none" },
244 { Opt_lookupcache_err, NULL }
247 enum {
248 Opt_local_lock_all, Opt_local_lock_flock, Opt_local_lock_posix,
249 Opt_local_lock_none,
251 Opt_local_lock_err
254 static match_table_t nfs_local_lock_tokens = {
255 { Opt_local_lock_all, "all" },
256 { Opt_local_lock_flock, "flock" },
257 { Opt_local_lock_posix, "posix" },
258 { Opt_local_lock_none, "none" },
260 { Opt_local_lock_err, NULL }
264 static void nfs_umount_begin(struct super_block *);
265 static int nfs_statfs(struct dentry *, struct kstatfs *);
266 static int nfs_show_options(struct seq_file *, struct vfsmount *);
267 static int nfs_show_stats(struct seq_file *, struct vfsmount *);
268 static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
269 static struct dentry *nfs_xdev_mount(struct file_system_type *fs_type,
270 int flags, const char *dev_name, void *raw_data);
271 static void nfs_put_super(struct super_block *);
272 static void nfs_kill_super(struct super_block *);
273 static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
275 static struct file_system_type nfs_fs_type = {
276 .owner = THIS_MODULE,
277 .name = "nfs",
278 .get_sb = nfs_get_sb,
279 .kill_sb = nfs_kill_super,
280 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
283 struct file_system_type nfs_xdev_fs_type = {
284 .owner = THIS_MODULE,
285 .name = "nfs",
286 .mount = nfs_xdev_mount,
287 .kill_sb = nfs_kill_super,
288 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
291 static const struct super_operations nfs_sops = {
292 .alloc_inode = nfs_alloc_inode,
293 .destroy_inode = nfs_destroy_inode,
294 .write_inode = nfs_write_inode,
295 .put_super = nfs_put_super,
296 .statfs = nfs_statfs,
297 .evict_inode = nfs_evict_inode,
298 .umount_begin = nfs_umount_begin,
299 .show_options = nfs_show_options,
300 .show_stats = nfs_show_stats,
301 .remount_fs = nfs_remount,
304 #ifdef CONFIG_NFS_V4
305 static int nfs4_validate_text_mount_data(void *options,
306 struct nfs_parsed_mount_data *args, const char *dev_name);
307 static int nfs4_try_mount(int flags, const char *dev_name,
308 struct nfs_parsed_mount_data *data, struct vfsmount *mnt);
309 static int nfs4_get_sb(struct file_system_type *fs_type,
310 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
311 static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type,
312 int flags, const char *dev_name, void *raw_data);
313 static struct dentry *nfs4_xdev_mount(struct file_system_type *fs_type,
314 int flags, const char *dev_name, void *raw_data);
315 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
316 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
317 static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
318 int flags, const char *dev_name, void *raw_data);
319 static void nfs4_kill_super(struct super_block *sb);
321 static struct file_system_type nfs4_fs_type = {
322 .owner = THIS_MODULE,
323 .name = "nfs4",
324 .get_sb = nfs4_get_sb,
325 .kill_sb = nfs4_kill_super,
326 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
329 static struct file_system_type nfs4_remote_fs_type = {
330 .owner = THIS_MODULE,
331 .name = "nfs4",
332 .mount = nfs4_remote_mount,
333 .kill_sb = nfs4_kill_super,
334 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
337 struct file_system_type nfs4_xdev_fs_type = {
338 .owner = THIS_MODULE,
339 .name = "nfs4",
340 .mount = nfs4_xdev_mount,
341 .kill_sb = nfs4_kill_super,
342 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
345 static struct file_system_type nfs4_remote_referral_fs_type = {
346 .owner = THIS_MODULE,
347 .name = "nfs4",
348 .mount = nfs4_remote_referral_mount,
349 .kill_sb = nfs4_kill_super,
350 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
353 struct file_system_type nfs4_referral_fs_type = {
354 .owner = THIS_MODULE,
355 .name = "nfs4",
356 .get_sb = nfs4_referral_get_sb,
357 .kill_sb = nfs4_kill_super,
358 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
361 static const struct super_operations nfs4_sops = {
362 .alloc_inode = nfs_alloc_inode,
363 .destroy_inode = nfs_destroy_inode,
364 .write_inode = nfs_write_inode,
365 .put_super = nfs_put_super,
366 .statfs = nfs_statfs,
367 .evict_inode = nfs4_evict_inode,
368 .umount_begin = nfs_umount_begin,
369 .show_options = nfs_show_options,
370 .show_stats = nfs_show_stats,
371 .remount_fs = nfs_remount,
373 #endif
375 static struct shrinker acl_shrinker = {
376 .shrink = nfs_access_cache_shrinker,
377 .seeks = DEFAULT_SEEKS,
381 * Register the NFS filesystems
383 int __init register_nfs_fs(void)
385 int ret;
387 ret = register_filesystem(&nfs_fs_type);
388 if (ret < 0)
389 goto error_0;
391 ret = nfs_register_sysctl();
392 if (ret < 0)
393 goto error_1;
394 #ifdef CONFIG_NFS_V4
395 ret = register_filesystem(&nfs4_fs_type);
396 if (ret < 0)
397 goto error_2;
398 #endif
399 register_shrinker(&acl_shrinker);
400 return 0;
402 #ifdef CONFIG_NFS_V4
403 error_2:
404 nfs_unregister_sysctl();
405 #endif
406 error_1:
407 unregister_filesystem(&nfs_fs_type);
408 error_0:
409 return ret;
413 * Unregister the NFS filesystems
415 void __exit unregister_nfs_fs(void)
417 unregister_shrinker(&acl_shrinker);
418 #ifdef CONFIG_NFS_V4
419 unregister_filesystem(&nfs4_fs_type);
420 #endif
421 nfs_unregister_sysctl();
422 unregister_filesystem(&nfs_fs_type);
425 void nfs_sb_active(struct super_block *sb)
427 struct nfs_server *server = NFS_SB(sb);
429 if (atomic_inc_return(&server->active) == 1)
430 atomic_inc(&sb->s_active);
433 void nfs_sb_deactive(struct super_block *sb)
435 struct nfs_server *server = NFS_SB(sb);
437 if (atomic_dec_and_test(&server->active))
438 deactivate_super(sb);
442 * Deliver file system statistics to userspace
444 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
446 struct nfs_server *server = NFS_SB(dentry->d_sb);
447 unsigned char blockbits;
448 unsigned long blockres;
449 struct nfs_fh *fh = NFS_FH(dentry->d_inode);
450 struct nfs_fsstat res;
451 int error = -ENOMEM;
453 res.fattr = nfs_alloc_fattr();
454 if (res.fattr == NULL)
455 goto out_err;
457 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
458 if (unlikely(error == -ESTALE)) {
459 struct dentry *pd_dentry;
461 pd_dentry = dget_parent(dentry);
462 if (pd_dentry != NULL) {
463 nfs_zap_caches(pd_dentry->d_inode);
464 dput(pd_dentry);
467 nfs_free_fattr(res.fattr);
468 if (error < 0)
469 goto out_err;
471 buf->f_type = NFS_SUPER_MAGIC;
474 * Current versions of glibc do not correctly handle the
475 * case where f_frsize != f_bsize. Eventually we want to
476 * report the value of wtmult in this field.
478 buf->f_frsize = dentry->d_sb->s_blocksize;
481 * On most *nix systems, f_blocks, f_bfree, and f_bavail
482 * are reported in units of f_frsize. Linux hasn't had
483 * an f_frsize field in its statfs struct until recently,
484 * thus historically Linux's sys_statfs reports these
485 * fields in units of f_bsize.
487 buf->f_bsize = dentry->d_sb->s_blocksize;
488 blockbits = dentry->d_sb->s_blocksize_bits;
489 blockres = (1 << blockbits) - 1;
490 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
491 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
492 buf->f_bavail = (res.abytes + blockres) >> blockbits;
494 buf->f_files = res.tfiles;
495 buf->f_ffree = res.afiles;
497 buf->f_namelen = server->namelen;
499 return 0;
501 out_err:
502 dprintk("%s: statfs error = %d\n", __func__, -error);
503 return error;
507 * Map the security flavour number to a name
509 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
511 static const struct {
512 rpc_authflavor_t flavour;
513 const char *str;
514 } sec_flavours[] = {
515 { RPC_AUTH_NULL, "null" },
516 { RPC_AUTH_UNIX, "sys" },
517 { RPC_AUTH_GSS_KRB5, "krb5" },
518 { RPC_AUTH_GSS_KRB5I, "krb5i" },
519 { RPC_AUTH_GSS_KRB5P, "krb5p" },
520 { RPC_AUTH_GSS_LKEY, "lkey" },
521 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
522 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
523 { RPC_AUTH_GSS_SPKM, "spkm" },
524 { RPC_AUTH_GSS_SPKMI, "spkmi" },
525 { RPC_AUTH_GSS_SPKMP, "spkmp" },
526 { UINT_MAX, "unknown" }
528 int i;
530 for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
531 if (sec_flavours[i].flavour == flavour)
532 break;
534 return sec_flavours[i].str;
537 static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
538 int showdefaults)
540 struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
542 seq_printf(m, ",mountproto=");
543 switch (sap->sa_family) {
544 case AF_INET:
545 switch (nfss->mountd_protocol) {
546 case IPPROTO_UDP:
547 seq_printf(m, RPCBIND_NETID_UDP);
548 break;
549 case IPPROTO_TCP:
550 seq_printf(m, RPCBIND_NETID_TCP);
551 break;
552 default:
553 if (showdefaults)
554 seq_printf(m, "auto");
556 break;
557 case AF_INET6:
558 switch (nfss->mountd_protocol) {
559 case IPPROTO_UDP:
560 seq_printf(m, RPCBIND_NETID_UDP6);
561 break;
562 case IPPROTO_TCP:
563 seq_printf(m, RPCBIND_NETID_TCP6);
564 break;
565 default:
566 if (showdefaults)
567 seq_printf(m, "auto");
569 break;
570 default:
571 if (showdefaults)
572 seq_printf(m, "auto");
576 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
577 int showdefaults)
579 struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
581 if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
582 return;
584 switch (sap->sa_family) {
585 case AF_INET: {
586 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
587 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
588 break;
590 case AF_INET6: {
591 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
592 seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
593 break;
595 default:
596 if (showdefaults)
597 seq_printf(m, ",mountaddr=unspecified");
600 if (nfss->mountd_version || showdefaults)
601 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
602 if (nfss->mountd_port || showdefaults)
603 seq_printf(m, ",mountport=%u", nfss->mountd_port);
605 nfs_show_mountd_netid(m, nfss, showdefaults);
608 #ifdef CONFIG_NFS_V4
609 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
610 int showdefaults)
612 struct nfs_client *clp = nfss->nfs_client;
614 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
615 seq_printf(m, ",minorversion=%u", clp->cl_minorversion);
617 #else
618 static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
619 int showdefaults)
622 #endif
625 * Describe the mount options in force on this server representation
627 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
628 int showdefaults)
630 static const struct proc_nfs_info {
631 int flag;
632 const char *str;
633 const char *nostr;
634 } nfs_info[] = {
635 { NFS_MOUNT_SOFT, ",soft", ",hard" },
636 { NFS_MOUNT_POSIX, ",posix", "" },
637 { NFS_MOUNT_NOCTO, ",nocto", "" },
638 { NFS_MOUNT_NOAC, ",noac", "" },
639 { NFS_MOUNT_NONLM, ",nolock", "" },
640 { NFS_MOUNT_NOACL, ",noacl", "" },
641 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
642 { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
643 { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
644 { 0, NULL, NULL }
646 const struct proc_nfs_info *nfs_infop;
647 struct nfs_client *clp = nfss->nfs_client;
648 u32 version = clp->rpc_ops->version;
649 int local_flock, local_fcntl;
651 seq_printf(m, ",vers=%u", version);
652 seq_printf(m, ",rsize=%u", nfss->rsize);
653 seq_printf(m, ",wsize=%u", nfss->wsize);
654 if (nfss->bsize != 0)
655 seq_printf(m, ",bsize=%u", nfss->bsize);
656 seq_printf(m, ",namlen=%u", nfss->namelen);
657 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
658 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
659 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
660 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
661 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
662 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
663 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
664 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
665 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
666 if (nfss->flags & nfs_infop->flag)
667 seq_puts(m, nfs_infop->str);
668 else
669 seq_puts(m, nfs_infop->nostr);
671 seq_printf(m, ",proto=%s",
672 rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
673 if (version == 4) {
674 if (nfss->port != NFS_PORT)
675 seq_printf(m, ",port=%u", nfss->port);
676 } else
677 if (nfss->port)
678 seq_printf(m, ",port=%u", nfss->port);
680 seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
681 seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
682 seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
684 if (version != 4)
685 nfs_show_mountd_options(m, nfss, showdefaults);
686 else
687 nfs_show_nfsv4_options(m, nfss, showdefaults);
689 if (nfss->options & NFS_OPTION_FSCACHE)
690 seq_printf(m, ",fsc");
692 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
693 if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
694 seq_printf(m, ",lookupcache=none");
695 else
696 seq_printf(m, ",lookupcache=pos");
699 local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
700 local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
702 if (!local_flock && !local_fcntl)
703 seq_printf(m, ",local_lock=none");
704 else if (local_flock && local_fcntl)
705 seq_printf(m, ",local_lock=all");
706 else if (local_flock)
707 seq_printf(m, ",local_lock=flock");
708 else
709 seq_printf(m, ",local_lock=posix");
713 * Describe the mount options on this VFS mountpoint
715 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
717 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
719 nfs_show_mount_options(m, nfss, 0);
721 seq_printf(m, ",addr=%s",
722 rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
723 RPC_DISPLAY_ADDR));
725 return 0;
729 * Present statistical information for this VFS mountpoint
731 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
733 int i, cpu;
734 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
735 struct rpc_auth *auth = nfss->client->cl_auth;
736 struct nfs_iostats totals = { };
738 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
741 * Display all mount option settings
743 seq_printf(m, "\n\topts:\t");
744 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
745 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
746 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
747 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
748 nfs_show_mount_options(m, nfss, 1);
750 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
752 seq_printf(m, "\n\tcaps:\t");
753 seq_printf(m, "caps=0x%x", nfss->caps);
754 seq_printf(m, ",wtmult=%u", nfss->wtmult);
755 seq_printf(m, ",dtsize=%u", nfss->dtsize);
756 seq_printf(m, ",bsize=%u", nfss->bsize);
757 seq_printf(m, ",namlen=%u", nfss->namelen);
759 #ifdef CONFIG_NFS_V4
760 if (nfss->nfs_client->rpc_ops->version == 4) {
761 seq_printf(m, "\n\tnfsv4:\t");
762 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
763 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
764 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
766 #endif
769 * Display security flavor in effect for this mount
771 seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
772 if (auth->au_flavor)
773 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
776 * Display superblock I/O counters
778 for_each_possible_cpu(cpu) {
779 struct nfs_iostats *stats;
781 preempt_disable();
782 stats = per_cpu_ptr(nfss->io_stats, cpu);
784 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
785 totals.events[i] += stats->events[i];
786 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
787 totals.bytes[i] += stats->bytes[i];
788 #ifdef CONFIG_NFS_FSCACHE
789 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
790 totals.fscache[i] += stats->fscache[i];
791 #endif
793 preempt_enable();
796 seq_printf(m, "\n\tevents:\t");
797 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
798 seq_printf(m, "%lu ", totals.events[i]);
799 seq_printf(m, "\n\tbytes:\t");
800 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
801 seq_printf(m, "%Lu ", totals.bytes[i]);
802 #ifdef CONFIG_NFS_FSCACHE
803 if (nfss->options & NFS_OPTION_FSCACHE) {
804 seq_printf(m, "\n\tfsc:\t");
805 for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
806 seq_printf(m, "%Lu ", totals.bytes[i]);
808 #endif
809 seq_printf(m, "\n");
811 rpc_print_iostats(m, nfss->client);
813 return 0;
817 * Begin unmount by attempting to remove all automounted mountpoints we added
818 * in response to xdev traversals and referrals
820 static void nfs_umount_begin(struct super_block *sb)
822 struct nfs_server *server;
823 struct rpc_clnt *rpc;
825 server = NFS_SB(sb);
826 /* -EIO all pending I/O */
827 rpc = server->client_acl;
828 if (!IS_ERR(rpc))
829 rpc_killall_tasks(rpc);
830 rpc = server->client;
831 if (!IS_ERR(rpc))
832 rpc_killall_tasks(rpc);
835 static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int version)
837 struct nfs_parsed_mount_data *data;
839 data = kzalloc(sizeof(*data), GFP_KERNEL);
840 if (data) {
841 data->acregmin = NFS_DEF_ACREGMIN;
842 data->acregmax = NFS_DEF_ACREGMAX;
843 data->acdirmin = NFS_DEF_ACDIRMIN;
844 data->acdirmax = NFS_DEF_ACDIRMAX;
845 data->mount_server.port = NFS_UNSPEC_PORT;
846 data->nfs_server.port = NFS_UNSPEC_PORT;
847 data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
848 data->auth_flavors[0] = RPC_AUTH_UNIX;
849 data->auth_flavor_len = 1;
850 data->version = version;
851 data->minorversion = 0;
853 return data;
857 * Sanity-check a server address provided by the mount command.
859 * Address family must be initialized, and address must not be
860 * the ANY address for that family.
862 static int nfs_verify_server_address(struct sockaddr *addr)
864 switch (addr->sa_family) {
865 case AF_INET: {
866 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
867 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
869 case AF_INET6: {
870 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
871 return !ipv6_addr_any(sa);
875 dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
876 return 0;
880 * Select between a default port value and a user-specified port value.
881 * If a zero value is set, then autobind will be used.
883 static void nfs_set_port(struct sockaddr *sap, int *port,
884 const unsigned short default_port)
886 if (*port == NFS_UNSPEC_PORT)
887 *port = default_port;
889 rpc_set_port(sap, *port);
893 * Sanity check the NFS transport protocol.
896 static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
898 switch (mnt->nfs_server.protocol) {
899 case XPRT_TRANSPORT_UDP:
900 case XPRT_TRANSPORT_TCP:
901 case XPRT_TRANSPORT_RDMA:
902 break;
903 default:
904 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
909 * For text based NFSv2/v3 mounts, the mount protocol transport default
910 * settings should depend upon the specified NFS transport.
912 static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
914 nfs_validate_transport_protocol(mnt);
916 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
917 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
918 return;
919 switch (mnt->nfs_server.protocol) {
920 case XPRT_TRANSPORT_UDP:
921 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
922 break;
923 case XPRT_TRANSPORT_TCP:
924 case XPRT_TRANSPORT_RDMA:
925 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
930 * Parse the value of the 'sec=' option.
932 static int nfs_parse_security_flavors(char *value,
933 struct nfs_parsed_mount_data *mnt)
935 substring_t args[MAX_OPT_ARGS];
937 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
939 switch (match_token(value, nfs_secflavor_tokens, args)) {
940 case Opt_sec_none:
941 mnt->auth_flavors[0] = RPC_AUTH_NULL;
942 break;
943 case Opt_sec_sys:
944 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
945 break;
946 case Opt_sec_krb5:
947 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
948 break;
949 case Opt_sec_krb5i:
950 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
951 break;
952 case Opt_sec_krb5p:
953 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
954 break;
955 case Opt_sec_lkey:
956 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
957 break;
958 case Opt_sec_lkeyi:
959 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
960 break;
961 case Opt_sec_lkeyp:
962 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
963 break;
964 case Opt_sec_spkm:
965 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
966 break;
967 case Opt_sec_spkmi:
968 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
969 break;
970 case Opt_sec_spkmp:
971 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
972 break;
973 default:
974 return 0;
977 mnt->auth_flavor_len = 1;
978 return 1;
982 * Error-check and convert a string of mount options from user space into
983 * a data structure. The whole mount string is processed; bad options are
984 * skipped as they are encountered. If there were no errors, return 1;
985 * otherwise return 0 (zero).
987 static int nfs_parse_mount_options(char *raw,
988 struct nfs_parsed_mount_data *mnt)
990 char *p, *string, *secdata;
991 int rc, sloppy = 0, invalid_option = 0;
992 unsigned short protofamily = AF_UNSPEC;
993 unsigned short mountfamily = AF_UNSPEC;
995 if (!raw) {
996 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
997 return 1;
999 dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
1001 secdata = alloc_secdata();
1002 if (!secdata)
1003 goto out_nomem;
1005 rc = security_sb_copy_data(raw, secdata);
1006 if (rc)
1007 goto out_security_failure;
1009 rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
1010 if (rc)
1011 goto out_security_failure;
1013 free_secdata(secdata);
1015 while ((p = strsep(&raw, ",")) != NULL) {
1016 substring_t args[MAX_OPT_ARGS];
1017 unsigned long option;
1018 int token;
1020 if (!*p)
1021 continue;
1023 dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
1025 token = match_token(p, nfs_mount_option_tokens, args);
1026 switch (token) {
1029 * boolean options: foo/nofoo
1031 case Opt_soft:
1032 mnt->flags |= NFS_MOUNT_SOFT;
1033 break;
1034 case Opt_hard:
1035 mnt->flags &= ~NFS_MOUNT_SOFT;
1036 break;
1037 case Opt_posix:
1038 mnt->flags |= NFS_MOUNT_POSIX;
1039 break;
1040 case Opt_noposix:
1041 mnt->flags &= ~NFS_MOUNT_POSIX;
1042 break;
1043 case Opt_cto:
1044 mnt->flags &= ~NFS_MOUNT_NOCTO;
1045 break;
1046 case Opt_nocto:
1047 mnt->flags |= NFS_MOUNT_NOCTO;
1048 break;
1049 case Opt_ac:
1050 mnt->flags &= ~NFS_MOUNT_NOAC;
1051 break;
1052 case Opt_noac:
1053 mnt->flags |= NFS_MOUNT_NOAC;
1054 break;
1055 case Opt_lock:
1056 mnt->flags &= ~NFS_MOUNT_NONLM;
1057 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1058 NFS_MOUNT_LOCAL_FCNTL);
1059 break;
1060 case Opt_nolock:
1061 mnt->flags |= NFS_MOUNT_NONLM;
1062 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1063 NFS_MOUNT_LOCAL_FCNTL);
1064 break;
1065 case Opt_v2:
1066 mnt->flags &= ~NFS_MOUNT_VER3;
1067 mnt->version = 2;
1068 break;
1069 case Opt_v3:
1070 mnt->flags |= NFS_MOUNT_VER3;
1071 mnt->version = 3;
1072 break;
1073 #ifdef CONFIG_NFS_V4
1074 case Opt_v4:
1075 mnt->flags &= ~NFS_MOUNT_VER3;
1076 mnt->version = 4;
1077 break;
1078 #endif
1079 case Opt_udp:
1080 mnt->flags &= ~NFS_MOUNT_TCP;
1081 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1082 break;
1083 case Opt_tcp:
1084 mnt->flags |= NFS_MOUNT_TCP;
1085 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1086 break;
1087 case Opt_rdma:
1088 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1089 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1090 xprt_load_transport(p);
1091 break;
1092 case Opt_acl:
1093 mnt->flags &= ~NFS_MOUNT_NOACL;
1094 break;
1095 case Opt_noacl:
1096 mnt->flags |= NFS_MOUNT_NOACL;
1097 break;
1098 case Opt_rdirplus:
1099 mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1100 break;
1101 case Opt_nordirplus:
1102 mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1103 break;
1104 case Opt_sharecache:
1105 mnt->flags &= ~NFS_MOUNT_UNSHARED;
1106 break;
1107 case Opt_nosharecache:
1108 mnt->flags |= NFS_MOUNT_UNSHARED;
1109 break;
1110 case Opt_resvport:
1111 mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1112 break;
1113 case Opt_noresvport:
1114 mnt->flags |= NFS_MOUNT_NORESVPORT;
1115 break;
1116 case Opt_fscache:
1117 mnt->options |= NFS_OPTION_FSCACHE;
1118 kfree(mnt->fscache_uniq);
1119 mnt->fscache_uniq = NULL;
1120 break;
1121 case Opt_nofscache:
1122 mnt->options &= ~NFS_OPTION_FSCACHE;
1123 kfree(mnt->fscache_uniq);
1124 mnt->fscache_uniq = NULL;
1125 break;
1128 * options that take numeric values
1130 case Opt_port:
1131 string = match_strdup(args);
1132 if (string == NULL)
1133 goto out_nomem;
1134 rc = strict_strtoul(string, 10, &option);
1135 kfree(string);
1136 if (rc != 0 || option > USHRT_MAX)
1137 goto out_invalid_value;
1138 mnt->nfs_server.port = option;
1139 break;
1140 case Opt_rsize:
1141 string = match_strdup(args);
1142 if (string == NULL)
1143 goto out_nomem;
1144 rc = strict_strtoul(string, 10, &option);
1145 kfree(string);
1146 if (rc != 0)
1147 goto out_invalid_value;
1148 mnt->rsize = option;
1149 break;
1150 case Opt_wsize:
1151 string = match_strdup(args);
1152 if (string == NULL)
1153 goto out_nomem;
1154 rc = strict_strtoul(string, 10, &option);
1155 kfree(string);
1156 if (rc != 0)
1157 goto out_invalid_value;
1158 mnt->wsize = option;
1159 break;
1160 case Opt_bsize:
1161 string = match_strdup(args);
1162 if (string == NULL)
1163 goto out_nomem;
1164 rc = strict_strtoul(string, 10, &option);
1165 kfree(string);
1166 if (rc != 0)
1167 goto out_invalid_value;
1168 mnt->bsize = option;
1169 break;
1170 case Opt_timeo:
1171 string = match_strdup(args);
1172 if (string == NULL)
1173 goto out_nomem;
1174 rc = strict_strtoul(string, 10, &option);
1175 kfree(string);
1176 if (rc != 0 || option == 0)
1177 goto out_invalid_value;
1178 mnt->timeo = option;
1179 break;
1180 case Opt_retrans:
1181 string = match_strdup(args);
1182 if (string == NULL)
1183 goto out_nomem;
1184 rc = strict_strtoul(string, 10, &option);
1185 kfree(string);
1186 if (rc != 0 || option == 0)
1187 goto out_invalid_value;
1188 mnt->retrans = option;
1189 break;
1190 case Opt_acregmin:
1191 string = match_strdup(args);
1192 if (string == NULL)
1193 goto out_nomem;
1194 rc = strict_strtoul(string, 10, &option);
1195 kfree(string);
1196 if (rc != 0)
1197 goto out_invalid_value;
1198 mnt->acregmin = option;
1199 break;
1200 case Opt_acregmax:
1201 string = match_strdup(args);
1202 if (string == NULL)
1203 goto out_nomem;
1204 rc = strict_strtoul(string, 10, &option);
1205 kfree(string);
1206 if (rc != 0)
1207 goto out_invalid_value;
1208 mnt->acregmax = option;
1209 break;
1210 case Opt_acdirmin:
1211 string = match_strdup(args);
1212 if (string == NULL)
1213 goto out_nomem;
1214 rc = strict_strtoul(string, 10, &option);
1215 kfree(string);
1216 if (rc != 0)
1217 goto out_invalid_value;
1218 mnt->acdirmin = option;
1219 break;
1220 case Opt_acdirmax:
1221 string = match_strdup(args);
1222 if (string == NULL)
1223 goto out_nomem;
1224 rc = strict_strtoul(string, 10, &option);
1225 kfree(string);
1226 if (rc != 0)
1227 goto out_invalid_value;
1228 mnt->acdirmax = option;
1229 break;
1230 case Opt_actimeo:
1231 string = match_strdup(args);
1232 if (string == NULL)
1233 goto out_nomem;
1234 rc = strict_strtoul(string, 10, &option);
1235 kfree(string);
1236 if (rc != 0)
1237 goto out_invalid_value;
1238 mnt->acregmin = mnt->acregmax =
1239 mnt->acdirmin = mnt->acdirmax = option;
1240 break;
1241 case Opt_namelen:
1242 string = match_strdup(args);
1243 if (string == NULL)
1244 goto out_nomem;
1245 rc = strict_strtoul(string, 10, &option);
1246 kfree(string);
1247 if (rc != 0)
1248 goto out_invalid_value;
1249 mnt->namlen = option;
1250 break;
1251 case Opt_mountport:
1252 string = match_strdup(args);
1253 if (string == NULL)
1254 goto out_nomem;
1255 rc = strict_strtoul(string, 10, &option);
1256 kfree(string);
1257 if (rc != 0 || option > USHRT_MAX)
1258 goto out_invalid_value;
1259 mnt->mount_server.port = option;
1260 break;
1261 case Opt_mountvers:
1262 string = match_strdup(args);
1263 if (string == NULL)
1264 goto out_nomem;
1265 rc = strict_strtoul(string, 10, &option);
1266 kfree(string);
1267 if (rc != 0 ||
1268 option < NFS_MNT_VERSION ||
1269 option > NFS_MNT3_VERSION)
1270 goto out_invalid_value;
1271 mnt->mount_server.version = option;
1272 break;
1273 case Opt_nfsvers:
1274 string = match_strdup(args);
1275 if (string == NULL)
1276 goto out_nomem;
1277 rc = strict_strtoul(string, 10, &option);
1278 kfree(string);
1279 if (rc != 0)
1280 goto out_invalid_value;
1281 switch (option) {
1282 case NFS2_VERSION:
1283 mnt->flags &= ~NFS_MOUNT_VER3;
1284 mnt->version = 2;
1285 break;
1286 case NFS3_VERSION:
1287 mnt->flags |= NFS_MOUNT_VER3;
1288 mnt->version = 3;
1289 break;
1290 #ifdef CONFIG_NFS_V4
1291 case NFS4_VERSION:
1292 mnt->flags &= ~NFS_MOUNT_VER3;
1293 mnt->version = 4;
1294 break;
1295 #endif
1296 default:
1297 goto out_invalid_value;
1299 break;
1300 case Opt_minorversion:
1301 string = match_strdup(args);
1302 if (string == NULL)
1303 goto out_nomem;
1304 rc = strict_strtoul(string, 10, &option);
1305 kfree(string);
1306 if (rc != 0)
1307 goto out_invalid_value;
1308 if (option > NFS4_MAX_MINOR_VERSION)
1309 goto out_invalid_value;
1310 mnt->minorversion = option;
1311 break;
1314 * options that take text values
1316 case Opt_sec:
1317 string = match_strdup(args);
1318 if (string == NULL)
1319 goto out_nomem;
1320 rc = nfs_parse_security_flavors(string, mnt);
1321 kfree(string);
1322 if (!rc) {
1323 dfprintk(MOUNT, "NFS: unrecognized "
1324 "security flavor\n");
1325 return 0;
1327 break;
1328 case Opt_proto:
1329 string = match_strdup(args);
1330 if (string == NULL)
1331 goto out_nomem;
1332 token = match_token(string,
1333 nfs_xprt_protocol_tokens, args);
1335 protofamily = AF_INET;
1336 switch (token) {
1337 case Opt_xprt_udp6:
1338 protofamily = AF_INET6;
1339 case Opt_xprt_udp:
1340 mnt->flags &= ~NFS_MOUNT_TCP;
1341 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1342 kfree(string);
1343 break;
1344 case Opt_xprt_tcp6:
1345 protofamily = AF_INET6;
1346 case Opt_xprt_tcp:
1347 mnt->flags |= NFS_MOUNT_TCP;
1348 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1349 kfree(string);
1350 break;
1351 case Opt_xprt_rdma:
1352 /* vector side protocols to TCP */
1353 mnt->flags |= NFS_MOUNT_TCP;
1354 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1355 xprt_load_transport(string);
1356 kfree(string);
1357 break;
1358 default:
1359 dfprintk(MOUNT, "NFS: unrecognized "
1360 "transport protocol\n");
1361 kfree(string);
1362 return 0;
1364 break;
1365 case Opt_mountproto:
1366 string = match_strdup(args);
1367 if (string == NULL)
1368 goto out_nomem;
1369 token = match_token(string,
1370 nfs_xprt_protocol_tokens, args);
1371 kfree(string);
1373 mountfamily = AF_INET;
1374 switch (token) {
1375 case Opt_xprt_udp6:
1376 mountfamily = AF_INET6;
1377 case Opt_xprt_udp:
1378 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1379 break;
1380 case Opt_xprt_tcp6:
1381 mountfamily = AF_INET6;
1382 case Opt_xprt_tcp:
1383 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1384 break;
1385 case Opt_xprt_rdma: /* not used for side protocols */
1386 default:
1387 dfprintk(MOUNT, "NFS: unrecognized "
1388 "transport protocol\n");
1389 return 0;
1391 break;
1392 case Opt_addr:
1393 string = match_strdup(args);
1394 if (string == NULL)
1395 goto out_nomem;
1396 mnt->nfs_server.addrlen =
1397 rpc_pton(string, strlen(string),
1398 (struct sockaddr *)
1399 &mnt->nfs_server.address,
1400 sizeof(mnt->nfs_server.address));
1401 kfree(string);
1402 if (mnt->nfs_server.addrlen == 0)
1403 goto out_invalid_address;
1404 break;
1405 case Opt_clientaddr:
1406 string = match_strdup(args);
1407 if (string == NULL)
1408 goto out_nomem;
1409 kfree(mnt->client_address);
1410 mnt->client_address = string;
1411 break;
1412 case Opt_mounthost:
1413 string = match_strdup(args);
1414 if (string == NULL)
1415 goto out_nomem;
1416 kfree(mnt->mount_server.hostname);
1417 mnt->mount_server.hostname = string;
1418 break;
1419 case Opt_mountaddr:
1420 string = match_strdup(args);
1421 if (string == NULL)
1422 goto out_nomem;
1423 mnt->mount_server.addrlen =
1424 rpc_pton(string, strlen(string),
1425 (struct sockaddr *)
1426 &mnt->mount_server.address,
1427 sizeof(mnt->mount_server.address));
1428 kfree(string);
1429 if (mnt->mount_server.addrlen == 0)
1430 goto out_invalid_address;
1431 break;
1432 case Opt_lookupcache:
1433 string = match_strdup(args);
1434 if (string == NULL)
1435 goto out_nomem;
1436 token = match_token(string,
1437 nfs_lookupcache_tokens, args);
1438 kfree(string);
1439 switch (token) {
1440 case Opt_lookupcache_all:
1441 mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1442 break;
1443 case Opt_lookupcache_positive:
1444 mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1445 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1446 break;
1447 case Opt_lookupcache_none:
1448 mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1449 break;
1450 default:
1451 dfprintk(MOUNT, "NFS: invalid "
1452 "lookupcache argument\n");
1453 return 0;
1455 break;
1456 case Opt_fscache_uniq:
1457 string = match_strdup(args);
1458 if (string == NULL)
1459 goto out_nomem;
1460 kfree(mnt->fscache_uniq);
1461 mnt->fscache_uniq = string;
1462 mnt->options |= NFS_OPTION_FSCACHE;
1463 break;
1464 case Opt_local_lock:
1465 string = match_strdup(args);
1466 if (string == NULL)
1467 goto out_nomem;
1468 token = match_token(string, nfs_local_lock_tokens,
1469 args);
1470 kfree(string);
1471 switch (token) {
1472 case Opt_local_lock_all:
1473 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1474 NFS_MOUNT_LOCAL_FCNTL);
1475 break;
1476 case Opt_local_lock_flock:
1477 mnt->flags |= NFS_MOUNT_LOCAL_FLOCK;
1478 break;
1479 case Opt_local_lock_posix:
1480 mnt->flags |= NFS_MOUNT_LOCAL_FCNTL;
1481 break;
1482 case Opt_local_lock_none:
1483 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1484 NFS_MOUNT_LOCAL_FCNTL);
1485 break;
1486 default:
1487 dfprintk(MOUNT, "NFS: invalid "
1488 "local_lock argument\n");
1489 return 0;
1491 break;
1494 * Special options
1496 case Opt_sloppy:
1497 sloppy = 1;
1498 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1499 break;
1500 case Opt_userspace:
1501 case Opt_deprecated:
1502 dfprintk(MOUNT, "NFS: ignoring mount option "
1503 "'%s'\n", p);
1504 break;
1506 default:
1507 invalid_option = 1;
1508 dfprintk(MOUNT, "NFS: unrecognized mount option "
1509 "'%s'\n", p);
1513 if (!sloppy && invalid_option)
1514 return 0;
1517 * verify that any proto=/mountproto= options match the address
1518 * familiies in the addr=/mountaddr= options.
1520 if (protofamily != AF_UNSPEC &&
1521 protofamily != mnt->nfs_server.address.ss_family)
1522 goto out_proto_mismatch;
1524 if (mountfamily != AF_UNSPEC) {
1525 if (mnt->mount_server.addrlen) {
1526 if (mountfamily != mnt->mount_server.address.ss_family)
1527 goto out_mountproto_mismatch;
1528 } else {
1529 if (mountfamily != mnt->nfs_server.address.ss_family)
1530 goto out_mountproto_mismatch;
1534 return 1;
1536 out_mountproto_mismatch:
1537 printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1538 "option\n");
1539 return 0;
1540 out_proto_mismatch:
1541 printk(KERN_INFO "NFS: server address does not match proto= option\n");
1542 return 0;
1543 out_invalid_address:
1544 printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1545 return 0;
1546 out_invalid_value:
1547 printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
1548 return 0;
1549 out_nomem:
1550 printk(KERN_INFO "NFS: not enough memory to parse option\n");
1551 return 0;
1552 out_security_failure:
1553 free_secdata(secdata);
1554 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1555 return 0;
1559 * Match the requested auth flavors with the list returned by
1560 * the server. Returns zero and sets the mount's authentication
1561 * flavor on success; returns -EACCES if server does not support
1562 * the requested flavor.
1564 static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1565 struct nfs_mount_request *request)
1567 unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1570 * Certain releases of Linux's mountd return an empty
1571 * flavor list. To prevent behavioral regression with
1572 * these servers (ie. rejecting mounts that used to
1573 * succeed), revert to pre-2.6.32 behavior (no checking)
1574 * if the returned flavor list is empty.
1576 if (server_authlist_len == 0)
1577 return 0;
1580 * We avoid sophisticated negotiating here, as there are
1581 * plenty of cases where we can get it wrong, providing
1582 * either too little or too much security.
1584 * RFC 2623, section 2.7 suggests we SHOULD prefer the
1585 * flavor listed first. However, some servers list
1586 * AUTH_NULL first. Our caller plants AUTH_SYS, the
1587 * preferred default, in args->auth_flavors[0] if user
1588 * didn't specify sec= mount option.
1590 for (i = 0; i < args->auth_flavor_len; i++)
1591 for (j = 0; j < server_authlist_len; j++)
1592 if (args->auth_flavors[i] == request->auth_flavs[j]) {
1593 dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1594 request->auth_flavs[j]);
1595 args->auth_flavors[0] = request->auth_flavs[j];
1596 return 0;
1599 dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1600 nfs_umount(request);
1601 return -EACCES;
1605 * Use the remote server's MOUNT service to request the NFS file handle
1606 * corresponding to the provided path.
1608 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1609 struct nfs_fh *root_fh)
1611 rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1612 unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
1613 struct nfs_mount_request request = {
1614 .sap = (struct sockaddr *)
1615 &args->mount_server.address,
1616 .dirpath = args->nfs_server.export_path,
1617 .protocol = args->mount_server.protocol,
1618 .fh = root_fh,
1619 .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
1620 .auth_flav_len = &server_authlist_len,
1621 .auth_flavs = server_authlist,
1623 int status;
1625 if (args->mount_server.version == 0) {
1626 switch (args->version) {
1627 default:
1628 args->mount_server.version = NFS_MNT3_VERSION;
1629 break;
1630 case 2:
1631 args->mount_server.version = NFS_MNT_VERSION;
1634 request.version = args->mount_server.version;
1636 if (args->mount_server.hostname)
1637 request.hostname = args->mount_server.hostname;
1638 else
1639 request.hostname = args->nfs_server.hostname;
1642 * Construct the mount server's address.
1644 if (args->mount_server.address.ss_family == AF_UNSPEC) {
1645 memcpy(request.sap, &args->nfs_server.address,
1646 args->nfs_server.addrlen);
1647 args->mount_server.addrlen = args->nfs_server.addrlen;
1649 request.salen = args->mount_server.addrlen;
1650 nfs_set_port(request.sap, &args->mount_server.port, 0);
1653 * Now ask the mount server to map our export path
1654 * to a file handle.
1656 status = nfs_mount(&request);
1657 if (status != 0) {
1658 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1659 request.hostname, status);
1660 return status;
1664 * MNTv1 (NFSv2) does not support auth flavor negotiation.
1666 if (args->mount_server.version != NFS_MNT3_VERSION)
1667 return 0;
1668 return nfs_walk_authlist(args, &request);
1671 static int nfs_parse_simple_hostname(const char *dev_name,
1672 char **hostname, size_t maxnamlen,
1673 char **export_path, size_t maxpathlen)
1675 size_t len;
1676 char *colon, *comma;
1678 colon = strchr(dev_name, ':');
1679 if (colon == NULL)
1680 goto out_bad_devname;
1682 len = colon - dev_name;
1683 if (len > maxnamlen)
1684 goto out_hostname;
1686 /* N.B. caller will free nfs_server.hostname in all cases */
1687 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1688 if (!*hostname)
1689 goto out_nomem;
1691 /* kill possible hostname list: not supported */
1692 comma = strchr(*hostname, ',');
1693 if (comma != NULL) {
1694 if (comma == *hostname)
1695 goto out_bad_devname;
1696 *comma = '\0';
1699 colon++;
1700 len = strlen(colon);
1701 if (len > maxpathlen)
1702 goto out_path;
1703 *export_path = kstrndup(colon, len, GFP_KERNEL);
1704 if (!*export_path)
1705 goto out_nomem;
1707 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1708 return 0;
1710 out_bad_devname:
1711 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1712 return -EINVAL;
1714 out_nomem:
1715 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1716 return -ENOMEM;
1718 out_hostname:
1719 dfprintk(MOUNT, "NFS: server hostname too long\n");
1720 return -ENAMETOOLONG;
1722 out_path:
1723 dfprintk(MOUNT, "NFS: export pathname too long\n");
1724 return -ENAMETOOLONG;
1728 * Hostname has square brackets around it because it contains one or
1729 * more colons. We look for the first closing square bracket, and a
1730 * colon must follow it.
1732 static int nfs_parse_protected_hostname(const char *dev_name,
1733 char **hostname, size_t maxnamlen,
1734 char **export_path, size_t maxpathlen)
1736 size_t len;
1737 char *start, *end;
1739 start = (char *)(dev_name + 1);
1741 end = strchr(start, ']');
1742 if (end == NULL)
1743 goto out_bad_devname;
1744 if (*(end + 1) != ':')
1745 goto out_bad_devname;
1747 len = end - start;
1748 if (len > maxnamlen)
1749 goto out_hostname;
1751 /* N.B. caller will free nfs_server.hostname in all cases */
1752 *hostname = kstrndup(start, len, GFP_KERNEL);
1753 if (*hostname == NULL)
1754 goto out_nomem;
1756 end += 2;
1757 len = strlen(end);
1758 if (len > maxpathlen)
1759 goto out_path;
1760 *export_path = kstrndup(end, len, GFP_KERNEL);
1761 if (!*export_path)
1762 goto out_nomem;
1764 return 0;
1766 out_bad_devname:
1767 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1768 return -EINVAL;
1770 out_nomem:
1771 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1772 return -ENOMEM;
1774 out_hostname:
1775 dfprintk(MOUNT, "NFS: server hostname too long\n");
1776 return -ENAMETOOLONG;
1778 out_path:
1779 dfprintk(MOUNT, "NFS: export pathname too long\n");
1780 return -ENAMETOOLONG;
1784 * Split "dev_name" into "hostname:export_path".
1786 * The leftmost colon demarks the split between the server's hostname
1787 * and the export path. If the hostname starts with a left square
1788 * bracket, then it may contain colons.
1790 * Note: caller frees hostname and export path, even on error.
1792 static int nfs_parse_devname(const char *dev_name,
1793 char **hostname, size_t maxnamlen,
1794 char **export_path, size_t maxpathlen)
1796 if (*dev_name == '[')
1797 return nfs_parse_protected_hostname(dev_name,
1798 hostname, maxnamlen,
1799 export_path, maxpathlen);
1801 return nfs_parse_simple_hostname(dev_name,
1802 hostname, maxnamlen,
1803 export_path, maxpathlen);
1807 * Validate the NFS2/NFS3 mount data
1808 * - fills in the mount root filehandle
1810 * For option strings, user space handles the following behaviors:
1812 * + DNS: mapping server host name to IP address ("addr=" option)
1814 * + failure mode: how to behave if a mount request can't be handled
1815 * immediately ("fg/bg" option)
1817 * + retry: how often to retry a mount request ("retry=" option)
1819 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1820 * mountproto=tcp after mountproto=udp, and so on
1822 static int nfs_validate_mount_data(void *options,
1823 struct nfs_parsed_mount_data *args,
1824 struct nfs_fh *mntfh,
1825 const char *dev_name)
1827 struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1828 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
1830 if (data == NULL)
1831 goto out_no_data;
1833 switch (data->version) {
1834 case 1:
1835 data->namlen = 0;
1836 case 2:
1837 data->bsize = 0;
1838 case 3:
1839 if (data->flags & NFS_MOUNT_VER3)
1840 goto out_no_v3;
1841 data->root.size = NFS2_FHSIZE;
1842 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1843 case 4:
1844 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1845 goto out_no_sec;
1846 case 5:
1847 memset(data->context, 0, sizeof(data->context));
1848 case 6:
1849 if (data->flags & NFS_MOUNT_VER3) {
1850 if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1851 goto out_invalid_fh;
1852 mntfh->size = data->root.size;
1853 args->version = 3;
1854 } else {
1855 mntfh->size = NFS2_FHSIZE;
1856 args->version = 2;
1860 memcpy(mntfh->data, data->root.data, mntfh->size);
1861 if (mntfh->size < sizeof(mntfh->data))
1862 memset(mntfh->data + mntfh->size, 0,
1863 sizeof(mntfh->data) - mntfh->size);
1866 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1867 * can deal with.
1869 args->flags = data->flags & NFS_MOUNT_FLAGMASK;
1870 args->flags |= NFS_MOUNT_LEGACY_INTERFACE;
1871 args->rsize = data->rsize;
1872 args->wsize = data->wsize;
1873 args->timeo = data->timeo;
1874 args->retrans = data->retrans;
1875 args->acregmin = data->acregmin;
1876 args->acregmax = data->acregmax;
1877 args->acdirmin = data->acdirmin;
1878 args->acdirmax = data->acdirmax;
1880 memcpy(sap, &data->addr, sizeof(data->addr));
1881 args->nfs_server.addrlen = sizeof(data->addr);
1882 if (!nfs_verify_server_address(sap))
1883 goto out_no_address;
1885 if (!(data->flags & NFS_MOUNT_TCP))
1886 args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1887 /* N.B. caller will free nfs_server.hostname in all cases */
1888 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1889 args->namlen = data->namlen;
1890 args->bsize = data->bsize;
1892 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1893 args->auth_flavors[0] = data->pseudoflavor;
1894 if (!args->nfs_server.hostname)
1895 goto out_nomem;
1897 if (!(data->flags & NFS_MOUNT_NONLM))
1898 args->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1899 NFS_MOUNT_LOCAL_FCNTL);
1900 else
1901 args->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1902 NFS_MOUNT_LOCAL_FCNTL);
1904 * The legacy version 6 binary mount data from userspace has a
1905 * field used only to transport selinux information into the
1906 * the kernel. To continue to support that functionality we
1907 * have a touch of selinux knowledge here in the NFS code. The
1908 * userspace code converted context=blah to just blah so we are
1909 * converting back to the full string selinux understands.
1911 if (data->context[0]){
1912 #ifdef CONFIG_SECURITY_SELINUX
1913 int rc;
1914 char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1915 if (!opts_str)
1916 return -ENOMEM;
1917 strcpy(opts_str, "context=");
1918 data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1919 strcat(opts_str, &data->context[0]);
1920 rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1921 kfree(opts_str);
1922 if (rc)
1923 return rc;
1924 #else
1925 return -EINVAL;
1926 #endif
1929 break;
1930 default: {
1931 int status;
1933 if (nfs_parse_mount_options((char *)options, args) == 0)
1934 return -EINVAL;
1936 if (!nfs_verify_server_address(sap))
1937 goto out_no_address;
1939 if (args->version == 4)
1940 #ifdef CONFIG_NFS_V4
1941 return nfs4_validate_text_mount_data(options,
1942 args, dev_name);
1943 #else
1944 goto out_v4_not_compiled;
1945 #endif
1947 nfs_set_port(sap, &args->nfs_server.port, 0);
1949 nfs_set_mount_transport_protocol(args);
1951 status = nfs_parse_devname(dev_name,
1952 &args->nfs_server.hostname,
1953 PAGE_SIZE,
1954 &args->nfs_server.export_path,
1955 NFS_MAXPATHLEN);
1956 if (!status)
1957 status = nfs_try_mount(args, mntfh);
1959 kfree(args->nfs_server.export_path);
1960 args->nfs_server.export_path = NULL;
1962 if (status)
1963 return status;
1965 break;
1969 #ifndef CONFIG_NFS_V3
1970 if (args->version == 3)
1971 goto out_v3_not_compiled;
1972 #endif /* !CONFIG_NFS_V3 */
1974 return 0;
1976 out_no_data:
1977 dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1978 return -EINVAL;
1980 out_no_v3:
1981 dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1982 data->version);
1983 return -EINVAL;
1985 out_no_sec:
1986 dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1987 return -EINVAL;
1989 #ifndef CONFIG_NFS_V3
1990 out_v3_not_compiled:
1991 dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1992 return -EPROTONOSUPPORT;
1993 #endif /* !CONFIG_NFS_V3 */
1995 #ifndef CONFIG_NFS_V4
1996 out_v4_not_compiled:
1997 dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
1998 return -EPROTONOSUPPORT;
1999 #endif /* !CONFIG_NFS_V4 */
2001 out_nomem:
2002 dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
2003 return -ENOMEM;
2005 out_no_address:
2006 dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
2007 return -EINVAL;
2009 out_invalid_fh:
2010 dfprintk(MOUNT, "NFS: invalid root filehandle\n");
2011 return -EINVAL;
2014 static int
2015 nfs_compare_remount_data(struct nfs_server *nfss,
2016 struct nfs_parsed_mount_data *data)
2018 if (data->flags != nfss->flags ||
2019 data->rsize != nfss->rsize ||
2020 data->wsize != nfss->wsize ||
2021 data->retrans != nfss->client->cl_timeout->to_retries ||
2022 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
2023 data->acregmin != nfss->acregmin / HZ ||
2024 data->acregmax != nfss->acregmax / HZ ||
2025 data->acdirmin != nfss->acdirmin / HZ ||
2026 data->acdirmax != nfss->acdirmax / HZ ||
2027 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
2028 data->nfs_server.port != nfss->port ||
2029 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
2030 !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
2031 (struct sockaddr *)&nfss->nfs_client->cl_addr))
2032 return -EINVAL;
2034 return 0;
2037 static int
2038 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
2040 int error;
2041 struct nfs_server *nfss = sb->s_fs_info;
2042 struct nfs_parsed_mount_data *data;
2043 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
2044 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
2045 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
2048 * Userspace mount programs that send binary options generally send
2049 * them populated with default values. We have no way to know which
2050 * ones were explicitly specified. Fall back to legacy behavior and
2051 * just return success.
2053 if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
2054 (nfsvers <= 3 && (!options || (options->version >= 1 &&
2055 options->version <= 6))))
2056 return 0;
2058 data = kzalloc(sizeof(*data), GFP_KERNEL);
2059 if (data == NULL)
2060 return -ENOMEM;
2062 /* fill out struct with values from existing mount */
2063 data->flags = nfss->flags;
2064 data->rsize = nfss->rsize;
2065 data->wsize = nfss->wsize;
2066 data->retrans = nfss->client->cl_timeout->to_retries;
2067 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
2068 data->acregmin = nfss->acregmin / HZ;
2069 data->acregmax = nfss->acregmax / HZ;
2070 data->acdirmin = nfss->acdirmin / HZ;
2071 data->acdirmax = nfss->acdirmax / HZ;
2072 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
2073 data->nfs_server.port = nfss->port;
2074 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
2075 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
2076 data->nfs_server.addrlen);
2078 /* overwrite those values with any that were specified */
2079 error = nfs_parse_mount_options((char *)options, data);
2080 if (error < 0)
2081 goto out;
2083 /* compare new mount options with old ones */
2084 error = nfs_compare_remount_data(nfss, data);
2085 out:
2086 kfree(data);
2087 return error;
2091 * Initialise the common bits of the superblock
2093 static inline void nfs_initialise_sb(struct super_block *sb)
2095 struct nfs_server *server = NFS_SB(sb);
2097 sb->s_magic = NFS_SUPER_MAGIC;
2099 /* We probably want something more informative here */
2100 snprintf(sb->s_id, sizeof(sb->s_id),
2101 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
2103 if (sb->s_blocksize == 0)
2104 sb->s_blocksize = nfs_block_bits(server->wsize,
2105 &sb->s_blocksize_bits);
2107 if (server->flags & NFS_MOUNT_NOAC)
2108 sb->s_flags |= MS_SYNCHRONOUS;
2110 sb->s_bdi = &server->backing_dev_info;
2112 nfs_super_set_maxbytes(sb, server->maxfilesize);
2116 * Finish setting up an NFS2/3 superblock
2118 static void nfs_fill_super(struct super_block *sb,
2119 struct nfs_parsed_mount_data *data)
2121 struct nfs_server *server = NFS_SB(sb);
2123 sb->s_blocksize_bits = 0;
2124 sb->s_blocksize = 0;
2125 if (data->bsize)
2126 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2128 if (server->nfs_client->rpc_ops->version == 3) {
2129 /* The VFS shouldn't apply the umask to mode bits. We will do
2130 * so ourselves when necessary.
2132 sb->s_flags |= MS_POSIXACL;
2133 sb->s_time_gran = 1;
2136 sb->s_op = &nfs_sops;
2137 nfs_initialise_sb(sb);
2141 * Finish setting up a cloned NFS2/3 superblock
2143 static void nfs_clone_super(struct super_block *sb,
2144 const struct super_block *old_sb)
2146 struct nfs_server *server = NFS_SB(sb);
2148 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2149 sb->s_blocksize = old_sb->s_blocksize;
2150 sb->s_maxbytes = old_sb->s_maxbytes;
2152 if (server->nfs_client->rpc_ops->version == 3) {
2153 /* The VFS shouldn't apply the umask to mode bits. We will do
2154 * so ourselves when necessary.
2156 sb->s_flags |= MS_POSIXACL;
2157 sb->s_time_gran = 1;
2160 sb->s_op = old_sb->s_op;
2161 nfs_initialise_sb(sb);
2164 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2166 const struct nfs_server *a = s->s_fs_info;
2167 const struct rpc_clnt *clnt_a = a->client;
2168 const struct rpc_clnt *clnt_b = b->client;
2170 if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2171 goto Ebusy;
2172 if (a->nfs_client != b->nfs_client)
2173 goto Ebusy;
2174 if (a->flags != b->flags)
2175 goto Ebusy;
2176 if (a->wsize != b->wsize)
2177 goto Ebusy;
2178 if (a->rsize != b->rsize)
2179 goto Ebusy;
2180 if (a->acregmin != b->acregmin)
2181 goto Ebusy;
2182 if (a->acregmax != b->acregmax)
2183 goto Ebusy;
2184 if (a->acdirmin != b->acdirmin)
2185 goto Ebusy;
2186 if (a->acdirmax != b->acdirmax)
2187 goto Ebusy;
2188 if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2189 goto Ebusy;
2190 return 1;
2191 Ebusy:
2192 return 0;
2195 struct nfs_sb_mountdata {
2196 struct nfs_server *server;
2197 int mntflags;
2200 static int nfs_set_super(struct super_block *s, void *data)
2202 struct nfs_sb_mountdata *sb_mntdata = data;
2203 struct nfs_server *server = sb_mntdata->server;
2204 int ret;
2206 s->s_flags = sb_mntdata->mntflags;
2207 s->s_fs_info = server;
2208 ret = set_anon_super(s, server);
2209 if (ret == 0)
2210 server->s_dev = s->s_dev;
2211 return ret;
2214 static int nfs_compare_super_address(struct nfs_server *server1,
2215 struct nfs_server *server2)
2217 struct sockaddr *sap1, *sap2;
2219 sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2220 sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2222 if (sap1->sa_family != sap2->sa_family)
2223 return 0;
2225 switch (sap1->sa_family) {
2226 case AF_INET: {
2227 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2228 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2229 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2230 return 0;
2231 if (sin1->sin_port != sin2->sin_port)
2232 return 0;
2233 break;
2235 case AF_INET6: {
2236 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2237 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2238 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2239 return 0;
2240 if (sin1->sin6_port != sin2->sin6_port)
2241 return 0;
2242 break;
2244 default:
2245 return 0;
2248 return 1;
2251 static int nfs_compare_super(struct super_block *sb, void *data)
2253 struct nfs_sb_mountdata *sb_mntdata = data;
2254 struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2255 int mntflags = sb_mntdata->mntflags;
2257 if (!nfs_compare_super_address(old, server))
2258 return 0;
2259 /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2260 if (old->flags & NFS_MOUNT_UNSHARED)
2261 return 0;
2262 if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2263 return 0;
2264 return nfs_compare_mount_options(sb, server, mntflags);
2267 static int nfs_bdi_register(struct nfs_server *server)
2269 return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2272 static int nfs_get_sb(struct file_system_type *fs_type,
2273 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2275 struct nfs_server *server = NULL;
2276 struct super_block *s;
2277 struct nfs_parsed_mount_data *data;
2278 struct nfs_fh *mntfh;
2279 struct dentry *mntroot;
2280 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2281 struct nfs_sb_mountdata sb_mntdata = {
2282 .mntflags = flags,
2284 int error = -ENOMEM;
2286 data = nfs_alloc_parsed_mount_data(NFS_DEFAULT_VERSION);
2287 mntfh = nfs_alloc_fhandle();
2288 if (data == NULL || mntfh == NULL)
2289 goto out_free_fh;
2291 security_init_mnt_opts(&data->lsm_opts);
2293 /* Validate the mount data */
2294 error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
2295 if (error < 0)
2296 goto out;
2298 #ifdef CONFIG_NFS_V4
2299 if (data->version == 4) {
2300 error = nfs4_try_mount(flags, dev_name, data, mnt);
2301 kfree(data->client_address);
2302 kfree(data->nfs_server.export_path);
2303 goto out;
2305 #endif /* CONFIG_NFS_V4 */
2307 /* Get a volume representation */
2308 server = nfs_create_server(data, mntfh);
2309 if (IS_ERR(server)) {
2310 error = PTR_ERR(server);
2311 goto out;
2313 sb_mntdata.server = server;
2315 if (server->flags & NFS_MOUNT_UNSHARED)
2316 compare_super = NULL;
2318 /* Get a superblock - note that we may end up sharing one that already exists */
2319 s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2320 if (IS_ERR(s)) {
2321 error = PTR_ERR(s);
2322 goto out_err_nosb;
2325 if (s->s_fs_info != server) {
2326 nfs_free_server(server);
2327 server = NULL;
2328 } else {
2329 error = nfs_bdi_register(server);
2330 if (error)
2331 goto error_splat_bdi;
2334 if (!s->s_root) {
2335 /* initial superblock/root creation */
2336 nfs_fill_super(s, data);
2337 nfs_fscache_get_super_cookie(
2338 s, data ? data->fscache_uniq : NULL, NULL);
2341 mntroot = nfs_get_root(s, mntfh);
2342 if (IS_ERR(mntroot)) {
2343 error = PTR_ERR(mntroot);
2344 goto error_splat_super;
2347 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2348 if (error)
2349 goto error_splat_root;
2351 s->s_flags |= MS_ACTIVE;
2352 mnt->mnt_sb = s;
2353 mnt->mnt_root = mntroot;
2354 error = 0;
2356 out:
2357 kfree(data->nfs_server.hostname);
2358 kfree(data->mount_server.hostname);
2359 kfree(data->fscache_uniq);
2360 security_free_mnt_opts(&data->lsm_opts);
2361 out_free_fh:
2362 nfs_free_fhandle(mntfh);
2363 kfree(data);
2364 return error;
2366 out_err_nosb:
2367 nfs_free_server(server);
2368 goto out;
2370 error_splat_root:
2371 dput(mntroot);
2372 error_splat_super:
2373 if (server && !s->s_root)
2374 bdi_unregister(&server->backing_dev_info);
2375 error_splat_bdi:
2376 deactivate_locked_super(s);
2377 goto out;
2381 * Ensure that we unregister the bdi before kill_anon_super
2382 * releases the device name
2384 static void nfs_put_super(struct super_block *s)
2386 struct nfs_server *server = NFS_SB(s);
2388 bdi_unregister(&server->backing_dev_info);
2392 * Destroy an NFS2/3 superblock
2394 static void nfs_kill_super(struct super_block *s)
2396 struct nfs_server *server = NFS_SB(s);
2398 kill_anon_super(s);
2399 nfs_fscache_release_super_cookie(s);
2400 nfs_free_server(server);
2404 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2406 static struct dentry *
2407 nfs_xdev_mount(struct file_system_type *fs_type, int flags,
2408 const char *dev_name, void *raw_data)
2410 struct nfs_clone_mount *data = raw_data;
2411 struct super_block *s;
2412 struct nfs_server *server;
2413 struct dentry *mntroot;
2414 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2415 struct nfs_sb_mountdata sb_mntdata = {
2416 .mntflags = flags,
2418 int error;
2420 dprintk("--> nfs_xdev_mount()\n");
2422 /* create a new volume representation */
2423 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2424 if (IS_ERR(server)) {
2425 error = PTR_ERR(server);
2426 goto out_err_noserver;
2428 sb_mntdata.server = server;
2430 if (server->flags & NFS_MOUNT_UNSHARED)
2431 compare_super = NULL;
2433 /* Get a superblock - note that we may end up sharing one that already exists */
2434 s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2435 if (IS_ERR(s)) {
2436 error = PTR_ERR(s);
2437 goto out_err_nosb;
2440 if (s->s_fs_info != server) {
2441 nfs_free_server(server);
2442 server = NULL;
2443 } else {
2444 error = nfs_bdi_register(server);
2445 if (error)
2446 goto error_splat_bdi;
2449 if (!s->s_root) {
2450 /* initial superblock/root creation */
2451 nfs_clone_super(s, data->sb);
2452 nfs_fscache_get_super_cookie(s, NULL, data);
2455 mntroot = nfs_get_root(s, data->fh);
2456 if (IS_ERR(mntroot)) {
2457 error = PTR_ERR(mntroot);
2458 goto error_splat_super;
2460 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2461 dput(mntroot);
2462 error = -ESTALE;
2463 goto error_splat_super;
2466 s->s_flags |= MS_ACTIVE;
2468 /* clone any lsm security options from the parent to the new sb */
2469 security_sb_clone_mnt_opts(data->sb, s);
2471 dprintk("<-- nfs_xdev_mount() = 0\n");
2472 return mntroot;
2474 out_err_nosb:
2475 nfs_free_server(server);
2476 out_err_noserver:
2477 dprintk("<-- nfs_xdev_mount() = %d [error]\n", error);
2478 return ERR_PTR(error);
2480 error_splat_super:
2481 if (server && !s->s_root)
2482 bdi_unregister(&server->backing_dev_info);
2483 error_splat_bdi:
2484 deactivate_locked_super(s);
2485 dprintk("<-- nfs_xdev_mount() = %d [splat]\n", error);
2486 return ERR_PTR(error);
2489 #ifdef CONFIG_NFS_V4
2492 * Finish setting up a cloned NFS4 superblock
2494 static void nfs4_clone_super(struct super_block *sb,
2495 const struct super_block *old_sb)
2497 sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2498 sb->s_blocksize = old_sb->s_blocksize;
2499 sb->s_maxbytes = old_sb->s_maxbytes;
2500 sb->s_time_gran = 1;
2501 sb->s_op = old_sb->s_op;
2502 nfs_initialise_sb(sb);
2506 * Set up an NFS4 superblock
2508 static void nfs4_fill_super(struct super_block *sb)
2510 sb->s_time_gran = 1;
2511 sb->s_op = &nfs4_sops;
2512 nfs_initialise_sb(sb);
2515 static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2517 args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
2518 NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
2521 static int nfs4_validate_text_mount_data(void *options,
2522 struct nfs_parsed_mount_data *args,
2523 const char *dev_name)
2525 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2527 nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
2529 nfs_validate_transport_protocol(args);
2531 nfs4_validate_mount_flags(args);
2533 if (args->version != 4) {
2534 dfprintk(MOUNT,
2535 "NFS4: Illegal mount version\n");
2536 return -EINVAL;
2539 if (args->auth_flavor_len > 1) {
2540 dfprintk(MOUNT,
2541 "NFS4: Too many RPC auth flavours specified\n");
2542 return -EINVAL;
2545 if (args->client_address == NULL) {
2546 dfprintk(MOUNT,
2547 "NFS4: mount program didn't pass callback address\n");
2548 return -EINVAL;
2551 return nfs_parse_devname(dev_name,
2552 &args->nfs_server.hostname,
2553 NFS4_MAXNAMLEN,
2554 &args->nfs_server.export_path,
2555 NFS4_MAXPATHLEN);
2559 * Validate NFSv4 mount options
2561 static int nfs4_validate_mount_data(void *options,
2562 struct nfs_parsed_mount_data *args,
2563 const char *dev_name)
2565 struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2566 struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2567 char *c;
2569 if (data == NULL)
2570 goto out_no_data;
2572 switch (data->version) {
2573 case 1:
2574 if (data->host_addrlen > sizeof(args->nfs_server.address))
2575 goto out_no_address;
2576 if (data->host_addrlen == 0)
2577 goto out_no_address;
2578 args->nfs_server.addrlen = data->host_addrlen;
2579 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
2580 return -EFAULT;
2581 if (!nfs_verify_server_address(sap))
2582 goto out_no_address;
2584 if (data->auth_flavourlen) {
2585 if (data->auth_flavourlen > 1)
2586 goto out_inval_auth;
2587 if (copy_from_user(&args->auth_flavors[0],
2588 data->auth_flavours,
2589 sizeof(args->auth_flavors[0])))
2590 return -EFAULT;
2593 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2594 if (IS_ERR(c))
2595 return PTR_ERR(c);
2596 args->nfs_server.hostname = c;
2598 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2599 if (IS_ERR(c))
2600 return PTR_ERR(c);
2601 args->nfs_server.export_path = c;
2602 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2604 c = strndup_user(data->client_addr.data, 16);
2605 if (IS_ERR(c))
2606 return PTR_ERR(c);
2607 args->client_address = c;
2610 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2611 * can deal with.
2614 args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
2615 args->rsize = data->rsize;
2616 args->wsize = data->wsize;
2617 args->timeo = data->timeo;
2618 args->retrans = data->retrans;
2619 args->acregmin = data->acregmin;
2620 args->acregmax = data->acregmax;
2621 args->acdirmin = data->acdirmin;
2622 args->acdirmax = data->acdirmax;
2623 args->nfs_server.protocol = data->proto;
2624 nfs_validate_transport_protocol(args);
2626 break;
2627 default:
2628 if (nfs_parse_mount_options((char *)options, args) == 0)
2629 return -EINVAL;
2631 if (!nfs_verify_server_address(sap))
2632 return -EINVAL;
2634 return nfs4_validate_text_mount_data(options, args, dev_name);
2637 return 0;
2639 out_no_data:
2640 dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2641 return -EINVAL;
2643 out_inval_auth:
2644 dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2645 data->auth_flavourlen);
2646 return -EINVAL;
2648 out_no_address:
2649 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2650 return -EINVAL;
2654 * Get the superblock for the NFS4 root partition
2656 static struct dentry *
2657 nfs4_remote_mount(struct file_system_type *fs_type, int flags,
2658 const char *dev_name, void *raw_data)
2660 struct nfs_parsed_mount_data *data = raw_data;
2661 struct super_block *s;
2662 struct nfs_server *server;
2663 struct nfs_fh *mntfh;
2664 struct dentry *mntroot;
2665 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2666 struct nfs_sb_mountdata sb_mntdata = {
2667 .mntflags = flags,
2669 int error = -ENOMEM;
2671 mntfh = nfs_alloc_fhandle();
2672 if (data == NULL || mntfh == NULL)
2673 goto out_free_fh;
2675 security_init_mnt_opts(&data->lsm_opts);
2677 /* Get a volume representation */
2678 server = nfs4_create_server(data, mntfh);
2679 if (IS_ERR(server)) {
2680 error = PTR_ERR(server);
2681 goto out;
2683 sb_mntdata.server = server;
2685 if (server->flags & NFS4_MOUNT_UNSHARED)
2686 compare_super = NULL;
2688 /* Get a superblock - note that we may end up sharing one that already exists */
2689 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2690 if (IS_ERR(s)) {
2691 error = PTR_ERR(s);
2692 goto out_free;
2695 if (s->s_fs_info != server) {
2696 nfs_free_server(server);
2697 server = NULL;
2698 } else {
2699 error = nfs_bdi_register(server);
2700 if (error)
2701 goto error_splat_bdi;
2704 if (!s->s_root) {
2705 /* initial superblock/root creation */
2706 nfs4_fill_super(s);
2707 nfs_fscache_get_super_cookie(
2708 s, data ? data->fscache_uniq : NULL, NULL);
2711 mntroot = nfs4_get_root(s, mntfh);
2712 if (IS_ERR(mntroot)) {
2713 error = PTR_ERR(mntroot);
2714 goto error_splat_super;
2717 error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2718 if (error)
2719 goto error_splat_root;
2721 s->s_flags |= MS_ACTIVE;
2723 security_free_mnt_opts(&data->lsm_opts);
2724 nfs_free_fhandle(mntfh);
2725 return mntroot;
2727 out:
2728 security_free_mnt_opts(&data->lsm_opts);
2729 out_free_fh:
2730 nfs_free_fhandle(mntfh);
2731 return ERR_PTR(error);
2733 out_free:
2734 nfs_free_server(server);
2735 goto out;
2737 error_splat_root:
2738 dput(mntroot);
2739 error_splat_super:
2740 if (server && !s->s_root)
2741 bdi_unregister(&server->backing_dev_info);
2742 error_splat_bdi:
2743 deactivate_locked_super(s);
2744 goto out;
2747 static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2748 int flags, void *data, const char *hostname)
2750 struct vfsmount *root_mnt;
2751 char *root_devname;
2752 size_t len;
2754 len = strlen(hostname) + 3;
2755 root_devname = kmalloc(len, GFP_KERNEL);
2756 if (root_devname == NULL)
2757 return ERR_PTR(-ENOMEM);
2758 snprintf(root_devname, len, "%s:/", hostname);
2759 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2760 kfree(root_devname);
2761 return root_mnt;
2764 static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
2766 char *page = (char *) __get_free_page(GFP_KERNEL);
2767 char *devname, *tmp;
2769 if (page == NULL)
2770 return;
2771 devname = nfs_path(path->mnt->mnt_devname,
2772 path->mnt->mnt_root, path->dentry,
2773 page, PAGE_SIZE);
2774 if (IS_ERR(devname))
2775 goto out_freepage;
2776 tmp = kstrdup(devname, GFP_KERNEL);
2777 if (tmp == NULL)
2778 goto out_freepage;
2779 kfree(mnt->mnt_devname);
2780 mnt->mnt_devname = tmp;
2781 out_freepage:
2782 free_page((unsigned long)page);
2785 struct nfs_referral_count {
2786 struct list_head list;
2787 const struct task_struct *task;
2788 unsigned int referral_count;
2791 static LIST_HEAD(nfs_referral_count_list);
2792 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
2794 static struct nfs_referral_count *nfs_find_referral_count(void)
2796 struct nfs_referral_count *p;
2798 list_for_each_entry(p, &nfs_referral_count_list, list) {
2799 if (p->task == current)
2800 return p;
2802 return NULL;
2805 #define NFS_MAX_NESTED_REFERRALS 2
2807 static int nfs_referral_loop_protect(void)
2809 struct nfs_referral_count *p, *new;
2810 int ret = -ENOMEM;
2812 new = kmalloc(sizeof(*new), GFP_KERNEL);
2813 if (!new)
2814 goto out;
2815 new->task = current;
2816 new->referral_count = 1;
2818 ret = 0;
2819 spin_lock(&nfs_referral_count_list_lock);
2820 p = nfs_find_referral_count();
2821 if (p != NULL) {
2822 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
2823 ret = -ELOOP;
2824 else
2825 p->referral_count++;
2826 } else {
2827 list_add(&new->list, &nfs_referral_count_list);
2828 new = NULL;
2830 spin_unlock(&nfs_referral_count_list_lock);
2831 kfree(new);
2832 out:
2833 return ret;
2836 static void nfs_referral_loop_unprotect(void)
2838 struct nfs_referral_count *p;
2840 spin_lock(&nfs_referral_count_list_lock);
2841 p = nfs_find_referral_count();
2842 p->referral_count--;
2843 if (p->referral_count == 0)
2844 list_del(&p->list);
2845 else
2846 p = NULL;
2847 spin_unlock(&nfs_referral_count_list_lock);
2848 kfree(p);
2851 static int nfs_follow_remote_path(struct vfsmount *root_mnt,
2852 const char *export_path, struct vfsmount *mnt_target)
2854 struct nameidata *nd = NULL;
2855 struct mnt_namespace *ns_private;
2856 struct super_block *s;
2857 int ret;
2859 nd = kmalloc(sizeof(*nd), GFP_KERNEL);
2860 if (nd == NULL)
2861 return -ENOMEM;
2863 ns_private = create_mnt_ns(root_mnt);
2864 ret = PTR_ERR(ns_private);
2865 if (IS_ERR(ns_private))
2866 goto out_mntput;
2868 ret = nfs_referral_loop_protect();
2869 if (ret != 0)
2870 goto out_put_mnt_ns;
2872 ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
2873 export_path, LOOKUP_FOLLOW, nd);
2875 nfs_referral_loop_unprotect();
2876 put_mnt_ns(ns_private);
2878 if (ret != 0)
2879 goto out_err;
2881 s = nd->path.mnt->mnt_sb;
2882 atomic_inc(&s->s_active);
2883 mnt_target->mnt_sb = s;
2884 mnt_target->mnt_root = dget(nd->path.dentry);
2886 /* Correct the device pathname */
2887 nfs_fix_devname(&nd->path, mnt_target);
2889 path_put(&nd->path);
2890 kfree(nd);
2891 down_write(&s->s_umount);
2892 return 0;
2893 out_put_mnt_ns:
2894 put_mnt_ns(ns_private);
2895 out_mntput:
2896 mntput(root_mnt);
2897 out_err:
2898 kfree(nd);
2899 return ret;
2902 static int nfs4_try_mount(int flags, const char *dev_name,
2903 struct nfs_parsed_mount_data *data,
2904 struct vfsmount *mnt)
2906 char *export_path;
2907 struct vfsmount *root_mnt;
2908 int error;
2910 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
2912 export_path = data->nfs_server.export_path;
2913 data->nfs_server.export_path = "/";
2914 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2915 data->nfs_server.hostname);
2916 data->nfs_server.export_path = export_path;
2918 error = PTR_ERR(root_mnt);
2919 if (IS_ERR(root_mnt))
2920 goto out;
2922 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2924 out:
2925 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
2926 error != 0 ? " [error]" : "");
2927 return error;
2931 * Get the superblock for an NFS4 mountpoint
2933 static int nfs4_get_sb(struct file_system_type *fs_type,
2934 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2936 struct nfs_parsed_mount_data *data;
2937 int error = -ENOMEM;
2939 data = nfs_alloc_parsed_mount_data(4);
2940 if (data == NULL)
2941 goto out_free_data;
2943 /* Validate the mount data */
2944 error = nfs4_validate_mount_data(raw_data, data, dev_name);
2945 if (error < 0)
2946 goto out;
2948 error = nfs4_try_mount(flags, dev_name, data, mnt);
2950 out:
2951 kfree(data->client_address);
2952 kfree(data->nfs_server.export_path);
2953 kfree(data->nfs_server.hostname);
2954 kfree(data->fscache_uniq);
2955 out_free_data:
2956 kfree(data);
2957 dprintk("<-- nfs4_get_sb() = %d%s\n", error,
2958 error != 0 ? " [error]" : "");
2959 return error;
2962 static void nfs4_kill_super(struct super_block *sb)
2964 struct nfs_server *server = NFS_SB(sb);
2966 dprintk("--> %s\n", __func__);
2967 nfs_super_return_all_delegations(sb);
2968 kill_anon_super(sb);
2969 nfs_fscache_release_super_cookie(sb);
2970 nfs_free_server(server);
2971 dprintk("<-- %s\n", __func__);
2975 * Clone an NFS4 server record on xdev traversal (FSID-change)
2977 static struct dentry *
2978 nfs4_xdev_mount(struct file_system_type *fs_type, int flags,
2979 const char *dev_name, void *raw_data)
2981 struct nfs_clone_mount *data = raw_data;
2982 struct super_block *s;
2983 struct nfs_server *server;
2984 struct dentry *mntroot;
2985 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2986 struct nfs_sb_mountdata sb_mntdata = {
2987 .mntflags = flags,
2989 int error;
2991 dprintk("--> nfs4_xdev_mount()\n");
2993 /* create a new volume representation */
2994 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2995 if (IS_ERR(server)) {
2996 error = PTR_ERR(server);
2997 goto out_err_noserver;
2999 sb_mntdata.server = server;
3001 if (server->flags & NFS4_MOUNT_UNSHARED)
3002 compare_super = NULL;
3004 /* Get a superblock - note that we may end up sharing one that already exists */
3005 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
3006 if (IS_ERR(s)) {
3007 error = PTR_ERR(s);
3008 goto out_err_nosb;
3011 if (s->s_fs_info != server) {
3012 nfs_free_server(server);
3013 server = NULL;
3014 } else {
3015 error = nfs_bdi_register(server);
3016 if (error)
3017 goto error_splat_bdi;
3020 if (!s->s_root) {
3021 /* initial superblock/root creation */
3022 nfs4_clone_super(s, data->sb);
3023 nfs_fscache_get_super_cookie(s, NULL, data);
3026 mntroot = nfs4_get_root(s, data->fh);
3027 if (IS_ERR(mntroot)) {
3028 error = PTR_ERR(mntroot);
3029 goto error_splat_super;
3031 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3032 dput(mntroot);
3033 error = -ESTALE;
3034 goto error_splat_super;
3037 s->s_flags |= MS_ACTIVE;
3039 security_sb_clone_mnt_opts(data->sb, s);
3041 dprintk("<-- nfs4_xdev_mount() = 0\n");
3042 return mntroot;
3044 out_err_nosb:
3045 nfs_free_server(server);
3046 out_err_noserver:
3047 dprintk("<-- nfs4_xdev_mount() = %d [error]\n", error);
3048 return ERR_PTR(error);
3050 error_splat_super:
3051 if (server && !s->s_root)
3052 bdi_unregister(&server->backing_dev_info);
3053 error_splat_bdi:
3054 deactivate_locked_super(s);
3055 dprintk("<-- nfs4_xdev_mount() = %d [splat]\n", error);
3056 return ERR_PTR(error);
3059 static struct dentry *
3060 nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
3061 const char *dev_name, void *raw_data)
3063 struct nfs_clone_mount *data = raw_data;
3064 struct super_block *s;
3065 struct nfs_server *server;
3066 struct dentry *mntroot;
3067 struct nfs_fh *mntfh;
3068 int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
3069 struct nfs_sb_mountdata sb_mntdata = {
3070 .mntflags = flags,
3072 int error = -ENOMEM;
3074 dprintk("--> nfs4_referral_get_sb()\n");
3076 mntfh = nfs_alloc_fhandle();
3077 if (mntfh == NULL)
3078 goto out_err_nofh;
3080 /* create a new volume representation */
3081 server = nfs4_create_referral_server(data, mntfh);
3082 if (IS_ERR(server)) {
3083 error = PTR_ERR(server);
3084 goto out_err_noserver;
3086 sb_mntdata.server = server;
3088 if (server->flags & NFS4_MOUNT_UNSHARED)
3089 compare_super = NULL;
3091 /* Get a superblock - note that we may end up sharing one that already exists */
3092 s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
3093 if (IS_ERR(s)) {
3094 error = PTR_ERR(s);
3095 goto out_err_nosb;
3098 if (s->s_fs_info != server) {
3099 nfs_free_server(server);
3100 server = NULL;
3101 } else {
3102 error = nfs_bdi_register(server);
3103 if (error)
3104 goto error_splat_bdi;
3107 if (!s->s_root) {
3108 /* initial superblock/root creation */
3109 nfs4_fill_super(s);
3110 nfs_fscache_get_super_cookie(s, NULL, data);
3113 mntroot = nfs4_get_root(s, mntfh);
3114 if (IS_ERR(mntroot)) {
3115 error = PTR_ERR(mntroot);
3116 goto error_splat_super;
3118 if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3119 dput(mntroot);
3120 error = -ESTALE;
3121 goto error_splat_super;
3124 s->s_flags |= MS_ACTIVE;
3126 security_sb_clone_mnt_opts(data->sb, s);
3128 nfs_free_fhandle(mntfh);
3129 dprintk("<-- nfs4_referral_get_sb() = 0\n");
3130 return mntroot;
3132 out_err_nosb:
3133 nfs_free_server(server);
3134 out_err_noserver:
3135 nfs_free_fhandle(mntfh);
3136 out_err_nofh:
3137 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
3138 return ERR_PTR(error);
3140 error_splat_super:
3141 if (server && !s->s_root)
3142 bdi_unregister(&server->backing_dev_info);
3143 error_splat_bdi:
3144 deactivate_locked_super(s);
3145 nfs_free_fhandle(mntfh);
3146 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
3147 return ERR_PTR(error);
3151 * Create an NFS4 server record on referral traversal
3153 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
3154 int flags, const char *dev_name, void *raw_data,
3155 struct vfsmount *mnt)
3157 struct nfs_clone_mount *data = raw_data;
3158 char *export_path;
3159 struct vfsmount *root_mnt;
3160 int error;
3162 dprintk("--> nfs4_referral_get_sb()\n");
3164 export_path = data->mnt_path;
3165 data->mnt_path = "/";
3167 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
3168 flags, data, data->hostname);
3169 data->mnt_path = export_path;
3171 error = PTR_ERR(root_mnt);
3172 if (IS_ERR(root_mnt))
3173 goto out;
3175 error = nfs_follow_remote_path(root_mnt, export_path, mnt);
3176 out:
3177 dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
3178 error != 0 ? " [error]" : "");
3179 return error;
3182 #endif /* CONFIG_NFS_V4 */