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