2 * linux/fs/lockd/host.c
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.
8 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
11 #include <linux/types.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/svc.h>
17 #include <linux/lockd/lockd.h>
18 #include <linux/lockd/sm_inter.h>
21 #define NLMDBG_FACILITY NLMDBG_HOSTCACHE
22 #define NLM_HOST_MAX 64
23 #define NLM_HOST_NRHASH 32
24 #define NLM_ADDRHASH(addr) (ntohl(addr) & (NLM_HOST_NRHASH-1))
25 #define NLM_HOST_REBIND (60 * HZ)
26 #define NLM_HOST_EXPIRE ((nrhosts > NLM_HOST_MAX)? 300 * HZ : 120 * HZ)
27 #define NLM_HOST_COLLECT ((nrhosts > NLM_HOST_MAX)? 120 * HZ : 60 * HZ)
28 #define NLM_HOST_ADDR(sv) (&(sv)->s_nlmclnt->cl_xprt->addr)
30 static struct nlm_host * nlm_hosts[NLM_HOST_NRHASH];
31 static unsigned long next_gc;
33 static DECLARE_MUTEX(nlm_host_sema);
36 static void nlm_gc_hosts(void);
39 * Find an NLM server handle in the cache. If there is none, create it.
42 nlmclnt_lookup_host(struct sockaddr_in *sin, int proto, int version)
44 return nlm_lookup_host(0, sin, proto, version);
48 * Find an NLM client handle in the cache. If there is none, create it.
51 nlmsvc_lookup_host(struct svc_rqst *rqstp)
53 return nlm_lookup_host(1, &rqstp->rq_addr,
54 rqstp->rq_prot, rqstp->rq_vers);
58 * Common host lookup routine for server & client
61 nlm_lookup_host(int server, struct sockaddr_in *sin,
62 int proto, int version)
64 struct nlm_host *host, **hp;
68 dprintk("lockd: nlm_lookup_host(%08x, p=%d, v=%d)\n",
69 (unsigned)(sin? ntohl(sin->sin_addr.s_addr) : 0), proto, version);
71 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
76 if (time_after_eq(jiffies, next_gc))
79 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
80 if (host->h_proto != proto)
82 if (host->h_version != version)
84 if (host->h_server != server)
87 if (nlm_cmp_addr(&host->h_addr, sin)) {
88 if (hp != nlm_hosts + hash) {
90 host->h_next = nlm_hosts[hash];
91 nlm_hosts[hash] = host;
99 /* Ooops, no host found, create it */
100 dprintk("lockd: creating host entry\n");
102 if (!(host = (struct nlm_host *) kmalloc(sizeof(*host), GFP_KERNEL)))
104 memset(host, 0, sizeof(*host));
106 addr = sin->sin_addr.s_addr;
107 sprintf(host->h_name, "%u.%u.%u.%u", NIPQUAD(addr));
110 host->h_addr.sin_port = 0; /* ouch! */
111 host->h_version = version;
112 host->h_proto = proto;
113 host->h_rpcclnt = NULL;
114 init_MUTEX(&host->h_sema);
115 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
116 host->h_expires = jiffies + NLM_HOST_EXPIRE;
117 atomic_set(&host->h_count, 1);
118 init_waitqueue_head(&host->h_gracewait);
119 host->h_state = 0; /* pseudo NSM state */
120 host->h_nsmstate = 0; /* real NSM state */
121 host->h_server = server;
122 host->h_next = nlm_hosts[hash];
123 nlm_hosts[hash] = host;
124 INIT_LIST_HEAD(&host->h_lockowners);
125 spin_lock_init(&host->h_lock);
126 INIT_LIST_HEAD(&host->h_granted);
127 INIT_LIST_HEAD(&host->h_reclaim);
129 if (++nrhosts > NLM_HOST_MAX)
138 nlm_find_client(void)
140 /* find a nlm_host for a client for which h_killed == 0.
144 down(&nlm_host_sema);
145 for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
146 struct nlm_host *host, **hp;
147 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
148 if (host->h_server &&
149 host->h_killed == 0) {
162 * Create the NLM RPC client for an NLM peer
165 nlm_bind_host(struct nlm_host *host)
167 struct rpc_clnt *clnt;
168 struct rpc_xprt *xprt;
170 dprintk("lockd: nlm_bind_host(%08x)\n",
171 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
173 /* Lock host handle */
176 /* If we've already created an RPC client, check whether
177 * RPC rebind is required
179 if ((clnt = host->h_rpcclnt) != NULL) {
180 xprt = clnt->cl_xprt;
181 if (time_after_eq(jiffies, host->h_nextrebind)) {
182 rpc_force_rebind(clnt);
183 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
184 dprintk("lockd: next rebind in %ld jiffies\n",
185 host->h_nextrebind - jiffies);
188 xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
192 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
193 xprt->resvport = 1; /* NLM requires a reserved port */
195 /* Existing NLM servers accept AUTH_UNIX only */
196 clnt = rpc_new_client(xprt, host->h_name, &nlm_program,
197 host->h_version, RPC_AUTH_UNIX);
200 clnt->cl_autobind = 1; /* turn on pmap queries */
201 clnt->cl_softrtry = 1; /* All queries are soft */
203 host->h_rpcclnt = clnt;
210 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
216 * Force a portmap lookup of the remote lockd port
219 nlm_rebind_host(struct nlm_host *host)
221 dprintk("lockd: rebind host %s\n", host->h_name);
222 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
223 rpc_force_rebind(host->h_rpcclnt);
224 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
229 * Increment NLM host count
231 struct nlm_host * nlm_get_host(struct nlm_host *host)
234 dprintk("lockd: get host %s\n", host->h_name);
235 atomic_inc(&host->h_count);
236 host->h_expires = jiffies + NLM_HOST_EXPIRE;
242 * Release NLM host after use
244 void nlm_release_host(struct nlm_host *host)
247 dprintk("lockd: release host %s\n", host->h_name);
248 BUG_ON(atomic_read(&host->h_count) < 0);
249 if (atomic_dec_and_test(&host->h_count)) {
250 BUG_ON(!list_empty(&host->h_lockowners));
251 BUG_ON(!list_empty(&host->h_granted));
252 BUG_ON(!list_empty(&host->h_reclaim));
258 * Shut down the hosts module.
259 * Note that this routine is called only at server shutdown time.
262 nlm_shutdown_hosts(void)
264 struct nlm_host *host;
267 dprintk("lockd: shutting down host module\n");
268 down(&nlm_host_sema);
270 /* First, make all hosts eligible for gc */
271 dprintk("lockd: nuking all hosts...\n");
272 for (i = 0; i < NLM_HOST_NRHASH; i++) {
273 for (host = nlm_hosts[i]; host; host = host->h_next)
274 host->h_expires = jiffies - 1;
277 /* Then, perform a garbage collection pass */
281 /* complain if any hosts are left */
283 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
284 dprintk("lockd: %d hosts left:\n", nrhosts);
285 for (i = 0; i < NLM_HOST_NRHASH; i++) {
286 for (host = nlm_hosts[i]; host; host = host->h_next) {
287 dprintk(" %s (cnt %d use %d exp %ld)\n",
288 host->h_name, atomic_read(&host->h_count),
289 host->h_inuse, host->h_expires);
296 * Garbage collect any unused NLM hosts.
297 * This GC combines reference counting for async operations with
298 * mark & sweep for resources held by remote clients.
303 struct nlm_host **q, *host;
304 struct rpc_clnt *clnt;
307 dprintk("lockd: host garbage collection\n");
308 for (i = 0; i < NLM_HOST_NRHASH; i++) {
309 for (host = nlm_hosts[i]; host; host = host->h_next)
313 /* Mark all hosts that hold locks, blocks or shares */
314 nlmsvc_mark_resources();
316 for (i = 0; i < NLM_HOST_NRHASH; i++) {
318 while ((host = *q) != NULL) {
319 if (atomic_read(&host->h_count) || host->h_inuse
320 || time_before(jiffies, host->h_expires)) {
321 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
322 host->h_name, atomic_read(&host->h_count),
323 host->h_inuse, host->h_expires);
327 dprintk("lockd: delete host %s\n", host->h_name);
329 /* Don't unmonitor hosts that have been invalidated */
330 if (host->h_monitored && !host->h_killed)
332 if ((clnt = host->h_rpcclnt) != NULL) {
333 if (atomic_read(&clnt->cl_users)) {
335 "lockd: active RPC handle\n");
338 rpc_destroy_client(host->h_rpcclnt);
346 next_gc = jiffies + NLM_HOST_COLLECT;