lockd: Combine __nsm_find() and nsm_find().
[linux-2.6] / fs / lockd / host.c
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>
12 #include <linux/slab.h>
13 #include <linux/in.h>
14 #include <linux/in6.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>
19 #include <linux/mutex.h>
20
21 #include <net/ipv6.h>
22
23 #define NLMDBG_FACILITY         NLMDBG_HOSTCACHE
24 #define NLM_HOST_NRHASH         32
25 #define NLM_HOST_REBIND         (60 * HZ)
26 #define NLM_HOST_EXPIRE         (300 * HZ)
27 #define NLM_HOST_COLLECT        (120 * HZ)
28
29 static struct hlist_head        nlm_hosts[NLM_HOST_NRHASH];
30 static unsigned long            next_gc;
31 static int                      nrhosts;
32 static DEFINE_MUTEX(nlm_host_mutex);
33
34 static void                     nlm_gc_hosts(void);
35 static struct nsm_handle        *nsm_find(const struct sockaddr_in *sin,
36                                                 const char *hostname,
37                                                 const size_t hostname_len,
38                                                 const int create);
39
40 /*
41  * Hash function must work well on big- and little-endian platforms
42  */
43 static 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
49 static 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
55 static 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
65 static 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
82 static 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
94 static 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
120 /*
121  * Common host lookup routine for server & client
122  */
123 static 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)
129 {
130         struct hlist_head *chain;
131         struct hlist_node *pos;
132         struct nlm_host *host;
133         struct nsm_handle *nsm = NULL;
134
135         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>");
139
140         mutex_lock(&nlm_host_mutex);
141
142         if (time_after_eq(jiffies, next_gc))
143                 nlm_gc_hosts();
144
145         /* 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          */
152         chain = &nlm_hosts[nlm_hash_address((struct sockaddr *)sin)];
153         hlist_for_each_entry(host, pos, chain, h_hash) {
154                 if (!nlm_cmp_addr(nlm_addr(host), (struct sockaddr *)sin))
155                         continue;
156
157                 /* See if we have an NSM handle for this client */
158                 if (!nsm)
159                         nsm = host->h_nsmhandle;
160
161                 if (host->h_proto != proto)
162                         continue;
163                 if (host->h_version != version)
164                         continue;
165                 if (host->h_server != server)
166                         continue;
167                 if (!nlm_cmp_addr(nlm_srcaddr(host), (struct sockaddr *)ssin))
168                         continue;
169
170                 /* Move to head of hash chain. */
171                 hlist_del(&host->h_hash);
172                 hlist_add_head(&host->h_hash, chain);
173
174                 nlm_get_host(host);
175                 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
176                                 host->h_name, host->h_addrbuf);
177                 goto out;
178         }
179
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          */
184         if (nsm)
185                 atomic_inc(&nsm->sm_count);
186         else {
187                 host = NULL;
188                 nsm = nsm_find(sin, hostname, hostname_len, 1);
189                 if (!nsm) {
190                         dprintk("lockd: nlm_lookup_host failed; "
191                                 "no nsm handle\n");
192                         goto out;
193                 }
194         }
195
196         host = kzalloc(sizeof(*host), GFP_KERNEL);
197         if (!host) {
198                 nsm_release(nsm);
199                 dprintk("lockd: nlm_lookup_host failed; no memory\n");
200                 goto out;
201         }
202         host->h_name       = nsm->sm_name;
203         memcpy(nlm_addr(host), sin, sizeof(*sin));
204         host->h_addrlen = sizeof(*sin);
205         nlm_clear_port(nlm_addr(host));
206         memcpy(nlm_srcaddr(host), ssin, sizeof(*ssin));
207         host->h_version    = version;
208         host->h_proto      = proto;
209         host->h_rpcclnt    = NULL;
210         mutex_init(&host->h_mutex);
211         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);
215         init_rwsem(&host->h_rwsem);
216         host->h_state      = 0;                 /* pseudo NSM state */
217         host->h_nsmstate   = 0;                 /* real NSM state */
218         host->h_nsmhandle  = nsm;
219         host->h_server     = server;
220         hlist_add_head(&host->h_hash, chain);
221         INIT_LIST_HEAD(&host->h_lockowners);
222         spin_lock_init(&host->h_lock);
223         INIT_LIST_HEAD(&host->h_granted);
224         INIT_LIST_HEAD(&host->h_reclaim);
225
226         nrhosts++;
227
228         nlm_display_address((struct sockaddr *)&host->h_addr,
229                                 host->h_addrbuf, sizeof(host->h_addrbuf));
230         nlm_display_address((struct sockaddr *)&host->h_srcaddr,
231                                 host->h_srcaddrbuf, sizeof(host->h_srcaddrbuf));
232
233         dprintk("lockd: nlm_lookup_host created host %s\n",
234                         host->h_name);
235
236 out:
237         mutex_unlock(&nlm_host_mutex);
238         return host;
239 }
240
241 /*
242  * Destroy a host
243  */
244 static void
245 nlm_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
257         clnt = host->h_rpcclnt;
258         if (clnt != NULL)
259                 rpc_shutdown_client(clnt);
260         kfree(host);
261 }
262
263 /*
264  * Find an NLM server handle in the cache. If there is none, create it.
265  */
266 struct nlm_host *nlmclnt_lookup_host(const struct sockaddr_in *sin,
267                                      int proto, u32 version,
268                                      const char *hostname,
269                                      unsigned int hostname_len)
270 {
271         const struct sockaddr_in source = {
272                 .sin_family     = AF_UNSPEC,
273         };
274
275         return nlm_lookup_host(0, sin, proto, version,
276                                hostname, hostname_len, &source);
277 }
278
279 /*
280  * Find an NLM client handle in the cache. If there is none, create it.
281  */
282 struct nlm_host *
283 nlmsvc_lookup_host(struct svc_rqst *rqstp,
284                         const char *hostname, unsigned int hostname_len)
285 {
286         const struct sockaddr_in source = {
287                 .sin_family     = AF_INET,
288                 .sin_addr       = rqstp->rq_daddr.addr,
289         };
290
291         return nlm_lookup_host(1, svc_addr_in(rqstp),
292                                rqstp->rq_prot, rqstp->rq_vers,
293                                hostname, hostname_len, &source);
294 }
295
296 /*
297  * Create the NLM RPC client for an NLM peer
298  */
299 struct rpc_clnt *
300 nlm_bind_host(struct nlm_host *host)
301 {
302         struct rpc_clnt *clnt;
303
304         dprintk("lockd: nlm_bind_host %s (%s), my addr=%s\n",
305                         host->h_name, host->h_addrbuf, host->h_srcaddrbuf);
306
307         /* Lock host handle */
308         mutex_lock(&host->h_mutex);
309
310         /* If we've already created an RPC client, check whether
311          * RPC rebind is required
312          */
313         if ((clnt = host->h_rpcclnt) != NULL) {
314                 if (time_after_eq(jiffies, host->h_nextrebind)) {
315                         rpc_force_rebind(clnt);
316                         host->h_nextrebind = jiffies + NLM_HOST_REBIND;
317                         dprintk("lockd: next rebind in %lu jiffies\n",
318                                         host->h_nextrebind - jiffies);
319                 }
320         } else {
321                 unsigned long increment = nlmsvc_timeout;
322                 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,
330                         .address        = nlm_addr(host),
331                         .addrsize       = host->h_addrlen,
332                         .saddress       = nlm_srcaddr(host),
333                         .timeout        = &timeparms,
334                         .servername     = host->h_name,
335                         .program        = &nlm_program,
336                         .version        = host->h_version,
337                         .authflavor     = RPC_AUTH_UNIX,
338                         .flags          = (RPC_CLNT_CREATE_NOPING |
339                                            RPC_CLNT_CREATE_AUTOBIND),
340                 };
341
342                 /*
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
350                 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                 }
357         }
358
359         mutex_unlock(&host->h_mutex);
360         return clnt;
361 }
362
363 /*
364  * Force a portmap lookup of the remote lockd port
365  */
366 void
367 nlm_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)) {
371                 rpc_force_rebind(host->h_rpcclnt);
372                 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
373         }
374 }
375
376 /*
377  * Increment NLM host count
378  */
379 struct 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  */
392 void nlm_release_host(struct nlm_host *host)
393 {
394         if (host != NULL) {
395                 dprintk("lockd: release host %s\n", host->h_name);
396                 BUG_ON(atomic_read(&host->h_count) < 0);
397                 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                 }
402         }
403 }
404
405 /*
406  * We were notified that the host indicated by address &sin
407  * has rebooted.
408  * Release all resources held by that peer.
409  */
410 void nlm_host_rebooted(const struct sockaddr_in *sin,
411                                 const char *hostname,
412                                 unsigned int hostname_len,
413                                 u32 new_state)
414 {
415         struct hlist_head *chain;
416         struct hlist_node *pos;
417         struct nsm_handle *nsm;
418         struct nlm_host *host;
419
420         nsm = nsm_find(sin, hostname, hostname_len, 0);
421         if (nsm == NULL) {
422                 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
423                                 hostname_len, hostname);
424                 return;
425         }
426
427         dprintk("lockd: nlm_host_rebooted(%.*s, %s)\n",
428                         hostname_len, hostname, nsm->sm_addrbuf);
429
430         /* 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          */
439 again:  mutex_lock(&nlm_host_mutex);
440         for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
441                 hlist_for_each_entry(host, pos, chain, h_hash) {
442                         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                 }
463         }
464
465         mutex_unlock(&nlm_host_mutex);
466 }
467
468 /*
469  * Shut down the hosts module.
470  * Note that this routine is called only at server shutdown time.
471  */
472 void
473 nlm_shutdown_hosts(void)
474 {
475         struct hlist_head *chain;
476         struct hlist_node *pos;
477         struct nlm_host *host;
478
479         dprintk("lockd: shutting down host module\n");
480         mutex_lock(&nlm_host_mutex);
481
482         /* First, make all hosts eligible for gc */
483         dprintk("lockd: nuking all hosts...\n");
484         for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
485                 hlist_for_each_entry(host, pos, chain, h_hash) {
486                         host->h_expires = jiffies - 1;
487                         if (host->h_rpcclnt) {
488                                 rpc_shutdown_client(host->h_rpcclnt);
489                                 host->h_rpcclnt = NULL;
490                         }
491                 }
492         }
493
494         /* Then, perform a garbage collection pass */
495         nlm_gc_hosts();
496         mutex_unlock(&nlm_host_mutex);
497
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);
502                 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
503                         hlist_for_each_entry(host, pos, chain, h_hash) {
504                                 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  */
517 static void
518 nlm_gc_hosts(void)
519 {
520         struct hlist_head *chain;
521         struct hlist_node *pos, *next;
522         struct nlm_host *host;
523
524         dprintk("lockd: host garbage collection\n");
525         for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
526                 hlist_for_each_entry(host, pos, chain, h_hash)
527                         host->h_inuse = 0;
528         }
529
530         /* Mark all hosts that hold locks, blocks or shares */
531         nlmsvc_mark_resources();
532
533         for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
534                 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
535                         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);
540                                 continue;
541                         }
542                         dprintk("lockd: delete host %s\n", host->h_name);
543                         hlist_del_init(&host->h_hash);
544
545                         nlm_destroy_host(host);
546                         nrhosts--;
547                 }
548         }
549
550         next_gc = jiffies + NLM_HOST_COLLECT;
551 }
552
553
554 /*
555  * Manage NSM handles
556  */
557 static LIST_HEAD(nsm_handles);
558 static DEFINE_SPINLOCK(nsm_lock);
559
560 static struct nsm_handle *nsm_find(const struct sockaddr_in *sin,
561                                    const char *hostname,
562                                    const size_t hostname_len,
563                                    const int create)
564 {
565         struct nsm_handle *nsm = NULL;
566         struct nsm_handle *pos;
567
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",
575                                 (int)hostname_len, hostname);
576                 }
577                 return NULL;
578         }
579
580 retry:
581         spin_lock(&nsm_lock);
582         list_for_each_entry(pos, &nsm_handles, sm_link) {
583
584                 if (hostname && nsm_use_hostnames) {
585                         if (strlen(pos->sm_name) != hostname_len
586                          || memcmp(pos->sm_name, hostname, hostname_len))
587                                 continue;
588                 } else if (!nlm_cmp_addr(nsm_addr(pos), (struct sockaddr *)sin))
589                         continue;
590                 atomic_inc(&pos->sm_count);
591                 kfree(nsm);
592                 nsm = pos;
593                 goto found;
594         }
595         if (nsm) {
596                 list_add(&nsm->sm_link, &nsm_handles);
597                 goto found;
598         }
599         spin_unlock(&nsm_lock);
600
601         if (!create)
602                 return NULL;
603
604         nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
605         if (nsm == NULL)
606                 return NULL;
607
608         memcpy(nsm_addr(nsm), sin, sizeof(*sin));
609         nsm->sm_addrlen = sizeof(*sin);
610         nsm->sm_name = (char *) (nsm + 1);
611         memcpy(nsm->sm_name, hostname, hostname_len);
612         nsm->sm_name[hostname_len] = '\0';
613         nlm_display_address((struct sockaddr *)&nsm->sm_addr,
614                                 nsm->sm_addrbuf, sizeof(nsm->sm_addrbuf));
615         atomic_set(&nsm->sm_count, 1);
616         goto retry;
617
618 found:
619         spin_unlock(&nsm_lock);
620         return nsm;
621 }
622
623 /*
624  * Release an NSM handle
625  */
626 void
627 nsm_release(struct nsm_handle *nsm)
628 {
629         if (!nsm)
630                 return;
631         if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
632                 list_del(&nsm->sm_link);
633                 spin_unlock(&nsm_lock);
634                 kfree(nsm);
635         }
636 }