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
21*5f60d5f6SAl Viro #include <linux/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
nsm_addr(const struct nsm_handle * nsm)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
nsm_create(struct net * net,const char * nodename)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,
8579caa5faSTrond Myklebust .cred = current_cred(),
8649b5699bSChuck Lever };
8749b5699bSChuck Lever
8849b5699bSChuck Lever return rpc_create(&args);
8949b5699bSChuck Lever }
9049b5699bSChuck Lever
nsm_mon_unmon(struct nsm_handle * nsm,u32 proc,struct nsm_res * res,const struct nlm_host * host)910e1cb5c0SStanislav Kinsbursky static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res,
920d0f4aabSAndrey Ryabinin const struct nlm_host *host)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds int status;
950d0f4aabSAndrey Ryabinin struct rpc_clnt *clnt;
96a4846750SChuck Lever struct nsm_args args = {
97cab2d3c9SChuck Lever .priv = &nsm->sm_priv,
98a4846750SChuck Lever .prog = NLM_PROGRAM,
99a4846750SChuck Lever .vers = 3,
100a4846750SChuck Lever .proc = NLMPROC_NSM_NOTIFY,
10129ed1407SChuck Lever .mon_name = nsm->sm_mon_name,
1020d0f4aabSAndrey Ryabinin .nodename = host->nodename,
103a4846750SChuck Lever };
104dead28daSChuck Lever struct rpc_message msg = {
105dead28daSChuck Lever .rpc_argp = &args,
106dead28daSChuck Lever .rpc_resp = res,
107dead28daSChuck Lever };
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds memset(res, 0, sizeof(*res));
1101da177e4SLinus Torvalds
1110d0f4aabSAndrey Ryabinin clnt = nsm_create(host->net, host->nodename);
1120d0f4aabSAndrey Ryabinin if (IS_ERR(clnt)) {
1130d0f4aabSAndrey Ryabinin dprintk("lockd: failed to create NSM upcall transport, "
114e919b076SVasily Averin "status=%ld, net=%x\n", PTR_ERR(clnt),
115e919b076SVasily Averin host->net->ns.inum);
1160d0f4aabSAndrey Ryabinin return PTR_ERR(clnt);
1170d0f4aabSAndrey Ryabinin }
1180d0f4aabSAndrey Ryabinin
119dead28daSChuck Lever msg.rpc_proc = &clnt->cl_procinfo[proc];
120e9406db2SStanislav Kinsbursky status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN);
121173b3afcSBenjamin Coddington if (status == -ECONNREFUSED) {
122173b3afcSBenjamin Coddington dprintk("lockd: NSM upcall RPC failed, status=%d, forcing rebind\n",
123173b3afcSBenjamin Coddington status);
124173b3afcSBenjamin Coddington rpc_force_rebind(clnt);
125173b3afcSBenjamin Coddington status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN);
126173b3afcSBenjamin Coddington }
1271da177e4SLinus Torvalds if (status < 0)
1285acf4315SChuck Lever dprintk("lockd: NSM upcall RPC failed, status=%d\n",
1291da177e4SLinus Torvalds status);
1301da177e4SLinus Torvalds else
1311da177e4SLinus Torvalds status = 0;
1320d0f4aabSAndrey Ryabinin
1330d0f4aabSAndrey Ryabinin rpc_shutdown_client(clnt);
1341da177e4SLinus Torvalds return status;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds
1371e49323cSChuck Lever /**
1381e49323cSChuck Lever * nsm_monitor - Notify a peer in case we reboot
1391e49323cSChuck Lever * @host: pointer to nlm_host of peer to notify
1401e49323cSChuck Lever *
1411e49323cSChuck Lever * If this peer is not already monitored, this function sends an
1421e49323cSChuck Lever * upcall to the local rpc.statd to record the name/address of
1431e49323cSChuck Lever * the peer to notify in case we reboot.
1441e49323cSChuck Lever *
1451e49323cSChuck Lever * Returns zero if the peer is monitored by the local rpc.statd;
1461e49323cSChuck Lever * otherwise a negative errno value is returned.
1471da177e4SLinus Torvalds */
nsm_monitor(const struct nlm_host * host)1481e49323cSChuck Lever int nsm_monitor(const struct nlm_host *host)
1491da177e4SLinus Torvalds {
1508dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle;
1511da177e4SLinus Torvalds struct nsm_res res;
1521da177e4SLinus Torvalds int status;
1531da177e4SLinus Torvalds
1549fee4902SChuck Lever dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
1558dead0dbSOlaf Kirch
1568dead0dbSOlaf Kirch if (nsm->sm_monitored)
157977faf39SOlaf Kirch return 0;
1581da177e4SLinus Torvalds
15929ed1407SChuck Lever /*
16029ed1407SChuck Lever * Choose whether to record the caller_name or IP address of
16129ed1407SChuck Lever * this peer in the local rpc.statd's database.
16229ed1407SChuck Lever */
16329ed1407SChuck Lever nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
16429ed1407SChuck Lever
1650d0f4aabSAndrey Ryabinin status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, host);
1666c9dc425SChuck Lever if (unlikely(res.status != 0))
1675d254b11SChuck Lever status = -EIO;
1686c9dc425SChuck Lever if (unlikely(status < 0)) {
1699af94fc4SJeff Layton pr_notice_ratelimited("lockd: cannot monitor %s\n", nsm->sm_name);
1701da177e4SLinus Torvalds return status;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds
1736c9dc425SChuck Lever nsm->sm_monitored = 1;
1746c9dc425SChuck Lever if (unlikely(nsm_local_state != res.state)) {
1756c9dc425SChuck Lever nsm_local_state = res.state;
1766c9dc425SChuck Lever dprintk("lockd: NSM state changed to %d\n", nsm_local_state);
1776c9dc425SChuck Lever }
1786c9dc425SChuck Lever return 0;
1796c9dc425SChuck Lever }
1806c9dc425SChuck Lever
181356c3eb4SChuck Lever /**
182356c3eb4SChuck Lever * nsm_unmonitor - Unregister peer notification
183356c3eb4SChuck Lever * @host: pointer to nlm_host of peer to stop monitoring
184356c3eb4SChuck Lever *
185356c3eb4SChuck Lever * If this peer is monitored, this function sends an upcall to
186356c3eb4SChuck Lever * tell the local rpc.statd not to send this peer a notification
187356c3eb4SChuck Lever * when we reboot.
1881da177e4SLinus Torvalds */
nsm_unmonitor(const struct nlm_host * host)189356c3eb4SChuck Lever void nsm_unmonitor(const struct nlm_host *host)
1901da177e4SLinus Torvalds {
1918dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle;
1921da177e4SLinus Torvalds struct nsm_res res;
193356c3eb4SChuck Lever int status;
1941da177e4SLinus Torvalds
195c751082cSElena Reshetova if (refcount_read(&nsm->sm_count) == 1
1969502c522SOlaf Kirch && nsm->sm_monitored && !nsm->sm_sticky) {
1979fee4902SChuck Lever dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
1989502c522SOlaf Kirch
1990d0f4aabSAndrey Ryabinin status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, host);
2000c7aef45SChuck Lever if (res.status != 0)
2010c7aef45SChuck Lever status = -EIO;
2021da177e4SLinus Torvalds if (status < 0)
2039502c522SOlaf Kirch printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
2049fee4902SChuck Lever nsm->sm_name);
2059502c522SOlaf Kirch else
2068dead0dbSOlaf Kirch nsm->sm_monitored = 0;
207977faf39SOlaf Kirch }
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds
nsm_lookup_hostname(const struct list_head * nsm_handles,const char * hostname,const size_t len)2100ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_hostname(const struct list_head *nsm_handles,
2110ad95472SAndrey Ryabinin const char *hostname, const size_t len)
2123420a8c4SChuck Lever {
2133420a8c4SChuck Lever struct nsm_handle *nsm;
2143420a8c4SChuck Lever
2150ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link)
2163420a8c4SChuck Lever if (strlen(nsm->sm_name) == len &&
2173420a8c4SChuck Lever memcmp(nsm->sm_name, hostname, len) == 0)
2183420a8c4SChuck Lever return nsm;
2193420a8c4SChuck Lever return NULL;
2203420a8c4SChuck Lever }
2213420a8c4SChuck Lever
nsm_lookup_addr(const struct list_head * nsm_handles,const struct sockaddr * sap)2220ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_addr(const struct list_head *nsm_handles,
2230ad95472SAndrey Ryabinin const struct sockaddr *sap)
22477a3ef33SChuck Lever {
22577a3ef33SChuck Lever struct nsm_handle *nsm;
22677a3ef33SChuck Lever
2270ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link)
2284516fc04SJeff Layton if (rpc_cmp_addr(nsm_addr(nsm), sap))
22977a3ef33SChuck Lever return nsm;
23077a3ef33SChuck Lever return NULL;
23177a3ef33SChuck Lever }
23277a3ef33SChuck Lever
nsm_lookup_priv(const struct list_head * nsm_handles,const struct nsm_private * priv)2330ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_priv(const struct list_head *nsm_handles,
2340ad95472SAndrey Ryabinin const struct nsm_private *priv)
2353420a8c4SChuck Lever {
2363420a8c4SChuck Lever struct nsm_handle *nsm;
2373420a8c4SChuck Lever
2380ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link)
2393420a8c4SChuck Lever if (memcmp(nsm->sm_priv.data, priv->data,
2403420a8c4SChuck Lever sizeof(priv->data)) == 0)
2413420a8c4SChuck Lever return nsm;
2423420a8c4SChuck Lever return NULL;
2433420a8c4SChuck Lever }
2443420a8c4SChuck Lever
2457e44d3beSChuck Lever /*
2467e44d3beSChuck Lever * Construct a unique cookie to match this nsm_handle to this monitored
2477e44d3beSChuck Lever * host. It is passed to the local rpc.statd via NSMPROC_MON, and
2487e44d3beSChuck Lever * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
2497e44d3beSChuck Lever * requests.
2507e44d3beSChuck Lever *
25194da7663SChuck Lever * The NSM protocol requires that these cookies be unique while the
25294da7663SChuck Lever * system is running. We prefer a stronger requirement of making them
25394da7663SChuck Lever * unique across reboots. If user space bugs cause a stale cookie to
25494da7663SChuck Lever * be sent to the kernel, it could cause the wrong host to lose its
25594da7663SChuck Lever * lock state if cookies were not unique across reboots.
25694da7663SChuck Lever *
25794da7663SChuck Lever * The cookies are exposed only to local user space via loopback. They
25894da7663SChuck Lever * do not appear on the physical network. If we want greater security
25994da7663SChuck Lever * for some reason, nsm_init_private() could perform a one-way hash to
26094da7663SChuck Lever * obscure the contents of the cookie.
2617e44d3beSChuck Lever */
nsm_init_private(struct nsm_handle * nsm)2627e44d3beSChuck Lever static void nsm_init_private(struct nsm_handle *nsm)
2637e44d3beSChuck Lever {
26494da7663SChuck Lever u64 *p = (u64 *)&nsm->sm_priv.data;
265ad5b365cSMans Rullgard s64 ns;
26694da7663SChuck Lever
2675eaaed4fSThomas Gleixner ns = ktime_get_ns();
268ad5b365cSMans Rullgard put_unaligned(ns, p);
269ad5b365cSMans Rullgard put_unaligned((unsigned long)nsm, p + 1);
2707e44d3beSChuck Lever }
2717e44d3beSChuck Lever
nsm_create_handle(const struct sockaddr * sap,const size_t salen,const char * hostname,const size_t hostname_len)272b39b897cSChuck Lever static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
273b39b897cSChuck Lever const size_t salen,
274b39b897cSChuck Lever const char *hostname,
275b39b897cSChuck Lever const size_t hostname_len)
276b39b897cSChuck Lever {
277b39b897cSChuck Lever struct nsm_handle *new;
278b39b897cSChuck Lever
279de8d38cfSSu Hui if (!hostname)
280de8d38cfSSu Hui return NULL;
281de8d38cfSSu Hui
282b39b897cSChuck Lever new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
283b39b897cSChuck Lever if (unlikely(new == NULL))
284b39b897cSChuck Lever return NULL;
285b39b897cSChuck Lever
286c751082cSElena Reshetova refcount_set(&new->sm_count, 1);
287b39b897cSChuck Lever new->sm_name = (char *)(new + 1);
288b39b897cSChuck Lever memcpy(nsm_addr(new), sap, salen);
289b39b897cSChuck Lever new->sm_addrlen = salen;
290b39b897cSChuck Lever nsm_init_private(new);
291c15128c5SChuck Lever
292c15128c5SChuck Lever if (rpc_ntop(nsm_addr(new), new->sm_addrbuf,
293c15128c5SChuck Lever sizeof(new->sm_addrbuf)) == 0)
294c15128c5SChuck Lever (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf),
295c15128c5SChuck Lever "unsupported address family");
296b39b897cSChuck Lever memcpy(new->sm_name, hostname, hostname_len);
297b39b897cSChuck Lever new->sm_name[hostname_len] = '\0';
298b39b897cSChuck Lever
299b39b897cSChuck Lever return new;
300b39b897cSChuck Lever }
301b39b897cSChuck Lever
30267c6d107SChuck Lever /**
30392fd91b9SChuck Lever * nsm_get_handle - Find or create a cached nsm_handle
3040ad95472SAndrey Ryabinin * @net: network namespace
30567c6d107SChuck Lever * @sap: pointer to socket address of handle to find
30667c6d107SChuck Lever * @salen: length of socket address
30767c6d107SChuck Lever * @hostname: pointer to C string containing hostname to find
30867c6d107SChuck Lever * @hostname_len: length of C string
30967c6d107SChuck Lever *
31092fd91b9SChuck Lever * Behavior is modulated by the global nsm_use_hostnames variable.
31167c6d107SChuck Lever *
31292fd91b9SChuck Lever * Returns a cached nsm_handle after bumping its ref count, or
31392fd91b9SChuck Lever * returns a fresh nsm_handle if a handle that matches @sap and/or
31492fd91b9SChuck Lever * @hostname cannot be found in the handle cache. Returns NULL if
31592fd91b9SChuck Lever * an error occurs.
31667c6d107SChuck Lever */
nsm_get_handle(const struct net * net,const struct sockaddr * sap,const size_t salen,const char * hostname,const size_t hostname_len)3170ad95472SAndrey Ryabinin struct nsm_handle *nsm_get_handle(const struct net *net,
3180ad95472SAndrey Ryabinin const struct sockaddr *sap,
31992fd91b9SChuck Lever const size_t salen, const char *hostname,
32092fd91b9SChuck Lever const size_t hostname_len)
32167c6d107SChuck Lever {
32277a3ef33SChuck Lever struct nsm_handle *cached, *new = NULL;
3230ad95472SAndrey Ryabinin struct lockd_net *ln = net_generic(net, lockd_net_id);
32467c6d107SChuck Lever
32567c6d107SChuck Lever if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
32667c6d107SChuck Lever if (printk_ratelimit()) {
32767c6d107SChuck Lever printk(KERN_WARNING "Invalid hostname \"%.*s\" "
32867c6d107SChuck Lever "in NFS lock request\n",
32967c6d107SChuck Lever (int)hostname_len, hostname);
33067c6d107SChuck Lever }
33167c6d107SChuck Lever return NULL;
33267c6d107SChuck Lever }
33367c6d107SChuck Lever
33467c6d107SChuck Lever retry:
33567c6d107SChuck Lever spin_lock(&nsm_lock);
33667c6d107SChuck Lever
33777a3ef33SChuck Lever if (nsm_use_hostnames && hostname != NULL)
3380ad95472SAndrey Ryabinin cached = nsm_lookup_hostname(&ln->nsm_handles,
3390ad95472SAndrey Ryabinin hostname, hostname_len);
34077a3ef33SChuck Lever else
3410ad95472SAndrey Ryabinin cached = nsm_lookup_addr(&ln->nsm_handles, sap);
34277a3ef33SChuck Lever
34377a3ef33SChuck Lever if (cached != NULL) {
344c751082cSElena Reshetova refcount_inc(&cached->sm_count);
34577a3ef33SChuck Lever spin_unlock(&nsm_lock);
34677a3ef33SChuck Lever kfree(new);
34777a3ef33SChuck Lever dprintk("lockd: found nsm_handle for %s (%s), "
34877a3ef33SChuck Lever "cnt %d\n", cached->sm_name,
34977a3ef33SChuck Lever cached->sm_addrbuf,
350c751082cSElena Reshetova refcount_read(&cached->sm_count));
35177a3ef33SChuck Lever return cached;
35267c6d107SChuck Lever }
35377a3ef33SChuck Lever
35477a3ef33SChuck Lever if (new != NULL) {
3550ad95472SAndrey Ryabinin list_add(&new->sm_link, &ln->nsm_handles);
35677a3ef33SChuck Lever spin_unlock(&nsm_lock);
3575cf1c4b1SChuck Lever dprintk("lockd: created nsm_handle for %s (%s)\n",
35877a3ef33SChuck Lever new->sm_name, new->sm_addrbuf);
35977a3ef33SChuck Lever return new;
36067c6d107SChuck Lever }
36177a3ef33SChuck Lever
36267c6d107SChuck Lever spin_unlock(&nsm_lock);
36367c6d107SChuck Lever
36477a3ef33SChuck Lever new = nsm_create_handle(sap, salen, hostname, hostname_len);
36577a3ef33SChuck Lever if (unlikely(new == NULL))
36667c6d107SChuck Lever return NULL;
36767c6d107SChuck Lever goto retry;
36867c6d107SChuck Lever }
36967c6d107SChuck Lever
37067c6d107SChuck Lever /**
3713420a8c4SChuck Lever * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
3720ad95472SAndrey Ryabinin * @net: network namespace
3733420a8c4SChuck Lever * @info: pointer to NLMPROC_SM_NOTIFY arguments
3743420a8c4SChuck Lever *
3757e469af9SJeff Layton * Returns a matching nsm_handle if found in the nsm cache. The returned
3767e469af9SJeff Layton * nsm_handle's reference count is bumped. Otherwise returns NULL if some
3777e469af9SJeff Layton * error occurred.
3783420a8c4SChuck Lever */
nsm_reboot_lookup(const struct net * net,const struct nlm_reboot * info)3790ad95472SAndrey Ryabinin struct nsm_handle *nsm_reboot_lookup(const struct net *net,
3800ad95472SAndrey Ryabinin const struct nlm_reboot *info)
3813420a8c4SChuck Lever {
3823420a8c4SChuck Lever struct nsm_handle *cached;
3830ad95472SAndrey Ryabinin struct lockd_net *ln = net_generic(net, lockd_net_id);
3843420a8c4SChuck Lever
3853420a8c4SChuck Lever spin_lock(&nsm_lock);
3863420a8c4SChuck Lever
3870ad95472SAndrey Ryabinin cached = nsm_lookup_priv(&ln->nsm_handles, &info->priv);
3883420a8c4SChuck Lever if (unlikely(cached == NULL)) {
3893420a8c4SChuck Lever spin_unlock(&nsm_lock);
3903420a8c4SChuck Lever dprintk("lockd: never saw rebooted peer '%.*s' before\n",
3913420a8c4SChuck Lever info->len, info->mon);
3923420a8c4SChuck Lever return cached;
3933420a8c4SChuck Lever }
3943420a8c4SChuck Lever
395c751082cSElena Reshetova refcount_inc(&cached->sm_count);
3963420a8c4SChuck Lever spin_unlock(&nsm_lock);
3973420a8c4SChuck Lever
3983420a8c4SChuck Lever dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
3993420a8c4SChuck Lever cached->sm_name, cached->sm_addrbuf,
400c751082cSElena Reshetova refcount_read(&cached->sm_count));
4013420a8c4SChuck Lever return cached;
4023420a8c4SChuck Lever }
4033420a8c4SChuck Lever
4043420a8c4SChuck Lever /**
40567c6d107SChuck Lever * nsm_release - Release an NSM handle
40667c6d107SChuck Lever * @nsm: pointer to handle to be released
40767c6d107SChuck Lever *
40867c6d107SChuck Lever */
nsm_release(struct nsm_handle * nsm)40967c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm)
41067c6d107SChuck Lever {
411c751082cSElena Reshetova if (refcount_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
41267c6d107SChuck Lever list_del(&nsm->sm_link);
41367c6d107SChuck Lever spin_unlock(&nsm_lock);
4145cf1c4b1SChuck Lever dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
4155cf1c4b1SChuck Lever nsm->sm_name, nsm->sm_addrbuf);
41667c6d107SChuck Lever kfree(nsm);
41767c6d107SChuck Lever }
41867c6d107SChuck Lever }
41967c6d107SChuck Lever
4201da177e4SLinus Torvalds /*
4211da177e4SLinus Torvalds * XDR functions for NSM.
4222ca7754dSChuck Lever *
4230bdd4ceaSAlexander A. Klimov * See https://www.opengroup.org/ for details on the Network
4242ca7754dSChuck Lever * Status Monitor wire protocol.
4251da177e4SLinus Torvalds */
4261da177e4SLinus Torvalds
encode_nsm_string(struct xdr_stream * xdr,const char * string)42749b17004SChuck Lever static void encode_nsm_string(struct xdr_stream *xdr, const char *string)
428099bd05fSChuck Lever {
42903eb1dcbSChuck Lever const u32 len = strlen(string);
43003eb1dcbSChuck Lever __be32 *p;
431099bd05fSChuck Lever
43249b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + len);
43303eb1dcbSChuck Lever xdr_encode_opaque(p, string, len);
434099bd05fSChuck Lever }
435099bd05fSChuck Lever
43649695174SChuck Lever /*
43749695174SChuck Lever * "mon_name" specifies the host to be monitored.
43849695174SChuck Lever */
encode_mon_name(struct xdr_stream * xdr,const struct nsm_args * argp)43949b17004SChuck Lever static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
44049695174SChuck Lever {
44149b17004SChuck Lever encode_nsm_string(xdr, argp->mon_name);
44249695174SChuck Lever }
44349695174SChuck Lever
444850c95fdSChuck Lever /*
445850c95fdSChuck Lever * The "my_id" argument specifies the hostname and RPC procedure
446850c95fdSChuck Lever * to be called when the status manager receives notification
44736e8e668SChuck Lever * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
448850c95fdSChuck Lever * has changed.
449850c95fdSChuck Lever */
encode_my_id(struct xdr_stream * xdr,const struct nsm_args * argp)45049b17004SChuck Lever static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
451850c95fdSChuck Lever {
45203eb1dcbSChuck Lever __be32 *p;
453850c95fdSChuck Lever
454303a7ce9SStanislav Kinsbursky encode_nsm_string(xdr, argp->nodename);
45549b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + 4 + 4);
45649b17004SChuck Lever *p++ = cpu_to_be32(argp->prog);
45749b17004SChuck Lever *p++ = cpu_to_be32(argp->vers);
45849b17004SChuck Lever *p = cpu_to_be32(argp->proc);
459850c95fdSChuck Lever }
460850c95fdSChuck Lever
461ea72a7f1SChuck Lever /*
462ea72a7f1SChuck Lever * The "mon_id" argument specifies the non-private arguments
46336e8e668SChuck Lever * of an NSMPROC_MON or NSMPROC_UNMON call.
464ea72a7f1SChuck Lever */
encode_mon_id(struct xdr_stream * xdr,const struct nsm_args * argp)46549b17004SChuck Lever static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
466ea72a7f1SChuck Lever {
46749b17004SChuck Lever encode_mon_name(xdr, argp);
46849b17004SChuck Lever encode_my_id(xdr, argp);
469ea72a7f1SChuck Lever }
470ea72a7f1SChuck Lever
4710490a54aSChuck Lever /*
4720490a54aSChuck Lever * The "priv" argument may contain private information required
47336e8e668SChuck Lever * by the NSMPROC_MON call. This information will be supplied in the
47436e8e668SChuck Lever * NLMPROC_SM_NOTIFY call.
4750490a54aSChuck Lever */
encode_priv(struct xdr_stream * xdr,const struct nsm_args * argp)47649b17004SChuck Lever static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
4770490a54aSChuck Lever {
47803eb1dcbSChuck Lever __be32 *p;
47903eb1dcbSChuck Lever
48003eb1dcbSChuck Lever p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
481cab2d3c9SChuck Lever xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds
nsm_xdr_enc_mon(struct rpc_rqst * req,struct xdr_stream * xdr,const void * argp)4849f06c719SChuck Lever static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr,
485bf96391eSChristoph Hellwig const void *argp)
4861da177e4SLinus Torvalds {
4879f06c719SChuck Lever encode_mon_id(xdr, argp);
4889f06c719SChuck Lever encode_priv(xdr, argp);
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds
nsm_xdr_enc_unmon(struct rpc_rqst * req,struct xdr_stream * xdr,const void * argp)4919f06c719SChuck Lever static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr,
492bf96391eSChristoph Hellwig const void *argp)
4931da177e4SLinus Torvalds {
4949f06c719SChuck Lever encode_mon_id(xdr, argp);
49503eb1dcbSChuck Lever }
49603eb1dcbSChuck Lever
nsm_xdr_dec_stat_res(struct rpc_rqst * rqstp,struct xdr_stream * xdr,void * data)497bf269551SChuck Lever static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp,
498bf269551SChuck Lever struct xdr_stream *xdr,
4991fa23391SChristoph Hellwig void *data)
50003eb1dcbSChuck Lever {
5011fa23391SChristoph Hellwig struct nsm_res *resp = data;
502bf269551SChuck Lever __be32 *p;
50303eb1dcbSChuck Lever
504bf269551SChuck Lever p = xdr_inline_decode(xdr, 4 + 4);
50503eb1dcbSChuck Lever if (unlikely(p == NULL))
50603eb1dcbSChuck Lever return -EIO;
50749b17004SChuck Lever resp->status = be32_to_cpup(p++);
50849b17004SChuck Lever resp->state = be32_to_cpup(p);
50903eb1dcbSChuck Lever
510bf269551SChuck Lever dprintk("lockd: %s status %d state %d\n",
511bf269551SChuck Lever __func__, resp->status, resp->state);
5121da177e4SLinus Torvalds return 0;
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds
nsm_xdr_dec_stat(struct rpc_rqst * rqstp,struct xdr_stream * xdr,void * data)515bf269551SChuck Lever static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp,
516bf269551SChuck Lever struct xdr_stream *xdr,
5171fa23391SChristoph Hellwig void *data)
5181da177e4SLinus Torvalds {
5191fa23391SChristoph Hellwig struct nsm_res *resp = data;
520bf269551SChuck Lever __be32 *p;
52103eb1dcbSChuck Lever
522bf269551SChuck Lever p = xdr_inline_decode(xdr, 4);
52303eb1dcbSChuck Lever if (unlikely(p == NULL))
52403eb1dcbSChuck Lever return -EIO;
52549b17004SChuck Lever resp->state = be32_to_cpup(p);
52603eb1dcbSChuck Lever
527bf269551SChuck Lever dprintk("lockd: %s state %d\n", __func__, resp->state);
5281da177e4SLinus Torvalds return 0;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds
5311da177e4SLinus Torvalds #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
5322ca7754dSChuck Lever #define SM_my_id_sz (SM_my_name_sz+3)
5332ca7754dSChuck Lever #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
5342ca7754dSChuck Lever #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
5350490a54aSChuck Lever #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
5360490a54aSChuck Lever #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
5371da177e4SLinus Torvalds #define SM_monres_sz 2
5381da177e4SLinus Torvalds #define SM_unmonres_sz 1
5391da177e4SLinus Torvalds
540499b4988SChristoph Hellwig static const struct rpc_procinfo nsm_procedures[] = {
54136e8e668SChuck Lever [NSMPROC_MON] = {
54236e8e668SChuck Lever .p_proc = NSMPROC_MON,
543bf96391eSChristoph Hellwig .p_encode = nsm_xdr_enc_mon,
5441fa23391SChristoph Hellwig .p_decode = nsm_xdr_dec_stat_res,
5452bea90d4SChuck Lever .p_arglen = SM_mon_sz,
5462bea90d4SChuck Lever .p_replen = SM_monres_sz,
54736e8e668SChuck Lever .p_statidx = NSMPROC_MON,
548cc0175c1SChuck Lever .p_name = "MONITOR",
5491da177e4SLinus Torvalds },
55036e8e668SChuck Lever [NSMPROC_UNMON] = {
55136e8e668SChuck Lever .p_proc = NSMPROC_UNMON,
552bf96391eSChristoph Hellwig .p_encode = nsm_xdr_enc_unmon,
5531fa23391SChristoph Hellwig .p_decode = nsm_xdr_dec_stat,
5542bea90d4SChuck Lever .p_arglen = SM_mon_id_sz,
5552bea90d4SChuck Lever .p_replen = SM_unmonres_sz,
55636e8e668SChuck Lever .p_statidx = NSMPROC_UNMON,
557cc0175c1SChuck Lever .p_name = "UNMONITOR",
5581da177e4SLinus Torvalds },
5591da177e4SLinus Torvalds };
5601da177e4SLinus Torvalds
5611c5876ddSChristoph Hellwig static unsigned int nsm_version1_counts[ARRAY_SIZE(nsm_procedures)];
562a613fa16STrond Myklebust static const struct rpc_version nsm_version1 = {
5631da177e4SLinus Torvalds .number = 1,
564e8c96f8cSTobias Klauser .nrprocs = ARRAY_SIZE(nsm_procedures),
5651c5876ddSChristoph Hellwig .procs = nsm_procedures,
5661c5876ddSChristoph Hellwig .counts = nsm_version1_counts,
5671da177e4SLinus Torvalds };
5681da177e4SLinus Torvalds
569a613fa16STrond Myklebust static const struct rpc_version *nsm_version[] = {
5701da177e4SLinus Torvalds [1] = &nsm_version1,
5711da177e4SLinus Torvalds };
5721da177e4SLinus Torvalds
5731da177e4SLinus Torvalds static struct rpc_stat nsm_stats;
5741da177e4SLinus Torvalds
575a613fa16STrond Myklebust static const struct rpc_program nsm_program = {
5761da177e4SLinus Torvalds .name = "statd",
57736e8e668SChuck Lever .number = NSM_PROGRAM,
578e8c96f8cSTobias Klauser .nrvers = ARRAY_SIZE(nsm_version),
5791da177e4SLinus Torvalds .version = nsm_version,
5801da177e4SLinus Torvalds .stats = &nsm_stats
5811da177e4SLinus Torvalds };
582