blob: 3d4610c2a2665f9ce7236988f6bd40c52d3623bd [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);
Adrian Bunkc5856462006-12-06 20:38:29 -080039static struct nsm_handle * nsm_find(const struct sockaddr_in *sin,
40 const char *hostname,
41 int hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Common host lookup routine for server & client
45 */
Adrian Bunkc5856462006-12-06 20:38:29 -080046static struct nlm_host *
Olaf Kirchcf712c22006-10-04 02:15:52 -070047nlm_lookup_host(int server, const struct sockaddr_in *sin,
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070048 int proto, int version,
49 const char *hostname,
50 int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Olaf Kirch0cea3272006-10-04 02:15:56 -070052 struct hlist_head *chain;
53 struct hlist_node *pos;
54 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070055 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int hash;
57
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070058 dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
59 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;
95
Olaf Kirch0cea3272006-10-04 02:15:56 -070096 /* Move to head of hash chain. */
97 hlist_del(&host->h_hash);
98 hlist_add_head(&host->h_hash, chain);
99
Olaf Kirchf0737a32006-10-04 02:15:54 -0700100 nlm_get_host(host);
101 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
NeilBrown6b54dae2006-10-04 02:16:15 -0700103 if (nsm)
104 atomic_inc(&nsm->sm_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Olaf Kirch0cea3272006-10-04 02:15:56 -0700106 host = NULL;
107
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700108 /* Sadly, the host isn't in our hash table yet. See if
109 * we have an NSM handle for it. If not, create one.
110 */
111 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700114 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700115 if (!host) {
116 nsm_release(nsm);
117 goto out;
118 }
119 host->h_name = nsm->sm_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 host->h_addr = *sin;
121 host->h_addr.sin_port = 0; /* ouch! */
122 host->h_version = version;
123 host->h_proto = proto;
124 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400125 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
127 host->h_expires = jiffies + NLM_HOST_EXPIRE;
128 atomic_set(&host->h_count, 1);
129 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400130 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 host->h_state = 0; /* pseudo NSM state */
132 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700133 host->h_nsmhandle = nsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 host->h_server = server;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700135 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 INIT_LIST_HEAD(&host->h_lockowners);
137 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500138 INIT_LIST_HEAD(&host->h_granted);
139 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (++nrhosts > NLM_HOST_MAX)
142 next_gc = 0;
143
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700144out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800145 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return host;
147}
148
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700149/*
150 * Destroy a host
151 */
152static void
153nlm_destroy_host(struct nlm_host *host)
154{
155 struct rpc_clnt *clnt;
156
157 BUG_ON(!list_empty(&host->h_lockowners));
158 BUG_ON(atomic_read(&host->h_count));
159
160 /*
161 * Release NSM handle and unmonitor host.
162 */
163 nsm_unmonitor(host);
164
165 if ((clnt = host->h_rpcclnt) != NULL) {
166 if (atomic_read(&clnt->cl_users)) {
167 printk(KERN_WARNING
168 "lockd: active RPC handle\n");
169 clnt->cl_dead = 1;
170 } else {
171 rpc_destroy_client(host->h_rpcclnt);
172 }
173 }
174 kfree(host);
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
Adrian Bunkc5856462006-12-06 20:38:29 -0800178 * Find an NLM server handle in the cache. If there is none, create it.
179 */
180struct nlm_host *
181nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
182 const char *hostname, int hostname_len)
183{
184 return nlm_lookup_host(0, sin, proto, version,
185 hostname, hostname_len);
186}
187
188/*
189 * Find an NLM client handle in the cache. If there is none, create it.
190 */
191struct nlm_host *
192nlmsvc_lookup_host(struct svc_rqst *rqstp,
193 const char *hostname, int hostname_len)
194{
195 return nlm_lookup_host(1, &rqstp->rq_addr,
196 rqstp->rq_prot, rqstp->rq_vers,
197 hostname, hostname_len);
198}
199
200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * Create the NLM RPC client for an NLM peer
202 */
203struct rpc_clnt *
204nlm_bind_host(struct nlm_host *host)
205{
206 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 dprintk("lockd: nlm_bind_host(%08x)\n",
209 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
210
211 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400212 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* If we've already created an RPC client, check whether
215 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
217 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700218 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100219 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
221 dprintk("lockd: next rebind in %ld jiffies\n",
222 host->h_nextrebind - jiffies);
223 }
224 } else {
Chuck Levere1ec7892006-08-22 20:06:20 -0400225 unsigned long increment = nlmsvc_timeout * HZ;
226 struct rpc_timeout timeparms = {
227 .to_initval = increment,
228 .to_increment = increment,
229 .to_maxval = increment * 6UL,
230 .to_retries = 5U,
231 };
232 struct rpc_create_args args = {
233 .protocol = host->h_proto,
234 .address = (struct sockaddr *)&host->h_addr,
235 .addrsize = sizeof(host->h_addr),
236 .timeout = &timeparms,
237 .servername = host->h_name,
238 .program = &nlm_program,
239 .version = host->h_version,
240 .authflavor = RPC_AUTH_UNIX,
241 .flags = (RPC_CLNT_CREATE_HARDRTRY |
242 RPC_CLNT_CREATE_AUTOBIND),
243 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Chuck Levere1ec7892006-08-22 20:06:20 -0400245 clnt = rpc_create(&args);
246 if (!IS_ERR(clnt))
247 host->h_rpcclnt = clnt;
248 else {
249 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
250 clnt = NULL;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Trond Myklebust50467912006-06-09 09:40:24 -0400254 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/*
259 * Force a portmap lookup of the remote lockd port
260 */
261void
262nlm_rebind_host(struct nlm_host *host)
263{
264 dprintk("lockd: rebind host %s\n", host->h_name);
265 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100266 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
268 }
269}
270
271/*
272 * Increment NLM host count
273 */
274struct nlm_host * nlm_get_host(struct nlm_host *host)
275{
276 if (host) {
277 dprintk("lockd: get host %s\n", host->h_name);
278 atomic_inc(&host->h_count);
279 host->h_expires = jiffies + NLM_HOST_EXPIRE;
280 }
281 return host;
282}
283
284/*
285 * Release NLM host after use
286 */
287void nlm_release_host(struct nlm_host *host)
288{
289 if (host != NULL) {
290 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500292 if (atomic_dec_and_test(&host->h_count)) {
293 BUG_ON(!list_empty(&host->h_lockowners));
294 BUG_ON(!list_empty(&host->h_granted));
295 BUG_ON(!list_empty(&host->h_reclaim));
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298}
299
300/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700301 * We were notified that the host indicated by address &sin
302 * has rebooted.
303 * Release all resources held by that peer.
304 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700305void nlm_host_rebooted(const struct sockaddr_in *sin,
306 const char *hostname, int hostname_len,
307 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700308{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700309 struct hlist_head *chain;
310 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700311 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700312 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700313
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700314 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
315 hostname, NIPQUAD(sin->sin_addr));
316
317 /* Find the NSM handle for this peer */
318 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700319 return;
320
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700321 /* When reclaiming locks on this peer, make sure that
322 * we set up a new notification */
323 nsm->sm_monitored = 0;
324
325 /* Mark all hosts tied to this NSM state as having rebooted.
326 * We run the loop repeatedly, because we drop the host table
327 * lock for this.
328 * To avoid processing a host several times, we match the nsmstate.
329 */
330again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700331 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
332 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700333 if (host->h_nsmhandle == nsm
334 && host->h_nsmstate != new_state) {
335 host->h_nsmstate = new_state;
336 host->h_state++;
337
338 nlm_get_host(host);
339 mutex_unlock(&nlm_host_mutex);
340
341 if (host->h_server) {
342 /* We're server for this guy, just ditch
343 * all the locks he held. */
344 nlmsvc_free_host_resources(host);
345 } else {
346 /* He's the server, initiate lock recovery. */
347 nlmclnt_recovery(host);
348 }
349
350 nlm_release_host(host);
351 goto again;
352 }
353 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700354 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700355
356 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700357}
358
359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * Shut down the hosts module.
361 * Note that this routine is called only at server shutdown time.
362 */
363void
364nlm_shutdown_hosts(void)
365{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700366 struct hlist_head *chain;
367 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800371 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /* First, make all hosts eligible for gc */
374 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700375 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
376 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 host->h_expires = jiffies - 1;
378 }
379
380 /* Then, perform a garbage collection pass */
381 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800382 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* complain if any hosts are left */
385 if (nrhosts) {
386 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
387 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700388 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
389 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 dprintk(" %s (cnt %d use %d exp %ld)\n",
391 host->h_name, atomic_read(&host->h_count),
392 host->h_inuse, host->h_expires);
393 }
394 }
395 }
396}
397
398/*
399 * Garbage collect any unused NLM hosts.
400 * This GC combines reference counting for async operations with
401 * mark & sweep for resources held by remote clients.
402 */
403static void
404nlm_gc_hosts(void)
405{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700406 struct hlist_head *chain;
407 struct hlist_node *pos, *next;
408 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700411 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
412 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 host->h_inuse = 0;
414 }
415
416 /* Mark all hosts that hold locks, blocks or shares */
417 nlmsvc_mark_resources();
418
Olaf Kirch0cea3272006-10-04 02:15:56 -0700419 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
420 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (atomic_read(&host->h_count) || host->h_inuse
422 || time_before(jiffies, host->h_expires)) {
423 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
424 host->h_name, atomic_read(&host->h_count),
425 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 continue;
427 }
428 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700429 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700430
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700431 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 nrhosts--;
433 }
434 }
435
436 next_gc = jiffies + NLM_HOST_COLLECT;
437}
438
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700439
440/*
441 * Manage NSM handles
442 */
443static LIST_HEAD(nsm_handles);
Neil Brown89e63ef2006-10-04 02:16:06 -0700444static DEFINE_MUTEX(nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700445
446static struct nsm_handle *
447__nsm_find(const struct sockaddr_in *sin,
448 const char *hostname, int hostname_len,
449 int create)
450{
451 struct nsm_handle *nsm = NULL;
452 struct list_head *pos;
453
454 if (!sin)
455 return NULL;
456
457 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
458 if (printk_ratelimit()) {
459 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
460 "in NFS lock request\n",
461 hostname_len, hostname);
462 }
463 return NULL;
464 }
465
Neil Brown89e63ef2006-10-04 02:16:06 -0700466 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700467 list_for_each(pos, &nsm_handles) {
468 nsm = list_entry(pos, struct nsm_handle, sm_link);
469
Olaf Kirchabd1f502006-10-04 02:16:01 -0700470 if (hostname && nsm_use_hostnames) {
471 if (strlen(nsm->sm_name) != hostname_len
472 || memcmp(nsm->sm_name, hostname, hostname_len))
473 continue;
474 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700475 continue;
476 atomic_inc(&nsm->sm_count);
477 goto out;
478 }
479
480 if (!create) {
481 nsm = NULL;
482 goto out;
483 }
484
485 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
486 if (nsm != NULL) {
487 nsm->sm_addr = *sin;
488 nsm->sm_name = (char *) (nsm + 1);
489 memcpy(nsm->sm_name, hostname, hostname_len);
490 nsm->sm_name[hostname_len] = '\0';
491 atomic_set(&nsm->sm_count, 1);
492
493 list_add(&nsm->sm_link, &nsm_handles);
494 }
495
Neil Brown89e63ef2006-10-04 02:16:06 -0700496out:
497 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700498 return nsm;
499}
500
Adrian Bunkc5856462006-12-06 20:38:29 -0800501static struct nsm_handle *
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700502nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
503{
504 return __nsm_find(sin, hostname, hostname_len, 1);
505}
506
507/*
508 * Release an NSM handle
509 */
510void
511nsm_release(struct nsm_handle *nsm)
512{
513 if (!nsm)
514 return;
515 if (atomic_dec_and_test(&nsm->sm_count)) {
Neil Brown89e63ef2006-10-04 02:16:06 -0700516 mutex_lock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700517 if (atomic_read(&nsm->sm_count) == 0) {
518 list_del(&nsm->sm_link);
519 kfree(nsm);
520 }
Neil Brown89e63ef2006-10-04 02:16:06 -0700521 mutex_unlock(&nsm_mutex);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700522 }
523}