host.c 13 KB

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