blob: 6d4aa8b3d610bd513791af8b7715d66b06f6ec1e [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>
Chuck Lever1b333c52008-08-27 16:57:23 -040014#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
16#include <linux/sunrpc/svc.h>
17#include <linux/lockd/lockd.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080018#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Chuck Lever1b333c52008-08-27 16:57:23 -040020#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define NLMDBG_FACILITY NLMDBG_HOSTCACHE
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define NLM_HOST_NRHASH 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define NLM_HOST_REBIND (60 * HZ)
NeilBrown1447d252008-02-08 13:03:37 +110025#define NLM_HOST_EXPIRE (300 * HZ)
26#define NLM_HOST_COLLECT (120 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Chuck Leverd2df0482010-12-14 15:06:22 +000028static struct hlist_head nlm_server_hosts[NLM_HOST_NRHASH];
Chuck Lever8ea6ecc2010-12-14 15:05:52 +000029static struct hlist_head nlm_client_hosts[NLM_HOST_NRHASH];
J. Bruce Fieldsb1137462010-12-14 15:05:03 +000030
31#define for_each_host(host, pos, chain, table) \
32 for ((chain) = (table); \
33 (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \
34 hlist_for_each_entry((host), (pos), (chain), h_hash)
35
36#define for_each_host_safe(host, pos, next, chain, table) \
37 for ((chain) = (table); \
38 (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \
39 hlist_for_each_entry_safe((host), (pos), (next), \
40 (chain), h_hash)
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static unsigned long next_gc;
Chuck Leverfcc072c2010-12-14 15:06:32 +000043static unsigned long nrhosts;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080044static DEFINE_MUTEX(nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static void nlm_gc_hosts(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Chuck Lever7f1ed182008-10-03 12:50:07 -040048struct nlm_lookup_host_info {
49 const int server; /* search for server|client */
Chuck Lever88541c82008-10-03 12:50:14 -040050 const struct sockaddr *sap; /* address to search for */
51 const size_t salen; /* it's length */
Chuck Lever7f1ed182008-10-03 12:50:07 -040052 const unsigned short protocol; /* transport to search for*/
53 const u32 version; /* NLM version to search for */
54 const char *hostname; /* remote's hostname */
55 const size_t hostname_len; /* it's length */
Chuck Lever88541c82008-10-03 12:50:14 -040056 const struct sockaddr *src_sap; /* our address (optional) */
Chuck Lever7f1ed182008-10-03 12:50:07 -040057 const size_t src_len; /* it's length */
Chuck Lever0cb26592008-12-23 15:21:38 -050058 const int noresvport; /* use non-priv port */
Chuck Lever7f1ed182008-10-03 12:50:07 -040059};
60
Chuck Leverede2fea2008-09-03 14:36:08 -040061/*
62 * Hash function must work well on big- and little-endian platforms
63 */
64static unsigned int __nlm_hash32(const __be32 n)
65{
66 unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
67 return hash ^ (hash >> 8);
68}
69
70static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
71{
72 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
73 return __nlm_hash32(sin->sin_addr.s_addr);
74}
75
76static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
77{
78 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
79 const struct in6_addr addr = sin6->sin6_addr;
80 return __nlm_hash32(addr.s6_addr32[0]) ^
81 __nlm_hash32(addr.s6_addr32[1]) ^
82 __nlm_hash32(addr.s6_addr32[2]) ^
83 __nlm_hash32(addr.s6_addr32[3]);
84}
85
86static unsigned int nlm_hash_address(const struct sockaddr *sap)
87{
88 unsigned int hash;
89
90 switch (sap->sa_family) {
91 case AF_INET:
92 hash = __nlm_hash_addr4(sap);
93 break;
94 case AF_INET6:
95 hash = __nlm_hash_addr6(sap);
96 break;
97 default:
98 hash = 0;
99 }
100 return hash & (NLM_HOST_NRHASH - 1);
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
Chuck Levera7952f42010-12-14 15:05:23 +0000104 * Allocate and initialize an nlm_host. Common to both client and server.
105 */
106static struct nlm_host *nlm_alloc_host(struct nlm_lookup_host_info *ni,
107 struct nsm_handle *nsm)
108{
109 struct nlm_host *host = NULL;
110 unsigned long now = jiffies;
111
112 if (nsm != NULL)
113 atomic_inc(&nsm->sm_count);
114 else {
115 host = NULL;
116 nsm = nsm_get_handle(ni->sap, ni->salen,
117 ni->hostname, ni->hostname_len);
118 if (unlikely(nsm == NULL)) {
119 dprintk("lockd: %s failed; no nsm handle\n",
120 __func__);
121 goto out;
122 }
123 }
124
125 host = kmalloc(sizeof(*host), GFP_KERNEL);
126 if (unlikely(host == NULL)) {
127 dprintk("lockd: %s failed; no memory\n", __func__);
128 nsm_release(nsm);
129 goto out;
130 }
131
132 memcpy(nlm_addr(host), ni->sap, ni->salen);
133 host->h_addrlen = ni->salen;
134 rpc_set_port(nlm_addr(host), 0);
135 host->h_srcaddrlen = 0;
136
137 host->h_rpcclnt = NULL;
138 host->h_name = nsm->sm_name;
139 host->h_version = ni->version;
140 host->h_proto = ni->protocol;
141 host->h_reclaiming = 0;
142 host->h_server = ni->server;
143 host->h_noresvport = ni->noresvport;
144 host->h_inuse = 0;
145 init_waitqueue_head(&host->h_gracewait);
146 init_rwsem(&host->h_rwsem);
147 host->h_state = 0;
148 host->h_nsmstate = 0;
149 host->h_pidcount = 0;
150 atomic_set(&host->h_count, 1);
151 mutex_init(&host->h_mutex);
152 host->h_nextrebind = now + NLM_HOST_REBIND;
153 host->h_expires = now + NLM_HOST_EXPIRE;
154 INIT_LIST_HEAD(&host->h_lockowners);
155 spin_lock_init(&host->h_lock);
156 INIT_LIST_HEAD(&host->h_granted);
157 INIT_LIST_HEAD(&host->h_reclaim);
158 host->h_nsmhandle = nsm;
159 host->h_addrbuf = nsm->sm_addrbuf;
160
161out:
162 return host;
163}
164
165/*
Chuck Lever723bb5b2010-12-14 15:05:33 +0000166 * Destroy an nlm_host and free associated resources
167 *
168 * Caller must hold nlm_host_mutex.
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700169 */
Chuck Lever723bb5b2010-12-14 15:05:33 +0000170static void nlm_destroy_host_locked(struct nlm_host *host)
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700171{
172 struct rpc_clnt *clnt;
173
Chuck Lever723bb5b2010-12-14 15:05:33 +0000174 dprintk("lockd: destroy host %s\n", host->h_name);
175
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700176 BUG_ON(!list_empty(&host->h_lockowners));
177 BUG_ON(atomic_read(&host->h_count));
178
Chuck Lever723bb5b2010-12-14 15:05:33 +0000179 hlist_del_init(&host->h_hash);
180
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700181 nsm_unmonitor(host);
Chuck Leverc8c23c42008-12-04 14:21:31 -0500182 nsm_release(host->h_nsmhandle);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700183
Trond Myklebust34f52e32007-06-14 16:40:31 -0400184 clnt = host->h_rpcclnt;
185 if (clnt != NULL)
186 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700187 kfree(host);
Chuck Lever723bb5b2010-12-14 15:05:33 +0000188
189 nrhosts--;
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700190}
191
Chuck Leverd7d20442008-10-03 12:50:21 -0400192/**
193 * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
194 * @sap: network address of server
195 * @salen: length of server address
196 * @protocol: transport protocol to use
197 * @version: NLM protocol version
198 * @hostname: '\0'-terminated hostname of server
Chuck Lever0cb26592008-12-23 15:21:38 -0500199 * @noresvport: 1 if non-privileged port should be used
Chuck Leverd7d20442008-10-03 12:50:21 -0400200 *
201 * Returns an nlm_host structure that matches the passed-in
202 * [server address, transport protocol, NLM version, server hostname].
203 * If one doesn't already exist in the host cache, a new handle is
204 * created and returned.
Adrian Bunkc5856462006-12-06 20:38:29 -0800205 */
Chuck Leverd7d20442008-10-03 12:50:21 -0400206struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
207 const size_t salen,
208 const unsigned short protocol,
Chuck Lever0cb26592008-12-23 15:21:38 -0500209 const u32 version,
210 const char *hostname,
211 int noresvport)
Adrian Bunkc5856462006-12-06 20:38:29 -0800212{
Chuck Lever7f1ed182008-10-03 12:50:07 -0400213 struct nlm_lookup_host_info ni = {
214 .server = 0,
Chuck Leverd7d20442008-10-03 12:50:21 -0400215 .sap = sap,
216 .salen = salen,
217 .protocol = protocol,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400218 .version = version,
219 .hostname = hostname,
Chuck Leverd7d20442008-10-03 12:50:21 -0400220 .hostname_len = strlen(hostname),
Chuck Lever0cb26592008-12-23 15:21:38 -0500221 .noresvport = noresvport,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400222 };
Chuck Lever8ea6ecc2010-12-14 15:05:52 +0000223 struct hlist_head *chain;
224 struct hlist_node *pos;
225 struct nlm_host *host;
226 struct nsm_handle *nsm = NULL;
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200227
Chuck Lever7f1ed182008-10-03 12:50:07 -0400228 dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
229 (hostname ? hostname : "<none>"), version,
Chuck Leverd7d20442008-10-03 12:50:21 -0400230 (protocol == IPPROTO_UDP ? "udp" : "tcp"));
Chuck Lever7f1ed182008-10-03 12:50:07 -0400231
Chuck Lever8ea6ecc2010-12-14 15:05:52 +0000232 mutex_lock(&nlm_host_mutex);
233
234 chain = &nlm_client_hosts[nlm_hash_address(sap)];
235 hlist_for_each_entry(host, pos, chain, h_hash) {
236 if (!rpc_cmp_addr(nlm_addr(host), sap))
237 continue;
238
239 /* Same address. Share an NSM handle if we already have one */
240 if (nsm == NULL)
241 nsm = host->h_nsmhandle;
242
243 if (host->h_proto != protocol)
244 continue;
245 if (host->h_version != version)
246 continue;
247
248 nlm_get_host(host);
249 dprintk("lockd: %s found host %s (%s)\n", __func__,
250 host->h_name, host->h_addrbuf);
251 goto out;
252 }
253
254 host = nlm_alloc_host(&ni, nsm);
255 if (unlikely(host == NULL))
256 goto out;
257
258 hlist_add_head(&host->h_hash, chain);
259 nrhosts++;
260
261 dprintk("lockd: %s created host %s (%s)\n", __func__,
262 host->h_name, host->h_addrbuf);
263
264out:
265 mutex_unlock(&nlm_host_mutex);
266 return host;
267}
268
269/**
270 * nlmclnt_release_host - release client nlm_host
271 * @host: nlm_host to release
272 *
273 */
274void nlmclnt_release_host(struct nlm_host *host)
275{
276 if (host == NULL)
277 return;
278
279 dprintk("lockd: release client host %s\n", host->h_name);
280
281 BUG_ON(atomic_read(&host->h_count) < 0);
282 BUG_ON(host->h_server);
283
284 if (atomic_dec_and_test(&host->h_count)) {
285 BUG_ON(!list_empty(&host->h_lockowners));
286 BUG_ON(!list_empty(&host->h_granted));
287 BUG_ON(!list_empty(&host->h_reclaim));
288
289 mutex_lock(&nlm_host_mutex);
290 nlm_destroy_host_locked(host);
291 mutex_unlock(&nlm_host_mutex);
292 }
Adrian Bunkc5856462006-12-06 20:38:29 -0800293}
294
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400295/**
296 * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
297 * @rqstp: incoming NLM request
298 * @hostname: name of client host
299 * @hostname_len: length of client hostname
300 *
301 * Returns an nlm_host structure that matches the [client address,
302 * transport protocol, NLM version, client hostname] of the passed-in
303 * NLM request. If one doesn't already exist in the host cache, a
304 * new handle is created and returned.
305 *
306 * Before possibly creating a new nlm_host, construct a sockaddr
307 * for a specific source address in case the local system has
308 * multiple network addresses. The family of the address in
309 * rq_daddr is guaranteed to be the same as the family of the
310 * address in rq_addr, so it's safe to use the same family for
311 * the source address.
Adrian Bunkc5856462006-12-06 20:38:29 -0800312 */
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400313struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
314 const char *hostname,
315 const size_t hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800316{
Chuck Lever67216b92010-12-14 15:06:12 +0000317 struct hlist_head *chain;
318 struct hlist_node *pos;
319 struct nlm_host *host = NULL;
320 struct nsm_handle *nsm = NULL;
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400321 struct sockaddr_in sin = {
Chuck Lever2860a022008-08-27 16:57:31 -0400322 .sin_family = AF_INET,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400323 };
324 struct sockaddr_in6 sin6 = {
325 .sin6_family = AF_INET6,
Chuck Lever2860a022008-08-27 16:57:31 -0400326 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400327 struct nlm_lookup_host_info ni = {
328 .server = 1,
Chuck Lever88541c82008-10-03 12:50:14 -0400329 .sap = svc_addr(rqstp),
330 .salen = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400331 .protocol = rqstp->rq_prot,
332 .version = rqstp->rq_vers,
333 .hostname = hostname,
334 .hostname_len = hostname_len,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400335 .src_len = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400336 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200337
Chuck Lever7f1ed182008-10-03 12:50:07 -0400338 dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
339 (int)hostname_len, hostname, rqstp->rq_vers,
340 (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
341
Chuck Lever67216b92010-12-14 15:06:12 +0000342 mutex_lock(&nlm_host_mutex);
343
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400344 switch (ni.sap->sa_family) {
345 case AF_INET:
346 sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr;
347 ni.src_sap = (struct sockaddr *)&sin;
348 break;
349 case AF_INET6:
350 ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6);
351 ni.src_sap = (struct sockaddr *)&sin6;
352 break;
353 default:
Chuck Lever67216b92010-12-14 15:06:12 +0000354 dprintk("lockd: %s failed; unrecognized address family\n",
355 __func__);
356 goto out;
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400357 }
358
Chuck Lever67216b92010-12-14 15:06:12 +0000359 if (time_after_eq(jiffies, next_gc))
360 nlm_gc_hosts();
361
Chuck Leverd2df0482010-12-14 15:06:22 +0000362 chain = &nlm_server_hosts[nlm_hash_address(ni.sap)];
Chuck Lever67216b92010-12-14 15:06:12 +0000363 hlist_for_each_entry(host, pos, chain, h_hash) {
364 if (!rpc_cmp_addr(nlm_addr(host), ni.sap))
365 continue;
366
367 /* Same address. Share an NSM handle if we already have one */
368 if (nsm == NULL)
369 nsm = host->h_nsmhandle;
370
371 if (host->h_proto != ni.protocol)
372 continue;
373 if (host->h_version != ni.version)
374 continue;
375 if (!rpc_cmp_addr(nlm_srcaddr(host), ni.src_sap))
376 continue;
377
378 /* Move to head of hash chain. */
379 hlist_del(&host->h_hash);
380 hlist_add_head(&host->h_hash, chain);
381
382 nlm_get_host(host);
383 dprintk("lockd: %s found host %s (%s)\n",
384 __func__, host->h_name, host->h_addrbuf);
385 goto out;
386 }
387
388 host = nlm_alloc_host(&ni, nsm);
389 if (unlikely(host == NULL))
390 goto out;
391
392 memcpy(nlm_srcaddr(host), ni.src_sap, ni.src_len);
393 host->h_srcaddrlen = ni.src_len;
394 hlist_add_head(&host->h_hash, chain);
395 nrhosts++;
396
397 dprintk("lockd: %s created host %s (%s)\n",
398 __func__, host->h_name, host->h_addrbuf);
399
400out:
401 mutex_unlock(&nlm_host_mutex);
402 return host;
403}
404
405/**
406 * nlmsvc_release_host - release server nlm_host
407 * @host: nlm_host to release
408 *
409 * Host is destroyed later in nlm_gc_host().
410 */
411void nlmsvc_release_host(struct nlm_host *host)
412{
413 if (host == NULL)
414 return;
415
416 dprintk("lockd: release server host %s\n", host->h_name);
417
418 BUG_ON(atomic_read(&host->h_count) < 0);
419 BUG_ON(!host->h_server);
420 atomic_dec(&host->h_count);
Adrian Bunkc5856462006-12-06 20:38:29 -0800421}
422
423/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * Create the NLM RPC client for an NLM peer
425 */
426struct rpc_clnt *
427nlm_bind_host(struct nlm_host *host)
428{
429 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Chuck Lever1df40b62008-12-04 14:19:53 -0500431 dprintk("lockd: nlm_bind_host %s (%s)\n",
432 host->h_name, host->h_addrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400435 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* If we've already created an RPC client, check whether
438 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
440 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700441 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100442 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
Chuck Lever1b333c52008-08-27 16:57:23 -0400444 dprintk("lockd: next rebind in %lu jiffies\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 host->h_nextrebind - jiffies);
446 }
447 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400448 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400449 struct rpc_timeout timeparms = {
450 .to_initval = increment,
451 .to_increment = increment,
452 .to_maxval = increment * 6UL,
453 .to_retries = 5U,
454 };
455 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400456 .net = &init_net,
Chuck Levere1ec7892006-08-22 20:06:20 -0400457 .protocol = host->h_proto,
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400458 .address = nlm_addr(host),
459 .addrsize = host->h_addrlen,
Chuck Levere1ec7892006-08-22 20:06:20 -0400460 .timeout = &timeparms,
461 .servername = host->h_name,
462 .program = &nlm_program,
463 .version = host->h_version,
464 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500465 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400466 RPC_CLNT_CREATE_AUTOBIND),
467 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Jeff Layton90bd17c2008-02-06 11:34:11 -0500469 /*
470 * lockd retries server side blocks automatically so we want
471 * those to be soft RPC calls. Client side calls need to be
472 * hard RPC tasks.
473 */
474 if (!host->h_server)
475 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
Chuck Lever0cb26592008-12-23 15:21:38 -0500476 if (host->h_noresvport)
477 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
Trond Myklebust8e35f8e2010-11-02 09:11:55 -0400478 if (host->h_srcaddrlen)
479 args.saddress = nlm_srcaddr(host);
Jeff Layton90bd17c2008-02-06 11:34:11 -0500480
Chuck Levere1ec7892006-08-22 20:06:20 -0400481 clnt = rpc_create(&args);
482 if (!IS_ERR(clnt))
483 host->h_rpcclnt = clnt;
484 else {
485 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
486 clnt = NULL;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
Trond Myklebust50467912006-06-09 09:40:24 -0400490 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/*
495 * Force a portmap lookup of the remote lockd port
496 */
497void
498nlm_rebind_host(struct nlm_host *host)
499{
500 dprintk("lockd: rebind host %s\n", host->h_name);
501 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100502 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
504 }
505}
506
507/*
508 * Increment NLM host count
509 */
510struct nlm_host * nlm_get_host(struct nlm_host *host)
511{
512 if (host) {
513 dprintk("lockd: get host %s\n", host->h_name);
514 atomic_inc(&host->h_count);
515 host->h_expires = jiffies + NLM_HOST_EXPIRE;
516 }
517 return host;
518}
519
J. Bruce Fieldsb10e30f2010-12-14 15:05:13 +0000520static struct nlm_host *next_host_state(struct hlist_head *cache,
521 struct nsm_handle *nsm,
522 const struct nlm_reboot *info)
523{
524 struct nlm_host *host = NULL;
525 struct hlist_head *chain;
526 struct hlist_node *pos;
527
528 mutex_lock(&nlm_host_mutex);
529 for_each_host(host, pos, chain, cache) {
530 if (host->h_nsmhandle == nsm
531 && host->h_nsmstate != info->state) {
532 host->h_nsmstate = info->state;
533 host->h_state++;
534
535 nlm_get_host(host);
536 mutex_unlock(&nlm_host_mutex);
537 goto out;
538 }
539 }
540out:
541 mutex_unlock(&nlm_host_mutex);
542 return host;
543}
544
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500545/**
546 * nlm_host_rebooted - Release all resources held by rebooted host
547 * @info: pointer to decoded results of NLM_SM_NOTIFY call
548 *
549 * We were notified that the specified host has rebooted. Release
550 * all resources held by that peer.
Olaf Kirchcf712c22006-10-04 02:15:52 -0700551 */
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500552void nlm_host_rebooted(const struct nlm_reboot *info)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700553{
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700554 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700555 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700556
Chuck Lever8c7378f2008-12-05 19:03:54 -0500557 nsm = nsm_reboot_lookup(info);
558 if (unlikely(nsm == NULL))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700559 return;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700560
561 /* Mark all hosts tied to this NSM state as having rebooted.
562 * We run the loop repeatedly, because we drop the host table
563 * lock for this.
564 * To avoid processing a host several times, we match the nsmstate.
565 */
Chuck Leverd2df0482010-12-14 15:06:22 +0000566 while ((host = next_host_state(nlm_server_hosts, nsm, info)) != NULL) {
Chuck Lever8ea6ecc2010-12-14 15:05:52 +0000567 nlmsvc_free_host_resources(host);
Chuck Lever67216b92010-12-14 15:06:12 +0000568 nlmsvc_release_host(host);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700569 }
Chuck Lever8ea6ecc2010-12-14 15:05:52 +0000570 while ((host = next_host_state(nlm_client_hosts, nsm, info)) != NULL) {
571 nlmclnt_recovery(host);
572 nlmclnt_release_host(host);
573 }
574
Jeff Laytoncdd30fa2010-02-05 15:09:12 -0500575 nsm_release(nsm);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700576}
577
578/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * Shut down the hosts module.
580 * Note that this routine is called only at server shutdown time.
581 */
582void
583nlm_shutdown_hosts(void)
584{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700585 struct hlist_head *chain;
586 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800590 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* First, make all hosts eligible for gc */
593 dprintk("lockd: nuking all hosts...\n");
Chuck Leverd2df0482010-12-14 15:06:22 +0000594 for_each_host(host, pos, chain, nlm_server_hosts) {
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000595 host->h_expires = jiffies - 1;
596 if (host->h_rpcclnt) {
597 rpc_shutdown_client(host->h_rpcclnt);
598 host->h_rpcclnt = NULL;
Jeff Laytond801b862008-01-29 10:30:55 -0500599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 /* Then, perform a garbage collection pass */
603 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800604 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* complain if any hosts are left */
Chuck Leverfcc072c2010-12-14 15:06:32 +0000607 if (nrhosts != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
Chuck Leverfcc072c2010-12-14 15:06:32 +0000609 dprintk("lockd: %lu hosts left:\n", nrhosts);
Chuck Leverd2df0482010-12-14 15:06:22 +0000610 for_each_host(host, pos, chain, nlm_server_hosts) {
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000611 dprintk(" %s (cnt %d use %d exp %ld)\n",
612 host->h_name, atomic_read(&host->h_count),
613 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 }
616}
617
618/*
619 * Garbage collect any unused NLM hosts.
620 * This GC combines reference counting for async operations with
621 * mark & sweep for resources held by remote clients.
622 */
623static void
624nlm_gc_hosts(void)
625{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700626 struct hlist_head *chain;
627 struct hlist_node *pos, *next;
628 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 dprintk("lockd: host garbage collection\n");
Chuck Leverd2df0482010-12-14 15:06:22 +0000631 for_each_host(host, pos, chain, nlm_server_hosts)
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000632 host->h_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* Mark all hosts that hold locks, blocks or shares */
635 nlmsvc_mark_resources();
636
Chuck Leverd2df0482010-12-14 15:06:22 +0000637 for_each_host_safe(host, pos, next, chain, nlm_server_hosts) {
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000638 if (atomic_read(&host->h_count) || host->h_inuse
639 || time_before(jiffies, host->h_expires)) {
640 dprintk("nlm_gc_hosts skipping %s "
641 "(cnt %d use %d exp %ld)\n",
642 host->h_name, atomic_read(&host->h_count),
643 host->h_inuse, host->h_expires);
644 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Chuck Lever723bb5b2010-12-14 15:05:33 +0000646 nlm_destroy_host_locked(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
649 next_gc = jiffies + NLM_HOST_COLLECT;
650}