blob: 0257a5594524b92e28b502cefffe80e4285f959b [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 -0700195/*
196 * Create the NLM RPC client for an NLM peer
197 */
198struct rpc_clnt *
199nlm_bind_host(struct nlm_host *host)
200{
201 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 dprintk("lockd: nlm_bind_host(%08x)\n",
204 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
205
206 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400207 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* If we've already created an RPC client, check whether
210 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
212 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700213 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100214 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
216 dprintk("lockd: next rebind in %ld jiffies\n",
217 host->h_nextrebind - jiffies);
218 }
219 } else {
Chuck Levere1ec7892006-08-22 20:06:20 -0400220 unsigned long increment = nlmsvc_timeout * HZ;
221 struct rpc_timeout timeparms = {
222 .to_initval = increment,
223 .to_increment = increment,
224 .to_maxval = increment * 6UL,
225 .to_retries = 5U,
226 };
227 struct rpc_create_args args = {
228 .protocol = host->h_proto,
229 .address = (struct sockaddr *)&host->h_addr,
230 .addrsize = sizeof(host->h_addr),
231 .timeout = &timeparms,
232 .servername = host->h_name,
233 .program = &nlm_program,
234 .version = host->h_version,
235 .authflavor = RPC_AUTH_UNIX,
236 .flags = (RPC_CLNT_CREATE_HARDRTRY |
237 RPC_CLNT_CREATE_AUTOBIND),
238 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Chuck Levere1ec7892006-08-22 20:06:20 -0400240 clnt = rpc_create(&args);
241 if (!IS_ERR(clnt))
242 host->h_rpcclnt = clnt;
243 else {
244 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
245 clnt = NULL;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
Trond Myklebust50467912006-06-09 09:40:24 -0400249 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * Force a portmap lookup of the remote lockd port
255 */
256void
257nlm_rebind_host(struct nlm_host *host)
258{
259 dprintk("lockd: rebind host %s\n", host->h_name);
260 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100261 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
263 }
264}
265
266/*
267 * Increment NLM host count
268 */
269struct nlm_host * nlm_get_host(struct nlm_host *host)
270{
271 if (host) {
272 dprintk("lockd: get host %s\n", host->h_name);
273 atomic_inc(&host->h_count);
274 host->h_expires = jiffies + NLM_HOST_EXPIRE;
275 }
276 return host;
277}
278
279/*
280 * Release NLM host after use
281 */
282void nlm_release_host(struct nlm_host *host)
283{
284 if (host != NULL) {
285 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500287 if (atomic_dec_and_test(&host->h_count)) {
288 BUG_ON(!list_empty(&host->h_lockowners));
289 BUG_ON(!list_empty(&host->h_granted));
290 BUG_ON(!list_empty(&host->h_reclaim));
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293}
294
295/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700296 * We were notified that the host indicated by address &sin
297 * has rebooted.
298 * Release all resources held by that peer.
299 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700300void nlm_host_rebooted(const struct sockaddr_in *sin,
301 const char *hostname, int hostname_len,
302 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700303{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700304 struct hlist_head *chain;
305 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700306 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700307 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700308
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700309 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
310 hostname, NIPQUAD(sin->sin_addr));
311
312 /* Find the NSM handle for this peer */
313 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700314 return;
315
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700316 /* When reclaiming locks on this peer, make sure that
317 * we set up a new notification */
318 nsm->sm_monitored = 0;
319
320 /* Mark all hosts tied to this NSM state as having rebooted.
321 * We run the loop repeatedly, because we drop the host table
322 * lock for this.
323 * To avoid processing a host several times, we match the nsmstate.
324 */
325again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700326 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
327 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700328 if (host->h_nsmhandle == nsm
329 && host->h_nsmstate != new_state) {
330 host->h_nsmstate = new_state;
331 host->h_state++;
332
333 nlm_get_host(host);
334 mutex_unlock(&nlm_host_mutex);
335
336 if (host->h_server) {
337 /* We're server for this guy, just ditch
338 * all the locks he held. */
339 nlmsvc_free_host_resources(host);
340 } else {
341 /* He's the server, initiate lock recovery. */
342 nlmclnt_recovery(host);
343 }
344
345 nlm_release_host(host);
346 goto again;
347 }
348 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700349 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700350
351 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700352}
353
354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * Shut down the hosts module.
356 * Note that this routine is called only at server shutdown time.
357 */
358void
359nlm_shutdown_hosts(void)
360{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700361 struct hlist_head *chain;
362 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800366 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* First, make all hosts eligible for gc */
369 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700370 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
371 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 host->h_expires = jiffies - 1;
373 }
374
375 /* Then, perform a garbage collection pass */
376 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800377 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* complain if any hosts are left */
380 if (nrhosts) {
381 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
382 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700383 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
384 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dprintk(" %s (cnt %d use %d exp %ld)\n",
386 host->h_name, atomic_read(&host->h_count),
387 host->h_inuse, host->h_expires);
388 }
389 }
390 }
391}
392
393/*
394 * Garbage collect any unused NLM hosts.
395 * This GC combines reference counting for async operations with
396 * mark & sweep for resources held by remote clients.
397 */
398static void
399nlm_gc_hosts(void)
400{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700401 struct hlist_head *chain;
402 struct hlist_node *pos, *next;
403 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 dprintk("lockd: host garbage collection\n");
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 host->h_inuse = 0;
409 }
410
411 /* Mark all hosts that hold locks, blocks or shares */
412 nlmsvc_mark_resources();
413
Olaf Kirch0cea3272006-10-04 02:15:56 -0700414 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
415 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (atomic_read(&host->h_count) || host->h_inuse
417 || time_before(jiffies, host->h_expires)) {
418 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
419 host->h_name, atomic_read(&host->h_count),
420 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 continue;
422 }
423 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700424 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700425
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700426 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 nrhosts--;
428 }
429 }
430
431 next_gc = jiffies + NLM_HOST_COLLECT;
432}
433
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700434
435/*
436 * Manage NSM handles
437 */
438static LIST_HEAD(nsm_handles);
Neil Brown89e63ef2006-10-04 02:16:06 -0700439static DEFINE_MUTEX(nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700440
441static struct nsm_handle *
442__nsm_find(const struct sockaddr_in *sin,
443 const char *hostname, int hostname_len,
444 int create)
445{
446 struct nsm_handle *nsm = NULL;
447 struct list_head *pos;
448
449 if (!sin)
450 return NULL;
451
452 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
453 if (printk_ratelimit()) {
454 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
455 "in NFS lock request\n",
456 hostname_len, hostname);
457 }
458 return NULL;
459 }
460
Neil Brown89e63ef2006-10-04 02:16:06 -0700461 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700462 list_for_each(pos, &nsm_handles) {
463 nsm = list_entry(pos, struct nsm_handle, sm_link);
464
Olaf Kirchabd1f502006-10-04 02:16:01 -0700465 if (hostname && nsm_use_hostnames) {
466 if (strlen(nsm->sm_name) != hostname_len
467 || memcmp(nsm->sm_name, hostname, hostname_len))
468 continue;
469 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700470 continue;
471 atomic_inc(&nsm->sm_count);
472 goto out;
473 }
474
475 if (!create) {
476 nsm = NULL;
477 goto out;
478 }
479
480 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
481 if (nsm != NULL) {
482 nsm->sm_addr = *sin;
483 nsm->sm_name = (char *) (nsm + 1);
484 memcpy(nsm->sm_name, hostname, hostname_len);
485 nsm->sm_name[hostname_len] = '\0';
486 atomic_set(&nsm->sm_count, 1);
487
488 list_add(&nsm->sm_link, &nsm_handles);
489 }
490
Neil Brown89e63ef2006-10-04 02:16:06 -0700491out:
492 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700493 return nsm;
494}
495
496struct nsm_handle *
497nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
498{
499 return __nsm_find(sin, hostname, hostname_len, 1);
500}
501
502/*
503 * Release an NSM handle
504 */
505void
506nsm_release(struct nsm_handle *nsm)
507{
508 if (!nsm)
509 return;
510 if (atomic_dec_and_test(&nsm->sm_count)) {
Neil Brown89e63ef2006-10-04 02:16:06 -0700511 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700512 if (atomic_read(&nsm->sm_count) == 0) {
513 list_del(&nsm->sm_link);
514 kfree(nsm);
515 }
Neil Brown89e63ef2006-10-04 02:16:06 -0700516 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700517 }
518}