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