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