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 19ad5b365cSMans Rullgard #include <asm/unaligned.h> 20ad5b365cSMans Rullgard 211da177e4SLinus Torvalds #define NLMDBG_FACILITY NLMDBG_MONITOR 2236e8e668SChuck Lever #define NSM_PROGRAM 100024 2336e8e668SChuck Lever #define NSM_VERSION 1 2436e8e668SChuck Lever 2536e8e668SChuck Lever enum { 2636e8e668SChuck Lever NSMPROC_NULL, 2736e8e668SChuck Lever NSMPROC_STAT, 2836e8e668SChuck Lever NSMPROC_MON, 2936e8e668SChuck Lever NSMPROC_UNMON, 3036e8e668SChuck Lever NSMPROC_UNMON_ALL, 3136e8e668SChuck Lever NSMPROC_SIMU_CRASH, 3236e8e668SChuck Lever NSMPROC_NOTIFY, 3336e8e668SChuck Lever }; 341da177e4SLinus Torvalds 359c1bfd03SChuck Lever struct nsm_args { 36cab2d3c9SChuck Lever struct nsm_private *priv; 379c1bfd03SChuck Lever u32 prog; /* RPC callback info */ 389c1bfd03SChuck Lever u32 vers; 399c1bfd03SChuck Lever u32 proc; 409c1bfd03SChuck Lever 419c1bfd03SChuck Lever char *mon_name; 429c1bfd03SChuck Lever }; 439c1bfd03SChuck Lever 449c1bfd03SChuck Lever struct nsm_res { 459c1bfd03SChuck Lever u32 status; 469c1bfd03SChuck Lever u32 state; 479c1bfd03SChuck Lever }; 489c1bfd03SChuck Lever 491da177e4SLinus Torvalds static struct rpc_program nsm_program; 5067c6d107SChuck Lever static LIST_HEAD(nsm_handles); 5167c6d107SChuck Lever static DEFINE_SPINLOCK(nsm_lock); 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* 541da177e4SLinus Torvalds * Local NSM state 551da177e4SLinus Torvalds */ 566c9dc425SChuck Lever u32 __read_mostly nsm_local_state; 57b7ba597fSChuck Lever int __read_mostly nsm_use_hostnames; 581da177e4SLinus Torvalds 598529bc51SChuck Lever static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm) 608529bc51SChuck Lever { 618529bc51SChuck Lever return (struct sockaddr *)&nsm->sm_addr; 628529bc51SChuck Lever } 638529bc51SChuck Lever 6467c6d107SChuck Lever static void nsm_display_ipv4_address(const struct sockaddr *sap, char *buf, 6567c6d107SChuck Lever const size_t len) 6667c6d107SChuck Lever { 6767c6d107SChuck Lever const struct sockaddr_in *sin = (struct sockaddr_in *)sap; 6867c6d107SChuck Lever snprintf(buf, len, "%pI4", &sin->sin_addr.s_addr); 6967c6d107SChuck Lever } 7067c6d107SChuck Lever 7167c6d107SChuck Lever static void nsm_display_ipv6_address(const struct sockaddr *sap, char *buf, 7267c6d107SChuck Lever const size_t len) 7367c6d107SChuck Lever { 7467c6d107SChuck Lever const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; 7567c6d107SChuck Lever 7667c6d107SChuck Lever if (ipv6_addr_v4mapped(&sin6->sin6_addr)) 7767c6d107SChuck Lever snprintf(buf, len, "%pI4", &sin6->sin6_addr.s6_addr32[3]); 7867c6d107SChuck Lever else if (sin6->sin6_scope_id != 0) 7967c6d107SChuck Lever snprintf(buf, len, "%pI6%%%u", &sin6->sin6_addr, 8067c6d107SChuck Lever sin6->sin6_scope_id); 8167c6d107SChuck Lever else 8267c6d107SChuck Lever snprintf(buf, len, "%pI6", &sin6->sin6_addr); 8367c6d107SChuck Lever } 8467c6d107SChuck Lever 8567c6d107SChuck Lever static void nsm_display_address(const struct sockaddr *sap, 8667c6d107SChuck Lever char *buf, const size_t len) 8767c6d107SChuck Lever { 8867c6d107SChuck Lever switch (sap->sa_family) { 8967c6d107SChuck Lever case AF_INET: 9067c6d107SChuck Lever nsm_display_ipv4_address(sap, buf, len); 9167c6d107SChuck Lever break; 9267c6d107SChuck Lever case AF_INET6: 9367c6d107SChuck Lever nsm_display_ipv6_address(sap, buf, len); 9467c6d107SChuck Lever break; 9567c6d107SChuck Lever default: 9667c6d107SChuck Lever snprintf(buf, len, "unsupported address family"); 9767c6d107SChuck Lever break; 9867c6d107SChuck Lever } 9967c6d107SChuck Lever } 10067c6d107SChuck Lever 10149b5699bSChuck Lever static struct rpc_clnt *nsm_create(void) 10249b5699bSChuck Lever { 10349b5699bSChuck Lever struct sockaddr_in sin = { 10449b5699bSChuck Lever .sin_family = AF_INET, 10549b5699bSChuck Lever .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 10649b5699bSChuck Lever }; 10749b5699bSChuck Lever struct rpc_create_args args = { 10849b5699bSChuck Lever .protocol = XPRT_TRANSPORT_UDP, 10949b5699bSChuck Lever .address = (struct sockaddr *)&sin, 11049b5699bSChuck Lever .addrsize = sizeof(sin), 11149b5699bSChuck Lever .servername = "rpc.statd", 11249b5699bSChuck Lever .program = &nsm_program, 11349b5699bSChuck Lever .version = NSM_VERSION, 11449b5699bSChuck Lever .authflavor = RPC_AUTH_NULL, 115*0e5c2632SChuck Lever .flags = RPC_CLNT_CREATE_NOPING, 11649b5699bSChuck Lever }; 11749b5699bSChuck Lever 11849b5699bSChuck Lever return rpc_create(&args); 11949b5699bSChuck Lever } 12049b5699bSChuck Lever 12149b5699bSChuck Lever static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds struct rpc_clnt *clnt; 1241da177e4SLinus Torvalds int status; 125a4846750SChuck Lever struct nsm_args args = { 126cab2d3c9SChuck Lever .priv = &nsm->sm_priv, 127a4846750SChuck Lever .prog = NLM_PROGRAM, 128a4846750SChuck Lever .vers = 3, 129a4846750SChuck Lever .proc = NLMPROC_NSM_NOTIFY, 13029ed1407SChuck Lever .mon_name = nsm->sm_mon_name, 131a4846750SChuck Lever }; 132dead28daSChuck Lever struct rpc_message msg = { 133dead28daSChuck Lever .rpc_argp = &args, 134dead28daSChuck Lever .rpc_resp = res, 135dead28daSChuck Lever }; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds clnt = nsm_create(); 1381da177e4SLinus Torvalds if (IS_ERR(clnt)) { 1391da177e4SLinus Torvalds status = PTR_ERR(clnt); 1405acf4315SChuck Lever dprintk("lockd: failed to create NSM upcall transport, " 1415acf4315SChuck Lever "status=%d\n", status); 1421da177e4SLinus Torvalds goto out; 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds memset(res, 0, sizeof(*res)); 1461da177e4SLinus Torvalds 147dead28daSChuck Lever msg.rpc_proc = &clnt->cl_procinfo[proc]; 148dead28daSChuck Lever status = rpc_call_sync(clnt, &msg, 0); 1491da177e4SLinus Torvalds if (status < 0) 1505acf4315SChuck Lever dprintk("lockd: NSM upcall RPC failed, status=%d\n", 1511da177e4SLinus Torvalds status); 1521da177e4SLinus Torvalds else 1531da177e4SLinus Torvalds status = 0; 15490c5755fSTrond Myklebust rpc_shutdown_client(clnt); 1551da177e4SLinus Torvalds out: 1561da177e4SLinus Torvalds return status; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591e49323cSChuck Lever /** 1601e49323cSChuck Lever * nsm_monitor - Notify a peer in case we reboot 1611e49323cSChuck Lever * @host: pointer to nlm_host of peer to notify 1621e49323cSChuck Lever * 1631e49323cSChuck Lever * If this peer is not already monitored, this function sends an 1641e49323cSChuck Lever * upcall to the local rpc.statd to record the name/address of 1651e49323cSChuck Lever * the peer to notify in case we reboot. 1661e49323cSChuck Lever * 1671e49323cSChuck Lever * Returns zero if the peer is monitored by the local rpc.statd; 1681e49323cSChuck Lever * otherwise a negative errno value is returned. 1691da177e4SLinus Torvalds */ 1701e49323cSChuck Lever int nsm_monitor(const struct nlm_host *host) 1711da177e4SLinus Torvalds { 1728dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 1731da177e4SLinus Torvalds struct nsm_res res; 1741da177e4SLinus Torvalds int status; 1751da177e4SLinus Torvalds 1769fee4902SChuck Lever dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name); 1778dead0dbSOlaf Kirch 1788dead0dbSOlaf Kirch if (nsm->sm_monitored) 179977faf39SOlaf Kirch return 0; 1801da177e4SLinus Torvalds 18129ed1407SChuck Lever /* 18229ed1407SChuck Lever * Choose whether to record the caller_name or IP address of 18329ed1407SChuck Lever * this peer in the local rpc.statd's database. 18429ed1407SChuck Lever */ 18529ed1407SChuck Lever nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf; 18629ed1407SChuck Lever 18736e8e668SChuck Lever status = nsm_mon_unmon(nsm, NSMPROC_MON, &res); 1886c9dc425SChuck Lever if (unlikely(res.status != 0)) 1895d254b11SChuck Lever status = -EIO; 1906c9dc425SChuck Lever if (unlikely(status < 0)) { 1919fee4902SChuck Lever printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name); 1921da177e4SLinus Torvalds return status; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1956c9dc425SChuck Lever nsm->sm_monitored = 1; 1966c9dc425SChuck Lever if (unlikely(nsm_local_state != res.state)) { 1976c9dc425SChuck Lever nsm_local_state = res.state; 1986c9dc425SChuck Lever dprintk("lockd: NSM state changed to %d\n", nsm_local_state); 1996c9dc425SChuck Lever } 2006c9dc425SChuck Lever return 0; 2016c9dc425SChuck Lever } 2026c9dc425SChuck Lever 203356c3eb4SChuck Lever /** 204356c3eb4SChuck Lever * nsm_unmonitor - Unregister peer notification 205356c3eb4SChuck Lever * @host: pointer to nlm_host of peer to stop monitoring 206356c3eb4SChuck Lever * 207356c3eb4SChuck Lever * If this peer is monitored, this function sends an upcall to 208356c3eb4SChuck Lever * tell the local rpc.statd not to send this peer a notification 209356c3eb4SChuck Lever * when we reboot. 2101da177e4SLinus Torvalds */ 211356c3eb4SChuck Lever void nsm_unmonitor(const struct nlm_host *host) 2121da177e4SLinus Torvalds { 2138dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 2141da177e4SLinus Torvalds struct nsm_res res; 215356c3eb4SChuck Lever int status; 2161da177e4SLinus Torvalds 2179502c522SOlaf Kirch if (atomic_read(&nsm->sm_count) == 1 2189502c522SOlaf Kirch && nsm->sm_monitored && !nsm->sm_sticky) { 2199fee4902SChuck Lever dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name); 2209502c522SOlaf Kirch 22136e8e668SChuck Lever status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res); 2220c7aef45SChuck Lever if (res.status != 0) 2230c7aef45SChuck Lever status = -EIO; 2241da177e4SLinus Torvalds if (status < 0) 2259502c522SOlaf Kirch printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", 2269fee4902SChuck Lever nsm->sm_name); 2279502c522SOlaf Kirch else 2288dead0dbSOlaf Kirch nsm->sm_monitored = 0; 229977faf39SOlaf Kirch } 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2323420a8c4SChuck Lever static struct nsm_handle *nsm_lookup_hostname(const char *hostname, 2333420a8c4SChuck Lever const size_t len) 2343420a8c4SChuck Lever { 2353420a8c4SChuck Lever struct nsm_handle *nsm; 2363420a8c4SChuck Lever 2373420a8c4SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 2383420a8c4SChuck Lever if (strlen(nsm->sm_name) == len && 2393420a8c4SChuck Lever memcmp(nsm->sm_name, hostname, len) == 0) 2403420a8c4SChuck Lever return nsm; 2413420a8c4SChuck Lever return NULL; 2423420a8c4SChuck Lever } 2433420a8c4SChuck Lever 24477a3ef33SChuck Lever static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap) 24577a3ef33SChuck Lever { 24677a3ef33SChuck Lever struct nsm_handle *nsm; 24777a3ef33SChuck Lever 24877a3ef33SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 24977a3ef33SChuck Lever if (nlm_cmp_addr(nsm_addr(nsm), sap)) 25077a3ef33SChuck Lever return nsm; 25177a3ef33SChuck Lever return NULL; 25277a3ef33SChuck Lever } 25377a3ef33SChuck Lever 2543420a8c4SChuck Lever static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv) 2553420a8c4SChuck Lever { 2563420a8c4SChuck Lever struct nsm_handle *nsm; 2573420a8c4SChuck Lever 2583420a8c4SChuck Lever list_for_each_entry(nsm, &nsm_handles, sm_link) 2593420a8c4SChuck Lever if (memcmp(nsm->sm_priv.data, priv->data, 2603420a8c4SChuck Lever sizeof(priv->data)) == 0) 2613420a8c4SChuck Lever return nsm; 2623420a8c4SChuck Lever return NULL; 2633420a8c4SChuck Lever } 2643420a8c4SChuck Lever 2657e44d3beSChuck Lever /* 2667e44d3beSChuck Lever * Construct a unique cookie to match this nsm_handle to this monitored 2677e44d3beSChuck Lever * host. It is passed to the local rpc.statd via NSMPROC_MON, and 2687e44d3beSChuck Lever * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these 2697e44d3beSChuck Lever * requests. 2707e44d3beSChuck Lever * 27194da7663SChuck Lever * The NSM protocol requires that these cookies be unique while the 27294da7663SChuck Lever * system is running. We prefer a stronger requirement of making them 27394da7663SChuck Lever * unique across reboots. If user space bugs cause a stale cookie to 27494da7663SChuck Lever * be sent to the kernel, it could cause the wrong host to lose its 27594da7663SChuck Lever * lock state if cookies were not unique across reboots. 27694da7663SChuck Lever * 27794da7663SChuck Lever * The cookies are exposed only to local user space via loopback. They 27894da7663SChuck Lever * do not appear on the physical network. If we want greater security 27994da7663SChuck Lever * for some reason, nsm_init_private() could perform a one-way hash to 28094da7663SChuck Lever * obscure the contents of the cookie. 2817e44d3beSChuck Lever */ 2827e44d3beSChuck Lever static void nsm_init_private(struct nsm_handle *nsm) 2837e44d3beSChuck Lever { 28494da7663SChuck Lever u64 *p = (u64 *)&nsm->sm_priv.data; 28594da7663SChuck Lever struct timespec ts; 286ad5b365cSMans Rullgard s64 ns; 28794da7663SChuck Lever 28894da7663SChuck Lever ktime_get_ts(&ts); 289ad5b365cSMans Rullgard ns = timespec_to_ns(&ts); 290ad5b365cSMans Rullgard put_unaligned(ns, p); 291ad5b365cSMans Rullgard put_unaligned((unsigned long)nsm, p + 1); 2927e44d3beSChuck Lever } 2937e44d3beSChuck Lever 294b39b897cSChuck Lever static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap, 295b39b897cSChuck Lever const size_t salen, 296b39b897cSChuck Lever const char *hostname, 297b39b897cSChuck Lever const size_t hostname_len) 298b39b897cSChuck Lever { 299b39b897cSChuck Lever struct nsm_handle *new; 300b39b897cSChuck Lever 301b39b897cSChuck Lever new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL); 302b39b897cSChuck Lever if (unlikely(new == NULL)) 303b39b897cSChuck Lever return NULL; 304b39b897cSChuck Lever 305b39b897cSChuck Lever atomic_set(&new->sm_count, 1); 306b39b897cSChuck Lever new->sm_name = (char *)(new + 1); 307b39b897cSChuck Lever memcpy(nsm_addr(new), sap, salen); 308b39b897cSChuck Lever new->sm_addrlen = salen; 309b39b897cSChuck Lever nsm_init_private(new); 310b39b897cSChuck Lever nsm_display_address((const struct sockaddr *)&new->sm_addr, 311b39b897cSChuck Lever new->sm_addrbuf, sizeof(new->sm_addrbuf)); 312b39b897cSChuck Lever memcpy(new->sm_name, hostname, hostname_len); 313b39b897cSChuck Lever new->sm_name[hostname_len] = '\0'; 314b39b897cSChuck Lever 315b39b897cSChuck Lever return new; 316b39b897cSChuck Lever } 317b39b897cSChuck Lever 31867c6d107SChuck Lever /** 31992fd91b9SChuck Lever * nsm_get_handle - Find or create a cached nsm_handle 32067c6d107SChuck Lever * @sap: pointer to socket address of handle to find 32167c6d107SChuck Lever * @salen: length of socket address 32267c6d107SChuck Lever * @hostname: pointer to C string containing hostname to find 32367c6d107SChuck Lever * @hostname_len: length of C string 32467c6d107SChuck Lever * 32592fd91b9SChuck Lever * Behavior is modulated by the global nsm_use_hostnames variable. 32667c6d107SChuck Lever * 32792fd91b9SChuck Lever * Returns a cached nsm_handle after bumping its ref count, or 32892fd91b9SChuck Lever * returns a fresh nsm_handle if a handle that matches @sap and/or 32992fd91b9SChuck Lever * @hostname cannot be found in the handle cache. Returns NULL if 33092fd91b9SChuck Lever * an error occurs. 33167c6d107SChuck Lever */ 33292fd91b9SChuck Lever struct nsm_handle *nsm_get_handle(const struct sockaddr *sap, 33392fd91b9SChuck Lever const size_t salen, const char *hostname, 33492fd91b9SChuck Lever const size_t hostname_len) 33567c6d107SChuck Lever { 33677a3ef33SChuck Lever struct nsm_handle *cached, *new = NULL; 33767c6d107SChuck Lever 33867c6d107SChuck Lever if (hostname && memchr(hostname, '/', hostname_len) != NULL) { 33967c6d107SChuck Lever if (printk_ratelimit()) { 34067c6d107SChuck Lever printk(KERN_WARNING "Invalid hostname \"%.*s\" " 34167c6d107SChuck Lever "in NFS lock request\n", 34267c6d107SChuck Lever (int)hostname_len, hostname); 34367c6d107SChuck Lever } 34467c6d107SChuck Lever return NULL; 34567c6d107SChuck Lever } 34667c6d107SChuck Lever 34767c6d107SChuck Lever retry: 34867c6d107SChuck Lever spin_lock(&nsm_lock); 34967c6d107SChuck Lever 35077a3ef33SChuck Lever if (nsm_use_hostnames && hostname != NULL) 35177a3ef33SChuck Lever cached = nsm_lookup_hostname(hostname, hostname_len); 35277a3ef33SChuck Lever else 35377a3ef33SChuck Lever cached = nsm_lookup_addr(sap); 35477a3ef33SChuck Lever 35577a3ef33SChuck Lever if (cached != NULL) { 35677a3ef33SChuck Lever atomic_inc(&cached->sm_count); 35777a3ef33SChuck Lever spin_unlock(&nsm_lock); 35877a3ef33SChuck Lever kfree(new); 35977a3ef33SChuck Lever dprintk("lockd: found nsm_handle for %s (%s), " 36077a3ef33SChuck Lever "cnt %d\n", cached->sm_name, 36177a3ef33SChuck Lever cached->sm_addrbuf, 36277a3ef33SChuck Lever atomic_read(&cached->sm_count)); 36377a3ef33SChuck Lever return cached; 36467c6d107SChuck Lever } 36577a3ef33SChuck Lever 36677a3ef33SChuck Lever if (new != NULL) { 36777a3ef33SChuck Lever list_add(&new->sm_link, &nsm_handles); 36877a3ef33SChuck Lever spin_unlock(&nsm_lock); 3695cf1c4b1SChuck Lever dprintk("lockd: created nsm_handle for %s (%s)\n", 37077a3ef33SChuck Lever new->sm_name, new->sm_addrbuf); 37177a3ef33SChuck Lever return new; 37267c6d107SChuck Lever } 37377a3ef33SChuck Lever 37467c6d107SChuck Lever spin_unlock(&nsm_lock); 37567c6d107SChuck Lever 37677a3ef33SChuck Lever new = nsm_create_handle(sap, salen, hostname, hostname_len); 37777a3ef33SChuck Lever if (unlikely(new == NULL)) 37867c6d107SChuck Lever return NULL; 37967c6d107SChuck Lever goto retry; 38067c6d107SChuck Lever } 38167c6d107SChuck Lever 38267c6d107SChuck Lever /** 3833420a8c4SChuck Lever * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle 3843420a8c4SChuck Lever * @info: pointer to NLMPROC_SM_NOTIFY arguments 3853420a8c4SChuck Lever * 3863420a8c4SChuck Lever * Returns a matching nsm_handle if found in the nsm cache; the returned 3873420a8c4SChuck Lever * nsm_handle's reference count is bumped and sm_monitored is cleared. 3883420a8c4SChuck Lever * Otherwise returns NULL if some error occurred. 3893420a8c4SChuck Lever */ 3903420a8c4SChuck Lever struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info) 3913420a8c4SChuck Lever { 3923420a8c4SChuck Lever struct nsm_handle *cached; 3933420a8c4SChuck Lever 3943420a8c4SChuck Lever spin_lock(&nsm_lock); 3953420a8c4SChuck Lever 3963420a8c4SChuck Lever cached = nsm_lookup_priv(&info->priv); 3973420a8c4SChuck Lever if (unlikely(cached == NULL)) { 3983420a8c4SChuck Lever spin_unlock(&nsm_lock); 3993420a8c4SChuck Lever dprintk("lockd: never saw rebooted peer '%.*s' before\n", 4003420a8c4SChuck Lever info->len, info->mon); 4013420a8c4SChuck Lever return cached; 4023420a8c4SChuck Lever } 4033420a8c4SChuck Lever 4043420a8c4SChuck Lever atomic_inc(&cached->sm_count); 4053420a8c4SChuck Lever spin_unlock(&nsm_lock); 4063420a8c4SChuck Lever 4073420a8c4SChuck Lever /* 4083420a8c4SChuck Lever * During subsequent lock activity, force a fresh 4093420a8c4SChuck Lever * notification to be set up for this host. 4103420a8c4SChuck Lever */ 4113420a8c4SChuck Lever cached->sm_monitored = 0; 4123420a8c4SChuck Lever 4133420a8c4SChuck Lever dprintk("lockd: host %s (%s) rebooted, cnt %d\n", 4143420a8c4SChuck Lever cached->sm_name, cached->sm_addrbuf, 4153420a8c4SChuck Lever atomic_read(&cached->sm_count)); 4163420a8c4SChuck Lever return cached; 4173420a8c4SChuck Lever } 4183420a8c4SChuck Lever 4193420a8c4SChuck Lever /** 42067c6d107SChuck Lever * nsm_release - Release an NSM handle 42167c6d107SChuck Lever * @nsm: pointer to handle to be released 42267c6d107SChuck Lever * 42367c6d107SChuck Lever */ 42467c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm) 42567c6d107SChuck Lever { 42667c6d107SChuck Lever if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) { 42767c6d107SChuck Lever list_del(&nsm->sm_link); 42867c6d107SChuck Lever spin_unlock(&nsm_lock); 4295cf1c4b1SChuck Lever dprintk("lockd: destroyed nsm_handle for %s (%s)\n", 4305cf1c4b1SChuck Lever nsm->sm_name, nsm->sm_addrbuf); 43167c6d107SChuck Lever kfree(nsm); 43267c6d107SChuck Lever } 43367c6d107SChuck Lever } 43467c6d107SChuck Lever 4351da177e4SLinus Torvalds /* 4361da177e4SLinus Torvalds * XDR functions for NSM. 4372ca7754dSChuck Lever * 4382ca7754dSChuck Lever * See http://www.opengroup.org/ for details on the Network 4392ca7754dSChuck Lever * Status Monitor wire protocol. 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds 44203eb1dcbSChuck Lever static int encode_nsm_string(struct xdr_stream *xdr, const char *string) 443099bd05fSChuck Lever { 44403eb1dcbSChuck Lever const u32 len = strlen(string); 44503eb1dcbSChuck Lever __be32 *p; 446099bd05fSChuck Lever 44703eb1dcbSChuck Lever if (unlikely(len > SM_MAXSTRLEN)) 44803eb1dcbSChuck Lever return -EIO; 44903eb1dcbSChuck Lever p = xdr_reserve_space(xdr, sizeof(u32) + len); 45003eb1dcbSChuck Lever if (unlikely(p == NULL)) 45103eb1dcbSChuck Lever return -EIO; 45203eb1dcbSChuck Lever xdr_encode_opaque(p, string, len); 45303eb1dcbSChuck Lever return 0; 454099bd05fSChuck Lever } 455099bd05fSChuck Lever 45649695174SChuck Lever /* 45749695174SChuck Lever * "mon_name" specifies the host to be monitored. 45849695174SChuck Lever */ 45903eb1dcbSChuck Lever static int encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp) 46049695174SChuck Lever { 46103eb1dcbSChuck Lever return encode_nsm_string(xdr, argp->mon_name); 46249695174SChuck Lever } 46349695174SChuck Lever 464850c95fdSChuck Lever /* 465850c95fdSChuck Lever * The "my_id" argument specifies the hostname and RPC procedure 466850c95fdSChuck Lever * to be called when the status manager receives notification 46736e8e668SChuck Lever * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name" 468850c95fdSChuck Lever * has changed. 469850c95fdSChuck Lever */ 47003eb1dcbSChuck Lever static int encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp) 471850c95fdSChuck Lever { 47203eb1dcbSChuck Lever int status; 47303eb1dcbSChuck Lever __be32 *p; 474850c95fdSChuck Lever 47503eb1dcbSChuck Lever status = encode_nsm_string(xdr, utsname()->nodename); 47603eb1dcbSChuck Lever if (unlikely(status != 0)) 47703eb1dcbSChuck Lever return status; 47803eb1dcbSChuck Lever p = xdr_reserve_space(xdr, 3 * sizeof(u32)); 47903eb1dcbSChuck Lever if (unlikely(p == NULL)) 48003eb1dcbSChuck Lever return -EIO; 481850c95fdSChuck Lever *p++ = htonl(argp->prog); 482850c95fdSChuck Lever *p++ = htonl(argp->vers); 483850c95fdSChuck Lever *p++ = htonl(argp->proc); 48403eb1dcbSChuck Lever return 0; 485850c95fdSChuck Lever } 486850c95fdSChuck Lever 487ea72a7f1SChuck Lever /* 488ea72a7f1SChuck Lever * The "mon_id" argument specifies the non-private arguments 48936e8e668SChuck Lever * of an NSMPROC_MON or NSMPROC_UNMON call. 490ea72a7f1SChuck Lever */ 49103eb1dcbSChuck Lever static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp) 492ea72a7f1SChuck Lever { 49303eb1dcbSChuck Lever int status; 494ea72a7f1SChuck Lever 49503eb1dcbSChuck Lever status = encode_mon_name(xdr, argp); 49603eb1dcbSChuck Lever if (unlikely(status != 0)) 49703eb1dcbSChuck Lever return status; 49803eb1dcbSChuck Lever return encode_my_id(xdr, argp); 499ea72a7f1SChuck Lever } 500ea72a7f1SChuck Lever 5010490a54aSChuck Lever /* 5020490a54aSChuck Lever * The "priv" argument may contain private information required 50336e8e668SChuck Lever * by the NSMPROC_MON call. This information will be supplied in the 50436e8e668SChuck Lever * NLMPROC_SM_NOTIFY call. 5050490a54aSChuck Lever */ 50603eb1dcbSChuck Lever static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp) 5070490a54aSChuck Lever { 50803eb1dcbSChuck Lever __be32 *p; 50903eb1dcbSChuck Lever 51003eb1dcbSChuck Lever p = xdr_reserve_space(xdr, SM_PRIV_SIZE); 51103eb1dcbSChuck Lever if (unlikely(p == NULL)) 51203eb1dcbSChuck Lever return -EIO; 513cab2d3c9SChuck Lever xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE); 5141da177e4SLinus Torvalds return 0; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 51703eb1dcbSChuck Lever static int xdr_enc_mon(struct rpc_rqst *req, __be32 *p, 51803eb1dcbSChuck Lever const struct nsm_args *argp) 5191da177e4SLinus Torvalds { 52003eb1dcbSChuck Lever struct xdr_stream xdr; 52103eb1dcbSChuck Lever int status; 52203eb1dcbSChuck Lever 52303eb1dcbSChuck Lever xdr_init_encode(&xdr, &req->rq_snd_buf, p); 52403eb1dcbSChuck Lever status = encode_mon_id(&xdr, argp); 52503eb1dcbSChuck Lever if (unlikely(status)) 52603eb1dcbSChuck Lever return status; 52703eb1dcbSChuck Lever return encode_priv(&xdr, argp); 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 53003eb1dcbSChuck Lever static int xdr_enc_unmon(struct rpc_rqst *req, __be32 *p, 53103eb1dcbSChuck Lever const struct nsm_args *argp) 5321da177e4SLinus Torvalds { 53303eb1dcbSChuck Lever struct xdr_stream xdr; 53403eb1dcbSChuck Lever 53503eb1dcbSChuck Lever xdr_init_encode(&xdr, &req->rq_snd_buf, p); 53603eb1dcbSChuck Lever return encode_mon_id(&xdr, argp); 53703eb1dcbSChuck Lever } 53803eb1dcbSChuck Lever 53903eb1dcbSChuck Lever static int xdr_dec_stat_res(struct rpc_rqst *rqstp, __be32 *p, 54003eb1dcbSChuck Lever struct nsm_res *resp) 54103eb1dcbSChuck Lever { 54203eb1dcbSChuck Lever struct xdr_stream xdr; 54303eb1dcbSChuck Lever 54403eb1dcbSChuck Lever xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p); 54503eb1dcbSChuck Lever p = xdr_inline_decode(&xdr, 2 * sizeof(u32)); 54603eb1dcbSChuck Lever if (unlikely(p == NULL)) 54703eb1dcbSChuck Lever return -EIO; 5481da177e4SLinus Torvalds resp->status = ntohl(*p++); 54903eb1dcbSChuck Lever resp->state = ntohl(*p); 55003eb1dcbSChuck Lever 55103eb1dcbSChuck Lever dprintk("lockd: xdr_dec_stat_res status %d state %d\n", 5521da177e4SLinus Torvalds resp->status, resp->state); 5531da177e4SLinus Torvalds return 0; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 55603eb1dcbSChuck Lever static int xdr_dec_stat(struct rpc_rqst *rqstp, __be32 *p, 55703eb1dcbSChuck Lever struct nsm_res *resp) 5581da177e4SLinus Torvalds { 55903eb1dcbSChuck Lever struct xdr_stream xdr; 56003eb1dcbSChuck Lever 56103eb1dcbSChuck Lever xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p); 56203eb1dcbSChuck Lever p = xdr_inline_decode(&xdr, sizeof(u32)); 56303eb1dcbSChuck Lever if (unlikely(p == NULL)) 56403eb1dcbSChuck Lever return -EIO; 56503eb1dcbSChuck Lever resp->state = ntohl(*p); 56603eb1dcbSChuck Lever 56703eb1dcbSChuck Lever dprintk("lockd: xdr_dec_stat state %d\n", resp->state); 5681da177e4SLinus Torvalds return 0; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5722ca7754dSChuck Lever #define SM_my_id_sz (SM_my_name_sz+3) 5732ca7754dSChuck Lever #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5742ca7754dSChuck Lever #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz) 5750490a54aSChuck Lever #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE)) 5760490a54aSChuck Lever #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz) 5771da177e4SLinus Torvalds #define SM_monres_sz 2 5781da177e4SLinus Torvalds #define SM_unmonres_sz 1 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds static struct rpc_procinfo nsm_procedures[] = { 58136e8e668SChuck Lever [NSMPROC_MON] = { 58236e8e668SChuck Lever .p_proc = NSMPROC_MON, 58303eb1dcbSChuck Lever .p_encode = (kxdrproc_t)xdr_enc_mon, 58403eb1dcbSChuck Lever .p_decode = (kxdrproc_t)xdr_dec_stat_res, 5852bea90d4SChuck Lever .p_arglen = SM_mon_sz, 5862bea90d4SChuck Lever .p_replen = SM_monres_sz, 58736e8e668SChuck Lever .p_statidx = NSMPROC_MON, 588cc0175c1SChuck Lever .p_name = "MONITOR", 5891da177e4SLinus Torvalds }, 59036e8e668SChuck Lever [NSMPROC_UNMON] = { 59136e8e668SChuck Lever .p_proc = NSMPROC_UNMON, 59203eb1dcbSChuck Lever .p_encode = (kxdrproc_t)xdr_enc_unmon, 59303eb1dcbSChuck Lever .p_decode = (kxdrproc_t)xdr_dec_stat, 5942bea90d4SChuck Lever .p_arglen = SM_mon_id_sz, 5952bea90d4SChuck Lever .p_replen = SM_unmonres_sz, 59636e8e668SChuck Lever .p_statidx = NSMPROC_UNMON, 597cc0175c1SChuck Lever .p_name = "UNMONITOR", 5981da177e4SLinus Torvalds }, 5991da177e4SLinus Torvalds }; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds static struct rpc_version nsm_version1 = { 6021da177e4SLinus Torvalds .number = 1, 603e8c96f8cSTobias Klauser .nrprocs = ARRAY_SIZE(nsm_procedures), 6041da177e4SLinus Torvalds .procs = nsm_procedures 6051da177e4SLinus Torvalds }; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds static struct rpc_version * nsm_version[] = { 6081da177e4SLinus Torvalds [1] = &nsm_version1, 6091da177e4SLinus Torvalds }; 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds static struct rpc_stat nsm_stats; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds static struct rpc_program nsm_program = { 6141da177e4SLinus Torvalds .name = "statd", 61536e8e668SChuck Lever .number = NSM_PROGRAM, 616e8c96f8cSTobias Klauser .nrvers = ARRAY_SIZE(nsm_version), 6171da177e4SLinus Torvalds .version = nsm_version, 6181da177e4SLinus Torvalds .stats = &nsm_stats 6191da177e4SLinus Torvalds }; 620