blob: c252a1c95857ce2a21c0e8a23b3baff8bfd1408a [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,
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070047 int proto, int version,
48 const char *hostname,
49 int hostname_len)
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
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070057 dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
58 NIPQUAD(sin->sin_addr.s_addr), proto, version,
59 server? "server" : "client",
60 hostname_len,
61 hostname? hostname : "<none>");
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
65
66 /* Lock hash table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080067 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if (time_after_eq(jiffies, next_gc))
70 nlm_gc_hosts();
71
Olaf Kirch8dead0d2006-10-04 02:15:53 -070072 /* We may keep several nlm_host objects for a peer, because each
73 * nlm_host is identified by
74 * (address, protocol, version, server/client)
75 * We could probably simplify this a little by putting all those
76 * different NLM rpc_clients into one single nlm_host object.
77 * This would allow us to have one nlm_host per address.
78 */
Olaf Kirch0cea3272006-10-04 02:15:56 -070079 chain = &nlm_hosts[hash];
80 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch8dead0d2006-10-04 02:15:53 -070081 if (!nlm_cmp_addr(&host->h_addr, sin))
82 continue;
83
84 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -070085 if (!nsm)
86 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (host->h_proto != proto)
89 continue;
90 if (host->h_version != version)
91 continue;
92 if (host->h_server != server)
93 continue;
94
Olaf Kirch0cea3272006-10-04 02:15:56 -070095 /* Move to head of hash chain. */
96 hlist_del(&host->h_hash);
97 hlist_add_head(&host->h_hash, chain);
98
Olaf Kirchf0737a32006-10-04 02:15:54 -070099 nlm_get_host(host);
100 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
NeilBrown6b54dae2006-10-04 02:16:15 -0700102 if (nsm)
103 atomic_inc(&nsm->sm_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Olaf Kirch0cea3272006-10-04 02:15:56 -0700105 host = NULL;
106
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700107 /* Sadly, the host isn't in our hash table yet. See if
108 * we have an NSM handle for it. If not, create one.
109 */
110 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
111 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700113 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700114 if (!host) {
115 nsm_release(nsm);
116 goto out;
117 }
118 host->h_name = nsm->sm_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 host->h_addr = *sin;
120 host->h_addr.sin_port = 0; /* ouch! */
121 host->h_version = version;
122 host->h_proto = proto;
123 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400124 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
126 host->h_expires = jiffies + NLM_HOST_EXPIRE;
127 atomic_set(&host->h_count, 1);
128 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400129 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 host->h_state = 0; /* pseudo NSM state */
131 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700132 host->h_nsmhandle = nsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 host->h_server = server;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700134 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 INIT_LIST_HEAD(&host->h_lockowners);
136 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500137 INIT_LIST_HEAD(&host->h_granted);
138 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (++nrhosts > NLM_HOST_MAX)
141 next_gc = 0;
142
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700143out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800144 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return host;
146}
147
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700148/*
149 * Destroy a host
150 */
151static void
152nlm_destroy_host(struct nlm_host *host)
153{
154 struct rpc_clnt *clnt;
155
156 BUG_ON(!list_empty(&host->h_lockowners));
157 BUG_ON(atomic_read(&host->h_count));
158
159 /*
160 * Release NSM handle and unmonitor host.
161 */
162 nsm_unmonitor(host);
163
Trond Myklebust34f52e32007-06-14 16:40:31 -0400164 clnt = host->h_rpcclnt;
165 if (clnt != NULL)
166 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700167 kfree(host);
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
Adrian Bunkc5856462006-12-06 20:38:29 -0800171 * Find an NLM server handle in the cache. If there is none, create it.
172 */
173struct nlm_host *
174nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
175 const char *hostname, int hostname_len)
176{
177 return nlm_lookup_host(0, sin, proto, version,
178 hostname, hostname_len);
179}
180
181/*
182 * Find an NLM client handle in the cache. If there is none, create it.
183 */
184struct nlm_host *
185nlmsvc_lookup_host(struct svc_rqst *rqstp,
186 const char *hostname, int hostname_len)
187{
Chuck Lever27459f02007-02-12 00:53:34 -0800188 return nlm_lookup_host(1, svc_addr_in(rqstp),
Adrian Bunkc5856462006-12-06 20:38:29 -0800189 rqstp->rq_prot, rqstp->rq_vers,
190 hostname, hostname_len);
191}
192
193/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * Create the NLM RPC client for an NLM peer
195 */
196struct rpc_clnt *
197nlm_bind_host(struct nlm_host *host)
198{
199 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 dprintk("lockd: nlm_bind_host(%08x)\n",
202 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
203
204 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400205 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* If we've already created an RPC client, check whether
208 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700211 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100212 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
214 dprintk("lockd: next rebind in %ld jiffies\n",
215 host->h_nextrebind - jiffies);
216 }
217 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400218 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400219 struct rpc_timeout timeparms = {
220 .to_initval = increment,
221 .to_increment = increment,
222 .to_maxval = increment * 6UL,
223 .to_retries = 5U,
224 };
225 struct rpc_create_args args = {
226 .protocol = host->h_proto,
227 .address = (struct sockaddr *)&host->h_addr,
228 .addrsize = sizeof(host->h_addr),
229 .timeout = &timeparms,
230 .servername = host->h_name,
231 .program = &nlm_program,
232 .version = host->h_version,
233 .authflavor = RPC_AUTH_UNIX,
234 .flags = (RPC_CLNT_CREATE_HARDRTRY |
235 RPC_CLNT_CREATE_AUTOBIND),
236 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Chuck Levere1ec7892006-08-22 20:06:20 -0400238 clnt = rpc_create(&args);
239 if (!IS_ERR(clnt))
240 host->h_rpcclnt = clnt;
241 else {
242 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
243 clnt = NULL;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Trond Myklebust50467912006-06-09 09:40:24 -0400247 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/*
252 * Force a portmap lookup of the remote lockd port
253 */
254void
255nlm_rebind_host(struct nlm_host *host)
256{
257 dprintk("lockd: rebind host %s\n", host->h_name);
258 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100259 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
261 }
262}
263
264/*
265 * Increment NLM host count
266 */
267struct nlm_host * nlm_get_host(struct nlm_host *host)
268{
269 if (host) {
270 dprintk("lockd: get host %s\n", host->h_name);
271 atomic_inc(&host->h_count);
272 host->h_expires = jiffies + NLM_HOST_EXPIRE;
273 }
274 return host;
275}
276
277/*
278 * Release NLM host after use
279 */
280void nlm_release_host(struct nlm_host *host)
281{
282 if (host != NULL) {
283 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500285 if (atomic_dec_and_test(&host->h_count)) {
286 BUG_ON(!list_empty(&host->h_lockowners));
287 BUG_ON(!list_empty(&host->h_granted));
288 BUG_ON(!list_empty(&host->h_reclaim));
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291}
292
293/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700294 * We were notified that the host indicated by address &sin
295 * has rebooted.
296 * Release all resources held by that peer.
297 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700298void nlm_host_rebooted(const struct sockaddr_in *sin,
299 const char *hostname, int hostname_len,
300 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700301{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700302 struct hlist_head *chain;
303 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700304 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700305 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700306
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700307 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
308 hostname, NIPQUAD(sin->sin_addr));
309
310 /* Find the NSM handle for this peer */
311 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700312 return;
313
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700314 /* When reclaiming locks on this peer, make sure that
315 * we set up a new notification */
316 nsm->sm_monitored = 0;
317
318 /* Mark all hosts tied to this NSM state as having rebooted.
319 * We run the loop repeatedly, because we drop the host table
320 * lock for this.
321 * To avoid processing a host several times, we match the nsmstate.
322 */
323again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700324 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
325 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700326 if (host->h_nsmhandle == nsm
327 && host->h_nsmstate != new_state) {
328 host->h_nsmstate = new_state;
329 host->h_state++;
330
331 nlm_get_host(host);
332 mutex_unlock(&nlm_host_mutex);
333
334 if (host->h_server) {
335 /* We're server for this guy, just ditch
336 * all the locks he held. */
337 nlmsvc_free_host_resources(host);
338 } else {
339 /* He's the server, initiate lock recovery. */
340 nlmclnt_recovery(host);
341 }
342
343 nlm_release_host(host);
344 goto again;
345 }
346 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700347 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700348
349 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700350}
351
352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Shut down the hosts module.
354 * Note that this routine is called only at server shutdown time.
355 */
356void
357nlm_shutdown_hosts(void)
358{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700359 struct hlist_head *chain;
360 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800364 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* First, make all hosts eligible for gc */
367 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700368 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
369 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 host->h_expires = jiffies - 1;
371 }
372
373 /* Then, perform a garbage collection pass */
374 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800375 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* complain if any hosts are left */
378 if (nrhosts) {
379 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
380 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700381 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
382 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dprintk(" %s (cnt %d use %d exp %ld)\n",
384 host->h_name, atomic_read(&host->h_count),
385 host->h_inuse, host->h_expires);
386 }
387 }
388 }
389}
390
391/*
392 * Garbage collect any unused NLM hosts.
393 * This GC combines reference counting for async operations with
394 * mark & sweep for resources held by remote clients.
395 */
396static void
397nlm_gc_hosts(void)
398{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700399 struct hlist_head *chain;
400 struct hlist_node *pos, *next;
401 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700404 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
405 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 host->h_inuse = 0;
407 }
408
409 /* Mark all hosts that hold locks, blocks or shares */
410 nlmsvc_mark_resources();
411
Olaf Kirch0cea3272006-10-04 02:15:56 -0700412 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
413 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (atomic_read(&host->h_count) || host->h_inuse
415 || time_before(jiffies, host->h_expires)) {
416 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
417 host->h_name, atomic_read(&host->h_count),
418 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 continue;
420 }
421 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700422 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700423
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700424 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 nrhosts--;
426 }
427 }
428
429 next_gc = jiffies + NLM_HOST_COLLECT;
430}
431
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700432
433/*
434 * Manage NSM handles
435 */
436static LIST_HEAD(nsm_handles);
Neil Brown89e63ef2006-10-04 02:16:06 -0700437static DEFINE_MUTEX(nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700438
439static struct nsm_handle *
440__nsm_find(const struct sockaddr_in *sin,
441 const char *hostname, int hostname_len,
442 int create)
443{
444 struct nsm_handle *nsm = NULL;
445 struct list_head *pos;
446
447 if (!sin)
448 return NULL;
449
450 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
451 if (printk_ratelimit()) {
452 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
453 "in NFS lock request\n",
454 hostname_len, hostname);
455 }
456 return NULL;
457 }
458
Neil Brown89e63ef2006-10-04 02:16:06 -0700459 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700460 list_for_each(pos, &nsm_handles) {
461 nsm = list_entry(pos, struct nsm_handle, sm_link);
462
Olaf Kirchabd1f502006-10-04 02:16:01 -0700463 if (hostname && nsm_use_hostnames) {
464 if (strlen(nsm->sm_name) != hostname_len
465 || memcmp(nsm->sm_name, hostname, hostname_len))
466 continue;
467 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700468 continue;
469 atomic_inc(&nsm->sm_count);
470 goto out;
471 }
472
473 if (!create) {
474 nsm = NULL;
475 goto out;
476 }
477
478 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
479 if (nsm != NULL) {
480 nsm->sm_addr = *sin;
481 nsm->sm_name = (char *) (nsm + 1);
482 memcpy(nsm->sm_name, hostname, hostname_len);
483 nsm->sm_name[hostname_len] = '\0';
484 atomic_set(&nsm->sm_count, 1);
485
486 list_add(&nsm->sm_link, &nsm_handles);
487 }
488
Neil Brown89e63ef2006-10-04 02:16:06 -0700489out:
490 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700491 return nsm;
492}
493
Adrian Bunkc5856462006-12-06 20:38:29 -0800494static struct nsm_handle *
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700495nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
496{
497 return __nsm_find(sin, hostname, hostname_len, 1);
498}
499
500/*
501 * Release an NSM handle
502 */
503void
504nsm_release(struct nsm_handle *nsm)
505{
506 if (!nsm)
507 return;
508 if (atomic_dec_and_test(&nsm->sm_count)) {
Neil Brown89e63ef2006-10-04 02:16:06 -0700509 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700510 if (atomic_read(&nsm->sm_count) == 0) {
511 list_del(&nsm->sm_link);
512 kfree(nsm);
513 }
Neil Brown89e63ef2006-10-04 02:16:06 -0700514 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700515 }
516}