nfs4super.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
  3. */
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/nfs4_mount.h>
  7. #include <linux/nfs_fs.h>
  8. #include "delegation.h"
  9. #include "internal.h"
  10. #include "nfs4_fs.h"
  11. #include "nfs4idmap.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. /* If we are holding a delegation, return it! */
  85. nfs_inode_return_delegation_noreclaim(inode);
  86. /* Note that above delegreturn would trigger pnfs return-on-close */
  87. pnfs_return_layout(inode);
  88. pnfs_destroy_layout(NFS_I(inode));
  89. /* First call standard NFS clear_inode() code */
  90. nfs_clear_inode(inode);
  91. }
  92. /*
  93. * Get the superblock for the NFS4 root partition
  94. */
  95. static struct dentry *
  96. nfs4_remote_mount(struct file_system_type *fs_type, int flags,
  97. const char *dev_name, void *info)
  98. {
  99. struct nfs_mount_info *mount_info = info;
  100. struct nfs_server *server;
  101. struct dentry *mntroot = ERR_PTR(-ENOMEM);
  102. mount_info->set_security = nfs_set_sb_security;
  103. /* Get a volume representation */
  104. server = nfs4_create_server(mount_info, &nfs_v4);
  105. if (IS_ERR(server)) {
  106. mntroot = ERR_CAST(server);
  107. goto out;
  108. }
  109. mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
  110. out:
  111. return mntroot;
  112. }
  113. static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
  114. int flags, void *data, const char *hostname)
  115. {
  116. struct vfsmount *root_mnt;
  117. char *root_devname;
  118. size_t len;
  119. len = strlen(hostname) + 5;
  120. root_devname = kmalloc(len, GFP_KERNEL);
  121. if (root_devname == NULL)
  122. return ERR_PTR(-ENOMEM);
  123. /* Does hostname needs to be enclosed in brackets? */
  124. if (strchr(hostname, ':'))
  125. snprintf(root_devname, len, "[%s]:/", hostname);
  126. else
  127. snprintf(root_devname, len, "%s:/", hostname);
  128. root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
  129. kfree(root_devname);
  130. return root_mnt;
  131. }
  132. struct nfs_referral_count {
  133. struct list_head list;
  134. const struct task_struct *task;
  135. unsigned int referral_count;
  136. };
  137. static LIST_HEAD(nfs_referral_count_list);
  138. static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
  139. static struct nfs_referral_count *nfs_find_referral_count(void)
  140. {
  141. struct nfs_referral_count *p;
  142. list_for_each_entry(p, &nfs_referral_count_list, list) {
  143. if (p->task == current)
  144. return p;
  145. }
  146. return NULL;
  147. }
  148. #define NFS_MAX_NESTED_REFERRALS 2
  149. static int nfs_referral_loop_protect(void)
  150. {
  151. struct nfs_referral_count *p, *new;
  152. int ret = -ENOMEM;
  153. new = kmalloc(sizeof(*new), GFP_KERNEL);
  154. if (!new)
  155. goto out;
  156. new->task = current;
  157. new->referral_count = 1;
  158. ret = 0;
  159. spin_lock(&nfs_referral_count_list_lock);
  160. p = nfs_find_referral_count();
  161. if (p != NULL) {
  162. if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
  163. ret = -ELOOP;
  164. else
  165. p->referral_count++;
  166. } else {
  167. list_add(&new->list, &nfs_referral_count_list);
  168. new = NULL;
  169. }
  170. spin_unlock(&nfs_referral_count_list_lock);
  171. kfree(new);
  172. out:
  173. return ret;
  174. }
  175. static void nfs_referral_loop_unprotect(void)
  176. {
  177. struct nfs_referral_count *p;
  178. spin_lock(&nfs_referral_count_list_lock);
  179. p = nfs_find_referral_count();
  180. p->referral_count--;
  181. if (p->referral_count == 0)
  182. list_del(&p->list);
  183. else
  184. p = NULL;
  185. spin_unlock(&nfs_referral_count_list_lock);
  186. kfree(p);
  187. }
  188. static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
  189. const char *export_path)
  190. {
  191. struct dentry *dentry;
  192. int err;
  193. if (IS_ERR(root_mnt))
  194. return ERR_CAST(root_mnt);
  195. err = nfs_referral_loop_protect();
  196. if (err) {
  197. mntput(root_mnt);
  198. return ERR_PTR(err);
  199. }
  200. dentry = mount_subtree(root_mnt, export_path);
  201. nfs_referral_loop_unprotect();
  202. return dentry;
  203. }
  204. struct dentry *nfs4_try_mount(int flags, const char *dev_name,
  205. struct nfs_mount_info *mount_info,
  206. struct nfs_subversion *nfs_mod)
  207. {
  208. char *export_path;
  209. struct vfsmount *root_mnt;
  210. struct dentry *res;
  211. struct nfs_parsed_mount_data *data = mount_info->parsed;
  212. dfprintk(MOUNT, "--> nfs4_try_mount()\n");
  213. export_path = data->nfs_server.export_path;
  214. data->nfs_server.export_path = "/";
  215. root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
  216. data->nfs_server.hostname);
  217. data->nfs_server.export_path = export_path;
  218. res = nfs_follow_remote_path(root_mnt, export_path);
  219. dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
  220. PTR_ERR_OR_ZERO(res),
  221. IS_ERR(res) ? " [error]" : "");
  222. return res;
  223. }
  224. static struct dentry *
  225. nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
  226. const char *dev_name, void *raw_data)
  227. {
  228. struct nfs_mount_info mount_info = {
  229. .fill_super = nfs_fill_super,
  230. .set_security = nfs_clone_sb_security,
  231. .cloned = raw_data,
  232. };
  233. struct nfs_server *server;
  234. struct dentry *mntroot = ERR_PTR(-ENOMEM);
  235. dprintk("--> nfs4_referral_get_sb()\n");
  236. mount_info.mntfh = nfs_alloc_fhandle();
  237. if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
  238. goto out;
  239. /* create a new volume representation */
  240. server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
  241. if (IS_ERR(server)) {
  242. mntroot = ERR_CAST(server);
  243. goto out;
  244. }
  245. mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
  246. out:
  247. nfs_free_fhandle(mount_info.mntfh);
  248. return mntroot;
  249. }
  250. /*
  251. * Create an NFS4 server record on referral traversal
  252. */
  253. static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
  254. int flags, const char *dev_name, void *raw_data)
  255. {
  256. struct nfs_clone_mount *data = raw_data;
  257. char *export_path;
  258. struct vfsmount *root_mnt;
  259. struct dentry *res;
  260. dprintk("--> nfs4_referral_mount()\n");
  261. export_path = data->mnt_path;
  262. data->mnt_path = "/";
  263. root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
  264. flags, data, data->hostname);
  265. data->mnt_path = export_path;
  266. res = nfs_follow_remote_path(root_mnt, export_path);
  267. dprintk("<-- nfs4_referral_mount() = %d%s\n",
  268. PTR_ERR_OR_ZERO(res),
  269. IS_ERR(res) ? " [error]" : "");
  270. return res;
  271. }
  272. static int __init init_nfs_v4(void)
  273. {
  274. int err;
  275. err = nfs_dns_resolver_init();
  276. if (err)
  277. goto out;
  278. err = nfs_idmap_init();
  279. if (err)
  280. goto out1;
  281. err = nfs4_register_sysctl();
  282. if (err)
  283. goto out2;
  284. register_nfs_version(&nfs_v4);
  285. return 0;
  286. out2:
  287. nfs_idmap_quit();
  288. out1:
  289. nfs_dns_resolver_destroy();
  290. out:
  291. return err;
  292. }
  293. static void __exit exit_nfs_v4(void)
  294. {
  295. /* Not called in the _init(), conditionally loaded */
  296. nfs4_pnfs_v3_ds_connect_unload();
  297. unregister_nfs_version(&nfs_v4);
  298. nfs4_unregister_sysctl();
  299. nfs_idmap_quit();
  300. nfs_dns_resolver_destroy();
  301. }
  302. MODULE_LICENSE("GPL");
  303. module_init(init_nfs_v4);
  304. module_exit(exit_nfs_v4);