NFS: Only initialize the ACL client in the v3 case
[linux-2.6-block.git] / fs / nfs / nfs4super.c
1 /*
2  * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
3  */
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>
9 #include "internal.h"
10 #include "nfs4_fs.h"
11 #include "nfs.h"
12
13 #define NFSDBG_FACILITY         NFSDBG_VFS
14
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);
21
22 static 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
30 static 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
38 static 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
46 struct 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
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,
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
69 struct 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
76 /*
77  * Set up an NFS4 superblock
78  */
79 static 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  */
96 static struct dentry *
97 nfs4_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, &nfs_v4);
109         if (IS_ERR(server)) {
110                 mntroot = ERR_CAST(server);
111                 goto out;
112         }
113
114         mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
115
116 out:
117         return mntroot;
118 }
119
120 static 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
141 struct nfs_referral_count {
142         struct list_head list;
143         const struct task_struct *task;
144         unsigned int referral_count;
145 };
146
147 static LIST_HEAD(nfs_referral_count_list);
148 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
149
150 static 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
163 static 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);
188 out:
189         return ret;
190 }
191
192 static 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
207 static 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
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)
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
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)
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
280         mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
281 out:
282         nfs_free_fhandle(mount_info.mntfh);
283         return mntroot;
284 }
285
286 /*
287  * Create an NFS4 server record on referral traversal
288  */
289 static 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
314 int __init init_nfs_v4(void)
315 {
316         int err;
317
318         err = nfs_idmap_init();
319         if (err)
320                 goto out;
321
322         err = nfs4_register_sysctl();
323         if (err)
324                 goto out1;
325
326         err = register_filesystem(&nfs4_fs_type);
327         if (err < 0)
328                 goto out2;
329
330         register_nfs_version(&nfs_v4);
331         return 0;
332 out2:
333         nfs4_unregister_sysctl();
334 out1:
335         nfs_idmap_quit();
336 out:
337         return err;
338 }
339
340 void exit_nfs_v4(void)
341 {
342         unregister_nfs_version(&nfs_v4);
343         unregister_filesystem(&nfs4_fs_type);
344         nfs4_unregister_sysctl();
345         nfs_idmap_quit();
346 }