3bb71e1b1e1ff35568c2ed527196d0d815231bdc
[safe/jmp/linux-2.6] / fs / lockd / mon.c
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/xprtsock.h>
14 #include <linux/sunrpc/svc.h>
15 #include <linux/lockd/lockd.h>
16 #include <linux/lockd/sm_inter.h>
17
18
19 #define NLMDBG_FACILITY         NLMDBG_MONITOR
20
21 static struct rpc_clnt *        nsm_create(void);
22
23 static struct rpc_program       nsm_program;
24
25 /*
26  * Local NSM state
27  */
28 int                             nsm_local_state;
29
30 /*
31  * Common procedure for SM_MON/SM_UNMON calls
32  */
33 static int
34 nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
35 {
36         struct rpc_clnt *clnt;
37         int             status;
38         struct nsm_args args = {
39                 .addr           = nsm_addr_in(nsm)->sin_addr.s_addr,
40                 .prog           = NLM_PROGRAM,
41                 .vers           = 3,
42                 .proc           = NLMPROC_NSM_NOTIFY,
43                 .mon_name       = nsm->sm_mon_name,
44         };
45         struct rpc_message msg = {
46                 .rpc_argp       = &args,
47                 .rpc_resp       = res,
48         };
49
50         clnt = nsm_create();
51         if (IS_ERR(clnt)) {
52                 status = PTR_ERR(clnt);
53                 dprintk("lockd: failed to create NSM upcall transport, "
54                                 "status=%d\n", status);
55                 goto out;
56         }
57
58         memset(res, 0, sizeof(*res));
59
60         msg.rpc_proc = &clnt->cl_procinfo[proc];
61         status = rpc_call_sync(clnt, &msg, 0);
62         if (status < 0)
63                 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
64                                 status);
65         else
66                 status = 0;
67         rpc_shutdown_client(clnt);
68  out:
69         return status;
70 }
71
72 /**
73  * nsm_monitor - Notify a peer in case we reboot
74  * @host: pointer to nlm_host of peer to notify
75  *
76  * If this peer is not already monitored, this function sends an
77  * upcall to the local rpc.statd to record the name/address of
78  * the peer to notify in case we reboot.
79  *
80  * Returns zero if the peer is monitored by the local rpc.statd;
81  * otherwise a negative errno value is returned.
82  */
83 int nsm_monitor(const struct nlm_host *host)
84 {
85         struct nsm_handle *nsm = host->h_nsmhandle;
86         struct nsm_res  res;
87         int             status;
88
89         dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
90
91         if (nsm->sm_monitored)
92                 return 0;
93
94         /*
95          * Choose whether to record the caller_name or IP address of
96          * this peer in the local rpc.statd's database.
97          */
98         nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
99
100         status = nsm_mon_unmon(nsm, SM_MON, &res);
101         if (res.status != 0)
102                 status = -EIO;
103         if (status < 0)
104                 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
105         else
106                 nsm->sm_monitored = 1;
107         return status;
108 }
109
110 /**
111  * nsm_unmonitor - Unregister peer notification
112  * @host: pointer to nlm_host of peer to stop monitoring
113  *
114  * If this peer is monitored, this function sends an upcall to
115  * tell the local rpc.statd not to send this peer a notification
116  * when we reboot.
117  */
118 void nsm_unmonitor(const struct nlm_host *host)
119 {
120         struct nsm_handle *nsm = host->h_nsmhandle;
121         struct nsm_res  res;
122         int status;
123
124         if (atomic_read(&nsm->sm_count) == 1
125          && nsm->sm_monitored && !nsm->sm_sticky) {
126                 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
127
128                 status = nsm_mon_unmon(nsm, SM_UNMON, &res);
129                 if (res.status != 0)
130                         status = -EIO;
131                 if (status < 0)
132                         printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
133                                         nsm->sm_name);
134                 else
135                         nsm->sm_monitored = 0;
136         }
137 }
138
139 /*
140  * Create NSM client for the local host
141  */
142 static struct rpc_clnt *
143 nsm_create(void)
144 {
145         struct sockaddr_in      sin = {
146                 .sin_family     = AF_INET,
147                 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
148                 .sin_port       = 0,
149         };
150         struct rpc_create_args args = {
151                 .protocol       = XPRT_TRANSPORT_UDP,
152                 .address        = (struct sockaddr *)&sin,
153                 .addrsize       = sizeof(sin),
154                 .servername     = "localhost",
155                 .program        = &nsm_program,
156                 .version        = SM_VERSION,
157                 .authflavor     = RPC_AUTH_NULL,
158         };
159
160         return rpc_create(&args);
161 }
162
163 /*
164  * XDR functions for NSM.
165  *
166  * See http://www.opengroup.org/ for details on the Network
167  * Status Monitor wire protocol.
168  */
169
170 static __be32 *xdr_encode_nsm_string(__be32 *p, char *string)
171 {
172         size_t len = strlen(string);
173
174         if (len > SM_MAXSTRLEN)
175                 len = SM_MAXSTRLEN;
176         return xdr_encode_opaque(p, string, len);
177 }
178
179 /*
180  * "mon_name" specifies the host to be monitored.
181  */
182 static __be32 *xdr_encode_mon_name(__be32 *p, struct nsm_args *argp)
183 {
184         return xdr_encode_nsm_string(p, argp->mon_name);
185 }
186
187 /*
188  * The "my_id" argument specifies the hostname and RPC procedure
189  * to be called when the status manager receives notification
190  * (via the SM_NOTIFY call) that the state of host "mon_name"
191  * has changed.
192  */
193 static __be32 *xdr_encode_my_id(__be32 *p, struct nsm_args *argp)
194 {
195         p = xdr_encode_nsm_string(p, utsname()->nodename);
196         if (!p)
197                 return ERR_PTR(-EIO);
198
199         *p++ = htonl(argp->prog);
200         *p++ = htonl(argp->vers);
201         *p++ = htonl(argp->proc);
202
203         return p;
204 }
205
206 /*
207  * The "mon_id" argument specifies the non-private arguments
208  * of an SM_MON or SM_UNMON call.
209  */
210 static __be32 *xdr_encode_mon_id(__be32 *p, struct nsm_args *argp)
211 {
212         p = xdr_encode_mon_name(p, argp);
213         if (!p)
214                 return ERR_PTR(-EIO);
215
216         return xdr_encode_my_id(p, argp);
217 }
218
219 /*
220  * The "priv" argument may contain private information required
221  * by the SM_MON call. This information will be supplied in the
222  * SM_NOTIFY call.
223  *
224  * Linux provides the raw IP address of the monitored host,
225  * left in network byte order.
226  */
227 static __be32 *xdr_encode_priv(__be32 *p, struct nsm_args *argp)
228 {
229         *p++ = argp->addr;
230         *p++ = 0;
231         *p++ = 0;
232         *p++ = 0;
233
234         return p;
235 }
236
237 static int
238 xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
239 {
240         p = xdr_encode_mon_id(p, argp);
241         if (IS_ERR(p))
242                 return PTR_ERR(p);
243
244         p = xdr_encode_priv(p, argp);
245         if (IS_ERR(p))
246                 return PTR_ERR(p);
247
248         rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
249         return 0;
250 }
251
252 static int
253 xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
254 {
255         p = xdr_encode_mon_id(p, argp);
256         if (IS_ERR(p))
257                 return PTR_ERR(p);
258         rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
259         return 0;
260 }
261
262 static int
263 xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
264 {
265         resp->status = ntohl(*p++);
266         resp->state = ntohl(*p++);
267         dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
268                         resp->status, resp->state);
269         return 0;
270 }
271
272 static int
273 xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
274 {
275         resp->state = ntohl(*p++);
276         return 0;
277 }
278
279 #define SM_my_name_sz   (1+XDR_QUADLEN(SM_MAXSTRLEN))
280 #define SM_my_id_sz     (SM_my_name_sz+3)
281 #define SM_mon_name_sz  (1+XDR_QUADLEN(SM_MAXSTRLEN))
282 #define SM_mon_id_sz    (SM_mon_name_sz+SM_my_id_sz)
283 #define SM_priv_sz      (XDR_QUADLEN(SM_PRIV_SIZE))
284 #define SM_mon_sz       (SM_mon_id_sz+SM_priv_sz)
285 #define SM_monres_sz    2
286 #define SM_unmonres_sz  1
287
288 static struct rpc_procinfo      nsm_procedures[] = {
289 [SM_MON] = {
290                 .p_proc         = SM_MON,
291                 .p_encode       = (kxdrproc_t) xdr_encode_mon,
292                 .p_decode       = (kxdrproc_t) xdr_decode_stat_res,
293                 .p_arglen       = SM_mon_sz,
294                 .p_replen       = SM_monres_sz,
295                 .p_statidx      = SM_MON,
296                 .p_name         = "MONITOR",
297         },
298 [SM_UNMON] = {
299                 .p_proc         = SM_UNMON,
300                 .p_encode       = (kxdrproc_t) xdr_encode_unmon,
301                 .p_decode       = (kxdrproc_t) xdr_decode_stat,
302                 .p_arglen       = SM_mon_id_sz,
303                 .p_replen       = SM_unmonres_sz,
304                 .p_statidx      = SM_UNMON,
305                 .p_name         = "UNMONITOR",
306         },
307 };
308
309 static struct rpc_version       nsm_version1 = {
310                 .number         = 1,
311                 .nrprocs        = ARRAY_SIZE(nsm_procedures),
312                 .procs          = nsm_procedures
313 };
314
315 static struct rpc_version *     nsm_version[] = {
316         [1] = &nsm_version1,
317 };
318
319 static struct rpc_stat          nsm_stats;
320
321 static struct rpc_program       nsm_program = {
322                 .name           = "statd",
323                 .number         = SM_PROGRAM,
324                 .nrvers         = ARRAY_SIZE(nsm_version),
325                 .version        = nsm_version,
326                 .stats          = &nsm_stats
327 };