host.c 12 KB

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