]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/lockd/host.c
Merge branch 'omap-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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>
1b333c54 14#include <linux/in6.h>
1da177e4
LT
15#include <linux/sunrpc/clnt.h>
16#include <linux/sunrpc/svc.h>
17#include <linux/lockd/lockd.h>
353ab6e9 18#include <linux/mutex.h>
1da177e4 19
1b333c54 20#include <net/ipv6.h>
1da177e4
LT
21
22#define NLMDBG_FACILITY NLMDBG_HOSTCACHE
1da177e4 23#define NLM_HOST_NRHASH 32
1da177e4 24#define NLM_HOST_REBIND (60 * HZ)
1447d25e
N
25#define NLM_HOST_EXPIRE (300 * HZ)
26#define NLM_HOST_COLLECT (120 * HZ)
1da177e4 27
0cea3276 28static struct hlist_head nlm_hosts[NLM_HOST_NRHASH];
1da177e4
LT
29static unsigned long next_gc;
30static int nrhosts;
353ab6e9 31static DEFINE_MUTEX(nlm_host_mutex);
1da177e4 32
1da177e4 33static void nlm_gc_hosts(void);
1da177e4 34
7f1ed18b
CL
35struct nlm_lookup_host_info {
36 const int server; /* search for server|client */
88541c84
CL
37 const struct sockaddr *sap; /* address to search for */
38 const size_t salen; /* it's length */
7f1ed18b
CL
39 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 */
88541c84 43 const struct sockaddr *src_sap; /* our address (optional) */
7f1ed18b 44 const size_t src_len; /* it's length */
0cb2659b 45 const int noresvport; /* use non-priv port */
7f1ed18b
CL
46};
47
ede2fea0
CL
48/*
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
1da177e4
LT
90/*
91 * Common host lookup routine for server & client
92 */
7f1ed18b 93static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
1da177e4 94{
0cea3276
OK
95 struct hlist_head *chain;
96 struct hlist_node *pos;
97 struct nlm_host *host;
8dead0db 98 struct nsm_handle *nsm = NULL;
1da177e4 99
353ab6e9 100 mutex_lock(&nlm_host_mutex);
1da177e4
LT
101
102 if (time_after_eq(jiffies, next_gc))
103 nlm_gc_hosts();
104
8dead0db
OK
105 /* 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 */
88541c84 112 chain = &nlm_hosts[nlm_hash_address(ni->sap)];
0cea3276 113 hlist_for_each_entry(host, pos, chain, h_hash) {
4516fc04 114 if (!rpc_cmp_addr(nlm_addr(host), ni->sap))
8dead0db
OK
115 continue;
116
117 /* See if we have an NSM handle for this client */
6b54dae2
N
118 if (!nsm)
119 nsm = host->h_nsmhandle;
8dead0db 120
7f1ed18b 121 if (host->h_proto != ni->protocol)
1da177e4 122 continue;
7f1ed18b 123 if (host->h_version != ni->version)
1da177e4 124 continue;
7f1ed18b 125 if (host->h_server != ni->server)
1da177e4 126 continue;
a8d82d9b 127 if (ni->server &&
4516fc04 128 !rpc_cmp_addr(nlm_srcaddr(host), ni->src_sap))
c98451bd 129 continue;
1da177e4 130
0cea3276
OK
131 /* Move to head of hash chain. */
132 hlist_del(&host->h_hash);
133 hlist_add_head(&host->h_hash, chain);
134
f0737a39 135 nlm_get_host(host);
1b333c54
CL
136 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
137 host->h_name, host->h_addrbuf);
f0737a39 138 goto out;
1da177e4
LT
139 }
140
c2526f42
CL
141 /*
142 * The host wasn't in our hash table. If we don't
143 * have an NSM handle for it yet, create one.
8dead0db 144 */
c2526f42
CL
145 if (nsm)
146 atomic_inc(&nsm->sm_count);
147 else {
148 host = NULL;
92fd91b9
CL
149 nsm = nsm_get_handle(ni->sap, ni->salen,
150 ni->hostname, ni->hostname_len);
1b333c54
CL
151 if (!nsm) {
152 dprintk("lockd: nlm_lookup_host failed; "
153 "no nsm handle\n");
c2526f42 154 goto out;
1b333c54 155 }
c2526f42 156 }
1da177e4 157
f8314dc6 158 host = kzalloc(sizeof(*host), GFP_KERNEL);
8dead0db
OK
159 if (!host) {
160 nsm_release(nsm);
1b333c54 161 dprintk("lockd: nlm_lookup_host failed; no memory\n");
8dead0db
OK
162 goto out;
163 }
164 host->h_name = nsm->sm_name;
1df40b60 165 host->h_addrbuf = nsm->sm_addrbuf;
88541c84
CL
166 memcpy(nlm_addr(host), ni->sap, ni->salen);
167 host->h_addrlen = ni->salen;
b97a5674 168 rpc_set_port(nlm_addr(host), 0);
88541c84 169 memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
7f1ed18b
CL
170 host->h_version = ni->version;
171 host->h_proto = ni->protocol;
1da177e4 172 host->h_rpcclnt = NULL;
50467914 173 mutex_init(&host->h_mutex);
1da177e4
LT
174 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);
28df955a 178 init_rwsem(&host->h_rwsem);
1da177e4
LT
179 host->h_state = 0; /* pseudo NSM state */
180 host->h_nsmstate = 0; /* real NSM state */
8dead0db 181 host->h_nsmhandle = nsm;
7f1ed18b 182 host->h_server = ni->server;
0cb2659b 183 host->h_noresvport = ni->noresvport;
0cea3276 184 hlist_add_head(&host->h_hash, chain);
1da177e4
LT
185 INIT_LIST_HEAD(&host->h_lockowners);
186 spin_lock_init(&host->h_lock);
26bcbf96
CH
187 INIT_LIST_HEAD(&host->h_granted);
188 INIT_LIST_HEAD(&host->h_reclaim);
1da177e4 189
1447d25e 190 nrhosts++;
1b333c54 191
1b333c54
CL
192 dprintk("lockd: nlm_lookup_host created host %s\n",
193 host->h_name);
194
8dead0db 195out:
353ab6e9 196 mutex_unlock(&nlm_host_mutex);
1da177e4
LT
197 return host;
198}
199
c53c1bb9
OK
200/*
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
c53c1bb9 211 nsm_unmonitor(host);
c8c23c42 212 nsm_release(host->h_nsmhandle);
c53c1bb9 213
34f52e35
TM
214 clnt = host->h_rpcclnt;
215 if (clnt != NULL)
216 rpc_shutdown_client(clnt);
c53c1bb9
OK
217 kfree(host);
218}
219
d7d20440
CL
220/**
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
0cb2659b 227 * @noresvport: 1 if non-privileged port should be used
d7d20440
CL
228 *
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.
c585646d 233 */
d7d20440
CL
234struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
235 const size_t salen,
236 const unsigned short protocol,
0cb2659b
CL
237 const u32 version,
238 const char *hostname,
239 int noresvport)
c585646d 240{
88541c84
CL
241 const struct sockaddr source = {
242 .sa_family = AF_UNSPEC,
2860a022 243 };
7f1ed18b
CL
244 struct nlm_lookup_host_info ni = {
245 .server = 0,
d7d20440
CL
246 .sap = sap,
247 .salen = salen,
248 .protocol = protocol,
7f1ed18b
CL
249 .version = version,
250 .hostname = hostname,
d7d20440 251 .hostname_len = strlen(hostname),
88541c84
CL
252 .src_sap = &source,
253 .src_len = sizeof(source),
0cb2659b 254 .noresvport = noresvport,
7f1ed18b 255 };
c98451bd 256
7f1ed18b
CL
257 dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
258 (hostname ? hostname : "<none>"), version,
d7d20440 259 (protocol == IPPROTO_UDP ? "udp" : "tcp"));
7f1ed18b
CL
260
261 return nlm_lookup_host(&ni);
c585646d
AB
262}
263
6bfbe8af
CL
264/**
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.
c585646d 281 */
6bfbe8af
CL
282struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
283 const char *hostname,
284 const size_t hostname_len)
c585646d 285{
6bfbe8af 286 struct sockaddr_in sin = {
2860a022 287 .sin_family = AF_INET,
6bfbe8af
CL
288 };
289 struct sockaddr_in6 sin6 = {
290 .sin6_family = AF_INET6,
2860a022 291 };
7f1ed18b
CL
292 struct nlm_lookup_host_info ni = {
293 .server = 1,
88541c84
CL
294 .sap = svc_addr(rqstp),
295 .salen = rqstp->rq_addrlen,
7f1ed18b
CL
296 .protocol = rqstp->rq_prot,
297 .version = rqstp->rq_vers,
298 .hostname = hostname,
299 .hostname_len = hostname_len,
6bfbe8af 300 .src_len = rqstp->rq_addrlen,
7f1ed18b
CL
301 };
302
303 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"));
c98451bd 306
6bfbe8af
CL
307 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
7f1ed18b 320 return nlm_lookup_host(&ni);
c585646d
AB
321}
322
1da177e4
LT
323/*
324 * 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;
1da177e4 330
1df40b60
CL
331 dprintk("lockd: nlm_bind_host %s (%s)\n",
332 host->h_name, host->h_addrbuf);
1da177e4
LT
333
334 /* Lock host handle */
50467914 335 mutex_lock(&host->h_mutex);
1da177e4
LT
336
337 /* If we've already created an RPC client, check whether
338 * RPC rebind is required
1da177e4
LT
339 */
340 if ((clnt = host->h_rpcclnt) != NULL) {
43118c29 341 if (time_after_eq(jiffies, host->h_nextrebind)) {
35f5a422 342 rpc_force_rebind(clnt);
1da177e4 343 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
1b333c54 344 dprintk("lockd: next rebind in %lu jiffies\n",
1da177e4
LT
345 host->h_nextrebind - jiffies);
346 }
347 } else {
21051ba6 348 unsigned long increment = nlmsvc_timeout;
e1ec7892
CL
349 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 = {
c653ce3f 356 .net = &init_net,
e1ec7892 357 .protocol = host->h_proto,
b4ed58fd
CL
358 .address = nlm_addr(host),
359 .addrsize = host->h_addrlen,
90151e6e 360 .saddress = nlm_srcaddr(host),
e1ec7892
CL
361 .timeout = &timeparms,
362 .servername = host->h_name,
363 .program = &nlm_program,
364 .version = host->h_version,
365 .authflavor = RPC_AUTH_UNIX,
90bd17c8 366 .flags = (RPC_CLNT_CREATE_NOPING |
e1ec7892
CL
367 RPC_CLNT_CREATE_AUTOBIND),
368 };
369
90bd17c8
JL
370 /*
371 * lockd retries server side blocks automatically so we want
372 * those to be soft RPC calls. Client side calls need to be
373 * hard RPC tasks.
374 */
375 if (!host->h_server)
376 args.flags |= RPC_CLNT_CREATE_HARDRTRY;
0cb2659b
CL
377 if (host->h_noresvport)
378 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
90bd17c8 379
e1ec7892
CL
380 clnt = rpc_create(&args);
381 if (!IS_ERR(clnt))
382 host->h_rpcclnt = clnt;
383 else {
384 printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
385 clnt = NULL;
386 }
1da177e4
LT
387 }
388
50467914 389 mutex_unlock(&host->h_mutex);
1da177e4 390 return clnt;
1da177e4
LT
391}
392
393/*
394 * Force a portmap lookup of the remote lockd port
395 */
396void
397nlm_rebind_host(struct nlm_host *host)
398{
399 dprintk("lockd: rebind host %s\n", host->h_name);
400 if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
35f5a422 401 rpc_force_rebind(host->h_rpcclnt);
1da177e4
LT
402 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
403 }
404}
405
406/*
407 * Increment NLM host count
408 */
409struct nlm_host * nlm_get_host(struct nlm_host *host)
410{
411 if (host) {
412 dprintk("lockd: get host %s\n", host->h_name);
413 atomic_inc(&host->h_count);
414 host->h_expires = jiffies + NLM_HOST_EXPIRE;
415 }
416 return host;
417}
418
419/*
420 * Release NLM host after use
421 */
422void nlm_release_host(struct nlm_host *host)
423{
424 if (host != NULL) {
425 dprintk("lockd: release host %s\n", host->h_name);
1da177e4 426 BUG_ON(atomic_read(&host->h_count) < 0);
4c060b53
TM
427 if (atomic_dec_and_test(&host->h_count)) {
428 BUG_ON(!list_empty(&host->h_lockowners));
429 BUG_ON(!list_empty(&host->h_granted));
430 BUG_ON(!list_empty(&host->h_reclaim));
431 }
1da177e4
LT
432 }
433}
434
7fefc9cb
CL
435/**
436 * nlm_host_rebooted - Release all resources held by rebooted host
437 * @info: pointer to decoded results of NLM_SM_NOTIFY call
438 *
439 * We were notified that the specified host has rebooted. Release
440 * all resources held by that peer.
cf712c24 441 */
7fefc9cb 442void nlm_host_rebooted(const struct nlm_reboot *info)
cf712c24 443{
0cea3276
OK
444 struct hlist_head *chain;
445 struct hlist_node *pos;
5c8dd29c 446 struct nsm_handle *nsm;
0cea3276 447 struct nlm_host *host;
cf712c24 448
8c7378fd
CL
449 nsm = nsm_reboot_lookup(info);
450 if (unlikely(nsm == NULL))
db4e4c9a 451 return;
5c8dd29c
OK
452
453 /* Mark all hosts tied to this NSM state as having rebooted.
454 * We run the loop repeatedly, because we drop the host table
455 * lock for this.
456 * To avoid processing a host several times, we match the nsmstate.
457 */
458again: mutex_lock(&nlm_host_mutex);
0cea3276
OK
459 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
460 hlist_for_each_entry(host, pos, chain, h_hash) {
5c8dd29c 461 if (host->h_nsmhandle == nsm
7fefc9cb
CL
462 && host->h_nsmstate != info->state) {
463 host->h_nsmstate = info->state;
5c8dd29c
OK
464 host->h_state++;
465
466 nlm_get_host(host);
467 mutex_unlock(&nlm_host_mutex);
468
469 if (host->h_server) {
470 /* We're server for this guy, just ditch
471 * all the locks he held. */
472 nlmsvc_free_host_resources(host);
473 } else {
474 /* He's the server, initiate lock recovery. */
475 nlmclnt_recovery(host);
476 }
477
478 nlm_release_host(host);
479 goto again;
480 }
481 }
cf712c24 482 }
5c8dd29c 483 mutex_unlock(&nlm_host_mutex);
cdd30fa1 484 nsm_release(nsm);
cf712c24
OK
485}
486
1da177e4
LT
487/*
488 * Shut down the hosts module.
489 * Note that this routine is called only at server shutdown time.
490 */
491void
492nlm_shutdown_hosts(void)
493{
0cea3276
OK
494 struct hlist_head *chain;
495 struct hlist_node *pos;
1da177e4 496 struct nlm_host *host;
1da177e4
LT
497
498 dprintk("lockd: shutting down host module\n");
353ab6e9 499 mutex_lock(&nlm_host_mutex);
1da177e4
LT
500
501 /* First, make all hosts eligible for gc */
502 dprintk("lockd: nuking all hosts...\n");
0cea3276 503 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
d801b861 504 hlist_for_each_entry(host, pos, chain, h_hash) {
1da177e4 505 host->h_expires = jiffies - 1;
d801b861
JL
506 if (host->h_rpcclnt) {
507 rpc_shutdown_client(host->h_rpcclnt);
508 host->h_rpcclnt = NULL;
509 }
510 }
1da177e4
LT
511 }
512
513 /* Then, perform a garbage collection pass */
514 nlm_gc_hosts();
353ab6e9 515 mutex_unlock(&nlm_host_mutex);
1da177e4
LT
516
517 /* complain if any hosts are left */
518 if (nrhosts) {
519 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
520 dprintk("lockd: %d hosts left:\n", nrhosts);
0cea3276
OK
521 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
522 hlist_for_each_entry(host, pos, chain, h_hash) {
1da177e4
LT
523 dprintk(" %s (cnt %d use %d exp %ld)\n",
524 host->h_name, atomic_read(&host->h_count),
525 host->h_inuse, host->h_expires);
526 }
527 }
528 }
529}
530
531/*
532 * Garbage collect any unused NLM hosts.
533 * This GC combines reference counting for async operations with
534 * mark & sweep for resources held by remote clients.
535 */
536static void
537nlm_gc_hosts(void)
538{
0cea3276
OK
539 struct hlist_head *chain;
540 struct hlist_node *pos, *next;
541 struct nlm_host *host;
1da177e4
LT
542
543 dprintk("lockd: host garbage collection\n");
0cea3276
OK
544 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
545 hlist_for_each_entry(host, pos, chain, h_hash)
1da177e4
LT
546 host->h_inuse = 0;
547 }
548
549 /* Mark all hosts that hold locks, blocks or shares */
550 nlmsvc_mark_resources();
551
0cea3276
OK
552 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
553 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
1da177e4
LT
554 if (atomic_read(&host->h_count) || host->h_inuse
555 || time_before(jiffies, host->h_expires)) {
556 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
557 host->h_name, atomic_read(&host->h_count),
558 host->h_inuse, host->h_expires);
1da177e4
LT
559 continue;
560 }
561 dprintk("lockd: delete host %s\n", host->h_name);
0cea3276 562 hlist_del_init(&host->h_hash);
977faf39 563
c53c1bb9 564 nlm_destroy_host(host);
1da177e4
LT
565 nrhosts--;
566 }
567 }
568
569 next_gc = jiffies + NLM_HOST_COLLECT;
570}