4 * This is the central lockd service.
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?
10 * Authors: Olaf Kirch (okir@monad.swb.de)
12 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
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>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
24 #include <linux/uio.h>
25 #include <linux/slab.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
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>
38 #define NLMDBG_FACILITY NLMDBG_SVC
39 #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
40 #define ALLOWED_SIGS (sigmask(SIGKILL))
42 static struct svc_program nlmsvc_program;
43 extern struct svc_program nsmsvc_program;
45 struct nlmsvc_binding * nlmsvc_ops;
46 EXPORT_SYMBOL(nlmsvc_ops);
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;
54 static DECLARE_MUTEX_LOCKED(lockd_start);
55 static DECLARE_WAIT_QUEUE_HEAD(lockd_exit);
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
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;
67 * Constants needed for the sysctl interface.
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;
75 static struct ctl_table_header * nlm_sysctl_table;
77 static unsigned long set_grace_period(void)
79 unsigned long grace_period;
81 /* Note: nlm_timeout should always be nonzero */
83 grace_period = ((nlm_grace_period + nlm_timeout - 1)
84 / nlm_timeout) * nlm_timeout * HZ;
86 grace_period = nlm_timeout * 5 * HZ;
87 nlmsvc_grace_period = 1;
88 return grace_period + jiffies;
91 static inline void clear_grace_period(void)
93 nlmsvc_grace_period = 0;
97 * This is the lockd kernel thread
100 lockd(struct svc_rqst *rqstp)
102 struct svc_serv *serv = rqstp->rq_server;
104 unsigned long grace_period_expire;
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
111 __module_get(THIS_MODULE);
115 * Let our maker know we're running.
117 nlmsvc_pid = current->pid;
124 if (!nlm_use_kstatd || nsm_kernel_statd_init() < 0)
125 nsm_statd_upcalls_init();
127 nsm_statd_upcalls_init();
130 /* Process request with signals blocked, but allow SIGKILL. */
131 allow_signal(SIGKILL);
137 dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
139 dprintk("NFS lockd/statd started (ver " LOCKD_VERSION ").\n");
143 nlm_timeout = LOCKD_DFLT_TIMEO;
144 nlmsvc_timeout = nlm_timeout * HZ;
146 grace_period_expire = set_grace_period();
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.
153 while ((nlmsvc_users || !signalled()) && nlmsvc_pid == current->pid) {
154 long timeout = MAX_SCHEDULE_TIMEOUT;
157 flush_signals(current);
159 nlmsvc_invalidate_all();
160 grace_period_expire = set_grace_period();
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).
170 if (!nlmsvc_grace_period) {
171 timeout = nlmsvc_retry_blocked();
172 } else if (time_before(grace_period_expire, jiffies))
173 clear_grace_period();
176 * Find a socket with data available and call its
179 err = svc_recv(serv, rqstp, timeout);
180 if (err == -EAGAIN || err == -EINTR)
184 "lockd: terminating on error %d\n",
189 dprintk("lockd: request from %08x\n",
190 (unsigned)ntohl(rqstp->rq_addr.sin_addr.s_addr));
192 svc_process(serv, rqstp);
197 * Check whether there's a new lockd process before
198 * shutting down the hosts and clearing the slot.
200 if (!nlmsvc_pid || current->pid == nlmsvc_pid) {
202 nlmsvc_invalidate_all();
203 nlm_shutdown_hosts();
207 "lockd: new process, skipping host shutdown\n");
208 wake_up(&lockd_exit);
210 /* Exit the RPC thread */
211 svc_exit_thread(rqstp);
218 module_put_and_exit(0);
222 * Bring up the lockd process if it's not already up.
228 struct svc_program * prog;
229 struct svc_serv * serv;
234 * Unconditionally increment the user count ... this is
235 * the number of clients who _want_ a lockd process.
239 * Check whether we're already up and running.
245 * Sanity check: if there's no pid,
246 * we should be the first user ...
248 if (nlmsvc_users > 1)
250 "lockd_up: no pid, %d users??\n", nlmsvc_users);
252 /* Register NLM program and possibly NSM (if using kstatd) */
254 prog = &nlmsvc_program;
257 prog = &nsmsvc_program;
259 serv = svc_create(prog, LOCKD_BUFSIZE);
261 printk(KERN_WARNING "lockd_up: create service failed\n");
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
272 "lockd_up: makesock failed, error=%d\n", error);
273 goto destroy_and_out;
278 * Create the kernel thread and wait for it to start.
280 error = svc_create_thread(lockd, serv);
283 "lockd_up: create thread failed, error=%d\n", error);
284 goto destroy_and_out;
289 * Note: svc_serv structures have an initial use count of 1,
290 * so we exit through here on both success and failure.
298 EXPORT_SYMBOL(lockd_up);
301 * Decrement the user count and bring down lockd if we're the last.
313 printk(KERN_WARNING "lockd_down: no users! pid=%d\n", nlmsvc_pid);
317 printk(KERN_WARNING "lockd_down: no lockd running.\n");
322 kill_proc(nlmsvc_pid, SIGKILL, 1);
324 * Wait for the lockd process to exit, but since we're holding
325 * the lockd semaphore, we can't wait around forever ...
327 clear_thread_flag(TIF_SIGPENDING);
328 interruptible_sleep_on_timeout(&lockd_exit, HZ);
331 "lockd_down: lockd failed to exit, clearing pid\n");
334 spin_lock_irq(¤t->sighand->siglock);
336 spin_unlock_irq(¤t->sighand->siglock);
340 EXPORT_SYMBOL(lockd_down);
343 * Sysctl parameters (same as module parameters, different interface).
346 /* Something that isn't CTL_ANY, CTL_NONE or a value that may clash. */
347 #define CTL_UNNUMBERED -2
349 static ctl_table nlm_sysctls[] = {
351 .ctl_name = CTL_UNNUMBERED,
352 .procname = "nlm_grace_period",
353 .data = &nlm_grace_period,
354 .maxlen = sizeof(int),
356 .proc_handler = &proc_doulongvec_minmax,
357 .extra1 = (unsigned long *) &nlm_grace_period_min,
358 .extra2 = (unsigned long *) &nlm_grace_period_max,
361 .ctl_name = CTL_UNNUMBERED,
362 .procname = "nlm_timeout",
363 .data = &nlm_timeout,
364 .maxlen = sizeof(int),
366 .proc_handler = &proc_doulongvec_minmax,
367 .extra1 = (unsigned long *) &nlm_timeout_min,
368 .extra2 = (unsigned long *) &nlm_timeout_max,
371 .ctl_name = CTL_UNNUMBERED,
372 .procname = "nlm_udpport",
373 .data = &nlm_udpport,
374 .maxlen = sizeof(int),
376 .proc_handler = &proc_dointvec_minmax,
377 .extra1 = (int *) &nlm_port_min,
378 .extra2 = (int *) &nlm_port_max,
381 .ctl_name = CTL_UNNUMBERED,
382 .procname = "nlm_tcpport",
383 .data = &nlm_tcpport,
384 .maxlen = sizeof(int),
386 .proc_handler = &proc_dointvec_minmax,
387 .extra1 = (int *) &nlm_port_min,
388 .extra2 = (int *) &nlm_port_max,
392 .ctl_name = CTL_UNNUMBERED,
393 .procname = "nlm_use_kstatd",
394 .data = &nlm_use_kstatd,
395 .maxlen = sizeof(int),
397 .proc_handler = &proc_dointvec,
403 static ctl_table nlm_sysctl_dir[] = {
405 .ctl_name = CTL_UNNUMBERED,
408 .child = nlm_sysctls,
413 static ctl_table nlm_sysctl_root[] = {
418 .child = nlm_sysctl_dir,
424 * Module (and driverfs) parameters.
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) \
431 __typeof__(type) num = which_strtol(val, &endp, 0); \
432 if (endp == val || *endp || num < (min) || num > (max)) \
434 *((int *) kp->arg) = num; \
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)
444 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
445 MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
446 MODULE_LICENSE("GPL");
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,
452 module_param_call(nlm_udpport, param_set_port, param_get_int,
454 module_param_call(nlm_tcpport, param_set_port, param_get_int,
458 * Initialising and terminating the module.
461 static int __init init_nlm(void)
463 nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root, 0);
464 return nlm_sysctl_table ? 0 : -ENOMEM;
467 static void __exit exit_nlm(void)
469 /* FIXME: delete all NLM clients */
470 nlm_shutdown_hosts();
471 unregister_sysctl_table(nlm_sysctl_table);
474 module_init(init_nlm);
475 module_exit(exit_nlm);
478 * Define NLM program and procedures
480 static struct svc_version nlmsvc_version1 = {
483 .vs_proc = nlmsvc_procedures,
484 .vs_xdrsize = NLMSVC_XDRSIZE,
486 static struct svc_version nlmsvc_version3 = {
489 .vs_proc = nlmsvc_procedures,
490 .vs_xdrsize = NLMSVC_XDRSIZE,
492 #ifdef CONFIG_LOCKD_V4
493 static struct svc_version nlmsvc_version4 = {
496 .vs_proc = nlmsvc_procedures4,
497 .vs_xdrsize = NLMSVC_XDRSIZE,
500 static struct svc_version * nlmsvc_version[] = {
501 [1] = &nlmsvc_version1,
502 [3] = &nlmsvc_version3,
503 #ifdef CONFIG_LOCKD_V4
504 [4] = &nlmsvc_version4,
508 static struct svc_stat nlmsvc_stats;
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 */
522 * Define NSM program and procedures
524 static struct svc_version nsmsvc_version1 = {
527 .vs_proc = nsmsvc_procedures,
528 .vs_xdrsize = SMSVC_XDRSIZE,
530 static struct svc_version * nsmsvc_version[] = {
531 [1] = &nsmsvc_version1,
534 static struct svc_stat nsmsvc_stats;
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 */