NFS: Only initialize the ACL client in the v3 case
[linux-2.6-block.git] / fs / nfs / nfs4super.c
CommitLineData
129d1977
BS
1/*
2 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
3 */
4#include <linux/init.h>
fbdefd64 5#include <linux/module.h>
129d1977 6#include <linux/nfs_idmap.h>
fbdefd64 7#include <linux/nfs4_mount.h>
466bfe7f 8#include <linux/nfs_fs.h>
fbdefd64 9#include "internal.h"
466bfe7f 10#include "nfs4_fs.h"
ab7017a3 11#include "nfs.h"
129d1977 12
fbdefd64
BS
13#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);
fbdefd64
BS
17static 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
fbdefd64
BS
38static 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
ab7017a3
BS
69struct 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
fbdefd64
BS
76/*
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 */
1179acc6 108 server = nfs4_create_server(mount_info, &nfs_v4);
fbdefd64
BS
109 if (IS_ERR(server)) {
110 mntroot = ERR_CAST(server);
111 goto out;
112 }
113
ab7017a3 114 mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
fbdefd64
BS
115
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,
ff9099f2
BS
229 struct nfs_mount_info *mount_info,
230 struct nfs_subversion *nfs_mod)
fbdefd64
BS
231{
232 char *export_path;
233 struct vfsmount *root_mnt;
234 struct dentry *res;
235 struct nfs_parsed_mount_data *data = mount_info->parsed;
236
237 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
238
239 mount_info->fill_super = nfs4_fill_super;
240
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;
246
247 res = nfs_follow_remote_path(root_mnt, export_path);
248
249 dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
250 IS_ERR(res) ? PTR_ERR(res) : 0,
251 IS_ERR(res) ? " [error]" : "");
252 return res;
253}
254
fbdefd64
BS
255static struct dentry *
256nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
257 const char *dev_name, void *raw_data)
258{
259 struct nfs_mount_info mount_info = {
260 .fill_super = nfs4_fill_super,
261 .set_security = nfs_clone_sb_security,
262 .cloned = raw_data,
263 };
264 struct nfs_server *server;
265 struct dentry *mntroot = ERR_PTR(-ENOMEM);
266
267 dprintk("--> nfs4_referral_get_sb()\n");
268
269 mount_info.mntfh = nfs_alloc_fhandle();
270 if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
271 goto out;
272
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);
277 goto out;
278 }
279
ab7017a3 280 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
fbdefd64
BS
281out:
282 nfs_free_fhandle(mount_info.mntfh);
283 return mntroot;
284}
285
286/*
287 * Create an NFS4 server record on referral traversal
288 */
289static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
290 int flags, const char *dev_name, void *raw_data)
291{
292 struct nfs_clone_mount *data = raw_data;
293 char *export_path;
294 struct vfsmount *root_mnt;
295 struct dentry *res;
296
297 dprintk("--> nfs4_referral_mount()\n");
298
299 export_path = data->mnt_path;
300 data->mnt_path = "/";
301
302 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
303 flags, data, data->hostname);
304 data->mnt_path = export_path;
305
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]" : "");
310 return res;
311}
312
313
129d1977
BS
314int __init init_nfs_v4(void)
315{
316 int err;
317
318 err = nfs_idmap_init();
319 if (err)
320 goto out;
321
466bfe7f
BS
322 err = nfs4_register_sysctl();
323 if (err)
324 goto out1;
325
fbdefd64
BS
326 err = register_filesystem(&nfs4_fs_type);
327 if (err < 0)
328 goto out2;
329
ab7017a3 330 register_nfs_version(&nfs_v4);
129d1977 331 return 0;
fbdefd64
BS
332out2:
333 nfs4_unregister_sysctl();
466bfe7f
BS
334out1:
335 nfs_idmap_quit();
129d1977
BS
336out:
337 return err;
338}
339
bb6e071f 340void exit_nfs_v4(void)
129d1977 341{
ab7017a3 342 unregister_nfs_version(&nfs_v4);
fbdefd64 343 unregister_filesystem(&nfs4_fs_type);
466bfe7f 344 nfs4_unregister_sysctl();
129d1977
BS
345 nfs_idmap_quit();
346}