host.c 13 KB

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