blob: 3cd96e2e12564c88a3d0dc331bb41e8b1d4efd77 [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>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static struct nlm_host * nlm_hosts[NLM_HOST_NRHASH];
31static unsigned long next_gc;
32static int nrhosts;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080033static DEFINE_MUTEX(nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35
36static void nlm_gc_hosts(void);
Olaf Kirch8dead0d2006-10-04 02:15:53 -070037static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
38 const char *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Find an NLM server handle in the cache. If there is none, create it.
42 */
43struct nlm_host *
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070044nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
45 const char *hostname, int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070047 return nlm_lookup_host(0, sin, proto, version,
48 hostname, hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51/*
52 * Find an NLM client handle in the cache. If there is none, create it.
53 */
54struct nlm_host *
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070055nlmsvc_lookup_host(struct svc_rqst *rqstp,
56 const char *hostname, int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 return nlm_lookup_host(1, &rqstp->rq_addr,
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070059 rqstp->rq_prot, rqstp->rq_vers,
60 hostname, hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63/*
64 * Common host lookup routine for server & client
65 */
66struct nlm_host *
Olaf Kirchcf712c22006-10-04 02:15:52 -070067nlm_lookup_host(int server, const struct sockaddr_in *sin,
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070068 int proto, int version,
69 const char *hostname,
70 int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct nlm_host *host, **hp;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070073 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int hash;
75
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070076 dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
77 NIPQUAD(sin->sin_addr.s_addr), proto, version,
78 server? "server" : "client",
79 hostname_len,
80 hostname? hostname : "<none>");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
84
85 /* Lock hash table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080086 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (time_after_eq(jiffies, next_gc))
89 nlm_gc_hosts();
90
Olaf Kirch8dead0d2006-10-04 02:15:53 -070091 /* We may keep several nlm_host objects for a peer, because each
92 * nlm_host is identified by
93 * (address, protocol, version, server/client)
94 * We could probably simplify this a little by putting all those
95 * different NLM rpc_clients into one single nlm_host object.
96 * This would allow us to have one nlm_host per address.
97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
Olaf Kirch8dead0d2006-10-04 02:15:53 -070099 if (!nlm_cmp_addr(&host->h_addr, sin))
100 continue;
101
102 /* See if we have an NSM handle for this client */
103 if (!nsm && (nsm = host->h_nsmhandle) != 0)
104 atomic_inc(&nsm->sm_count);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (host->h_proto != proto)
107 continue;
108 if (host->h_version != version)
109 continue;
110 if (host->h_server != server)
111 continue;
112
Olaf Kirchf0737a32006-10-04 02:15:54 -0700113 if (hp != nlm_hosts + hash) {
114 *hp = host->h_next;
115 host->h_next = nlm_hosts[hash];
116 nlm_hosts[hash] = host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Olaf Kirchf0737a32006-10-04 02:15:54 -0700118 nlm_get_host(host);
119 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700122 /* Sadly, the host isn't in our hash table yet. See if
123 * we have an NSM handle for it. If not, create one.
124 */
125 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700128 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700129 if (!host) {
130 nsm_release(nsm);
131 goto out;
132 }
133 host->h_name = nsm->sm_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 host->h_addr = *sin;
135 host->h_addr.sin_port = 0; /* ouch! */
136 host->h_version = version;
137 host->h_proto = proto;
138 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400139 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
141 host->h_expires = jiffies + NLM_HOST_EXPIRE;
142 atomic_set(&host->h_count, 1);
143 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400144 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 host->h_state = 0; /* pseudo NSM state */
146 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700147 host->h_nsmhandle = nsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 host->h_server = server;
149 host->h_next = nlm_hosts[hash];
150 nlm_hosts[hash] = host;
151 INIT_LIST_HEAD(&host->h_lockowners);
152 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500153 INIT_LIST_HEAD(&host->h_granted);
154 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (++nrhosts > NLM_HOST_MAX)
157 next_gc = 0;
158
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700159out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800160 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return host;
162}
163
164struct nlm_host *
165nlm_find_client(void)
166{
167 /* find a nlm_host for a client for which h_killed == 0.
168 * and return it
169 */
170 int hash;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800171 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
173 struct nlm_host *host, **hp;
174 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
175 if (host->h_server &&
176 host->h_killed == 0) {
177 nlm_get_host(host);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800178 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return host;
180 }
181 }
182 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800183 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return NULL;
185}
186
187
188/*
189 * Create the NLM RPC client for an NLM peer
190 */
191struct rpc_clnt *
192nlm_bind_host(struct nlm_host *host)
193{
194 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 dprintk("lockd: nlm_bind_host(%08x)\n",
197 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
198
199 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400200 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* If we've already created an RPC client, check whether
203 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
205 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700206 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100207 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
209 dprintk("lockd: next rebind in %ld jiffies\n",
210 host->h_nextrebind - jiffies);
211 }
212 } else {
Chuck Levere1ec7892006-08-22 20:06:20 -0400213 unsigned long increment = nlmsvc_timeout * HZ;
214 struct rpc_timeout timeparms = {
215 .to_initval = increment,
216 .to_increment = increment,
217 .to_maxval = increment * 6UL,
218 .to_retries = 5U,
219 };
220 struct rpc_create_args args = {
221 .protocol = host->h_proto,
222 .address = (struct sockaddr *)&host->h_addr,
223 .addrsize = sizeof(host->h_addr),
224 .timeout = &timeparms,
225 .servername = host->h_name,
226 .program = &nlm_program,
227 .version = host->h_version,
228 .authflavor = RPC_AUTH_UNIX,
229 .flags = (RPC_CLNT_CREATE_HARDRTRY |
230 RPC_CLNT_CREATE_AUTOBIND),
231 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Chuck Levere1ec7892006-08-22 20:06:20 -0400233 clnt = rpc_create(&args);
234 if (!IS_ERR(clnt))
235 host->h_rpcclnt = clnt;
236 else {
237 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
238 clnt = NULL;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
Trond Myklebust50467912006-06-09 09:40:24 -0400242 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/*
247 * Force a portmap lookup of the remote lockd port
248 */
249void
250nlm_rebind_host(struct nlm_host *host)
251{
252 dprintk("lockd: rebind host %s\n", host->h_name);
253 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100254 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
256 }
257}
258
259/*
260 * Increment NLM host count
261 */
262struct nlm_host * nlm_get_host(struct nlm_host *host)
263{
264 if (host) {
265 dprintk("lockd: get host %s\n", host->h_name);
266 atomic_inc(&host->h_count);
267 host->h_expires = jiffies + NLM_HOST_EXPIRE;
268 }
269 return host;
270}
271
272/*
273 * Release NLM host after use
274 */
275void nlm_release_host(struct nlm_host *host)
276{
277 if (host != NULL) {
278 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500280 if (atomic_dec_and_test(&host->h_count)) {
281 BUG_ON(!list_empty(&host->h_lockowners));
282 BUG_ON(!list_empty(&host->h_granted));
283 BUG_ON(!list_empty(&host->h_reclaim));
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286}
287
288/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700289 * We were notified that the host indicated by address &sin
290 * has rebooted.
291 * Release all resources held by that peer.
292 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700293void nlm_host_rebooted(const struct sockaddr_in *sin,
294 const char *hostname, int hostname_len,
295 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700296{
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700297 struct nsm_handle *nsm;
298 struct nlm_host *host, **hp;
299 int hash;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700300
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700301 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
302 hostname, NIPQUAD(sin->sin_addr));
303
304 /* Find the NSM handle for this peer */
305 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700306 return;
307
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700308 /* When reclaiming locks on this peer, make sure that
309 * we set up a new notification */
310 nsm->sm_monitored = 0;
311
312 /* Mark all hosts tied to this NSM state as having rebooted.
313 * We run the loop repeatedly, because we drop the host table
314 * lock for this.
315 * To avoid processing a host several times, we match the nsmstate.
316 */
317again: mutex_lock(&nlm_host_mutex);
318 for (hash = 0; hash < NLM_HOST_NRHASH; hash++) {
319 for (hp = &nlm_hosts[hash]; (host = *hp); hp = &host->h_next) {
320 if (host->h_nsmhandle == nsm
321 && host->h_nsmstate != new_state) {
322 host->h_nsmstate = new_state;
323 host->h_state++;
324
325 nlm_get_host(host);
326 mutex_unlock(&nlm_host_mutex);
327
328 if (host->h_server) {
329 /* We're server for this guy, just ditch
330 * all the locks he held. */
331 nlmsvc_free_host_resources(host);
332 } else {
333 /* He's the server, initiate lock recovery. */
334 nlmclnt_recovery(host);
335 }
336
337 nlm_release_host(host);
338 goto again;
339 }
340 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700341 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700342
343 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700344}
345
346/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * Shut down the hosts module.
348 * Note that this routine is called only at server shutdown time.
349 */
350void
351nlm_shutdown_hosts(void)
352{
353 struct nlm_host *host;
354 int i;
355
356 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800357 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* First, make all hosts eligible for gc */
360 dprintk("lockd: nuking all hosts...\n");
361 for (i = 0; i < NLM_HOST_NRHASH; i++) {
362 for (host = nlm_hosts[i]; host; host = host->h_next)
363 host->h_expires = jiffies - 1;
364 }
365
366 /* Then, perform a garbage collection pass */
367 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800368 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* complain if any hosts are left */
371 if (nrhosts) {
372 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
373 dprintk("lockd: %d hosts left:\n", nrhosts);
374 for (i = 0; i < NLM_HOST_NRHASH; i++) {
375 for (host = nlm_hosts[i]; host; host = host->h_next) {
376 dprintk(" %s (cnt %d use %d exp %ld)\n",
377 host->h_name, atomic_read(&host->h_count),
378 host->h_inuse, host->h_expires);
379 }
380 }
381 }
382}
383
384/*
385 * Garbage collect any unused NLM hosts.
386 * This GC combines reference counting for async operations with
387 * mark & sweep for resources held by remote clients.
388 */
389static void
390nlm_gc_hosts(void)
391{
392 struct nlm_host **q, *host;
393 struct rpc_clnt *clnt;
394 int i;
395
396 dprintk("lockd: host garbage collection\n");
397 for (i = 0; i < NLM_HOST_NRHASH; i++) {
398 for (host = nlm_hosts[i]; host; host = host->h_next)
399 host->h_inuse = 0;
400 }
401
402 /* Mark all hosts that hold locks, blocks or shares */
403 nlmsvc_mark_resources();
404
405 for (i = 0; i < NLM_HOST_NRHASH; i++) {
406 q = &nlm_hosts[i];
407 while ((host = *q) != NULL) {
408 if (atomic_read(&host->h_count) || host->h_inuse
409 || time_before(jiffies, host->h_expires)) {
410 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
411 host->h_name, atomic_read(&host->h_count),
412 host->h_inuse, host->h_expires);
413 q = &host->h_next;
414 continue;
415 }
416 dprintk("lockd: delete host %s\n", host->h_name);
417 *q = host->h_next;
Olaf Kirch977faf32006-10-04 02:15:51 -0700418
419 /*
420 * Unmonitor unless host was invalidated (i.e. lockd restarted)
421 */
422 nsm_unmonitor(host);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if ((clnt = host->h_rpcclnt) != NULL) {
425 if (atomic_read(&clnt->cl_users)) {
426 printk(KERN_WARNING
427 "lockd: active RPC handle\n");
428 clnt->cl_dead = 1;
429 } else {
430 rpc_destroy_client(host->h_rpcclnt);
431 }
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 kfree(host);
434 nrhosts--;
435 }
436 }
437
438 next_gc = jiffies + NLM_HOST_COLLECT;
439}
440
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700441
442/*
443 * Manage NSM handles
444 */
445static LIST_HEAD(nsm_handles);
446static DECLARE_MUTEX(nsm_sema);
447
448static struct nsm_handle *
449__nsm_find(const struct sockaddr_in *sin,
450 const char *hostname, int hostname_len,
451 int create)
452{
453 struct nsm_handle *nsm = NULL;
454 struct list_head *pos;
455
456 if (!sin)
457 return NULL;
458
459 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
460 if (printk_ratelimit()) {
461 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
462 "in NFS lock request\n",
463 hostname_len, hostname);
464 }
465 return NULL;
466 }
467
468 down(&nsm_sema);
469 list_for_each(pos, &nsm_handles) {
470 nsm = list_entry(pos, struct nsm_handle, sm_link);
471
472 if (!nlm_cmp_addr(&nsm->sm_addr, sin))
473 continue;
474 atomic_inc(&nsm->sm_count);
475 goto out;
476 }
477
478 if (!create) {
479 nsm = NULL;
480 goto out;
481 }
482
483 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
484 if (nsm != NULL) {
485 nsm->sm_addr = *sin;
486 nsm->sm_name = (char *) (nsm + 1);
487 memcpy(nsm->sm_name, hostname, hostname_len);
488 nsm->sm_name[hostname_len] = '\0';
489 atomic_set(&nsm->sm_count, 1);
490
491 list_add(&nsm->sm_link, &nsm_handles);
492 }
493
494out: up(&nsm_sema);
495 return nsm;
496}
497
498struct nsm_handle *
499nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
500{
501 return __nsm_find(sin, hostname, hostname_len, 1);
502}
503
504/*
505 * Release an NSM handle
506 */
507void
508nsm_release(struct nsm_handle *nsm)
509{
510 if (!nsm)
511 return;
512 if (atomic_dec_and_test(&nsm->sm_count)) {
513 down(&nsm_sema);
514 if (atomic_read(&nsm->sm_count) == 0) {
515 list_del(&nsm->sm_link);
516 kfree(nsm);
517 }
518 up(&nsm_sema);
519 }
520}