MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / lockd / host.c
blobfc4f414a8c5fbf448ad813606bcca6a49bc7495e
1 /*
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>
9 */
11 #include <linux/types.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/in.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;
32 static int nrhosts;
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.
41 struct nlm_host *
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.
50 struct nlm_host *
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
60 struct nlm_host *
61 nlm_lookup_host(int server, struct sockaddr_in *sin,
62 int proto, int version)
64 struct nlm_host *host, **hp;
65 u32 addr;
66 int hash;
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);
73 /* Lock hash table */
74 down(&nlm_host_sema);
76 if (time_after_eq(jiffies, next_gc))
77 nlm_gc_hosts();
79 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
80 if (host->h_proto != proto)
81 continue;
82 if (host->h_version != version)
83 continue;
84 if (host->h_server != server)
85 continue;
87 if (nlm_cmp_addr(&host->h_addr, sin)) {
88 if (hp != nlm_hosts + hash) {
89 *hp = host->h_next;
90 host->h_next = nlm_hosts[hash];
91 nlm_hosts[hash] = host;
93 nlm_get_host(host);
94 up(&nlm_host_sema);
95 return 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)))
103 goto nohost;
104 memset(host, 0, sizeof(*host));
106 addr = sin->sin_addr.s_addr;
107 sprintf(host->h_name, "%d.%d.%d.%d",
108 (unsigned char) (ntohl(addr) >> 24),
109 (unsigned char) (ntohl(addr) >> 16),
110 (unsigned char) (ntohl(addr) >> 8),
111 (unsigned char) (ntohl(addr) >> 0));
113 host->h_addr = *sin;
114 host->h_addr.sin_port = 0; /* ouch! */
115 host->h_version = version;
116 host->h_proto = proto;
117 host->h_authflavor = RPC_AUTH_UNIX;
118 host->h_rpcclnt = NULL;
119 init_MUTEX(&host->h_sema);
120 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
121 host->h_expires = jiffies + NLM_HOST_EXPIRE;
122 atomic_set(&host->h_count, 1);
123 init_waitqueue_head(&host->h_gracewait);
124 host->h_state = 0; /* pseudo NSM state */
125 host->h_nsmstate = 0; /* real NSM state */
126 host->h_server = server;
127 host->h_next = nlm_hosts[hash];
128 nlm_hosts[hash] = host;
129 INIT_LIST_HEAD(&host->h_lockowners);
130 spin_lock_init(&host->h_lock);
132 if (++nrhosts > NLM_HOST_MAX)
133 next_gc = 0;
135 nohost:
136 up(&nlm_host_sema);
137 return host;
140 struct nlm_host *
141 nlm_find_client(void)
143 /* find a nlm_host for a client for which h_killed == 0.
144 * and return it
146 int hash;
147 down(&nlm_host_sema);
148 for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
149 struct nlm_host *host, **hp;
150 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
151 if (host->h_server &&
152 host->h_killed == 0) {
153 nlm_get_host(host);
154 up(&nlm_host_sema);
155 return host;
159 up(&nlm_host_sema);
160 return NULL;
165 * Create the NLM RPC client for an NLM peer
167 struct rpc_clnt *
168 nlm_bind_host(struct nlm_host *host)
170 struct rpc_clnt *clnt;
171 struct rpc_xprt *xprt;
173 dprintk("lockd: nlm_bind_host(%08x)\n",
174 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
176 /* Lock host handle */
177 down(&host->h_sema);
179 /* If we've already created an RPC client, check whether
180 * RPC rebind is required
181 * Note: why keep rebinding if we're on a tcp connection?
183 if ((clnt = host->h_rpcclnt) != NULL) {
184 xprt = clnt->cl_xprt;
185 if (!xprt->stream && time_after_eq(jiffies, host->h_nextrebind)) {
186 clnt->cl_port = 0;
187 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
188 dprintk("lockd: next rebind in %ld jiffies\n",
189 host->h_nextrebind - jiffies);
191 } else {
192 xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
193 if (IS_ERR(xprt))
194 goto forgetit;
196 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
198 clnt = rpc_create_client(xprt, host->h_name, &nlm_program,
199 host->h_version, host->h_authflavor);
200 if (IS_ERR(clnt)) {
201 xprt_destroy(xprt);
202 goto forgetit;
204 clnt->cl_autobind = 1; /* turn on pmap queries */
205 xprt->nocong = 1; /* No congestion control for NLM */
206 xprt->resvport = 1; /* NLM requires a reserved port */
208 host->h_rpcclnt = clnt;
211 up(&host->h_sema);
212 return clnt;
214 forgetit:
215 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
216 up(&host->h_sema);
217 return NULL;
221 * Force a portmap lookup of the remote lockd port
223 void
224 nlm_rebind_host(struct nlm_host *host)
226 dprintk("lockd: rebind host %s\n", host->h_name);
227 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
228 host->h_rpcclnt->cl_port = 0;
229 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
234 * Increment NLM host count
236 struct nlm_host * nlm_get_host(struct nlm_host *host)
238 if (host) {
239 dprintk("lockd: get host %s\n", host->h_name);
240 atomic_inc(&host->h_count);
241 host->h_expires = jiffies + NLM_HOST_EXPIRE;
243 return host;
247 * Release NLM host after use
249 void nlm_release_host(struct nlm_host *host)
251 if (host != NULL) {
252 dprintk("lockd: release host %s\n", host->h_name);
253 atomic_dec(&host->h_count);
254 BUG_ON(atomic_read(&host->h_count) < 0);
259 * Shut down the hosts module.
260 * Note that this routine is called only at server shutdown time.
262 void
263 nlm_shutdown_hosts(void)
265 struct nlm_host *host;
266 int i;
268 dprintk("lockd: shutting down host module\n");
269 down(&nlm_host_sema);
271 /* First, make all hosts eligible for gc */
272 dprintk("lockd: nuking all hosts...\n");
273 for (i = 0; i < NLM_HOST_NRHASH; i++) {
274 for (host = nlm_hosts[i]; host; host = host->h_next)
275 host->h_expires = jiffies - 1;
278 /* Then, perform a garbage collection pass */
279 nlm_gc_hosts();
280 up(&nlm_host_sema);
282 /* complain if any hosts are left */
283 if (nrhosts) {
284 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
285 dprintk("lockd: %d hosts left:\n", nrhosts);
286 for (i = 0; i < NLM_HOST_NRHASH; i++) {
287 for (host = nlm_hosts[i]; host; host = host->h_next) {
288 dprintk(" %s (cnt %d use %d exp %ld)\n",
289 host->h_name, atomic_read(&host->h_count),
290 host->h_inuse, host->h_expires);
297 * Garbage collect any unused NLM hosts.
298 * This GC combines reference counting for async operations with
299 * mark & sweep for resources held by remote clients.
301 static void
302 nlm_gc_hosts(void)
304 struct nlm_host **q, *host;
305 struct rpc_clnt *clnt;
306 int i;
308 dprintk("lockd: host garbage collection\n");
309 for (i = 0; i < NLM_HOST_NRHASH; i++) {
310 for (host = nlm_hosts[i]; host; host = host->h_next)
311 host->h_inuse = 0;
314 /* Mark all hosts that hold locks, blocks or shares */
315 nlmsvc_mark_resources();
317 for (i = 0; i < NLM_HOST_NRHASH; i++) {
318 q = &nlm_hosts[i];
319 while ((host = *q) != NULL) {
320 if (atomic_read(&host->h_count) || host->h_inuse
321 || time_before(jiffies, host->h_expires)) {
322 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
323 host->h_name, atomic_read(&host->h_count),
324 host->h_inuse, host->h_expires);
325 q = &host->h_next;
326 continue;
328 dprintk("lockd: delete host %s\n", host->h_name);
329 *q = host->h_next;
330 /* Don't unmonitor hosts that have been invalidated */
331 if (host->h_monitored && !host->h_killed)
332 nsm_unmonitor(host);
333 if ((clnt = host->h_rpcclnt) != NULL) {
334 if (atomic_read(&clnt->cl_users)) {
335 printk(KERN_WARNING
336 "lockd: active RPC handle\n");
337 clnt->cl_dead = 1;
338 } else {
339 rpc_destroy_client(host->h_rpcclnt);
342 BUG_ON(!list_empty(&host->h_lockowners));
343 kfree(host);
344 nrhosts--;
348 next_gc = jiffies + NLM_HOST_COLLECT;