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 extern 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;
41 status = PTR_ERR(clnt);
45 args.addr = host->h_addr.sin_addr.s_addr;
46 args.proto= (host->h_proto<<1) | host->h_server;
47 args.prog = NLM_PROGRAM;
48 args.vers = host->h_version;
49 args.proc = NLMPROC_NSM_NOTIFY;
50 memset(res, 0, sizeof(*res));
52 status = rpc_call(clnt, proc, &args, res, 0);
54 printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
63 * Set up monitoring of a remote host
66 nsm_monitor(struct nlm_host *host)
71 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
73 status = nsm_mon_unmon(host, SM_MON, &res);
75 if (status < 0 || res.status != 0)
76 printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
78 host->h_monitored = 1;
83 * Cease to monitor remote host
86 nsm_unmonitor(struct nlm_host *host)
91 dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
93 status = nsm_mon_unmon(host, SM_UNMON, &res);
95 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", host->h_name);
97 host->h_monitored = 0;
102 * Create NSM client for the local host
104 static struct rpc_clnt *
107 struct rpc_xprt *xprt;
108 struct rpc_clnt *clnt;
109 struct sockaddr_in sin;
111 sin.sin_family = AF_INET;
112 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
115 xprt = xprt_create_proto(IPPROTO_UDP, &sin, NULL);
117 return (struct rpc_clnt *)xprt;
119 clnt = rpc_create_client(xprt, "localhost",
120 &nsm_program, SM_VERSION,
124 clnt->cl_softrtry = 1;
126 clnt->cl_oneshot = 1;
127 xprt->resvport = 1; /* NSM requires a reserved port */
136 * XDR functions for NSM.
140 xdr_encode_common(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
143 u32 addr = ntohl(argp->addr);
146 * Use the dotted-quad IP address of the remote host as
147 * identifier. Linux statd always looks up the canonical
148 * hostname first for whatever remote hostname it receives,
149 * so this works alright.
151 sprintf(buffer, "%d.%d.%d.%d", (addr>>24) & 0xff, (addr>>16) & 0xff,
152 (addr>>8) & 0xff, (addr) & 0xff);
153 if (!(p = xdr_encode_string(p, buffer))
154 || !(p = xdr_encode_string(p, system_utsname.nodename)))
155 return ERR_PTR(-EIO);
156 *p++ = htonl(argp->prog);
157 *p++ = htonl(argp->vers);
158 *p++ = htonl(argp->proc);
164 xdr_encode_mon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
166 p = xdr_encode_common(rqstp, p, argp);
173 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
178 xdr_encode_unmon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
180 p = xdr_encode_common(rqstp, p, argp);
183 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
188 xdr_decode_stat_res(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
190 resp->status = ntohl(*p++);
191 resp->state = ntohl(*p++);
192 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
193 resp->status, resp->state);
198 xdr_decode_stat(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
200 resp->state = ntohl(*p++);
204 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
205 #define SM_my_id_sz (3+1+SM_my_name_sz)
206 #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
207 #define SM_mon_sz (SM_mon_id_sz+4)
208 #define SM_monres_sz 2
209 #define SM_unmonres_sz 1
212 # define MAX(a, b) (((a) > (b))? (a) : (b))
215 static struct rpc_procinfo nsm_procedures[] = {
218 .p_encode = (kxdrproc_t) xdr_encode_mon,
219 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
220 .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
224 .p_encode = (kxdrproc_t) xdr_encode_unmon,
225 .p_decode = (kxdrproc_t) xdr_decode_stat,
226 .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
230 static struct rpc_version nsm_version1 = {
232 .nrprocs = sizeof(nsm_procedures)/sizeof(nsm_procedures[0]),
233 .procs = nsm_procedures
236 static struct rpc_version * nsm_version[] = {
240 static struct rpc_stat nsm_stats;
242 struct rpc_program nsm_program = {
244 .number = SM_PROGRAM,
245 .nrvers = sizeof(nsm_version)/sizeof(nsm_version[0]),
246 .version = nsm_version,