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