Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/lockd/svc.c |
| 4 | * |
| 5 | * This is the central lockd service. |
| 6 | * |
| 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? |
| 10 | * |
| 11 | * Authors: Olaf Kirch (okir@monad.swb.de) |
| 12 | * |
| 13 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/sysctl.h> |
| 19 | #include <linux/moduleparam.h> |
| 20 | |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 21 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/errno.h> |
| 23 | #include <linux/in.h> |
| 24 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/smp.h> |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 27 | #include <linux/kthread.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 28 | #include <linux/freezer.h> |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 29 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 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> |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 36 | #include <linux/sunrpc/svc_xprt.h> |
NeilBrown | 24e3666 | 2006-10-02 02:17:45 -0700 | [diff] [blame] | 37 | #include <net/ip.h> |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 38 | #include <net/addrconf.h> |
| 39 | #include <net/ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/lockd/lockd.h> |
| 41 | #include <linux/nfs.h> |
| 42 | |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 43 | #include "netns.h" |
Jeff Layton | d68e3c4 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 44 | #include "procfs.h" |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #define NLMDBG_FACILITY NLMDBG_SVC |
| 47 | #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE) |
| 48 | #define ALLOWED_SIGS (sigmask(SIGKILL)) |
| 49 | |
| 50 | static struct svc_program nlmsvc_program; |
| 51 | |
Julia Lawall | 2a29745 | 2015-12-23 22:25:13 +0100 | [diff] [blame] | 52 | const struct nlmsvc_binding *nlmsvc_ops; |
Trond Myklebust | 2de5987 | 2008-12-23 15:21:33 -0500 | [diff] [blame] | 53 | EXPORT_SYMBOL_GPL(nlmsvc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 55 | static DEFINE_MUTEX(nlmsvc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static unsigned int nlmsvc_users; |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 57 | static struct svc_serv *nlmsvc_serv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | unsigned long nlmsvc_timeout; |
| 59 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 60 | unsigned int lockd_net_id; |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
| 63 | * These can be set at insmod time (useful for NFS as root filesystem), |
| 64 | * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003 |
| 65 | */ |
| 66 | static unsigned long nlm_grace_period; |
| 67 | static unsigned long nlm_timeout = LOCKD_DFLT_TIMEO; |
| 68 | static int nlm_udpport, nlm_tcpport; |
| 69 | |
Jeff Layton | c72a476 | 2008-10-20 11:51:58 -0400 | [diff] [blame] | 70 | /* RLIM_NOFILE defaults to 1024. That seems like a reasonable default here. */ |
| 71 | static unsigned int nlm_max_connections = 1024; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* |
| 74 | * Constants needed for the sysctl interface. |
| 75 | */ |
| 76 | static const unsigned long nlm_grace_period_min = 0; |
| 77 | static const unsigned long nlm_grace_period_max = 240; |
| 78 | static const unsigned long nlm_timeout_min = 3; |
| 79 | static const unsigned long nlm_timeout_max = 20; |
| 80 | static const int nlm_port_min = 0, nlm_port_max = 65535; |
| 81 | |
Chuck Lever | 90d5b18 | 2008-03-14 14:18:30 -0400 | [diff] [blame] | 82 | #ifdef CONFIG_SYSCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | static struct ctl_table_header * nlm_sysctl_table; |
Chuck Lever | 90d5b18 | 2008-03-14 14:18:30 -0400 | [diff] [blame] | 84 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 86 | static unsigned long get_lockd_grace_period(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | /* Note: nlm_timeout should always be nonzero */ |
| 89 | if (nlm_grace_period) |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 90 | return roundup(nlm_grace_period, nlm_timeout) * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | else |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 92 | return nlm_timeout * 5 * HZ; |
| 93 | } |
| 94 | |
Stanislav Kinsbursky | 08d44a3 | 2012-07-25 16:56:50 +0400 | [diff] [blame] | 95 | static void grace_ender(struct work_struct *grace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Geliang Tang | ea44463 | 2016-01-01 22:06:29 +0800 | [diff] [blame] | 97 | struct delayed_work *dwork = to_delayed_work(grace); |
Stanislav Kinsbursky | 08d44a3 | 2012-07-25 16:56:50 +0400 | [diff] [blame] | 98 | struct lockd_net *ln = container_of(dwork, struct lockd_net, |
| 99 | grace_period_end); |
| 100 | |
| 101 | locks_end_grace(&ln->lockd_manager); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 104 | static void set_grace_period(struct net *net) |
J. Bruce Fields | c8ab5f2 | 2008-03-18 19:00:19 -0400 | [diff] [blame] | 105 | { |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 106 | unsigned long grace_period = get_lockd_grace_period(); |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 107 | struct lockd_net *ln = net_generic(net, lockd_net_id); |
J. Bruce Fields | c8ab5f2 | 2008-03-18 19:00:19 -0400 | [diff] [blame] | 108 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 109 | locks_start_grace(net, &ln->lockd_manager); |
Stanislav Kinsbursky | 66547b0 | 2012-07-25 16:56:43 +0400 | [diff] [blame] | 110 | cancel_delayed_work_sync(&ln->grace_period_end); |
| 111 | schedule_delayed_work(&ln->grace_period_end, grace_period); |
J. Bruce Fields | c8ab5f2 | 2008-03-18 19:00:19 -0400 | [diff] [blame] | 112 | } |
| 113 | |
J. Bruce Fields | 89996df | 2009-05-06 16:32:54 -0400 | [diff] [blame] | 114 | static void restart_grace(void) |
| 115 | { |
| 116 | if (nlmsvc_ops) { |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 117 | struct net *net = &init_net; |
| 118 | struct lockd_net *ln = net_generic(net, lockd_net_id); |
Stanislav Kinsbursky | 66547b0 | 2012-07-25 16:56:43 +0400 | [diff] [blame] | 119 | |
| 120 | cancel_delayed_work_sync(&ln->grace_period_end); |
Stanislav Kinsbursky | 08d44a3 | 2012-07-25 16:56:50 +0400 | [diff] [blame] | 121 | locks_end_grace(&ln->lockd_manager); |
J. Bruce Fields | 89996df | 2009-05-06 16:32:54 -0400 | [diff] [blame] | 122 | nlmsvc_invalidate_all(); |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 123 | set_grace_period(net); |
J. Bruce Fields | 89996df | 2009-05-06 16:32:54 -0400 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* |
| 128 | * This is the lockd kernel thread |
| 129 | */ |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 130 | static int |
| 131 | lockd(void *vrqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
J. Bruce Fields | 5b444cc | 2012-08-17 21:47:53 -0400 | [diff] [blame] | 133 | int err = 0; |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 134 | struct svc_rqst *rqstp = vrqstp; |
J. Bruce Fields | efda760 | 2017-03-28 21:25:08 -0400 | [diff] [blame] | 135 | struct net *net = &init_net; |
| 136 | struct lockd_net *ln = net_generic(net, lockd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 138 | /* try_to_freeze() is called from svc_recv() */ |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 139 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 141 | /* Allow SIGKILL to tell lockd to drop all of its locks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | allow_signal(SIGKILL); |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n"); |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* |
| 147 | * The main request loop. We don't terminate until the last |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 148 | * NFS mount or NFS daemon has gone away. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | */ |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 150 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | long timeout = MAX_SCHEDULE_TIMEOUT; |
Pavel Emelyanov | 5216a8e | 2008-02-21 10:57:45 +0300 | [diff] [blame] | 152 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Jeff Layton | c72a476 | 2008-10-20 11:51:58 -0400 | [diff] [blame] | 154 | /* update sv_maxconn if it has changed */ |
| 155 | rqstp->rq_server->sv_maxconn = nlm_max_connections; |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (signalled()) { |
| 158 | flush_signals(current); |
J. Bruce Fields | 89996df | 2009-05-06 16:32:54 -0400 | [diff] [blame] | 159 | restart_grace(); |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 160 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
J. Bruce Fields | 8fafa90 | 2008-01-24 11:11:34 -0500 | [diff] [blame] | 163 | timeout = nlmsvc_retry_blocked(); |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | /* |
| 166 | * Find a socket with data available and call its |
| 167 | * recvfrom routine. |
| 168 | */ |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 169 | err = svc_recv(rqstp, timeout); |
J. Bruce Fields | 5b444cc | 2012-08-17 21:47:53 -0400 | [diff] [blame] | 170 | if (err == -EAGAIN || err == -EINTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | continue; |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 172 | dprintk("lockd: request from %s\n", |
| 173 | svc_print_addr(rqstp, buf, sizeof(buf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 175 | svc_process(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Trond Myklebust | 2bd6157 | 2006-01-03 09:55:19 +0100 | [diff] [blame] | 177 | flush_signals(current); |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 178 | if (nlmsvc_ops) |
| 179 | nlmsvc_invalidate_all(); |
| 180 | nlm_shutdown_hosts(); |
J. Bruce Fields | efda760 | 2017-03-28 21:25:08 -0400 | [diff] [blame] | 181 | cancel_delayed_work_sync(&ln->grace_period_end); |
| 182 | locks_end_grace(&ln->lockd_manager); |
NeilBrown | 6a4e252 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 183 | |
| 184 | dprintk("lockd_down: service stopped\n"); |
| 185 | |
| 186 | svc_exit_thread(rqstp); |
| 187 | |
Linus Torvalds | 35ce8ae | 2022-01-17 05:49:30 +0200 | [diff] [blame] | 188 | module_put_and_kthread_exit(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Chuck Lever | eb16e90 | 2009-03-18 20:47:59 -0400 | [diff] [blame] | 191 | static int create_lockd_listener(struct svc_serv *serv, const char *name, |
Stanislav Kinsbursky | c228fa2 | 2012-01-31 15:07:48 +0400 | [diff] [blame] | 192 | struct net *net, const int family, |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 193 | const unsigned short port, |
| 194 | const struct cred *cred) |
Chuck Lever | d3fe5ea | 2008-12-31 16:06:04 -0500 | [diff] [blame] | 195 | { |
| 196 | struct svc_xprt *xprt; |
| 197 | |
Stanislav Kinsbursky | c228fa2 | 2012-01-31 15:07:48 +0400 | [diff] [blame] | 198 | xprt = svc_find_xprt(serv, name, net, family, 0); |
Chuck Lever | d3fe5ea | 2008-12-31 16:06:04 -0500 | [diff] [blame] | 199 | if (xprt == NULL) |
Stanislav Kinsbursky | c228fa2 | 2012-01-31 15:07:48 +0400 | [diff] [blame] | 200 | return svc_create_xprt(serv, name, net, family, port, |
Trond Myklebust | 4df493a | 2019-04-09 12:13:37 -0400 | [diff] [blame] | 201 | SVC_SOCK_DEFAULTS, cred); |
Chuck Lever | d3fe5ea | 2008-12-31 16:06:04 -0500 | [diff] [blame] | 202 | svc_xprt_put(xprt); |
| 203 | return 0; |
| 204 | } |
| 205 | |
Stanislav Kinsbursky | c228fa2 | 2012-01-31 15:07:48 +0400 | [diff] [blame] | 206 | static int create_lockd_family(struct svc_serv *serv, struct net *net, |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 207 | const int family, const struct cred *cred) |
Chuck Lever | eb16e90 | 2009-03-18 20:47:59 -0400 | [diff] [blame] | 208 | { |
| 209 | int err; |
| 210 | |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 211 | err = create_lockd_listener(serv, "udp", net, family, nlm_udpport, |
| 212 | cred); |
Chuck Lever | eb16e90 | 2009-03-18 20:47:59 -0400 | [diff] [blame] | 213 | if (err < 0) |
| 214 | return err; |
| 215 | |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 216 | return create_lockd_listener(serv, "tcp", net, family, nlm_tcpport, |
| 217 | cred); |
Chuck Lever | eb16e90 | 2009-03-18 20:47:59 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 220 | /* |
Chuck Lever | 8c3916f | 2008-10-03 17:15:23 -0400 | [diff] [blame] | 221 | * Ensure there are active UDP and TCP listeners for lockd. |
| 222 | * |
| 223 | * Even if we have only TCP NFS mounts and/or TCP NFSDs, some |
| 224 | * local services (such as rpc.statd) still require UDP, and |
| 225 | * some NFS servers do not yet support NLM over TCP. |
| 226 | * |
| 227 | * Returns zero if all listeners are available; otherwise a |
| 228 | * negative errno value is returned. |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 229 | */ |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 230 | static int make_socks(struct svc_serv *serv, struct net *net, |
| 231 | const struct cred *cred) |
NeilBrown | 24e3666 | 2006-10-02 02:17:45 -0700 | [diff] [blame] | 232 | { |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 233 | static int warned; |
Chuck Lever | 0dba7c2 | 2008-12-31 16:06:11 -0500 | [diff] [blame] | 234 | int err; |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 235 | |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 236 | err = create_lockd_family(serv, net, PF_INET, cred); |
Chuck Lever | 0dba7c2 | 2008-12-31 16:06:11 -0500 | [diff] [blame] | 237 | if (err < 0) |
| 238 | goto out_err; |
| 239 | |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 240 | err = create_lockd_family(serv, net, PF_INET6, cred); |
Chuck Lever | eb16e90 | 2009-03-18 20:47:59 -0400 | [diff] [blame] | 241 | if (err < 0 && err != -EAFNOSUPPORT) |
Chuck Lever | 0dba7c2 | 2008-12-31 16:06:11 -0500 | [diff] [blame] | 242 | goto out_err; |
| 243 | |
| 244 | warned = 0; |
| 245 | return 0; |
| 246 | |
| 247 | out_err: |
| 248 | if (warned++ == 0) |
NeilBrown | 7dcf91e | 2006-10-02 02:17:52 -0700 | [diff] [blame] | 249 | printk(KERN_WARNING |
Chuck Lever | 0dba7c2 | 2008-12-31 16:06:11 -0500 | [diff] [blame] | 250 | "lockd_up: makesock failed, error=%d\n", err); |
Jeff Layton | 679b033 | 2014-03-25 11:55:26 -0700 | [diff] [blame] | 251 | svc_shutdown_net(serv, net); |
NeilBrown | 24e3666 | 2006-10-02 02:17:45 -0700 | [diff] [blame] | 252 | return err; |
| 253 | } |
| 254 | |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 255 | static int lockd_up_net(struct svc_serv *serv, struct net *net, |
| 256 | const struct cred *cred) |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 257 | { |
| 258 | struct lockd_net *ln = net_generic(net, lockd_net_id); |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 259 | int error; |
| 260 | |
Stanislav Kinsbursky | 786185b | 2012-05-04 12:49:41 +0400 | [diff] [blame] | 261 | if (ln->nlmsvc_users++) |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 262 | return 0; |
| 263 | |
Stanislav Kinsbursky | dbf9b5d7 | 2012-04-25 18:22:47 +0400 | [diff] [blame] | 264 | error = svc_bind(serv, net); |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 265 | if (error) |
Stanislav Kinsbursky | dbf9b5d7 | 2012-04-25 18:22:47 +0400 | [diff] [blame] | 266 | goto err_bind; |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 267 | |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 268 | error = make_socks(serv, net, cred); |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 269 | if (error < 0) |
J. Bruce Fields | 7c17705 | 2014-08-29 16:25:50 -0400 | [diff] [blame] | 270 | goto err_bind; |
Stanislav Kinsbursky | 5630f7f | 2012-07-25 16:57:29 +0400 | [diff] [blame] | 271 | set_grace_period(net); |
Vasily Averin | e919b07 | 2017-11-08 08:55:55 +0300 | [diff] [blame] | 272 | dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum); |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 273 | return 0; |
| 274 | |
Stanislav Kinsbursky | dbf9b5d7 | 2012-04-25 18:22:47 +0400 | [diff] [blame] | 275 | err_bind: |
Stanislav Kinsbursky | 786185b | 2012-05-04 12:49:41 +0400 | [diff] [blame] | 276 | ln->nlmsvc_users--; |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 277 | return error; |
| 278 | } |
| 279 | |
Stanislav Kinsbursky | 4db7769 | 2012-04-25 18:22:40 +0400 | [diff] [blame] | 280 | static void lockd_down_net(struct svc_serv *serv, struct net *net) |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 281 | { |
| 282 | struct lockd_net *ln = net_generic(net, lockd_net_id); |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 283 | |
| 284 | if (ln->nlmsvc_users) { |
Stanislav Kinsbursky | 3b64739 | 2012-01-31 15:08:29 +0400 | [diff] [blame] | 285 | if (--ln->nlmsvc_users == 0) { |
| 286 | nlm_shutdown_hosts_net(net); |
Vasily Averin | 3a2b19d | 2017-11-02 13:03:42 +0300 | [diff] [blame] | 287 | cancel_delayed_work_sync(&ln->grace_period_end); |
| 288 | locks_end_grace(&ln->lockd_manager); |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 289 | svc_shutdown_net(serv, net); |
Vasily Averin | e919b07 | 2017-11-08 08:55:55 +0300 | [diff] [blame] | 290 | dprintk("%s: per-net data destroyed; net=%x\n", |
| 291 | __func__, net->ns.inum); |
Stanislav Kinsbursky | 3b64739 | 2012-01-31 15:08:29 +0400 | [diff] [blame] | 292 | } |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 293 | } else { |
NeilBrown | 6b044fb | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 294 | pr_err("%s: no users! net=%x\n", |
| 295 | __func__, net->ns.inum); |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 296 | BUG(); |
| 297 | } |
| 298 | } |
| 299 | |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 300 | static int lockd_inetaddr_event(struct notifier_block *this, |
| 301 | unsigned long event, void *ptr) |
| 302 | { |
| 303 | struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; |
| 304 | struct sockaddr_in sin; |
| 305 | |
NeilBrown | 5a8a7ff | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 306 | if (event != NETDEV_DOWN) |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 307 | goto out; |
| 308 | |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 309 | if (nlmsvc_serv) { |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 310 | dprintk("lockd_inetaddr_event: removed %pI4\n", |
| 311 | &ifa->ifa_local); |
| 312 | sin.sin_family = AF_INET; |
| 313 | sin.sin_addr.s_addr = ifa->ifa_local; |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 314 | svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin); |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | out: |
| 318 | return NOTIFY_DONE; |
| 319 | } |
| 320 | |
| 321 | static struct notifier_block lockd_inetaddr_notifier = { |
| 322 | .notifier_call = lockd_inetaddr_event, |
| 323 | }; |
| 324 | |
| 325 | #if IS_ENABLED(CONFIG_IPV6) |
| 326 | static int lockd_inet6addr_event(struct notifier_block *this, |
| 327 | unsigned long event, void *ptr) |
| 328 | { |
| 329 | struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr; |
| 330 | struct sockaddr_in6 sin6; |
| 331 | |
NeilBrown | 5a8a7ff | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 332 | if (event != NETDEV_DOWN) |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 333 | goto out; |
| 334 | |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 335 | if (nlmsvc_serv) { |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 336 | dprintk("lockd_inet6addr_event: removed %pI6\n", &ifa->addr); |
| 337 | sin6.sin6_family = AF_INET6; |
| 338 | sin6.sin6_addr = ifa->addr; |
Scott Mayhew | c01410f | 2017-01-05 16:34:50 -0500 | [diff] [blame] | 339 | if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL) |
| 340 | sin6.sin6_scope_id = ifa->idev->dev->ifindex; |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 341 | svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin6); |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | out: |
| 345 | return NOTIFY_DONE; |
| 346 | } |
| 347 | |
| 348 | static struct notifier_block lockd_inet6addr_notifier = { |
| 349 | .notifier_call = lockd_inet6addr_event, |
| 350 | }; |
| 351 | #endif |
| 352 | |
Chuck Lever | afea565 | 2017-08-01 12:00:06 -0400 | [diff] [blame] | 353 | static const struct svc_serv_ops lockd_sv_ops = { |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 354 | .svo_shutdown = svc_rpcb_cleanup, |
NeilBrown | 6b044fb | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 355 | .svo_function = lockd, |
Jeff Layton | b9e13cd | 2015-06-08 12:06:51 -0700 | [diff] [blame] | 356 | .svo_enqueue_xprt = svc_xprt_do_enqueue, |
NeilBrown | 6b044fb | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 357 | .svo_module = THIS_MODULE, |
Jeff Layton | ea126e7 | 2015-06-08 12:03:32 -0700 | [diff] [blame] | 358 | }; |
| 359 | |
NeilBrown | ecd3ad6 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 360 | static int lockd_get(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Jeff Layton | d751a7c | 2008-02-07 16:34:55 -0500 | [diff] [blame] | 362 | struct svc_serv *serv; |
NeilBrown | b73a297 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 363 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 365 | if (nlmsvc_serv) { |
| 366 | svc_get(nlmsvc_serv); |
NeilBrown | ecd3ad6 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 367 | nlmsvc_users++; |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 368 | return 0; |
| 369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * Sanity check: if there's no pid, |
| 373 | * we should be the first user ... |
| 374 | */ |
NeilBrown | 4a3ae42 | 2006-10-02 02:17:53 -0700 | [diff] [blame] | 375 | if (nlmsvc_users) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | printk(KERN_WARNING |
| 377 | "lockd_up: no pid, %d users??\n", nlmsvc_users); |
| 378 | |
Trond Myklebust | 06bed7d | 2015-01-02 15:05:25 -0500 | [diff] [blame] | 379 | if (!nlm_timeout) |
| 380 | nlm_timeout = LOCKD_DFLT_TIMEO; |
| 381 | nlmsvc_timeout = nlm_timeout * HZ; |
| 382 | |
Jeff Layton | ea126e7 | 2015-06-08 12:03:32 -0700 | [diff] [blame] | 383 | serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, &lockd_sv_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (!serv) { |
| 385 | printk(KERN_WARNING "lockd_up: create service failed\n"); |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 386 | return -ENOMEM; |
Stanislav Kinsbursky | 2445223 | 2012-04-25 18:22:54 +0400 | [diff] [blame] | 387 | } |
NeilBrown | b73a297 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 388 | |
NeilBrown | 6b044fb | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 389 | serv->sv_maxconn = nlm_max_connections; |
| 390 | error = svc_set_num_threads(serv, NULL, 1); |
NeilBrown | b73a297 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 391 | /* The thread now holds the only reference */ |
| 392 | svc_put(serv); |
| 393 | if (error < 0) |
| 394 | return error; |
| 395 | |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 396 | nlmsvc_serv = serv; |
Scott Mayhew | 0751ddf | 2015-12-11 16:46:00 -0500 | [diff] [blame] | 397 | register_inetaddr_notifier(&lockd_inetaddr_notifier); |
| 398 | #if IS_ENABLED(CONFIG_IPV6) |
| 399 | register_inet6addr_notifier(&lockd_inet6addr_notifier); |
| 400 | #endif |
Stanislav Kinsbursky | 8dbf28e | 2012-04-25 18:23:16 +0400 | [diff] [blame] | 401 | dprintk("lockd_up: service created\n"); |
NeilBrown | ecd3ad6 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 402 | nlmsvc_users++; |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 403 | return 0; |
Stanislav Kinsbursky | 2445223 | 2012-04-25 18:22:54 +0400 | [diff] [blame] | 404 | } |
| 405 | |
NeilBrown | 865b674 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 406 | static void lockd_put(void) |
| 407 | { |
| 408 | if (WARN(nlmsvc_users <= 0, "lockd_down: no users!\n")) |
| 409 | return; |
| 410 | if (--nlmsvc_users) |
| 411 | return; |
| 412 | |
| 413 | unregister_inetaddr_notifier(&lockd_inetaddr_notifier); |
| 414 | #if IS_ENABLED(CONFIG_IPV6) |
| 415 | unregister_inet6addr_notifier(&lockd_inet6addr_notifier); |
| 416 | #endif |
| 417 | |
NeilBrown | 6b044fb | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 418 | svc_set_num_threads(nlmsvc_serv, NULL, 0); |
NeilBrown | 865b674 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 419 | nlmsvc_serv = NULL; |
| 420 | dprintk("lockd_down: service destroyed\n"); |
| 421 | } |
| 422 | |
Stanislav Kinsbursky | 2445223 | 2012-04-25 18:22:54 +0400 | [diff] [blame] | 423 | /* |
| 424 | * Bring up the lockd process if it's not already up. |
| 425 | */ |
Trond Myklebust | 40373b1 | 2019-04-09 12:13:39 -0400 | [diff] [blame] | 426 | int lockd_up(struct net *net, const struct cred *cred) |
Stanislav Kinsbursky | 2445223 | 2012-04-25 18:22:54 +0400 | [diff] [blame] | 427 | { |
Stanislav Kinsbursky | 7d13ec7 | 2012-04-25 18:23:02 +0400 | [diff] [blame] | 428 | int error; |
Stanislav Kinsbursky | 2445223 | 2012-04-25 18:22:54 +0400 | [diff] [blame] | 429 | |
| 430 | mutex_lock(&nlmsvc_mutex); |
| 431 | |
NeilBrown | ecd3ad6 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 432 | error = lockd_get(); |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 433 | if (error) |
NeilBrown | 865b674 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 434 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
NeilBrown | b73a297 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 436 | error = lockd_up_net(nlmsvc_serv, net, cred); |
Vasily Averin | dc3033e | 2017-10-20 17:33:18 +0300 | [diff] [blame] | 437 | if (error < 0) { |
NeilBrown | 865b674 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 438 | lockd_put(); |
| 439 | goto err; |
Vasily Averin | dc3033e | 2017-10-20 17:33:18 +0300 | [diff] [blame] | 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
NeilBrown | 865b674 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 442 | err: |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 443 | mutex_unlock(&nlmsvc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | return error; |
| 445 | } |
Trond Myklebust | 2de5987 | 2008-12-23 15:21:33 -0500 | [diff] [blame] | 446 | EXPORT_SYMBOL_GPL(lockd_up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | /* |
| 449 | * Decrement the user count and bring down lockd if we're the last. |
| 450 | */ |
| 451 | void |
Stanislav Kinsbursky | e3f70ea | 2012-03-29 18:54:33 +0400 | [diff] [blame] | 452 | lockd_down(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 454 | mutex_lock(&nlmsvc_mutex); |
NeilBrown | 2840fe8 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 455 | lockd_down_net(nlmsvc_serv, net); |
NeilBrown | 865b674 | 2021-11-29 15:51:25 +1100 | [diff] [blame] | 456 | lockd_put(); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 457 | mutex_unlock(&nlmsvc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
Trond Myklebust | 2de5987 | 2008-12-23 15:21:33 -0500 | [diff] [blame] | 459 | EXPORT_SYMBOL_GPL(lockd_down); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Chuck Lever | 90d5b18 | 2008-03-14 14:18:30 -0400 | [diff] [blame] | 461 | #ifdef CONFIG_SYSCTL |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* |
| 464 | * Sysctl parameters (same as module parameters, different interface). |
| 465 | */ |
| 466 | |
Joe Perches | 7ac9fe5 | 2014-06-06 14:38:02 -0700 | [diff] [blame] | 467 | static struct ctl_table nlm_sysctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | .procname = "nlm_grace_period", |
| 470 | .data = &nlm_grace_period, |
Steve Dickson | 7ee91ec | 2005-07-13 01:10:47 -0700 | [diff] [blame] | 471 | .maxlen = sizeof(unsigned long), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 473 | .proc_handler = proc_doulongvec_minmax, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | .extra1 = (unsigned long *) &nlm_grace_period_min, |
| 475 | .extra2 = (unsigned long *) &nlm_grace_period_max, |
| 476 | }, |
| 477 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | .procname = "nlm_timeout", |
| 479 | .data = &nlm_timeout, |
Steve Dickson | 7ee91ec | 2005-07-13 01:10:47 -0700 | [diff] [blame] | 480 | .maxlen = sizeof(unsigned long), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 482 | .proc_handler = proc_doulongvec_minmax, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | .extra1 = (unsigned long *) &nlm_timeout_min, |
| 484 | .extra2 = (unsigned long *) &nlm_timeout_max, |
| 485 | }, |
| 486 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | .procname = "nlm_udpport", |
| 488 | .data = &nlm_udpport, |
| 489 | .maxlen = sizeof(int), |
| 490 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 491 | .proc_handler = proc_dointvec_minmax, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | .extra1 = (int *) &nlm_port_min, |
| 493 | .extra2 = (int *) &nlm_port_max, |
| 494 | }, |
| 495 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | .procname = "nlm_tcpport", |
| 497 | .data = &nlm_tcpport, |
| 498 | .maxlen = sizeof(int), |
| 499 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 500 | .proc_handler = proc_dointvec_minmax, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | .extra1 = (int *) &nlm_port_min, |
| 502 | .extra2 = (int *) &nlm_port_max, |
| 503 | }, |
Olaf Kirch | abd1f50 | 2006-10-04 02:16:01 -0700 | [diff] [blame] | 504 | { |
Olaf Kirch | abd1f50 | 2006-10-04 02:16:01 -0700 | [diff] [blame] | 505 | .procname = "nsm_use_hostnames", |
| 506 | .data = &nsm_use_hostnames, |
| 507 | .maxlen = sizeof(int), |
| 508 | .mode = 0644, |
Jia He | d02a3a2 | 2021-08-03 12:59:37 +0200 | [diff] [blame] | 509 | .proc_handler = proc_dobool, |
Olaf Kirch | abd1f50 | 2006-10-04 02:16:01 -0700 | [diff] [blame] | 510 | }, |
Olaf Kirch | 460f5ca | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 511 | { |
Olaf Kirch | 460f5ca | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 512 | .procname = "nsm_local_state", |
| 513 | .data = &nsm_local_state, |
| 514 | .maxlen = sizeof(int), |
| 515 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 516 | .proc_handler = proc_dointvec, |
Olaf Kirch | 460f5ca | 2006-10-04 02:16:03 -0700 | [diff] [blame] | 517 | }, |
Eric W. Biederman | ab09203 | 2009-11-05 14:25:10 -0800 | [diff] [blame] | 518 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | }; |
| 520 | |
Joe Perches | 7ac9fe5 | 2014-06-06 14:38:02 -0700 | [diff] [blame] | 521 | static struct ctl_table nlm_sysctl_dir[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | .procname = "nfs", |
| 524 | .mode = 0555, |
| 525 | .child = nlm_sysctls, |
| 526 | }, |
Eric W. Biederman | ab09203 | 2009-11-05 14:25:10 -0800 | [diff] [blame] | 527 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | }; |
| 529 | |
Joe Perches | 7ac9fe5 | 2014-06-06 14:38:02 -0700 | [diff] [blame] | 530 | static struct ctl_table nlm_sysctl_root[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | .procname = "fs", |
| 533 | .mode = 0555, |
| 534 | .child = nlm_sysctl_dir, |
| 535 | }, |
Eric W. Biederman | ab09203 | 2009-11-05 14:25:10 -0800 | [diff] [blame] | 536 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | }; |
| 538 | |
Chuck Lever | 90d5b18 | 2008-03-14 14:18:30 -0400 | [diff] [blame] | 539 | #endif /* CONFIG_SYSCTL */ |
| 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | /* |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 542 | * Module (and sysfs) parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | */ |
| 544 | |
| 545 | #define param_set_min_max(name, type, which_strtol, min, max) \ |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 546 | static int param_set_##name(const char *val, const struct kernel_param *kp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { \ |
| 548 | char *endp; \ |
| 549 | __typeof__(type) num = which_strtol(val, &endp, 0); \ |
| 550 | if (endp == val || *endp || num < (min) || num > (max)) \ |
| 551 | return -EINVAL; \ |
NeilBrown | de5b8e8 | 2012-02-07 15:35:42 +1100 | [diff] [blame] | 552 | *((type *) kp->arg) = num; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return 0; \ |
| 554 | } |
| 555 | |
| 556 | static inline int is_callback(u32 proc) |
| 557 | { |
| 558 | return proc == NLMPROC_GRANTED |
| 559 | || proc == NLMPROC_GRANTED_MSG |
| 560 | || proc == NLMPROC_TEST_RES |
| 561 | || proc == NLMPROC_LOCK_RES |
| 562 | || proc == NLMPROC_CANCEL_RES |
| 563 | || proc == NLMPROC_UNLOCK_RES |
| 564 | || proc == NLMPROC_NSM_NOTIFY; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | static int lockd_authenticate(struct svc_rqst *rqstp) |
| 569 | { |
| 570 | rqstp->rq_client = NULL; |
| 571 | switch (rqstp->rq_authop->flavour) { |
| 572 | case RPC_AUTH_NULL: |
| 573 | case RPC_AUTH_UNIX: |
Chuck Lever | 5c2465d | 2021-07-15 15:52:12 -0400 | [diff] [blame] | 574 | rqstp->rq_auth_stat = rpc_auth_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | if (rqstp->rq_proc == 0) |
| 576 | return SVC_OK; |
| 577 | if (is_callback(rqstp->rq_proc)) { |
| 578 | /* Leave it to individual procedures to |
| 579 | * call nlmsvc_lookup_host(rqstp) |
| 580 | */ |
| 581 | return SVC_OK; |
| 582 | } |
| 583 | return svc_set_client(rqstp); |
| 584 | } |
Chuck Lever | 5c2465d | 2021-07-15 15:52:12 -0400 | [diff] [blame] | 585 | rqstp->rq_auth_stat = rpc_autherr_badcred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | return SVC_DENIED; |
| 587 | } |
| 588 | |
| 589 | |
| 590 | param_set_min_max(port, int, simple_strtol, 0, 65535) |
| 591 | param_set_min_max(grace_period, unsigned long, simple_strtoul, |
| 592 | nlm_grace_period_min, nlm_grace_period_max) |
| 593 | param_set_min_max(timeout, unsigned long, simple_strtoul, |
| 594 | nlm_timeout_min, nlm_timeout_max) |
| 595 | |
| 596 | MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); |
| 597 | MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION "."); |
| 598 | MODULE_LICENSE("GPL"); |
| 599 | |
| 600 | module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong, |
| 601 | &nlm_grace_period, 0644); |
| 602 | module_param_call(nlm_timeout, param_set_timeout, param_get_ulong, |
| 603 | &nlm_timeout, 0644); |
| 604 | module_param_call(nlm_udpport, param_set_port, param_get_int, |
| 605 | &nlm_udpport, 0644); |
| 606 | module_param_call(nlm_tcpport, param_set_port, param_get_int, |
| 607 | &nlm_tcpport, 0644); |
Olaf Kirch | abd1f50 | 2006-10-04 02:16:01 -0700 | [diff] [blame] | 608 | module_param(nsm_use_hostnames, bool, 0644); |
Jeff Layton | c72a476 | 2008-10-20 11:51:58 -0400 | [diff] [blame] | 609 | module_param(nlm_max_connections, uint, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 611 | static int lockd_init_net(struct net *net) |
| 612 | { |
Stanislav Kinsbursky | 66547b0 | 2012-07-25 16:56:43 +0400 | [diff] [blame] | 613 | struct lockd_net *ln = net_generic(net, lockd_net_id); |
| 614 | |
| 615 | INIT_DELAYED_WORK(&ln->grace_period_end, grace_ender); |
Jeff Layton | f779002 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 616 | INIT_LIST_HEAD(&ln->lockd_manager.list); |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 617 | ln->lockd_manager.block_opens = false; |
Andrey Ryabinin | 0ad9547 | 2015-09-23 15:49:29 +0300 | [diff] [blame] | 618 | INIT_LIST_HEAD(&ln->nsm_handles); |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static void lockd_exit_net(struct net *net) |
| 623 | { |
Vasily Averin | a3152f1 | 2017-11-06 16:23:24 +0300 | [diff] [blame] | 624 | struct lockd_net *ln = net_generic(net, lockd_net_id); |
| 625 | |
| 626 | WARN_ONCE(!list_empty(&ln->lockd_manager.list), |
| 627 | "net %x %s: lockd_manager.list is not empty\n", |
| 628 | net->ns.inum, __func__); |
| 629 | WARN_ONCE(!list_empty(&ln->nsm_handles), |
| 630 | "net %x %s: nsm_handles list is not empty\n", |
| 631 | net->ns.inum, __func__); |
| 632 | WARN_ONCE(delayed_work_pending(&ln->grace_period_end), |
| 633 | "net %x %s: grace_period_end was not cancelled\n", |
| 634 | net->ns.inum, __func__); |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | static struct pernet_operations lockd_net_ops = { |
| 638 | .init = lockd_init_net, |
| 639 | .exit = lockd_exit_net, |
| 640 | .id = &lockd_net_id, |
| 641 | .size = sizeof(struct lockd_net), |
| 642 | }; |
| 643 | |
| 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | /* |
| 646 | * Initialising and terminating the module. |
| 647 | */ |
| 648 | |
| 649 | static int __init init_nlm(void) |
| 650 | { |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 651 | int err; |
| 652 | |
Chuck Lever | 90d5b18 | 2008-03-14 14:18:30 -0400 | [diff] [blame] | 653 | #ifdef CONFIG_SYSCTL |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 654 | err = -ENOMEM; |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 655 | nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root); |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 656 | if (nlm_sysctl_table == NULL) |
| 657 | goto err_sysctl; |
Chuck Lever | 90d5b18 | 2008-03-14 14:18:30 -0400 | [diff] [blame] | 658 | #endif |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 659 | err = register_pernet_subsys(&lockd_net_ops); |
| 660 | if (err) |
| 661 | goto err_pernet; |
Jeff Layton | d68e3c4 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 662 | |
| 663 | err = lockd_create_procfs(); |
| 664 | if (err) |
| 665 | goto err_procfs; |
| 666 | |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 667 | return 0; |
| 668 | |
Jeff Layton | d68e3c4 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 669 | err_procfs: |
| 670 | unregister_pernet_subsys(&lockd_net_ops); |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 671 | err_pernet: |
| 672 | #ifdef CONFIG_SYSCTL |
| 673 | unregister_sysctl_table(nlm_sysctl_table); |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 674 | err_sysctl: |
Kees Cook | 12dd7ec | 2014-05-01 14:15:02 -0700 | [diff] [blame] | 675 | #endif |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 676 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static void __exit exit_nlm(void) |
| 680 | { |
| 681 | /* FIXME: delete all NLM clients */ |
| 682 | nlm_shutdown_hosts(); |
Jeff Layton | d68e3c4 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 683 | lockd_remove_procfs(); |
Stanislav Kinsbursky | a9c5d73 | 2012-01-31 15:07:57 +0400 | [diff] [blame] | 684 | unregister_pernet_subsys(&lockd_net_ops); |
Chuck Lever | 90d5b18 | 2008-03-14 14:18:30 -0400 | [diff] [blame] | 685 | #ifdef CONFIG_SYSCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | unregister_sysctl_table(nlm_sysctl_table); |
Chuck Lever | 90d5b18 | 2008-03-14 14:18:30 -0400 | [diff] [blame] | 687 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | module_init(init_nlm); |
| 691 | module_exit(exit_nlm); |
| 692 | |
Chuck Lever | a9ad1a8 | 2021-06-03 16:50:46 -0400 | [diff] [blame] | 693 | /** |
| 694 | * nlmsvc_dispatch - Process an NLM Request |
| 695 | * @rqstp: incoming request |
| 696 | * @statp: pointer to location of accept_stat field in RPC Reply buffer |
| 697 | * |
| 698 | * Return values: |
| 699 | * %0: Processing complete; do not send a Reply |
| 700 | * %1: Processing complete; send Reply in rqstp->rq_res |
| 701 | */ |
| 702 | static int nlmsvc_dispatch(struct svc_rqst *rqstp, __be32 *statp) |
| 703 | { |
| 704 | const struct svc_procedure *procp = rqstp->rq_procinfo; |
Chuck Lever | a9ad1a8 | 2021-06-03 16:50:46 -0400 | [diff] [blame] | 705 | |
| 706 | svcxdr_init_decode(rqstp); |
Chuck Lever | 16c6636 | 2021-10-12 11:57:22 -0400 | [diff] [blame] | 707 | if (!procp->pc_decode(rqstp, &rqstp->rq_arg_stream)) |
Chuck Lever | a9ad1a8 | 2021-06-03 16:50:46 -0400 | [diff] [blame] | 708 | goto out_decode_err; |
| 709 | |
| 710 | *statp = procp->pc_func(rqstp); |
| 711 | if (*statp == rpc_drop_reply) |
| 712 | return 0; |
| 713 | if (*statp != rpc_success) |
| 714 | return 1; |
| 715 | |
| 716 | svcxdr_init_encode(rqstp); |
Chuck Lever | fda4944 | 2021-10-13 10:41:06 -0400 | [diff] [blame] | 717 | if (!procp->pc_encode(rqstp, &rqstp->rq_res_stream)) |
Chuck Lever | a9ad1a8 | 2021-06-03 16:50:46 -0400 | [diff] [blame] | 718 | goto out_encode_err; |
| 719 | |
| 720 | return 1; |
| 721 | |
| 722 | out_decode_err: |
| 723 | *statp = rpc_garbage_args; |
| 724 | return 1; |
| 725 | |
| 726 | out_encode_err: |
| 727 | *statp = rpc_system_err; |
| 728 | return 1; |
| 729 | } |
| 730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | /* |
| 732 | * Define NLM program and procedures |
| 733 | */ |
Christoph Hellwig | 7fd38af | 2017-05-08 23:40:27 +0200 | [diff] [blame] | 734 | static unsigned int nlmsvc_version1_count[17]; |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 735 | static const struct svc_version nlmsvc_version1 = { |
| 736 | .vs_vers = 1, |
| 737 | .vs_nproc = 17, |
| 738 | .vs_proc = nlmsvc_procedures, |
| 739 | .vs_count = nlmsvc_version1_count, |
Chuck Lever | a9ad1a8 | 2021-06-03 16:50:46 -0400 | [diff] [blame] | 740 | .vs_dispatch = nlmsvc_dispatch, |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 741 | .vs_xdrsize = NLMSVC_XDRSIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | }; |
Christoph Hellwig | 7fd38af | 2017-05-08 23:40:27 +0200 | [diff] [blame] | 743 | static unsigned int nlmsvc_version3_count[24]; |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 744 | static const struct svc_version nlmsvc_version3 = { |
| 745 | .vs_vers = 3, |
| 746 | .vs_nproc = 24, |
| 747 | .vs_proc = nlmsvc_procedures, |
| 748 | .vs_count = nlmsvc_version3_count, |
Chuck Lever | a9ad1a8 | 2021-06-03 16:50:46 -0400 | [diff] [blame] | 749 | .vs_dispatch = nlmsvc_dispatch, |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 750 | .vs_xdrsize = NLMSVC_XDRSIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | }; |
| 752 | #ifdef CONFIG_LOCKD_V4 |
Christoph Hellwig | 7fd38af | 2017-05-08 23:40:27 +0200 | [diff] [blame] | 753 | static unsigned int nlmsvc_version4_count[24]; |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 754 | static const struct svc_version nlmsvc_version4 = { |
| 755 | .vs_vers = 4, |
| 756 | .vs_nproc = 24, |
| 757 | .vs_proc = nlmsvc_procedures4, |
| 758 | .vs_count = nlmsvc_version4_count, |
Chuck Lever | a9ad1a8 | 2021-06-03 16:50:46 -0400 | [diff] [blame] | 759 | .vs_dispatch = nlmsvc_dispatch, |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 760 | .vs_xdrsize = NLMSVC_XDRSIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | }; |
| 762 | #endif |
Christoph Hellwig | e967918 | 2017-05-12 16:21:37 +0200 | [diff] [blame] | 763 | static const struct svc_version *nlmsvc_version[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | [1] = &nlmsvc_version1, |
| 765 | [3] = &nlmsvc_version3, |
| 766 | #ifdef CONFIG_LOCKD_V4 |
| 767 | [4] = &nlmsvc_version4, |
| 768 | #endif |
| 769 | }; |
| 770 | |
| 771 | static struct svc_stat nlmsvc_stats; |
| 772 | |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 773 | #define NLM_NRVERS ARRAY_SIZE(nlmsvc_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | static struct svc_program nlmsvc_program = { |
| 775 | .pg_prog = NLM_PROGRAM, /* program number */ |
| 776 | .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */ |
| 777 | .pg_vers = nlmsvc_version, /* version table */ |
| 778 | .pg_name = "lockd", /* service name */ |
| 779 | .pg_class = "nfsd", /* share authentication with nfsd */ |
| 780 | .pg_stats = &nlmsvc_stats, /* stats table */ |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 781 | .pg_authenticate = &lockd_authenticate, /* export authentication */ |
| 782 | .pg_init_request = svc_generic_init_request, |
Trond Myklebust | 642ee6b | 2019-04-09 11:46:17 -0400 | [diff] [blame] | 783 | .pg_rpcbind_set = svc_generic_rpcbind_set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | }; |