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