blob: 626b6c116a6d8eb3ef063efdaca0991ae859367c [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
Olaf Kirch9502c522006-10-04 02:15:56 -070033nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
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
Olaf Kirch9502c522006-10-04 02:15:56 -070049 memset(&args, 0, sizeof(args));
50 args.addr = nsm->sm_addr.sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 args.prog = NLM_PROGRAM;
Olaf Kirch9502c522006-10-04 02:15:56 -070052 args.vers = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 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{
Olaf Kirch8dead0d2006-10-04 02:15:53 -070073 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct nsm_res res;
75 int status;
76
77 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
Olaf Kirch8dead0d2006-10-04 02:15:53 -070078 BUG_ON(nsm == NULL);
79
80 if (nsm->sm_monitored)
Olaf Kirch977faf32006-10-04 02:15:51 -070081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Olaf Kirch9502c522006-10-04 02:15:56 -070083 status = nsm_mon_unmon(nsm, SM_MON, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (status < 0 || res.status != 0)
86 printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
87 else
Olaf Kirch8dead0d2006-10-04 02:15:53 -070088 nsm->sm_monitored = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return status;
90}
91
92/*
93 * Cease to monitor remote host
94 */
95int
96nsm_unmonitor(struct nlm_host *host)
97{
Olaf Kirch8dead0d2006-10-04 02:15:53 -070098 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct nsm_res res;
Olaf Kirch977faf32006-10-04 02:15:51 -0700100 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700102 if (nsm == NULL)
Olaf Kirch977faf32006-10-04 02:15:51 -0700103 return 0;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700104 host->h_nsmhandle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Olaf Kirch9502c522006-10-04 02:15:56 -0700106 if (atomic_read(&nsm->sm_count) == 1
107 && nsm->sm_monitored && !nsm->sm_sticky) {
108 dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
109
110 status = nsm_mon_unmon(nsm, SM_UNMON, &res);
Olaf Kirch977faf32006-10-04 02:15:51 -0700111 if (status < 0)
Olaf Kirch9502c522006-10-04 02:15:56 -0700112 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
113 host->h_name);
114 else
115 nsm->sm_monitored = 0;
Olaf Kirch977faf32006-10-04 02:15:51 -0700116 }
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700117 nsm_release(nsm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return status;
119}
120
121/*
122 * Create NSM client for the local host
123 */
124static struct rpc_clnt *
125nsm_create(void)
126{
Chuck Levere1ec7892006-08-22 20:06:20 -0400127 struct sockaddr_in sin = {
128 .sin_family = AF_INET,
129 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
130 .sin_port = 0,
131 };
132 struct rpc_create_args args = {
133 .protocol = IPPROTO_UDP,
134 .address = (struct sockaddr *)&sin,
135 .addrsize = sizeof(sin),
136 .servername = "localhost",
137 .program = &nsm_program,
138 .version = SM_VERSION,
139 .authflavor = RPC_AUTH_NULL,
140 .flags = (RPC_CLNT_CREATE_ONESHOT),
141 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Chuck Levere1ec7892006-08-22 20:06:20 -0400143 return rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146/*
147 * XDR functions for NSM.
148 */
149
150static u32 *
151xdr_encode_common(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
152{
153 char buffer[20];
154
155 /*
156 * Use the dotted-quad IP address of the remote host as
157 * identifier. Linux statd always looks up the canonical
158 * hostname first for whatever remote hostname it receives,
159 * so this works alright.
160 */
161 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
162 if (!(p = xdr_encode_string(p, buffer))
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700163 || !(p = xdr_encode_string(p, utsname()->nodename)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return ERR_PTR(-EIO);
165 *p++ = htonl(argp->prog);
166 *p++ = htonl(argp->vers);
167 *p++ = htonl(argp->proc);
168
169 return p;
170}
171
172static int
173xdr_encode_mon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
174{
175 p = xdr_encode_common(rqstp, p, argp);
176 if (IS_ERR(p))
177 return PTR_ERR(p);
Olaf Kirch9502c522006-10-04 02:15:56 -0700178
179 /* Surprise - there may even be room for an IPv6 address now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 *p++ = argp->addr;
Olaf Kirch9502c522006-10-04 02:15:56 -0700181 *p++ = 0;
182 *p++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *p++ = 0;
184 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
185 return 0;
186}
187
188static int
189xdr_encode_unmon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
190{
191 p = xdr_encode_common(rqstp, p, argp);
192 if (IS_ERR(p))
193 return PTR_ERR(p);
194 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
195 return 0;
196}
197
198static int
199xdr_decode_stat_res(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
200{
201 resp->status = ntohl(*p++);
202 resp->state = ntohl(*p++);
203 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
204 resp->status, resp->state);
205 return 0;
206}
207
208static int
209xdr_decode_stat(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
210{
211 resp->state = ntohl(*p++);
212 return 0;
213}
214
215#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
216#define SM_my_id_sz (3+1+SM_my_name_sz)
217#define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
218#define SM_mon_sz (SM_mon_id_sz+4)
219#define SM_monres_sz 2
220#define SM_unmonres_sz 1
221
222#ifndef MAX
223# define MAX(a, b) (((a) > (b))? (a) : (b))
224#endif
225
226static struct rpc_procinfo nsm_procedures[] = {
227[SM_MON] = {
228 .p_proc = SM_MON,
229 .p_encode = (kxdrproc_t) xdr_encode_mon,
230 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
231 .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
Chuck Levercc0175c2006-03-20 13:44:22 -0500232 .p_statidx = SM_MON,
233 .p_name = "MONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 },
235[SM_UNMON] = {
236 .p_proc = SM_UNMON,
237 .p_encode = (kxdrproc_t) xdr_encode_unmon,
238 .p_decode = (kxdrproc_t) xdr_decode_stat,
239 .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
Chuck Levercc0175c2006-03-20 13:44:22 -0500240 .p_statidx = SM_UNMON,
241 .p_name = "UNMONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 },
243};
244
245static struct rpc_version nsm_version1 = {
Tobias Klausere8c96f82006-03-24 03:15:34 -0800246 .number = 1,
247 .nrprocs = ARRAY_SIZE(nsm_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .procs = nsm_procedures
249};
250
251static struct rpc_version * nsm_version[] = {
252 [1] = &nsm_version1,
253};
254
255static struct rpc_stat nsm_stats;
256
257static struct rpc_program nsm_program = {
258 .name = "statd",
259 .number = SM_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800260 .nrvers = ARRAY_SIZE(nsm_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .version = nsm_version,
262 .stats = &nsm_stats
263};