RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / lockd / svc.c
blob70c087943426a2ab736b9c1f30d8634fc2335a1e
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>
10 #include <linux/in.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>
23 #include <net/ip.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;
62 #ifdef CONFIG_SYSCTL
63 static struct ctl_table_header * nlm_sysctl_table;
64 #endif
66 static unsigned long get_lockd_grace_period(void)
68 /* Note: nlm_timeout should always be nonzero */
69 if (nlm_grace_period)
70 return roundup(nlm_grace_period, nlm_timeout) * HZ;
71 else
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)
96 if (nlmsvc_ops) {
97 cancel_delayed_work_sync(&grace_period_end);
98 locks_end_grace(&lockd_manager);
99 nlmsvc_invalidate_all();
100 set_grace_period();
105 * This is the lockd kernel thread
107 static int
108 lockd(void *vrqstp)
110 int err = 0, preverr = 0;
111 struct svc_rqst *rqstp = vrqstp;
113 /* try_to_freeze() is called from svc_recv() */
114 set_freezable();
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");
121 lock_kernel();
123 if (!nlm_timeout)
124 nlm_timeout = LOCKD_DFLT_TIMEO;
125 nlmsvc_timeout = nlm_timeout * HZ;
127 set_grace_period();
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;
140 if (signalled()) {
141 flush_signals(current);
142 restart_grace();
143 continue;
146 timeout = nlmsvc_retry_blocked();
149 * Find a socket with data available and call its
150 * recvfrom routine.
152 err = svc_recv(rqstp, timeout);
153 if (err == -EAGAIN || err == -EINTR) {
154 preverr = err;
155 continue;
157 if (err < 0) {
158 if (err != preverr) {
159 printk(KERN_WARNING "%s: unexpected error "
160 "from svc_recv (%d)\n", __func__, err);
161 preverr = err;
163 schedule_timeout_interruptible(HZ);
164 continue;
166 preverr = err;
168 dprintk("lockd: request from %s\n",
169 svc_print_addr(rqstp, buf, sizeof(buf)));
171 svc_process(rqstp);
173 flush_signals(current);
174 cancel_delayed_work_sync(&grace_period_end);
175 locks_end_grace(&lockd_manager);
176 if (nlmsvc_ops)
177 nlmsvc_invalidate_all();
178 nlm_shutdown_hosts();
179 unlock_kernel();
180 return 0;
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);
189 if (xprt == NULL)
190 return svc_create_xprt(serv, name, family, port,
191 SVC_SOCK_DEFAULTS);
192 svc_xprt_put(xprt);
193 return 0;
196 static int create_lockd_family(struct svc_serv *serv, const int family)
198 int err;
200 err = create_lockd_listener(serv, "udp", family, nlm_udpport);
201 if (err < 0)
202 return err;
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)
219 static int warned;
220 int err;
222 err = create_lockd_family(serv, PF_INET);
223 if (err < 0)
224 goto out_err;
226 err = create_lockd_family(serv, PF_INET6);
227 if (err < 0 && err != -EAFNOSUPPORT)
228 goto out_err;
230 warned = 0;
231 return 0;
233 out_err:
234 if (warned++ == 0)
235 printk(KERN_WARNING
236 "lockd_up: makesock failed, error=%d\n", err);
237 return err;
241 * Bring up the lockd process if it's not already up.
243 int lockd_up(void)
245 struct svc_serv *serv;
246 int error = 0;
248 mutex_lock(&nlmsvc_mutex);
250 * Check whether we're already up and running.
252 if (nlmsvc_rqst)
253 goto out;
256 * Sanity check: if there's no pid,
257 * we should be the first user ...
259 if (nlmsvc_users)
260 printk(KERN_WARNING
261 "lockd_up: no pid, %d users??\n", nlmsvc_users);
263 error = -ENOMEM;
264 serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, NULL);
265 if (!serv) {
266 printk(KERN_WARNING "lockd_up: create service failed\n");
267 goto out;
270 error = make_socks(serv);
271 if (error < 0)
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);
280 nlmsvc_rqst = NULL;
281 printk(KERN_WARNING
282 "lockd_up: svc_rqst allocation failed, error=%d\n",
283 error);
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);
294 nlmsvc_task = NULL;
295 nlmsvc_rqst = NULL;
296 printk(KERN_WARNING
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.
305 destroy_and_out:
306 svc_destroy(serv);
307 out:
308 if (!error)
309 nlmsvc_users++;
310 mutex_unlock(&nlmsvc_mutex);
311 return error;
313 EXPORT_SYMBOL_GPL(lockd_up);
316 * Decrement the user count and bring down lockd if we're the last.
318 void
319 lockd_down(void)
321 mutex_lock(&nlmsvc_mutex);
322 if (nlmsvc_users) {
323 if (--nlmsvc_users)
324 goto out;
325 } else {
326 printk(KERN_ERR "lockd_down: no users! task=%p\n",
327 nlmsvc_task);
328 BUG();
331 if (!nlmsvc_task) {
332 printk(KERN_ERR "lockd_down: no lockd running.\n");
333 BUG();
335 kthread_stop(nlmsvc_task);
336 svc_exit_thread(nlmsvc_rqst);
337 nlmsvc_task = NULL;
338 nlmsvc_rqst = NULL;
339 out:
340 mutex_unlock(&nlmsvc_mutex);
342 EXPORT_SYMBOL_GPL(lockd_down);
344 #ifdef CONFIG_SYSCTL
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),
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,
361 .procname = "nlm_timeout",
362 .data = &nlm_timeout,
363 .maxlen = sizeof(unsigned long),
364 .mode = 0644,
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),
373 .mode = 0644,
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),
382 .mode = 0644,
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),
391 .mode = 0644,
392 .proc_handler = proc_dointvec,
395 .procname = "nsm_local_state",
396 .data = &nsm_local_state,
397 .maxlen = sizeof(int),
398 .mode = 0644,
399 .proc_handler = proc_dointvec,
404 static ctl_table nlm_sysctl_dir[] = {
406 .procname = "nfs",
407 .mode = 0555,
408 .child = nlm_sysctls,
413 static ctl_table nlm_sysctl_root[] = {
415 .procname = "fs",
416 .mode = 0555,
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) \
431 char *endp; \
432 __typeof__(type) num = which_strtol(val, &endp, 0); \
433 if (endp == val || *endp || num < (min) || num > (max)) \
434 return -EINVAL; \
435 *((int *) kp->arg) = num; \
436 return 0; \
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) {
455 case RPC_AUTH_NULL:
456 case RPC_AUTH_UNIX:
457 if (rqstp->rq_proc == 0)
458 return SVC_OK;
459 if (is_callback(rqstp->rq_proc)) {
460 /* Leave it to individual procedures to
461 * call nlmsvc_lookup_host(rqstp)
463 return SVC_OK;
465 return svc_set_client(rqstp);
467 return SVC_DENIED;
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,
484 &nlm_timeout, 0644);
485 module_param_call(nlm_udpport, param_set_port, param_get_int,
486 &nlm_udpport, 0644);
487 module_param_call(nlm_tcpport, param_set_port, param_get_int,
488 &nlm_tcpport, 0644);
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)
498 #ifdef CONFIG_SYSCTL
499 nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root);
500 return nlm_sysctl_table ? 0 : -ENOMEM;
501 #else
502 return 0;
503 #endif
506 static void __exit exit_nlm(void)
508 nlm_shutdown_hosts();
509 #ifdef CONFIG_SYSCTL
510 unregister_sysctl_table(nlm_sysctl_table);
511 #endif
514 module_init(init_nlm);
515 module_exit(exit_nlm);
518 * Define NLM program and procedures
520 static struct svc_version nlmsvc_version1 = {
521 .vs_vers = 1,
522 .vs_nproc = 17,
523 .vs_proc = nlmsvc_procedures,
524 .vs_xdrsize = NLMSVC_XDRSIZE,
526 static struct svc_version nlmsvc_version3 = {
527 .vs_vers = 3,
528 .vs_nproc = 24,
529 .vs_proc = nlmsvc_procedures,
530 .vs_xdrsize = NLMSVC_XDRSIZE,
532 #ifdef CONFIG_LOCKD_V4
533 static struct svc_version nlmsvc_version4 = {
534 .vs_vers = 4,
535 .vs_nproc = 24,
536 .vs_proc = nlmsvc_procedures4,
537 .vs_xdrsize = NLMSVC_XDRSIZE,
539 #endif
540 static struct svc_version * nlmsvc_version[] = {
541 [1] = &nlmsvc_version1,
542 [3] = &nlmsvc_version3,
543 #ifdef CONFIG_LOCKD_V4
544 [4] = &nlmsvc_version4,
545 #endif
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 */