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, 8579caa5faSTrond Myklebust .cred = current_cred(), 8649b5699bSChuck Lever }; 8749b5699bSChuck Lever 8849b5699bSChuck Lever return rpc_create(&args); 8949b5699bSChuck Lever } 9049b5699bSChuck Lever 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 */ 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 */ 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 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 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 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 */ 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 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 279b39b897cSChuck Lever new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL); 280b39b897cSChuck Lever if (unlikely(new == NULL)) 281b39b897cSChuck Lever return NULL; 282b39b897cSChuck Lever 283c751082cSElena Reshetova refcount_set(&new->sm_count, 1); 284b39b897cSChuck Lever new->sm_name = (char *)(new + 1); 285b39b897cSChuck Lever memcpy(nsm_addr(new), sap, salen); 286b39b897cSChuck Lever new->sm_addrlen = salen; 287b39b897cSChuck Lever nsm_init_private(new); 288c15128c5SChuck Lever 289c15128c5SChuck Lever if (rpc_ntop(nsm_addr(new), new->sm_addrbuf, 290c15128c5SChuck Lever sizeof(new->sm_addrbuf)) == 0) 291c15128c5SChuck Lever (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf), 292c15128c5SChuck Lever "unsupported address family"); 293b39b897cSChuck Lever memcpy(new->sm_name, hostname, hostname_len); 294b39b897cSChuck Lever new->sm_name[hostname_len] = '\0'; 295b39b897cSChuck Lever 296b39b897cSChuck Lever return new; 297b39b897cSChuck Lever } 298b39b897cSChuck Lever 29967c6d107SChuck Lever /** 30092fd91b9SChuck Lever * nsm_get_handle - Find or create a cached nsm_handle 3010ad95472SAndrey Ryabinin * @net: network namespace 30267c6d107SChuck Lever * @sap: pointer to socket address of handle to find 30367c6d107SChuck Lever * @salen: length of socket address 30467c6d107SChuck Lever * @hostname: pointer to C string containing hostname to find 30567c6d107SChuck Lever * @hostname_len: length of C string 30667c6d107SChuck Lever * 30792fd91b9SChuck Lever * Behavior is modulated by the global nsm_use_hostnames variable. 30867c6d107SChuck Lever * 30992fd91b9SChuck Lever * Returns a cached nsm_handle after bumping its ref count, or 31092fd91b9SChuck Lever * returns a fresh nsm_handle if a handle that matches @sap and/or 31192fd91b9SChuck Lever * @hostname cannot be found in the handle cache. Returns NULL if 31292fd91b9SChuck Lever * an error occurs. 31367c6d107SChuck Lever */ 3140ad95472SAndrey Ryabinin struct nsm_handle *nsm_get_handle(const struct net *net, 3150ad95472SAndrey Ryabinin const struct sockaddr *sap, 31692fd91b9SChuck Lever const size_t salen, const char *hostname, 31792fd91b9SChuck Lever const size_t hostname_len) 31867c6d107SChuck Lever { 31977a3ef33SChuck Lever struct nsm_handle *cached, *new = NULL; 3200ad95472SAndrey Ryabinin struct lockd_net *ln = net_generic(net, lockd_net_id); 32167c6d107SChuck Lever 32267c6d107SChuck Lever if (hostname && memchr(hostname, '/', hostname_len) != NULL) { 32367c6d107SChuck Lever if (printk_ratelimit()) { 32467c6d107SChuck Lever printk(KERN_WARNING "Invalid hostname \"%.*s\" " 32567c6d107SChuck Lever "in NFS lock request\n", 32667c6d107SChuck Lever (int)hostname_len, hostname); 32767c6d107SChuck Lever } 32867c6d107SChuck Lever return NULL; 32967c6d107SChuck Lever } 33067c6d107SChuck Lever 33167c6d107SChuck Lever retry: 33267c6d107SChuck Lever spin_lock(&nsm_lock); 33367c6d107SChuck Lever 33477a3ef33SChuck Lever if (nsm_use_hostnames && hostname != NULL) 3350ad95472SAndrey Ryabinin cached = nsm_lookup_hostname(&ln->nsm_handles, 3360ad95472SAndrey Ryabinin hostname, hostname_len); 33777a3ef33SChuck Lever else 3380ad95472SAndrey Ryabinin cached = nsm_lookup_addr(&ln->nsm_handles, sap); 33977a3ef33SChuck Lever 34077a3ef33SChuck Lever if (cached != NULL) { 341c751082cSElena Reshetova refcount_inc(&cached->sm_count); 34277a3ef33SChuck Lever spin_unlock(&nsm_lock); 34377a3ef33SChuck Lever kfree(new); 34477a3ef33SChuck Lever dprintk("lockd: found nsm_handle for %s (%s), " 34577a3ef33SChuck Lever "cnt %d\n", cached->sm_name, 34677a3ef33SChuck Lever cached->sm_addrbuf, 347c751082cSElena Reshetova refcount_read(&cached->sm_count)); 34877a3ef33SChuck Lever return cached; 34967c6d107SChuck Lever } 35077a3ef33SChuck Lever 35177a3ef33SChuck Lever if (new != NULL) { 3520ad95472SAndrey Ryabinin list_add(&new->sm_link, &ln->nsm_handles); 35377a3ef33SChuck Lever spin_unlock(&nsm_lock); 3545cf1c4b1SChuck Lever dprintk("lockd: created nsm_handle for %s (%s)\n", 35577a3ef33SChuck Lever new->sm_name, new->sm_addrbuf); 35677a3ef33SChuck Lever return new; 35767c6d107SChuck Lever } 35877a3ef33SChuck Lever 35967c6d107SChuck Lever spin_unlock(&nsm_lock); 36067c6d107SChuck Lever 36177a3ef33SChuck Lever new = nsm_create_handle(sap, salen, hostname, hostname_len); 36277a3ef33SChuck Lever if (unlikely(new == NULL)) 36367c6d107SChuck Lever return NULL; 36467c6d107SChuck Lever goto retry; 36567c6d107SChuck Lever } 36667c6d107SChuck Lever 36767c6d107SChuck Lever /** 3683420a8c4SChuck Lever * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle 3690ad95472SAndrey Ryabinin * @net: network namespace 3703420a8c4SChuck Lever * @info: pointer to NLMPROC_SM_NOTIFY arguments 3713420a8c4SChuck Lever * 3727e469af9SJeff Layton * Returns a matching nsm_handle if found in the nsm cache. The returned 3737e469af9SJeff Layton * nsm_handle's reference count is bumped. Otherwise returns NULL if some 3747e469af9SJeff Layton * error occurred. 3753420a8c4SChuck Lever */ 3760ad95472SAndrey Ryabinin struct nsm_handle *nsm_reboot_lookup(const struct net *net, 3770ad95472SAndrey Ryabinin const struct nlm_reboot *info) 3783420a8c4SChuck Lever { 3793420a8c4SChuck Lever struct nsm_handle *cached; 3800ad95472SAndrey Ryabinin struct lockd_net *ln = net_generic(net, lockd_net_id); 3813420a8c4SChuck Lever 3823420a8c4SChuck Lever spin_lock(&nsm_lock); 3833420a8c4SChuck Lever 3840ad95472SAndrey Ryabinin cached = nsm_lookup_priv(&ln->nsm_handles, &info->priv); 3853420a8c4SChuck Lever if (unlikely(cached == NULL)) { 3863420a8c4SChuck Lever spin_unlock(&nsm_lock); 3873420a8c4SChuck Lever dprintk("lockd: never saw rebooted peer '%.*s' before\n", 3883420a8c4SChuck Lever info->len, info->mon); 3893420a8c4SChuck Lever return cached; 3903420a8c4SChuck Lever } 3913420a8c4SChuck Lever 392c751082cSElena Reshetova refcount_inc(&cached->sm_count); 3933420a8c4SChuck Lever spin_unlock(&nsm_lock); 3943420a8c4SChuck Lever 3953420a8c4SChuck Lever dprintk("lockd: host %s (%s) rebooted, cnt %d\n", 3963420a8c4SChuck Lever cached->sm_name, cached->sm_addrbuf, 397c751082cSElena Reshetova refcount_read(&cached->sm_count)); 3983420a8c4SChuck Lever return cached; 3993420a8c4SChuck Lever } 4003420a8c4SChuck Lever 4013420a8c4SChuck Lever /** 40267c6d107SChuck Lever * nsm_release - Release an NSM handle 40367c6d107SChuck Lever * @nsm: pointer to handle to be released 40467c6d107SChuck Lever * 40567c6d107SChuck Lever */ 40667c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm) 40767c6d107SChuck Lever { 408c751082cSElena Reshetova if (refcount_dec_and_lock(&nsm->sm_count, &nsm_lock)) { 40967c6d107SChuck Lever list_del(&nsm->sm_link); 41067c6d107SChuck Lever spin_unlock(&nsm_lock); 4115cf1c4b1SChuck Lever dprintk("lockd: destroyed nsm_handle for %s (%s)\n", 4125cf1c4b1SChuck Lever nsm->sm_name, nsm->sm_addrbuf); 41367c6d107SChuck Lever kfree(nsm); 41467c6d107SChuck Lever } 41567c6d107SChuck Lever } 41667c6d107SChuck Lever 4171da177e4SLinus Torvalds /* 4181da177e4SLinus Torvalds * XDR functions for NSM. 4192ca7754dSChuck Lever * 420*0bdd4ceaSAlexander A. Klimov * See https://www.opengroup.org/ for details on the Network 4212ca7754dSChuck Lever * Status Monitor wire protocol. 4221da177e4SLinus Torvalds */ 4231da177e4SLinus Torvalds 42449b17004SChuck Lever static void encode_nsm_string(struct xdr_stream *xdr, const char *string) 425099bd05fSChuck Lever { 42603eb1dcbSChuck Lever const u32 len = strlen(string); 42703eb1dcbSChuck Lever __be32 *p; 428099bd05fSChuck Lever 42949b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + len); 43003eb1dcbSChuck Lever xdr_encode_opaque(p, string, len); 431099bd05fSChuck Lever } 432099bd05fSChuck Lever 43349695174SChuck Lever /* 43449695174SChuck Lever * "mon_name" specifies the host to be monitored. 43549695174SChuck Lever */ 43649b17004SChuck Lever static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp) 43749695174SChuck Lever { 43849b17004SChuck Lever encode_nsm_string(xdr, argp->mon_name); 43949695174SChuck Lever } 44049695174SChuck Lever 441850c95fdSChuck Lever /* 442850c95fdSChuck Lever * The "my_id" argument specifies the hostname and RPC procedure 443850c95fdSChuck Lever * to be called when the status manager receives notification 44436e8e668SChuck Lever * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name" 445850c95fdSChuck Lever * has changed. 446850c95fdSChuck Lever */ 44749b17004SChuck Lever static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp) 448850c95fdSChuck Lever { 44903eb1dcbSChuck Lever __be32 *p; 450850c95fdSChuck Lever 451303a7ce9SStanislav Kinsbursky encode_nsm_string(xdr, argp->nodename); 45249b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + 4 + 4); 45349b17004SChuck Lever *p++ = cpu_to_be32(argp->prog); 45449b17004SChuck Lever *p++ = cpu_to_be32(argp->vers); 45549b17004SChuck Lever *p = cpu_to_be32(argp->proc); 456850c95fdSChuck Lever } 457850c95fdSChuck Lever 458ea72a7f1SChuck Lever /* 459ea72a7f1SChuck Lever * The "mon_id" argument specifies the non-private arguments 46036e8e668SChuck Lever * of an NSMPROC_MON or NSMPROC_UNMON call. 461ea72a7f1SChuck Lever */ 46249b17004SChuck Lever static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp) 463ea72a7f1SChuck Lever { 46449b17004SChuck Lever encode_mon_name(xdr, argp); 46549b17004SChuck Lever encode_my_id(xdr, argp); 466ea72a7f1SChuck Lever } 467ea72a7f1SChuck Lever 4680490a54aSChuck Lever /* 4690490a54aSChuck Lever * The "priv" argument may contain private information required 47036e8e668SChuck Lever * by the NSMPROC_MON call. This information will be supplied in the 47136e8e668SChuck Lever * NLMPROC_SM_NOTIFY call. 4720490a54aSChuck Lever */ 47349b17004SChuck Lever static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp) 4740490a54aSChuck Lever { 47503eb1dcbSChuck Lever __be32 *p; 47603eb1dcbSChuck Lever 47703eb1dcbSChuck Lever p = xdr_reserve_space(xdr, SM_PRIV_SIZE); 478cab2d3c9SChuck Lever xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE); 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4819f06c719SChuck Lever static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr, 482bf96391eSChristoph Hellwig const void *argp) 4831da177e4SLinus Torvalds { 4849f06c719SChuck Lever encode_mon_id(xdr, argp); 4859f06c719SChuck Lever encode_priv(xdr, argp); 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4889f06c719SChuck Lever static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr, 489bf96391eSChristoph Hellwig const void *argp) 4901da177e4SLinus Torvalds { 4919f06c719SChuck Lever encode_mon_id(xdr, argp); 49203eb1dcbSChuck Lever } 49303eb1dcbSChuck Lever 494bf269551SChuck Lever static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp, 495bf269551SChuck Lever struct xdr_stream *xdr, 4961fa23391SChristoph Hellwig void *data) 49703eb1dcbSChuck Lever { 4981fa23391SChristoph Hellwig struct nsm_res *resp = data; 499bf269551SChuck Lever __be32 *p; 50003eb1dcbSChuck Lever 501bf269551SChuck Lever p = xdr_inline_decode(xdr, 4 + 4); 50203eb1dcbSChuck Lever if (unlikely(p == NULL)) 50303eb1dcbSChuck Lever return -EIO; 50449b17004SChuck Lever resp->status = be32_to_cpup(p++); 50549b17004SChuck Lever resp->state = be32_to_cpup(p); 50603eb1dcbSChuck Lever 507bf269551SChuck Lever dprintk("lockd: %s status %d state %d\n", 508bf269551SChuck Lever __func__, resp->status, resp->state); 5091da177e4SLinus Torvalds return 0; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 512bf269551SChuck Lever static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp, 513bf269551SChuck Lever struct xdr_stream *xdr, 5141fa23391SChristoph Hellwig void *data) 5151da177e4SLinus Torvalds { 5161fa23391SChristoph Hellwig struct nsm_res *resp = data; 517bf269551SChuck Lever __be32 *p; 51803eb1dcbSChuck Lever 519bf269551SChuck Lever p = xdr_inline_decode(xdr, 4); 52003eb1dcbSChuck Lever if (unlikely(p == NULL)) 52103eb1dcbSChuck Lever return -EIO; 52249b17004SChuck Lever resp->state = be32_to_cpup(p); 52303eb1dcbSChuck Lever 524bf269551SChuck Lever dprintk("lockd: %s state %d\n", __func__, resp->state); 5251da177e4SLinus Torvalds return 0; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5292ca7754dSChuck Lever #define SM_my_id_sz (SM_my_name_sz+3) 5302ca7754dSChuck Lever #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5312ca7754dSChuck Lever #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz) 5320490a54aSChuck Lever #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE)) 5330490a54aSChuck Lever #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz) 5341da177e4SLinus Torvalds #define SM_monres_sz 2 5351da177e4SLinus Torvalds #define SM_unmonres_sz 1 5361da177e4SLinus Torvalds 537499b4988SChristoph Hellwig static const struct rpc_procinfo nsm_procedures[] = { 53836e8e668SChuck Lever [NSMPROC_MON] = { 53936e8e668SChuck Lever .p_proc = NSMPROC_MON, 540bf96391eSChristoph Hellwig .p_encode = nsm_xdr_enc_mon, 5411fa23391SChristoph Hellwig .p_decode = nsm_xdr_dec_stat_res, 5422bea90d4SChuck Lever .p_arglen = SM_mon_sz, 5432bea90d4SChuck Lever .p_replen = SM_monres_sz, 54436e8e668SChuck Lever .p_statidx = NSMPROC_MON, 545cc0175c1SChuck Lever .p_name = "MONITOR", 5461da177e4SLinus Torvalds }, 54736e8e668SChuck Lever [NSMPROC_UNMON] = { 54836e8e668SChuck Lever .p_proc = NSMPROC_UNMON, 549bf96391eSChristoph Hellwig .p_encode = nsm_xdr_enc_unmon, 5501fa23391SChristoph Hellwig .p_decode = nsm_xdr_dec_stat, 5512bea90d4SChuck Lever .p_arglen = SM_mon_id_sz, 5522bea90d4SChuck Lever .p_replen = SM_unmonres_sz, 55336e8e668SChuck Lever .p_statidx = NSMPROC_UNMON, 554cc0175c1SChuck Lever .p_name = "UNMONITOR", 5551da177e4SLinus Torvalds }, 5561da177e4SLinus Torvalds }; 5571da177e4SLinus Torvalds 5581c5876ddSChristoph Hellwig static unsigned int nsm_version1_counts[ARRAY_SIZE(nsm_procedures)]; 559a613fa16STrond Myklebust static const struct rpc_version nsm_version1 = { 5601da177e4SLinus Torvalds .number = 1, 561e8c96f8cSTobias Klauser .nrprocs = ARRAY_SIZE(nsm_procedures), 5621c5876ddSChristoph Hellwig .procs = nsm_procedures, 5631c5876ddSChristoph Hellwig .counts = nsm_version1_counts, 5641da177e4SLinus Torvalds }; 5651da177e4SLinus Torvalds 566a613fa16STrond Myklebust static const struct rpc_version *nsm_version[] = { 5671da177e4SLinus Torvalds [1] = &nsm_version1, 5681da177e4SLinus Torvalds }; 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds static struct rpc_stat nsm_stats; 5711da177e4SLinus Torvalds 572a613fa16STrond Myklebust static const struct rpc_program nsm_program = { 5731da177e4SLinus Torvalds .name = "statd", 57436e8e668SChuck Lever .number = NSM_PROGRAM, 575e8c96f8cSTobias Klauser .nrvers = ARRAY_SIZE(nsm_version), 5761da177e4SLinus Torvalds .version = nsm_version, 5771da177e4SLinus Torvalds .stats = &nsm_stats 5781da177e4SLinus Torvalds }; 579