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/slab.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;
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.
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.
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
67 nlm_lookup_host(int server, const struct sockaddr_in *sin,
68 int proto, int version,
72 struct hlist_head *chain;
73 struct hlist_node *pos;
74 struct nlm_host *host;
75 struct nsm_handle *nsm = NULL;
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",
82 hostname? hostname : "<none>");
85 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
88 mutex_lock(&nlm_host_mutex);
90 if (time_after_eq(jiffies, next_gc))
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))
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)
111 if (host->h_version != version)
113 if (host->h_server != server)
116 /* Move to head of hash chain. */
117 hlist_del(&host->h_hash);
118 hlist_add_head(&host->h_hash, chain);
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)))
132 host = kzalloc(sizeof(*host), GFP_KERNEL);
137 host->h_name = nsm->sm_name;
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)
163 mutex_unlock(&nlm_host_mutex);
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.
183 if ((clnt = host->h_rpcclnt) != NULL) {
184 if (atomic_read(&clnt->cl_users)) {
186 "lockd: active RPC handle\n");
189 rpc_destroy_client(host->h_rpcclnt);
196 * Create the NLM RPC client for an NLM peer
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);
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,
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);
242 host->h_rpcclnt = clnt;
244 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
249 mutex_unlock(&host->h_mutex);
254 * Force a portmap lookup of the remote lockd port
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)
272 dprintk("lockd: get host %s\n", host->h_name);
273 atomic_inc(&host->h_count);
274 host->h_expires = jiffies + NLM_HOST_EXPIRE;
280 * Release NLM host after use
282 void nlm_release_host(struct nlm_host *host)
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
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,
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)))
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
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;
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);
341 /* He's the server, initiate lock recovery. */
342 nlmclnt_recovery(host);
345 nlm_release_host(host);
351 mutex_unlock(&nlm_host_mutex);
355 * Shut down the hosts module.
356 * Note that this routine is called only at server shutdown time.
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 */
377 mutex_unlock(&nlm_host_mutex);
379 /* complain if any hosts are left */
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.
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)
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);
423 dprintk("lockd: delete host %s\n", host->h_name);
424 hlist_del_init(&host->h_hash);
426 nlm_destroy_host(host);
431 next_gc = jiffies + NLM_HOST_COLLECT;
438 static LIST_HEAD(nsm_handles);
439 static DEFINE_MUTEX(nsm_mutex);
441 static struct nsm_handle *
442 __nsm_find(const struct sockaddr_in *sin,
443 const char *hostname, int hostname_len,
446 struct nsm_handle *nsm = NULL;
447 struct list_head *pos;
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);
461 mutex_lock(&nsm_mutex);
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))
469 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
471 atomic_inc(&nsm->sm_count);
480 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
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);
492 mutex_unlock(&nsm_mutex);
497 nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
499 return __nsm_find(sin, hostname, hostname_len, 1);
503 * Release an NSM handle
506 nsm_release(struct nsm_handle *nsm)
510 if (atomic_dec_and_test(&nsm->sm_count)) {
511 mutex_lock(&nsm_mutex);
512 if (atomic_read(&nsm->sm_count) == 0) {
513 list_del(&nsm->sm_link);
516 mutex_unlock(&nsm_mutex);