2 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/nfs_idmap.h>
7 #include <linux/nfs4_mount.h>
8 #include <linux/nfs_fs.h>
13 #define NFSDBG_FACILITY NFSDBG_VFS
15 static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type,
16 int flags, const char *dev_name, void *raw_data);
17 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
18 int flags, const char *dev_name, void *raw_data);
19 static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
20 int flags, const char *dev_name, void *raw_data);
22 static struct file_system_type nfs4_fs_type = {
25 .mount = nfs_fs_mount,
26 .kill_sb = nfs_kill_super,
27 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
30 static struct file_system_type nfs4_remote_fs_type = {
33 .mount = nfs4_remote_mount,
34 .kill_sb = nfs_kill_super,
35 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
38 static struct file_system_type nfs4_remote_referral_fs_type = {
41 .mount = nfs4_remote_referral_mount,
42 .kill_sb = nfs_kill_super,
43 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
46 struct file_system_type nfs4_referral_fs_type = {
49 .mount = nfs4_referral_mount,
50 .kill_sb = nfs_kill_super,
51 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
54 static const struct super_operations nfs4_sops = {
55 .alloc_inode = nfs_alloc_inode,
56 .destroy_inode = nfs_destroy_inode,
57 .write_inode = nfs4_write_inode,
58 .put_super = nfs_put_super,
60 .evict_inode = nfs4_evict_inode,
61 .umount_begin = nfs_umount_begin,
62 .show_options = nfs_show_options,
63 .show_devname = nfs_show_devname,
64 .show_path = nfs_show_path,
65 .show_stats = nfs_show_stats,
66 .remount_fs = nfs_remount,
69 struct nfs_subversion nfs_v4 = {
71 .nfs_fs = &nfs4_fs_type,
72 .rpc_vers = &nfs_version4,
73 .rpc_ops = &nfs_v4_clientops,
77 * Set up an NFS4 superblock
79 static void nfs4_fill_super(struct super_block *sb,
80 struct nfs_mount_info *mount_info)
83 sb->s_op = &nfs4_sops;
85 * The VFS shouldn't apply the umask to mode bits. We will do
86 * so ourselves when necessary.
88 sb->s_flags |= MS_POSIXACL;
89 sb->s_xattr = nfs4_xattr_handlers;
90 nfs_initialise_sb(sb);
94 * Get the superblock for the NFS4 root partition
96 static struct dentry *
97 nfs4_remote_mount(struct file_system_type *fs_type, int flags,
98 const char *dev_name, void *info)
100 struct nfs_mount_info *mount_info = info;
101 struct nfs_server *server;
102 struct dentry *mntroot = ERR_PTR(-ENOMEM);
104 mount_info->fill_super = nfs4_fill_super;
105 mount_info->set_security = nfs_set_sb_security;
107 /* Get a volume representation */
108 server = nfs4_create_server(mount_info, &nfs_v4);
109 if (IS_ERR(server)) {
110 mntroot = ERR_CAST(server);
114 mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
120 static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
121 int flags, void *data, const char *hostname)
123 struct vfsmount *root_mnt;
127 len = strlen(hostname) + 5;
128 root_devname = kmalloc(len, GFP_KERNEL);
129 if (root_devname == NULL)
130 return ERR_PTR(-ENOMEM);
131 /* Does hostname needs to be enclosed in brackets? */
132 if (strchr(hostname, ':'))
133 snprintf(root_devname, len, "[%s]:/", hostname);
135 snprintf(root_devname, len, "%s:/", hostname);
136 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
141 struct nfs_referral_count {
142 struct list_head list;
143 const struct task_struct *task;
144 unsigned int referral_count;
147 static LIST_HEAD(nfs_referral_count_list);
148 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
150 static struct nfs_referral_count *nfs_find_referral_count(void)
152 struct nfs_referral_count *p;
154 list_for_each_entry(p, &nfs_referral_count_list, list) {
155 if (p->task == current)
161 #define NFS_MAX_NESTED_REFERRALS 2
163 static int nfs_referral_loop_protect(void)
165 struct nfs_referral_count *p, *new;
168 new = kmalloc(sizeof(*new), GFP_KERNEL);
172 new->referral_count = 1;
175 spin_lock(&nfs_referral_count_list_lock);
176 p = nfs_find_referral_count();
178 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
183 list_add(&new->list, &nfs_referral_count_list);
186 spin_unlock(&nfs_referral_count_list_lock);
192 static void nfs_referral_loop_unprotect(void)
194 struct nfs_referral_count *p;
196 spin_lock(&nfs_referral_count_list_lock);
197 p = nfs_find_referral_count();
199 if (p->referral_count == 0)
203 spin_unlock(&nfs_referral_count_list_lock);
207 static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
208 const char *export_path)
210 struct dentry *dentry;
213 if (IS_ERR(root_mnt))
214 return ERR_CAST(root_mnt);
216 err = nfs_referral_loop_protect();
222 dentry = mount_subtree(root_mnt, export_path);
223 nfs_referral_loop_unprotect();
228 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
229 struct nfs_mount_info *mount_info,
230 struct nfs_subversion *nfs_mod)
233 struct vfsmount *root_mnt;
235 struct nfs_parsed_mount_data *data = mount_info->parsed;
237 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
239 mount_info->fill_super = nfs4_fill_super;
241 export_path = data->nfs_server.export_path;
242 data->nfs_server.export_path = "/";
243 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
244 data->nfs_server.hostname);
245 data->nfs_server.export_path = export_path;
247 res = nfs_follow_remote_path(root_mnt, export_path);
249 dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
250 IS_ERR(res) ? PTR_ERR(res) : 0,
251 IS_ERR(res) ? " [error]" : "");
255 static struct dentry *
256 nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
257 const char *dev_name, void *raw_data)
259 struct nfs_mount_info mount_info = {
260 .fill_super = nfs4_fill_super,
261 .set_security = nfs_clone_sb_security,
264 struct nfs_server *server;
265 struct dentry *mntroot = ERR_PTR(-ENOMEM);
267 dprintk("--> nfs4_referral_get_sb()\n");
269 mount_info.mntfh = nfs_alloc_fhandle();
270 if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
273 /* create a new volume representation */
274 server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
275 if (IS_ERR(server)) {
276 mntroot = ERR_CAST(server);
280 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
282 nfs_free_fhandle(mount_info.mntfh);
287 * Create an NFS4 server record on referral traversal
289 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
290 int flags, const char *dev_name, void *raw_data)
292 struct nfs_clone_mount *data = raw_data;
294 struct vfsmount *root_mnt;
297 dprintk("--> nfs4_referral_mount()\n");
299 export_path = data->mnt_path;
300 data->mnt_path = "/";
302 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
303 flags, data, data->hostname);
304 data->mnt_path = export_path;
306 res = nfs_follow_remote_path(root_mnt, export_path);
307 dprintk("<-- nfs4_referral_mount() = %ld%s\n",
308 IS_ERR(res) ? PTR_ERR(res) : 0,
309 IS_ERR(res) ? " [error]" : "");
314 int __init init_nfs_v4(void)
318 err = nfs_idmap_init();
322 err = nfs4_register_sysctl();
326 err = register_filesystem(&nfs4_fs_type);
330 register_nfs_version(&nfs_v4);
333 nfs4_unregister_sysctl();
340 void exit_nfs_v4(void)
342 unregister_nfs_version(&nfs_v4);
343 unregister_filesystem(&nfs4_fs_type);
344 nfs4_unregister_sysctl();