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