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