host.c 16 KB

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