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