blob: bb464d12104c44dd67b0eee1496c49a8df138ac7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * Common host lookup routine for server & client
92 */
Chuck Lever7f1ed182008-10-03 12:50:07 -040093static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Olaf Kirch0cea3272006-10-04 02:15:56 -070095 struct hlist_head *chain;
96 struct hlist_node *pos;
97 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -070098 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800100 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (time_after_eq(jiffies, next_gc))
103 nlm_gc_hosts();
104
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700105 /* We may keep several nlm_host objects for a peer, because each
106 * nlm_host is identified by
107 * (address, protocol, version, server/client)
108 * We could probably simplify this a little by putting all those
109 * different NLM rpc_clients into one single nlm_host object.
110 * This would allow us to have one nlm_host per address.
111 */
Chuck Lever88541c82008-10-03 12:50:14 -0400112 chain = &nlm_hosts[nlm_hash_address(ni->sap)];
Olaf Kirch0cea3272006-10-04 02:15:56 -0700113 hlist_for_each_entry(host, pos, chain, h_hash) {
Jeff Layton4516fc02009-08-14 12:57:54 -0400114 if (!rpc_cmp_addr(nlm_addr(host), ni->sap))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700115 continue;
116
117 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -0700118 if (!nsm)
119 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700120
Chuck Lever7f1ed182008-10-03 12:50:07 -0400121 if (host->h_proto != ni->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 continue;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400123 if (host->h_version != ni->version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 continue;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400125 if (host->h_server != ni->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 continue;
Chuck Levera8d82d92008-11-24 12:51:55 -0500127 if (ni->server &&
Jeff Layton4516fc02009-08-14 12:57:54 -0400128 !rpc_cmp_addr(nlm_srcaddr(host), ni->src_sap))
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200129 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Olaf Kirch0cea3272006-10-04 02:15:56 -0700131 /* Move to head of hash chain. */
132 hlist_del(&host->h_hash);
133 hlist_add_head(&host->h_hash, chain);
134
Olaf Kirchf0737a32006-10-04 02:15:54 -0700135 nlm_get_host(host);
Chuck Lever1b333c52008-08-27 16:57:23 -0400136 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
137 host->h_name, host->h_addrbuf);
Olaf Kirchf0737a32006-10-04 02:15:54 -0700138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400140
141 /*
142 * The host wasn't in our hash table. If we don't
143 * have an NSM handle for it yet, create one.
144 */
NeilBrown6b54dae2006-10-04 02:16:15 -0700145 if (nsm)
146 atomic_inc(&nsm->sm_count);
Chuck Leverc2526f42008-08-27 16:57:15 -0400147 else {
148 host = NULL;
Chuck Lever92fd91b2008-12-05 19:04:01 -0500149 nsm = nsm_get_handle(ni->sap, ni->salen,
150 ni->hostname, ni->hostname_len);
Chuck Lever1b333c52008-08-27 16:57:23 -0400151 if (!nsm) {
152 dprintk("lockd: nlm_lookup_host failed; "
153 "no nsm handle\n");
Chuck Leverc2526f42008-08-27 16:57:15 -0400154 goto out;
Chuck Lever1b333c52008-08-27 16:57:23 -0400155 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700158 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700159 if (!host) {
160 nsm_release(nsm);
Chuck Lever1b333c52008-08-27 16:57:23 -0400161 dprintk("lockd: nlm_lookup_host failed; no memory\n");
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700162 goto out;
163 }
164 host->h_name = nsm->sm_name;
Chuck Lever1df40b62008-12-04 14:19:53 -0500165 host->h_addrbuf = nsm->sm_addrbuf;
Chuck Lever88541c82008-10-03 12:50:14 -0400166 memcpy(nlm_addr(host), ni->sap, ni->salen);
167 host->h_addrlen = ni->salen;
Chuck Leverb97a5672009-08-09 15:09:38 -0400168 rpc_set_port(nlm_addr(host), 0);
Chuck Lever88541c82008-10-03 12:50:14 -0400169 memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
Chuck Lever7f1ed182008-10-03 12:50:07 -0400170 host->h_version = ni->version;
171 host->h_proto = ni->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400173 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
175 host->h_expires = jiffies + NLM_HOST_EXPIRE;
176 atomic_set(&host->h_count, 1);
177 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400178 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 host->h_state = 0; /* pseudo NSM state */
180 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700181 host->h_nsmhandle = nsm;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400182 host->h_server = ni->server;
Chuck Lever0cb26592008-12-23 15:21:38 -0500183 host->h_noresvport = ni->noresvport;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700184 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 INIT_LIST_HEAD(&host->h_lockowners);
186 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500187 INIT_LIST_HEAD(&host->h_granted);
188 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
NeilBrown1447d252008-02-08 13:03:37 +1100190 nrhosts++;
Chuck Lever1b333c52008-08-27 16:57:23 -0400191
Chuck Lever1b333c52008-08-27 16:57:23 -0400192 dprintk("lockd: nlm_lookup_host created host %s\n",
193 host->h_name);
194
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700195out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800196 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return host;
198}
199
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700200/*
201 * Destroy a host
202 */
203static void
204nlm_destroy_host(struct nlm_host *host)
205{
206 struct rpc_clnt *clnt;
207
208 BUG_ON(!list_empty(&host->h_lockowners));
209 BUG_ON(atomic_read(&host->h_count));
210
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700211 nsm_unmonitor(host);
Chuck Leverc8c23c42008-12-04 14:21:31 -0500212 nsm_release(host->h_nsmhandle);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700213
Trond Myklebust34f52e32007-06-14 16:40:31 -0400214 clnt = host->h_rpcclnt;
215 if (clnt != NULL)
216 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700217 kfree(host);
218}
219
Chuck Leverd7d20442008-10-03 12:50:21 -0400220/**
221 * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
222 * @sap: network address of server
223 * @salen: length of server address
224 * @protocol: transport protocol to use
225 * @version: NLM protocol version
226 * @hostname: '\0'-terminated hostname of server
Chuck Lever0cb26592008-12-23 15:21:38 -0500227 * @noresvport: 1 if non-privileged port should be used
Chuck Leverd7d20442008-10-03 12:50:21 -0400228 *
229 * Returns an nlm_host structure that matches the passed-in
230 * [server address, transport protocol, NLM version, server hostname].
231 * If one doesn't already exist in the host cache, a new handle is
232 * created and returned.
Adrian Bunkc5856462006-12-06 20:38:29 -0800233 */
Chuck Leverd7d20442008-10-03 12:50:21 -0400234struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
235 const size_t salen,
236 const unsigned short protocol,
Chuck Lever0cb26592008-12-23 15:21:38 -0500237 const u32 version,
238 const char *hostname,
239 int noresvport)
Adrian Bunkc5856462006-12-06 20:38:29 -0800240{
Chuck Lever88541c82008-10-03 12:50:14 -0400241 const struct sockaddr source = {
242 .sa_family = AF_UNSPEC,
Chuck Lever2860a022008-08-27 16:57:31 -0400243 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400244 struct nlm_lookup_host_info ni = {
245 .server = 0,
Chuck Leverd7d20442008-10-03 12:50:21 -0400246 .sap = sap,
247 .salen = salen,
248 .protocol = protocol,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400249 .version = version,
250 .hostname = hostname,
Chuck Leverd7d20442008-10-03 12:50:21 -0400251 .hostname_len = strlen(hostname),
Chuck Lever88541c82008-10-03 12:50:14 -0400252 .src_sap = &source,
253 .src_len = sizeof(source),
Chuck Lever0cb26592008-12-23 15:21:38 -0500254 .noresvport = noresvport,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400255 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200256
Chuck Lever7f1ed182008-10-03 12:50:07 -0400257 dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
258 (hostname ? hostname : "<none>"), version,
Chuck Leverd7d20442008-10-03 12:50:21 -0400259 (protocol == IPPROTO_UDP ? "udp" : "tcp"));
Chuck Lever7f1ed182008-10-03 12:50:07 -0400260
261 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800262}
263
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400264/**
265 * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
266 * @rqstp: incoming NLM request
267 * @hostname: name of client host
268 * @hostname_len: length of client hostname
269 *
270 * Returns an nlm_host structure that matches the [client address,
271 * transport protocol, NLM version, client hostname] of the passed-in
272 * NLM request. If one doesn't already exist in the host cache, a
273 * new handle is created and returned.
274 *
275 * Before possibly creating a new nlm_host, construct a sockaddr
276 * for a specific source address in case the local system has
277 * multiple network addresses. The family of the address in
278 * rq_daddr is guaranteed to be the same as the family of the
279 * address in rq_addr, so it's safe to use the same family for
280 * the source address.
Adrian Bunkc5856462006-12-06 20:38:29 -0800281 */
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400282struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
283 const char *hostname,
284 const size_t hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800285{
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400286 struct sockaddr_in sin = {
Chuck Lever2860a022008-08-27 16:57:31 -0400287 .sin_family = AF_INET,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400288 };
289 struct sockaddr_in6 sin6 = {
290 .sin6_family = AF_INET6,
Chuck Lever2860a022008-08-27 16:57:31 -0400291 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400292 struct nlm_lookup_host_info ni = {
293 .server = 1,
Chuck Lever88541c82008-10-03 12:50:14 -0400294 .sap = svc_addr(rqstp),
295 .salen = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400296 .protocol = rqstp->rq_prot,
297 .version = rqstp->rq_vers,
298 .hostname = hostname,
299 .hostname_len = hostname_len,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400300 .src_len = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400301 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200302
Chuck Lever7f1ed182008-10-03 12:50:07 -0400303 dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
304 (int)hostname_len, hostname, rqstp->rq_vers,
305 (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
306
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400307 switch (ni.sap->sa_family) {
308 case AF_INET:
309 sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr;
310 ni.src_sap = (struct sockaddr *)&sin;
311 break;
312 case AF_INET6:
313 ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6);
314 ni.src_sap = (struct sockaddr *)&sin6;
315 break;
316 default:
317 return NULL;
318 }
319
Chuck Lever7f1ed182008-10-03 12:50:07 -0400320 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800321}
322
323/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * Create the NLM RPC client for an NLM peer
325 */
326struct rpc_clnt *
327nlm_bind_host(struct nlm_host *host)
328{
329 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Chuck Lever1df40b62008-12-04 14:19:53 -0500331 dprintk("lockd: nlm_bind_host %s (%s)\n",
332 host->h_name, host->h_addrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400335 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* If we've already created an RPC client, check whether
338 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
340 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700341 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100342 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
Chuck Lever1b333c52008-08-27 16:57:23 -0400344 dprintk("lockd: next rebind in %lu jiffies\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 host->h_nextrebind - jiffies);
346 }
347 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400348 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400349 struct rpc_timeout timeparms = {
350 .to_initval = increment,
351 .to_increment = increment,
352 .to_maxval = increment * 6UL,
353 .to_retries = 5U,
354 };
355 struct rpc_create_args args = {
356 .protocol = host->h_proto,
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400357 .address = nlm_addr(host),
358 .addrsize = host->h_addrlen,
Chuck Lever90151e62008-09-03 14:35:46 -0400359 .saddress = nlm_srcaddr(host),
Chuck Levere1ec7892006-08-22 20:06:20 -0400360 .timeout = &timeparms,
361 .servername = host->h_name,
362 .program = &nlm_program,
363 .version = host->h_version,
364 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500365 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400366 RPC_CLNT_CREATE_AUTOBIND),
367 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Jeff Layton90bd17c2008-02-06 11:34:11 -0500369 /*
370 * lockd retries server side blocks automatically so we want
371 * those to be soft RPC calls. Client side calls need to be
372 * hard RPC tasks.
373 */
374 if (!host->h_server)
375 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
Chuck Lever0cb26592008-12-23 15:21:38 -0500376 if (host->h_noresvport)
377 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
Jeff Layton90bd17c2008-02-06 11:34:11 -0500378
Chuck Levere1ec7892006-08-22 20:06:20 -0400379 clnt = rpc_create(&args);
380 if (!IS_ERR(clnt))
381 host->h_rpcclnt = clnt;
382 else {
383 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
384 clnt = NULL;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Trond Myklebust50467912006-06-09 09:40:24 -0400388 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392/*
393 * Force a portmap lookup of the remote lockd port
394 */
395void
396nlm_rebind_host(struct nlm_host *host)
397{
398 dprintk("lockd: rebind host %s\n", host->h_name);
399 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100400 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
402 }
403}
404
405/*
406 * Increment NLM host count
407 */
408struct nlm_host * nlm_get_host(struct nlm_host *host)
409{
410 if (host) {
411 dprintk("lockd: get host %s\n", host->h_name);
412 atomic_inc(&host->h_count);
413 host->h_expires = jiffies + NLM_HOST_EXPIRE;
414 }
415 return host;
416}
417
418/*
419 * Release NLM host after use
420 */
421void nlm_release_host(struct nlm_host *host)
422{
423 if (host != NULL) {
424 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500426 if (atomic_dec_and_test(&host->h_count)) {
427 BUG_ON(!list_empty(&host->h_lockowners));
428 BUG_ON(!list_empty(&host->h_granted));
429 BUG_ON(!list_empty(&host->h_reclaim));
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432}
433
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500434/**
435 * nlm_host_rebooted - Release all resources held by rebooted host
436 * @info: pointer to decoded results of NLM_SM_NOTIFY call
437 *
438 * We were notified that the specified host has rebooted. Release
439 * all resources held by that peer.
Olaf Kirchcf712c22006-10-04 02:15:52 -0700440 */
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500441void nlm_host_rebooted(const struct nlm_reboot *info)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700442{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700443 struct hlist_head *chain;
444 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700445 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700446 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700447
Chuck Lever8c7378f2008-12-05 19:03:54 -0500448 nsm = nsm_reboot_lookup(info);
449 if (unlikely(nsm == NULL))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700450 return;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700451
452 /* Mark all hosts tied to this NSM state as having rebooted.
453 * We run the loop repeatedly, because we drop the host table
454 * lock for this.
455 * To avoid processing a host several times, we match the nsmstate.
456 */
457again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700458 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
459 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700460 if (host->h_nsmhandle == nsm
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500461 && host->h_nsmstate != info->state) {
462 host->h_nsmstate = info->state;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700463 host->h_state++;
464
465 nlm_get_host(host);
466 mutex_unlock(&nlm_host_mutex);
467
468 if (host->h_server) {
469 /* We're server for this guy, just ditch
470 * all the locks he held. */
471 nlmsvc_free_host_resources(host);
472 } else {
473 /* He's the server, initiate lock recovery. */
474 nlmclnt_recovery(host);
475 }
476
477 nlm_release_host(host);
478 goto again;
479 }
480 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700481 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700482 mutex_unlock(&nlm_host_mutex);
Jeff Laytoncdd30fa2010-02-05 15:09:12 -0500483 nsm_release(nsm);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700484}
485
486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * Shut down the hosts module.
488 * Note that this routine is called only at server shutdown time.
489 */
490void
491nlm_shutdown_hosts(void)
492{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700493 struct hlist_head *chain;
494 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800498 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* First, make all hosts eligible for gc */
501 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700502 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
Jeff Laytond801b862008-01-29 10:30:55 -0500503 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 host->h_expires = jiffies - 1;
Jeff Laytond801b862008-01-29 10:30:55 -0500505 if (host->h_rpcclnt) {
506 rpc_shutdown_client(host->h_rpcclnt);
507 host->h_rpcclnt = NULL;
508 }
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
512 /* Then, perform a garbage collection pass */
513 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800514 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* complain if any hosts are left */
517 if (nrhosts) {
518 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
519 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700520 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
521 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 dprintk(" %s (cnt %d use %d exp %ld)\n",
523 host->h_name, atomic_read(&host->h_count),
524 host->h_inuse, host->h_expires);
525 }
526 }
527 }
528}
529
530/*
531 * Garbage collect any unused NLM hosts.
532 * This GC combines reference counting for async operations with
533 * mark & sweep for resources held by remote clients.
534 */
535static void
536nlm_gc_hosts(void)
537{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700538 struct hlist_head *chain;
539 struct hlist_node *pos, *next;
540 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700543 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
544 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 host->h_inuse = 0;
546 }
547
548 /* Mark all hosts that hold locks, blocks or shares */
549 nlmsvc_mark_resources();
550
Olaf Kirch0cea3272006-10-04 02:15:56 -0700551 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
552 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (atomic_read(&host->h_count) || host->h_inuse
554 || time_before(jiffies, host->h_expires)) {
555 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
556 host->h_name, atomic_read(&host->h_count),
557 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 continue;
559 }
560 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700561 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700562
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700563 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 nrhosts--;
565 }
566 }
567
568 next_gc = jiffies + NLM_HOST_COLLECT;
569}