blob: e58e1426d161ae79faa8554c300397e22f8513e0 [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/*
Chuck Levera7952f42010-12-14 15:05:23 +0000103 * Allocate and initialize an nlm_host. Common to both client and server.
104 */
105static struct nlm_host *nlm_alloc_host(struct nlm_lookup_host_info *ni,
106 struct nsm_handle *nsm)
107{
108 struct nlm_host *host = NULL;
109 unsigned long now = jiffies;
110
111 if (nsm != NULL)
112 atomic_inc(&nsm->sm_count);
113 else {
114 host = NULL;
115 nsm = nsm_get_handle(ni->sap, ni->salen,
116 ni->hostname, ni->hostname_len);
117 if (unlikely(nsm == NULL)) {
118 dprintk("lockd: %s failed; no nsm handle\n",
119 __func__);
120 goto out;
121 }
122 }
123
124 host = kmalloc(sizeof(*host), GFP_KERNEL);
125 if (unlikely(host == NULL)) {
126 dprintk("lockd: %s failed; no memory\n", __func__);
127 nsm_release(nsm);
128 goto out;
129 }
130
131 memcpy(nlm_addr(host), ni->sap, ni->salen);
132 host->h_addrlen = ni->salen;
133 rpc_set_port(nlm_addr(host), 0);
134 host->h_srcaddrlen = 0;
135
136 host->h_rpcclnt = NULL;
137 host->h_name = nsm->sm_name;
138 host->h_version = ni->version;
139 host->h_proto = ni->protocol;
140 host->h_reclaiming = 0;
141 host->h_server = ni->server;
142 host->h_noresvport = ni->noresvport;
143 host->h_inuse = 0;
144 init_waitqueue_head(&host->h_gracewait);
145 init_rwsem(&host->h_rwsem);
146 host->h_state = 0;
147 host->h_nsmstate = 0;
148 host->h_pidcount = 0;
149 atomic_set(&host->h_count, 1);
150 mutex_init(&host->h_mutex);
151 host->h_nextrebind = now + NLM_HOST_REBIND;
152 host->h_expires = now + NLM_HOST_EXPIRE;
153 INIT_LIST_HEAD(&host->h_lockowners);
154 spin_lock_init(&host->h_lock);
155 INIT_LIST_HEAD(&host->h_granted);
156 INIT_LIST_HEAD(&host->h_reclaim);
157 host->h_nsmhandle = nsm;
158 host->h_addrbuf = nsm->sm_addrbuf;
159
160out:
161 return host;
162}
163
164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * Common host lookup routine for server & client
166 */
Chuck Lever7f1ed182008-10-03 12:50:07 -0400167static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700169 struct hlist_head *chain;
170 struct hlist_node *pos;
171 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700172 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800174 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (time_after_eq(jiffies, next_gc))
177 nlm_gc_hosts();
178
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700179 /* We may keep several nlm_host objects for a peer, because each
180 * nlm_host is identified by
181 * (address, protocol, version, server/client)
182 * We could probably simplify this a little by putting all those
183 * different NLM rpc_clients into one single nlm_host object.
184 * This would allow us to have one nlm_host per address.
185 */
Chuck Lever88541c82008-10-03 12:50:14 -0400186 chain = &nlm_hosts[nlm_hash_address(ni->sap)];
Olaf Kirch0cea3272006-10-04 02:15:56 -0700187 hlist_for_each_entry(host, pos, chain, h_hash) {
Jeff Layton4516fc02009-08-14 12:57:54 -0400188 if (!rpc_cmp_addr(nlm_addr(host), ni->sap))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700189 continue;
190
191 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -0700192 if (!nsm)
193 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700194
Chuck Lever7f1ed182008-10-03 12:50:07 -0400195 if (host->h_proto != ni->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 continue;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400197 if (host->h_version != ni->version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 continue;
Chuck Lever7f1ed182008-10-03 12:50:07 -0400199 if (host->h_server != ni->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 continue;
Trond Myklebust8e35f8e2010-11-02 09:11:55 -0400201 if (ni->server && ni->src_len != 0 &&
Jeff Layton4516fc02009-08-14 12:57:54 -0400202 !rpc_cmp_addr(nlm_srcaddr(host), ni->src_sap))
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200203 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Olaf Kirch0cea3272006-10-04 02:15:56 -0700205 /* Move to head of hash chain. */
206 hlist_del(&host->h_hash);
207 hlist_add_head(&host->h_hash, chain);
208
Olaf Kirchf0737a32006-10-04 02:15:54 -0700209 nlm_get_host(host);
Chuck Lever1b333c52008-08-27 16:57:23 -0400210 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
211 host->h_name, host->h_addrbuf);
Olaf Kirchf0737a32006-10-04 02:15:54 -0700212 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400214
Chuck Levera7952f42010-12-14 15:05:23 +0000215 host = nlm_alloc_host(ni, nsm);
216 if (unlikely(host == NULL))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700217 goto out;
Chuck Levera7952f42010-12-14 15:05:23 +0000218
Chuck Lever88541c82008-10-03 12:50:14 -0400219 memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
Trond Myklebust8e35f8e2010-11-02 09:11:55 -0400220 host->h_srcaddrlen = ni->src_len;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700221 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
NeilBrown1447d252008-02-08 13:03:37 +1100223 nrhosts++;
Chuck Lever1b333c52008-08-27 16:57:23 -0400224
Chuck Lever1b333c52008-08-27 16:57:23 -0400225 dprintk("lockd: nlm_lookup_host created host %s\n",
226 host->h_name);
227
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700228out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800229 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return host;
231}
232
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700233/*
Chuck Lever723bb5b2010-12-14 15:05:33 +0000234 * Destroy an nlm_host and free associated resources
235 *
236 * Caller must hold nlm_host_mutex.
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700237 */
Chuck Lever723bb5b2010-12-14 15:05:33 +0000238static void nlm_destroy_host_locked(struct nlm_host *host)
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700239{
240 struct rpc_clnt *clnt;
241
Chuck Lever723bb5b2010-12-14 15:05:33 +0000242 dprintk("lockd: destroy host %s\n", host->h_name);
243
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700244 BUG_ON(!list_empty(&host->h_lockowners));
245 BUG_ON(atomic_read(&host->h_count));
246
Chuck Lever723bb5b2010-12-14 15:05:33 +0000247 hlist_del_init(&host->h_hash);
248
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700249 nsm_unmonitor(host);
Chuck Leverc8c23c42008-12-04 14:21:31 -0500250 nsm_release(host->h_nsmhandle);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700251
Trond Myklebust34f52e32007-06-14 16:40:31 -0400252 clnt = host->h_rpcclnt;
253 if (clnt != NULL)
254 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700255 kfree(host);
Chuck Lever723bb5b2010-12-14 15:05:33 +0000256
257 nrhosts--;
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700258}
259
Chuck Leverd7d20442008-10-03 12:50:21 -0400260/**
261 * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
262 * @sap: network address of server
263 * @salen: length of server address
264 * @protocol: transport protocol to use
265 * @version: NLM protocol version
266 * @hostname: '\0'-terminated hostname of server
Chuck Lever0cb26592008-12-23 15:21:38 -0500267 * @noresvport: 1 if non-privileged port should be used
Chuck Leverd7d20442008-10-03 12:50:21 -0400268 *
269 * Returns an nlm_host structure that matches the passed-in
270 * [server address, transport protocol, NLM version, server hostname].
271 * If one doesn't already exist in the host cache, a new handle is
272 * created and returned.
Adrian Bunkc5856462006-12-06 20:38:29 -0800273 */
Chuck Leverd7d20442008-10-03 12:50:21 -0400274struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
275 const size_t salen,
276 const unsigned short protocol,
Chuck Lever0cb26592008-12-23 15:21:38 -0500277 const u32 version,
278 const char *hostname,
279 int noresvport)
Adrian Bunkc5856462006-12-06 20:38:29 -0800280{
Chuck Lever7f1ed182008-10-03 12:50:07 -0400281 struct nlm_lookup_host_info ni = {
282 .server = 0,
Chuck Leverd7d20442008-10-03 12:50:21 -0400283 .sap = sap,
284 .salen = salen,
285 .protocol = protocol,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400286 .version = version,
287 .hostname = hostname,
Chuck Leverd7d20442008-10-03 12:50:21 -0400288 .hostname_len = strlen(hostname),
Chuck Lever0cb26592008-12-23 15:21:38 -0500289 .noresvport = noresvport,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400290 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200291
Chuck Lever7f1ed182008-10-03 12:50:07 -0400292 dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
293 (hostname ? hostname : "<none>"), version,
Chuck Leverd7d20442008-10-03 12:50:21 -0400294 (protocol == IPPROTO_UDP ? "udp" : "tcp"));
Chuck Lever7f1ed182008-10-03 12:50:07 -0400295
296 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800297}
298
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400299/**
300 * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
301 * @rqstp: incoming NLM request
302 * @hostname: name of client host
303 * @hostname_len: length of client hostname
304 *
305 * Returns an nlm_host structure that matches the [client address,
306 * transport protocol, NLM version, client hostname] of the passed-in
307 * NLM request. If one doesn't already exist in the host cache, a
308 * new handle is created and returned.
309 *
310 * Before possibly creating a new nlm_host, construct a sockaddr
311 * for a specific source address in case the local system has
312 * multiple network addresses. The family of the address in
313 * rq_daddr is guaranteed to be the same as the family of the
314 * address in rq_addr, so it's safe to use the same family for
315 * the source address.
Adrian Bunkc5856462006-12-06 20:38:29 -0800316 */
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400317struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
318 const char *hostname,
319 const size_t hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800320{
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400321 struct sockaddr_in sin = {
Chuck Lever2860a022008-08-27 16:57:31 -0400322 .sin_family = AF_INET,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400323 };
324 struct sockaddr_in6 sin6 = {
325 .sin6_family = AF_INET6,
Chuck Lever2860a022008-08-27 16:57:31 -0400326 };
Chuck Lever7f1ed182008-10-03 12:50:07 -0400327 struct nlm_lookup_host_info ni = {
328 .server = 1,
Chuck Lever88541c82008-10-03 12:50:14 -0400329 .sap = svc_addr(rqstp),
330 .salen = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400331 .protocol = rqstp->rq_prot,
332 .version = rqstp->rq_vers,
333 .hostname = hostname,
334 .hostname_len = hostname_len,
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400335 .src_len = rqstp->rq_addrlen,
Chuck Lever7f1ed182008-10-03 12:50:07 -0400336 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200337
Chuck Lever7f1ed182008-10-03 12:50:07 -0400338 dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
339 (int)hostname_len, hostname, rqstp->rq_vers,
340 (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
341
Chuck Lever6bfbe8a2008-10-03 12:50:29 -0400342 switch (ni.sap->sa_family) {
343 case AF_INET:
344 sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr;
345 ni.src_sap = (struct sockaddr *)&sin;
346 break;
347 case AF_INET6:
348 ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6);
349 ni.src_sap = (struct sockaddr *)&sin6;
350 break;
351 default:
352 return NULL;
353 }
354
Chuck Lever7f1ed182008-10-03 12:50:07 -0400355 return nlm_lookup_host(&ni);
Adrian Bunkc5856462006-12-06 20:38:29 -0800356}
357
358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * Create the NLM RPC client for an NLM peer
360 */
361struct rpc_clnt *
362nlm_bind_host(struct nlm_host *host)
363{
364 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Chuck Lever1df40b62008-12-04 14:19:53 -0500366 dprintk("lockd: nlm_bind_host %s (%s)\n",
367 host->h_name, host->h_addrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400370 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* If we've already created an RPC client, check whether
373 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
375 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700376 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100377 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
Chuck Lever1b333c52008-08-27 16:57:23 -0400379 dprintk("lockd: next rebind in %lu jiffies\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 host->h_nextrebind - jiffies);
381 }
382 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400383 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400384 struct rpc_timeout timeparms = {
385 .to_initval = increment,
386 .to_increment = increment,
387 .to_maxval = increment * 6UL,
388 .to_retries = 5U,
389 };
390 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400391 .net = &init_net,
Chuck Levere1ec7892006-08-22 20:06:20 -0400392 .protocol = host->h_proto,
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400393 .address = nlm_addr(host),
394 .addrsize = host->h_addrlen,
Chuck Levere1ec7892006-08-22 20:06:20 -0400395 .timeout = &timeparms,
396 .servername = host->h_name,
397 .program = &nlm_program,
398 .version = host->h_version,
399 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500400 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400401 RPC_CLNT_CREATE_AUTOBIND),
402 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jeff Layton90bd17c2008-02-06 11:34:11 -0500404 /*
405 * lockd retries server side blocks automatically so we want
406 * those to be soft RPC calls. Client side calls need to be
407 * hard RPC tasks.
408 */
409 if (!host->h_server)
410 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
Chuck Lever0cb26592008-12-23 15:21:38 -0500411 if (host->h_noresvport)
412 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
Trond Myklebust8e35f8e2010-11-02 09:11:55 -0400413 if (host->h_srcaddrlen)
414 args.saddress = nlm_srcaddr(host);
Jeff Layton90bd17c2008-02-06 11:34:11 -0500415
Chuck Levere1ec7892006-08-22 20:06:20 -0400416 clnt = rpc_create(&args);
417 if (!IS_ERR(clnt))
418 host->h_rpcclnt = clnt;
419 else {
420 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
421 clnt = NULL;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Trond Myklebust50467912006-06-09 09:40:24 -0400425 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429/*
430 * Force a portmap lookup of the remote lockd port
431 */
432void
433nlm_rebind_host(struct nlm_host *host)
434{
435 dprintk("lockd: rebind host %s\n", host->h_name);
436 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100437 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
439 }
440}
441
442/*
443 * Increment NLM host count
444 */
445struct nlm_host * nlm_get_host(struct nlm_host *host)
446{
447 if (host) {
448 dprintk("lockd: get host %s\n", host->h_name);
449 atomic_inc(&host->h_count);
450 host->h_expires = jiffies + NLM_HOST_EXPIRE;
451 }
452 return host;
453}
454
455/*
456 * Release NLM host after use
457 */
458void nlm_release_host(struct nlm_host *host)
459{
460 if (host != NULL) {
461 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500463 if (atomic_dec_and_test(&host->h_count)) {
464 BUG_ON(!list_empty(&host->h_lockowners));
465 BUG_ON(!list_empty(&host->h_granted));
466 BUG_ON(!list_empty(&host->h_reclaim));
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469}
470
J. Bruce Fieldsb10e30f2010-12-14 15:05:13 +0000471static struct nlm_host *next_host_state(struct hlist_head *cache,
472 struct nsm_handle *nsm,
473 const struct nlm_reboot *info)
474{
475 struct nlm_host *host = NULL;
476 struct hlist_head *chain;
477 struct hlist_node *pos;
478
479 mutex_lock(&nlm_host_mutex);
480 for_each_host(host, pos, chain, cache) {
481 if (host->h_nsmhandle == nsm
482 && host->h_nsmstate != info->state) {
483 host->h_nsmstate = info->state;
484 host->h_state++;
485
486 nlm_get_host(host);
487 mutex_unlock(&nlm_host_mutex);
488 goto out;
489 }
490 }
491out:
492 mutex_unlock(&nlm_host_mutex);
493 return host;
494}
495
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500496/**
497 * nlm_host_rebooted - Release all resources held by rebooted host
498 * @info: pointer to decoded results of NLM_SM_NOTIFY call
499 *
500 * We were notified that the specified host has rebooted. Release
501 * all resources held by that peer.
Olaf Kirchcf712c22006-10-04 02:15:52 -0700502 */
Chuck Lever7fefc9c2008-12-05 19:03:31 -0500503void nlm_host_rebooted(const struct nlm_reboot *info)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700504{
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700505 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700506 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700507
Chuck Lever8c7378f2008-12-05 19:03:54 -0500508 nsm = nsm_reboot_lookup(info);
509 if (unlikely(nsm == NULL))
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700510 return;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700511
512 /* Mark all hosts tied to this NSM state as having rebooted.
513 * We run the loop repeatedly, because we drop the host table
514 * lock for this.
515 * To avoid processing a host several times, we match the nsmstate.
516 */
J. Bruce Fieldsb10e30f2010-12-14 15:05:13 +0000517 while ((host = next_host_state(nlm_hosts, nsm, info)) != NULL) {
518 if (host->h_server) {
519 /* We're server for this guy, just ditch
520 * all the locks he held. */
521 nlmsvc_free_host_resources(host);
522 } else {
523 /* He's the server, initiate lock recovery. */
524 nlmclnt_recovery(host);
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700525 }
J. Bruce Fieldsb10e30f2010-12-14 15:05:13 +0000526 nlm_release_host(host);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700527 }
Jeff Laytoncdd30fa2010-02-05 15:09:12 -0500528 nsm_release(nsm);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700529}
530
531/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * Shut down the hosts module.
533 * Note that this routine is called only at server shutdown time.
534 */
535void
536nlm_shutdown_hosts(void)
537{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700538 struct hlist_head *chain;
539 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800543 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* First, make all hosts eligible for gc */
546 dprintk("lockd: nuking all hosts...\n");
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000547 for_each_host(host, pos, chain, nlm_hosts) {
548 host->h_expires = jiffies - 1;
549 if (host->h_rpcclnt) {
550 rpc_shutdown_client(host->h_rpcclnt);
551 host->h_rpcclnt = NULL;
Jeff Laytond801b862008-01-29 10:30:55 -0500552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
555 /* Then, perform a garbage collection pass */
556 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800557 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* complain if any hosts are left */
560 if (nrhosts) {
561 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
562 dprintk("lockd: %d hosts left:\n", nrhosts);
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000563 for_each_host(host, pos, chain, nlm_hosts) {
564 dprintk(" %s (cnt %d use %d exp %ld)\n",
565 host->h_name, atomic_read(&host->h_count),
566 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 }
569}
570
571/*
572 * Garbage collect any unused NLM hosts.
573 * This GC combines reference counting for async operations with
574 * mark & sweep for resources held by remote clients.
575 */
576static void
577nlm_gc_hosts(void)
578{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700579 struct hlist_head *chain;
580 struct hlist_node *pos, *next;
581 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 dprintk("lockd: host garbage collection\n");
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000584 for_each_host(host, pos, chain, nlm_hosts)
585 host->h_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* Mark all hosts that hold locks, blocks or shares */
588 nlmsvc_mark_resources();
589
J. Bruce Fieldsb1137462010-12-14 15:05:03 +0000590 for_each_host_safe(host, pos, next, chain, nlm_hosts) {
591 if (atomic_read(&host->h_count) || host->h_inuse
592 || time_before(jiffies, host->h_expires)) {
593 dprintk("nlm_gc_hosts skipping %s "
594 "(cnt %d use %d exp %ld)\n",
595 host->h_name, atomic_read(&host->h_count),
596 host->h_inuse, host->h_expires);
597 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Chuck Lever723bb5b2010-12-14 15:05:33 +0000599 nlm_destroy_host_locked(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 next_gc = jiffies + NLM_HOST_COLLECT;
603}