blob: 8a505573c2898da482dc79e5f02cae344e0b5641 [file] [log] [blame]
Bryan Schumaker129d1972012-07-16 16:39:13 -04001/*
2 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
3 */
4#include <linux/init.h>
Bryan Schumakerfbdefd62012-07-16 16:39:20 -04005#include <linux/module.h>
Bryan Schumaker129d1972012-07-16 16:39:13 -04006#include <linux/nfs_idmap.h>
Bryan Schumakerfbdefd62012-07-16 16:39:20 -04007#include <linux/nfs4_mount.h>
Bryan Schumaker466bfe72012-07-16 16:39:14 -04008#include <linux/nfs_fs.h>
Bryan Schumakerfbdefd62012-07-16 16:39:20 -04009#include "internal.h"
Bryan Schumaker466bfe72012-07-16 16:39:14 -040010#include "nfs4_fs.h"
Bryan Schumakerab7017a2012-07-30 16:05:16 -040011#include "nfs.h"
Bryan Schumaker129d1972012-07-16 16:39:13 -040012
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040013#define NFSDBG_FACILITY NFSDBG_VFS
14
15static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type,
16 int flags, const char *dev_name, void *raw_data);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040017static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
18 int flags, const char *dev_name, void *raw_data);
19static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
20 int flags, const char *dev_name, void *raw_data);
21
22static struct file_system_type nfs4_fs_type = {
23 .owner = THIS_MODULE,
24 .name = "nfs4",
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,
28};
29
30static struct file_system_type nfs4_remote_fs_type = {
31 .owner = THIS_MODULE,
32 .name = "nfs4",
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,
36};
37
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040038static struct file_system_type nfs4_remote_referral_fs_type = {
39 .owner = THIS_MODULE,
40 .name = "nfs4",
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,
44};
45
46struct file_system_type nfs4_referral_fs_type = {
47 .owner = THIS_MODULE,
48 .name = "nfs4",
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,
52};
53
54static 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,
59 .statfs = nfs_statfs,
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,
67};
68
Bryan Schumakerab7017a2012-07-30 16:05:16 -040069struct nfs_subversion nfs_v4 = {
70 .owner = THIS_MODULE,
71 .nfs_fs = &nfs4_fs_type,
72 .rpc_vers = &nfs_version4,
73 .rpc_ops = &nfs_v4_clientops,
74};
75
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040076/*
77 * Set up an NFS4 superblock
78 */
79static void nfs4_fill_super(struct super_block *sb,
80 struct nfs_mount_info *mount_info)
81{
82 sb->s_time_gran = 1;
83 sb->s_op = &nfs4_sops;
84 /*
85 * The VFS shouldn't apply the umask to mode bits. We will do
86 * so ourselves when necessary.
87 */
88 sb->s_flags |= MS_POSIXACL;
89 sb->s_xattr = nfs4_xattr_handlers;
90 nfs_initialise_sb(sb);
91}
92
93/*
94 * Get the superblock for the NFS4 root partition
95 */
96static struct dentry *
97nfs4_remote_mount(struct file_system_type *fs_type, int flags,
98 const char *dev_name, void *info)
99{
100 struct nfs_mount_info *mount_info = info;
101 struct nfs_server *server;
102 struct dentry *mntroot = ERR_PTR(-ENOMEM);
103
104 mount_info->fill_super = nfs4_fill_super;
105 mount_info->set_security = nfs_set_sb_security;
106
107 /* Get a volume representation */
108 server = nfs4_create_server(mount_info->parsed, mount_info->mntfh);
109 if (IS_ERR(server)) {
110 mntroot = ERR_CAST(server);
111 goto out;
112 }
113
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400114 mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400115
116out:
117 return mntroot;
118}
119
120static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
121 int flags, void *data, const char *hostname)
122{
123 struct vfsmount *root_mnt;
124 char *root_devname;
125 size_t len;
126
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);
134 else
135 snprintf(root_devname, len, "%s:/", hostname);
136 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
137 kfree(root_devname);
138 return root_mnt;
139}
140
141struct nfs_referral_count {
142 struct list_head list;
143 const struct task_struct *task;
144 unsigned int referral_count;
145};
146
147static LIST_HEAD(nfs_referral_count_list);
148static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
149
150static struct nfs_referral_count *nfs_find_referral_count(void)
151{
152 struct nfs_referral_count *p;
153
154 list_for_each_entry(p, &nfs_referral_count_list, list) {
155 if (p->task == current)
156 return p;
157 }
158 return NULL;
159}
160
161#define NFS_MAX_NESTED_REFERRALS 2
162
163static int nfs_referral_loop_protect(void)
164{
165 struct nfs_referral_count *p, *new;
166 int ret = -ENOMEM;
167
168 new = kmalloc(sizeof(*new), GFP_KERNEL);
169 if (!new)
170 goto out;
171 new->task = current;
172 new->referral_count = 1;
173
174 ret = 0;
175 spin_lock(&nfs_referral_count_list_lock);
176 p = nfs_find_referral_count();
177 if (p != NULL) {
178 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
179 ret = -ELOOP;
180 else
181 p->referral_count++;
182 } else {
183 list_add(&new->list, &nfs_referral_count_list);
184 new = NULL;
185 }
186 spin_unlock(&nfs_referral_count_list_lock);
187 kfree(new);
188out:
189 return ret;
190}
191
192static void nfs_referral_loop_unprotect(void)
193{
194 struct nfs_referral_count *p;
195
196 spin_lock(&nfs_referral_count_list_lock);
197 p = nfs_find_referral_count();
198 p->referral_count--;
199 if (p->referral_count == 0)
200 list_del(&p->list);
201 else
202 p = NULL;
203 spin_unlock(&nfs_referral_count_list_lock);
204 kfree(p);
205}
206
207static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
208 const char *export_path)
209{
210 struct dentry *dentry;
211 int err;
212
213 if (IS_ERR(root_mnt))
214 return ERR_CAST(root_mnt);
215
216 err = nfs_referral_loop_protect();
217 if (err) {
218 mntput(root_mnt);
219 return ERR_PTR(err);
220 }
221
222 dentry = mount_subtree(root_mnt, export_path);
223 nfs_referral_loop_unprotect();
224
225 return dentry;
226}
227
228struct dentry *nfs4_try_mount(int flags, const char *dev_name,
229 struct nfs_mount_info *mount_info)
230{
231 char *export_path;
232 struct vfsmount *root_mnt;
233 struct dentry *res;
234 struct nfs_parsed_mount_data *data = mount_info->parsed;
235
236 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
237
238 mount_info->fill_super = nfs4_fill_super;
239
240 export_path = data->nfs_server.export_path;
241 data->nfs_server.export_path = "/";
242 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
243 data->nfs_server.hostname);
244 data->nfs_server.export_path = export_path;
245
246 res = nfs_follow_remote_path(root_mnt, export_path);
247
248 dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
249 IS_ERR(res) ? PTR_ERR(res) : 0,
250 IS_ERR(res) ? " [error]" : "");
251 return res;
252}
253
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400254static struct dentry *
255nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
256 const char *dev_name, void *raw_data)
257{
258 struct nfs_mount_info mount_info = {
259 .fill_super = nfs4_fill_super,
260 .set_security = nfs_clone_sb_security,
261 .cloned = raw_data,
262 };
263 struct nfs_server *server;
264 struct dentry *mntroot = ERR_PTR(-ENOMEM);
265
266 dprintk("--> nfs4_referral_get_sb()\n");
267
268 mount_info.mntfh = nfs_alloc_fhandle();
269 if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
270 goto out;
271
272 /* create a new volume representation */
273 server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
274 if (IS_ERR(server)) {
275 mntroot = ERR_CAST(server);
276 goto out;
277 }
278
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400279 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400280out:
281 nfs_free_fhandle(mount_info.mntfh);
282 return mntroot;
283}
284
285/*
286 * Create an NFS4 server record on referral traversal
287 */
288static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
289 int flags, const char *dev_name, void *raw_data)
290{
291 struct nfs_clone_mount *data = raw_data;
292 char *export_path;
293 struct vfsmount *root_mnt;
294 struct dentry *res;
295
296 dprintk("--> nfs4_referral_mount()\n");
297
298 export_path = data->mnt_path;
299 data->mnt_path = "/";
300
301 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
302 flags, data, data->hostname);
303 data->mnt_path = export_path;
304
305 res = nfs_follow_remote_path(root_mnt, export_path);
306 dprintk("<-- nfs4_referral_mount() = %ld%s\n",
307 IS_ERR(res) ? PTR_ERR(res) : 0,
308 IS_ERR(res) ? " [error]" : "");
309 return res;
310}
311
312
Bryan Schumaker129d1972012-07-16 16:39:13 -0400313int __init init_nfs_v4(void)
314{
315 int err;
316
317 err = nfs_idmap_init();
318 if (err)
319 goto out;
320
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400321 err = nfs4_register_sysctl();
322 if (err)
323 goto out1;
324
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400325 err = register_filesystem(&nfs4_fs_type);
326 if (err < 0)
327 goto out2;
328
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400329 register_nfs_version(&nfs_v4);
Bryan Schumaker129d1972012-07-16 16:39:13 -0400330 return 0;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400331out2:
332 nfs4_unregister_sysctl();
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400333out1:
334 nfs_idmap_quit();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400335out:
336 return err;
337}
338
Bryan Schumakerbb6e0712012-07-17 15:18:30 -0400339void exit_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400340{
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400341 unregister_nfs_version(&nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400342 unregister_filesystem(&nfs4_fs_type);
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400343 nfs4_unregister_sysctl();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400344 nfs_idmap_quit();
345}