blob: f23750db16508f96a31d45d1821e8e56b452868a [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
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Olaf Kirch0cea3272006-10-04 02:15:56 -070029static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static unsigned long next_gc;
31static int nrhosts;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080032static DEFINE_MUTEX(nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34
35static void nlm_gc_hosts(void);
Olaf Kirch8dead0d2006-10-04 02:15:53 -070036static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
Chuck Lever48df0202007-11-01 16:56:53 -040037 const char *, unsigned int, int);
Adrian Bunkc5856462006-12-06 20:38:29 -080038static struct nsm_handle * nsm_find(const struct sockaddr_in *sin,
39 const char *hostname,
Chuck Lever48df0202007-11-01 16:56:53 -040040 unsigned int hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Common host lookup routine for server & client
44 */
Chuck Levereb188602008-03-14 14:18:37 -040045static struct nlm_host *nlm_lookup_host(int server,
46 const struct sockaddr_in *sin,
47 int proto, u32 version,
48 const char *hostname,
49 unsigned int hostname_len,
50 const struct sockaddr_in *ssin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Olaf Kirch0cea3272006-10-04 02:15:56 -070052 struct hlist_head *chain;
53 struct hlist_node *pos;
54 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070055 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int hash;
57
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020058 dprintk("lockd: nlm_lookup_host("NIPQUAD_FMT"->"NIPQUAD_FMT
Chuck Levereb188602008-03-14 14:18:37 -040059 ", p=%d, v=%u, my role=%s, name=%.*s)\n",
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020060 NIPQUAD(ssin->sin_addr.s_addr),
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070061 NIPQUAD(sin->sin_addr.s_addr), proto, version,
62 server? "server" : "client",
63 hostname_len,
64 hostname? hostname : "<none>");
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
68
69 /* Lock hash table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080070 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 if (time_after_eq(jiffies, next_gc))
73 nlm_gc_hosts();
74
Olaf Kirch8dead0d2006-10-04 02:15:53 -070075 /* We may keep several nlm_host objects for a peer, because each
76 * nlm_host is identified by
77 * (address, protocol, version, server/client)
78 * We could probably simplify this a little by putting all those
79 * different NLM rpc_clients into one single nlm_host object.
80 * This would allow us to have one nlm_host per address.
81 */
Olaf Kirch0cea3272006-10-04 02:15:56 -070082 chain = &nlm_hosts[hash];
83 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch8dead0d2006-10-04 02:15:53 -070084 if (!nlm_cmp_addr(&host->h_addr, sin))
85 continue;
86
87 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -070088 if (!nsm)
89 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (host->h_proto != proto)
92 continue;
93 if (host->h_version != version)
94 continue;
95 if (host->h_server != server)
96 continue;
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020097 if (!nlm_cmp_addr(&host->h_saddr, ssin))
98 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Olaf Kirch0cea3272006-10-04 02:15:56 -0700100 /* Move to head of hash chain. */
101 hlist_del(&host->h_hash);
102 hlist_add_head(&host->h_hash, chain);
103
Olaf Kirchf0737a32006-10-04 02:15:54 -0700104 nlm_get_host(host);
105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
NeilBrown6b54dae2006-10-04 02:16:15 -0700107 if (nsm)
108 atomic_inc(&nsm->sm_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Olaf Kirch0cea3272006-10-04 02:15:56 -0700110 host = NULL;
111
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700112 /* Sadly, the host isn't in our hash table yet. See if
113 * we have an NSM handle for it. If not, create one.
114 */
115 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
116 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700118 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700119 if (!host) {
120 nsm_release(nsm);
121 goto out;
122 }
123 host->h_name = nsm->sm_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 host->h_addr = *sin;
125 host->h_addr.sin_port = 0; /* ouch! */
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200126 host->h_saddr = *ssin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 host->h_version = version;
128 host->h_proto = proto;
129 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400130 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
132 host->h_expires = jiffies + NLM_HOST_EXPIRE;
133 atomic_set(&host->h_count, 1);
134 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400135 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 host->h_state = 0; /* pseudo NSM state */
137 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700138 host->h_nsmhandle = nsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 host->h_server = server;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700140 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 INIT_LIST_HEAD(&host->h_lockowners);
142 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500143 INIT_LIST_HEAD(&host->h_granted);
144 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (++nrhosts > NLM_HOST_MAX)
147 next_gc = 0;
148
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700149out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800150 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return host;
152}
153
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700154/*
155 * Destroy a host
156 */
157static void
158nlm_destroy_host(struct nlm_host *host)
159{
160 struct rpc_clnt *clnt;
161
162 BUG_ON(!list_empty(&host->h_lockowners));
163 BUG_ON(atomic_read(&host->h_count));
164
165 /*
166 * Release NSM handle and unmonitor host.
167 */
168 nsm_unmonitor(host);
169
Trond Myklebust34f52e32007-06-14 16:40:31 -0400170 clnt = host->h_rpcclnt;
171 if (clnt != NULL)
172 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700173 kfree(host);
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
Adrian Bunkc5856462006-12-06 20:38:29 -0800177 * Find an NLM server handle in the cache. If there is none, create it.
178 */
Chuck Levereb188602008-03-14 14:18:37 -0400179struct nlm_host *nlmclnt_lookup_host(const struct sockaddr_in *sin,
180 int proto, u32 version,
181 const char *hostname,
182 unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800183{
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200184 struct sockaddr_in ssin = {0};
185
Adrian Bunkc5856462006-12-06 20:38:29 -0800186 return nlm_lookup_host(0, sin, proto, version,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200187 hostname, hostname_len, &ssin);
Adrian Bunkc5856462006-12-06 20:38:29 -0800188}
189
190/*
191 * Find an NLM client handle in the cache. If there is none, create it.
192 */
193struct nlm_host *
194nlmsvc_lookup_host(struct svc_rqst *rqstp,
Chuck Lever48df0202007-11-01 16:56:53 -0400195 const char *hostname, unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800196{
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200197 struct sockaddr_in ssin = {0};
198
199 ssin.sin_addr = rqstp->rq_daddr.addr;
Chuck Lever27459f02007-02-12 00:53:34 -0800200 return nlm_lookup_host(1, svc_addr_in(rqstp),
Adrian Bunkc5856462006-12-06 20:38:29 -0800201 rqstp->rq_prot, rqstp->rq_vers,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200202 hostname, hostname_len, &ssin);
Adrian Bunkc5856462006-12-06 20:38:29 -0800203}
204
205/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * Create the NLM RPC client for an NLM peer
207 */
208struct rpc_clnt *
209nlm_bind_host(struct nlm_host *host)
210{
211 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200213 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT"->"NIPQUAD_FMT")\n",
214 NIPQUAD(host->h_saddr.sin_addr),
215 NIPQUAD(host->h_addr.sin_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400218 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* If we've already created an RPC client, check whether
221 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
223 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700224 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100225 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
227 dprintk("lockd: next rebind in %ld jiffies\n",
228 host->h_nextrebind - jiffies);
229 }
230 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400231 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400232 struct rpc_timeout timeparms = {
233 .to_initval = increment,
234 .to_increment = increment,
235 .to_maxval = increment * 6UL,
236 .to_retries = 5U,
237 };
238 struct rpc_create_args args = {
239 .protocol = host->h_proto,
240 .address = (struct sockaddr *)&host->h_addr,
241 .addrsize = sizeof(host->h_addr),
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200242 .saddress = (struct sockaddr *)&host->h_saddr,
Chuck Levere1ec7892006-08-22 20:06:20 -0400243 .timeout = &timeparms,
244 .servername = host->h_name,
245 .program = &nlm_program,
246 .version = host->h_version,
247 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500248 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400249 RPC_CLNT_CREATE_AUTOBIND),
250 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jeff Layton90bd17c2008-02-06 11:34:11 -0500252 /*
253 * lockd retries server side blocks automatically so we want
254 * those to be soft RPC calls. Client side calls need to be
255 * hard RPC tasks.
256 */
257 if (!host->h_server)
258 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
259
Chuck Levere1ec7892006-08-22 20:06:20 -0400260 clnt = rpc_create(&args);
261 if (!IS_ERR(clnt))
262 host->h_rpcclnt = clnt;
263 else {
264 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
265 clnt = NULL;
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
Trond Myklebust50467912006-06-09 09:40:24 -0400269 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
274 * Force a portmap lookup of the remote lockd port
275 */
276void
277nlm_rebind_host(struct nlm_host *host)
278{
279 dprintk("lockd: rebind host %s\n", host->h_name);
280 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100281 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
283 }
284}
285
286/*
287 * Increment NLM host count
288 */
289struct nlm_host * nlm_get_host(struct nlm_host *host)
290{
291 if (host) {
292 dprintk("lockd: get host %s\n", host->h_name);
293 atomic_inc(&host->h_count);
294 host->h_expires = jiffies + NLM_HOST_EXPIRE;
295 }
296 return host;
297}
298
299/*
300 * Release NLM host after use
301 */
302void nlm_release_host(struct nlm_host *host)
303{
304 if (host != NULL) {
305 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500307 if (atomic_dec_and_test(&host->h_count)) {
308 BUG_ON(!list_empty(&host->h_lockowners));
309 BUG_ON(!list_empty(&host->h_granted));
310 BUG_ON(!list_empty(&host->h_reclaim));
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313}
314
315/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700316 * We were notified that the host indicated by address &sin
317 * has rebooted.
318 * Release all resources held by that peer.
319 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700320void nlm_host_rebooted(const struct sockaddr_in *sin,
Chuck Lever48df0202007-11-01 16:56:53 -0400321 const char *hostname,
322 unsigned int hostname_len,
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700323 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700324{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700325 struct hlist_head *chain;
326 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700327 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700328 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700329
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700330 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
331 hostname, NIPQUAD(sin->sin_addr));
332
333 /* Find the NSM handle for this peer */
334 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700335 return;
336
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700337 /* When reclaiming locks on this peer, make sure that
338 * we set up a new notification */
339 nsm->sm_monitored = 0;
340
341 /* Mark all hosts tied to this NSM state as having rebooted.
342 * We run the loop repeatedly, because we drop the host table
343 * lock for this.
344 * To avoid processing a host several times, we match the nsmstate.
345 */
346again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700347 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
348 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700349 if (host->h_nsmhandle == nsm
350 && host->h_nsmstate != new_state) {
351 host->h_nsmstate = new_state;
352 host->h_state++;
353
354 nlm_get_host(host);
355 mutex_unlock(&nlm_host_mutex);
356
357 if (host->h_server) {
358 /* We're server for this guy, just ditch
359 * all the locks he held. */
360 nlmsvc_free_host_resources(host);
361 } else {
362 /* He's the server, initiate lock recovery. */
363 nlmclnt_recovery(host);
364 }
365
366 nlm_release_host(host);
367 goto again;
368 }
369 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700370 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700371
372 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700373}
374
375/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * Shut down the hosts module.
377 * Note that this routine is called only at server shutdown time.
378 */
379void
380nlm_shutdown_hosts(void)
381{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700382 struct hlist_head *chain;
383 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800387 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* First, make all hosts eligible for gc */
390 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700391 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
Jeff Laytond801b862008-01-29 10:30:55 -0500392 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 host->h_expires = jiffies - 1;
Jeff Laytond801b862008-01-29 10:30:55 -0500394 if (host->h_rpcclnt) {
395 rpc_shutdown_client(host->h_rpcclnt);
396 host->h_rpcclnt = NULL;
397 }
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 /* Then, perform a garbage collection pass */
402 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800403 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* complain if any hosts are left */
406 if (nrhosts) {
407 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
408 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700409 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
410 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 dprintk(" %s (cnt %d use %d exp %ld)\n",
412 host->h_name, atomic_read(&host->h_count),
413 host->h_inuse, host->h_expires);
414 }
415 }
416 }
417}
418
419/*
420 * Garbage collect any unused NLM hosts.
421 * This GC combines reference counting for async operations with
422 * mark & sweep for resources held by remote clients.
423 */
424static void
425nlm_gc_hosts(void)
426{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700427 struct hlist_head *chain;
428 struct hlist_node *pos, *next;
429 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700432 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
433 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 host->h_inuse = 0;
435 }
436
437 /* Mark all hosts that hold locks, blocks or shares */
438 nlmsvc_mark_resources();
439
Olaf Kirch0cea3272006-10-04 02:15:56 -0700440 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
441 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (atomic_read(&host->h_count) || host->h_inuse
443 || time_before(jiffies, host->h_expires)) {
444 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
445 host->h_name, atomic_read(&host->h_count),
446 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 continue;
448 }
449 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700450 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700451
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700452 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 nrhosts--;
454 }
455 }
456
457 next_gc = jiffies + NLM_HOST_COLLECT;
458}
459
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700460
461/*
462 * Manage NSM handles
463 */
464static LIST_HEAD(nsm_handles);
Neil Brown89e63ef2006-10-04 02:16:06 -0700465static DEFINE_MUTEX(nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700466
467static struct nsm_handle *
468__nsm_find(const struct sockaddr_in *sin,
Chuck Lever48df0202007-11-01 16:56:53 -0400469 const char *hostname, unsigned int hostname_len,
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700470 int create)
471{
472 struct nsm_handle *nsm = NULL;
473 struct list_head *pos;
474
475 if (!sin)
476 return NULL;
477
478 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
479 if (printk_ratelimit()) {
480 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
481 "in NFS lock request\n",
482 hostname_len, hostname);
483 }
484 return NULL;
485 }
486
Neil Brown89e63ef2006-10-04 02:16:06 -0700487 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700488 list_for_each(pos, &nsm_handles) {
489 nsm = list_entry(pos, struct nsm_handle, sm_link);
490
Olaf Kirchabd1f502006-10-04 02:16:01 -0700491 if (hostname && nsm_use_hostnames) {
492 if (strlen(nsm->sm_name) != hostname_len
493 || memcmp(nsm->sm_name, hostname, hostname_len))
494 continue;
495 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700496 continue;
497 atomic_inc(&nsm->sm_count);
498 goto out;
499 }
500
501 if (!create) {
502 nsm = NULL;
503 goto out;
504 }
505
506 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
507 if (nsm != NULL) {
508 nsm->sm_addr = *sin;
509 nsm->sm_name = (char *) (nsm + 1);
510 memcpy(nsm->sm_name, hostname, hostname_len);
511 nsm->sm_name[hostname_len] = '\0';
512 atomic_set(&nsm->sm_count, 1);
513
514 list_add(&nsm->sm_link, &nsm_handles);
515 }
516
Neil Brown89e63ef2006-10-04 02:16:06 -0700517out:
518 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700519 return nsm;
520}
521
Adrian Bunkc5856462006-12-06 20:38:29 -0800522static struct nsm_handle *
Chuck Lever48df0202007-11-01 16:56:53 -0400523nsm_find(const struct sockaddr_in *sin, const char *hostname,
524 unsigned int hostname_len)
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700525{
526 return __nsm_find(sin, hostname, hostname_len, 1);
527}
528
529/*
530 * Release an NSM handle
531 */
532void
533nsm_release(struct nsm_handle *nsm)
534{
535 if (!nsm)
536 return;
537 if (atomic_dec_and_test(&nsm->sm_count)) {
Neil Brown89e63ef2006-10-04 02:16:06 -0700538 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700539 if (atomic_read(&nsm->sm_count) == 0) {
540 list_del(&nsm->sm_link);
541 kfree(nsm);
542 }
Neil Brown89e63ef2006-10-04 02:16:06 -0700543 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700544 }
545}