host.c 13 KB

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