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