7f5bc28ea8d10a284ffd2f60678b1647ec1c526f
[pandora-kernel.git] / fs / nfs / super.c
1 /*
2  *  linux/fs/nfs/super.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs superblock handling functions
7  *
8  *  Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
10  *
11  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12  *  J.S.Peatfield@damtp.cam.ac.uk
13  *
14  *  Split from inode.c by David Howells <dhowells@redhat.com>
15  *
16  * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
17  *   particular server are held in the same superblock
18  * - NFS superblocks can have several effective roots to the dentry tree
19  * - directory type roots are spliced into the tree when a path from one root reaches the root
20  *   of another (see nfs_lookup())
21  */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25
26 #include <linux/time.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/errno.h>
32 #include <linux/unistd.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/stats.h>
35 #include <linux/sunrpc/metrics.h>
36 #include <linux/nfs_fs.h>
37 #include <linux/nfs_mount.h>
38 #include <linux/nfs4_mount.h>
39 #include <linux/lockd/bind.h>
40 #include <linux/smp_lock.h>
41 #include <linux/seq_file.h>
42 #include <linux/mount.h>
43 #include <linux/nfs_idmap.h>
44 #include <linux/vfs.h>
45 #include <linux/inet.h>
46 #include <linux/nfs_xdr.h>
47 #include <linux/magic.h>
48
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 const 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 const 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         ret = nfs_register_sysctl();
155         if (ret < 0)
156                 goto error_1;
157 #ifdef CONFIG_NFS_V4
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 #endif
169 error_1:
170         unregister_filesystem(&nfs_fs_type);
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         if (error < 0)
208                 goto out_err;
209         buf->f_type = NFS_SUPER_MAGIC;
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
237         unlock_kernel();
238         return 0;
239
240  out_err:
241         dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
242         unlock_kernel();
243         return error;
244 }
245
246 /*
247  * Map the security flavour number to a name
248  */
249 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
250 {
251         static const struct {
252                 rpc_authflavor_t flavour;
253                 const char *str;
254         } sec_flavours[] = {
255                 { RPC_AUTH_NULL, "null" },
256                 { RPC_AUTH_UNIX, "sys" },
257                 { RPC_AUTH_GSS_KRB5, "krb5" },
258                 { RPC_AUTH_GSS_KRB5I, "krb5i" },
259                 { RPC_AUTH_GSS_KRB5P, "krb5p" },
260                 { RPC_AUTH_GSS_LKEY, "lkey" },
261                 { RPC_AUTH_GSS_LKEYI, "lkeyi" },
262                 { RPC_AUTH_GSS_LKEYP, "lkeyp" },
263                 { RPC_AUTH_GSS_SPKM, "spkm" },
264                 { RPC_AUTH_GSS_SPKMI, "spkmi" },
265                 { RPC_AUTH_GSS_SPKMP, "spkmp" },
266                 { UINT_MAX, "unknown" }
267         };
268         int i;
269
270         for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
271                 if (sec_flavours[i].flavour == flavour)
272                         break;
273         }
274         return sec_flavours[i].str;
275 }
276
277 /*
278  * Describe the mount options in force on this server representation
279  */
280 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
281 {
282         static const struct proc_nfs_info {
283                 int flag;
284                 const char *str;
285                 const char *nostr;
286         } nfs_info[] = {
287                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
288                 { NFS_MOUNT_INTR, ",intr", "" },
289                 { NFS_MOUNT_NOCTO, ",nocto", "" },
290                 { NFS_MOUNT_NOAC, ",noac", "" },
291                 { NFS_MOUNT_NONLM, ",nolock", "" },
292                 { NFS_MOUNT_NOACL, ",noacl", "" },
293                 { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
294                 { 0, NULL, NULL }
295         };
296         const struct proc_nfs_info *nfs_infop;
297         struct nfs_client *clp = nfss->nfs_client;
298         char buf[12];
299         const char *proto;
300
301         seq_printf(m, ",vers=%d", clp->rpc_ops->version);
302         seq_printf(m, ",rsize=%d", nfss->rsize);
303         seq_printf(m, ",wsize=%d", nfss->wsize);
304         if (nfss->acregmin != 3*HZ || showdefaults)
305                 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
306         if (nfss->acregmax != 60*HZ || showdefaults)
307                 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
308         if (nfss->acdirmin != 30*HZ || showdefaults)
309                 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
310         if (nfss->acdirmax != 60*HZ || showdefaults)
311                 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
312         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
313                 if (nfss->flags & nfs_infop->flag)
314                         seq_puts(m, nfs_infop->str);
315                 else
316                         seq_puts(m, nfs_infop->nostr);
317         }
318         switch (nfss->client->cl_xprt->prot) {
319                 case IPPROTO_TCP:
320                         proto = "tcp";
321                         break;
322                 case IPPROTO_UDP:
323                         proto = "udp";
324                         break;
325                 default:
326                         snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
327                         proto = buf;
328         }
329         seq_printf(m, ",proto=%s", proto);
330         seq_printf(m, ",timeo=%lu", 10U * clp->retrans_timeo / HZ);
331         seq_printf(m, ",retrans=%u", clp->retrans_count);
332         seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
333 }
334
335 /*
336  * Describe the mount options on this VFS mountpoint
337  */
338 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
339 {
340         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
341
342         nfs_show_mount_options(m, nfss, 0);
343
344         seq_puts(m, ",addr=");
345         seq_escape(m, nfss->nfs_client->cl_hostname, " \t\n\\");
346
347         return 0;
348 }
349
350 /*
351  * Present statistical information for this VFS mountpoint
352  */
353 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
354 {
355         int i, cpu;
356         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
357         struct rpc_auth *auth = nfss->client->cl_auth;
358         struct nfs_iostats totals = { };
359
360         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
361
362         /*
363          * Display all mount option settings
364          */
365         seq_printf(m, "\n\topts:\t");
366         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
367         seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
368         seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
369         seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
370         nfs_show_mount_options(m, nfss, 1);
371
372         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
373
374         seq_printf(m, "\n\tcaps:\t");
375         seq_printf(m, "caps=0x%x", nfss->caps);
376         seq_printf(m, ",wtmult=%d", nfss->wtmult);
377         seq_printf(m, ",dtsize=%d", nfss->dtsize);
378         seq_printf(m, ",bsize=%d", nfss->bsize);
379         seq_printf(m, ",namelen=%d", nfss->namelen);
380
381 #ifdef CONFIG_NFS_V4
382         if (nfss->nfs_client->cl_nfsversion == 4) {
383                 seq_printf(m, "\n\tnfsv4:\t");
384                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
385                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
386                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
387         }
388 #endif
389
390         /*
391          * Display security flavor in effect for this mount
392          */
393         seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
394         if (auth->au_flavor)
395                 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
396
397         /*
398          * Display superblock I/O counters
399          */
400         for_each_possible_cpu(cpu) {
401                 struct nfs_iostats *stats;
402
403                 preempt_disable();
404                 stats = per_cpu_ptr(nfss->io_stats, cpu);
405
406                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
407                         totals.events[i] += stats->events[i];
408                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
409                         totals.bytes[i] += stats->bytes[i];
410
411                 preempt_enable();
412         }
413
414         seq_printf(m, "\n\tevents:\t");
415         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
416                 seq_printf(m, "%lu ", totals.events[i]);
417         seq_printf(m, "\n\tbytes:\t");
418         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
419                 seq_printf(m, "%Lu ", totals.bytes[i]);
420         seq_printf(m, "\n");
421
422         rpc_print_iostats(m, nfss->client);
423
424         return 0;
425 }
426
427 /*
428  * Begin unmount by attempting to remove all automounted mountpoints we added
429  * in response to xdev traversals and referrals
430  */
431 static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
432 {
433         struct nfs_server *server = NFS_SB(vfsmnt->mnt_sb);
434         struct rpc_clnt *rpc;
435
436         shrink_submounts(vfsmnt, &nfs_automount_list);
437
438         if (!(flags & MNT_FORCE))
439                 return;
440         /* -EIO all pending I/O */
441         rpc = server->client_acl;
442         if (!IS_ERR(rpc))
443                 rpc_killall_tasks(rpc);
444         rpc = server->client;
445         if (!IS_ERR(rpc))
446                 rpc_killall_tasks(rpc);
447 }
448
449 /*
450  * Sanity-check a server address provided by the mount command
451  */
452 static int nfs_verify_server_address(struct sockaddr *addr)
453 {
454         switch (addr->sa_family) {
455         case AF_INET: {
456                 struct sockaddr_in *sa = (struct sockaddr_in *) addr;
457                 if (sa->sin_addr.s_addr != INADDR_ANY)
458                         return 1;
459                 break;
460         }
461         }
462
463         return 0;
464 }
465
466 /*
467  * Validate the NFS2/NFS3 mount data
468  * - fills in the mount root filehandle
469  */
470 static int nfs_validate_mount_data(struct nfs_mount_data *data,
471                                    struct nfs_fh *mntfh)
472 {
473         if (data == NULL) {
474                 dprintk("%s: missing data argument\n", __FUNCTION__);
475                 return -EINVAL;
476         }
477
478         if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
479                 dprintk("%s: bad mount version\n", __FUNCTION__);
480                 return -EINVAL;
481         }
482
483         switch (data->version) {
484                 case 1:
485                         data->namlen = 0;
486                 case 2:
487                         data->bsize  = 0;
488                 case 3:
489                         if (data->flags & NFS_MOUNT_VER3) {
490                                 dprintk("%s: mount structure version %d does not support NFSv3\n",
491                                                 __FUNCTION__,
492                                                 data->version);
493                                 return -EINVAL;
494                         }
495                         data->root.size = NFS2_FHSIZE;
496                         memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
497                 case 4:
498                         if (data->flags & NFS_MOUNT_SECFLAVOUR) {
499                                 dprintk("%s: mount structure version %d does not support strong security\n",
500                                                 __FUNCTION__,
501                                                 data->version);
502                                 return -EINVAL;
503                         }
504                 case 5:
505                         memset(data->context, 0, sizeof(data->context));
506         }
507
508         /* Set the pseudoflavor */
509         if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
510                 data->pseudoflavor = RPC_AUTH_UNIX;
511
512 #ifndef CONFIG_NFS_V3
513         /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
514         if (data->flags & NFS_MOUNT_VER3) {
515                 dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
516                 return -EPROTONOSUPPORT;
517         }
518 #endif /* CONFIG_NFS_V3 */
519
520         /* We now require that the mount process passes the remote address */
521         if (!nfs_verify_server_address((struct sockaddr *) &data->addr)) {
522                 dprintk("%s: mount program didn't pass remote address!\n",
523                         __FUNCTION__);
524                 return -EINVAL;
525         }
526
527         /* Prepare the root filehandle */
528         if (data->flags & NFS_MOUNT_VER3)
529                 mntfh->size = data->root.size;
530         else
531                 mntfh->size = NFS2_FHSIZE;
532
533         if (mntfh->size > sizeof(mntfh->data)) {
534                 dprintk("%s: invalid root filehandle\n", __FUNCTION__);
535                 return -EINVAL;
536         }
537
538         memcpy(mntfh->data, data->root.data, mntfh->size);
539         if (mntfh->size < sizeof(mntfh->data))
540                 memset(mntfh->data + mntfh->size, 0,
541                        sizeof(mntfh->data) - mntfh->size);
542
543         return 0;
544 }
545
546 /*
547  * Initialise the common bits of the superblock
548  */
549 static inline void nfs_initialise_sb(struct super_block *sb)
550 {
551         struct nfs_server *server = NFS_SB(sb);
552
553         sb->s_magic = NFS_SUPER_MAGIC;
554
555         /* We probably want something more informative here */
556         snprintf(sb->s_id, sizeof(sb->s_id),
557                  "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
558
559         if (sb->s_blocksize == 0)
560                 sb->s_blocksize = nfs_block_bits(server->wsize,
561                                                  &sb->s_blocksize_bits);
562
563         if (server->flags & NFS_MOUNT_NOAC)
564                 sb->s_flags |= MS_SYNCHRONOUS;
565
566         nfs_super_set_maxbytes(sb, server->maxfilesize);
567 }
568
569 /*
570  * Finish setting up an NFS2/3 superblock
571  */
572 static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
573 {
574         struct nfs_server *server = NFS_SB(sb);
575
576         sb->s_blocksize_bits = 0;
577         sb->s_blocksize = 0;
578         if (data->bsize)
579                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
580
581         if (server->flags & NFS_MOUNT_VER3) {
582                 /* The VFS shouldn't apply the umask to mode bits. We will do
583                  * so ourselves when necessary.
584                  */
585                 sb->s_flags |= MS_POSIXACL;
586                 sb->s_time_gran = 1;
587         }
588
589         sb->s_op = &nfs_sops;
590         nfs_initialise_sb(sb);
591 }
592
593 /*
594  * Finish setting up a cloned NFS2/3 superblock
595  */
596 static void nfs_clone_super(struct super_block *sb,
597                             const struct super_block *old_sb)
598 {
599         struct nfs_server *server = NFS_SB(sb);
600
601         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
602         sb->s_blocksize = old_sb->s_blocksize;
603         sb->s_maxbytes = old_sb->s_maxbytes;
604
605         if (server->flags & NFS_MOUNT_VER3) {
606                 /* The VFS shouldn't apply the umask to mode bits. We will do
607                  * so ourselves when necessary.
608                  */
609                 sb->s_flags |= MS_POSIXACL;
610                 sb->s_time_gran = 1;
611         }
612
613         sb->s_op = old_sb->s_op;
614         nfs_initialise_sb(sb);
615 }
616
617 static int nfs_set_super(struct super_block *s, void *_server)
618 {
619         struct nfs_server *server = _server;
620         int ret;
621
622         s->s_fs_info = server;
623         ret = set_anon_super(s, server);
624         if (ret == 0)
625                 server->s_dev = s->s_dev;
626         return ret;
627 }
628
629 static int nfs_compare_super(struct super_block *sb, void *data)
630 {
631         struct nfs_server *server = data, *old = NFS_SB(sb);
632
633         if (old->nfs_client != server->nfs_client)
634                 return 0;
635         if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
636                 return 0;
637         return 1;
638 }
639
640 static int nfs_get_sb(struct file_system_type *fs_type,
641         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
642 {
643         struct nfs_server *server = NULL;
644         struct super_block *s;
645         struct nfs_fh mntfh;
646         struct nfs_mount_data *data = raw_data;
647         struct dentry *mntroot;
648         int error;
649
650         /* Validate the mount data */
651         error = nfs_validate_mount_data(data, &mntfh);
652         if (error < 0)
653                 goto out;
654
655         /* Get a volume representation */
656         server = nfs_create_server(data, &mntfh);
657         if (IS_ERR(server)) {
658                 error = PTR_ERR(server);
659                 goto out;
660         }
661
662         /* Get a superblock - note that we may end up sharing one that already exists */
663         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
664         if (IS_ERR(s)) {
665                 error = PTR_ERR(s);
666                 goto out_err_nosb;
667         }
668
669         if (s->s_fs_info != server) {
670                 nfs_free_server(server);
671                 server = NULL;
672         }
673
674         if (!s->s_root) {
675                 /* initial superblock/root creation */
676                 s->s_flags = flags;
677                 nfs_fill_super(s, data);
678         }
679
680         mntroot = nfs_get_root(s, &mntfh);
681         if (IS_ERR(mntroot)) {
682                 error = PTR_ERR(mntroot);
683                 goto error_splat_super;
684         }
685
686         s->s_flags |= MS_ACTIVE;
687         mnt->mnt_sb = s;
688         mnt->mnt_root = mntroot;
689         error = 0;
690
691 out:
692         return error;
693
694 out_err_nosb:
695         nfs_free_server(server);
696         goto out;
697
698 error_splat_super:
699         up_write(&s->s_umount);
700         deactivate_super(s);
701         goto out;
702 }
703
704 /*
705  * Destroy an NFS2/3 superblock
706  */
707 static void nfs_kill_super(struct super_block *s)
708 {
709         struct nfs_server *server = NFS_SB(s);
710
711         kill_anon_super(s);
712         nfs_free_server(server);
713 }
714
715 /*
716  * Clone an NFS2/3 server record on xdev traversal (FSID-change)
717  */
718 static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
719                            const char *dev_name, void *raw_data,
720                            struct vfsmount *mnt)
721 {
722         struct nfs_clone_mount *data = raw_data;
723         struct super_block *s;
724         struct nfs_server *server;
725         struct dentry *mntroot;
726         int error;
727
728         dprintk("--> nfs_xdev_get_sb()\n");
729
730         /* create a new volume representation */
731         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
732         if (IS_ERR(server)) {
733                 error = PTR_ERR(server);
734                 goto out_err_noserver;
735         }
736
737         /* Get a superblock - note that we may end up sharing one that already exists */
738         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
739         if (IS_ERR(s)) {
740                 error = PTR_ERR(s);
741                 goto out_err_nosb;
742         }
743
744         if (s->s_fs_info != server) {
745                 nfs_free_server(server);
746                 server = NULL;
747         }
748
749         if (!s->s_root) {
750                 /* initial superblock/root creation */
751                 s->s_flags = flags;
752                 nfs_clone_super(s, data->sb);
753         }
754
755         mntroot = nfs_get_root(s, data->fh);
756         if (IS_ERR(mntroot)) {
757                 error = PTR_ERR(mntroot);
758                 goto error_splat_super;
759         }
760
761         s->s_flags |= MS_ACTIVE;
762         mnt->mnt_sb = s;
763         mnt->mnt_root = mntroot;
764
765         dprintk("<-- nfs_xdev_get_sb() = 0\n");
766         return 0;
767
768 out_err_nosb:
769         nfs_free_server(server);
770 out_err_noserver:
771         dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
772         return error;
773
774 error_splat_super:
775         up_write(&s->s_umount);
776         deactivate_super(s);
777         dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
778         return error;
779 }
780
781 #ifdef CONFIG_NFS_V4
782
783 /*
784  * Finish setting up a cloned NFS4 superblock
785  */
786 static void nfs4_clone_super(struct super_block *sb,
787                             const struct super_block *old_sb)
788 {
789         sb->s_blocksize_bits = old_sb->s_blocksize_bits;
790         sb->s_blocksize = old_sb->s_blocksize;
791         sb->s_maxbytes = old_sb->s_maxbytes;
792         sb->s_time_gran = 1;
793         sb->s_op = old_sb->s_op;
794         nfs_initialise_sb(sb);
795 }
796
797 /*
798  * Set up an NFS4 superblock
799  */
800 static void nfs4_fill_super(struct super_block *sb)
801 {
802         sb->s_time_gran = 1;
803         sb->s_op = &nfs4_sops;
804         nfs_initialise_sb(sb);
805 }
806
807 /*
808  * Get the superblock for an NFS4 mountpoint
809  */
810 static int nfs4_get_sb(struct file_system_type *fs_type,
811         int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
812 {
813         struct nfs4_mount_data *data = raw_data;
814         struct super_block *s;
815         struct nfs_server *server;
816         struct sockaddr_in addr;
817         rpc_authflavor_t authflavour;
818         struct nfs_fh mntfh;
819         struct dentry *mntroot;
820         char *p, *mntpath = NULL, *hostname = NULL, *ip_addr = NULL;
821         int error;
822
823         if (data == NULL) {
824                 dprintk("%s: missing data argument\n", __FUNCTION__);
825                 return -EINVAL;
826         }
827         if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
828                 dprintk("%s: bad mount version\n", __FUNCTION__);
829                 return -EINVAL;
830         }
831
832         /* We now require that the mount process passes the remote address */
833         if (data->host_addrlen != sizeof(addr))
834                 return -EINVAL;
835
836         if (copy_from_user(&addr, data->host_addr, sizeof(addr)))
837                 return -EFAULT;
838
839         if (!nfs_verify_server_address((struct sockaddr *) &addr)) {
840                 dprintk("%s: mount program didn't pass remote IP address!\n",
841                                 __FUNCTION__);
842                 return -EINVAL;
843         }
844
845         /* RFC3530: The default port for NFS is 2049 */
846         if (addr.sin_port == 0)
847                 addr.sin_port = htons(NFS_PORT);
848
849         /* Grab the authentication type */
850         authflavour = RPC_AUTH_UNIX;
851         if (data->auth_flavourlen != 0) {
852                 if (data->auth_flavourlen != 1) {
853                         dprintk("%s: Invalid number of RPC auth flavours %d.\n",
854                                         __FUNCTION__, data->auth_flavourlen);
855                         error = -EINVAL;
856                         goto out;
857                 }
858
859                 if (copy_from_user(&authflavour, data->auth_flavours,
860                                    sizeof(authflavour))) {
861                         error = -EFAULT;
862                         goto out;
863                 }
864         }
865
866         p = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
867         if (IS_ERR(p))
868                 goto out_err;
869         hostname = p;
870
871         p = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
872         if (IS_ERR(p))
873                 goto out_err;
874         mntpath = p;
875
876         dprintk("MNTPATH: %s\n", mntpath);
877
878         p = strndup_user(data->client_addr.data, 16);
879         if (IS_ERR(p))
880                 goto out_err;
881         ip_addr = p;
882
883         /* Get a volume representation */
884         server = nfs4_create_server(data, hostname, &addr, mntpath, ip_addr,
885                                     authflavour, &mntfh);
886         if (IS_ERR(server)) {
887                 error = PTR_ERR(server);
888                 goto out;
889         }
890
891         /* Get a superblock - note that we may end up sharing one that already exists */
892         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
893         if (IS_ERR(s)) {
894                 error = PTR_ERR(s);
895                 goto out_free;
896         }
897
898         if (s->s_fs_info != server) {
899                 nfs_free_server(server);
900                 server = NULL;
901         }
902
903         if (!s->s_root) {
904                 /* initial superblock/root creation */
905                 s->s_flags = flags;
906                 nfs4_fill_super(s);
907         }
908
909         mntroot = nfs4_get_root(s, &mntfh);
910         if (IS_ERR(mntroot)) {
911                 error = PTR_ERR(mntroot);
912                 goto error_splat_super;
913         }
914
915         s->s_flags |= MS_ACTIVE;
916         mnt->mnt_sb = s;
917         mnt->mnt_root = mntroot;
918         error = 0;
919
920 out:
921         kfree(ip_addr);
922         kfree(mntpath);
923         kfree(hostname);
924         return error;
925
926 out_err:
927         error = PTR_ERR(p);
928         goto out;
929
930 out_free:
931         nfs_free_server(server);
932         goto out;
933
934 error_splat_super:
935         up_write(&s->s_umount);
936         deactivate_super(s);
937         goto out;
938 }
939
940 static void nfs4_kill_super(struct super_block *sb)
941 {
942         struct nfs_server *server = NFS_SB(sb);
943
944         nfs_return_all_delegations(sb);
945         kill_anon_super(sb);
946
947         nfs4_renewd_prepare_shutdown(server);
948         nfs_free_server(server);
949 }
950
951 /*
952  * Clone an NFS4 server record on xdev traversal (FSID-change)
953  */
954 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
955                             const char *dev_name, void *raw_data,
956                             struct vfsmount *mnt)
957 {
958         struct nfs_clone_mount *data = raw_data;
959         struct super_block *s;
960         struct nfs_server *server;
961         struct dentry *mntroot;
962         int error;
963
964         dprintk("--> nfs4_xdev_get_sb()\n");
965
966         /* create a new volume representation */
967         server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
968         if (IS_ERR(server)) {
969                 error = PTR_ERR(server);
970                 goto out_err_noserver;
971         }
972
973         /* Get a superblock - note that we may end up sharing one that already exists */
974         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
975         if (IS_ERR(s)) {
976                 error = PTR_ERR(s);
977                 goto out_err_nosb;
978         }
979
980         if (s->s_fs_info != server) {
981                 nfs_free_server(server);
982                 server = NULL;
983         }
984
985         if (!s->s_root) {
986                 /* initial superblock/root creation */
987                 s->s_flags = flags;
988                 nfs4_clone_super(s, data->sb);
989         }
990
991         mntroot = nfs4_get_root(s, data->fh);
992         if (IS_ERR(mntroot)) {
993                 error = PTR_ERR(mntroot);
994                 goto error_splat_super;
995         }
996
997         s->s_flags |= MS_ACTIVE;
998         mnt->mnt_sb = s;
999         mnt->mnt_root = mntroot;
1000
1001         dprintk("<-- nfs4_xdev_get_sb() = 0\n");
1002         return 0;
1003
1004 out_err_nosb:
1005         nfs_free_server(server);
1006 out_err_noserver:
1007         dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
1008         return error;
1009
1010 error_splat_super:
1011         up_write(&s->s_umount);
1012         deactivate_super(s);
1013         dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
1014         return error;
1015 }
1016
1017 /*
1018  * Create an NFS4 server record on referral traversal
1019  */
1020 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
1021                                 const char *dev_name, void *raw_data,
1022                                 struct vfsmount *mnt)
1023 {
1024         struct nfs_clone_mount *data = raw_data;
1025         struct super_block *s;
1026         struct nfs_server *server;
1027         struct dentry *mntroot;
1028         struct nfs_fh mntfh;
1029         int error;
1030
1031         dprintk("--> nfs4_referral_get_sb()\n");
1032
1033         /* create a new volume representation */
1034         server = nfs4_create_referral_server(data, &mntfh);
1035         if (IS_ERR(server)) {
1036                 error = PTR_ERR(server);
1037                 goto out_err_noserver;
1038         }
1039
1040         /* Get a superblock - note that we may end up sharing one that already exists */
1041         s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1042         if (IS_ERR(s)) {
1043                 error = PTR_ERR(s);
1044                 goto out_err_nosb;
1045         }
1046
1047         if (s->s_fs_info != server) {
1048                 nfs_free_server(server);
1049                 server = NULL;
1050         }
1051
1052         if (!s->s_root) {
1053                 /* initial superblock/root creation */
1054                 s->s_flags = flags;
1055                 nfs4_fill_super(s);
1056         }
1057
1058         mntroot = nfs4_get_root(s, &mntfh);
1059         if (IS_ERR(mntroot)) {
1060                 error = PTR_ERR(mntroot);
1061                 goto error_splat_super;
1062         }
1063
1064         s->s_flags |= MS_ACTIVE;
1065         mnt->mnt_sb = s;
1066         mnt->mnt_root = mntroot;
1067
1068         dprintk("<-- nfs4_referral_get_sb() = 0\n");
1069         return 0;
1070
1071 out_err_nosb:
1072         nfs_free_server(server);
1073 out_err_noserver:
1074         dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
1075         return error;
1076
1077 error_splat_super:
1078         up_write(&s->s_umount);
1079         deactivate_super(s);
1080         dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
1081         return error;
1082 }
1083
1084 #endif /* CONFIG_NFS_V4 */