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