nfs: switch NFS from ->get_sb() to ->mount()
[platform/kernel/kernel-mfld-blackbay.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 /*
1012  * Error-check and convert a string of mount options from user space into
1013  * a data structure.  The whole mount string is processed; bad options are
1014  * skipped as they are encountered.  If there were no errors, return 1;
1015  * otherwise return 0 (zero).
1016  */
1017 static int nfs_parse_mount_options(char *raw,
1018                                    struct nfs_parsed_mount_data *mnt)
1019 {
1020         char *p, *string, *secdata;
1021         int rc, sloppy = 0, invalid_option = 0;
1022         unsigned short protofamily = AF_UNSPEC;
1023         unsigned short mountfamily = AF_UNSPEC;
1024
1025         if (!raw) {
1026                 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
1027                 return 1;
1028         }
1029         dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
1030
1031         secdata = alloc_secdata();
1032         if (!secdata)
1033                 goto out_nomem;
1034
1035         rc = security_sb_copy_data(raw, secdata);
1036         if (rc)
1037                 goto out_security_failure;
1038
1039         rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
1040         if (rc)
1041                 goto out_security_failure;
1042
1043         free_secdata(secdata);
1044
1045         while ((p = strsep(&raw, ",")) != NULL) {
1046                 substring_t args[MAX_OPT_ARGS];
1047                 unsigned long option;
1048                 int token;
1049
1050                 if (!*p)
1051                         continue;
1052
1053                 dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", p);
1054
1055                 token = match_token(p, nfs_mount_option_tokens, args);
1056                 switch (token) {
1057
1058                 /*
1059                  * boolean options:  foo/nofoo
1060                  */
1061                 case Opt_soft:
1062                         mnt->flags |= NFS_MOUNT_SOFT;
1063                         break;
1064                 case Opt_hard:
1065                         mnt->flags &= ~NFS_MOUNT_SOFT;
1066                         break;
1067                 case Opt_posix:
1068                         mnt->flags |= NFS_MOUNT_POSIX;
1069                         break;
1070                 case Opt_noposix:
1071                         mnt->flags &= ~NFS_MOUNT_POSIX;
1072                         break;
1073                 case Opt_cto:
1074                         mnt->flags &= ~NFS_MOUNT_NOCTO;
1075                         break;
1076                 case Opt_nocto:
1077                         mnt->flags |= NFS_MOUNT_NOCTO;
1078                         break;
1079                 case Opt_ac:
1080                         mnt->flags &= ~NFS_MOUNT_NOAC;
1081                         break;
1082                 case Opt_noac:
1083                         mnt->flags |= NFS_MOUNT_NOAC;
1084                         break;
1085                 case Opt_lock:
1086                         mnt->flags &= ~NFS_MOUNT_NONLM;
1087                         mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1088                                         NFS_MOUNT_LOCAL_FCNTL);
1089                         break;
1090                 case Opt_nolock:
1091                         mnt->flags |= NFS_MOUNT_NONLM;
1092                         mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1093                                        NFS_MOUNT_LOCAL_FCNTL);
1094                         break;
1095                 case Opt_v2:
1096                         mnt->flags &= ~NFS_MOUNT_VER3;
1097                         mnt->version = 2;
1098                         break;
1099                 case Opt_v3:
1100                         mnt->flags |= NFS_MOUNT_VER3;
1101                         mnt->version = 3;
1102                         break;
1103                 case Opt_v4:
1104                         mnt->flags &= ~NFS_MOUNT_VER3;
1105                         mnt->version = 4;
1106                         break;
1107                 case Opt_udp:
1108                         mnt->flags &= ~NFS_MOUNT_TCP;
1109                         mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1110                         break;
1111                 case Opt_tcp:
1112                         mnt->flags |= NFS_MOUNT_TCP;
1113                         mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1114                         break;
1115                 case Opt_rdma:
1116                         mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1117                         mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1118                         xprt_load_transport(p);
1119                         break;
1120                 case Opt_acl:
1121                         mnt->flags &= ~NFS_MOUNT_NOACL;
1122                         break;
1123                 case Opt_noacl:
1124                         mnt->flags |= NFS_MOUNT_NOACL;
1125                         break;
1126                 case Opt_rdirplus:
1127                         mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1128                         break;
1129                 case Opt_nordirplus:
1130                         mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1131                         break;
1132                 case Opt_sharecache:
1133                         mnt->flags &= ~NFS_MOUNT_UNSHARED;
1134                         break;
1135                 case Opt_nosharecache:
1136                         mnt->flags |= NFS_MOUNT_UNSHARED;
1137                         break;
1138                 case Opt_resvport:
1139                         mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1140                         break;
1141                 case Opt_noresvport:
1142                         mnt->flags |= NFS_MOUNT_NORESVPORT;
1143                         break;
1144                 case Opt_fscache:
1145                         mnt->options |= NFS_OPTION_FSCACHE;
1146                         kfree(mnt->fscache_uniq);
1147                         mnt->fscache_uniq = NULL;
1148                         break;
1149                 case Opt_nofscache:
1150                         mnt->options &= ~NFS_OPTION_FSCACHE;
1151                         kfree(mnt->fscache_uniq);
1152                         mnt->fscache_uniq = NULL;
1153                         break;
1154
1155                 /*
1156                  * options that take numeric values
1157                  */
1158                 case Opt_port:
1159                         string = match_strdup(args);
1160                         if (string == NULL)
1161                                 goto out_nomem;
1162                         rc = strict_strtoul(string, 10, &option);
1163                         kfree(string);
1164                         if (rc != 0 || option > USHRT_MAX)
1165                                 goto out_invalid_value;
1166                         mnt->nfs_server.port = option;
1167                         break;
1168                 case Opt_rsize:
1169                         string = match_strdup(args);
1170                         if (string == NULL)
1171                                 goto out_nomem;
1172                         rc = strict_strtoul(string, 10, &option);
1173                         kfree(string);
1174                         if (rc != 0)
1175                                 goto out_invalid_value;
1176                         mnt->rsize = option;
1177                         break;
1178                 case Opt_wsize:
1179                         string = match_strdup(args);
1180                         if (string == NULL)
1181                                 goto out_nomem;
1182                         rc = strict_strtoul(string, 10, &option);
1183                         kfree(string);
1184                         if (rc != 0)
1185                                 goto out_invalid_value;
1186                         mnt->wsize = option;
1187                         break;
1188                 case Opt_bsize:
1189                         string = match_strdup(args);
1190                         if (string == NULL)
1191                                 goto out_nomem;
1192                         rc = strict_strtoul(string, 10, &option);
1193                         kfree(string);
1194                         if (rc != 0)
1195                                 goto out_invalid_value;
1196                         mnt->bsize = option;
1197                         break;
1198                 case Opt_timeo:
1199                         string = match_strdup(args);
1200                         if (string == NULL)
1201                                 goto out_nomem;
1202                         rc = strict_strtoul(string, 10, &option);
1203                         kfree(string);
1204                         if (rc != 0 || option == 0)
1205                                 goto out_invalid_value;
1206                         mnt->timeo = option;
1207                         break;
1208                 case Opt_retrans:
1209                         string = match_strdup(args);
1210                         if (string == NULL)
1211                                 goto out_nomem;
1212                         rc = strict_strtoul(string, 10, &option);
1213                         kfree(string);
1214                         if (rc != 0 || option == 0)
1215                                 goto out_invalid_value;
1216                         mnt->retrans = option;
1217                         break;
1218                 case Opt_acregmin:
1219                         string = match_strdup(args);
1220                         if (string == NULL)
1221                                 goto out_nomem;
1222                         rc = strict_strtoul(string, 10, &option);
1223                         kfree(string);
1224                         if (rc != 0)
1225                                 goto out_invalid_value;
1226                         mnt->acregmin = option;
1227                         break;
1228                 case Opt_acregmax:
1229                         string = match_strdup(args);
1230                         if (string == NULL)
1231                                 goto out_nomem;
1232                         rc = strict_strtoul(string, 10, &option);
1233                         kfree(string);
1234                         if (rc != 0)
1235                                 goto out_invalid_value;
1236                         mnt->acregmax = option;
1237                         break;
1238                 case Opt_acdirmin:
1239                         string = match_strdup(args);
1240                         if (string == NULL)
1241                                 goto out_nomem;
1242                         rc = strict_strtoul(string, 10, &option);
1243                         kfree(string);
1244                         if (rc != 0)
1245                                 goto out_invalid_value;
1246                         mnt->acdirmin = option;
1247                         break;
1248                 case Opt_acdirmax:
1249                         string = match_strdup(args);
1250                         if (string == NULL)
1251                                 goto out_nomem;
1252                         rc = strict_strtoul(string, 10, &option);
1253                         kfree(string);
1254                         if (rc != 0)
1255                                 goto out_invalid_value;
1256                         mnt->acdirmax = option;
1257                         break;
1258                 case Opt_actimeo:
1259                         string = match_strdup(args);
1260                         if (string == NULL)
1261                                 goto out_nomem;
1262                         rc = strict_strtoul(string, 10, &option);
1263                         kfree(string);
1264                         if (rc != 0)
1265                                 goto out_invalid_value;
1266                         mnt->acregmin = mnt->acregmax =
1267                         mnt->acdirmin = mnt->acdirmax = option;
1268                         break;
1269                 case Opt_namelen:
1270                         string = match_strdup(args);
1271                         if (string == NULL)
1272                                 goto out_nomem;
1273                         rc = strict_strtoul(string, 10, &option);
1274                         kfree(string);
1275                         if (rc != 0)
1276                                 goto out_invalid_value;
1277                         mnt->namlen = option;
1278                         break;
1279                 case Opt_mountport:
1280                         string = match_strdup(args);
1281                         if (string == NULL)
1282                                 goto out_nomem;
1283                         rc = strict_strtoul(string, 10, &option);
1284                         kfree(string);
1285                         if (rc != 0 || option > USHRT_MAX)
1286                                 goto out_invalid_value;
1287                         mnt->mount_server.port = option;
1288                         break;
1289                 case Opt_mountvers:
1290                         string = match_strdup(args);
1291                         if (string == NULL)
1292                                 goto out_nomem;
1293                         rc = strict_strtoul(string, 10, &option);
1294                         kfree(string);
1295                         if (rc != 0 ||
1296                             option < NFS_MNT_VERSION ||
1297                             option > NFS_MNT3_VERSION)
1298                                 goto out_invalid_value;
1299                         mnt->mount_server.version = option;
1300                         break;
1301                 case Opt_nfsvers:
1302                         string = match_strdup(args);
1303                         if (string == NULL)
1304                                 goto out_nomem;
1305                         rc = strict_strtoul(string, 10, &option);
1306                         kfree(string);
1307                         if (rc != 0)
1308                                 goto out_invalid_value;
1309                         switch (option) {
1310                         case NFS2_VERSION:
1311                                 mnt->flags &= ~NFS_MOUNT_VER3;
1312                                 mnt->version = 2;
1313                                 break;
1314                         case NFS3_VERSION:
1315                                 mnt->flags |= NFS_MOUNT_VER3;
1316                                 mnt->version = 3;
1317                                 break;
1318                         case NFS4_VERSION:
1319                                 mnt->flags &= ~NFS_MOUNT_VER3;
1320                                 mnt->version = 4;
1321                                 break;
1322                         default:
1323                                 goto out_invalid_value;
1324                         }
1325                         break;
1326                 case Opt_minorversion:
1327                         string = match_strdup(args);
1328                         if (string == NULL)
1329                                 goto out_nomem;
1330                         rc = strict_strtoul(string, 10, &option);
1331                         kfree(string);
1332                         if (rc != 0)
1333                                 goto out_invalid_value;
1334                         if (option > NFS4_MAX_MINOR_VERSION)
1335                                 goto out_invalid_value;
1336                         mnt->minorversion = option;
1337                         break;
1338
1339                 /*
1340                  * options that take text values
1341                  */
1342                 case Opt_sec:
1343                         string = match_strdup(args);
1344                         if (string == NULL)
1345                                 goto out_nomem;
1346                         rc = nfs_parse_security_flavors(string, mnt);
1347                         kfree(string);
1348                         if (!rc) {
1349                                 dfprintk(MOUNT, "NFS:   unrecognized "
1350                                                 "security flavor\n");
1351                                 return 0;
1352                         }
1353                         break;
1354                 case Opt_proto:
1355                         string = match_strdup(args);
1356                         if (string == NULL)
1357                                 goto out_nomem;
1358                         token = match_token(string,
1359                                             nfs_xprt_protocol_tokens, args);
1360
1361                         protofamily = AF_INET;
1362                         switch (token) {
1363                         case Opt_xprt_udp6:
1364                                 protofamily = AF_INET6;
1365                         case Opt_xprt_udp:
1366                                 mnt->flags &= ~NFS_MOUNT_TCP;
1367                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1368                                 kfree(string);
1369                                 break;
1370                         case Opt_xprt_tcp6:
1371                                 protofamily = AF_INET6;
1372                         case Opt_xprt_tcp:
1373                                 mnt->flags |= NFS_MOUNT_TCP;
1374                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1375                                 kfree(string);
1376                                 break;
1377                         case Opt_xprt_rdma:
1378                                 /* vector side protocols to TCP */
1379                                 mnt->flags |= NFS_MOUNT_TCP;
1380                                 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1381                                 xprt_load_transport(string);
1382                                 kfree(string);
1383                                 break;
1384                         default:
1385                                 dfprintk(MOUNT, "NFS:   unrecognized "
1386                                                 "transport protocol\n");
1387                                 kfree(string);
1388                                 return 0;
1389                         }
1390                         break;
1391                 case Opt_mountproto:
1392                         string = match_strdup(args);
1393                         if (string == NULL)
1394                                 goto out_nomem;
1395                         token = match_token(string,
1396                                             nfs_xprt_protocol_tokens, args);
1397                         kfree(string);
1398
1399                         mountfamily = AF_INET;
1400                         switch (token) {
1401                         case Opt_xprt_udp6:
1402                                 mountfamily = AF_INET6;
1403                         case Opt_xprt_udp:
1404                                 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1405                                 break;
1406                         case Opt_xprt_tcp6:
1407                                 mountfamily = AF_INET6;
1408                         case Opt_xprt_tcp:
1409                                 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1410                                 break;
1411                         case Opt_xprt_rdma: /* not used for side protocols */
1412                         default:
1413                                 dfprintk(MOUNT, "NFS:   unrecognized "
1414                                                 "transport protocol\n");
1415                                 return 0;
1416                         }
1417                         break;
1418                 case Opt_addr:
1419                         string = match_strdup(args);
1420                         if (string == NULL)
1421                                 goto out_nomem;
1422                         mnt->nfs_server.addrlen =
1423                                 rpc_pton(string, strlen(string),
1424                                         (struct sockaddr *)
1425                                         &mnt->nfs_server.address,
1426                                         sizeof(mnt->nfs_server.address));
1427                         kfree(string);
1428                         if (mnt->nfs_server.addrlen == 0)
1429                                 goto out_invalid_address;
1430                         break;
1431                 case Opt_clientaddr:
1432                         string = match_strdup(args);
1433                         if (string == NULL)
1434                                 goto out_nomem;
1435                         kfree(mnt->client_address);
1436                         mnt->client_address = string;
1437                         break;
1438                 case Opt_mounthost:
1439                         string = match_strdup(args);
1440                         if (string == NULL)
1441                                 goto out_nomem;
1442                         kfree(mnt->mount_server.hostname);
1443                         mnt->mount_server.hostname = string;
1444                         break;
1445                 case Opt_mountaddr:
1446                         string = match_strdup(args);
1447                         if (string == NULL)
1448                                 goto out_nomem;
1449                         mnt->mount_server.addrlen =
1450                                 rpc_pton(string, strlen(string),
1451                                         (struct sockaddr *)
1452                                         &mnt->mount_server.address,
1453                                         sizeof(mnt->mount_server.address));
1454                         kfree(string);
1455                         if (mnt->mount_server.addrlen == 0)
1456                                 goto out_invalid_address;
1457                         break;
1458                 case Opt_lookupcache:
1459                         string = match_strdup(args);
1460                         if (string == NULL)
1461                                 goto out_nomem;
1462                         token = match_token(string,
1463                                         nfs_lookupcache_tokens, args);
1464                         kfree(string);
1465                         switch (token) {
1466                                 case Opt_lookupcache_all:
1467                                         mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1468                                         break;
1469                                 case Opt_lookupcache_positive:
1470                                         mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1471                                         mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1472                                         break;
1473                                 case Opt_lookupcache_none:
1474                                         mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1475                                         break;
1476                                 default:
1477                                         dfprintk(MOUNT, "NFS:   invalid "
1478                                                         "lookupcache argument\n");
1479                                         return 0;
1480                         };
1481                         break;
1482                 case Opt_fscache_uniq:
1483                         string = match_strdup(args);
1484                         if (string == NULL)
1485                                 goto out_nomem;
1486                         kfree(mnt->fscache_uniq);
1487                         mnt->fscache_uniq = string;
1488                         mnt->options |= NFS_OPTION_FSCACHE;
1489                         break;
1490                 case Opt_local_lock:
1491                         string = match_strdup(args);
1492                         if (string == NULL)
1493                                 goto out_nomem;
1494                         token = match_token(string, nfs_local_lock_tokens,
1495                                         args);
1496                         kfree(string);
1497                         switch (token) {
1498                         case Opt_local_lock_all:
1499                                 mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
1500                                                NFS_MOUNT_LOCAL_FCNTL);
1501                                 break;
1502                         case Opt_local_lock_flock:
1503                                 mnt->flags |= NFS_MOUNT_LOCAL_FLOCK;
1504                                 break;
1505                         case Opt_local_lock_posix:
1506                                 mnt->flags |= NFS_MOUNT_LOCAL_FCNTL;
1507                                 break;
1508                         case Opt_local_lock_none:
1509                                 mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
1510                                                 NFS_MOUNT_LOCAL_FCNTL);
1511                                 break;
1512                         default:
1513                                 dfprintk(MOUNT, "NFS:   invalid "
1514                                                 "local_lock argument\n");
1515                                 return 0;
1516                         };
1517                         break;
1518
1519                 /*
1520                  * Special options
1521                  */
1522                 case Opt_sloppy:
1523                         sloppy = 1;
1524                         dfprintk(MOUNT, "NFS:   relaxing parsing rules\n");
1525                         break;
1526                 case Opt_userspace:
1527                 case Opt_deprecated:
1528                         dfprintk(MOUNT, "NFS:   ignoring mount option "
1529                                         "'%s'\n", p);
1530                         break;
1531
1532                 default:
1533                         invalid_option = 1;
1534                         dfprintk(MOUNT, "NFS:   unrecognized mount option "
1535                                         "'%s'\n", p);
1536                 }
1537         }
1538
1539         if (!sloppy && invalid_option)
1540                 return 0;
1541
1542         /*
1543          * verify that any proto=/mountproto= options match the address
1544          * familiies in the addr=/mountaddr= options.
1545          */
1546         if (protofamily != AF_UNSPEC &&
1547             protofamily != mnt->nfs_server.address.ss_family)
1548                 goto out_proto_mismatch;
1549
1550         if (mountfamily != AF_UNSPEC) {
1551                 if (mnt->mount_server.addrlen) {
1552                         if (mountfamily != mnt->mount_server.address.ss_family)
1553                                 goto out_mountproto_mismatch;
1554                 } else {
1555                         if (mountfamily != mnt->nfs_server.address.ss_family)
1556                                 goto out_mountproto_mismatch;
1557                 }
1558         }
1559
1560         return 1;
1561
1562 out_mountproto_mismatch:
1563         printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1564                          "option\n");
1565         return 0;
1566 out_proto_mismatch:
1567         printk(KERN_INFO "NFS: server address does not match proto= option\n");
1568         return 0;
1569 out_invalid_address:
1570         printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1571         return 0;
1572 out_invalid_value:
1573         printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
1574         return 0;
1575 out_nomem:
1576         printk(KERN_INFO "NFS: not enough memory to parse option\n");
1577         return 0;
1578 out_security_failure:
1579         free_secdata(secdata);
1580         printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1581         return 0;
1582 }
1583
1584 /*
1585  * Match the requested auth flavors with the list returned by
1586  * the server.  Returns zero and sets the mount's authentication
1587  * flavor on success; returns -EACCES if server does not support
1588  * the requested flavor.
1589  */
1590 static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1591                              struct nfs_mount_request *request)
1592 {
1593         unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1594
1595         /*
1596          * Certain releases of Linux's mountd return an empty
1597          * flavor list.  To prevent behavioral regression with
1598          * these servers (ie. rejecting mounts that used to
1599          * succeed), revert to pre-2.6.32 behavior (no checking)
1600          * if the returned flavor list is empty.
1601          */
1602         if (server_authlist_len == 0)
1603                 return 0;
1604
1605         /*
1606          * We avoid sophisticated negotiating here, as there are
1607          * plenty of cases where we can get it wrong, providing
1608          * either too little or too much security.
1609          *
1610          * RFC 2623, section 2.7 suggests we SHOULD prefer the
1611          * flavor listed first.  However, some servers list
1612          * AUTH_NULL first.  Our caller plants AUTH_SYS, the
1613          * preferred default, in args->auth_flavors[0] if user
1614          * didn't specify sec= mount option.
1615          */
1616         for (i = 0; i < args->auth_flavor_len; i++)
1617                 for (j = 0; j < server_authlist_len; j++)
1618                         if (args->auth_flavors[i] == request->auth_flavs[j]) {
1619                                 dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1620                                         request->auth_flavs[j]);
1621                                 args->auth_flavors[0] = request->auth_flavs[j];
1622                                 return 0;
1623                         }
1624
1625         dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1626         nfs_umount(request);
1627         return -EACCES;
1628 }
1629
1630 /*
1631  * Use the remote server's MOUNT service to request the NFS file handle
1632  * corresponding to the provided path.
1633  */
1634 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1635                          struct nfs_fh *root_fh)
1636 {
1637         rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1638         unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
1639         struct nfs_mount_request request = {
1640                 .sap            = (struct sockaddr *)
1641                                                 &args->mount_server.address,
1642                 .dirpath        = args->nfs_server.export_path,
1643                 .protocol       = args->mount_server.protocol,
1644                 .fh             = root_fh,
1645                 .noresvport     = args->flags & NFS_MOUNT_NORESVPORT,
1646                 .auth_flav_len  = &server_authlist_len,
1647                 .auth_flavs     = server_authlist,
1648         };
1649         int status;
1650
1651         if (args->mount_server.version == 0) {
1652                 switch (args->version) {
1653                         default:
1654                                 args->mount_server.version = NFS_MNT3_VERSION;
1655                                 break;
1656                         case 2:
1657                                 args->mount_server.version = NFS_MNT_VERSION;
1658                 }
1659         }
1660         request.version = args->mount_server.version;
1661
1662         if (args->mount_server.hostname)
1663                 request.hostname = args->mount_server.hostname;
1664         else
1665                 request.hostname = args->nfs_server.hostname;
1666
1667         /*
1668          * Construct the mount server's address.
1669          */
1670         if (args->mount_server.address.ss_family == AF_UNSPEC) {
1671                 memcpy(request.sap, &args->nfs_server.address,
1672                        args->nfs_server.addrlen);
1673                 args->mount_server.addrlen = args->nfs_server.addrlen;
1674         }
1675         request.salen = args->mount_server.addrlen;
1676         nfs_set_port(request.sap, &args->mount_server.port, 0);
1677
1678         /*
1679          * Now ask the mount server to map our export path
1680          * to a file handle.
1681          */
1682         status = nfs_mount(&request);
1683         if (status != 0) {
1684                 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1685                                 request.hostname, status);
1686                 return status;
1687         }
1688
1689         /*
1690          * MNTv1 (NFSv2) does not support auth flavor negotiation.
1691          */
1692         if (args->mount_server.version != NFS_MNT3_VERSION)
1693                 return 0;
1694         return nfs_walk_authlist(args, &request);
1695 }
1696
1697 static int nfs_parse_simple_hostname(const char *dev_name,
1698                                      char **hostname, size_t maxnamlen,
1699                                      char **export_path, size_t maxpathlen)
1700 {
1701         size_t len;
1702         char *colon, *comma;
1703
1704         colon = strchr(dev_name, ':');
1705         if (colon == NULL)
1706                 goto out_bad_devname;
1707
1708         len = colon - dev_name;
1709         if (len > maxnamlen)
1710                 goto out_hostname;
1711
1712         /* N.B. caller will free nfs_server.hostname in all cases */
1713         *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1714         if (!*hostname)
1715                 goto out_nomem;
1716
1717         /* kill possible hostname list: not supported */
1718         comma = strchr(*hostname, ',');
1719         if (comma != NULL) {
1720                 if (comma == *hostname)
1721                         goto out_bad_devname;
1722                 *comma = '\0';
1723         }
1724
1725         colon++;
1726         len = strlen(colon);
1727         if (len > maxpathlen)
1728                 goto out_path;
1729         *export_path = kstrndup(colon, len, GFP_KERNEL);
1730         if (!*export_path)
1731                 goto out_nomem;
1732
1733         dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1734         return 0;
1735
1736 out_bad_devname:
1737         dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1738         return -EINVAL;
1739
1740 out_nomem:
1741         dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1742         return -ENOMEM;
1743
1744 out_hostname:
1745         dfprintk(MOUNT, "NFS: server hostname too long\n");
1746         return -ENAMETOOLONG;
1747
1748 out_path:
1749         dfprintk(MOUNT, "NFS: export pathname too long\n");
1750         return -ENAMETOOLONG;
1751 }
1752
1753 /*
1754  * Hostname has square brackets around it because it contains one or
1755  * more colons.  We look for the first closing square bracket, and a
1756  * colon must follow it.
1757  */
1758 static int nfs_parse_protected_hostname(const char *dev_name,
1759                                         char **hostname, size_t maxnamlen,
1760                                         char **export_path, size_t maxpathlen)
1761 {
1762         size_t len;
1763         char *start, *end;
1764
1765         start = (char *)(dev_name + 1);
1766
1767         end = strchr(start, ']');
1768         if (end == NULL)
1769                 goto out_bad_devname;
1770         if (*(end + 1) != ':')
1771                 goto out_bad_devname;
1772
1773         len = end - start;
1774         if (len > maxnamlen)
1775                 goto out_hostname;
1776
1777         /* N.B. caller will free nfs_server.hostname in all cases */
1778         *hostname = kstrndup(start, len, GFP_KERNEL);
1779         if (*hostname == NULL)
1780                 goto out_nomem;
1781
1782         end += 2;
1783         len = strlen(end);
1784         if (len > maxpathlen)
1785                 goto out_path;
1786         *export_path = kstrndup(end, len, GFP_KERNEL);
1787         if (!*export_path)
1788                 goto out_nomem;
1789
1790         return 0;
1791
1792 out_bad_devname:
1793         dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1794         return -EINVAL;
1795
1796 out_nomem:
1797         dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1798         return -ENOMEM;
1799
1800 out_hostname:
1801         dfprintk(MOUNT, "NFS: server hostname too long\n");
1802         return -ENAMETOOLONG;
1803
1804 out_path:
1805         dfprintk(MOUNT, "NFS: export pathname too long\n");
1806         return -ENAMETOOLONG;
1807 }
1808
1809 /*
1810  * Split "dev_name" into "hostname:export_path".
1811  *
1812  * The leftmost colon demarks the split between the server's hostname
1813  * and the export path.  If the hostname starts with a left square
1814  * bracket, then it may contain colons.
1815  *
1816  * Note: caller frees hostname and export path, even on error.
1817  */
1818 static int nfs_parse_devname(const char *dev_name,
1819                              char **hostname, size_t maxnamlen,
1820                              char **export_path, size_t maxpathlen)
1821 {
1822         if (*dev_name == '[')
1823                 return nfs_parse_protected_hostname(dev_name,
1824                                                     hostname, maxnamlen,
1825                                                     export_path, maxpathlen);
1826
1827         return nfs_parse_simple_hostname(dev_name,
1828                                          hostname, maxnamlen,
1829                                          export_path, maxpathlen);
1830 }
1831
1832 /*
1833  * Validate the NFS2/NFS3 mount data
1834  * - fills in the mount root filehandle
1835  *
1836  * For option strings, user space handles the following behaviors:
1837  *
1838  * + DNS: mapping server host name to IP address ("addr=" option)
1839  *
1840  * + failure mode: how to behave if a mount request can't be handled
1841  *   immediately ("fg/bg" option)
1842  *
1843  * + retry: how often to retry a mount request ("retry=" option)
1844  *
1845  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1846  *   mountproto=tcp after mountproto=udp, and so on
1847  */
1848 static int nfs_validate_mount_data(void *options,
1849                                    struct nfs_parsed_mount_data *args,
1850                                    struct nfs_fh *mntfh,
1851                                    const char *dev_name)
1852 {
1853         struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1854         struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
1855
1856         if (data == NULL)
1857                 goto out_no_data;
1858
1859         switch (data->version) {
1860         case 1:
1861                 data->namlen = 0;
1862         case 2:
1863                 data->bsize = 0;
1864         case 3:
1865                 if (data->flags & NFS_MOUNT_VER3)
1866                         goto out_no_v3;
1867                 data->root.size = NFS2_FHSIZE;
1868                 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1869         case 4:
1870                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1871                         goto out_no_sec;
1872         case 5:
1873                 memset(data->context, 0, sizeof(data->context));
1874         case 6:
1875                 if (data->flags & NFS_MOUNT_VER3) {
1876                         if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1877                                 goto out_invalid_fh;
1878                         mntfh->size = data->root.size;
1879                         args->version = 3;
1880                 } else {
1881                         mntfh->size = NFS2_FHSIZE;
1882                         args->version = 2;
1883                 }
1884
1885
1886                 memcpy(mntfh->data, data->root.data, mntfh->size);
1887                 if (mntfh->size < sizeof(mntfh->data))
1888                         memset(mntfh->data + mntfh->size, 0,
1889                                sizeof(mntfh->data) - mntfh->size);
1890
1891                 /*
1892                  * Translate to nfs_parsed_mount_data, which nfs_fill_super
1893                  * can deal with.
1894                  */
1895                 args->flags             = data->flags & NFS_MOUNT_FLAGMASK;
1896                 args->flags             |= NFS_MOUNT_LEGACY_INTERFACE;
1897                 args->rsize             = data->rsize;
1898                 args->wsize             = data->wsize;
1899                 args->timeo             = data->timeo;
1900                 args->retrans           = data->retrans;
1901                 args->acregmin          = data->acregmin;
1902                 args->acregmax          = data->acregmax;
1903                 args->acdirmin          = data->acdirmin;
1904                 args->acdirmax          = data->acdirmax;
1905
1906                 memcpy(sap, &data->addr, sizeof(data->addr));
1907                 args->nfs_server.addrlen = sizeof(data->addr);
1908                 if (!nfs_verify_server_address(sap))
1909                         goto out_no_address;
1910
1911                 if (!(data->flags & NFS_MOUNT_TCP))
1912                         args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1913                 /* N.B. caller will free nfs_server.hostname in all cases */
1914                 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1915                 args->namlen            = data->namlen;
1916                 args->bsize             = data->bsize;
1917
1918                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1919                         args->auth_flavors[0] = data->pseudoflavor;
1920                 if (!args->nfs_server.hostname)
1921                         goto out_nomem;
1922
1923                 if (!(data->flags & NFS_MOUNT_NONLM))
1924                         args->flags &= ~(NFS_MOUNT_LOCAL_FLOCK|
1925                                          NFS_MOUNT_LOCAL_FCNTL);
1926                 else
1927                         args->flags |= (NFS_MOUNT_LOCAL_FLOCK|
1928                                         NFS_MOUNT_LOCAL_FCNTL);
1929                 /*
1930                  * The legacy version 6 binary mount data from userspace has a
1931                  * field used only to transport selinux information into the
1932                  * the kernel.  To continue to support that functionality we
1933                  * have a touch of selinux knowledge here in the NFS code. The
1934                  * userspace code converted context=blah to just blah so we are
1935                  * converting back to the full string selinux understands.
1936                  */
1937                 if (data->context[0]){
1938 #ifdef CONFIG_SECURITY_SELINUX
1939                         int rc;
1940                         char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1941                         if (!opts_str)
1942                                 return -ENOMEM;
1943                         strcpy(opts_str, "context=");
1944                         data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1945                         strcat(opts_str, &data->context[0]);
1946                         rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1947                         kfree(opts_str);
1948                         if (rc)
1949                                 return rc;
1950 #else
1951                         return -EINVAL;
1952 #endif
1953                 }
1954
1955                 break;
1956         default: {
1957                 int status;
1958
1959                 if (nfs_parse_mount_options((char *)options, args) == 0)
1960                         return -EINVAL;
1961
1962                 if (!nfs_verify_server_address(sap))
1963                         goto out_no_address;
1964
1965                 if (args->version == 4)
1966 #ifdef CONFIG_NFS_V4
1967                         return nfs4_validate_text_mount_data(options,
1968                                                              args, dev_name);
1969 #else
1970                         goto out_v4_not_compiled;
1971 #endif
1972
1973                 nfs_set_port(sap, &args->nfs_server.port, 0);
1974
1975                 nfs_set_mount_transport_protocol(args);
1976
1977                 status = nfs_parse_devname(dev_name,
1978                                            &args->nfs_server.hostname,
1979                                            PAGE_SIZE,
1980                                            &args->nfs_server.export_path,
1981                                            NFS_MAXPATHLEN);
1982                 if (!status)
1983                         status = nfs_try_mount(args, mntfh);
1984
1985                 kfree(args->nfs_server.export_path);
1986                 args->nfs_server.export_path = NULL;
1987
1988                 if (status)
1989                         return status;
1990
1991                 break;
1992                 }
1993         }
1994
1995 #ifndef CONFIG_NFS_V3
1996         if (args->version == 3)
1997                 goto out_v3_not_compiled;
1998 #endif /* !CONFIG_NFS_V3 */
1999
2000         return 0;
2001
2002 out_no_data:
2003         dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
2004         return -EINVAL;
2005
2006 out_no_v3:
2007         dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
2008                  data->version);
2009         return -EINVAL;
2010
2011 out_no_sec:
2012         dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
2013         return -EINVAL;
2014
2015 #ifndef CONFIG_NFS_V3
2016 out_v3_not_compiled:
2017         dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
2018         return -EPROTONOSUPPORT;
2019 #endif /* !CONFIG_NFS_V3 */
2020
2021 #ifndef CONFIG_NFS_V4
2022 out_v4_not_compiled:
2023         dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
2024         return -EPROTONOSUPPORT;
2025 #endif /* !CONFIG_NFS_V4 */
2026
2027 out_nomem:
2028         dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
2029         return -ENOMEM;
2030
2031 out_no_address:
2032         dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
2033         return -EINVAL;
2034
2035 out_invalid_fh:
2036         dfprintk(MOUNT, "NFS: invalid root filehandle\n");
2037         return -EINVAL;
2038 }
2039
2040 static int
2041 nfs_compare_remount_data(struct nfs_server *nfss,
2042                          struct nfs_parsed_mount_data *data)
2043 {
2044         if (data->flags != nfss->flags ||
2045             data->rsize != nfss->rsize ||
2046             data->wsize != nfss->wsize ||
2047             data->retrans != nfss->client->cl_timeout->to_retries ||
2048             data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
2049             data->acregmin != nfss->acregmin / HZ ||
2050             data->acregmax != nfss->acregmax / HZ ||
2051             data->acdirmin != nfss->acdirmin / HZ ||
2052             data->acdirmax != nfss->acdirmax / HZ ||
2053             data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
2054             data->nfs_server.port != nfss->port ||
2055             data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
2056             !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
2057                           (struct sockaddr *)&nfss->nfs_client->cl_addr))
2058                 return -EINVAL;
2059
2060         return 0;
2061 }
2062
2063 static int
2064 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
2065 {
2066         int error;
2067         struct nfs_server *nfss = sb->s_fs_info;
2068         struct nfs_parsed_mount_data *data;
2069         struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
2070         struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
2071         u32 nfsvers = nfss->nfs_client->rpc_ops->version;
2072
2073         /*
2074          * Userspace mount programs that send binary options generally send
2075          * them populated with default values. We have no way to know which
2076          * ones were explicitly specified. Fall back to legacy behavior and
2077          * just return success.
2078          */
2079         if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
2080             (nfsvers <= 3 && (!options || (options->version >= 1 &&
2081                                            options->version <= 6))))
2082                 return 0;
2083
2084         data = kzalloc(sizeof(*data), GFP_KERNEL);
2085         if (data == NULL)
2086                 return -ENOMEM;
2087
2088         /* fill out struct with values from existing mount */
2089         data->flags = nfss->flags;
2090         data->rsize = nfss->rsize;
2091         data->wsize = nfss->wsize;
2092         data->retrans = nfss->client->cl_timeout->to_retries;
2093         data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
2094         data->acregmin = nfss->acregmin / HZ;
2095         data->acregmax = nfss->acregmax / HZ;
2096         data->acdirmin = nfss->acdirmin / HZ;
2097         data->acdirmax = nfss->acdirmax / HZ;
2098         data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
2099         data->nfs_server.port = nfss->port;
2100         data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
2101         memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
2102                 data->nfs_server.addrlen);
2103
2104         /* overwrite those values with any that were specified */
2105         error = nfs_parse_mount_options((char *)options, data);
2106         if (error < 0)
2107                 goto out;
2108
2109         /* compare new mount options with old ones */
2110         error = nfs_compare_remount_data(nfss, data);
2111 out:
2112         kfree(data);
2113         return error;
2114 }
2115
2116 /*
2117  * Initialise the common bits of the superblock
2118  */
2119 static inline void nfs_initialise_sb(struct super_block *sb)
2120 {
2121         struct nfs_server *server = NFS_SB(sb);
2122
2123         sb->s_magic = NFS_SUPER_MAGIC;
2124
2125         /* We probably want something more informative here */
2126         snprintf(sb->s_id, sizeof(sb->s_id),
2127                  "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
2128
2129         if (sb->s_blocksize == 0)
2130                 sb->s_blocksize = nfs_block_bits(server->wsize,
2131                                                  &sb->s_blocksize_bits);
2132
2133         if (server->flags & NFS_MOUNT_NOAC)
2134                 sb->s_flags |= MS_SYNCHRONOUS;
2135
2136         sb->s_bdi = &server->backing_dev_info;
2137
2138         nfs_super_set_maxbytes(sb, server->maxfilesize);
2139 }
2140
2141 /*
2142  * Finish setting up an NFS2/3 superblock
2143  */
2144 static void nfs_fill_super(struct super_block *sb,
2145                            struct nfs_parsed_mount_data *data)
2146 {
2147         struct nfs_server *server = NFS_SB(sb);
2148
2149         sb->s_blocksize_bits = 0;
2150         sb->s_blocksize = 0;
2151         if (data->bsize)
2152                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2153
2154         if (server->nfs_client->rpc_ops->version == 3) {
2155                 /* The VFS shouldn't apply the umask to mode bits. We will do
2156                  * so ourselves when necessary.
2157                  */
2158                 sb->s_flags |= MS_POSIXACL;
2159                 sb->s_time_gran = 1;
2160         }
2161
2162         sb->s_op = &nfs_sops;
2163         nfs_initialise_sb(sb);
2164 }
2165
2166 /*
2167  * Finish setting up a cloned NFS2/3 superblock
2168  */
2169 static void nfs_clone_super(struct super_block *sb,
2170                             const struct super_block *old_sb)
2171 {
2172         struct nfs_server *server = NFS_SB(sb);
2173
2174         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2175         sb->s_blocksize = old_sb->s_blocksize;
2176         sb->s_maxbytes = old_sb->s_maxbytes;
2177
2178         if (server->nfs_client->rpc_ops->version == 3) {
2179                 /* The VFS shouldn't apply the umask to mode bits. We will do
2180                  * so ourselves when necessary.
2181                  */
2182                 sb->s_flags |= MS_POSIXACL;
2183                 sb->s_time_gran = 1;
2184         }
2185
2186         sb->s_op = old_sb->s_op;
2187         nfs_initialise_sb(sb);
2188 }
2189
2190 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2191 {
2192         const struct nfs_server *a = s->s_fs_info;
2193         const struct rpc_clnt *clnt_a = a->client;
2194         const struct rpc_clnt *clnt_b = b->client;
2195
2196         if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2197                 goto Ebusy;
2198         if (a->nfs_client != b->nfs_client)
2199                 goto Ebusy;
2200         if (a->flags != b->flags)
2201                 goto Ebusy;
2202         if (a->wsize != b->wsize)
2203                 goto Ebusy;
2204         if (a->rsize != b->rsize)
2205                 goto Ebusy;
2206         if (a->acregmin != b->acregmin)
2207                 goto Ebusy;
2208         if (a->acregmax != b->acregmax)
2209                 goto Ebusy;
2210         if (a->acdirmin != b->acdirmin)
2211                 goto Ebusy;
2212         if (a->acdirmax != b->acdirmax)
2213                 goto Ebusy;
2214         if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2215                 goto Ebusy;
2216         return 1;
2217 Ebusy:
2218         return 0;
2219 }
2220
2221 struct nfs_sb_mountdata {
2222         struct nfs_server *server;
2223         int mntflags;
2224 };
2225
2226 static int nfs_set_super(struct super_block *s, void *data)
2227 {
2228         struct nfs_sb_mountdata *sb_mntdata = data;
2229         struct nfs_server *server = sb_mntdata->server;
2230         int ret;
2231
2232         s->s_flags = sb_mntdata->mntflags;
2233         s->s_fs_info = server;
2234         s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
2235         ret = set_anon_super(s, server);
2236         if (ret == 0)
2237                 server->s_dev = s->s_dev;
2238         return ret;
2239 }
2240
2241 static int nfs_compare_super_address(struct nfs_server *server1,
2242                                      struct nfs_server *server2)
2243 {
2244         struct sockaddr *sap1, *sap2;
2245
2246         sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2247         sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2248
2249         if (sap1->sa_family != sap2->sa_family)
2250                 return 0;
2251
2252         switch (sap1->sa_family) {
2253         case AF_INET: {
2254                 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2255                 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2256                 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2257                         return 0;
2258                 if (sin1->sin_port != sin2->sin_port)
2259                         return 0;
2260                 break;
2261         }
2262         case AF_INET6: {
2263                 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2264                 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2265                 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2266                         return 0;
2267                 if (sin1->sin6_port != sin2->sin6_port)
2268                         return 0;
2269                 break;
2270         }
2271         default:
2272                 return 0;
2273         }
2274
2275         return 1;
2276 }
2277
2278 static int nfs_compare_super(struct super_block *sb, void *data)
2279 {
2280         struct nfs_sb_mountdata *sb_mntdata = data;
2281         struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2282         int mntflags = sb_mntdata->mntflags;
2283
2284         if (!nfs_compare_super_address(old, server))
2285                 return 0;
2286         /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2287         if (old->flags & NFS_MOUNT_UNSHARED)
2288                 return 0;
2289         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2290                 return 0;
2291         return nfs_compare_mount_options(sb, server, mntflags);
2292 }
2293
2294 static int nfs_bdi_register(struct nfs_server *server)
2295 {
2296         return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2297 }
2298
2299 static struct dentry *nfs_fs_mount(struct file_system_type *fs_type,
2300         int flags, const char *dev_name, void *raw_data)
2301 {
2302         struct nfs_server *server = NULL;
2303         struct super_block *s;
2304         struct nfs_parsed_mount_data *data;
2305         struct nfs_fh *mntfh;
2306         struct dentry *mntroot = ERR_PTR(-ENOMEM);
2307         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2308         struct nfs_sb_mountdata sb_mntdata = {
2309                 .mntflags = flags,
2310         };
2311         int error;
2312
2313         data = nfs_alloc_parsed_mount_data(NFS_DEFAULT_VERSION);
2314         mntfh = nfs_alloc_fhandle();
2315         if (data == NULL || mntfh == NULL)
2316                 goto out_free_fh;
2317
2318         security_init_mnt_opts(&data->lsm_opts);
2319
2320         /* Validate the mount data */
2321         error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
2322         if (error < 0) {
2323                 mntroot = ERR_PTR(error);
2324                 goto out;
2325         }
2326
2327 #ifdef CONFIG_NFS_V4
2328         if (data->version == 4) {
2329                 mntroot = nfs4_try_mount(flags, dev_name, data);
2330                 kfree(data->client_address);
2331                 kfree(data->nfs_server.export_path);
2332                 goto out;
2333         }
2334 #endif  /* CONFIG_NFS_V4 */
2335
2336         /* Get a volume representation */
2337         server = nfs_create_server(data, mntfh);
2338         if (IS_ERR(server)) {
2339                 mntroot = ERR_CAST(server);
2340                 goto out;
2341         }
2342         sb_mntdata.server = server;
2343
2344         if (server->flags & NFS_MOUNT_UNSHARED)
2345                 compare_super = NULL;
2346
2347         /* Get a superblock - note that we may end up sharing one that already exists */
2348         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2349         if (IS_ERR(s)) {
2350                 mntroot = ERR_CAST(s);
2351                 goto out_err_nosb;
2352         }
2353
2354         if (s->s_fs_info != server) {
2355                 nfs_free_server(server);
2356                 server = NULL;
2357         } else {
2358                 error = nfs_bdi_register(server);
2359                 if (error) {
2360                         mntroot = ERR_PTR(error);
2361                         goto error_splat_bdi;
2362                 }
2363         }
2364
2365         if (!s->s_root) {
2366                 /* initial superblock/root creation */
2367                 nfs_fill_super(s, data);
2368                 nfs_fscache_get_super_cookie(
2369                         s, data ? data->fscache_uniq : NULL, NULL);
2370         }
2371
2372         mntroot = nfs_get_root(s, mntfh, dev_name);
2373         if (IS_ERR(mntroot))
2374                 goto error_splat_super;
2375
2376         error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2377         if (error)
2378                 goto error_splat_root;
2379
2380         s->s_flags |= MS_ACTIVE;
2381
2382 out:
2383         kfree(data->nfs_server.hostname);
2384         kfree(data->mount_server.hostname);
2385         kfree(data->fscache_uniq);
2386         security_free_mnt_opts(&data->lsm_opts);
2387 out_free_fh:
2388         nfs_free_fhandle(mntfh);
2389         kfree(data);
2390         return mntroot;
2391
2392 out_err_nosb:
2393         nfs_free_server(server);
2394         goto out;
2395
2396 error_splat_root:
2397         dput(mntroot);
2398         mntroot = ERR_PTR(error);
2399 error_splat_super:
2400         if (server && !s->s_root)
2401                 bdi_unregister(&server->backing_dev_info);
2402 error_splat_bdi:
2403         deactivate_locked_super(s);
2404         goto out;
2405 }
2406
2407 /*
2408  * Ensure that we unregister the bdi before kill_anon_super
2409  * releases the device name
2410  */
2411 static void nfs_put_super(struct super_block *s)
2412 {
2413         struct nfs_server *server = NFS_SB(s);
2414
2415         bdi_unregister(&server->backing_dev_info);
2416 }
2417
2418 /*
2419  * Destroy an NFS2/3 superblock
2420  */
2421 static void nfs_kill_super(struct super_block *s)
2422 {
2423         struct nfs_server *server = NFS_SB(s);
2424
2425         kill_anon_super(s);
2426         nfs_fscache_release_super_cookie(s);
2427         nfs_free_server(server);
2428 }
2429
2430 /*
2431  * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2432  */
2433 static struct dentry *
2434 nfs_xdev_mount(struct file_system_type *fs_type, int flags,
2435                 const char *dev_name, void *raw_data)
2436 {
2437         struct nfs_clone_mount *data = raw_data;
2438         struct super_block *s;
2439         struct nfs_server *server;
2440         struct dentry *mntroot;
2441         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2442         struct nfs_sb_mountdata sb_mntdata = {
2443                 .mntflags = flags,
2444         };
2445         int error;
2446
2447         dprintk("--> nfs_xdev_mount()\n");
2448
2449         /* create a new volume representation */
2450         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2451         if (IS_ERR(server)) {
2452                 error = PTR_ERR(server);
2453                 goto out_err_noserver;
2454         }
2455         sb_mntdata.server = server;
2456
2457         if (server->flags & NFS_MOUNT_UNSHARED)
2458                 compare_super = NULL;
2459
2460         /* Get a superblock - note that we may end up sharing one that already exists */
2461         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2462         if (IS_ERR(s)) {
2463                 error = PTR_ERR(s);
2464                 goto out_err_nosb;
2465         }
2466
2467         if (s->s_fs_info != server) {
2468                 nfs_free_server(server);
2469                 server = NULL;
2470         } else {
2471                 error = nfs_bdi_register(server);
2472                 if (error)
2473                         goto error_splat_bdi;
2474         }
2475
2476         if (!s->s_root) {
2477                 /* initial superblock/root creation */
2478                 nfs_clone_super(s, data->sb);
2479                 nfs_fscache_get_super_cookie(s, NULL, data);
2480         }
2481
2482         mntroot = nfs_get_root(s, data->fh, dev_name);
2483         if (IS_ERR(mntroot)) {
2484                 error = PTR_ERR(mntroot);
2485                 goto error_splat_super;
2486         }
2487         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2488                 dput(mntroot);
2489                 error = -ESTALE;
2490                 goto error_splat_super;
2491         }
2492
2493         s->s_flags |= MS_ACTIVE;
2494
2495         /* clone any lsm security options from the parent to the new sb */
2496         security_sb_clone_mnt_opts(data->sb, s);
2497
2498         dprintk("<-- nfs_xdev_mount() = 0\n");
2499         return mntroot;
2500
2501 out_err_nosb:
2502         nfs_free_server(server);
2503 out_err_noserver:
2504         dprintk("<-- nfs_xdev_mount() = %d [error]\n", error);
2505         return ERR_PTR(error);
2506
2507 error_splat_super:
2508         if (server && !s->s_root)
2509                 bdi_unregister(&server->backing_dev_info);
2510 error_splat_bdi:
2511         deactivate_locked_super(s);
2512         dprintk("<-- nfs_xdev_mount() = %d [splat]\n", error);
2513         return ERR_PTR(error);
2514 }
2515
2516 #ifdef CONFIG_NFS_V4
2517
2518 /*
2519  * Finish setting up a cloned NFS4 superblock
2520  */
2521 static void nfs4_clone_super(struct super_block *sb,
2522                             const struct super_block *old_sb)
2523 {
2524         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2525         sb->s_blocksize = old_sb->s_blocksize;
2526         sb->s_maxbytes = old_sb->s_maxbytes;
2527         sb->s_time_gran = 1;
2528         sb->s_op = old_sb->s_op;
2529         /*
2530          * The VFS shouldn't apply the umask to mode bits. We will do
2531          * so ourselves when necessary.
2532          */
2533         sb->s_flags  |= MS_POSIXACL;
2534         sb->s_xattr  = old_sb->s_xattr;
2535         nfs_initialise_sb(sb);
2536 }
2537
2538 /*
2539  * Set up an NFS4 superblock
2540  */
2541 static void nfs4_fill_super(struct super_block *sb)
2542 {
2543         sb->s_time_gran = 1;
2544         sb->s_op = &nfs4_sops;
2545         /*
2546          * The VFS shouldn't apply the umask to mode bits. We will do
2547          * so ourselves when necessary.
2548          */
2549         sb->s_flags  |= MS_POSIXACL;
2550         sb->s_xattr = nfs4_xattr_handlers;
2551         nfs_initialise_sb(sb);
2552 }
2553
2554 static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2555 {
2556         args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
2557                          NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
2558 }
2559
2560 static int nfs4_validate_text_mount_data(void *options,
2561                                          struct nfs_parsed_mount_data *args,
2562                                          const char *dev_name)
2563 {
2564         struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2565
2566         nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
2567
2568         nfs_validate_transport_protocol(args);
2569
2570         nfs4_validate_mount_flags(args);
2571
2572         if (args->version != 4) {
2573                 dfprintk(MOUNT,
2574                          "NFS4: Illegal mount version\n");
2575                 return -EINVAL;
2576         }
2577
2578         if (args->auth_flavor_len > 1) {
2579                 dfprintk(MOUNT,
2580                          "NFS4: Too many RPC auth flavours specified\n");
2581                 return -EINVAL;
2582         }
2583
2584         if (args->client_address == NULL) {
2585                 dfprintk(MOUNT,
2586                          "NFS4: mount program didn't pass callback address\n");
2587                 return -EINVAL;
2588         }
2589
2590         return nfs_parse_devname(dev_name,
2591                                    &args->nfs_server.hostname,
2592                                    NFS4_MAXNAMLEN,
2593                                    &args->nfs_server.export_path,
2594                                    NFS4_MAXPATHLEN);
2595 }
2596
2597 /*
2598  * Validate NFSv4 mount options
2599  */
2600 static int nfs4_validate_mount_data(void *options,
2601                                     struct nfs_parsed_mount_data *args,
2602                                     const char *dev_name)
2603 {
2604         struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2605         struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2606         char *c;
2607
2608         if (data == NULL)
2609                 goto out_no_data;
2610
2611         switch (data->version) {
2612         case 1:
2613                 if (data->host_addrlen > sizeof(args->nfs_server.address))
2614                         goto out_no_address;
2615                 if (data->host_addrlen == 0)
2616                         goto out_no_address;
2617                 args->nfs_server.addrlen = data->host_addrlen;
2618                 if (copy_from_user(sap, data->host_addr, data->host_addrlen))
2619                         return -EFAULT;
2620                 if (!nfs_verify_server_address(sap))
2621                         goto out_no_address;
2622
2623                 if (data->auth_flavourlen) {
2624                         if (data->auth_flavourlen > 1)
2625                                 goto out_inval_auth;
2626                         if (copy_from_user(&args->auth_flavors[0],
2627                                            data->auth_flavours,
2628                                            sizeof(args->auth_flavors[0])))
2629                                 return -EFAULT;
2630                 }
2631
2632                 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2633                 if (IS_ERR(c))
2634                         return PTR_ERR(c);
2635                 args->nfs_server.hostname = c;
2636
2637                 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2638                 if (IS_ERR(c))
2639                         return PTR_ERR(c);
2640                 args->nfs_server.export_path = c;
2641                 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2642
2643                 c = strndup_user(data->client_addr.data, 16);
2644                 if (IS_ERR(c))
2645                         return PTR_ERR(c);
2646                 args->client_address = c;
2647
2648                 /*
2649                  * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2650                  * can deal with.
2651                  */
2652
2653                 args->flags     = data->flags & NFS4_MOUNT_FLAGMASK;
2654                 args->rsize     = data->rsize;
2655                 args->wsize     = data->wsize;
2656                 args->timeo     = data->timeo;
2657                 args->retrans   = data->retrans;
2658                 args->acregmin  = data->acregmin;
2659                 args->acregmax  = data->acregmax;
2660                 args->acdirmin  = data->acdirmin;
2661                 args->acdirmax  = data->acdirmax;
2662                 args->nfs_server.protocol = data->proto;
2663                 nfs_validate_transport_protocol(args);
2664
2665                 break;
2666         default:
2667                 if (nfs_parse_mount_options((char *)options, args) == 0)
2668                         return -EINVAL;
2669
2670                 if (!nfs_verify_server_address(sap))
2671                         return -EINVAL;
2672
2673                 return nfs4_validate_text_mount_data(options, args, dev_name);
2674         }
2675
2676         return 0;
2677
2678 out_no_data:
2679         dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2680         return -EINVAL;
2681
2682 out_inval_auth:
2683         dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2684                  data->auth_flavourlen);
2685         return -EINVAL;
2686
2687 out_no_address:
2688         dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2689         return -EINVAL;
2690 }
2691
2692 /*
2693  * Get the superblock for the NFS4 root partition
2694  */
2695 static struct dentry *
2696 nfs4_remote_mount(struct file_system_type *fs_type, int flags,
2697                   const char *dev_name, void *raw_data)
2698 {
2699         struct nfs_parsed_mount_data *data = raw_data;
2700         struct super_block *s;
2701         struct nfs_server *server;
2702         struct nfs_fh *mntfh;
2703         struct dentry *mntroot;
2704         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2705         struct nfs_sb_mountdata sb_mntdata = {
2706                 .mntflags = flags,
2707         };
2708         int error = -ENOMEM;
2709
2710         mntfh = nfs_alloc_fhandle();
2711         if (data == NULL || mntfh == NULL)
2712                 goto out_free_fh;
2713
2714         security_init_mnt_opts(&data->lsm_opts);
2715
2716         /* Get a volume representation */
2717         server = nfs4_create_server(data, mntfh);
2718         if (IS_ERR(server)) {
2719                 error = PTR_ERR(server);
2720                 goto out;
2721         }
2722         sb_mntdata.server = server;
2723
2724         if (server->flags & NFS4_MOUNT_UNSHARED)
2725                 compare_super = NULL;
2726
2727         /* Get a superblock - note that we may end up sharing one that already exists */
2728         s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2729         if (IS_ERR(s)) {
2730                 error = PTR_ERR(s);
2731                 goto out_free;
2732         }
2733
2734         if (s->s_fs_info != server) {
2735                 nfs_free_server(server);
2736                 server = NULL;
2737         } else {
2738                 error = nfs_bdi_register(server);
2739                 if (error)
2740                         goto error_splat_bdi;
2741         }
2742
2743         if (!s->s_root) {
2744                 /* initial superblock/root creation */
2745                 nfs4_fill_super(s);
2746                 nfs_fscache_get_super_cookie(
2747                         s, data ? data->fscache_uniq : NULL, NULL);
2748         }
2749
2750         mntroot = nfs4_get_root(s, mntfh, dev_name);
2751         if (IS_ERR(mntroot)) {
2752                 error = PTR_ERR(mntroot);
2753                 goto error_splat_super;
2754         }
2755
2756         error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2757         if (error)
2758                 goto error_splat_root;
2759
2760         s->s_flags |= MS_ACTIVE;
2761
2762         security_free_mnt_opts(&data->lsm_opts);
2763         nfs_free_fhandle(mntfh);
2764         return mntroot;
2765
2766 out:
2767         security_free_mnt_opts(&data->lsm_opts);
2768 out_free_fh:
2769         nfs_free_fhandle(mntfh);
2770         return ERR_PTR(error);
2771
2772 out_free:
2773         nfs_free_server(server);
2774         goto out;
2775
2776 error_splat_root:
2777         dput(mntroot);
2778 error_splat_super:
2779         if (server && !s->s_root)
2780                 bdi_unregister(&server->backing_dev_info);
2781 error_splat_bdi:
2782         deactivate_locked_super(s);
2783         goto out;
2784 }
2785
2786 static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2787                 int flags, void *data, const char *hostname)
2788 {
2789         struct vfsmount *root_mnt;
2790         char *root_devname;
2791         size_t len;
2792
2793         len = strlen(hostname) + 3;
2794         root_devname = kmalloc(len, GFP_KERNEL);
2795         if (root_devname == NULL)
2796                 return ERR_PTR(-ENOMEM);
2797         snprintf(root_devname, len, "%s:/", hostname);
2798         root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2799         kfree(root_devname);
2800         return root_mnt;
2801 }
2802
2803 struct nfs_referral_count {
2804         struct list_head list;
2805         const struct task_struct *task;
2806         unsigned int referral_count;
2807 };
2808
2809 static LIST_HEAD(nfs_referral_count_list);
2810 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
2811
2812 static struct nfs_referral_count *nfs_find_referral_count(void)
2813 {
2814         struct nfs_referral_count *p;
2815
2816         list_for_each_entry(p, &nfs_referral_count_list, list) {
2817                 if (p->task == current)
2818                         return p;
2819         }
2820         return NULL;
2821 }
2822
2823 #define NFS_MAX_NESTED_REFERRALS 2
2824
2825 static int nfs_referral_loop_protect(void)
2826 {
2827         struct nfs_referral_count *p, *new;
2828         int ret = -ENOMEM;
2829
2830         new = kmalloc(sizeof(*new), GFP_KERNEL);
2831         if (!new)
2832                 goto out;
2833         new->task = current;
2834         new->referral_count = 1;
2835
2836         ret = 0;
2837         spin_lock(&nfs_referral_count_list_lock);
2838         p = nfs_find_referral_count();
2839         if (p != NULL) {
2840                 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
2841                         ret = -ELOOP;
2842                 else
2843                         p->referral_count++;
2844         } else {
2845                 list_add(&new->list, &nfs_referral_count_list);
2846                 new = NULL;
2847         }
2848         spin_unlock(&nfs_referral_count_list_lock);
2849         kfree(new);
2850 out:
2851         return ret;
2852 }
2853
2854 static void nfs_referral_loop_unprotect(void)
2855 {
2856         struct nfs_referral_count *p;
2857
2858         spin_lock(&nfs_referral_count_list_lock);
2859         p = nfs_find_referral_count();
2860         p->referral_count--;
2861         if (p->referral_count == 0)
2862                 list_del(&p->list);
2863         else
2864                 p = NULL;
2865         spin_unlock(&nfs_referral_count_list_lock);
2866         kfree(p);
2867 }
2868
2869 static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
2870                 const char *export_path)
2871 {
2872         struct nameidata *nd = NULL;
2873         struct mnt_namespace *ns_private;
2874         struct super_block *s;
2875         struct dentry *dentry;
2876         int ret;
2877
2878         nd = kmalloc(sizeof(*nd), GFP_KERNEL);
2879         if (nd == NULL)
2880                 return ERR_PTR(-ENOMEM);
2881
2882         ns_private = create_mnt_ns(root_mnt);
2883         ret = PTR_ERR(ns_private);
2884         if (IS_ERR(ns_private))
2885                 goto out_mntput;
2886
2887         ret = nfs_referral_loop_protect();
2888         if (ret != 0)
2889                 goto out_put_mnt_ns;
2890
2891         ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
2892                         export_path, LOOKUP_FOLLOW, nd);
2893
2894         nfs_referral_loop_unprotect();
2895         put_mnt_ns(ns_private);
2896
2897         if (ret != 0)
2898                 goto out_err;
2899
2900         s = nd->path.mnt->mnt_sb;
2901         atomic_inc(&s->s_active);
2902         dentry = dget(nd->path.dentry);
2903
2904         path_put(&nd->path);
2905         kfree(nd);
2906         down_write(&s->s_umount);
2907         return dentry;
2908 out_put_mnt_ns:
2909         put_mnt_ns(ns_private);
2910 out_mntput:
2911         mntput(root_mnt);
2912 out_err:
2913         kfree(nd);
2914         return ERR_PTR(ret);
2915 }
2916
2917 static struct dentry *nfs4_try_mount(int flags, const char *dev_name,
2918                          struct nfs_parsed_mount_data *data)
2919 {
2920         char *export_path;
2921         struct vfsmount *root_mnt;
2922         struct dentry *res;
2923
2924         dfprintk(MOUNT, "--> nfs4_try_mount()\n");
2925
2926         export_path = data->nfs_server.export_path;
2927         data->nfs_server.export_path = "/";
2928         root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2929                         data->nfs_server.hostname);
2930         data->nfs_server.export_path = export_path;
2931
2932         res = ERR_CAST(root_mnt);
2933         if (!IS_ERR(root_mnt))
2934                 res = nfs_follow_remote_path(root_mnt, export_path);
2935
2936         dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
2937                         IS_ERR(res) ? PTR_ERR(res) : 0,
2938                         IS_ERR(res) ? " [error]" : "");
2939         return res;
2940 }
2941
2942 /*
2943  * Get the superblock for an NFS4 mountpoint
2944  */
2945 static struct dentry *nfs4_mount(struct file_system_type *fs_type,
2946         int flags, const char *dev_name, void *raw_data)
2947 {
2948         struct nfs_parsed_mount_data *data;
2949         int error = -ENOMEM;
2950         struct dentry *res = ERR_PTR(-ENOMEM);
2951
2952         data = nfs_alloc_parsed_mount_data(4);
2953         if (data == NULL)
2954                 goto out_free_data;
2955
2956         /* Validate the mount data */
2957         error = nfs4_validate_mount_data(raw_data, data, dev_name);
2958         if (error < 0) {
2959                 res = ERR_PTR(error);
2960                 goto out;
2961         }
2962
2963         res = nfs4_try_mount(flags, dev_name, data);
2964         if (IS_ERR(res))
2965                 error = PTR_ERR(res);
2966
2967 out:
2968         kfree(data->client_address);
2969         kfree(data->nfs_server.export_path);
2970         kfree(data->nfs_server.hostname);
2971         kfree(data->fscache_uniq);
2972 out_free_data:
2973         kfree(data);
2974         dprintk("<-- nfs4_mount() = %d%s\n", error,
2975                         error != 0 ? " [error]" : "");
2976         return res;
2977 }
2978
2979 static void nfs4_kill_super(struct super_block *sb)
2980 {
2981         struct nfs_server *server = NFS_SB(sb);
2982
2983         dprintk("--> %s\n", __func__);
2984         nfs_super_return_all_delegations(sb);
2985         kill_anon_super(sb);
2986         nfs_fscache_release_super_cookie(sb);
2987         nfs_free_server(server);
2988         dprintk("<-- %s\n", __func__);
2989 }
2990
2991 /*
2992  * Clone an NFS4 server record on xdev traversal (FSID-change)
2993  */
2994 static struct dentry *
2995 nfs4_xdev_mount(struct file_system_type *fs_type, int flags,
2996                  const char *dev_name, void *raw_data)
2997 {
2998         struct nfs_clone_mount *data = raw_data;
2999         struct super_block *s;
3000         struct nfs_server *server;
3001         struct dentry *mntroot;
3002         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
3003         struct nfs_sb_mountdata sb_mntdata = {
3004                 .mntflags = flags,
3005         };
3006         int error;
3007
3008         dprintk("--> nfs4_xdev_mount()\n");
3009
3010         /* create a new volume representation */
3011         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
3012         if (IS_ERR(server)) {
3013                 error = PTR_ERR(server);
3014                 goto out_err_noserver;
3015         }
3016         sb_mntdata.server = server;
3017
3018         if (server->flags & NFS4_MOUNT_UNSHARED)
3019                 compare_super = NULL;
3020
3021         /* Get a superblock - note that we may end up sharing one that already exists */
3022         s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
3023         if (IS_ERR(s)) {
3024                 error = PTR_ERR(s);
3025                 goto out_err_nosb;
3026         }
3027
3028         if (s->s_fs_info != server) {
3029                 nfs_free_server(server);
3030                 server = NULL;
3031         } else {
3032                 error = nfs_bdi_register(server);
3033                 if (error)
3034                         goto error_splat_bdi;
3035         }
3036
3037         if (!s->s_root) {
3038                 /* initial superblock/root creation */
3039                 nfs4_clone_super(s, data->sb);
3040                 nfs_fscache_get_super_cookie(s, NULL, data);
3041         }
3042
3043         mntroot = nfs4_get_root(s, data->fh, dev_name);
3044         if (IS_ERR(mntroot)) {
3045                 error = PTR_ERR(mntroot);
3046                 goto error_splat_super;
3047         }
3048         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3049                 dput(mntroot);
3050                 error = -ESTALE;
3051                 goto error_splat_super;
3052         }
3053
3054         s->s_flags |= MS_ACTIVE;
3055
3056         security_sb_clone_mnt_opts(data->sb, s);
3057
3058         dprintk("<-- nfs4_xdev_mount() = 0\n");
3059         return mntroot;
3060
3061 out_err_nosb:
3062         nfs_free_server(server);
3063 out_err_noserver:
3064         dprintk("<-- nfs4_xdev_mount() = %d [error]\n", error);
3065         return ERR_PTR(error);
3066
3067 error_splat_super:
3068         if (server && !s->s_root)
3069                 bdi_unregister(&server->backing_dev_info);
3070 error_splat_bdi:
3071         deactivate_locked_super(s);
3072         dprintk("<-- nfs4_xdev_mount() = %d [splat]\n", error);
3073         return ERR_PTR(error);
3074 }
3075
3076 static struct dentry *
3077 nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
3078                            const char *dev_name, void *raw_data)
3079 {
3080         struct nfs_clone_mount *data = raw_data;
3081         struct super_block *s;
3082         struct nfs_server *server;
3083         struct dentry *mntroot;
3084         struct nfs_fh *mntfh;
3085         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
3086         struct nfs_sb_mountdata sb_mntdata = {
3087                 .mntflags = flags,
3088         };
3089         int error = -ENOMEM;
3090
3091         dprintk("--> nfs4_referral_get_sb()\n");
3092
3093         mntfh = nfs_alloc_fhandle();
3094         if (mntfh == NULL)
3095                 goto out_err_nofh;
3096
3097         /* create a new volume representation */
3098         server = nfs4_create_referral_server(data, mntfh);
3099         if (IS_ERR(server)) {
3100                 error = PTR_ERR(server);
3101                 goto out_err_noserver;
3102         }
3103         sb_mntdata.server = server;
3104
3105         if (server->flags & NFS4_MOUNT_UNSHARED)
3106                 compare_super = NULL;
3107
3108         /* Get a superblock - note that we may end up sharing one that already exists */
3109         s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
3110         if (IS_ERR(s)) {
3111                 error = PTR_ERR(s);
3112                 goto out_err_nosb;
3113         }
3114
3115         if (s->s_fs_info != server) {
3116                 nfs_free_server(server);
3117                 server = NULL;
3118         } else {
3119                 error = nfs_bdi_register(server);
3120                 if (error)
3121                         goto error_splat_bdi;
3122         }
3123
3124         if (!s->s_root) {
3125                 /* initial superblock/root creation */
3126                 nfs4_fill_super(s);
3127                 nfs_fscache_get_super_cookie(s, NULL, data);
3128         }
3129
3130         mntroot = nfs4_get_root(s, mntfh, dev_name);
3131         if (IS_ERR(mntroot)) {
3132                 error = PTR_ERR(mntroot);
3133                 goto error_splat_super;
3134         }
3135         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3136                 dput(mntroot);
3137                 error = -ESTALE;
3138                 goto error_splat_super;
3139         }
3140
3141         s->s_flags |= MS_ACTIVE;
3142
3143         security_sb_clone_mnt_opts(data->sb, s);
3144
3145         nfs_free_fhandle(mntfh);
3146         dprintk("<-- nfs4_referral_get_sb() = 0\n");
3147         return mntroot;
3148
3149 out_err_nosb:
3150         nfs_free_server(server);
3151 out_err_noserver:
3152         nfs_free_fhandle(mntfh);
3153 out_err_nofh:
3154         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
3155         return ERR_PTR(error);
3156
3157 error_splat_super:
3158         if (server && !s->s_root)
3159                 bdi_unregister(&server->backing_dev_info);
3160 error_splat_bdi:
3161         deactivate_locked_super(s);
3162         nfs_free_fhandle(mntfh);
3163         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
3164         return ERR_PTR(error);
3165 }
3166
3167 /*
3168  * Create an NFS4 server record on referral traversal
3169  */
3170 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
3171                 int flags, const char *dev_name, void *raw_data)
3172 {
3173         struct nfs_clone_mount *data = raw_data;
3174         char *export_path;
3175         struct vfsmount *root_mnt;
3176         struct dentry *res;
3177
3178         dprintk("--> nfs4_referral_mount()\n");
3179
3180         export_path = data->mnt_path;
3181         data->mnt_path = "/";
3182
3183         root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
3184                         flags, data, data->hostname);
3185         data->mnt_path = export_path;
3186
3187         res = ERR_CAST(root_mnt);
3188         if (!IS_ERR(root_mnt))
3189                 res = nfs_follow_remote_path(root_mnt, export_path);
3190         dprintk("<-- nfs4_referral_mount() = %ld%s\n",
3191                         IS_ERR(res) ? PTR_ERR(res) : 0,
3192                         IS_ERR(res) ? " [error]" : "");
3193         return res;
3194 }
3195
3196 #endif /* CONFIG_NFS_V4 */