host.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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_hosts[NLM_HOST_NRHASH];
  25. static unsigned long next_gc;
  26. static int nrhosts;
  27. static DEFINE_MUTEX(nlm_host_mutex);
  28. static void nlm_gc_hosts(void);
  29. struct nlm_lookup_host_info {
  30. const int server; /* search for server|client */
  31. const struct sockaddr *sap; /* address to search for */
  32. const size_t salen; /* it's length */
  33. const unsigned short protocol; /* transport to search for*/
  34. const u32 version; /* NLM version to search for */
  35. const char *hostname; /* remote's hostname */
  36. const size_t hostname_len; /* it's length */
  37. const struct sockaddr *src_sap; /* our address (optional) */
  38. const size_t src_len; /* it's length */
  39. const int noresvport; /* use non-priv port */
  40. };
  41. /*
  42. * Hash function must work well on big- and little-endian platforms
  43. */
  44. static unsigned int __nlm_hash32(const __be32 n)
  45. {
  46. unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
  47. return hash ^ (hash >> 8);
  48. }
  49. static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
  50. {
  51. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  52. return __nlm_hash32(sin->sin_addr.s_addr);
  53. }
  54. static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
  55. {
  56. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  57. const struct in6_addr addr = sin6->sin6_addr;
  58. return __nlm_hash32(addr.s6_addr32[0]) ^
  59. __nlm_hash32(addr.s6_addr32[1]) ^
  60. __nlm_hash32(addr.s6_addr32[2]) ^
  61. __nlm_hash32(addr.s6_addr32[3]);
  62. }
  63. static unsigned int nlm_hash_address(const struct sockaddr *sap)
  64. {
  65. unsigned int hash;
  66. switch (sap->sa_family) {
  67. case AF_INET:
  68. hash = __nlm_hash_addr4(sap);
  69. break;
  70. case AF_INET6:
  71. hash = __nlm_hash_addr6(sap);
  72. break;
  73. default:
  74. hash = 0;
  75. }
  76. return hash & (NLM_HOST_NRHASH - 1);
  77. }
  78. static void nlm_clear_port(struct sockaddr *sap)
  79. {
  80. switch (sap->sa_family) {
  81. case AF_INET:
  82. ((struct sockaddr_in *)sap)->sin_port = 0;
  83. break;
  84. case AF_INET6:
  85. ((struct sockaddr_in6 *)sap)->sin6_port = 0;
  86. break;
  87. }
  88. }
  89. /*
  90. * Common host lookup routine for server & client
  91. */
  92. static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
  93. {
  94. struct hlist_head *chain;
  95. struct hlist_node *pos;
  96. struct nlm_host *host;
  97. struct nsm_handle *nsm = NULL;
  98. mutex_lock(&nlm_host_mutex);
  99. if (time_after_eq(jiffies, next_gc))
  100. nlm_gc_hosts();
  101. /* We may keep several nlm_host objects for a peer, because each
  102. * nlm_host is identified by
  103. * (address, protocol, version, server/client)
  104. * We could probably simplify this a little by putting all those
  105. * different NLM rpc_clients into one single nlm_host object.
  106. * This would allow us to have one nlm_host per address.
  107. */
  108. chain = &nlm_hosts[nlm_hash_address(ni->sap)];
  109. hlist_for_each_entry(host, pos, chain, h_hash) {
  110. if (!nlm_cmp_addr(nlm_addr(host), ni->sap))
  111. continue;
  112. /* See if we have an NSM handle for this client */
  113. if (!nsm)
  114. nsm = host->h_nsmhandle;
  115. if (host->h_proto != ni->protocol)
  116. continue;
  117. if (host->h_version != ni->version)
  118. continue;
  119. if (host->h_server != ni->server)
  120. continue;
  121. if (ni->server &&
  122. !nlm_cmp_addr(nlm_srcaddr(host), ni->src_sap))
  123. continue;
  124. /* Move to head of hash chain. */
  125. hlist_del(&host->h_hash);
  126. hlist_add_head(&host->h_hash, chain);
  127. nlm_get_host(host);
  128. dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
  129. host->h_name, host->h_addrbuf);
  130. goto out;
  131. }
  132. /*
  133. * The host wasn't in our hash table. If we don't
  134. * have an NSM handle for it yet, create one.
  135. */
  136. if (nsm)
  137. atomic_inc(&nsm->sm_count);
  138. else {
  139. host = NULL;
  140. nsm = nsm_get_handle(ni->sap, ni->salen,
  141. ni->hostname, ni->hostname_len);
  142. if (!nsm) {
  143. dprintk("lockd: nlm_lookup_host failed; "
  144. "no nsm handle\n");
  145. goto out;
  146. }
  147. }
  148. host = kzalloc(sizeof(*host), GFP_KERNEL);
  149. if (!host) {
  150. nsm_release(nsm);
  151. dprintk("lockd: nlm_lookup_host failed; no memory\n");
  152. goto out;
  153. }
  154. host->h_name = nsm->sm_name;
  155. host->h_addrbuf = nsm->sm_addrbuf;
  156. memcpy(nlm_addr(host), ni->sap, ni->salen);
  157. host->h_addrlen = ni->salen;
  158. nlm_clear_port(nlm_addr(host));
  159. memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
  160. host->h_version = ni->version;
  161. host->h_proto = ni->protocol;
  162. host->h_rpcclnt = NULL;
  163. mutex_init(&host->h_mutex);
  164. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  165. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  166. atomic_set(&host->h_count, 1);
  167. init_waitqueue_head(&host->h_gracewait);
  168. init_rwsem(&host->h_rwsem);
  169. host->h_state = 0; /* pseudo NSM state */
  170. host->h_nsmstate = 0; /* real NSM state */
  171. host->h_nsmhandle = nsm;
  172. host->h_server = ni->server;
  173. host->h_noresvport = ni->noresvport;
  174. hlist_add_head(&host->h_hash, chain);
  175. INIT_LIST_HEAD(&host->h_lockowners);
  176. spin_lock_init(&host->h_lock);
  177. INIT_LIST_HEAD(&host->h_granted);
  178. INIT_LIST_HEAD(&host->h_reclaim);
  179. nrhosts++;
  180. dprintk("lockd: nlm_lookup_host created host %s\n",
  181. host->h_name);
  182. out:
  183. mutex_unlock(&nlm_host_mutex);
  184. return host;
  185. }
  186. /*
  187. * Destroy a host
  188. */
  189. static void
  190. nlm_destroy_host(struct nlm_host *host)
  191. {
  192. struct rpc_clnt *clnt;
  193. BUG_ON(!list_empty(&host->h_lockowners));
  194. BUG_ON(atomic_read(&host->h_count));
  195. nsm_unmonitor(host);
  196. nsm_release(host->h_nsmhandle);
  197. clnt = host->h_rpcclnt;
  198. if (clnt != NULL)
  199. rpc_shutdown_client(clnt);
  200. kfree(host);
  201. }
  202. /**
  203. * nlmclnt_lookup_host - Find an NLM host handle matching a remote server
  204. * @sap: network address of server
  205. * @salen: length of server address
  206. * @protocol: transport protocol to use
  207. * @version: NLM protocol version
  208. * @hostname: '\0'-terminated hostname of server
  209. * @noresvport: 1 if non-privileged port should be used
  210. *
  211. * Returns an nlm_host structure that matches the passed-in
  212. * [server address, transport protocol, NLM version, server hostname].
  213. * If one doesn't already exist in the host cache, a new handle is
  214. * created and returned.
  215. */
  216. struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
  217. const size_t salen,
  218. const unsigned short protocol,
  219. const u32 version,
  220. const char *hostname,
  221. int noresvport)
  222. {
  223. const struct sockaddr source = {
  224. .sa_family = AF_UNSPEC,
  225. };
  226. struct nlm_lookup_host_info ni = {
  227. .server = 0,
  228. .sap = sap,
  229. .salen = salen,
  230. .protocol = protocol,
  231. .version = version,
  232. .hostname = hostname,
  233. .hostname_len = strlen(hostname),
  234. .src_sap = &source,
  235. .src_len = sizeof(source),
  236. .noresvport = noresvport,
  237. };
  238. dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
  239. (hostname ? hostname : "<none>"), version,
  240. (protocol == IPPROTO_UDP ? "udp" : "tcp"));
  241. return nlm_lookup_host(&ni);
  242. }
  243. /**
  244. * nlmsvc_lookup_host - Find an NLM host handle matching a remote client
  245. * @rqstp: incoming NLM request
  246. * @hostname: name of client host
  247. * @hostname_len: length of client hostname
  248. *
  249. * Returns an nlm_host structure that matches the [client address,
  250. * transport protocol, NLM version, client hostname] of the passed-in
  251. * NLM request. If one doesn't already exist in the host cache, a
  252. * new handle is created and returned.
  253. *
  254. * Before possibly creating a new nlm_host, construct a sockaddr
  255. * for a specific source address in case the local system has
  256. * multiple network addresses. The family of the address in
  257. * rq_daddr is guaranteed to be the same as the family of the
  258. * address in rq_addr, so it's safe to use the same family for
  259. * the source address.
  260. */
  261. struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
  262. const char *hostname,
  263. const size_t hostname_len)
  264. {
  265. struct sockaddr_in sin = {
  266. .sin_family = AF_INET,
  267. };
  268. struct sockaddr_in6 sin6 = {
  269. .sin6_family = AF_INET6,
  270. };
  271. struct nlm_lookup_host_info ni = {
  272. .server = 1,
  273. .sap = svc_addr(rqstp),
  274. .salen = rqstp->rq_addrlen,
  275. .protocol = rqstp->rq_prot,
  276. .version = rqstp->rq_vers,
  277. .hostname = hostname,
  278. .hostname_len = hostname_len,
  279. .src_len = rqstp->rq_addrlen,
  280. };
  281. dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
  282. (int)hostname_len, hostname, rqstp->rq_vers,
  283. (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
  284. switch (ni.sap->sa_family) {
  285. case AF_INET:
  286. sin.sin_addr.s_addr = rqstp->rq_daddr.addr.s_addr;
  287. ni.src_sap = (struct sockaddr *)&sin;
  288. break;
  289. case AF_INET6:
  290. ipv6_addr_copy(&sin6.sin6_addr, &rqstp->rq_daddr.addr6);
  291. ni.src_sap = (struct sockaddr *)&sin6;
  292. break;
  293. default:
  294. return NULL;
  295. }
  296. return nlm_lookup_host(&ni);
  297. }
  298. /*
  299. * Create the NLM RPC client for an NLM peer
  300. */
  301. struct rpc_clnt *
  302. nlm_bind_host(struct nlm_host *host)
  303. {
  304. struct rpc_clnt *clnt;
  305. dprintk("lockd: nlm_bind_host %s (%s)\n",
  306. host->h_name, host->h_addrbuf);
  307. /* Lock host handle */
  308. mutex_lock(&host->h_mutex);
  309. /* If we've already created an RPC client, check whether
  310. * RPC rebind is required
  311. */
  312. if ((clnt = host->h_rpcclnt) != NULL) {
  313. if (time_after_eq(jiffies, host->h_nextrebind)) {
  314. rpc_force_rebind(clnt);
  315. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  316. dprintk("lockd: next rebind in %lu jiffies\n",
  317. host->h_nextrebind - jiffies);
  318. }
  319. } else {
  320. unsigned long increment = nlmsvc_timeout;
  321. struct rpc_timeout timeparms = {
  322. .to_initval = increment,
  323. .to_increment = increment,
  324. .to_maxval = increment * 6UL,
  325. .to_retries = 5U,
  326. };
  327. struct rpc_create_args args = {
  328. .protocol = host->h_proto,
  329. .address = nlm_addr(host),
  330. .addrsize = host->h_addrlen,
  331. .saddress = nlm_srcaddr(host),
  332. .timeout = &timeparms,
  333. .servername = host->h_name,
  334. .program = &nlm_program,
  335. .version = host->h_version,
  336. .authflavor = RPC_AUTH_UNIX,
  337. .flags = (RPC_CLNT_CREATE_NOPING |
  338. RPC_CLNT_CREATE_AUTOBIND),
  339. };
  340. /*
  341. * lockd retries server side blocks automatically so we want
  342. * those to be soft RPC calls. Client side calls need to be
  343. * hard RPC tasks.
  344. */
  345. if (!host->h_server)
  346. args.flags |= RPC_CLNT_CREATE_HARDRTRY;
  347. if (host->h_noresvport)
  348. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  349. clnt = rpc_create(&args);
  350. if (!IS_ERR(clnt))
  351. host->h_rpcclnt = clnt;
  352. else {
  353. printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
  354. clnt = NULL;
  355. }
  356. }
  357. mutex_unlock(&host->h_mutex);
  358. return clnt;
  359. }
  360. /*
  361. * Force a portmap lookup of the remote lockd port
  362. */
  363. void
  364. nlm_rebind_host(struct nlm_host *host)
  365. {
  366. dprintk("lockd: rebind host %s\n", host->h_name);
  367. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  368. rpc_force_rebind(host->h_rpcclnt);
  369. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  370. }
  371. }
  372. /*
  373. * Increment NLM host count
  374. */
  375. struct nlm_host * nlm_get_host(struct nlm_host *host)
  376. {
  377. if (host) {
  378. dprintk("lockd: get host %s\n", host->h_name);
  379. atomic_inc(&host->h_count);
  380. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  381. }
  382. return host;
  383. }
  384. /*
  385. * Release NLM host after use
  386. */
  387. void nlm_release_host(struct nlm_host *host)
  388. {
  389. if (host != NULL) {
  390. dprintk("lockd: release host %s\n", host->h_name);
  391. BUG_ON(atomic_read(&host->h_count) < 0);
  392. if (atomic_dec_and_test(&host->h_count)) {
  393. BUG_ON(!list_empty(&host->h_lockowners));
  394. BUG_ON(!list_empty(&host->h_granted));
  395. BUG_ON(!list_empty(&host->h_reclaim));
  396. }
  397. }
  398. }
  399. /**
  400. * nlm_host_rebooted - Release all resources held by rebooted host
  401. * @info: pointer to decoded results of NLM_SM_NOTIFY call
  402. *
  403. * We were notified that the specified host has rebooted. Release
  404. * all resources held by that peer.
  405. */
  406. void nlm_host_rebooted(const struct nlm_reboot *info)
  407. {
  408. struct hlist_head *chain;
  409. struct hlist_node *pos;
  410. struct nsm_handle *nsm;
  411. struct nlm_host *host;
  412. nsm = nsm_reboot_lookup(info);
  413. if (unlikely(nsm == NULL))
  414. return;
  415. /* Mark all hosts tied to this NSM state as having rebooted.
  416. * We run the loop repeatedly, because we drop the host table
  417. * lock for this.
  418. * To avoid processing a host several times, we match the nsmstate.
  419. */
  420. again: mutex_lock(&nlm_host_mutex);
  421. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  422. hlist_for_each_entry(host, pos, chain, h_hash) {
  423. if (host->h_nsmhandle == nsm
  424. && host->h_nsmstate != info->state) {
  425. host->h_nsmstate = info->state;
  426. host->h_state++;
  427. nlm_get_host(host);
  428. mutex_unlock(&nlm_host_mutex);
  429. if (host->h_server) {
  430. /* We're server for this guy, just ditch
  431. * all the locks he held. */
  432. nlmsvc_free_host_resources(host);
  433. } else {
  434. /* He's the server, initiate lock recovery. */
  435. nlmclnt_recovery(host);
  436. }
  437. nlm_release_host(host);
  438. goto again;
  439. }
  440. }
  441. }
  442. mutex_unlock(&nlm_host_mutex);
  443. }
  444. /*
  445. * Shut down the hosts module.
  446. * Note that this routine is called only at server shutdown time.
  447. */
  448. void
  449. nlm_shutdown_hosts(void)
  450. {
  451. struct hlist_head *chain;
  452. struct hlist_node *pos;
  453. struct nlm_host *host;
  454. dprintk("lockd: shutting down host module\n");
  455. mutex_lock(&nlm_host_mutex);
  456. /* First, make all hosts eligible for gc */
  457. dprintk("lockd: nuking all hosts...\n");
  458. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  459. hlist_for_each_entry(host, pos, chain, h_hash) {
  460. host->h_expires = jiffies - 1;
  461. if (host->h_rpcclnt) {
  462. rpc_shutdown_client(host->h_rpcclnt);
  463. host->h_rpcclnt = NULL;
  464. }
  465. }
  466. }
  467. /* Then, perform a garbage collection pass */
  468. nlm_gc_hosts();
  469. mutex_unlock(&nlm_host_mutex);
  470. /* complain if any hosts are left */
  471. if (nrhosts) {
  472. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  473. dprintk("lockd: %d hosts left:\n", nrhosts);
  474. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  475. hlist_for_each_entry(host, pos, chain, h_hash) {
  476. dprintk(" %s (cnt %d use %d exp %ld)\n",
  477. host->h_name, atomic_read(&host->h_count),
  478. host->h_inuse, host->h_expires);
  479. }
  480. }
  481. }
  482. }
  483. /*
  484. * Garbage collect any unused NLM hosts.
  485. * This GC combines reference counting for async operations with
  486. * mark & sweep for resources held by remote clients.
  487. */
  488. static void
  489. nlm_gc_hosts(void)
  490. {
  491. struct hlist_head *chain;
  492. struct hlist_node *pos, *next;
  493. struct nlm_host *host;
  494. dprintk("lockd: host garbage collection\n");
  495. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  496. hlist_for_each_entry(host, pos, chain, h_hash)
  497. host->h_inuse = 0;
  498. }
  499. /* Mark all hosts that hold locks, blocks or shares */
  500. nlmsvc_mark_resources();
  501. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  502. hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
  503. if (atomic_read(&host->h_count) || host->h_inuse
  504. || time_before(jiffies, host->h_expires)) {
  505. dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
  506. host->h_name, atomic_read(&host->h_count),
  507. host->h_inuse, host->h_expires);
  508. continue;
  509. }
  510. dprintk("lockd: delete host %s\n", host->h_name);
  511. hlist_del_init(&host->h_hash);
  512. nlm_destroy_host(host);
  513. nrhosts--;
  514. }
  515. }
  516. next_gc = jiffies + NLM_HOST_COLLECT;
  517. }