blob: 4449ef0b783716f6485894fab582b34f4658dc7d [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
167struct nlm_host *
168nlm_find_client(void)
169{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700170 struct hlist_head *chain;
171 struct hlist_node *pos;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* find a nlm_host for a client for which h_killed == 0.
174 * and return it
175 */
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800176 mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700177 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
178 struct nlm_host *host;
179
180 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (host->h_server &&
182 host->h_killed == 0) {
183 nlm_get_host(host);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800184 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return host;
186 }
187 }
188 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800189 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return NULL;
191}
192
193
194/*
195 * Create the NLM RPC client for an NLM peer
196 */
197struct rpc_clnt *
198nlm_bind_host(struct nlm_host *host)
199{
200 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 dprintk("lockd: nlm_bind_host(%08x)\n",
203 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
204
205 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400206 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* If we've already created an RPC client, check whether
209 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
211 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700212 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100213 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
215 dprintk("lockd: next rebind in %ld jiffies\n",
216 host->h_nextrebind - jiffies);
217 }
218 } else {
Chuck Levere1ec7892006-08-22 20:06:20 -0400219 unsigned long increment = nlmsvc_timeout * HZ;
220 struct rpc_timeout timeparms = {
221 .to_initval = increment,
222 .to_increment = increment,
223 .to_maxval = increment * 6UL,
224 .to_retries = 5U,
225 };
226 struct rpc_create_args args = {
227 .protocol = host->h_proto,
228 .address = (struct sockaddr *)&host->h_addr,
229 .addrsize = sizeof(host->h_addr),
230 .timeout = &timeparms,
231 .servername = host->h_name,
232 .program = &nlm_program,
233 .version = host->h_version,
234 .authflavor = RPC_AUTH_UNIX,
235 .flags = (RPC_CLNT_CREATE_HARDRTRY |
236 RPC_CLNT_CREATE_AUTOBIND),
237 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Chuck Levere1ec7892006-08-22 20:06:20 -0400239 clnt = rpc_create(&args);
240 if (!IS_ERR(clnt))
241 host->h_rpcclnt = clnt;
242 else {
243 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
244 clnt = NULL;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Trond Myklebust50467912006-06-09 09:40:24 -0400248 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
253 * Force a portmap lookup of the remote lockd port
254 */
255void
256nlm_rebind_host(struct nlm_host *host)
257{
258 dprintk("lockd: rebind host %s\n", host->h_name);
259 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100260 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
262 }
263}
264
265/*
266 * Increment NLM host count
267 */
268struct nlm_host * nlm_get_host(struct nlm_host *host)
269{
270 if (host) {
271 dprintk("lockd: get host %s\n", host->h_name);
272 atomic_inc(&host->h_count);
273 host->h_expires = jiffies + NLM_HOST_EXPIRE;
274 }
275 return host;
276}
277
278/*
279 * Release NLM host after use
280 */
281void nlm_release_host(struct nlm_host *host)
282{
283 if (host != NULL) {
284 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500286 if (atomic_dec_and_test(&host->h_count)) {
287 BUG_ON(!list_empty(&host->h_lockowners));
288 BUG_ON(!list_empty(&host->h_granted));
289 BUG_ON(!list_empty(&host->h_reclaim));
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292}
293
294/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700295 * We were notified that the host indicated by address &sin
296 * has rebooted.
297 * Release all resources held by that peer.
298 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700299void nlm_host_rebooted(const struct sockaddr_in *sin,
300 const char *hostname, int hostname_len,
301 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700302{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700303 struct hlist_head *chain;
304 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700305 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700306 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700307
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700308 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
309 hostname, NIPQUAD(sin->sin_addr));
310
311 /* Find the NSM handle for this peer */
312 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700313 return;
314
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700315 /* When reclaiming locks on this peer, make sure that
316 * we set up a new notification */
317 nsm->sm_monitored = 0;
318
319 /* Mark all hosts tied to this NSM state as having rebooted.
320 * We run the loop repeatedly, because we drop the host table
321 * lock for this.
322 * To avoid processing a host several times, we match the nsmstate.
323 */
324again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700325 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
326 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700327 if (host->h_nsmhandle == nsm
328 && host->h_nsmstate != new_state) {
329 host->h_nsmstate = new_state;
330 host->h_state++;
331
332 nlm_get_host(host);
333 mutex_unlock(&nlm_host_mutex);
334
335 if (host->h_server) {
336 /* We're server for this guy, just ditch
337 * all the locks he held. */
338 nlmsvc_free_host_resources(host);
339 } else {
340 /* He's the server, initiate lock recovery. */
341 nlmclnt_recovery(host);
342 }
343
344 nlm_release_host(host);
345 goto again;
346 }
347 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700348 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700349
350 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700351}
352
353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * Shut down the hosts module.
355 * Note that this routine is called only at server shutdown time.
356 */
357void
358nlm_shutdown_hosts(void)
359{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700360 struct hlist_head *chain;
361 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800365 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* First, make all hosts eligible for gc */
368 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700369 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
370 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 host->h_expires = jiffies - 1;
372 }
373
374 /* Then, perform a garbage collection pass */
375 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800376 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* complain if any hosts are left */
379 if (nrhosts) {
380 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
381 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700382 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
383 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dprintk(" %s (cnt %d use %d exp %ld)\n",
385 host->h_name, atomic_read(&host->h_count),
386 host->h_inuse, host->h_expires);
387 }
388 }
389 }
390}
391
392/*
393 * Garbage collect any unused NLM hosts.
394 * This GC combines reference counting for async operations with
395 * mark & sweep for resources held by remote clients.
396 */
397static void
398nlm_gc_hosts(void)
399{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700400 struct hlist_head *chain;
401 struct hlist_node *pos, *next;
402 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct rpc_clnt *clnt;
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
426 /*
427 * Unmonitor unless host was invalidated (i.e. lockd restarted)
428 */
429 nsm_unmonitor(host);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if ((clnt = host->h_rpcclnt) != NULL) {
432 if (atomic_read(&clnt->cl_users)) {
433 printk(KERN_WARNING
434 "lockd: active RPC handle\n");
435 clnt->cl_dead = 1;
436 } else {
437 rpc_destroy_client(host->h_rpcclnt);
438 }
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 kfree(host);
441 nrhosts--;
442 }
443 }
444
445 next_gc = jiffies + NLM_HOST_COLLECT;
446}
447
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700448
449/*
450 * Manage NSM handles
451 */
452static LIST_HEAD(nsm_handles);
453static DECLARE_MUTEX(nsm_sema);
454
455static struct nsm_handle *
456__nsm_find(const struct sockaddr_in *sin,
457 const char *hostname, int hostname_len,
458 int create)
459{
460 struct nsm_handle *nsm = NULL;
461 struct list_head *pos;
462
463 if (!sin)
464 return NULL;
465
466 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
467 if (printk_ratelimit()) {
468 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
469 "in NFS lock request\n",
470 hostname_len, hostname);
471 }
472 return NULL;
473 }
474
475 down(&nsm_sema);
476 list_for_each(pos, &nsm_handles) {
477 nsm = list_entry(pos, struct nsm_handle, sm_link);
478
479 if (!nlm_cmp_addr(&nsm->sm_addr, sin))
480 continue;
481 atomic_inc(&nsm->sm_count);
482 goto out;
483 }
484
485 if (!create) {
486 nsm = NULL;
487 goto out;
488 }
489
490 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
491 if (nsm != NULL) {
492 nsm->sm_addr = *sin;
493 nsm->sm_name = (char *) (nsm + 1);
494 memcpy(nsm->sm_name, hostname, hostname_len);
495 nsm->sm_name[hostname_len] = '\0';
496 atomic_set(&nsm->sm_count, 1);
497
498 list_add(&nsm->sm_link, &nsm_handles);
499 }
500
501out: up(&nsm_sema);
502 return nsm;
503}
504
505struct nsm_handle *
506nsm_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)) {
520 down(&nsm_sema);
521 if (atomic_read(&nsm->sm_count) == 0) {
522 list_del(&nsm->sm_link);
523 kfree(nsm);
524 }
525 up(&nsm_sema);
526 }
527}