blob: a17664c7eacc4542ee8a27a6543c4b548f6efc62 [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 */
Chuck Levereb188602008-03-14 14:18:37 -040044static struct nlm_host *nlm_lookup_host(int server,
45 const struct sockaddr_in *sin,
46 int proto, u32 version,
47 const char *hostname,
48 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
Chuck Levereb188602008-03-14 14:18:37 -040058 ", p=%d, v=%u, my role=%s, name=%.*s)\n",
Frank van Maarseveenc98451b2007-07-09 22:25:29 +020059 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
NeilBrown1447d252008-02-08 13:03:37 +1100145 nrhosts++;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700146out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800147 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return host;
149}
150
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700151/*
152 * Destroy a host
153 */
154static void
155nlm_destroy_host(struct nlm_host *host)
156{
157 struct rpc_clnt *clnt;
158
159 BUG_ON(!list_empty(&host->h_lockowners));
160 BUG_ON(atomic_read(&host->h_count));
161
162 /*
163 * Release NSM handle and unmonitor host.
164 */
165 nsm_unmonitor(host);
166
Trond Myklebust34f52e32007-06-14 16:40:31 -0400167 clnt = host->h_rpcclnt;
168 if (clnt != NULL)
169 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700170 kfree(host);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
Adrian Bunkc5856462006-12-06 20:38:29 -0800174 * Find an NLM server handle in the cache. If there is none, create it.
175 */
Chuck Levereb188602008-03-14 14:18:37 -0400176struct nlm_host *nlmclnt_lookup_host(const struct sockaddr_in *sin,
177 int proto, u32 version,
178 const char *hostname,
179 unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800180{
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200181 struct sockaddr_in ssin = {0};
182
Adrian Bunkc5856462006-12-06 20:38:29 -0800183 return nlm_lookup_host(0, sin, proto, version,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200184 hostname, hostname_len, &ssin);
Adrian Bunkc5856462006-12-06 20:38:29 -0800185}
186
187/*
188 * Find an NLM client handle in the cache. If there is none, create it.
189 */
190struct nlm_host *
191nlmsvc_lookup_host(struct svc_rqst *rqstp,
Chuck Lever48df0202007-11-01 16:56:53 -0400192 const char *hostname, unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800193{
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200194 struct sockaddr_in ssin = {0};
195
196 ssin.sin_addr = rqstp->rq_daddr.addr;
Chuck Lever27459f02007-02-12 00:53:34 -0800197 return nlm_lookup_host(1, svc_addr_in(rqstp),
Adrian Bunkc5856462006-12-06 20:38:29 -0800198 rqstp->rq_prot, rqstp->rq_vers,
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200199 hostname, hostname_len, &ssin);
Adrian Bunkc5856462006-12-06 20:38:29 -0800200}
201
202/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * Create the NLM RPC client for an NLM peer
204 */
205struct rpc_clnt *
206nlm_bind_host(struct nlm_host *host)
207{
208 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200210 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT"->"NIPQUAD_FMT")\n",
211 NIPQUAD(host->h_saddr.sin_addr),
212 NIPQUAD(host->h_addr.sin_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400215 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* If we've already created an RPC client, check whether
218 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
220 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700221 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100222 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
224 dprintk("lockd: next rebind in %ld jiffies\n",
225 host->h_nextrebind - jiffies);
226 }
227 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400228 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400229 struct rpc_timeout timeparms = {
230 .to_initval = increment,
231 .to_increment = increment,
232 .to_maxval = increment * 6UL,
233 .to_retries = 5U,
234 };
235 struct rpc_create_args args = {
236 .protocol = host->h_proto,
237 .address = (struct sockaddr *)&host->h_addr,
238 .addrsize = sizeof(host->h_addr),
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200239 .saddress = (struct sockaddr *)&host->h_saddr,
Chuck Levere1ec7892006-08-22 20:06:20 -0400240 .timeout = &timeparms,
241 .servername = host->h_name,
242 .program = &nlm_program,
243 .version = host->h_version,
244 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500245 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400246 RPC_CLNT_CREATE_AUTOBIND),
247 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jeff Layton90bd17c2008-02-06 11:34:11 -0500249 /*
250 * lockd retries server side blocks automatically so we want
251 * those to be soft RPC calls. Client side calls need to be
252 * hard RPC tasks.
253 */
254 if (!host->h_server)
255 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
256
Chuck Levere1ec7892006-08-22 20:06:20 -0400257 clnt = rpc_create(&args);
258 if (!IS_ERR(clnt))
259 host->h_rpcclnt = clnt;
260 else {
261 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
262 clnt = NULL;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Trond Myklebust50467912006-06-09 09:40:24 -0400266 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/*
271 * Force a portmap lookup of the remote lockd port
272 */
273void
274nlm_rebind_host(struct nlm_host *host)
275{
276 dprintk("lockd: rebind host %s\n", host->h_name);
277 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100278 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
280 }
281}
282
283/*
284 * Increment NLM host count
285 */
286struct nlm_host * nlm_get_host(struct nlm_host *host)
287{
288 if (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;
292 }
293 return host;
294}
295
296/*
297 * Release NLM host after use
298 */
299void nlm_release_host(struct nlm_host *host)
300{
301 if (host != NULL) {
302 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500304 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));
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310}
311
312/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700313 * We were notified that the host indicated by address &sin
314 * has rebooted.
315 * Release all resources held by that peer.
316 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700317void nlm_host_rebooted(const struct sockaddr_in *sin,
Chuck Lever48df0202007-11-01 16:56:53 -0400318 const char *hostname,
319 unsigned int hostname_len,
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700320 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700321{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700322 struct hlist_head *chain;
323 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700324 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700325 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700326
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700327 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
328 hostname, NIPQUAD(sin->sin_addr));
329
330 /* Find the NSM handle for this peer */
331 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700332 return;
333
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700334 /* When reclaiming locks on this peer, make sure that
335 * we set up a new notification */
336 nsm->sm_monitored = 0;
337
338 /* Mark all hosts tied to this NSM state as having rebooted.
339 * We run the loop repeatedly, because we drop the host table
340 * lock for this.
341 * To avoid processing a host several times, we match the nsmstate.
342 */
343again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700344 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
345 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700346 if (host->h_nsmhandle == nsm
347 && host->h_nsmstate != new_state) {
348 host->h_nsmstate = new_state;
349 host->h_state++;
350
351 nlm_get_host(host);
352 mutex_unlock(&nlm_host_mutex);
353
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);
358 } else {
359 /* He's the server, initiate lock recovery. */
360 nlmclnt_recovery(host);
361 }
362
363 nlm_release_host(host);
364 goto again;
365 }
366 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700367 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700368
369 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700370}
371
372/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * Shut down the hosts module.
374 * Note that this routine is called only at server shutdown time.
375 */
376void
377nlm_shutdown_hosts(void)
378{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700379 struct hlist_head *chain;
380 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800384 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* First, make all hosts eligible for gc */
387 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700388 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
Jeff Laytond801b862008-01-29 10:30:55 -0500389 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 host->h_expires = jiffies - 1;
Jeff Laytond801b862008-01-29 10:30:55 -0500391 if (host->h_rpcclnt) {
392 rpc_shutdown_client(host->h_rpcclnt);
393 host->h_rpcclnt = NULL;
394 }
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398 /* Then, perform a garbage collection pass */
399 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800400 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* complain if any hosts are left */
403 if (nrhosts) {
404 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
405 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700406 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
407 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 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);
411 }
412 }
413 }
414}
415
416/*
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.
420 */
421static void
422nlm_gc_hosts(void)
423{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700424 struct hlist_head *chain;
425 struct hlist_node *pos, *next;
426 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700429 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
430 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 host->h_inuse = 0;
432 }
433
434 /* Mark all hosts that hold locks, blocks or shares */
435 nlmsvc_mark_resources();
436
Olaf Kirch0cea3272006-10-04 02:15:56 -0700437 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
438 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 continue;
445 }
446 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700447 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700448
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700449 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 nrhosts--;
451 }
452 }
453
454 next_gc = jiffies + NLM_HOST_COLLECT;
455}
456
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700457
458/*
459 * Manage NSM handles
460 */
461static LIST_HEAD(nsm_handles);
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500462static DEFINE_SPINLOCK(nsm_lock);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700463
464static struct nsm_handle *
465__nsm_find(const struct sockaddr_in *sin,
Chuck Lever48df0202007-11-01 16:56:53 -0400466 const char *hostname, unsigned int hostname_len,
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700467 int create)
468{
469 struct nsm_handle *nsm = NULL;
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500470 struct nsm_handle *pos;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700471
472 if (!sin)
473 return NULL;
474
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);
480 }
481 return NULL;
482 }
483
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500484retry:
485 spin_lock(&nsm_lock);
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500486 list_for_each_entry(pos, &nsm_handles, sm_link) {
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700487
Olaf Kirchabd1f502006-10-04 02:16:01 -0700488 if (hostname && nsm_use_hostnames) {
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500489 if (strlen(pos->sm_name) != hostname_len
490 || memcmp(pos->sm_name, hostname, hostname_len))
Olaf Kirchabd1f502006-10-04 02:16:01 -0700491 continue;
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500492 } else if (!nlm_cmp_addr(&pos->sm_addr, sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700493 continue;
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500494 atomic_inc(&pos->sm_count);
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500495 kfree(nsm);
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500496 nsm = pos;
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500497 goto found;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700498 }
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500499 if (nsm) {
500 list_add(&nsm->sm_link, &nsm_handles);
501 goto found;
502 }
503 spin_unlock(&nsm_lock);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700504
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500505 if (!create)
506 return NULL;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700507
508 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500509 if (nsm == NULL)
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500510 return NULL;
511
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500512 nsm->sm_addr = *sin;
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);
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500517 goto retry;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700518
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500519found:
520 spin_unlock(&nsm_lock);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700521 return nsm;
522}
523
Adrian Bunkc5856462006-12-06 20:38:29 -0800524static struct nsm_handle *
Chuck Lever48df0202007-11-01 16:56:53 -0400525nsm_find(const struct sockaddr_in *sin, const char *hostname,
526 unsigned int hostname_len)
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700527{
528 return __nsm_find(sin, hostname, hostname_len, 1);
529}
530
531/*
532 * Release an NSM handle
533 */
534void
535nsm_release(struct nsm_handle *nsm)
536{
537 if (!nsm)
538 return;
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500539 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
J. Bruce Fields164f98a2008-02-20 14:02:47 -0500540 list_del(&nsm->sm_link);
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500541 spin_unlock(&nsm_lock);
J. Bruce Fields164f98a2008-02-20 14:02:47 -0500542 kfree(nsm);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700543 }
544}