Merge tag 'for-3.6-rc3' of git://gitorious.org/linux-pwm/linux-pwm
[deliverable/linux.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>
94da7663 12#include <linux/ktime.h>
5a0e3ad6 13#include <linux/slab.h>
94da7663 14
1da177e4 15#include <linux/sunrpc/clnt.h>
0896a725 16#include <linux/sunrpc/xprtsock.h>
1da177e4
LT
17#include <linux/sunrpc/svc.h>
18#include <linux/lockd/lockd.h>
1da177e4 19
ad5b365c
MR
20#include <asm/unaligned.h>
21
1da177e4 22#define NLMDBG_FACILITY NLMDBG_MONITOR
36e8e668
CL
23#define NSM_PROGRAM 100024
24#define NSM_VERSION 1
25
26enum {
27 NSMPROC_NULL,
28 NSMPROC_STAT,
29 NSMPROC_MON,
30 NSMPROC_UNMON,
31 NSMPROC_UNMON_ALL,
32 NSMPROC_SIMU_CRASH,
33 NSMPROC_NOTIFY,
34};
1da177e4 35
9c1bfd03 36struct nsm_args {
cab2d3c9 37 struct nsm_private *priv;
9c1bfd03
CL
38 u32 prog; /* RPC callback info */
39 u32 vers;
40 u32 proc;
41
42 char *mon_name;
43};
44
45struct nsm_res {
46 u32 status;
47 u32 state;
48};
49
a613fa16 50static const struct rpc_program nsm_program;
67c6d107
CL
51static LIST_HEAD(nsm_handles);
52static DEFINE_SPINLOCK(nsm_lock);
1da177e4
LT
53
54/*
55 * Local NSM state
56 */
6c9dc425 57u32 __read_mostly nsm_local_state;
90ab5ee9 58bool __read_mostly nsm_use_hostnames;
1da177e4 59
8529bc51
CL
60static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm)
61{
62 return (struct sockaddr *)&nsm->sm_addr;
63}
64
0e1cb5c0 65static struct rpc_clnt *nsm_create(struct net *net)
49b5699b
CL
66{
67 struct sockaddr_in sin = {
68 .sin_family = AF_INET,
69 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
70 };
71 struct rpc_create_args args = {
0e1cb5c0 72 .net = net,
49b5699b
CL
73 .protocol = XPRT_TRANSPORT_UDP,
74 .address = (struct sockaddr *)&sin,
75 .addrsize = sizeof(sin),
76 .servername = "rpc.statd",
77 .program = &nsm_program,
78 .version = NSM_VERSION,
79 .authflavor = RPC_AUTH_NULL,
0e5c2632 80 .flags = RPC_CLNT_CREATE_NOPING,
49b5699b
CL
81 };
82
83 return rpc_create(&args);
84}
85
0e1cb5c0
SK
86static int nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res,
87 struct net *net)
1da177e4
LT
88{
89 struct rpc_clnt *clnt;
90 int status;
a4846750 91 struct nsm_args args = {
cab2d3c9 92 .priv = &nsm->sm_priv,
a4846750
CL
93 .prog = NLM_PROGRAM,
94 .vers = 3,
95 .proc = NLMPROC_NSM_NOTIFY,
29ed1407 96 .mon_name = nsm->sm_mon_name,
a4846750 97 };
dead28da
CL
98 struct rpc_message msg = {
99 .rpc_argp = &args,
100 .rpc_resp = res,
101 };
1da177e4 102
0e1cb5c0 103 clnt = nsm_create(net);
1da177e4
LT
104 if (IS_ERR(clnt)) {
105 status = PTR_ERR(clnt);
5acf4315
CL
106 dprintk("lockd: failed to create NSM upcall transport, "
107 "status=%d\n", status);
1da177e4
LT
108 goto out;
109 }
110
1da177e4
LT
111 memset(res, 0, sizeof(*res));
112
dead28da
CL
113 msg.rpc_proc = &clnt->cl_procinfo[proc];
114 status = rpc_call_sync(clnt, &msg, 0);
1da177e4 115 if (status < 0)
5acf4315
CL
116 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
117 status);
1da177e4
LT
118 else
119 status = 0;
90c5755f 120 rpc_shutdown_client(clnt);
1da177e4
LT
121 out:
122 return status;
123}
124
1e49323c
CL
125/**
126 * nsm_monitor - Notify a peer in case we reboot
127 * @host: pointer to nlm_host of peer to notify
128 *
129 * If this peer is not already monitored, this function sends an
130 * upcall to the local rpc.statd to record the name/address of
131 * the peer to notify in case we reboot.
132 *
133 * Returns zero if the peer is monitored by the local rpc.statd;
134 * otherwise a negative errno value is returned.
1da177e4 135 */
1e49323c 136int nsm_monitor(const struct nlm_host *host)
1da177e4 137{
8dead0db 138 struct nsm_handle *nsm = host->h_nsmhandle;
1da177e4
LT
139 struct nsm_res res;
140 int status;
141
9fee4902 142 dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
8dead0db
OK
143
144 if (nsm->sm_monitored)
977faf39 145 return 0;
1da177e4 146
29ed1407
CL
147 /*
148 * Choose whether to record the caller_name or IP address of
149 * this peer in the local rpc.statd's database.
150 */
151 nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
152
0e1cb5c0 153 status = nsm_mon_unmon(nsm, NSMPROC_MON, &res, host->net);
6c9dc425 154 if (unlikely(res.status != 0))
5d254b11 155 status = -EIO;
6c9dc425 156 if (unlikely(status < 0)) {
9fee4902 157 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
6c9dc425
CL
158 return status;
159 }
160
161 nsm->sm_monitored = 1;
162 if (unlikely(nsm_local_state != res.state)) {
163 nsm_local_state = res.state;
164 dprintk("lockd: NSM state changed to %d\n", nsm_local_state);
165 }
166 return 0;
1da177e4
LT
167}
168
356c3eb4
CL
169/**
170 * nsm_unmonitor - Unregister peer notification
171 * @host: pointer to nlm_host of peer to stop monitoring
172 *
173 * If this peer is monitored, this function sends an upcall to
174 * tell the local rpc.statd not to send this peer a notification
175 * when we reboot.
1da177e4 176 */
356c3eb4 177void nsm_unmonitor(const struct nlm_host *host)
1da177e4 178{
8dead0db 179 struct nsm_handle *nsm = host->h_nsmhandle;
1da177e4 180 struct nsm_res res;
356c3eb4 181 int status;
1da177e4 182
9502c522
OK
183 if (atomic_read(&nsm->sm_count) == 1
184 && nsm->sm_monitored && !nsm->sm_sticky) {
9fee4902 185 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
9502c522 186
0e1cb5c0 187 status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res, host->net);
0c7aef45
CL
188 if (res.status != 0)
189 status = -EIO;
977faf39 190 if (status < 0)
9502c522 191 printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
9fee4902 192 nsm->sm_name);
9502c522
OK
193 else
194 nsm->sm_monitored = 0;
977faf39 195 }
1da177e4
LT
196}
197
3420a8c4
CL
198static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
199 const size_t len)
200{
201 struct nsm_handle *nsm;
202
203 list_for_each_entry(nsm, &nsm_handles, sm_link)
204 if (strlen(nsm->sm_name) == len &&
205 memcmp(nsm->sm_name, hostname, len) == 0)
206 return nsm;
207 return NULL;
208}
209
77a3ef33
CL
210static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap)
211{
212 struct nsm_handle *nsm;
213
214 list_for_each_entry(nsm, &nsm_handles, sm_link)
4516fc04 215 if (rpc_cmp_addr(nsm_addr(nsm), sap))
77a3ef33
CL
216 return nsm;
217 return NULL;
218}
219
3420a8c4
CL
220static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
221{
222 struct nsm_handle *nsm;
223
224 list_for_each_entry(nsm, &nsm_handles, sm_link)
225 if (memcmp(nsm->sm_priv.data, priv->data,
226 sizeof(priv->data)) == 0)
227 return nsm;
228 return NULL;
229}
230
7e44d3be
CL
231/*
232 * Construct a unique cookie to match this nsm_handle to this monitored
233 * host. It is passed to the local rpc.statd via NSMPROC_MON, and
234 * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
235 * requests.
236 *
94da7663
CL
237 * The NSM protocol requires that these cookies be unique while the
238 * system is running. We prefer a stronger requirement of making them
239 * unique across reboots. If user space bugs cause a stale cookie to
240 * be sent to the kernel, it could cause the wrong host to lose its
241 * lock state if cookies were not unique across reboots.
242 *
243 * The cookies are exposed only to local user space via loopback. They
244 * do not appear on the physical network. If we want greater security
245 * for some reason, nsm_init_private() could perform a one-way hash to
246 * obscure the contents of the cookie.
7e44d3be
CL
247 */
248static void nsm_init_private(struct nsm_handle *nsm)
249{
94da7663
CL
250 u64 *p = (u64 *)&nsm->sm_priv.data;
251 struct timespec ts;
ad5b365c 252 s64 ns;
94da7663
CL
253
254 ktime_get_ts(&ts);
ad5b365c
MR
255 ns = timespec_to_ns(&ts);
256 put_unaligned(ns, p);
257 put_unaligned((unsigned long)nsm, p + 1);
7e44d3be
CL
258}
259
b39b897c
CL
260static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
261 const size_t salen,
262 const char *hostname,
263 const size_t hostname_len)
264{
265 struct nsm_handle *new;
266
267 new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
268 if (unlikely(new == NULL))
269 return NULL;
270
271 atomic_set(&new->sm_count, 1);
272 new->sm_name = (char *)(new + 1);
273 memcpy(nsm_addr(new), sap, salen);
274 new->sm_addrlen = salen;
275 nsm_init_private(new);
c15128c5
CL
276
277 if (rpc_ntop(nsm_addr(new), new->sm_addrbuf,
278 sizeof(new->sm_addrbuf)) == 0)
279 (void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf),
280 "unsupported address family");
b39b897c
CL
281 memcpy(new->sm_name, hostname, hostname_len);
282 new->sm_name[hostname_len] = '\0';
283
284 return new;
285}
286
67c6d107 287/**
92fd91b9 288 * nsm_get_handle - Find or create a cached nsm_handle
67c6d107
CL
289 * @sap: pointer to socket address of handle to find
290 * @salen: length of socket address
291 * @hostname: pointer to C string containing hostname to find
292 * @hostname_len: length of C string
67c6d107 293 *
92fd91b9 294 * Behavior is modulated by the global nsm_use_hostnames variable.
67c6d107 295 *
92fd91b9
CL
296 * Returns a cached nsm_handle after bumping its ref count, or
297 * returns a fresh nsm_handle if a handle that matches @sap and/or
298 * @hostname cannot be found in the handle cache. Returns NULL if
299 * an error occurs.
67c6d107 300 */
92fd91b9
CL
301struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
302 const size_t salen, const char *hostname,
303 const size_t hostname_len)
67c6d107 304{
77a3ef33 305 struct nsm_handle *cached, *new = NULL;
67c6d107 306
67c6d107
CL
307 if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
308 if (printk_ratelimit()) {
309 printk(KERN_WARNING "Invalid hostname \"%.*s\" "
310 "in NFS lock request\n",
311 (int)hostname_len, hostname);
312 }
313 return NULL;
314 }
315
316retry:
317 spin_lock(&nsm_lock);
77a3ef33
CL
318
319 if (nsm_use_hostnames && hostname != NULL)
320 cached = nsm_lookup_hostname(hostname, hostname_len);
321 else
322 cached = nsm_lookup_addr(sap);
323
324 if (cached != NULL) {
325 atomic_inc(&cached->sm_count);
326 spin_unlock(&nsm_lock);
327 kfree(new);
328 dprintk("lockd: found nsm_handle for %s (%s), "
329 "cnt %d\n", cached->sm_name,
330 cached->sm_addrbuf,
331 atomic_read(&cached->sm_count));
332 return cached;
67c6d107 333 }
77a3ef33
CL
334
335 if (new != NULL) {
336 list_add(&new->sm_link, &nsm_handles);
337 spin_unlock(&nsm_lock);
5cf1c4b1 338 dprintk("lockd: created nsm_handle for %s (%s)\n",
77a3ef33
CL
339 new->sm_name, new->sm_addrbuf);
340 return new;
67c6d107 341 }
77a3ef33 342
67c6d107
CL
343 spin_unlock(&nsm_lock);
344
77a3ef33
CL
345 new = nsm_create_handle(sap, salen, hostname, hostname_len);
346 if (unlikely(new == NULL))
67c6d107 347 return NULL;
67c6d107 348 goto retry;
67c6d107
CL
349}
350
3420a8c4
CL
351/**
352 * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
353 * @info: pointer to NLMPROC_SM_NOTIFY arguments
354 *
7e469af9
JL
355 * Returns a matching nsm_handle if found in the nsm cache. The returned
356 * nsm_handle's reference count is bumped. Otherwise returns NULL if some
357 * error occurred.
3420a8c4
CL
358 */
359struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
360{
361 struct nsm_handle *cached;
362
363 spin_lock(&nsm_lock);
364
94da7663 365 cached = nsm_lookup_priv(&info->priv);
3420a8c4
CL
366 if (unlikely(cached == NULL)) {
367 spin_unlock(&nsm_lock);
368 dprintk("lockd: never saw rebooted peer '%.*s' before\n",
369 info->len, info->mon);
370 return cached;
371 }
372
373 atomic_inc(&cached->sm_count);
374 spin_unlock(&nsm_lock);
375
3420a8c4
CL
376 dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
377 cached->sm_name, cached->sm_addrbuf,
378 atomic_read(&cached->sm_count));
379 return cached;
380}
381
67c6d107
CL
382/**
383 * nsm_release - Release an NSM handle
384 * @nsm: pointer to handle to be released
385 *
386 */
387void nsm_release(struct nsm_handle *nsm)
388{
67c6d107
CL
389 if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
390 list_del(&nsm->sm_link);
391 spin_unlock(&nsm_lock);
5cf1c4b1
CL
392 dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
393 nsm->sm_name, nsm->sm_addrbuf);
67c6d107
CL
394 kfree(nsm);
395 }
396}
397
1da177e4
LT
398/*
399 * XDR functions for NSM.
2ca7754d
CL
400 *
401 * See http://www.opengroup.org/ for details on the Network
402 * Status Monitor wire protocol.
1da177e4
LT
403 */
404
49b17004 405static void encode_nsm_string(struct xdr_stream *xdr, const char *string)
099bd05f 406{
03eb1dcb
CL
407 const u32 len = strlen(string);
408 __be32 *p;
409
49b17004
CL
410 BUG_ON(len > SM_MAXSTRLEN);
411 p = xdr_reserve_space(xdr, 4 + len);
03eb1dcb 412 xdr_encode_opaque(p, string, len);
099bd05f
CL
413}
414
49695174
CL
415/*
416 * "mon_name" specifies the host to be monitored.
49695174 417 */
49b17004 418static void encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
49695174 419{
49b17004 420 encode_nsm_string(xdr, argp->mon_name);
49695174
CL
421}
422
850c95fd
CL
423/*
424 * The "my_id" argument specifies the hostname and RPC procedure
425 * to be called when the status manager receives notification
36e8e668 426 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
850c95fd
CL
427 * has changed.
428 */
49b17004 429static void encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
850c95fd 430{
03eb1dcb
CL
431 __be32 *p;
432
49b17004
CL
433 encode_nsm_string(xdr, utsname()->nodename);
434 p = xdr_reserve_space(xdr, 4 + 4 + 4);
435 *p++ = cpu_to_be32(argp->prog);
436 *p++ = cpu_to_be32(argp->vers);
437 *p = cpu_to_be32(argp->proc);
850c95fd
CL
438}
439
ea72a7f1
CL
440/*
441 * The "mon_id" argument specifies the non-private arguments
36e8e668 442 * of an NSMPROC_MON or NSMPROC_UNMON call.
ea72a7f1 443 */
49b17004 444static void encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
ea72a7f1 445{
49b17004
CL
446 encode_mon_name(xdr, argp);
447 encode_my_id(xdr, argp);
ea72a7f1
CL
448}
449
0490a54a
CL
450/*
451 * The "priv" argument may contain private information required
36e8e668
CL
452 * by the NSMPROC_MON call. This information will be supplied in the
453 * NLMPROC_SM_NOTIFY call.
0490a54a 454 */
49b17004 455static void encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
0490a54a 456{
03eb1dcb
CL
457 __be32 *p;
458
459 p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
cab2d3c9 460 xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
0490a54a
CL
461}
462
9f06c719
CL
463static void nsm_xdr_enc_mon(struct rpc_rqst *req, struct xdr_stream *xdr,
464 const struct nsm_args *argp)
1da177e4 465{
9f06c719
CL
466 encode_mon_id(xdr, argp);
467 encode_priv(xdr, argp);
1da177e4
LT
468}
469
9f06c719
CL
470static void nsm_xdr_enc_unmon(struct rpc_rqst *req, struct xdr_stream *xdr,
471 const struct nsm_args *argp)
1da177e4 472{
9f06c719 473 encode_mon_id(xdr, argp);
1da177e4
LT
474}
475
bf269551
CL
476static int nsm_xdr_dec_stat_res(struct rpc_rqst *rqstp,
477 struct xdr_stream *xdr,
478 struct nsm_res *resp)
1da177e4 479{
bf269551 480 __be32 *p;
03eb1dcb 481
bf269551 482 p = xdr_inline_decode(xdr, 4 + 4);
03eb1dcb
CL
483 if (unlikely(p == NULL))
484 return -EIO;
49b17004
CL
485 resp->status = be32_to_cpup(p++);
486 resp->state = be32_to_cpup(p);
03eb1dcb 487
bf269551
CL
488 dprintk("lockd: %s status %d state %d\n",
489 __func__, resp->status, resp->state);
1da177e4
LT
490 return 0;
491}
492
bf269551
CL
493static int nsm_xdr_dec_stat(struct rpc_rqst *rqstp,
494 struct xdr_stream *xdr,
495 struct nsm_res *resp)
1da177e4 496{
bf269551 497 __be32 *p;
03eb1dcb 498
bf269551 499 p = xdr_inline_decode(xdr, 4);
03eb1dcb
CL
500 if (unlikely(p == NULL))
501 return -EIO;
49b17004 502 resp->state = be32_to_cpup(p);
03eb1dcb 503
bf269551 504 dprintk("lockd: %s state %d\n", __func__, resp->state);
1da177e4
LT
505 return 0;
506}
507
508#define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
2ca7754d
CL
509#define SM_my_id_sz (SM_my_name_sz+3)
510#define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
511#define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
0490a54a
CL
512#define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
513#define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
1da177e4
LT
514#define SM_monres_sz 2
515#define SM_unmonres_sz 1
516
1da177e4 517static struct rpc_procinfo nsm_procedures[] = {
36e8e668
CL
518[NSMPROC_MON] = {
519 .p_proc = NSMPROC_MON,
9f06c719 520 .p_encode = (kxdreproc_t)nsm_xdr_enc_mon,
bf269551 521 .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat_res,
2bea90d4
CL
522 .p_arglen = SM_mon_sz,
523 .p_replen = SM_monres_sz,
36e8e668 524 .p_statidx = NSMPROC_MON,
cc0175c1 525 .p_name = "MONITOR",
1da177e4 526 },
36e8e668
CL
527[NSMPROC_UNMON] = {
528 .p_proc = NSMPROC_UNMON,
9f06c719 529 .p_encode = (kxdreproc_t)nsm_xdr_enc_unmon,
bf269551 530 .p_decode = (kxdrdproc_t)nsm_xdr_dec_stat,
2bea90d4
CL
531 .p_arglen = SM_mon_id_sz,
532 .p_replen = SM_unmonres_sz,
36e8e668 533 .p_statidx = NSMPROC_UNMON,
cc0175c1 534 .p_name = "UNMONITOR",
1da177e4
LT
535 },
536};
537
a613fa16 538static const struct rpc_version nsm_version1 = {
e8c96f8c
TK
539 .number = 1,
540 .nrprocs = ARRAY_SIZE(nsm_procedures),
1da177e4
LT
541 .procs = nsm_procedures
542};
543
a613fa16 544static const struct rpc_version *nsm_version[] = {
1da177e4
LT
545 [1] = &nsm_version1,
546};
547
548static struct rpc_stat nsm_stats;
549
a613fa16 550static const struct rpc_program nsm_program = {
1da177e4 551 .name = "statd",
36e8e668 552 .number = NSM_PROGRAM,
e8c96f8c 553 .nrvers = ARRAY_SIZE(nsm_version),
1da177e4
LT
554 .version = nsm_version,
555 .stats = &nsm_stats
556};
This page took 0.64102 seconds and 5 git commands to generate.