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