blob: 1c825f3bef51c461a6decd9793682beab540b417 [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
26static struct file_system_type nfs4_fs_type = {
27 .owner = THIS_MODULE,
28 .name = "nfs4",
29 .mount = nfs_fs_mount,
30 .kill_sb = nfs_kill_super,
31 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
32};
33
34static struct file_system_type nfs4_remote_fs_type = {
35 .owner = THIS_MODULE,
36 .name = "nfs4",
37 .mount = nfs4_remote_mount,
38 .kill_sb = nfs_kill_super,
39 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
40};
41
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040042static struct file_system_type nfs4_remote_referral_fs_type = {
43 .owner = THIS_MODULE,
44 .name = "nfs4",
45 .mount = nfs4_remote_referral_mount,
46 .kill_sb = nfs_kill_super,
47 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
48};
49
50struct file_system_type nfs4_referral_fs_type = {
51 .owner = THIS_MODULE,
52 .name = "nfs4",
53 .mount = nfs4_referral_mount,
54 .kill_sb = nfs_kill_super,
55 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
56};
57
58static const struct super_operations nfs4_sops = {
59 .alloc_inode = nfs_alloc_inode,
60 .destroy_inode = nfs_destroy_inode,
61 .write_inode = nfs4_write_inode,
62 .put_super = nfs_put_super,
63 .statfs = nfs_statfs,
64 .evict_inode = nfs4_evict_inode,
65 .umount_begin = nfs_umount_begin,
66 .show_options = nfs_show_options,
67 .show_devname = nfs_show_devname,
68 .show_path = nfs_show_path,
69 .show_stats = nfs_show_stats,
70 .remount_fs = nfs_remount,
71};
72
Bryan Schumakerab7017a2012-07-30 16:05:16 -040073struct nfs_subversion nfs_v4 = {
74 .owner = THIS_MODULE,
75 .nfs_fs = &nfs4_fs_type,
76 .rpc_vers = &nfs_version4,
77 .rpc_ops = &nfs_v4_clientops,
Bryan Schumaker6a744902012-07-30 16:05:20 -040078 .sops = &nfs4_sops,
79 .xattr = nfs4_xattr_handlers,
Bryan Schumakerab7017a2012-07-30 16:05:16 -040080};
81
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040082static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
83{
84 int ret = nfs_write_inode(inode, wbc);
85
86 if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
87 int status;
88 bool sync = true;
89
90 if (wbc->sync_mode == WB_SYNC_NONE)
91 sync = false;
92
93 status = pnfs_layoutcommit_inode(inode, sync);
94 if (status < 0)
95 return status;
96 }
97 return ret;
98}
99
100/*
101 * Clean out any remaining NFSv4 state that might be left over due
102 * to open() calls that passed nfs_atomic_lookup, but failed to call
103 * nfs_open().
104 */
105static void nfs4_evict_inode(struct inode *inode)
106{
107 truncate_inode_pages(&inode->i_data, 0);
108 clear_inode(inode);
109 pnfs_return_layout(inode);
110 pnfs_destroy_layout(NFS_I(inode));
111 /* If we are holding a delegation, return it! */
112 nfs_inode_return_delegation_noreclaim(inode);
113 /* First call standard NFS clear_inode() code */
114 nfs_clear_inode(inode);
115}
116
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400117/*
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400118 * Get the superblock for the NFS4 root partition
119 */
120static struct dentry *
121nfs4_remote_mount(struct file_system_type *fs_type, int flags,
122 const char *dev_name, void *info)
123{
124 struct nfs_mount_info *mount_info = info;
125 struct nfs_server *server;
126 struct dentry *mntroot = ERR_PTR(-ENOMEM);
127
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400128 mount_info->set_security = nfs_set_sb_security;
129
130 /* Get a volume representation */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400131 server = nfs4_create_server(mount_info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400132 if (IS_ERR(server)) {
133 mntroot = ERR_CAST(server);
134 goto out;
135 }
136
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400137 mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400138
139out:
140 return mntroot;
141}
142
143static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
144 int flags, void *data, const char *hostname)
145{
146 struct vfsmount *root_mnt;
147 char *root_devname;
148 size_t len;
149
150 len = strlen(hostname) + 5;
151 root_devname = kmalloc(len, GFP_KERNEL);
152 if (root_devname == NULL)
153 return ERR_PTR(-ENOMEM);
154 /* Does hostname needs to be enclosed in brackets? */
155 if (strchr(hostname, ':'))
156 snprintf(root_devname, len, "[%s]:/", hostname);
157 else
158 snprintf(root_devname, len, "%s:/", hostname);
159 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
160 kfree(root_devname);
161 return root_mnt;
162}
163
164struct nfs_referral_count {
165 struct list_head list;
166 const struct task_struct *task;
167 unsigned int referral_count;
168};
169
170static LIST_HEAD(nfs_referral_count_list);
171static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
172
173static struct nfs_referral_count *nfs_find_referral_count(void)
174{
175 struct nfs_referral_count *p;
176
177 list_for_each_entry(p, &nfs_referral_count_list, list) {
178 if (p->task == current)
179 return p;
180 }
181 return NULL;
182}
183
184#define NFS_MAX_NESTED_REFERRALS 2
185
186static int nfs_referral_loop_protect(void)
187{
188 struct nfs_referral_count *p, *new;
189 int ret = -ENOMEM;
190
191 new = kmalloc(sizeof(*new), GFP_KERNEL);
192 if (!new)
193 goto out;
194 new->task = current;
195 new->referral_count = 1;
196
197 ret = 0;
198 spin_lock(&nfs_referral_count_list_lock);
199 p = nfs_find_referral_count();
200 if (p != NULL) {
201 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
202 ret = -ELOOP;
203 else
204 p->referral_count++;
205 } else {
206 list_add(&new->list, &nfs_referral_count_list);
207 new = NULL;
208 }
209 spin_unlock(&nfs_referral_count_list_lock);
210 kfree(new);
211out:
212 return ret;
213}
214
215static void nfs_referral_loop_unprotect(void)
216{
217 struct nfs_referral_count *p;
218
219 spin_lock(&nfs_referral_count_list_lock);
220 p = nfs_find_referral_count();
221 p->referral_count--;
222 if (p->referral_count == 0)
223 list_del(&p->list);
224 else
225 p = NULL;
226 spin_unlock(&nfs_referral_count_list_lock);
227 kfree(p);
228}
229
230static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
231 const char *export_path)
232{
233 struct dentry *dentry;
234 int err;
235
236 if (IS_ERR(root_mnt))
237 return ERR_CAST(root_mnt);
238
239 err = nfs_referral_loop_protect();
240 if (err) {
241 mntput(root_mnt);
242 return ERR_PTR(err);
243 }
244
245 dentry = mount_subtree(root_mnt, export_path);
246 nfs_referral_loop_unprotect();
247
248 return dentry;
249}
250
251struct dentry *nfs4_try_mount(int flags, const char *dev_name,
Bryan Schumakerff9099f22012-07-30 16:05:18 -0400252 struct nfs_mount_info *mount_info,
253 struct nfs_subversion *nfs_mod)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400254{
255 char *export_path;
256 struct vfsmount *root_mnt;
257 struct dentry *res;
258 struct nfs_parsed_mount_data *data = mount_info->parsed;
259
260 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
261
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400262 export_path = data->nfs_server.export_path;
263 data->nfs_server.export_path = "/";
264 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
265 data->nfs_server.hostname);
266 data->nfs_server.export_path = export_path;
267
268 res = nfs_follow_remote_path(root_mnt, export_path);
269
270 dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
271 IS_ERR(res) ? PTR_ERR(res) : 0,
272 IS_ERR(res) ? " [error]" : "");
273 return res;
274}
275
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400276static struct dentry *
277nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
278 const char *dev_name, void *raw_data)
279{
280 struct nfs_mount_info mount_info = {
Bryan Schumaker6a744902012-07-30 16:05:20 -0400281 .fill_super = nfs_fill_super,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400282 .set_security = nfs_clone_sb_security,
283 .cloned = raw_data,
284 };
285 struct nfs_server *server;
286 struct dentry *mntroot = ERR_PTR(-ENOMEM);
287
288 dprintk("--> nfs4_referral_get_sb()\n");
289
290 mount_info.mntfh = nfs_alloc_fhandle();
291 if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
292 goto out;
293
294 /* create a new volume representation */
295 server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
296 if (IS_ERR(server)) {
297 mntroot = ERR_CAST(server);
298 goto out;
299 }
300
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400301 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400302out:
303 nfs_free_fhandle(mount_info.mntfh);
304 return mntroot;
305}
306
307/*
308 * Create an NFS4 server record on referral traversal
309 */
310static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
311 int flags, const char *dev_name, void *raw_data)
312{
313 struct nfs_clone_mount *data = raw_data;
314 char *export_path;
315 struct vfsmount *root_mnt;
316 struct dentry *res;
317
318 dprintk("--> nfs4_referral_mount()\n");
319
320 export_path = data->mnt_path;
321 data->mnt_path = "/";
322
323 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
324 flags, data, data->hostname);
325 data->mnt_path = export_path;
326
327 res = nfs_follow_remote_path(root_mnt, export_path);
328 dprintk("<-- nfs4_referral_mount() = %ld%s\n",
329 IS_ERR(res) ? PTR_ERR(res) : 0,
330 IS_ERR(res) ? " [error]" : "");
331 return res;
332}
333
334
Bryan Schumaker129d1972012-07-16 16:39:13 -0400335int __init init_nfs_v4(void)
336{
337 int err;
338
339 err = nfs_idmap_init();
340 if (err)
341 goto out;
342
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400343 err = nfs4_register_sysctl();
344 if (err)
345 goto out1;
346
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400347 err = register_filesystem(&nfs4_fs_type);
348 if (err < 0)
349 goto out2;
350
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400351 register_nfs_version(&nfs_v4);
Bryan Schumaker129d1972012-07-16 16:39:13 -0400352 return 0;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400353out2:
354 nfs4_unregister_sysctl();
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400355out1:
356 nfs_idmap_quit();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400357out:
358 return err;
359}
360
Bryan Schumakerbb6e0712012-07-17 15:18:30 -0400361void exit_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400362{
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400363 unregister_nfs_version(&nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400364 unregister_filesystem(&nfs4_fs_type);
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400365 nfs4_unregister_sysctl();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400366 nfs_idmap_quit();
367}