ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / nfsd / nfssvc.c
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
11 #include <linux/config.h>
12 #include <linux/module.h>
13
14 #include <linux/time.h>
15 #include <linux/errno.h>
16 #include <linux/nfs.h>
17 #include <linux/in.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>
24
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/lockd/bind.h>
34
35 #define NFSDDBG_FACILITY        NFSDDBG_SVC
36
37 /* these signals will be delivered to an nfsd thread 
38  * when handling a request
39  */
40 #define ALLOWED_SIGS    (sigmask(SIGKILL))
41 /* these signals will be delivered to an nfsd thread
42  * when not handling a request. i.e. when waiting
43  */
44 #define SHUTDOWN_SIGS   (sigmask(SIGKILL) | sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT))
45 /* if the last thread dies with SIGHUP, then the exports table is
46  * left unchanged ( like 2.4-{0-9} ).  Any other signal will clear
47  * the exports table (like 2.2).
48  */
49 #define SIG_NOCLEAN     SIGHUP
50
51 extern struct svc_program       nfsd_program;
52 static void                     nfsd(struct svc_rqst *rqstp);
53 struct timeval                  nfssvc_boot;
54 static struct svc_serv          *nfsd_serv;
55 static atomic_t                 nfsd_busy;
56 static unsigned long            nfsd_last_call;
57 static spinlock_t               nfsd_call_lock = SPIN_LOCK_UNLOCKED;
58
59 struct nfsd_list {
60         struct list_head        list;
61         struct task_struct      *task;
62 };
63 struct list_head nfsd_list = LIST_HEAD_INIT(nfsd_list);
64
65 /*
66  * Maximum number of nfsd processes
67  */
68 #define NFSD_MAXSERVS           8192
69
70 int nfsd_nrthreads(void)
71 {
72         if (nfsd_serv == NULL)
73                 return 0;
74         else
75                 return nfsd_serv->sv_nrthreads;
76 }
77
78 int
79 nfsd_svc(unsigned short port, int nrservs)
80 {
81         int     error;
82         int     none_left;      
83         struct list_head *victim;
84         
85         lock_kernel();
86         dprintk("nfsd: creating service\n");
87         error = -EINVAL;
88         if (nrservs <= 0)
89                 nrservs = 0;
90         if (nrservs > NFSD_MAXSERVS)
91                 nrservs = NFSD_MAXSERVS;
92         
93         /* Readahead param cache - will no-op if it already exists */
94         error = nfsd_racache_init(2*nrservs);
95         nfs4_state_init();
96         if (error<0)
97                 goto out;
98         if (!nfsd_serv) {
99                 atomic_set(&nfsd_busy, 0);
100                 error = -ENOMEM;
101                 nfsd_serv = svc_create(&nfsd_program, NFSD_BUFSIZE);
102                 if (nfsd_serv == NULL)
103                         goto out;
104                 error = svc_makesock(nfsd_serv, IPPROTO_UDP, port);
105                 if (error < 0)
106                         goto failure;
107
108 #ifdef CONFIG_NFSD_TCP
109                 error = svc_makesock(nfsd_serv, IPPROTO_TCP, port);
110                 if (error < 0)
111                         goto failure;
112 #endif
113                 do_gettimeofday(&nfssvc_boot);          /* record boot time */
114         } else
115                 nfsd_serv->sv_nrthreads++;
116         nrservs -= (nfsd_serv->sv_nrthreads-1);
117         while (nrservs > 0) {
118                 nrservs--;
119                 __module_get(THIS_MODULE);
120                 error = svc_create_thread(nfsd, nfsd_serv);
121                 if (error < 0) {
122                         module_put(THIS_MODULE);
123                         break;
124                 }
125         }
126         victim = nfsd_list.next;
127         while (nrservs < 0 && victim != &nfsd_list) {
128                 struct nfsd_list *nl =
129                         list_entry(victim,struct nfsd_list, list);
130                 victim = victim->next;
131                 send_sig(SIG_NOCLEAN, nl->task, 1);
132                 nrservs++;
133         }
134  failure:
135         none_left = (nfsd_serv->sv_nrthreads == 1);
136         svc_destroy(nfsd_serv);         /* Release server */
137         if (none_left) {
138                 nfsd_serv = NULL;
139                 nfsd_racache_shutdown();
140                 nfs4_state_shutdown();
141         }
142  out:
143         unlock_kernel();
144         return error;
145 }
146
147 static inline void
148 update_thread_usage(int busy_threads)
149 {
150         unsigned long prev_call;
151         unsigned long diff;
152         int decile;
153
154         spin_lock(&nfsd_call_lock);
155         prev_call = nfsd_last_call;
156         nfsd_last_call = jiffies;
157         decile = busy_threads*10/nfsdstats.th_cnt;
158         if (decile>0 && decile <= 10) {
159                 diff = nfsd_last_call - prev_call;
160                 if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP)
161                         nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP;
162                 if (decile == 10)
163                         nfsdstats.th_fullcnt++;
164         }
165         spin_unlock(&nfsd_call_lock);
166 }
167
168 /*
169  * This is the NFS server kernel thread
170  */
171 static void
172 nfsd(struct svc_rqst *rqstp)
173 {
174         struct svc_serv *serv = rqstp->rq_server;
175         struct fs_struct *fsp;
176         int             err;
177         struct nfsd_list me;
178         sigset_t shutdown_mask, allowed_mask;
179
180         /* Lock module and set up kernel thread */
181         lock_kernel();
182         daemonize("nfsd");
183         current->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
184
185         /* After daemonize() this kernel thread shares current->fs
186          * with the init process. We need to create files with a
187          * umask of 0 instead of init's umask. */
188         fsp = copy_fs_struct(current->fs);
189         if (!fsp) {
190                 printk("Unable to start nfsd thread: out of memory\n");
191                 goto out;
192         }
193         exit_fs(current);
194         current->fs = fsp;
195         current->fs->umask = 0;
196
197         siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS);
198         siginitsetinv(&allowed_mask, ALLOWED_SIGS);
199
200         nfsdstats.th_cnt++;
201
202         lockd_up();                             /* start lockd */
203
204         me.task = current;
205         list_add(&me.list, &nfsd_list);
206
207         unlock_kernel();
208
209         /*
210          * We want less throttling in balance_dirty_pages() so that nfs to
211          * localhost doesn't cause nfsd to lock up due to all the client's
212          * dirty pages.
213          */
214         current->flags |= PF_LESS_THROTTLE;
215
216         /*
217          * The main request loop
218          */
219         for (;;) {
220                 /* Block all but the shutdown signals */
221                 sigprocmask(SIG_SETMASK, &shutdown_mask, NULL);
222
223                 /*
224                  * Find a socket with data available and call its
225                  * recvfrom routine.
226                  */
227                 while ((err = svc_recv(serv, rqstp,
228                                        60*60*HZ)) == -EAGAIN)
229                         ;
230                 if (err < 0)
231                         break;
232                 update_thread_usage(atomic_read(&nfsd_busy));
233                 atomic_inc(&nfsd_busy);
234
235                 /* Lock the export hash tables for reading. */
236                 exp_readlock();
237
238                 /* Process request with signals blocked.  */
239                 sigprocmask(SIG_SETMASK, &allowed_mask, NULL);
240
241                 svc_process(serv, rqstp);
242
243                 /* Unlock export hash tables */
244                 exp_readunlock();
245                 update_thread_usage(atomic_read(&nfsd_busy));
246                 atomic_dec(&nfsd_busy);
247         }
248
249         if (err != -EINTR) {
250                 printk(KERN_WARNING "nfsd: terminating on error %d\n", -err);
251         } else {
252                 unsigned int    signo;
253
254                 for (signo = 1; signo <= _NSIG; signo++)
255                         if (sigismember(&current->pending.signal, signo) &&
256                             !sigismember(&current->blocked, signo))
257                                 break;
258                 err = signo;
259         }
260
261         lock_kernel();
262
263         /* Release lockd */
264         lockd_down();
265
266         /* Check if this is last thread */
267         if (serv->sv_nrthreads==1) {
268                 
269                 printk(KERN_WARNING "nfsd: last server has exited\n");
270                 if (err != SIG_NOCLEAN) {
271                         printk(KERN_WARNING "nfsd: unexporting all filesystems\n");
272                         nfsd_export_flush();
273                 }
274                 nfsd_serv = NULL;
275                 nfsd_racache_shutdown();        /* release read-ahead cache */
276                 nfs4_state_shutdown();
277         }
278         list_del(&me.list);
279         nfsdstats.th_cnt --;
280
281 out:
282         /* Release the thread */
283         svc_exit_thread(rqstp);
284
285         /* Release module */
286         module_put_and_exit(0);
287 }
288
289 int
290 nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp)
291 {
292         struct svc_procedure    *proc;
293         kxdrproc_t              xdr;
294         u32                     nfserr;
295         u32                     *nfserrp;
296
297         dprintk("nfsd_dispatch: vers %d proc %d\n",
298                                 rqstp->rq_vers, rqstp->rq_proc);
299         proc = rqstp->rq_procinfo;
300
301         /* Check whether we have this call in the cache. */
302         switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
303         case RC_INTR:
304         case RC_DROPIT:
305                 return 0;
306         case RC_REPLY:
307                 return 1;
308         case RC_DOIT:;
309                 /* do it */
310         }
311
312         /* Decode arguments */
313         xdr = proc->pc_decode;
314         if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base,
315                         rqstp->rq_argp)) {
316                 dprintk("nfsd: failed to decode arguments!\n");
317                 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
318                 *statp = rpc_garbage_args;
319                 return 1;
320         }
321
322         /* need to grab the location to store the status, as
323          * nfsv4 does some encoding while processing 
324          */
325         nfserrp = rqstp->rq_res.head[0].iov_base
326                 + rqstp->rq_res.head[0].iov_len;
327         rqstp->rq_res.head[0].iov_len += sizeof(u32);
328
329         /* Now call the procedure handler, and encode NFS status. */
330         nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
331         if (nfserr == nfserr_dropit) {
332                 dprintk("nfsd: Dropping request due to malloc failure!\n");
333                 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
334                 return 0;
335         }
336
337         if (rqstp->rq_proc != 0)
338                 *nfserrp++ = nfserr;
339
340         /* Encode result.
341          * For NFSv2, additional info is never returned in case of an error.
342          */
343         if (!(nfserr && rqstp->rq_vers == 2)) {
344                 xdr = proc->pc_encode;
345                 if (xdr && !xdr(rqstp, nfserrp,
346                                 rqstp->rq_resp)) {
347                         /* Failed to encode result. Release cache entry */
348                         dprintk("nfsd: failed to encode result!\n");
349                         nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
350                         *statp = rpc_system_err;
351                         return 1;
352                 }
353         }
354
355         /* Store reply in cache. */
356         nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
357         return 1;
358 }
359
360 extern struct svc_version nfsd_version2, nfsd_version3, nfsd_version4;
361
362 static struct svc_version *     nfsd_version[] = {
363         [2] = &nfsd_version2,
364 #if defined(CONFIG_NFSD_V3)
365         [3] = &nfsd_version3,
366 #endif
367 #if defined(CONFIG_NFSD_V4)
368         [4] = &nfsd_version4,
369 #endif
370 };
371
372 #define NFSD_NRVERS             (sizeof(nfsd_version)/sizeof(nfsd_version[0]))
373 struct svc_program              nfsd_program = {
374         .pg_prog                = NFS_PROGRAM,          /* program number */
375         .pg_nvers               = NFSD_NRVERS,          /* nr of entries in nfsd_version */
376         .pg_vers                = nfsd_version,         /* version table */
377         .pg_name                = "nfsd",               /* program name */
378         .pg_class               = "nfsd",               /* authentication class */
379         .pg_stats               = &nfsd_svcstats,       /* version table */
380 };