]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/lockd/mon.c
[PATCH] knfsd: lockd: Make nlm_host_rebooted use the nsm_handle
[net-next-2.6.git] / fs / lockd / mon.c
CommitLineData
1da177e4
LT
1/*
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;
dead28da
CL
38 struct rpc_message msg = {
39 .rpc_argp = &args,
40 .rpc_resp = res,
41 };
1da177e4
LT
42
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
dead28da
CL
56 msg.rpc_proc = &clnt->cl_procinfo[proc];
57 status = rpc_call_sync(clnt, &msg, 0);
1da177e4
LT
58 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{
8dead0db 73 struct nsm_handle *nsm = host->h_nsmhandle;
1da177e4
LT
74 struct nsm_res res;
75 int status;
76
77 dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
8dead0db
OK
78 BUG_ON(nsm == NULL);
79
80 if (nsm->sm_monitored)
977faf39 81 return 0;
1da177e4
LT
82
83 status = nsm_mon_unmon(host, SM_MON, &res);
84
85 if (status < 0 || res.status != 0)
86 printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
87 else
8dead0db 88 nsm->sm_monitored = 1;
1da177e4
LT
89 return status;
90}
91
92/*
93 * Cease to monitor remote host
94 */
95int
96nsm_unmonitor(struct nlm_host *host)
97{
8dead0db 98 struct nsm_handle *nsm = host->h_nsmhandle;
1da177e4 99 struct nsm_res res;
977faf39 100 int status = 0;
1da177e4
LT
101
102 dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
8dead0db 103 if (nsm == NULL)
977faf39 104 return 0;
8dead0db 105 host->h_nsmhandle = NULL;
977faf39
OK
106
107 if (!host->h_killed) {
108 status = nsm_mon_unmon(host, SM_UNMON, &res);
109 if (status < 0)
110 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", host->h_name);
8dead0db 111 nsm->sm_monitored = 0;
977faf39 112 }
8dead0db 113 nsm_release(nsm);
1da177e4
LT
114 return status;
115}
116
117/*
118 * Create NSM client for the local host
119 */
120static struct rpc_clnt *
121nsm_create(void)
122{
e1ec7892
CL
123 struct sockaddr_in sin = {
124 .sin_family = AF_INET,
125 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
126 .sin_port = 0,
127 };
128 struct rpc_create_args args = {
129 .protocol = IPPROTO_UDP,
130 .address = (struct sockaddr *)&sin,
131 .addrsize = sizeof(sin),
132 .servername = "localhost",
133 .program = &nsm_program,
134 .version = SM_VERSION,
135 .authflavor = RPC_AUTH_NULL,
136 .flags = (RPC_CLNT_CREATE_ONESHOT),
137 };
138
139 return rpc_create(&args);
1da177e4
LT
140}
141
142/*
143 * XDR functions for NSM.
144 */
145
146static u32 *
147xdr_encode_common(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
148{
149 char buffer[20];
150
151 /*
152 * Use the dotted-quad IP address of the remote host as
153 * identifier. Linux statd always looks up the canonical
154 * hostname first for whatever remote hostname it receives,
155 * so this works alright.
156 */
157 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
158 if (!(p = xdr_encode_string(p, buffer))
e9ff3990 159 || !(p = xdr_encode_string(p, utsname()->nodename)))
1da177e4
LT
160 return ERR_PTR(-EIO);
161 *p++ = htonl(argp->prog);
162 *p++ = htonl(argp->vers);
163 *p++ = htonl(argp->proc);
164
165 return p;
166}
167
168static int
169xdr_encode_mon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
170{
171 p = xdr_encode_common(rqstp, p, argp);
172 if (IS_ERR(p))
173 return PTR_ERR(p);
174 *p++ = argp->addr;
175 *p++ = argp->vers;
176 *p++ = argp->proto;
177 *p++ = 0;
178 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
179 return 0;
180}
181
182static int
183xdr_encode_unmon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
184{
185 p = xdr_encode_common(rqstp, p, argp);
186 if (IS_ERR(p))
187 return PTR_ERR(p);
188 rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
189 return 0;
190}
191
192static int
193xdr_decode_stat_res(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
194{
195 resp->status = ntohl(*p++);
196 resp->state = ntohl(*p++);
197 dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
198 resp->status, resp->state);
199 return 0;
200}
201
202static int
203xdr_decode_stat(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
204{
205 resp->state = ntohl(*p++);
206 return 0;
207}
208
209#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
210#define SM_my_id_sz (3+1+SM_my_name_sz)
211#define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
212#define SM_mon_sz (SM_mon_id_sz+4)
213#define SM_monres_sz 2
214#define SM_unmonres_sz 1
215
216#ifndef MAX
217# define MAX(a, b) (((a) > (b))? (a) : (b))
218#endif
219
220static struct rpc_procinfo nsm_procedures[] = {
221[SM_MON] = {
222 .p_proc = SM_MON,
223 .p_encode = (kxdrproc_t) xdr_encode_mon,
224 .p_decode = (kxdrproc_t) xdr_decode_stat_res,
225 .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
cc0175c1
CL
226 .p_statidx = SM_MON,
227 .p_name = "MONITOR",
1da177e4
LT
228 },
229[SM_UNMON] = {
230 .p_proc = SM_UNMON,
231 .p_encode = (kxdrproc_t) xdr_encode_unmon,
232 .p_decode = (kxdrproc_t) xdr_decode_stat,
233 .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
cc0175c1
CL
234 .p_statidx = SM_UNMON,
235 .p_name = "UNMONITOR",
1da177e4
LT
236 },
237};
238
239static struct rpc_version nsm_version1 = {
e8c96f8c
TK
240 .number = 1,
241 .nrprocs = ARRAY_SIZE(nsm_procedures),
1da177e4
LT
242 .procs = nsm_procedures
243};
244
245static struct rpc_version * nsm_version[] = {
246 [1] = &nsm_version1,
247};
248
249static struct rpc_stat nsm_stats;
250
251static struct rpc_program nsm_program = {
252 .name = "statd",
253 .number = SM_PROGRAM,
e8c96f8c 254 .nrvers = ARRAY_SIZE(nsm_version),
1da177e4
LT
255 .version = nsm_version,
256 .stats = &nsm_stats
257};