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