Import 2.4.0-test2pre7
[davej-history.git] / fs / lockd / host.c
bloba18c2d109fdf261e2ebc87e48a28dbb8a27373bb
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/malloc.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_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 = 0;
33 static int nrhosts = 0;
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.
42 struct nlm_host *
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.
51 struct nlm_host *
52 nlmsvc_lookup_host(struct svc_rqst *rqstp)
54 return nlm_lookup_host(rqstp->rq_client, &rqstp->rq_addr, 0, 0);
58 * Match the given host against client/address
60 static inline int
61 nlm_match_host(struct nlm_host *host, struct svc_client *clnt,
62 struct sockaddr_in *sin)
64 if (clnt)
65 return host->h_exportent == clnt;
66 return nlm_cmp_addr(&host->h_addr, sin);
70 * Common host lookup routine for server & client
72 struct nlm_host *
73 nlm_lookup_host(struct svc_client *clnt, struct sockaddr_in *sin,
74 int proto, int version)
76 struct nlm_host *host, **hp;
77 u32 addr;
78 int hash;
80 if (!clnt && !sin) {
81 printk(KERN_NOTICE "lockd: no clnt or addr in lookup_host!\n");
82 return NULL;
85 dprintk("lockd: nlm_lookup_host(%08x, p=%d, v=%d)\n",
86 (unsigned)(sin? ntohl(sin->sin_addr.s_addr) : 0), proto, version);
88 if (clnt)
89 hash = NLM_PTRHASH(clnt);
90 else
91 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
93 /* Lock hash table */
94 down(&nlm_host_sema);
96 if (time_after_eq(jiffies, next_gc))
97 nlm_gc_hosts();
99 for (hp = &nlm_hosts[hash]; (host = *hp); hp = &host->h_next) {
100 if (host->h_version != version || host->h_proto != proto)
101 continue;
103 if (nlm_match_host(host, clnt, sin)) {
104 if (hp != nlm_hosts + hash) {
105 *hp = host->h_next;
106 host->h_next = nlm_hosts[hash];
107 nlm_hosts[hash] = host;
109 nlm_get_host(host);
110 up(&nlm_host_sema);
111 return host;
115 /* special hack for nlmsvc_invalidate_client */
116 if (sin == NULL)
117 goto nohost;
119 /* Ooops, no host found, create it */
120 dprintk("lockd: creating host entry\n");
122 if (!(host = (struct nlm_host *) kmalloc(sizeof(*host), GFP_KERNEL)))
123 goto nohost;
124 memset(host, 0, sizeof(*host));
126 addr = sin->sin_addr.s_addr;
127 sprintf(host->h_name, "%d.%d.%d.%d",
128 (unsigned char) (ntohl(addr) >> 24),
129 (unsigned char) (ntohl(addr) >> 16),
130 (unsigned char) (ntohl(addr) >> 8),
131 (unsigned char) (ntohl(addr) >> 0));
133 host->h_addr = *sin;
134 host->h_addr.sin_port = 0; /* ouch! */
135 host->h_version = version;
136 host->h_proto = proto;
137 host->h_authflavor = RPC_AUTH_UNIX;
138 host->h_rpcclnt = NULL;
139 init_MUTEX(&host->h_sema);
140 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
141 host->h_expires = jiffies + NLM_HOST_EXPIRE;
142 host->h_count = 1;
143 init_waitqueue_head(&host->h_gracewait);
144 host->h_state = 0; /* pseudo NSM state */
145 host->h_nsmstate = 0; /* real NSM state */
146 host->h_exportent = clnt;
148 host->h_next = nlm_hosts[hash];
149 nlm_hosts[hash] = host;
151 if (++nrhosts > NLM_HOST_MAX)
152 next_gc = 0;
154 nohost:
155 up(&nlm_host_sema);
156 return host;
160 * Create the NLM RPC client for an NLM peer
162 struct rpc_clnt *
163 nlm_bind_host(struct nlm_host *host)
165 struct rpc_clnt *clnt;
166 struct rpc_xprt *xprt;
168 dprintk("lockd: nlm_bind_host(%08x)\n",
169 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
171 /* Lock host handle */
172 down(&host->h_sema);
174 /* If we've already created an RPC client, check whether
175 * RPC rebind is required
176 * Note: why keep rebinding if we're on a tcp connection?
178 if ((clnt = host->h_rpcclnt) != NULL) {
179 xprt = clnt->cl_xprt;
180 if (!xprt->stream && time_after_eq(jiffies, host->h_nextrebind)) {
181 clnt->cl_port = 0;
182 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
183 dprintk("lockd: next rebind in %ld jiffies\n",
184 host->h_nextrebind - jiffies);
186 } else {
187 uid_t saved_fsuid = current->fsuid;
188 kernel_cap_t saved_cap = current->cap_effective;
190 /* Create RPC socket as root user so we get a priv port */
191 current->fsuid = 0;
192 cap_raise (current->cap_effective, CAP_NET_BIND_SERVICE);
193 xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
194 current->fsuid = saved_fsuid;
195 current->cap_effective = saved_cap;
196 if (xprt == NULL)
197 goto forgetit;
199 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
201 clnt = rpc_create_client(xprt, host->h_name, &nlm_program,
202 host->h_version, host->h_authflavor);
203 if (clnt == NULL) {
204 xprt_destroy(xprt);
205 goto forgetit;
207 clnt->cl_autobind = 1; /* turn on pmap queries */
208 xprt->nocong = 1; /* No congestion control for NLM */
210 host->h_rpcclnt = clnt;
213 up(&host->h_sema);
214 return clnt;
216 forgetit:
217 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
218 up(&host->h_sema);
219 return NULL;
223 * Force a portmap lookup of the remote lockd port
225 void
226 nlm_rebind_host(struct nlm_host *host)
228 dprintk("lockd: rebind host %s\n", host->h_name);
229 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
230 host->h_rpcclnt->cl_port = 0;
231 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
236 * Increment NLM host count
238 struct nlm_host * nlm_get_host(struct nlm_host *host)
240 if (host) {
241 dprintk("lockd: get host %s\n", host->h_name);
242 host->h_count ++;
243 host->h_expires = jiffies + NLM_HOST_EXPIRE;
245 return host;
249 * Release NLM host after use
251 void nlm_release_host(struct nlm_host *host)
253 if (host && host->h_count) {
254 dprintk("lockd: release host %s\n", host->h_name);
255 host->h_count --;
260 * Shut down the hosts module.
261 * Note that this routine is called only at server shutdown time.
263 void
264 nlm_shutdown_hosts(void)
266 struct nlm_host *host;
267 int i;
269 dprintk("lockd: shutting down host module\n");
270 down(&nlm_host_sema);
272 /* First, make all hosts eligible for gc */
273 dprintk("lockd: nuking all hosts...\n");
274 for (i = 0; i < NLM_HOST_NRHASH; i++) {
275 for (host = nlm_hosts[i]; host; host = host->h_next)
276 host->h_expires = 0;
279 /* Then, perform a garbage collection pass */
280 nlm_gc_hosts();
281 up(&nlm_host_sema);
283 /* complain if any hosts are left */
284 if (nrhosts) {
285 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
286 dprintk("lockd: %d hosts left:\n", nrhosts);
287 for (i = 0; i < NLM_HOST_NRHASH; i++) {
288 for (host = nlm_hosts[i]; host; host = host->h_next) {
289 dprintk(" %s (cnt %d use %d exp %ld)\n",
290 host->h_name, host->h_count,
291 host->h_inuse, host->h_expires);
298 * Garbage collect any unused NLM hosts.
299 * This GC combines reference counting for async operations with
300 * mark & sweep for resources held by remote clients.
302 static void
303 nlm_gc_hosts(void)
305 struct nlm_host **q, *host;
306 struct rpc_clnt *clnt;
307 int i;
309 dprintk("lockd: host garbage collection\n");
310 for (i = 0; i < NLM_HOST_NRHASH; i++) {
311 for (host = nlm_hosts[i]; host; host = host->h_next)
312 host->h_inuse = 0;
315 /* Mark all hosts that hold locks, blocks or shares */
316 nlmsvc_mark_resources();
318 for (i = 0; i < NLM_HOST_NRHASH; i++) {
319 q = &nlm_hosts[i];
320 while ((host = *q) != NULL) {
321 if (host->h_count || host->h_inuse
322 || time_before(jiffies, host->h_expires)) {
323 q = &host->h_next;
324 continue;
326 dprintk("lockd: delete host %s\n", host->h_name);
327 *q = host->h_next;
328 if (host->h_monitored)
329 nsm_unmonitor(host);
330 if ((clnt = host->h_rpcclnt) != NULL) {
331 if (atomic_read(&clnt->cl_users)) {
332 printk(KERN_WARNING
333 "lockd: active RPC handle\n");
334 clnt->cl_dead = 1;
335 } else {
336 rpc_destroy_client(host->h_rpcclnt);
339 kfree(host);
340 nrhosts--;
344 next_gc = jiffies + NLM_HOST_COLLECT;