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 *, unsigned int, int);
38 static struct nsm_handle * nsm_find(const struct sockaddr_in *sin,
40 unsigned int hostname_len);
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, const char *hostname,
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=%d, 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);
145 if (++nrhosts > NLM_HOST_MAX)
149 mutex_unlock(&nlm_host_mutex);
157 nlm_destroy_host(struct nlm_host *host)
159 struct rpc_clnt *clnt;
161 BUG_ON(!list_empty(&host->h_lockowners));
162 BUG_ON(atomic_read(&host->h_count));
165 * Release NSM handle and unmonitor host.
169 clnt = host->h_rpcclnt;
171 rpc_shutdown_client(clnt);
176 * Find an NLM server handle in the cache. If there is none, create it.
179 nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
180 const char *hostname, unsigned int hostname_len)
182 struct sockaddr_in ssin = {0};
184 return nlm_lookup_host(0, sin, proto, version,
185 hostname, hostname_len, &ssin);
189 * Find an NLM client handle in the cache. If there is none, create it.
192 nlmsvc_lookup_host(struct svc_rqst *rqstp,
193 const char *hostname, unsigned int hostname_len)
195 struct sockaddr_in ssin = {0};
197 ssin.sin_addr = rqstp->rq_daddr.addr;
198 return nlm_lookup_host(1, svc_addr_in(rqstp),
199 rqstp->rq_prot, rqstp->rq_vers,
200 hostname, hostname_len, &ssin);
204 * Create the NLM RPC client for an NLM peer
207 nlm_bind_host(struct nlm_host *host)
209 struct rpc_clnt *clnt;
211 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT"->"NIPQUAD_FMT")\n",
212 NIPQUAD(host->h_saddr.sin_addr),
213 NIPQUAD(host->h_addr.sin_addr));
215 /* Lock host handle */
216 mutex_lock(&host->h_mutex);
218 /* If we've already created an RPC client, check whether
219 * RPC rebind is required
221 if ((clnt = host->h_rpcclnt) != NULL) {
222 if (time_after_eq(jiffies, host->h_nextrebind)) {
223 rpc_force_rebind(clnt);
224 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
225 dprintk("lockd: next rebind in %ld jiffies\n",
226 host->h_nextrebind - jiffies);
229 unsigned long increment = nlmsvc_timeout;
230 struct rpc_timeout timeparms = {
231 .to_initval = increment,
232 .to_increment = increment,
233 .to_maxval = increment * 6UL,
236 struct rpc_create_args args = {
237 .protocol = host->h_proto,
238 .address = (struct sockaddr *)&host->h_addr,
239 .addrsize = sizeof(host->h_addr),
240 .saddress = (struct sockaddr *)&host->h_saddr,
241 .timeout = &timeparms,
242 .servername = host->h_name,
243 .program = &nlm_program,
244 .version = host->h_version,
245 .authflavor = RPC_AUTH_UNIX,
246 .flags = (RPC_CLNT_CREATE_HARDRTRY |
247 RPC_CLNT_CREATE_NOPING |
248 RPC_CLNT_CREATE_AUTOBIND),
251 clnt = rpc_create(&args);
253 host->h_rpcclnt = clnt;
255 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
260 mutex_unlock(&host->h_mutex);
265 * Force a portmap lookup of the remote lockd port
268 nlm_rebind_host(struct nlm_host *host)
270 dprintk("lockd: rebind host %s\n", host->h_name);
271 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
272 rpc_force_rebind(host->h_rpcclnt);
273 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
278 * Increment NLM host count
280 struct nlm_host * nlm_get_host(struct nlm_host *host)
283 dprintk("lockd: get host %s\n", host->h_name);
284 atomic_inc(&host->h_count);
285 host->h_expires = jiffies + NLM_HOST_EXPIRE;
291 * Release NLM host after use
293 void nlm_release_host(struct nlm_host *host)
296 dprintk("lockd: release host %s\n", host->h_name);
297 BUG_ON(atomic_read(&host->h_count) < 0);
298 if (atomic_dec_and_test(&host->h_count)) {
299 BUG_ON(!list_empty(&host->h_lockowners));
300 BUG_ON(!list_empty(&host->h_granted));
301 BUG_ON(!list_empty(&host->h_reclaim));
307 * We were notified that the host indicated by address &sin
309 * Release all resources held by that peer.
311 void nlm_host_rebooted(const struct sockaddr_in *sin,
312 const char *hostname,
313 unsigned int hostname_len,
316 struct hlist_head *chain;
317 struct hlist_node *pos;
318 struct nsm_handle *nsm;
319 struct nlm_host *host;
321 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
322 hostname, NIPQUAD(sin->sin_addr));
324 /* Find the NSM handle for this peer */
325 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
328 /* When reclaiming locks on this peer, make sure that
329 * we set up a new notification */
330 nsm->sm_monitored = 0;
332 /* Mark all hosts tied to this NSM state as having rebooted.
333 * We run the loop repeatedly, because we drop the host table
335 * To avoid processing a host several times, we match the nsmstate.
337 again: mutex_lock(&nlm_host_mutex);
338 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
339 hlist_for_each_entry(host, pos, chain, h_hash) {
340 if (host->h_nsmhandle == nsm
341 && host->h_nsmstate != new_state) {
342 host->h_nsmstate = new_state;
346 mutex_unlock(&nlm_host_mutex);
348 if (host->h_server) {
349 /* We're server for this guy, just ditch
350 * all the locks he held. */
351 nlmsvc_free_host_resources(host);
353 /* He's the server, initiate lock recovery. */
354 nlmclnt_recovery(host);
357 nlm_release_host(host);
363 mutex_unlock(&nlm_host_mutex);
367 * Shut down the hosts module.
368 * Note that this routine is called only at server shutdown time.
371 nlm_shutdown_hosts(void)
373 struct hlist_head *chain;
374 struct hlist_node *pos;
375 struct nlm_host *host;
377 dprintk("lockd: shutting down host module\n");
378 mutex_lock(&nlm_host_mutex);
380 /* First, make all hosts eligible for gc */
381 dprintk("lockd: nuking all hosts...\n");
382 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
383 hlist_for_each_entry(host, pos, chain, h_hash) {
384 host->h_expires = jiffies - 1;
385 if (host->h_rpcclnt) {
386 rpc_shutdown_client(host->h_rpcclnt);
387 host->h_rpcclnt = NULL;
392 /* Then, perform a garbage collection pass */
394 mutex_unlock(&nlm_host_mutex);
396 /* complain if any hosts are left */
398 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
399 dprintk("lockd: %d hosts left:\n", nrhosts);
400 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
401 hlist_for_each_entry(host, pos, chain, h_hash) {
402 dprintk(" %s (cnt %d use %d exp %ld)\n",
403 host->h_name, atomic_read(&host->h_count),
404 host->h_inuse, host->h_expires);
411 * Garbage collect any unused NLM hosts.
412 * This GC combines reference counting for async operations with
413 * mark & sweep for resources held by remote clients.
418 struct hlist_head *chain;
419 struct hlist_node *pos, *next;
420 struct nlm_host *host;
422 dprintk("lockd: host garbage collection\n");
423 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
424 hlist_for_each_entry(host, pos, chain, h_hash)
428 /* Mark all hosts that hold locks, blocks or shares */
429 nlmsvc_mark_resources();
431 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
432 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
433 if (atomic_read(&host->h_count) || host->h_inuse
434 || time_before(jiffies, host->h_expires)) {
435 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
436 host->h_name, atomic_read(&host->h_count),
437 host->h_inuse, host->h_expires);
440 dprintk("lockd: delete host %s\n", host->h_name);
441 hlist_del_init(&host->h_hash);
443 nlm_destroy_host(host);
448 next_gc = jiffies + NLM_HOST_COLLECT;
455 static LIST_HEAD(nsm_handles);
456 static DEFINE_MUTEX(nsm_mutex);
458 static struct nsm_handle *
459 __nsm_find(const struct sockaddr_in *sin,
460 const char *hostname, unsigned int hostname_len,
463 struct nsm_handle *nsm = NULL;
464 struct list_head *pos;
469 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
470 if (printk_ratelimit()) {
471 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
472 "in NFS lock request\n",
473 hostname_len, hostname);
478 mutex_lock(&nsm_mutex);
479 list_for_each(pos, &nsm_handles) {
480 nsm = list_entry(pos, struct nsm_handle, sm_link);
482 if (hostname && nsm_use_hostnames) {
483 if (strlen(nsm->sm_name) != hostname_len
484 || memcmp(nsm->sm_name, hostname, hostname_len))
486 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
488 atomic_inc(&nsm->sm_count);
497 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
500 nsm->sm_name = (char *) (nsm + 1);
501 memcpy(nsm->sm_name, hostname, hostname_len);
502 nsm->sm_name[hostname_len] = '\0';
503 atomic_set(&nsm->sm_count, 1);
505 list_add(&nsm->sm_link, &nsm_handles);
509 mutex_unlock(&nsm_mutex);
513 static struct nsm_handle *
514 nsm_find(const struct sockaddr_in *sin, const char *hostname,
515 unsigned int hostname_len)
517 return __nsm_find(sin, hostname, hostname_len, 1);
521 * Release an NSM handle
524 nsm_release(struct nsm_handle *nsm)
528 if (atomic_dec_and_test(&nsm->sm_count)) {
529 mutex_lock(&nsm_mutex);
530 if (atomic_read(&nsm->sm_count) == 0) {
531 list_del(&nsm->sm_link);
534 mutex_unlock(&nsm_mutex);