]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/lockd/host.c
NLM: set RPC_CLNT_CREATE_NOPING for NLM RPC clients
[mirror_ubuntu-artful-kernel.git] / fs / lockd / host.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
12#include <linux/slab.h>
13#include <linux/in.h>
14#include <linux/sunrpc/clnt.h>
15#include <linux/sunrpc/svc.h>
16#include <linux/lockd/lockd.h>
17#include <linux/lockd/sm_inter.h>
353ab6e9 18#include <linux/mutex.h>
1da177e4
LT
19
20
21#define NLMDBG_FACILITY NLMDBG_HOSTCACHE
22#define NLM_HOST_MAX 64
23#define NLM_HOST_NRHASH 32
24#define NLM_ADDRHASH(addr) (ntohl(addr) & (NLM_HOST_NRHASH-1))
25#define NLM_HOST_REBIND (60 * HZ)
26#define NLM_HOST_EXPIRE ((nrhosts > NLM_HOST_MAX)? 300 * HZ : 120 * HZ)
27#define NLM_HOST_COLLECT ((nrhosts > NLM_HOST_MAX)? 120 * HZ : 60 * HZ)
1da177e4 28
0cea3276 29static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
1da177e4
LT
30static unsigned long next_gc;
31static int nrhosts;
353ab6e9 32static DEFINE_MUTEX(nlm_host_mutex);
1da177e4
LT
33
34
35static void nlm_gc_hosts(void);
8dead0db 36static struct nsm_handle * __nsm_find(const struct sockaddr_in *,
48df020a 37 const char *, unsigned int, int);
c585646d
AB
38static struct nsm_handle * nsm_find(const struct sockaddr_in *sin,
39 const char *hostname,
48df020a 40 unsigned int hostname_len);
1da177e4
LT
41
42/*
43 * Common host lookup routine for server & client
44 */
c585646d 45static struct nlm_host *
cf712c24 46nlm_lookup_host(int server, const struct sockaddr_in *sin,
c98451bd 47 int proto, int version, const char *hostname,
48df020a
CL
48 unsigned int hostname_len,
49 const struct sockaddr_in *ssin)
1da177e4 50{
0cea3276
OK
51 struct hlist_head *chain;
52 struct hlist_node *pos;
53 struct nlm_host *host;
8dead0db 54 struct nsm_handle *nsm = NULL;
1da177e4
LT
55 int hash;
56
c98451bd
FM
57 dprintk("lockd: nlm_lookup_host("NIPQUAD_FMT"->"NIPQUAD_FMT
58 ", p=%d, v=%d, my role=%s, name=%.*s)\n",
59 NIPQUAD(ssin->sin_addr.s_addr),
db4e4c9a
OK
60 NIPQUAD(sin->sin_addr.s_addr), proto, version,
61 server? "server" : "client",
62 hostname_len,
63 hostname? hostname : "<none>");
64
1da177e4
LT
65
66 hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
67
68 /* Lock hash table */
353ab6e9 69 mutex_lock(&nlm_host_mutex);
1da177e4
LT
70
71 if (time_after_eq(jiffies, next_gc))
72 nlm_gc_hosts();
73
8dead0db
OK
74 /* We may keep several nlm_host objects for a peer, because each
75 * nlm_host is identified by
76 * (address, protocol, version, server/client)
77 * We could probably simplify this a little by putting all those
78 * different NLM rpc_clients into one single nlm_host object.
79 * This would allow us to have one nlm_host per address.
80 */
0cea3276
OK
81 chain = &nlm_hosts[hash];
82 hlist_for_each_entry(host, pos, chain, h_hash) {
8dead0db
OK
83 if (!nlm_cmp_addr(&host->h_addr, sin))
84 continue;
85
86 /* See if we have an NSM handle for this client */
6b54dae2
N
87 if (!nsm)
88 nsm = host->h_nsmhandle;
8dead0db 89
1da177e4
LT
90 if (host->h_proto != proto)
91 continue;
92 if (host->h_version != version)
93 continue;
94 if (host->h_server != server)
95 continue;
c98451bd
FM
96 if (!nlm_cmp_addr(&host->h_saddr, ssin))
97 continue;
1da177e4 98
0cea3276
OK
99 /* Move to head of hash chain. */
100 hlist_del(&host->h_hash);
101 hlist_add_head(&host->h_hash, chain);
102
f0737a39
OK
103 nlm_get_host(host);
104 goto out;
1da177e4 105 }
6b54dae2
N
106 if (nsm)
107 atomic_inc(&nsm->sm_count);
1da177e4 108
0cea3276
OK
109 host = NULL;
110
8dead0db
OK
111 /* Sadly, the host isn't in our hash table yet. See if
112 * we have an NSM handle for it. If not, create one.
113 */
114 if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
115 goto out;
1da177e4 116
f8314dc6 117 host = kzalloc(sizeof(*host), GFP_KERNEL);
8dead0db
OK
118 if (!host) {
119 nsm_release(nsm);
120 goto out;
121 }
122 host->h_name = nsm->sm_name;
1da177e4
LT
123 host->h_addr = *sin;
124 host->h_addr.sin_port = 0; /* ouch! */
c98451bd 125 host->h_saddr = *ssin;
1da177e4
LT
126 host->h_version = version;
127 host->h_proto = proto;
128 host->h_rpcclnt = NULL;
50467914 129 mutex_init(&host->h_mutex);
1da177e4
LT
130 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
131 host->h_expires = jiffies + NLM_HOST_EXPIRE;
132 atomic_set(&host->h_count, 1);
133 init_waitqueue_head(&host->h_gracewait);
28df955a 134 init_rwsem(&host->h_rwsem);
1da177e4
LT
135 host->h_state = 0; /* pseudo NSM state */
136 host->h_nsmstate = 0; /* real NSM state */
8dead0db 137 host->h_nsmhandle = nsm;
1da177e4 138 host->h_server = server;
0cea3276 139 hlist_add_head(&host->h_hash, chain);
1da177e4
LT
140 INIT_LIST_HEAD(&host->h_lockowners);
141 spin_lock_init(&host->h_lock);
26bcbf96
CH
142 INIT_LIST_HEAD(&host->h_granted);
143 INIT_LIST_HEAD(&host->h_reclaim);
1da177e4
LT
144
145 if (++nrhosts > NLM_HOST_MAX)
146 next_gc = 0;
147
8dead0db 148out:
353ab6e9 149 mutex_unlock(&nlm_host_mutex);
1da177e4
LT
150 return host;
151}
152
c53c1bb9
OK
153/*
154 * Destroy a host
155 */
156static void
157nlm_destroy_host(struct nlm_host *host)
158{
159 struct rpc_clnt *clnt;
160
161 BUG_ON(!list_empty(&host->h_lockowners));
162 BUG_ON(atomic_read(&host->h_count));
163
164 /*
165 * Release NSM handle and unmonitor host.
166 */
167 nsm_unmonitor(host);
168
34f52e35
TM
169 clnt = host->h_rpcclnt;
170 if (clnt != NULL)
171 rpc_shutdown_client(clnt);
c53c1bb9
OK
172 kfree(host);
173}
174
c585646d
AB
175/*
176 * Find an NLM server handle in the cache. If there is none, create it.
177 */
178struct nlm_host *
179nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
48df020a 180 const char *hostname, unsigned int hostname_len)
c585646d 181{
c98451bd
FM
182 struct sockaddr_in ssin = {0};
183
c585646d 184 return nlm_lookup_host(0, sin, proto, version,
c98451bd 185 hostname, hostname_len, &ssin);
c585646d
AB
186}
187
188/*
189 * Find an NLM client handle in the cache. If there is none, create it.
190 */
191struct nlm_host *
192nlmsvc_lookup_host(struct svc_rqst *rqstp,
48df020a 193 const char *hostname, unsigned int hostname_len)
c585646d 194{
c98451bd
FM
195 struct sockaddr_in ssin = {0};
196
197 ssin.sin_addr = rqstp->rq_daddr.addr;
27459f09 198 return nlm_lookup_host(1, svc_addr_in(rqstp),
c585646d 199 rqstp->rq_prot, rqstp->rq_vers,
c98451bd 200 hostname, hostname_len, &ssin);
c585646d
AB
201}
202
1da177e4
LT
203/*
204 * Create the NLM RPC client for an NLM peer
205 */
206struct rpc_clnt *
207nlm_bind_host(struct nlm_host *host)
208{
209 struct rpc_clnt *clnt;
1da177e4 210
c98451bd
FM
211 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT"->"NIPQUAD_FMT")\n",
212 NIPQUAD(host->h_saddr.sin_addr),
213 NIPQUAD(host->h_addr.sin_addr));
1da177e4
LT
214
215 /* Lock host handle */
50467914 216 mutex_lock(&host->h_mutex);
1da177e4
LT
217
218 /* If we've already created an RPC client, check whether
219 * RPC rebind is required
1da177e4
LT
220 */
221 if ((clnt = host->h_rpcclnt) != NULL) {
43118c29 222 if (time_after_eq(jiffies, host->h_nextrebind)) {
35f5a422 223 rpc_force_rebind(clnt);
1da177e4
LT
224 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
225 dprintk("lockd: next rebind in %ld jiffies\n",
226 host->h_nextrebind - jiffies);
227 }
228 } else {
21051ba6 229 unsigned long increment = nlmsvc_timeout;
e1ec7892
CL
230 struct rpc_timeout timeparms = {
231 .to_initval = increment,
232 .to_increment = increment,
233 .to_maxval = increment * 6UL,
234 .to_retries = 5U,
235 };
236 struct rpc_create_args args = {
237 .protocol = host->h_proto,
238 .address = (struct sockaddr *)&host->h_addr,
239 .addrsize = sizeof(host->h_addr),
c98451bd 240 .saddress = (struct sockaddr *)&host->h_saddr,
e1ec7892
CL
241 .timeout = &timeparms,
242 .servername = host->h_name,
243 .program = &nlm_program,
244 .version = host->h_version,
245 .authflavor = RPC_AUTH_UNIX,
246 .flags = (RPC_CLNT_CREATE_HARDRTRY |
031fd3aa 247 RPC_CLNT_CREATE_NOPING |
e1ec7892
CL
248 RPC_CLNT_CREATE_AUTOBIND),
249 };
250
251 clnt = rpc_create(&args);
252 if (!IS_ERR(clnt))
253 host->h_rpcclnt = clnt;
254 else {
255 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
256 clnt = NULL;
257 }
1da177e4
LT
258 }
259
50467914 260 mutex_unlock(&host->h_mutex);
1da177e4 261 return clnt;
1da177e4
LT
262}
263
264/*
265 * Force a portmap lookup of the remote lockd port
266 */
267void
268nlm_rebind_host(struct nlm_host *host)
269{
270 dprintk("lockd: rebind host %s\n", host->h_name);
271 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
35f5a422 272 rpc_force_rebind(host->h_rpcclnt);
1da177e4
LT
273 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
274 }
275}
276
277/*
278 * Increment NLM host count
279 */
280struct nlm_host * nlm_get_host(struct nlm_host *host)
281{
282 if (host) {
283 dprintk("lockd: get host %s\n", host->h_name);
284 atomic_inc(&host->h_count);
285 host->h_expires = jiffies + NLM_HOST_EXPIRE;
286 }
287 return host;
288}
289
290/*
291 * Release NLM host after use
292 */
293void nlm_release_host(struct nlm_host *host)
294{
295 if (host != NULL) {
296 dprintk("lockd: release host %s\n", host->h_name);
1da177e4 297 BUG_ON(atomic_read(&host->h_count) < 0);
4c060b53
TM
298 if (atomic_dec_and_test(&host->h_count)) {
299 BUG_ON(!list_empty(&host->h_lockowners));
300 BUG_ON(!list_empty(&host->h_granted));
301 BUG_ON(!list_empty(&host->h_reclaim));
302 }
1da177e4
LT
303 }
304}
305
cf712c24
OK
306/*
307 * We were notified that the host indicated by address &sin
308 * has rebooted.
309 * Release all resources held by that peer.
310 */
5c8dd29c 311void nlm_host_rebooted(const struct sockaddr_in *sin,
48df020a
CL
312 const char *hostname,
313 unsigned int hostname_len,
5c8dd29c 314 u32 new_state)
cf712c24 315{
0cea3276
OK
316 struct hlist_head *chain;
317 struct hlist_node *pos;
5c8dd29c 318 struct nsm_handle *nsm;
0cea3276 319 struct nlm_host *host;
cf712c24 320
5c8dd29c
OK
321 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
322 hostname, NIPQUAD(sin->sin_addr));
323
324 /* Find the NSM handle for this peer */
325 if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
db4e4c9a
OK
326 return;
327
5c8dd29c
OK
328 /* When reclaiming locks on this peer, make sure that
329 * we set up a new notification */
330 nsm->sm_monitored = 0;
331
332 /* Mark all hosts tied to this NSM state as having rebooted.
333 * We run the loop repeatedly, because we drop the host table
334 * lock for this.
335 * To avoid processing a host several times, we match the nsmstate.
336 */
337again: mutex_lock(&nlm_host_mutex);
0cea3276
OK
338 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
339 hlist_for_each_entry(host, pos, chain, h_hash) {
5c8dd29c
OK
340 if (host->h_nsmhandle == nsm
341 && host->h_nsmstate != new_state) {
342 host->h_nsmstate = new_state;
343 host->h_state++;
344
345 nlm_get_host(host);
346 mutex_unlock(&nlm_host_mutex);
347
348 if (host->h_server) {
349 /* We're server for this guy, just ditch
350 * all the locks he held. */
351 nlmsvc_free_host_resources(host);
352 } else {
353 /* He's the server, initiate lock recovery. */
354 nlmclnt_recovery(host);
355 }
356
357 nlm_release_host(host);
358 goto again;
359 }
360 }
cf712c24 361 }
5c8dd29c
OK
362
363 mutex_unlock(&nlm_host_mutex);
cf712c24
OK
364}
365
1da177e4
LT
366/*
367 * Shut down the hosts module.
368 * Note that this routine is called only at server shutdown time.
369 */
370void
371nlm_shutdown_hosts(void)
372{
0cea3276
OK
373 struct hlist_head *chain;
374 struct hlist_node *pos;
1da177e4 375 struct nlm_host *host;
1da177e4
LT
376
377 dprintk("lockd: shutting down host module\n");
353ab6e9 378 mutex_lock(&nlm_host_mutex);
1da177e4
LT
379
380 /* First, make all hosts eligible for gc */
381 dprintk("lockd: nuking all hosts...\n");
0cea3276 382 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
d801b861 383 hlist_for_each_entry(host, pos, chain, h_hash) {
1da177e4 384 host->h_expires = jiffies - 1;
d801b861
JL
385 if (host->h_rpcclnt) {
386 rpc_shutdown_client(host->h_rpcclnt);
387 host->h_rpcclnt = NULL;
388 }
389 }
1da177e4
LT
390 }
391
392 /* Then, perform a garbage collection pass */
393 nlm_gc_hosts();
353ab6e9 394 mutex_unlock(&nlm_host_mutex);
1da177e4
LT
395
396 /* complain if any hosts are left */
397 if (nrhosts) {
398 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
399 dprintk("lockd: %d hosts left:\n", nrhosts);
0cea3276
OK
400 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
401 hlist_for_each_entry(host, pos, chain, h_hash) {
1da177e4
LT
402 dprintk(" %s (cnt %d use %d exp %ld)\n",
403 host->h_name, atomic_read(&host->h_count),
404 host->h_inuse, host->h_expires);
405 }
406 }
407 }
408}
409
410/*
411 * Garbage collect any unused NLM hosts.
412 * This GC combines reference counting for async operations with
413 * mark & sweep for resources held by remote clients.
414 */
415static void
416nlm_gc_hosts(void)
417{
0cea3276
OK
418 struct hlist_head *chain;
419 struct hlist_node *pos, *next;
420 struct nlm_host *host;
1da177e4
LT
421
422 dprintk("lockd: host garbage collection\n");
0cea3276
OK
423 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
424 hlist_for_each_entry(host, pos, chain, h_hash)
1da177e4
LT
425 host->h_inuse = 0;
426 }
427
428 /* Mark all hosts that hold locks, blocks or shares */
429 nlmsvc_mark_resources();
430
0cea3276
OK
431 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
432 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
1da177e4
LT
433 if (atomic_read(&host->h_count) || host->h_inuse
434 || time_before(jiffies, host->h_expires)) {
435 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
436 host->h_name, atomic_read(&host->h_count),
437 host->h_inuse, host->h_expires);
1da177e4
LT
438 continue;
439 }
440 dprintk("lockd: delete host %s\n", host->h_name);
0cea3276 441 hlist_del_init(&host->h_hash);
977faf39 442
c53c1bb9 443 nlm_destroy_host(host);
1da177e4
LT
444 nrhosts--;
445 }
446 }
447
448 next_gc = jiffies + NLM_HOST_COLLECT;
449}
450
8dead0db
OK
451
452/*
453 * Manage NSM handles
454 */
455static LIST_HEAD(nsm_handles);
89e63ef6 456static DEFINE_MUTEX(nsm_mutex);
8dead0db
OK
457
458static struct nsm_handle *
459__nsm_find(const struct sockaddr_in *sin,
48df020a 460 const char *hostname, unsigned int hostname_len,
8dead0db
OK
461 int create)
462{
463 struct nsm_handle *nsm = NULL;
464 struct list_head *pos;
465
466 if (!sin)
467 return NULL;
468
469 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
470 if (printk_ratelimit()) {
471 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
472 "in NFS lock request\n",
473 hostname_len, hostname);
474 }
475 return NULL;
476 }
477
89e63ef6 478 mutex_lock(&nsm_mutex);
8dead0db
OK
479 list_for_each(pos, &nsm_handles) {
480 nsm = list_entry(pos, struct nsm_handle, sm_link);
481
abd1f500
OK
482 if (hostname && nsm_use_hostnames) {
483 if (strlen(nsm->sm_name) != hostname_len
484 || memcmp(nsm->sm_name, hostname, hostname_len))
485 continue;
486 } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
8dead0db
OK
487 continue;
488 atomic_inc(&nsm->sm_count);
489 goto out;
490 }
491
492 if (!create) {
493 nsm = NULL;
494 goto out;
495 }
496
497 nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
498 if (nsm != NULL) {
499 nsm->sm_addr = *sin;
500 nsm->sm_name = (char *) (nsm + 1);
501 memcpy(nsm->sm_name, hostname, hostname_len);
502 nsm->sm_name[hostname_len] = '\0';
503 atomic_set(&nsm->sm_count, 1);
504
505 list_add(&nsm->sm_link, &nsm_handles);
506 }
507
89e63ef6
NB
508out:
509 mutex_unlock(&nsm_mutex);
8dead0db
OK
510 return nsm;
511}
512
c585646d 513static struct nsm_handle *
48df020a
CL
514nsm_find(const struct sockaddr_in *sin, const char *hostname,
515 unsigned int hostname_len)
8dead0db
OK
516{
517 return __nsm_find(sin, hostname, hostname_len, 1);
518}
519
520/*
521 * Release an NSM handle
522 */
523void
524nsm_release(struct nsm_handle *nsm)
525{
526 if (!nsm)
527 return;
528 if (atomic_dec_and_test(&nsm->sm_count)) {
89e63ef6 529 mutex_lock(&nsm_mutex);
8dead0db
OK
530 if (atomic_read(&nsm->sm_count) == 0) {
531 list_del(&nsm->sm_link);
532 kfree(nsm);
533 }
89e63ef6 534 mutex_unlock(&nsm_mutex);
8dead0db
OK
535 }
536}