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/svc.h>
14 #include <linux/lockd/lockd.h>
15 #include <linux/lockd/sm_inter.h>
18 #define NLMDBG_FACILITY NLMDBG_MONITOR
20 static struct rpc_clnt * nsm_create(void);
22 static struct rpc_program nsm_program;
30 * Common procedure for SM_MON/SM_UNMON calls
33 nsm_mon_unmon(struct nlm_host *host, u32 proc, struct nsm_res *res)
35 struct rpc_clnt *clnt;
38 struct rpc_message msg = {
45 status = PTR_ERR(clnt);
49 args.addr = host->h_addr.sin_addr.s_addr;
50 args.proto= (host->h_proto<<1) | host->h_server;
51 args.prog = NLM_PROGRAM;
52 args.vers = host->h_version;
53 args.proc = NLMPROC_NSM_NOTIFY;
54 memset(res, 0, sizeof(*res));
56 msg.rpc_proc = &clnt->cl_procinfo[proc];
57 status = rpc_call_sync(clnt, &msg, 0);
59 printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
68 * Set up monitoring of a remote host
71 nsm_monitor(struct nlm_host *host)
76 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
78 status = nsm_mon_unmon(host, SM_MON, &res);
80 if (status < 0 || res.status != 0)
81 printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
83 host->h_monitored = 1;
88 * Cease to monitor remote host
91 nsm_unmonitor(struct nlm_host *host)
96 dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
98 status = nsm_mon_unmon(host, SM_UNMON, &res);
100 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", host->h_name);
102 host->h_monitored = 0;
107 * Create NSM client for the local host
109 static struct rpc_clnt *
112 struct rpc_xprt *xprt;
113 struct rpc_clnt *clnt;
114 struct sockaddr_in sin;
116 sin.sin_family = AF_INET;
117 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
120 xprt = xprt_create_proto(IPPROTO_UDP, &sin, NULL);
122 return (struct rpc_clnt *)xprt;
123 xprt->resvport = 1; /* NSM requires a reserved port */
125 clnt = rpc_create_client(xprt, "localhost",
126 &nsm_program, SM_VERSION,
130 clnt->cl_softrtry = 1;
131 clnt->cl_oneshot = 1;
139 * XDR functions for NSM.
143 xdr_encode_common(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
148 * Use the dotted-quad IP address of the remote host as
149 * identifier. Linux statd always looks up the canonical
150 * hostname first for whatever remote hostname it receives,
151 * so this works alright.
153 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
154 if (!(p = xdr_encode_string(p, buffer))
155 || !(p = xdr_encode_string(p, system_utsname.nodename)))
156 return ERR_PTR(-EIO);
157 *p++ = htonl(argp->prog);
158 *p++ = htonl(argp->vers);
159 *p++ = htonl(argp->proc);
165 xdr_encode_mon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
167 p = xdr_encode_common(rqstp, p, argp);
174 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
179 xdr_encode_unmon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
181 p = xdr_encode_common(rqstp, p, argp);
184 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
189 xdr_decode_stat_res(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
191 resp->status = ntohl(*p++);
192 resp->state = ntohl(*p++);
193 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
194 resp->status, resp->state);
199 xdr_decode_stat(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
201 resp->state = ntohl(*p++);
205 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
206 #define SM_my_id_sz (3+1+SM_my_name_sz)
207 #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
208 #define SM_mon_sz (SM_mon_id_sz+4)
209 #define SM_monres_sz 2
210 #define SM_unmonres_sz 1
213 # define MAX(a, b) (((a) > (b))? (a) : (b))
216 static struct rpc_procinfo nsm_procedures[] = {
219 .p_encode = (kxdrproc_t) xdr_encode_mon,
220 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
221 .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
227 .p_encode = (kxdrproc_t) xdr_encode_unmon,
228 .p_decode = (kxdrproc_t) xdr_decode_stat,
229 .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
230 .p_statidx = SM_UNMON,
231 .p_name = "UNMONITOR",
235 static struct rpc_version nsm_version1 = {
237 .nrprocs = ARRAY_SIZE(nsm_procedures),
238 .procs = nsm_procedures
241 static struct rpc_version * nsm_version[] = {
245 static struct rpc_stat nsm_stats;
247 static struct rpc_program nsm_program = {
249 .number = SM_PROGRAM,
250 .nrvers = ARRAY_SIZE(nsm_version),
251 .version = nsm_version,