blob: ca6b16fc3101a3df88d60350060eae56d6528535 [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 */
Adrian Bunkc5856462006-12-06 20:38:29 -080045static struct nlm_host *
Olaf Kirchcf712c22006-10-04 02:15:52 -070046nlm_lookup_host(int server, const struct sockaddr_in *sin,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020047 int proto, int version, const char *hostname,
Chuck Lever48df0202007-11-01 16:56:53 -040048 unsigned int hostname_len,
49 const struct sockaddr_in *ssin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Olaf Kirch0cea3272006-10-04 02:15:56 -070051 struct hlist_head *chain;
52 struct hlist_node *pos;
53 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070054 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int hash;
56
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020057 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),
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070060 NIPQUAD(sin->sin_addr.s_addr), proto, version,
61 server? "server" : "client",
62 hostname_len,
63 hostname? hostname : "<none>");
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
67
68 /* Lock hash table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080069 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 if (time_after_eq(jiffies, next_gc))
72 nlm_gc_hosts();
73
Olaf Kirch8dead0d2006-10-04 02:15:53 -070074 /* 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.
80 */
Olaf Kirch0cea3272006-10-04 02:15:56 -070081 chain = &nlm_hosts[hash];
82 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch8dead0d2006-10-04 02:15:53 -070083 if (!nlm_cmp_addr(&host->h_addr, sin))
84 continue;
85
86 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -070087 if (!nsm)
88 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (host->h_proto != proto)
91 continue;
92 if (host->h_version != version)
93 continue;
94 if (host->h_server != server)
95 continue;
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020096 if (!nlm_cmp_addr(&host->h_saddr, ssin))
97 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Olaf Kirch0cea3272006-10-04 02:15:56 -070099 /* Move to head of hash chain. */
100 hlist_del(&host->h_hash);
101 hlist_add_head(&host->h_hash, chain);
102
Olaf Kirchf0737a32006-10-04 02:15:54 -0700103 nlm_get_host(host);
104 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
NeilBrown6b54dae2006-10-04 02:16:15 -0700106 if (nsm)
107 atomic_inc(&nsm->sm_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Olaf Kirch0cea3272006-10-04 02:15:56 -0700109 host = NULL;
110
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700111 /* Sadly, the host isn't in our hash table yet. See if
112 * we have an NSM handle for it. If not, create one.
113 */
114 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
115 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700117 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700118 if (!host) {
119 nsm_release(nsm);
120 goto out;
121 }
122 host->h_name = nsm->sm_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 host->h_addr = *sin;
124 host->h_addr.sin_port = 0; /* ouch! */
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200125 host->h_saddr = *ssin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 host->h_version = version;
127 host->h_proto = proto;
128 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400129 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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);
Trond Myklebust28df9552006-06-09 09:40:27 -0400134 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 host->h_state = 0; /* pseudo NSM state */
136 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700137 host->h_nsmhandle = nsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 host->h_server = server;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700139 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 INIT_LIST_HEAD(&host->h_lockowners);
141 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500142 INIT_LIST_HEAD(&host->h_granted);
143 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 if (++nrhosts > NLM_HOST_MAX)
146 next_gc = 0;
147
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700148out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800149 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return host;
151}
152
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700153/*
154 * Destroy a host
155 */
156static void
157nlm_destroy_host(struct nlm_host *host)
158{
159 struct rpc_clnt *clnt;
160
161 BUG_ON(!list_empty(&host->h_lockowners));
162 BUG_ON(atomic_read(&host->h_count));
163
164 /*
165 * Release NSM handle and unmonitor host.
166 */
167 nsm_unmonitor(host);
168
Trond Myklebust34f52e32007-06-14 16:40:31 -0400169 clnt = host->h_rpcclnt;
170 if (clnt != NULL)
171 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700172 kfree(host);
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
Adrian Bunkc5856462006-12-06 20:38:29 -0800176 * Find an NLM server handle in the cache. If there is none, create it.
177 */
178struct nlm_host *
179nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
Chuck Lever48df0202007-11-01 16:56:53 -0400180 const char *hostname, unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800181{
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200182 struct sockaddr_in ssin = {0};
183
Adrian Bunkc5856462006-12-06 20:38:29 -0800184 return nlm_lookup_host(0, sin, proto, version,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200185 hostname, hostname_len, &ssin);
Adrian Bunkc5856462006-12-06 20:38:29 -0800186}
187
188/*
189 * Find an NLM client handle in the cache. If there is none, create it.
190 */
191struct nlm_host *
192nlmsvc_lookup_host(struct svc_rqst *rqstp,
Chuck Lever48df0202007-11-01 16:56:53 -0400193 const char *hostname, unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800194{
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200195 struct sockaddr_in ssin = {0};
196
197 ssin.sin_addr = rqstp->rq_daddr.addr;
Chuck Lever27459f02007-02-12 00:53:34 -0800198 return nlm_lookup_host(1, svc_addr_in(rqstp),
Adrian Bunkc5856462006-12-06 20:38:29 -0800199 rqstp->rq_prot, rqstp->rq_vers,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200200 hostname, hostname_len, &ssin);
Adrian Bunkc5856462006-12-06 20:38:29 -0800201}
202
203/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * Create the NLM RPC client for an NLM peer
205 */
206struct rpc_clnt *
207nlm_bind_host(struct nlm_host *host)
208{
209 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200211 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT"->"NIPQUAD_FMT")\n",
212 NIPQUAD(host->h_saddr.sin_addr),
213 NIPQUAD(host->h_addr.sin_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400216 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* If we've already created an RPC client, check whether
219 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700222 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100223 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
225 dprintk("lockd: next rebind in %ld jiffies\n",
226 host->h_nextrebind - jiffies);
227 }
228 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400229 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400230 struct rpc_timeout timeparms = {
231 .to_initval = increment,
232 .to_increment = increment,
233 .to_maxval = increment * 6UL,
234 .to_retries = 5U,
235 };
236 struct rpc_create_args args = {
237 .protocol = host->h_proto,
238 .address = (struct sockaddr *)&host->h_addr,
239 .addrsize = sizeof(host->h_addr),
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200240 .saddress = (struct sockaddr *)&host->h_saddr,
Chuck Levere1ec7892006-08-22 20:06:20 -0400241 .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_AUTOBIND),
248 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Chuck Levere1ec7892006-08-22 20:06:20 -0400250 clnt = rpc_create(&args);
251 if (!IS_ERR(clnt))
252 host->h_rpcclnt = clnt;
253 else {
254 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
255 clnt = NULL;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Trond Myklebust50467912006-06-09 09:40:24 -0400259 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/*
264 * Force a portmap lookup of the remote lockd port
265 */
266void
267nlm_rebind_host(struct nlm_host *host)
268{
269 dprintk("lockd: rebind host %s\n", host->h_name);
270 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100271 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
273 }
274}
275
276/*
277 * Increment NLM host count
278 */
279struct nlm_host * nlm_get_host(struct nlm_host *host)
280{
281 if (host) {
282 dprintk("lockd: get host %s\n", host->h_name);
283 atomic_inc(&host->h_count);
284 host->h_expires = jiffies + NLM_HOST_EXPIRE;
285 }
286 return host;
287}
288
289/*
290 * Release NLM host after use
291 */
292void nlm_release_host(struct nlm_host *host)
293{
294 if (host != NULL) {
295 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500297 if (atomic_dec_and_test(&host->h_count)) {
298 BUG_ON(!list_empty(&host->h_lockowners));
299 BUG_ON(!list_empty(&host->h_granted));
300 BUG_ON(!list_empty(&host->h_reclaim));
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303}
304
305/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700306 * We were notified that the host indicated by address &sin
307 * has rebooted.
308 * Release all resources held by that peer.
309 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700310void nlm_host_rebooted(const struct sockaddr_in *sin,
Chuck Lever48df0202007-11-01 16:56:53 -0400311 const char *hostname,
312 unsigned int hostname_len,
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700313 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700314{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700315 struct hlist_head *chain;
316 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700317 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700318 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700319
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700320 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
321 hostname, NIPQUAD(sin->sin_addr));
322
323 /* Find the NSM handle for this peer */
324 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700325 return;
326
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700327 /* When reclaiming locks on this peer, make sure that
328 * we set up a new notification */
329 nsm->sm_monitored = 0;
330
331 /* Mark all hosts tied to this NSM state as having rebooted.
332 * We run the loop repeatedly, because we drop the host table
333 * lock for this.
334 * To avoid processing a host several times, we match the nsmstate.
335 */
336again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700337 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
338 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700339 if (host->h_nsmhandle == nsm
340 && host->h_nsmstate != new_state) {
341 host->h_nsmstate = new_state;
342 host->h_state++;
343
344 nlm_get_host(host);
345 mutex_unlock(&nlm_host_mutex);
346
347 if (host->h_server) {
348 /* We're server for this guy, just ditch
349 * all the locks he held. */
350 nlmsvc_free_host_resources(host);
351 } else {
352 /* He's the server, initiate lock recovery. */
353 nlmclnt_recovery(host);
354 }
355
356 nlm_release_host(host);
357 goto again;
358 }
359 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700360 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700361
362 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700363}
364
365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * Shut down the hosts module.
367 * Note that this routine is called only at server shutdown time.
368 */
369void
370nlm_shutdown_hosts(void)
371{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700372 struct hlist_head *chain;
373 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800377 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* First, make all hosts eligible for gc */
380 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700381 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
Jeff Laytond801b862008-01-29 10:30:55 -0500382 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 host->h_expires = jiffies - 1;
Jeff Laytond801b862008-01-29 10:30:55 -0500384 if (host->h_rpcclnt) {
385 rpc_shutdown_client(host->h_rpcclnt);
386 host->h_rpcclnt = NULL;
387 }
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391 /* Then, perform a garbage collection pass */
392 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800393 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* complain if any hosts are left */
396 if (nrhosts) {
397 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
398 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700399 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
400 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dprintk(" %s (cnt %d use %d exp %ld)\n",
402 host->h_name, atomic_read(&host->h_count),
403 host->h_inuse, host->h_expires);
404 }
405 }
406 }
407}
408
409/*
410 * Garbage collect any unused NLM hosts.
411 * This GC combines reference counting for async operations with
412 * mark & sweep for resources held by remote clients.
413 */
414static void
415nlm_gc_hosts(void)
416{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700417 struct hlist_head *chain;
418 struct hlist_node *pos, *next;
419 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700422 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
423 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 host->h_inuse = 0;
425 }
426
427 /* Mark all hosts that hold locks, blocks or shares */
428 nlmsvc_mark_resources();
429
Olaf Kirch0cea3272006-10-04 02:15:56 -0700430 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
431 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (atomic_read(&host->h_count) || host->h_inuse
433 || time_before(jiffies, host->h_expires)) {
434 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
435 host->h_name, atomic_read(&host->h_count),
436 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 continue;
438 }
439 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700440 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700441
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700442 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 nrhosts--;
444 }
445 }
446
447 next_gc = jiffies + NLM_HOST_COLLECT;
448}
449
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700450
451/*
452 * Manage NSM handles
453 */
454static LIST_HEAD(nsm_handles);
Neil Brown89e63ef2006-10-04 02:16:06 -0700455static DEFINE_MUTEX(nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700456
457static struct nsm_handle *
458__nsm_find(const struct sockaddr_in *sin,
Chuck Lever48df0202007-11-01 16:56:53 -0400459 const char *hostname, unsigned int hostname_len,
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700460 int create)
461{
462 struct nsm_handle *nsm = NULL;
463 struct list_head *pos;
464
465 if (!sin)
466 return NULL;
467
468 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
469 if (printk_ratelimit()) {
470 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
471 "in NFS lock request\n",
472 hostname_len, hostname);
473 }
474 return NULL;
475 }
476
Neil Brown89e63ef2006-10-04 02:16:06 -0700477 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700478 list_for_each(pos, &nsm_handles) {
479 nsm = list_entry(pos, struct nsm_handle, sm_link);
480
Olaf Kirchabd1f502006-10-04 02:16:01 -0700481 if (hostname && nsm_use_hostnames) {
482 if (strlen(nsm->sm_name) != hostname_len
483 || memcmp(nsm->sm_name, hostname, hostname_len))
484 continue;
485 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700486 continue;
487 atomic_inc(&nsm->sm_count);
488 goto out;
489 }
490
491 if (!create) {
492 nsm = NULL;
493 goto out;
494 }
495
496 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
497 if (nsm != NULL) {
498 nsm->sm_addr = *sin;
499 nsm->sm_name = (char *) (nsm + 1);
500 memcpy(nsm->sm_name, hostname, hostname_len);
501 nsm->sm_name[hostname_len] = '\0';
502 atomic_set(&nsm->sm_count, 1);
503
504 list_add(&nsm->sm_link, &nsm_handles);
505 }
506
Neil Brown89e63ef2006-10-04 02:16:06 -0700507out:
508 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700509 return nsm;
510}
511
Adrian Bunkc5856462006-12-06 20:38:29 -0800512static struct nsm_handle *
Chuck Lever48df0202007-11-01 16:56:53 -0400513nsm_find(const struct sockaddr_in *sin, const char *hostname,
514 unsigned int hostname_len)
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700515{
516 return __nsm_find(sin, hostname, hostname_len, 1);
517}
518
519/*
520 * Release an NSM handle
521 */
522void
523nsm_release(struct nsm_handle *nsm)
524{
525 if (!nsm)
526 return;
527 if (atomic_dec_and_test(&nsm->sm_count)) {
Neil Brown89e63ef2006-10-04 02:16:06 -0700528 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700529 if (atomic_read(&nsm->sm_count) == 0) {
530 list_del(&nsm->sm_link);
531 kfree(nsm);
532 }
Neil Brown89e63ef2006-10-04 02:16:06 -0700533 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700534 }
535}