blob: fdf7ed4bd5dd22e6014c8bf6d15e8326808fe87d [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"
Jeff Layton65294c12019-08-18 14:18:48 -040030#include "filecache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define NFSDDBG_FACILITY NFSDDBG_SVC
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034extern struct svc_program nfsd_program;
Jeff Layton9867d762008-06-10 08:40:38 -040035static int nfsd(void *vrqstp);
Trond Myklebust029be5d2019-04-09 11:46:18 -040036#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
37static int nfsd_acl_rpcbind_set(struct net *,
38 const struct svc_program *,
39 u32, int,
40 unsigned short,
41 unsigned short);
Trond Myklebuste333f3b2019-04-09 11:46:19 -040042static __be32 nfsd_acl_init_request(struct svc_rqst *,
43 const struct svc_program *,
44 struct svc_process_info *);
Trond Myklebust029be5d2019-04-09 11:46:18 -040045#endif
46static int nfsd_rpcbind_set(struct net *,
47 const struct svc_program *,
48 u32, int,
49 unsigned short,
50 unsigned short);
Trond Myklebuste333f3b2019-04-09 11:46:19 -040051static __be32 nfsd_init_request(struct svc_rqst *,
52 const struct svc_program *,
53 struct svc_process_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Neil Brownbedbdd82008-06-10 08:40:35 -040055/*
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +030056 * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
Neil Brownbedbdd82008-06-10 08:40:35 -040057 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
58 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
59 *
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +030060 * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
Neil Brownbedbdd82008-06-10 08:40:35 -040061 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
62 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
63 * entry of ->sv_pools[].
64 *
65 * Transitions of the thread count between zero and non-zero are of particular
66 * interest since the svc_serv needs to be created and initialized at that
67 * point, or freed.
Jeff Layton3dd98a32008-06-10 08:40:36 -040068 *
69 * Finally, the nfsd_mutex also protects some of the global variables that are
70 * accessed when nfsd starts and that are settable via the write_* routines in
71 * nfsctl.c. In particular:
72 *
73 * user_recovery_dirname
74 * user_lease_time
75 * nfsd_versions
Neil Brownbedbdd82008-06-10 08:40:35 -040076 */
77DEFINE_MUTEX(nfsd_mutex);
Neil Brownbedbdd82008-06-10 08:40:35 -040078
Andy Adamson4bd9b0f42009-06-24 15:37:45 -040079/*
80 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
81 * nfsd_drc_max_pages limits the total amount of memory available for
82 * version 4.1 DRC caches.
83 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
84 */
85spinlock_t nfsd_drc_lock;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -080086unsigned long nfsd_drc_max_mem;
87unsigned long nfsd_drc_mem_used;
Andy Adamson4bd9b0f42009-06-24 15:37:45 -040088
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080089#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
90static struct svc_stat nfsd_acl_svcstats;
Christoph Hellwige9679182017-05-12 16:21:37 +020091static const struct svc_version *nfsd_acl_version[] = {
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080092 [2] = &nfsd_acl_version2,
93 [3] = &nfsd_acl_version3,
94};
95
96#define NFSD_ACL_MINVERS 2
Tobias Klausere8c96f82006-03-24 03:15:34 -080097#define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
Christoph Hellwige9679182017-05-12 16:21:37 +020098static const struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -080099
100static struct svc_program nfsd_acl_program = {
101 .pg_prog = NFS_ACL_PROGRAM,
102 .pg_nvers = NFSD_ACL_NRVERS,
103 .pg_vers = nfsd_acl_versions,
NeilBrown1a8eff62007-01-26 00:56:58 -0800104 .pg_name = "nfsacl",
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800105 .pg_class = "nfsd",
106 .pg_stats = &nfsd_acl_svcstats,
107 .pg_authenticate = &svc_set_client,
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400108 .pg_init_request = nfsd_acl_init_request,
Trond Myklebust029be5d2019-04-09 11:46:18 -0400109 .pg_rpcbind_set = nfsd_acl_rpcbind_set,
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800110};
111
112static struct svc_stat nfsd_acl_svcstats = {
113 .program = &nfsd_acl_program,
114};
115#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
116
Christoph Hellwige9679182017-05-12 16:21:37 +0200117static const struct svc_version *nfsd_version[] = {
NeilBrown70c3b762005-11-07 01:00:25 -0800118 [2] = &nfsd_version2,
119#if defined(CONFIG_NFSD_V3)
120 [3] = &nfsd_version3,
121#endif
122#if defined(CONFIG_NFSD_V4)
123 [4] = &nfsd_version4,
124#endif
125};
126
127#define NFSD_MINVERS 2
Tobias Klausere8c96f82006-03-24 03:15:34 -0800128#define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
NeilBrown70c3b762005-11-07 01:00:25 -0800129
130struct svc_program nfsd_program = {
Andreas Gruenbacher3fb803a2006-02-01 03:04:34 -0800131#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
132 .pg_next = &nfsd_acl_program,
133#endif
NeilBrown70c3b762005-11-07 01:00:25 -0800134 .pg_prog = NFS_PROGRAM, /* program number */
135 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400136 .pg_vers = nfsd_version, /* version table */
NeilBrown70c3b762005-11-07 01:00:25 -0800137 .pg_name = "nfsd", /* program name */
138 .pg_class = "nfsd", /* authentication class */
139 .pg_stats = &nfsd_svcstats, /* version table */
140 .pg_authenticate = &svc_set_client, /* export authentication */
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400141 .pg_init_request = nfsd_init_request,
Trond Myklebust029be5d2019-04-09 11:46:18 -0400142 .pg_rpcbind_set = nfsd_rpcbind_set,
NeilBrown70c3b762005-11-07 01:00:25 -0800143};
144
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400145static bool
146nfsd_support_version(int vers)
147{
148 if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
149 return nfsd_version[vers] != NULL;
150 return false;
151}
Benny Halevy8daf2202009-04-03 08:28:59 +0300152
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400153static bool *
154nfsd_alloc_versions(void)
155{
156 bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
157 unsigned i;
158
159 if (vers) {
160 /* All compiled versions are enabled by default */
161 for (i = 0; i < NFSD_NRVERS; i++)
162 vers[i] = nfsd_support_version(i);
163 }
164 return vers;
165}
166
167static bool *
168nfsd_alloc_minorversions(void)
169{
170 bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
171 sizeof(bool), GFP_KERNEL);
172 unsigned i;
173
174 if (vers) {
175 /* All minor versions are enabled by default */
176 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
177 vers[i] = nfsd_support_version(4);
178 }
179 return vers;
180}
181
182void
183nfsd_netns_free_versions(struct nfsd_net *nn)
184{
185 kfree(nn->nfsd_versions);
186 kfree(nn->nfsd4_minorversions);
187 nn->nfsd_versions = NULL;
188 nn->nfsd4_minorversions = NULL;
189}
190
191static void
192nfsd_netns_init_versions(struct nfsd_net *nn)
193{
194 if (!nn->nfsd_versions) {
195 nn->nfsd_versions = nfsd_alloc_versions();
196 nn->nfsd4_minorversions = nfsd_alloc_minorversions();
197 if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
198 nfsd_netns_free_versions(nn);
199 }
200}
201
202int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
NeilBrown6658d3a2006-10-02 02:17:46 -0700203{
204 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
Pavel Emelyanov15ddb4a2010-05-14 15:33:36 +0400205 return 0;
NeilBrown6658d3a2006-10-02 02:17:46 -0700206 switch(change) {
207 case NFSD_SET:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400208 if (nn->nfsd_versions)
209 nn->nfsd_versions[vers] = nfsd_support_version(vers);
NeilBrown1a8eff62007-01-26 00:56:58 -0800210 break;
NeilBrown6658d3a2006-10-02 02:17:46 -0700211 case NFSD_CLEAR:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400212 nfsd_netns_init_versions(nn);
213 if (nn->nfsd_versions)
214 nn->nfsd_versions[vers] = false;
NeilBrown6658d3a2006-10-02 02:17:46 -0700215 break;
216 case NFSD_TEST:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400217 if (nn->nfsd_versions)
218 return nn->nfsd_versions[vers];
219 /* Fallthrough */
NeilBrown6658d3a2006-10-02 02:17:46 -0700220 case NFSD_AVAIL:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400221 return nfsd_support_version(vers);
NeilBrown6658d3a2006-10-02 02:17:46 -0700222 }
223 return 0;
224}
Benny Halevy8daf2202009-04-03 08:28:59 +0300225
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500226static void
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400227nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500228{
229 unsigned i;
230
231 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400232 if (nn->nfsd4_minorversions[i])
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500233 return;
234 }
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400235 nfsd_vers(nn, 4, NFSD_CLEAR);
Trond Myklebustd3635ff2017-02-22 18:35:32 -0500236}
237
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400238int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
Benny Halevy8daf2202009-04-03 08:28:59 +0300239{
NeilBrown928c6fb2017-03-10 11:36:39 +1100240 if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
241 change != NFSD_AVAIL)
Benny Halevy8daf2202009-04-03 08:28:59 +0300242 return -1;
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400243
Benny Halevy8daf2202009-04-03 08:28:59 +0300244 switch(change) {
245 case NFSD_SET:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400246 if (nn->nfsd4_minorversions) {
247 nfsd_vers(nn, 4, NFSD_SET);
248 nn->nfsd4_minorversions[minorversion] =
249 nfsd_vers(nn, 4, NFSD_TEST);
250 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300251 break;
252 case NFSD_CLEAR:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400253 nfsd_netns_init_versions(nn);
254 if (nn->nfsd4_minorversions) {
255 nn->nfsd4_minorversions[minorversion] = false;
256 nfsd_adjust_nfsd_versions4(nn);
257 }
Benny Halevy8daf2202009-04-03 08:28:59 +0300258 break;
259 case NFSD_TEST:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400260 if (nn->nfsd4_minorversions)
261 return nn->nfsd4_minorversions[minorversion];
262 return nfsd_vers(nn, 4, NFSD_TEST);
Benny Halevy8daf2202009-04-03 08:28:59 +0300263 case NFSD_AVAIL:
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400264 return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
265 nfsd_vers(nn, 4, NFSD_AVAIL);
Benny Halevy8daf2202009-04-03 08:28:59 +0300266 }
267 return 0;
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/*
271 * Maximum number of nfsd processes
272 */
273#define NFSD_MAXSERVS 8192
274
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300275int nfsd_nrthreads(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Neil Brownc7d106c2008-06-12 13:38:42 +1000277 int rv = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300278 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
279
Neil Brownc7d106c2008-06-12 13:38:42 +1000280 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300281 if (nn->nfsd_serv)
282 rv = nn->nfsd_serv->sv_nrthreads;
Neil Brownc7d106c2008-06-12 13:38:42 +1000283 mutex_unlock(&nfsd_mutex);
284 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Trond Myklebust4df493a2019-04-09 12:13:37 -0400287static int nfsd_init_socks(struct net *net, const struct cred *cred)
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400288{
289 int error;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300290 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
291
292 if (!list_empty(&nn->nfsd_serv->sv_permsocks))
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400293 return 0;
294
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300295 error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
Trond Myklebust4df493a2019-04-09 12:13:37 -0400296 SVC_SOCK_DEFAULTS, cred);
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400297 if (error < 0)
298 return error;
299
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300300 error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
Trond Myklebust4df493a2019-04-09 12:13:37 -0400301 SVC_SOCK_DEFAULTS, cred);
J. Bruce Fields59db4a02010-07-21 18:29:25 -0400302 if (error < 0)
303 return error;
304
305 return 0;
306}
307
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300308static int nfsd_users = 0;
Jeff Layton4ad9a342010-07-19 16:50:04 -0400309
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300310static int nfsd_startup_generic(int nrservs)
311{
312 int ret;
313
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300314 if (nfsd_users++)
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300315 return 0;
316
Jeff Layton65294c12019-08-18 14:18:48 -0400317 ret = nfsd_file_cache_init();
318 if (ret)
319 goto dec_users;
Kinglong Meed9499a92014-07-30 21:26:05 +0800320
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300321 ret = nfs4_state_start();
322 if (ret)
Jeff Layton501cb182019-08-18 14:18:56 -0400323 goto out_file_cache;
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300324 return 0;
325
Jeff Layton65294c12019-08-18 14:18:48 -0400326out_file_cache:
327 nfsd_file_cache_shutdown();
Kinglong Meed9499a92014-07-30 21:26:05 +0800328dec_users:
329 nfsd_users--;
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300330 return ret;
331}
332
333static void nfsd_shutdown_generic(void)
334{
Stanislav Kinsbursky4539f142012-12-06 14:23:39 +0300335 if (--nfsd_users)
336 return;
337
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300338 nfs4_state_shutdown();
Jeff Layton65294c12019-08-18 14:18:48 -0400339 nfsd_file_cache_shutdown();
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300340}
341
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400342static bool nfsd_needs_lockd(struct nfsd_net *nn)
Kinglong Mee8ef66712013-12-31 13:17:30 +0800343{
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400344 return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
Kinglong Mee8ef66712013-12-31 13:17:30 +0800345}
346
Trond Myklebust27c438f2019-09-02 13:02:56 -0400347void nfsd_copy_boot_verifier(__be32 verf[2], struct nfsd_net *nn)
348{
349 int seq = 0;
350
351 do {
352 read_seqbegin_or_lock(&nn->boot_lock, &seq);
353 /*
354 * This is opaque to client, so no need to byte-swap. Use
355 * __force to keep sparse happy. y2038 time_t overflow is
356 * irrelevant in this usage
357 */
358 verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
359 verf[1] = (__force __be32)nn->nfssvc_boot.tv_nsec;
360 } while (need_seqretry(&nn->boot_lock, seq));
361 done_seqretry(&nn->boot_lock, seq);
362}
363
YueHaibing65643f42019-09-23 13:58:59 +0800364static void nfsd_reset_boot_verifier_locked(struct nfsd_net *nn)
Trond Myklebust27c438f2019-09-02 13:02:56 -0400365{
366 ktime_get_real_ts64(&nn->nfssvc_boot);
367}
368
369void nfsd_reset_boot_verifier(struct nfsd_net *nn)
370{
371 write_seqlock(&nn->boot_lock);
372 nfsd_reset_boot_verifier_locked(nn);
373 write_sequnlock(&nn->boot_lock);
374}
375
Trond Myklebust4df493a2019-04-09 12:13:37 -0400376static int nfsd_startup_net(int nrservs, struct net *net, const struct cred *cred)
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300377{
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300378 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300379 int ret;
380
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300381 if (nn->nfsd_net_up)
382 return 0;
383
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300384 ret = nfsd_startup_generic(nrservs);
385 if (ret)
386 return ret;
Trond Myklebust4df493a2019-04-09 12:13:37 -0400387 ret = nfsd_init_socks(net, cred);
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300388 if (ret)
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300389 goto out_socks;
Kinglong Mee8ef66712013-12-31 13:17:30 +0800390
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400391 if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
Trond Myklebust40373b12019-04-09 12:13:39 -0400392 ret = lockd_up(net, cred);
Kinglong Mee8ef66712013-12-31 13:17:30 +0800393 if (ret)
394 goto out_socks;
395 nn->lockd_up = 1;
396 }
397
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300398 ret = nfs4_state_start_net(net);
399 if (ret)
400 goto out_lockd;
401
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300402 nn->nfsd_net_up = true;
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300403 return 0;
404
405out_lockd:
Kinglong Mee8ef66712013-12-31 13:17:30 +0800406 if (nn->lockd_up) {
407 lockd_down(net);
408 nn->lockd_up = 0;
409 }
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300410out_socks:
Stanislav Kinsburskybda9cac2012-12-06 14:23:29 +0300411 nfsd_shutdown_generic();
Jeff Layton4ad9a342010-07-19 16:50:04 -0400412 return ret;
413}
414
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300415static void nfsd_shutdown_net(struct net *net)
416{
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300417 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
418
Trond Myklebust5e113222019-09-02 13:02:55 -0400419 nfsd_file_cache_purge(net);
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300420 nfs4_state_shutdown_net(net);
Kinglong Mee8ef66712013-12-31 13:17:30 +0800421 if (nn->lockd_up) {
422 lockd_down(net);
423 nn->lockd_up = 0;
424 }
Stanislav Kinsbursky2c2fe292012-12-06 14:23:14 +0300425 nn->nfsd_net_up = false;
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300426 nfsd_shutdown_generic();
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300427}
428
Scott Mayhew36684992015-12-11 16:45:59 -0500429static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
430 void *ptr)
431{
432 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
433 struct net_device *dev = ifa->ifa_dev->dev;
434 struct net *net = dev_net(dev);
435 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
436 struct sockaddr_in sin;
437
Vasily Averin2317dc52017-11-10 10:19:35 +0300438 if ((event != NETDEV_DOWN) ||
439 !atomic_inc_not_zero(&nn->ntf_refcnt))
Scott Mayhew36684992015-12-11 16:45:59 -0500440 goto out;
441
442 if (nn->nfsd_serv) {
443 dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
444 sin.sin_family = AF_INET;
445 sin.sin_addr.s_addr = ifa->ifa_local;
446 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
447 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300448 atomic_dec(&nn->ntf_refcnt);
449 wake_up(&nn->ntf_wq);
Scott Mayhew36684992015-12-11 16:45:59 -0500450
451out:
452 return NOTIFY_DONE;
453}
454
455static struct notifier_block nfsd_inetaddr_notifier = {
456 .notifier_call = nfsd_inetaddr_event,
457};
458
459#if IS_ENABLED(CONFIG_IPV6)
460static int nfsd_inet6addr_event(struct notifier_block *this,
461 unsigned long event, void *ptr)
462{
463 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
464 struct net_device *dev = ifa->idev->dev;
465 struct net *net = dev_net(dev);
466 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
467 struct sockaddr_in6 sin6;
468
Vasily Averin2317dc52017-11-10 10:19:35 +0300469 if ((event != NETDEV_DOWN) ||
470 !atomic_inc_not_zero(&nn->ntf_refcnt))
Scott Mayhew36684992015-12-11 16:45:59 -0500471 goto out;
472
473 if (nn->nfsd_serv) {
474 dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
475 sin6.sin6_family = AF_INET6;
476 sin6.sin6_addr = ifa->addr;
Scott Mayhew7b198242017-01-05 16:34:49 -0500477 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
478 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
Scott Mayhew36684992015-12-11 16:45:59 -0500479 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
480 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300481 atomic_dec(&nn->ntf_refcnt);
482 wake_up(&nn->ntf_wq);
Scott Mayhew36684992015-12-11 16:45:59 -0500483out:
484 return NOTIFY_DONE;
485}
486
487static struct notifier_block nfsd_inet6addr_notifier = {
488 .notifier_call = nfsd_inet6addr_event,
489};
490#endif
491
Vasily Averin1eca45f2016-09-21 15:33:05 +0300492/* Only used under nfsd_mutex, so this atomic may be overkill: */
493static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
494
Stanislav Kinsbursky541e8642012-12-06 14:23:44 +0300495static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
Jeff Layton4ad9a342010-07-19 16:50:04 -0400496{
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300497 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
498
Vasily Averin2317dc52017-11-10 10:19:35 +0300499 atomic_dec(&nn->ntf_refcnt);
Vasily Averin1eca45f2016-09-21 15:33:05 +0300500 /* check if the notifier still has clients */
501 if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
502 unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500503#if IS_ENABLED(CONFIG_IPV6)
Vasily Averin1eca45f2016-09-21 15:33:05 +0300504 unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500505#endif
Vasily Averin1eca45f2016-09-21 15:33:05 +0300506 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300507 wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
Vasily Averin1eca45f2016-09-21 15:33:05 +0300508
Jeff Layton4ad9a342010-07-19 16:50:04 -0400509 /*
510 * write_ports can create the server without actually starting
511 * any threads--if we get shut down before any threads are
512 * started, then nfsd_last_thread will be run before any of this
Kinglong Mee691412b2016-01-04 11:15:21 +0800513 * other initialization has been done except the rpcb information.
Jeff Layton4ad9a342010-07-19 16:50:04 -0400514 */
Kinglong Mee691412b2016-01-04 11:15:21 +0800515 svc_rpcb_cleanup(serv, net);
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300516 if (!nn->nfsd_net_up)
Jeff Layton4ad9a342010-07-19 16:50:04 -0400517 return;
Kinglong Mee691412b2016-01-04 11:15:21 +0800518
Stanislav Kinsbursky6ff50b32012-12-06 14:23:09 +0300519 nfsd_shutdown_net(net);
Jeff Laytone096bbc2008-06-10 08:40:37 -0400520 printk(KERN_WARNING "nfsd: last server has exited, flushing export "
521 "cache\n");
Stanislav Kinsburskyb3853e02012-04-11 15:13:21 +0400522 nfsd_export_flush(net);
NeilBrownbc591cc2006-10-02 02:17:44 -0700523}
NeilBrown6658d3a2006-10-02 02:17:46 -0700524
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400525void nfsd_reset_versions(struct nfsd_net *nn)
NeilBrown6658d3a2006-10-02 02:17:46 -0700526{
NeilBrown6658d3a2006-10-02 02:17:46 -0700527 int i;
528
NeilBrown800a9382017-03-10 11:36:39 +1100529 for (i = 0; i < NFSD_NRVERS; i++)
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400530 if (nfsd_vers(nn, i, NFSD_TEST))
NeilBrown800a9382017-03-10 11:36:39 +1100531 return;
NeilBrown6658d3a2006-10-02 02:17:46 -0700532
NeilBrown800a9382017-03-10 11:36:39 +1100533 for (i = 0; i < NFSD_NRVERS; i++)
534 if (i != 4)
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400535 nfsd_vers(nn, i, NFSD_SET);
NeilBrown800a9382017-03-10 11:36:39 +1100536 else {
537 int minor = 0;
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400538 while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
NeilBrown800a9382017-03-10 11:36:39 +1100539 minor++;
540 }
NeilBrown6658d3a2006-10-02 02:17:46 -0700541}
542
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300543/*
544 * Each session guarantees a negotiated per slot memory cache for replies
545 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
546 * NFSv4.1 server might want to use more memory for a DRC than a machine
547 * with mutiple services.
548 *
549 * Impose a hard limit on the number of pages for the DRC which varies
550 * according to the machines free pages. This is of course only a default.
551 *
552 * For now this is a #defined shift which could be under admin control
553 * in the future.
554 */
555static void set_max_drc(void)
556{
J. Bruce Fields44d86602017-09-19 20:51:31 -0400557 #define NFSD_DRC_SIZE_SHIFT 7
Andy Adamson0c193052009-07-27 19:09:19 -0400558 nfsd_drc_max_mem = (nr_free_buffer_pages()
559 >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
560 nfsd_drc_mem_used = 0;
Andy Adamson4bd9b0f42009-06-24 15:37:45 -0400561 spin_lock_init(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800562 dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
Andy Adamsonc3d06f92009-04-03 08:28:18 +0300563}
Neil Brownbedbdd82008-06-10 08:40:35 -0400564
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500565static int nfsd_get_default_max_blksize(void)
566{
567 struct sysinfo i;
568 unsigned long long target;
569 unsigned long ret;
570
571 si_meminfo(&i);
J. Bruce Fields508f9222012-01-30 16:21:11 -0500572 target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500573 /*
574 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
575 * machines, but only uses 32K on 128M machines. Bottom out at
576 * 8K on 32M and smaller. Of course, this is only a default.
577 */
578 target >>= 12;
579
580 ret = NFSSVC_MAXBLKSIZE;
581 while (ret > target && ret >= 8*1024*2)
582 ret /= 2;
583 return ret;
584}
585
Chuck Leverafea5652017-08-01 12:00:06 -0400586static const struct svc_serv_ops nfsd_thread_sv_ops = {
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700587 .svo_shutdown = nfsd_last_thread,
588 .svo_function = nfsd,
589 .svo_enqueue_xprt = svc_xprt_do_enqueue,
Jeff Layton598e2352015-06-08 12:08:33 -0700590 .svo_setup = svc_set_num_threads,
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700591 .svo_module = THIS_MODULE,
Jeff Laytonea126e72015-06-08 12:03:32 -0700592};
593
Stanislav Kinsbursky67774362012-12-10 12:19:20 +0300594int nfsd_create_serv(struct net *net)
NeilBrown02a375f2006-10-02 02:17:46 -0700595{
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400596 int error;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300597 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400598
Neil Brownbedbdd82008-06-10 08:40:35 -0400599 WARN_ON(!mutex_is_locked(&nfsd_mutex));
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300600 if (nn->nfsd_serv) {
601 svc_get(nn->nfsd_serv);
NeilBrown02a375f2006-10-02 02:17:46 -0700602 return 0;
603 }
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500604 if (nfsd_max_blksize == 0)
605 nfsd_max_blksize = nfsd_get_default_max_blksize();
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400606 nfsd_reset_versions(nn);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300607 nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700608 &nfsd_thread_sv_ops);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300609 if (nn->nfsd_serv == NULL)
Jeff Layton628b3682010-07-21 16:40:08 -0400610 return -ENOMEM;
Neil Brownbedbdd82008-06-10 08:40:35 -0400611
Jeff Layton5b8db002014-07-02 16:11:22 -0400612 nn->nfsd_serv->sv_maxconn = nn->max_connections;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300613 error = svc_bind(nn->nfsd_serv, net);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400614 if (error < 0) {
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300615 svc_destroy(nn->nfsd_serv);
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400616 return error;
617 }
618
Jeff Layton628b3682010-07-21 16:40:08 -0400619 set_max_drc();
Vasily Averin1eca45f2016-09-21 15:33:05 +0300620 /* check if the notifier is already set */
621 if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
622 register_inetaddr_notifier(&nfsd_inetaddr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500623#if IS_ENABLED(CONFIG_IPV6)
Vasily Averin1eca45f2016-09-21 15:33:05 +0300624 register_inet6addr_notifier(&nfsd_inet6addr_notifier);
Scott Mayhew36684992015-12-11 16:45:59 -0500625#endif
Vasily Averin1eca45f2016-09-21 15:33:05 +0300626 }
Vasily Averin2317dc52017-11-10 10:19:35 +0300627 atomic_inc(&nn->ntf_refcnt);
Trond Myklebust27c438f2019-09-02 13:02:56 -0400628 nfsd_reset_boot_verifier(nn);
J. Bruce Fields87b0fc72012-01-30 16:18:35 -0500629 return 0;
NeilBrown02a375f2006-10-02 02:17:46 -0700630}
631
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300632int nfsd_nrpools(struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700633{
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300634 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
635
636 if (nn->nfsd_serv == NULL)
Greg Bankseed29652006-10-02 02:18:02 -0700637 return 0;
638 else
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300639 return nn->nfsd_serv->sv_nrpools;
Greg Bankseed29652006-10-02 02:18:02 -0700640}
641
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300642int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700643{
644 int i = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300645 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Greg Bankseed29652006-10-02 02:18:02 -0700646
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300647 if (nn->nfsd_serv != NULL) {
648 for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
649 nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
Greg Bankseed29652006-10-02 02:18:02 -0700650 }
651
652 return 0;
653}
654
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300655void nfsd_destroy(struct net *net)
656{
657 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
658 int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
659
660 if (destroy)
661 svc_shutdown_net(nn->nfsd_serv, net);
662 svc_destroy(nn->nfsd_serv);
663 if (destroy)
664 nn->nfsd_serv = NULL;
665}
666
Stanislav Kinsbursky3938a0d2012-12-10 12:19:30 +0300667int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
Greg Bankseed29652006-10-02 02:18:02 -0700668{
669 int i = 0;
670 int tot = 0;
671 int err = 0;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300672 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Greg Bankseed29652006-10-02 02:18:02 -0700673
Neil Brownbedbdd82008-06-10 08:40:35 -0400674 WARN_ON(!mutex_is_locked(&nfsd_mutex));
675
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300676 if (nn->nfsd_serv == NULL || n <= 0)
Greg Bankseed29652006-10-02 02:18:02 -0700677 return 0;
678
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300679 if (n > nn->nfsd_serv->sv_nrpools)
680 n = nn->nfsd_serv->sv_nrpools;
Greg Bankseed29652006-10-02 02:18:02 -0700681
682 /* enforce a global maximum number of threads */
683 tot = 0;
684 for (i = 0; i < n; i++) {
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800685 nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
Greg Bankseed29652006-10-02 02:18:02 -0700686 tot += nthreads[i];
687 }
688 if (tot > NFSD_MAXSERVS) {
689 /* total too large: scale down requested numbers */
690 for (i = 0; i < n && tot > 0; i++) {
691 int new = nthreads[i] * NFSD_MAXSERVS / tot;
692 tot -= (nthreads[i] - new);
693 nthreads[i] = new;
694 }
695 for (i = 0; i < n && tot > 0; i++) {
696 nthreads[i]--;
697 tot--;
698 }
699 }
700
701 /*
702 * There must always be a thread in pool 0; the admin
703 * can't shut down NFS completely using pool_threads.
704 */
705 if (nthreads[0] == 0)
706 nthreads[0] = 1;
707
708 /* apply the new numbers */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300709 svc_get(nn->nfsd_serv);
Greg Bankseed29652006-10-02 02:18:02 -0700710 for (i = 0; i < n; i++) {
Jeff Layton598e2352015-06-08 12:08:33 -0700711 err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
712 &nn->nfsd_serv->sv_pools[i], nthreads[i]);
Greg Bankseed29652006-10-02 02:18:02 -0700713 if (err)
714 break;
715 }
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400716 nfsd_destroy(net);
Greg Bankseed29652006-10-02 02:18:02 -0700717 return err;
718}
719
Jeff Laytonac77efb2010-07-20 14:10:22 -0400720/*
721 * Adjust the number of threads and return the new number of threads.
722 * This is also the function that starts the server if necessary, if
723 * this is the first time nrservs is nonzero.
724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725int
Trond Myklebust4df493a2019-04-09 12:13:37 -0400726nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 int error;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400729 bool nfsd_up_before;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300730 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Neil Brownbedbdd82008-06-10 08:40:35 -0400731
732 mutex_lock(&nfsd_mutex);
NeilBrown6658d3a2006-10-02 02:17:46 -0700733 dprintk("nfsd: creating service\n");
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800734
735 nrservs = max(nrservs, 0);
736 nrservs = min(nrservs, NFSD_MAXSERVS);
NeilBrown671e1fc2009-06-16 11:03:20 +1000737 error = 0;
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800738
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300739 if (nrservs == 0 && nn->nfsd_serv == NULL)
NeilBrown671e1fc2009-06-16 11:03:20 +1000740 goto out;
741
Stanislav Kinsbursky67774362012-12-10 12:19:20 +0300742 error = nfsd_create_serv(net);
NeilBrown02a375f2006-10-02 02:17:46 -0700743 if (error)
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400744 goto out;
745
Stanislav Kinsbursky903d9bf2012-12-06 14:23:34 +0300746 nfsd_up_before = nn->nfsd_net_up;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400747
Trond Myklebust4df493a2019-04-09 12:13:37 -0400748 error = nfsd_startup_net(nrservs, net, cred);
J. Bruce Fieldsaf4718f2010-07-21 18:31:42 -0400749 if (error)
750 goto out_destroy;
Jeff Layton598e2352015-06-08 12:08:33 -0700751 error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
752 NULL, nrservs);
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400753 if (error)
754 goto out_shutdown;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300755 /* We are holding a reference to nn->nfsd_serv which
J. Bruce Fieldsaf4718f2010-07-21 18:31:42 -0400756 * we don't want to count in the return value,
757 * so subtract 1
758 */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300759 error = nn->nfsd_serv->sv_nrthreads - 1;
J. Bruce Fields774f8bb2010-08-02 14:12:44 -0400760out_shutdown:
761 if (error < 0 && !nfsd_up_before)
Stanislav Kinsbursky541e8642012-12-06 14:23:44 +0300762 nfsd_shutdown_net(net);
Jeff Layton4ad9a342010-07-19 16:50:04 -0400763out_destroy:
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +0400764 nfsd_destroy(net); /* Release server */
Jeff Layton4ad9a342010-07-19 16:50:04 -0400765out:
Neil Brownbedbdd82008-06-10 08:40:35 -0400766 mutex_unlock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return error;
768}
769
Trond Myklebust029be5d2019-04-09 11:46:18 -0400770#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
771static bool
772nfsd_support_acl_version(int vers)
773{
774 if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
775 return nfsd_acl_version[vers] != NULL;
776 return false;
777}
778
779static int
780nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
781 u32 version, int family, unsigned short proto,
782 unsigned short port)
783{
784 if (!nfsd_support_acl_version(version) ||
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400785 !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
Trond Myklebust029be5d2019-04-09 11:46:18 -0400786 return 0;
787 return svc_generic_rpcbind_set(net, progp, version, family,
788 proto, port);
789}
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400790
791static __be32
792nfsd_acl_init_request(struct svc_rqst *rqstp,
793 const struct svc_program *progp,
794 struct svc_process_info *ret)
795{
796 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
797 int i;
798
799 if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
800 nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
801 return svc_generic_init_request(rqstp, progp, ret);
802
803 ret->mismatch.lovers = NFSD_ACL_NRVERS;
804 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
805 if (nfsd_support_acl_version(rqstp->rq_vers) &&
806 nfsd_vers(nn, i, NFSD_TEST)) {
807 ret->mismatch.lovers = i;
808 break;
809 }
810 }
811 if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
812 return rpc_prog_unavail;
813 ret->mismatch.hivers = NFSD_ACL_MINVERS;
814 for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
815 if (nfsd_support_acl_version(rqstp->rq_vers) &&
816 nfsd_vers(nn, i, NFSD_TEST)) {
817 ret->mismatch.hivers = i;
818 break;
819 }
820 }
821 return rpc_prog_mismatch;
822}
Trond Myklebust029be5d2019-04-09 11:46:18 -0400823#endif
824
825static int
826nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
827 u32 version, int family, unsigned short proto,
828 unsigned short port)
829{
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400830 if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
Trond Myklebust029be5d2019-04-09 11:46:18 -0400831 return 0;
832 return svc_generic_rpcbind_set(net, progp, version, family,
833 proto, port);
834}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Trond Myklebuste333f3b2019-04-09 11:46:19 -0400836static __be32
837nfsd_init_request(struct svc_rqst *rqstp,
838 const struct svc_program *progp,
839 struct svc_process_info *ret)
840{
841 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
842 int i;
843
844 if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
845 return svc_generic_init_request(rqstp, progp, ret);
846
847 ret->mismatch.lovers = NFSD_NRVERS;
848 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
849 if (nfsd_vers(nn, i, NFSD_TEST)) {
850 ret->mismatch.lovers = i;
851 break;
852 }
853 }
854 if (ret->mismatch.lovers == NFSD_NRVERS)
855 return rpc_prog_unavail;
856 ret->mismatch.hivers = NFSD_MINVERS;
857 for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
858 if (nfsd_vers(nn, i, NFSD_TEST)) {
859 ret->mismatch.hivers = i;
860 break;
861 }
862 }
863 return rpc_prog_mismatch;
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * This is the NFS server kernel thread
868 */
Jeff Layton9867d762008-06-10 08:40:38 -0400869static int
870nfsd(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Jeff Layton9867d762008-06-10 08:40:38 -0400872 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
Stanislav Kinsbursky88c47662012-12-06 18:34:42 +0300873 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
874 struct net *net = perm_sock->xpt_net;
Jeff Layton5b8db002014-07-02 16:11:22 -0400875 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields5b444cc2012-08-17 21:47:53 -0400876 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /* Lock module and set up kernel thread */
Neil Brownbedbdd82008-06-10 08:40:35 -0400879 mutex_lock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Jeff Layton9867d762008-06-10 08:40:38 -0400881 /* At this point, the thread shares current->fs
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100882 * with the init process. We need to create files with the
883 * umask as defined by the client instead of init's umask. */
Al Viro3e93cd62009-03-29 19:00:13 -0400884 if (unshare_fs_struct() < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 printk("Unable to start nfsd thread: out of memory\n");
886 goto out;
887 }
Al Viro3e93cd62009-03-29 19:00:13 -0400888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 current->fs->umask = 0;
890
Jeff Layton9867d762008-06-10 08:40:38 -0400891 /*
892 * thread is spawned with all signals set to SIG_IGN, re-enable
Jeff Layton100766f2008-06-30 14:09:46 -0400893 * the ones that will bring down the thread
Jeff Layton9867d762008-06-10 08:40:38 -0400894 */
Jeff Layton100766f2008-06-30 14:09:46 -0400895 allow_signal(SIGKILL);
896 allow_signal(SIGHUP);
897 allow_signal(SIGINT);
898 allow_signal(SIGQUIT);
Neil Brownbedbdd82008-06-10 08:40:35 -0400899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 nfsdstats.th_cnt++;
Neil Brownbedbdd82008-06-10 08:40:35 -0400901 mutex_unlock(&nfsd_mutex);
902
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700903 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /*
906 * The main request loop
907 */
908 for (;;) {
Jeff Layton5b8db002014-07-02 16:11:22 -0400909 /* Update sv_maxconn if it has changed */
910 rqstp->rq_server->sv_maxconn = nn->max_connections;
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /*
913 * Find a socket with data available and call its
914 * recvfrom routine.
915 */
NeilBrown6fb2b472006-10-02 02:17:50 -0700916 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 ;
Jeff Layton9867d762008-06-10 08:40:38 -0400918 if (err == -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
David Howellse0e81732009-09-02 09:13:40 +0100920 validate_process_creds();
NeilBrown6fb2b472006-10-02 02:17:50 -0700921 svc_process(rqstp);
David Howellse0e81732009-09-02 09:13:40 +0100922 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
NeilBrown24e36662006-10-02 02:17:45 -0700925 /* Clear signals before calling svc_exit_thread() */
NeilBrown9e4160522005-04-16 15:26:37 -0700926 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Neil Brownbedbdd82008-06-10 08:40:35 -0400928 mutex_lock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 nfsdstats.th_cnt --;
930
931out:
Stanislav Kinsbursky57c8b13e2012-07-03 16:46:41 +0400932 rqstp->rq_server = NULL;
Stanislav Kinsbursky786185b2012-05-04 12:49:41 +0400933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* Release the thread */
935 svc_exit_thread(rqstp);
936
Stanislav Kinsbursky88c47662012-12-06 18:34:42 +0300937 nfsd_destroy(net);
Stanislav Kinsbursky57c8b13e2012-07-03 16:46:41 +0400938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* Release module */
Neil Brownbedbdd82008-06-10 08:40:35 -0400940 mutex_unlock(&nfsd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 module_put_and_exit(0);
Jeff Layton9867d762008-06-10 08:40:38 -0400942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Andy Adamson32c1eb02007-07-17 04:04:48 -0700945static __be32 map_new_errors(u32 vers, __be32 nfserr)
946{
947 if (nfserr == nfserr_jukebox && vers == 2)
948 return nfserr_dropit;
949 if (nfserr == nfserr_wrongsec && vers < 4)
950 return nfserr_acces;
951 return nfserr;
952}
953
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400954/*
955 * A write procedure can have a large argument, and a read procedure can
956 * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
957 * reply that can both be larger than a page. The xdr code has taken
958 * advantage of this assumption to be a sloppy about bounds checking in
959 * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that
960 * problem, we enforce these assumptions here:
961 */
962static bool nfs_request_too_big(struct svc_rqst *rqstp,
Christoph Hellwig860bda22017-05-12 16:11:49 +0200963 const struct svc_procedure *proc)
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400964{
965 /*
966 * The ACL code has more careful bounds-checking and is not
967 * susceptible to this problem:
968 */
969 if (rqstp->rq_prog != NFS_PROGRAM)
970 return false;
971 /*
972 * Ditto NFSv4 (which can in theory have argument and reply both
973 * more than a page):
974 */
975 if (rqstp->rq_vers >= 4)
976 return false;
977 /* The reply will be small, we're OK: */
978 if (proc->pc_xdrressize > 0 &&
979 proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
980 return false;
981
982 return rqstp->rq_arg.len > PAGE_SIZE;
983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985int
Al Viroc7afef12006-10-19 23:29:02 -0700986nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Christoph Hellwig860bda22017-05-12 16:11:49 +0200988 const struct svc_procedure *proc;
Al Viroad451d32006-10-19 23:28:55 -0700989 __be32 nfserr;
990 __be32 *nfserrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 dprintk("nfsd_dispatch: vers %d proc %d\n",
993 rqstp->rq_vers, rqstp->rq_proc);
994 proc = rqstp->rq_procinfo;
995
J. Bruce Fieldse6838a22017-04-21 16:10:18 -0400996 if (nfs_request_too_big(rqstp, proc)) {
997 dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
998 *statp = rpc_garbage_args;
999 return 1;
1000 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001001 /*
1002 * Give the xdr decoder a chance to change this if it wants
1003 * (necessary in the NFSv4.0 compound case)
1004 */
1005 rqstp->rq_cachetype = proc->pc_cachetype;
1006 /* Decode arguments */
Christoph Hellwig026fec72017-05-08 19:01:48 +02001007 if (proc->pc_decode &&
1008 !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) {
J. Bruce Fields10910062011-01-24 12:11:02 -05001009 dprintk("nfsd: failed to decode arguments!\n");
1010 *statp = rpc_garbage_args;
1011 return 1;
1012 }
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* Check whether we have this call in the cache. */
J. Bruce Fields10910062011-01-24 12:11:02 -05001015 switch (nfsd_cache_lookup(rqstp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 case RC_DROPIT:
1017 return 0;
1018 case RC_REPLY:
1019 return 1;
1020 case RC_DOIT:;
1021 /* do it */
1022 }
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /* need to grab the location to store the status, as
1025 * nfsv4 does some encoding while processing
1026 */
1027 nfserrp = rqstp->rq_res.head[0].iov_base
1028 + rqstp->rq_res.head[0].iov_len;
Al Viroad451d32006-10-19 23:28:55 -07001029 rqstp->rq_res.head[0].iov_len += sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /* Now call the procedure handler, and encode NFS status. */
Christoph Hellwiga6beb732017-05-08 17:35:49 +02001032 nfserr = proc->pc_func(rqstp);
Andy Adamson32c1eb02007-07-17 04:04:48 -07001033 nfserr = map_new_errors(rqstp->rq_vers, nfserr);
Jeff Layton78b65eb2014-11-19 07:51:17 -05001034 if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) {
J. Bruce Fields45457e02007-06-22 17:26:32 -04001035 dprintk("nfsd: Dropping request; may be revisited later\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1037 return 0;
1038 }
1039
1040 if (rqstp->rq_proc != 0)
1041 *nfserrp++ = nfserr;
1042
1043 /* Encode result.
1044 * For NFSv2, additional info is never returned in case of an error.
1045 */
1046 if (!(nfserr && rqstp->rq_vers == 2)) {
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001047 if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /* Failed to encode result. Release cache entry */
1049 dprintk("nfsd: failed to encode result!\n");
1050 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1051 *statp = rpc_system_err;
1052 return 1;
1053 }
1054 }
1055
1056 /* Store reply in cache. */
J. Bruce Fields57d276d2012-11-16 15:22:43 -05001057 nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 1;
1059}
Greg Banks03cf6c92009-01-13 21:26:36 +11001060
1061int nfsd_pool_stats_open(struct inode *inode, struct file *file)
1062{
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001063 int ret;
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +03001064 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001065
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001066 mutex_lock(&nfsd_mutex);
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001067 if (nn->nfsd_serv == NULL) {
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001068 mutex_unlock(&nfsd_mutex);
Greg Banks03cf6c92009-01-13 21:26:36 +11001069 return -ENODEV;
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001070 }
1071 /* bump up the psudo refcount while traversing */
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001072 svc_get(nn->nfsd_serv);
1073 ret = svc_pool_stats_open(nn->nfsd_serv, file);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001074 mutex_unlock(&nfsd_mutex);
1075 return ret;
1076}
1077
1078int nfsd_pool_stats_release(struct inode *inode, struct file *file)
1079{
1080 int ret = seq_release(inode, file);
Stanislav Kinsbursky11f77942013-02-01 15:56:12 +03001081 struct net *net = inode->i_sb->s_fs_info;
Stanislav Kinsbursky786185b2012-05-04 12:49:41 +04001082
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001083 mutex_lock(&nfsd_mutex);
1084 /* this function really, really should have been called svc_put() */
Stanislav Kinsbursky19f7e2c2012-07-03 16:46:41 +04001085 nfsd_destroy(net);
Ryusei Yamaguchied2d8ae2009-08-16 00:54:41 +09001086 mutex_unlock(&nfsd_mutex);
1087 return ret;
Greg Banks03cf6c92009-01-13 21:26:36 +11001088}