import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / fs / lockd / host.c
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
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>
19
20
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)
30
31 static struct nlm_host *        nlm_hosts[NLM_HOST_NRHASH];
32 static unsigned long            next_gc;
33 static int                      nrhosts;
34 static DECLARE_MUTEX(nlm_host_sema);
35
36
37 static void                     nlm_gc_hosts(void);
38
39 /*
40  * Find an NLM server handle in the cache. If there is none, create it.
41  */
42 struct nlm_host *
43 nlmclnt_lookup_host(struct sockaddr_in *sin, int proto, int version)
44 {
45         return nlm_lookup_host(NULL, sin, proto, version);
46 }
47
48 /*
49  * Find an NLM client handle in the cache. If there is none, create it.
50  */
51 struct nlm_host *
52 nlmsvc_lookup_host(struct svc_rqst *rqstp)
53 {
54         return nlm_lookup_host(rqstp->rq_client, &rqstp->rq_addr,
55                                rqstp->rq_prot, rqstp->rq_vers);
56 }
57
58 /*
59  * Match the given host against client/address
60  */
61 static inline int
62 nlm_match_host(struct nlm_host *host, struct svc_client *clnt,
63                                         struct sockaddr_in *sin)
64 {
65         if (clnt)
66                 return host->h_exportent == clnt;
67         return nlm_cmp_addr(&host->h_addr, sin);
68 }
69
70 /*
71  * Common host lookup routine for server & client
72  */
73 struct nlm_host *
74 nlm_lookup_host(struct svc_client *clnt, struct sockaddr_in *sin,
75                                         int proto, int version)
76 {
77         struct nlm_host *host, **hp;
78         u32             addr;
79         int             hash;
80
81         if (!clnt && !sin) {
82                 printk(KERN_NOTICE "lockd: no clnt or addr in lookup_host!\n");
83                 return NULL;
84         }
85
86         dprintk("lockd: nlm_lookup_host(%08x, p=%d, v=%d)\n",
87                         (unsigned)(sin? ntohl(sin->sin_addr.s_addr) : 0), proto, version);
88
89         if (clnt)
90                 hash = NLM_PTRHASH(clnt);
91         else
92                 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
93
94         /* Lock hash table */
95         down(&nlm_host_sema);
96
97         if (time_after_eq(jiffies, next_gc))
98                 nlm_gc_hosts();
99
100         for (hp = &nlm_hosts[hash]; (host = *hp); hp = &host->h_next) {
101                 if (proto && host->h_proto != proto)
102                         continue;
103                 if (version && host->h_version != version)
104                         continue;
105
106                 if (nlm_match_host(host, clnt, sin)) {
107                         if (hp != nlm_hosts + hash) {
108                                 *hp = host->h_next;
109                                 host->h_next = nlm_hosts[hash];
110                                 nlm_hosts[hash] = host;
111                         }
112                         nlm_get_host(host);
113                         up(&nlm_host_sema);
114                         return host;
115                 }
116         }
117
118         /* special hack for nlmsvc_invalidate_client */
119         if (sin == NULL)
120                 goto nohost;
121
122         /* Ooops, no host found, create it */
123         dprintk("lockd: creating host entry\n");
124
125         if (!(host = (struct nlm_host *) kmalloc(sizeof(*host), GFP_KERNEL)))
126                 goto nohost;
127         memset(host, 0, sizeof(*host));
128
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));
135
136         host->h_addr       = *sin;
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;
145         host->h_count      = 1;
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;
150
151         host->h_next       = nlm_hosts[hash];
152         nlm_hosts[hash]    = host;
153
154         if (++nrhosts > NLM_HOST_MAX)
155                 next_gc = 0;
156
157 nohost:
158         up(&nlm_host_sema);
159         return host;
160 }
161
162 /*
163  * Create the NLM RPC client for an NLM peer
164  */
165 struct rpc_clnt *
166 nlm_bind_host(struct nlm_host *host)
167 {
168         struct rpc_clnt *clnt;
169         struct rpc_xprt *xprt;
170
171         dprintk("lockd: nlm_bind_host(%08x)\n",
172                         (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
173
174         /* Lock host handle */
175         down(&host->h_sema);
176
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?
180          */
181         if ((clnt = host->h_rpcclnt) != NULL) {
182                 xprt = clnt->cl_xprt;
183                 if (!xprt->stream && time_after_eq(jiffies, host->h_nextrebind)) {
184                         clnt->cl_port = 0;
185                         host->h_nextrebind = jiffies + NLM_HOST_REBIND;
186                         dprintk("lockd: next rebind in %ld jiffies\n",
187                                         host->h_nextrebind - jiffies);
188                 }
189         } else {
190                 uid_t saved_fsuid = current->fsuid;
191                 kernel_cap_t saved_cap = current->cap_effective;
192
193                 /* Create RPC socket as root user so we get a priv port */
194                 current->fsuid = 0;
195                 cap_raise (current->cap_effective, CAP_NET_BIND_SERVICE);
196                 xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
197                 current->fsuid = saved_fsuid;
198                 current->cap_effective = saved_cap;
199                 if (xprt == NULL)
200                         goto forgetit;
201
202                 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
203
204                 clnt = rpc_create_client(xprt, host->h_name, &nlm_program,
205                                         host->h_version, host->h_authflavor);
206                 if (clnt == NULL) {
207                         xprt_destroy(xprt);
208                         goto forgetit;
209                 }
210                 clnt->cl_autobind = 1;  /* turn on pmap queries */
211                 xprt->nocong = 1;       /* No congestion control for NLM */
212
213                 host->h_rpcclnt = clnt;
214         }
215
216         up(&host->h_sema);
217         return clnt;
218
219 forgetit:
220         printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
221         up(&host->h_sema);
222         return NULL;
223 }
224
225 /*
226  * Force a portmap lookup of the remote lockd port
227  */
228 void
229 nlm_rebind_host(struct nlm_host *host)
230 {
231         dprintk("lockd: rebind host %s\n", host->h_name);
232         if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
233                 host->h_rpcclnt->cl_port = 0;
234                 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
235         }
236 }
237
238 /*
239  * Increment NLM host count
240  */
241 struct nlm_host * nlm_get_host(struct nlm_host *host)
242 {
243         if (host) {
244                 dprintk("lockd: get host %s\n", host->h_name);
245                 host->h_count ++;
246                 host->h_expires = jiffies + NLM_HOST_EXPIRE;
247         }
248         return host;
249 }
250
251 /*
252  * Release NLM host after use
253  */
254 void nlm_release_host(struct nlm_host *host)
255 {
256         if (host && host->h_count) {
257                 dprintk("lockd: release host %s\n", host->h_name);
258                 host->h_count --;
259         }
260 }
261
262 /*
263  * Shut down the hosts module.
264  * Note that this routine is called only at server shutdown time.
265  */
266 void
267 nlm_shutdown_hosts(void)
268 {
269         struct nlm_host *host;
270         int             i;
271
272         dprintk("lockd: shutting down host module\n");
273         down(&nlm_host_sema);
274
275         /* First, make all hosts eligible for gc */
276         dprintk("lockd: nuking all hosts...\n");
277         for (i = 0; i < NLM_HOST_NRHASH; i++) {
278                 for (host = nlm_hosts[i]; host; host = host->h_next)
279                         host->h_expires = 0;
280         }
281
282         /* Then, perform a garbage collection pass */
283         nlm_gc_hosts();
284         up(&nlm_host_sema);
285
286         /* complain if any hosts are left */
287         if (nrhosts) {
288                 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
289                 dprintk("lockd: %d hosts left:\n", nrhosts);
290                 for (i = 0; i < NLM_HOST_NRHASH; i++) {
291                         for (host = nlm_hosts[i]; host; host = host->h_next) {
292                                 dprintk("       %s (cnt %d use %d exp %ld)\n",
293                                         host->h_name, host->h_count,
294                                         host->h_inuse, host->h_expires);
295                         }
296                 }
297         }
298 }
299
300 /*
301  * Garbage collect any unused NLM hosts.
302  * This GC combines reference counting for async operations with
303  * mark & sweep for resources held by remote clients.
304  */
305 static void
306 nlm_gc_hosts(void)
307 {
308         struct nlm_host **q, *host;
309         struct rpc_clnt *clnt;
310         int             i;
311
312         dprintk("lockd: host garbage collection\n");
313         for (i = 0; i < NLM_HOST_NRHASH; i++) {
314                 for (host = nlm_hosts[i]; host; host = host->h_next)
315                         host->h_inuse = 0;
316         }
317
318         /* Mark all hosts that hold locks, blocks or shares */
319         nlmsvc_mark_resources();
320
321         for (i = 0; i < NLM_HOST_NRHASH; i++) {
322                 q = &nlm_hosts[i];
323                 while ((host = *q) != NULL) {
324                         if (host->h_count || host->h_inuse
325                          || time_before(jiffies, host->h_expires)) {
326                                 q = &host->h_next;
327                                 continue;
328                         }
329                         dprintk("lockd: delete host %s\n", host->h_name);
330                         *q = host->h_next;
331                         /* Don't unmonitor hosts that have been invalidated */
332                         if (host->h_monitored && !host->h_killed)
333                                 nsm_unmonitor(host);
334                         if ((clnt = host->h_rpcclnt) != NULL) {
335                                 if (atomic_read(&clnt->cl_users)) {
336                                         printk(KERN_WARNING
337                                                 "lockd: active RPC handle\n");
338                                         clnt->cl_dead = 1;
339                                 } else {
340                                         rpc_destroy_client(host->h_rpcclnt);
341                                 }
342                         }
343                         kfree(host);
344                         nrhosts--;
345                 }
346         }
347
348         next_gc = jiffies + NLM_HOST_COLLECT;
349 }
350