NFS: Share NFS superblocks per-protocol per-server per-FSID
[usb.git] / fs / nfs / super.c
blob867b5dcd3a40a878d725b16d39c0a23d95dfe8ac
1 /*
2 * linux/fs/nfs/super.c
4 * Copyright (C) 1992 Rick Sladkey
6 * nfs superblock handling functions
8 * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
14 * Split from inode.c by David Howells <dhowells@redhat.com>
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())
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
27 #include <linux/time.h>
28 #include <linux/kernel.h>
29 #include <linux/mm.h>
30 #include <linux/string.h>
31 #include <linux/stat.h>
32 #include <linux/errno.h>
33 #include <linux/unistd.h>
34 #include <linux/sunrpc/clnt.h>
35 #include <linux/sunrpc/stats.h>
36 #include <linux/sunrpc/metrics.h>
37 #include <linux/nfs_fs.h>
38 #include <linux/nfs_mount.h>
39 #include <linux/nfs4_mount.h>
40 #include <linux/lockd/bind.h>
41 #include <linux/smp_lock.h>
42 #include <linux/seq_file.h>
43 #include <linux/mount.h>
44 #include <linux/nfs_idmap.h>
45 #include <linux/vfs.h>
46 #include <linux/inet.h>
47 #include <linux/nfs_xdr.h>
49 #include <asm/system.h>
50 #include <asm/uaccess.h>
52 #include "nfs4_fs.h"
53 #include "callback.h"
54 #include "delegation.h"
55 #include "iostat.h"
56 #include "internal.h"
58 #define NFSDBG_FACILITY NFSDBG_VFS
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 *);
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,
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,
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,
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);
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,
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,
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,
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,
139 #endif
141 static struct shrinker *acl_shrinker;
144 * Register the NFS filesystems
146 int __init register_nfs_fs(void)
148 int ret;
150 ret = register_filesystem(&nfs_fs_type);
151 if (ret < 0)
152 goto error_0;
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;
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;
176 * Unregister the NFS filesystems
178 void __exit unregister_nfs_fs(void)
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);
190 * Deliver file system statistics to userspace
192 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
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,
202 int error;
204 lock_kernel();
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;
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.
216 buf->f_frsize = dentry->d_sb->s_blocksize;
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.
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;
232 buf->f_files = res.tfiles;
233 buf->f_ffree = res.afiles;
235 buf->f_namelen = server->namelen;
236 out:
237 unlock_kernel();
238 return 0;
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;
248 * Map the security flavour number to a name
250 static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
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" }
269 int i;
271 for (i=0; sec_flavours[i].flavour != -1; i++) {
272 if (sec_flavours[i].flavour == flavour)
273 break;
275 return sec_flavours[i].str;
279 * Describe the mount options in force on this server representation
281 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
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 { 0, NULL, NULL }
296 const struct proc_nfs_info *nfs_infop;
297 struct nfs_client *clp = nfss->nfs_client;
298 char buf[12];
299 const char *proto;
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);
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;
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));
336 * Describe the mount options on this VFS mountpoint
338 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
340 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
342 nfs_show_mount_options(m, nfss, 0);
344 seq_puts(m, ",addr=");
345 seq_escape(m, nfss->nfs_client->cl_hostname, " \t\n\\");
347 return 0;
351 * Present statistical information for this VFS mountpoint
353 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
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 = { };
360 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
363 * Display all mount option settings
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);
372 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
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);
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);
388 #endif
391 * Display security flavor in effect for this mount
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);
398 * Display superblock I/O counters
400 for_each_possible_cpu(cpu) {
401 struct nfs_iostats *stats;
403 preempt_disable();
404 stats = per_cpu_ptr(nfss->io_stats, cpu);
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];
411 preempt_enable();
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");
422 rpc_print_iostats(m, nfss->client);
424 return 0;
428 * Begin unmount by attempting to remove all automounted mountpoints we added
429 * in response to xdev traversals and referrals
431 static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
433 shrink_submounts(vfsmnt, &nfs_automount_list);
437 * Validate the NFS2/NFS3 mount data
438 * - fills in the mount root filehandle
440 static int nfs_validate_mount_data(struct nfs_mount_data *data,
441 struct nfs_fh *mntfh)
443 if (data == NULL) {
444 dprintk("%s: missing data argument\n", __FUNCTION__);
445 return -EINVAL;
448 if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
449 dprintk("%s: bad mount version\n", __FUNCTION__);
450 return -EINVAL;
453 switch (data->version) {
454 case 1:
455 data->namlen = 0;
456 case 2:
457 data->bsize = 0;
458 case 3:
459 if (data->flags & NFS_MOUNT_VER3) {
460 dprintk("%s: mount structure version %d does not support NFSv3\n",
461 __FUNCTION__,
462 data->version);
463 return -EINVAL;
465 data->root.size = NFS2_FHSIZE;
466 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
467 case 4:
468 if (data->flags & NFS_MOUNT_SECFLAVOUR) {
469 dprintk("%s: mount structure version %d does not support strong security\n",
470 __FUNCTION__,
471 data->version);
472 return -EINVAL;
474 /* Fill in pseudoflavor for mount version < 5 */
475 data->pseudoflavor = RPC_AUTH_UNIX;
476 case 5:
477 memset(data->context, 0, sizeof(data->context));
480 #ifndef CONFIG_NFS_V3
481 /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
482 if (data->flags & NFS_MOUNT_VER3) {
483 dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
484 return -EPROTONOSUPPORT;
486 #endif /* CONFIG_NFS_V3 */
488 /* We now require that the mount process passes the remote address */
489 if (data->addr.sin_addr.s_addr == INADDR_ANY) {
490 dprintk("%s: mount program didn't pass remote address!\n",
491 __FUNCTION__);
492 return -EINVAL;
495 /* Prepare the root filehandle */
496 if (data->flags & NFS_MOUNT_VER3)
497 mntfh->size = data->root.size;
498 else
499 mntfh->size = NFS2_FHSIZE;
501 if (mntfh->size > sizeof(mntfh->data)) {
502 dprintk("%s: invalid root filehandle\n", __FUNCTION__);
503 return -EINVAL;
506 memcpy(mntfh->data, data->root.data, mntfh->size);
507 if (mntfh->size < sizeof(mntfh->data))
508 memset(mntfh->data + mntfh->size, 0,
509 sizeof(mntfh->data) - mntfh->size);
511 return 0;
515 * Initialise the common bits of the superblock
517 static inline void nfs_initialise_sb(struct super_block *sb)
519 struct nfs_server *server = NFS_SB(sb);
521 sb->s_magic = NFS_SUPER_MAGIC;
523 /* We probably want something more informative here */
524 snprintf(sb->s_id, sizeof(sb->s_id),
525 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
527 if (sb->s_blocksize == 0)
528 sb->s_blocksize = nfs_block_bits(server->wsize,
529 &sb->s_blocksize_bits);
531 if (server->flags & NFS_MOUNT_NOAC)
532 sb->s_flags |= MS_SYNCHRONOUS;
534 nfs_super_set_maxbytes(sb, server->maxfilesize);
538 * Finish setting up an NFS2/3 superblock
540 static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
542 struct nfs_server *server = NFS_SB(sb);
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);
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.
553 sb->s_flags |= MS_POSIXACL;
554 sb->s_time_gran = 1;
557 sb->s_op = &nfs_sops;
558 nfs_initialise_sb(sb);
562 * Finish setting up a cloned NFS2/3 superblock
564 static void nfs_clone_super(struct super_block *sb,
565 const struct super_block *old_sb)
567 struct nfs_server *server = NFS_SB(sb);
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;
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.
577 sb->s_flags |= MS_POSIXACL;
578 sb->s_time_gran = 1;
581 sb->s_op = old_sb->s_op;
582 nfs_initialise_sb(sb);
585 static int nfs_set_super(struct super_block *s, void *_server)
587 struct nfs_server *server = _server;
588 int ret;
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;
597 static int nfs_compare_super(struct super_block *sb, void *data)
599 struct nfs_server *server = data, *old = NFS_SB(sb);
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;
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)
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;
618 /* Validate the mount data */
619 error = nfs_validate_mount_data(data, &mntfh);
620 if (error < 0)
621 return error;
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;
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;
637 if (s->s_fs_info != server) {
638 nfs_free_server(server);
639 server = NULL;
642 if (!s->s_root) {
643 /* initial superblock/root creation */
644 s->s_flags = flags;
645 nfs_fill_super(s, data);
648 mntroot = nfs_get_root(s, &mntfh);
649 if (IS_ERR(mntroot)) {
650 error = PTR_ERR(mntroot);
651 goto error_splat_super;
654 s->s_flags |= MS_ACTIVE;
655 mnt->mnt_sb = s;
656 mnt->mnt_root = mntroot;
657 return 0;
659 out_err_nosb:
660 nfs_free_server(server);
661 out_err_noserver:
662 return error;
664 error_splat_super:
665 up_write(&s->s_umount);
666 deactivate_super(s);
667 return error;
671 * Destroy an NFS2/3 superblock
673 static void nfs_kill_super(struct super_block *s)
675 struct nfs_server *server = NFS_SB(s);
677 kill_anon_super(s);
678 nfs_free_server(server);
682 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
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)
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;
694 dprintk("--> nfs_xdev_get_sb()\n");
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;
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;
710 if (s->s_fs_info != server) {
711 nfs_free_server(server);
712 server = NULL;
715 if (!s->s_root) {
716 /* initial superblock/root creation */
717 s->s_flags = flags;
718 nfs_clone_super(s, data->sb);
721 mntroot = nfs_get_root(s, data->fh);
722 if (IS_ERR(mntroot)) {
723 error = PTR_ERR(mntroot);
724 goto error_splat_super;
727 s->s_flags |= MS_ACTIVE;
728 mnt->mnt_sb = s;
729 mnt->mnt_root = mntroot;
731 dprintk("<-- nfs_xdev_get_sb() = 0\n");
732 return 0;
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;
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;
747 #ifdef CONFIG_NFS_V4
750 * Finish setting up a cloned NFS4 superblock
752 static void nfs4_clone_super(struct super_block *sb,
753 const struct super_block *old_sb)
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);
764 * Set up an NFS4 superblock
766 static void nfs4_fill_super(struct super_block *sb)
768 sb->s_time_gran = 1;
769 sb->s_op = &nfs4_sops;
770 nfs_initialise_sb(sb);
773 static void *nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
775 void *p = NULL;
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);
786 if (copy_from_user(dst, src->data, maxlen)) {
787 kfree(p);
788 return ERR_PTR(-EFAULT);
790 dst[maxlen] = '\0';
791 return dst;
795 * Get the superblock for an NFS4 mountpoint
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)
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;
811 if (data == NULL) {
812 dprintk("%s: missing data argument\n", __FUNCTION__);
813 return -EINVAL;
815 if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
816 dprintk("%s: bad mount version\n", __FUNCTION__);
817 return -EINVAL;
820 /* We now require that the mount process passes the remote address */
821 if (data->host_addrlen != sizeof(addr))
822 return -EINVAL;
824 if (copy_from_user(&addr, data->host_addr, sizeof(addr)))
825 return -EFAULT;
827 if (addr.sin_family != AF_INET ||
828 addr.sin_addr.s_addr == INADDR_ANY
830 dprintk("%s: mount program didn't pass remote IP address!\n",
831 __FUNCTION__);
832 return -EINVAL;
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;
845 if (copy_from_user(&authflavour, data->auth_flavours,
846 sizeof(authflavour))) {
847 error = -EFAULT;
848 goto out_err_noserver;
852 p = nfs_copy_user_string(NULL, &data->hostname, 256);
853 if (IS_ERR(p))
854 goto out_err;
855 hostname = p;
857 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
858 if (IS_ERR(p))
859 goto out_err;
860 mntpath = p;
862 dprintk("MNTPATH: %s\n", mntpath);
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;
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;
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;
884 if (!s->s_root) {
885 /* initial superblock/root creation */
886 s->s_flags = flags;
888 nfs4_fill_super(s);
889 } else {
890 nfs_free_server(server);
893 mntroot = nfs4_get_root(s, &mntfh);
894 if (IS_ERR(mntroot)) {
895 error = PTR_ERR(mntroot);
896 goto error_splat_super;
899 s->s_flags |= MS_ACTIVE;
900 mnt->mnt_sb = s;
901 mnt->mnt_root = mntroot;
902 kfree(mntpath);
903 kfree(hostname);
904 return 0;
906 out_err:
907 error = PTR_ERR(p);
908 goto out_err_noserver;
910 out_free:
911 nfs_free_server(server);
912 out_err_noserver:
913 kfree(mntpath);
914 kfree(hostname);
915 return error;
917 error_splat_super:
918 up_write(&s->s_umount);
919 deactivate_super(s);
920 goto out_err_noserver;
923 static void nfs4_kill_super(struct super_block *sb)
925 struct nfs_server *server = NFS_SB(sb);
927 nfs_return_all_delegations(sb);
928 kill_anon_super(sb);
930 nfs4_renewd_prepare_shutdown(server);
931 nfs_free_server(server);
935 * Clone an NFS4 server record on xdev traversal (FSID-change)
937 static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
938 const char *dev_name, void *raw_data,
939 struct vfsmount *mnt)
941 struct nfs_clone_mount *data = raw_data;
942 struct super_block *s;
943 struct nfs_server *server;
944 struct dentry *mntroot;
945 int error;
947 dprintk("--> nfs4_xdev_get_sb()\n");
949 /* create a new volume representation */
950 server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
951 if (IS_ERR(server)) {
952 error = PTR_ERR(server);
953 goto out_err_noserver;
956 /* Get a superblock - note that we may end up sharing one that already exists */
957 s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
958 if (IS_ERR(s)) {
959 error = PTR_ERR(s);
960 goto out_err_nosb;
963 if (s->s_fs_info != server) {
964 nfs_free_server(server);
965 server = NULL;
968 if (!s->s_root) {
969 /* initial superblock/root creation */
970 s->s_flags = flags;
971 nfs4_clone_super(s, data->sb);
974 mntroot = nfs4_get_root(s, data->fh);
975 if (IS_ERR(mntroot)) {
976 error = PTR_ERR(mntroot);
977 goto error_splat_super;
980 s->s_flags |= MS_ACTIVE;
981 mnt->mnt_sb = s;
982 mnt->mnt_root = mntroot;
984 dprintk("<-- nfs4_xdev_get_sb() = 0\n");
985 return 0;
987 out_err_nosb:
988 nfs_free_server(server);
989 out_err_noserver:
990 dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
991 return error;
993 error_splat_super:
994 up_write(&s->s_umount);
995 deactivate_super(s);
996 dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
997 return error;
1001 * Create an NFS4 server record on referral traversal
1003 static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
1004 const char *dev_name, void *raw_data,
1005 struct vfsmount *mnt)
1007 struct nfs_clone_mount *data = raw_data;
1008 struct super_block *s;
1009 struct nfs_server *server;
1010 struct dentry *mntroot;
1011 struct nfs_fh mntfh;
1012 int error;
1014 dprintk("--> nfs4_referral_get_sb()\n");
1016 /* create a new volume representation */
1017 server = nfs4_create_referral_server(data, &mntfh);
1018 if (IS_ERR(server)) {
1019 error = PTR_ERR(server);
1020 goto out_err_noserver;
1023 /* Get a superblock - note that we may end up sharing one that already exists */
1024 s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1025 if (IS_ERR(s)) {
1026 error = PTR_ERR(s);
1027 goto out_err_nosb;
1030 if (s->s_fs_info != server) {
1031 nfs_free_server(server);
1032 server = NULL;
1035 if (!s->s_root) {
1036 /* initial superblock/root creation */
1037 s->s_flags = flags;
1038 nfs4_fill_super(s);
1041 mntroot = nfs4_get_root(s, data->fh);
1042 if (IS_ERR(mntroot)) {
1043 error = PTR_ERR(mntroot);
1044 goto error_splat_super;
1047 s->s_flags |= MS_ACTIVE;
1048 mnt->mnt_sb = s;
1049 mnt->mnt_root = mntroot;
1051 dprintk("<-- nfs4_referral_get_sb() = 0\n");
1052 return 0;
1054 out_err_nosb:
1055 nfs_free_server(server);
1056 out_err_noserver:
1057 dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
1058 return error;
1060 error_splat_super:
1061 up_write(&s->s_umount);
1062 deactivate_super(s);
1063 dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
1064 return error;
1067 #endif /* CONFIG_NFS_V4 */