NFS: Add support for AF_INET6 addresses in __nfs_find_client()
[linux-2.6] / fs / nfs / super.c
1 /*
2  *  linux/fs/nfs/super.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs superblock handling functions
7  *
8  *  Modularised by Alan Cox <Alan.Cox@linux.org>, 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/nfs_xdr.h>
51 #include <linux/magic.h>
52 #include <linux/parser.h>
53
54 #include <asm/system.h>
55 #include <asm/uaccess.h>
56
57 #include "nfs4_fs.h"
58 #include "callback.h"
59 #include "delegation.h"
60 #include "iostat.h"
61 #include "internal.h"
62
63 #define NFSDBG_FACILITY         NFSDBG_VFS
64
65 enum {
66         /* Mount options that take no arguments */
67         Opt_soft, Opt_hard,
68         Opt_intr, Opt_nointr,
69         Opt_posix, Opt_noposix,
70         Opt_cto, Opt_nocto,
71         Opt_ac, Opt_noac,
72         Opt_lock, Opt_nolock,
73         Opt_v2, Opt_v3,
74         Opt_udp, Opt_tcp, Opt_rdma,
75         Opt_acl, Opt_noacl,
76         Opt_rdirplus, Opt_nordirplus,
77         Opt_sharecache, Opt_nosharecache,
78
79         /* Mount options that take integer arguments */
80         Opt_port,
81         Opt_rsize, Opt_wsize, Opt_bsize,
82         Opt_timeo, Opt_retrans,
83         Opt_acregmin, Opt_acregmax,
84         Opt_acdirmin, Opt_acdirmax,
85         Opt_actimeo,
86         Opt_namelen,
87         Opt_mountport,
88         Opt_mountvers,
89         Opt_nfsvers,
90
91         /* Mount options that take string arguments */
92         Opt_sec, Opt_proto, Opt_mountproto,
93         Opt_addr, Opt_mountaddr, Opt_clientaddr,
94
95         /* Mount options that are ignored */
96         Opt_userspace, Opt_deprecated,
97
98         Opt_err
99 };
100
101 static match_table_t nfs_mount_option_tokens = {
102         { Opt_userspace, "bg" },
103         { Opt_userspace, "fg" },
104         { Opt_soft, "soft" },
105         { Opt_hard, "hard" },
106         { Opt_intr, "intr" },
107         { Opt_nointr, "nointr" },
108         { Opt_posix, "posix" },
109         { Opt_noposix, "noposix" },
110         { Opt_cto, "cto" },
111         { Opt_nocto, "nocto" },
112         { Opt_ac, "ac" },
113         { Opt_noac, "noac" },
114         { Opt_lock, "lock" },
115         { Opt_nolock, "nolock" },
116         { Opt_v2, "v2" },
117         { Opt_v3, "v3" },
118         { Opt_udp, "udp" },
119         { Opt_tcp, "tcp" },
120         { Opt_rdma, "rdma" },
121         { Opt_acl, "acl" },
122         { Opt_noacl, "noacl" },
123         { Opt_rdirplus, "rdirplus" },
124         { Opt_nordirplus, "nordirplus" },
125         { Opt_sharecache, "sharecache" },
126         { Opt_nosharecache, "nosharecache" },
127
128         { Opt_port, "port=%u" },
129         { Opt_rsize, "rsize=%u" },
130         { Opt_wsize, "wsize=%u" },
131         { Opt_bsize, "bsize=%u" },
132         { Opt_timeo, "timeo=%u" },
133         { Opt_retrans, "retrans=%u" },
134         { Opt_acregmin, "acregmin=%u" },
135         { Opt_acregmax, "acregmax=%u" },
136         { Opt_acdirmin, "acdirmin=%u" },
137         { Opt_acdirmax, "acdirmax=%u" },
138         { Opt_actimeo, "actimeo=%u" },
139         { Opt_userspace, "retry=%u" },
140         { Opt_namelen, "namlen=%u" },
141         { Opt_mountport, "mountport=%u" },
142         { Opt_mountvers, "mountvers=%u" },
143         { Opt_nfsvers, "nfsvers=%u" },
144         { Opt_nfsvers, "vers=%u" },
145
146         { Opt_sec, "sec=%s" },
147         { Opt_proto, "proto=%s" },
148         { Opt_mountproto, "mountproto=%s" },
149         { Opt_addr, "addr=%s" },
150         { Opt_clientaddr, "clientaddr=%s" },
151         { Opt_userspace, "mounthost=%s" },
152         { Opt_mountaddr, "mountaddr=%s" },
153
154         { Opt_err, NULL }
155 };
156
157 enum {
158         Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
159
160         Opt_xprt_err
161 };
162
163 static match_table_t nfs_xprt_protocol_tokens = {
164         { Opt_xprt_udp, "udp" },
165         { Opt_xprt_tcp, "tcp" },
166         { Opt_xprt_rdma, "rdma" },
167
168         { Opt_xprt_err, NULL }
169 };
170
171 enum {
172         Opt_sec_none, Opt_sec_sys,
173         Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
174         Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
175         Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
176
177         Opt_sec_err
178 };
179
180 static match_table_t nfs_secflavor_tokens = {
181         { Opt_sec_none, "none" },
182         { Opt_sec_none, "null" },
183         { Opt_sec_sys, "sys" },
184
185         { Opt_sec_krb5, "krb5" },
186         { Opt_sec_krb5i, "krb5i" },
187         { Opt_sec_krb5p, "krb5p" },
188
189         { Opt_sec_lkey, "lkey" },
190         { Opt_sec_lkeyi, "lkeyi" },
191         { Opt_sec_lkeyp, "lkeyp" },
192
193         { Opt_sec_err, NULL }
194 };
195
196
197 static void nfs_umount_begin(struct vfsmount *, int);
198 static int  nfs_statfs(struct dentry *, struct kstatfs *);
199 static int  nfs_show_options(struct seq_file *, struct vfsmount *);
200 static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
201 static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
202 static int nfs_xdev_get_sb(struct file_system_type *fs_type,
203                 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
204 static void nfs_kill_super(struct super_block *);
205 static void nfs_put_super(struct super_block *);
206
207 static struct file_system_type nfs_fs_type = {
208         .owner          = THIS_MODULE,
209         .name           = "nfs",
210         .get_sb         = nfs_get_sb,
211         .kill_sb        = nfs_kill_super,
212         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
213 };
214
215 struct file_system_type nfs_xdev_fs_type = {
216         .owner          = THIS_MODULE,
217         .name           = "nfs",
218         .get_sb         = nfs_xdev_get_sb,
219         .kill_sb        = nfs_kill_super,
220         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
221 };
222
223 static const struct super_operations nfs_sops = {
224         .alloc_inode    = nfs_alloc_inode,
225         .destroy_inode  = nfs_destroy_inode,
226         .write_inode    = nfs_write_inode,
227         .put_super      = nfs_put_super,
228         .statfs         = nfs_statfs,
229         .clear_inode    = nfs_clear_inode,
230         .umount_begin   = nfs_umount_begin,
231         .show_options   = nfs_show_options,
232         .show_stats     = nfs_show_stats,
233 };
234
235 #ifdef CONFIG_NFS_V4
236 static int nfs4_get_sb(struct file_system_type *fs_type,
237         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
238 static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
239         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
240 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
241         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
242 static void nfs4_kill_super(struct super_block *sb);
243
244 static struct file_system_type nfs4_fs_type = {
245         .owner          = THIS_MODULE,
246         .name           = "nfs4",
247         .get_sb         = nfs4_get_sb,
248         .kill_sb        = nfs4_kill_super,
249         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
250 };
251
252 struct file_system_type nfs4_xdev_fs_type = {
253         .owner          = THIS_MODULE,
254         .name           = "nfs4",
255         .get_sb         = nfs4_xdev_get_sb,
256         .kill_sb        = nfs4_kill_super,
257         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
258 };
259
260 struct file_system_type nfs4_referral_fs_type = {
261         .owner          = THIS_MODULE,
262         .name           = "nfs4",
263         .get_sb         = nfs4_referral_get_sb,
264         .kill_sb        = nfs4_kill_super,
265         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
266 };
267
268 static const struct super_operations nfs4_sops = {
269         .alloc_inode    = nfs_alloc_inode,
270         .destroy_inode  = nfs_destroy_inode,
271         .write_inode    = nfs_write_inode,
272         .statfs         = nfs_statfs,
273         .clear_inode    = nfs4_clear_inode,
274         .umount_begin   = nfs_umount_begin,
275         .show_options   = nfs_show_options,
276         .show_stats     = nfs_show_stats,
277 };
278 #endif
279
280 static struct shrinker acl_shrinker = {
281         .shrink         = nfs_access_cache_shrinker,
282         .seeks          = DEFAULT_SEEKS,
283 };
284
285 /*
286  * Register the NFS filesystems
287  */
288 int __init register_nfs_fs(void)
289 {
290         int ret;
291
292         ret = register_filesystem(&nfs_fs_type);
293         if (ret < 0)
294                 goto error_0;
295
296         ret = nfs_register_sysctl();
297         if (ret < 0)
298                 goto error_1;
299 #ifdef CONFIG_NFS_V4
300         ret = register_filesystem(&nfs4_fs_type);
301         if (ret < 0)
302                 goto error_2;
303 #endif
304         register_shrinker(&acl_shrinker);
305         return 0;
306
307 #ifdef CONFIG_NFS_V4
308 error_2:
309         nfs_unregister_sysctl();
310 #endif
311 error_1:
312         unregister_filesystem(&nfs_fs_type);
313 error_0:
314         return ret;
315 }
316
317 /*
318  * Unregister the NFS filesystems
319  */
320 void __exit unregister_nfs_fs(void)
321 {
322         unregister_shrinker(&acl_shrinker);
323 #ifdef CONFIG_NFS_V4
324         unregister_filesystem(&nfs4_fs_type);
325 #endif
326         nfs_unregister_sysctl();
327         unregister_filesystem(&nfs_fs_type);
328 }
329
330 void nfs_sb_active(struct nfs_server *server)
331 {
332         atomic_inc(&server->active);
333 }
334
335 void nfs_sb_deactive(struct nfs_server *server)
336 {
337         if (atomic_dec_and_test(&server->active))
338                 wake_up(&server->active_wq);
339 }
340
341 static void nfs_put_super(struct super_block *sb)
342 {
343         struct nfs_server *server = NFS_SB(sb);
344         /*
345          * Make sure there are no outstanding ops to this server.
346          * If so, wait for them to finish before allowing the
347          * unmount to continue.
348          */
349         wait_event(server->active_wq, atomic_read(&server->active) == 0);
350 }
351
352 /*
353  * Deliver file system statistics to userspace
354  */
355 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
356 {
357         struct nfs_server *server = NFS_SB(dentry->d_sb);
358         unsigned char blockbits;
359         unsigned long blockres;
360         struct nfs_fh *fh = NFS_FH(dentry->d_inode);
361         struct nfs_fattr fattr;
362         struct nfs_fsstat res = {
363                         .fattr = &fattr,
364         };
365         int error;
366
367         lock_kernel();
368
369         error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
370         if (error < 0)
371                 goto out_err;
372         buf->f_type = NFS_SUPER_MAGIC;
373
374         /*
375          * Current versions of glibc do not correctly handle the
376          * case where f_frsize != f_bsize.  Eventually we want to
377          * report the value of wtmult in this field.
378          */
379         buf->f_frsize = dentry->d_sb->s_blocksize;
380
381         /*
382          * On most *nix systems, f_blocks, f_bfree, and f_bavail
383          * are reported in units of f_frsize.  Linux hasn't had
384          * an f_frsize field in its statfs struct until recently,
385          * thus historically Linux's sys_statfs reports these
386          * fields in units of f_bsize.
387          */
388         buf->f_bsize = dentry->d_sb->s_blocksize;
389         blockbits = dentry->d_sb->s_blocksize_bits;
390         blockres = (1 << blockbits) - 1;
391         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
392         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
393         buf->f_bavail = (res.abytes + blockres) >> blockbits;
394
395         buf->f_files = res.tfiles;
396         buf->f_ffree = res.afiles;
397
398         buf->f_namelen = server->namelen;
399
400         unlock_kernel();
401         return 0;
402
403  out_err:
404         dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
405         unlock_kernel();
406         return error;
407 }
408
409 /*
410  * Map the security flavour number to a name
411  */
412 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
413 {
414         static const struct {
415                 rpc_authflavor_t flavour;
416                 const char *str;
417         } sec_flavours[] = {
418                 { RPC_AUTH_NULL, "null" },
419                 { RPC_AUTH_UNIX, "sys" },
420                 { RPC_AUTH_GSS_KRB5, "krb5" },
421                 { RPC_AUTH_GSS_KRB5I, "krb5i" },
422                 { RPC_AUTH_GSS_KRB5P, "krb5p" },
423                 { RPC_AUTH_GSS_LKEY, "lkey" },
424                 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
425                 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
426                 { RPC_AUTH_GSS_SPKM, "spkm" },
427                 { RPC_AUTH_GSS_SPKMI, "spkmi" },
428                 { RPC_AUTH_GSS_SPKMP, "spkmp" },
429                 { UINT_MAX, "unknown" }
430         };
431         int i;
432
433         for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
434                 if (sec_flavours[i].flavour == flavour)
435                         break;
436         }
437         return sec_flavours[i].str;
438 }
439
440 /*
441  * Describe the mount options in force on this server representation
442  */
443 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
444 {
445         static const struct proc_nfs_info {
446                 int flag;
447                 const char *str;
448                 const char *nostr;
449         } nfs_info[] = {
450                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
451                 { NFS_MOUNT_INTR, ",intr", ",nointr" },
452                 { NFS_MOUNT_NOCTO, ",nocto", "" },
453                 { NFS_MOUNT_NOAC, ",noac", "" },
454                 { NFS_MOUNT_NONLM, ",nolock", "" },
455                 { NFS_MOUNT_NOACL, ",noacl", "" },
456                 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
457                 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
458                 { 0, NULL, NULL }
459         };
460         const struct proc_nfs_info *nfs_infop;
461         struct nfs_client *clp = nfss->nfs_client;
462
463         seq_printf(m, ",vers=%d", clp->rpc_ops->version);
464         seq_printf(m, ",rsize=%d", nfss->rsize);
465         seq_printf(m, ",wsize=%d", nfss->wsize);
466         if (nfss->acregmin != 3*HZ || showdefaults)
467                 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
468         if (nfss->acregmax != 60*HZ || showdefaults)
469                 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
470         if (nfss->acdirmin != 30*HZ || showdefaults)
471                 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
472         if (nfss->acdirmax != 60*HZ || showdefaults)
473                 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
474         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
475                 if (nfss->flags & nfs_infop->flag)
476                         seq_puts(m, nfs_infop->str);
477                 else
478                         seq_puts(m, nfs_infop->nostr);
479         }
480         seq_printf(m, ",proto=%s",
481                    rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
482         seq_printf(m, ",timeo=%lu", 10U * clp->retrans_timeo / HZ);
483         seq_printf(m, ",retrans=%u", clp->retrans_count);
484         seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
485 }
486
487 /*
488  * Describe the mount options on this VFS mountpoint
489  */
490 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
491 {
492         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
493
494         nfs_show_mount_options(m, nfss, 0);
495
496         seq_printf(m, ",addr=%s",
497                         rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
498                                                         RPC_DISPLAY_ADDR));
499
500         return 0;
501 }
502
503 /*
504  * Present statistical information for this VFS mountpoint
505  */
506 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
507 {
508         int i, cpu;
509         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
510         struct rpc_auth *auth = nfss->client->cl_auth;
511         struct nfs_iostats totals = { };
512
513         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
514
515         /*
516          * Display all mount option settings
517          */
518         seq_printf(m, "\n\topts:\t");
519         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
520         seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
521         seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
522         seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
523         nfs_show_mount_options(m, nfss, 1);
524
525         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
526
527         seq_printf(m, "\n\tcaps:\t");
528         seq_printf(m, "caps=0x%x", nfss->caps);
529         seq_printf(m, ",wtmult=%d", nfss->wtmult);
530         seq_printf(m, ",dtsize=%d", nfss->dtsize);
531         seq_printf(m, ",bsize=%d", nfss->bsize);
532         seq_printf(m, ",namelen=%d", nfss->namelen);
533
534 #ifdef CONFIG_NFS_V4
535         if (nfss->nfs_client->rpc_ops->version == 4) {
536                 seq_printf(m, "\n\tnfsv4:\t");
537                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
538                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
539                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
540         }
541 #endif
542
543         /*
544          * Display security flavor in effect for this mount
545          */
546         seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
547         if (auth->au_flavor)
548                 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
549
550         /*
551          * Display superblock I/O counters
552          */
553         for_each_possible_cpu(cpu) {
554                 struct nfs_iostats *stats;
555
556                 preempt_disable();
557                 stats = per_cpu_ptr(nfss->io_stats, cpu);
558
559                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
560                         totals.events[i] += stats->events[i];
561                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
562                         totals.bytes[i] += stats->bytes[i];
563
564                 preempt_enable();
565         }
566
567         seq_printf(m, "\n\tevents:\t");
568         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
569                 seq_printf(m, "%lu ", totals.events[i]);
570         seq_printf(m, "\n\tbytes:\t");
571         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
572                 seq_printf(m, "%Lu ", totals.bytes[i]);
573         seq_printf(m, "\n");
574
575         rpc_print_iostats(m, nfss->client);
576
577         return 0;
578 }
579
580 /*
581  * Begin unmount by attempting to remove all automounted mountpoints we added
582  * in response to xdev traversals and referrals
583  */
584 static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
585 {
586         struct nfs_server *server = NFS_SB(vfsmnt->mnt_sb);
587         struct rpc_clnt *rpc;
588
589         shrink_submounts(vfsmnt, &nfs_automount_list);
590
591         if (!(flags & MNT_FORCE))
592                 return;
593         /* -EIO all pending I/O */
594         rpc = server->client_acl;
595         if (!IS_ERR(rpc))
596                 rpc_killall_tasks(rpc);
597         rpc = server->client;
598         if (!IS_ERR(rpc))
599                 rpc_killall_tasks(rpc);
600 }
601
602 /*
603  * Set the port number in an address.  Be agnostic about the address family.
604  */
605 static void nfs_set_port(struct sockaddr *sap, unsigned short port)
606 {
607         switch (sap->sa_family) {
608         case AF_INET: {
609                 struct sockaddr_in *ap = (struct sockaddr_in *)sap;
610                 ap->sin_port = htons(port);
611                 break;
612         }
613         case AF_INET6: {
614                 struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
615                 ap->sin6_port = htons(port);
616                 break;
617         }
618         }
619 }
620
621 /*
622  * Sanity-check a server address provided by the mount command.
623  *
624  * Address family must be initialized, and address must not be
625  * the ANY address for that family.
626  */
627 static int nfs_verify_server_address(struct sockaddr *addr)
628 {
629         switch (addr->sa_family) {
630         case AF_INET: {
631                 struct sockaddr_in *sa = (struct sockaddr_in *)addr;
632                 return sa->sin_addr.s_addr != INADDR_ANY;
633         }
634         case AF_INET6: {
635                 struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
636                 return !ipv6_addr_any(sa);
637         }
638         }
639
640         return 0;
641 }
642
643 /*
644  * Error-check and convert a string of mount options from user space into
645  * a data structure
646  */
647 static int nfs_parse_mount_options(char *raw,
648                                    struct nfs_parsed_mount_data *mnt)
649 {
650         char *p, *string;
651         unsigned short port = 0;
652
653         if (!raw) {
654                 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
655                 return 1;
656         }
657         dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
658
659         while ((p = strsep(&raw, ",")) != NULL) {
660                 substring_t args[MAX_OPT_ARGS];
661                 int option, token;
662
663                 if (!*p)
664                         continue;
665
666                 dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", p);
667
668                 token = match_token(p, nfs_mount_option_tokens, args);
669                 switch (token) {
670                 case Opt_soft:
671                         mnt->flags |= NFS_MOUNT_SOFT;
672                         break;
673                 case Opt_hard:
674                         mnt->flags &= ~NFS_MOUNT_SOFT;
675                         break;
676                 case Opt_intr:
677                         mnt->flags |= NFS_MOUNT_INTR;
678                         break;
679                 case Opt_nointr:
680                         mnt->flags &= ~NFS_MOUNT_INTR;
681                         break;
682                 case Opt_posix:
683                         mnt->flags |= NFS_MOUNT_POSIX;
684                         break;
685                 case Opt_noposix:
686                         mnt->flags &= ~NFS_MOUNT_POSIX;
687                         break;
688                 case Opt_cto:
689                         mnt->flags &= ~NFS_MOUNT_NOCTO;
690                         break;
691                 case Opt_nocto:
692                         mnt->flags |= NFS_MOUNT_NOCTO;
693                         break;
694                 case Opt_ac:
695                         mnt->flags &= ~NFS_MOUNT_NOAC;
696                         break;
697                 case Opt_noac:
698                         mnt->flags |= NFS_MOUNT_NOAC;
699                         break;
700                 case Opt_lock:
701                         mnt->flags &= ~NFS_MOUNT_NONLM;
702                         break;
703                 case Opt_nolock:
704                         mnt->flags |= NFS_MOUNT_NONLM;
705                         break;
706                 case Opt_v2:
707                         mnt->flags &= ~NFS_MOUNT_VER3;
708                         break;
709                 case Opt_v3:
710                         mnt->flags |= NFS_MOUNT_VER3;
711                         break;
712                 case Opt_udp:
713                         mnt->flags &= ~NFS_MOUNT_TCP;
714                         mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
715                         mnt->timeo = 7;
716                         mnt->retrans = 5;
717                         break;
718                 case Opt_tcp:
719                         mnt->flags |= NFS_MOUNT_TCP;
720                         mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
721                         mnt->timeo = 600;
722                         mnt->retrans = 2;
723                         break;
724                 case Opt_rdma:
725                         mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
726                         mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
727                         mnt->timeo = 600;
728                         mnt->retrans = 2;
729                         break;
730                 case Opt_acl:
731                         mnt->flags &= ~NFS_MOUNT_NOACL;
732                         break;
733                 case Opt_noacl:
734                         mnt->flags |= NFS_MOUNT_NOACL;
735                         break;
736                 case Opt_rdirplus:
737                         mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
738                         break;
739                 case Opt_nordirplus:
740                         mnt->flags |= NFS_MOUNT_NORDIRPLUS;
741                         break;
742                 case Opt_sharecache:
743                         mnt->flags &= ~NFS_MOUNT_UNSHARED;
744                         break;
745                 case Opt_nosharecache:
746                         mnt->flags |= NFS_MOUNT_UNSHARED;
747                         break;
748
749                 case Opt_port:
750                         if (match_int(args, &option))
751                                 return 0;
752                         if (option < 0 || option > 65535)
753                                 return 0;
754                         port = option;
755                         break;
756                 case Opt_rsize:
757                         if (match_int(args, &mnt->rsize))
758                                 return 0;
759                         break;
760                 case Opt_wsize:
761                         if (match_int(args, &mnt->wsize))
762                                 return 0;
763                         break;
764                 case Opt_bsize:
765                         if (match_int(args, &option))
766                                 return 0;
767                         if (option < 0)
768                                 return 0;
769                         mnt->bsize = option;
770                         break;
771                 case Opt_timeo:
772                         if (match_int(args, &mnt->timeo))
773                                 return 0;
774                         break;
775                 case Opt_retrans:
776                         if (match_int(args, &mnt->retrans))
777                                 return 0;
778                         break;
779                 case Opt_acregmin:
780                         if (match_int(args, &mnt->acregmin))
781                                 return 0;
782                         break;
783                 case Opt_acregmax:
784                         if (match_int(args, &mnt->acregmax))
785                                 return 0;
786                         break;
787                 case Opt_acdirmin:
788                         if (match_int(args, &mnt->acdirmin))
789                                 return 0;
790                         break;
791                 case Opt_acdirmax:
792                         if (match_int(args, &mnt->acdirmax))
793                                 return 0;
794                         break;
795                 case Opt_actimeo:
796                         if (match_int(args, &option))
797                                 return 0;
798                         if (option < 0)
799                                 return 0;
800                         mnt->acregmin =
801                         mnt->acregmax =
802                         mnt->acdirmin =
803                         mnt->acdirmax = option;
804                         break;
805                 case Opt_namelen:
806                         if (match_int(args, &mnt->namlen))
807                                 return 0;
808                         break;
809                 case Opt_mountport:
810                         if (match_int(args, &option))
811                                 return 0;
812                         if (option < 0 || option > 65535)
813                                 return 0;
814                         mnt->mount_server.port = option;
815                         break;
816                 case Opt_mountvers:
817                         if (match_int(args, &option))
818                                 return 0;
819                         if (option < 0)
820                                 return 0;
821                         mnt->mount_server.version = option;
822                         break;
823                 case Opt_nfsvers:
824                         if (match_int(args, &option))
825                                 return 0;
826                         switch (option) {
827                         case 2:
828                                 mnt->flags &= ~NFS_MOUNT_VER3;
829                                 break;
830                         case 3:
831                                 mnt->flags |= NFS_MOUNT_VER3;
832                                 break;
833                         default:
834                                 goto out_unrec_vers;
835                         }
836                         break;
837
838                 case Opt_sec:
839                         string = match_strdup(args);
840                         if (string == NULL)
841                                 goto out_nomem;
842                         token = match_token(string, nfs_secflavor_tokens, args);
843                         kfree(string);
844
845                         /*
846                          * The flags setting is for v2/v3.  The flavor_len
847                          * setting is for v4.  v2/v3 also need to know the
848                          * difference between NULL and UNIX.
849                          */
850                         switch (token) {
851                         case Opt_sec_none:
852                                 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
853                                 mnt->auth_flavor_len = 0;
854                                 mnt->auth_flavors[0] = RPC_AUTH_NULL;
855                                 break;
856                         case Opt_sec_sys:
857                                 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
858                                 mnt->auth_flavor_len = 0;
859                                 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
860                                 break;
861                         case Opt_sec_krb5:
862                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
863                                 mnt->auth_flavor_len = 1;
864                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
865                                 break;
866                         case Opt_sec_krb5i:
867                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
868                                 mnt->auth_flavor_len = 1;
869                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
870                                 break;
871                         case Opt_sec_krb5p:
872                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
873                                 mnt->auth_flavor_len = 1;
874                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
875                                 break;
876                         case Opt_sec_lkey:
877                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
878                                 mnt->auth_flavor_len = 1;
879                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
880                                 break;
881                         case Opt_sec_lkeyi:
882                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
883                                 mnt->auth_flavor_len = 1;
884                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
885                                 break;
886                         case Opt_sec_lkeyp:
887                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
888                                 mnt->auth_flavor_len = 1;
889                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
890                                 break;
891                         case Opt_sec_spkm:
892                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
893                                 mnt->auth_flavor_len = 1;
894                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
895                                 break;
896                         case Opt_sec_spkmi:
897                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
898                                 mnt->auth_flavor_len = 1;
899                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
900                                 break;
901                         case Opt_sec_spkmp:
902                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
903                                 mnt->auth_flavor_len = 1;
904                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
905                                 break;
906                         default:
907                                 goto out_unrec_sec;
908                         }
909                         break;
910                 case Opt_proto:
911                         string = match_strdup(args);
912                         if (string == NULL)
913                                 goto out_nomem;
914                         token = match_token(string,
915                                             nfs_xprt_protocol_tokens, args);
916                         kfree(string);
917
918                         switch (token) {
919                         case Opt_xprt_udp:
920                                 mnt->flags &= ~NFS_MOUNT_TCP;
921                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
922                                 mnt->timeo = 7;
923                                 mnt->retrans = 5;
924                                 break;
925                         case Opt_xprt_tcp:
926                                 mnt->flags |= NFS_MOUNT_TCP;
927                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
928                                 mnt->timeo = 600;
929                                 mnt->retrans = 2;
930                                 break;
931                         case Opt_xprt_rdma:
932                                 /* vector side protocols to TCP */
933                                 mnt->flags |= NFS_MOUNT_TCP;
934                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
935                                 mnt->timeo = 600;
936                                 mnt->retrans = 2;
937                                 break;
938                         default:
939                                 goto out_unrec_xprt;
940                         }
941                         break;
942                 case Opt_mountproto:
943                         string = match_strdup(args);
944                         if (string == NULL)
945                                 goto out_nomem;
946                         token = match_token(string,
947                                             nfs_xprt_protocol_tokens, args);
948                         kfree(string);
949
950                         switch (token) {
951                         case Opt_xprt_udp:
952                                 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
953                                 break;
954                         case Opt_xprt_tcp:
955                                 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
956                                 break;
957                         case Opt_xprt_rdma: /* not used for side protocols */
958                         default:
959                                 goto out_unrec_xprt;
960                         }
961                         break;
962                 case Opt_addr:
963                         string = match_strdup(args);
964                         if (string == NULL)
965                                 goto out_nomem;
966                         mnt->nfs_server.address.sin_family = AF_INET;
967                         mnt->nfs_server.address.sin_addr.s_addr =
968                                                         in_aton(string);
969                         kfree(string);
970                         break;
971                 case Opt_clientaddr:
972                         string = match_strdup(args);
973                         if (string == NULL)
974                                 goto out_nomem;
975                         mnt->client_address = string;
976                         break;
977                 case Opt_mountaddr:
978                         string = match_strdup(args);
979                         if (string == NULL)
980                                 goto out_nomem;
981                         mnt->mount_server.address.sin_family = AF_INET;
982                         mnt->mount_server.address.sin_addr.s_addr =
983                                                         in_aton(string);
984                         kfree(string);
985                         break;
986
987                 case Opt_userspace:
988                 case Opt_deprecated:
989                         break;
990
991                 default:
992                         goto out_unknown;
993                 }
994         }
995
996         nfs_set_port((struct sockaddr *)&mnt->nfs_server.address, port);
997
998         return 1;
999
1000 out_nomem:
1001         printk(KERN_INFO "NFS: not enough memory to parse option\n");
1002         return 0;
1003
1004 out_unrec_vers:
1005         printk(KERN_INFO "NFS: unrecognized NFS version number\n");
1006         return 0;
1007
1008 out_unrec_xprt:
1009         printk(KERN_INFO "NFS: unrecognized transport protocol\n");
1010         return 0;
1011
1012 out_unrec_sec:
1013         printk(KERN_INFO "NFS: unrecognized security flavor\n");
1014         return 0;
1015
1016 out_unknown:
1017         printk(KERN_INFO "NFS: unknown mount option: %s\n", p);
1018         return 0;
1019 }
1020
1021 /*
1022  * Use the remote server's MOUNT service to request the NFS file handle
1023  * corresponding to the provided path.
1024  */
1025 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1026                          struct nfs_fh *root_fh)
1027 {
1028         struct sockaddr_in sin;
1029         int status;
1030
1031         if (args->mount_server.version == 0) {
1032                 if (args->flags & NFS_MOUNT_VER3)
1033                         args->mount_server.version = NFS_MNT3_VERSION;
1034                 else
1035                         args->mount_server.version = NFS_MNT_VERSION;
1036         }
1037
1038         /*
1039          * Construct the mount server's address.
1040          */
1041         if (args->mount_server.address.sin_addr.s_addr != INADDR_ANY)
1042                 sin = args->mount_server.address;
1043         else
1044                 sin = args->nfs_server.address;
1045         /*
1046          * autobind will be used if mount_server.port == 0
1047          */
1048         nfs_set_port((struct sockaddr *)&sin, args->mount_server.port);
1049
1050         /*
1051          * Now ask the mount server to map our export path
1052          * to a file handle.
1053          */
1054         status = nfs_mount((struct sockaddr *) &sin,
1055                            sizeof(sin),
1056                            args->nfs_server.hostname,
1057                            args->nfs_server.export_path,
1058                            args->mount_server.version,
1059                            args->mount_server.protocol,
1060                            root_fh);
1061         if (status == 0)
1062                 return 0;
1063
1064         dfprintk(MOUNT, "NFS: unable to mount server " NIPQUAD_FMT
1065                         ", error %d\n", NIPQUAD(sin.sin_addr.s_addr), status);
1066         return status;
1067 }
1068
1069 /*
1070  * Validate the NFS2/NFS3 mount data
1071  * - fills in the mount root filehandle
1072  *
1073  * For option strings, user space handles the following behaviors:
1074  *
1075  * + DNS: mapping server host name to IP address ("addr=" option)
1076  *
1077  * + failure mode: how to behave if a mount request can't be handled
1078  *   immediately ("fg/bg" option)
1079  *
1080  * + retry: how often to retry a mount request ("retry=" option)
1081  *
1082  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1083  *   mountproto=tcp after mountproto=udp, and so on
1084  */
1085 static int nfs_validate_mount_data(void *options,
1086                                    struct nfs_parsed_mount_data *args,
1087                                    struct nfs_fh *mntfh,
1088                                    const char *dev_name)
1089 {
1090         struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1091
1092         memset(args, 0, sizeof(*args));
1093
1094         if (data == NULL)
1095                 goto out_no_data;
1096
1097         args->flags             = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1098         args->rsize             = NFS_MAX_FILE_IO_SIZE;
1099         args->wsize             = NFS_MAX_FILE_IO_SIZE;
1100         args->timeo             = 600;
1101         args->retrans           = 2;
1102         args->acregmin          = 3;
1103         args->acregmax          = 60;
1104         args->acdirmin          = 30;
1105         args->acdirmax          = 60;
1106         args->mount_server.protocol = XPRT_TRANSPORT_UDP;
1107         args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1108
1109         switch (data->version) {
1110         case 1:
1111                 data->namlen = 0;
1112         case 2:
1113                 data->bsize = 0;
1114         case 3:
1115                 if (data->flags & NFS_MOUNT_VER3)
1116                         goto out_no_v3;
1117                 data->root.size = NFS2_FHSIZE;
1118                 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1119         case 4:
1120                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1121                         goto out_no_sec;
1122         case 5:
1123                 memset(data->context, 0, sizeof(data->context));
1124         case 6:
1125                 if (data->flags & NFS_MOUNT_VER3)
1126                         mntfh->size = data->root.size;
1127                 else
1128                         mntfh->size = NFS2_FHSIZE;
1129
1130                 if (mntfh->size > sizeof(mntfh->data))
1131                         goto out_invalid_fh;
1132
1133                 memcpy(mntfh->data, data->root.data, mntfh->size);
1134                 if (mntfh->size < sizeof(mntfh->data))
1135                         memset(mntfh->data + mntfh->size, 0,
1136                                sizeof(mntfh->data) - mntfh->size);
1137
1138                 if (!nfs_verify_server_address((struct sockaddr *) &data->addr))
1139                         goto out_no_address;
1140
1141                 /*
1142                  * Translate to nfs_parsed_mount_data, which nfs_fill_super
1143                  * can deal with.
1144                  */
1145                 args->flags             = data->flags;
1146                 args->rsize             = data->rsize;
1147                 args->wsize             = data->wsize;
1148                 args->flags             = data->flags;
1149                 args->timeo             = data->timeo;
1150                 args->retrans           = data->retrans;
1151                 args->acregmin          = data->acregmin;
1152                 args->acregmax          = data->acregmax;
1153                 args->acdirmin          = data->acdirmin;
1154                 args->acdirmax          = data->acdirmax;
1155                 args->nfs_server.address = data->addr;
1156                 if (!(data->flags & NFS_MOUNT_TCP))
1157                         args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1158                 /* N.B. caller will free nfs_server.hostname in all cases */
1159                 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1160                 args->namlen            = data->namlen;
1161                 args->bsize             = data->bsize;
1162                 args->auth_flavors[0]   = data->pseudoflavor;
1163                 break;
1164         default: {
1165                 unsigned int len;
1166                 char *c;
1167                 int status;
1168
1169                 if (nfs_parse_mount_options((char *)options, args) == 0)
1170                         return -EINVAL;
1171
1172                 if (!nfs_verify_server_address((struct sockaddr *)
1173                                                 &args->nfs_server.address))
1174                         goto out_no_address;
1175
1176                 c = strchr(dev_name, ':');
1177                 if (c == NULL)
1178                         return -EINVAL;
1179                 len = c - dev_name;
1180                 /* N.B. caller will free nfs_server.hostname in all cases */
1181                 args->nfs_server.hostname = kstrndup(dev_name, len, GFP_KERNEL);
1182
1183                 c++;
1184                 if (strlen(c) > NFS_MAXPATHLEN)
1185                         return -ENAMETOOLONG;
1186                 args->nfs_server.export_path = c;
1187
1188                 status = nfs_try_mount(args, mntfh);
1189                 if (status)
1190                         return status;
1191
1192                 break;
1193                 }
1194         }
1195
1196         if (!(args->flags & NFS_MOUNT_SECFLAVOUR))
1197                 args->auth_flavors[0] = RPC_AUTH_UNIX;
1198
1199 #ifndef CONFIG_NFS_V3
1200         if (args->flags & NFS_MOUNT_VER3)
1201                 goto out_v3_not_compiled;
1202 #endif /* !CONFIG_NFS_V3 */
1203
1204         return 0;
1205
1206 out_no_data:
1207         dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1208         return -EINVAL;
1209
1210 out_no_v3:
1211         dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1212                  data->version);
1213         return -EINVAL;
1214
1215 out_no_sec:
1216         dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1217         return -EINVAL;
1218
1219 #ifndef CONFIG_NFS_V3
1220 out_v3_not_compiled:
1221         dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1222         return -EPROTONOSUPPORT;
1223 #endif /* !CONFIG_NFS_V3 */
1224
1225 out_no_address:
1226         dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1227         return -EINVAL;
1228
1229 out_invalid_fh:
1230         dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1231         return -EINVAL;
1232 }
1233
1234 /*
1235  * Initialise the common bits of the superblock
1236  */
1237 static inline void nfs_initialise_sb(struct super_block *sb)
1238 {
1239         struct nfs_server *server = NFS_SB(sb);
1240
1241         sb->s_magic = NFS_SUPER_MAGIC;
1242
1243         /* We probably want something more informative here */
1244         snprintf(sb->s_id, sizeof(sb->s_id),
1245                  "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1246
1247         if (sb->s_blocksize == 0)
1248                 sb->s_blocksize = nfs_block_bits(server->wsize,
1249                                                  &sb->s_blocksize_bits);
1250
1251         if (server->flags & NFS_MOUNT_NOAC)
1252                 sb->s_flags |= MS_SYNCHRONOUS;
1253
1254         nfs_super_set_maxbytes(sb, server->maxfilesize);
1255 }
1256
1257 /*
1258  * Finish setting up an NFS2/3 superblock
1259  */
1260 static void nfs_fill_super(struct super_block *sb,
1261                            struct nfs_parsed_mount_data *data)
1262 {
1263         struct nfs_server *server = NFS_SB(sb);
1264
1265         sb->s_blocksize_bits = 0;
1266         sb->s_blocksize = 0;
1267         if (data->bsize)
1268                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1269
1270         if (server->flags & NFS_MOUNT_VER3) {
1271                 /* The VFS shouldn't apply the umask to mode bits. We will do
1272                  * so ourselves when necessary.
1273                  */
1274                 sb->s_flags |= MS_POSIXACL;
1275                 sb->s_time_gran = 1;
1276         }
1277
1278         sb->s_op = &nfs_sops;
1279         nfs_initialise_sb(sb);
1280 }
1281
1282 /*
1283  * Finish setting up a cloned NFS2/3 superblock
1284  */
1285 static void nfs_clone_super(struct super_block *sb,
1286                             const struct super_block *old_sb)
1287 {
1288         struct nfs_server *server = NFS_SB(sb);
1289
1290         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1291         sb->s_blocksize = old_sb->s_blocksize;
1292         sb->s_maxbytes = old_sb->s_maxbytes;
1293
1294         if (server->flags & NFS_MOUNT_VER3) {
1295                 /* The VFS shouldn't apply the umask to mode bits. We will do
1296                  * so ourselves when necessary.
1297                  */
1298                 sb->s_flags |= MS_POSIXACL;
1299                 sb->s_time_gran = 1;
1300         }
1301
1302         sb->s_op = old_sb->s_op;
1303         nfs_initialise_sb(sb);
1304 }
1305
1306 #define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1307
1308 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1309 {
1310         const struct nfs_server *a = s->s_fs_info;
1311         const struct rpc_clnt *clnt_a = a->client;
1312         const struct rpc_clnt *clnt_b = b->client;
1313
1314         if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1315                 goto Ebusy;
1316         if (a->nfs_client != b->nfs_client)
1317                 goto Ebusy;
1318         if (a->flags != b->flags)
1319                 goto Ebusy;
1320         if (a->wsize != b->wsize)
1321                 goto Ebusy;
1322         if (a->rsize != b->rsize)
1323                 goto Ebusy;
1324         if (a->acregmin != b->acregmin)
1325                 goto Ebusy;
1326         if (a->acregmax != b->acregmax)
1327                 goto Ebusy;
1328         if (a->acdirmin != b->acdirmin)
1329                 goto Ebusy;
1330         if (a->acdirmax != b->acdirmax)
1331                 goto Ebusy;
1332         if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1333                 goto Ebusy;
1334         return 1;
1335 Ebusy:
1336         return 0;
1337 }
1338
1339 struct nfs_sb_mountdata {
1340         struct nfs_server *server;
1341         int mntflags;
1342 };
1343
1344 static int nfs_set_super(struct super_block *s, void *data)
1345 {
1346         struct nfs_sb_mountdata *sb_mntdata = data;
1347         struct nfs_server *server = sb_mntdata->server;
1348         int ret;
1349
1350         s->s_flags = sb_mntdata->mntflags;
1351         s->s_fs_info = server;
1352         ret = set_anon_super(s, server);
1353         if (ret == 0)
1354                 server->s_dev = s->s_dev;
1355         return ret;
1356 }
1357
1358 static int nfs_compare_super_address(struct nfs_server *server1,
1359                                      struct nfs_server *server2)
1360 {
1361         struct sockaddr *sap1, *sap2;
1362
1363         sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1364         sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1365
1366         if (sap1->sa_family != sap2->sa_family)
1367                 return 0;
1368
1369         switch (sap1->sa_family) {
1370         case AF_INET: {
1371                 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1372                 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1373                 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1374                         return 0;
1375                 if (sin1->sin_port != sin2->sin_port)
1376                         return 0;
1377                 break;
1378         }
1379         case AF_INET6: {
1380                 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1381                 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1382                 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1383                         return 0;
1384                 if (sin1->sin6_port != sin2->sin6_port)
1385                         return 0;
1386                 break;
1387         }
1388         default:
1389                 return 0;
1390         }
1391
1392         return 1;
1393 }
1394
1395 static int nfs_compare_super(struct super_block *sb, void *data)
1396 {
1397         struct nfs_sb_mountdata *sb_mntdata = data;
1398         struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1399         int mntflags = sb_mntdata->mntflags;
1400
1401         if (!nfs_compare_super_address(old, server))
1402                 return 0;
1403         /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1404         if (old->flags & NFS_MOUNT_UNSHARED)
1405                 return 0;
1406         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1407                 return 0;
1408         return nfs_compare_mount_options(sb, server, mntflags);
1409 }
1410
1411 static int nfs_get_sb(struct file_system_type *fs_type,
1412         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1413 {
1414         struct nfs_server *server = NULL;
1415         struct super_block *s;
1416         struct nfs_fh mntfh;
1417         struct nfs_parsed_mount_data data;
1418         struct dentry *mntroot;
1419         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1420         struct nfs_sb_mountdata sb_mntdata = {
1421                 .mntflags = flags,
1422         };
1423         int error;
1424
1425         /* Validate the mount data */
1426         error = nfs_validate_mount_data(raw_data, &data, &mntfh, dev_name);
1427         if (error < 0)
1428                 goto out;
1429
1430         /* Get a volume representation */
1431         server = nfs_create_server(&data, &mntfh);
1432         if (IS_ERR(server)) {
1433                 error = PTR_ERR(server);
1434                 goto out;
1435         }
1436         sb_mntdata.server = server;
1437
1438         if (server->flags & NFS_MOUNT_UNSHARED)
1439                 compare_super = NULL;
1440
1441         /* Get a superblock - note that we may end up sharing one that already exists */
1442         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
1443         if (IS_ERR(s)) {
1444                 error = PTR_ERR(s);
1445                 goto out_err_nosb;
1446         }
1447
1448         if (s->s_fs_info != server) {
1449                 nfs_free_server(server);
1450                 server = NULL;
1451         }
1452
1453         if (!s->s_root) {
1454                 /* initial superblock/root creation */
1455                 nfs_fill_super(s, &data);
1456         }
1457
1458         mntroot = nfs_get_root(s, &mntfh);
1459         if (IS_ERR(mntroot)) {
1460                 error = PTR_ERR(mntroot);
1461                 goto error_splat_super;
1462         }
1463
1464         s->s_flags |= MS_ACTIVE;
1465         mnt->mnt_sb = s;
1466         mnt->mnt_root = mntroot;
1467         error = 0;
1468
1469 out:
1470         kfree(data.nfs_server.hostname);
1471         return error;
1472
1473 out_err_nosb:
1474         nfs_free_server(server);
1475         goto out;
1476
1477 error_splat_super:
1478         up_write(&s->s_umount);
1479         deactivate_super(s);
1480         goto out;
1481 }
1482
1483 /*
1484  * Destroy an NFS2/3 superblock
1485  */
1486 static void nfs_kill_super(struct super_block *s)
1487 {
1488         struct nfs_server *server = NFS_SB(s);
1489
1490         kill_anon_super(s);
1491         nfs_free_server(server);
1492 }
1493
1494 /*
1495  * Clone an NFS2/3 server record on xdev traversal (FSID-change)
1496  */
1497 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
1498                            const char *dev_name, void *raw_data,
1499                            struct vfsmount *mnt)
1500 {
1501         struct nfs_clone_mount *data = raw_data;
1502         struct super_block *s;
1503         struct nfs_server *server;
1504         struct dentry *mntroot;
1505         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1506         struct nfs_sb_mountdata sb_mntdata = {
1507                 .mntflags = flags,
1508         };
1509         int error;
1510
1511         dprintk("--> nfs_xdev_get_sb()\n");
1512
1513         /* create a new volume representation */
1514         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
1515         if (IS_ERR(server)) {
1516                 error = PTR_ERR(server);
1517                 goto out_err_noserver;
1518         }
1519         sb_mntdata.server = server;
1520
1521         if (server->flags & NFS_MOUNT_UNSHARED)
1522                 compare_super = NULL;
1523
1524         /* Get a superblock - note that we may end up sharing one that already exists */
1525         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
1526         if (IS_ERR(s)) {
1527                 error = PTR_ERR(s);
1528                 goto out_err_nosb;
1529         }
1530
1531         if (s->s_fs_info != server) {
1532                 nfs_free_server(server);
1533                 server = NULL;
1534         }
1535
1536         if (!s->s_root) {
1537                 /* initial superblock/root creation */
1538                 nfs_clone_super(s, data->sb);
1539         }
1540
1541         mntroot = nfs_get_root(s, data->fh);
1542         if (IS_ERR(mntroot)) {
1543                 error = PTR_ERR(mntroot);
1544                 goto error_splat_super;
1545         }
1546         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
1547                 dput(mntroot);
1548                 error = -ESTALE;
1549                 goto error_splat_super;
1550         }
1551
1552         s->s_flags |= MS_ACTIVE;
1553         mnt->mnt_sb = s;
1554         mnt->mnt_root = mntroot;
1555
1556         dprintk("<-- nfs_xdev_get_sb() = 0\n");
1557         return 0;
1558
1559 out_err_nosb:
1560         nfs_free_server(server);
1561 out_err_noserver:
1562         dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
1563         return error;
1564
1565 error_splat_super:
1566         up_write(&s->s_umount);
1567         deactivate_super(s);
1568         dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
1569         return error;
1570 }
1571
1572 #ifdef CONFIG_NFS_V4
1573
1574 /*
1575  * Finish setting up a cloned NFS4 superblock
1576  */
1577 static void nfs4_clone_super(struct super_block *sb,
1578                             const struct super_block *old_sb)
1579 {
1580         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1581         sb->s_blocksize = old_sb->s_blocksize;
1582         sb->s_maxbytes = old_sb->s_maxbytes;
1583         sb->s_time_gran = 1;
1584         sb->s_op = old_sb->s_op;
1585         nfs_initialise_sb(sb);
1586 }
1587
1588 /*
1589  * Set up an NFS4 superblock
1590  */
1591 static void nfs4_fill_super(struct super_block *sb)
1592 {
1593         sb->s_time_gran = 1;
1594         sb->s_op = &nfs4_sops;
1595         nfs_initialise_sb(sb);
1596 }
1597
1598 /*
1599  * If the user didn't specify a port, set the port number to
1600  * the NFS version 4 default port.
1601  */
1602 static void nfs4_default_port(struct sockaddr *sap)
1603 {
1604         switch (sap->sa_family) {
1605         case AF_INET: {
1606                 struct sockaddr_in *ap = (struct sockaddr_in *)sap;
1607                 if (ap->sin_port == 0)
1608                         ap->sin_port = htons(NFS_PORT);
1609                 break;
1610         }
1611         case AF_INET6: {
1612                 struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
1613                 if (ap->sin6_port == 0)
1614                         ap->sin6_port = htons(NFS_PORT);
1615                 break;
1616         }
1617         }
1618 }
1619
1620 /*
1621  * Validate NFSv4 mount options
1622  */
1623 static int nfs4_validate_mount_data(void *options,
1624                                     struct nfs_parsed_mount_data *args,
1625                                     const char *dev_name)
1626 {
1627         struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
1628         char *c;
1629
1630         memset(args, 0, sizeof(*args));
1631
1632         if (data == NULL)
1633                 goto out_no_data;
1634
1635         args->rsize             = NFS_MAX_FILE_IO_SIZE;
1636         args->wsize             = NFS_MAX_FILE_IO_SIZE;
1637         args->timeo             = 600;
1638         args->retrans           = 2;
1639         args->acregmin          = 3;
1640         args->acregmax          = 60;
1641         args->acdirmin          = 30;
1642         args->acdirmax          = 60;
1643         args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1644
1645         switch (data->version) {
1646         case 1:
1647                 if (data->host_addrlen != sizeof(args->nfs_server.address))
1648                         goto out_no_address;
1649                 if (copy_from_user(&args->nfs_server.address,
1650                                    data->host_addr,
1651                                    sizeof(args->nfs_server.address)))
1652                         return -EFAULT;
1653                 if (!nfs_verify_server_address((struct sockaddr *)
1654                                                 &args->nfs_server.address))
1655                         goto out_no_address;
1656
1657                 nfs4_default_port((struct sockaddr *)
1658                                   &args->nfs_server.address);
1659
1660                 switch (data->auth_flavourlen) {
1661                 case 0:
1662                         args->auth_flavors[0] = RPC_AUTH_UNIX;
1663                         break;
1664                 case 1:
1665                         if (copy_from_user(&args->auth_flavors[0],
1666                                            data->auth_flavours,
1667                                            sizeof(args->auth_flavors[0])))
1668                                 return -EFAULT;
1669                         break;
1670                 default:
1671                         goto out_inval_auth;
1672                 }
1673
1674                 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1675                 if (IS_ERR(c))
1676                         return PTR_ERR(c);
1677                 args->nfs_server.hostname = c;
1678
1679                 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1680                 if (IS_ERR(c))
1681                         return PTR_ERR(c);
1682                 args->nfs_server.export_path = c;
1683                 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
1684
1685                 c = strndup_user(data->client_addr.data, 16);
1686                 if (IS_ERR(c))
1687                         return PTR_ERR(c);
1688                 args->client_address = c;
1689
1690                 /*
1691                  * Translate to nfs_parsed_mount_data, which nfs4_fill_super
1692                  * can deal with.
1693                  */
1694
1695                 args->flags     = data->flags & NFS4_MOUNT_FLAGMASK;
1696                 args->rsize     = data->rsize;
1697                 args->wsize     = data->wsize;
1698                 args->timeo     = data->timeo;
1699                 args->retrans   = data->retrans;
1700                 args->acregmin  = data->acregmin;
1701                 args->acregmax  = data->acregmax;
1702                 args->acdirmin  = data->acdirmin;
1703                 args->acdirmax  = data->acdirmax;
1704                 args->nfs_server.protocol = data->proto;
1705
1706                 break;
1707         default: {
1708                 unsigned int len;
1709
1710                 if (nfs_parse_mount_options((char *)options, args) == 0)
1711                         return -EINVAL;
1712
1713                 if (!nfs_verify_server_address((struct sockaddr *)
1714                                                 &args->nfs_server.address))
1715                         return -EINVAL;
1716
1717                 nfs4_default_port((struct sockaddr *)
1718                                   &args->nfs_server.address);
1719
1720                 switch (args->auth_flavor_len) {
1721                 case 0:
1722                         args->auth_flavors[0] = RPC_AUTH_UNIX;
1723                         break;
1724                 case 1:
1725                         break;
1726                 default:
1727                         goto out_inval_auth;
1728                 }
1729
1730                 /*
1731                  * Split "dev_name" into "hostname:mntpath".
1732                  */
1733                 c = strchr(dev_name, ':');
1734                 if (c == NULL)
1735                         return -EINVAL;
1736                 /* while calculating len, pretend ':' is '\0' */
1737                 len = c - dev_name;
1738                 if (len > NFS4_MAXNAMLEN)
1739                         return -ENAMETOOLONG;
1740                 /* N.B. caller will free nfs_server.hostname in all cases */
1741                 args->nfs_server.hostname = kstrndup(dev_name, len, GFP_KERNEL);
1742
1743                 c++;                    /* step over the ':' */
1744                 len = strlen(c);
1745                 if (len > NFS4_MAXPATHLEN)
1746                         return -ENAMETOOLONG;
1747                 args->nfs_server.export_path = kstrndup(c, len, GFP_KERNEL);
1748
1749                 dprintk("NFS: MNTPATH: '%s'\n", args->nfs_server.export_path);
1750
1751                 if (args->client_address == NULL)
1752                         goto out_no_client_address;
1753
1754                 break;
1755                 }
1756         }
1757
1758         return 0;
1759
1760 out_no_data:
1761         dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
1762         return -EINVAL;
1763
1764 out_inval_auth:
1765         dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
1766                  data->auth_flavourlen);
1767         return -EINVAL;
1768
1769 out_no_address:
1770         dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
1771         return -EINVAL;
1772
1773 out_no_client_address:
1774         dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
1775         return -EINVAL;
1776 }
1777
1778 /*
1779  * Get the superblock for an NFS4 mountpoint
1780  */
1781 static int nfs4_get_sb(struct file_system_type *fs_type,
1782         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1783 {
1784         struct nfs_parsed_mount_data data;
1785         struct super_block *s;
1786         struct nfs_server *server;
1787         struct nfs_fh mntfh;
1788         struct dentry *mntroot;
1789         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1790         struct nfs_sb_mountdata sb_mntdata = {
1791                 .mntflags = flags,
1792         };
1793         int error;
1794
1795         /* Validate the mount data */
1796         error = nfs4_validate_mount_data(raw_data, &data, dev_name);
1797         if (error < 0)
1798                 goto out;
1799
1800         /* Get a volume representation */
1801         server = nfs4_create_server(&data, &mntfh);
1802         if (IS_ERR(server)) {
1803                 error = PTR_ERR(server);
1804                 goto out;
1805         }
1806         sb_mntdata.server = server;
1807
1808         if (server->flags & NFS4_MOUNT_UNSHARED)
1809                 compare_super = NULL;
1810
1811         /* Get a superblock - note that we may end up sharing one that already exists */
1812         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
1813         if (IS_ERR(s)) {
1814                 error = PTR_ERR(s);
1815                 goto out_free;
1816         }
1817
1818         if (s->s_fs_info != server) {
1819                 nfs_free_server(server);
1820                 server = NULL;
1821         }
1822
1823         if (!s->s_root) {
1824                 /* initial superblock/root creation */
1825                 nfs4_fill_super(s);
1826         }
1827
1828         mntroot = nfs4_get_root(s, &mntfh);
1829         if (IS_ERR(mntroot)) {
1830                 error = PTR_ERR(mntroot);
1831                 goto error_splat_super;
1832         }
1833
1834         s->s_flags |= MS_ACTIVE;
1835         mnt->mnt_sb = s;
1836         mnt->mnt_root = mntroot;
1837         error = 0;
1838
1839 out:
1840         kfree(data.client_address);
1841         kfree(data.nfs_server.export_path);
1842         kfree(data.nfs_server.hostname);
1843         return error;
1844
1845 out_free:
1846         nfs_free_server(server);
1847         goto out;
1848
1849 error_splat_super:
1850         up_write(&s->s_umount);
1851         deactivate_super(s);
1852         goto out;
1853 }
1854
1855 static void nfs4_kill_super(struct super_block *sb)
1856 {
1857         struct nfs_server *server = NFS_SB(sb);
1858
1859         nfs_return_all_delegations(sb);
1860         kill_anon_super(sb);
1861
1862         nfs4_renewd_prepare_shutdown(server);
1863         nfs_free_server(server);
1864 }
1865
1866 /*
1867  * Clone an NFS4 server record on xdev traversal (FSID-change)
1868  */
1869 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
1870                             const char *dev_name, void *raw_data,
1871                             struct vfsmount *mnt)
1872 {
1873         struct nfs_clone_mount *data = raw_data;
1874         struct super_block *s;
1875         struct nfs_server *server;
1876         struct dentry *mntroot;
1877         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1878         struct nfs_sb_mountdata sb_mntdata = {
1879                 .mntflags = flags,
1880         };
1881         int error;
1882
1883         dprintk("--> nfs4_xdev_get_sb()\n");
1884
1885         /* create a new volume representation */
1886         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
1887         if (IS_ERR(server)) {
1888                 error = PTR_ERR(server);
1889                 goto out_err_noserver;
1890         }
1891         sb_mntdata.server = server;
1892
1893         if (server->flags & NFS4_MOUNT_UNSHARED)
1894                 compare_super = NULL;
1895
1896         /* Get a superblock - note that we may end up sharing one that already exists */
1897         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
1898         if (IS_ERR(s)) {
1899                 error = PTR_ERR(s);
1900                 goto out_err_nosb;
1901         }
1902
1903         if (s->s_fs_info != server) {
1904                 nfs_free_server(server);
1905                 server = NULL;
1906         }
1907
1908         if (!s->s_root) {
1909                 /* initial superblock/root creation */
1910                 nfs4_clone_super(s, data->sb);
1911         }
1912
1913         mntroot = nfs4_get_root(s, data->fh);
1914         if (IS_ERR(mntroot)) {
1915                 error = PTR_ERR(mntroot);
1916                 goto error_splat_super;
1917         }
1918         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
1919                 dput(mntroot);
1920                 error = -ESTALE;
1921                 goto error_splat_super;
1922         }
1923
1924         s->s_flags |= MS_ACTIVE;
1925         mnt->mnt_sb = s;
1926         mnt->mnt_root = mntroot;
1927
1928         dprintk("<-- nfs4_xdev_get_sb() = 0\n");
1929         return 0;
1930
1931 out_err_nosb:
1932         nfs_free_server(server);
1933 out_err_noserver:
1934         dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
1935         return error;
1936
1937 error_splat_super:
1938         up_write(&s->s_umount);
1939         deactivate_super(s);
1940         dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
1941         return error;
1942 }
1943
1944 /*
1945  * Create an NFS4 server record on referral traversal
1946  */
1947 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
1948                                 const char *dev_name, void *raw_data,
1949                                 struct vfsmount *mnt)
1950 {
1951         struct nfs_clone_mount *data = raw_data;
1952         struct super_block *s;
1953         struct nfs_server *server;
1954         struct dentry *mntroot;
1955         struct nfs_fh mntfh;
1956         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1957         struct nfs_sb_mountdata sb_mntdata = {
1958                 .mntflags = flags,
1959         };
1960         int error;
1961
1962         dprintk("--> nfs4_referral_get_sb()\n");
1963
1964         /* create a new volume representation */
1965         server = nfs4_create_referral_server(data, &mntfh);
1966         if (IS_ERR(server)) {
1967                 error = PTR_ERR(server);
1968                 goto out_err_noserver;
1969         }
1970         sb_mntdata.server = server;
1971
1972         if (server->flags & NFS4_MOUNT_UNSHARED)
1973                 compare_super = NULL;
1974
1975         /* Get a superblock - note that we may end up sharing one that already exists */
1976         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
1977         if (IS_ERR(s)) {
1978                 error = PTR_ERR(s);
1979                 goto out_err_nosb;
1980         }
1981
1982         if (s->s_fs_info != server) {
1983                 nfs_free_server(server);
1984                 server = NULL;
1985         }
1986
1987         if (!s->s_root) {
1988                 /* initial superblock/root creation */
1989                 nfs4_fill_super(s);
1990         }
1991
1992         mntroot = nfs4_get_root(s, &mntfh);
1993         if (IS_ERR(mntroot)) {
1994                 error = PTR_ERR(mntroot);
1995                 goto error_splat_super;
1996         }
1997         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
1998                 dput(mntroot);
1999                 error = -ESTALE;
2000                 goto error_splat_super;
2001         }
2002
2003         s->s_flags |= MS_ACTIVE;
2004         mnt->mnt_sb = s;
2005         mnt->mnt_root = mntroot;
2006
2007         dprintk("<-- nfs4_referral_get_sb() = 0\n");
2008         return 0;
2009
2010 out_err_nosb:
2011         nfs_free_server(server);
2012 out_err_noserver:
2013         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2014         return error;
2015
2016 error_splat_super:
2017         up_write(&s->s_umount);
2018         deactivate_super(s);
2019         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2020         return error;
2021 }
2022
2023 #endif /* CONFIG_NFS_V4 */