host.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. if ((clnt = host->h_rpcclnt) != NULL) {
  139. if (atomic_read(&clnt->cl_users)) {
  140. printk(KERN_WARNING
  141. "lockd: active RPC handle\n");
  142. clnt->cl_dead = 1;
  143. } else {
  144. rpc_destroy_client(host->h_rpcclnt);
  145. }
  146. }
  147. kfree(host);
  148. }
  149. /*
  150. * Find an NLM server handle in the cache. If there is none, create it.
  151. */
  152. struct nlm_host *
  153. nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
  154. const char *hostname, int hostname_len)
  155. {
  156. return nlm_lookup_host(0, sin, proto, version,
  157. hostname, hostname_len);
  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, int hostname_len)
  165. {
  166. return nlm_lookup_host(1, svc_addr_in(rqstp),
  167. rqstp->rq_prot, rqstp->rq_vers,
  168. hostname, hostname_len);
  169. }
  170. /*
  171. * Create the NLM RPC client for an NLM peer
  172. */
  173. struct rpc_clnt *
  174. nlm_bind_host(struct nlm_host *host)
  175. {
  176. struct rpc_clnt *clnt;
  177. dprintk("lockd: nlm_bind_host(%08x)\n",
  178. (unsigned)ntohl(host->h_addr.sin_addr.s_addr));
  179. /* Lock host handle */
  180. mutex_lock(&host->h_mutex);
  181. /* If we've already created an RPC client, check whether
  182. * RPC rebind is required
  183. */
  184. if ((clnt = host->h_rpcclnt) != NULL) {
  185. if (time_after_eq(jiffies, host->h_nextrebind)) {
  186. rpc_force_rebind(clnt);
  187. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  188. dprintk("lockd: next rebind in %ld jiffies\n",
  189. host->h_nextrebind - jiffies);
  190. }
  191. } else {
  192. unsigned long increment = nlmsvc_timeout * HZ;
  193. struct rpc_timeout timeparms = {
  194. .to_initval = increment,
  195. .to_increment = increment,
  196. .to_maxval = increment * 6UL,
  197. .to_retries = 5U,
  198. };
  199. struct rpc_create_args args = {
  200. .protocol = host->h_proto,
  201. .address = (struct sockaddr *)&host->h_addr,
  202. .addrsize = sizeof(host->h_addr),
  203. .timeout = &timeparms,
  204. .servername = host->h_name,
  205. .program = &nlm_program,
  206. .version = host->h_version,
  207. .authflavor = RPC_AUTH_UNIX,
  208. .flags = (RPC_CLNT_CREATE_HARDRTRY |
  209. RPC_CLNT_CREATE_AUTOBIND),
  210. };
  211. clnt = rpc_create(&args);
  212. if (!IS_ERR(clnt))
  213. host->h_rpcclnt = clnt;
  214. else {
  215. printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
  216. clnt = NULL;
  217. }
  218. }
  219. mutex_unlock(&host->h_mutex);
  220. return clnt;
  221. }
  222. /*
  223. * Force a portmap lookup of the remote lockd port
  224. */
  225. void
  226. nlm_rebind_host(struct nlm_host *host)
  227. {
  228. dprintk("lockd: rebind host %s\n", host->h_name);
  229. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  230. rpc_force_rebind(host->h_rpcclnt);
  231. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  232. }
  233. }
  234. /*
  235. * Increment NLM host count
  236. */
  237. struct nlm_host * nlm_get_host(struct nlm_host *host)
  238. {
  239. if (host) {
  240. dprintk("lockd: get host %s\n", host->h_name);
  241. atomic_inc(&host->h_count);
  242. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  243. }
  244. return host;
  245. }
  246. /*
  247. * Release NLM host after use
  248. */
  249. void nlm_release_host(struct nlm_host *host)
  250. {
  251. if (host != NULL) {
  252. dprintk("lockd: release host %s\n", host->h_name);
  253. BUG_ON(atomic_read(&host->h_count) < 0);
  254. if (atomic_dec_and_test(&host->h_count)) {
  255. BUG_ON(!list_empty(&host->h_lockowners));
  256. BUG_ON(!list_empty(&host->h_granted));
  257. BUG_ON(!list_empty(&host->h_reclaim));
  258. }
  259. }
  260. }
  261. /*
  262. * We were notified that the host indicated by address &sin
  263. * has rebooted.
  264. * Release all resources held by that peer.
  265. */
  266. void nlm_host_rebooted(const struct sockaddr_in *sin,
  267. const char *hostname, int hostname_len,
  268. u32 new_state)
  269. {
  270. struct hlist_head *chain;
  271. struct hlist_node *pos;
  272. struct nsm_handle *nsm;
  273. struct nlm_host *host;
  274. dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
  275. hostname, NIPQUAD(sin->sin_addr));
  276. /* Find the NSM handle for this peer */
  277. if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
  278. return;
  279. /* When reclaiming locks on this peer, make sure that
  280. * we set up a new notification */
  281. nsm->sm_monitored = 0;
  282. /* Mark all hosts tied to this NSM state as having rebooted.
  283. * We run the loop repeatedly, because we drop the host table
  284. * lock for this.
  285. * To avoid processing a host several times, we match the nsmstate.
  286. */
  287. again: mutex_lock(&nlm_host_mutex);
  288. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  289. hlist_for_each_entry(host, pos, chain, h_hash) {
  290. if (host->h_nsmhandle == nsm
  291. && host->h_nsmstate != new_state) {
  292. host->h_nsmstate = new_state;
  293. host->h_state++;
  294. nlm_get_host(host);
  295. mutex_unlock(&nlm_host_mutex);
  296. if (host->h_server) {
  297. /* We're server for this guy, just ditch
  298. * all the locks he held. */
  299. nlmsvc_free_host_resources(host);
  300. } else {
  301. /* He's the server, initiate lock recovery. */
  302. nlmclnt_recovery(host);
  303. }
  304. nlm_release_host(host);
  305. goto again;
  306. }
  307. }
  308. }
  309. mutex_unlock(&nlm_host_mutex);
  310. }
  311. /*
  312. * Shut down the hosts module.
  313. * Note that this routine is called only at server shutdown time.
  314. */
  315. void
  316. nlm_shutdown_hosts(void)
  317. {
  318. struct hlist_head *chain;
  319. struct hlist_node *pos;
  320. struct nlm_host *host;
  321. dprintk("lockd: shutting down host module\n");
  322. mutex_lock(&nlm_host_mutex);
  323. /* First, make all hosts eligible for gc */
  324. dprintk("lockd: nuking all hosts...\n");
  325. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  326. hlist_for_each_entry(host, pos, chain, h_hash)
  327. host->h_expires = jiffies - 1;
  328. }
  329. /* Then, perform a garbage collection pass */
  330. nlm_gc_hosts();
  331. mutex_unlock(&nlm_host_mutex);
  332. /* complain if any hosts are left */
  333. if (nrhosts) {
  334. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  335. dprintk("lockd: %d hosts left:\n", nrhosts);
  336. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  337. hlist_for_each_entry(host, pos, chain, h_hash) {
  338. dprintk(" %s (cnt %d use %d exp %ld)\n",
  339. host->h_name, atomic_read(&host->h_count),
  340. host->h_inuse, host->h_expires);
  341. }
  342. }
  343. }
  344. }
  345. /*
  346. * Garbage collect any unused NLM hosts.
  347. * This GC combines reference counting for async operations with
  348. * mark & sweep for resources held by remote clients.
  349. */
  350. static void
  351. nlm_gc_hosts(void)
  352. {
  353. struct hlist_head *chain;
  354. struct hlist_node *pos, *next;
  355. struct nlm_host *host;
  356. dprintk("lockd: host garbage collection\n");
  357. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  358. hlist_for_each_entry(host, pos, chain, h_hash)
  359. host->h_inuse = 0;
  360. }
  361. /* Mark all hosts that hold locks, blocks or shares */
  362. nlmsvc_mark_resources();
  363. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  364. hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
  365. if (atomic_read(&host->h_count) || host->h_inuse
  366. || time_before(jiffies, host->h_expires)) {
  367. dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
  368. host->h_name, atomic_read(&host->h_count),
  369. host->h_inuse, host->h_expires);
  370. continue;
  371. }
  372. dprintk("lockd: delete host %s\n", host->h_name);
  373. hlist_del_init(&host->h_hash);
  374. nlm_destroy_host(host);
  375. nrhosts--;
  376. }
  377. }
  378. next_gc = jiffies + NLM_HOST_COLLECT;
  379. }
  380. /*
  381. * Manage NSM handles
  382. */
  383. static LIST_HEAD(nsm_handles);
  384. static DEFINE_MUTEX(nsm_mutex);
  385. static struct nsm_handle *
  386. __nsm_find(const struct sockaddr_in *sin,
  387. const char *hostname, int hostname_len,
  388. int create)
  389. {
  390. struct nsm_handle *nsm = NULL;
  391. struct list_head *pos;
  392. if (!sin)
  393. return NULL;
  394. if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
  395. if (printk_ratelimit()) {
  396. printk(KERN_WARNING "Invalid hostname \"%.*s\" "
  397. "in NFS lock request\n",
  398. hostname_len, hostname);
  399. }
  400. return NULL;
  401. }
  402. mutex_lock(&nsm_mutex);
  403. list_for_each(pos, &nsm_handles) {
  404. nsm = list_entry(pos, struct nsm_handle, sm_link);
  405. if (hostname && nsm_use_hostnames) {
  406. if (strlen(nsm->sm_name) != hostname_len
  407. || memcmp(nsm->sm_name, hostname, hostname_len))
  408. continue;
  409. } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
  410. continue;
  411. atomic_inc(&nsm->sm_count);
  412. goto out;
  413. }
  414. if (!create) {
  415. nsm = NULL;
  416. goto out;
  417. }
  418. nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
  419. if (nsm != NULL) {
  420. nsm->sm_addr = *sin;
  421. nsm->sm_name = (char *) (nsm + 1);
  422. memcpy(nsm->sm_name, hostname, hostname_len);
  423. nsm->sm_name[hostname_len] = '\0';
  424. atomic_set(&nsm->sm_count, 1);
  425. list_add(&nsm->sm_link, &nsm_handles);
  426. }
  427. out:
  428. mutex_unlock(&nsm_mutex);
  429. return nsm;
  430. }
  431. static struct nsm_handle *
  432. nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
  433. {
  434. return __nsm_find(sin, hostname, hostname_len, 1);
  435. }
  436. /*
  437. * Release an NSM handle
  438. */
  439. void
  440. nsm_release(struct nsm_handle *nsm)
  441. {
  442. if (!nsm)
  443. return;
  444. if (atomic_dec_and_test(&nsm->sm_count)) {
  445. mutex_lock(&nsm_mutex);
  446. if (atomic_read(&nsm->sm_count) == 0) {
  447. list_del(&nsm->sm_link);
  448. kfree(nsm);
  449. }
  450. mutex_unlock(&nsm_mutex);
  451. }
  452. }