blob: 378edcfe9701a34f28d2185ef3663debdf939ee5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Central processing for nfsd.
4 *
5 * Authors: Olaf Kirch (okir@monad.swb.de)
6 *
7 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
8 */
9
Ingo Molnar3f07c012017-02-08 18:51:30 +010010#include <linux/sched/signal.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070011#include <linux/freezer.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/fs_struct.h>
Andy Adamsonc3d06f92009-04-03 08:28:18 +030014#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sunrpc/stats.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/sunrpc/svcsock.h>
Scott Mayhew36684992015-12-11 16:45:59 -050018#include <linux/sunrpc/svc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/lockd/bind.h>
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000020#include <linux/nfsacl.h>
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +090021#include <linux/seq_file.h>
Scott Mayhew36684992015-12-11 16:45:59 -050022#include <linux/inetdevice.h>
23#include <net/addrconf.h>
24#include <net/ipv6.h>
Pavel Emelyanovfc5d00b2010-09-29 16:03:50 +040025#include <net/net_namespace.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020026#include "nfsd.h"
27#include "cache.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050028#include "vfs.h"
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +030029#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#define NFSDDBG_FACILITY NFSDDBG_SVC
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033extern struct svc_program nfsd_program;
Jeff Layton9867d762008-06-10 08:40:38 -040034static int nfsd(void *vrqstp);
Trond Myklebust029be5d2019-04-09 11:46:18 -040035#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
36static int nfsd_acl_rpcbind_set(struct net *,
37 const struct svc_program *,
38 u32, int,
39 unsigned short,
40 unsigned short);
41#endif
42static int nfsd_rpcbind_set(struct net *,
43 const struct svc_program *,
44 u32, int,
45 unsigned short,
46 unsigned short);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Neil Brownbedbdd82008-06-10 08:40:35 -040048/*
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +030049 * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
Neil Brownbedbdd82008-06-10 08:40:35 -040050 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
51 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
52 *
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +030053 * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
Neil Brownbedbdd82008-06-10 08:40:35 -040054 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
55 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
56 * entry of ->sv_pools[].
57 *
58 * Transitions of the thread count between zero and non-zero are of particular
59 * interest since the svc_serv needs to be created and initialized at that
60 * point, or freed.
Jeff Layton3dd98a32008-06-10 08:40:36 -040061 *
62 * Finally, the nfsd_mutex also protects some of the global variables that are
63 * accessed when nfsd starts and that are settable via the write_* routines in
64 * nfsctl.c. In particular:
65 *
66 * user_recovery_dirname
67 * user_lease_time
68 * nfsd_versions
Neil Brownbedbdd82008-06-10 08:40:35 -040069 */
70DEFINE_MUTEX(nfsd_mutex);
Neil Brownbedbdd82008-06-10 08:40:35 -040071
Andy Adamson4bd9b0f42009-06-24 15:37:45 -040072/*
73 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
74 * nfsd_drc_max_pages limits the total amount of memory available for
75 * version 4.1 DRC caches.
76 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
77 */
78spinlock_t nfsd_drc_lock;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -080079unsigned long nfsd_drc_max_mem;
80unsigned long nfsd_drc_mem_used;
Andy Adamson4bd9b0f42009-06-24 15:37:45 -040081
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080082#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
83static struct svc_stat nfsd_acl_svcstats;
Christoph Hellwige9679182017-05-12 16:21:37 +020084static const struct svc_version *nfsd_acl_version[] = {
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080085 [2] = &nfsd_acl_version2,
86 [3] = &nfsd_acl_version3,
87};
88
89#define NFSD_ACL_MINVERS 2
Tobias Klausere8c96f82006-03-24 03:15:34 -080090#define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
Christoph Hellwige9679182017-05-12 16:21:37 +020091static const struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080092
93static struct svc_program nfsd_acl_program = {
94 .pg_prog = NFS_ACL_PROGRAM,
95 .pg_nvers = NFSD_ACL_NRVERS,
96 .pg_vers = nfsd_acl_versions,
NeilBrown1a8eff62007-01-26 00:56:58 -080097 .pg_name = "nfsacl",
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080098 .pg_class = "nfsd",
99 .pg_stats = &nfsd_acl_svcstats,
100 .pg_authenticate = &svc_set_client,
Trond Myklebust8e5b6772019-04-09 11:46:15 -0400101 .pg_init_request = svc_generic_init_request,
Trond Myklebust029be5d2019-04-09 11:46:18 -0400102 .pg_rpcbind_set = nfsd_acl_rpcbind_set,
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800103};
104
105static struct svc_stat nfsd_acl_svcstats = {
106 .program = &nfsd_acl_program,
107};
108#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
109
Christoph Hellwige9679182017-05-12 16:21:37 +0200110static const struct svc_version *nfsd_version[] = {
NeilBrown70c3b762005-11-07 01:00:25 -0800111 [2] = &nfsd_version2,
112#if defined(CONFIG_NFSD_V3)
113 [3] = &nfsd_version3,
114#endif
115#if defined(CONFIG_NFSD_V4)
116 [4] = &nfsd_version4,
117#endif
118};
119
120#define NFSD_MINVERS 2
Tobias Klausere8c96f82006-03-24 03:15:34 -0800121#define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
Christoph Hellwige9679182017-05-12 16:21:37 +0200122static const struct svc_version *nfsd_versions[NFSD_NRVERS];
NeilBrown70c3b762005-11-07 01:00:25 -0800123
124struct svc_program nfsd_program = {
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800125#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
126 .pg_next = &nfsd_acl_program,
127#endif
NeilBrown70c3b762005-11-07 01:00:25 -0800128 .pg_prog = NFS_PROGRAM, /* program number */
129 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
130 .pg_vers = nfsd_versions, /* version table */
131 .pg_name = "nfsd", /* program name */
132 .pg_class = "nfsd", /* authentication class */
133 .pg_stats = &nfsd_svcstats, /* version table */
134 .pg_authenticate = &svc_set_client, /* export authentication */
Trond Myklebust8e5b6772019-04-09 11:46:15 -0400135 .pg_init_request = svc_generic_init_request,
Trond Myklebust029be5d2019-04-09 11:46:18 -0400136 .pg_rpcbind_set = nfsd_rpcbind_set,
NeilBrown70c3b762005-11-07 01:00:25 -0800137};
138
J. Bruce Fields35f7a142013-07-08 19:51:44 -0400139static bool nfsd_supported_minorversions[NFSD_SUPPORTED_MINOR_VERSION + 1] = {
140 [0] = 1,
141 [1] = 1,
J. Bruce Fieldsc23ae602015-01-12 14:52:15 -0500142 [2] = 1,
J. Bruce Fields35f7a142013-07-08 19:51:44 -0400143};
Benny Halevy8daf2202009-04-03 08:28:59 +0300144
NeilBrown6658d3a2006-10-02 02:17:46 -0700145int nfsd_vers(int vers, enum vers_op change)
146{
147 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
Pavel Emelyanov15ddb4a2010-05-14 15:33:36 +0400148 return 0;
NeilBrown6658d3a2006-10-02 02:17:46 -0700149 switch(change) {
150 case NFSD_SET:
151 nfsd_versions[vers] = nfsd_version[vers];
NeilBrown6658d3a2006-10-02 02:17:46 -0700152#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
153 if (vers < NFSD_ACL_NRVERS)
NeilBrown1a8eff62007-01-26 00:56:58 -0800154 nfsd_acl_versions[vers] = nfsd_acl_version[vers];
NeilBrown6658d3a2006-10-02 02:17:46 -0700155#endif
NeilBrown1a8eff62007-01-26 00:56:58 -0800156 break;
NeilBrown6658d3a2006-10-02 02:17:46 -0700157 case NFSD_CLEAR:
158 nfsd_versions[vers] = NULL;
159#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
160 if (vers < NFSD_ACL_NRVERS)
NeilBrown1a8eff62007-01-26 00:56:58 -0800161 nfsd_acl_versions[vers] = NULL;
NeilBrown6658d3a2006-10-02 02:17:46 -0700162#endif
163 break;
164 case NFSD_TEST:
165 return nfsd_versions[vers] != NULL;
166 case NFSD_AVAIL:
167 return nfsd_version[vers] != NULL;
168 }
169 return 0;
170}
Benny Halevy8daf2202009-04-03 08:28:59 +0300171
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500172static void
173nfsd_adjust_nfsd_versions4(void)
174{
175 unsigned i;
176
177 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
178 if (nfsd_supported_minorversions[i])
179 return;
180 }
181 nfsd_vers(4, NFSD_CLEAR);
182}
183
Benny Halevy8daf2202009-04-03 08:28:59 +0300184int nfsd_minorversion(u32 minorversion, enum vers_op change)
185{
NeilBrown928c6fb2017-03-10 11:36:39 +1100186 if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
187 change != NFSD_AVAIL)
Benny Halevy8daf2202009-04-03 08:28:59 +0300188 return -1;
189 switch(change) {
190 case NFSD_SET:
J. Bruce Fields35f7a142013-07-08 19:51:44 -0400191 nfsd_supported_minorversions[minorversion] = true;
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500192 nfsd_vers(4, NFSD_SET);
Benny Halevy8daf2202009-04-03 08:28:59 +0300193 break;
194 case NFSD_CLEAR:
J. Bruce Fields35f7a142013-07-08 19:51:44 -0400195 nfsd_supported_minorversions[minorversion] = false;
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500196 nfsd_adjust_nfsd_versions4();
Benny Halevy8daf2202009-04-03 08:28:59 +0300197 break;
198 case NFSD_TEST:
J. Bruce Fields35f7a142013-07-08 19:51:44 -0400199 return nfsd_supported_minorversions[minorversion];
Benny Halevy8daf2202009-04-03 08:28:59 +0300200 case NFSD_AVAIL:
201 return minorversion <= NFSD_SUPPORTED_MINOR_VERSION;
202 }
203 return 0;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
207 * Maximum number of nfsd processes
208 */
209#define NFSD_MAXSERVS 8192
210
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300211int nfsd_nrthreads(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Neil Brownc7d106c2008-06-12 13:38:42 +1000213 int rv = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300214 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
215
Neil Brownc7d106c2008-06-12 13:38:42 +1000216 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300217 if (nn->nfsd_serv)
218 rv = nn->nfsd_serv->sv_nrthreads;
Neil Brownc7d106c2008-06-12 13:38:42 +1000219 mutex_unlock(&nfsd_mutex);
220 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Stanislav Kinsburskydb6e1822012-12-10 12:19:09 +0300223static int nfsd_init_socks(struct net *net)
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400224{
225 int error;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300226 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
227
228 if (!list_empty(&nn->nfsd_serv->sv_permsocks))
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400229 return 0;
230
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300231 error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400232 SVC_SOCK_DEFAULTS);
233 if (error < 0)
234 return error;
235
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300236 error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400237 SVC_SOCK_DEFAULTS);
238 if (error < 0)
239 return error;
240
241 return 0;
242}
243
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300244static int nfsd_users = 0;
Jeff Layton4ad9a342010-07-19 16:50:04 -0400245
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300246static int nfsd_startup_generic(int nrservs)
247{
248 int ret;
249
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300250 if (nfsd_users++)
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300251 return 0;
252
253 /*
254 * Readahead param cache - will no-op if it already exists.
255 * (Note therefore results will be suboptimal if number of
256 * threads is modified after nfsd start.)
257 */
258 ret = nfsd_racache_init(2*nrservs);
259 if (ret)
Kinglong Meed9499a92014-07-30 21:26:05 +0800260 goto dec_users;
261
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300262 ret = nfs4_state_start();
263 if (ret)
264 goto out_racache;
265 return 0;
266
267out_racache:
268 nfsd_racache_shutdown();
Kinglong Meed9499a92014-07-30 21:26:05 +0800269dec_users:
270 nfsd_users--;
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300271 return ret;
272}
273
274static void nfsd_shutdown_generic(void)
275{
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300276 if (--nfsd_users)
277 return;
278
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300279 nfs4_state_shutdown();
280 nfsd_racache_shutdown();
281}
282
Kinglong Mee8ef66712013-12-31 13:17:30 +0800283static bool nfsd_needs_lockd(void)
284{
Kinglong Meeff888252014-01-06 11:28:41 +0800285#if defined(CONFIG_NFSD_V3)
Kinglong Mee8ef66712013-12-31 13:17:30 +0800286 return (nfsd_versions[2] != NULL) || (nfsd_versions[3] != NULL);
Kinglong Meeff888252014-01-06 11:28:41 +0800287#else
288 return (nfsd_versions[2] != NULL);
289#endif
Kinglong Mee8ef66712013-12-31 13:17:30 +0800290}
291
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300292static int nfsd_startup_net(int nrservs, struct net *net)
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300293{
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300294 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300295 int ret;
296
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300297 if (nn->nfsd_net_up)
298 return 0;
299
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300300 ret = nfsd_startup_generic(nrservs);
301 if (ret)
302 return ret;
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300303 ret = nfsd_init_socks(net);
304 if (ret)
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300305 goto out_socks;
Kinglong Mee8ef66712013-12-31 13:17:30 +0800306
307 if (nfsd_needs_lockd() && !nn->lockd_up) {
308 ret = lockd_up(net);
309 if (ret)
310 goto out_socks;
311 nn->lockd_up = 1;
312 }
313
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300314 ret = nfs4_state_start_net(net);
315 if (ret)
316 goto out_lockd;
317
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300318 nn->nfsd_net_up = true;
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300319 return 0;
320
321out_lockd:
Kinglong Mee8ef66712013-12-31 13:17:30 +0800322 if (nn->lockd_up) {
323 lockd_down(net);
324 nn->lockd_up = 0;
325 }
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300326out_socks:
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300327 nfsd_shutdown_generic();
Jeff Layton4ad9a342010-07-19 16:50:04 -0400328 return ret;
329}
330
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300331static void nfsd_shutdown_net(struct net *net)
332{
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300333 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
334
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300335 nfs4_state_shutdown_net(net);
Kinglong Mee8ef66712013-12-31 13:17:30 +0800336 if (nn->lockd_up) {
337 lockd_down(net);
338 nn->lockd_up = 0;
339 }
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300340 nn->nfsd_net_up = false;
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300341 nfsd_shutdown_generic();
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300342}
343
Scott Mayhew36684992015-12-11 16:45:59 -0500344static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
345 void *ptr)
346{
347 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
348 struct net_device *dev = ifa->ifa_dev->dev;
349 struct net *net = dev_net(dev);
350 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
351 struct sockaddr_in sin;
352
Vasily Averin2317dc52017-11-10 10:19:35 +0300353 if ((event != NETDEV_DOWN) ||
354 !atomic_inc_not_zero(&nn->ntf_refcnt))
Scott Mayhew36684992015-12-11 16:45:59 -0500355 goto out;
356
357 if (nn->nfsd_serv) {
358 dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
359 sin.sin_family = AF_INET;
360 sin.sin_addr.s_addr = ifa->ifa_local;
361 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
362 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300363 atomic_dec(&nn->ntf_refcnt);
364 wake_up(&nn->ntf_wq);
Scott Mayhew36684992015-12-11 16:45:59 -0500365
366out:
367 return NOTIFY_DONE;
368}
369
370static struct notifier_block nfsd_inetaddr_notifier = {
371 .notifier_call = nfsd_inetaddr_event,
372};
373
374#if IS_ENABLED(CONFIG_IPV6)
375static int nfsd_inet6addr_event(struct notifier_block *this,
376 unsigned long event, void *ptr)
377{
378 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
379 struct net_device *dev = ifa->idev->dev;
380 struct net *net = dev_net(dev);
381 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
382 struct sockaddr_in6 sin6;
383
Vasily Averin2317dc52017-11-10 10:19:35 +0300384 if ((event != NETDEV_DOWN) ||
385 !atomic_inc_not_zero(&nn->ntf_refcnt))
Scott Mayhew36684992015-12-11 16:45:59 -0500386 goto out;
387
388 if (nn->nfsd_serv) {
389 dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
390 sin6.sin6_family = AF_INET6;
391 sin6.sin6_addr = ifa->addr;
Scott Mayhew7b198242017-01-05 16:34:49 -0500392 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
393 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
Scott Mayhew36684992015-12-11 16:45:59 -0500394 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
395 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300396 atomic_dec(&nn->ntf_refcnt);
397 wake_up(&nn->ntf_wq);
Scott Mayhew36684992015-12-11 16:45:59 -0500398out:
399 return NOTIFY_DONE;
400}
401
402static struct notifier_block nfsd_inet6addr_notifier = {
403 .notifier_call = nfsd_inet6addr_event,
404};
405#endif
406
Vasily Averin1eca45f2016-09-21 15:33:05 +0300407/* Only used under nfsd_mutex, so this atomic may be overkill: */
408static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
409
Stanislav Kinsbursky541e8642012-12-06 14:23:44 +0300410static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
Jeff Layton4ad9a342010-07-19 16:50:04 -0400411{
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300412 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
413
Vasily Averin2317dc52017-11-10 10:19:35 +0300414 atomic_dec(&nn->ntf_refcnt);
Vasily Averin1eca45f2016-09-21 15:33:05 +0300415 /* check if the notifier still has clients */
416 if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
417 unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500418#if IS_ENABLED(CONFIG_IPV6)
Vasily Averin1eca45f2016-09-21 15:33:05 +0300419 unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500420#endif
Vasily Averin1eca45f2016-09-21 15:33:05 +0300421 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300422 wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
Vasily Averin1eca45f2016-09-21 15:33:05 +0300423
Jeff Layton4ad9a342010-07-19 16:50:04 -0400424 /*
425 * write_ports can create the server without actually starting
426 * any threads--if we get shut down before any threads are
427 * started, then nfsd_last_thread will be run before any of this
Kinglong Mee691412b2016-01-04 11:15:21 +0800428 * other initialization has been done except the rpcb information.
Jeff Layton4ad9a342010-07-19 16:50:04 -0400429 */
Kinglong Mee691412b2016-01-04 11:15:21 +0800430 svc_rpcb_cleanup(serv, net);
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300431 if (!nn->nfsd_net_up)
Jeff Layton4ad9a342010-07-19 16:50:04 -0400432 return;
Kinglong Mee691412b2016-01-04 11:15:21 +0800433
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300434 nfsd_shutdown_net(net);
Jeff Laytone096bbc2008-06-10 08:40:37 -0400435 printk(KERN_WARNING "nfsd: last server has exited, flushing export "
436 "cache\n");
Stanislav Kinsburskyb3853e02012-04-11 15:13:21 +0400437 nfsd_export_flush(net);
NeilBrownbc591cc2006-10-02 02:17:44 -0700438}
NeilBrown6658d3a2006-10-02 02:17:46 -0700439
440void nfsd_reset_versions(void)
441{
NeilBrown6658d3a2006-10-02 02:17:46 -0700442 int i;
443
NeilBrown800a9382017-03-10 11:36:39 +1100444 for (i = 0; i < NFSD_NRVERS; i++)
445 if (nfsd_vers(i, NFSD_TEST))
446 return;
NeilBrown6658d3a2006-10-02 02:17:46 -0700447
NeilBrown800a9382017-03-10 11:36:39 +1100448 for (i = 0; i < NFSD_NRVERS; i++)
449 if (i != 4)
450 nfsd_vers(i, NFSD_SET);
451 else {
452 int minor = 0;
453 while (nfsd_minorversion(minor, NFSD_SET) >= 0)
454 minor++;
455 }
NeilBrown6658d3a2006-10-02 02:17:46 -0700456}
457
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300458/*
459 * Each session guarantees a negotiated per slot memory cache for replies
460 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
461 * NFSv4.1 server might want to use more memory for a DRC than a machine
462 * with mutiple services.
463 *
464 * Impose a hard limit on the number of pages for the DRC which varies
465 * according to the machines free pages. This is of course only a default.
466 *
467 * For now this is a #defined shift which could be under admin control
468 * in the future.
469 */
470static void set_max_drc(void)
471{
J. Bruce Fields44d86602017-09-19 20:51:31 -0400472 #define NFSD_DRC_SIZE_SHIFT 7
Andy Adamson0c193052009-07-27 19:09:19 -0400473 nfsd_drc_max_mem = (nr_free_buffer_pages()
474 >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
475 nfsd_drc_mem_used = 0;
Andy Adamson4bd9b0f42009-06-24 15:37:45 -0400476 spin_lock_init(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800477 dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300478}
Neil Brownbedbdd82008-06-10 08:40:35 -0400479
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500480static int nfsd_get_default_max_blksize(void)
481{
482 struct sysinfo i;
483 unsigned long long target;
484 unsigned long ret;
485
486 si_meminfo(&i);
J. Bruce Fields508f9222012-01-30 16:21:11 -0500487 target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500488 /*
489 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
490 * machines, but only uses 32K on 128M machines. Bottom out at
491 * 8K on 32M and smaller. Of course, this is only a default.
492 */
493 target >>= 12;
494
495 ret = NFSSVC_MAXBLKSIZE;
496 while (ret > target && ret >= 8*1024*2)
497 ret /= 2;
498 return ret;
499}
500
Chuck Leverafea5652017-08-01 12:00:06 -0400501static const struct svc_serv_ops nfsd_thread_sv_ops = {
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700502 .svo_shutdown = nfsd_last_thread,
503 .svo_function = nfsd,
504 .svo_enqueue_xprt = svc_xprt_do_enqueue,
Jeff Layton598e2352015-06-08 12:08:33 -0700505 .svo_setup = svc_set_num_threads,
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700506 .svo_module = THIS_MODULE,
Jeff Laytonea126e72015-06-08 12:03:32 -0700507};
508
Stanislav Kinsbursky67774362012-12-10 12:19:20 +0300509int nfsd_create_serv(struct net *net)
NeilBrown02a375f2006-10-02 02:17:46 -0700510{
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400511 int error;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300512 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400513
Neil Brownbedbdd82008-06-10 08:40:35 -0400514 WARN_ON(!mutex_is_locked(&nfsd_mutex));
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300515 if (nn->nfsd_serv) {
516 svc_get(nn->nfsd_serv);
NeilBrown02a375f2006-10-02 02:17:46 -0700517 return 0;
518 }
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500519 if (nfsd_max_blksize == 0)
520 nfsd_max_blksize = nfsd_get_default_max_blksize();
J. Bruce Fieldse844a7b2010-08-06 15:48:03 -0400521 nfsd_reset_versions();
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300522 nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700523 &nfsd_thread_sv_ops);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300524 if (nn->nfsd_serv == NULL)
Jeff Layton628b3682010-07-21 16:40:08 -0400525 return -ENOMEM;
Neil Brownbedbdd82008-06-10 08:40:35 -0400526
Jeff Layton5b8db002014-07-02 16:11:22 -0400527 nn->nfsd_serv->sv_maxconn = nn->max_connections;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300528 error = svc_bind(nn->nfsd_serv, net);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400529 if (error < 0) {
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300530 svc_destroy(nn->nfsd_serv);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400531 return error;
532 }
533
Jeff Layton628b3682010-07-21 16:40:08 -0400534 set_max_drc();
Vasily Averin1eca45f2016-09-21 15:33:05 +0300535 /* check if the notifier is already set */
536 if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
537 register_inetaddr_notifier(&nfsd_inetaddr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500538#if IS_ENABLED(CONFIG_IPV6)
Vasily Averin1eca45f2016-09-21 15:33:05 +0300539 register_inet6addr_notifier(&nfsd_inet6addr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500540#endif
Vasily Averin1eca45f2016-09-21 15:33:05 +0300541 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300542 atomic_inc(&nn->ntf_refcnt);
Arnd Bergmann256a89f2017-10-19 12:04:11 +0200543 ktime_get_real_ts64(&nn->nfssvc_boot); /* record boot time */
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500544 return 0;
NeilBrown02a375f2006-10-02 02:17:46 -0700545}
546
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300547int nfsd_nrpools(struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700548{
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300549 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
550
551 if (nn->nfsd_serv == NULL)
Greg Bankseed29652006-10-02 02:18:02 -0700552 return 0;
553 else
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300554 return nn->nfsd_serv->sv_nrpools;
Greg Bankseed29652006-10-02 02:18:02 -0700555}
556
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300557int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700558{
559 int i = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300560 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Greg Bankseed29652006-10-02 02:18:02 -0700561
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300562 if (nn->nfsd_serv != NULL) {
563 for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
564 nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
Greg Bankseed29652006-10-02 02:18:02 -0700565 }
566
567 return 0;
568}
569
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300570void nfsd_destroy(struct net *net)
571{
572 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
573 int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
574
575 if (destroy)
576 svc_shutdown_net(nn->nfsd_serv, net);
577 svc_destroy(nn->nfsd_serv);
578 if (destroy)
579 nn->nfsd_serv = NULL;
580}
581
Stanislav Kinsbursky3938a0d2012-12-10 12:19:30 +0300582int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700583{
584 int i = 0;
585 int tot = 0;
586 int err = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300587 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Greg Bankseed29652006-10-02 02:18:02 -0700588
Neil Brownbedbdd82008-06-10 08:40:35 -0400589 WARN_ON(!mutex_is_locked(&nfsd_mutex));
590
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300591 if (nn->nfsd_serv == NULL || n <= 0)
Greg Bankseed29652006-10-02 02:18:02 -0700592 return 0;
593
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300594 if (n > nn->nfsd_serv->sv_nrpools)
595 n = nn->nfsd_serv->sv_nrpools;
Greg Bankseed29652006-10-02 02:18:02 -0700596
597 /* enforce a global maximum number of threads */
598 tot = 0;
599 for (i = 0; i < n; i++) {
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800600 nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
Greg Bankseed29652006-10-02 02:18:02 -0700601 tot += nthreads[i];
602 }
603 if (tot > NFSD_MAXSERVS) {
604 /* total too large: scale down requested numbers */
605 for (i = 0; i < n && tot > 0; i++) {
606 int new = nthreads[i] * NFSD_MAXSERVS / tot;
607 tot -= (nthreads[i] - new);
608 nthreads[i] = new;
609 }
610 for (i = 0; i < n && tot > 0; i++) {
611 nthreads[i]--;
612 tot--;
613 }
614 }
615
616 /*
617 * There must always be a thread in pool 0; the admin
618 * can't shut down NFS completely using pool_threads.
619 */
620 if (nthreads[0] == 0)
621 nthreads[0] = 1;
622
623 /* apply the new numbers */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300624 svc_get(nn->nfsd_serv);
Greg Bankseed29652006-10-02 02:18:02 -0700625 for (i = 0; i < n; i++) {
Jeff Layton598e2352015-06-08 12:08:33 -0700626 err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
627 &nn->nfsd_serv->sv_pools[i], nthreads[i]);
Greg Bankseed29652006-10-02 02:18:02 -0700628 if (err)
629 break;
630 }
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400631 nfsd_destroy(net);
Greg Bankseed29652006-10-02 02:18:02 -0700632 return err;
633}
634
Jeff Laytonac77efb2010-07-20 14:10:22 -0400635/*
636 * Adjust the number of threads and return the new number of threads.
637 * This is also the function that starts the server if necessary, if
638 * this is the first time nrservs is nonzero.
639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640int
Stanislav Kinsburskyd41a9412012-12-10 12:19:25 +0300641nfsd_svc(int nrservs, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int error;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400644 bool nfsd_up_before;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300645 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Neil Brownbedbdd82008-06-10 08:40:35 -0400646
647 mutex_lock(&nfsd_mutex);
NeilBrown6658d3a2006-10-02 02:17:46 -0700648 dprintk("nfsd: creating service\n");
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800649
650 nrservs = max(nrservs, 0);
651 nrservs = min(nrservs, NFSD_MAXSERVS);
NeilBrown671e1fc2009-06-16 11:03:20 +1000652 error = 0;
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800653
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300654 if (nrservs == 0 && nn->nfsd_serv == NULL)
NeilBrown671e1fc2009-06-16 11:03:20 +1000655 goto out;
656
Stanislav Kinsbursky67774362012-12-10 12:19:20 +0300657 error = nfsd_create_serv(net);
NeilBrown02a375f2006-10-02 02:17:46 -0700658 if (error)
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400659 goto out;
660
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300661 nfsd_up_before = nn->nfsd_net_up;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400662
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300663 error = nfsd_startup_net(nrservs, net);
J. Bruce Fieldsaf4718f2010-07-21 18:31:42 -0400664 if (error)
665 goto out_destroy;
Jeff Layton598e2352015-06-08 12:08:33 -0700666 error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
667 NULL, nrservs);
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400668 if (error)
669 goto out_shutdown;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300670 /* We are holding a reference to nn->nfsd_serv which
J. Bruce Fieldsaf4718f2010-07-21 18:31:42 -0400671 * we don't want to count in the return value,
672 * so subtract 1
673 */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300674 error = nn->nfsd_serv->sv_nrthreads - 1;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400675out_shutdown:
676 if (error < 0 && !nfsd_up_before)
Stanislav Kinsbursky541e8642012-12-06 14:23:44 +0300677 nfsd_shutdown_net(net);
Jeff Layton4ad9a342010-07-19 16:50:04 -0400678out_destroy:
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400679 nfsd_destroy(net); /* Release server */
Jeff Layton4ad9a342010-07-19 16:50:04 -0400680out:
Neil Brownbedbdd82008-06-10 08:40:35 -0400681 mutex_unlock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return error;
683}
684
Trond Myklebust029be5d2019-04-09 11:46:18 -0400685#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
686static bool
687nfsd_support_acl_version(int vers)
688{
689 if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
690 return nfsd_acl_version[vers] != NULL;
691 return false;
692}
693
694static int
695nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
696 u32 version, int family, unsigned short proto,
697 unsigned short port)
698{
699 if (!nfsd_support_acl_version(version) ||
700 !nfsd_vers(version, NFSD_TEST))
701 return 0;
702 return svc_generic_rpcbind_set(net, progp, version, family,
703 proto, port);
704}
705#endif
706
707static int
708nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
709 u32 version, int family, unsigned short proto,
710 unsigned short port)
711{
712 if (!nfsd_vers(version, NFSD_TEST))
713 return 0;
714 return svc_generic_rpcbind_set(net, progp, version, family,
715 proto, port);
716}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718/*
719 * This is the NFS server kernel thread
720 */
Jeff Layton9867d762008-06-10 08:40:38 -0400721static int
722nfsd(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Jeff Layton9867d762008-06-10 08:40:38 -0400724 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
Stanislav Kinsbursky88c47662012-12-06 18:34:42 +0300725 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
726 struct net *net = perm_sock->xpt_net;
Jeff Layton5b8db002014-07-02 16:11:22 -0400727 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields5b444cc2012-08-17 21:47:53 -0400728 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* Lock module and set up kernel thread */
Neil Brownbedbdd82008-06-10 08:40:35 -0400731 mutex_lock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Jeff Layton9867d762008-06-10 08:40:38 -0400733 /* At this point, the thread shares current->fs
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100734 * with the init process. We need to create files with the
735 * umask as defined by the client instead of init's umask. */
Al Viro3e93cd62009-03-29 19:00:13 -0400736 if (unshare_fs_struct() < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 printk("Unable to start nfsd thread: out of memory\n");
738 goto out;
739 }
Al Viro3e93cd62009-03-29 19:00:13 -0400740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 current->fs->umask = 0;
742
Jeff Layton9867d762008-06-10 08:40:38 -0400743 /*
744 * thread is spawned with all signals set to SIG_IGN, re-enable
Jeff Layton100766f2008-06-30 14:09:46 -0400745 * the ones that will bring down the thread
Jeff Layton9867d762008-06-10 08:40:38 -0400746 */
Jeff Layton100766f2008-06-30 14:09:46 -0400747 allow_signal(SIGKILL);
748 allow_signal(SIGHUP);
749 allow_signal(SIGINT);
750 allow_signal(SIGQUIT);
Neil Brownbedbdd82008-06-10 08:40:35 -0400751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 nfsdstats.th_cnt++;
Neil Brownbedbdd82008-06-10 08:40:35 -0400753 mutex_unlock(&nfsd_mutex);
754
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700755 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /*
758 * The main request loop
759 */
760 for (;;) {
Jeff Layton5b8db002014-07-02 16:11:22 -0400761 /* Update sv_maxconn if it has changed */
762 rqstp->rq_server->sv_maxconn = nn->max_connections;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * Find a socket with data available and call its
766 * recvfrom routine.
767 */
NeilBrown6fb2b472006-10-02 02:17:50 -0700768 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ;
Jeff Layton9867d762008-06-10 08:40:38 -0400770 if (err == -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
David Howellse0e81732009-09-02 09:13:40 +0100772 validate_process_creds();
NeilBrown6fb2b472006-10-02 02:17:50 -0700773 svc_process(rqstp);
David Howellse0e81732009-09-02 09:13:40 +0100774 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
NeilBrown24e36662006-10-02 02:17:45 -0700777 /* Clear signals before calling svc_exit_thread() */
NeilBrown9e4160522005-04-16 15:26:37 -0700778 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Neil Brownbedbdd82008-06-10 08:40:35 -0400780 mutex_lock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 nfsdstats.th_cnt --;
782
783out:
Stanislav Kinsbursky57c8b13e2012-07-03 16:46:41 +0400784 rqstp->rq_server = NULL;
Stanislav Kinsbursky786185b2012-05-04 12:49:41 +0400785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* Release the thread */
787 svc_exit_thread(rqstp);
788
Stanislav Kinsbursky88c47662012-12-06 18:34:42 +0300789 nfsd_destroy(net);
Stanislav Kinsbursky57c8b13e2012-07-03 16:46:41 +0400790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* Release module */
Neil Brownbedbdd82008-06-10 08:40:35 -0400792 mutex_unlock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 module_put_and_exit(0);
Jeff Layton9867d762008-06-10 08:40:38 -0400794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Andy Adamson32c1eb02007-07-17 04:04:48 -0700797static __be32 map_new_errors(u32 vers, __be32 nfserr)
798{
799 if (nfserr == nfserr_jukebox && vers == 2)
800 return nfserr_dropit;
801 if (nfserr == nfserr_wrongsec && vers < 4)
802 return nfserr_acces;
803 return nfserr;
804}
805
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400806/*
807 * A write procedure can have a large argument, and a read procedure can
808 * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
809 * reply that can both be larger than a page. The xdr code has taken
810 * advantage of this assumption to be a sloppy about bounds checking in
811 * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that
812 * problem, we enforce these assumptions here:
813 */
814static bool nfs_request_too_big(struct svc_rqst *rqstp,
Christoph Hellwig860bda22017-05-12 16:11:49 +0200815 const struct svc_procedure *proc)
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400816{
817 /*
818 * The ACL code has more careful bounds-checking and is not
819 * susceptible to this problem:
820 */
821 if (rqstp->rq_prog != NFS_PROGRAM)
822 return false;
823 /*
824 * Ditto NFSv4 (which can in theory have argument and reply both
825 * more than a page):
826 */
827 if (rqstp->rq_vers >= 4)
828 return false;
829 /* The reply will be small, we're OK: */
830 if (proc->pc_xdrressize > 0 &&
831 proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
832 return false;
833
834 return rqstp->rq_arg.len > PAGE_SIZE;
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837int
Al Viroc7afef12006-10-19 23:29:02 -0700838nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Christoph Hellwig860bda22017-05-12 16:11:49 +0200840 const struct svc_procedure *proc;
Al Viroad451d32006-10-19 23:28:55 -0700841 __be32 nfserr;
842 __be32 *nfserrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 dprintk("nfsd_dispatch: vers %d proc %d\n",
845 rqstp->rq_vers, rqstp->rq_proc);
846 proc = rqstp->rq_procinfo;
847
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400848 if (nfs_request_too_big(rqstp, proc)) {
849 dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
850 *statp = rpc_garbage_args;
851 return 1;
852 }
J. Bruce Fields10910062011-01-24 12:11:02 -0500853 /*
854 * Give the xdr decoder a chance to change this if it wants
855 * (necessary in the NFSv4.0 compound case)
856 */
857 rqstp->rq_cachetype = proc->pc_cachetype;
858 /* Decode arguments */
Christoph Hellwig026fec72017-05-08 19:01:48 +0200859 if (proc->pc_decode &&
860 !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) {
J. Bruce Fields10910062011-01-24 12:11:02 -0500861 dprintk("nfsd: failed to decode arguments!\n");
862 *statp = rpc_garbage_args;
863 return 1;
864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /* Check whether we have this call in the cache. */
J. Bruce Fields10910062011-01-24 12:11:02 -0500867 switch (nfsd_cache_lookup(rqstp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 case RC_DROPIT:
869 return 0;
870 case RC_REPLY:
871 return 1;
872 case RC_DOIT:;
873 /* do it */
874 }
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* need to grab the location to store the status, as
877 * nfsv4 does some encoding while processing
878 */
879 nfserrp = rqstp->rq_res.head[0].iov_base
880 + rqstp->rq_res.head[0].iov_len;
Al Viroad451d32006-10-19 23:28:55 -0700881 rqstp->rq_res.head[0].iov_len += sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 /* Now call the procedure handler, and encode NFS status. */
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200884 nfserr = proc->pc_func(rqstp);
Andy Adamson32c1eb02007-07-17 04:04:48 -0700885 nfserr = map_new_errors(rqstp->rq_vers, nfserr);
Jeff Layton78b65eb2014-11-19 07:51:17 -0500886 if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) {
J. Bruce Fields45457e02007-06-22 17:26:32 -0400887 dprintk("nfsd: Dropping request; may be revisited later\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
889 return 0;
890 }
891
892 if (rqstp->rq_proc != 0)
893 *nfserrp++ = nfserr;
894
895 /* Encode result.
896 * For NFSv2, additional info is never returned in case of an error.
897 */
898 if (!(nfserr && rqstp->rq_vers == 2)) {
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200899 if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* Failed to encode result. Release cache entry */
901 dprintk("nfsd: failed to encode result!\n");
902 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
903 *statp = rpc_system_err;
904 return 1;
905 }
906 }
907
908 /* Store reply in cache. */
J. Bruce Fields57d276d2012-11-16 15:22:43 -0500909 nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return 1;
911}
Greg Banks03cf6c92009-01-13 21:26:36 +1100912
913int nfsd_pool_stats_open(struct inode *inode, struct file *file)
914{
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900915 int ret;
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +0300916 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300917
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900918 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300919 if (nn->nfsd_serv == NULL) {
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900920 mutex_unlock(&nfsd_mutex);
Greg Banks03cf6c92009-01-13 21:26:36 +1100921 return -ENODEV;
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900922 }
923 /* bump up the psudo refcount while traversing */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300924 svc_get(nn->nfsd_serv);
925 ret = svc_pool_stats_open(nn->nfsd_serv, file);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900926 mutex_unlock(&nfsd_mutex);
927 return ret;
928}
929
930int nfsd_pool_stats_release(struct inode *inode, struct file *file)
931{
932 int ret = seq_release(inode, file);
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +0300933 struct net *net = inode->i_sb->s_fs_info;
Stanislav Kinsbursky786185b2012-05-04 12:49:41 +0400934
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900935 mutex_lock(&nfsd_mutex);
936 /* this function really, really should have been called svc_put() */
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400937 nfsd_destroy(net);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +0900938 mutex_unlock(&nfsd_mutex);
939 return ret;
Greg Banks03cf6c92009-01-13 21:26:36 +1100940}