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