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/mutex.h>
  18. #include <net/ipv6.h>
  19. #define NLMDBG_FACILITY NLMDBG_HOSTCACHE
  20. #define NLM_HOST_NRHASH 32
  21. #define NLM_HOST_REBIND (60 * HZ)
  22. #define NLM_HOST_EXPIRE (300 * HZ)
  23. #define NLM_HOST_COLLECT (120 * HZ)
  24. static struct hlist_head nlm_server_hosts[NLM_HOST_NRHASH];
  25. static struct hlist_head nlm_client_hosts[NLM_HOST_NRHASH];
  26. #define for_each_host(host, pos, chain, table) \
  27. for ((chain) = (table); \
  28. (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \
  29. hlist_for_each_entry((host), (pos), (chain), h_hash)
  30. #define for_each_host_safe(host, pos, next, chain, table) \
  31. for ((chain) = (table); \
  32. (chain) < (table) + NLM_HOST_NRHASH; ++(chain)) \
  33. hlist_for_each_entry_safe((host), (pos), (next), \
  34. (chain), h_hash)
  35. static unsigned long next_gc;
  36. static int nrhosts;
  37. static DEFINE_MUTEX(nlm_host_mutex);
  38. static void nlm_gc_hosts(void);
  39. struct nlm_lookup_host_info {
  40. const int server; /* search for server|client */
  41. const struct sockaddr *sap; /* address to search for */
  42. const size_t salen; /* it's length */
  43. const unsigned short protocol; /* transport to search for*/
  44. const u32 version; /* NLM version to search for */
  45. const char *hostname; /* remote's hostname */
  46. const size_t hostname_len; /* it's length */
  47. const struct sockaddr *src_sap; /* our address (optional) */
  48. const size_t src_len; /* it's length */
  49. const int noresvport; /* use non-priv port */
  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. out:
  142. return host;
  143. }
  144. /*
  145. * Common host lookup routine for server & client
  146. */
  147. static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
  148. {
  149. struct hlist_head *chain;
  150. struct hlist_node *pos;
  151. struct nlm_host *host;
  152. struct nsm_handle *nsm = NULL;
  153. mutex_lock(&nlm_host_mutex);
  154. if (time_after_eq(jiffies, next_gc))
  155. nlm_gc_hosts();
  156. /* We may keep several nlm_host objects for a peer, because each
  157. * nlm_host is identified by
  158. * (address, protocol, version, server/client)
  159. * We could probably simplify this a little by putting all those
  160. * different NLM rpc_clients into one single nlm_host object.
  161. * This would allow us to have one nlm_host per address.
  162. */
  163. chain = &nlm_server_hosts[nlm_hash_address(ni->sap)];
  164. hlist_for_each_entry(host, pos, chain, h_hash) {
  165. if (!rpc_cmp_addr(nlm_addr(host), ni->sap))
  166. continue;
  167. /* See if we have an NSM handle for this client */
  168. if (!nsm)
  169. nsm = host->h_nsmhandle;
  170. if (host->h_proto != ni->protocol)
  171. continue;
  172. if (host->h_version != ni->version)
  173. continue;
  174. if (host->h_server != ni->server)
  175. continue;
  176. if (ni->server && ni->src_len != 0 &&
  177. !rpc_cmp_addr(nlm_srcaddr(host), ni->src_sap))
  178. continue;
  179. /* Move to head of hash chain. */
  180. hlist_del(&host->h_hash);
  181. hlist_add_head(&host->h_hash, chain);
  182. nlm_get_host(host);
  183. dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
  184. host->h_name, host->h_addrbuf);
  185. goto out;
  186. }
  187. host = nlm_alloc_host(ni, nsm);
  188. if (unlikely(host == NULL))
  189. goto out;
  190. memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
  191. host->h_srcaddrlen = ni->src_len;
  192. hlist_add_head(&host->h_hash, chain);
  193. nrhosts++;
  194. dprintk("lockd: nlm_lookup_host created host %s\n",
  195. host->h_name);
  196. out:
  197. mutex_unlock(&nlm_host_mutex);
  198. return host;
  199. }
  200. /*
  201. * Destroy an nlm_host and free associated resources
  202. *
  203. * Caller must hold nlm_host_mutex.
  204. */
  205. static void nlm_destroy_host_locked(struct nlm_host *host)
  206. {
  207. struct rpc_clnt *clnt;
  208. dprintk("lockd: destroy host %s\n", host->h_name);
  209. BUG_ON(!list_empty(&host->h_lockowners));
  210. BUG_ON(atomic_read(&host->h_count));
  211. hlist_del_init(&host->h_hash);
  212. nsm_unmonitor(host);
  213. nsm_release(host->h_nsmhandle);
  214. clnt = host->h_rpcclnt;
  215. if (clnt != NULL)
  216. rpc_shutdown_client(clnt);
  217. kfree(host);
  218. nrhosts--;
  219. }
  220. /**
  221. * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
  222. * @sap: network address of server
  223. * @salen: length of server address
  224. * @protocol: transport protocol to use
  225. * @version: NLM protocol version
  226. * @hostname: '\0'-terminated hostname of server
  227. * @noresvport: 1 if non-privileged port should be used
  228. *
  229. * Returns an nlm_host structure that matches the passed-in
  230. * [server address, transport protocol, NLM version, server hostname].
  231. * If one doesn't already exist in the host cache, a new handle is
  232. * created and returned.
  233. */
  234. struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
  235. const size_t salen,
  236. const unsigned short protocol,
  237. const u32 version,
  238. const char *hostname,
  239. int noresvport)
  240. {
  241. struct nlm_lookup_host_info ni = {
  242. .server = 0,
  243. .sap = sap,
  244. .salen = salen,
  245. .protocol = protocol,
  246. .version = version,
  247. .hostname = hostname,
  248. .hostname_len = strlen(hostname),
  249. .noresvport = noresvport,
  250. };
  251. struct hlist_head *chain;
  252. struct hlist_node *pos;
  253. struct nlm_host *host;
  254. struct nsm_handle *nsm = NULL;
  255. dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
  256. (hostname ? hostname : "<none>"), version,
  257. (protocol == IPPROTO_UDP ? "udp" : "tcp"));
  258. mutex_lock(&nlm_host_mutex);
  259. chain = &nlm_client_hosts[nlm_hash_address(sap)];
  260. hlist_for_each_entry(host, pos, chain, h_hash) {
  261. if (!rpc_cmp_addr(nlm_addr(host), sap))
  262. continue;
  263. /* Same address. Share an NSM handle if we already have one */
  264. if (nsm == NULL)
  265. nsm = host->h_nsmhandle;
  266. if (host->h_proto != protocol)
  267. continue;
  268. if (host->h_version != version)
  269. continue;
  270. nlm_get_host(host);
  271. dprintk("lockd: %s found host %s (%s)\n", __func__,
  272. host->h_name, host->h_addrbuf);
  273. goto out;
  274. }
  275. host = nlm_alloc_host(&ni, nsm);
  276. if (unlikely(host == NULL))
  277. goto out;
  278. hlist_add_head(&host->h_hash, chain);
  279. nrhosts++;
  280. dprintk("lockd: %s created host %s (%s)\n", __func__,
  281. host->h_name, host->h_addrbuf);
  282. out:
  283. mutex_unlock(&nlm_host_mutex);
  284. return host;
  285. }
  286. /**
  287. * nlmclnt_release_host - release client nlm_host
  288. * @host: nlm_host to release
  289. *
  290. */
  291. void nlmclnt_release_host(struct nlm_host *host)
  292. {
  293. if (host == NULL)
  294. return;
  295. dprintk("lockd: release client host %s\n", host->h_name);
  296. BUG_ON(atomic_read(&host->h_count) < 0);
  297. BUG_ON(host->h_server);
  298. if (atomic_dec_and_test(&host->h_count)) {
  299. BUG_ON(!list_empty(&host->h_lockowners));
  300. BUG_ON(!list_empty(&host->h_granted));
  301. BUG_ON(!list_empty(&host->h_reclaim));
  302. mutex_lock(&nlm_host_mutex);
  303. nlm_destroy_host_locked(host);
  304. mutex_unlock(&nlm_host_mutex);
  305. }
  306. }
  307. /**
  308. * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
  309. * @rqstp: incoming NLM request
  310. * @hostname: name of client host
  311. * @hostname_len: length of client hostname
  312. *
  313. * Returns an nlm_host structure that matches the [client address,
  314. * transport protocol, NLM version, client hostname] of the passed-in
  315. * NLM request. If one doesn't already exist in the host cache, a
  316. * new handle is created and returned.
  317. *
  318. * Before possibly creating a new nlm_host, construct a sockaddr
  319. * for a specific source address in case the local system has
  320. * multiple network addresses. The family of the address in
  321. * rq_daddr is guaranteed to be the same as the family of the
  322. * address in rq_addr, so it's safe to use the same family for
  323. * the source address.
  324. */
  325. struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
  326. const char *hostname,
  327. const size_t hostname_len)
  328. {
  329. struct hlist_head *chain;
  330. struct hlist_node *pos;
  331. struct nlm_host *host = NULL;
  332. struct nsm_handle *nsm = NULL;
  333. struct sockaddr_in sin = {
  334. .sin_family = AF_INET,
  335. };
  336. struct sockaddr_in6 sin6 = {
  337. .sin6_family = AF_INET6,
  338. };
  339. struct nlm_lookup_host_info ni = {
  340. .server = 1,
  341. .sap = svc_addr(rqstp),
  342. .salen = rqstp->rq_addrlen,
  343. .protocol = rqstp->rq_prot,
  344. .version = rqstp->rq_vers,
  345. .hostname = hostname,
  346. .hostname_len = hostname_len,
  347. .src_len = rqstp->rq_addrlen,
  348. };
  349. dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
  350. (int)hostname_len, hostname, rqstp->rq_vers,
  351. (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
  352. mutex_lock(&nlm_host_mutex);
  353. switch (ni.sap->sa_family) {
  354. case AF_INET:
  355. sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr;
  356. ni.src_sap = (struct sockaddr *)&sin;
  357. break;
  358. case AF_INET6:
  359. ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6);
  360. ni.src_sap = (struct sockaddr *)&sin6;
  361. break;
  362. default:
  363. dprintk("lockd: %s failed; unrecognized address family\n",
  364. __func__);
  365. goto out;
  366. }
  367. if (time_after_eq(jiffies, next_gc))
  368. nlm_gc_hosts();
  369. chain = &nlm_server_hosts[nlm_hash_address(ni.sap)];
  370. hlist_for_each_entry(host, pos, chain, h_hash) {
  371. if (!rpc_cmp_addr(nlm_addr(host), ni.sap))
  372. continue;
  373. /* Same address. Share an NSM handle if we already have one */
  374. if (nsm == NULL)
  375. nsm = host->h_nsmhandle;
  376. if (host->h_proto != ni.protocol)
  377. continue;
  378. if (host->h_version != ni.version)
  379. continue;
  380. if (!rpc_cmp_addr(nlm_srcaddr(host), ni.src_sap))
  381. continue;
  382. /* Move to head of hash chain. */
  383. hlist_del(&host->h_hash);
  384. hlist_add_head(&host->h_hash, chain);
  385. nlm_get_host(host);
  386. dprintk("lockd: %s found host %s (%s)\n",
  387. __func__, host->h_name, host->h_addrbuf);
  388. goto out;
  389. }
  390. host = nlm_alloc_host(&ni, nsm);
  391. if (unlikely(host == NULL))
  392. goto out;
  393. memcpy(nlm_srcaddr(host), ni.src_sap, ni.src_len);
  394. host->h_srcaddrlen = ni.src_len;
  395. hlist_add_head(&host->h_hash, chain);
  396. nrhosts++;
  397. dprintk("lockd: %s created host %s (%s)\n",
  398. __func__, host->h_name, host->h_addrbuf);
  399. out:
  400. mutex_unlock(&nlm_host_mutex);
  401. return host;
  402. }
  403. /**
  404. * nlmsvc_release_host - release server nlm_host
  405. * @host: nlm_host to release
  406. *
  407. * Host is destroyed later in nlm_gc_host().
  408. */
  409. void nlmsvc_release_host(struct nlm_host *host)
  410. {
  411. if (host == NULL)
  412. return;
  413. dprintk("lockd: release server host %s\n", host->h_name);
  414. BUG_ON(atomic_read(&host->h_count) < 0);
  415. BUG_ON(!host->h_server);
  416. atomic_dec(&host->h_count);
  417. }
  418. /*
  419. * Create the NLM RPC client for an NLM peer
  420. */
  421. struct rpc_clnt *
  422. nlm_bind_host(struct nlm_host *host)
  423. {
  424. struct rpc_clnt *clnt;
  425. dprintk("lockd: nlm_bind_host %s (%s)\n",
  426. host->h_name, host->h_addrbuf);
  427. /* Lock host handle */
  428. mutex_lock(&host->h_mutex);
  429. /* If we've already created an RPC client, check whether
  430. * RPC rebind is required
  431. */
  432. if ((clnt = host->h_rpcclnt) != NULL) {
  433. if (time_after_eq(jiffies, host->h_nextrebind)) {
  434. rpc_force_rebind(clnt);
  435. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  436. dprintk("lockd: next rebind in %lu jiffies\n",
  437. host->h_nextrebind - jiffies);
  438. }
  439. } else {
  440. unsigned long increment = nlmsvc_timeout;
  441. struct rpc_timeout timeparms = {
  442. .to_initval = increment,
  443. .to_increment = increment,
  444. .to_maxval = increment * 6UL,
  445. .to_retries = 5U,
  446. };
  447. struct rpc_create_args args = {
  448. .net = &init_net,
  449. .protocol = host->h_proto,
  450. .address = nlm_addr(host),
  451. .addrsize = host->h_addrlen,
  452. .timeout = &timeparms,
  453. .servername = host->h_name,
  454. .program = &nlm_program,
  455. .version = host->h_version,
  456. .authflavor = RPC_AUTH_UNIX,
  457. .flags = (RPC_CLNT_CREATE_NOPING |
  458. RPC_CLNT_CREATE_AUTOBIND),
  459. };
  460. /*
  461. * lockd retries server side blocks automatically so we want
  462. * those to be soft RPC calls. Client side calls need to be
  463. * hard RPC tasks.
  464. */
  465. if (!host->h_server)
  466. args.flags |= RPC_CLNT_CREATE_HARDRTRY;
  467. if (host->h_noresvport)
  468. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  469. if (host->h_srcaddrlen)
  470. args.saddress = nlm_srcaddr(host);
  471. clnt = rpc_create(&args);
  472. if (!IS_ERR(clnt))
  473. host->h_rpcclnt = clnt;
  474. else {
  475. printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
  476. clnt = NULL;
  477. }
  478. }
  479. mutex_unlock(&host->h_mutex);
  480. return clnt;
  481. }
  482. /*
  483. * Force a portmap lookup of the remote lockd port
  484. */
  485. void
  486. nlm_rebind_host(struct nlm_host *host)
  487. {
  488. dprintk("lockd: rebind host %s\n", host->h_name);
  489. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  490. rpc_force_rebind(host->h_rpcclnt);
  491. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  492. }
  493. }
  494. /*
  495. * Increment NLM host count
  496. */
  497. struct nlm_host * nlm_get_host(struct nlm_host *host)
  498. {
  499. if (host) {
  500. dprintk("lockd: get host %s\n", host->h_name);
  501. atomic_inc(&host->h_count);
  502. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  503. }
  504. return host;
  505. }
  506. static struct nlm_host *next_host_state(struct hlist_head *cache,
  507. struct nsm_handle *nsm,
  508. const struct nlm_reboot *info)
  509. {
  510. struct nlm_host *host = NULL;
  511. struct hlist_head *chain;
  512. struct hlist_node *pos;
  513. mutex_lock(&nlm_host_mutex);
  514. for_each_host(host, pos, chain, cache) {
  515. if (host->h_nsmhandle == nsm
  516. && host->h_nsmstate != info->state) {
  517. host->h_nsmstate = info->state;
  518. host->h_state++;
  519. nlm_get_host(host);
  520. mutex_unlock(&nlm_host_mutex);
  521. goto out;
  522. }
  523. }
  524. out:
  525. mutex_unlock(&nlm_host_mutex);
  526. return host;
  527. }
  528. /**
  529. * nlm_host_rebooted - Release all resources held by rebooted host
  530. * @info: pointer to decoded results of NLM_SM_NOTIFY call
  531. *
  532. * We were notified that the specified host has rebooted. Release
  533. * all resources held by that peer.
  534. */
  535. void nlm_host_rebooted(const struct nlm_reboot *info)
  536. {
  537. struct nsm_handle *nsm;
  538. struct nlm_host *host;
  539. nsm = nsm_reboot_lookup(info);
  540. if (unlikely(nsm == NULL))
  541. return;
  542. /* Mark all hosts tied to this NSM state as having rebooted.
  543. * We run the loop repeatedly, because we drop the host table
  544. * lock for this.
  545. * To avoid processing a host several times, we match the nsmstate.
  546. */
  547. while ((host = next_host_state(nlm_server_hosts, nsm, info)) != NULL) {
  548. nlmsvc_free_host_resources(host);
  549. nlmsvc_release_host(host);
  550. }
  551. while ((host = next_host_state(nlm_client_hosts, nsm, info)) != NULL) {
  552. nlmclnt_recovery(host);
  553. nlmclnt_release_host(host);
  554. }
  555. nsm_release(nsm);
  556. }
  557. /*
  558. * Shut down the hosts module.
  559. * Note that this routine is called only at server shutdown time.
  560. */
  561. void
  562. nlm_shutdown_hosts(void)
  563. {
  564. struct hlist_head *chain;
  565. struct hlist_node *pos;
  566. struct nlm_host *host;
  567. dprintk("lockd: shutting down host module\n");
  568. mutex_lock(&nlm_host_mutex);
  569. /* First, make all hosts eligible for gc */
  570. dprintk("lockd: nuking all hosts...\n");
  571. for_each_host(host, pos, chain, nlm_server_hosts) {
  572. host->h_expires = jiffies - 1;
  573. if (host->h_rpcclnt) {
  574. rpc_shutdown_client(host->h_rpcclnt);
  575. host->h_rpcclnt = NULL;
  576. }
  577. }
  578. /* Then, perform a garbage collection pass */
  579. nlm_gc_hosts();
  580. mutex_unlock(&nlm_host_mutex);
  581. /* complain if any hosts are left */
  582. if (nrhosts) {
  583. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  584. dprintk("lockd: %d hosts left:\n", nrhosts);
  585. for_each_host(host, pos, chain, nlm_server_hosts) {
  586. dprintk(" %s (cnt %d use %d exp %ld)\n",
  587. host->h_name, atomic_read(&host->h_count),
  588. host->h_inuse, host->h_expires);
  589. }
  590. }
  591. }
  592. /*
  593. * Garbage collect any unused NLM hosts.
  594. * This GC combines reference counting for async operations with
  595. * mark & sweep for resources held by remote clients.
  596. */
  597. static void
  598. nlm_gc_hosts(void)
  599. {
  600. struct hlist_head *chain;
  601. struct hlist_node *pos, *next;
  602. struct nlm_host *host;
  603. dprintk("lockd: host garbage collection\n");
  604. for_each_host(host, pos, chain, nlm_server_hosts)
  605. host->h_inuse = 0;
  606. /* Mark all hosts that hold locks, blocks or shares */
  607. nlmsvc_mark_resources();
  608. for_each_host_safe(host, pos, next, chain, nlm_server_hosts) {
  609. if (atomic_read(&host->h_count) || host->h_inuse
  610. || time_before(jiffies, host->h_expires)) {
  611. dprintk("nlm_gc_hosts skipping %s "
  612. "(cnt %d use %d exp %ld)\n",
  613. host->h_name, atomic_read(&host->h_count),
  614. host->h_inuse, host->h_expires);
  615. continue;
  616. }
  617. nlm_destroy_host_locked(host);
  618. }
  619. next_gc = jiffies + NLM_HOST_COLLECT;
  620. }