host.c 15 KB

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