nfs4super.c 8.9 KB

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