host.c 18 KB

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