Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nfs4super.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Bryan Schumaker <[email protected]>
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 "delegation.h"
10 #include "internal.h"
11 #include "nfs4_fs.h"
12 #include "pnfs.h"
13 #include "nfs.h"
14 
15 #define NFSDBG_FACILITY NFSDBG_VFS
16 
17 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
18 static void nfs4_evict_inode(struct inode *inode);
19 static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type,
20  int flags, const char *dev_name, void *raw_data);
21 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
22  int flags, const char *dev_name, void *raw_data);
23 static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
24  int flags, const char *dev_name, void *raw_data);
25 
26 static 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,
32 };
33 
34 static 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,
40 };
41 
43  .owner = THIS_MODULE,
44  .name = "nfs4",
45  .mount = nfs4_referral_mount,
46  .kill_sb = nfs_kill_super,
48 };
49 
50 static 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 
66  .owner = THIS_MODULE,
67  .nfs_fs = &nfs4_fs_type,
68  .rpc_vers = &nfs_version4,
69  .rpc_ops = &nfs_v4_clientops,
70  .sops = &nfs4_sops,
71  .xattr = nfs4_xattr_handlers,
72 };
73 
74 static 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  */
97 static 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! */
105  /* First call standard NFS clear_inode() code */
106  nfs_clear_inode(inode);
107 }
108 
109 /*
110  * Get the superblock for the NFS4 root partition
111  */
112 static struct dentry *
113 nfs4_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 
120  mount_info->set_security = nfs_set_sb_security;
121 
122  /* Get a volume representation */
123  server = nfs4_create_server(mount_info, &nfs_v4);
124  if (IS_ERR(server)) {
125  mntroot = ERR_CAST(server);
126  goto out;
127  }
128 
129  mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
130 
131 out:
132  return mntroot;
133 }
134 
135 static 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 
157  struct list_head list;
158  const struct task_struct *task;
159  unsigned int referral_count;
160 };
161 
162 static LIST_HEAD(nfs_referral_count_list);
163 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
164 
165 static 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 
178 static 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) {
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);
203 out:
204  return ret;
205 }
206 
207 static 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 
222 static 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 
243 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
244  struct nfs_mount_info *mount_info,
245  struct nfs_subversion *nfs_mod)
246 {
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 
254  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 
268 static struct dentry *
269 nfs4_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 = {
274  .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 
293  mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
294 out:
295  nfs_free_fhandle(mount_info.mntfh);
296  return mntroot;
297 }
298 
299 /*
300  * Create an NFS4 server record on referral traversal
301  */
302 static 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 
327 static int __init init_nfs_v4(void)
328 {
329  int err;
330 
331  err = nfs_idmap_init();
332  if (err)
333  goto out;
334 
335  err = nfs4_register_sysctl();
336  if (err)
337  goto out1;
338 
339  register_nfs_version(&nfs_v4);
340  return 0;
341 out1:
342  nfs_idmap_quit();
343 out:
344  return err;
345 }
346 
347 static void __exit exit_nfs_v4(void)
348 {
349  unregister_nfs_version(&nfs_v4);
351  nfs_idmap_quit();
352 }
353 
354 MODULE_LICENSE("GPL");
355 
356 module_init(init_nfs_v4);
357 module_exit(exit_nfs_v4);