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