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/malloc.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
, 0, 0);
58 * Match the given host against client/address
61 nlm_match_host(struct nlm_host
*host
, struct svc_client
*clnt
,
62 struct sockaddr_in
*sin
)
65 return host
->h_exportent
== clnt
;
66 return nlm_cmp_addr(&host
->h_addr
, sin
);
70 * Common host lookup routine for server & client
73 nlm_lookup_host(struct svc_client
*clnt
, struct sockaddr_in
*sin
,
74 int proto
, int version
)
76 struct nlm_host
*host
, **hp
;
81 printk(KERN_NOTICE
"lockd: no clnt or addr in lookup_host!\n");
85 dprintk("lockd: nlm_lookup_host(%08x, p=%d, v=%d)\n",
86 (unsigned)(sin
? ntohl(sin
->sin_addr
.s_addr
) : 0), proto
, version
);
89 hash
= NLM_PTRHASH(clnt
);
91 hash
= NLM_ADDRHASH(sin
->sin_addr
.s_addr
);
96 if (time_after_eq(jiffies
, next_gc
))
99 for (hp
= &nlm_hosts
[hash
]; (host
= *hp
); hp
= &host
->h_next
) {
100 if (host
->h_version
!= version
|| host
->h_proto
!= proto
)
103 if (nlm_match_host(host
, clnt
, sin
)) {
104 if (hp
!= nlm_hosts
+ hash
) {
106 host
->h_next
= nlm_hosts
[hash
];
107 nlm_hosts
[hash
] = host
;
115 /* special hack for nlmsvc_invalidate_client */
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
)))
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));
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
;
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
)
160 * Create the NLM RPC client for an NLM peer
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 */
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
)) {
182 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
183 dprintk("lockd: next rebind in %ld jiffies\n",
184 host
->h_nextrebind
- jiffies
);
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 */
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
;
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
);
207 clnt
->cl_autobind
= 1; /* turn on pmap queries */
208 xprt
->nocong
= 1; /* No congestion control for NLM */
210 host
->h_rpcclnt
= clnt
;
217 printk("lockd: couldn't create RPC handle for %s\n", host
->h_name
);
223 * Force a portmap lookup of the remote lockd port
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
)
241 dprintk("lockd: get host %s\n", host
->h_name
);
243 host
->h_expires
= jiffies
+ NLM_HOST_EXPIRE
;
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
);
260 * Shut down the hosts module.
261 * Note that this routine is called only at server shutdown time.
264 nlm_shutdown_hosts(void)
266 struct nlm_host
*host
;
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
)
279 /* Then, perform a garbage collection pass */
283 /* complain if any hosts are left */
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.
305 struct nlm_host
**q
, *host
;
306 struct rpc_clnt
*clnt
;
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
)
315 /* Mark all hosts that hold locks, blocks or shares */
316 nlmsvc_mark_resources();
318 for (i
= 0; i
< NLM_HOST_NRHASH
; i
++) {
320 while ((host
= *q
) != NULL
) {
321 if (host
->h_count
|| host
->h_inuse
322 || time_before(jiffies
, host
->h_expires
)) {
326 dprintk("lockd: delete host %s\n", host
->h_name
);
328 if (host
->h_monitored
)
330 if ((clnt
= host
->h_rpcclnt
) != NULL
) {
331 if (atomic_read(&clnt
->cl_users
)) {
333 "lockd: active RPC handle\n");
336 rpc_destroy_client(host
->h_rpcclnt
);
344 next_gc
= jiffies
+ NLM_HOST_COLLECT
;