blob: 572601e98dcdec6d6d88da33ec0b757bd8b7d3f4 [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 *,
37 const char *, int, int);
Adrian Bunkc5856462006-12-06 20:38:29 -080038static struct nsm_handle * nsm_find(const struct sockaddr_in *sin,
39 const char *hostname,
40 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,
48 int hostname_len, 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
144 if (++nrhosts > NLM_HOST_MAX)
145 next_gc = 0;
146
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700147out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800148 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return host;
150}
151
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700152/*
153 * Destroy a host
154 */
155static void
156nlm_destroy_host(struct nlm_host *host)
157{
158 struct rpc_clnt *clnt;
159
160 BUG_ON(!list_empty(&host->h_lockowners));
161 BUG_ON(atomic_read(&host->h_count));
162
163 /*
164 * Release NSM handle and unmonitor host.
165 */
166 nsm_unmonitor(host);
167
Trond Myklebust34f52e32007-06-14 16:40:31 -0400168 clnt = host->h_rpcclnt;
169 if (clnt != NULL)
170 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700171 kfree(host);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Adrian Bunkc5856462006-12-06 20:38:29 -0800175 * Find an NLM server handle in the cache. If there is none, create it.
176 */
177struct nlm_host *
178nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
179 const char *hostname, int hostname_len)
180{
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,
192 const char *hostname, int hostname_len)
193{
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,
245 .flags = (RPC_CLNT_CREATE_HARDRTRY |
246 RPC_CLNT_CREATE_AUTOBIND),
247 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Chuck Levere1ec7892006-08-22 20:06:20 -0400249 clnt = rpc_create(&args);
250 if (!IS_ERR(clnt))
251 host->h_rpcclnt = clnt;
252 else {
253 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
254 clnt = NULL;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Trond Myklebust50467912006-06-09 09:40:24 -0400258 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
263 * Force a portmap lookup of the remote lockd port
264 */
265void
266nlm_rebind_host(struct nlm_host *host)
267{
268 dprintk("lockd: rebind host %s\n", host->h_name);
269 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100270 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
272 }
273}
274
275/*
276 * Increment NLM host count
277 */
278struct nlm_host * nlm_get_host(struct nlm_host *host)
279{
280 if (host) {
281 dprintk("lockd: get host %s\n", host->h_name);
282 atomic_inc(&host->h_count);
283 host->h_expires = jiffies + NLM_HOST_EXPIRE;
284 }
285 return host;
286}
287
288/*
289 * Release NLM host after use
290 */
291void nlm_release_host(struct nlm_host *host)
292{
293 if (host != NULL) {
294 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500296 if (atomic_dec_and_test(&host->h_count)) {
297 BUG_ON(!list_empty(&host->h_lockowners));
298 BUG_ON(!list_empty(&host->h_granted));
299 BUG_ON(!list_empty(&host->h_reclaim));
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302}
303
304/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700305 * We were notified that the host indicated by address &sin
306 * has rebooted.
307 * Release all resources held by that peer.
308 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700309void nlm_host_rebooted(const struct sockaddr_in *sin,
310 const char *hostname, int hostname_len,
311 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700312{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700313 struct hlist_head *chain;
314 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700315 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700316 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700317
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700318 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
319 hostname, NIPQUAD(sin->sin_addr));
320
321 /* Find the NSM handle for this peer */
322 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700323 return;
324
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700325 /* When reclaiming locks on this peer, make sure that
326 * we set up a new notification */
327 nsm->sm_monitored = 0;
328
329 /* Mark all hosts tied to this NSM state as having rebooted.
330 * We run the loop repeatedly, because we drop the host table
331 * lock for this.
332 * To avoid processing a host several times, we match the nsmstate.
333 */
334again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700335 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
336 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700337 if (host->h_nsmhandle == nsm
338 && host->h_nsmstate != new_state) {
339 host->h_nsmstate = new_state;
340 host->h_state++;
341
342 nlm_get_host(host);
343 mutex_unlock(&nlm_host_mutex);
344
345 if (host->h_server) {
346 /* We're server for this guy, just ditch
347 * all the locks he held. */
348 nlmsvc_free_host_resources(host);
349 } else {
350 /* He's the server, initiate lock recovery. */
351 nlmclnt_recovery(host);
352 }
353
354 nlm_release_host(host);
355 goto again;
356 }
357 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700358 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700359
360 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700361}
362
363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * Shut down the hosts module.
365 * Note that this routine is called only at server shutdown time.
366 */
367void
368nlm_shutdown_hosts(void)
369{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700370 struct hlist_head *chain;
371 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800375 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* First, make all hosts eligible for gc */
378 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700379 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
380 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 host->h_expires = jiffies - 1;
382 }
383
384 /* Then, perform a garbage collection pass */
385 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800386 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* complain if any hosts are left */
389 if (nrhosts) {
390 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
391 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700392 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
393 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 dprintk(" %s (cnt %d use %d exp %ld)\n",
395 host->h_name, atomic_read(&host->h_count),
396 host->h_inuse, host->h_expires);
397 }
398 }
399 }
400}
401
402/*
403 * Garbage collect any unused NLM hosts.
404 * This GC combines reference counting for async operations with
405 * mark & sweep for resources held by remote clients.
406 */
407static void
408nlm_gc_hosts(void)
409{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700410 struct hlist_head *chain;
411 struct hlist_node *pos, *next;
412 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700415 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
416 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 host->h_inuse = 0;
418 }
419
420 /* Mark all hosts that hold locks, blocks or shares */
421 nlmsvc_mark_resources();
422
Olaf Kirch0cea3272006-10-04 02:15:56 -0700423 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
424 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (atomic_read(&host->h_count) || host->h_inuse
426 || time_before(jiffies, host->h_expires)) {
427 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
428 host->h_name, atomic_read(&host->h_count),
429 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 continue;
431 }
432 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700433 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700434
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700435 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 nrhosts--;
437 }
438 }
439
440 next_gc = jiffies + NLM_HOST_COLLECT;
441}
442
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700443
444/*
445 * Manage NSM handles
446 */
447static LIST_HEAD(nsm_handles);
Neil Brown89e63ef2006-10-04 02:16:06 -0700448static DEFINE_MUTEX(nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700449
450static struct nsm_handle *
451__nsm_find(const struct sockaddr_in *sin,
452 const char *hostname, int hostname_len,
453 int create)
454{
455 struct nsm_handle *nsm = NULL;
456 struct list_head *pos;
457
458 if (!sin)
459 return NULL;
460
461 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
462 if (printk_ratelimit()) {
463 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
464 "in NFS lock request\n",
465 hostname_len, hostname);
466 }
467 return NULL;
468 }
469
Neil Brown89e63ef2006-10-04 02:16:06 -0700470 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700471 list_for_each(pos, &nsm_handles) {
472 nsm = list_entry(pos, struct nsm_handle, sm_link);
473
Olaf Kirchabd1f502006-10-04 02:16:01 -0700474 if (hostname && nsm_use_hostnames) {
475 if (strlen(nsm->sm_name) != hostname_len
476 || memcmp(nsm->sm_name, hostname, hostname_len))
477 continue;
478 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700479 continue;
480 atomic_inc(&nsm->sm_count);
481 goto out;
482 }
483
484 if (!create) {
485 nsm = NULL;
486 goto out;
487 }
488
489 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
490 if (nsm != NULL) {
491 nsm->sm_addr = *sin;
492 nsm->sm_name = (char *) (nsm + 1);
493 memcpy(nsm->sm_name, hostname, hostname_len);
494 nsm->sm_name[hostname_len] = '\0';
495 atomic_set(&nsm->sm_count, 1);
496
497 list_add(&nsm->sm_link, &nsm_handles);
498 }
499
Neil Brown89e63ef2006-10-04 02:16:06 -0700500out:
501 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700502 return nsm;
503}
504
Adrian Bunkc5856462006-12-06 20:38:29 -0800505static struct nsm_handle *
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700506nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
507{
508 return __nsm_find(sin, hostname, hostname_len, 1);
509}
510
511/*
512 * Release an NSM handle
513 */
514void
515nsm_release(struct nsm_handle *nsm)
516{
517 if (!nsm)
518 return;
519 if (atomic_dec_and_test(&nsm->sm_count)) {
Neil Brown89e63ef2006-10-04 02:16:06 -0700520 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700521 if (atomic_read(&nsm->sm_count) == 0) {
522 list_del(&nsm->sm_link);
523 kfree(nsm);
524 }
Neil Brown89e63ef2006-10-04 02:16:06 -0700525 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700526 }
527}