xref: /linux/fs/lockd/mon.c (revision 94da7663db26530a8377f7219f8be8bd4d4822c2)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/lockd/mon.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * The kernel statd client.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/utsname.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
12*94da7663SChuck Lever #include <linux/ktime.h>
13*94da7663SChuck Lever 
141da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
150896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
161da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
171da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
181da177e4SLinus Torvalds #include <linux/lockd/sm_inter.h>
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_MONITOR
2236e8e668SChuck Lever #define NSM_PROGRAM		100024
2336e8e668SChuck Lever #define NSM_VERSION		1
2436e8e668SChuck Lever 
2536e8e668SChuck Lever enum {
2636e8e668SChuck Lever 	NSMPROC_NULL,
2736e8e668SChuck Lever 	NSMPROC_STAT,
2836e8e668SChuck Lever 	NSMPROC_MON,
2936e8e668SChuck Lever 	NSMPROC_UNMON,
3036e8e668SChuck Lever 	NSMPROC_UNMON_ALL,
3136e8e668SChuck Lever 	NSMPROC_SIMU_CRASH,
3236e8e668SChuck Lever 	NSMPROC_NOTIFY,
3336e8e668SChuck Lever };
341da177e4SLinus Torvalds 
359c1bfd03SChuck Lever struct nsm_args {
36cab2d3c9SChuck Lever 	struct nsm_private	*priv;
379c1bfd03SChuck Lever 	u32			prog;		/* RPC callback info */
389c1bfd03SChuck Lever 	u32			vers;
399c1bfd03SChuck Lever 	u32			proc;
409c1bfd03SChuck Lever 
419c1bfd03SChuck Lever 	char			*mon_name;
429c1bfd03SChuck Lever };
439c1bfd03SChuck Lever 
449c1bfd03SChuck Lever struct nsm_res {
459c1bfd03SChuck Lever 	u32			status;
469c1bfd03SChuck Lever 	u32			state;
479c1bfd03SChuck Lever };
489c1bfd03SChuck Lever 
491da177e4SLinus Torvalds static struct rpc_clnt *	nsm_create(void);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static struct rpc_program	nsm_program;
5267c6d107SChuck Lever static				LIST_HEAD(nsm_handles);
5367c6d107SChuck Lever static				DEFINE_SPINLOCK(nsm_lock);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * Local NSM state
571da177e4SLinus Torvalds  */
58460f5cacSOlaf Kirch int				nsm_local_state;
591da177e4SLinus Torvalds 
6067c6d107SChuck Lever static void nsm_display_ipv4_address(const struct sockaddr *sap, char *buf,
6167c6d107SChuck Lever 				     const size_t len)
6267c6d107SChuck Lever {
6367c6d107SChuck Lever 	const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
6467c6d107SChuck Lever 	snprintf(buf, len, "%pI4", &sin->sin_addr.s_addr);
6567c6d107SChuck Lever }
6667c6d107SChuck Lever 
6767c6d107SChuck Lever static void nsm_display_ipv6_address(const struct sockaddr *sap, char *buf,
6867c6d107SChuck Lever 				     const size_t len)
6967c6d107SChuck Lever {
7067c6d107SChuck Lever 	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
7167c6d107SChuck Lever 
7267c6d107SChuck Lever 	if (ipv6_addr_v4mapped(&sin6->sin6_addr))
7367c6d107SChuck Lever 		snprintf(buf, len, "%pI4", &sin6->sin6_addr.s6_addr32[3]);
7467c6d107SChuck Lever 	else if (sin6->sin6_scope_id != 0)
7567c6d107SChuck Lever 		snprintf(buf, len, "%pI6%%%u", &sin6->sin6_addr,
7667c6d107SChuck Lever 				sin6->sin6_scope_id);
7767c6d107SChuck Lever 	else
7867c6d107SChuck Lever 		snprintf(buf, len, "%pI6", &sin6->sin6_addr);
7967c6d107SChuck Lever }
8067c6d107SChuck Lever 
8167c6d107SChuck Lever static void nsm_display_address(const struct sockaddr *sap,
8267c6d107SChuck Lever 				char *buf, const size_t len)
8367c6d107SChuck Lever {
8467c6d107SChuck Lever 	switch (sap->sa_family) {
8567c6d107SChuck Lever 	case AF_INET:
8667c6d107SChuck Lever 		nsm_display_ipv4_address(sap, buf, len);
8767c6d107SChuck Lever 		break;
8867c6d107SChuck Lever 	case AF_INET6:
8967c6d107SChuck Lever 		nsm_display_ipv6_address(sap, buf, len);
9067c6d107SChuck Lever 		break;
9167c6d107SChuck Lever 	default:
9267c6d107SChuck Lever 		snprintf(buf, len, "unsupported address family");
9367c6d107SChuck Lever 		break;
9467c6d107SChuck Lever 	}
9567c6d107SChuck Lever }
9667c6d107SChuck Lever 
971da177e4SLinus Torvalds /*
9836e8e668SChuck Lever  * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
991da177e4SLinus Torvalds  */
1001da177e4SLinus Torvalds static int
1019502c522SOlaf Kirch nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
1041da177e4SLinus Torvalds 	int		status;
105a4846750SChuck Lever 	struct nsm_args args = {
106cab2d3c9SChuck Lever 		.priv		= &nsm->sm_priv,
107a4846750SChuck Lever 		.prog		= NLM_PROGRAM,
108a4846750SChuck Lever 		.vers		= 3,
109a4846750SChuck Lever 		.proc		= NLMPROC_NSM_NOTIFY,
11029ed1407SChuck Lever 		.mon_name	= nsm->sm_mon_name,
111a4846750SChuck Lever 	};
112dead28daSChuck Lever 	struct rpc_message msg = {
113dead28daSChuck Lever 		.rpc_argp	= &args,
114dead28daSChuck Lever 		.rpc_resp	= res,
115dead28daSChuck Lever 	};
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	clnt = nsm_create();
1181da177e4SLinus Torvalds 	if (IS_ERR(clnt)) {
1191da177e4SLinus Torvalds 		status = PTR_ERR(clnt);
1205acf4315SChuck Lever 		dprintk("lockd: failed to create NSM upcall transport, "
1215acf4315SChuck Lever 				"status=%d\n", status);
1221da177e4SLinus Torvalds 		goto out;
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	memset(res, 0, sizeof(*res));
1261da177e4SLinus Torvalds 
127dead28daSChuck Lever 	msg.rpc_proc = &clnt->cl_procinfo[proc];
128dead28daSChuck Lever 	status = rpc_call_sync(clnt, &msg, 0);
1291da177e4SLinus Torvalds 	if (status < 0)
1305acf4315SChuck Lever 		dprintk("lockd: NSM upcall RPC failed, status=%d\n",
1311da177e4SLinus Torvalds 				status);
1321da177e4SLinus Torvalds 	else
1331da177e4SLinus Torvalds 		status = 0;
13490c5755fSTrond Myklebust 	rpc_shutdown_client(clnt);
1351da177e4SLinus Torvalds  out:
1361da177e4SLinus Torvalds 	return status;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391e49323cSChuck Lever /**
1401e49323cSChuck Lever  * nsm_monitor - Notify a peer in case we reboot
1411e49323cSChuck Lever  * @host: pointer to nlm_host of peer to notify
1421e49323cSChuck Lever  *
1431e49323cSChuck Lever  * If this peer is not already monitored, this function sends an
1441e49323cSChuck Lever  * upcall to the local rpc.statd to record the name/address of
1451e49323cSChuck Lever  * the peer to notify in case we reboot.
1461e49323cSChuck Lever  *
1471e49323cSChuck Lever  * Returns zero if the peer is monitored by the local rpc.statd;
1481e49323cSChuck Lever  * otherwise a negative errno value is returned.
1491da177e4SLinus Torvalds  */
1501e49323cSChuck Lever int nsm_monitor(const struct nlm_host *host)
1511da177e4SLinus Torvalds {
1528dead0dbSOlaf Kirch 	struct nsm_handle *nsm = host->h_nsmhandle;
1531da177e4SLinus Torvalds 	struct nsm_res	res;
1541da177e4SLinus Torvalds 	int		status;
1551da177e4SLinus Torvalds 
1569fee4902SChuck Lever 	dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
1578dead0dbSOlaf Kirch 
1588dead0dbSOlaf Kirch 	if (nsm->sm_monitored)
159977faf39SOlaf Kirch 		return 0;
1601da177e4SLinus Torvalds 
16129ed1407SChuck Lever 	/*
16229ed1407SChuck Lever 	 * Choose whether to record the caller_name or IP address of
16329ed1407SChuck Lever 	 * this peer in the local rpc.statd's database.
16429ed1407SChuck Lever 	 */
16529ed1407SChuck Lever 	nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
16629ed1407SChuck Lever 
16736e8e668SChuck Lever 	status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
1685d254b11SChuck Lever 	if (res.status != 0)
1695d254b11SChuck Lever 		status = -EIO;
1705d254b11SChuck Lever 	if (status < 0)
1719fee4902SChuck Lever 		printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
1721da177e4SLinus Torvalds 	else
1738dead0dbSOlaf Kirch 		nsm->sm_monitored = 1;
1741da177e4SLinus Torvalds 	return status;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
177356c3eb4SChuck Lever /**
178356c3eb4SChuck Lever  * nsm_unmonitor - Unregister peer notification
179356c3eb4SChuck Lever  * @host: pointer to nlm_host of peer to stop monitoring
180356c3eb4SChuck Lever  *
181356c3eb4SChuck Lever  * If this peer is monitored, this function sends an upcall to
182356c3eb4SChuck Lever  * tell the local rpc.statd not to send this peer a notification
183356c3eb4SChuck Lever  * when we reboot.
1841da177e4SLinus Torvalds  */
185356c3eb4SChuck Lever void nsm_unmonitor(const struct nlm_host *host)
1861da177e4SLinus Torvalds {
1878dead0dbSOlaf Kirch 	struct nsm_handle *nsm = host->h_nsmhandle;
1881da177e4SLinus Torvalds 	struct nsm_res	res;
189356c3eb4SChuck Lever 	int status;
1901da177e4SLinus Torvalds 
1919502c522SOlaf Kirch 	if (atomic_read(&nsm->sm_count) == 1
1929502c522SOlaf Kirch 	 && nsm->sm_monitored && !nsm->sm_sticky) {
1939fee4902SChuck Lever 		dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
1949502c522SOlaf Kirch 
19536e8e668SChuck Lever 		status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
1960c7aef45SChuck Lever 		if (res.status != 0)
1970c7aef45SChuck Lever 			status = -EIO;
1981da177e4SLinus Torvalds 		if (status < 0)
1999502c522SOlaf Kirch 			printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
2009fee4902SChuck Lever 					nsm->sm_name);
2019502c522SOlaf Kirch 		else
2028dead0dbSOlaf Kirch 			nsm->sm_monitored = 0;
203977faf39SOlaf Kirch 	}
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2063420a8c4SChuck Lever static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
2073420a8c4SChuck Lever 					      const size_t len)
2083420a8c4SChuck Lever {
2093420a8c4SChuck Lever 	struct nsm_handle *nsm;
2103420a8c4SChuck Lever 
2113420a8c4SChuck Lever 	list_for_each_entry(nsm, &nsm_handles, sm_link)
2123420a8c4SChuck Lever 		if (strlen(nsm->sm_name) == len &&
2133420a8c4SChuck Lever 		    memcmp(nsm->sm_name, hostname, len) == 0)
2143420a8c4SChuck Lever 			return nsm;
2153420a8c4SChuck Lever 	return NULL;
2163420a8c4SChuck Lever }
2173420a8c4SChuck Lever 
21877a3ef33SChuck Lever static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap)
21977a3ef33SChuck Lever {
22077a3ef33SChuck Lever 	struct nsm_handle *nsm;
22177a3ef33SChuck Lever 
22277a3ef33SChuck Lever 	list_for_each_entry(nsm, &nsm_handles, sm_link)
22377a3ef33SChuck Lever 		if (nlm_cmp_addr(nsm_addr(nsm), sap))
22477a3ef33SChuck Lever 			return nsm;
22577a3ef33SChuck Lever 	return NULL;
22677a3ef33SChuck Lever }
22777a3ef33SChuck Lever 
2283420a8c4SChuck Lever static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
2293420a8c4SChuck Lever {
2303420a8c4SChuck Lever 	struct nsm_handle *nsm;
2313420a8c4SChuck Lever 
2323420a8c4SChuck Lever 	list_for_each_entry(nsm, &nsm_handles, sm_link)
2333420a8c4SChuck Lever 		if (memcmp(nsm->sm_priv.data, priv->data,
2343420a8c4SChuck Lever 					sizeof(priv->data)) == 0)
2353420a8c4SChuck Lever 			return nsm;
2363420a8c4SChuck Lever 	return NULL;
2373420a8c4SChuck Lever }
2383420a8c4SChuck Lever 
2397e44d3beSChuck Lever /*
2407e44d3beSChuck Lever  * Construct a unique cookie to match this nsm_handle to this monitored
2417e44d3beSChuck Lever  * host.  It is passed to the local rpc.statd via NSMPROC_MON, and
2427e44d3beSChuck Lever  * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
2437e44d3beSChuck Lever  * requests.
2447e44d3beSChuck Lever  *
245*94da7663SChuck Lever  * The NSM protocol requires that these cookies be unique while the
246*94da7663SChuck Lever  * system is running.  We prefer a stronger requirement of making them
247*94da7663SChuck Lever  * unique across reboots.  If user space bugs cause a stale cookie to
248*94da7663SChuck Lever  * be sent to the kernel, it could cause the wrong host to lose its
249*94da7663SChuck Lever  * lock state if cookies were not unique across reboots.
250*94da7663SChuck Lever  *
251*94da7663SChuck Lever  * The cookies are exposed only to local user space via loopback.  They
252*94da7663SChuck Lever  * do not appear on the physical network.  If we want greater security
253*94da7663SChuck Lever  * for some reason, nsm_init_private() could perform a one-way hash to
254*94da7663SChuck Lever  * obscure the contents of the cookie.
2557e44d3beSChuck Lever  */
2567e44d3beSChuck Lever static void nsm_init_private(struct nsm_handle *nsm)
2577e44d3beSChuck Lever {
258*94da7663SChuck Lever 	u64 *p = (u64 *)&nsm->sm_priv.data;
259*94da7663SChuck Lever 	struct timespec ts;
260*94da7663SChuck Lever 
261*94da7663SChuck Lever 	ktime_get_ts(&ts);
262*94da7663SChuck Lever 	*p++ = timespec_to_ns(&ts);
263*94da7663SChuck Lever 	*p = (unsigned long)nsm;
2647e44d3beSChuck Lever }
2657e44d3beSChuck Lever 
266b39b897cSChuck Lever static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
267b39b897cSChuck Lever 					    const size_t salen,
268b39b897cSChuck Lever 					    const char *hostname,
269b39b897cSChuck Lever 					    const size_t hostname_len)
270b39b897cSChuck Lever {
271b39b897cSChuck Lever 	struct nsm_handle *new;
272b39b897cSChuck Lever 
273b39b897cSChuck Lever 	new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
274b39b897cSChuck Lever 	if (unlikely(new == NULL))
275b39b897cSChuck Lever 		return NULL;
276b39b897cSChuck Lever 
277b39b897cSChuck Lever 	atomic_set(&new->sm_count, 1);
278b39b897cSChuck Lever 	new->sm_name = (char *)(new + 1);
279b39b897cSChuck Lever 	memcpy(nsm_addr(new), sap, salen);
280b39b897cSChuck Lever 	new->sm_addrlen = salen;
281b39b897cSChuck Lever 	nsm_init_private(new);
282b39b897cSChuck Lever 	nsm_display_address((const struct sockaddr *)&new->sm_addr,
283b39b897cSChuck Lever 				new->sm_addrbuf, sizeof(new->sm_addrbuf));
284b39b897cSChuck Lever 	memcpy(new->sm_name, hostname, hostname_len);
285b39b897cSChuck Lever 	new->sm_name[hostname_len] = '\0';
286b39b897cSChuck Lever 
287b39b897cSChuck Lever 	return new;
288b39b897cSChuck Lever }
289b39b897cSChuck Lever 
29067c6d107SChuck Lever /**
29192fd91b9SChuck Lever  * nsm_get_handle - Find or create a cached nsm_handle
29267c6d107SChuck Lever  * @sap: pointer to socket address of handle to find
29367c6d107SChuck Lever  * @salen: length of socket address
29467c6d107SChuck Lever  * @hostname: pointer to C string containing hostname to find
29567c6d107SChuck Lever  * @hostname_len: length of C string
29667c6d107SChuck Lever  *
29792fd91b9SChuck Lever  * Behavior is modulated by the global nsm_use_hostnames variable.
29867c6d107SChuck Lever  *
29992fd91b9SChuck Lever  * Returns a cached nsm_handle after bumping its ref count, or
30092fd91b9SChuck Lever  * returns a fresh nsm_handle if a handle that matches @sap and/or
30192fd91b9SChuck Lever  * @hostname cannot be found in the handle cache.  Returns NULL if
30292fd91b9SChuck Lever  * an error occurs.
30367c6d107SChuck Lever  */
30492fd91b9SChuck Lever struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
30592fd91b9SChuck Lever 				  const size_t salen, const char *hostname,
30692fd91b9SChuck Lever 				  const size_t hostname_len)
30767c6d107SChuck Lever {
30877a3ef33SChuck Lever 	struct nsm_handle *cached, *new = NULL;
30967c6d107SChuck Lever 
31067c6d107SChuck Lever 	if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
31167c6d107SChuck Lever 		if (printk_ratelimit()) {
31267c6d107SChuck Lever 			printk(KERN_WARNING "Invalid hostname \"%.*s\" "
31367c6d107SChuck Lever 					    "in NFS lock request\n",
31467c6d107SChuck Lever 				(int)hostname_len, hostname);
31567c6d107SChuck Lever 		}
31667c6d107SChuck Lever 		return NULL;
31767c6d107SChuck Lever 	}
31867c6d107SChuck Lever 
31967c6d107SChuck Lever retry:
32067c6d107SChuck Lever 	spin_lock(&nsm_lock);
32167c6d107SChuck Lever 
32277a3ef33SChuck Lever 	if (nsm_use_hostnames && hostname != NULL)
32377a3ef33SChuck Lever 		cached = nsm_lookup_hostname(hostname, hostname_len);
32477a3ef33SChuck Lever 	else
32577a3ef33SChuck Lever 		cached = nsm_lookup_addr(sap);
32677a3ef33SChuck Lever 
32777a3ef33SChuck Lever 	if (cached != NULL) {
32877a3ef33SChuck Lever 		atomic_inc(&cached->sm_count);
32977a3ef33SChuck Lever 		spin_unlock(&nsm_lock);
33077a3ef33SChuck Lever 		kfree(new);
33177a3ef33SChuck Lever 		dprintk("lockd: found nsm_handle for %s (%s), "
33277a3ef33SChuck Lever 				"cnt %d\n", cached->sm_name,
33377a3ef33SChuck Lever 				cached->sm_addrbuf,
33477a3ef33SChuck Lever 				atomic_read(&cached->sm_count));
33577a3ef33SChuck Lever 		return cached;
33667c6d107SChuck Lever 	}
33777a3ef33SChuck Lever 
33877a3ef33SChuck Lever 	if (new != NULL) {
33977a3ef33SChuck Lever 		list_add(&new->sm_link, &nsm_handles);
34077a3ef33SChuck Lever 		spin_unlock(&nsm_lock);
3415cf1c4b1SChuck Lever 		dprintk("lockd: created nsm_handle for %s (%s)\n",
34277a3ef33SChuck Lever 				new->sm_name, new->sm_addrbuf);
34377a3ef33SChuck Lever 		return new;
34467c6d107SChuck Lever 	}
34577a3ef33SChuck Lever 
34667c6d107SChuck Lever 	spin_unlock(&nsm_lock);
34767c6d107SChuck Lever 
34877a3ef33SChuck Lever 	new = nsm_create_handle(sap, salen, hostname, hostname_len);
34977a3ef33SChuck Lever 	if (unlikely(new == NULL))
35067c6d107SChuck Lever 		return NULL;
35167c6d107SChuck Lever 	goto retry;
35267c6d107SChuck Lever }
35367c6d107SChuck Lever 
35467c6d107SChuck Lever /**
3553420a8c4SChuck Lever  * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
3563420a8c4SChuck Lever  * @info: pointer to NLMPROC_SM_NOTIFY arguments
3573420a8c4SChuck Lever  *
3583420a8c4SChuck Lever  * Returns a matching nsm_handle if found in the nsm cache; the returned
3593420a8c4SChuck Lever  * nsm_handle's reference count is bumped and sm_monitored is cleared.
3603420a8c4SChuck Lever  * Otherwise returns NULL if some error occurred.
3613420a8c4SChuck Lever  */
3623420a8c4SChuck Lever struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
3633420a8c4SChuck Lever {
3643420a8c4SChuck Lever 	struct nsm_handle *cached;
3653420a8c4SChuck Lever 
3663420a8c4SChuck Lever 	spin_lock(&nsm_lock);
3673420a8c4SChuck Lever 
3683420a8c4SChuck Lever 	cached = nsm_lookup_priv(&info->priv);
3693420a8c4SChuck Lever 	if (unlikely(cached == NULL)) {
3703420a8c4SChuck Lever 		spin_unlock(&nsm_lock);
3713420a8c4SChuck Lever 		dprintk("lockd: never saw rebooted peer '%.*s' before\n",
3723420a8c4SChuck Lever 				info->len, info->mon);
3733420a8c4SChuck Lever 		return cached;
3743420a8c4SChuck Lever 	}
3753420a8c4SChuck Lever 
3763420a8c4SChuck Lever 	atomic_inc(&cached->sm_count);
3773420a8c4SChuck Lever 	spin_unlock(&nsm_lock);
3783420a8c4SChuck Lever 
3793420a8c4SChuck Lever 	/*
3803420a8c4SChuck Lever 	 * During subsequent lock activity, force a fresh
3813420a8c4SChuck Lever 	 * notification to be set up for this host.
3823420a8c4SChuck Lever 	 */
3833420a8c4SChuck Lever 	cached->sm_monitored = 0;
3843420a8c4SChuck Lever 
3853420a8c4SChuck Lever 	dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
3863420a8c4SChuck Lever 			cached->sm_name, cached->sm_addrbuf,
3873420a8c4SChuck Lever 			atomic_read(&cached->sm_count));
3883420a8c4SChuck Lever 	return cached;
3893420a8c4SChuck Lever }
3903420a8c4SChuck Lever 
3913420a8c4SChuck Lever /**
39267c6d107SChuck Lever  * nsm_release - Release an NSM handle
39367c6d107SChuck Lever  * @nsm: pointer to handle to be released
39467c6d107SChuck Lever  *
39567c6d107SChuck Lever  */
39667c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm)
39767c6d107SChuck Lever {
39867c6d107SChuck Lever 	if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
39967c6d107SChuck Lever 		list_del(&nsm->sm_link);
40067c6d107SChuck Lever 		spin_unlock(&nsm_lock);
4015cf1c4b1SChuck Lever 		dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
4025cf1c4b1SChuck Lever 				nsm->sm_name, nsm->sm_addrbuf);
40367c6d107SChuck Lever 		kfree(nsm);
40467c6d107SChuck Lever 	}
40567c6d107SChuck Lever }
40667c6d107SChuck Lever 
4071da177e4SLinus Torvalds /*
4081da177e4SLinus Torvalds  * Create NSM client for the local host
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds static struct rpc_clnt *
4111da177e4SLinus Torvalds nsm_create(void)
4121da177e4SLinus Torvalds {
413e1ec7892SChuck Lever 	struct sockaddr_in	sin = {
414e1ec7892SChuck Lever 		.sin_family	= AF_INET,
415e1ec7892SChuck Lever 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
416e1ec7892SChuck Lever 		.sin_port	= 0,
417e1ec7892SChuck Lever 	};
418e1ec7892SChuck Lever 	struct rpc_create_args args = {
4190896a725S\"Talpey, Thomas\ 		.protocol	= XPRT_TRANSPORT_UDP,
420e1ec7892SChuck Lever 		.address	= (struct sockaddr *)&sin,
421e1ec7892SChuck Lever 		.addrsize	= sizeof(sin),
422e1ec7892SChuck Lever 		.servername	= "localhost",
423e1ec7892SChuck Lever 		.program	= &nsm_program,
42436e8e668SChuck Lever 		.version	= NSM_VERSION,
425e1ec7892SChuck Lever 		.authflavor	= RPC_AUTH_NULL,
426e1ec7892SChuck Lever 	};
4271da177e4SLinus Torvalds 
428e1ec7892SChuck Lever 	return rpc_create(&args);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /*
4321da177e4SLinus Torvalds  * XDR functions for NSM.
4332ca7754dSChuck Lever  *
4342ca7754dSChuck Lever  * See http://www.opengroup.org/ for details on the Network
4352ca7754dSChuck Lever  * Status Monitor wire protocol.
4361da177e4SLinus Torvalds  */
4371da177e4SLinus Torvalds 
43803eb1dcbSChuck Lever static int encode_nsm_string(struct xdr_stream *xdr, const char *string)
439099bd05fSChuck Lever {
44003eb1dcbSChuck Lever 	const u32 len = strlen(string);
44103eb1dcbSChuck Lever 	__be32 *p;
442099bd05fSChuck Lever 
44303eb1dcbSChuck Lever 	if (unlikely(len > SM_MAXSTRLEN))
44403eb1dcbSChuck Lever 		return -EIO;
44503eb1dcbSChuck Lever 	p = xdr_reserve_space(xdr, sizeof(u32) + len);
44603eb1dcbSChuck Lever 	if (unlikely(p == NULL))
44703eb1dcbSChuck Lever 		return -EIO;
44803eb1dcbSChuck Lever 	xdr_encode_opaque(p, string, len);
44903eb1dcbSChuck Lever 	return 0;
450099bd05fSChuck Lever }
451099bd05fSChuck Lever 
45249695174SChuck Lever /*
45349695174SChuck Lever  * "mon_name" specifies the host to be monitored.
45449695174SChuck Lever  */
45503eb1dcbSChuck Lever static int encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
45649695174SChuck Lever {
45703eb1dcbSChuck Lever 	return encode_nsm_string(xdr, argp->mon_name);
45849695174SChuck Lever }
45949695174SChuck Lever 
460850c95fdSChuck Lever /*
461850c95fdSChuck Lever  * The "my_id" argument specifies the hostname and RPC procedure
462850c95fdSChuck Lever  * to be called when the status manager receives notification
46336e8e668SChuck Lever  * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
464850c95fdSChuck Lever  * has changed.
465850c95fdSChuck Lever  */
46603eb1dcbSChuck Lever static int encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
467850c95fdSChuck Lever {
46803eb1dcbSChuck Lever 	int status;
46903eb1dcbSChuck Lever 	__be32 *p;
470850c95fdSChuck Lever 
47103eb1dcbSChuck Lever 	status = encode_nsm_string(xdr, utsname()->nodename);
47203eb1dcbSChuck Lever 	if (unlikely(status != 0))
47303eb1dcbSChuck Lever 		return status;
47403eb1dcbSChuck Lever 	p = xdr_reserve_space(xdr, 3 * sizeof(u32));
47503eb1dcbSChuck Lever 	if (unlikely(p == NULL))
47603eb1dcbSChuck Lever 		return -EIO;
477850c95fdSChuck Lever 	*p++ = htonl(argp->prog);
478850c95fdSChuck Lever 	*p++ = htonl(argp->vers);
479850c95fdSChuck Lever 	*p++ = htonl(argp->proc);
48003eb1dcbSChuck Lever 	return 0;
481850c95fdSChuck Lever }
482850c95fdSChuck Lever 
483ea72a7f1SChuck Lever /*
484ea72a7f1SChuck Lever  * The "mon_id" argument specifies the non-private arguments
48536e8e668SChuck Lever  * of an NSMPROC_MON or NSMPROC_UNMON call.
486ea72a7f1SChuck Lever  */
48703eb1dcbSChuck Lever static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
488ea72a7f1SChuck Lever {
48903eb1dcbSChuck Lever 	int status;
490ea72a7f1SChuck Lever 
49103eb1dcbSChuck Lever 	status = encode_mon_name(xdr, argp);
49203eb1dcbSChuck Lever 	if (unlikely(status != 0))
49303eb1dcbSChuck Lever 		return status;
49403eb1dcbSChuck Lever 	return encode_my_id(xdr, argp);
495ea72a7f1SChuck Lever }
496ea72a7f1SChuck Lever 
4970490a54aSChuck Lever /*
4980490a54aSChuck Lever  * The "priv" argument may contain private information required
49936e8e668SChuck Lever  * by the NSMPROC_MON call. This information will be supplied in the
50036e8e668SChuck Lever  * NLMPROC_SM_NOTIFY call.
5010490a54aSChuck Lever  */
50203eb1dcbSChuck Lever static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
5030490a54aSChuck Lever {
50403eb1dcbSChuck Lever 	__be32 *p;
50503eb1dcbSChuck Lever 
50603eb1dcbSChuck Lever 	p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
50703eb1dcbSChuck Lever 	if (unlikely(p == NULL))
50803eb1dcbSChuck Lever 		return -EIO;
509cab2d3c9SChuck Lever 	xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
5101da177e4SLinus Torvalds 	return 0;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
51303eb1dcbSChuck Lever static int xdr_enc_mon(struct rpc_rqst *req, __be32 *p,
51403eb1dcbSChuck Lever 		       const struct nsm_args *argp)
5151da177e4SLinus Torvalds {
51603eb1dcbSChuck Lever 	struct xdr_stream xdr;
51703eb1dcbSChuck Lever 	int status;
51803eb1dcbSChuck Lever 
51903eb1dcbSChuck Lever 	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
52003eb1dcbSChuck Lever 	status = encode_mon_id(&xdr, argp);
52103eb1dcbSChuck Lever 	if (unlikely(status))
52203eb1dcbSChuck Lever 		return status;
52303eb1dcbSChuck Lever 	return encode_priv(&xdr, argp);
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
52603eb1dcbSChuck Lever static int xdr_enc_unmon(struct rpc_rqst *req, __be32 *p,
52703eb1dcbSChuck Lever 			 const struct nsm_args *argp)
5281da177e4SLinus Torvalds {
52903eb1dcbSChuck Lever 	struct xdr_stream xdr;
53003eb1dcbSChuck Lever 
53103eb1dcbSChuck Lever 	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
53203eb1dcbSChuck Lever 	return encode_mon_id(&xdr, argp);
53303eb1dcbSChuck Lever }
53403eb1dcbSChuck Lever 
53503eb1dcbSChuck Lever static int xdr_dec_stat_res(struct rpc_rqst *rqstp, __be32 *p,
53603eb1dcbSChuck Lever 			    struct nsm_res *resp)
53703eb1dcbSChuck Lever {
53803eb1dcbSChuck Lever 	struct xdr_stream xdr;
53903eb1dcbSChuck Lever 
54003eb1dcbSChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
54103eb1dcbSChuck Lever 	p = xdr_inline_decode(&xdr, 2 * sizeof(u32));
54203eb1dcbSChuck Lever 	if (unlikely(p == NULL))
54303eb1dcbSChuck Lever 		return -EIO;
5441da177e4SLinus Torvalds 	resp->status = ntohl(*p++);
54503eb1dcbSChuck Lever 	resp->state = ntohl(*p);
54603eb1dcbSChuck Lever 
54703eb1dcbSChuck Lever 	dprintk("lockd: xdr_dec_stat_res status %d state %d\n",
5481da177e4SLinus Torvalds 			resp->status, resp->state);
5491da177e4SLinus Torvalds 	return 0;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
55203eb1dcbSChuck Lever static int xdr_dec_stat(struct rpc_rqst *rqstp, __be32 *p,
55303eb1dcbSChuck Lever 			struct nsm_res *resp)
5541da177e4SLinus Torvalds {
55503eb1dcbSChuck Lever 	struct xdr_stream xdr;
55603eb1dcbSChuck Lever 
55703eb1dcbSChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
55803eb1dcbSChuck Lever 	p = xdr_inline_decode(&xdr, sizeof(u32));
55903eb1dcbSChuck Lever 	if (unlikely(p == NULL))
56003eb1dcbSChuck Lever 		return -EIO;
56103eb1dcbSChuck Lever 	resp->state = ntohl(*p);
56203eb1dcbSChuck Lever 
56303eb1dcbSChuck Lever 	dprintk("lockd: xdr_dec_stat state %d\n", resp->state);
5641da177e4SLinus Torvalds 	return 0;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds #define SM_my_name_sz	(1+XDR_QUADLEN(SM_MAXSTRLEN))
5682ca7754dSChuck Lever #define SM_my_id_sz	(SM_my_name_sz+3)
5692ca7754dSChuck Lever #define SM_mon_name_sz	(1+XDR_QUADLEN(SM_MAXSTRLEN))
5702ca7754dSChuck Lever #define SM_mon_id_sz	(SM_mon_name_sz+SM_my_id_sz)
5710490a54aSChuck Lever #define SM_priv_sz	(XDR_QUADLEN(SM_PRIV_SIZE))
5720490a54aSChuck Lever #define SM_mon_sz	(SM_mon_id_sz+SM_priv_sz)
5731da177e4SLinus Torvalds #define SM_monres_sz	2
5741da177e4SLinus Torvalds #define SM_unmonres_sz	1
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds static struct rpc_procinfo	nsm_procedures[] = {
57736e8e668SChuck Lever [NSMPROC_MON] = {
57836e8e668SChuck Lever 		.p_proc		= NSMPROC_MON,
57903eb1dcbSChuck Lever 		.p_encode	= (kxdrproc_t)xdr_enc_mon,
58003eb1dcbSChuck Lever 		.p_decode	= (kxdrproc_t)xdr_dec_stat_res,
5812bea90d4SChuck Lever 		.p_arglen	= SM_mon_sz,
5822bea90d4SChuck Lever 		.p_replen	= SM_monres_sz,
58336e8e668SChuck Lever 		.p_statidx	= NSMPROC_MON,
584cc0175c1SChuck Lever 		.p_name		= "MONITOR",
5851da177e4SLinus Torvalds 	},
58636e8e668SChuck Lever [NSMPROC_UNMON] = {
58736e8e668SChuck Lever 		.p_proc		= NSMPROC_UNMON,
58803eb1dcbSChuck Lever 		.p_encode	= (kxdrproc_t)xdr_enc_unmon,
58903eb1dcbSChuck Lever 		.p_decode	= (kxdrproc_t)xdr_dec_stat,
5902bea90d4SChuck Lever 		.p_arglen	= SM_mon_id_sz,
5912bea90d4SChuck Lever 		.p_replen	= SM_unmonres_sz,
59236e8e668SChuck Lever 		.p_statidx	= NSMPROC_UNMON,
593cc0175c1SChuck Lever 		.p_name		= "UNMONITOR",
5941da177e4SLinus Torvalds 	},
5951da177e4SLinus Torvalds };
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds static struct rpc_version	nsm_version1 = {
5981da177e4SLinus Torvalds 		.number		= 1,
599e8c96f8cSTobias Klauser 		.nrprocs	= ARRAY_SIZE(nsm_procedures),
6001da177e4SLinus Torvalds 		.procs		= nsm_procedures
6011da177e4SLinus Torvalds };
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds static struct rpc_version *	nsm_version[] = {
6041da177e4SLinus Torvalds 	[1] = &nsm_version1,
6051da177e4SLinus Torvalds };
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds static struct rpc_stat		nsm_stats;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds static struct rpc_program	nsm_program = {
6101da177e4SLinus Torvalds 		.name		= "statd",
61136e8e668SChuck Lever 		.number		= NSM_PROGRAM,
612e8c96f8cSTobias Klauser 		.nrvers		= ARRAY_SIZE(nsm_version),
6131da177e4SLinus Torvalds 		.version	= nsm_version,
6141da177e4SLinus Torvalds 		.stats		= &nsm_stats
6151da177e4SLinus Torvalds };
616