blob: 99d737bd4325477420274092d6f12f470cc9d623 [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
Olaf Kirch0cea3272006-10-04 02:15:56 -070028static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static unsigned long next_gc;
30static int nrhosts;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080031static DEFINE_MUTEX(nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static void nlm_gc_hosts(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Chuck Lever7f1ed182008-10-03 12:50:07 -040035struct nlm_lookup_host_info {
36 const int server; /* search for server|client */
Chuck Lever88541c82008-10-03 12:50:14 -040037 const struct sockaddr *sap; /* address to search for */
38 const size_t salen; /* it's length */
Chuck Lever7f1ed182008-10-03 12:50:07 -040039 const unsigned short protocol; /* transport to search for*/
40 const u32 version; /* NLM version to search for */
41 const char *hostname; /* remote's hostname */
42 const size_t hostname_len; /* it's length */
Chuck Lever88541c82008-10-03 12:50:14 -040043 const struct sockaddr *src_sap; /* our address (optional) */
Chuck Lever7f1ed182008-10-03 12:50:07 -040044 const size_t src_len; /* it's length */
Chuck Lever0cb26592008-12-23 15:21:38 -050045 const int noresvport; /* use non-priv port */
Chuck Lever7f1ed182008-10-03 12:50:07 -040046};
47
Chuck Leverede2fea2008-09-03 14:36:08 -040048/*
49 * Hash function must work well on big- and little-endian platforms
50 */
51static unsigned int __nlm_hash32(const __be32 n)
52{
53 unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
54 return hash ^ (hash >> 8);
55}
56
57static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
58{
59 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
60 return __nlm_hash32(sin->sin_addr.s_addr);
61}
62
63static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
64{
65 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
66 const struct in6_addr addr = sin6->sin6_addr;
67 return __nlm_hash32(addr.s6_addr32[0]) ^
68 __nlm_hash32(addr.s6_addr32[1]) ^
69 __nlm_hash32(addr.s6_addr32[2]) ^
70 __nlm_hash32(addr.s6_addr32[3]);
71}
72
73static unsigned int nlm_hash_address(const struct sockaddr *sap)
74{
75 unsigned int hash;
76
77 switch (sap->sa_family) {
78 case AF_INET:
79 hash = __nlm_hash_addr4(sap);
80 break;
81 case AF_INET6:
82 hash = __nlm_hash_addr6(sap);
83 break;
84 default:
85 hash = 0;
86 }
87 return hash & (NLM_HOST_NRHASH - 1);
88}
89
Chuck Lever396cb3d2008-08-27 16:57:38 -040090static void nlm_clear_port(struct sockaddr *sap)
91{
92 switch (sap->sa_family) {
93 case AF_INET:
94 ((struct sockaddr_in *)sap)->sin_port = 0;
95 break;
96 case AF_INET6:
97 ((struct sockaddr_in6 *)sap)->sin6_port = 0;
98 break;
99 }
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * Common host lookup routine for server & client
104 */
Chuck Lever7f1ed182008-10-03 12:50:07 -0400105static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700107 struct hlist_head *chain;
108 struct hlist_node *pos;
109 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700110 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800112 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if (time_after_eq(jiffies, next_gc))
115 nlm_gc_hosts();
116
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700117 /* We may keep several nlm_host objects for a peer, because each
118 * nlm_host is identified by
119 * (address, protocol, version, server/client)
120 * We could probably simplify this a little by putting all those
121 * different NLM rpc_clients into one single nlm_host object.
122 * This would allow us to have one nlm_host per address.
123 */
Chuck Lever88541c82008-10-03 12:50:14 -0400124 chain = &nlm_hosts[nlm_hash_address(ni->sap)];
Olaf Kirch0cea3272006-10-04 02:15:56 -0700125 hlist_for_each_entry(host, pos, chain, h_hash) {
Chuck Lever88541c82008-10-03 12:50:14 -0400126 if (!nlm_cmp_addr(nlm_addr(host), ni->sap))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700127 continue;
128
129 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -0700130 if (!nsm)
131 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700132
Chuck Lever7f1ed182008-10-03 12:50:07 -0400133 if (host->h_proto != ni->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 continue;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400135 if (host->h_version != ni->version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 continue;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400137 if (host->h_server != ni->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 continue;
Chuck Levera8d82d92008-11-24 12:51:55 -0500139 if (ni->server &&
140 !nlm_cmp_addr(nlm_srcaddr(host), ni->src_sap))
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200141 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Olaf Kirch0cea3272006-10-04 02:15:56 -0700143 /* Move to head of hash chain. */
144 hlist_del(&host->h_hash);
145 hlist_add_head(&host->h_hash, chain);
146
Olaf Kirchf0737a32006-10-04 02:15:54 -0700147 nlm_get_host(host);
Chuck Lever1b333c52008-08-27 16:57:23 -0400148 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
149 host->h_name, host->h_addrbuf);
Olaf Kirchf0737a32006-10-04 02:15:54 -0700150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400152
153 /*
154 * The host wasn't in our hash table. If we don't
155 * have an NSM handle for it yet, create one.
156 */
NeilBrown6b54dae2006-10-04 02:16:15 -0700157 if (nsm)
158 atomic_inc(&nsm->sm_count);
Chuck Leverc2526f42008-08-27 16:57:15 -0400159 else {
160 host = NULL;
Chuck Lever92fd91b2008-12-05 19:04:01 -0500161 nsm = nsm_get_handle(ni->sap, ni->salen,
162 ni->hostname, ni->hostname_len);
Chuck Lever1b333c52008-08-27 16:57:23 -0400163 if (!nsm) {
164 dprintk("lockd: nlm_lookup_host failed; "
165 "no nsm handle\n");
Chuck Leverc2526f42008-08-27 16:57:15 -0400166 goto out;
Chuck Lever1b333c52008-08-27 16:57:23 -0400167 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700170 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700171 if (!host) {
172 nsm_release(nsm);
Chuck Lever1b333c52008-08-27 16:57:23 -0400173 dprintk("lockd: nlm_lookup_host failed; no memory\n");
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700174 goto out;
175 }
176 host->h_name = nsm->sm_name;
Chuck Lever1df40b62008-12-04 14:19:53 -0500177 host->h_addrbuf = nsm->sm_addrbuf;
Chuck Lever88541c82008-10-03 12:50:14 -0400178 memcpy(nlm_addr(host), ni->sap, ni->salen);
179 host->h_addrlen = ni->salen;
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400180 nlm_clear_port(nlm_addr(host));
Chuck Lever88541c82008-10-03 12:50:14 -0400181 memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
Chuck Lever7f1ed182008-10-03 12:50:07 -0400182 host->h_version = ni->version;
183 host->h_proto = ni->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400185 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
187 host->h_expires = jiffies + NLM_HOST_EXPIRE;
188 atomic_set(&host->h_count, 1);
189 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400190 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 host->h_state = 0; /* pseudo NSM state */
192 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700193 host->h_nsmhandle = nsm;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400194 host->h_server = ni->server;
Chuck Lever0cb26592008-12-23 15:21:38 -0500195 host->h_noresvport = ni->noresvport;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700196 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 INIT_LIST_HEAD(&host->h_lockowners);
198 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500199 INIT_LIST_HEAD(&host->h_granted);
200 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
NeilBrown1447d252008-02-08 13:03:37 +1100202 nrhosts++;
Chuck Lever1b333c52008-08-27 16:57:23 -0400203
Chuck Lever1b333c52008-08-27 16:57:23 -0400204 dprintk("lockd: nlm_lookup_host created host %s\n",
205 host->h_name);
206
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700207out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800208 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return host;
210}
211
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700212/*
213 * Destroy a host
214 */
215static void
216nlm_destroy_host(struct nlm_host *host)
217{
218 struct rpc_clnt *clnt;
219
220 BUG_ON(!list_empty(&host->h_lockowners));
221 BUG_ON(atomic_read(&host->h_count));
222
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700223 nsm_unmonitor(host);
Chuck Leverc8c23c42008-12-04 14:21:31 -0500224 nsm_release(host->h_nsmhandle);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700225
Trond Myklebust34f52e32007-06-14 16:40:31 -0400226 clnt = host->h_rpcclnt;
227 if (clnt != NULL)
228 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700229 kfree(host);
230}
231
Chuck Leverd7d20442008-10-03 12:50:21 -0400232/**
233 * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
234 * @sap: network address of server
235 * @salen: length of server address
236 * @protocol: transport protocol to use
237 * @version: NLM protocol version
238 * @hostname: '\0'-terminated hostname of server
Chuck Lever0cb26592008-12-23 15:21:38 -0500239 * @noresvport: 1 if non-privileged port should be used
Chuck Leverd7d20442008-10-03 12:50:21 -0400240 *
241 * Returns an nlm_host structure that matches the passed-in
242 * [server address, transport protocol, NLM version, server hostname].
243 * If one doesn't already exist in the host cache, a new handle is
244 * created and returned.
Adrian Bunkc5856462006-12-06 20:38:29 -0800245 */
Chuck Leverd7d20442008-10-03 12:50:21 -0400246struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
247 const size_t salen,
248 const unsigned short protocol,
Chuck Lever0cb26592008-12-23 15:21:38 -0500249 const u32 version,
250 const char *hostname,
251 int noresvport)
Adrian Bunkc5856462006-12-06 20:38:29 -0800252{
Chuck Lever88541c82008-10-03 12:50:14 -0400253 const struct sockaddr source = {
254 .sa_family = AF_UNSPEC,
Chuck Lever2860a022008-08-27 16:57:31 -0400255 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400256 struct nlm_lookup_host_info ni = {
257 .server = 0,
Chuck Leverd7d20442008-10-03 12:50:21 -0400258 .sap = sap,
259 .salen = salen,
260 .protocol = protocol,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400261 .version = version,
262 .hostname = hostname,
Chuck Leverd7d20442008-10-03 12:50:21 -0400263 .hostname_len = strlen(hostname),
Chuck Lever88541c82008-10-03 12:50:14 -0400264 .src_sap = &source,
265 .src_len = sizeof(source),
Chuck Lever0cb26592008-12-23 15:21:38 -0500266 .noresvport = noresvport,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400267 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200268
Chuck Lever7f1ed182008-10-03 12:50:07 -0400269 dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
270 (hostname ? hostname : "<none>"), version,
Chuck Leverd7d20442008-10-03 12:50:21 -0400271 (protocol == IPPROTO_UDP ? "udp" : "tcp"));
Chuck Lever7f1ed182008-10-03 12:50:07 -0400272
273 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800274}
275
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400276/**
277 * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
278 * @rqstp: incoming NLM request
279 * @hostname: name of client host
280 * @hostname_len: length of client hostname
281 *
282 * Returns an nlm_host structure that matches the [client address,
283 * transport protocol, NLM version, client hostname] of the passed-in
284 * NLM request. If one doesn't already exist in the host cache, a
285 * new handle is created and returned.
286 *
287 * Before possibly creating a new nlm_host, construct a sockaddr
288 * for a specific source address in case the local system has
289 * multiple network addresses. The family of the address in
290 * rq_daddr is guaranteed to be the same as the family of the
291 * address in rq_addr, so it's safe to use the same family for
292 * the source address.
Adrian Bunkc5856462006-12-06 20:38:29 -0800293 */
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400294struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
295 const char *hostname,
296 const size_t hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800297{
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400298 struct sockaddr_in sin = {
Chuck Lever2860a022008-08-27 16:57:31 -0400299 .sin_family = AF_INET,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400300 };
301 struct sockaddr_in6 sin6 = {
302 .sin6_family = AF_INET6,
Chuck Lever2860a022008-08-27 16:57:31 -0400303 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400304 struct nlm_lookup_host_info ni = {
305 .server = 1,
Chuck Lever88541c82008-10-03 12:50:14 -0400306 .sap = svc_addr(rqstp),
307 .salen = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400308 .protocol = rqstp->rq_prot,
309 .version = rqstp->rq_vers,
310 .hostname = hostname,
311 .hostname_len = hostname_len,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400312 .src_len = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400313 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200314
Chuck Lever7f1ed182008-10-03 12:50:07 -0400315 dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
316 (int)hostname_len, hostname, rqstp->rq_vers,
317 (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
318
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400319 switch (ni.sap->sa_family) {
320 case AF_INET:
321 sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr;
322 ni.src_sap = (struct sockaddr *)&sin;
323 break;
324 case AF_INET6:
325 ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6);
326 ni.src_sap = (struct sockaddr *)&sin6;
327 break;
328 default:
329 return NULL;
330 }
331
Chuck Lever7f1ed182008-10-03 12:50:07 -0400332 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800333}
334
335/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * Create the NLM RPC client for an NLM peer
337 */
338struct rpc_clnt *
339nlm_bind_host(struct nlm_host *host)
340{
341 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Chuck Lever1df40b62008-12-04 14:19:53 -0500343 dprintk("lockd: nlm_bind_host %s (%s)\n",
344 host->h_name, host->h_addrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400347 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* If we've already created an RPC client, check whether
350 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
352 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700353 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100354 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
Chuck Lever1b333c52008-08-27 16:57:23 -0400356 dprintk("lockd: next rebind in %lu jiffies\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 host->h_nextrebind - jiffies);
358 }
359 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400360 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400361 struct rpc_timeout timeparms = {
362 .to_initval = increment,
363 .to_increment = increment,
364 .to_maxval = increment * 6UL,
365 .to_retries = 5U,
366 };
367 struct rpc_create_args args = {
368 .protocol = host->h_proto,
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400369 .address = nlm_addr(host),
370 .addrsize = host->h_addrlen,
Chuck Lever90151e62008-09-03 14:35:46 -0400371 .saddress = nlm_srcaddr(host),
Chuck Levere1ec7892006-08-22 20:06:20 -0400372 .timeout = &timeparms,
373 .servername = host->h_name,
374 .program = &nlm_program,
375 .version = host->h_version,
376 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500377 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400378 RPC_CLNT_CREATE_AUTOBIND),
379 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jeff Layton90bd17c2008-02-06 11:34:11 -0500381 /*
382 * lockd retries server side blocks automatically so we want
383 * those to be soft RPC calls. Client side calls need to be
384 * hard RPC tasks.
385 */
386 if (!host->h_server)
387 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
Chuck Lever0cb26592008-12-23 15:21:38 -0500388 if (host->h_noresvport)
389 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
Jeff Layton90bd17c2008-02-06 11:34:11 -0500390
Chuck Levere1ec7892006-08-22 20:06:20 -0400391 clnt = rpc_create(&args);
392 if (!IS_ERR(clnt))
393 host->h_rpcclnt = clnt;
394 else {
395 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
396 clnt = NULL;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Trond Myklebust50467912006-06-09 09:40:24 -0400400 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/*
405 * Force a portmap lookup of the remote lockd port
406 */
407void
408nlm_rebind_host(struct nlm_host *host)
409{
410 dprintk("lockd: rebind host %s\n", host->h_name);
411 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100412 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
414 }
415}
416
417/*
418 * Increment NLM host count
419 */
420struct nlm_host * nlm_get_host(struct nlm_host *host)
421{
422 if (host) {
423 dprintk("lockd: get host %s\n", host->h_name);
424 atomic_inc(&host->h_count);
425 host->h_expires = jiffies + NLM_HOST_EXPIRE;
426 }
427 return host;
428}
429
430/*
431 * Release NLM host after use
432 */
433void nlm_release_host(struct nlm_host *host)
434{
435 if (host != NULL) {
436 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500438 if (atomic_dec_and_test(&host->h_count)) {
439 BUG_ON(!list_empty(&host->h_lockowners));
440 BUG_ON(!list_empty(&host->h_granted));
441 BUG_ON(!list_empty(&host->h_reclaim));
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444}
445
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500446/**
447 * nlm_host_rebooted - Release all resources held by rebooted host
448 * @info: pointer to decoded results of NLM_SM_NOTIFY call
449 *
450 * We were notified that the specified host has rebooted. Release
451 * all resources held by that peer.
Olaf Kirchcf712c22006-10-04 02:15:52 -0700452 */
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500453void nlm_host_rebooted(const struct nlm_reboot *info)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700454{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700455 struct hlist_head *chain;
456 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700457 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700458 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700459
Chuck Lever8c7378f2008-12-05 19:03:54 -0500460 nsm = nsm_reboot_lookup(info);
461 if (unlikely(nsm == NULL))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700462 return;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700463
464 /* Mark all hosts tied to this NSM state as having rebooted.
465 * We run the loop repeatedly, because we drop the host table
466 * lock for this.
467 * To avoid processing a host several times, we match the nsmstate.
468 */
469again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700470 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
471 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700472 if (host->h_nsmhandle == nsm
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500473 && host->h_nsmstate != info->state) {
474 host->h_nsmstate = info->state;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700475 host->h_state++;
476
477 nlm_get_host(host);
478 mutex_unlock(&nlm_host_mutex);
479
480 if (host->h_server) {
481 /* We're server for this guy, just ditch
482 * all the locks he held. */
483 nlmsvc_free_host_resources(host);
484 } else {
485 /* He's the server, initiate lock recovery. */
486 nlmclnt_recovery(host);
487 }
488
489 nlm_release_host(host);
490 goto again;
491 }
492 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700493 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700494
495 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700496}
497
498/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * Shut down the hosts module.
500 * Note that this routine is called only at server shutdown time.
501 */
502void
503nlm_shutdown_hosts(void)
504{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700505 struct hlist_head *chain;
506 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800510 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /* First, make all hosts eligible for gc */
513 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700514 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
Jeff Laytond801b862008-01-29 10:30:55 -0500515 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 host->h_expires = jiffies - 1;
Jeff Laytond801b862008-01-29 10:30:55 -0500517 if (host->h_rpcclnt) {
518 rpc_shutdown_client(host->h_rpcclnt);
519 host->h_rpcclnt = NULL;
520 }
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
524 /* Then, perform a garbage collection pass */
525 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800526 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* complain if any hosts are left */
529 if (nrhosts) {
530 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
531 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700532 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
533 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 dprintk(" %s (cnt %d use %d exp %ld)\n",
535 host->h_name, atomic_read(&host->h_count),
536 host->h_inuse, host->h_expires);
537 }
538 }
539 }
540}
541
542/*
543 * Garbage collect any unused NLM hosts.
544 * This GC combines reference counting for async operations with
545 * mark & sweep for resources held by remote clients.
546 */
547static void
548nlm_gc_hosts(void)
549{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700550 struct hlist_head *chain;
551 struct hlist_node *pos, *next;
552 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700555 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
556 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 host->h_inuse = 0;
558 }
559
560 /* Mark all hosts that hold locks, blocks or shares */
561 nlmsvc_mark_resources();
562
Olaf Kirch0cea3272006-10-04 02:15:56 -0700563 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
564 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (atomic_read(&host->h_count) || host->h_inuse
566 || time_before(jiffies, host->h_expires)) {
567 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
568 host->h_name, atomic_read(&host->h_count),
569 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 continue;
571 }
572 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700573 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700574
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700575 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 nrhosts--;
577 }
578 }
579
580 next_gc = jiffies + NLM_HOST_COLLECT;
581}