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/in6.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>
23 #define NLMDBG_FACILITY NLMDBG_HOSTCACHE
24 #define NLM_HOST_NRHASH 32
25 #define NLM_HOST_REBIND (60 * HZ)
26 #define NLM_HOST_EXPIRE (300 * HZ)
27 #define NLM_HOST_COLLECT (120 * HZ)
29 static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
30 static unsigned long next_gc;
32 static DEFINE_MUTEX(nlm_host_mutex);
34 static void nlm_gc_hosts(void);
35 static struct nsm_handle *nsm_find(const struct sockaddr_in *sin,
37 const size_t hostname_len,
41 * Hash function must work well on big- and little-endian platforms
43 static unsigned int __nlm_hash32(const __be32 n)
45 unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
46 return hash ^ (hash >> 8);
49 static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
51 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
52 return __nlm_hash32(sin->sin_addr.s_addr);
55 static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
57 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
58 const struct in6_addr addr = sin6->sin6_addr;
59 return __nlm_hash32(addr.s6_addr32[0]) ^
60 __nlm_hash32(addr.s6_addr32[1]) ^
61 __nlm_hash32(addr.s6_addr32[2]) ^
62 __nlm_hash32(addr.s6_addr32[3]);
65 static unsigned int nlm_hash_address(const struct sockaddr *sap)
69 switch (sap->sa_family) {
71 hash = __nlm_hash_addr4(sap);
74 hash = __nlm_hash_addr6(sap);
79 return hash & (NLM_HOST_NRHASH - 1);
82 static void nlm_clear_port(struct sockaddr *sap)
84 switch (sap->sa_family) {
86 ((struct sockaddr_in *)sap)->sin_port = 0;
89 ((struct sockaddr_in6 *)sap)->sin6_port = 0;
94 static void nlm_display_address(const struct sockaddr *sap,
95 char *buf, const size_t len)
97 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
98 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
100 switch (sap->sa_family) {
102 snprintf(buf, len, "unspecified");
105 snprintf(buf, len, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
108 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
109 snprintf(buf, len, NIPQUAD_FMT,
110 NIPQUAD(sin6->sin6_addr.s6_addr32[3]));
112 snprintf(buf, len, NIP6_FMT, NIP6(sin6->sin6_addr));
115 snprintf(buf, len, "unsupported address family");
121 * Common host lookup routine for server & client
123 static struct nlm_host *nlm_lookup_host(int server,
124 const struct sockaddr_in *sin,
125 int proto, u32 version,
126 const char *hostname,
127 unsigned int hostname_len,
128 const struct sockaddr_in *ssin)
130 struct hlist_head *chain;
131 struct hlist_node *pos;
132 struct nlm_host *host;
133 struct nsm_handle *nsm = NULL;
135 dprintk("lockd: nlm_lookup_host(proto=%d, vers=%u,"
136 " my role is %s, hostname=%.*s)\n",
137 proto, version, server ? "server" : "client",
138 hostname_len, hostname ? hostname : "<none>");
140 mutex_lock(&nlm_host_mutex);
142 if (time_after_eq(jiffies, next_gc))
145 /* We may keep several nlm_host objects for a peer, because each
146 * nlm_host is identified by
147 * (address, protocol, version, server/client)
148 * We could probably simplify this a little by putting all those
149 * different NLM rpc_clients into one single nlm_host object.
150 * This would allow us to have one nlm_host per address.
152 chain = &nlm_hosts[nlm_hash_address((struct sockaddr *)sin)];
153 hlist_for_each_entry(host, pos, chain, h_hash) {
154 if (!nlm_cmp_addr(nlm_addr(host), (struct sockaddr *)sin))
157 /* See if we have an NSM handle for this client */
159 nsm = host->h_nsmhandle;
161 if (host->h_proto != proto)
163 if (host->h_version != version)
165 if (host->h_server != server)
167 if (!nlm_cmp_addr(nlm_srcaddr(host), (struct sockaddr *)ssin))
170 /* Move to head of hash chain. */
171 hlist_del(&host->h_hash);
172 hlist_add_head(&host->h_hash, chain);
175 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
176 host->h_name, host->h_addrbuf);
181 * The host wasn't in our hash table. If we don't
182 * have an NSM handle for it yet, create one.
185 atomic_inc(&nsm->sm_count);
188 nsm = nsm_find(sin, hostname, hostname_len, 1);
190 dprintk("lockd: nlm_lookup_host failed; "
196 host = kzalloc(sizeof(*host), GFP_KERNEL);
199 dprintk("lockd: nlm_lookup_host failed; no memory\n");
202 host->h_name = nsm->sm_name;
203 memcpy(nlm_addr(host), sin, sizeof(*sin));
204 host->h_addrlen = sizeof(*sin);
205 nlm_clear_port(nlm_addr(host));
206 memcpy(nlm_srcaddr(host), ssin, sizeof(*ssin));
207 host->h_version = version;
208 host->h_proto = proto;
209 host->h_rpcclnt = NULL;
210 mutex_init(&host->h_mutex);
211 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
212 host->h_expires = jiffies + NLM_HOST_EXPIRE;
213 atomic_set(&host->h_count, 1);
214 init_waitqueue_head(&host->h_gracewait);
215 init_rwsem(&host->h_rwsem);
216 host->h_state = 0; /* pseudo NSM state */
217 host->h_nsmstate = 0; /* real NSM state */
218 host->h_nsmhandle = nsm;
219 host->h_server = server;
220 hlist_add_head(&host->h_hash, chain);
221 INIT_LIST_HEAD(&host->h_lockowners);
222 spin_lock_init(&host->h_lock);
223 INIT_LIST_HEAD(&host->h_granted);
224 INIT_LIST_HEAD(&host->h_reclaim);
228 nlm_display_address((struct sockaddr *)&host->h_addr,
229 host->h_addrbuf, sizeof(host->h_addrbuf));
230 nlm_display_address((struct sockaddr *)&host->h_srcaddr,
231 host->h_srcaddrbuf, sizeof(host->h_srcaddrbuf));
233 dprintk("lockd: nlm_lookup_host created host %s\n",
237 mutex_unlock(&nlm_host_mutex);
245 nlm_destroy_host(struct nlm_host *host)
247 struct rpc_clnt *clnt;
249 BUG_ON(!list_empty(&host->h_lockowners));
250 BUG_ON(atomic_read(&host->h_count));
253 * Release NSM handle and unmonitor host.
257 clnt = host->h_rpcclnt;
259 rpc_shutdown_client(clnt);
264 * Find an NLM server handle in the cache. If there is none, create it.
266 struct nlm_host *nlmclnt_lookup_host(const struct sockaddr_in *sin,
267 int proto, u32 version,
268 const char *hostname,
269 unsigned int hostname_len)
271 const struct sockaddr_in source = {
272 .sin_family = AF_UNSPEC,
275 return nlm_lookup_host(0, sin, proto, version,
276 hostname, hostname_len, &source);
280 * Find an NLM client handle in the cache. If there is none, create it.
283 nlmsvc_lookup_host(struct svc_rqst *rqstp,
284 const char *hostname, unsigned int hostname_len)
286 const struct sockaddr_in source = {
287 .sin_family = AF_INET,
288 .sin_addr = rqstp->rq_daddr.addr,
291 return nlm_lookup_host(1, svc_addr_in(rqstp),
292 rqstp->rq_prot, rqstp->rq_vers,
293 hostname, hostname_len, &source);
297 * Create the NLM RPC client for an NLM peer
300 nlm_bind_host(struct nlm_host *host)
302 struct rpc_clnt *clnt;
304 dprintk("lockd: nlm_bind_host %s (%s), my addr=%s\n",
305 host->h_name, host->h_addrbuf, host->h_srcaddrbuf);
307 /* Lock host handle */
308 mutex_lock(&host->h_mutex);
310 /* If we've already created an RPC client, check whether
311 * RPC rebind is required
313 if ((clnt = host->h_rpcclnt) != NULL) {
314 if (time_after_eq(jiffies, host->h_nextrebind)) {
315 rpc_force_rebind(clnt);
316 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
317 dprintk("lockd: next rebind in %lu jiffies\n",
318 host->h_nextrebind - jiffies);
321 unsigned long increment = nlmsvc_timeout;
322 struct rpc_timeout timeparms = {
323 .to_initval = increment,
324 .to_increment = increment,
325 .to_maxval = increment * 6UL,
328 struct rpc_create_args args = {
329 .protocol = host->h_proto,
330 .address = nlm_addr(host),
331 .addrsize = host->h_addrlen,
332 .saddress = nlm_srcaddr(host),
333 .timeout = &timeparms,
334 .servername = host->h_name,
335 .program = &nlm_program,
336 .version = host->h_version,
337 .authflavor = RPC_AUTH_UNIX,
338 .flags = (RPC_CLNT_CREATE_NOPING |
339 RPC_CLNT_CREATE_AUTOBIND),
343 * lockd retries server side blocks automatically so we want
344 * those to be soft RPC calls. Client side calls need to be
348 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
350 clnt = rpc_create(&args);
352 host->h_rpcclnt = clnt;
354 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
359 mutex_unlock(&host->h_mutex);
364 * Force a portmap lookup of the remote lockd port
367 nlm_rebind_host(struct nlm_host *host)
369 dprintk("lockd: rebind host %s\n", host->h_name);
370 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
371 rpc_force_rebind(host->h_rpcclnt);
372 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
377 * Increment NLM host count
379 struct nlm_host * nlm_get_host(struct nlm_host *host)
382 dprintk("lockd: get host %s\n", host->h_name);
383 atomic_inc(&host->h_count);
384 host->h_expires = jiffies + NLM_HOST_EXPIRE;
390 * Release NLM host after use
392 void nlm_release_host(struct nlm_host *host)
395 dprintk("lockd: release host %s\n", host->h_name);
396 BUG_ON(atomic_read(&host->h_count) < 0);
397 if (atomic_dec_and_test(&host->h_count)) {
398 BUG_ON(!list_empty(&host->h_lockowners));
399 BUG_ON(!list_empty(&host->h_granted));
400 BUG_ON(!list_empty(&host->h_reclaim));
406 * We were notified that the host indicated by address &sin
408 * Release all resources held by that peer.
410 void nlm_host_rebooted(const struct sockaddr_in *sin,
411 const char *hostname,
412 unsigned int hostname_len,
415 struct hlist_head *chain;
416 struct hlist_node *pos;
417 struct nsm_handle *nsm;
418 struct nlm_host *host;
420 nsm = nsm_find(sin, hostname, hostname_len, 0);
422 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
423 hostname_len, hostname);
427 dprintk("lockd: nlm_host_rebooted(%.*s, %s)\n",
428 hostname_len, hostname, nsm->sm_addrbuf);
430 /* When reclaiming locks on this peer, make sure that
431 * we set up a new notification */
432 nsm->sm_monitored = 0;
434 /* Mark all hosts tied to this NSM state as having rebooted.
435 * We run the loop repeatedly, because we drop the host table
437 * To avoid processing a host several times, we match the nsmstate.
439 again: mutex_lock(&nlm_host_mutex);
440 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
441 hlist_for_each_entry(host, pos, chain, h_hash) {
442 if (host->h_nsmhandle == nsm
443 && host->h_nsmstate != new_state) {
444 host->h_nsmstate = new_state;
448 mutex_unlock(&nlm_host_mutex);
450 if (host->h_server) {
451 /* We're server for this guy, just ditch
452 * all the locks he held. */
453 nlmsvc_free_host_resources(host);
455 /* He's the server, initiate lock recovery. */
456 nlmclnt_recovery(host);
459 nlm_release_host(host);
465 mutex_unlock(&nlm_host_mutex);
469 * Shut down the hosts module.
470 * Note that this routine is called only at server shutdown time.
473 nlm_shutdown_hosts(void)
475 struct hlist_head *chain;
476 struct hlist_node *pos;
477 struct nlm_host *host;
479 dprintk("lockd: shutting down host module\n");
480 mutex_lock(&nlm_host_mutex);
482 /* First, make all hosts eligible for gc */
483 dprintk("lockd: nuking all hosts...\n");
484 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
485 hlist_for_each_entry(host, pos, chain, h_hash) {
486 host->h_expires = jiffies - 1;
487 if (host->h_rpcclnt) {
488 rpc_shutdown_client(host->h_rpcclnt);
489 host->h_rpcclnt = NULL;
494 /* Then, perform a garbage collection pass */
496 mutex_unlock(&nlm_host_mutex);
498 /* complain if any hosts are left */
500 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
501 dprintk("lockd: %d hosts left:\n", nrhosts);
502 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
503 hlist_for_each_entry(host, pos, chain, h_hash) {
504 dprintk(" %s (cnt %d use %d exp %ld)\n",
505 host->h_name, atomic_read(&host->h_count),
506 host->h_inuse, host->h_expires);
513 * Garbage collect any unused NLM hosts.
514 * This GC combines reference counting for async operations with
515 * mark & sweep for resources held by remote clients.
520 struct hlist_head *chain;
521 struct hlist_node *pos, *next;
522 struct nlm_host *host;
524 dprintk("lockd: host garbage collection\n");
525 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
526 hlist_for_each_entry(host, pos, chain, h_hash)
530 /* Mark all hosts that hold locks, blocks or shares */
531 nlmsvc_mark_resources();
533 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
534 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
535 if (atomic_read(&host->h_count) || host->h_inuse
536 || time_before(jiffies, host->h_expires)) {
537 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
538 host->h_name, atomic_read(&host->h_count),
539 host->h_inuse, host->h_expires);
542 dprintk("lockd: delete host %s\n", host->h_name);
543 hlist_del_init(&host->h_hash);
545 nlm_destroy_host(host);
550 next_gc = jiffies + NLM_HOST_COLLECT;
557 static LIST_HEAD(nsm_handles);
558 static DEFINE_SPINLOCK(nsm_lock);
560 static struct nsm_handle *nsm_find(const struct sockaddr_in *sin,
561 const char *hostname,
562 const size_t hostname_len,
565 struct nsm_handle *nsm = NULL;
566 struct nsm_handle *pos;
571 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
572 if (printk_ratelimit()) {
573 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
574 "in NFS lock request\n",
575 (int)hostname_len, hostname);
581 spin_lock(&nsm_lock);
582 list_for_each_entry(pos, &nsm_handles, sm_link) {
584 if (hostname && nsm_use_hostnames) {
585 if (strlen(pos->sm_name) != hostname_len
586 || memcmp(pos->sm_name, hostname, hostname_len))
588 } else if (!nlm_cmp_addr(nsm_addr(pos), (struct sockaddr *)sin))
590 atomic_inc(&pos->sm_count);
596 list_add(&nsm->sm_link, &nsm_handles);
599 spin_unlock(&nsm_lock);
604 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
608 memcpy(nsm_addr(nsm), sin, sizeof(*sin));
609 nsm->sm_addrlen = sizeof(*sin);
610 nsm->sm_name = (char *) (nsm + 1);
611 memcpy(nsm->sm_name, hostname, hostname_len);
612 nsm->sm_name[hostname_len] = '\0';
613 nlm_display_address((struct sockaddr *)&nsm->sm_addr,
614 nsm->sm_addrbuf, sizeof(nsm->sm_addrbuf));
615 atomic_set(&nsm->sm_count, 1);
619 spin_unlock(&nsm_lock);
624 * Release an NSM handle
627 nsm_release(struct nsm_handle *nsm)
631 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
632 list_del(&nsm->sm_link);
633 spin_unlock(&nsm_lock);