- patches.fixes/patch-2.6.11-rc1: 2.6.11-rc1.
[linux-flexiantxendom0-3.2.10.git] / 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/config.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/sysctl.h>
19 #include <linux/moduleparam.h>
20
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/in.h>
24 #include <linux/uio.h>
25 #include <linux/slab.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
28
29 #include <linux/sunrpc/types.h>
30 #include <linux/sunrpc/stats.h>
31 #include <linux/sunrpc/clnt.h>
32 #include <linux/sunrpc/svc.h>
33 #include <linux/sunrpc/svcsock.h>
34 #include <linux/lockd/lockd.h>
35 #include <linux/lockd/sm_inter.h>
36 #include <linux/nfs.h>
37
38 #define NLMDBG_FACILITY         NLMDBG_SVC
39 #define LOCKD_BUFSIZE           (1024 + NLMSVC_XDRSIZE)
40 #define ALLOWED_SIGS            (sigmask(SIGKILL))
41
42 static struct svc_program       nlmsvc_program;
43 extern struct svc_program       nsmsvc_program;
44
45 struct nlmsvc_binding *         nlmsvc_ops;
46 EXPORT_SYMBOL(nlmsvc_ops);
47
48 static DECLARE_MUTEX(nlmsvc_sema);
49 static unsigned int             nlmsvc_users;
50 static pid_t                    nlmsvc_pid;
51 int                             nlmsvc_grace_period;
52 unsigned long                   nlmsvc_timeout;
53
54 static DECLARE_MUTEX_LOCKED(lockd_start);
55 static DECLARE_WAIT_QUEUE_HEAD(lockd_exit);
56
57 /*
58  * These can be set at insmod time (useful for NFS as root filesystem),
59  * and also changed through the sysctl interface.  -- Jamie Lokier, Aug 2003
60  */
61 static unsigned long            nlm_grace_period;
62 static unsigned long            nlm_timeout = LOCKD_DFLT_TIMEO;
63 static int                      nlm_udpport, nlm_tcpport;
64 static int                      nlm_use_kstatd = 1;
65
66 /*
67  * Constants needed for the sysctl interface.
68  */
69 static const unsigned long      nlm_grace_period_min = 0;
70 static const unsigned long      nlm_grace_period_max = 240;
71 static const unsigned long      nlm_timeout_min = 3;
72 static const unsigned long      nlm_timeout_max = 20;
73 static const int                nlm_port_min = 0, nlm_port_max = 65535;
74
75 static struct ctl_table_header * nlm_sysctl_table;
76
77 static unsigned long set_grace_period(void)
78 {
79         unsigned long grace_period;
80
81         /* Note: nlm_timeout should always be nonzero */
82         if (nlm_grace_period)
83                 grace_period = ((nlm_grace_period + nlm_timeout - 1)
84                                 / nlm_timeout) * nlm_timeout * HZ;
85         else
86                 grace_period = nlm_timeout * 5 * HZ;
87         nlmsvc_grace_period = 1;
88         return grace_period + jiffies;
89 }
90
91 static inline void clear_grace_period(void)
92 {
93         nlmsvc_grace_period = 0;
94 }
95
96 /*
97  * This is the lockd kernel thread
98  */
99 static void
100 lockd(struct svc_rqst *rqstp)
101 {
102         struct svc_serv *serv = rqstp->rq_server;
103         int             err = 0;
104         unsigned long grace_period_expire;
105
106         /* Lock module and set up kernel thread */
107         /* lockd_up is waiting for us to startup, so will
108          * be holding a reference to this module, so it
109          * is safe to just claim another reference
110          */
111         __module_get(THIS_MODULE);
112         lock_kernel();
113
114         /*
115          * Let our maker know we're running.
116          */
117         nlmsvc_pid = current->pid;
118         up(&lockd_start);
119
120         daemonize("lockd");
121
122         /* Set up statd */
123 #ifdef CONFIG_STATD
124         if (!nlm_use_kstatd || nsm_kernel_statd_init() < 0)
125                 nsm_statd_upcalls_init();
126 #else
127         nsm_statd_upcalls_init();
128 #endif
129
130         /* Process request with signals blocked, but allow SIGKILL.  */
131         allow_signal(SIGKILL);
132
133         /* kick rpciod */
134         rpciod_up();
135
136 #ifndef CONFIG_STATD
137         dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
138 #else
139         dprintk("NFS lockd/statd started (ver " LOCKD_VERSION ").\n");
140 #endif
141
142         if (!nlm_timeout)
143                 nlm_timeout = LOCKD_DFLT_TIMEO;
144         nlmsvc_timeout = nlm_timeout * HZ;
145
146         grace_period_expire = set_grace_period();
147
148         /*
149          * The main request loop. We don't terminate until the last
150          * NFS mount or NFS daemon has gone away, and we've been sent a
151          * signal, or else another process has taken over our job.
152          */
153         while ((nlmsvc_users || !signalled()) && nlmsvc_pid == current->pid) {
154                 long timeout = MAX_SCHEDULE_TIMEOUT;
155
156                 if (signalled()) {
157                         flush_signals(current);
158                         if (nlmsvc_ops) {
159                                 nlmsvc_invalidate_all();
160                                 grace_period_expire = set_grace_period();
161                         }
162                 }
163
164                 /*
165                  * Retry any blocked locks that have been notified by
166                  * the VFS. Don't do this during grace period.
167                  * (Theoretically, there shouldn't even be blocked locks
168                  * during grace period).
169                  */
170                 if (!nlmsvc_grace_period) {
171                         timeout = nlmsvc_retry_blocked();
172                 } else if (time_before(grace_period_expire, jiffies))
173                         clear_grace_period();
174
175                 /*
176                  * Find a socket with data available and call its
177                  * recvfrom routine.
178                  */
179                 err = svc_recv(serv, rqstp, timeout);
180                 if (err == -EAGAIN || err == -EINTR)
181                         continue;
182                 if (err < 0) {
183                         printk(KERN_WARNING
184                                "lockd: terminating on error %d\n",
185                                -err);
186                         break;
187                 }
188
189                 dprintk("lockd: request from %08x\n",
190                         (unsigned)ntohl(rqstp->rq_addr.sin_addr.s_addr));
191
192                 svc_process(serv, rqstp);
193
194         }
195
196         /*
197          * Check whether there's a new lockd process before
198          * shutting down the hosts and clearing the slot.
199          */
200         if (!nlmsvc_pid || current->pid == nlmsvc_pid) {
201                 if (nlmsvc_ops)
202                         nlmsvc_invalidate_all();
203                 nlm_shutdown_hosts();
204                 nlmsvc_pid = 0;
205         } else
206                 printk(KERN_DEBUG
207                         "lockd: new process, skipping host shutdown\n");
208         wake_up(&lockd_exit);
209                 
210         /* Exit the RPC thread */
211         svc_exit_thread(rqstp);
212
213         /* release rpciod */
214         rpciod_down();
215
216         /* Release module */
217         unlock_kernel();
218         module_put_and_exit(0);
219 }
220
221 /*
222  * Bring up the lockd process if it's not already up.
223  */
224 int
225 lockd_up(void)
226 {
227         static int              warned;
228         struct svc_program *    prog;
229         struct svc_serv *       serv;
230         int                     error = 0;
231
232         down(&nlmsvc_sema);
233         /*
234          * Unconditionally increment the user count ... this is
235          * the number of clients who _want_ a lockd process.
236          */
237         nlmsvc_users++; 
238         /*
239          * Check whether we're already up and running.
240          */
241         if (nlmsvc_pid)
242                 goto out;
243
244         /*
245          * Sanity check: if there's no pid,
246          * we should be the first user ...
247          */
248         if (nlmsvc_users > 1)
249                 printk(KERN_WARNING
250                         "lockd_up: no pid, %d users??\n", nlmsvc_users);
251
252         /* Register NLM program and possibly NSM (if using kstatd) */
253         error = -ENOMEM;
254         prog = &nlmsvc_program;
255 #ifdef CONFIG_STATD
256         if (nlm_use_kstatd)
257                 prog = &nsmsvc_program;
258 #endif
259         serv = svc_create(prog, LOCKD_BUFSIZE);
260         if (!serv) {
261                 printk(KERN_WARNING "lockd_up: create service failed\n");
262                 goto out;
263         }
264
265         if ((error = svc_makesock(serv, IPPROTO_UDP, nlm_udpport)) < 0 
266 #ifdef CONFIG_NFSD_TCP
267          || (error = svc_makesock(serv, IPPROTO_TCP, nlm_tcpport)) < 0
268 #endif
269                 ) {
270                 if (warned++ == 0) 
271                         printk(KERN_WARNING
272                                 "lockd_up: makesock failed, error=%d\n", error);
273                 goto destroy_and_out;
274         } 
275         warned = 0;
276
277         /*
278          * Create the kernel thread and wait for it to start.
279          */
280         error = svc_create_thread(lockd, serv);
281         if (error) {
282                 printk(KERN_WARNING
283                         "lockd_up: create thread failed, error=%d\n", error);
284                 goto destroy_and_out;
285         }
286         down(&lockd_start);
287
288         /*
289          * Note: svc_serv structures have an initial use count of 1,
290          * so we exit through here on both success and failure.
291          */
292 destroy_and_out:
293         svc_destroy(serv);
294 out:
295         up(&nlmsvc_sema);
296         return error;
297 }
298 EXPORT_SYMBOL(lockd_up);
299
300 /*
301  * Decrement the user count and bring down lockd if we're the last.
302  */
303 void
304 lockd_down(void)
305 {
306         static int warned;
307
308         down(&nlmsvc_sema);
309         if (nlmsvc_users) {
310                 if (--nlmsvc_users)
311                         goto out;
312         } else
313                 printk(KERN_WARNING "lockd_down: no users! pid=%d\n", nlmsvc_pid);
314
315         if (!nlmsvc_pid) {
316                 if (warned++ == 0)
317                         printk(KERN_WARNING "lockd_down: no lockd running.\n"); 
318                 goto out;
319         }
320         warned = 0;
321
322         kill_proc(nlmsvc_pid, SIGKILL, 1);
323         /*
324          * Wait for the lockd process to exit, but since we're holding
325          * the lockd semaphore, we can't wait around forever ...
326          */
327         clear_thread_flag(TIF_SIGPENDING);
328         interruptible_sleep_on_timeout(&lockd_exit, HZ);
329         if (nlmsvc_pid) {
330                 printk(KERN_WARNING 
331                         "lockd_down: lockd failed to exit, clearing pid\n");
332                 nlmsvc_pid = 0;
333         }
334         spin_lock_irq(&current->sighand->siglock);
335         recalc_sigpending();
336         spin_unlock_irq(&current->sighand->siglock);
337 out:
338         up(&nlmsvc_sema);
339 }
340 EXPORT_SYMBOL(lockd_down);
341
342 /*
343  * Sysctl parameters (same as module parameters, different interface).
344  */
345
346 /* Something that isn't CTL_ANY, CTL_NONE or a value that may clash. */
347 #define CTL_UNNUMBERED          -2
348
349 static ctl_table nlm_sysctls[] = {
350         {
351                 .ctl_name       = CTL_UNNUMBERED,
352                 .procname       = "nlm_grace_period",
353                 .data           = &nlm_grace_period,
354                 .maxlen         = sizeof(int),
355                 .mode           = 0644,
356                 .proc_handler   = &proc_doulongvec_minmax,
357                 .extra1         = (unsigned long *) &nlm_grace_period_min,
358                 .extra2         = (unsigned long *) &nlm_grace_period_max,
359         },
360         {
361                 .ctl_name       = CTL_UNNUMBERED,
362                 .procname       = "nlm_timeout",
363                 .data           = &nlm_timeout,
364                 .maxlen         = sizeof(int),
365                 .mode           = 0644,
366                 .proc_handler   = &proc_doulongvec_minmax,
367                 .extra1         = (unsigned long *) &nlm_timeout_min,
368                 .extra2         = (unsigned long *) &nlm_timeout_max,
369         },
370         {
371                 .ctl_name       = CTL_UNNUMBERED,
372                 .procname       = "nlm_udpport",
373                 .data           = &nlm_udpport,
374                 .maxlen         = sizeof(int),
375                 .mode           = 0644,
376                 .proc_handler   = &proc_dointvec_minmax,
377                 .extra1         = (int *) &nlm_port_min,
378                 .extra2         = (int *) &nlm_port_max,
379         },
380         {
381                 .ctl_name       = CTL_UNNUMBERED,
382                 .procname       = "nlm_tcpport",
383                 .data           = &nlm_tcpport,
384                 .maxlen         = sizeof(int),
385                 .mode           = 0644,
386                 .proc_handler   = &proc_dointvec_minmax,
387                 .extra1         = (int *) &nlm_port_min,
388                 .extra2         = (int *) &nlm_port_max,
389         },
390 #ifdef CONFIG_STATD
391         {
392                 .ctl_name       = CTL_UNNUMBERED,
393                 .procname       = "nlm_use_kstatd",
394                 .data           = &nlm_use_kstatd,
395                 .maxlen         = sizeof(int),
396                 .mode           = 0644,
397                 .proc_handler   = &proc_dointvec,
398         },
399 #endif
400         { .ctl_name = 0 }
401 };
402
403 static ctl_table nlm_sysctl_dir[] = {
404         {
405                 .ctl_name       = CTL_UNNUMBERED,
406                 .procname       = "nfs",
407                 .mode           = 0555,
408                 .child          = nlm_sysctls,
409         },
410         { .ctl_name = 0 }
411 };
412
413 static ctl_table nlm_sysctl_root[] = {
414         {
415                 .ctl_name       = CTL_FS,
416                 .procname       = "fs",
417                 .mode           = 0555,
418                 .child          = nlm_sysctl_dir,
419         },
420         { .ctl_name = 0 }
421 };
422
423 /*
424  * Module (and driverfs) parameters.
425  */
426
427 #define param_set_min_max(name, type, which_strtol, min, max)           \
428 static int param_set_##name(const char *val, struct kernel_param *kp)   \
429 {                                                                       \
430         char *endp;                                                     \
431         __typeof__(type) num = which_strtol(val, &endp, 0);             \
432         if (endp == val || *endp || num < (min) || num > (max))         \
433                 return -EINVAL;                                         \
434         *((int *) kp->arg) = num;                                       \
435         return 0;                                                       \
436 }
437
438 param_set_min_max(port, int, simple_strtol, 0, 65535)
439 param_set_min_max(grace_period, unsigned long, simple_strtoul,
440                   nlm_grace_period_min, nlm_grace_period_max)
441 param_set_min_max(timeout, unsigned long, simple_strtoul,
442                   nlm_timeout_min, nlm_timeout_max)
443
444 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
445 MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
446 MODULE_LICENSE("GPL");
447
448 module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
449                   &nlm_grace_period, 0644);
450 module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
451                   &nlm_timeout, 0644);
452 module_param_call(nlm_udpport, param_set_port, param_get_int,
453                   &nlm_udpport, 0644);
454 module_param_call(nlm_tcpport, param_set_port, param_get_int,
455                   &nlm_tcpport, 0644);
456
457 /*
458  * Initialising and terminating the module.
459  */
460
461 static int __init init_nlm(void)
462 {
463         nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root, 0);
464         return nlm_sysctl_table ? 0 : -ENOMEM;
465 }
466
467 static void __exit exit_nlm(void)
468 {
469         /* FIXME: delete all NLM clients */
470         nlm_shutdown_hosts();
471         unregister_sysctl_table(nlm_sysctl_table);
472 }
473
474 module_init(init_nlm);
475 module_exit(exit_nlm);
476
477 /*
478  * Define NLM program and procedures
479  */
480 static struct svc_version       nlmsvc_version1 = {
481                 .vs_vers        = 1,
482                 .vs_nproc       = 17,
483                 .vs_proc        = nlmsvc_procedures,
484                 .vs_xdrsize     = NLMSVC_XDRSIZE,
485 };
486 static struct svc_version       nlmsvc_version3 = {
487                 .vs_vers        = 3,
488                 .vs_nproc       = 24,
489                 .vs_proc        = nlmsvc_procedures,
490                 .vs_xdrsize     = NLMSVC_XDRSIZE,
491 };
492 #ifdef CONFIG_LOCKD_V4
493 static struct svc_version       nlmsvc_version4 = {
494                 .vs_vers        = 4,
495                 .vs_nproc       = 24,
496                 .vs_proc        = nlmsvc_procedures4,
497                 .vs_xdrsize     = NLMSVC_XDRSIZE,
498 };
499 #endif
500 static struct svc_version *     nlmsvc_version[] = {
501         [1] = &nlmsvc_version1,
502         [3] = &nlmsvc_version3,
503 #ifdef CONFIG_LOCKD_V4
504         [4] = &nlmsvc_version4,
505 #endif
506 };
507
508 static struct svc_stat          nlmsvc_stats;
509
510 #define NLM_NRVERS      (sizeof(nlmsvc_version)/sizeof(nlmsvc_version[0]))
511 static struct svc_program       nlmsvc_program = {
512         .pg_prog                = NLM_PROGRAM,          /* program number */
513         .pg_nvers               = NLM_NRVERS,           /* number of entries in nlmsvc_version */
514         .pg_vers                = nlmsvc_version,       /* version table */
515         .pg_name                = "lockd",              /* service name */
516         .pg_class               = "nfsd",               /* share authentication with nfsd */
517         .pg_stats               = &nlmsvc_stats,        /* stats table */
518 };
519
520 #ifdef CONFIG_STATD
521 /*
522  * Define NSM program and procedures
523  */
524 static struct svc_version       nsmsvc_version1 = {
525                 .vs_vers        = 1,
526                 .vs_nproc       = 7,
527                 .vs_proc        = nsmsvc_procedures,
528                 .vs_xdrsize     = SMSVC_XDRSIZE,
529 };
530 static struct svc_version *     nsmsvc_version[] = {
531         [1] = &nsmsvc_version1,
532 };
533
534 static struct svc_stat          nsmsvc_stats;
535
536 #define SM_NRVERS       (sizeof(nsmsvc_version)/sizeof(nsmsvc_version[0]))
537 struct svc_program      nsmsvc_program = {
538         .pg_next        = &nlmsvc_program,
539         .pg_prog        = SM_PROGRAM,           /* program number */
540         .pg_nvers       = SM_NRVERS,            /* number of entries in nlmsvc_version */
541         .pg_vers        = nsmsvc_version,       /* version table */
542         .pg_name        = "statd",              /* service name */
543         .pg_class       = "nfsd",               /* share authentication with nfsd */
544         .pg_stats       = &nsmsvc_stats,        /* stats table */
545 };
546 #endif
547