host.c 17 KB

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