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;
136 /* update sv_maxconn if it has changed */
137 rqstp->rq_server->sv_maxconn = nlm_max_connections;
139 timeout = nlmsvc_retry_blocked();
142 * Find a socket with data available and call its
145 err = svc_recv(rqstp, timeout);
146 if (err == -EAGAIN || err == -EINTR)
150 nlmsvc_invalidate_all();
151 nlm_shutdown_hosts();
152 cancel_delayed_work_sync(&ln->grace_period_end);
153 locks_end_grace(&ln->lockd_manager);
155 dprintk("lockd_down: service stopped\n");
157 svc_exit_thread(rqstp);
161 static int create_lockd_listener(struct svc_serv *serv, const char *name,
162 struct net *net, const int family,
163 const unsigned short port,
164 const struct cred *cred)
166 struct svc_xprt *xprt;
168 xprt = svc_find_xprt(serv, name, net, family, 0);
170 return svc_xprt_create(serv, name, net, family, port,
171 SVC_SOCK_DEFAULTS, cred);
176 static int create_lockd_family(struct svc_serv *serv, struct net *net,
177 const int family, const struct cred *cred)
181 err = create_lockd_listener(serv, "udp", net, family, nlm_udpport,
186 return create_lockd_listener(serv, "tcp", net, family, nlm_tcpport,
191 * Ensure there are active UDP and TCP listeners for lockd.
193 * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
194 * local services (such as rpc.statd) still require UDP, and
195 * some NFS servers do not yet support NLM over TCP.
197 * Returns zero if all listeners are available; otherwise a
198 * negative errno value is returned.
200 static int make_socks(struct svc_serv *serv, struct net *net,
201 const struct cred *cred)
206 err = create_lockd_family(serv, net, PF_INET, cred);
210 err = create_lockd_family(serv, net, PF_INET6, cred);
211 if (err < 0 && err != -EAFNOSUPPORT)
220 "lockd_up: makesock failed, error=%d\n", err);
221 svc_xprt_destroy_all(serv, net);
222 svc_rpcb_cleanup(serv, net);
226 static int lockd_up_net(struct svc_serv *serv, struct net *net,
227 const struct cred *cred)
229 struct lockd_net *ln = net_generic(net, lockd_net_id);
232 if (ln->nlmsvc_users++)
235 error = svc_bind(serv, net);
239 error = make_socks(serv, net, cred);
242 set_grace_period(net);
243 dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum);
251 static void lockd_down_net(struct svc_serv *serv, struct net *net)
253 struct lockd_net *ln = net_generic(net, lockd_net_id);
255 if (ln->nlmsvc_users) {
256 if (--ln->nlmsvc_users == 0) {
257 nlm_shutdown_hosts_net(net);
258 cancel_delayed_work_sync(&ln->grace_period_end);
259 locks_end_grace(&ln->lockd_manager);
260 svc_xprt_destroy_all(serv, net);
261 svc_rpcb_cleanup(serv, net);
264 pr_err("%s: no users! net=%x\n",
265 __func__, net->ns.inum);
270 static int lockd_inetaddr_event(struct notifier_block *this,
271 unsigned long event, void *ptr)
273 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
274 struct sockaddr_in sin;
276 if (event != NETDEV_DOWN)
280 dprintk("lockd_inetaddr_event: removed %pI4\n",
282 sin.sin_family = AF_INET;
283 sin.sin_addr.s_addr = ifa->ifa_local;
284 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin);
291 static struct notifier_block lockd_inetaddr_notifier = {
292 .notifier_call = lockd_inetaddr_event,
295 #if IS_ENABLED(CONFIG_IPV6)
296 static int lockd_inet6addr_event(struct notifier_block *this,
297 unsigned long event, void *ptr)
299 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
300 struct sockaddr_in6 sin6;
302 if (event != NETDEV_DOWN)
306 dprintk("lockd_inet6addr_event: removed %pI6\n", &ifa->addr);
307 sin6.sin6_family = AF_INET6;
308 sin6.sin6_addr = ifa->addr;
309 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
310 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
311 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin6);
318 static struct notifier_block lockd_inet6addr_notifier = {
319 .notifier_call = lockd_inet6addr_event,
323 static int lockd_get(void)
325 struct svc_serv *serv;
334 * Sanity check: if there's no pid,
335 * we should be the first user ...
339 "lockd_up: no pid, %d users??\n", nlmsvc_users);
342 nlm_timeout = LOCKD_DFLT_TIMEO;
343 nlmsvc_timeout = nlm_timeout * HZ;
345 serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, lockd);
347 printk(KERN_WARNING "lockd_up: create service failed\n");
351 serv->sv_maxconn = nlm_max_connections;
352 error = svc_set_num_threads(serv, NULL, 1);
353 /* The thread now holds the only reference */
359 register_inetaddr_notifier(&lockd_inetaddr_notifier);
360 #if IS_ENABLED(CONFIG_IPV6)
361 register_inet6addr_notifier(&lockd_inet6addr_notifier);
363 dprintk("lockd_up: service created\n");
368 static void lockd_put(void)
370 if (WARN(nlmsvc_users <= 0, "lockd_down: no users!\n"))
375 unregister_inetaddr_notifier(&lockd_inetaddr_notifier);
376 #if IS_ENABLED(CONFIG_IPV6)
377 unregister_inet6addr_notifier(&lockd_inet6addr_notifier);
380 svc_set_num_threads(nlmsvc_serv, NULL, 0);
382 dprintk("lockd_down: service destroyed\n");
386 * Bring up the lockd process if it's not already up.
388 int lockd_up(struct net *net, const struct cred *cred)
392 mutex_lock(&nlmsvc_mutex);
398 error = lockd_up_net(nlmsvc_serv, net, cred);
405 mutex_unlock(&nlmsvc_mutex);
408 EXPORT_SYMBOL_GPL(lockd_up);
411 * Decrement the user count and bring down lockd if we're the last.
414 lockd_down(struct net *net)
416 mutex_lock(&nlmsvc_mutex);
417 lockd_down_net(nlmsvc_serv, net);
419 mutex_unlock(&nlmsvc_mutex);
421 EXPORT_SYMBOL_GPL(lockd_down);
426 * Sysctl parameters (same as module parameters, different interface).
429 static struct ctl_table nlm_sysctls[] = {
431 .procname = "nlm_grace_period",
432 .data = &nlm_grace_period,
433 .maxlen = sizeof(unsigned long),
435 .proc_handler = proc_doulongvec_minmax,
436 .extra1 = (unsigned long *) &nlm_grace_period_min,
437 .extra2 = (unsigned long *) &nlm_grace_period_max,
440 .procname = "nlm_timeout",
441 .data = &nlm_timeout,
442 .maxlen = sizeof(unsigned long),
444 .proc_handler = proc_doulongvec_minmax,
445 .extra1 = (unsigned long *) &nlm_timeout_min,
446 .extra2 = (unsigned long *) &nlm_timeout_max,
449 .procname = "nlm_udpport",
450 .data = &nlm_udpport,
451 .maxlen = sizeof(int),
453 .proc_handler = proc_dointvec_minmax,
454 .extra1 = (int *) &nlm_port_min,
455 .extra2 = (int *) &nlm_port_max,
458 .procname = "nlm_tcpport",
459 .data = &nlm_tcpport,
460 .maxlen = sizeof(int),
462 .proc_handler = proc_dointvec_minmax,
463 .extra1 = (int *) &nlm_port_min,
464 .extra2 = (int *) &nlm_port_max,
467 .procname = "nsm_use_hostnames",
468 .data = &nsm_use_hostnames,
469 .maxlen = sizeof(bool),
471 .proc_handler = proc_dobool,
474 .procname = "nsm_local_state",
475 .data = &nsm_local_state,
476 .maxlen = sizeof(int),
478 .proc_handler = proc_dointvec,
483 #endif /* CONFIG_SYSCTL */
486 * Module (and sysfs) parameters.
489 #define param_set_min_max(name, type, which_strtol, min, max) \
490 static int param_set_##name(const char *val, const struct kernel_param *kp) \
493 __typeof__(type) num = which_strtol(val, &endp, 0); \
494 if (endp == val || *endp || num < (min) || num > (max)) \
496 *((type *) kp->arg) = num; \
500 static inline int is_callback(u32 proc)
502 return proc == NLMPROC_GRANTED
503 || proc == NLMPROC_GRANTED_MSG
504 || proc == NLMPROC_TEST_RES
505 || proc == NLMPROC_LOCK_RES
506 || proc == NLMPROC_CANCEL_RES
507 || proc == NLMPROC_UNLOCK_RES
508 || proc == NLMPROC_NSM_NOTIFY;
512 static int lockd_authenticate(struct svc_rqst *rqstp)
514 rqstp->rq_client = NULL;
515 switch (rqstp->rq_authop->flavour) {
518 rqstp->rq_auth_stat = rpc_auth_ok;
519 if (rqstp->rq_proc == 0)
521 if (is_callback(rqstp->rq_proc)) {
522 /* Leave it to individual procedures to
523 * call nlmsvc_lookup_host(rqstp)
527 return svc_set_client(rqstp);
529 rqstp->rq_auth_stat = rpc_autherr_badcred;
534 param_set_min_max(port, int, simple_strtol, 0, 65535)
535 param_set_min_max(grace_period, unsigned long, simple_strtoul,
536 nlm_grace_period_min, nlm_grace_period_max)
537 param_set_min_max(timeout, unsigned long, simple_strtoul,
538 nlm_timeout_min, nlm_timeout_max)
540 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
541 MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
542 MODULE_LICENSE("GPL");
544 module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
545 &nlm_grace_period, 0644);
546 module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
548 module_param_call(nlm_udpport, param_set_port, param_get_int,
550 module_param_call(nlm_tcpport, param_set_port, param_get_int,
552 module_param(nsm_use_hostnames, bool, 0644);
553 module_param(nlm_max_connections, uint, 0644);
555 static int lockd_init_net(struct net *net)
557 struct lockd_net *ln = net_generic(net, lockd_net_id);
559 INIT_DELAYED_WORK(&ln->grace_period_end, grace_ender);
560 INIT_LIST_HEAD(&ln->lockd_manager.list);
561 ln->lockd_manager.block_opens = false;
562 INIT_LIST_HEAD(&ln->nsm_handles);
566 static void lockd_exit_net(struct net *net)
568 struct lockd_net *ln = net_generic(net, lockd_net_id);
570 WARN_ONCE(!list_empty(&ln->lockd_manager.list),
571 "net %x %s: lockd_manager.list is not empty\n",
572 net->ns.inum, __func__);
573 WARN_ONCE(!list_empty(&ln->nsm_handles),
574 "net %x %s: nsm_handles list is not empty\n",
575 net->ns.inum, __func__);
576 WARN_ONCE(delayed_work_pending(&ln->grace_period_end),
577 "net %x %s: grace_period_end was not cancelled\n",
578 net->ns.inum, __func__);
581 static struct pernet_operations lockd_net_ops = {
582 .init = lockd_init_net,
583 .exit = lockd_exit_net,
585 .size = sizeof(struct lockd_net),
590 * Initialising and terminating the module.
593 static int __init init_nlm(void)
599 nlm_sysctl_table = register_sysctl("fs/nfs", nlm_sysctls);
600 if (nlm_sysctl_table == NULL)
603 err = register_pernet_subsys(&lockd_net_ops);
607 err = lockd_create_procfs();
614 unregister_pernet_subsys(&lockd_net_ops);
617 unregister_sysctl_table(nlm_sysctl_table);
623 static void __exit exit_nlm(void)
625 /* FIXME: delete all NLM clients */
626 nlm_shutdown_hosts();
627 lockd_remove_procfs();
628 unregister_pernet_subsys(&lockd_net_ops);
630 unregister_sysctl_table(nlm_sysctl_table);
634 module_init(init_nlm);
635 module_exit(exit_nlm);
638 * nlmsvc_dispatch - Process an NLM Request
639 * @rqstp: incoming request
642 * %0: Processing complete; do not send a Reply
643 * %1: Processing complete; send Reply in rqstp->rq_res
645 static int nlmsvc_dispatch(struct svc_rqst *rqstp)
647 const struct svc_procedure *procp = rqstp->rq_procinfo;
648 __be32 *statp = rqstp->rq_accept_statp;
650 if (!procp->pc_decode(rqstp, &rqstp->rq_arg_stream))
653 *statp = procp->pc_func(rqstp);
654 if (*statp == rpc_drop_reply)
656 if (*statp != rpc_success)
659 if (!procp->pc_encode(rqstp, &rqstp->rq_res_stream))
665 *statp = rpc_garbage_args;
669 *statp = rpc_system_err;
674 * Define NLM program and procedures
676 static DEFINE_PER_CPU_ALIGNED(unsigned long, nlmsvc_version1_count[17]);
677 static const struct svc_version nlmsvc_version1 = {
680 .vs_proc = nlmsvc_procedures,
681 .vs_count = nlmsvc_version1_count,
682 .vs_dispatch = nlmsvc_dispatch,
683 .vs_xdrsize = NLMSVC_XDRSIZE,
686 static DEFINE_PER_CPU_ALIGNED(unsigned long,
687 nlmsvc_version3_count[ARRAY_SIZE(nlmsvc_procedures)]);
688 static const struct svc_version nlmsvc_version3 = {
690 .vs_nproc = ARRAY_SIZE(nlmsvc_procedures),
691 .vs_proc = nlmsvc_procedures,
692 .vs_count = nlmsvc_version3_count,
693 .vs_dispatch = nlmsvc_dispatch,
694 .vs_xdrsize = NLMSVC_XDRSIZE,
697 #ifdef CONFIG_LOCKD_V4
698 static DEFINE_PER_CPU_ALIGNED(unsigned long,
699 nlmsvc_version4_count[ARRAY_SIZE(nlmsvc_procedures4)]);
700 static const struct svc_version nlmsvc_version4 = {
702 .vs_nproc = ARRAY_SIZE(nlmsvc_procedures4),
703 .vs_proc = nlmsvc_procedures4,
704 .vs_count = nlmsvc_version4_count,
705 .vs_dispatch = nlmsvc_dispatch,
706 .vs_xdrsize = NLMSVC_XDRSIZE,
710 static const struct svc_version *nlmsvc_version[] = {
711 [1] = &nlmsvc_version1,
712 [3] = &nlmsvc_version3,
713 #ifdef CONFIG_LOCKD_V4
714 [4] = &nlmsvc_version4,
718 static struct svc_stat nlmsvc_stats;
720 #define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
721 static struct svc_program nlmsvc_program = {
722 .pg_prog = NLM_PROGRAM, /* program number */
723 .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
724 .pg_vers = nlmsvc_version, /* version table */
725 .pg_name = "lockd", /* service name */
726 .pg_class = "nfsd", /* share authentication with nfsd */
727 .pg_stats = &nlmsvc_stats, /* stats table */
728 .pg_authenticate = &lockd_authenticate, /* export authentication */
729 .pg_init_request = svc_generic_init_request,
730 .pg_rpcbind_set = svc_generic_rpcbind_set,