NLM: Refactor make_socks() function
[safe/jmp/linux-2.6] / fs / lockd / svc.c
1 /*
2  * linux/fs/lockd/svc.c
3  *
4  * This is the central lockd service.
5  *
6  * FIXME: Separate the lockd NFS server functionality from the lockd NFS
7  *        client functionality. Oh why didn't Sun create two separate
8  *        services in the first place?
9  *
10  * Authors:     Olaf Kirch (okir@monad.swb.de)
11  *
12  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13  */
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/sysctl.h>
18 #include <linux/moduleparam.h>
19
20 #include <linux/sched.h>
21 #include <linux/errno.h>
22 #include <linux/in.h>
23 #include <linux/uio.h>
24 #include <linux/slab.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/mutex.h>
28 #include <linux/kthread.h>
29 #include <linux/freezer.h>
30
31 #include <linux/sunrpc/types.h>
32 #include <linux/sunrpc/stats.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/svc.h>
35 #include <linux/sunrpc/svcsock.h>
36 #include <net/ip.h>
37 #include <linux/lockd/lockd.h>
38 #include <linux/nfs.h>
39
40 #define NLMDBG_FACILITY         NLMDBG_SVC
41 #define LOCKD_BUFSIZE           (1024 + NLMSVC_XDRSIZE)
42 #define ALLOWED_SIGS            (sigmask(SIGKILL))
43
44 static struct svc_program       nlmsvc_program;
45
46 struct nlmsvc_binding *         nlmsvc_ops;
47 EXPORT_SYMBOL_GPL(nlmsvc_ops);
48
49 static DEFINE_MUTEX(nlmsvc_mutex);
50 static unsigned int             nlmsvc_users;
51 static struct task_struct       *nlmsvc_task;
52 static struct svc_rqst          *nlmsvc_rqst;
53 unsigned long                   nlmsvc_timeout;
54
55 /*
56  * If the kernel has IPv6 support available, always listen for
57  * both AF_INET and AF_INET6 requests.
58  */
59 #if (defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) && \
60         defined(CONFIG_SUNRPC_REGISTER_V4)
61 static const sa_family_t        nlmsvc_family = AF_INET6;
62 #else   /* (CONFIG_IPV6 || CONFIG_IPV6_MODULE) && CONFIG_SUNRPC_REGISTER_V4 */
63 static const sa_family_t        nlmsvc_family = AF_INET;
64 #endif  /* (CONFIG_IPV6 || CONFIG_IPV6_MODULE) && CONFIG_SUNRPC_REGISTER_V4 */
65
66 /*
67  * These can be set at insmod time (useful for NFS as root filesystem),
68  * and also changed through the sysctl interface.  -- Jamie Lokier, Aug 2003
69  */
70 static unsigned long            nlm_grace_period;
71 static unsigned long            nlm_timeout = LOCKD_DFLT_TIMEO;
72 static int                      nlm_udpport, nlm_tcpport;
73
74 /* RLIM_NOFILE defaults to 1024. That seems like a reasonable default here. */
75 static unsigned int             nlm_max_connections = 1024;
76
77 /*
78  * Constants needed for the sysctl interface.
79  */
80 static const unsigned long      nlm_grace_period_min = 0;
81 static const unsigned long      nlm_grace_period_max = 240;
82 static const unsigned long      nlm_timeout_min = 3;
83 static const unsigned long      nlm_timeout_max = 20;
84 static const int                nlm_port_min = 0, nlm_port_max = 65535;
85
86 #ifdef CONFIG_SYSCTL
87 static struct ctl_table_header * nlm_sysctl_table;
88 #endif
89
90 static unsigned long get_lockd_grace_period(void)
91 {
92         /* Note: nlm_timeout should always be nonzero */
93         if (nlm_grace_period)
94                 return roundup(nlm_grace_period, nlm_timeout) * HZ;
95         else
96                 return nlm_timeout * 5 * HZ;
97 }
98
99 static struct lock_manager lockd_manager = {
100 };
101
102 static void grace_ender(struct work_struct *not_used)
103 {
104         locks_end_grace(&lockd_manager);
105 }
106
107 static DECLARE_DELAYED_WORK(grace_period_end, grace_ender);
108
109 static void set_grace_period(void)
110 {
111         unsigned long grace_period = get_lockd_grace_period();
112
113         locks_start_grace(&lockd_manager);
114         cancel_delayed_work_sync(&grace_period_end);
115         schedule_delayed_work(&grace_period_end, grace_period);
116 }
117
118 /*
119  * This is the lockd kernel thread
120  */
121 static int
122 lockd(void *vrqstp)
123 {
124         int             err = 0, preverr = 0;
125         struct svc_rqst *rqstp = vrqstp;
126
127         /* try_to_freeze() is called from svc_recv() */
128         set_freezable();
129
130         /* Allow SIGKILL to tell lockd to drop all of its locks */
131         allow_signal(SIGKILL);
132
133         dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
134
135         /*
136          * FIXME: it would be nice if lockd didn't spend its entire life
137          * running under the BKL. At the very least, it would be good to
138          * have someone clarify what it's intended to protect here. I've
139          * seen some handwavy posts about posix locking needing to be
140          * done under the BKL, but it's far from clear.
141          */
142         lock_kernel();
143
144         if (!nlm_timeout)
145                 nlm_timeout = LOCKD_DFLT_TIMEO;
146         nlmsvc_timeout = nlm_timeout * HZ;
147
148         set_grace_period();
149
150         /*
151          * The main request loop. We don't terminate until the last
152          * NFS mount or NFS daemon has gone away.
153          */
154         while (!kthread_should_stop()) {
155                 long timeout = MAX_SCHEDULE_TIMEOUT;
156                 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
157
158                 /* update sv_maxconn if it has changed */
159                 rqstp->rq_server->sv_maxconn = nlm_max_connections;
160
161                 if (signalled()) {
162                         flush_signals(current);
163                         if (nlmsvc_ops) {
164                                 nlmsvc_invalidate_all();
165                                 set_grace_period();
166                         }
167                         continue;
168                 }
169
170                 timeout = nlmsvc_retry_blocked();
171
172                 /*
173                  * Find a socket with data available and call its
174                  * recvfrom routine.
175                  */
176                 err = svc_recv(rqstp, timeout);
177                 if (err == -EAGAIN || err == -EINTR) {
178                         preverr = err;
179                         continue;
180                 }
181                 if (err < 0) {
182                         if (err != preverr) {
183                                 printk(KERN_WARNING "%s: unexpected error "
184                                         "from svc_recv (%d)\n", __func__, err);
185                                 preverr = err;
186                         }
187                         schedule_timeout_interruptible(HZ);
188                         continue;
189                 }
190                 preverr = err;
191
192                 dprintk("lockd: request from %s\n",
193                                 svc_print_addr(rqstp, buf, sizeof(buf)));
194
195                 svc_process(rqstp);
196         }
197         flush_signals(current);
198         cancel_delayed_work_sync(&grace_period_end);
199         locks_end_grace(&lockd_manager);
200         if (nlmsvc_ops)
201                 nlmsvc_invalidate_all();
202         nlm_shutdown_hosts();
203         unlock_kernel();
204         return 0;
205 }
206
207 static int create_lockd_listener(struct svc_serv *serv, char *name,
208                                  unsigned short port)
209 {
210         struct svc_xprt *xprt;
211
212         xprt = svc_find_xprt(serv, name, 0, 0);
213         if (xprt == NULL)
214                 return svc_create_xprt(serv, name, port, SVC_SOCK_DEFAULTS);
215
216         svc_xprt_put(xprt);
217         return 0;
218 }
219
220 /*
221  * Ensure there are active UDP and TCP listeners for lockd.
222  *
223  * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
224  * local services (such as rpc.statd) still require UDP, and
225  * some NFS servers do not yet support NLM over TCP.
226  *
227  * Returns zero if all listeners are available; otherwise a
228  * negative errno value is returned.
229  */
230 static int make_socks(struct svc_serv *serv)
231 {
232         static int warned;
233         int err = 0;
234
235         err = create_lockd_listener(serv, "udp", nlm_udpport);
236         if (err >= 0)
237                 err = create_lockd_listener(serv, "tcp", nlm_tcpport);
238         if (err >= 0) {
239                 warned = 0;
240                 err = 0;
241         } else if (warned++ == 0)
242                 printk(KERN_WARNING
243                        "lockd_up: makesock failed, error=%d\n", err);
244         return err;
245 }
246
247 /*
248  * Bring up the lockd process if it's not already up.
249  */
250 int lockd_up(void)
251 {
252         struct svc_serv *serv;
253         int             error = 0;
254
255         mutex_lock(&nlmsvc_mutex);
256         /*
257          * Check whether we're already up and running.
258          */
259         if (nlmsvc_rqst)
260                 goto out;
261
262         /*
263          * Sanity check: if there's no pid,
264          * we should be the first user ...
265          */
266         if (nlmsvc_users)
267                 printk(KERN_WARNING
268                         "lockd_up: no pid, %d users??\n", nlmsvc_users);
269
270         error = -ENOMEM;
271         serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, nlmsvc_family, NULL);
272         if (!serv) {
273                 printk(KERN_WARNING "lockd_up: create service failed\n");
274                 goto out;
275         }
276
277         error = make_socks(serv);
278         if (error < 0)
279                 goto destroy_and_out;
280
281         /*
282          * Create the kernel thread and wait for it to start.
283          */
284         nlmsvc_rqst = svc_prepare_thread(serv, &serv->sv_pools[0]);
285         if (IS_ERR(nlmsvc_rqst)) {
286                 error = PTR_ERR(nlmsvc_rqst);
287                 nlmsvc_rqst = NULL;
288                 printk(KERN_WARNING
289                         "lockd_up: svc_rqst allocation failed, error=%d\n",
290                         error);
291                 goto destroy_and_out;
292         }
293
294         svc_sock_update_bufs(serv);
295         serv->sv_maxconn = nlm_max_connections;
296
297         nlmsvc_task = kthread_run(lockd, nlmsvc_rqst, serv->sv_name);
298         if (IS_ERR(nlmsvc_task)) {
299                 error = PTR_ERR(nlmsvc_task);
300                 svc_exit_thread(nlmsvc_rqst);
301                 nlmsvc_task = NULL;
302                 nlmsvc_rqst = NULL;
303                 printk(KERN_WARNING
304                         "lockd_up: kthread_run failed, error=%d\n", error);
305                 goto destroy_and_out;
306         }
307
308         /*
309          * Note: svc_serv structures have an initial use count of 1,
310          * so we exit through here on both success and failure.
311          */
312 destroy_and_out:
313         svc_destroy(serv);
314 out:
315         if (!error)
316                 nlmsvc_users++;
317         mutex_unlock(&nlmsvc_mutex);
318         return error;
319 }
320 EXPORT_SYMBOL_GPL(lockd_up);
321
322 /*
323  * Decrement the user count and bring down lockd if we're the last.
324  */
325 void
326 lockd_down(void)
327 {
328         mutex_lock(&nlmsvc_mutex);
329         if (nlmsvc_users) {
330                 if (--nlmsvc_users)
331                         goto out;
332         } else {
333                 printk(KERN_ERR "lockd_down: no users! task=%p\n",
334                         nlmsvc_task);
335                 BUG();
336         }
337
338         if (!nlmsvc_task) {
339                 printk(KERN_ERR "lockd_down: no lockd running.\n");
340                 BUG();
341         }
342         kthread_stop(nlmsvc_task);
343         svc_exit_thread(nlmsvc_rqst);
344         nlmsvc_task = NULL;
345         nlmsvc_rqst = NULL;
346 out:
347         mutex_unlock(&nlmsvc_mutex);
348 }
349 EXPORT_SYMBOL_GPL(lockd_down);
350
351 #ifdef CONFIG_SYSCTL
352
353 /*
354  * Sysctl parameters (same as module parameters, different interface).
355  */
356
357 static ctl_table nlm_sysctls[] = {
358         {
359                 .ctl_name       = CTL_UNNUMBERED,
360                 .procname       = "nlm_grace_period",
361                 .data           = &nlm_grace_period,
362                 .maxlen         = sizeof(unsigned long),
363                 .mode           = 0644,
364                 .proc_handler   = &proc_doulongvec_minmax,
365                 .extra1         = (unsigned long *) &nlm_grace_period_min,
366                 .extra2         = (unsigned long *) &nlm_grace_period_max,
367         },
368         {
369                 .ctl_name       = CTL_UNNUMBERED,
370                 .procname       = "nlm_timeout",
371                 .data           = &nlm_timeout,
372                 .maxlen         = sizeof(unsigned long),
373                 .mode           = 0644,
374                 .proc_handler   = &proc_doulongvec_minmax,
375                 .extra1         = (unsigned long *) &nlm_timeout_min,
376                 .extra2         = (unsigned long *) &nlm_timeout_max,
377         },
378         {
379                 .ctl_name       = CTL_UNNUMBERED,
380                 .procname       = "nlm_udpport",
381                 .data           = &nlm_udpport,
382                 .maxlen         = sizeof(int),
383                 .mode           = 0644,
384                 .proc_handler   = &proc_dointvec_minmax,
385                 .extra1         = (int *) &nlm_port_min,
386                 .extra2         = (int *) &nlm_port_max,
387         },
388         {
389                 .ctl_name       = CTL_UNNUMBERED,
390                 .procname       = "nlm_tcpport",
391                 .data           = &nlm_tcpport,
392                 .maxlen         = sizeof(int),
393                 .mode           = 0644,
394                 .proc_handler   = &proc_dointvec_minmax,
395                 .extra1         = (int *) &nlm_port_min,
396                 .extra2         = (int *) &nlm_port_max,
397         },
398         {
399                 .ctl_name       = CTL_UNNUMBERED,
400                 .procname       = "nsm_use_hostnames",
401                 .data           = &nsm_use_hostnames,
402                 .maxlen         = sizeof(int),
403                 .mode           = 0644,
404                 .proc_handler   = &proc_dointvec,
405         },
406         {
407                 .ctl_name       = CTL_UNNUMBERED,
408                 .procname       = "nsm_local_state",
409                 .data           = &nsm_local_state,
410                 .maxlen         = sizeof(int),
411                 .mode           = 0644,
412                 .proc_handler   = &proc_dointvec,
413         },
414         { .ctl_name = 0 }
415 };
416
417 static ctl_table nlm_sysctl_dir[] = {
418         {
419                 .ctl_name       = CTL_UNNUMBERED,
420                 .procname       = "nfs",
421                 .mode           = 0555,
422                 .child          = nlm_sysctls,
423         },
424         { .ctl_name = 0 }
425 };
426
427 static ctl_table nlm_sysctl_root[] = {
428         {
429                 .ctl_name       = CTL_FS,
430                 .procname       = "fs",
431                 .mode           = 0555,
432                 .child          = nlm_sysctl_dir,
433         },
434         { .ctl_name = 0 }
435 };
436
437 #endif  /* CONFIG_SYSCTL */
438
439 /*
440  * Module (and sysfs) parameters.
441  */
442
443 #define param_set_min_max(name, type, which_strtol, min, max)           \
444 static int param_set_##name(const char *val, struct kernel_param *kp)   \
445 {                                                                       \
446         char *endp;                                                     \
447         __typeof__(type) num = which_strtol(val, &endp, 0);             \
448         if (endp == val || *endp || num < (min) || num > (max))         \
449                 return -EINVAL;                                         \
450         *((int *) kp->arg) = num;                                       \
451         return 0;                                                       \
452 }
453
454 static inline int is_callback(u32 proc)
455 {
456         return proc == NLMPROC_GRANTED
457                 || proc == NLMPROC_GRANTED_MSG
458                 || proc == NLMPROC_TEST_RES
459                 || proc == NLMPROC_LOCK_RES
460                 || proc == NLMPROC_CANCEL_RES
461                 || proc == NLMPROC_UNLOCK_RES
462                 || proc == NLMPROC_NSM_NOTIFY;
463 }
464
465
466 static int lockd_authenticate(struct svc_rqst *rqstp)
467 {
468         rqstp->rq_client = NULL;
469         switch (rqstp->rq_authop->flavour) {
470                 case RPC_AUTH_NULL:
471                 case RPC_AUTH_UNIX:
472                         if (rqstp->rq_proc == 0)
473                                 return SVC_OK;
474                         if (is_callback(rqstp->rq_proc)) {
475                                 /* Leave it to individual procedures to
476                                  * call nlmsvc_lookup_host(rqstp)
477                                  */
478                                 return SVC_OK;
479                         }
480                         return svc_set_client(rqstp);
481         }
482         return SVC_DENIED;
483 }
484
485
486 param_set_min_max(port, int, simple_strtol, 0, 65535)
487 param_set_min_max(grace_period, unsigned long, simple_strtoul,
488                   nlm_grace_period_min, nlm_grace_period_max)
489 param_set_min_max(timeout, unsigned long, simple_strtoul,
490                   nlm_timeout_min, nlm_timeout_max)
491
492 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
493 MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
494 MODULE_LICENSE("GPL");
495
496 module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
497                   &nlm_grace_period, 0644);
498 module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
499                   &nlm_timeout, 0644);
500 module_param_call(nlm_udpport, param_set_port, param_get_int,
501                   &nlm_udpport, 0644);
502 module_param_call(nlm_tcpport, param_set_port, param_get_int,
503                   &nlm_tcpport, 0644);
504 module_param(nsm_use_hostnames, bool, 0644);
505 module_param(nlm_max_connections, uint, 0644);
506
507 /*
508  * Initialising and terminating the module.
509  */
510
511 static int __init init_nlm(void)
512 {
513 #ifdef CONFIG_SYSCTL
514         nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root);
515         return nlm_sysctl_table ? 0 : -ENOMEM;
516 #else
517         return 0;
518 #endif
519 }
520
521 static void __exit exit_nlm(void)
522 {
523         /* FIXME: delete all NLM clients */
524         nlm_shutdown_hosts();
525 #ifdef CONFIG_SYSCTL
526         unregister_sysctl_table(nlm_sysctl_table);
527 #endif
528 }
529
530 module_init(init_nlm);
531 module_exit(exit_nlm);
532
533 /*
534  * Define NLM program and procedures
535  */
536 static struct svc_version       nlmsvc_version1 = {
537                 .vs_vers        = 1,
538                 .vs_nproc       = 17,
539                 .vs_proc        = nlmsvc_procedures,
540                 .vs_xdrsize     = NLMSVC_XDRSIZE,
541 };
542 static struct svc_version       nlmsvc_version3 = {
543                 .vs_vers        = 3,
544                 .vs_nproc       = 24,
545                 .vs_proc        = nlmsvc_procedures,
546                 .vs_xdrsize     = NLMSVC_XDRSIZE,
547 };
548 #ifdef CONFIG_LOCKD_V4
549 static struct svc_version       nlmsvc_version4 = {
550                 .vs_vers        = 4,
551                 .vs_nproc       = 24,
552                 .vs_proc        = nlmsvc_procedures4,
553                 .vs_xdrsize     = NLMSVC_XDRSIZE,
554 };
555 #endif
556 static struct svc_version *     nlmsvc_version[] = {
557         [1] = &nlmsvc_version1,
558         [3] = &nlmsvc_version3,
559 #ifdef CONFIG_LOCKD_V4
560         [4] = &nlmsvc_version4,
561 #endif
562 };
563
564 static struct svc_stat          nlmsvc_stats;
565
566 #define NLM_NRVERS      ARRAY_SIZE(nlmsvc_version)
567 static struct svc_program       nlmsvc_program = {
568         .pg_prog                = NLM_PROGRAM,          /* program number */
569         .pg_nvers               = NLM_NRVERS,           /* number of entries in nlmsvc_version */
570         .pg_vers                = nlmsvc_version,       /* version table */
571         .pg_name                = "lockd",              /* service name */
572         .pg_class               = "nfsd",               /* share authentication with nfsd */
573         .pg_stats               = &nlmsvc_stats,        /* stats table */
574         .pg_authenticate = &lockd_authenticate  /* export authentication */
575 };