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