blob: 8ae4c02d7dfda7e8ab60154e5975de496749d7da [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>
Chuck Lever94da7662008-12-11 17:56:07 -050012#include <linux/ktime.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sunrpc/clnt.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040015#include <linux/sunrpc/xprtsock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sunrpc/svc.h>
17#include <linux/lockd/lockd.h>
18#include <linux/lockd/sm_inter.h>
19
20
21#define NLMDBG_FACILITY NLMDBG_MONITOR
Chuck Lever36e8e662008-12-05 19:02:07 -050022#define NSM_PROGRAM 100024
23#define NSM_VERSION 1
24
25enum {
26 NSMPROC_NULL,
27 NSMPROC_STAT,
28 NSMPROC_MON,
29 NSMPROC_UNMON,
30 NSMPROC_UNMON_ALL,
31 NSMPROC_SIMU_CRASH,
32 NSMPROC_NOTIFY,
33};
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Chuck Lever9c1bfd02008-12-05 19:01:59 -050035struct nsm_args {
Chuck Levercab2d3c2008-12-05 19:03:24 -050036 struct nsm_private *priv;
Chuck Lever9c1bfd02008-12-05 19:01:59 -050037 u32 prog; /* RPC callback info */
38 u32 vers;
39 u32 proc;
40
41 char *mon_name;
42};
43
44struct nsm_res {
45 u32 status;
46 u32 state;
47};
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static struct rpc_clnt * nsm_create(void);
50
51static struct rpc_program nsm_program;
Chuck Lever67c6d102008-12-05 19:02:45 -050052static LIST_HEAD(nsm_handles);
53static DEFINE_SPINLOCK(nsm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * Local NSM state
57 */
Olaf Kirch460f5ca2006-10-04 02:16:03 -070058int nsm_local_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Chuck Lever67c6d102008-12-05 19:02:45 -050060static void nsm_display_ipv4_address(const struct sockaddr *sap, char *buf,
61 const size_t len)
62{
63 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
64 snprintf(buf, len, "%pI4", &sin->sin_addr.s_addr);
65}
66
67static void nsm_display_ipv6_address(const struct sockaddr *sap, char *buf,
68 const size_t len)
69{
70 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
71
72 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
73 snprintf(buf, len, "%pI4", &sin6->sin6_addr.s6_addr32[3]);
74 else if (sin6->sin6_scope_id != 0)
75 snprintf(buf, len, "%pI6%%%u", &sin6->sin6_addr,
76 sin6->sin6_scope_id);
77 else
78 snprintf(buf, len, "%pI6", &sin6->sin6_addr);
79}
80
81static void nsm_display_address(const struct sockaddr *sap,
82 char *buf, const size_t len)
83{
84 switch (sap->sa_family) {
85 case AF_INET:
86 nsm_display_ipv4_address(sap, buf, len);
87 break;
88 case AF_INET6:
89 nsm_display_ipv6_address(sap, buf, len);
90 break;
91 default:
92 snprintf(buf, len, "unsupported address family");
93 break;
94 }
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
Chuck Lever36e8e662008-12-05 19:02:07 -050098 * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
100static int
Olaf Kirch9502c522006-10-04 02:15:56 -0700101nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct rpc_clnt *clnt;
104 int status;
Chuck Levera4846752008-12-04 14:20:23 -0500105 struct nsm_args args = {
Chuck Levercab2d3c2008-12-05 19:03:24 -0500106 .priv = &nsm->sm_priv,
Chuck Levera4846752008-12-04 14:20:23 -0500107 .prog = NLM_PROGRAM,
108 .vers = 3,
109 .proc = NLMPROC_NSM_NOTIFY,
Chuck Lever29ed1402008-12-04 14:20:46 -0500110 .mon_name = nsm->sm_mon_name,
Chuck Levera4846752008-12-04 14:20:23 -0500111 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500112 struct rpc_message msg = {
113 .rpc_argp = &args,
114 .rpc_resp = res,
115 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 clnt = nsm_create();
118 if (IS_ERR(clnt)) {
119 status = PTR_ERR(clnt);
Chuck Lever5acf4312008-12-04 14:20:31 -0500120 dprintk("lockd: failed to create NSM upcall transport, "
121 "status=%d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 goto out;
123 }
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 memset(res, 0, sizeof(*res));
126
Chuck Leverdead28d2006-03-20 13:44:23 -0500127 msg.rpc_proc = &clnt->cl_procinfo[proc];
128 status = rpc_call_sync(clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (status < 0)
Chuck Lever5acf4312008-12-04 14:20:31 -0500130 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
131 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 else
133 status = 0;
Trond Myklebust90c57552007-06-09 19:49:36 -0400134 rpc_shutdown_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 out:
136 return status;
137}
138
Chuck Lever1e493232008-12-04 14:21:24 -0500139/**
140 * nsm_monitor - Notify a peer in case we reboot
141 * @host: pointer to nlm_host of peer to notify
142 *
143 * If this peer is not already monitored, this function sends an
144 * upcall to the local rpc.statd to record the name/address of
145 * the peer to notify in case we reboot.
146 *
147 * Returns zero if the peer is monitored by the local rpc.statd;
148 * otherwise a negative errno value is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Chuck Lever1e493232008-12-04 14:21:24 -0500150int nsm_monitor(const struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700152 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct nsm_res res;
154 int status;
155
Chuck Lever9fee4902008-12-04 14:20:53 -0500156 dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700157
158 if (nsm->sm_monitored)
Olaf Kirch977faf32006-10-04 02:15:51 -0700159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Chuck Lever29ed1402008-12-04 14:20:46 -0500161 /*
162 * Choose whether to record the caller_name or IP address of
163 * this peer in the local rpc.statd's database.
164 */
165 nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
166
Chuck Lever36e8e662008-12-05 19:02:07 -0500167 status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
Chuck Lever5d254b12008-12-04 14:21:15 -0500168 if (res.status != 0)
169 status = -EIO;
170 if (status < 0)
Chuck Lever9fee4902008-12-04 14:20:53 -0500171 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 else
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700173 nsm->sm_monitored = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return status;
175}
176
Chuck Lever356c3eb2008-12-04 14:21:38 -0500177/**
178 * nsm_unmonitor - Unregister peer notification
179 * @host: pointer to nlm_host of peer to stop monitoring
180 *
181 * If this peer is monitored, this function sends an upcall to
182 * tell the local rpc.statd not to send this peer a notification
183 * when we reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
Chuck Lever356c3eb2008-12-04 14:21:38 -0500185void nsm_unmonitor(const struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Olaf Kirch8dead0d2006-10-04 02:15:53 -0700187 struct nsm_handle *nsm = host->h_nsmhandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct nsm_res res;
Chuck Lever356c3eb2008-12-04 14:21:38 -0500189 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Olaf Kirch9502c522006-10-04 02:15:56 -0700191 if (atomic_read(&nsm->sm_count) == 1
192 && nsm->sm_monitored && !nsm->sm_sticky) {
Chuck Lever9fee4902008-12-04 14:20:53 -0500193 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
Olaf Kirch9502c522006-10-04 02:15:56 -0700194
Chuck Lever36e8e662008-12-05 19:02:07 -0500195 status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
Chuck Lever0c7aef42008-12-04 14:21:46 -0500196 if (res.status != 0)
197 status = -EIO;
Olaf Kirch977faf32006-10-04 02:15:51 -0700198 if (status < 0)
Olaf Kirch9502c522006-10-04 02:15:56 -0700199 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
Chuck Lever9fee4902008-12-04 14:20:53 -0500200 nsm->sm_name);
Olaf Kirch9502c522006-10-04 02:15:56 -0700201 else
202 nsm->sm_monitored = 0;
Olaf Kirch977faf32006-10-04 02:15:51 -0700203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Chuck Lever3420a8c2008-12-05 19:03:46 -0500206static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
207 const size_t len)
208{
209 struct nsm_handle *nsm;
210
211 list_for_each_entry(nsm, &nsm_handles, sm_link)
212 if (strlen(nsm->sm_name) == len &&
213 memcmp(nsm->sm_name, hostname, len) == 0)
214 return nsm;
215 return NULL;
216}
217
Chuck Lever77a3ef32008-12-11 17:55:59 -0500218static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap)
219{
220 struct nsm_handle *nsm;
221
222 list_for_each_entry(nsm, &nsm_handles, sm_link)
223 if (nlm_cmp_addr(nsm_addr(nsm), sap))
224 return nsm;
225 return NULL;
226}
227
Chuck Lever3420a8c2008-12-05 19:03:46 -0500228static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
229{
230 struct nsm_handle *nsm;
231
232 list_for_each_entry(nsm, &nsm_handles, sm_link)
233 if (memcmp(nsm->sm_priv.data, priv->data,
234 sizeof(priv->data)) == 0)
235 return nsm;
236 return NULL;
237}
238
Chuck Lever7e44d3b2008-12-05 19:03:16 -0500239/*
240 * Construct a unique cookie to match this nsm_handle to this monitored
241 * host. It is passed to the local rpc.statd via NSMPROC_MON, and
242 * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
243 * requests.
244 *
Chuck Lever94da7662008-12-11 17:56:07 -0500245 * The NSM protocol requires that these cookies be unique while the
246 * system is running. We prefer a stronger requirement of making them
247 * unique across reboots. If user space bugs cause a stale cookie to
248 * be sent to the kernel, it could cause the wrong host to lose its
249 * lock state if cookies were not unique across reboots.
250 *
251 * The cookies are exposed only to local user space via loopback. They
252 * do not appear on the physical network. If we want greater security
253 * for some reason, nsm_init_private() could perform a one-way hash to
254 * obscure the contents of the cookie.
Chuck Lever7e44d3b2008-12-05 19:03:16 -0500255 */
256static void nsm_init_private(struct nsm_handle *nsm)
257{
Chuck Lever94da7662008-12-11 17:56:07 -0500258 u64 *p = (u64 *)&nsm->sm_priv.data;
259 struct timespec ts;
260
261 ktime_get_ts(&ts);
262 *p++ = timespec_to_ns(&ts);
263 *p = (unsigned long)nsm;
Chuck Lever7e44d3b2008-12-05 19:03:16 -0500264}
265
Chuck Leverb39b8972008-12-11 17:55:52 -0500266static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
267 const size_t salen,
268 const char *hostname,
269 const size_t hostname_len)
270{
271 struct nsm_handle *new;
272
273 new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
274 if (unlikely(new == NULL))
275 return NULL;
276
277 atomic_set(&new->sm_count, 1);
278 new->sm_name = (char *)(new + 1);
279 memcpy(nsm_addr(new), sap, salen);
280 new->sm_addrlen = salen;
281 nsm_init_private(new);
282 nsm_display_address((const struct sockaddr *)&new->sm_addr,
283 new->sm_addrbuf, sizeof(new->sm_addrbuf));
284 memcpy(new->sm_name, hostname, hostname_len);
285 new->sm_name[hostname_len] = '\0';
286
287 return new;
288}
289
Chuck Lever67c6d102008-12-05 19:02:45 -0500290/**
Chuck Lever92fd91b2008-12-05 19:04:01 -0500291 * nsm_get_handle - Find or create a cached nsm_handle
Chuck Lever67c6d102008-12-05 19:02:45 -0500292 * @sap: pointer to socket address of handle to find
293 * @salen: length of socket address
294 * @hostname: pointer to C string containing hostname to find
295 * @hostname_len: length of C string
Chuck Lever67c6d102008-12-05 19:02:45 -0500296 *
Chuck Lever92fd91b2008-12-05 19:04:01 -0500297 * Behavior is modulated by the global nsm_use_hostnames variable.
Chuck Lever67c6d102008-12-05 19:02:45 -0500298 *
Chuck Lever92fd91b2008-12-05 19:04:01 -0500299 * Returns a cached nsm_handle after bumping its ref count, or
300 * returns a fresh nsm_handle if a handle that matches @sap and/or
301 * @hostname cannot be found in the handle cache. Returns NULL if
302 * an error occurs.
Chuck Lever67c6d102008-12-05 19:02:45 -0500303 */
Chuck Lever92fd91b2008-12-05 19:04:01 -0500304struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
305 const size_t salen, const char *hostname,
306 const size_t hostname_len)
Chuck Lever67c6d102008-12-05 19:02:45 -0500307{
Chuck Lever77a3ef32008-12-11 17:55:59 -0500308 struct nsm_handle *cached, *new = NULL;
Chuck Lever67c6d102008-12-05 19:02:45 -0500309
Chuck Lever67c6d102008-12-05 19:02:45 -0500310 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
311 if (printk_ratelimit()) {
312 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
313 "in NFS lock request\n",
314 (int)hostname_len, hostname);
315 }
316 return NULL;
317 }
318
319retry:
320 spin_lock(&nsm_lock);
Chuck Lever67c6d102008-12-05 19:02:45 -0500321
Chuck Lever77a3ef32008-12-11 17:55:59 -0500322 if (nsm_use_hostnames && hostname != NULL)
323 cached = nsm_lookup_hostname(hostname, hostname_len);
324 else
325 cached = nsm_lookup_addr(sap);
326
327 if (cached != NULL) {
328 atomic_inc(&cached->sm_count);
329 spin_unlock(&nsm_lock);
330 kfree(new);
331 dprintk("lockd: found nsm_handle for %s (%s), "
332 "cnt %d\n", cached->sm_name,
333 cached->sm_addrbuf,
334 atomic_read(&cached->sm_count));
335 return cached;
Chuck Lever67c6d102008-12-05 19:02:45 -0500336 }
Chuck Lever77a3ef32008-12-11 17:55:59 -0500337
338 if (new != NULL) {
339 list_add(&new->sm_link, &nsm_handles);
340 spin_unlock(&nsm_lock);
Chuck Lever5cf1c4b2008-12-05 19:02:53 -0500341 dprintk("lockd: created nsm_handle for %s (%s)\n",
Chuck Lever77a3ef32008-12-11 17:55:59 -0500342 new->sm_name, new->sm_addrbuf);
343 return new;
Chuck Lever67c6d102008-12-05 19:02:45 -0500344 }
Chuck Lever77a3ef32008-12-11 17:55:59 -0500345
Chuck Lever67c6d102008-12-05 19:02:45 -0500346 spin_unlock(&nsm_lock);
347
Chuck Lever77a3ef32008-12-11 17:55:59 -0500348 new = nsm_create_handle(sap, salen, hostname, hostname_len);
349 if (unlikely(new == NULL))
Chuck Lever67c6d102008-12-05 19:02:45 -0500350 return NULL;
Chuck Lever67c6d102008-12-05 19:02:45 -0500351 goto retry;
Chuck Lever67c6d102008-12-05 19:02:45 -0500352}
353
354/**
Chuck Lever3420a8c2008-12-05 19:03:46 -0500355 * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
356 * @info: pointer to NLMPROC_SM_NOTIFY arguments
357 *
358 * Returns a matching nsm_handle if found in the nsm cache; the returned
359 * nsm_handle's reference count is bumped and sm_monitored is cleared.
360 * Otherwise returns NULL if some error occurred.
361 */
362struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
363{
364 struct nsm_handle *cached;
365
366 spin_lock(&nsm_lock);
367
Chuck Lever94da7662008-12-11 17:56:07 -0500368 cached = nsm_lookup_priv(&info->priv);
Chuck Lever3420a8c2008-12-05 19:03:46 -0500369 if (unlikely(cached == NULL)) {
370 spin_unlock(&nsm_lock);
371 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
372 info->len, info->mon);
373 return cached;
374 }
375
376 atomic_inc(&cached->sm_count);
377 spin_unlock(&nsm_lock);
378
379 /*
380 * During subsequent lock activity, force a fresh
381 * notification to be set up for this host.
382 */
383 cached->sm_monitored = 0;
384
385 dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
386 cached->sm_name, cached->sm_addrbuf,
387 atomic_read(&cached->sm_count));
388 return cached;
389}
390
391/**
Chuck Lever67c6d102008-12-05 19:02:45 -0500392 * nsm_release - Release an NSM handle
393 * @nsm: pointer to handle to be released
394 *
395 */
396void nsm_release(struct nsm_handle *nsm)
397{
Chuck Lever67c6d102008-12-05 19:02:45 -0500398 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
399 list_del(&nsm->sm_link);
400 spin_unlock(&nsm_lock);
Chuck Lever5cf1c4b2008-12-05 19:02:53 -0500401 dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
402 nsm->sm_name, nsm->sm_addrbuf);
Chuck Lever67c6d102008-12-05 19:02:45 -0500403 kfree(nsm);
404 }
405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*
408 * Create NSM client for the local host
409 */
410static struct rpc_clnt *
411nsm_create(void)
412{
Chuck Levere1ec7892006-08-22 20:06:20 -0400413 struct sockaddr_in sin = {
414 .sin_family = AF_INET,
415 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
416 .sin_port = 0,
417 };
418 struct rpc_create_args args = {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400419 .protocol = XPRT_TRANSPORT_UDP,
Chuck Levere1ec7892006-08-22 20:06:20 -0400420 .address = (struct sockaddr *)&sin,
421 .addrsize = sizeof(sin),
422 .servername = "localhost",
423 .program = &nsm_program,
Chuck Lever36e8e662008-12-05 19:02:07 -0500424 .version = NSM_VERSION,
Chuck Levere1ec7892006-08-22 20:06:20 -0400425 .authflavor = RPC_AUTH_NULL,
Chuck Levere1ec7892006-08-22 20:06:20 -0400426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Chuck Levere1ec7892006-08-22 20:06:20 -0400428 return rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431/*
432 * XDR functions for NSM.
Chuck Lever2ca77542008-03-14 14:26:01 -0400433 *
434 * See http://www.opengroup.org/ for details on the Network
435 * Status Monitor wire protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500438static int encode_nsm_string(struct xdr_stream *xdr, const char *string)
Chuck Lever099bd052008-03-14 14:25:32 -0400439{
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500440 const u32 len = strlen(string);
441 __be32 *p;
Chuck Lever099bd052008-03-14 14:25:32 -0400442
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500443 if (unlikely(len > SM_MAXSTRLEN))
444 return -EIO;
445 p = xdr_reserve_space(xdr, sizeof(u32) + len);
446 if (unlikely(p == NULL))
447 return -EIO;
448 xdr_encode_opaque(p, string, len);
449 return 0;
Chuck Lever099bd052008-03-14 14:25:32 -0400450}
451
Chuck Lever49695172008-03-14 14:25:39 -0400452/*
453 * "mon_name" specifies the host to be monitored.
Chuck Lever49695172008-03-14 14:25:39 -0400454 */
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500455static int encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
Chuck Lever49695172008-03-14 14:25:39 -0400456{
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500457 return encode_nsm_string(xdr, argp->mon_name);
Chuck Lever49695172008-03-14 14:25:39 -0400458}
459
Chuck Lever850c95f2008-03-14 14:25:46 -0400460/*
461 * The "my_id" argument specifies the hostname and RPC procedure
462 * to be called when the status manager receives notification
Chuck Lever36e8e662008-12-05 19:02:07 -0500463 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
Chuck Lever850c95f2008-03-14 14:25:46 -0400464 * has changed.
465 */
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500466static int encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
Chuck Lever850c95f2008-03-14 14:25:46 -0400467{
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500468 int status;
469 __be32 *p;
Chuck Lever850c95f2008-03-14 14:25:46 -0400470
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500471 status = encode_nsm_string(xdr, utsname()->nodename);
472 if (unlikely(status != 0))
473 return status;
474 p = xdr_reserve_space(xdr, 3 * sizeof(u32));
475 if (unlikely(p == NULL))
476 return -EIO;
Chuck Lever850c95f2008-03-14 14:25:46 -0400477 *p++ = htonl(argp->prog);
478 *p++ = htonl(argp->vers);
479 *p++ = htonl(argp->proc);
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500480 return 0;
Chuck Lever850c95f2008-03-14 14:25:46 -0400481}
482
Chuck Leverea72a7f2008-03-14 14:25:53 -0400483/*
484 * The "mon_id" argument specifies the non-private arguments
Chuck Lever36e8e662008-12-05 19:02:07 -0500485 * of an NSMPROC_MON or NSMPROC_UNMON call.
Chuck Leverea72a7f2008-03-14 14:25:53 -0400486 */
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500487static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
Chuck Leverea72a7f2008-03-14 14:25:53 -0400488{
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500489 int status;
Chuck Leverea72a7f2008-03-14 14:25:53 -0400490
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500491 status = encode_mon_name(xdr, argp);
492 if (unlikely(status != 0))
493 return status;
494 return encode_my_id(xdr, argp);
Chuck Leverea72a7f2008-03-14 14:25:53 -0400495}
496
Chuck Lever0490a542008-03-14 14:26:08 -0400497/*
498 * The "priv" argument may contain private information required
Chuck Lever36e8e662008-12-05 19:02:07 -0500499 * by the NSMPROC_MON call. This information will be supplied in the
500 * NLMPROC_SM_NOTIFY call.
Chuck Lever0490a542008-03-14 14:26:08 -0400501 */
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500502static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
Chuck Lever0490a542008-03-14 14:26:08 -0400503{
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500504 __be32 *p;
505
506 p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
507 if (unlikely(p == NULL))
508 return -EIO;
Chuck Levercab2d3c2008-12-05 19:03:24 -0500509 xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
511}
512
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500513static int xdr_enc_mon(struct rpc_rqst *req, __be32 *p,
514 const struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500516 struct xdr_stream xdr;
517 int status;
518
519 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
520 status = encode_mon_id(&xdr, argp);
521 if (unlikely(status))
522 return status;
523 return encode_priv(&xdr, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500526static int xdr_enc_unmon(struct rpc_rqst *req, __be32 *p,
527 const struct nsm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500529 struct xdr_stream xdr;
530
531 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
532 return encode_mon_id(&xdr, argp);
533}
534
535static int xdr_dec_stat_res(struct rpc_rqst *rqstp, __be32 *p,
536 struct nsm_res *resp)
537{
538 struct xdr_stream xdr;
539
540 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
541 p = xdr_inline_decode(&xdr, 2 * sizeof(u32));
542 if (unlikely(p == NULL))
543 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 resp->status = ntohl(*p++);
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500545 resp->state = ntohl(*p);
546
547 dprintk("lockd: xdr_dec_stat_res status %d state %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 resp->status, resp->state);
549 return 0;
550}
551
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500552static int xdr_dec_stat(struct rpc_rqst *rqstp, __be32 *p,
553 struct nsm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500555 struct xdr_stream xdr;
556
557 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
558 p = xdr_inline_decode(&xdr, sizeof(u32));
559 if (unlikely(p == NULL))
560 return -EIO;
561 resp->state = ntohl(*p);
562
563 dprintk("lockd: xdr_dec_stat state %d\n", resp->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return 0;
565}
566
567#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
Chuck Lever2ca77542008-03-14 14:26:01 -0400568#define SM_my_id_sz (SM_my_name_sz+3)
569#define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
570#define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
Chuck Lever0490a542008-03-14 14:26:08 -0400571#define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
572#define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573#define SM_monres_sz 2
574#define SM_unmonres_sz 1
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static struct rpc_procinfo nsm_procedures[] = {
Chuck Lever36e8e662008-12-05 19:02:07 -0500577[NSMPROC_MON] = {
578 .p_proc = NSMPROC_MON,
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500579 .p_encode = (kxdrproc_t)xdr_enc_mon,
580 .p_decode = (kxdrproc_t)xdr_dec_stat_res,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400581 .p_arglen = SM_mon_sz,
582 .p_replen = SM_monres_sz,
Chuck Lever36e8e662008-12-05 19:02:07 -0500583 .p_statidx = NSMPROC_MON,
Chuck Levercc0175c2006-03-20 13:44:22 -0500584 .p_name = "MONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 },
Chuck Lever36e8e662008-12-05 19:02:07 -0500586[NSMPROC_UNMON] = {
587 .p_proc = NSMPROC_UNMON,
Chuck Lever03eb1dc2008-12-05 19:02:15 -0500588 .p_encode = (kxdrproc_t)xdr_enc_unmon,
589 .p_decode = (kxdrproc_t)xdr_dec_stat,
Chuck Lever2bea90d2007-03-29 16:47:53 -0400590 .p_arglen = SM_mon_id_sz,
591 .p_replen = SM_unmonres_sz,
Chuck Lever36e8e662008-12-05 19:02:07 -0500592 .p_statidx = NSMPROC_UNMON,
Chuck Levercc0175c2006-03-20 13:44:22 -0500593 .p_name = "UNMONITOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 },
595};
596
597static struct rpc_version nsm_version1 = {
Tobias Klausere8c96f82006-03-24 03:15:34 -0800598 .number = 1,
599 .nrprocs = ARRAY_SIZE(nsm_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .procs = nsm_procedures
601};
602
603static struct rpc_version * nsm_version[] = {
604 [1] = &nsm_version1,
605};
606
607static struct rpc_stat nsm_stats;
608
609static struct rpc_program nsm_program = {
610 .name = "statd",
Chuck Lever36e8e662008-12-05 19:02:07 -0500611 .number = NSM_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800612 .nrvers = ARRAY_SIZE(nsm_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 .version = nsm_version,
614 .stats = &nsm_stats
615};