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_PTRHASH(ptr) ((((u32)(unsigned long) ptr) / 32) & (NLM_HOST_NRHASH-1))
26 #define NLM_HOST_REBIND (60 * HZ)
27 #define NLM_HOST_EXPIRE ((nrhosts > NLM_HOST_MAX)? 300 * HZ : 120 * HZ)
28 #define NLM_HOST_COLLECT ((nrhosts > NLM_HOST_MAX)? 120 * HZ : 60 * HZ)
29 #define NLM_HOST_ADDR(sv) (&(sv)->s_nlmclnt->cl_xprt->addr)
31 static struct nlm_host * nlm_hosts[NLM_HOST_NRHASH];
32 static unsigned long next_gc;
34 static DECLARE_MUTEX(nlm_host_sema);
37 static void nlm_gc_hosts(void);
40 * Find an NLM server handle in the cache. If there is none, create it.
43 nlmclnt_lookup_host(struct sockaddr_in *sin, int proto, int version)
45 return nlm_lookup_host(NULL, sin, proto, version);
49 * Find an NLM client handle in the cache. If there is none, create it.
52 nlmsvc_lookup_host(struct svc_rqst *rqstp)
54 return nlm_lookup_host(rqstp->rq_client, &rqstp->rq_addr,
55 rqstp->rq_prot, rqstp->rq_vers);
59 * Match the given host against client/address
62 nlm_match_host(struct nlm_host *host, struct svc_client *clnt,
63 struct sockaddr_in *sin)
66 return host->h_exportent == clnt;
67 return nlm_cmp_addr(&host->h_addr, sin);
71 * Common host lookup routine for server & client
74 nlm_lookup_host(struct svc_client *clnt, struct sockaddr_in *sin,
75 int proto, int version)
77 struct nlm_host *host, **hp;
82 printk(KERN_NOTICE "lockd: no clnt or addr in lookup_host!\n");
86 dprintk("lockd: nlm_lookup_host(%08x, p=%d, v=%d)\n",
87 (unsigned)(sin? ntohl(sin->sin_addr.s_addr) : 0), proto, version);
90 hash = NLM_PTRHASH(clnt);
92 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
97 if (time_after_eq(jiffies, next_gc))
100 for (hp = &nlm_hosts[hash]; (host = *hp); hp = &host->h_next) {
101 if (proto && host->h_proto != proto)
103 if (version && host->h_version != version)
106 if (nlm_match_host(host, clnt, sin)) {
107 if (hp != nlm_hosts + hash) {
109 host->h_next = nlm_hosts[hash];
110 nlm_hosts[hash] = host;
118 /* special hack for nlmsvc_invalidate_client */
122 /* Ooops, no host found, create it */
123 dprintk("lockd: creating host entry\n");
125 if (!(host = (struct nlm_host *) kmalloc(sizeof(*host), GFP_KERNEL)))
127 memset(host, 0, sizeof(*host));
129 addr = sin->sin_addr.s_addr;
130 sprintf(host->h_name, "%d.%d.%d.%d",
131 (unsigned char) (ntohl(addr) >> 24),
132 (unsigned char) (ntohl(addr) >> 16),
133 (unsigned char) (ntohl(addr) >> 8),
134 (unsigned char) (ntohl(addr) >> 0));
137 host->h_addr.sin_port = 0; /* ouch! */
138 host->h_version = version;
139 host->h_proto = proto;
140 host->h_authflavor = RPC_AUTH_UNIX;
141 host->h_rpcclnt = NULL;
142 init_MUTEX(&host->h_sema);
143 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
144 host->h_expires = jiffies + NLM_HOST_EXPIRE;
146 init_waitqueue_head(&host->h_gracewait);
147 host->h_state = 0; /* pseudo NSM state */
148 host->h_nsmstate = 0; /* real NSM state */
149 host->h_exportent = clnt;
151 host->h_next = nlm_hosts[hash];
152 nlm_hosts[hash] = host;
154 if (++nrhosts > NLM_HOST_MAX)
163 * Create the NLM RPC client for an NLM peer
166 nlm_bind_host(struct nlm_host *host)
168 struct rpc_clnt *clnt;
169 struct rpc_xprt *xprt;
171 dprintk("lockd: nlm_bind_host(%08x)\n",
172 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
174 /* Lock host handle */
177 /* If we've already created an RPC client, check whether
178 * RPC rebind is required
179 * Note: why keep rebinding if we're on a tcp connection?
181 if ((clnt = host->h_rpcclnt) != NULL) {
182 xprt = clnt->cl_xprt;
183 if (!xprt->stream && time_after_eq(jiffies, host->h_nextrebind)) {
185 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
186 dprintk("lockd: next rebind in %ld jiffies\n",
187 host->h_nextrebind - jiffies);
190 xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
194 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
196 clnt = rpc_create_client(xprt, host->h_name, &nlm_program,
197 host->h_version, host->h_authflavor);
202 clnt->cl_autobind = 1; /* turn on pmap queries */
203 xprt->nocong = 1; /* No congestion control for NLM */
204 xprt->resvport = 1; /* NLM requires a reserved port */
206 host->h_rpcclnt = clnt;
213 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
219 * Force a portmap lookup of the remote lockd port
222 nlm_rebind_host(struct nlm_host *host)
224 dprintk("lockd: rebind host %s\n", host->h_name);
225 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
226 host->h_rpcclnt->cl_port = 0;
227 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
232 * Increment NLM host count
234 struct nlm_host * nlm_get_host(struct nlm_host *host)
237 dprintk("lockd: get host %s\n", host->h_name);
239 host->h_expires = jiffies + NLM_HOST_EXPIRE;
245 * Release NLM host after use
247 void nlm_release_host(struct nlm_host *host)
249 if (host && host->h_count) {
250 dprintk("lockd: release host %s\n", host->h_name);
256 * Shut down the hosts module.
257 * Note that this routine is called only at server shutdown time.
260 nlm_shutdown_hosts(void)
262 struct nlm_host *host;
265 dprintk("lockd: shutting down host module\n");
266 down(&nlm_host_sema);
268 /* First, make all hosts eligible for gc */
269 dprintk("lockd: nuking all hosts...\n");
270 for (i = 0; i < NLM_HOST_NRHASH; i++) {
271 for (host = nlm_hosts[i]; host; host = host->h_next)
275 /* Then, perform a garbage collection pass */
279 /* complain if any hosts are left */
281 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
282 dprintk("lockd: %d hosts left:\n", nrhosts);
283 for (i = 0; i < NLM_HOST_NRHASH; i++) {
284 for (host = nlm_hosts[i]; host; host = host->h_next) {
285 dprintk(" %s (cnt %d use %d exp %ld)\n",
286 host->h_name, host->h_count,
287 host->h_inuse, host->h_expires);
294 * Garbage collect any unused NLM hosts.
295 * This GC combines reference counting for async operations with
296 * mark & sweep for resources held by remote clients.
301 struct nlm_host **q, *host;
302 struct rpc_clnt *clnt;
305 dprintk("lockd: host garbage collection\n");
306 for (i = 0; i < NLM_HOST_NRHASH; i++) {
307 for (host = nlm_hosts[i]; host; host = host->h_next)
311 /* Mark all hosts that hold locks, blocks or shares */
312 nlmsvc_mark_resources();
314 for (i = 0; i < NLM_HOST_NRHASH; i++) {
316 while ((host = *q) != NULL) {
317 if (host->h_count || host->h_inuse
318 || time_before(jiffies, host->h_expires)) {
322 dprintk("lockd: delete host %s\n", host->h_name);
324 /* Don't unmonitor hosts that have been invalidated */
325 if (host->h_monitored && !host->h_killed)
327 if ((clnt = host->h_rpcclnt) != NULL) {
328 if (atomic_read(&clnt->cl_users)) {
330 "lockd: active RPC handle\n");
333 rpc_destroy_client(host->h_rpcclnt);
341 next_gc = jiffies + NLM_HOST_COLLECT;