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 nsm_handle *nsm, u32 proc, struct nsm_res *res)
35 struct rpc_clnt *clnt;
38 struct rpc_message msg = {
45 status = PTR_ERR(clnt);
49 memset(&args, 0, sizeof(args));
50 args.mon_name = nsm->sm_name;
51 args.addr = nsm->sm_addr.sin_addr.s_addr;
52 args.prog = NLM_PROGRAM;
54 args.proc = NLMPROC_NSM_NOTIFY;
55 memset(res, 0, sizeof(*res));
57 msg.rpc_proc = &clnt->cl_procinfo[proc];
58 status = rpc_call_sync(clnt, &msg, 0);
60 printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
69 * Set up monitoring of a remote host
72 nsm_monitor(struct nlm_host *host)
74 struct nsm_handle *nsm = host->h_nsmhandle;
78 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
81 if (nsm->sm_monitored)
84 status = nsm_mon_unmon(nsm, SM_MON, &res);
86 if (status < 0 || res.status != 0)
87 printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
89 nsm->sm_monitored = 1;
94 * Cease to monitor remote host
97 nsm_unmonitor(struct nlm_host *host)
99 struct nsm_handle *nsm = host->h_nsmhandle;
105 host->h_nsmhandle = NULL;
107 if (atomic_read(&nsm->sm_count) == 1
108 && nsm->sm_monitored && !nsm->sm_sticky) {
109 dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
111 status = nsm_mon_unmon(nsm, SM_UNMON, &res);
113 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
116 nsm->sm_monitored = 0;
123 * Create NSM client for the local host
125 static struct rpc_clnt *
128 struct sockaddr_in sin = {
129 .sin_family = AF_INET,
130 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
133 struct rpc_create_args args = {
134 .protocol = IPPROTO_UDP,
135 .address = (struct sockaddr *)&sin,
136 .addrsize = sizeof(sin),
137 .servername = "localhost",
138 .program = &nsm_program,
139 .version = SM_VERSION,
140 .authflavor = RPC_AUTH_NULL,
141 .flags = (RPC_CLNT_CREATE_ONESHOT),
144 return rpc_create(&args);
148 * XDR functions for NSM.
152 xdr_encode_common(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
154 char buffer[20], *name;
157 * Use the dotted-quad IP address of the remote host as
158 * identifier. Linux statd always looks up the canonical
159 * hostname first for whatever remote hostname it receives,
160 * so this works alright.
162 if (nsm_use_hostnames) {
163 name = argp->mon_name;
165 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
168 if (!(p = xdr_encode_string(p, name))
169 || !(p = xdr_encode_string(p, utsname()->nodename)))
170 return ERR_PTR(-EIO);
171 *p++ = htonl(argp->prog);
172 *p++ = htonl(argp->vers);
173 *p++ = htonl(argp->proc);
179 xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
181 p = xdr_encode_common(rqstp, p, argp);
185 /* Surprise - there may even be room for an IPv6 address now */
190 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
195 xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
197 p = xdr_encode_common(rqstp, p, argp);
200 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
205 xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
207 resp->status = ntohl(*p++);
208 resp->state = ntohl(*p++);
209 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
210 resp->status, resp->state);
215 xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
217 resp->state = ntohl(*p++);
221 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
222 #define SM_my_id_sz (3+1+SM_my_name_sz)
223 #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
224 #define SM_mon_sz (SM_mon_id_sz+4)
225 #define SM_monres_sz 2
226 #define SM_unmonres_sz 1
229 # define MAX(a, b) (((a) > (b))? (a) : (b))
232 static struct rpc_procinfo nsm_procedures[] = {
235 .p_encode = (kxdrproc_t) xdr_encode_mon,
236 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
237 .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
243 .p_encode = (kxdrproc_t) xdr_encode_unmon,
244 .p_decode = (kxdrproc_t) xdr_decode_stat,
245 .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
246 .p_statidx = SM_UNMON,
247 .p_name = "UNMONITOR",
251 static struct rpc_version nsm_version1 = {
253 .nrprocs = ARRAY_SIZE(nsm_procedures),
254 .procs = nsm_procedures
257 static struct rpc_version * nsm_version[] = {
261 static struct rpc_stat nsm_stats;
263 static struct rpc_program nsm_program = {
265 .number = SM_PROGRAM,
266 .nrvers = ARRAY_SIZE(nsm_version),
267 .version = nsm_version,