[SERIAL] sunhv: Convert to of_driver layer.
[linux/fpc-iii.git] / fs / nfsd / nfssvc.c
blob3790727e5dfdb8a8fb94ba4c5c7ca497d4ae6ae4
1 /*
2 * linux/fs/nfsd/nfssvc.c
4 * Central processing for nfsd.
6 * Authors: Olaf Kirch (okir@monad.swb.de)
8 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
9 */
11 #include <linux/config.h>
12 #include <linux/module.h>
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>
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>
37 #define NFSDDBG_FACILITY NFSDDBG_SVC
39 /* these signals will be delivered to an nfsd thread
40 * when handling a request
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
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).
51 #define SIG_NOCLEAN SIGHUP
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);
61 struct nfsd_list {
62 struct list_head list;
63 struct task_struct *task;
65 static struct list_head nfsd_list = LIST_HEAD_INIT(nfsd_list);
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,
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];
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,
88 static struct svc_stat nfsd_acl_svcstats = {
89 .program = &nfsd_acl_program,
91 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
93 static struct svc_version * nfsd_version[] = {
94 [2] = &nfsd_version2,
95 #if defined(CONFIG_NFSD_V3)
96 [3] = &nfsd_version3,
97 #endif
98 #if defined(CONFIG_NFSD_V4)
99 [4] = &nfsd_version4,
100 #endif
103 #define NFSD_MINVERS 2
104 #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
105 static struct svc_version *nfsd_versions[NFSD_NRVERS];
107 struct svc_program nfsd_program = {
108 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
109 .pg_next = &nfsd_acl_program,
110 #endif
111 .pg_prog = NFS_PROGRAM, /* program number */
112 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
113 .pg_vers = nfsd_versions, /* version table */
114 .pg_name = "nfsd", /* program name */
115 .pg_class = "nfsd", /* authentication class */
116 .pg_stats = &nfsd_svcstats, /* version table */
117 .pg_authenticate = &svc_set_client, /* export authentication */
122 * Maximum number of nfsd processes
124 #define NFSD_MAXSERVS 8192
126 int nfsd_nrthreads(void)
128 if (nfsd_serv == NULL)
129 return 0;
130 else
131 return nfsd_serv->sv_nrthreads;
135 nfsd_svc(unsigned short port, int nrservs)
137 int error;
138 int none_left, found_one, i;
139 struct list_head *victim;
141 lock_kernel();
142 dprintk("nfsd: creating service: vers 0x%x\n",
143 nfsd_versbits);
144 error = -EINVAL;
145 if (nrservs <= 0)
146 nrservs = 0;
147 if (nrservs > NFSD_MAXSERVS)
148 nrservs = NFSD_MAXSERVS;
150 /* Readahead param cache - will no-op if it already exists */
151 error = nfsd_racache_init(2*nrservs);
152 if (error<0)
153 goto out;
154 error = nfs4_state_start();
155 if (error<0)
156 goto out;
157 if (!nfsd_serv) {
159 * Use the nfsd_ctlbits to define which
160 * versions that will be advertised.
161 * If nfsd_ctlbits doesn't list any version,
162 * export them all.
164 found_one = 0;
166 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
167 if (NFSCTL_VERISSET(nfsd_versbits, i)) {
168 nfsd_program.pg_vers[i] = nfsd_version[i];
169 found_one = 1;
170 } else
171 nfsd_program.pg_vers[i] = NULL;
174 if (!found_one) {
175 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++)
176 nfsd_program.pg_vers[i] = nfsd_version[i];
180 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
181 found_one = 0;
183 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
184 if (NFSCTL_VERISSET(nfsd_versbits, i)) {
185 nfsd_acl_program.pg_vers[i] =
186 nfsd_acl_version[i];
187 found_one = 1;
188 } else
189 nfsd_acl_program.pg_vers[i] = NULL;
192 if (!found_one) {
193 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++)
194 nfsd_acl_program.pg_vers[i] =
195 nfsd_acl_version[i];
197 #endif
199 atomic_set(&nfsd_busy, 0);
200 error = -ENOMEM;
201 nfsd_serv = svc_create(&nfsd_program, NFSD_BUFSIZE);
202 if (nfsd_serv == NULL)
203 goto out;
204 error = svc_makesock(nfsd_serv, IPPROTO_UDP, port);
205 if (error < 0)
206 goto failure;
208 #ifdef CONFIG_NFSD_TCP
209 error = svc_makesock(nfsd_serv, IPPROTO_TCP, port);
210 if (error < 0)
211 goto failure;
212 #endif
213 do_gettimeofday(&nfssvc_boot); /* record boot time */
214 } else
215 nfsd_serv->sv_nrthreads++;
216 nrservs -= (nfsd_serv->sv_nrthreads-1);
217 while (nrservs > 0) {
218 nrservs--;
219 __module_get(THIS_MODULE);
220 error = svc_create_thread(nfsd, nfsd_serv);
221 if (error < 0) {
222 module_put(THIS_MODULE);
223 break;
226 victim = nfsd_list.next;
227 while (nrservs < 0 && victim != &nfsd_list) {
228 struct nfsd_list *nl =
229 list_entry(victim,struct nfsd_list, list);
230 victim = victim->next;
231 send_sig(SIG_NOCLEAN, nl->task, 1);
232 nrservs++;
234 failure:
235 none_left = (nfsd_serv->sv_nrthreads == 1);
236 svc_destroy(nfsd_serv); /* Release server */
237 if (none_left) {
238 nfsd_serv = NULL;
239 nfsd_racache_shutdown();
240 nfs4_state_shutdown();
242 out:
243 unlock_kernel();
244 return error;
247 static inline void
248 update_thread_usage(int busy_threads)
250 unsigned long prev_call;
251 unsigned long diff;
252 int decile;
254 spin_lock(&nfsd_call_lock);
255 prev_call = nfsd_last_call;
256 nfsd_last_call = jiffies;
257 decile = busy_threads*10/nfsdstats.th_cnt;
258 if (decile>0 && decile <= 10) {
259 diff = nfsd_last_call - prev_call;
260 if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP)
261 nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP;
262 if (decile == 10)
263 nfsdstats.th_fullcnt++;
265 spin_unlock(&nfsd_call_lock);
269 * This is the NFS server kernel thread
271 static void
272 nfsd(struct svc_rqst *rqstp)
274 struct svc_serv *serv = rqstp->rq_server;
275 struct fs_struct *fsp;
276 int err;
277 struct nfsd_list me;
278 sigset_t shutdown_mask, allowed_mask;
280 /* Lock module and set up kernel thread */
281 lock_kernel();
282 daemonize("nfsd");
284 /* After daemonize() this kernel thread shares current->fs
285 * with the init process. We need to create files with a
286 * umask of 0 instead of init's umask. */
287 fsp = copy_fs_struct(current->fs);
288 if (!fsp) {
289 printk("Unable to start nfsd thread: out of memory\n");
290 goto out;
292 exit_fs(current);
293 current->fs = fsp;
294 current->fs->umask = 0;
296 siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS);
297 siginitsetinv(&allowed_mask, ALLOWED_SIGS);
299 nfsdstats.th_cnt++;
301 lockd_up(); /* start lockd */
303 me.task = current;
304 list_add(&me.list, &nfsd_list);
306 unlock_kernel();
309 * We want less throttling in balance_dirty_pages() so that nfs to
310 * localhost doesn't cause nfsd to lock up due to all the client's
311 * dirty pages.
313 current->flags |= PF_LESS_THROTTLE;
316 * The main request loop
318 for (;;) {
319 /* Block all but the shutdown signals */
320 sigprocmask(SIG_SETMASK, &shutdown_mask, NULL);
323 * Find a socket with data available and call its
324 * recvfrom routine.
326 while ((err = svc_recv(serv, rqstp,
327 60*60*HZ)) == -EAGAIN)
329 if (err < 0)
330 break;
331 update_thread_usage(atomic_read(&nfsd_busy));
332 atomic_inc(&nfsd_busy);
334 /* Lock the export hash tables for reading. */
335 exp_readlock();
337 /* Process request with signals blocked. */
338 sigprocmask(SIG_SETMASK, &allowed_mask, NULL);
340 svc_process(serv, rqstp);
342 /* Unlock export hash tables */
343 exp_readunlock();
344 update_thread_usage(atomic_read(&nfsd_busy));
345 atomic_dec(&nfsd_busy);
348 if (err != -EINTR) {
349 printk(KERN_WARNING "nfsd: terminating on error %d\n", -err);
350 } else {
351 unsigned int signo;
353 for (signo = 1; signo <= _NSIG; signo++)
354 if (sigismember(&current->pending.signal, signo) &&
355 !sigismember(&current->blocked, signo))
356 break;
357 err = signo;
359 /* Clear signals before calling lockd_down() and svc_exit_thread() */
360 flush_signals(current);
362 lock_kernel();
364 /* Release lockd */
365 lockd_down();
367 /* Check if this is last thread */
368 if (serv->sv_nrthreads==1) {
370 printk(KERN_WARNING "nfsd: last server has exited\n");
371 if (err != SIG_NOCLEAN) {
372 printk(KERN_WARNING "nfsd: unexporting all filesystems\n");
373 nfsd_export_flush();
375 nfsd_serv = NULL;
376 nfsd_racache_shutdown(); /* release read-ahead cache */
377 nfs4_state_shutdown();
379 list_del(&me.list);
380 nfsdstats.th_cnt --;
382 out:
383 /* Release the thread */
384 svc_exit_thread(rqstp);
386 /* Release module */
387 unlock_kernel();
388 module_put_and_exit(0);
392 nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp)
394 struct svc_procedure *proc;
395 kxdrproc_t xdr;
396 u32 nfserr;
397 u32 *nfserrp;
399 dprintk("nfsd_dispatch: vers %d proc %d\n",
400 rqstp->rq_vers, rqstp->rq_proc);
401 proc = rqstp->rq_procinfo;
403 /* Check whether we have this call in the cache. */
404 switch (nfsd_cache_lookup(rqstp, proc->pc_cachetype)) {
405 case RC_INTR:
406 case RC_DROPIT:
407 return 0;
408 case RC_REPLY:
409 return 1;
410 case RC_DOIT:;
411 /* do it */
414 /* Decode arguments */
415 xdr = proc->pc_decode;
416 if (xdr && !xdr(rqstp, (u32*)rqstp->rq_arg.head[0].iov_base,
417 rqstp->rq_argp)) {
418 dprintk("nfsd: failed to decode arguments!\n");
419 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
420 *statp = rpc_garbage_args;
421 return 1;
424 /* need to grab the location to store the status, as
425 * nfsv4 does some encoding while processing
427 nfserrp = rqstp->rq_res.head[0].iov_base
428 + rqstp->rq_res.head[0].iov_len;
429 rqstp->rq_res.head[0].iov_len += sizeof(u32);
431 /* Now call the procedure handler, and encode NFS status. */
432 nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
433 if (nfserr == nfserr_jukebox && rqstp->rq_vers == 2)
434 nfserr = nfserr_dropit;
435 if (nfserr == nfserr_dropit) {
436 dprintk("nfsd: Dropping request due to malloc failure!\n");
437 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
438 return 0;
441 if (rqstp->rq_proc != 0)
442 *nfserrp++ = nfserr;
444 /* Encode result.
445 * For NFSv2, additional info is never returned in case of an error.
447 if (!(nfserr && rqstp->rq_vers == 2)) {
448 xdr = proc->pc_encode;
449 if (xdr && !xdr(rqstp, nfserrp,
450 rqstp->rq_resp)) {
451 /* Failed to encode result. Release cache entry */
452 dprintk("nfsd: failed to encode result!\n");
453 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
454 *statp = rpc_system_err;
455 return 1;
459 /* Store reply in cache. */
460 nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
461 return 1;