~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/lockd/host.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/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>
 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, 0, 0);
 55 }
 56 
 57 /*
 58  * Match the given host against client/address
 59  */
 60 static inline int
 61 nlm_match_host(struct nlm_host *host, struct svc_client *clnt,
 62                                         struct sockaddr_in *sin)
 63 {
 64         if (clnt)
 65                 return host->h_exportent == clnt;
 66         return nlm_cmp_addr(&host->h_addr, sin);
 67 }
 68 
 69 /*
 70  * Common host lookup routine for server & client
 71  */
 72 struct nlm_host *
 73 nlm_lookup_host(struct svc_client *clnt, struct sockaddr_in *sin,
 74                                         int proto, int version)
 75 {
 76         struct nlm_host *host, **hp;
 77         u32             addr;
 78         int             hash;
 79 
 80         if (!clnt && !sin) {
 81                 printk(KERN_NOTICE "lockd: no clnt or addr in lookup_host!\n");
 82                 return NULL;
 83         }
 84 
 85         dprintk("lockd: nlm_lookup_host(%08x, p=%d, v=%d)\n",
 86                         (unsigned)(sin? ntohl(sin->sin_addr.s_addr) : 0), proto, version);
 87 
 88         if (clnt)
 89                 hash = NLM_PTRHASH(clnt);
 90         else
 91                 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
 92 
 93         /* Lock hash table */
 94         down(&nlm_host_sema);
 95 
 96         if (time_after_eq(jiffies, next_gc))
 97                 nlm_gc_hosts();
 98 
 99         for (hp = &nlm_hosts[hash]; (host = *hp); hp = &host->h_next) {
100                 if (host->h_version != version || host->h_proto != proto)
101                         continue;
102 
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;
108                         }
109                         nlm_get_host(host);
110                         up(&nlm_host_sema);
111                         return host;
112                 }
113         }
114 
115         /* special hack for nlmsvc_invalidate_client */
116         if (sin == NULL)
117                 goto nohost;
118 
119         /* Ooops, no host found, create it */
120         dprintk("lockd: creating host entry\n");
121 
122         if (!(host = (struct nlm_host *) kmalloc(sizeof(*host), GFP_KERNEL)))
123                 goto nohost;
124         memset(host, 0, sizeof(*host));
125 
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));
132 
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;
147 
148         host->h_next       = nlm_hosts[hash];
149         nlm_hosts[hash]    = host;
150 
151         if (++nrhosts > NLM_HOST_MAX)
152                 next_gc = 0;
153 
154 nohost:
155         up(&nlm_host_sema);
156         return host;
157 }
158 
159 /*
160  * Create the NLM RPC client for an NLM peer
161  */
162 struct rpc_clnt *
163 nlm_bind_host(struct nlm_host *host)
164 {
165         struct rpc_clnt *clnt;
166         struct rpc_xprt *xprt;
167 
168         dprintk("lockd: nlm_bind_host(%08x)\n",
169                         (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
170 
171         /* Lock host handle */
172         down(&host->h_sema);
173 
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?
177          */
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);
185                 }
186         } else {
187                 uid_t saved_fsuid = current->fsuid;
188                 kernel_cap_t saved_cap = current->cap_effective;
189 
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;
198 
199                 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
200 
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;
206                 }
207                 clnt->cl_autobind = 1;  /* turn on pmap queries */
208                 xprt->nocong = 1;       /* No congestion control for NLM */
209 
210                 host->h_rpcclnt = clnt;
211         }
212 
213         up(&host->h_sema);
214         return clnt;
215 
216 forgetit:
217         printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
218         up(&host->h_sema);
219         return NULL;
220 }
221 
222 /*
223  * Force a portmap lookup of the remote lockd port
224  */
225 void
226 nlm_rebind_host(struct nlm_host *host)
227 {
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;
232         }
233 }
234 
235 /*
236  * Increment NLM host count
237  */
238 struct nlm_host * nlm_get_host(struct nlm_host *host)
239 {
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;
244         }
245         return host;
246 }
247 
248 /*
249  * Release NLM host after use
250  */
251 void nlm_release_host(struct nlm_host *host)
252 {
253         if (host && host->h_count) {
254                 dprintk("lockd: release host %s\n", host->h_name);
255                 host->h_count --;
256         }
257 }
258 
259 /*
260  * Shut down the hosts module.
261  * Note that this routine is called only at server shutdown time.
262  */
263 void
264 nlm_shutdown_hosts(void)
265 {
266         struct nlm_host *host;
267         int             i;
268 
269         dprintk("lockd: shutting down host module\n");
270         down(&nlm_host_sema);
271 
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;
277         }
278 
279         /* Then, perform a garbage collection pass */
280         nlm_gc_hosts();
281         up(&nlm_host_sema);
282 
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);
292                         }
293                 }
294         }
295 }
296 
297 /*
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.
301  */
302 static void
303 nlm_gc_hosts(void)
304 {
305         struct nlm_host **q, *host;
306         struct rpc_clnt *clnt;
307         int             i;
308 
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;
313         }
314 
315         /* Mark all hosts that hold locks, blocks or shares */
316         nlmsvc_mark_resources();
317 
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;
325                         }
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);
337                                 }
338                         }
339                         kfree(host);
340                         nrhosts--;
341                 }
342         }
343 
344         next_gc = jiffies + NLM_HOST_COLLECT;
345 }
346 
347 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.