host.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * linux/fs/lockd/host.c
  3. *
  4. * Management for NLM peer hosts. The nlm_host struct is shared
  5. * between client and server implementation. The only reason to
  6. * do so is to reduce code bloat.
  7. *
  8. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/in.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/sunrpc/svc.h>
  16. #include <linux/lockd/lockd.h>
  17. #include <linux/lockd/sm_inter.h>
  18. #define NLMDBG_FACILITY NLMDBG_HOSTCACHE
  19. #define NLM_HOST_MAX 64
  20. #define NLM_HOST_NRHASH 32
  21. #define NLM_ADDRHASH(addr) (ntohl(addr) & (NLM_HOST_NRHASH-1))
  22. #define NLM_HOST_REBIND (60 * HZ)
  23. #define NLM_HOST_EXPIRE ((nrhosts > NLM_HOST_MAX)? 300 * HZ : 120 * HZ)
  24. #define NLM_HOST_COLLECT ((nrhosts > NLM_HOST_MAX)? 120 * HZ : 60 * HZ)
  25. #define NLM_HOST_ADDR(sv) (&(sv)->s_nlmclnt->cl_xprt->addr)
  26. static struct nlm_host * nlm_hosts[NLM_HOST_NRHASH];
  27. static unsigned long next_gc;
  28. static int nrhosts;
  29. static DECLARE_MUTEX(nlm_host_sema);
  30. static void nlm_gc_hosts(void);
  31. /*
  32. * Find an NLM server handle in the cache. If there is none, create it.
  33. */
  34. struct nlm_host *
  35. nlmclnt_lookup_host(struct sockaddr_in *sin, int proto, int version)
  36. {
  37. return nlm_lookup_host(0, sin, proto, version);
  38. }
  39. /*
  40. * Find an NLM client handle in the cache. If there is none, create it.
  41. */
  42. struct nlm_host *
  43. nlmsvc_lookup_host(struct svc_rqst *rqstp)
  44. {
  45. return nlm_lookup_host(1, &rqstp->rq_addr,
  46. rqstp->rq_prot, rqstp->rq_vers);
  47. }
  48. /*
  49. * Common host lookup routine for server & client
  50. */
  51. struct nlm_host *
  52. nlm_lookup_host(int server, struct sockaddr_in *sin,
  53. int proto, int version)
  54. {
  55. struct nlm_host *host, **hp;
  56. u32 addr;
  57. int hash;
  58. dprintk("lockd: nlm_lookup_host(%08x, p=%d, v=%d)\n",
  59. (unsigned)(sin? ntohl(sin->sin_addr.s_addr) : 0), proto, version);
  60. hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
  61. /* Lock hash table */
  62. down(&nlm_host_sema);
  63. if (time_after_eq(jiffies, next_gc))
  64. nlm_gc_hosts();
  65. for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
  66. if (host->h_proto != proto)
  67. continue;
  68. if (host->h_version != version)
  69. continue;
  70. if (host->h_server != server)
  71. continue;
  72. if (nlm_cmp_addr(&host->h_addr, sin)) {
  73. if (hp != nlm_hosts + hash) {
  74. *hp = host->h_next;
  75. host->h_next = nlm_hosts[hash];
  76. nlm_hosts[hash] = host;
  77. }
  78. nlm_get_host(host);
  79. up(&nlm_host_sema);
  80. return host;
  81. }
  82. }
  83. /* Ooops, no host found, create it */
  84. dprintk("lockd: creating host entry\n");
  85. if (!(host = (struct nlm_host *) kmalloc(sizeof(*host), GFP_KERNEL)))
  86. goto nohost;
  87. memset(host, 0, sizeof(*host));
  88. addr = sin->sin_addr.s_addr;
  89. sprintf(host->h_name, "%u.%u.%u.%u", NIPQUAD(addr));
  90. host->h_addr = *sin;
  91. host->h_addr.sin_port = 0; /* ouch! */
  92. host->h_version = version;
  93. host->h_proto = proto;
  94. host->h_rpcclnt = NULL;
  95. init_MUTEX(&host->h_sema);
  96. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  97. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  98. atomic_set(&host->h_count, 1);
  99. init_waitqueue_head(&host->h_gracewait);
  100. host->h_state = 0; /* pseudo NSM state */
  101. host->h_nsmstate = 0; /* real NSM state */
  102. host->h_server = server;
  103. host->h_next = nlm_hosts[hash];
  104. nlm_hosts[hash] = host;
  105. INIT_LIST_HEAD(&host->h_lockowners);
  106. spin_lock_init(&host->h_lock);
  107. if (++nrhosts > NLM_HOST_MAX)
  108. next_gc = 0;
  109. nohost:
  110. up(&nlm_host_sema);
  111. return host;
  112. }
  113. struct nlm_host *
  114. nlm_find_client(void)
  115. {
  116. /* find a nlm_host for a client for which h_killed == 0.
  117. * and return it
  118. */
  119. int hash;
  120. down(&nlm_host_sema);
  121. for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
  122. struct nlm_host *host, **hp;
  123. for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
  124. if (host->h_server &&
  125. host->h_killed == 0) {
  126. nlm_get_host(host);
  127. up(&nlm_host_sema);
  128. return host;
  129. }
  130. }
  131. }
  132. up(&nlm_host_sema);
  133. return NULL;
  134. }
  135. /*
  136. * Create the NLM RPC client for an NLM peer
  137. */
  138. struct rpc_clnt *
  139. nlm_bind_host(struct nlm_host *host)
  140. {
  141. struct rpc_clnt *clnt;
  142. struct rpc_xprt *xprt;
  143. dprintk("lockd: nlm_bind_host(%08x)\n",
  144. (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
  145. /* Lock host handle */
  146. down(&host->h_sema);
  147. /* If we've already created an RPC client, check whether
  148. * RPC rebind is required
  149. * Note: why keep rebinding if we're on a tcp connection?
  150. */
  151. if ((clnt = host->h_rpcclnt) != NULL) {
  152. xprt = clnt->cl_xprt;
  153. if (!xprt->stream && time_after_eq(jiffies, host->h_nextrebind)) {
  154. clnt->cl_port = 0;
  155. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  156. dprintk("lockd: next rebind in %ld jiffies\n",
  157. host->h_nextrebind - jiffies);
  158. }
  159. } else {
  160. xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
  161. if (IS_ERR(xprt))
  162. goto forgetit;
  163. xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
  164. xprt->nocong = 1; /* No congestion control for NLM */
  165. xprt->resvport = 1; /* NLM requires a reserved port */
  166. /* Existing NLM servers accept AUTH_UNIX only */
  167. clnt = rpc_create_client(xprt, host->h_name, &nlm_program,
  168. host->h_version, RPC_AUTH_UNIX);
  169. if (IS_ERR(clnt))
  170. goto forgetit;
  171. clnt->cl_autobind = 1; /* turn on pmap queries */
  172. host->h_rpcclnt = clnt;
  173. }
  174. up(&host->h_sema);
  175. return clnt;
  176. forgetit:
  177. printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
  178. up(&host->h_sema);
  179. return NULL;
  180. }
  181. /*
  182. * Force a portmap lookup of the remote lockd port
  183. */
  184. void
  185. nlm_rebind_host(struct nlm_host *host)
  186. {
  187. dprintk("lockd: rebind host %s\n", host->h_name);
  188. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  189. host->h_rpcclnt->cl_port = 0;
  190. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  191. }
  192. }
  193. /*
  194. * Increment NLM host count
  195. */
  196. struct nlm_host * nlm_get_host(struct nlm_host *host)
  197. {
  198. if (host) {
  199. dprintk("lockd: get host %s\n", host->h_name);
  200. atomic_inc(&host->h_count);
  201. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  202. }
  203. return host;
  204. }
  205. /*
  206. * Release NLM host after use
  207. */
  208. void nlm_release_host(struct nlm_host *host)
  209. {
  210. if (host != NULL) {
  211. dprintk("lockd: release host %s\n", host->h_name);
  212. atomic_dec(&host->h_count);
  213. BUG_ON(atomic_read(&host->h_count) < 0);
  214. }
  215. }
  216. /*
  217. * Shut down the hosts module.
  218. * Note that this routine is called only at server shutdown time.
  219. */
  220. void
  221. nlm_shutdown_hosts(void)
  222. {
  223. struct nlm_host *host;
  224. int i;
  225. dprintk("lockd: shutting down host module\n");
  226. down(&nlm_host_sema);
  227. /* First, make all hosts eligible for gc */
  228. dprintk("lockd: nuking all hosts...\n");
  229. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  230. for (host = nlm_hosts[i]; host; host = host->h_next)
  231. host->h_expires = jiffies - 1;
  232. }
  233. /* Then, perform a garbage collection pass */
  234. nlm_gc_hosts();
  235. up(&nlm_host_sema);
  236. /* complain if any hosts are left */
  237. if (nrhosts) {
  238. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  239. dprintk("lockd: %d hosts left:\n", nrhosts);
  240. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  241. for (host = nlm_hosts[i]; host; host = host->h_next) {
  242. dprintk(" %s (cnt %d use %d exp %ld)\n",
  243. host->h_name, atomic_read(&host->h_count),
  244. host->h_inuse, host->h_expires);
  245. }
  246. }
  247. }
  248. }
  249. /*
  250. * Garbage collect any unused NLM hosts.
  251. * This GC combines reference counting for async operations with
  252. * mark & sweep for resources held by remote clients.
  253. */
  254. static void
  255. nlm_gc_hosts(void)
  256. {
  257. struct nlm_host **q, *host;
  258. struct rpc_clnt *clnt;
  259. int i;
  260. dprintk("lockd: host garbage collection\n");
  261. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  262. for (host = nlm_hosts[i]; host; host = host->h_next)
  263. host->h_inuse = 0;
  264. }
  265. /* Mark all hosts that hold locks, blocks or shares */
  266. nlmsvc_mark_resources();
  267. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  268. q = &nlm_hosts[i];
  269. while ((host = *q) != NULL) {
  270. if (atomic_read(&host->h_count) || host->h_inuse
  271. || time_before(jiffies, host->h_expires)) {
  272. dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
  273. host->h_name, atomic_read(&host->h_count),
  274. host->h_inuse, host->h_expires);
  275. q = &host->h_next;
  276. continue;
  277. }
  278. dprintk("lockd: delete host %s\n", host->h_name);
  279. *q = host->h_next;
  280. /* Don't unmonitor hosts that have been invalidated */
  281. if (host->h_monitored && !host->h_killed)
  282. nsm_unmonitor(host);
  283. if ((clnt = host->h_rpcclnt) != NULL) {
  284. if (atomic_read(&clnt->cl_users)) {
  285. printk(KERN_WARNING
  286. "lockd: active RPC handle\n");
  287. clnt->cl_dead = 1;
  288. } else {
  289. rpc_destroy_client(host->h_rpcclnt);
  290. }
  291. }
  292. BUG_ON(!list_empty(&host->h_lockowners));
  293. kfree(host);
  294. nrhosts--;
  295. }
  296. }
  297. next_gc = jiffies + NLM_HOST_COLLECT;
  298. }