Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
1da177e4 LT |
12 | #include <linux/slab.h> |
13 | #include <linux/in.h> | |
1b333c54 | 14 | #include <linux/in6.h> |
1da177e4 LT |
15 | #include <linux/sunrpc/clnt.h> |
16 | #include <linux/sunrpc/svc.h> | |
17 | #include <linux/lockd/lockd.h> | |
353ab6e9 | 18 | #include <linux/mutex.h> |
1da177e4 | 19 | |
1b333c54 | 20 | #include <net/ipv6.h> |
1da177e4 LT |
21 | |
22 | #define NLMDBG_FACILITY NLMDBG_HOSTCACHE | |
1da177e4 | 23 | #define NLM_HOST_NRHASH 32 |
1da177e4 | 24 | #define NLM_HOST_REBIND (60 * HZ) |
1447d25e N |
25 | #define NLM_HOST_EXPIRE (300 * HZ) |
26 | #define NLM_HOST_COLLECT (120 * HZ) | |
1da177e4 | 27 | |
0cea3276 | 28 | static struct hlist_head nlm_hosts[NLM_HOST_NRHASH]; |
8ea6ecc8 | 29 | static struct hlist_head nlm_client_hosts[NLM_HOST_NRHASH]; |
b1137468 BF |
30 | |
31 | #define for_each_host(host, pos, chain, table) \ | |
32 | for ((chain) = (table); \ | |
33 | (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \ | |
34 | hlist_for_each_entry((host), (pos), (chain), h_hash) | |
35 | ||
36 | #define for_each_host_safe(host, pos, next, chain, table) \ | |
37 | for ((chain) = (table); \ | |
38 | (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \ | |
39 | hlist_for_each_entry_safe((host), (pos), (next), \ | |
40 | (chain), h_hash) | |
41 | ||
1da177e4 LT |
42 | static unsigned long next_gc; |
43 | static int nrhosts; | |
353ab6e9 | 44 | static DEFINE_MUTEX(nlm_host_mutex); |
1da177e4 | 45 | |
1da177e4 | 46 | static void nlm_gc_hosts(void); |
1da177e4 | 47 | |
7f1ed18b CL |
48 | struct nlm_lookup_host_info { |
49 | const int server; /* search for server|client */ | |
88541c84 CL |
50 | const struct sockaddr *sap; /* address to search for */ |
51 | const size_t salen; /* it's length */ | |
7f1ed18b CL |
52 | const unsigned short protocol; /* transport to search for*/ |
53 | const u32 version; /* NLM version to search for */ | |
54 | const char *hostname; /* remote's hostname */ | |
55 | const size_t hostname_len; /* it's length */ | |
88541c84 | 56 | const struct sockaddr *src_sap; /* our address (optional) */ |
7f1ed18b | 57 | const size_t src_len; /* it's length */ |
0cb2659b | 58 | const int noresvport; /* use non-priv port */ |
7f1ed18b CL |
59 | }; |
60 | ||
ede2fea0 CL |
61 | /* |
62 | * Hash function must work well on big- and little-endian platforms | |
63 | */ | |
64 | static unsigned int __nlm_hash32(const __be32 n) | |
65 | { | |
66 | unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16); | |
67 | return hash ^ (hash >> 8); | |
68 | } | |
69 | ||
70 | static unsigned int __nlm_hash_addr4(const struct sockaddr *sap) | |
71 | { | |
72 | const struct sockaddr_in *sin = (struct sockaddr_in *)sap; | |
73 | return __nlm_hash32(sin->sin_addr.s_addr); | |
74 | } | |
75 | ||
76 | static unsigned int __nlm_hash_addr6(const struct sockaddr *sap) | |
77 | { | |
78 | const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; | |
79 | const struct in6_addr addr = sin6->sin6_addr; | |
80 | return __nlm_hash32(addr.s6_addr32[0]) ^ | |
81 | __nlm_hash32(addr.s6_addr32[1]) ^ | |
82 | __nlm_hash32(addr.s6_addr32[2]) ^ | |
83 | __nlm_hash32(addr.s6_addr32[3]); | |
84 | } | |
85 | ||
86 | static unsigned int nlm_hash_address(const struct sockaddr *sap) | |
87 | { | |
88 | unsigned int hash; | |
89 | ||
90 | switch (sap->sa_family) { | |
91 | case AF_INET: | |
92 | hash = __nlm_hash_addr4(sap); | |
93 | break; | |
94 | case AF_INET6: | |
95 | hash = __nlm_hash_addr6(sap); | |
96 | break; | |
97 | default: | |
98 | hash = 0; | |
99 | } | |
100 | return hash & (NLM_HOST_NRHASH - 1); | |
101 | } | |
102 | ||
a7952f40 CL |
103 | /* |
104 | * Allocate and initialize an nlm_host. Common to both client and server. | |
105 | */ | |
106 | static struct nlm_host *nlm_alloc_host(struct nlm_lookup_host_info *ni, | |
107 | struct nsm_handle *nsm) | |
108 | { | |
109 | struct nlm_host *host = NULL; | |
110 | unsigned long now = jiffies; | |
111 | ||
112 | if (nsm != NULL) | |
113 | atomic_inc(&nsm->sm_count); | |
114 | else { | |
115 | host = NULL; | |
116 | nsm = nsm_get_handle(ni->sap, ni->salen, | |
117 | ni->hostname, ni->hostname_len); | |
118 | if (unlikely(nsm == NULL)) { | |
119 | dprintk("lockd: %s failed; no nsm handle\n", | |
120 | __func__); | |
121 | goto out; | |
122 | } | |
123 | } | |
124 | ||
125 | host = kmalloc(sizeof(*host), GFP_KERNEL); | |
126 | if (unlikely(host == NULL)) { | |
127 | dprintk("lockd: %s failed; no memory\n", __func__); | |
128 | nsm_release(nsm); | |
129 | goto out; | |
130 | } | |
131 | ||
132 | memcpy(nlm_addr(host), ni->sap, ni->salen); | |
133 | host->h_addrlen = ni->salen; | |
134 | rpc_set_port(nlm_addr(host), 0); | |
135 | host->h_srcaddrlen = 0; | |
136 | ||
137 | host->h_rpcclnt = NULL; | |
138 | host->h_name = nsm->sm_name; | |
139 | host->h_version = ni->version; | |
140 | host->h_proto = ni->protocol; | |
141 | host->h_reclaiming = 0; | |
142 | host->h_server = ni->server; | |
143 | host->h_noresvport = ni->noresvport; | |
144 | host->h_inuse = 0; | |
145 | init_waitqueue_head(&host->h_gracewait); | |
146 | init_rwsem(&host->h_rwsem); | |
147 | host->h_state = 0; | |
148 | host->h_nsmstate = 0; | |
149 | host->h_pidcount = 0; | |
150 | atomic_set(&host->h_count, 1); | |
151 | mutex_init(&host->h_mutex); | |
152 | host->h_nextrebind = now + NLM_HOST_REBIND; | |
153 | host->h_expires = now + NLM_HOST_EXPIRE; | |
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 | host->h_nsmhandle = nsm; | |
159 | host->h_addrbuf = nsm->sm_addrbuf; | |
160 | ||
161 | out: | |
162 | return host; | |
163 | } | |
164 | ||
1da177e4 LT |
165 | /* |
166 | * Common host lookup routine for server & client | |
167 | */ | |
7f1ed18b | 168 | static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni) |
1da177e4 | 169 | { |
0cea3276 OK |
170 | struct hlist_head *chain; |
171 | struct hlist_node *pos; | |
172 | struct nlm_host *host; | |
8dead0db | 173 | struct nsm_handle *nsm = NULL; |
1da177e4 | 174 | |
353ab6e9 | 175 | mutex_lock(&nlm_host_mutex); |
1da177e4 LT |
176 | |
177 | if (time_after_eq(jiffies, next_gc)) | |
178 | nlm_gc_hosts(); | |
179 | ||
8dead0db OK |
180 | /* We may keep several nlm_host objects for a peer, because each |
181 | * nlm_host is identified by | |
182 | * (address, protocol, version, server/client) | |
183 | * We could probably simplify this a little by putting all those | |
184 | * different NLM rpc_clients into one single nlm_host object. | |
185 | * This would allow us to have one nlm_host per address. | |
186 | */ | |
88541c84 | 187 | chain = &nlm_hosts[nlm_hash_address(ni->sap)]; |
0cea3276 | 188 | hlist_for_each_entry(host, pos, chain, h_hash) { |
4516fc04 | 189 | if (!rpc_cmp_addr(nlm_addr(host), ni->sap)) |
8dead0db OK |
190 | continue; |
191 | ||
192 | /* See if we have an NSM handle for this client */ | |
6b54dae2 N |
193 | if (!nsm) |
194 | nsm = host->h_nsmhandle; | |
8dead0db | 195 | |
7f1ed18b | 196 | if (host->h_proto != ni->protocol) |
1da177e4 | 197 | continue; |
7f1ed18b | 198 | if (host->h_version != ni->version) |
1da177e4 | 199 | continue; |
7f1ed18b | 200 | if (host->h_server != ni->server) |
1da177e4 | 201 | continue; |
8e35f8e7 | 202 | if (ni->server && ni->src_len != 0 && |
4516fc04 | 203 | !rpc_cmp_addr(nlm_srcaddr(host), ni->src_sap)) |
c98451bd | 204 | continue; |
1da177e4 | 205 | |
0cea3276 OK |
206 | /* Move to head of hash chain. */ |
207 | hlist_del(&host->h_hash); | |
208 | hlist_add_head(&host->h_hash, chain); | |
209 | ||
f0737a39 | 210 | nlm_get_host(host); |
1b333c54 CL |
211 | dprintk("lockd: nlm_lookup_host found host %s (%s)\n", |
212 | host->h_name, host->h_addrbuf); | |
f0737a39 | 213 | goto out; |
1da177e4 LT |
214 | } |
215 | ||
a7952f40 CL |
216 | host = nlm_alloc_host(ni, nsm); |
217 | if (unlikely(host == NULL)) | |
8dead0db | 218 | goto out; |
a7952f40 | 219 | |
88541c84 | 220 | memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len); |
8e35f8e7 | 221 | host->h_srcaddrlen = ni->src_len; |
0cea3276 | 222 | hlist_add_head(&host->h_hash, chain); |
1da177e4 | 223 | |
1447d25e | 224 | nrhosts++; |
1b333c54 | 225 | |
1b333c54 CL |
226 | dprintk("lockd: nlm_lookup_host created host %s\n", |
227 | host->h_name); | |
228 | ||
8dead0db | 229 | out: |
353ab6e9 | 230 | mutex_unlock(&nlm_host_mutex); |
1da177e4 LT |
231 | return host; |
232 | } | |
233 | ||
c53c1bb9 | 234 | /* |
723bb5b5 CL |
235 | * Destroy an nlm_host and free associated resources |
236 | * | |
237 | * Caller must hold nlm_host_mutex. | |
c53c1bb9 | 238 | */ |
723bb5b5 | 239 | static void nlm_destroy_host_locked(struct nlm_host *host) |
c53c1bb9 OK |
240 | { |
241 | struct rpc_clnt *clnt; | |
242 | ||
723bb5b5 CL |
243 | dprintk("lockd: destroy host %s\n", host->h_name); |
244 | ||
c53c1bb9 OK |
245 | BUG_ON(!list_empty(&host->h_lockowners)); |
246 | BUG_ON(atomic_read(&host->h_count)); | |
247 | ||
723bb5b5 CL |
248 | hlist_del_init(&host->h_hash); |
249 | ||
c53c1bb9 | 250 | nsm_unmonitor(host); |
c8c23c42 | 251 | nsm_release(host->h_nsmhandle); |
c53c1bb9 | 252 | |
34f52e35 TM |
253 | clnt = host->h_rpcclnt; |
254 | if (clnt != NULL) | |
255 | rpc_shutdown_client(clnt); | |
c53c1bb9 | 256 | kfree(host); |
723bb5b5 CL |
257 | |
258 | nrhosts--; | |
c53c1bb9 OK |
259 | } |
260 | ||
d7d20440 CL |
261 | /** |
262 | * nlmclnt_lookup_host - Find an NLM host handle matching a remote server | |
263 | * @sap: network address of server | |
264 | * @salen: length of server address | |
265 | * @protocol: transport protocol to use | |
266 | * @version: NLM protocol version | |
267 | * @hostname: '\0'-terminated hostname of server | |
0cb2659b | 268 | * @noresvport: 1 if non-privileged port should be used |
d7d20440 CL |
269 | * |
270 | * Returns an nlm_host structure that matches the passed-in | |
271 | * [server address, transport protocol, NLM version, server hostname]. | |
272 | * If one doesn't already exist in the host cache, a new handle is | |
273 | * created and returned. | |
c585646d | 274 | */ |
d7d20440 CL |
275 | struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap, |
276 | const size_t salen, | |
277 | const unsigned short protocol, | |
0cb2659b CL |
278 | const u32 version, |
279 | const char *hostname, | |
280 | int noresvport) | |
c585646d | 281 | { |
7f1ed18b CL |
282 | struct nlm_lookup_host_info ni = { |
283 | .server = 0, | |
d7d20440 CL |
284 | .sap = sap, |
285 | .salen = salen, | |
286 | .protocol = protocol, | |
7f1ed18b CL |
287 | .version = version, |
288 | .hostname = hostname, | |
d7d20440 | 289 | .hostname_len = strlen(hostname), |
0cb2659b | 290 | .noresvport = noresvport, |
7f1ed18b | 291 | }; |
8ea6ecc8 CL |
292 | struct hlist_head *chain; |
293 | struct hlist_node *pos; | |
294 | struct nlm_host *host; | |
295 | struct nsm_handle *nsm = NULL; | |
c98451bd | 296 | |
7f1ed18b CL |
297 | dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__, |
298 | (hostname ? hostname : "<none>"), version, | |
d7d20440 | 299 | (protocol == IPPROTO_UDP ? "udp" : "tcp")); |
7f1ed18b | 300 | |
8ea6ecc8 CL |
301 | mutex_lock(&nlm_host_mutex); |
302 | ||
303 | chain = &nlm_client_hosts[nlm_hash_address(sap)]; | |
304 | hlist_for_each_entry(host, pos, chain, h_hash) { | |
305 | if (!rpc_cmp_addr(nlm_addr(host), sap)) | |
306 | continue; | |
307 | ||
308 | /* Same address. Share an NSM handle if we already have one */ | |
309 | if (nsm == NULL) | |
310 | nsm = host->h_nsmhandle; | |
311 | ||
312 | if (host->h_proto != protocol) | |
313 | continue; | |
314 | if (host->h_version != version) | |
315 | continue; | |
316 | ||
317 | nlm_get_host(host); | |
318 | dprintk("lockd: %s found host %s (%s)\n", __func__, | |
319 | host->h_name, host->h_addrbuf); | |
320 | goto out; | |
321 | } | |
322 | ||
323 | host = nlm_alloc_host(&ni, nsm); | |
324 | if (unlikely(host == NULL)) | |
325 | goto out; | |
326 | ||
327 | hlist_add_head(&host->h_hash, chain); | |
328 | nrhosts++; | |
329 | ||
330 | dprintk("lockd: %s created host %s (%s)\n", __func__, | |
331 | host->h_name, host->h_addrbuf); | |
332 | ||
333 | out: | |
334 | mutex_unlock(&nlm_host_mutex); | |
335 | return host; | |
336 | } | |
337 | ||
338 | /** | |
339 | * nlmclnt_release_host - release client nlm_host | |
340 | * @host: nlm_host to release | |
341 | * | |
342 | */ | |
343 | void nlmclnt_release_host(struct nlm_host *host) | |
344 | { | |
345 | if (host == NULL) | |
346 | return; | |
347 | ||
348 | dprintk("lockd: release client host %s\n", host->h_name); | |
349 | ||
350 | BUG_ON(atomic_read(&host->h_count) < 0); | |
351 | BUG_ON(host->h_server); | |
352 | ||
353 | if (atomic_dec_and_test(&host->h_count)) { | |
354 | BUG_ON(!list_empty(&host->h_lockowners)); | |
355 | BUG_ON(!list_empty(&host->h_granted)); | |
356 | BUG_ON(!list_empty(&host->h_reclaim)); | |
357 | ||
358 | mutex_lock(&nlm_host_mutex); | |
359 | nlm_destroy_host_locked(host); | |
360 | mutex_unlock(&nlm_host_mutex); | |
361 | } | |
c585646d AB |
362 | } |
363 | ||
6bfbe8af CL |
364 | /** |
365 | * nlmsvc_lookup_host - Find an NLM host handle matching a remote client | |
366 | * @rqstp: incoming NLM request | |
367 | * @hostname: name of client host | |
368 | * @hostname_len: length of client hostname | |
369 | * | |
370 | * Returns an nlm_host structure that matches the [client address, | |
371 | * transport protocol, NLM version, client hostname] of the passed-in | |
372 | * NLM request. If one doesn't already exist in the host cache, a | |
373 | * new handle is created and returned. | |
374 | * | |
375 | * Before possibly creating a new nlm_host, construct a sockaddr | |
376 | * for a specific source address in case the local system has | |
377 | * multiple network addresses. The family of the address in | |
378 | * rq_daddr is guaranteed to be the same as the family of the | |
379 | * address in rq_addr, so it's safe to use the same family for | |
380 | * the source address. | |
c585646d | 381 | */ |
6bfbe8af CL |
382 | struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp, |
383 | const char *hostname, | |
384 | const size_t hostname_len) | |
c585646d | 385 | { |
67216b94 CL |
386 | struct hlist_head *chain; |
387 | struct hlist_node *pos; | |
388 | struct nlm_host *host = NULL; | |
389 | struct nsm_handle *nsm = NULL; | |
6bfbe8af | 390 | struct sockaddr_in sin = { |
2860a022 | 391 | .sin_family = AF_INET, |
6bfbe8af CL |
392 | }; |
393 | struct sockaddr_in6 sin6 = { | |
394 | .sin6_family = AF_INET6, | |
2860a022 | 395 | }; |
7f1ed18b CL |
396 | struct nlm_lookup_host_info ni = { |
397 | .server = 1, | |
88541c84 CL |
398 | .sap = svc_addr(rqstp), |
399 | .salen = rqstp->rq_addrlen, | |
7f1ed18b CL |
400 | .protocol = rqstp->rq_prot, |
401 | .version = rqstp->rq_vers, | |
402 | .hostname = hostname, | |
403 | .hostname_len = hostname_len, | |
6bfbe8af | 404 | .src_len = rqstp->rq_addrlen, |
7f1ed18b CL |
405 | }; |
406 | ||
407 | dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__, | |
408 | (int)hostname_len, hostname, rqstp->rq_vers, | |
409 | (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp")); | |
c98451bd | 410 | |
67216b94 CL |
411 | mutex_lock(&nlm_host_mutex); |
412 | ||
6bfbe8af CL |
413 | switch (ni.sap->sa_family) { |
414 | case AF_INET: | |
415 | sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr; | |
416 | ni.src_sap = (struct sockaddr *)&sin; | |
417 | break; | |
418 | case AF_INET6: | |
419 | ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6); | |
420 | ni.src_sap = (struct sockaddr *)&sin6; | |
421 | break; | |
422 | default: | |
67216b94 CL |
423 | dprintk("lockd: %s failed; unrecognized address family\n", |
424 | __func__); | |
425 | goto out; | |
6bfbe8af CL |
426 | } |
427 | ||
67216b94 CL |
428 | if (time_after_eq(jiffies, next_gc)) |
429 | nlm_gc_hosts(); | |
430 | ||
431 | chain = &nlm_hosts[nlm_hash_address(ni.sap)]; | |
432 | hlist_for_each_entry(host, pos, chain, h_hash) { | |
433 | if (!rpc_cmp_addr(nlm_addr(host), ni.sap)) | |
434 | continue; | |
435 | ||
436 | /* Same address. Share an NSM handle if we already have one */ | |
437 | if (nsm == NULL) | |
438 | nsm = host->h_nsmhandle; | |
439 | ||
440 | if (host->h_proto != ni.protocol) | |
441 | continue; | |
442 | if (host->h_version != ni.version) | |
443 | continue; | |
444 | if (!rpc_cmp_addr(nlm_srcaddr(host), ni.src_sap)) | |
445 | continue; | |
446 | ||
447 | /* Move to head of hash chain. */ | |
448 | hlist_del(&host->h_hash); | |
449 | hlist_add_head(&host->h_hash, chain); | |
450 | ||
451 | nlm_get_host(host); | |
452 | dprintk("lockd: %s found host %s (%s)\n", | |
453 | __func__, host->h_name, host->h_addrbuf); | |
454 | goto out; | |
455 | } | |
456 | ||
457 | host = nlm_alloc_host(&ni, nsm); | |
458 | if (unlikely(host == NULL)) | |
459 | goto out; | |
460 | ||
461 | memcpy(nlm_srcaddr(host), ni.src_sap, ni.src_len); | |
462 | host->h_srcaddrlen = ni.src_len; | |
463 | hlist_add_head(&host->h_hash, chain); | |
464 | nrhosts++; | |
465 | ||
466 | dprintk("lockd: %s created host %s (%s)\n", | |
467 | __func__, host->h_name, host->h_addrbuf); | |
468 | ||
469 | out: | |
470 | mutex_unlock(&nlm_host_mutex); | |
471 | return host; | |
472 | } | |
473 | ||
474 | /** | |
475 | * nlmsvc_release_host - release server nlm_host | |
476 | * @host: nlm_host to release | |
477 | * | |
478 | * Host is destroyed later in nlm_gc_host(). | |
479 | */ | |
480 | void nlmsvc_release_host(struct nlm_host *host) | |
481 | { | |
482 | if (host == NULL) | |
483 | return; | |
484 | ||
485 | dprintk("lockd: release server host %s\n", host->h_name); | |
486 | ||
487 | BUG_ON(atomic_read(&host->h_count) < 0); | |
488 | BUG_ON(!host->h_server); | |
489 | atomic_dec(&host->h_count); | |
c585646d AB |
490 | } |
491 | ||
1da177e4 LT |
492 | /* |
493 | * Create the NLM RPC client for an NLM peer | |
494 | */ | |
495 | struct rpc_clnt * | |
496 | nlm_bind_host(struct nlm_host *host) | |
497 | { | |
498 | struct rpc_clnt *clnt; | |
1da177e4 | 499 | |
1df40b60 CL |
500 | dprintk("lockd: nlm_bind_host %s (%s)\n", |
501 | host->h_name, host->h_addrbuf); | |
1da177e4 LT |
502 | |
503 | /* Lock host handle */ | |
50467914 | 504 | mutex_lock(&host->h_mutex); |
1da177e4 LT |
505 | |
506 | /* If we've already created an RPC client, check whether | |
507 | * RPC rebind is required | |
1da177e4 LT |
508 | */ |
509 | if ((clnt = host->h_rpcclnt) != NULL) { | |
43118c29 | 510 | if (time_after_eq(jiffies, host->h_nextrebind)) { |
35f5a422 | 511 | rpc_force_rebind(clnt); |
1da177e4 | 512 | host->h_nextrebind = jiffies + NLM_HOST_REBIND; |
1b333c54 | 513 | dprintk("lockd: next rebind in %lu jiffies\n", |
1da177e4 LT |
514 | host->h_nextrebind - jiffies); |
515 | } | |
516 | } else { | |
21051ba6 | 517 | unsigned long increment = nlmsvc_timeout; |
e1ec7892 CL |
518 | struct rpc_timeout timeparms = { |
519 | .to_initval = increment, | |
520 | .to_increment = increment, | |
521 | .to_maxval = increment * 6UL, | |
522 | .to_retries = 5U, | |
523 | }; | |
524 | struct rpc_create_args args = { | |
c653ce3f | 525 | .net = &init_net, |
e1ec7892 | 526 | .protocol = host->h_proto, |
b4ed58fd CL |
527 | .address = nlm_addr(host), |
528 | .addrsize = host->h_addrlen, | |
e1ec7892 CL |
529 | .timeout = &timeparms, |
530 | .servername = host->h_name, | |
531 | .program = &nlm_program, | |
532 | .version = host->h_version, | |
533 | .authflavor = RPC_AUTH_UNIX, | |
90bd17c8 | 534 | .flags = (RPC_CLNT_CREATE_NOPING | |
e1ec7892 CL |
535 | RPC_CLNT_CREATE_AUTOBIND), |
536 | }; | |
537 | ||
90bd17c8 JL |
538 | /* |
539 | * lockd retries server side blocks automatically so we want | |
540 | * those to be soft RPC calls. Client side calls need to be | |
541 | * hard RPC tasks. | |
542 | */ | |
543 | if (!host->h_server) | |
544 | args.flags |= RPC_CLNT_CREATE_HARDRTRY; | |
0cb2659b CL |
545 | if (host->h_noresvport) |
546 | args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; | |
8e35f8e7 TM |
547 | if (host->h_srcaddrlen) |
548 | args.saddress = nlm_srcaddr(host); | |
90bd17c8 | 549 | |
e1ec7892 CL |
550 | clnt = rpc_create(&args); |
551 | if (!IS_ERR(clnt)) | |
552 | host->h_rpcclnt = clnt; | |
553 | else { | |
554 | printk("lockd: couldn't create RPC handle for %s\n", host->h_name); | |
555 | clnt = NULL; | |
556 | } | |
1da177e4 LT |
557 | } |
558 | ||
50467914 | 559 | mutex_unlock(&host->h_mutex); |
1da177e4 | 560 | return clnt; |
1da177e4 LT |
561 | } |
562 | ||
563 | /* | |
564 | * Force a portmap lookup of the remote lockd port | |
565 | */ | |
566 | void | |
567 | nlm_rebind_host(struct nlm_host *host) | |
568 | { | |
569 | dprintk("lockd: rebind host %s\n", host->h_name); | |
570 | if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) { | |
35f5a422 | 571 | rpc_force_rebind(host->h_rpcclnt); |
1da177e4 LT |
572 | host->h_nextrebind = jiffies + NLM_HOST_REBIND; |
573 | } | |
574 | } | |
575 | ||
576 | /* | |
577 | * Increment NLM host count | |
578 | */ | |
579 | struct nlm_host * nlm_get_host(struct nlm_host *host) | |
580 | { | |
581 | if (host) { | |
582 | dprintk("lockd: get host %s\n", host->h_name); | |
583 | atomic_inc(&host->h_count); | |
584 | host->h_expires = jiffies + NLM_HOST_EXPIRE; | |
585 | } | |
586 | return host; | |
587 | } | |
588 | ||
b10e30f6 BF |
589 | static struct nlm_host *next_host_state(struct hlist_head *cache, |
590 | struct nsm_handle *nsm, | |
591 | const struct nlm_reboot *info) | |
592 | { | |
593 | struct nlm_host *host = NULL; | |
594 | struct hlist_head *chain; | |
595 | struct hlist_node *pos; | |
596 | ||
597 | mutex_lock(&nlm_host_mutex); | |
598 | for_each_host(host, pos, chain, cache) { | |
599 | if (host->h_nsmhandle == nsm | |
600 | && host->h_nsmstate != info->state) { | |
601 | host->h_nsmstate = info->state; | |
602 | host->h_state++; | |
603 | ||
604 | nlm_get_host(host); | |
605 | mutex_unlock(&nlm_host_mutex); | |
606 | goto out; | |
607 | } | |
608 | } | |
609 | out: | |
610 | mutex_unlock(&nlm_host_mutex); | |
611 | return host; | |
612 | } | |
613 | ||
7fefc9cb CL |
614 | /** |
615 | * nlm_host_rebooted - Release all resources held by rebooted host | |
616 | * @info: pointer to decoded results of NLM_SM_NOTIFY call | |
617 | * | |
618 | * We were notified that the specified host has rebooted. Release | |
619 | * all resources held by that peer. | |
cf712c24 | 620 | */ |
7fefc9cb | 621 | void nlm_host_rebooted(const struct nlm_reboot *info) |
cf712c24 | 622 | { |
5c8dd29c | 623 | struct nsm_handle *nsm; |
0cea3276 | 624 | struct nlm_host *host; |
cf712c24 | 625 | |
8c7378fd CL |
626 | nsm = nsm_reboot_lookup(info); |
627 | if (unlikely(nsm == NULL)) | |
db4e4c9a | 628 | return; |
5c8dd29c OK |
629 | |
630 | /* Mark all hosts tied to this NSM state as having rebooted. | |
631 | * We run the loop repeatedly, because we drop the host table | |
632 | * lock for this. | |
633 | * To avoid processing a host several times, we match the nsmstate. | |
634 | */ | |
b10e30f6 | 635 | while ((host = next_host_state(nlm_hosts, nsm, info)) != NULL) { |
8ea6ecc8 | 636 | nlmsvc_free_host_resources(host); |
67216b94 | 637 | nlmsvc_release_host(host); |
cf712c24 | 638 | } |
8ea6ecc8 CL |
639 | while ((host = next_host_state(nlm_client_hosts, nsm, info)) != NULL) { |
640 | nlmclnt_recovery(host); | |
641 | nlmclnt_release_host(host); | |
642 | } | |
643 | ||
cdd30fa1 | 644 | nsm_release(nsm); |
cf712c24 OK |
645 | } |
646 | ||
1da177e4 LT |
647 | /* |
648 | * Shut down the hosts module. | |
649 | * Note that this routine is called only at server shutdown time. | |
650 | */ | |
651 | void | |
652 | nlm_shutdown_hosts(void) | |
653 | { | |
0cea3276 OK |
654 | struct hlist_head *chain; |
655 | struct hlist_node *pos; | |
1da177e4 | 656 | struct nlm_host *host; |
1da177e4 LT |
657 | |
658 | dprintk("lockd: shutting down host module\n"); | |
353ab6e9 | 659 | mutex_lock(&nlm_host_mutex); |
1da177e4 LT |
660 | |
661 | /* First, make all hosts eligible for gc */ | |
662 | dprintk("lockd: nuking all hosts...\n"); | |
b1137468 BF |
663 | for_each_host(host, pos, chain, nlm_hosts) { |
664 | host->h_expires = jiffies - 1; | |
665 | if (host->h_rpcclnt) { | |
666 | rpc_shutdown_client(host->h_rpcclnt); | |
667 | host->h_rpcclnt = NULL; | |
d801b861 | 668 | } |
1da177e4 LT |
669 | } |
670 | ||
671 | /* Then, perform a garbage collection pass */ | |
672 | nlm_gc_hosts(); | |
353ab6e9 | 673 | mutex_unlock(&nlm_host_mutex); |
1da177e4 LT |
674 | |
675 | /* complain if any hosts are left */ | |
676 | if (nrhosts) { | |
677 | printk(KERN_WARNING "lockd: couldn't shutdown host module!\n"); | |
678 | dprintk("lockd: %d hosts left:\n", nrhosts); | |
b1137468 BF |
679 | for_each_host(host, pos, chain, nlm_hosts) { |
680 | dprintk(" %s (cnt %d use %d exp %ld)\n", | |
681 | host->h_name, atomic_read(&host->h_count), | |
682 | host->h_inuse, host->h_expires); | |
1da177e4 LT |
683 | } |
684 | } | |
685 | } | |
686 | ||
687 | /* | |
688 | * Garbage collect any unused NLM hosts. | |
689 | * This GC combines reference counting for async operations with | |
690 | * mark & sweep for resources held by remote clients. | |
691 | */ | |
692 | static void | |
693 | nlm_gc_hosts(void) | |
694 | { | |
0cea3276 OK |
695 | struct hlist_head *chain; |
696 | struct hlist_node *pos, *next; | |
697 | struct nlm_host *host; | |
1da177e4 LT |
698 | |
699 | dprintk("lockd: host garbage collection\n"); | |
b1137468 BF |
700 | for_each_host(host, pos, chain, nlm_hosts) |
701 | host->h_inuse = 0; | |
1da177e4 LT |
702 | |
703 | /* Mark all hosts that hold locks, blocks or shares */ | |
704 | nlmsvc_mark_resources(); | |
705 | ||
b1137468 BF |
706 | for_each_host_safe(host, pos, next, chain, nlm_hosts) { |
707 | if (atomic_read(&host->h_count) || host->h_inuse | |
708 | || time_before(jiffies, host->h_expires)) { | |
709 | dprintk("nlm_gc_hosts skipping %s " | |
710 | "(cnt %d use %d exp %ld)\n", | |
711 | host->h_name, atomic_read(&host->h_count), | |
712 | host->h_inuse, host->h_expires); | |
713 | continue; | |
1da177e4 | 714 | } |
723bb5b5 | 715 | nlm_destroy_host_locked(host); |
1da177e4 LT |
716 | } |
717 | ||
718 | next_gc = jiffies + NLM_HOST_COLLECT; | |
719 | } |