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