host.c 15 KB

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