blob: c3f119426d832ea396ebffbcd2820e335477bd8b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/host.c
3 *
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.
7 *
8 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9 */
10
11#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
13#include <linux/in.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>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080018#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20
21#define NLMDBG_FACILITY NLMDBG_HOSTCACHE
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define NLM_HOST_NRHASH 32
23#define NLM_ADDRHASH(addr) (ntohl(addr) & (NLM_HOST_NRHASH-1))
24#define NLM_HOST_REBIND (60 * HZ)
NeilBrown1447d252008-02-08 13:03:37 +110025#define NLM_HOST_EXPIRE (300 * HZ)
26#define NLM_HOST_COLLECT (120 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Olaf Kirch0cea3272006-10-04 02:15:56 -070028static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static unsigned long next_gc;
30static int nrhosts;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080031static DEFINE_MUTEX(nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33
34static void nlm_gc_hosts(void);
Olaf Kirch8dead0d2006-10-04 02:15:53 -070035static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
Chuck Lever48df0202007-11-01 16:56:53 -040036 const char *, unsigned int, int);
Adrian Bunkc5856462006-12-06 20:38:29 -080037static struct nsm_handle * nsm_find(const struct sockaddr_in *sin,
38 const char *hostname,
Chuck Lever48df0202007-11-01 16:56:53 -040039 unsigned int hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Common host lookup routine for server & client
43 */
Adrian Bunkc5856462006-12-06 20:38:29 -080044static struct nlm_host *
Olaf Kirchcf712c22006-10-04 02:15:52 -070045nlm_lookup_host(int server, const struct sockaddr_in *sin,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020046 int proto, int version, const char *hostname,
Chuck Lever48df0202007-11-01 16:56:53 -040047 unsigned int hostname_len,
48 const struct sockaddr_in *ssin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Olaf Kirch0cea3272006-10-04 02:15:56 -070050 struct hlist_head *chain;
51 struct hlist_node *pos;
52 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070053 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int hash;
55
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020056 dprintk("lockd: nlm_lookup_host("NIPQUAD_FMT"->"NIPQUAD_FMT
57 ", p=%d, v=%d, my role=%s, name=%.*s)\n",
58 NIPQUAD(ssin->sin_addr.s_addr),
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070059 NIPQUAD(sin->sin_addr.s_addr), proto, version,
60 server? "server" : "client",
61 hostname_len,
62 hostname? hostname : "<none>");
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
66
67 /* Lock hash table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080068 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 if (time_after_eq(jiffies, next_gc))
71 nlm_gc_hosts();
72
Olaf Kirch8dead0d2006-10-04 02:15:53 -070073 /* We may keep several nlm_host objects for a peer, because each
74 * nlm_host is identified by
75 * (address, protocol, version, server/client)
76 * We could probably simplify this a little by putting all those
77 * different NLM rpc_clients into one single nlm_host object.
78 * This would allow us to have one nlm_host per address.
79 */
Olaf Kirch0cea3272006-10-04 02:15:56 -070080 chain = &nlm_hosts[hash];
81 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch8dead0d2006-10-04 02:15:53 -070082 if (!nlm_cmp_addr(&host->h_addr, sin))
83 continue;
84
85 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -070086 if (!nsm)
87 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (host->h_proto != proto)
90 continue;
91 if (host->h_version != version)
92 continue;
93 if (host->h_server != server)
94 continue;
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020095 if (!nlm_cmp_addr(&host->h_saddr, ssin))
96 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Olaf Kirch0cea3272006-10-04 02:15:56 -070098 /* Move to head of hash chain. */
99 hlist_del(&host->h_hash);
100 hlist_add_head(&host->h_hash, chain);
101
Olaf Kirchf0737a32006-10-04 02:15:54 -0700102 nlm_get_host(host);
103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
NeilBrown6b54dae2006-10-04 02:16:15 -0700105 if (nsm)
106 atomic_inc(&nsm->sm_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Olaf Kirch0cea3272006-10-04 02:15:56 -0700108 host = NULL;
109
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700110 /* Sadly, the host isn't in our hash table yet. See if
111 * we have an NSM handle for it. If not, create one.
112 */
113 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
114 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700116 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700117 if (!host) {
118 nsm_release(nsm);
119 goto out;
120 }
121 host->h_name = nsm->sm_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 host->h_addr = *sin;
123 host->h_addr.sin_port = 0; /* ouch! */
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200124 host->h_saddr = *ssin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 host->h_version = version;
126 host->h_proto = proto;
127 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400128 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
130 host->h_expires = jiffies + NLM_HOST_EXPIRE;
131 atomic_set(&host->h_count, 1);
132 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400133 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 host->h_state = 0; /* pseudo NSM state */
135 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700136 host->h_nsmhandle = nsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 host->h_server = server;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700138 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 INIT_LIST_HEAD(&host->h_lockowners);
140 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500141 INIT_LIST_HEAD(&host->h_granted);
142 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
NeilBrown1447d252008-02-08 13:03:37 +1100144 nrhosts++;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700145out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800146 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return host;
148}
149
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700150/*
151 * Destroy a host
152 */
153static void
154nlm_destroy_host(struct nlm_host *host)
155{
156 struct rpc_clnt *clnt;
157
158 BUG_ON(!list_empty(&host->h_lockowners));
159 BUG_ON(atomic_read(&host->h_count));
160
161 /*
162 * Release NSM handle and unmonitor host.
163 */
164 nsm_unmonitor(host);
165
Trond Myklebust34f52e32007-06-14 16:40:31 -0400166 clnt = host->h_rpcclnt;
167 if (clnt != NULL)
168 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700169 kfree(host);
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
Adrian Bunkc5856462006-12-06 20:38:29 -0800173 * Find an NLM server handle in the cache. If there is none, create it.
174 */
175struct nlm_host *
176nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
Chuck Lever48df0202007-11-01 16:56:53 -0400177 const char *hostname, unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800178{
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200179 struct sockaddr_in ssin = {0};
180
Adrian Bunkc5856462006-12-06 20:38:29 -0800181 return nlm_lookup_host(0, sin, proto, version,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200182 hostname, hostname_len, &ssin);
Adrian Bunkc5856462006-12-06 20:38:29 -0800183}
184
185/*
186 * Find an NLM client handle in the cache. If there is none, create it.
187 */
188struct nlm_host *
189nlmsvc_lookup_host(struct svc_rqst *rqstp,
Chuck Lever48df0202007-11-01 16:56:53 -0400190 const char *hostname, unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800191{
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200192 struct sockaddr_in ssin = {0};
193
194 ssin.sin_addr = rqstp->rq_daddr.addr;
Chuck Lever27459f02007-02-12 00:53:34 -0800195 return nlm_lookup_host(1, svc_addr_in(rqstp),
Adrian Bunkc5856462006-12-06 20:38:29 -0800196 rqstp->rq_prot, rqstp->rq_vers,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200197 hostname, hostname_len, &ssin);
Adrian Bunkc5856462006-12-06 20:38:29 -0800198}
199
200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * Create the NLM RPC client for an NLM peer
202 */
203struct rpc_clnt *
204nlm_bind_host(struct nlm_host *host)
205{
206 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200208 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT"->"NIPQUAD_FMT")\n",
209 NIPQUAD(host->h_saddr.sin_addr),
210 NIPQUAD(host->h_addr.sin_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400213 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* If we've already created an RPC client, check whether
216 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 */
218 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700219 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100220 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
222 dprintk("lockd: next rebind in %ld jiffies\n",
223 host->h_nextrebind - jiffies);
224 }
225 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400226 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400227 struct rpc_timeout timeparms = {
228 .to_initval = increment,
229 .to_increment = increment,
230 .to_maxval = increment * 6UL,
231 .to_retries = 5U,
232 };
233 struct rpc_create_args args = {
234 .protocol = host->h_proto,
235 .address = (struct sockaddr *)&host->h_addr,
236 .addrsize = sizeof(host->h_addr),
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200237 .saddress = (struct sockaddr *)&host->h_saddr,
Chuck Levere1ec7892006-08-22 20:06:20 -0400238 .timeout = &timeparms,
239 .servername = host->h_name,
240 .program = &nlm_program,
241 .version = host->h_version,
242 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500243 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400244 RPC_CLNT_CREATE_AUTOBIND),
245 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jeff Layton90bd17c2008-02-06 11:34:11 -0500247 /*
248 * lockd retries server side blocks automatically so we want
249 * those to be soft RPC calls. Client side calls need to be
250 * hard RPC tasks.
251 */
252 if (!host->h_server)
253 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
254
Chuck Levere1ec7892006-08-22 20:06:20 -0400255 clnt = rpc_create(&args);
256 if (!IS_ERR(clnt))
257 host->h_rpcclnt = clnt;
258 else {
259 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
260 clnt = NULL;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Trond Myklebust50467912006-06-09 09:40:24 -0400264 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/*
269 * Force a portmap lookup of the remote lockd port
270 */
271void
272nlm_rebind_host(struct nlm_host *host)
273{
274 dprintk("lockd: rebind host %s\n", host->h_name);
275 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100276 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
278 }
279}
280
281/*
282 * Increment NLM host count
283 */
284struct nlm_host * nlm_get_host(struct nlm_host *host)
285{
286 if (host) {
287 dprintk("lockd: get host %s\n", host->h_name);
288 atomic_inc(&host->h_count);
289 host->h_expires = jiffies + NLM_HOST_EXPIRE;
290 }
291 return host;
292}
293
294/*
295 * Release NLM host after use
296 */
297void nlm_release_host(struct nlm_host *host)
298{
299 if (host != NULL) {
300 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500302 if (atomic_dec_and_test(&host->h_count)) {
303 BUG_ON(!list_empty(&host->h_lockowners));
304 BUG_ON(!list_empty(&host->h_granted));
305 BUG_ON(!list_empty(&host->h_reclaim));
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308}
309
310/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700311 * We were notified that the host indicated by address &sin
312 * has rebooted.
313 * Release all resources held by that peer.
314 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700315void nlm_host_rebooted(const struct sockaddr_in *sin,
Chuck Lever48df0202007-11-01 16:56:53 -0400316 const char *hostname,
317 unsigned int hostname_len,
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700318 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700319{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700320 struct hlist_head *chain;
321 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700322 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700323 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700324
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700325 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
326 hostname, NIPQUAD(sin->sin_addr));
327
328 /* Find the NSM handle for this peer */
329 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700330 return;
331
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700332 /* When reclaiming locks on this peer, make sure that
333 * we set up a new notification */
334 nsm->sm_monitored = 0;
335
336 /* Mark all hosts tied to this NSM state as having rebooted.
337 * We run the loop repeatedly, because we drop the host table
338 * lock for this.
339 * To avoid processing a host several times, we match the nsmstate.
340 */
341again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700342 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
343 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700344 if (host->h_nsmhandle == nsm
345 && host->h_nsmstate != new_state) {
346 host->h_nsmstate = new_state;
347 host->h_state++;
348
349 nlm_get_host(host);
350 mutex_unlock(&nlm_host_mutex);
351
352 if (host->h_server) {
353 /* We're server for this guy, just ditch
354 * all the locks he held. */
355 nlmsvc_free_host_resources(host);
356 } else {
357 /* He's the server, initiate lock recovery. */
358 nlmclnt_recovery(host);
359 }
360
361 nlm_release_host(host);
362 goto again;
363 }
364 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700365 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700366
367 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700368}
369
370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * Shut down the hosts module.
372 * Note that this routine is called only at server shutdown time.
373 */
374void
375nlm_shutdown_hosts(void)
376{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700377 struct hlist_head *chain;
378 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800382 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* First, make all hosts eligible for gc */
385 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700386 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
Jeff Laytond801b862008-01-29 10:30:55 -0500387 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 host->h_expires = jiffies - 1;
Jeff Laytond801b862008-01-29 10:30:55 -0500389 if (host->h_rpcclnt) {
390 rpc_shutdown_client(host->h_rpcclnt);
391 host->h_rpcclnt = NULL;
392 }
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 /* Then, perform a garbage collection pass */
397 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800398 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* complain if any hosts are left */
401 if (nrhosts) {
402 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
403 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700404 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
405 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 dprintk(" %s (cnt %d use %d exp %ld)\n",
407 host->h_name, atomic_read(&host->h_count),
408 host->h_inuse, host->h_expires);
409 }
410 }
411 }
412}
413
414/*
415 * Garbage collect any unused NLM hosts.
416 * This GC combines reference counting for async operations with
417 * mark & sweep for resources held by remote clients.
418 */
419static void
420nlm_gc_hosts(void)
421{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700422 struct hlist_head *chain;
423 struct hlist_node *pos, *next;
424 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700427 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
428 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 host->h_inuse = 0;
430 }
431
432 /* Mark all hosts that hold locks, blocks or shares */
433 nlmsvc_mark_resources();
434
Olaf Kirch0cea3272006-10-04 02:15:56 -0700435 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
436 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (atomic_read(&host->h_count) || host->h_inuse
438 || time_before(jiffies, host->h_expires)) {
439 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
440 host->h_name, atomic_read(&host->h_count),
441 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 continue;
443 }
444 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700445 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700446
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700447 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 nrhosts--;
449 }
450 }
451
452 next_gc = jiffies + NLM_HOST_COLLECT;
453}
454
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700455
456/*
457 * Manage NSM handles
458 */
459static LIST_HEAD(nsm_handles);
Neil Brown89e63ef2006-10-04 02:16:06 -0700460static DEFINE_MUTEX(nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700461
462static struct nsm_handle *
463__nsm_find(const struct sockaddr_in *sin,
Chuck Lever48df0202007-11-01 16:56:53 -0400464 const char *hostname, unsigned int hostname_len,
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700465 int create)
466{
467 struct nsm_handle *nsm = NULL;
468 struct list_head *pos;
469
470 if (!sin)
471 return NULL;
472
473 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
474 if (printk_ratelimit()) {
475 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
476 "in NFS lock request\n",
477 hostname_len, hostname);
478 }
479 return NULL;
480 }
481
Neil Brown89e63ef2006-10-04 02:16:06 -0700482 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700483 list_for_each(pos, &nsm_handles) {
484 nsm = list_entry(pos, struct nsm_handle, sm_link);
485
Olaf Kirchabd1f502006-10-04 02:16:01 -0700486 if (hostname && nsm_use_hostnames) {
487 if (strlen(nsm->sm_name) != hostname_len
488 || memcmp(nsm->sm_name, hostname, hostname_len))
489 continue;
490 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700491 continue;
492 atomic_inc(&nsm->sm_count);
493 goto out;
494 }
495
496 if (!create) {
497 nsm = NULL;
498 goto out;
499 }
500
501 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
502 if (nsm != NULL) {
503 nsm->sm_addr = *sin;
504 nsm->sm_name = (char *) (nsm + 1);
505 memcpy(nsm->sm_name, hostname, hostname_len);
506 nsm->sm_name[hostname_len] = '\0';
507 atomic_set(&nsm->sm_count, 1);
508
509 list_add(&nsm->sm_link, &nsm_handles);
510 }
511
Neil Brown89e63ef2006-10-04 02:16:06 -0700512out:
513 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700514 return nsm;
515}
516
Adrian Bunkc5856462006-12-06 20:38:29 -0800517static struct nsm_handle *
Chuck Lever48df0202007-11-01 16:56:53 -0400518nsm_find(const struct sockaddr_in *sin, const char *hostname,
519 unsigned int hostname_len)
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700520{
521 return __nsm_find(sin, hostname, hostname_len, 1);
522}
523
524/*
525 * Release an NSM handle
526 */
527void
528nsm_release(struct nsm_handle *nsm)
529{
530 if (!nsm)
531 return;
532 if (atomic_dec_and_test(&nsm->sm_count)) {
Neil Brown89e63ef2006-10-04 02:16:06 -0700533 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700534 if (atomic_read(&nsm->sm_count) == 0) {
535 list_del(&nsm->sm_link);
536 kfree(nsm);
537 }
Neil Brown89e63ef2006-10-04 02:16:06 -0700538 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700539 }
540}