xref: /openbmc/linux/fs/lockd/mon.c (revision e919b0765287f36f7e9c84aed785eddfbbd98dfa)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/lockd/mon.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * The kernel statd client.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
1294da7663SChuck Lever #include <linux/ktime.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
1494da7663SChuck Lever 
151da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
165976687aSJeff Layton #include <linux/sunrpc/addr.h>
170896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
181da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
191da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
201da177e4SLinus Torvalds 
21ad5b365cSMans Rullgard #include <asm/unaligned.h>
22ad5b365cSMans Rullgard 
23e9406db2SStanislav Kinsbursky #include "netns.h"
24e9406db2SStanislav Kinsbursky 
251da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_MONITOR
2636e8e668SChuck Lever #define NSM_PROGRAM		100024
2736e8e668SChuck Lever #define NSM_VERSION		1
2836e8e668SChuck Lever 
2936e8e668SChuck Lever enum {
3036e8e668SChuck Lever 	NSMPROC_NULL,
3136e8e668SChuck Lever 	NSMPROC_STAT,
3236e8e668SChuck Lever 	NSMPROC_MON,
3336e8e668SChuck Lever 	NSMPROC_UNMON,
3436e8e668SChuck Lever 	NSMPROC_UNMON_ALL,
3536e8e668SChuck Lever 	NSMPROC_SIMU_CRASH,
3636e8e668SChuck Lever 	NSMPROC_NOTIFY,
3736e8e668SChuck Lever };
381da177e4SLinus Torvalds 
399c1bfd03SChuck Lever struct nsm_args {
40cab2d3c9SChuck Lever 	struct nsm_private	*priv;
419c1bfd03SChuck Lever 	u32			prog;		/* RPC callback info */
429c1bfd03SChuck Lever 	u32			vers;
439c1bfd03SChuck Lever 	u32			proc;
449c1bfd03SChuck Lever 
459c1bfd03SChuck Lever 	char			*mon_name;
460d0f4aabSAndrey Ryabinin 	const char		*nodename;
479c1bfd03SChuck Lever };
489c1bfd03SChuck Lever 
499c1bfd03SChuck Lever struct nsm_res {
509c1bfd03SChuck Lever 	u32			status;
519c1bfd03SChuck Lever 	u32			state;
529c1bfd03SChuck Lever };
539c1bfd03SChuck Lever 
54a613fa16STrond Myklebust static const struct rpc_program	nsm_program;
5567c6d107SChuck Lever static				DEFINE_SPINLOCK(nsm_lock);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * Local NSM state
591da177e4SLinus Torvalds  */
606c9dc425SChuck Lever u32	__read_mostly		nsm_local_state;
6190ab5ee9SRusty Russell bool	__read_mostly		nsm_use_hostnames;
621da177e4SLinus Torvalds 
638529bc51SChuck Lever static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm)
648529bc51SChuck Lever {
658529bc51SChuck Lever 	return (struct sockaddr *)&nsm->sm_addr;
668529bc51SChuck Lever }
678529bc51SChuck Lever 
6803a9a42aSTrond Myklebust static struct rpc_clnt *nsm_create(struct net *net, const char *nodename)
6949b5699bSChuck Lever {
7049b5699bSChuck Lever 	struct sockaddr_in sin = {
7149b5699bSChuck Lever 		.sin_family		= AF_INET,
7249b5699bSChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_LOOPBACK),
7349b5699bSChuck Lever 	};
7449b5699bSChuck Lever 	struct rpc_create_args args = {
750e1cb5c0SStanislav Kinsbursky 		.net			= net,
76e9406db2SStanislav Kinsbursky 		.protocol		= XPRT_TRANSPORT_TCP,
7749b5699bSChuck Lever 		.address		= (struct sockaddr *)&sin,
7849b5699bSChuck Lever 		.addrsize		= sizeof(sin),
7949b5699bSChuck Lever 		.servername		= "rpc.statd",
8003a9a42aSTrond Myklebust 		.nodename		= nodename,
8149b5699bSChuck Lever 		.program		= &nsm_program,
8249b5699bSChuck Lever 		.version		= NSM_VERSION,
8349b5699bSChuck Lever 		.authflavor		= RPC_AUTH_NULL,
840e5c2632SChuck Lever 		.flags			= RPC_CLNT_CREATE_NOPING,
8549b5699bSChuck Lever 	};
8649b5699bSChuck Lever 
8749b5699bSChuck Lever 	return rpc_create(&args);
8849b5699bSChuck Lever }
8949b5699bSChuck Lever 
900e1cb5c0SStanislav Kinsbursky static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res,
910d0f4aabSAndrey Ryabinin 			 const struct nlm_host *host)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	int		status;
940d0f4aabSAndrey Ryabinin 	struct rpc_clnt *clnt;
95a4846750SChuck Lever 	struct nsm_args args = {
96cab2d3c9SChuck Lever 		.priv		= &nsm->sm_priv,
97a4846750SChuck Lever 		.prog		= NLM_PROGRAM,
98a4846750SChuck Lever 		.vers		= 3,
99a4846750SChuck Lever 		.proc		= NLMPROC_NSM_NOTIFY,
10029ed1407SChuck Lever 		.mon_name	= nsm->sm_mon_name,
1010d0f4aabSAndrey Ryabinin 		.nodename	= host->nodename,
102a4846750SChuck Lever 	};
103dead28daSChuck Lever 	struct rpc_message msg = {
104dead28daSChuck Lever 		.rpc_argp	= &args,
105dead28daSChuck Lever 		.rpc_resp	= res,
106dead28daSChuck Lever 	};
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	memset(res, 0, sizeof(*res));
1091da177e4SLinus Torvalds 
1100d0f4aabSAndrey Ryabinin 	clnt = nsm_create(host->net, host->nodename);
1110d0f4aabSAndrey Ryabinin 	if (IS_ERR(clnt)) {
1120d0f4aabSAndrey Ryabinin 		dprintk("lockd: failed to create NSM upcall transport, "
113*e919b076SVasily Averin 			"status=%ld, net=%x\n", PTR_ERR(clnt),
114*e919b076SVasily Averin 			host->net->ns.inum);
1150d0f4aabSAndrey Ryabinin 		return PTR_ERR(clnt);
1160d0f4aabSAndrey Ryabinin 	}
1170d0f4aabSAndrey Ryabinin 
118dead28daSChuck Lever 	msg.rpc_proc = &clnt->cl_procinfo[proc];
119e9406db2SStanislav Kinsbursky 	status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN);
120173b3afcSBenjamin Coddington 	if (status == -ECONNREFUSED) {
121173b3afcSBenjamin Coddington 		dprintk("lockd:	NSM upcall RPC failed, status=%d, forcing rebind\n",
122173b3afcSBenjamin Coddington 				status);
123173b3afcSBenjamin Coddington 		rpc_force_rebind(clnt);
124173b3afcSBenjamin Coddington 		status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN);
125173b3afcSBenjamin Coddington 	}
1261da177e4SLinus Torvalds 	if (status < 0)
1275acf4315SChuck Lever 		dprintk("lockd: NSM upcall RPC failed, status=%d\n",
1281da177e4SLinus Torvalds 				status);
1291da177e4SLinus Torvalds 	else
1301da177e4SLinus Torvalds 		status = 0;
1310d0f4aabSAndrey Ryabinin 
1320d0f4aabSAndrey Ryabinin 	rpc_shutdown_client(clnt);
1331da177e4SLinus Torvalds 	return status;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1361e49323cSChuck Lever /**
1371e49323cSChuck Lever  * nsm_monitor - Notify a peer in case we reboot
1381e49323cSChuck Lever  * @host: pointer to nlm_host of peer to notify
1391e49323cSChuck Lever  *
1401e49323cSChuck Lever  * If this peer is not already monitored, this function sends an
1411e49323cSChuck Lever  * upcall to the local rpc.statd to record the name/address of
1421e49323cSChuck Lever  * the peer to notify in case we reboot.
1431e49323cSChuck Lever  *
1441e49323cSChuck Lever  * Returns zero if the peer is monitored by the local rpc.statd;
1451e49323cSChuck Lever  * otherwise a negative errno value is returned.
1461da177e4SLinus Torvalds  */
1471e49323cSChuck Lever int nsm_monitor(const struct nlm_host *host)
1481da177e4SLinus Torvalds {
1498dead0dbSOlaf Kirch 	struct nsm_handle *nsm = host->h_nsmhandle;
1501da177e4SLinus Torvalds 	struct nsm_res	res;
1511da177e4SLinus Torvalds 	int		status;
1521da177e4SLinus Torvalds 
1539fee4902SChuck Lever 	dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
1548dead0dbSOlaf Kirch 
1558dead0dbSOlaf Kirch 	if (nsm->sm_monitored)
156977faf39SOlaf Kirch 		return 0;
1571da177e4SLinus Torvalds 
15829ed1407SChuck Lever 	/*
15929ed1407SChuck Lever 	 * Choose whether to record the caller_name or IP address of
16029ed1407SChuck Lever 	 * this peer in the local rpc.statd's database.
16129ed1407SChuck Lever 	 */
16229ed1407SChuck Lever 	nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
16329ed1407SChuck Lever 
1640d0f4aabSAndrey Ryabinin 	status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, host);
1656c9dc425SChuck Lever 	if (unlikely(res.status != 0))
1665d254b11SChuck Lever 		status = -EIO;
1676c9dc425SChuck Lever 	if (unlikely(status < 0)) {
1689af94fc4SJeff Layton 		pr_notice_ratelimited("lockd: cannot monitor %s\n", nsm->sm_name);
1691da177e4SLinus Torvalds 		return status;
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds 
1726c9dc425SChuck Lever 	nsm->sm_monitored = 1;
1736c9dc425SChuck Lever 	if (unlikely(nsm_local_state != res.state)) {
1746c9dc425SChuck Lever 		nsm_local_state = res.state;
1756c9dc425SChuck Lever 		dprintk("lockd: NSM state changed to %d\n", nsm_local_state);
1766c9dc425SChuck Lever 	}
1776c9dc425SChuck Lever 	return 0;
1786c9dc425SChuck Lever }
1796c9dc425SChuck Lever 
180356c3eb4SChuck Lever /**
181356c3eb4SChuck Lever  * nsm_unmonitor - Unregister peer notification
182356c3eb4SChuck Lever  * @host: pointer to nlm_host of peer to stop monitoring
183356c3eb4SChuck Lever  *
184356c3eb4SChuck Lever  * If this peer is monitored, this function sends an upcall to
185356c3eb4SChuck Lever  * tell the local rpc.statd not to send this peer a notification
186356c3eb4SChuck Lever  * when we reboot.
1871da177e4SLinus Torvalds  */
188356c3eb4SChuck Lever void nsm_unmonitor(const struct nlm_host *host)
1891da177e4SLinus Torvalds {
1908dead0dbSOlaf Kirch 	struct nsm_handle *nsm = host->h_nsmhandle;
1911da177e4SLinus Torvalds 	struct nsm_res	res;
192356c3eb4SChuck Lever 	int status;
1931da177e4SLinus Torvalds 
1949502c522SOlaf Kirch 	if (atomic_read(&nsm->sm_count) == 1
1959502c522SOlaf Kirch 	 && nsm->sm_monitored && !nsm->sm_sticky) {
1969fee4902SChuck Lever 		dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
1979502c522SOlaf Kirch 
1980d0f4aabSAndrey Ryabinin 		status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, host);
1990c7aef45SChuck Lever 		if (res.status != 0)
2000c7aef45SChuck Lever 			status = -EIO;
2011da177e4SLinus Torvalds 		if (status < 0)
2029502c522SOlaf Kirch 			printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
2039fee4902SChuck Lever 					nsm->sm_name);
2049502c522SOlaf Kirch 		else
2058dead0dbSOlaf Kirch 			nsm->sm_monitored = 0;
206977faf39SOlaf Kirch 	}
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2090ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_hostname(const struct list_head *nsm_handles,
2100ad95472SAndrey Ryabinin 					const char *hostname, const size_t len)
2113420a8c4SChuck Lever {
2123420a8c4SChuck Lever 	struct nsm_handle *nsm;
2133420a8c4SChuck Lever 
2140ad95472SAndrey Ryabinin 	list_for_each_entry(nsm, nsm_handles, sm_link)
2153420a8c4SChuck Lever 		if (strlen(nsm->sm_name) == len &&
2163420a8c4SChuck Lever 		    memcmp(nsm->sm_name, hostname, len) == 0)
2173420a8c4SChuck Lever 			return nsm;
2183420a8c4SChuck Lever 	return NULL;
2193420a8c4SChuck Lever }
2203420a8c4SChuck Lever 
2210ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_addr(const struct list_head *nsm_handles,
2220ad95472SAndrey Ryabinin 					const struct sockaddr *sap)
22377a3ef33SChuck Lever {
22477a3ef33SChuck Lever 	struct nsm_handle *nsm;
22577a3ef33SChuck Lever 
2260ad95472SAndrey Ryabinin 	list_for_each_entry(nsm, nsm_handles, sm_link)
2274516fc04SJeff Layton 		if (rpc_cmp_addr(nsm_addr(nsm), sap))
22877a3ef33SChuck Lever 			return nsm;
22977a3ef33SChuck Lever 	return NULL;
23077a3ef33SChuck Lever }
23177a3ef33SChuck Lever 
2320ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_priv(const struct list_head *nsm_handles,
2330ad95472SAndrey Ryabinin 					const struct nsm_private *priv)
2343420a8c4SChuck Lever {
2353420a8c4SChuck Lever 	struct nsm_handle *nsm;
2363420a8c4SChuck Lever 
2370ad95472SAndrey Ryabinin 	list_for_each_entry(nsm, nsm_handles, sm_link)
2383420a8c4SChuck Lever 		if (memcmp(nsm->sm_priv.data, priv->data,
2393420a8c4SChuck Lever 					sizeof(priv->data)) == 0)
2403420a8c4SChuck Lever 			return nsm;
2413420a8c4SChuck Lever 	return NULL;
2423420a8c4SChuck Lever }
2433420a8c4SChuck Lever 
2447e44d3beSChuck Lever /*
2457e44d3beSChuck Lever  * Construct a unique cookie to match this nsm_handle to this monitored
2467e44d3beSChuck Lever  * host.  It is passed to the local rpc.statd via NSMPROC_MON, and
2477e44d3beSChuck Lever  * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
2487e44d3beSChuck Lever  * requests.
2497e44d3beSChuck Lever  *
25094da7663SChuck Lever  * The NSM protocol requires that these cookies be unique while the
25194da7663SChuck Lever  * system is running.  We prefer a stronger requirement of making them
25294da7663SChuck Lever  * unique across reboots.  If user space bugs cause a stale cookie to
25394da7663SChuck Lever  * be sent to the kernel, it could cause the wrong host to lose its
25494da7663SChuck Lever  * lock state if cookies were not unique across reboots.
25594da7663SChuck Lever  *
25694da7663SChuck Lever  * The cookies are exposed only to local user space via loopback.  They
25794da7663SChuck Lever  * do not appear on the physical network.  If we want greater security
25894da7663SChuck Lever  * for some reason, nsm_init_private() could perform a one-way hash to
25994da7663SChuck Lever  * obscure the contents of the cookie.
2607e44d3beSChuck Lever  */
2617e44d3beSChuck Lever static void nsm_init_private(struct nsm_handle *nsm)
2627e44d3beSChuck Lever {
26394da7663SChuck Lever 	u64 *p = (u64 *)&nsm->sm_priv.data;
264ad5b365cSMans Rullgard 	s64 ns;
26594da7663SChuck Lever 
2665eaaed4fSThomas Gleixner 	ns = ktime_get_ns();
267ad5b365cSMans Rullgard 	put_unaligned(ns, p);
268ad5b365cSMans Rullgard 	put_unaligned((unsigned long)nsm, p + 1);
2697e44d3beSChuck Lever }
2707e44d3beSChuck Lever 
271b39b897cSChuck Lever static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
272b39b897cSChuck Lever 					    const size_t salen,
273b39b897cSChuck Lever 					    const char *hostname,
274b39b897cSChuck Lever 					    const size_t hostname_len)
275b39b897cSChuck Lever {
276b39b897cSChuck Lever 	struct nsm_handle *new;
277b39b897cSChuck Lever 
278b39b897cSChuck Lever 	new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
279b39b897cSChuck Lever 	if (unlikely(new == NULL))
280b39b897cSChuck Lever 		return NULL;
281b39b897cSChuck Lever 
282b39b897cSChuck Lever 	atomic_set(&new->sm_count, 1);
283b39b897cSChuck Lever 	new->sm_name = (char *)(new + 1);
284b39b897cSChuck Lever 	memcpy(nsm_addr(new), sap, salen);
285b39b897cSChuck Lever 	new->sm_addrlen = salen;
286b39b897cSChuck Lever 	nsm_init_private(new);
287c15128c5SChuck Lever 
288c15128c5SChuck Lever 	if (rpc_ntop(nsm_addr(new), new->sm_addrbuf,
289c15128c5SChuck Lever 					sizeof(new->sm_addrbuf)) == 0)
290c15128c5SChuck Lever 		(void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf),
291c15128c5SChuck Lever 				"unsupported address family");
292b39b897cSChuck Lever 	memcpy(new->sm_name, hostname, hostname_len);
293b39b897cSChuck Lever 	new->sm_name[hostname_len] = '\0';
294b39b897cSChuck Lever 
295b39b897cSChuck Lever 	return new;
296b39b897cSChuck Lever }
297b39b897cSChuck Lever 
29867c6d107SChuck Lever /**
29992fd91b9SChuck Lever  * nsm_get_handle - Find or create a cached nsm_handle
3000ad95472SAndrey Ryabinin  * @net: network namespace
30167c6d107SChuck Lever  * @sap: pointer to socket address of handle to find
30267c6d107SChuck Lever  * @salen: length of socket address
30367c6d107SChuck Lever  * @hostname: pointer to C string containing hostname to find
30467c6d107SChuck Lever  * @hostname_len: length of C string
30567c6d107SChuck Lever  *
30692fd91b9SChuck Lever  * Behavior is modulated by the global nsm_use_hostnames variable.
30767c6d107SChuck Lever  *
30892fd91b9SChuck Lever  * Returns a cached nsm_handle after bumping its ref count, or
30992fd91b9SChuck Lever  * returns a fresh nsm_handle if a handle that matches @sap and/or
31092fd91b9SChuck Lever  * @hostname cannot be found in the handle cache.  Returns NULL if
31192fd91b9SChuck Lever  * an error occurs.
31267c6d107SChuck Lever  */
3130ad95472SAndrey Ryabinin struct nsm_handle *nsm_get_handle(const struct net *net,
3140ad95472SAndrey Ryabinin 				  const struct sockaddr *sap,
31592fd91b9SChuck Lever 				  const size_t salen, const char *hostname,
31692fd91b9SChuck Lever 				  const size_t hostname_len)
31767c6d107SChuck Lever {
31877a3ef33SChuck Lever 	struct nsm_handle *cached, *new = NULL;
3190ad95472SAndrey Ryabinin 	struct lockd_net *ln = net_generic(net, lockd_net_id);
32067c6d107SChuck Lever 
32167c6d107SChuck Lever 	if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
32267c6d107SChuck Lever 		if (printk_ratelimit()) {
32367c6d107SChuck Lever 			printk(KERN_WARNING "Invalid hostname \"%.*s\" "
32467c6d107SChuck Lever 					    "in NFS lock request\n",
32567c6d107SChuck Lever 				(int)hostname_len, hostname);
32667c6d107SChuck Lever 		}
32767c6d107SChuck Lever 		return NULL;
32867c6d107SChuck Lever 	}
32967c6d107SChuck Lever 
33067c6d107SChuck Lever retry:
33167c6d107SChuck Lever 	spin_lock(&nsm_lock);
33267c6d107SChuck Lever 
33377a3ef33SChuck Lever 	if (nsm_use_hostnames && hostname != NULL)
3340ad95472SAndrey Ryabinin 		cached = nsm_lookup_hostname(&ln->nsm_handles,
3350ad95472SAndrey Ryabinin 					hostname, hostname_len);
33677a3ef33SChuck Lever 	else
3370ad95472SAndrey Ryabinin 		cached = nsm_lookup_addr(&ln->nsm_handles, sap);
33877a3ef33SChuck Lever 
33977a3ef33SChuck Lever 	if (cached != NULL) {
34077a3ef33SChuck Lever 		atomic_inc(&cached->sm_count);
34177a3ef33SChuck Lever 		spin_unlock(&nsm_lock);
34277a3ef33SChuck Lever 		kfree(new);
34377a3ef33SChuck Lever 		dprintk("lockd: found nsm_handle for %s (%s), "
34477a3ef33SChuck Lever 				"cnt %d\n", cached->sm_name,
34577a3ef33SChuck Lever 				cached->sm_addrbuf,
34677a3ef33SChuck Lever 				atomic_read(&cached->sm_count));
34777a3ef33SChuck Lever 		return cached;
34867c6d107SChuck Lever 	}
34977a3ef33SChuck Lever 
35077a3ef33SChuck Lever 	if (new != NULL) {
3510ad95472SAndrey Ryabinin 		list_add(&new->sm_link, &ln->nsm_handles);
35277a3ef33SChuck Lever 		spin_unlock(&nsm_lock);
3535cf1c4b1SChuck Lever 		dprintk("lockd: created nsm_handle for %s (%s)\n",
35477a3ef33SChuck Lever 				new->sm_name, new->sm_addrbuf);
35577a3ef33SChuck Lever 		return new;
35667c6d107SChuck Lever 	}
35777a3ef33SChuck Lever 
35867c6d107SChuck Lever 	spin_unlock(&nsm_lock);
35967c6d107SChuck Lever 
36077a3ef33SChuck Lever 	new = nsm_create_handle(sap, salen, hostname, hostname_len);
36177a3ef33SChuck Lever 	if (unlikely(new == NULL))
36267c6d107SChuck Lever 		return NULL;
36367c6d107SChuck Lever 	goto retry;
36467c6d107SChuck Lever }
36567c6d107SChuck Lever 
36667c6d107SChuck Lever /**
3673420a8c4SChuck Lever  * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
3680ad95472SAndrey Ryabinin  * @net:  network namespace
3693420a8c4SChuck Lever  * @info: pointer to NLMPROC_SM_NOTIFY arguments
3703420a8c4SChuck Lever  *
3717e469af9SJeff Layton  * Returns a matching nsm_handle if found in the nsm cache. The returned
3727e469af9SJeff Layton  * nsm_handle's reference count is bumped. Otherwise returns NULL if some
3737e469af9SJeff Layton  * error occurred.
3743420a8c4SChuck Lever  */
3750ad95472SAndrey Ryabinin struct nsm_handle *nsm_reboot_lookup(const struct net *net,
3760ad95472SAndrey Ryabinin 				const struct nlm_reboot *info)
3773420a8c4SChuck Lever {
3783420a8c4SChuck Lever 	struct nsm_handle *cached;
3790ad95472SAndrey Ryabinin 	struct lockd_net *ln = net_generic(net, lockd_net_id);
3803420a8c4SChuck Lever 
3813420a8c4SChuck Lever 	spin_lock(&nsm_lock);
3823420a8c4SChuck Lever 
3830ad95472SAndrey Ryabinin 	cached = nsm_lookup_priv(&ln->nsm_handles, &info->priv);
3843420a8c4SChuck Lever 	if (unlikely(cached == NULL)) {
3853420a8c4SChuck Lever 		spin_unlock(&nsm_lock);
3863420a8c4SChuck Lever 		dprintk("lockd: never saw rebooted peer '%.*s' before\n",
3873420a8c4SChuck Lever 				info->len, info->mon);
3883420a8c4SChuck Lever 		return cached;
3893420a8c4SChuck Lever 	}
3903420a8c4SChuck Lever 
3913420a8c4SChuck Lever 	atomic_inc(&cached->sm_count);
3923420a8c4SChuck Lever 	spin_unlock(&nsm_lock);
3933420a8c4SChuck Lever 
3943420a8c4SChuck Lever 	dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
3953420a8c4SChuck Lever 			cached->sm_name, cached->sm_addrbuf,
3963420a8c4SChuck Lever 			atomic_read(&cached->sm_count));
3973420a8c4SChuck Lever 	return cached;
3983420a8c4SChuck Lever }
3993420a8c4SChuck Lever 
4003420a8c4SChuck Lever /**
40167c6d107SChuck Lever  * nsm_release - Release an NSM handle
40267c6d107SChuck Lever  * @nsm: pointer to handle to be released
40367c6d107SChuck Lever  *
40467c6d107SChuck Lever  */
40567c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm)
40667c6d107SChuck Lever {
40767c6d107SChuck Lever 	if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
40867c6d107SChuck Lever 		list_del(&nsm->sm_link);
40967c6d107SChuck Lever 		spin_unlock(&nsm_lock);
4105cf1c4b1SChuck Lever 		dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
4115cf1c4b1SChuck Lever 				nsm->sm_name, nsm->sm_addrbuf);
41267c6d107SChuck Lever 		kfree(nsm);
41367c6d107SChuck Lever 	}
41467c6d107SChuck Lever }
41567c6d107SChuck Lever 
4161da177e4SLinus Torvalds /*
4171da177e4SLinus Torvalds  * XDR functions for NSM.
4182ca7754dSChuck Lever  *
4192ca7754dSChuck Lever  * See http://www.opengroup.org/ for details on the Network
4202ca7754dSChuck Lever  * Status Monitor wire protocol.
4211da177e4SLinus Torvalds  */
4221da177e4SLinus Torvalds 
42349b17004SChuck Lever static void encode_nsm_string(struct xdr_stream *xdr, const char *string)
424099bd05fSChuck Lever {
42503eb1dcbSChuck Lever 	const u32 len = strlen(string);
42603eb1dcbSChuck Lever 	__be32 *p;
427099bd05fSChuck Lever 
42849b17004SChuck Lever 	p = xdr_reserve_space(xdr, 4 + len);
42903eb1dcbSChuck Lever 	xdr_encode_opaque(p, string, len);
430099bd05fSChuck Lever }
431099bd05fSChuck Lever 
43249695174SChuck Lever /*
43349695174SChuck Lever  * "mon_name" specifies the host to be monitored.
43449695174SChuck Lever  */
43549b17004SChuck Lever static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
43649695174SChuck Lever {
43749b17004SChuck Lever 	encode_nsm_string(xdr, argp->mon_name);
43849695174SChuck Lever }
43949695174SChuck Lever 
440850c95fdSChuck Lever /*
441850c95fdSChuck Lever  * The "my_id" argument specifies the hostname and RPC procedure
442850c95fdSChuck Lever  * to be called when the status manager receives notification
44336e8e668SChuck Lever  * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
444850c95fdSChuck Lever  * has changed.
445850c95fdSChuck Lever  */
44649b17004SChuck Lever static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
447850c95fdSChuck Lever {
44803eb1dcbSChuck Lever 	__be32 *p;
449850c95fdSChuck Lever 
450303a7ce9SStanislav Kinsbursky 	encode_nsm_string(xdr, argp->nodename);
45149b17004SChuck Lever 	p = xdr_reserve_space(xdr, 4 + 4 + 4);
45249b17004SChuck Lever 	*p++ = cpu_to_be32(argp->prog);
45349b17004SChuck Lever 	*p++ = cpu_to_be32(argp->vers);
45449b17004SChuck Lever 	*p = cpu_to_be32(argp->proc);
455850c95fdSChuck Lever }
456850c95fdSChuck Lever 
457ea72a7f1SChuck Lever /*
458ea72a7f1SChuck Lever  * The "mon_id" argument specifies the non-private arguments
45936e8e668SChuck Lever  * of an NSMPROC_MON or NSMPROC_UNMON call.
460ea72a7f1SChuck Lever  */
46149b17004SChuck Lever static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
462ea72a7f1SChuck Lever {
46349b17004SChuck Lever 	encode_mon_name(xdr, argp);
46449b17004SChuck Lever 	encode_my_id(xdr, argp);
465ea72a7f1SChuck Lever }
466ea72a7f1SChuck Lever 
4670490a54aSChuck Lever /*
4680490a54aSChuck Lever  * The "priv" argument may contain private information required
46936e8e668SChuck Lever  * by the NSMPROC_MON call. This information will be supplied in the
47036e8e668SChuck Lever  * NLMPROC_SM_NOTIFY call.
4710490a54aSChuck Lever  */
47249b17004SChuck Lever static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
4730490a54aSChuck Lever {
47403eb1dcbSChuck Lever 	__be32 *p;
47503eb1dcbSChuck Lever 
47603eb1dcbSChuck Lever 	p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
477cab2d3c9SChuck Lever 	xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4809f06c719SChuck Lever static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr,
481bf96391eSChristoph Hellwig 			    const void *argp)
4821da177e4SLinus Torvalds {
4839f06c719SChuck Lever 	encode_mon_id(xdr, argp);
4849f06c719SChuck Lever 	encode_priv(xdr, argp);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4879f06c719SChuck Lever static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr,
488bf96391eSChristoph Hellwig 			      const void *argp)
4891da177e4SLinus Torvalds {
4909f06c719SChuck Lever 	encode_mon_id(xdr, argp);
49103eb1dcbSChuck Lever }
49203eb1dcbSChuck Lever 
493bf269551SChuck Lever static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp,
494bf269551SChuck Lever 				struct xdr_stream *xdr,
4951fa23391SChristoph Hellwig 				void *data)
49603eb1dcbSChuck Lever {
4971fa23391SChristoph Hellwig 	struct nsm_res *resp = data;
498bf269551SChuck Lever 	__be32 *p;
49903eb1dcbSChuck Lever 
500bf269551SChuck Lever 	p = xdr_inline_decode(xdr, 4 + 4);
50103eb1dcbSChuck Lever 	if (unlikely(p == NULL))
50203eb1dcbSChuck Lever 		return -EIO;
50349b17004SChuck Lever 	resp->status = be32_to_cpup(p++);
50449b17004SChuck Lever 	resp->state = be32_to_cpup(p);
50503eb1dcbSChuck Lever 
506bf269551SChuck Lever 	dprintk("lockd: %s status %d state %d\n",
507bf269551SChuck Lever 		__func__, resp->status, resp->state);
5081da177e4SLinus Torvalds 	return 0;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
511bf269551SChuck Lever static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp,
512bf269551SChuck Lever 			    struct xdr_stream *xdr,
5131fa23391SChristoph Hellwig 			    void *data)
5141da177e4SLinus Torvalds {
5151fa23391SChristoph Hellwig 	struct nsm_res *resp = data;
516bf269551SChuck Lever 	__be32 *p;
51703eb1dcbSChuck Lever 
518bf269551SChuck Lever 	p = xdr_inline_decode(xdr, 4);
51903eb1dcbSChuck Lever 	if (unlikely(p == NULL))
52003eb1dcbSChuck Lever 		return -EIO;
52149b17004SChuck Lever 	resp->state = be32_to_cpup(p);
52203eb1dcbSChuck Lever 
523bf269551SChuck Lever 	dprintk("lockd: %s state %d\n", __func__, resp->state);
5241da177e4SLinus Torvalds 	return 0;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds #define SM_my_name_sz	(1+XDR_QUADLEN(SM_MAXSTRLEN))
5282ca7754dSChuck Lever #define SM_my_id_sz	(SM_my_name_sz+3)
5292ca7754dSChuck Lever #define SM_mon_name_sz	(1+XDR_QUADLEN(SM_MAXSTRLEN))
5302ca7754dSChuck Lever #define SM_mon_id_sz	(SM_mon_name_sz+SM_my_id_sz)
5310490a54aSChuck Lever #define SM_priv_sz	(XDR_QUADLEN(SM_PRIV_SIZE))
5320490a54aSChuck Lever #define SM_mon_sz	(SM_mon_id_sz+SM_priv_sz)
5331da177e4SLinus Torvalds #define SM_monres_sz	2
5341da177e4SLinus Torvalds #define SM_unmonres_sz	1
5351da177e4SLinus Torvalds 
536499b4988SChristoph Hellwig static const struct rpc_procinfo nsm_procedures[] = {
53736e8e668SChuck Lever [NSMPROC_MON] = {
53836e8e668SChuck Lever 		.p_proc		= NSMPROC_MON,
539bf96391eSChristoph Hellwig 		.p_encode	= nsm_xdr_enc_mon,
5401fa23391SChristoph Hellwig 		.p_decode	= nsm_xdr_dec_stat_res,
5412bea90d4SChuck Lever 		.p_arglen	= SM_mon_sz,
5422bea90d4SChuck Lever 		.p_replen	= SM_monres_sz,
54336e8e668SChuck Lever 		.p_statidx	= NSMPROC_MON,
544cc0175c1SChuck Lever 		.p_name		= "MONITOR",
5451da177e4SLinus Torvalds 	},
54636e8e668SChuck Lever [NSMPROC_UNMON] = {
54736e8e668SChuck Lever 		.p_proc		= NSMPROC_UNMON,
548bf96391eSChristoph Hellwig 		.p_encode	= nsm_xdr_enc_unmon,
5491fa23391SChristoph Hellwig 		.p_decode	= nsm_xdr_dec_stat,
5502bea90d4SChuck Lever 		.p_arglen	= SM_mon_id_sz,
5512bea90d4SChuck Lever 		.p_replen	= SM_unmonres_sz,
55236e8e668SChuck Lever 		.p_statidx	= NSMPROC_UNMON,
553cc0175c1SChuck Lever 		.p_name		= "UNMONITOR",
5541da177e4SLinus Torvalds 	},
5551da177e4SLinus Torvalds };
5561da177e4SLinus Torvalds 
5571c5876ddSChristoph Hellwig static unsigned int nsm_version1_counts[ARRAY_SIZE(nsm_procedures)];
558a613fa16STrond Myklebust static const struct rpc_version nsm_version1 = {
5591da177e4SLinus Torvalds 	.number		= 1,
560e8c96f8cSTobias Klauser 	.nrprocs	= ARRAY_SIZE(nsm_procedures),
5611c5876ddSChristoph Hellwig 	.procs		= nsm_procedures,
5621c5876ddSChristoph Hellwig 	.counts		= nsm_version1_counts,
5631da177e4SLinus Torvalds };
5641da177e4SLinus Torvalds 
565a613fa16STrond Myklebust static const struct rpc_version *nsm_version[] = {
5661da177e4SLinus Torvalds 	[1] = &nsm_version1,
5671da177e4SLinus Torvalds };
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds static struct rpc_stat		nsm_stats;
5701da177e4SLinus Torvalds 
571a613fa16STrond Myklebust static const struct rpc_program nsm_program = {
5721da177e4SLinus Torvalds 	.name		= "statd",
57336e8e668SChuck Lever 	.number		= NSM_PROGRAM,
574e8c96f8cSTobias Klauser 	.nrvers		= ARRAY_SIZE(nsm_version),
5751da177e4SLinus Torvalds 	.version	= nsm_version,
5761da177e4SLinus Torvalds 	.stats		= &nsm_stats
5771da177e4SLinus Torvalds };
578