nfs4super.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/mount.h>
  8. #include <linux/nfs4_mount.h>
  9. #include <linux/nfs_fs.h>
  10. #include <linux/nfs_ssc.h>
  11. #include "delegation.h"
  12. #include "internal.h"
  13. #include "nfs4_fs.h"
  14. #include "nfs4idmap.h"
  15. #include "dns_resolve.h"
  16. #include "pnfs.h"
  17. #include "nfs.h"
  18. #define NFSDBG_FACILITY NFSDBG_VFS
  19. static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
  20. static void nfs4_evict_inode(struct inode *inode);
  21. static const struct super_operations nfs4_sops = {
  22. .alloc_inode = nfs_alloc_inode,
  23. .free_inode = nfs_free_inode,
  24. .write_inode = nfs4_write_inode,
  25. .drop_inode = nfs_drop_inode,
  26. .statfs = nfs_statfs,
  27. .evict_inode = nfs4_evict_inode,
  28. .umount_begin = nfs_umount_begin,
  29. .show_options = nfs_show_options,
  30. .show_devname = nfs_show_devname,
  31. .show_path = nfs_show_path,
  32. .show_stats = nfs_show_stats,
  33. };
  34. struct nfs_subversion nfs_v4 = {
  35. .owner = THIS_MODULE,
  36. .nfs_fs = &nfs4_fs_type,
  37. .rpc_vers = &nfs_version4,
  38. .rpc_ops = &nfs_v4_clientops,
  39. .sops = &nfs4_sops,
  40. .xattr = nfs4_xattr_handlers,
  41. };
  42. static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
  43. {
  44. int ret = nfs_write_inode(inode, wbc);
  45. if (ret == 0)
  46. ret = pnfs_layoutcommit_inode(inode,
  47. wbc->sync_mode == WB_SYNC_ALL);
  48. return ret;
  49. }
  50. /*
  51. * Clean out any remaining NFSv4 state that might be left over due
  52. * to open() calls that passed nfs_atomic_lookup, but failed to call
  53. * nfs_open().
  54. */
  55. static void nfs4_evict_inode(struct inode *inode)
  56. {
  57. truncate_inode_pages_final(&inode->i_data);
  58. clear_inode(inode);
  59. /* If we are holding a delegation, return and free it */
  60. nfs_inode_evict_delegation(inode);
  61. /* Note that above delegreturn would trigger pnfs return-on-close */
  62. pnfs_return_layout(inode);
  63. pnfs_destroy_layout_final(NFS_I(inode));
  64. /* First call standard NFS clear_inode() code */
  65. nfs_clear_inode(inode);
  66. nfs4_xattr_cache_zap(inode);
  67. }
  68. struct nfs_referral_count {
  69. struct list_head list;
  70. const struct task_struct *task;
  71. unsigned int referral_count;
  72. };
  73. static LIST_HEAD(nfs_referral_count_list);
  74. static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
  75. static struct nfs_referral_count *nfs_find_referral_count(void)
  76. {
  77. struct nfs_referral_count *p;
  78. list_for_each_entry(p, &nfs_referral_count_list, list) {
  79. if (p->task == current)
  80. return p;
  81. }
  82. return NULL;
  83. }
  84. #define NFS_MAX_NESTED_REFERRALS 2
  85. static int nfs_referral_loop_protect(void)
  86. {
  87. struct nfs_referral_count *p, *new;
  88. int ret = -ENOMEM;
  89. new = kmalloc_obj(*new);
  90. if (!new)
  91. goto out;
  92. new->task = current;
  93. new->referral_count = 1;
  94. ret = 0;
  95. spin_lock(&nfs_referral_count_list_lock);
  96. p = nfs_find_referral_count();
  97. if (p != NULL) {
  98. if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
  99. ret = -ELOOP;
  100. else
  101. p->referral_count++;
  102. } else {
  103. list_add(&new->list, &nfs_referral_count_list);
  104. new = NULL;
  105. }
  106. spin_unlock(&nfs_referral_count_list_lock);
  107. kfree(new);
  108. out:
  109. return ret;
  110. }
  111. static void nfs_referral_loop_unprotect(void)
  112. {
  113. struct nfs_referral_count *p;
  114. spin_lock(&nfs_referral_count_list_lock);
  115. p = nfs_find_referral_count();
  116. p->referral_count--;
  117. if (p->referral_count == 0)
  118. list_del(&p->list);
  119. else
  120. p = NULL;
  121. spin_unlock(&nfs_referral_count_list_lock);
  122. kfree(p);
  123. }
  124. static int do_nfs4_mount(struct nfs_server *server,
  125. struct fs_context *fc,
  126. const char *hostname,
  127. const char *export_path)
  128. {
  129. struct nfs_fs_context *root_ctx;
  130. struct nfs_fs_context *ctx;
  131. struct fs_context *root_fc;
  132. struct vfsmount *root_mnt;
  133. struct dentry *dentry;
  134. char *source;
  135. int ret;
  136. if (IS_ERR(server))
  137. return PTR_ERR(server);
  138. root_fc = vfs_dup_fs_context(fc);
  139. if (IS_ERR(root_fc)) {
  140. nfs_free_server(server);
  141. return PTR_ERR(root_fc);
  142. }
  143. kfree(root_fc->source);
  144. root_fc->source = NULL;
  145. ctx = nfs_fc2context(fc);
  146. root_ctx = nfs_fc2context(root_fc);
  147. root_ctx->internal = true;
  148. root_ctx->server = server;
  149. if (ctx->fscache_uniq) {
  150. ret = vfs_parse_fs_string(root_fc, "fsc", ctx->fscache_uniq);
  151. if (ret < 0) {
  152. put_fs_context(root_fc);
  153. return ret;
  154. }
  155. }
  156. /* We leave export_path unset as it's not used to find the root. */
  157. /* Does hostname needs to be enclosed in brackets? */
  158. if (strchr(hostname, ':'))
  159. source = kasprintf(GFP_KERNEL, "[%s]:/", hostname);
  160. else
  161. source = kasprintf(GFP_KERNEL, "%s:/", hostname);
  162. if (!source) {
  163. put_fs_context(root_fc);
  164. return -ENOMEM;
  165. }
  166. ret = vfs_parse_fs_string(root_fc, "source", source);
  167. kfree(source);
  168. if (ret < 0) {
  169. put_fs_context(root_fc);
  170. return ret;
  171. }
  172. root_mnt = fc_mount(root_fc);
  173. put_fs_context(root_fc);
  174. if (IS_ERR(root_mnt))
  175. return PTR_ERR(root_mnt);
  176. ret = nfs_referral_loop_protect();
  177. if (ret) {
  178. mntput(root_mnt);
  179. return ret;
  180. }
  181. dentry = mount_subtree(root_mnt, export_path);
  182. nfs_referral_loop_unprotect();
  183. if (IS_ERR(dentry))
  184. return PTR_ERR(dentry);
  185. fc->root = dentry;
  186. return 0;
  187. }
  188. int nfs4_try_get_tree(struct fs_context *fc)
  189. {
  190. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  191. int err;
  192. dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
  193. /* We create a mount for the server's root, walk to the requested
  194. * location and then create another mount for that.
  195. */
  196. err= do_nfs4_mount(nfs4_create_server(fc),
  197. fc, ctx->nfs_server.hostname,
  198. ctx->nfs_server.export_path);
  199. if (err) {
  200. nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
  201. dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
  202. } else {
  203. dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
  204. }
  205. return err;
  206. }
  207. /*
  208. * Create an NFS4 server record on referral traversal
  209. */
  210. int nfs4_get_referral_tree(struct fs_context *fc)
  211. {
  212. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  213. int err;
  214. dprintk("--> nfs4_referral_mount()\n");
  215. /* create a new volume representation */
  216. err = do_nfs4_mount(nfs4_create_referral_server(fc),
  217. fc, ctx->nfs_server.hostname,
  218. ctx->nfs_server.export_path);
  219. if (err) {
  220. nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
  221. dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
  222. } else {
  223. dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
  224. }
  225. return err;
  226. }
  227. static int __init init_nfs_v4(void)
  228. {
  229. int err;
  230. err = nfs_dns_resolver_init();
  231. if (err)
  232. goto out;
  233. err = nfs_idmap_init();
  234. if (err)
  235. goto out1;
  236. #ifdef CONFIG_NFS_V4_2
  237. err = nfs4_xattr_cache_init();
  238. if (err)
  239. goto out2;
  240. #endif
  241. err = nfs4_register_sysctl();
  242. if (err)
  243. goto out2;
  244. #ifdef CONFIG_NFS_V4_2
  245. nfs42_ssc_register_ops();
  246. #endif
  247. register_nfs_version(&nfs_v4);
  248. return 0;
  249. out2:
  250. nfs_idmap_quit();
  251. out1:
  252. nfs_dns_resolver_destroy();
  253. out:
  254. return err;
  255. }
  256. static void __exit exit_nfs_v4(void)
  257. {
  258. /* Not called in the _init(), conditionally loaded */
  259. nfs4_pnfs_v3_ds_connect_unload();
  260. unregister_nfs_version(&nfs_v4);
  261. #ifdef CONFIG_NFS_V4_2
  262. nfs4_xattr_cache_exit();
  263. nfs42_ssc_unregister_ops();
  264. #endif
  265. nfs4_unregister_sysctl();
  266. nfs_idmap_quit();
  267. nfs_dns_resolver_destroy();
  268. }
  269. MODULE_DESCRIPTION("NFSv4 client support");
  270. MODULE_LICENSE("GPL");
  271. module_init(init_nfs_v4);
  272. module_exit(exit_nfs_v4);