host.c 8.6 KB

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