blob: 18d94ea984ba4add43d1af0ca90ed155fc67c700 [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);
Trond Myklebuste333f3b2019-04-09 11:46:19 -040041static __be32 nfsd_acl_init_request(struct svc_rqst *,
42 const struct svc_program *,
43 struct svc_process_info *);
Trond Myklebust029be5d2019-04-09 11:46:18 -040044#endif
45static int nfsd_rpcbind_set(struct net *,
46 const struct svc_program *,
47 u32, int,
48 unsigned short,
49 unsigned short);
Trond Myklebuste333f3b2019-04-09 11:46:19 -040050static __be32 nfsd_init_request(struct svc_rqst *,
51 const struct svc_program *,
52 struct svc_process_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Neil Brownbedbdd82008-06-10 08:40:35 -040054/*
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +030055 * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
Neil Brownbedbdd82008-06-10 08:40:35 -040056 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
57 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
58 *
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +030059 * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
Neil Brownbedbdd82008-06-10 08:40:35 -040060 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
61 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
62 * entry of ->sv_pools[].
63 *
64 * Transitions of the thread count between zero and non-zero are of particular
65 * interest since the svc_serv needs to be created and initialized at that
66 * point, or freed.
Jeff Layton3dd98a32008-06-10 08:40:36 -040067 *
68 * Finally, the nfsd_mutex also protects some of the global variables that are
69 * accessed when nfsd starts and that are settable via the write_* routines in
70 * nfsctl.c. In particular:
71 *
72 * user_recovery_dirname
73 * user_lease_time
74 * nfsd_versions
Neil Brownbedbdd82008-06-10 08:40:35 -040075 */
76DEFINE_MUTEX(nfsd_mutex);
Neil Brownbedbdd82008-06-10 08:40:35 -040077
Andy Adamson4bd9b0f42009-06-24 15:37:45 -040078/*
79 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
80 * nfsd_drc_max_pages limits the total amount of memory available for
81 * version 4.1 DRC caches.
82 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
83 */
84spinlock_t nfsd_drc_lock;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -080085unsigned long nfsd_drc_max_mem;
86unsigned long nfsd_drc_mem_used;
Andy Adamson4bd9b0f42009-06-24 15:37:45 -040087
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080088#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
89static struct svc_stat nfsd_acl_svcstats;
Christoph Hellwige9679182017-05-12 16:21:37 +020090static const struct svc_version *nfsd_acl_version[] = {
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080091 [2] = &nfsd_acl_version2,
92 [3] = &nfsd_acl_version3,
93};
94
95#define NFSD_ACL_MINVERS 2
Tobias Klausere8c96f82006-03-24 03:15:34 -080096#define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
Christoph Hellwige9679182017-05-12 16:21:37 +020097static const struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080098
99static struct svc_program nfsd_acl_program = {
100 .pg_prog = NFS_ACL_PROGRAM,
101 .pg_nvers = NFSD_ACL_NRVERS,
102 .pg_vers = nfsd_acl_versions,
NeilBrown1a8eff62007-01-26 00:56:58 -0800103 .pg_name = "nfsacl",
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800104 .pg_class = "nfsd",
105 .pg_stats = &nfsd_acl_svcstats,
106 .pg_authenticate = &svc_set_client,
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400107 .pg_init_request = nfsd_acl_init_request,
Trond Myklebust029be5d2019-04-09 11:46:18 -0400108 .pg_rpcbind_set = nfsd_acl_rpcbind_set,
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800109};
110
111static struct svc_stat nfsd_acl_svcstats = {
112 .program = &nfsd_acl_program,
113};
114#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
115
Christoph Hellwige9679182017-05-12 16:21:37 +0200116static const struct svc_version *nfsd_version[] = {
NeilBrown70c3b762005-11-07 01:00:25 -0800117 [2] = &nfsd_version2,
118#if defined(CONFIG_NFSD_V3)
119 [3] = &nfsd_version3,
120#endif
121#if defined(CONFIG_NFSD_V4)
122 [4] = &nfsd_version4,
123#endif
124};
125
126#define NFSD_MINVERS 2
Tobias Klausere8c96f82006-03-24 03:15:34 -0800127#define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
NeilBrown70c3b762005-11-07 01:00:25 -0800128
129struct svc_program nfsd_program = {
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800130#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
131 .pg_next = &nfsd_acl_program,
132#endif
NeilBrown70c3b762005-11-07 01:00:25 -0800133 .pg_prog = NFS_PROGRAM, /* program number */
134 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400135 .pg_vers = nfsd_version, /* version table */
NeilBrown70c3b762005-11-07 01:00:25 -0800136 .pg_name = "nfsd", /* program name */
137 .pg_class = "nfsd", /* authentication class */
138 .pg_stats = &nfsd_svcstats, /* version table */
139 .pg_authenticate = &svc_set_client, /* export authentication */
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400140 .pg_init_request = nfsd_init_request,
Trond Myklebust029be5d2019-04-09 11:46:18 -0400141 .pg_rpcbind_set = nfsd_rpcbind_set,
NeilBrown70c3b762005-11-07 01:00:25 -0800142};
143
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400144static bool
145nfsd_support_version(int vers)
146{
147 if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
148 return nfsd_version[vers] != NULL;
149 return false;
150}
Benny Halevy8daf2202009-04-03 08:28:59 +0300151
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400152static bool *
153nfsd_alloc_versions(void)
154{
155 bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
156 unsigned i;
157
158 if (vers) {
159 /* All compiled versions are enabled by default */
160 for (i = 0; i < NFSD_NRVERS; i++)
161 vers[i] = nfsd_support_version(i);
162 }
163 return vers;
164}
165
166static bool *
167nfsd_alloc_minorversions(void)
168{
169 bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
170 sizeof(bool), GFP_KERNEL);
171 unsigned i;
172
173 if (vers) {
174 /* All minor versions are enabled by default */
175 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
176 vers[i] = nfsd_support_version(4);
177 }
178 return vers;
179}
180
181void
182nfsd_netns_free_versions(struct nfsd_net *nn)
183{
184 kfree(nn->nfsd_versions);
185 kfree(nn->nfsd4_minorversions);
186 nn->nfsd_versions = NULL;
187 nn->nfsd4_minorversions = NULL;
188}
189
190static void
191nfsd_netns_init_versions(struct nfsd_net *nn)
192{
193 if (!nn->nfsd_versions) {
194 nn->nfsd_versions = nfsd_alloc_versions();
195 nn->nfsd4_minorversions = nfsd_alloc_minorversions();
196 if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
197 nfsd_netns_free_versions(nn);
198 }
199}
200
201int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
NeilBrown6658d3a2006-10-02 02:17:46 -0700202{
203 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
Pavel Emelyanov15ddb4a2010-05-14 15:33:36 +0400204 return 0;
NeilBrown6658d3a2006-10-02 02:17:46 -0700205 switch(change) {
206 case NFSD_SET:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400207 if (nn->nfsd_versions)
208 nn->nfsd_versions[vers] = nfsd_support_version(vers);
NeilBrown1a8eff62007-01-26 00:56:58 -0800209 break;
NeilBrown6658d3a2006-10-02 02:17:46 -0700210 case NFSD_CLEAR:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400211 nfsd_netns_init_versions(nn);
212 if (nn->nfsd_versions)
213 nn->nfsd_versions[vers] = false;
NeilBrown6658d3a2006-10-02 02:17:46 -0700214 break;
215 case NFSD_TEST:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400216 if (nn->nfsd_versions)
217 return nn->nfsd_versions[vers];
218 /* Fallthrough */
NeilBrown6658d3a2006-10-02 02:17:46 -0700219 case NFSD_AVAIL:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400220 return nfsd_support_version(vers);
NeilBrown6658d3a2006-10-02 02:17:46 -0700221 }
222 return 0;
223}
Benny Halevy8daf2202009-04-03 08:28:59 +0300224
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500225static void
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400226nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500227{
228 unsigned i;
229
230 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400231 if (nn->nfsd4_minorversions[i])
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500232 return;
233 }
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400234 nfsd_vers(nn, 4, NFSD_CLEAR);
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500235}
236
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400237int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
Benny Halevy8daf2202009-04-03 08:28:59 +0300238{
NeilBrown928c6fb2017-03-10 11:36:39 +1100239 if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
240 change != NFSD_AVAIL)
Benny Halevy8daf2202009-04-03 08:28:59 +0300241 return -1;
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400242
Benny Halevy8daf2202009-04-03 08:28:59 +0300243 switch(change) {
244 case NFSD_SET:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400245 if (nn->nfsd4_minorversions) {
246 nfsd_vers(nn, 4, NFSD_SET);
247 nn->nfsd4_minorversions[minorversion] =
248 nfsd_vers(nn, 4, NFSD_TEST);
249 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300250 break;
251 case NFSD_CLEAR:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400252 nfsd_netns_init_versions(nn);
253 if (nn->nfsd4_minorversions) {
254 nn->nfsd4_minorversions[minorversion] = false;
255 nfsd_adjust_nfsd_versions4(nn);
256 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300257 break;
258 case NFSD_TEST:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400259 if (nn->nfsd4_minorversions)
260 return nn->nfsd4_minorversions[minorversion];
261 return nfsd_vers(nn, 4, NFSD_TEST);
Benny Halevy8daf2202009-04-03 08:28:59 +0300262 case NFSD_AVAIL:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400263 return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
264 nfsd_vers(nn, 4, NFSD_AVAIL);
Benny Halevy8daf2202009-04-03 08:28:59 +0300265 }
266 return 0;
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
270 * Maximum number of nfsd processes
271 */
272#define NFSD_MAXSERVS 8192
273
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300274int nfsd_nrthreads(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Neil Brownc7d106c2008-06-12 13:38:42 +1000276 int rv = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300277 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
278
Neil Brownc7d106c2008-06-12 13:38:42 +1000279 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300280 if (nn->nfsd_serv)
281 rv = nn->nfsd_serv->sv_nrthreads;
Neil Brownc7d106c2008-06-12 13:38:42 +1000282 mutex_unlock(&nfsd_mutex);
283 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Trond Myklebust4df493a2019-04-09 12:13:37 -0400286static int nfsd_init_socks(struct net *net, const struct cred *cred)
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400287{
288 int error;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300289 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
290
291 if (!list_empty(&nn->nfsd_serv->sv_permsocks))
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400292 return 0;
293
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300294 error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
Trond Myklebust4df493a2019-04-09 12:13:37 -0400295 SVC_SOCK_DEFAULTS, cred);
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400296 if (error < 0)
297 return error;
298
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300299 error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
Trond Myklebust4df493a2019-04-09 12:13:37 -0400300 SVC_SOCK_DEFAULTS, cred);
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400301 if (error < 0)
302 return error;
303
304 return 0;
305}
306
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300307static int nfsd_users = 0;
Jeff Layton4ad9a342010-07-19 16:50:04 -0400308
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300309static int nfsd_startup_generic(int nrservs)
310{
311 int ret;
312
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300313 if (nfsd_users++)
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300314 return 0;
315
316 /*
317 * Readahead param cache - will no-op if it already exists.
318 * (Note therefore results will be suboptimal if number of
319 * threads is modified after nfsd start.)
320 */
321 ret = nfsd_racache_init(2*nrservs);
322 if (ret)
Kinglong Meed9499a92014-07-30 21:26:05 +0800323 goto dec_users;
324
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300325 ret = nfs4_state_start();
326 if (ret)
327 goto out_racache;
328 return 0;
329
330out_racache:
331 nfsd_racache_shutdown();
Kinglong Meed9499a92014-07-30 21:26:05 +0800332dec_users:
333 nfsd_users--;
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300334 return ret;
335}
336
337static void nfsd_shutdown_generic(void)
338{
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300339 if (--nfsd_users)
340 return;
341
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300342 nfs4_state_shutdown();
343 nfsd_racache_shutdown();
344}
345
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400346static bool nfsd_needs_lockd(struct nfsd_net *nn)
Kinglong Mee8ef66712013-12-31 13:17:30 +0800347{
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400348 return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
Kinglong Mee8ef66712013-12-31 13:17:30 +0800349}
350
Trond Myklebust4df493a2019-04-09 12:13:37 -0400351static int nfsd_startup_net(int nrservs, struct net *net, const struct cred *cred)
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300352{
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300353 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300354 int ret;
355
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300356 if (nn->nfsd_net_up)
357 return 0;
358
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300359 ret = nfsd_startup_generic(nrservs);
360 if (ret)
361 return ret;
Trond Myklebust4df493a2019-04-09 12:13:37 -0400362 ret = nfsd_init_socks(net, cred);
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300363 if (ret)
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300364 goto out_socks;
Kinglong Mee8ef66712013-12-31 13:17:30 +0800365
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400366 if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
Trond Myklebust40373b12019-04-09 12:13:39 -0400367 ret = lockd_up(net, cred);
Kinglong Mee8ef66712013-12-31 13:17:30 +0800368 if (ret)
369 goto out_socks;
370 nn->lockd_up = 1;
371 }
372
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300373 ret = nfs4_state_start_net(net);
374 if (ret)
375 goto out_lockd;
376
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300377 nn->nfsd_net_up = true;
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300378 return 0;
379
380out_lockd:
Kinglong Mee8ef66712013-12-31 13:17:30 +0800381 if (nn->lockd_up) {
382 lockd_down(net);
383 nn->lockd_up = 0;
384 }
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300385out_socks:
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300386 nfsd_shutdown_generic();
Jeff Layton4ad9a342010-07-19 16:50:04 -0400387 return ret;
388}
389
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300390static void nfsd_shutdown_net(struct net *net)
391{
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300392 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
393
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300394 nfs4_state_shutdown_net(net);
Kinglong Mee8ef66712013-12-31 13:17:30 +0800395 if (nn->lockd_up) {
396 lockd_down(net);
397 nn->lockd_up = 0;
398 }
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300399 nn->nfsd_net_up = false;
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300400 nfsd_shutdown_generic();
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300401}
402
Scott Mayhew36684992015-12-11 16:45:59 -0500403static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
404 void *ptr)
405{
406 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
407 struct net_device *dev = ifa->ifa_dev->dev;
408 struct net *net = dev_net(dev);
409 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
410 struct sockaddr_in sin;
411
Vasily Averin2317dc52017-11-10 10:19:35 +0300412 if ((event != NETDEV_DOWN) ||
413 !atomic_inc_not_zero(&nn->ntf_refcnt))
Scott Mayhew36684992015-12-11 16:45:59 -0500414 goto out;
415
416 if (nn->nfsd_serv) {
417 dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
418 sin.sin_family = AF_INET;
419 sin.sin_addr.s_addr = ifa->ifa_local;
420 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
421 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300422 atomic_dec(&nn->ntf_refcnt);
423 wake_up(&nn->ntf_wq);
Scott Mayhew36684992015-12-11 16:45:59 -0500424
425out:
426 return NOTIFY_DONE;
427}
428
429static struct notifier_block nfsd_inetaddr_notifier = {
430 .notifier_call = nfsd_inetaddr_event,
431};
432
433#if IS_ENABLED(CONFIG_IPV6)
434static int nfsd_inet6addr_event(struct notifier_block *this,
435 unsigned long event, void *ptr)
436{
437 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
438 struct net_device *dev = ifa->idev->dev;
439 struct net *net = dev_net(dev);
440 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
441 struct sockaddr_in6 sin6;
442
Vasily Averin2317dc52017-11-10 10:19:35 +0300443 if ((event != NETDEV_DOWN) ||
444 !atomic_inc_not_zero(&nn->ntf_refcnt))
Scott Mayhew36684992015-12-11 16:45:59 -0500445 goto out;
446
447 if (nn->nfsd_serv) {
448 dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
449 sin6.sin6_family = AF_INET6;
450 sin6.sin6_addr = ifa->addr;
Scott Mayhew7b198242017-01-05 16:34:49 -0500451 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
452 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
Scott Mayhew36684992015-12-11 16:45:59 -0500453 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
454 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300455 atomic_dec(&nn->ntf_refcnt);
456 wake_up(&nn->ntf_wq);
Scott Mayhew36684992015-12-11 16:45:59 -0500457out:
458 return NOTIFY_DONE;
459}
460
461static struct notifier_block nfsd_inet6addr_notifier = {
462 .notifier_call = nfsd_inet6addr_event,
463};
464#endif
465
Vasily Averin1eca45f2016-09-21 15:33:05 +0300466/* Only used under nfsd_mutex, so this atomic may be overkill: */
467static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
468
Stanislav Kinsbursky541e8642012-12-06 14:23:44 +0300469static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
Jeff Layton4ad9a342010-07-19 16:50:04 -0400470{
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300471 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
472
Vasily Averin2317dc52017-11-10 10:19:35 +0300473 atomic_dec(&nn->ntf_refcnt);
Vasily Averin1eca45f2016-09-21 15:33:05 +0300474 /* check if the notifier still has clients */
475 if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
476 unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500477#if IS_ENABLED(CONFIG_IPV6)
Vasily Averin1eca45f2016-09-21 15:33:05 +0300478 unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500479#endif
Vasily Averin1eca45f2016-09-21 15:33:05 +0300480 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300481 wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
Vasily Averin1eca45f2016-09-21 15:33:05 +0300482
Jeff Layton4ad9a342010-07-19 16:50:04 -0400483 /*
484 * write_ports can create the server without actually starting
485 * any threads--if we get shut down before any threads are
486 * started, then nfsd_last_thread will be run before any of this
Kinglong Mee691412b2016-01-04 11:15:21 +0800487 * other initialization has been done except the rpcb information.
Jeff Layton4ad9a342010-07-19 16:50:04 -0400488 */
Kinglong Mee691412b2016-01-04 11:15:21 +0800489 svc_rpcb_cleanup(serv, net);
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300490 if (!nn->nfsd_net_up)
Jeff Layton4ad9a342010-07-19 16:50:04 -0400491 return;
Kinglong Mee691412b2016-01-04 11:15:21 +0800492
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300493 nfsd_shutdown_net(net);
Jeff Laytone096bbc2008-06-10 08:40:37 -0400494 printk(KERN_WARNING "nfsd: last server has exited, flushing export "
495 "cache\n");
Stanislav Kinsburskyb3853e02012-04-11 15:13:21 +0400496 nfsd_export_flush(net);
NeilBrownbc591cc2006-10-02 02:17:44 -0700497}
NeilBrown6658d3a2006-10-02 02:17:46 -0700498
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400499void nfsd_reset_versions(struct nfsd_net *nn)
NeilBrown6658d3a2006-10-02 02:17:46 -0700500{
NeilBrown6658d3a2006-10-02 02:17:46 -0700501 int i;
502
NeilBrown800a9382017-03-10 11:36:39 +1100503 for (i = 0; i < NFSD_NRVERS; i++)
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400504 if (nfsd_vers(nn, i, NFSD_TEST))
NeilBrown800a9382017-03-10 11:36:39 +1100505 return;
NeilBrown6658d3a2006-10-02 02:17:46 -0700506
NeilBrown800a9382017-03-10 11:36:39 +1100507 for (i = 0; i < NFSD_NRVERS; i++)
508 if (i != 4)
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400509 nfsd_vers(nn, i, NFSD_SET);
NeilBrown800a9382017-03-10 11:36:39 +1100510 else {
511 int minor = 0;
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400512 while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
NeilBrown800a9382017-03-10 11:36:39 +1100513 minor++;
514 }
NeilBrown6658d3a2006-10-02 02:17:46 -0700515}
516
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300517/*
518 * Each session guarantees a negotiated per slot memory cache for replies
519 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
520 * NFSv4.1 server might want to use more memory for a DRC than a machine
521 * with mutiple services.
522 *
523 * Impose a hard limit on the number of pages for the DRC which varies
524 * according to the machines free pages. This is of course only a default.
525 *
526 * For now this is a #defined shift which could be under admin control
527 * in the future.
528 */
529static void set_max_drc(void)
530{
J. Bruce Fields44d86602017-09-19 20:51:31 -0400531 #define NFSD_DRC_SIZE_SHIFT 7
Andy Adamson0c193052009-07-27 19:09:19 -0400532 nfsd_drc_max_mem = (nr_free_buffer_pages()
533 >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
534 nfsd_drc_mem_used = 0;
Andy Adamson4bd9b0f42009-06-24 15:37:45 -0400535 spin_lock_init(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800536 dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300537}
Neil Brownbedbdd82008-06-10 08:40:35 -0400538
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500539static int nfsd_get_default_max_blksize(void)
540{
541 struct sysinfo i;
542 unsigned long long target;
543 unsigned long ret;
544
545 si_meminfo(&i);
J. Bruce Fields508f9222012-01-30 16:21:11 -0500546 target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500547 /*
548 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
549 * machines, but only uses 32K on 128M machines. Bottom out at
550 * 8K on 32M and smaller. Of course, this is only a default.
551 */
552 target >>= 12;
553
554 ret = NFSSVC_MAXBLKSIZE;
555 while (ret > target && ret >= 8*1024*2)
556 ret /= 2;
557 return ret;
558}
559
Chuck Leverafea5652017-08-01 12:00:06 -0400560static const struct svc_serv_ops nfsd_thread_sv_ops = {
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700561 .svo_shutdown = nfsd_last_thread,
562 .svo_function = nfsd,
563 .svo_enqueue_xprt = svc_xprt_do_enqueue,
Jeff Layton598e2352015-06-08 12:08:33 -0700564 .svo_setup = svc_set_num_threads,
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700565 .svo_module = THIS_MODULE,
Jeff Laytonea126e72015-06-08 12:03:32 -0700566};
567
Stanislav Kinsbursky67774362012-12-10 12:19:20 +0300568int nfsd_create_serv(struct net *net)
NeilBrown02a375f2006-10-02 02:17:46 -0700569{
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400570 int error;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300571 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400572
Neil Brownbedbdd82008-06-10 08:40:35 -0400573 WARN_ON(!mutex_is_locked(&nfsd_mutex));
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300574 if (nn->nfsd_serv) {
575 svc_get(nn->nfsd_serv);
NeilBrown02a375f2006-10-02 02:17:46 -0700576 return 0;
577 }
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500578 if (nfsd_max_blksize == 0)
579 nfsd_max_blksize = nfsd_get_default_max_blksize();
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400580 nfsd_reset_versions(nn);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300581 nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700582 &nfsd_thread_sv_ops);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300583 if (nn->nfsd_serv == NULL)
Jeff Layton628b3682010-07-21 16:40:08 -0400584 return -ENOMEM;
Neil Brownbedbdd82008-06-10 08:40:35 -0400585
Jeff Layton5b8db002014-07-02 16:11:22 -0400586 nn->nfsd_serv->sv_maxconn = nn->max_connections;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300587 error = svc_bind(nn->nfsd_serv, net);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400588 if (error < 0) {
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300589 svc_destroy(nn->nfsd_serv);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400590 return error;
591 }
592
Jeff Layton628b3682010-07-21 16:40:08 -0400593 set_max_drc();
Vasily Averin1eca45f2016-09-21 15:33:05 +0300594 /* check if the notifier is already set */
595 if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
596 register_inetaddr_notifier(&nfsd_inetaddr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500597#if IS_ENABLED(CONFIG_IPV6)
Vasily Averin1eca45f2016-09-21 15:33:05 +0300598 register_inet6addr_notifier(&nfsd_inet6addr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500599#endif
Vasily Averin1eca45f2016-09-21 15:33:05 +0300600 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300601 atomic_inc(&nn->ntf_refcnt);
Arnd Bergmann256a89f2017-10-19 12:04:11 +0200602 ktime_get_real_ts64(&nn->nfssvc_boot); /* record boot time */
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500603 return 0;
NeilBrown02a375f2006-10-02 02:17:46 -0700604}
605
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300606int nfsd_nrpools(struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700607{
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300608 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
609
610 if (nn->nfsd_serv == NULL)
Greg Bankseed29652006-10-02 02:18:02 -0700611 return 0;
612 else
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300613 return nn->nfsd_serv->sv_nrpools;
Greg Bankseed29652006-10-02 02:18:02 -0700614}
615
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300616int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700617{
618 int i = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300619 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Greg Bankseed29652006-10-02 02:18:02 -0700620
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300621 if (nn->nfsd_serv != NULL) {
622 for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
623 nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
Greg Bankseed29652006-10-02 02:18:02 -0700624 }
625
626 return 0;
627}
628
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300629void nfsd_destroy(struct net *net)
630{
631 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
632 int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
633
634 if (destroy)
635 svc_shutdown_net(nn->nfsd_serv, net);
636 svc_destroy(nn->nfsd_serv);
637 if (destroy)
638 nn->nfsd_serv = NULL;
639}
640
Stanislav Kinsbursky3938a0d2012-12-10 12:19:30 +0300641int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700642{
643 int i = 0;
644 int tot = 0;
645 int err = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300646 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Greg Bankseed29652006-10-02 02:18:02 -0700647
Neil Brownbedbdd82008-06-10 08:40:35 -0400648 WARN_ON(!mutex_is_locked(&nfsd_mutex));
649
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300650 if (nn->nfsd_serv == NULL || n <= 0)
Greg Bankseed29652006-10-02 02:18:02 -0700651 return 0;
652
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300653 if (n > nn->nfsd_serv->sv_nrpools)
654 n = nn->nfsd_serv->sv_nrpools;
Greg Bankseed29652006-10-02 02:18:02 -0700655
656 /* enforce a global maximum number of threads */
657 tot = 0;
658 for (i = 0; i < n; i++) {
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800659 nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
Greg Bankseed29652006-10-02 02:18:02 -0700660 tot += nthreads[i];
661 }
662 if (tot > NFSD_MAXSERVS) {
663 /* total too large: scale down requested numbers */
664 for (i = 0; i < n && tot > 0; i++) {
665 int new = nthreads[i] * NFSD_MAXSERVS / tot;
666 tot -= (nthreads[i] - new);
667 nthreads[i] = new;
668 }
669 for (i = 0; i < n && tot > 0; i++) {
670 nthreads[i]--;
671 tot--;
672 }
673 }
674
675 /*
676 * There must always be a thread in pool 0; the admin
677 * can't shut down NFS completely using pool_threads.
678 */
679 if (nthreads[0] == 0)
680 nthreads[0] = 1;
681
682 /* apply the new numbers */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300683 svc_get(nn->nfsd_serv);
Greg Bankseed29652006-10-02 02:18:02 -0700684 for (i = 0; i < n; i++) {
Jeff Layton598e2352015-06-08 12:08:33 -0700685 err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
686 &nn->nfsd_serv->sv_pools[i], nthreads[i]);
Greg Bankseed29652006-10-02 02:18:02 -0700687 if (err)
688 break;
689 }
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400690 nfsd_destroy(net);
Greg Bankseed29652006-10-02 02:18:02 -0700691 return err;
692}
693
Jeff Laytonac77efb2010-07-20 14:10:22 -0400694/*
695 * Adjust the number of threads and return the new number of threads.
696 * This is also the function that starts the server if necessary, if
697 * this is the first time nrservs is nonzero.
698 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699int
Trond Myklebust4df493a2019-04-09 12:13:37 -0400700nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 int error;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400703 bool nfsd_up_before;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300704 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Neil Brownbedbdd82008-06-10 08:40:35 -0400705
706 mutex_lock(&nfsd_mutex);
NeilBrown6658d3a2006-10-02 02:17:46 -0700707 dprintk("nfsd: creating service\n");
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800708
709 nrservs = max(nrservs, 0);
710 nrservs = min(nrservs, NFSD_MAXSERVS);
NeilBrown671e1fc2009-06-16 11:03:20 +1000711 error = 0;
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800712
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300713 if (nrservs == 0 && nn->nfsd_serv == NULL)
NeilBrown671e1fc2009-06-16 11:03:20 +1000714 goto out;
715
Stanislav Kinsbursky67774362012-12-10 12:19:20 +0300716 error = nfsd_create_serv(net);
NeilBrown02a375f2006-10-02 02:17:46 -0700717 if (error)
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400718 goto out;
719
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300720 nfsd_up_before = nn->nfsd_net_up;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400721
Trond Myklebust4df493a2019-04-09 12:13:37 -0400722 error = nfsd_startup_net(nrservs, net, cred);
J. Bruce Fieldsaf4718f2010-07-21 18:31:42 -0400723 if (error)
724 goto out_destroy;
Jeff Layton598e2352015-06-08 12:08:33 -0700725 error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
726 NULL, nrservs);
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400727 if (error)
728 goto out_shutdown;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300729 /* We are holding a reference to nn->nfsd_serv which
J. Bruce Fieldsaf4718f2010-07-21 18:31:42 -0400730 * we don't want to count in the return value,
731 * so subtract 1
732 */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300733 error = nn->nfsd_serv->sv_nrthreads - 1;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400734out_shutdown:
735 if (error < 0 && !nfsd_up_before)
Stanislav Kinsbursky541e8642012-12-06 14:23:44 +0300736 nfsd_shutdown_net(net);
Jeff Layton4ad9a342010-07-19 16:50:04 -0400737out_destroy:
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400738 nfsd_destroy(net); /* Release server */
Jeff Layton4ad9a342010-07-19 16:50:04 -0400739out:
Neil Brownbedbdd82008-06-10 08:40:35 -0400740 mutex_unlock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return error;
742}
743
Trond Myklebust029be5d2019-04-09 11:46:18 -0400744#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
745static bool
746nfsd_support_acl_version(int vers)
747{
748 if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
749 return nfsd_acl_version[vers] != NULL;
750 return false;
751}
752
753static int
754nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
755 u32 version, int family, unsigned short proto,
756 unsigned short port)
757{
758 if (!nfsd_support_acl_version(version) ||
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400759 !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
Trond Myklebust029be5d2019-04-09 11:46:18 -0400760 return 0;
761 return svc_generic_rpcbind_set(net, progp, version, family,
762 proto, port);
763}
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400764
765static __be32
766nfsd_acl_init_request(struct svc_rqst *rqstp,
767 const struct svc_program *progp,
768 struct svc_process_info *ret)
769{
770 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
771 int i;
772
773 if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
774 nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
775 return svc_generic_init_request(rqstp, progp, ret);
776
777 ret->mismatch.lovers = NFSD_ACL_NRVERS;
778 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
779 if (nfsd_support_acl_version(rqstp->rq_vers) &&
780 nfsd_vers(nn, i, NFSD_TEST)) {
781 ret->mismatch.lovers = i;
782 break;
783 }
784 }
785 if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
786 return rpc_prog_unavail;
787 ret->mismatch.hivers = NFSD_ACL_MINVERS;
788 for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
789 if (nfsd_support_acl_version(rqstp->rq_vers) &&
790 nfsd_vers(nn, i, NFSD_TEST)) {
791 ret->mismatch.hivers = i;
792 break;
793 }
794 }
795 return rpc_prog_mismatch;
796}
Trond Myklebust029be5d2019-04-09 11:46:18 -0400797#endif
798
799static int
800nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
801 u32 version, int family, unsigned short proto,
802 unsigned short port)
803{
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400804 if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
Trond Myklebust029be5d2019-04-09 11:46:18 -0400805 return 0;
806 return svc_generic_rpcbind_set(net, progp, version, family,
807 proto, port);
808}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400810static __be32
811nfsd_init_request(struct svc_rqst *rqstp,
812 const struct svc_program *progp,
813 struct svc_process_info *ret)
814{
815 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
816 int i;
817
818 if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
819 return svc_generic_init_request(rqstp, progp, ret);
820
821 ret->mismatch.lovers = NFSD_NRVERS;
822 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
823 if (nfsd_vers(nn, i, NFSD_TEST)) {
824 ret->mismatch.lovers = i;
825 break;
826 }
827 }
828 if (ret->mismatch.lovers == NFSD_NRVERS)
829 return rpc_prog_unavail;
830 ret->mismatch.hivers = NFSD_MINVERS;
831 for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
832 if (nfsd_vers(nn, i, NFSD_TEST)) {
833 ret->mismatch.hivers = i;
834 break;
835 }
836 }
837 return rpc_prog_mismatch;
838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/*
841 * This is the NFS server kernel thread
842 */
Jeff Layton9867d762008-06-10 08:40:38 -0400843static int
844nfsd(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Jeff Layton9867d762008-06-10 08:40:38 -0400846 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
Stanislav Kinsbursky88c47662012-12-06 18:34:42 +0300847 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
848 struct net *net = perm_sock->xpt_net;
Jeff Layton5b8db002014-07-02 16:11:22 -0400849 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields5b444cc2012-08-17 21:47:53 -0400850 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* Lock module and set up kernel thread */
Neil Brownbedbdd82008-06-10 08:40:35 -0400853 mutex_lock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Jeff Layton9867d762008-06-10 08:40:38 -0400855 /* At this point, the thread shares current->fs
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100856 * with the init process. We need to create files with the
857 * umask as defined by the client instead of init's umask. */
Al Viro3e93cd62009-03-29 19:00:13 -0400858 if (unshare_fs_struct() < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 printk("Unable to start nfsd thread: out of memory\n");
860 goto out;
861 }
Al Viro3e93cd62009-03-29 19:00:13 -0400862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 current->fs->umask = 0;
864
Jeff Layton9867d762008-06-10 08:40:38 -0400865 /*
866 * thread is spawned with all signals set to SIG_IGN, re-enable
Jeff Layton100766f2008-06-30 14:09:46 -0400867 * the ones that will bring down the thread
Jeff Layton9867d762008-06-10 08:40:38 -0400868 */
Jeff Layton100766f2008-06-30 14:09:46 -0400869 allow_signal(SIGKILL);
870 allow_signal(SIGHUP);
871 allow_signal(SIGINT);
872 allow_signal(SIGQUIT);
Neil Brownbedbdd82008-06-10 08:40:35 -0400873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 nfsdstats.th_cnt++;
Neil Brownbedbdd82008-06-10 08:40:35 -0400875 mutex_unlock(&nfsd_mutex);
876
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700877 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /*
880 * The main request loop
881 */
882 for (;;) {
Jeff Layton5b8db002014-07-02 16:11:22 -0400883 /* Update sv_maxconn if it has changed */
884 rqstp->rq_server->sv_maxconn = nn->max_connections;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Find a socket with data available and call its
888 * recvfrom routine.
889 */
NeilBrown6fb2b472006-10-02 02:17:50 -0700890 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 ;
Jeff Layton9867d762008-06-10 08:40:38 -0400892 if (err == -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
David Howellse0e81732009-09-02 09:13:40 +0100894 validate_process_creds();
NeilBrown6fb2b472006-10-02 02:17:50 -0700895 svc_process(rqstp);
David Howellse0e81732009-09-02 09:13:40 +0100896 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
NeilBrown24e36662006-10-02 02:17:45 -0700899 /* Clear signals before calling svc_exit_thread() */
NeilBrown9e4160522005-04-16 15:26:37 -0700900 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Neil Brownbedbdd82008-06-10 08:40:35 -0400902 mutex_lock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 nfsdstats.th_cnt --;
904
905out:
Stanislav Kinsbursky57c8b13e2012-07-03 16:46:41 +0400906 rqstp->rq_server = NULL;
Stanislav Kinsbursky786185b2012-05-04 12:49:41 +0400907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* Release the thread */
909 svc_exit_thread(rqstp);
910
Stanislav Kinsbursky88c47662012-12-06 18:34:42 +0300911 nfsd_destroy(net);
Stanislav Kinsbursky57c8b13e2012-07-03 16:46:41 +0400912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* Release module */
Neil Brownbedbdd82008-06-10 08:40:35 -0400914 mutex_unlock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 module_put_and_exit(0);
Jeff Layton9867d762008-06-10 08:40:38 -0400916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Andy Adamson32c1eb02007-07-17 04:04:48 -0700919static __be32 map_new_errors(u32 vers, __be32 nfserr)
920{
921 if (nfserr == nfserr_jukebox && vers == 2)
922 return nfserr_dropit;
923 if (nfserr == nfserr_wrongsec && vers < 4)
924 return nfserr_acces;
925 return nfserr;
926}
927
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400928/*
929 * A write procedure can have a large argument, and a read procedure can
930 * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
931 * reply that can both be larger than a page. The xdr code has taken
932 * advantage of this assumption to be a sloppy about bounds checking in
933 * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that
934 * problem, we enforce these assumptions here:
935 */
936static bool nfs_request_too_big(struct svc_rqst *rqstp,
Christoph Hellwig860bda22017-05-12 16:11:49 +0200937 const struct svc_procedure *proc)
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400938{
939 /*
940 * The ACL code has more careful bounds-checking and is not
941 * susceptible to this problem:
942 */
943 if (rqstp->rq_prog != NFS_PROGRAM)
944 return false;
945 /*
946 * Ditto NFSv4 (which can in theory have argument and reply both
947 * more than a page):
948 */
949 if (rqstp->rq_vers >= 4)
950 return false;
951 /* The reply will be small, we're OK: */
952 if (proc->pc_xdrressize > 0 &&
953 proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
954 return false;
955
956 return rqstp->rq_arg.len > PAGE_SIZE;
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959int
Al Viroc7afef12006-10-19 23:29:02 -0700960nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Christoph Hellwig860bda22017-05-12 16:11:49 +0200962 const struct svc_procedure *proc;
Al Viroad451d32006-10-19 23:28:55 -0700963 __be32 nfserr;
964 __be32 *nfserrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 dprintk("nfsd_dispatch: vers %d proc %d\n",
967 rqstp->rq_vers, rqstp->rq_proc);
968 proc = rqstp->rq_procinfo;
969
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400970 if (nfs_request_too_big(rqstp, proc)) {
971 dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
972 *statp = rpc_garbage_args;
973 return 1;
974 }
J. Bruce Fields10910062011-01-24 12:11:02 -0500975 /*
976 * Give the xdr decoder a chance to change this if it wants
977 * (necessary in the NFSv4.0 compound case)
978 */
979 rqstp->rq_cachetype = proc->pc_cachetype;
980 /* Decode arguments */
Christoph Hellwig026fec72017-05-08 19:01:48 +0200981 if (proc->pc_decode &&
982 !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) {
J. Bruce Fields10910062011-01-24 12:11:02 -0500983 dprintk("nfsd: failed to decode arguments!\n");
984 *statp = rpc_garbage_args;
985 return 1;
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* Check whether we have this call in the cache. */
J. Bruce Fields10910062011-01-24 12:11:02 -0500989 switch (nfsd_cache_lookup(rqstp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 case RC_DROPIT:
991 return 0;
992 case RC_REPLY:
993 return 1;
994 case RC_DOIT:;
995 /* do it */
996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* need to grab the location to store the status, as
999 * nfsv4 does some encoding while processing
1000 */
1001 nfserrp = rqstp->rq_res.head[0].iov_base
1002 + rqstp->rq_res.head[0].iov_len;
Al Viroad451d32006-10-19 23:28:55 -07001003 rqstp->rq_res.head[0].iov_len += sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 /* Now call the procedure handler, and encode NFS status. */
Christoph Hellwiga6beb732017-05-08 17:35:49 +02001006 nfserr = proc->pc_func(rqstp);
Andy Adamson32c1eb02007-07-17 04:04:48 -07001007 nfserr = map_new_errors(rqstp->rq_vers, nfserr);
Jeff Layton78b65eb2014-11-19 07:51:17 -05001008 if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) {
J. Bruce Fields45457e02007-06-22 17:26:32 -04001009 dprintk("nfsd: Dropping request; may be revisited later\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1011 return 0;
1012 }
1013
1014 if (rqstp->rq_proc != 0)
1015 *nfserrp++ = nfserr;
1016
1017 /* Encode result.
1018 * For NFSv2, additional info is never returned in case of an error.
1019 */
1020 if (!(nfserr && rqstp->rq_vers == 2)) {
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001021 if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /* Failed to encode result. Release cache entry */
1023 dprintk("nfsd: failed to encode result!\n");
1024 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1025 *statp = rpc_system_err;
1026 return 1;
1027 }
1028 }
1029
1030 /* Store reply in cache. */
J. Bruce Fields57d276d2012-11-16 15:22:43 -05001031 nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 1;
1033}
Greg Banks03cf6c92009-01-13 21:26:36 +11001034
1035int nfsd_pool_stats_open(struct inode *inode, struct file *file)
1036{
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001037 int ret;
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +03001038 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001039
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001040 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001041 if (nn->nfsd_serv == NULL) {
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001042 mutex_unlock(&nfsd_mutex);
Greg Banks03cf6c92009-01-13 21:26:36 +11001043 return -ENODEV;
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001044 }
1045 /* bump up the psudo refcount while traversing */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001046 svc_get(nn->nfsd_serv);
1047 ret = svc_pool_stats_open(nn->nfsd_serv, file);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001048 mutex_unlock(&nfsd_mutex);
1049 return ret;
1050}
1051
1052int nfsd_pool_stats_release(struct inode *inode, struct file *file)
1053{
1054 int ret = seq_release(inode, file);
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +03001055 struct net *net = inode->i_sb->s_fs_info;
Stanislav Kinsbursky786185b2012-05-04 12:49:41 +04001056
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001057 mutex_lock(&nfsd_mutex);
1058 /* this function really, really should have been called svc_put() */
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +04001059 nfsd_destroy(net);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001060 mutex_unlock(&nfsd_mutex);
1061 return ret;
Greg Banks03cf6c92009-01-13 21:26:36 +11001062}