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