blob: 81308832e99427c3f7c43b281aa79163b204b2ff [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
20
Chuck Lever9c1bfd02008-12-05 19:01:59 -050021struct nsm_args {
22 __be32 addr; /* remote address */
23 u32 prog; /* RPC callback info */
24 u32 vers;
25 u32 proc;
26
27 char *mon_name;
28};
29
30struct nsm_res {
31 u32 status;
32 u32 state;
33};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static struct rpc_clnt * nsm_create(void);
36
37static struct rpc_program nsm_program;
38
39/*
40 * Local NSM state
41 */
Olaf Kirch460f5ca2006-10-04 02:16:03 -070042int nsm_local_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * Common procedure for SM_MON/SM_UNMON calls
46 */
47static int
Olaf Kirch9502c522006-10-04 02:15:56 -070048nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct rpc_clnt *clnt;
51 int status;
Chuck Levera4846752008-12-04 14:20:23 -050052 struct nsm_args args = {
53 .addr = nsm_addr_in(nsm)->sin_addr.s_addr,
54 .prog = NLM_PROGRAM,
55 .vers = 3,
56 .proc = NLMPROC_NSM_NOTIFY,
Chuck Lever29ed1402008-12-04 14:20:46 -050057 .mon_name = nsm->sm_mon_name,
Chuck Levera4846752008-12-04 14:20:23 -050058 };
Chuck Leverdead28d2006-03-20 13:44:23 -050059 struct rpc_message msg = {
60 .rpc_argp = &args,
61 .rpc_resp = res,
62 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 clnt = nsm_create();
65 if (IS_ERR(clnt)) {
66 status = PTR_ERR(clnt);
Chuck Lever5acf4312008-12-04 14:20:31 -050067 dprintk("lockd: failed to create NSM upcall transport, "
68 "status=%d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 goto out;
70 }
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 memset(res, 0, sizeof(*res));
73
Chuck Leverdead28d2006-03-20 13:44:23 -050074 msg.rpc_proc = &clnt->cl_procinfo[proc];
75 status = rpc_call_sync(clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (status < 0)
Chuck Lever5acf4312008-12-04 14:20:31 -050077 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
78 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 else
80 status = 0;
Trond Myklebust90c57552007-06-09 19:49:36 -040081 rpc_shutdown_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 out:
83 return status;
84}
85
Chuck Lever1e493232008-12-04 14:21:24 -050086/**
87 * nsm_monitor - Notify a peer in case we reboot
88 * @host: pointer to nlm_host of peer to notify
89 *
90 * If this peer is not already monitored, this function sends an
91 * upcall to the local rpc.statd to record the name/address of
92 * the peer to notify in case we reboot.
93 *
94 * Returns zero if the peer is monitored by the local rpc.statd;
95 * otherwise a negative errno value is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Chuck Lever1e493232008-12-04 14:21:24 -050097int nsm_monitor(const struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Olaf Kirch8dead0d2006-10-04 02:15:53 -070099 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct nsm_res res;
101 int status;
102
Chuck Lever9fee4902008-12-04 14:20:53 -0500103 dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700104
105 if (nsm->sm_monitored)
Olaf Kirch977faf32006-10-04 02:15:51 -0700106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Chuck Lever29ed1402008-12-04 14:20:46 -0500108 /*
109 * Choose whether to record the caller_name or IP address of
110 * this peer in the local rpc.statd's database.
111 */
112 nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
113
Olaf Kirch9502c522006-10-04 02:15:56 -0700114 status = nsm_mon_unmon(nsm, SM_MON, &res);
Chuck Lever5d254b12008-12-04 14:21:15 -0500115 if (res.status != 0)
116 status = -EIO;
117 if (status < 0)
Chuck Lever9fee4902008-12-04 14:20:53 -0500118 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 else
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700120 nsm->sm_monitored = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return status;
122}
123
Chuck Lever356c3eb2008-12-04 14:21:38 -0500124/**
125 * nsm_unmonitor - Unregister peer notification
126 * @host: pointer to nlm_host of peer to stop monitoring
127 *
128 * If this peer is monitored, this function sends an upcall to
129 * tell the local rpc.statd not to send this peer a notification
130 * when we reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
Chuck Lever356c3eb2008-12-04 14:21:38 -0500132void nsm_unmonitor(const struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700134 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct nsm_res res;
Chuck Lever356c3eb2008-12-04 14:21:38 -0500136 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Olaf Kirch9502c522006-10-04 02:15:56 -0700138 if (atomic_read(&nsm->sm_count) == 1
139 && nsm->sm_monitored && !nsm->sm_sticky) {
Chuck Lever9fee4902008-12-04 14:20:53 -0500140 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
Olaf Kirch9502c522006-10-04 02:15:56 -0700141
142 status = nsm_mon_unmon(nsm, SM_UNMON, &res);
Chuck Lever0c7aef42008-12-04 14:21:46 -0500143 if (res.status != 0)
144 status = -EIO;
Olaf Kirch977faf32006-10-04 02:15:51 -0700145 if (status < 0)
Olaf Kirch9502c522006-10-04 02:15:56 -0700146 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
Chuck Lever9fee4902008-12-04 14:20:53 -0500147 nsm->sm_name);
Olaf Kirch9502c522006-10-04 02:15:56 -0700148 else
149 nsm->sm_monitored = 0;
Olaf Kirch977faf32006-10-04 02:15:51 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/*
154 * Create NSM client for the local host
155 */
156static struct rpc_clnt *
157nsm_create(void)
158{
Chuck Levere1ec7892006-08-22 20:06:20 -0400159 struct sockaddr_in sin = {
160 .sin_family = AF_INET,
161 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
162 .sin_port = 0,
163 };
164 struct rpc_create_args args = {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400165 .protocol = XPRT_TRANSPORT_UDP,
Chuck Levere1ec7892006-08-22 20:06:20 -0400166 .address = (struct sockaddr *)&sin,
167 .addrsize = sizeof(sin),
168 .servername = "localhost",
169 .program = &nsm_program,
170 .version = SM_VERSION,
171 .authflavor = RPC_AUTH_NULL,
Chuck Levere1ec7892006-08-22 20:06:20 -0400172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Chuck Levere1ec7892006-08-22 20:06:20 -0400174 return rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177/*
178 * XDR functions for NSM.
Chuck Lever2ca77542008-03-14 14:26:01 -0400179 *
180 * See http://www.opengroup.org/ for details on the Network
181 * Status Monitor wire protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
183
Chuck Lever099bd052008-03-14 14:25:32 -0400184static __be32 *xdr_encode_nsm_string(__be32 *p, char *string)
185{
186 size_t len = strlen(string);
187
188 if (len > SM_MAXSTRLEN)
189 len = SM_MAXSTRLEN;
190 return xdr_encode_opaque(p, string, len);
191}
192
Chuck Lever49695172008-03-14 14:25:39 -0400193/*
194 * "mon_name" specifies the host to be monitored.
Chuck Lever49695172008-03-14 14:25:39 -0400195 */
196static __be32 *xdr_encode_mon_name(__be32 *p, struct nsm_args *argp)
197{
Chuck Lever29ed1402008-12-04 14:20:46 -0500198 return xdr_encode_nsm_string(p, argp->mon_name);
Chuck Lever49695172008-03-14 14:25:39 -0400199}
200
Chuck Lever850c95f2008-03-14 14:25:46 -0400201/*
202 * The "my_id" argument specifies the hostname and RPC procedure
203 * to be called when the status manager receives notification
204 * (via the SM_NOTIFY call) that the state of host "mon_name"
205 * has changed.
206 */
207static __be32 *xdr_encode_my_id(__be32 *p, struct nsm_args *argp)
208{
209 p = xdr_encode_nsm_string(p, utsname()->nodename);
210 if (!p)
211 return ERR_PTR(-EIO);
212
213 *p++ = htonl(argp->prog);
214 *p++ = htonl(argp->vers);
215 *p++ = htonl(argp->proc);
216
217 return p;
218}
219
Chuck Leverea72a7f2008-03-14 14:25:53 -0400220/*
221 * The "mon_id" argument specifies the non-private arguments
222 * of an SM_MON or SM_UNMON call.
223 */
224static __be32 *xdr_encode_mon_id(__be32 *p, struct nsm_args *argp)
225{
226 p = xdr_encode_mon_name(p, argp);
227 if (!p)
228 return ERR_PTR(-EIO);
229
230 return xdr_encode_my_id(p, argp);
231}
232
Chuck Lever0490a542008-03-14 14:26:08 -0400233/*
234 * The "priv" argument may contain private information required
235 * by the SM_MON call. This information will be supplied in the
236 * SM_NOTIFY call.
237 *
238 * Linux provides the raw IP address of the monitored host,
239 * left in network byte order.
240 */
241static __be32 *xdr_encode_priv(__be32 *p, struct nsm_args *argp)
242{
243 *p++ = argp->addr;
244 *p++ = 0;
245 *p++ = 0;
246 *p++ = 0;
247
248 return p;
249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int
Al Viro52921e02006-10-19 23:28:46 -0700252xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Chuck Lever2ca77542008-03-14 14:26:01 -0400254 p = xdr_encode_mon_id(p, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (IS_ERR(p))
256 return PTR_ERR(p);
Olaf Kirch9502c522006-10-04 02:15:56 -0700257
Chuck Lever0490a542008-03-14 14:26:08 -0400258 p = xdr_encode_priv(p, argp);
259 if (IS_ERR(p))
260 return PTR_ERR(p);
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
263 return 0;
264}
265
266static int
Al Viro52921e02006-10-19 23:28:46 -0700267xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Chuck Lever2ca77542008-03-14 14:26:01 -0400269 p = xdr_encode_mon_id(p, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (IS_ERR(p))
271 return PTR_ERR(p);
272 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
273 return 0;
274}
275
276static int
Al Viro52921e02006-10-19 23:28:46 -0700277xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 resp->status = ntohl(*p++);
280 resp->state = ntohl(*p++);
281 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
282 resp->status, resp->state);
283 return 0;
284}
285
286static int
Al Viro52921e02006-10-19 23:28:46 -0700287xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 resp->state = ntohl(*p++);
290 return 0;
291}
292
293#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
Chuck Lever2ca77542008-03-14 14:26:01 -0400294#define SM_my_id_sz (SM_my_name_sz+3)
295#define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
296#define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
Chuck Lever0490a542008-03-14 14:26:08 -0400297#define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
298#define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#define SM_monres_sz 2
300#define SM_unmonres_sz 1
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static struct rpc_procinfo nsm_procedures[] = {
303[SM_MON] = {
304 .p_proc = SM_MON,
305 .p_encode = (kxdrproc_t) xdr_encode_mon,
306 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400307 .p_arglen = SM_mon_sz,
308 .p_replen = SM_monres_sz,
Chuck Levercc0175c2006-03-20 13:44:22 -0500309 .p_statidx = SM_MON,
310 .p_name = "MONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 },
312[SM_UNMON] = {
313 .p_proc = SM_UNMON,
314 .p_encode = (kxdrproc_t) xdr_encode_unmon,
315 .p_decode = (kxdrproc_t) xdr_decode_stat,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400316 .p_arglen = SM_mon_id_sz,
317 .p_replen = SM_unmonres_sz,
Chuck Levercc0175c2006-03-20 13:44:22 -0500318 .p_statidx = SM_UNMON,
319 .p_name = "UNMONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 },
321};
322
323static struct rpc_version nsm_version1 = {
Tobias Klausere8c96f82006-03-24 03:15:34 -0800324 .number = 1,
325 .nrprocs = ARRAY_SIZE(nsm_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .procs = nsm_procedures
327};
328
329static struct rpc_version * nsm_version[] = {
330 [1] = &nsm_version1,
331};
332
333static struct rpc_stat nsm_stats;
334
335static struct rpc_program nsm_program = {
336 .name = "statd",
337 .number = SM_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800338 .nrvers = ARRAY_SIZE(nsm_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 .version = nsm_version,
340 .stats = &nsm_stats
341};