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