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; 45303a7ce9SStanislav Kinsbursky 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 89a4ee8d97STrond Myklebust static struct rpc_clnt *nsm_client_set(struct lockd_net *ln, 90a4ee8d97STrond Myklebust struct rpc_clnt *clnt) 91a4ee8d97STrond Myklebust { 92a4ee8d97STrond Myklebust spin_lock(&ln->nsm_clnt_lock); 93a4ee8d97STrond Myklebust if (ln->nsm_users == 0) { 94a4ee8d97STrond Myklebust if (clnt == NULL) 95a4ee8d97STrond Myklebust goto out; 96a4ee8d97STrond Myklebust ln->nsm_clnt = clnt; 97a4ee8d97STrond Myklebust } 98a4ee8d97STrond Myklebust clnt = ln->nsm_clnt; 99a4ee8d97STrond Myklebust ln->nsm_users++; 100a4ee8d97STrond Myklebust out: 101a4ee8d97STrond Myklebust spin_unlock(&ln->nsm_clnt_lock); 102a4ee8d97STrond Myklebust return clnt; 103a4ee8d97STrond Myklebust } 104a4ee8d97STrond Myklebust 10503a9a42aSTrond Myklebust static struct rpc_clnt *nsm_client_get(struct net *net, const char *nodename) 106e9406db2SStanislav Kinsbursky { 107a4ee8d97STrond Myklebust struct rpc_clnt *clnt, *new; 108e9406db2SStanislav Kinsbursky struct lockd_net *ln = net_generic(net, lockd_net_id); 109e9406db2SStanislav Kinsbursky 110a4ee8d97STrond Myklebust clnt = nsm_client_set(ln, NULL); 111a4ee8d97STrond Myklebust if (clnt != NULL) 112e9406db2SStanislav Kinsbursky goto out; 113e9406db2SStanislav Kinsbursky 11403a9a42aSTrond Myklebust clnt = new = nsm_create(net, nodename); 115a4ee8d97STrond Myklebust if (IS_ERR(clnt)) 116a4ee8d97STrond Myklebust goto out; 117a4ee8d97STrond Myklebust 118a4ee8d97STrond Myklebust clnt = nsm_client_set(ln, new); 119a4ee8d97STrond Myklebust if (clnt != new) 120a4ee8d97STrond Myklebust rpc_shutdown_client(new); 121e9406db2SStanislav Kinsbursky out: 122e9406db2SStanislav Kinsbursky return clnt; 123e9406db2SStanislav Kinsbursky } 124e9406db2SStanislav Kinsbursky 125cb7323ffSStanislav Kinsbursky static void nsm_client_put(struct net *net) 126e9406db2SStanislav Kinsbursky { 127e9406db2SStanislav Kinsbursky struct lockd_net *ln = net_generic(net, lockd_net_id); 128e498daa8STrond Myklebust struct rpc_clnt *clnt = NULL; 129e9406db2SStanislav Kinsbursky 130e9406db2SStanislav Kinsbursky spin_lock(&ln->nsm_clnt_lock); 131e498daa8STrond Myklebust ln->nsm_users--; 132e498daa8STrond Myklebust if (ln->nsm_users == 0) { 133e498daa8STrond Myklebust clnt = ln->nsm_clnt; 134e9406db2SStanislav Kinsbursky ln->nsm_clnt = NULL; 135e9406db2SStanislav Kinsbursky } 136e9406db2SStanislav Kinsbursky spin_unlock(&ln->nsm_clnt_lock); 137e498daa8STrond Myklebust if (clnt != NULL) 138e9406db2SStanislav Kinsbursky rpc_shutdown_client(clnt); 139e9406db2SStanislav Kinsbursky } 140e9406db2SStanislav Kinsbursky 1410e1cb5c0SStanislav Kinsbursky static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res, 142cb7323ffSStanislav Kinsbursky struct rpc_clnt *clnt) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds int status; 145a4846750SChuck Lever struct nsm_args args = { 146cab2d3c9SChuck Lever .priv = &nsm->sm_priv, 147a4846750SChuck Lever .prog = NLM_PROGRAM, 148a4846750SChuck Lever .vers = 3, 149a4846750SChuck Lever .proc = NLMPROC_NSM_NOTIFY, 15029ed1407SChuck Lever .mon_name = nsm->sm_mon_name, 151cb7323ffSStanislav Kinsbursky .nodename = clnt->cl_nodename, 152a4846750SChuck Lever }; 153dead28daSChuck Lever struct rpc_message msg = { 154dead28daSChuck Lever .rpc_argp = &args, 155dead28daSChuck Lever .rpc_resp = res, 156dead28daSChuck Lever }; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds memset(res, 0, sizeof(*res)); 1591da177e4SLinus Torvalds 160dead28daSChuck Lever msg.rpc_proc = &clnt->cl_procinfo[proc]; 161e9406db2SStanislav Kinsbursky status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN); 162173b3afcSBenjamin Coddington if (status == -ECONNREFUSED) { 163173b3afcSBenjamin Coddington dprintk("lockd: NSM upcall RPC failed, status=%d, forcing rebind\n", 164173b3afcSBenjamin Coddington status); 165173b3afcSBenjamin Coddington rpc_force_rebind(clnt); 166173b3afcSBenjamin Coddington status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFTCONN); 167173b3afcSBenjamin Coddington } 1681da177e4SLinus Torvalds if (status < 0) 1695acf4315SChuck Lever dprintk("lockd: NSM upcall RPC failed, status=%d\n", 1701da177e4SLinus Torvalds status); 1711da177e4SLinus Torvalds else 1721da177e4SLinus Torvalds status = 0; 1731da177e4SLinus Torvalds return status; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761e49323cSChuck Lever /** 1771e49323cSChuck Lever * nsm_monitor - Notify a peer in case we reboot 1781e49323cSChuck Lever * @host: pointer to nlm_host of peer to notify 1791e49323cSChuck Lever * 1801e49323cSChuck Lever * If this peer is not already monitored, this function sends an 1811e49323cSChuck Lever * upcall to the local rpc.statd to record the name/address of 1821e49323cSChuck Lever * the peer to notify in case we reboot. 1831e49323cSChuck Lever * 1841e49323cSChuck Lever * Returns zero if the peer is monitored by the local rpc.statd; 1851e49323cSChuck Lever * otherwise a negative errno value is returned. 1861da177e4SLinus Torvalds */ 1871e49323cSChuck Lever int nsm_monitor(const struct nlm_host *host) 1881da177e4SLinus Torvalds { 1898dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 1901da177e4SLinus Torvalds struct nsm_res res; 1911da177e4SLinus Torvalds int status; 192cb7323ffSStanislav Kinsbursky struct rpc_clnt *clnt; 19303a9a42aSTrond Myklebust const char *nodename = NULL; 1941da177e4SLinus Torvalds 1959fee4902SChuck Lever dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name); 1968dead0dbSOlaf Kirch 1978dead0dbSOlaf Kirch if (nsm->sm_monitored) 198977faf39SOlaf Kirch return 0; 1991da177e4SLinus Torvalds 20003a9a42aSTrond Myklebust if (host->h_rpcclnt) 20103a9a42aSTrond Myklebust nodename = host->h_rpcclnt->cl_nodename; 20203a9a42aSTrond Myklebust 20329ed1407SChuck Lever /* 20429ed1407SChuck Lever * Choose whether to record the caller_name or IP address of 20529ed1407SChuck Lever * this peer in the local rpc.statd's database. 20629ed1407SChuck Lever */ 20729ed1407SChuck Lever nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf; 20829ed1407SChuck Lever 20903a9a42aSTrond Myklebust clnt = nsm_client_get(host->net, nodename); 210cb7323ffSStanislav Kinsbursky if (IS_ERR(clnt)) { 211cb7323ffSStanislav Kinsbursky status = PTR_ERR(clnt); 212cb7323ffSStanislav Kinsbursky dprintk("lockd: failed to create NSM upcall transport, " 213cb7323ffSStanislav Kinsbursky "status=%d, net=%p\n", status, host->net); 214cb7323ffSStanislav Kinsbursky return status; 215cb7323ffSStanislav Kinsbursky } 216cb7323ffSStanislav Kinsbursky 217cb7323ffSStanislav Kinsbursky status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, clnt); 2186c9dc425SChuck Lever if (unlikely(res.status != 0)) 2195d254b11SChuck Lever status = -EIO; 2206c9dc425SChuck Lever if (unlikely(status < 0)) { 2219af94fc4SJeff Layton pr_notice_ratelimited("lockd: cannot monitor %s\n", nsm->sm_name); 2221da177e4SLinus Torvalds return status; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 2256c9dc425SChuck Lever nsm->sm_monitored = 1; 2266c9dc425SChuck Lever if (unlikely(nsm_local_state != res.state)) { 2276c9dc425SChuck Lever nsm_local_state = res.state; 2286c9dc425SChuck Lever dprintk("lockd: NSM state changed to %d\n", nsm_local_state); 2296c9dc425SChuck Lever } 2306c9dc425SChuck Lever return 0; 2316c9dc425SChuck Lever } 2326c9dc425SChuck Lever 233356c3eb4SChuck Lever /** 234356c3eb4SChuck Lever * nsm_unmonitor - Unregister peer notification 235356c3eb4SChuck Lever * @host: pointer to nlm_host of peer to stop monitoring 236356c3eb4SChuck Lever * 237356c3eb4SChuck Lever * If this peer is monitored, this function sends an upcall to 238356c3eb4SChuck Lever * tell the local rpc.statd not to send this peer a notification 239356c3eb4SChuck Lever * when we reboot. 2401da177e4SLinus Torvalds */ 241356c3eb4SChuck Lever void nsm_unmonitor(const struct nlm_host *host) 2421da177e4SLinus Torvalds { 2438dead0dbSOlaf Kirch struct nsm_handle *nsm = host->h_nsmhandle; 2441da177e4SLinus Torvalds struct nsm_res res; 245356c3eb4SChuck Lever int status; 2461da177e4SLinus Torvalds 2479502c522SOlaf Kirch if (atomic_read(&nsm->sm_count) == 1 2489502c522SOlaf Kirch && nsm->sm_monitored && !nsm->sm_sticky) { 249cb7323ffSStanislav Kinsbursky struct lockd_net *ln = net_generic(host->net, lockd_net_id); 250cb7323ffSStanislav Kinsbursky 2519fee4902SChuck Lever dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name); 2529502c522SOlaf Kirch 253cb7323ffSStanislav Kinsbursky status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, ln->nsm_clnt); 2540c7aef45SChuck Lever if (res.status != 0) 2550c7aef45SChuck Lever status = -EIO; 2561da177e4SLinus Torvalds if (status < 0) 2579502c522SOlaf Kirch printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", 2589fee4902SChuck Lever nsm->sm_name); 2599502c522SOlaf Kirch else 2608dead0dbSOlaf Kirch nsm->sm_monitored = 0; 261cb7323ffSStanislav Kinsbursky 262cb7323ffSStanislav Kinsbursky nsm_client_put(host->net); 263977faf39SOlaf Kirch } 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 266*0ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_hostname(const struct list_head *nsm_handles, 267*0ad95472SAndrey Ryabinin const char *hostname, const size_t len) 2683420a8c4SChuck Lever { 2693420a8c4SChuck Lever struct nsm_handle *nsm; 2703420a8c4SChuck Lever 271*0ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link) 2723420a8c4SChuck Lever if (strlen(nsm->sm_name) == len && 2733420a8c4SChuck Lever memcmp(nsm->sm_name, hostname, len) == 0) 2743420a8c4SChuck Lever return nsm; 2753420a8c4SChuck Lever return NULL; 2763420a8c4SChuck Lever } 2773420a8c4SChuck Lever 278*0ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_addr(const struct list_head *nsm_handles, 279*0ad95472SAndrey Ryabinin const struct sockaddr *sap) 28077a3ef33SChuck Lever { 28177a3ef33SChuck Lever struct nsm_handle *nsm; 28277a3ef33SChuck Lever 283*0ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link) 2844516fc04SJeff Layton if (rpc_cmp_addr(nsm_addr(nsm), sap)) 28577a3ef33SChuck Lever return nsm; 28677a3ef33SChuck Lever return NULL; 28777a3ef33SChuck Lever } 28877a3ef33SChuck Lever 289*0ad95472SAndrey Ryabinin static struct nsm_handle *nsm_lookup_priv(const struct list_head *nsm_handles, 290*0ad95472SAndrey Ryabinin const struct nsm_private *priv) 2913420a8c4SChuck Lever { 2923420a8c4SChuck Lever struct nsm_handle *nsm; 2933420a8c4SChuck Lever 294*0ad95472SAndrey Ryabinin list_for_each_entry(nsm, nsm_handles, sm_link) 2953420a8c4SChuck Lever if (memcmp(nsm->sm_priv.data, priv->data, 2963420a8c4SChuck Lever sizeof(priv->data)) == 0) 2973420a8c4SChuck Lever return nsm; 2983420a8c4SChuck Lever return NULL; 2993420a8c4SChuck Lever } 3003420a8c4SChuck Lever 3017e44d3beSChuck Lever /* 3027e44d3beSChuck Lever * Construct a unique cookie to match this nsm_handle to this monitored 3037e44d3beSChuck Lever * host. It is passed to the local rpc.statd via NSMPROC_MON, and 3047e44d3beSChuck Lever * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these 3057e44d3beSChuck Lever * requests. 3067e44d3beSChuck Lever * 30794da7663SChuck Lever * The NSM protocol requires that these cookies be unique while the 30894da7663SChuck Lever * system is running. We prefer a stronger requirement of making them 30994da7663SChuck Lever * unique across reboots. If user space bugs cause a stale cookie to 31094da7663SChuck Lever * be sent to the kernel, it could cause the wrong host to lose its 31194da7663SChuck Lever * lock state if cookies were not unique across reboots. 31294da7663SChuck Lever * 31394da7663SChuck Lever * The cookies are exposed only to local user space via loopback. They 31494da7663SChuck Lever * do not appear on the physical network. If we want greater security 31594da7663SChuck Lever * for some reason, nsm_init_private() could perform a one-way hash to 31694da7663SChuck Lever * obscure the contents of the cookie. 3177e44d3beSChuck Lever */ 3187e44d3beSChuck Lever static void nsm_init_private(struct nsm_handle *nsm) 3197e44d3beSChuck Lever { 32094da7663SChuck Lever u64 *p = (u64 *)&nsm->sm_priv.data; 321ad5b365cSMans Rullgard s64 ns; 32294da7663SChuck Lever 3235eaaed4fSThomas Gleixner ns = ktime_get_ns(); 324ad5b365cSMans Rullgard put_unaligned(ns, p); 325ad5b365cSMans Rullgard put_unaligned((unsigned long)nsm, p + 1); 3267e44d3beSChuck Lever } 3277e44d3beSChuck Lever 328b39b897cSChuck Lever static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap, 329b39b897cSChuck Lever const size_t salen, 330b39b897cSChuck Lever const char *hostname, 331b39b897cSChuck Lever const size_t hostname_len) 332b39b897cSChuck Lever { 333b39b897cSChuck Lever struct nsm_handle *new; 334b39b897cSChuck Lever 335b39b897cSChuck Lever new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL); 336b39b897cSChuck Lever if (unlikely(new == NULL)) 337b39b897cSChuck Lever return NULL; 338b39b897cSChuck Lever 339b39b897cSChuck Lever atomic_set(&new->sm_count, 1); 340b39b897cSChuck Lever new->sm_name = (char *)(new + 1); 341b39b897cSChuck Lever memcpy(nsm_addr(new), sap, salen); 342b39b897cSChuck Lever new->sm_addrlen = salen; 343b39b897cSChuck Lever nsm_init_private(new); 344c15128c5SChuck Lever 345c15128c5SChuck Lever if (rpc_ntop(nsm_addr(new), new->sm_addrbuf, 346c15128c5SChuck Lever sizeof(new->sm_addrbuf)) == 0) 347c15128c5SChuck Lever (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf), 348c15128c5SChuck Lever "unsupported address family"); 349b39b897cSChuck Lever memcpy(new->sm_name, hostname, hostname_len); 350b39b897cSChuck Lever new->sm_name[hostname_len] = '\0'; 351b39b897cSChuck Lever 352b39b897cSChuck Lever return new; 353b39b897cSChuck Lever } 354b39b897cSChuck Lever 35567c6d107SChuck Lever /** 35692fd91b9SChuck Lever * nsm_get_handle - Find or create a cached nsm_handle 357*0ad95472SAndrey Ryabinin * @net: network namespace 35867c6d107SChuck Lever * @sap: pointer to socket address of handle to find 35967c6d107SChuck Lever * @salen: length of socket address 36067c6d107SChuck Lever * @hostname: pointer to C string containing hostname to find 36167c6d107SChuck Lever * @hostname_len: length of C string 36267c6d107SChuck Lever * 36392fd91b9SChuck Lever * Behavior is modulated by the global nsm_use_hostnames variable. 36467c6d107SChuck Lever * 36592fd91b9SChuck Lever * Returns a cached nsm_handle after bumping its ref count, or 36692fd91b9SChuck Lever * returns a fresh nsm_handle if a handle that matches @sap and/or 36792fd91b9SChuck Lever * @hostname cannot be found in the handle cache. Returns NULL if 36892fd91b9SChuck Lever * an error occurs. 36967c6d107SChuck Lever */ 370*0ad95472SAndrey Ryabinin struct nsm_handle *nsm_get_handle(const struct net *net, 371*0ad95472SAndrey Ryabinin const struct sockaddr *sap, 37292fd91b9SChuck Lever const size_t salen, const char *hostname, 37392fd91b9SChuck Lever const size_t hostname_len) 37467c6d107SChuck Lever { 37577a3ef33SChuck Lever struct nsm_handle *cached, *new = NULL; 376*0ad95472SAndrey Ryabinin struct lockd_net *ln = net_generic(net, lockd_net_id); 37767c6d107SChuck Lever 37867c6d107SChuck Lever if (hostname && memchr(hostname, '/', hostname_len) != NULL) { 37967c6d107SChuck Lever if (printk_ratelimit()) { 38067c6d107SChuck Lever printk(KERN_WARNING "Invalid hostname \"%.*s\" " 38167c6d107SChuck Lever "in NFS lock request\n", 38267c6d107SChuck Lever (int)hostname_len, hostname); 38367c6d107SChuck Lever } 38467c6d107SChuck Lever return NULL; 38567c6d107SChuck Lever } 38667c6d107SChuck Lever 38767c6d107SChuck Lever retry: 38867c6d107SChuck Lever spin_lock(&nsm_lock); 38967c6d107SChuck Lever 39077a3ef33SChuck Lever if (nsm_use_hostnames && hostname != NULL) 391*0ad95472SAndrey Ryabinin cached = nsm_lookup_hostname(&ln->nsm_handles, 392*0ad95472SAndrey Ryabinin hostname, hostname_len); 39377a3ef33SChuck Lever else 394*0ad95472SAndrey Ryabinin cached = nsm_lookup_addr(&ln->nsm_handles, sap); 39577a3ef33SChuck Lever 39677a3ef33SChuck Lever if (cached != NULL) { 39777a3ef33SChuck Lever atomic_inc(&cached->sm_count); 39877a3ef33SChuck Lever spin_unlock(&nsm_lock); 39977a3ef33SChuck Lever kfree(new); 40077a3ef33SChuck Lever dprintk("lockd: found nsm_handle for %s (%s), " 40177a3ef33SChuck Lever "cnt %d\n", cached->sm_name, 40277a3ef33SChuck Lever cached->sm_addrbuf, 40377a3ef33SChuck Lever atomic_read(&cached->sm_count)); 40477a3ef33SChuck Lever return cached; 40567c6d107SChuck Lever } 40677a3ef33SChuck Lever 40777a3ef33SChuck Lever if (new != NULL) { 408*0ad95472SAndrey Ryabinin list_add(&new->sm_link, &ln->nsm_handles); 40977a3ef33SChuck Lever spin_unlock(&nsm_lock); 4105cf1c4b1SChuck Lever dprintk("lockd: created nsm_handle for %s (%s)\n", 41177a3ef33SChuck Lever new->sm_name, new->sm_addrbuf); 41277a3ef33SChuck Lever return new; 41367c6d107SChuck Lever } 41477a3ef33SChuck Lever 41567c6d107SChuck Lever spin_unlock(&nsm_lock); 41667c6d107SChuck Lever 41777a3ef33SChuck Lever new = nsm_create_handle(sap, salen, hostname, hostname_len); 41877a3ef33SChuck Lever if (unlikely(new == NULL)) 41967c6d107SChuck Lever return NULL; 42067c6d107SChuck Lever goto retry; 42167c6d107SChuck Lever } 42267c6d107SChuck Lever 42367c6d107SChuck Lever /** 4243420a8c4SChuck Lever * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle 425*0ad95472SAndrey Ryabinin * @net: network namespace 4263420a8c4SChuck Lever * @info: pointer to NLMPROC_SM_NOTIFY arguments 4273420a8c4SChuck Lever * 4287e469af9SJeff Layton * Returns a matching nsm_handle if found in the nsm cache. The returned 4297e469af9SJeff Layton * nsm_handle's reference count is bumped. Otherwise returns NULL if some 4307e469af9SJeff Layton * error occurred. 4313420a8c4SChuck Lever */ 432*0ad95472SAndrey Ryabinin struct nsm_handle *nsm_reboot_lookup(const struct net *net, 433*0ad95472SAndrey Ryabinin const struct nlm_reboot *info) 4343420a8c4SChuck Lever { 4353420a8c4SChuck Lever struct nsm_handle *cached; 436*0ad95472SAndrey Ryabinin struct lockd_net *ln = net_generic(net, lockd_net_id); 4373420a8c4SChuck Lever 4383420a8c4SChuck Lever spin_lock(&nsm_lock); 4393420a8c4SChuck Lever 440*0ad95472SAndrey Ryabinin cached = nsm_lookup_priv(&ln->nsm_handles, &info->priv); 4413420a8c4SChuck Lever if (unlikely(cached == NULL)) { 4423420a8c4SChuck Lever spin_unlock(&nsm_lock); 4433420a8c4SChuck Lever dprintk("lockd: never saw rebooted peer '%.*s' before\n", 4443420a8c4SChuck Lever info->len, info->mon); 4453420a8c4SChuck Lever return cached; 4463420a8c4SChuck Lever } 4473420a8c4SChuck Lever 4483420a8c4SChuck Lever atomic_inc(&cached->sm_count); 4493420a8c4SChuck Lever spin_unlock(&nsm_lock); 4503420a8c4SChuck Lever 4513420a8c4SChuck Lever dprintk("lockd: host %s (%s) rebooted, cnt %d\n", 4523420a8c4SChuck Lever cached->sm_name, cached->sm_addrbuf, 4533420a8c4SChuck Lever atomic_read(&cached->sm_count)); 4543420a8c4SChuck Lever return cached; 4553420a8c4SChuck Lever } 4563420a8c4SChuck Lever 4573420a8c4SChuck Lever /** 45867c6d107SChuck Lever * nsm_release - Release an NSM handle 45967c6d107SChuck Lever * @nsm: pointer to handle to be released 46067c6d107SChuck Lever * 46167c6d107SChuck Lever */ 46267c6d107SChuck Lever void nsm_release(struct nsm_handle *nsm) 46367c6d107SChuck Lever { 46467c6d107SChuck Lever if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) { 46567c6d107SChuck Lever list_del(&nsm->sm_link); 46667c6d107SChuck Lever spin_unlock(&nsm_lock); 4675cf1c4b1SChuck Lever dprintk("lockd: destroyed nsm_handle for %s (%s)\n", 4685cf1c4b1SChuck Lever nsm->sm_name, nsm->sm_addrbuf); 46967c6d107SChuck Lever kfree(nsm); 47067c6d107SChuck Lever } 47167c6d107SChuck Lever } 47267c6d107SChuck Lever 4731da177e4SLinus Torvalds /* 4741da177e4SLinus Torvalds * XDR functions for NSM. 4752ca7754dSChuck Lever * 4762ca7754dSChuck Lever * See http://www.opengroup.org/ for details on the Network 4772ca7754dSChuck Lever * Status Monitor wire protocol. 4781da177e4SLinus Torvalds */ 4791da177e4SLinus Torvalds 48049b17004SChuck Lever static void encode_nsm_string(struct xdr_stream *xdr, const char *string) 481099bd05fSChuck Lever { 48203eb1dcbSChuck Lever const u32 len = strlen(string); 48303eb1dcbSChuck Lever __be32 *p; 484099bd05fSChuck Lever 48549b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + len); 48603eb1dcbSChuck Lever xdr_encode_opaque(p, string, len); 487099bd05fSChuck Lever } 488099bd05fSChuck Lever 48949695174SChuck Lever /* 49049695174SChuck Lever * "mon_name" specifies the host to be monitored. 49149695174SChuck Lever */ 49249b17004SChuck Lever static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp) 49349695174SChuck Lever { 49449b17004SChuck Lever encode_nsm_string(xdr, argp->mon_name); 49549695174SChuck Lever } 49649695174SChuck Lever 497850c95fdSChuck Lever /* 498850c95fdSChuck Lever * The "my_id" argument specifies the hostname and RPC procedure 499850c95fdSChuck Lever * to be called when the status manager receives notification 50036e8e668SChuck Lever * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name" 501850c95fdSChuck Lever * has changed. 502850c95fdSChuck Lever */ 50349b17004SChuck Lever static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp) 504850c95fdSChuck Lever { 50503eb1dcbSChuck Lever __be32 *p; 506850c95fdSChuck Lever 507303a7ce9SStanislav Kinsbursky encode_nsm_string(xdr, argp->nodename); 50849b17004SChuck Lever p = xdr_reserve_space(xdr, 4 + 4 + 4); 50949b17004SChuck Lever *p++ = cpu_to_be32(argp->prog); 51049b17004SChuck Lever *p++ = cpu_to_be32(argp->vers); 51149b17004SChuck Lever *p = cpu_to_be32(argp->proc); 512850c95fdSChuck Lever } 513850c95fdSChuck Lever 514ea72a7f1SChuck Lever /* 515ea72a7f1SChuck Lever * The "mon_id" argument specifies the non-private arguments 51636e8e668SChuck Lever * of an NSMPROC_MON or NSMPROC_UNMON call. 517ea72a7f1SChuck Lever */ 51849b17004SChuck Lever static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp) 519ea72a7f1SChuck Lever { 52049b17004SChuck Lever encode_mon_name(xdr, argp); 52149b17004SChuck Lever encode_my_id(xdr, argp); 522ea72a7f1SChuck Lever } 523ea72a7f1SChuck Lever 5240490a54aSChuck Lever /* 5250490a54aSChuck Lever * The "priv" argument may contain private information required 52636e8e668SChuck Lever * by the NSMPROC_MON call. This information will be supplied in the 52736e8e668SChuck Lever * NLMPROC_SM_NOTIFY call. 5280490a54aSChuck Lever */ 52949b17004SChuck Lever static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp) 5300490a54aSChuck Lever { 53103eb1dcbSChuck Lever __be32 *p; 53203eb1dcbSChuck Lever 53303eb1dcbSChuck Lever p = xdr_reserve_space(xdr, SM_PRIV_SIZE); 534cab2d3c9SChuck Lever xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE); 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5379f06c719SChuck Lever static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr, 53803eb1dcbSChuck Lever const struct nsm_args *argp) 5391da177e4SLinus Torvalds { 5409f06c719SChuck Lever encode_mon_id(xdr, argp); 5419f06c719SChuck Lever encode_priv(xdr, argp); 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 5449f06c719SChuck Lever static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr, 54503eb1dcbSChuck Lever const struct nsm_args *argp) 5461da177e4SLinus Torvalds { 5479f06c719SChuck Lever encode_mon_id(xdr, argp); 54803eb1dcbSChuck Lever } 54903eb1dcbSChuck Lever 550bf269551SChuck Lever static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp, 551bf269551SChuck Lever struct xdr_stream *xdr, 55203eb1dcbSChuck Lever struct nsm_res *resp) 55303eb1dcbSChuck Lever { 554bf269551SChuck Lever __be32 *p; 55503eb1dcbSChuck Lever 556bf269551SChuck Lever p = xdr_inline_decode(xdr, 4 + 4); 55703eb1dcbSChuck Lever if (unlikely(p == NULL)) 55803eb1dcbSChuck Lever return -EIO; 55949b17004SChuck Lever resp->status = be32_to_cpup(p++); 56049b17004SChuck Lever resp->state = be32_to_cpup(p); 56103eb1dcbSChuck Lever 562bf269551SChuck Lever dprintk("lockd: %s status %d state %d\n", 563bf269551SChuck Lever __func__, resp->status, resp->state); 5641da177e4SLinus Torvalds return 0; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 567bf269551SChuck Lever static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp, 568bf269551SChuck Lever struct xdr_stream *xdr, 56903eb1dcbSChuck Lever struct nsm_res *resp) 5701da177e4SLinus Torvalds { 571bf269551SChuck Lever __be32 *p; 57203eb1dcbSChuck Lever 573bf269551SChuck Lever p = xdr_inline_decode(xdr, 4); 57403eb1dcbSChuck Lever if (unlikely(p == NULL)) 57503eb1dcbSChuck Lever return -EIO; 57649b17004SChuck Lever resp->state = be32_to_cpup(p); 57703eb1dcbSChuck Lever 578bf269551SChuck Lever dprintk("lockd: %s state %d\n", __func__, resp->state); 5791da177e4SLinus Torvalds return 0; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5832ca7754dSChuck Lever #define SM_my_id_sz (SM_my_name_sz+3) 5842ca7754dSChuck Lever #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN)) 5852ca7754dSChuck Lever #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz) 5860490a54aSChuck Lever #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE)) 5870490a54aSChuck Lever #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz) 5881da177e4SLinus Torvalds #define SM_monres_sz 2 5891da177e4SLinus Torvalds #define SM_unmonres_sz 1 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds static struct rpc_procinfo nsm_procedures[] = { 59236e8e668SChuck Lever [NSMPROC_MON] = { 59336e8e668SChuck Lever .p_proc = NSMPROC_MON, 5949f06c719SChuck Lever .p_encode = (kxdreproc_t)nsm_xdr_enc_mon, 595bf269551SChuck Lever .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat_res, 5962bea90d4SChuck Lever .p_arglen = SM_mon_sz, 5972bea90d4SChuck Lever .p_replen = SM_monres_sz, 59836e8e668SChuck Lever .p_statidx = NSMPROC_MON, 599cc0175c1SChuck Lever .p_name = "MONITOR", 6001da177e4SLinus Torvalds }, 60136e8e668SChuck Lever [NSMPROC_UNMON] = { 60236e8e668SChuck Lever .p_proc = NSMPROC_UNMON, 6039f06c719SChuck Lever .p_encode = (kxdreproc_t)nsm_xdr_enc_unmon, 604bf269551SChuck Lever .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat, 6052bea90d4SChuck Lever .p_arglen = SM_mon_id_sz, 6062bea90d4SChuck Lever .p_replen = SM_unmonres_sz, 60736e8e668SChuck Lever .p_statidx = NSMPROC_UNMON, 608cc0175c1SChuck Lever .p_name = "UNMONITOR", 6091da177e4SLinus Torvalds }, 6101da177e4SLinus Torvalds }; 6111da177e4SLinus Torvalds 612a613fa16STrond Myklebust static const struct rpc_version nsm_version1 = { 6131da177e4SLinus Torvalds .number = 1, 614e8c96f8cSTobias Klauser .nrprocs = ARRAY_SIZE(nsm_procedures), 6151da177e4SLinus Torvalds .procs = nsm_procedures 6161da177e4SLinus Torvalds }; 6171da177e4SLinus Torvalds 618a613fa16STrond Myklebust static const struct rpc_version *nsm_version[] = { 6191da177e4SLinus Torvalds [1] = &nsm_version1, 6201da177e4SLinus Torvalds }; 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds static struct rpc_stat nsm_stats; 6231da177e4SLinus Torvalds 624a613fa16STrond Myklebust static const struct rpc_program nsm_program = { 6251da177e4SLinus Torvalds .name = "statd", 62636e8e668SChuck Lever .number = NSM_PROGRAM, 627e8c96f8cSTobias Klauser .nrvers = ARRAY_SIZE(nsm_version), 6281da177e4SLinus Torvalds .version = nsm_version, 6291da177e4SLinus Torvalds .stats = &nsm_stats 6301da177e4SLinus Torvalds }; 631