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