host.c 13 KB

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