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