blob: bd61221ad2c5542b08f42249d10285c159f66b1d [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 Schumaker19d87ca2012-07-30 16:05:21 -04009#include "delegation.h"
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040010#include "internal.h"
Bryan Schumaker466bfe72012-07-16 16:39:14 -040011#include "nfs4_fs.h"
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040012#include "pnfs.h"
Bryan Schumakerab7017a2012-07-30 16:05:16 -040013#include "nfs.h"
Bryan Schumaker129d1972012-07-16 16:39:13 -040014
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040015#define NFSDBG_FACILITY NFSDBG_VFS
16
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040017static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
18static void nfs4_evict_inode(struct inode *inode);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040019static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type,
20 int flags, const char *dev_name, void *raw_data);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040021static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
22 int flags, const char *dev_name, void *raw_data);
23static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
24 int flags, const char *dev_name, void *raw_data);
25
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040026static struct file_system_type nfs4_remote_fs_type = {
27 .owner = THIS_MODULE,
28 .name = "nfs4",
29 .mount = nfs4_remote_mount,
30 .kill_sb = nfs_kill_super,
31 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
32};
33
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040034static struct file_system_type nfs4_remote_referral_fs_type = {
35 .owner = THIS_MODULE,
36 .name = "nfs4",
37 .mount = nfs4_remote_referral_mount,
38 .kill_sb = nfs_kill_super,
39 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
40};
41
42struct file_system_type nfs4_referral_fs_type = {
43 .owner = THIS_MODULE,
44 .name = "nfs4",
45 .mount = nfs4_referral_mount,
46 .kill_sb = nfs_kill_super,
47 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
48};
49
50static const struct super_operations nfs4_sops = {
51 .alloc_inode = nfs_alloc_inode,
52 .destroy_inode = nfs_destroy_inode,
53 .write_inode = nfs4_write_inode,
54 .put_super = nfs_put_super,
55 .statfs = nfs_statfs,
56 .evict_inode = nfs4_evict_inode,
57 .umount_begin = nfs_umount_begin,
58 .show_options = nfs_show_options,
59 .show_devname = nfs_show_devname,
60 .show_path = nfs_show_path,
61 .show_stats = nfs_show_stats,
62 .remount_fs = nfs_remount,
63};
64
Bryan Schumakerab7017a2012-07-30 16:05:16 -040065struct nfs_subversion nfs_v4 = {
66 .owner = THIS_MODULE,
67 .nfs_fs = &nfs4_fs_type,
68 .rpc_vers = &nfs_version4,
69 .rpc_ops = &nfs_v4_clientops,
Bryan Schumaker6a744902012-07-30 16:05:20 -040070 .sops = &nfs4_sops,
71 .xattr = nfs4_xattr_handlers,
Bryan Schumakerab7017a2012-07-30 16:05:16 -040072};
73
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040074static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
75{
76 int ret = nfs_write_inode(inode, wbc);
77
78 if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
79 int status;
80 bool sync = true;
81
82 if (wbc->sync_mode == WB_SYNC_NONE)
83 sync = false;
84
85 status = pnfs_layoutcommit_inode(inode, sync);
86 if (status < 0)
87 return status;
88 }
89 return ret;
90}
91
92/*
93 * Clean out any remaining NFSv4 state that might be left over due
94 * to open() calls that passed nfs_atomic_lookup, but failed to call
95 * nfs_open().
96 */
97static void nfs4_evict_inode(struct inode *inode)
98{
99 truncate_inode_pages(&inode->i_data, 0);
100 clear_inode(inode);
101 pnfs_return_layout(inode);
102 pnfs_destroy_layout(NFS_I(inode));
103 /* If we are holding a delegation, return it! */
104 nfs_inode_return_delegation_noreclaim(inode);
105 /* First call standard NFS clear_inode() code */
106 nfs_clear_inode(inode);
107}
108
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400109/*
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400110 * Get the superblock for the NFS4 root partition
111 */
112static struct dentry *
113nfs4_remote_mount(struct file_system_type *fs_type, int flags,
114 const char *dev_name, void *info)
115{
116 struct nfs_mount_info *mount_info = info;
117 struct nfs_server *server;
118 struct dentry *mntroot = ERR_PTR(-ENOMEM);
119
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400120 mount_info->set_security = nfs_set_sb_security;
121
122 /* Get a volume representation */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400123 server = nfs4_create_server(mount_info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400124 if (IS_ERR(server)) {
125 mntroot = ERR_CAST(server);
126 goto out;
127 }
128
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400129 mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400130
131out:
132 return mntroot;
133}
134
135static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
136 int flags, void *data, const char *hostname)
137{
138 struct vfsmount *root_mnt;
139 char *root_devname;
140 size_t len;
141
142 len = strlen(hostname) + 5;
143 root_devname = kmalloc(len, GFP_KERNEL);
144 if (root_devname == NULL)
145 return ERR_PTR(-ENOMEM);
146 /* Does hostname needs to be enclosed in brackets? */
147 if (strchr(hostname, ':'))
148 snprintf(root_devname, len, "[%s]:/", hostname);
149 else
150 snprintf(root_devname, len, "%s:/", hostname);
151 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
152 kfree(root_devname);
153 return root_mnt;
154}
155
156struct nfs_referral_count {
157 struct list_head list;
158 const struct task_struct *task;
159 unsigned int referral_count;
160};
161
162static LIST_HEAD(nfs_referral_count_list);
163static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
164
165static struct nfs_referral_count *nfs_find_referral_count(void)
166{
167 struct nfs_referral_count *p;
168
169 list_for_each_entry(p, &nfs_referral_count_list, list) {
170 if (p->task == current)
171 return p;
172 }
173 return NULL;
174}
175
176#define NFS_MAX_NESTED_REFERRALS 2
177
178static int nfs_referral_loop_protect(void)
179{
180 struct nfs_referral_count *p, *new;
181 int ret = -ENOMEM;
182
183 new = kmalloc(sizeof(*new), GFP_KERNEL);
184 if (!new)
185 goto out;
186 new->task = current;
187 new->referral_count = 1;
188
189 ret = 0;
190 spin_lock(&nfs_referral_count_list_lock);
191 p = nfs_find_referral_count();
192 if (p != NULL) {
193 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
194 ret = -ELOOP;
195 else
196 p->referral_count++;
197 } else {
198 list_add(&new->list, &nfs_referral_count_list);
199 new = NULL;
200 }
201 spin_unlock(&nfs_referral_count_list_lock);
202 kfree(new);
203out:
204 return ret;
205}
206
207static void nfs_referral_loop_unprotect(void)
208{
209 struct nfs_referral_count *p;
210
211 spin_lock(&nfs_referral_count_list_lock);
212 p = nfs_find_referral_count();
213 p->referral_count--;
214 if (p->referral_count == 0)
215 list_del(&p->list);
216 else
217 p = NULL;
218 spin_unlock(&nfs_referral_count_list_lock);
219 kfree(p);
220}
221
222static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
223 const char *export_path)
224{
225 struct dentry *dentry;
226 int err;
227
228 if (IS_ERR(root_mnt))
229 return ERR_CAST(root_mnt);
230
231 err = nfs_referral_loop_protect();
232 if (err) {
233 mntput(root_mnt);
234 return ERR_PTR(err);
235 }
236
237 dentry = mount_subtree(root_mnt, export_path);
238 nfs_referral_loop_unprotect();
239
240 return dentry;
241}
242
243struct dentry *nfs4_try_mount(int flags, const char *dev_name,
Bryan Schumakerff9099f22012-07-30 16:05:18 -0400244 struct nfs_mount_info *mount_info,
245 struct nfs_subversion *nfs_mod)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400246{
247 char *export_path;
248 struct vfsmount *root_mnt;
249 struct dentry *res;
250 struct nfs_parsed_mount_data *data = mount_info->parsed;
251
252 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
253
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400254 export_path = data->nfs_server.export_path;
255 data->nfs_server.export_path = "/";
256 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
257 data->nfs_server.hostname);
258 data->nfs_server.export_path = export_path;
259
260 res = nfs_follow_remote_path(root_mnt, export_path);
261
262 dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
263 IS_ERR(res) ? PTR_ERR(res) : 0,
264 IS_ERR(res) ? " [error]" : "");
265 return res;
266}
267
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400268static struct dentry *
269nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
270 const char *dev_name, void *raw_data)
271{
272 struct nfs_mount_info mount_info = {
Bryan Schumaker6a744902012-07-30 16:05:20 -0400273 .fill_super = nfs_fill_super,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400274 .set_security = nfs_clone_sb_security,
275 .cloned = raw_data,
276 };
277 struct nfs_server *server;
278 struct dentry *mntroot = ERR_PTR(-ENOMEM);
279
280 dprintk("--> nfs4_referral_get_sb()\n");
281
282 mount_info.mntfh = nfs_alloc_fhandle();
283 if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
284 goto out;
285
286 /* create a new volume representation */
287 server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
288 if (IS_ERR(server)) {
289 mntroot = ERR_CAST(server);
290 goto out;
291 }
292
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400293 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400294out:
295 nfs_free_fhandle(mount_info.mntfh);
296 return mntroot;
297}
298
299/*
300 * Create an NFS4 server record on referral traversal
301 */
302static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
303 int flags, const char *dev_name, void *raw_data)
304{
305 struct nfs_clone_mount *data = raw_data;
306 char *export_path;
307 struct vfsmount *root_mnt;
308 struct dentry *res;
309
310 dprintk("--> nfs4_referral_mount()\n");
311
312 export_path = data->mnt_path;
313 data->mnt_path = "/";
314
315 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
316 flags, data, data->hostname);
317 data->mnt_path = export_path;
318
319 res = nfs_follow_remote_path(root_mnt, export_path);
320 dprintk("<-- nfs4_referral_mount() = %ld%s\n",
321 IS_ERR(res) ? PTR_ERR(res) : 0,
322 IS_ERR(res) ? " [error]" : "");
323 return res;
324}
325
326
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400327static int __init init_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400328{
329 int err;
330
331 err = nfs_idmap_init();
332 if (err)
333 goto out;
334
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400335 err = nfs4_register_sysctl();
336 if (err)
337 goto out1;
338
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400339 register_nfs_version(&nfs_v4);
Bryan Schumaker129d1972012-07-16 16:39:13 -0400340 return 0;
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400341out1:
342 nfs_idmap_quit();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400343out:
344 return err;
345}
346
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400347static void __exit exit_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400348{
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400349 unregister_nfs_version(&nfs_v4);
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400350 nfs4_unregister_sysctl();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400351 nfs_idmap_quit();
352}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400353
354MODULE_LICENSE("GPL");
355
356module_init(init_nfs_v4);
357module_exit(exit_nfs_v4);