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