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