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>
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>
17 #include <linux/uio.h>
18 #include <linux/unistd.h>
19 #include <linux/slab.h>
20 #include <linux/smp.h>
21 #include <linux/freezer.h>
22 #include <linux/fs_struct.h>
23 #include <linux/kthread.h>
24 #include <linux/swap.h>
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>
38 #define NFSDDBG_FACILITY NFSDDBG_SVC
40 extern struct svc_program nfsd_program
;
41 static int nfsd(void *vrqstp
);
42 struct timeval nfssvc_boot
;
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
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[].
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
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:
62 * user_recovery_dirname
66 DEFINE_MUTEX(nfsd_mutex
);
67 struct svc_serv
*nfsd_serv
;
70 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
71 * nfsd_drc_max_pages limits the total amount of memory available for
72 * version 4.1 DRC caches.
73 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
75 spinlock_t nfsd_drc_lock
;
76 unsigned int nfsd_drc_max_mem
;
77 unsigned int nfsd_drc_mem_used
;
79 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
80 static struct svc_stat nfsd_acl_svcstats
;
81 static struct svc_version
* nfsd_acl_version
[] = {
82 [2] = &nfsd_acl_version2
,
83 [3] = &nfsd_acl_version3
,
86 #define NFSD_ACL_MINVERS 2
87 #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
88 static struct svc_version
*nfsd_acl_versions
[NFSD_ACL_NRVERS
];
90 static struct svc_program nfsd_acl_program
= {
91 .pg_prog
= NFS_ACL_PROGRAM
,
92 .pg_nvers
= NFSD_ACL_NRVERS
,
93 .pg_vers
= nfsd_acl_versions
,
96 .pg_stats
= &nfsd_acl_svcstats
,
97 .pg_authenticate
= &svc_set_client
,
100 static struct svc_stat nfsd_acl_svcstats
= {
101 .program
= &nfsd_acl_program
,
103 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
105 static struct svc_version
* nfsd_version
[] = {
106 [2] = &nfsd_version2
,
107 #if defined(CONFIG_NFSD_V3)
108 [3] = &nfsd_version3
,
110 #if defined(CONFIG_NFSD_V4)
111 [4] = &nfsd_version4
,
115 #define NFSD_MINVERS 2
116 #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
117 static struct svc_version
*nfsd_versions
[NFSD_NRVERS
];
119 struct svc_program nfsd_program
= {
120 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
121 .pg_next
= &nfsd_acl_program
,
123 .pg_prog
= NFS_PROGRAM
, /* program number */
124 .pg_nvers
= NFSD_NRVERS
, /* nr of entries in nfsd_version */
125 .pg_vers
= nfsd_versions
, /* version table */
126 .pg_name
= "nfsd", /* program name */
127 .pg_class
= "nfsd", /* authentication class */
128 .pg_stats
= &nfsd_svcstats
, /* version table */
129 .pg_authenticate
= &svc_set_client
, /* export authentication */
133 u32 nfsd_supported_minorversion
;
135 int nfsd_vers(int vers
, enum vers_op change
)
137 if (vers
< NFSD_MINVERS
|| vers
>= NFSD_NRVERS
)
141 nfsd_versions
[vers
] = nfsd_version
[vers
];
142 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
143 if (vers
< NFSD_ACL_NRVERS
)
144 nfsd_acl_versions
[vers
] = nfsd_acl_version
[vers
];
148 nfsd_versions
[vers
] = NULL
;
149 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
150 if (vers
< NFSD_ACL_NRVERS
)
151 nfsd_acl_versions
[vers
] = NULL
;
155 return nfsd_versions
[vers
] != NULL
;
157 return nfsd_version
[vers
] != NULL
;
162 int nfsd_minorversion(u32 minorversion
, enum vers_op change
)
164 if (minorversion
> NFSD_SUPPORTED_MINOR_VERSION
)
168 nfsd_supported_minorversion
= minorversion
;
171 if (minorversion
== 0)
173 nfsd_supported_minorversion
= minorversion
- 1;
176 return minorversion
<= nfsd_supported_minorversion
;
178 return minorversion
<= NFSD_SUPPORTED_MINOR_VERSION
;
184 * Maximum number of nfsd processes
186 #define NFSD_MAXSERVS 8192
188 int nfsd_nrthreads(void)
191 mutex_lock(&nfsd_mutex
);
193 rv
= nfsd_serv
->sv_nrthreads
;
194 mutex_unlock(&nfsd_mutex
);
198 static void nfsd_last_thread(struct svc_serv
*serv
)
200 /* When last nfsd thread exits we need to do some clean-up */
201 struct svc_xprt
*xprt
;
202 list_for_each_entry(xprt
, &serv
->sv_permsocks
, xpt_list
)
205 nfsd_racache_shutdown();
206 nfs4_state_shutdown();
208 printk(KERN_WARNING
"nfsd: last server has exited, flushing export "
213 void nfsd_reset_versions(void)
218 for (i
= NFSD_MINVERS
; i
< NFSD_NRVERS
; i
++) {
219 if (nfsd_program
.pg_vers
[i
])
224 for (i
= NFSD_MINVERS
; i
< NFSD_NRVERS
; i
++)
225 nfsd_program
.pg_vers
[i
] = nfsd_version
[i
];
226 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
227 for (i
= NFSD_ACL_MINVERS
; i
< NFSD_ACL_NRVERS
; i
++)
228 nfsd_acl_program
.pg_vers
[i
] =
235 * Each session guarantees a negotiated per slot memory cache for replies
236 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
237 * NFSv4.1 server might want to use more memory for a DRC than a machine
238 * with mutiple services.
240 * Impose a hard limit on the number of pages for the DRC which varies
241 * according to the machines free pages. This is of course only a default.
243 * For now this is a #defined shift which could be under admin control
246 static void set_max_drc(void)
248 #define NFSD_DRC_SIZE_SHIFT 10
249 nfsd_drc_max_mem
= (nr_free_buffer_pages()
250 >> NFSD_DRC_SIZE_SHIFT
) * PAGE_SIZE
;
251 nfsd_drc_mem_used
= 0;
252 spin_lock_init(&nfsd_drc_lock
);
253 dprintk("%s nfsd_drc_max_mem %u \n", __func__
, nfsd_drc_max_mem
);
256 int nfsd_create_serv(void)
260 WARN_ON(!mutex_is_locked(&nfsd_mutex
));
265 if (nfsd_max_blksize
== 0) {
266 /* choose a suitable default */
269 /* Aim for 1/4096 of memory per thread
270 * This gives 1MB on 4Gig machines
271 * But only uses 32K on 128M machines.
272 * Bottom out at 8K on 32M and smaller.
273 * Of course, this is only a default.
275 nfsd_max_blksize
= NFSSVC_MAXBLKSIZE
;
276 i
.totalram
<<= PAGE_SHIFT
- 12;
277 while (nfsd_max_blksize
> i
.totalram
&&
278 nfsd_max_blksize
>= 8*1024*2)
279 nfsd_max_blksize
/= 2;
282 nfsd_serv
= svc_create_pooled(&nfsd_program
, nfsd_max_blksize
,
283 nfsd_last_thread
, nfsd
, THIS_MODULE
);
284 if (nfsd_serv
== NULL
)
289 do_gettimeofday(&nfssvc_boot
); /* record boot time */
293 static int nfsd_init_socks(int port
)
296 if (!list_empty(&nfsd_serv
->sv_permsocks
))
299 error
= svc_create_xprt(nfsd_serv
, "udp", PF_INET
, port
,
308 error
= svc_create_xprt(nfsd_serv
, "tcp", PF_INET
, port
,
320 int nfsd_nrpools(void)
322 if (nfsd_serv
== NULL
)
325 return nfsd_serv
->sv_nrpools
;
328 int nfsd_get_nrthreads(int n
, int *nthreads
)
332 if (nfsd_serv
!= NULL
) {
333 for (i
= 0; i
< nfsd_serv
->sv_nrpools
&& i
< n
; i
++)
334 nthreads
[i
] = nfsd_serv
->sv_pools
[i
].sp_nrthreads
;
340 int nfsd_set_nrthreads(int n
, int *nthreads
)
346 WARN_ON(!mutex_is_locked(&nfsd_mutex
));
348 if (nfsd_serv
== NULL
|| n
<= 0)
351 if (n
> nfsd_serv
->sv_nrpools
)
352 n
= nfsd_serv
->sv_nrpools
;
354 /* enforce a global maximum number of threads */
356 for (i
= 0; i
< n
; i
++) {
357 if (nthreads
[i
] > NFSD_MAXSERVS
)
358 nthreads
[i
] = NFSD_MAXSERVS
;
361 if (tot
> NFSD_MAXSERVS
) {
362 /* total too large: scale down requested numbers */
363 for (i
= 0; i
< n
&& tot
> 0; i
++) {
364 int new = nthreads
[i
] * NFSD_MAXSERVS
/ tot
;
365 tot
-= (nthreads
[i
] - new);
368 for (i
= 0; i
< n
&& tot
> 0; i
++) {
375 * There must always be a thread in pool 0; the admin
376 * can't shut down NFS completely using pool_threads.
378 if (nthreads
[0] == 0)
381 /* apply the new numbers */
383 for (i
= 0; i
< n
; i
++) {
384 err
= svc_set_num_threads(nfsd_serv
, &nfsd_serv
->sv_pools
[i
],
389 svc_destroy(nfsd_serv
);
395 nfsd_svc(unsigned short port
, int nrservs
)
399 mutex_lock(&nfsd_mutex
);
400 dprintk("nfsd: creating service\n");
403 if (nrservs
> NFSD_MAXSERVS
)
404 nrservs
= NFSD_MAXSERVS
;
406 if (nrservs
== 0 && nfsd_serv
== NULL
)
409 /* Readahead param cache - will no-op if it already exists */
410 error
= nfsd_racache_init(2*nrservs
);
415 nfsd_reset_versions();
417 error
= nfsd_create_serv();
421 error
= nfsd_init_socks(port
);
425 error
= svc_set_num_threads(nfsd_serv
, NULL
, nrservs
);
427 /* We are holding a reference to nfsd_serv which
428 * we don't want to count in the return value,
431 error
= nfsd_serv
->sv_nrthreads
- 1;
433 svc_destroy(nfsd_serv
); /* Release server */
435 mutex_unlock(&nfsd_mutex
);
441 * This is the NFS server kernel thread
446 struct svc_rqst
*rqstp
= (struct svc_rqst
*) vrqstp
;
447 int err
, preverr
= 0;
449 /* Lock module and set up kernel thread */
450 mutex_lock(&nfsd_mutex
);
452 /* At this point, the thread shares current->fs
453 * with the init process. We need to create files with a
454 * umask of 0 instead of init's umask. */
455 if (unshare_fs_struct() < 0) {
456 printk("Unable to start nfsd thread: out of memory\n");
460 current
->fs
->umask
= 0;
463 * thread is spawned with all signals set to SIG_IGN, re-enable
464 * the ones that will bring down the thread
466 allow_signal(SIGKILL
);
467 allow_signal(SIGHUP
);
468 allow_signal(SIGINT
);
469 allow_signal(SIGQUIT
);
472 mutex_unlock(&nfsd_mutex
);
475 * We want less throttling in balance_dirty_pages() so that nfs to
476 * localhost doesn't cause nfsd to lock up due to all the client's
479 current
->flags
|= PF_LESS_THROTTLE
;
483 * The main request loop
487 * Find a socket with data available and call its
490 while ((err
= svc_recv(rqstp
, 60*60*HZ
)) == -EAGAIN
)
495 if (err
!= preverr
) {
496 printk(KERN_WARNING
"%s: unexpected error "
497 "from svc_recv (%d)\n", __func__
, -err
);
500 schedule_timeout_uninterruptible(HZ
);
505 /* Lock the export hash tables for reading. */
510 /* Unlock export hash tables */
514 /* Clear signals before calling svc_exit_thread() */
515 flush_signals(current
);
517 mutex_lock(&nfsd_mutex
);
521 /* Release the thread */
522 svc_exit_thread(rqstp
);
525 mutex_unlock(&nfsd_mutex
);
526 module_put_and_exit(0);
530 static __be32
map_new_errors(u32 vers
, __be32 nfserr
)
532 if (nfserr
== nfserr_jukebox
&& vers
== 2)
533 return nfserr_dropit
;
534 if (nfserr
== nfserr_wrongsec
&& vers
< 4)
540 nfsd_dispatch(struct svc_rqst
*rqstp
, __be32
*statp
)
542 struct svc_procedure
*proc
;
547 dprintk("nfsd_dispatch: vers %d proc %d\n",
548 rqstp
->rq_vers
, rqstp
->rq_proc
);
549 proc
= rqstp
->rq_procinfo
;
551 /* Check whether we have this call in the cache. */
552 switch (nfsd_cache_lookup(rqstp
, proc
->pc_cachetype
)) {
562 /* Decode arguments */
563 xdr
= proc
->pc_decode
;
564 if (xdr
&& !xdr(rqstp
, (__be32
*)rqstp
->rq_arg
.head
[0].iov_base
,
566 dprintk("nfsd: failed to decode arguments!\n");
567 nfsd_cache_update(rqstp
, RC_NOCACHE
, NULL
);
568 *statp
= rpc_garbage_args
;
572 /* need to grab the location to store the status, as
573 * nfsv4 does some encoding while processing
575 nfserrp
= rqstp
->rq_res
.head
[0].iov_base
576 + rqstp
->rq_res
.head
[0].iov_len
;
577 rqstp
->rq_res
.head
[0].iov_len
+= sizeof(__be32
);
579 /* NFSv4.1 DRC requires statp */
580 if (rqstp
->rq_vers
== 4)
581 nfsd4_set_statp(rqstp
, statp
);
583 /* Now call the procedure handler, and encode NFS status. */
584 nfserr
= proc
->pc_func(rqstp
, rqstp
->rq_argp
, rqstp
->rq_resp
);
585 nfserr
= map_new_errors(rqstp
->rq_vers
, nfserr
);
586 if (nfserr
== nfserr_dropit
) {
587 dprintk("nfsd: Dropping request; may be revisited later\n");
588 nfsd_cache_update(rqstp
, RC_NOCACHE
, NULL
);
592 if (rqstp
->rq_proc
!= 0)
596 * For NFSv2, additional info is never returned in case of an error.
598 if (!(nfserr
&& rqstp
->rq_vers
== 2)) {
599 xdr
= proc
->pc_encode
;
600 if (xdr
&& !xdr(rqstp
, nfserrp
,
602 /* Failed to encode result. Release cache entry */
603 dprintk("nfsd: failed to encode result!\n");
604 nfsd_cache_update(rqstp
, RC_NOCACHE
, NULL
);
605 *statp
= rpc_system_err
;
610 /* Store reply in cache. */
611 nfsd_cache_update(rqstp
, proc
->pc_cachetype
, statp
+ 1);
615 int nfsd_pool_stats_open(struct inode
*inode
, struct file
*file
)
617 if (nfsd_serv
== NULL
)
619 return svc_pool_stats_open(nfsd_serv
, file
);