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 *, int, int);
38 static struct nsm_handle
* nsm_find(const struct sockaddr_in
*sin
,
43 * Common host lookup routine for server & client
45 static struct nlm_host
*
46 nlm_lookup_host(int server
, const struct sockaddr_in
*sin
,
47 int proto
, int version
, const char *hostname
,
48 int hostname_len
, 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
);
144 if (++nrhosts
> NLM_HOST_MAX
)
148 mutex_unlock(&nlm_host_mutex
);
156 nlm_destroy_host(struct nlm_host
*host
)
158 struct rpc_clnt
*clnt
;
160 BUG_ON(!list_empty(&host
->h_lockowners
));
161 BUG_ON(atomic_read(&host
->h_count
));
164 * Release NSM handle and unmonitor host.
168 clnt
= host
->h_rpcclnt
;
170 rpc_shutdown_client(clnt
);
175 * Find an NLM server handle in the cache. If there is none, create it.
178 nlmclnt_lookup_host(const struct sockaddr_in
*sin
, int proto
, int version
,
179 const char *hostname
, int hostname_len
)
181 struct sockaddr_in ssin
= {0};
183 return nlm_lookup_host(0, sin
, proto
, version
,
184 hostname
, hostname_len
, &ssin
);
188 * Find an NLM client handle in the cache. If there is none, create it.
191 nlmsvc_lookup_host(struct svc_rqst
*rqstp
,
192 const char *hostname
, int hostname_len
)
194 struct sockaddr_in ssin
= {0};
196 ssin
.sin_addr
= rqstp
->rq_daddr
.addr
;
197 return nlm_lookup_host(1, svc_addr_in(rqstp
),
198 rqstp
->rq_prot
, rqstp
->rq_vers
,
199 hostname
, hostname_len
, &ssin
);
203 * Create the NLM RPC client for an NLM peer
206 nlm_bind_host(struct nlm_host
*host
)
208 struct rpc_clnt
*clnt
;
210 dprintk("lockd: nlm_bind_host("NIPQUAD_FMT
"->"NIPQUAD_FMT
")\n",
211 NIPQUAD(host
->h_saddr
.sin_addr
),
212 NIPQUAD(host
->h_addr
.sin_addr
));
214 /* Lock host handle */
215 mutex_lock(&host
->h_mutex
);
217 /* If we've already created an RPC client, check whether
218 * RPC rebind is required
220 if ((clnt
= host
->h_rpcclnt
) != NULL
) {
221 if (time_after_eq(jiffies
, host
->h_nextrebind
)) {
222 rpc_force_rebind(clnt
);
223 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
224 dprintk("lockd: next rebind in %ld jiffies\n",
225 host
->h_nextrebind
- jiffies
);
228 unsigned long increment
= nlmsvc_timeout
;
229 struct rpc_timeout timeparms
= {
230 .to_initval
= increment
,
231 .to_increment
= increment
,
232 .to_maxval
= increment
* 6UL,
235 struct rpc_create_args args
= {
236 .protocol
= host
->h_proto
,
237 .address
= (struct sockaddr
*)&host
->h_addr
,
238 .addrsize
= sizeof(host
->h_addr
),
239 .saddress
= (struct sockaddr
*)&host
->h_saddr
,
240 .timeout
= &timeparms
,
241 .servername
= host
->h_name
,
242 .program
= &nlm_program
,
243 .version
= host
->h_version
,
244 .authflavor
= RPC_AUTH_UNIX
,
245 .flags
= (RPC_CLNT_CREATE_HARDRTRY
|
246 RPC_CLNT_CREATE_AUTOBIND
),
249 clnt
= rpc_create(&args
);
251 host
->h_rpcclnt
= clnt
;
253 printk("lockd: couldn't create RPC handle for %s\n", host
->h_name
);
258 mutex_unlock(&host
->h_mutex
);
263 * Force a portmap lookup of the remote lockd port
266 nlm_rebind_host(struct nlm_host
*host
)
268 dprintk("lockd: rebind host %s\n", host
->h_name
);
269 if (host
->h_rpcclnt
&& time_after_eq(jiffies
, host
->h_nextrebind
)) {
270 rpc_force_rebind(host
->h_rpcclnt
);
271 host
->h_nextrebind
= jiffies
+ NLM_HOST_REBIND
;
276 * Increment NLM host count
278 struct nlm_host
* nlm_get_host(struct nlm_host
*host
)
281 dprintk("lockd: get host %s\n", host
->h_name
);
282 atomic_inc(&host
->h_count
);
283 host
->h_expires
= jiffies
+ NLM_HOST_EXPIRE
;
289 * Release NLM host after use
291 void nlm_release_host(struct nlm_host
*host
)
294 dprintk("lockd: release host %s\n", host
->h_name
);
295 BUG_ON(atomic_read(&host
->h_count
) < 0);
296 if (atomic_dec_and_test(&host
->h_count
)) {
297 BUG_ON(!list_empty(&host
->h_lockowners
));
298 BUG_ON(!list_empty(&host
->h_granted
));
299 BUG_ON(!list_empty(&host
->h_reclaim
));
305 * We were notified that the host indicated by address &sin
307 * Release all resources held by that peer.
309 void nlm_host_rebooted(const struct sockaddr_in
*sin
,
310 const char *hostname
, int hostname_len
,
313 struct hlist_head
*chain
;
314 struct hlist_node
*pos
;
315 struct nsm_handle
*nsm
;
316 struct nlm_host
*host
;
318 dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
319 hostname
, NIPQUAD(sin
->sin_addr
));
321 /* Find the NSM handle for this peer */
322 if (!(nsm
= __nsm_find(sin
, hostname
, hostname_len
, 0)))
325 /* When reclaiming locks on this peer, make sure that
326 * we set up a new notification */
327 nsm
->sm_monitored
= 0;
329 /* Mark all hosts tied to this NSM state as having rebooted.
330 * We run the loop repeatedly, because we drop the host table
332 * To avoid processing a host several times, we match the nsmstate.
334 again
: mutex_lock(&nlm_host_mutex
);
335 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
336 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
337 if (host
->h_nsmhandle
== nsm
338 && host
->h_nsmstate
!= new_state
) {
339 host
->h_nsmstate
= new_state
;
343 mutex_unlock(&nlm_host_mutex
);
345 if (host
->h_server
) {
346 /* We're server for this guy, just ditch
347 * all the locks he held. */
348 nlmsvc_free_host_resources(host
);
350 /* He's the server, initiate lock recovery. */
351 nlmclnt_recovery(host
);
354 nlm_release_host(host
);
360 mutex_unlock(&nlm_host_mutex
);
364 * Shut down the hosts module.
365 * Note that this routine is called only at server shutdown time.
368 nlm_shutdown_hosts(void)
370 struct hlist_head
*chain
;
371 struct hlist_node
*pos
;
372 struct nlm_host
*host
;
374 dprintk("lockd: shutting down host module\n");
375 mutex_lock(&nlm_host_mutex
);
377 /* First, make all hosts eligible for gc */
378 dprintk("lockd: nuking all hosts...\n");
379 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
380 hlist_for_each_entry(host
, pos
, chain
, h_hash
)
381 host
->h_expires
= jiffies
- 1;
384 /* Then, perform a garbage collection pass */
386 mutex_unlock(&nlm_host_mutex
);
388 /* complain if any hosts are left */
390 printk(KERN_WARNING
"lockd: couldn't shutdown host module!\n");
391 dprintk("lockd: %d hosts left:\n", nrhosts
);
392 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
393 hlist_for_each_entry(host
, pos
, chain
, h_hash
) {
394 dprintk(" %s (cnt %d use %d exp %ld)\n",
395 host
->h_name
, atomic_read(&host
->h_count
),
396 host
->h_inuse
, host
->h_expires
);
403 * Garbage collect any unused NLM hosts.
404 * This GC combines reference counting for async operations with
405 * mark & sweep for resources held by remote clients.
410 struct hlist_head
*chain
;
411 struct hlist_node
*pos
, *next
;
412 struct nlm_host
*host
;
414 dprintk("lockd: host garbage collection\n");
415 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
416 hlist_for_each_entry(host
, pos
, chain
, h_hash
)
420 /* Mark all hosts that hold locks, blocks or shares */
421 nlmsvc_mark_resources();
423 for (chain
= nlm_hosts
; chain
< nlm_hosts
+ NLM_HOST_NRHASH
; ++chain
) {
424 hlist_for_each_entry_safe(host
, pos
, next
, chain
, h_hash
) {
425 if (atomic_read(&host
->h_count
) || host
->h_inuse
426 || time_before(jiffies
, host
->h_expires
)) {
427 dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
428 host
->h_name
, atomic_read(&host
->h_count
),
429 host
->h_inuse
, host
->h_expires
);
432 dprintk("lockd: delete host %s\n", host
->h_name
);
433 hlist_del_init(&host
->h_hash
);
435 nlm_destroy_host(host
);
440 next_gc
= jiffies
+ NLM_HOST_COLLECT
;
447 static LIST_HEAD(nsm_handles
);
448 static DEFINE_MUTEX(nsm_mutex
);
450 static struct nsm_handle
*
451 __nsm_find(const struct sockaddr_in
*sin
,
452 const char *hostname
, int hostname_len
,
455 struct nsm_handle
*nsm
= NULL
;
456 struct list_head
*pos
;
461 if (hostname
&& memchr(hostname
, '/', hostname_len
) != NULL
) {
462 if (printk_ratelimit()) {
463 printk(KERN_WARNING
"Invalid hostname \"%.*s\" "
464 "in NFS lock request\n",
465 hostname_len
, hostname
);
470 mutex_lock(&nsm_mutex
);
471 list_for_each(pos
, &nsm_handles
) {
472 nsm
= list_entry(pos
, struct nsm_handle
, sm_link
);
474 if (hostname
&& nsm_use_hostnames
) {
475 if (strlen(nsm
->sm_name
) != hostname_len
476 || memcmp(nsm
->sm_name
, hostname
, hostname_len
))
478 } else if (!nlm_cmp_addr(&nsm
->sm_addr
, sin
))
480 atomic_inc(&nsm
->sm_count
);
489 nsm
= kzalloc(sizeof(*nsm
) + hostname_len
+ 1, GFP_KERNEL
);
492 nsm
->sm_name
= (char *) (nsm
+ 1);
493 memcpy(nsm
->sm_name
, hostname
, hostname_len
);
494 nsm
->sm_name
[hostname_len
] = '\0';
495 atomic_set(&nsm
->sm_count
, 1);
497 list_add(&nsm
->sm_link
, &nsm_handles
);
501 mutex_unlock(&nsm_mutex
);
505 static struct nsm_handle
*
506 nsm_find(const struct sockaddr_in
*sin
, const char *hostname
, int hostname_len
)
508 return __nsm_find(sin
, hostname
, hostname_len
, 1);
512 * Release an NSM handle
515 nsm_release(struct nsm_handle
*nsm
)
519 if (atomic_dec_and_test(&nsm
->sm_count
)) {
520 mutex_lock(&nsm_mutex
);
521 if (atomic_read(&nsm
->sm_count
) == 0) {
522 list_del(&nsm
->sm_link
);
525 mutex_unlock(&nsm_mutex
);