upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[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/nfsd/syscall.h>
34 #include <linux/lockd/bind.h>
35
36 #define NFSDDBG_FACILITY        NFSDDBG_SVC
37
38 /* these signals will be delivered to an nfsd thread 
39  * when handling a request
40  */
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
44  */
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).
49  */
50 #define SIG_NOCLEAN     SIGHUP
51
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);
56
57 struct nfsd_list {
58         struct list_head        list;
59         struct task_struct      *task;
60 };
61 static struct list_head nfsd_list = LIST_HEAD_INIT(nfsd_list);
62
63 extern struct svc_version nfsd_version2, nfsd_version3, nfsd_version4;
64
65 static struct svc_version *     nfsd_version[] = {
66         [2] = &nfsd_version2,
67 #if defined(CONFIG_NFSD_V3)
68         [3] = &nfsd_version3,
69 #endif
70 #if defined(CONFIG_NFSD_V4)
71         [4] = &nfsd_version4,
72 #endif
73 };
74
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 */
85
86 };
87
88 /*
89  * Maximum number of nfsd processes
90  */
91 #define NFSD_MAXSERVS           8192
92
93 int nfsd_nrthreads(void)
94 {
95         if (nfsd_serv == NULL)
96                 return 0;
97         else
98                 return nfsd_serv->sv_nrthreads;
99 }
100
101 int
102 nfsd_svc(unsigned short port, int nrservs)
103 {
104         int     error;
105         int     none_left, found_one, i;
106         struct list_head *victim;
107         
108         lock_kernel();
109         dprintk("nfsd: creating service: port %d vers 0x%x proto 0x%x\n",
110                 nfsd_port, nfsd_versbits, nfsd_portbits);
111         error = -EINVAL;
112         if (nrservs <= 0)
113                 nrservs = 0;
114         if (nrservs > NFSD_MAXSERVS)
115                 nrservs = NFSD_MAXSERVS;
116         
117         /*
118          * If set, use the nfsd_ctlbits to define which
119          * versions that will be advertised
120          */
121         found_one = 0;
122         if (nfsd_versbits) {
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];
126                                 found_one = 1;
127                         } else
128                                 nfsd_program.pg_vers[i] = NULL;
129                 }
130         }
131         if (!found_one) {
132                 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
133                         nfsd_program.pg_vers[i] = nfsd_version[i];
134         }
135
136         /* Readahead param cache - will no-op if it already exists */
137         error = nfsd_racache_init(2*nrservs);
138         if (error<0)
139                 goto out;
140         error = nfs4_state_init();
141         if (error<0)
142                 goto out;
143
144         if (!nfsd_serv) {
145                 atomic_set(&nfsd_busy, 0);
146                 error = -ENOMEM;
147                 nfsd_serv = svc_create(&nfsd_program, NFSD_BUFSIZE);
148                 if (nfsd_serv == NULL)
149                         goto out;
150                 if (!nfsd_portbits || NFSCTL_UDPISSET(nfsd_portbits)) {
151                         error = svc_makesock(nfsd_serv, IPPROTO_UDP, nfsd_port);
152                         if (error < 0)
153                                 goto failure;
154                 }
155 #ifdef CONFIG_NFSD_TCP
156                 if (!nfsd_portbits || NFSCTL_TCPISSET(nfsd_portbits)) {
157                     error = svc_makesock(nfsd_serv, IPPROTO_TCP, nfsd_port);
158                     if (error < 0)
159                             goto failure;
160                 }
161 #endif
162                 do_gettimeofday(&nfssvc_boot);          /* record boot time */
163         } else
164                 nfsd_serv->sv_nrthreads++;
165         nrservs -= (nfsd_serv->sv_nrthreads-1);
166         while (nrservs > 0) {
167                 nrservs--;
168                 __module_get(THIS_MODULE);
169                 error = svc_create_thread(nfsd, nfsd_serv);
170                 if (error < 0) {
171                         module_put(THIS_MODULE);
172                         break;
173                 }
174         }
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);
181                 nrservs++;
182         }
183  failure:
184         none_left = (nfsd_serv->sv_nrthreads == 1);
185         svc_destroy(nfsd_serv);         /* Release server */
186         if (none_left) {
187                 nfsd_serv = NULL;
188                 nfsd_racache_shutdown();
189                 nfs4_state_shutdown();
190         }
191  out:
192         unlock_kernel();
193         return error;
194 }
195
196 static inline void
197 update_thread_usage(int busy_threads)
198 {
199         unsigned long prev_call;
200         unsigned long diff;
201         int decile;
202
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;
211                 if (decile == 10)
212                         nfsdstats.th_fullcnt++;
213         }
214         spin_unlock(&nfsd_call_lock);
215 }
216
217 /*
218  * This is the NFS server kernel thread
219  */
220 static void
221 nfsd(struct svc_rqst *rqstp)
222 {
223         struct svc_serv *serv = rqstp->rq_server;
224         struct fs_struct *fsp;
225         int             err;
226         struct nfsd_list me;
227         sigset_t shutdown_mask, allowed_mask;
228
229         /* Lock module and set up kernel thread */
230         lock_kernel();
231         daemonize("nfsd");
232
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);
237         if (!fsp) {
238                 printk("Unable to start nfsd thread: out of memory\n");
239                 goto out;
240         }
241         exit_fs(current);
242         current->fs = fsp;
243         current->fs->umask = 0;
244
245         siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS);
246         siginitsetinv(&allowed_mask, ALLOWED_SIGS);
247
248         nfsdstats.th_cnt++;
249
250         lockd_up();                             /* start lockd */
251
252         me.task = current;
253         list_add(&me.list, &nfsd_list);
254
255         unlock_kernel();
256
257         /*
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
260          * dirty pages.
261          */
262         current->flags |= PF_LESS_THROTTLE;
263
264         /*
265          * The main request loop
266          */
267         for (;;) {
268                 /* Block all but the shutdown signals */
269                 sigprocmask(SIG_SETMASK, &shutdown_mask, NULL);
270
271                 /*
272                  * Find a socket with data available and call its
273                  * recvfrom routine.
274                  */
275                 while ((err = svc_recv(serv, rqstp,
276                                        60*60*HZ)) == -EAGAIN)
277                         ;
278                 if (err < 0)
279                         break;
280                 update_thread_usage(atomic_read(&nfsd_busy));
281                 atomic_inc(&nfsd_busy);
282
283                 /* Lock the export hash tables for reading. */
284                 exp_readlock();
285
286                 /* Process request with signals blocked.  */
287                 sigprocmask(SIG_SETMASK, &allowed_mask, NULL);
288
289                 svc_process(serv, rqstp);
290
291                 /* Unlock export hash tables */
292                 exp_readunlock();
293                 update_thread_usage(atomic_read(&nfsd_busy));
294                 atomic_dec(&nfsd_busy);
295         }
296
297         if (err != -EINTR) {
298                 printk(KERN_WARNING "nfsd: terminating on error %d\n", -err);
299         } else {
300                 unsigned int    signo;
301
302                 for (signo = 1; signo <= _NSIG; signo++)
303                         if (sigismember(&current->pending.signal, signo) &&
304                             !sigismember(&current->blocked, signo))
305                                 break;
306                 err = signo;
307         }
308         /* Clear signals before calling lockd_down() and svc_exit_thread() */
309         flush_signals(current);
310
311         lock_kernel();
312
313         /* Release lockd */
314         lockd_down();
315
316         /* Check if this is last thread */
317         if (serv->sv_nrthreads==1) {
318                 
319                 printk(KERN_WARNING "nfsd: last server has exited\n");
320                 if (err != SIG_NOCLEAN) {
321                         printk(KERN_WARNING "nfsd: unexporting all filesystems\n");
322                         nfsd_export_flush();
323                 }
324                 nfsd_serv = NULL;
325                 nfsd_racache_shutdown();        /* release read-ahead cache */
326                 nfs4_state_shutdown();
327         }
328         list_del(&me.list);
329         nfsdstats.th_cnt --;
330
331 out:
332         /* Release the thread */
333         svc_exit_thread(rqstp);
334
335         /* Release module */
336         module_put_and_exit(0);
337 }
338
339 int
340 nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp)
341 {
342         struct svc_procedure    *proc;
343         kxdrproc_t              xdr;
344         u32                     nfserr;
345         u32                     *nfserrp;
346
347         dprintk("nfsd_dispatch: vers %d proc %d\n",
348                                 rqstp->rq_vers, rqstp->rq_proc);
349         proc = rqstp->rq_procinfo;
350
351         /* Check whether we have this call in the cache. */
352         switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
353         case RC_INTR:
354         case RC_DROPIT:
355                 return 0;
356         case RC_REPLY:
357                 return 1;
358         case RC_DOIT:;
359                 /* do it */
360         }
361
362         /* Decode arguments */
363         xdr = proc->pc_decode;
364         if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base,
365                         rqstp->rq_argp)) {
366                 dprintk("nfsd: failed to decode arguments!\n");
367                 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
368                 *statp = rpc_garbage_args;
369                 return 1;
370         }
371
372         /* need to grab the location to store the status, as
373          * nfsv4 does some encoding while processing 
374          */
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);
378
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);
386                 return 0;
387         }
388
389         if (rqstp->rq_proc != 0)
390                 *nfserrp++ = nfserr;
391
392         /* Encode result.
393          * For NFSv2, additional info is never returned in case of an error.
394          */
395         if (!(nfserr && rqstp->rq_vers == 2)) {
396                 xdr = proc->pc_encode;
397                 if (xdr && !xdr(rqstp, nfserrp,
398                                 rqstp->rq_resp)) {
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;
403                         return 1;
404                 }
405         }
406
407         /* Store reply in cache. */
408         nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
409         return 1;
410 }
411