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/utsname.h> 111da177e4SLinus Torvalds #include <linux/kernel.h> 1294da7663SChuck Lever #include <linux/ktime.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 191da177e4SLinus Torvalds #define NLMDBG_FACILITY NLMDBG_MONITOR 2036e8e668SChuck Lever #define NSM_PROGRAM 100024 2136e8e668SChuck Lever #define NSM_VERSION 1 2236e8e668SChuck Lever 2336e8e668SChuck Lever enum { 2436e8e668SChuck Lever NSMPROC_NULL, 2536e8e668SChuck Lever NSMPROC_STAT, 2636e8e668SChuck Lever NSMPROC_MON, 2736e8e668SChuck Lever NSMPROC_UNMON, 2836e8e668SChuck Lever NSMPROC_UNMON_ALL, 2936e8e668SChuck Lever NSMPROC_SIMU_CRASH, 3036e8e668SChuck Lever NSMPROC_NOTIFY, 3136e8e668SChuck Lever }; 321da177e4SLinus Torvalds 339c1bfd03SChuck Lever struct nsm_args { 34cab2d3c9SChuck Lever struct nsm_private *priv; 359c1bfd03SChuck Lever u32 prog; /* RPC callback info */ 369c1bfd03SChuck Lever u32 vers; 379c1bfd03SChuck Lever u32 proc; 389c1bfd03SChuck Lever 399c1bfd03SChuck Lever char *mon_name; 409c1bfd03SChuck Lever }; 419c1bfd03SChuck Lever 429c1bfd03SChuck Lever struct nsm_res { 439c1bfd03SChuck Lever u32 status; 449c1bfd03SChuck Lever u32 state; 459c1bfd03SChuck Lever }; 469c1bfd03SChuck Lever 471da177e4SLinus Torvalds static struct rpc_program nsm_program; 4867c6d107SChuck Lever static LIST_HEAD(nsm_handles); 4967c6d107SChuck Lever static DEFINE_SPINLOCK(nsm_lock); 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds /* 521da177e4SLinus Torvalds * Local NSM state 531da177e4SLinus Torvalds */ 54b7ba597fSChuck Lever int __read_mostly nsm_local_state; 55b7ba597fSChuck Lever int __read_mostly nsm_use_hostnames; 561da177e4SLinus Torvalds 578529bc51SChuck Lever static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm) 588529bc51SChuck Lever { 598529bc51SChuck Lever return (struct sockaddr *)&nsm->sm_addr; 608529bc51SChuck Lever } 618529bc51SChuck Lever 6267c6d107SChuck Lever static void nsm_display_ipv4_address(const struct sockaddr *sap, char *buf, 6367c6d107SChuck Lever const size_t len) 6467c6d107SChuck Lever { 6567c6d107SChuck Lever const struct sockaddr_in *sin = (struct sockaddr_in *)sap; 6667c6d107SChuck Lever snprintf(buf, len, "%pI4", &sin->sin_addr.s_addr); 6767c6d107SChuck Lever } 6867c6d107SChuck Lever 6967c6d107SChuck Lever static void nsm_display_ipv6_address(const struct sockaddr *sap, char *buf, 7067c6d107SChuck Lever const size_t len) 7167c6d107SChuck Lever { 7267c6d107SChuck Lever const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; 7367c6d107SChuck Lever 7467c6d107SChuck Lever if (ipv6_addr_v4mapped(&sin6->sin6_addr)) 7567c6d107SChuck Lever snprintf(buf, len, "%pI4", &sin6->sin6_addr.s6_addr32[3]); 7667c6d107SChuck Lever else if (sin6->sin6_scope_id != 0) 7767c6d107SChuck Lever snprintf(buf, len, "%pI6%%%u", &sin6->sin6_addr, 7867c6d107SChuck Lever sin6->sin6_scope_id); 7967c6d107SChuck Lever else 8067c6d107SChuck Lever snprintf(buf, len, "%pI6", &sin6->sin6_addr); 8167c6d107SChuck Lever } 8267c6d107SChuck Lever 8367c6d107SChuck Lever static void nsm_display_address(const struct sockaddr *sap, 8467c6d107SChuck Lever char *buf, const size_t len) 8567c6d107SChuck Lever { 8667c6d107SChuck Lever switch (sap->sa_family) { 8767c6d107SChuck Lever case AF_INET: 8867c6d107SChuck Lever nsm_display_ipv4_address(sap, buf, len); 8967c6d107SChuck Lever break; 9067c6d107SChuck Lever case AF_INET6: 9167c6d107SChuck Lever nsm_display_ipv6_address(sap, buf, len); 9267c6d107SChuck Lever break; 9367c6d107SChuck Lever default: 9467c6d107SChuck Lever snprintf(buf, len, "unsupported address family"); 9567c6d107SChuck Lever break; 9667c6d107SChuck Lever } 9767c6d107SChuck Lever } 9867c6d107SChuck Lever 99*49b5699bSChuck Lever static struct rpc_clnt *nsm_create(void) 100*49b5699bSChuck Lever { 101*49b5699bSChuck Lever struct sockaddr_in sin = { 102*49b5699bSChuck Lever .sin_family = AF_INET, 103*49b5699bSChuck Lever .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 104*49b5699bSChuck Lever }; 105*49b5699bSChuck Lever struct rpc_create_args args = { 106*49b5699bSChuck Lever .protocol = XPRT_TRANSPORT_UDP, 107*49b5699bSChuck Lever .address = (struct sockaddr *)&sin, 108*49b5699bSChuck Lever .addrsize = sizeof(sin), 109*49b5699bSChuck Lever .servername = "rpc.statd", 110*49b5699bSChuck Lever .program = &nsm_program, 111*49b5699bSChuck Lever .version = NSM_VERSION, 112*49b5699bSChuck Lever .authflavor = RPC_AUTH_NULL, 113*49b5699bSChuck Lever }; 114*49b5699bSChuck Lever 115*49b5699bSChuck Lever return rpc_create(&args); 116*49b5699bSChuck Lever } 117*49b5699bSChuck Lever 118*49b5699bSChuck Lever static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds struct rpc_clnt *clnt; 1211da177e4SLinus Torvalds int status; 122a4846750SChuck Lever struct nsm_args args = { 123cab2d3c9SChuck Lever .priv = &nsm->sm_priv, 124a4846750SChuck Lever .prog = NLM_PROGRAM, 125a4846750SChuck Lever .vers = 3, 126a4846750SChuck Lever .proc = NLMPROC_NSM_NOTIFY, 12729ed1407SChuck Lever .mon_name = nsm->sm_mon_name, 128a4846750SChuck Lever }; 129dead28daSChuck Lever struct rpc_message msg = { 130dead28daSChuck Lever .rpc_argp = &args, 131dead28daSChuck Lever .rpc_resp = res, 132dead28daSChuck Lever }; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds clnt = nsm_create(); 1351da177e4SLinus Torvalds if (IS_ERR(clnt)) { 1361da177e4SLinus Torvalds status = PTR_ERR(clnt); 1375acf4315SChuck Lever dprintk("lockd: failed to create NSM upcall transport, " 1385acf4315SChuck Lever "status=%d\n", status); 1391da177e4SLinus Torvalds goto out; 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds memset(res, 0, sizeof(*res)); 1431da177e4SLinus Torvalds 144dead28daSChuck Lever msg.rpc_proc = &clnt->cl_procinfo[proc]; 145dead28daSChuck Lever status = rpc_call_sync(clnt, &msg, 0); 1461da177e4SLinus Torvalds if (status < 0) 1475acf4315SChuck Lever dprintk("lockd: NSM upcall RPC failed, status=%d\n", 1481da177e4SLinus Torvalds status); 1491da177e4SLinus Torvalds else 1501da177e4SLinus Torvalds status = 0; 15190c5755fSTrond Myklebust rpc_shutdown_client(clnt); 1521da177e4SLinus Torvalds out: 1531da177e4SLinus Torvalds return status; 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561e49323cSChuck Lever /** 1571e49323cSChuck Lever * nsm_monitor - Notify a peer in case we reboot 1581e49323cSChuck Lever * @host: pointer to nlm_host of peer to notify 1591e49323cSChuck Lever * 1601e49323cSChuck Lever * If this peer is not already monitored, this function sends an 1611e49323cSChuck Lever * upcall to the local rpc.statd to record the name/address of 1621e49323cSChuck Lever * the peer to notify in case we reboot. 1631e49323cSChuck Lever * 1641e49323cSChuck Lever * Returns zero if the peer is monitored by the local rpc.statd; 1651e49323cSChuck Lever * otherwise a negative errno value is returned. 1661da177e4SLinus Torvalds */ 1671e49323cSChuck Lever int nsm_monitor(const struct nlm_host *host) 1681da177e4SLinus Torvalds { 1698dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 1701da177e4SLinus Torvalds struct nsm_res res; 1711da177e4SLinus Torvalds int status; 1721da177e4SLinus Torvalds 1739fee4902SChuck Lever dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name); 1748dead0dbSOlaf Kirch 1758dead0dbSOlaf Kirch if (nsm->sm_monitored) 176977faf39SOlaf Kirch return 0; 1771da177e4SLinus Torvalds 17829ed1407SChuck Lever /* 17929ed1407SChuck Lever * Choose whether to record the caller_name or IP address of 18029ed1407SChuck Lever * this peer in the local rpc.statd's database. 18129ed1407SChuck Lever */ 18229ed1407SChuck Lever nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf; 18329ed1407SChuck Lever 18436e8e668SChuck Lever status = nsm_mon_unmon(nsm, NSMPROC_MON, &res); 1855d254b11SChuck Lever if (res.status != 0) 1865d254b11SChuck Lever status = -EIO; 1875d254b11SChuck Lever if (status < 0) 1889fee4902SChuck Lever printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name); 1891da177e4SLinus Torvalds else 1908dead0dbSOlaf Kirch nsm->sm_monitored = 1; 1911da177e4SLinus Torvalds return status; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 194356c3eb4SChuck Lever /** 195356c3eb4SChuck Lever * nsm_unmonitor - Unregister peer notification 196356c3eb4SChuck Lever * @host: pointer to nlm_host of peer to stop monitoring 197356c3eb4SChuck Lever * 198356c3eb4SChuck Lever * If this peer is monitored, this function sends an upcall to 199356c3eb4SChuck Lever * tell the local rpc.statd not to send this peer a notification 200356c3eb4SChuck Lever * when we reboot. 2011da177e4SLinus Torvalds */ 202356c3eb4SChuck Lever void nsm_unmonitor(const struct nlm_host *host) 2031da177e4SLinus Torvalds { 2048dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 2051da177e4SLinus Torvalds struct nsm_res res; 206356c3eb4SChuck Lever int status; 2071da177e4SLinus Torvalds 2089502c522SOlaf Kirch if (atomic_read(&nsm->sm_count) == 1 2099502c522SOlaf Kirch && nsm->sm_monitored && !nsm->sm_sticky) { 2109fee4902SChuck Lever dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name); 2119502c522SOlaf Kirch 21236e8e668SChuck Lever status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res); 2130c7aef45SChuck Lever if (res.status != 0) 2140c7aef45SChuck Lever status = -EIO; 2151da177e4SLinus Torvalds if (status < 0) 2169502c522SOlaf Kirch printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", 2179fee4902SChuck Lever nsm->sm_name); 2189502c522SOlaf Kirch else 2198dead0dbSOlaf Kirch nsm->sm_monitored = 0; 220977faf39SOlaf Kirch } 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2233420a8c4SChuck Lever static struct nsm_handle *nsm_lookup_hostname(const char *hostname, 2243420a8c4SChuck Lever const size_t len) 2253420a8c4SChuck Lever { 2263420a8c4SChuck Lever struct nsm_handle *nsm; 2273420a8c4SChuck Lever 2283420a8c4SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 2293420a8c4SChuck Lever if (strlen(nsm->sm_name) == len && 2303420a8c4SChuck Lever memcmp(nsm->sm_name, hostname, len) == 0) 2313420a8c4SChuck Lever return nsm; 2323420a8c4SChuck Lever return NULL; 2333420a8c4SChuck Lever } 2343420a8c4SChuck Lever 23577a3ef33SChuck Lever static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap) 23677a3ef33SChuck Lever { 23777a3ef33SChuck Lever struct nsm_handle *nsm; 23877a3ef33SChuck Lever 23977a3ef33SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 24077a3ef33SChuck Lever if (nlm_cmp_addr(nsm_addr(nsm), sap)) 24177a3ef33SChuck Lever return nsm; 24277a3ef33SChuck Lever return NULL; 24377a3ef33SChuck Lever } 24477a3ef33SChuck Lever 2453420a8c4SChuck Lever static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv) 2463420a8c4SChuck Lever { 2473420a8c4SChuck Lever struct nsm_handle *nsm; 2483420a8c4SChuck Lever 2493420a8c4SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 2503420a8c4SChuck Lever if (memcmp(nsm->sm_priv.data, priv->data, 2513420a8c4SChuck Lever sizeof(priv->data)) == 0) 2523420a8c4SChuck Lever return nsm; 2533420a8c4SChuck Lever return NULL; 2543420a8c4SChuck Lever } 2553420a8c4SChuck Lever 2567e44d3beSChuck Lever /* 2577e44d3beSChuck Lever * Construct a unique cookie to match this nsm_handle to this monitored 2587e44d3beSChuck Lever * host. It is passed to the local rpc.statd via NSMPROC_MON, and 2597e44d3beSChuck Lever * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these 2607e44d3beSChuck Lever * requests. 2617e44d3beSChuck Lever * 26294da7663SChuck Lever * The NSM protocol requires that these cookies be unique while the 26394da7663SChuck Lever * system is running. We prefer a stronger requirement of making them 26494da7663SChuck Lever * unique across reboots. If user space bugs cause a stale cookie to 26594da7663SChuck Lever * be sent to the kernel, it could cause the wrong host to lose its 26694da7663SChuck Lever * lock state if cookies were not unique across reboots. 26794da7663SChuck Lever * 26894da7663SChuck Lever * The cookies are exposed only to local user space via loopback. They 26994da7663SChuck Lever * do not appear on the physical network. If we want greater security 27094da7663SChuck Lever * for some reason, nsm_init_private() could perform a one-way hash to 27194da7663SChuck Lever * obscure the contents of the cookie. 2727e44d3beSChuck Lever */ 2737e44d3beSChuck Lever static void nsm_init_private(struct nsm_handle *nsm) 2747e44d3beSChuck Lever { 27594da7663SChuck Lever u64 *p = (u64 *)&nsm->sm_priv.data; 27694da7663SChuck Lever struct timespec ts; 27794da7663SChuck Lever 27894da7663SChuck Lever ktime_get_ts(&ts); 27994da7663SChuck Lever *p++ = timespec_to_ns(&ts); 28094da7663SChuck Lever *p = (unsigned long)nsm; 2817e44d3beSChuck Lever } 2827e44d3beSChuck Lever 283b39b897cSChuck Lever static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap, 284b39b897cSChuck Lever const size_t salen, 285b39b897cSChuck Lever const char *hostname, 286b39b897cSChuck Lever const size_t hostname_len) 287b39b897cSChuck Lever { 288b39b897cSChuck Lever struct nsm_handle *new; 289b39b897cSChuck Lever 290b39b897cSChuck Lever new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL); 291b39b897cSChuck Lever if (unlikely(new == NULL)) 292b39b897cSChuck Lever return NULL; 293b39b897cSChuck Lever 294b39b897cSChuck Lever atomic_set(&new->sm_count, 1); 295b39b897cSChuck Lever new->sm_name = (char *)(new + 1); 296b39b897cSChuck Lever memcpy(nsm_addr(new), sap, salen); 297b39b897cSChuck Lever new->sm_addrlen = salen; 298b39b897cSChuck Lever nsm_init_private(new); 299b39b897cSChuck Lever nsm_display_address((const struct sockaddr *)&new->sm_addr, 300b39b897cSChuck Lever new->sm_addrbuf, sizeof(new->sm_addrbuf)); 301b39b897cSChuck Lever memcpy(new->sm_name, hostname, hostname_len); 302b39b897cSChuck Lever new->sm_name[hostname_len] = '\0'; 303b39b897cSChuck Lever 304b39b897cSChuck Lever return new; 305b39b897cSChuck Lever } 306b39b897cSChuck Lever 30767c6d107SChuck Lever /** 30892fd91b9SChuck Lever * nsm_get_handle - Find or create a cached nsm_handle 30967c6d107SChuck Lever * @sap: pointer to socket address of handle to find 31067c6d107SChuck Lever * @salen: length of socket address 31167c6d107SChuck Lever * @hostname: pointer to C string containing hostname to find 31267c6d107SChuck Lever * @hostname_len: length of C string 31367c6d107SChuck Lever * 31492fd91b9SChuck Lever * Behavior is modulated by the global nsm_use_hostnames variable. 31567c6d107SChuck Lever * 31692fd91b9SChuck Lever * Returns a cached nsm_handle after bumping its ref count, or 31792fd91b9SChuck Lever * returns a fresh nsm_handle if a handle that matches @sap and/or 31892fd91b9SChuck Lever * @hostname cannot be found in the handle cache. Returns NULL if 31992fd91b9SChuck Lever * an error occurs. 32067c6d107SChuck Lever */ 32192fd91b9SChuck Lever struct nsm_handle *nsm_get_handle(const struct sockaddr *sap, 32292fd91b9SChuck Lever const size_t salen, const char *hostname, 32392fd91b9SChuck Lever const size_t hostname_len) 32467c6d107SChuck Lever { 32577a3ef33SChuck Lever struct nsm_handle *cached, *new = NULL; 32667c6d107SChuck Lever 32767c6d107SChuck Lever if (hostname && memchr(hostname, '/', hostname_len) != NULL) { 32867c6d107SChuck Lever if (printk_ratelimit()) { 32967c6d107SChuck Lever printk(KERN_WARNING "Invalid hostname \"%.*s\" " 33067c6d107SChuck Lever "in NFS lock request\n", 33167c6d107SChuck Lever (int)hostname_len, hostname); 33267c6d107SChuck Lever } 33367c6d107SChuck Lever return NULL; 33467c6d107SChuck Lever } 33567c6d107SChuck Lever 33667c6d107SChuck Lever retry: 33767c6d107SChuck Lever spin_lock(&nsm_lock); 33867c6d107SChuck Lever 33977a3ef33SChuck Lever if (nsm_use_hostnames && hostname != NULL) 34077a3ef33SChuck Lever cached = nsm_lookup_hostname(hostname, hostname_len); 34177a3ef33SChuck Lever else 34277a3ef33SChuck Lever cached = nsm_lookup_addr(sap); 34377a3ef33SChuck Lever 34477a3ef33SChuck Lever if (cached != NULL) { 34577a3ef33SChuck Lever atomic_inc(&cached->sm_count); 34677a3ef33SChuck Lever spin_unlock(&nsm_lock); 34777a3ef33SChuck Lever kfree(new); 34877a3ef33SChuck Lever dprintk("lockd: found nsm_handle for %s (%s), " 34977a3ef33SChuck Lever "cnt %d\n", cached->sm_name, 35077a3ef33SChuck Lever cached->sm_addrbuf, 35177a3ef33SChuck Lever atomic_read(&cached->sm_count)); 35277a3ef33SChuck Lever return cached; 35367c6d107SChuck Lever } 35477a3ef33SChuck Lever 35577a3ef33SChuck Lever if (new != NULL) { 35677a3ef33SChuck Lever list_add(&new->sm_link, &nsm_handles); 35777a3ef33SChuck Lever spin_unlock(&nsm_lock); 3585cf1c4b1SChuck Lever dprintk("lockd: created nsm_handle for %s (%s)\n", 35977a3ef33SChuck Lever new->sm_name, new->sm_addrbuf); 36077a3ef33SChuck Lever return new; 36167c6d107SChuck Lever } 36277a3ef33SChuck Lever 36367c6d107SChuck Lever spin_unlock(&nsm_lock); 36467c6d107SChuck Lever 36577a3ef33SChuck Lever new = nsm_create_handle(sap, salen, hostname, hostname_len); 36677a3ef33SChuck Lever if (unlikely(new == NULL)) 36767c6d107SChuck Lever return NULL; 36867c6d107SChuck Lever goto retry; 36967c6d107SChuck Lever } 37067c6d107SChuck Lever 37167c6d107SChuck Lever /** 3723420a8c4SChuck Lever * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle 3733420a8c4SChuck Lever * @info: pointer to NLMPROC_SM_NOTIFY arguments 3743420a8c4SChuck Lever * 3753420a8c4SChuck Lever * Returns a matching nsm_handle if found in the nsm cache; the returned 3763420a8c4SChuck Lever * nsm_handle's reference count is bumped and sm_monitored is cleared. 3773420a8c4SChuck Lever * Otherwise returns NULL if some error occurred. 3783420a8c4SChuck Lever */ 3793420a8c4SChuck Lever struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info) 3803420a8c4SChuck Lever { 3813420a8c4SChuck Lever struct nsm_handle *cached; 3823420a8c4SChuck Lever 3833420a8c4SChuck Lever spin_lock(&nsm_lock); 3843420a8c4SChuck Lever 3853420a8c4SChuck Lever cached = nsm_lookup_priv(&info->priv); 3863420a8c4SChuck Lever if (unlikely(cached == NULL)) { 3873420a8c4SChuck Lever spin_unlock(&nsm_lock); 3883420a8c4SChuck Lever dprintk("lockd: never saw rebooted peer '%.*s' before\n", 3893420a8c4SChuck Lever info->len, info->mon); 3903420a8c4SChuck Lever return cached; 3913420a8c4SChuck Lever } 3923420a8c4SChuck Lever 3933420a8c4SChuck Lever atomic_inc(&cached->sm_count); 3943420a8c4SChuck Lever spin_unlock(&nsm_lock); 3953420a8c4SChuck Lever 3963420a8c4SChuck Lever /* 3973420a8c4SChuck Lever * During subsequent lock activity, force a fresh 3983420a8c4SChuck Lever * notification to be set up for this host. 3993420a8c4SChuck Lever */ 4003420a8c4SChuck Lever cached->sm_monitored = 0; 4013420a8c4SChuck Lever 4023420a8c4SChuck Lever dprintk("lockd: host %s (%s) rebooted, cnt %d\n", 4033420a8c4SChuck Lever cached->sm_name, cached->sm_addrbuf, 4043420a8c4SChuck Lever atomic_read(&cached->sm_count)); 4053420a8c4SChuck Lever return cached; 4063420a8c4SChuck Lever } 4073420a8c4SChuck Lever 4083420a8c4SChuck Lever /** 40967c6d107SChuck Lever * nsm_release - Release an NSM handle 41067c6d107SChuck Lever * @nsm: pointer to handle to be released 41167c6d107SChuck Lever * 41267c6d107SChuck Lever */ 41367c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm) 41467c6d107SChuck Lever { 41567c6d107SChuck Lever if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) { 41667c6d107SChuck Lever list_del(&nsm->sm_link); 41767c6d107SChuck Lever spin_unlock(&nsm_lock); 4185cf1c4b1SChuck Lever dprintk("lockd: destroyed nsm_handle for %s (%s)\n", 4195cf1c4b1SChuck Lever nsm->sm_name, nsm->sm_addrbuf); 42067c6d107SChuck Lever kfree(nsm); 42167c6d107SChuck Lever } 42267c6d107SChuck Lever } 42367c6d107SChuck Lever 4241da177e4SLinus Torvalds /* 4251da177e4SLinus Torvalds * XDR functions for NSM. 4262ca7754dSChuck Lever * 4272ca7754dSChuck Lever * See http://www.opengroup.org/ for details on the Network 4282ca7754dSChuck Lever * Status Monitor wire protocol. 4291da177e4SLinus Torvalds */ 4301da177e4SLinus Torvalds 43103eb1dcbSChuck Lever static int encode_nsm_string(struct xdr_stream *xdr, const char *string) 432099bd05fSChuck Lever { 43303eb1dcbSChuck Lever const u32 len = strlen(string); 43403eb1dcbSChuck Lever __be32 *p; 435099bd05fSChuck Lever 43603eb1dcbSChuck Lever if (unlikely(len > SM_MAXSTRLEN)) 43703eb1dcbSChuck Lever return -EIO; 43803eb1dcbSChuck Lever p = xdr_reserve_space(xdr, sizeof(u32) + len); 43903eb1dcbSChuck Lever if (unlikely(p == NULL)) 44003eb1dcbSChuck Lever return -EIO; 44103eb1dcbSChuck Lever xdr_encode_opaque(p, string, len); 44203eb1dcbSChuck Lever return 0; 443099bd05fSChuck Lever } 444099bd05fSChuck Lever 44549695174SChuck Lever /* 44649695174SChuck Lever * "mon_name" specifies the host to be monitored. 44749695174SChuck Lever */ 44803eb1dcbSChuck Lever static int encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp) 44949695174SChuck Lever { 45003eb1dcbSChuck Lever return encode_nsm_string(xdr, argp->mon_name); 45149695174SChuck Lever } 45249695174SChuck Lever 453850c95fdSChuck Lever /* 454850c95fdSChuck Lever * The "my_id" argument specifies the hostname and RPC procedure 455850c95fdSChuck Lever * to be called when the status manager receives notification 45636e8e668SChuck Lever * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name" 457850c95fdSChuck Lever * has changed. 458850c95fdSChuck Lever */ 45903eb1dcbSChuck Lever static int encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp) 460850c95fdSChuck Lever { 46103eb1dcbSChuck Lever int status; 46203eb1dcbSChuck Lever __be32 *p; 463850c95fdSChuck Lever 46403eb1dcbSChuck Lever status = encode_nsm_string(xdr, utsname()->nodename); 46503eb1dcbSChuck Lever if (unlikely(status != 0)) 46603eb1dcbSChuck Lever return status; 46703eb1dcbSChuck Lever p = xdr_reserve_space(xdr, 3 * sizeof(u32)); 46803eb1dcbSChuck Lever if (unlikely(p == NULL)) 46903eb1dcbSChuck Lever return -EIO; 470850c95fdSChuck Lever *p++ = htonl(argp->prog); 471850c95fdSChuck Lever *p++ = htonl(argp->vers); 472850c95fdSChuck Lever *p++ = htonl(argp->proc); 47303eb1dcbSChuck Lever return 0; 474850c95fdSChuck Lever } 475850c95fdSChuck Lever 476ea72a7f1SChuck Lever /* 477ea72a7f1SChuck Lever * The "mon_id" argument specifies the non-private arguments 47836e8e668SChuck Lever * of an NSMPROC_MON or NSMPROC_UNMON call. 479ea72a7f1SChuck Lever */ 48003eb1dcbSChuck Lever static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp) 481ea72a7f1SChuck Lever { 48203eb1dcbSChuck Lever int status; 483ea72a7f1SChuck Lever 48403eb1dcbSChuck Lever status = encode_mon_name(xdr, argp); 48503eb1dcbSChuck Lever if (unlikely(status != 0)) 48603eb1dcbSChuck Lever return status; 48703eb1dcbSChuck Lever return encode_my_id(xdr, argp); 488ea72a7f1SChuck Lever } 489ea72a7f1SChuck Lever 4900490a54aSChuck Lever /* 4910490a54aSChuck Lever * The "priv" argument may contain private information required 49236e8e668SChuck Lever * by the NSMPROC_MON call. This information will be supplied in the 49336e8e668SChuck Lever * NLMPROC_SM_NOTIFY call. 4940490a54aSChuck Lever */ 49503eb1dcbSChuck Lever static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp) 4960490a54aSChuck Lever { 49703eb1dcbSChuck Lever __be32 *p; 49803eb1dcbSChuck Lever 49903eb1dcbSChuck Lever p = xdr_reserve_space(xdr, SM_PRIV_SIZE); 50003eb1dcbSChuck Lever if (unlikely(p == NULL)) 50103eb1dcbSChuck Lever return -EIO; 502cab2d3c9SChuck Lever xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE); 5031da177e4SLinus Torvalds return 0; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 50603eb1dcbSChuck Lever static int xdr_enc_mon(struct rpc_rqst *req, __be32 *p, 50703eb1dcbSChuck Lever const struct nsm_args *argp) 5081da177e4SLinus Torvalds { 50903eb1dcbSChuck Lever struct xdr_stream xdr; 51003eb1dcbSChuck Lever int status; 51103eb1dcbSChuck Lever 51203eb1dcbSChuck Lever xdr_init_encode(&xdr, &req->rq_snd_buf, p); 51303eb1dcbSChuck Lever status = encode_mon_id(&xdr, argp); 51403eb1dcbSChuck Lever if (unlikely(status)) 51503eb1dcbSChuck Lever return status; 51603eb1dcbSChuck Lever return encode_priv(&xdr, argp); 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 51903eb1dcbSChuck Lever static int xdr_enc_unmon(struct rpc_rqst *req, __be32 *p, 52003eb1dcbSChuck Lever const struct nsm_args *argp) 5211da177e4SLinus Torvalds { 52203eb1dcbSChuck Lever struct xdr_stream xdr; 52303eb1dcbSChuck Lever 52403eb1dcbSChuck Lever xdr_init_encode(&xdr, &req->rq_snd_buf, p); 52503eb1dcbSChuck Lever return encode_mon_id(&xdr, argp); 52603eb1dcbSChuck Lever } 52703eb1dcbSChuck Lever 52803eb1dcbSChuck Lever static int xdr_dec_stat_res(struct rpc_rqst *rqstp, __be32 *p, 52903eb1dcbSChuck Lever struct nsm_res *resp) 53003eb1dcbSChuck Lever { 53103eb1dcbSChuck Lever struct xdr_stream xdr; 53203eb1dcbSChuck Lever 53303eb1dcbSChuck Lever xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p); 53403eb1dcbSChuck Lever p = xdr_inline_decode(&xdr, 2 * sizeof(u32)); 53503eb1dcbSChuck Lever if (unlikely(p == NULL)) 53603eb1dcbSChuck Lever return -EIO; 5371da177e4SLinus Torvalds resp->status = ntohl(*p++); 53803eb1dcbSChuck Lever resp->state = ntohl(*p); 53903eb1dcbSChuck Lever 54003eb1dcbSChuck Lever dprintk("lockd: xdr_dec_stat_res status %d state %d\n", 5411da177e4SLinus Torvalds resp->status, resp->state); 5421da177e4SLinus Torvalds return 0; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 54503eb1dcbSChuck Lever static int xdr_dec_stat(struct rpc_rqst *rqstp, __be32 *p, 54603eb1dcbSChuck Lever struct nsm_res *resp) 5471da177e4SLinus Torvalds { 54803eb1dcbSChuck Lever struct xdr_stream xdr; 54903eb1dcbSChuck Lever 55003eb1dcbSChuck Lever xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p); 55103eb1dcbSChuck Lever p = xdr_inline_decode(&xdr, sizeof(u32)); 55203eb1dcbSChuck Lever if (unlikely(p == NULL)) 55303eb1dcbSChuck Lever return -EIO; 55403eb1dcbSChuck Lever resp->state = ntohl(*p); 55503eb1dcbSChuck Lever 55603eb1dcbSChuck Lever dprintk("lockd: xdr_dec_stat state %d\n", 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, 57203eb1dcbSChuck Lever .p_encode = (kxdrproc_t)xdr_enc_mon, 57303eb1dcbSChuck Lever .p_decode = (kxdrproc_t)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, 58103eb1dcbSChuck Lever .p_encode = (kxdrproc_t)xdr_enc_unmon, 58203eb1dcbSChuck Lever .p_decode = (kxdrproc_t)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 5901da177e4SLinus Torvalds static 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 5961da177e4SLinus Torvalds static struct rpc_version * nsm_version[] = { 5971da177e4SLinus Torvalds [1] = &nsm_version1, 5981da177e4SLinus Torvalds }; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds static struct rpc_stat nsm_stats; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds static 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