lockd: Support AF_INET6 when hashing addresses in nlm_lookup_host
[linux-2.6.git] / 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
35 static void                     nlm_gc_hosts(void);
36 static struct nsm_handle *      __nsm_find(const struct sockaddr_in *,
37                                         const char *, unsigned int, int);
38 static struct nsm_handle *      nsm_find(const struct sockaddr_in *sin,
39                                          const char *hostname,
40                                          unsigned int hostname_len);
41
42 /*
43  * Hash function must work well on big- and little-endian platforms
44  */
45 static unsigned int __nlm_hash32(const __be32 n)
46 {
47         unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
48         return hash ^ (hash >> 8);
49 }
50
51 static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
52 {
53         const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
54         return __nlm_hash32(sin->sin_addr.s_addr);
55 }
56
57 static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
58 {
59         const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
60         const struct in6_addr addr = sin6->sin6_addr;
61         return __nlm_hash32(addr.s6_addr32[0]) ^
62                __nlm_hash32(addr.s6_addr32[1]) ^
63                __nlm_hash32(addr.s6_addr32[2]) ^
64                __nlm_hash32(addr.s6_addr32[3]);
65 }
66
67 static unsigned int nlm_hash_address(const struct sockaddr *sap)
68 {
69         unsigned int hash;
70
71         switch (sap->sa_family) {
72         case AF_INET:
73                 hash = __nlm_hash_addr4(sap);
74                 break;
75         case AF_INET6:
76                 hash = __nlm_hash_addr6(sap);
77                 break;
78         default:
79                 hash = 0;
80         }
81         return hash & (NLM_HOST_NRHASH - 1);
82 }
83
84 static void nlm_clear_port(struct sockaddr *sap)
85 {
86         switch (sap->sa_family) {
87         case AF_INET:
88                 ((struct sockaddr_in *)sap)->sin_port = 0;
89                 break;
90         case AF_INET6:
91                 ((struct sockaddr_in6 *)sap)->sin6_port = 0;
92                 break;
93         }
94 }
95
96 static void nlm_display_address(const struct sockaddr *sap,
97                                 char *buf, const size_t len)
98 {
99         const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
100         const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
101
102         switch (sap->sa_family) {
103         case AF_UNSPEC:
104                 snprintf(buf, len, "unspecified");
105                 break;
106         case AF_INET:
107                 snprintf(buf, len, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
108                 break;
109         case AF_INET6:
110                 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
111                         snprintf(buf, len, NIPQUAD_FMT,
112                                  NIPQUAD(sin6->sin6_addr.s6_addr32[3]));
113                 else
114                         snprintf(buf, len, NIP6_FMT, NIP6(sin6->sin6_addr));
115                 break;
116         default:
117                 snprintf(buf, len, "unsupported address family");
118                 break;
119         }
120 }
121
122 /*
123  * Common host lookup routine for server & client
124  */
125 static struct nlm_host *nlm_lookup_host(int server,
126                                         const struct sockaddr_in *sin,
127                                         int proto, u32 version,
128                                         const char *hostname,
129                                         unsigned int hostname_len,
130                                         const struct sockaddr_in *ssin)
131 {
132         struct hlist_head *chain;
133         struct hlist_node *pos;
134         struct nlm_host *host;
135         struct nsm_handle *nsm = NULL;
136
137         dprintk("lockd: nlm_lookup_host(proto=%d, vers=%u,"
138                         " my role is %s, hostname=%.*s)\n",
139                         proto, version, server ? "server" : "client",
140                         hostname_len, hostname ? hostname : "<none>");
141
142         mutex_lock(&nlm_host_mutex);
143
144         if (time_after_eq(jiffies, next_gc))
145                 nlm_gc_hosts();
146
147         /* We may keep several nlm_host objects for a peer, because each
148          * nlm_host is identified by
149          * (address, protocol, version, server/client)
150          * We could probably simplify this a little by putting all those
151          * different NLM rpc_clients into one single nlm_host object.
152          * This would allow us to have one nlm_host per address.
153          */
154         chain = &nlm_hosts[nlm_hash_address((struct sockaddr *)sin)];
155         hlist_for_each_entry(host, pos, chain, h_hash) {
156                 if (!nlm_cmp_addr(nlm_addr(host), (struct sockaddr *)sin))
157                         continue;
158
159                 /* See if we have an NSM handle for this client */
160                 if (!nsm)
161                         nsm = host->h_nsmhandle;
162
163                 if (host->h_proto != proto)
164                         continue;
165                 if (host->h_version != version)
166                         continue;
167                 if (host->h_server != server)
168                         continue;
169                 if (!nlm_cmp_addr(nlm_srcaddr(host), (struct sockaddr *)ssin))
170                         continue;
171
172                 /* Move to head of hash chain. */
173                 hlist_del(&host->h_hash);
174                 hlist_add_head(&host->h_hash, chain);
175
176                 nlm_get_host(host);
177                 dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
178                                 host->h_name, host->h_addrbuf);
179                 goto out;
180         }
181
182         /*
183          * The host wasn't in our hash table.  If we don't
184          * have an NSM handle for it yet, create one.
185          */
186         if (nsm)
187                 atomic_inc(&nsm->sm_count);
188         else {
189                 host = NULL;
190                 nsm = nsm_find(sin, hostname, hostname_len);
191                 if (!nsm) {
192                         dprintk("lockd: nlm_lookup_host failed; "
193                                 "no nsm handle\n");
194                         goto out;
195                 }
196         }
197
198         host = kzalloc(sizeof(*host), GFP_KERNEL);
199         if (!host) {
200                 nsm_release(nsm);
201                 dprintk("lockd: nlm_lookup_host failed; no memory\n");
202                 goto out;
203         }
204         host->h_name       = nsm->sm_name;
205         memcpy(nlm_addr(host), sin, sizeof(*sin));
206         host->h_addrlen = sizeof(*sin);
207         nlm_clear_port(nlm_addr(host));
208         memcpy(nlm_srcaddr(host), ssin, sizeof(*ssin));
209         host->h_version    = version;
210         host->h_proto      = proto;
211         host->h_rpcclnt    = NULL;
212         mutex_init(&host->h_mutex);
213         host->h_nextrebind = jiffies + NLM_HOST_REBIND;
214         host->h_expires    = jiffies + NLM_HOST_EXPIRE;
215         atomic_set(&host->h_count, 1);
216         init_waitqueue_head(&host->h_gracewait);
217         init_rwsem(&host->h_rwsem);
218         host->h_state      = 0;                 /* pseudo NSM state */
219         host->h_nsmstate   = 0;                 /* real NSM state */
220         host->h_nsmhandle  = nsm;
221         host->h_server     = server;
222         hlist_add_head(&host->h_hash, chain);
223         INIT_LIST_HEAD(&host->h_lockowners);
224         spin_lock_init(&host->h_lock);
225         INIT_LIST_HEAD(&host->h_granted);
226         INIT_LIST_HEAD(&host->h_reclaim);
227
228         nrhosts++;
229
230         nlm_display_address((struct sockaddr *)&host->h_addr,
231                                 host->h_addrbuf, sizeof(host->h_addrbuf));
232         nlm_display_address((struct sockaddr *)&host->h_srcaddr,
233                                 host->h_srcaddrbuf, sizeof(host->h_srcaddrbuf));
234
235         dprintk("lockd: nlm_lookup_host created host %s\n",
236                         host->h_name);
237
238 out:
239         mutex_unlock(&nlm_host_mutex);
240         return host;
241 }
242
243 /*
244  * Destroy a host
245  */
246 static void
247 nlm_destroy_host(struct nlm_host *host)
248 {
249         struct rpc_clnt *clnt;
250
251         BUG_ON(!list_empty(&host->h_lockowners));
252         BUG_ON(atomic_read(&host->h_count));
253
254         /*
255          * Release NSM handle and unmonitor host.
256          */
257         nsm_unmonitor(host);
258
259         clnt = host->h_rpcclnt;
260         if (clnt != NULL)
261                 rpc_shutdown_client(clnt);
262         kfree(host);
263 }
264
265 /*
266  * Find an NLM server handle in the cache. If there is none, create it.
267  */
268 struct nlm_host *nlmclnt_lookup_host(const struct sockaddr_in *sin,
269                                      int proto, u32 version,
270                                      const char *hostname,
271                                      unsigned int hostname_len)
272 {
273         const struct sockaddr_in source = {
274                 .sin_family     = AF_UNSPEC,
275         };
276
277         return nlm_lookup_host(0, sin, proto, version,
278                                hostname, hostname_len, &source);
279 }
280
281 /*
282  * Find an NLM client handle in the cache. If there is none, create it.
283  */
284 struct nlm_host *
285 nlmsvc_lookup_host(struct svc_rqst *rqstp,
286                         const char *hostname, unsigned int hostname_len)
287 {
288         const struct sockaddr_in source = {
289                 .sin_family     = AF_INET,
290                 .sin_addr       = rqstp->rq_daddr.addr,
291         };
292
293         return nlm_lookup_host(1, svc_addr_in(rqstp),
294                                rqstp->rq_prot, rqstp->rq_vers,
295                                hostname, hostname_len, &source);
296 }
297
298 /*
299  * Create the NLM RPC client for an NLM peer
300  */
301 struct rpc_clnt *
302 nlm_bind_host(struct nlm_host *host)
303 {
304         struct rpc_clnt *clnt;
305
306         dprintk("lockd: nlm_bind_host %s (%s), my addr=%s\n",
307                         host->h_name, host->h_addrbuf, host->h_srcaddrbuf);
308
309         /* Lock host handle */
310         mutex_lock(&host->h_mutex);
311
312         /* If we've already created an RPC client, check whether
313          * RPC rebind is required
314          */
315         if ((clnt = host->h_rpcclnt) != NULL) {
316                 if (time_after_eq(jiffies, host->h_nextrebind)) {
317                         rpc_force_rebind(clnt);
318                         host->h_nextrebind = jiffies + NLM_HOST_REBIND;
319                         dprintk("lockd: next rebind in %lu jiffies\n",
320                                         host->h_nextrebind - jiffies);
321                 }
322         } else {
323                 unsigned long increment = nlmsvc_timeout;
324                 struct rpc_timeout timeparms = {
325                         .to_initval     = increment,
326                         .to_increment   = increment,
327                         .to_maxval      = increment * 6UL,
328                         .to_retries     = 5U,
329                 };
330                 struct rpc_create_args args = {
331                         .protocol       = host->h_proto,
332                         .address        = nlm_addr(host),
333                         .addrsize       = host->h_addrlen,
334                         .saddress       = nlm_srcaddr(host),
335                         .timeout        = &timeparms,
336                         .servername     = host->h_name,
337                         .program        = &nlm_program,
338                         .version        = host->h_version,
339                         .authflavor     = RPC_AUTH_UNIX,
340                         .flags          = (RPC_CLNT_CREATE_NOPING |
341                                            RPC_CLNT_CREATE_AUTOBIND),
342                 };
343
344                 /*
345                  * lockd retries server side blocks automatically so we want
346                  * those to be soft RPC calls. Client side calls need to be
347                  * hard RPC tasks.
348                  */
349                 if (!host->h_server)
350                         args.flags |= RPC_CLNT_CREATE_HARDRTRY;
351
352                 clnt = rpc_create(&args);
353                 if (!IS_ERR(clnt))
354                         host->h_rpcclnt = clnt;
355                 else {
356                         printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
357                         clnt = NULL;
358                 }
359         }
360
361         mutex_unlock(&host->h_mutex);
362         return clnt;
363 }
364
365 /*
366  * Force a portmap lookup of the remote lockd port
367  */
368 void
369 nlm_rebind_host(struct nlm_host *host)
370 {
371         dprintk("lockd: rebind host %s\n", host->h_name);
372         if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
373                 rpc_force_rebind(host->h_rpcclnt);
374                 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
375         }
376 }
377
378 /*
379  * Increment NLM host count
380  */
381 struct nlm_host * nlm_get_host(struct nlm_host *host)
382 {
383         if (host) {
384                 dprintk("lockd: get host %s\n", host->h_name);
385                 atomic_inc(&host->h_count);
386                 host->h_expires = jiffies + NLM_HOST_EXPIRE;
387         }
388         return host;
389 }
390
391 /*
392  * Release NLM host after use
393  */
394 void nlm_release_host(struct nlm_host *host)
395 {
396         if (host != NULL) {
397                 dprintk("lockd: release host %s\n", host->h_name);
398                 BUG_ON(atomic_read(&host->h_count) < 0);
399                 if (atomic_dec_and_test(&host->h_count)) {
400                         BUG_ON(!list_empty(&host->h_lockowners));
401                         BUG_ON(!list_empty(&host->h_granted));
402                         BUG_ON(!list_empty(&host->h_reclaim));
403                 }
404         }
405 }
406
407 /*
408  * We were notified that the host indicated by address &sin
409  * has rebooted.
410  * Release all resources held by that peer.
411  */
412 void nlm_host_rebooted(const struct sockaddr_in *sin,
413                                 const char *hostname,
414                                 unsigned int hostname_len,
415                                 u32 new_state)
416 {
417         struct hlist_head *chain;
418         struct hlist_node *pos;
419         struct nsm_handle *nsm;
420         struct nlm_host *host;
421
422         /* Find the NSM handle for this peer */
423         nsm = __nsm_find(sin, hostname, hostname_len, 0);
424         if (nsm == NULL) {
425                 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
426                                 hostname_len, hostname);
427                 return;
428         }
429
430         dprintk("lockd: nlm_host_rebooted(%.*s, %s)\n",
431                         hostname_len, hostname, nsm->sm_addrbuf);
432
433         /* When reclaiming locks on this peer, make sure that
434          * we set up a new notification */
435         nsm->sm_monitored = 0;
436
437         /* Mark all hosts tied to this NSM state as having rebooted.
438          * We run the loop repeatedly, because we drop the host table
439          * lock for this.
440          * To avoid processing a host several times, we match the nsmstate.
441          */
442 again:  mutex_lock(&nlm_host_mutex);
443         for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
444                 hlist_for_each_entry(host, pos, chain, h_hash) {
445                         if (host->h_nsmhandle == nsm
446                          && host->h_nsmstate != new_state) {
447                                 host->h_nsmstate = new_state;
448                                 host->h_state++;
449
450                                 nlm_get_host(host);
451                                 mutex_unlock(&nlm_host_mutex);
452
453                                 if (host->h_server) {
454                                         /* We're server for this guy, just ditch
455                                          * all the locks he held. */
456                                         nlmsvc_free_host_resources(host);
457                                 } else {
458                                         /* He's the server, initiate lock recovery. */
459                                         nlmclnt_recovery(host);
460                                 }
461
462                                 nlm_release_host(host);
463                                 goto again;
464                         }
465                 }
466         }
467
468         mutex_unlock(&nlm_host_mutex);
469 }
470
471 /*
472  * Shut down the hosts module.
473  * Note that this routine is called only at server shutdown time.
474  */
475 void
476 nlm_shutdown_hosts(void)
477 {
478         struct hlist_head *chain;
479         struct hlist_node *pos;
480         struct nlm_host *host;
481
482         dprintk("lockd: shutting down host module\n");
483         mutex_lock(&nlm_host_mutex);
484
485         /* First, make all hosts eligible for gc */
486         dprintk("lockd: nuking all hosts...\n");
487         for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
488                 hlist_for_each_entry(host, pos, chain, h_hash) {
489                         host->h_expires = jiffies - 1;
490                         if (host->h_rpcclnt) {
491                                 rpc_shutdown_client(host->h_rpcclnt);
492                                 host->h_rpcclnt = NULL;
493                         }
494                 }
495         }
496
497         /* Then, perform a garbage collection pass */
498         nlm_gc_hosts();
499         mutex_unlock(&nlm_host_mutex);
500
501         /* complain if any hosts are left */
502         if (nrhosts) {
503                 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
504                 dprintk("lockd: %d hosts left:\n", nrhosts);
505                 for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
506                         hlist_for_each_entry(host, pos, chain, h_hash) {
507                                 dprintk("       %s (cnt %d use %d exp %ld)\n",
508                                         host->h_name, atomic_read(&host->h_count),
509                                         host->h_inuse, host->h_expires);
510                         }
511                 }
512         }
513 }
514
515 /*
516  * Garbage collect any unused NLM hosts.
517  * This GC combines reference counting for async operations with
518  * mark & sweep for resources held by remote clients.
519  */
520 static void
521 nlm_gc_hosts(void)
522 {
523         struct hlist_head *chain;
524         struct hlist_node *pos, *next;
525         struct nlm_host *host;
526
527         dprintk("lockd: host garbage collection\n");
528         for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
529                 hlist_for_each_entry(host, pos, chain, h_hash)
530                         host->h_inuse = 0;
531         }
532
533         /* Mark all hosts that hold locks, blocks or shares */
534         nlmsvc_mark_resources();
535
536         for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
537                 hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
538                         if (atomic_read(&host->h_count) || host->h_inuse
539                          || time_before(jiffies, host->h_expires)) {
540                                 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
541                                         host->h_name, atomic_read(&host->h_count),
542                                         host->h_inuse, host->h_expires);
543                                 continue;
544                         }
545                         dprintk("lockd: delete host %s\n", host->h_name);
546                         hlist_del_init(&host->h_hash);
547
548                         nlm_destroy_host(host);
549                         nrhosts--;
550                 }
551         }
552
553         next_gc = jiffies + NLM_HOST_COLLECT;
554 }
555
556
557 /*
558  * Manage NSM handles
559  */
560 static LIST_HEAD(nsm_handles);
561 static DEFINE_SPINLOCK(nsm_lock);
562
563 static struct nsm_handle *
564 __nsm_find(const struct sockaddr_in *sin,
565                 const char *hostname, unsigned int hostname_len,
566                 int create)
567 {
568         struct nsm_handle *nsm = NULL;
569         struct nsm_handle *pos;
570
571         if (!sin)
572                 return NULL;
573
574         if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
575                 if (printk_ratelimit()) {
576                         printk(KERN_WARNING "Invalid hostname \"%.*s\" "
577                                             "in NFS lock request\n",
578                                 hostname_len, hostname);
579                 }
580                 return NULL;
581         }
582
583 retry:
584         spin_lock(&nsm_lock);
585         list_for_each_entry(pos, &nsm_handles, sm_link) {
586
587                 if (hostname && nsm_use_hostnames) {
588                         if (strlen(pos->sm_name) != hostname_len
589                          || memcmp(pos->sm_name, hostname, hostname_len))
590                                 continue;
591                 } else if (!nlm_cmp_addr(nsm_addr(pos), (struct sockaddr *)sin))
592                         continue;
593                 atomic_inc(&pos->sm_count);
594                 kfree(nsm);
595                 nsm = pos;
596                 goto found;
597         }
598         if (nsm) {
599                 list_add(&nsm->sm_link, &nsm_handles);
600                 goto found;
601         }
602         spin_unlock(&nsm_lock);
603
604         if (!create)
605                 return NULL;
606
607         nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
608         if (nsm == NULL)
609                 return NULL;
610
611         memcpy(nsm_addr(nsm), sin, sizeof(*sin));
612         nsm->sm_addrlen = sizeof(*sin);
613         nsm->sm_name = (char *) (nsm + 1);
614         memcpy(nsm->sm_name, hostname, hostname_len);
615         nsm->sm_name[hostname_len] = '\0';
616         nlm_display_address((struct sockaddr *)&nsm->sm_addr,
617                                 nsm->sm_addrbuf, sizeof(nsm->sm_addrbuf));
618         atomic_set(&nsm->sm_count, 1);
619         goto retry;
620
621 found:
622         spin_unlock(&nsm_lock);
623         return nsm;
624 }
625
626 static struct nsm_handle *
627 nsm_find(const struct sockaddr_in *sin, const char *hostname,
628          unsigned int hostname_len)
629 {
630         return __nsm_find(sin, hostname, hostname_len, 1);
631 }
632
633 /*
634  * Release an NSM handle
635  */
636 void
637 nsm_release(struct nsm_handle *nsm)
638 {
639         if (!nsm)
640                 return;
641         if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
642                 list_del(&nsm->sm_link);
643                 spin_unlock(&nsm_lock);
644                 kfree(nsm);
645         }
646 }