nfs4super.c 8.8 KB

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