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