blob: 3353ed8421a7af49e69e98758860d7d7754f7185 [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 */
Olaf Kirch460f5ca2006-10-04 02:16:03 -070027int nsm_local_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
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));
Olaf Kirchabd1f502006-10-04 02:16:01 -070050 args.mon_name = nsm->sm_name;
Olaf Kirch9502c522006-10-04 02:15:56 -070051 args.addr = nsm->sm_addr.sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 args.prog = NLM_PROGRAM;
Olaf Kirch9502c522006-10-04 02:15:56 -070053 args.vers = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 args.proc = NLMPROC_NSM_NOTIFY;
55 memset(res, 0, sizeof(*res));
56
Chuck Leverdead28d2006-03-20 13:44:23 -050057 msg.rpc_proc = &clnt->cl_procinfo[proc];
58 status = rpc_call_sync(clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (status < 0)
60 printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
61 status);
62 else
63 status = 0;
Trond Myklebust90c57552007-06-09 19:49:36 -040064 rpc_shutdown_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 out:
66 return status;
67}
68
69/*
70 * Set up monitoring of a remote host
71 */
72int
73nsm_monitor(struct nlm_host *host)
74{
Olaf Kirch8dead0d2006-10-04 02:15:53 -070075 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct nsm_res res;
77 int status;
78
79 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
Olaf Kirch8dead0d2006-10-04 02:15:53 -070080 BUG_ON(nsm == NULL);
81
82 if (nsm->sm_monitored)
Olaf Kirch977faf32006-10-04 02:15:51 -070083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Olaf Kirch9502c522006-10-04 02:15:56 -070085 status = nsm_mon_unmon(nsm, SM_MON, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 if (status < 0 || res.status != 0)
88 printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
89 else
Olaf Kirch8dead0d2006-10-04 02:15:53 -070090 nsm->sm_monitored = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return status;
92}
93
94/*
95 * Cease to monitor remote host
96 */
97int
98nsm_unmonitor(struct nlm_host *host)
99{
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700100 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct nsm_res res;
Olaf Kirch977faf32006-10-04 02:15:51 -0700102 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700104 if (nsm == NULL)
Olaf Kirch977faf32006-10-04 02:15:51 -0700105 return 0;
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700106 host->h_nsmhandle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Olaf Kirch9502c522006-10-04 02:15:56 -0700108 if (atomic_read(&nsm->sm_count) == 1
109 && nsm->sm_monitored && !nsm->sm_sticky) {
110 dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
111
112 status = nsm_mon_unmon(nsm, SM_UNMON, &res);
Olaf Kirch977faf32006-10-04 02:15:51 -0700113 if (status < 0)
Olaf Kirch9502c522006-10-04 02:15:56 -0700114 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
115 host->h_name);
116 else
117 nsm->sm_monitored = 0;
Olaf Kirch977faf32006-10-04 02:15:51 -0700118 }
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700119 nsm_release(nsm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return status;
121}
122
123/*
124 * Create NSM client for the local host
125 */
126static struct rpc_clnt *
127nsm_create(void)
128{
Chuck Levere1ec7892006-08-22 20:06:20 -0400129 struct sockaddr_in sin = {
130 .sin_family = AF_INET,
131 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
132 .sin_port = 0,
133 };
134 struct rpc_create_args args = {
135 .protocol = IPPROTO_UDP,
136 .address = (struct sockaddr *)&sin,
137 .addrsize = sizeof(sin),
138 .servername = "localhost",
139 .program = &nsm_program,
140 .version = SM_VERSION,
141 .authflavor = RPC_AUTH_NULL,
Chuck Levere1ec7892006-08-22 20:06:20 -0400142 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Chuck Levere1ec7892006-08-22 20:06:20 -0400144 return rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/*
148 * XDR functions for NSM.
149 */
150
Al Viro52921e02006-10-19 23:28:46 -0700151static __be32 *
152xdr_encode_common(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Olaf Kirchabd1f502006-10-04 02:16:01 -0700154 char buffer[20], *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /*
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.
161 */
Olaf Kirchabd1f502006-10-04 02:16:01 -0700162 if (nsm_use_hostnames) {
163 name = argp->mon_name;
164 } else {
165 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
166 name = buffer;
167 }
168 if (!(p = xdr_encode_string(p, name))
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700169 || !(p = xdr_encode_string(p, utsname()->nodename)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return ERR_PTR(-EIO);
171 *p++ = htonl(argp->prog);
172 *p++ = htonl(argp->vers);
173 *p++ = htonl(argp->proc);
174
175 return p;
176}
177
178static int
Al Viro52921e02006-10-19 23:28:46 -0700179xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 p = xdr_encode_common(rqstp, p, argp);
182 if (IS_ERR(p))
183 return PTR_ERR(p);
Olaf Kirch9502c522006-10-04 02:15:56 -0700184
185 /* Surprise - there may even be room for an IPv6 address now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *p++ = argp->addr;
Olaf Kirch9502c522006-10-04 02:15:56 -0700187 *p++ = 0;
188 *p++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *p++ = 0;
190 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
191 return 0;
192}
193
194static int
Al Viro52921e02006-10-19 23:28:46 -0700195xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 p = xdr_encode_common(rqstp, p, argp);
198 if (IS_ERR(p))
199 return PTR_ERR(p);
200 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
201 return 0;
202}
203
204static int
Al Viro52921e02006-10-19 23:28:46 -0700205xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
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);
211 return 0;
212}
213
214static int
Al Viro52921e02006-10-19 23:28:46 -0700215xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 resp->state = ntohl(*p++);
218 return 0;
219}
220
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
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static struct rpc_procinfo nsm_procedures[] = {
229[SM_MON] = {
230 .p_proc = SM_MON,
231 .p_encode = (kxdrproc_t) xdr_encode_mon,
232 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400233 .p_arglen = SM_mon_sz,
234 .p_replen = SM_monres_sz,
Chuck Levercc0175c2006-03-20 13:44:22 -0500235 .p_statidx = SM_MON,
236 .p_name = "MONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 },
238[SM_UNMON] = {
239 .p_proc = SM_UNMON,
240 .p_encode = (kxdrproc_t) xdr_encode_unmon,
241 .p_decode = (kxdrproc_t) xdr_decode_stat,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400242 .p_arglen = SM_mon_id_sz,
243 .p_replen = SM_unmonres_sz,
Chuck Levercc0175c2006-03-20 13:44:22 -0500244 .p_statidx = SM_UNMON,
245 .p_name = "UNMONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 },
247};
248
249static struct rpc_version nsm_version1 = {
Tobias Klausere8c96f82006-03-24 03:15:34 -0800250 .number = 1,
251 .nrprocs = ARRAY_SIZE(nsm_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .procs = nsm_procedures
253};
254
255static struct rpc_version * nsm_version[] = {
256 [1] = &nsm_version1,
257};
258
259static struct rpc_stat nsm_stats;
260
261static struct rpc_program nsm_program = {
262 .name = "statd",
263 .number = SM_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800264 .nrvers = ARRAY_SIZE(nsm_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .version = nsm_version,
266 .stats = &nsm_stats
267};