NFS: Add version registering framework
[deliverable/linux.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_xdev_mount(struct file_system_type *fs_type,
18 int flags, const char *dev_name, void *raw_data);
19 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
20 int flags, const char *dev_name, void *raw_data);
21 static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
22 int flags, const char *dev_name, void *raw_data);
23
24 static struct file_system_type nfs4_fs_type = {
25 .owner = THIS_MODULE,
26 .name = "nfs4",
27 .mount = nfs_fs_mount,
28 .kill_sb = nfs_kill_super,
29 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
30 };
31
32 static struct file_system_type nfs4_remote_fs_type = {
33 .owner = THIS_MODULE,
34 .name = "nfs4",
35 .mount = nfs4_remote_mount,
36 .kill_sb = nfs_kill_super,
37 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
38 };
39
40 struct file_system_type nfs4_xdev_fs_type = {
41 .owner = THIS_MODULE,
42 .name = "nfs4",
43 .mount = nfs4_xdev_mount,
44 .kill_sb = nfs_kill_super,
45 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
46 };
47
48 static struct file_system_type nfs4_remote_referral_fs_type = {
49 .owner = THIS_MODULE,
50 .name = "nfs4",
51 .mount = nfs4_remote_referral_mount,
52 .kill_sb = nfs_kill_super,
53 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
54 };
55
56 struct file_system_type nfs4_referral_fs_type = {
57 .owner = THIS_MODULE,
58 .name = "nfs4",
59 .mount = nfs4_referral_mount,
60 .kill_sb = nfs_kill_super,
61 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
62 };
63
64 static const struct super_operations nfs4_sops = {
65 .alloc_inode = nfs_alloc_inode,
66 .destroy_inode = nfs_destroy_inode,
67 .write_inode = nfs4_write_inode,
68 .put_super = nfs_put_super,
69 .statfs = nfs_statfs,
70 .evict_inode = nfs4_evict_inode,
71 .umount_begin = nfs_umount_begin,
72 .show_options = nfs_show_options,
73 .show_devname = nfs_show_devname,
74 .show_path = nfs_show_path,
75 .show_stats = nfs_show_stats,
76 .remount_fs = nfs_remount,
77 };
78
79 struct nfs_subversion nfs_v4 = {
80 .owner = THIS_MODULE,
81 .nfs_fs = &nfs4_fs_type,
82 .rpc_vers = &nfs_version4,
83 .rpc_ops = &nfs_v4_clientops,
84 };
85
86 /*
87 * Set up an NFS4 superblock
88 */
89 static void nfs4_fill_super(struct super_block *sb,
90 struct nfs_mount_info *mount_info)
91 {
92 sb->s_time_gran = 1;
93 sb->s_op = &nfs4_sops;
94 /*
95 * The VFS shouldn't apply the umask to mode bits. We will do
96 * so ourselves when necessary.
97 */
98 sb->s_flags |= MS_POSIXACL;
99 sb->s_xattr = nfs4_xattr_handlers;
100 nfs_initialise_sb(sb);
101 }
102
103 /*
104 * Get the superblock for the NFS4 root partition
105 */
106 static struct dentry *
107 nfs4_remote_mount(struct file_system_type *fs_type, int flags,
108 const char *dev_name, void *info)
109 {
110 struct nfs_mount_info *mount_info = info;
111 struct nfs_server *server;
112 struct dentry *mntroot = ERR_PTR(-ENOMEM);
113
114 mount_info->fill_super = nfs4_fill_super;
115 mount_info->set_security = nfs_set_sb_security;
116
117 /* Get a volume representation */
118 server = nfs4_create_server(mount_info->parsed, mount_info->mntfh);
119 if (IS_ERR(server)) {
120 mntroot = ERR_CAST(server);
121 goto out;
122 }
123
124 mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
125
126 out:
127 return mntroot;
128 }
129
130 static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
131 int flags, void *data, const char *hostname)
132 {
133 struct vfsmount *root_mnt;
134 char *root_devname;
135 size_t len;
136
137 len = strlen(hostname) + 5;
138 root_devname = kmalloc(len, GFP_KERNEL);
139 if (root_devname == NULL)
140 return ERR_PTR(-ENOMEM);
141 /* Does hostname needs to be enclosed in brackets? */
142 if (strchr(hostname, ':'))
143 snprintf(root_devname, len, "[%s]:/", hostname);
144 else
145 snprintf(root_devname, len, "%s:/", hostname);
146 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
147 kfree(root_devname);
148 return root_mnt;
149 }
150
151 struct nfs_referral_count {
152 struct list_head list;
153 const struct task_struct *task;
154 unsigned int referral_count;
155 };
156
157 static LIST_HEAD(nfs_referral_count_list);
158 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
159
160 static struct nfs_referral_count *nfs_find_referral_count(void)
161 {
162 struct nfs_referral_count *p;
163
164 list_for_each_entry(p, &nfs_referral_count_list, list) {
165 if (p->task == current)
166 return p;
167 }
168 return NULL;
169 }
170
171 #define NFS_MAX_NESTED_REFERRALS 2
172
173 static int nfs_referral_loop_protect(void)
174 {
175 struct nfs_referral_count *p, *new;
176 int ret = -ENOMEM;
177
178 new = kmalloc(sizeof(*new), GFP_KERNEL);
179 if (!new)
180 goto out;
181 new->task = current;
182 new->referral_count = 1;
183
184 ret = 0;
185 spin_lock(&nfs_referral_count_list_lock);
186 p = nfs_find_referral_count();
187 if (p != NULL) {
188 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
189 ret = -ELOOP;
190 else
191 p->referral_count++;
192 } else {
193 list_add(&new->list, &nfs_referral_count_list);
194 new = NULL;
195 }
196 spin_unlock(&nfs_referral_count_list_lock);
197 kfree(new);
198 out:
199 return ret;
200 }
201
202 static void nfs_referral_loop_unprotect(void)
203 {
204 struct nfs_referral_count *p;
205
206 spin_lock(&nfs_referral_count_list_lock);
207 p = nfs_find_referral_count();
208 p->referral_count--;
209 if (p->referral_count == 0)
210 list_del(&p->list);
211 else
212 p = NULL;
213 spin_unlock(&nfs_referral_count_list_lock);
214 kfree(p);
215 }
216
217 static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
218 const char *export_path)
219 {
220 struct dentry *dentry;
221 int err;
222
223 if (IS_ERR(root_mnt))
224 return ERR_CAST(root_mnt);
225
226 err = nfs_referral_loop_protect();
227 if (err) {
228 mntput(root_mnt);
229 return ERR_PTR(err);
230 }
231
232 dentry = mount_subtree(root_mnt, export_path);
233 nfs_referral_loop_unprotect();
234
235 return dentry;
236 }
237
238 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
239 struct nfs_mount_info *mount_info)
240 {
241 char *export_path;
242 struct vfsmount *root_mnt;
243 struct dentry *res;
244 struct nfs_parsed_mount_data *data = mount_info->parsed;
245
246 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
247
248 mount_info->fill_super = nfs4_fill_super;
249
250 export_path = data->nfs_server.export_path;
251 data->nfs_server.export_path = "/";
252 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
253 data->nfs_server.hostname);
254 data->nfs_server.export_path = export_path;
255
256 res = nfs_follow_remote_path(root_mnt, export_path);
257
258 dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
259 IS_ERR(res) ? PTR_ERR(res) : 0,
260 IS_ERR(res) ? " [error]" : "");
261 return res;
262 }
263
264 /*
265 * Clone an NFS4 server record on xdev traversal (FSID-change)
266 */
267 static struct dentry *
268 nfs4_xdev_mount(struct file_system_type *fs_type, int flags,
269 const char *dev_name, void *raw_data)
270 {
271 struct nfs_mount_info mount_info = {
272 .fill_super = nfs_clone_super,
273 .set_security = nfs_clone_sb_security,
274 .cloned = raw_data,
275 };
276 return nfs_xdev_mount_common(&nfs4_fs_type, flags, dev_name, &mount_info);
277 }
278
279 static struct dentry *
280 nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
281 const char *dev_name, void *raw_data)
282 {
283 struct nfs_mount_info mount_info = {
284 .fill_super = nfs4_fill_super,
285 .set_security = nfs_clone_sb_security,
286 .cloned = raw_data,
287 };
288 struct nfs_server *server;
289 struct dentry *mntroot = ERR_PTR(-ENOMEM);
290
291 dprintk("--> nfs4_referral_get_sb()\n");
292
293 mount_info.mntfh = nfs_alloc_fhandle();
294 if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
295 goto out;
296
297 /* create a new volume representation */
298 server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
299 if (IS_ERR(server)) {
300 mntroot = ERR_CAST(server);
301 goto out;
302 }
303
304 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
305 out:
306 nfs_free_fhandle(mount_info.mntfh);
307 return mntroot;
308 }
309
310 /*
311 * Create an NFS4 server record on referral traversal
312 */
313 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
314 int flags, const char *dev_name, void *raw_data)
315 {
316 struct nfs_clone_mount *data = raw_data;
317 char *export_path;
318 struct vfsmount *root_mnt;
319 struct dentry *res;
320
321 dprintk("--> nfs4_referral_mount()\n");
322
323 export_path = data->mnt_path;
324 data->mnt_path = "/";
325
326 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
327 flags, data, data->hostname);
328 data->mnt_path = export_path;
329
330 res = nfs_follow_remote_path(root_mnt, export_path);
331 dprintk("<-- nfs4_referral_mount() = %ld%s\n",
332 IS_ERR(res) ? PTR_ERR(res) : 0,
333 IS_ERR(res) ? " [error]" : "");
334 return res;
335 }
336
337
338 int __init init_nfs_v4(void)
339 {
340 int err;
341
342 err = nfs_idmap_init();
343 if (err)
344 goto out;
345
346 err = nfs4_register_sysctl();
347 if (err)
348 goto out1;
349
350 err = register_filesystem(&nfs4_fs_type);
351 if (err < 0)
352 goto out2;
353
354 register_nfs_version(&nfs_v4);
355 return 0;
356 out2:
357 nfs4_unregister_sysctl();
358 out1:
359 nfs_idmap_quit();
360 out:
361 return err;
362 }
363
364 void exit_nfs_v4(void)
365 {
366 unregister_nfs_version(&nfs_v4);
367 unregister_filesystem(&nfs4_fs_type);
368 nfs4_unregister_sysctl();
369 nfs_idmap_quit();
370 }
This page took 0.038079 seconds and 5 git commands to generate.