Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[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
531         if (server->flags & NFS_MOUNT_TAGXID)
532                 sb->s_flags |= MS_TAGXID;
533
534         nfs_super_set_maxbytes(sb, server->maxfilesize);
535 }
536
537 /*
538  * Finish setting up an NFS2/3 superblock
539  */
540 static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
541 {
542         struct nfs_server *server = NFS_SB(sb);
543
544         sb->s_blocksize_bits = 0;
545         sb->s_blocksize = 0;
546         if (data->bsize)
547                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
548
549         if (server->flags & NFS_MOUNT_VER3) {
550                 /* The VFS shouldn't apply the umask to mode bits. We will do
551                  * so ourselves when necessary.
552                  */
553                 sb->s_flags |= MS_POSIXACL;
554                 sb->s_time_gran = 1;
555         }
556
557         sb->s_op = &nfs_sops;
558         nfs_initialise_sb(sb);
559 }
560
561 /*
562  * Finish setting up a cloned NFS2/3 superblock
563  */
564 static void nfs_clone_super(struct super_block *sb,
565                             const struct super_block *old_sb)
566 {
567         struct nfs_server *server = NFS_SB(sb);
568
569         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
570         sb->s_blocksize = old_sb->s_blocksize;
571         sb->s_maxbytes = old_sb->s_maxbytes;
572
573         if (server->flags & NFS_MOUNT_VER3) {
574                 /* The VFS shouldn't apply the umask to mode bits. We will do
575                  * so ourselves when necessary.
576                  */
577                 sb->s_flags |= MS_POSIXACL;
578                 sb->s_time_gran = 1;
579         }
580
581         sb->s_op = old_sb->s_op;
582         nfs_initialise_sb(sb);
583 }
584
585 static int nfs_set_super(struct super_block *s, void *_server)
586 {
587         struct nfs_server *server = _server;
588         int ret;
589
590         s->s_fs_info = server;
591         ret = set_anon_super(s, server);
592         if (ret == 0)
593                 server->s_dev = s->s_dev;
594         return ret;
595 }
596
597 static int nfs_compare_super(struct super_block *sb, void *data)
598 {
599         struct nfs_server *server = data, *old = NFS_SB(sb);
600
601         if (old->nfs_client != server->nfs_client)
602                 return 0;
603         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
604                 return 0;
605         return 1;
606 }
607
608 static int nfs_get_sb(struct file_system_type *fs_type,
609         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
610 {
611         struct nfs_server *server = NULL;
612         struct super_block *s;
613         struct nfs_fh mntfh;
614         struct nfs_mount_data *data = raw_data;
615         struct dentry *mntroot;
616         int error;
617
618         /* Validate the mount data */
619         error = nfs_validate_mount_data(data, &mntfh);
620         if (error < 0)
621                 return error;
622
623         /* Get a volume representation */
624         server = nfs_create_server(data, &mntfh);
625         if (IS_ERR(server)) {
626                 error = PTR_ERR(server);
627                 goto out_err_noserver;
628         }
629
630         /* Get a superblock - note that we may end up sharing one that already exists */
631         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
632         if (IS_ERR(s)) {
633                 error = PTR_ERR(s);
634                 goto out_err_nosb;
635         }
636
637         if (s->s_fs_info != server) {
638                 nfs_free_server(server);
639                 server = NULL;
640         }
641
642         if (!s->s_root) {
643                 /* initial superblock/root creation */
644                 s->s_flags = flags;
645                 nfs_fill_super(s, data);
646         }
647
648         mntroot = nfs_get_root(s, &mntfh);
649         if (IS_ERR(mntroot)) {
650                 error = PTR_ERR(mntroot);
651                 goto error_splat_super;
652         }
653
654         s->s_flags |= MS_ACTIVE;
655         mnt->mnt_sb = s;
656         mnt->mnt_root = mntroot;
657         return 0;
658
659 out_err_nosb:
660         nfs_free_server(server);
661 out_err_noserver:
662         return error;
663
664 error_splat_super:
665         up_write(&s->s_umount);
666         deactivate_super(s);
667         return error;
668 }
669
670 /*
671  * Destroy an NFS2/3 superblock
672  */
673 static void nfs_kill_super(struct super_block *s)
674 {
675         struct nfs_server *server = NFS_SB(s);
676
677         kill_anon_super(s);
678         nfs_free_server(server);
679 }
680
681 /*
682  * Clone an NFS2/3 server record on xdev traversal (FSID-change)
683  */
684 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
685                            const char *dev_name, void *raw_data,
686                            struct vfsmount *mnt)
687 {
688         struct nfs_clone_mount *data = raw_data;
689         struct super_block *s;
690         struct nfs_server *server;
691         struct dentry *mntroot;
692         int error;
693
694         dprintk("--> nfs_xdev_get_sb()\n");
695
696         /* create a new volume representation */
697         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
698         if (IS_ERR(server)) {
699                 error = PTR_ERR(server);
700                 goto out_err_noserver;
701         }
702
703         /* Get a superblock - note that we may end up sharing one that already exists */
704         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
705         if (IS_ERR(s)) {
706                 error = PTR_ERR(s);
707                 goto out_err_nosb;
708         }
709
710         if (s->s_fs_info != server) {
711                 nfs_free_server(server);
712                 server = NULL;
713         }
714
715         if (!s->s_root) {
716                 /* initial superblock/root creation */
717                 s->s_flags = flags;
718                 nfs_clone_super(s, data->sb);
719         }
720
721         mntroot = nfs_get_root(s, data->fh);
722         if (IS_ERR(mntroot)) {
723                 error = PTR_ERR(mntroot);
724                 goto error_splat_super;
725         }
726
727         s->s_flags |= MS_ACTIVE;
728         mnt->mnt_sb = s;
729         mnt->mnt_root = mntroot;
730
731         dprintk("<-- nfs_xdev_get_sb() = 0\n");
732         return 0;
733
734 out_err_nosb:
735         nfs_free_server(server);
736 out_err_noserver:
737         dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
738         return error;
739
740 error_splat_super:
741         up_write(&s->s_umount);
742         deactivate_super(s);
743         dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
744         return error;
745 }
746
747 #ifdef CONFIG_NFS_V4
748
749 /*
750  * Finish setting up a cloned NFS4 superblock
751  */
752 static void nfs4_clone_super(struct super_block *sb,
753                             const struct super_block *old_sb)
754 {
755         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
756         sb->s_blocksize = old_sb->s_blocksize;
757         sb->s_maxbytes = old_sb->s_maxbytes;
758         sb->s_time_gran = 1;
759         sb->s_op = old_sb->s_op;
760         nfs_initialise_sb(sb);
761 }
762
763 /*
764  * Set up an NFS4 superblock
765  */
766 static void nfs4_fill_super(struct super_block *sb)
767 {
768         sb->s_time_gran = 1;
769         sb->s_op = &nfs4_sops;
770         nfs_initialise_sb(sb);
771 }
772
773 static void *nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
774 {
775         void *p = NULL;
776
777         if (!src->len)
778                 return ERR_PTR(-EINVAL);
779         if (src->len < maxlen)
780                 maxlen = src->len;
781         if (dst == NULL) {
782                 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
783                 if (p == NULL)
784                         return ERR_PTR(-ENOMEM);
785         }
786         if (copy_from_user(dst, src->data, maxlen)) {
787                 kfree(p);
788                 return ERR_PTR(-EFAULT);
789         }
790         dst[maxlen] = '\0';
791         return dst;
792 }
793
794 /*
795  * Get the superblock for an NFS4 mountpoint
796  */
797 static int nfs4_get_sb(struct file_system_type *fs_type,
798         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
799 {
800         struct nfs4_mount_data *data = raw_data;
801         struct super_block *s;
802         struct nfs_server *server;
803         struct sockaddr_in addr;
804         rpc_authflavor_t authflavour;
805         struct nfs_fh mntfh;
806         struct dentry *mntroot;
807         char *mntpath = NULL, *hostname = NULL, ip_addr[16];
808         void *p;
809         int error;
810
811         if (data == NULL) {
812                 dprintk("%s: missing data argument\n", __FUNCTION__);
813                 return -EINVAL;
814         }
815         if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
816                 dprintk("%s: bad mount version\n", __FUNCTION__);
817                 return -EINVAL;
818         }
819
820         /* We now require that the mount process passes the remote address */
821         if (data->host_addrlen != sizeof(addr))
822                 return -EINVAL;
823
824         if (copy_from_user(&addr, data->host_addr, sizeof(addr)))
825                 return -EFAULT;
826
827         if (addr.sin_family != AF_INET ||
828             addr.sin_addr.s_addr == INADDR_ANY
829             ) {
830                 dprintk("%s: mount program didn't pass remote IP address!\n",
831                                 __FUNCTION__);
832                 return -EINVAL;
833         }
834
835         /* Grab the authentication type */
836         authflavour = RPC_AUTH_UNIX;
837         if (data->auth_flavourlen != 0) {
838                 if (data->auth_flavourlen != 1) {
839                         dprintk("%s: Invalid number of RPC auth flavours %d.\n",
840                                         __FUNCTION__, data->auth_flavourlen);
841                         error = -EINVAL;
842                         goto out_err_noserver;
843                 }
844
845                 if (copy_from_user(&authflavour, data->auth_flavours,
846                                    sizeof(authflavour))) {
847                         error = -EFAULT;
848                         goto out_err_noserver;
849                 }
850         }
851
852         p = nfs_copy_user_string(NULL, &data->hostname, 256);
853         if (IS_ERR(p))
854                 goto out_err;
855         hostname = p;
856
857         p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
858         if (IS_ERR(p))
859                 goto out_err;
860         mntpath = p;
861
862         dprintk("MNTPATH: %s\n", mntpath);
863
864         p = nfs_copy_user_string(ip_addr, &data->client_addr,
865                                  sizeof(ip_addr) - 1);
866         if (IS_ERR(p))
867                 goto out_err;
868
869         /* Get a volume representation */
870         server = nfs4_create_server(data, hostname, &addr, mntpath, ip_addr,
871                                     authflavour, &mntfh);
872         if (IS_ERR(server)) {
873                 error = PTR_ERR(server);
874                 goto out_err_noserver;
875         }
876
877         /* Get a superblock - note that we may end up sharing one that already exists */
878         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
879         if (IS_ERR(s)) {
880                 error = PTR_ERR(s);
881                 goto out_free;
882         }
883
884         if (s->s_fs_info != server) {
885                 nfs_free_server(server);
886                 server = NULL;
887         }
888
889         if (!s->s_root) {
890                 /* initial superblock/root creation */
891                 s->s_flags = flags;
892                 nfs4_fill_super(s);
893         }
894
895         mntroot = nfs4_get_root(s, &mntfh);
896         if (IS_ERR(mntroot)) {
897                 error = PTR_ERR(mntroot);
898                 goto error_splat_super;
899         }
900
901         s->s_flags |= MS_ACTIVE;
902         mnt->mnt_sb = s;
903         mnt->mnt_root = mntroot;
904         kfree(mntpath);
905         kfree(hostname);
906         return 0;
907
908 out_err:
909         error = PTR_ERR(p);
910         goto out_err_noserver;
911
912 out_free:
913         nfs_free_server(server);
914 out_err_noserver:
915         kfree(mntpath);
916         kfree(hostname);
917         return error;
918
919 error_splat_super:
920         up_write(&s->s_umount);
921         deactivate_super(s);
922         goto out_err_noserver;
923 }
924
925 static void nfs4_kill_super(struct super_block *sb)
926 {
927         struct nfs_server *server = NFS_SB(sb);
928
929         nfs_return_all_delegations(sb);
930         kill_anon_super(sb);
931
932         nfs4_renewd_prepare_shutdown(server);
933         nfs_free_server(server);
934 }
935
936 /*
937  * Clone an NFS4 server record on xdev traversal (FSID-change)
938  */
939 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
940                             const char *dev_name, void *raw_data,
941                             struct vfsmount *mnt)
942 {
943         struct nfs_clone_mount *data = raw_data;
944         struct super_block *s;
945         struct nfs_server *server;
946         struct dentry *mntroot;
947         int error;
948
949         dprintk("--> nfs4_xdev_get_sb()\n");
950
951         /* create a new volume representation */
952         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
953         if (IS_ERR(server)) {
954                 error = PTR_ERR(server);
955                 goto out_err_noserver;
956         }
957
958         /* Get a superblock - note that we may end up sharing one that already exists */
959         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
960         if (IS_ERR(s)) {
961                 error = PTR_ERR(s);
962                 goto out_err_nosb;
963         }
964
965         if (s->s_fs_info != server) {
966                 nfs_free_server(server);
967                 server = NULL;
968         }
969
970         if (!s->s_root) {
971                 /* initial superblock/root creation */
972                 s->s_flags = flags;
973                 nfs4_clone_super(s, data->sb);
974         }
975
976         mntroot = nfs4_get_root(s, data->fh);
977         if (IS_ERR(mntroot)) {
978                 error = PTR_ERR(mntroot);
979                 goto error_splat_super;
980         }
981
982         s->s_flags |= MS_ACTIVE;
983         mnt->mnt_sb = s;
984         mnt->mnt_root = mntroot;
985
986         dprintk("<-- nfs4_xdev_get_sb() = 0\n");
987         return 0;
988
989 out_err_nosb:
990         nfs_free_server(server);
991 out_err_noserver:
992         dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
993         return error;
994
995 error_splat_super:
996         up_write(&s->s_umount);
997         deactivate_super(s);
998         dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
999         return error;
1000 }
1001
1002 /*
1003  * Create an NFS4 server record on referral traversal
1004  */
1005 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
1006                                 const char *dev_name, void *raw_data,
1007                                 struct vfsmount *mnt)
1008 {
1009         struct nfs_clone_mount *data = raw_data;
1010         struct super_block *s;
1011         struct nfs_server *server;
1012         struct dentry *mntroot;
1013         struct nfs_fh mntfh;
1014         int error;
1015
1016         dprintk("--> nfs4_referral_get_sb()\n");
1017
1018         /* create a new volume representation */
1019         server = nfs4_create_referral_server(data, &mntfh);
1020         if (IS_ERR(server)) {
1021                 error = PTR_ERR(server);
1022                 goto out_err_noserver;
1023         }
1024
1025         /* Get a superblock - note that we may end up sharing one that already exists */
1026         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1027         if (IS_ERR(s)) {
1028                 error = PTR_ERR(s);
1029                 goto out_err_nosb;
1030         }
1031
1032         if (s->s_fs_info != server) {
1033                 nfs_free_server(server);
1034                 server = NULL;
1035         }
1036
1037         if (!s->s_root) {
1038                 /* initial superblock/root creation */
1039                 s->s_flags = flags;
1040                 nfs4_fill_super(s);
1041         }
1042
1043         mntroot = nfs4_get_root(s, data->fh);
1044         if (IS_ERR(mntroot)) {
1045                 error = PTR_ERR(mntroot);
1046                 goto error_splat_super;
1047         }
1048
1049         s->s_flags |= MS_ACTIVE;
1050         mnt->mnt_sb = s;
1051         mnt->mnt_root = mntroot;
1052
1053         dprintk("<-- nfs4_referral_get_sb() = 0\n");
1054         return 0;
1055
1056 out_err_nosb:
1057         nfs_free_server(server);
1058 out_err_noserver:
1059         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
1060         return error;
1061
1062 error_splat_super:
1063         up_write(&s->s_umount);
1064         deactivate_super(s);
1065         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
1066         return error;
1067 }
1068
1069 #endif /* CONFIG_NFS_V4 */