blob: 112ebf8b8dfe040c518e28f83b60fb7ff20fbd22 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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_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)
29
30static struct nlm_host * nlm_hosts[NLM_HOST_NRHASH];
31static unsigned long next_gc;
32static int nrhosts;
33static DECLARE_MUTEX(nlm_host_sema);
34
35
36static void nlm_gc_hosts(void);
37
38/*
39 * Find an NLM server handle in the cache. If there is none, create it.
40 */
41struct nlm_host *
42nlmclnt_lookup_host(struct sockaddr_in *sin, int proto, int version)
43{
44 return nlm_lookup_host(0, sin, proto, version);
45}
46
47/*
48 * Find an NLM client handle in the cache. If there is none, create it.
49 */
50struct nlm_host *
51nlmsvc_lookup_host(struct svc_rqst *rqstp)
52{
53 return nlm_lookup_host(1, &rqstp->rq_addr,
54 rqstp->rq_prot, rqstp->rq_vers);
55}
56
57/*
58 * Common host lookup routine for server & client
59 */
60struct nlm_host *
61nlm_lookup_host(int server, struct sockaddr_in *sin,
62 int proto, int version)
63{
64 struct nlm_host *host, **hp;
65 u32 addr;
66 int hash;
67
68 dprintk("lockd: nlm_lookup_host(%08x, p=%d, v=%d)\n",
69 (unsigned)(sin? ntohl(sin->sin_addr.s_addr) : 0), proto, version);
70
71 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
72
73 /* Lock hash table */
74 down(&nlm_host_sema);
75
76 if (time_after_eq(jiffies, next_gc))
77 nlm_gc_hosts();
78
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;
86
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;
92 }
93 nlm_get_host(host);
94 up(&nlm_host_sema);
95 return host;
96 }
97 }
98
99 /* Ooops, no host found, create it */
100 dprintk("lockd: creating host entry\n");
101
102 if (!(host = (struct nlm_host *) kmalloc(sizeof(*host), GFP_KERNEL)))
103 goto nohost;
104 memset(host, 0, sizeof(*host));
105
106 addr = sin->sin_addr.s_addr;
107 sprintf(host->h_name, "%u.%u.%u.%u", NIPQUAD(addr));
108
109 host->h_addr = *sin;
110 host->h_addr.sin_port = 0; /* ouch! */
111 host->h_version = version;
112 host->h_proto = proto;
113 host->h_rpcclnt = NULL;
114 init_MUTEX(&host->h_sema);
115 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
116 host->h_expires = jiffies + NLM_HOST_EXPIRE;
117 atomic_set(&host->h_count, 1);
118 init_waitqueue_head(&host->h_gracewait);
119 host->h_state = 0; /* pseudo NSM state */
120 host->h_nsmstate = 0; /* real NSM state */
121 host->h_server = server;
122 host->h_next = nlm_hosts[hash];
123 nlm_hosts[hash] = host;
124 INIT_LIST_HEAD(&host->h_lockowners);
125 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500126 INIT_LIST_HEAD(&host->h_granted);
127 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 if (++nrhosts > NLM_HOST_MAX)
130 next_gc = 0;
131
132nohost:
133 up(&nlm_host_sema);
134 return host;
135}
136
137struct nlm_host *
138nlm_find_client(void)
139{
140 /* find a nlm_host for a client for which h_killed == 0.
141 * and return it
142 */
143 int hash;
144 down(&nlm_host_sema);
145 for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
146 struct nlm_host *host, **hp;
147 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
148 if (host->h_server &&
149 host->h_killed == 0) {
150 nlm_get_host(host);
151 up(&nlm_host_sema);
152 return host;
153 }
154 }
155 }
156 up(&nlm_host_sema);
157 return NULL;
158}
159
160
161/*
162 * Create the NLM RPC client for an NLM peer
163 */
164struct rpc_clnt *
165nlm_bind_host(struct nlm_host *host)
166{
167 struct rpc_clnt *clnt;
168 struct rpc_xprt *xprt;
169
170 dprintk("lockd: nlm_bind_host(%08x)\n",
171 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
172
173 /* Lock host handle */
174 down(&host->h_sema);
175
176 /* If we've already created an RPC client, check whether
177 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
179 if ((clnt = host->h_rpcclnt) != NULL) {
180 xprt = clnt->cl_xprt;
Chuck Lever43118c22005-08-25 16:25:49 -0700181 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100182 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
184 dprintk("lockd: next rebind in %ld jiffies\n",
185 host->h_nextrebind - jiffies);
186 }
187 } else {
188 xprt = xprt_create_proto(host->h_proto, &host->h_addr, NULL);
189 if (IS_ERR(xprt))
190 goto forgetit;
191
192 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000193 xprt->resvport = 1; /* NLM requires a reserved port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /* Existing NLM servers accept AUTH_UNIX only */
Trond Myklebust04266472006-03-20 13:44:40 -0500196 clnt = rpc_new_client(xprt, host->h_name, &nlm_program,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 host->h_version, RPC_AUTH_UNIX);
Trond Myklebust5b616f52005-06-22 17:16:20 +0000198 if (IS_ERR(clnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto forgetit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 clnt->cl_autobind = 1; /* turn on pmap queries */
Trond Myklebust04266472006-03-20 13:44:40 -0500201 clnt->cl_softrtry = 1; /* All queries are soft */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 host->h_rpcclnt = clnt;
204 }
205
206 up(&host->h_sema);
207 return clnt;
208
209forgetit:
210 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
211 up(&host->h_sema);
212 return NULL;
213}
214
215/*
216 * Force a portmap lookup of the remote lockd port
217 */
218void
219nlm_rebind_host(struct nlm_host *host)
220{
221 dprintk("lockd: rebind host %s\n", host->h_name);
222 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100223 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
225 }
226}
227
228/*
229 * Increment NLM host count
230 */
231struct nlm_host * nlm_get_host(struct nlm_host *host)
232{
233 if (host) {
234 dprintk("lockd: get host %s\n", host->h_name);
235 atomic_inc(&host->h_count);
236 host->h_expires = jiffies + NLM_HOST_EXPIRE;
237 }
238 return host;
239}
240
241/*
242 * Release NLM host after use
243 */
244void nlm_release_host(struct nlm_host *host)
245{
246 if (host != NULL) {
247 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500249 if (atomic_dec_and_test(&host->h_count)) {
250 BUG_ON(!list_empty(&host->h_lockowners));
251 BUG_ON(!list_empty(&host->h_granted));
252 BUG_ON(!list_empty(&host->h_reclaim));
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255}
256
257/*
258 * Shut down the hosts module.
259 * Note that this routine is called only at server shutdown time.
260 */
261void
262nlm_shutdown_hosts(void)
263{
264 struct nlm_host *host;
265 int i;
266
267 dprintk("lockd: shutting down host module\n");
268 down(&nlm_host_sema);
269
270 /* First, make all hosts eligible for gc */
271 dprintk("lockd: nuking all hosts...\n");
272 for (i = 0; i < NLM_HOST_NRHASH; i++) {
273 for (host = nlm_hosts[i]; host; host = host->h_next)
274 host->h_expires = jiffies - 1;
275 }
276
277 /* Then, perform a garbage collection pass */
278 nlm_gc_hosts();
279 up(&nlm_host_sema);
280
281 /* complain if any hosts are left */
282 if (nrhosts) {
283 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
284 dprintk("lockd: %d hosts left:\n", nrhosts);
285 for (i = 0; i < NLM_HOST_NRHASH; i++) {
286 for (host = nlm_hosts[i]; host; host = host->h_next) {
287 dprintk(" %s (cnt %d use %d exp %ld)\n",
288 host->h_name, atomic_read(&host->h_count),
289 host->h_inuse, host->h_expires);
290 }
291 }
292 }
293}
294
295/*
296 * Garbage collect any unused NLM hosts.
297 * This GC combines reference counting for async operations with
298 * mark & sweep for resources held by remote clients.
299 */
300static void
301nlm_gc_hosts(void)
302{
303 struct nlm_host **q, *host;
304 struct rpc_clnt *clnt;
305 int i;
306
307 dprintk("lockd: host garbage collection\n");
308 for (i = 0; i < NLM_HOST_NRHASH; i++) {
309 for (host = nlm_hosts[i]; host; host = host->h_next)
310 host->h_inuse = 0;
311 }
312
313 /* Mark all hosts that hold locks, blocks or shares */
314 nlmsvc_mark_resources();
315
316 for (i = 0; i < NLM_HOST_NRHASH; i++) {
317 q = &nlm_hosts[i];
318 while ((host = *q) != NULL) {
319 if (atomic_read(&host->h_count) || host->h_inuse
320 || time_before(jiffies, host->h_expires)) {
321 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
322 host->h_name, atomic_read(&host->h_count),
323 host->h_inuse, host->h_expires);
324 q = &host->h_next;
325 continue;
326 }
327 dprintk("lockd: delete host %s\n", host->h_name);
328 *q = host->h_next;
329 /* Don't unmonitor hosts that have been invalidated */
330 if (host->h_monitored && !host->h_killed)
331 nsm_unmonitor(host);
332 if ((clnt = host->h_rpcclnt) != NULL) {
333 if (atomic_read(&clnt->cl_users)) {
334 printk(KERN_WARNING
335 "lockd: active RPC handle\n");
336 clnt->cl_dead = 1;
337 } else {
338 rpc_destroy_client(host->h_rpcclnt);
339 }
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 kfree(host);
342 nrhosts--;
343 }
344 }
345
346 next_gc = jiffies + NLM_HOST_COLLECT;
347}
348