ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
90 /*
91  * This is the lockd kernel thread
92  */
93 static void
94 lockd(struct svc_rqst *rqstp)
95 {
96         struct svc_serv *serv = rqstp->rq_server;
97         int             err = 0;
98         unsigned long grace_period_expire;
99
100         /* Lock module and set up kernel thread */
101         /* lockd_up is waiting for us to startup, so will
102          * be holding a reference to this module, so it
103          * is safe to just claim another reference
104          */
105         __module_get(THIS_MODULE);
106         lock_kernel();
107
108         /*
109          * Let our maker know we're running.
110          */
111         nlmsvc_pid = current->pid;
112         up(&lockd_start);
113
114         daemonize("lockd");
115
116         /* Process request with signals blocked, but allow SIGKILL.  */
117         allow_signal(SIGKILL);
118
119         /* kick rpciod */
120         rpciod_up();
121
122         dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
123
124         if (!nlm_timeout)
125                 nlm_timeout = LOCKD_DFLT_TIMEO;
126         nlmsvc_timeout = nlm_timeout * HZ;
127
128         grace_period_expire = set_grace_period();
129
130         /*
131          * The main request loop. We don't terminate until the last
132          * NFS mount or NFS daemon has gone away, and we've been sent a
133          * signal, or else another process has taken over our job.
134          */
135         while ((nlmsvc_users || !signalled()) && nlmsvc_pid == current->pid) {
136                 long timeout = MAX_SCHEDULE_TIMEOUT;
137
138                 if (signalled()) {
139                         flush_signals(current);
140                         if (nlmsvc_ops) {
141                                 nlmsvc_invalidate_all();
142                                 grace_period_expire = set_grace_period();
143                         }
144                 }
145
146                 /*
147                  * Retry any blocked locks that have been notified by
148                  * the VFS. Don't do this during grace period.
149                  * (Theoretically, there shouldn't even be blocked locks
150                  * during grace period).
151                  */
152                 if (!nlmsvc_grace_period) {
153                         timeout = nlmsvc_retry_blocked();
154                 } else if (time_before(grace_period_expire, jiffies))
155                         clear_grace_period();
156
157                 /*
158                  * Find a socket with data available and call its
159                  * recvfrom routine.
160                  */
161                 err = svc_recv(serv, rqstp, timeout);
162                 if (err == -EAGAIN || err == -EINTR)
163                         continue;
164                 if (err < 0) {
165                         printk(KERN_WARNING
166                                "lockd: terminating on error %d\n",
167                                -err);
168                         break;
169                 }
170
171                 dprintk("lockd: request from %08x\n",
172                         (unsigned)ntohl(rqstp->rq_addr.sin_addr.s_addr));
173
174                 svc_process(serv, rqstp);
175
176         }
177
178         /*
179          * Check whether there's a new lockd process before
180          * shutting down the hosts and clearing the slot.
181          */
182         if (!nlmsvc_pid || current->pid == nlmsvc_pid) {
183                 if (nlmsvc_ops)
184                         nlmsvc_invalidate_all();
185                 nlm_shutdown_hosts();
186                 nlmsvc_pid = 0;
187         } else
188                 printk(KERN_DEBUG
189                         "lockd: new process, skipping host shutdown\n");
190         wake_up(&lockd_exit);
191                 
192         /* Exit the RPC thread */
193         svc_exit_thread(rqstp);
194
195         /* release rpciod */
196         rpciod_down();
197
198         /* Release module */
199         unlock_kernel();
200         module_put_and_exit(0);
201 }
202
203 /*
204  * Bring up the lockd process if it's not already up.
205  */
206 int
207 lockd_up(void)
208 {
209         static int              warned;
210         struct svc_serv *       serv;
211         int                     error = 0;
212
213         down(&nlmsvc_sema);
214         /*
215          * Unconditionally increment the user count ... this is
216          * the number of clients who _want_ a lockd process.
217          */
218         nlmsvc_users++; 
219         /*
220          * Check whether we're already up and running.
221          */
222         if (nlmsvc_pid)
223                 goto out;
224
225         /*
226          * Sanity check: if there's no pid,
227          * we should be the first user ...
228          */
229         if (nlmsvc_users > 1)
230                 printk(KERN_WARNING
231                         "lockd_up: no pid, %d users??\n", nlmsvc_users);
232
233         error = -ENOMEM;
234         serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE);
235         if (!serv) {
236                 printk(KERN_WARNING "lockd_up: create service failed\n");
237                 goto out;
238         }
239
240         if ((error = svc_makesock(serv, IPPROTO_UDP, nlm_udpport)) < 0 
241 #ifdef CONFIG_NFSD_TCP
242          || (error = svc_makesock(serv, IPPROTO_TCP, nlm_tcpport)) < 0
243 #endif
244                 ) {
245                 if (warned++ == 0) 
246                         printk(KERN_WARNING
247                                 "lockd_up: makesock failed, error=%d\n", error);
248                 goto destroy_and_out;
249         } 
250         warned = 0;
251
252         /*
253          * Create the kernel thread and wait for it to start.
254          */
255         error = svc_create_thread(lockd, serv);
256         if (error) {
257                 printk(KERN_WARNING
258                         "lockd_up: create thread failed, error=%d\n", error);
259                 goto destroy_and_out;
260         }
261         down(&lockd_start);
262
263         /*
264          * Note: svc_serv structures have an initial use count of 1,
265          * so we exit through here on both success and failure.
266          */
267 destroy_and_out:
268         svc_destroy(serv);
269 out:
270         up(&nlmsvc_sema);
271         return error;
272 }
273
274 /*
275  * Decrement the user count and bring down lockd if we're the last.
276  */
277 void
278 lockd_down(void)
279 {
280         static int warned;
281
282         down(&nlmsvc_sema);
283         if (nlmsvc_users) {
284                 if (--nlmsvc_users)
285                         goto out;
286         } else
287                 printk(KERN_WARNING "lockd_down: no users! pid=%d\n", nlmsvc_pid);
288
289         if (!nlmsvc_pid) {
290                 if (warned++ == 0)
291                         printk(KERN_WARNING "lockd_down: no lockd running.\n"); 
292                 goto out;
293         }
294         warned = 0;
295
296         kill_proc(nlmsvc_pid, SIGKILL, 1);
297         /*
298          * Wait for the lockd process to exit, but since we're holding
299          * the lockd semaphore, we can't wait around forever ...
300          */
301         clear_thread_flag(TIF_SIGPENDING);
302         interruptible_sleep_on_timeout(&lockd_exit, HZ);
303         if (nlmsvc_pid) {
304                 printk(KERN_WARNING 
305                         "lockd_down: lockd failed to exit, clearing pid\n");
306                 nlmsvc_pid = 0;
307         }
308         spin_lock_irq(&current->sighand->siglock);
309         recalc_sigpending();
310         spin_unlock_irq(&current->sighand->siglock);
311 out:
312         up(&nlmsvc_sema);
313 }
314
315 /*
316  * Sysctl parameters (same as module parameters, different interface).
317  */
318
319 /* Something that isn't CTL_ANY, CTL_NONE or a value that may clash. */
320 #define CTL_UNNUMBERED          -2
321
322 static ctl_table nlm_sysctls[] = {
323         {
324                 .ctl_name       = CTL_UNNUMBERED,
325                 .procname       = "nlm_grace_period",
326                 .data           = &nlm_grace_period,
327                 .maxlen         = sizeof(int),
328                 .mode           = 0644,
329                 .proc_handler   = &proc_doulongvec_minmax,
330                 .extra1         = (unsigned long *) &nlm_grace_period_min,
331                 .extra2         = (unsigned long *) &nlm_grace_period_max,
332         },
333         {
334                 .ctl_name       = CTL_UNNUMBERED,
335                 .procname       = "nlm_timeout",
336                 .data           = &nlm_timeout,
337                 .maxlen         = sizeof(int),
338                 .mode           = 0644,
339                 .proc_handler   = &proc_doulongvec_minmax,
340                 .extra1         = (unsigned long *) &nlm_timeout_min,
341                 .extra2         = (unsigned long *) &nlm_timeout_max,
342         },
343         {
344                 .ctl_name       = CTL_UNNUMBERED,
345                 .procname       = "nlm_udpport",
346                 .data           = &nlm_udpport,
347                 .maxlen         = sizeof(int),
348                 .mode           = 0644,
349                 .proc_handler   = &proc_dointvec_minmax,
350                 .extra1         = (int *) &nlm_port_min,
351                 .extra2         = (int *) &nlm_port_max,
352         },
353         {
354                 .ctl_name       = CTL_UNNUMBERED,
355                 .procname       = "nlm_tcpport",
356                 .data           = &nlm_tcpport,
357                 .maxlen         = sizeof(int),
358                 .mode           = 0644,
359                 .proc_handler   = &proc_dointvec_minmax,
360                 .extra1         = (int *) &nlm_port_min,
361                 .extra2         = (int *) &nlm_port_max,
362         },
363         { .ctl_name = 0 }
364 };
365
366 static ctl_table nlm_sysctl_dir[] = {
367         {
368                 .ctl_name       = CTL_UNNUMBERED,
369                 .procname       = "nfs",
370                 .mode           = 0555,
371                 .child          = nlm_sysctls,
372         },
373         { .ctl_name = 0 }
374 };
375
376 static ctl_table nlm_sysctl_root[] = {
377         {
378                 .ctl_name       = CTL_FS,
379                 .procname       = "fs",
380                 .mode           = 0555,
381                 .child          = nlm_sysctl_dir,
382         },
383         { .ctl_name = 0 }
384 };
385
386 /*
387  * Module (and driverfs) parameters.
388  */
389
390 #define param_set_min_max(name, type, which_strtol, min, max)           \
391 static int param_set_##name(const char *val, struct kernel_param *kp)   \
392 {                                                                       \
393         char *endp;                                                     \
394         __typeof__(type) num = which_strtol(val, &endp, 0);             \
395         if (endp == val || *endp || num < (min) || num > (max))         \
396                 return -EINVAL;                                         \
397         *((int *) kp->arg) = num;                                       \
398         return 0;                                                       \
399 }
400
401 param_set_min_max(port, int, simple_strtol, 0, 65535)
402 param_set_min_max(grace_period, unsigned long, simple_strtoul,
403                   nlm_grace_period_min, nlm_grace_period_max)
404 param_set_min_max(timeout, unsigned long, simple_strtoul,
405                   nlm_timeout_min, nlm_timeout_max)
406
407 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
408 MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
409 MODULE_LICENSE("GPL");
410
411 module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
412                   &nlm_grace_period, 644);
413 module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
414                   &nlm_timeout, 644);
415 module_param_call(nlm_udpport, param_set_port, param_get_int,
416                   &nlm_udpport, 644);
417 module_param_call(nlm_tcpport, param_set_port, param_get_int,
418                   &nlm_tcpport, 644);
419
420 /*
421  * Initialising and terminating the module.
422  */
423
424 static int __init init_nlm(void)
425 {
426         nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root, 0);
427         return nlm_sysctl_table ? 0 : -ENOMEM;
428 }
429
430 static void __exit exit_nlm(void)
431 {
432         /* FIXME: delete all NLM clients */
433         nlm_shutdown_hosts();
434         unregister_sysctl_table(nlm_sysctl_table);
435 }
436
437 module_init(init_nlm);
438 module_exit(exit_nlm);
439
440 /*
441  * Define NLM program and procedures
442  */
443 static struct svc_version       nlmsvc_version1 = {
444                 .vs_vers        = 1,
445                 .vs_nproc       = 17,
446                 .vs_proc        = nlmsvc_procedures,
447                 .vs_xdrsize     = NLMSVC_XDRSIZE,
448 };
449 static struct svc_version       nlmsvc_version3 = {
450                 .vs_vers        = 3,
451                 .vs_nproc       = 24,
452                 .vs_proc        = nlmsvc_procedures,
453                 .vs_xdrsize     = NLMSVC_XDRSIZE,
454 };
455 #ifdef CONFIG_LOCKD_V4
456 static struct svc_version       nlmsvc_version4 = {
457                 .vs_vers        = 4,
458                 .vs_nproc       = 24,
459                 .vs_proc        = nlmsvc_procedures4,
460                 .vs_xdrsize     = NLMSVC_XDRSIZE,
461 };
462 #endif
463 static struct svc_version *     nlmsvc_version[] = {
464         [1] = &nlmsvc_version1,
465         [3] = &nlmsvc_version3,
466 #ifdef CONFIG_LOCKD_V4
467         [4] = &nlmsvc_version4,
468 #endif
469 };
470
471 static struct svc_stat          nlmsvc_stats;
472
473 #define NLM_NRVERS      (sizeof(nlmsvc_version)/sizeof(nlmsvc_version[0]))
474 struct svc_program      nlmsvc_program = {
475         .pg_prog        = NLM_PROGRAM,          /* program number */
476         .pg_nvers       = NLM_NRVERS,           /* number of entries in nlmsvc_version */
477         .pg_vers        = nlmsvc_version,       /* version table */
478         .pg_name        = "lockd",              /* service name */
479         .pg_class       = "nfsd",               /* share authentication with nfsd */
480         .pg_stats       = &nlmsvc_stats,        /* stats table */
481 };