blob: 0fc9836db4e7d17a53eed97fb6920c713be02ed9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/mon.c
3 *
4 * The kernel statd client.
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
9#include <linux/types.h>
10#include <linux/utsname.h>
11#include <linux/kernel.h>
12#include <linux/sunrpc/clnt.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040013#include <linux/sunrpc/xprtsock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sunrpc/svc.h>
15#include <linux/lockd/lockd.h>
16#include <linux/lockd/sm_inter.h>
17
18
19#define NLMDBG_FACILITY NLMDBG_MONITOR
Chuck Lever36e8e662008-12-05 19:02:07 -050020#define NSM_PROGRAM 100024
21#define NSM_VERSION 1
22
23enum {
24 NSMPROC_NULL,
25 NSMPROC_STAT,
26 NSMPROC_MON,
27 NSMPROC_UNMON,
28 NSMPROC_UNMON_ALL,
29 NSMPROC_SIMU_CRASH,
30 NSMPROC_NOTIFY,
31};
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Chuck Lever9c1bfd02008-12-05 19:01:59 -050033struct nsm_args {
34 __be32 addr; /* remote address */
35 u32 prog; /* RPC callback info */
36 u32 vers;
37 u32 proc;
38
39 char *mon_name;
40};
41
42struct nsm_res {
43 u32 status;
44 u32 state;
45};
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static struct rpc_clnt * nsm_create(void);
48
49static struct rpc_program nsm_program;
50
51/*
52 * Local NSM state
53 */
Olaf Kirch460f5ca2006-10-04 02:16:03 -070054int nsm_local_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
Chuck Lever36e8e662008-12-05 19:02:07 -050057 * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59static int
Olaf Kirch9502c522006-10-04 02:15:56 -070060nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 struct rpc_clnt *clnt;
63 int status;
Chuck Levera4846752008-12-04 14:20:23 -050064 struct nsm_args args = {
65 .addr = nsm_addr_in(nsm)->sin_addr.s_addr,
66 .prog = NLM_PROGRAM,
67 .vers = 3,
68 .proc = NLMPROC_NSM_NOTIFY,
Chuck Lever29ed1402008-12-04 14:20:46 -050069 .mon_name = nsm->sm_mon_name,
Chuck Levera4846752008-12-04 14:20:23 -050070 };
Chuck Leverdead28d2006-03-20 13:44:23 -050071 struct rpc_message msg = {
72 .rpc_argp = &args,
73 .rpc_resp = res,
74 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 clnt = nsm_create();
77 if (IS_ERR(clnt)) {
78 status = PTR_ERR(clnt);
Chuck Lever5acf4312008-12-04 14:20:31 -050079 dprintk("lockd: failed to create NSM upcall transport, "
80 "status=%d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 goto out;
82 }
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 memset(res, 0, sizeof(*res));
85
Chuck Leverdead28d2006-03-20 13:44:23 -050086 msg.rpc_proc = &clnt->cl_procinfo[proc];
87 status = rpc_call_sync(clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (status < 0)
Chuck Lever5acf4312008-12-04 14:20:31 -050089 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
90 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 else
92 status = 0;
Trond Myklebust90c57552007-06-09 19:49:36 -040093 rpc_shutdown_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 out:
95 return status;
96}
97
Chuck Lever1e49323c2008-12-04 14:21:24 -050098/**
99 * nsm_monitor - Notify a peer in case we reboot
100 * @host: pointer to nlm_host of peer to notify
101 *
102 * If this peer is not already monitored, this function sends an
103 * upcall to the local rpc.statd to record the name/address of
104 * the peer to notify in case we reboot.
105 *
106 * Returns zero if the peer is monitored by the local rpc.statd;
107 * otherwise a negative errno value is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Chuck Lever1e49323c2008-12-04 14:21:24 -0500109int nsm_monitor(const struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700111 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct nsm_res res;
113 int status;
114
Chuck Lever9fee4902008-12-04 14:20:53 -0500115 dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700116
117 if (nsm->sm_monitored)
Olaf Kirch977faf32006-10-04 02:15:51 -0700118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Chuck Lever29ed1402008-12-04 14:20:46 -0500120 /*
121 * Choose whether to record the caller_name or IP address of
122 * this peer in the local rpc.statd's database.
123 */
124 nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
125
Chuck Lever36e8e662008-12-05 19:02:07 -0500126 status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
Chuck Lever5d254b12008-12-04 14:21:15 -0500127 if (res.status != 0)
128 status = -EIO;
129 if (status < 0)
Chuck Lever9fee4902008-12-04 14:20:53 -0500130 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 else
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700132 nsm->sm_monitored = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return status;
134}
135
Chuck Lever356c3eb2008-12-04 14:21:38 -0500136/**
137 * nsm_unmonitor - Unregister peer notification
138 * @host: pointer to nlm_host of peer to stop monitoring
139 *
140 * If this peer is monitored, this function sends an upcall to
141 * tell the local rpc.statd not to send this peer a notification
142 * when we reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
Chuck Lever356c3eb2008-12-04 14:21:38 -0500144void nsm_unmonitor(const struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700146 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct nsm_res res;
Chuck Lever356c3eb2008-12-04 14:21:38 -0500148 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Olaf Kirch9502c522006-10-04 02:15:56 -0700150 if (atomic_read(&nsm->sm_count) == 1
151 && nsm->sm_monitored && !nsm->sm_sticky) {
Chuck Lever9fee4902008-12-04 14:20:53 -0500152 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
Olaf Kirch9502c522006-10-04 02:15:56 -0700153
Chuck Lever36e8e662008-12-05 19:02:07 -0500154 status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
Chuck Lever0c7aef42008-12-04 14:21:46 -0500155 if (res.status != 0)
156 status = -EIO;
Olaf Kirch977faf32006-10-04 02:15:51 -0700157 if (status < 0)
Olaf Kirch9502c522006-10-04 02:15:56 -0700158 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
Chuck Lever9fee4902008-12-04 14:20:53 -0500159 nsm->sm_name);
Olaf Kirch9502c522006-10-04 02:15:56 -0700160 else
161 nsm->sm_monitored = 0;
Olaf Kirch977faf32006-10-04 02:15:51 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165/*
166 * Create NSM client for the local host
167 */
168static struct rpc_clnt *
169nsm_create(void)
170{
Chuck Levere1ec7892006-08-22 20:06:20 -0400171 struct sockaddr_in sin = {
172 .sin_family = AF_INET,
173 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
174 .sin_port = 0,
175 };
176 struct rpc_create_args args = {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400177 .protocol = XPRT_TRANSPORT_UDP,
Chuck Levere1ec7892006-08-22 20:06:20 -0400178 .address = (struct sockaddr *)&sin,
179 .addrsize = sizeof(sin),
180 .servername = "localhost",
181 .program = &nsm_program,
Chuck Lever36e8e662008-12-05 19:02:07 -0500182 .version = NSM_VERSION,
Chuck Levere1ec7892006-08-22 20:06:20 -0400183 .authflavor = RPC_AUTH_NULL,
Chuck Levere1ec7892006-08-22 20:06:20 -0400184 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Chuck Levere1ec7892006-08-22 20:06:20 -0400186 return rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189/*
190 * XDR functions for NSM.
Chuck Lever2ca77542008-03-14 14:26:01 -0400191 *
192 * See http://www.opengroup.org/ for details on the Network
193 * Status Monitor wire protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
195
Chuck Lever099bd052008-03-14 14:25:32 -0400196static __be32 *xdr_encode_nsm_string(__be32 *p, char *string)
197{
198 size_t len = strlen(string);
199
200 if (len > SM_MAXSTRLEN)
201 len = SM_MAXSTRLEN;
202 return xdr_encode_opaque(p, string, len);
203}
204
Chuck Lever49695172008-03-14 14:25:39 -0400205/*
206 * "mon_name" specifies the host to be monitored.
Chuck Lever49695172008-03-14 14:25:39 -0400207 */
208static __be32 *xdr_encode_mon_name(__be32 *p, struct nsm_args *argp)
209{
Chuck Lever29ed1402008-12-04 14:20:46 -0500210 return xdr_encode_nsm_string(p, argp->mon_name);
Chuck Lever49695172008-03-14 14:25:39 -0400211}
212
Chuck Lever850c95f2008-03-14 14:25:46 -0400213/*
214 * The "my_id" argument specifies the hostname and RPC procedure
215 * to be called when the status manager receives notification
Chuck Lever36e8e662008-12-05 19:02:07 -0500216 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
Chuck Lever850c95f2008-03-14 14:25:46 -0400217 * has changed.
218 */
219static __be32 *xdr_encode_my_id(__be32 *p, struct nsm_args *argp)
220{
221 p = xdr_encode_nsm_string(p, utsname()->nodename);
222 if (!p)
223 return ERR_PTR(-EIO);
224
225 *p++ = htonl(argp->prog);
226 *p++ = htonl(argp->vers);
227 *p++ = htonl(argp->proc);
228
229 return p;
230}
231
Chuck Leverea72a7f2008-03-14 14:25:53 -0400232/*
233 * The "mon_id" argument specifies the non-private arguments
Chuck Lever36e8e662008-12-05 19:02:07 -0500234 * of an NSMPROC_MON or NSMPROC_UNMON call.
Chuck Leverea72a7f2008-03-14 14:25:53 -0400235 */
236static __be32 *xdr_encode_mon_id(__be32 *p, struct nsm_args *argp)
237{
238 p = xdr_encode_mon_name(p, argp);
239 if (!p)
240 return ERR_PTR(-EIO);
241
242 return xdr_encode_my_id(p, argp);
243}
244
Chuck Lever0490a542008-03-14 14:26:08 -0400245/*
246 * The "priv" argument may contain private information required
Chuck Lever36e8e662008-12-05 19:02:07 -0500247 * by the NSMPROC_MON call. This information will be supplied in the
248 * NLMPROC_SM_NOTIFY call.
Chuck Lever0490a542008-03-14 14:26:08 -0400249 *
250 * Linux provides the raw IP address of the monitored host,
251 * left in network byte order.
252 */
253static __be32 *xdr_encode_priv(__be32 *p, struct nsm_args *argp)
254{
255 *p++ = argp->addr;
256 *p++ = 0;
257 *p++ = 0;
258 *p++ = 0;
259
260 return p;
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static int
Al Viro52921e02006-10-19 23:28:46 -0700264xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Chuck Lever2ca77542008-03-14 14:26:01 -0400266 p = xdr_encode_mon_id(p, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (IS_ERR(p))
268 return PTR_ERR(p);
Olaf Kirch9502c522006-10-04 02:15:56 -0700269
Chuck Lever0490a542008-03-14 14:26:08 -0400270 p = xdr_encode_priv(p, argp);
271 if (IS_ERR(p))
272 return PTR_ERR(p);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
275 return 0;
276}
277
278static int
Al Viro52921e02006-10-19 23:28:46 -0700279xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Chuck Lever2ca77542008-03-14 14:26:01 -0400281 p = xdr_encode_mon_id(p, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (IS_ERR(p))
283 return PTR_ERR(p);
284 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
285 return 0;
286}
287
288static int
Al Viro52921e02006-10-19 23:28:46 -0700289xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 resp->status = ntohl(*p++);
292 resp->state = ntohl(*p++);
293 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
294 resp->status, resp->state);
295 return 0;
296}
297
298static int
Al Viro52921e02006-10-19 23:28:46 -0700299xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 resp->state = ntohl(*p++);
302 return 0;
303}
304
305#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
Chuck Lever2ca77542008-03-14 14:26:01 -0400306#define SM_my_id_sz (SM_my_name_sz+3)
307#define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
308#define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
Chuck Lever0490a542008-03-14 14:26:08 -0400309#define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
310#define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311#define SM_monres_sz 2
312#define SM_unmonres_sz 1
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static struct rpc_procinfo nsm_procedures[] = {
Chuck Lever36e8e662008-12-05 19:02:07 -0500315[NSMPROC_MON] = {
316 .p_proc = NSMPROC_MON,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .p_encode = (kxdrproc_t) xdr_encode_mon,
318 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400319 .p_arglen = SM_mon_sz,
320 .p_replen = SM_monres_sz,
Chuck Lever36e8e662008-12-05 19:02:07 -0500321 .p_statidx = NSMPROC_MON,
Chuck Levercc0175c2006-03-20 13:44:22 -0500322 .p_name = "MONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 },
Chuck Lever36e8e662008-12-05 19:02:07 -0500324[NSMPROC_UNMON] = {
325 .p_proc = NSMPROC_UNMON,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .p_encode = (kxdrproc_t) xdr_encode_unmon,
327 .p_decode = (kxdrproc_t) xdr_decode_stat,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400328 .p_arglen = SM_mon_id_sz,
329 .p_replen = SM_unmonres_sz,
Chuck Lever36e8e662008-12-05 19:02:07 -0500330 .p_statidx = NSMPROC_UNMON,
Chuck Levercc0175c2006-03-20 13:44:22 -0500331 .p_name = "UNMONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 },
333};
334
335static struct rpc_version nsm_version1 = {
Tobias Klausere8c96f82006-03-24 03:15:34 -0800336 .number = 1,
337 .nrprocs = ARRAY_SIZE(nsm_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .procs = nsm_procedures
339};
340
341static struct rpc_version * nsm_version[] = {
342 [1] = &nsm_version1,
343};
344
345static struct rpc_stat nsm_stats;
346
347static struct rpc_program nsm_program = {
348 .name = "statd",
Chuck Lever36e8e662008-12-05 19:02:07 -0500349 .number = NSM_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800350 .nrvers = ARRAY_SIZE(nsm_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 .version = nsm_version,
352 .stats = &nsm_stats
353};