blob: cada3a12d557c4d9c979b0626a9364247f6a3b6c [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];
J. Bruce Fieldsb1137462010-12-14 15:05:03 +000029
30#define for_each_host(host, pos, chain, table) \
31 for ((chain) = (table); \
32 (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \
33 hlist_for_each_entry((host), (pos), (chain), h_hash)
34
35#define for_each_host_safe(host, pos, next, chain, table) \
36 for ((chain) = (table); \
37 (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \
38 hlist_for_each_entry_safe((host), (pos), (next), \
39 (chain), h_hash)
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static unsigned long next_gc;
42static int nrhosts;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080043static DEFINE_MUTEX(nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static void nlm_gc_hosts(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Chuck Lever7f1ed182008-10-03 12:50:07 -040047struct nlm_lookup_host_info {
48 const int server; /* search for server|client */
Chuck Lever88541c82008-10-03 12:50:14 -040049 const struct sockaddr *sap; /* address to search for */
50 const size_t salen; /* it's length */
Chuck Lever7f1ed182008-10-03 12:50:07 -040051 const unsigned short protocol; /* transport to search for*/
52 const u32 version; /* NLM version to search for */
53 const char *hostname; /* remote's hostname */
54 const size_t hostname_len; /* it's length */
Chuck Lever88541c82008-10-03 12:50:14 -040055 const struct sockaddr *src_sap; /* our address (optional) */
Chuck Lever7f1ed182008-10-03 12:50:07 -040056 const size_t src_len; /* it's length */
Chuck Lever0cb26592008-12-23 15:21:38 -050057 const int noresvport; /* use non-priv port */
Chuck Lever7f1ed182008-10-03 12:50:07 -040058};
59
Chuck Leverede2fea2008-09-03 14:36:08 -040060/*
61 * Hash function must work well on big- and little-endian platforms
62 */
63static unsigned int __nlm_hash32(const __be32 n)
64{
65 unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
66 return hash ^ (hash >> 8);
67}
68
69static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
70{
71 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
72 return __nlm_hash32(sin->sin_addr.s_addr);
73}
74
75static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
76{
77 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
78 const struct in6_addr addr = sin6->sin6_addr;
79 return __nlm_hash32(addr.s6_addr32[0]) ^
80 __nlm_hash32(addr.s6_addr32[1]) ^
81 __nlm_hash32(addr.s6_addr32[2]) ^
82 __nlm_hash32(addr.s6_addr32[3]);
83}
84
85static unsigned int nlm_hash_address(const struct sockaddr *sap)
86{
87 unsigned int hash;
88
89 switch (sap->sa_family) {
90 case AF_INET:
91 hash = __nlm_hash_addr4(sap);
92 break;
93 case AF_INET6:
94 hash = __nlm_hash_addr6(sap);
95 break;
96 default:
97 hash = 0;
98 }
99 return hash & (NLM_HOST_NRHASH - 1);
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) {
Jeff Layton4516fc02009-08-14 12:57:54 -0400126 if (!rpc_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;
Trond Myklebust8e35f8e2010-11-02 09:11:55 -0400139 if (ni->server && ni->src_len != 0 &&
Jeff Layton4516fc02009-08-14 12:57:54 -0400140 !rpc_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 Leverb97a5672009-08-09 15:09:38 -0400180 rpc_set_port(nlm_addr(host), 0);
Chuck Lever88541c82008-10-03 12:50:14 -0400181 memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
Trond Myklebust8e35f8e2010-11-02 09:11:55 -0400182 host->h_srcaddrlen = 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 Lever7f1ed182008-10-03 12:50:07 -0400254 struct nlm_lookup_host_info ni = {
255 .server = 0,
Chuck Leverd7d20442008-10-03 12:50:21 -0400256 .sap = sap,
257 .salen = salen,
258 .protocol = protocol,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400259 .version = version,
260 .hostname = hostname,
Chuck Leverd7d20442008-10-03 12:50:21 -0400261 .hostname_len = strlen(hostname),
Chuck Lever0cb26592008-12-23 15:21:38 -0500262 .noresvport = noresvport,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400263 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200264
Chuck Lever7f1ed182008-10-03 12:50:07 -0400265 dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
266 (hostname ? hostname : "<none>"), version,
Chuck Leverd7d20442008-10-03 12:50:21 -0400267 (protocol == IPPROTO_UDP ? "udp" : "tcp"));
Chuck Lever7f1ed182008-10-03 12:50:07 -0400268
269 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800270}
271
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400272/**
273 * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
274 * @rqstp: incoming NLM request
275 * @hostname: name of client host
276 * @hostname_len: length of client hostname
277 *
278 * Returns an nlm_host structure that matches the [client address,
279 * transport protocol, NLM version, client hostname] of the passed-in
280 * NLM request. If one doesn't already exist in the host cache, a
281 * new handle is created and returned.
282 *
283 * Before possibly creating a new nlm_host, construct a sockaddr
284 * for a specific source address in case the local system has
285 * multiple network addresses. The family of the address in
286 * rq_daddr is guaranteed to be the same as the family of the
287 * address in rq_addr, so it's safe to use the same family for
288 * the source address.
Adrian Bunkc5856462006-12-06 20:38:29 -0800289 */
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400290struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
291 const char *hostname,
292 const size_t hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800293{
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400294 struct sockaddr_in sin = {
Chuck Lever2860a022008-08-27 16:57:31 -0400295 .sin_family = AF_INET,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400296 };
297 struct sockaddr_in6 sin6 = {
298 .sin6_family = AF_INET6,
Chuck Lever2860a022008-08-27 16:57:31 -0400299 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400300 struct nlm_lookup_host_info ni = {
301 .server = 1,
Chuck Lever88541c82008-10-03 12:50:14 -0400302 .sap = svc_addr(rqstp),
303 .salen = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400304 .protocol = rqstp->rq_prot,
305 .version = rqstp->rq_vers,
306 .hostname = hostname,
307 .hostname_len = hostname_len,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400308 .src_len = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400309 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200310
Chuck Lever7f1ed182008-10-03 12:50:07 -0400311 dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
312 (int)hostname_len, hostname, rqstp->rq_vers,
313 (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
314
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400315 switch (ni.sap->sa_family) {
316 case AF_INET:
317 sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr;
318 ni.src_sap = (struct sockaddr *)&sin;
319 break;
320 case AF_INET6:
321 ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6);
322 ni.src_sap = (struct sockaddr *)&sin6;
323 break;
324 default:
325 return NULL;
326 }
327
Chuck Lever7f1ed182008-10-03 12:50:07 -0400328 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800329}
330
331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * Create the NLM RPC client for an NLM peer
333 */
334struct rpc_clnt *
335nlm_bind_host(struct nlm_host *host)
336{
337 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Chuck Lever1df40b62008-12-04 14:19:53 -0500339 dprintk("lockd: nlm_bind_host %s (%s)\n",
340 host->h_name, host->h_addrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400343 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* If we've already created an RPC client, check whether
346 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
348 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700349 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100350 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
Chuck Lever1b333c52008-08-27 16:57:23 -0400352 dprintk("lockd: next rebind in %lu jiffies\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 host->h_nextrebind - jiffies);
354 }
355 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400356 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400357 struct rpc_timeout timeparms = {
358 .to_initval = increment,
359 .to_increment = increment,
360 .to_maxval = increment * 6UL,
361 .to_retries = 5U,
362 };
363 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400364 .net = &init_net,
Chuck Levere1ec7892006-08-22 20:06:20 -0400365 .protocol = host->h_proto,
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400366 .address = nlm_addr(host),
367 .addrsize = host->h_addrlen,
Chuck Levere1ec7892006-08-22 20:06:20 -0400368 .timeout = &timeparms,
369 .servername = host->h_name,
370 .program = &nlm_program,
371 .version = host->h_version,
372 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500373 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400374 RPC_CLNT_CREATE_AUTOBIND),
375 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jeff Layton90bd17c2008-02-06 11:34:11 -0500377 /*
378 * lockd retries server side blocks automatically so we want
379 * those to be soft RPC calls. Client side calls need to be
380 * hard RPC tasks.
381 */
382 if (!host->h_server)
383 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
Chuck Lever0cb26592008-12-23 15:21:38 -0500384 if (host->h_noresvport)
385 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
Trond Myklebust8e35f8e2010-11-02 09:11:55 -0400386 if (host->h_srcaddrlen)
387 args.saddress = nlm_srcaddr(host);
Jeff Layton90bd17c2008-02-06 11:34:11 -0500388
Chuck Levere1ec7892006-08-22 20:06:20 -0400389 clnt = rpc_create(&args);
390 if (!IS_ERR(clnt))
391 host->h_rpcclnt = clnt;
392 else {
393 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
394 clnt = NULL;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
Trond Myklebust50467912006-06-09 09:40:24 -0400398 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402/*
403 * Force a portmap lookup of the remote lockd port
404 */
405void
406nlm_rebind_host(struct nlm_host *host)
407{
408 dprintk("lockd: rebind host %s\n", host->h_name);
409 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100410 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
412 }
413}
414
415/*
416 * Increment NLM host count
417 */
418struct nlm_host * nlm_get_host(struct nlm_host *host)
419{
420 if (host) {
421 dprintk("lockd: get host %s\n", host->h_name);
422 atomic_inc(&host->h_count);
423 host->h_expires = jiffies + NLM_HOST_EXPIRE;
424 }
425 return host;
426}
427
428/*
429 * Release NLM host after use
430 */
431void nlm_release_host(struct nlm_host *host)
432{
433 if (host != NULL) {
434 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500436 if (atomic_dec_and_test(&host->h_count)) {
437 BUG_ON(!list_empty(&host->h_lockowners));
438 BUG_ON(!list_empty(&host->h_granted));
439 BUG_ON(!list_empty(&host->h_reclaim));
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442}
443
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500444/**
445 * nlm_host_rebooted - Release all resources held by rebooted host
446 * @info: pointer to decoded results of NLM_SM_NOTIFY call
447 *
448 * We were notified that the specified host has rebooted. Release
449 * all resources held by that peer.
Olaf Kirchcf712c22006-10-04 02:15:52 -0700450 */
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500451void nlm_host_rebooted(const struct nlm_reboot *info)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700452{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700453 struct hlist_head *chain;
454 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700455 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700456 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700457
Chuck Lever8c7378f2008-12-05 19:03:54 -0500458 nsm = nsm_reboot_lookup(info);
459 if (unlikely(nsm == NULL))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700460 return;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700461
462 /* Mark all hosts tied to this NSM state as having rebooted.
463 * We run the loop repeatedly, because we drop the host table
464 * lock for this.
465 * To avoid processing a host several times, we match the nsmstate.
466 */
467again: mutex_lock(&nlm_host_mutex);
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000468 for_each_host(host, pos, chain, nlm_hosts) {
469 if (host->h_nsmhandle == nsm
470 && host->h_nsmstate != info->state) {
471 host->h_nsmstate = info->state;
472 host->h_state++;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700473
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000474 nlm_get_host(host);
475 mutex_unlock(&nlm_host_mutex);
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700476
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000477 if (host->h_server) {
478 /* We're server for this guy, just ditch
479 * all the locks he held. */
480 nlmsvc_free_host_resources(host);
481 } else {
482 /* He's the server, initiate lock recovery. */
483 nlmclnt_recovery(host);
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700484 }
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000485
486 nlm_release_host(host);
487 goto again;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700488 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700489 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700490 mutex_unlock(&nlm_host_mutex);
Jeff Laytoncdd30fa2010-02-05 15:09:12 -0500491 nsm_release(nsm);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700492}
493
494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * Shut down the hosts module.
496 * Note that this routine is called only at server shutdown time.
497 */
498void
499nlm_shutdown_hosts(void)
500{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700501 struct hlist_head *chain;
502 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800506 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /* First, make all hosts eligible for gc */
509 dprintk("lockd: nuking all hosts...\n");
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000510 for_each_host(host, pos, chain, nlm_hosts) {
511 host->h_expires = jiffies - 1;
512 if (host->h_rpcclnt) {
513 rpc_shutdown_client(host->h_rpcclnt);
514 host->h_rpcclnt = NULL;
Jeff Laytond801b862008-01-29 10:30:55 -0500515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 /* Then, perform a garbage collection pass */
519 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800520 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* complain if any hosts are left */
523 if (nrhosts) {
524 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
525 dprintk("lockd: %d hosts left:\n", nrhosts);
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000526 for_each_host(host, pos, chain, nlm_hosts) {
527 dprintk(" %s (cnt %d use %d exp %ld)\n",
528 host->h_name, atomic_read(&host->h_count),
529 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 }
532}
533
534/*
535 * Garbage collect any unused NLM hosts.
536 * This GC combines reference counting for async operations with
537 * mark & sweep for resources held by remote clients.
538 */
539static void
540nlm_gc_hosts(void)
541{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700542 struct hlist_head *chain;
543 struct hlist_node *pos, *next;
544 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 dprintk("lockd: host garbage collection\n");
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000547 for_each_host(host, pos, chain, nlm_hosts)
548 host->h_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Mark all hosts that hold locks, blocks or shares */
551 nlmsvc_mark_resources();
552
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000553 for_each_host_safe(host, pos, next, chain, nlm_hosts) {
554 if (atomic_read(&host->h_count) || host->h_inuse
555 || time_before(jiffies, host->h_expires)) {
556 dprintk("nlm_gc_hosts skipping %s "
557 "(cnt %d use %d exp %ld)\n",
558 host->h_name, atomic_read(&host->h_count),
559 host->h_inuse, host->h_expires);
560 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000562 dprintk("lockd: delete host %s\n", host->h_name);
563 hlist_del_init(&host->h_hash);
564
565 nlm_destroy_host(host);
566 nrhosts--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 next_gc = jiffies + NLM_HOST_COLLECT;
570}