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