[PATCH] knfsd: when looking up a lockd host, pass hostname & length
[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/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 nlm_host *        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
38 /*
39  * Find an NLM server handle in the cache. If there is none, create it.
40  */
41 struct nlm_host *
42 nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
43                         const char *hostname, int hostname_len)
44 {
45         return nlm_lookup_host(0, sin, proto, version,
46                                hostname, hostname_len);
47 }
48
49 /*
50  * Find an NLM client handle in the cache. If there is none, create it.
51  */
52 struct nlm_host *
53 nlmsvc_lookup_host(struct svc_rqst *rqstp,
54                         const char *hostname, int hostname_len)
55 {
56         return nlm_lookup_host(1, &rqstp->rq_addr,
57                                rqstp->rq_prot, rqstp->rq_vers,
58                                hostname, hostname_len);
59 }
60
61 /*
62  * Common host lookup routine for server & client
63  */
64 struct nlm_host *
65 nlm_lookup_host(int server, const struct sockaddr_in *sin,
66                                         int proto, int version,
67                                         const char *hostname,
68                                         int hostname_len)
69 {
70         struct nlm_host *host, **hp;
71         u32             addr;
72         int             hash;
73
74         dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
75                         NIPQUAD(sin->sin_addr.s_addr), proto, version,
76                         server? "server" : "client",
77                         hostname_len,
78                         hostname? hostname : "<none>");
79
80
81         hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
82
83         /* Lock hash table */
84         mutex_lock(&nlm_host_mutex);
85
86         if (time_after_eq(jiffies, next_gc))
87                 nlm_gc_hosts();
88
89         for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
90                 if (host->h_proto != proto)
91                         continue;
92                 if (host->h_version != version)
93                         continue;
94                 if (host->h_server != server)
95                         continue;
96
97                 if (nlm_cmp_addr(&host->h_addr, sin)) {
98                         if (hp != nlm_hosts + hash) {
99                                 *hp = host->h_next;
100                                 host->h_next = nlm_hosts[hash];
101                                 nlm_hosts[hash] = host;
102                         }
103                         nlm_get_host(host);
104                         mutex_unlock(&nlm_host_mutex);
105                         return host;
106                 }
107         }
108
109         /* Ooops, no host found, create it */
110         dprintk("lockd: creating host entry\n");
111
112         host = kzalloc(sizeof(*host), GFP_KERNEL);
113         if (!host)
114                 goto nohost;
115
116         addr = sin->sin_addr.s_addr;
117         sprintf(host->h_name, "%u.%u.%u.%u", NIPQUAD(addr));
118
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_server     = server;
133         host->h_next       = nlm_hosts[hash];
134         nlm_hosts[hash]    = host;
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 nohost:
144         mutex_unlock(&nlm_host_mutex);
145         return host;
146 }
147
148 struct nlm_host *
149 nlm_find_client(void)
150 {
151         /* find a nlm_host for a client for which h_killed == 0.
152          * and return it
153          */
154         int hash;
155         mutex_lock(&nlm_host_mutex);
156         for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
157                 struct nlm_host *host, **hp;
158                 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
159                         if (host->h_server &&
160                             host->h_killed == 0) {
161                                 nlm_get_host(host);
162                                 mutex_unlock(&nlm_host_mutex);
163                                 return host;
164                         }
165                 }
166         }
167         mutex_unlock(&nlm_host_mutex);
168         return NULL;
169 }
170
171                                 
172 /*
173  * Create the NLM RPC client for an NLM peer
174  */
175 struct rpc_clnt *
176 nlm_bind_host(struct nlm_host *host)
177 {
178         struct rpc_clnt *clnt;
179
180         dprintk("lockd: nlm_bind_host(%08x)\n",
181                         (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
182
183         /* Lock host handle */
184         mutex_lock(&host->h_mutex);
185
186         /* If we've already created an RPC client, check whether
187          * RPC rebind is required
188          */
189         if ((clnt = host->h_rpcclnt) != NULL) {
190                 if (time_after_eq(jiffies, host->h_nextrebind)) {
191                         rpc_force_rebind(clnt);
192                         host->h_nextrebind = jiffies + NLM_HOST_REBIND;
193                         dprintk("lockd: next rebind in %ld jiffies\n",
194                                         host->h_nextrebind - jiffies);
195                 }
196         } else {
197                 unsigned long increment = nlmsvc_timeout * HZ;
198                 struct rpc_timeout timeparms = {
199                         .to_initval     = increment,
200                         .to_increment   = increment,
201                         .to_maxval      = increment * 6UL,
202                         .to_retries     = 5U,
203                 };
204                 struct rpc_create_args args = {
205                         .protocol       = host->h_proto,
206                         .address        = (struct sockaddr *)&host->h_addr,
207                         .addrsize       = sizeof(host->h_addr),
208                         .timeout        = &timeparms,
209                         .servername     = host->h_name,
210                         .program        = &nlm_program,
211                         .version        = host->h_version,
212                         .authflavor     = RPC_AUTH_UNIX,
213                         .flags          = (RPC_CLNT_CREATE_HARDRTRY |
214                                            RPC_CLNT_CREATE_AUTOBIND),
215                 };
216
217                 clnt = rpc_create(&args);
218                 if (!IS_ERR(clnt))
219                         host->h_rpcclnt = clnt;
220                 else {
221                         printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
222                         clnt = NULL;
223                 }
224         }
225
226         mutex_unlock(&host->h_mutex);
227         return clnt;
228 }
229
230 /*
231  * Force a portmap lookup of the remote lockd port
232  */
233 void
234 nlm_rebind_host(struct nlm_host *host)
235 {
236         dprintk("lockd: rebind host %s\n", host->h_name);
237         if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
238                 rpc_force_rebind(host->h_rpcclnt);
239                 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
240         }
241 }
242
243 /*
244  * Increment NLM host count
245  */
246 struct nlm_host * nlm_get_host(struct nlm_host *host)
247 {
248         if (host) {
249                 dprintk("lockd: get host %s\n", host->h_name);
250                 atomic_inc(&host->h_count);
251                 host->h_expires = jiffies + NLM_HOST_EXPIRE;
252         }
253         return host;
254 }
255
256 /*
257  * Release NLM host after use
258  */
259 void nlm_release_host(struct nlm_host *host)
260 {
261         if (host != NULL) {
262                 dprintk("lockd: release host %s\n", host->h_name);
263                 BUG_ON(atomic_read(&host->h_count) < 0);
264                 if (atomic_dec_and_test(&host->h_count)) {
265                         BUG_ON(!list_empty(&host->h_lockowners));
266                         BUG_ON(!list_empty(&host->h_granted));
267                         BUG_ON(!list_empty(&host->h_reclaim));
268                 }
269         }
270 }
271
272 /*
273  * We were notified that the host indicated by address &sin
274  * has rebooted.
275  * Release all resources held by that peer.
276  */
277 void nlm_host_rebooted(const struct sockaddr_in *sin, const struct nlm_reboot *argp)
278 {
279         struct nlm_host *host;
280         int server;
281
282         /* Obtain the host pointer for this NFS server and try to
283          * reclaim all locks we hold on this server.
284          */
285         server = (argp->proto & 1)? 1 : 0;
286         host = nlm_lookup_host(server, sin, argp->proto >> 1, argp->vers,
287                         argp->mon, argp->len);
288         if (host == NULL)
289                 return;
290
291         if (server == 0) {
292                 /* We are client, he's the server: try to reclaim all locks. */
293                 nlmclnt_recovery(host, argp->state);
294         } else {
295                 /* He's the client, we're the server: delete all locks held by the client */
296                 nlmsvc_free_host_resources(host);
297         }
298         nlm_release_host(host);
299 }
300
301 /*
302  * Shut down the hosts module.
303  * Note that this routine is called only at server shutdown time.
304  */
305 void
306 nlm_shutdown_hosts(void)
307 {
308         struct nlm_host *host;
309         int             i;
310
311         dprintk("lockd: shutting down host module\n");
312         mutex_lock(&nlm_host_mutex);
313
314         /* First, make all hosts eligible for gc */
315         dprintk("lockd: nuking all hosts...\n");
316         for (i = 0; i < NLM_HOST_NRHASH; i++) {
317                 for (host = nlm_hosts[i]; host; host = host->h_next)
318                         host->h_expires = jiffies - 1;
319         }
320
321         /* Then, perform a garbage collection pass */
322         nlm_gc_hosts();
323         mutex_unlock(&nlm_host_mutex);
324
325         /* complain if any hosts are left */
326         if (nrhosts) {
327                 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
328                 dprintk("lockd: %d hosts left:\n", nrhosts);
329                 for (i = 0; i < NLM_HOST_NRHASH; i++) {
330                         for (host = nlm_hosts[i]; host; host = host->h_next) {
331                                 dprintk("       %s (cnt %d use %d exp %ld)\n",
332                                         host->h_name, atomic_read(&host->h_count),
333                                         host->h_inuse, host->h_expires);
334                         }
335                 }
336         }
337 }
338
339 /*
340  * Garbage collect any unused NLM hosts.
341  * This GC combines reference counting for async operations with
342  * mark & sweep for resources held by remote clients.
343  */
344 static void
345 nlm_gc_hosts(void)
346 {
347         struct nlm_host **q, *host;
348         struct rpc_clnt *clnt;
349         int             i;
350
351         dprintk("lockd: host garbage collection\n");
352         for (i = 0; i < NLM_HOST_NRHASH; i++) {
353                 for (host = nlm_hosts[i]; host; host = host->h_next)
354                         host->h_inuse = 0;
355         }
356
357         /* Mark all hosts that hold locks, blocks or shares */
358         nlmsvc_mark_resources();
359
360         for (i = 0; i < NLM_HOST_NRHASH; i++) {
361                 q = &nlm_hosts[i];
362                 while ((host = *q) != NULL) {
363                         if (atomic_read(&host->h_count) || host->h_inuse
364                          || time_before(jiffies, host->h_expires)) {
365                                 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
366                                         host->h_name, atomic_read(&host->h_count),
367                                         host->h_inuse, host->h_expires);
368                                 q = &host->h_next;
369                                 continue;
370                         }
371                         dprintk("lockd: delete host %s\n", host->h_name);
372                         *q = host->h_next;
373
374                         /*
375                          * Unmonitor unless host was invalidated (i.e. lockd restarted)
376                          */
377                         nsm_unmonitor(host);
378
379                         if ((clnt = host->h_rpcclnt) != NULL) {
380                                 if (atomic_read(&clnt->cl_users)) {
381                                         printk(KERN_WARNING
382                                                 "lockd: active RPC handle\n");
383                                         clnt->cl_dead = 1;
384                                 } else {
385                                         rpc_destroy_client(host->h_rpcclnt);
386                                 }
387                         }
388                         kfree(host);
389                         nrhosts--;
390                 }
391         }
392
393         next_gc = jiffies + NLM_HOST_COLLECT;
394 }
395