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