2 * linux/fs/nfsd/nfssvc.c
4 * Central processing for nfsd.
6 * Authors: Olaf Kirch (okir@monad.swb.de)
8 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/time.h>
14 #include <linux/errno.h>
15 #include <linux/nfs.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>
22 #include <linux/freezer.h>
23 #include <linux/fs_struct.h>
24 #include <linux/kthread.h>
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>
34 #include <linux/nfsd/syscall.h>
35 #include <linux/lockd/bind.h>
36 #include <linux/nfsacl.h>
38 #define NFSDDBG_FACILITY NFSDDBG_SVC
40 extern struct svc_program nfsd_program;
41 static int nfsd(void *vrqstp);
42 struct timeval nfssvc_boot;
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
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[].
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
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:
62 * user_recovery_dirname
66 DEFINE_MUTEX(nfsd_mutex);
67 struct svc_serv *nfsd_serv;
69 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
70 static struct svc_stat nfsd_acl_svcstats;
71 static struct svc_version * nfsd_acl_version[] = {
72 [2] = &nfsd_acl_version2,
73 [3] = &nfsd_acl_version3,
76 #define NFSD_ACL_MINVERS 2
77 #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
78 static struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
80 static struct svc_program nfsd_acl_program = {
81 .pg_prog = NFS_ACL_PROGRAM,
82 .pg_nvers = NFSD_ACL_NRVERS,
83 .pg_vers = nfsd_acl_versions,
86 .pg_stats = &nfsd_acl_svcstats,
87 .pg_authenticate = &svc_set_client,
90 static struct svc_stat nfsd_acl_svcstats = {
91 .program = &nfsd_acl_program,
93 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
95 static struct svc_version * nfsd_version[] = {
97 #if defined(CONFIG_NFSD_V3)
100 #if defined(CONFIG_NFSD_V4)
101 [4] = &nfsd_version4,
105 #define NFSD_MINVERS 2
106 #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
107 static struct svc_version *nfsd_versions[NFSD_NRVERS];
109 struct svc_program nfsd_program = {
110 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
111 .pg_next = &nfsd_acl_program,
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 */
123 int nfsd_vers(int vers, enum vers_op change)
125 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
129 nfsd_versions[vers] = nfsd_version[vers];
130 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
131 if (vers < NFSD_ACL_NRVERS)
132 nfsd_acl_versions[vers] = nfsd_acl_version[vers];
136 nfsd_versions[vers] = NULL;
137 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
138 if (vers < NFSD_ACL_NRVERS)
139 nfsd_acl_versions[vers] = NULL;
143 return nfsd_versions[vers] != NULL;
145 return nfsd_version[vers] != NULL;
150 * Maximum number of nfsd processes
152 #define NFSD_MAXSERVS 8192
154 int nfsd_nrthreads(void)
157 mutex_lock(&nfsd_mutex);
159 rv = nfsd_serv->sv_nrthreads;
160 mutex_unlock(&nfsd_mutex);
164 static void nfsd_last_thread(struct svc_serv *serv)
166 /* When last nfsd thread exits we need to do some clean-up */
167 struct svc_xprt *xprt;
168 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list)
171 nfsd_racache_shutdown();
172 nfs4_state_shutdown();
174 printk(KERN_WARNING "nfsd: last server has exited, flushing export "
179 void nfsd_reset_versions(void)
184 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
185 if (nfsd_program.pg_vers[i])
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] =
201 int nfsd_create_serv(void)
205 WARN_ON(!mutex_is_locked(&nfsd_mutex));
210 if (nfsd_max_blksize == 0) {
211 /* choose a suitable default */
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.
220 nfsd_max_blksize = NFSSVC_MAXBLKSIZE;
221 i.totalram <<= PAGE_SHIFT - 12;
222 while (nfsd_max_blksize > i.totalram &&
223 nfsd_max_blksize >= 8*1024*2)
224 nfsd_max_blksize /= 2;
227 nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
229 nfsd_last_thread, nfsd, THIS_MODULE);
230 if (nfsd_serv == NULL)
233 do_gettimeofday(&nfssvc_boot); /* record boot time */
237 static int nfsd_init_socks(int port)
240 if (!list_empty(&nfsd_serv->sv_permsocks))
243 error = svc_create_xprt(nfsd_serv, "udp", port,
252 error = svc_create_xprt(nfsd_serv, "tcp", port,
264 int nfsd_nrpools(void)
266 if (nfsd_serv == NULL)
269 return nfsd_serv->sv_nrpools;
272 int nfsd_get_nrthreads(int n, int *nthreads)
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;
284 int nfsd_set_nrthreads(int n, int *nthreads)
290 WARN_ON(!mutex_is_locked(&nfsd_mutex));
292 if (nfsd_serv == NULL || n <= 0)
295 if (n > nfsd_serv->sv_nrpools)
296 n = nfsd_serv->sv_nrpools;
298 /* enforce a global maximum number of threads */
300 for (i = 0; i < n; i++) {
301 if (nthreads[i] > NFSD_MAXSERVS)
302 nthreads[i] = NFSD_MAXSERVS;
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);
312 for (i = 0; i < n && tot > 0; i++) {
319 * There must always be a thread in pool 0; the admin
320 * can't shut down NFS completely using pool_threads.
322 if (nthreads[0] == 0)
325 /* apply the new numbers */
327 for (i = 0; i < n; i++) {
328 err = svc_set_num_threads(nfsd_serv, &nfsd_serv->sv_pools[i],
333 svc_destroy(nfsd_serv);
339 nfsd_svc(unsigned short port, int nrservs)
343 mutex_lock(&nfsd_mutex);
344 dprintk("nfsd: creating service\n");
348 if (nrservs > NFSD_MAXSERVS)
349 nrservs = NFSD_MAXSERVS;
351 /* Readahead param cache - will no-op if it already exists */
352 error = nfsd_racache_init(2*nrservs);
357 nfsd_reset_versions();
359 error = nfsd_create_serv();
363 error = nfsd_init_socks(port);
367 error = svc_set_num_threads(nfsd_serv, NULL, nrservs);
369 svc_destroy(nfsd_serv); /* Release server */
371 mutex_unlock(&nfsd_mutex);
377 * This is the NFS server kernel thread
382 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
383 struct fs_struct *fsp;
384 int err, preverr = 0;
386 /* Lock module and set up kernel thread */
387 mutex_lock(&nfsd_mutex);
389 /* At this point, the thread shares current->fs
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);
394 printk("Unable to start nfsd thread: out of memory\n");
399 current->fs->umask = 0;
402 * thread is spawned with all signals set to SIG_IGN, re-enable
403 * the ones that will bring down the thread
405 allow_signal(SIGKILL);
406 allow_signal(SIGHUP);
407 allow_signal(SIGINT);
408 allow_signal(SIGQUIT);
411 mutex_unlock(&nfsd_mutex);
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
418 current->flags |= PF_LESS_THROTTLE;
422 * The main request loop
426 * Find a socket with data available and call its
429 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
434 if (err != preverr) {
435 printk(KERN_WARNING "%s: unexpected error "
436 "from svc_recv (%d)\n", __func__, -err);
439 schedule_timeout_uninterruptible(HZ);
444 /* Lock the export hash tables for reading. */
449 /* Unlock export hash tables */
453 /* Clear signals before calling svc_exit_thread() */
454 flush_signals(current);
456 mutex_lock(&nfsd_mutex);
460 /* Release the thread */
461 svc_exit_thread(rqstp);
464 mutex_unlock(&nfsd_mutex);
465 module_put_and_exit(0);
469 static __be32 map_new_errors(u32 vers, __be32 nfserr)
471 if (nfserr == nfserr_jukebox && vers == 2)
472 return nfserr_dropit;
473 if (nfserr == nfserr_wrongsec && vers < 4)
479 nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
481 struct svc_procedure *proc;
486 dprintk("nfsd_dispatch: vers %d proc %d\n",
487 rqstp->rq_vers, rqstp->rq_proc);
488 proc = rqstp->rq_procinfo;
490 /* Check whether we have this call in the cache. */
491 switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
501 /* Decode arguments */
502 xdr = proc->pc_decode;
503 if (xdr && !xdr(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base,
505 dprintk("nfsd: failed to decode arguments!\n");
506 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
507 *statp = rpc_garbage_args;
511 /* need to grab the location to store the status, as
512 * nfsv4 does some encoding while processing
514 nfserrp = rqstp->rq_res.head[0].iov_base
515 + rqstp->rq_res.head[0].iov_len;
516 rqstp->rq_res.head[0].iov_len += sizeof(__be32);
518 /* NFSv4.1 DRC requires statp */
519 if (rqstp->rq_vers == 4)
520 nfsd4_set_statp(rqstp, statp);
522 /* Now call the procedure handler, and encode NFS status. */
523 nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
524 nfserr = map_new_errors(rqstp->rq_vers, nfserr);
525 if (nfserr == nfserr_dropit) {
526 dprintk("nfsd: Dropping request; may be revisited later\n");
527 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
531 if (rqstp->rq_proc != 0)
535 * For NFSv2, additional info is never returned in case of an error.
537 if (!(nfserr && rqstp->rq_vers == 2)) {
538 xdr = proc->pc_encode;
539 if (xdr && !xdr(rqstp, nfserrp,
541 /* Failed to encode result. Release cache entry */
542 dprintk("nfsd: failed to encode result!\n");
543 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
544 *statp = rpc_system_err;
549 /* Store reply in cache. */
550 nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
554 int nfsd_pool_stats_open(struct inode *inode, struct file *file)
556 if (nfsd_serv == NULL)
558 return svc_pool_stats_open(nfsd_serv, file);