host.c 17 KB

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