c3eb0759fd57fef30d8e76bfe35a15c13bbd6bba
[safe/jmp/linux-2.6] / 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 #include <linux/sched.h>
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/freezer.h>
23 #include <linux/fs_struct.h>
24 #include <linux/kthread.h>
25
26 #include <linux/sunrpc/types.h>
27 #include <linux/sunrpc/stats.h>
28 #include <linux/sunrpc/svc.h>
29 #include <linux/sunrpc/svcsock.h>
30 #include <linux/sunrpc/cache.h>
31 #include <linux/nfsd/nfsd.h>
32 #include <linux/nfsd/stats.h>
33 #include <linux/nfsd/cache.h>
34 #include <linux/nfsd/syscall.h>
35 #include <linux/lockd/bind.h>
36 #include <linux/nfsacl.h>
37
38 #define NFSDDBG_FACILITY        NFSDDBG_SVC
39
40 extern struct svc_program       nfsd_program;
41 static int                      nfsd(void *vrqstp);
42 struct timeval                  nfssvc_boot;
43
44 /*
45  * nfsd_mutex protects nfsd_serv -- both the pointer itself and the members
46  * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
47  * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
48  *
49  * If (out side the lock) nfsd_serv is non-NULL, then it must point to a
50  * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
51  * of nfsd threads must exist and each must listed in ->sp_all_threads in each
52  * entry of ->sv_pools[].
53  *
54  * Transitions of the thread count between zero and non-zero are of particular
55  * interest since the svc_serv needs to be created and initialized at that
56  * point, or freed.
57  *
58  * Finally, the nfsd_mutex also protects some of the global variables that are
59  * accessed when nfsd starts and that are settable via the write_* routines in
60  * nfsctl.c. In particular:
61  *
62  *      user_recovery_dirname
63  *      user_lease_time
64  *      nfsd_versions
65  */
66 DEFINE_MUTEX(nfsd_mutex);
67 struct svc_serv                 *nfsd_serv;
68
69 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
70 static struct svc_stat  nfsd_acl_svcstats;
71 static struct svc_version *     nfsd_acl_version[] = {
72         [2] = &nfsd_acl_version2,
73         [3] = &nfsd_acl_version3,
74 };
75
76 #define NFSD_ACL_MINVERS            2
77 #define NFSD_ACL_NRVERS         ARRAY_SIZE(nfsd_acl_version)
78 static struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
79
80 static struct svc_program       nfsd_acl_program = {
81         .pg_prog                = NFS_ACL_PROGRAM,
82         .pg_nvers               = NFSD_ACL_NRVERS,
83         .pg_vers                = nfsd_acl_versions,
84         .pg_name                = "nfsacl",
85         .pg_class               = "nfsd",
86         .pg_stats               = &nfsd_acl_svcstats,
87         .pg_authenticate        = &svc_set_client,
88 };
89
90 static struct svc_stat  nfsd_acl_svcstats = {
91         .program        = &nfsd_acl_program,
92 };
93 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
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 int nfsd_vers(int vers, enum vers_op change)
124 {
125         if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
126                 return -1;
127         switch(change) {
128         case NFSD_SET:
129                 nfsd_versions[vers] = nfsd_version[vers];
130 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
131                 if (vers < NFSD_ACL_NRVERS)
132                         nfsd_acl_versions[vers] = nfsd_acl_version[vers];
133 #endif
134                 break;
135         case NFSD_CLEAR:
136                 nfsd_versions[vers] = NULL;
137 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
138                 if (vers < NFSD_ACL_NRVERS)
139                         nfsd_acl_versions[vers] = NULL;
140 #endif
141                 break;
142         case NFSD_TEST:
143                 return nfsd_versions[vers] != NULL;
144         case NFSD_AVAIL:
145                 return nfsd_version[vers] != NULL;
146         }
147         return 0;
148 }
149 /*
150  * Maximum number of nfsd processes
151  */
152 #define NFSD_MAXSERVS           8192
153
154 int nfsd_nrthreads(void)
155 {
156         int rv = 0;
157         mutex_lock(&nfsd_mutex);
158         if (nfsd_serv)
159                 rv = nfsd_serv->sv_nrthreads;
160         mutex_unlock(&nfsd_mutex);
161         return rv;
162 }
163
164 static void nfsd_last_thread(struct svc_serv *serv)
165 {
166         /* When last nfsd thread exits we need to do some clean-up */
167         struct svc_xprt *xprt;
168         list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list)
169                 lockd_down();
170         nfsd_serv = NULL;
171         nfsd_racache_shutdown();
172         nfs4_state_shutdown();
173
174         printk(KERN_WARNING "nfsd: last server has exited, flushing export "
175                             "cache\n");
176         nfsd_export_flush();
177 }
178
179 void nfsd_reset_versions(void)
180 {
181         int found_one = 0;
182         int i;
183
184         for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
185                 if (nfsd_program.pg_vers[i])
186                         found_one = 1;
187         }
188
189         if (!found_one) {
190                 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
191                         nfsd_program.pg_vers[i] = nfsd_version[i];
192 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
193                 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++)
194                         nfsd_acl_program.pg_vers[i] =
195                                 nfsd_acl_version[i];
196 #endif
197         }
198 }
199
200
201 int nfsd_create_serv(void)
202 {
203         int err = 0;
204
205         WARN_ON(!mutex_is_locked(&nfsd_mutex));
206         if (nfsd_serv) {
207                 svc_get(nfsd_serv);
208                 return 0;
209         }
210         if (nfsd_max_blksize == 0) {
211                 /* choose a suitable default */
212                 struct sysinfo i;
213                 si_meminfo(&i);
214                 /* Aim for 1/4096 of memory per thread
215                  * This gives 1MB on 4Gig machines
216                  * But only uses 32K on 128M machines.
217                  * Bottom out at 8K on 32M and smaller.
218                  * Of course, this is only a default.
219                  */
220                 nfsd_max_blksize = NFSSVC_MAXBLKSIZE;
221                 i.totalram <<= PAGE_SHIFT - 12;
222                 while (nfsd_max_blksize > i.totalram &&
223                        nfsd_max_blksize >= 8*1024*2)
224                         nfsd_max_blksize /= 2;
225         }
226
227         nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
228                                       AF_INET,
229                                       nfsd_last_thread, nfsd, THIS_MODULE);
230         if (nfsd_serv == NULL)
231                 err = -ENOMEM;
232
233         do_gettimeofday(&nfssvc_boot);          /* record boot time */
234         return err;
235 }
236
237 static int nfsd_init_socks(int port)
238 {
239         int error;
240         if (!list_empty(&nfsd_serv->sv_permsocks))
241                 return 0;
242
243         error = svc_create_xprt(nfsd_serv, "udp", port,
244                                         SVC_SOCK_DEFAULTS);
245         if (error < 0)
246                 return error;
247
248         error = lockd_up();
249         if (error < 0)
250                 return error;
251
252         error = svc_create_xprt(nfsd_serv, "tcp", port,
253                                         SVC_SOCK_DEFAULTS);
254         if (error < 0)
255                 return error;
256
257         error = lockd_up();
258         if (error < 0)
259                 return error;
260
261         return 0;
262 }
263
264 int nfsd_nrpools(void)
265 {
266         if (nfsd_serv == NULL)
267                 return 0;
268         else
269                 return nfsd_serv->sv_nrpools;
270 }
271
272 int nfsd_get_nrthreads(int n, int *nthreads)
273 {
274         int i = 0;
275
276         if (nfsd_serv != NULL) {
277                 for (i = 0; i < nfsd_serv->sv_nrpools && i < n; i++)
278                         nthreads[i] = nfsd_serv->sv_pools[i].sp_nrthreads;
279         }
280
281         return 0;
282 }
283
284 int nfsd_set_nrthreads(int n, int *nthreads)
285 {
286         int i = 0;
287         int tot = 0;
288         int err = 0;
289
290         WARN_ON(!mutex_is_locked(&nfsd_mutex));
291
292         if (nfsd_serv == NULL || n <= 0)
293                 return 0;
294
295         if (n > nfsd_serv->sv_nrpools)
296                 n = nfsd_serv->sv_nrpools;
297
298         /* enforce a global maximum number of threads */
299         tot = 0;
300         for (i = 0; i < n; i++) {
301                 if (nthreads[i] > NFSD_MAXSERVS)
302                         nthreads[i] = NFSD_MAXSERVS;
303                 tot += nthreads[i];
304         }
305         if (tot > NFSD_MAXSERVS) {
306                 /* total too large: scale down requested numbers */
307                 for (i = 0; i < n && tot > 0; i++) {
308                         int new = nthreads[i] * NFSD_MAXSERVS / tot;
309                         tot -= (nthreads[i] - new);
310                         nthreads[i] = new;
311                 }
312                 for (i = 0; i < n && tot > 0; i++) {
313                         nthreads[i]--;
314                         tot--;
315                 }
316         }
317
318         /*
319          * There must always be a thread in pool 0; the admin
320          * can't shut down NFS completely using pool_threads.
321          */
322         if (nthreads[0] == 0)
323                 nthreads[0] = 1;
324
325         /* apply the new numbers */
326         svc_get(nfsd_serv);
327         for (i = 0; i < n; i++) {
328                 err = svc_set_num_threads(nfsd_serv, &nfsd_serv->sv_pools[i],
329                                           nthreads[i]);
330                 if (err)
331                         break;
332         }
333         svc_destroy(nfsd_serv);
334
335         return err;
336 }
337
338 int
339 nfsd_svc(unsigned short port, int nrservs)
340 {
341         int     error;
342
343         mutex_lock(&nfsd_mutex);
344         dprintk("nfsd: creating service\n");
345         error = -EINVAL;
346         if (nrservs <= 0)
347                 nrservs = 0;
348         if (nrservs > NFSD_MAXSERVS)
349                 nrservs = NFSD_MAXSERVS;
350         
351         /* Readahead param cache - will no-op if it already exists */
352         error = nfsd_racache_init(2*nrservs);
353         if (error<0)
354                 goto out;
355         nfs4_state_start();
356
357         nfsd_reset_versions();
358
359         error = nfsd_create_serv();
360
361         if (error)
362                 goto out;
363         error = nfsd_init_socks(port);
364         if (error)
365                 goto failure;
366
367         error = svc_set_num_threads(nfsd_serv, NULL, nrservs);
368  failure:
369         svc_destroy(nfsd_serv);         /* Release server */
370  out:
371         mutex_unlock(&nfsd_mutex);
372         return error;
373 }
374
375
376 /*
377  * This is the NFS server kernel thread
378  */
379 static int
380 nfsd(void *vrqstp)
381 {
382         struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
383         struct fs_struct *fsp;
384         int err, preverr = 0;
385
386         /* Lock module and set up kernel thread */
387         mutex_lock(&nfsd_mutex);
388
389         /* At this point, the thread shares current->fs
390          * with the init process. We need to create files with a
391          * umask of 0 instead of init's umask. */
392         fsp = copy_fs_struct(current->fs);
393         if (!fsp) {
394                 printk("Unable to start nfsd thread: out of memory\n");
395                 goto out;
396         }
397         exit_fs(current);
398         current->fs = fsp;
399         current->fs->umask = 0;
400
401         /*
402          * thread is spawned with all signals set to SIG_IGN, re-enable
403          * the ones that will bring down the thread
404          */
405         allow_signal(SIGKILL);
406         allow_signal(SIGHUP);
407         allow_signal(SIGINT);
408         allow_signal(SIGQUIT);
409
410         nfsdstats.th_cnt++;
411         mutex_unlock(&nfsd_mutex);
412
413         /*
414          * We want less throttling in balance_dirty_pages() so that nfs to
415          * localhost doesn't cause nfsd to lock up due to all the client's
416          * dirty pages.
417          */
418         current->flags |= PF_LESS_THROTTLE;
419         set_freezable();
420
421         /*
422          * The main request loop
423          */
424         for (;;) {
425                 /*
426                  * Find a socket with data available and call its
427                  * recvfrom routine.
428                  */
429                 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
430                         ;
431                 if (err == -EINTR)
432                         break;
433                 else if (err < 0) {
434                         if (err != preverr) {
435                                 printk(KERN_WARNING "%s: unexpected error "
436                                         "from svc_recv (%d)\n", __func__, -err);
437                                 preverr = err;
438                         }
439                         schedule_timeout_uninterruptible(HZ);
440                         continue;
441                 }
442
443
444                 /* Lock the export hash tables for reading. */
445                 exp_readlock();
446
447                 svc_process(rqstp);
448
449                 /* Unlock export hash tables */
450                 exp_readunlock();
451         }
452
453         /* Clear signals before calling svc_exit_thread() */
454         flush_signals(current);
455
456         mutex_lock(&nfsd_mutex);
457         nfsdstats.th_cnt --;
458
459 out:
460         /* Release the thread */
461         svc_exit_thread(rqstp);
462
463         /* Release module */
464         mutex_unlock(&nfsd_mutex);
465         module_put_and_exit(0);
466         return 0;
467 }
468
469 static __be32 map_new_errors(u32 vers, __be32 nfserr)
470 {
471         if (nfserr == nfserr_jukebox && vers == 2)
472                 return nfserr_dropit;
473         if (nfserr == nfserr_wrongsec && vers < 4)
474                 return nfserr_acces;
475         return nfserr;
476 }
477
478 int
479 nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
480 {
481         struct svc_procedure    *proc;
482         kxdrproc_t              xdr;
483         __be32                  nfserr;
484         __be32                  *nfserrp;
485
486         dprintk("nfsd_dispatch: vers %d proc %d\n",
487                                 rqstp->rq_vers, rqstp->rq_proc);
488         proc = rqstp->rq_procinfo;
489
490         /* Check whether we have this call in the cache. */
491         switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
492         case RC_INTR:
493         case RC_DROPIT:
494                 return 0;
495         case RC_REPLY:
496                 return 1;
497         case RC_DOIT:;
498                 /* do it */
499         }
500
501         /* Decode arguments */
502         xdr = proc->pc_decode;
503         if (xdr && !xdr(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base,
504                         rqstp->rq_argp)) {
505                 dprintk("nfsd: failed to decode arguments!\n");
506                 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
507                 *statp = rpc_garbage_args;
508                 return 1;
509         }
510
511         /* need to grab the location to store the status, as
512          * nfsv4 does some encoding while processing 
513          */
514         nfserrp = rqstp->rq_res.head[0].iov_base
515                 + rqstp->rq_res.head[0].iov_len;
516         rqstp->rq_res.head[0].iov_len += sizeof(__be32);
517
518         /* Now call the procedure handler, and encode NFS status. */
519         nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
520         nfserr = map_new_errors(rqstp->rq_vers, nfserr);
521         if (nfserr == nfserr_dropit) {
522                 dprintk("nfsd: Dropping request; may be revisited later\n");
523                 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
524                 return 0;
525         }
526
527         if (rqstp->rq_proc != 0)
528                 *nfserrp++ = nfserr;
529
530         /* Encode result.
531          * For NFSv2, additional info is never returned in case of an error.
532          */
533         if (!(nfserr && rqstp->rq_vers == 2)) {
534                 xdr = proc->pc_encode;
535                 if (xdr && !xdr(rqstp, nfserrp,
536                                 rqstp->rq_resp)) {
537                         /* Failed to encode result. Release cache entry */
538                         dprintk("nfsd: failed to encode result!\n");
539                         nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
540                         *statp = rpc_system_err;
541                         return 1;
542                 }
543         }
544
545         /* Store reply in cache. */
546         nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
547         return 1;
548 }