host.c 13 KB

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