4 * The kernel statd client.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/utsname.h>
11 #include <linux/kernel.h>
12 #include <linux/sunrpc/clnt.h>
13 #include <linux/sunrpc/xprtsock.h>
14 #include <linux/sunrpc/svc.h>
15 #include <linux/lockd/lockd.h>
16 #include <linux/lockd/sm_inter.h>
19 #define NLMDBG_FACILITY NLMDBG_MONITOR
20 #define NSM_PROGRAM 100024
34 __be32 addr; /* remote address */
35 u32 prog; /* RPC callback info */
47 static struct rpc_clnt * nsm_create(void);
49 static struct rpc_program nsm_program;
57 * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
60 nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
62 struct rpc_clnt *clnt;
64 struct nsm_args args = {
65 .addr = nsm_addr_in(nsm)->sin_addr.s_addr,
68 .proc = NLMPROC_NSM_NOTIFY,
69 .mon_name = nsm->sm_mon_name,
71 struct rpc_message msg = {
78 status = PTR_ERR(clnt);
79 dprintk("lockd: failed to create NSM upcall transport, "
80 "status=%d\n", status);
84 memset(res, 0, sizeof(*res));
86 msg.rpc_proc = &clnt->cl_procinfo[proc];
87 status = rpc_call_sync(clnt, &msg, 0);
89 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
93 rpc_shutdown_client(clnt);
99 * nsm_monitor - Notify a peer in case we reboot
100 * @host: pointer to nlm_host of peer to notify
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.
106 * Returns zero if the peer is monitored by the local rpc.statd;
107 * otherwise a negative errno value is returned.
109 int nsm_monitor(const struct nlm_host *host)
111 struct nsm_handle *nsm = host->h_nsmhandle;
115 dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
117 if (nsm->sm_monitored)
121 * Choose whether to record the caller_name or IP address of
122 * this peer in the local rpc.statd's database.
124 nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
126 status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
130 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
132 nsm->sm_monitored = 1;
137 * nsm_unmonitor - Unregister peer notification
138 * @host: pointer to nlm_host of peer to stop monitoring
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
144 void nsm_unmonitor(const struct nlm_host *host)
146 struct nsm_handle *nsm = host->h_nsmhandle;
150 if (atomic_read(&nsm->sm_count) == 1
151 && nsm->sm_monitored && !nsm->sm_sticky) {
152 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
154 status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
158 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
161 nsm->sm_monitored = 0;
166 * Create NSM client for the local host
168 static struct rpc_clnt *
171 struct sockaddr_in sin = {
172 .sin_family = AF_INET,
173 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
176 struct rpc_create_args args = {
177 .protocol = XPRT_TRANSPORT_UDP,
178 .address = (struct sockaddr *)&sin,
179 .addrsize = sizeof(sin),
180 .servername = "localhost",
181 .program = &nsm_program,
182 .version = NSM_VERSION,
183 .authflavor = RPC_AUTH_NULL,
186 return rpc_create(&args);
190 * XDR functions for NSM.
192 * See http://www.opengroup.org/ for details on the Network
193 * Status Monitor wire protocol.
196 static int encode_nsm_string(struct xdr_stream *xdr, const char *string)
198 const u32 len = strlen(string);
201 if (unlikely(len > SM_MAXSTRLEN))
203 p = xdr_reserve_space(xdr, sizeof(u32) + len);
204 if (unlikely(p == NULL))
206 xdr_encode_opaque(p, string, len);
211 * "mon_name" specifies the host to be monitored.
213 static int encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
215 return encode_nsm_string(xdr, argp->mon_name);
219 * The "my_id" argument specifies the hostname and RPC procedure
220 * to be called when the status manager receives notification
221 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
224 static int encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
229 status = encode_nsm_string(xdr, utsname()->nodename);
230 if (unlikely(status != 0))
232 p = xdr_reserve_space(xdr, 3 * sizeof(u32));
233 if (unlikely(p == NULL))
235 *p++ = htonl(argp->prog);
236 *p++ = htonl(argp->vers);
237 *p++ = htonl(argp->proc);
242 * The "mon_id" argument specifies the non-private arguments
243 * of an NSMPROC_MON or NSMPROC_UNMON call.
245 static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
249 status = encode_mon_name(xdr, argp);
250 if (unlikely(status != 0))
252 return encode_my_id(xdr, argp);
256 * The "priv" argument may contain private information required
257 * by the NSMPROC_MON call. This information will be supplied in the
258 * NLMPROC_SM_NOTIFY call.
260 * Linux provides the raw IP address of the monitored host,
261 * left in network byte order.
263 static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
267 p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
268 if (unlikely(p == NULL))
277 static int xdr_enc_mon(struct rpc_rqst *req, __be32 *p,
278 const struct nsm_args *argp)
280 struct xdr_stream xdr;
283 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
284 status = encode_mon_id(&xdr, argp);
285 if (unlikely(status))
287 return encode_priv(&xdr, argp);
290 static int xdr_enc_unmon(struct rpc_rqst *req, __be32 *p,
291 const struct nsm_args *argp)
293 struct xdr_stream xdr;
295 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
296 return encode_mon_id(&xdr, argp);
299 static int xdr_dec_stat_res(struct rpc_rqst *rqstp, __be32 *p,
300 struct nsm_res *resp)
302 struct xdr_stream xdr;
304 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
305 p = xdr_inline_decode(&xdr, 2 * sizeof(u32));
306 if (unlikely(p == NULL))
308 resp->status = ntohl(*p++);
309 resp->state = ntohl(*p);
311 dprintk("lockd: xdr_dec_stat_res status %d state %d\n",
312 resp->status, resp->state);
316 static int xdr_dec_stat(struct rpc_rqst *rqstp, __be32 *p,
317 struct nsm_res *resp)
319 struct xdr_stream xdr;
321 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
322 p = xdr_inline_decode(&xdr, sizeof(u32));
323 if (unlikely(p == NULL))
325 resp->state = ntohl(*p);
327 dprintk("lockd: xdr_dec_stat state %d\n", resp->state);
331 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
332 #define SM_my_id_sz (SM_my_name_sz+3)
333 #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
334 #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
335 #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
336 #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
337 #define SM_monres_sz 2
338 #define SM_unmonres_sz 1
340 static struct rpc_procinfo nsm_procedures[] = {
342 .p_proc = NSMPROC_MON,
343 .p_encode = (kxdrproc_t)xdr_enc_mon,
344 .p_decode = (kxdrproc_t)xdr_dec_stat_res,
345 .p_arglen = SM_mon_sz,
346 .p_replen = SM_monres_sz,
347 .p_statidx = NSMPROC_MON,
351 .p_proc = NSMPROC_UNMON,
352 .p_encode = (kxdrproc_t)xdr_enc_unmon,
353 .p_decode = (kxdrproc_t)xdr_dec_stat,
354 .p_arglen = SM_mon_id_sz,
355 .p_replen = SM_unmonres_sz,
356 .p_statidx = NSMPROC_UNMON,
357 .p_name = "UNMONITOR",
361 static struct rpc_version nsm_version1 = {
363 .nrprocs = ARRAY_SIZE(nsm_procedures),
364 .procs = nsm_procedures
367 static struct rpc_version * nsm_version[] = {
371 static struct rpc_stat nsm_stats;
373 static struct rpc_program nsm_program = {
375 .number = NSM_PROGRAM,
376 .nrvers = ARRAY_SIZE(nsm_version),
377 .version = nsm_version,