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_NRHASH 32
23 #define NLM_ADDRHASH(addr) (ntohl(addr) & (NLM_HOST_NRHASH-1))
24 #define NLM_HOST_REBIND (60 * HZ)
25 #define NLM_HOST_EXPIRE (300 * HZ)
26 #define NLM_HOST_COLLECT (120 * HZ)
28 static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
29 static unsigned long next_gc;
31 static DEFINE_MUTEX(nlm_host_mutex);
34 static void nlm_gc_hosts(void);
35 static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
36 const char *, unsigned int, int);
37 static struct nsm_handle * nsm_find(const struct sockaddr_in *sin,
39 unsigned int hostname_len);
42 * Common host lookup routine for server & client
44 static struct nlm_host *nlm_lookup_host(int server,
45 const struct sockaddr_in *sin,
46 int proto, u32 version,
48 unsigned int hostname_len,
49 const struct sockaddr_in *ssin)
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("NIPQUAD_FMT"->"NIPQUAD_FMT
58 ", p=%d, v=%u, my role=%s, name=%.*s)\n",
59 NIPQUAD(ssin->sin_addr.s_addr),
60 NIPQUAD(sin->sin_addr.s_addr), proto, version,
61 server? "server" : "client",
63 hostname? hostname : "<none>");
66 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
69 mutex_lock(&nlm_host_mutex);
71 if (time_after_eq(jiffies, next_gc))
74 /* We may keep several nlm_host objects for a peer, because each
75 * nlm_host is identified by
76 * (address, protocol, version, server/client)
77 * We could probably simplify this a little by putting all those
78 * different NLM rpc_clients into one single nlm_host object.
79 * This would allow us to have one nlm_host per address.
81 chain = &nlm_hosts[hash];
82 hlist_for_each_entry(host, pos, chain, h_hash) {
83 if (!nlm_cmp_addr(&host->h_addr, sin))
86 /* See if we have an NSM handle for this client */
88 nsm = host->h_nsmhandle;
90 if (host->h_proto != proto)
92 if (host->h_version != version)
94 if (host->h_server != server)
96 if (!nlm_cmp_addr(&host->h_saddr, ssin))
99 /* Move to head of hash chain. */
100 hlist_del(&host->h_hash);
101 hlist_add_head(&host->h_hash, chain);
107 atomic_inc(&nsm->sm_count);
111 /* Sadly, the host isn't in our hash table yet. See if
112 * we have an NSM handle for it. If not, create one.
114 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
117 host = kzalloc(sizeof(*host), GFP_KERNEL);
122 host->h_name = nsm->sm_name;
124 host->h_addr.sin_port = 0; /* ouch! */
125 host->h_saddr = *ssin;
126 host->h_version = version;
127 host->h_proto = proto;
128 host->h_rpcclnt = NULL;
129 mutex_init(&host->h_mutex);
130 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
131 host->h_expires = jiffies + NLM_HOST_EXPIRE;
132 atomic_set(&host->h_count, 1);
133 init_waitqueue_head(&host->h_gracewait);
134 init_rwsem(&host->h_rwsem);
135 host->h_state = 0; /* pseudo NSM state */
136 host->h_nsmstate = 0; /* real NSM state */
137 host->h_nsmhandle = nsm;
138 host->h_server = server;
139 hlist_add_head(&host->h_hash, chain);
140 INIT_LIST_HEAD(&host->h_lockowners);
141 spin_lock_init(&host->h_lock);
142 INIT_LIST_HEAD(&host->h_granted);
143 INIT_LIST_HEAD(&host->h_reclaim);
147 mutex_unlock(&nlm_host_mutex);
155 nlm_destroy_host(struct nlm_host *host)
157 struct rpc_clnt *clnt;
159 BUG_ON(!list_empty(&host->h_lockowners));
160 BUG_ON(atomic_read(&host->h_count));
163 * Release NSM handle and unmonitor host.
167 clnt = host->h_rpcclnt;
169 rpc_shutdown_client(clnt);
174 * Find an NLM server handle in the cache. If there is none, create it.
176 struct nlm_host *nlmclnt_lookup_host(const struct sockaddr_in *sin,
177 int proto, u32 version,
178 const char *hostname,
179 unsigned int hostname_len)
181 struct sockaddr_in ssin = {0};
183 return nlm_lookup_host(0, sin, proto, version,
184 hostname, hostname_len, &ssin);
188 * Find an NLM client handle in the cache. If there is none, create it.
191 nlmsvc_lookup_host(struct svc_rqst *rqstp,
192 const char *hostname, unsigned int hostname_len)
194 struct sockaddr_in ssin = {0};
196 ssin.sin_addr = rqstp->rq_daddr.addr;
197 return nlm_lookup_host(1, svc_addr_in(rqstp),
198 rqstp->rq_prot, rqstp->rq_vers,
199 hostname, hostname_len, &ssin);
203 * Create the NLM RPC client for an NLM peer
206 nlm_bind_host(struct nlm_host *host)
208 struct rpc_clnt *clnt;
210 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT"->"NIPQUAD_FMT")\n",
211 NIPQUAD(host->h_saddr.sin_addr),
212 NIPQUAD(host->h_addr.sin_addr));
214 /* Lock host handle */
215 mutex_lock(&host->h_mutex);
217 /* If we've already created an RPC client, check whether
218 * RPC rebind is required
220 if ((clnt = host->h_rpcclnt) != NULL) {
221 if (time_after_eq(jiffies, host->h_nextrebind)) {
222 rpc_force_rebind(clnt);
223 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
224 dprintk("lockd: next rebind in %ld jiffies\n",
225 host->h_nextrebind - jiffies);
228 unsigned long increment = nlmsvc_timeout;
229 struct rpc_timeout timeparms = {
230 .to_initval = increment,
231 .to_increment = increment,
232 .to_maxval = increment * 6UL,
235 struct rpc_create_args args = {
236 .protocol = host->h_proto,
237 .address = (struct sockaddr *)&host->h_addr,
238 .addrsize = sizeof(host->h_addr),
239 .saddress = (struct sockaddr *)&host->h_saddr,
240 .timeout = &timeparms,
241 .servername = host->h_name,
242 .program = &nlm_program,
243 .version = host->h_version,
244 .authflavor = RPC_AUTH_UNIX,
245 .flags = (RPC_CLNT_CREATE_NOPING |
246 RPC_CLNT_CREATE_AUTOBIND),
250 * lockd retries server side blocks automatically so we want
251 * those to be soft RPC calls. Client side calls need to be
255 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
257 clnt = rpc_create(&args);
259 host->h_rpcclnt = clnt;
261 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
266 mutex_unlock(&host->h_mutex);
271 * Force a portmap lookup of the remote lockd port
274 nlm_rebind_host(struct nlm_host *host)
276 dprintk("lockd: rebind host %s\n", host->h_name);
277 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
278 rpc_force_rebind(host->h_rpcclnt);
279 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
284 * Increment NLM host count
286 struct nlm_host * nlm_get_host(struct nlm_host *host)
289 dprintk("lockd: get host %s\n", host->h_name);
290 atomic_inc(&host->h_count);
291 host->h_expires = jiffies + NLM_HOST_EXPIRE;
297 * Release NLM host after use
299 void nlm_release_host(struct nlm_host *host)
302 dprintk("lockd: release host %s\n", host->h_name);
303 BUG_ON(atomic_read(&host->h_count) < 0);
304 if (atomic_dec_and_test(&host->h_count)) {
305 BUG_ON(!list_empty(&host->h_lockowners));
306 BUG_ON(!list_empty(&host->h_granted));
307 BUG_ON(!list_empty(&host->h_reclaim));
313 * We were notified that the host indicated by address &sin
315 * Release all resources held by that peer.
317 void nlm_host_rebooted(const struct sockaddr_in *sin,
318 const char *hostname,
319 unsigned int hostname_len,
322 struct hlist_head *chain;
323 struct hlist_node *pos;
324 struct nsm_handle *nsm;
325 struct nlm_host *host;
327 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
328 hostname, NIPQUAD(sin->sin_addr));
330 /* Find the NSM handle for this peer */
331 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
334 /* When reclaiming locks on this peer, make sure that
335 * we set up a new notification */
336 nsm->sm_monitored = 0;
338 /* Mark all hosts tied to this NSM state as having rebooted.
339 * We run the loop repeatedly, because we drop the host table
341 * To avoid processing a host several times, we match the nsmstate.
343 again: mutex_lock(&nlm_host_mutex);
344 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
345 hlist_for_each_entry(host, pos, chain, h_hash) {
346 if (host->h_nsmhandle == nsm
347 && host->h_nsmstate != new_state) {
348 host->h_nsmstate = new_state;
352 mutex_unlock(&nlm_host_mutex);
354 if (host->h_server) {
355 /* We're server for this guy, just ditch
356 * all the locks he held. */
357 nlmsvc_free_host_resources(host);
359 /* He's the server, initiate lock recovery. */
360 nlmclnt_recovery(host);
363 nlm_release_host(host);
369 mutex_unlock(&nlm_host_mutex);
373 * Shut down the hosts module.
374 * Note that this routine is called only at server shutdown time.
377 nlm_shutdown_hosts(void)
379 struct hlist_head *chain;
380 struct hlist_node *pos;
381 struct nlm_host *host;
383 dprintk("lockd: shutting down host module\n");
384 mutex_lock(&nlm_host_mutex);
386 /* First, make all hosts eligible for gc */
387 dprintk("lockd: nuking all hosts...\n");
388 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
389 hlist_for_each_entry(host, pos, chain, h_hash) {
390 host->h_expires = jiffies - 1;
391 if (host->h_rpcclnt) {
392 rpc_shutdown_client(host->h_rpcclnt);
393 host->h_rpcclnt = NULL;
398 /* Then, perform a garbage collection pass */
400 mutex_unlock(&nlm_host_mutex);
402 /* complain if any hosts are left */
404 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
405 dprintk("lockd: %d hosts left:\n", nrhosts);
406 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
407 hlist_for_each_entry(host, pos, chain, h_hash) {
408 dprintk(" %s (cnt %d use %d exp %ld)\n",
409 host->h_name, atomic_read(&host->h_count),
410 host->h_inuse, host->h_expires);
417 * Garbage collect any unused NLM hosts.
418 * This GC combines reference counting for async operations with
419 * mark & sweep for resources held by remote clients.
424 struct hlist_head *chain;
425 struct hlist_node *pos, *next;
426 struct nlm_host *host;
428 dprintk("lockd: host garbage collection\n");
429 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
430 hlist_for_each_entry(host, pos, chain, h_hash)
434 /* Mark all hosts that hold locks, blocks or shares */
435 nlmsvc_mark_resources();
437 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
438 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
439 if (atomic_read(&host->h_count) || host->h_inuse
440 || time_before(jiffies, host->h_expires)) {
441 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
442 host->h_name, atomic_read(&host->h_count),
443 host->h_inuse, host->h_expires);
446 dprintk("lockd: delete host %s\n", host->h_name);
447 hlist_del_init(&host->h_hash);
449 nlm_destroy_host(host);
454 next_gc = jiffies + NLM_HOST_COLLECT;
461 static LIST_HEAD(nsm_handles);
462 static DEFINE_SPINLOCK(nsm_lock);
464 static struct nsm_handle *
465 __nsm_find(const struct sockaddr_in *sin,
466 const char *hostname, unsigned int hostname_len,
469 struct nsm_handle *nsm = NULL;
470 struct nsm_handle *pos;
475 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
476 if (printk_ratelimit()) {
477 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
478 "in NFS lock request\n",
479 hostname_len, hostname);
485 spin_lock(&nsm_lock);
486 list_for_each_entry(pos, &nsm_handles, sm_link) {
488 if (hostname && nsm_use_hostnames) {
489 if (strlen(pos->sm_name) != hostname_len
490 || memcmp(pos->sm_name, hostname, hostname_len))
492 } else if (!nlm_cmp_addr(&pos->sm_addr, sin))
494 atomic_inc(&pos->sm_count);
500 list_add(&nsm->sm_link, &nsm_handles);
503 spin_unlock(&nsm_lock);
508 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
513 nsm->sm_name = (char *) (nsm + 1);
514 memcpy(nsm->sm_name, hostname, hostname_len);
515 nsm->sm_name[hostname_len] = '\0';
516 atomic_set(&nsm->sm_count, 1);
520 spin_unlock(&nsm_lock);
524 static struct nsm_handle *
525 nsm_find(const struct sockaddr_in *sin, const char *hostname,
526 unsigned int hostname_len)
528 return __nsm_find(sin, hostname, hostname_len, 1);
532 * Release an NSM handle
535 nsm_release(struct nsm_handle *nsm)
539 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
540 list_del(&nsm->sm_link);
541 spin_unlock(&nsm_lock);