]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/nfs/super.c
5085f53be2fefa4dc621ef1448ef613c1ab172eb
[linux-2.6-omap-h63xx.git] / fs / nfs / super.c
1 /*
2  *  linux/fs/nfs/super.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs superblock handling functions
7  *
8  *  Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
10  *
11  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12  *  J.S.Peatfield@damtp.cam.ac.uk
13  *
14  *  Split from inode.c by David Howells <dhowells@redhat.com>
15  *
16  * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
17  *   particular server are held in the same superblock
18  * - NFS superblocks can have several effective roots to the dentry tree
19  * - directory type roots are spliced into the tree when a path from one root reaches the root
20  *   of another (see nfs_lookup())
21  */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25
26 #include <linux/time.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/errno.h>
32 #include <linux/unistd.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/stats.h>
35 #include <linux/sunrpc/metrics.h>
36 #include <linux/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         .shrink         = nfs_access_cache_shrinker,
305         .seeks          = DEFAULT_SEEKS,
306 };
307
308 /*
309  * Register the NFS filesystems
310  */
311 int __init register_nfs_fs(void)
312 {
313         int ret;
314
315         ret = register_filesystem(&nfs_fs_type);
316         if (ret < 0)
317                 goto error_0;
318
319         ret = nfs_register_sysctl();
320         if (ret < 0)
321                 goto error_1;
322 #ifdef CONFIG_NFS_V4
323         ret = register_filesystem(&nfs4_fs_type);
324         if (ret < 0)
325                 goto error_2;
326 #endif
327         register_shrinker(&acl_shrinker);
328         return 0;
329
330 #ifdef CONFIG_NFS_V4
331 error_2:
332         nfs_unregister_sysctl();
333 #endif
334 error_1:
335         unregister_filesystem(&nfs_fs_type);
336 error_0:
337         return ret;
338 }
339
340 /*
341  * Unregister the NFS filesystems
342  */
343 void __exit unregister_nfs_fs(void)
344 {
345         unregister_shrinker(&acl_shrinker);
346 #ifdef CONFIG_NFS_V4
347         unregister_filesystem(&nfs4_fs_type);
348 #endif
349         nfs_unregister_sysctl();
350         unregister_filesystem(&nfs_fs_type);
351 }
352
353 /*
354  * Deliver file system statistics to userspace
355  */
356 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
357 {
358         struct nfs_server *server = NFS_SB(dentry->d_sb);
359         unsigned char blockbits;
360         unsigned long blockres;
361         struct nfs_fh *fh = NFS_FH(dentry->d_inode);
362         struct nfs_fattr fattr;
363         struct nfs_fsstat res = {
364                         .fattr = &fattr,
365         };
366         int error;
367
368         lock_kernel();
369
370         error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
371         if (error < 0)
372                 goto out_err;
373         buf->f_type = NFS_SUPER_MAGIC;
374
375         /*
376          * Current versions of glibc do not correctly handle the
377          * case where f_frsize != f_bsize.  Eventually we want to
378          * report the value of wtmult in this field.
379          */
380         buf->f_frsize = dentry->d_sb->s_blocksize;
381
382         /*
383          * On most *nix systems, f_blocks, f_bfree, and f_bavail
384          * are reported in units of f_frsize.  Linux hasn't had
385          * an f_frsize field in its statfs struct until recently,
386          * thus historically Linux's sys_statfs reports these
387          * fields in units of f_bsize.
388          */
389         buf->f_bsize = dentry->d_sb->s_blocksize;
390         blockbits = dentry->d_sb->s_blocksize_bits;
391         blockres = (1 << blockbits) - 1;
392         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
393         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
394         buf->f_bavail = (res.abytes + blockres) >> blockbits;
395
396         buf->f_files = res.tfiles;
397         buf->f_ffree = res.afiles;
398
399         buf->f_namelen = server->namelen;
400
401         unlock_kernel();
402         return 0;
403
404  out_err:
405         dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
406         unlock_kernel();
407         return error;
408 }
409
410 /*
411  * Map the security flavour number to a name
412  */
413 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
414 {
415         static const struct {
416                 rpc_authflavor_t flavour;
417                 const char *str;
418         } sec_flavours[] = {
419                 { RPC_AUTH_NULL, "null" },
420                 { RPC_AUTH_UNIX, "sys" },
421                 { RPC_AUTH_GSS_KRB5, "krb5" },
422                 { RPC_AUTH_GSS_KRB5I, "krb5i" },
423                 { RPC_AUTH_GSS_KRB5P, "krb5p" },
424                 { RPC_AUTH_GSS_LKEY, "lkey" },
425                 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
426                 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
427                 { RPC_AUTH_GSS_SPKM, "spkm" },
428                 { RPC_AUTH_GSS_SPKMI, "spkmi" },
429                 { RPC_AUTH_GSS_SPKMP, "spkmp" },
430                 { UINT_MAX, "unknown" }
431         };
432         int i;
433
434         for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
435                 if (sec_flavours[i].flavour == flavour)
436                         break;
437         }
438         return sec_flavours[i].str;
439 }
440
441 /*
442  * Describe the mount options in force on this server representation
443  */
444 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
445 {
446         static const struct proc_nfs_info {
447                 int flag;
448                 const char *str;
449                 const char *nostr;
450         } nfs_info[] = {
451                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
452                 { NFS_MOUNT_INTR, ",intr", "" },
453                 { NFS_MOUNT_NOCTO, ",nocto", "" },
454                 { NFS_MOUNT_NOAC, ",noac", "" },
455                 { NFS_MOUNT_NONLM, ",nolock", "" },
456                 { NFS_MOUNT_NOACL, ",noacl", "" },
457                 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
458                 { NFS_MOUNT_UNSHARED, ",nosharecache", ""},
459                 { 0, NULL, NULL }
460         };
461         const struct proc_nfs_info *nfs_infop;
462         struct nfs_client *clp = nfss->nfs_client;
463         char buf[12];
464         const char *proto;
465
466         seq_printf(m, ",vers=%d", clp->rpc_ops->version);
467         seq_printf(m, ",rsize=%d", nfss->rsize);
468         seq_printf(m, ",wsize=%d", nfss->wsize);
469         if (nfss->acregmin != 3*HZ || showdefaults)
470                 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
471         if (nfss->acregmax != 60*HZ || showdefaults)
472                 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
473         if (nfss->acdirmin != 30*HZ || showdefaults)
474                 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
475         if (nfss->acdirmax != 60*HZ || showdefaults)
476                 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
477         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
478                 if (nfss->flags & nfs_infop->flag)
479                         seq_puts(m, nfs_infop->str);
480                 else
481                         seq_puts(m, nfs_infop->nostr);
482         }
483         switch (nfss->client->cl_xprt->prot) {
484                 case IPPROTO_TCP:
485                         proto = "tcp";
486                         break;
487                 case IPPROTO_UDP:
488                         proto = "udp";
489                         break;
490                 default:
491                         snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
492                         proto = buf;
493         }
494         seq_printf(m, ",proto=%s", proto);
495         seq_printf(m, ",timeo=%lu", 10U * clp->retrans_timeo / HZ);
496         seq_printf(m, ",retrans=%u", clp->retrans_count);
497         seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
498 }
499
500 /*
501  * Describe the mount options on this VFS mountpoint
502  */
503 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
504 {
505         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
506
507         nfs_show_mount_options(m, nfss, 0);
508
509         seq_printf(m, ",addr="NIPQUAD_FMT,
510                 NIPQUAD(nfss->nfs_client->cl_addr.sin_addr));
511
512         return 0;
513 }
514
515 /*
516  * Present statistical information for this VFS mountpoint
517  */
518 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
519 {
520         int i, cpu;
521         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
522         struct rpc_auth *auth = nfss->client->cl_auth;
523         struct nfs_iostats totals = { };
524
525         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
526
527         /*
528          * Display all mount option settings
529          */
530         seq_printf(m, "\n\topts:\t");
531         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
532         seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
533         seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
534         seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
535         nfs_show_mount_options(m, nfss, 1);
536
537         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
538
539         seq_printf(m, "\n\tcaps:\t");
540         seq_printf(m, "caps=0x%x", nfss->caps);
541         seq_printf(m, ",wtmult=%d", nfss->wtmult);
542         seq_printf(m, ",dtsize=%d", nfss->dtsize);
543         seq_printf(m, ",bsize=%d", nfss->bsize);
544         seq_printf(m, ",namelen=%d", nfss->namelen);
545
546 #ifdef CONFIG_NFS_V4
547         if (nfss->nfs_client->cl_nfsversion == 4) {
548                 seq_printf(m, "\n\tnfsv4:\t");
549                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
550                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
551                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
552         }
553 #endif
554
555         /*
556          * Display security flavor in effect for this mount
557          */
558         seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
559         if (auth->au_flavor)
560                 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
561
562         /*
563          * Display superblock I/O counters
564          */
565         for_each_possible_cpu(cpu) {
566                 struct nfs_iostats *stats;
567
568                 preempt_disable();
569                 stats = per_cpu_ptr(nfss->io_stats, cpu);
570
571                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
572                         totals.events[i] += stats->events[i];
573                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
574                         totals.bytes[i] += stats->bytes[i];
575
576                 preempt_enable();
577         }
578
579         seq_printf(m, "\n\tevents:\t");
580         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
581                 seq_printf(m, "%lu ", totals.events[i]);
582         seq_printf(m, "\n\tbytes:\t");
583         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
584                 seq_printf(m, "%Lu ", totals.bytes[i]);
585         seq_printf(m, "\n");
586
587         rpc_print_iostats(m, nfss->client);
588
589         return 0;
590 }
591
592 /*
593  * Begin unmount by attempting to remove all automounted mountpoints we added
594  * in response to xdev traversals and referrals
595  */
596 static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
597 {
598         struct nfs_server *server = NFS_SB(vfsmnt->mnt_sb);
599         struct rpc_clnt *rpc;
600
601         shrink_submounts(vfsmnt, &nfs_automount_list);
602
603         if (!(flags & MNT_FORCE))
604                 return;
605         /* -EIO all pending I/O */
606         rpc = server->client_acl;
607         if (!IS_ERR(rpc))
608                 rpc_killall_tasks(rpc);
609         rpc = server->client;
610         if (!IS_ERR(rpc))
611                 rpc_killall_tasks(rpc);
612 }
613
614 /*
615  * Sanity-check a server address provided by the mount command
616  */
617 static int nfs_verify_server_address(struct sockaddr *addr)
618 {
619         switch (addr->sa_family) {
620         case AF_INET: {
621                 struct sockaddr_in *sa = (struct sockaddr_in *) addr;
622                 if (sa->sin_addr.s_addr != INADDR_ANY)
623                         return 1;
624                 break;
625         }
626         }
627
628         return 0;
629 }
630
631 /*
632  * Error-check and convert a string of mount options from user space into
633  * a data structure
634  */
635 static int nfs_parse_mount_options(char *raw,
636                                    struct nfs_parsed_mount_data *mnt)
637 {
638         char *p, *string;
639
640         if (!raw) {
641                 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
642                 return 1;
643         }
644         dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
645
646         while ((p = strsep(&raw, ",")) != NULL) {
647                 substring_t args[MAX_OPT_ARGS];
648                 int option, token;
649
650                 if (!*p)
651                         continue;
652
653                 dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", p);
654
655                 token = match_token(p, nfs_mount_option_tokens, args);
656                 switch (token) {
657                 case Opt_soft:
658                         mnt->flags |= NFS_MOUNT_SOFT;
659                         break;
660                 case Opt_hard:
661                         mnt->flags &= ~NFS_MOUNT_SOFT;
662                         break;
663                 case Opt_intr:
664                         mnt->flags |= NFS_MOUNT_INTR;
665                         break;
666                 case Opt_nointr:
667                         mnt->flags &= ~NFS_MOUNT_INTR;
668                         break;
669                 case Opt_posix:
670                         mnt->flags |= NFS_MOUNT_POSIX;
671                         break;
672                 case Opt_noposix:
673                         mnt->flags &= ~NFS_MOUNT_POSIX;
674                         break;
675                 case Opt_cto:
676                         mnt->flags &= ~NFS_MOUNT_NOCTO;
677                         break;
678                 case Opt_nocto:
679                         mnt->flags |= NFS_MOUNT_NOCTO;
680                         break;
681                 case Opt_ac:
682                         mnt->flags &= ~NFS_MOUNT_NOAC;
683                         break;
684                 case Opt_noac:
685                         mnt->flags |= NFS_MOUNT_NOAC;
686                         break;
687                 case Opt_lock:
688                         mnt->flags &= ~NFS_MOUNT_NONLM;
689                         break;
690                 case Opt_nolock:
691                         mnt->flags |= NFS_MOUNT_NONLM;
692                         break;
693                 case Opt_v2:
694                         mnt->flags &= ~NFS_MOUNT_VER3;
695                         break;
696                 case Opt_v3:
697                         mnt->flags |= NFS_MOUNT_VER3;
698                         break;
699                 case Opt_udp:
700                         mnt->flags &= ~NFS_MOUNT_TCP;
701                         mnt->nfs_server.protocol = IPPROTO_UDP;
702                         mnt->timeo = 7;
703                         mnt->retrans = 5;
704                         break;
705                 case Opt_tcp:
706                         mnt->flags |= NFS_MOUNT_TCP;
707                         mnt->nfs_server.protocol = IPPROTO_TCP;
708                         mnt->timeo = 600;
709                         mnt->retrans = 2;
710                         break;
711                 case Opt_acl:
712                         mnt->flags &= ~NFS_MOUNT_NOACL;
713                         break;
714                 case Opt_noacl:
715                         mnt->flags |= NFS_MOUNT_NOACL;
716                         break;
717                 case Opt_rdirplus:
718                         mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
719                         break;
720                 case Opt_nordirplus:
721                         mnt->flags |= NFS_MOUNT_NORDIRPLUS;
722                         break;
723                 case Opt_sharecache:
724                         mnt->flags &= ~NFS_MOUNT_UNSHARED;
725                         break;
726                 case Opt_nosharecache:
727                         mnt->flags |= NFS_MOUNT_UNSHARED;
728                         break;
729
730                 case Opt_port:
731                         if (match_int(args, &option))
732                                 return 0;
733                         if (option < 0 || option > 65535)
734                                 return 0;
735                         mnt->nfs_server.address.sin_port = htons(option);
736                         break;
737                 case Opt_rsize:
738                         if (match_int(args, &mnt->rsize))
739                                 return 0;
740                         break;
741                 case Opt_wsize:
742                         if (match_int(args, &mnt->wsize))
743                                 return 0;
744                         break;
745                 case Opt_bsize:
746                         if (match_int(args, &option))
747                                 return 0;
748                         if (option < 0)
749                                 return 0;
750                         mnt->bsize = option;
751                         break;
752                 case Opt_timeo:
753                         if (match_int(args, &mnt->timeo))
754                                 return 0;
755                         break;
756                 case Opt_retrans:
757                         if (match_int(args, &mnt->retrans))
758                                 return 0;
759                         break;
760                 case Opt_acregmin:
761                         if (match_int(args, &mnt->acregmin))
762                                 return 0;
763                         break;
764                 case Opt_acregmax:
765                         if (match_int(args, &mnt->acregmax))
766                                 return 0;
767                         break;
768                 case Opt_acdirmin:
769                         if (match_int(args, &mnt->acdirmin))
770                                 return 0;
771                         break;
772                 case Opt_acdirmax:
773                         if (match_int(args, &mnt->acdirmax))
774                                 return 0;
775                         break;
776                 case Opt_actimeo:
777                         if (match_int(args, &option))
778                                 return 0;
779                         if (option < 0)
780                                 return 0;
781                         mnt->acregmin =
782                         mnt->acregmax =
783                         mnt->acdirmin =
784                         mnt->acdirmax = option;
785                         break;
786                 case Opt_namelen:
787                         if (match_int(args, &mnt->namlen))
788                                 return 0;
789                         break;
790                 case Opt_mountport:
791                         if (match_int(args, &option))
792                                 return 0;
793                         if (option < 0 || option > 65535)
794                                 return 0;
795                         mnt->mount_server.port = option;
796                         break;
797                 case Opt_mountprog:
798                         if (match_int(args, &option))
799                                 return 0;
800                         if (option < 0)
801                                 return 0;
802                         mnt->mount_server.program = option;
803                         break;
804                 case Opt_mountvers:
805                         if (match_int(args, &option))
806                                 return 0;
807                         if (option < 0)
808                                 return 0;
809                         mnt->mount_server.version = option;
810                         break;
811                 case Opt_nfsprog:
812                         if (match_int(args, &option))
813                                 return 0;
814                         if (option < 0)
815                                 return 0;
816                         mnt->nfs_server.program = option;
817                         break;
818                 case Opt_nfsvers:
819                         if (match_int(args, &option))
820                                 return 0;
821                         switch (option) {
822                         case 2:
823                                 mnt->flags &= ~NFS_MOUNT_VER3;
824                                 break;
825                         case 3:
826                                 mnt->flags |= NFS_MOUNT_VER3;
827                                 break;
828                         default:
829                                 goto out_unrec_vers;
830                         }
831                         break;
832
833                 case Opt_sec:
834                         string = match_strdup(args);
835                         if (string == NULL)
836                                 goto out_nomem;
837                         token = match_token(string, nfs_secflavor_tokens, args);
838                         kfree(string);
839
840                         /*
841                          * The flags setting is for v2/v3.  The flavor_len
842                          * setting is for v4.  v2/v3 also need to know the
843                          * difference between NULL and UNIX.
844                          */
845                         switch (token) {
846                         case Opt_sec_none:
847                                 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
848                                 mnt->auth_flavor_len = 0;
849                                 mnt->auth_flavors[0] = RPC_AUTH_NULL;
850                                 break;
851                         case Opt_sec_sys:
852                                 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
853                                 mnt->auth_flavor_len = 0;
854                                 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
855                                 break;
856                         case Opt_sec_krb5:
857                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
858                                 mnt->auth_flavor_len = 1;
859                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
860                                 break;
861                         case Opt_sec_krb5i:
862                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
863                                 mnt->auth_flavor_len = 1;
864                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
865                                 break;
866                         case Opt_sec_krb5p:
867                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
868                                 mnt->auth_flavor_len = 1;
869                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
870                                 break;
871                         case Opt_sec_lkey:
872                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
873                                 mnt->auth_flavor_len = 1;
874                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
875                                 break;
876                         case Opt_sec_lkeyi:
877                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
878                                 mnt->auth_flavor_len = 1;
879                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
880                                 break;
881                         case Opt_sec_lkeyp:
882                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
883                                 mnt->auth_flavor_len = 1;
884                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
885                                 break;
886                         case Opt_sec_spkm:
887                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
888                                 mnt->auth_flavor_len = 1;
889                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
890                                 break;
891                         case Opt_sec_spkmi:
892                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
893                                 mnt->auth_flavor_len = 1;
894                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
895                                 break;
896                         case Opt_sec_spkmp:
897                                 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
898                                 mnt->auth_flavor_len = 1;
899                                 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
900                                 break;
901                         default:
902                                 goto out_unrec_sec;
903                         }
904                         break;
905                 case Opt_proto:
906                         string = match_strdup(args);
907                         if (string == NULL)
908                                 goto out_nomem;
909                         token = match_token(string,
910                                             nfs_xprt_protocol_tokens, args);
911                         kfree(string);
912
913                         switch (token) {
914                         case Opt_xprt_udp:
915                                 mnt->flags &= ~NFS_MOUNT_TCP;
916                                 mnt->nfs_server.protocol = IPPROTO_UDP;
917                                 mnt->timeo = 7;
918                                 mnt->retrans = 5;
919                                 break;
920                         case Opt_xprt_tcp:
921                                 mnt->flags |= NFS_MOUNT_TCP;
922                                 mnt->nfs_server.protocol = IPPROTO_TCP;
923                                 mnt->timeo = 600;
924                                 mnt->retrans = 2;
925                                 break;
926                         default:
927                                 goto out_unrec_xprt;
928                         }
929                         break;
930                 case Opt_mountproto:
931                         string = match_strdup(args);
932                         if (string == NULL)
933                                 goto out_nomem;
934                         token = match_token(string,
935                                             nfs_xprt_protocol_tokens, args);
936                         kfree(string);
937
938                         switch (token) {
939                         case Opt_xprt_udp:
940                                 mnt->mount_server.protocol = IPPROTO_UDP;
941                                 break;
942                         case Opt_xprt_tcp:
943                                 mnt->mount_server.protocol = IPPROTO_TCP;
944                                 break;
945                         default:
946                                 goto out_unrec_xprt;
947                         }
948                         break;
949                 case Opt_addr:
950                         string = match_strdup(args);
951                         if (string == NULL)
952                                 goto out_nomem;
953                         mnt->nfs_server.address.sin_family = AF_INET;
954                         mnt->nfs_server.address.sin_addr.s_addr =
955                                                         in_aton(string);
956                         kfree(string);
957                         break;
958                 case Opt_clientaddr:
959                         string = match_strdup(args);
960                         if (string == NULL)
961                                 goto out_nomem;
962                         mnt->client_address = string;
963                         break;
964                 case Opt_mounthost:
965                         string = match_strdup(args);
966                         if (string == NULL)
967                                 goto out_nomem;
968                         mnt->mount_server.address.sin_family = AF_INET;
969                         mnt->mount_server.address.sin_addr.s_addr =
970                                                         in_aton(string);
971                         kfree(string);
972                         break;
973
974                 case Opt_userspace:
975                 case Opt_deprecated:
976                         break;
977
978                 default:
979                         goto out_unknown;
980                 }
981         }
982
983         return 1;
984
985 out_nomem:
986         printk(KERN_INFO "NFS: not enough memory to parse option\n");
987         return 0;
988
989 out_unrec_vers:
990         printk(KERN_INFO "NFS: unrecognized NFS version number\n");
991         return 0;
992
993 out_unrec_xprt:
994         printk(KERN_INFO "NFS: unrecognized transport protocol\n");
995         return 0;
996
997 out_unrec_sec:
998         printk(KERN_INFO "NFS: unrecognized security flavor\n");
999         return 0;
1000
1001 out_unknown:
1002         printk(KERN_INFO "NFS: unknown mount option: %s\n", p);
1003         return 0;
1004 }
1005
1006 /*
1007  * Use the remote server's MOUNT service to request the NFS file handle
1008  * corresponding to the provided path.
1009  */
1010 static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1011                          struct nfs_fh *root_fh)
1012 {
1013         struct sockaddr_in sin;
1014         int status;
1015
1016         if (args->mount_server.version == 0) {
1017                 if (args->flags & NFS_MOUNT_VER3)
1018                         args->mount_server.version = NFS_MNT3_VERSION;
1019                 else
1020                         args->mount_server.version = NFS_MNT_VERSION;
1021         }
1022
1023         /*
1024          * Construct the mount server's address.
1025          */
1026         if (args->mount_server.address.sin_addr.s_addr != INADDR_ANY)
1027                 sin = args->mount_server.address;
1028         else
1029                 sin = args->nfs_server.address;
1030         if (args->mount_server.port != 0)
1031                 sin.sin_port = htons(args->mount_server.port);
1032
1033         /*
1034          * Now ask the mount server to map our export path
1035          * to a file handle.
1036          */
1037         status = nfs_mount((struct sockaddr *) &sin,
1038                            sizeof(sin),
1039                            args->nfs_server.hostname,
1040                            args->nfs_server.export_path,
1041                            args->mount_server.version,
1042                            args->mount_server.protocol,
1043                            root_fh);
1044         if (status == 0)
1045                 return 0;
1046
1047         dfprintk(MOUNT, "NFS: unable to mount server " NIPQUAD_FMT
1048                         ", error %d\n", NIPQUAD(sin.sin_addr.s_addr), status);
1049         return status;
1050 }
1051
1052 /*
1053  * Validate the NFS2/NFS3 mount data
1054  * - fills in the mount root filehandle
1055  *
1056  * For option strings, user space handles the following behaviors:
1057  *
1058  * + DNS: mapping server host name to IP address ("addr=" option)
1059  *
1060  * + failure mode: how to behave if a mount request can't be handled
1061  *   immediately ("fg/bg" option)
1062  *
1063  * + retry: how often to retry a mount request ("retry=" option)
1064  *
1065  * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1066  *   mountproto=tcp after mountproto=udp, and so on
1067  *
1068  * XXX: as far as I can tell, changing the NFS program number is not
1069  *      supported in the NFS client.
1070  */
1071 static int nfs_validate_mount_data(struct nfs_mount_data **options,
1072                                    struct nfs_fh *mntfh,
1073                                    const char *dev_name)
1074 {
1075         struct nfs_mount_data *data = *options;
1076
1077         if (data == NULL)
1078                 goto out_no_data;
1079
1080         switch (data->version) {
1081         case 1:
1082                 data->namlen = 0;
1083         case 2:
1084                 data->bsize = 0;
1085         case 3:
1086                 if (data->flags & NFS_MOUNT_VER3)
1087                         goto out_no_v3;
1088                 data->root.size = NFS2_FHSIZE;
1089                 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1090         case 4:
1091                 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1092                         goto out_no_sec;
1093         case 5:
1094                 memset(data->context, 0, sizeof(data->context));
1095         case 6:
1096                 if (data->flags & NFS_MOUNT_VER3)
1097                         mntfh->size = data->root.size;
1098                 else
1099                         mntfh->size = NFS2_FHSIZE;
1100
1101                 if (mntfh->size > sizeof(mntfh->data))
1102                         goto out_invalid_fh;
1103
1104                 memcpy(mntfh->data, data->root.data, mntfh->size);
1105                 if (mntfh->size < sizeof(mntfh->data))
1106                         memset(mntfh->data + mntfh->size, 0,
1107                                sizeof(mntfh->data) - mntfh->size);
1108                 break;
1109         default: {
1110                 unsigned int len;
1111                 char *c;
1112                 int status;
1113                 struct nfs_parsed_mount_data args = {
1114                         .flags          = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP),
1115                         .rsize          = NFS_MAX_FILE_IO_SIZE,
1116                         .wsize          = NFS_MAX_FILE_IO_SIZE,
1117                         .timeo          = 600,
1118                         .retrans        = 2,
1119                         .acregmin       = 3,
1120                         .acregmax       = 60,
1121                         .acdirmin       = 30,
1122                         .acdirmax       = 60,
1123                         .mount_server.protocol = IPPROTO_UDP,
1124                         .mount_server.program = NFS_MNT_PROGRAM,
1125                         .nfs_server.protocol = IPPROTO_TCP,
1126                         .nfs_server.program = NFS_PROGRAM,
1127                 };
1128
1129                 if (nfs_parse_mount_options((char *) *options, &args) == 0)
1130                         return -EINVAL;
1131
1132                 data = kzalloc(sizeof(*data), GFP_KERNEL);
1133                 if (data == NULL)
1134                         return -ENOMEM;
1135
1136                 /*
1137                  * NB: after this point, caller will free "data"
1138                  * if we return an error
1139                  */
1140                 *options = data;
1141
1142                 c = strchr(dev_name, ':');
1143                 if (c == NULL)
1144                         return -EINVAL;
1145                 len = c - dev_name;
1146                 if (len > sizeof(data->hostname))
1147                         return -ENAMETOOLONG;
1148                 strncpy(data->hostname, dev_name, len);
1149                 args.nfs_server.hostname = data->hostname;
1150
1151                 c++;
1152                 if (strlen(c) > NFS_MAXPATHLEN)
1153                         return -ENAMETOOLONG;
1154                 args.nfs_server.export_path = c;
1155
1156                 status = nfs_try_mount(&args, mntfh);
1157                 if (status)
1158                         return status;
1159
1160                 /*
1161                  * Translate to nfs_mount_data, which nfs_fill_super
1162                  * can deal with.
1163                  */
1164                 data->version           = 6;
1165                 data->flags             = args.flags;
1166                 data->rsize             = args.rsize;
1167                 data->wsize             = args.wsize;
1168                 data->timeo             = args.timeo;
1169                 data->retrans           = args.retrans;
1170                 data->acregmin          = args.acregmin;
1171                 data->acregmax          = args.acregmax;
1172                 data->acdirmin          = args.acdirmin;
1173                 data->acdirmax          = args.acdirmax;
1174                 data->addr              = args.nfs_server.address;
1175                 data->namlen            = args.namlen;
1176                 data->bsize             = args.bsize;
1177                 data->pseudoflavor      = args.auth_flavors[0];
1178
1179                 break;
1180                 }
1181         }
1182
1183         if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
1184                 data->pseudoflavor = RPC_AUTH_UNIX;
1185
1186 #ifndef CONFIG_NFS_V3
1187         if (data->flags & NFS_MOUNT_VER3)
1188                 goto out_v3_not_compiled;
1189 #endif /* !CONFIG_NFS_V3 */
1190
1191         if (!nfs_verify_server_address((struct sockaddr *) &data->addr))
1192                 goto out_no_address;
1193
1194         return 0;
1195
1196 out_no_data:
1197         dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1198         return -EINVAL;
1199
1200 out_no_v3:
1201         dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1202                  data->version);
1203         return -EINVAL;
1204
1205 out_no_sec:
1206         dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1207         return -EINVAL;
1208
1209 #ifndef CONFIG_NFS_V3
1210 out_v3_not_compiled:
1211         dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1212         return -EPROTONOSUPPORT;
1213 #endif /* !CONFIG_NFS_V3 */
1214
1215 out_no_address:
1216         dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1217         return -EINVAL;
1218
1219 out_invalid_fh:
1220         dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1221         return -EINVAL;
1222 }
1223
1224 /*
1225  * Initialise the common bits of the superblock
1226  */
1227 static inline void nfs_initialise_sb(struct super_block *sb)
1228 {
1229         struct nfs_server *server = NFS_SB(sb);
1230
1231         sb->s_magic = NFS_SUPER_MAGIC;
1232
1233         /* We probably want something more informative here */
1234         snprintf(sb->s_id, sizeof(sb->s_id),
1235                  "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
1236
1237         if (sb->s_blocksize == 0)
1238                 sb->s_blocksize = nfs_block_bits(server->wsize,
1239                                                  &sb->s_blocksize_bits);
1240
1241         if (server->flags & NFS_MOUNT_NOAC)
1242                 sb->s_flags |= MS_SYNCHRONOUS;
1243
1244         nfs_super_set_maxbytes(sb, server->maxfilesize);
1245 }
1246
1247 /*
1248  * Finish setting up an NFS2/3 superblock
1249  */
1250 static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
1251 {
1252         struct nfs_server *server = NFS_SB(sb);
1253
1254         sb->s_blocksize_bits = 0;
1255         sb->s_blocksize = 0;
1256         if (data->bsize)
1257                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
1258
1259         if (server->flags & NFS_MOUNT_VER3) {
1260                 /* The VFS shouldn't apply the umask to mode bits. We will do
1261                  * so ourselves when necessary.
1262                  */
1263                 sb->s_flags |= MS_POSIXACL;
1264                 sb->s_time_gran = 1;
1265         }
1266
1267         sb->s_op = &nfs_sops;
1268         nfs_initialise_sb(sb);
1269 }
1270
1271 /*
1272  * Finish setting up a cloned NFS2/3 superblock
1273  */
1274 static void nfs_clone_super(struct super_block *sb,
1275                             const struct super_block *old_sb)
1276 {
1277         struct nfs_server *server = NFS_SB(sb);
1278
1279         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
1280         sb->s_blocksize = old_sb->s_blocksize;
1281         sb->s_maxbytes = old_sb->s_maxbytes;
1282
1283         if (server->flags & NFS_MOUNT_VER3) {
1284                 /* The VFS shouldn't apply the umask to mode bits. We will do
1285                  * so ourselves when necessary.
1286                  */
1287                 sb->s_flags |= MS_POSIXACL;
1288                 sb->s_time_gran = 1;
1289         }
1290
1291         sb->s_op = old_sb->s_op;
1292         nfs_initialise_sb(sb);
1293 }
1294
1295 #define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
1296
1297 static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
1298 {
1299         const struct nfs_server *a = s->s_fs_info;
1300         const struct rpc_clnt *clnt_a = a->client;
1301         const struct rpc_clnt *clnt_b = b->client;
1302
1303         if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
1304                 goto Ebusy;
1305         if (a->nfs_client != b->nfs_client)
1306                 goto Ebusy;
1307         if (a->flags != b->flags)
1308                 goto Ebusy;
1309         if (a->wsize != b->wsize)
1310                 goto Ebusy;
1311         if (a->rsize != b->rsize)
1312                 goto Ebusy;
1313         if (a->acregmin != b->acregmin)
1314                 goto Ebusy;
1315         if (a->acregmax != b->acregmax)
1316                 goto Ebusy;
1317         if (a->acdirmin != b->acdirmin)
1318                 goto Ebusy;
1319         if (a->acdirmax != b->acdirmax)
1320                 goto Ebusy;
1321         if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
1322                 goto Ebusy;
1323         return 1;
1324 Ebusy:
1325         return 0;
1326 }
1327
1328 struct nfs_sb_mountdata {
1329         struct nfs_server *server;
1330         int mntflags;
1331 };
1332
1333 static int nfs_set_super(struct super_block *s, void *data)
1334 {
1335         struct nfs_sb_mountdata *sb_mntdata = data;
1336         struct nfs_server *server = sb_mntdata->server;
1337         int ret;
1338
1339         s->s_flags = sb_mntdata->mntflags;
1340         s->s_fs_info = server;
1341         ret = set_anon_super(s, server);
1342         if (ret == 0)
1343                 server->s_dev = s->s_dev;
1344         return ret;
1345 }
1346
1347 static int nfs_compare_super(struct super_block *sb, void *data)
1348 {
1349         struct nfs_sb_mountdata *sb_mntdata = data;
1350         struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
1351         int mntflags = sb_mntdata->mntflags;
1352
1353         if (memcmp(&old->nfs_client->cl_addr,
1354                                 &server->nfs_client->cl_addr,
1355                                 sizeof(old->nfs_client->cl_addr)) != 0)
1356                 return 0;
1357         /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
1358         if (old->flags & NFS_MOUNT_UNSHARED)
1359                 return 0;
1360         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
1361                 return 0;
1362         return nfs_compare_mount_options(sb, server, mntflags);
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         struct nfs_sb_mountdata sb_mntdata = {
1375                 .mntflags = flags,
1376         };
1377         int error;
1378
1379         /* Validate the mount data */
1380         error = nfs_validate_mount_data(&data, &mntfh, dev_name);
1381         if (error < 0)
1382                 goto out;
1383
1384         /* Get a volume representation */
1385         server = nfs_create_server(data, &mntfh);
1386         if (IS_ERR(server)) {
1387                 error = PTR_ERR(server);
1388                 goto out;
1389         }
1390         sb_mntdata.server = server;
1391
1392         if (server->flags & NFS_MOUNT_UNSHARED)
1393                 compare_super = NULL;
1394
1395         /* Get a superblock - note that we may end up sharing one that already exists */
1396         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
1397         if (IS_ERR(s)) {
1398                 error = PTR_ERR(s);
1399                 goto out_err_nosb;
1400         }
1401
1402         if (s->s_fs_info != server) {
1403                 nfs_free_server(server);
1404                 server = NULL;
1405         }
1406
1407         if (!s->s_root) {
1408                 /* initial superblock/root creation */
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         struct nfs_sb_mountdata sb_mntdata = {
1462                 .mntflags = flags,
1463         };
1464         int error;
1465
1466         dprintk("--> nfs_xdev_get_sb()\n");
1467
1468         /* create a new volume representation */
1469         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
1470         if (IS_ERR(server)) {
1471                 error = PTR_ERR(server);
1472                 goto out_err_noserver;
1473         }
1474         sb_mntdata.server = server;
1475
1476         if (server->flags & NFS_MOUNT_UNSHARED)
1477                 compare_super = NULL;
1478
1479         /* Get a superblock - note that we may end up sharing one that already exists */
1480         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
1481         if (IS_ERR(s)) {
1482                 error = PTR_ERR(s);
1483                 goto out_err_nosb;
1484         }
1485
1486         if (s->s_fs_info != server) {
1487                 nfs_free_server(server);
1488                 server = NULL;
1489         }
1490
1491         if (!s->s_root) {
1492                 /* initial superblock/root creation */
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 -ENAMETOOLONG;
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 -ENAMETOOLONG;
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                 if (args.client_address == NULL)
1687                         goto out_no_client_address;
1688
1689                 *ip_addr = args.client_address;
1690
1691                 break;
1692                 }
1693         }
1694
1695         return 0;
1696
1697 out_no_data:
1698         dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
1699         return -EINVAL;
1700
1701 out_inval_auth:
1702         dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
1703                  data->auth_flavourlen);
1704         return -EINVAL;
1705
1706 out_no_address:
1707         dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
1708         return -EINVAL;
1709
1710 out_no_client_address:
1711         dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
1712         return -EINVAL;
1713 }
1714
1715 /*
1716  * Get the superblock for an NFS4 mountpoint
1717  */
1718 static int nfs4_get_sb(struct file_system_type *fs_type,
1719         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
1720 {
1721         struct nfs4_mount_data *data = raw_data;
1722         struct super_block *s;
1723         struct nfs_server *server;
1724         struct sockaddr_in addr;
1725         rpc_authflavor_t authflavour;
1726         struct nfs_fh mntfh;
1727         struct dentry *mntroot;
1728         char *mntpath = NULL, *hostname = NULL, *ip_addr = NULL;
1729         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1730         struct nfs_sb_mountdata sb_mntdata = {
1731                 .mntflags = flags,
1732         };
1733         int error;
1734
1735         /* Validate the mount data */
1736         error = nfs4_validate_mount_data(&data, dev_name, &addr, &authflavour,
1737                                          &hostname, &mntpath, &ip_addr);
1738         if (error < 0)
1739                 goto out;
1740
1741         /* Get a volume representation */
1742         server = nfs4_create_server(data, hostname, &addr, mntpath, ip_addr,
1743                                     authflavour, &mntfh);
1744         if (IS_ERR(server)) {
1745                 error = PTR_ERR(server);
1746                 goto out;
1747         }
1748         sb_mntdata.server = server;
1749
1750         if (server->flags & NFS4_MOUNT_UNSHARED)
1751                 compare_super = NULL;
1752
1753         /* Get a superblock - note that we may end up sharing one that already exists */
1754         s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
1755         if (IS_ERR(s)) {
1756                 error = PTR_ERR(s);
1757                 goto out_free;
1758         }
1759
1760         if (s->s_fs_info != server) {
1761                 nfs_free_server(server);
1762                 server = NULL;
1763         }
1764
1765         if (!s->s_root) {
1766                 /* initial superblock/root creation */
1767                 nfs4_fill_super(s);
1768         }
1769
1770         mntroot = nfs4_get_root(s, &mntfh);
1771         if (IS_ERR(mntroot)) {
1772                 error = PTR_ERR(mntroot);
1773                 goto error_splat_super;
1774         }
1775
1776         s->s_flags |= MS_ACTIVE;
1777         mnt->mnt_sb = s;
1778         mnt->mnt_root = mntroot;
1779         error = 0;
1780
1781 out:
1782         kfree(ip_addr);
1783         kfree(mntpath);
1784         kfree(hostname);
1785         return error;
1786
1787 out_free:
1788         nfs_free_server(server);
1789         goto out;
1790
1791 error_splat_super:
1792         up_write(&s->s_umount);
1793         deactivate_super(s);
1794         goto out;
1795 }
1796
1797 static void nfs4_kill_super(struct super_block *sb)
1798 {
1799         struct nfs_server *server = NFS_SB(sb);
1800
1801         nfs_return_all_delegations(sb);
1802         kill_anon_super(sb);
1803
1804         nfs4_renewd_prepare_shutdown(server);
1805         nfs_free_server(server);
1806 }
1807
1808 /*
1809  * Clone an NFS4 server record on xdev traversal (FSID-change)
1810  */
1811 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
1812                             const char *dev_name, void *raw_data,
1813                             struct vfsmount *mnt)
1814 {
1815         struct nfs_clone_mount *data = raw_data;
1816         struct super_block *s;
1817         struct nfs_server *server;
1818         struct dentry *mntroot;
1819         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1820         struct nfs_sb_mountdata sb_mntdata = {
1821                 .mntflags = flags,
1822         };
1823         int error;
1824
1825         dprintk("--> nfs4_xdev_get_sb()\n");
1826
1827         /* create a new volume representation */
1828         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
1829         if (IS_ERR(server)) {
1830                 error = PTR_ERR(server);
1831                 goto out_err_noserver;
1832         }
1833         sb_mntdata.server = server;
1834
1835         if (server->flags & NFS4_MOUNT_UNSHARED)
1836                 compare_super = NULL;
1837
1838         /* Get a superblock - note that we may end up sharing one that already exists */
1839         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
1840         if (IS_ERR(s)) {
1841                 error = PTR_ERR(s);
1842                 goto out_err_nosb;
1843         }
1844
1845         if (s->s_fs_info != server) {
1846                 nfs_free_server(server);
1847                 server = NULL;
1848         }
1849
1850         if (!s->s_root) {
1851                 /* initial superblock/root creation */
1852                 nfs4_clone_super(s, data->sb);
1853         }
1854
1855         mntroot = nfs4_get_root(s, data->fh);
1856         if (IS_ERR(mntroot)) {
1857                 error = PTR_ERR(mntroot);
1858                 goto error_splat_super;
1859         }
1860
1861         s->s_flags |= MS_ACTIVE;
1862         mnt->mnt_sb = s;
1863         mnt->mnt_root = mntroot;
1864
1865         dprintk("<-- nfs4_xdev_get_sb() = 0\n");
1866         return 0;
1867
1868 out_err_nosb:
1869         nfs_free_server(server);
1870 out_err_noserver:
1871         dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
1872         return error;
1873
1874 error_splat_super:
1875         up_write(&s->s_umount);
1876         deactivate_super(s);
1877         dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
1878         return error;
1879 }
1880
1881 /*
1882  * Create an NFS4 server record on referral traversal
1883  */
1884 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
1885                                 const char *dev_name, void *raw_data,
1886                                 struct vfsmount *mnt)
1887 {
1888         struct nfs_clone_mount *data = raw_data;
1889         struct super_block *s;
1890         struct nfs_server *server;
1891         struct dentry *mntroot;
1892         struct nfs_fh mntfh;
1893         int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
1894         struct nfs_sb_mountdata sb_mntdata = {
1895                 .mntflags = flags,
1896         };
1897         int error;
1898
1899         dprintk("--> nfs4_referral_get_sb()\n");
1900
1901         /* create a new volume representation */
1902         server = nfs4_create_referral_server(data, &mntfh);
1903         if (IS_ERR(server)) {
1904                 error = PTR_ERR(server);
1905                 goto out_err_noserver;
1906         }
1907         sb_mntdata.server = server;
1908
1909         if (server->flags & NFS4_MOUNT_UNSHARED)
1910                 compare_super = NULL;
1911
1912         /* Get a superblock - note that we may end up sharing one that already exists */
1913         s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
1914         if (IS_ERR(s)) {
1915                 error = PTR_ERR(s);
1916                 goto out_err_nosb;
1917         }
1918
1919         if (s->s_fs_info != server) {
1920                 nfs_free_server(server);
1921                 server = NULL;
1922         }
1923
1924         if (!s->s_root) {
1925                 /* initial superblock/root creation */
1926                 nfs4_fill_super(s);
1927         }
1928
1929         mntroot = nfs4_get_root(s, &mntfh);
1930         if (IS_ERR(mntroot)) {
1931                 error = PTR_ERR(mntroot);
1932                 goto error_splat_super;
1933         }
1934
1935         s->s_flags |= MS_ACTIVE;
1936         mnt->mnt_sb = s;
1937         mnt->mnt_root = mntroot;
1938
1939         dprintk("<-- nfs4_referral_get_sb() = 0\n");
1940         return 0;
1941
1942 out_err_nosb:
1943         nfs_free_server(server);
1944 out_err_noserver:
1945         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
1946         return error;
1947
1948 error_splat_super:
1949         up_write(&s->s_umount);
1950         deactivate_super(s);
1951         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
1952         return error;
1953 }
1954
1955 #endif /* CONFIG_NFS_V4 */