blob: 6579948070a482a9d4f8742682a248ee929aa7a7 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/sysctl.h>
19#include <linux/moduleparam.h>
20
Ingo Molnar3f07c012017-02-08 18:51:30 +010021#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/in.h>
24#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/smp.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080026#include <linux/mutex.h>
Jeff Laytond751a7c2008-02-07 16:34:55 -050027#include <linux/kthread.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070028#include <linux/freezer.h>
Scott Mayhew0751ddf2015-12-11 16:46:00 -050029#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
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 Mayhew0751ddf2015-12-11 16:46:00 -050036#include <linux/sunrpc/svc_xprt.h>
NeilBrown24e36662006-10-02 02:17:45 -070037#include <net/ip.h>
Scott Mayhew0751ddf2015-12-11 16:46:00 -050038#include <net/addrconf.h>
39#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/lockd/lockd.h>
41#include <linux/nfs.h>
42
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +040043#include "netns.h"
Jeff Laytond68e3c42014-09-12 16:40:20 -040044#include "procfs.h"
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +040045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define NLMDBG_FACILITY NLMDBG_SVC
47#define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static struct svc_program nlmsvc_program;
50
Julia Lawall2a297452015-12-23 22:25:13 +010051const struct nlmsvc_binding *nlmsvc_ops;
Trond Myklebust2de59872008-12-23 15:21:33 -050052EXPORT_SYMBOL_GPL(nlmsvc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ingo Molnar353ab6e2006-03-26 01:37:12 -080054static DEFINE_MUTEX(nlmsvc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static unsigned int nlmsvc_users;
NeilBrown2840fe82021-11-29 15:51:25 +110056static struct svc_serv *nlmsvc_serv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057unsigned long nlmsvc_timeout;
58
NeilBrownc743b422023-07-18 16:38:08 +100059static void nlmsvc_request_retry(struct timer_list *tl)
60{
61 svc_wake_up(nlmsvc_serv);
62}
63DEFINE_TIMER(nlmsvc_retry, nlmsvc_request_retry);
64
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030065unsigned int lockd_net_id;
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +040066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * These can be set at insmod time (useful for NFS as root filesystem),
69 * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003
70 */
71static unsigned long nlm_grace_period;
72static unsigned long nlm_timeout = LOCKD_DFLT_TIMEO;
73static int nlm_udpport, nlm_tcpport;
74
Jeff Laytonc72a4762008-10-20 11:51:58 -040075/* RLIM_NOFILE defaults to 1024. That seems like a reasonable default here. */
76static unsigned int nlm_max_connections = 1024;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
79 * Constants needed for the sysctl interface.
80 */
81static const unsigned long nlm_grace_period_min = 0;
82static const unsigned long nlm_grace_period_max = 240;
83static const unsigned long nlm_timeout_min = 3;
84static const unsigned long nlm_timeout_max = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Chuck Lever90d5b182008-03-14 14:18:30 -040086#ifdef CONFIG_SYSCTL
Tom Rixfc412a62023-05-02 14:07:13 -040087static const int nlm_port_min = 0, nlm_port_max = 65535;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static struct ctl_table_header * nlm_sysctl_table;
Chuck Lever90d5b182008-03-14 14:18:30 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Marc Eshel9a8db972007-07-17 04:04:35 -070091static unsigned long get_lockd_grace_period(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* Note: nlm_timeout should always be nonzero */
94 if (nlm_grace_period)
Marc Eshel9a8db972007-07-17 04:04:35 -070095 return roundup(nlm_grace_period, nlm_timeout) * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 else
Marc Eshel9a8db972007-07-17 04:04:35 -070097 return nlm_timeout * 5 * HZ;
98}
99
Stanislav Kinsbursky08d44a32012-07-25 16:56:50 +0400100static void grace_ender(struct work_struct *grace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Geliang Tangea444632016-01-01 22:06:29 +0800102 struct delayed_work *dwork = to_delayed_work(grace);
Stanislav Kinsbursky08d44a32012-07-25 16:56:50 +0400103 struct lockd_net *ln = container_of(dwork, struct lockd_net,
104 grace_period_end);
105
106 locks_end_grace(&ln->lockd_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +0400109static void set_grace_period(struct net *net)
J. Bruce Fieldsc8ab5f22008-03-18 19:00:19 -0400110{
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -0400111 unsigned long grace_period = get_lockd_grace_period();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +0400112 struct lockd_net *ln = net_generic(net, lockd_net_id);
J. Bruce Fieldsc8ab5f22008-03-18 19:00:19 -0400113
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +0400114 locks_start_grace(net, &ln->lockd_manager);
Stanislav Kinsbursky66547b02012-07-25 16:56:43 +0400115 cancel_delayed_work_sync(&ln->grace_period_end);
116 schedule_delayed_work(&ln->grace_period_end, grace_period);
J. Bruce Fieldsc8ab5f22008-03-18 19:00:19 -0400117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * This is the lockd kernel thread
121 */
Jeff Laytond751a7c2008-02-07 16:34:55 -0500122static int
123lockd(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Jeff Laytond751a7c2008-02-07 16:34:55 -0500125 struct svc_rqst *rqstp = vrqstp;
J. Bruce Fieldsefda7602017-03-28 21:25:08 -0400126 struct net *net = &init_net;
127 struct lockd_net *ln = net_generic(net, lockd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jeff Laytond751a7c2008-02-07 16:34:55 -0500129 /* try_to_freeze() is called from svc_recv() */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700130 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
135 * The main request loop. We don't terminate until the last
Jeff Laytond751a7c2008-02-07 16:34:55 -0500136 * NFS mount or NFS daemon has gone away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
Jeff Laytond751a7c2008-02-07 16:34:55 -0500138 while (!kthread_should_stop()) {
Jeff Laytonc72a4762008-10-20 11:51:58 -0400139 /* update sv_maxconn if it has changed */
140 rqstp->rq_server->sv_maxconn = nlm_max_connections;
141
NeilBrownc743b422023-07-18 16:38:08 +1000142 nlmsvc_retry_blocked();
143 svc_recv(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
Jeff Laytond751a7c2008-02-07 16:34:55 -0500145 if (nlmsvc_ops)
146 nlmsvc_invalidate_all();
147 nlm_shutdown_hosts();
J. Bruce Fieldsefda7602017-03-28 21:25:08 -0400148 cancel_delayed_work_sync(&ln->grace_period_end);
149 locks_end_grace(&ln->lockd_manager);
NeilBrown6a4e2522021-11-29 15:51:25 +1100150
151 dprintk("lockd_down: service stopped\n");
152
153 svc_exit_thread(rqstp);
Chuck Leverf49169c2022-02-16 12:31:09 -0500154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Chuck Levereb16e902009-03-18 20:47:59 -0400157static int create_lockd_listener(struct svc_serv *serv, const char *name,
Stanislav Kinsburskyc228fa22012-01-31 15:07:48 +0400158 struct net *net, const int family,
Trond Myklebust40373b12019-04-09 12:13:39 -0400159 const unsigned short port,
160 const struct cred *cred)
Chuck Leverd3fe5ea2008-12-31 16:06:04 -0500161{
162 struct svc_xprt *xprt;
163
Stanislav Kinsburskyc228fa22012-01-31 15:07:48 +0400164 xprt = svc_find_xprt(serv, name, net, family, 0);
Chuck Leverd3fe5ea2008-12-31 16:06:04 -0500165 if (xprt == NULL)
Chuck Lever352ad312022-01-26 11:42:08 -0500166 return svc_xprt_create(serv, name, net, family, port,
167 SVC_SOCK_DEFAULTS, cred);
Chuck Leverd3fe5ea2008-12-31 16:06:04 -0500168 svc_xprt_put(xprt);
169 return 0;
170}
171
Stanislav Kinsburskyc228fa22012-01-31 15:07:48 +0400172static int create_lockd_family(struct svc_serv *serv, struct net *net,
Trond Myklebust40373b12019-04-09 12:13:39 -0400173 const int family, const struct cred *cred)
Chuck Levereb16e902009-03-18 20:47:59 -0400174{
175 int err;
176
Trond Myklebust40373b12019-04-09 12:13:39 -0400177 err = create_lockd_listener(serv, "udp", net, family, nlm_udpport,
178 cred);
Chuck Levereb16e902009-03-18 20:47:59 -0400179 if (err < 0)
180 return err;
181
Trond Myklebust40373b12019-04-09 12:13:39 -0400182 return create_lockd_listener(serv, "tcp", net, family, nlm_tcpport,
183 cred);
Chuck Levereb16e902009-03-18 20:47:59 -0400184}
185
Chuck Lever482fb942007-02-12 00:53:29 -0800186/*
Chuck Lever8c3916f2008-10-03 17:15:23 -0400187 * Ensure there are active UDP and TCP listeners for lockd.
188 *
189 * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
190 * local services (such as rpc.statd) still require UDP, and
191 * some NFS servers do not yet support NLM over TCP.
192 *
193 * Returns zero if all listeners are available; otherwise a
194 * negative errno value is returned.
Chuck Lever482fb942007-02-12 00:53:29 -0800195 */
Trond Myklebust40373b12019-04-09 12:13:39 -0400196static int make_socks(struct svc_serv *serv, struct net *net,
197 const struct cred *cred)
NeilBrown24e36662006-10-02 02:17:45 -0700198{
Chuck Lever482fb942007-02-12 00:53:29 -0800199 static int warned;
Chuck Lever0dba7c22008-12-31 16:06:11 -0500200 int err;
Chuck Lever482fb942007-02-12 00:53:29 -0800201
Trond Myklebust40373b12019-04-09 12:13:39 -0400202 err = create_lockd_family(serv, net, PF_INET, cred);
Chuck Lever0dba7c22008-12-31 16:06:11 -0500203 if (err < 0)
204 goto out_err;
205
Trond Myklebust40373b12019-04-09 12:13:39 -0400206 err = create_lockd_family(serv, net, PF_INET6, cred);
Chuck Levereb16e902009-03-18 20:47:59 -0400207 if (err < 0 && err != -EAFNOSUPPORT)
Chuck Lever0dba7c22008-12-31 16:06:11 -0500208 goto out_err;
209
210 warned = 0;
211 return 0;
212
213out_err:
214 if (warned++ == 0)
NeilBrown7dcf91e2006-10-02 02:17:52 -0700215 printk(KERN_WARNING
Chuck Lever0dba7c22008-12-31 16:06:11 -0500216 "lockd_up: makesock failed, error=%d\n", err);
Chuck Leverc7d7ec82022-01-26 11:30:55 -0500217 svc_xprt_destroy_all(serv, net);
Chuck Lever87cdd862022-01-25 13:49:29 -0500218 svc_rpcb_cleanup(serv, net);
NeilBrown24e36662006-10-02 02:17:45 -0700219 return err;
220}
221
Trond Myklebust40373b12019-04-09 12:13:39 -0400222static int lockd_up_net(struct svc_serv *serv, struct net *net,
223 const struct cred *cred)
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400224{
225 struct lockd_net *ln = net_generic(net, lockd_net_id);
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400226 int error;
227
Stanislav Kinsbursky786185b2012-05-04 12:49:41 +0400228 if (ln->nlmsvc_users++)
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400229 return 0;
230
Stanislav Kinsburskydbf9b5d72012-04-25 18:22:47 +0400231 error = svc_bind(serv, net);
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400232 if (error)
Stanislav Kinsburskydbf9b5d72012-04-25 18:22:47 +0400233 goto err_bind;
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400234
Trond Myklebust40373b12019-04-09 12:13:39 -0400235 error = make_socks(serv, net, cred);
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400236 if (error < 0)
J. Bruce Fields7c177052014-08-29 16:25:50 -0400237 goto err_bind;
Stanislav Kinsbursky5630f7f2012-07-25 16:57:29 +0400238 set_grace_period(net);
Vasily Averine919b072017-11-08 08:55:55 +0300239 dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum);
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400240 return 0;
241
Stanislav Kinsburskydbf9b5d72012-04-25 18:22:47 +0400242err_bind:
Stanislav Kinsbursky786185b2012-05-04 12:49:41 +0400243 ln->nlmsvc_users--;
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400244 return error;
245}
246
Stanislav Kinsbursky4db77692012-04-25 18:22:40 +0400247static void lockd_down_net(struct svc_serv *serv, struct net *net)
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400248{
249 struct lockd_net *ln = net_generic(net, lockd_net_id);
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400250
251 if (ln->nlmsvc_users) {
Stanislav Kinsbursky3b647392012-01-31 15:08:29 +0400252 if (--ln->nlmsvc_users == 0) {
253 nlm_shutdown_hosts_net(net);
Vasily Averin3a2b19d2017-11-02 13:03:42 +0300254 cancel_delayed_work_sync(&ln->grace_period_end);
255 locks_end_grace(&ln->lockd_manager);
Chuck Leverc7d7ec82022-01-26 11:30:55 -0500256 svc_xprt_destroy_all(serv, net);
Chuck Lever87cdd862022-01-25 13:49:29 -0500257 svc_rpcb_cleanup(serv, net);
Stanislav Kinsbursky3b647392012-01-31 15:08:29 +0400258 }
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400259 } else {
NeilBrown6b044fb2021-11-29 15:51:25 +1100260 pr_err("%s: no users! net=%x\n",
261 __func__, net->ns.inum);
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400262 BUG();
263 }
264}
265
Scott Mayhew0751ddf2015-12-11 16:46:00 -0500266static int lockd_inetaddr_event(struct notifier_block *this,
267 unsigned long event, void *ptr)
268{
269 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
270 struct sockaddr_in sin;
271
NeilBrown5a8a7ff2021-11-29 15:51:25 +1100272 if (event != NETDEV_DOWN)
Scott Mayhew0751ddf2015-12-11 16:46:00 -0500273 goto out;
274
NeilBrown2840fe82021-11-29 15:51:25 +1100275 if (nlmsvc_serv) {
Scott Mayhew0751ddf2015-12-11 16:46:00 -0500276 dprintk("lockd_inetaddr_event: removed %pI4\n",
277 &ifa->ifa_local);
278 sin.sin_family = AF_INET;
279 sin.sin_addr.s_addr = ifa->ifa_local;
NeilBrown2840fe82021-11-29 15:51:25 +1100280 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin);
Scott Mayhew0751ddf2015-12-11 16:46:00 -0500281 }
282
283out:
284 return NOTIFY_DONE;
285}
286
287static struct notifier_block lockd_inetaddr_notifier = {
288 .notifier_call = lockd_inetaddr_event,
289};
290
291#if IS_ENABLED(CONFIG_IPV6)
292static int lockd_inet6addr_event(struct notifier_block *this,
293 unsigned long event, void *ptr)
294{
295 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
296 struct sockaddr_in6 sin6;
297
NeilBrown5a8a7ff2021-11-29 15:51:25 +1100298 if (event != NETDEV_DOWN)
Scott Mayhew0751ddf2015-12-11 16:46:00 -0500299 goto out;
300
NeilBrown2840fe82021-11-29 15:51:25 +1100301 if (nlmsvc_serv) {
Scott Mayhew0751ddf2015-12-11 16:46:00 -0500302 dprintk("lockd_inet6addr_event: removed %pI6\n", &ifa->addr);
303 sin6.sin6_family = AF_INET6;
304 sin6.sin6_addr = ifa->addr;
Scott Mayhewc01410f2017-01-05 16:34:50 -0500305 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
306 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
NeilBrown2840fe82021-11-29 15:51:25 +1100307 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin6);
Scott Mayhew0751ddf2015-12-11 16:46:00 -0500308 }
309
310out:
311 return NOTIFY_DONE;
312}
313
314static struct notifier_block lockd_inet6addr_notifier = {
315 .notifier_call = lockd_inet6addr_event,
316};
317#endif
318
NeilBrownecd3ad62021-11-29 15:51:25 +1100319static int lockd_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Jeff Laytond751a7c2008-02-07 16:34:55 -0500321 struct svc_serv *serv;
NeilBrownb73a2972021-11-29 15:51:25 +1100322 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
NeilBrown2840fe82021-11-29 15:51:25 +1100324 if (nlmsvc_serv) {
NeilBrownecd3ad62021-11-29 15:51:25 +1100325 nlmsvc_users++;
NeilBrown2840fe82021-11-29 15:51:25 +1100326 return 0;
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /*
330 * Sanity check: if there's no pid,
331 * we should be the first user ...
332 */
NeilBrown4a3ae422006-10-02 02:17:53 -0700333 if (nlmsvc_users)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 printk(KERN_WARNING
335 "lockd_up: no pid, %d users??\n", nlmsvc_users);
336
Trond Myklebust06bed7d2015-01-02 15:05:25 -0500337 if (!nlm_timeout)
338 nlm_timeout = LOCKD_DFLT_TIMEO;
339 nlmsvc_timeout = nlm_timeout * HZ;
340
Chuck Lever37902c62022-02-16 12:16:27 -0500341 serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, lockd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!serv) {
343 printk(KERN_WARNING "lockd_up: create service failed\n");
NeilBrown2840fe82021-11-29 15:51:25 +1100344 return -ENOMEM;
Stanislav Kinsbursky24452232012-04-25 18:22:54 +0400345 }
NeilBrownb73a2972021-11-29 15:51:25 +1100346
NeilBrown6b044fb2021-11-29 15:51:25 +1100347 serv->sv_maxconn = nlm_max_connections;
348 error = svc_set_num_threads(serv, NULL, 1);
NeilBrownb73a2972021-11-29 15:51:25 +1100349 /* The thread now holds the only reference */
350 svc_put(serv);
351 if (error < 0)
352 return error;
353
NeilBrown2840fe82021-11-29 15:51:25 +1100354 nlmsvc_serv = serv;
Scott Mayhew0751ddf2015-12-11 16:46:00 -0500355 register_inetaddr_notifier(&lockd_inetaddr_notifier);
356#if IS_ENABLED(CONFIG_IPV6)
357 register_inet6addr_notifier(&lockd_inet6addr_notifier);
358#endif
Stanislav Kinsbursky8dbf28e2012-04-25 18:23:16 +0400359 dprintk("lockd_up: service created\n");
NeilBrownecd3ad62021-11-29 15:51:25 +1100360 nlmsvc_users++;
NeilBrown2840fe82021-11-29 15:51:25 +1100361 return 0;
Stanislav Kinsbursky24452232012-04-25 18:22:54 +0400362}
363
NeilBrown865b6742021-11-29 15:51:25 +1100364static void lockd_put(void)
365{
366 if (WARN(nlmsvc_users <= 0, "lockd_down: no users!\n"))
367 return;
368 if (--nlmsvc_users)
369 return;
370
371 unregister_inetaddr_notifier(&lockd_inetaddr_notifier);
372#if IS_ENABLED(CONFIG_IPV6)
373 unregister_inet6addr_notifier(&lockd_inet6addr_notifier);
374#endif
375
NeilBrown6b044fb2021-11-29 15:51:25 +1100376 svc_set_num_threads(nlmsvc_serv, NULL, 0);
NeilBrownc743b422023-07-18 16:38:08 +1000377 timer_delete_sync(&nlmsvc_retry);
NeilBrown865b6742021-11-29 15:51:25 +1100378 nlmsvc_serv = NULL;
379 dprintk("lockd_down: service destroyed\n");
380}
381
Stanislav Kinsbursky24452232012-04-25 18:22:54 +0400382/*
383 * Bring up the lockd process if it's not already up.
384 */
Trond Myklebust40373b12019-04-09 12:13:39 -0400385int lockd_up(struct net *net, const struct cred *cred)
Stanislav Kinsbursky24452232012-04-25 18:22:54 +0400386{
Stanislav Kinsbursky7d13ec72012-04-25 18:23:02 +0400387 int error;
Stanislav Kinsbursky24452232012-04-25 18:22:54 +0400388
389 mutex_lock(&nlmsvc_mutex);
390
NeilBrownecd3ad62021-11-29 15:51:25 +1100391 error = lockd_get();
NeilBrown2840fe82021-11-29 15:51:25 +1100392 if (error)
NeilBrown865b6742021-11-29 15:51:25 +1100393 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
NeilBrownb73a2972021-11-29 15:51:25 +1100395 error = lockd_up_net(nlmsvc_serv, net, cred);
Vasily Averindc3033e2017-10-20 17:33:18 +0300396 if (error < 0) {
NeilBrown865b6742021-11-29 15:51:25 +1100397 lockd_put();
398 goto err;
Vasily Averindc3033e2017-10-20 17:33:18 +0300399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
NeilBrown865b6742021-11-29 15:51:25 +1100401err:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800402 mutex_unlock(&nlmsvc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return error;
404}
Trond Myklebust2de59872008-12-23 15:21:33 -0500405EXPORT_SYMBOL_GPL(lockd_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407/*
408 * Decrement the user count and bring down lockd if we're the last.
409 */
410void
Stanislav Kinsburskye3f70ea2012-03-29 18:54:33 +0400411lockd_down(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800413 mutex_lock(&nlmsvc_mutex);
NeilBrown2840fe82021-11-29 15:51:25 +1100414 lockd_down_net(nlmsvc_serv, net);
NeilBrown865b6742021-11-29 15:51:25 +1100415 lockd_put();
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800416 mutex_unlock(&nlmsvc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Trond Myklebust2de59872008-12-23 15:21:33 -0500418EXPORT_SYMBOL_GPL(lockd_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Chuck Lever90d5b182008-03-14 14:18:30 -0400420#ifdef CONFIG_SYSCTL
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/*
423 * Sysctl parameters (same as module parameters, different interface).
424 */
425
Joe Perches7ac9fe52014-06-06 14:38:02 -0700426static struct ctl_table nlm_sysctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 .procname = "nlm_grace_period",
429 .data = &nlm_grace_period,
Steve Dickson7ee91ec2005-07-13 01:10:47 -0700430 .maxlen = sizeof(unsigned long),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800432 .proc_handler = proc_doulongvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 .extra1 = (unsigned long *) &nlm_grace_period_min,
434 .extra2 = (unsigned long *) &nlm_grace_period_max,
435 },
436 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 .procname = "nlm_timeout",
438 .data = &nlm_timeout,
Steve Dickson7ee91ec2005-07-13 01:10:47 -0700439 .maxlen = sizeof(unsigned long),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800441 .proc_handler = proc_doulongvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 .extra1 = (unsigned long *) &nlm_timeout_min,
443 .extra2 = (unsigned long *) &nlm_timeout_max,
444 },
445 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 .procname = "nlm_udpport",
447 .data = &nlm_udpport,
448 .maxlen = sizeof(int),
449 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800450 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 .extra1 = (int *) &nlm_port_min,
452 .extra2 = (int *) &nlm_port_max,
453 },
454 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .procname = "nlm_tcpport",
456 .data = &nlm_tcpport,
457 .maxlen = sizeof(int),
458 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800459 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 .extra1 = (int *) &nlm_port_min,
461 .extra2 = (int *) &nlm_port_max,
462 },
Olaf Kirchabd1f502006-10-04 02:16:01 -0700463 {
Olaf Kirchabd1f502006-10-04 02:16:01 -0700464 .procname = "nsm_use_hostnames",
465 .data = &nsm_use_hostnames,
Ondrej Mosnacekf1aa2eb2023-02-10 15:58:23 +0100466 .maxlen = sizeof(bool),
Olaf Kirchabd1f502006-10-04 02:16:01 -0700467 .mode = 0644,
Jia Hed02a3a22021-08-03 12:59:37 +0200468 .proc_handler = proc_dobool,
Olaf Kirchabd1f502006-10-04 02:16:01 -0700469 },
Olaf Kirch460f5ca2006-10-04 02:16:03 -0700470 {
Olaf Kirch460f5ca2006-10-04 02:16:03 -0700471 .procname = "nsm_local_state",
472 .data = &nsm_local_state,
473 .maxlen = sizeof(int),
474 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800475 .proc_handler = proc_dointvec,
Olaf Kirch460f5ca2006-10-04 02:16:03 -0700476 },
Eric W. Biedermanab092032009-11-05 14:25:10 -0800477 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478};
479
Chuck Lever90d5b182008-03-14 14:18:30 -0400480#endif /* CONFIG_SYSCTL */
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100483 * Module (and sysfs) parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 */
485
486#define param_set_min_max(name, type, which_strtol, min, max) \
Kees Cooke4dca7b2017-10-17 19:04:42 -0700487static int param_set_##name(const char *val, const struct kernel_param *kp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{ \
489 char *endp; \
490 __typeof__(type) num = which_strtol(val, &endp, 0); \
491 if (endp == val || *endp || num < (min) || num > (max)) \
492 return -EINVAL; \
NeilBrownde5b8e82012-02-07 15:35:42 +1100493 *((type *) kp->arg) = num; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0; \
495}
496
497static inline int is_callback(u32 proc)
498{
499 return proc == NLMPROC_GRANTED
500 || proc == NLMPROC_GRANTED_MSG
501 || proc == NLMPROC_TEST_RES
502 || proc == NLMPROC_LOCK_RES
503 || proc == NLMPROC_CANCEL_RES
504 || proc == NLMPROC_UNLOCK_RES
505 || proc == NLMPROC_NSM_NOTIFY;
506}
507
508
Chuck Lever78c542f2023-07-29 20:58:54 -0400509static enum svc_auth_status lockd_authenticate(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 rqstp->rq_client = NULL;
512 switch (rqstp->rq_authop->flavour) {
513 case RPC_AUTH_NULL:
514 case RPC_AUTH_UNIX:
Chuck Lever5c2465d2021-07-15 15:52:12 -0400515 rqstp->rq_auth_stat = rpc_auth_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (rqstp->rq_proc == 0)
517 return SVC_OK;
518 if (is_callback(rqstp->rq_proc)) {
519 /* Leave it to individual procedures to
520 * call nlmsvc_lookup_host(rqstp)
521 */
522 return SVC_OK;
523 }
524 return svc_set_client(rqstp);
525 }
Chuck Lever5c2465d2021-07-15 15:52:12 -0400526 rqstp->rq_auth_stat = rpc_autherr_badcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return SVC_DENIED;
528}
529
530
531param_set_min_max(port, int, simple_strtol, 0, 65535)
532param_set_min_max(grace_period, unsigned long, simple_strtoul,
533 nlm_grace_period_min, nlm_grace_period_max)
534param_set_min_max(timeout, unsigned long, simple_strtoul,
535 nlm_timeout_min, nlm_timeout_max)
536
537MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
538MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
539MODULE_LICENSE("GPL");
540
541module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
542 &nlm_grace_period, 0644);
543module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
544 &nlm_timeout, 0644);
545module_param_call(nlm_udpport, param_set_port, param_get_int,
546 &nlm_udpport, 0644);
547module_param_call(nlm_tcpport, param_set_port, param_get_int,
548 &nlm_tcpport, 0644);
Olaf Kirchabd1f502006-10-04 02:16:01 -0700549module_param(nsm_use_hostnames, bool, 0644);
Jeff Laytonc72a4762008-10-20 11:51:58 -0400550module_param(nlm_max_connections, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400552static int lockd_init_net(struct net *net)
553{
Stanislav Kinsbursky66547b02012-07-25 16:56:43 +0400554 struct lockd_net *ln = net_generic(net, lockd_net_id);
555
556 INIT_DELAYED_WORK(&ln->grace_period_end, grace_ender);
Jeff Laytonf7790022014-09-12 16:40:20 -0400557 INIT_LIST_HEAD(&ln->lockd_manager.list);
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400558 ln->lockd_manager.block_opens = false;
Andrey Ryabinin0ad95472015-09-23 15:49:29 +0300559 INIT_LIST_HEAD(&ln->nsm_handles);
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400560 return 0;
561}
562
563static void lockd_exit_net(struct net *net)
564{
Vasily Averina3152f12017-11-06 16:23:24 +0300565 struct lockd_net *ln = net_generic(net, lockd_net_id);
566
567 WARN_ONCE(!list_empty(&ln->lockd_manager.list),
568 "net %x %s: lockd_manager.list is not empty\n",
569 net->ns.inum, __func__);
570 WARN_ONCE(!list_empty(&ln->nsm_handles),
571 "net %x %s: nsm_handles list is not empty\n",
572 net->ns.inum, __func__);
573 WARN_ONCE(delayed_work_pending(&ln->grace_period_end),
574 "net %x %s: grace_period_end was not cancelled\n",
575 net->ns.inum, __func__);
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400576}
577
578static struct pernet_operations lockd_net_ops = {
579 .init = lockd_init_net,
580 .exit = lockd_exit_net,
581 .id = &lockd_net_id,
582 .size = sizeof(struct lockd_net),
583};
584
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * Initialising and terminating the module.
588 */
589
590static int __init init_nlm(void)
591{
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400592 int err;
593
Chuck Lever90d5b182008-03-14 14:18:30 -0400594#ifdef CONFIG_SYSCTL
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400595 err = -ENOMEM;
Luis Chamberlain37b768c2023-03-10 00:37:19 -0800596 nlm_sysctl_table = register_sysctl("fs/nfs", nlm_sysctls);
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400597 if (nlm_sysctl_table == NULL)
598 goto err_sysctl;
Chuck Lever90d5b182008-03-14 14:18:30 -0400599#endif
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400600 err = register_pernet_subsys(&lockd_net_ops);
601 if (err)
602 goto err_pernet;
Jeff Laytond68e3c42014-09-12 16:40:20 -0400603
604 err = lockd_create_procfs();
605 if (err)
606 goto err_procfs;
607
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400608 return 0;
609
Jeff Laytond68e3c42014-09-12 16:40:20 -0400610err_procfs:
611 unregister_pernet_subsys(&lockd_net_ops);
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400612err_pernet:
613#ifdef CONFIG_SYSCTL
614 unregister_sysctl_table(nlm_sysctl_table);
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400615err_sysctl:
Kees Cook12dd7ec2014-05-01 14:15:02 -0700616#endif
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620static void __exit exit_nlm(void)
621{
622 /* FIXME: delete all NLM clients */
623 nlm_shutdown_hosts();
Jeff Laytond68e3c42014-09-12 16:40:20 -0400624 lockd_remove_procfs();
Stanislav Kinsburskya9c5d732012-01-31 15:07:57 +0400625 unregister_pernet_subsys(&lockd_net_ops);
Chuck Lever90d5b182008-03-14 14:18:30 -0400626#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unregister_sysctl_table(nlm_sysctl_table);
Chuck Lever90d5b182008-03-14 14:18:30 -0400628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631module_init(init_nlm);
632module_exit(exit_nlm);
633
Chuck Levera9ad1a82021-06-03 16:50:46 -0400634/**
635 * nlmsvc_dispatch - Process an NLM Request
636 * @rqstp: incoming request
Chuck Levera9ad1a82021-06-03 16:50:46 -0400637 *
638 * Return values:
639 * %0: Processing complete; do not send a Reply
640 * %1: Processing complete; send Reply in rqstp->rq_res
641 */
Chuck Levercee4db12023-01-08 11:30:59 -0500642static int nlmsvc_dispatch(struct svc_rqst *rqstp)
Chuck Levera9ad1a82021-06-03 16:50:46 -0400643{
644 const struct svc_procedure *procp = rqstp->rq_procinfo;
Chuck Levercee4db12023-01-08 11:30:59 -0500645 __be32 *statp = rqstp->rq_accept_statp;
Chuck Levera9ad1a82021-06-03 16:50:46 -0400646
Chuck Lever16c66362021-10-12 11:57:22 -0400647 if (!procp->pc_decode(rqstp, &rqstp->rq_arg_stream))
Chuck Levera9ad1a82021-06-03 16:50:46 -0400648 goto out_decode_err;
649
650 *statp = procp->pc_func(rqstp);
651 if (*statp == rpc_drop_reply)
652 return 0;
653 if (*statp != rpc_success)
654 return 1;
655
Chuck Leverfda49442021-10-13 10:41:06 -0400656 if (!procp->pc_encode(rqstp, &rqstp->rq_res_stream))
Chuck Levera9ad1a82021-06-03 16:50:46 -0400657 goto out_encode_err;
658
659 return 1;
660
661out_decode_err:
662 *statp = rpc_garbage_args;
663 return 1;
664
665out_encode_err:
666 *statp = rpc_system_err;
667 return 1;
668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/*
671 * Define NLM program and procedures
672 */
Chuck Lever65ba3d22023-01-10 10:31:54 -0500673static DEFINE_PER_CPU_ALIGNED(unsigned long, nlmsvc_version1_count[17]);
Christoph Hellwige9679182017-05-12 16:21:37 +0200674static const struct svc_version nlmsvc_version1 = {
675 .vs_vers = 1,
676 .vs_nproc = 17,
677 .vs_proc = nlmsvc_procedures,
678 .vs_count = nlmsvc_version1_count,
Chuck Levera9ad1a82021-06-03 16:50:46 -0400679 .vs_dispatch = nlmsvc_dispatch,
Christoph Hellwige9679182017-05-12 16:21:37 +0200680 .vs_xdrsize = NLMSVC_XDRSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681};
Chuck Lever65ba3d22023-01-10 10:31:54 -0500682
683static DEFINE_PER_CPU_ALIGNED(unsigned long,
684 nlmsvc_version3_count[ARRAY_SIZE(nlmsvc_procedures)]);
Christoph Hellwige9679182017-05-12 16:21:37 +0200685static const struct svc_version nlmsvc_version3 = {
686 .vs_vers = 3,
Chuck Lever65ba3d22023-01-10 10:31:54 -0500687 .vs_nproc = ARRAY_SIZE(nlmsvc_procedures),
Christoph Hellwige9679182017-05-12 16:21:37 +0200688 .vs_proc = nlmsvc_procedures,
689 .vs_count = nlmsvc_version3_count,
Chuck Levera9ad1a82021-06-03 16:50:46 -0400690 .vs_dispatch = nlmsvc_dispatch,
Christoph Hellwige9679182017-05-12 16:21:37 +0200691 .vs_xdrsize = NLMSVC_XDRSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692};
Chuck Lever65ba3d22023-01-10 10:31:54 -0500693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#ifdef CONFIG_LOCKD_V4
Chuck Lever65ba3d22023-01-10 10:31:54 -0500695static DEFINE_PER_CPU_ALIGNED(unsigned long,
696 nlmsvc_version4_count[ARRAY_SIZE(nlmsvc_procedures4)]);
Christoph Hellwige9679182017-05-12 16:21:37 +0200697static const struct svc_version nlmsvc_version4 = {
698 .vs_vers = 4,
Chuck Lever65ba3d22023-01-10 10:31:54 -0500699 .vs_nproc = ARRAY_SIZE(nlmsvc_procedures4),
Christoph Hellwige9679182017-05-12 16:21:37 +0200700 .vs_proc = nlmsvc_procedures4,
701 .vs_count = nlmsvc_version4_count,
Chuck Levera9ad1a82021-06-03 16:50:46 -0400702 .vs_dispatch = nlmsvc_dispatch,
Christoph Hellwige9679182017-05-12 16:21:37 +0200703 .vs_xdrsize = NLMSVC_XDRSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704};
705#endif
Chuck Lever65ba3d22023-01-10 10:31:54 -0500706
Christoph Hellwige9679182017-05-12 16:21:37 +0200707static const struct svc_version *nlmsvc_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 [1] = &nlmsvc_version1,
709 [3] = &nlmsvc_version3,
710#ifdef CONFIG_LOCKD_V4
711 [4] = &nlmsvc_version4,
712#endif
713};
714
715static struct svc_stat nlmsvc_stats;
716
Tobias Klausere8c96f82006-03-24 03:15:34 -0800717#define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static struct svc_program nlmsvc_program = {
719 .pg_prog = NLM_PROGRAM, /* program number */
720 .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
721 .pg_vers = nlmsvc_version, /* version table */
722 .pg_name = "lockd", /* service name */
723 .pg_class = "nfsd", /* share authentication with nfsd */
724 .pg_stats = &nlmsvc_stats, /* stats table */
Trond Myklebust8e5b6772019-04-09 11:46:15 -0400725 .pg_authenticate = &lockd_authenticate, /* export authentication */
726 .pg_init_request = svc_generic_init_request,
Trond Myklebust642ee6b2019-04-09 11:46:17 -0400727 .pg_rpcbind_set = svc_generic_rpcbind_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728};