[PATCH] knfsd: lockd: optionally use hostnames for identifying peers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / lockd / host.c
bloba1423c66df04b4eb33cbe97f1c0ed150af799cfa
1 /*
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>
9 */
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 #include <linux/mutex.h>
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)
30 static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
31 static unsigned long next_gc;
32 static int nrhosts;
33 static DEFINE_MUTEX(nlm_host_mutex);
36 static void nlm_gc_hosts(void);
37 static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
38 const char *, int, int);
41 * Find an NLM server handle in the cache. If there is none, create it.
43 struct nlm_host *
44 nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
45 const char *hostname, int hostname_len)
47 return nlm_lookup_host(0, sin, proto, version,
48 hostname, hostname_len);
52 * Find an NLM client handle in the cache. If there is none, create it.
54 struct nlm_host *
55 nlmsvc_lookup_host(struct svc_rqst *rqstp,
56 const char *hostname, int hostname_len)
58 return nlm_lookup_host(1, &rqstp->rq_addr,
59 rqstp->rq_prot, rqstp->rq_vers,
60 hostname, hostname_len);
64 * Common host lookup routine for server & client
66 struct nlm_host *
67 nlm_lookup_host(int server, const struct sockaddr_in *sin,
68 int proto, int version,
69 const char *hostname,
70 int hostname_len)
72 struct hlist_head *chain;
73 struct hlist_node *pos;
74 struct nlm_host *host;
75 struct nsm_handle *nsm = NULL;
76 int hash;
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>");
85 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
87 /* Lock hash table */
88 mutex_lock(&nlm_host_mutex);
90 if (time_after_eq(jiffies, next_gc))
91 nlm_gc_hosts();
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.
100 chain = &nlm_hosts[hash];
101 hlist_for_each_entry(host, pos, chain, h_hash) {
102 if (!nlm_cmp_addr(&host->h_addr, sin))
103 continue;
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);
109 if (host->h_proto != proto)
110 continue;
111 if (host->h_version != version)
112 continue;
113 if (host->h_server != server)
114 continue;
116 /* Move to head of hash chain. */
117 hlist_del(&host->h_hash);
118 hlist_add_head(&host->h_hash, chain);
120 nlm_get_host(host);
121 goto out;
124 host = NULL;
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.
129 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
130 goto out;
132 host = kzalloc(sizeof(*host), GFP_KERNEL);
133 if (!host) {
134 nsm_release(nsm);
135 goto out;
137 host->h_name = nsm->sm_name;
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;
143 mutex_init(&host->h_mutex);
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);
148 init_rwsem(&host->h_rwsem);
149 host->h_state = 0; /* pseudo NSM state */
150 host->h_nsmstate = 0; /* real NSM state */
151 host->h_nsmhandle = nsm;
152 host->h_server = server;
153 hlist_add_head(&host->h_hash, chain);
154 INIT_LIST_HEAD(&host->h_lockowners);
155 spin_lock_init(&host->h_lock);
156 INIT_LIST_HEAD(&host->h_granted);
157 INIT_LIST_HEAD(&host->h_reclaim);
159 if (++nrhosts > NLM_HOST_MAX)
160 next_gc = 0;
162 out:
163 mutex_unlock(&nlm_host_mutex);
164 return host;
168 * Destroy a host
170 static void
171 nlm_destroy_host(struct nlm_host *host)
173 struct rpc_clnt *clnt;
175 BUG_ON(!list_empty(&host->h_lockowners));
176 BUG_ON(atomic_read(&host->h_count));
179 * Release NSM handle and unmonitor host.
181 nsm_unmonitor(host);
183 if ((clnt = host->h_rpcclnt) != NULL) {
184 if (atomic_read(&clnt->cl_users)) {
185 printk(KERN_WARNING
186 "lockd: active RPC handle\n");
187 clnt->cl_dead = 1;
188 } else {
189 rpc_destroy_client(host->h_rpcclnt);
192 kfree(host);
196 * Create the NLM RPC client for an NLM peer
198 struct rpc_clnt *
199 nlm_bind_host(struct nlm_host *host)
201 struct rpc_clnt *clnt;
203 dprintk("lockd: nlm_bind_host(%08x)\n",
204 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
206 /* Lock host handle */
207 mutex_lock(&host->h_mutex);
209 /* If we've already created an RPC client, check whether
210 * RPC rebind is required
212 if ((clnt = host->h_rpcclnt) != NULL) {
213 if (time_after_eq(jiffies, host->h_nextrebind)) {
214 rpc_force_rebind(clnt);
215 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
216 dprintk("lockd: next rebind in %ld jiffies\n",
217 host->h_nextrebind - jiffies);
219 } else {
220 unsigned long increment = nlmsvc_timeout * HZ;
221 struct rpc_timeout timeparms = {
222 .to_initval = increment,
223 .to_increment = increment,
224 .to_maxval = increment * 6UL,
225 .to_retries = 5U,
227 struct rpc_create_args args = {
228 .protocol = host->h_proto,
229 .address = (struct sockaddr *)&host->h_addr,
230 .addrsize = sizeof(host->h_addr),
231 .timeout = &timeparms,
232 .servername = host->h_name,
233 .program = &nlm_program,
234 .version = host->h_version,
235 .authflavor = RPC_AUTH_UNIX,
236 .flags = (RPC_CLNT_CREATE_HARDRTRY |
237 RPC_CLNT_CREATE_AUTOBIND),
240 clnt = rpc_create(&args);
241 if (!IS_ERR(clnt))
242 host->h_rpcclnt = clnt;
243 else {
244 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
245 clnt = NULL;
249 mutex_unlock(&host->h_mutex);
250 return clnt;
254 * Force a portmap lookup of the remote lockd port
256 void
257 nlm_rebind_host(struct nlm_host *host)
259 dprintk("lockd: rebind host %s\n", host->h_name);
260 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
261 rpc_force_rebind(host->h_rpcclnt);
262 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
267 * Increment NLM host count
269 struct nlm_host * nlm_get_host(struct nlm_host *host)
271 if (host) {
272 dprintk("lockd: get host %s\n", host->h_name);
273 atomic_inc(&host->h_count);
274 host->h_expires = jiffies + NLM_HOST_EXPIRE;
276 return host;
280 * Release NLM host after use
282 void nlm_release_host(struct nlm_host *host)
284 if (host != NULL) {
285 dprintk("lockd: release host %s\n", host->h_name);
286 BUG_ON(atomic_read(&host->h_count) < 0);
287 if (atomic_dec_and_test(&host->h_count)) {
288 BUG_ON(!list_empty(&host->h_lockowners));
289 BUG_ON(!list_empty(&host->h_granted));
290 BUG_ON(!list_empty(&host->h_reclaim));
296 * We were notified that the host indicated by address &sin
297 * has rebooted.
298 * Release all resources held by that peer.
300 void nlm_host_rebooted(const struct sockaddr_in *sin,
301 const char *hostname, int hostname_len,
302 u32 new_state)
304 struct hlist_head *chain;
305 struct hlist_node *pos;
306 struct nsm_handle *nsm;
307 struct nlm_host *host;
309 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
310 hostname, NIPQUAD(sin->sin_addr));
312 /* Find the NSM handle for this peer */
313 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
314 return;
316 /* When reclaiming locks on this peer, make sure that
317 * we set up a new notification */
318 nsm->sm_monitored = 0;
320 /* Mark all hosts tied to this NSM state as having rebooted.
321 * We run the loop repeatedly, because we drop the host table
322 * lock for this.
323 * To avoid processing a host several times, we match the nsmstate.
325 again: mutex_lock(&nlm_host_mutex);
326 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
327 hlist_for_each_entry(host, pos, chain, h_hash) {
328 if (host->h_nsmhandle == nsm
329 && host->h_nsmstate != new_state) {
330 host->h_nsmstate = new_state;
331 host->h_state++;
333 nlm_get_host(host);
334 mutex_unlock(&nlm_host_mutex);
336 if (host->h_server) {
337 /* We're server for this guy, just ditch
338 * all the locks he held. */
339 nlmsvc_free_host_resources(host);
340 } else {
341 /* He's the server, initiate lock recovery. */
342 nlmclnt_recovery(host);
345 nlm_release_host(host);
346 goto again;
351 mutex_unlock(&nlm_host_mutex);
355 * Shut down the hosts module.
356 * Note that this routine is called only at server shutdown time.
358 void
359 nlm_shutdown_hosts(void)
361 struct hlist_head *chain;
362 struct hlist_node *pos;
363 struct nlm_host *host;
365 dprintk("lockd: shutting down host module\n");
366 mutex_lock(&nlm_host_mutex);
368 /* First, make all hosts eligible for gc */
369 dprintk("lockd: nuking all hosts...\n");
370 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
371 hlist_for_each_entry(host, pos, chain, h_hash)
372 host->h_expires = jiffies - 1;
375 /* Then, perform a garbage collection pass */
376 nlm_gc_hosts();
377 mutex_unlock(&nlm_host_mutex);
379 /* complain if any hosts are left */
380 if (nrhosts) {
381 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
382 dprintk("lockd: %d hosts left:\n", nrhosts);
383 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
384 hlist_for_each_entry(host, pos, chain, h_hash) {
385 dprintk(" %s (cnt %d use %d exp %ld)\n",
386 host->h_name, atomic_read(&host->h_count),
387 host->h_inuse, host->h_expires);
394 * Garbage collect any unused NLM hosts.
395 * This GC combines reference counting for async operations with
396 * mark & sweep for resources held by remote clients.
398 static void
399 nlm_gc_hosts(void)
401 struct hlist_head *chain;
402 struct hlist_node *pos, *next;
403 struct nlm_host *host;
405 dprintk("lockd: host garbage collection\n");
406 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
407 hlist_for_each_entry(host, pos, chain, h_hash)
408 host->h_inuse = 0;
411 /* Mark all hosts that hold locks, blocks or shares */
412 nlmsvc_mark_resources();
414 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
415 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
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);
421 continue;
423 dprintk("lockd: delete host %s\n", host->h_name);
424 hlist_del_init(&host->h_hash);
426 nlm_destroy_host(host);
427 nrhosts--;
431 next_gc = jiffies + NLM_HOST_COLLECT;
436 * Manage NSM handles
438 static LIST_HEAD(nsm_handles);
439 static DECLARE_MUTEX(nsm_sema);
441 static struct nsm_handle *
442 __nsm_find(const struct sockaddr_in *sin,
443 const char *hostname, int hostname_len,
444 int create)
446 struct nsm_handle *nsm = NULL;
447 struct list_head *pos;
449 if (!sin)
450 return NULL;
452 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
453 if (printk_ratelimit()) {
454 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
455 "in NFS lock request\n",
456 hostname_len, hostname);
458 return NULL;
461 down(&nsm_sema);
462 list_for_each(pos, &nsm_handles) {
463 nsm = list_entry(pos, struct nsm_handle, sm_link);
465 if (hostname && nsm_use_hostnames) {
466 if (strlen(nsm->sm_name) != hostname_len
467 || memcmp(nsm->sm_name, hostname, hostname_len))
468 continue;
469 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
470 continue;
471 atomic_inc(&nsm->sm_count);
472 goto out;
475 if (!create) {
476 nsm = NULL;
477 goto out;
480 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
481 if (nsm != NULL) {
482 nsm->sm_addr = *sin;
483 nsm->sm_name = (char *) (nsm + 1);
484 memcpy(nsm->sm_name, hostname, hostname_len);
485 nsm->sm_name[hostname_len] = '\0';
486 atomic_set(&nsm->sm_count, 1);
488 list_add(&nsm->sm_link, &nsm_handles);
491 out: up(&nsm_sema);
492 return nsm;
495 struct nsm_handle *
496 nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
498 return __nsm_find(sin, hostname, hostname_len, 1);
502 * Release an NSM handle
504 void
505 nsm_release(struct nsm_handle *nsm)
507 if (!nsm)
508 return;
509 if (atomic_dec_and_test(&nsm->sm_count)) {
510 down(&nsm_sema);
511 if (atomic_read(&nsm->sm_count) == 0) {
512 list_del(&nsm->sm_link);
513 kfree(nsm);
515 up(&nsm_sema);