host.c 13 KB

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