3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/moduleparam.h>
8 #include <linux/sched.h>
9 #include <linux/errno.h>
11 #include <linux/uio.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/mutex.h>
15 #include <linux/kthread.h>
16 #include <linux/freezer.h>
18 #include <linux/sunrpc/types.h>
19 #include <linux/sunrpc/stats.h>
20 #include <linux/sunrpc/clnt.h>
21 #include <linux/sunrpc/svc.h>
22 #include <linux/sunrpc/svcsock.h>
24 #include <linux/lockd/lockd.h>
25 #include <linux/nfs.h>
27 #define NLMDBG_FACILITY NLMDBG_SVC
28 #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
29 #define ALLOWED_SIGS (sigmask(SIGKILL))
31 static struct svc_program nlmsvc_program
;
33 struct nlmsvc_binding
* nlmsvc_ops
;
34 EXPORT_SYMBOL_GPL(nlmsvc_ops
);
36 static DEFINE_MUTEX(nlmsvc_mutex
);
37 static unsigned int nlmsvc_users
;
38 static struct task_struct
*nlmsvc_task
;
39 static struct svc_rqst
*nlmsvc_rqst
;
40 unsigned long nlmsvc_timeout
;
43 * These can be set at insmod time (useful for NFS as root filesystem),
44 * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003
46 static unsigned long nlm_grace_period
;
47 static unsigned long nlm_timeout
= LOCKD_DFLT_TIMEO
;
48 static int nlm_udpport
, nlm_tcpport
;
50 /* RLIM_NOFILE defaults to 1024. That seems like a reasonable default here. */
51 static unsigned int nlm_max_connections
= 1024;
54 * Constants needed for the sysctl interface.
56 static const unsigned long nlm_grace_period_min
= 0;
57 static const unsigned long nlm_grace_period_max
= 240;
58 static const unsigned long nlm_timeout_min
= 3;
59 static const unsigned long nlm_timeout_max
= 20;
60 static const int nlm_port_min
= 0, nlm_port_max
= 65535;
63 static struct ctl_table_header
* nlm_sysctl_table
;
66 static unsigned long get_lockd_grace_period(void)
68 /* Note: nlm_timeout should always be nonzero */
70 return roundup(nlm_grace_period
, nlm_timeout
) * HZ
;
72 return nlm_timeout
* 5 * HZ
;
75 static struct lock_manager lockd_manager
= {
78 static void grace_ender(struct work_struct
*not_used
)
80 locks_end_grace(&lockd_manager
);
83 static DECLARE_DELAYED_WORK(grace_period_end
, grace_ender
);
85 static void set_grace_period(void)
87 unsigned long grace_period
= get_lockd_grace_period();
89 locks_start_grace(&lockd_manager
);
90 cancel_delayed_work_sync(&grace_period_end
);
91 schedule_delayed_work(&grace_period_end
, grace_period
);
94 static void restart_grace(void)
97 cancel_delayed_work_sync(&grace_period_end
);
98 locks_end_grace(&lockd_manager
);
99 nlmsvc_invalidate_all();
105 * This is the lockd kernel thread
110 int err
= 0, preverr
= 0;
111 struct svc_rqst
*rqstp
= vrqstp
;
113 /* try_to_freeze() is called from svc_recv() */
116 /* Allow SIGKILL to tell lockd to drop all of its locks */
117 allow_signal(SIGKILL
);
119 dprintk("NFS locking service started (ver " LOCKD_VERSION
").\n");
124 nlm_timeout
= LOCKD_DFLT_TIMEO
;
125 nlmsvc_timeout
= nlm_timeout
* HZ
;
130 * The main request loop. We don't terminate until the last
131 * NFS mount or NFS daemon has gone away.
133 while (!kthread_should_stop()) {
134 long timeout
= MAX_SCHEDULE_TIMEOUT
;
135 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
137 /* update sv_maxconn if it has changed */
138 rqstp
->rq_server
->sv_maxconn
= nlm_max_connections
;
141 flush_signals(current
);
146 timeout
= nlmsvc_retry_blocked();
149 * Find a socket with data available and call its
152 err
= svc_recv(rqstp
, timeout
);
153 if (err
== -EAGAIN
|| err
== -EINTR
) {
158 if (err
!= preverr
) {
159 printk(KERN_WARNING
"%s: unexpected error "
160 "from svc_recv (%d)\n", __func__
, err
);
163 schedule_timeout_interruptible(HZ
);
168 dprintk("lockd: request from %s\n",
169 svc_print_addr(rqstp
, buf
, sizeof(buf
)));
173 flush_signals(current
);
174 cancel_delayed_work_sync(&grace_period_end
);
175 locks_end_grace(&lockd_manager
);
177 nlmsvc_invalidate_all();
178 nlm_shutdown_hosts();
183 static int create_lockd_listener(struct svc_serv
*serv
, const char *name
,
184 const int family
, const unsigned short port
)
186 struct svc_xprt
*xprt
;
188 xprt
= svc_find_xprt(serv
, name
, family
, 0);
190 return svc_create_xprt(serv
, name
, family
, port
,
196 static int create_lockd_family(struct svc_serv
*serv
, const int family
)
200 err
= create_lockd_listener(serv
, "udp", family
, nlm_udpport
);
204 return create_lockd_listener(serv
, "tcp", family
, nlm_tcpport
);
208 * Ensure there are active UDP and TCP listeners for lockd.
210 * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
211 * local services (such as rpc.statd) still require UDP, and
212 * some NFS servers do not yet support NLM over TCP.
214 * Returns zero if all listeners are available; otherwise a
215 * negative errno value is returned.
217 static int make_socks(struct svc_serv
*serv
)
222 err
= create_lockd_family(serv
, PF_INET
);
226 err
= create_lockd_family(serv
, PF_INET6
);
227 if (err
< 0 && err
!= -EAFNOSUPPORT
)
236 "lockd_up: makesock failed, error=%d\n", err
);
241 * Bring up the lockd process if it's not already up.
245 struct svc_serv
*serv
;
248 mutex_lock(&nlmsvc_mutex
);
250 * Check whether we're already up and running.
256 * Sanity check: if there's no pid,
257 * we should be the first user ...
261 "lockd_up: no pid, %d users??\n", nlmsvc_users
);
264 serv
= svc_create(&nlmsvc_program
, LOCKD_BUFSIZE
, NULL
);
266 printk(KERN_WARNING
"lockd_up: create service failed\n");
270 error
= make_socks(serv
);
272 goto destroy_and_out
;
275 * Create the kernel thread and wait for it to start.
277 nlmsvc_rqst
= svc_prepare_thread(serv
, &serv
->sv_pools
[0]);
278 if (IS_ERR(nlmsvc_rqst
)) {
279 error
= PTR_ERR(nlmsvc_rqst
);
282 "lockd_up: svc_rqst allocation failed, error=%d\n",
284 goto destroy_and_out
;
287 svc_sock_update_bufs(serv
);
288 serv
->sv_maxconn
= nlm_max_connections
;
290 nlmsvc_task
= kthread_run(lockd
, nlmsvc_rqst
, serv
->sv_name
);
291 if (IS_ERR(nlmsvc_task
)) {
292 error
= PTR_ERR(nlmsvc_task
);
293 svc_exit_thread(nlmsvc_rqst
);
297 "lockd_up: kthread_run failed, error=%d\n", error
);
298 goto destroy_and_out
;
302 * Note: svc_serv structures have an initial use count of 1,
303 * so we exit through here on both success and failure.
310 mutex_unlock(&nlmsvc_mutex
);
313 EXPORT_SYMBOL_GPL(lockd_up
);
316 * Decrement the user count and bring down lockd if we're the last.
321 mutex_lock(&nlmsvc_mutex
);
326 printk(KERN_ERR
"lockd_down: no users! task=%p\n",
332 printk(KERN_ERR
"lockd_down: no lockd running.\n");
335 kthread_stop(nlmsvc_task
);
336 svc_exit_thread(nlmsvc_rqst
);
340 mutex_unlock(&nlmsvc_mutex
);
342 EXPORT_SYMBOL_GPL(lockd_down
);
347 * Sysctl parameters (same as module parameters, different interface).
350 static ctl_table nlm_sysctls
[] = {
352 .procname
= "nlm_grace_period",
353 .data
= &nlm_grace_period
,
354 .maxlen
= sizeof(unsigned long),
356 .proc_handler
= proc_doulongvec_minmax
,
357 .extra1
= (unsigned long *) &nlm_grace_period_min
,
358 .extra2
= (unsigned long *) &nlm_grace_period_max
,
361 .procname
= "nlm_timeout",
362 .data
= &nlm_timeout
,
363 .maxlen
= sizeof(unsigned long),
365 .proc_handler
= proc_doulongvec_minmax
,
366 .extra1
= (unsigned long *) &nlm_timeout_min
,
367 .extra2
= (unsigned long *) &nlm_timeout_max
,
370 .procname
= "nlm_udpport",
371 .data
= &nlm_udpport
,
372 .maxlen
= sizeof(int),
374 .proc_handler
= proc_dointvec_minmax
,
375 .extra1
= (int *) &nlm_port_min
,
376 .extra2
= (int *) &nlm_port_max
,
379 .procname
= "nlm_tcpport",
380 .data
= &nlm_tcpport
,
381 .maxlen
= sizeof(int),
383 .proc_handler
= proc_dointvec_minmax
,
384 .extra1
= (int *) &nlm_port_min
,
385 .extra2
= (int *) &nlm_port_max
,
388 .procname
= "nsm_use_hostnames",
389 .data
= &nsm_use_hostnames
,
390 .maxlen
= sizeof(int),
392 .proc_handler
= proc_dointvec
,
395 .procname
= "nsm_local_state",
396 .data
= &nsm_local_state
,
397 .maxlen
= sizeof(int),
399 .proc_handler
= proc_dointvec
,
404 static ctl_table nlm_sysctl_dir
[] = {
408 .child
= nlm_sysctls
,
413 static ctl_table nlm_sysctl_root
[] = {
417 .child
= nlm_sysctl_dir
,
422 #endif /* CONFIG_SYSCTL */
425 * Module (and sysfs) parameters.
428 #define param_set_min_max(name, type, which_strtol, min, max) \
429 static int param_set_##name(const char *val, struct kernel_param *kp) \
432 __typeof__(type) num = which_strtol(val, &endp, 0); \
433 if (endp == val || *endp || num < (min) || num > (max)) \
435 *((int *) kp->arg) = num; \
439 static inline int is_callback(u32 proc
)
441 return proc
== NLMPROC_GRANTED
442 || proc
== NLMPROC_GRANTED_MSG
443 || proc
== NLMPROC_TEST_RES
444 || proc
== NLMPROC_LOCK_RES
445 || proc
== NLMPROC_CANCEL_RES
446 || proc
== NLMPROC_UNLOCK_RES
447 || proc
== NLMPROC_NSM_NOTIFY
;
451 static int lockd_authenticate(struct svc_rqst
*rqstp
)
453 rqstp
->rq_client
= NULL
;
454 switch (rqstp
->rq_authop
->flavour
) {
457 if (rqstp
->rq_proc
== 0)
459 if (is_callback(rqstp
->rq_proc
)) {
460 /* Leave it to individual procedures to
461 * call nlmsvc_lookup_host(rqstp)
465 return svc_set_client(rqstp
);
471 param_set_min_max(port
, int, simple_strtol
, 0, 65535)
472 param_set_min_max(grace_period
, unsigned long, simple_strtoul
,
473 nlm_grace_period_min
, nlm_grace_period_max
)
474 param_set_min_max(timeout
, unsigned long, simple_strtoul
,
475 nlm_timeout_min
, nlm_timeout_max
)
477 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
478 MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION
".");
479 MODULE_LICENSE("GPL");
481 module_param_call(nlm_grace_period
, param_set_grace_period
, param_get_ulong
,
482 &nlm_grace_period
, 0644);
483 module_param_call(nlm_timeout
, param_set_timeout
, param_get_ulong
,
485 module_param_call(nlm_udpport
, param_set_port
, param_get_int
,
487 module_param_call(nlm_tcpport
, param_set_port
, param_get_int
,
489 module_param(nsm_use_hostnames
, bool, 0644);
490 module_param(nlm_max_connections
, uint
, 0644);
493 * Initialising and terminating the module.
496 static int __init
init_nlm(void)
499 nlm_sysctl_table
= register_sysctl_table(nlm_sysctl_root
);
500 return nlm_sysctl_table
? 0 : -ENOMEM
;
506 static void __exit
exit_nlm(void)
508 nlm_shutdown_hosts();
510 unregister_sysctl_table(nlm_sysctl_table
);
514 module_init(init_nlm
);
515 module_exit(exit_nlm
);
518 * Define NLM program and procedures
520 static struct svc_version nlmsvc_version1
= {
523 .vs_proc
= nlmsvc_procedures
,
524 .vs_xdrsize
= NLMSVC_XDRSIZE
,
526 static struct svc_version nlmsvc_version3
= {
529 .vs_proc
= nlmsvc_procedures
,
530 .vs_xdrsize
= NLMSVC_XDRSIZE
,
532 #ifdef CONFIG_LOCKD_V4
533 static struct svc_version nlmsvc_version4
= {
536 .vs_proc
= nlmsvc_procedures4
,
537 .vs_xdrsize
= NLMSVC_XDRSIZE
,
540 static struct svc_version
* nlmsvc_version
[] = {
541 [1] = &nlmsvc_version1
,
542 [3] = &nlmsvc_version3
,
543 #ifdef CONFIG_LOCKD_V4
544 [4] = &nlmsvc_version4
,
548 static struct svc_stat nlmsvc_stats
;
550 #define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
551 static struct svc_program nlmsvc_program
= {
552 .pg_prog
= NLM_PROGRAM
, /* program number */
553 .pg_nvers
= NLM_NRVERS
, /* number of entries in nlmsvc_version */
554 .pg_vers
= nlmsvc_version
, /* version table */
555 .pg_name
= "lockd", /* service name */
556 .pg_class
= "nfsd", /* share authentication with nfsd */
557 .pg_stats
= &nlmsvc_stats
, /* stats table */
558 .pg_authenticate
= &lockd_authenticate
/* export authentication */