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