Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
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
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/sysctl.h>
19#include <linux/moduleparam.h>
20
21#include <linux/sched/signal.h>
22#include <linux/errno.h>
23#include <linux/in.h>
24#include <linux/uio.h>
25#include <linux/smp.h>
26#include <linux/mutex.h>
27#include <linux/freezer.h>
28#include <linux/inetdevice.h>
29
30#include <linux/sunrpc/types.h>
31#include <linux/sunrpc/stats.h>
32#include <linux/sunrpc/clnt.h>
33#include <linux/sunrpc/svc.h>
34#include <linux/sunrpc/svcsock.h>
35#include <linux/sunrpc/svc_xprt.h>
36#include <net/ip.h>
37#include <net/addrconf.h>
38#include <net/ipv6.h>
39#include <linux/lockd/lockd.h>
40#include <linux/nfs.h>
41
42#include "netns.h"
43#include "procfs.h"
44
45#define NLMDBG_FACILITY NLMDBG_SVC
46#define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
47
48static struct svc_program nlmsvc_program;
49
50const struct nlmsvc_binding *nlmsvc_ops;
51EXPORT_SYMBOL_GPL(nlmsvc_ops);
52
53static DEFINE_MUTEX(nlmsvc_mutex);
54static unsigned int nlmsvc_users;
55static struct svc_serv *nlmsvc_serv;
56
57static void nlmsvc_request_retry(struct timer_list *tl)
58{
59 svc_wake_up(nlmsvc_serv);
60}
61DEFINE_TIMER(nlmsvc_retry, nlmsvc_request_retry);
62
63unsigned int lockd_net_id;
64
65/*
66 * These can be set at insmod time (useful for NFS as root filesystem),
67 * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003
68 */
69static unsigned long nlm_grace_period;
70unsigned long nlm_timeout = LOCKD_DFLT_TIMEO;
71static int nlm_udpport, nlm_tcpport;
72
73/*
74 * Constants needed for the sysctl interface.
75 */
76static const unsigned long nlm_grace_period_min = 0;
77static const unsigned long nlm_grace_period_max = 240;
78static const unsigned long nlm_timeout_min = 3;
79static const unsigned long nlm_timeout_max = 20;
80
81#ifdef CONFIG_SYSCTL
82static const int nlm_port_min = 0, nlm_port_max = 65535;
83static struct ctl_table_header * nlm_sysctl_table;
84#endif
85
86static unsigned long get_lockd_grace_period(void)
87{
88 /* Note: nlm_timeout should always be nonzero */
89 if (nlm_grace_period)
90 return roundup(nlm_grace_period, nlm_timeout) * HZ;
91 else
92 return nlm_timeout * 5 * HZ;
93}
94
95static void grace_ender(struct work_struct *grace)
96{
97 struct delayed_work *dwork = to_delayed_work(grace);
98 struct lockd_net *ln = container_of(dwork, struct lockd_net,
99 grace_period_end);
100
101 locks_end_grace(&ln->lockd_manager);
102}
103
104static void set_grace_period(struct net *net)
105{
106 unsigned long grace_period = get_lockd_grace_period();
107 struct lockd_net *ln = net_generic(net, lockd_net_id);
108
109 locks_start_grace(net, &ln->lockd_manager);
110 cancel_delayed_work_sync(&ln->grace_period_end);
111 schedule_delayed_work(&ln->grace_period_end, grace_period);
112}
113
114/*
115 * This is the lockd kernel thread
116 */
117static int
118lockd(void *vrqstp)
119{
120 struct svc_rqst *rqstp = vrqstp;
121 struct net *net = &init_net;
122 struct lockd_net *ln = net_generic(net, lockd_net_id);
123
124 svc_thread_init_status(rqstp, 0);
125
126 /* try_to_freeze() is called from svc_recv() */
127 set_freezable();
128
129 dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
130
131 /*
132 * The main request loop. We don't terminate until the last
133 * NFS mount or NFS daemon has gone away.
134 */
135 while (!svc_thread_should_stop(rqstp)) {
136 nlmsvc_retry_blocked(rqstp);
137 svc_recv(rqstp);
138 }
139 if (nlmsvc_ops)
140 nlmsvc_invalidate_all();
141 nlm_shutdown_hosts();
142 cancel_delayed_work_sync(&ln->grace_period_end);
143 locks_end_grace(&ln->lockd_manager);
144
145 dprintk("lockd_down: service stopped\n");
146
147 svc_exit_thread(rqstp);
148 return 0;
149}
150
151static int create_lockd_listener(struct svc_serv *serv, const char *name,
152 struct net *net, const int family,
153 const unsigned short port,
154 const struct cred *cred)
155{
156 struct svc_xprt *xprt;
157
158 xprt = svc_find_xprt(serv, name, net, family, 0);
159 if (xprt == NULL)
160 return svc_xprt_create(serv, name, net, family, port,
161 SVC_SOCK_DEFAULTS, cred);
162 svc_xprt_put(xprt);
163 return 0;
164}
165
166static int create_lockd_family(struct svc_serv *serv, struct net *net,
167 const int family, const struct cred *cred)
168{
169 int err;
170
171 err = create_lockd_listener(serv, "udp", net, family, nlm_udpport,
172 cred);
173 if (err < 0)
174 return err;
175
176 return create_lockd_listener(serv, "tcp", net, family, nlm_tcpport,
177 cred);
178}
179
180/*
181 * Ensure there are active UDP and TCP listeners for lockd.
182 *
183 * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
184 * local services (such as rpc.statd) still require UDP, and
185 * some NFS servers do not yet support NLM over TCP.
186 *
187 * Returns zero if all listeners are available; otherwise a
188 * negative errno value is returned.
189 */
190static int make_socks(struct svc_serv *serv, struct net *net,
191 const struct cred *cred)
192{
193 static int warned;
194 int err;
195
196 err = create_lockd_family(serv, net, PF_INET, cred);
197 if (err < 0)
198 goto out_err;
199
200 err = create_lockd_family(serv, net, PF_INET6, cred);
201 if (err < 0 && err != -EAFNOSUPPORT)
202 goto out_err;
203
204 warned = 0;
205 return 0;
206
207out_err:
208 if (warned++ == 0)
209 printk(KERN_WARNING
210 "lockd_up: makesock failed, error=%d\n", err);
211 svc_xprt_destroy_all(serv, net);
212 svc_rpcb_cleanup(serv, net);
213 return err;
214}
215
216static int lockd_up_net(struct svc_serv *serv, struct net *net,
217 const struct cred *cred)
218{
219 struct lockd_net *ln = net_generic(net, lockd_net_id);
220 int error;
221
222 if (ln->nlmsvc_users++)
223 return 0;
224
225 error = svc_bind(serv, net);
226 if (error)
227 goto err_bind;
228
229 error = make_socks(serv, net, cred);
230 if (error < 0)
231 goto err_bind;
232 set_grace_period(net);
233 dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum);
234 return 0;
235
236err_bind:
237 ln->nlmsvc_users--;
238 return error;
239}
240
241static void lockd_down_net(struct svc_serv *serv, struct net *net)
242{
243 struct lockd_net *ln = net_generic(net, lockd_net_id);
244
245 if (ln->nlmsvc_users) {
246 if (--ln->nlmsvc_users == 0) {
247 nlm_shutdown_hosts_net(net);
248 cancel_delayed_work_sync(&ln->grace_period_end);
249 locks_end_grace(&ln->lockd_manager);
250 svc_xprt_destroy_all(serv, net);
251 svc_rpcb_cleanup(serv, net);
252 }
253 } else {
254 pr_err("%s: no users! net=%x\n",
255 __func__, net->ns.inum);
256 BUG();
257 }
258}
259
260static int lockd_inetaddr_event(struct notifier_block *this,
261 unsigned long event, void *ptr)
262{
263 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
264 struct sockaddr_in sin;
265
266 if (event != NETDEV_DOWN)
267 goto out;
268
269 if (nlmsvc_serv) {
270 dprintk("lockd_inetaddr_event: removed %pI4\n",
271 &ifa->ifa_local);
272 sin.sin_family = AF_INET;
273 sin.sin_addr.s_addr = ifa->ifa_local;
274 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin);
275 }
276
277out:
278 return NOTIFY_DONE;
279}
280
281static struct notifier_block lockd_inetaddr_notifier = {
282 .notifier_call = lockd_inetaddr_event,
283};
284
285#if IS_ENABLED(CONFIG_IPV6)
286static int lockd_inet6addr_event(struct notifier_block *this,
287 unsigned long event, void *ptr)
288{
289 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
290 struct sockaddr_in6 sin6;
291
292 if (event != NETDEV_DOWN)
293 goto out;
294
295 if (nlmsvc_serv) {
296 dprintk("lockd_inet6addr_event: removed %pI6\n", &ifa->addr);
297 sin6.sin6_family = AF_INET6;
298 sin6.sin6_addr = ifa->addr;
299 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
300 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
301 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin6);
302 }
303
304out:
305 return NOTIFY_DONE;
306}
307
308static struct notifier_block lockd_inet6addr_notifier = {
309 .notifier_call = lockd_inet6addr_event,
310};
311#endif
312
313static int lockd_get(void)
314{
315 struct svc_serv *serv;
316 int error;
317
318 if (nlmsvc_serv) {
319 nlmsvc_users++;
320 return 0;
321 }
322
323 /*
324 * Sanity check: if there's no pid,
325 * we should be the first user ...
326 */
327 if (nlmsvc_users)
328 printk(KERN_WARNING
329 "lockd_up: no pid, %d users??\n", nlmsvc_users);
330
331 serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, lockd);
332 if (!serv) {
333 printk(KERN_WARNING "lockd_up: create service failed\n");
334 return -ENOMEM;
335 }
336
337 error = svc_set_num_threads(serv, NULL, 1);
338 if (error < 0) {
339 svc_destroy(&serv);
340 return error;
341 }
342
343 nlmsvc_serv = serv;
344 register_inetaddr_notifier(&lockd_inetaddr_notifier);
345#if IS_ENABLED(CONFIG_IPV6)
346 register_inet6addr_notifier(&lockd_inet6addr_notifier);
347#endif
348 dprintk("lockd_up: service created\n");
349 nlmsvc_users++;
350 return 0;
351}
352
353static void lockd_put(void)
354{
355 if (WARN(nlmsvc_users <= 0, "lockd_down: no users!\n"))
356 return;
357 if (--nlmsvc_users)
358 return;
359
360 unregister_inetaddr_notifier(&lockd_inetaddr_notifier);
361#if IS_ENABLED(CONFIG_IPV6)
362 unregister_inet6addr_notifier(&lockd_inet6addr_notifier);
363#endif
364
365 svc_set_num_threads(nlmsvc_serv, NULL, 0);
366 timer_delete_sync(&nlmsvc_retry);
367 svc_destroy(&nlmsvc_serv);
368 dprintk("lockd_down: service destroyed\n");
369}
370
371/*
372 * Bring up the lockd process if it's not already up.
373 */
374int lockd_up(struct net *net, const struct cred *cred)
375{
376 int error;
377
378 mutex_lock(&nlmsvc_mutex);
379
380 error = lockd_get();
381 if (error)
382 goto err;
383
384 error = lockd_up_net(nlmsvc_serv, net, cred);
385 if (error < 0) {
386 lockd_put();
387 goto err;
388 }
389
390err:
391 mutex_unlock(&nlmsvc_mutex);
392 return error;
393}
394EXPORT_SYMBOL_GPL(lockd_up);
395
396/*
397 * Decrement the user count and bring down lockd if we're the last.
398 */
399void
400lockd_down(struct net *net)
401{
402 mutex_lock(&nlmsvc_mutex);
403 lockd_down_net(nlmsvc_serv, net);
404 lockd_put();
405 mutex_unlock(&nlmsvc_mutex);
406}
407EXPORT_SYMBOL_GPL(lockd_down);
408
409#ifdef CONFIG_SYSCTL
410
411/*
412 * Sysctl parameters (same as module parameters, different interface).
413 */
414
415static const struct ctl_table nlm_sysctls[] = {
416 {
417 .procname = "nlm_grace_period",
418 .data = &nlm_grace_period,
419 .maxlen = sizeof(unsigned long),
420 .mode = 0644,
421 .proc_handler = proc_doulongvec_minmax,
422 .extra1 = (unsigned long *) &nlm_grace_period_min,
423 .extra2 = (unsigned long *) &nlm_grace_period_max,
424 },
425 {
426 .procname = "nlm_timeout",
427 .data = &nlm_timeout,
428 .maxlen = sizeof(unsigned long),
429 .mode = 0644,
430 .proc_handler = proc_doulongvec_minmax,
431 .extra1 = (unsigned long *) &nlm_timeout_min,
432 .extra2 = (unsigned long *) &nlm_timeout_max,
433 },
434 {
435 .procname = "nlm_udpport",
436 .data = &nlm_udpport,
437 .maxlen = sizeof(int),
438 .mode = 0644,
439 .proc_handler = proc_dointvec_minmax,
440 .extra1 = (int *) &nlm_port_min,
441 .extra2 = (int *) &nlm_port_max,
442 },
443 {
444 .procname = "nlm_tcpport",
445 .data = &nlm_tcpport,
446 .maxlen = sizeof(int),
447 .mode = 0644,
448 .proc_handler = proc_dointvec_minmax,
449 .extra1 = (int *) &nlm_port_min,
450 .extra2 = (int *) &nlm_port_max,
451 },
452 {
453 .procname = "nsm_use_hostnames",
454 .data = &nsm_use_hostnames,
455 .maxlen = sizeof(bool),
456 .mode = 0644,
457 .proc_handler = proc_dobool,
458 },
459 {
460 .procname = "nsm_local_state",
461 .data = &nsm_local_state,
462 .maxlen = sizeof(int),
463 .mode = 0644,
464 .proc_handler = proc_dointvec,
465 },
466};
467
468#endif /* CONFIG_SYSCTL */
469
470/*
471 * Module (and sysfs) parameters.
472 */
473
474#define param_set_min_max(name, type, which_strtol, min, max) \
475static int param_set_##name(const char *val, const struct kernel_param *kp) \
476{ \
477 char *endp; \
478 __typeof__(type) num = which_strtol(val, &endp, 0); \
479 if (endp == val || *endp || num < (min) || num > (max)) \
480 return -EINVAL; \
481 *((type *) kp->arg) = num; \
482 return 0; \
483}
484
485static inline int is_callback(u32 proc)
486{
487 return proc == NLMPROC_GRANTED
488 || proc == NLMPROC_GRANTED_MSG
489 || proc == NLMPROC_TEST_RES
490 || proc == NLMPROC_LOCK_RES
491 || proc == NLMPROC_CANCEL_RES
492 || proc == NLMPROC_UNLOCK_RES
493 || proc == NLMPROC_NSM_NOTIFY;
494}
495
496
497static enum svc_auth_status lockd_authenticate(struct svc_rqst *rqstp)
498{
499 rqstp->rq_client = NULL;
500 switch (rqstp->rq_authop->flavour) {
501 case RPC_AUTH_NULL:
502 case RPC_AUTH_UNIX:
503 rqstp->rq_auth_stat = rpc_auth_ok;
504 if (rqstp->rq_proc == 0)
505 return SVC_OK;
506 if (is_callback(rqstp->rq_proc)) {
507 /* Leave it to individual procedures to
508 * call nlmsvc_lookup_host(rqstp)
509 */
510 return SVC_OK;
511 }
512 return svc_set_client(rqstp);
513 }
514 rqstp->rq_auth_stat = rpc_autherr_badcred;
515 return SVC_DENIED;
516}
517
518
519param_set_min_max(port, int, simple_strtol, 0, 65535)
520param_set_min_max(grace_period, unsigned long, simple_strtoul,
521 nlm_grace_period_min, nlm_grace_period_max)
522param_set_min_max(timeout, unsigned long, simple_strtoul,
523 nlm_timeout_min, nlm_timeout_max)
524
525MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
526MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
527MODULE_LICENSE("GPL");
528
529module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
530 &nlm_grace_period, 0644);
531module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
532 &nlm_timeout, 0644);
533module_param_call(nlm_udpport, param_set_port, param_get_int,
534 &nlm_udpport, 0644);
535module_param_call(nlm_tcpport, param_set_port, param_get_int,
536 &nlm_tcpport, 0644);
537module_param(nsm_use_hostnames, bool, 0644);
538
539static int lockd_init_net(struct net *net)
540{
541 struct lockd_net *ln = net_generic(net, lockd_net_id);
542
543 INIT_DELAYED_WORK(&ln->grace_period_end, grace_ender);
544 INIT_LIST_HEAD(&ln->lockd_manager.list);
545 ln->lockd_manager.block_opens = false;
546 INIT_LIST_HEAD(&ln->nsm_handles);
547 return 0;
548}
549
550static void lockd_exit_net(struct net *net)
551{
552 struct lockd_net *ln = net_generic(net, lockd_net_id);
553
554 WARN_ONCE(!list_empty(&ln->lockd_manager.list),
555 "net %x %s: lockd_manager.list is not empty\n",
556 net->ns.inum, __func__);
557 WARN_ONCE(!list_empty(&ln->nsm_handles),
558 "net %x %s: nsm_handles list is not empty\n",
559 net->ns.inum, __func__);
560 WARN_ONCE(delayed_work_pending(&ln->grace_period_end),
561 "net %x %s: grace_period_end was not cancelled\n",
562 net->ns.inum, __func__);
563}
564
565static struct pernet_operations lockd_net_ops = {
566 .init = lockd_init_net,
567 .exit = lockd_exit_net,
568 .id = &lockd_net_id,
569 .size = sizeof(struct lockd_net),
570};
571
572
573/*
574 * Initialising and terminating the module.
575 */
576
577static int __init init_nlm(void)
578{
579 int err;
580
581#ifdef CONFIG_SYSCTL
582 err = -ENOMEM;
583 nlm_sysctl_table = register_sysctl("fs/nfs", nlm_sysctls);
584 if (nlm_sysctl_table == NULL)
585 goto err_sysctl;
586#endif
587 err = register_pernet_subsys(&lockd_net_ops);
588 if (err)
589 goto err_pernet;
590
591 err = lockd_create_procfs();
592 if (err)
593 goto err_procfs;
594
595 return 0;
596
597err_procfs:
598 unregister_pernet_subsys(&lockd_net_ops);
599err_pernet:
600#ifdef CONFIG_SYSCTL
601 unregister_sysctl_table(nlm_sysctl_table);
602err_sysctl:
603#endif
604 return err;
605}
606
607static void __exit exit_nlm(void)
608{
609 /* FIXME: delete all NLM clients */
610 nlm_shutdown_hosts();
611 lockd_remove_procfs();
612 unregister_pernet_subsys(&lockd_net_ops);
613#ifdef CONFIG_SYSCTL
614 unregister_sysctl_table(nlm_sysctl_table);
615#endif
616}
617
618module_init(init_nlm);
619module_exit(exit_nlm);
620
621/**
622 * nlmsvc_dispatch - Process an NLM Request
623 * @rqstp: incoming request
624 *
625 * Return values:
626 * %0: Processing complete; do not send a Reply
627 * %1: Processing complete; send Reply in rqstp->rq_res
628 */
629static int nlmsvc_dispatch(struct svc_rqst *rqstp)
630{
631 const struct svc_procedure *procp = rqstp->rq_procinfo;
632 __be32 *statp = rqstp->rq_accept_statp;
633
634 if (!procp->pc_decode(rqstp, &rqstp->rq_arg_stream))
635 goto out_decode_err;
636
637 *statp = procp->pc_func(rqstp);
638 if (*statp == rpc_drop_reply)
639 return 0;
640 if (*statp != rpc_success)
641 return 1;
642
643 if (!procp->pc_encode(rqstp, &rqstp->rq_res_stream))
644 goto out_encode_err;
645
646 return 1;
647
648out_decode_err:
649 *statp = rpc_garbage_args;
650 return 1;
651
652out_encode_err:
653 *statp = rpc_system_err;
654 return 1;
655}
656
657/*
658 * Define NLM program and procedures
659 */
660static DEFINE_PER_CPU_ALIGNED(unsigned long, nlmsvc_version1_count[17]);
661static const struct svc_version nlmsvc_version1 = {
662 .vs_vers = 1,
663 .vs_nproc = 17,
664 .vs_proc = nlmsvc_procedures,
665 .vs_count = nlmsvc_version1_count,
666 .vs_dispatch = nlmsvc_dispatch,
667 .vs_xdrsize = NLMSVC_XDRSIZE,
668};
669
670static DEFINE_PER_CPU_ALIGNED(unsigned long,
671 nlmsvc_version3_count[ARRAY_SIZE(nlmsvc_procedures)]);
672static const struct svc_version nlmsvc_version3 = {
673 .vs_vers = 3,
674 .vs_nproc = ARRAY_SIZE(nlmsvc_procedures),
675 .vs_proc = nlmsvc_procedures,
676 .vs_count = nlmsvc_version3_count,
677 .vs_dispatch = nlmsvc_dispatch,
678 .vs_xdrsize = NLMSVC_XDRSIZE,
679};
680
681#ifdef CONFIG_LOCKD_V4
682static DEFINE_PER_CPU_ALIGNED(unsigned long,
683 nlmsvc_version4_count[ARRAY_SIZE(nlmsvc_procedures4)]);
684static const struct svc_version nlmsvc_version4 = {
685 .vs_vers = 4,
686 .vs_nproc = ARRAY_SIZE(nlmsvc_procedures4),
687 .vs_proc = nlmsvc_procedures4,
688 .vs_count = nlmsvc_version4_count,
689 .vs_dispatch = nlmsvc_dispatch,
690 .vs_xdrsize = NLMSVC_XDRSIZE,
691};
692#endif
693
694static const struct svc_version *nlmsvc_version[] = {
695 [1] = &nlmsvc_version1,
696 [3] = &nlmsvc_version3,
697#ifdef CONFIG_LOCKD_V4
698 [4] = &nlmsvc_version4,
699#endif
700};
701
702#define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
703static struct svc_program nlmsvc_program = {
704 .pg_prog = NLM_PROGRAM, /* program number */
705 .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
706 .pg_vers = nlmsvc_version, /* version table */
707 .pg_name = "lockd", /* service name */
708 .pg_class = "nfsd", /* share authentication with nfsd */
709 .pg_authenticate = &lockd_authenticate, /* export authentication */
710 .pg_init_request = svc_generic_init_request,
711 .pg_rpcbind_set = svc_generic_rpcbind_set,
712};