Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Central processing for nfsd. |
3 | * | |
4 | * Authors: Olaf Kirch (okir@monad.swb.de) | |
5 | * | |
6 | * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de> | |
7 | */ | |
8 | ||
e8edc6e0 | 9 | #include <linux/sched.h> |
83144186 | 10 | #include <linux/freezer.h> |
143cb494 | 11 | #include <linux/module.h> |
1da177e4 | 12 | #include <linux/fs_struct.h> |
c3d06f9c | 13 | #include <linux/swap.h> |
1da177e4 | 14 | |
1da177e4 | 15 | #include <linux/sunrpc/stats.h> |
1da177e4 | 16 | #include <linux/sunrpc/svcsock.h> |
1da177e4 | 17 | #include <linux/lockd/bind.h> |
a257cdd0 | 18 | #include <linux/nfsacl.h> |
ed2d8aed | 19 | #include <linux/seq_file.h> |
fc5d00b0 | 20 | #include <net/net_namespace.h> |
9a74af21 BH |
21 | #include "nfsd.h" |
22 | #include "cache.h" | |
0a3adade | 23 | #include "vfs.h" |
2c2fe290 | 24 | #include "netns.h" |
1da177e4 LT |
25 | |
26 | #define NFSDDBG_FACILITY NFSDDBG_SVC | |
27 | ||
1da177e4 | 28 | extern struct svc_program nfsd_program; |
9867d76c | 29 | static int nfsd(void *vrqstp); |
1da177e4 | 30 | |
bedbdd8b | 31 | /* |
9dd9845f | 32 | * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members |
bedbdd8b NB |
33 | * of the svc_serv struct. In particular, ->sv_nrthreads but also to some |
34 | * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt | |
35 | * | |
9dd9845f | 36 | * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a |
bedbdd8b NB |
37 | * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number |
38 | * of nfsd threads must exist and each must listed in ->sp_all_threads in each | |
39 | * entry of ->sv_pools[]. | |
40 | * | |
41 | * Transitions of the thread count between zero and non-zero are of particular | |
42 | * interest since the svc_serv needs to be created and initialized at that | |
43 | * point, or freed. | |
3dd98a3b JL |
44 | * |
45 | * Finally, the nfsd_mutex also protects some of the global variables that are | |
46 | * accessed when nfsd starts and that are settable via the write_* routines in | |
47 | * nfsctl.c. In particular: | |
48 | * | |
49 | * user_recovery_dirname | |
50 | * user_lease_time | |
51 | * nfsd_versions | |
bedbdd8b NB |
52 | */ |
53 | DEFINE_MUTEX(nfsd_mutex); | |
bedbdd8b | 54 | |
4bd9b0f4 AA |
55 | /* |
56 | * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used. | |
57 | * nfsd_drc_max_pages limits the total amount of memory available for | |
58 | * version 4.1 DRC caches. | |
59 | * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage. | |
60 | */ | |
61 | spinlock_t nfsd_drc_lock; | |
0c193054 AA |
62 | unsigned int nfsd_drc_max_mem; |
63 | unsigned int nfsd_drc_mem_used; | |
4bd9b0f4 | 64 | |
3fb803a9 AG |
65 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) |
66 | static struct svc_stat nfsd_acl_svcstats; | |
67 | static struct svc_version * nfsd_acl_version[] = { | |
68 | [2] = &nfsd_acl_version2, | |
69 | [3] = &nfsd_acl_version3, | |
70 | }; | |
71 | ||
72 | #define NFSD_ACL_MINVERS 2 | |
e8c96f8c | 73 | #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version) |
3fb803a9 AG |
74 | static struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS]; |
75 | ||
76 | static struct svc_program nfsd_acl_program = { | |
77 | .pg_prog = NFS_ACL_PROGRAM, | |
78 | .pg_nvers = NFSD_ACL_NRVERS, | |
79 | .pg_vers = nfsd_acl_versions, | |
1a8eff6d | 80 | .pg_name = "nfsacl", |
3fb803a9 AG |
81 | .pg_class = "nfsd", |
82 | .pg_stats = &nfsd_acl_svcstats, | |
83 | .pg_authenticate = &svc_set_client, | |
84 | }; | |
85 | ||
86 | static struct svc_stat nfsd_acl_svcstats = { | |
87 | .program = &nfsd_acl_program, | |
88 | }; | |
89 | #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */ | |
90 | ||
70c3b76c N |
91 | static struct svc_version * nfsd_version[] = { |
92 | [2] = &nfsd_version2, | |
93 | #if defined(CONFIG_NFSD_V3) | |
94 | [3] = &nfsd_version3, | |
95 | #endif | |
96 | #if defined(CONFIG_NFSD_V4) | |
97 | [4] = &nfsd_version4, | |
98 | #endif | |
99 | }; | |
100 | ||
101 | #define NFSD_MINVERS 2 | |
e8c96f8c | 102 | #define NFSD_NRVERS ARRAY_SIZE(nfsd_version) |
70c3b76c N |
103 | static struct svc_version *nfsd_versions[NFSD_NRVERS]; |
104 | ||
105 | struct svc_program nfsd_program = { | |
3fb803a9 AG |
106 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) |
107 | .pg_next = &nfsd_acl_program, | |
108 | #endif | |
70c3b76c N |
109 | .pg_prog = NFS_PROGRAM, /* program number */ |
110 | .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */ | |
111 | .pg_vers = nfsd_versions, /* version table */ | |
112 | .pg_name = "nfsd", /* program name */ | |
113 | .pg_class = "nfsd", /* authentication class */ | |
114 | .pg_stats = &nfsd_svcstats, /* version table */ | |
115 | .pg_authenticate = &svc_set_client, /* export authentication */ | |
116 | ||
117 | }; | |
118 | ||
8daf220a BH |
119 | u32 nfsd_supported_minorversion; |
120 | ||
6658d3a7 N |
121 | int nfsd_vers(int vers, enum vers_op change) |
122 | { | |
123 | if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS) | |
15ddb4ae | 124 | return 0; |
6658d3a7 N |
125 | switch(change) { |
126 | case NFSD_SET: | |
127 | nfsd_versions[vers] = nfsd_version[vers]; | |
6658d3a7 N |
128 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) |
129 | if (vers < NFSD_ACL_NRVERS) | |
1a8eff6d | 130 | nfsd_acl_versions[vers] = nfsd_acl_version[vers]; |
6658d3a7 | 131 | #endif |
1a8eff6d | 132 | break; |
6658d3a7 N |
133 | case NFSD_CLEAR: |
134 | nfsd_versions[vers] = NULL; | |
135 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) | |
136 | if (vers < NFSD_ACL_NRVERS) | |
1a8eff6d | 137 | nfsd_acl_versions[vers] = NULL; |
6658d3a7 N |
138 | #endif |
139 | break; | |
140 | case NFSD_TEST: | |
141 | return nfsd_versions[vers] != NULL; | |
142 | case NFSD_AVAIL: | |
143 | return nfsd_version[vers] != NULL; | |
144 | } | |
145 | return 0; | |
146 | } | |
8daf220a BH |
147 | |
148 | int nfsd_minorversion(u32 minorversion, enum vers_op change) | |
149 | { | |
150 | if (minorversion > NFSD_SUPPORTED_MINOR_VERSION) | |
151 | return -1; | |
152 | switch(change) { | |
153 | case NFSD_SET: | |
154 | nfsd_supported_minorversion = minorversion; | |
155 | break; | |
156 | case NFSD_CLEAR: | |
157 | if (minorversion == 0) | |
158 | return -1; | |
159 | nfsd_supported_minorversion = minorversion - 1; | |
160 | break; | |
161 | case NFSD_TEST: | |
162 | return minorversion <= nfsd_supported_minorversion; | |
163 | case NFSD_AVAIL: | |
164 | return minorversion <= NFSD_SUPPORTED_MINOR_VERSION; | |
165 | } | |
166 | return 0; | |
167 | } | |
168 | ||
1da177e4 LT |
169 | /* |
170 | * Maximum number of nfsd processes | |
171 | */ | |
172 | #define NFSD_MAXSERVS 8192 | |
173 | ||
9dd9845f | 174 | int nfsd_nrthreads(struct net *net) |
1da177e4 | 175 | { |
c7d106c9 | 176 | int rv = 0; |
9dd9845f SK |
177 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
178 | ||
c7d106c9 | 179 | mutex_lock(&nfsd_mutex); |
9dd9845f SK |
180 | if (nn->nfsd_serv) |
181 | rv = nn->nfsd_serv->sv_nrthreads; | |
c7d106c9 NB |
182 | mutex_unlock(&nfsd_mutex); |
183 | return rv; | |
1da177e4 LT |
184 | } |
185 | ||
db6e182c | 186 | static int nfsd_init_socks(struct net *net) |
59db4a0c BF |
187 | { |
188 | int error; | |
9dd9845f SK |
189 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
190 | ||
191 | if (!list_empty(&nn->nfsd_serv->sv_permsocks)) | |
59db4a0c BF |
192 | return 0; |
193 | ||
9dd9845f | 194 | error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT, |
59db4a0c BF |
195 | SVC_SOCK_DEFAULTS); |
196 | if (error < 0) | |
197 | return error; | |
198 | ||
9dd9845f | 199 | error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT, |
59db4a0c BF |
200 | SVC_SOCK_DEFAULTS); |
201 | if (error < 0) | |
202 | return error; | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
4ad9a344 JL |
207 | static bool nfsd_up = false; |
208 | ||
bda9cac1 SK |
209 | static int nfsd_startup_generic(int nrservs) |
210 | { | |
211 | int ret; | |
212 | ||
213 | if (nfsd_up) | |
214 | return 0; | |
215 | ||
216 | /* | |
217 | * Readahead param cache - will no-op if it already exists. | |
218 | * (Note therefore results will be suboptimal if number of | |
219 | * threads is modified after nfsd start.) | |
220 | */ | |
221 | ret = nfsd_racache_init(2*nrservs); | |
222 | if (ret) | |
223 | return ret; | |
224 | ret = nfs4_state_start(); | |
225 | if (ret) | |
226 | goto out_racache; | |
227 | return 0; | |
228 | ||
229 | out_racache: | |
230 | nfsd_racache_shutdown(); | |
231 | return ret; | |
232 | } | |
233 | ||
234 | static void nfsd_shutdown_generic(void) | |
235 | { | |
236 | nfs4_state_shutdown(); | |
237 | nfsd_racache_shutdown(); | |
903d9bf0 | 238 | nfsd_up = false; |
bda9cac1 SK |
239 | } |
240 | ||
903d9bf0 | 241 | static int nfsd_startup_net(int nrservs, struct net *net) |
6ff50b3d | 242 | { |
2c2fe290 | 243 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
6ff50b3d SK |
244 | int ret; |
245 | ||
2c2fe290 SK |
246 | if (nn->nfsd_net_up) |
247 | return 0; | |
248 | ||
903d9bf0 | 249 | ret = nfsd_startup_generic(nrservs); |
6ff50b3d SK |
250 | if (ret) |
251 | return ret; | |
903d9bf0 SK |
252 | ret = nfsd_init_socks(net); |
253 | if (ret) | |
254 | goto out_socks; | |
6ff50b3d SK |
255 | ret = lockd_up(net); |
256 | if (ret) | |
903d9bf0 | 257 | goto out_socks; |
6ff50b3d SK |
258 | ret = nfs4_state_start_net(net); |
259 | if (ret) | |
260 | goto out_lockd; | |
261 | ||
2c2fe290 | 262 | nn->nfsd_net_up = true; |
903d9bf0 | 263 | nfsd_up = true; |
6ff50b3d SK |
264 | return 0; |
265 | ||
266 | out_lockd: | |
267 | lockd_down(net); | |
903d9bf0 | 268 | out_socks: |
bda9cac1 | 269 | nfsd_shutdown_generic(); |
4ad9a344 JL |
270 | return ret; |
271 | } | |
272 | ||
6ff50b3d SK |
273 | static void nfsd_shutdown_net(struct net *net) |
274 | { | |
2c2fe290 SK |
275 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
276 | ||
6ff50b3d SK |
277 | nfs4_state_shutdown_net(net); |
278 | lockd_down(net); | |
2c2fe290 | 279 | nn->nfsd_net_up = false; |
903d9bf0 | 280 | nfsd_shutdown_generic(); |
6ff50b3d SK |
281 | } |
282 | ||
db42d1a7 | 283 | static void nfsd_shutdown(struct net *net) |
4ad9a344 | 284 | { |
903d9bf0 SK |
285 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
286 | ||
4ad9a344 JL |
287 | /* |
288 | * write_ports can create the server without actually starting | |
289 | * any threads--if we get shut down before any threads are | |
290 | * started, then nfsd_last_thread will be run before any of this | |
291 | * other initialization has been done. | |
292 | */ | |
903d9bf0 | 293 | if (!nn->nfsd_net_up) |
4ad9a344 | 294 | return; |
6ff50b3d | 295 | nfsd_shutdown_net(net); |
4ad9a344 JL |
296 | } |
297 | ||
5ecebb7c | 298 | static void nfsd_last_thread(struct svc_serv *serv, struct net *net) |
bc591ccf | 299 | { |
db42d1a7 | 300 | nfsd_shutdown(net); |
bc591ccf | 301 | |
5ecebb7c | 302 | svc_rpcb_cleanup(serv, net); |
16d05870 | 303 | |
e096bbc6 JL |
304 | printk(KERN_WARNING "nfsd: last server has exited, flushing export " |
305 | "cache\n"); | |
b3853e0e | 306 | nfsd_export_flush(net); |
bc591ccf | 307 | } |
6658d3a7 N |
308 | |
309 | void nfsd_reset_versions(void) | |
310 | { | |
311 | int found_one = 0; | |
312 | int i; | |
313 | ||
314 | for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) { | |
315 | if (nfsd_program.pg_vers[i]) | |
316 | found_one = 1; | |
317 | } | |
318 | ||
319 | if (!found_one) { | |
320 | for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) | |
321 | nfsd_program.pg_vers[i] = nfsd_version[i]; | |
322 | #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) | |
323 | for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) | |
324 | nfsd_acl_program.pg_vers[i] = | |
325 | nfsd_acl_version[i]; | |
326 | #endif | |
327 | } | |
328 | } | |
329 | ||
c3d06f9c AA |
330 | /* |
331 | * Each session guarantees a negotiated per slot memory cache for replies | |
332 | * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated | |
333 | * NFSv4.1 server might want to use more memory for a DRC than a machine | |
334 | * with mutiple services. | |
335 | * | |
336 | * Impose a hard limit on the number of pages for the DRC which varies | |
337 | * according to the machines free pages. This is of course only a default. | |
338 | * | |
339 | * For now this is a #defined shift which could be under admin control | |
340 | * in the future. | |
341 | */ | |
342 | static void set_max_drc(void) | |
343 | { | |
6a14dd1a | 344 | #define NFSD_DRC_SIZE_SHIFT 10 |
0c193054 AA |
345 | nfsd_drc_max_mem = (nr_free_buffer_pages() |
346 | >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE; | |
347 | nfsd_drc_mem_used = 0; | |
4bd9b0f4 | 348 | spin_lock_init(&nfsd_drc_lock); |
0c193054 | 349 | dprintk("%s nfsd_drc_max_mem %u \n", __func__, nfsd_drc_max_mem); |
c3d06f9c | 350 | } |
bedbdd8b | 351 | |
87b0fc7d | 352 | static int nfsd_get_default_max_blksize(void) |
02a375f0 | 353 | { |
87b0fc7d BF |
354 | struct sysinfo i; |
355 | unsigned long long target; | |
356 | unsigned long ret; | |
bedbdd8b | 357 | |
87b0fc7d | 358 | si_meminfo(&i); |
508f9227 | 359 | target = (i.totalram - i.totalhigh) << PAGE_SHIFT; |
87b0fc7d BF |
360 | /* |
361 | * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig | |
362 | * machines, but only uses 32K on 128M machines. Bottom out at | |
363 | * 8K on 32M and smaller. Of course, this is only a default. | |
364 | */ | |
365 | target >>= 12; | |
366 | ||
367 | ret = NFSSVC_MAXBLKSIZE; | |
368 | while (ret > target && ret >= 8*1024*2) | |
369 | ret /= 2; | |
370 | return ret; | |
371 | } | |
372 | ||
6777436b | 373 | int nfsd_create_serv(struct net *net) |
87b0fc7d | 374 | { |
9793f7c8 | 375 | int error; |
b9c0ef85 | 376 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
9793f7c8 | 377 | |
bedbdd8b | 378 | WARN_ON(!mutex_is_locked(&nfsd_mutex)); |
9dd9845f SK |
379 | if (nn->nfsd_serv) { |
380 | svc_get(nn->nfsd_serv); | |
02a375f0 N |
381 | return 0; |
382 | } | |
87b0fc7d BF |
383 | if (nfsd_max_blksize == 0) |
384 | nfsd_max_blksize = nfsd_get_default_max_blksize(); | |
e844a7b9 | 385 | nfsd_reset_versions(); |
9dd9845f | 386 | nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize, |
a75c5d01 | 387 | nfsd_last_thread, nfsd, THIS_MODULE); |
9dd9845f | 388 | if (nn->nfsd_serv == NULL) |
628b3687 | 389 | return -ENOMEM; |
bedbdd8b | 390 | |
9dd9845f | 391 | error = svc_bind(nn->nfsd_serv, net); |
9793f7c8 | 392 | if (error < 0) { |
9dd9845f | 393 | svc_destroy(nn->nfsd_serv); |
9793f7c8 SK |
394 | return error; |
395 | } | |
396 | ||
628b3687 | 397 | set_max_drc(); |
b9c0ef85 | 398 | do_gettimeofday(&nn->nfssvc_boot); /* record boot time */ |
87b0fc7d | 399 | return 0; |
02a375f0 N |
400 | } |
401 | ||
9dd9845f | 402 | int nfsd_nrpools(struct net *net) |
eed2965a | 403 | { |
9dd9845f SK |
404 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
405 | ||
406 | if (nn->nfsd_serv == NULL) | |
eed2965a GB |
407 | return 0; |
408 | else | |
9dd9845f | 409 | return nn->nfsd_serv->sv_nrpools; |
eed2965a GB |
410 | } |
411 | ||
9dd9845f | 412 | int nfsd_get_nrthreads(int n, int *nthreads, struct net *net) |
eed2965a GB |
413 | { |
414 | int i = 0; | |
9dd9845f | 415 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
eed2965a | 416 | |
9dd9845f SK |
417 | if (nn->nfsd_serv != NULL) { |
418 | for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++) | |
419 | nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads; | |
eed2965a GB |
420 | } |
421 | ||
422 | return 0; | |
423 | } | |
424 | ||
9dd9845f SK |
425 | void nfsd_destroy(struct net *net) |
426 | { | |
427 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
428 | int destroy = (nn->nfsd_serv->sv_nrthreads == 1); | |
429 | ||
430 | if (destroy) | |
431 | svc_shutdown_net(nn->nfsd_serv, net); | |
432 | svc_destroy(nn->nfsd_serv); | |
433 | if (destroy) | |
434 | nn->nfsd_serv = NULL; | |
435 | } | |
436 | ||
3938a0d5 | 437 | int nfsd_set_nrthreads(int n, int *nthreads, struct net *net) |
eed2965a GB |
438 | { |
439 | int i = 0; | |
440 | int tot = 0; | |
441 | int err = 0; | |
9dd9845f | 442 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
eed2965a | 443 | |
bedbdd8b NB |
444 | WARN_ON(!mutex_is_locked(&nfsd_mutex)); |
445 | ||
9dd9845f | 446 | if (nn->nfsd_serv == NULL || n <= 0) |
eed2965a GB |
447 | return 0; |
448 | ||
9dd9845f SK |
449 | if (n > nn->nfsd_serv->sv_nrpools) |
450 | n = nn->nfsd_serv->sv_nrpools; | |
eed2965a GB |
451 | |
452 | /* enforce a global maximum number of threads */ | |
453 | tot = 0; | |
454 | for (i = 0; i < n; i++) { | |
455 | if (nthreads[i] > NFSD_MAXSERVS) | |
456 | nthreads[i] = NFSD_MAXSERVS; | |
457 | tot += nthreads[i]; | |
458 | } | |
459 | if (tot > NFSD_MAXSERVS) { | |
460 | /* total too large: scale down requested numbers */ | |
461 | for (i = 0; i < n && tot > 0; i++) { | |
462 | int new = nthreads[i] * NFSD_MAXSERVS / tot; | |
463 | tot -= (nthreads[i] - new); | |
464 | nthreads[i] = new; | |
465 | } | |
466 | for (i = 0; i < n && tot > 0; i++) { | |
467 | nthreads[i]--; | |
468 | tot--; | |
469 | } | |
470 | } | |
471 | ||
472 | /* | |
473 | * There must always be a thread in pool 0; the admin | |
474 | * can't shut down NFS completely using pool_threads. | |
475 | */ | |
476 | if (nthreads[0] == 0) | |
477 | nthreads[0] = 1; | |
478 | ||
479 | /* apply the new numbers */ | |
9dd9845f | 480 | svc_get(nn->nfsd_serv); |
eed2965a | 481 | for (i = 0; i < n; i++) { |
9dd9845f | 482 | err = svc_set_num_threads(nn->nfsd_serv, &nn->nfsd_serv->sv_pools[i], |
eed2965a GB |
483 | nthreads[i]); |
484 | if (err) | |
485 | break; | |
486 | } | |
19f7e2ca | 487 | nfsd_destroy(net); |
eed2965a GB |
488 | return err; |
489 | } | |
490 | ||
ac77efbe JL |
491 | /* |
492 | * Adjust the number of threads and return the new number of threads. | |
493 | * This is also the function that starts the server if necessary, if | |
494 | * this is the first time nrservs is nonzero. | |
495 | */ | |
1da177e4 | 496 | int |
d41a9417 | 497 | nfsd_svc(int nrservs, struct net *net) |
1da177e4 LT |
498 | { |
499 | int error; | |
774f8bbd | 500 | bool nfsd_up_before; |
9dd9845f | 501 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
bedbdd8b NB |
502 | |
503 | mutex_lock(&nfsd_mutex); | |
6658d3a7 | 504 | dprintk("nfsd: creating service\n"); |
1da177e4 LT |
505 | if (nrservs <= 0) |
506 | nrservs = 0; | |
507 | if (nrservs > NFSD_MAXSERVS) | |
508 | nrservs = NFSD_MAXSERVS; | |
671e1fcf | 509 | error = 0; |
9dd9845f | 510 | if (nrservs == 0 && nn->nfsd_serv == NULL) |
671e1fcf N |
511 | goto out; |
512 | ||
6777436b | 513 | error = nfsd_create_serv(net); |
02a375f0 | 514 | if (error) |
774f8bbd BF |
515 | goto out; |
516 | ||
903d9bf0 | 517 | nfsd_up_before = nn->nfsd_net_up; |
774f8bbd | 518 | |
903d9bf0 | 519 | error = nfsd_startup_net(nrservs, net); |
af4718f3 BF |
520 | if (error) |
521 | goto out_destroy; | |
9dd9845f | 522 | error = svc_set_num_threads(nn->nfsd_serv, NULL, nrservs); |
774f8bbd BF |
523 | if (error) |
524 | goto out_shutdown; | |
9dd9845f | 525 | /* We are holding a reference to nn->nfsd_serv which |
af4718f3 BF |
526 | * we don't want to count in the return value, |
527 | * so subtract 1 | |
528 | */ | |
9dd9845f | 529 | error = nn->nfsd_serv->sv_nrthreads - 1; |
4ad9a344 | 530 | out_shutdown: |
774f8bbd | 531 | if (error < 0 && !nfsd_up_before) |
db42d1a7 | 532 | nfsd_shutdown(net); |
774f8bbd | 533 | out_destroy: |
19f7e2ca | 534 | nfsd_destroy(net); /* Release server */ |
4ad9a344 | 535 | out: |
bedbdd8b | 536 | mutex_unlock(&nfsd_mutex); |
1da177e4 LT |
537 | return error; |
538 | } | |
539 | ||
1da177e4 LT |
540 | |
541 | /* | |
542 | * This is the NFS server kernel thread | |
543 | */ | |
9867d76c JL |
544 | static int |
545 | nfsd(void *vrqstp) | |
1da177e4 | 546 | { |
9867d76c | 547 | struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp; |
5b444cc9 | 548 | int err; |
1da177e4 LT |
549 | |
550 | /* Lock module and set up kernel thread */ | |
bedbdd8b | 551 | mutex_lock(&nfsd_mutex); |
1da177e4 | 552 | |
9867d76c | 553 | /* At this point, the thread shares current->fs |
1da177e4 LT |
554 | * with the init process. We need to create files with a |
555 | * umask of 0 instead of init's umask. */ | |
3e93cd67 | 556 | if (unshare_fs_struct() < 0) { |
1da177e4 LT |
557 | printk("Unable to start nfsd thread: out of memory\n"); |
558 | goto out; | |
559 | } | |
3e93cd67 | 560 | |
1da177e4 LT |
561 | current->fs->umask = 0; |
562 | ||
9867d76c JL |
563 | /* |
564 | * thread is spawned with all signals set to SIG_IGN, re-enable | |
100766f8 | 565 | * the ones that will bring down the thread |
9867d76c | 566 | */ |
100766f8 JL |
567 | allow_signal(SIGKILL); |
568 | allow_signal(SIGHUP); | |
569 | allow_signal(SIGINT); | |
570 | allow_signal(SIGQUIT); | |
bedbdd8b | 571 | |
1da177e4 | 572 | nfsdstats.th_cnt++; |
bedbdd8b NB |
573 | mutex_unlock(&nfsd_mutex); |
574 | ||
1da177e4 LT |
575 | /* |
576 | * We want less throttling in balance_dirty_pages() so that nfs to | |
577 | * localhost doesn't cause nfsd to lock up due to all the client's | |
578 | * dirty pages. | |
579 | */ | |
580 | current->flags |= PF_LESS_THROTTLE; | |
83144186 | 581 | set_freezable(); |
1da177e4 LT |
582 | |
583 | /* | |
584 | * The main request loop | |
585 | */ | |
586 | for (;;) { | |
1da177e4 LT |
587 | /* |
588 | * Find a socket with data available and call its | |
589 | * recvfrom routine. | |
590 | */ | |
6fb2b47f | 591 | while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN) |
1da177e4 | 592 | ; |
9867d76c | 593 | if (err == -EINTR) |
1da177e4 | 594 | break; |
e0e81739 | 595 | validate_process_creds(); |
6fb2b47f | 596 | svc_process(rqstp); |
e0e81739 | 597 | validate_process_creds(); |
1da177e4 LT |
598 | } |
599 | ||
24e36663 | 600 | /* Clear signals before calling svc_exit_thread() */ |
9e416052 | 601 | flush_signals(current); |
1da177e4 | 602 | |
bedbdd8b | 603 | mutex_lock(&nfsd_mutex); |
1da177e4 LT |
604 | nfsdstats.th_cnt --; |
605 | ||
606 | out: | |
57c8b13e | 607 | rqstp->rq_server = NULL; |
786185b5 | 608 | |
1da177e4 LT |
609 | /* Release the thread */ |
610 | svc_exit_thread(rqstp); | |
611 | ||
57c8b13e SK |
612 | nfsd_destroy(&init_net); |
613 | ||
1da177e4 | 614 | /* Release module */ |
bedbdd8b | 615 | mutex_unlock(&nfsd_mutex); |
1da177e4 | 616 | module_put_and_exit(0); |
9867d76c | 617 | return 0; |
1da177e4 LT |
618 | } |
619 | ||
32c1eb0c AA |
620 | static __be32 map_new_errors(u32 vers, __be32 nfserr) |
621 | { | |
622 | if (nfserr == nfserr_jukebox && vers == 2) | |
623 | return nfserr_dropit; | |
624 | if (nfserr == nfserr_wrongsec && vers < 4) | |
625 | return nfserr_acces; | |
626 | return nfserr; | |
627 | } | |
628 | ||
1da177e4 | 629 | int |
c7afef1f | 630 | nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp) |
1da177e4 LT |
631 | { |
632 | struct svc_procedure *proc; | |
633 | kxdrproc_t xdr; | |
ad451d38 AV |
634 | __be32 nfserr; |
635 | __be32 *nfserrp; | |
1da177e4 LT |
636 | |
637 | dprintk("nfsd_dispatch: vers %d proc %d\n", | |
638 | rqstp->rq_vers, rqstp->rq_proc); | |
639 | proc = rqstp->rq_procinfo; | |
640 | ||
1091006c BF |
641 | /* |
642 | * Give the xdr decoder a chance to change this if it wants | |
643 | * (necessary in the NFSv4.0 compound case) | |
644 | */ | |
645 | rqstp->rq_cachetype = proc->pc_cachetype; | |
646 | /* Decode arguments */ | |
647 | xdr = proc->pc_decode; | |
648 | if (xdr && !xdr(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base, | |
649 | rqstp->rq_argp)) { | |
650 | dprintk("nfsd: failed to decode arguments!\n"); | |
651 | *statp = rpc_garbage_args; | |
652 | return 1; | |
653 | } | |
654 | ||
1da177e4 | 655 | /* Check whether we have this call in the cache. */ |
1091006c | 656 | switch (nfsd_cache_lookup(rqstp)) { |
1da177e4 LT |
657 | case RC_INTR: |
658 | case RC_DROPIT: | |
659 | return 0; | |
660 | case RC_REPLY: | |
661 | return 1; | |
662 | case RC_DOIT:; | |
663 | /* do it */ | |
664 | } | |
665 | ||
1da177e4 LT |
666 | /* need to grab the location to store the status, as |
667 | * nfsv4 does some encoding while processing | |
668 | */ | |
669 | nfserrp = rqstp->rq_res.head[0].iov_base | |
670 | + rqstp->rq_res.head[0].iov_len; | |
ad451d38 | 671 | rqstp->rq_res.head[0].iov_len += sizeof(__be32); |
1da177e4 LT |
672 | |
673 | /* Now call the procedure handler, and encode NFS status. */ | |
674 | nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp); | |
32c1eb0c | 675 | nfserr = map_new_errors(rqstp->rq_vers, nfserr); |
9e701c61 | 676 | if (nfserr == nfserr_dropit || rqstp->rq_dropme) { |
45457e09 | 677 | dprintk("nfsd: Dropping request; may be revisited later\n"); |
1da177e4 LT |
678 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); |
679 | return 0; | |
680 | } | |
681 | ||
682 | if (rqstp->rq_proc != 0) | |
683 | *nfserrp++ = nfserr; | |
684 | ||
685 | /* Encode result. | |
686 | * For NFSv2, additional info is never returned in case of an error. | |
687 | */ | |
688 | if (!(nfserr && rqstp->rq_vers == 2)) { | |
689 | xdr = proc->pc_encode; | |
690 | if (xdr && !xdr(rqstp, nfserrp, | |
691 | rqstp->rq_resp)) { | |
692 | /* Failed to encode result. Release cache entry */ | |
693 | dprintk("nfsd: failed to encode result!\n"); | |
694 | nfsd_cache_update(rqstp, RC_NOCACHE, NULL); | |
695 | *statp = rpc_system_err; | |
696 | return 1; | |
697 | } | |
698 | } | |
699 | ||
700 | /* Store reply in cache. */ | |
57d276d7 | 701 | nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1); |
1da177e4 LT |
702 | return 1; |
703 | } | |
03cf6c9f GB |
704 | |
705 | int nfsd_pool_stats_open(struct inode *inode, struct file *file) | |
706 | { | |
ed2d8aed | 707 | int ret; |
9dd9845f SK |
708 | struct net *net = &init_net; |
709 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
710 | ||
ed2d8aed | 711 | mutex_lock(&nfsd_mutex); |
9dd9845f | 712 | if (nn->nfsd_serv == NULL) { |
ed2d8aed | 713 | mutex_unlock(&nfsd_mutex); |
03cf6c9f | 714 | return -ENODEV; |
ed2d8aed RY |
715 | } |
716 | /* bump up the psudo refcount while traversing */ | |
9dd9845f SK |
717 | svc_get(nn->nfsd_serv); |
718 | ret = svc_pool_stats_open(nn->nfsd_serv, file); | |
ed2d8aed RY |
719 | mutex_unlock(&nfsd_mutex); |
720 | return ret; | |
721 | } | |
722 | ||
723 | int nfsd_pool_stats_release(struct inode *inode, struct file *file) | |
724 | { | |
725 | int ret = seq_release(inode, file); | |
786185b5 SK |
726 | struct net *net = &init_net; |
727 | ||
ed2d8aed RY |
728 | mutex_lock(&nfsd_mutex); |
729 | /* this function really, really should have been called svc_put() */ | |
19f7e2ca | 730 | nfsd_destroy(net); |
ed2d8aed RY |
731 | mutex_unlock(&nfsd_mutex); |
732 | return ret; | |
03cf6c9f | 733 | } |