host.c 13 KB

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