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