MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / lockd / mon.c
blobe277c689eaf7e0eb4898c3dc03b91a19094902b6
1 /*
2 * linux/fs/lockd/mon.c
4 * The kernel statd client.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
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;
25 * Local NSM state
27 int nsm_local_state;
30 * Common procedure for SM_MON/SM_UNMON calls
32 static int
33 nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
35 struct rpc_clnt *clnt;
36 int status;
37 struct nsm_args args;
38 struct rpc_message msg = {
39 .rpc_argp = &args,
40 .rpc_resp = res,
43 clnt = nsm_create();
44 if (IS_ERR(clnt)) {
45 status = PTR_ERR(clnt);
46 goto out;
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;
53 args.vers = 3;
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);
59 if (status < 0)
60 printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
61 status);
62 else
63 status = 0;
64 out:
65 return status;
69 * Set up monitoring of a remote host
71 int
72 nsm_monitor(struct nlm_host *host)
74 struct nsm_handle *nsm = host->h_nsmhandle;
75 struct nsm_res res;
76 int status;
78 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
79 BUG_ON(nsm == NULL);
81 if (nsm->sm_monitored)
82 return 0;
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);
88 else
89 nsm->sm_monitored = 1;
90 return status;
94 * Cease to monitor remote host
96 int
97 nsm_unmonitor(struct nlm_host *host)
99 struct nsm_handle *nsm = host->h_nsmhandle;
100 struct nsm_res res;
101 int status = 0;
103 if (nsm == NULL)
104 return 0;
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);
112 if (status < 0)
113 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
114 host->h_name);
115 else
116 nsm->sm_monitored = 0;
118 nsm_release(nsm);
119 return status;
123 * Create NSM client for the local host
125 static struct rpc_clnt *
126 nsm_create(void)
128 struct sockaddr_in sin = {
129 .sin_family = AF_INET,
130 #if 0 // mask by Victor Yu. 02-12-2007
131 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
132 .sin_port = 0,
133 #endif
135 struct rpc_create_args args = {
136 .protocol = IPPROTO_UDP,
137 .address = (struct sockaddr *)&sin,
138 .addrsize = sizeof(sin),
139 .servername = "localhost",
140 .program = &nsm_program,
141 .version = SM_VERSION,
142 .authflavor = RPC_AUTH_NULL,
143 .flags = (RPC_CLNT_CREATE_ONESHOT),
146 #if 1 // add by Victor Yu. 02-12-2007
147 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
148 sin.sin_port = 0;
149 #endif
150 return rpc_create(&args);
154 * XDR functions for NSM.
157 static __be32 *
158 xdr_encode_common(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
160 char buffer[20], *name;
163 * Use the dotted-quad IP address of the remote host as
164 * identifier. Linux statd always looks up the canonical
165 * hostname first for whatever remote hostname it receives,
166 * so this works alright.
168 if (nsm_use_hostnames) {
169 name = argp->mon_name;
170 } else {
171 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
172 name = buffer;
174 if (!(p = xdr_encode_string(p, name))
175 || !(p = xdr_encode_string(p, utsname()->nodename)))
176 return ERR_PTR(-EIO);
177 *p++ = htonl(argp->prog);
178 *p++ = htonl(argp->vers);
179 *p++ = htonl(argp->proc);
181 return p;
184 static int
185 xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
187 p = xdr_encode_common(rqstp, p, argp);
188 if (IS_ERR(p))
189 return PTR_ERR(p);
191 /* Surprise - there may even be room for an IPv6 address now */
192 *p++ = argp->addr;
193 *p++ = 0;
194 *p++ = 0;
195 *p++ = 0;
196 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
197 return 0;
200 static int
201 xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
203 p = xdr_encode_common(rqstp, p, argp);
204 if (IS_ERR(p))
205 return PTR_ERR(p);
206 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
207 return 0;
210 static int
211 xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
213 resp->status = ntohl(*p++);
214 resp->state = ntohl(*p++);
215 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
216 resp->status, resp->state);
217 return 0;
220 static int
221 xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
223 resp->state = ntohl(*p++);
224 return 0;
227 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
228 #define SM_my_id_sz (3+1+SM_my_name_sz)
229 #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
230 #define SM_mon_sz (SM_mon_id_sz+4)
231 #define SM_monres_sz 2
232 #define SM_unmonres_sz 1
234 #ifndef MAX
235 # define MAX(a, b) (((a) > (b))? (a) : (b))
236 #endif
238 static struct rpc_procinfo nsm_procedures[] = {
239 [SM_MON] = {
240 .p_proc = SM_MON,
241 .p_encode = (kxdrproc_t) xdr_encode_mon,
242 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
243 .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
244 .p_statidx = SM_MON,
245 .p_name = "MONITOR",
247 [SM_UNMON] = {
248 .p_proc = SM_UNMON,
249 .p_encode = (kxdrproc_t) xdr_encode_unmon,
250 .p_decode = (kxdrproc_t) xdr_decode_stat,
251 .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
252 .p_statidx = SM_UNMON,
253 .p_name = "UNMONITOR",
257 static struct rpc_version nsm_version1 = {
258 .number = 1,
259 .nrprocs = ARRAY_SIZE(nsm_procedures),
260 .procs = nsm_procedures
263 static struct rpc_version * nsm_version[] = {
264 [1] = &nsm_version1,
267 static struct rpc_stat nsm_stats;
269 static struct rpc_program nsm_program = {
270 .name = "statd",
271 .number = SM_PROGRAM,
272 .nrvers = ARRAY_SIZE(nsm_version),
273 .version = nsm_version,
274 .stats = &nsm_stats