2 * linux/fs/lockd/host.c
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.
8 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
11 #include <linux/types.h>
12 #include <linux/slab.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>
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)
29 static struct hlist_head nlm_hosts
[NLM_HOST_NRHASH
];
30 static unsigned long next_gc
;
32 static DEFINE_MUTEX(nlm_host_mutex
);
35 static void nlm_gc_hosts(void);
36 static struct nsm_handle
* __nsm_find(const struct sockaddr_in
*,
37 const char *, unsigned int, int);
38 static struct nsm_handle
* nsm_find(const struct sockaddr_in
*sin
,
40 unsigned int hostname_len
);
43 * Common host lookup routine for server & client
45 static struct nlm_host
*nlm_lookup_host(int server
,
46 const struct sockaddr_in
*sin
,
47 int proto
, u32 version
,
49 unsigned int hostname_len
,
50 const struct sockaddr_in
*ssin
)
52 struct hlist_head
*chain
;
53 struct hlist_node
*pos
;
54 struct nlm_host
*host
;
55 struct nsm_handle
*nsm
= NULL
;
58 dprintk("lockd: nlm_lookup_host("NIPQUAD_FMT
"->"NIPQUAD_FMT
59 ", p=%d, v=%u, my role=%s, name=%.*s)\n",
60 NIPQUAD(ssin
->sin_addr
.s_addr
),
61 NIPQUAD(sin
->sin_addr
.s_addr
), proto
, version
,
62 server
? "server" : "client",
64 hostname
? hostname
: "<none>");
67 hash
= NLM_ADDRHASH(sin
->sin_addr
.s_addr
);
70 mutex_lock(&nlm_host_mutex
);
72 if (time_after_eq(jiffies
, next_gc
))
75 /* We may keep several nlm_host objects for a peer, because each
76 * nlm_host is identified by
77 * (address, protocol, version, server/client)
78 * We could probably simplify this a little by putting all those
79 * different NLM rpc_clients into one single nlm_host object.
80 * This would allow us to have one nlm_host per address.
82 chain
= &nlm_hosts
[hash
];
83 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
84 if (!nlm_cmp_addr(&host
->h_addr
, sin
))
87 /* See if we have an NSM handle for this client */
89 nsm
= host
->h_nsmhandle
;
91 if (host
->h_proto
!= proto
)
93 if (host
->h_version
!= version
)
95 if (host
->h_server
!= server
)
97 if (!nlm_cmp_addr(&host
->h_saddr
, ssin
))
100 /* Move to head of hash chain. */
101 hlist_del(&host
->h_hash
);
102 hlist_add_head(&host
->h_hash
, chain
);
108 atomic_inc(&nsm
->sm_count
);
112 /* Sadly, the host isn't in our hash table yet. See if
113 * we have an NSM handle for it. If not, create one.
115 if (!nsm
&& !(nsm
= nsm_find(sin
, hostname
, hostname_len
)))
118 host
= kzalloc(sizeof(*host
), GFP_KERNEL
);
123 host
->h_name
= nsm
->sm_name
;
125 host
->h_addr
.sin_port
= 0; /* ouch! */
126 host
->h_saddr
= *ssin
;
127 host
->h_version
= version
;
128 host
->h_proto
= proto
;
129 host
->h_rpcclnt
= NULL
;
130 mutex_init(&host
->h_mutex
);
131 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
132 host
->h_expires
= jiffies
+ NLM_HOST_EXPIRE
;
133 atomic_set(&host
->h_count
, 1);
134 init_waitqueue_head(&host
->h_gracewait
);
135 init_rwsem(&host
->h_rwsem
);
136 host
->h_state
= 0; /* pseudo NSM state */
137 host
->h_nsmstate
= 0; /* real NSM state */
138 host
->h_nsmhandle
= nsm
;
139 host
->h_server
= server
;
140 hlist_add_head(&host
->h_hash
, chain
);
141 INIT_LIST_HEAD(&host
->h_lockowners
);
142 spin_lock_init(&host
->h_lock
);
143 INIT_LIST_HEAD(&host
->h_granted
);
144 INIT_LIST_HEAD(&host
->h_reclaim
);
146 if (++nrhosts
> NLM_HOST_MAX
)
150 mutex_unlock(&nlm_host_mutex
);
158 nlm_destroy_host(struct nlm_host
*host
)
160 struct rpc_clnt
*clnt
;
162 BUG_ON(!list_empty(&host
->h_lockowners
));
163 BUG_ON(atomic_read(&host
->h_count
));
166 * Release NSM handle and unmonitor host.
170 clnt
= host
->h_rpcclnt
;
172 rpc_shutdown_client(clnt
);
177 * Find an NLM server handle in the cache. If there is none, create it.
179 struct nlm_host
*nlmclnt_lookup_host(const struct sockaddr_in
*sin
,
180 int proto
, u32 version
,
181 const char *hostname
,
182 unsigned int hostname_len
)
184 struct sockaddr_in ssin
= {0};
186 return nlm_lookup_host(0, sin
, proto
, version
,
187 hostname
, hostname_len
, &ssin
);
191 * Find an NLM client handle in the cache. If there is none, create it.
194 nlmsvc_lookup_host(struct svc_rqst
*rqstp
,
195 const char *hostname
, unsigned int hostname_len
)
197 struct sockaddr_in ssin
= {0};
199 ssin
.sin_addr
= rqstp
->rq_daddr
.addr
;
200 return nlm_lookup_host(1, svc_addr_in(rqstp
),
201 rqstp
->rq_prot
, rqstp
->rq_vers
,
202 hostname
, hostname_len
, &ssin
);
206 * Create the NLM RPC client for an NLM peer
209 nlm_bind_host(struct nlm_host
*host
)
211 struct rpc_clnt
*clnt
;
213 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT
"->"NIPQUAD_FMT
")\n",
214 NIPQUAD(host
->h_saddr
.sin_addr
),
215 NIPQUAD(host
->h_addr
.sin_addr
));
217 /* Lock host handle */
218 mutex_lock(&host
->h_mutex
);
220 /* If we've already created an RPC client, check whether
221 * RPC rebind is required
223 if ((clnt
= host
->h_rpcclnt
) != NULL
) {
224 if (time_after_eq(jiffies
, host
->h_nextrebind
)) {
225 rpc_force_rebind(clnt
);
226 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
227 dprintk("lockd: next rebind in %ld jiffies\n",
228 host
->h_nextrebind
- jiffies
);
231 unsigned long increment
= nlmsvc_timeout
;
232 struct rpc_timeout timeparms
= {
233 .to_initval
= increment
,
234 .to_increment
= increment
,
235 .to_maxval
= increment
* 6UL,
238 struct rpc_create_args args
= {
239 .protocol
= host
->h_proto
,
240 .address
= (struct sockaddr
*)&host
->h_addr
,
241 .addrsize
= sizeof(host
->h_addr
),
242 .saddress
= (struct sockaddr
*)&host
->h_saddr
,
243 .timeout
= &timeparms
,
244 .servername
= host
->h_name
,
245 .program
= &nlm_program
,
246 .version
= host
->h_version
,
247 .authflavor
= RPC_AUTH_UNIX
,
248 .flags
= (RPC_CLNT_CREATE_NOPING
|
249 RPC_CLNT_CREATE_AUTOBIND
),
253 * lockd retries server side blocks automatically so we want
254 * those to be soft RPC calls. Client side calls need to be
258 args
.flags
|= RPC_CLNT_CREATE_HARDRTRY
;
260 clnt
= rpc_create(&args
);
262 host
->h_rpcclnt
= clnt
;
264 printk("lockd: couldn't create RPC handle for %s\n", host
->h_name
);
269 mutex_unlock(&host
->h_mutex
);
274 * Force a portmap lookup of the remote lockd port
277 nlm_rebind_host(struct nlm_host
*host
)
279 dprintk("lockd: rebind host %s\n", host
->h_name
);
280 if (host
->h_rpcclnt
&& time_after_eq(jiffies
, host
->h_nextrebind
)) {
281 rpc_force_rebind(host
->h_rpcclnt
);
282 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
287 * Increment NLM host count
289 struct nlm_host
* nlm_get_host(struct nlm_host
*host
)
292 dprintk("lockd: get host %s\n", host
->h_name
);
293 atomic_inc(&host
->h_count
);
294 host
->h_expires
= jiffies
+ NLM_HOST_EXPIRE
;
300 * Release NLM host after use
302 void nlm_release_host(struct nlm_host
*host
)
305 dprintk("lockd: release host %s\n", host
->h_name
);
306 BUG_ON(atomic_read(&host
->h_count
) < 0);
307 if (atomic_dec_and_test(&host
->h_count
)) {
308 BUG_ON(!list_empty(&host
->h_lockowners
));
309 BUG_ON(!list_empty(&host
->h_granted
));
310 BUG_ON(!list_empty(&host
->h_reclaim
));
316 * We were notified that the host indicated by address &sin
318 * Release all resources held by that peer.
320 void nlm_host_rebooted(const struct sockaddr_in
*sin
,
321 const char *hostname
,
322 unsigned int hostname_len
,
325 struct hlist_head
*chain
;
326 struct hlist_node
*pos
;
327 struct nsm_handle
*nsm
;
328 struct nlm_host
*host
;
330 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
331 hostname
, NIPQUAD(sin
->sin_addr
));
333 /* Find the NSM handle for this peer */
334 if (!(nsm
= __nsm_find(sin
, hostname
, hostname_len
, 0)))
337 /* When reclaiming locks on this peer, make sure that
338 * we set up a new notification */
339 nsm
->sm_monitored
= 0;
341 /* Mark all hosts tied to this NSM state as having rebooted.
342 * We run the loop repeatedly, because we drop the host table
344 * To avoid processing a host several times, we match the nsmstate.
346 again
: mutex_lock(&nlm_host_mutex
);
347 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
348 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
349 if (host
->h_nsmhandle
== nsm
350 && host
->h_nsmstate
!= new_state
) {
351 host
->h_nsmstate
= new_state
;
355 mutex_unlock(&nlm_host_mutex
);
357 if (host
->h_server
) {
358 /* We're server for this guy, just ditch
359 * all the locks he held. */
360 nlmsvc_free_host_resources(host
);
362 /* He's the server, initiate lock recovery. */
363 nlmclnt_recovery(host
);
366 nlm_release_host(host
);
372 mutex_unlock(&nlm_host_mutex
);
376 * Shut down the hosts module.
377 * Note that this routine is called only at server shutdown time.
380 nlm_shutdown_hosts(void)
382 struct hlist_head
*chain
;
383 struct hlist_node
*pos
;
384 struct nlm_host
*host
;
386 dprintk("lockd: shutting down host module\n");
387 mutex_lock(&nlm_host_mutex
);
389 /* First, make all hosts eligible for gc */
390 dprintk("lockd: nuking all hosts...\n");
391 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
392 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
393 host
->h_expires
= jiffies
- 1;
394 if (host
->h_rpcclnt
) {
395 rpc_shutdown_client(host
->h_rpcclnt
);
396 host
->h_rpcclnt
= NULL
;
401 /* Then, perform a garbage collection pass */
403 mutex_unlock(&nlm_host_mutex
);
405 /* complain if any hosts are left */
407 printk(KERN_WARNING
"lockd: couldn't shutdown host module!\n");
408 dprintk("lockd: %d hosts left:\n", nrhosts
);
409 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
410 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
411 dprintk(" %s (cnt %d use %d exp %ld)\n",
412 host
->h_name
, atomic_read(&host
->h_count
),
413 host
->h_inuse
, host
->h_expires
);
420 * Garbage collect any unused NLM hosts.
421 * This GC combines reference counting for async operations with
422 * mark & sweep for resources held by remote clients.
427 struct hlist_head
*chain
;
428 struct hlist_node
*pos
, *next
;
429 struct nlm_host
*host
;
431 dprintk("lockd: host garbage collection\n");
432 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
433 hlist_for_each_entry(host
, pos
, chain
, h_hash
)
437 /* Mark all hosts that hold locks, blocks or shares */
438 nlmsvc_mark_resources();
440 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
441 hlist_for_each_entry_safe(host
, pos
, next
, chain
, h_hash
) {
442 if (atomic_read(&host
->h_count
) || host
->h_inuse
443 || time_before(jiffies
, host
->h_expires
)) {
444 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
445 host
->h_name
, atomic_read(&host
->h_count
),
446 host
->h_inuse
, host
->h_expires
);
449 dprintk("lockd: delete host %s\n", host
->h_name
);
450 hlist_del_init(&host
->h_hash
);
452 nlm_destroy_host(host
);
457 next_gc
= jiffies
+ NLM_HOST_COLLECT
;
464 static LIST_HEAD(nsm_handles
);
465 static DEFINE_MUTEX(nsm_mutex
);
467 static struct nsm_handle
*
468 __nsm_find(const struct sockaddr_in
*sin
,
469 const char *hostname
, unsigned int hostname_len
,
472 struct nsm_handle
*nsm
= NULL
;
473 struct list_head
*pos
;
478 if (hostname
&& memchr(hostname
, '/', hostname_len
) != NULL
) {
479 if (printk_ratelimit()) {
480 printk(KERN_WARNING
"Invalid hostname \"%.*s\" "
481 "in NFS lock request\n",
482 hostname_len
, hostname
);
487 mutex_lock(&nsm_mutex
);
488 list_for_each(pos
, &nsm_handles
) {
489 nsm
= list_entry(pos
, struct nsm_handle
, sm_link
);
491 if (hostname
&& nsm_use_hostnames
) {
492 if (strlen(nsm
->sm_name
) != hostname_len
493 || memcmp(nsm
->sm_name
, hostname
, hostname_len
))
495 } else if (!nlm_cmp_addr(&nsm
->sm_addr
, sin
))
497 atomic_inc(&nsm
->sm_count
);
506 nsm
= kzalloc(sizeof(*nsm
) + hostname_len
+ 1, GFP_KERNEL
);
509 nsm
->sm_name
= (char *) (nsm
+ 1);
510 memcpy(nsm
->sm_name
, hostname
, hostname_len
);
511 nsm
->sm_name
[hostname_len
] = '\0';
512 atomic_set(&nsm
->sm_count
, 1);
514 list_add(&nsm
->sm_link
, &nsm_handles
);
518 mutex_unlock(&nsm_mutex
);
522 static struct nsm_handle
*
523 nsm_find(const struct sockaddr_in
*sin
, const char *hostname
,
524 unsigned int hostname_len
)
526 return __nsm_find(sin
, hostname
, hostname_len
, 1);
530 * Release an NSM handle
533 nsm_release(struct nsm_handle
*nsm
)
537 if (atomic_dec_and_test(&nsm
->sm_count
)) {
538 mutex_lock(&nsm_mutex
);
539 if (atomic_read(&nsm
->sm_count
) == 0) {
540 list_del(&nsm
->sm_link
);
543 mutex_unlock(&nsm_mutex
);