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/sched.h>
13 #include <linux/slab.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>
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)
30 static struct hlist_head nlm_hosts
[NLM_HOST_NRHASH
];
31 static unsigned long next_gc
;
33 static DEFINE_MUTEX(nlm_host_mutex
);
36 static void nlm_gc_hosts(void);
37 static struct nsm_handle
* __nsm_find(const struct sockaddr_in
*,
38 const char *, int, int);
39 static struct nsm_handle
* nsm_find(const struct sockaddr_in
*sin
,
44 * Common host lookup routine for server & client
46 static struct nlm_host
*
47 nlm_lookup_host(int server
, const struct sockaddr_in
*sin
,
48 int proto
, int version
,
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(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n",
59 NIPQUAD(sin
->sin_addr
.s_addr
), proto
, version
,
60 server
? "server" : "client",
62 hostname
? hostname
: "<none>");
65 hash
= NLM_ADDRHASH(sin
->sin_addr
.s_addr
);
68 mutex_lock(&nlm_host_mutex
);
70 if (time_after_eq(jiffies
, next_gc
))
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.
80 chain
= &nlm_hosts
[hash
];
81 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
82 if (!nlm_cmp_addr(&host
->h_addr
, sin
))
85 /* See if we have an NSM handle for this client */
87 nsm
= host
->h_nsmhandle
;
89 if (host
->h_proto
!= proto
)
91 if (host
->h_version
!= version
)
93 if (host
->h_server
!= server
)
96 /* Move to head of hash chain. */
97 hlist_del(&host
->h_hash
);
98 hlist_add_head(&host
->h_hash
, chain
);
104 atomic_inc(&nsm
->sm_count
);
108 /* Sadly, the host isn't in our hash table yet. See if
109 * we have an NSM handle for it. If not, create one.
111 if (!nsm
&& !(nsm
= nsm_find(sin
, hostname
, hostname_len
)))
114 host
= kzalloc(sizeof(*host
), GFP_KERNEL
);
119 host
->h_name
= nsm
->sm_name
;
121 host
->h_addr
.sin_port
= 0; /* ouch! */
122 host
->h_version
= version
;
123 host
->h_proto
= proto
;
124 host
->h_rpcclnt
= NULL
;
125 mutex_init(&host
->h_mutex
);
126 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
127 host
->h_expires
= jiffies
+ NLM_HOST_EXPIRE
;
128 atomic_set(&host
->h_count
, 1);
129 init_waitqueue_head(&host
->h_gracewait
);
130 init_rwsem(&host
->h_rwsem
);
131 host
->h_state
= 0; /* pseudo NSM state */
132 host
->h_nsmstate
= 0; /* real NSM state */
133 host
->h_nsmhandle
= nsm
;
134 host
->h_server
= server
;
135 hlist_add_head(&host
->h_hash
, chain
);
136 INIT_LIST_HEAD(&host
->h_lockowners
);
137 spin_lock_init(&host
->h_lock
);
138 INIT_LIST_HEAD(&host
->h_granted
);
139 INIT_LIST_HEAD(&host
->h_reclaim
);
141 if (++nrhosts
> NLM_HOST_MAX
)
145 mutex_unlock(&nlm_host_mutex
);
153 nlm_destroy_host(struct nlm_host
*host
)
155 struct rpc_clnt
*clnt
;
157 BUG_ON(!list_empty(&host
->h_lockowners
));
158 BUG_ON(atomic_read(&host
->h_count
));
161 * Release NSM handle and unmonitor host.
165 if ((clnt
= host
->h_rpcclnt
) != NULL
) {
166 if (atomic_read(&clnt
->cl_users
)) {
168 "lockd: active RPC handle\n");
171 rpc_destroy_client(host
->h_rpcclnt
);
178 * Find an NLM server handle in the cache. If there is none, create it.
181 nlmclnt_lookup_host(const struct sockaddr_in
*sin
, int proto
, int version
,
182 const char *hostname
, int hostname_len
)
184 return nlm_lookup_host(0, sin
, proto
, version
,
185 hostname
, hostname_len
);
189 * Find an NLM client handle in the cache. If there is none, create it.
192 nlmsvc_lookup_host(struct svc_rqst
*rqstp
,
193 const char *hostname
, int hostname_len
)
195 return nlm_lookup_host(1, &rqstp
->rq_addr
,
196 rqstp
->rq_prot
, rqstp
->rq_vers
,
197 hostname
, hostname_len
);
201 * Create the NLM RPC client for an NLM peer
204 nlm_bind_host(struct nlm_host
*host
)
206 struct rpc_clnt
*clnt
;
208 dprintk("lockd: nlm_bind_host(%08x)\n",
209 (unsigned)ntohl(host
->h_addr
.sin_addr
.s_addr
));
211 /* Lock host handle */
212 mutex_lock(&host
->h_mutex
);
214 /* If we've already created an RPC client, check whether
215 * RPC rebind is required
217 if ((clnt
= host
->h_rpcclnt
) != NULL
) {
218 if (time_after_eq(jiffies
, host
->h_nextrebind
)) {
219 rpc_force_rebind(clnt
);
220 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
221 dprintk("lockd: next rebind in %ld jiffies\n",
222 host
->h_nextrebind
- jiffies
);
225 unsigned long increment
= nlmsvc_timeout
* HZ
;
226 struct rpc_timeout timeparms
= {
227 .to_initval
= increment
,
228 .to_increment
= increment
,
229 .to_maxval
= increment
* 6UL,
232 struct rpc_create_args args
= {
233 .protocol
= host
->h_proto
,
234 .address
= (struct sockaddr
*)&host
->h_addr
,
235 .addrsize
= sizeof(host
->h_addr
),
236 .timeout
= &timeparms
,
237 .servername
= host
->h_name
,
238 .program
= &nlm_program
,
239 .version
= host
->h_version
,
240 .authflavor
= RPC_AUTH_UNIX
,
241 .flags
= (RPC_CLNT_CREATE_HARDRTRY
|
242 RPC_CLNT_CREATE_AUTOBIND
),
245 clnt
= rpc_create(&args
);
247 host
->h_rpcclnt
= clnt
;
249 printk("lockd: couldn't create RPC handle for %s\n", host
->h_name
);
254 mutex_unlock(&host
->h_mutex
);
259 * Force a portmap lookup of the remote lockd port
262 nlm_rebind_host(struct nlm_host
*host
)
264 dprintk("lockd: rebind host %s\n", host
->h_name
);
265 if (host
->h_rpcclnt
&& time_after_eq(jiffies
, host
->h_nextrebind
)) {
266 rpc_force_rebind(host
->h_rpcclnt
);
267 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
272 * Increment NLM host count
274 struct nlm_host
* nlm_get_host(struct nlm_host
*host
)
277 dprintk("lockd: get host %s\n", host
->h_name
);
278 atomic_inc(&host
->h_count
);
279 host
->h_expires
= jiffies
+ NLM_HOST_EXPIRE
;
285 * Release NLM host after use
287 void nlm_release_host(struct nlm_host
*host
)
290 dprintk("lockd: release host %s\n", host
->h_name
);
291 BUG_ON(atomic_read(&host
->h_count
) < 0);
292 if (atomic_dec_and_test(&host
->h_count
)) {
293 BUG_ON(!list_empty(&host
->h_lockowners
));
294 BUG_ON(!list_empty(&host
->h_granted
));
295 BUG_ON(!list_empty(&host
->h_reclaim
));
301 * We were notified that the host indicated by address &sin
303 * Release all resources held by that peer.
305 void nlm_host_rebooted(const struct sockaddr_in
*sin
,
306 const char *hostname
, int hostname_len
,
309 struct hlist_head
*chain
;
310 struct hlist_node
*pos
;
311 struct nsm_handle
*nsm
;
312 struct nlm_host
*host
;
314 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
315 hostname
, NIPQUAD(sin
->sin_addr
));
317 /* Find the NSM handle for this peer */
318 if (!(nsm
= __nsm_find(sin
, hostname
, hostname_len
, 0)))
321 /* When reclaiming locks on this peer, make sure that
322 * we set up a new notification */
323 nsm
->sm_monitored
= 0;
325 /* Mark all hosts tied to this NSM state as having rebooted.
326 * We run the loop repeatedly, because we drop the host table
328 * To avoid processing a host several times, we match the nsmstate.
330 again
: mutex_lock(&nlm_host_mutex
);
331 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
332 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
333 if (host
->h_nsmhandle
== nsm
334 && host
->h_nsmstate
!= new_state
) {
335 host
->h_nsmstate
= new_state
;
339 mutex_unlock(&nlm_host_mutex
);
341 if (host
->h_server
) {
342 /* We're server for this guy, just ditch
343 * all the locks he held. */
344 nlmsvc_free_host_resources(host
);
346 /* He's the server, initiate lock recovery. */
347 nlmclnt_recovery(host
);
350 nlm_release_host(host
);
356 mutex_unlock(&nlm_host_mutex
);
360 * Shut down the hosts module.
361 * Note that this routine is called only at server shutdown time.
364 nlm_shutdown_hosts(void)
366 struct hlist_head
*chain
;
367 struct hlist_node
*pos
;
368 struct nlm_host
*host
;
370 dprintk("lockd: shutting down host module\n");
371 mutex_lock(&nlm_host_mutex
);
373 /* First, make all hosts eligible for gc */
374 dprintk("lockd: nuking all hosts...\n");
375 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
376 hlist_for_each_entry(host
, pos
, chain
, h_hash
)
377 host
->h_expires
= jiffies
- 1;
380 /* Then, perform a garbage collection pass */
382 mutex_unlock(&nlm_host_mutex
);
384 /* complain if any hosts are left */
386 printk(KERN_WARNING
"lockd: couldn't shutdown host module!\n");
387 dprintk("lockd: %d hosts left:\n", nrhosts
);
388 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
389 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
390 dprintk(" %s (cnt %d use %d exp %ld)\n",
391 host
->h_name
, atomic_read(&host
->h_count
),
392 host
->h_inuse
, host
->h_expires
);
399 * Garbage collect any unused NLM hosts.
400 * This GC combines reference counting for async operations with
401 * mark & sweep for resources held by remote clients.
406 struct hlist_head
*chain
;
407 struct hlist_node
*pos
, *next
;
408 struct nlm_host
*host
;
410 dprintk("lockd: host garbage collection\n");
411 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
412 hlist_for_each_entry(host
, pos
, chain
, h_hash
)
416 /* Mark all hosts that hold locks, blocks or shares */
417 nlmsvc_mark_resources();
419 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
420 hlist_for_each_entry_safe(host
, pos
, next
, chain
, h_hash
) {
421 if (atomic_read(&host
->h_count
) || host
->h_inuse
422 || time_before(jiffies
, host
->h_expires
)) {
423 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
424 host
->h_name
, atomic_read(&host
->h_count
),
425 host
->h_inuse
, host
->h_expires
);
428 dprintk("lockd: delete host %s\n", host
->h_name
);
429 hlist_del_init(&host
->h_hash
);
431 nlm_destroy_host(host
);
436 next_gc
= jiffies
+ NLM_HOST_COLLECT
;
443 static LIST_HEAD(nsm_handles
);
444 static DEFINE_MUTEX(nsm_mutex
);
446 static struct nsm_handle
*
447 __nsm_find(const struct sockaddr_in
*sin
,
448 const char *hostname
, int hostname_len
,
451 struct nsm_handle
*nsm
= NULL
;
452 struct list_head
*pos
;
457 if (hostname
&& memchr(hostname
, '/', hostname_len
) != NULL
) {
458 if (printk_ratelimit()) {
459 printk(KERN_WARNING
"Invalid hostname \"%.*s\" "
460 "in NFS lock request\n",
461 hostname_len
, hostname
);
466 mutex_lock(&nsm_mutex
);
467 list_for_each(pos
, &nsm_handles
) {
468 nsm
= list_entry(pos
, struct nsm_handle
, sm_link
);
470 if (hostname
&& nsm_use_hostnames
) {
471 if (strlen(nsm
->sm_name
) != hostname_len
472 || memcmp(nsm
->sm_name
, hostname
, hostname_len
))
474 } else if (!nlm_cmp_addr(&nsm
->sm_addr
, sin
))
476 atomic_inc(&nsm
->sm_count
);
485 nsm
= kzalloc(sizeof(*nsm
) + hostname_len
+ 1, GFP_KERNEL
);
488 nsm
->sm_name
= (char *) (nsm
+ 1);
489 memcpy(nsm
->sm_name
, hostname
, hostname_len
);
490 nsm
->sm_name
[hostname_len
] = '\0';
491 atomic_set(&nsm
->sm_count
, 1);
493 list_add(&nsm
->sm_link
, &nsm_handles
);
497 mutex_unlock(&nsm_mutex
);
501 static struct nsm_handle
*
502 nsm_find(const struct sockaddr_in
*sin
, const char *hostname
, int hostname_len
)
504 return __nsm_find(sin
, hostname
, hostname_len
, 1);
508 * Release an NSM handle
511 nsm_release(struct nsm_handle
*nsm
)
515 if (atomic_dec_and_test(&nsm
->sm_count
)) {
516 mutex_lock(&nsm_mutex
);
517 if (atomic_read(&nsm
->sm_count
) == 0) {
518 list_del(&nsm
->sm_link
);
521 mutex_unlock(&nsm_mutex
);