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