blob: 481ce7ee10024a4c7552dad5c94ee4d0316a50e0 [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>
12#include <linux/sched.h>
13#include <linux/slab.h>
14#include <linux/in.h>
15#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
21
22#define NLMDBG_FACILITY NLMDBG_HOSTCACHE
23#define NLM_HOST_MAX 64
24#define NLM_HOST_NRHASH 32
25#define NLM_ADDRHASH(addr) (ntohl(addr) & (NLM_HOST_NRHASH-1))
26#define NLM_HOST_REBIND (60 * HZ)
27#define NLM_HOST_EXPIRE ((nrhosts > NLM_HOST_MAX)? 300 * HZ : 120 * HZ)
28#define NLM_HOST_COLLECT ((nrhosts > NLM_HOST_MAX)? 120 * HZ : 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static struct nlm_host * nlm_hosts[NLM_HOST_NRHASH];
31static unsigned long next_gc;
32static int nrhosts;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080033static DEFINE_MUTEX(nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35
36static void nlm_gc_hosts(void);
37
38/*
39 * Find an NLM server handle in the cache. If there is none, create it.
40 */
41struct nlm_host *
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070042nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
43 const char *hostname, int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070045 return nlm_lookup_host(0, sin, proto, version,
46 hostname, hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49/*
50 * Find an NLM client handle in the cache. If there is none, create it.
51 */
52struct nlm_host *
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070053nlmsvc_lookup_host(struct svc_rqst *rqstp,
54 const char *hostname, int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 return nlm_lookup_host(1, &rqstp->rq_addr,
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070057 rqstp->rq_prot, rqstp->rq_vers,
58 hostname, hostname_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61/*
62 * Common host lookup routine for server & client
63 */
64struct nlm_host *
Olaf Kirchcf712c22006-10-04 02:15:52 -070065nlm_lookup_host(int server, const struct sockaddr_in *sin,
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070066 int proto, int version,
67 const char *hostname,
68 int hostname_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct nlm_host *host, **hp;
71 u32 addr;
72 int hash;
73
Olaf Kirchdb4e4c92006-10-04 02:15:52 -070074 dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
75 NIPQUAD(sin->sin_addr.s_addr), proto, version,
76 server? "server" : "client",
77 hostname_len,
78 hostname? hostname : "<none>");
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
82
83 /* Lock hash table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080084 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 if (time_after_eq(jiffies, next_gc))
87 nlm_gc_hosts();
88
89 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
90 if (host->h_proto != proto)
91 continue;
92 if (host->h_version != version)
93 continue;
94 if (host->h_server != server)
95 continue;
96
97 if (nlm_cmp_addr(&host->h_addr, sin)) {
98 if (hp != nlm_hosts + hash) {
99 *hp = host->h_next;
100 host->h_next = nlm_hosts[hash];
101 nlm_hosts[hash] = host;
102 }
103 nlm_get_host(host);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800104 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return host;
106 }
107 }
108
109 /* Ooops, no host found, create it */
110 dprintk("lockd: creating host entry\n");
111
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700112 host = kzalloc(sizeof(*host), GFP_KERNEL);
113 if (!host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 goto nohost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 addr = sin->sin_addr.s_addr;
117 sprintf(host->h_name, "%u.%u.%u.%u", NIPQUAD(addr));
118
119 host->h_addr = *sin;
120 host->h_addr.sin_port = 0; /* ouch! */
121 host->h_version = version;
122 host->h_proto = proto;
123 host->h_rpcclnt = NULL;
Trond Myklebust50467912006-06-09 09:40:24 -0400124 mutex_init(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
126 host->h_expires = jiffies + NLM_HOST_EXPIRE;
127 atomic_set(&host->h_count, 1);
128 init_waitqueue_head(&host->h_gracewait);
Trond Myklebust28df9552006-06-09 09:40:27 -0400129 init_rwsem(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 host->h_state = 0; /* pseudo NSM state */
131 host->h_nsmstate = 0; /* real NSM state */
132 host->h_server = server;
133 host->h_next = nlm_hosts[hash];
134 nlm_hosts[hash] = host;
135 INIT_LIST_HEAD(&host->h_lockowners);
136 spin_lock_init(&host->h_lock);
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500137 INIT_LIST_HEAD(&host->h_granted);
138 INIT_LIST_HEAD(&host->h_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (++nrhosts > NLM_HOST_MAX)
141 next_gc = 0;
142
143nohost:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800144 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return host;
146}
147
148struct nlm_host *
149nlm_find_client(void)
150{
151 /* find a nlm_host for a client for which h_killed == 0.
152 * and return it
153 */
154 int hash;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800155 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
157 struct nlm_host *host, **hp;
158 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
159 if (host->h_server &&
160 host->h_killed == 0) {
161 nlm_get_host(host);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800162 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return host;
164 }
165 }
166 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800167 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return NULL;
169}
170
171
172/*
173 * Create the NLM RPC client for an NLM peer
174 */
175struct rpc_clnt *
176nlm_bind_host(struct nlm_host *host)
177{
178 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 dprintk("lockd: nlm_bind_host(%08x)\n",
181 (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
182
183 /* Lock host handle */
Trond Myklebust50467912006-06-09 09:40:24 -0400184 mutex_lock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* If we've already created an RPC client, check whether
187 * RPC rebind is required
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189 if ((clnt = host->h_rpcclnt) != NULL) {
Chuck Lever43118c22005-08-25 16:25:49 -0700190 if (time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100191 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
193 dprintk("lockd: next rebind in %ld jiffies\n",
194 host->h_nextrebind - jiffies);
195 }
196 } else {
Chuck Levere1ec7892006-08-22 20:06:20 -0400197 unsigned long increment = nlmsvc_timeout * HZ;
198 struct rpc_timeout timeparms = {
199 .to_initval = increment,
200 .to_increment = increment,
201 .to_maxval = increment * 6UL,
202 .to_retries = 5U,
203 };
204 struct rpc_create_args args = {
205 .protocol = host->h_proto,
206 .address = (struct sockaddr *)&host->h_addr,
207 .addrsize = sizeof(host->h_addr),
208 .timeout = &timeparms,
209 .servername = host->h_name,
210 .program = &nlm_program,
211 .version = host->h_version,
212 .authflavor = RPC_AUTH_UNIX,
213 .flags = (RPC_CLNT_CREATE_HARDRTRY |
214 RPC_CLNT_CREATE_AUTOBIND),
215 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Chuck Levere1ec7892006-08-22 20:06:20 -0400217 clnt = rpc_create(&args);
218 if (!IS_ERR(clnt))
219 host->h_rpcclnt = clnt;
220 else {
221 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
222 clnt = NULL;
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Trond Myklebust50467912006-06-09 09:40:24 -0400226 mutex_unlock(&host->h_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/*
231 * Force a portmap lookup of the remote lockd port
232 */
233void
234nlm_rebind_host(struct nlm_host *host)
235{
236 dprintk("lockd: rebind host %s\n", host->h_name);
237 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
Chuck Lever35f5a422006-01-03 09:55:50 +0100238 rpc_force_rebind(host->h_rpcclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
240 }
241}
242
243/*
244 * Increment NLM host count
245 */
246struct nlm_host * nlm_get_host(struct nlm_host *host)
247{
248 if (host) {
249 dprintk("lockd: get host %s\n", host->h_name);
250 atomic_inc(&host->h_count);
251 host->h_expires = jiffies + NLM_HOST_EXPIRE;
252 }
253 return host;
254}
255
256/*
257 * Release NLM host after use
258 */
259void nlm_release_host(struct nlm_host *host)
260{
261 if (host != NULL) {
262 dprintk("lockd: release host %s\n", host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 BUG_ON(atomic_read(&host->h_count) < 0);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500264 if (atomic_dec_and_test(&host->h_count)) {
265 BUG_ON(!list_empty(&host->h_lockowners));
266 BUG_ON(!list_empty(&host->h_granted));
267 BUG_ON(!list_empty(&host->h_reclaim));
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270}
271
272/*
Olaf Kirchcf712c22006-10-04 02:15:52 -0700273 * We were notified that the host indicated by address &sin
274 * has rebooted.
275 * Release all resources held by that peer.
276 */
277void nlm_host_rebooted(const struct sockaddr_in *sin, const struct nlm_reboot *argp)
278{
279 struct nlm_host *host;
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700280 int server;
Olaf Kirchcf712c22006-10-04 02:15:52 -0700281
282 /* Obtain the host pointer for this NFS server and try to
283 * reclaim all locks we hold on this server.
284 */
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700285 server = (argp->proto & 1)? 1 : 0;
286 host = nlm_lookup_host(server, sin, argp->proto >> 1, argp->vers,
287 argp->mon, argp->len);
288 if (host == NULL)
289 return;
290
291 if (server == 0) {
Olaf Kirchcf712c22006-10-04 02:15:52 -0700292 /* We are client, he's the server: try to reclaim all locks. */
Olaf Kirchcf712c22006-10-04 02:15:52 -0700293 nlmclnt_recovery(host, argp->state);
294 } else {
295 /* He's the client, we're the server: delete all locks held by the client */
Olaf Kirchcf712c22006-10-04 02:15:52 -0700296 nlmsvc_free_host_resources(host);
297 }
298 nlm_release_host(host);
299}
300
301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * Shut down the hosts module.
303 * Note that this routine is called only at server shutdown time.
304 */
305void
306nlm_shutdown_hosts(void)
307{
308 struct nlm_host *host;
309 int i;
310
311 dprintk("lockd: shutting down host module\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800312 mutex_lock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* First, make all hosts eligible for gc */
315 dprintk("lockd: nuking all hosts...\n");
316 for (i = 0; i < NLM_HOST_NRHASH; i++) {
317 for (host = nlm_hosts[i]; host; host = host->h_next)
318 host->h_expires = jiffies - 1;
319 }
320
321 /* Then, perform a garbage collection pass */
322 nlm_gc_hosts();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800323 mutex_unlock(&nlm_host_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* complain if any hosts are left */
326 if (nrhosts) {
327 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
328 dprintk("lockd: %d hosts left:\n", nrhosts);
329 for (i = 0; i < NLM_HOST_NRHASH; i++) {
330 for (host = nlm_hosts[i]; host; host = host->h_next) {
331 dprintk(" %s (cnt %d use %d exp %ld)\n",
332 host->h_name, atomic_read(&host->h_count),
333 host->h_inuse, host->h_expires);
334 }
335 }
336 }
337}
338
339/*
340 * Garbage collect any unused NLM hosts.
341 * This GC combines reference counting for async operations with
342 * mark & sweep for resources held by remote clients.
343 */
344static void
345nlm_gc_hosts(void)
346{
347 struct nlm_host **q, *host;
348 struct rpc_clnt *clnt;
349 int i;
350
351 dprintk("lockd: host garbage collection\n");
352 for (i = 0; i < NLM_HOST_NRHASH; i++) {
353 for (host = nlm_hosts[i]; host; host = host->h_next)
354 host->h_inuse = 0;
355 }
356
357 /* Mark all hosts that hold locks, blocks or shares */
358 nlmsvc_mark_resources();
359
360 for (i = 0; i < NLM_HOST_NRHASH; i++) {
361 q = &nlm_hosts[i];
362 while ((host = *q) != NULL) {
363 if (atomic_read(&host->h_count) || host->h_inuse
364 || time_before(jiffies, host->h_expires)) {
365 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
366 host->h_name, atomic_read(&host->h_count),
367 host->h_inuse, host->h_expires);
368 q = &host->h_next;
369 continue;
370 }
371 dprintk("lockd: delete host %s\n", host->h_name);
372 *q = host->h_next;
Olaf Kirch977faf32006-10-04 02:15:51 -0700373
374 /*
375 * Unmonitor unless host was invalidated (i.e. lockd restarted)
376 */
377 nsm_unmonitor(host);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if ((clnt = host->h_rpcclnt) != NULL) {
380 if (atomic_read(&clnt->cl_users)) {
381 printk(KERN_WARNING
382 "lockd: active RPC handle\n");
383 clnt->cl_dead = 1;
384 } else {
385 rpc_destroy_client(host->h_rpcclnt);
386 }
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 kfree(host);
389 nrhosts--;
390 }
391 }
392
393 next_gc = jiffies + NLM_HOST_COLLECT;
394}
395