[PATCH] knfsd: lockd: make nlm_traverse_* more flexible
[deliverable/linux.git] / fs / lockd / host.c
CommitLineData
1da177e4
LT
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>
353ab6e9 19#include <linux/mutex.h>
1da177e4
LT
20
21
22#define NLMDBG_FACILITY NLMDBG_HOSTCACHE
23#define NLM_HOST_MAX 64
24#define NLM_HOST_NRHASH 32
25#define NLM_ADDRHASH(addr) (ntohl(addr) & (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)
1da177e4 29
0cea3276 30static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
1da177e4
LT
31static unsigned long next_gc;
32static int nrhosts;
353ab6e9 33static DEFINE_MUTEX(nlm_host_mutex);
1da177e4
LT
34
35
36static void nlm_gc_hosts(void);
8dead0db
OK
37static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
38 const char *, int, int);
1da177e4
LT
39
40/*
41 * Find an NLM server handle in the cache. If there is none, create it.
42 */
43struct nlm_host *
db4e4c9a
OK
44nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
45 const char *hostname, int hostname_len)
1da177e4 46{
db4e4c9a
OK
47 return nlm_lookup_host(0, sin, proto, version,
48 hostname, hostname_len);
1da177e4
LT
49}
50
51/*
52 * Find an NLM client handle in the cache. If there is none, create it.
53 */
54struct nlm_host *
db4e4c9a
OK
55nlmsvc_lookup_host(struct svc_rqst *rqstp,
56 const char *hostname, int hostname_len)
1da177e4
LT
57{
58 return nlm_lookup_host(1, &rqstp->rq_addr,
db4e4c9a
OK
59 rqstp->rq_prot, rqstp->rq_vers,
60 hostname, hostname_len);
1da177e4
LT
61}
62
63/*
64 * Common host lookup routine for server & client
65 */
66struct nlm_host *
cf712c24 67nlm_lookup_host(int server, const struct sockaddr_in *sin,
db4e4c9a
OK
68 int proto, int version,
69 const char *hostname,
70 int hostname_len)
1da177e4 71{
0cea3276
OK
72 struct hlist_head *chain;
73 struct hlist_node *pos;
74 struct nlm_host *host;
8dead0db 75 struct nsm_handle *nsm = NULL;
1da177e4
LT
76 int hash;
77
db4e4c9a
OK
78 dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
79 NIPQUAD(sin->sin_addr.s_addr), proto, version,
80 server? "server" : "client",
81 hostname_len,
82 hostname? hostname : "<none>");
83
1da177e4
LT
84
85 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
86
87 /* Lock hash table */
353ab6e9 88 mutex_lock(&nlm_host_mutex);
1da177e4
LT
89
90 if (time_after_eq(jiffies, next_gc))
91 nlm_gc_hosts();
92
8dead0db
OK
93 /* We may keep several nlm_host objects for a peer, because each
94 * nlm_host is identified by
95 * (address, protocol, version, server/client)
96 * We could probably simplify this a little by putting all those
97 * different NLM rpc_clients into one single nlm_host object.
98 * This would allow us to have one nlm_host per address.
99 */
0cea3276
OK
100 chain = &nlm_hosts[hash];
101 hlist_for_each_entry(host, pos, chain, h_hash) {
8dead0db
OK
102 if (!nlm_cmp_addr(&host->h_addr, sin))
103 continue;
104
105 /* See if we have an NSM handle for this client */
106 if (!nsm && (nsm = host->h_nsmhandle) != 0)
107 atomic_inc(&nsm->sm_count);
108
1da177e4
LT
109 if (host->h_proto != proto)
110 continue;
111 if (host->h_version != version)
112 continue;
113 if (host->h_server != server)
114 continue;
115
0cea3276
OK
116 /* Move to head of hash chain. */
117 hlist_del(&host->h_hash);
118 hlist_add_head(&host->h_hash, chain);
119
f0737a39
OK
120 nlm_get_host(host);
121 goto out;
1da177e4
LT
122 }
123
0cea3276
OK
124 host = NULL;
125
8dead0db
OK
126 /* Sadly, the host isn't in our hash table yet. See if
127 * we have an NSM handle for it. If not, create one.
128 */
129 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
130 goto out;
1da177e4 131
f8314dc6 132 host = kzalloc(sizeof(*host), GFP_KERNEL);
8dead0db
OK
133 if (!host) {
134 nsm_release(nsm);
135 goto out;
136 }
137 host->h_name = nsm->sm_name;
1da177e4
LT
138 host->h_addr = *sin;
139 host->h_addr.sin_port = 0; /* ouch! */
140 host->h_version = version;
141 host->h_proto = proto;
142 host->h_rpcclnt = NULL;
50467914 143 mutex_init(&host->h_mutex);
1da177e4
LT
144 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
145 host->h_expires = jiffies + NLM_HOST_EXPIRE;
146 atomic_set(&host->h_count, 1);
147 init_waitqueue_head(&host->h_gracewait);
28df955a 148 init_rwsem(&host->h_rwsem);
1da177e4
LT
149 host->h_state = 0; /* pseudo NSM state */
150 host->h_nsmstate = 0; /* real NSM state */
8dead0db 151 host->h_nsmhandle = nsm;
1da177e4 152 host->h_server = server;
0cea3276 153 hlist_add_head(&host->h_hash, chain);
1da177e4
LT
154 INIT_LIST_HEAD(&host->h_lockowners);
155 spin_lock_init(&host->h_lock);
26bcbf96
CH
156 INIT_LIST_HEAD(&host->h_granted);
157 INIT_LIST_HEAD(&host->h_reclaim);
1da177e4
LT
158
159 if (++nrhosts > NLM_HOST_MAX)
160 next_gc = 0;
161
8dead0db 162out:
353ab6e9 163 mutex_unlock(&nlm_host_mutex);
1da177e4
LT
164 return host;
165}
166
167struct nlm_host *
168nlm_find_client(void)
169{
0cea3276
OK
170 struct hlist_head *chain;
171 struct hlist_node *pos;
172
1da177e4
LT
173 /* find a nlm_host for a client for which h_killed == 0.
174 * and return it
175 */
353ab6e9 176 mutex_lock(&nlm_host_mutex);
0cea3276
OK
177 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
178 struct nlm_host *host;
179
180 hlist_for_each_entry(host, pos, chain, h_hash) {
1da177e4
LT
181 if (host->h_server &&
182 host->h_killed == 0) {
183 nlm_get_host(host);
353ab6e9 184 mutex_unlock(&nlm_host_mutex);
1da177e4
LT
185 return host;
186 }
187 }
188 }
353ab6e9 189 mutex_unlock(&nlm_host_mutex);
1da177e4
LT
190 return NULL;
191}
192
193
194/*
195 * Create the NLM RPC client for an NLM peer
196 */
197struct rpc_clnt *
198nlm_bind_host(struct nlm_host *host)
199{
200 struct rpc_clnt *clnt;
1da177e4
LT
201
202 dprintk("lockd: nlm_bind_host(%08x)\n",
203 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
204
205 /* Lock host handle */
50467914 206 mutex_lock(&host->h_mutex);
1da177e4
LT
207
208 /* If we've already created an RPC client, check whether
209 * RPC rebind is required
1da177e4
LT
210 */
211 if ((clnt = host->h_rpcclnt) != NULL) {
43118c29 212 if (time_after_eq(jiffies, host->h_nextrebind)) {
35f5a422 213 rpc_force_rebind(clnt);
1da177e4
LT
214 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
215 dprintk("lockd: next rebind in %ld jiffies\n",
216 host->h_nextrebind - jiffies);
217 }
218 } else {
e1ec7892
CL
219 unsigned long increment = nlmsvc_timeout * HZ;
220 struct rpc_timeout timeparms = {
221 .to_initval = increment,
222 .to_increment = increment,
223 .to_maxval = increment * 6UL,
224 .to_retries = 5U,
225 };
226 struct rpc_create_args args = {
227 .protocol = host->h_proto,
228 .address = (struct sockaddr *)&host->h_addr,
229 .addrsize = sizeof(host->h_addr),
230 .timeout = &timeparms,
231 .servername = host->h_name,
232 .program = &nlm_program,
233 .version = host->h_version,
234 .authflavor = RPC_AUTH_UNIX,
235 .flags = (RPC_CLNT_CREATE_HARDRTRY |
236 RPC_CLNT_CREATE_AUTOBIND),
237 };
238
239 clnt = rpc_create(&args);
240 if (!IS_ERR(clnt))
241 host->h_rpcclnt = clnt;
242 else {
243 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
244 clnt = NULL;
245 }
1da177e4
LT
246 }
247
50467914 248 mutex_unlock(&host->h_mutex);
1da177e4 249 return clnt;
1da177e4
LT
250}
251
252/*
253 * Force a portmap lookup of the remote lockd port
254 */
255void
256nlm_rebind_host(struct nlm_host *host)
257{
258 dprintk("lockd: rebind host %s\n", host->h_name);
259 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
35f5a422 260 rpc_force_rebind(host->h_rpcclnt);
1da177e4
LT
261 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
262 }
263}
264
265/*
266 * Increment NLM host count
267 */
268struct nlm_host * nlm_get_host(struct nlm_host *host)
269{
270 if (host) {
271 dprintk("lockd: get host %s\n", host->h_name);
272 atomic_inc(&host->h_count);
273 host->h_expires = jiffies + NLM_HOST_EXPIRE;
274 }
275 return host;
276}
277
278/*
279 * Release NLM host after use
280 */
281void nlm_release_host(struct nlm_host *host)
282{
283 if (host != NULL) {
284 dprintk("lockd: release host %s\n", host->h_name);
1da177e4 285 BUG_ON(atomic_read(&host->h_count) < 0);
4c060b53
TM
286 if (atomic_dec_and_test(&host->h_count)) {
287 BUG_ON(!list_empty(&host->h_lockowners));
288 BUG_ON(!list_empty(&host->h_granted));
289 BUG_ON(!list_empty(&host->h_reclaim));
290 }
1da177e4
LT
291 }
292}
293
cf712c24
OK
294/*
295 * We were notified that the host indicated by address &sin
296 * has rebooted.
297 * Release all resources held by that peer.
298 */
5c8dd29c
OK
299void nlm_host_rebooted(const struct sockaddr_in *sin,
300 const char *hostname, int hostname_len,
301 u32 new_state)
cf712c24 302{
0cea3276
OK
303 struct hlist_head *chain;
304 struct hlist_node *pos;
5c8dd29c 305 struct nsm_handle *nsm;
0cea3276 306 struct nlm_host *host;
cf712c24 307
5c8dd29c
OK
308 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
309 hostname, NIPQUAD(sin->sin_addr));
310
311 /* Find the NSM handle for this peer */
312 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
db4e4c9a
OK
313 return;
314
5c8dd29c
OK
315 /* When reclaiming locks on this peer, make sure that
316 * we set up a new notification */
317 nsm->sm_monitored = 0;
318
319 /* Mark all hosts tied to this NSM state as having rebooted.
320 * We run the loop repeatedly, because we drop the host table
321 * lock for this.
322 * To avoid processing a host several times, we match the nsmstate.
323 */
324again: mutex_lock(&nlm_host_mutex);
0cea3276
OK
325 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
326 hlist_for_each_entry(host, pos, chain, h_hash) {
5c8dd29c
OK
327 if (host->h_nsmhandle == nsm
328 && host->h_nsmstate != new_state) {
329 host->h_nsmstate = new_state;
330 host->h_state++;
331
332 nlm_get_host(host);
333 mutex_unlock(&nlm_host_mutex);
334
335 if (host->h_server) {
336 /* We're server for this guy, just ditch
337 * all the locks he held. */
338 nlmsvc_free_host_resources(host);
339 } else {
340 /* He's the server, initiate lock recovery. */
341 nlmclnt_recovery(host);
342 }
343
344 nlm_release_host(host);
345 goto again;
346 }
347 }
cf712c24 348 }
5c8dd29c
OK
349
350 mutex_unlock(&nlm_host_mutex);
cf712c24
OK
351}
352
1da177e4
LT
353/*
354 * Shut down the hosts module.
355 * Note that this routine is called only at server shutdown time.
356 */
357void
358nlm_shutdown_hosts(void)
359{
0cea3276
OK
360 struct hlist_head *chain;
361 struct hlist_node *pos;
1da177e4 362 struct nlm_host *host;
1da177e4
LT
363
364 dprintk("lockd: shutting down host module\n");
353ab6e9 365 mutex_lock(&nlm_host_mutex);
1da177e4
LT
366
367 /* First, make all hosts eligible for gc */
368 dprintk("lockd: nuking all hosts...\n");
0cea3276
OK
369 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
370 hlist_for_each_entry(host, pos, chain, h_hash)
1da177e4
LT
371 host->h_expires = jiffies - 1;
372 }
373
374 /* Then, perform a garbage collection pass */
375 nlm_gc_hosts();
353ab6e9 376 mutex_unlock(&nlm_host_mutex);
1da177e4
LT
377
378 /* complain if any hosts are left */
379 if (nrhosts) {
380 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
381 dprintk("lockd: %d hosts left:\n", nrhosts);
0cea3276
OK
382 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
383 hlist_for_each_entry(host, pos, chain, h_hash) {
1da177e4
LT
384 dprintk(" %s (cnt %d use %d exp %ld)\n",
385 host->h_name, atomic_read(&host->h_count),
386 host->h_inuse, host->h_expires);
387 }
388 }
389 }
390}
391
392/*
393 * Garbage collect any unused NLM hosts.
394 * This GC combines reference counting for async operations with
395 * mark & sweep for resources held by remote clients.
396 */
397static void
398nlm_gc_hosts(void)
399{
0cea3276
OK
400 struct hlist_head *chain;
401 struct hlist_node *pos, *next;
402 struct nlm_host *host;
1da177e4 403 struct rpc_clnt *clnt;
1da177e4
LT
404
405 dprintk("lockd: host garbage collection\n");
0cea3276
OK
406 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
407 hlist_for_each_entry(host, pos, chain, h_hash)
1da177e4
LT
408 host->h_inuse = 0;
409 }
410
411 /* Mark all hosts that hold locks, blocks or shares */
412 nlmsvc_mark_resources();
413
0cea3276
OK
414 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
415 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
1da177e4
LT
416 if (atomic_read(&host->h_count) || host->h_inuse
417 || time_before(jiffies, host->h_expires)) {
418 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
419 host->h_name, atomic_read(&host->h_count),
420 host->h_inuse, host->h_expires);
1da177e4
LT
421 continue;
422 }
423 dprintk("lockd: delete host %s\n", host->h_name);
0cea3276 424 hlist_del_init(&host->h_hash);
977faf39
OK
425
426 /*
427 * Unmonitor unless host was invalidated (i.e. lockd restarted)
428 */
429 nsm_unmonitor(host);
430
1da177e4
LT
431 if ((clnt = host->h_rpcclnt) != NULL) {
432 if (atomic_read(&clnt->cl_users)) {
433 printk(KERN_WARNING
434 "lockd: active RPC handle\n");
435 clnt->cl_dead = 1;
436 } else {
437 rpc_destroy_client(host->h_rpcclnt);
438 }
439 }
1da177e4
LT
440 kfree(host);
441 nrhosts--;
442 }
443 }
444
445 next_gc = jiffies + NLM_HOST_COLLECT;
446}
447
8dead0db
OK
448
449/*
450 * Manage NSM handles
451 */
452static LIST_HEAD(nsm_handles);
453static DECLARE_MUTEX(nsm_sema);
454
455static struct nsm_handle *
456__nsm_find(const struct sockaddr_in *sin,
457 const char *hostname, int hostname_len,
458 int create)
459{
460 struct nsm_handle *nsm = NULL;
461 struct list_head *pos;
462
463 if (!sin)
464 return NULL;
465
466 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
467 if (printk_ratelimit()) {
468 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
469 "in NFS lock request\n",
470 hostname_len, hostname);
471 }
472 return NULL;
473 }
474
475 down(&nsm_sema);
476 list_for_each(pos, &nsm_handles) {
477 nsm = list_entry(pos, struct nsm_handle, sm_link);
478
479 if (!nlm_cmp_addr(&nsm->sm_addr, sin))
480 continue;
481 atomic_inc(&nsm->sm_count);
482 goto out;
483 }
484
485 if (!create) {
486 nsm = NULL;
487 goto out;
488 }
489
490 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
491 if (nsm != NULL) {
492 nsm->sm_addr = *sin;
493 nsm->sm_name = (char *) (nsm + 1);
494 memcpy(nsm->sm_name, hostname, hostname_len);
495 nsm->sm_name[hostname_len] = '\0';
496 atomic_set(&nsm->sm_count, 1);
497
498 list_add(&nsm->sm_link, &nsm_handles);
499 }
500
501out: up(&nsm_sema);
502 return nsm;
503}
504
505struct nsm_handle *
506nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
507{
508 return __nsm_find(sin, hostname, hostname_len, 1);
509}
510
511/*
512 * Release an NSM handle
513 */
514void
515nsm_release(struct nsm_handle *nsm)
516{
517 if (!nsm)
518 return;
519 if (atomic_dec_and_test(&nsm->sm_count)) {
520 down(&nsm_sema);
521 if (atomic_read(&nsm->sm_count) == 0) {
522 list_del(&nsm->sm_link);
523 kfree(nsm);
524 }
525 up(&nsm_sema);
526 }
527}
This page took 0.174358 seconds and 5 git commands to generate.