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/slab.h>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/svc.h>
16 #include <linux/lockd/lockd.h>
17 #include <linux/lockd/sm_inter.h>
18 #include <linux/mutex.h>
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)
29 static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
30 static unsigned long next_gc;
32 static DEFINE_MUTEX(nlm_host_mutex);
35 static void nlm_gc_hosts(void);
36 static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
37 const char *, int, int);
38 static struct nsm_handle * nsm_find(const struct sockaddr_in *sin,
43 * Common host lookup routine for server & client
45 static struct nlm_host *
46 nlm_lookup_host(int server, const struct sockaddr_in *sin,
47 int proto, int version,
51 struct hlist_head *chain;
52 struct hlist_node *pos;
53 struct nlm_host *host;
54 struct nsm_handle *nsm = NULL;
57 dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
58 NIPQUAD(sin->sin_addr.s_addr), proto, version,
59 server? "server" : "client",
61 hostname? hostname : "<none>");
64 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
67 mutex_lock(&nlm_host_mutex);
69 if (time_after_eq(jiffies, next_gc))
72 /* We may keep several nlm_host objects for a peer, because each
73 * nlm_host is identified by
74 * (address, protocol, version, server/client)
75 * We could probably simplify this a little by putting all those
76 * different NLM rpc_clients into one single nlm_host object.
77 * This would allow us to have one nlm_host per address.
79 chain = &nlm_hosts[hash];
80 hlist_for_each_entry(host, pos, chain, h_hash) {
81 if (!nlm_cmp_addr(&host->h_addr, sin))
84 /* See if we have an NSM handle for this client */
86 nsm = host->h_nsmhandle;
88 if (host->h_proto != proto)
90 if (host->h_version != version)
92 if (host->h_server != server)
95 /* Move to head of hash chain. */
96 hlist_del(&host->h_hash);
97 hlist_add_head(&host->h_hash, chain);
103 atomic_inc(&nsm->sm_count);
107 /* Sadly, the host isn't in our hash table yet. See if
108 * we have an NSM handle for it. If not, create one.
110 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
113 host = kzalloc(sizeof(*host), GFP_KERNEL);
118 host->h_name = nsm->sm_name;
120 host->h_addr.sin_port = 0; /* ouch! */
121 host->h_version = version;
122 host->h_proto = proto;
123 host->h_rpcclnt = NULL;
124 mutex_init(&host->h_mutex);
125 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
126 host->h_expires = jiffies + NLM_HOST_EXPIRE;
127 atomic_set(&host->h_count, 1);
128 init_waitqueue_head(&host->h_gracewait);
129 init_rwsem(&host->h_rwsem);
130 host->h_state = 0; /* pseudo NSM state */
131 host->h_nsmstate = 0; /* real NSM state */
132 host->h_nsmhandle = nsm;
133 host->h_server = server;
134 hlist_add_head(&host->h_hash, chain);
135 INIT_LIST_HEAD(&host->h_lockowners);
136 spin_lock_init(&host->h_lock);
137 INIT_LIST_HEAD(&host->h_granted);
138 INIT_LIST_HEAD(&host->h_reclaim);
140 if (++nrhosts > NLM_HOST_MAX)
144 mutex_unlock(&nlm_host_mutex);
152 nlm_destroy_host(struct nlm_host *host)
154 struct rpc_clnt *clnt;
156 BUG_ON(!list_empty(&host->h_lockowners));
157 BUG_ON(atomic_read(&host->h_count));
160 * Release NSM handle and unmonitor host.
164 clnt = host->h_rpcclnt;
166 rpc_shutdown_client(clnt);
171 * Find an NLM server handle in the cache. If there is none, create it.
174 nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
175 const char *hostname, int hostname_len)
177 return nlm_lookup_host(0, sin, proto, version,
178 hostname, hostname_len);
182 * Find an NLM client handle in the cache. If there is none, create it.
185 nlmsvc_lookup_host(struct svc_rqst *rqstp,
186 const char *hostname, int hostname_len)
188 return nlm_lookup_host(1, svc_addr_in(rqstp),
189 rqstp->rq_prot, rqstp->rq_vers,
190 hostname, hostname_len);
194 * Create the NLM RPC client for an NLM peer
197 nlm_bind_host(struct nlm_host *host)
199 struct rpc_clnt *clnt;
201 dprintk("lockd: nlm_bind_host(%08x)\n",
202 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
204 /* Lock host handle */
205 mutex_lock(&host->h_mutex);
207 /* If we've already created an RPC client, check whether
208 * RPC rebind is required
210 if ((clnt = host->h_rpcclnt) != NULL) {
211 if (time_after_eq(jiffies, host->h_nextrebind)) {
212 rpc_force_rebind(clnt);
213 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
214 dprintk("lockd: next rebind in %ld jiffies\n",
215 host->h_nextrebind - jiffies);
218 unsigned long increment = nlmsvc_timeout;
219 struct rpc_timeout timeparms = {
220 .to_initval = increment,
221 .to_increment = increment,
222 .to_maxval = increment * 6UL,
225 struct rpc_create_args args = {
226 .protocol = host->h_proto,
227 .address = (struct sockaddr *)&host->h_addr,
228 .addrsize = sizeof(host->h_addr),
229 .timeout = &timeparms,
230 .servername = host->h_name,
231 .program = &nlm_program,
232 .version = host->h_version,
233 .authflavor = RPC_AUTH_UNIX,
234 .flags = (RPC_CLNT_CREATE_HARDRTRY |
235 RPC_CLNT_CREATE_AUTOBIND),
238 clnt = rpc_create(&args);
240 host->h_rpcclnt = clnt;
242 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
247 mutex_unlock(&host->h_mutex);
252 * Force a portmap lookup of the remote lockd port
255 nlm_rebind_host(struct nlm_host *host)
257 dprintk("lockd: rebind host %s\n", host->h_name);
258 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
259 rpc_force_rebind(host->h_rpcclnt);
260 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
265 * Increment NLM host count
267 struct nlm_host * nlm_get_host(struct nlm_host *host)
270 dprintk("lockd: get host %s\n", host->h_name);
271 atomic_inc(&host->h_count);
272 host->h_expires = jiffies + NLM_HOST_EXPIRE;
278 * Release NLM host after use
280 void nlm_release_host(struct nlm_host *host)
283 dprintk("lockd: release host %s\n", host->h_name);
284 BUG_ON(atomic_read(&host->h_count) < 0);
285 if (atomic_dec_and_test(&host->h_count)) {
286 BUG_ON(!list_empty(&host->h_lockowners));
287 BUG_ON(!list_empty(&host->h_granted));
288 BUG_ON(!list_empty(&host->h_reclaim));
294 * We were notified that the host indicated by address &sin
296 * Release all resources held by that peer.
298 void nlm_host_rebooted(const struct sockaddr_in *sin,
299 const char *hostname, int hostname_len,
302 struct hlist_head *chain;
303 struct hlist_node *pos;
304 struct nsm_handle *nsm;
305 struct nlm_host *host;
307 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
308 hostname, NIPQUAD(sin->sin_addr));
310 /* Find the NSM handle for this peer */
311 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
314 /* When reclaiming locks on this peer, make sure that
315 * we set up a new notification */
316 nsm->sm_monitored = 0;
318 /* Mark all hosts tied to this NSM state as having rebooted.
319 * We run the loop repeatedly, because we drop the host table
321 * To avoid processing a host several times, we match the nsmstate.
323 again: mutex_lock(&nlm_host_mutex);
324 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
325 hlist_for_each_entry(host, pos, chain, h_hash) {
326 if (host->h_nsmhandle == nsm
327 && host->h_nsmstate != new_state) {
328 host->h_nsmstate = new_state;
332 mutex_unlock(&nlm_host_mutex);
334 if (host->h_server) {
335 /* We're server for this guy, just ditch
336 * all the locks he held. */
337 nlmsvc_free_host_resources(host);
339 /* He's the server, initiate lock recovery. */
340 nlmclnt_recovery(host);
343 nlm_release_host(host);
349 mutex_unlock(&nlm_host_mutex);
353 * Shut down the hosts module.
354 * Note that this routine is called only at server shutdown time.
357 nlm_shutdown_hosts(void)
359 struct hlist_head *chain;
360 struct hlist_node *pos;
361 struct nlm_host *host;
363 dprintk("lockd: shutting down host module\n");
364 mutex_lock(&nlm_host_mutex);
366 /* First, make all hosts eligible for gc */
367 dprintk("lockd: nuking all hosts...\n");
368 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
369 hlist_for_each_entry(host, pos, chain, h_hash)
370 host->h_expires = jiffies - 1;
373 /* Then, perform a garbage collection pass */
375 mutex_unlock(&nlm_host_mutex);
377 /* complain if any hosts are left */
379 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
380 dprintk("lockd: %d hosts left:\n", nrhosts);
381 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
382 hlist_for_each_entry(host, pos, chain, h_hash) {
383 dprintk(" %s (cnt %d use %d exp %ld)\n",
384 host->h_name, atomic_read(&host->h_count),
385 host->h_inuse, host->h_expires);
392 * Garbage collect any unused NLM hosts.
393 * This GC combines reference counting for async operations with
394 * mark & sweep for resources held by remote clients.
399 struct hlist_head *chain;
400 struct hlist_node *pos, *next;
401 struct nlm_host *host;
403 dprintk("lockd: host garbage collection\n");
404 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
405 hlist_for_each_entry(host, pos, chain, h_hash)
409 /* Mark all hosts that hold locks, blocks or shares */
410 nlmsvc_mark_resources();
412 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
413 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
414 if (atomic_read(&host->h_count) || host->h_inuse
415 || time_before(jiffies, host->h_expires)) {
416 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
417 host->h_name, atomic_read(&host->h_count),
418 host->h_inuse, host->h_expires);
421 dprintk("lockd: delete host %s\n", host->h_name);
422 hlist_del_init(&host->h_hash);
424 nlm_destroy_host(host);
429 next_gc = jiffies + NLM_HOST_COLLECT;
436 static LIST_HEAD(nsm_handles);
437 static DEFINE_MUTEX(nsm_mutex);
439 static struct nsm_handle *
440 __nsm_find(const struct sockaddr_in *sin,
441 const char *hostname, int hostname_len,
444 struct nsm_handle *nsm = NULL;
445 struct list_head *pos;
450 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
451 if (printk_ratelimit()) {
452 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
453 "in NFS lock request\n",
454 hostname_len, hostname);
459 mutex_lock(&nsm_mutex);
460 list_for_each(pos, &nsm_handles) {
461 nsm = list_entry(pos, struct nsm_handle, sm_link);
463 if (hostname && nsm_use_hostnames) {
464 if (strlen(nsm->sm_name) != hostname_len
465 || memcmp(nsm->sm_name, hostname, hostname_len))
467 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
469 atomic_inc(&nsm->sm_count);
478 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
481 nsm->sm_name = (char *) (nsm + 1);
482 memcpy(nsm->sm_name, hostname, hostname_len);
483 nsm->sm_name[hostname_len] = '\0';
484 atomic_set(&nsm->sm_count, 1);
486 list_add(&nsm->sm_link, &nsm_handles);
490 mutex_unlock(&nsm_mutex);
494 static struct nsm_handle *
495 nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
497 return __nsm_find(sin, hostname, hostname_len, 1);
501 * Release an NSM handle
504 nsm_release(struct nsm_handle *nsm)
508 if (atomic_dec_and_test(&nsm->sm_count)) {
509 mutex_lock(&nsm_mutex);
510 if (atomic_read(&nsm->sm_count) == 0) {
511 list_del(&nsm->sm_link);
514 mutex_unlock(&nsm_mutex);