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