1 // SPDX-License-Identifier: GPL-2.0-only
5 * This is the central lockd service.
7 * FIXME: Separate the lockd NFS server functionality from the lockd NFS
8 * client functionality. Oh why didn't Sun create two separate
9 * services in the first place?
11 * Authors: Olaf Kirch (okir@monad.swb.de)
13 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/sysctl.h>
19 #include <linux/moduleparam.h>
21 #include <linux/sched/signal.h>
22 #include <linux/errno.h>
24 #include <linux/uio.h>
25 #include <linux/smp.h>
26 #include <linux/mutex.h>
27 #include <linux/kthread.h>
28 #include <linux/freezer.h>
29 #include <linux/inetdevice.h>
31 #include <linux/sunrpc/types.h>
32 #include <linux/sunrpc/stats.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/svc.h>
35 #include <linux/sunrpc/svcsock.h>
36 #include <linux/sunrpc/svc_xprt.h>
38 #include <net/addrconf.h>
40 #include <linux/lockd/lockd.h>
41 #include <linux/nfs.h>
46 #define NLMDBG_FACILITY NLMDBG_SVC
47 #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
49 static struct svc_program nlmsvc_program;
51 const struct nlmsvc_binding *nlmsvc_ops;
52 EXPORT_SYMBOL_GPL(nlmsvc_ops);
54 static DEFINE_MUTEX(nlmsvc_mutex);
55 static unsigned int nlmsvc_users;
56 static struct svc_serv *nlmsvc_serv;
57 unsigned long nlmsvc_timeout;
59 unsigned int lockd_net_id;
62 * These can be set at insmod time (useful for NFS as root filesystem),
63 * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003
65 static unsigned long nlm_grace_period;
66 static unsigned long nlm_timeout = LOCKD_DFLT_TIMEO;
67 static int nlm_udpport, nlm_tcpport;
69 /* RLIM_NOFILE defaults to 1024. That seems like a reasonable default here. */
70 static unsigned int nlm_max_connections = 1024;
73 * Constants needed for the sysctl interface.
75 static const unsigned long nlm_grace_period_min = 0;
76 static const unsigned long nlm_grace_period_max = 240;
77 static const unsigned long nlm_timeout_min = 3;
78 static const unsigned long nlm_timeout_max = 20;
81 static const int nlm_port_min = 0, nlm_port_max = 65535;
82 static struct ctl_table_header * nlm_sysctl_table;
85 static unsigned long get_lockd_grace_period(void)
87 /* Note: nlm_timeout should always be nonzero */
89 return roundup(nlm_grace_period, nlm_timeout) * HZ;
91 return nlm_timeout * 5 * HZ;
94 static void grace_ender(struct work_struct *grace)
96 struct delayed_work *dwork = to_delayed_work(grace);
97 struct lockd_net *ln = container_of(dwork, struct lockd_net,
100 locks_end_grace(&ln->lockd_manager);
103 static void set_grace_period(struct net *net)
105 unsigned long grace_period = get_lockd_grace_period();
106 struct lockd_net *ln = net_generic(net, lockd_net_id);
108 locks_start_grace(net, &ln->lockd_manager);
109 cancel_delayed_work_sync(&ln->grace_period_end);
110 schedule_delayed_work(&ln->grace_period_end, grace_period);
114 * This is the lockd kernel thread
120 struct svc_rqst *rqstp = vrqstp;
121 struct net *net = &init_net;
122 struct lockd_net *ln = net_generic(net, lockd_net_id);
124 /* try_to_freeze() is called from svc_recv() */
127 dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
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 timeout = nlmsvc_retry_blocked();
143 * Find a socket with data available and call its
146 err = svc_recv(rqstp, timeout);
147 if (err == -EAGAIN || err == -EINTR)
149 dprintk("lockd: request from %s\n",
150 svc_print_addr(rqstp, buf, sizeof(buf)));
155 nlmsvc_invalidate_all();
156 nlm_shutdown_hosts();
157 cancel_delayed_work_sync(&ln->grace_period_end);
158 locks_end_grace(&ln->lockd_manager);
160 dprintk("lockd_down: service stopped\n");
162 svc_exit_thread(rqstp);
166 static int create_lockd_listener(struct svc_serv *serv, const char *name,
167 struct net *net, const int family,
168 const unsigned short port,
169 const struct cred *cred)
171 struct svc_xprt *xprt;
173 xprt = svc_find_xprt(serv, name, net, family, 0);
175 return svc_xprt_create(serv, name, net, family, port,
176 SVC_SOCK_DEFAULTS, cred);
181 static int create_lockd_family(struct svc_serv *serv, struct net *net,
182 const int family, const struct cred *cred)
186 err = create_lockd_listener(serv, "udp", net, family, nlm_udpport,
191 return create_lockd_listener(serv, "tcp", net, family, nlm_tcpport,
196 * Ensure there are active UDP and TCP listeners for lockd.
198 * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
199 * local services (such as rpc.statd) still require UDP, and
200 * some NFS servers do not yet support NLM over TCP.
202 * Returns zero if all listeners are available; otherwise a
203 * negative errno value is returned.
205 static int make_socks(struct svc_serv *serv, struct net *net,
206 const struct cred *cred)
211 err = create_lockd_family(serv, net, PF_INET, cred);
215 err = create_lockd_family(serv, net, PF_INET6, cred);
216 if (err < 0 && err != -EAFNOSUPPORT)
225 "lockd_up: makesock failed, error=%d\n", err);
226 svc_xprt_destroy_all(serv, net);
227 svc_rpcb_cleanup(serv, net);
231 static int lockd_up_net(struct svc_serv *serv, struct net *net,
232 const struct cred *cred)
234 struct lockd_net *ln = net_generic(net, lockd_net_id);
237 if (ln->nlmsvc_users++)
240 error = svc_bind(serv, net);
244 error = make_socks(serv, net, cred);
247 set_grace_period(net);
248 dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum);
256 static void lockd_down_net(struct svc_serv *serv, struct net *net)
258 struct lockd_net *ln = net_generic(net, lockd_net_id);
260 if (ln->nlmsvc_users) {
261 if (--ln->nlmsvc_users == 0) {
262 nlm_shutdown_hosts_net(net);
263 cancel_delayed_work_sync(&ln->grace_period_end);
264 locks_end_grace(&ln->lockd_manager);
265 svc_xprt_destroy_all(serv, net);
266 svc_rpcb_cleanup(serv, net);
269 pr_err("%s: no users! net=%x\n",
270 __func__, net->ns.inum);
275 static int lockd_inetaddr_event(struct notifier_block *this,
276 unsigned long event, void *ptr)
278 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
279 struct sockaddr_in sin;
281 if (event != NETDEV_DOWN)
285 dprintk("lockd_inetaddr_event: removed %pI4\n",
287 sin.sin_family = AF_INET;
288 sin.sin_addr.s_addr = ifa->ifa_local;
289 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin);
296 static struct notifier_block lockd_inetaddr_notifier = {
297 .notifier_call = lockd_inetaddr_event,
300 #if IS_ENABLED(CONFIG_IPV6)
301 static int lockd_inet6addr_event(struct notifier_block *this,
302 unsigned long event, void *ptr)
304 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
305 struct sockaddr_in6 sin6;
307 if (event != NETDEV_DOWN)
311 dprintk("lockd_inet6addr_event: removed %pI6\n", &ifa->addr);
312 sin6.sin6_family = AF_INET6;
313 sin6.sin6_addr = ifa->addr;
314 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
315 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
316 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin6);
323 static struct notifier_block lockd_inet6addr_notifier = {
324 .notifier_call = lockd_inet6addr_event,
328 static int lockd_get(void)
330 struct svc_serv *serv;
339 * Sanity check: if there's no pid,
340 * we should be the first user ...
344 "lockd_up: no pid, %d users??\n", nlmsvc_users);
347 nlm_timeout = LOCKD_DFLT_TIMEO;
348 nlmsvc_timeout = nlm_timeout * HZ;
350 serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, lockd);
352 printk(KERN_WARNING "lockd_up: create service failed\n");
356 serv->sv_maxconn = nlm_max_connections;
357 error = svc_set_num_threads(serv, NULL, 1);
358 /* The thread now holds the only reference */
364 register_inetaddr_notifier(&lockd_inetaddr_notifier);
365 #if IS_ENABLED(CONFIG_IPV6)
366 register_inet6addr_notifier(&lockd_inet6addr_notifier);
368 dprintk("lockd_up: service created\n");
373 static void lockd_put(void)
375 if (WARN(nlmsvc_users <= 0, "lockd_down: no users!\n"))
380 unregister_inetaddr_notifier(&lockd_inetaddr_notifier);
381 #if IS_ENABLED(CONFIG_IPV6)
382 unregister_inet6addr_notifier(&lockd_inet6addr_notifier);
385 svc_set_num_threads(nlmsvc_serv, NULL, 0);
387 dprintk("lockd_down: service destroyed\n");
391 * Bring up the lockd process if it's not already up.
393 int lockd_up(struct net *net, const struct cred *cred)
397 mutex_lock(&nlmsvc_mutex);
403 error = lockd_up_net(nlmsvc_serv, net, cred);
410 mutex_unlock(&nlmsvc_mutex);
413 EXPORT_SYMBOL_GPL(lockd_up);
416 * Decrement the user count and bring down lockd if we're the last.
419 lockd_down(struct net *net)
421 mutex_lock(&nlmsvc_mutex);
422 lockd_down_net(nlmsvc_serv, net);
424 mutex_unlock(&nlmsvc_mutex);
426 EXPORT_SYMBOL_GPL(lockd_down);
431 * Sysctl parameters (same as module parameters, different interface).
434 static struct ctl_table nlm_sysctls[] = {
436 .procname = "nlm_grace_period",
437 .data = &nlm_grace_period,
438 .maxlen = sizeof(unsigned long),
440 .proc_handler = proc_doulongvec_minmax,
441 .extra1 = (unsigned long *) &nlm_grace_period_min,
442 .extra2 = (unsigned long *) &nlm_grace_period_max,
445 .procname = "nlm_timeout",
446 .data = &nlm_timeout,
447 .maxlen = sizeof(unsigned long),
449 .proc_handler = proc_doulongvec_minmax,
450 .extra1 = (unsigned long *) &nlm_timeout_min,
451 .extra2 = (unsigned long *) &nlm_timeout_max,
454 .procname = "nlm_udpport",
455 .data = &nlm_udpport,
456 .maxlen = sizeof(int),
458 .proc_handler = proc_dointvec_minmax,
459 .extra1 = (int *) &nlm_port_min,
460 .extra2 = (int *) &nlm_port_max,
463 .procname = "nlm_tcpport",
464 .data = &nlm_tcpport,
465 .maxlen = sizeof(int),
467 .proc_handler = proc_dointvec_minmax,
468 .extra1 = (int *) &nlm_port_min,
469 .extra2 = (int *) &nlm_port_max,
472 .procname = "nsm_use_hostnames",
473 .data = &nsm_use_hostnames,
474 .maxlen = sizeof(bool),
476 .proc_handler = proc_dobool,
479 .procname = "nsm_local_state",
480 .data = &nsm_local_state,
481 .maxlen = sizeof(int),
483 .proc_handler = proc_dointvec,
488 #endif /* CONFIG_SYSCTL */
491 * Module (and sysfs) parameters.
494 #define param_set_min_max(name, type, which_strtol, min, max) \
495 static int param_set_##name(const char *val, const struct kernel_param *kp) \
498 __typeof__(type) num = which_strtol(val, &endp, 0); \
499 if (endp == val || *endp || num < (min) || num > (max)) \
501 *((type *) kp->arg) = num; \
505 static inline int is_callback(u32 proc)
507 return proc == NLMPROC_GRANTED
508 || proc == NLMPROC_GRANTED_MSG
509 || proc == NLMPROC_TEST_RES
510 || proc == NLMPROC_LOCK_RES
511 || proc == NLMPROC_CANCEL_RES
512 || proc == NLMPROC_UNLOCK_RES
513 || proc == NLMPROC_NSM_NOTIFY;
517 static int lockd_authenticate(struct svc_rqst *rqstp)
519 rqstp->rq_client = NULL;
520 switch (rqstp->rq_authop->flavour) {
523 rqstp->rq_auth_stat = rpc_auth_ok;
524 if (rqstp->rq_proc == 0)
526 if (is_callback(rqstp->rq_proc)) {
527 /* Leave it to individual procedures to
528 * call nlmsvc_lookup_host(rqstp)
532 return svc_set_client(rqstp);
534 rqstp->rq_auth_stat = rpc_autherr_badcred;
539 param_set_min_max(port, int, simple_strtol, 0, 65535)
540 param_set_min_max(grace_period, unsigned long, simple_strtoul,
541 nlm_grace_period_min, nlm_grace_period_max)
542 param_set_min_max(timeout, unsigned long, simple_strtoul,
543 nlm_timeout_min, nlm_timeout_max)
545 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
546 MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
547 MODULE_LICENSE("GPL");
549 module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
550 &nlm_grace_period, 0644);
551 module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
553 module_param_call(nlm_udpport, param_set_port, param_get_int,
555 module_param_call(nlm_tcpport, param_set_port, param_get_int,
557 module_param(nsm_use_hostnames, bool, 0644);
558 module_param(nlm_max_connections, uint, 0644);
560 static int lockd_init_net(struct net *net)
562 struct lockd_net *ln = net_generic(net, lockd_net_id);
564 INIT_DELAYED_WORK(&ln->grace_period_end, grace_ender);
565 INIT_LIST_HEAD(&ln->lockd_manager.list);
566 ln->lockd_manager.block_opens = false;
567 INIT_LIST_HEAD(&ln->nsm_handles);
571 static void lockd_exit_net(struct net *net)
573 struct lockd_net *ln = net_generic(net, lockd_net_id);
575 WARN_ONCE(!list_empty(&ln->lockd_manager.list),
576 "net %x %s: lockd_manager.list is not empty\n",
577 net->ns.inum, __func__);
578 WARN_ONCE(!list_empty(&ln->nsm_handles),
579 "net %x %s: nsm_handles list is not empty\n",
580 net->ns.inum, __func__);
581 WARN_ONCE(delayed_work_pending(&ln->grace_period_end),
582 "net %x %s: grace_period_end was not cancelled\n",
583 net->ns.inum, __func__);
586 static struct pernet_operations lockd_net_ops = {
587 .init = lockd_init_net,
588 .exit = lockd_exit_net,
590 .size = sizeof(struct lockd_net),
595 * Initialising and terminating the module.
598 static int __init init_nlm(void)
604 nlm_sysctl_table = register_sysctl("fs/nfs", nlm_sysctls);
605 if (nlm_sysctl_table == NULL)
608 err = register_pernet_subsys(&lockd_net_ops);
612 err = lockd_create_procfs();
619 unregister_pernet_subsys(&lockd_net_ops);
622 unregister_sysctl_table(nlm_sysctl_table);
628 static void __exit exit_nlm(void)
630 /* FIXME: delete all NLM clients */
631 nlm_shutdown_hosts();
632 lockd_remove_procfs();
633 unregister_pernet_subsys(&lockd_net_ops);
635 unregister_sysctl_table(nlm_sysctl_table);
639 module_init(init_nlm);
640 module_exit(exit_nlm);
643 * nlmsvc_dispatch - Process an NLM Request
644 * @rqstp: incoming request
647 * %0: Processing complete; do not send a Reply
648 * %1: Processing complete; send Reply in rqstp->rq_res
650 static int nlmsvc_dispatch(struct svc_rqst *rqstp)
652 const struct svc_procedure *procp = rqstp->rq_procinfo;
653 __be32 *statp = rqstp->rq_accept_statp;
655 if (!procp->pc_decode(rqstp, &rqstp->rq_arg_stream))
658 *statp = procp->pc_func(rqstp);
659 if (*statp == rpc_drop_reply)
661 if (*statp != rpc_success)
664 if (!procp->pc_encode(rqstp, &rqstp->rq_res_stream))
670 *statp = rpc_garbage_args;
674 *statp = rpc_system_err;
679 * Define NLM program and procedures
681 static DEFINE_PER_CPU_ALIGNED(unsigned long, nlmsvc_version1_count[17]);
682 static const struct svc_version nlmsvc_version1 = {
685 .vs_proc = nlmsvc_procedures,
686 .vs_count = nlmsvc_version1_count,
687 .vs_dispatch = nlmsvc_dispatch,
688 .vs_xdrsize = NLMSVC_XDRSIZE,
691 static DEFINE_PER_CPU_ALIGNED(unsigned long,
692 nlmsvc_version3_count[ARRAY_SIZE(nlmsvc_procedures)]);
693 static const struct svc_version nlmsvc_version3 = {
695 .vs_nproc = ARRAY_SIZE(nlmsvc_procedures),
696 .vs_proc = nlmsvc_procedures,
697 .vs_count = nlmsvc_version3_count,
698 .vs_dispatch = nlmsvc_dispatch,
699 .vs_xdrsize = NLMSVC_XDRSIZE,
702 #ifdef CONFIG_LOCKD_V4
703 static DEFINE_PER_CPU_ALIGNED(unsigned long,
704 nlmsvc_version4_count[ARRAY_SIZE(nlmsvc_procedures4)]);
705 static const struct svc_version nlmsvc_version4 = {
707 .vs_nproc = ARRAY_SIZE(nlmsvc_procedures4),
708 .vs_proc = nlmsvc_procedures4,
709 .vs_count = nlmsvc_version4_count,
710 .vs_dispatch = nlmsvc_dispatch,
711 .vs_xdrsize = NLMSVC_XDRSIZE,
715 static const struct svc_version *nlmsvc_version[] = {
716 [1] = &nlmsvc_version1,
717 [3] = &nlmsvc_version3,
718 #ifdef CONFIG_LOCKD_V4
719 [4] = &nlmsvc_version4,
723 static struct svc_stat nlmsvc_stats;
725 #define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
726 static struct svc_program nlmsvc_program = {
727 .pg_prog = NLM_PROGRAM, /* program number */
728 .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
729 .pg_vers = nlmsvc_version, /* version table */
730 .pg_name = "lockd", /* service name */
731 .pg_class = "nfsd", /* share authentication with nfsd */
732 .pg_stats = &nlmsvc_stats, /* stats table */
733 .pg_authenticate = &lockd_authenticate, /* export authentication */
734 .pg_init_request = svc_generic_init_request,
735 .pg_rpcbind_set = svc_generic_rpcbind_set,