host.c 13 KB

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