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/config.h>
12 #include <linux/module.h>
14 #include <linux/time.h>
15 #include <linux/errno.h>
16 #include <linux/nfs.h>
18 #include <linux/uio.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/fs_struct.h>
25 #include <linux/sunrpc/types.h>
26 #include <linux/sunrpc/stats.h>
27 #include <linux/sunrpc/svc.h>
28 #include <linux/sunrpc/svcsock.h>
29 #include <linux/sunrpc/cache.h>
30 #include <linux/nfsd/nfsd.h>
31 #include <linux/nfsd/stats.h>
32 #include <linux/nfsd/cache.h>
33 #include <linux/nfsd/syscall.h>
34 #include <linux/lockd/bind.h>
36 #define NFSDDBG_FACILITY NFSDDBG_SVC
38 /* these signals will be delivered to an nfsd thread
39 * when handling a request
41 #define ALLOWED_SIGS (sigmask(SIGKILL))
42 /* these signals will be delivered to an nfsd thread
43 * when not handling a request. i.e. when waiting
45 #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT))
46 /* if the last thread dies with SIGHUP, then the exports table is
47 * left unchanged ( like 2.4-{0-9} ). Any other signal will clear
48 * the exports table (like 2.2).
50 #define SIG_NOCLEAN SIGHUP
52 extern struct svc_program nfsd_program;
53 static void nfsd(struct svc_rqst *rqstp);
54 struct timeval nfssvc_boot;
55 static struct svc_serv *nfsd_serv; static atomic_t nfsd_busy; static unsigned long nfsd_last_call; static DEFINE_SPINLOCK(nfsd_call_lock);
58 struct list_head list;
59 struct task_struct *task;
61 static struct list_head nfsd_list = LIST_HEAD_INIT(nfsd_list);
63 extern struct svc_version nfsd_version2, nfsd_version3, nfsd_version4;
65 static struct svc_version * nfsd_version[] = {
67 #if defined(CONFIG_NFSD_V3)
70 #if defined(CONFIG_NFSD_V4)
75 #define NFSD_MINVERS 2
76 #define NFSD_NRVERS (sizeof(nfsd_version)/sizeof(nfsd_version[0]))
77 struct svc_program nfsd_program = {
78 .pg_prog = NFS_PROGRAM, /* program number */
79 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
80 .pg_vers = nfsd_version, /* version table */
81 .pg_name = "nfsd", /* program name */
82 .pg_class = "nfsd", /* authentication class */
83 .pg_stats = &nfsd_svcstats, /* version table */
84 .pg_authenticate = &svc_set_client, /* export authentication */
89 * Maximum number of nfsd processes
91 #define NFSD_MAXSERVS 8192
93 int nfsd_nrthreads(void)
95 if (nfsd_serv == NULL)
98 return nfsd_serv->sv_nrthreads;
102 nfsd_svc(unsigned short port, int nrservs)
105 int none_left, found_one, i;
106 struct list_head *victim;
109 dprintk("nfsd: creating service: port %d vers 0x%x proto 0x%x\n",
110 nfsd_port, nfsd_versbits, nfsd_portbits);
114 if (nrservs > NFSD_MAXSERVS)
115 nrservs = NFSD_MAXSERVS;
118 * If set, use the nfsd_ctlbits to define which
119 * versions that will be advertised
123 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
124 if (NFSCTL_VERISSET(nfsd_versbits, i)) {
125 nfsd_program.pg_vers[i] = nfsd_version[i];
128 nfsd_program.pg_vers[i] = NULL;
132 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
133 nfsd_program.pg_vers[i] = nfsd_version[i];
136 /* Readahead param cache - will no-op if it already exists */
137 error = nfsd_racache_init(2*nrservs);
140 error = nfs4_state_init();
145 atomic_set(&nfsd_busy, 0);
147 nfsd_serv = svc_create(&nfsd_program, NFSD_BUFSIZE);
148 if (nfsd_serv == NULL)
150 if (!nfsd_portbits || NFSCTL_UDPISSET(nfsd_portbits)) {
151 error = svc_makesock(nfsd_serv, IPPROTO_UDP, nfsd_port);
155 #ifdef CONFIG_NFSD_TCP
156 if (!nfsd_portbits || NFSCTL_TCPISSET(nfsd_portbits)) {
157 error = svc_makesock(nfsd_serv, IPPROTO_TCP, nfsd_port);
162 do_gettimeofday(&nfssvc_boot); /* record boot time */
164 nfsd_serv->sv_nrthreads++;
165 nrservs -= (nfsd_serv->sv_nrthreads-1);
166 while (nrservs > 0) {
168 __module_get(THIS_MODULE);
169 error = svc_create_thread(nfsd, nfsd_serv);
171 module_put(THIS_MODULE);
175 victim = nfsd_list.next;
176 while (nrservs < 0 && victim != &nfsd_list) {
177 struct nfsd_list *nl =
178 list_entry(victim,struct nfsd_list, list);
179 victim = victim->next;
180 send_sig(SIG_NOCLEAN, nl->task, 1);
184 none_left = (nfsd_serv->sv_nrthreads == 1);
185 svc_destroy(nfsd_serv); /* Release server */
188 nfsd_racache_shutdown();
189 nfs4_state_shutdown();
197 update_thread_usage(int busy_threads)
199 unsigned long prev_call;
203 spin_lock(&nfsd_call_lock);
204 prev_call = nfsd_last_call;
205 nfsd_last_call = jiffies;
206 decile = busy_threads*10/nfsdstats.th_cnt;
207 if (decile>0 && decile <= 10) {
208 diff = nfsd_last_call - prev_call;
209 if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP)
210 nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP;
212 nfsdstats.th_fullcnt++;
214 spin_unlock(&nfsd_call_lock);
218 * This is the NFS server kernel thread
221 nfsd(struct svc_rqst *rqstp)
223 struct svc_serv *serv = rqstp->rq_server;
224 struct fs_struct *fsp;
227 sigset_t shutdown_mask, allowed_mask;
229 /* Lock module and set up kernel thread */
233 /* After daemonize() this kernel thread shares current->fs
234 * with the init process. We need to create files with a
235 * umask of 0 instead of init's umask. */
236 fsp = copy_fs_struct(current->fs);
238 printk("Unable to start nfsd thread: out of memory\n");
243 current->fs->umask = 0;
245 siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS);
246 siginitsetinv(&allowed_mask, ALLOWED_SIGS);
250 lockd_up(); /* start lockd */
253 list_add(&me.list, &nfsd_list);
258 * We want less throttling in balance_dirty_pages() so that nfs to
259 * localhost doesn't cause nfsd to lock up due to all the client's
262 current->flags |= PF_LESS_THROTTLE;
265 * The main request loop
268 /* Block all but the shutdown signals */
269 sigprocmask(SIG_SETMASK, &shutdown_mask, NULL);
272 * Find a socket with data available and call its
275 while ((err = svc_recv(serv, rqstp,
276 60*60*HZ)) == -EAGAIN)
280 update_thread_usage(atomic_read(&nfsd_busy));
281 atomic_inc(&nfsd_busy);
283 /* Lock the export hash tables for reading. */
286 /* Process request with signals blocked. */
287 sigprocmask(SIG_SETMASK, &allowed_mask, NULL);
289 svc_process(serv, rqstp);
291 /* Unlock export hash tables */
293 update_thread_usage(atomic_read(&nfsd_busy));
294 atomic_dec(&nfsd_busy);
298 printk(KERN_WARNING "nfsd: terminating on error %d\n", -err);
302 for (signo = 1; signo <= _NSIG; signo++)
303 if (sigismember(¤t->pending.signal, signo) &&
304 !sigismember(¤t->blocked, signo))
308 /* Clear signals before calling lockd_down() and svc_exit_thread() */
309 flush_signals(current);
316 /* Check if this is last thread */
317 if (serv->sv_nrthreads==1) {
319 printk(KERN_WARNING "nfsd: last server has exited\n");
320 if (err != SIG_NOCLEAN) {
321 printk(KERN_WARNING "nfsd: unexporting all filesystems\n");
325 nfsd_racache_shutdown(); /* release read-ahead cache */
326 nfs4_state_shutdown();
332 /* Release the thread */
333 svc_exit_thread(rqstp);
336 module_put_and_exit(0);
340 nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp)
342 struct svc_procedure *proc;
347 dprintk("nfsd_dispatch: vers %d proc %d\n",
348 rqstp->rq_vers, rqstp->rq_proc);
349 proc = rqstp->rq_procinfo;
351 /* Check whether we have this call in the cache. */
352 switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
362 /* Decode arguments */
363 xdr = proc->pc_decode;
364 if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base,
366 dprintk("nfsd: failed to decode arguments!\n");
367 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
368 *statp = rpc_garbage_args;
372 /* need to grab the location to store the status, as
373 * nfsv4 does some encoding while processing
375 nfserrp = rqstp->rq_res.head[0].iov_base
376 + rqstp->rq_res.head[0].iov_len;
377 rqstp->rq_res.head[0].iov_len += sizeof(u32);
379 /* Now call the procedure handler, and encode NFS status. */
380 nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
381 if (nfserr == nfserr_jukebox && rqstp->rq_vers == 2)
382 nfserr = nfserr_dropit;
383 if (nfserr == nfserr_dropit) {
384 dprintk("nfsd: Dropping request due to malloc failure!\n");
385 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
389 if (rqstp->rq_proc != 0)
393 * For NFSv2, additional info is never returned in case of an error.
395 if (!(nfserr && rqstp->rq_vers == 2)) {
396 xdr = proc->pc_encode;
397 if (xdr && !xdr(rqstp, nfserrp,
399 /* Failed to encode result. Release cache entry */
400 dprintk("nfsd: failed to encode result!\n");
401 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
402 *statp = rpc_system_err;
407 /* Store reply in cache. */
408 nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);