host.c 15 KB

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