cache_lib.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/nfs/cache_lib.c
  4. *
  5. * Helper routines for the NFS client caches
  6. *
  7. * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
  8. */
  9. #include <linux/kmod.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/mount.h>
  13. #include <linux/namei.h>
  14. #include <linux/slab.h>
  15. #include <linux/sunrpc/cache.h>
  16. #include <linux/sunrpc/rpc_pipe_fs.h>
  17. #include <net/net_namespace.h>
  18. #include "cache_lib.h"
  19. #define NFS_CACHE_UPCALL_PATHLEN 256
  20. #define NFS_CACHE_UPCALL_TIMEOUT 15
  21. static char nfs_cache_getent_prog[NFS_CACHE_UPCALL_PATHLEN] =
  22. "/sbin/nfs_cache_getent";
  23. static unsigned long nfs_cache_getent_timeout = NFS_CACHE_UPCALL_TIMEOUT;
  24. module_param_string(cache_getent, nfs_cache_getent_prog,
  25. sizeof(nfs_cache_getent_prog), 0600);
  26. MODULE_PARM_DESC(cache_getent, "Path to the client cache upcall program");
  27. module_param_named(cache_getent_timeout, nfs_cache_getent_timeout, ulong, 0600);
  28. MODULE_PARM_DESC(cache_getent_timeout, "Timeout (in seconds) after which "
  29. "the cache upcall is assumed to have failed");
  30. int nfs_cache_upcall(struct cache_detail *cd, char *entry_name)
  31. {
  32. static char *envp[] = { "HOME=/",
  33. "TERM=linux",
  34. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  35. NULL
  36. };
  37. char *argv[] = {
  38. nfs_cache_getent_prog,
  39. cd->name,
  40. entry_name,
  41. NULL
  42. };
  43. int ret = -EACCES;
  44. if (nfs_cache_getent_prog[0] == '\0')
  45. goto out;
  46. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  47. /*
  48. * Disable the upcall mechanism if we're getting an ENOENT or
  49. * EACCES error. The admin can re-enable it on the fly by using
  50. * sysfs to set the 'cache_getent' parameter once the problem
  51. * has been fixed.
  52. */
  53. if (ret == -ENOENT || ret == -EACCES)
  54. nfs_cache_getent_prog[0] = '\0';
  55. out:
  56. return ret > 0 ? 0 : ret;
  57. }
  58. /*
  59. * Deferred request handling
  60. */
  61. void nfs_cache_defer_req_put(struct nfs_cache_defer_req *dreq)
  62. {
  63. if (refcount_dec_and_test(&dreq->count))
  64. kfree(dreq);
  65. }
  66. static void nfs_dns_cache_revisit(struct cache_deferred_req *d, int toomany)
  67. {
  68. struct nfs_cache_defer_req *dreq;
  69. dreq = container_of(d, struct nfs_cache_defer_req, deferred_req);
  70. complete(&dreq->completion);
  71. nfs_cache_defer_req_put(dreq);
  72. }
  73. static struct cache_deferred_req *nfs_dns_cache_defer(struct cache_req *req)
  74. {
  75. struct nfs_cache_defer_req *dreq;
  76. dreq = container_of(req, struct nfs_cache_defer_req, req);
  77. dreq->deferred_req.revisit = nfs_dns_cache_revisit;
  78. refcount_inc(&dreq->count);
  79. return &dreq->deferred_req;
  80. }
  81. struct nfs_cache_defer_req *nfs_cache_defer_req_alloc(void)
  82. {
  83. struct nfs_cache_defer_req *dreq;
  84. dreq = kzalloc(sizeof(*dreq), GFP_KERNEL);
  85. if (dreq) {
  86. init_completion(&dreq->completion);
  87. refcount_set(&dreq->count, 1);
  88. dreq->req.defer = nfs_dns_cache_defer;
  89. }
  90. return dreq;
  91. }
  92. int nfs_cache_wait_for_upcall(struct nfs_cache_defer_req *dreq)
  93. {
  94. if (wait_for_completion_timeout(&dreq->completion,
  95. nfs_cache_getent_timeout * HZ) == 0)
  96. return -ETIMEDOUT;
  97. return 0;
  98. }
  99. int nfs_cache_register_sb(struct super_block *sb, struct cache_detail *cd)
  100. {
  101. int ret;
  102. struct dentry *dir;
  103. dir = rpc_d_lookup_sb(sb, "cache");
  104. ret = sunrpc_cache_register_pipefs(dir, cd->name, 0600, cd);
  105. dput(dir);
  106. return ret;
  107. }
  108. int nfs_cache_register_net(struct net *net, struct cache_detail *cd)
  109. {
  110. struct super_block *pipefs_sb;
  111. int ret = 0;
  112. sunrpc_init_cache_detail(cd);
  113. pipefs_sb = rpc_get_sb_net(net);
  114. if (pipefs_sb) {
  115. ret = nfs_cache_register_sb(pipefs_sb, cd);
  116. rpc_put_sb_net(net);
  117. if (ret)
  118. sunrpc_destroy_cache_detail(cd);
  119. }
  120. return ret;
  121. }
  122. void nfs_cache_unregister_sb(struct super_block *sb, struct cache_detail *cd)
  123. {
  124. sunrpc_cache_unregister_pipefs(cd);
  125. }
  126. void nfs_cache_unregister_net(struct net *net, struct cache_detail *cd)
  127. {
  128. struct super_block *pipefs_sb;
  129. pipefs_sb = rpc_get_sb_net(net);
  130. if (pipefs_sb) {
  131. nfs_cache_unregister_sb(pipefs_sb, cd);
  132. rpc_put_sb_net(net);
  133. }
  134. sunrpc_destroy_cache_detail(cd);
  135. }