]> err.no Git - linux-2.6/blob - fs/lockd/host.c
[PATCH] knfsd: lockd: introduce nsm_handle
[linux-2.6] / 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 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 nlm_host *host, **hp;
73         struct nsm_handle *nsm = NULL;
74         int             hash;
75
76         dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
77                         NIPQUAD(sin->sin_addr.s_addr), proto, version,
78                         server? "server" : "client",
79                         hostname_len,
80                         hostname? hostname : "<none>");
81
82
83         hash = NLM_ADDRHASH(sin->sin_addr.s_addr);
84
85         /* Lock hash table */
86         mutex_lock(&nlm_host_mutex);
87
88         if (time_after_eq(jiffies, next_gc))
89                 nlm_gc_hosts();
90
91         /* We may keep several nlm_host objects for a peer, because each
92          * nlm_host is identified by
93          * (address, protocol, version, server/client)
94          * We could probably simplify this a little by putting all those
95          * different NLM rpc_clients into one single nlm_host object.
96          * This would allow us to have one nlm_host per address.
97          */
98         for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
99                 if (!nlm_cmp_addr(&host->h_addr, sin))
100                         continue;
101
102                 /* See if we have an NSM handle for this client */
103                 if (!nsm && (nsm = host->h_nsmhandle) != 0)
104                         atomic_inc(&nsm->sm_count);
105
106                 if (host->h_proto != proto)
107                         continue;
108                 if (host->h_version != version)
109                         continue;
110                 if (host->h_server != server)
111                         continue;
112
113                 {
114                         if (hp != nlm_hosts + hash) {
115                                 *hp = host->h_next;
116                                 host->h_next = nlm_hosts[hash];
117                                 nlm_hosts[hash] = host;
118                         }
119                         nlm_get_host(host);
120                         mutex_unlock(&nlm_host_mutex);
121                         return host;
122                 }
123         }
124
125         /* Sadly, the host isn't in our hash table yet. See if
126          * we have an NSM handle for it. If not, create one.
127          */
128         if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len)))
129                 goto out;
130
131         host = kzalloc(sizeof(*host), GFP_KERNEL);
132         if (!host) {
133                 nsm_release(nsm);
134                 goto out;
135         }
136         host->h_name       = nsm->sm_name;
137         host->h_addr       = *sin;
138         host->h_addr.sin_port = 0;      /* ouch! */
139         host->h_version    = version;
140         host->h_proto      = proto;
141         host->h_rpcclnt    = NULL;
142         mutex_init(&host->h_mutex);
143         host->h_nextrebind = jiffies + NLM_HOST_REBIND;
144         host->h_expires    = jiffies + NLM_HOST_EXPIRE;
145         atomic_set(&host->h_count, 1);
146         init_waitqueue_head(&host->h_gracewait);
147         init_rwsem(&host->h_rwsem);
148         host->h_state      = 0;                 /* pseudo NSM state */
149         host->h_nsmstate   = 0;                 /* real NSM state */
150         host->h_nsmhandle  = nsm;
151         host->h_server     = server;
152         host->h_next       = nlm_hosts[hash];
153         nlm_hosts[hash]    = host;
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 struct nlm_host *
168 nlm_find_client(void)
169 {
170         /* find a nlm_host for a client for which h_killed == 0.
171          * and return it
172          */
173         int hash;
174         mutex_lock(&nlm_host_mutex);
175         for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
176                 struct nlm_host *host, **hp;
177                 for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
178                         if (host->h_server &&
179                             host->h_killed == 0) {
180                                 nlm_get_host(host);
181                                 mutex_unlock(&nlm_host_mutex);
182                                 return host;
183                         }
184                 }
185         }
186         mutex_unlock(&nlm_host_mutex);
187         return NULL;
188 }
189
190                                 
191 /*
192  * Create the NLM RPC client for an NLM peer
193  */
194 struct rpc_clnt *
195 nlm_bind_host(struct nlm_host *host)
196 {
197         struct rpc_clnt *clnt;
198
199         dprintk("lockd: nlm_bind_host(%08x)\n",
200                         (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
201
202         /* Lock host handle */
203         mutex_lock(&host->h_mutex);
204
205         /* If we've already created an RPC client, check whether
206          * RPC rebind is required
207          */
208         if ((clnt = host->h_rpcclnt) != NULL) {
209                 if (time_after_eq(jiffies, host->h_nextrebind)) {
210                         rpc_force_rebind(clnt);
211                         host->h_nextrebind = jiffies + NLM_HOST_REBIND;
212                         dprintk("lockd: next rebind in %ld jiffies\n",
213                                         host->h_nextrebind - jiffies);
214                 }
215         } else {
216                 unsigned long increment = nlmsvc_timeout * HZ;
217                 struct rpc_timeout timeparms = {
218                         .to_initval     = increment,
219                         .to_increment   = increment,
220                         .to_maxval      = increment * 6UL,
221                         .to_retries     = 5U,
222                 };
223                 struct rpc_create_args args = {
224                         .protocol       = host->h_proto,
225                         .address        = (struct sockaddr *)&host->h_addr,
226                         .addrsize       = sizeof(host->h_addr),
227                         .timeout        = &timeparms,
228                         .servername     = host->h_name,
229                         .program        = &nlm_program,
230                         .version        = host->h_version,
231                         .authflavor     = RPC_AUTH_UNIX,
232                         .flags          = (RPC_CLNT_CREATE_HARDRTRY |
233                                            RPC_CLNT_CREATE_AUTOBIND),
234                 };
235
236                 clnt = rpc_create(&args);
237                 if (!IS_ERR(clnt))
238                         host->h_rpcclnt = clnt;
239                 else {
240                         printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
241                         clnt = NULL;
242                 }
243         }
244
245         mutex_unlock(&host->h_mutex);
246         return clnt;
247 }
248
249 /*
250  * Force a portmap lookup of the remote lockd port
251  */
252 void
253 nlm_rebind_host(struct nlm_host *host)
254 {
255         dprintk("lockd: rebind host %s\n", host->h_name);
256         if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
257                 rpc_force_rebind(host->h_rpcclnt);
258                 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
259         }
260 }
261
262 /*
263  * Increment NLM host count
264  */
265 struct nlm_host * nlm_get_host(struct nlm_host *host)
266 {
267         if (host) {
268                 dprintk("lockd: get host %s\n", host->h_name);
269                 atomic_inc(&host->h_count);
270                 host->h_expires = jiffies + NLM_HOST_EXPIRE;
271         }
272         return host;
273 }
274
275 /*
276  * Release NLM host after use
277  */
278 void nlm_release_host(struct nlm_host *host)
279 {
280         if (host != NULL) {
281                 dprintk("lockd: release host %s\n", host->h_name);
282                 BUG_ON(atomic_read(&host->h_count) < 0);
283                 if (atomic_dec_and_test(&host->h_count)) {
284                         BUG_ON(!list_empty(&host->h_lockowners));
285                         BUG_ON(!list_empty(&host->h_granted));
286                         BUG_ON(!list_empty(&host->h_reclaim));
287                 }
288         }
289 }
290
291 /*
292  * We were notified that the host indicated by address &sin
293  * has rebooted.
294  * Release all resources held by that peer.
295  */
296 void nlm_host_rebooted(const struct sockaddr_in *sin, const struct nlm_reboot *argp)
297 {
298         struct nlm_host *host;
299         int server;
300
301         /* Obtain the host pointer for this NFS server and try to
302          * reclaim all locks we hold on this server.
303          */
304         server = (argp->proto & 1)? 1 : 0;
305         host = nlm_lookup_host(server, sin, argp->proto >> 1, argp->vers,
306                         argp->mon, argp->len);
307         if (host == NULL)
308                 return;
309
310         if (server == 0) {
311                 /* We are client, he's the server: try to reclaim all locks. */
312                 nlmclnt_recovery(host, argp->state);
313         } else {
314                 /* He's the client, we're the server: delete all locks held by the client */
315                 nlmsvc_free_host_resources(host);
316         }
317         nlm_release_host(host);
318 }
319
320 /*
321  * Shut down the hosts module.
322  * Note that this routine is called only at server shutdown time.
323  */
324 void
325 nlm_shutdown_hosts(void)
326 {
327         struct nlm_host *host;
328         int             i;
329
330         dprintk("lockd: shutting down host module\n");
331         mutex_lock(&nlm_host_mutex);
332
333         /* First, make all hosts eligible for gc */
334         dprintk("lockd: nuking all hosts...\n");
335         for (i = 0; i < NLM_HOST_NRHASH; i++) {
336                 for (host = nlm_hosts[i]; host; host = host->h_next)
337                         host->h_expires = jiffies - 1;
338         }
339
340         /* Then, perform a garbage collection pass */
341         nlm_gc_hosts();
342         mutex_unlock(&nlm_host_mutex);
343
344         /* complain if any hosts are left */
345         if (nrhosts) {
346                 printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
347                 dprintk("lockd: %d hosts left:\n", nrhosts);
348                 for (i = 0; i < NLM_HOST_NRHASH; i++) {
349                         for (host = nlm_hosts[i]; host; host = host->h_next) {
350                                 dprintk("       %s (cnt %d use %d exp %ld)\n",
351                                         host->h_name, atomic_read(&host->h_count),
352                                         host->h_inuse, host->h_expires);
353                         }
354                 }
355         }
356 }
357
358 /*
359  * Garbage collect any unused NLM hosts.
360  * This GC combines reference counting for async operations with
361  * mark & sweep for resources held by remote clients.
362  */
363 static void
364 nlm_gc_hosts(void)
365 {
366         struct nlm_host **q, *host;
367         struct rpc_clnt *clnt;
368         int             i;
369
370         dprintk("lockd: host garbage collection\n");
371         for (i = 0; i < NLM_HOST_NRHASH; i++) {
372                 for (host = nlm_hosts[i]; host; host = host->h_next)
373                         host->h_inuse = 0;
374         }
375
376         /* Mark all hosts that hold locks, blocks or shares */
377         nlmsvc_mark_resources();
378
379         for (i = 0; i < NLM_HOST_NRHASH; i++) {
380                 q = &nlm_hosts[i];
381                 while ((host = *q) != NULL) {
382                         if (atomic_read(&host->h_count) || host->h_inuse
383                          || time_before(jiffies, host->h_expires)) {
384                                 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
385                                         host->h_name, atomic_read(&host->h_count),
386                                         host->h_inuse, host->h_expires);
387                                 q = &host->h_next;
388                                 continue;
389                         }
390                         dprintk("lockd: delete host %s\n", host->h_name);
391                         *q = host->h_next;
392
393                         /*
394                          * Unmonitor unless host was invalidated (i.e. lockd restarted)
395                          */
396                         nsm_unmonitor(host);
397
398                         if ((clnt = host->h_rpcclnt) != NULL) {
399                                 if (atomic_read(&clnt->cl_users)) {
400                                         printk(KERN_WARNING
401                                                 "lockd: active RPC handle\n");
402                                         clnt->cl_dead = 1;
403                                 } else {
404                                         rpc_destroy_client(host->h_rpcclnt);
405                                 }
406                         }
407                         kfree(host);
408                         nrhosts--;
409                 }
410         }
411
412         next_gc = jiffies + NLM_HOST_COLLECT;
413 }
414
415
416 /*
417  * Manage NSM handles
418  */
419 static LIST_HEAD(nsm_handles);
420 static DECLARE_MUTEX(nsm_sema);
421
422 static struct nsm_handle *
423 __nsm_find(const struct sockaddr_in *sin,
424                 const char *hostname, int hostname_len,
425                 int create)
426 {
427         struct nsm_handle *nsm = NULL;
428         struct list_head *pos;
429
430         if (!sin)
431                 return NULL;
432
433         if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
434                 if (printk_ratelimit()) {
435                         printk(KERN_WARNING "Invalid hostname \"%.*s\" "
436                                             "in NFS lock request\n",
437                                 hostname_len, hostname);
438                 }
439                 return NULL;
440         }
441
442         down(&nsm_sema);
443         list_for_each(pos, &nsm_handles) {
444                 nsm = list_entry(pos, struct nsm_handle, sm_link);
445
446                 if (!nlm_cmp_addr(&nsm->sm_addr, sin))
447                         continue;
448                 atomic_inc(&nsm->sm_count);
449                 goto out;
450         }
451
452         if (!create) {
453                 nsm = NULL;
454                 goto out;
455         }
456
457         nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
458         if (nsm != NULL) {
459                 nsm->sm_addr = *sin;
460                 nsm->sm_name = (char *) (nsm + 1);
461                 memcpy(nsm->sm_name, hostname, hostname_len);
462                 nsm->sm_name[hostname_len] = '\0';
463                 atomic_set(&nsm->sm_count, 1);
464
465                 list_add(&nsm->sm_link, &nsm_handles);
466         }
467
468 out:    up(&nsm_sema);
469         return nsm;
470 }
471
472 struct nsm_handle *
473 nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
474 {
475         return __nsm_find(sin, hostname, hostname_len, 1);
476 }
477
478 /*
479  * Release an NSM handle
480  */
481 void
482 nsm_release(struct nsm_handle *nsm)
483 {
484         if (!nsm)
485                 return;
486         if (atomic_dec_and_test(&nsm->sm_count)) {
487                 down(&nsm_sema);
488                 if (atomic_read(&nsm->sm_count) == 0) {
489                         list_del(&nsm->sm_link);
490                         kfree(nsm);
491                 }
492                 up(&nsm_sema);
493         }
494 }