nfs4super.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "delegation.h"
  10. #include "internal.h"
  11. #include "nfs4_fs.h"
  12. #include "pnfs.h"
  13. #include "nfs.h"
  14. #define NFSDBG_FACILITY NFSDBG_VFS
  15. static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
  16. static void nfs4_evict_inode(struct inode *inode);
  17. static struct dentry *nfs4_remote_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. static struct file_system_type nfs4_remote_fs_type = {
  24. .owner = THIS_MODULE,
  25. .name = "nfs4",
  26. .mount = nfs4_remote_mount,
  27. .kill_sb = nfs_kill_super,
  28. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  29. };
  30. static struct file_system_type nfs4_remote_referral_fs_type = {
  31. .owner = THIS_MODULE,
  32. .name = "nfs4",
  33. .mount = nfs4_remote_referral_mount,
  34. .kill_sb = nfs_kill_super,
  35. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  36. };
  37. struct file_system_type nfs4_referral_fs_type = {
  38. .owner = THIS_MODULE,
  39. .name = "nfs4",
  40. .mount = nfs4_referral_mount,
  41. .kill_sb = nfs_kill_super,
  42. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  43. };
  44. static const struct super_operations nfs4_sops = {
  45. .alloc_inode = nfs_alloc_inode,
  46. .destroy_inode = nfs_destroy_inode,
  47. .write_inode = nfs4_write_inode,
  48. .put_super = nfs_put_super,
  49. .statfs = nfs_statfs,
  50. .evict_inode = nfs4_evict_inode,
  51. .umount_begin = nfs_umount_begin,
  52. .show_options = nfs_show_options,
  53. .show_devname = nfs_show_devname,
  54. .show_path = nfs_show_path,
  55. .show_stats = nfs_show_stats,
  56. .remount_fs = nfs_remount,
  57. };
  58. struct nfs_subversion nfs_v4 = {
  59. .owner = THIS_MODULE,
  60. .nfs_fs = &nfs4_fs_type,
  61. .rpc_vers = &nfs_version4,
  62. .rpc_ops = &nfs_v4_clientops,
  63. .sops = &nfs4_sops,
  64. .xattr = nfs4_xattr_handlers,
  65. };
  66. static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
  67. {
  68. int ret = nfs_write_inode(inode, wbc);
  69. if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
  70. int status;
  71. bool sync = true;
  72. if (wbc->sync_mode == WB_SYNC_NONE)
  73. sync = false;
  74. status = pnfs_layoutcommit_inode(inode, sync);
  75. if (status < 0)
  76. return status;
  77. }
  78. return ret;
  79. }
  80. /*
  81. * Clean out any remaining NFSv4 state that might be left over due
  82. * to open() calls that passed nfs_atomic_lookup, but failed to call
  83. * nfs_open().
  84. */
  85. static void nfs4_evict_inode(struct inode *inode)
  86. {
  87. truncate_inode_pages(&inode->i_data, 0);
  88. clear_inode(inode);
  89. pnfs_return_layout(inode);
  90. pnfs_destroy_layout(NFS_I(inode));
  91. /* If we are holding a delegation, return it! */
  92. nfs_inode_return_delegation_noreclaim(inode);
  93. /* First call standard NFS clear_inode() code */
  94. nfs_clear_inode(inode);
  95. }
  96. /*
  97. * Get the superblock for the NFS4 root partition
  98. */
  99. static struct dentry *
  100. nfs4_remote_mount(struct file_system_type *fs_type, int flags,
  101. const char *dev_name, void *info)
  102. {
  103. struct nfs_mount_info *mount_info = info;
  104. struct nfs_server *server;
  105. struct dentry *mntroot = ERR_PTR(-ENOMEM);
  106. mount_info->set_security = nfs_set_sb_security;
  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. mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
  114. out:
  115. return mntroot;
  116. }
  117. static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
  118. int flags, void *data, const char *hostname)
  119. {
  120. struct vfsmount *root_mnt;
  121. char *root_devname;
  122. size_t len;
  123. len = strlen(hostname) + 5;
  124. root_devname = kmalloc(len, GFP_KERNEL);
  125. if (root_devname == NULL)
  126. return ERR_PTR(-ENOMEM);
  127. /* Does hostname needs to be enclosed in brackets? */
  128. if (strchr(hostname, ':'))
  129. snprintf(root_devname, len, "[%s]:/", hostname);
  130. else
  131. snprintf(root_devname, len, "%s:/", hostname);
  132. root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
  133. kfree(root_devname);
  134. return root_mnt;
  135. }
  136. struct nfs_referral_count {
  137. struct list_head list;
  138. const struct task_struct *task;
  139. unsigned int referral_count;
  140. };
  141. static LIST_HEAD(nfs_referral_count_list);
  142. static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
  143. static struct nfs_referral_count *nfs_find_referral_count(void)
  144. {
  145. struct nfs_referral_count *p;
  146. list_for_each_entry(p, &nfs_referral_count_list, list) {
  147. if (p->task == current)
  148. return p;
  149. }
  150. return NULL;
  151. }
  152. #define NFS_MAX_NESTED_REFERRALS 2
  153. static int nfs_referral_loop_protect(void)
  154. {
  155. struct nfs_referral_count *p, *new;
  156. int ret = -ENOMEM;
  157. new = kmalloc(sizeof(*new), GFP_KERNEL);
  158. if (!new)
  159. goto out;
  160. new->task = current;
  161. new->referral_count = 1;
  162. ret = 0;
  163. spin_lock(&nfs_referral_count_list_lock);
  164. p = nfs_find_referral_count();
  165. if (p != NULL) {
  166. if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
  167. ret = -ELOOP;
  168. else
  169. p->referral_count++;
  170. } else {
  171. list_add(&new->list, &nfs_referral_count_list);
  172. new = NULL;
  173. }
  174. spin_unlock(&nfs_referral_count_list_lock);
  175. kfree(new);
  176. out:
  177. return ret;
  178. }
  179. static void nfs_referral_loop_unprotect(void)
  180. {
  181. struct nfs_referral_count *p;
  182. spin_lock(&nfs_referral_count_list_lock);
  183. p = nfs_find_referral_count();
  184. p->referral_count--;
  185. if (p->referral_count == 0)
  186. list_del(&p->list);
  187. else
  188. p = NULL;
  189. spin_unlock(&nfs_referral_count_list_lock);
  190. kfree(p);
  191. }
  192. static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
  193. const char *export_path)
  194. {
  195. struct dentry *dentry;
  196. int err;
  197. if (IS_ERR(root_mnt))
  198. return ERR_CAST(root_mnt);
  199. err = nfs_referral_loop_protect();
  200. if (err) {
  201. mntput(root_mnt);
  202. return ERR_PTR(err);
  203. }
  204. dentry = mount_subtree(root_mnt, export_path);
  205. nfs_referral_loop_unprotect();
  206. return dentry;
  207. }
  208. struct dentry *nfs4_try_mount(int flags, const char *dev_name,
  209. struct nfs_mount_info *mount_info,
  210. struct nfs_subversion *nfs_mod)
  211. {
  212. char *export_path;
  213. struct vfsmount *root_mnt;
  214. struct dentry *res;
  215. struct nfs_parsed_mount_data *data = mount_info->parsed;
  216. dfprintk(MOUNT, "--> nfs4_try_mount()\n");
  217. export_path = data->nfs_server.export_path;
  218. data->nfs_server.export_path = "/";
  219. root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
  220. data->nfs_server.hostname);
  221. data->nfs_server.export_path = export_path;
  222. res = nfs_follow_remote_path(root_mnt, export_path);
  223. dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n",
  224. IS_ERR(res) ? PTR_ERR(res) : 0,
  225. IS_ERR(res) ? " [error]" : "");
  226. return res;
  227. }
  228. static struct dentry *
  229. nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
  230. const char *dev_name, void *raw_data)
  231. {
  232. struct nfs_mount_info mount_info = {
  233. .fill_super = nfs_fill_super,
  234. .set_security = nfs_clone_sb_security,
  235. .cloned = raw_data,
  236. };
  237. struct nfs_server *server;
  238. struct dentry *mntroot = ERR_PTR(-ENOMEM);
  239. dprintk("--> nfs4_referral_get_sb()\n");
  240. mount_info.mntfh = nfs_alloc_fhandle();
  241. if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
  242. goto out;
  243. /* create a new volume representation */
  244. server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
  245. if (IS_ERR(server)) {
  246. mntroot = ERR_CAST(server);
  247. goto out;
  248. }
  249. mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
  250. out:
  251. nfs_free_fhandle(mount_info.mntfh);
  252. return mntroot;
  253. }
  254. /*
  255. * Create an NFS4 server record on referral traversal
  256. */
  257. static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
  258. int flags, const char *dev_name, void *raw_data)
  259. {
  260. struct nfs_clone_mount *data = raw_data;
  261. char *export_path;
  262. struct vfsmount *root_mnt;
  263. struct dentry *res;
  264. dprintk("--> nfs4_referral_mount()\n");
  265. export_path = data->mnt_path;
  266. data->mnt_path = "/";
  267. root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
  268. flags, data, data->hostname);
  269. data->mnt_path = export_path;
  270. res = nfs_follow_remote_path(root_mnt, export_path);
  271. dprintk("<-- nfs4_referral_mount() = %ld%s\n",
  272. IS_ERR(res) ? PTR_ERR(res) : 0,
  273. IS_ERR(res) ? " [error]" : "");
  274. return res;
  275. }
  276. static int __init init_nfs_v4(void)
  277. {
  278. int err;
  279. err = nfs_idmap_init();
  280. if (err)
  281. goto out;
  282. err = nfs4_register_sysctl();
  283. if (err)
  284. goto out1;
  285. register_nfs_version(&nfs_v4);
  286. return 0;
  287. out1:
  288. nfs_idmap_quit();
  289. out:
  290. return err;
  291. }
  292. static void __exit exit_nfs_v4(void)
  293. {
  294. unregister_nfs_version(&nfs_v4);
  295. nfs4_unregister_sysctl();
  296. nfs_idmap_quit();
  297. }
  298. MODULE_LICENSE("GPL");
  299. module_init(init_nfs_v4);
  300. module_exit(exit_nfs_v4);