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