blob: 3b55fe5e94a2f5f514037da4bcc54ddc50cd4699 [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>
12#include <linux/sched.h>
13#include <linux/slab.h>
14#include <linux/in.h>
15#include <linux/sunrpc/clnt.h>
16#include <linux/sunrpc/svc.h>
17#include <linux/lockd/lockd.h>
18#include <linux/lockd/sm_inter.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21
22#define NLMDBG_FACILITY NLMDBG_HOSTCACHE
23#define NLM_HOST_MAX 64
24#define NLM_HOST_NRHASH 32
25#define NLM_ADDRHASH(addr) (ntohl(addr) & (NLM_HOST_NRHASH-1))
26#define NLM_HOST_REBIND (60 * HZ)
27#define NLM_HOST_EXPIRE ((nrhosts > NLM_HOST_MAX)? 300 * HZ : 120 * HZ)
28#define NLM_HOST_COLLECT ((nrhosts > NLM_HOST_MAX)? 120 * HZ : 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Olaf Kirch0cea3272006-10-04 02:15:56 -070030static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static unsigned long next_gc;
32static int nrhosts;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080033static DEFINE_MUTEX(nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35
36static void nlm_gc_hosts(void);
Olaf Kirch8dead0d2006-10-04 02:15:53 -070037static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
38 const char *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Find an NLM server handle in the cache. If there is none, create it.
42 */
43struct nlm_host *
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070044nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
45 const char *hostname, int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070047 return nlm_lookup_host(0, sin, proto, version,
48 hostname, hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51/*
52 * Find an NLM client handle in the cache. If there is none, create it.
53 */
54struct nlm_host *
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070055nlmsvc_lookup_host(struct svc_rqst *rqstp,
56 const char *hostname, int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 return nlm_lookup_host(1, &rqstp->rq_addr,
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070059 rqstp->rq_prot, rqstp->rq_vers,
60 hostname, hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63/*
64 * Common host lookup routine for server & client
65 */
66struct nlm_host *
Olaf Kirchcf712c22006-10-04 02:15:52 -070067nlm_lookup_host(int server, const struct sockaddr_in *sin,
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070068 int proto, int version,
69 const char *hostname,
70 int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Olaf Kirch0cea3272006-10-04 02:15:56 -070072 struct hlist_head *chain;
73 struct hlist_node *pos;
74 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070075 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int hash;
77
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070078 dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
79 NIPQUAD(sin->sin_addr.s_addr), proto, version,
80 server? "server" : "client",
81 hostname_len,
82 hostname? hostname : "<none>");
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
86
87 /* Lock hash table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080088 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (time_after_eq(jiffies, next_gc))
91 nlm_gc_hosts();
92
Olaf Kirch8dead0d2006-10-04 02:15:53 -070093 /* We may keep several nlm_host objects for a peer, because each
94 * nlm_host is identified by
95 * (address, protocol, version, server/client)
96 * We could probably simplify this a little by putting all those
97 * different NLM rpc_clients into one single nlm_host object.
98 * This would allow us to have one nlm_host per address.
99 */
Olaf Kirch0cea3272006-10-04 02:15:56 -0700100 chain = &nlm_hosts[hash];
101 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700102 if (!nlm_cmp_addr(&host->h_addr, sin))
103 continue;
104
105 /* See if we have an NSM handle for this client */
106 if (!nsm && (nsm = host->h_nsmhandle) != 0)
107 atomic_inc(&nsm->sm_count);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (host->h_proto != proto)
110 continue;
111 if (host->h_version != version)
112 continue;
113 if (host->h_server != server)
114 continue;
115
Olaf Kirch0cea3272006-10-04 02:15:56 -0700116 /* Move to head of hash chain. */
117 hlist_del(&host->h_hash);
118 hlist_add_head(&host->h_hash, chain);
119
Olaf Kirchf0737a32006-10-04 02:15:54 -0700120 nlm_get_host(host);
121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Olaf Kirch0cea3272006-10-04 02:15:56 -0700124 host = NULL;
125
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700126 /* Sadly, the host isn't in our hash table yet. See if
127 * we have an NSM handle for it. If not, create one.
128 */
129 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700132 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700133 if (!host) {
134 nsm_release(nsm);
135 goto out;
136 }
137 host->h_name = nsm->sm_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 host->h_addr = *sin;
139 host->h_addr.sin_port = 0; /* ouch! */
140 host->h_version = version;
141 host->h_proto = proto;
142 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400143 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
145 host->h_expires = jiffies + NLM_HOST_EXPIRE;
146 atomic_set(&host->h_count, 1);
147 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400148 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 host->h_state = 0; /* pseudo NSM state */
150 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700151 host->h_nsmhandle = nsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 host->h_server = server;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700153 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 INIT_LIST_HEAD(&host->h_lockowners);
155 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500156 INIT_LIST_HEAD(&host->h_granted);
157 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (++nrhosts > NLM_HOST_MAX)
160 next_gc = 0;
161
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700162out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800163 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return host;
165}
166
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700167/*
168 * Destroy a host
169 */
170static void
171nlm_destroy_host(struct nlm_host *host)
172{
173 struct rpc_clnt *clnt;
174
175 BUG_ON(!list_empty(&host->h_lockowners));
176 BUG_ON(atomic_read(&host->h_count));
177
178 /*
179 * Release NSM handle and unmonitor host.
180 */
181 nsm_unmonitor(host);
182
183 if ((clnt = host->h_rpcclnt) != NULL) {
184 if (atomic_read(&clnt->cl_users)) {
185 printk(KERN_WARNING
186 "lockd: active RPC handle\n");
187 clnt->cl_dead = 1;
188 } else {
189 rpc_destroy_client(host->h_rpcclnt);
190 }
191 }
192 kfree(host);
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195struct nlm_host *
196nlm_find_client(void)
197{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700198 struct hlist_head *chain;
199 struct hlist_node *pos;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* find a nlm_host for a client for which h_killed == 0.
202 * and return it
203 */
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800204 mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700205 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
206 struct nlm_host *host;
207
208 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (host->h_server &&
210 host->h_killed == 0) {
211 nlm_get_host(host);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800212 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return host;
214 }
215 }
216 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800217 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return NULL;
219}
220
221
222/*
223 * Create the NLM RPC client for an NLM peer
224 */
225struct rpc_clnt *
226nlm_bind_host(struct nlm_host *host)
227{
228 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 dprintk("lockd: nlm_bind_host(%08x)\n",
231 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
232
233 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400234 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* If we've already created an RPC client, check whether
237 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700240 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100241 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
243 dprintk("lockd: next rebind in %ld jiffies\n",
244 host->h_nextrebind - jiffies);
245 }
246 } else {
Chuck Levere1ec7892006-08-22 20:06:20 -0400247 unsigned long increment = nlmsvc_timeout * HZ;
248 struct rpc_timeout timeparms = {
249 .to_initval = increment,
250 .to_increment = increment,
251 .to_maxval = increment * 6UL,
252 .to_retries = 5U,
253 };
254 struct rpc_create_args args = {
255 .protocol = host->h_proto,
256 .address = (struct sockaddr *)&host->h_addr,
257 .addrsize = sizeof(host->h_addr),
258 .timeout = &timeparms,
259 .servername = host->h_name,
260 .program = &nlm_program,
261 .version = host->h_version,
262 .authflavor = RPC_AUTH_UNIX,
263 .flags = (RPC_CLNT_CREATE_HARDRTRY |
264 RPC_CLNT_CREATE_AUTOBIND),
265 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Chuck Levere1ec7892006-08-22 20:06:20 -0400267 clnt = rpc_create(&args);
268 if (!IS_ERR(clnt))
269 host->h_rpcclnt = clnt;
270 else {
271 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
272 clnt = NULL;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
Trond Myklebust50467912006-06-09 09:40:24 -0400276 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * Force a portmap lookup of the remote lockd port
282 */
283void
284nlm_rebind_host(struct nlm_host *host)
285{
286 dprintk("lockd: rebind host %s\n", host->h_name);
287 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100288 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
290 }
291}
292
293/*
294 * Increment NLM host count
295 */
296struct nlm_host * nlm_get_host(struct nlm_host *host)
297{
298 if (host) {
299 dprintk("lockd: get host %s\n", host->h_name);
300 atomic_inc(&host->h_count);
301 host->h_expires = jiffies + NLM_HOST_EXPIRE;
302 }
303 return host;
304}
305
306/*
307 * Release NLM host after use
308 */
309void nlm_release_host(struct nlm_host *host)
310{
311 if (host != NULL) {
312 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500314 if (atomic_dec_and_test(&host->h_count)) {
315 BUG_ON(!list_empty(&host->h_lockowners));
316 BUG_ON(!list_empty(&host->h_granted));
317 BUG_ON(!list_empty(&host->h_reclaim));
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320}
321
322/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700323 * We were notified that the host indicated by address &sin
324 * has rebooted.
325 * Release all resources held by that peer.
326 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700327void nlm_host_rebooted(const struct sockaddr_in *sin,
328 const char *hostname, int hostname_len,
329 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700330{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700331 struct hlist_head *chain;
332 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700333 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700334 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700335
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700336 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
337 hostname, NIPQUAD(sin->sin_addr));
338
339 /* Find the NSM handle for this peer */
340 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700341 return;
342
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700343 /* When reclaiming locks on this peer, make sure that
344 * we set up a new notification */
345 nsm->sm_monitored = 0;
346
347 /* Mark all hosts tied to this NSM state as having rebooted.
348 * We run the loop repeatedly, because we drop the host table
349 * lock for this.
350 * To avoid processing a host several times, we match the nsmstate.
351 */
352again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700353 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
354 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700355 if (host->h_nsmhandle == nsm
356 && host->h_nsmstate != new_state) {
357 host->h_nsmstate = new_state;
358 host->h_state++;
359
360 nlm_get_host(host);
361 mutex_unlock(&nlm_host_mutex);
362
363 if (host->h_server) {
364 /* We're server for this guy, just ditch
365 * all the locks he held. */
366 nlmsvc_free_host_resources(host);
367 } else {
368 /* He's the server, initiate lock recovery. */
369 nlmclnt_recovery(host);
370 }
371
372 nlm_release_host(host);
373 goto again;
374 }
375 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700376 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700377
378 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700379}
380
381/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * Shut down the hosts module.
383 * Note that this routine is called only at server shutdown time.
384 */
385void
386nlm_shutdown_hosts(void)
387{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700388 struct hlist_head *chain;
389 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800393 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* First, make all hosts eligible for gc */
396 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700397 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
398 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 host->h_expires = jiffies - 1;
400 }
401
402 /* Then, perform a garbage collection pass */
403 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800404 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* complain if any hosts are left */
407 if (nrhosts) {
408 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
409 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700410 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
411 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 dprintk(" %s (cnt %d use %d exp %ld)\n",
413 host->h_name, atomic_read(&host->h_count),
414 host->h_inuse, host->h_expires);
415 }
416 }
417 }
418}
419
420/*
421 * Garbage collect any unused NLM hosts.
422 * This GC combines reference counting for async operations with
423 * mark & sweep for resources held by remote clients.
424 */
425static void
426nlm_gc_hosts(void)
427{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700428 struct hlist_head *chain;
429 struct hlist_node *pos, *next;
430 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700433 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
434 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 host->h_inuse = 0;
436 }
437
438 /* Mark all hosts that hold locks, blocks or shares */
439 nlmsvc_mark_resources();
440
Olaf Kirch0cea3272006-10-04 02:15:56 -0700441 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
442 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (atomic_read(&host->h_count) || host->h_inuse
444 || time_before(jiffies, host->h_expires)) {
445 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
446 host->h_name, atomic_read(&host->h_count),
447 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 continue;
449 }
450 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700451 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700452
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700453 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 nrhosts--;
455 }
456 }
457
458 next_gc = jiffies + NLM_HOST_COLLECT;
459}
460
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700461
462/*
463 * Manage NSM handles
464 */
465static LIST_HEAD(nsm_handles);
466static DECLARE_MUTEX(nsm_sema);
467
468static struct nsm_handle *
469__nsm_find(const struct sockaddr_in *sin,
470 const char *hostname, int hostname_len,
471 int create)
472{
473 struct nsm_handle *nsm = NULL;
474 struct list_head *pos;
475
476 if (!sin)
477 return NULL;
478
479 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
480 if (printk_ratelimit()) {
481 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
482 "in NFS lock request\n",
483 hostname_len, hostname);
484 }
485 return NULL;
486 }
487
488 down(&nsm_sema);
489 list_for_each(pos, &nsm_handles) {
490 nsm = list_entry(pos, struct nsm_handle, sm_link);
491
492 if (!nlm_cmp_addr(&nsm->sm_addr, sin))
493 continue;
494 atomic_inc(&nsm->sm_count);
495 goto out;
496 }
497
498 if (!create) {
499 nsm = NULL;
500 goto out;
501 }
502
503 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
504 if (nsm != NULL) {
505 nsm->sm_addr = *sin;
506 nsm->sm_name = (char *) (nsm + 1);
507 memcpy(nsm->sm_name, hostname, hostname_len);
508 nsm->sm_name[hostname_len] = '\0';
509 atomic_set(&nsm->sm_count, 1);
510
511 list_add(&nsm->sm_link, &nsm_handles);
512 }
513
514out: up(&nsm_sema);
515 return nsm;
516}
517
518struct nsm_handle *
519nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
520{
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)) {
533 down(&nsm_sema);
534 if (atomic_read(&nsm->sm_count) == 0) {
535 list_del(&nsm->sm_link);
536 kfree(nsm);
537 }
538 up(&nsm_sema);
539 }
540}