blob: 81b5e7778d70c88e8bc4359ec5595598a80c9e07 [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;
38
39 clnt = nsm_create();
40 if (IS_ERR(clnt)) {
41 status = PTR_ERR(clnt);
42 goto out;
43 }
44
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));
51
52 status = rpc_call(clnt, proc, &args, res, 0);
53 if (status < 0)
54 printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
55 status);
56 else
57 status = 0;
58 out:
59 return status;
60}
61
62/*
63 * Set up monitoring of a remote host
64 */
65int
66nsm_monitor(struct nlm_host *host)
67{
68 struct nsm_res res;
69 int status;
70
71 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
72
73 status = nsm_mon_unmon(host, SM_MON, &res);
74
75 if (status < 0 || res.status != 0)
76 printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
77 else
78 host->h_monitored = 1;
79 return status;
80}
81
82/*
83 * Cease to monitor remote host
84 */
85int
86nsm_unmonitor(struct nlm_host *host)
87{
88 struct nsm_res res;
89 int status;
90
91 dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
92
93 status = nsm_mon_unmon(host, SM_UNMON, &res);
94 if (status < 0)
95 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", host->h_name);
96 else
97 host->h_monitored = 0;
98 return status;
99}
100
101/*
102 * Create NSM client for the local host
103 */
104static struct rpc_clnt *
105nsm_create(void)
106{
107 struct rpc_xprt *xprt;
108 struct rpc_clnt *clnt;
109 struct sockaddr_in sin;
110
111 sin.sin_family = AF_INET;
112 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
113 sin.sin_port = 0;
114
115 xprt = xprt_create_proto(IPPROTO_UDP, &sin, NULL);
116 if (IS_ERR(xprt))
117 return (struct rpc_clnt *)xprt;
118
119 clnt = rpc_create_client(xprt, "localhost",
120 &nsm_program, SM_VERSION,
121 RPC_AUTH_NULL);
122 if (IS_ERR(clnt))
Trond Myklebust5b616f52005-06-22 17:16:20 +0000123 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 clnt->cl_softrtry = 1;
125 clnt->cl_chatty = 1;
126 clnt->cl_oneshot = 1;
127 xprt->resvport = 1; /* NSM requires a reserved port */
128 return clnt;
129
Trond Myklebust5b616f52005-06-22 17:16:20 +0000130out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return clnt;
132}
133
134/*
135 * XDR functions for NSM.
136 */
137
138static u32 *
139xdr_encode_common(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
140{
141 char buffer[20];
142
143 /*
144 * Use the dotted-quad IP address of the remote host as
145 * identifier. Linux statd always looks up the canonical
146 * hostname first for whatever remote hostname it receives,
147 * so this works alright.
148 */
149 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
150 if (!(p = xdr_encode_string(p, buffer))
151 || !(p = xdr_encode_string(p, system_utsname.nodename)))
152 return ERR_PTR(-EIO);
153 *p++ = htonl(argp->prog);
154 *p++ = htonl(argp->vers);
155 *p++ = htonl(argp->proc);
156
157 return p;
158}
159
160static int
161xdr_encode_mon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
162{
163 p = xdr_encode_common(rqstp, p, argp);
164 if (IS_ERR(p))
165 return PTR_ERR(p);
166 *p++ = argp->addr;
167 *p++ = argp->vers;
168 *p++ = argp->proto;
169 *p++ = 0;
170 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
171 return 0;
172}
173
174static int
175xdr_encode_unmon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
176{
177 p = xdr_encode_common(rqstp, p, argp);
178 if (IS_ERR(p))
179 return PTR_ERR(p);
180 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
181 return 0;
182}
183
184static int
185xdr_decode_stat_res(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
186{
187 resp->status = ntohl(*p++);
188 resp->state = ntohl(*p++);
189 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
190 resp->status, resp->state);
191 return 0;
192}
193
194static int
195xdr_decode_stat(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
196{
197 resp->state = ntohl(*p++);
198 return 0;
199}
200
201#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
202#define SM_my_id_sz (3+1+SM_my_name_sz)
203#define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
204#define SM_mon_sz (SM_mon_id_sz+4)
205#define SM_monres_sz 2
206#define SM_unmonres_sz 1
207
208#ifndef MAX
209# define MAX(a, b) (((a) > (b))? (a) : (b))
210#endif
211
212static struct rpc_procinfo nsm_procedures[] = {
213[SM_MON] = {
214 .p_proc = SM_MON,
215 .p_encode = (kxdrproc_t) xdr_encode_mon,
216 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
217 .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
218 },
219[SM_UNMON] = {
220 .p_proc = SM_UNMON,
221 .p_encode = (kxdrproc_t) xdr_encode_unmon,
222 .p_decode = (kxdrproc_t) xdr_decode_stat,
223 .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
224 },
225};
226
227static struct rpc_version nsm_version1 = {
228 .number = 1,
229 .nrprocs = sizeof(nsm_procedures)/sizeof(nsm_procedures[0]),
230 .procs = nsm_procedures
231};
232
233static struct rpc_version * nsm_version[] = {
234 [1] = &nsm_version1,
235};
236
237static struct rpc_stat nsm_stats;
238
239static struct rpc_program nsm_program = {
240 .name = "statd",
241 .number = SM_PROGRAM,
242 .nrvers = sizeof(nsm_version)/sizeof(nsm_version[0]),
243 .version = nsm_version,
244 .stats = &nsm_stats
245};