blob: ed103387964d648751833c277f0b00764b767c76 [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>
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
Chuck Lever1b333c52008-08-27 16:57:23 -040021#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define NLMDBG_FACILITY NLMDBG_HOSTCACHE
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define NLM_HOST_NRHASH 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define NLM_HOST_REBIND (60 * HZ)
NeilBrown1447d252008-02-08 13:03:37 +110026#define NLM_HOST_EXPIRE (300 * HZ)
27#define NLM_HOST_COLLECT (120 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static void nlm_gc_hosts(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Chuck Lever7f1ed182008-10-03 12:50:07 -040036struct nlm_lookup_host_info {
37 const int server; /* search for server|client */
Chuck Lever88541c82008-10-03 12:50:14 -040038 const struct sockaddr *sap; /* address to search for */
39 const size_t salen; /* it's length */
Chuck Lever7f1ed182008-10-03 12:50:07 -040040 const unsigned short protocol; /* transport to search for*/
41 const u32 version; /* NLM version to search for */
42 const char *hostname; /* remote's hostname */
43 const size_t hostname_len; /* it's length */
Chuck Lever88541c82008-10-03 12:50:14 -040044 const struct sockaddr *src_sap; /* our address (optional) */
Chuck Lever7f1ed182008-10-03 12:50:07 -040045 const size_t src_len; /* it's length */
Chuck Lever0cb26592008-12-23 15:21:38 -050046 const int noresvport; /* use non-priv port */
Chuck Lever7f1ed182008-10-03 12:50:07 -040047};
48
Chuck Leverede2fea2008-09-03 14:36:08 -040049/*
50 * Hash function must work well on big- and little-endian platforms
51 */
52static unsigned int __nlm_hash32(const __be32 n)
53{
54 unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
55 return hash ^ (hash >> 8);
56}
57
58static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
59{
60 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
61 return __nlm_hash32(sin->sin_addr.s_addr);
62}
63
64static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
65{
66 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
67 const struct in6_addr addr = sin6->sin6_addr;
68 return __nlm_hash32(addr.s6_addr32[0]) ^
69 __nlm_hash32(addr.s6_addr32[1]) ^
70 __nlm_hash32(addr.s6_addr32[2]) ^
71 __nlm_hash32(addr.s6_addr32[3]);
72}
73
74static unsigned int nlm_hash_address(const struct sockaddr *sap)
75{
76 unsigned int hash;
77
78 switch (sap->sa_family) {
79 case AF_INET:
80 hash = __nlm_hash_addr4(sap);
81 break;
82 case AF_INET6:
83 hash = __nlm_hash_addr6(sap);
84 break;
85 default:
86 hash = 0;
87 }
88 return hash & (NLM_HOST_NRHASH - 1);
89}
90
Chuck Lever396cb3d2008-08-27 16:57:38 -040091static void nlm_clear_port(struct sockaddr *sap)
92{
93 switch (sap->sa_family) {
94 case AF_INET:
95 ((struct sockaddr_in *)sap)->sin_port = 0;
96 break;
97 case AF_INET6:
98 ((struct sockaddr_in6 *)sap)->sin6_port = 0;
99 break;
100 }
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Common host lookup routine for server & client
105 */
Chuck Lever7f1ed182008-10-03 12:50:07 -0400106static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700108 struct hlist_head *chain;
109 struct hlist_node *pos;
110 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700111 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800113 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (time_after_eq(jiffies, next_gc))
116 nlm_gc_hosts();
117
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700118 /* We may keep several nlm_host objects for a peer, because each
119 * nlm_host is identified by
120 * (address, protocol, version, server/client)
121 * We could probably simplify this a little by putting all those
122 * different NLM rpc_clients into one single nlm_host object.
123 * This would allow us to have one nlm_host per address.
124 */
Chuck Lever88541c82008-10-03 12:50:14 -0400125 chain = &nlm_hosts[nlm_hash_address(ni->sap)];
Olaf Kirch0cea3272006-10-04 02:15:56 -0700126 hlist_for_each_entry(host, pos, chain, h_hash) {
Chuck Lever88541c82008-10-03 12:50:14 -0400127 if (!nlm_cmp_addr(nlm_addr(host), ni->sap))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700128 continue;
129
130 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -0700131 if (!nsm)
132 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700133
Chuck Lever7f1ed182008-10-03 12:50:07 -0400134 if (host->h_proto != ni->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 continue;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400136 if (host->h_version != ni->version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 continue;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400138 if (host->h_server != ni->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 continue;
Chuck Levera8d82d92008-11-24 12:51:55 -0500140 if (ni->server &&
141 !nlm_cmp_addr(nlm_srcaddr(host), ni->src_sap))
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200142 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Olaf Kirch0cea3272006-10-04 02:15:56 -0700144 /* Move to head of hash chain. */
145 hlist_del(&host->h_hash);
146 hlist_add_head(&host->h_hash, chain);
147
Olaf Kirchf0737a32006-10-04 02:15:54 -0700148 nlm_get_host(host);
Chuck Lever1b333c52008-08-27 16:57:23 -0400149 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
150 host->h_name, host->h_addrbuf);
Olaf Kirchf0737a32006-10-04 02:15:54 -0700151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400153
154 /*
155 * The host wasn't in our hash table. If we don't
156 * have an NSM handle for it yet, create one.
157 */
NeilBrown6b54dae2006-10-04 02:16:15 -0700158 if (nsm)
159 atomic_inc(&nsm->sm_count);
Chuck Leverc2526f42008-08-27 16:57:15 -0400160 else {
161 host = NULL;
Chuck Lever88541c82008-10-03 12:50:14 -0400162 nsm = nsm_find(ni->sap, ni->salen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400163 ni->hostname, ni->hostname_len, 1);
Chuck Lever1b333c52008-08-27 16:57:23 -0400164 if (!nsm) {
165 dprintk("lockd: nlm_lookup_host failed; "
166 "no nsm handle\n");
Chuck Leverc2526f42008-08-27 16:57:15 -0400167 goto out;
Chuck Lever1b333c52008-08-27 16:57:23 -0400168 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700171 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700172 if (!host) {
173 nsm_release(nsm);
Chuck Lever1b333c52008-08-27 16:57:23 -0400174 dprintk("lockd: nlm_lookup_host failed; no memory\n");
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700175 goto out;
176 }
177 host->h_name = nsm->sm_name;
Chuck Lever1df40b62008-12-04 14:19:53 -0500178 host->h_addrbuf = nsm->sm_addrbuf;
Chuck Lever88541c82008-10-03 12:50:14 -0400179 memcpy(nlm_addr(host), ni->sap, ni->salen);
180 host->h_addrlen = ni->salen;
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400181 nlm_clear_port(nlm_addr(host));
Chuck Lever88541c82008-10-03 12:50:14 -0400182 memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
Chuck Lever7f1ed182008-10-03 12:50:07 -0400183 host->h_version = ni->version;
184 host->h_proto = ni->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400186 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
188 host->h_expires = jiffies + NLM_HOST_EXPIRE;
189 atomic_set(&host->h_count, 1);
190 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400191 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 host->h_state = 0; /* pseudo NSM state */
193 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700194 host->h_nsmhandle = nsm;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400195 host->h_server = ni->server;
Chuck Lever0cb26592008-12-23 15:21:38 -0500196 host->h_noresvport = ni->noresvport;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700197 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 INIT_LIST_HEAD(&host->h_lockowners);
199 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500200 INIT_LIST_HEAD(&host->h_granted);
201 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
NeilBrown1447d252008-02-08 13:03:37 +1100203 nrhosts++;
Chuck Lever1b333c52008-08-27 16:57:23 -0400204
Chuck Lever1b333c52008-08-27 16:57:23 -0400205 dprintk("lockd: nlm_lookup_host created host %s\n",
206 host->h_name);
207
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700208out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800209 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return host;
211}
212
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700213/*
214 * Destroy a host
215 */
216static void
217nlm_destroy_host(struct nlm_host *host)
218{
219 struct rpc_clnt *clnt;
220
221 BUG_ON(!list_empty(&host->h_lockowners));
222 BUG_ON(atomic_read(&host->h_count));
223
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700224 nsm_unmonitor(host);
Chuck Leverc8c23c42008-12-04 14:21:31 -0500225 nsm_release(host->h_nsmhandle);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700226
Trond Myklebust34f52e32007-06-14 16:40:31 -0400227 clnt = host->h_rpcclnt;
228 if (clnt != NULL)
229 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700230 kfree(host);
231}
232
Chuck Leverd7d20442008-10-03 12:50:21 -0400233/**
234 * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
235 * @sap: network address of server
236 * @salen: length of server address
237 * @protocol: transport protocol to use
238 * @version: NLM protocol version
239 * @hostname: '\0'-terminated hostname of server
Chuck Lever0cb26592008-12-23 15:21:38 -0500240 * @noresvport: 1 if non-privileged port should be used
Chuck Leverd7d20442008-10-03 12:50:21 -0400241 *
242 * Returns an nlm_host structure that matches the passed-in
243 * [server address, transport protocol, NLM version, server hostname].
244 * If one doesn't already exist in the host cache, a new handle is
245 * created and returned.
Adrian Bunkc5856462006-12-06 20:38:29 -0800246 */
Chuck Leverd7d20442008-10-03 12:50:21 -0400247struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
248 const size_t salen,
249 const unsigned short protocol,
Chuck Lever0cb26592008-12-23 15:21:38 -0500250 const u32 version,
251 const char *hostname,
252 int noresvport)
Adrian Bunkc5856462006-12-06 20:38:29 -0800253{
Chuck Lever88541c82008-10-03 12:50:14 -0400254 const struct sockaddr source = {
255 .sa_family = AF_UNSPEC,
Chuck Lever2860a022008-08-27 16:57:31 -0400256 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400257 struct nlm_lookup_host_info ni = {
258 .server = 0,
Chuck Leverd7d20442008-10-03 12:50:21 -0400259 .sap = sap,
260 .salen = salen,
261 .protocol = protocol,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400262 .version = version,
263 .hostname = hostname,
Chuck Leverd7d20442008-10-03 12:50:21 -0400264 .hostname_len = strlen(hostname),
Chuck Lever88541c82008-10-03 12:50:14 -0400265 .src_sap = &source,
266 .src_len = sizeof(source),
Chuck Lever0cb26592008-12-23 15:21:38 -0500267 .noresvport = noresvport,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400268 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200269
Chuck Lever7f1ed182008-10-03 12:50:07 -0400270 dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
271 (hostname ? hostname : "<none>"), version,
Chuck Leverd7d20442008-10-03 12:50:21 -0400272 (protocol == IPPROTO_UDP ? "udp" : "tcp"));
Chuck Lever7f1ed182008-10-03 12:50:07 -0400273
274 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800275}
276
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400277/**
278 * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
279 * @rqstp: incoming NLM request
280 * @hostname: name of client host
281 * @hostname_len: length of client hostname
282 *
283 * Returns an nlm_host structure that matches the [client address,
284 * transport protocol, NLM version, client hostname] of the passed-in
285 * NLM request. If one doesn't already exist in the host cache, a
286 * new handle is created and returned.
287 *
288 * Before possibly creating a new nlm_host, construct a sockaddr
289 * for a specific source address in case the local system has
290 * multiple network addresses. The family of the address in
291 * rq_daddr is guaranteed to be the same as the family of the
292 * address in rq_addr, so it's safe to use the same family for
293 * the source address.
Adrian Bunkc5856462006-12-06 20:38:29 -0800294 */
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400295struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
296 const char *hostname,
297 const size_t hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800298{
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400299 struct sockaddr_in sin = {
Chuck Lever2860a022008-08-27 16:57:31 -0400300 .sin_family = AF_INET,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400301 };
302 struct sockaddr_in6 sin6 = {
303 .sin6_family = AF_INET6,
Chuck Lever2860a022008-08-27 16:57:31 -0400304 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400305 struct nlm_lookup_host_info ni = {
306 .server = 1,
Chuck Lever88541c82008-10-03 12:50:14 -0400307 .sap = svc_addr(rqstp),
308 .salen = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400309 .protocol = rqstp->rq_prot,
310 .version = rqstp->rq_vers,
311 .hostname = hostname,
312 .hostname_len = hostname_len,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400313 .src_len = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400314 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200315
Chuck Lever7f1ed182008-10-03 12:50:07 -0400316 dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
317 (int)hostname_len, hostname, rqstp->rq_vers,
318 (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
319
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400320 switch (ni.sap->sa_family) {
321 case AF_INET:
322 sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr;
323 ni.src_sap = (struct sockaddr *)&sin;
324 break;
325 case AF_INET6:
326 ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6);
327 ni.src_sap = (struct sockaddr *)&sin6;
328 break;
329 default:
330 return NULL;
331 }
332
Chuck Lever7f1ed182008-10-03 12:50:07 -0400333 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800334}
335
336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Create the NLM RPC client for an NLM peer
338 */
339struct rpc_clnt *
340nlm_bind_host(struct nlm_host *host)
341{
342 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Chuck Lever1df40b62008-12-04 14:19:53 -0500344 dprintk("lockd: nlm_bind_host %s (%s)\n",
345 host->h_name, host->h_addrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400348 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* If we've already created an RPC client, check whether
351 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
353 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700354 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100355 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
Chuck Lever1b333c52008-08-27 16:57:23 -0400357 dprintk("lockd: next rebind in %lu jiffies\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 host->h_nextrebind - jiffies);
359 }
360 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400361 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400362 struct rpc_timeout timeparms = {
363 .to_initval = increment,
364 .to_increment = increment,
365 .to_maxval = increment * 6UL,
366 .to_retries = 5U,
367 };
368 struct rpc_create_args args = {
369 .protocol = host->h_proto,
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400370 .address = nlm_addr(host),
371 .addrsize = host->h_addrlen,
Chuck Lever90151e62008-09-03 14:35:46 -0400372 .saddress = nlm_srcaddr(host),
Chuck Levere1ec7892006-08-22 20:06:20 -0400373 .timeout = &timeparms,
374 .servername = host->h_name,
375 .program = &nlm_program,
376 .version = host->h_version,
377 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500378 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400379 RPC_CLNT_CREATE_AUTOBIND),
380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Jeff Layton90bd17c2008-02-06 11:34:11 -0500382 /*
383 * lockd retries server side blocks automatically so we want
384 * those to be soft RPC calls. Client side calls need to be
385 * hard RPC tasks.
386 */
387 if (!host->h_server)
388 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
Chuck Lever0cb26592008-12-23 15:21:38 -0500389 if (host->h_noresvport)
390 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
Jeff Layton90bd17c2008-02-06 11:34:11 -0500391
Chuck Levere1ec7892006-08-22 20:06:20 -0400392 clnt = rpc_create(&args);
393 if (!IS_ERR(clnt))
394 host->h_rpcclnt = clnt;
395 else {
396 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
397 clnt = NULL;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Trond Myklebust50467912006-06-09 09:40:24 -0400401 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/*
406 * Force a portmap lookup of the remote lockd port
407 */
408void
409nlm_rebind_host(struct nlm_host *host)
410{
411 dprintk("lockd: rebind host %s\n", host->h_name);
412 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100413 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
415 }
416}
417
418/*
419 * Increment NLM host count
420 */
421struct nlm_host * nlm_get_host(struct nlm_host *host)
422{
423 if (host) {
424 dprintk("lockd: get host %s\n", host->h_name);
425 atomic_inc(&host->h_count);
426 host->h_expires = jiffies + NLM_HOST_EXPIRE;
427 }
428 return host;
429}
430
431/*
432 * Release NLM host after use
433 */
434void nlm_release_host(struct nlm_host *host)
435{
436 if (host != NULL) {
437 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500439 if (atomic_dec_and_test(&host->h_count)) {
440 BUG_ON(!list_empty(&host->h_lockowners));
441 BUG_ON(!list_empty(&host->h_granted));
442 BUG_ON(!list_empty(&host->h_reclaim));
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445}
446
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500447/**
448 * nlm_host_rebooted - Release all resources held by rebooted host
449 * @info: pointer to decoded results of NLM_SM_NOTIFY call
450 *
451 * We were notified that the specified host has rebooted. Release
452 * all resources held by that peer.
Olaf Kirchcf712c22006-10-04 02:15:52 -0700453 */
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500454void nlm_host_rebooted(const struct nlm_reboot *info)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700455{
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500456 const struct sockaddr_in sin = {
457 .sin_family = AF_INET,
458 .sin_addr.s_addr = info->addr,
459 };
Olaf Kirch0cea3272006-10-04 02:15:56 -0700460 struct hlist_head *chain;
461 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700462 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700463 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700464
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500465 nsm = nsm_find((struct sockaddr *)&sin, sizeof(sin),
466 info->mon, info->len, 0);
Chuck Lever1b333c52008-08-27 16:57:23 -0400467 if (nsm == NULL) {
468 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500469 info->len, info->mon);
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700470 return;
Chuck Lever1b333c52008-08-27 16:57:23 -0400471 }
472
473 dprintk("lockd: nlm_host_rebooted(%.*s, %s)\n",
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500474 info->len, info->mon, nsm->sm_addrbuf);
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700475
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700476 /* When reclaiming locks on this peer, make sure that
477 * we set up a new notification */
478 nsm->sm_monitored = 0;
479
480 /* Mark all hosts tied to this NSM state as having rebooted.
481 * We run the loop repeatedly, because we drop the host table
482 * lock for this.
483 * To avoid processing a host several times, we match the nsmstate.
484 */
485again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700486 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
487 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700488 if (host->h_nsmhandle == nsm
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500489 && host->h_nsmstate != info->state) {
490 host->h_nsmstate = info->state;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700491 host->h_state++;
492
493 nlm_get_host(host);
494 mutex_unlock(&nlm_host_mutex);
495
496 if (host->h_server) {
497 /* We're server for this guy, just ditch
498 * all the locks he held. */
499 nlmsvc_free_host_resources(host);
500 } else {
501 /* He's the server, initiate lock recovery. */
502 nlmclnt_recovery(host);
503 }
504
505 nlm_release_host(host);
506 goto again;
507 }
508 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700509 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700510
511 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700512}
513
514/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * Shut down the hosts module.
516 * Note that this routine is called only at server shutdown time.
517 */
518void
519nlm_shutdown_hosts(void)
520{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700521 struct hlist_head *chain;
522 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800526 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* First, make all hosts eligible for gc */
529 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700530 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
Jeff Laytond801b862008-01-29 10:30:55 -0500531 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 host->h_expires = jiffies - 1;
Jeff Laytond801b862008-01-29 10:30:55 -0500533 if (host->h_rpcclnt) {
534 rpc_shutdown_client(host->h_rpcclnt);
535 host->h_rpcclnt = NULL;
536 }
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 /* Then, perform a garbage collection pass */
541 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800542 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* complain if any hosts are left */
545 if (nrhosts) {
546 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
547 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700548 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
549 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 dprintk(" %s (cnt %d use %d exp %ld)\n",
551 host->h_name, atomic_read(&host->h_count),
552 host->h_inuse, host->h_expires);
553 }
554 }
555 }
556}
557
558/*
559 * Garbage collect any unused NLM hosts.
560 * This GC combines reference counting for async operations with
561 * mark & sweep for resources held by remote clients.
562 */
563static void
564nlm_gc_hosts(void)
565{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700566 struct hlist_head *chain;
567 struct hlist_node *pos, *next;
568 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700571 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
572 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 host->h_inuse = 0;
574 }
575
576 /* Mark all hosts that hold locks, blocks or shares */
577 nlmsvc_mark_resources();
578
Olaf Kirch0cea3272006-10-04 02:15:56 -0700579 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
580 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (atomic_read(&host->h_count) || host->h_inuse
582 || time_before(jiffies, host->h_expires)) {
583 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
584 host->h_name, atomic_read(&host->h_count),
585 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 continue;
587 }
588 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700589 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700590
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700591 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 nrhosts--;
593 }
594 }
595
596 next_gc = jiffies + NLM_HOST_COLLECT;
597}