host.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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/in6.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. #include <net/ipv6.h>
  20. #define NLMDBG_FACILITY NLMDBG_HOSTCACHE
  21. #define NLM_HOST_NRHASH 32
  22. #define NLM_HOST_REBIND (60 * HZ)
  23. #define NLM_HOST_EXPIRE (300 * HZ)
  24. #define NLM_HOST_COLLECT (120 * 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 *sap,
  31. const size_t salen,
  32. const char *hostname,
  33. const size_t hostname_len,
  34. const int create);
  35. struct nlm_lookup_host_info {
  36. const int server; /* search for server|client */
  37. const struct sockaddr *sap; /* address to search for */
  38. const size_t salen; /* it's length */
  39. const unsigned short protocol; /* transport to search for*/
  40. const u32 version; /* NLM version to search for */
  41. const char *hostname; /* remote's hostname */
  42. const size_t hostname_len; /* it's length */
  43. const struct sockaddr *src_sap; /* our address (optional) */
  44. const size_t src_len; /* it's length */
  45. };
  46. /*
  47. * Hash function must work well on big- and little-endian platforms
  48. */
  49. static unsigned int __nlm_hash32(const __be32 n)
  50. {
  51. unsigned int hash = (__force u32)n ^ ((__force u32)n >> 16);
  52. return hash ^ (hash >> 8);
  53. }
  54. static unsigned int __nlm_hash_addr4(const struct sockaddr *sap)
  55. {
  56. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  57. return __nlm_hash32(sin->sin_addr.s_addr);
  58. }
  59. static unsigned int __nlm_hash_addr6(const struct sockaddr *sap)
  60. {
  61. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  62. const struct in6_addr addr = sin6->sin6_addr;
  63. return __nlm_hash32(addr.s6_addr32[0]) ^
  64. __nlm_hash32(addr.s6_addr32[1]) ^
  65. __nlm_hash32(addr.s6_addr32[2]) ^
  66. __nlm_hash32(addr.s6_addr32[3]);
  67. }
  68. static unsigned int nlm_hash_address(const struct sockaddr *sap)
  69. {
  70. unsigned int hash;
  71. switch (sap->sa_family) {
  72. case AF_INET:
  73. hash = __nlm_hash_addr4(sap);
  74. break;
  75. case AF_INET6:
  76. hash = __nlm_hash_addr6(sap);
  77. break;
  78. default:
  79. hash = 0;
  80. }
  81. return hash & (NLM_HOST_NRHASH - 1);
  82. }
  83. static void nlm_clear_port(struct sockaddr *sap)
  84. {
  85. switch (sap->sa_family) {
  86. case AF_INET:
  87. ((struct sockaddr_in *)sap)->sin_port = 0;
  88. break;
  89. case AF_INET6:
  90. ((struct sockaddr_in6 *)sap)->sin6_port = 0;
  91. break;
  92. }
  93. }
  94. static void nlm_display_address(const struct sockaddr *sap,
  95. char *buf, const size_t len)
  96. {
  97. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  98. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  99. switch (sap->sa_family) {
  100. case AF_UNSPEC:
  101. snprintf(buf, len, "unspecified");
  102. break;
  103. case AF_INET:
  104. snprintf(buf, len, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
  105. break;
  106. case AF_INET6:
  107. if (ipv6_addr_v4mapped(&sin6->sin6_addr))
  108. snprintf(buf, len, NIPQUAD_FMT,
  109. NIPQUAD(sin6->sin6_addr.s6_addr32[3]));
  110. else
  111. snprintf(buf, len, NIP6_FMT, NIP6(sin6->sin6_addr));
  112. break;
  113. default:
  114. snprintf(buf, len, "unsupported address family");
  115. break;
  116. }
  117. }
  118. /*
  119. * Common host lookup routine for server & client
  120. */
  121. static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
  122. {
  123. struct hlist_head *chain;
  124. struct hlist_node *pos;
  125. struct nlm_host *host;
  126. struct nsm_handle *nsm = NULL;
  127. mutex_lock(&nlm_host_mutex);
  128. if (time_after_eq(jiffies, next_gc))
  129. nlm_gc_hosts();
  130. /* We may keep several nlm_host objects for a peer, because each
  131. * nlm_host is identified by
  132. * (address, protocol, version, server/client)
  133. * We could probably simplify this a little by putting all those
  134. * different NLM rpc_clients into one single nlm_host object.
  135. * This would allow us to have one nlm_host per address.
  136. */
  137. chain = &nlm_hosts[nlm_hash_address(ni->sap)];
  138. hlist_for_each_entry(host, pos, chain, h_hash) {
  139. if (!nlm_cmp_addr(nlm_addr(host), ni->sap))
  140. continue;
  141. /* See if we have an NSM handle for this client */
  142. if (!nsm)
  143. nsm = host->h_nsmhandle;
  144. if (host->h_proto != ni->protocol)
  145. continue;
  146. if (host->h_version != ni->version)
  147. continue;
  148. if (host->h_server != ni->server)
  149. continue;
  150. if (!nlm_cmp_addr(nlm_srcaddr(host), ni->src_sap))
  151. continue;
  152. /* Move to head of hash chain. */
  153. hlist_del(&host->h_hash);
  154. hlist_add_head(&host->h_hash, chain);
  155. nlm_get_host(host);
  156. dprintk("lockd: nlm_lookup_host found host %s (%s)\n",
  157. host->h_name, host->h_addrbuf);
  158. goto out;
  159. }
  160. /*
  161. * The host wasn't in our hash table. If we don't
  162. * have an NSM handle for it yet, create one.
  163. */
  164. if (nsm)
  165. atomic_inc(&nsm->sm_count);
  166. else {
  167. host = NULL;
  168. nsm = nsm_find(ni->sap, ni->salen,
  169. ni->hostname, ni->hostname_len, 1);
  170. if (!nsm) {
  171. dprintk("lockd: nlm_lookup_host failed; "
  172. "no nsm handle\n");
  173. goto out;
  174. }
  175. }
  176. host = kzalloc(sizeof(*host), GFP_KERNEL);
  177. if (!host) {
  178. nsm_release(nsm);
  179. dprintk("lockd: nlm_lookup_host failed; no memory\n");
  180. goto out;
  181. }
  182. host->h_name = nsm->sm_name;
  183. memcpy(nlm_addr(host), ni->sap, ni->salen);
  184. host->h_addrlen = ni->salen;
  185. nlm_clear_port(nlm_addr(host));
  186. memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
  187. host->h_version = ni->version;
  188. host->h_proto = ni->protocol;
  189. host->h_rpcclnt = NULL;
  190. mutex_init(&host->h_mutex);
  191. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  192. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  193. atomic_set(&host->h_count, 1);
  194. init_waitqueue_head(&host->h_gracewait);
  195. init_rwsem(&host->h_rwsem);
  196. host->h_state = 0; /* pseudo NSM state */
  197. host->h_nsmstate = 0; /* real NSM state */
  198. host->h_nsmhandle = nsm;
  199. host->h_server = ni->server;
  200. hlist_add_head(&host->h_hash, chain);
  201. INIT_LIST_HEAD(&host->h_lockowners);
  202. spin_lock_init(&host->h_lock);
  203. INIT_LIST_HEAD(&host->h_granted);
  204. INIT_LIST_HEAD(&host->h_reclaim);
  205. nrhosts++;
  206. nlm_display_address((struct sockaddr *)&host->h_addr,
  207. host->h_addrbuf, sizeof(host->h_addrbuf));
  208. nlm_display_address((struct sockaddr *)&host->h_srcaddr,
  209. host->h_srcaddrbuf, sizeof(host->h_srcaddrbuf));
  210. dprintk("lockd: nlm_lookup_host created host %s\n",
  211. host->h_name);
  212. out:
  213. mutex_unlock(&nlm_host_mutex);
  214. return host;
  215. }
  216. /*
  217. * Destroy a host
  218. */
  219. static void
  220. nlm_destroy_host(struct nlm_host *host)
  221. {
  222. struct rpc_clnt *clnt;
  223. BUG_ON(!list_empty(&host->h_lockowners));
  224. BUG_ON(atomic_read(&host->h_count));
  225. /*
  226. * Release NSM handle and unmonitor host.
  227. */
  228. nsm_unmonitor(host);
  229. clnt = host->h_rpcclnt;
  230. if (clnt != NULL)
  231. rpc_shutdown_client(clnt);
  232. kfree(host);
  233. }
  234. /*
  235. * Find an NLM server handle in the cache. If there is none, create it.
  236. */
  237. struct nlm_host *nlmclnt_lookup_host(const struct sockaddr_in *sin,
  238. int proto, u32 version,
  239. const char *hostname,
  240. unsigned int hostname_len)
  241. {
  242. const struct sockaddr source = {
  243. .sa_family = AF_UNSPEC,
  244. };
  245. struct nlm_lookup_host_info ni = {
  246. .server = 0,
  247. .sap = (struct sockaddr *)sin,
  248. .salen = sizeof(*sin),
  249. .protocol = proto,
  250. .version = version,
  251. .hostname = hostname,
  252. .hostname_len = hostname_len,
  253. .src_sap = &source,
  254. .src_len = sizeof(source),
  255. };
  256. dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
  257. (hostname ? hostname : "<none>"), version,
  258. (proto == IPPROTO_UDP ? "udp" : "tcp"));
  259. return nlm_lookup_host(&ni);
  260. }
  261. /*
  262. * Find an NLM client handle in the cache. If there is none, create it.
  263. */
  264. struct nlm_host *
  265. nlmsvc_lookup_host(struct svc_rqst *rqstp,
  266. const char *hostname, unsigned int hostname_len)
  267. {
  268. const struct sockaddr_in source = {
  269. .sin_family = AF_INET,
  270. .sin_addr = rqstp->rq_daddr.addr,
  271. };
  272. struct nlm_lookup_host_info ni = {
  273. .server = 1,
  274. .sap = svc_addr(rqstp),
  275. .salen = rqstp->rq_addrlen,
  276. .protocol = rqstp->rq_prot,
  277. .version = rqstp->rq_vers,
  278. .hostname = hostname,
  279. .hostname_len = hostname_len,
  280. .src_sap = (struct sockaddr *)&source,
  281. .src_len = sizeof(source),
  282. };
  283. dprintk("lockd: %s(host='%*s', vers=%u, proto=%s)\n", __func__,
  284. (int)hostname_len, hostname, rqstp->rq_vers,
  285. (rqstp->rq_prot == IPPROTO_UDP ? "udp" : "tcp"));
  286. return nlm_lookup_host(&ni);
  287. }
  288. /*
  289. * Create the NLM RPC client for an NLM peer
  290. */
  291. struct rpc_clnt *
  292. nlm_bind_host(struct nlm_host *host)
  293. {
  294. struct rpc_clnt *clnt;
  295. dprintk("lockd: nlm_bind_host %s (%s), my addr=%s\n",
  296. host->h_name, host->h_addrbuf, host->h_srcaddrbuf);
  297. /* Lock host handle */
  298. mutex_lock(&host->h_mutex);
  299. /* If we've already created an RPC client, check whether
  300. * RPC rebind is required
  301. */
  302. if ((clnt = host->h_rpcclnt) != NULL) {
  303. if (time_after_eq(jiffies, host->h_nextrebind)) {
  304. rpc_force_rebind(clnt);
  305. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  306. dprintk("lockd: next rebind in %lu jiffies\n",
  307. host->h_nextrebind - jiffies);
  308. }
  309. } else {
  310. unsigned long increment = nlmsvc_timeout;
  311. struct rpc_timeout timeparms = {
  312. .to_initval = increment,
  313. .to_increment = increment,
  314. .to_maxval = increment * 6UL,
  315. .to_retries = 5U,
  316. };
  317. struct rpc_create_args args = {
  318. .protocol = host->h_proto,
  319. .address = nlm_addr(host),
  320. .addrsize = host->h_addrlen,
  321. .saddress = nlm_srcaddr(host),
  322. .timeout = &timeparms,
  323. .servername = host->h_name,
  324. .program = &nlm_program,
  325. .version = host->h_version,
  326. .authflavor = RPC_AUTH_UNIX,
  327. .flags = (RPC_CLNT_CREATE_NOPING |
  328. RPC_CLNT_CREATE_AUTOBIND),
  329. };
  330. /*
  331. * lockd retries server side blocks automatically so we want
  332. * those to be soft RPC calls. Client side calls need to be
  333. * hard RPC tasks.
  334. */
  335. if (!host->h_server)
  336. args.flags |= RPC_CLNT_CREATE_HARDRTRY;
  337. clnt = rpc_create(&args);
  338. if (!IS_ERR(clnt))
  339. host->h_rpcclnt = clnt;
  340. else {
  341. printk("lockd: couldn't create RPC handle for %s\n", host->h_name);
  342. clnt = NULL;
  343. }
  344. }
  345. mutex_unlock(&host->h_mutex);
  346. return clnt;
  347. }
  348. /*
  349. * Force a portmap lookup of the remote lockd port
  350. */
  351. void
  352. nlm_rebind_host(struct nlm_host *host)
  353. {
  354. dprintk("lockd: rebind host %s\n", host->h_name);
  355. if (host->h_rpcclnt && time_after_eq(jiffies, host->h_nextrebind)) {
  356. rpc_force_rebind(host->h_rpcclnt);
  357. host->h_nextrebind = jiffies + NLM_HOST_REBIND;
  358. }
  359. }
  360. /*
  361. * Increment NLM host count
  362. */
  363. struct nlm_host * nlm_get_host(struct nlm_host *host)
  364. {
  365. if (host) {
  366. dprintk("lockd: get host %s\n", host->h_name);
  367. atomic_inc(&host->h_count);
  368. host->h_expires = jiffies + NLM_HOST_EXPIRE;
  369. }
  370. return host;
  371. }
  372. /*
  373. * Release NLM host after use
  374. */
  375. void nlm_release_host(struct nlm_host *host)
  376. {
  377. if (host != NULL) {
  378. dprintk("lockd: release host %s\n", host->h_name);
  379. BUG_ON(atomic_read(&host->h_count) < 0);
  380. if (atomic_dec_and_test(&host->h_count)) {
  381. BUG_ON(!list_empty(&host->h_lockowners));
  382. BUG_ON(!list_empty(&host->h_granted));
  383. BUG_ON(!list_empty(&host->h_reclaim));
  384. }
  385. }
  386. }
  387. /*
  388. * We were notified that the host indicated by address &sin
  389. * has rebooted.
  390. * Release all resources held by that peer.
  391. */
  392. void nlm_host_rebooted(const struct sockaddr_in *sin,
  393. const char *hostname,
  394. unsigned int hostname_len,
  395. u32 new_state)
  396. {
  397. struct hlist_head *chain;
  398. struct hlist_node *pos;
  399. struct nsm_handle *nsm;
  400. struct nlm_host *host;
  401. nsm = nsm_find((struct sockaddr *)sin, sizeof(*sin),
  402. hostname, hostname_len, 0);
  403. if (nsm == NULL) {
  404. dprintk("lockd: never saw rebooted peer '%.*s' before\n",
  405. hostname_len, hostname);
  406. return;
  407. }
  408. dprintk("lockd: nlm_host_rebooted(%.*s, %s)\n",
  409. hostname_len, hostname, nsm->sm_addrbuf);
  410. /* When reclaiming locks on this peer, make sure that
  411. * we set up a new notification */
  412. nsm->sm_monitored = 0;
  413. /* Mark all hosts tied to this NSM state as having rebooted.
  414. * We run the loop repeatedly, because we drop the host table
  415. * lock for this.
  416. * To avoid processing a host several times, we match the nsmstate.
  417. */
  418. again: mutex_lock(&nlm_host_mutex);
  419. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  420. hlist_for_each_entry(host, pos, chain, h_hash) {
  421. if (host->h_nsmhandle == nsm
  422. && host->h_nsmstate != new_state) {
  423. host->h_nsmstate = new_state;
  424. host->h_state++;
  425. nlm_get_host(host);
  426. mutex_unlock(&nlm_host_mutex);
  427. if (host->h_server) {
  428. /* We're server for this guy, just ditch
  429. * all the locks he held. */
  430. nlmsvc_free_host_resources(host);
  431. } else {
  432. /* He's the server, initiate lock recovery. */
  433. nlmclnt_recovery(host);
  434. }
  435. nlm_release_host(host);
  436. goto again;
  437. }
  438. }
  439. }
  440. mutex_unlock(&nlm_host_mutex);
  441. }
  442. /*
  443. * Shut down the hosts module.
  444. * Note that this routine is called only at server shutdown time.
  445. */
  446. void
  447. nlm_shutdown_hosts(void)
  448. {
  449. struct hlist_head *chain;
  450. struct hlist_node *pos;
  451. struct nlm_host *host;
  452. dprintk("lockd: shutting down host module\n");
  453. mutex_lock(&nlm_host_mutex);
  454. /* First, make all hosts eligible for gc */
  455. dprintk("lockd: nuking all hosts...\n");
  456. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  457. hlist_for_each_entry(host, pos, chain, h_hash) {
  458. host->h_expires = jiffies - 1;
  459. if (host->h_rpcclnt) {
  460. rpc_shutdown_client(host->h_rpcclnt);
  461. host->h_rpcclnt = NULL;
  462. }
  463. }
  464. }
  465. /* Then, perform a garbage collection pass */
  466. nlm_gc_hosts();
  467. mutex_unlock(&nlm_host_mutex);
  468. /* complain if any hosts are left */
  469. if (nrhosts) {
  470. printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
  471. dprintk("lockd: %d hosts left:\n", nrhosts);
  472. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  473. hlist_for_each_entry(host, pos, chain, h_hash) {
  474. dprintk(" %s (cnt %d use %d exp %ld)\n",
  475. host->h_name, atomic_read(&host->h_count),
  476. host->h_inuse, host->h_expires);
  477. }
  478. }
  479. }
  480. }
  481. /*
  482. * Garbage collect any unused NLM hosts.
  483. * This GC combines reference counting for async operations with
  484. * mark & sweep for resources held by remote clients.
  485. */
  486. static void
  487. nlm_gc_hosts(void)
  488. {
  489. struct hlist_head *chain;
  490. struct hlist_node *pos, *next;
  491. struct nlm_host *host;
  492. dprintk("lockd: host garbage collection\n");
  493. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  494. hlist_for_each_entry(host, pos, chain, h_hash)
  495. host->h_inuse = 0;
  496. }
  497. /* Mark all hosts that hold locks, blocks or shares */
  498. nlmsvc_mark_resources();
  499. for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
  500. hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
  501. if (atomic_read(&host->h_count) || host->h_inuse
  502. || time_before(jiffies, host->h_expires)) {
  503. dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
  504. host->h_name, atomic_read(&host->h_count),
  505. host->h_inuse, host->h_expires);
  506. continue;
  507. }
  508. dprintk("lockd: delete host %s\n", host->h_name);
  509. hlist_del_init(&host->h_hash);
  510. nlm_destroy_host(host);
  511. nrhosts--;
  512. }
  513. }
  514. next_gc = jiffies + NLM_HOST_COLLECT;
  515. }
  516. /*
  517. * Manage NSM handles
  518. */
  519. static LIST_HEAD(nsm_handles);
  520. static DEFINE_SPINLOCK(nsm_lock);
  521. static struct nsm_handle *nsm_find(const struct sockaddr *sap,
  522. const size_t salen,
  523. const char *hostname,
  524. const size_t hostname_len,
  525. const int create)
  526. {
  527. struct nsm_handle *nsm = NULL;
  528. struct nsm_handle *pos;
  529. if (!sap)
  530. return NULL;
  531. if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
  532. if (printk_ratelimit()) {
  533. printk(KERN_WARNING "Invalid hostname \"%.*s\" "
  534. "in NFS lock request\n",
  535. (int)hostname_len, hostname);
  536. }
  537. return NULL;
  538. }
  539. retry:
  540. spin_lock(&nsm_lock);
  541. list_for_each_entry(pos, &nsm_handles, sm_link) {
  542. if (hostname && nsm_use_hostnames) {
  543. if (strlen(pos->sm_name) != hostname_len
  544. || memcmp(pos->sm_name, hostname, hostname_len))
  545. continue;
  546. } else if (!nlm_cmp_addr(nsm_addr(pos), sap))
  547. continue;
  548. atomic_inc(&pos->sm_count);
  549. kfree(nsm);
  550. nsm = pos;
  551. goto found;
  552. }
  553. if (nsm) {
  554. list_add(&nsm->sm_link, &nsm_handles);
  555. goto found;
  556. }
  557. spin_unlock(&nsm_lock);
  558. if (!create)
  559. return NULL;
  560. nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
  561. if (nsm == NULL)
  562. return NULL;
  563. memcpy(nsm_addr(nsm), sap, salen);
  564. nsm->sm_addrlen = salen;
  565. nsm->sm_name = (char *) (nsm + 1);
  566. memcpy(nsm->sm_name, hostname, hostname_len);
  567. nsm->sm_name[hostname_len] = '\0';
  568. nlm_display_address((struct sockaddr *)&nsm->sm_addr,
  569. nsm->sm_addrbuf, sizeof(nsm->sm_addrbuf));
  570. atomic_set(&nsm->sm_count, 1);
  571. goto retry;
  572. found:
  573. spin_unlock(&nsm_lock);
  574. return nsm;
  575. }
  576. /*
  577. * Release an NSM handle
  578. */
  579. void
  580. nsm_release(struct nsm_handle *nsm)
  581. {
  582. if (!nsm)
  583. return;
  584. if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
  585. list_del(&nsm->sm_link);
  586. spin_unlock(&nsm_lock);
  587. kfree(nsm);
  588. }
  589. }