host.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. const struct cred *cred;
  53. };
  54. /*
  55. * Hash function must work well on big- and little-endian platforms
  56. */
  57. static unsigned int __nlm_hash32(const __be32 n)
  58. {
  59. unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
  60. return hash ^ (hash >> 8);
  61. }
  62. static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
  63. {
  64. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  65. return __nlm_hash32(sin->sin_addr.s_addr);
  66. }
  67. static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
  68. {
  69. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  70. const struct in6_addr addr = sin6->sin6_addr;
  71. return __nlm_hash32(addr.s6_addr32[0]) ^
  72. __nlm_hash32(addr.s6_addr32[1]) ^
  73. __nlm_hash32(addr.s6_addr32[2]) ^
  74. __nlm_hash32(addr.s6_addr32[3]);
  75. }
  76. static unsigned int nlm_hash_address(const struct sockaddr *sap)
  77. {
  78. unsigned int hash;
  79. switch (sap->sa_family) {
  80. case AF_INET:
  81. hash = __nlm_hash_addr4(sap);
  82. break;
  83. case AF_INET6:
  84. hash = __nlm_hash_addr6(sap);
  85. break;
  86. default:
  87. hash = 0;
  88. }
  89. return hash & (NLM_HOST_NRHASH - 1);
  90. }
  91. /*
  92. * Allocate and initialize an nlm_host. Common to both client and server.
  93. */
  94. static struct nlm_host *nlm_alloc_host(struct nlm_lookup_host_info *ni,
  95. struct nsm_handle *nsm)
  96. {
  97. struct nlm_host *host = NULL;
  98. unsigned long now = jiffies;
  99. if (nsm != NULL)
  100. refcount_inc(&nsm->sm_count);
  101. else {
  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. host->h_cred = get_cred(ni->cred);
  145. strscpy(host->nodename, utsname()->nodename, sizeof(host->nodename));
  146. out:
  147. return host;
  148. }
  149. /*
  150. * Destroy an nlm_host and free associated resources
  151. *
  152. * Caller must hold nlm_host_mutex.
  153. */
  154. static void nlm_destroy_host_locked(struct nlm_host *host)
  155. {
  156. struct rpc_clnt *clnt;
  157. struct lockd_net *ln = net_generic(host->net, lockd_net_id);
  158. dprintk("lockd: destroy host %s\n", host->h_name);
  159. hlist_del_init(&host->h_hash);
  160. nsm_unmonitor(host);
  161. nsm_release(host->h_nsmhandle);
  162. clnt = host->h_rpcclnt;
  163. if (clnt != NULL)
  164. rpc_shutdown_client(clnt);
  165. put_cred(host->h_cred);
  166. kfree(host);
  167. ln->nrhosts--;
  168. nrhosts--;
  169. }
  170. /**
  171. * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
  172. * @sap: network address of server
  173. * @salen: length of server address
  174. * @protocol: transport protocol to use
  175. * @version: NLM protocol version
  176. * @hostname: '\0'-terminated hostname of server
  177. * @noresvport: 1 if non-privileged port should be used
  178. * @net: pointer to net namespace
  179. * @cred: pointer to cred
  180. *
  181. * Returns an nlm_host structure that matches the passed-in
  182. * [server address, transport protocol, NLM version, server hostname].
  183. * If one doesn't already exist in the host cache, a new handle is
  184. * created and returned.
  185. */
  186. struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
  187. const size_t salen,
  188. const unsigned short protocol,
  189. const u32 version,
  190. const char *hostname,
  191. int noresvport,
  192. struct net *net,
  193. const struct cred *cred)
  194. {
  195. struct nlm_lookup_host_info ni = {
  196. .server = 0,
  197. .sap = sap,
  198. .salen = salen,
  199. .protocol = protocol,
  200. .version = version,
  201. .hostname = hostname,
  202. .hostname_len = strlen(hostname),
  203. .noresvport = noresvport,
  204. .net = net,
  205. .cred = cred,
  206. };
  207. struct hlist_head *chain;
  208. struct nlm_host *host;
  209. struct nsm_handle *nsm = NULL;
  210. struct lockd_net *ln = net_generic(net, lockd_net_id);
  211. dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
  212. (hostname ? hostname : "<none>"), version,
  213. (protocol == IPPROTO_UDP ? "udp" : "tcp"));
  214. mutex_lock(&nlm_host_mutex);
  215. chain = &nlm_client_hosts[nlm_hash_address(sap)];
  216. hlist_for_each_entry(host, chain, h_hash) {
  217. if (host->net != net)
  218. continue;
  219. if (!rpc_cmp_addr(nlm_addr(host), sap))
  220. continue;
  221. /* Same address. Share an NSM handle if we already have one */
  222. if (nsm == NULL)
  223. nsm = host->h_nsmhandle;
  224. if (host->h_proto != protocol)
  225. continue;
  226. if (host->h_version != version)
  227. continue;
  228. nlm_get_host(host);
  229. dprintk("lockd: %s found host %s (%s)\n", __func__,
  230. host->h_name, host->h_addrbuf);
  231. goto out;
  232. }
  233. host = nlm_alloc_host(&ni, nsm);
  234. if (unlikely(host == NULL))
  235. goto out;
  236. hlist_add_head(&host->h_hash, chain);
  237. ln->nrhosts++;
  238. nrhosts++;
  239. dprintk("lockd: %s created host %s (%s)\n", __func__,
  240. host->h_name, host->h_addrbuf);
  241. out:
  242. mutex_unlock(&nlm_host_mutex);
  243. return host;
  244. }
  245. /**
  246. * nlmclnt_release_host - release client nlm_host
  247. * @host: nlm_host to release
  248. *
  249. */
  250. void nlmclnt_release_host(struct nlm_host *host)
  251. {
  252. if (host == NULL)
  253. return;
  254. dprintk("lockd: release client host %s\n", host->h_name);
  255. WARN_ON_ONCE(host->h_server);
  256. if (refcount_dec_and_mutex_lock(&host->h_count, &nlm_host_mutex)) {
  257. WARN_ON_ONCE(!list_empty(&host->h_lockowners));
  258. WARN_ON_ONCE(!list_empty(&host->h_granted));
  259. WARN_ON_ONCE(!list_empty(&host->h_reclaim));
  260. nlm_destroy_host_locked(host);
  261. mutex_unlock(&nlm_host_mutex);
  262. }
  263. }
  264. /**
  265. * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
  266. * @rqstp: incoming NLM request
  267. * @hostname: name of client host
  268. * @hostname_len: length of client hostname
  269. *
  270. * Returns an nlm_host structure that matches the [client address,
  271. * transport protocol, NLM version, client hostname] of the passed-in
  272. * NLM request. If one doesn't already exist in the host cache, a
  273. * new handle is created and returned.
  274. *
  275. * Before possibly creating a new nlm_host, construct a sockaddr
  276. * for a specific source address in case the local system has
  277. * multiple network addresses. The family of the address in
  278. * rq_daddr is guaranteed to be the same as the family of the
  279. * address in rq_addr, so it's safe to use the same family for
  280. * the source address.
  281. */
  282. struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
  283. const char *hostname,
  284. const size_t hostname_len)
  285. {
  286. struct hlist_head *chain;
  287. struct nlm_host *host = NULL;
  288. struct nsm_handle *nsm = NULL;
  289. struct sockaddr *src_sap = svc_daddr(rqstp);
  290. size_t src_len = rqstp->rq_daddrlen;
  291. struct net *net = SVC_NET(rqstp);
  292. struct nlm_lookup_host_info ni = {
  293. .server = 1,
  294. .sap = svc_addr(rqstp),
  295. .salen = rqstp->rq_addrlen,
  296. .protocol = rqstp->rq_prot,
  297. .version = rqstp->rq_vers,
  298. .hostname = hostname,
  299. .hostname_len = hostname_len,
  300. .net = net,
  301. };
  302. struct lockd_net *ln = net_generic(net, lockd_net_id);
  303. dprintk("lockd: %s(host='%.*s', vers=%u, proto=%s)\n", __func__,
  304. (int)hostname_len, hostname, rqstp->rq_vers,
  305. (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
  306. mutex_lock(&nlm_host_mutex);
  307. if (time_after_eq(jiffies, ln->next_gc))
  308. nlm_gc_hosts(net);
  309. chain = &nlm_server_hosts[nlm_hash_address(ni.sap)];
  310. hlist_for_each_entry(host, chain, h_hash) {
  311. if (host->net != net)
  312. continue;
  313. if (!rpc_cmp_addr(nlm_addr(host), ni.sap))
  314. continue;
  315. /* Same address. Share an NSM handle if we already have one */
  316. if (nsm == NULL)
  317. nsm = host->h_nsmhandle;
  318. if (host->h_proto != ni.protocol)
  319. continue;
  320. if (host->h_version != ni.version)
  321. continue;
  322. if (!rpc_cmp_addr(nlm_srcaddr(host), src_sap))
  323. continue;
  324. /* Move to head of hash chain. */
  325. hlist_del(&host->h_hash);
  326. hlist_add_head(&host->h_hash, chain);
  327. nlm_get_host(host);
  328. dprintk("lockd: %s found host %s (%s)\n",
  329. __func__, host->h_name, host->h_addrbuf);
  330. goto out;
  331. }
  332. host = nlm_alloc_host(&ni, nsm);
  333. if (unlikely(host == NULL))
  334. goto out;
  335. memcpy(nlm_srcaddr(host), src_sap, src_len);
  336. host->h_srcaddrlen = src_len;
  337. hlist_add_head(&host->h_hash, chain);
  338. ln->nrhosts++;
  339. nrhosts++;
  340. refcount_inc(&host->h_count);
  341. dprintk("lockd: %s created host %s (%s)\n",
  342. __func__, host->h_name, host->h_addrbuf);
  343. out:
  344. mutex_unlock(&nlm_host_mutex);
  345. return host;
  346. }
  347. /**
  348. * nlmsvc_release_host - release server nlm_host
  349. * @host: nlm_host to release
  350. *
  351. * Host is destroyed later in nlm_gc_host().
  352. */
  353. void nlmsvc_release_host(struct nlm_host *host)
  354. {
  355. if (host == NULL)
  356. return;
  357. dprintk("lockd: release server host %s\n", host->h_name);
  358. WARN_ON_ONCE(!host->h_server);
  359. refcount_dec(&host->h_count);
  360. }
  361. /*
  362. * Create the NLM RPC client for an NLM peer
  363. */
  364. struct rpc_clnt *
  365. nlm_bind_host(struct nlm_host *host)
  366. {
  367. struct rpc_clnt *clnt;
  368. dprintk("lockd: nlm_bind_host %s (%s)\n",
  369. host->h_name, host->h_addrbuf);
  370. /* Lock host handle */
  371. mutex_lock(&host->h_mutex);
  372. /* If we've already created an RPC client, check whether
  373. * RPC rebind is required
  374. */
  375. if ((clnt = host->h_rpcclnt) != NULL) {
  376. nlm_rebind_host(host);
  377. } else {
  378. unsigned long increment = nlm_timeout * HZ;
  379. struct rpc_timeout timeparms = {
  380. .to_initval = increment,
  381. .to_increment = increment,
  382. .to_maxval = increment * 6UL,
  383. .to_retries = 5U,
  384. };
  385. struct rpc_create_args args = {
  386. .net = host->net,
  387. .protocol = host->h_proto,
  388. .address = nlm_addr(host),
  389. .addrsize = host->h_addrlen,
  390. .timeout = &timeparms,
  391. .servername = host->h_name,
  392. .program = &nlm_program,
  393. .version = host->h_version,
  394. .authflavor = RPC_AUTH_UNIX,
  395. .flags = (RPC_CLNT_CREATE_NOPING |
  396. RPC_CLNT_CREATE_AUTOBIND |
  397. RPC_CLNT_CREATE_REUSEPORT),
  398. .cred = host->h_cred,
  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. * nlm_rebind_host - If needed, force a portmap lookup of the peer's lockd port
  424. * @host: NLM host handle for peer
  425. *
  426. * This is not needed when using a connection-oriented protocol, such as TCP.
  427. * The existing autobind mechanism is sufficient to force a rebind when
  428. * required, e.g. on connection state transitions.
  429. */
  430. void
  431. nlm_rebind_host(struct nlm_host *host)
  432. {
  433. if (host->h_proto != IPPROTO_UDP)
  434. return;
  435. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  436. rpc_force_rebind(host->h_rpcclnt);
  437. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  438. }
  439. }
  440. /*
  441. * Increment NLM host count
  442. */
  443. struct nlm_host * nlm_get_host(struct nlm_host *host)
  444. {
  445. if (host) {
  446. dprintk("lockd: get host %s\n", host->h_name);
  447. refcount_inc(&host->h_count);
  448. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  449. }
  450. return host;
  451. }
  452. static struct nlm_host *next_host_state(struct hlist_head *cache,
  453. struct nsm_handle *nsm,
  454. const struct nlm_reboot *info)
  455. {
  456. struct nlm_host *host;
  457. struct hlist_head *chain;
  458. mutex_lock(&nlm_host_mutex);
  459. for_each_host(host, chain, cache) {
  460. if (host->h_nsmhandle == nsm
  461. && host->h_nsmstate != info->state) {
  462. host->h_nsmstate = info->state;
  463. host->h_state++;
  464. nlm_get_host(host);
  465. mutex_unlock(&nlm_host_mutex);
  466. return host;
  467. }
  468. }
  469. mutex_unlock(&nlm_host_mutex);
  470. return NULL;
  471. }
  472. /**
  473. * nlm_host_rebooted - Release all resources held by rebooted host
  474. * @net: network namespace
  475. * @info: pointer to decoded results of NLM_SM_NOTIFY call
  476. *
  477. * We were notified that the specified host has rebooted. Release
  478. * all resources held by that peer.
  479. */
  480. void nlm_host_rebooted(const struct net *net, const struct nlm_reboot *info)
  481. {
  482. struct nsm_handle *nsm;
  483. struct nlm_host *host;
  484. nsm = nsm_reboot_lookup(net, info);
  485. if (unlikely(nsm == NULL))
  486. return;
  487. /* Mark all hosts tied to this NSM state as having rebooted.
  488. * We run the loop repeatedly, because we drop the host table
  489. * lock for this.
  490. * To avoid processing a host several times, we match the nsmstate.
  491. */
  492. while ((host = next_host_state(nlm_server_hosts, nsm, info)) != NULL) {
  493. nlmsvc_free_host_resources(host);
  494. nlmsvc_release_host(host);
  495. }
  496. while ((host = next_host_state(nlm_client_hosts, nsm, info)) != NULL) {
  497. nlmclnt_recovery(host);
  498. nlmclnt_release_host(host);
  499. }
  500. nsm_release(nsm);
  501. }
  502. static void nlm_complain_hosts(struct net *net)
  503. {
  504. struct hlist_head *chain;
  505. struct nlm_host *host;
  506. if (net) {
  507. struct lockd_net *ln = net_generic(net, lockd_net_id);
  508. if (ln->nrhosts == 0)
  509. return;
  510. pr_warn("lockd: couldn't shutdown host module for net %x!\n",
  511. net->ns.inum);
  512. dprintk("lockd: %lu hosts left in net %x:\n", ln->nrhosts,
  513. net->ns.inum);
  514. } else {
  515. if (nrhosts == 0)
  516. return;
  517. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  518. dprintk("lockd: %lu hosts left:\n", nrhosts);
  519. }
  520. for_each_host(host, chain, nlm_server_hosts) {
  521. if (net && host->net != net)
  522. continue;
  523. dprintk(" %s (cnt %d use %d exp %ld net %x)\n",
  524. host->h_name, refcount_read(&host->h_count),
  525. host->h_inuse, host->h_expires, host->net->ns.inum);
  526. }
  527. }
  528. void
  529. nlm_shutdown_hosts_net(struct net *net)
  530. {
  531. struct hlist_head *chain;
  532. struct nlm_host *host;
  533. mutex_lock(&nlm_host_mutex);
  534. /* First, make all hosts eligible for gc */
  535. dprintk("lockd: nuking all hosts in net %x...\n",
  536. net ? net->ns.inum : 0);
  537. for_each_host(host, chain, nlm_server_hosts) {
  538. if (net && host->net != net)
  539. continue;
  540. host->h_expires = jiffies - 1;
  541. if (host->h_rpcclnt) {
  542. rpc_shutdown_client(host->h_rpcclnt);
  543. host->h_rpcclnt = NULL;
  544. }
  545. nlmsvc_free_host_resources(host);
  546. }
  547. /* Then, perform a garbage collection pass */
  548. nlm_gc_hosts(net);
  549. nlm_complain_hosts(net);
  550. mutex_unlock(&nlm_host_mutex);
  551. }
  552. /*
  553. * Shut down the hosts module.
  554. * Note that this routine is called only at server shutdown time.
  555. */
  556. void
  557. nlm_shutdown_hosts(void)
  558. {
  559. dprintk("lockd: shutting down host module\n");
  560. nlm_shutdown_hosts_net(NULL);
  561. }
  562. /*
  563. * Garbage collect any unused NLM hosts.
  564. * This GC combines reference counting for async operations with
  565. * mark & sweep for resources held by remote clients.
  566. */
  567. static void
  568. nlm_gc_hosts(struct net *net)
  569. {
  570. struct hlist_head *chain;
  571. struct hlist_node *next;
  572. struct nlm_host *host;
  573. dprintk("lockd: host garbage collection for net %x\n",
  574. net ? net->ns.inum : 0);
  575. for_each_host(host, chain, nlm_server_hosts) {
  576. if (net && host->net != net)
  577. continue;
  578. host->h_inuse = 0;
  579. }
  580. /* Mark all hosts that hold locks, blocks or shares */
  581. nlmsvc_mark_resources(net);
  582. for_each_host_safe(host, next, chain, nlm_server_hosts) {
  583. if (net && host->net != net)
  584. continue;
  585. if (host->h_inuse || time_before(jiffies, host->h_expires)) {
  586. dprintk("nlm_gc_hosts skipping %s "
  587. "(cnt %d use %d exp %ld net %x)\n",
  588. host->h_name, refcount_read(&host->h_count),
  589. host->h_inuse, host->h_expires,
  590. host->net->ns.inum);
  591. continue;
  592. }
  593. if (refcount_dec_if_one(&host->h_count))
  594. nlm_destroy_host_locked(host);
  595. }
  596. if (net) {
  597. struct lockd_net *ln = net_generic(net, lockd_net_id);
  598. ln->next_gc = jiffies + NLM_HOST_COLLECT;
  599. }
  600. }