host.c 13 KB

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