Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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 #include <linux/nfsacl.h>
36
37 #define NFSDDBG_FACILITY        NFSDDBG_SVC
38
39 /* these signals will be delivered to an nfsd thread 
40  * when handling a request
41  */
42 #define ALLOWED_SIGS    (sigmask(SIGKILL))
43 /* these signals will be delivered to an nfsd thread
44  * when not handling a request. i.e. when waiting
45  */
46 #define SHUTDOWN_SIGS   (sigmask(SIGKILL) | sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT))
47 /* if the last thread dies with SIGHUP, then the exports table is
48  * left unchanged ( like 2.4-{0-9} ).  Any other signal will clear
49  * the exports table (like 2.2).
50  */
51 #define SIG_NOCLEAN     SIGHUP
52
53 extern struct svc_program       nfsd_program;
54 static void                     nfsd(struct svc_rqst *rqstp);
55 struct timeval                  nfssvc_boot;
56        struct svc_serv          *nfsd_serv;
57 static atomic_t                 nfsd_busy;
58 static unsigned long            nfsd_last_call;
59 static DEFINE_SPINLOCK(nfsd_call_lock);
60
61 struct nfsd_list {
62         struct list_head        list;
63         struct task_struct      *task;
64 };
65 static struct list_head nfsd_list = LIST_HEAD_INIT(nfsd_list);
66
67 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
68 static struct svc_stat  nfsd_acl_svcstats;
69 static struct svc_version *     nfsd_acl_version[] = {
70         [2] = &nfsd_acl_version2,
71         [3] = &nfsd_acl_version3,
72 };
73
74 #define NFSD_ACL_MINVERS            2
75 #define NFSD_ACL_NRVERS         ARRAY_SIZE(nfsd_acl_version)
76 static struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
77
78 static struct svc_program       nfsd_acl_program = {
79         .pg_prog                = NFS_ACL_PROGRAM,
80         .pg_nvers               = NFSD_ACL_NRVERS,
81         .pg_vers                = nfsd_acl_versions,
82         .pg_name                = "nfsd",
83         .pg_class               = "nfsd",
84         .pg_stats               = &nfsd_acl_svcstats,
85         .pg_authenticate        = &svc_set_client,
86 };
87
88 static struct svc_stat  nfsd_acl_svcstats = {
89         .program        = &nfsd_acl_program,
90 };
91 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
92
93 extern struct svc_version nfsd_version2, nfsd_version3, nfsd_version4;
94
95 static 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
106 #define NFSD_NRVERS             ARRAY_SIZE(nfsd_version)
107 static struct svc_version *nfsd_versions[NFSD_NRVERS];
108
109 struct svc_program              nfsd_program = {
110 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
111         .pg_next                = &nfsd_acl_program,
112 #endif
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
123 /*
124  * Maximum number of nfsd processes
125  */
126 #define NFSD_MAXSERVS           8192
127
128 int nfsd_nrthreads(void)
129 {
130         if (nfsd_serv == NULL)
131                 return 0;
132         else
133                 return nfsd_serv->sv_nrthreads;
134 }
135
136 int
137 nfsd_svc(unsigned short port, int nrservs)
138 {
139         int     error;
140         int     none_left, found_one, i;
141         struct list_head *victim;
142         
143         lock_kernel();
144         dprintk("nfsd: creating service: port %d vers 0x%x proto 0x%x\n",
145                 nfsd_port, nfsd_versbits, nfsd_portbits);
146         error = -EINVAL;
147         if (nrservs <= 0)
148                 nrservs = 0;
149         if (nrservs > NFSD_MAXSERVS)
150                 nrservs = NFSD_MAXSERVS;
151         
152         /* Readahead param cache - will no-op if it already exists */
153         error = nfsd_racache_init(2*nrservs);
154         if (error<0)
155                 goto out;
156         error = nfs4_state_start();
157         if (error<0)
158                 goto out;
159         if (!nfsd_serv) {
160                 /*
161                  * Use the nfsd_ctlbits to define which
162                  * versions that will be advertised.
163                  * If nfsd_ctlbits doesn't list any version,
164                  * export them all.
165                  */
166                 found_one = 0;
167
168                 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
169                         if (NFSCTL_VERISSET(nfsd_versbits, i)) {
170                                 nfsd_program.pg_vers[i] = nfsd_version[i];
171                                 found_one = 1;
172                         } else
173                                 nfsd_program.pg_vers[i] = NULL;
174                 }
175
176                 if (!found_one) {
177                         for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
178                                 nfsd_program.pg_vers[i] = nfsd_version[i];
179                 }
180
181
182 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
183                 found_one = 0;
184
185                 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
186                         if (NFSCTL_VERISSET(nfsd_versbits, i)) {
187                                 nfsd_acl_program.pg_vers[i] =
188                                         nfsd_acl_version[i];
189                                 found_one = 1;
190                         } else
191                                 nfsd_acl_program.pg_vers[i] = NULL;
192                 }
193
194                 if (!found_one) {
195                         for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++)
196                                 nfsd_acl_program.pg_vers[i] =
197                                         nfsd_acl_version[i];
198                 }
199 #endif
200
201                 atomic_set(&nfsd_busy, 0);
202                 error = -ENOMEM;
203                 nfsd_serv = svc_create(&nfsd_program, NFSD_BUFSIZE);
204                 if (nfsd_serv == NULL)
205                         goto out;
206                 if (NFSCTL_UDPISSET(nfsd_portbits))
207                         port = nfsd_port;
208                 error = svc_makesock(nfsd_serv, IPPROTO_UDP, port);
209                 if (error < 0)
210                         goto failure;
211
212 #ifdef CONFIG_NFSD_TCP
213                 if (NFSCTL_TCPISSET(nfsd_portbits))
214                         port = nfsd_port;
215                 error = svc_makesock(nfsd_serv, IPPROTO_TCP, port);
216                 if (error < 0)
217                         goto failure;
218 #endif
219                 do_gettimeofday(&nfssvc_boot);          /* record boot time */
220         } else
221                 nfsd_serv->sv_nrthreads++;
222         nrservs -= (nfsd_serv->sv_nrthreads-1);
223         while (nrservs > 0) {
224                 nrservs--;
225                 __module_get(THIS_MODULE);
226                 error = svc_create_thread(nfsd, nfsd_serv);
227                 if (error < 0) {
228                         module_put(THIS_MODULE);
229                         break;
230                 }
231         }
232         victim = nfsd_list.next;
233         while (nrservs < 0 && victim != &nfsd_list) {
234                 struct nfsd_list *nl =
235                         list_entry(victim,struct nfsd_list, list);
236                 victim = victim->next;
237                 send_sig(SIG_NOCLEAN, nl->task, 1);
238                 nrservs++;
239         }
240  failure:
241         none_left = (nfsd_serv->sv_nrthreads == 1);
242         svc_destroy(nfsd_serv);         /* Release server */
243         if (none_left) {
244                 nfsd_serv = NULL;
245                 nfsd_racache_shutdown();
246                 nfs4_state_shutdown();
247         }
248  out:
249         unlock_kernel();
250         return error;
251 }
252
253 static inline void
254 update_thread_usage(int busy_threads)
255 {
256         unsigned long prev_call;
257         unsigned long diff;
258         int decile;
259
260         spin_lock(&nfsd_call_lock);
261         prev_call = nfsd_last_call;
262         nfsd_last_call = jiffies;
263         decile = busy_threads*10/nfsdstats.th_cnt;
264         if (decile>0 && decile <= 10) {
265                 diff = nfsd_last_call - prev_call;
266                 if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP)
267                         nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP;
268                 if (decile == 10)
269                         nfsdstats.th_fullcnt++;
270         }
271         spin_unlock(&nfsd_call_lock);
272 }
273
274 /*
275  * This is the NFS server kernel thread
276  */
277 static void
278 nfsd(struct svc_rqst *rqstp)
279 {
280         struct svc_serv *serv = rqstp->rq_server;
281         struct fs_struct *fsp;
282         int             err;
283         struct nfsd_list me;
284         sigset_t shutdown_mask, allowed_mask;
285
286         /* Lock module and set up kernel thread */
287         lock_kernel();
288         daemonize("nfsd");
289
290         /* After daemonize() this kernel thread shares current->fs
291          * with the init process. We need to create files with a
292          * umask of 0 instead of init's umask. */
293         fsp = copy_fs_struct(current->fs);
294         if (!fsp) {
295                 printk("Unable to start nfsd thread: out of memory\n");
296                 goto out;
297         }
298         exit_fs(current);
299         current->fs = fsp;
300         current->fs->umask = 0;
301
302         siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS);
303         siginitsetinv(&allowed_mask, ALLOWED_SIGS);
304
305         nfsdstats.th_cnt++;
306
307         lockd_up();                             /* start lockd */
308
309         me.task = current;
310         list_add(&me.list, &nfsd_list);
311
312         unlock_kernel();
313
314         /*
315          * We want less throttling in balance_dirty_pages() so that nfs to
316          * localhost doesn't cause nfsd to lock up due to all the client's
317          * dirty pages.
318          */
319         current->flags |= PF_LESS_THROTTLE;
320
321         /*
322          * The main request loop
323          */
324         for (;;) {
325                 /* Block all but the shutdown signals */
326                 sigprocmask(SIG_SETMASK, &shutdown_mask, NULL);
327
328                 /*
329                  * Find a socket with data available and call its
330                  * recvfrom routine.
331                  */
332                 while ((err = svc_recv(serv, rqstp,
333                                        60*60*HZ)) == -EAGAIN)
334                         ;
335                 if (err < 0)
336                         break;
337                 update_thread_usage(atomic_read(&nfsd_busy));
338                 atomic_inc(&nfsd_busy);
339
340                 /* Lock the export hash tables for reading. */
341                 exp_readlock();
342
343                 /* Process request with signals blocked.  */
344                 sigprocmask(SIG_SETMASK, &allowed_mask, NULL);
345
346                 svc_process(serv, rqstp);
347
348                 /* Unlock export hash tables */
349                 exp_readunlock();
350                 update_thread_usage(atomic_read(&nfsd_busy));
351                 atomic_dec(&nfsd_busy);
352         }
353
354         if (err != -EINTR) {
355                 printk(KERN_WARNING "nfsd: terminating on error %d\n", -err);
356         } else {
357                 unsigned int    signo;
358
359                 for (signo = 1; signo <= _NSIG; signo++)
360                         if (sigismember(&current->pending.signal, signo) &&
361                             !sigismember(&current->blocked, signo))
362                                 break;
363                 err = signo;
364         }
365         /* Clear signals before calling lockd_down() and svc_exit_thread() */
366         flush_signals(current);
367
368         lock_kernel();
369
370         /* Release lockd */
371         lockd_down();
372
373         /* Check if this is last thread */
374         if (serv->sv_nrthreads==1) {
375                 
376                 printk(KERN_WARNING "nfsd: last server has exited\n");
377                 if (err != SIG_NOCLEAN) {
378                         printk(KERN_WARNING "nfsd: unexporting all filesystems\n");
379                         nfsd_export_flush();
380                 }
381                 nfsd_serv = NULL;
382                 nfsd_racache_shutdown();        /* release read-ahead cache */
383                 nfs4_state_shutdown();
384         }
385         list_del(&me.list);
386         nfsdstats.th_cnt --;
387
388 out:
389         /* Release the thread */
390         svc_exit_thread(rqstp);
391
392         /* Release module */
393         unlock_kernel();
394         module_put_and_exit(0);
395 }
396
397 int
398 nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp)
399 {
400         struct svc_procedure    *proc;
401         kxdrproc_t              xdr;
402         u32                     nfserr;
403         u32                     *nfserrp;
404
405         dprintk("nfsd_dispatch: vers %d proc %d\n",
406                                 rqstp->rq_vers, rqstp->rq_proc);
407         proc = rqstp->rq_procinfo;
408
409         /* Check whether we have this call in the cache. */
410         switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
411         case RC_INTR:
412         case RC_DROPIT:
413                 return 0;
414         case RC_REPLY:
415                 return 1;
416         case RC_DOIT:;
417                 /* do it */
418         }
419
420         /* Decode arguments */
421         xdr = proc->pc_decode;
422         if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base,
423                         rqstp->rq_argp)) {
424                 dprintk("nfsd: failed to decode arguments!\n");
425                 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
426                 *statp = rpc_garbage_args;
427                 return 1;
428         }
429
430         /* need to grab the location to store the status, as
431          * nfsv4 does some encoding while processing 
432          */
433         nfserrp = rqstp->rq_res.head[0].iov_base
434                 + rqstp->rq_res.head[0].iov_len;
435         rqstp->rq_res.head[0].iov_len += sizeof(u32);
436
437         /* Now call the procedure handler, and encode NFS status. */
438         nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
439         if (nfserr == nfserr_jukebox && rqstp->rq_vers == 2)
440                 nfserr = nfserr_dropit;
441         if (nfserr == nfserr_dropit) {
442                 dprintk("nfsd: Dropping request due to malloc failure!\n");
443                 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
444                 return 0;
445         }
446
447         if (rqstp->rq_proc != 0)
448                 *nfserrp++ = nfserr;
449
450         /* Encode result.
451          * For NFSv2, additional info is never returned in case of an error.
452          */
453         if (!(nfserr && rqstp->rq_vers == 2)) {
454                 xdr = proc->pc_encode;
455                 if (xdr && !xdr(rqstp, nfserrp,
456                                 rqstp->rq_resp)) {
457                         /* Failed to encode result. Release cache entry */
458                         dprintk("nfsd: failed to encode result!\n");
459                         nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
460                         *statp = rpc_system_err;
461                         return 1;
462                 }
463         }
464
465         /* Store reply in cache. */
466         nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
467         return 1;
468 }