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> | |
12 | #include <linux/sched.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/in.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> | |
353ab6e9 | 19 | #include <linux/mutex.h> |
1da177e4 LT |
20 | |
21 | ||
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) | |
1da177e4 LT |
29 | |
30 | static struct nlm_host * nlm_hosts[NLM_HOST_NRHASH]; | |
31 | static unsigned long next_gc; | |
32 | static int nrhosts; | |
353ab6e9 | 33 | static DEFINE_MUTEX(nlm_host_mutex); |
1da177e4 LT |
34 | |
35 | ||
36 | static void nlm_gc_hosts(void); | |
8dead0db OK |
37 | static struct nsm_handle * __nsm_find(const struct sockaddr_in *, |
38 | const char *, int, int); | |
1da177e4 LT |
39 | |
40 | /* | |
41 | * Find an NLM server handle in the cache. If there is none, create it. | |
42 | */ | |
43 | struct nlm_host * | |
db4e4c9a OK |
44 | nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version, |
45 | const char *hostname, int hostname_len) | |
1da177e4 | 46 | { |
db4e4c9a OK |
47 | return nlm_lookup_host(0, sin, proto, version, |
48 | hostname, hostname_len); | |
1da177e4 LT |
49 | } |
50 | ||
51 | /* | |
52 | * Find an NLM client handle in the cache. If there is none, create it. | |
53 | */ | |
54 | struct nlm_host * | |
db4e4c9a OK |
55 | nlmsvc_lookup_host(struct svc_rqst *rqstp, |
56 | const char *hostname, int hostname_len) | |
1da177e4 LT |
57 | { |
58 | return nlm_lookup_host(1, &rqstp->rq_addr, | |
db4e4c9a OK |
59 | rqstp->rq_prot, rqstp->rq_vers, |
60 | hostname, hostname_len); | |
1da177e4 LT |
61 | } |
62 | ||
63 | /* | |
64 | * Common host lookup routine for server & client | |
65 | */ | |
66 | struct nlm_host * | |
cf712c24 | 67 | nlm_lookup_host(int server, const struct sockaddr_in *sin, |
db4e4c9a OK |
68 | int proto, int version, |
69 | const char *hostname, | |
70 | int hostname_len) | |
1da177e4 LT |
71 | { |
72 | struct nlm_host *host, **hp; | |
8dead0db | 73 | struct nsm_handle *nsm = NULL; |
1da177e4 LT |
74 | int hash; |
75 | ||
db4e4c9a OK |
76 | dprintk("lockd: nlm_lookup_host(%u.%u.%u.%u, p=%d, v=%d, my role=%s, name=%.*s)\n", |
77 | NIPQUAD(sin->sin_addr.s_addr), proto, version, | |
78 | server? "server" : "client", | |
79 | hostname_len, | |
80 | hostname? hostname : "<none>"); | |
81 | ||
1da177e4 LT |
82 | |
83 | hash = NLM_ADDRHASH(sin->sin_addr.s_addr); | |
84 | ||
85 | /* Lock hash table */ | |
353ab6e9 | 86 | mutex_lock(&nlm_host_mutex); |
1da177e4 LT |
87 | |
88 | if (time_after_eq(jiffies, next_gc)) | |
89 | nlm_gc_hosts(); | |
90 | ||
8dead0db OK |
91 | /* We may keep several nlm_host objects for a peer, because each |
92 | * nlm_host is identified by | |
93 | * (address, protocol, version, server/client) | |
94 | * We could probably simplify this a little by putting all those | |
95 | * different NLM rpc_clients into one single nlm_host object. | |
96 | * This would allow us to have one nlm_host per address. | |
97 | */ | |
1da177e4 | 98 | for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) { |
8dead0db OK |
99 | if (!nlm_cmp_addr(&host->h_addr, sin)) |
100 | continue; | |
101 | ||
102 | /* See if we have an NSM handle for this client */ | |
103 | if (!nsm && (nsm = host->h_nsmhandle) != 0) | |
104 | atomic_inc(&nsm->sm_count); | |
105 | ||
1da177e4 LT |
106 | if (host->h_proto != proto) |
107 | continue; | |
108 | if (host->h_version != version) | |
109 | continue; | |
110 | if (host->h_server != server) | |
111 | continue; | |
112 | ||
f0737a39 OK |
113 | if (hp != nlm_hosts + hash) { |
114 | *hp = host->h_next; | |
115 | host->h_next = nlm_hosts[hash]; | |
116 | nlm_hosts[hash] = host; | |
1da177e4 | 117 | } |
f0737a39 OK |
118 | nlm_get_host(host); |
119 | goto out; | |
1da177e4 LT |
120 | } |
121 | ||
8dead0db OK |
122 | /* Sadly, the host isn't in our hash table yet. See if |
123 | * we have an NSM handle for it. If not, create one. | |
124 | */ | |
125 | if (!nsm && !(nsm = nsm_find(sin, hostname, hostname_len))) | |
126 | goto out; | |
1da177e4 | 127 | |
f8314dc6 | 128 | host = kzalloc(sizeof(*host), GFP_KERNEL); |
8dead0db OK |
129 | if (!host) { |
130 | nsm_release(nsm); | |
131 | goto out; | |
132 | } | |
133 | host->h_name = nsm->sm_name; | |
1da177e4 LT |
134 | host->h_addr = *sin; |
135 | host->h_addr.sin_port = 0; /* ouch! */ | |
136 | host->h_version = version; | |
137 | host->h_proto = proto; | |
138 | host->h_rpcclnt = NULL; | |
50467914 | 139 | mutex_init(&host->h_mutex); |
1da177e4 LT |
140 | host->h_nextrebind = jiffies + NLM_HOST_REBIND; |
141 | host->h_expires = jiffies + NLM_HOST_EXPIRE; | |
142 | atomic_set(&host->h_count, 1); | |
143 | init_waitqueue_head(&host->h_gracewait); | |
28df955a | 144 | init_rwsem(&host->h_rwsem); |
1da177e4 LT |
145 | host->h_state = 0; /* pseudo NSM state */ |
146 | host->h_nsmstate = 0; /* real NSM state */ | |
8dead0db | 147 | host->h_nsmhandle = nsm; |
1da177e4 LT |
148 | host->h_server = server; |
149 | host->h_next = nlm_hosts[hash]; | |
150 | nlm_hosts[hash] = host; | |
151 | INIT_LIST_HEAD(&host->h_lockowners); | |
152 | spin_lock_init(&host->h_lock); | |
26bcbf96 CH |
153 | INIT_LIST_HEAD(&host->h_granted); |
154 | INIT_LIST_HEAD(&host->h_reclaim); | |
1da177e4 LT |
155 | |
156 | if (++nrhosts > NLM_HOST_MAX) | |
157 | next_gc = 0; | |
158 | ||
8dead0db | 159 | out: |
353ab6e9 | 160 | mutex_unlock(&nlm_host_mutex); |
1da177e4 LT |
161 | return host; |
162 | } | |
163 | ||
164 | struct nlm_host * | |
165 | nlm_find_client(void) | |
166 | { | |
167 | /* find a nlm_host for a client for which h_killed == 0. | |
168 | * and return it | |
169 | */ | |
170 | int hash; | |
353ab6e9 | 171 | mutex_lock(&nlm_host_mutex); |
1da177e4 LT |
172 | for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) { |
173 | struct nlm_host *host, **hp; | |
174 | for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) { | |
175 | if (host->h_server && | |
176 | host->h_killed == 0) { | |
177 | nlm_get_host(host); | |
353ab6e9 | 178 | mutex_unlock(&nlm_host_mutex); |
1da177e4 LT |
179 | return host; |
180 | } | |
181 | } | |
182 | } | |
353ab6e9 | 183 | mutex_unlock(&nlm_host_mutex); |
1da177e4 LT |
184 | return NULL; |
185 | } | |
186 | ||
187 | ||
188 | /* | |
189 | * Create the NLM RPC client for an NLM peer | |
190 | */ | |
191 | struct rpc_clnt * | |
192 | nlm_bind_host(struct nlm_host *host) | |
193 | { | |
194 | struct rpc_clnt *clnt; | |
1da177e4 LT |
195 | |
196 | dprintk("lockd: nlm_bind_host(%08x)\n", | |
197 | (unsigned)ntohl(host->h_addr.sin_addr.s_addr)); | |
198 | ||
199 | /* Lock host handle */ | |
50467914 | 200 | mutex_lock(&host->h_mutex); |
1da177e4 LT |
201 | |
202 | /* If we've already created an RPC client, check whether | |
203 | * RPC rebind is required | |
1da177e4 LT |
204 | */ |
205 | if ((clnt = host->h_rpcclnt) != NULL) { | |
43118c29 | 206 | if (time_after_eq(jiffies, host->h_nextrebind)) { |
35f5a422 | 207 | rpc_force_rebind(clnt); |
1da177e4 LT |
208 | host->h_nextrebind = jiffies + NLM_HOST_REBIND; |
209 | dprintk("lockd: next rebind in %ld jiffies\n", | |
210 | host->h_nextrebind - jiffies); | |
211 | } | |
212 | } else { | |
e1ec7892 CL |
213 | unsigned long increment = nlmsvc_timeout * HZ; |
214 | struct rpc_timeout timeparms = { | |
215 | .to_initval = increment, | |
216 | .to_increment = increment, | |
217 | .to_maxval = increment * 6UL, | |
218 | .to_retries = 5U, | |
219 | }; | |
220 | struct rpc_create_args args = { | |
221 | .protocol = host->h_proto, | |
222 | .address = (struct sockaddr *)&host->h_addr, | |
223 | .addrsize = sizeof(host->h_addr), | |
224 | .timeout = &timeparms, | |
225 | .servername = host->h_name, | |
226 | .program = &nlm_program, | |
227 | .version = host->h_version, | |
228 | .authflavor = RPC_AUTH_UNIX, | |
229 | .flags = (RPC_CLNT_CREATE_HARDRTRY | | |
230 | RPC_CLNT_CREATE_AUTOBIND), | |
231 | }; | |
232 | ||
233 | clnt = rpc_create(&args); | |
234 | if (!IS_ERR(clnt)) | |
235 | host->h_rpcclnt = clnt; | |
236 | else { | |
237 | printk("lockd: couldn't create RPC handle for %s\n", host->h_name); | |
238 | clnt = NULL; | |
239 | } | |
1da177e4 LT |
240 | } |
241 | ||
50467914 | 242 | mutex_unlock(&host->h_mutex); |
1da177e4 | 243 | return clnt; |
1da177e4 LT |
244 | } |
245 | ||
246 | /* | |
247 | * Force a portmap lookup of the remote lockd port | |
248 | */ | |
249 | void | |
250 | nlm_rebind_host(struct nlm_host *host) | |
251 | { | |
252 | dprintk("lockd: rebind host %s\n", host->h_name); | |
253 | if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) { | |
35f5a422 | 254 | rpc_force_rebind(host->h_rpcclnt); |
1da177e4 LT |
255 | host->h_nextrebind = jiffies + NLM_HOST_REBIND; |
256 | } | |
257 | } | |
258 | ||
259 | /* | |
260 | * Increment NLM host count | |
261 | */ | |
262 | struct nlm_host * nlm_get_host(struct nlm_host *host) | |
263 | { | |
264 | if (host) { | |
265 | dprintk("lockd: get host %s\n", host->h_name); | |
266 | atomic_inc(&host->h_count); | |
267 | host->h_expires = jiffies + NLM_HOST_EXPIRE; | |
268 | } | |
269 | return host; | |
270 | } | |
271 | ||
272 | /* | |
273 | * Release NLM host after use | |
274 | */ | |
275 | void nlm_release_host(struct nlm_host *host) | |
276 | { | |
277 | if (host != NULL) { | |
278 | dprintk("lockd: release host %s\n", host->h_name); | |
1da177e4 | 279 | BUG_ON(atomic_read(&host->h_count) < 0); |
4c060b53 TM |
280 | if (atomic_dec_and_test(&host->h_count)) { |
281 | BUG_ON(!list_empty(&host->h_lockowners)); | |
282 | BUG_ON(!list_empty(&host->h_granted)); | |
283 | BUG_ON(!list_empty(&host->h_reclaim)); | |
284 | } | |
1da177e4 LT |
285 | } |
286 | } | |
287 | ||
cf712c24 OK |
288 | /* |
289 | * We were notified that the host indicated by address &sin | |
290 | * has rebooted. | |
291 | * Release all resources held by that peer. | |
292 | */ | |
293 | void nlm_host_rebooted(const struct sockaddr_in *sin, const struct nlm_reboot *argp) | |
294 | { | |
295 | struct nlm_host *host; | |
db4e4c9a | 296 | int server; |
cf712c24 OK |
297 | |
298 | /* Obtain the host pointer for this NFS server and try to | |
299 | * reclaim all locks we hold on this server. | |
300 | */ | |
db4e4c9a OK |
301 | server = (argp->proto & 1)? 1 : 0; |
302 | host = nlm_lookup_host(server, sin, argp->proto >> 1, argp->vers, | |
303 | argp->mon, argp->len); | |
304 | if (host == NULL) | |
305 | return; | |
306 | ||
307 | if (server == 0) { | |
cf712c24 | 308 | /* We are client, he's the server: try to reclaim all locks. */ |
cf712c24 OK |
309 | nlmclnt_recovery(host, argp->state); |
310 | } else { | |
311 | /* He's the client, we're the server: delete all locks held by the client */ | |
cf712c24 OK |
312 | nlmsvc_free_host_resources(host); |
313 | } | |
314 | nlm_release_host(host); | |
315 | } | |
316 | ||
1da177e4 LT |
317 | /* |
318 | * Shut down the hosts module. | |
319 | * Note that this routine is called only at server shutdown time. | |
320 | */ | |
321 | void | |
322 | nlm_shutdown_hosts(void) | |
323 | { | |
324 | struct nlm_host *host; | |
325 | int i; | |
326 | ||
327 | dprintk("lockd: shutting down host module\n"); | |
353ab6e9 | 328 | mutex_lock(&nlm_host_mutex); |
1da177e4 LT |
329 | |
330 | /* First, make all hosts eligible for gc */ | |
331 | dprintk("lockd: nuking all hosts...\n"); | |
332 | for (i = 0; i < NLM_HOST_NRHASH; i++) { | |
333 | for (host = nlm_hosts[i]; host; host = host->h_next) | |
334 | host->h_expires = jiffies - 1; | |
335 | } | |
336 | ||
337 | /* Then, perform a garbage collection pass */ | |
338 | nlm_gc_hosts(); | |
353ab6e9 | 339 | mutex_unlock(&nlm_host_mutex); |
1da177e4 LT |
340 | |
341 | /* complain if any hosts are left */ | |
342 | if (nrhosts) { | |
343 | printk(KERN_WARNING "lockd: couldn't shutdown host module!\n"); | |
344 | dprintk("lockd: %d hosts left:\n", nrhosts); | |
345 | for (i = 0; i < NLM_HOST_NRHASH; i++) { | |
346 | for (host = nlm_hosts[i]; host; host = host->h_next) { | |
347 | dprintk(" %s (cnt %d use %d exp %ld)\n", | |
348 | host->h_name, atomic_read(&host->h_count), | |
349 | host->h_inuse, host->h_expires); | |
350 | } | |
351 | } | |
352 | } | |
353 | } | |
354 | ||
355 | /* | |
356 | * Garbage collect any unused NLM hosts. | |
357 | * This GC combines reference counting for async operations with | |
358 | * mark & sweep for resources held by remote clients. | |
359 | */ | |
360 | static void | |
361 | nlm_gc_hosts(void) | |
362 | { | |
363 | struct nlm_host **q, *host; | |
364 | struct rpc_clnt *clnt; | |
365 | int i; | |
366 | ||
367 | dprintk("lockd: host garbage collection\n"); | |
368 | for (i = 0; i < NLM_HOST_NRHASH; i++) { | |
369 | for (host = nlm_hosts[i]; host; host = host->h_next) | |
370 | host->h_inuse = 0; | |
371 | } | |
372 | ||
373 | /* Mark all hosts that hold locks, blocks or shares */ | |
374 | nlmsvc_mark_resources(); | |
375 | ||
376 | for (i = 0; i < NLM_HOST_NRHASH; i++) { | |
377 | q = &nlm_hosts[i]; | |
378 | while ((host = *q) != NULL) { | |
379 | if (atomic_read(&host->h_count) || host->h_inuse | |
380 | || time_before(jiffies, host->h_expires)) { | |
381 | dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n", | |
382 | host->h_name, atomic_read(&host->h_count), | |
383 | host->h_inuse, host->h_expires); | |
384 | q = &host->h_next; | |
385 | continue; | |
386 | } | |
387 | dprintk("lockd: delete host %s\n", host->h_name); | |
388 | *q = host->h_next; | |
977faf39 OK |
389 | |
390 | /* | |
391 | * Unmonitor unless host was invalidated (i.e. lockd restarted) | |
392 | */ | |
393 | nsm_unmonitor(host); | |
394 | ||
1da177e4 LT |
395 | if ((clnt = host->h_rpcclnt) != NULL) { |
396 | if (atomic_read(&clnt->cl_users)) { | |
397 | printk(KERN_WARNING | |
398 | "lockd: active RPC handle\n"); | |
399 | clnt->cl_dead = 1; | |
400 | } else { | |
401 | rpc_destroy_client(host->h_rpcclnt); | |
402 | } | |
403 | } | |
1da177e4 LT |
404 | kfree(host); |
405 | nrhosts--; | |
406 | } | |
407 | } | |
408 | ||
409 | next_gc = jiffies + NLM_HOST_COLLECT; | |
410 | } | |
411 | ||
8dead0db OK |
412 | |
413 | /* | |
414 | * Manage NSM handles | |
415 | */ | |
416 | static LIST_HEAD(nsm_handles); | |
417 | static DECLARE_MUTEX(nsm_sema); | |
418 | ||
419 | static struct nsm_handle * | |
420 | __nsm_find(const struct sockaddr_in *sin, | |
421 | const char *hostname, int hostname_len, | |
422 | int create) | |
423 | { | |
424 | struct nsm_handle *nsm = NULL; | |
425 | struct list_head *pos; | |
426 | ||
427 | if (!sin) | |
428 | return NULL; | |
429 | ||
430 | if (hostname && memchr(hostname, '/', hostname_len) != NULL) { | |
431 | if (printk_ratelimit()) { | |
432 | printk(KERN_WARNING "Invalid hostname \"%.*s\" " | |
433 | "in NFS lock request\n", | |
434 | hostname_len, hostname); | |
435 | } | |
436 | return NULL; | |
437 | } | |
438 | ||
439 | down(&nsm_sema); | |
440 | list_for_each(pos, &nsm_handles) { | |
441 | nsm = list_entry(pos, struct nsm_handle, sm_link); | |
442 | ||
443 | if (!nlm_cmp_addr(&nsm->sm_addr, sin)) | |
444 | continue; | |
445 | atomic_inc(&nsm->sm_count); | |
446 | goto out; | |
447 | } | |
448 | ||
449 | if (!create) { | |
450 | nsm = NULL; | |
451 | goto out; | |
452 | } | |
453 | ||
454 | nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL); | |
455 | if (nsm != NULL) { | |
456 | nsm->sm_addr = *sin; | |
457 | nsm->sm_name = (char *) (nsm + 1); | |
458 | memcpy(nsm->sm_name, hostname, hostname_len); | |
459 | nsm->sm_name[hostname_len] = '\0'; | |
460 | atomic_set(&nsm->sm_count, 1); | |
461 | ||
462 | list_add(&nsm->sm_link, &nsm_handles); | |
463 | } | |
464 | ||
465 | out: up(&nsm_sema); | |
466 | return nsm; | |
467 | } | |
468 | ||
469 | struct nsm_handle * | |
470 | nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len) | |
471 | { | |
472 | return __nsm_find(sin, hostname, hostname_len, 1); | |
473 | } | |
474 | ||
475 | /* | |
476 | * Release an NSM handle | |
477 | */ | |
478 | void | |
479 | nsm_release(struct nsm_handle *nsm) | |
480 | { | |
481 | if (!nsm) | |
482 | return; | |
483 | if (atomic_dec_and_test(&nsm->sm_count)) { | |
484 | down(&nsm_sema); | |
485 | if (atomic_read(&nsm->sm_count) == 0) { | |
486 | list_del(&nsm->sm_link); | |
487 | kfree(nsm); | |
488 | } | |
489 | up(&nsm_sema); | |
490 | } | |
491 | } |