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_NRHASH 32
23 #define NLM_ADDRHASH(addr) (ntohl(addr) & (NLM_HOST_NRHASH-1))
24 #define NLM_HOST_REBIND (60 * HZ)
25 #define NLM_HOST_EXPIRE (300 * HZ)
26 #define NLM_HOST_COLLECT (120 * HZ)
28 static struct hlist_head nlm_hosts
[NLM_HOST_NRHASH
];
29 static unsigned long next_gc
;
31 static DEFINE_MUTEX(nlm_host_mutex
);
34 static void nlm_gc_hosts(void);
35 static struct nsm_handle
* __nsm_find(const struct sockaddr_in
*,
36 const char *, unsigned int, int);
37 static struct nsm_handle
* nsm_find(const struct sockaddr_in
*sin
,
39 unsigned int hostname_len
);
42 * Common host lookup routine for server & client
44 static struct nlm_host
*
45 nlm_lookup_host(int server
, const struct sockaddr_in
*sin
,
46 int proto
, int version
, const char *hostname
,
47 unsigned int hostname_len
,
48 const struct sockaddr_in
*ssin
)
50 struct hlist_head
*chain
;
51 struct hlist_node
*pos
;
52 struct nlm_host
*host
;
53 struct nsm_handle
*nsm
= NULL
;
56 dprintk("lockd: nlm_lookup_host("NIPQUAD_FMT
"->"NIPQUAD_FMT
57 ", p=%d, v=%d, my role=%s, name=%.*s)\n",
58 NIPQUAD(ssin
->sin_addr
.s_addr
),
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
)
95 if (!nlm_cmp_addr(&host
->h_saddr
, ssin
))
98 /* Move to head of hash chain. */
99 hlist_del(&host
->h_hash
);
100 hlist_add_head(&host
->h_hash
, chain
);
106 atomic_inc(&nsm
->sm_count
);
110 /* Sadly, the host isn't in our hash table yet. See if
111 * we have an NSM handle for it. If not, create one.
113 if (!nsm
&& !(nsm
= nsm_find(sin
, hostname
, hostname_len
)))
116 host
= kzalloc(sizeof(*host
), GFP_KERNEL
);
121 host
->h_name
= nsm
->sm_name
;
123 host
->h_addr
.sin_port
= 0; /* ouch! */
124 host
->h_saddr
= *ssin
;
125 host
->h_version
= version
;
126 host
->h_proto
= proto
;
127 host
->h_rpcclnt
= NULL
;
128 mutex_init(&host
->h_mutex
);
129 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
130 host
->h_expires
= jiffies
+ NLM_HOST_EXPIRE
;
131 atomic_set(&host
->h_count
, 1);
132 init_waitqueue_head(&host
->h_gracewait
);
133 init_rwsem(&host
->h_rwsem
);
134 host
->h_state
= 0; /* pseudo NSM state */
135 host
->h_nsmstate
= 0; /* real NSM state */
136 host
->h_nsmhandle
= nsm
;
137 host
->h_server
= server
;
138 hlist_add_head(&host
->h_hash
, chain
);
139 INIT_LIST_HEAD(&host
->h_lockowners
);
140 spin_lock_init(&host
->h_lock
);
141 INIT_LIST_HEAD(&host
->h_granted
);
142 INIT_LIST_HEAD(&host
->h_reclaim
);
146 mutex_unlock(&nlm_host_mutex
);
154 nlm_destroy_host(struct nlm_host
*host
)
156 struct rpc_clnt
*clnt
;
158 BUG_ON(!list_empty(&host
->h_lockowners
));
159 BUG_ON(atomic_read(&host
->h_count
));
162 * Release NSM handle and unmonitor host.
166 clnt
= host
->h_rpcclnt
;
168 rpc_shutdown_client(clnt
);
173 * Find an NLM server handle in the cache. If there is none, create it.
176 nlmclnt_lookup_host(const struct sockaddr_in
*sin
, int proto
, int version
,
177 const char *hostname
, unsigned int hostname_len
)
179 struct sockaddr_in ssin
= {0};
181 return nlm_lookup_host(0, sin
, proto
, version
,
182 hostname
, hostname_len
, &ssin
);
186 * Find an NLM client handle in the cache. If there is none, create it.
189 nlmsvc_lookup_host(struct svc_rqst
*rqstp
,
190 const char *hostname
, unsigned int hostname_len
)
192 struct sockaddr_in ssin
= {0};
194 ssin
.sin_addr
= rqstp
->rq_daddr
.addr
;
195 return nlm_lookup_host(1, svc_addr_in(rqstp
),
196 rqstp
->rq_prot
, rqstp
->rq_vers
,
197 hostname
, hostname_len
, &ssin
);
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("NIPQUAD_FMT
"->"NIPQUAD_FMT
")\n",
209 NIPQUAD(host
->h_saddr
.sin_addr
),
210 NIPQUAD(host
->h_addr
.sin_addr
));
212 /* Lock host handle */
213 mutex_lock(&host
->h_mutex
);
215 /* If we've already created an RPC client, check whether
216 * RPC rebind is required
218 if ((clnt
= host
->h_rpcclnt
) != NULL
) {
219 if (time_after_eq(jiffies
, host
->h_nextrebind
)) {
220 rpc_force_rebind(clnt
);
221 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
222 dprintk("lockd: next rebind in %ld jiffies\n",
223 host
->h_nextrebind
- jiffies
);
226 unsigned long increment
= nlmsvc_timeout
;
227 struct rpc_timeout timeparms
= {
228 .to_initval
= increment
,
229 .to_increment
= increment
,
230 .to_maxval
= increment
* 6UL,
233 struct rpc_create_args args
= {
234 .protocol
= host
->h_proto
,
235 .address
= (struct sockaddr
*)&host
->h_addr
,
236 .addrsize
= sizeof(host
->h_addr
),
237 .saddress
= (struct sockaddr
*)&host
->h_saddr
,
238 .timeout
= &timeparms
,
239 .servername
= host
->h_name
,
240 .program
= &nlm_program
,
241 .version
= host
->h_version
,
242 .authflavor
= RPC_AUTH_UNIX
,
243 .flags
= (RPC_CLNT_CREATE_NOPING
|
244 RPC_CLNT_CREATE_AUTOBIND
),
248 * lockd retries server side blocks automatically so we want
249 * those to be soft RPC calls. Client side calls need to be
253 args
.flags
|= RPC_CLNT_CREATE_HARDRTRY
;
255 clnt
= rpc_create(&args
);
257 host
->h_rpcclnt
= clnt
;
259 printk("lockd: couldn't create RPC handle for %s\n", host
->h_name
);
264 mutex_unlock(&host
->h_mutex
);
269 * Force a portmap lookup of the remote lockd port
272 nlm_rebind_host(struct nlm_host
*host
)
274 dprintk("lockd: rebind host %s\n", host
->h_name
);
275 if (host
->h_rpcclnt
&& time_after_eq(jiffies
, host
->h_nextrebind
)) {
276 rpc_force_rebind(host
->h_rpcclnt
);
277 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
282 * Increment NLM host count
284 struct nlm_host
* nlm_get_host(struct nlm_host
*host
)
287 dprintk("lockd: get host %s\n", host
->h_name
);
288 atomic_inc(&host
->h_count
);
289 host
->h_expires
= jiffies
+ NLM_HOST_EXPIRE
;
295 * Release NLM host after use
297 void nlm_release_host(struct nlm_host
*host
)
300 dprintk("lockd: release host %s\n", host
->h_name
);
301 BUG_ON(atomic_read(&host
->h_count
) < 0);
302 if (atomic_dec_and_test(&host
->h_count
)) {
303 BUG_ON(!list_empty(&host
->h_lockowners
));
304 BUG_ON(!list_empty(&host
->h_granted
));
305 BUG_ON(!list_empty(&host
->h_reclaim
));
311 * We were notified that the host indicated by address &sin
313 * Release all resources held by that peer.
315 void nlm_host_rebooted(const struct sockaddr_in
*sin
,
316 const char *hostname
,
317 unsigned int hostname_len
,
320 struct hlist_head
*chain
;
321 struct hlist_node
*pos
;
322 struct nsm_handle
*nsm
;
323 struct nlm_host
*host
;
325 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
326 hostname
, NIPQUAD(sin
->sin_addr
));
328 /* Find the NSM handle for this peer */
329 if (!(nsm
= __nsm_find(sin
, hostname
, hostname_len
, 0)))
332 /* When reclaiming locks on this peer, make sure that
333 * we set up a new notification */
334 nsm
->sm_monitored
= 0;
336 /* Mark all hosts tied to this NSM state as having rebooted.
337 * We run the loop repeatedly, because we drop the host table
339 * To avoid processing a host several times, we match the nsmstate.
341 again
: mutex_lock(&nlm_host_mutex
);
342 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
343 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
344 if (host
->h_nsmhandle
== nsm
345 && host
->h_nsmstate
!= new_state
) {
346 host
->h_nsmstate
= new_state
;
350 mutex_unlock(&nlm_host_mutex
);
352 if (host
->h_server
) {
353 /* We're server for this guy, just ditch
354 * all the locks he held. */
355 nlmsvc_free_host_resources(host
);
357 /* He's the server, initiate lock recovery. */
358 nlmclnt_recovery(host
);
361 nlm_release_host(host
);
367 mutex_unlock(&nlm_host_mutex
);
371 * Shut down the hosts module.
372 * Note that this routine is called only at server shutdown time.
375 nlm_shutdown_hosts(void)
377 struct hlist_head
*chain
;
378 struct hlist_node
*pos
;
379 struct nlm_host
*host
;
381 dprintk("lockd: shutting down host module\n");
382 mutex_lock(&nlm_host_mutex
);
384 /* First, make all hosts eligible for gc */
385 dprintk("lockd: nuking all hosts...\n");
386 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
387 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
388 host
->h_expires
= jiffies
- 1;
389 if (host
->h_rpcclnt
) {
390 rpc_shutdown_client(host
->h_rpcclnt
);
391 host
->h_rpcclnt
= NULL
;
396 /* Then, perform a garbage collection pass */
398 mutex_unlock(&nlm_host_mutex
);
400 /* complain if any hosts are left */
402 printk(KERN_WARNING
"lockd: couldn't shutdown host module!\n");
403 dprintk("lockd: %d hosts left:\n", nrhosts
);
404 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
405 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
406 dprintk(" %s (cnt %d use %d exp %ld)\n",
407 host
->h_name
, atomic_read(&host
->h_count
),
408 host
->h_inuse
, host
->h_expires
);
415 * Garbage collect any unused NLM hosts.
416 * This GC combines reference counting for async operations with
417 * mark & sweep for resources held by remote clients.
422 struct hlist_head
*chain
;
423 struct hlist_node
*pos
, *next
;
424 struct nlm_host
*host
;
426 dprintk("lockd: host garbage collection\n");
427 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
428 hlist_for_each_entry(host
, pos
, chain
, h_hash
)
432 /* Mark all hosts that hold locks, blocks or shares */
433 nlmsvc_mark_resources();
435 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
436 hlist_for_each_entry_safe(host
, pos
, next
, chain
, h_hash
) {
437 if (atomic_read(&host
->h_count
) || host
->h_inuse
438 || time_before(jiffies
, host
->h_expires
)) {
439 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
440 host
->h_name
, atomic_read(&host
->h_count
),
441 host
->h_inuse
, host
->h_expires
);
444 dprintk("lockd: delete host %s\n", host
->h_name
);
445 hlist_del_init(&host
->h_hash
);
447 nlm_destroy_host(host
);
452 next_gc
= jiffies
+ NLM_HOST_COLLECT
;
459 static LIST_HEAD(nsm_handles
);
460 static DEFINE_MUTEX(nsm_mutex
);
462 static struct nsm_handle
*
463 __nsm_find(const struct sockaddr_in
*sin
,
464 const char *hostname
, unsigned int hostname_len
,
467 struct nsm_handle
*nsm
= NULL
;
468 struct list_head
*pos
;
473 if (hostname
&& memchr(hostname
, '/', hostname_len
) != NULL
) {
474 if (printk_ratelimit()) {
475 printk(KERN_WARNING
"Invalid hostname \"%.*s\" "
476 "in NFS lock request\n",
477 hostname_len
, hostname
);
482 mutex_lock(&nsm_mutex
);
483 list_for_each(pos
, &nsm_handles
) {
484 nsm
= list_entry(pos
, struct nsm_handle
, sm_link
);
486 if (hostname
&& nsm_use_hostnames
) {
487 if (strlen(nsm
->sm_name
) != hostname_len
488 || memcmp(nsm
->sm_name
, hostname
, hostname_len
))
490 } else if (!nlm_cmp_addr(&nsm
->sm_addr
, sin
))
492 atomic_inc(&nsm
->sm_count
);
501 nsm
= kzalloc(sizeof(*nsm
) + hostname_len
+ 1, GFP_KERNEL
);
504 nsm
->sm_name
= (char *) (nsm
+ 1);
505 memcpy(nsm
->sm_name
, hostname
, hostname_len
);
506 nsm
->sm_name
[hostname_len
] = '\0';
507 atomic_set(&nsm
->sm_count
, 1);
509 list_add(&nsm
->sm_link
, &nsm_handles
);
513 mutex_unlock(&nsm_mutex
);
517 static struct nsm_handle
*
518 nsm_find(const struct sockaddr_in
*sin
, const char *hostname
,
519 unsigned int hostname_len
)
521 return __nsm_find(sin
, hostname
, hostname_len
, 1);
525 * Release an NSM handle
528 nsm_release(struct nsm_handle
*nsm
)
532 mutex_lock(&nsm_mutex
);
533 if (atomic_dec_and_test(&nsm
->sm_count
)) {
534 list_del(&nsm
->sm_link
);
537 mutex_unlock(&nsm_mutex
);