]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/nfs/super.c
NFS: Clean up nfs_sb_active/nfs_sb_deactive
[linux-2.6-omap-h63xx.git] / fs / nfs / super.c
1 /*
2  *  linux/fs/nfs/super.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs superblock handling functions
7  *
8  *  Modularised by Alan Cox <Alan.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
95         /* Special mount options */
96         Opt_userspace, Opt_deprecated, Opt_sloppy,
97
98         Opt_err
99 };
100
101 static match_table_t nfs_mount_option_tokens = {
102         { Opt_userspace, "bg" },
103         { Opt_userspace, "fg" },
104         { Opt_userspace, "retry=%s" },
105
106         { Opt_sloppy, "sloppy" },
107
108         { Opt_soft, "soft" },
109         { Opt_hard, "hard" },
110         { Opt_deprecated, "intr" },
111         { Opt_deprecated, "nointr" },
112         { Opt_posix, "posix" },
113         { Opt_noposix, "noposix" },
114         { Opt_cto, "cto" },
115         { Opt_nocto, "nocto" },
116         { Opt_ac, "ac" },
117         { Opt_noac, "noac" },
118         { Opt_lock, "lock" },
119         { Opt_nolock, "nolock" },
120         { Opt_v2, "v2" },
121         { Opt_v3, "v3" },
122         { Opt_udp, "udp" },
123         { Opt_tcp, "tcp" },
124         { Opt_rdma, "rdma" },
125         { Opt_acl, "acl" },
126         { Opt_noacl, "noacl" },
127         { Opt_rdirplus, "rdirplus" },
128         { Opt_nordirplus, "nordirplus" },
129         { Opt_sharecache, "sharecache" },
130         { Opt_nosharecache, "nosharecache" },
131
132         { Opt_port, "port=%u" },
133         { Opt_rsize, "rsize=%u" },
134         { Opt_wsize, "wsize=%u" },
135         { Opt_bsize, "bsize=%u" },
136         { Opt_timeo, "timeo=%u" },
137         { Opt_retrans, "retrans=%u" },
138         { Opt_acregmin, "acregmin=%u" },
139         { Opt_acregmax, "acregmax=%u" },
140         { Opt_acdirmin, "acdirmin=%u" },
141         { Opt_acdirmax, "acdirmax=%u" },
142         { Opt_actimeo, "actimeo=%u" },
143         { Opt_namelen, "namlen=%u" },
144         { Opt_mountport, "mountport=%u" },
145         { Opt_mountvers, "mountvers=%u" },
146         { Opt_nfsvers, "nfsvers=%u" },
147         { Opt_nfsvers, "vers=%u" },
148
149         { Opt_sec, "sec=%s" },
150         { Opt_proto, "proto=%s" },
151         { Opt_mountproto, "mountproto=%s" },
152         { Opt_addr, "addr=%s" },
153         { Opt_clientaddr, "clientaddr=%s" },
154         { Opt_mounthost, "mounthost=%s" },
155         { Opt_mountaddr, "mountaddr=%s" },
156
157         { Opt_err, NULL }
158 };
159
160 enum {
161         Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
162
163         Opt_xprt_err
164 };
165
166 static match_table_t nfs_xprt_protocol_tokens = {
167         { Opt_xprt_udp, "udp" },
168         { Opt_xprt_tcp, "tcp" },
169         { Opt_xprt_rdma, "rdma" },
170
171         { Opt_xprt_err, NULL }
172 };
173
174 enum {
175         Opt_sec_none, Opt_sec_sys,
176         Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
177         Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
178         Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
179
180         Opt_sec_err
181 };
182
183 static match_table_t nfs_secflavor_tokens = {
184         { Opt_sec_none, "none" },
185         { Opt_sec_none, "null" },
186         { Opt_sec_sys, "sys" },
187
188         { Opt_sec_krb5, "krb5" },
189         { Opt_sec_krb5i, "krb5i" },
190         { Opt_sec_krb5p, "krb5p" },
191
192         { Opt_sec_lkey, "lkey" },
193         { Opt_sec_lkeyi, "lkeyi" },
194         { Opt_sec_lkeyp, "lkeyp" },
195
196         { Opt_sec_spkm, "spkm3" },
197         { Opt_sec_spkmi, "spkm3i" },
198         { Opt_sec_spkmp, "spkm3p" },
199
200         { Opt_sec_err, NULL }
201 };
202
203
204 static void nfs_umount_begin(struct super_block *);
205 static int  nfs_statfs(struct dentry *, struct kstatfs *);
206 static int  nfs_show_options(struct seq_file *, struct vfsmount *);
207 static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
208 static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
209 static int nfs_xdev_get_sb(struct file_system_type *fs_type,
210                 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
211 static void nfs_kill_super(struct super_block *);
212 static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
213
214 static struct file_system_type nfs_fs_type = {
215         .owner          = THIS_MODULE,
216         .name           = "nfs",
217         .get_sb         = nfs_get_sb,
218         .kill_sb        = nfs_kill_super,
219         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
220 };
221
222 struct file_system_type nfs_xdev_fs_type = {
223         .owner          = THIS_MODULE,
224         .name           = "nfs",
225         .get_sb         = nfs_xdev_get_sb,
226         .kill_sb        = nfs_kill_super,
227         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
228 };
229
230 static const struct super_operations nfs_sops = {
231         .alloc_inode    = nfs_alloc_inode,
232         .destroy_inode  = nfs_destroy_inode,
233         .write_inode    = nfs_write_inode,
234         .statfs         = nfs_statfs,
235         .clear_inode    = nfs_clear_inode,
236         .umount_begin   = nfs_umount_begin,
237         .show_options   = nfs_show_options,
238         .show_stats     = nfs_show_stats,
239         .remount_fs     = nfs_remount,
240 };
241
242 #ifdef CONFIG_NFS_V4
243 static int nfs4_get_sb(struct file_system_type *fs_type,
244         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
245 static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
246         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
247 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
248         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
249 static void nfs4_kill_super(struct super_block *sb);
250
251 static struct file_system_type nfs4_fs_type = {
252         .owner          = THIS_MODULE,
253         .name           = "nfs4",
254         .get_sb         = nfs4_get_sb,
255         .kill_sb        = nfs4_kill_super,
256         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
257 };
258
259 struct file_system_type nfs4_xdev_fs_type = {
260         .owner          = THIS_MODULE,
261         .name           = "nfs4",
262         .get_sb         = nfs4_xdev_get_sb,
263         .kill_sb        = nfs4_kill_super,
264         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
265 };
266
267 struct file_system_type nfs4_referral_fs_type = {
268         .owner          = THIS_MODULE,
269         .name           = "nfs4",
270         .get_sb         = nfs4_referral_get_sb,
271         .kill_sb        = nfs4_kill_super,
272         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
273 };
274
275 static const struct super_operations nfs4_sops = {
276         .alloc_inode    = nfs_alloc_inode,
277         .destroy_inode  = nfs_destroy_inode,
278         .write_inode    = nfs_write_inode,
279         .statfs         = nfs_statfs,
280         .clear_inode    = nfs4_clear_inode,
281         .umount_begin   = nfs_umount_begin,
282         .show_options   = nfs_show_options,
283         .show_stats     = nfs_show_stats,
284         .remount_fs     = nfs_remount,
285 };
286 #endif
287
288 static struct shrinker acl_shrinker = {
289         .shrink         = nfs_access_cache_shrinker,
290         .seeks          = DEFAULT_SEEKS,
291 };
292
293 /*
294  * Register the NFS filesystems
295  */
296 int __init register_nfs_fs(void)
297 {
298         int ret;
299
300         ret = register_filesystem(&nfs_fs_type);
301         if (ret < 0)
302                 goto error_0;
303
304         ret = nfs_register_sysctl();
305         if (ret < 0)
306                 goto error_1;
307 #ifdef CONFIG_NFS_V4
308         ret = register_filesystem(&nfs4_fs_type);
309         if (ret < 0)
310                 goto error_2;
311 #endif
312         register_shrinker(&acl_shrinker);
313         return 0;
314
315 #ifdef CONFIG_NFS_V4
316 error_2:
317         nfs_unregister_sysctl();
318 #endif
319 error_1:
320         unregister_filesystem(&nfs_fs_type);
321 error_0:
322         return ret;
323 }
324
325 /*
326  * Unregister the NFS filesystems
327  */
328 void __exit unregister_nfs_fs(void)
329 {
330         unregister_shrinker(&acl_shrinker);
331 #ifdef CONFIG_NFS_V4
332         unregister_filesystem(&nfs4_fs_type);
333 #endif
334         nfs_unregister_sysctl();
335         unregister_filesystem(&nfs_fs_type);
336 }
337
338 void nfs_sb_active(struct super_block *sb)
339 {
340         struct nfs_server *server = NFS_SB(sb);
341
342         if (atomic_inc_return(&server->active) == 1)
343                 atomic_inc(&sb->s_active);
344 }
345
346 void nfs_sb_deactive(struct super_block *sb)
347 {
348         struct nfs_server *server = NFS_SB(sb);
349
350         if (atomic_dec_and_test(&server->active))
351                 deactivate_super(sb);
352 }
353
354 /*
355  * Deliver file system statistics to userspace
356  */
357 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
358 {
359         struct nfs_server *server = NFS_SB(dentry->d_sb);
360         unsigned char blockbits;
361         unsigned long blockres;
362         struct nfs_fh *fh = NFS_FH(dentry->d_inode);
363         struct nfs_fattr fattr;
364         struct nfs_fsstat res = {
365                         .fattr = &fattr,
366         };
367         int error;
368
369         error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
370         if (error < 0)
371                 goto out_err;
372         buf->f_type = NFS_SUPER_MAGIC;
373
374         /*
375          * Current versions of glibc do not correctly handle the
376          * case where f_frsize != f_bsize.  Eventually we want to
377          * report the value of wtmult in this field.
378          */
379         buf->f_frsize = dentry->d_sb->s_blocksize;
380
381         /*
382          * On most *nix systems, f_blocks, f_bfree, and f_bavail
383          * are reported in units of f_frsize.  Linux hasn't had
384          * an f_frsize field in its statfs struct until recently,
385          * thus historically Linux's sys_statfs reports these
386          * fields in units of f_bsize.
387          */
388         buf->f_bsize = dentry->d_sb->s_blocksize;
389         blockbits = dentry->d_sb->s_blocksize_bits;
390         blockres = (1 << blockbits) - 1;
391         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
392         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
393         buf->f_bavail = (res.abytes + blockres) >> blockbits;
394
395         buf->f_files = res.tfiles;
396         buf->f_ffree = res.afiles;
397
398         buf->f_namelen = server->namelen;
399
400         return 0;
401
402  out_err:
403         dprintk("%s: statfs error = %d\n", __func__, -error);
404         return error;
405 }
406
407 /*
408  * Map the security flavour number to a name
409  */
410 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
411 {
412         static const struct {
413                 rpc_authflavor_t flavour;
414                 const char *str;
415         } sec_flavours[] = {
416                 { RPC_AUTH_NULL, "null" },
417                 { RPC_AUTH_UNIX, "sys" },
418                 { RPC_AUTH_GSS_KRB5, "krb5" },
419                 { RPC_AUTH_GSS_KRB5I, "krb5i" },
420                 { RPC_AUTH_GSS_KRB5P, "krb5p" },
421                 { RPC_AUTH_GSS_LKEY, "lkey" },
422                 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
423                 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
424                 { RPC_AUTH_GSS_SPKM, "spkm" },
425                 { RPC_AUTH_GSS_SPKMI, "spkmi" },
426                 { RPC_AUTH_GSS_SPKMP, "spkmp" },
427                 { UINT_MAX, "unknown" }
428         };
429         int i;
430
431         for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
432                 if (sec_flavours[i].flavour == flavour)
433                         break;
434         }
435         return sec_flavours[i].str;
436 }
437
438 static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
439                                     int showdefaults)
440 {
441         struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
442
443         switch (sap->sa_family) {
444         case AF_INET: {
445                 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
446                 seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
447                                 NIPQUAD(sin->sin_addr.s_addr));
448                 break;
449         }
450         case AF_INET6: {
451                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
452                 seq_printf(m, ",mountaddr=" NIP6_FMT,
453                                 NIP6(sin6->sin6_addr));
454                 break;
455         }
456         default:
457                 if (showdefaults)
458                         seq_printf(m, ",mountaddr=unspecified");
459         }
460
461         if (nfss->mountd_version || showdefaults)
462                 seq_printf(m, ",mountvers=%u", nfss->mountd_version);
463         if (nfss->mountd_port || showdefaults)
464                 seq_printf(m, ",mountport=%u", nfss->mountd_port);
465
466         switch (nfss->mountd_protocol) {
467         case IPPROTO_UDP:
468                 seq_printf(m, ",mountproto=udp");
469                 break;
470         case IPPROTO_TCP:
471                 seq_printf(m, ",mountproto=tcp");
472                 break;
473         default:
474                 if (showdefaults)
475                         seq_printf(m, ",mountproto=auto");
476         }
477 }
478
479 /*
480  * Describe the mount options in force on this server representation
481  */
482 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
483                                    int showdefaults)
484 {
485         static const struct proc_nfs_info {
486                 int flag;
487                 const char *str;
488                 const char *nostr;
489         } nfs_info[] = {
490                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
491                 { NFS_MOUNT_INTR, ",intr", ",nointr" },
492                 { NFS_MOUNT_POSIX, ",posix", "" },
493                 { NFS_MOUNT_NOCTO, ",nocto", "" },
494                 { NFS_MOUNT_NOAC, ",noac", "" },
495                 { NFS_MOUNT_NONLM, ",nolock", "" },
496                 { NFS_MOUNT_NOACL, ",noacl", "" },
497                 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
498                 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
499                 { 0, NULL, NULL }
500         };
501         const struct proc_nfs_info *nfs_infop;
502         struct nfs_client *clp = nfss->nfs_client;
503         u32 version = clp->rpc_ops->version;
504
505         seq_printf(m, ",vers=%u", version);
506         seq_printf(m, ",rsize=%u", nfss->rsize);
507         seq_printf(m, ",wsize=%u", nfss->wsize);
508         if (nfss->bsize != 0)
509                 seq_printf(m, ",bsize=%u", nfss->bsize);
510         seq_printf(m, ",namlen=%u", nfss->namelen);
511         if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
512                 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
513         if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
514                 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
515         if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
516                 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
517         if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
518                 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
519         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
520                 if (nfss->flags & nfs_infop->flag)
521                         seq_puts(m, nfs_infop->str);
522                 else
523                         seq_puts(m, nfs_infop->nostr);
524         }
525         seq_printf(m, ",proto=%s",
526                    rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
527         if (version == 4) {
528                 if (nfss->port != NFS_PORT)
529                         seq_printf(m, ",port=%u", nfss->port);
530         } else
531                 if (nfss->port)
532                         seq_printf(m, ",port=%u", nfss->port);
533
534         seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
535         seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
536         seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
537
538         if (version != 4)
539                 nfs_show_mountd_options(m, nfss, showdefaults);
540
541 #ifdef CONFIG_NFS_V4
542         if (clp->rpc_ops->version == 4)
543                 seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
544 #endif
545 }
546
547 /*
548  * Describe the mount options on this VFS mountpoint
549  */
550 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
551 {
552         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
553
554         nfs_show_mount_options(m, nfss, 0);
555
556         seq_printf(m, ",addr=%s",
557                         rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
558                                                         RPC_DISPLAY_ADDR));
559
560         return 0;
561 }
562
563 /*
564  * Present statistical information for this VFS mountpoint
565  */
566 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
567 {
568         int i, cpu;
569         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
570         struct rpc_auth *auth = nfss->client->cl_auth;
571         struct nfs_iostats totals = { };
572
573         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
574
575         /*
576          * Display all mount option settings
577          */
578         seq_printf(m, "\n\topts:\t");
579         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
580         seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
581         seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
582         seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
583         nfs_show_mount_options(m, nfss, 1);
584
585         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
586
587         seq_printf(m, "\n\tcaps:\t");
588         seq_printf(m, "caps=0x%x", nfss->caps);
589         seq_printf(m, ",wtmult=%u", nfss->wtmult);
590         seq_printf(m, ",dtsize=%u", nfss->dtsize);
591         seq_printf(m, ",bsize=%u", nfss->bsize);
592         seq_printf(m, ",namlen=%u", nfss->namelen);
593
594 #ifdef CONFIG_NFS_V4
595         if (nfss->nfs_client->rpc_ops->version == 4) {
596                 seq_printf(m, "\n\tnfsv4:\t");
597                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
598                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
599                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
600         }
601 #endif
602
603         /*
604          * Display security flavor in effect for this mount
605          */
606         seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
607         if (auth->au_flavor)
608                 seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
609
610         /*
611          * Display superblock I/O counters
612          */
613         for_each_possible_cpu(cpu) {
614                 struct nfs_iostats *stats;
615
616                 preempt_disable();
617                 stats = per_cpu_ptr(nfss->io_stats, cpu);
618
619                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
620                         totals.events[i] += stats->events[i];
621                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
622                         totals.bytes[i] += stats->bytes[i];
623
624                 preempt_enable();
625         }
626
627         seq_printf(m, "\n\tevents:\t");
628         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
629                 seq_printf(m, "%lu ", totals.events[i]);
630         seq_printf(m, "\n\tbytes:\t");
631         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
632                 seq_printf(m, "%Lu ", totals.bytes[i]);
633         seq_printf(m, "\n");
634
635         rpc_print_iostats(m, nfss->client);
636
637         return 0;
638 }
639
640 /*
641  * Begin unmount by attempting to remove all automounted mountpoints we added
642  * in response to xdev traversals and referrals
643  */
644 static void nfs_umount_begin(struct super_block *sb)
645 {
646         struct nfs_server *server = NFS_SB(sb);
647         struct rpc_clnt *rpc;
648
649         /* -EIO all pending I/O */
650         rpc = server->client_acl;
651         if (!IS_ERR(rpc))
652                 rpc_killall_tasks(rpc);
653         rpc = server->client;
654         if (!IS_ERR(rpc))
655                 rpc_killall_tasks(rpc);
656 }
657
658 /*
659  * Set the port number in an address.  Be agnostic about the address family.
660  */
661 static void nfs_set_port(struct sockaddr *sap, unsigned short port)
662 {
663         switch (sap->sa_family) {
664         case AF_INET: {
665                 struct sockaddr_in *ap = (struct sockaddr_in *)sap;
666                 ap->sin_port = htons(port);
667                 break;
668         }
669         case AF_INET6: {
670                 struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
671                 ap->sin6_port = htons(port);
672                 break;
673         }
674         }
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
1254                 /*
1255                  * Special options
1256                  */
1257                 case Opt_sloppy:
1258                         sloppy = 1;
1259                         dfprintk(MOUNT, "NFS:   relaxing parsing rules\n");
1260                         break;
1261                 case Opt_userspace:
1262                 case Opt_deprecated:
1263                         dfprintk(MOUNT, "NFS:   ignoring mount option "
1264                                         "'%s'\n", p);
1265                         break;
1266
1267                 default:
1268                         errors++;
1269                         dfprintk(MOUNT, "NFS:   unrecognized mount option "
1270                                         "'%s'\n", p);
1271                 }
1272         }
1273
1274         if (errors > 0) {
1275                 dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
1276                                 errors, (errors == 1 ? "" : "s"));
1277                 if (!sloppy)
1278                         return 0;
1279         }
1280         return 1;
1281
1282 out_nomem:
1283         printk(KERN_INFO "NFS: not enough memory to parse option\n");
1284         return 0;
1285 out_security_failure:
1286         free_secdata(secdata);
1287         printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1288         return 0;
1289 }
1290
1291 /*
1292  * Use the remote server's MOUNT service to request the NFS file handle
1293  * corresponding to the provided path.
1294  */
1295 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1296                          struct nfs_fh *root_fh)
1297 {
1298         struct sockaddr *sap = (struct sockaddr *)&args->mount_server.address;
1299         char *hostname;
1300         int status;
1301
1302         if (args->mount_server.version == 0) {
1303                 if (args->flags & NFS_MOUNT_VER3)
1304                         args->mount_server.version = NFS_MNT3_VERSION;
1305                 else
1306                         args->mount_server.version = NFS_MNT_VERSION;
1307         }
1308
1309         if (args->mount_server.hostname)
1310                 hostname = args->mount_server.hostname;
1311         else
1312                 hostname = args->nfs_server.hostname;
1313
1314         /*
1315          * Construct the mount server's address.
1316          */
1317         if (args->mount_server.address.ss_family == AF_UNSPEC) {
1318                 memcpy(sap, &args->nfs_server.address,
1319                        args->nfs_server.addrlen);
1320                 args->mount_server.addrlen = args->nfs_server.addrlen;
1321         }
1322
1323         /*
1324          * autobind will be used if mount_server.port == 0
1325          */
1326         nfs_set_port(sap, args->mount_server.port);
1327
1328         /*
1329          * Now ask the mount server to map our export path
1330          * to a file handle.
1331          */
1332         status = nfs_mount(sap,
1333                            args->mount_server.addrlen,
1334                            hostname,
1335                            args->nfs_server.export_path,
1336                            args->mount_server.version,
1337                            args->mount_server.protocol,
1338                            root_fh);
1339         if (status == 0)
1340                 return 0;
1341
1342         dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1343                         hostname, status);
1344         return status;
1345 }
1346
1347 static int nfs_parse_simple_hostname(const char *dev_name,
1348                                      char **hostname, size_t maxnamlen,
1349                                      char **export_path, size_t maxpathlen)
1350 {
1351         size_t len;
1352         char *colon, *comma;
1353
1354         colon = strchr(dev_name, ':');
1355         if (colon == NULL)
1356                 goto out_bad_devname;
1357
1358         len = colon - dev_name;
1359         if (len > maxnamlen)
1360                 goto out_hostname;
1361
1362         /* N.B. caller will free nfs_server.hostname in all cases */
1363         *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1364         if (!*hostname)
1365                 goto out_nomem;
1366
1367         /* kill possible hostname list: not supported */
1368         comma = strchr(*hostname, ',');
1369         if (comma != NULL) {
1370                 if (comma == *hostname)
1371                         goto out_bad_devname;
1372                 *comma = '\0';
1373         }
1374
1375         colon++;
1376         len = strlen(colon);
1377         if (len > maxpathlen)
1378                 goto out_path;
1379         *export_path = kstrndup(colon, len, GFP_KERNEL);
1380         if (!*export_path)
1381                 goto out_nomem;
1382
1383         dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1384         return 0;
1385
1386 out_bad_devname:
1387         dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1388         return -EINVAL;
1389
1390 out_nomem:
1391         dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1392         return -ENOMEM;
1393
1394 out_hostname:
1395         dfprintk(MOUNT, "NFS: server hostname too long\n");
1396         return -ENAMETOOLONG;
1397
1398 out_path:
1399         dfprintk(MOUNT, "NFS: export pathname too long\n");
1400         return -ENAMETOOLONG;
1401 }
1402
1403 /*
1404  * Hostname has square brackets around it because it contains one or
1405  * more colons.  We look for the first closing square bracket, and a
1406  * colon must follow it.
1407  */
1408 static int nfs_parse_protected_hostname(const char *dev_name,
1409                                         char **hostname, size_t maxnamlen,
1410                                         char **export_path, size_t maxpathlen)
1411 {
1412         size_t len;
1413         char *start, *end;
1414
1415         start = (char *)(dev_name + 1);
1416
1417         end = strchr(start, ']');
1418         if (end == NULL)
1419                 goto out_bad_devname;
1420         if (*(end + 1) != ':')
1421                 goto out_bad_devname;
1422
1423         len = end - start;
1424         if (len > maxnamlen)
1425                 goto out_hostname;
1426
1427         /* N.B. caller will free nfs_server.hostname in all cases */
1428         *hostname = kstrndup(start, len, GFP_KERNEL);
1429         if (*hostname == NULL)
1430                 goto out_nomem;
1431
1432         end += 2;
1433         len = strlen(end);
1434         if (len > maxpathlen)
1435                 goto out_path;
1436         *export_path = kstrndup(end, len, GFP_KERNEL);
1437         if (!*export_path)
1438                 goto out_nomem;
1439
1440         return 0;
1441
1442 out_bad_devname:
1443         dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1444         return -EINVAL;
1445
1446 out_nomem:
1447         dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1448         return -ENOMEM;
1449
1450 out_hostname:
1451         dfprintk(MOUNT, "NFS: server hostname too long\n");
1452         return -ENAMETOOLONG;
1453
1454 out_path:
1455         dfprintk(MOUNT, "NFS: export pathname too long\n");
1456         return -ENAMETOOLONG;
1457 }
1458
1459 /*
1460  * Split "dev_name" into "hostname:export_path".
1461  *
1462  * The leftmost colon demarks the split between the server's hostname
1463  * and the export path.  If the hostname starts with a left square
1464  * bracket, then it may contain colons.
1465  *
1466  * Note: caller frees hostname and export path, even on error.
1467  */
1468 static int nfs_parse_devname(const char *dev_name,
1469                              char **hostname, size_t maxnamlen,
1470                              char **export_path, size_t maxpathlen)
1471 {
1472         if (*dev_name == '[')
1473                 return nfs_parse_protected_hostname(dev_name,
1474                                                     hostname, maxnamlen,
1475                                                     export_path, maxpathlen);
1476
1477         return nfs_parse_simple_hostname(dev_name,
1478                                          hostname, maxnamlen,
1479                                          export_path, maxpathlen);
1480 }
1481
1482 /*
1483  * Validate the NFS2/NFS3 mount data
1484  * - fills in the mount root filehandle
1485  *
1486  * For option strings, user space handles the following behaviors:
1487  *
1488  * + DNS: mapping server host name to IP address ("addr=" option)
1489  *
1490  * + failure mode: how to behave if a mount request can't be handled
1491  *   immediately ("fg/bg" option)
1492  *
1493  * + retry: how often to retry a mount request ("retry=" option)
1494  *
1495  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1496  *   mountproto=tcp after mountproto=udp, and so on
1497  */
1498 static int nfs_validate_mount_data(void *options,
1499                                    struct nfs_parsed_mount_data *args,
1500                                    struct nfs_fh *mntfh,
1501                                    const char *dev_name)
1502 {
1503         struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1504
1505         if (data == NULL)
1506                 goto out_no_data;
1507
1508         args->flags             = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1509         args->rsize             = NFS_MAX_FILE_IO_SIZE;
1510         args->wsize             = NFS_MAX_FILE_IO_SIZE;
1511         args->acregmin          = NFS_DEF_ACREGMIN;
1512         args->acregmax          = NFS_DEF_ACREGMAX;
1513         args->acdirmin          = NFS_DEF_ACDIRMIN;
1514         args->acdirmax          = NFS_DEF_ACDIRMAX;
1515         args->mount_server.port = 0;    /* autobind unless user sets port */
1516         args->nfs_server.port   = 0;    /* autobind unless user sets port */
1517         args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1518         args->auth_flavors[0]   = RPC_AUTH_UNIX;
1519
1520         switch (data->version) {
1521         case 1:
1522                 data->namlen = 0;
1523         case 2:
1524                 data->bsize = 0;
1525         case 3:
1526                 if (data->flags & NFS_MOUNT_VER3)
1527                         goto out_no_v3;
1528                 data->root.size = NFS2_FHSIZE;
1529                 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1530         case 4:
1531                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1532                         goto out_no_sec;
1533         case 5:
1534                 memset(data->context, 0, sizeof(data->context));
1535         case 6:
1536                 if (data->flags & NFS_MOUNT_VER3) {
1537                         if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1538                                 goto out_invalid_fh;
1539                         mntfh->size = data->root.size;
1540                 } else
1541                         mntfh->size = NFS2_FHSIZE;
1542
1543
1544                 memcpy(mntfh->data, data->root.data, mntfh->size);
1545                 if (mntfh->size < sizeof(mntfh->data))
1546                         memset(mntfh->data + mntfh->size, 0,
1547                                sizeof(mntfh->data) - mntfh->size);
1548
1549                 /*
1550                  * Translate to nfs_parsed_mount_data, which nfs_fill_super
1551                  * can deal with.
1552                  */
1553                 args->flags             = data->flags;
1554                 args->rsize             = data->rsize;
1555                 args->wsize             = data->wsize;
1556                 args->timeo             = data->timeo;
1557                 args->retrans           = data->retrans;
1558                 args->acregmin          = data->acregmin;
1559                 args->acregmax          = data->acregmax;
1560                 args->acdirmin          = data->acdirmin;
1561                 args->acdirmax          = data->acdirmax;
1562
1563                 memcpy(&args->nfs_server.address, &data->addr,
1564                        sizeof(data->addr));
1565                 args->nfs_server.addrlen = sizeof(data->addr);
1566                 if (!nfs_verify_server_address((struct sockaddr *)
1567                                                 &args->nfs_server.address))
1568                         goto out_no_address;
1569
1570                 if (!(data->flags & NFS_MOUNT_TCP))
1571                         args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1572                 /* N.B. caller will free nfs_server.hostname in all cases */
1573                 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1574                 args->namlen            = data->namlen;
1575                 args->bsize             = data->bsize;
1576
1577                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1578                         args->auth_flavors[0] = data->pseudoflavor;
1579                 if (!args->nfs_server.hostname)
1580                         goto out_nomem;
1581
1582                 /*
1583                  * The legacy version 6 binary mount data from userspace has a
1584                  * field used only to transport selinux information into the
1585                  * the kernel.  To continue to support that functionality we
1586                  * have a touch of selinux knowledge here in the NFS code. The
1587                  * userspace code converted context=blah to just blah so we are
1588                  * converting back to the full string selinux understands.
1589                  */
1590                 if (data->context[0]){
1591 #ifdef CONFIG_SECURITY_SELINUX
1592                         int rc;
1593                         char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1594                         if (!opts_str)
1595                                 return -ENOMEM;
1596                         strcpy(opts_str, "context=");
1597                         data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1598                         strcat(opts_str, &data->context[0]);
1599                         rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1600                         kfree(opts_str);
1601                         if (rc)
1602                                 return rc;
1603 #else
1604                         return -EINVAL;
1605 #endif
1606                 }
1607
1608                 break;
1609         default: {
1610                 int status;
1611
1612                 if (nfs_parse_mount_options((char *)options, args) == 0)
1613                         return -EINVAL;
1614
1615                 if (!nfs_verify_server_address((struct sockaddr *)
1616                                                 &args->nfs_server.address))
1617                         goto out_no_address;
1618
1619                 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1620                                 args->nfs_server.port);
1621
1622                 nfs_set_mount_transport_protocol(args);
1623
1624                 status = nfs_parse_devname(dev_name,
1625                                            &args->nfs_server.hostname,
1626                                            PAGE_SIZE,
1627                                            &args->nfs_server.export_path,
1628                                            NFS_MAXPATHLEN);
1629                 if (!status)
1630                         status = nfs_try_mount(args, mntfh);
1631
1632                 kfree(args->nfs_server.export_path);
1633                 args->nfs_server.export_path = NULL;
1634
1635                 if (status)
1636                         return status;
1637
1638                 break;
1639                 }
1640         }
1641
1642 #ifndef CONFIG_NFS_V3
1643         if (args->flags & NFS_MOUNT_VER3)
1644                 goto out_v3_not_compiled;
1645 #endif /* !CONFIG_NFS_V3 */
1646
1647         return 0;
1648
1649 out_no_data:
1650         dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1651         return -EINVAL;
1652
1653 out_no_v3:
1654         dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1655                  data->version);
1656         return -EINVAL;
1657
1658 out_no_sec:
1659         dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1660         return -EINVAL;
1661
1662 #ifndef CONFIG_NFS_V3
1663 out_v3_not_compiled:
1664         dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1665         return -EPROTONOSUPPORT;
1666 #endif /* !CONFIG_NFS_V3 */
1667
1668 out_nomem:
1669         dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1670         return -ENOMEM;
1671
1672 out_no_address:
1673         dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1674         return -EINVAL;
1675
1676 out_invalid_fh:
1677         dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1678         return -EINVAL;
1679 }
1680
1681 static int
1682 nfs_compare_remount_data(struct nfs_server *nfss,
1683                          struct nfs_parsed_mount_data *data)
1684 {
1685         if (data->flags != nfss->flags ||
1686             data->rsize != nfss->rsize ||
1687             data->wsize != nfss->wsize ||
1688             data->retrans != nfss->client->cl_timeout->to_retries ||
1689             data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1690             data->acregmin != nfss->acregmin / HZ ||
1691             data->acregmax != nfss->acregmax / HZ ||
1692             data->acdirmin != nfss->acdirmin / HZ ||
1693             data->acdirmax != nfss->acdirmax / HZ ||
1694             data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1695             data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1696             memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1697                    data->nfs_server.addrlen) != 0)
1698                 return -EINVAL;
1699
1700         return 0;
1701 }
1702
1703 static int
1704 nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1705 {
1706         int error;
1707         struct nfs_server *nfss = sb->s_fs_info;
1708         struct nfs_parsed_mount_data *data;
1709         struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1710         struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
1711         u32 nfsvers = nfss->nfs_client->rpc_ops->version;
1712
1713         /*
1714          * Userspace mount programs that send binary options generally send
1715          * them populated with default values. We have no way to know which
1716          * ones were explicitly specified. Fall back to legacy behavior and
1717          * just return success.
1718          */
1719         if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1720             (nfsvers <= 3 && (!options || (options->version >= 1 &&
1721                                            options->version <= 6))))
1722                 return 0;
1723
1724         data = kzalloc(sizeof(*data), GFP_KERNEL);
1725         if (data == NULL)
1726                 return -ENOMEM;
1727
1728         /* fill out struct with values from existing mount */
1729         data->flags = nfss->flags;
1730         data->rsize = nfss->rsize;
1731         data->wsize = nfss->wsize;
1732         data->retrans = nfss->client->cl_timeout->to_retries;
1733         data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1734         data->acregmin = nfss->acregmin / HZ;
1735         data->acregmax = nfss->acregmax / HZ;
1736         data->acdirmin = nfss->acdirmin / HZ;
1737         data->acdirmax = nfss->acdirmax / HZ;
1738         data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1739         data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1740         memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1741                 data->nfs_server.addrlen);
1742
1743         /* overwrite those values with any that were specified */
1744         error = nfs_parse_mount_options((char *)options, data);
1745         if (error < 0)
1746                 goto out;
1747
1748         /* compare new mount options with old ones */
1749         error = nfs_compare_remount_data(nfss, data);
1750 out:
1751         kfree(data);
1752         return error;
1753 }
1754
1755 /*
1756  * Initialise the common bits of the superblock
1757  */
1758 static inline void nfs_initialise_sb(struct super_block *sb)
1759 {
1760         struct nfs_server *server = NFS_SB(sb);
1761
1762         sb->s_magic = NFS_SUPER_MAGIC;
1763
1764         /* We probably want something more informative here */
1765         snprintf(sb->s_id, sizeof(sb->s_id),
1766                  "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1767
1768         if (sb->s_blocksize == 0)
1769                 sb->s_blocksize = nfs_block_bits(server->wsize,
1770                                                  &sb->s_blocksize_bits);
1771
1772         if (server->flags & NFS_MOUNT_NOAC)
1773                 sb->s_flags |= MS_SYNCHRONOUS;
1774
1775         nfs_super_set_maxbytes(sb, server->maxfilesize);
1776 }
1777
1778 /*
1779  * Finish setting up an NFS2/3 superblock
1780  */
1781 static void nfs_fill_super(struct super_block *sb,
1782                            struct nfs_parsed_mount_data *data)
1783 {
1784         struct nfs_server *server = NFS_SB(sb);
1785
1786         sb->s_blocksize_bits = 0;
1787         sb->s_blocksize = 0;
1788         if (data->bsize)
1789                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1790
1791         if (server->flags & NFS_MOUNT_VER3) {
1792                 /* The VFS shouldn't apply the umask to mode bits. We will do
1793                  * so ourselves when necessary.
1794                  */
1795                 sb->s_flags |= MS_POSIXACL;
1796                 sb->s_time_gran = 1;
1797         }
1798
1799         sb->s_op = &nfs_sops;
1800         nfs_initialise_sb(sb);
1801 }
1802
1803 /*
1804  * Finish setting up a cloned NFS2/3 superblock
1805  */
1806 static void nfs_clone_super(struct super_block *sb,
1807                             const struct super_block *old_sb)
1808 {
1809         struct nfs_server *server = NFS_SB(sb);
1810
1811         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1812         sb->s_blocksize = old_sb->s_blocksize;
1813         sb->s_maxbytes = old_sb->s_maxbytes;
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 = old_sb->s_op;
1824         nfs_initialise_sb(sb);
1825 }
1826
1827 #define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1828
1829 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1830 {
1831         const struct nfs_server *a = s->s_fs_info;
1832         const struct rpc_clnt *clnt_a = a->client;
1833         const struct rpc_clnt *clnt_b = b->client;
1834
1835         if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1836                 goto Ebusy;
1837         if (a->nfs_client != b->nfs_client)
1838                 goto Ebusy;
1839         if (a->flags != b->flags)
1840                 goto Ebusy;
1841         if (a->wsize != b->wsize)
1842                 goto Ebusy;
1843         if (a->rsize != b->rsize)
1844                 goto Ebusy;
1845         if (a->acregmin != b->acregmin)
1846                 goto Ebusy;
1847         if (a->acregmax != b->acregmax)
1848                 goto Ebusy;
1849         if (a->acdirmin != b->acdirmin)
1850                 goto Ebusy;
1851         if (a->acdirmax != b->acdirmax)
1852                 goto Ebusy;
1853         if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1854                 goto Ebusy;
1855         return 1;
1856 Ebusy:
1857         return 0;
1858 }
1859
1860 struct nfs_sb_mountdata {
1861         struct nfs_server *server;
1862         int mntflags;
1863 };
1864
1865 static int nfs_set_super(struct super_block *s, void *data)
1866 {
1867         struct nfs_sb_mountdata *sb_mntdata = data;
1868         struct nfs_server *server = sb_mntdata->server;
1869         int ret;
1870
1871         s->s_flags = sb_mntdata->mntflags;
1872         s->s_fs_info = server;
1873         ret = set_anon_super(s, server);
1874         if (ret == 0)
1875                 server->s_dev = s->s_dev;
1876         return ret;
1877 }
1878
1879 static int nfs_compare_super_address(struct nfs_server *server1,
1880                                      struct nfs_server *server2)
1881 {
1882         struct sockaddr *sap1, *sap2;
1883
1884         sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
1885         sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
1886
1887         if (sap1->sa_family != sap2->sa_family)
1888                 return 0;
1889
1890         switch (sap1->sa_family) {
1891         case AF_INET: {
1892                 struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
1893                 struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
1894                 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
1895                         return 0;
1896                 if (sin1->sin_port != sin2->sin_port)
1897                         return 0;
1898                 break;
1899         }
1900         case AF_INET6: {
1901                 struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
1902                 struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
1903                 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
1904                         return 0;
1905                 if (sin1->sin6_port != sin2->sin6_port)
1906                         return 0;
1907                 break;
1908         }
1909         default:
1910                 return 0;
1911         }
1912
1913         return 1;
1914 }
1915
1916 static int nfs_compare_super(struct super_block *sb, void *data)
1917 {
1918         struct nfs_sb_mountdata *sb_mntdata = data;
1919         struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1920         int mntflags = sb_mntdata->mntflags;
1921
1922         if (!nfs_compare_super_address(old, server))
1923                 return 0;
1924         /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1925         if (old->flags & NFS_MOUNT_UNSHARED)
1926                 return 0;
1927         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1928                 return 0;
1929         return nfs_compare_mount_options(sb, server, mntflags);
1930 }
1931
1932 static int nfs_bdi_register(struct nfs_server *server)
1933 {
1934         return bdi_register_dev(&server->backing_dev_info, server->s_dev);
1935 }
1936
1937 static int nfs_get_sb(struct file_system_type *fs_type,
1938         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1939 {
1940         struct nfs_server *server = NULL;
1941         struct super_block *s;
1942         struct nfs_parsed_mount_data *data;
1943         struct nfs_fh *mntfh;
1944         struct dentry *mntroot;
1945         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1946         struct nfs_sb_mountdata sb_mntdata = {
1947                 .mntflags = flags,
1948         };
1949         int error = -ENOMEM;
1950
1951         data = kzalloc(sizeof(*data), GFP_KERNEL);
1952         mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
1953         if (data == NULL || mntfh == NULL)
1954                 goto out_free_fh;
1955
1956         security_init_mnt_opts(&data->lsm_opts);
1957
1958         /* Validate the mount data */
1959         error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
1960         if (error < 0)
1961                 goto out;
1962
1963         /* Get a volume representation */
1964         server = nfs_create_server(data, mntfh);
1965         if (IS_ERR(server)) {
1966                 error = PTR_ERR(server);
1967                 goto out;
1968         }
1969         sb_mntdata.server = server;
1970
1971         if (server->flags & NFS_MOUNT_UNSHARED)
1972                 compare_super = NULL;
1973
1974         /* Get a superblock - note that we may end up sharing one that already exists */
1975         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
1976         if (IS_ERR(s)) {
1977                 error = PTR_ERR(s);
1978                 goto out_err_nosb;
1979         }
1980
1981         if (s->s_fs_info != server) {
1982                 nfs_free_server(server);
1983                 server = NULL;
1984         } else {
1985                 error = nfs_bdi_register(server);
1986                 if (error)
1987                         goto error_splat_super;
1988         }
1989
1990         if (!s->s_root) {
1991                 /* initial superblock/root creation */
1992                 nfs_fill_super(s, data);
1993         }
1994
1995         mntroot = nfs_get_root(s, mntfh);
1996         if (IS_ERR(mntroot)) {
1997                 error = PTR_ERR(mntroot);
1998                 goto error_splat_super;
1999         }
2000
2001         error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2002         if (error)
2003                 goto error_splat_root;
2004
2005         s->s_flags |= MS_ACTIVE;
2006         mnt->mnt_sb = s;
2007         mnt->mnt_root = mntroot;
2008         error = 0;
2009
2010 out:
2011         kfree(data->nfs_server.hostname);
2012         kfree(data->mount_server.hostname);
2013         security_free_mnt_opts(&data->lsm_opts);
2014 out_free_fh:
2015         kfree(mntfh);
2016         kfree(data);
2017         return error;
2018
2019 out_err_nosb:
2020         nfs_free_server(server);
2021         goto out;
2022
2023 error_splat_root:
2024         dput(mntroot);
2025 error_splat_super:
2026         up_write(&s->s_umount);
2027         deactivate_super(s);
2028         goto out;
2029 }
2030
2031 /*
2032  * Destroy an NFS2/3 superblock
2033  */
2034 static void nfs_kill_super(struct super_block *s)
2035 {
2036         struct nfs_server *server = NFS_SB(s);
2037
2038         bdi_unregister(&server->backing_dev_info);
2039         kill_anon_super(s);
2040         nfs_free_server(server);
2041 }
2042
2043 /*
2044  * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2045  */
2046 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2047                            const char *dev_name, void *raw_data,
2048                            struct vfsmount *mnt)
2049 {
2050         struct nfs_clone_mount *data = raw_data;
2051         struct super_block *s;
2052         struct nfs_server *server;
2053         struct dentry *mntroot;
2054         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2055         struct nfs_sb_mountdata sb_mntdata = {
2056                 .mntflags = flags,
2057         };
2058         int error;
2059
2060         dprintk("--> nfs_xdev_get_sb()\n");
2061
2062         /* create a new volume representation */
2063         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2064         if (IS_ERR(server)) {
2065                 error = PTR_ERR(server);
2066                 goto out_err_noserver;
2067         }
2068         sb_mntdata.server = server;
2069
2070         if (server->flags & NFS_MOUNT_UNSHARED)
2071                 compare_super = NULL;
2072
2073         /* Get a superblock - note that we may end up sharing one that already exists */
2074         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2075         if (IS_ERR(s)) {
2076                 error = PTR_ERR(s);
2077                 goto out_err_nosb;
2078         }
2079
2080         if (s->s_fs_info != server) {
2081                 nfs_free_server(server);
2082                 server = NULL;
2083         } else {
2084                 error = nfs_bdi_register(server);
2085                 if (error)
2086                         goto error_splat_super;
2087         }
2088
2089         if (!s->s_root) {
2090                 /* initial superblock/root creation */
2091                 nfs_clone_super(s, data->sb);
2092         }
2093
2094         mntroot = nfs_get_root(s, data->fh);
2095         if (IS_ERR(mntroot)) {
2096                 error = PTR_ERR(mntroot);
2097                 goto error_splat_super;
2098         }
2099         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2100                 dput(mntroot);
2101                 error = -ESTALE;
2102                 goto error_splat_super;
2103         }
2104
2105         s->s_flags |= MS_ACTIVE;
2106         mnt->mnt_sb = s;
2107         mnt->mnt_root = mntroot;
2108
2109         /* clone any lsm security options from the parent to the new sb */
2110         security_sb_clone_mnt_opts(data->sb, s);
2111
2112         dprintk("<-- nfs_xdev_get_sb() = 0\n");
2113         return 0;
2114
2115 out_err_nosb:
2116         nfs_free_server(server);
2117 out_err_noserver:
2118         dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2119         return error;
2120
2121 error_splat_super:
2122         up_write(&s->s_umount);
2123         deactivate_super(s);
2124         dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2125         return error;
2126 }
2127
2128 #ifdef CONFIG_NFS_V4
2129
2130 /*
2131  * Finish setting up a cloned NFS4 superblock
2132  */
2133 static void nfs4_clone_super(struct super_block *sb,
2134                             const struct super_block *old_sb)
2135 {
2136         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2137         sb->s_blocksize = old_sb->s_blocksize;
2138         sb->s_maxbytes = old_sb->s_maxbytes;
2139         sb->s_time_gran = 1;
2140         sb->s_op = old_sb->s_op;
2141         nfs_initialise_sb(sb);
2142 }
2143
2144 /*
2145  * Set up an NFS4 superblock
2146  */
2147 static void nfs4_fill_super(struct super_block *sb)
2148 {
2149         sb->s_time_gran = 1;
2150         sb->s_op = &nfs4_sops;
2151         nfs_initialise_sb(sb);
2152 }
2153
2154 /*
2155  * Validate NFSv4 mount options
2156  */
2157 static int nfs4_validate_mount_data(void *options,
2158                                     struct nfs_parsed_mount_data *args,
2159                                     const char *dev_name)
2160 {
2161         struct sockaddr_in *ap;
2162         struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2163         char *c;
2164
2165         if (data == NULL)
2166                 goto out_no_data;
2167
2168         args->rsize             = NFS_MAX_FILE_IO_SIZE;
2169         args->wsize             = NFS_MAX_FILE_IO_SIZE;
2170         args->acregmin          = NFS_DEF_ACREGMIN;
2171         args->acregmax          = NFS_DEF_ACREGMAX;
2172         args->acdirmin          = NFS_DEF_ACDIRMIN;
2173         args->acdirmax          = NFS_DEF_ACDIRMAX;
2174         args->nfs_server.port   = NFS_PORT; /* 2049 unless user set port= */
2175         args->auth_flavors[0]   = RPC_AUTH_UNIX;
2176         args->auth_flavor_len   = 0;
2177
2178         switch (data->version) {
2179         case 1:
2180                 ap = (struct sockaddr_in *)&args->nfs_server.address;
2181                 if (data->host_addrlen > sizeof(args->nfs_server.address))
2182                         goto out_no_address;
2183                 if (data->host_addrlen == 0)
2184                         goto out_no_address;
2185                 args->nfs_server.addrlen = data->host_addrlen;
2186                 if (copy_from_user(ap, data->host_addr, data->host_addrlen))
2187                         return -EFAULT;
2188                 if (!nfs_verify_server_address((struct sockaddr *)
2189                                                 &args->nfs_server.address))
2190                         goto out_no_address;
2191
2192                 if (data->auth_flavourlen) {
2193                         if (data->auth_flavourlen > 1)
2194                                 goto out_inval_auth;
2195                         if (copy_from_user(&args->auth_flavors[0],
2196                                            data->auth_flavours,
2197                                            sizeof(args->auth_flavors[0])))
2198                                 return -EFAULT;
2199                 }
2200
2201                 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2202                 if (IS_ERR(c))
2203                         return PTR_ERR(c);
2204                 args->nfs_server.hostname = c;
2205
2206                 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2207                 if (IS_ERR(c))
2208                         return PTR_ERR(c);
2209                 args->nfs_server.export_path = c;
2210                 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2211
2212                 c = strndup_user(data->client_addr.data, 16);
2213                 if (IS_ERR(c))
2214                         return PTR_ERR(c);
2215                 args->client_address = c;
2216
2217                 /*
2218                  * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2219                  * can deal with.
2220                  */
2221
2222                 args->flags     = data->flags & NFS4_MOUNT_FLAGMASK;
2223                 args->rsize     = data->rsize;
2224                 args->wsize     = data->wsize;
2225                 args->timeo     = data->timeo;
2226                 args->retrans   = data->retrans;
2227                 args->acregmin  = data->acregmin;
2228                 args->acregmax  = data->acregmax;
2229                 args->acdirmin  = data->acdirmin;
2230                 args->acdirmax  = data->acdirmax;
2231                 args->nfs_server.protocol = data->proto;
2232                 nfs_validate_transport_protocol(args);
2233
2234                 break;
2235         default: {
2236                 int status;
2237
2238                 if (nfs_parse_mount_options((char *)options, args) == 0)
2239                         return -EINVAL;
2240
2241                 if (!nfs_verify_server_address((struct sockaddr *)
2242                                                 &args->nfs_server.address))
2243                         return -EINVAL;
2244
2245                 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
2246                                 args->nfs_server.port);
2247
2248                 nfs_validate_transport_protocol(args);
2249
2250                 if (args->auth_flavor_len > 1)
2251                         goto out_inval_auth;
2252
2253                 if (args->client_address == NULL)
2254                         goto out_no_client_address;
2255
2256                 status = nfs_parse_devname(dev_name,
2257                                            &args->nfs_server.hostname,
2258                                            NFS4_MAXNAMLEN,
2259                                            &args->nfs_server.export_path,
2260                                            NFS4_MAXPATHLEN);
2261                 if (status < 0)
2262                         return status;
2263
2264                 break;
2265                 }
2266         }
2267
2268         return 0;
2269
2270 out_no_data:
2271         dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2272         return -EINVAL;
2273
2274 out_inval_auth:
2275         dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2276                  data->auth_flavourlen);
2277         return -EINVAL;
2278
2279 out_no_address:
2280         dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2281         return -EINVAL;
2282
2283 out_no_client_address:
2284         dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
2285         return -EINVAL;
2286 }
2287
2288 /*
2289  * Get the superblock for an NFS4 mountpoint
2290  */
2291 static int nfs4_get_sb(struct file_system_type *fs_type,
2292         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2293 {
2294         struct nfs_parsed_mount_data *data;
2295         struct super_block *s;
2296         struct nfs_server *server;
2297         struct nfs_fh *mntfh;
2298         struct dentry *mntroot;
2299         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2300         struct nfs_sb_mountdata sb_mntdata = {
2301                 .mntflags = flags,
2302         };
2303         int error = -ENOMEM;
2304
2305         data = kzalloc(sizeof(*data), GFP_KERNEL);
2306         mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
2307         if (data == NULL || mntfh == NULL)
2308                 goto out_free_fh;
2309
2310         security_init_mnt_opts(&data->lsm_opts);
2311
2312         /* Validate the mount data */
2313         error = nfs4_validate_mount_data(raw_data, data, dev_name);
2314         if (error < 0)
2315                 goto out;
2316
2317         /* Get a volume representation */
2318         server = nfs4_create_server(data, mntfh);
2319         if (IS_ERR(server)) {
2320                 error = PTR_ERR(server);
2321                 goto out;
2322         }
2323         sb_mntdata.server = server;
2324
2325         if (server->flags & NFS4_MOUNT_UNSHARED)
2326                 compare_super = NULL;
2327
2328         /* Get a superblock - note that we may end up sharing one that already exists */
2329         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2330         if (IS_ERR(s)) {
2331                 error = PTR_ERR(s);
2332                 goto out_free;
2333         }
2334
2335         if (s->s_fs_info != server) {
2336                 nfs_free_server(server);
2337                 server = NULL;
2338         } else {
2339                 error = nfs_bdi_register(server);
2340                 if (error)
2341                         goto error_splat_super;
2342         }
2343
2344         if (!s->s_root) {
2345                 /* initial superblock/root creation */
2346                 nfs4_fill_super(s);
2347         }
2348
2349         mntroot = nfs4_get_root(s, mntfh);
2350         if (IS_ERR(mntroot)) {
2351                 error = PTR_ERR(mntroot);
2352                 goto error_splat_super;
2353         }
2354
2355         error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2356         if (error)
2357                 goto error_splat_root;
2358
2359         s->s_flags |= MS_ACTIVE;
2360         mnt->mnt_sb = s;
2361         mnt->mnt_root = mntroot;
2362         error = 0;
2363
2364 out:
2365         kfree(data->client_address);
2366         kfree(data->nfs_server.export_path);
2367         kfree(data->nfs_server.hostname);
2368         security_free_mnt_opts(&data->lsm_opts);
2369 out_free_fh:
2370         kfree(mntfh);
2371         kfree(data);
2372         return error;
2373
2374 out_free:
2375         nfs_free_server(server);
2376         goto out;
2377
2378 error_splat_root:
2379         dput(mntroot);
2380 error_splat_super:
2381         up_write(&s->s_umount);
2382         deactivate_super(s);
2383         goto out;
2384 }
2385
2386 static void nfs4_kill_super(struct super_block *sb)
2387 {
2388         struct nfs_server *server = NFS_SB(sb);
2389
2390         nfs_return_all_delegations(sb);
2391         kill_anon_super(sb);
2392
2393         nfs4_renewd_prepare_shutdown(server);
2394         nfs_free_server(server);
2395 }
2396
2397 /*
2398  * Clone an NFS4 server record on xdev traversal (FSID-change)
2399  */
2400 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2401                             const char *dev_name, void *raw_data,
2402                             struct vfsmount *mnt)
2403 {
2404         struct nfs_clone_mount *data = raw_data;
2405         struct super_block *s;
2406         struct nfs_server *server;
2407         struct dentry *mntroot;
2408         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2409         struct nfs_sb_mountdata sb_mntdata = {
2410                 .mntflags = flags,
2411         };
2412         int error;
2413
2414         dprintk("--> nfs4_xdev_get_sb()\n");
2415
2416         /* create a new volume representation */
2417         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2418         if (IS_ERR(server)) {
2419                 error = PTR_ERR(server);
2420                 goto out_err_noserver;
2421         }
2422         sb_mntdata.server = server;
2423
2424         if (server->flags & NFS4_MOUNT_UNSHARED)
2425                 compare_super = NULL;
2426
2427         /* Get a superblock - note that we may end up sharing one that already exists */
2428         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2429         if (IS_ERR(s)) {
2430                 error = PTR_ERR(s);
2431                 goto out_err_nosb;
2432         }
2433
2434         if (s->s_fs_info != server) {
2435                 nfs_free_server(server);
2436                 server = NULL;
2437         } else {
2438                 error = nfs_bdi_register(server);
2439                 if (error)
2440                         goto error_splat_super;
2441         }
2442
2443         if (!s->s_root) {
2444                 /* initial superblock/root creation */
2445                 nfs4_clone_super(s, data->sb);
2446         }
2447
2448         mntroot = nfs4_get_root(s, data->fh);
2449         if (IS_ERR(mntroot)) {
2450                 error = PTR_ERR(mntroot);
2451                 goto error_splat_super;
2452         }
2453         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2454                 dput(mntroot);
2455                 error = -ESTALE;
2456                 goto error_splat_super;
2457         }
2458
2459         s->s_flags |= MS_ACTIVE;
2460         mnt->mnt_sb = s;
2461         mnt->mnt_root = mntroot;
2462
2463         security_sb_clone_mnt_opts(data->sb, s);
2464
2465         dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2466         return 0;
2467
2468 out_err_nosb:
2469         nfs_free_server(server);
2470 out_err_noserver:
2471         dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2472         return error;
2473
2474 error_splat_super:
2475         up_write(&s->s_umount);
2476         deactivate_super(s);
2477         dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2478         return error;
2479 }
2480
2481 /*
2482  * Create an NFS4 server record on referral traversal
2483  */
2484 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
2485                                 const char *dev_name, void *raw_data,
2486                                 struct vfsmount *mnt)
2487 {
2488         struct nfs_clone_mount *data = raw_data;
2489         struct super_block *s;
2490         struct nfs_server *server;
2491         struct dentry *mntroot;
2492         struct nfs_fh mntfh;
2493         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2494         struct nfs_sb_mountdata sb_mntdata = {
2495                 .mntflags = flags,
2496         };
2497         int error;
2498
2499         dprintk("--> nfs4_referral_get_sb()\n");
2500
2501         /* create a new volume representation */
2502         server = nfs4_create_referral_server(data, &mntfh);
2503         if (IS_ERR(server)) {
2504                 error = PTR_ERR(server);
2505                 goto out_err_noserver;
2506         }
2507         sb_mntdata.server = server;
2508
2509         if (server->flags & NFS4_MOUNT_UNSHARED)
2510                 compare_super = NULL;
2511
2512         /* Get a superblock - note that we may end up sharing one that already exists */
2513         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2514         if (IS_ERR(s)) {
2515                 error = PTR_ERR(s);
2516                 goto out_err_nosb;
2517         }
2518
2519         if (s->s_fs_info != server) {
2520                 nfs_free_server(server);
2521                 server = NULL;
2522         } else {
2523                 error = nfs_bdi_register(server);
2524                 if (error)
2525                         goto error_splat_super;
2526         }
2527
2528         if (!s->s_root) {
2529                 /* initial superblock/root creation */
2530                 nfs4_fill_super(s);
2531         }
2532
2533         mntroot = nfs4_get_root(s, &mntfh);
2534         if (IS_ERR(mntroot)) {
2535                 error = PTR_ERR(mntroot);
2536                 goto error_splat_super;
2537         }
2538         if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2539                 dput(mntroot);
2540                 error = -ESTALE;
2541                 goto error_splat_super;
2542         }
2543
2544         s->s_flags |= MS_ACTIVE;
2545         mnt->mnt_sb = s;
2546         mnt->mnt_root = mntroot;
2547
2548         security_sb_clone_mnt_opts(data->sb, s);
2549
2550         dprintk("<-- nfs4_referral_get_sb() = 0\n");
2551         return 0;
2552
2553 out_err_nosb:
2554         nfs_free_server(server);
2555 out_err_noserver:
2556         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
2557         return error;
2558
2559 error_splat_super:
2560         up_write(&s->s_umount);
2561         deactivate_super(s);
2562         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
2563         return error;
2564 }
2565
2566 #endif /* CONFIG_NFS_V4 */