This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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/vserver/xid.h>
48
49 #include <asm/system.h>
50 #include <asm/uaccess.h>
51
52 #include "nfs4_fs.h"
53 #include "callback.h"
54 #include "delegation.h"
55 #include "iostat.h"
56 #include "internal.h"
57
58 #define NFSDBG_FACILITY         NFSDBG_VFS
59
60 static void nfs_umount_begin(struct vfsmount *, int);
61 static int  nfs_statfs(struct dentry *, struct kstatfs *);
62 static int  nfs_show_options(struct seq_file *, struct vfsmount *);
63 static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
64 static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
65 static int nfs_xdev_get_sb(struct file_system_type *fs_type,
66                 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
67 static void nfs_kill_super(struct super_block *);
68
69 static struct file_system_type nfs_fs_type = {
70         .owner          = THIS_MODULE,
71         .name           = "nfs",
72         .get_sb         = nfs_get_sb,
73         .kill_sb        = nfs_kill_super,
74         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
75 };
76
77 struct file_system_type nfs_xdev_fs_type = {
78         .owner          = THIS_MODULE,
79         .name           = "nfs",
80         .get_sb         = nfs_xdev_get_sb,
81         .kill_sb        = nfs_kill_super,
82         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
83 };
84
85 static struct super_operations nfs_sops = {
86         .alloc_inode    = nfs_alloc_inode,
87         .destroy_inode  = nfs_destroy_inode,
88         .write_inode    = nfs_write_inode,
89         .statfs         = nfs_statfs,
90         .clear_inode    = nfs_clear_inode,
91         .umount_begin   = nfs_umount_begin,
92         .show_options   = nfs_show_options,
93         .show_stats     = nfs_show_stats,
94 };
95
96 #ifdef CONFIG_NFS_V4
97 static int nfs4_get_sb(struct file_system_type *fs_type,
98         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
99 static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
100         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
101 static int nfs4_referral_get_sb(struct file_system_type *fs_type,
102         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
103 static void nfs4_kill_super(struct super_block *sb);
104
105 static struct file_system_type nfs4_fs_type = {
106         .owner          = THIS_MODULE,
107         .name           = "nfs4",
108         .get_sb         = nfs4_get_sb,
109         .kill_sb        = nfs4_kill_super,
110         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
111 };
112
113 struct file_system_type nfs4_xdev_fs_type = {
114         .owner          = THIS_MODULE,
115         .name           = "nfs4",
116         .get_sb         = nfs4_xdev_get_sb,
117         .kill_sb        = nfs4_kill_super,
118         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
119 };
120
121 struct file_system_type nfs4_referral_fs_type = {
122         .owner          = THIS_MODULE,
123         .name           = "nfs4",
124         .get_sb         = nfs4_referral_get_sb,
125         .kill_sb        = nfs4_kill_super,
126         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
127 };
128
129 static struct super_operations nfs4_sops = {
130         .alloc_inode    = nfs_alloc_inode,
131         .destroy_inode  = nfs_destroy_inode,
132         .write_inode    = nfs_write_inode,
133         .statfs         = nfs_statfs,
134         .clear_inode    = nfs4_clear_inode,
135         .umount_begin   = nfs_umount_begin,
136         .show_options   = nfs_show_options,
137         .show_stats     = nfs_show_stats,
138 };
139 #endif
140
141 /*
142  * Register the NFS filesystems
143  */
144 int __init register_nfs_fs(void)
145 {
146         int ret;
147
148         ret = register_filesystem(&nfs_fs_type);
149         if (ret < 0)
150                 goto error_0;
151
152 #ifdef CONFIG_NFS_V4
153         ret = nfs_register_sysctl();
154         if (ret < 0)
155                 goto error_1;
156         ret = register_filesystem(&nfs4_fs_type);
157         if (ret < 0)
158                 goto error_2;
159 #endif
160         return 0;
161
162 #ifdef CONFIG_NFS_V4
163 error_2:
164         nfs_unregister_sysctl();
165 error_1:
166         unregister_filesystem(&nfs_fs_type);
167 #endif
168 error_0:
169         return ret;
170 }
171
172 /*
173  * Unregister the NFS filesystems
174  */
175 void __exit unregister_nfs_fs(void)
176 {
177 #ifdef CONFIG_NFS_V4
178         unregister_filesystem(&nfs4_fs_type);
179         nfs_unregister_sysctl();
180 #endif
181         unregister_filesystem(&nfs_fs_type);
182 }
183
184 /*
185  * Deliver file system statistics to userspace
186  */
187 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
188 {
189         struct nfs_server *server = NFS_SB(dentry->d_sb);
190         unsigned char blockbits;
191         unsigned long blockres;
192         struct nfs_fh *fh = NFS_FH(dentry->d_inode);
193         struct nfs_fattr fattr;
194         struct nfs_fsstat res = {
195                         .fattr = &fattr,
196         };
197         int error;
198
199         lock_kernel();
200
201         error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
202         buf->f_type = NFS_SUPER_MAGIC;
203         if (error < 0)
204                 goto out_err;
205
206         /*
207          * Current versions of glibc do not correctly handle the
208          * case where f_frsize != f_bsize.  Eventually we want to
209          * report the value of wtmult in this field.
210          */
211         buf->f_frsize = dentry->d_sb->s_blocksize;
212
213         /*
214          * On most *nix systems, f_blocks, f_bfree, and f_bavail
215          * are reported in units of f_frsize.  Linux hasn't had
216          * an f_frsize field in its statfs struct until recently,
217          * thus historically Linux's sys_statfs reports these
218          * fields in units of f_bsize.
219          */
220         buf->f_bsize = dentry->d_sb->s_blocksize;
221         blockbits = dentry->d_sb->s_blocksize_bits;
222         blockres = (1 << blockbits) - 1;
223         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
224         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
225         buf->f_bavail = (res.abytes + blockres) >> blockbits;
226
227         buf->f_files = res.tfiles;
228         buf->f_ffree = res.afiles;
229
230         buf->f_namelen = server->namelen;
231  out:
232         unlock_kernel();
233         return 0;
234
235  out_err:
236         dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
237         buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
238         goto out;
239
240 }
241
242 /*
243  * Map the security flavour number to a name
244  */
245 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
246 {
247         static const struct {
248                 rpc_authflavor_t flavour;
249                 const char *str;
250         } sec_flavours[] = {
251                 { RPC_AUTH_NULL, "null" },
252                 { RPC_AUTH_UNIX, "sys" },
253                 { RPC_AUTH_GSS_KRB5, "krb5" },
254                 { RPC_AUTH_GSS_KRB5I, "krb5i" },
255                 { RPC_AUTH_GSS_KRB5P, "krb5p" },
256                 { RPC_AUTH_GSS_LKEY, "lkey" },
257                 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
258                 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
259                 { RPC_AUTH_GSS_SPKM, "spkm" },
260                 { RPC_AUTH_GSS_SPKMI, "spkmi" },
261                 { RPC_AUTH_GSS_SPKMP, "spkmp" },
262                 { -1, "unknown" }
263         };
264         int i;
265
266         for (i=0; sec_flavours[i].flavour != -1; i++) {
267                 if (sec_flavours[i].flavour == flavour)
268                         break;
269         }
270         return sec_flavours[i].str;
271 }
272
273 /*
274  * Describe the mount options in force on this server representation
275  */
276 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
277 {
278         static const struct proc_nfs_info {
279                 int flag;
280                 const char *str;
281                 const char *nostr;
282         } nfs_info[] = {
283                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
284                 { NFS_MOUNT_INTR, ",intr", "" },
285                 { NFS_MOUNT_NOCTO, ",nocto", "" },
286                 { NFS_MOUNT_NOAC, ",noac", "" },
287                 { NFS_MOUNT_NONLM, ",nolock", "" },
288                 { NFS_MOUNT_NOACL, ",noacl", "" },
289                 { NFS_MOUNT_FSCACHE, ",fsc", "" },
290                 { NFS_MOUNT_TAGXID, ",tagxid", "" },
291                 { 0, NULL, NULL }
292         };
293         const struct proc_nfs_info *nfs_infop;
294         struct nfs_client *clp = nfss->nfs_client;
295         char buf[12];
296         const char *proto;
297
298         seq_printf(m, ",vers=%d", clp->rpc_ops->version);
299         seq_printf(m, ",rsize=%d", nfss->rsize);
300         seq_printf(m, ",wsize=%d", nfss->wsize);
301         if (nfss->acregmin != 3*HZ || showdefaults)
302                 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
303         if (nfss->acregmax != 60*HZ || showdefaults)
304                 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
305         if (nfss->acdirmin != 30*HZ || showdefaults)
306                 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
307         if (nfss->acdirmax != 60*HZ || showdefaults)
308                 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
309         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
310                 if (nfss->flags & nfs_infop->flag)
311                         seq_puts(m, nfs_infop->str);
312                 else
313                         seq_puts(m, nfs_infop->nostr);
314         }
315         switch (nfss->client->cl_xprt->prot) {
316                 case IPPROTO_TCP:
317                         proto = "tcp";
318                         break;
319                 case IPPROTO_UDP:
320                         proto = "udp";
321                         break;
322                 default:
323                         snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
324                         proto = buf;
325         }
326         seq_printf(m, ",proto=%s", proto);
327         seq_printf(m, ",timeo=%lu", 10U * clp->retrans_timeo / HZ);
328         seq_printf(m, ",retrans=%u", clp->retrans_count);
329         seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
330 }
331
332 /*
333  * Describe the mount options on this VFS mountpoint
334  */
335 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
336 {
337         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
338
339         nfs_show_mount_options(m, nfss, 0);
340
341         seq_puts(m, ",addr=");
342         seq_escape(m, nfss->nfs_client->cl_hostname, " \t\n\\");
343
344         return 0;
345 }
346
347 /*
348  * Present statistical information for this VFS mountpoint
349  */
350 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
351 {
352         int i, cpu;
353         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
354         struct rpc_auth *auth = nfss->client->cl_auth;
355         struct nfs_iostats totals = { };
356
357         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
358
359         /*
360          * Display all mount option settings
361          */
362         seq_printf(m, "\n\topts:\t");
363         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
364         seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
365         seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
366         seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
367         nfs_show_mount_options(m, nfss, 1);
368
369         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
370
371         seq_printf(m, "\n\tcaps:\t");
372         seq_printf(m, "caps=0x%x", nfss->caps);
373         seq_printf(m, ",wtmult=%d", nfss->wtmult);
374         seq_printf(m, ",dtsize=%d", nfss->dtsize);
375         seq_printf(m, ",bsize=%d", nfss->bsize);
376         seq_printf(m, ",namelen=%d", nfss->namelen);
377
378 #ifdef CONFIG_NFS_V4
379         if (nfss->nfs_client->cl_nfsversion == 4) {
380                 seq_printf(m, "\n\tnfsv4:\t");
381                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
382                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
383                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
384         }
385 #endif
386
387         /*
388          * Display security flavor in effect for this mount
389          */
390         seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
391         if (auth->au_flavor)
392                 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
393
394         /*
395          * Display superblock I/O counters
396          */
397         for_each_possible_cpu(cpu) {
398                 struct nfs_iostats *stats;
399
400                 preempt_disable();
401                 stats = per_cpu_ptr(nfss->io_stats, cpu);
402
403                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
404                         totals.events[i] += stats->events[i];
405                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
406                         totals.bytes[i] += stats->bytes[i];
407
408                 preempt_enable();
409         }
410
411         seq_printf(m, "\n\tevents:\t");
412         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
413                 seq_printf(m, "%lu ", totals.events[i]);
414         seq_printf(m, "\n\tbytes:\t");
415         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
416                 seq_printf(m, "%Lu ", totals.bytes[i]);
417         seq_printf(m, "\n");
418
419         rpc_print_iostats(m, nfss->client);
420
421         return 0;
422 }
423
424 /*
425  * Begin unmount by attempting to remove all automounted mountpoints we added
426  * in response to xdev traversals and referrals
427  */
428 static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
429 {
430         shrink_submounts(vfsmnt, &nfs_automount_list);
431 }
432
433 /*
434  * Validate the NFS2/NFS3 mount data
435  * - fills in the mount root filehandle
436  */
437 static int nfs_validate_mount_data(struct nfs_mount_data *data,
438                                    struct nfs_fh *mntfh)
439 {
440         if (data == NULL) {
441                 dprintk("%s: missing data argument\n", __FUNCTION__);
442                 return -EINVAL;
443         }
444
445         if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
446                 dprintk("%s: bad mount version\n", __FUNCTION__);
447                 return -EINVAL;
448         }
449
450         switch (data->version) {
451                 case 1:
452                         data->namlen = 0;
453                 case 2:
454                         data->bsize  = 0;
455                 case 3:
456                         if (data->flags & NFS_MOUNT_VER3) {
457                                 dprintk("%s: mount structure version %d does not support NFSv3\n",
458                                                 __FUNCTION__,
459                                                 data->version);
460                                 return -EINVAL;
461                         }
462                         data->root.size = NFS2_FHSIZE;
463                         memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
464                 case 4:
465                         if (data->flags & NFS_MOUNT_SECFLAVOUR) {
466                                 dprintk("%s: mount structure version %d does not support strong security\n",
467                                                 __FUNCTION__,
468                                                 data->version);
469                                 return -EINVAL;
470                         }
471                         /* Fill in pseudoflavor for mount version < 5 */
472                         data->pseudoflavor = RPC_AUTH_UNIX;
473                 case 5:
474                         memset(data->context, 0, sizeof(data->context));
475         }
476
477 #ifndef CONFIG_NFS_V3
478         /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
479         if (data->flags & NFS_MOUNT_VER3) {
480                 dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
481                 return -EPROTONOSUPPORT;
482         }
483 #endif /* CONFIG_NFS_V3 */
484
485         /* We now require that the mount process passes the remote address */
486         if (data->addr.sin_addr.s_addr == INADDR_ANY) {
487                 dprintk("%s: mount program didn't pass remote address!\n",
488                         __FUNCTION__);
489                 return -EINVAL;
490         }
491
492         /* Prepare the root filehandle */
493         if (data->flags & NFS_MOUNT_VER3)
494                 mntfh->size = data->root.size;
495         else
496                 mntfh->size = NFS2_FHSIZE;
497
498         if (mntfh->size > sizeof(mntfh->data)) {
499                 dprintk("%s: invalid root filehandle\n", __FUNCTION__);
500                 return -EINVAL;
501         }
502
503         memcpy(mntfh->data, data->root.data, mntfh->size);
504         if (mntfh->size < sizeof(mntfh->data))
505                 memset(mntfh->data + mntfh->size, 0,
506                        sizeof(mntfh->data) - mntfh->size);
507
508         return 0;
509 }
510
511 /*
512  * Initialise the common bits of the superblock
513  */
514 static inline void nfs_initialise_sb(struct super_block *sb)
515 {
516         struct nfs_server *server = NFS_SB(sb);
517
518         sb->s_magic = NFS_SUPER_MAGIC;
519
520         /* We probably want something more informative here */
521         snprintf(sb->s_id, sizeof(sb->s_id),
522                  "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
523
524         if (sb->s_blocksize == 0)
525                 sb->s_blocksize = nfs_block_bits(server->wsize,
526                                                  &sb->s_blocksize_bits);
527
528         if (server->flags & NFS_MOUNT_NOAC)
529                 sb->s_flags |= MS_SYNCHRONOUS;
530         if (server->flags & NFS_MOUNT_TAGXID)
531                 sb->s_flags |= MS_TAGXID;
532
533         nfs_super_set_maxbytes(sb, server->maxfilesize);
534 }
535
536 /*
537  * Finish setting up an NFS2/3 superblock
538  */
539 static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
540 {
541         struct nfs_server *server = NFS_SB(sb);
542
543         sb->s_blocksize_bits = 0;
544         sb->s_blocksize = 0;
545         if (data->bsize)
546                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
547
548         if (server->flags & NFS_MOUNT_VER3) {
549                 /* The VFS shouldn't apply the umask to mode bits. We will do
550                  * so ourselves when necessary.
551                  */
552                 sb->s_flags |= MS_POSIXACL;
553                 sb->s_time_gran = 1;
554         }
555
556         sb->s_op = &nfs_sops;
557         nfs_initialise_sb(sb);
558 }
559
560 /*
561  * Finish setting up a cloned NFS2/3 superblock
562  */
563 static void nfs_clone_super(struct super_block *sb,
564                             const struct super_block *old_sb)
565 {
566         struct nfs_server *server = NFS_SB(sb);
567
568         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
569         sb->s_blocksize = old_sb->s_blocksize;
570         sb->s_maxbytes = old_sb->s_maxbytes;
571
572         if (server->flags & NFS_MOUNT_VER3) {
573                 /* The VFS shouldn't apply the umask to mode bits. We will do
574                  * so ourselves when necessary.
575                  */
576                 sb->s_flags |= MS_POSIXACL;
577                 sb->s_time_gran = 1;
578         }
579
580         sb->s_op = old_sb->s_op;
581         nfs_initialise_sb(sb);
582 }
583
584 static int nfs_set_super(struct super_block *s, void *_server)
585 {
586         struct nfs_server *server = _server;
587         int ret;
588
589         s->s_fs_info = server;
590         ret = set_anon_super(s, server);
591         if (ret == 0)
592                 server->s_dev = s->s_dev;
593         return ret;
594 }
595
596 static int nfs_compare_super(struct super_block *sb, void *data)
597 {
598         struct nfs_server *server = data, *old = NFS_SB(sb);
599
600         if (old->nfs_client != server->nfs_client)
601                 return 0;
602         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
603                 return 0;
604         return 1;
605 }
606
607 static int nfs_get_sb(struct file_system_type *fs_type,
608         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
609 {
610         struct nfs_server *server = NULL;
611         struct super_block *s;
612         struct nfs_fh mntfh;
613         struct nfs_mount_data *data = raw_data;
614         struct dentry *mntroot;
615         int error;
616
617         /* Validate the mount data */
618         error = nfs_validate_mount_data(data, &mntfh);
619         if (error < 0)
620                 return error;
621
622         /* Get a volume representation */
623         server = nfs_create_server(data, &mntfh);
624         if (IS_ERR(server)) {
625                 error = PTR_ERR(server);
626                 goto out_err_noserver;
627         }
628
629         /* Get a superblock - note that we may end up sharing one that already exists */
630         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
631         if (IS_ERR(s)) {
632                 error = PTR_ERR(s);
633                 goto out_err_nosb;
634         }
635
636         if (s->s_fs_info != server) {
637                 nfs_free_server(server);
638                 server = NULL;
639         }
640
641         if (!s->s_root) {
642                 /* initial superblock/root creation */
643                 s->s_flags = flags;
644                 nfs_fill_super(s, data);
645         }
646
647         mntroot = nfs_get_root(s, &mntfh);
648         if (IS_ERR(mntroot)) {
649                 error = PTR_ERR(mntroot);
650                 goto error_splat_super;
651         }
652
653         s->s_flags |= MS_ACTIVE;
654         mnt->mnt_sb = s;
655         mnt->mnt_root = mntroot;
656         return 0;
657
658 out_err_nosb:
659         nfs_free_server(server);
660 out_err_noserver:
661         return error;
662
663 error_splat_super:
664         up_write(&s->s_umount);
665         deactivate_super(s);
666         return error;
667 }
668
669 /*
670  * Destroy an NFS2/3 superblock
671  */
672 static void nfs_kill_super(struct super_block *s)
673 {
674         struct nfs_server *server = NFS_SB(s);
675
676         kill_anon_super(s);
677         nfs_free_server(server);
678 }
679
680 /*
681  * Clone an NFS2/3 server record on xdev traversal (FSID-change)
682  */
683 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
684                            const char *dev_name, void *raw_data,
685                            struct vfsmount *mnt)
686 {
687         struct nfs_clone_mount *data = raw_data;
688         struct super_block *s;
689         struct nfs_server *server;
690         struct dentry *mntroot;
691         int error;
692
693         dprintk("--> nfs_xdev_get_sb()\n");
694
695         /* create a new volume representation */
696         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
697         if (IS_ERR(server)) {
698                 error = PTR_ERR(server);
699                 goto out_err_noserver;
700         }
701
702         /* Get a superblock - note that we may end up sharing one that already exists */
703         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
704         if (IS_ERR(s)) {
705                 error = PTR_ERR(s);
706                 goto out_err_nosb;
707         }
708
709         if (s->s_fs_info != server) {
710                 nfs_free_server(server);
711                 server = NULL;
712         }
713
714         if (!s->s_root) {
715                 /* initial superblock/root creation */
716                 s->s_flags = flags;
717                 nfs_clone_super(s, data->sb);
718         }
719
720         mntroot = nfs_get_root(s, data->fh);
721         if (IS_ERR(mntroot)) {
722                 error = PTR_ERR(mntroot);
723                 goto error_splat_super;
724         }
725
726         s->s_flags |= MS_ACTIVE;
727         mnt->mnt_sb = s;
728         mnt->mnt_root = mntroot;
729
730         dprintk("<-- nfs_xdev_get_sb() = 0\n");
731         return 0;
732
733 out_err_nosb:
734         nfs_free_server(server);
735 out_err_noserver:
736         dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
737         return error;
738
739 error_splat_super:
740         up_write(&s->s_umount);
741         deactivate_super(s);
742         dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
743         return error;
744 }
745
746 #ifdef CONFIG_NFS_V4
747
748 /*
749  * Finish setting up a cloned NFS4 superblock
750  */
751 static void nfs4_clone_super(struct super_block *sb,
752                             const struct super_block *old_sb)
753 {
754         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
755         sb->s_blocksize = old_sb->s_blocksize;
756         sb->s_maxbytes = old_sb->s_maxbytes;
757         sb->s_time_gran = 1;
758         sb->s_op = old_sb->s_op;
759         nfs_initialise_sb(sb);
760 }
761
762 /*
763  * Set up an NFS4 superblock
764  */
765 static void nfs4_fill_super(struct super_block *sb)
766 {
767         sb->s_time_gran = 1;
768         sb->s_op = &nfs4_sops;
769         nfs_initialise_sb(sb);
770 }
771
772 static void *nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
773 {
774         void *p = NULL;
775
776         if (!src->len)
777                 return ERR_PTR(-EINVAL);
778         if (src->len < maxlen)
779                 maxlen = src->len;
780         if (dst == NULL) {
781                 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
782                 if (p == NULL)
783                         return ERR_PTR(-ENOMEM);
784         }
785         if (copy_from_user(dst, src->data, maxlen)) {
786                 kfree(p);
787                 return ERR_PTR(-EFAULT);
788         }
789         dst[maxlen] = '\0';
790         return dst;
791 }
792
793 /*
794  * Get the superblock for an NFS4 mountpoint
795  */
796 static int nfs4_get_sb(struct file_system_type *fs_type,
797         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
798 {
799         struct nfs4_mount_data *data = raw_data;
800         struct super_block *s;
801         struct nfs_server *server;
802         struct sockaddr_in addr;
803         rpc_authflavor_t authflavour;
804         struct nfs_fh mntfh;
805         struct dentry *mntroot;
806         char *mntpath = NULL, *hostname = NULL, ip_addr[16];
807         void *p;
808         int error;
809
810         if (data == NULL) {
811                 dprintk("%s: missing data argument\n", __FUNCTION__);
812                 return -EINVAL;
813         }
814         if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
815                 dprintk("%s: bad mount version\n", __FUNCTION__);
816                 return -EINVAL;
817         }
818
819         /* We now require that the mount process passes the remote address */
820         if (data->host_addrlen != sizeof(addr))
821                 return -EINVAL;
822
823         if (copy_from_user(&addr, data->host_addr, sizeof(addr)))
824                 return -EFAULT;
825
826         if (addr.sin_family != AF_INET ||
827             addr.sin_addr.s_addr == INADDR_ANY
828             ) {
829                 dprintk("%s: mount program didn't pass remote IP address!\n",
830                                 __FUNCTION__);
831                 return -EINVAL;
832         }
833
834         /* Grab the authentication type */
835         authflavour = RPC_AUTH_UNIX;
836         if (data->auth_flavourlen != 0) {
837                 if (data->auth_flavourlen != 1) {
838                         dprintk("%s: Invalid number of RPC auth flavours %d.\n",
839                                         __FUNCTION__, data->auth_flavourlen);
840                         error = -EINVAL;
841                         goto out_err_noserver;
842                 }
843
844                 if (copy_from_user(&authflavour, data->auth_flavours,
845                                    sizeof(authflavour))) {
846                         error = -EFAULT;
847                         goto out_err_noserver;
848                 }
849         }
850
851         p = nfs_copy_user_string(NULL, &data->hostname, 256);
852         if (IS_ERR(p))
853                 goto out_err;
854         hostname = p;
855
856         p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
857         if (IS_ERR(p))
858                 goto out_err;
859         mntpath = p;
860
861         dprintk("MNTPATH: %s\n", mntpath);
862
863         p = nfs_copy_user_string(ip_addr, &data->client_addr,
864                                  sizeof(ip_addr) - 1);
865         if (IS_ERR(p))
866                 goto out_err;
867
868         /* Get a volume representation */
869         server = nfs4_create_server(data, hostname, &addr, mntpath, ip_addr,
870                                     authflavour, &mntfh);
871         if (IS_ERR(server)) {
872                 error = PTR_ERR(server);
873                 goto out_err_noserver;
874         }
875
876         /* Get a superblock - note that we may end up sharing one that already exists */
877         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
878         if (IS_ERR(s)) {
879                 error = PTR_ERR(s);
880                 goto out_free;
881         }
882
883         if (s->s_fs_info != server) {
884                 nfs_free_server(server);
885                 server = NULL;
886         }
887
888         if (!s->s_root) {
889                 /* initial superblock/root creation */
890                 s->s_flags = flags;
891                 nfs4_fill_super(s);
892         }
893
894         mntroot = nfs4_get_root(s, &mntfh);
895         if (IS_ERR(mntroot)) {
896                 error = PTR_ERR(mntroot);
897                 goto error_splat_super;
898         }
899
900         s->s_flags |= MS_ACTIVE;
901         mnt->mnt_sb = s;
902         mnt->mnt_root = mntroot;
903         kfree(mntpath);
904         kfree(hostname);
905         return 0;
906
907 out_err:
908         error = PTR_ERR(p);
909         goto out_err_noserver;
910
911 out_free:
912         nfs_free_server(server);
913 out_err_noserver:
914         kfree(mntpath);
915         kfree(hostname);
916         return error;
917
918 error_splat_super:
919         up_write(&s->s_umount);
920         deactivate_super(s);
921         goto out_err_noserver;
922 }
923
924 static void nfs4_kill_super(struct super_block *sb)
925 {
926         struct nfs_server *server = NFS_SB(sb);
927
928         nfs_return_all_delegations(sb);
929         kill_anon_super(sb);
930
931         nfs4_renewd_prepare_shutdown(server);
932         nfs_free_server(server);
933 }
934
935 /*
936  * Clone an NFS4 server record on xdev traversal (FSID-change)
937  */
938 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
939                             const char *dev_name, void *raw_data,
940                             struct vfsmount *mnt)
941 {
942         struct nfs_clone_mount *data = raw_data;
943         struct super_block *s;
944         struct nfs_server *server;
945         struct dentry *mntroot;
946         int error;
947
948         dprintk("--> nfs4_xdev_get_sb()\n");
949
950         /* create a new volume representation */
951         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
952         if (IS_ERR(server)) {
953                 error = PTR_ERR(server);
954                 goto out_err_noserver;
955         }
956
957         /* Get a superblock - note that we may end up sharing one that already exists */
958         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
959         if (IS_ERR(s)) {
960                 error = PTR_ERR(s);
961                 goto out_err_nosb;
962         }
963
964         if (s->s_fs_info != server) {
965                 nfs_free_server(server);
966                 server = NULL;
967         }
968
969         if (!s->s_root) {
970                 /* initial superblock/root creation */
971                 s->s_flags = flags;
972                 nfs4_clone_super(s, data->sb);
973         }
974
975         mntroot = nfs4_get_root(s, data->fh);
976         if (IS_ERR(mntroot)) {
977                 error = PTR_ERR(mntroot);
978                 goto error_splat_super;
979         }
980
981         s->s_flags |= MS_ACTIVE;
982         mnt->mnt_sb = s;
983         mnt->mnt_root = mntroot;
984
985         dprintk("<-- nfs4_xdev_get_sb() = 0\n");
986         return 0;
987
988 out_err_nosb:
989         nfs_free_server(server);
990 out_err_noserver:
991         dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
992         return error;
993
994 error_splat_super:
995         up_write(&s->s_umount);
996         deactivate_super(s);
997         dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
998         return error;
999 }
1000
1001 /*
1002  * Create an NFS4 server record on referral traversal
1003  */
1004 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
1005                                 const char *dev_name, void *raw_data,
1006                                 struct vfsmount *mnt)
1007 {
1008         struct nfs_clone_mount *data = raw_data;
1009         struct super_block *s;
1010         struct nfs_server *server;
1011         struct dentry *mntroot;
1012         struct nfs_fh mntfh;
1013         int error;
1014
1015         dprintk("--> nfs4_referral_get_sb()\n");
1016
1017         /* create a new volume representation */
1018         server = nfs4_create_referral_server(data, &mntfh);
1019         if (IS_ERR(server)) {
1020                 error = PTR_ERR(server);
1021                 goto out_err_noserver;
1022         }
1023
1024         /* Get a superblock - note that we may end up sharing one that already exists */
1025         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1026         if (IS_ERR(s)) {
1027                 error = PTR_ERR(s);
1028                 goto out_err_nosb;
1029         }
1030
1031         if (s->s_fs_info != server) {
1032                 nfs_free_server(server);
1033                 server = NULL;
1034         }
1035
1036         if (!s->s_root) {
1037                 /* initial superblock/root creation */
1038                 s->s_flags = flags;
1039                 nfs4_fill_super(s);
1040         }
1041
1042         mntroot = nfs4_get_root(s, data->fh);
1043         if (IS_ERR(mntroot)) {
1044                 error = PTR_ERR(mntroot);
1045                 goto error_splat_super;
1046         }
1047
1048         s->s_flags |= MS_ACTIVE;
1049         mnt->mnt_sb = s;
1050         mnt->mnt_root = mntroot;
1051
1052         dprintk("<-- nfs4_referral_get_sb() = 0\n");
1053         return 0;
1054
1055 out_err_nosb:
1056         nfs_free_server(server);
1057 out_err_noserver:
1058         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
1059         return error;
1060
1061 error_splat_super:
1062         up_write(&s->s_umount);
1063         deactivate_super(s);
1064         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
1065         return error;
1066 }
1067
1068 #endif /* CONFIG_NFS_V4 */