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