nfs4super.c 8.6 KB

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