blob: b9eeafe99a6622671eba8fea57f7392bc3a7a094 [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);
Chuck Leverbc48e4d2008-09-03 14:36:16 -040035static struct nsm_handle *nsm_find(const struct sockaddr_in *sin,
36 const char *hostname,
37 const size_t hostname_len,
38 const int create);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Chuck Leverede2fea2008-09-03 14:36:08 -040040/*
41 * Hash function must work well on big- and little-endian platforms
42 */
43static unsigned int __nlm_hash32(const __be32 n)
44{
45 unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
46 return hash ^ (hash >> 8);
47}
48
49static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
50{
51 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
52 return __nlm_hash32(sin->sin_addr.s_addr);
53}
54
55static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
56{
57 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
58 const struct in6_addr addr = sin6->sin6_addr;
59 return __nlm_hash32(addr.s6_addr32[0]) ^
60 __nlm_hash32(addr.s6_addr32[1]) ^
61 __nlm_hash32(addr.s6_addr32[2]) ^
62 __nlm_hash32(addr.s6_addr32[3]);
63}
64
65static unsigned int nlm_hash_address(const struct sockaddr *sap)
66{
67 unsigned int hash;
68
69 switch (sap->sa_family) {
70 case AF_INET:
71 hash = __nlm_hash_addr4(sap);
72 break;
73 case AF_INET6:
74 hash = __nlm_hash_addr6(sap);
75 break;
76 default:
77 hash = 0;
78 }
79 return hash & (NLM_HOST_NRHASH - 1);
80}
81
Chuck Lever396cb3d2008-08-27 16:57:38 -040082static void nlm_clear_port(struct sockaddr *sap)
83{
84 switch (sap->sa_family) {
85 case AF_INET:
86 ((struct sockaddr_in *)sap)->sin_port = 0;
87 break;
88 case AF_INET6:
89 ((struct sockaddr_in6 *)sap)->sin6_port = 0;
90 break;
91 }
92}
93
Chuck Lever1b333c52008-08-27 16:57:23 -040094static void nlm_display_address(const struct sockaddr *sap,
95 char *buf, const size_t len)
96{
97 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
98 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
99
100 switch (sap->sa_family) {
101 case AF_UNSPEC:
102 snprintf(buf, len, "unspecified");
103 break;
104 case AF_INET:
105 snprintf(buf, len, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
106 break;
107 case AF_INET6:
108 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
109 snprintf(buf, len, NIPQUAD_FMT,
110 NIPQUAD(sin6->sin6_addr.s6_addr32[3]));
111 else
112 snprintf(buf, len, NIP6_FMT, NIP6(sin6->sin6_addr));
113 break;
114 default:
115 snprintf(buf, len, "unsupported address family");
116 break;
117 }
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * Common host lookup routine for server & client
122 */
Chuck Levereb188602008-03-14 14:18:37 -0400123static struct nlm_host *nlm_lookup_host(int server,
124 const struct sockaddr_in *sin,
125 int proto, u32 version,
126 const char *hostname,
127 unsigned int hostname_len,
128 const struct sockaddr_in *ssin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700130 struct hlist_head *chain;
131 struct hlist_node *pos;
132 struct nlm_host *host;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700133 struct nsm_handle *nsm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Chuck Lever1b333c52008-08-27 16:57:23 -0400135 dprintk("lockd: nlm_lookup_host(proto=%d, vers=%u,"
136 " my role is %s, hostname=%.*s)\n",
137 proto, version, server ? "server" : "client",
138 hostname_len, hostname ? hostname : "<none>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800140 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (time_after_eq(jiffies, next_gc))
143 nlm_gc_hosts();
144
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700145 /* We may keep several nlm_host objects for a peer, because each
146 * nlm_host is identified by
147 * (address, protocol, version, server/client)
148 * We could probably simplify this a little by putting all those
149 * different NLM rpc_clients into one single nlm_host object.
150 * This would allow us to have one nlm_host per address.
151 */
Chuck Leverede2fea2008-09-03 14:36:08 -0400152 chain = &nlm_hosts[nlm_hash_address((struct sockaddr *)sin)];
Olaf Kirch0cea3272006-10-04 02:15:56 -0700153 hlist_for_each_entry(host, pos, chain, h_hash) {
Chuck Lever781b61a2008-09-03 14:36:01 -0400154 if (!nlm_cmp_addr(nlm_addr(host), (struct sockaddr *)sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700155 continue;
156
157 /* See if we have an NSM handle for this client */
NeilBrown6b54dae2006-10-04 02:16:15 -0700158 if (!nsm)
159 nsm = host->h_nsmhandle;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (host->h_proto != proto)
162 continue;
163 if (host->h_version != version)
164 continue;
165 if (host->h_server != server)
166 continue;
Chuck Lever781b61a2008-09-03 14:36:01 -0400167 if (!nlm_cmp_addr(nlm_srcaddr(host), (struct sockaddr *)ssin))
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200168 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Olaf Kirch0cea3272006-10-04 02:15:56 -0700170 /* Move to head of hash chain. */
171 hlist_del(&host->h_hash);
172 hlist_add_head(&host->h_hash, chain);
173
Olaf Kirchf0737a32006-10-04 02:15:54 -0700174 nlm_get_host(host);
Chuck Lever1b333c52008-08-27 16:57:23 -0400175 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
176 host->h_name, host->h_addrbuf);
Olaf Kirchf0737a32006-10-04 02:15:54 -0700177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400179
180 /*
181 * The host wasn't in our hash table. If we don't
182 * have an NSM handle for it yet, create one.
183 */
NeilBrown6b54dae2006-10-04 02:16:15 -0700184 if (nsm)
185 atomic_inc(&nsm->sm_count);
Chuck Leverc2526f42008-08-27 16:57:15 -0400186 else {
187 host = NULL;
Chuck Leverbc48e4d2008-09-03 14:36:16 -0400188 nsm = nsm_find(sin, hostname, hostname_len, 1);
Chuck Lever1b333c52008-08-27 16:57:23 -0400189 if (!nsm) {
190 dprintk("lockd: nlm_lookup_host failed; "
191 "no nsm handle\n");
Chuck Leverc2526f42008-08-27 16:57:15 -0400192 goto out;
Chuck Lever1b333c52008-08-27 16:57:23 -0400193 }
Chuck Leverc2526f42008-08-27 16:57:15 -0400194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700196 host = kzalloc(sizeof(*host), GFP_KERNEL);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700197 if (!host) {
198 nsm_release(nsm);
Chuck Lever1b333c52008-08-27 16:57:23 -0400199 dprintk("lockd: nlm_lookup_host failed; no memory\n");
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700200 goto out;
201 }
202 host->h_name = nsm->sm_name;
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400203 memcpy(nlm_addr(host), sin, sizeof(*sin));
204 host->h_addrlen = sizeof(*sin);
205 nlm_clear_port(nlm_addr(host));
Chuck Lever90151e62008-09-03 14:35:46 -0400206 memcpy(nlm_srcaddr(host), ssin, sizeof(*ssin));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 host->h_version = version;
208 host->h_proto = proto;
209 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400210 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
212 host->h_expires = jiffies + NLM_HOST_EXPIRE;
213 atomic_set(&host->h_count, 1);
214 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400215 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 host->h_state = 0; /* pseudo NSM state */
217 host->h_nsmstate = 0; /* real NSM state */
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700218 host->h_nsmhandle = nsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 host->h_server = server;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700220 hlist_add_head(&host->h_hash, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 INIT_LIST_HEAD(&host->h_lockowners);
222 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500223 INIT_LIST_HEAD(&host->h_granted);
224 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
NeilBrown1447d252008-02-08 13:03:37 +1100226 nrhosts++;
Chuck Lever1b333c52008-08-27 16:57:23 -0400227
228 nlm_display_address((struct sockaddr *)&host->h_addr,
229 host->h_addrbuf, sizeof(host->h_addrbuf));
Chuck Lever90151e62008-09-03 14:35:46 -0400230 nlm_display_address((struct sockaddr *)&host->h_srcaddr,
231 host->h_srcaddrbuf, sizeof(host->h_srcaddrbuf));
Chuck Lever1b333c52008-08-27 16:57:23 -0400232
233 dprintk("lockd: nlm_lookup_host created host %s\n",
234 host->h_name);
235
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700236out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800237 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return host;
239}
240
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700241/*
242 * Destroy a host
243 */
244static void
245nlm_destroy_host(struct nlm_host *host)
246{
247 struct rpc_clnt *clnt;
248
249 BUG_ON(!list_empty(&host->h_lockowners));
250 BUG_ON(atomic_read(&host->h_count));
251
252 /*
253 * Release NSM handle and unmonitor host.
254 */
255 nsm_unmonitor(host);
256
Trond Myklebust34f52e32007-06-14 16:40:31 -0400257 clnt = host->h_rpcclnt;
258 if (clnt != NULL)
259 rpc_shutdown_client(clnt);
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700260 kfree(host);
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
Adrian Bunkc5856462006-12-06 20:38:29 -0800264 * Find an NLM server handle in the cache. If there is none, create it.
265 */
Chuck Levereb188602008-03-14 14:18:37 -0400266struct nlm_host *nlmclnt_lookup_host(const struct sockaddr_in *sin,
267 int proto, u32 version,
268 const char *hostname,
269 unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800270{
Chuck Lever2860a022008-08-27 16:57:31 -0400271 const struct sockaddr_in source = {
272 .sin_family = AF_UNSPEC,
273 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200274
Adrian Bunkc5856462006-12-06 20:38:29 -0800275 return nlm_lookup_host(0, sin, proto, version,
Chuck Lever2860a022008-08-27 16:57:31 -0400276 hostname, hostname_len, &source);
Adrian Bunkc5856462006-12-06 20:38:29 -0800277}
278
279/*
280 * Find an NLM client handle in the cache. If there is none, create it.
281 */
282struct nlm_host *
283nlmsvc_lookup_host(struct svc_rqst *rqstp,
Chuck Lever48df020a2007-11-01 16:56:53 -0400284 const char *hostname, unsigned int hostname_len)
Adrian Bunkc5856462006-12-06 20:38:29 -0800285{
Chuck Lever2860a022008-08-27 16:57:31 -0400286 const struct sockaddr_in source = {
287 .sin_family = AF_INET,
288 .sin_addr = rqstp->rq_daddr.addr,
289 };
Frank van Maarseveenc98451b2007-07-09 22:25:29 +0200290
Chuck Lever27459f02007-02-12 00:53:34 -0800291 return nlm_lookup_host(1, svc_addr_in(rqstp),
Adrian Bunkc5856462006-12-06 20:38:29 -0800292 rqstp->rq_prot, rqstp->rq_vers,
Chuck Lever2860a022008-08-27 16:57:31 -0400293 hostname, hostname_len, &source);
Adrian Bunkc5856462006-12-06 20:38:29 -0800294}
295
296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Create the NLM RPC client for an NLM peer
298 */
299struct rpc_clnt *
300nlm_bind_host(struct nlm_host *host)
301{
302 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Chuck Lever1b333c52008-08-27 16:57:23 -0400304 dprintk("lockd: nlm_bind_host %s (%s), my addr=%s\n",
Chuck Lever90151e62008-09-03 14:35:46 -0400305 host->h_name, host->h_addrbuf, host->h_srcaddrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400308 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* If we've already created an RPC client, check whether
311 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700314 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100315 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
Chuck Lever1b333c52008-08-27 16:57:23 -0400317 dprintk("lockd: next rebind in %lu jiffies\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 host->h_nextrebind - jiffies);
319 }
320 } else {
Trond Myklebust21051ba2007-05-14 16:50:44 -0400321 unsigned long increment = nlmsvc_timeout;
Chuck Levere1ec7892006-08-22 20:06:20 -0400322 struct rpc_timeout timeparms = {
323 .to_initval = increment,
324 .to_increment = increment,
325 .to_maxval = increment * 6UL,
326 .to_retries = 5U,
327 };
328 struct rpc_create_args args = {
329 .protocol = host->h_proto,
Chuck Leverb4ed58f2008-09-03 14:35:39 -0400330 .address = nlm_addr(host),
331 .addrsize = host->h_addrlen,
Chuck Lever90151e62008-09-03 14:35:46 -0400332 .saddress = nlm_srcaddr(host),
Chuck Levere1ec7892006-08-22 20:06:20 -0400333 .timeout = &timeparms,
334 .servername = host->h_name,
335 .program = &nlm_program,
336 .version = host->h_version,
337 .authflavor = RPC_AUTH_UNIX,
Jeff Layton90bd17c2008-02-06 11:34:11 -0500338 .flags = (RPC_CLNT_CREATE_NOPING |
Chuck Levere1ec7892006-08-22 20:06:20 -0400339 RPC_CLNT_CREATE_AUTOBIND),
340 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jeff Layton90bd17c2008-02-06 11:34:11 -0500342 /*
343 * lockd retries server side blocks automatically so we want
344 * those to be soft RPC calls. Client side calls need to be
345 * hard RPC tasks.
346 */
347 if (!host->h_server)
348 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
349
Chuck Levere1ec7892006-08-22 20:06:20 -0400350 clnt = rpc_create(&args);
351 if (!IS_ERR(clnt))
352 host->h_rpcclnt = clnt;
353 else {
354 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
355 clnt = NULL;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
Trond Myklebust50467912006-06-09 09:40:24 -0400359 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/*
364 * Force a portmap lookup of the remote lockd port
365 */
366void
367nlm_rebind_host(struct nlm_host *host)
368{
369 dprintk("lockd: rebind host %s\n", host->h_name);
370 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100371 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
373 }
374}
375
376/*
377 * Increment NLM host count
378 */
379struct nlm_host * nlm_get_host(struct nlm_host *host)
380{
381 if (host) {
382 dprintk("lockd: get host %s\n", host->h_name);
383 atomic_inc(&host->h_count);
384 host->h_expires = jiffies + NLM_HOST_EXPIRE;
385 }
386 return host;
387}
388
389/*
390 * Release NLM host after use
391 */
392void nlm_release_host(struct nlm_host *host)
393{
394 if (host != NULL) {
395 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500397 if (atomic_dec_and_test(&host->h_count)) {
398 BUG_ON(!list_empty(&host->h_lockowners));
399 BUG_ON(!list_empty(&host->h_granted));
400 BUG_ON(!list_empty(&host->h_reclaim));
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403}
404
405/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700406 * We were notified that the host indicated by address &sin
407 * has rebooted.
408 * Release all resources held by that peer.
409 */
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700410void nlm_host_rebooted(const struct sockaddr_in *sin,
Chuck Lever48df020a2007-11-01 16:56:53 -0400411 const char *hostname,
412 unsigned int hostname_len,
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700413 u32 new_state)
Olaf Kirchcf712c22006-10-04 02:15:52 -0700414{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700415 struct hlist_head *chain;
416 struct hlist_node *pos;
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700417 struct nsm_handle *nsm;
Olaf Kirch0cea3272006-10-04 02:15:56 -0700418 struct nlm_host *host;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700419
Chuck Leverbc48e4d2008-09-03 14:36:16 -0400420 nsm = nsm_find(sin, hostname, hostname_len, 0);
Chuck Lever1b333c52008-08-27 16:57:23 -0400421 if (nsm == NULL) {
422 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
423 hostname_len, hostname);
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700424 return;
Chuck Lever1b333c52008-08-27 16:57:23 -0400425 }
426
427 dprintk("lockd: nlm_host_rebooted(%.*s, %s)\n",
428 hostname_len, hostname, nsm->sm_addrbuf);
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700429
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700430 /* When reclaiming locks on this peer, make sure that
431 * we set up a new notification */
432 nsm->sm_monitored = 0;
433
434 /* Mark all hosts tied to this NSM state as having rebooted.
435 * We run the loop repeatedly, because we drop the host table
436 * lock for this.
437 * To avoid processing a host several times, we match the nsmstate.
438 */
439again: mutex_lock(&nlm_host_mutex);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700440 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
441 hlist_for_each_entry(host, pos, chain, h_hash) {
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700442 if (host->h_nsmhandle == nsm
443 && host->h_nsmstate != new_state) {
444 host->h_nsmstate = new_state;
445 host->h_state++;
446
447 nlm_get_host(host);
448 mutex_unlock(&nlm_host_mutex);
449
450 if (host->h_server) {
451 /* We're server for this guy, just ditch
452 * all the locks he held. */
453 nlmsvc_free_host_resources(host);
454 } else {
455 /* He's the server, initiate lock recovery. */
456 nlmclnt_recovery(host);
457 }
458
459 nlm_release_host(host);
460 goto again;
461 }
462 }
Olaf Kirchcf712c22006-10-04 02:15:52 -0700463 }
Olaf Kirch5c8dd292006-10-04 02:15:55 -0700464
465 mutex_unlock(&nlm_host_mutex);
Olaf Kirchcf712c22006-10-04 02:15:52 -0700466}
467
468/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * Shut down the hosts module.
470 * Note that this routine is called only at server shutdown time.
471 */
472void
473nlm_shutdown_hosts(void)
474{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700475 struct hlist_head *chain;
476 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800480 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* First, make all hosts eligible for gc */
483 dprintk("lockd: nuking all hosts...\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700484 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
Jeff Laytond801b862008-01-29 10:30:55 -0500485 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 host->h_expires = jiffies - 1;
Jeff Laytond801b862008-01-29 10:30:55 -0500487 if (host->h_rpcclnt) {
488 rpc_shutdown_client(host->h_rpcclnt);
489 host->h_rpcclnt = NULL;
490 }
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
494 /* Then, perform a garbage collection pass */
495 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800496 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* complain if any hosts are left */
499 if (nrhosts) {
500 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
501 dprintk("lockd: %d hosts left:\n", nrhosts);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700502 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
503 hlist_for_each_entry(host, pos, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 dprintk(" %s (cnt %d use %d exp %ld)\n",
505 host->h_name, atomic_read(&host->h_count),
506 host->h_inuse, host->h_expires);
507 }
508 }
509 }
510}
511
512/*
513 * Garbage collect any unused NLM hosts.
514 * This GC combines reference counting for async operations with
515 * mark & sweep for resources held by remote clients.
516 */
517static void
518nlm_gc_hosts(void)
519{
Olaf Kirch0cea3272006-10-04 02:15:56 -0700520 struct hlist_head *chain;
521 struct hlist_node *pos, *next;
522 struct nlm_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 dprintk("lockd: host garbage collection\n");
Olaf Kirch0cea3272006-10-04 02:15:56 -0700525 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
526 hlist_for_each_entry(host, pos, chain, h_hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 host->h_inuse = 0;
528 }
529
530 /* Mark all hosts that hold locks, blocks or shares */
531 nlmsvc_mark_resources();
532
Olaf Kirch0cea3272006-10-04 02:15:56 -0700533 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
534 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (atomic_read(&host->h_count) || host->h_inuse
536 || time_before(jiffies, host->h_expires)) {
537 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
538 host->h_name, atomic_read(&host->h_count),
539 host->h_inuse, host->h_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 continue;
541 }
542 dprintk("lockd: delete host %s\n", host->h_name);
Olaf Kirch0cea3272006-10-04 02:15:56 -0700543 hlist_del_init(&host->h_hash);
Olaf Kirch977faf32006-10-04 02:15:51 -0700544
Olaf Kirchc53c1bb2006-10-04 02:16:00 -0700545 nlm_destroy_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 nrhosts--;
547 }
548 }
549
550 next_gc = jiffies + NLM_HOST_COLLECT;
551}
552
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700553
554/*
555 * Manage NSM handles
556 */
557static LIST_HEAD(nsm_handles);
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500558static DEFINE_SPINLOCK(nsm_lock);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700559
Chuck Leverbc48e4d2008-09-03 14:36:16 -0400560static struct nsm_handle *nsm_find(const struct sockaddr_in *sin,
561 const char *hostname,
562 const size_t hostname_len,
563 const int create)
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700564{
565 struct nsm_handle *nsm = NULL;
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500566 struct nsm_handle *pos;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700567
568 if (!sin)
569 return NULL;
570
571 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
572 if (printk_ratelimit()) {
573 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
574 "in NFS lock request\n",
Chuck Leverbc48e4d2008-09-03 14:36:16 -0400575 (int)hostname_len, hostname);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700576 }
577 return NULL;
578 }
579
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500580retry:
581 spin_lock(&nsm_lock);
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500582 list_for_each_entry(pos, &nsm_handles, sm_link) {
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700583
Olaf Kirchabd1f502006-10-04 02:16:01 -0700584 if (hostname && nsm_use_hostnames) {
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500585 if (strlen(pos->sm_name) != hostname_len
586 || memcmp(pos->sm_name, hostname, hostname_len))
Olaf Kirchabd1f502006-10-04 02:16:01 -0700587 continue;
Chuck Lever781b61a2008-09-03 14:36:01 -0400588 } else if (!nlm_cmp_addr(nsm_addr(pos), (struct sockaddr *)sin))
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700589 continue;
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500590 atomic_inc(&pos->sm_count);
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500591 kfree(nsm);
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500592 nsm = pos;
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500593 goto found;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700594 }
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500595 if (nsm) {
596 list_add(&nsm->sm_link, &nsm_handles);
597 goto found;
598 }
599 spin_unlock(&nsm_lock);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700600
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500601 if (!create)
602 return NULL;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700603
604 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500605 if (nsm == NULL)
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500606 return NULL;
607
Chuck Lever7e9d77462008-09-03 14:35:54 -0400608 memcpy(nsm_addr(nsm), sin, sizeof(*sin));
609 nsm->sm_addrlen = sizeof(*sin);
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500610 nsm->sm_name = (char *) (nsm + 1);
611 memcpy(nsm->sm_name, hostname, hostname_len);
612 nsm->sm_name[hostname_len] = '\0';
Chuck Lever1b333c52008-08-27 16:57:23 -0400613 nlm_display_address((struct sockaddr *)&nsm->sm_addr,
614 nsm->sm_addrbuf, sizeof(nsm->sm_addrbuf));
J. Bruce Fieldsa95e56e2008-02-20 15:27:31 -0500615 atomic_set(&nsm->sm_count, 1);
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500616 goto retry;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700617
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500618found:
619 spin_unlock(&nsm_lock);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700620 return nsm;
621}
622
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700623/*
624 * Release an NSM handle
625 */
626void
627nsm_release(struct nsm_handle *nsm)
628{
629 if (!nsm)
630 return;
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500631 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
J. Bruce Fields164f98a2008-02-20 14:02:47 -0500632 list_del(&nsm->sm_link);
J. Bruce Fieldsd8421202008-02-20 15:40:15 -0500633 spin_unlock(&nsm_lock);
J. Bruce Fields164f98a2008-02-20 14:02:47 -0500634 kfree(nsm);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700635 }
636}