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/kernel.h> 1194da7663SChuck Lever #include <linux/ktime.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 1394da7663SChuck Lever 141da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 155976687aSJeff Layton #include <linux/sunrpc/addr.h> 160896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 171da177e4SLinus Torvalds #include <linux/sunrpc/svc.h> 181da177e4SLinus Torvalds #include <linux/lockd/lockd.h> 191da177e4SLinus Torvalds 20ad5b365cSMans Rullgard #include <asm/unaligned.h> 21ad5b365cSMans Rullgard 22e9406db2SStanislav Kinsbursky #include "netns.h" 23e9406db2SStanislav Kinsbursky 241da177e4SLinus Torvalds #define NLMDBG_FACILITY NLMDBG_MONITOR 2536e8e668SChuck Lever #define NSM_PROGRAM 100024 2636e8e668SChuck Lever #define NSM_VERSION 1 2736e8e668SChuck Lever 2836e8e668SChuck Lever enum { 2936e8e668SChuck Lever NSMPROC_NULL, 3036e8e668SChuck Lever NSMPROC_STAT, 3136e8e668SChuck Lever NSMPROC_MON, 3236e8e668SChuck Lever NSMPROC_UNMON, 3336e8e668SChuck Lever NSMPROC_UNMON_ALL, 3436e8e668SChuck Lever NSMPROC_SIMU_CRASH, 3536e8e668SChuck Lever NSMPROC_NOTIFY, 3636e8e668SChuck Lever }; 371da177e4SLinus Torvalds 389c1bfd03SChuck Lever struct nsm_args { 39cab2d3c9SChuck Lever struct nsm_private *priv; 409c1bfd03SChuck Lever u32 prog; /* RPC callback info */ 419c1bfd03SChuck Lever u32 vers; 429c1bfd03SChuck Lever u32 proc; 439c1bfd03SChuck Lever 449c1bfd03SChuck Lever char *mon_name; 450d0f4aabSAndrey Ryabinin const char *nodename; 469c1bfd03SChuck Lever }; 479c1bfd03SChuck Lever 489c1bfd03SChuck Lever struct nsm_res { 499c1bfd03SChuck Lever u32 status; 509c1bfd03SChuck Lever u32 state; 519c1bfd03SChuck Lever }; 529c1bfd03SChuck Lever 53a613fa16STrond Myklebust static const struct rpc_program nsm_program; 5467c6d107SChuck Lever static DEFINE_SPINLOCK(nsm_lock); 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* 571da177e4SLinus Torvalds * Local NSM state 581da177e4SLinus Torvalds */ 596c9dc425SChuck Lever u32 __read_mostly nsm_local_state; 6090ab5ee9SRusty Russell bool __read_mostly nsm_use_hostnames; 611da177e4SLinus Torvalds 628529bc51SChuck Lever static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm) 638529bc51SChuck Lever { 648529bc51SChuck Lever return (struct sockaddr *)&nsm->sm_addr; 658529bc51SChuck Lever } 668529bc51SChuck Lever 6703a9a42aSTrond Myklebust static struct rpc_clnt *nsm_create(struct net *net, const char *nodename) 6849b5699bSChuck Lever { 6949b5699bSChuck Lever struct sockaddr_in sin = { 7049b5699bSChuck Lever .sin_family = AF_INET, 7149b5699bSChuck Lever .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 7249b5699bSChuck Lever }; 7349b5699bSChuck Lever struct rpc_create_args args = { 740e1cb5c0SStanislav Kinsbursky .net = net, 75e9406db2SStanislav Kinsbursky .protocol = XPRT_TRANSPORT_TCP, 7649b5699bSChuck Lever .address = (struct sockaddr *)&sin, 7749b5699bSChuck Lever .addrsize = sizeof(sin), 7849b5699bSChuck Lever .servername = "rpc.statd", 7903a9a42aSTrond Myklebust .nodename = nodename, 8049b5699bSChuck Lever .program = &nsm_program, 8149b5699bSChuck Lever .version = NSM_VERSION, 8249b5699bSChuck Lever .authflavor = RPC_AUTH_NULL, 830e5c2632SChuck Lever .flags = RPC_CLNT_CREATE_NOPING, 8449b5699bSChuck Lever }; 8549b5699bSChuck Lever 8649b5699bSChuck Lever return rpc_create(&args); 8749b5699bSChuck Lever } 8849b5699bSChuck Lever 890e1cb5c0SStanislav Kinsbursky static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res, 900d0f4aabSAndrey Ryabinin const struct nlm_host *host) 911da177e4SLinus Torvalds { 921da177e4SLinus Torvalds int status; 930d0f4aabSAndrey Ryabinin struct rpc_clnt *clnt; 94a4846750SChuck Lever struct nsm_args args = { 95cab2d3c9SChuck Lever .priv = &nsm->sm_priv, 96a4846750SChuck Lever .prog = NLM_PROGRAM, 97a4846750SChuck Lever .vers = 3, 98a4846750SChuck Lever .proc = NLMPROC_NSM_NOTIFY, 9929ed1407SChuck Lever .mon_name = nsm->sm_mon_name, 1000d0f4aabSAndrey Ryabinin .nodename = host->nodename, 101a4846750SChuck Lever }; 102dead28daSChuck Lever struct rpc_message msg = { 103dead28daSChuck Lever .rpc_argp = &args, 104dead28daSChuck Lever .rpc_resp = res, 105dead28daSChuck Lever }; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds memset(res, 0, sizeof(*res)); 1081da177e4SLinus Torvalds 1090d0f4aabSAndrey Ryabinin clnt = nsm_create(host->net, host->nodename); 1100d0f4aabSAndrey Ryabinin if (IS_ERR(clnt)) { 1110d0f4aabSAndrey Ryabinin dprintk("lockd: failed to create NSM upcall transport, " 1120d0f4aabSAndrey Ryabinin "status=%ld, net=%p\n", PTR_ERR(clnt), host->net); 1130d0f4aabSAndrey Ryabinin return PTR_ERR(clnt); 1140d0f4aabSAndrey Ryabinin } 1150d0f4aabSAndrey Ryabinin 116dead28daSChuck Lever msg.rpc_proc = &clnt->cl_procinfo[proc]; 117e9406db2SStanislav Kinsbursky status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN); 118173b3afcSBenjamin Coddington if (status == -ECONNREFUSED) { 119173b3afcSBenjamin Coddington dprintk("lockd: NSM upcall RPC failed, status=%d, forcing rebind\n", 120173b3afcSBenjamin Coddington status); 121173b3afcSBenjamin Coddington rpc_force_rebind(clnt); 122173b3afcSBenjamin Coddington status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN); 123173b3afcSBenjamin Coddington } 1241da177e4SLinus Torvalds if (status < 0) 1255acf4315SChuck Lever dprintk("lockd: NSM upcall RPC failed, status=%d\n", 1261da177e4SLinus Torvalds status); 1271da177e4SLinus Torvalds else 1281da177e4SLinus Torvalds status = 0; 1290d0f4aabSAndrey Ryabinin 1300d0f4aabSAndrey Ryabinin rpc_shutdown_client(clnt); 1311da177e4SLinus Torvalds return status; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341e49323cSChuck Lever /** 1351e49323cSChuck Lever * nsm_monitor - Notify a peer in case we reboot 1361e49323cSChuck Lever * @host: pointer to nlm_host of peer to notify 1371e49323cSChuck Lever * 1381e49323cSChuck Lever * If this peer is not already monitored, this function sends an 1391e49323cSChuck Lever * upcall to the local rpc.statd to record the name/address of 1401e49323cSChuck Lever * the peer to notify in case we reboot. 1411e49323cSChuck Lever * 1421e49323cSChuck Lever * Returns zero if the peer is monitored by the local rpc.statd; 1431e49323cSChuck Lever * otherwise a negative errno value is returned. 1441da177e4SLinus Torvalds */ 1451e49323cSChuck Lever int nsm_monitor(const struct nlm_host *host) 1461da177e4SLinus Torvalds { 1478dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 1481da177e4SLinus Torvalds struct nsm_res res; 1491da177e4SLinus Torvalds int status; 1501da177e4SLinus Torvalds 1519fee4902SChuck Lever dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name); 1528dead0dbSOlaf Kirch 1538dead0dbSOlaf Kirch if (nsm->sm_monitored) 154977faf39SOlaf Kirch return 0; 1551da177e4SLinus Torvalds 15629ed1407SChuck Lever /* 15729ed1407SChuck Lever * Choose whether to record the caller_name or IP address of 15829ed1407SChuck Lever * this peer in the local rpc.statd's database. 15929ed1407SChuck Lever */ 16029ed1407SChuck Lever nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf; 16129ed1407SChuck Lever 1620d0f4aabSAndrey Ryabinin status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, host); 1636c9dc425SChuck Lever if (unlikely(res.status != 0)) 1645d254b11SChuck Lever status = -EIO; 1656c9dc425SChuck Lever if (unlikely(status < 0)) { 1669af94fc4SJeff Layton pr_notice_ratelimited("lockd: cannot monitor %s\n", nsm->sm_name); 1671da177e4SLinus Torvalds return status; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1706c9dc425SChuck Lever nsm->sm_monitored = 1; 1716c9dc425SChuck Lever if (unlikely(nsm_local_state != res.state)) { 1726c9dc425SChuck Lever nsm_local_state = res.state; 1736c9dc425SChuck Lever dprintk("lockd: NSM state changed to %d\n", nsm_local_state); 1746c9dc425SChuck Lever } 1756c9dc425SChuck Lever return 0; 1766c9dc425SChuck Lever } 1776c9dc425SChuck Lever 178356c3eb4SChuck Lever /** 179356c3eb4SChuck Lever * nsm_unmonitor - Unregister peer notification 180356c3eb4SChuck Lever * @host: pointer to nlm_host of peer to stop monitoring 181356c3eb4SChuck Lever * 182356c3eb4SChuck Lever * If this peer is monitored, this function sends an upcall to 183356c3eb4SChuck Lever * tell the local rpc.statd not to send this peer a notification 184356c3eb4SChuck Lever * when we reboot. 1851da177e4SLinus Torvalds */ 186356c3eb4SChuck Lever void nsm_unmonitor(const struct nlm_host *host) 1871da177e4SLinus Torvalds { 1888dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 1891da177e4SLinus Torvalds struct nsm_res res; 190356c3eb4SChuck Lever int status; 1911da177e4SLinus Torvalds 1929502c522SOlaf Kirch if (atomic_read(&nsm->sm_count) == 1 1939502c522SOlaf Kirch && nsm->sm_monitored && !nsm->sm_sticky) { 1949fee4902SChuck Lever dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name); 1959502c522SOlaf Kirch 1960d0f4aabSAndrey Ryabinin status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, host); 1970c7aef45SChuck Lever if (res.status != 0) 1980c7aef45SChuck Lever status = -EIO; 1991da177e4SLinus Torvalds if (status < 0) 2009502c522SOlaf Kirch printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", 2019fee4902SChuck Lever nsm->sm_name); 2029502c522SOlaf Kirch else 2038dead0dbSOlaf Kirch nsm->sm_monitored = 0; 204977faf39SOlaf Kirch } 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2070ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_hostname(const struct list_head *nsm_handles, 2080ad95472SAndrey Ryabinin const char *hostname, const size_t len) 2093420a8c4SChuck Lever { 2103420a8c4SChuck Lever struct nsm_handle *nsm; 2113420a8c4SChuck Lever 2120ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link) 2133420a8c4SChuck Lever if (strlen(nsm->sm_name) == len && 2143420a8c4SChuck Lever memcmp(nsm->sm_name, hostname, len) == 0) 2153420a8c4SChuck Lever return nsm; 2163420a8c4SChuck Lever return NULL; 2173420a8c4SChuck Lever } 2183420a8c4SChuck Lever 2190ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_addr(const struct list_head *nsm_handles, 2200ad95472SAndrey Ryabinin const struct sockaddr *sap) 22177a3ef33SChuck Lever { 22277a3ef33SChuck Lever struct nsm_handle *nsm; 22377a3ef33SChuck Lever 2240ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link) 2254516fc04SJeff Layton if (rpc_cmp_addr(nsm_addr(nsm), sap)) 22677a3ef33SChuck Lever return nsm; 22777a3ef33SChuck Lever return NULL; 22877a3ef33SChuck Lever } 22977a3ef33SChuck Lever 2300ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_priv(const struct list_head *nsm_handles, 2310ad95472SAndrey Ryabinin const struct nsm_private *priv) 2323420a8c4SChuck Lever { 2333420a8c4SChuck Lever struct nsm_handle *nsm; 2343420a8c4SChuck Lever 2350ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link) 2363420a8c4SChuck Lever if (memcmp(nsm->sm_priv.data, priv->data, 2373420a8c4SChuck Lever sizeof(priv->data)) == 0) 2383420a8c4SChuck Lever return nsm; 2393420a8c4SChuck Lever return NULL; 2403420a8c4SChuck Lever } 2413420a8c4SChuck Lever 2427e44d3beSChuck Lever /* 2437e44d3beSChuck Lever * Construct a unique cookie to match this nsm_handle to this monitored 2447e44d3beSChuck Lever * host. It is passed to the local rpc.statd via NSMPROC_MON, and 2457e44d3beSChuck Lever * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these 2467e44d3beSChuck Lever * requests. 2477e44d3beSChuck Lever * 24894da7663SChuck Lever * The NSM protocol requires that these cookies be unique while the 24994da7663SChuck Lever * system is running. We prefer a stronger requirement of making them 25094da7663SChuck Lever * unique across reboots. If user space bugs cause a stale cookie to 25194da7663SChuck Lever * be sent to the kernel, it could cause the wrong host to lose its 25294da7663SChuck Lever * lock state if cookies were not unique across reboots. 25394da7663SChuck Lever * 25494da7663SChuck Lever * The cookies are exposed only to local user space via loopback. They 25594da7663SChuck Lever * do not appear on the physical network. If we want greater security 25694da7663SChuck Lever * for some reason, nsm_init_private() could perform a one-way hash to 25794da7663SChuck Lever * obscure the contents of the cookie. 2587e44d3beSChuck Lever */ 2597e44d3beSChuck Lever static void nsm_init_private(struct nsm_handle *nsm) 2607e44d3beSChuck Lever { 26194da7663SChuck Lever u64 *p = (u64 *)&nsm->sm_priv.data; 262ad5b365cSMans Rullgard s64 ns; 26394da7663SChuck Lever 2645eaaed4fSThomas Gleixner ns = ktime_get_ns(); 265ad5b365cSMans Rullgard put_unaligned(ns, p); 266ad5b365cSMans Rullgard put_unaligned((unsigned long)nsm, p + 1); 2677e44d3beSChuck Lever } 2687e44d3beSChuck Lever 269b39b897cSChuck Lever static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap, 270b39b897cSChuck Lever const size_t salen, 271b39b897cSChuck Lever const char *hostname, 272b39b897cSChuck Lever const size_t hostname_len) 273b39b897cSChuck Lever { 274b39b897cSChuck Lever struct nsm_handle *new; 275b39b897cSChuck Lever 276b39b897cSChuck Lever new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL); 277b39b897cSChuck Lever if (unlikely(new == NULL)) 278b39b897cSChuck Lever return NULL; 279b39b897cSChuck Lever 280b39b897cSChuck Lever atomic_set(&new->sm_count, 1); 281b39b897cSChuck Lever new->sm_name = (char *)(new + 1); 282b39b897cSChuck Lever memcpy(nsm_addr(new), sap, salen); 283b39b897cSChuck Lever new->sm_addrlen = salen; 284b39b897cSChuck Lever nsm_init_private(new); 285c15128c5SChuck Lever 286c15128c5SChuck Lever if (rpc_ntop(nsm_addr(new), new->sm_addrbuf, 287c15128c5SChuck Lever sizeof(new->sm_addrbuf)) == 0) 288c15128c5SChuck Lever (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf), 289c15128c5SChuck Lever "unsupported address family"); 290b39b897cSChuck Lever memcpy(new->sm_name, hostname, hostname_len); 291b39b897cSChuck Lever new->sm_name[hostname_len] = '\0'; 292b39b897cSChuck Lever 293b39b897cSChuck Lever return new; 294b39b897cSChuck Lever } 295b39b897cSChuck Lever 29667c6d107SChuck Lever /** 29792fd91b9SChuck Lever * nsm_get_handle - Find or create a cached nsm_handle 2980ad95472SAndrey Ryabinin * @net: network namespace 29967c6d107SChuck Lever * @sap: pointer to socket address of handle to find 30067c6d107SChuck Lever * @salen: length of socket address 30167c6d107SChuck Lever * @hostname: pointer to C string containing hostname to find 30267c6d107SChuck Lever * @hostname_len: length of C string 30367c6d107SChuck Lever * 30492fd91b9SChuck Lever * Behavior is modulated by the global nsm_use_hostnames variable. 30567c6d107SChuck Lever * 30692fd91b9SChuck Lever * Returns a cached nsm_handle after bumping its ref count, or 30792fd91b9SChuck Lever * returns a fresh nsm_handle if a handle that matches @sap and/or 30892fd91b9SChuck Lever * @hostname cannot be found in the handle cache. Returns NULL if 30992fd91b9SChuck Lever * an error occurs. 31067c6d107SChuck Lever */ 3110ad95472SAndrey Ryabinin struct nsm_handle *nsm_get_handle(const struct net *net, 3120ad95472SAndrey Ryabinin const struct sockaddr *sap, 31392fd91b9SChuck Lever const size_t salen, const char *hostname, 31492fd91b9SChuck Lever const size_t hostname_len) 31567c6d107SChuck Lever { 31677a3ef33SChuck Lever struct nsm_handle *cached, *new = NULL; 3170ad95472SAndrey Ryabinin struct lockd_net *ln = net_generic(net, lockd_net_id); 31867c6d107SChuck Lever 31967c6d107SChuck Lever if (hostname && memchr(hostname, '/', hostname_len) != NULL) { 32067c6d107SChuck Lever if (printk_ratelimit()) { 32167c6d107SChuck Lever printk(KERN_WARNING "Invalid hostname \"%.*s\" " 32267c6d107SChuck Lever "in NFS lock request\n", 32367c6d107SChuck Lever (int)hostname_len, hostname); 32467c6d107SChuck Lever } 32567c6d107SChuck Lever return NULL; 32667c6d107SChuck Lever } 32767c6d107SChuck Lever 32867c6d107SChuck Lever retry: 32967c6d107SChuck Lever spin_lock(&nsm_lock); 33067c6d107SChuck Lever 33177a3ef33SChuck Lever if (nsm_use_hostnames && hostname != NULL) 3320ad95472SAndrey Ryabinin cached = nsm_lookup_hostname(&ln->nsm_handles, 3330ad95472SAndrey Ryabinin hostname, hostname_len); 33477a3ef33SChuck Lever else 3350ad95472SAndrey Ryabinin cached = nsm_lookup_addr(&ln->nsm_handles, sap); 33677a3ef33SChuck Lever 33777a3ef33SChuck Lever if (cached != NULL) { 33877a3ef33SChuck Lever atomic_inc(&cached->sm_count); 33977a3ef33SChuck Lever spin_unlock(&nsm_lock); 34077a3ef33SChuck Lever kfree(new); 34177a3ef33SChuck Lever dprintk("lockd: found nsm_handle for %s (%s), " 34277a3ef33SChuck Lever "cnt %d\n", cached->sm_name, 34377a3ef33SChuck Lever cached->sm_addrbuf, 34477a3ef33SChuck Lever atomic_read(&cached->sm_count)); 34577a3ef33SChuck Lever return cached; 34667c6d107SChuck Lever } 34777a3ef33SChuck Lever 34877a3ef33SChuck Lever if (new != NULL) { 3490ad95472SAndrey Ryabinin list_add(&new->sm_link, &ln->nsm_handles); 35077a3ef33SChuck Lever spin_unlock(&nsm_lock); 3515cf1c4b1SChuck Lever dprintk("lockd: created nsm_handle for %s (%s)\n", 35277a3ef33SChuck Lever new->sm_name, new->sm_addrbuf); 35377a3ef33SChuck Lever return new; 35467c6d107SChuck Lever } 35577a3ef33SChuck Lever 35667c6d107SChuck Lever spin_unlock(&nsm_lock); 35767c6d107SChuck Lever 35877a3ef33SChuck Lever new = nsm_create_handle(sap, salen, hostname, hostname_len); 35977a3ef33SChuck Lever if (unlikely(new == NULL)) 36067c6d107SChuck Lever return NULL; 36167c6d107SChuck Lever goto retry; 36267c6d107SChuck Lever } 36367c6d107SChuck Lever 36467c6d107SChuck Lever /** 3653420a8c4SChuck Lever * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle 3660ad95472SAndrey Ryabinin * @net: network namespace 3673420a8c4SChuck Lever * @info: pointer to NLMPROC_SM_NOTIFY arguments 3683420a8c4SChuck Lever * 3697e469af9SJeff Layton * Returns a matching nsm_handle if found in the nsm cache. The returned 3707e469af9SJeff Layton * nsm_handle's reference count is bumped. Otherwise returns NULL if some 3717e469af9SJeff Layton * error occurred. 3723420a8c4SChuck Lever */ 3730ad95472SAndrey Ryabinin struct nsm_handle *nsm_reboot_lookup(const struct net *net, 3740ad95472SAndrey Ryabinin const struct nlm_reboot *info) 3753420a8c4SChuck Lever { 3763420a8c4SChuck Lever struct nsm_handle *cached; 3770ad95472SAndrey Ryabinin struct lockd_net *ln = net_generic(net, lockd_net_id); 3783420a8c4SChuck Lever 3793420a8c4SChuck Lever spin_lock(&nsm_lock); 3803420a8c4SChuck Lever 3810ad95472SAndrey Ryabinin cached = nsm_lookup_priv(&ln->nsm_handles, &info->priv); 3823420a8c4SChuck Lever if (unlikely(cached == NULL)) { 3833420a8c4SChuck Lever spin_unlock(&nsm_lock); 3843420a8c4SChuck Lever dprintk("lockd: never saw rebooted peer '%.*s' before\n", 3853420a8c4SChuck Lever info->len, info->mon); 3863420a8c4SChuck Lever return cached; 3873420a8c4SChuck Lever } 3883420a8c4SChuck Lever 3893420a8c4SChuck Lever atomic_inc(&cached->sm_count); 3903420a8c4SChuck Lever spin_unlock(&nsm_lock); 3913420a8c4SChuck Lever 3923420a8c4SChuck Lever dprintk("lockd: host %s (%s) rebooted, cnt %d\n", 3933420a8c4SChuck Lever cached->sm_name, cached->sm_addrbuf, 3943420a8c4SChuck Lever atomic_read(&cached->sm_count)); 3953420a8c4SChuck Lever return cached; 3963420a8c4SChuck Lever } 3973420a8c4SChuck Lever 3983420a8c4SChuck Lever /** 39967c6d107SChuck Lever * nsm_release - Release an NSM handle 40067c6d107SChuck Lever * @nsm: pointer to handle to be released 40167c6d107SChuck Lever * 40267c6d107SChuck Lever */ 40367c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm) 40467c6d107SChuck Lever { 40567c6d107SChuck Lever if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) { 40667c6d107SChuck Lever list_del(&nsm->sm_link); 40767c6d107SChuck Lever spin_unlock(&nsm_lock); 4085cf1c4b1SChuck Lever dprintk("lockd: destroyed nsm_handle for %s (%s)\n", 4095cf1c4b1SChuck Lever nsm->sm_name, nsm->sm_addrbuf); 41067c6d107SChuck Lever kfree(nsm); 41167c6d107SChuck Lever } 41267c6d107SChuck Lever } 41367c6d107SChuck Lever 4141da177e4SLinus Torvalds /* 4151da177e4SLinus Torvalds * XDR functions for NSM. 4162ca7754dSChuck Lever * 4172ca7754dSChuck Lever * See http://www.opengroup.org/ for details on the Network 4182ca7754dSChuck Lever * Status Monitor wire protocol. 4191da177e4SLinus Torvalds */ 4201da177e4SLinus Torvalds 42149b17004SChuck Lever static void encode_nsm_string(struct xdr_stream *xdr, const char *string) 422099bd05fSChuck Lever { 42303eb1dcbSChuck Lever const u32 len = strlen(string); 42403eb1dcbSChuck Lever __be32 *p; 425099bd05fSChuck Lever 42649b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + len); 42703eb1dcbSChuck Lever xdr_encode_opaque(p, string, len); 428099bd05fSChuck Lever } 429099bd05fSChuck Lever 43049695174SChuck Lever /* 43149695174SChuck Lever * "mon_name" specifies the host to be monitored. 43249695174SChuck Lever */ 43349b17004SChuck Lever static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp) 43449695174SChuck Lever { 43549b17004SChuck Lever encode_nsm_string(xdr, argp->mon_name); 43649695174SChuck Lever } 43749695174SChuck Lever 438850c95fdSChuck Lever /* 439850c95fdSChuck Lever * The "my_id" argument specifies the hostname and RPC procedure 440850c95fdSChuck Lever * to be called when the status manager receives notification 44136e8e668SChuck Lever * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name" 442850c95fdSChuck Lever * has changed. 443850c95fdSChuck Lever */ 44449b17004SChuck Lever static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp) 445850c95fdSChuck Lever { 44603eb1dcbSChuck Lever __be32 *p; 447850c95fdSChuck Lever 448303a7ce9SStanislav Kinsbursky encode_nsm_string(xdr, argp->nodename); 44949b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + 4 + 4); 45049b17004SChuck Lever *p++ = cpu_to_be32(argp->prog); 45149b17004SChuck Lever *p++ = cpu_to_be32(argp->vers); 45249b17004SChuck Lever *p = cpu_to_be32(argp->proc); 453850c95fdSChuck Lever } 454850c95fdSChuck Lever 455ea72a7f1SChuck Lever /* 456ea72a7f1SChuck Lever * The "mon_id" argument specifies the non-private arguments 45736e8e668SChuck Lever * of an NSMPROC_MON or NSMPROC_UNMON call. 458ea72a7f1SChuck Lever */ 45949b17004SChuck Lever static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp) 460ea72a7f1SChuck Lever { 46149b17004SChuck Lever encode_mon_name(xdr, argp); 46249b17004SChuck Lever encode_my_id(xdr, argp); 463ea72a7f1SChuck Lever } 464ea72a7f1SChuck Lever 4650490a54aSChuck Lever /* 4660490a54aSChuck Lever * The "priv" argument may contain private information required 46736e8e668SChuck Lever * by the NSMPROC_MON call. This information will be supplied in the 46836e8e668SChuck Lever * NLMPROC_SM_NOTIFY call. 4690490a54aSChuck Lever */ 47049b17004SChuck Lever static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp) 4710490a54aSChuck Lever { 47203eb1dcbSChuck Lever __be32 *p; 47303eb1dcbSChuck Lever 47403eb1dcbSChuck Lever p = xdr_reserve_space(xdr, SM_PRIV_SIZE); 475cab2d3c9SChuck Lever xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE); 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds 4789f06c719SChuck Lever static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr, 479*bf96391eSChristoph Hellwig const void *argp) 4801da177e4SLinus Torvalds { 4819f06c719SChuck Lever encode_mon_id(xdr, argp); 4829f06c719SChuck Lever encode_priv(xdr, argp); 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 4859f06c719SChuck Lever static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr, 486*bf96391eSChristoph Hellwig const void *argp) 4871da177e4SLinus Torvalds { 4889f06c719SChuck Lever encode_mon_id(xdr, argp); 48903eb1dcbSChuck Lever } 49003eb1dcbSChuck Lever 491bf269551SChuck Lever static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp, 492bf269551SChuck Lever struct xdr_stream *xdr, 49303eb1dcbSChuck Lever struct nsm_res *resp) 49403eb1dcbSChuck Lever { 495bf269551SChuck Lever __be32 *p; 49603eb1dcbSChuck Lever 497bf269551SChuck Lever p = xdr_inline_decode(xdr, 4 + 4); 49803eb1dcbSChuck Lever if (unlikely(p == NULL)) 49903eb1dcbSChuck Lever return -EIO; 50049b17004SChuck Lever resp->status = be32_to_cpup(p++); 50149b17004SChuck Lever resp->state = be32_to_cpup(p); 50203eb1dcbSChuck Lever 503bf269551SChuck Lever dprintk("lockd: %s status %d state %d\n", 504bf269551SChuck Lever __func__, resp->status, resp->state); 5051da177e4SLinus Torvalds return 0; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 508bf269551SChuck Lever static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp, 509bf269551SChuck Lever struct xdr_stream *xdr, 51003eb1dcbSChuck Lever struct nsm_res *resp) 5111da177e4SLinus Torvalds { 512bf269551SChuck Lever __be32 *p; 51303eb1dcbSChuck Lever 514bf269551SChuck Lever p = xdr_inline_decode(xdr, 4); 51503eb1dcbSChuck Lever if (unlikely(p == NULL)) 51603eb1dcbSChuck Lever return -EIO; 51749b17004SChuck Lever resp->state = be32_to_cpup(p); 51803eb1dcbSChuck Lever 519bf269551SChuck Lever dprintk("lockd: %s state %d\n", __func__, resp->state); 5201da177e4SLinus Torvalds return 0; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5242ca7754dSChuck Lever #define SM_my_id_sz (SM_my_name_sz+3) 5252ca7754dSChuck Lever #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5262ca7754dSChuck Lever #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz) 5270490a54aSChuck Lever #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE)) 5280490a54aSChuck Lever #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz) 5291da177e4SLinus Torvalds #define SM_monres_sz 2 5301da177e4SLinus Torvalds #define SM_unmonres_sz 1 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds static struct rpc_procinfo nsm_procedures[] = { 53336e8e668SChuck Lever [NSMPROC_MON] = { 53436e8e668SChuck Lever .p_proc = NSMPROC_MON, 535*bf96391eSChristoph Hellwig .p_encode = nsm_xdr_enc_mon, 536bf269551SChuck Lever .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat_res, 5372bea90d4SChuck Lever .p_arglen = SM_mon_sz, 5382bea90d4SChuck Lever .p_replen = SM_monres_sz, 53936e8e668SChuck Lever .p_statidx = NSMPROC_MON, 540cc0175c1SChuck Lever .p_name = "MONITOR", 5411da177e4SLinus Torvalds }, 54236e8e668SChuck Lever [NSMPROC_UNMON] = { 54336e8e668SChuck Lever .p_proc = NSMPROC_UNMON, 544*bf96391eSChristoph Hellwig .p_encode = nsm_xdr_enc_unmon, 545bf269551SChuck Lever .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat, 5462bea90d4SChuck Lever .p_arglen = SM_mon_id_sz, 5472bea90d4SChuck Lever .p_replen = SM_unmonres_sz, 54836e8e668SChuck Lever .p_statidx = NSMPROC_UNMON, 549cc0175c1SChuck Lever .p_name = "UNMONITOR", 5501da177e4SLinus Torvalds }, 5511da177e4SLinus Torvalds }; 5521da177e4SLinus Torvalds 553a613fa16STrond Myklebust static const struct rpc_version nsm_version1 = { 5541da177e4SLinus Torvalds .number = 1, 555e8c96f8cSTobias Klauser .nrprocs = ARRAY_SIZE(nsm_procedures), 5561da177e4SLinus Torvalds .procs = nsm_procedures 5571da177e4SLinus Torvalds }; 5581da177e4SLinus Torvalds 559a613fa16STrond Myklebust static const struct rpc_version *nsm_version[] = { 5601da177e4SLinus Torvalds [1] = &nsm_version1, 5611da177e4SLinus Torvalds }; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds static struct rpc_stat nsm_stats; 5641da177e4SLinus Torvalds 565a613fa16STrond Myklebust static const struct rpc_program nsm_program = { 5661da177e4SLinus Torvalds .name = "statd", 56736e8e668SChuck Lever .number = NSM_PROGRAM, 568e8c96f8cSTobias Klauser .nrvers = ARRAY_SIZE(nsm_version), 5691da177e4SLinus Torvalds .version = nsm_version, 5701da177e4SLinus Torvalds .stats = &nsm_stats 5711da177e4SLinus Torvalds }; 572