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