nfs4super.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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(sizeof(*new), GFP_KERNEL);
  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 fs_context *root_fc;
  131. struct vfsmount *root_mnt;
  132. struct dentry *dentry;
  133. size_t len;
  134. int ret;
  135. struct fs_parameter param = {
  136. .key = "source",
  137. .type = fs_value_is_string,
  138. .dirfd = -1,
  139. };
  140. if (IS_ERR(server))
  141. return PTR_ERR(server);
  142. root_fc = vfs_dup_fs_context(fc);
  143. if (IS_ERR(root_fc)) {
  144. nfs_free_server(server);
  145. return PTR_ERR(root_fc);
  146. }
  147. kfree(root_fc->source);
  148. root_fc->source = NULL;
  149. root_ctx = nfs_fc2context(root_fc);
  150. root_ctx->internal = true;
  151. root_ctx->server = server;
  152. /* We leave export_path unset as it's not used to find the root. */
  153. len = strlen(hostname) + 5;
  154. param.string = kmalloc(len, GFP_KERNEL);
  155. if (param.string == NULL) {
  156. put_fs_context(root_fc);
  157. return -ENOMEM;
  158. }
  159. /* Does hostname needs to be enclosed in brackets? */
  160. if (strchr(hostname, ':'))
  161. param.size = snprintf(param.string, len, "[%s]:/", hostname);
  162. else
  163. param.size = snprintf(param.string, len, "%s:/", hostname);
  164. ret = vfs_parse_fs_param(root_fc, &param);
  165. kfree(param.string);
  166. if (ret < 0) {
  167. put_fs_context(root_fc);
  168. return ret;
  169. }
  170. root_mnt = fc_mount(root_fc);
  171. put_fs_context(root_fc);
  172. if (IS_ERR(root_mnt))
  173. return PTR_ERR(root_mnt);
  174. ret = nfs_referral_loop_protect();
  175. if (ret) {
  176. mntput(root_mnt);
  177. return ret;
  178. }
  179. dentry = mount_subtree(root_mnt, export_path);
  180. nfs_referral_loop_unprotect();
  181. if (IS_ERR(dentry))
  182. return PTR_ERR(dentry);
  183. fc->root = dentry;
  184. return 0;
  185. }
  186. int nfs4_try_get_tree(struct fs_context *fc)
  187. {
  188. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  189. int err;
  190. dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
  191. /* We create a mount for the server's root, walk to the requested
  192. * location and then create another mount for that.
  193. */
  194. err= do_nfs4_mount(nfs4_create_server(fc),
  195. fc, ctx->nfs_server.hostname,
  196. ctx->nfs_server.export_path);
  197. if (err) {
  198. nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
  199. dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
  200. } else {
  201. dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
  202. }
  203. return err;
  204. }
  205. /*
  206. * Create an NFS4 server record on referral traversal
  207. */
  208. int nfs4_get_referral_tree(struct fs_context *fc)
  209. {
  210. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  211. int err;
  212. dprintk("--> nfs4_referral_mount()\n");
  213. /* create a new volume representation */
  214. err = do_nfs4_mount(nfs4_create_referral_server(fc),
  215. fc, ctx->nfs_server.hostname,
  216. ctx->nfs_server.export_path);
  217. if (err) {
  218. nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
  219. dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
  220. } else {
  221. dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
  222. }
  223. return err;
  224. }
  225. static int __init init_nfs_v4(void)
  226. {
  227. int err;
  228. err = nfs_dns_resolver_init();
  229. if (err)
  230. goto out;
  231. err = nfs_idmap_init();
  232. if (err)
  233. goto out1;
  234. #ifdef CONFIG_NFS_V4_2
  235. err = nfs4_xattr_cache_init();
  236. if (err)
  237. goto out2;
  238. #endif
  239. err = nfs4_register_sysctl();
  240. if (err)
  241. goto out2;
  242. #ifdef CONFIG_NFS_V4_2
  243. nfs42_ssc_register_ops();
  244. #endif
  245. register_nfs_version(&nfs_v4);
  246. return 0;
  247. out2:
  248. nfs_idmap_quit();
  249. out1:
  250. nfs_dns_resolver_destroy();
  251. out:
  252. return err;
  253. }
  254. static void __exit exit_nfs_v4(void)
  255. {
  256. /* Not called in the _init(), conditionally loaded */
  257. nfs4_pnfs_v3_ds_connect_unload();
  258. unregister_nfs_version(&nfs_v4);
  259. #ifdef CONFIG_NFS_V4_2
  260. nfs4_xattr_cache_exit();
  261. nfs42_ssc_unregister_ops();
  262. #endif
  263. nfs4_unregister_sysctl();
  264. nfs_idmap_quit();
  265. nfs_dns_resolver_destroy();
  266. }
  267. MODULE_LICENSE("GPL");
  268. MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
  269. module_init(init_nfs_v4);
  270. module_exit(exit_nfs_v4);