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> 150896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 161da177e4SLinus Torvalds #include <linux/sunrpc/svc.h> 171da177e4SLinus Torvalds #include <linux/lockd/lockd.h> 181da177e4SLinus Torvalds 19ad5b365cSMans Rullgard #include <asm/unaligned.h> 20ad5b365cSMans Rullgard 21e9406db2SStanislav Kinsbursky #include "netns.h" 22e9406db2SStanislav Kinsbursky 231da177e4SLinus Torvalds #define NLMDBG_FACILITY NLMDBG_MONITOR 2436e8e668SChuck Lever #define NSM_PROGRAM 100024 2536e8e668SChuck Lever #define NSM_VERSION 1 2636e8e668SChuck Lever 2736e8e668SChuck Lever enum { 2836e8e668SChuck Lever NSMPROC_NULL, 2936e8e668SChuck Lever NSMPROC_STAT, 3036e8e668SChuck Lever NSMPROC_MON, 3136e8e668SChuck Lever NSMPROC_UNMON, 3236e8e668SChuck Lever NSMPROC_UNMON_ALL, 3336e8e668SChuck Lever NSMPROC_SIMU_CRASH, 3436e8e668SChuck Lever NSMPROC_NOTIFY, 3536e8e668SChuck Lever }; 361da177e4SLinus Torvalds 379c1bfd03SChuck Lever struct nsm_args { 38cab2d3c9SChuck Lever struct nsm_private *priv; 399c1bfd03SChuck Lever u32 prog; /* RPC callback info */ 409c1bfd03SChuck Lever u32 vers; 419c1bfd03SChuck Lever u32 proc; 429c1bfd03SChuck Lever 439c1bfd03SChuck Lever char *mon_name; 44303a7ce9SStanislav Kinsbursky char *nodename; 459c1bfd03SChuck Lever }; 469c1bfd03SChuck Lever 479c1bfd03SChuck Lever struct nsm_res { 489c1bfd03SChuck Lever u32 status; 499c1bfd03SChuck Lever u32 state; 509c1bfd03SChuck Lever }; 519c1bfd03SChuck Lever 52a613fa16STrond Myklebust static const struct rpc_program nsm_program; 5367c6d107SChuck Lever static LIST_HEAD(nsm_handles); 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 670e1cb5c0SStanislav Kinsbursky static struct rpc_clnt *nsm_create(struct net *net) 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", 7949b5699bSChuck Lever .program = &nsm_program, 8049b5699bSChuck Lever .version = NSM_VERSION, 8149b5699bSChuck Lever .authflavor = RPC_AUTH_NULL, 820e5c2632SChuck Lever .flags = RPC_CLNT_CREATE_NOPING, 8349b5699bSChuck Lever }; 8449b5699bSChuck Lever 8549b5699bSChuck Lever return rpc_create(&args); 8649b5699bSChuck Lever } 8749b5699bSChuck Lever 88*cb7323ffSStanislav Kinsbursky static struct rpc_clnt *nsm_client_get(struct net *net) 89e9406db2SStanislav Kinsbursky { 90e9406db2SStanislav Kinsbursky static DEFINE_MUTEX(nsm_create_mutex); 91e9406db2SStanislav Kinsbursky struct rpc_clnt *clnt; 92e9406db2SStanislav Kinsbursky struct lockd_net *ln = net_generic(net, lockd_net_id); 93e9406db2SStanislav Kinsbursky 94e9406db2SStanislav Kinsbursky spin_lock(&ln->nsm_clnt_lock); 95e9406db2SStanislav Kinsbursky if (ln->nsm_users) { 96e9406db2SStanislav Kinsbursky ln->nsm_users++; 97e9406db2SStanislav Kinsbursky clnt = ln->nsm_clnt; 98e9406db2SStanislav Kinsbursky spin_unlock(&ln->nsm_clnt_lock); 99e9406db2SStanislav Kinsbursky goto out; 100e9406db2SStanislav Kinsbursky } 101e9406db2SStanislav Kinsbursky spin_unlock(&ln->nsm_clnt_lock); 102e9406db2SStanislav Kinsbursky 103e9406db2SStanislav Kinsbursky mutex_lock(&nsm_create_mutex); 104e9406db2SStanislav Kinsbursky clnt = nsm_create(net); 105e9406db2SStanislav Kinsbursky if (!IS_ERR(clnt)) { 106e9406db2SStanislav Kinsbursky ln->nsm_clnt = clnt; 107e9406db2SStanislav Kinsbursky smp_wmb(); 108e9406db2SStanislav Kinsbursky ln->nsm_users = 1; 109e9406db2SStanislav Kinsbursky } 110e9406db2SStanislav Kinsbursky mutex_unlock(&nsm_create_mutex); 111e9406db2SStanislav Kinsbursky out: 112e9406db2SStanislav Kinsbursky return clnt; 113e9406db2SStanislav Kinsbursky } 114e9406db2SStanislav Kinsbursky 115*cb7323ffSStanislav Kinsbursky static void nsm_client_put(struct net *net) 116e9406db2SStanislav Kinsbursky { 117e9406db2SStanislav Kinsbursky struct lockd_net *ln = net_generic(net, lockd_net_id); 118e9406db2SStanislav Kinsbursky struct rpc_clnt *clnt = ln->nsm_clnt; 119e9406db2SStanislav Kinsbursky int shutdown = 0; 120e9406db2SStanislav Kinsbursky 121e9406db2SStanislav Kinsbursky spin_lock(&ln->nsm_clnt_lock); 122e9406db2SStanislav Kinsbursky if (ln->nsm_users) { 123e9406db2SStanislav Kinsbursky if (--ln->nsm_users) 124e9406db2SStanislav Kinsbursky ln->nsm_clnt = NULL; 125e9406db2SStanislav Kinsbursky shutdown = !ln->nsm_users; 126e9406db2SStanislav Kinsbursky } 127e9406db2SStanislav Kinsbursky spin_unlock(&ln->nsm_clnt_lock); 128e9406db2SStanislav Kinsbursky 129e9406db2SStanislav Kinsbursky if (shutdown) 130e9406db2SStanislav Kinsbursky rpc_shutdown_client(clnt); 131e9406db2SStanislav Kinsbursky } 132e9406db2SStanislav Kinsbursky 1330e1cb5c0SStanislav Kinsbursky static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res, 134*cb7323ffSStanislav Kinsbursky struct rpc_clnt *clnt) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds int status; 137a4846750SChuck Lever struct nsm_args args = { 138cab2d3c9SChuck Lever .priv = &nsm->sm_priv, 139a4846750SChuck Lever .prog = NLM_PROGRAM, 140a4846750SChuck Lever .vers = 3, 141a4846750SChuck Lever .proc = NLMPROC_NSM_NOTIFY, 14229ed1407SChuck Lever .mon_name = nsm->sm_mon_name, 143*cb7323ffSStanislav Kinsbursky .nodename = clnt->cl_nodename, 144a4846750SChuck Lever }; 145dead28daSChuck Lever struct rpc_message msg = { 146dead28daSChuck Lever .rpc_argp = &args, 147dead28daSChuck Lever .rpc_resp = res, 148dead28daSChuck Lever }; 1491da177e4SLinus Torvalds 150*cb7323ffSStanislav Kinsbursky BUG_ON(clnt == NULL); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds memset(res, 0, sizeof(*res)); 1531da177e4SLinus Torvalds 154dead28daSChuck Lever msg.rpc_proc = &clnt->cl_procinfo[proc]; 155e9406db2SStanislav Kinsbursky status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN); 1561da177e4SLinus Torvalds if (status < 0) 1575acf4315SChuck Lever dprintk("lockd: NSM upcall RPC failed, status=%d\n", 1581da177e4SLinus Torvalds status); 1591da177e4SLinus Torvalds else 1601da177e4SLinus Torvalds status = 0; 1611da177e4SLinus Torvalds return status; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 1641e49323cSChuck Lever /** 1651e49323cSChuck Lever * nsm_monitor - Notify a peer in case we reboot 1661e49323cSChuck Lever * @host: pointer to nlm_host of peer to notify 1671e49323cSChuck Lever * 1681e49323cSChuck Lever * If this peer is not already monitored, this function sends an 1691e49323cSChuck Lever * upcall to the local rpc.statd to record the name/address of 1701e49323cSChuck Lever * the peer to notify in case we reboot. 1711e49323cSChuck Lever * 1721e49323cSChuck Lever * Returns zero if the peer is monitored by the local rpc.statd; 1731e49323cSChuck Lever * otherwise a negative errno value is returned. 1741da177e4SLinus Torvalds */ 1751e49323cSChuck Lever int nsm_monitor(const struct nlm_host *host) 1761da177e4SLinus Torvalds { 1778dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 1781da177e4SLinus Torvalds struct nsm_res res; 1791da177e4SLinus Torvalds int status; 180*cb7323ffSStanislav Kinsbursky struct rpc_clnt *clnt; 1811da177e4SLinus Torvalds 1829fee4902SChuck Lever dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name); 1838dead0dbSOlaf Kirch 1848dead0dbSOlaf Kirch if (nsm->sm_monitored) 185977faf39SOlaf Kirch return 0; 1861da177e4SLinus Torvalds 18729ed1407SChuck Lever /* 18829ed1407SChuck Lever * Choose whether to record the caller_name or IP address of 18929ed1407SChuck Lever * this peer in the local rpc.statd's database. 19029ed1407SChuck Lever */ 19129ed1407SChuck Lever nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf; 19229ed1407SChuck Lever 193*cb7323ffSStanislav Kinsbursky clnt = nsm_client_get(host->net); 194*cb7323ffSStanislav Kinsbursky if (IS_ERR(clnt)) { 195*cb7323ffSStanislav Kinsbursky status = PTR_ERR(clnt); 196*cb7323ffSStanislav Kinsbursky dprintk("lockd: failed to create NSM upcall transport, " 197*cb7323ffSStanislav Kinsbursky "status=%d, net=%p\n", status, host->net); 198*cb7323ffSStanislav Kinsbursky return status; 199*cb7323ffSStanislav Kinsbursky } 200*cb7323ffSStanislav Kinsbursky 201*cb7323ffSStanislav Kinsbursky status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, clnt); 2026c9dc425SChuck Lever if (unlikely(res.status != 0)) 2035d254b11SChuck Lever status = -EIO; 2046c9dc425SChuck Lever if (unlikely(status < 0)) { 2059fee4902SChuck Lever printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name); 2061da177e4SLinus Torvalds return status; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2096c9dc425SChuck Lever nsm->sm_monitored = 1; 2106c9dc425SChuck Lever if (unlikely(nsm_local_state != res.state)) { 2116c9dc425SChuck Lever nsm_local_state = res.state; 2126c9dc425SChuck Lever dprintk("lockd: NSM state changed to %d\n", nsm_local_state); 2136c9dc425SChuck Lever } 2146c9dc425SChuck Lever return 0; 2156c9dc425SChuck Lever } 2166c9dc425SChuck Lever 217356c3eb4SChuck Lever /** 218356c3eb4SChuck Lever * nsm_unmonitor - Unregister peer notification 219356c3eb4SChuck Lever * @host: pointer to nlm_host of peer to stop monitoring 220356c3eb4SChuck Lever * 221356c3eb4SChuck Lever * If this peer is monitored, this function sends an upcall to 222356c3eb4SChuck Lever * tell the local rpc.statd not to send this peer a notification 223356c3eb4SChuck Lever * when we reboot. 2241da177e4SLinus Torvalds */ 225356c3eb4SChuck Lever void nsm_unmonitor(const struct nlm_host *host) 2261da177e4SLinus Torvalds { 2278dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 2281da177e4SLinus Torvalds struct nsm_res res; 229356c3eb4SChuck Lever int status; 2301da177e4SLinus Torvalds 2319502c522SOlaf Kirch if (atomic_read(&nsm->sm_count) == 1 2329502c522SOlaf Kirch && nsm->sm_monitored && !nsm->sm_sticky) { 233*cb7323ffSStanislav Kinsbursky struct lockd_net *ln = net_generic(host->net, lockd_net_id); 234*cb7323ffSStanislav Kinsbursky 2359fee4902SChuck Lever dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name); 2369502c522SOlaf Kirch 237*cb7323ffSStanislav Kinsbursky status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, ln->nsm_clnt); 2380c7aef45SChuck Lever if (res.status != 0) 2390c7aef45SChuck Lever status = -EIO; 2401da177e4SLinus Torvalds if (status < 0) 2419502c522SOlaf Kirch printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", 2429fee4902SChuck Lever nsm->sm_name); 2439502c522SOlaf Kirch else 2448dead0dbSOlaf Kirch nsm->sm_monitored = 0; 245*cb7323ffSStanislav Kinsbursky 246*cb7323ffSStanislav Kinsbursky nsm_client_put(host->net); 247977faf39SOlaf Kirch } 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2503420a8c4SChuck Lever static struct nsm_handle *nsm_lookup_hostname(const char *hostname, 2513420a8c4SChuck Lever const size_t len) 2523420a8c4SChuck Lever { 2533420a8c4SChuck Lever struct nsm_handle *nsm; 2543420a8c4SChuck Lever 2553420a8c4SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 2563420a8c4SChuck Lever if (strlen(nsm->sm_name) == len && 2573420a8c4SChuck Lever memcmp(nsm->sm_name, hostname, len) == 0) 2583420a8c4SChuck Lever return nsm; 2593420a8c4SChuck Lever return NULL; 2603420a8c4SChuck Lever } 2613420a8c4SChuck Lever 26277a3ef33SChuck Lever static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap) 26377a3ef33SChuck Lever { 26477a3ef33SChuck Lever struct nsm_handle *nsm; 26577a3ef33SChuck Lever 26677a3ef33SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 2674516fc04SJeff Layton if (rpc_cmp_addr(nsm_addr(nsm), sap)) 26877a3ef33SChuck Lever return nsm; 26977a3ef33SChuck Lever return NULL; 27077a3ef33SChuck Lever } 27177a3ef33SChuck Lever 2723420a8c4SChuck Lever static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv) 2733420a8c4SChuck Lever { 2743420a8c4SChuck Lever struct nsm_handle *nsm; 2753420a8c4SChuck Lever 2763420a8c4SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 2773420a8c4SChuck Lever if (memcmp(nsm->sm_priv.data, priv->data, 2783420a8c4SChuck Lever sizeof(priv->data)) == 0) 2793420a8c4SChuck Lever return nsm; 2803420a8c4SChuck Lever return NULL; 2813420a8c4SChuck Lever } 2823420a8c4SChuck Lever 2837e44d3beSChuck Lever /* 2847e44d3beSChuck Lever * Construct a unique cookie to match this nsm_handle to this monitored 2857e44d3beSChuck Lever * host. It is passed to the local rpc.statd via NSMPROC_MON, and 2867e44d3beSChuck Lever * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these 2877e44d3beSChuck Lever * requests. 2887e44d3beSChuck Lever * 28994da7663SChuck Lever * The NSM protocol requires that these cookies be unique while the 29094da7663SChuck Lever * system is running. We prefer a stronger requirement of making them 29194da7663SChuck Lever * unique across reboots. If user space bugs cause a stale cookie to 29294da7663SChuck Lever * be sent to the kernel, it could cause the wrong host to lose its 29394da7663SChuck Lever * lock state if cookies were not unique across reboots. 29494da7663SChuck Lever * 29594da7663SChuck Lever * The cookies are exposed only to local user space via loopback. They 29694da7663SChuck Lever * do not appear on the physical network. If we want greater security 29794da7663SChuck Lever * for some reason, nsm_init_private() could perform a one-way hash to 29894da7663SChuck Lever * obscure the contents of the cookie. 2997e44d3beSChuck Lever */ 3007e44d3beSChuck Lever static void nsm_init_private(struct nsm_handle *nsm) 3017e44d3beSChuck Lever { 30294da7663SChuck Lever u64 *p = (u64 *)&nsm->sm_priv.data; 30394da7663SChuck Lever struct timespec ts; 304ad5b365cSMans Rullgard s64 ns; 30594da7663SChuck Lever 30694da7663SChuck Lever ktime_get_ts(&ts); 307ad5b365cSMans Rullgard ns = timespec_to_ns(&ts); 308ad5b365cSMans Rullgard put_unaligned(ns, p); 309ad5b365cSMans Rullgard put_unaligned((unsigned long)nsm, p + 1); 3107e44d3beSChuck Lever } 3117e44d3beSChuck Lever 312b39b897cSChuck Lever static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap, 313b39b897cSChuck Lever const size_t salen, 314b39b897cSChuck Lever const char *hostname, 315b39b897cSChuck Lever const size_t hostname_len) 316b39b897cSChuck Lever { 317b39b897cSChuck Lever struct nsm_handle *new; 318b39b897cSChuck Lever 319b39b897cSChuck Lever new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL); 320b39b897cSChuck Lever if (unlikely(new == NULL)) 321b39b897cSChuck Lever return NULL; 322b39b897cSChuck Lever 323b39b897cSChuck Lever atomic_set(&new->sm_count, 1); 324b39b897cSChuck Lever new->sm_name = (char *)(new + 1); 325b39b897cSChuck Lever memcpy(nsm_addr(new), sap, salen); 326b39b897cSChuck Lever new->sm_addrlen = salen; 327b39b897cSChuck Lever nsm_init_private(new); 328c15128c5SChuck Lever 329c15128c5SChuck Lever if (rpc_ntop(nsm_addr(new), new->sm_addrbuf, 330c15128c5SChuck Lever sizeof(new->sm_addrbuf)) == 0) 331c15128c5SChuck Lever (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf), 332c15128c5SChuck Lever "unsupported address family"); 333b39b897cSChuck Lever memcpy(new->sm_name, hostname, hostname_len); 334b39b897cSChuck Lever new->sm_name[hostname_len] = '\0'; 335b39b897cSChuck Lever 336b39b897cSChuck Lever return new; 337b39b897cSChuck Lever } 338b39b897cSChuck Lever 33967c6d107SChuck Lever /** 34092fd91b9SChuck Lever * nsm_get_handle - Find or create a cached nsm_handle 34167c6d107SChuck Lever * @sap: pointer to socket address of handle to find 34267c6d107SChuck Lever * @salen: length of socket address 34367c6d107SChuck Lever * @hostname: pointer to C string containing hostname to find 34467c6d107SChuck Lever * @hostname_len: length of C string 34567c6d107SChuck Lever * 34692fd91b9SChuck Lever * Behavior is modulated by the global nsm_use_hostnames variable. 34767c6d107SChuck Lever * 34892fd91b9SChuck Lever * Returns a cached nsm_handle after bumping its ref count, or 34992fd91b9SChuck Lever * returns a fresh nsm_handle if a handle that matches @sap and/or 35092fd91b9SChuck Lever * @hostname cannot be found in the handle cache. Returns NULL if 35192fd91b9SChuck Lever * an error occurs. 35267c6d107SChuck Lever */ 35392fd91b9SChuck Lever struct nsm_handle *nsm_get_handle(const struct sockaddr *sap, 35492fd91b9SChuck Lever const size_t salen, const char *hostname, 35592fd91b9SChuck Lever const size_t hostname_len) 35667c6d107SChuck Lever { 35777a3ef33SChuck Lever struct nsm_handle *cached, *new = NULL; 35867c6d107SChuck Lever 35967c6d107SChuck Lever if (hostname && memchr(hostname, '/', hostname_len) != NULL) { 36067c6d107SChuck Lever if (printk_ratelimit()) { 36167c6d107SChuck Lever printk(KERN_WARNING "Invalid hostname \"%.*s\" " 36267c6d107SChuck Lever "in NFS lock request\n", 36367c6d107SChuck Lever (int)hostname_len, hostname); 36467c6d107SChuck Lever } 36567c6d107SChuck Lever return NULL; 36667c6d107SChuck Lever } 36767c6d107SChuck Lever 36867c6d107SChuck Lever retry: 36967c6d107SChuck Lever spin_lock(&nsm_lock); 37067c6d107SChuck Lever 37177a3ef33SChuck Lever if (nsm_use_hostnames && hostname != NULL) 37277a3ef33SChuck Lever cached = nsm_lookup_hostname(hostname, hostname_len); 37377a3ef33SChuck Lever else 37477a3ef33SChuck Lever cached = nsm_lookup_addr(sap); 37577a3ef33SChuck Lever 37677a3ef33SChuck Lever if (cached != NULL) { 37777a3ef33SChuck Lever atomic_inc(&cached->sm_count); 37877a3ef33SChuck Lever spin_unlock(&nsm_lock); 37977a3ef33SChuck Lever kfree(new); 38077a3ef33SChuck Lever dprintk("lockd: found nsm_handle for %s (%s), " 38177a3ef33SChuck Lever "cnt %d\n", cached->sm_name, 38277a3ef33SChuck Lever cached->sm_addrbuf, 38377a3ef33SChuck Lever atomic_read(&cached->sm_count)); 38477a3ef33SChuck Lever return cached; 38567c6d107SChuck Lever } 38677a3ef33SChuck Lever 38777a3ef33SChuck Lever if (new != NULL) { 38877a3ef33SChuck Lever list_add(&new->sm_link, &nsm_handles); 38977a3ef33SChuck Lever spin_unlock(&nsm_lock); 3905cf1c4b1SChuck Lever dprintk("lockd: created nsm_handle for %s (%s)\n", 39177a3ef33SChuck Lever new->sm_name, new->sm_addrbuf); 39277a3ef33SChuck Lever return new; 39367c6d107SChuck Lever } 39477a3ef33SChuck Lever 39567c6d107SChuck Lever spin_unlock(&nsm_lock); 39667c6d107SChuck Lever 39777a3ef33SChuck Lever new = nsm_create_handle(sap, salen, hostname, hostname_len); 39877a3ef33SChuck Lever if (unlikely(new == NULL)) 39967c6d107SChuck Lever return NULL; 40067c6d107SChuck Lever goto retry; 40167c6d107SChuck Lever } 40267c6d107SChuck Lever 40367c6d107SChuck Lever /** 4043420a8c4SChuck Lever * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle 4053420a8c4SChuck Lever * @info: pointer to NLMPROC_SM_NOTIFY arguments 4063420a8c4SChuck Lever * 4077e469af9SJeff Layton * Returns a matching nsm_handle if found in the nsm cache. The returned 4087e469af9SJeff Layton * nsm_handle's reference count is bumped. Otherwise returns NULL if some 4097e469af9SJeff Layton * error occurred. 4103420a8c4SChuck Lever */ 4113420a8c4SChuck Lever struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info) 4123420a8c4SChuck Lever { 4133420a8c4SChuck Lever struct nsm_handle *cached; 4143420a8c4SChuck Lever 4153420a8c4SChuck Lever spin_lock(&nsm_lock); 4163420a8c4SChuck Lever 4173420a8c4SChuck Lever cached = nsm_lookup_priv(&info->priv); 4183420a8c4SChuck Lever if (unlikely(cached == NULL)) { 4193420a8c4SChuck Lever spin_unlock(&nsm_lock); 4203420a8c4SChuck Lever dprintk("lockd: never saw rebooted peer '%.*s' before\n", 4213420a8c4SChuck Lever info->len, info->mon); 4223420a8c4SChuck Lever return cached; 4233420a8c4SChuck Lever } 4243420a8c4SChuck Lever 4253420a8c4SChuck Lever atomic_inc(&cached->sm_count); 4263420a8c4SChuck Lever spin_unlock(&nsm_lock); 4273420a8c4SChuck Lever 4283420a8c4SChuck Lever dprintk("lockd: host %s (%s) rebooted, cnt %d\n", 4293420a8c4SChuck Lever cached->sm_name, cached->sm_addrbuf, 4303420a8c4SChuck Lever atomic_read(&cached->sm_count)); 4313420a8c4SChuck Lever return cached; 4323420a8c4SChuck Lever } 4333420a8c4SChuck Lever 4343420a8c4SChuck Lever /** 43567c6d107SChuck Lever * nsm_release - Release an NSM handle 43667c6d107SChuck Lever * @nsm: pointer to handle to be released 43767c6d107SChuck Lever * 43867c6d107SChuck Lever */ 43967c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm) 44067c6d107SChuck Lever { 44167c6d107SChuck Lever if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) { 44267c6d107SChuck Lever list_del(&nsm->sm_link); 44367c6d107SChuck Lever spin_unlock(&nsm_lock); 4445cf1c4b1SChuck Lever dprintk("lockd: destroyed nsm_handle for %s (%s)\n", 4455cf1c4b1SChuck Lever nsm->sm_name, nsm->sm_addrbuf); 44667c6d107SChuck Lever kfree(nsm); 44767c6d107SChuck Lever } 44867c6d107SChuck Lever } 44967c6d107SChuck Lever 4501da177e4SLinus Torvalds /* 4511da177e4SLinus Torvalds * XDR functions for NSM. 4522ca7754dSChuck Lever * 4532ca7754dSChuck Lever * See http://www.opengroup.org/ for details on the Network 4542ca7754dSChuck Lever * Status Monitor wire protocol. 4551da177e4SLinus Torvalds */ 4561da177e4SLinus Torvalds 45749b17004SChuck Lever static void encode_nsm_string(struct xdr_stream *xdr, const char *string) 458099bd05fSChuck Lever { 45903eb1dcbSChuck Lever const u32 len = strlen(string); 46003eb1dcbSChuck Lever __be32 *p; 461099bd05fSChuck Lever 46249b17004SChuck Lever BUG_ON(len > SM_MAXSTRLEN); 46349b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + len); 46403eb1dcbSChuck Lever xdr_encode_opaque(p, string, len); 465099bd05fSChuck Lever } 466099bd05fSChuck Lever 46749695174SChuck Lever /* 46849695174SChuck Lever * "mon_name" specifies the host to be monitored. 46949695174SChuck Lever */ 47049b17004SChuck Lever static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp) 47149695174SChuck Lever { 47249b17004SChuck Lever encode_nsm_string(xdr, argp->mon_name); 47349695174SChuck Lever } 47449695174SChuck Lever 475850c95fdSChuck Lever /* 476850c95fdSChuck Lever * The "my_id" argument specifies the hostname and RPC procedure 477850c95fdSChuck Lever * to be called when the status manager receives notification 47836e8e668SChuck Lever * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name" 479850c95fdSChuck Lever * has changed. 480850c95fdSChuck Lever */ 48149b17004SChuck Lever static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp) 482850c95fdSChuck Lever { 48303eb1dcbSChuck Lever __be32 *p; 484850c95fdSChuck Lever 485303a7ce9SStanislav Kinsbursky encode_nsm_string(xdr, argp->nodename); 48649b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + 4 + 4); 48749b17004SChuck Lever *p++ = cpu_to_be32(argp->prog); 48849b17004SChuck Lever *p++ = cpu_to_be32(argp->vers); 48949b17004SChuck Lever *p = cpu_to_be32(argp->proc); 490850c95fdSChuck Lever } 491850c95fdSChuck Lever 492ea72a7f1SChuck Lever /* 493ea72a7f1SChuck Lever * The "mon_id" argument specifies the non-private arguments 49436e8e668SChuck Lever * of an NSMPROC_MON or NSMPROC_UNMON call. 495ea72a7f1SChuck Lever */ 49649b17004SChuck Lever static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp) 497ea72a7f1SChuck Lever { 49849b17004SChuck Lever encode_mon_name(xdr, argp); 49949b17004SChuck Lever encode_my_id(xdr, argp); 500ea72a7f1SChuck Lever } 501ea72a7f1SChuck Lever 5020490a54aSChuck Lever /* 5030490a54aSChuck Lever * The "priv" argument may contain private information required 50436e8e668SChuck Lever * by the NSMPROC_MON call. This information will be supplied in the 50536e8e668SChuck Lever * NLMPROC_SM_NOTIFY call. 5060490a54aSChuck Lever */ 50749b17004SChuck Lever static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp) 5080490a54aSChuck Lever { 50903eb1dcbSChuck Lever __be32 *p; 51003eb1dcbSChuck Lever 51103eb1dcbSChuck Lever p = xdr_reserve_space(xdr, SM_PRIV_SIZE); 512cab2d3c9SChuck Lever xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE); 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5159f06c719SChuck Lever static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr, 51603eb1dcbSChuck Lever const struct nsm_args *argp) 5171da177e4SLinus Torvalds { 5189f06c719SChuck Lever encode_mon_id(xdr, argp); 5199f06c719SChuck Lever encode_priv(xdr, argp); 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5229f06c719SChuck Lever static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr, 52303eb1dcbSChuck Lever const struct nsm_args *argp) 5241da177e4SLinus Torvalds { 5259f06c719SChuck Lever encode_mon_id(xdr, argp); 52603eb1dcbSChuck Lever } 52703eb1dcbSChuck Lever 528bf269551SChuck Lever static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp, 529bf269551SChuck Lever struct xdr_stream *xdr, 53003eb1dcbSChuck Lever struct nsm_res *resp) 53103eb1dcbSChuck Lever { 532bf269551SChuck Lever __be32 *p; 53303eb1dcbSChuck Lever 534bf269551SChuck Lever p = xdr_inline_decode(xdr, 4 + 4); 53503eb1dcbSChuck Lever if (unlikely(p == NULL)) 53603eb1dcbSChuck Lever return -EIO; 53749b17004SChuck Lever resp->status = be32_to_cpup(p++); 53849b17004SChuck Lever resp->state = be32_to_cpup(p); 53903eb1dcbSChuck Lever 540bf269551SChuck Lever dprintk("lockd: %s status %d state %d\n", 541bf269551SChuck Lever __func__, resp->status, resp->state); 5421da177e4SLinus Torvalds return 0; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 545bf269551SChuck Lever static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp, 546bf269551SChuck Lever struct xdr_stream *xdr, 54703eb1dcbSChuck Lever struct nsm_res *resp) 5481da177e4SLinus Torvalds { 549bf269551SChuck Lever __be32 *p; 55003eb1dcbSChuck Lever 551bf269551SChuck Lever p = xdr_inline_decode(xdr, 4); 55203eb1dcbSChuck Lever if (unlikely(p == NULL)) 55303eb1dcbSChuck Lever return -EIO; 55449b17004SChuck Lever resp->state = be32_to_cpup(p); 55503eb1dcbSChuck Lever 556bf269551SChuck Lever dprintk("lockd: %s state %d\n", __func__, resp->state); 5571da177e4SLinus Torvalds return 0; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5612ca7754dSChuck Lever #define SM_my_id_sz (SM_my_name_sz+3) 5622ca7754dSChuck Lever #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5632ca7754dSChuck Lever #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz) 5640490a54aSChuck Lever #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE)) 5650490a54aSChuck Lever #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz) 5661da177e4SLinus Torvalds #define SM_monres_sz 2 5671da177e4SLinus Torvalds #define SM_unmonres_sz 1 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds static struct rpc_procinfo nsm_procedures[] = { 57036e8e668SChuck Lever [NSMPROC_MON] = { 57136e8e668SChuck Lever .p_proc = NSMPROC_MON, 5729f06c719SChuck Lever .p_encode = (kxdreproc_t)nsm_xdr_enc_mon, 573bf269551SChuck Lever .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat_res, 5742bea90d4SChuck Lever .p_arglen = SM_mon_sz, 5752bea90d4SChuck Lever .p_replen = SM_monres_sz, 57636e8e668SChuck Lever .p_statidx = NSMPROC_MON, 577cc0175c1SChuck Lever .p_name = "MONITOR", 5781da177e4SLinus Torvalds }, 57936e8e668SChuck Lever [NSMPROC_UNMON] = { 58036e8e668SChuck Lever .p_proc = NSMPROC_UNMON, 5819f06c719SChuck Lever .p_encode = (kxdreproc_t)nsm_xdr_enc_unmon, 582bf269551SChuck Lever .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat, 5832bea90d4SChuck Lever .p_arglen = SM_mon_id_sz, 5842bea90d4SChuck Lever .p_replen = SM_unmonres_sz, 58536e8e668SChuck Lever .p_statidx = NSMPROC_UNMON, 586cc0175c1SChuck Lever .p_name = "UNMONITOR", 5871da177e4SLinus Torvalds }, 5881da177e4SLinus Torvalds }; 5891da177e4SLinus Torvalds 590a613fa16STrond Myklebust static const struct rpc_version nsm_version1 = { 5911da177e4SLinus Torvalds .number = 1, 592e8c96f8cSTobias Klauser .nrprocs = ARRAY_SIZE(nsm_procedures), 5931da177e4SLinus Torvalds .procs = nsm_procedures 5941da177e4SLinus Torvalds }; 5951da177e4SLinus Torvalds 596a613fa16STrond Myklebust static const struct rpc_version *nsm_version[] = { 5971da177e4SLinus Torvalds [1] = &nsm_version1, 5981da177e4SLinus Torvalds }; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds static struct rpc_stat nsm_stats; 6011da177e4SLinus Torvalds 602a613fa16STrond Myklebust static const struct rpc_program nsm_program = { 6031da177e4SLinus Torvalds .name = "statd", 60436e8e668SChuck Lever .number = NSM_PROGRAM, 605e8c96f8cSTobias Klauser .nrvers = ARRAY_SIZE(nsm_version), 6061da177e4SLinus Torvalds .version = nsm_version, 6071da177e4SLinus Torvalds .stats = &nsm_stats 6081da177e4SLinus Torvalds }; 609