knfsd: add file to export stats about nfsd pools
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / nfsd / nfssvc.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfsd/nfssvc.c
3 *
4 * Central processing for nfsd.
5 *
6 * Authors: Olaf Kirch (okir@monad.swb.de)
7 *
8 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
9 */
10
1da177e4 11#include <linux/module.h>
e8edc6e0 12#include <linux/sched.h>
1da177e4
LT
13#include <linux/time.h>
14#include <linux/errno.h>
15#include <linux/nfs.h>
16#include <linux/in.h>
17#include <linux/uio.h>
18#include <linux/unistd.h>
19#include <linux/slab.h>
20#include <linux/smp.h>
21#include <linux/smp_lock.h>
83144186 22#include <linux/freezer.h>
1da177e4 23#include <linux/fs_struct.h>
9867d76c 24#include <linux/kthread.h>
1da177e4
LT
25
26#include <linux/sunrpc/types.h>
27#include <linux/sunrpc/stats.h>
28#include <linux/sunrpc/svc.h>
29#include <linux/sunrpc/svcsock.h>
30#include <linux/sunrpc/cache.h>
31#include <linux/nfsd/nfsd.h>
32#include <linux/nfsd/stats.h>
33#include <linux/nfsd/cache.h>
70c3b76c 34#include <linux/nfsd/syscall.h>
1da177e4 35#include <linux/lockd/bind.h>
a257cdd0 36#include <linux/nfsacl.h>
1da177e4
LT
37
38#define NFSDDBG_FACILITY NFSDDBG_SVC
39
1da177e4 40extern struct svc_program nfsd_program;
9867d76c 41static int nfsd(void *vrqstp);
1da177e4 42struct timeval nfssvc_boot;
1da177e4 43
bedbdd8b
NB
44/*
45 * nfsd_mutex protects nfsd_serv -- both the pointer itself and the members
46 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
47 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
48 *
49 * If (out side the lock) nfsd_serv is non-NULL, then it must point to a
50 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
51 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
52 * entry of ->sv_pools[].
53 *
54 * Transitions of the thread count between zero and non-zero are of particular
55 * interest since the svc_serv needs to be created and initialized at that
56 * point, or freed.
3dd98a3b
JL
57 *
58 * Finally, the nfsd_mutex also protects some of the global variables that are
59 * accessed when nfsd starts and that are settable via the write_* routines in
60 * nfsctl.c. In particular:
61 *
62 * user_recovery_dirname
63 * user_lease_time
64 * nfsd_versions
bedbdd8b
NB
65 */
66DEFINE_MUTEX(nfsd_mutex);
67struct svc_serv *nfsd_serv;
68
3fb803a9
AG
69#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
70static struct svc_stat nfsd_acl_svcstats;
71static struct svc_version * nfsd_acl_version[] = {
72 [2] = &nfsd_acl_version2,
73 [3] = &nfsd_acl_version3,
74};
75
76#define NFSD_ACL_MINVERS 2
e8c96f8c 77#define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
3fb803a9
AG
78static struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
79
80static struct svc_program nfsd_acl_program = {
81 .pg_prog = NFS_ACL_PROGRAM,
82 .pg_nvers = NFSD_ACL_NRVERS,
83 .pg_vers = nfsd_acl_versions,
1a8eff6d 84 .pg_name = "nfsacl",
3fb803a9
AG
85 .pg_class = "nfsd",
86 .pg_stats = &nfsd_acl_svcstats,
87 .pg_authenticate = &svc_set_client,
88};
89
90static struct svc_stat nfsd_acl_svcstats = {
91 .program = &nfsd_acl_program,
92};
93#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
94
70c3b76c
N
95static struct svc_version * nfsd_version[] = {
96 [2] = &nfsd_version2,
97#if defined(CONFIG_NFSD_V3)
98 [3] = &nfsd_version3,
99#endif
100#if defined(CONFIG_NFSD_V4)
101 [4] = &nfsd_version4,
102#endif
103};
104
105#define NFSD_MINVERS 2
e8c96f8c 106#define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
70c3b76c
N
107static struct svc_version *nfsd_versions[NFSD_NRVERS];
108
109struct svc_program nfsd_program = {
3fb803a9
AG
110#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
111 .pg_next = &nfsd_acl_program,
112#endif
70c3b76c
N
113 .pg_prog = NFS_PROGRAM, /* program number */
114 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
115 .pg_vers = nfsd_versions, /* version table */
116 .pg_name = "nfsd", /* program name */
117 .pg_class = "nfsd", /* authentication class */
118 .pg_stats = &nfsd_svcstats, /* version table */
119 .pg_authenticate = &svc_set_client, /* export authentication */
120
121};
122
6658d3a7
N
123int nfsd_vers(int vers, enum vers_op change)
124{
125 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
126 return -1;
127 switch(change) {
128 case NFSD_SET:
129 nfsd_versions[vers] = nfsd_version[vers];
6658d3a7
N
130#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
131 if (vers < NFSD_ACL_NRVERS)
1a8eff6d 132 nfsd_acl_versions[vers] = nfsd_acl_version[vers];
6658d3a7 133#endif
1a8eff6d 134 break;
6658d3a7
N
135 case NFSD_CLEAR:
136 nfsd_versions[vers] = NULL;
137#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
138 if (vers < NFSD_ACL_NRVERS)
1a8eff6d 139 nfsd_acl_versions[vers] = NULL;
6658d3a7
N
140#endif
141 break;
142 case NFSD_TEST:
143 return nfsd_versions[vers] != NULL;
144 case NFSD_AVAIL:
145 return nfsd_version[vers] != NULL;
146 }
147 return 0;
148}
1da177e4
LT
149/*
150 * Maximum number of nfsd processes
151 */
152#define NFSD_MAXSERVS 8192
153
154int nfsd_nrthreads(void)
155{
c7d106c9
NB
156 int rv = 0;
157 mutex_lock(&nfsd_mutex);
158 if (nfsd_serv)
159 rv = nfsd_serv->sv_nrthreads;
160 mutex_unlock(&nfsd_mutex);
161 return rv;
1da177e4
LT
162}
163
bc591ccf
N
164static void nfsd_last_thread(struct svc_serv *serv)
165{
166 /* When last nfsd thread exits we need to do some clean-up */
7a182083
TT
167 struct svc_xprt *xprt;
168 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list)
24e36663 169 lockd_down();
bc591ccf
N
170 nfsd_serv = NULL;
171 nfsd_racache_shutdown();
172 nfs4_state_shutdown();
173
e096bbc6
JL
174 printk(KERN_WARNING "nfsd: last server has exited, flushing export "
175 "cache\n");
176 nfsd_export_flush();
bc591ccf 177}
6658d3a7
N
178
179void nfsd_reset_versions(void)
180{
181 int found_one = 0;
182 int i;
183
184 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
185 if (nfsd_program.pg_vers[i])
186 found_one = 1;
187 }
188
189 if (!found_one) {
190 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
191 nfsd_program.pg_vers[i] = nfsd_version[i];
192#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
193 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++)
194 nfsd_acl_program.pg_vers[i] =
195 nfsd_acl_version[i];
196#endif
197 }
198}
199
bedbdd8b 200
b41b66d6 201int nfsd_create_serv(void)
02a375f0
N
202{
203 int err = 0;
bedbdd8b
NB
204
205 WARN_ON(!mutex_is_locked(&nfsd_mutex));
02a375f0 206 if (nfsd_serv) {
9a24ab57 207 svc_get(nfsd_serv);
02a375f0
N
208 return 0;
209 }
596bbe53
N
210 if (nfsd_max_blksize == 0) {
211 /* choose a suitable default */
212 struct sysinfo i;
213 si_meminfo(&i);
214 /* Aim for 1/4096 of memory per thread
215 * This gives 1MB on 4Gig machines
216 * But only uses 32K on 128M machines.
217 * Bottom out at 8K on 32M and smaller.
218 * Of course, this is only a default.
219 */
220 nfsd_max_blksize = NFSSVC_MAXBLKSIZE;
44c55600 221 i.totalram <<= PAGE_SHIFT - 12;
596bbe53
N
222 while (nfsd_max_blksize > i.totalram &&
223 nfsd_max_blksize >= 8*1024*2)
224 nfsd_max_blksize /= 2;
225 }
02a375f0 226
e096bbc6 227 nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
e851db5b 228 AF_INET,
a75c5d01 229 nfsd_last_thread, nfsd, THIS_MODULE);
02a375f0
N
230 if (nfsd_serv == NULL)
231 err = -ENOMEM;
bedbdd8b 232
02a375f0
N
233 do_gettimeofday(&nfssvc_boot); /* record boot time */
234 return err;
235}
236
237static int nfsd_init_socks(int port)
238{
239 int error;
240 if (!list_empty(&nfsd_serv->sv_permsocks))
241 return 0;
242
26a41409 243 error = svc_create_xprt(nfsd_serv, "udp", port,
482fb94e 244 SVC_SOCK_DEFAULTS);
02a375f0
N
245 if (error < 0)
246 return error;
247
1cd9cd16
CL
248 error = lockd_up();
249 if (error < 0)
250 return error;
251
26a41409 252 error = svc_create_xprt(nfsd_serv, "tcp", port,
482fb94e 253 SVC_SOCK_DEFAULTS);
02a375f0
N
254 if (error < 0)
255 return error;
26a41409
CL
256
257 error = lockd_up();
258 if (error < 0)
259 return error;
260
02a375f0
N
261 return 0;
262}
263
eed2965a
GB
264int nfsd_nrpools(void)
265{
266 if (nfsd_serv == NULL)
267 return 0;
268 else
269 return nfsd_serv->sv_nrpools;
270}
271
272int nfsd_get_nrthreads(int n, int *nthreads)
273{
274 int i = 0;
275
276 if (nfsd_serv != NULL) {
277 for (i = 0; i < nfsd_serv->sv_nrpools && i < n; i++)
278 nthreads[i] = nfsd_serv->sv_pools[i].sp_nrthreads;
279 }
280
281 return 0;
282}
283
284int nfsd_set_nrthreads(int n, int *nthreads)
285{
286 int i = 0;
287 int tot = 0;
288 int err = 0;
289
bedbdd8b
NB
290 WARN_ON(!mutex_is_locked(&nfsd_mutex));
291
eed2965a
GB
292 if (nfsd_serv == NULL || n <= 0)
293 return 0;
294
295 if (n > nfsd_serv->sv_nrpools)
296 n = nfsd_serv->sv_nrpools;
297
298 /* enforce a global maximum number of threads */
299 tot = 0;
300 for (i = 0; i < n; i++) {
301 if (nthreads[i] > NFSD_MAXSERVS)
302 nthreads[i] = NFSD_MAXSERVS;
303 tot += nthreads[i];
304 }
305 if (tot > NFSD_MAXSERVS) {
306 /* total too large: scale down requested numbers */
307 for (i = 0; i < n && tot > 0; i++) {
308 int new = nthreads[i] * NFSD_MAXSERVS / tot;
309 tot -= (nthreads[i] - new);
310 nthreads[i] = new;
311 }
312 for (i = 0; i < n && tot > 0; i++) {
313 nthreads[i]--;
314 tot--;
315 }
316 }
317
318 /*
319 * There must always be a thread in pool 0; the admin
320 * can't shut down NFS completely using pool_threads.
321 */
322 if (nthreads[0] == 0)
323 nthreads[0] = 1;
324
325 /* apply the new numbers */
eed2965a
GB
326 svc_get(nfsd_serv);
327 for (i = 0; i < n; i++) {
328 err = svc_set_num_threads(nfsd_serv, &nfsd_serv->sv_pools[i],
329 nthreads[i]);
330 if (err)
331 break;
332 }
333 svc_destroy(nfsd_serv);
eed2965a
GB
334
335 return err;
336}
337
1da177e4
LT
338int
339nfsd_svc(unsigned short port, int nrservs)
340{
341 int error;
bedbdd8b
NB
342
343 mutex_lock(&nfsd_mutex);
6658d3a7 344 dprintk("nfsd: creating service\n");
1da177e4
LT
345 error = -EINVAL;
346 if (nrservs <= 0)
347 nrservs = 0;
348 if (nrservs > NFSD_MAXSERVS)
349 nrservs = NFSD_MAXSERVS;
350
351 /* Readahead param cache - will no-op if it already exists */
352 error = nfsd_racache_init(2*nrservs);
353 if (error<0)
354 goto out;
e8ff2a84 355 nfs4_state_start();
02a375f0
N
356
357 nfsd_reset_versions();
358
359 error = nfsd_create_serv();
360
361 if (error)
362 goto out;
363 error = nfsd_init_socks(port);
364 if (error)
365 goto failure;
366
eec09661 367 error = svc_set_num_threads(nfsd_serv, NULL, nrservs);
1da177e4 368 failure:
1da177e4 369 svc_destroy(nfsd_serv); /* Release server */
1da177e4 370 out:
bedbdd8b 371 mutex_unlock(&nfsd_mutex);
1da177e4
LT
372 return error;
373}
374
1da177e4
LT
375
376/*
377 * This is the NFS server kernel thread
378 */
9867d76c
JL
379static int
380nfsd(void *vrqstp)
1da177e4 381{
9867d76c 382 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
1da177e4 383 struct fs_struct *fsp;
9867d76c 384 int err, preverr = 0;
1da177e4
LT
385
386 /* Lock module and set up kernel thread */
bedbdd8b 387 mutex_lock(&nfsd_mutex);
1da177e4 388
9867d76c 389 /* At this point, the thread shares current->fs
1da177e4
LT
390 * with the init process. We need to create files with a
391 * umask of 0 instead of init's umask. */
392 fsp = copy_fs_struct(current->fs);
393 if (!fsp) {
394 printk("Unable to start nfsd thread: out of memory\n");
395 goto out;
396 }
397 exit_fs(current);
398 current->fs = fsp;
399 current->fs->umask = 0;
400
9867d76c
JL
401 /*
402 * thread is spawned with all signals set to SIG_IGN, re-enable
100766f8 403 * the ones that will bring down the thread
9867d76c 404 */
100766f8
JL
405 allow_signal(SIGKILL);
406 allow_signal(SIGHUP);
407 allow_signal(SIGINT);
408 allow_signal(SIGQUIT);
bedbdd8b 409
1da177e4 410 nfsdstats.th_cnt++;
bedbdd8b
NB
411 mutex_unlock(&nfsd_mutex);
412
1da177e4
LT
413 /*
414 * We want less throttling in balance_dirty_pages() so that nfs to
415 * localhost doesn't cause nfsd to lock up due to all the client's
416 * dirty pages.
417 */
418 current->flags |= PF_LESS_THROTTLE;
83144186 419 set_freezable();
1da177e4
LT
420
421 /*
422 * The main request loop
423 */
424 for (;;) {
1da177e4
LT
425 /*
426 * Find a socket with data available and call its
427 * recvfrom routine.
428 */
6fb2b47f 429 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
1da177e4 430 ;
9867d76c 431 if (err == -EINTR)
1da177e4 432 break;
9867d76c
JL
433 else if (err < 0) {
434 if (err != preverr) {
435 printk(KERN_WARNING "%s: unexpected error "
436 "from svc_recv (%d)\n", __func__, -err);
437 preverr = err;
438 }
439 schedule_timeout_uninterruptible(HZ);
440 continue;
441 }
442
1da177e4
LT
443
444 /* Lock the export hash tables for reading. */
445 exp_readlock();
446
6fb2b47f 447 svc_process(rqstp);
1da177e4
LT
448
449 /* Unlock export hash tables */
450 exp_readunlock();
1da177e4
LT
451 }
452
24e36663 453 /* Clear signals before calling svc_exit_thread() */
9e416052 454 flush_signals(current);
1da177e4 455
bedbdd8b 456 mutex_lock(&nfsd_mutex);
1da177e4
LT
457 nfsdstats.th_cnt --;
458
459out:
460 /* Release the thread */
461 svc_exit_thread(rqstp);
462
463 /* Release module */
bedbdd8b 464 mutex_unlock(&nfsd_mutex);
1da177e4 465 module_put_and_exit(0);
9867d76c 466 return 0;
1da177e4
LT
467}
468
32c1eb0c
AA
469static __be32 map_new_errors(u32 vers, __be32 nfserr)
470{
471 if (nfserr == nfserr_jukebox && vers == 2)
472 return nfserr_dropit;
473 if (nfserr == nfserr_wrongsec && vers < 4)
474 return nfserr_acces;
475 return nfserr;
476}
477
1da177e4 478int
c7afef1f 479nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
1da177e4
LT
480{
481 struct svc_procedure *proc;
482 kxdrproc_t xdr;
ad451d38
AV
483 __be32 nfserr;
484 __be32 *nfserrp;
1da177e4
LT
485
486 dprintk("nfsd_dispatch: vers %d proc %d\n",
487 rqstp->rq_vers, rqstp->rq_proc);
488 proc = rqstp->rq_procinfo;
489
490 /* Check whether we have this call in the cache. */
491 switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
492 case RC_INTR:
493 case RC_DROPIT:
494 return 0;
495 case RC_REPLY:
496 return 1;
497 case RC_DOIT:;
498 /* do it */
499 }
500
501 /* Decode arguments */
502 xdr = proc->pc_decode;
ad451d38 503 if (xdr && !xdr(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base,
1da177e4
LT
504 rqstp->rq_argp)) {
505 dprintk("nfsd: failed to decode arguments!\n");
506 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
507 *statp = rpc_garbage_args;
508 return 1;
509 }
510
511 /* need to grab the location to store the status, as
512 * nfsv4 does some encoding while processing
513 */
514 nfserrp = rqstp->rq_res.head[0].iov_base
515 + rqstp->rq_res.head[0].iov_len;
ad451d38 516 rqstp->rq_res.head[0].iov_len += sizeof(__be32);
1da177e4
LT
517
518 /* Now call the procedure handler, and encode NFS status. */
519 nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
32c1eb0c 520 nfserr = map_new_errors(rqstp->rq_vers, nfserr);
1da177e4 521 if (nfserr == nfserr_dropit) {
45457e09 522 dprintk("nfsd: Dropping request; may be revisited later\n");
1da177e4
LT
523 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
524 return 0;
525 }
526
527 if (rqstp->rq_proc != 0)
528 *nfserrp++ = nfserr;
529
530 /* Encode result.
531 * For NFSv2, additional info is never returned in case of an error.
532 */
533 if (!(nfserr && rqstp->rq_vers == 2)) {
534 xdr = proc->pc_encode;
535 if (xdr && !xdr(rqstp, nfserrp,
536 rqstp->rq_resp)) {
537 /* Failed to encode result. Release cache entry */
538 dprintk("nfsd: failed to encode result!\n");
539 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
540 *statp = rpc_system_err;
541 return 1;
542 }
543 }
544
545 /* Store reply in cache. */
546 nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
547 return 1;
548}
03cf6c9f
GB
549
550int nfsd_pool_stats_open(struct inode *inode, struct file *file)
551{
552 if (nfsd_serv == NULL)
553 return -ENODEV;
554 return svc_pool_stats_open(nfsd_serv, file);
555}