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