Merge tag 'for-6.7-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[linux-block.git] / fs / nfsd / nfssvc.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4
LT
3 * 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
3f07c014 10#include <linux/sched/signal.h>
83144186 11#include <linux/freezer.h>
143cb494 12#include <linux/module.h>
1da177e4 13#include <linux/fs_struct.h>
c3d06f9c 14#include <linux/swap.h>
91d2e9b5 15#include <linux/siphash.h>
1da177e4 16
1da177e4 17#include <linux/sunrpc/stats.h>
1da177e4 18#include <linux/sunrpc/svcsock.h>
36684996 19#include <linux/sunrpc/svc_xprt.h>
1da177e4 20#include <linux/lockd/bind.h>
a257cdd0 21#include <linux/nfsacl.h>
ed2d8aed 22#include <linux/seq_file.h>
36684996
SM
23#include <linux/inetdevice.h>
24#include <net/addrconf.h>
25#include <net/ipv6.h>
fc5d00b0 26#include <net/net_namespace.h>
9a74af21
BH
27#include "nfsd.h"
28#include "cache.h"
0a3adade 29#include "vfs.h"
2c2fe290 30#include "netns.h"
65294c1f 31#include "filecache.h"
1da177e4 32
0dfdad1c
CL
33#include "trace.h"
34
1da177e4
LT
35#define NFSDDBG_FACILITY NFSDDBG_SVC
36
1da177e4 37extern struct svc_program nfsd_program;
9867d76c 38static int nfsd(void *vrqstp);
029be5d0
TM
39#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
40static int nfsd_acl_rpcbind_set(struct net *,
41 const struct svc_program *,
42 u32, int,
43 unsigned short,
44 unsigned short);
e333f3bb
TM
45static __be32 nfsd_acl_init_request(struct svc_rqst *,
46 const struct svc_program *,
47 struct svc_process_info *);
029be5d0
TM
48#endif
49static int nfsd_rpcbind_set(struct net *,
50 const struct svc_program *,
51 u32, int,
52 unsigned short,
53 unsigned short);
e333f3bb
TM
54static __be32 nfsd_init_request(struct svc_rqst *,
55 const struct svc_program *,
56 struct svc_process_info *);
1da177e4 57
bedbdd8b 58/*
2a36395f
N
59 * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and some members
60 * of the svc_serv struct such as ->sv_temp_socks and ->sv_permsocks.
bedbdd8b 61 *
9dd9845f 62 * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
ec52361d
N
63 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0 (unless
64 * nn->keep_active is set). That number of nfsd threads must
65 * exist and each must be listed in ->sp_all_threads in some entry of
66 * ->sv_pools[].
bedbdd8b 67 *
ec52361d
N
68 * Each active thread holds a counted reference on nn->nfsd_serv, as does
69 * the nn->keep_active flag and various transient calls to svc_get().
3dd98a3b
JL
70 *
71 * Finally, the nfsd_mutex also protects some of the global variables that are
72 * accessed when nfsd starts and that are settable via the write_* routines in
73 * nfsctl.c. In particular:
74 *
75 * user_recovery_dirname
76 * user_lease_time
77 * nfsd_versions
bedbdd8b
NB
78 */
79DEFINE_MUTEX(nfsd_mutex);
bedbdd8b 80
4bd9b0f4
AA
81/*
82 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
83 * nfsd_drc_max_pages limits the total amount of memory available for
84 * version 4.1 DRC caches.
85 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
86 */
b73ac680 87DEFINE_SPINLOCK(nfsd_drc_lock);
697ce9be
ZY
88unsigned long nfsd_drc_max_mem;
89unsigned long nfsd_drc_mem_used;
4bd9b0f4 90
3fb803a9
AG
91#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
92static struct svc_stat nfsd_acl_svcstats;
e9679189 93static const struct svc_version *nfsd_acl_version[] = {
2f3a4b2a 94# if defined(CONFIG_NFSD_V2_ACL)
3fb803a9 95 [2] = &nfsd_acl_version2,
2f3a4b2a
JL
96# endif
97# if defined(CONFIG_NFSD_V3_ACL)
3fb803a9 98 [3] = &nfsd_acl_version3,
2f3a4b2a 99# endif
3fb803a9
AG
100};
101
102#define NFSD_ACL_MINVERS 2
e8c96f8c 103#define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
3fb803a9
AG
104
105static struct svc_program nfsd_acl_program = {
106 .pg_prog = NFS_ACL_PROGRAM,
107 .pg_nvers = NFSD_ACL_NRVERS,
7c149057 108 .pg_vers = nfsd_acl_version,
1a8eff6d 109 .pg_name = "nfsacl",
3fb803a9
AG
110 .pg_class = "nfsd",
111 .pg_stats = &nfsd_acl_svcstats,
112 .pg_authenticate = &svc_set_client,
e333f3bb 113 .pg_init_request = nfsd_acl_init_request,
029be5d0 114 .pg_rpcbind_set = nfsd_acl_rpcbind_set,
3fb803a9
AG
115};
116
117static struct svc_stat nfsd_acl_svcstats = {
118 .program = &nfsd_acl_program,
119};
120#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
121
e9679189 122static const struct svc_version *nfsd_version[] = {
2f3a4b2a 123#if defined(CONFIG_NFSD_V2)
70c3b76c 124 [2] = &nfsd_version2,
2f3a4b2a 125#endif
70c3b76c 126 [3] = &nfsd_version3,
70c3b76c
N
127#if defined(CONFIG_NFSD_V4)
128 [4] = &nfsd_version4,
129#endif
130};
131
132#define NFSD_MINVERS 2
e8c96f8c 133#define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
70c3b76c
N
134
135struct svc_program nfsd_program = {
3fb803a9
AG
136#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
137 .pg_next = &nfsd_acl_program,
138#endif
70c3b76c
N
139 .pg_prog = NFS_PROGRAM, /* program number */
140 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
e333f3bb 141 .pg_vers = nfsd_version, /* version table */
70c3b76c
N
142 .pg_name = "nfsd", /* program name */
143 .pg_class = "nfsd", /* authentication class */
144 .pg_stats = &nfsd_svcstats, /* version table */
145 .pg_authenticate = &svc_set_client, /* export authentication */
e333f3bb 146 .pg_init_request = nfsd_init_request,
029be5d0 147 .pg_rpcbind_set = nfsd_rpcbind_set,
70c3b76c
N
148};
149
e333f3bb
TM
150static bool
151nfsd_support_version(int vers)
152{
153 if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
154 return nfsd_version[vers] != NULL;
155 return false;
156}
157
158static bool *
159nfsd_alloc_versions(void)
160{
161 bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
162 unsigned i;
163
164 if (vers) {
165 /* All compiled versions are enabled by default */
166 for (i = 0; i < NFSD_NRVERS; i++)
167 vers[i] = nfsd_support_version(i);
168 }
169 return vers;
170}
171
172static bool *
173nfsd_alloc_minorversions(void)
174{
175 bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
176 sizeof(bool), GFP_KERNEL);
177 unsigned i;
8daf220a 178
e333f3bb
TM
179 if (vers) {
180 /* All minor versions are enabled by default */
181 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
182 vers[i] = nfsd_support_version(4);
183 }
184 return vers;
185}
186
187void
188nfsd_netns_free_versions(struct nfsd_net *nn)
189{
190 kfree(nn->nfsd_versions);
191 kfree(nn->nfsd4_minorversions);
192 nn->nfsd_versions = NULL;
193 nn->nfsd4_minorversions = NULL;
194}
195
196static void
197nfsd_netns_init_versions(struct nfsd_net *nn)
198{
199 if (!nn->nfsd_versions) {
200 nn->nfsd_versions = nfsd_alloc_versions();
201 nn->nfsd4_minorversions = nfsd_alloc_minorversions();
202 if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
203 nfsd_netns_free_versions(nn);
204 }
205}
206
207int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
6658d3a7
N
208{
209 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
15ddb4ae 210 return 0;
6658d3a7
N
211 switch(change) {
212 case NFSD_SET:
e333f3bb
TM
213 if (nn->nfsd_versions)
214 nn->nfsd_versions[vers] = nfsd_support_version(vers);
1a8eff6d 215 break;
6658d3a7 216 case NFSD_CLEAR:
e333f3bb
TM
217 nfsd_netns_init_versions(nn);
218 if (nn->nfsd_versions)
219 nn->nfsd_versions[vers] = false;
6658d3a7
N
220 break;
221 case NFSD_TEST:
e333f3bb
TM
222 if (nn->nfsd_versions)
223 return nn->nfsd_versions[vers];
df561f66 224 fallthrough;
6658d3a7 225 case NFSD_AVAIL:
e333f3bb 226 return nfsd_support_version(vers);
6658d3a7
N
227 }
228 return 0;
229}
8daf220a 230
d3635ff0 231static void
e333f3bb 232nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
d3635ff0
TM
233{
234 unsigned i;
235
236 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
e333f3bb 237 if (nn->nfsd4_minorversions[i])
d3635ff0
TM
238 return;
239 }
e333f3bb 240 nfsd_vers(nn, 4, NFSD_CLEAR);
d3635ff0
TM
241}
242
e333f3bb 243int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
8daf220a 244{
928c6fb3
N
245 if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
246 change != NFSD_AVAIL)
8daf220a 247 return -1;
e333f3bb 248
8daf220a
BH
249 switch(change) {
250 case NFSD_SET:
e333f3bb
TM
251 if (nn->nfsd4_minorversions) {
252 nfsd_vers(nn, 4, NFSD_SET);
253 nn->nfsd4_minorversions[minorversion] =
254 nfsd_vers(nn, 4, NFSD_TEST);
255 }
8daf220a
BH
256 break;
257 case NFSD_CLEAR:
e333f3bb
TM
258 nfsd_netns_init_versions(nn);
259 if (nn->nfsd4_minorversions) {
260 nn->nfsd4_minorversions[minorversion] = false;
261 nfsd_adjust_nfsd_versions4(nn);
262 }
8daf220a
BH
263 break;
264 case NFSD_TEST:
e333f3bb
TM
265 if (nn->nfsd4_minorversions)
266 return nn->nfsd4_minorversions[minorversion];
267 return nfsd_vers(nn, 4, NFSD_TEST);
8daf220a 268 case NFSD_AVAIL:
e333f3bb
TM
269 return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
270 nfsd_vers(nn, 4, NFSD_AVAIL);
8daf220a
BH
271 }
272 return 0;
273}
274
1da177e4
LT
275/*
276 * Maximum number of nfsd processes
277 */
278#define NFSD_MAXSERVS 8192
279
9dd9845f 280int nfsd_nrthreads(struct net *net)
1da177e4 281{
c7d106c9 282 int rv = 0;
9dd9845f
SK
283 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
284
c7d106c9 285 mutex_lock(&nfsd_mutex);
9dd9845f
SK
286 if (nn->nfsd_serv)
287 rv = nn->nfsd_serv->sv_nrthreads;
c7d106c9
NB
288 mutex_unlock(&nfsd_mutex);
289 return rv;
1da177e4
LT
290}
291
4df493a2 292static int nfsd_init_socks(struct net *net, const struct cred *cred)
59db4a0c
BF
293{
294 int error;
9dd9845f
SK
295 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
296
297 if (!list_empty(&nn->nfsd_serv->sv_permsocks))
59db4a0c
BF
298 return 0;
299
352ad314
CL
300 error = svc_xprt_create(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
301 SVC_SOCK_DEFAULTS, cred);
59db4a0c
BF
302 if (error < 0)
303 return error;
304
352ad314
CL
305 error = svc_xprt_create(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
306 SVC_SOCK_DEFAULTS, cred);
59db4a0c
BF
307 if (error < 0)
308 return error;
309
310 return 0;
311}
312
4539f149 313static int nfsd_users = 0;
4ad9a344 314
70c53075 315static int nfsd_startup_generic(void)
bda9cac1
SK
316{
317 int ret;
318
4539f149 319 if (nfsd_users++)
bda9cac1
SK
320 return 0;
321
65294c1f
JL
322 ret = nfsd_file_cache_init();
323 if (ret)
324 goto dec_users;
d9499a95 325
bda9cac1
SK
326 ret = nfs4_state_start();
327 if (ret)
501cb184 328 goto out_file_cache;
bda9cac1
SK
329 return 0;
330
65294c1f
JL
331out_file_cache:
332 nfsd_file_cache_shutdown();
d9499a95
KM
333dec_users:
334 nfsd_users--;
bda9cac1
SK
335 return ret;
336}
337
338static void nfsd_shutdown_generic(void)
339{
4539f149
SK
340 if (--nfsd_users)
341 return;
342
bda9cac1 343 nfs4_state_shutdown();
65294c1f 344 nfsd_file_cache_shutdown();
bda9cac1
SK
345}
346
e333f3bb 347static bool nfsd_needs_lockd(struct nfsd_net *nn)
8ef66714 348{
e333f3bb 349 return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
8ef66714
KM
350}
351
91d2e9b5 352/**
3988a578 353 * nfsd_copy_write_verifier - Atomically copy a write verifier
91d2e9b5
CL
354 * @verf: buffer in which to receive the verifier cookie
355 * @nn: NFS net namespace
356 *
357 * This function provides a wait-free mechanism for copying the
3988a578 358 * namespace's write verifier without tearing it.
91d2e9b5 359 */
3988a578 360void nfsd_copy_write_verifier(__be32 verf[2], struct nfsd_net *nn)
27c438f5
TM
361{
362 int seq = 0;
363
364 do {
91d2e9b5 365 read_seqbegin_or_lock(&nn->writeverf_lock, &seq);
90d21755 366 memcpy(verf, nn->writeverf, sizeof(nn->writeverf));
91d2e9b5
CL
367 } while (need_seqretry(&nn->writeverf_lock, seq));
368 done_seqretry(&nn->writeverf_lock, seq);
27c438f5
TM
369}
370
3988a578 371static void nfsd_reset_write_verifier_locked(struct nfsd_net *nn)
27c438f5 372{
91d2e9b5
CL
373 struct timespec64 now;
374 u64 verf;
375
376 /*
377 * Because the time value is hashed, y2038 time_t overflow
378 * is irrelevant in this usage.
379 */
380 ktime_get_raw_ts64(&now);
381 verf = siphash_2u64(now.tv_sec, now.tv_nsec, &nn->siphash_key);
382 memcpy(nn->writeverf, &verf, sizeof(nn->writeverf));
27c438f5
TM
383}
384
91d2e9b5 385/**
3988a578 386 * nfsd_reset_write_verifier - Generate a new write verifier
91d2e9b5
CL
387 * @nn: NFS net namespace
388 *
389 * This function updates the ->writeverf field of @nn. This field
390 * contains an opaque cookie that, according to Section 18.32.3 of
391 * RFC 8881, "the client can use to determine whether a server has
392 * changed instance state (e.g., server restart) between a call to
393 * WRITE and a subsequent call to either WRITE or COMMIT. This
394 * cookie MUST be unchanged during a single instance of the NFSv4.1
395 * server and MUST be unique between instances of the NFSv4.1
396 * server."
397 */
3988a578 398void nfsd_reset_write_verifier(struct nfsd_net *nn)
27c438f5 399{
91d2e9b5 400 write_seqlock(&nn->writeverf_lock);
3988a578 401 nfsd_reset_write_verifier_locked(nn);
91d2e9b5 402 write_sequnlock(&nn->writeverf_lock);
27c438f5
TM
403}
404
5e092be7
CL
405/*
406 * Crank up a set of per-namespace resources for a new NFSD instance,
407 * including lockd, a duplicate reply cache, an open file cache
408 * instance, and a cache of NFSv4 state objects.
409 */
70c53075 410static int nfsd_startup_net(struct net *net, const struct cred *cred)
6ff50b3d 411{
2c2fe290 412 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6ff50b3d
SK
413 int ret;
414
2c2fe290
SK
415 if (nn->nfsd_net_up)
416 return 0;
417
70c53075 418 ret = nfsd_startup_generic();
6ff50b3d
SK
419 if (ret)
420 return ret;
4df493a2 421 ret = nfsd_init_socks(net, cred);
903d9bf0
SK
422 if (ret)
423 goto out_socks;
8ef66714 424
e333f3bb 425 if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
40373b12 426 ret = lockd_up(net, cred);
8ef66714
KM
427 if (ret)
428 goto out_socks;
e44b4bf2 429 nn->lockd_up = true;
8ef66714
KM
430 }
431
9542e6a6 432 ret = nfsd_file_cache_start_net(net);
6ff50b3d
SK
433 if (ret)
434 goto out_lockd;
f5f9d4a3
JL
435
436 ret = nfsd_reply_cache_init(nn);
9542e6a6
TM
437 if (ret)
438 goto out_filecache;
6ff50b3d 439
f5f9d4a3
JL
440 ret = nfs4_state_start_net(net);
441 if (ret)
442 goto out_reply_cache;
443
f4e44b39
DN
444#ifdef CONFIG_NFSD_V4_2_INTER_SSC
445 nfsd4_ssc_init_umount_work(nn);
446#endif
2c2fe290 447 nn->nfsd_net_up = true;
6ff50b3d
SK
448 return 0;
449
f5f9d4a3
JL
450out_reply_cache:
451 nfsd_reply_cache_shutdown(nn);
9542e6a6
TM
452out_filecache:
453 nfsd_file_cache_shutdown_net(net);
6ff50b3d 454out_lockd:
8ef66714
KM
455 if (nn->lockd_up) {
456 lockd_down(net);
e44b4bf2 457 nn->lockd_up = false;
8ef66714 458 }
903d9bf0 459out_socks:
bda9cac1 460 nfsd_shutdown_generic();
4ad9a344
JL
461 return ret;
462}
463
6ff50b3d
SK
464static void nfsd_shutdown_net(struct net *net)
465{
2c2fe290
SK
466 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
467
6ff50b3d 468 nfs4_state_shutdown_net(net);
f5f9d4a3 469 nfsd_reply_cache_shutdown(nn);
789e1e10 470 nfsd_file_cache_shutdown_net(net);
8ef66714
KM
471 if (nn->lockd_up) {
472 lockd_down(net);
e44b4bf2 473 nn->lockd_up = false;
8ef66714 474 }
2c2fe290 475 nn->nfsd_net_up = false;
903d9bf0 476 nfsd_shutdown_generic();
6ff50b3d
SK
477}
478
d057cfec 479static DEFINE_SPINLOCK(nfsd_notifier_lock);
36684996
SM
480static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
481 void *ptr)
482{
483 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
484 struct net_device *dev = ifa->ifa_dev->dev;
485 struct net *net = dev_net(dev);
486 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
487 struct sockaddr_in sin;
488
d057cfec 489 if (event != NETDEV_DOWN || !nn->nfsd_serv)
36684996
SM
490 goto out;
491
d057cfec 492 spin_lock(&nfsd_notifier_lock);
36684996
SM
493 if (nn->nfsd_serv) {
494 dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
495 sin.sin_family = AF_INET;
496 sin.sin_addr.s_addr = ifa->ifa_local;
497 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
498 }
d057cfec 499 spin_unlock(&nfsd_notifier_lock);
36684996
SM
500
501out:
502 return NOTIFY_DONE;
503}
504
505static struct notifier_block nfsd_inetaddr_notifier = {
506 .notifier_call = nfsd_inetaddr_event,
507};
508
509#if IS_ENABLED(CONFIG_IPV6)
510static int nfsd_inet6addr_event(struct notifier_block *this,
511 unsigned long event, void *ptr)
512{
513 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
514 struct net_device *dev = ifa->idev->dev;
515 struct net *net = dev_net(dev);
516 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
517 struct sockaddr_in6 sin6;
518
d057cfec 519 if (event != NETDEV_DOWN || !nn->nfsd_serv)
36684996
SM
520 goto out;
521
d057cfec 522 spin_lock(&nfsd_notifier_lock);
36684996
SM
523 if (nn->nfsd_serv) {
524 dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
525 sin6.sin6_family = AF_INET6;
526 sin6.sin6_addr = ifa->addr;
7b19824d
SM
527 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
528 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
36684996
SM
529 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
530 }
d057cfec
N
531 spin_unlock(&nfsd_notifier_lock);
532
36684996
SM
533out:
534 return NOTIFY_DONE;
535}
536
537static struct notifier_block nfsd_inet6addr_notifier = {
538 .notifier_call = nfsd_inet6addr_event,
539};
540#endif
541
1eca45f8
VA
542/* Only used under nfsd_mutex, so this atomic may be overkill: */
543static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
544
9f28a971 545static void nfsd_last_thread(struct net *net)
4ad9a344 546{
903d9bf0 547 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9f28a971
N
548 struct svc_serv *serv = nn->nfsd_serv;
549
550 spin_lock(&nfsd_notifier_lock);
551 nn->nfsd_serv = NULL;
552 spin_unlock(&nfsd_notifier_lock);
903d9bf0 553
1eca45f8
VA
554 /* check if the notifier still has clients */
555 if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
556 unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
36684996 557#if IS_ENABLED(CONFIG_IPV6)
1eca45f8 558 unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
36684996 559#endif
1eca45f8
VA
560 }
561
9f28a971
N
562 svc_xprt_destroy_all(serv, net);
563
4ad9a344
JL
564 /*
565 * write_ports can create the server without actually starting
566 * any threads--if we get shut down before any threads are
567 * started, then nfsd_last_thread will be run before any of this
691412b4 568 * other initialization has been done except the rpcb information.
4ad9a344 569 */
691412b4 570 svc_rpcb_cleanup(serv, net);
903d9bf0 571 if (!nn->nfsd_net_up)
4ad9a344 572 return;
16d05870 573
691412b4 574 nfsd_shutdown_net(net);
b3853e0e 575 nfsd_export_flush(net);
bc591ccf 576}
6658d3a7 577
e333f3bb 578void nfsd_reset_versions(struct nfsd_net *nn)
6658d3a7 579{
6658d3a7
N
580 int i;
581
800a938f 582 for (i = 0; i < NFSD_NRVERS; i++)
e333f3bb 583 if (nfsd_vers(nn, i, NFSD_TEST))
800a938f 584 return;
6658d3a7 585
800a938f
N
586 for (i = 0; i < NFSD_NRVERS; i++)
587 if (i != 4)
e333f3bb 588 nfsd_vers(nn, i, NFSD_SET);
800a938f
N
589 else {
590 int minor = 0;
e333f3bb 591 while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
800a938f
N
592 minor++;
593 }
6658d3a7
N
594}
595
c3d06f9c
AA
596/*
597 * Each session guarantees a negotiated per slot memory cache for replies
598 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
599 * NFSv4.1 server might want to use more memory for a DRC than a machine
600 * with mutiple services.
601 *
602 * Impose a hard limit on the number of pages for the DRC which varies
603 * according to the machines free pages. This is of course only a default.
604 *
605 * For now this is a #defined shift which could be under admin control
606 * in the future.
607 */
608static void set_max_drc(void)
609{
44d8660d 610 #define NFSD_DRC_SIZE_SHIFT 7
0c193054
AA
611 nfsd_drc_max_mem = (nr_free_buffer_pages()
612 >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
613 nfsd_drc_mem_used = 0;
697ce9be 614 dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
c3d06f9c 615}
bedbdd8b 616
87b0fc7d 617static int nfsd_get_default_max_blksize(void)
02a375f0 618{
87b0fc7d
BF
619 struct sysinfo i;
620 unsigned long long target;
621 unsigned long ret;
bedbdd8b 622
87b0fc7d 623 si_meminfo(&i);
508f9227 624 target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
87b0fc7d
BF
625 /*
626 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
627 * machines, but only uses 32K on 128M machines. Bottom out at
628 * 8K on 32M and smaller. Of course, this is only a default.
629 */
630 target >>= 12;
631
632 ret = NFSSVC_MAXBLKSIZE;
633 while (ret > target && ret >= 8*1024*2)
634 ret /= 2;
635 return ret;
636}
637
c6c7f2a8
TM
638void nfsd_shutdown_threads(struct net *net)
639{
640 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
641 struct svc_serv *serv;
642
643 mutex_lock(&nfsd_mutex);
644 serv = nn->nfsd_serv;
645 if (serv == NULL) {
646 mutex_unlock(&nfsd_mutex);
647 return;
648 }
649
650 svc_get(serv);
651 /* Kill outstanding nfsd threads */
3ebdbe52 652 svc_set_num_threads(serv, NULL, 0);
9f28a971
N
653 nfsd_last_thread(net);
654 svc_put(serv);
c6c7f2a8 655 mutex_unlock(&nfsd_mutex);
c6c7f2a8
TM
656}
657
44fb26c6 658bool i_am_nfsd(void)
28df3d15
BF
659{
660 return kthread_func(current) == nfsd;
661}
662
6777436b 663int nfsd_create_serv(struct net *net)
87b0fc7d 664{
9793f7c8 665 int error;
b9c0ef85 666 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
d057cfec 667 struct svc_serv *serv;
9793f7c8 668
bedbdd8b 669 WARN_ON(!mutex_is_locked(&nfsd_mutex));
9dd9845f
SK
670 if (nn->nfsd_serv) {
671 svc_get(nn->nfsd_serv);
02a375f0
N
672 return 0;
673 }
87b0fc7d
BF
674 if (nfsd_max_blksize == 0)
675 nfsd_max_blksize = nfsd_get_default_max_blksize();
e333f3bb 676 nfsd_reset_versions(nn);
37902c63 677 serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize, nfsd);
d057cfec 678 if (serv == NULL)
628b3687 679 return -ENOMEM;
bedbdd8b 680
d057cfec
N
681 serv->sv_maxconn = nn->max_connections;
682 error = svc_bind(serv, net);
9793f7c8 683 if (error < 0) {
d057cfec 684 svc_put(serv);
9793f7c8
SK
685 return error;
686 }
d057cfec
N
687 spin_lock(&nfsd_notifier_lock);
688 nn->nfsd_serv = serv;
689 spin_unlock(&nfsd_notifier_lock);
9793f7c8 690
628b3687 691 set_max_drc();
1eca45f8
VA
692 /* check if the notifier is already set */
693 if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
694 register_inetaddr_notifier(&nfsd_inetaddr_notifier);
36684996 695#if IS_ENABLED(CONFIG_IPV6)
1eca45f8 696 register_inet6addr_notifier(&nfsd_inet6addr_notifier);
36684996 697#endif
1eca45f8 698 }
3988a578 699 nfsd_reset_write_verifier(nn);
87b0fc7d 700 return 0;
02a375f0
N
701}
702
9dd9845f 703int nfsd_nrpools(struct net *net)
eed2965a 704{
9dd9845f
SK
705 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
706
707 if (nn->nfsd_serv == NULL)
eed2965a
GB
708 return 0;
709 else
9dd9845f 710 return nn->nfsd_serv->sv_nrpools;
eed2965a
GB
711}
712
9dd9845f 713int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
eed2965a 714{
9dd9845f 715 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2e8fc923
N
716 struct svc_serv *serv = nn->nfsd_serv;
717 int i;
eed2965a 718
2e8fc923
N
719 if (serv)
720 for (i = 0; i < serv->sv_nrpools && i < n; i++)
721 nthreads[i] = atomic_read(&serv->sv_pools[i].sp_nrthreads);
eed2965a
GB
722 return 0;
723}
724
3938a0d5 725int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
eed2965a
GB
726{
727 int i = 0;
728 int tot = 0;
729 int err = 0;
9dd9845f 730 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
eed2965a 731
bedbdd8b
NB
732 WARN_ON(!mutex_is_locked(&nfsd_mutex));
733
9dd9845f 734 if (nn->nfsd_serv == NULL || n <= 0)
eed2965a
GB
735 return 0;
736
9dd9845f
SK
737 if (n > nn->nfsd_serv->sv_nrpools)
738 n = nn->nfsd_serv->sv_nrpools;
eed2965a
GB
739
740 /* enforce a global maximum number of threads */
741 tot = 0;
742 for (i = 0; i < n; i++) {
3c7aa15d 743 nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
eed2965a
GB
744 tot += nthreads[i];
745 }
746 if (tot > NFSD_MAXSERVS) {
747 /* total too large: scale down requested numbers */
748 for (i = 0; i < n && tot > 0; i++) {
3409e4f1 749 int new = nthreads[i] * NFSD_MAXSERVS / tot;
eed2965a
GB
750 tot -= (nthreads[i] - new);
751 nthreads[i] = new;
752 }
753 for (i = 0; i < n && tot > 0; i++) {
754 nthreads[i]--;
755 tot--;
756 }
757 }
758
759 /*
760 * There must always be a thread in pool 0; the admin
761 * can't shut down NFS completely using pool_threads.
762 */
763 if (nthreads[0] == 0)
764 nthreads[0] = 1;
765
766 /* apply the new numbers */
9dd9845f 767 svc_get(nn->nfsd_serv);
eed2965a 768 for (i = 0; i < n; i++) {
3ebdbe52
N
769 err = svc_set_num_threads(nn->nfsd_serv,
770 &nn->nfsd_serv->sv_pools[i],
771 nthreads[i]);
eed2965a
GB
772 if (err)
773 break;
774 }
9f28a971 775 svc_put(nn->nfsd_serv);
eed2965a
GB
776 return err;
777}
778
ac77efbe
JL
779/*
780 * Adjust the number of threads and return the new number of threads.
781 * This is also the function that starts the server if necessary, if
782 * this is the first time nrservs is nonzero.
783 */
1da177e4 784int
4df493a2 785nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
1da177e4
LT
786{
787 int error;
9dd9845f 788 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9f28a971 789 struct svc_serv *serv;
bedbdd8b
NB
790
791 mutex_lock(&nfsd_mutex);
6658d3a7 792 dprintk("nfsd: creating service\n");
3c7aa15d
KM
793
794 nrservs = max(nrservs, 0);
795 nrservs = min(nrservs, NFSD_MAXSERVS);
671e1fcf 796 error = 0;
3c7aa15d 797
9dd9845f 798 if (nrservs == 0 && nn->nfsd_serv == NULL)
671e1fcf
N
799 goto out;
800
72f78ae0 801 strscpy(nn->nfsd_name, utsname()->nodename,
7627d7dc
SM
802 sizeof(nn->nfsd_name));
803
6777436b 804 error = nfsd_create_serv(net);
02a375f0 805 if (error)
774f8bbd 806 goto out;
9f28a971 807 serv = nn->nfsd_serv;
774f8bbd 808
70c53075 809 error = nfsd_startup_net(net, cred);
af4718f3 810 if (error)
8c62d127 811 goto out_put;
9f28a971 812 error = svc_set_num_threads(serv, NULL, nrservs);
774f8bbd 813 if (error)
bf320752 814 goto out_put;
9f28a971 815 error = serv->sv_nrthreads;
8c62d127 816out_put:
ec52361d
N
817 /* Threads now hold service active */
818 if (xchg(&nn->keep_active, 0))
9f28a971 819 svc_put(serv);
bf320752
N
820
821 if (serv->sv_nrthreads == 0)
822 nfsd_last_thread(net);
9f28a971 823 svc_put(serv);
4ad9a344 824out:
bedbdd8b 825 mutex_unlock(&nfsd_mutex);
1da177e4
LT
826 return error;
827}
828
029be5d0
TM
829#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
830static bool
831nfsd_support_acl_version(int vers)
832{
833 if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
834 return nfsd_acl_version[vers] != NULL;
835 return false;
836}
837
838static int
839nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
840 u32 version, int family, unsigned short proto,
841 unsigned short port)
842{
843 if (!nfsd_support_acl_version(version) ||
e333f3bb 844 !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
029be5d0
TM
845 return 0;
846 return svc_generic_rpcbind_set(net, progp, version, family,
847 proto, port);
848}
e333f3bb
TM
849
850static __be32
851nfsd_acl_init_request(struct svc_rqst *rqstp,
852 const struct svc_program *progp,
853 struct svc_process_info *ret)
854{
855 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
856 int i;
857
858 if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
859 nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
860 return svc_generic_init_request(rqstp, progp, ret);
861
862 ret->mismatch.lovers = NFSD_ACL_NRVERS;
863 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
864 if (nfsd_support_acl_version(rqstp->rq_vers) &&
865 nfsd_vers(nn, i, NFSD_TEST)) {
866 ret->mismatch.lovers = i;
867 break;
868 }
869 }
870 if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
871 return rpc_prog_unavail;
872 ret->mismatch.hivers = NFSD_ACL_MINVERS;
873 for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
874 if (nfsd_support_acl_version(rqstp->rq_vers) &&
875 nfsd_vers(nn, i, NFSD_TEST)) {
876 ret->mismatch.hivers = i;
877 break;
878 }
879 }
880 return rpc_prog_mismatch;
881}
029be5d0
TM
882#endif
883
884static int
885nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
886 u32 version, int family, unsigned short proto,
887 unsigned short port)
888{
e333f3bb 889 if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
029be5d0
TM
890 return 0;
891 return svc_generic_rpcbind_set(net, progp, version, family,
892 proto, port);
893}
1da177e4 894
e333f3bb
TM
895static __be32
896nfsd_init_request(struct svc_rqst *rqstp,
897 const struct svc_program *progp,
898 struct svc_process_info *ret)
899{
900 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
901 int i;
902
903 if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
904 return svc_generic_init_request(rqstp, progp, ret);
905
906 ret->mismatch.lovers = NFSD_NRVERS;
907 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
908 if (nfsd_vers(nn, i, NFSD_TEST)) {
909 ret->mismatch.lovers = i;
910 break;
911 }
912 }
913 if (ret->mismatch.lovers == NFSD_NRVERS)
914 return rpc_prog_unavail;
915 ret->mismatch.hivers = NFSD_MINVERS;
916 for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
917 if (nfsd_vers(nn, i, NFSD_TEST)) {
918 ret->mismatch.hivers = i;
919 break;
920 }
921 }
922 return rpc_prog_mismatch;
923}
924
1da177e4
LT
925/*
926 * This is the NFS server kernel thread
927 */
9867d76c
JL
928static int
929nfsd(void *vrqstp)
1da177e4 930{
9867d76c 931 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
88c47666
SK
932 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
933 struct net *net = perm_sock->xpt_net;
5b8db00b 934 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 935
9867d76c 936 /* At this point, the thread shares current->fs
47057abd
AG
937 * with the init process. We need to create files with the
938 * umask as defined by the client instead of init's umask. */
3e93cd67 939 if (unshare_fs_struct() < 0) {
1da177e4
LT
940 printk("Unable to start nfsd thread: out of memory\n");
941 goto out;
942 }
3e93cd67 943
1da177e4
LT
944 current->fs->umask = 0;
945
9b6c8c9b 946 atomic_inc(&nfsdstats.th_cnt);
bedbdd8b 947
83144186 948 set_freezable();
1da177e4
LT
949
950 /*
951 * The main request loop
952 */
fa341560 953 while (!svc_thread_should_stop(rqstp)) {
5b8db00b
JL
954 /* Update sv_maxconn if it has changed */
955 rqstp->rq_server->sv_maxconn = nn->max_connections;
956
c743b425 957 svc_recv(rqstp);
e0e81739 958 validate_process_creds();
1da177e4
LT
959 }
960
9b6c8c9b 961 atomic_dec(&nfsdstats.th_cnt);
1da177e4
LT
962
963out:
964 /* Release the thread */
965 svc_exit_thread(rqstp);
9867d76c 966 return 0;
1da177e4
LT
967}
968
383c440d
CL
969/**
970 * nfsd_dispatch - Process an NFS or NFSACL Request
971 * @rqstp: incoming request
383c440d
CL
972 *
973 * This RPC dispatcher integrates the NFS server's duplicate reply cache.
974 *
975 * Return values:
976 * %0: Processing complete; do not send a Reply
977 * %1: Processing complete; send Reply in rqstp->rq_res
978 */
cee4db19 979int nfsd_dispatch(struct svc_rqst *rqstp)
1da177e4 980{
4c96cb56 981 const struct svc_procedure *proc = rqstp->rq_procinfo;
cee4db19 982 __be32 *statp = rqstp->rq_accept_statp;
e7421ce7 983 struct nfsd_cacherep *rp;
1da177e4 984
1091006c
BF
985 /*
986 * Give the xdr decoder a chance to change this if it wants
987 * (necessary in the NFSv4.0 compound case)
988 */
989 rqstp->rq_cachetype = proc->pc_cachetype;
5191955d 990
16c66364 991 if (!proc->pc_decode(rqstp, &rqstp->rq_arg_stream))
85085aac 992 goto out_decode_err;
1091006c 993
bd9d6a3e
LB
994 /*
995 * Release rq_status_counter setting it to an odd value after the rpc
996 * request has been properly parsed. rq_status_counter is used to
997 * notify the consumers if the rqstp fields are stable
998 * (rq_status_counter is odd) or not meaningful (rq_status_counter
999 * is even).
1000 */
1001 smp_store_release(&rqstp->rq_status_counter, rqstp->rq_status_counter | 1);
1002
cb18eca4
CL
1003 rp = NULL;
1004 switch (nfsd_cache_lookup(rqstp, &rp)) {
84c138e7
CL
1005 case RC_DOIT:
1006 break;
1da177e4 1007 case RC_REPLY:
85085aac 1008 goto out_cached_reply;
84c138e7 1009 case RC_DROPIT:
85085aac 1010 goto out_dropit;
1da177e4
LT
1011 }
1012
cc028a10 1013 *statp = proc->pc_func(rqstp);
93155647 1014 if (test_bit(RQ_DROPME, &rqstp->rq_flags))
85085aac 1015 goto out_update_drop;
1da177e4 1016
fda49441 1017 if (!proc->pc_encode(rqstp, &rqstp->rq_res_stream))
f0af2210 1018 goto out_encode_err;
1da177e4 1019
bd9d6a3e
LB
1020 /*
1021 * Release rq_status_counter setting it to an even value after the rpc
1022 * request has been properly processed.
1023 */
1024 smp_store_release(&rqstp->rq_status_counter, rqstp->rq_status_counter + 1);
1025
cb18eca4 1026 nfsd_cache_update(rqstp, rp, rqstp->rq_cachetype, statp + 1);
85085aac
CL
1027out_cached_reply:
1028 return 1;
1029
85085aac 1030out_decode_err:
0dfdad1c 1031 trace_nfsd_garbage_args_err(rqstp);
85085aac
CL
1032 *statp = rpc_garbage_args;
1033 return 1;
1034
1035out_update_drop:
cb18eca4 1036 nfsd_cache_update(rqstp, rp, RC_NOCACHE, NULL);
85085aac
CL
1037out_dropit:
1038 return 0;
1039
1040out_encode_err:
0dfdad1c 1041 trace_nfsd_cant_encode_err(rqstp);
cb18eca4 1042 nfsd_cache_update(rqstp, rp, RC_NOCACHE, NULL);
85085aac 1043 *statp = rpc_system_err;
1da177e4
LT
1044 return 1;
1045}
03cf6c9f 1046
788f7183
CL
1047/**
1048 * nfssvc_decode_voidarg - Decode void arguments
1049 * @rqstp: Server RPC transaction context
16c66364 1050 * @xdr: XDR stream positioned at arguments to decode
788f7183
CL
1051 *
1052 * Return values:
c44b31c2
CL
1053 * %false: Arguments were not valid
1054 * %true: Decoding was successful
788f7183 1055 */
c44b31c2 1056bool nfssvc_decode_voidarg(struct svc_rqst *rqstp, struct xdr_stream *xdr)
788f7183 1057{
291cd656 1058 return true;
788f7183
CL
1059}
1060
1061/**
1062 * nfssvc_encode_voidres - Encode void results
1063 * @rqstp: Server RPC transaction context
fda49441 1064 * @xdr: XDR stream into which to encode results
788f7183
CL
1065 *
1066 * Return values:
130e2054
CL
1067 * %false: Local error while encoding
1068 * %true: Encoding was successful
788f7183 1069 */
130e2054 1070bool nfssvc_encode_voidres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
788f7183 1071{
130e2054 1072 return true;
788f7183
CL
1073}
1074
03cf6c9f
GB
1075int nfsd_pool_stats_open(struct inode *inode, struct file *file)
1076{
ed2d8aed 1077 int ret;
11f77942 1078 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
9dd9845f 1079
ed2d8aed 1080 mutex_lock(&nfsd_mutex);
9dd9845f 1081 if (nn->nfsd_serv == NULL) {
ed2d8aed 1082 mutex_unlock(&nfsd_mutex);
03cf6c9f 1083 return -ENODEV;
ed2d8aed 1084 }
9dd9845f
SK
1085 svc_get(nn->nfsd_serv);
1086 ret = svc_pool_stats_open(nn->nfsd_serv, file);
ed2d8aed
RY
1087 mutex_unlock(&nfsd_mutex);
1088 return ret;
1089}
1090
1091int nfsd_pool_stats_release(struct inode *inode, struct file *file)
1092{
88956eab
N
1093 struct seq_file *seq = file->private_data;
1094 struct svc_serv *serv = seq->private;
ed2d8aed 1095 int ret = seq_release(inode, file);
786185b5 1096
ed2d8aed 1097 mutex_lock(&nfsd_mutex);
88956eab 1098 svc_put(serv);
ed2d8aed
RY
1099 mutex_unlock(&nfsd_mutex);
1100 return ret;
03cf6c9f 1101}