host.c 18 KB

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