host.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. mutex_init(&host->h_mutex);
  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. init_rwsem(&host->h_rwsem);
  102. host->h_state = 0; /* pseudo NSM state */
  103. host->h_nsmstate = 0; /* real NSM state */
  104. host->h_server = server;
  105. host->h_next = nlm_hosts[hash];
  106. nlm_hosts[hash] = host;
  107. INIT_LIST_HEAD(&host->h_lockowners);
  108. spin_lock_init(&host->h_lock);
  109. INIT_LIST_HEAD(&host->h_granted);
  110. INIT_LIST_HEAD(&host->h_reclaim);
  111. if (++nrhosts > NLM_HOST_MAX)
  112. next_gc = 0;
  113. nohost:
  114. mutex_unlock(&nlm_host_mutex);
  115. return host;
  116. }
  117. struct nlm_host *
  118. nlm_find_client(void)
  119. {
  120. /* find a nlm_host for a client for which h_killed == 0.
  121. * and return it
  122. */
  123. int hash;
  124. mutex_lock(&nlm_host_mutex);
  125. for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
  126. struct nlm_host *host, **hp;
  127. for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
  128. if (host->h_server &&
  129. host->h_killed == 0) {
  130. nlm_get_host(host);
  131. mutex_unlock(&nlm_host_mutex);
  132. return host;
  133. }
  134. }
  135. }
  136. mutex_unlock(&nlm_host_mutex);
  137. return NULL;
  138. }
  139. /*
  140. * Create the NLM RPC client for an NLM peer
  141. */
  142. struct rpc_clnt *
  143. nlm_bind_host(struct nlm_host *host)
  144. {
  145. struct rpc_clnt *clnt;
  146. struct rpc_xprt *xprt;
  147. dprintk("lockd: nlm_bind_host(%08x)\n",
  148. (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
  149. /* Lock host handle */
  150. mutex_lock(&host->h_mutex);
  151. /* If we've already created an RPC client, check whether
  152. * RPC rebind is required
  153. */
  154. if ((clnt = host->h_rpcclnt) != NULL) {
  155. xprt = clnt->cl_xprt;
  156. if (time_after_eq(jiffies, host->h_nextrebind)) {
  157. rpc_force_rebind(clnt);
  158. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  159. dprintk("lockd: next rebind in %ld jiffies\n",
  160. host->h_nextrebind - jiffies);
  161. }
  162. } else {
  163. xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
  164. if (IS_ERR(xprt))
  165. goto forgetit;
  166. xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
  167. xprt->resvport = 1; /* NLM requires a reserved port */
  168. /* Existing NLM servers accept AUTH_UNIX only */
  169. clnt = rpc_new_client(xprt, host->h_name, &nlm_program,
  170. host->h_version, RPC_AUTH_UNIX);
  171. if (IS_ERR(clnt))
  172. goto forgetit;
  173. clnt->cl_autobind = 1; /* turn on pmap queries */
  174. clnt->cl_softrtry = 1; /* All queries are soft */
  175. host->h_rpcclnt = clnt;
  176. }
  177. mutex_unlock(&host->h_mutex);
  178. return clnt;
  179. forgetit:
  180. printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
  181. mutex_unlock(&host->h_mutex);
  182. return NULL;
  183. }
  184. /*
  185. * Force a portmap lookup of the remote lockd port
  186. */
  187. void
  188. nlm_rebind_host(struct nlm_host *host)
  189. {
  190. dprintk("lockd: rebind host %s\n", host->h_name);
  191. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  192. rpc_force_rebind(host->h_rpcclnt);
  193. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  194. }
  195. }
  196. /*
  197. * Increment NLM host count
  198. */
  199. struct nlm_host * nlm_get_host(struct nlm_host *host)
  200. {
  201. if (host) {
  202. dprintk("lockd: get host %s\n", host->h_name);
  203. atomic_inc(&host->h_count);
  204. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  205. }
  206. return host;
  207. }
  208. /*
  209. * Release NLM host after use
  210. */
  211. void nlm_release_host(struct nlm_host *host)
  212. {
  213. if (host != NULL) {
  214. dprintk("lockd: release host %s\n", host->h_name);
  215. BUG_ON(atomic_read(&host->h_count) < 0);
  216. if (atomic_dec_and_test(&host->h_count)) {
  217. BUG_ON(!list_empty(&host->h_lockowners));
  218. BUG_ON(!list_empty(&host->h_granted));
  219. BUG_ON(!list_empty(&host->h_reclaim));
  220. }
  221. }
  222. }
  223. /*
  224. * Shut down the hosts module.
  225. * Note that this routine is called only at server shutdown time.
  226. */
  227. void
  228. nlm_shutdown_hosts(void)
  229. {
  230. struct nlm_host *host;
  231. int i;
  232. dprintk("lockd: shutting down host module\n");
  233. mutex_lock(&nlm_host_mutex);
  234. /* First, make all hosts eligible for gc */
  235. dprintk("lockd: nuking all hosts...\n");
  236. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  237. for (host = nlm_hosts[i]; host; host = host->h_next)
  238. host->h_expires = jiffies - 1;
  239. }
  240. /* Then, perform a garbage collection pass */
  241. nlm_gc_hosts();
  242. mutex_unlock(&nlm_host_mutex);
  243. /* complain if any hosts are left */
  244. if (nrhosts) {
  245. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  246. dprintk("lockd: %d hosts left:\n", nrhosts);
  247. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  248. for (host = nlm_hosts[i]; host; host = host->h_next) {
  249. dprintk(" %s (cnt %d use %d exp %ld)\n",
  250. host->h_name, atomic_read(&host->h_count),
  251. host->h_inuse, host->h_expires);
  252. }
  253. }
  254. }
  255. }
  256. /*
  257. * Garbage collect any unused NLM hosts.
  258. * This GC combines reference counting for async operations with
  259. * mark & sweep for resources held by remote clients.
  260. */
  261. static void
  262. nlm_gc_hosts(void)
  263. {
  264. struct nlm_host **q, *host;
  265. struct rpc_clnt *clnt;
  266. int i;
  267. dprintk("lockd: host garbage collection\n");
  268. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  269. for (host = nlm_hosts[i]; host; host = host->h_next)
  270. host->h_inuse = 0;
  271. }
  272. /* Mark all hosts that hold locks, blocks or shares */
  273. nlmsvc_mark_resources();
  274. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  275. q = &nlm_hosts[i];
  276. while ((host = *q) != NULL) {
  277. if (atomic_read(&host->h_count) || host->h_inuse
  278. || time_before(jiffies, host->h_expires)) {
  279. dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
  280. host->h_name, atomic_read(&host->h_count),
  281. host->h_inuse, host->h_expires);
  282. q = &host->h_next;
  283. continue;
  284. }
  285. dprintk("lockd: delete host %s\n", host->h_name);
  286. *q = host->h_next;
  287. /* Don't unmonitor hosts that have been invalidated */
  288. if (host->h_monitored && !host->h_killed)
  289. nsm_unmonitor(host);
  290. if ((clnt = host->h_rpcclnt) != NULL) {
  291. if (atomic_read(&clnt->cl_users)) {
  292. printk(KERN_WARNING
  293. "lockd: active RPC handle\n");
  294. clnt->cl_dead = 1;
  295. } else {
  296. rpc_destroy_client(host->h_rpcclnt);
  297. }
  298. }
  299. kfree(host);
  300. nrhosts--;
  301. }
  302. }
  303. next_gc = jiffies + NLM_HOST_COLLECT;
  304. }