nfs4super.c 8.6 KB

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