nfs4super.c 7.5 KB

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