host.c 13 KB

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