blob: 3fc683f46b3e1cd38df9e0da718fa1f4d46f3c8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/mon.c
3 *
4 * The kernel statd client.
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
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>
16
17
18#define NLMDBG_FACILITY NLMDBG_MONITOR
19
20static struct rpc_clnt * nsm_create(void);
21
22static struct rpc_program nsm_program;
23
24/*
25 * Local NSM state
26 */
27u32 nsm_local_state;
28
29/*
30 * Common procedure for SM_MON/SM_UNMON calls
31 */
32static int
33nsm_mon_unmon(struct nlm_host *host, u32 proc, struct nsm_res *res)
34{
35 struct rpc_clnt *clnt;
36 int status;
37 struct nsm_args args;
Chuck Leverdead28d2006-03-20 13:44:23 -050038 struct rpc_message msg = {
39 .rpc_argp = &args,
40 .rpc_resp = res,
41 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 clnt = nsm_create();
44 if (IS_ERR(clnt)) {
45 status = PTR_ERR(clnt);
46 goto out;
47 }
48
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));
55
Chuck Leverdead28d2006-03-20 13:44:23 -050056 msg.rpc_proc = &clnt->cl_procinfo[proc];
57 status = rpc_call_sync(clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (status < 0)
59 printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
60 status);
61 else
62 status = 0;
63 out:
64 return status;
65}
66
67/*
68 * Set up monitoring of a remote host
69 */
70int
71nsm_monitor(struct nlm_host *host)
72{
73 struct nsm_res res;
74 int status;
75
76 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
77
78 status = nsm_mon_unmon(host, SM_MON, &res);
79
80 if (status < 0 || res.status != 0)
81 printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
82 else
83 host->h_monitored = 1;
84 return status;
85}
86
87/*
88 * Cease to monitor remote host
89 */
90int
91nsm_unmonitor(struct nlm_host *host)
92{
93 struct nsm_res res;
94 int status;
95
96 dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
97
98 status = nsm_mon_unmon(host, SM_UNMON, &res);
99 if (status < 0)
100 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", host->h_name);
101 else
102 host->h_monitored = 0;
103 return status;
104}
105
106/*
107 * Create NSM client for the local host
108 */
109static struct rpc_clnt *
110nsm_create(void)
111{
112 struct rpc_xprt *xprt;
113 struct rpc_clnt *clnt;
114 struct sockaddr_in sin;
115
116 sin.sin_family = AF_INET;
117 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
118 sin.sin_port = 0;
119
120 xprt = xprt_create_proto(IPPROTO_UDP, &sin, NULL);
121 if (IS_ERR(xprt))
122 return (struct rpc_clnt *)xprt;
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000123 xprt->resvport = 1; /* NSM requires a reserved port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 clnt = rpc_create_client(xprt, "localhost",
126 &nsm_program, SM_VERSION,
127 RPC_AUTH_NULL);
128 if (IS_ERR(clnt))
Trond Myklebust5b616f52005-06-22 17:16:20 +0000129 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 clnt->cl_softrtry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 clnt->cl_oneshot = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return clnt;
133
Trond Myklebust5b616f52005-06-22 17:16:20 +0000134out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return clnt;
136}
137
138/*
139 * XDR functions for NSM.
140 */
141
142static u32 *
143xdr_encode_common(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
144{
145 char buffer[20];
146
147 /*
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.
152 */
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);
160
161 return p;
162}
163
164static int
165xdr_encode_mon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
166{
167 p = xdr_encode_common(rqstp, p, argp);
168 if (IS_ERR(p))
169 return PTR_ERR(p);
170 *p++ = argp->addr;
171 *p++ = argp->vers;
172 *p++ = argp->proto;
173 *p++ = 0;
174 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
175 return 0;
176}
177
178static int
179xdr_encode_unmon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
180{
181 p = xdr_encode_common(rqstp, p, argp);
182 if (IS_ERR(p))
183 return PTR_ERR(p);
184 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
185 return 0;
186}
187
188static int
189xdr_decode_stat_res(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
190{
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);
195 return 0;
196}
197
198static int
199xdr_decode_stat(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
200{
201 resp->state = ntohl(*p++);
202 return 0;
203}
204
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
211
212#ifndef MAX
213# define MAX(a, b) (((a) > (b))? (a) : (b))
214#endif
215
216static struct rpc_procinfo nsm_procedures[] = {
217[SM_MON] = {
218 .p_proc = SM_MON,
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,
Chuck Levercc0175c2006-03-20 13:44:22 -0500222 .p_statidx = SM_MON,
223 .p_name = "MONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 },
225[SM_UNMON] = {
226 .p_proc = SM_UNMON,
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,
Chuck Levercc0175c2006-03-20 13:44:22 -0500230 .p_statidx = SM_UNMON,
231 .p_name = "UNMONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 },
233};
234
235static struct rpc_version nsm_version1 = {
Tobias Klausere8c96f82006-03-24 03:15:34 -0800236 .number = 1,
237 .nrprocs = ARRAY_SIZE(nsm_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .procs = nsm_procedures
239};
240
241static struct rpc_version * nsm_version[] = {
242 [1] = &nsm_version1,
243};
244
245static struct rpc_stat nsm_stats;
246
247static struct rpc_program nsm_program = {
248 .name = "statd",
249 .number = SM_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800250 .nrvers = ARRAY_SIZE(nsm_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .version = nsm_version,
252 .stats = &nsm_stats
253};