host.c 13 KB

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