host.c 16 KB

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