host.c 15 KB

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