NFS: Error when mounting the same filesystem with different options
[linux-2.6] / fs / nfs / super.c
1 /*
2  *  linux/fs/nfs/super.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs superblock handling functions
7  *
8  *  Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
10  *
11  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12  *  J.S.Peatfield@damtp.cam.ac.uk
13  *
14  *  Split from inode.c by David Howells <dhowells@redhat.com>
15  *
16  * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
17  *   particular server are held in the same superblock
18  * - NFS superblocks can have several effective roots to the dentry tree
19  * - directory type roots are spliced into the tree when a path from one root reaches the root
20  *   of another (see nfs_lookup())
21  */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25
26 #include <linux/time.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/errno.h>
32 #include <linux/unistd.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/stats.h>
35 #include <linux/sunrpc/metrics.h>
36 #include <linux/nfs_fs.h>
37 #include <linux/nfs_mount.h>
38 #include <linux/nfs4_mount.h>
39 #include <linux/lockd/bind.h>
40 #include <linux/smp_lock.h>
41 #include <linux/seq_file.h>
42 #include <linux/mount.h>
43 #include <linux/nfs_idmap.h>
44 #include <linux/vfs.h>
45 #include <linux/inet.h>
46 #include <linux/nfs_xdr.h>
47 #include <linux/magic.h>
48 #include <linux/parser.h>
49
50 #include <asm/system.h>
51 #include <asm/uaccess.h>
52
53 #include "nfs4_fs.h"
54 #include "callback.h"
55 #include "delegation.h"
56 #include "iostat.h"
57 #include "internal.h"
58
59 #define NFSDBG_FACILITY         NFSDBG_VFS
60
61
62 struct nfs_parsed_mount_data {
63         int                     flags;
64         int                     rsize, wsize;
65         int                     timeo, retrans;
66         int                     acregmin, acregmax,
67                                 acdirmin, acdirmax;
68         int                     namlen;
69         unsigned int            bsize;
70         unsigned int            auth_flavor_len;
71         rpc_authflavor_t        auth_flavors[1];
72         char                    *client_address;
73
74         struct {
75                 struct sockaddr_in      address;
76                 unsigned int            program;
77                 unsigned int            version;
78                 unsigned short          port;
79                 int                     protocol;
80         } mount_server;
81
82         struct {
83                 struct sockaddr_in      address;
84                 char                    *hostname;
85                 char                    *export_path;
86                 unsigned int            program;
87                 int                     protocol;
88         } nfs_server;
89 };
90
91 enum {
92         /* Mount options that take no arguments */
93         Opt_soft, Opt_hard,
94         Opt_intr, Opt_nointr,
95         Opt_posix, Opt_noposix,
96         Opt_cto, Opt_nocto,
97         Opt_ac, Opt_noac,
98         Opt_lock, Opt_nolock,
99         Opt_v2, Opt_v3,
100         Opt_udp, Opt_tcp,
101         Opt_acl, Opt_noacl,
102         Opt_rdirplus, Opt_nordirplus,
103         Opt_sharecache, Opt_nosharecache,
104
105         /* Mount options that take integer arguments */
106         Opt_port,
107         Opt_rsize, Opt_wsize, Opt_bsize,
108         Opt_timeo, Opt_retrans,
109         Opt_acregmin, Opt_acregmax,
110         Opt_acdirmin, Opt_acdirmax,
111         Opt_actimeo,
112         Opt_namelen,
113         Opt_mountport,
114         Opt_mountprog, Opt_mountvers,
115         Opt_nfsprog, Opt_nfsvers,
116
117         /* Mount options that take string arguments */
118         Opt_sec, Opt_proto, Opt_mountproto,
119         Opt_addr, Opt_mounthost, Opt_clientaddr,
120
121         /* Mount options that are ignored */
122         Opt_userspace, Opt_deprecated,
123
124         Opt_err
125 };
126
127 static match_table_t nfs_mount_option_tokens = {
128         { Opt_userspace, "bg" },
129         { Opt_userspace, "fg" },
130         { Opt_soft, "soft" },
131         { Opt_hard, "hard" },
132         { Opt_intr, "intr" },
133         { Opt_nointr, "nointr" },
134         { Opt_posix, "posix" },
135         { Opt_noposix, "noposix" },
136         { Opt_cto, "cto" },
137         { Opt_nocto, "nocto" },
138         { Opt_ac, "ac" },
139         { Opt_noac, "noac" },
140         { Opt_lock, "lock" },
141         { Opt_nolock, "nolock" },
142         { Opt_v2, "v2" },
143         { Opt_v3, "v3" },
144         { Opt_udp, "udp" },
145         { Opt_tcp, "tcp" },
146         { Opt_acl, "acl" },
147         { Opt_noacl, "noacl" },
148         { Opt_rdirplus, "rdirplus" },
149         { Opt_nordirplus, "nordirplus" },
150         { Opt_sharecache, "sharecache" },
151         { Opt_nosharecache, "nosharecache" },
152
153         { Opt_port, "port=%u" },
154         { Opt_rsize, "rsize=%u" },
155         { Opt_wsize, "wsize=%u" },
156         { Opt_bsize, "bsize=%u" },
157         { Opt_timeo, "timeo=%u" },
158         { Opt_retrans, "retrans=%u" },
159         { Opt_acregmin, "acregmin=%u" },
160         { Opt_acregmax, "acregmax=%u" },
161         { Opt_acdirmin, "acdirmin=%u" },
162         { Opt_acdirmax, "acdirmax=%u" },
163         { Opt_actimeo, "actimeo=%u" },
164         { Opt_userspace, "retry=%u" },
165         { Opt_namelen, "namlen=%u" },
166         { Opt_mountport, "mountport=%u" },
167         { Opt_mountprog, "mountprog=%u" },
168         { Opt_mountvers, "mountvers=%u" },
169         { Opt_nfsprog, "nfsprog=%u" },
170         { Opt_nfsvers, "nfsvers=%u" },
171         { Opt_nfsvers, "vers=%u" },
172
173         { Opt_sec, "sec=%s" },
174         { Opt_proto, "proto=%s" },
175         { Opt_mountproto, "mountproto=%s" },
176         { Opt_addr, "addr=%s" },
177         { Opt_clientaddr, "clientaddr=%s" },
178         { Opt_mounthost, "mounthost=%s" },
179
180         { Opt_err, NULL }
181 };
182
183 enum {
184         Opt_xprt_udp, Opt_xprt_tcp,
185
186         Opt_xprt_err
187 };
188
189 static match_table_t nfs_xprt_protocol_tokens = {
190         { Opt_xprt_udp, "udp" },
191         { Opt_xprt_tcp, "tcp" },
192
193         { Opt_xprt_err, NULL }
194 };
195
196 enum {
197         Opt_sec_none, Opt_sec_sys,
198         Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
199         Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
200         Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
201
202         Opt_sec_err
203 };
204
205 static match_table_t nfs_secflavor_tokens = {
206         { Opt_sec_none, "none" },
207         { Opt_sec_none, "null" },
208         { Opt_sec_sys, "sys" },
209
210         { Opt_sec_krb5, "krb5" },
211         { Opt_sec_krb5i, "krb5i" },
212         { Opt_sec_krb5p, "krb5p" },
213
214         { Opt_sec_lkey, "lkey" },
215         { Opt_sec_lkeyi, "lkeyi" },
216         { Opt_sec_lkeyp, "lkeyp" },
217
218         { Opt_sec_err, NULL }
219 };
220
221
222 static void nfs_umount_begin(struct vfsmount *, int);
223 static int  nfs_statfs(struct dentry *, struct kstatfs *);
224 static int  nfs_show_options(struct seq_file *, struct vfsmount *);
225 static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
226 static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
227 static int nfs_xdev_get_sb(struct file_system_type *fs_type,
228                 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
229 static void nfs_kill_super(struct super_block *);
230
231 static struct file_system_type nfs_fs_type = {
232         .owner          = THIS_MODULE,
233         .name           = "nfs",
234         .get_sb         = nfs_get_sb,
235         .kill_sb        = nfs_kill_super,
236         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
237 };
238
239 struct file_system_type nfs_xdev_fs_type = {
240         .owner          = THIS_MODULE,
241         .name           = "nfs",
242         .get_sb         = nfs_xdev_get_sb,
243         .kill_sb        = nfs_kill_super,
244         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
245 };
246
247 static const struct super_operations nfs_sops = {
248         .alloc_inode    = nfs_alloc_inode,
249         .destroy_inode  = nfs_destroy_inode,
250         .write_inode    = nfs_write_inode,
251         .statfs         = nfs_statfs,
252         .clear_inode    = nfs_clear_inode,
253         .umount_begin   = nfs_umount_begin,
254         .show_options   = nfs_show_options,
255         .show_stats     = nfs_show_stats,
256 };
257
258 #ifdef CONFIG_NFS_V4
259 static int nfs4_get_sb(struct file_system_type *fs_type,
260         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
261 static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
262         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
263 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
264         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
265 static void nfs4_kill_super(struct super_block *sb);
266
267 static struct file_system_type nfs4_fs_type = {
268         .owner          = THIS_MODULE,
269         .name           = "nfs4",
270         .get_sb         = nfs4_get_sb,
271         .kill_sb        = nfs4_kill_super,
272         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
273 };
274
275 struct file_system_type nfs4_xdev_fs_type = {
276         .owner          = THIS_MODULE,
277         .name           = "nfs4",
278         .get_sb         = nfs4_xdev_get_sb,
279         .kill_sb        = nfs4_kill_super,
280         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
281 };
282
283 struct file_system_type nfs4_referral_fs_type = {
284         .owner          = THIS_MODULE,
285         .name           = "nfs4",
286         .get_sb         = nfs4_referral_get_sb,
287         .kill_sb        = nfs4_kill_super,
288         .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
289 };
290
291 static const struct super_operations nfs4_sops = {
292         .alloc_inode    = nfs_alloc_inode,
293         .destroy_inode  = nfs_destroy_inode,
294         .write_inode    = nfs_write_inode,
295         .statfs         = nfs_statfs,
296         .clear_inode    = nfs4_clear_inode,
297         .umount_begin   = nfs_umount_begin,
298         .show_options   = nfs_show_options,
299         .show_stats     = nfs_show_stats,
300 };
301 #endif
302
303 static struct shrinker *acl_shrinker;
304
305 /*
306  * Register the NFS filesystems
307  */
308 int __init register_nfs_fs(void)
309 {
310         int ret;
311
312         ret = register_filesystem(&nfs_fs_type);
313         if (ret < 0)
314                 goto error_0;
315
316         ret = nfs_register_sysctl();
317         if (ret < 0)
318                 goto error_1;
319 #ifdef CONFIG_NFS_V4
320         ret = register_filesystem(&nfs4_fs_type);
321         if (ret < 0)
322                 goto error_2;
323 #endif
324         acl_shrinker = set_shrinker(DEFAULT_SEEKS, nfs_access_cache_shrinker);
325         return 0;
326
327 #ifdef CONFIG_NFS_V4
328 error_2:
329         nfs_unregister_sysctl();
330 #endif
331 error_1:
332         unregister_filesystem(&nfs_fs_type);
333 error_0:
334         return ret;
335 }
336
337 /*
338  * Unregister the NFS filesystems
339  */
340 void __exit unregister_nfs_fs(void)
341 {
342         if (acl_shrinker != NULL)
343                 remove_shrinker(acl_shrinker);
344 #ifdef CONFIG_NFS_V4
345         unregister_filesystem(&nfs4_fs_type);
346         nfs_unregister_sysctl();
347 #endif
348         unregister_filesystem(&nfs_fs_type);
349 }
350
351 /*
352  * Deliver file system statistics to userspace
353  */
354 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
355 {
356         struct nfs_server *server = NFS_SB(dentry->d_sb);
357         unsigned char blockbits;
358         unsigned long blockres;
359         struct nfs_fh *fh = NFS_FH(dentry->d_inode);
360         struct nfs_fattr fattr;
361         struct nfs_fsstat res = {
362                         .fattr = &fattr,
363         };
364         int error;
365
366         lock_kernel();
367
368         error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
369         if (error < 0)
370                 goto out_err;
371         buf->f_type = NFS_SUPER_MAGIC;
372
373         /*
374          * Current versions of glibc do not correctly handle the
375          * case where f_frsize != f_bsize.  Eventually we want to
376          * report the value of wtmult in this field.
377          */
378         buf->f_frsize = dentry->d_sb->s_blocksize;
379
380         /*
381          * On most *nix systems, f_blocks, f_bfree, and f_bavail
382          * are reported in units of f_frsize.  Linux hasn't had
383          * an f_frsize field in its statfs struct until recently,
384          * thus historically Linux's sys_statfs reports these
385          * fields in units of f_bsize.
386          */
387         buf->f_bsize = dentry->d_sb->s_blocksize;
388         blockbits = dentry->d_sb->s_blocksize_bits;
389         blockres = (1 << blockbits) - 1;
390         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
391         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
392         buf->f_bavail = (res.abytes + blockres) >> blockbits;
393
394         buf->f_files = res.tfiles;
395         buf->f_ffree = res.afiles;
396
397         buf->f_namelen = server->namelen;
398
399         unlock_kernel();
400         return 0;
401
402  out_err:
403         dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
404         unlock_kernel();
405         return error;
406 }
407
408 /*
409  * Map the security flavour number to a name
410  */
411 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
412 {
413         static const struct {
414                 rpc_authflavor_t flavour;
415                 const char *str;
416         } sec_flavours[] = {
417                 { RPC_AUTH_NULL, "null" },
418                 { RPC_AUTH_UNIX, "sys" },
419                 { RPC_AUTH_GSS_KRB5, "krb5" },
420                 { RPC_AUTH_GSS_KRB5I, "krb5i" },
421                 { RPC_AUTH_GSS_KRB5P, "krb5p" },
422                 { RPC_AUTH_GSS_LKEY, "lkey" },
423                 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
424                 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
425                 { RPC_AUTH_GSS_SPKM, "spkm" },
426                 { RPC_AUTH_GSS_SPKMI, "spkmi" },
427                 { RPC_AUTH_GSS_SPKMP, "spkmp" },
428                 { UINT_MAX, "unknown" }
429         };
430         int i;
431
432         for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
433                 if (sec_flavours[i].flavour == flavour)
434                         break;
435         }
436         return sec_flavours[i].str;
437 }
438
439 /*
440  * Describe the mount options in force on this server representation
441  */
442 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
443 {
444         static const struct proc_nfs_info {
445                 int flag;
446                 const char *str;
447                 const char *nostr;
448         } nfs_info[] = {
449                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
450                 { NFS_MOUNT_INTR, ",intr", "" },
451                 { NFS_MOUNT_NOCTO, ",nocto", "" },
452                 { NFS_MOUNT_NOAC, ",noac", "" },
453                 { NFS_MOUNT_NONLM, ",nolock", "" },
454                 { NFS_MOUNT_NOACL, ",noacl", "" },
455                 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
456                 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
457                 { 0, NULL, NULL }
458         };
459         const struct proc_nfs_info *nfs_infop;
460         struct nfs_client *clp = nfss->nfs_client;
461         char buf[12];
462         const char *proto;
463
464         seq_printf(m, ",vers=%d", clp->rpc_ops->version);
465         seq_printf(m, ",rsize=%d", nfss->rsize);
466         seq_printf(m, ",wsize=%d", nfss->wsize);
467         if (nfss->acregmin != 3*HZ || showdefaults)
468                 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
469         if (nfss->acregmax != 60*HZ || showdefaults)
470                 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
471         if (nfss->acdirmin != 30*HZ || showdefaults)
472                 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
473         if (nfss->acdirmax != 60*HZ || showdefaults)
474                 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
475         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
476                 if (nfss->flags & nfs_infop->flag)
477                         seq_puts(m, nfs_infop->str);
478                 else
479                         seq_puts(m, nfs_infop->nostr);
480         }
481         switch (nfss->client->cl_xprt->prot) {
482                 case IPPROTO_TCP:
483                         proto = "tcp";
484                         break;
485                 case IPPROTO_UDP:
486                         proto = "udp";
487                         break;
488                 default:
489                         snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
490                         proto = buf;
491         }
492         seq_printf(m, ",proto=%s", proto);
493         seq_printf(m, ",timeo=%lu", 10U * clp->retrans_timeo / HZ);
494         seq_printf(m, ",retrans=%u", clp->retrans_count);
495         seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
496 }
497
498 /*
499  * Describe the mount options on this VFS mountpoint
500  */
501 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
502 {
503         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
504
505         nfs_show_mount_options(m, nfss, 0);
506
507         seq_puts(m, ",addr=");
508         seq_escape(m, nfss->nfs_client->cl_hostname, " \t\n\\");
509
510         return 0;
511 }
512
513 /*
514  * Present statistical information for this VFS mountpoint
515  */
516 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
517 {
518         int i, cpu;
519         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
520         struct rpc_auth *auth = nfss->client->cl_auth;
521         struct nfs_iostats totals = { };
522
523         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
524
525         /*
526          * Display all mount option settings
527          */
528         seq_printf(m, "\n\topts:\t");
529         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
530         seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
531         seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
532         seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
533         nfs_show_mount_options(m, nfss, 1);
534
535         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
536
537         seq_printf(m, "\n\tcaps:\t");
538         seq_printf(m, "caps=0x%x", nfss->caps);
539         seq_printf(m, ",wtmult=%d", nfss->wtmult);
540         seq_printf(m, ",dtsize=%d", nfss->dtsize);
541         seq_printf(m, ",bsize=%d", nfss->bsize);
542         seq_printf(m, ",namelen=%d", nfss->namelen);
543
544 #ifdef CONFIG_NFS_V4
545         if (nfss->nfs_client->cl_nfsversion == 4) {
546                 seq_printf(m, "\n\tnfsv4:\t");
547                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
548                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
549                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
550         }
551 #endif
552
553         /*
554          * Display security flavor in effect for this mount
555          */
556         seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
557         if (auth->au_flavor)
558                 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
559
560         /*
561          * Display superblock I/O counters
562          */
563         for_each_possible_cpu(cpu) {
564                 struct nfs_iostats *stats;
565
566                 preempt_disable();
567                 stats = per_cpu_ptr(nfss->io_stats, cpu);
568
569                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
570                         totals.events[i] += stats->events[i];
571                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
572                         totals.bytes[i] += stats->bytes[i];
573
574                 preempt_enable();
575         }
576
577         seq_printf(m, "\n\tevents:\t");
578         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
579                 seq_printf(m, "%lu ", totals.events[i]);
580         seq_printf(m, "\n\tbytes:\t");
581         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
582                 seq_printf(m, "%Lu ", totals.bytes[i]);
583         seq_printf(m, "\n");
584
585         rpc_print_iostats(m, nfss->client);
586
587         return 0;
588 }
589
590 /*
591  * Begin unmount by attempting to remove all automounted mountpoints we added
592  * in response to xdev traversals and referrals
593  */
594 static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
595 {
596         struct nfs_server *server = NFS_SB(vfsmnt->mnt_sb);
597         struct rpc_clnt *rpc;
598
599         shrink_submounts(vfsmnt, &nfs_automount_list);
600
601         if (!(flags & MNT_FORCE))
602                 return;
603         /* -EIO all pending I/O */
604         rpc = server->client_acl;
605         if (!IS_ERR(rpc))
606                 rpc_killall_tasks(rpc);
607         rpc = server->client;
608         if (!IS_ERR(rpc))
609                 rpc_killall_tasks(rpc);
610 }
611
612 /*
613  * Sanity-check a server address provided by the mount command
614  */
615 static int nfs_verify_server_address(struct sockaddr *addr)
616 {
617         switch (addr->sa_family) {
618         case AF_INET: {
619                 struct sockaddr_in *sa = (struct sockaddr_in *) addr;
620                 if (sa->sin_addr.s_addr != INADDR_ANY)
621                         return 1;
622                 break;
623         }
624         }
625
626         return 0;
627 }
628
629 /*
630  * Error-check and convert a string of mount options from user space into
631  * a data structure
632  */
633 static int nfs_parse_mount_options(char *raw,
634                                    struct nfs_parsed_mount_data *mnt)
635 {
636         char *p, *string;
637
638         if (!raw) {
639                 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
640                 return 1;
641         }
642         dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
643
644         while ((p = strsep(&raw, ",")) != NULL) {
645                 substring_t args[MAX_OPT_ARGS];
646                 int option, token;
647
648                 if (!*p)
649                         continue;
650
651                 dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", p);
652
653                 token = match_token(p, nfs_mount_option_tokens, args);
654                 switch (token) {
655                 case Opt_soft:
656                         mnt->flags |= NFS_MOUNT_SOFT;
657                         break;
658                 case Opt_hard:
659                         mnt->flags &= ~NFS_MOUNT_SOFT;
660                         break;
661                 case Opt_intr:
662                         mnt->flags |= NFS_MOUNT_INTR;
663                         break;
664                 case Opt_nointr:
665                         mnt->flags &= ~NFS_MOUNT_INTR;
666                         break;
667                 case Opt_posix:
668                         mnt->flags |= NFS_MOUNT_POSIX;
669                         break;
670                 case Opt_noposix:
671                         mnt->flags &= ~NFS_MOUNT_POSIX;
672                         break;
673                 case Opt_cto:
674                         mnt->flags &= ~NFS_MOUNT_NOCTO;
675                         break;
676                 case Opt_nocto:
677                         mnt->flags |= NFS_MOUNT_NOCTO;
678                         break;
679                 case Opt_ac:
680                         mnt->flags &= ~NFS_MOUNT_NOAC;
681                         break;
682                 case Opt_noac:
683                         mnt->flags |= NFS_MOUNT_NOAC;
684                         break;
685                 case Opt_lock:
686                         mnt->flags &= ~NFS_MOUNT_NONLM;
687                         break;
688                 case Opt_nolock:
689                         mnt->flags |= NFS_MOUNT_NONLM;
690                         break;
691                 case Opt_v2:
692                         mnt->flags &= ~NFS_MOUNT_VER3;
693                         break;
694                 case Opt_v3:
695                         mnt->flags |= NFS_MOUNT_VER3;
696                         break;
697                 case Opt_udp:
698                         mnt->flags &= ~NFS_MOUNT_TCP;
699                         mnt->nfs_server.protocol = IPPROTO_UDP;
700                         mnt->timeo = 7;
701                         mnt->retrans = 5;
702                         break;
703                 case Opt_tcp:
704                         mnt->flags |= NFS_MOUNT_TCP;
705                         mnt->nfs_server.protocol = IPPROTO_TCP;
706                         mnt->timeo = 600;
707                         mnt->retrans = 2;
708                         break;
709                 case Opt_acl:
710                         mnt->flags &= ~NFS_MOUNT_NOACL;
711                         break;
712                 case Opt_noacl:
713                         mnt->flags |= NFS_MOUNT_NOACL;
714                         break;
715                 case Opt_rdirplus:
716                         mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
717                         break;
718                 case Opt_nordirplus:
719                         mnt->flags |= NFS_MOUNT_NORDIRPLUS;
720                         break;
721                 case Opt_sharecache:
722                         mnt->flags &= ~NFS_MOUNT_UNSHARED;
723                         break;
724                 case Opt_nosharecache:
725                         mnt->flags |= NFS_MOUNT_UNSHARED;
726                         break;
727
728                 case Opt_port:
729                         if (match_int(args, &option))
730                                 return 0;
731                         if (option < 0 || option > 65535)
732                                 return 0;
733                         mnt->nfs_server.address.sin_port = htonl(option);
734                         break;
735                 case Opt_rsize:
736                         if (match_int(args, &mnt->rsize))
737                                 return 0;
738                         break;
739                 case Opt_wsize:
740                         if (match_int(args, &mnt->wsize))
741                                 return 0;
742                         break;
743                 case Opt_bsize:
744                         if (match_int(args, &option))
745                                 return 0;
746                         if (option < 0)
747                                 return 0;
748                         mnt->bsize = option;
749                         break;
750                 case Opt_timeo:
751                         if (match_int(args, &mnt->timeo))
752                                 return 0;
753                         break;
754                 case Opt_retrans:
755                         if (match_int(args, &mnt->retrans))
756                                 return 0;
757                         break;
758                 case Opt_acregmin:
759                         if (match_int(args, &mnt->acregmin))
760                                 return 0;
761                         break;
762                 case Opt_acregmax:
763                         if (match_int(args, &mnt->acregmax))
764                                 return 0;
765                         break;
766                 case Opt_acdirmin:
767                         if (match_int(args, &mnt->acdirmin))
768                                 return 0;
769                         break;
770                 case Opt_acdirmax:
771                         if (match_int(args, &mnt->acdirmax))
772                                 return 0;
773                         break;
774                 case Opt_actimeo:
775                         if (match_int(args, &option))
776                                 return 0;
777                         if (option < 0)
778                                 return 0;
779                         mnt->acregmin =
780                         mnt->acregmax =
781                         mnt->acdirmin =
782                         mnt->acdirmax = option;
783                         break;
784                 case Opt_namelen:
785                         if (match_int(args, &mnt->namlen))
786                                 return 0;
787                         break;
788                 case Opt_mountport:
789                         if (match_int(args, &option))
790                                 return 0;
791                         if (option < 0 || option > 65535)
792                                 return 0;
793                         mnt->mount_server.port = option;
794                         break;
795                 case Opt_mountprog:
796                         if (match_int(args, &option))
797                                 return 0;
798                         if (option < 0)
799                                 return 0;
800                         mnt->mount_server.program = option;
801                         break;
802                 case Opt_mountvers:
803                         if (match_int(args, &option))
804                                 return 0;
805                         if (option < 0)
806                                 return 0;
807                         mnt->mount_server.version = option;
808                         break;
809                 case Opt_nfsprog:
810                         if (match_int(args, &option))
811                                 return 0;
812                         if (option < 0)
813                                 return 0;
814                         mnt->nfs_server.program = option;
815                         break;
816                 case Opt_nfsvers:
817                         if (match_int(args, &option))
818                                 return 0;
819                         switch (option) {
820                         case 2:
821                                 mnt->flags &= ~NFS_MOUNT_VER3;
822                                 break;
823                         case 3:
824                                 mnt->flags |= NFS_MOUNT_VER3;
825                                 break;
826                         default:
827                                 goto out_unrec_vers;
828                         }
829                         break;
830
831                 case Opt_sec:
832                         string = match_strdup(args);
833                         if (string == NULL)
834                                 goto out_nomem;
835                         token = match_token(string, nfs_secflavor_tokens, args);
836                         kfree(string);
837
838                         /*
839                          * The flags setting is for v2/v3.  The flavor_len
840                          * setting is for v4.  v2/v3 also need to know the
841                          * difference between NULL and UNIX.
842                          */
843                         switch (token) {
844                         case Opt_sec_none:
845                                 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
846                                 mnt->auth_flavor_len = 0;
847                                 mnt->auth_flavors[0] = RPC_AUTH_NULL;
848                                 break;
849                         case Opt_sec_sys:
850                                 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
851                                 mnt->auth_flavor_len = 0;
852                                 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
853                                 break;
854                         case Opt_sec_krb5:
855                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
856                                 mnt->auth_flavor_len = 1;
857                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
858                                 break;
859                         case Opt_sec_krb5i:
860                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
861                                 mnt->auth_flavor_len = 1;
862                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
863                                 break;
864                         case Opt_sec_krb5p:
865                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
866                                 mnt->auth_flavor_len = 1;
867                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
868                                 break;
869                         case Opt_sec_lkey:
870                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
871                                 mnt->auth_flavor_len = 1;
872                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
873                                 break;
874                         case Opt_sec_lkeyi:
875                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
876                                 mnt->auth_flavor_len = 1;
877                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
878                                 break;
879                         case Opt_sec_lkeyp:
880                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
881                                 mnt->auth_flavor_len = 1;
882                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
883                                 break;
884                         case Opt_sec_spkm:
885                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
886                                 mnt->auth_flavor_len = 1;
887                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
888                                 break;
889                         case Opt_sec_spkmi:
890                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
891                                 mnt->auth_flavor_len = 1;
892                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
893                                 break;
894                         case Opt_sec_spkmp:
895                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
896                                 mnt->auth_flavor_len = 1;
897                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
898                                 break;
899                         default:
900                                 goto out_unrec_sec;
901                         }
902                         break;
903                 case Opt_proto:
904                         string = match_strdup(args);
905                         if (string == NULL)
906                                 goto out_nomem;
907                         token = match_token(string,
908                                             nfs_xprt_protocol_tokens, args);
909                         kfree(string);
910
911                         switch (token) {
912                         case Opt_udp:
913                                 mnt->flags &= ~NFS_MOUNT_TCP;
914                                 mnt->nfs_server.protocol = IPPROTO_UDP;
915                                 mnt->timeo = 7;
916                                 mnt->retrans = 5;
917                                 break;
918                         case Opt_tcp:
919                                 mnt->flags |= NFS_MOUNT_TCP;
920                                 mnt->nfs_server.protocol = IPPROTO_TCP;
921                                 mnt->timeo = 600;
922                                 mnt->retrans = 2;
923                                 break;
924                         default:
925                                 goto out_unrec_xprt;
926                         }
927                         break;
928                 case Opt_mountproto:
929                         string = match_strdup(args);
930                         if (string == NULL)
931                                 goto out_nomem;
932                         token = match_token(string,
933                                             nfs_xprt_protocol_tokens, args);
934                         kfree(string);
935
936                         switch (token) {
937                         case Opt_udp:
938                                 mnt->mount_server.protocol = IPPROTO_UDP;
939                                 break;
940                         case Opt_tcp:
941                                 mnt->mount_server.protocol = IPPROTO_TCP;
942                                 break;
943                         default:
944                                 goto out_unrec_xprt;
945                         }
946                         break;
947                 case Opt_addr:
948                         string = match_strdup(args);
949                         if (string == NULL)
950                                 goto out_nomem;
951                         mnt->nfs_server.address.sin_family = AF_INET;
952                         mnt->nfs_server.address.sin_addr.s_addr =
953                                                         in_aton(string);
954                         kfree(string);
955                         break;
956                 case Opt_clientaddr:
957                         string = match_strdup(args);
958                         if (string == NULL)
959                                 goto out_nomem;
960                         mnt->client_address = string;
961                         break;
962                 case Opt_mounthost:
963                         string = match_strdup(args);
964                         if (string == NULL)
965                                 goto out_nomem;
966                         mnt->mount_server.address.sin_family = AF_INET;
967                         mnt->mount_server.address.sin_addr.s_addr =
968                                                         in_aton(string);
969                         kfree(string);
970                         break;
971
972                 case Opt_userspace:
973                 case Opt_deprecated:
974                         break;
975
976                 default:
977                         goto out_unknown;
978                 }
979         }
980
981         return 1;
982
983 out_nomem:
984         printk(KERN_INFO "NFS: not enough memory to parse option\n");
985         return 0;
986
987 out_unrec_vers:
988         printk(KERN_INFO "NFS: unrecognized NFS version number\n");
989         return 0;
990
991 out_unrec_xprt:
992         printk(KERN_INFO "NFS: unrecognized transport protocol\n");
993         return 0;
994
995 out_unrec_sec:
996         printk(KERN_INFO "NFS: unrecognized security flavor\n");
997         return 0;
998
999 out_unknown:
1000         printk(KERN_INFO "NFS: unknown mount option: %s\n", p);
1001         return 0;
1002 }
1003
1004 /*
1005  * Use the remote server's MOUNT service to request the NFS file handle
1006  * corresponding to the provided path.
1007  */
1008 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1009                          struct nfs_fh *root_fh)
1010 {
1011         struct sockaddr_in sin;
1012         int status;
1013
1014         if (args->mount_server.version == 0) {
1015                 if (args->flags & NFS_MOUNT_VER3)
1016                         args->mount_server.version = NFS_MNT3_VERSION;
1017                 else
1018                         args->mount_server.version = NFS_MNT_VERSION;
1019         }
1020
1021         /*
1022          * Construct the mount server's address.
1023          */
1024         if (args->mount_server.address.sin_addr.s_addr != INADDR_ANY)
1025                 sin = args->mount_server.address;
1026         else
1027                 sin = args->nfs_server.address;
1028         if (args->mount_server.port == 0) {
1029                 status = rpcb_getport_sync(&sin,
1030                                            args->mount_server.program,
1031                                            args->mount_server.version,
1032                                            args->mount_server.protocol);
1033                 if (status < 0)
1034                         goto out_err;
1035                 sin.sin_port = htons(status);
1036         } else
1037                 sin.sin_port = htons(args->mount_server.port);
1038
1039         /*
1040          * Now ask the mount server to map our export path
1041          * to a file handle.
1042          */
1043         status = nfs_mount((struct sockaddr *) &sin,
1044                            sizeof(sin),
1045                            args->nfs_server.hostname,
1046                            args->nfs_server.export_path,
1047                            args->mount_server.version,
1048                            args->mount_server.protocol,
1049                            root_fh);
1050         if (status < 0)
1051                 goto out_err;
1052
1053         return status;
1054
1055 out_err:
1056         dfprintk(MOUNT, "NFS: unable to contact server on host "
1057                  NIPQUAD_FMT "\n", NIPQUAD(sin.sin_addr.s_addr));
1058         return status;
1059 }
1060
1061 /*
1062  * Validate the NFS2/NFS3 mount data
1063  * - fills in the mount root filehandle
1064  *
1065  * For option strings, user space handles the following behaviors:
1066  *
1067  * + DNS: mapping server host name to IP address ("addr=" option)
1068  *
1069  * + failure mode: how to behave if a mount request can't be handled
1070  *   immediately ("fg/bg" option)
1071  *
1072  * + retry: how often to retry a mount request ("retry=" option)
1073  *
1074  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1075  *   mountproto=tcp after mountproto=udp, and so on
1076  *
1077  * XXX: as far as I can tell, changing the NFS program number is not
1078  *      supported in the NFS client.
1079  */
1080 static int nfs_validate_mount_data(struct nfs_mount_data **options,
1081                                    struct nfs_fh *mntfh,
1082                                    const char *dev_name)
1083 {
1084         struct nfs_mount_data *data = *options;
1085
1086         if (data == NULL)
1087                 goto out_no_data;
1088
1089         switch (data->version) {
1090         case 1:
1091                 data->namlen = 0;
1092         case 2:
1093                 data->bsize = 0;
1094         case 3:
1095                 if (data->flags & NFS_MOUNT_VER3)
1096                         goto out_no_v3;
1097                 data->root.size = NFS2_FHSIZE;
1098                 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1099         case 4:
1100                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1101                         goto out_no_sec;
1102         case 5:
1103                 memset(data->context, 0, sizeof(data->context));
1104         case 6:
1105                 if (data->flags & NFS_MOUNT_VER3)
1106                         mntfh->size = data->root.size;
1107                 else
1108                         mntfh->size = NFS2_FHSIZE;
1109
1110                 if (mntfh->size > sizeof(mntfh->data))
1111                         goto out_invalid_fh;
1112
1113                 memcpy(mntfh->data, data->root.data, mntfh->size);
1114                 if (mntfh->size < sizeof(mntfh->data))
1115                         memset(mntfh->data + mntfh->size, 0,
1116                                sizeof(mntfh->data) - mntfh->size);
1117                 break;
1118         default: {
1119                 unsigned int len;
1120                 char *c;
1121                 int status;
1122                 struct nfs_parsed_mount_data args = {
1123                         .flags          = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP),
1124                         .rsize          = NFS_MAX_FILE_IO_SIZE,
1125                         .wsize          = NFS_MAX_FILE_IO_SIZE,
1126                         .timeo          = 600,
1127                         .retrans        = 2,
1128                         .acregmin       = 3,
1129                         .acregmax       = 60,
1130                         .acdirmin       = 30,
1131                         .acdirmax       = 60,
1132                         .mount_server.protocol = IPPROTO_UDP,
1133                         .mount_server.program = NFS_MNT_PROGRAM,
1134                         .nfs_server.protocol = IPPROTO_TCP,
1135                         .nfs_server.program = NFS_PROGRAM,
1136                 };
1137
1138                 if (nfs_parse_mount_options((char *) *options, &args) == 0)
1139                         return -EINVAL;
1140
1141                 data = kzalloc(sizeof(*data), GFP_KERNEL);
1142                 if (data == NULL)
1143                         return -ENOMEM;
1144
1145                 /*
1146                  * NB: after this point, caller will free "data"
1147                  * if we return an error
1148                  */
1149                 *options = data;
1150
1151                 c = strchr(dev_name, ':');
1152                 if (c == NULL)
1153                         return -EINVAL;
1154                 len = c - dev_name - 1;
1155                 if (len > sizeof(data->hostname))
1156                         return -EINVAL;
1157                 strncpy(data->hostname, dev_name, len);
1158                 args.nfs_server.hostname = data->hostname;
1159
1160                 c++;
1161                 if (strlen(c) > NFS_MAXPATHLEN)
1162                         return -EINVAL;
1163                 args.nfs_server.export_path = c;
1164
1165                 status = nfs_try_mount(&args, mntfh);
1166                 if (status)
1167                         return -EINVAL;
1168
1169                 /*
1170                  * Translate to nfs_mount_data, which nfs_fill_super
1171                  * can deal with.
1172                  */
1173                 data->version           = 6;
1174                 data->flags             = args.flags;
1175                 data->rsize             = args.rsize;
1176                 data->wsize             = args.wsize;
1177                 data->timeo             = args.timeo;
1178                 data->retrans           = args.retrans;
1179                 data->acregmin          = args.acregmin;
1180                 data->acregmax          = args.acregmax;
1181                 data->acdirmin          = args.acdirmin;
1182                 data->acdirmax          = args.acdirmax;
1183                 data->addr              = args.nfs_server.address;
1184                 data->namlen            = args.namlen;
1185                 data->bsize             = args.bsize;
1186                 data->pseudoflavor      = args.auth_flavors[0];
1187
1188                 break;
1189                 }
1190         }
1191
1192         if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
1193                 data->pseudoflavor = RPC_AUTH_UNIX;
1194
1195 #ifndef CONFIG_NFS_V3
1196         if (data->flags & NFS_MOUNT_VER3)
1197                 goto out_v3_not_compiled;
1198 #endif /* !CONFIG_NFS_V3 */
1199
1200         if (!nfs_verify_server_address((struct sockaddr *) &data->addr))
1201                 goto out_no_address;
1202
1203         return 0;
1204
1205 out_no_data:
1206         dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1207         return -EINVAL;
1208
1209 out_no_v3:
1210         dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1211                  data->version);
1212         return -EINVAL;
1213
1214 out_no_sec:
1215         dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1216         return -EINVAL;
1217
1218 #ifndef CONFIG_NFS_V3
1219 out_v3_not_compiled:
1220         dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1221         return -EPROTONOSUPPORT;
1222 #endif /* !CONFIG_NFS_V3 */
1223
1224 out_no_address:
1225         dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1226         return -EINVAL;
1227
1228 out_invalid_fh:
1229         dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1230         return -EINVAL;
1231 }
1232
1233 /*
1234  * Initialise the common bits of the superblock
1235  */
1236 static inline void nfs_initialise_sb(struct super_block *sb)
1237 {
1238         struct nfs_server *server = NFS_SB(sb);
1239
1240         sb->s_magic = NFS_SUPER_MAGIC;
1241
1242         /* We probably want something more informative here */
1243         snprintf(sb->s_id, sizeof(sb->s_id),
1244                  "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1245
1246         if (sb->s_blocksize == 0)
1247                 sb->s_blocksize = nfs_block_bits(server->wsize,
1248                                                  &sb->s_blocksize_bits);
1249
1250         if (server->flags & NFS_MOUNT_NOAC)
1251                 sb->s_flags |= MS_SYNCHRONOUS;
1252
1253         nfs_super_set_maxbytes(sb, server->maxfilesize);
1254 }
1255
1256 /*
1257  * Finish setting up an NFS2/3 superblock
1258  */
1259 static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
1260 {
1261         struct nfs_server *server = NFS_SB(sb);
1262
1263         sb->s_blocksize_bits = 0;
1264         sb->s_blocksize = 0;
1265         if (data->bsize)
1266                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1267
1268         if (server->flags & NFS_MOUNT_VER3) {
1269                 /* The VFS shouldn't apply the umask to mode bits. We will do
1270                  * so ourselves when necessary.
1271                  */
1272                 sb->s_flags |= MS_POSIXACL;
1273                 sb->s_time_gran = 1;
1274         }
1275
1276         sb->s_op = &nfs_sops;
1277         nfs_initialise_sb(sb);
1278 }
1279
1280 /*
1281  * Finish setting up a cloned NFS2/3 superblock
1282  */
1283 static void nfs_clone_super(struct super_block *sb,
1284                             const struct super_block *old_sb)
1285 {
1286         struct nfs_server *server = NFS_SB(sb);
1287
1288         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1289         sb->s_blocksize = old_sb->s_blocksize;
1290         sb->s_maxbytes = old_sb->s_maxbytes;
1291
1292         if (server->flags & NFS_MOUNT_VER3) {
1293                 /* The VFS shouldn't apply the umask to mode bits. We will do
1294                  * so ourselves when necessary.
1295                  */
1296                 sb->s_flags |= MS_POSIXACL;
1297                 sb->s_time_gran = 1;
1298         }
1299
1300         sb->s_op = old_sb->s_op;
1301         nfs_initialise_sb(sb);
1302 }
1303
1304 static int nfs_set_super(struct super_block *s, void *_server)
1305 {
1306         struct nfs_server *server = _server;
1307         int ret;
1308
1309         s->s_fs_info = server;
1310         ret = set_anon_super(s, server);
1311         if (ret == 0)
1312                 server->s_dev = s->s_dev;
1313         return ret;
1314 }
1315
1316 static int nfs_compare_super(struct super_block *sb, void *data)
1317 {
1318         struct nfs_server *server = data, *old = NFS_SB(sb);
1319
1320         if (memcmp(&old->nfs_client->cl_addr,
1321                                 &server->nfs_client->cl_addr,
1322                                 sizeof(old->nfs_client->cl_addr)) != 0)
1323                 return 0;
1324         /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1325         if (old->flags & NFS_MOUNT_UNSHARED)
1326                 return 0;
1327         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1328                 return 0;
1329         return 1;
1330 }
1331
1332 #define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1333
1334 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1335 {
1336         const struct nfs_server *a = s->s_fs_info;
1337         const struct rpc_clnt *clnt_a = a->client;
1338         const struct rpc_clnt *clnt_b = b->client;
1339
1340         if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1341                 goto Ebusy;
1342         if (a->nfs_client != b->nfs_client)
1343                 goto Ebusy;
1344         if (a->flags != b->flags)
1345                 goto Ebusy;
1346         if (a->wsize != b->wsize)
1347                 goto Ebusy;
1348         if (a->rsize != b->rsize)
1349                 goto Ebusy;
1350         if (a->acregmin != b->acregmin)
1351                 goto Ebusy;
1352         if (a->acregmax != b->acregmax)
1353                 goto Ebusy;
1354         if (a->acdirmin != b->acdirmin)
1355                 goto Ebusy;
1356         if (a->acdirmax != b->acdirmax)
1357                 goto Ebusy;
1358         if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1359                 goto Ebusy;
1360         return 0;
1361 Ebusy:
1362         return -EBUSY;
1363 }
1364
1365 static int nfs_get_sb(struct file_system_type *fs_type,
1366         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1367 {
1368         struct nfs_server *server = NULL;
1369         struct super_block *s;
1370         struct nfs_fh mntfh;
1371         struct nfs_mount_data *data = raw_data;
1372         struct dentry *mntroot;
1373         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1374         int error;
1375
1376         /* Validate the mount data */
1377         error = nfs_validate_mount_data(&data, &mntfh, dev_name);
1378         if (error < 0)
1379                 goto out;
1380
1381         /* Get a volume representation */
1382         server = nfs_create_server(data, &mntfh);
1383         if (IS_ERR(server)) {
1384                 error = PTR_ERR(server);
1385                 goto out;
1386         }
1387
1388         if (server->flags & NFS_MOUNT_UNSHARED)
1389                 compare_super = NULL;
1390
1391         /* Get a superblock - note that we may end up sharing one that already exists */
1392         s = sget(fs_type, compare_super, nfs_set_super, server);
1393         if (IS_ERR(s)) {
1394                 error = PTR_ERR(s);
1395                 goto out_err_nosb;
1396         }
1397
1398         if (s->s_fs_info != server) {
1399                 error = nfs_compare_mount_options(s, server, flags);
1400                 nfs_free_server(server);
1401                 server = NULL;
1402                 if (error < 0)
1403                         goto error_splat_super;
1404         }
1405
1406         if (!s->s_root) {
1407                 /* initial superblock/root creation */
1408                 s->s_flags = flags;
1409                 nfs_fill_super(s, data);
1410         }
1411
1412         mntroot = nfs_get_root(s, &mntfh);
1413         if (IS_ERR(mntroot)) {
1414                 error = PTR_ERR(mntroot);
1415                 goto error_splat_super;
1416         }
1417
1418         s->s_flags |= MS_ACTIVE;
1419         mnt->mnt_sb = s;
1420         mnt->mnt_root = mntroot;
1421         error = 0;
1422
1423 out:
1424         if (data != raw_data)
1425                 kfree(data);
1426         return error;
1427
1428 out_err_nosb:
1429         nfs_free_server(server);
1430         goto out;
1431
1432 error_splat_super:
1433         up_write(&s->s_umount);
1434         deactivate_super(s);
1435         goto out;
1436 }
1437
1438 /*
1439  * Destroy an NFS2/3 superblock
1440  */
1441 static void nfs_kill_super(struct super_block *s)
1442 {
1443         struct nfs_server *server = NFS_SB(s);
1444
1445         kill_anon_super(s);
1446         nfs_free_server(server);
1447 }
1448
1449 /*
1450  * Clone an NFS2/3 server record on xdev traversal (FSID-change)
1451  */
1452 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
1453                            const char *dev_name, void *raw_data,
1454                            struct vfsmount *mnt)
1455 {
1456         struct nfs_clone_mount *data = raw_data;
1457         struct super_block *s;
1458         struct nfs_server *server;
1459         struct dentry *mntroot;
1460         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1461         int error;
1462
1463         dprintk("--> nfs_xdev_get_sb()\n");
1464
1465         /* create a new volume representation */
1466         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
1467         if (IS_ERR(server)) {
1468                 error = PTR_ERR(server);
1469                 goto out_err_noserver;
1470         }
1471
1472         if (server->flags & NFS_MOUNT_UNSHARED)
1473                 compare_super = NULL;
1474
1475         /* Get a superblock - note that we may end up sharing one that already exists */
1476         s = sget(&nfs_fs_type, compare_super, nfs_set_super, server);
1477         if (IS_ERR(s)) {
1478                 error = PTR_ERR(s);
1479                 goto out_err_nosb;
1480         }
1481
1482         if (s->s_fs_info != server) {
1483                 error = nfs_compare_mount_options(s, server, flags);
1484                 nfs_free_server(server);
1485                 server = NULL;
1486                 if (error < 0)
1487                         goto error_splat_super;
1488         }
1489
1490         if (!s->s_root) {
1491                 /* initial superblock/root creation */
1492                 s->s_flags = flags;
1493                 nfs_clone_super(s, data->sb);
1494         }
1495
1496         mntroot = nfs_get_root(s, data->fh);
1497         if (IS_ERR(mntroot)) {
1498                 error = PTR_ERR(mntroot);
1499                 goto error_splat_super;
1500         }
1501
1502         s->s_flags |= MS_ACTIVE;
1503         mnt->mnt_sb = s;
1504         mnt->mnt_root = mntroot;
1505
1506         dprintk("<-- nfs_xdev_get_sb() = 0\n");
1507         return 0;
1508
1509 out_err_nosb:
1510         nfs_free_server(server);
1511 out_err_noserver:
1512         dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
1513         return error;
1514
1515 error_splat_super:
1516         up_write(&s->s_umount);
1517         deactivate_super(s);
1518         dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
1519         return error;
1520 }
1521
1522 #ifdef CONFIG_NFS_V4
1523
1524 /*
1525  * Finish setting up a cloned NFS4 superblock
1526  */
1527 static void nfs4_clone_super(struct super_block *sb,
1528                             const struct super_block *old_sb)
1529 {
1530         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1531         sb->s_blocksize = old_sb->s_blocksize;
1532         sb->s_maxbytes = old_sb->s_maxbytes;
1533         sb->s_time_gran = 1;
1534         sb->s_op = old_sb->s_op;
1535         nfs_initialise_sb(sb);
1536 }
1537
1538 /*
1539  * Set up an NFS4 superblock
1540  */
1541 static void nfs4_fill_super(struct super_block *sb)
1542 {
1543         sb->s_time_gran = 1;
1544         sb->s_op = &nfs4_sops;
1545         nfs_initialise_sb(sb);
1546 }
1547
1548 /*
1549  * Validate NFSv4 mount options
1550  */
1551 static int nfs4_validate_mount_data(struct nfs4_mount_data **options,
1552                                     const char *dev_name,
1553                                     struct sockaddr_in *addr,
1554                                     rpc_authflavor_t *authflavour,
1555                                     char **hostname,
1556                                     char **mntpath,
1557                                     char **ip_addr)
1558 {
1559         struct nfs4_mount_data *data = *options;
1560         char *c;
1561
1562         if (data == NULL)
1563                 goto out_no_data;
1564
1565         switch (data->version) {
1566         case 1:
1567                 if (data->host_addrlen != sizeof(*addr))
1568                         goto out_no_address;
1569                 if (copy_from_user(addr, data->host_addr, sizeof(*addr)))
1570                         return -EFAULT;
1571                 if (addr->sin_port == 0)
1572                         addr->sin_port = htons(NFS_PORT);
1573                 if (!nfs_verify_server_address((struct sockaddr *) addr))
1574                         goto out_no_address;
1575
1576                 switch (data->auth_flavourlen) {
1577                 case 0:
1578                         *authflavour = RPC_AUTH_UNIX;
1579                         break;
1580                 case 1:
1581                         if (copy_from_user(authflavour, data->auth_flavours,
1582                                            sizeof(*authflavour)))
1583                                 return -EFAULT;
1584                         break;
1585                 default:
1586                         goto out_inval_auth;
1587                 }
1588
1589                 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
1590                 if (IS_ERR(c))
1591                         return PTR_ERR(c);
1592                 *hostname = c;
1593
1594                 c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
1595                 if (IS_ERR(c))
1596                         return PTR_ERR(c);
1597                 *mntpath = c;
1598                 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *mntpath);
1599
1600                 c = strndup_user(data->client_addr.data, 16);
1601                 if (IS_ERR(c))
1602                         return PTR_ERR(c);
1603                 *ip_addr = c;
1604
1605                 break;
1606         default: {
1607                 unsigned int len;
1608                 struct nfs_parsed_mount_data args = {
1609                         .rsize          = NFS_MAX_FILE_IO_SIZE,
1610                         .wsize          = NFS_MAX_FILE_IO_SIZE,
1611                         .timeo          = 600,
1612                         .retrans        = 2,
1613                         .acregmin       = 3,
1614                         .acregmax       = 60,
1615                         .acdirmin       = 30,
1616                         .acdirmax       = 60,
1617                         .nfs_server.protocol = IPPROTO_TCP,
1618                 };
1619
1620                 if (nfs_parse_mount_options((char *) *options, &args) == 0)
1621                         return -EINVAL;
1622
1623                 if (!nfs_verify_server_address((struct sockaddr *)
1624                                                 &args.nfs_server.address))
1625                         return -EINVAL;
1626                 *addr = args.nfs_server.address;
1627
1628                 switch (args.auth_flavor_len) {
1629                 case 0:
1630                         *authflavour = RPC_AUTH_UNIX;
1631                         break;
1632                 case 1:
1633                         *authflavour = (rpc_authflavor_t) args.auth_flavors[0];
1634                         break;
1635                 default:
1636                         goto out_inval_auth;
1637                 }
1638
1639                 /*
1640                  * Translate to nfs4_mount_data, which nfs4_fill_super
1641                  * can deal with.
1642                  */
1643                 data = kzalloc(sizeof(*data), GFP_KERNEL);
1644                 if (data == NULL)
1645                         return -ENOMEM;
1646                 *options = data;
1647
1648                 data->version   = 1;
1649                 data->flags     = args.flags & NFS4_MOUNT_FLAGMASK;
1650                 data->rsize     = args.rsize;
1651                 data->wsize     = args.wsize;
1652                 data->timeo     = args.timeo;
1653                 data->retrans   = args.retrans;
1654                 data->acregmin  = args.acregmin;
1655                 data->acregmax  = args.acregmax;
1656                 data->acdirmin  = args.acdirmin;
1657                 data->acdirmax  = args.acdirmax;
1658                 data->proto     = args.nfs_server.protocol;
1659
1660                 /*
1661                  * Split "dev_name" into "hostname:mntpath".
1662                  */
1663                 c = strchr(dev_name, ':');
1664                 if (c == NULL)
1665                         return -EINVAL;
1666                 /* while calculating len, pretend ':' is '\0' */
1667                 len = c - dev_name;
1668                 if (len > NFS4_MAXNAMLEN)
1669                         return -EINVAL;
1670                 *hostname = kzalloc(len, GFP_KERNEL);
1671                 if (*hostname == NULL)
1672                         return -ENOMEM;
1673                 strncpy(*hostname, dev_name, len - 1);
1674
1675                 c++;                    /* step over the ':' */
1676                 len = strlen(c);
1677                 if (len > NFS4_MAXPATHLEN)
1678                         return -EINVAL;
1679                 *mntpath = kzalloc(len + 1, GFP_KERNEL);
1680                 if (*mntpath == NULL)
1681                         return -ENOMEM;
1682                 strncpy(*mntpath, c, len);
1683
1684                 dprintk("MNTPATH: %s\n", *mntpath);
1685
1686                 *ip_addr = args.client_address;
1687
1688                 break;
1689                 }
1690         }
1691
1692         return 0;
1693
1694 out_no_data:
1695         dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
1696         return -EINVAL;
1697
1698 out_inval_auth:
1699         dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
1700                  data->auth_flavourlen);
1701         return -EINVAL;
1702
1703 out_no_address:
1704         dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
1705         return -EINVAL;
1706 }
1707
1708 /*
1709  * Get the superblock for an NFS4 mountpoint
1710  */
1711 static int nfs4_get_sb(struct file_system_type *fs_type,
1712         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1713 {
1714         struct nfs4_mount_data *data = raw_data;
1715         struct super_block *s;
1716         struct nfs_server *server;
1717         struct sockaddr_in addr;
1718         rpc_authflavor_t authflavour;
1719         struct nfs_fh mntfh;
1720         struct dentry *mntroot;
1721         char *mntpath = NULL, *hostname = NULL, *ip_addr = NULL;
1722         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1723         int error;
1724
1725         /* Validate the mount data */
1726         error = nfs4_validate_mount_data(&data, dev_name, &addr, &authflavour,
1727                                          &hostname, &mntpath, &ip_addr);
1728         if (error < 0)
1729                 goto out;
1730
1731         /* Get a volume representation */
1732         server = nfs4_create_server(data, hostname, &addr, mntpath, ip_addr,
1733                                     authflavour, &mntfh);
1734         if (IS_ERR(server)) {
1735                 error = PTR_ERR(server);
1736                 goto out;
1737         }
1738
1739         if (server->flags & NFS4_MOUNT_UNSHARED)
1740                 compare_super = NULL;
1741
1742         /* Get a superblock - note that we may end up sharing one that already exists */
1743         s = sget(fs_type, compare_super, nfs_set_super, server);
1744         if (IS_ERR(s)) {
1745                 error = PTR_ERR(s);
1746                 goto out_free;
1747         }
1748
1749         if (s->s_fs_info != server) {
1750                 nfs_free_server(server);
1751                 server = NULL;
1752         }
1753
1754         if (!s->s_root) {
1755                 /* initial superblock/root creation */
1756                 s->s_flags = flags;
1757                 nfs4_fill_super(s);
1758         }
1759
1760         mntroot = nfs4_get_root(s, &mntfh);
1761         if (IS_ERR(mntroot)) {
1762                 error = PTR_ERR(mntroot);
1763                 goto error_splat_super;
1764         }
1765
1766         s->s_flags |= MS_ACTIVE;
1767         mnt->mnt_sb = s;
1768         mnt->mnt_root = mntroot;
1769         error = 0;
1770
1771 out:
1772         kfree(ip_addr);
1773         kfree(mntpath);
1774         kfree(hostname);
1775         return error;
1776
1777 out_free:
1778         nfs_free_server(server);
1779         goto out;
1780
1781 error_splat_super:
1782         up_write(&s->s_umount);
1783         deactivate_super(s);
1784         goto out;
1785 }
1786
1787 static void nfs4_kill_super(struct super_block *sb)
1788 {
1789         struct nfs_server *server = NFS_SB(sb);
1790
1791         nfs_return_all_delegations(sb);
1792         kill_anon_super(sb);
1793
1794         nfs4_renewd_prepare_shutdown(server);
1795         nfs_free_server(server);
1796 }
1797
1798 /*
1799  * Clone an NFS4 server record on xdev traversal (FSID-change)
1800  */
1801 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
1802                             const char *dev_name, void *raw_data,
1803                             struct vfsmount *mnt)
1804 {
1805         struct nfs_clone_mount *data = raw_data;
1806         struct super_block *s;
1807         struct nfs_server *server;
1808         struct dentry *mntroot;
1809         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1810         int error;
1811
1812         dprintk("--> nfs4_xdev_get_sb()\n");
1813
1814         /* create a new volume representation */
1815         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
1816         if (IS_ERR(server)) {
1817                 error = PTR_ERR(server);
1818                 goto out_err_noserver;
1819         }
1820
1821         if (server->flags & NFS4_MOUNT_UNSHARED)
1822                 compare_super = NULL;
1823
1824         /* Get a superblock - note that we may end up sharing one that already exists */
1825         s = sget(&nfs_fs_type, compare_super, nfs_set_super, server);
1826         if (IS_ERR(s)) {
1827                 error = PTR_ERR(s);
1828                 goto out_err_nosb;
1829         }
1830
1831         if (s->s_fs_info != server) {
1832                 nfs_free_server(server);
1833                 server = NULL;
1834         }
1835
1836         if (!s->s_root) {
1837                 /* initial superblock/root creation */
1838                 s->s_flags = flags;
1839                 nfs4_clone_super(s, data->sb);
1840         }
1841
1842         mntroot = nfs4_get_root(s, data->fh);
1843         if (IS_ERR(mntroot)) {
1844                 error = PTR_ERR(mntroot);
1845                 goto error_splat_super;
1846         }
1847
1848         s->s_flags |= MS_ACTIVE;
1849         mnt->mnt_sb = s;
1850         mnt->mnt_root = mntroot;
1851
1852         dprintk("<-- nfs4_xdev_get_sb() = 0\n");
1853         return 0;
1854
1855 out_err_nosb:
1856         nfs_free_server(server);
1857 out_err_noserver:
1858         dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
1859         return error;
1860
1861 error_splat_super:
1862         up_write(&s->s_umount);
1863         deactivate_super(s);
1864         dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
1865         return error;
1866 }
1867
1868 /*
1869  * Create an NFS4 server record on referral traversal
1870  */
1871 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
1872                                 const char *dev_name, void *raw_data,
1873                                 struct vfsmount *mnt)
1874 {
1875         struct nfs_clone_mount *data = raw_data;
1876         struct super_block *s;
1877         struct nfs_server *server;
1878         struct dentry *mntroot;
1879         struct nfs_fh mntfh;
1880         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1881         int error;
1882
1883         dprintk("--> nfs4_referral_get_sb()\n");
1884
1885         /* create a new volume representation */
1886         server = nfs4_create_referral_server(data, &mntfh);
1887         if (IS_ERR(server)) {
1888                 error = PTR_ERR(server);
1889                 goto out_err_noserver;
1890         }
1891
1892         if (server->flags & NFS4_MOUNT_UNSHARED)
1893                 compare_super = NULL;
1894
1895         /* Get a superblock - note that we may end up sharing one that already exists */
1896         s = sget(&nfs_fs_type, compare_super, nfs_set_super, server);
1897         if (IS_ERR(s)) {
1898                 error = PTR_ERR(s);
1899                 goto out_err_nosb;
1900         }
1901
1902         if (s->s_fs_info != server) {
1903                 nfs_free_server(server);
1904                 server = NULL;
1905         }
1906
1907         if (!s->s_root) {
1908                 /* initial superblock/root creation */
1909                 s->s_flags = flags;
1910                 nfs4_fill_super(s);
1911         }
1912
1913         mntroot = nfs4_get_root(s, &mntfh);
1914         if (IS_ERR(mntroot)) {
1915                 error = PTR_ERR(mntroot);
1916                 goto error_splat_super;
1917         }
1918
1919         s->s_flags |= MS_ACTIVE;
1920         mnt->mnt_sb = s;
1921         mnt->mnt_root = mntroot;
1922
1923         dprintk("<-- nfs4_referral_get_sb() = 0\n");
1924         return 0;
1925
1926 out_err_nosb:
1927         nfs_free_server(server);
1928 out_err_noserver:
1929         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
1930         return error;
1931
1932 error_splat_super:
1933         up_write(&s->s_umount);
1934         deactivate_super(s);
1935         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
1936         return error;
1937 }
1938
1939 #endif /* CONFIG_NFS_V4 */