host.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. static struct nlm_host * nlm_hosts[NLM_HOST_NRHASH];
  27. static unsigned long next_gc;
  28. static int nrhosts;
  29. static DEFINE_MUTEX(nlm_host_mutex);
  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. mutex_lock(&nlm_host_mutex);
  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. mutex_unlock(&nlm_host_mutex);
  80. return host;
  81. }
  82. }
  83. /* Ooops, no host found, create it */
  84. dprintk("lockd: creating host entry\n");
  85. host = kzalloc(sizeof(*host), GFP_KERNEL);
  86. if (!host)
  87. goto nohost;
  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. mutex_init(&host->h_mutex);
  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. init_rwsem(&host->h_rwsem);
  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. dprintk("lockd: nlm_bind_host(%08x)\n",
  146. (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
  147. /* Lock host handle */
  148. mutex_lock(&host->h_mutex);
  149. /* If we've already created an RPC client, check whether
  150. * RPC rebind is required
  151. */
  152. if ((clnt = host->h_rpcclnt) != NULL) {
  153. if (time_after_eq(jiffies, host->h_nextrebind)) {
  154. rpc_force_rebind(clnt);
  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. unsigned long increment = nlmsvc_timeout * HZ;
  161. struct rpc_timeout timeparms = {
  162. .to_initval = increment,
  163. .to_increment = increment,
  164. .to_maxval = increment * 6UL,
  165. .to_retries = 5U,
  166. };
  167. struct rpc_create_args args = {
  168. .protocol = host->h_proto,
  169. .address = (struct sockaddr *)&host->h_addr,
  170. .addrsize = sizeof(host->h_addr),
  171. .timeout = &timeparms,
  172. .servername = host->h_name,
  173. .program = &nlm_program,
  174. .version = host->h_version,
  175. .authflavor = RPC_AUTH_UNIX,
  176. .flags = (RPC_CLNT_CREATE_HARDRTRY |
  177. RPC_CLNT_CREATE_AUTOBIND),
  178. };
  179. clnt = rpc_create(&args);
  180. if (!IS_ERR(clnt))
  181. host->h_rpcclnt = clnt;
  182. else {
  183. printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
  184. clnt = NULL;
  185. }
  186. }
  187. mutex_unlock(&host->h_mutex);
  188. return clnt;
  189. }
  190. /*
  191. * Force a portmap lookup of the remote lockd port
  192. */
  193. void
  194. nlm_rebind_host(struct nlm_host *host)
  195. {
  196. dprintk("lockd: rebind host %s\n", host->h_name);
  197. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  198. rpc_force_rebind(host->h_rpcclnt);
  199. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  200. }
  201. }
  202. /*
  203. * Increment NLM host count
  204. */
  205. struct nlm_host * nlm_get_host(struct nlm_host *host)
  206. {
  207. if (host) {
  208. dprintk("lockd: get host %s\n", host->h_name);
  209. atomic_inc(&host->h_count);
  210. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  211. }
  212. return host;
  213. }
  214. /*
  215. * Release NLM host after use
  216. */
  217. void nlm_release_host(struct nlm_host *host)
  218. {
  219. if (host != NULL) {
  220. dprintk("lockd: release host %s\n", host->h_name);
  221. BUG_ON(atomic_read(&host->h_count) < 0);
  222. if (atomic_dec_and_test(&host->h_count)) {
  223. BUG_ON(!list_empty(&host->h_lockowners));
  224. BUG_ON(!list_empty(&host->h_granted));
  225. BUG_ON(!list_empty(&host->h_reclaim));
  226. }
  227. }
  228. }
  229. /*
  230. * Shut down the hosts module.
  231. * Note that this routine is called only at server shutdown time.
  232. */
  233. void
  234. nlm_shutdown_hosts(void)
  235. {
  236. struct nlm_host *host;
  237. int i;
  238. dprintk("lockd: shutting down host module\n");
  239. mutex_lock(&nlm_host_mutex);
  240. /* First, make all hosts eligible for gc */
  241. dprintk("lockd: nuking all hosts...\n");
  242. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  243. for (host = nlm_hosts[i]; host; host = host->h_next)
  244. host->h_expires = jiffies - 1;
  245. }
  246. /* Then, perform a garbage collection pass */
  247. nlm_gc_hosts();
  248. mutex_unlock(&nlm_host_mutex);
  249. /* complain if any hosts are left */
  250. if (nrhosts) {
  251. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  252. dprintk("lockd: %d hosts left:\n", nrhosts);
  253. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  254. for (host = nlm_hosts[i]; host; host = host->h_next) {
  255. dprintk(" %s (cnt %d use %d exp %ld)\n",
  256. host->h_name, atomic_read(&host->h_count),
  257. host->h_inuse, host->h_expires);
  258. }
  259. }
  260. }
  261. }
  262. /*
  263. * Garbage collect any unused NLM hosts.
  264. * This GC combines reference counting for async operations with
  265. * mark & sweep for resources held by remote clients.
  266. */
  267. static void
  268. nlm_gc_hosts(void)
  269. {
  270. struct nlm_host **q, *host;
  271. struct rpc_clnt *clnt;
  272. int i;
  273. dprintk("lockd: host garbage collection\n");
  274. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  275. for (host = nlm_hosts[i]; host; host = host->h_next)
  276. host->h_inuse = 0;
  277. }
  278. /* Mark all hosts that hold locks, blocks or shares */
  279. nlmsvc_mark_resources();
  280. for (i = 0; i < NLM_HOST_NRHASH; i++) {
  281. q = &nlm_hosts[i];
  282. while ((host = *q) != NULL) {
  283. if (atomic_read(&host->h_count) || host->h_inuse
  284. || time_before(jiffies, host->h_expires)) {
  285. dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
  286. host->h_name, atomic_read(&host->h_count),
  287. host->h_inuse, host->h_expires);
  288. q = &host->h_next;
  289. continue;
  290. }
  291. dprintk("lockd: delete host %s\n", host->h_name);
  292. *q = host->h_next;
  293. /* Don't unmonitor hosts that have been invalidated */
  294. if (host->h_monitored && !host->h_killed)
  295. nsm_unmonitor(host);
  296. if ((clnt = host->h_rpcclnt) != NULL) {
  297. if (atomic_read(&clnt->cl_users)) {
  298. printk(KERN_WARNING
  299. "lockd: active RPC handle\n");
  300. clnt->cl_dead = 1;
  301. } else {
  302. rpc_destroy_client(host->h_rpcclnt);
  303. }
  304. }
  305. kfree(host);
  306. nrhosts--;
  307. }
  308. }
  309. next_gc = jiffies + NLM_HOST_COLLECT;
  310. }