8ae4c02d7dfda7e8ab60154e5975de496749d7da
[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/ktime.h>
13
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/xprtsock.h>
16 #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
22 #define NSM_PROGRAM             100024
23 #define NSM_VERSION             1
24
25 enum {
26         NSMPROC_NULL,
27         NSMPROC_STAT,
28         NSMPROC_MON,
29         NSMPROC_UNMON,
30         NSMPROC_UNMON_ALL,
31         NSMPROC_SIMU_CRASH,
32         NSMPROC_NOTIFY,
33 };
34
35 struct nsm_args {
36         struct nsm_private      *priv;
37         u32                     prog;           /* RPC callback info */
38         u32                     vers;
39         u32                     proc;
40
41         char                    *mon_name;
42 };
43
44 struct nsm_res {
45         u32                     status;
46         u32                     state;
47 };
48
49 static struct rpc_clnt *        nsm_create(void);
50
51 static struct rpc_program       nsm_program;
52 static                          LIST_HEAD(nsm_handles);
53 static                          DEFINE_SPINLOCK(nsm_lock);
54
55 /*
56  * Local NSM state
57  */
58 int                             nsm_local_state;
59
60 static 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
67 static 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
81 static 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
97 /*
98  * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
99  */
100 static int
101 nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
102 {
103         struct rpc_clnt *clnt;
104         int             status;
105         struct nsm_args args = {
106                 .priv           = &nsm->sm_priv,
107                 .prog           = NLM_PROGRAM,
108                 .vers           = 3,
109                 .proc           = NLMPROC_NSM_NOTIFY,
110                 .mon_name       = nsm->sm_mon_name,
111         };
112         struct rpc_message msg = {
113                 .rpc_argp       = &args,
114                 .rpc_resp       = res,
115         };
116
117         clnt = nsm_create();
118         if (IS_ERR(clnt)) {
119                 status = PTR_ERR(clnt);
120                 dprintk("lockd: failed to create NSM upcall transport, "
121                                 "status=%d\n", status);
122                 goto out;
123         }
124
125         memset(res, 0, sizeof(*res));
126
127         msg.rpc_proc = &clnt->cl_procinfo[proc];
128         status = rpc_call_sync(clnt, &msg, 0);
129         if (status < 0)
130                 dprintk("lockd: NSM upcall RPC failed, status=%d\n",
131                                 status);
132         else
133                 status = 0;
134         rpc_shutdown_client(clnt);
135  out:
136         return status;
137 }
138
139 /**
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.
149  */
150 int nsm_monitor(const struct nlm_host *host)
151 {
152         struct nsm_handle *nsm = host->h_nsmhandle;
153         struct nsm_res  res;
154         int             status;
155
156         dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
157
158         if (nsm->sm_monitored)
159                 return 0;
160
161         /*
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
167         status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
168         if (res.status != 0)
169                 status = -EIO;
170         if (status < 0)
171                 printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
172         else
173                 nsm->sm_monitored = 1;
174         return status;
175 }
176
177 /**
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.
184  */
185 void nsm_unmonitor(const struct nlm_host *host)
186 {
187         struct nsm_handle *nsm = host->h_nsmhandle;
188         struct nsm_res  res;
189         int status;
190
191         if (atomic_read(&nsm->sm_count) == 1
192          && nsm->sm_monitored && !nsm->sm_sticky) {
193                 dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
194
195                 status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
196                 if (res.status != 0)
197                         status = -EIO;
198                 if (status < 0)
199                         printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
200                                         nsm->sm_name);
201                 else
202                         nsm->sm_monitored = 0;
203         }
204 }
205
206 static 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
218 static 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
228 static 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
239 /*
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  *
245  * 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.
255  */
256 static void nsm_init_private(struct nsm_handle *nsm)
257 {
258         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;
264 }
265
266 static 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
290 /**
291  * nsm_get_handle - Find or create a cached nsm_handle
292  * @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
296  *
297  * Behavior is modulated by the global nsm_use_hostnames variable.
298  *
299  * 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.
303  */
304 struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
305                                   const size_t salen, const char *hostname,
306                                   const size_t hostname_len)
307 {
308         struct nsm_handle *cached, *new = NULL;
309
310         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
319 retry:
320         spin_lock(&nsm_lock);
321
322         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;
336         }
337
338         if (new != NULL) {
339                 list_add(&new->sm_link, &nsm_handles);
340                 spin_unlock(&nsm_lock);
341                 dprintk("lockd: created nsm_handle for %s (%s)\n",
342                                 new->sm_name, new->sm_addrbuf);
343                 return new;
344         }
345
346         spin_unlock(&nsm_lock);
347
348         new = nsm_create_handle(sap, salen, hostname, hostname_len);
349         if (unlikely(new == NULL))
350                 return NULL;
351         goto retry;
352 }
353
354 /**
355  * 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  */
362 struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
363 {
364         struct nsm_handle *cached;
365
366         spin_lock(&nsm_lock);
367
368         cached = nsm_lookup_priv(&info->priv);
369         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 /**
392  * nsm_release - Release an NSM handle
393  * @nsm: pointer to handle to be released
394  *
395  */
396 void nsm_release(struct nsm_handle *nsm)
397 {
398         if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
399                 list_del(&nsm->sm_link);
400                 spin_unlock(&nsm_lock);
401                 dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
402                                 nsm->sm_name, nsm->sm_addrbuf);
403                 kfree(nsm);
404         }
405 }
406
407 /*
408  * Create NSM client for the local host
409  */
410 static struct rpc_clnt *
411 nsm_create(void)
412 {
413         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 = {
419                 .protocol       = XPRT_TRANSPORT_UDP,
420                 .address        = (struct sockaddr *)&sin,
421                 .addrsize       = sizeof(sin),
422                 .servername     = "localhost",
423                 .program        = &nsm_program,
424                 .version        = NSM_VERSION,
425                 .authflavor     = RPC_AUTH_NULL,
426         };
427
428         return rpc_create(&args);
429 }
430
431 /*
432  * XDR functions for NSM.
433  *
434  * See http://www.opengroup.org/ for details on the Network
435  * Status Monitor wire protocol.
436  */
437
438 static int encode_nsm_string(struct xdr_stream *xdr, const char *string)
439 {
440         const u32 len = strlen(string);
441         __be32 *p;
442
443         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;
450 }
451
452 /*
453  * "mon_name" specifies the host to be monitored.
454  */
455 static int encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
456 {
457         return encode_nsm_string(xdr, argp->mon_name);
458 }
459
460 /*
461  * The "my_id" argument specifies the hostname and RPC procedure
462  * to be called when the status manager receives notification
463  * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
464  * has changed.
465  */
466 static int encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
467 {
468         int status;
469         __be32 *p;
470
471         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;
477         *p++ = htonl(argp->prog);
478         *p++ = htonl(argp->vers);
479         *p++ = htonl(argp->proc);
480         return 0;
481 }
482
483 /*
484  * The "mon_id" argument specifies the non-private arguments
485  * of an NSMPROC_MON or NSMPROC_UNMON call.
486  */
487 static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
488 {
489         int status;
490
491         status = encode_mon_name(xdr, argp);
492         if (unlikely(status != 0))
493                 return status;
494         return encode_my_id(xdr, argp);
495 }
496
497 /*
498  * The "priv" argument may contain private information required
499  * by the NSMPROC_MON call. This information will be supplied in the
500  * NLMPROC_SM_NOTIFY call.
501  */
502 static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
503 {
504         __be32 *p;
505
506         p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
507         if (unlikely(p == NULL))
508                 return -EIO;
509         xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
510         return 0;
511 }
512
513 static int xdr_enc_mon(struct rpc_rqst *req, __be32 *p,
514                        const struct nsm_args *argp)
515 {
516         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);
524 }
525
526 static int xdr_enc_unmon(struct rpc_rqst *req, __be32 *p,
527                          const struct nsm_args *argp)
528 {
529         struct xdr_stream xdr;
530
531         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
532         return encode_mon_id(&xdr, argp);
533 }
534
535 static 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;
544         resp->status = ntohl(*p++);
545         resp->state = ntohl(*p);
546
547         dprintk("lockd: xdr_dec_stat_res status %d state %d\n",
548                         resp->status, resp->state);
549         return 0;
550 }
551
552 static int xdr_dec_stat(struct rpc_rqst *rqstp, __be32 *p,
553                         struct nsm_res *resp)
554 {
555         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);
564         return 0;
565 }
566
567 #define SM_my_name_sz   (1+XDR_QUADLEN(SM_MAXSTRLEN))
568 #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)
571 #define SM_priv_sz      (XDR_QUADLEN(SM_PRIV_SIZE))
572 #define SM_mon_sz       (SM_mon_id_sz+SM_priv_sz)
573 #define SM_monres_sz    2
574 #define SM_unmonres_sz  1
575
576 static struct rpc_procinfo      nsm_procedures[] = {
577 [NSMPROC_MON] = {
578                 .p_proc         = NSMPROC_MON,
579                 .p_encode       = (kxdrproc_t)xdr_enc_mon,
580                 .p_decode       = (kxdrproc_t)xdr_dec_stat_res,
581                 .p_arglen       = SM_mon_sz,
582                 .p_replen       = SM_monres_sz,
583                 .p_statidx      = NSMPROC_MON,
584                 .p_name         = "MONITOR",
585         },
586 [NSMPROC_UNMON] = {
587                 .p_proc         = NSMPROC_UNMON,
588                 .p_encode       = (kxdrproc_t)xdr_enc_unmon,
589                 .p_decode       = (kxdrproc_t)xdr_dec_stat,
590                 .p_arglen       = SM_mon_id_sz,
591                 .p_replen       = SM_unmonres_sz,
592                 .p_statidx      = NSMPROC_UNMON,
593                 .p_name         = "UNMONITOR",
594         },
595 };
596
597 static struct rpc_version       nsm_version1 = {
598                 .number         = 1,
599                 .nrprocs        = ARRAY_SIZE(nsm_procedures),
600                 .procs          = nsm_procedures
601 };
602
603 static struct rpc_version *     nsm_version[] = {
604         [1] = &nsm_version1,
605 };
606
607 static struct rpc_stat          nsm_stats;
608
609 static struct rpc_program       nsm_program = {
610                 .name           = "statd",
611                 .number         = NSM_PROGRAM,
612                 .nrvers         = ARRAY_SIZE(nsm_version),
613                 .version        = nsm_version,
614                 .stats          = &nsm_stats
615 };