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