Merge branch 'core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[linux-2.6] / fs / nfs / super.c
1 /*
2  *  linux/fs/nfs/super.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs superblock handling functions
7  *
8  *  Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
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  *
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())
21  */
22
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>
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/nfs_idmap.h>
46 #include <linux/vfs.h>
47 #include <linux/inet.h>
48 #include <linux/in6.h>
49 #include <net/ipv6.h>
50 #include <linux/netdevice.h>
51 #include <linux/nfs_xdr.h>
52 #include <linux/magic.h>
53 #include <linux/parser.h>
54
55 #include <asm/system.h>
56 #include <asm/uaccess.h>
57
58 #include "nfs4_fs.h"
59 #include "callback.h"
60 #include "delegation.h"
61 #include "iostat.h"
62 #include "internal.h"
63
64 #define NFSDBG_FACILITY         NFSDBG_VFS
65
66 enum {
67         /* Mount options that take no arguments */
68         Opt_soft, Opt_hard,
69         Opt_posix, Opt_noposix,
70         Opt_cto, Opt_nocto,
71         Opt_ac, Opt_noac,
72         Opt_lock, Opt_nolock,
73         Opt_v2, Opt_v3,
74         Opt_udp, Opt_tcp, Opt_rdma,
75         Opt_acl, Opt_noacl,
76         Opt_rdirplus, Opt_nordirplus,
77         Opt_sharecache, Opt_nosharecache,
78
79         /* Mount options that take integer arguments */
80         Opt_port,
81         Opt_rsize, Opt_wsize, Opt_bsize,
82         Opt_timeo, Opt_retrans,
83         Opt_acregmin, Opt_acregmax,
84         Opt_acdirmin, Opt_acdirmax,
85         Opt_actimeo,
86         Opt_namelen,
87         Opt_mountport,
88         Opt_mountvers,
89         Opt_nfsvers,
90
91         /* Mount options that take string arguments */
92         Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
93         Opt_addr, Opt_mountaddr, Opt_clientaddr,
94         Opt_lookupcache,
95
96         /* Special mount options */
97         Opt_userspace, Opt_deprecated, Opt_sloppy,
98
99         Opt_err
100 };
101
102 static const match_table_t nfs_mount_option_tokens = {
103         { Opt_userspace, "bg" },
104         { Opt_userspace, "fg" },
105         { Opt_userspace, "retry=%s" },
106
107         { Opt_sloppy, "sloppy" },
108
109         { Opt_soft, "soft" },
110         { Opt_hard, "hard" },
111         { Opt_deprecated, "intr" },
112         { Opt_deprecated, "nointr" },
113         { Opt_posix, "posix" },
114         { Opt_noposix, "noposix" },
115         { Opt_cto, "cto" },
116         { Opt_nocto, "nocto" },
117         { Opt_ac, "ac" },
118         { Opt_noac, "noac" },
119         { Opt_lock, "lock" },
120         { Opt_nolock, "nolock" },
121         { Opt_v2, "v2" },
122         { Opt_v3, "v3" },
123         { Opt_udp, "udp" },
124         { Opt_tcp, "tcp" },
125         { Opt_rdma, "rdma" },
126         { Opt_acl, "acl" },
127         { Opt_noacl, "noacl" },
128         { Opt_rdirplus, "rdirplus" },
129         { Opt_nordirplus, "nordirplus" },
130         { Opt_sharecache, "sharecache" },
131         { Opt_nosharecache, "nosharecache" },
132
133         { Opt_port, "port=%u" },
134         { Opt_rsize, "rsize=%u" },
135         { Opt_wsize, "wsize=%u" },
136         { Opt_bsize, "bsize=%u" },
137         { Opt_timeo, "timeo=%u" },
138         { Opt_retrans, "retrans=%u" },
139         { Opt_acregmin, "acregmin=%u" },
140         { Opt_acregmax, "acregmax=%u" },
141         { Opt_acdirmin, "acdirmin=%u" },
142         { Opt_acdirmax, "acdirmax=%u" },
143         { Opt_actimeo, "actimeo=%u" },
144         { Opt_namelen, "namlen=%u" },
145         { Opt_mountport, "mountport=%u" },
146         { Opt_mountvers, "mountvers=%u" },
147         { Opt_nfsvers, "nfsvers=%u" },
148         { Opt_nfsvers, "vers=%u" },
149
150         { Opt_sec, "sec=%s" },
151         { Opt_proto, "proto=%s" },
152         { Opt_mountproto, "mountproto=%s" },
153         { Opt_addr, "addr=%s" },
154         { Opt_clientaddr, "clientaddr=%s" },
155         { Opt_mounthost, "mounthost=%s" },
156         { Opt_mountaddr, "mountaddr=%s" },
157
158         { Opt_lookupcache, "lookupcache=%s" },
159
160         { Opt_err, NULL }
161 };
162
163 enum {
164         Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
165
166         Opt_xprt_err
167 };
168
169 static const match_table_t nfs_xprt_protocol_tokens = {
170         { Opt_xprt_udp, "udp" },
171         { Opt_xprt_tcp, "tcp" },
172         { Opt_xprt_rdma, "rdma" },
173
174         { Opt_xprt_err, NULL }
175 };
176
177 enum {
178         Opt_sec_none, Opt_sec_sys,
179         Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
180         Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
181         Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
182
183         Opt_sec_err
184 };
185
186 static const match_table_t nfs_secflavor_tokens = {
187         { Opt_sec_none, "none" },
188         { Opt_sec_none, "null" },
189         { Opt_sec_sys, "sys" },
190
191         { Opt_sec_krb5, "krb5" },
192         { Opt_sec_krb5i, "krb5i" },
193         { Opt_sec_krb5p, "krb5p" },
194
195         { Opt_sec_lkey, "lkey" },
196         { Opt_sec_lkeyi, "lkeyi" },
197         { Opt_sec_lkeyp, "lkeyp" },
198
199         { Opt_sec_spkm, "spkm3" },
200         { Opt_sec_spkmi, "spkm3i" },
201         { Opt_sec_spkmp, "spkm3p" },
202
203         { Opt_sec_err, NULL }
204 };
205
206 enum {
207         Opt_lookupcache_all, Opt_lookupcache_positive,
208         Opt_lookupcache_none,
209
210         Opt_lookupcache_err
211 };
212
213 static match_table_t nfs_lookupcache_tokens = {
214         { Opt_lookupcache_all, "all" },
215         { Opt_lookupcache_positive, "pos" },
216         { Opt_lookupcache_positive, "positive" },
217         { Opt_lookupcache_none, "none" },
218
219         { Opt_lookupcache_err, NULL }
220 };
221
222
223 static void nfs_umount_begin(struct super_block *);
224 static int  nfs_statfs(struct dentry *, struct kstatfs *);
225 static int  nfs_show_options(struct seq_file *, struct vfsmount *);
226 static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
227 static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
228 static int nfs_xdev_get_sb(struct file_system_type *fs_type,
229                 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
230 static void nfs_kill_super(struct super_block *);
231 static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
232
233 static struct file_system_type nfs_fs_type = {
234         .owner          = THIS_MODULE,
235         .name           = "nfs",
236         .get_sb         = nfs_get_sb,
237         .kill_sb        = nfs_kill_super,
238         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
239 };
240
241 struct file_system_type nfs_xdev_fs_type = {
242         .owner          = THIS_MODULE,
243         .name           = "nfs",
244         .get_sb         = nfs_xdev_get_sb,
245         .kill_sb        = nfs_kill_super,
246         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
247 };
248
249 static const struct super_operations nfs_sops = {
250         .alloc_inode    = nfs_alloc_inode,
251         .destroy_inode  = nfs_destroy_inode,
252         .write_inode    = nfs_write_inode,
253         .statfs         = nfs_statfs,
254         .clear_inode    = nfs_clear_inode,
255         .umount_begin   = nfs_umount_begin,
256         .show_options   = nfs_show_options,
257         .show_stats     = nfs_show_stats,
258         .remount_fs     = nfs_remount,
259 };
260
261 #ifdef CONFIG_NFS_V4
262 static int nfs4_get_sb(struct file_system_type *fs_type,
263         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
264 static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
265         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
266 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
267         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
268 static void nfs4_kill_super(struct super_block *sb);
269
270 static struct file_system_type nfs4_fs_type = {
271         .owner          = THIS_MODULE,
272         .name           = "nfs4",
273         .get_sb         = nfs4_get_sb,
274         .kill_sb        = nfs4_kill_super,
275         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
276 };
277
278 struct file_system_type nfs4_xdev_fs_type = {
279         .owner          = THIS_MODULE,
280         .name           = "nfs4",
281         .get_sb         = nfs4_xdev_get_sb,
282         .kill_sb        = nfs4_kill_super,
283         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
284 };
285
286 struct file_system_type nfs4_referral_fs_type = {
287         .owner          = THIS_MODULE,
288         .name           = "nfs4",
289         .get_sb         = nfs4_referral_get_sb,
290         .kill_sb        = nfs4_kill_super,
291         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
292 };
293
294 static const struct super_operations nfs4_sops = {
295         .alloc_inode    = nfs_alloc_inode,
296         .destroy_inode  = nfs_destroy_inode,
297         .write_inode    = nfs_write_inode,
298         .statfs         = nfs_statfs,
299         .clear_inode    = nfs4_clear_inode,
300         .umount_begin   = nfs_umount_begin,
301         .show_options   = nfs_show_options,
302         .show_stats     = nfs_show_stats,
303         .remount_fs     = nfs_remount,
304 };
305 #endif
306
307 static struct shrinker acl_shrinker = {
308         .shrink         = nfs_access_cache_shrinker,
309         .seeks          = DEFAULT_SEEKS,
310 };
311
312 /*
313  * Register the NFS filesystems
314  */
315 int __init register_nfs_fs(void)
316 {
317         int ret;
318
319         ret = register_filesystem(&nfs_fs_type);
320         if (ret < 0)
321                 goto error_0;
322
323         ret = nfs_register_sysctl();
324         if (ret < 0)
325                 goto error_1;
326 #ifdef CONFIG_NFS_V4
327         ret = register_filesystem(&nfs4_fs_type);
328         if (ret < 0)
329                 goto error_2;
330 #endif
331         register_shrinker(&acl_shrinker);
332         return 0;
333
334 #ifdef CONFIG_NFS_V4
335 error_2:
336         nfs_unregister_sysctl();
337 #endif
338 error_1:
339         unregister_filesystem(&nfs_fs_type);
340 error_0:
341         return ret;
342 }
343
344 /*
345  * Unregister the NFS filesystems
346  */
347 void __exit unregister_nfs_fs(void)
348 {
349         unregister_shrinker(&acl_shrinker);
350 #ifdef CONFIG_NFS_V4
351         unregister_filesystem(&nfs4_fs_type);
352 #endif
353         nfs_unregister_sysctl();
354         unregister_filesystem(&nfs_fs_type);
355 }
356
357 void nfs_sb_active(struct super_block *sb)
358 {
359         struct nfs_server *server = NFS_SB(sb);
360
361         if (atomic_inc_return(&server->active) == 1)
362                 atomic_inc(&sb->s_active);
363 }
364
365 void nfs_sb_deactive(struct super_block *sb)
366 {
367         struct nfs_server *server = NFS_SB(sb);
368
369         if (atomic_dec_and_test(&server->active))
370                 deactivate_super(sb);
371 }
372
373 /*
374  * Deliver file system statistics to userspace
375  */
376 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
377 {
378         struct nfs_server *server = NFS_SB(dentry->d_sb);
379         unsigned char blockbits;
380         unsigned long blockres;
381         struct nfs_fh *fh = NFS_FH(dentry->d_inode);
382         struct nfs_fattr fattr;
383         struct nfs_fsstat res = {
384                         .fattr = &fattr,
385         };
386         int error;
387
388         error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
389         if (error < 0)
390                 goto out_err;
391         buf->f_type = NFS_SUPER_MAGIC;
392
393         /*
394          * Current versions of glibc do not correctly handle the
395          * case where f_frsize != f_bsize.  Eventually we want to
396          * report the value of wtmult in this field.
397          */
398         buf->f_frsize = dentry->d_sb->s_blocksize;
399
400         /*
401          * On most *nix systems, f_blocks, f_bfree, and f_bavail
402          * are reported in units of f_frsize.  Linux hasn't had
403          * an f_frsize field in its statfs struct until recently,
404          * thus historically Linux's sys_statfs reports these
405          * fields in units of f_bsize.
406          */
407         buf->f_bsize = dentry->d_sb->s_blocksize;
408         blockbits = dentry->d_sb->s_blocksize_bits;
409         blockres = (1 << blockbits) - 1;
410         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
411         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
412         buf->f_bavail = (res.abytes + blockres) >> blockbits;
413
414         buf->f_files = res.tfiles;
415         buf->f_ffree = res.afiles;
416
417         buf->f_namelen = server->namelen;
418
419         return 0;
420
421  out_err:
422         dprintk("%s: statfs error = %d\n", __func__, -error);
423         return error;
424 }
425
426 /*
427  * Map the security flavour number to a name
428  */
429 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
430 {
431         static const struct {
432                 rpc_authflavor_t flavour;
433                 const char *str;
434         } sec_flavours[] = {
435                 { RPC_AUTH_NULL, "null" },
436                 { RPC_AUTH_UNIX, "sys" },
437                 { RPC_AUTH_GSS_KRB5, "krb5" },
438                 { RPC_AUTH_GSS_KRB5I, "krb5i" },
439                 { RPC_AUTH_GSS_KRB5P, "krb5p" },
440                 { RPC_AUTH_GSS_LKEY, "lkey" },
441                 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
442                 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
443                 { RPC_AUTH_GSS_SPKM, "spkm" },
444                 { RPC_AUTH_GSS_SPKMI, "spkmi" },
445                 { RPC_AUTH_GSS_SPKMP, "spkmp" },
446                 { UINT_MAX, "unknown" }
447         };
448         int i;
449
450         for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
451                 if (sec_flavours[i].flavour == flavour)
452                         break;
453         }
454         return sec_flavours[i].str;
455 }
456
457 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
458                                     int showdefaults)
459 {
460         struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
461
462         switch (sap->sa_family) {
463         case AF_INET: {
464                 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
465                 seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
466                 break;
467         }
468         case AF_INET6: {
469                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
470                 seq_printf(m, ",mountaddr=%pI6", &sin6->sin6_addr);
471                 break;
472         }
473         default:
474                 if (showdefaults)
475                         seq_printf(m, ",mountaddr=unspecified");
476         }
477
478         if (nfss->mountd_version || showdefaults)
479                 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
480         if (nfss->mountd_port || showdefaults)
481                 seq_printf(m, ",mountport=%u", nfss->mountd_port);
482
483         switch (nfss->mountd_protocol) {
484         case IPPROTO_UDP:
485                 seq_printf(m, ",mountproto=udp");
486                 break;
487         case IPPROTO_TCP:
488                 seq_printf(m, ",mountproto=tcp");
489                 break;
490         default:
491                 if (showdefaults)
492                         seq_printf(m, ",mountproto=auto");
493         }
494 }
495
496 /*
497  * Describe the mount options in force on this server representation
498  */
499 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
500                                    int showdefaults)
501 {
502         static const struct proc_nfs_info {
503                 int flag;
504                 const char *str;
505                 const char *nostr;
506         } nfs_info[] = {
507                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
508                 { NFS_MOUNT_INTR, ",intr", ",nointr" },
509                 { NFS_MOUNT_POSIX, ",posix", "" },
510                 { NFS_MOUNT_NOCTO, ",nocto", "" },
511                 { NFS_MOUNT_NOAC, ",noac", "" },
512                 { NFS_MOUNT_NONLM, ",nolock", "" },
513                 { NFS_MOUNT_NOACL, ",noacl", "" },
514                 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
515                 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
516                 { 0, NULL, NULL }
517         };
518         const struct proc_nfs_info *nfs_infop;
519         struct nfs_client *clp = nfss->nfs_client;
520         u32 version = clp->rpc_ops->version;
521
522         seq_printf(m, ",vers=%u", version);
523         seq_printf(m, ",rsize=%u", nfss->rsize);
524         seq_printf(m, ",wsize=%u", nfss->wsize);
525         if (nfss->bsize != 0)
526                 seq_printf(m, ",bsize=%u", nfss->bsize);
527         seq_printf(m, ",namlen=%u", nfss->namelen);
528         if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
529                 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
530         if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
531                 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
532         if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
533                 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
534         if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
535                 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
536         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
537                 if (nfss->flags & nfs_infop->flag)
538                         seq_puts(m, nfs_infop->str);
539                 else
540                         seq_puts(m, nfs_infop->nostr);
541         }
542         seq_printf(m, ",proto=%s",
543                    rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
544         if (version == 4) {
545                 if (nfss->port != NFS_PORT)
546                         seq_printf(m, ",port=%u", nfss->port);
547         } else
548                 if (nfss->port)
549                         seq_printf(m, ",port=%u", nfss->port);
550
551         seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
552         seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
553         seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
554
555         if (version != 4)
556                 nfs_show_mountd_options(m, nfss, showdefaults);
557
558 #ifdef CONFIG_NFS_V4
559         if (clp->rpc_ops->version == 4)
560                 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
561 #endif
562 }
563
564 /*
565  * Describe the mount options on this VFS mountpoint
566  */
567 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
568 {
569         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
570
571         nfs_show_mount_options(m, nfss, 0);
572
573         seq_printf(m, ",addr=%s",
574                         rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
575                                                         RPC_DISPLAY_ADDR));
576
577         return 0;
578 }
579
580 /*
581  * Present statistical information for this VFS mountpoint
582  */
583 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
584 {
585         int i, cpu;
586         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
587         struct rpc_auth *auth = nfss->client->cl_auth;
588         struct nfs_iostats totals = { };
589
590         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
591
592         /*
593          * Display all mount option settings
594          */
595         seq_printf(m, "\n\topts:\t");
596         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
597         seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
598         seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
599         seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
600         nfs_show_mount_options(m, nfss, 1);
601
602         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
603
604         seq_printf(m, "\n\tcaps:\t");
605         seq_printf(m, "caps=0x%x", nfss->caps);
606         seq_printf(m, ",wtmult=%u", nfss->wtmult);
607         seq_printf(m, ",dtsize=%u", nfss->dtsize);
608         seq_printf(m, ",bsize=%u", nfss->bsize);
609         seq_printf(m, ",namlen=%u", nfss->namelen);
610
611 #ifdef CONFIG_NFS_V4
612         if (nfss->nfs_client->rpc_ops->version == 4) {
613                 seq_printf(m, "\n\tnfsv4:\t");
614                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
615                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
616                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
617         }
618 #endif
619
620         /*
621          * Display security flavor in effect for this mount
622          */
623         seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
624         if (auth->au_flavor)
625                 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
626
627         /*
628          * Display superblock I/O counters
629          */
630         for_each_possible_cpu(cpu) {
631                 struct nfs_iostats *stats;
632
633                 preempt_disable();
634                 stats = per_cpu_ptr(nfss->io_stats, cpu);
635
636                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
637                         totals.events[i] += stats->events[i];
638                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
639                         totals.bytes[i] += stats->bytes[i];
640
641                 preempt_enable();
642         }
643
644         seq_printf(m, "\n\tevents:\t");
645         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
646                 seq_printf(m, "%lu ", totals.events[i]);
647         seq_printf(m, "\n\tbytes:\t");
648         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
649                 seq_printf(m, "%Lu ", totals.bytes[i]);
650         seq_printf(m, "\n");
651
652         rpc_print_iostats(m, nfss->client);
653
654         return 0;
655 }
656
657 /*
658  * Begin unmount by attempting to remove all automounted mountpoints we added
659  * in response to xdev traversals and referrals
660  */
661 static void nfs_umount_begin(struct super_block *sb)
662 {
663         struct nfs_server *server = NFS_SB(sb);
664         struct rpc_clnt *rpc;
665
666         /* -EIO all pending I/O */
667         rpc = server->client_acl;
668         if (!IS_ERR(rpc))
669                 rpc_killall_tasks(rpc);
670         rpc = server->client;
671         if (!IS_ERR(rpc))
672                 rpc_killall_tasks(rpc);
673 }
674
675 /*
676  * Sanity-check a server address provided by the mount command.
677  *
678  * Address family must be initialized, and address must not be
679  * the ANY address for that family.
680  */
681 static int nfs_verify_server_address(struct sockaddr *addr)
682 {
683         switch (addr->sa_family) {
684         case AF_INET: {
685                 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
686                 return sa->sin_addr.s_addr != htonl(INADDR_ANY);
687         }
688         case AF_INET6: {
689                 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
690                 return !ipv6_addr_any(sa);
691         }
692         }
693
694         return 0;
695 }
696
697 static void nfs_parse_ipv4_address(char *string, size_t str_len,
698                                    struct sockaddr *sap, size_t *addr_len)
699 {
700         struct sockaddr_in *sin = (struct sockaddr_in *)sap;
701         u8 *addr = (u8 *)&sin->sin_addr.s_addr;
702
703         if (str_len <= INET_ADDRSTRLEN) {
704                 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
705                                 (int)str_len, string);
706
707                 sin->sin_family = AF_INET;
708                 *addr_len = sizeof(*sin);
709                 if (in4_pton(string, str_len, addr, '\0', NULL))
710                         return;
711         }
712
713         sap->sa_family = AF_UNSPEC;
714         *addr_len = 0;
715 }
716
717 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
718 static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
719                                    const char *delim,
720                                    struct sockaddr_in6 *sin6)
721 {
722         char *p;
723         size_t len;
724
725         if ((string + str_len) == delim)
726                 return 1;
727
728         if (*delim != IPV6_SCOPE_DELIMITER)
729                 return 0;
730
731         if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
732                 return 0;
733
734         len = (string + str_len) - delim - 1;
735         p = kstrndup(delim + 1, len, GFP_KERNEL);
736         if (p) {
737                 unsigned long scope_id = 0;
738                 struct net_device *dev;
739
740                 dev = dev_get_by_name(&init_net, p);
741                 if (dev != NULL) {
742                         scope_id = dev->ifindex;
743                         dev_put(dev);
744                 } else {
745                         if (strict_strtoul(p, 10, &scope_id) == 0) {
746                                 kfree(p);
747                                 return 0;
748                         }
749                 }
750
751                 kfree(p);
752
753                 sin6->sin6_scope_id = scope_id;
754                 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
755                 return 1;
756         }
757
758         return 0;
759 }
760
761 static void nfs_parse_ipv6_address(char *string, size_t str_len,
762                                    struct sockaddr *sap, size_t *addr_len)
763 {
764         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
765         u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
766         const char *delim;
767
768         if (str_len <= INET6_ADDRSTRLEN) {
769                 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
770                                 (int)str_len, string);
771
772                 sin6->sin6_family = AF_INET6;
773                 *addr_len = sizeof(*sin6);
774                 if (in6_pton(string, str_len, addr,
775                                         IPV6_SCOPE_DELIMITER, &delim) != 0) {
776                         if (nfs_parse_ipv6_scope_id(string, str_len,
777                                                         delim, sin6) != 0)
778                                 return;
779                 }
780         }
781
782         sap->sa_family = AF_UNSPEC;
783         *addr_len = 0;
784 }
785 #else
786 static void nfs_parse_ipv6_address(char *string, size_t str_len,
787                                    struct sockaddr *sap, size_t *addr_len)
788 {
789         sap->sa_family = AF_UNSPEC;
790         *addr_len = 0;
791 }
792 #endif
793
794 /*
795  * Construct a sockaddr based on the contents of a string that contains
796  * an IP address in presentation format.
797  *
798  * If there is a problem constructing the new sockaddr, set the address
799  * family to AF_UNSPEC.
800  */
801 void nfs_parse_ip_address(char *string, size_t str_len,
802                                  struct sockaddr *sap, size_t *addr_len)
803 {
804         unsigned int i, colons;
805
806         colons = 0;
807         for (i = 0; i < str_len; i++)
808                 if (string[i] == ':')
809                         colons++;
810
811         if (colons >= 2)
812                 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
813         else
814                 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
815 }
816
817 /*
818  * Sanity check the NFS transport protocol.
819  *
820  */
821 static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
822 {
823         switch (mnt->nfs_server.protocol) {
824         case XPRT_TRANSPORT_UDP:
825         case XPRT_TRANSPORT_TCP:
826         case XPRT_TRANSPORT_RDMA:
827                 break;
828         default:
829                 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
830         }
831 }
832
833 /*
834  * For text based NFSv2/v3 mounts, the mount protocol transport default
835  * settings should depend upon the specified NFS transport.
836  */
837 static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
838 {
839         nfs_validate_transport_protocol(mnt);
840
841         if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
842             mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
843                         return;
844         switch (mnt->nfs_server.protocol) {
845         case XPRT_TRANSPORT_UDP:
846                 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
847                 break;
848         case XPRT_TRANSPORT_TCP:
849         case XPRT_TRANSPORT_RDMA:
850                 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
851         }
852 }
853
854 /*
855  * Parse the value of the 'sec=' option.
856  *
857  * The flavor_len setting is for v4 mounts.
858  */
859 static int nfs_parse_security_flavors(char *value,
860                                       struct nfs_parsed_mount_data *mnt)
861 {
862         substring_t args[MAX_OPT_ARGS];
863
864         dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
865
866         switch (match_token(value, nfs_secflavor_tokens, args)) {
867         case Opt_sec_none:
868                 mnt->auth_flavor_len = 0;
869                 mnt->auth_flavors[0] = RPC_AUTH_NULL;
870                 break;
871         case Opt_sec_sys:
872                 mnt->auth_flavor_len = 0;
873                 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
874                 break;
875         case Opt_sec_krb5:
876                 mnt->auth_flavor_len = 1;
877                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
878                 break;
879         case Opt_sec_krb5i:
880                 mnt->auth_flavor_len = 1;
881                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
882                 break;
883         case Opt_sec_krb5p:
884                 mnt->auth_flavor_len = 1;
885                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
886                 break;
887         case Opt_sec_lkey:
888                 mnt->auth_flavor_len = 1;
889                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
890                 break;
891         case Opt_sec_lkeyi:
892                 mnt->auth_flavor_len = 1;
893                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
894                 break;
895         case Opt_sec_lkeyp:
896                 mnt->auth_flavor_len = 1;
897                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
898                 break;
899         case Opt_sec_spkm:
900                 mnt->auth_flavor_len = 1;
901                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
902                 break;
903         case Opt_sec_spkmi:
904                 mnt->auth_flavor_len = 1;
905                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
906                 break;
907         case Opt_sec_spkmp:
908                 mnt->auth_flavor_len = 1;
909                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
910                 break;
911         default:
912                 return 0;
913         }
914
915         return 1;
916 }
917
918 static void nfs_parse_invalid_value(const char *option)
919 {
920         dfprintk(MOUNT, "NFS:   bad value specified for %s option\n", option);
921 }
922
923 /*
924  * Error-check and convert a string of mount options from user space into
925  * a data structure.  The whole mount string is processed; bad options are
926  * skipped as they are encountered.  If there were no errors, return 1;
927  * otherwise return 0 (zero).
928  */
929 static int nfs_parse_mount_options(char *raw,
930                                    struct nfs_parsed_mount_data *mnt)
931 {
932         char *p, *string, *secdata;
933         int rc, sloppy = 0, errors = 0;
934
935         if (!raw) {
936                 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
937                 return 1;
938         }
939         dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
940
941         secdata = alloc_secdata();
942         if (!secdata)
943                 goto out_nomem;
944
945         rc = security_sb_copy_data(raw, secdata);
946         if (rc)
947                 goto out_security_failure;
948
949         rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
950         if (rc)
951                 goto out_security_failure;
952
953         free_secdata(secdata);
954
955         while ((p = strsep(&raw, ",")) != NULL) {
956                 substring_t args[MAX_OPT_ARGS];
957                 int option, token;
958
959                 if (!*p)
960                         continue;
961
962                 dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", p);
963
964                 token = match_token(p, nfs_mount_option_tokens, args);
965                 switch (token) {
966
967                 /*
968                  * boolean options:  foo/nofoo
969                  */
970                 case Opt_soft:
971                         mnt->flags |= NFS_MOUNT_SOFT;
972                         break;
973                 case Opt_hard:
974                         mnt->flags &= ~NFS_MOUNT_SOFT;
975                         break;
976                 case Opt_posix:
977                         mnt->flags |= NFS_MOUNT_POSIX;
978                         break;
979                 case Opt_noposix:
980                         mnt->flags &= ~NFS_MOUNT_POSIX;
981                         break;
982                 case Opt_cto:
983                         mnt->flags &= ~NFS_MOUNT_NOCTO;
984                         break;
985                 case Opt_nocto:
986                         mnt->flags |= NFS_MOUNT_NOCTO;
987                         break;
988                 case Opt_ac:
989                         mnt->flags &= ~NFS_MOUNT_NOAC;
990                         break;
991                 case Opt_noac:
992                         mnt->flags |= NFS_MOUNT_NOAC;
993                         break;
994                 case Opt_lock:
995                         mnt->flags &= ~NFS_MOUNT_NONLM;
996                         break;
997                 case Opt_nolock:
998                         mnt->flags |= NFS_MOUNT_NONLM;
999                         break;
1000                 case Opt_v2:
1001                         mnt->flags &= ~NFS_MOUNT_VER3;
1002                         break;
1003                 case Opt_v3:
1004                         mnt->flags |= NFS_MOUNT_VER3;
1005                         break;
1006                 case Opt_udp:
1007                         mnt->flags &= ~NFS_MOUNT_TCP;
1008                         mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1009                         break;
1010                 case Opt_tcp:
1011                         mnt->flags |= NFS_MOUNT_TCP;
1012                         mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1013                         break;
1014                 case Opt_rdma:
1015                         mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1016                         mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1017                         break;
1018                 case Opt_acl:
1019                         mnt->flags &= ~NFS_MOUNT_NOACL;
1020                         break;
1021                 case Opt_noacl:
1022                         mnt->flags |= NFS_MOUNT_NOACL;
1023                         break;
1024                 case Opt_rdirplus:
1025                         mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1026                         break;
1027                 case Opt_nordirplus:
1028                         mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1029                         break;
1030                 case Opt_sharecache:
1031                         mnt->flags &= ~NFS_MOUNT_UNSHARED;
1032                         break;
1033                 case Opt_nosharecache:
1034                         mnt->flags |= NFS_MOUNT_UNSHARED;
1035                         break;
1036
1037                 /*
1038                  * options that take numeric values
1039                  */
1040                 case Opt_port:
1041                         if (match_int(args, &option) ||
1042                             option < 0 || option > USHORT_MAX) {
1043                                 errors++;
1044                                 nfs_parse_invalid_value("port");
1045                         } else
1046                                 mnt->nfs_server.port = option;
1047                         break;
1048                 case Opt_rsize:
1049                         if (match_int(args, &option) || option < 0) {
1050                                 errors++;
1051                                 nfs_parse_invalid_value("rsize");
1052                         } else
1053                                 mnt->rsize = option;
1054                         break;
1055                 case Opt_wsize:
1056                         if (match_int(args, &option) || option < 0) {
1057                                 errors++;
1058                                 nfs_parse_invalid_value("wsize");
1059                         } else
1060                                 mnt->wsize = option;
1061                         break;
1062                 case Opt_bsize:
1063                         if (match_int(args, &option) || option < 0) {
1064                                 errors++;
1065                                 nfs_parse_invalid_value("bsize");
1066                         } else
1067                                 mnt->bsize = option;
1068                         break;
1069                 case Opt_timeo:
1070                         if (match_int(args, &option) || option <= 0) {
1071                                 errors++;
1072                                 nfs_parse_invalid_value("timeo");
1073                         } else
1074                                 mnt->timeo = option;
1075                         break;
1076                 case Opt_retrans:
1077                         if (match_int(args, &option) || option <= 0) {
1078                                 errors++;
1079                                 nfs_parse_invalid_value("retrans");
1080                         } else
1081                                 mnt->retrans = option;
1082                         break;
1083                 case Opt_acregmin:
1084                         if (match_int(args, &option) || option < 0) {
1085                                 errors++;
1086                                 nfs_parse_invalid_value("acregmin");
1087                         } else
1088                                 mnt->acregmin = option;
1089                         break;
1090                 case Opt_acregmax:
1091                         if (match_int(args, &option) || option < 0) {
1092                                 errors++;
1093                                 nfs_parse_invalid_value("acregmax");
1094                         } else
1095                                 mnt->acregmax = option;
1096                         break;
1097                 case Opt_acdirmin:
1098                         if (match_int(args, &option) || option < 0) {
1099                                 errors++;
1100                                 nfs_parse_invalid_value("acdirmin");
1101                         } else
1102                                 mnt->acdirmin = option;
1103                         break;
1104                 case Opt_acdirmax:
1105                         if (match_int(args, &option) || option < 0) {
1106                                 errors++;
1107                                 nfs_parse_invalid_value("acdirmax");
1108                         } else
1109                                 mnt->acdirmax = option;
1110                         break;
1111                 case Opt_actimeo:
1112                         if (match_int(args, &option) || option < 0) {
1113                                 errors++;
1114                                 nfs_parse_invalid_value("actimeo");
1115                         } else
1116                                 mnt->acregmin = mnt->acregmax =
1117                                 mnt->acdirmin = mnt->acdirmax = option;
1118                         break;
1119                 case Opt_namelen:
1120                         if (match_int(args, &option) || option < 0) {
1121                                 errors++;
1122                                 nfs_parse_invalid_value("namlen");
1123                         } else
1124                                 mnt->namlen = option;
1125                         break;
1126                 case Opt_mountport:
1127                         if (match_int(args, &option) ||
1128                             option < 0 || option > USHORT_MAX) {
1129                                 errors++;
1130                                 nfs_parse_invalid_value("mountport");
1131                         } else
1132                                 mnt->mount_server.port = option;
1133                         break;
1134                 case Opt_mountvers:
1135                         if (match_int(args, &option) ||
1136                             option < NFS_MNT_VERSION ||
1137                             option > NFS_MNT3_VERSION) {
1138                                 errors++;
1139                                 nfs_parse_invalid_value("mountvers");
1140                         } else
1141                                 mnt->mount_server.version = option;
1142                         break;
1143                 case Opt_nfsvers:
1144                         if (match_int(args, &option)) {
1145                                 errors++;
1146                                 nfs_parse_invalid_value("nfsvers");
1147                                 break;
1148                         }
1149                         switch (option) {
1150                         case NFS2_VERSION:
1151                                 mnt->flags &= ~NFS_MOUNT_VER3;
1152                                 break;
1153                         case NFS3_VERSION:
1154                                 mnt->flags |= NFS_MOUNT_VER3;
1155                                 break;
1156                         default:
1157                                 errors++;
1158                                 nfs_parse_invalid_value("nfsvers");
1159                         }
1160                         break;
1161
1162                 /*
1163                  * options that take text values
1164                  */
1165                 case Opt_sec:
1166                         string = match_strdup(args);
1167                         if (string == NULL)
1168                                 goto out_nomem;
1169                         rc = nfs_parse_security_flavors(string, mnt);
1170                         kfree(string);
1171                         if (!rc) {
1172                                 errors++;
1173                                 dfprintk(MOUNT, "NFS:   unrecognized "
1174                                                 "security flavor\n");
1175                         }
1176                         break;
1177                 case Opt_proto:
1178                         string = match_strdup(args);
1179                         if (string == NULL)
1180                                 goto out_nomem;
1181                         token = match_token(string,
1182                                             nfs_xprt_protocol_tokens, args);
1183                         kfree(string);
1184
1185                         switch (token) {
1186                         case Opt_xprt_udp:
1187                                 mnt->flags &= ~NFS_MOUNT_TCP;
1188                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1189                                 break;
1190                         case Opt_xprt_tcp:
1191                                 mnt->flags |= NFS_MOUNT_TCP;
1192                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1193                                 break;
1194                         case Opt_xprt_rdma:
1195                                 /* vector side protocols to TCP */
1196                                 mnt->flags |= NFS_MOUNT_TCP;
1197                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1198                                 break;
1199                         default:
1200                                 errors++;
1201                                 dfprintk(MOUNT, "NFS:   unrecognized "
1202                                                 "transport protocol\n");
1203                         }
1204                         break;
1205                 case Opt_mountproto:
1206                         string = match_strdup(args);
1207                         if (string == NULL)
1208                                 goto out_nomem;
1209                         token = match_token(string,
1210                                             nfs_xprt_protocol_tokens, args);
1211                         kfree(string);
1212
1213                         switch (token) {
1214                         case Opt_xprt_udp:
1215                                 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1216                                 break;
1217                         case Opt_xprt_tcp:
1218                                 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1219                                 break;
1220                         case Opt_xprt_rdma: /* not used for side protocols */
1221                         default:
1222                                 errors++;
1223                                 dfprintk(MOUNT, "NFS:   unrecognized "
1224                                                 "transport protocol\n");
1225                         }
1226                         break;
1227                 case Opt_addr:
1228                         string = match_strdup(args);
1229                         if (string == NULL)
1230                                 goto out_nomem;
1231                         nfs_parse_ip_address(string, strlen(string),
1232                                              (struct sockaddr *)
1233                                                 &mnt->nfs_server.address,
1234                                              &mnt->nfs_server.addrlen);
1235                         kfree(string);
1236                         break;
1237                 case Opt_clientaddr:
1238                         string = match_strdup(args);
1239                         if (string == NULL)
1240                                 goto out_nomem;
1241                         kfree(mnt->client_address);
1242                         mnt->client_address = string;
1243                         break;
1244                 case Opt_mounthost:
1245                         string = match_strdup(args);
1246                         if (string == NULL)
1247                                 goto out_nomem;
1248                         kfree(mnt->mount_server.hostname);
1249                         mnt->mount_server.hostname = string;
1250                         break;
1251                 case Opt_mountaddr:
1252                         string = match_strdup(args);
1253                         if (string == NULL)
1254                                 goto out_nomem;
1255                         nfs_parse_ip_address(string, strlen(string),
1256                                              (struct sockaddr *)
1257                                                 &mnt->mount_server.address,
1258                                              &mnt->mount_server.addrlen);
1259                         kfree(string);
1260                         break;
1261                 case Opt_lookupcache:
1262                         string = match_strdup(args);
1263                         if (string == NULL)
1264                                 goto out_nomem;
1265                         token = match_token(string,
1266                                         nfs_lookupcache_tokens, args);
1267                         kfree(string);
1268                         switch (token) {
1269                                 case Opt_lookupcache_all:
1270                                         mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1271                                         break;
1272                                 case Opt_lookupcache_positive:
1273                                         mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1274                                         mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1275                                         break;
1276                                 case Opt_lookupcache_none:
1277                                         mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1278                                         break;
1279                                 default:
1280                                         errors++;
1281                                         dfprintk(MOUNT, "NFS:   invalid "
1282                                                         "lookupcache argument\n");
1283                         };
1284                         break;
1285
1286                 /*
1287                  * Special options
1288                  */
1289                 case Opt_sloppy:
1290                         sloppy = 1;
1291                         dfprintk(MOUNT, "NFS:   relaxing parsing rules\n");
1292                         break;
1293                 case Opt_userspace:
1294                 case Opt_deprecated:
1295                         dfprintk(MOUNT, "NFS:   ignoring mount option "
1296                                         "'%s'\n", p);
1297                         break;
1298
1299                 default:
1300                         errors++;
1301                         dfprintk(MOUNT, "NFS:   unrecognized mount option "
1302                                         "'%s'\n", p);
1303                 }
1304         }
1305
1306         if (errors > 0) {
1307                 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1308                                 errors, (errors == 1 ? "" : "s"));
1309                 if (!sloppy)
1310                         return 0;
1311         }
1312         return 1;
1313
1314 out_nomem:
1315         printk(KERN_INFO "NFS: not enough memory to parse option\n");
1316         return 0;
1317 out_security_failure:
1318         free_secdata(secdata);
1319         printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1320         return 0;
1321 }
1322
1323 /*
1324  * Use the remote server's MOUNT service to request the NFS file handle
1325  * corresponding to the provided path.
1326  */
1327 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1328                          struct nfs_fh *root_fh)
1329 {
1330         struct sockaddr *sap = (struct sockaddr *)&args->mount_server.address;
1331         char *hostname;
1332         int status;
1333
1334         if (args->mount_server.version == 0) {
1335                 if (args->flags & NFS_MOUNT_VER3)
1336                         args->mount_server.version = NFS_MNT3_VERSION;
1337                 else
1338                         args->mount_server.version = NFS_MNT_VERSION;
1339         }
1340
1341         if (args->mount_server.hostname)
1342                 hostname = args->mount_server.hostname;
1343         else
1344                 hostname = args->nfs_server.hostname;
1345
1346         /*
1347          * Construct the mount server's address.
1348          */
1349         if (args->mount_server.address.ss_family == AF_UNSPEC) {
1350                 memcpy(sap, &args->nfs_server.address,
1351                        args->nfs_server.addrlen);
1352                 args->mount_server.addrlen = args->nfs_server.addrlen;
1353         }
1354
1355         /*
1356          * autobind will be used if mount_server.port == 0
1357          */
1358         nfs_set_port(sap, args->mount_server.port);
1359
1360         /*
1361          * Now ask the mount server to map our export path
1362          * to a file handle.
1363          */
1364         status = nfs_mount(sap,
1365                            args->mount_server.addrlen,
1366                            hostname,
1367                            args->nfs_server.export_path,
1368                            args->mount_server.version,
1369                            args->mount_server.protocol,
1370                            root_fh);
1371         if (status == 0)
1372                 return 0;
1373
1374         dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1375                         hostname, status);
1376         return status;
1377 }
1378
1379 static int nfs_parse_simple_hostname(const char *dev_name,
1380                                      char **hostname, size_t maxnamlen,
1381                                      char **export_path, size_t maxpathlen)
1382 {
1383         size_t len;
1384         char *colon, *comma;
1385
1386         colon = strchr(dev_name, ':');
1387         if (colon == NULL)
1388                 goto out_bad_devname;
1389
1390         len = colon - dev_name;
1391         if (len > maxnamlen)
1392                 goto out_hostname;
1393
1394         /* N.B. caller will free nfs_server.hostname in all cases */
1395         *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1396         if (!*hostname)
1397                 goto out_nomem;
1398
1399         /* kill possible hostname list: not supported */
1400         comma = strchr(*hostname, ',');
1401         if (comma != NULL) {
1402                 if (comma == *hostname)
1403                         goto out_bad_devname;
1404                 *comma = '\0';
1405         }
1406
1407         colon++;
1408         len = strlen(colon);
1409         if (len > maxpathlen)
1410                 goto out_path;
1411         *export_path = kstrndup(colon, len, GFP_KERNEL);
1412         if (!*export_path)
1413                 goto out_nomem;
1414
1415         dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1416         return 0;
1417
1418 out_bad_devname:
1419         dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1420         return -EINVAL;
1421
1422 out_nomem:
1423         dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1424         return -ENOMEM;
1425
1426 out_hostname:
1427         dfprintk(MOUNT, "NFS: server hostname too long\n");
1428         return -ENAMETOOLONG;
1429
1430 out_path:
1431         dfprintk(MOUNT, "NFS: export pathname too long\n");
1432         return -ENAMETOOLONG;
1433 }
1434
1435 /*
1436  * Hostname has square brackets around it because it contains one or
1437  * more colons.  We look for the first closing square bracket, and a
1438  * colon must follow it.
1439  */
1440 static int nfs_parse_protected_hostname(const char *dev_name,
1441                                         char **hostname, size_t maxnamlen,
1442                                         char **export_path, size_t maxpathlen)
1443 {
1444         size_t len;
1445         char *start, *end;
1446
1447         start = (char *)(dev_name + 1);
1448
1449         end = strchr(start, ']');
1450         if (end == NULL)
1451                 goto out_bad_devname;
1452         if (*(end + 1) != ':')
1453                 goto out_bad_devname;
1454
1455         len = end - start;
1456         if (len > maxnamlen)
1457                 goto out_hostname;
1458
1459         /* N.B. caller will free nfs_server.hostname in all cases */
1460         *hostname = kstrndup(start, len, GFP_KERNEL);
1461         if (*hostname == NULL)
1462                 goto out_nomem;
1463
1464         end += 2;
1465         len = strlen(end);
1466         if (len > maxpathlen)
1467                 goto out_path;
1468         *export_path = kstrndup(end, len, GFP_KERNEL);
1469         if (!*export_path)
1470                 goto out_nomem;
1471
1472         return 0;
1473
1474 out_bad_devname:
1475         dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1476         return -EINVAL;
1477
1478 out_nomem:
1479         dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1480         return -ENOMEM;
1481
1482 out_hostname:
1483         dfprintk(MOUNT, "NFS: server hostname too long\n");
1484         return -ENAMETOOLONG;
1485
1486 out_path:
1487         dfprintk(MOUNT, "NFS: export pathname too long\n");
1488         return -ENAMETOOLONG;
1489 }
1490
1491 /*
1492  * Split "dev_name" into "hostname:export_path".
1493  *
1494  * The leftmost colon demarks the split between the server's hostname
1495  * and the export path.  If the hostname starts with a left square
1496  * bracket, then it may contain colons.
1497  *
1498  * Note: caller frees hostname and export path, even on error.
1499  */
1500 static int nfs_parse_devname(const char *dev_name,
1501                              char **hostname, size_t maxnamlen,
1502                              char **export_path, size_t maxpathlen)
1503 {
1504         if (*dev_name == '[')
1505                 return nfs_parse_protected_hostname(dev_name,
1506                                                     hostname, maxnamlen,
1507                                                     export_path, maxpathlen);
1508
1509         return nfs_parse_simple_hostname(dev_name,
1510                                          hostname, maxnamlen,
1511                                          export_path, maxpathlen);
1512 }
1513
1514 /*
1515  * Validate the NFS2/NFS3 mount data
1516  * - fills in the mount root filehandle
1517  *
1518  * For option strings, user space handles the following behaviors:
1519  *
1520  * + DNS: mapping server host name to IP address ("addr=" option)
1521  *
1522  * + failure mode: how to behave if a mount request can't be handled
1523  *   immediately ("fg/bg" option)
1524  *
1525  * + retry: how often to retry a mount request ("retry=" option)
1526  *
1527  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1528  *   mountproto=tcp after mountproto=udp, and so on
1529  */
1530 static int nfs_validate_mount_data(void *options,
1531                                    struct nfs_parsed_mount_data *args,
1532                                    struct nfs_fh *mntfh,
1533                                    const char *dev_name)
1534 {
1535         struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1536
1537         if (data == NULL)
1538                 goto out_no_data;
1539
1540         args->flags             = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1541         args->rsize             = NFS_MAX_FILE_IO_SIZE;
1542         args->wsize             = NFS_MAX_FILE_IO_SIZE;
1543         args->acregmin          = NFS_DEF_ACREGMIN;
1544         args->acregmax          = NFS_DEF_ACREGMAX;
1545         args->acdirmin          = NFS_DEF_ACDIRMIN;
1546         args->acdirmax          = NFS_DEF_ACDIRMAX;
1547         args->mount_server.port = 0;    /* autobind unless user sets port */
1548         args->nfs_server.port   = 0;    /* autobind unless user sets port */
1549         args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1550         args->auth_flavors[0]   = RPC_AUTH_UNIX;
1551
1552         switch (data->version) {
1553         case 1:
1554                 data->namlen = 0;
1555         case 2:
1556                 data->bsize = 0;
1557         case 3:
1558                 if (data->flags & NFS_MOUNT_VER3)
1559                         goto out_no_v3;
1560                 data->root.size = NFS2_FHSIZE;
1561                 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1562         case 4:
1563                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1564                         goto out_no_sec;
1565         case 5:
1566                 memset(data->context, 0, sizeof(data->context));
1567         case 6:
1568                 if (data->flags & NFS_MOUNT_VER3) {
1569                         if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1570                                 goto out_invalid_fh;
1571                         mntfh->size = data->root.size;
1572                 } else
1573                         mntfh->size = NFS2_FHSIZE;
1574
1575
1576                 memcpy(mntfh->data, data->root.data, mntfh->size);
1577                 if (mntfh->size < sizeof(mntfh->data))
1578                         memset(mntfh->data + mntfh->size, 0,
1579                                sizeof(mntfh->data) - mntfh->size);
1580
1581                 /*
1582                  * Translate to nfs_parsed_mount_data, which nfs_fill_super
1583                  * can deal with.
1584                  */
1585                 args->flags             = data->flags & NFS_MOUNT_FLAGMASK;
1586                 args->rsize             = data->rsize;
1587                 args->wsize             = data->wsize;
1588                 args->timeo             = data->timeo;
1589                 args->retrans           = data->retrans;
1590                 args->acregmin          = data->acregmin;
1591                 args->acregmax          = data->acregmax;
1592                 args->acdirmin          = data->acdirmin;
1593                 args->acdirmax          = data->acdirmax;
1594
1595                 memcpy(&args->nfs_server.address, &data->addr,
1596                        sizeof(data->addr));
1597                 args->nfs_server.addrlen = sizeof(data->addr);
1598                 if (!nfs_verify_server_address((struct sockaddr *)
1599                                                 &args->nfs_server.address))
1600                         goto out_no_address;
1601
1602                 if (!(data->flags & NFS_MOUNT_TCP))
1603                         args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1604                 /* N.B. caller will free nfs_server.hostname in all cases */
1605                 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1606                 args->namlen            = data->namlen;
1607                 args->bsize             = data->bsize;
1608
1609                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1610                         args->auth_flavors[0] = data->pseudoflavor;
1611                 if (!args->nfs_server.hostname)
1612                         goto out_nomem;
1613
1614                 /*
1615                  * The legacy version 6 binary mount data from userspace has a
1616                  * field used only to transport selinux information into the
1617                  * the kernel.  To continue to support that functionality we
1618                  * have a touch of selinux knowledge here in the NFS code. The
1619                  * userspace code converted context=blah to just blah so we are
1620                  * converting back to the full string selinux understands.
1621                  */
1622                 if (data->context[0]){
1623 #ifdef CONFIG_SECURITY_SELINUX
1624                         int rc;
1625                         char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1626                         if (!opts_str)
1627                                 return -ENOMEM;
1628                         strcpy(opts_str, "context=");
1629                         data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1630                         strcat(opts_str, &data->context[0]);
1631                         rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1632                         kfree(opts_str);
1633                         if (rc)
1634                                 return rc;
1635 #else
1636                         return -EINVAL;
1637 #endif
1638                 }
1639
1640                 break;
1641         default: {
1642                 int status;
1643
1644                 if (nfs_parse_mount_options((char *)options, args) == 0)
1645                         return -EINVAL;
1646
1647                 if (!nfs_verify_server_address((struct sockaddr *)
1648                                                 &args->nfs_server.address))
1649                         goto out_no_address;
1650
1651                 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1652                                 args->nfs_server.port);
1653
1654                 nfs_set_mount_transport_protocol(args);
1655
1656                 status = nfs_parse_devname(dev_name,
1657                                            &args->nfs_server.hostname,
1658                                            PAGE_SIZE,
1659                                            &args->nfs_server.export_path,
1660                                            NFS_MAXPATHLEN);
1661                 if (!status)
1662                         status = nfs_try_mount(args, mntfh);
1663
1664                 kfree(args->nfs_server.export_path);
1665                 args->nfs_server.export_path = NULL;
1666
1667                 if (status)
1668                         return status;
1669
1670                 break;
1671                 }
1672         }
1673
1674 #ifndef CONFIG_NFS_V3
1675         if (args->flags & NFS_MOUNT_VER3)
1676                 goto out_v3_not_compiled;
1677 #endif /* !CONFIG_NFS_V3 */
1678
1679         return 0;
1680
1681 out_no_data:
1682         dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1683         return -EINVAL;
1684
1685 out_no_v3:
1686         dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1687                  data->version);
1688         return -EINVAL;
1689
1690 out_no_sec:
1691         dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1692         return -EINVAL;
1693
1694 #ifndef CONFIG_NFS_V3
1695 out_v3_not_compiled:
1696         dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1697         return -EPROTONOSUPPORT;
1698 #endif /* !CONFIG_NFS_V3 */
1699
1700 out_nomem:
1701         dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1702         return -ENOMEM;
1703
1704 out_no_address:
1705         dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1706         return -EINVAL;
1707
1708 out_invalid_fh:
1709         dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1710         return -EINVAL;
1711 }
1712
1713 static int
1714 nfs_compare_remount_data(struct nfs_server *nfss,
1715                          struct nfs_parsed_mount_data *data)
1716 {
1717         if (data->flags != nfss->flags ||
1718             data->rsize != nfss->rsize ||
1719             data->wsize != nfss->wsize ||
1720             data->retrans != nfss->client->cl_timeout->to_retries ||
1721             data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1722             data->acregmin != nfss->acregmin / HZ ||
1723             data->acregmax != nfss->acregmax / HZ ||
1724             data->acdirmin != nfss->acdirmin / HZ ||
1725             data->acdirmax != nfss->acdirmax / HZ ||
1726             data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1727             data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1728             memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1729                    data->nfs_server.addrlen) != 0)
1730                 return -EINVAL;
1731
1732         return 0;
1733 }
1734
1735 static int
1736 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1737 {
1738         int error;
1739         struct nfs_server *nfss = sb->s_fs_info;
1740         struct nfs_parsed_mount_data *data;
1741         struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1742         struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
1743         u32 nfsvers = nfss->nfs_client->rpc_ops->version;
1744
1745         /*
1746          * Userspace mount programs that send binary options generally send
1747          * them populated with default values. We have no way to know which
1748          * ones were explicitly specified. Fall back to legacy behavior and
1749          * just return success.
1750          */
1751         if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1752             (nfsvers <= 3 && (!options || (options->version >= 1 &&
1753                                            options->version <= 6))))
1754                 return 0;
1755
1756         data = kzalloc(sizeof(*data), GFP_KERNEL);
1757         if (data == NULL)
1758                 return -ENOMEM;
1759
1760         /* fill out struct with values from existing mount */
1761         data->flags = nfss->flags;
1762         data->rsize = nfss->rsize;
1763         data->wsize = nfss->wsize;
1764         data->retrans = nfss->client->cl_timeout->to_retries;
1765         data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1766         data->acregmin = nfss->acregmin / HZ;
1767         data->acregmax = nfss->acregmax / HZ;
1768         data->acdirmin = nfss->acdirmin / HZ;
1769         data->acdirmax = nfss->acdirmax / HZ;
1770         data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1771         data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1772         memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1773                 data->nfs_server.addrlen);
1774
1775         /* overwrite those values with any that were specified */
1776         error = nfs_parse_mount_options((char *)options, data);
1777         if (error < 0)
1778                 goto out;
1779
1780         /* compare new mount options with old ones */
1781         error = nfs_compare_remount_data(nfss, data);
1782 out:
1783         kfree(data);
1784         return error;
1785 }
1786
1787 /*
1788  * Initialise the common bits of the superblock
1789  */
1790 static inline void nfs_initialise_sb(struct super_block *sb)
1791 {
1792         struct nfs_server *server = NFS_SB(sb);
1793
1794         sb->s_magic = NFS_SUPER_MAGIC;
1795
1796         /* We probably want something more informative here */
1797         snprintf(sb->s_id, sizeof(sb->s_id),
1798                  "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1799
1800         if (sb->s_blocksize == 0)
1801                 sb->s_blocksize = nfs_block_bits(server->wsize,
1802                                                  &sb->s_blocksize_bits);
1803
1804         if (server->flags & NFS_MOUNT_NOAC)
1805                 sb->s_flags |= MS_SYNCHRONOUS;
1806
1807         nfs_super_set_maxbytes(sb, server->maxfilesize);
1808 }
1809
1810 /*
1811  * Finish setting up an NFS2/3 superblock
1812  */
1813 static void nfs_fill_super(struct super_block *sb,
1814                            struct nfs_parsed_mount_data *data)
1815 {
1816         struct nfs_server *server = NFS_SB(sb);
1817
1818         sb->s_blocksize_bits = 0;
1819         sb->s_blocksize = 0;
1820         if (data->bsize)
1821                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1822
1823         if (server->flags & NFS_MOUNT_VER3) {
1824                 /* The VFS shouldn't apply the umask to mode bits. We will do
1825                  * so ourselves when necessary.
1826                  */
1827                 sb->s_flags |= MS_POSIXACL;
1828                 sb->s_time_gran = 1;
1829         }
1830
1831         sb->s_op = &nfs_sops;
1832         nfs_initialise_sb(sb);
1833 }
1834
1835 /*
1836  * Finish setting up a cloned NFS2/3 superblock
1837  */
1838 static void nfs_clone_super(struct super_block *sb,
1839                             const struct super_block *old_sb)
1840 {
1841         struct nfs_server *server = NFS_SB(sb);
1842
1843         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1844         sb->s_blocksize = old_sb->s_blocksize;
1845         sb->s_maxbytes = old_sb->s_maxbytes;
1846
1847         if (server->flags & NFS_MOUNT_VER3) {
1848                 /* The VFS shouldn't apply the umask to mode bits. We will do
1849                  * so ourselves when necessary.
1850                  */
1851                 sb->s_flags |= MS_POSIXACL;
1852                 sb->s_time_gran = 1;
1853         }
1854
1855         sb->s_op = old_sb->s_op;
1856         nfs_initialise_sb(sb);
1857 }
1858
1859 #define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1860
1861 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1862 {
1863         const struct nfs_server *a = s->s_fs_info;
1864         const struct rpc_clnt *clnt_a = a->client;
1865         const struct rpc_clnt *clnt_b = b->client;
1866
1867         if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1868                 goto Ebusy;
1869         if (a->nfs_client != b->nfs_client)
1870                 goto Ebusy;
1871         if (a->flags != b->flags)
1872                 goto Ebusy;
1873         if (a->wsize != b->wsize)
1874                 goto Ebusy;
1875         if (a->rsize != b->rsize)
1876                 goto Ebusy;
1877         if (a->acregmin != b->acregmin)
1878                 goto Ebusy;
1879         if (a->acregmax != b->acregmax)
1880                 goto Ebusy;
1881         if (a->acdirmin != b->acdirmin)
1882                 goto Ebusy;
1883         if (a->acdirmax != b->acdirmax)
1884                 goto Ebusy;
1885         if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1886                 goto Ebusy;
1887         return 1;
1888 Ebusy:
1889         return 0;
1890 }
1891
1892 struct nfs_sb_mountdata {
1893         struct nfs_server *server;
1894         int mntflags;
1895 };
1896
1897 static int nfs_set_super(struct super_block *s, void *data)
1898 {
1899         struct nfs_sb_mountdata *sb_mntdata = data;
1900         struct nfs_server *server = sb_mntdata->server;
1901         int ret;
1902
1903         s->s_flags = sb_mntdata->mntflags;
1904         s->s_fs_info = server;
1905         ret = set_anon_super(s, server);
1906         if (ret == 0)
1907                 server->s_dev = s->s_dev;
1908         return ret;
1909 }
1910
1911 static int nfs_compare_super_address(struct nfs_server *server1,
1912                                      struct nfs_server *server2)
1913 {
1914         struct sockaddr *sap1, *sap2;
1915
1916         sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1917         sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1918
1919         if (sap1->sa_family != sap2->sa_family)
1920                 return 0;
1921
1922         switch (sap1->sa_family) {
1923         case AF_INET: {
1924                 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1925                 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1926                 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1927                         return 0;
1928                 if (sin1->sin_port != sin2->sin_port)
1929                         return 0;
1930                 break;
1931         }
1932         case AF_INET6: {
1933                 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1934                 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1935                 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1936                         return 0;
1937                 if (sin1->sin6_port != sin2->sin6_port)
1938                         return 0;
1939                 break;
1940         }
1941         default:
1942                 return 0;
1943         }
1944
1945         return 1;
1946 }
1947
1948 static int nfs_compare_super(struct super_block *sb, void *data)
1949 {
1950         struct nfs_sb_mountdata *sb_mntdata = data;
1951         struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1952         int mntflags = sb_mntdata->mntflags;
1953
1954         if (!nfs_compare_super_address(old, server))
1955                 return 0;
1956         /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1957         if (old->flags & NFS_MOUNT_UNSHARED)
1958                 return 0;
1959         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1960                 return 0;
1961         return nfs_compare_mount_options(sb, server, mntflags);
1962 }
1963
1964 static int nfs_bdi_register(struct nfs_server *server)
1965 {
1966         return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1967 }
1968
1969 static int nfs_get_sb(struct file_system_type *fs_type,
1970         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1971 {
1972         struct nfs_server *server = NULL;
1973         struct super_block *s;
1974         struct nfs_parsed_mount_data *data;
1975         struct nfs_fh *mntfh;
1976         struct dentry *mntroot;
1977         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1978         struct nfs_sb_mountdata sb_mntdata = {
1979                 .mntflags = flags,
1980         };
1981         int error = -ENOMEM;
1982
1983         data = kzalloc(sizeof(*data), GFP_KERNEL);
1984         mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1985         if (data == NULL || mntfh == NULL)
1986                 goto out_free_fh;
1987
1988         security_init_mnt_opts(&data->lsm_opts);
1989
1990         /* Validate the mount data */
1991         error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
1992         if (error < 0)
1993                 goto out;
1994
1995         /* Get a volume representation */
1996         server = nfs_create_server(data, mntfh);
1997         if (IS_ERR(server)) {
1998                 error = PTR_ERR(server);
1999                 goto out;
2000         }
2001         sb_mntdata.server = server;
2002
2003         if (server->flags & NFS_MOUNT_UNSHARED)
2004                 compare_super = NULL;
2005
2006         /* Get a superblock - note that we may end up sharing one that already exists */
2007         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2008         if (IS_ERR(s)) {
2009                 error = PTR_ERR(s);
2010                 goto out_err_nosb;
2011         }
2012
2013         if (s->s_fs_info != server) {
2014                 nfs_free_server(server);
2015                 server = NULL;
2016         } else {
2017                 error = nfs_bdi_register(server);
2018                 if (error)
2019                         goto error_splat_super;
2020         }
2021
2022         if (!s->s_root) {
2023                 /* initial superblock/root creation */
2024                 nfs_fill_super(s, data);
2025         }
2026
2027         mntroot = nfs_get_root(s, mntfh);
2028         if (IS_ERR(mntroot)) {
2029                 error = PTR_ERR(mntroot);
2030                 goto error_splat_super;
2031         }
2032
2033         error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2034         if (error)
2035                 goto error_splat_root;
2036
2037         s->s_flags |= MS_ACTIVE;
2038         mnt->mnt_sb = s;
2039         mnt->mnt_root = mntroot;
2040         error = 0;
2041
2042 out:
2043         kfree(data->nfs_server.hostname);
2044         kfree(data->mount_server.hostname);
2045         security_free_mnt_opts(&data->lsm_opts);
2046 out_free_fh:
2047         kfree(mntfh);
2048         kfree(data);
2049         return error;
2050
2051 out_err_nosb:
2052         nfs_free_server(server);
2053         goto out;
2054
2055 error_splat_root:
2056         dput(mntroot);
2057 error_splat_super:
2058         up_write(&s->s_umount);
2059         deactivate_super(s);
2060         goto out;
2061 }
2062
2063 /*
2064  * Destroy an NFS2/3 superblock
2065  */
2066 static void nfs_kill_super(struct super_block *s)
2067 {
2068         struct nfs_server *server = NFS_SB(s);
2069
2070         bdi_unregister(&server->backing_dev_info);
2071         kill_anon_super(s);
2072         nfs_free_server(server);
2073 }
2074
2075 /*
2076  * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2077  */
2078 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2079                            const char *dev_name, void *raw_data,
2080                            struct vfsmount *mnt)
2081 {
2082         struct nfs_clone_mount *data = raw_data;
2083         struct super_block *s;
2084         struct nfs_server *server;
2085         struct dentry *mntroot;
2086         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2087         struct nfs_sb_mountdata sb_mntdata = {
2088                 .mntflags = flags,
2089         };
2090         int error;
2091
2092         dprintk("--> nfs_xdev_get_sb()\n");
2093
2094         /* create a new volume representation */
2095         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2096         if (IS_ERR(server)) {
2097                 error = PTR_ERR(server);
2098                 goto out_err_noserver;
2099         }
2100         sb_mntdata.server = server;
2101
2102         if (server->flags & NFS_MOUNT_UNSHARED)
2103                 compare_super = NULL;
2104
2105         /* Get a superblock - note that we may end up sharing one that already exists */
2106         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2107         if (IS_ERR(s)) {
2108                 error = PTR_ERR(s);
2109                 goto out_err_nosb;
2110         }
2111
2112         if (s->s_fs_info != server) {
2113                 nfs_free_server(server);
2114                 server = NULL;
2115         } else {
2116                 error = nfs_bdi_register(server);
2117                 if (error)
2118                         goto error_splat_super;
2119         }
2120
2121         if (!s->s_root) {
2122                 /* initial superblock/root creation */
2123                 nfs_clone_super(s, data->sb);
2124         }
2125
2126         mntroot = nfs_get_root(s, data->fh);
2127         if (IS_ERR(mntroot)) {
2128                 error = PTR_ERR(mntroot);
2129                 goto error_splat_super;
2130         }
2131         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2132                 dput(mntroot);
2133                 error = -ESTALE;
2134                 goto error_splat_super;
2135         }
2136
2137         s->s_flags |= MS_ACTIVE;
2138         mnt->mnt_sb = s;
2139         mnt->mnt_root = mntroot;
2140
2141         /* clone any lsm security options from the parent to the new sb */
2142         security_sb_clone_mnt_opts(data->sb, s);
2143
2144         dprintk("<-- nfs_xdev_get_sb() = 0\n");
2145         return 0;
2146
2147 out_err_nosb:
2148         nfs_free_server(server);
2149 out_err_noserver:
2150         dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2151         return error;
2152
2153 error_splat_super:
2154         up_write(&s->s_umount);
2155         deactivate_super(s);
2156         dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2157         return error;
2158 }
2159
2160 #ifdef CONFIG_NFS_V4
2161
2162 /*
2163  * Finish setting up a cloned NFS4 superblock
2164  */
2165 static void nfs4_clone_super(struct super_block *sb,
2166                             const struct super_block *old_sb)
2167 {
2168         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2169         sb->s_blocksize = old_sb->s_blocksize;
2170         sb->s_maxbytes = old_sb->s_maxbytes;
2171         sb->s_time_gran = 1;
2172         sb->s_op = old_sb->s_op;
2173         nfs_initialise_sb(sb);
2174 }
2175
2176 /*
2177  * Set up an NFS4 superblock
2178  */
2179 static void nfs4_fill_super(struct super_block *sb)
2180 {
2181         sb->s_time_gran = 1;
2182         sb->s_op = &nfs4_sops;
2183         nfs_initialise_sb(sb);
2184 }
2185
2186 /*
2187  * Validate NFSv4 mount options
2188  */
2189 static int nfs4_validate_mount_data(void *options,
2190                                     struct nfs_parsed_mount_data *args,
2191                                     const char *dev_name)
2192 {
2193         struct sockaddr_in *ap;
2194         struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2195         char *c;
2196
2197         if (data == NULL)
2198                 goto out_no_data;
2199
2200         args->rsize             = NFS_MAX_FILE_IO_SIZE;
2201         args->wsize             = NFS_MAX_FILE_IO_SIZE;
2202         args->acregmin          = NFS_DEF_ACREGMIN;
2203         args->acregmax          = NFS_DEF_ACREGMAX;
2204         args->acdirmin          = NFS_DEF_ACDIRMIN;
2205         args->acdirmax          = NFS_DEF_ACDIRMAX;
2206         args->nfs_server.port   = NFS_PORT; /* 2049 unless user set port= */
2207         args->auth_flavors[0]   = RPC_AUTH_UNIX;
2208         args->auth_flavor_len   = 0;
2209
2210         switch (data->version) {
2211         case 1:
2212                 ap = (struct sockaddr_in *)&args->nfs_server.address;
2213                 if (data->host_addrlen > sizeof(args->nfs_server.address))
2214                         goto out_no_address;
2215                 if (data->host_addrlen == 0)
2216                         goto out_no_address;
2217                 args->nfs_server.addrlen = data->host_addrlen;
2218                 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
2219                         return -EFAULT;
2220                 if (!nfs_verify_server_address((struct sockaddr *)
2221                                                 &args->nfs_server.address))
2222                         goto out_no_address;
2223
2224                 if (data->auth_flavourlen) {
2225                         if (data->auth_flavourlen > 1)
2226                                 goto out_inval_auth;
2227                         if (copy_from_user(&args->auth_flavors[0],
2228                                            data->auth_flavours,
2229                                            sizeof(args->auth_flavors[0])))
2230                                 return -EFAULT;
2231                 }
2232
2233                 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2234                 if (IS_ERR(c))
2235                         return PTR_ERR(c);
2236                 args->nfs_server.hostname = c;
2237
2238                 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2239                 if (IS_ERR(c))
2240                         return PTR_ERR(c);
2241                 args->nfs_server.export_path = c;
2242                 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2243
2244                 c = strndup_user(data->client_addr.data, 16);
2245                 if (IS_ERR(c))
2246                         return PTR_ERR(c);
2247                 args->client_address = c;
2248
2249                 /*
2250                  * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2251                  * can deal with.
2252                  */
2253
2254                 args->flags     = data->flags & NFS4_MOUNT_FLAGMASK;
2255                 args->rsize     = data->rsize;
2256                 args->wsize     = data->wsize;
2257                 args->timeo     = data->timeo;
2258                 args->retrans   = data->retrans;
2259                 args->acregmin  = data->acregmin;
2260                 args->acregmax  = data->acregmax;
2261                 args->acdirmin  = data->acdirmin;
2262                 args->acdirmax  = data->acdirmax;
2263                 args->nfs_server.protocol = data->proto;
2264                 nfs_validate_transport_protocol(args);
2265
2266                 break;
2267         default: {
2268                 int status;
2269
2270                 if (nfs_parse_mount_options((char *)options, args) == 0)
2271                         return -EINVAL;
2272
2273                 if (!nfs_verify_server_address((struct sockaddr *)
2274                                                 &args->nfs_server.address))
2275                         return -EINVAL;
2276
2277                 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2278                                 args->nfs_server.port);
2279
2280                 nfs_validate_transport_protocol(args);
2281
2282                 if (args->auth_flavor_len > 1)
2283                         goto out_inval_auth;
2284
2285                 if (args->client_address == NULL)
2286                         goto out_no_client_address;
2287
2288                 status = nfs_parse_devname(dev_name,
2289                                            &args->nfs_server.hostname,
2290                                            NFS4_MAXNAMLEN,
2291                                            &args->nfs_server.export_path,
2292                                            NFS4_MAXPATHLEN);
2293                 if (status < 0)
2294                         return status;
2295
2296                 break;
2297                 }
2298         }
2299
2300         return 0;
2301
2302 out_no_data:
2303         dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2304         return -EINVAL;
2305
2306 out_inval_auth:
2307         dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2308                  data->auth_flavourlen);
2309         return -EINVAL;
2310
2311 out_no_address:
2312         dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2313         return -EINVAL;
2314
2315 out_no_client_address:
2316         dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2317         return -EINVAL;
2318 }
2319
2320 /*
2321  * Get the superblock for an NFS4 mountpoint
2322  */
2323 static int nfs4_get_sb(struct file_system_type *fs_type,
2324         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2325 {
2326         struct nfs_parsed_mount_data *data;
2327         struct super_block *s;
2328         struct nfs_server *server;
2329         struct nfs_fh *mntfh;
2330         struct dentry *mntroot;
2331         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2332         struct nfs_sb_mountdata sb_mntdata = {
2333                 .mntflags = flags,
2334         };
2335         int error = -ENOMEM;
2336
2337         data = kzalloc(sizeof(*data), GFP_KERNEL);
2338         mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2339         if (data == NULL || mntfh == NULL)
2340                 goto out_free_fh;
2341
2342         security_init_mnt_opts(&data->lsm_opts);
2343
2344         /* Validate the mount data */
2345         error = nfs4_validate_mount_data(raw_data, data, dev_name);
2346         if (error < 0)
2347                 goto out;
2348
2349         /* Get a volume representation */
2350         server = nfs4_create_server(data, mntfh);
2351         if (IS_ERR(server)) {
2352                 error = PTR_ERR(server);
2353                 goto out;
2354         }
2355         sb_mntdata.server = server;
2356
2357         if (server->flags & NFS4_MOUNT_UNSHARED)
2358                 compare_super = NULL;
2359
2360         /* Get a superblock - note that we may end up sharing one that already exists */
2361         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2362         if (IS_ERR(s)) {
2363                 error = PTR_ERR(s);
2364                 goto out_free;
2365         }
2366
2367         if (s->s_fs_info != server) {
2368                 nfs_free_server(server);
2369                 server = NULL;
2370         } else {
2371                 error = nfs_bdi_register(server);
2372                 if (error)
2373                         goto error_splat_super;
2374         }
2375
2376         if (!s->s_root) {
2377                 /* initial superblock/root creation */
2378                 nfs4_fill_super(s);
2379         }
2380
2381         mntroot = nfs4_get_root(s, mntfh);
2382         if (IS_ERR(mntroot)) {
2383                 error = PTR_ERR(mntroot);
2384                 goto error_splat_super;
2385         }
2386
2387         error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2388         if (error)
2389                 goto error_splat_root;
2390
2391         s->s_flags |= MS_ACTIVE;
2392         mnt->mnt_sb = s;
2393         mnt->mnt_root = mntroot;
2394         error = 0;
2395
2396 out:
2397         kfree(data->client_address);
2398         kfree(data->nfs_server.export_path);
2399         kfree(data->nfs_server.hostname);
2400         security_free_mnt_opts(&data->lsm_opts);
2401 out_free_fh:
2402         kfree(mntfh);
2403         kfree(data);
2404         return error;
2405
2406 out_free:
2407         nfs_free_server(server);
2408         goto out;
2409
2410 error_splat_root:
2411         dput(mntroot);
2412 error_splat_super:
2413         up_write(&s->s_umount);
2414         deactivate_super(s);
2415         goto out;
2416 }
2417
2418 static void nfs4_kill_super(struct super_block *sb)
2419 {
2420         struct nfs_server *server = NFS_SB(sb);
2421
2422         nfs_return_all_delegations(sb);
2423         kill_anon_super(sb);
2424
2425         nfs4_renewd_prepare_shutdown(server);
2426         nfs_free_server(server);
2427 }
2428
2429 /*
2430  * Clone an NFS4 server record on xdev traversal (FSID-change)
2431  */
2432 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2433                             const char *dev_name, void *raw_data,
2434                             struct vfsmount *mnt)
2435 {
2436         struct nfs_clone_mount *data = raw_data;
2437         struct super_block *s;
2438         struct nfs_server *server;
2439         struct dentry *mntroot;
2440         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2441         struct nfs_sb_mountdata sb_mntdata = {
2442                 .mntflags = flags,
2443         };
2444         int error;
2445
2446         dprintk("--> nfs4_xdev_get_sb()\n");
2447
2448         /* create a new volume representation */
2449         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2450         if (IS_ERR(server)) {
2451                 error = PTR_ERR(server);
2452                 goto out_err_noserver;
2453         }
2454         sb_mntdata.server = server;
2455
2456         if (server->flags & NFS4_MOUNT_UNSHARED)
2457                 compare_super = NULL;
2458
2459         /* Get a superblock - note that we may end up sharing one that already exists */
2460         s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2461         if (IS_ERR(s)) {
2462                 error = PTR_ERR(s);
2463                 goto out_err_nosb;
2464         }
2465
2466         if (s->s_fs_info != server) {
2467                 nfs_free_server(server);
2468                 server = NULL;
2469         } else {
2470                 error = nfs_bdi_register(server);
2471                 if (error)
2472                         goto error_splat_super;
2473         }
2474
2475         if (!s->s_root) {
2476                 /* initial superblock/root creation */
2477                 nfs4_clone_super(s, data->sb);
2478         }
2479
2480         mntroot = nfs4_get_root(s, data->fh);
2481         if (IS_ERR(mntroot)) {
2482                 error = PTR_ERR(mntroot);
2483                 goto error_splat_super;
2484         }
2485         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2486                 dput(mntroot);
2487                 error = -ESTALE;
2488                 goto error_splat_super;
2489         }
2490
2491         s->s_flags |= MS_ACTIVE;
2492         mnt->mnt_sb = s;
2493         mnt->mnt_root = mntroot;
2494
2495         security_sb_clone_mnt_opts(data->sb, s);
2496
2497         dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2498         return 0;
2499
2500 out_err_nosb:
2501         nfs_free_server(server);
2502 out_err_noserver:
2503         dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2504         return error;
2505
2506 error_splat_super:
2507         up_write(&s->s_umount);
2508         deactivate_super(s);
2509         dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2510         return error;
2511 }
2512
2513 /*
2514  * Create an NFS4 server record on referral traversal
2515  */
2516 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2517                                 const char *dev_name, void *raw_data,
2518                                 struct vfsmount *mnt)
2519 {
2520         struct nfs_clone_mount *data = raw_data;
2521         struct super_block *s;
2522         struct nfs_server *server;
2523         struct dentry *mntroot;
2524         struct nfs_fh mntfh;
2525         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2526         struct nfs_sb_mountdata sb_mntdata = {
2527                 .mntflags = flags,
2528         };
2529         int error;
2530
2531         dprintk("--> nfs4_referral_get_sb()\n");
2532
2533         /* create a new volume representation */
2534         server = nfs4_create_referral_server(data, &mntfh);
2535         if (IS_ERR(server)) {
2536                 error = PTR_ERR(server);
2537                 goto out_err_noserver;
2538         }
2539         sb_mntdata.server = server;
2540
2541         if (server->flags & NFS4_MOUNT_UNSHARED)
2542                 compare_super = NULL;
2543
2544         /* Get a superblock - note that we may end up sharing one that already exists */
2545         s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2546         if (IS_ERR(s)) {
2547                 error = PTR_ERR(s);
2548                 goto out_err_nosb;
2549         }
2550
2551         if (s->s_fs_info != server) {
2552                 nfs_free_server(server);
2553                 server = NULL;
2554         } else {
2555                 error = nfs_bdi_register(server);
2556                 if (error)
2557                         goto error_splat_super;
2558         }
2559
2560         if (!s->s_root) {
2561                 /* initial superblock/root creation */
2562                 nfs4_fill_super(s);
2563         }
2564
2565         mntroot = nfs4_get_root(s, &mntfh);
2566         if (IS_ERR(mntroot)) {
2567                 error = PTR_ERR(mntroot);
2568                 goto error_splat_super;
2569         }
2570         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2571                 dput(mntroot);
2572                 error = -ESTALE;
2573                 goto error_splat_super;
2574         }
2575
2576         s->s_flags |= MS_ACTIVE;
2577         mnt->mnt_sb = s;
2578         mnt->mnt_root = mntroot;
2579
2580         security_sb_clone_mnt_opts(data->sb, s);
2581
2582         dprintk("<-- nfs4_referral_get_sb() = 0\n");
2583         return 0;
2584
2585 out_err_nosb:
2586         nfs_free_server(server);
2587 out_err_noserver:
2588         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2589         return error;
2590
2591 error_splat_super:
2592         up_write(&s->s_umount);
2593         deactivate_super(s);
2594         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2595         return error;
2596 }
2597
2598 #endif /* CONFIG_NFS_V4 */