host.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. */
  150. if ((clnt = host->h_rpcclnt) != NULL) {
  151. xprt = clnt->cl_xprt;
  152. if (time_after_eq(jiffies, host->h_nextrebind)) {
  153. rpc_force_rebind(clnt);
  154. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  155. dprintk("lockd: next rebind in %ld jiffies\n",
  156. host->h_nextrebind - jiffies);
  157. }
  158. } else {
  159. xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
  160. if (IS_ERR(xprt))
  161. goto forgetit;
  162. xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
  163. xprt->resvport = 1; /* NLM requires a reserved port */
  164. /* Existing NLM servers accept AUTH_UNIX only */
  165. clnt = rpc_create_client(xprt, host->h_name, &nlm_program,
  166. host->h_version, RPC_AUTH_UNIX);
  167. if (IS_ERR(clnt))
  168. goto forgetit;
  169. clnt->cl_autobind = 1; /* turn on pmap queries */
  170. host->h_rpcclnt = clnt;
  171. }
  172. up(&host->h_sema);
  173. return clnt;
  174. forgetit:
  175. printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
  176. up(&host->h_sema);
  177. return NULL;
  178. }
  179. /*
  180. * Force a portmap lookup of the remote lockd port
  181. */
  182. void
  183. nlm_rebind_host(struct nlm_host *host)
  184. {
  185. dprintk("lockd: rebind host %s\n", host->h_name);
  186. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  187. rpc_force_rebind(host->h_rpcclnt);
  188. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  189. }
  190. }
  191. /*
  192. * Increment NLM host count
  193. */
  194. struct nlm_host * nlm_get_host(struct nlm_host *host)
  195. {
  196. if (host) {
  197. dprintk("lockd: get host %s\n", host->h_name);
  198. atomic_inc(&host->h_count);
  199. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  200. }
  201. return host;
  202. }
  203. /*
  204. * Release NLM host after use
  205. */
  206. void nlm_release_host(struct nlm_host *host)
  207. {
  208. if (host != NULL) {
  209. dprintk("lockd: release host %s\n", host->h_name);
  210. atomic_dec(&host->h_count);
  211. BUG_ON(atomic_read(&host->h_count) < 0);
  212. }
  213. }
  214. /*
  215. * Shut down the hosts module.
  216. * Note that this routine is called only at server shutdown time.
  217. */
  218. void
  219. nlm_shutdown_hosts(void)
  220. {
  221. struct nlm_host *host;
  222. int i;
  223. dprintk("lockd: shutting down host module\n");
  224. down(&nlm_host_sema);
  225. /* First, make all hosts eligible for gc */
  226. dprintk("lockd: nuking all hosts...\n");
  227. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  228. for (host = nlm_hosts[i]; host; host = host->h_next)
  229. host->h_expires = jiffies - 1;
  230. }
  231. /* Then, perform a garbage collection pass */
  232. nlm_gc_hosts();
  233. up(&nlm_host_sema);
  234. /* complain if any hosts are left */
  235. if (nrhosts) {
  236. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  237. dprintk("lockd: %d hosts left:\n", nrhosts);
  238. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  239. for (host = nlm_hosts[i]; host; host = host->h_next) {
  240. dprintk(" %s (cnt %d use %d exp %ld)\n",
  241. host->h_name, atomic_read(&host->h_count),
  242. host->h_inuse, host->h_expires);
  243. }
  244. }
  245. }
  246. }
  247. /*
  248. * Garbage collect any unused NLM hosts.
  249. * This GC combines reference counting for async operations with
  250. * mark & sweep for resources held by remote clients.
  251. */
  252. static void
  253. nlm_gc_hosts(void)
  254. {
  255. struct nlm_host **q, *host;
  256. struct rpc_clnt *clnt;
  257. int i;
  258. dprintk("lockd: host garbage collection\n");
  259. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  260. for (host = nlm_hosts[i]; host; host = host->h_next)
  261. host->h_inuse = 0;
  262. }
  263. /* Mark all hosts that hold locks, blocks or shares */
  264. nlmsvc_mark_resources();
  265. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  266. q = &nlm_hosts[i];
  267. while ((host = *q) != NULL) {
  268. if (atomic_read(&host->h_count) || host->h_inuse
  269. || time_before(jiffies, host->h_expires)) {
  270. dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
  271. host->h_name, atomic_read(&host->h_count),
  272. host->h_inuse, host->h_expires);
  273. q = &host->h_next;
  274. continue;
  275. }
  276. dprintk("lockd: delete host %s\n", host->h_name);
  277. *q = host->h_next;
  278. /* Don't unmonitor hosts that have been invalidated */
  279. if (host->h_monitored && !host->h_killed)
  280. nsm_unmonitor(host);
  281. if ((clnt = host->h_rpcclnt) != NULL) {
  282. if (atomic_read(&clnt->cl_users)) {
  283. printk(KERN_WARNING
  284. "lockd: active RPC handle\n");
  285. clnt->cl_dead = 1;
  286. } else {
  287. rpc_destroy_client(host->h_rpcclnt);
  288. }
  289. }
  290. BUG_ON(!list_empty(&host->h_lockowners));
  291. kfree(host);
  292. nrhosts--;
  293. }
  294. }
  295. next_gc = jiffies + NLM_HOST_COLLECT;
  296. }