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